blob: 6be76ad02ef686813a3aa9d609c4214ff2c39bd4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6ec1b752014-12-10 08:55:51 -07002/*
3 * Simulate an I2C eeprom
4 *
5 * Copyright (c) 2014 Google, Inc
Simon Glass6ec1b752014-12-10 08:55:51 -07006 */
7
8#include <common.h>
9#include <dm.h>
Simon Glass38068822015-05-04 11:31:08 -060010#include <errno.h>
Simon Glass6ec1b752014-12-10 08:55:51 -070011#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass6ec1b752014-12-10 08:55:51 -070013#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 Glass6ec1b752014-12-10 08:55:51 -070022struct 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 Beckett951674a2019-10-28 17:44:59 +000027 uint chip_addr_offset_mask; /* mask of addr bits used for offset */
Simon Glass6ec1b752014-12-10 08:55:51 -070028};
29
30struct sandbox_i2c_flash {
31 uint8_t *data;
Robert Beckett22e93512019-10-28 17:44:58 +000032 uint prev_addr; /* slave address of previous access */
33 uint prev_offset; /* offset of previous access */
Simon Glass6ec1b752014-12-10 08:55:51 -070034};
35
36void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
37 enum sandbox_i2c_eeprom_test_mode mode)
38{
39 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
40
41 plat->test_mode = mode;
42}
43
44void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
45{
46 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
47
48 plat->offset_len = offset_len;
49}
50
Robert Beckett951674a2019-10-28 17:44:59 +000051void sandbox_i2c_eeprom_set_chip_addr_offset_mask(struct udevice *dev,
52 uint mask)
53{
54 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
55
56 plat->chip_addr_offset_mask = mask;
57}
58
Robert Beckett22e93512019-10-28 17:44:58 +000059uint 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
66uint 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 Glass6ec1b752014-12-10 08:55:51 -070073static 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);
Robert Beckett951674a2019-10-28 17:44:59 +000077 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(emul);
78 uint offset = msg->addr & plat->chip_addr_offset_mask;
Simon Glass6ec1b752014-12-10 08:55:51 -070079
80 debug("\n%s\n", __func__);
81 debug_buffer(0, priv->data, 1, 16, 0);
Robert Beckett22e93512019-10-28 17:44:58 +000082
83 /* store addr for testing visibity */
84 priv->prev_addr = msg->addr;
85
Simon Glass6ec1b752014-12-10 08:55:51 -070086 for (; nmsgs > 0; nmsgs--, msg++) {
Simon Glass6ec1b752014-12-10 08:55:51 -070087 int len;
88 u8 *ptr;
89
90 if (!plat->size)
91 return -ENODEV;
Simon Glass6ec1b752014-12-10 08:55:51 -070092 len = msg->len;
Robert Beckett951674a2019-10-28 17:44:59 +000093 debug(" %s: msg->addr=%x msg->len=%d",
Simon Glass6ec1b752014-12-10 08:55:51 -070094 msg->flags & I2C_M_RD ? "read" : "write",
Robert Beckett951674a2019-10-28 17:44:59 +000095 msg->addr, msg->len);
Simon Glass6ec1b752014-12-10 08:55:51 -070096 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 Beckett22e93512019-10-28 17:44:58 +0000100 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 Glass6ec1b752014-12-10 08:55:51 -0700110 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 Beckett22e93512019-10-28 17:44:58 +0000123 /* 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 Glass6ec1b752014-12-10 08:55:51 -0700140 }
Simon Glass6ec1b752014-12-10 08:55:51 -0700141 }
142 }
143 debug_buffer(0, priv->data, 1, 16, 0);
144
145 return 0;
146}
147
148struct dm_i2c_ops sandbox_i2c_emul_ops = {
149 .xfer = sandbox_i2c_eeprom_xfer,
150};
151
152static int sandbox_i2c_eeprom_ofdata_to_platdata(struct udevice *dev)
153{
154 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
155
Simon Glass2dd57f52017-05-18 20:09:52 -0600156 plat->size = dev_read_u32_default(dev, "sandbox,size", 32);
157 plat->filename = dev_read_string(dev, "sandbox,filename");
Simon Glass6ec1b752014-12-10 08:55:51 -0700158 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 Beckett951674a2019-10-28 17:44:59 +0000165 plat->chip_addr_offset_mask = 0;
Simon Glass6ec1b752014-12-10 08:55:51 -0700166
167 return 0;
168}
169
170static int sandbox_i2c_eeprom_probe(struct udevice *dev)
171{
172 struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
173 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
174
175 priv->data = calloc(1, plat->size);
176 if (!priv->data)
177 return -ENOMEM;
178
179 return 0;
180}
181
182static int sandbox_i2c_eeprom_remove(struct udevice *dev)
183{
184 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
185
186 free(priv->data);
187
188 return 0;
189}
190
191static const struct udevice_id sandbox_i2c_ids[] = {
192 { .compatible = "sandbox,i2c-eeprom" },
193 { }
194};
195
196U_BOOT_DRIVER(sandbox_i2c_emul) = {
197 .name = "sandbox_i2c_eeprom_emul",
198 .id = UCLASS_I2C_EMUL,
199 .of_match = sandbox_i2c_ids,
200 .ofdata_to_platdata = sandbox_i2c_eeprom_ofdata_to_platdata,
201 .probe = sandbox_i2c_eeprom_probe,
202 .remove = sandbox_i2c_eeprom_remove,
203 .priv_auto_alloc_size = sizeof(struct sandbox_i2c_flash),
204 .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_flash_plat_data),
205 .ops = &sandbox_i2c_emul_ops,
206};