3 * Heiko Schocher, DENX Software Engineering, hs@denx.de
5 * SPDX-License-Identifier: GPL-2.0+
12 #define RTC_RV3029_CTRL1 0x00
13 #define RTC_RV3029_CTRL1_EERE (1 << 3)
15 #define RTC_RV3029_CTRL_STATUS 0x03
16 #define RTC_RV3029_CTRLS_EEBUSY (1 << 7)
18 #define RTC_RV3029_CTRL_RESET 0x04
19 #define RTC_RV3029_CTRL_SYS_R (1 << 4)
21 #define RTC_RV3029_CLOCK_PAGE 0x08
22 #define RTC_RV3029_PAGE_LEN 7
24 #define RV3029C2_W_SECONDS 0x00
25 #define RV3029C2_W_MINUTES 0x01
26 #define RV3029C2_W_HOURS 0x02
27 #define RV3029C2_W_DATE 0x03
28 #define RV3029C2_W_DAYS 0x04
29 #define RV3029C2_W_MONTHS 0x05
30 #define RV3029C2_W_YEARS 0x06
32 #define RV3029C2_REG_HR_12_24 (1 << 6) /* 24h/12h mode */
33 #define RV3029C2_REG_HR_PM (1 << 5) /* PM/AM bit in 12h mode */
35 #define RTC_RV3029_EEPROM_CTRL 0x30
36 #define RTC_RV3029_TRICKLE_1K (1 << 4)
37 #define RTC_RV3029_TRICKLE_5K (1 << 5)
38 #define RTC_RV3029_TRICKLE_20K (1 << 6)
39 #define RTC_RV3029_TRICKLE_80K (1 << 7)
41 int rtc_get( struct rtc_time *tmp )
44 unsigned char buf[RTC_RV3029_PAGE_LEN];
46 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \
49 printf("%s: error reading RTC: %x\n", __func__, ret);
52 tmp->tm_sec = bcd2bin( buf[RV3029C2_W_SECONDS] & 0x7f);
53 tmp->tm_min = bcd2bin( buf[RV3029C2_W_MINUTES] & 0x7f);
54 if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_12_24) {
56 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f);
57 if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM)
61 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f);
63 tmp->tm_mday = bcd2bin( buf[RV3029C2_W_DATE] & 0x3F );
64 tmp->tm_mon = bcd2bin( buf[RV3029C2_W_MONTHS] & 0x1F );
65 tmp->tm_wday = bcd2bin( buf[RV3029C2_W_DAYS] & 0x07 );
66 /* RTC supports only years > 1999 */
67 tmp->tm_year = bcd2bin( buf[RV3029C2_W_YEARS]) + 2000;
71 debug( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
72 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
73 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
78 int rtc_set( struct rtc_time *tmp )
81 unsigned char buf[RTC_RV3029_PAGE_LEN];
83 debug( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
84 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
85 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
87 if (tmp->tm_year < 2000) {
88 printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
91 buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec);
92 buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min);
93 buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour);
95 buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24;
96 buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday);
97 buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday);
98 buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon);
100 buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year);
101 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1,
102 buf, RTC_RV3029_PAGE_LEN);
104 /* give the RTC some time to update */
109 /* sets EERE-Bit (automatic EEPROM refresh) */
110 static void set_eere_bit(int state)
112 unsigned char reg_ctrl1;
114 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
118 reg_ctrl1 |= RTC_RV3029_CTRL1_EERE;
120 reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE);
122 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
126 /* waits until EEPROM page is no longer busy (times out after 10ms*loops) */
127 static int wait_eebusy(int loops)
130 unsigned char ctrl_status;
132 for (i = 0; i < loops; i++) {
133 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS,
136 if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0)
143 void rtc_reset (void)
145 unsigned char buf[RTC_RV3029_PAGE_LEN];
147 buf[0] = RTC_RV3029_CTRL_SYS_R;
148 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
151 #if defined(CONFIG_SYS_RV3029_TCR)
153 * because EEPROM_CTRL register is in EEPROM page it is necessary to
154 * disable automatic EEPROM refresh and check if EEPROM is busy
155 * before EEPORM_CTRL register may be accessed
159 /* read current trickle charger setting */
160 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL,
162 /* enable automatic EEPROM refresh again */
166 * to minimize EEPROM access write trickle charger setting only if it
167 * differs from current value
169 if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) {
170 buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR;
172 * write trickle charger setting (disable autom. EEPROM
173 * refresh and wait until EEPROM is idle)
177 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
178 RTC_RV3029_EEPROM_CTRL, 1, buf, 1);
180 * it is necessary to wait 10ms before EEBUSY-Bit may be read
181 * (this is not documented in the data sheet yet, but the
182 * manufacturer recommends it)
185 /* wait until EEPROM write access is finished */