blob: 9be58425bfba05e1973edd1155a480bb41caf0b3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +01002/*
Philipp Tomsicha73610d2018-11-27 22:53:58 +01003 * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
4 *
5 * Based on a the Linux rtc-rv3029c2.c driver written by:
6 * Gregory Hermant <gregory.hermant@calao-systems.com>
7 * Michael Buesch <m@bues.ch>
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +01008 */
Philipp Tomsicha73610d2018-11-27 22:53:58 +01009
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010010#include <common.h>
11#include <command.h>
Philipp Tomsicha73610d2018-11-27 22:53:58 +010012#include <dm.h>
Simon Glasscb3ef682019-11-14 12:57:50 -070013#include <eeprom.h>
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010014#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010016#include <rtc.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010019
Philipp Tomsicha73610d2018-11-27 22:53:58 +010020#define RTC_RV3029_PAGE_LEN 7
Heiko Schocher71d19f32011-03-28 09:24:22 +020021
Philipp Tomsicha73610d2018-11-27 22:53:58 +010022/* control section */
23#define RV3029_ONOFF_CTRL 0x00
24#define RV3029_ONOFF_CTRL_WE BIT(0)
25#define RV3029_ONOFF_CTRL_TE BIT(1)
26#define RV3029_ONOFF_CTRL_TAR BIT(2)
27#define RV3029_ONOFF_CTRL_EERE BIT(3)
28#define RV3029_ONOFF_CTRL_SRON BIT(4)
29#define RV3029_ONOFF_CTRL_TD0 BIT(5)
30#define RV3029_ONOFF_CTRL_TD1 BIT(6)
31#define RV3029_ONOFF_CTRL_CLKINT BIT(7)
32#define RV3029_IRQ_CTRL 0x01
33#define RV3029_IRQ_CTRL_AIE BIT(0)
34#define RV3029_IRQ_CTRL_TIE BIT(1)
35#define RV3029_IRQ_CTRL_V1IE BIT(2)
36#define RV3029_IRQ_CTRL_V2IE BIT(3)
37#define RV3029_IRQ_CTRL_SRIE BIT(4)
38#define RV3029_IRQ_FLAGS 0x02
39#define RV3029_IRQ_FLAGS_AF BIT(0)
40#define RV3029_IRQ_FLAGS_TF BIT(1)
41#define RV3029_IRQ_FLAGS_V1IF BIT(2)
42#define RV3029_IRQ_FLAGS_V2IF BIT(3)
43#define RV3029_IRQ_FLAGS_SRF BIT(4)
44#define RV3029_STATUS 0x03
45#define RV3029_STATUS_VLOW1 BIT(2)
46#define RV3029_STATUS_VLOW2 BIT(3)
47#define RV3029_STATUS_SR BIT(4)
48#define RV3029_STATUS_PON BIT(5)
49#define RV3029_STATUS_EEBUSY BIT(7)
50#define RV3029_RST_CTRL 0x04
51#define RV3029_RST_CTRL_SYSR BIT(4)
52#define RV3029_CONTROL_SECTION_LEN 0x05
Heiko Schocher71d19f32011-03-28 09:24:22 +020053
Philipp Tomsicha73610d2018-11-27 22:53:58 +010054/* watch section */
55#define RV3029_W_SEC 0x08
56#define RV3029_W_MINUTES 0x09
57#define RV3029_W_HOURS 0x0A
58#define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
59#define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
60#define RV3029_W_DATE 0x0B
61#define RV3029_W_DAYS 0x0C
62#define RV3029_W_MONTHS 0x0D
63#define RV3029_W_YEARS 0x0E
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010064
Philipp Tomsicha73610d2018-11-27 22:53:58 +010065/* eeprom control section */
66#define RV3029_CONTROL_E2P_EECTRL 0x30
67#define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
68#define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
69#define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
70#define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
71#define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
72 RV3029_TRICKLE_5K |\
73 RV3029_TRICKLE_20K |\
74 RV3029_TRICKLE_80K)
75#define RV3029_TRICKLE_SHIFT 4
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010076
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010077
Philipp Tomsicha73610d2018-11-27 22:53:58 +010078static int rv3029_rtc_get(struct udevice *dev, struct rtc_time *tm)
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010079{
Philipp Tomsicha73610d2018-11-27 22:53:58 +010080 u8 regs[RTC_RV3029_PAGE_LEN];
81 int ret;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010082
Philipp Tomsicha73610d2018-11-27 22:53:58 +010083 ret = dm_i2c_read(dev, RV3029_W_SEC, regs, sizeof(regs));
84 if (ret < 0) {
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010085 printf("%s: error reading RTC: %x\n", __func__, ret);
Philipp Tomsicha73610d2018-11-27 22:53:58 +010086 return -EIO;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010087 }
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010088
Philipp Tomsicha73610d2018-11-27 22:53:58 +010089 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
90 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
91
92 /* HR field has a more complex interpretation */
93 {
94 const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
95
96 if (_hr & RV3029_REG_HR_12_24) {
97 /* 12h format */
98 tm->tm_hour = bcd2bin(_hr & 0x1f);
99 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
100 tm->tm_hour += 12;
101 } else {
102 /* 24h format */
103 tm->tm_hour = bcd2bin(_hr & 0x3f);
104 }
105 }
106
107 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
108 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100109 /* RTC supports only years > 1999 */
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100110 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 2000;
111 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100112
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100113 tm->tm_yday = 0;
114 tm->tm_isdst = 0;
115
116 debug("%s: %4d-%02d-%02d (wday=%d) %2d:%02d:%02d\n",
117 __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
118 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100119
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100120 return 0;
121}
122
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100123static int rv3029_rtc_set(struct udevice *dev, const struct rtc_time *tm)
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100124{
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100125 u8 regs[RTC_RV3029_PAGE_LEN];
Wolfgang Denkb6337412011-11-04 15:55:13 +0000126
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100127 debug("%s: %4d-%02d-%02d (wday=%d( %2d:%02d:%02d\n",
128 __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
129 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100130
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100131
132 if (tm->tm_year < 2000) {
133 printf("%s: year %d (before 2000) not supported\n",
134 __func__, tm->tm_year);
135 return -EINVAL;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100136 }
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100137
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100138 regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
139 regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
140 regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
141 regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
142 regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
143 regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
144 regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 2000);
145
146 return dm_i2c_write(dev, RV3029_W_SEC, regs, sizeof(regs));
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100147}
148
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100149static int rv3029_rtc_reset(struct udevice *dev)
Heiko Schocher71d19f32011-03-28 09:24:22 +0200150{
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100151 u8 ctrl = RV3029_RST_CTRL_SYSR;
152 unsigned long start;
153 const unsigned long timeout_ms = 10000;
154 int ret;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200155
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100156 /* trigger the system-reset */
157 ret = dm_i2c_write(dev, RV3029_RST_CTRL, &ctrl, 1);
158 if (ret < 0)
159 return -EIO;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200160
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100161 /* wait for the system-reset to complete */
162 start = get_timer(0);
163 do {
164 if (get_timer(start) > timeout_ms)
165 return -ETIMEDOUT;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200166
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100167 ret = dm_i2c_read(dev, RV3029_RST_CTRL, &ctrl, 1);
168 if (ret < 0)
169 return -EIO;
170 } while (ctrl & RV3029_RST_CTRL_SYSR);
171
172 return 0;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200173}
174
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100175static int rv3029_rtc_read8(struct udevice *dev, unsigned int reg)
Heiko Schocher71d19f32011-03-28 09:24:22 +0200176{
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100177 u8 data;
178 int ret;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200179
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100180 ret = dm_i2c_read(dev, reg, &data, sizeof(data));
181 return ret < 0 ? ret : data;
182}
Heiko Schocher71d19f32011-03-28 09:24:22 +0200183
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100184static int rv3029_rtc_write8(struct udevice *dev, unsigned int reg, int val)
185{
186 u8 data = val;
187
188 return dm_i2c_write(dev, reg, &data, 1);
189}
190
191#if defined(OF_CONTROL)
192static int rv3029_get_sr(struct udevice *dev, u8 *buf)
193{
194 int ret = dm_i2c_read(dev, RV3029_STATUS, buf, 1);
195
196 if (ret < 0)
197 return -EIO;
198
199 dev_dbg(dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
200 return 0;
201}
202
203static int rv3029_set_sr(struct udevice *dev, u8 val)
204{
205 int ret;
206
207 ret = dm_i2c_read(dev, RV3029_STATUS, &val, 1);
208 if (ret < 0)
209 return -EIO;
210
211 dev_dbg(dev, "status = 0x%.2x (%d)\n", val, val);
212 return 0;
213}
214
215static int rv3029_eeprom_busywait(struct udevice *dev)
216{
217 int i, ret;
218 u8 sr;
219
220 for (i = 100; i > 0; i--) {
221 ret = rv3029_get_sr(dev, &sr);
222 if (ret < 0)
223 break;
224 if (!(sr & RV3029_STATUS_EEBUSY))
Heiko Schocher71d19f32011-03-28 09:24:22 +0200225 break;
226 udelay(10000);
227 }
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100228 if (i <= 0) {
229 dev_err(dev, "EEPROM busy wait timeout.\n");
230 return -ETIMEDOUT;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200231 }
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100232
233 return ret;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100234}
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100235
236static int rv3029_update_bits(struct udevice *dev, u8 reg, u8 mask, u8 set)
237{
238 u8 buf;
239 int ret;
240
241 ret = dm_i2c_read(dev, reg, &buf, 1);
242 if (ret < 0)
243 return ret;
244
245 if ((buf & mask) == (set && mask))
246 return 0;
247
248 buf = (buf & ~mask) | (set & mask);
249 ret = dm_i2c_read(dev, reg, &buf, 1);
250 if (ret < 0)
251 return ret;
252
253 return 0;
254}
255
256static int rv3029_eeprom_exit(struct udevice *dev)
257{
258 /* Re-enable eeprom refresh */
259 return rv3029_update_bits(dev, RV3029_ONOFF_CTRL,
260 RV3029_ONOFF_CTRL_EERE,
261 RV3029_ONOFF_CTRL_EERE);
262}
263
264static int rv3029_eeprom_enter(struct udevice *dev)
265{
266 int ret;
267 u8 sr;
268
269 /* Check whether we are in the allowed voltage range. */
270 ret = rv3029_get_sr(dev, &sr);
271 if (ret < 0)
272 return ret;
273 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
274 /* We clear the bits and retry once just in case
275 * we had a brown out in early startup.
276 */
277 sr &= ~RV3029_STATUS_VLOW1;
278 sr &= ~RV3029_STATUS_VLOW2;
279 ret = rv3029_set_sr(dev, sr);
280 if (ret < 0)
281 return ret;
282 udelay(10000);
283 ret = rv3029_get_sr(dev, &sr);
284 if (ret < 0)
285 return ret;
286 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
287 dev_err(dev, "Supply voltage is too low to safely access the EEPROM.\n");
288 return -ENODEV;
289 }
290 }
291
292 /* Disable eeprom refresh. */
293 ret = rv3029_update_bits(dev,
294 RV3029_ONOFF_CTRL, RV3029_ONOFF_CTRL_EERE, 0);
295 if (ret < 0)
296 return ret;
297
298 /* Wait for any previous eeprom accesses to finish. */
299 ret = rv3029_eeprom_busywait(dev);
300 if (ret < 0)
301 rv3029_eeprom_exit(dev);
302
303 return ret;
304}
305
306static int rv3029_eeprom_read(struct udevice *dev, u8 reg,
307 u8 buf[], size_t len)
308{
309 int ret, err;
310
311 err = rv3029_eeprom_enter(dev);
312 if (err < 0)
313 return err;
314
315 ret = dm_i2c_read(dev, reg, buf, len);
316
317 err = rv3029_eeprom_exit(dev);
318 if (err < 0)
319 return err;
320
321 return ret;
322}
323
324static int rv3029_eeprom_write(struct udevice *dev, u8 reg,
325 u8 const buf[], size_t len)
326{
327 int ret;
328 size_t i;
329 u8 tmp;
330
331 ret = rv3029_eeprom_enter(dev);
332 if (ret < 0)
333 return ret;
334
335 for (i = 0; i < len; i++, reg++) {
336 ret = dm_i2c_read(dev, reg, &tmp, 1);
337 if (ret < 0)
338 break;
339 if (tmp != buf[i]) {
340 ret = dm_i2c_write(dev, reg, &buf[i], 1);
341 if (ret < 0)
342 break;
343 }
344 ret = rv3029_eeprom_busywait(dev);
345 if (ret < 0)
346 break;
347 }
348
349 ret = rv3029_eeprom_exit(dev);
350 if (ret < 0)
351 return ret;
352
353 return 0;
354}
355
356static int rv3029_eeprom_update_bits(struct udevice *dev,
357 u8 reg, u8 mask, u8 set)
358{
359 u8 buf;
360 int ret;
361
362 ret = rv3029_eeprom_read(dev, reg, &buf, 1);
363 if (ret < 0)
364 return ret;
365
366 /*
367 * If the EEPROM already reads the correct bitpattern, we don't need
368 * to update it.
369 */
370 if ((buf & mask) == (set & mask))
371 return 0;
372
373 buf = (buf & ~mask) | (set & mask);
374 ret = rv3029_eeprom_write(dev, reg, &buf, 1);
375 if (ret < 0)
376 return ret;
377
378 return 0;
379}
380
381static void rv3029_trickle_config(struct udevice *dev)
382{
383 static const struct rv3029_trickle_tab_elem {
384 u32 r; /* resistance in ohms */
385 u8 conf; /* trickle config bits */
386 } rv3029_trickle_tab[] = {
387 {
388 .r = 1076,
389 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
390 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
391 }, {
392 .r = 1091,
393 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
394 RV3029_TRICKLE_20K,
395 }, {
396 .r = 1137,
397 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
398 RV3029_TRICKLE_80K,
399 }, {
400 .r = 1154,
401 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
402 }, {
403 .r = 1371,
404 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
405 RV3029_TRICKLE_80K,
406 }, {
407 .r = 1395,
408 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
409 }, {
410 .r = 1472,
411 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
412 }, {
413 .r = 1500,
414 .conf = RV3029_TRICKLE_1K,
415 }, {
416 .r = 3810,
417 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
418 RV3029_TRICKLE_80K,
419 }, {
420 .r = 4000,
421 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
422 }, {
423 .r = 4706,
424 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
425 }, {
426 .r = 5000,
427 .conf = RV3029_TRICKLE_5K,
428 }, {
429 .r = 16000,
430 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
431 }, {
432 .r = 20000,
433 .conf = RV3029_TRICKLE_20K,
434 }, {
435 .r = 80000,
436 .conf = RV3029_TRICKLE_80K,
437 },
438 };
439 int err;
440 u32 ohms;
441 u8 trickle_set_bits = 0;
442
443 /* Configure the trickle charger. */
444 err = dev_read_u32(dev, "trickle-resistor-ohms", &ohms);
445
446 if (!err) {
447 /* Find trickle-charger config */
448 for (int i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++)
449 if (rv3029_trickle_tab[i].r >= ohms) {
450 dev_dbg(dev, "trickle charger at %d ohms\n",
451 rv3029_trickle_tab[i].r);
452 trickle_set_bits = rv3029_trickle_tab[i].conf;
453 break;
454 }
455 }
456
457 dev_dbg(dev, "trickle charger config 0x%x\n", trickle_set_bits);
458 err = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL,
459 RV3029_TRICKLE_MASK,
460 trickle_set_bits);
461 if (err < 0)
462 dev_dbg(dev, "failed to update trickle charger\n");
463}
464#else
465static inline void rv3029_trickle_config(struct udevice *dev)
466{
467}
468#endif
469
470static int rv3029_probe(struct udevice *dev)
471{
472 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
473 DM_I2C_CHIP_WR_ADDRESS);
474
475 rv3029_trickle_config(dev);
476 return 0;
477}
478
479static const struct rtc_ops rv3029_rtc_ops = {
480 .get = rv3029_rtc_get,
481 .set = rv3029_rtc_set,
482 .read8 = rv3029_rtc_read8,
483 .write8 = rv3029_rtc_write8,
484 .reset = rv3029_rtc_reset,
485};
486
487static const struct udevice_id rv3029_rtc_ids[] = {
488 { .compatible = "mc,rv3029" },
489 { .compatible = "mc,rv3029c2" },
490 { }
491};
492
493U_BOOT_DRIVER(rtc_rv3029) = {
494 .name = "rtc-rv3029",
495 .id = UCLASS_RTC,
496 .probe = rv3029_probe,
497 .of_match = rv3029_rtc_ids,
498 .ops = &rv3029_rtc_ops,
499};