rtc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Read/write DS3231M RTC to/from the sysclock.
  3. *
  4. * This currently doesn't try to maintain any kind of subsecond
  5. * accuracy.
  6. */
  7. #include <string.h>
  8. #include "fw.h"
  9. #include "console.h"
  10. #include "io.h"
  11. #include "systime.h"
  12. static inline uint32_t i2c_wait(void)
  13. {
  14. waitfor(I2C_IRQ);
  15. }
  16. static void i2c_send(uint8_t byte, uint8_t ctl)
  17. {
  18. i2c_wait();
  19. I2C_WDATA = (byte << 8) | I2C_NAK | ctl;
  20. }
  21. static bool i2c_acked(void)
  22. {
  23. i2c_wait();
  24. return !(I2C_RDATA & I2C_NAK);
  25. }
  26. static uint8_t i2c_recv(uint8_t ctl)
  27. {
  28. uint32_t rdata;
  29. i2c_wait();
  30. I2C_WDATA = (~0xff) | ctl;
  31. i2c_wait();
  32. return I2C_RDATA_DATA;
  33. }
  34. #define RTC_REGS 19 /* Total RTC registers */
  35. #define RTC_TIME_REGS 7 /* RTC registers with time info */
  36. #define RTC_ADDR 0x68
  37. #define RTC_WCMD ((RTC_ADDR << 1)+0)
  38. #define RTC_RCMD ((RTC_ADDR << 1)+1)
  39. static unsigned int unbcd(uint8_t v)
  40. {
  41. return (v & 0x0f) + (v >> 4) * 10;
  42. }
  43. static uint8_t tobcd(unsigned int v)
  44. {
  45. uint8_t q;
  46. v %= 100;
  47. return ((v/10) << 4) + (v%10);
  48. }
  49. static int i2c_start_rtc(void)
  50. {
  51. int sda_retry_count = 16;
  52. i2c_set_speed(400);
  53. /* SDA held low? */
  54. while (!(I2C_RDATA & I2C_SDA)) {
  55. i2c_send(0xff, I2C_DUMMY);
  56. if (!sda_retry_count--) {
  57. con_printf("RTC: I2C SDA stuck low\n");
  58. return -1;
  59. }
  60. }
  61. i2c_send(RTC_WCMD, 0);
  62. if (!i2c_acked()) {
  63. con_printf("No RTC detected at I2C address 0x%02x\n", RTC_ADDR);
  64. i2c_send(0xff, I2C_P);
  65. return -1;
  66. }
  67. return 0;
  68. }
  69. void read_rtc(void)
  70. {
  71. uint8_t rtc_regs[RTC_TIME_REGS];
  72. int i;
  73. struct tms tms;
  74. if (i2c_start_rtc())
  75. return;
  76. i2c_send(0, I2C_SR); /* Starting register */
  77. i2c_send(RTC_RCMD, 0);
  78. for (i = 0; i < RTC_TIME_REGS-1; i++)
  79. rtc_regs[i] = i2c_recv(0);
  80. rtc_regs[i] = i2c_recv(I2C_NAK | I2C_P);
  81. /* Convert to struct tms and set systime */
  82. memset(&tms, 0, sizeof tms);
  83. unsigned int sec = unbcd(rtc_regs[0]);
  84. tms.tm_2sec = sec >> 1;
  85. tms.hold.tm_1sec = sec & 1;
  86. tms.hold.tm_tick = 0x4000; /* Without more info, assume mid-second */
  87. tms.tm_min = unbcd(rtc_regs[1]);
  88. unsigned int hour;
  89. if (rtc_regs[2] & 0x40) {
  90. /* AM/PM mode - this shouldn't happen */
  91. hour = unbcd(rtc_regs[2] & 0x1f);
  92. if (hour > 11)
  93. hour -= 12;
  94. if (rtc_regs[2] & 0x20)
  95. hour += 12;
  96. } else {
  97. /* 24-hour mode */
  98. hour = unbcd(rtc_regs[2]);
  99. }
  100. tms.tm_hour = hour;
  101. tms.tm_mday = unbcd(rtc_regs[4]);
  102. tms.tm_mon = unbcd(rtc_regs[5] & 0x1f);
  103. tms.tm_year = unbcd(rtc_regs[6]) + (rtc_regs[5] & 0x80 ? 100 : 0) + 20;
  104. set_systime(tms);
  105. #ifdef TEST
  106. con_printf("RTC register content:\n");
  107. for (i = 0; i < RTC_REGS; i++)
  108. con_printf(" %02x", rtc_regs[i]);
  109. #endif
  110. con_printf("RTC time: %04u-%02u-%02u %02u:%02u:%02u\n",
  111. tms.tm_year + 1980, tms.tm_mon, tms.tm_mday,
  112. tms.tm_hour, tms.tm_min, tms_sec(tms));
  113. }
  114. bool do_write_rtc;
  115. void write_rtc(void)
  116. {
  117. uint8_t rtc_regs[RTC_TIME_REGS];
  118. int i;
  119. struct tms tms;
  120. int sda_retry_count = 16;
  121. do_write_rtc = false;
  122. tms = get_systime();
  123. rtc_regs[0] = tobcd(tms_sec(tms));
  124. rtc_regs[1] = tobcd(tms.tm_min);
  125. rtc_regs[2] = tobcd(tms.tm_hour);
  126. rtc_regs[3] = 0; /* Day of week, unused */
  127. rtc_regs[4] = tobcd(tms.tm_mday);
  128. rtc_regs[5] = tobcd(tms.tm_mon);
  129. rtc_regs[6] = tobcd(tms.tm_year - 20);
  130. if (i2c_start_rtc())
  131. return;
  132. i2c_send(0, 0); /* Starting register */
  133. for (i = 0; i < RTC_TIME_REGS-1; i++)
  134. i2c_send(rtc_regs[i], 0);
  135. i2c_send(rtc_regs[i], I2C_P);
  136. }