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 */
28 struct sandbox_i2c_flash {
30 uint prev_addr; /* slave address of previous access */
31 uint prev_offset; /* offset of previous access */
34 void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
35 enum sandbox_i2c_eeprom_test_mode mode)
37 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
39 plat->test_mode = mode;
42 void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
44 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
46 plat->offset_len = offset_len;
49 uint sanbox_i2c_eeprom_get_prev_addr(struct udevice *dev)
51 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
53 return priv->prev_addr;
56 uint sanbox_i2c_eeprom_get_prev_offset(struct udevice *dev)
58 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
60 return priv->prev_offset;
63 static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg,
66 struct sandbox_i2c_flash *priv = dev_get_priv(emul);
69 debug("\n%s\n", __func__);
70 debug_buffer(0, priv->data, 1, 16, 0);
72 /* store addr for testing visibity */
73 priv->prev_addr = msg->addr;
75 for (; nmsgs > 0; nmsgs--, msg++) {
76 struct sandbox_i2c_flash_plat_data *plat =
77 dev_get_platdata(emul);
84 debug(" %s: msg->len=%d",
85 msg->flags & I2C_M_RD ? "read" : "write",
87 if (msg->flags & I2C_M_RD) {
88 if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
90 debug(", offset %x, len %x: ", offset, len);
91 if (offset + len > plat->size) {
92 int overflow = offset + len - plat->size;
93 int initial = len - overflow;
95 memcpy(msg->buf, priv->data + offset, initial);
96 memcpy(msg->buf + initial, priv->data,
99 memcpy(msg->buf, priv->data + offset, len);
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) {
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)
114 /* store offset for testing visibility */
115 priv->prev_offset = offset;
117 /* For testing, map offsets into our limited buffer.
118 * offset wraps every 256 bytes
121 debug("mapped offset to %x\n", offset);
123 if (offset + len > plat->size) {
124 int overflow = offset + len - plat->size;
125 int initial = len - overflow;
127 memcpy(priv->data + offset, ptr, initial);
128 memcpy(priv->data, ptr + initial, overflow);
130 memcpy(priv->data + offset, ptr, len);
134 debug_buffer(0, priv->data, 1, 16, 0);
139 struct dm_i2c_ops sandbox_i2c_emul_ops = {
140 .xfer = sandbox_i2c_eeprom_xfer,
143 static int sandbox_i2c_eeprom_ofdata_to_platdata(struct udevice *dev)
145 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
147 plat->size = dev_read_u32_default(dev, "sandbox,size", 32);
148 plat->filename = dev_read_string(dev, "sandbox,filename");
149 if (!plat->filename) {
150 debug("%s: No filename for device '%s'\n", __func__,
154 plat->test_mode = SIE_TEST_MODE_NONE;
155 plat->offset_len = 1;
160 static int sandbox_i2c_eeprom_probe(struct udevice *dev)
162 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
163 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
165 priv->data = calloc(1, plat->size);
172 static int sandbox_i2c_eeprom_remove(struct udevice *dev)
174 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
181 static const struct udevice_id sandbox_i2c_ids[] = {
182 { .compatible = "sandbox,i2c-eeprom" },
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,