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;
87 debug( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
88 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
89 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
94 int rtc_set( struct rtc_time *tmp )
97 unsigned char buf[RTC_RV3029_PAGE_LEN];
99 debug( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
100 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
101 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
103 if (tmp->tm_year < 2000) {
104 printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
107 buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec);
108 buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min);
109 buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour);
111 buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24;
112 buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday);
113 buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday);
114 buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon);
115 tmp->tm_year -= 2000;
116 buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year);
117 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1,
118 buf, RTC_RV3029_PAGE_LEN);
120 /* give the RTC some time to update */
125 /* sets EERE-Bit (automatic EEPROM refresh) */
126 static void set_eere_bit(int state)
128 unsigned char reg_ctrl1;
130 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
134 reg_ctrl1 |= RTC_RV3029_CTRL1_EERE;
136 reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE);
138 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
142 /* waits until EEPROM page is no longer busy (times out after 10ms*loops) */
143 static int wait_eebusy(int loops)
146 unsigned char ctrl_status;
148 for (i = 0; i < loops; i++) {
149 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS,
152 if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0)
159 void rtc_reset (void)
161 unsigned char buf[RTC_RV3029_PAGE_LEN];
163 buf[0] = RTC_RV3029_CTRL_SYS_R;
164 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
167 #if defined(CONFIG_SYS_RV3029_TCR)
169 * because EEPROM_CTRL register is in EEPROM page it is necessary to
170 * disable automatic EEPROM refresh and check if EEPROM is busy
171 * before EEPORM_CTRL register may be accessed
175 /* read current trickle charger setting */
176 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL,
178 /* enable automatic EEPROM refresh again */
182 * to minimize EEPROM access write trickle charger setting only if it
183 * differs from current value
185 if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) {
186 buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR;
188 * write trickle charger setting (disable autom. EEPROM
189 * refresh and wait until EEPROM is idle)
193 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
194 RTC_RV3029_EEPROM_CTRL, 1, buf, 1);
196 * it is necessary to wait 10ms before EEBUSY-Bit may be read
197 * (this is not documented in the data sheet yet, but the
198 * manufacturer recommends it)
201 /* wait until EEPROM write access is finished */