1 // SPDX-License-Identifier: GPL-2.0+
3 * Date & Time support for Micro Crystal RV-8803-C7.
5 * based on ds1307.c which is
6 * (C) Copyright 2001, 2002, 2003
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * Keith Outwater, keith_outwater@mvis.com`
9 * Steven Scholz, steven.scholz@imc-berlin.de
20 * RTC register addresses
22 #define RTC_SEC_REG_ADDR 0x00
23 #define RTC_MIN_REG_ADDR 0x01
24 #define RTC_HR_REG_ADDR 0x02
25 #define RTC_DAY_REG_ADDR 0x03
26 #define RTC_DATE_REG_ADDR 0x04
27 #define RTC_MON_REG_ADDR 0x05
28 #define RTC_YR_REG_ADDR 0x06
30 #define RTC_FLAG_REG_ADDR 0x0E
31 #define RTC_FLAG_BIT_V1F BIT(0)
32 #define RTC_FLAG_BIT_V2F BIT(1)
34 #define RTC_CTL_REG_ADDR 0x0F
35 #define RTC_CTL_BIT_RST BIT(0)
37 static int rv8803_rtc_set(struct udevice *dev, const struct rtc_time *tm)
42 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
43 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
44 tm->tm_hour, tm->tm_min, tm->tm_sec);
46 if (tm->tm_year < 2000 || tm->tm_year > 2099)
47 printf("WARNING: year should be between 2000 and 2099!\n");
49 buf[RTC_YR_REG_ADDR] = bin2bcd(tm->tm_year % 100);
50 buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon);
51 buf[RTC_DAY_REG_ADDR] = 1 << (tm->tm_wday & 0x7);
52 buf[RTC_DATE_REG_ADDR] = bin2bcd(tm->tm_mday);
53 buf[RTC_HR_REG_ADDR] = bin2bcd(tm->tm_hour);
54 buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min);
55 buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec);
57 ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
64 static int rv8803_rtc_get(struct udevice *dev, struct rtc_time *tm)
70 flags = dm_i2c_reg_read(dev, RTC_FLAG_REG_ADDR);
73 debug("%s: flags=%Xh\n", __func__, flags);
75 if (flags & RTC_FLAG_BIT_V1F)
76 printf("### Warning: temperature compensation has stopped\n");
78 if (flags & RTC_FLAG_BIT_V2F) {
79 printf("### Warning: Voltage low, data is invalid\n");
83 ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
87 tm->tm_sec = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F);
88 tm->tm_min = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F);
89 tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x3F);
90 tm->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F);
91 tm->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F);
92 tm->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) + 2000;
93 tm->tm_wday = fls(buf[RTC_DAY_REG_ADDR] & 0x7F) - 1;
97 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
98 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
99 tm->tm_hour, tm->tm_min, tm->tm_sec);
104 static int rv8803_rtc_reset(struct udevice *dev)
107 struct rtc_time tmp = {
117 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, RTC_CTL_BIT_RST);
121 /* clear all flags */
122 ret = dm_i2c_reg_write(dev, RTC_FLAG_REG_ADDR, 0);
126 ret = rv8803_rtc_set(dev, &tmp);
131 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, 0);
135 debug("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
136 tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
137 tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
142 static int rv8803_probe(struct udevice *dev)
144 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
145 DM_I2C_CHIP_WR_ADDRESS);
150 static const struct rtc_ops rv8803_rtc_ops = {
151 .get = rv8803_rtc_get,
152 .set = rv8803_rtc_set,
153 .reset = rv8803_rtc_reset,
156 static const struct udevice_id rv8803_rtc_ids[] = {
157 { .compatible = "microcrystal,rv8803", },
161 U_BOOT_DRIVER(rtc_rv8803) = {
162 .name = "rtc-rv8803",
164 .probe = rv8803_probe,
165 .of_match = rv8803_rtc_ids,
166 .ops = &rv8803_rtc_ops,