4 * Device driver for Ricoh's Real Time Controller RS5C372A.
6 * Copyright (C) 2004 Gary Jennejohn garyj@denx.de
8 * Based in part in ds1307.c -
9 * (C) Copyright 2001, 2002, 2003
10 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 * Keith Outwater, keith_outwater@mvis.com`
12 * Steven Scholz, steven.scholz@imc-berlin.de
14 * See file CREDITS for list of people who contributed to this
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 #if defined(CONFIG_CMD_DATE)
39 * Reads are always done starting with register 15, which requires some
40 * jumping-through-hoops to access the data correctly.
42 * Writes are always done starting with register 0.
48 static unsigned int rtc_debug = DEBUG;
50 #define rtc_debug 0 /* gcc will remove all the debug code for us */
53 #ifndef CONFIG_SYS_I2C_RTC_ADDR
54 #define CONFIG_SYS_I2C_RTC_ADDR 0x32
57 #define RS5C372_RAM_SIZE 0x10
58 #define RATE_32000HZ 0x80 /* Rate Select 32.000KHz */
59 #define RATE_32768HZ 0x00 /* Rate Select 32.768KHz */
61 #define STATUS_XPT 0x10 /* data invalid because voltage was 0 */
63 #define USE_24HOUR_MODE 0x20
64 #define TWELVE_HOUR_MODE(n) ((((n) >> 5) & 1) == 0)
65 #define HOURS_AP(n) (((n) >> 5) & 1)
66 #define HOURS_12(n) bcd2bin((n) & 0x1F)
67 #define HOURS_24(n) bcd2bin((n) & 0x3F)
70 static int setup_done = 0;
73 rs5c372_readram(unsigned char *buf, int len)
77 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, len);
79 printf("%s: failed to read\n", __FUNCTION__);
83 if (buf[0] & STATUS_XPT)
84 printf("### Warning: RTC lost power\n");
92 unsigned char buf[RS5C372_RAM_SIZE + 1];
95 /* note that this returns reg. 15 in buf[1] */
96 ret = rs5c372_readram(&buf[1], RS5C372_RAM_SIZE);
98 printf("%s: failed\n", __FUNCTION__);
103 /* we want to start writing at register 0 so we have to copy the */
104 /* register contents up one slot */
105 for (ret = 2; ret < 9; ret++)
106 buf[ret - 1] = buf[ret];
107 /* registers 0 to 6 (time values) are not touched */
108 buf[8] = RATE_32768HZ; /* reg. 7 */
109 buf[9] = 0; /* reg. 8 */
110 buf[10] = 0; /* reg. 9 */
111 buf[11] = 0; /* reg. 10 */
112 buf[12] = 0; /* reg. 11 */
113 buf[13] = 0; /* reg. 12 */
114 buf[14] = 0; /* reg. 13 */
115 buf[15] = 0; /* reg. 14 */
116 buf[16] = USE_24HOUR_MODE; /* reg. 15 */
117 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, RS5C372_RAM_SIZE+1);
119 printf("%s: failed\n", __FUNCTION__);
128 rs5c372_convert_to_time(struct rtc_time *dt, unsigned char *buf)
130 /* buf[0] is register 15 */
131 dt->tm_sec = bcd2bin(buf[1]);
132 dt->tm_min = bcd2bin(buf[2]);
134 if (TWELVE_HOUR_MODE(buf[0])) {
135 dt->tm_hour = HOURS_12(buf[3]);
136 if (HOURS_AP(buf[3])) /* PM */
138 } else /* 24-hour-mode */
139 dt->tm_hour = HOURS_24(buf[3]);
141 dt->tm_mday = bcd2bin(buf[5]);
142 dt->tm_mon = bcd2bin(buf[6]);
143 dt->tm_year = bcd2bin(buf[7]);
144 if (dt->tm_year >= 70)
149 dt->tm_wday = bcd2bin(buf[4] & 0x07);
154 printf("rs5c372_convert_to_time: year = %d\n", dt->tm_year);
155 printf("rs5c372_convert_to_time: mon = %d\n", dt->tm_mon);
156 printf("rs5c372_convert_to_time: mday = %d\n", dt->tm_mday);
157 printf("rs5c372_convert_to_time: hour = %d\n", dt->tm_hour);
158 printf("rs5c372_convert_to_time: min = %d\n", dt->tm_min);
159 printf("rs5c372_convert_to_time: sec = %d\n", dt->tm_sec);
164 * Get the current time from the RTC
167 rtc_get (struct rtc_time *tmp)
169 unsigned char buf[RS5C372_RAM_SIZE];
178 memset(buf, 0, sizeof(buf));
180 /* note that this returns reg. 15 in buf[0] */
181 ret = rs5c372_readram(buf, RS5C372_RAM_SIZE);
183 printf("%s: failed\n", __FUNCTION__);
187 rs5c372_convert_to_time(tmp, buf);
195 int rtc_set (struct rtc_time *tmp)
197 unsigned char buf[8], reg15;
207 printf("rtc_set: tm_year = %d\n", tmp->tm_year);
208 printf("rtc_set: tm_mon = %d\n", tmp->tm_mon);
209 printf("rtc_set: tm_mday = %d\n", tmp->tm_mday);
210 printf("rtc_set: tm_hour = %d\n", tmp->tm_hour);
211 printf("rtc_set: tm_min = %d\n", tmp->tm_min);
212 printf("rtc_set: tm_sec = %d\n", tmp->tm_sec);
215 memset(buf, 0, sizeof(buf));
217 /* only read register 15 */
218 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 1);
221 /* need to save register 15 */
223 buf[0] = 0; /* register address on RS5C372 */
224 buf[1] = bin2bcd(tmp->tm_sec);
225 buf[2] = bin2bcd(tmp->tm_min);
226 /* need to handle 12 hour mode */
227 if (TWELVE_HOUR_MODE(reg15)) {
228 if (tmp->tm_hour >= 12) { /* PM */
229 /* 12 PM is a special case */
230 if (tmp->tm_hour == 12)
231 buf[3] = bin2bcd(tmp->tm_hour);
233 buf[3] = bin2bcd(tmp->tm_hour - 12);
237 buf[3] = bin2bcd(tmp->tm_hour);
240 buf[4] = bin2bcd(tmp->tm_wday);
241 buf[5] = bin2bcd(tmp->tm_mday);
242 buf[6] = bin2bcd(tmp->tm_mon);
243 if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
244 printf("WARNING: year should be between 1970 and 2069!\n");
245 buf[7] = bin2bcd(tmp->tm_year % 100);
247 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 8);
249 printf("rs5c372_set_datetime(), i2c_master_send() returned %d\n",ret);
260 * Reset the RTC. We set the date back to 1970-01-01.
275 /* Jan. 1, 1970 was a Thursday */
284 printf ("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
285 tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
286 tmp.tm_hour, tmp.tm_min, tmp.tm_sec);