xref: /openbmc/u-boot/drivers/rtc/rv3029.c (revision 23ff8633)
1 /*
2  * (C) Copyright 2010
3  * Heiko Schocher, DENX Software Engineering, hs@denx.de
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <command.h>
9 #include <i2c.h>
10 #include <rtc.h>
11 
12 #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 
18 #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 
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)
40 
41 int 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 
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 );
74 
75 	return 0;
76 }
77 
78 int rtc_set( struct rtc_time *tmp )
79 {
80 	int	ret;
81 	unsigned char buf[RTC_RV3029_PAGE_LEN];
82 
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);
86 
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);
106 	return ret;
107 }
108 
109 /* sets EERE-Bit  (automatic EEPROM refresh) */
110 static void set_eere_bit(int state)
111 {
112 	unsigned char reg_ctrl1;
113 
114 	(void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
115 			&reg_ctrl1, 1);
116 
117 	if (state)
118 		reg_ctrl1 |= RTC_RV3029_CTRL1_EERE;
119 	else
120 		reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE);
121 
122 	(void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
123 		&reg_ctrl1, 1);
124 }
125 
126 /* waits until EEPROM page is no longer busy (times out after 10ms*loops) */
127 static int wait_eebusy(int loops)
128 {
129 	int i;
130 	unsigned char ctrl_status;
131 
132 	for (i = 0; i < loops; i++) {
133 		(void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS,
134 			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 
143 void rtc_reset (void)
144 {
145 	unsigned char buf[RTC_RV3029_PAGE_LEN];
146 
147 	buf[0] = RTC_RV3029_CTRL_SYS_R;
148 	(void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
149 			buf, 1);
150 
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 */
160 	(void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL,
161 			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);
177 		(void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
178 				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
190 }
191