rtc-imxdi.c 13 KB

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