Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Simulate an I2C real time clock |
| 4 | * |
| 5 | * Copyright (c) 2015 Google, Inc |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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> |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 18 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 20 | #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 Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 31 | /** |
| 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 | */ |
| 39 | struct sandbox_i2c_rtc_plat_data { |
| 40 | long base_time; |
| 41 | long offset; |
| 42 | bool use_system_time; |
| 43 | u8 reg[REG_COUNT]; |
| 44 | }; |
| 45 | |
| 46 | struct sandbox_i2c_rtc { |
| 47 | unsigned int offset_secs; |
| 48 | }; |
| 49 | |
| 50 | long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time, |
| 51 | int offset) |
| 52 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 53 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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; |
Heinrich Schuchardt | c0d1a00 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 60 | os_set_time_offset(plat->offset); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 61 | |
| 62 | return old_offset; |
| 63 | } |
| 64 | |
| 65 | long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time) |
| 66 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 67 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 68 | long old_base_time; |
| 69 | |
| 70 | old_base_time = plat->base_time; |
| 71 | if (base_time != -1) |
| 72 | plat->base_time = base_time; |
| 73 | |
| 74 | return old_base_time; |
| 75 | } |
| 76 | |
| 77 | static void reset_time(struct udevice *dev) |
| 78 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 79 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 80 | struct rtc_time now; |
| 81 | |
| 82 | os_localtime(&now); |
| 83 | plat->base_time = rtc_mktime(&now); |
Heinrich Schuchardt | c0d1a00 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 84 | plat->offset = os_get_time_offset(); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 85 | plat->use_system_time = true; |
| 86 | } |
| 87 | |
| 88 | static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time) |
| 89 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 90 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 91 | struct rtc_time tm_now; |
| 92 | long now; |
| 93 | |
| 94 | if (plat->use_system_time) { |
| 95 | os_localtime(&tm_now); |
| 96 | now = rtc_mktime(&tm_now); |
| 97 | } else { |
| 98 | now = plat->base_time; |
| 99 | } |
| 100 | |
Heinrich Schuchardt | 57da69d | 2018-07-07 23:39:11 +0200 | [diff] [blame] | 101 | rtc_to_tm(now + plat->offset, time); |
| 102 | |
| 103 | return 0; |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time) |
| 107 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 108 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 109 | struct rtc_time tm_now; |
| 110 | long now; |
| 111 | |
| 112 | if (plat->use_system_time) { |
| 113 | os_localtime(&tm_now); |
| 114 | now = rtc_mktime(&tm_now); |
| 115 | } else { |
| 116 | now = plat->base_time; |
| 117 | } |
| 118 | plat->offset = rtc_mktime(time) - now; |
Heinrich Schuchardt | c0d1a00 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 119 | os_set_time_offset(plat->offset); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* Update the current time in the registers */ |
| 125 | static int sandbox_i2c_rtc_prepare_read(struct udevice *emul) |
| 126 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 127 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 128 | struct rtc_time time; |
| 129 | int ret; |
| 130 | |
| 131 | ret = sandbox_i2c_rtc_get(emul, &time); |
| 132 | if (ret) |
| 133 | return ret; |
| 134 | |
| 135 | plat->reg[REG_SEC] = time.tm_sec; |
| 136 | plat->reg[REG_MIN] = time.tm_min; |
| 137 | plat->reg[REG_HOUR] = time.tm_hour; |
| 138 | plat->reg[REG_MDAY] = time.tm_mday; |
| 139 | plat->reg[REG_MON] = time.tm_mon; |
| 140 | plat->reg[REG_YEAR] = time.tm_year - 1900; |
| 141 | plat->reg[REG_WDAY] = time.tm_wday; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int sandbox_i2c_rtc_complete_write(struct udevice *emul) |
| 147 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 148 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 149 | struct rtc_time time; |
| 150 | int ret; |
| 151 | |
| 152 | time.tm_sec = plat->reg[REG_SEC]; |
| 153 | time.tm_min = plat->reg[REG_MIN]; |
| 154 | time.tm_hour = plat->reg[REG_HOUR]; |
| 155 | time.tm_mday = plat->reg[REG_MDAY]; |
| 156 | time.tm_mon = plat->reg[REG_MON]; |
| 157 | time.tm_year = plat->reg[REG_YEAR] + 1900; |
| 158 | time.tm_wday = plat->reg[REG_WDAY]; |
| 159 | |
| 160 | ret = sandbox_i2c_rtc_set(emul, &time); |
| 161 | if (ret) |
| 162 | return ret; |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg, |
| 168 | int nmsgs) |
| 169 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 170 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 171 | uint offset = 0; |
| 172 | int ret; |
| 173 | |
| 174 | debug("\n%s\n", __func__); |
| 175 | ret = sandbox_i2c_rtc_prepare_read(emul); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 179 | int len; |
| 180 | u8 *ptr; |
| 181 | |
| 182 | len = msg->len; |
| 183 | debug(" %s: msg->len=%d", |
| 184 | msg->flags & I2C_M_RD ? "read" : "write", |
| 185 | msg->len); |
| 186 | if (msg->flags & I2C_M_RD) { |
| 187 | debug(", offset %x, len %x: ", offset, len); |
| 188 | |
| 189 | /* Read the register */ |
| 190 | memcpy(msg->buf, plat->reg + offset, len); |
| 191 | memset(msg->buf + len, '\xff', msg->len - len); |
| 192 | debug_buffer(0, msg->buf, 1, msg->len, 0); |
| 193 | } else if (len >= 1) { |
| 194 | ptr = msg->buf; |
| 195 | offset = *ptr++ & (REG_COUNT - 1); |
| 196 | len--; |
| 197 | debug(", set offset %x: ", offset); |
| 198 | debug_buffer(0, msg->buf, 1, msg->len, 0); |
| 199 | |
| 200 | /* Write the register */ |
| 201 | memcpy(plat->reg + offset, ptr, len); |
Rasmus Villemoes | 5b7c51e | 2020-07-06 22:01:17 +0200 | [diff] [blame] | 202 | /* If the reset register was written to, do reset. */ |
| 203 | if (offset <= REG_RESET && REG_RESET < offset + len) |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 204 | reset_time(emul); |
| 205 | } |
| 206 | } |
| 207 | ret = sandbox_i2c_rtc_complete_write(emul); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = { |
| 215 | .xfer = sandbox_i2c_rtc_xfer, |
| 216 | }; |
| 217 | |
| 218 | static int sandbox_i2c_rtc_bind(struct udevice *dev) |
| 219 | { |
| 220 | reset_time(dev); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static const struct udevice_id sandbox_i2c_rtc_ids[] = { |
| 226 | { .compatible = "sandbox,i2c-rtc" }, |
| 227 | { } |
| 228 | }; |
| 229 | |
| 230 | U_BOOT_DRIVER(sandbox_i2c_rtc_emul) = { |
| 231 | .name = "sandbox_i2c_rtc_emul", |
| 232 | .id = UCLASS_I2C_EMUL, |
| 233 | .of_match = sandbox_i2c_rtc_ids, |
| 234 | .bind = sandbox_i2c_rtc_bind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 235 | .priv_auto = sizeof(struct sandbox_i2c_rtc), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 236 | .plat_auto = sizeof(struct sandbox_i2c_rtc_plat_data), |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 237 | .ops = &sandbox_i2c_rtc_emul_ops, |
| 238 | }; |