rtc-imxdi.c 13 KB

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