1 // SPDX-License-Identifier: GPL-2.0+
3 * Simulate an I2C real time clock
5 * Copyright (c) 2015 Google, Inc
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.
25 #define debug_buffer print_buffer
27 #define debug_buffer(x, ...)
30 long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
33 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
36 old_offset = plat->offset;
37 plat->use_system_time = use_system_time;
39 plat->offset = offset;
40 os_set_time_offset(plat->offset);
45 long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time)
47 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
50 old_base_time = plat->base_time;
52 plat->base_time = base_time;
57 static void reset_time(struct udevice *dev)
59 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
63 plat->base_time = rtc_mktime(&now);
64 plat->offset = os_get_time_offset();
65 plat->use_system_time = true;
68 static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time)
70 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
71 struct rtc_time tm_now;
74 if (plat->use_system_time) {
75 os_localtime(&tm_now);
76 now = rtc_mktime(&tm_now);
78 now = plat->base_time;
81 rtc_to_tm(now + plat->offset, time);
86 static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time)
88 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
89 struct rtc_time tm_now;
92 if (plat->use_system_time) {
93 os_localtime(&tm_now);
94 now = rtc_mktime(&tm_now);
96 now = plat->base_time;
98 plat->offset = rtc_mktime(time) - now;
99 os_set_time_offset(plat->offset);
104 /* Update the current time in the registers */
105 static int sandbox_i2c_rtc_prepare_read(struct udevice *emul)
107 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
108 struct rtc_time time;
111 ret = sandbox_i2c_rtc_get(emul, &time);
115 plat->reg[REG_SEC] = time.tm_sec;
116 plat->reg[REG_MIN] = time.tm_min;
117 plat->reg[REG_HOUR] = time.tm_hour;
118 plat->reg[REG_MDAY] = time.tm_mday;
119 plat->reg[REG_MON] = time.tm_mon;
120 plat->reg[REG_YEAR] = time.tm_year - 1900;
121 plat->reg[REG_WDAY] = time.tm_wday;
126 static int sandbox_i2c_rtc_complete_write(struct udevice *emul)
128 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
129 struct rtc_time time;
132 time.tm_sec = plat->reg[REG_SEC];
133 time.tm_min = plat->reg[REG_MIN];
134 time.tm_hour = plat->reg[REG_HOUR];
135 time.tm_mday = plat->reg[REG_MDAY];
136 time.tm_mon = plat->reg[REG_MON];
137 time.tm_year = plat->reg[REG_YEAR] + 1900;
138 time.tm_wday = plat->reg[REG_WDAY];
140 ret = sandbox_i2c_rtc_set(emul, &time);
147 static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg,
150 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
154 debug("\n%s\n", __func__);
155 ret = sandbox_i2c_rtc_prepare_read(emul);
158 for (; nmsgs > 0; nmsgs--, msg++) {
163 debug(" %s: msg->len=%d",
164 msg->flags & I2C_M_RD ? "read" : "write",
166 if (msg->flags & I2C_M_RD) {
167 debug(", offset %x, len %x: ", offset, len);
169 /* Read the register */
170 memcpy(msg->buf, plat->reg + offset, len);
171 memset(msg->buf + len, '\xff', msg->len - len);
172 debug_buffer(0, msg->buf, 1, msg->len, 0);
173 } else if (len >= 1) {
175 offset = *ptr++ & (REG_COUNT - 1);
177 debug(", set offset %x: ", offset);
178 debug_buffer(0, msg->buf, 1, msg->len, 0);
180 /* Write the register */
181 memcpy(plat->reg + offset, ptr, len);
182 /* If the reset register was written to, do reset. */
183 if (offset <= REG_RESET && REG_RESET < offset + len)
187 ret = sandbox_i2c_rtc_complete_write(emul);
194 struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = {
195 .xfer = sandbox_i2c_rtc_xfer,
198 static int sandbox_i2c_rtc_bind(struct udevice *dev)
205 static int sandbox_i2c_rtc_probe(struct udevice *dev)
207 const u8 mac[] = { 0x02, 0x00, 0x11, 0x22, 0x33, 0x48 };
208 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
210 memcpy(&plat->reg[0x40], mac, sizeof(mac));
214 static const struct udevice_id sandbox_i2c_rtc_ids[] = {
215 { .compatible = "sandbox,i2c-rtc-emul" },
219 U_BOOT_DRIVER(sandbox_i2c_rtc_emul) = {
220 .name = "sandbox_i2c_rtc_emul",
221 .id = UCLASS_I2C_EMUL,
222 .of_match = sandbox_i2c_rtc_ids,
223 .bind = sandbox_i2c_rtc_bind,
224 .probe = sandbox_i2c_rtc_probe,
225 .priv_auto = sizeof(struct sandbox_i2c_rtc),
226 .plat_auto = sizeof(struct sandbox_i2c_rtc_plat_data),
227 .ops = &sandbox_i2c_rtc_emul_ops,