blob: 2ab40f08336f2dd9879f1b1f45c531dc508a0d83 [file] [log] [blame]
Michael Walle2febcea2019-05-29 01:29:58 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Date & Time support for Micro Crystal RV-8803-C7.
4 *
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
10 *
11 */
12
13#include <common.h>
14#include <command.h>
15#include <dm.h>
16#include <rtc.h>
17#include <i2c.h>
18
19/*
20 * RTC register addresses
21 */
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
29
30#define RTC_FLAG_REG_ADDR 0x0E
31#define RTC_FLAG_BIT_V1F BIT(0)
32#define RTC_FLAG_BIT_V2F BIT(1)
33
34#define RTC_CTL_REG_ADDR 0x0F
35#define RTC_CTL_BIT_RST BIT(0)
36
37static int rv8803_rtc_set(struct udevice *dev, const struct rtc_time *tm)
38{
39 int ret;
40 u8 buf[7];
41
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);
45
46 if (tm->tm_year < 2000 || tm->tm_year > 2099)
47 printf("WARNING: year should be between 2000 and 2099!\n");
48
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);
56
57 ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
58 if (ret < 0)
59 return ret;
60
61 return 0;
62}
63
64static int rv8803_rtc_get(struct udevice *dev, struct rtc_time *tm)
65{
66 int ret;
67 u8 buf[7];
68 int flags;
69
70 flags = dm_i2c_reg_read(dev, RTC_FLAG_REG_ADDR);
71 if (flags < 0)
72 return flags;
73 debug("%s: flags=%Xh\n", __func__, flags);
74
75 if (flags & RTC_FLAG_BIT_V1F)
76 printf("### Warning: temperature compensation has stopped\n");
77
78 if (flags & RTC_FLAG_BIT_V2F) {
79 printf("### Warning: Voltage low, data is invalid\n");
80 return -1;
81 }
82
83 ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
84 if (ret < 0)
85 return ret;
86
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;
94 tm->tm_yday = 0;
95 tm->tm_isdst = 0;
96
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);
100
101 return 0;
102}
103
104static int rv8803_rtc_reset(struct udevice *dev)
105{
106 int ret;
107 struct rtc_time tmp = {
108 .tm_year = 2000,
109 .tm_mon = 1,
110 .tm_mday = 1,
111 .tm_hour = 0,
112 .tm_min = 0,
113 .tm_sec = 0,
114 };
115
116 /* assert reset */
117 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, RTC_CTL_BIT_RST);
118 if (ret < 0)
119 return ret;
120
121 /* clear all flags */
122 ret = dm_i2c_reg_write(dev, RTC_FLAG_REG_ADDR, 0);
123 if (ret < 0)
124 return ret;
125
126 ret = rv8803_rtc_set(dev, &tmp);
127 if (ret < 0)
128 return ret;
129
130 /* clear reset */
131 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, 0);
132 if (ret < 0)
133 return ret;
134
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);
138
139 return 0;
140}
141
142static int rv8803_probe(struct udevice *dev)
143{
144 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
145 DM_I2C_CHIP_WR_ADDRESS);
146
147 return 0;
148}
149
150static const struct rtc_ops rv8803_rtc_ops = {
151 .get = rv8803_rtc_get,
152 .set = rv8803_rtc_set,
153 .reset = rv8803_rtc_reset,
154};
155
156static const struct udevice_id rv8803_rtc_ids[] = {
157 { .compatible = "microcrystal,rv8803", },
158 { }
159};
160
161U_BOOT_DRIVER(rtc_rv8803) = {
162 .name = "rtc-rv8803",
163 .id = UCLASS_RTC,
164 .probe = rv8803_probe,
165 .of_match = rv8803_rtc_ids,
166 .ops = &rv8803_rtc_ops,
167};