ds1307.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * (C) Copyright 2001, 2002, 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. * Keith Outwater, keith_outwater@mvis.com`
  5. * Steven Scholz, steven.scholz@imc-berlin.de
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. /*
  10. * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
  11. * DS1307 and DS1338/9 Real Time Clock (RTC).
  12. *
  13. * based on ds1337.c
  14. */
  15. #include <common.h>
  16. #include <command.h>
  17. #include <rtc.h>
  18. #include <i2c.h>
  19. #if defined(CONFIG_CMD_DATE)
  20. /*---------------------------------------------------------------------*/
  21. #undef DEBUG_RTC
  22. #ifdef DEBUG_RTC
  23. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  24. #else
  25. #define DEBUGR(fmt,args...)
  26. #endif
  27. /*---------------------------------------------------------------------*/
  28. #ifndef CONFIG_SYS_I2C_RTC_ADDR
  29. # define CONFIG_SYS_I2C_RTC_ADDR 0x68
  30. #endif
  31. #if defined(CONFIG_RTC_DS1307) && (CONFIG_SYS_I2C_SPEED > 100000)
  32. # error The DS1307 is specified only up to 100kHz!
  33. #endif
  34. /*
  35. * RTC register addresses
  36. */
  37. #define RTC_SEC_REG_ADDR 0x00
  38. #define RTC_MIN_REG_ADDR 0x01
  39. #define RTC_HR_REG_ADDR 0x02
  40. #define RTC_DAY_REG_ADDR 0x03
  41. #define RTC_DATE_REG_ADDR 0x04
  42. #define RTC_MON_REG_ADDR 0x05
  43. #define RTC_YR_REG_ADDR 0x06
  44. #define RTC_CTL_REG_ADDR 0x07
  45. #define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */
  46. #define RTC_CTL_BIT_RS0 0x01 /* Rate select 0 */
  47. #define RTC_CTL_BIT_RS1 0x02 /* Rate select 1 */
  48. #define RTC_CTL_BIT_SQWE 0x10 /* Square Wave Enable */
  49. #define RTC_CTL_BIT_OUT 0x80 /* Output Control */
  50. /* MCP7941X-specific bits */
  51. #define MCP7941X_BIT_ST 0x80
  52. #define MCP7941X_BIT_VBATEN 0x08
  53. static uchar rtc_read (uchar reg);
  54. static void rtc_write (uchar reg, uchar val);
  55. /*
  56. * Get the current time from the RTC
  57. */
  58. int rtc_get (struct rtc_time *tmp)
  59. {
  60. int rel = 0;
  61. uchar sec, min, hour, mday, wday, mon, year;
  62. #ifdef CONFIG_RTC_MCP79411
  63. read_rtc:
  64. #endif
  65. sec = rtc_read (RTC_SEC_REG_ADDR);
  66. min = rtc_read (RTC_MIN_REG_ADDR);
  67. hour = rtc_read (RTC_HR_REG_ADDR);
  68. wday = rtc_read (RTC_DAY_REG_ADDR);
  69. mday = rtc_read (RTC_DATE_REG_ADDR);
  70. mon = rtc_read (RTC_MON_REG_ADDR);
  71. year = rtc_read (RTC_YR_REG_ADDR);
  72. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  73. "hr: %02x min: %02x sec: %02x\n",
  74. year, mon, mday, wday, hour, min, sec);
  75. #ifdef CONFIG_RTC_DS1307
  76. if (sec & RTC_SEC_BIT_CH) {
  77. printf ("### Warning: RTC oscillator has stopped\n");
  78. /* clear the CH flag */
  79. rtc_write (RTC_SEC_REG_ADDR,
  80. rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH);
  81. rel = -1;
  82. }
  83. #endif
  84. #ifdef CONFIG_RTC_MCP79411
  85. /* make sure that the backup battery is enabled */
  86. if (!(wday & MCP7941X_BIT_VBATEN)) {
  87. rtc_write(RTC_DAY_REG_ADDR,
  88. wday | MCP7941X_BIT_VBATEN);
  89. }
  90. /* clock halted? turn it on, so clock can tick. */
  91. if (!(sec & MCP7941X_BIT_ST)) {
  92. rtc_write(RTC_SEC_REG_ADDR, MCP7941X_BIT_ST);
  93. printf("Started RTC\n");
  94. goto read_rtc;
  95. }
  96. #endif
  97. tmp->tm_sec = bcd2bin (sec & 0x7F);
  98. tmp->tm_min = bcd2bin (min & 0x7F);
  99. tmp->tm_hour = bcd2bin (hour & 0x3F);
  100. tmp->tm_mday = bcd2bin (mday & 0x3F);
  101. tmp->tm_mon = bcd2bin (mon & 0x1F);
  102. tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
  103. tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
  104. tmp->tm_yday = 0;
  105. tmp->tm_isdst= 0;
  106. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  107. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  108. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  109. return rel;
  110. }
  111. /*
  112. * Set the RTC
  113. */
  114. int rtc_set (struct rtc_time *tmp)
  115. {
  116. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  117. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  118. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  119. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  120. printf("WARNING: year should be between 1970 and 2069!\n");
  121. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  122. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  123. #ifdef CONFIG_RTC_MCP79411
  124. rtc_write (RTC_DAY_REG_ADDR,
  125. bin2bcd (tmp->tm_wday + 1) | MCP7941X_BIT_VBATEN);
  126. #else
  127. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
  128. #endif
  129. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  130. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  131. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  132. #ifdef CONFIG_RTC_MCP79411
  133. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec) | MCP7941X_BIT_ST);
  134. #else
  135. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  136. #endif
  137. return 0;
  138. }
  139. /*
  140. * Reset the RTC. We setting the date back to 1970-01-01.
  141. * We also enable the oscillator output on the SQW/OUT pin and program
  142. * it for 32,768 Hz output. Note that according to the datasheet, turning
  143. * on the square wave output increases the current drain on the backup
  144. * battery to something between 480nA and 800nA.
  145. */
  146. void rtc_reset (void)
  147. {
  148. struct rtc_time tmp;
  149. rtc_write (RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */
  150. rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0);
  151. tmp.tm_year = 1970;
  152. tmp.tm_mon = 1;
  153. tmp.tm_mday= 1;
  154. tmp.tm_hour = 0;
  155. tmp.tm_min = 0;
  156. tmp.tm_sec = 0;
  157. rtc_set(&tmp);
  158. printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  159. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  160. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  161. return;
  162. }
  163. /*
  164. * Helper functions
  165. */
  166. static
  167. uchar rtc_read (uchar reg)
  168. {
  169. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  170. }
  171. static void rtc_write (uchar reg, uchar val)
  172. {
  173. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  174. }
  175. #endif