blob: ba418c25daf6b99fa6b692ca9decdba7294010a3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc404aa62015-04-20 12:37:24 -06002/*
3 * Simulate an I2C real time clock
4 *
5 * Copyright (c) 2015 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glassc404aa62015-04-20 12:37:24 -06007 */
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>
Simon Glassc404aa62015-04-20 12:37:24 -060018#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Simon Glassc404aa62015-04-20 12:37:24 -060020#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
Simon Glassc404aa62015-04-20 12:37:24 -060031long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
32 int offset)
33{
Simon Glassfa20e932020-12-03 16:55:20 -070034 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
Simon Glassc404aa62015-04-20 12:37:24 -060035 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;
Heinrich Schuchardtc0d1a002020-12-30 18:07:48 +010041 os_set_time_offset(plat->offset);
Simon Glassc404aa62015-04-20 12:37:24 -060042
43 return old_offset;
44}
45
46long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time)
47{
Simon Glassfa20e932020-12-03 16:55:20 -070048 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
Simon Glassc404aa62015-04-20 12:37:24 -060049 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
58static void reset_time(struct udevice *dev)
59{
Simon Glassfa20e932020-12-03 16:55:20 -070060 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
Simon Glassc404aa62015-04-20 12:37:24 -060061 struct rtc_time now;
62
63 os_localtime(&now);
64 plat->base_time = rtc_mktime(&now);
Heinrich Schuchardtc0d1a002020-12-30 18:07:48 +010065 plat->offset = os_get_time_offset();
Simon Glassc404aa62015-04-20 12:37:24 -060066 plat->use_system_time = true;
67}
68
69static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time)
70{
Simon Glassfa20e932020-12-03 16:55:20 -070071 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
Simon Glassc404aa62015-04-20 12:37:24 -060072 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
Heinrich Schuchardt57da69d2018-07-07 23:39:11 +020082 rtc_to_tm(now + plat->offset, time);
83
84 return 0;
Simon Glassc404aa62015-04-20 12:37:24 -060085}
86
87static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time)
88{
Simon Glassfa20e932020-12-03 16:55:20 -070089 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
Simon Glassc404aa62015-04-20 12:37:24 -060090 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;
Heinrich Schuchardtc0d1a002020-12-30 18:07:48 +0100100 os_set_time_offset(plat->offset);
Simon Glassc404aa62015-04-20 12:37:24 -0600101
102 return 0;
103}
104
105/* Update the current time in the registers */
106static int sandbox_i2c_rtc_prepare_read(struct udevice *emul)
107{
Simon Glassfa20e932020-12-03 16:55:20 -0700108 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
Simon Glassc404aa62015-04-20 12:37:24 -0600109 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
127static int sandbox_i2c_rtc_complete_write(struct udevice *emul)
128{
Simon Glassfa20e932020-12-03 16:55:20 -0700129 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
Simon Glassc404aa62015-04-20 12:37:24 -0600130 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
148static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg,
149 int nmsgs)
150{
Simon Glassfa20e932020-12-03 16:55:20 -0700151 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
Simon Glassc404aa62015-04-20 12:37:24 -0600152 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);
Rasmus Villemoes5b7c51e2020-07-06 22:01:17 +0200183 /* If the reset register was written to, do reset. */
184 if (offset <= REG_RESET && REG_RESET < offset + len)
Simon Glassc404aa62015-04-20 12:37:24 -0600185 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
195struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = {
196 .xfer = sandbox_i2c_rtc_xfer,
197};
198
199static int sandbox_i2c_rtc_bind(struct udevice *dev)
200{
201 reset_time(dev);
202
203 return 0;
204}
205
206static const struct udevice_id sandbox_i2c_rtc_ids[] = {
Simon Glass98af3742021-02-03 06:01:17 -0700207 { .compatible = "sandbox,i2c-rtc-emul" },
Simon Glassc404aa62015-04-20 12:37:24 -0600208 { }
209};
210
211U_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,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700216 .priv_auto = sizeof(struct sandbox_i2c_rtc),
Simon Glass71fa5b42020-12-03 16:55:18 -0700217 .plat_auto = sizeof(struct sandbox_i2c_rtc_plat_data),
Simon Glassc404aa62015-04-20 12:37:24 -0600218 .ops = &sandbox_i2c_rtc_emul_ops,
219};