1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007
4  * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com.
5  */
6 
7 /*
8  * Epson RX8025 RTC driver.
9  */
10 
11 #include <command.h>
12 #include <dm.h>
13 #include <i2c.h>
14 #include <rtc.h>
15 
16 /*---------------------------------------------------------------------*/
17 #undef DEBUG_RTC
18 
19 #ifdef DEBUG_RTC
20 #define DEBUGR(fmt,args...) printf(fmt ,##args)
21 #else
22 #define DEBUGR(fmt,args...)
23 #endif
24 /*---------------------------------------------------------------------*/
25 
26 enum rx_model {
27 	model_rx_8025,
28 	model_rx_8035,
29 };
30 
31 /*
32  * RTC register addresses
33  */
34 #define RTC_SEC_REG_ADDR	0x00
35 #define RTC_MIN_REG_ADDR	0x01
36 #define RTC_HR_REG_ADDR		0x02
37 #define RTC_DAY_REG_ADDR	0x03
38 #define RTC_DATE_REG_ADDR	0x04
39 #define RTC_MON_REG_ADDR	0x05
40 #define RTC_YR_REG_ADDR		0x06
41 #define RTC_OFFSET_REG_ADDR	0x07
42 
43 #define RTC_CTL1_REG_ADDR	0x0e
44 #define RTC_CTL2_REG_ADDR	0x0f
45 
46 /*
47  * Control register 1 bits
48  */
49 #define RTC_CTL1_BIT_2412	0x20
50 
51 /*
52  * Control register 2 bits
53  */
54 #define RTC_CTL2_BIT_PON	0x10
55 #define RTC_CTL2_BIT_VDET	0x40
56 #define RTC_CTL2_BIT_XST	0x20
57 #define RTC_CTL2_BIT_VDSL	0x80
58 
59 /*
60  * Note: the RX8025 I2C RTC requires register
61  * reads and write to consist of a single bus
62  * cycle. It is not allowed to write the register
63  * address in a first cycle that is terminated by
64  * a STOP condition. The chips needs a 'restart'
65  * sequence (start sequence without a prior stop).
66  */
67 
68 #define rtc_read(reg) buf[(reg) & 0xf]
69 
70 static int rtc_write(struct udevice *dev, uchar reg, uchar val);
71 
rx8025_is_osc_stopped(enum rx_model model,int ctrl2)72 static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
73 {
74 	int xstp = ctrl2 & RTC_CTL2_BIT_XST;
75 	/* XSTP bit has different polarity on RX-8025 vs RX-8035.
76 	 * RX-8025: 0 == oscillator stopped
77 	 * RX-8035: 1 == oscillator stopped
78 	 */
79 
80 	if (model == model_rx_8025)
81 		xstp = !xstp;
82 
83 	return xstp;
84 }
85 
86 /*
87  * Get the current time from the RTC
88  */
rx8025_rtc_get(struct udevice * dev,struct rtc_time * tmp)89 static int rx8025_rtc_get(struct udevice *dev, struct rtc_time *tmp)
90 {
91 	int rel = 0;
92 	uchar sec, min, hour, mday, wday, mon, year, ctl2;
93 	uchar buf[16];
94 
95 	if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
96 		printf("Error reading from RTC\n");
97 		return -EIO;
98 	}
99 
100 	sec = rtc_read(RTC_SEC_REG_ADDR);
101 	min = rtc_read(RTC_MIN_REG_ADDR);
102 	hour = rtc_read(RTC_HR_REG_ADDR);
103 	wday = rtc_read(RTC_DAY_REG_ADDR);
104 	mday = rtc_read(RTC_DATE_REG_ADDR);
105 	mon = rtc_read(RTC_MON_REG_ADDR);
106 	year = rtc_read(RTC_YR_REG_ADDR);
107 
108 	DEBUGR("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
109 	       "hr: %02x min: %02x sec: %02x\n",
110 	       year, mon, mday, wday, hour, min, sec);
111 
112 	/* dump status */
113 	ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
114 	if (ctl2 & RTC_CTL2_BIT_PON) {
115 		printf("RTC: power-on detected\n");
116 		rel = -1;
117 	}
118 
119 	if (ctl2 & RTC_CTL2_BIT_VDET) {
120 		printf("RTC: voltage drop detected\n");
121 		rel = -1;
122 	}
123 	if (rx8025_is_osc_stopped(dev->driver_data, ctl2)) {
124 		printf("RTC: oscillator stop detected\n");
125 		rel = -1;
126 	}
127 
128 	tmp->tm_sec  = bcd2bin(sec & 0x7F);
129 	tmp->tm_min  = bcd2bin(min & 0x7F);
130 	if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412)
131 		tmp->tm_hour = bcd2bin(hour & 0x3F);
132 	else
133 		tmp->tm_hour = bcd2bin(hour & 0x1F) % 12 +
134 			       ((hour & 0x20) ? 12 : 0);
135 
136 	tmp->tm_mday = bcd2bin (mday & 0x3F);
137 	tmp->tm_mon  = bcd2bin (mon & 0x1F);
138 	tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
139 	tmp->tm_wday = bcd2bin (wday & 0x07);
140 	tmp->tm_yday = 0;
141 	tmp->tm_isdst= 0;
142 
143 	DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
144 	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
145 	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
146 
147 	return rel;
148 }
149 
150 /*
151  * Set the RTC
152  */
rx8025_rtc_set(struct udevice * dev,const struct rtc_time * tmp)153 static int rx8025_rtc_set(struct udevice *dev, const struct rtc_time *tmp)
154 {
155 	/* To work around the read/write cycle issue mentioned
156 	 * at the top of this file, write all the time registers
157 	 * in one I2C transaction
158 	 */
159 	u8 write_op[8];
160 
161 	/* 2412 flag must be set before doing a RTC write,
162 	 * otherwise the seconds and minute register
163 	 * will be cleared when the flag is set
164 	 */
165 	if (rtc_write(dev, RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412))
166 		return -EIO;
167 
168 	DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
169 	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
170 	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
171 
172 	if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
173 		printf("WARNING: year should be between 1970 and 2069!\n");
174 
175 	write_op[RTC_SEC_REG_ADDR]  = bin2bcd(tmp->tm_sec);
176 	write_op[RTC_MIN_REG_ADDR]  = bin2bcd(tmp->tm_min);
177 	write_op[RTC_HR_REG_ADDR]   = bin2bcd(tmp->tm_hour);
178 	write_op[RTC_DAY_REG_ADDR]	= bin2bcd(tmp->tm_wday);
179 	write_op[RTC_DATE_REG_ADDR]	= bin2bcd(tmp->tm_mday);
180 	write_op[RTC_MON_REG_ADDR]  = bin2bcd(tmp->tm_mon);
181 	write_op[RTC_YR_REG_ADDR]	= bin2bcd(tmp->tm_year % 100);
182 	write_op[RTC_OFFSET_REG_ADDR] = 0;
183 
184 	return dm_i2c_write(dev, 0, &write_op[0], 8);
185 }
186 
187 /*
188  * Reset the RTC
189  */
rx8025_rtc_reset(struct udevice * dev)190 static int rx8025_rtc_reset(struct udevice *dev)
191 {
192 	uchar buf[16];
193 	uchar ctl2;
194 
195 	if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
196 		printf("Error reading from RTC\n");
197 		return -EIO;
198 	}
199 
200 	ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
201 	ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
202 
203 	if (dev->driver_data == model_rx_8035)
204 		ctl2 &= ~(RTC_CTL2_BIT_XST);
205 	else
206 		ctl2 |= RTC_CTL2_BIT_XST;
207 
208 	return rtc_write(dev, RTC_CTL2_REG_ADDR, ctl2);
209 }
210 
211 /*
212  * Helper functions
213  */
rtc_write(struct udevice * dev,uchar reg,uchar val)214 static int rtc_write(struct udevice *dev, uchar reg, uchar val)
215 {
216 	/* The RX8025/RX8035 uses the top 4 bits of the
217 	 * 'offset' byte as the start register address,
218 	 * and the bottom 4 bits as a 'transfer' mode setting
219 	 * (only applicable for reads)
220 	 */
221 	u8 offset = (reg << 4);
222 
223 	if (dm_i2c_reg_write(dev, offset, val)) {
224 		printf("Error writing to RTC\n");
225 		return -EIO;
226 	}
227 
228 	return 0;
229 }
230 
rx8025_probe(struct udevice * dev)231 static int rx8025_probe(struct udevice *dev)
232 {
233 	uchar buf[16];
234 	int ret = 0;
235 
236 	if (i2c_get_chip_offset_len(dev) != 1)
237 		ret = i2c_set_chip_offset_len(dev, 1);
238 
239 	if (ret)
240 		return ret;
241 
242 	return dm_i2c_read(dev, 0, buf, sizeof(buf));
243 }
244 
245 static const struct rtc_ops rx8025_rtc_ops = {
246 	.get = rx8025_rtc_get,
247 	.set = rx8025_rtc_set,
248 	.reset = rx8025_rtc_reset,
249 };
250 
251 static const struct udevice_id rx8025_rtc_ids[] = {
252 	{ .compatible = "epson,rx8025", .data = model_rx_8025 },
253 	{ .compatible = "epson,rx8035", .data = model_rx_8035 },
254 	{ }
255 };
256 
257 U_BOOT_DRIVER(rx8025_rtc) = {
258 	.name	  = "rx8025_rtc",
259 	.id	      = UCLASS_RTC,
260 	.probe    = rx8025_probe,
261 	.of_match = rx8025_rtc_ids,
262 	.ops	  = &rx8025_rtc_ops,
263 };
264