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