rtc-snvs.c 10 KB

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