ds1307.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. static uchar rtc_read (uchar reg);
  51. static void rtc_write (uchar reg, uchar val);
  52. /*
  53. * Get the current time from the RTC
  54. */
  55. int rtc_get (struct rtc_time *tmp)
  56. {
  57. int rel = 0;
  58. uchar sec, min, hour, mday, wday, mon, year;
  59. sec = rtc_read (RTC_SEC_REG_ADDR);
  60. min = rtc_read (RTC_MIN_REG_ADDR);
  61. hour = rtc_read (RTC_HR_REG_ADDR);
  62. wday = rtc_read (RTC_DAY_REG_ADDR);
  63. mday = rtc_read (RTC_DATE_REG_ADDR);
  64. mon = rtc_read (RTC_MON_REG_ADDR);
  65. year = rtc_read (RTC_YR_REG_ADDR);
  66. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  67. "hr: %02x min: %02x sec: %02x\n",
  68. year, mon, mday, wday, hour, min, sec);
  69. if (sec & RTC_SEC_BIT_CH) {
  70. printf ("### Warning: RTC oscillator has stopped\n");
  71. /* clear the CH flag */
  72. rtc_write (RTC_SEC_REG_ADDR,
  73. rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH);
  74. rel = -1;
  75. }
  76. tmp->tm_sec = bcd2bin (sec & 0x7F);
  77. tmp->tm_min = bcd2bin (min & 0x7F);
  78. tmp->tm_hour = bcd2bin (hour & 0x3F);
  79. tmp->tm_mday = bcd2bin (mday & 0x3F);
  80. tmp->tm_mon = bcd2bin (mon & 0x1F);
  81. tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
  82. tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
  83. tmp->tm_yday = 0;
  84. tmp->tm_isdst= 0;
  85. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  86. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  87. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  88. return rel;
  89. }
  90. /*
  91. * Set the RTC
  92. */
  93. int rtc_set (struct rtc_time *tmp)
  94. {
  95. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  96. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  97. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  98. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  99. printf("WARNING: year should be between 1970 and 2069!\n");
  100. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  101. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  102. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
  103. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  104. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  105. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  106. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  107. return 0;
  108. }
  109. /*
  110. * Reset the RTC. We setting the date back to 1970-01-01.
  111. * We also enable the oscillator output on the SQW/OUT pin and program
  112. * it for 32,768 Hz output. Note that according to the datasheet, turning
  113. * on the square wave output increases the current drain on the backup
  114. * battery to something between 480nA and 800nA.
  115. */
  116. void rtc_reset (void)
  117. {
  118. struct rtc_time tmp;
  119. rtc_write (RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */
  120. rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0);
  121. tmp.tm_year = 1970;
  122. tmp.tm_mon = 1;
  123. tmp.tm_mday= 1;
  124. tmp.tm_hour = 0;
  125. tmp.tm_min = 0;
  126. tmp.tm_sec = 0;
  127. rtc_set(&tmp);
  128. printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  129. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  130. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  131. return;
  132. }
  133. /*
  134. * Helper functions
  135. */
  136. static
  137. uchar rtc_read (uchar reg)
  138. {
  139. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  140. }
  141. static void rtc_write (uchar reg, uchar val)
  142. {
  143. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  144. }
  145. #endif