rtc-imxdi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. /* Some background:
  118. *
  119. * The DryIce unit is a complex security/tamper monitor device. To be able do
  120. * its job in a useful manner it runs a bigger statemachine to bring it into
  121. * security/tamper failure state and once again to bring it out of this state.
  122. *
  123. * This unit can be in one of three states:
  124. *
  125. * - "NON-VALID STATE"
  126. * always after the battery power was removed
  127. * - "FAILURE STATE"
  128. * if one of the enabled security events has happened
  129. * - "VALID STATE"
  130. * if the unit works as expected
  131. *
  132. * Everything stops when the unit enters the failure state including the RTC
  133. * counter (to be able to detect the time the security event happened).
  134. *
  135. * The following events (when enabled) let the DryIce unit enter the failure
  136. * state:
  137. *
  138. * - wire-mesh-tamper detect
  139. * - external tamper B detect
  140. * - external tamper A detect
  141. * - temperature tamper detect
  142. * - clock tamper detect
  143. * - voltage tamper detect
  144. * - RTC counter overflow
  145. * - monotonic counter overflow
  146. * - external boot
  147. *
  148. * If we find the DryIce unit in "FAILURE STATE" and the TDCHL cleared, we
  149. * can only detect this state. In this case the unit is completely locked and
  150. * must force a second "SYSTEM POR" to bring the DryIce into the
  151. * "NON-VALID STATE" + "FAILURE STATE" where a recovery is possible.
  152. * If the TDCHL is set in the "FAILURE STATE" we are out of luck. In this case
  153. * a battery power cycle is required.
  154. *
  155. * In the "NON-VALID STATE" + "FAILURE STATE" we can clear the "FAILURE STATE"
  156. * and recover the DryIce unit. By clearing the "NON-VALID STATE" as the last
  157. * task, we bring back this unit into life.
  158. */
  159. /*
  160. * Do a write into the unit without interrupt support.
  161. * We do not need to check the WEF here, because the only reason this kind of
  162. * write error can happen is if we write to the unit twice within the 122 us
  163. * interval. This cannot happen, since we are using this function only while
  164. * setting up the unit.
  165. */
  166. static void di_write_busy_wait(const struct imxdi_dev *imxdi, u32 val,
  167. unsigned reg)
  168. {
  169. /* do the register write */
  170. writel(val, imxdi->ioaddr + reg);
  171. /*
  172. * now it takes four 32,768 kHz clock cycles to take
  173. * the change into effect = 122 us
  174. */
  175. usleep_range(130, 200);
  176. }
  177. static void di_report_tamper_info(struct imxdi_dev *imxdi, u32 dsr)
  178. {
  179. u32 dtcr;
  180. dtcr = readl(imxdi->ioaddr + DTCR);
  181. dev_emerg(&imxdi->pdev->dev, "DryIce tamper event detected\n");
  182. /* the following flags force a transition into the "FAILURE STATE" */
  183. if (dsr & DSR_VTD)
  184. dev_emerg(&imxdi->pdev->dev, "%sVoltage Tamper Event\n",
  185. dtcr & DTCR_VTE ? "" : "Spurious ");
  186. if (dsr & DSR_CTD)
  187. dev_emerg(&imxdi->pdev->dev, "%s32768 Hz Clock Tamper Event\n",
  188. dtcr & DTCR_CTE ? "" : "Spurious ");
  189. if (dsr & DSR_TTD)
  190. dev_emerg(&imxdi->pdev->dev, "%sTemperature Tamper Event\n",
  191. dtcr & DTCR_TTE ? "" : "Spurious ");
  192. if (dsr & DSR_SAD)
  193. dev_emerg(&imxdi->pdev->dev,
  194. "%sSecure Controller Alarm Event\n",
  195. dtcr & DTCR_SAIE ? "" : "Spurious ");
  196. if (dsr & DSR_EBD)
  197. dev_emerg(&imxdi->pdev->dev, "%sExternal Boot Tamper Event\n",
  198. dtcr & DTCR_EBE ? "" : "Spurious ");
  199. if (dsr & DSR_ETAD)
  200. dev_emerg(&imxdi->pdev->dev, "%sExternal Tamper A Event\n",
  201. dtcr & DTCR_ETAE ? "" : "Spurious ");
  202. if (dsr & DSR_ETBD)
  203. dev_emerg(&imxdi->pdev->dev, "%sExternal Tamper B Event\n",
  204. dtcr & DTCR_ETBE ? "" : "Spurious ");
  205. if (dsr & DSR_WTD)
  206. dev_emerg(&imxdi->pdev->dev, "%sWire-mesh Tamper Event\n",
  207. dtcr & DTCR_WTE ? "" : "Spurious ");
  208. if (dsr & DSR_MCO)
  209. dev_emerg(&imxdi->pdev->dev,
  210. "%sMonotonic-counter Overflow Event\n",
  211. dtcr & DTCR_MOE ? "" : "Spurious ");
  212. if (dsr & DSR_TCO)
  213. dev_emerg(&imxdi->pdev->dev, "%sTimer-counter Overflow Event\n",
  214. dtcr & DTCR_TOE ? "" : "Spurious ");
  215. }
  216. static void di_what_is_to_be_done(struct imxdi_dev *imxdi,
  217. const char *power_supply)
  218. {
  219. dev_emerg(&imxdi->pdev->dev, "Please cycle the %s power supply in order to get the DryIce/RTC unit working again\n",
  220. power_supply);
  221. }
  222. static int di_handle_failure_state(struct imxdi_dev *imxdi, u32 dsr)
  223. {
  224. u32 dcr;
  225. dev_dbg(&imxdi->pdev->dev, "DSR register reports: %08X\n", dsr);
  226. /* report the cause */
  227. di_report_tamper_info(imxdi, dsr);
  228. dcr = readl(imxdi->ioaddr + DCR);
  229. if (dcr & DCR_FSHL) {
  230. /* we are out of luck */
  231. di_what_is_to_be_done(imxdi, "battery");
  232. return -ENODEV;
  233. }
  234. /*
  235. * with the next SYSTEM POR we will transit from the "FAILURE STATE"
  236. * into the "NON-VALID STATE" + "FAILURE STATE"
  237. */
  238. di_what_is_to_be_done(imxdi, "main");
  239. return -ENODEV;
  240. }
  241. static int di_handle_valid_state(struct imxdi_dev *imxdi, u32 dsr)
  242. {
  243. /* initialize alarm */
  244. di_write_busy_wait(imxdi, DCAMR_UNSET, DCAMR);
  245. di_write_busy_wait(imxdi, 0, DCALR);
  246. /* clear alarm flag */
  247. if (dsr & DSR_CAF)
  248. di_write_busy_wait(imxdi, DSR_CAF, DSR);
  249. return 0;
  250. }
  251. static int di_handle_invalid_state(struct imxdi_dev *imxdi, u32 dsr)
  252. {
  253. u32 dcr, sec;
  254. /*
  255. * lets disable all sources which can force the DryIce unit into
  256. * the "FAILURE STATE" for now
  257. */
  258. di_write_busy_wait(imxdi, 0x00000000, DTCR);
  259. /* and lets protect them at runtime from any change */
  260. di_write_busy_wait(imxdi, DCR_TDCSL, DCR);
  261. sec = readl(imxdi->ioaddr + DTCMR);
  262. if (sec != 0)
  263. dev_warn(&imxdi->pdev->dev,
  264. "The security violation has happend at %u seconds\n",
  265. sec);
  266. /*
  267. * the timer cannot be set/modified if
  268. * - the TCHL or TCSL bit is set in DCR
  269. */
  270. dcr = readl(imxdi->ioaddr + DCR);
  271. if (!(dcr & DCR_TCE)) {
  272. if (dcr & DCR_TCHL) {
  273. /* we are out of luck */
  274. di_what_is_to_be_done(imxdi, "battery");
  275. return -ENODEV;
  276. }
  277. if (dcr & DCR_TCSL) {
  278. di_what_is_to_be_done(imxdi, "main");
  279. return -ENODEV;
  280. }
  281. }
  282. /*
  283. * - the timer counter stops/is stopped if
  284. * - its overflow flag is set (TCO in DSR)
  285. * -> clear overflow bit to make it count again
  286. * - NVF is set in DSR
  287. * -> clear non-valid bit to make it count again
  288. * - its TCE (DCR) is cleared
  289. * -> set TCE to make it count
  290. * - it was never set before
  291. * -> write a time into it (required again if the NVF was set)
  292. */
  293. /* state handled */
  294. di_write_busy_wait(imxdi, DSR_NVF, DSR);
  295. /* clear overflow flag */
  296. di_write_busy_wait(imxdi, DSR_TCO, DSR);
  297. /* enable the counter */
  298. di_write_busy_wait(imxdi, dcr | DCR_TCE, DCR);
  299. /* set and trigger it to make it count */
  300. di_write_busy_wait(imxdi, sec, DTCMR);
  301. /* now prepare for the valid state */
  302. return di_handle_valid_state(imxdi, __raw_readl(imxdi->ioaddr + DSR));
  303. }
  304. static int di_handle_invalid_and_failure_state(struct imxdi_dev *imxdi, u32 dsr)
  305. {
  306. u32 dcr;
  307. /*
  308. * now we must first remove the tamper sources in order to get the
  309. * device out of the "FAILURE STATE"
  310. * To disable any of the following sources we need to modify the DTCR
  311. */
  312. if (dsr & (DSR_WTD | DSR_ETBD | DSR_ETAD | DSR_EBD | DSR_SAD |
  313. DSR_TTD | DSR_CTD | DSR_VTD | DSR_MCO | DSR_TCO)) {
  314. dcr = __raw_readl(imxdi->ioaddr + DCR);
  315. if (dcr & DCR_TDCHL) {
  316. /*
  317. * the tamper register is locked. We cannot disable the
  318. * tamper detection. The TDCHL can only be reset by a
  319. * DRYICE POR, but we cannot force a DRYICE POR in
  320. * softwere because we are still in "FAILURE STATE".
  321. * We need a DRYICE POR via battery power cycling....
  322. */
  323. /*
  324. * out of luck!
  325. * we cannot disable them without a DRYICE POR
  326. */
  327. di_what_is_to_be_done(imxdi, "battery");
  328. return -ENODEV;
  329. }
  330. if (dcr & DCR_TDCSL) {
  331. /* a soft lock can be removed by a SYSTEM POR */
  332. di_what_is_to_be_done(imxdi, "main");
  333. return -ENODEV;
  334. }
  335. }
  336. /* disable all sources */
  337. di_write_busy_wait(imxdi, 0x00000000, DTCR);
  338. /* clear the status bits now */
  339. di_write_busy_wait(imxdi, dsr & (DSR_WTD | DSR_ETBD | DSR_ETAD |
  340. DSR_EBD | DSR_SAD | DSR_TTD | DSR_CTD | DSR_VTD |
  341. DSR_MCO | DSR_TCO), DSR);
  342. dsr = readl(imxdi->ioaddr + DSR);
  343. if ((dsr & ~(DSR_NVF | DSR_SVF | DSR_WBF | DSR_WNF |
  344. DSR_WCF | DSR_WEF)) != 0)
  345. dev_warn(&imxdi->pdev->dev,
  346. "There are still some sources of pain in DSR: %08x!\n",
  347. dsr & ~(DSR_NVF | DSR_SVF | DSR_WBF | DSR_WNF |
  348. DSR_WCF | DSR_WEF));
  349. /*
  350. * now we are trying to clear the "Security-violation flag" to
  351. * get the DryIce out of this state
  352. */
  353. di_write_busy_wait(imxdi, DSR_SVF, DSR);
  354. /* success? */
  355. dsr = readl(imxdi->ioaddr + DSR);
  356. if (dsr & DSR_SVF) {
  357. dev_crit(&imxdi->pdev->dev,
  358. "Cannot clear the security violation flag. We are ending up in an endless loop!\n");
  359. /* last resort */
  360. di_what_is_to_be_done(imxdi, "battery");
  361. return -ENODEV;
  362. }
  363. /*
  364. * now we have left the "FAILURE STATE" and ending up in the
  365. * "NON-VALID STATE" time to recover everything
  366. */
  367. return di_handle_invalid_state(imxdi, dsr);
  368. }
  369. static int di_handle_state(struct imxdi_dev *imxdi)
  370. {
  371. int rc;
  372. u32 dsr;
  373. dsr = readl(imxdi->ioaddr + DSR);
  374. switch (dsr & (DSR_NVF | DSR_SVF)) {
  375. case DSR_NVF:
  376. dev_warn(&imxdi->pdev->dev, "Invalid stated unit detected\n");
  377. rc = di_handle_invalid_state(imxdi, dsr);
  378. break;
  379. case DSR_SVF:
  380. dev_warn(&imxdi->pdev->dev, "Failure stated unit detected\n");
  381. rc = di_handle_failure_state(imxdi, dsr);
  382. break;
  383. case DSR_NVF | DSR_SVF:
  384. dev_warn(&imxdi->pdev->dev,
  385. "Failure+Invalid stated unit detected\n");
  386. rc = di_handle_invalid_and_failure_state(imxdi, dsr);
  387. break;
  388. default:
  389. dev_notice(&imxdi->pdev->dev, "Unlocked unit detected\n");
  390. rc = di_handle_valid_state(imxdi, dsr);
  391. }
  392. return rc;
  393. }
  394. /*
  395. * enable a dryice interrupt
  396. */
  397. static void di_int_enable(struct imxdi_dev *imxdi, u32 intr)
  398. {
  399. unsigned long flags;
  400. spin_lock_irqsave(&imxdi->irq_lock, flags);
  401. writel(readl(imxdi->ioaddr + DIER) | intr,
  402. imxdi->ioaddr + DIER);
  403. spin_unlock_irqrestore(&imxdi->irq_lock, flags);
  404. }
  405. /*
  406. * disable a dryice interrupt
  407. */
  408. static void di_int_disable(struct imxdi_dev *imxdi, u32 intr)
  409. {
  410. unsigned long flags;
  411. spin_lock_irqsave(&imxdi->irq_lock, flags);
  412. writel(readl(imxdi->ioaddr + DIER) & ~intr,
  413. imxdi->ioaddr + DIER);
  414. spin_unlock_irqrestore(&imxdi->irq_lock, flags);
  415. }
  416. /*
  417. * This function attempts to clear the dryice write-error flag.
  418. *
  419. * A dryice write error is similar to a bus fault and should not occur in
  420. * normal operation. Clearing the flag requires another write, so the root
  421. * cause of the problem may need to be fixed before the flag can be cleared.
  422. */
  423. static void clear_write_error(struct imxdi_dev *imxdi)
  424. {
  425. int cnt;
  426. dev_warn(&imxdi->pdev->dev, "WARNING: Register write error!\n");
  427. /* clear the write error flag */
  428. writel(DSR_WEF, imxdi->ioaddr + DSR);
  429. /* wait for it to take effect */
  430. for (cnt = 0; cnt < 1000; cnt++) {
  431. if ((readl(imxdi->ioaddr + DSR) & DSR_WEF) == 0)
  432. return;
  433. udelay(10);
  434. }
  435. dev_err(&imxdi->pdev->dev,
  436. "ERROR: Cannot clear write-error flag!\n");
  437. }
  438. /*
  439. * Write a dryice register and wait until it completes.
  440. *
  441. * This function uses interrupts to determine when the
  442. * write has completed.
  443. */
  444. static int di_write_wait(struct imxdi_dev *imxdi, u32 val, int reg)
  445. {
  446. int ret;
  447. int rc = 0;
  448. /* serialize register writes */
  449. mutex_lock(&imxdi->write_mutex);
  450. /* enable the write-complete interrupt */
  451. di_int_enable(imxdi, DIER_WCIE);
  452. imxdi->dsr = 0;
  453. /* do the register write */
  454. writel(val, imxdi->ioaddr + reg);
  455. /* wait for the write to finish */
  456. ret = wait_event_interruptible_timeout(imxdi->write_wait,
  457. imxdi->dsr & (DSR_WCF | DSR_WEF), msecs_to_jiffies(1));
  458. if (ret < 0) {
  459. rc = ret;
  460. goto out;
  461. } else if (ret == 0) {
  462. dev_warn(&imxdi->pdev->dev,
  463. "Write-wait timeout "
  464. "val = 0x%08x reg = 0x%08x\n", val, reg);
  465. }
  466. /* check for write error */
  467. if (imxdi->dsr & DSR_WEF) {
  468. clear_write_error(imxdi);
  469. rc = -EIO;
  470. }
  471. out:
  472. mutex_unlock(&imxdi->write_mutex);
  473. return rc;
  474. }
  475. /*
  476. * read the seconds portion of the current time from the dryice time counter
  477. */
  478. static int dryice_rtc_read_time(struct device *dev, struct rtc_time *tm)
  479. {
  480. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  481. unsigned long now;
  482. now = readl(imxdi->ioaddr + DTCMR);
  483. rtc_time_to_tm(now, tm);
  484. return 0;
  485. }
  486. /*
  487. * set the seconds portion of dryice time counter and clear the
  488. * fractional part.
  489. */
  490. static int dryice_rtc_set_mmss(struct device *dev, unsigned long secs)
  491. {
  492. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  493. u32 dcr, dsr;
  494. int rc;
  495. dcr = readl(imxdi->ioaddr + DCR);
  496. dsr = readl(imxdi->ioaddr + DSR);
  497. if (!(dcr & DCR_TCE) || (dsr & DSR_SVF)) {
  498. if (dcr & DCR_TCHL) {
  499. /* we are even more out of luck */
  500. di_what_is_to_be_done(imxdi, "battery");
  501. return -EPERM;
  502. }
  503. if ((dcr & DCR_TCSL) || (dsr & DSR_SVF)) {
  504. /* we are out of luck for now */
  505. di_what_is_to_be_done(imxdi, "main");
  506. return -EPERM;
  507. }
  508. }
  509. /* zero the fractional part first */
  510. rc = di_write_wait(imxdi, 0, DTCLR);
  511. if (rc != 0)
  512. return rc;
  513. rc = di_write_wait(imxdi, secs, DTCMR);
  514. if (rc != 0)
  515. return rc;
  516. return di_write_wait(imxdi, readl(imxdi->ioaddr + DCR) | DCR_TCE, DCR);
  517. }
  518. static int dryice_rtc_alarm_irq_enable(struct device *dev,
  519. unsigned int enabled)
  520. {
  521. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  522. if (enabled)
  523. di_int_enable(imxdi, DIER_CAIE);
  524. else
  525. di_int_disable(imxdi, DIER_CAIE);
  526. return 0;
  527. }
  528. /*
  529. * read the seconds portion of the alarm register.
  530. * the fractional part of the alarm register is always zero.
  531. */
  532. static int dryice_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  533. {
  534. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  535. u32 dcamr;
  536. dcamr = readl(imxdi->ioaddr + DCAMR);
  537. rtc_time_to_tm(dcamr, &alarm->time);
  538. /* alarm is enabled if the interrupt is enabled */
  539. alarm->enabled = (readl(imxdi->ioaddr + DIER) & DIER_CAIE) != 0;
  540. /* don't allow the DSR read to mess up DSR_WCF */
  541. mutex_lock(&imxdi->write_mutex);
  542. /* alarm is pending if the alarm flag is set */
  543. alarm->pending = (readl(imxdi->ioaddr + DSR) & DSR_CAF) != 0;
  544. mutex_unlock(&imxdi->write_mutex);
  545. return 0;
  546. }
  547. /*
  548. * set the seconds portion of dryice alarm register
  549. */
  550. static int dryice_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  551. {
  552. struct imxdi_dev *imxdi = dev_get_drvdata(dev);
  553. unsigned long now;
  554. unsigned long alarm_time;
  555. int rc;
  556. rc = rtc_tm_to_time(&alarm->time, &alarm_time);
  557. if (rc)
  558. return rc;
  559. /* don't allow setting alarm in the past */
  560. now = readl(imxdi->ioaddr + DTCMR);
  561. if (alarm_time < now)
  562. return -EINVAL;
  563. /* write the new alarm time */
  564. rc = di_write_wait(imxdi, (u32)alarm_time, DCAMR);
  565. if (rc)
  566. return rc;
  567. if (alarm->enabled)
  568. di_int_enable(imxdi, DIER_CAIE); /* enable alarm intr */
  569. else
  570. di_int_disable(imxdi, DIER_CAIE); /* disable alarm intr */
  571. return 0;
  572. }
  573. static struct rtc_class_ops dryice_rtc_ops = {
  574. .read_time = dryice_rtc_read_time,
  575. .set_mmss = dryice_rtc_set_mmss,
  576. .alarm_irq_enable = dryice_rtc_alarm_irq_enable,
  577. .read_alarm = dryice_rtc_read_alarm,
  578. .set_alarm = dryice_rtc_set_alarm,
  579. };
  580. /*
  581. * dryice "normal" interrupt handler
  582. */
  583. static irqreturn_t dryice_norm_irq(int irq, void *dev_id)
  584. {
  585. struct imxdi_dev *imxdi = dev_id;
  586. u32 dsr, dier;
  587. irqreturn_t rc = IRQ_NONE;
  588. dier = readl(imxdi->ioaddr + DIER);
  589. dsr = readl(imxdi->ioaddr + DSR);
  590. /* handle the security violation event */
  591. if (dier & DIER_SVIE) {
  592. if (dsr & DSR_SVF) {
  593. /*
  594. * Disable the interrupt when this kind of event has
  595. * happened.
  596. * There cannot be more than one event of this type,
  597. * because it needs a complex state change
  598. * including a main power cycle to get again out of
  599. * this state.
  600. */
  601. di_int_disable(imxdi, DIER_SVIE);
  602. /* report the violation */
  603. di_report_tamper_info(imxdi, dsr);
  604. rc = IRQ_HANDLED;
  605. }
  606. }
  607. /* handle write complete and write error cases */
  608. if (dier & DIER_WCIE) {
  609. /*If the write wait queue is empty then there is no pending
  610. operations. It means the interrupt is for DryIce -Security.
  611. IRQ must be returned as none.*/
  612. if (list_empty_careful(&imxdi->write_wait.task_list))
  613. return rc;
  614. /* DSR_WCF clears itself on DSR read */
  615. if (dsr & (DSR_WCF | DSR_WEF)) {
  616. /* mask the interrupt */
  617. di_int_disable(imxdi, DIER_WCIE);
  618. /* save the dsr value for the wait queue */
  619. imxdi->dsr |= dsr;
  620. wake_up_interruptible(&imxdi->write_wait);
  621. rc = IRQ_HANDLED;
  622. }
  623. }
  624. /* handle the alarm case */
  625. if (dier & DIER_CAIE) {
  626. /* DSR_WCF clears itself on DSR read */
  627. if (dsr & DSR_CAF) {
  628. /* mask the interrupt */
  629. di_int_disable(imxdi, DIER_CAIE);
  630. /* finish alarm in user context */
  631. schedule_work(&imxdi->work);
  632. rc = IRQ_HANDLED;
  633. }
  634. }
  635. return rc;
  636. }
  637. /*
  638. * post the alarm event from user context so it can sleep
  639. * on the write completion.
  640. */
  641. static void dryice_work(struct work_struct *work)
  642. {
  643. struct imxdi_dev *imxdi = container_of(work,
  644. struct imxdi_dev, work);
  645. /* dismiss the interrupt (ignore error) */
  646. di_write_wait(imxdi, DSR_CAF, DSR);
  647. /* pass the alarm event to the rtc framework. */
  648. rtc_update_irq(imxdi->rtc, 1, RTC_AF | RTC_IRQF);
  649. }
  650. /*
  651. * probe for dryice rtc device
  652. */
  653. static int __init dryice_rtc_probe(struct platform_device *pdev)
  654. {
  655. struct resource *res;
  656. struct imxdi_dev *imxdi;
  657. int rc;
  658. imxdi = devm_kzalloc(&pdev->dev, sizeof(*imxdi), GFP_KERNEL);
  659. if (!imxdi)
  660. return -ENOMEM;
  661. imxdi->pdev = pdev;
  662. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  663. imxdi->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  664. if (IS_ERR(imxdi->ioaddr))
  665. return PTR_ERR(imxdi->ioaddr);
  666. spin_lock_init(&imxdi->irq_lock);
  667. imxdi->irq = platform_get_irq(pdev, 0);
  668. if (imxdi->irq < 0)
  669. return imxdi->irq;
  670. init_waitqueue_head(&imxdi->write_wait);
  671. INIT_WORK(&imxdi->work, dryice_work);
  672. mutex_init(&imxdi->write_mutex);
  673. imxdi->clk = devm_clk_get(&pdev->dev, NULL);
  674. if (IS_ERR(imxdi->clk))
  675. return PTR_ERR(imxdi->clk);
  676. rc = clk_prepare_enable(imxdi->clk);
  677. if (rc)
  678. return rc;
  679. /*
  680. * Initialize dryice hardware
  681. */
  682. /* mask all interrupts */
  683. writel(0, imxdi->ioaddr + DIER);
  684. rc = di_handle_state(imxdi);
  685. if (rc != 0)
  686. goto err;
  687. rc = devm_request_irq(&pdev->dev, imxdi->irq, dryice_norm_irq,
  688. IRQF_SHARED, pdev->name, imxdi);
  689. if (rc) {
  690. dev_warn(&pdev->dev, "interrupt not available.\n");
  691. goto err;
  692. }
  693. platform_set_drvdata(pdev, imxdi);
  694. imxdi->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  695. &dryice_rtc_ops, THIS_MODULE);
  696. if (IS_ERR(imxdi->rtc)) {
  697. rc = PTR_ERR(imxdi->rtc);
  698. goto err;
  699. }
  700. return 0;
  701. err:
  702. clk_disable_unprepare(imxdi->clk);
  703. return rc;
  704. }
  705. static int __exit dryice_rtc_remove(struct platform_device *pdev)
  706. {
  707. struct imxdi_dev *imxdi = platform_get_drvdata(pdev);
  708. flush_work(&imxdi->work);
  709. /* mask all interrupts */
  710. writel(0, imxdi->ioaddr + DIER);
  711. clk_disable_unprepare(imxdi->clk);
  712. return 0;
  713. }
  714. #ifdef CONFIG_OF
  715. static const struct of_device_id dryice_dt_ids[] = {
  716. { .compatible = "fsl,imx25-rtc" },
  717. { /* sentinel */ }
  718. };
  719. MODULE_DEVICE_TABLE(of, dryice_dt_ids);
  720. #endif
  721. static struct platform_driver dryice_rtc_driver = {
  722. .driver = {
  723. .name = "imxdi_rtc",
  724. .of_match_table = of_match_ptr(dryice_dt_ids),
  725. },
  726. .remove = __exit_p(dryice_rtc_remove),
  727. };
  728. module_platform_driver_probe(dryice_rtc_driver, dryice_rtc_probe);
  729. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  730. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  731. MODULE_DESCRIPTION("IMX DryIce Realtime Clock Driver (RTC)");
  732. MODULE_LICENSE("GPL");