rx8025.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * (C) Copyright 2007
  3. * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Epson RX8025 RTC driver.
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <rtc.h>
  13. #include <i2c.h>
  14. #if defined(CONFIG_CMD_DATE)
  15. /*---------------------------------------------------------------------*/
  16. #undef DEBUG_RTC
  17. #ifdef DEBUG_RTC
  18. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  19. #else
  20. #define DEBUGR(fmt,args...)
  21. #endif
  22. /*---------------------------------------------------------------------*/
  23. #ifndef CONFIG_SYS_I2C_RTC_ADDR
  24. # define CONFIG_SYS_I2C_RTC_ADDR 0x32
  25. #endif
  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_CTL1_REG_ADDR 0x0e
  37. #define RTC_CTL2_REG_ADDR 0x0f
  38. /*
  39. * Control register 1 bits
  40. */
  41. #define RTC_CTL1_BIT_2412 0x20
  42. /*
  43. * Control register 2 bits
  44. */
  45. #define RTC_CTL2_BIT_PON 0x10
  46. #define RTC_CTL2_BIT_VDET 0x40
  47. #define RTC_CTL2_BIT_XST 0x20
  48. #define RTC_CTL2_BIT_VDSL 0x80
  49. /*
  50. * Note: the RX8025 I2C RTC requires register
  51. * reads and write to consist of a single bus
  52. * cycle. It is not allowed to write the register
  53. * address in a first cycle that is terminated by
  54. * a STOP condition. The chips needs a 'restart'
  55. * sequence (start sequence without a prior stop).
  56. * This driver has been written for a 4xx board.
  57. * U-Boot's 4xx i2c driver is currently not capable
  58. * to generate such cycles to some work arounds
  59. * are used.
  60. */
  61. /* static uchar rtc_read (uchar reg); */
  62. #define rtc_read(reg) buf[((reg) + 1) & 0xf]
  63. static void rtc_write (uchar reg, uchar val);
  64. /*
  65. * Get the current time from the RTC
  66. */
  67. int rtc_get (struct rtc_time *tmp)
  68. {
  69. int rel = 0;
  70. uchar sec, min, hour, mday, wday, mon, year, ctl2;
  71. uchar buf[16];
  72. if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 16))
  73. printf("Error reading from RTC\n");
  74. sec = rtc_read(RTC_SEC_REG_ADDR);
  75. min = rtc_read(RTC_MIN_REG_ADDR);
  76. hour = rtc_read(RTC_HR_REG_ADDR);
  77. wday = rtc_read(RTC_DAY_REG_ADDR);
  78. mday = rtc_read(RTC_DATE_REG_ADDR);
  79. mon = rtc_read(RTC_MON_REG_ADDR);
  80. year = rtc_read(RTC_YR_REG_ADDR);
  81. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  82. "hr: %02x min: %02x sec: %02x\n",
  83. year, mon, mday, wday, hour, min, sec);
  84. /* dump status */
  85. ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
  86. if (ctl2 & RTC_CTL2_BIT_PON) {
  87. printf("RTC: power-on detected\n");
  88. rel = -1;
  89. }
  90. if (ctl2 & RTC_CTL2_BIT_VDET) {
  91. printf("RTC: voltage drop detected\n");
  92. rel = -1;
  93. }
  94. if (!(ctl2 & RTC_CTL2_BIT_XST)) {
  95. printf("RTC: oscillator stop detected\n");
  96. rel = -1;
  97. }
  98. tmp->tm_sec = bcd2bin (sec & 0x7F);
  99. tmp->tm_min = bcd2bin (min & 0x7F);
  100. if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412)
  101. tmp->tm_hour = bcd2bin (hour & 0x3F);
  102. else
  103. tmp->tm_hour = bcd2bin (hour & 0x1F) % 12 +
  104. ((hour & 0x20) ? 12 : 0);
  105. tmp->tm_mday = bcd2bin (mday & 0x3F);
  106. tmp->tm_mon = bcd2bin (mon & 0x1F);
  107. tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
  108. tmp->tm_wday = bcd2bin (wday & 0x07);
  109. tmp->tm_yday = 0;
  110. tmp->tm_isdst= 0;
  111. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  112. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  113. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  114. return rel;
  115. }
  116. /*
  117. * Set the RTC
  118. */
  119. int rtc_set (struct rtc_time *tmp)
  120. {
  121. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  122. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  123. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  124. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  125. printf("WARNING: year should be between 1970 and 2069!\n");
  126. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  127. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  128. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday));
  129. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  130. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  131. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  132. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  133. rtc_write (RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412);
  134. return 0;
  135. }
  136. /*
  137. * Reset the RTC. We setting the date back to 1970-01-01.
  138. */
  139. void rtc_reset (void)
  140. {
  141. struct rtc_time tmp;
  142. uchar buf[16];
  143. uchar ctl2;
  144. if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 16))
  145. printf("Error reading from RTC\n");
  146. ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
  147. ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
  148. ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL;
  149. rtc_write (RTC_CTL2_REG_ADDR, ctl2);
  150. tmp.tm_year = 1970;
  151. tmp.tm_mon = 1;
  152. tmp.tm_mday= 1;
  153. tmp.tm_hour = 0;
  154. tmp.tm_min = 0;
  155. tmp.tm_sec = 0;
  156. rtc_set(&tmp);
  157. printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  158. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  159. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  160. return;
  161. }
  162. /*
  163. * Helper functions
  164. */
  165. static void rtc_write (uchar reg, uchar val)
  166. {
  167. uchar buf[2];
  168. buf[0] = reg << 4;
  169. buf[1] = val;
  170. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 2) != 0)
  171. printf("Error writing to RTC\n");
  172. }
  173. #endif /* CONFIG_RTC_RX8025 && CONFIG_CMD_DATE */