rtc-imxdi.c 15 KB

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