blob: 6f32087ede56d7eb98de2affdd64f52625a52342 [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{
Simon Glassc69cda22020-12-03 16:55:20 -070039 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
Simon Glass6ec1b752014-12-10 08:55:51 -070040
41 plat->test_mode = mode;
42}
43
44void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
45{
Simon Glassc69cda22020-12-03 16:55:20 -070046 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
Simon Glass6ec1b752014-12-10 08:55:51 -070047
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{
Simon Glassc69cda22020-12-03 16:55:20 -070054 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
Robert Beckett951674a2019-10-28 17:44:59 +000055
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);
Simon Glassc69cda22020-12-03 16:55:20 -070077 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(emul);
Robert Beckett951674a2019-10-28 17:44:59 +000078 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
Simon Glassd1998a92020-12-03 16:55:21 -0700152static int sandbox_i2c_eeprom_of_to_plat(struct udevice *dev)
Simon Glass6ec1b752014-12-10 08:55:51 -0700153{
Simon Glassc69cda22020-12-03 16:55:20 -0700154 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
Simon Glass6ec1b752014-12-10 08:55:51 -0700155
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{
Simon Glassc69cda22020-12-03 16:55:20 -0700172 struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
Simon Glass6ec1b752014-12-10 08:55:51 -0700173 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
Sean Anderson472caa62022-05-05 13:11:42 -0400174 /* For eth3 */
175 const u8 mac[] = { 0x02, 0x00, 0x11, 0x22, 0x33, 0x45 };
Simon Glass6ec1b752014-12-10 08:55:51 -0700176
177 priv->data = calloc(1, plat->size);
178 if (!priv->data)
179 return -ENOMEM;
180
Sean Anderson472caa62022-05-05 13:11:42 -0400181 memcpy(&priv->data[24], mac, sizeof(mac));
182
Simon Glass6ec1b752014-12-10 08:55:51 -0700183 return 0;
184}
185
186static int sandbox_i2c_eeprom_remove(struct udevice *dev)
187{
188 struct sandbox_i2c_flash *priv = dev_get_priv(dev);
189
190 free(priv->data);
191
192 return 0;
193}
194
195static const struct udevice_id sandbox_i2c_ids[] = {
196 { .compatible = "sandbox,i2c-eeprom" },
197 { }
198};
199
200U_BOOT_DRIVER(sandbox_i2c_emul) = {
201 .name = "sandbox_i2c_eeprom_emul",
202 .id = UCLASS_I2C_EMUL,
203 .of_match = sandbox_i2c_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700204 .of_to_plat = sandbox_i2c_eeprom_of_to_plat,
Simon Glass6ec1b752014-12-10 08:55:51 -0700205 .probe = sandbox_i2c_eeprom_probe,
206 .remove = sandbox_i2c_eeprom_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700207 .priv_auto = sizeof(struct sandbox_i2c_flash),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700208 .plat_auto = sizeof(struct sandbox_i2c_flash_plat_data),
Simon Glass6ec1b752014-12-10 08:55:51 -0700209 .ops = &sandbox_i2c_emul_ops,
210};