blob: f08040a8599a201b2b9c7727d23415be3b9bbfc1 [file] [log] [blame]
Heiko Schocher641f0ab2011-01-12 08:20:05 +01001/*
2 * (C) Copyright 2010
3 * Heiko Schocher, DENX Software Engineering, hs@denx.de
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Heiko Schocher641f0ab2011-01-12 08:20:05 +01006 */
7#include <common.h>
8#include <command.h>
9#include <i2c.h>
10#include <rtc.h>
11
Heiko Schocher1f1b7012011-03-28 09:24:22 +020012#define RTC_RV3029_CTRL1 0x00
13#define RTC_RV3029_CTRL1_EERE (1 << 3)
14
15#define RTC_RV3029_CTRL_STATUS 0x03
16#define RTC_RV3029_CTRLS_EEBUSY (1 << 7)
17
Heiko Schocher641f0ab2011-01-12 08:20:05 +010018#define RTC_RV3029_CTRL_RESET 0x04
19#define RTC_RV3029_CTRL_SYS_R (1 << 4)
20
21#define RTC_RV3029_CLOCK_PAGE 0x08
22#define RTC_RV3029_PAGE_LEN 7
23
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
31
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 */
34
Heiko Schocher1f1b7012011-03-28 09:24:22 +020035#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)
40
Heiko Schocher641f0ab2011-01-12 08:20:05 +010041int rtc_get( struct rtc_time *tmp )
42{
43 int ret;
44 unsigned char buf[RTC_RV3029_PAGE_LEN];
45
46 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \
47 RTC_RV3029_PAGE_LEN);
48 if (ret) {
49 printf("%s: error reading RTC: %x\n", __func__, ret);
50 return -1;
51 }
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) {
55 /* 12h format */
56 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f);
57 if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM)
58 /* PM flag set */
59 tmp->tm_hour += 12;
60 } else
61 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f);
62
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;
68 tmp->tm_yday = 0;
69 tmp->tm_isdst = 0;
70
Wolfgang Denkf2974f62011-11-04 15:55:13 +000071 debug( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
Heiko Schocher641f0ab2011-01-12 08:20:05 +010072 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
73 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
74
Heiko Schocher641f0ab2011-01-12 08:20:05 +010075 return 0;
76}
77
78int rtc_set( struct rtc_time *tmp )
79{
80 int ret;
81 unsigned char buf[RTC_RV3029_PAGE_LEN];
Wolfgang Denkf2974f62011-11-04 15:55:13 +000082
83 debug( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
Heiko Schocher641f0ab2011-01-12 08:20:05 +010084 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
85 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Heiko Schocher641f0ab2011-01-12 08:20:05 +010086
87 if (tmp->tm_year < 2000) {
88 printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
89 return -1;
90 }
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);
94 /* set 24h format */
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);
99 tmp->tm_year -= 2000;
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);
103
104 /* give the RTC some time to update */
105 udelay(1000);
Wolfgang Denkf2974f62011-11-04 15:55:13 +0000106 return ret;
Heiko Schocher641f0ab2011-01-12 08:20:05 +0100107}
108
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200109/* sets EERE-Bit (automatic EEPROM refresh) */
110static void set_eere_bit(int state)
111{
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200112 unsigned char reg_ctrl1;
113
Wolfgang Denkf2974f62011-11-04 15:55:13 +0000114 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200115 &reg_ctrl1, 1);
116
117 if (state)
118 reg_ctrl1 |= RTC_RV3029_CTRL1_EERE;
119 else
120 reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE);
121
Wolfgang Denkf2974f62011-11-04 15:55:13 +0000122 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200123 &reg_ctrl1, 1);
124}
125
126/* waits until EEPROM page is no longer busy (times out after 10ms*loops) */
127static int wait_eebusy(int loops)
128{
Wolfgang Denkf2974f62011-11-04 15:55:13 +0000129 int i;
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200130 unsigned char ctrl_status;
131
132 for (i = 0; i < loops; i++) {
Wolfgang Denkf2974f62011-11-04 15:55:13 +0000133 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS,
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200134 1, &ctrl_status, 1);
135
136 if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0)
137 break;
138 udelay(10000);
139 }
140 return i;
141}
142
Heiko Schocher641f0ab2011-01-12 08:20:05 +0100143void rtc_reset (void)
144{
Heiko Schocher641f0ab2011-01-12 08:20:05 +0100145 unsigned char buf[RTC_RV3029_PAGE_LEN];
146
147 buf[0] = RTC_RV3029_CTRL_SYS_R;
Wolfgang Denkf2974f62011-11-04 15:55:13 +0000148 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
Heiko Schocher641f0ab2011-01-12 08:20:05 +0100149 buf, 1);
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200150
151#if defined(CONFIG_SYS_RV3029_TCR)
152 /*
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
156 */
157 set_eere_bit(0);
158 wait_eebusy(100);
159 /* read current trickle charger setting */
Wolfgang Denkf2974f62011-11-04 15:55:13 +0000160 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL,
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200161 1, buf, 1);
162 /* enable automatic EEPROM refresh again */
163 set_eere_bit(1);
164
165 /*
166 * to minimize EEPROM access write trickle charger setting only if it
167 * differs from current value
168 */
169 if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) {
170 buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR;
171 /*
172 * write trickle charger setting (disable autom. EEPROM
173 * refresh and wait until EEPROM is idle)
174 */
175 set_eere_bit(0);
176 wait_eebusy(100);
Wolfgang Denkf2974f62011-11-04 15:55:13 +0000177 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
Heiko Schocher1f1b7012011-03-28 09:24:22 +0200178 RTC_RV3029_EEPROM_CTRL, 1, buf, 1);
179 /*
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)
183 */
184 udelay(10000);
185 /* wait until EEPROM write access is finished */
186 wait_eebusy(100);
187 set_eere_bit(1);
188 }
189#endif
Heiko Schocher641f0ab2011-01-12 08:20:05 +0100190}