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_CTRL1 0x00
29 #define RTC_RV3029_CTRL1_EERE (1 << 3)
31 #define RTC_RV3029_CTRL_STATUS 0x03
32 #define RTC_RV3029_CTRLS_EEBUSY (1 << 7)
34 #define RTC_RV3029_CTRL_RESET 0x04
35 #define RTC_RV3029_CTRL_SYS_R (1 << 4)
37 #define RTC_RV3029_CLOCK_PAGE 0x08
38 #define RTC_RV3029_PAGE_LEN 7
40 #define RV3029C2_W_SECONDS 0x00
41 #define RV3029C2_W_MINUTES 0x01
42 #define RV3029C2_W_HOURS 0x02
43 #define RV3029C2_W_DATE 0x03
44 #define RV3029C2_W_DAYS 0x04
45 #define RV3029C2_W_MONTHS 0x05
46 #define RV3029C2_W_YEARS 0x06
48 #define RV3029C2_REG_HR_12_24 (1 << 6) /* 24h/12h mode */
49 #define RV3029C2_REG_HR_PM (1 << 5) /* PM/AM bit in 12h mode */
51 #define RTC_RV3029_EEPROM_CTRL 0x30
52 #define RTC_RV3029_TRICKLE_1K (1 << 4)
53 #define RTC_RV3029_TRICKLE_5K (1 << 5)
54 #define RTC_RV3029_TRICKLE_20K (1 << 6)
55 #define RTC_RV3029_TRICKLE_80K (1 << 7)
57 int rtc_get( struct rtc_time *tmp )
60 unsigned char buf[RTC_RV3029_PAGE_LEN];
62 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \
65 printf("%s: error reading RTC: %x\n", __func__, ret);
68 tmp->tm_sec = bcd2bin( buf[RV3029C2_W_SECONDS] & 0x7f);
69 tmp->tm_min = bcd2bin( buf[RV3029C2_W_MINUTES] & 0x7f);
70 if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_12_24) {
72 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f);
73 if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM)
77 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f);
79 tmp->tm_mday = bcd2bin( buf[RV3029C2_W_DATE] & 0x3F );
80 tmp->tm_mon = bcd2bin( buf[RV3029C2_W_MONTHS] & 0x1F );
81 tmp->tm_wday = bcd2bin( buf[RV3029C2_W_DAYS] & 0x07 );
82 /* RTC supports only years > 1999 */
83 tmp->tm_year = bcd2bin( buf[RV3029C2_W_YEARS]) + 2000;
88 printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
89 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
90 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
96 int rtc_set( struct rtc_time *tmp )
99 unsigned char buf[RTC_RV3029_PAGE_LEN];
101 printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
102 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
103 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
106 if (tmp->tm_year < 2000) {
107 printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
110 buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec);
111 buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min);
112 buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour);
114 buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24;
115 buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday);
116 buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday);
117 buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon);
118 tmp->tm_year -= 2000;
119 buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year);
120 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1,
121 buf, RTC_RV3029_PAGE_LEN);
123 /* give the RTC some time to update */
128 /* sets EERE-Bit (automatic EEPROM refresh) */
129 static void set_eere_bit(int state)
132 unsigned char reg_ctrl1;
134 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
138 reg_ctrl1 |= RTC_RV3029_CTRL1_EERE;
140 reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE);
142 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
146 /* waits until EEPROM page is no longer busy (times out after 10ms*loops) */
147 static int wait_eebusy(int loops)
150 unsigned char ctrl_status;
152 for (i = 0; i < loops; i++) {
153 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS,
156 if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0)
163 void rtc_reset (void)
166 unsigned char buf[RTC_RV3029_PAGE_LEN];
168 buf[0] = RTC_RV3029_CTRL_SYS_R;
169 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
172 #if defined(CONFIG_SYS_RV3029_TCR)
174 * because EEPROM_CTRL register is in EEPROM page it is necessary to
175 * disable automatic EEPROM refresh and check if EEPROM is busy
176 * before EEPORM_CTRL register may be accessed
180 /* read current trickle charger setting */
181 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL,
183 /* enable automatic EEPROM refresh again */
187 * to minimize EEPROM access write trickle charger setting only if it
188 * differs from current value
190 if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) {
191 buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR;
193 * write trickle charger setting (disable autom. EEPROM
194 * refresh and wait until EEPROM is idle)
198 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
199 RTC_RV3029_EEPROM_CTRL, 1, buf, 1);
201 * it is necessary to wait 10ms before EEBUSY-Bit may be read
202 * (this is not documented in the data sheet yet, but the
203 * manufacturer recommends it)
206 /* wait until EEPROM write access is finished */