rx8025.c 4.7 KB

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