rtc-imxdi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright 2010 Orex Computed Radiography
  4. */
  5. /*
  6. * The code contained herein is licensed under the GNU General Public
  7. * License. You may obtain a copy of the GNU General Public License
  8. * Version 2 or later at the following locations:
  9. *
  10. * http://www.opensource.org/licenses/gpl-license.html
  11. * http://www.gnu.org/copyleft/gpl.html
  12. */
  13. /* based on rtc-mc13892.c */
  14. /*
  15. * This driver uses the 47-bit 32 kHz counter in the Freescale DryIce block
  16. * to implement a Linux RTC. Times and alarms are truncated to seconds.
  17. * Since the RTC framework performs API locking via rtc->ops_lock the
  18. * only simultaneous accesses we need to deal with is updating DryIce
  19. * registers while servicing an alarm.
  20. *
  21. * Note that reading the DSR (DryIce Status Register) automatically clears
  22. * the WCF (Write Complete Flag). All DryIce writes are synchronized to the
  23. * LP (Low Power) domain and set the WCF upon completion. Writes to the
  24. * DIER (DryIce Interrupt Enable Register) are the only exception. These
  25. * occur at normal bus speeds and do not set WCF. Periodic interrupts are
  26. * not supported by the hardware.
  27. */
  28. #include <linux/io.h>
  29. #include <linux/clk.h>
  30. #include <linux/delay.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/rtc.h>
  34. #include <linux/sched.h>
  35. #include <linux/workqueue.h>
  36. #include <linux/of.h>
  37. /* DryIce Register Definitions */
  38. #define DTCMR 0x00 /* Time Counter MSB Reg */
  39. #define DTCLR 0x04 /* Time Counter LSB Reg */
  40. #define DCAMR 0x08 /* Clock Alarm MSB Reg */
  41. #define DCALR 0x0c /* Clock Alarm LSB Reg */
  42. #define DCAMR_UNSET 0xFFFFFFFF /* doomsday - 1 sec */
  43. #define DCR 0x10 /* Control Reg */
  44. #define DCR_TCE (1 << 3) /* Time Counter Enable */
  45. #define DSR 0x14 /* Status Reg */
  46. #define DSR_WBF (1 << 10) /* Write Busy Flag */
  47. #define DSR_WNF (1 << 9) /* Write Next Flag */
  48. #define DSR_WCF (1 << 8) /* Write Complete Flag */
  49. #define DSR_WEF (1 << 7) /* Write Error Flag */
  50. #define DSR_CAF (1 << 4) /* Clock Alarm Flag */
  51. #define DSR_NVF (1 << 1) /* Non-Valid Flag */
  52. #define DSR_SVF (1 << 0) /* Security Violation Flag */
  53. #define DIER 0x18 /* Interrupt Enable Reg */
  54. #define DIER_WNIE (1 << 9) /* Write Next Interrupt Enable */
  55. #define DIER_WCIE (1 << 8) /* Write Complete Interrupt Enable */
  56. #define DIER_WEIE (1 << 7) /* Write Error Interrupt Enable */
  57. #define DIER_CAIE (1 << 4) /* Clock Alarm Interrupt Enable */
  58. /**
  59. * struct imxdi_dev - private imxdi rtc data
  60. * @pdev: pionter to platform dev
  61. * @rtc: pointer to rtc struct
  62. * @ioaddr: IO registers pointer
  63. * @irq: dryice normal interrupt
  64. * @clk: input reference clock
  65. * @dsr: copy of the DSR register
  66. * @irq_lock: interrupt enable register (DIER) lock
  67. * @write_wait: registers write complete queue
  68. * @write_mutex: serialize registers write
  69. * @work: schedule alarm work
  70. */
  71. struct imxdi_dev {
  72. struct platform_device *pdev;
  73. struct rtc_device *rtc;
  74. void __iomem *ioaddr;
  75. int irq;
  76. struct clk *clk;
  77. u32 dsr;
  78. spinlock_t irq_lock;
  79. wait_queue_head_t write_wait;
  80. struct mutex write_mutex;
  81. struct work_struct work;
  82. };
  83. /*
  84. * enable a dryice interrupt
  85. */
  86. static void di_int_enable(struct imxdi_dev *imxdi, u32 intr)
  87. {
  88. unsigned long flags;
  89. spin_lock_irqsave(&imxdi->irq_lock, flags);
  90. __raw_writel(__raw_readl(imxdi->ioaddr + DIER) | intr,
  91. imxdi->ioaddr + DIER);
  92. spin_unlock_irqrestore(&imxdi->irq_lock, flags);
  93. }
  94. /*
  95. * disable a dryice interrupt
  96. */
  97. static void di_int_disable(struct imxdi_dev *imxdi, u32 intr)
  98. {
  99. unsigned long flags;
  100. spin_lock_irqsave(&imxdi->irq_lock, flags);
  101. __raw_writel(__raw_readl(imxdi->ioaddr + DIER) & ~intr,
  102. imxdi->ioaddr + DIER);
  103. spin_unlock_irqrestore(&imxdi->irq_lock, flags);
  104. }
  105. /*
  106. * This function attempts to clear the dryice write-error flag.
  107. *
  108. * A dryice write error is similar to a bus fault and should not occur in
  109. * normal operation. Clearing the flag requires another write, so the root
  110. * cause of the problem may need to be fixed before the flag can be cleared.
  111. */
  112. static void clear_write_error(struct imxdi_dev *imxdi)
  113. {
  114. int cnt;
  115. dev_warn(&imxdi->pdev->dev, "WARNING: Register write error!\n");
  116. /* clear the write error flag */
  117. __raw_writel(DSR_WEF, imxdi->ioaddr + DSR);
  118. /* wait for it to take effect */
  119. for (cnt = 0; cnt < 1000; cnt++) {
  120. if ((__raw_readl(imxdi->ioaddr + DSR) & DSR_WEF) == 0)
  121. return;
  122. udelay(10);
  123. }
  124. dev_err(&imxdi->pdev->dev,
  125. "ERROR: Cannot clear write-error flag!\n");
  126. }
  127. /*
  128. * Write a dryice register and wait until it completes.
  129. *
  130. * This function uses interrupts to determine when the
  131. * write has completed.
  132. */
  133. static int di_write_wait(struct imxdi_dev *imxdi, u32 val, int reg)
  134. {
  135. int ret;
  136. int rc = 0;
  137. /* serialize register writes */
  138. mutex_lock(&imxdi->write_mutex);
  139. /* enable the write-complete interrupt */
  140. di_int_enable(imxdi, DIER_WCIE);
  141. imxdi->dsr = 0;
  142. /* do the register write */
  143. __raw_writel(val, imxdi->ioaddr + reg);
  144. /* wait for the write to finish */
  145. ret = wait_event_interruptible_timeout(imxdi->write_wait,
  146. imxdi->dsr & (DSR_WCF | DSR_WEF), msecs_to_jiffies(1));
  147. if (ret < 0) {
  148. rc = ret;
  149. goto out;
  150. } else if (ret == 0) {
  151. dev_warn(&imxdi->pdev->dev,
  152. "Write-wait timeout "
  153. "val = 0x%08x reg = 0x%08x\n", val, reg);
  154. }
  155. /* check for write error */
  156. if (imxdi->dsr & DSR_WEF) {
  157. clear_write_error(imxdi);
  158. rc = -EIO;
  159. }
  160. out:
  161. mutex_unlock(&imxdi->write_mutex);
  162. return rc;
  163. }
  164. /*
  165. * read the seconds portion of the current time from the dryice time counter
  166. */
  167. static int dryice_rtc_read_time(struct device *dev, struct rtc_time *tm)
  168. {
  169. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  170. unsigned long now;
  171. now = __raw_readl(imxdi->ioaddr + DTCMR);
  172. rtc_time_to_tm(now, tm);
  173. return 0;
  174. }
  175. /*
  176. * set the seconds portion of dryice time counter and clear the
  177. * fractional part.
  178. */
  179. static int dryice_rtc_set_mmss(struct device *dev, unsigned long secs)
  180. {
  181. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  182. int rc;
  183. /* zero the fractional part first */
  184. rc = di_write_wait(imxdi, 0, DTCLR);
  185. if (rc == 0)
  186. rc = di_write_wait(imxdi, secs, DTCMR);
  187. return rc;
  188. }
  189. static int dryice_rtc_alarm_irq_enable(struct device *dev,
  190. unsigned int enabled)
  191. {
  192. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  193. if (enabled)
  194. di_int_enable(imxdi, DIER_CAIE);
  195. else
  196. di_int_disable(imxdi, DIER_CAIE);
  197. return 0;
  198. }
  199. /*
  200. * read the seconds portion of the alarm register.
  201. * the fractional part of the alarm register is always zero.
  202. */
  203. static int dryice_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  204. {
  205. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  206. u32 dcamr;
  207. dcamr = __raw_readl(imxdi->ioaddr + DCAMR);
  208. rtc_time_to_tm(dcamr, &alarm->time);
  209. /* alarm is enabled if the interrupt is enabled */
  210. alarm->enabled = (__raw_readl(imxdi->ioaddr + DIER) & DIER_CAIE) != 0;
  211. /* don't allow the DSR read to mess up DSR_WCF */
  212. mutex_lock(&imxdi->write_mutex);
  213. /* alarm is pending if the alarm flag is set */
  214. alarm->pending = (__raw_readl(imxdi->ioaddr + DSR) & DSR_CAF) != 0;
  215. mutex_unlock(&imxdi->write_mutex);
  216. return 0;
  217. }
  218. /*
  219. * set the seconds portion of dryice alarm register
  220. */
  221. static int dryice_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  222. {
  223. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  224. unsigned long now;
  225. unsigned long alarm_time;
  226. int rc;
  227. rc = rtc_tm_to_time(&alarm->time, &alarm_time);
  228. if (rc)
  229. return rc;
  230. /* don't allow setting alarm in the past */
  231. now = __raw_readl(imxdi->ioaddr + DTCMR);
  232. if (alarm_time < now)
  233. return -EINVAL;
  234. /* write the new alarm time */
  235. rc = di_write_wait(imxdi, (u32)alarm_time, DCAMR);
  236. if (rc)
  237. return rc;
  238. if (alarm->enabled)
  239. di_int_enable(imxdi, DIER_CAIE); /* enable alarm intr */
  240. else
  241. di_int_disable(imxdi, DIER_CAIE); /* disable alarm intr */
  242. return 0;
  243. }
  244. static struct rtc_class_ops dryice_rtc_ops = {
  245. .read_time = dryice_rtc_read_time,
  246. .set_mmss = dryice_rtc_set_mmss,
  247. .alarm_irq_enable = dryice_rtc_alarm_irq_enable,
  248. .read_alarm = dryice_rtc_read_alarm,
  249. .set_alarm = dryice_rtc_set_alarm,
  250. };
  251. /*
  252. * dryice "normal" interrupt handler
  253. */
  254. static irqreturn_t dryice_norm_irq(int irq, void *dev_id)
  255. {
  256. struct imxdi_dev *imxdi = dev_id;
  257. u32 dsr, dier;
  258. irqreturn_t rc = IRQ_NONE;
  259. dier = __raw_readl(imxdi->ioaddr + DIER);
  260. /* handle write complete and write error cases */
  261. if ((dier & DIER_WCIE)) {
  262. /*If the write wait queue is empty then there is no pending
  263. operations. It means the interrupt is for DryIce -Security.
  264. IRQ must be returned as none.*/
  265. if (list_empty_careful(&imxdi->write_wait.task_list))
  266. return rc;
  267. /* DSR_WCF clears itself on DSR read */
  268. dsr = __raw_readl(imxdi->ioaddr + DSR);
  269. if ((dsr & (DSR_WCF | DSR_WEF))) {
  270. /* mask the interrupt */
  271. di_int_disable(imxdi, DIER_WCIE);
  272. /* save the dsr value for the wait queue */
  273. imxdi->dsr |= dsr;
  274. wake_up_interruptible(&imxdi->write_wait);
  275. rc = IRQ_HANDLED;
  276. }
  277. }
  278. /* handle the alarm case */
  279. if ((dier & DIER_CAIE)) {
  280. /* DSR_WCF clears itself on DSR read */
  281. dsr = __raw_readl(imxdi->ioaddr + DSR);
  282. if (dsr & DSR_CAF) {
  283. /* mask the interrupt */
  284. di_int_disable(imxdi, DIER_CAIE);
  285. /* finish alarm in user context */
  286. schedule_work(&imxdi->work);
  287. rc = IRQ_HANDLED;
  288. }
  289. }
  290. return rc;
  291. }
  292. /*
  293. * post the alarm event from user context so it can sleep
  294. * on the write completion.
  295. */
  296. static void dryice_work(struct work_struct *work)
  297. {
  298. struct imxdi_dev *imxdi = container_of(work,
  299. struct imxdi_dev, work);
  300. /* dismiss the interrupt (ignore error) */
  301. di_write_wait(imxdi, DSR_CAF, DSR);
  302. /* pass the alarm event to the rtc framework. */
  303. rtc_update_irq(imxdi->rtc, 1, RTC_AF | RTC_IRQF);
  304. }
  305. /*
  306. * probe for dryice rtc device
  307. */
  308. static int dryice_rtc_probe(struct platform_device *pdev)
  309. {
  310. struct resource *res;
  311. struct imxdi_dev *imxdi;
  312. int rc;
  313. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  314. if (!res)
  315. return -ENODEV;
  316. imxdi = devm_kzalloc(&pdev->dev, sizeof(*imxdi), GFP_KERNEL);
  317. if (!imxdi)
  318. return -ENOMEM;
  319. imxdi->pdev = pdev;
  320. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  321. pdev->name))
  322. return -EBUSY;
  323. imxdi->ioaddr = devm_ioremap(&pdev->dev, res->start,
  324. resource_size(res));
  325. if (imxdi->ioaddr == NULL)
  326. return -ENOMEM;
  327. spin_lock_init(&imxdi->irq_lock);
  328. imxdi->irq = platform_get_irq(pdev, 0);
  329. if (imxdi->irq < 0)
  330. return imxdi->irq;
  331. init_waitqueue_head(&imxdi->write_wait);
  332. INIT_WORK(&imxdi->work, dryice_work);
  333. mutex_init(&imxdi->write_mutex);
  334. imxdi->clk = clk_get(&pdev->dev, NULL);
  335. if (IS_ERR(imxdi->clk))
  336. return PTR_ERR(imxdi->clk);
  337. clk_prepare_enable(imxdi->clk);
  338. /*
  339. * Initialize dryice hardware
  340. */
  341. /* mask all interrupts */
  342. __raw_writel(0, imxdi->ioaddr + DIER);
  343. rc = devm_request_irq(&pdev->dev, imxdi->irq, dryice_norm_irq,
  344. IRQF_SHARED, pdev->name, imxdi);
  345. if (rc) {
  346. dev_warn(&pdev->dev, "interrupt not available.\n");
  347. goto err;
  348. }
  349. /* put dryice into valid state */
  350. if (__raw_readl(imxdi->ioaddr + DSR) & DSR_NVF) {
  351. rc = di_write_wait(imxdi, DSR_NVF | DSR_SVF, DSR);
  352. if (rc)
  353. goto err;
  354. }
  355. /* initialize alarm */
  356. rc = di_write_wait(imxdi, DCAMR_UNSET, DCAMR);
  357. if (rc)
  358. goto err;
  359. rc = di_write_wait(imxdi, 0, DCALR);
  360. if (rc)
  361. goto err;
  362. /* clear alarm flag */
  363. if (__raw_readl(imxdi->ioaddr + DSR) & DSR_CAF) {
  364. rc = di_write_wait(imxdi, DSR_CAF, DSR);
  365. if (rc)
  366. goto err;
  367. }
  368. /* the timer won't count if it has never been written to */
  369. if (__raw_readl(imxdi->ioaddr + DTCMR) == 0) {
  370. rc = di_write_wait(imxdi, 0, DTCMR);
  371. if (rc)
  372. goto err;
  373. }
  374. /* start keeping time */
  375. if (!(__raw_readl(imxdi->ioaddr + DCR) & DCR_TCE)) {
  376. rc = di_write_wait(imxdi,
  377. __raw_readl(imxdi->ioaddr + DCR) | DCR_TCE,
  378. DCR);
  379. if (rc)
  380. goto err;
  381. }
  382. platform_set_drvdata(pdev, imxdi);
  383. imxdi->rtc = rtc_device_register(pdev->name, &pdev->dev,
  384. &dryice_rtc_ops, THIS_MODULE);
  385. if (IS_ERR(imxdi->rtc)) {
  386. rc = PTR_ERR(imxdi->rtc);
  387. goto err;
  388. }
  389. return 0;
  390. err:
  391. clk_disable_unprepare(imxdi->clk);
  392. clk_put(imxdi->clk);
  393. return rc;
  394. }
  395. static int __devexit dryice_rtc_remove(struct platform_device *pdev)
  396. {
  397. struct imxdi_dev *imxdi = platform_get_drvdata(pdev);
  398. flush_work(&imxdi->work);
  399. /* mask all interrupts */
  400. __raw_writel(0, imxdi->ioaddr + DIER);
  401. rtc_device_unregister(imxdi->rtc);
  402. clk_disable_unprepare(imxdi->clk);
  403. clk_put(imxdi->clk);
  404. return 0;
  405. }
  406. #ifdef CONFIG_OF
  407. static const struct of_device_id dryice_dt_ids[] = {
  408. { .compatible = "fsl,imx25-rtc" },
  409. { /* sentinel */ }
  410. };
  411. MODULE_DEVICE_TABLE(of, dryice_dt_ids);
  412. #endif
  413. static struct platform_driver dryice_rtc_driver = {
  414. .driver = {
  415. .name = "imxdi_rtc",
  416. .owner = THIS_MODULE,
  417. .of_match_table = of_match_ptr(dryice_dt_ids),
  418. },
  419. .remove = __devexit_p(dryice_rtc_remove),
  420. };
  421. static int __init dryice_rtc_init(void)
  422. {
  423. return platform_driver_probe(&dryice_rtc_driver, dryice_rtc_probe);
  424. }
  425. static void __exit dryice_rtc_exit(void)
  426. {
  427. platform_driver_unregister(&dryice_rtc_driver);
  428. }
  429. module_init(dryice_rtc_init);
  430. module_exit(dryice_rtc_exit);
  431. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  432. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  433. MODULE_DESCRIPTION("IMX DryIce Realtime Clock Driver (RTC)");
  434. MODULE_LICENSE("GPL");