rtc-snvs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. int ret;
  113. rtc_tm_to_time(tm, &time);
  114. /* Disable RTC first */
  115. ret = snvs_rtc_enable(data, false);
  116. if (ret)
  117. return ret;
  118. /* Write 32-bit time to 47-bit timer, leaving 15 LSBs blank */
  119. regmap_write(data->regmap, data->offset + SNVS_LPSRTCLR, time << CNTR_TO_SECS_SH);
  120. regmap_write(data->regmap, data->offset + SNVS_LPSRTCMR, time >> (32 - CNTR_TO_SECS_SH));
  121. /* Enable RTC again */
  122. ret = snvs_rtc_enable(data, true);
  123. return ret;
  124. }
  125. static int snvs_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  126. {
  127. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  128. u32 lptar, lpsr;
  129. regmap_read(data->regmap, data->offset + SNVS_LPTAR, &lptar);
  130. rtc_time_to_tm(lptar, &alrm->time);
  131. regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr);
  132. alrm->pending = (lpsr & SNVS_LPSR_LPTA) ? 1 : 0;
  133. return 0;
  134. }
  135. static int snvs_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  136. {
  137. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  138. regmap_update_bits(data->regmap, data->offset + SNVS_LPCR,
  139. (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN),
  140. enable ? (SNVS_LPCR_LPTA_EN | SNVS_LPCR_LPWUI_EN) : 0);
  141. rtc_write_sync_lp(data);
  142. return 0;
  143. }
  144. static int snvs_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  145. {
  146. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  147. struct rtc_time *alrm_tm = &alrm->time;
  148. unsigned long time;
  149. rtc_tm_to_time(alrm_tm, &time);
  150. regmap_update_bits(data->regmap, data->offset + SNVS_LPCR, SNVS_LPCR_LPTA_EN, 0);
  151. regmap_write(data->regmap, data->offset + SNVS_LPTAR, time);
  152. /* Clear alarm interrupt status bit */
  153. regmap_write(data->regmap, data->offset + SNVS_LPSR, SNVS_LPSR_LPTA);
  154. return snvs_rtc_alarm_irq_enable(dev, alrm->enabled);
  155. }
  156. static const struct rtc_class_ops snvs_rtc_ops = {
  157. .read_time = snvs_rtc_read_time,
  158. .set_time = snvs_rtc_set_time,
  159. .read_alarm = snvs_rtc_read_alarm,
  160. .set_alarm = snvs_rtc_set_alarm,
  161. .alarm_irq_enable = snvs_rtc_alarm_irq_enable,
  162. };
  163. static irqreturn_t snvs_rtc_irq_handler(int irq, void *dev_id)
  164. {
  165. struct device *dev = dev_id;
  166. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  167. u32 lpsr;
  168. u32 events = 0;
  169. regmap_read(data->regmap, data->offset + SNVS_LPSR, &lpsr);
  170. if (lpsr & SNVS_LPSR_LPTA) {
  171. events |= (RTC_AF | RTC_IRQF);
  172. /* RTC alarm should be one-shot */
  173. snvs_rtc_alarm_irq_enable(dev, 0);
  174. rtc_update_irq(data->rtc, 1, events);
  175. }
  176. /* clear interrupt status */
  177. regmap_write(data->regmap, data->offset + SNVS_LPSR, lpsr);
  178. return events ? IRQ_HANDLED : IRQ_NONE;
  179. }
  180. static const struct regmap_config snvs_rtc_config = {
  181. .reg_bits = 32,
  182. .val_bits = 32,
  183. .reg_stride = 4,
  184. };
  185. static int snvs_rtc_probe(struct platform_device *pdev)
  186. {
  187. struct snvs_rtc_data *data;
  188. struct resource *res;
  189. int ret;
  190. void __iomem *mmio;
  191. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  192. if (!data)
  193. return -ENOMEM;
  194. data->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap");
  195. if (IS_ERR(data->regmap)) {
  196. dev_warn(&pdev->dev, "snvs rtc: you use old dts file, please update it\n");
  197. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  198. mmio = devm_ioremap_resource(&pdev->dev, res);
  199. if (IS_ERR(mmio))
  200. return PTR_ERR(mmio);
  201. data->regmap = devm_regmap_init_mmio(&pdev->dev, mmio, &snvs_rtc_config);
  202. } else {
  203. data->offset = SNVS_LPREGISTER_OFFSET;
  204. of_property_read_u32(pdev->dev.of_node, "offset", &data->offset);
  205. }
  206. if (IS_ERR(data->regmap)) {
  207. dev_err(&pdev->dev, "Can't find snvs syscon\n");
  208. return -ENODEV;
  209. }
  210. data->irq = platform_get_irq(pdev, 0);
  211. if (data->irq < 0)
  212. return data->irq;
  213. data->clk = devm_clk_get(&pdev->dev, "snvs-rtc");
  214. if (IS_ERR(data->clk)) {
  215. data->clk = NULL;
  216. } else {
  217. ret = clk_prepare_enable(data->clk);
  218. if (ret) {
  219. dev_err(&pdev->dev,
  220. "Could not prepare or enable the snvs clock\n");
  221. return ret;
  222. }
  223. }
  224. platform_set_drvdata(pdev, data);
  225. /* Initialize glitch detect */
  226. regmap_write(data->regmap, data->offset + SNVS_LPPGDR, SNVS_LPPGDR_INIT);
  227. /* Clear interrupt status */
  228. regmap_write(data->regmap, data->offset + SNVS_LPSR, 0xffffffff);
  229. /* Enable RTC */
  230. ret = snvs_rtc_enable(data, true);
  231. if (ret) {
  232. dev_err(&pdev->dev, "failed to enable rtc %d\n", ret);
  233. goto error_rtc_device_register;
  234. }
  235. device_init_wakeup(&pdev->dev, true);
  236. ret = devm_request_irq(&pdev->dev, data->irq, snvs_rtc_irq_handler,
  237. IRQF_SHARED, "rtc alarm", &pdev->dev);
  238. if (ret) {
  239. dev_err(&pdev->dev, "failed to request irq %d: %d\n",
  240. data->irq, ret);
  241. goto error_rtc_device_register;
  242. }
  243. data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  244. &snvs_rtc_ops, THIS_MODULE);
  245. if (IS_ERR(data->rtc)) {
  246. ret = PTR_ERR(data->rtc);
  247. dev_err(&pdev->dev, "failed to register rtc: %d\n", ret);
  248. goto error_rtc_device_register;
  249. }
  250. return 0;
  251. error_rtc_device_register:
  252. if (data->clk)
  253. clk_disable_unprepare(data->clk);
  254. return ret;
  255. }
  256. #ifdef CONFIG_PM_SLEEP
  257. static int snvs_rtc_suspend(struct device *dev)
  258. {
  259. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  260. if (device_may_wakeup(dev))
  261. return enable_irq_wake(data->irq);
  262. return 0;
  263. }
  264. static int snvs_rtc_suspend_noirq(struct device *dev)
  265. {
  266. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  267. if (data->clk)
  268. clk_disable_unprepare(data->clk);
  269. return 0;
  270. }
  271. static int snvs_rtc_resume(struct device *dev)
  272. {
  273. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  274. if (device_may_wakeup(dev))
  275. return disable_irq_wake(data->irq);
  276. return 0;
  277. }
  278. static int snvs_rtc_resume_noirq(struct device *dev)
  279. {
  280. struct snvs_rtc_data *data = dev_get_drvdata(dev);
  281. if (data->clk)
  282. return clk_prepare_enable(data->clk);
  283. return 0;
  284. }
  285. static const struct dev_pm_ops snvs_rtc_pm_ops = {
  286. .suspend = snvs_rtc_suspend,
  287. .suspend_noirq = snvs_rtc_suspend_noirq,
  288. .resume = snvs_rtc_resume,
  289. .resume_noirq = snvs_rtc_resume_noirq,
  290. };
  291. #define SNVS_RTC_PM_OPS (&snvs_rtc_pm_ops)
  292. #else
  293. #define SNVS_RTC_PM_OPS NULL
  294. #endif
  295. static const struct of_device_id snvs_dt_ids[] = {
  296. { .compatible = "fsl,sec-v4.0-mon-rtc-lp", },
  297. { /* sentinel */ }
  298. };
  299. MODULE_DEVICE_TABLE(of, snvs_dt_ids);
  300. static struct platform_driver snvs_rtc_driver = {
  301. .driver = {
  302. .name = "snvs_rtc",
  303. .pm = SNVS_RTC_PM_OPS,
  304. .of_match_table = snvs_dt_ids,
  305. },
  306. .probe = snvs_rtc_probe,
  307. };
  308. module_platform_driver(snvs_rtc_driver);
  309. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  310. MODULE_DESCRIPTION("Freescale SNVS RTC Driver");
  311. MODULE_LICENSE("GPL");