rtc-imxdi.c 25 KB

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