rtc-mxc_v2.c 10 KB

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