Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | dd18e5d | 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 | dd18e5d | 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 | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 18 | #include <i2c.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
Simon Glass | dd18e5d | 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 | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 31 | long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time, |
| 32 | int offset) |
| 33 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 34 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 35 | 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 Schuchardt | 43db075 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 41 | os_set_time_offset(plat->offset); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 42 | |
| 43 | return old_offset; |
| 44 | } |
| 45 | |
| 46 | long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time) |
| 47 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 48 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 49 | 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 | |
| 58 | static void reset_time(struct udevice *dev) |
| 59 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 60 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 61 | struct rtc_time now; |
| 62 | |
| 63 | os_localtime(&now); |
| 64 | plat->base_time = rtc_mktime(&now); |
Heinrich Schuchardt | 43db075 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 65 | plat->offset = os_get_time_offset(); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 66 | plat->use_system_time = true; |
| 67 | } |
| 68 | |
| 69 | static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time) |
| 70 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 71 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 72 | 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 Schuchardt | 992c1db | 2018-07-07 23:39:11 +0200 | [diff] [blame] | 82 | rtc_to_tm(now + plat->offset, time); |
| 83 | |
| 84 | return 0; |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time) |
| 88 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 89 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev); |
Simon Glass | dd18e5d | 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 | plat->offset = rtc_mktime(time) - now; |
Heinrich Schuchardt | 43db075 | 2020-12-30 18:07:48 +0100 | [diff] [blame] | 100 | os_set_time_offset(plat->offset); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* Update the current time in the registers */ |
| 106 | static int sandbox_i2c_rtc_prepare_read(struct udevice *emul) |
| 107 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 108 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 109 | 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 | |
| 127 | static int sandbox_i2c_rtc_complete_write(struct udevice *emul) |
| 128 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 129 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 130 | 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 | |
| 148 | static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg, |
| 149 | int nmsgs) |
| 150 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 151 | struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul); |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 152 | 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 Villemoes | a3e3652 | 2020-07-06 22:01:17 +0200 | [diff] [blame] | 183 | /* If the reset register was written to, do reset. */ |
| 184 | if (offset <= REG_RESET && REG_RESET < offset + len) |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 185 | 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 | |
| 195 | struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = { |
| 196 | .xfer = sandbox_i2c_rtc_xfer, |
| 197 | }; |
| 198 | |
| 199 | static int sandbox_i2c_rtc_bind(struct udevice *dev) |
| 200 | { |
| 201 | reset_time(dev); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static const struct udevice_id sandbox_i2c_rtc_ids[] = { |
Simon Glass | c4085d7 | 2021-02-03 06:01:17 -0700 | [diff] [blame] | 207 | { .compatible = "sandbox,i2c-rtc-emul" }, |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 208 | { } |
| 209 | }; |
| 210 | |
| 211 | U_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 Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 216 | .priv_auto = sizeof(struct sandbox_i2c_rtc), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 217 | .plat_auto = sizeof(struct sandbox_i2c_rtc_plat_data), |
Simon Glass | dd18e5d | 2015-04-20 12:37:24 -0600 | [diff] [blame] | 218 | .ops = &sandbox_i2c_rtc_emul_ops, |
| 219 | }; |