rtc-snvs.c 10 KB

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