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 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
Simon Glass | 07a3b23 | 2015-05-04 11:31:08 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 11 | #include <i2c.h> |
| 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 */ |
| 26 | }; |
| 27 | |
| 28 | struct sandbox_i2c_flash { |
| 29 | uint8_t *data; |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame^] | 30 | uint prev_addr; /* slave address of previous access */ |
| 31 | uint prev_offset; /* offset of previous access */ |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev, |
| 35 | enum sandbox_i2c_eeprom_test_mode mode) |
| 36 | { |
| 37 | struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev); |
| 38 | |
| 39 | plat->test_mode = mode; |
| 40 | } |
| 41 | |
| 42 | void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len) |
| 43 | { |
| 44 | struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev); |
| 45 | |
| 46 | plat->offset_len = offset_len; |
| 47 | } |
| 48 | |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame^] | 49 | uint sanbox_i2c_eeprom_get_prev_addr(struct udevice *dev) |
| 50 | { |
| 51 | struct sandbox_i2c_flash *priv = dev_get_priv(dev); |
| 52 | |
| 53 | return priv->prev_addr; |
| 54 | } |
| 55 | |
| 56 | uint sanbox_i2c_eeprom_get_prev_offset(struct udevice *dev) |
| 57 | { |
| 58 | struct sandbox_i2c_flash *priv = dev_get_priv(dev); |
| 59 | |
| 60 | return priv->prev_offset; |
| 61 | } |
| 62 | |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 63 | static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg, |
| 64 | int nmsgs) |
| 65 | { |
| 66 | struct sandbox_i2c_flash *priv = dev_get_priv(emul); |
| 67 | uint offset = 0; |
| 68 | |
| 69 | debug("\n%s\n", __func__); |
| 70 | debug_buffer(0, priv->data, 1, 16, 0); |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame^] | 71 | |
| 72 | /* store addr for testing visibity */ |
| 73 | priv->prev_addr = msg->addr; |
| 74 | |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 75 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 76 | struct sandbox_i2c_flash_plat_data *plat = |
| 77 | dev_get_platdata(emul); |
| 78 | int len; |
| 79 | u8 *ptr; |
| 80 | |
| 81 | if (!plat->size) |
| 82 | return -ENODEV; |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 83 | len = msg->len; |
| 84 | debug(" %s: msg->len=%d", |
| 85 | msg->flags & I2C_M_RD ? "read" : "write", |
| 86 | msg->len); |
| 87 | if (msg->flags & I2C_M_RD) { |
| 88 | if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE) |
| 89 | len = 1; |
| 90 | debug(", offset %x, len %x: ", offset, len); |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame^] | 91 | if (offset + len > plat->size) { |
| 92 | int overflow = offset + len - plat->size; |
| 93 | int initial = len - overflow; |
| 94 | |
| 95 | memcpy(msg->buf, priv->data + offset, initial); |
| 96 | memcpy(msg->buf + initial, priv->data, |
| 97 | overflow); |
| 98 | } else { |
| 99 | memcpy(msg->buf, priv->data + offset, len); |
| 100 | } |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 101 | memset(msg->buf + len, '\xff', msg->len - len); |
| 102 | debug_buffer(0, msg->buf, 1, msg->len, 0); |
| 103 | } else if (len >= plat->offset_len) { |
| 104 | int i; |
| 105 | |
| 106 | ptr = msg->buf; |
| 107 | for (i = 0; i < plat->offset_len; i++, len--) |
| 108 | offset = (offset << 8) | *ptr++; |
| 109 | debug(", set offset %x: ", offset); |
| 110 | debug_buffer(0, msg->buf, 1, msg->len, 0); |
| 111 | if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE) |
| 112 | len = min(len, 1); |
| 113 | |
Robert Beckett | 1fe8a49 | 2019-10-28 17:44:58 +0000 | [diff] [blame^] | 114 | /* store offset for testing visibility */ |
| 115 | priv->prev_offset = offset; |
| 116 | |
| 117 | /* For testing, map offsets into our limited buffer. |
| 118 | * offset wraps every 256 bytes |
| 119 | */ |
| 120 | offset &= 0xff; |
| 121 | debug("mapped offset to %x\n", offset); |
| 122 | |
| 123 | if (offset + len > plat->size) { |
| 124 | int overflow = offset + len - plat->size; |
| 125 | int initial = len - overflow; |
| 126 | |
| 127 | memcpy(priv->data + offset, ptr, initial); |
| 128 | memcpy(priv->data, ptr + initial, overflow); |
| 129 | } else { |
| 130 | memcpy(priv->data + offset, ptr, len); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 131 | } |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | debug_buffer(0, priv->data, 1, 16, 0); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | struct dm_i2c_ops sandbox_i2c_emul_ops = { |
| 140 | .xfer = sandbox_i2c_eeprom_xfer, |
| 141 | }; |
| 142 | |
| 143 | static int sandbox_i2c_eeprom_ofdata_to_platdata(struct udevice *dev) |
| 144 | { |
| 145 | struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev); |
| 146 | |
Simon Glass | 41f4f34 | 2017-05-18 20:09:52 -0600 | [diff] [blame] | 147 | plat->size = dev_read_u32_default(dev, "sandbox,size", 32); |
| 148 | plat->filename = dev_read_string(dev, "sandbox,filename"); |
Simon Glass | 45be32c | 2014-12-10 08:55:51 -0700 | [diff] [blame] | 149 | if (!plat->filename) { |
| 150 | debug("%s: No filename for device '%s'\n", __func__, |
| 151 | dev->name); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | plat->test_mode = SIE_TEST_MODE_NONE; |
| 155 | plat->offset_len = 1; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int sandbox_i2c_eeprom_probe(struct udevice *dev) |
| 161 | { |
| 162 | struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev); |
| 163 | struct sandbox_i2c_flash *priv = dev_get_priv(dev); |
| 164 | |
| 165 | priv->data = calloc(1, plat->size); |
| 166 | if (!priv->data) |
| 167 | return -ENOMEM; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int sandbox_i2c_eeprom_remove(struct udevice *dev) |
| 173 | { |
| 174 | struct sandbox_i2c_flash *priv = dev_get_priv(dev); |
| 175 | |
| 176 | free(priv->data); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static const struct udevice_id sandbox_i2c_ids[] = { |
| 182 | { .compatible = "sandbox,i2c-eeprom" }, |
| 183 | { } |
| 184 | }; |
| 185 | |
| 186 | U_BOOT_DRIVER(sandbox_i2c_emul) = { |
| 187 | .name = "sandbox_i2c_eeprom_emul", |
| 188 | .id = UCLASS_I2C_EMUL, |
| 189 | .of_match = sandbox_i2c_ids, |
| 190 | .ofdata_to_platdata = sandbox_i2c_eeprom_ofdata_to_platdata, |
| 191 | .probe = sandbox_i2c_eeprom_probe, |
| 192 | .remove = sandbox_i2c_eeprom_remove, |
| 193 | .priv_auto_alloc_size = sizeof(struct sandbox_i2c_flash), |
| 194 | .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_flash_plat_data), |
| 195 | .ops = &sandbox_i2c_emul_ops, |
| 196 | }; |