rtc-imxdi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  4. * Copyright 2010 Orex Computed Radiography
  5. */
  6. /*
  7. * This driver uses the 47-bit 32 kHz counter in the Freescale DryIce block
  8. * to implement a Linux RTC. Times and alarms are truncated to seconds.
  9. * Since the RTC framework performs API locking via rtc->ops_lock the
  10. * only simultaneous accesses we need to deal with is updating DryIce
  11. * registers while servicing an alarm.
  12. *
  13. * Note that reading the DSR (DryIce Status Register) automatically clears
  14. * the WCF (Write Complete Flag). All DryIce writes are synchronized to the
  15. * LP (Low Power) domain and set the WCF upon completion. Writes to the
  16. * DIER (DryIce Interrupt Enable Register) are the only exception. These
  17. * occur at normal bus speeds and do not set WCF. Periodic interrupts are
  18. * not supported by the hardware.
  19. */
  20. #include <linux/io.h>
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_wakeirq.h>
  26. #include <linux/rtc.h>
  27. #include <linux/sched.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/of.h>
  31. /* DryIce Register Definitions */
  32. #define DTCMR 0x00 /* Time Counter MSB Reg */
  33. #define DTCLR 0x04 /* Time Counter LSB Reg */
  34. #define DCAMR 0x08 /* Clock Alarm MSB Reg */
  35. #define DCALR 0x0c /* Clock Alarm LSB Reg */
  36. #define DCAMR_UNSET 0xFFFFFFFF /* doomsday - 1 sec */
  37. #define DCR 0x10 /* Control Reg */
  38. #define DCR_TDCHL (1 << 30) /* Tamper-detect configuration hard lock */
  39. #define DCR_TDCSL (1 << 29) /* Tamper-detect configuration soft lock */
  40. #define DCR_KSSL (1 << 27) /* Key-select soft lock */
  41. #define DCR_MCHL (1 << 20) /* Monotonic-counter hard lock */
  42. #define DCR_MCSL (1 << 19) /* Monotonic-counter soft lock */
  43. #define DCR_TCHL (1 << 18) /* Timer-counter hard lock */
  44. #define DCR_TCSL (1 << 17) /* Timer-counter soft lock */
  45. #define DCR_FSHL (1 << 16) /* Failure state hard lock */
  46. #define DCR_TCE (1 << 3) /* Time Counter Enable */
  47. #define DCR_MCE (1 << 2) /* Monotonic Counter Enable */
  48. #define DSR 0x14 /* Status Reg */
  49. #define DSR_WTD (1 << 23) /* Wire-mesh tamper detected */
  50. #define DSR_ETBD (1 << 22) /* External tamper B detected */
  51. #define DSR_ETAD (1 << 21) /* External tamper A detected */
  52. #define DSR_EBD (1 << 20) /* External boot detected */
  53. #define DSR_SAD (1 << 19) /* SCC alarm detected */
  54. #define DSR_TTD (1 << 18) /* Temperature tamper detected */
  55. #define DSR_CTD (1 << 17) /* Clock tamper detected */
  56. #define DSR_VTD (1 << 16) /* Voltage tamper detected */
  57. #define DSR_WBF (1 << 10) /* Write Busy Flag (synchronous) */
  58. #define DSR_WNF (1 << 9) /* Write Next Flag (synchronous) */
  59. #define DSR_WCF (1 << 8) /* Write Complete Flag (synchronous)*/
  60. #define DSR_WEF (1 << 7) /* Write Error Flag */
  61. #define DSR_CAF (1 << 4) /* Clock Alarm Flag */
  62. #define DSR_MCO (1 << 3) /* monotonic counter overflow */
  63. #define DSR_TCO (1 << 2) /* time counter overflow */
  64. #define DSR_NVF (1 << 1) /* Non-Valid Flag */
  65. #define DSR_SVF (1 << 0) /* Security Violation Flag */
  66. #define DIER 0x18 /* Interrupt Enable Reg (synchronous) */
  67. #define DIER_WNIE (1 << 9) /* Write Next Interrupt Enable */
  68. #define DIER_WCIE (1 << 8) /* Write Complete Interrupt Enable */
  69. #define DIER_WEIE (1 << 7) /* Write Error Interrupt Enable */
  70. #define DIER_CAIE (1 << 4) /* Clock Alarm Interrupt Enable */
  71. #define DIER_SVIE (1 << 0) /* Security-violation Interrupt Enable */
  72. #define DMCR 0x1c /* DryIce Monotonic Counter Reg */
  73. #define DTCR 0x28 /* DryIce Tamper Configuration Reg */
  74. #define DTCR_MOE (1 << 9) /* monotonic overflow enabled */
  75. #define DTCR_TOE (1 << 8) /* time overflow enabled */
  76. #define DTCR_WTE (1 << 7) /* wire-mesh tamper enabled */
  77. #define DTCR_ETBE (1 << 6) /* external B tamper enabled */
  78. #define DTCR_ETAE (1 << 5) /* external A tamper enabled */
  79. #define DTCR_EBE (1 << 4) /* external boot tamper enabled */
  80. #define DTCR_SAIE (1 << 3) /* SCC enabled */
  81. #define DTCR_TTE (1 << 2) /* temperature tamper enabled */
  82. #define DTCR_CTE (1 << 1) /* clock tamper enabled */
  83. #define DTCR_VTE (1 << 0) /* voltage tamper enabled */
  84. #define DGPR 0x3c /* DryIce General Purpose Reg */
  85. /**
  86. * struct imxdi_dev - private imxdi rtc data
  87. * @pdev: pointer to platform dev
  88. * @rtc: pointer to rtc struct
  89. * @ioaddr: IO registers pointer
  90. * @clk: input reference clock
  91. * @dsr: copy of the DSR register
  92. * @irq_lock: interrupt enable register (DIER) lock
  93. * @write_wait: registers write complete queue
  94. * @write_mutex: serialize registers write
  95. * @work: schedule alarm work
  96. */
  97. struct imxdi_dev {
  98. struct platform_device *pdev;
  99. struct rtc_device *rtc;
  100. void __iomem *ioaddr;
  101. struct clk *clk;
  102. u32 dsr;
  103. spinlock_t irq_lock;
  104. wait_queue_head_t write_wait;
  105. struct mutex write_mutex;
  106. struct work_struct work;
  107. };
  108. /* Some background:
  109. *
  110. * The DryIce unit is a complex security/tamper monitor device. To be able do
  111. * its job in a useful manner it runs a bigger statemachine to bring it into
  112. * security/tamper failure state and once again to bring it out of this state.
  113. *
  114. * This unit can be in one of three states:
  115. *
  116. * - "NON-VALID STATE"
  117. * always after the battery power was removed
  118. * - "FAILURE STATE"
  119. * if one of the enabled security events has happened
  120. * - "VALID STATE"
  121. * if the unit works as expected
  122. *
  123. * Everything stops when the unit enters the failure state including the RTC
  124. * counter (to be able to detect the time the security event happened).
  125. *
  126. * The following events (when enabled) let the DryIce unit enter the failure
  127. * state:
  128. *
  129. * - wire-mesh-tamper detect
  130. * - external tamper B detect
  131. * - external tamper A detect
  132. * - temperature tamper detect
  133. * - clock tamper detect
  134. * - voltage tamper detect
  135. * - RTC counter overflow
  136. * - monotonic counter overflow
  137. * - external boot
  138. *
  139. * If we find the DryIce unit in "FAILURE STATE" and the TDCHL cleared, we
  140. * can only detect this state. In this case the unit is completely locked and
  141. * must force a second "SYSTEM POR" to bring the DryIce into the
  142. * "NON-VALID STATE" + "FAILURE STATE" where a recovery is possible.
  143. * If the TDCHL is set in the "FAILURE STATE" we are out of luck. In this case
  144. * a battery power cycle is required.
  145. *
  146. * In the "NON-VALID STATE" + "FAILURE STATE" we can clear the "FAILURE STATE"
  147. * and recover the DryIce unit. By clearing the "NON-VALID STATE" as the last
  148. * task, we bring back this unit into life.
  149. */
  150. /*
  151. * Do a write into the unit without interrupt support.
  152. * We do not need to check the WEF here, because the only reason this kind of
  153. * write error can happen is if we write to the unit twice within the 122 us
  154. * interval. This cannot happen, since we are using this function only while
  155. * setting up the unit.
  156. */
  157. static void di_write_busy_wait(const struct imxdi_dev *imxdi, u32 val,
  158. unsigned reg)
  159. {
  160. /* do the register write */
  161. writel(val, imxdi->ioaddr + reg);
  162. /*
  163. * now it takes four 32,768 kHz clock cycles to take
  164. * the change into effect = 122 us
  165. */
  166. usleep_range(130, 200);
  167. }
  168. static void di_report_tamper_info(struct imxdi_dev *imxdi, u32 dsr)
  169. {
  170. u32 dtcr;
  171. dtcr = readl(imxdi->ioaddr + DTCR);
  172. dev_emerg(&imxdi->pdev->dev, "DryIce tamper event detected\n");
  173. /* the following flags force a transition into the "FAILURE STATE" */
  174. if (dsr & DSR_VTD)
  175. dev_emerg(&imxdi->pdev->dev, "%sVoltage Tamper Event\n",
  176. dtcr & DTCR_VTE ? "" : "Spurious ");
  177. if (dsr & DSR_CTD)
  178. dev_emerg(&imxdi->pdev->dev, "%s32768 Hz Clock Tamper Event\n",
  179. dtcr & DTCR_CTE ? "" : "Spurious ");
  180. if (dsr & DSR_TTD)
  181. dev_emerg(&imxdi->pdev->dev, "%sTemperature Tamper Event\n",
  182. dtcr & DTCR_TTE ? "" : "Spurious ");
  183. if (dsr & DSR_SAD)
  184. dev_emerg(&imxdi->pdev->dev,
  185. "%sSecure Controller Alarm Event\n",
  186. dtcr & DTCR_SAIE ? "" : "Spurious ");
  187. if (dsr & DSR_EBD)
  188. dev_emerg(&imxdi->pdev->dev, "%sExternal Boot Tamper Event\n",
  189. dtcr & DTCR_EBE ? "" : "Spurious ");
  190. if (dsr & DSR_ETAD)
  191. dev_emerg(&imxdi->pdev->dev, "%sExternal Tamper A Event\n",
  192. dtcr & DTCR_ETAE ? "" : "Spurious ");
  193. if (dsr & DSR_ETBD)
  194. dev_emerg(&imxdi->pdev->dev, "%sExternal Tamper B Event\n",
  195. dtcr & DTCR_ETBE ? "" : "Spurious ");
  196. if (dsr & DSR_WTD)
  197. dev_emerg(&imxdi->pdev->dev, "%sWire-mesh Tamper Event\n",
  198. dtcr & DTCR_WTE ? "" : "Spurious ");
  199. if (dsr & DSR_MCO)
  200. dev_emerg(&imxdi->pdev->dev,
  201. "%sMonotonic-counter Overflow Event\n",
  202. dtcr & DTCR_MOE ? "" : "Spurious ");
  203. if (dsr & DSR_TCO)
  204. dev_emerg(&imxdi->pdev->dev, "%sTimer-counter Overflow Event\n",
  205. dtcr & DTCR_TOE ? "" : "Spurious ");
  206. }
  207. static void di_what_is_to_be_done(struct imxdi_dev *imxdi,
  208. const char *power_supply)
  209. {
  210. dev_emerg(&imxdi->pdev->dev, "Please cycle the %s power supply in order to get the DryIce/RTC unit working again\n",
  211. power_supply);
  212. }
  213. static int di_handle_failure_state(struct imxdi_dev *imxdi, u32 dsr)
  214. {
  215. u32 dcr;
  216. dev_dbg(&imxdi->pdev->dev, "DSR register reports: %08X\n", dsr);
  217. /* report the cause */
  218. di_report_tamper_info(imxdi, dsr);
  219. dcr = readl(imxdi->ioaddr + DCR);
  220. if (dcr & DCR_FSHL) {
  221. /* we are out of luck */
  222. di_what_is_to_be_done(imxdi, "battery");
  223. return -ENODEV;
  224. }
  225. /*
  226. * with the next SYSTEM POR we will transit from the "FAILURE STATE"
  227. * into the "NON-VALID STATE" + "FAILURE STATE"
  228. */
  229. di_what_is_to_be_done(imxdi, "main");
  230. return -ENODEV;
  231. }
  232. static int di_handle_valid_state(struct imxdi_dev *imxdi, u32 dsr)
  233. {
  234. /* initialize alarm */
  235. di_write_busy_wait(imxdi, DCAMR_UNSET, DCAMR);
  236. di_write_busy_wait(imxdi, 0, DCALR);
  237. /* clear alarm flag */
  238. if (dsr & DSR_CAF)
  239. di_write_busy_wait(imxdi, DSR_CAF, DSR);
  240. return 0;
  241. }
  242. static int di_handle_invalid_state(struct imxdi_dev *imxdi, u32 dsr)
  243. {
  244. u32 dcr, sec;
  245. /*
  246. * lets disable all sources which can force the DryIce unit into
  247. * the "FAILURE STATE" for now
  248. */
  249. di_write_busy_wait(imxdi, 0x00000000, DTCR);
  250. /* and lets protect them at runtime from any change */
  251. di_write_busy_wait(imxdi, DCR_TDCSL, DCR);
  252. sec = readl(imxdi->ioaddr + DTCMR);
  253. if (sec != 0)
  254. dev_warn(&imxdi->pdev->dev,
  255. "The security violation has happened at %u seconds\n",
  256. sec);
  257. /*
  258. * the timer cannot be set/modified if
  259. * - the TCHL or TCSL bit is set in DCR
  260. */
  261. dcr = readl(imxdi->ioaddr + DCR);
  262. if (!(dcr & DCR_TCE)) {
  263. if (dcr & DCR_TCHL) {
  264. /* we are out of luck */
  265. di_what_is_to_be_done(imxdi, "battery");
  266. return -ENODEV;
  267. }
  268. if (dcr & DCR_TCSL) {
  269. di_what_is_to_be_done(imxdi, "main");
  270. return -ENODEV;
  271. }
  272. }
  273. /*
  274. * - the timer counter stops/is stopped if
  275. * - its overflow flag is set (TCO in DSR)
  276. * -> clear overflow bit to make it count again
  277. * - NVF is set in DSR
  278. * -> clear non-valid bit to make it count again
  279. * - its TCE (DCR) is cleared
  280. * -> set TCE to make it count
  281. * - it was never set before
  282. * -> write a time into it (required again if the NVF was set)
  283. */
  284. /* state handled */
  285. di_write_busy_wait(imxdi, DSR_NVF, DSR);
  286. /* clear overflow flag */
  287. di_write_busy_wait(imxdi, DSR_TCO, DSR);
  288. /* enable the counter */
  289. di_write_busy_wait(imxdi, dcr | DCR_TCE, DCR);
  290. /* set and trigger it to make it count */
  291. di_write_busy_wait(imxdi, sec, DTCMR);
  292. /* now prepare for the valid state */
  293. return di_handle_valid_state(imxdi, __raw_readl(imxdi->ioaddr + DSR));
  294. }
  295. static int di_handle_invalid_and_failure_state(struct imxdi_dev *imxdi, u32 dsr)
  296. {
  297. u32 dcr;
  298. /*
  299. * now we must first remove the tamper sources in order to get the
  300. * device out of the "FAILURE STATE"
  301. * To disable any of the following sources we need to modify the DTCR
  302. */
  303. if (dsr & (DSR_WTD | DSR_ETBD | DSR_ETAD | DSR_EBD | DSR_SAD |
  304. DSR_TTD | DSR_CTD | DSR_VTD | DSR_MCO | DSR_TCO)) {
  305. dcr = __raw_readl(imxdi->ioaddr + DCR);
  306. if (dcr & DCR_TDCHL) {
  307. /*
  308. * the tamper register is locked. We cannot disable the
  309. * tamper detection. The TDCHL can only be reset by a
  310. * DRYICE POR, but we cannot force a DRYICE POR in
  311. * software because we are still in "FAILURE STATE".
  312. * We need a DRYICE POR via battery power cycling....
  313. */
  314. /*
  315. * out of luck!
  316. * we cannot disable them without a DRYICE POR
  317. */
  318. di_what_is_to_be_done(imxdi, "battery");
  319. return -ENODEV;
  320. }
  321. if (dcr & DCR_TDCSL) {
  322. /* a soft lock can be removed by a SYSTEM POR */
  323. di_what_is_to_be_done(imxdi, "main");
  324. return -ENODEV;
  325. }
  326. }
  327. /* disable all sources */
  328. di_write_busy_wait(imxdi, 0x00000000, DTCR);
  329. /* clear the status bits now */
  330. di_write_busy_wait(imxdi, dsr & (DSR_WTD | DSR_ETBD | DSR_ETAD |
  331. DSR_EBD | DSR_SAD | DSR_TTD | DSR_CTD | DSR_VTD |
  332. DSR_MCO | DSR_TCO), DSR);
  333. dsr = readl(imxdi->ioaddr + DSR);
  334. if ((dsr & ~(DSR_NVF | DSR_SVF | DSR_WBF | DSR_WNF |
  335. DSR_WCF | DSR_WEF)) != 0)
  336. dev_warn(&imxdi->pdev->dev,
  337. "There are still some sources of pain in DSR: %08x!\n",
  338. dsr & ~(DSR_NVF | DSR_SVF | DSR_WBF | DSR_WNF |
  339. DSR_WCF | DSR_WEF));
  340. /*
  341. * now we are trying to clear the "Security-violation flag" to
  342. * get the DryIce out of this state
  343. */
  344. di_write_busy_wait(imxdi, DSR_SVF, DSR);
  345. /* success? */
  346. dsr = readl(imxdi->ioaddr + DSR);
  347. if (dsr & DSR_SVF) {
  348. dev_crit(&imxdi->pdev->dev,
  349. "Cannot clear the security violation flag. We are ending up in an endless loop!\n");
  350. /* last resort */
  351. di_what_is_to_be_done(imxdi, "battery");
  352. return -ENODEV;
  353. }
  354. /*
  355. * now we have left the "FAILURE STATE" and ending up in the
  356. * "NON-VALID STATE" time to recover everything
  357. */
  358. return di_handle_invalid_state(imxdi, dsr);
  359. }
  360. static int di_handle_state(struct imxdi_dev *imxdi)
  361. {
  362. int rc;
  363. u32 dsr;
  364. dsr = readl(imxdi->ioaddr + DSR);
  365. switch (dsr & (DSR_NVF | DSR_SVF)) {
  366. case DSR_NVF:
  367. dev_warn(&imxdi->pdev->dev, "Invalid stated unit detected\n");
  368. rc = di_handle_invalid_state(imxdi, dsr);
  369. break;
  370. case DSR_SVF:
  371. dev_warn(&imxdi->pdev->dev, "Failure stated unit detected\n");
  372. rc = di_handle_failure_state(imxdi, dsr);
  373. break;
  374. case DSR_NVF | DSR_SVF:
  375. dev_warn(&imxdi->pdev->dev,
  376. "Failure+Invalid stated unit detected\n");
  377. rc = di_handle_invalid_and_failure_state(imxdi, dsr);
  378. break;
  379. default:
  380. dev_notice(&imxdi->pdev->dev, "Unlocked unit detected\n");
  381. rc = di_handle_valid_state(imxdi, dsr);
  382. }
  383. return rc;
  384. }
  385. /*
  386. * enable a dryice interrupt
  387. */
  388. static void di_int_enable(struct imxdi_dev *imxdi, u32 intr)
  389. {
  390. unsigned long flags;
  391. spin_lock_irqsave(&imxdi->irq_lock, flags);
  392. writel(readl(imxdi->ioaddr + DIER) | intr,
  393. imxdi->ioaddr + DIER);
  394. spin_unlock_irqrestore(&imxdi->irq_lock, flags);
  395. }
  396. /*
  397. * disable a dryice interrupt
  398. */
  399. static void di_int_disable(struct imxdi_dev *imxdi, u32 intr)
  400. {
  401. unsigned long flags;
  402. spin_lock_irqsave(&imxdi->irq_lock, flags);
  403. writel(readl(imxdi->ioaddr + DIER) & ~intr,
  404. imxdi->ioaddr + DIER);
  405. spin_unlock_irqrestore(&imxdi->irq_lock, flags);
  406. }
  407. /*
  408. * This function attempts to clear the dryice write-error flag.
  409. *
  410. * A dryice write error is similar to a bus fault and should not occur in
  411. * normal operation. Clearing the flag requires another write, so the root
  412. * cause of the problem may need to be fixed before the flag can be cleared.
  413. */
  414. static void clear_write_error(struct imxdi_dev *imxdi)
  415. {
  416. int cnt;
  417. dev_warn(&imxdi->pdev->dev, "WARNING: Register write error!\n");
  418. /* clear the write error flag */
  419. writel(DSR_WEF, imxdi->ioaddr + DSR);
  420. /* wait for it to take effect */
  421. for (cnt = 0; cnt < 1000; cnt++) {
  422. if ((readl(imxdi->ioaddr + DSR) & DSR_WEF) == 0)
  423. return;
  424. udelay(10);
  425. }
  426. dev_err(&imxdi->pdev->dev,
  427. "ERROR: Cannot clear write-error flag!\n");
  428. }
  429. /*
  430. * Write a dryice register and wait until it completes.
  431. *
  432. * This function uses interrupts to determine when the
  433. * write has completed.
  434. */
  435. static int di_write_wait(struct imxdi_dev *imxdi, u32 val, int reg)
  436. {
  437. int ret;
  438. int rc = 0;
  439. /* serialize register writes */
  440. mutex_lock(&imxdi->write_mutex);
  441. /* enable the write-complete interrupt */
  442. di_int_enable(imxdi, DIER_WCIE);
  443. imxdi->dsr = 0;
  444. /* do the register write */
  445. writel(val, imxdi->ioaddr + reg);
  446. /* wait for the write to finish */
  447. ret = wait_event_interruptible_timeout(imxdi->write_wait,
  448. imxdi->dsr & (DSR_WCF | DSR_WEF), msecs_to_jiffies(1));
  449. if (ret < 0) {
  450. rc = ret;
  451. goto out;
  452. } else if (ret == 0) {
  453. dev_warn(&imxdi->pdev->dev,
  454. "Write-wait timeout "
  455. "val = 0x%08x reg = 0x%08x\n", val, reg);
  456. }
  457. /* check for write error */
  458. if (imxdi->dsr & DSR_WEF) {
  459. clear_write_error(imxdi);
  460. rc = -EIO;
  461. }
  462. out:
  463. mutex_unlock(&imxdi->write_mutex);
  464. return rc;
  465. }
  466. /*
  467. * read the seconds portion of the current time from the dryice time counter
  468. */
  469. static int dryice_rtc_read_time(struct device *dev, struct rtc_time *tm)
  470. {
  471. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  472. unsigned long now;
  473. now = readl(imxdi->ioaddr + DTCMR);
  474. rtc_time64_to_tm(now, tm);
  475. return 0;
  476. }
  477. /*
  478. * set the seconds portion of dryice time counter and clear the
  479. * fractional part.
  480. */
  481. static int dryice_rtc_set_time(struct device *dev, struct rtc_time *tm)
  482. {
  483. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  484. u32 dcr, dsr;
  485. int rc;
  486. dcr = readl(imxdi->ioaddr + DCR);
  487. dsr = readl(imxdi->ioaddr + DSR);
  488. if (!(dcr & DCR_TCE) || (dsr & DSR_SVF)) {
  489. if (dcr & DCR_TCHL) {
  490. /* we are even more out of luck */
  491. di_what_is_to_be_done(imxdi, "battery");
  492. return -EPERM;
  493. }
  494. if ((dcr & DCR_TCSL) || (dsr & DSR_SVF)) {
  495. /* we are out of luck for now */
  496. di_what_is_to_be_done(imxdi, "main");
  497. return -EPERM;
  498. }
  499. }
  500. /* zero the fractional part first */
  501. rc = di_write_wait(imxdi, 0, DTCLR);
  502. if (rc != 0)
  503. return rc;
  504. rc = di_write_wait(imxdi, rtc_tm_to_time64(tm), DTCMR);
  505. if (rc != 0)
  506. return rc;
  507. return di_write_wait(imxdi, readl(imxdi->ioaddr + DCR) | DCR_TCE, DCR);
  508. }
  509. static int dryice_rtc_alarm_irq_enable(struct device *dev,
  510. unsigned int enabled)
  511. {
  512. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  513. if (enabled)
  514. di_int_enable(imxdi, DIER_CAIE);
  515. else
  516. di_int_disable(imxdi, DIER_CAIE);
  517. return 0;
  518. }
  519. /*
  520. * read the seconds portion of the alarm register.
  521. * the fractional part of the alarm register is always zero.
  522. */
  523. static int dryice_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  524. {
  525. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  526. u32 dcamr;
  527. dcamr = readl(imxdi->ioaddr + DCAMR);
  528. rtc_time64_to_tm(dcamr, &alarm->time);
  529. /* alarm is enabled if the interrupt is enabled */
  530. alarm->enabled = (readl(imxdi->ioaddr + DIER) & DIER_CAIE) != 0;
  531. /* don't allow the DSR read to mess up DSR_WCF */
  532. mutex_lock(&imxdi->write_mutex);
  533. /* alarm is pending if the alarm flag is set */
  534. alarm->pending = (readl(imxdi->ioaddr + DSR) & DSR_CAF) != 0;
  535. mutex_unlock(&imxdi->write_mutex);
  536. return 0;
  537. }
  538. /*
  539. * set the seconds portion of dryice alarm register
  540. */
  541. static int dryice_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  542. {
  543. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  544. int rc;
  545. /* write the new alarm time */
  546. rc = di_write_wait(imxdi, rtc_tm_to_time64(&alarm->time), DCAMR);
  547. if (rc)
  548. return rc;
  549. if (alarm->enabled)
  550. di_int_enable(imxdi, DIER_CAIE); /* enable alarm intr */
  551. else
  552. di_int_disable(imxdi, DIER_CAIE); /* disable alarm intr */
  553. return 0;
  554. }
  555. static const struct rtc_class_ops dryice_rtc_ops = {
  556. .read_time = dryice_rtc_read_time,
  557. .set_time = dryice_rtc_set_time,
  558. .alarm_irq_enable = dryice_rtc_alarm_irq_enable,
  559. .read_alarm = dryice_rtc_read_alarm,
  560. .set_alarm = dryice_rtc_set_alarm,
  561. };
  562. /*
  563. * interrupt handler for dryice "normal" and security violation interrupt
  564. */
  565. static irqreturn_t dryice_irq(int irq, void *dev_id)
  566. {
  567. struct imxdi_dev *imxdi = dev_id;
  568. u32 dsr, dier;
  569. irqreturn_t rc = IRQ_NONE;
  570. dier = readl(imxdi->ioaddr + DIER);
  571. dsr = readl(imxdi->ioaddr + DSR);
  572. /* handle the security violation event */
  573. if (dier & DIER_SVIE) {
  574. if (dsr & DSR_SVF) {
  575. /*
  576. * Disable the interrupt when this kind of event has
  577. * happened.
  578. * There cannot be more than one event of this type,
  579. * because it needs a complex state change
  580. * including a main power cycle to get again out of
  581. * this state.
  582. */
  583. di_int_disable(imxdi, DIER_SVIE);
  584. /* report the violation */
  585. di_report_tamper_info(imxdi, dsr);
  586. rc = IRQ_HANDLED;
  587. }
  588. }
  589. /* handle write complete and write error cases */
  590. if (dier & DIER_WCIE) {
  591. /*If the write wait queue is empty then there is no pending
  592. operations. It means the interrupt is for DryIce -Security.
  593. IRQ must be returned as none.*/
  594. if (list_empty_careful(&imxdi->write_wait.head))
  595. return rc;
  596. /* DSR_WCF clears itself on DSR read */
  597. if (dsr & (DSR_WCF | DSR_WEF)) {
  598. /* mask the interrupt */
  599. di_int_disable(imxdi, DIER_WCIE);
  600. /* save the dsr value for the wait queue */
  601. imxdi->dsr |= dsr;
  602. wake_up_interruptible(&imxdi->write_wait);
  603. rc = IRQ_HANDLED;
  604. }
  605. }
  606. /* handle the alarm case */
  607. if (dier & DIER_CAIE) {
  608. /* DSR_WCF clears itself on DSR read */
  609. if (dsr & DSR_CAF) {
  610. /* mask the interrupt */
  611. di_int_disable(imxdi, DIER_CAIE);
  612. /* finish alarm in user context */
  613. schedule_work(&imxdi->work);
  614. rc = IRQ_HANDLED;
  615. }
  616. }
  617. return rc;
  618. }
  619. /*
  620. * post the alarm event from user context so it can sleep
  621. * on the write completion.
  622. */
  623. static void dryice_work(struct work_struct *work)
  624. {
  625. struct imxdi_dev *imxdi = container_of(work,
  626. struct imxdi_dev, work);
  627. /* dismiss the interrupt (ignore error) */
  628. di_write_wait(imxdi, DSR_CAF, DSR);
  629. /* pass the alarm event to the rtc framework. */
  630. rtc_update_irq(imxdi->rtc, 1, RTC_AF | RTC_IRQF);
  631. }
  632. /*
  633. * probe for dryice rtc device
  634. */
  635. static int __init dryice_rtc_probe(struct platform_device *pdev)
  636. {
  637. struct imxdi_dev *imxdi;
  638. int norm_irq, sec_irq;
  639. int rc;
  640. imxdi = devm_kzalloc(&pdev->dev, sizeof(*imxdi), GFP_KERNEL);
  641. if (!imxdi)
  642. return -ENOMEM;
  643. imxdi->pdev = pdev;
  644. imxdi->ioaddr = devm_platform_ioremap_resource(pdev, 0);
  645. if (IS_ERR(imxdi->ioaddr))
  646. return PTR_ERR(imxdi->ioaddr);
  647. spin_lock_init(&imxdi->irq_lock);
  648. norm_irq = platform_get_irq(pdev, 0);
  649. if (norm_irq < 0)
  650. return norm_irq;
  651. /* the 2nd irq is the security violation irq
  652. * make this optional, don't break the device tree ABI
  653. */
  654. sec_irq = platform_get_irq(pdev, 1);
  655. if (sec_irq <= 0)
  656. sec_irq = IRQ_NOTCONNECTED;
  657. init_waitqueue_head(&imxdi->write_wait);
  658. INIT_WORK(&imxdi->work, dryice_work);
  659. mutex_init(&imxdi->write_mutex);
  660. imxdi->rtc = devm_rtc_allocate_device(&pdev->dev);
  661. if (IS_ERR(imxdi->rtc))
  662. return PTR_ERR(imxdi->rtc);
  663. imxdi->clk = devm_clk_get(&pdev->dev, NULL);
  664. if (IS_ERR(imxdi->clk))
  665. return PTR_ERR(imxdi->clk);
  666. rc = clk_prepare_enable(imxdi->clk);
  667. if (rc)
  668. return rc;
  669. /*
  670. * Initialize dryice hardware
  671. */
  672. /* mask all interrupts */
  673. writel(0, imxdi->ioaddr + DIER);
  674. rc = di_handle_state(imxdi);
  675. if (rc != 0)
  676. goto err;
  677. rc = devm_request_irq(&pdev->dev, norm_irq, dryice_irq,
  678. IRQF_SHARED, pdev->name, imxdi);
  679. if (rc) {
  680. dev_warn(&pdev->dev, "interrupt not available.\n");
  681. goto err;
  682. }
  683. rc = devm_request_irq(&pdev->dev, sec_irq, dryice_irq,
  684. IRQF_SHARED, pdev->name, imxdi);
  685. if (rc) {
  686. dev_warn(&pdev->dev, "security violation interrupt not available.\n");
  687. /* this is not an error, see above */
  688. }
  689. platform_set_drvdata(pdev, imxdi);
  690. device_init_wakeup(&pdev->dev, true);
  691. dev_pm_set_wake_irq(&pdev->dev, norm_irq);
  692. imxdi->rtc->ops = &dryice_rtc_ops;
  693. imxdi->rtc->range_max = U32_MAX;
  694. rc = devm_rtc_register_device(imxdi->rtc);
  695. if (rc)
  696. goto err;
  697. return 0;
  698. err:
  699. clk_disable_unprepare(imxdi->clk);
  700. return rc;
  701. }
  702. static int __exit dryice_rtc_remove(struct platform_device *pdev)
  703. {
  704. struct imxdi_dev *imxdi = platform_get_drvdata(pdev);
  705. flush_work(&imxdi->work);
  706. /* mask all interrupts */
  707. writel(0, imxdi->ioaddr + DIER);
  708. clk_disable_unprepare(imxdi->clk);
  709. return 0;
  710. }
  711. static const struct of_device_id dryice_dt_ids[] = {
  712. { .compatible = "fsl,imx25-rtc" },
  713. { /* sentinel */ }
  714. };
  715. MODULE_DEVICE_TABLE(of, dryice_dt_ids);
  716. static struct platform_driver dryice_rtc_driver = {
  717. .driver = {
  718. .name = "imxdi_rtc",
  719. .of_match_table = dryice_dt_ids,
  720. },
  721. .remove = __exit_p(dryice_rtc_remove),
  722. };
  723. module_platform_driver_probe(dryice_rtc_driver, dryice_rtc_probe);
  724. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  725. MODULE_AUTHOR("Baruch Siach <[email protected]>");
  726. MODULE_DESCRIPTION("IMX DryIce Realtime Clock Driver (RTC)");
  727. MODULE_LICENSE("GPL");