ds1307.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. /*
  26. * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
  27. * DS1307 and DS1338 Real Time Clock (RTC).
  28. *
  29. * based on ds1337.c
  30. */
  31. #include <common.h>
  32. #include <command.h>
  33. #include <rtc.h>
  34. #include <i2c.h>
  35. #if (defined(CONFIG_RTC_DS1307) || defined(CONFIG_RTC_DS1338) ) && \
  36. defined(CONFIG_CMD_DATE)
  37. /*---------------------------------------------------------------------*/
  38. #undef DEBUG_RTC
  39. #ifdef DEBUG_RTC
  40. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  41. #else
  42. #define DEBUGR(fmt,args...)
  43. #endif
  44. /*---------------------------------------------------------------------*/
  45. #ifndef CFG_I2C_RTC_ADDR
  46. # define CFG_I2C_RTC_ADDR 0x68
  47. #endif
  48. #if defined(CONFIG_RTC_DS1307) && (CFG_I2C_SPEED > 100000)
  49. # error The DS1307 is specified only up to 100kHz!
  50. #endif
  51. /*
  52. * RTC register addresses
  53. */
  54. #define RTC_SEC_REG_ADDR 0x00
  55. #define RTC_MIN_REG_ADDR 0x01
  56. #define RTC_HR_REG_ADDR 0x02
  57. #define RTC_DAY_REG_ADDR 0x03
  58. #define RTC_DATE_REG_ADDR 0x04
  59. #define RTC_MON_REG_ADDR 0x05
  60. #define RTC_YR_REG_ADDR 0x06
  61. #define RTC_CTL_REG_ADDR 0x07
  62. #define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */
  63. #define RTC_CTL_BIT_RS0 0x01 /* Rate select 0 */
  64. #define RTC_CTL_BIT_RS1 0x02 /* Rate select 1 */
  65. #define RTC_CTL_BIT_SQWE 0x10 /* Square Wave Enable */
  66. #define RTC_CTL_BIT_OUT 0x80 /* Output Control */
  67. static uchar rtc_read (uchar reg);
  68. static void rtc_write (uchar reg, uchar val);
  69. static uchar bin2bcd (unsigned int n);
  70. static unsigned bcd2bin (uchar c);
  71. /*
  72. * Get the current time from the RTC
  73. */
  74. void rtc_get (struct rtc_time *tmp)
  75. {
  76. uchar sec, min, hour, mday, wday, mon, year;
  77. sec = rtc_read (RTC_SEC_REG_ADDR);
  78. min = rtc_read (RTC_MIN_REG_ADDR);
  79. hour = rtc_read (RTC_HR_REG_ADDR);
  80. wday = rtc_read (RTC_DAY_REG_ADDR);
  81. mday = rtc_read (RTC_DATE_REG_ADDR);
  82. mon = rtc_read (RTC_MON_REG_ADDR);
  83. year = rtc_read (RTC_YR_REG_ADDR);
  84. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  85. "hr: %02x min: %02x sec: %02x\n",
  86. year, mon, mday, wday, hour, min, sec);
  87. if (sec & RTC_SEC_BIT_CH) {
  88. printf ("### Warning: RTC oscillator has stopped\n");
  89. /* clear the CH flag */
  90. rtc_write (RTC_SEC_REG_ADDR,
  91. rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH);
  92. }
  93. tmp->tm_sec = bcd2bin (sec & 0x7F);
  94. tmp->tm_min = bcd2bin (min & 0x7F);
  95. tmp->tm_hour = bcd2bin (hour & 0x3F);
  96. tmp->tm_mday = bcd2bin (mday & 0x3F);
  97. tmp->tm_mon = bcd2bin (mon & 0x1F);
  98. tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
  99. tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
  100. tmp->tm_yday = 0;
  101. tmp->tm_isdst= 0;
  102. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  103. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  104. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  105. }
  106. /*
  107. * Set the RTC
  108. */
  109. void rtc_set (struct rtc_time *tmp)
  110. {
  111. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  112. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  113. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  114. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  115. printf("WARNING: year should be between 1970 and 2069!\n");
  116. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  117. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  118. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
  119. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  120. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  121. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  122. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  123. }
  124. /*
  125. * Reset the RTC. We setting the date back to 1970-01-01.
  126. * We also enable the oscillator output on the SQW/OUT pin and program
  127. * it for 32,768 Hz output. Note that according to the datasheet, turning
  128. * on the square wave output increases the current drain on the backup
  129. * battery to something between 480nA and 800nA.
  130. */
  131. void rtc_reset (void)
  132. {
  133. struct rtc_time tmp;
  134. rtc_write (RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */
  135. rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0);
  136. tmp.tm_year = 1970;
  137. tmp.tm_mon = 1;
  138. tmp.tm_mday= 1;
  139. tmp.tm_hour = 0;
  140. tmp.tm_min = 0;
  141. tmp.tm_sec = 0;
  142. rtc_set(&tmp);
  143. printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  144. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  145. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  146. return;
  147. }
  148. /*
  149. * Helper functions
  150. */
  151. static
  152. uchar rtc_read (uchar reg)
  153. {
  154. return (i2c_reg_read (CFG_I2C_RTC_ADDR, reg));
  155. }
  156. static void rtc_write (uchar reg, uchar val)
  157. {
  158. i2c_reg_write (CFG_I2C_RTC_ADDR, reg, val);
  159. }
  160. static unsigned bcd2bin (uchar n)
  161. {
  162. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  163. }
  164. static unsigned char bin2bcd (unsigned int n)
  165. {
  166. return (((n / 10) << 4) | (n % 10));
  167. }
  168. #endif