rtc.c 3.2 KB

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