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; |
| 60 | |
| 61 | return old_offset; |
| 62 | } |
| 63 | |
| 64 | long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time) |
| 65 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 66 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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 | |
| 76 | static void reset_time(struct udevice *dev) |
| 77 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 78 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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 | |
| 87 | static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time) |
| 88 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 89 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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 Schuchardt | 57da69d | 2018-07-07 23:39:11 +0200 | [diff] [blame] | 100 | rtc_to_tm(now + plat->offset, time); |
| 101 | |
| 102 | return 0; |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time) |
| 106 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 107 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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 */ |
| 123 | static int sandbox_i2c_rtc_prepare_read(struct udevice *emul) |
| 124 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 125 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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 | |
| 144 | static int sandbox_i2c_rtc_complete_write(struct udevice *emul) |
| 145 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 146 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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 | |
| 165 | static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg, |
| 166 | int nmsgs) |
| 167 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 168 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 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 Villemoes | 5b7c51e | 2020-07-06 22:01:17 +0200 | [diff] [blame] | 200 | /* If the reset register was written to, do reset. */ |
| 201 | if (offset <= REG_RESET && REG_RESET < offset + len) |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 202 | 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 | |
| 212 | struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = { |
| 213 | .xfer = sandbox_i2c_rtc_xfer, |
| 214 | }; |
| 215 | |
| 216 | static int sandbox_i2c_rtc_bind(struct udevice *dev) |
| 217 | { |
| 218 | reset_time(dev); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static const struct udevice_id sandbox_i2c_rtc_ids[] = { |
| 224 | { .compatible = "sandbox,i2c-rtc" }, |
| 225 | { } |
| 226 | }; |
| 227 | |
| 228 | U_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, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 233 | .priv_auto = sizeof(struct sandbox_i2c_rtc), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 234 | .plat_auto = sizeof(struct sandbox_i2c_rtc_plat_data), |
Simon Glass | c404aa6 | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 235 | .ops = &sandbox_i2c_rtc_emul_ops, |
| 236 | }; |