rtc-mxc_v2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. if (time > U32_MAX) {
  143. dev_err(dev, "RTC exceeded by %llus\n", time - U32_MAX);
  144. return -EINVAL;
  145. }
  146. ret = mxc_rtc_lock(pdata);
  147. if (ret)
  148. return ret;
  149. writel(time, pdata->ioaddr + SRTC_LPSCMR);
  150. mxc_rtc_sync_lp_locked(dev, pdata->ioaddr);
  151. return mxc_rtc_unlock(pdata);
  152. }
  153. /*
  154. * This function reads the current alarm value into the passed in \b alrm
  155. * argument. It updates the \b alrm's pending field value based on the whether
  156. * an alarm interrupt occurs or not.
  157. *
  158. * @param alrm contains the RTC alarm value upon return
  159. *
  160. * @return 0 if successful; non-zero otherwise.
  161. */
  162. static int mxc_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  163. {
  164. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  165. void __iomem *ioaddr = pdata->ioaddr;
  166. int ret;
  167. ret = mxc_rtc_lock(pdata);
  168. if (ret)
  169. return ret;
  170. rtc_time_to_tm(readl(ioaddr + SRTC_LPSAR), &alrm->time);
  171. alrm->pending = !!(readl(ioaddr + SRTC_LPSR) & SRTC_LPSR_ALP);
  172. return mxc_rtc_unlock(pdata);
  173. }
  174. /*
  175. * Enable/Disable alarm interrupt
  176. * The caller should hold the pdata->lock
  177. */
  178. static void mxc_rtc_alarm_irq_enable_locked(struct mxc_rtc_data *pdata,
  179. unsigned int enable)
  180. {
  181. u32 lp_cr = readl(pdata->ioaddr + SRTC_LPCR);
  182. if (enable)
  183. lp_cr |= (SRTC_LPCR_ALP | SRTC_LPCR_WAE);
  184. else
  185. lp_cr &= ~(SRTC_LPCR_ALP | SRTC_LPCR_WAE);
  186. writel(lp_cr, pdata->ioaddr + SRTC_LPCR);
  187. }
  188. static int mxc_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  189. {
  190. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  191. int ret = mxc_rtc_lock(pdata);
  192. if (ret)
  193. return ret;
  194. mxc_rtc_alarm_irq_enable_locked(pdata, enable);
  195. return mxc_rtc_unlock(pdata);
  196. }
  197. /*
  198. * This function sets the RTC alarm based on passed in alrm.
  199. *
  200. * @param alrm the alarm value to be set in the RTC
  201. *
  202. * @return 0 if successful; non-zero otherwise.
  203. */
  204. static int mxc_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  205. {
  206. const time64_t time = rtc_tm_to_time64(&alrm->time);
  207. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  208. int ret = mxc_rtc_lock(pdata);
  209. if (ret)
  210. return ret;
  211. if (time > U32_MAX) {
  212. dev_err(dev, "Hopefully I am out of service by then :-(\n");
  213. return -EINVAL;
  214. }
  215. writel((u32)time, pdata->ioaddr + SRTC_LPSAR);
  216. /* clear alarm interrupt status bit */
  217. writel(SRTC_LPSR_ALP, pdata->ioaddr + SRTC_LPSR);
  218. mxc_rtc_sync_lp_locked(dev, pdata->ioaddr);
  219. mxc_rtc_alarm_irq_enable_locked(pdata, alrm->enabled);
  220. mxc_rtc_sync_lp_locked(dev, pdata->ioaddr);
  221. mxc_rtc_unlock(pdata);
  222. return ret;
  223. }
  224. static const struct rtc_class_ops mxc_rtc_ops = {
  225. .read_time = mxc_rtc_read_time,
  226. .set_time = mxc_rtc_set_time,
  227. .read_alarm = mxc_rtc_read_alarm,
  228. .set_alarm = mxc_rtc_set_alarm,
  229. .alarm_irq_enable = mxc_rtc_alarm_irq_enable,
  230. };
  231. static int mxc_rtc_wait_for_flag(void __iomem *ioaddr, int flag)
  232. {
  233. unsigned int timeout = REG_READ_TIMEOUT;
  234. while (!(readl(ioaddr) & flag)) {
  235. if (!--timeout)
  236. return -EBUSY;
  237. }
  238. return 0;
  239. }
  240. static int mxc_rtc_probe(struct platform_device *pdev)
  241. {
  242. struct mxc_rtc_data *pdata;
  243. struct resource *res;
  244. void __iomem *ioaddr;
  245. int ret = 0;
  246. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  247. if (!pdata)
  248. return -ENOMEM;
  249. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  250. pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  251. if (IS_ERR(pdata->ioaddr))
  252. return PTR_ERR(pdata->ioaddr);
  253. ioaddr = pdata->ioaddr;
  254. pdata->clk = devm_clk_get(&pdev->dev, NULL);
  255. if (IS_ERR(pdata->clk)) {
  256. dev_err(&pdev->dev, "unable to get rtc clock!\n");
  257. return PTR_ERR(pdata->clk);
  258. }
  259. spin_lock_init(&pdata->lock);
  260. pdata->irq = platform_get_irq(pdev, 0);
  261. if (pdata->irq < 0)
  262. return pdata->irq;
  263. device_init_wakeup(&pdev->dev, 1);
  264. ret = clk_prepare_enable(pdata->clk);
  265. if (ret)
  266. return ret;
  267. /* initialize glitch detect */
  268. writel(SRTC_LPPDR_INIT, ioaddr + SRTC_LPPDR);
  269. /* clear lp interrupt status */
  270. writel(0xFFFFFFFF, ioaddr + SRTC_LPSR);
  271. /* move out of init state */
  272. writel((SRTC_LPCR_IE | SRTC_LPCR_NSA), ioaddr + SRTC_LPCR);
  273. ret = mxc_rtc_wait_for_flag(ioaddr + SRTC_LPSR, SRTC_LPSR_IES);
  274. if (ret) {
  275. dev_err(&pdev->dev, "Timeout waiting for SRTC_LPSR_IES\n");
  276. clk_disable_unprepare(pdata->clk);
  277. return ret;
  278. }
  279. /* move out of non-valid state */
  280. writel((SRTC_LPCR_IE | SRTC_LPCR_NVE | SRTC_LPCR_NSA |
  281. SRTC_LPCR_EN_LP), ioaddr + SRTC_LPCR);
  282. ret = mxc_rtc_wait_for_flag(ioaddr + SRTC_LPSR, SRTC_LPSR_NVES);
  283. if (ret) {
  284. dev_err(&pdev->dev, "Timeout waiting for SRTC_LPSR_NVES\n");
  285. clk_disable_unprepare(pdata->clk);
  286. return ret;
  287. }
  288. clk_disable(pdata->clk);
  289. platform_set_drvdata(pdev, pdata);
  290. ret =
  291. devm_request_irq(&pdev->dev, pdata->irq, mxc_rtc_interrupt, 0,
  292. pdev->name, &pdev->dev);
  293. if (ret < 0) {
  294. dev_err(&pdev->dev, "interrupt not available.\n");
  295. clk_unprepare(pdata->clk);
  296. return ret;
  297. }
  298. pdata->rtc =
  299. devm_rtc_device_register(&pdev->dev, pdev->name, &mxc_rtc_ops,
  300. THIS_MODULE);
  301. if (IS_ERR(pdata->rtc)) {
  302. clk_unprepare(pdata->clk);
  303. return PTR_ERR(pdata->rtc);
  304. }
  305. return 0;
  306. }
  307. static int mxc_rtc_remove(struct platform_device *pdev)
  308. {
  309. struct mxc_rtc_data *pdata = platform_get_drvdata(pdev);
  310. clk_disable_unprepare(pdata->clk);
  311. return 0;
  312. }
  313. #ifdef CONFIG_PM_SLEEP
  314. static int mxc_rtc_suspend(struct device *dev)
  315. {
  316. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  317. if (device_may_wakeup(dev))
  318. enable_irq_wake(pdata->irq);
  319. return 0;
  320. }
  321. static int mxc_rtc_resume(struct device *dev)
  322. {
  323. struct mxc_rtc_data *pdata = dev_get_drvdata(dev);
  324. if (device_may_wakeup(dev))
  325. disable_irq_wake(pdata->irq);
  326. return 0;
  327. }
  328. #endif
  329. static SIMPLE_DEV_PM_OPS(mxc_rtc_pm_ops, mxc_rtc_suspend, mxc_rtc_resume);
  330. static const struct of_device_id mxc_ids[] = {
  331. { .compatible = "fsl,imx53-rtc", },
  332. {}
  333. };
  334. static struct platform_driver mxc_rtc_driver = {
  335. .driver = {
  336. .name = "mxc_rtc_v2",
  337. .of_match_table = mxc_ids,
  338. .pm = &mxc_rtc_pm_ops,
  339. },
  340. .probe = mxc_rtc_probe,
  341. .remove = mxc_rtc_remove,
  342. };
  343. module_platform_driver(mxc_rtc_driver);
  344. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  345. MODULE_DESCRIPTION("Real Time Clock (RTC) Driver for i.MX53");
  346. MODULE_LICENSE("GPL");