ds3231.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2006
  4. * Markus Klotzbuecher, mk@denx.de
  5. */
  6. /*
  7. * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
  8. * Extremly Accurate DS3231 Real Time Clock (RTC).
  9. *
  10. * copied from ds1337.c
  11. */
  12. #include <common.h>
  13. #include <command.h>
  14. #include <rtc.h>
  15. #include <i2c.h>
  16. #if defined(CONFIG_CMD_DATE)
  17. /*
  18. * RTC register addresses
  19. */
  20. #define RTC_SEC_REG_ADDR 0x0
  21. #define RTC_MIN_REG_ADDR 0x1
  22. #define RTC_HR_REG_ADDR 0x2
  23. #define RTC_DAY_REG_ADDR 0x3
  24. #define RTC_DATE_REG_ADDR 0x4
  25. #define RTC_MON_REG_ADDR 0x5
  26. #define RTC_YR_REG_ADDR 0x6
  27. #define RTC_CTL_REG_ADDR 0x0e
  28. #define RTC_STAT_REG_ADDR 0x0f
  29. /*
  30. * RTC control register bits
  31. */
  32. #define RTC_CTL_BIT_A1IE 0x1 /* Alarm 1 interrupt enable */
  33. #define RTC_CTL_BIT_A2IE 0x2 /* Alarm 2 interrupt enable */
  34. #define RTC_CTL_BIT_INTCN 0x4 /* Interrupt control */
  35. #define RTC_CTL_BIT_RS1 0x8 /* Rate select 1 */
  36. #define RTC_CTL_BIT_RS2 0x10 /* Rate select 2 */
  37. #define RTC_CTL_BIT_DOSC 0x80 /* Disable Oscillator */
  38. /*
  39. * RTC status register bits
  40. */
  41. #define RTC_STAT_BIT_A1F 0x1 /* Alarm 1 flag */
  42. #define RTC_STAT_BIT_A2F 0x2 /* Alarm 2 flag */
  43. #define RTC_STAT_BIT_OSF 0x80 /* Oscillator stop flag */
  44. #define RTC_STAT_BIT_BB32KHZ 0x40 /* Battery backed 32KHz Output */
  45. #define RTC_STAT_BIT_EN32KHZ 0x8 /* Enable 32KHz Output */
  46. static uchar rtc_read (uchar reg);
  47. static void rtc_write (uchar reg, uchar val);
  48. /*
  49. * Get the current time from the RTC
  50. */
  51. int rtc_get (struct rtc_time *tmp)
  52. {
  53. int rel = 0;
  54. uchar sec, min, hour, mday, wday, mon_cent, year, control, status;
  55. control = rtc_read (RTC_CTL_REG_ADDR);
  56. status = rtc_read (RTC_STAT_REG_ADDR);
  57. sec = rtc_read (RTC_SEC_REG_ADDR);
  58. min = rtc_read (RTC_MIN_REG_ADDR);
  59. hour = rtc_read (RTC_HR_REG_ADDR);
  60. wday = rtc_read (RTC_DAY_REG_ADDR);
  61. mday = rtc_read (RTC_DATE_REG_ADDR);
  62. mon_cent = rtc_read (RTC_MON_REG_ADDR);
  63. year = rtc_read (RTC_YR_REG_ADDR);
  64. debug("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  65. "hr: %02x min: %02x sec: %02x control: %02x status: %02x\n",
  66. year, mon_cent, mday, wday, hour, min, sec, control, status);
  67. if (status & RTC_STAT_BIT_OSF) {
  68. printf ("### Warning: RTC oscillator has stopped\n");
  69. /* clear the OSF flag */
  70. rtc_write (RTC_STAT_REG_ADDR,
  71. rtc_read (RTC_STAT_REG_ADDR) & ~RTC_STAT_BIT_OSF);
  72. rel = -1;
  73. }
  74. tmp->tm_sec = bcd2bin (sec & 0x7F);
  75. tmp->tm_min = bcd2bin (min & 0x7F);
  76. tmp->tm_hour = bcd2bin (hour & 0x3F);
  77. tmp->tm_mday = bcd2bin (mday & 0x3F);
  78. tmp->tm_mon = bcd2bin (mon_cent & 0x1F);
  79. tmp->tm_year = bcd2bin (year) + ((mon_cent & 0x80) ? 2000 : 1900);
  80. tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
  81. tmp->tm_yday = 0;
  82. tmp->tm_isdst= 0;
  83. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  84. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  85. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  86. return rel;
  87. }
  88. /*
  89. * Set the RTC
  90. */
  91. int rtc_set (struct rtc_time *tmp)
  92. {
  93. uchar century;
  94. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  95. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  96. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  97. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  98. century = (tmp->tm_year >= 2000) ? 0x80 : 0;
  99. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon) | century);
  100. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
  101. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  102. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  103. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  104. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  105. return 0;
  106. }
  107. /*
  108. * Reset the RTC. We also enable the oscillator output on the
  109. * SQW/INTB* pin and program it for 32,768 Hz output. Note that
  110. * according to the datasheet, turning on the square wave output
  111. * increases the current drain on the backup battery from about
  112. * 600 nA to 2uA.
  113. */
  114. void rtc_reset (void)
  115. {
  116. rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS2);
  117. }
  118. /*
  119. * Enable 32KHz output
  120. */
  121. void rtc_enable_32khz_output(void)
  122. {
  123. rtc_write(RTC_STAT_REG_ADDR,
  124. RTC_STAT_BIT_BB32KHZ | RTC_STAT_BIT_EN32KHZ);
  125. }
  126. /*
  127. * Helper functions
  128. */
  129. static
  130. uchar rtc_read (uchar reg)
  131. {
  132. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  133. }
  134. static void rtc_write (uchar reg, uchar val)
  135. {
  136. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  137. }
  138. #endif