blob: 8033695ba6d8eb2c2de8cdd1a54adfe1069e9f23 [file] [log] [blame]
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +01001/*
2 * (C) Copyright 2010
3 * Heiko Schocher, DENX Software Engineering, hs@denx.de
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23#include <common.h>
24#include <command.h>
25#include <i2c.h>
26#include <rtc.h>
27
Heiko Schocher71d19f32011-03-28 09:24:22 +020028#define RTC_RV3029_CTRL1 0x00
29#define RTC_RV3029_CTRL1_EERE (1 << 3)
30
31#define RTC_RV3029_CTRL_STATUS 0x03
32#define RTC_RV3029_CTRLS_EEBUSY (1 << 7)
33
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010034#define RTC_RV3029_CTRL_RESET 0x04
35#define RTC_RV3029_CTRL_SYS_R (1 << 4)
36
37#define RTC_RV3029_CLOCK_PAGE 0x08
38#define RTC_RV3029_PAGE_LEN 7
39
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
47
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 */
50
Heiko Schocher71d19f32011-03-28 09:24:22 +020051#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)
56
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010057int rtc_get( struct rtc_time *tmp )
58{
59 int ret;
60 unsigned char buf[RTC_RV3029_PAGE_LEN];
61
62 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \
63 RTC_RV3029_PAGE_LEN);
64 if (ret) {
65 printf("%s: error reading RTC: %x\n", __func__, ret);
66 return -1;
67 }
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) {
71 /* 12h format */
72 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f);
73 if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM)
74 /* PM flag set */
75 tmp->tm_hour += 12;
76 } else
77 tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f);
78
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;
84 tmp->tm_yday = 0;
85 tmp->tm_isdst = 0;
86
Wolfgang Denkb6337412011-11-04 15:55:13 +000087 debug( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010088 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
89 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
90
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010091 return 0;
92}
93
94int rtc_set( struct rtc_time *tmp )
95{
96 int ret;
97 unsigned char buf[RTC_RV3029_PAGE_LEN];
Wolfgang Denkb6337412011-11-04 15:55:13 +000098
99 debug( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100100 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
101 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100102
103 if (tmp->tm_year < 2000) {
104 printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
105 return -1;
106 }
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);
110 /* set 24h format */
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);
119
120 /* give the RTC some time to update */
121 udelay(1000);
Wolfgang Denkb6337412011-11-04 15:55:13 +0000122 return ret;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100123}
124
Heiko Schocher71d19f32011-03-28 09:24:22 +0200125/* sets EERE-Bit (automatic EEPROM refresh) */
126static void set_eere_bit(int state)
127{
Heiko Schocher71d19f32011-03-28 09:24:22 +0200128 unsigned char reg_ctrl1;
129
Wolfgang Denkb6337412011-11-04 15:55:13 +0000130 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
Heiko Schocher71d19f32011-03-28 09:24:22 +0200131 &reg_ctrl1, 1);
132
133 if (state)
134 reg_ctrl1 |= RTC_RV3029_CTRL1_EERE;
135 else
136 reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE);
137
Wolfgang Denkb6337412011-11-04 15:55:13 +0000138 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
Heiko Schocher71d19f32011-03-28 09:24:22 +0200139 &reg_ctrl1, 1);
140}
141
142/* waits until EEPROM page is no longer busy (times out after 10ms*loops) */
143static int wait_eebusy(int loops)
144{
Wolfgang Denkb6337412011-11-04 15:55:13 +0000145 int i;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200146 unsigned char ctrl_status;
147
148 for (i = 0; i < loops; i++) {
Wolfgang Denkb6337412011-11-04 15:55:13 +0000149 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS,
Heiko Schocher71d19f32011-03-28 09:24:22 +0200150 1, &ctrl_status, 1);
151
152 if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0)
153 break;
154 udelay(10000);
155 }
156 return i;
157}
158
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100159void rtc_reset (void)
160{
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100161 unsigned char buf[RTC_RV3029_PAGE_LEN];
162
163 buf[0] = RTC_RV3029_CTRL_SYS_R;
Wolfgang Denkb6337412011-11-04 15:55:13 +0000164 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100165 buf, 1);
Heiko Schocher71d19f32011-03-28 09:24:22 +0200166
167#if defined(CONFIG_SYS_RV3029_TCR)
168 /*
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
172 */
173 set_eere_bit(0);
174 wait_eebusy(100);
175 /* read current trickle charger setting */
Wolfgang Denkb6337412011-11-04 15:55:13 +0000176 (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL,
Heiko Schocher71d19f32011-03-28 09:24:22 +0200177 1, buf, 1);
178 /* enable automatic EEPROM refresh again */
179 set_eere_bit(1);
180
181 /*
182 * to minimize EEPROM access write trickle charger setting only if it
183 * differs from current value
184 */
185 if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) {
186 buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR;
187 /*
188 * write trickle charger setting (disable autom. EEPROM
189 * refresh and wait until EEPROM is idle)
190 */
191 set_eere_bit(0);
192 wait_eebusy(100);
Wolfgang Denkb6337412011-11-04 15:55:13 +0000193 (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
Heiko Schocher71d19f32011-03-28 09:24:22 +0200194 RTC_RV3029_EEPROM_CTRL, 1, buf, 1);
195 /*
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)
199 */
200 udelay(10000);
201 /* wait until EEPROM write access is finished */
202 wait_eebusy(100);
203 set_eere_bit(1);
204 }
205#endif
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100206}