Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Simulate an I2C eeprom |
| 4 | * |
| 5 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 07a3b23 | 2015-05-04 11:31:08 -0600 | [diff] [blame] | 9 | #include <errno.h> |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 10 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <asm/test.h> |
| 14 | |
| 15 | #ifdef DEBUG |
| 16 | #define debug_buffer print_buffer |
| 17 | #else |
| 18 | #define debug_buffer(x, ...) |
| 19 | #endif |
| 20 | |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 21 | struct sandbox_i2c_flash_plat_data { |
| 22 | enum sandbox_i2c_eeprom_test_mode test_mode; |
| 23 | const char *filename; |
| 24 | int offset_len; /* Length of an offset in bytes */ |
| 25 | int size; /* Size of data buffer */ |
Robert Beckett | f695f6e | 2019-10-28 17:44:59 +0000 | [diff] [blame] | 26 | uint chip_addr_offset_mask; /* mask of addr bits used for offset */ |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | struct sandbox_i2c_flash { |
| 30 | uint8_t *data; |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame] | 31 | uint prev_addr; /* slave address of previous access */ |
| 32 | uint prev_offset; /* offset of previous access */ |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev, |
| 36 | enum sandbox_i2c_eeprom_test_mode mode) |
| 37 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 38 | struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 39 | |
| 40 | plat->test_mode = mode; |
| 41 | } |
| 42 | |
| 43 | void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len) |
| 44 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 45 | struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 46 | |
| 47 | plat->offset_len = offset_len; |
| 48 | } |
| 49 | |
Robert Beckett | f695f6e | 2019-10-28 17:44:59 +0000 | [diff] [blame] | 50 | void sandbox_i2c_eeprom_set_chip_addr_offset_mask(struct udevice *dev, |
| 51 | uint mask) |
| 52 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 53 | struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev); |
Robert Beckett | f695f6e | 2019-10-28 17:44:59 +0000 | [diff] [blame] | 54 | |
| 55 | plat->chip_addr_offset_mask = mask; |
| 56 | } |
| 57 | |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame] | 58 | uint sanbox_i2c_eeprom_get_prev_addr(struct udevice *dev) |
| 59 | { |
| 60 | struct sandbox_i2c_flash *priv = dev_get_priv(dev); |
| 61 | |
| 62 | return priv->prev_addr; |
| 63 | } |
| 64 | |
| 65 | uint sanbox_i2c_eeprom_get_prev_offset(struct udevice *dev) |
| 66 | { |
| 67 | struct sandbox_i2c_flash *priv = dev_get_priv(dev); |
| 68 | |
| 69 | return priv->prev_offset; |
| 70 | } |
| 71 | |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 72 | static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg, |
| 73 | int nmsgs) |
| 74 | { |
| 75 | struct sandbox_i2c_flash *priv = dev_get_priv(emul); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 76 | struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(emul); |
Robert Beckett | f695f6e | 2019-10-28 17:44:59 +0000 | [diff] [blame] | 77 | uint offset = msg->addr & plat->chip_addr_offset_mask; |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 78 | |
| 79 | debug("\n%s\n", __func__); |
| 80 | debug_buffer(0, priv->data, 1, 16, 0); |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame] | 81 | |
| 82 | /* store addr for testing visibity */ |
| 83 | priv->prev_addr = msg->addr; |
| 84 | |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 85 | for (; nmsgs > 0; nmsgs--, msg++) { |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 86 | int len; |
| 87 | u8 *ptr; |
| 88 | |
| 89 | if (!plat->size) |
| 90 | return -ENODEV; |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 91 | len = msg->len; |
Robert Beckett | f695f6e | 2019-10-28 17:44:59 +0000 | [diff] [blame] | 92 | debug(" %s: msg->addr=%x msg->len=%d", |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 93 | msg->flags & I2C_M_RD ? "read" : "write", |
Robert Beckett | f695f6e | 2019-10-28 17:44:59 +0000 | [diff] [blame] | 94 | msg->addr, msg->len); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 95 | if (msg->flags & I2C_M_RD) { |
| 96 | if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE) |
| 97 | len = 1; |
| 98 | debug(", offset %x, len %x: ", offset, len); |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame] | 99 | if (offset + len > plat->size) { |
| 100 | int overflow = offset + len - plat->size; |
| 101 | int initial = len - overflow; |
| 102 | |
| 103 | memcpy(msg->buf, priv->data + offset, initial); |
| 104 | memcpy(msg->buf + initial, priv->data, |
| 105 | overflow); |
| 106 | } else { |
| 107 | memcpy(msg->buf, priv->data + offset, len); |
| 108 | } |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 109 | memset(msg->buf + len, '\xff', msg->len - len); |
| 110 | debug_buffer(0, msg->buf, 1, msg->len, 0); |
| 111 | } else if (len >= plat->offset_len) { |
| 112 | int i; |
| 113 | |
| 114 | ptr = msg->buf; |
| 115 | for (i = 0; i < plat->offset_len; i++, len--) |
| 116 | offset = (offset << 8) | *ptr++; |
| 117 | debug(", set offset %x: ", offset); |
| 118 | debug_buffer(0, msg->buf, 1, msg->len, 0); |
| 119 | if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE) |
| 120 | len = min(len, 1); |
| 121 | |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame] | 122 | /* store offset for testing visibility */ |
| 123 | priv->prev_offset = offset; |
| 124 | |
| 125 | /* For testing, map offsets into our limited buffer. |
| 126 | * offset wraps every 256 bytes |
| 127 | */ |
| 128 | offset &= 0xff; |
| 129 | debug("mapped offset to %x\n", offset); |
| 130 | |
| 131 | if (offset + len > plat->size) { |
| 132 | int overflow = offset + len - plat->size; |
| 133 | int initial = len - overflow; |
| 134 | |
| 135 | memcpy(priv->data + offset, ptr, initial); |
| 136 | memcpy(priv->data, ptr + initial, overflow); |
| 137 | } else { |
| 138 | memcpy(priv->data + offset, ptr, len); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 139 | } |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | debug_buffer(0, priv->data, 1, 16, 0); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | struct dm_i2c_ops sandbox_i2c_emul_ops = { |
| 148 | .xfer = sandbox_i2c_eeprom_xfer, |
| 149 | }; |
| 150 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 151 | static int sandbox_i2c_eeprom_of_to_plat(struct udevice *dev) |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 152 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 153 | struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 154 | |
Simon Glass | 41f4f34 | 2017-05-18 20:09:52 -0600 | [diff] [blame] | 155 | plat->size = dev_read_u32_default(dev, "sandbox,size", 32); |
| 156 | plat->filename = dev_read_string(dev, "sandbox,filename"); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 157 | if (!plat->filename) { |
| 158 | debug("%s: No filename for device '%s'\n", __func__, |
| 159 | dev->name); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | plat->test_mode = SIE_TEST_MODE_NONE; |
| 163 | plat->offset_len = 1; |
Robert Beckett | f695f6e | 2019-10-28 17:44:59 +0000 | [diff] [blame] | 164 | plat->chip_addr_offset_mask = 0; |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int sandbox_i2c_eeprom_probe(struct udevice *dev) |
| 170 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 171 | struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 172 | struct sandbox_i2c_flash *priv = dev_get_priv(dev); |
Sean Anderson | e2dc0e6 | 2022-05-05 13:11:42 -0400 | [diff] [blame] | 173 | /* For eth3 */ |
| 174 | const u8 mac[] = { 0x02, 0x00, 0x11, 0x22, 0x33, 0x45 }; |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 175 | |
| 176 | priv->data = calloc(1, plat->size); |
| 177 | if (!priv->data) |
| 178 | return -ENOMEM; |
| 179 | |
Sean Anderson | e2dc0e6 | 2022-05-05 13:11:42 -0400 | [diff] [blame] | 180 | memcpy(&priv->data[24], mac, sizeof(mac)); |
| 181 | |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int sandbox_i2c_eeprom_remove(struct udevice *dev) |
| 186 | { |
| 187 | struct sandbox_i2c_flash *priv = dev_get_priv(dev); |
| 188 | |
| 189 | free(priv->data); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static const struct udevice_id sandbox_i2c_ids[] = { |
| 195 | { .compatible = "sandbox,i2c-eeprom" }, |
| 196 | { } |
| 197 | }; |
| 198 | |
| 199 | U_BOOT_DRIVER(sandbox_i2c_emul) = { |
| 200 | .name = "sandbox_i2c_eeprom_emul", |
| 201 | .id = UCLASS_I2C_EMUL, |
| 202 | .of_match = sandbox_i2c_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 203 | .of_to_plat = sandbox_i2c_eeprom_of_to_plat, |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 204 | .probe = sandbox_i2c_eeprom_probe, |
| 205 | .remove = sandbox_i2c_eeprom_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 206 | .priv_auto = sizeof(struct sandbox_i2c_flash), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 207 | .plat_auto = sizeof(struct sandbox_i2c_flash_plat_data), |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 208 | .ops = &sandbox_i2c_emul_ops, |
| 209 | }; |