rx8025.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007
  4. * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com.
  5. */
  6. /*
  7. * Epson RX8025 RTC driver.
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <dm.h>
  12. #include <i2c.h>
  13. #include <rtc.h>
  14. /*---------------------------------------------------------------------*/
  15. #undef DEBUG_RTC
  16. #ifdef DEBUG_RTC
  17. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  18. #else
  19. #define DEBUGR(fmt,args...)
  20. #endif
  21. /*---------------------------------------------------------------------*/
  22. enum rx_model {
  23. model_rx_8025,
  24. model_rx_8035,
  25. };
  26. /*
  27. * RTC register addresses
  28. */
  29. #define RTC_SEC_REG_ADDR 0x00
  30. #define RTC_MIN_REG_ADDR 0x01
  31. #define RTC_HR_REG_ADDR 0x02
  32. #define RTC_DAY_REG_ADDR 0x03
  33. #define RTC_DATE_REG_ADDR 0x04
  34. #define RTC_MON_REG_ADDR 0x05
  35. #define RTC_YR_REG_ADDR 0x06
  36. #define RTC_OFFSET_REG_ADDR 0x07
  37. #define RTC_CTL1_REG_ADDR 0x0e
  38. #define RTC_CTL2_REG_ADDR 0x0f
  39. /*
  40. * Control register 1 bits
  41. */
  42. #define RTC_CTL1_BIT_2412 0x20
  43. /*
  44. * Control register 2 bits
  45. */
  46. #define RTC_CTL2_BIT_PON 0x10
  47. #define RTC_CTL2_BIT_VDET 0x40
  48. #define RTC_CTL2_BIT_XST 0x20
  49. #define RTC_CTL2_BIT_VDSL 0x80
  50. /*
  51. * Note: the RX8025 I2C RTC requires register
  52. * reads and write to consist of a single bus
  53. * cycle. It is not allowed to write the register
  54. * address in a first cycle that is terminated by
  55. * a STOP condition. The chips needs a 'restart'
  56. * sequence (start sequence without a prior stop).
  57. */
  58. #define rtc_read(reg) buf[(reg) & 0xf]
  59. static int rtc_write(struct udevice *dev, uchar reg, uchar val);
  60. static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
  61. {
  62. int xstp = ctrl2 & RTC_CTL2_BIT_XST;
  63. /* XSTP bit has different polarity on RX-8025 vs RX-8035.
  64. * RX-8025: 0 == oscillator stopped
  65. * RX-8035: 1 == oscillator stopped
  66. */
  67. if (model == model_rx_8025)
  68. xstp = !xstp;
  69. return xstp;
  70. }
  71. /*
  72. * Get the current time from the RTC
  73. */
  74. static int rx8025_rtc_get(struct udevice *dev, struct rtc_time *tmp)
  75. {
  76. int rel = 0;
  77. uchar sec, min, hour, mday, wday, mon, year, ctl2;
  78. uchar buf[16];
  79. if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
  80. printf("Error reading from RTC\n");
  81. return -EIO;
  82. }
  83. sec = rtc_read(RTC_SEC_REG_ADDR);
  84. min = rtc_read(RTC_MIN_REG_ADDR);
  85. hour = rtc_read(RTC_HR_REG_ADDR);
  86. wday = rtc_read(RTC_DAY_REG_ADDR);
  87. mday = rtc_read(RTC_DATE_REG_ADDR);
  88. mon = rtc_read(RTC_MON_REG_ADDR);
  89. year = rtc_read(RTC_YR_REG_ADDR);
  90. DEBUGR("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  91. "hr: %02x min: %02x sec: %02x\n",
  92. year, mon, mday, wday, hour, min, sec);
  93. /* dump status */
  94. ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
  95. if (ctl2 & RTC_CTL2_BIT_PON) {
  96. printf("RTC: power-on detected\n");
  97. rel = -1;
  98. }
  99. if (ctl2 & RTC_CTL2_BIT_VDET) {
  100. printf("RTC: voltage drop detected\n");
  101. rel = -1;
  102. }
  103. if (rx8025_is_osc_stopped(dev->driver_data, ctl2)) {
  104. printf("RTC: oscillator stop detected\n");
  105. rel = -1;
  106. }
  107. tmp->tm_sec = bcd2bin(sec & 0x7F);
  108. tmp->tm_min = bcd2bin(min & 0x7F);
  109. if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412)
  110. tmp->tm_hour = bcd2bin(hour & 0x3F);
  111. else
  112. tmp->tm_hour = bcd2bin(hour & 0x1F) % 12 +
  113. ((hour & 0x20) ? 12 : 0);
  114. tmp->tm_mday = bcd2bin (mday & 0x3F);
  115. tmp->tm_mon = bcd2bin (mon & 0x1F);
  116. tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
  117. tmp->tm_wday = bcd2bin (wday & 0x07);
  118. tmp->tm_yday = 0;
  119. tmp->tm_isdst= 0;
  120. DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  121. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  122. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  123. return rel;
  124. }
  125. /*
  126. * Set the RTC
  127. */
  128. static int rx8025_rtc_set(struct udevice *dev, const struct rtc_time *tmp)
  129. {
  130. /* To work around the read/write cycle issue mentioned
  131. * at the top of this file, write all the time registers
  132. * in one I2C transaction
  133. */
  134. u8 write_op[8];
  135. /* 2412 flag must be set before doing a RTC write,
  136. * otherwise the seconds and minute register
  137. * will be cleared when the flag is set
  138. */
  139. if (rtc_write(dev, RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412))
  140. return -EIO;
  141. DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  142. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  143. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  144. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  145. printf("WARNING: year should be between 1970 and 2069!\n");
  146. write_op[RTC_SEC_REG_ADDR] = bin2bcd(tmp->tm_sec);
  147. write_op[RTC_MIN_REG_ADDR] = bin2bcd(tmp->tm_min);
  148. write_op[RTC_HR_REG_ADDR] = bin2bcd(tmp->tm_hour);
  149. write_op[RTC_DAY_REG_ADDR] = bin2bcd(tmp->tm_wday);
  150. write_op[RTC_DATE_REG_ADDR] = bin2bcd(tmp->tm_mday);
  151. write_op[RTC_MON_REG_ADDR] = bin2bcd(tmp->tm_mon);
  152. write_op[RTC_YR_REG_ADDR] = bin2bcd(tmp->tm_year % 100);
  153. write_op[RTC_OFFSET_REG_ADDR] = 0;
  154. return dm_i2c_write(dev, 0, &write_op[0], 8);
  155. }
  156. /*
  157. * Reset the RTC
  158. */
  159. static int rx8025_rtc_reset(struct udevice *dev)
  160. {
  161. uchar buf[16];
  162. uchar ctl2;
  163. if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
  164. printf("Error reading from RTC\n");
  165. return -EIO;
  166. }
  167. ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
  168. ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
  169. if (dev->driver_data == model_rx_8035)
  170. ctl2 &= ~(RTC_CTL2_BIT_XST);
  171. else
  172. ctl2 |= RTC_CTL2_BIT_XST;
  173. return rtc_write(dev, RTC_CTL2_REG_ADDR, ctl2);
  174. }
  175. /*
  176. * Helper functions
  177. */
  178. static int rtc_write(struct udevice *dev, uchar reg, uchar val)
  179. {
  180. /* The RX8025/RX8035 uses the top 4 bits of the
  181. * 'offset' byte as the start register address,
  182. * and the bottom 4 bits as a 'transfer' mode setting
  183. * (only applicable for reads)
  184. */
  185. u8 offset = (reg << 4);
  186. if (dm_i2c_reg_write(dev, offset, val)) {
  187. printf("Error writing to RTC\n");
  188. return -EIO;
  189. }
  190. return 0;
  191. }
  192. static int rx8025_probe(struct udevice *dev)
  193. {
  194. uchar buf[16];
  195. int ret = 0;
  196. if (i2c_get_chip_offset_len(dev) != 1)
  197. ret = i2c_set_chip_offset_len(dev, 1);
  198. if (ret)
  199. return ret;
  200. return dm_i2c_read(dev, 0, buf, sizeof(buf));
  201. }
  202. static const struct rtc_ops rx8025_rtc_ops = {
  203. .get = rx8025_rtc_get,
  204. .set = rx8025_rtc_set,
  205. .reset = rx8025_rtc_reset,
  206. };
  207. static const struct udevice_id rx8025_rtc_ids[] = {
  208. { .compatible = "epson,rx8025", .data = model_rx_8025 },
  209. { .compatible = "epson,rx8035", .data = model_rx_8035 },
  210. { }
  211. };
  212. U_BOOT_DRIVER(rx8025_rtc) = {
  213. .name = "rx8025_rtc",
  214. .id = UCLASS_RTC,
  215. .probe = rx8025_probe,
  216. .of_match = rx8025_rtc_ids,
  217. .ops = &rx8025_rtc_ops,
  218. };