blob: 87c4320d5f4f8cc07233d5d750ea47161783432d [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>
15#include <rtc.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010017
Philipp Tomsicha73610d2018-11-27 22:53:58 +010018#define RTC_RV3029_PAGE_LEN 7
Heiko Schocher71d19f32011-03-28 09:24:22 +020019
Philipp Tomsicha73610d2018-11-27 22:53:58 +010020/* control section */
21#define RV3029_ONOFF_CTRL 0x00
22#define RV3029_ONOFF_CTRL_WE BIT(0)
23#define RV3029_ONOFF_CTRL_TE BIT(1)
24#define RV3029_ONOFF_CTRL_TAR BIT(2)
25#define RV3029_ONOFF_CTRL_EERE BIT(3)
26#define RV3029_ONOFF_CTRL_SRON BIT(4)
27#define RV3029_ONOFF_CTRL_TD0 BIT(5)
28#define RV3029_ONOFF_CTRL_TD1 BIT(6)
29#define RV3029_ONOFF_CTRL_CLKINT BIT(7)
30#define RV3029_IRQ_CTRL 0x01
31#define RV3029_IRQ_CTRL_AIE BIT(0)
32#define RV3029_IRQ_CTRL_TIE BIT(1)
33#define RV3029_IRQ_CTRL_V1IE BIT(2)
34#define RV3029_IRQ_CTRL_V2IE BIT(3)
35#define RV3029_IRQ_CTRL_SRIE BIT(4)
36#define RV3029_IRQ_FLAGS 0x02
37#define RV3029_IRQ_FLAGS_AF BIT(0)
38#define RV3029_IRQ_FLAGS_TF BIT(1)
39#define RV3029_IRQ_FLAGS_V1IF BIT(2)
40#define RV3029_IRQ_FLAGS_V2IF BIT(3)
41#define RV3029_IRQ_FLAGS_SRF BIT(4)
42#define RV3029_STATUS 0x03
43#define RV3029_STATUS_VLOW1 BIT(2)
44#define RV3029_STATUS_VLOW2 BIT(3)
45#define RV3029_STATUS_SR BIT(4)
46#define RV3029_STATUS_PON BIT(5)
47#define RV3029_STATUS_EEBUSY BIT(7)
48#define RV3029_RST_CTRL 0x04
49#define RV3029_RST_CTRL_SYSR BIT(4)
50#define RV3029_CONTROL_SECTION_LEN 0x05
Heiko Schocher71d19f32011-03-28 09:24:22 +020051
Philipp Tomsicha73610d2018-11-27 22:53:58 +010052/* watch section */
53#define RV3029_W_SEC 0x08
54#define RV3029_W_MINUTES 0x09
55#define RV3029_W_HOURS 0x0A
56#define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
57#define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
58#define RV3029_W_DATE 0x0B
59#define RV3029_W_DAYS 0x0C
60#define RV3029_W_MONTHS 0x0D
61#define RV3029_W_YEARS 0x0E
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010062
Philipp Tomsicha73610d2018-11-27 22:53:58 +010063/* eeprom control section */
64#define RV3029_CONTROL_E2P_EECTRL 0x30
65#define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
66#define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
67#define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
68#define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
69#define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
70 RV3029_TRICKLE_5K |\
71 RV3029_TRICKLE_20K |\
72 RV3029_TRICKLE_80K)
73#define RV3029_TRICKLE_SHIFT 4
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010074
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010075
Philipp Tomsicha73610d2018-11-27 22:53:58 +010076static int rv3029_rtc_get(struct udevice *dev, struct rtc_time *tm)
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010077{
Philipp Tomsicha73610d2018-11-27 22:53:58 +010078 u8 regs[RTC_RV3029_PAGE_LEN];
79 int ret;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010080
Philipp Tomsicha73610d2018-11-27 22:53:58 +010081 ret = dm_i2c_read(dev, RV3029_W_SEC, regs, sizeof(regs));
82 if (ret < 0) {
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010083 printf("%s: error reading RTC: %x\n", __func__, ret);
Philipp Tomsicha73610d2018-11-27 22:53:58 +010084 return -EIO;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010085 }
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +010086
Philipp Tomsicha73610d2018-11-27 22:53:58 +010087 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
88 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
89
90 /* HR field has a more complex interpretation */
91 {
92 const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
93
94 if (_hr & RV3029_REG_HR_12_24) {
95 /* 12h format */
96 tm->tm_hour = bcd2bin(_hr & 0x1f);
97 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
98 tm->tm_hour += 12;
99 } else {
100 /* 24h format */
101 tm->tm_hour = bcd2bin(_hr & 0x3f);
102 }
103 }
104
105 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
106 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100107 /* RTC supports only years > 1999 */
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100108 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 2000;
109 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100110
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100111 tm->tm_yday = 0;
112 tm->tm_isdst = 0;
113
114 debug("%s: %4d-%02d-%02d (wday=%d) %2d:%02d:%02d\n",
115 __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
116 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100117
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100118 return 0;
119}
120
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100121static int rv3029_rtc_set(struct udevice *dev, const struct rtc_time *tm)
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100122{
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100123 u8 regs[RTC_RV3029_PAGE_LEN];
Wolfgang Denkb6337412011-11-04 15:55:13 +0000124
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100125 debug("%s: %4d-%02d-%02d (wday=%d( %2d:%02d:%02d\n",
126 __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
127 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100128
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100129
130 if (tm->tm_year < 2000) {
131 printf("%s: year %d (before 2000) not supported\n",
132 __func__, tm->tm_year);
133 return -EINVAL;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100134 }
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100135
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100136 regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
137 regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
138 regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
139 regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
140 regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
141 regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
142 regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 2000);
143
144 return dm_i2c_write(dev, RV3029_W_SEC, regs, sizeof(regs));
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100145}
146
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100147static int rv3029_rtc_reset(struct udevice *dev)
Heiko Schocher71d19f32011-03-28 09:24:22 +0200148{
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100149 u8 ctrl = RV3029_RST_CTRL_SYSR;
150 unsigned long start;
151 const unsigned long timeout_ms = 10000;
152 int ret;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200153
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100154 /* trigger the system-reset */
155 ret = dm_i2c_write(dev, RV3029_RST_CTRL, &ctrl, 1);
156 if (ret < 0)
157 return -EIO;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200158
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100159 /* wait for the system-reset to complete */
160 start = get_timer(0);
161 do {
162 if (get_timer(start) > timeout_ms)
163 return -ETIMEDOUT;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200164
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100165 ret = dm_i2c_read(dev, RV3029_RST_CTRL, &ctrl, 1);
166 if (ret < 0)
167 return -EIO;
168 } while (ctrl & RV3029_RST_CTRL_SYSR);
169
170 return 0;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200171}
172
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100173static int rv3029_rtc_read8(struct udevice *dev, unsigned int reg)
Heiko Schocher71d19f32011-03-28 09:24:22 +0200174{
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100175 u8 data;
176 int ret;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200177
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100178 ret = dm_i2c_read(dev, reg, &data, sizeof(data));
179 return ret < 0 ? ret : data;
180}
Heiko Schocher71d19f32011-03-28 09:24:22 +0200181
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100182static int rv3029_rtc_write8(struct udevice *dev, unsigned int reg, int val)
183{
184 u8 data = val;
185
186 return dm_i2c_write(dev, reg, &data, 1);
187}
188
189#if defined(OF_CONTROL)
190static int rv3029_get_sr(struct udevice *dev, u8 *buf)
191{
192 int ret = dm_i2c_read(dev, RV3029_STATUS, buf, 1);
193
194 if (ret < 0)
195 return -EIO;
196
197 dev_dbg(dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
198 return 0;
199}
200
201static int rv3029_set_sr(struct udevice *dev, u8 val)
202{
203 int ret;
204
205 ret = dm_i2c_read(dev, RV3029_STATUS, &val, 1);
206 if (ret < 0)
207 return -EIO;
208
209 dev_dbg(dev, "status = 0x%.2x (%d)\n", val, val);
210 return 0;
211}
212
213static int rv3029_eeprom_busywait(struct udevice *dev)
214{
215 int i, ret;
216 u8 sr;
217
218 for (i = 100; i > 0; i--) {
219 ret = rv3029_get_sr(dev, &sr);
220 if (ret < 0)
221 break;
222 if (!(sr & RV3029_STATUS_EEBUSY))
Heiko Schocher71d19f32011-03-28 09:24:22 +0200223 break;
224 udelay(10000);
225 }
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100226 if (i <= 0) {
227 dev_err(dev, "EEPROM busy wait timeout.\n");
228 return -ETIMEDOUT;
Heiko Schocher71d19f32011-03-28 09:24:22 +0200229 }
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100230
231 return ret;
Heiko Schocher5c4fa2e2011-01-12 08:20:05 +0100232}
Philipp Tomsicha73610d2018-11-27 22:53:58 +0100233
234static int rv3029_update_bits(struct udevice *dev, u8 reg, u8 mask, u8 set)
235{
236 u8 buf;
237 int ret;
238
239 ret = dm_i2c_read(dev, reg, &buf, 1);
240 if (ret < 0)
241 return ret;
242
243 if ((buf & mask) == (set && mask))
244 return 0;
245
246 buf = (buf & ~mask) | (set & mask);
247 ret = dm_i2c_read(dev, reg, &buf, 1);
248 if (ret < 0)
249 return ret;
250
251 return 0;
252}
253
254static int rv3029_eeprom_exit(struct udevice *dev)
255{
256 /* Re-enable eeprom refresh */
257 return rv3029_update_bits(dev, RV3029_ONOFF_CTRL,
258 RV3029_ONOFF_CTRL_EERE,
259 RV3029_ONOFF_CTRL_EERE);
260}
261
262static int rv3029_eeprom_enter(struct udevice *dev)
263{
264 int ret;
265 u8 sr;
266
267 /* Check whether we are in the allowed voltage range. */
268 ret = rv3029_get_sr(dev, &sr);
269 if (ret < 0)
270 return ret;
271 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
272 /* We clear the bits and retry once just in case
273 * we had a brown out in early startup.
274 */
275 sr &= ~RV3029_STATUS_VLOW1;
276 sr &= ~RV3029_STATUS_VLOW2;
277 ret = rv3029_set_sr(dev, sr);
278 if (ret < 0)
279 return ret;
280 udelay(10000);
281 ret = rv3029_get_sr(dev, &sr);
282 if (ret < 0)
283 return ret;
284 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
285 dev_err(dev, "Supply voltage is too low to safely access the EEPROM.\n");
286 return -ENODEV;
287 }
288 }
289
290 /* Disable eeprom refresh. */
291 ret = rv3029_update_bits(dev,
292 RV3029_ONOFF_CTRL, RV3029_ONOFF_CTRL_EERE, 0);
293 if (ret < 0)
294 return ret;
295
296 /* Wait for any previous eeprom accesses to finish. */
297 ret = rv3029_eeprom_busywait(dev);
298 if (ret < 0)
299 rv3029_eeprom_exit(dev);
300
301 return ret;
302}
303
304static int rv3029_eeprom_read(struct udevice *dev, u8 reg,
305 u8 buf[], size_t len)
306{
307 int ret, err;
308
309 err = rv3029_eeprom_enter(dev);
310 if (err < 0)
311 return err;
312
313 ret = dm_i2c_read(dev, reg, buf, len);
314
315 err = rv3029_eeprom_exit(dev);
316 if (err < 0)
317 return err;
318
319 return ret;
320}
321
322static int rv3029_eeprom_write(struct udevice *dev, u8 reg,
323 u8 const buf[], size_t len)
324{
325 int ret;
326 size_t i;
327 u8 tmp;
328
329 ret = rv3029_eeprom_enter(dev);
330 if (ret < 0)
331 return ret;
332
333 for (i = 0; i < len; i++, reg++) {
334 ret = dm_i2c_read(dev, reg, &tmp, 1);
335 if (ret < 0)
336 break;
337 if (tmp != buf[i]) {
338 ret = dm_i2c_write(dev, reg, &buf[i], 1);
339 if (ret < 0)
340 break;
341 }
342 ret = rv3029_eeprom_busywait(dev);
343 if (ret < 0)
344 break;
345 }
346
347 ret = rv3029_eeprom_exit(dev);
348 if (ret < 0)
349 return ret;
350
351 return 0;
352}
353
354static int rv3029_eeprom_update_bits(struct udevice *dev,
355 u8 reg, u8 mask, u8 set)
356{
357 u8 buf;
358 int ret;
359
360 ret = rv3029_eeprom_read(dev, reg, &buf, 1);
361 if (ret < 0)
362 return ret;
363
364 /*
365 * If the EEPROM already reads the correct bitpattern, we don't need
366 * to update it.
367 */
368 if ((buf & mask) == (set & mask))
369 return 0;
370
371 buf = (buf & ~mask) | (set & mask);
372 ret = rv3029_eeprom_write(dev, reg, &buf, 1);
373 if (ret < 0)
374 return ret;
375
376 return 0;
377}
378
379static void rv3029_trickle_config(struct udevice *dev)
380{
381 static const struct rv3029_trickle_tab_elem {
382 u32 r; /* resistance in ohms */
383 u8 conf; /* trickle config bits */
384 } rv3029_trickle_tab[] = {
385 {
386 .r = 1076,
387 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
388 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
389 }, {
390 .r = 1091,
391 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
392 RV3029_TRICKLE_20K,
393 }, {
394 .r = 1137,
395 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
396 RV3029_TRICKLE_80K,
397 }, {
398 .r = 1154,
399 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
400 }, {
401 .r = 1371,
402 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
403 RV3029_TRICKLE_80K,
404 }, {
405 .r = 1395,
406 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
407 }, {
408 .r = 1472,
409 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
410 }, {
411 .r = 1500,
412 .conf = RV3029_TRICKLE_1K,
413 }, {
414 .r = 3810,
415 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
416 RV3029_TRICKLE_80K,
417 }, {
418 .r = 4000,
419 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
420 }, {
421 .r = 4706,
422 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
423 }, {
424 .r = 5000,
425 .conf = RV3029_TRICKLE_5K,
426 }, {
427 .r = 16000,
428 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
429 }, {
430 .r = 20000,
431 .conf = RV3029_TRICKLE_20K,
432 }, {
433 .r = 80000,
434 .conf = RV3029_TRICKLE_80K,
435 },
436 };
437 int err;
438 u32 ohms;
439 u8 trickle_set_bits = 0;
440
441 /* Configure the trickle charger. */
442 err = dev_read_u32(dev, "trickle-resistor-ohms", &ohms);
443
444 if (!err) {
445 /* Find trickle-charger config */
446 for (int i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++)
447 if (rv3029_trickle_tab[i].r >= ohms) {
448 dev_dbg(dev, "trickle charger at %d ohms\n",
449 rv3029_trickle_tab[i].r);
450 trickle_set_bits = rv3029_trickle_tab[i].conf;
451 break;
452 }
453 }
454
455 dev_dbg(dev, "trickle charger config 0x%x\n", trickle_set_bits);
456 err = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL,
457 RV3029_TRICKLE_MASK,
458 trickle_set_bits);
459 if (err < 0)
460 dev_dbg(dev, "failed to update trickle charger\n");
461}
462#else
463static inline void rv3029_trickle_config(struct udevice *dev)
464{
465}
466#endif
467
468static int rv3029_probe(struct udevice *dev)
469{
470 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
471 DM_I2C_CHIP_WR_ADDRESS);
472
473 rv3029_trickle_config(dev);
474 return 0;
475}
476
477static const struct rtc_ops rv3029_rtc_ops = {
478 .get = rv3029_rtc_get,
479 .set = rv3029_rtc_set,
480 .read8 = rv3029_rtc_read8,
481 .write8 = rv3029_rtc_write8,
482 .reset = rv3029_rtc_reset,
483};
484
485static const struct udevice_id rv3029_rtc_ids[] = {
486 { .compatible = "mc,rv3029" },
487 { .compatible = "mc,rv3029c2" },
488 { }
489};
490
491U_BOOT_DRIVER(rtc_rv3029) = {
492 .name = "rtc-rv3029",
493 .id = UCLASS_RTC,
494 .probe = rv3029_probe,
495 .of_match = rv3029_rtc_ids,
496 .ops = &rv3029_rtc_ops,
497};