2 * Simulate an I2C eeprom
4 * Copyright (c) 2014 Google, Inc
6 * SPDX-License-Identifier: GPL-2.0+
17 #define debug_buffer print_buffer
19 #define debug_buffer(x, ...)
22 struct sandbox_i2c_flash_plat_data {
23 enum sandbox_i2c_eeprom_test_mode test_mode;
25 int offset_len; /* Length of an offset in bytes */
26 int size; /* Size of data buffer */
29 struct sandbox_i2c_flash {
33 void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
34 enum sandbox_i2c_eeprom_test_mode mode)
36 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
38 plat->test_mode = mode;
41 void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
43 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
45 plat->offset_len = offset_len;
48 static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg,
51 struct sandbox_i2c_flash *priv = dev_get_priv(emul);
54 debug("\n%s\n", __func__);
55 debug_buffer(0, priv->data, 1, 16, 0);
56 for (; nmsgs > 0; nmsgs--, msg++) {
57 struct sandbox_i2c_flash_plat_data *plat =
58 dev_get_platdata(emul);
64 if (msg->addr + msg->len > plat->size) {
65 debug("%s: Address %x, len %x is outside range 0..%x\n",
66 __func__, msg->addr, msg->len, plat->size);
70 debug(" %s: msg->len=%d",
71 msg->flags & I2C_M_RD ? "read" : "write",
73 if (msg->flags & I2C_M_RD) {
74 if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
76 debug(", offset %x, len %x: ", offset, len);
77 memcpy(msg->buf, priv->data + offset, len);
78 memset(msg->buf + len, '\xff', msg->len - len);
79 debug_buffer(0, msg->buf, 1, msg->len, 0);
80 } else if (len >= plat->offset_len) {
84 for (i = 0; i < plat->offset_len; i++, len--)
85 offset = (offset << 8) | *ptr++;
86 debug(", set offset %x: ", offset);
87 debug_buffer(0, msg->buf, 1, msg->len, 0);
88 if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
91 /* For testing, map offsets into our limited buffer */
92 for (i = 24; i > 0; i -= 8) {
93 if (offset > (1 << i)) {
94 offset = (offset >> i) |
95 (offset & ((1 << i) - 1));
99 memcpy(priv->data + offset, ptr, len);
102 debug_buffer(0, priv->data, 1, 16, 0);
107 struct dm_i2c_ops sandbox_i2c_emul_ops = {
108 .xfer = sandbox_i2c_eeprom_xfer,
111 static int sandbox_i2c_eeprom_ofdata_to_platdata(struct udevice *dev)
113 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
115 plat->size = dev_read_u32_default(dev, "sandbox,size", 32);
116 plat->filename = dev_read_string(dev, "sandbox,filename");
117 if (!plat->filename) {
118 debug("%s: No filename for device '%s'\n", __func__,
122 plat->test_mode = SIE_TEST_MODE_NONE;
123 plat->offset_len = 1;
128 static int sandbox_i2c_eeprom_probe(struct udevice *dev)
130 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
131 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
133 priv->data = calloc(1, plat->size);
140 static int sandbox_i2c_eeprom_remove(struct udevice *dev)
142 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
149 static const struct udevice_id sandbox_i2c_ids[] = {
150 { .compatible = "sandbox,i2c-eeprom" },
154 U_BOOT_DRIVER(sandbox_i2c_emul) = {
155 .name = "sandbox_i2c_eeprom_emul",
156 .id = UCLASS_I2C_EMUL,
157 .of_match = sandbox_i2c_ids,
158 .ofdata_to_platdata = sandbox_i2c_eeprom_ofdata_to_platdata,
159 .probe = sandbox_i2c_eeprom_probe,
160 .remove = sandbox_i2c_eeprom_remove,
161 .priv_auto_alloc_size = sizeof(struct sandbox_i2c_flash),
162 .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_flash_plat_data),
163 .ops = &sandbox_i2c_emul_ops,