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.
26 #define debug_buffer print_buffer
28 #define debug_buffer(x, ...)
31 long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
34 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
37 old_offset = plat->offset;
38 plat->use_system_time = use_system_time;
40 plat->offset = offset;
41 os_set_time_offset(plat->offset);
46 long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time)
48 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
51 old_base_time = plat->base_time;
53 plat->base_time = base_time;
58 static void reset_time(struct udevice *dev)
60 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
64 plat->base_time = rtc_mktime(&now);
65 plat->offset = os_get_time_offset();
66 plat->use_system_time = true;
69 static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time)
71 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
72 struct rtc_time tm_now;
75 if (plat->use_system_time) {
76 os_localtime(&tm_now);
77 now = rtc_mktime(&tm_now);
79 now = plat->base_time;
82 rtc_to_tm(now + plat->offset, time);
87 static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time)
89 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
90 struct rtc_time tm_now;
93 if (plat->use_system_time) {
94 os_localtime(&tm_now);
95 now = rtc_mktime(&tm_now);
97 now = plat->base_time;
99 plat->offset = rtc_mktime(time) - now;
100 os_set_time_offset(plat->offset);
105 /* Update the current time in the registers */
106 static int sandbox_i2c_rtc_prepare_read(struct udevice *emul)
108 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
109 struct rtc_time time;
112 ret = sandbox_i2c_rtc_get(emul, &time);
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;
127 static int sandbox_i2c_rtc_complete_write(struct udevice *emul)
129 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
130 struct rtc_time time;
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];
141 ret = sandbox_i2c_rtc_set(emul, &time);
148 static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg,
151 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
155 debug("\n%s\n", __func__);
156 ret = sandbox_i2c_rtc_prepare_read(emul);
159 for (; nmsgs > 0; nmsgs--, msg++) {
164 debug(" %s: msg->len=%d",
165 msg->flags & I2C_M_RD ? "read" : "write",
167 if (msg->flags & I2C_M_RD) {
168 debug(", offset %x, len %x: ", offset, len);
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) {
176 offset = *ptr++ & (REG_COUNT - 1);
178 debug(", set offset %x: ", offset);
179 debug_buffer(0, msg->buf, 1, msg->len, 0);
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)
188 ret = sandbox_i2c_rtc_complete_write(emul);
195 struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = {
196 .xfer = sandbox_i2c_rtc_xfer,
199 static int sandbox_i2c_rtc_bind(struct udevice *dev)
206 static int sandbox_i2c_rtc_probe(struct udevice *dev)
208 const u8 mac[] = { 0x02, 0x00, 0x11, 0x22, 0x33, 0x48 };
209 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
211 memcpy(&plat->reg[0x40], mac, sizeof(mac));
215 static const struct udevice_id sandbox_i2c_rtc_ids[] = {
216 { .compatible = "sandbox,i2c-rtc-emul" },
220 U_BOOT_DRIVER(sandbox_i2c_rtc_emul) = {
221 .name = "sandbox_i2c_rtc_emul",
222 .id = UCLASS_I2C_EMUL,
223 .of_match = sandbox_i2c_rtc_ids,
224 .bind = sandbox_i2c_rtc_bind,
225 .probe = sandbox_i2c_rtc_probe,
226 .priv_auto = sizeof(struct sandbox_i2c_rtc),
227 .plat_auto = sizeof(struct sandbox_i2c_rtc_plat_data),
228 .ops = &sandbox_i2c_rtc_emul_ops,