1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Simulate an I2C real time clock
4 *
5 * Copyright (c) 2015 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9 /*
10 * This is a test driver. It starts off with the current time of the machine,
11 * but also supports setting the time, using an offset from the current
12 * clock. This driver is only intended for testing, not accurate
13 * time-keeping. It does not change the system time.
14 */
15
16 #include <common.h>
17 #include <dm.h>
18 #include <i2c.h>
19 #include <log.h>
20 #include <os.h>
21 #include <rtc.h>
22 #include <asm/rtc.h>
23 #include <asm/test.h>
24
25 #ifdef DEBUG
26 #define debug_buffer print_buffer
27 #else
28 #define debug_buffer(x, ...)
29 #endif
30
sandbox_i2c_rtc_set_offset(struct udevice * dev,bool use_system_time,int offset)31 long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
32 int offset)
33 {
34 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
35 long old_offset;
36
37 old_offset = plat->offset;
38 plat->use_system_time = use_system_time;
39 if (offset != -1)
40 plat->offset = offset;
41 os_set_time_offset(plat->offset);
42
43 return old_offset;
44 }
45
sandbox_i2c_rtc_get_set_base_time(struct udevice * dev,long base_time)46 long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time)
47 {
48 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
49 long old_base_time;
50
51 old_base_time = plat->base_time;
52 if (base_time != -1)
53 plat->base_time = base_time;
54
55 return old_base_time;
56 }
57
reset_time(struct udevice * dev)58 static void reset_time(struct udevice *dev)
59 {
60 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
61 struct rtc_time now;
62
63 os_localtime(&now);
64 plat->base_time = rtc_mktime(&now);
65 plat->offset = os_get_time_offset();
66 plat->use_system_time = true;
67 }
68
sandbox_i2c_rtc_get(struct udevice * dev,struct rtc_time * time)69 static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time)
70 {
71 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
72 struct rtc_time tm_now;
73 long now;
74
75 if (plat->use_system_time) {
76 os_localtime(&tm_now);
77 now = rtc_mktime(&tm_now);
78 } else {
79 now = plat->base_time;
80 }
81
82 rtc_to_tm(now + plat->offset, time);
83
84 return 0;
85 }
86
sandbox_i2c_rtc_set(struct udevice * dev,const struct rtc_time * time)87 static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time)
88 {
89 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
90 struct rtc_time tm_now;
91 long now;
92
93 if (plat->use_system_time) {
94 os_localtime(&tm_now);
95 now = rtc_mktime(&tm_now);
96 } else {
97 now = plat->base_time;
98 }
99 plat->offset = rtc_mktime(time) - now;
100 os_set_time_offset(plat->offset);
101
102 return 0;
103 }
104
105 /* Update the current time in the registers */
sandbox_i2c_rtc_prepare_read(struct udevice * emul)106 static int sandbox_i2c_rtc_prepare_read(struct udevice *emul)
107 {
108 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
109 struct rtc_time time;
110 int ret;
111
112 ret = sandbox_i2c_rtc_get(emul, &time);
113 if (ret)
114 return ret;
115
116 plat->reg[REG_SEC] = time.tm_sec;
117 plat->reg[REG_MIN] = time.tm_min;
118 plat->reg[REG_HOUR] = time.tm_hour;
119 plat->reg[REG_MDAY] = time.tm_mday;
120 plat->reg[REG_MON] = time.tm_mon;
121 plat->reg[REG_YEAR] = time.tm_year - 1900;
122 plat->reg[REG_WDAY] = time.tm_wday;
123
124 return 0;
125 }
126
sandbox_i2c_rtc_complete_write(struct udevice * emul)127 static int sandbox_i2c_rtc_complete_write(struct udevice *emul)
128 {
129 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
130 struct rtc_time time;
131 int ret;
132
133 time.tm_sec = plat->reg[REG_SEC];
134 time.tm_min = plat->reg[REG_MIN];
135 time.tm_hour = plat->reg[REG_HOUR];
136 time.tm_mday = plat->reg[REG_MDAY];
137 time.tm_mon = plat->reg[REG_MON];
138 time.tm_year = plat->reg[REG_YEAR] + 1900;
139 time.tm_wday = plat->reg[REG_WDAY];
140
141 ret = sandbox_i2c_rtc_set(emul, &time);
142 if (ret)
143 return ret;
144
145 return 0;
146 }
147
sandbox_i2c_rtc_xfer(struct udevice * emul,struct i2c_msg * msg,int nmsgs)148 static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg,
149 int nmsgs)
150 {
151 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
152 uint offset = 0;
153 int ret;
154
155 debug("\n%s\n", __func__);
156 ret = sandbox_i2c_rtc_prepare_read(emul);
157 if (ret)
158 return ret;
159 for (; nmsgs > 0; nmsgs--, msg++) {
160 int len;
161 u8 *ptr;
162
163 len = msg->len;
164 debug(" %s: msg->len=%d",
165 msg->flags & I2C_M_RD ? "read" : "write",
166 msg->len);
167 if (msg->flags & I2C_M_RD) {
168 debug(", offset %x, len %x: ", offset, len);
169
170 /* Read the register */
171 memcpy(msg->buf, plat->reg + offset, len);
172 memset(msg->buf + len, '\xff', msg->len - len);
173 debug_buffer(0, msg->buf, 1, msg->len, 0);
174 } else if (len >= 1) {
175 ptr = msg->buf;
176 offset = *ptr++ & (REG_COUNT - 1);
177 len--;
178 debug(", set offset %x: ", offset);
179 debug_buffer(0, msg->buf, 1, msg->len, 0);
180
181 /* Write the register */
182 memcpy(plat->reg + offset, ptr, len);
183 /* If the reset register was written to, do reset. */
184 if (offset <= REG_RESET && REG_RESET < offset + len)
185 reset_time(emul);
186 }
187 }
188 ret = sandbox_i2c_rtc_complete_write(emul);
189 if (ret)
190 return ret;
191
192 return 0;
193 }
194
195 struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = {
196 .xfer = sandbox_i2c_rtc_xfer,
197 };
198
sandbox_i2c_rtc_bind(struct udevice * dev)199 static int sandbox_i2c_rtc_bind(struct udevice *dev)
200 {
201 reset_time(dev);
202
203 return 0;
204 }
205
206 static const struct udevice_id sandbox_i2c_rtc_ids[] = {
207 { .compatible = "sandbox,i2c-rtc-emul" },
208 { }
209 };
210
211 U_BOOT_DRIVER(sandbox_i2c_rtc_emul) = {
212 .name = "sandbox_i2c_rtc_emul",
213 .id = UCLASS_I2C_EMUL,
214 .of_match = sandbox_i2c_rtc_ids,
215 .bind = sandbox_i2c_rtc_bind,
216 .priv_auto = sizeof(struct sandbox_i2c_rtc),
217 .plat_auto = sizeof(struct sandbox_i2c_rtc_plat_data),
218 .ops = &sandbox_i2c_rtc_emul_ops,
219 };
220