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