3 * Heiko Schocher, DENX Software Engineering, hs@denx.de
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #define RTC_RV3029_CTRL_RESET 0x04
29 #define RTC_RV3029_CTRL_SYS_R (1 << 4)
31 #define RTC_RV3029_CLOCK_PAGE 0x08
32 #define RTC_RV3029_PAGE_LEN 7
34 #define RV3029C2_W_SECONDS 0x00
35 #define RV3029C2_W_MINUTES 0x01
36 #define RV3029C2_W_HOURS 0x02
37 #define RV3029C2_W_DATE 0x03
38 #define RV3029C2_W_DAYS 0x04
39 #define RV3029C2_W_MONTHS 0x05
40 #define RV3029C2_W_YEARS 0x06
42 #define RV3029C2_REG_HR_12_24 (1 << 6) /* 24h/12h mode */
43 #define RV3029C2_REG_HR_PM (1 << 5) /* PM/AM bit in 12h mode */
45 int rtc_get( struct rtc_time *tmp )
48 unsigned char buf[RTC_RV3029_PAGE_LEN];
50 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \
53 printf("%s: error reading RTC: %x\n", __func__, ret);
56 tmp->tm_sec = bcd2bin( buf[RV3029C2_W_SECONDS] & 0x7f);
57 tmp->tm_min = bcd2bin( buf[RV3029C2_W_MINUTES] & 0x7f);
58 if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_12_24) {
60 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f);
61 if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM)
65 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f);
67 tmp->tm_mday = bcd2bin( buf[RV3029C2_W_DATE] & 0x3F );
68 tmp->tm_mon = bcd2bin( buf[RV3029C2_W_MONTHS] & 0x1F );
69 tmp->tm_wday = bcd2bin( buf[RV3029C2_W_DAYS] & 0x07 );
70 /* RTC supports only years > 1999 */
71 tmp->tm_year = bcd2bin( buf[RV3029C2_W_YEARS]) + 2000;
76 printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
77 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
78 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
84 int rtc_set( struct rtc_time *tmp )
87 unsigned char buf[RTC_RV3029_PAGE_LEN];
89 printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
90 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
91 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
94 if (tmp->tm_year < 2000) {
95 printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
98 buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec);
99 buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min);
100 buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour);
102 buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24;
103 buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday);
104 buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday);
105 buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon);
106 tmp->tm_year -= 2000;
107 buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year);
108 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1,
109 buf, RTC_RV3029_PAGE_LEN);
111 /* give the RTC some time to update */
116 void rtc_reset (void)
119 unsigned char buf[RTC_RV3029_PAGE_LEN];
121 buf[0] = RTC_RV3029_CTRL_SYS_R;
122 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,