blob: 284267f2ea35fb90423dd0af75b0dc82311964ae [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass45be32c2014-12-10 08:55:51 -07002/*
3 * Simulate an I2C eeprom
4 *
5 * Copyright (c) 2014 Google, Inc
Simon Glass45be32c2014-12-10 08:55:51 -07006 */
7
8#include <common.h>
9#include <dm.h>
Simon Glass07a3b232015-05-04 11:31:08 -060010#include <errno.h>
Simon Glass45be32c2014-12-10 08:55:51 -070011#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 Glass45be32c2014-12-10 08:55:51 -070021struct 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
28struct sandbox_i2c_flash {
29 uint8_t *data;
Robert Beckett1fe8a492019-10-28 17:44:58 +000030 uint prev_addr; /* slave address of previous access */
31 uint prev_offset; /* offset of previous access */
Simon Glass45be32c2014-12-10 08:55:51 -070032};
33
34void 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
42void 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 Beckett1fe8a492019-10-28 17:44:58 +000049uint 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
56uint 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 Glass45be32c2014-12-10 08:55:51 -070063static 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 Beckett1fe8a492019-10-28 17:44:58 +000071
72 /* store addr for testing visibity */
73 priv->prev_addr = msg->addr;
74
Simon Glass45be32c2014-12-10 08:55:51 -070075 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 Glass45be32c2014-12-10 08:55:51 -070083 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 Beckett1fe8a492019-10-28 17:44:58 +000091 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 Glass45be32c2014-12-10 08:55:51 -0700101 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 Beckett1fe8a492019-10-28 17:44:58 +0000114 /* 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 Glass45be32c2014-12-10 08:55:51 -0700131 }
Simon Glass45be32c2014-12-10 08:55:51 -0700132 }
133 }
134 debug_buffer(0, priv->data, 1, 16, 0);
135
136 return 0;
137}
138
139struct dm_i2c_ops sandbox_i2c_emul_ops = {
140 .xfer = sandbox_i2c_eeprom_xfer,
141};
142
143static 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 Glass41f4f342017-05-18 20:09:52 -0600147 plat->size = dev_read_u32_default(dev, "sandbox,size", 32);
148 plat->filename = dev_read_string(dev, "sandbox,filename");
Simon Glass45be32c2014-12-10 08:55:51 -0700149 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
160static 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
172static 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
181static const struct udevice_id sandbox_i2c_ids[] = {
182 { .compatible = "sandbox,i2c-eeprom" },
183 { }
184};
185
186U_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};