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
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
28 * DS1307 and DS1338 Real Time Clock (RTC).
38 #if defined(CONFIG_CMD_DATE)
40 /*---------------------------------------------------------------------*/
44 #define DEBUGR(fmt,args...) printf(fmt ,##args)
46 #define DEBUGR(fmt,args...)
48 /*---------------------------------------------------------------------*/
50 #ifndef CONFIG_SYS_I2C_RTC_ADDR
51 # define CONFIG_SYS_I2C_RTC_ADDR 0x68
54 #if defined(CONFIG_RTC_DS1307) && (CONFIG_SYS_I2C_SPEED > 100000)
55 # error The DS1307 is specified only up to 100kHz!
59 * RTC register addresses
61 #define RTC_SEC_REG_ADDR 0x00
62 #define RTC_MIN_REG_ADDR 0x01
63 #define RTC_HR_REG_ADDR 0x02
64 #define RTC_DAY_REG_ADDR 0x03
65 #define RTC_DATE_REG_ADDR 0x04
66 #define RTC_MON_REG_ADDR 0x05
67 #define RTC_YR_REG_ADDR 0x06
68 #define RTC_CTL_REG_ADDR 0x07
70 #define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */
72 #define RTC_CTL_BIT_RS0 0x01 /* Rate select 0 */
73 #define RTC_CTL_BIT_RS1 0x02 /* Rate select 1 */
74 #define RTC_CTL_BIT_SQWE 0x10 /* Square Wave Enable */
75 #define RTC_CTL_BIT_OUT 0x80 /* Output Control */
77 static uchar rtc_read (uchar reg);
78 static void rtc_write (uchar reg, uchar val);
81 * Get the current time from the RTC
83 int rtc_get (struct rtc_time *tmp)
86 uchar sec, min, hour, mday, wday, mon, year;
88 sec = rtc_read (RTC_SEC_REG_ADDR);
89 min = rtc_read (RTC_MIN_REG_ADDR);
90 hour = rtc_read (RTC_HR_REG_ADDR);
91 wday = rtc_read (RTC_DAY_REG_ADDR);
92 mday = rtc_read (RTC_DATE_REG_ADDR);
93 mon = rtc_read (RTC_MON_REG_ADDR);
94 year = rtc_read (RTC_YR_REG_ADDR);
96 DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
97 "hr: %02x min: %02x sec: %02x\n",
98 year, mon, mday, wday, hour, min, sec);
100 if (sec & RTC_SEC_BIT_CH) {
101 printf ("### Warning: RTC oscillator has stopped\n");
102 /* clear the CH flag */
103 rtc_write (RTC_SEC_REG_ADDR,
104 rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH);
108 tmp->tm_sec = bcd2bin (sec & 0x7F);
109 tmp->tm_min = bcd2bin (min & 0x7F);
110 tmp->tm_hour = bcd2bin (hour & 0x3F);
111 tmp->tm_mday = bcd2bin (mday & 0x3F);
112 tmp->tm_mon = bcd2bin (mon & 0x1F);
113 tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
114 tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
118 DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
119 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
120 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
129 int rtc_set (struct rtc_time *tmp)
131 DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
132 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
133 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
135 if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
136 printf("WARNING: year should be between 1970 and 2069!\n");
138 rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
139 rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
140 rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
141 rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
142 rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
143 rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
144 rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
151 * Reset the RTC. We setting the date back to 1970-01-01.
152 * We also enable the oscillator output on the SQW/OUT pin and program
153 * it for 32,768 Hz output. Note that according to the datasheet, turning
154 * on the square wave output increases the current drain on the backup
155 * battery to something between 480nA and 800nA.
157 void rtc_reset (void)
161 rtc_write (RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */
162 rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0);
173 printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
174 tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
175 tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
186 uchar rtc_read (uchar reg)
188 return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
192 static void rtc_write (uchar reg, uchar val)
194 i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);