rtc-mxc_v2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Real Time Clock (RTC) Driver for i.MX53
  4. * Copyright (c) 2004-2011 Freescale Semiconductor, Inc.
  5. * Copyright (c) 2017 Beckhoff Automation GmbH & Co. KG
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/rtc.h>
  13. #define SRTC_LPPDR_INIT 0x41736166 /* init for glitch detect */
  14. #define SRTC_LPCR_EN_LP BIT(3) /* lp enable */
  15. #define SRTC_LPCR_WAE BIT(4) /* lp wakeup alarm enable */
  16. #define SRTC_LPCR_ALP BIT(7) /* lp alarm flag */
  17. #define SRTC_LPCR_NSA BIT(11) /* lp non secure access */
  18. #define SRTC_LPCR_NVE BIT(14) /* lp non valid state exit bit */
  19. #define SRTC_LPCR_IE BIT(15) /* lp init state exit bit */
  20. #define SRTC_LPSR_ALP BIT(3) /* lp alarm flag */
  21. #define SRTC_LPSR_NVES BIT(14) /* lp non-valid state exit status */
  22. #define SRTC_LPSR_IES BIT(15) /* lp init state exit status */
  23. #define SRTC_LPSCMR 0x00 /* LP Secure Counter MSB Reg */
  24. #define SRTC_LPSCLR 0x04 /* LP Secure Counter LSB Reg */
  25. #define SRTC_LPSAR 0x08 /* LP Secure Alarm Reg */
  26. #define SRTC_LPCR 0x10 /* LP Control Reg */
  27. #define SRTC_LPSR 0x14 /* LP Status Reg */
  28. #define SRTC_LPPDR 0x18 /* LP Power Supply Glitch Detector Reg */
  29. /* max. number of retries to read registers, 120 was max during test */
  30. #define REG_READ_TIMEOUT 2000
  31. struct mxc_rtc_data {
  32. struct rtc_device *rtc;
  33. void __iomem *ioaddr;
  34. struct clk *clk;
  35. spinlock_t lock; /* protects register access */
  36. int irq;
  37. };
  38. /*
  39. * This function does write synchronization for writes to the lp srtc block.
  40. * To take care of the asynchronous CKIL clock, all writes from the IP domain
  41. * will be synchronized to the CKIL domain.
  42. * The caller should hold the pdata->lock
  43. */
  44. static void mxc_rtc_sync_lp_locked(struct device *dev, void __iomem *ioaddr)
  45. {
  46. unsigned int i;
  47. /* Wait for 3 CKIL cycles */
  48. for (i = 0; i < 3; i++) {
  49. const u32 count = readl(ioaddr + SRTC_LPSCLR);
  50. unsigned int timeout = REG_READ_TIMEOUT;
  51. while ((readl(ioaddr + SRTC_LPSCLR)) == count) {
  52. if (!--timeout) {
  53. dev_err_once(dev, "SRTC_LPSCLR stuck! Check your hw.\n");
  54. return;
  55. }
  56. }
  57. }
  58. }
  59. /* This function is the RTC interrupt service routine. */
  60. static irqreturn_t mxc_rtc_interrupt(int irq, void *dev_id)
  61. {
  62. struct device *dev = dev_id;
  63. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  64. void __iomem *ioaddr = pdata->ioaddr;
  65. unsigned long flags;
  66. u32 lp_status;
  67. u32 lp_cr;
  68. spin_lock_irqsave(&pdata->lock, flags);
  69. if (clk_enable(pdata->clk)) {
  70. spin_unlock_irqrestore(&pdata->lock, flags);
  71. return IRQ_NONE;
  72. }
  73. lp_status = readl(ioaddr + SRTC_LPSR);
  74. lp_cr = readl(ioaddr + SRTC_LPCR);
  75. /* update irq data & counter */
  76. if (lp_status & SRTC_LPSR_ALP) {
  77. if (lp_cr & SRTC_LPCR_ALP)
  78. rtc_update_irq(pdata->rtc, 1, RTC_AF | RTC_IRQF);
  79. /* disable further lp alarm interrupts */
  80. lp_cr &= ~(SRTC_LPCR_ALP | SRTC_LPCR_WAE);
  81. }
  82. /* Update interrupt enables */
  83. writel(lp_cr, ioaddr + SRTC_LPCR);
  84. /* clear interrupt status */
  85. writel(lp_status, ioaddr + SRTC_LPSR);
  86. mxc_rtc_sync_lp_locked(dev, ioaddr);
  87. clk_disable(pdata->clk);
  88. spin_unlock_irqrestore(&pdata->lock, flags);
  89. return IRQ_HANDLED;
  90. }
  91. /*
  92. * Enable clk and aquire spinlock
  93. * @return 0 if successful; non-zero otherwise.
  94. */
  95. static int mxc_rtc_lock(struct mxc_rtc_data *const pdata)
  96. {
  97. int ret;
  98. spin_lock_irq(&pdata->lock);
  99. ret = clk_enable(pdata->clk);
  100. if (ret) {
  101. spin_unlock_irq(&pdata->lock);
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. static int mxc_rtc_unlock(struct mxc_rtc_data *const pdata)
  107. {
  108. clk_disable(pdata->clk);
  109. spin_unlock_irq(&pdata->lock);
  110. return 0;
  111. }
  112. /*
  113. * This function reads the current RTC time into tm in Gregorian date.
  114. *
  115. * @param tm contains the RTC time value upon return
  116. *
  117. * @return 0 if successful; non-zero otherwise.
  118. */
  119. static int mxc_rtc_read_time(struct device *dev, struct rtc_time *tm)
  120. {
  121. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  122. const int clk_failed = clk_enable(pdata->clk);
  123. if (!clk_failed) {
  124. const time64_t now = readl(pdata->ioaddr + SRTC_LPSCMR);
  125. rtc_time64_to_tm(now, tm);
  126. clk_disable(pdata->clk);
  127. return 0;
  128. }
  129. return clk_failed;
  130. }
  131. /*
  132. * This function sets the internal RTC time based on tm in Gregorian date.
  133. *
  134. * @param tm the time value to be set in the RTC
  135. *
  136. * @return 0 if successful; non-zero otherwise.
  137. */
  138. static int mxc_rtc_set_time(struct device *dev, struct rtc_time *tm)
  139. {
  140. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  141. time64_t time = rtc_tm_to_time64(tm);
  142. int ret;
  143. ret = mxc_rtc_lock(pdata);
  144. if (ret)
  145. return ret;
  146. writel(time, pdata->ioaddr + SRTC_LPSCMR);
  147. mxc_rtc_sync_lp_locked(dev, pdata->ioaddr);
  148. return mxc_rtc_unlock(pdata);
  149. }
  150. /*
  151. * This function reads the current alarm value into the passed in \b alrm
  152. * argument. It updates the \b alrm's pending field value based on the whether
  153. * an alarm interrupt occurs or not.
  154. *
  155. * @param alrm contains the RTC alarm value upon return
  156. *
  157. * @return 0 if successful; non-zero otherwise.
  158. */
  159. static int mxc_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  160. {
  161. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  162. void __iomem *ioaddr = pdata->ioaddr;
  163. int ret;
  164. ret = mxc_rtc_lock(pdata);
  165. if (ret)
  166. return ret;
  167. rtc_time64_to_tm(readl(ioaddr + SRTC_LPSAR), &alrm->time);
  168. alrm->pending = !!(readl(ioaddr + SRTC_LPSR) & SRTC_LPSR_ALP);
  169. return mxc_rtc_unlock(pdata);
  170. }
  171. /*
  172. * Enable/Disable alarm interrupt
  173. * The caller should hold the pdata->lock
  174. */
  175. static void mxc_rtc_alarm_irq_enable_locked(struct mxc_rtc_data *pdata,
  176. unsigned int enable)
  177. {
  178. u32 lp_cr = readl(pdata->ioaddr + SRTC_LPCR);
  179. if (enable)
  180. lp_cr |= (SRTC_LPCR_ALP | SRTC_LPCR_WAE);
  181. else
  182. lp_cr &= ~(SRTC_LPCR_ALP | SRTC_LPCR_WAE);
  183. writel(lp_cr, pdata->ioaddr + SRTC_LPCR);
  184. }
  185. static int mxc_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  186. {
  187. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  188. int ret = mxc_rtc_lock(pdata);
  189. if (ret)
  190. return ret;
  191. mxc_rtc_alarm_irq_enable_locked(pdata, enable);
  192. return mxc_rtc_unlock(pdata);
  193. }
  194. /*
  195. * This function sets the RTC alarm based on passed in alrm.
  196. *
  197. * @param alrm the alarm value to be set in the RTC
  198. *
  199. * @return 0 if successful; non-zero otherwise.
  200. */
  201. static int mxc_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  202. {
  203. const time64_t time = rtc_tm_to_time64(&alrm->time);
  204. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  205. int ret = mxc_rtc_lock(pdata);
  206. if (ret)
  207. return ret;
  208. writel((u32)time, pdata->ioaddr + SRTC_LPSAR);
  209. /* clear alarm interrupt status bit */
  210. writel(SRTC_LPSR_ALP, pdata->ioaddr + SRTC_LPSR);
  211. mxc_rtc_sync_lp_locked(dev, pdata->ioaddr);
  212. mxc_rtc_alarm_irq_enable_locked(pdata, alrm->enabled);
  213. mxc_rtc_sync_lp_locked(dev, pdata->ioaddr);
  214. mxc_rtc_unlock(pdata);
  215. return ret;
  216. }
  217. static const struct rtc_class_ops mxc_rtc_ops = {
  218. .read_time = mxc_rtc_read_time,
  219. .set_time = mxc_rtc_set_time,
  220. .read_alarm = mxc_rtc_read_alarm,
  221. .set_alarm = mxc_rtc_set_alarm,
  222. .alarm_irq_enable = mxc_rtc_alarm_irq_enable,
  223. };
  224. static int mxc_rtc_wait_for_flag(void __iomem *ioaddr, int flag)
  225. {
  226. unsigned int timeout = REG_READ_TIMEOUT;
  227. while (!(readl(ioaddr) & flag)) {
  228. if (!--timeout)
  229. return -EBUSY;
  230. }
  231. return 0;
  232. }
  233. static int mxc_rtc_probe(struct platform_device *pdev)
  234. {
  235. struct mxc_rtc_data *pdata;
  236. struct resource *res;
  237. void __iomem *ioaddr;
  238. int ret = 0;
  239. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  240. if (!pdata)
  241. return -ENOMEM;
  242. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  243. pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  244. if (IS_ERR(pdata->ioaddr))
  245. return PTR_ERR(pdata->ioaddr);
  246. ioaddr = pdata->ioaddr;
  247. pdata->clk = devm_clk_get(&pdev->dev, NULL);
  248. if (IS_ERR(pdata->clk)) {
  249. dev_err(&pdev->dev, "unable to get rtc clock!\n");
  250. return PTR_ERR(pdata->clk);
  251. }
  252. spin_lock_init(&pdata->lock);
  253. pdata->irq = platform_get_irq(pdev, 0);
  254. if (pdata->irq < 0)
  255. return pdata->irq;
  256. device_init_wakeup(&pdev->dev, 1);
  257. ret = clk_prepare_enable(pdata->clk);
  258. if (ret)
  259. return ret;
  260. /* initialize glitch detect */
  261. writel(SRTC_LPPDR_INIT, ioaddr + SRTC_LPPDR);
  262. /* clear lp interrupt status */
  263. writel(0xFFFFFFFF, ioaddr + SRTC_LPSR);
  264. /* move out of init state */
  265. writel((SRTC_LPCR_IE | SRTC_LPCR_NSA), ioaddr + SRTC_LPCR);
  266. ret = mxc_rtc_wait_for_flag(ioaddr + SRTC_LPSR, SRTC_LPSR_IES);
  267. if (ret) {
  268. dev_err(&pdev->dev, "Timeout waiting for SRTC_LPSR_IES\n");
  269. clk_disable_unprepare(pdata->clk);
  270. return ret;
  271. }
  272. /* move out of non-valid state */
  273. writel((SRTC_LPCR_IE | SRTC_LPCR_NVE | SRTC_LPCR_NSA |
  274. SRTC_LPCR_EN_LP), ioaddr + SRTC_LPCR);
  275. ret = mxc_rtc_wait_for_flag(ioaddr + SRTC_LPSR, SRTC_LPSR_NVES);
  276. if (ret) {
  277. dev_err(&pdev->dev, "Timeout waiting for SRTC_LPSR_NVES\n");
  278. clk_disable_unprepare(pdata->clk);
  279. return ret;
  280. }
  281. pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
  282. if (IS_ERR(pdata->rtc))
  283. return PTR_ERR(pdata->rtc);
  284. pdata->rtc->ops = &mxc_rtc_ops;
  285. pdata->rtc->range_max = U32_MAX;
  286. clk_disable(pdata->clk);
  287. platform_set_drvdata(pdev, pdata);
  288. ret =
  289. devm_request_irq(&pdev->dev, pdata->irq, mxc_rtc_interrupt, 0,
  290. pdev->name, &pdev->dev);
  291. if (ret < 0) {
  292. dev_err(&pdev->dev, "interrupt not available.\n");
  293. clk_unprepare(pdata->clk);
  294. return ret;
  295. }
  296. ret = rtc_register_device(pdata->rtc);
  297. if (ret < 0)
  298. clk_unprepare(pdata->clk);
  299. return ret;
  300. }
  301. static int mxc_rtc_remove(struct platform_device *pdev)
  302. {
  303. struct mxc_rtc_data *pdata = platform_get_drvdata(pdev);
  304. clk_disable_unprepare(pdata->clk);
  305. return 0;
  306. }
  307. #ifdef CONFIG_PM_SLEEP
  308. static int mxc_rtc_suspend(struct device *dev)
  309. {
  310. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  311. if (device_may_wakeup(dev))
  312. enable_irq_wake(pdata->irq);
  313. return 0;
  314. }
  315. static int mxc_rtc_resume(struct device *dev)
  316. {
  317. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  318. if (device_may_wakeup(dev))
  319. disable_irq_wake(pdata->irq);
  320. return 0;
  321. }
  322. #endif
  323. static SIMPLE_DEV_PM_OPS(mxc_rtc_pm_ops, mxc_rtc_suspend, mxc_rtc_resume);
  324. static const struct of_device_id mxc_ids[] = {
  325. { .compatible = "fsl,imx53-rtc", },
  326. {}
  327. };
  328. static struct platform_driver mxc_rtc_driver = {
  329. .driver = {
  330. .name = "mxc_rtc_v2",
  331. .of_match_table = mxc_ids,
  332. .pm = &mxc_rtc_pm_ops,
  333. },
  334. .probe = mxc_rtc_probe,
  335. .remove = mxc_rtc_remove,
  336. };
  337. module_platform_driver(mxc_rtc_driver);
  338. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  339. MODULE_DESCRIPTION("Real Time Clock (RTC) Driver for i.MX53");
  340. MODULE_LICENSE("GPL");