1 // SPDX-License-Identifier: GPL-2.0+
3 * Simulate an I2C eeprom
5 * Copyright (c) 2014 Google, Inc
16 #define debug_buffer print_buffer
18 #define debug_buffer(x, ...)
21 struct sandbox_i2c_flash_plat_data {
22 enum sandbox_i2c_eeprom_test_mode test_mode;
24 int offset_len; /* Length of an offset in bytes */
25 int size; /* Size of data buffer */
26 uint chip_addr_offset_mask; /* mask of addr bits used for offset */
29 struct sandbox_i2c_flash {
31 uint prev_addr; /* slave address of previous access */
32 uint prev_offset; /* offset of previous access */
35 void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
36 enum sandbox_i2c_eeprom_test_mode mode)
38 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
40 plat->test_mode = mode;
43 void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
45 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
47 plat->offset_len = offset_len;
50 void sandbox_i2c_eeprom_set_chip_addr_offset_mask(struct udevice *dev,
53 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
55 plat->chip_addr_offset_mask = mask;
58 uint sanbox_i2c_eeprom_get_prev_addr(struct udevice *dev)
60 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
62 return priv->prev_addr;
65 uint sanbox_i2c_eeprom_get_prev_offset(struct udevice *dev)
67 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
69 return priv->prev_offset;
72 static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg,
75 struct sandbox_i2c_flash *priv = dev_get_priv(emul);
76 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(emul);
77 uint offset = msg->addr & plat->chip_addr_offset_mask;
79 debug("\n%s\n", __func__);
80 debug_buffer(0, priv->data, 1, 16, 0);
82 /* store addr for testing visibity */
83 priv->prev_addr = msg->addr;
85 for (; nmsgs > 0; nmsgs--, msg++) {
92 debug(" %s: msg->addr=%x msg->len=%d",
93 msg->flags & I2C_M_RD ? "read" : "write",
95 if (msg->flags & I2C_M_RD) {
96 if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
98 debug(", offset %x, len %x: ", offset, len);
99 if (offset + len > plat->size) {
100 int overflow = offset + len - plat->size;
101 int initial = len - overflow;
103 memcpy(msg->buf, priv->data + offset, initial);
104 memcpy(msg->buf + initial, priv->data,
107 memcpy(msg->buf, priv->data + offset, len);
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) {
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)
122 /* store offset for testing visibility */
123 priv->prev_offset = offset;
125 /* For testing, map offsets into our limited buffer.
126 * offset wraps every 256 bytes
129 debug("mapped offset to %x\n", offset);
131 if (offset + len > plat->size) {
132 int overflow = offset + len - plat->size;
133 int initial = len - overflow;
135 memcpy(priv->data + offset, ptr, initial);
136 memcpy(priv->data, ptr + initial, overflow);
138 memcpy(priv->data + offset, ptr, len);
142 debug_buffer(0, priv->data, 1, 16, 0);
147 struct dm_i2c_ops sandbox_i2c_emul_ops = {
148 .xfer = sandbox_i2c_eeprom_xfer,
151 static int sandbox_i2c_eeprom_of_to_plat(struct udevice *dev)
153 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
155 plat->size = dev_read_u32_default(dev, "sandbox,size", 32);
156 plat->filename = dev_read_string(dev, "sandbox,filename");
157 if (!plat->filename) {
158 debug("%s: No filename for device '%s'\n", __func__,
162 plat->test_mode = SIE_TEST_MODE_NONE;
163 plat->offset_len = 1;
164 plat->chip_addr_offset_mask = 0;
169 static int sandbox_i2c_eeprom_probe(struct udevice *dev)
171 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
172 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
174 const u8 mac[] = { 0x02, 0x00, 0x11, 0x22, 0x33, 0x45 };
176 priv->data = calloc(1, plat->size);
180 memcpy(&priv->data[24], mac, sizeof(mac));
185 static int sandbox_i2c_eeprom_remove(struct udevice *dev)
187 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
194 static const struct udevice_id sandbox_i2c_ids[] = {
195 { .compatible = "sandbox,i2c-eeprom" },
199 U_BOOT_DRIVER(sandbox_i2c_emul) = {
200 .name = "sandbox_i2c_eeprom_emul",
201 .id = UCLASS_I2C_EMUL,
202 .of_match = sandbox_i2c_ids,
203 .of_to_plat = sandbox_i2c_eeprom_of_to_plat,
204 .probe = sandbox_i2c_eeprom_probe,
205 .remove = sandbox_i2c_eeprom_remove,
206 .priv_auto = sizeof(struct sandbox_i2c_flash),
207 .plat_auto = sizeof(struct sandbox_i2c_flash_plat_data),
208 .ops = &sandbox_i2c_emul_ops,