imxdi.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * (C) Copyright 2009-2012 ADVANSEE
  3. * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
  4. *
  5. * Based on the Linux rtc-imxdi.c driver, which is:
  6. * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  7. * Copyright 2010 Orex Computed Radiography
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /*
  28. * Date & Time support for Freescale i.MX DryIce RTC
  29. */
  30. #include <common.h>
  31. #include <command.h>
  32. #include <linux/compat.h>
  33. #include <rtc.h>
  34. #if defined(CONFIG_CMD_DATE)
  35. #include <asm/io.h>
  36. #include <asm/arch/imx-regs.h>
  37. /* DryIce Register Definitions */
  38. struct imxdi_regs {
  39. u32 dtcmr; /* Time Counter MSB Reg */
  40. u32 dtclr; /* Time Counter LSB Reg */
  41. u32 dcamr; /* Clock Alarm MSB Reg */
  42. u32 dcalr; /* Clock Alarm LSB Reg */
  43. u32 dcr; /* Control Reg */
  44. u32 dsr; /* Status Reg */
  45. u32 dier; /* Interrupt Enable Reg */
  46. };
  47. #define DCAMR_UNSET 0xFFFFFFFF /* doomsday - 1 sec */
  48. #define DCR_TCE (1 << 3) /* Time Counter Enable */
  49. #define DSR_WBF (1 << 10) /* Write Busy Flag */
  50. #define DSR_WNF (1 << 9) /* Write Next Flag */
  51. #define DSR_WCF (1 << 8) /* Write Complete Flag */
  52. #define DSR_WEF (1 << 7) /* Write Error Flag */
  53. #define DSR_CAF (1 << 4) /* Clock Alarm Flag */
  54. #define DSR_NVF (1 << 1) /* Non-Valid Flag */
  55. #define DSR_SVF (1 << 0) /* Security Violation Flag */
  56. #define DIER_WNIE (1 << 9) /* Write Next Interrupt Enable */
  57. #define DIER_WCIE (1 << 8) /* Write Complete Interrupt Enable */
  58. #define DIER_WEIE (1 << 7) /* Write Error Interrupt Enable */
  59. #define DIER_CAIE (1 << 4) /* Clock Alarm Interrupt Enable */
  60. /* Driver Private Data */
  61. struct imxdi_data {
  62. struct imxdi_regs __iomem *regs;
  63. int init_done;
  64. };
  65. static struct imxdi_data data;
  66. /*
  67. * This function attempts to clear the dryice write-error flag.
  68. *
  69. * A dryice write error is similar to a bus fault and should not occur in
  70. * normal operation. Clearing the flag requires another write, so the root
  71. * cause of the problem may need to be fixed before the flag can be cleared.
  72. */
  73. static void clear_write_error(void)
  74. {
  75. int cnt;
  76. puts("### Warning: RTC - Register write error!\n");
  77. /* clear the write error flag */
  78. __raw_writel(DSR_WEF, &data.regs->dsr);
  79. /* wait for it to take effect */
  80. for (cnt = 0; cnt < 1000; cnt++) {
  81. if ((__raw_readl(&data.regs->dsr) & DSR_WEF) == 0)
  82. return;
  83. udelay(10);
  84. }
  85. puts("### Error: RTC - Cannot clear write-error flag!\n");
  86. }
  87. /*
  88. * Write a dryice register and wait until it completes.
  89. *
  90. * Use interrupt flags to determine when the write has completed.
  91. */
  92. #define DI_WRITE_WAIT(val, reg) \
  93. ( \
  94. /* do the register write */ \
  95. __raw_writel((val), &data.regs->reg), \
  96. \
  97. di_write_wait((val), #reg) \
  98. )
  99. static int di_write_wait(u32 val, const char *reg)
  100. {
  101. int cnt;
  102. int ret = 0;
  103. int rc = 0;
  104. /* wait for the write to finish */
  105. for (cnt = 0; cnt < 100; cnt++) {
  106. if ((__raw_readl(&data.regs->dsr) & (DSR_WCF | DSR_WEF)) != 0) {
  107. ret = 1;
  108. break;
  109. }
  110. udelay(10);
  111. }
  112. if (ret == 0)
  113. printf("### Warning: RTC - Write-wait timeout "
  114. "val = 0x%.8x reg = %s\n", val, reg);
  115. /* check for write error */
  116. if (__raw_readl(&data.regs->dsr) & DSR_WEF) {
  117. clear_write_error();
  118. rc = -1;
  119. }
  120. return rc;
  121. }
  122. /*
  123. * Initialize dryice hardware
  124. */
  125. static int di_init(void)
  126. {
  127. int rc = 0;
  128. data.regs = (struct imxdi_regs __iomem *)IMX_DRYICE_BASE;
  129. /* mask all interrupts */
  130. __raw_writel(0, &data.regs->dier);
  131. /* put dryice into valid state */
  132. if (__raw_readl(&data.regs->dsr) & DSR_NVF) {
  133. rc = DI_WRITE_WAIT(DSR_NVF | DSR_SVF, dsr);
  134. if (rc)
  135. goto err;
  136. }
  137. /* initialize alarm */
  138. rc = DI_WRITE_WAIT(DCAMR_UNSET, dcamr);
  139. if (rc)
  140. goto err;
  141. rc = DI_WRITE_WAIT(0, dcalr);
  142. if (rc)
  143. goto err;
  144. /* clear alarm flag */
  145. if (__raw_readl(&data.regs->dsr) & DSR_CAF) {
  146. rc = DI_WRITE_WAIT(DSR_CAF, dsr);
  147. if (rc)
  148. goto err;
  149. }
  150. /* the timer won't count if it has never been written to */
  151. if (__raw_readl(&data.regs->dtcmr) == 0) {
  152. rc = DI_WRITE_WAIT(0, dtcmr);
  153. if (rc)
  154. goto err;
  155. }
  156. /* start keeping time */
  157. if (!(__raw_readl(&data.regs->dcr) & DCR_TCE)) {
  158. rc = DI_WRITE_WAIT(__raw_readl(&data.regs->dcr) | DCR_TCE, dcr);
  159. if (rc)
  160. goto err;
  161. }
  162. data.init_done = 1;
  163. return 0;
  164. err:
  165. return rc;
  166. }
  167. int rtc_get(struct rtc_time *tmp)
  168. {
  169. unsigned long now;
  170. int rc = 0;
  171. if (!data.init_done) {
  172. rc = di_init();
  173. if (rc)
  174. goto err;
  175. }
  176. now = __raw_readl(&data.regs->dtcmr);
  177. to_tm(now, tmp);
  178. err:
  179. return rc;
  180. }
  181. int rtc_set(struct rtc_time *tmp)
  182. {
  183. unsigned long now;
  184. int rc;
  185. if (!data.init_done) {
  186. rc = di_init();
  187. if (rc)
  188. goto err;
  189. }
  190. now = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday,
  191. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  192. /* zero the fractional part first */
  193. rc = DI_WRITE_WAIT(0, dtclr);
  194. if (rc == 0)
  195. rc = DI_WRITE_WAIT(now, dtcmr);
  196. err:
  197. return rc;
  198. }
  199. void rtc_reset(void)
  200. {
  201. di_init();
  202. }
  203. #endif