rtc-snvs.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (C) 2011-2012 Freescale Semiconductor, Inc.
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #include <linux/init.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/rtc.h>
  19. #include <linux/clk.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/regmap.h>
  22. #define SNVS_LPREGISTER_OFFSET 0x34
  23. /* These register offsets are relative to LP (Low Power) range */
  24. #define SNVS_LPCR 0x04
  25. #define SNVS_LPSR 0x18
  26. #define SNVS_LPSRTCMR 0x1c
  27. #define SNVS_LPSRTCLR 0x20
  28. #define SNVS_LPTAR 0x24
  29. #define SNVS_LPPGDR 0x30
  30. #define SNVS_LPCR_SRTC_ENV (1 << 0)
  31. #define SNVS_LPCR_LPTA_EN (1 << 1)
  32. #define SNVS_LPCR_LPWUI_EN (1 << 3)
  33. #define SNVS_LPSR_LPTA (1 << 0)
  34. #define SNVS_LPPGDR_INIT 0x41736166
  35. #define CNTR_TO_SECS_SH 15
  36. struct snvs_rtc_data {
  37. struct rtc_device *rtc;
  38. struct regmap *regmap;
  39. int offset;
  40. int irq;
  41. struct clk *clk;
  42. };
  43. static u32 rtc_read_lp_counter(struct snvs_rtc_data *data)
  44. {
  45. u64 read1, read2;
  46. u32 val;
  47. do {
  48. regmap_read(data->regmap, data->offset + SNVS_LPSRTCMR, &val);
  49. read1 = val;
  50. read1 <<= 32;
  51. regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &val);
  52. read1 |= val;
  53. regmap_read(data->regmap, data->offset + SNVS_LPSRTCMR, &val);
  54. read2 = val;
  55. read2 <<= 32;
  56. regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &val);
  57. read2 |= val;
  58. } while (read1 != read2);
  59. /* Convert 47-bit counter to 32-bit raw second count */
  60. return (u32) (read1 >> CNTR_TO_SECS_SH);
  61. }
  62. static void rtc_write_sync_lp(struct snvs_rtc_data *data)
  63. {
  64. u32 count1, count2, count3;
  65. int i;
  66. /* Wait for 3 CKIL cycles */
  67. for (i = 0; i < 3; i++) {
  68. do {
  69. regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &count1);
  70. regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &count2);
  71. } while (count1 != count2);
  72. /* Now wait until counter value changes */
  73. do {
  74. do {
  75. regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &count2);
  76. regmap_read(data->regmap, data->offset + SNVS_LPSRTCLR, &count3);
  77. } while (count2 != count3);
  78. } while (count3 == count1);
  79. }
  80. }
  81. static int snvs_rtc_enable(struct snvs_rtc_data *data, bool enable)
  82. {
  83. int timeout = 1000;
  84. u32 lpcr;
  85. regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, SNVS_LPCR_SRTC_ENV,
  86. enable ? SNVS_LPCR_SRTC_ENV : 0);
  87. while (--timeout) {
  88. regmap_read(data->regmap, data->offset + SNVS_LPCR, &lpcr);
  89. if (enable) {
  90. if (lpcr & SNVS_LPCR_SRTC_ENV)
  91. break;
  92. } else {
  93. if (!(lpcr & SNVS_LPCR_SRTC_ENV))
  94. break;
  95. }
  96. }
  97. if (!timeout)
  98. return -ETIMEDOUT;
  99. return 0;
  100. }
  101. static int snvs_rtc_read_time(struct device *dev, struct rtc_time *tm)
  102. {
  103. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  104. unsigned long time = rtc_read_lp_counter(data);
  105. rtc_time_to_tm(time, tm);
  106. return 0;
  107. }
  108. static int snvs_rtc_set_time(struct device *dev, struct rtc_time *tm)
  109. {
  110. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  111. unsigned long time;
  112. rtc_tm_to_time(tm, &time);
  113. /* Disable RTC first */
  114. snvs_rtc_enable(data, false);
  115. /* Write 32-bit time to 47-bit timer, leaving 15 LSBs blank */
  116. regmap_write(data->regmap, data->offset + SNVS_LPSRTCLR, time << CNTR_TO_SECS_SH);
  117. regmap_write(data->regmap, data->offset + SNVS_LPSRTCMR, time >> (32 - CNTR_TO_SECS_SH));
  118. /* Enable RTC again */
  119. snvs_rtc_enable(data, true);
  120. return 0;
  121. }
  122. static int snvs_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  123. {
  124. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  125. u32 lptar, lpsr;
  126. regmap_read(data->regmap, data->offset + SNVS_LPTAR, &lptar);
  127. rtc_time_to_tm(lptar, &alrm->time);
  128. regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr);
  129. alrm->pending = (lpsr & SNVS_LPSR_LPTA) ? 1 : 0;
  130. return 0;
  131. }
  132. static int snvs_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  133. {
  134. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  135. regmap_update_bits(data->regmap, data->offset + SNVS_LPCR,
  136. (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN),
  137. enable ? (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN) : 0);
  138. rtc_write_sync_lp(data);
  139. return 0;
  140. }
  141. static int snvs_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  142. {
  143. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  144. struct rtc_time *alrm_tm = &alrm->time;
  145. unsigned long time;
  146. rtc_tm_to_time(alrm_tm, &time);
  147. regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, SNVS_LPCR_LPTA_EN, 0);
  148. regmap_write(data->regmap, data->offset + SNVS_LPTAR, time);
  149. /* Clear alarm interrupt status bit */
  150. regmap_write(data->regmap, data->offset + SNVS_LPSR, SNVS_LPSR_LPTA);
  151. return snvs_rtc_alarm_irq_enable(dev, alrm->enabled);
  152. }
  153. static const struct rtc_class_ops snvs_rtc_ops = {
  154. .read_time = snvs_rtc_read_time,
  155. .set_time = snvs_rtc_set_time,
  156. .read_alarm = snvs_rtc_read_alarm,
  157. .set_alarm = snvs_rtc_set_alarm,
  158. .alarm_irq_enable = snvs_rtc_alarm_irq_enable,
  159. };
  160. static irqreturn_t snvs_rtc_irq_handler(int irq, void *dev_id)
  161. {
  162. struct device *dev = dev_id;
  163. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  164. u32 lpsr;
  165. u32 events = 0;
  166. regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr);
  167. if (lpsr & SNVS_LPSR_LPTA) {
  168. events |= (RTC_AF | RTC_IRQF);
  169. /* RTC alarm should be one-shot */
  170. snvs_rtc_alarm_irq_enable(dev, 0);
  171. rtc_update_irq(data->rtc, 1, events);
  172. }
  173. /* clear interrupt status */
  174. regmap_write(data->regmap, data->offset + SNVS_LPSR, lpsr);
  175. return events ? IRQ_HANDLED : IRQ_NONE;
  176. }
  177. static const struct regmap_config snvs_rtc_config = {
  178. .reg_bits = 32,
  179. .val_bits = 32,
  180. .reg_stride = 4,
  181. };
  182. static int snvs_rtc_probe(struct platform_device *pdev)
  183. {
  184. struct snvs_rtc_data *data;
  185. struct resource *res;
  186. int ret;
  187. void __iomem *mmio;
  188. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  189. if (!data)
  190. return -ENOMEM;
  191. data->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap");
  192. if (IS_ERR(data->regmap)) {
  193. dev_warn(&pdev->dev, "snvs rtc: you use old dts file, please update it\n");
  194. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  195. mmio = devm_ioremap_resource(&pdev->dev, res);
  196. if (IS_ERR(mmio))
  197. return PTR_ERR(mmio);
  198. data->regmap = devm_regmap_init_mmio(&pdev->dev, mmio, &snvs_rtc_config);
  199. } else {
  200. data->offset = SNVS_LPREGISTER_OFFSET;
  201. of_property_read_u32(pdev->dev.of_node, "offset", &data->offset);
  202. }
  203. if (!data->regmap) {
  204. dev_err(&pdev->dev, "Can't find snvs syscon\n");
  205. return -ENODEV;
  206. }
  207. data->irq = platform_get_irq(pdev, 0);
  208. if (data->irq < 0)
  209. return data->irq;
  210. data->clk = devm_clk_get(&pdev->dev, "snvs-rtc");
  211. if (IS_ERR(data->clk)) {
  212. data->clk = NULL;
  213. } else {
  214. ret = clk_prepare_enable(data->clk);
  215. if (ret) {
  216. dev_err(&pdev->dev,
  217. "Could not prepare or enable the snvs clock\n");
  218. return ret;
  219. }
  220. }
  221. platform_set_drvdata(pdev, data);
  222. /* Initialize glitch detect */
  223. regmap_write(data->regmap, data->offset + SNVS_LPPGDR, SNVS_LPPGDR_INIT);
  224. /* Clear interrupt status */
  225. regmap_write(data->regmap, data->offset + SNVS_LPSR, 0xffffffff);
  226. /* Enable RTC */
  227. snvs_rtc_enable(data, true);
  228. device_init_wakeup(&pdev->dev, true);
  229. ret = devm_request_irq(&pdev->dev, data->irq, snvs_rtc_irq_handler,
  230. IRQF_SHARED, "rtc alarm", &pdev->dev);
  231. if (ret) {
  232. dev_err(&pdev->dev, "failed to request irq %d: %d\n",
  233. data->irq, ret);
  234. goto error_rtc_device_register;
  235. }
  236. data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  237. &snvs_rtc_ops, THIS_MODULE);
  238. if (IS_ERR(data->rtc)) {
  239. ret = PTR_ERR(data->rtc);
  240. dev_err(&pdev->dev, "failed to register rtc: %d\n", ret);
  241. goto error_rtc_device_register;
  242. }
  243. return 0;
  244. error_rtc_device_register:
  245. if (data->clk)
  246. clk_disable_unprepare(data->clk);
  247. return ret;
  248. }
  249. #ifdef CONFIG_PM_SLEEP
  250. static int snvs_rtc_suspend(struct device *dev)
  251. {
  252. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  253. if (device_may_wakeup(dev))
  254. return enable_irq_wake(data->irq);
  255. return 0;
  256. }
  257. static int snvs_rtc_suspend_noirq(struct device *dev)
  258. {
  259. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  260. if (data->clk)
  261. clk_disable_unprepare(data->clk);
  262. return 0;
  263. }
  264. static int snvs_rtc_resume(struct device *dev)
  265. {
  266. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  267. if (device_may_wakeup(dev))
  268. return disable_irq_wake(data->irq);
  269. return 0;
  270. }
  271. static int snvs_rtc_resume_noirq(struct device *dev)
  272. {
  273. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  274. if (data->clk)
  275. return clk_prepare_enable(data->clk);
  276. return 0;
  277. }
  278. static const struct dev_pm_ops snvs_rtc_pm_ops = {
  279. .suspend = snvs_rtc_suspend,
  280. .suspend_noirq = snvs_rtc_suspend_noirq,
  281. .resume = snvs_rtc_resume,
  282. .resume_noirq = snvs_rtc_resume_noirq,
  283. };
  284. #define SNVS_RTC_PM_OPS (&snvs_rtc_pm_ops)
  285. #else
  286. #define SNVS_RTC_PM_OPS NULL
  287. #endif
  288. static const struct of_device_id snvs_dt_ids[] = {
  289. { .compatible = "fsl,sec-v4.0-mon-rtc-lp", },
  290. { /* sentinel */ }
  291. };
  292. MODULE_DEVICE_TABLE(of, snvs_dt_ids);
  293. static struct platform_driver snvs_rtc_driver = {
  294. .driver = {
  295. .name = "snvs_rtc",
  296. .pm = SNVS_RTC_PM_OPS,
  297. .of_match_table = snvs_dt_ids,
  298. },
  299. .probe = snvs_rtc_probe,
  300. };
  301. module_platform_driver(snvs_rtc_driver);
  302. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  303. MODULE_DESCRIPTION("Freescale SNVS RTC Driver");
  304. MODULE_LICENSE("GPL");