rx8025.c 4.6 KB

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