blob: 7f78ff83cb07760fa6ceb773f75a9e02729a8159 [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 -060031/**
32 * struct sandbox_i2c_rtc_plat_data - platform data for the RTC
33 *
34 * @base_time: Base system time when RTC device was bound
35 * @offset: RTC offset from current system time
36 * @use_system_time: true to use system time, false to use @base_time
37 * @reg: Register values
38 */
39struct sandbox_i2c_rtc_plat_data {
40 long base_time;
41 long offset;
42 bool use_system_time;
43 u8 reg[REG_COUNT];
44};
45
46struct sandbox_i2c_rtc {
47 unsigned int offset_secs;
48};
49
50long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
51 int offset)
52{
53 struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
54 long old_offset;
55
56 old_offset = plat->offset;
57 plat->use_system_time = use_system_time;
58 if (offset != -1)
59 plat->offset = offset;
60
61 return old_offset;
62}
63
64long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time)
65{
66 struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
67 long old_base_time;
68
69 old_base_time = plat->base_time;
70 if (base_time != -1)
71 plat->base_time = base_time;
72
73 return old_base_time;
74}
75
76static void reset_time(struct udevice *dev)
77{
78 struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
79 struct rtc_time now;
80
81 os_localtime(&now);
82 plat->base_time = rtc_mktime(&now);
83 plat->offset = 0;
84 plat->use_system_time = true;
85}
86
87static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time)
88{
89 struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(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
Heinrich Schuchardt57da69d2018-07-07 23:39:11 +0200100 rtc_to_tm(now + plat->offset, time);
101
102 return 0;
Simon Glassc404aa62015-04-20 12:37:24 -0600103}
104
105static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time)
106{
107 struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
108 struct rtc_time tm_now;
109 long now;
110
111 if (plat->use_system_time) {
112 os_localtime(&tm_now);
113 now = rtc_mktime(&tm_now);
114 } else {
115 now = plat->base_time;
116 }
117 plat->offset = rtc_mktime(time) - now;
118
119 return 0;
120}
121
122/* Update the current time in the registers */
123static int sandbox_i2c_rtc_prepare_read(struct udevice *emul)
124{
125 struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(emul);
126 struct rtc_time time;
127 int ret;
128
129 ret = sandbox_i2c_rtc_get(emul, &time);
130 if (ret)
131 return ret;
132
133 plat->reg[REG_SEC] = time.tm_sec;
134 plat->reg[REG_MIN] = time.tm_min;
135 plat->reg[REG_HOUR] = time.tm_hour;
136 plat->reg[REG_MDAY] = time.tm_mday;
137 plat->reg[REG_MON] = time.tm_mon;
138 plat->reg[REG_YEAR] = time.tm_year - 1900;
139 plat->reg[REG_WDAY] = time.tm_wday;
140
141 return 0;
142}
143
144static int sandbox_i2c_rtc_complete_write(struct udevice *emul)
145{
146 struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(emul);
147 struct rtc_time time;
148 int ret;
149
150 time.tm_sec = plat->reg[REG_SEC];
151 time.tm_min = plat->reg[REG_MIN];
152 time.tm_hour = plat->reg[REG_HOUR];
153 time.tm_mday = plat->reg[REG_MDAY];
154 time.tm_mon = plat->reg[REG_MON];
155 time.tm_year = plat->reg[REG_YEAR] + 1900;
156 time.tm_wday = plat->reg[REG_WDAY];
157
158 ret = sandbox_i2c_rtc_set(emul, &time);
159 if (ret)
160 return ret;
161
162 return 0;
163}
164
165static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg,
166 int nmsgs)
167{
168 struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(emul);
169 uint offset = 0;
170 int ret;
171
172 debug("\n%s\n", __func__);
173 ret = sandbox_i2c_rtc_prepare_read(emul);
174 if (ret)
175 return ret;
176 for (; nmsgs > 0; nmsgs--, msg++) {
177 int len;
178 u8 *ptr;
179
180 len = msg->len;
181 debug(" %s: msg->len=%d",
182 msg->flags & I2C_M_RD ? "read" : "write",
183 msg->len);
184 if (msg->flags & I2C_M_RD) {
185 debug(", offset %x, len %x: ", offset, len);
186
187 /* Read the register */
188 memcpy(msg->buf, plat->reg + offset, len);
189 memset(msg->buf + len, '\xff', msg->len - len);
190 debug_buffer(0, msg->buf, 1, msg->len, 0);
191 } else if (len >= 1) {
192 ptr = msg->buf;
193 offset = *ptr++ & (REG_COUNT - 1);
194 len--;
195 debug(", set offset %x: ", offset);
196 debug_buffer(0, msg->buf, 1, msg->len, 0);
197
198 /* Write the register */
199 memcpy(plat->reg + offset, ptr, len);
Rasmus Villemoes5b7c51e2020-07-06 22:01:17 +0200200 /* If the reset register was written to, do reset. */
201 if (offset <= REG_RESET && REG_RESET < offset + len)
Simon Glassc404aa62015-04-20 12:37:24 -0600202 reset_time(emul);
203 }
204 }
205 ret = sandbox_i2c_rtc_complete_write(emul);
206 if (ret)
207 return ret;
208
209 return 0;
210}
211
212struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = {
213 .xfer = sandbox_i2c_rtc_xfer,
214};
215
216static int sandbox_i2c_rtc_bind(struct udevice *dev)
217{
218 reset_time(dev);
219
220 return 0;
221}
222
223static const struct udevice_id sandbox_i2c_rtc_ids[] = {
224 { .compatible = "sandbox,i2c-rtc" },
225 { }
226};
227
228U_BOOT_DRIVER(sandbox_i2c_rtc_emul) = {
229 .name = "sandbox_i2c_rtc_emul",
230 .id = UCLASS_I2C_EMUL,
231 .of_match = sandbox_i2c_rtc_ids,
232 .bind = sandbox_i2c_rtc_bind,
233 .priv_auto_alloc_size = sizeof(struct sandbox_i2c_rtc),
234 .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_rtc_plat_data),
235 .ops = &sandbox_i2c_rtc_emul_ops,
236};