1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Simulate an I2C eeprom
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2014 Google, Inc
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <common.h>
10*4882a593Smuzhiyun #include <dm.h>
11*4882a593Smuzhiyun #include <errno.h>
12*4882a593Smuzhiyun #include <i2c.h>
13*4882a593Smuzhiyun #include <malloc.h>
14*4882a593Smuzhiyun #include <asm/test.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #ifdef DEBUG
17*4882a593Smuzhiyun #define debug_buffer print_buffer
18*4882a593Smuzhiyun #else
19*4882a593Smuzhiyun #define debug_buffer(x, ...)
20*4882a593Smuzhiyun #endif
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun struct sandbox_i2c_flash_plat_data {
25*4882a593Smuzhiyun enum sandbox_i2c_eeprom_test_mode test_mode;
26*4882a593Smuzhiyun const char *filename;
27*4882a593Smuzhiyun int offset_len; /* Length of an offset in bytes */
28*4882a593Smuzhiyun int size; /* Size of data buffer */
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct sandbox_i2c_flash {
32*4882a593Smuzhiyun uint8_t *data;
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
sandbox_i2c_eeprom_set_test_mode(struct udevice * dev,enum sandbox_i2c_eeprom_test_mode mode)35*4882a593Smuzhiyun void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
36*4882a593Smuzhiyun enum sandbox_i2c_eeprom_test_mode mode)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun plat->test_mode = mode;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
sandbox_i2c_eeprom_set_offset_len(struct udevice * dev,int offset_len)43*4882a593Smuzhiyun void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun plat->offset_len = offset_len;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
sandbox_i2c_eeprom_xfer(struct udevice * emul,struct i2c_msg * msg,int nmsgs)50*4882a593Smuzhiyun static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg,
51*4882a593Smuzhiyun int nmsgs)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun struct sandbox_i2c_flash *priv = dev_get_priv(emul);
54*4882a593Smuzhiyun uint offset = 0;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun debug("\n%s\n", __func__);
57*4882a593Smuzhiyun debug_buffer(0, priv->data, 1, 16, 0);
58*4882a593Smuzhiyun for (; nmsgs > 0; nmsgs--, msg++) {
59*4882a593Smuzhiyun struct sandbox_i2c_flash_plat_data *plat =
60*4882a593Smuzhiyun dev_get_platdata(emul);
61*4882a593Smuzhiyun int len;
62*4882a593Smuzhiyun u8 *ptr;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun if (!plat->size)
65*4882a593Smuzhiyun return -ENODEV;
66*4882a593Smuzhiyun if (msg->addr + msg->len > plat->size) {
67*4882a593Smuzhiyun debug("%s: Address %x, len %x is outside range 0..%x\n",
68*4882a593Smuzhiyun __func__, msg->addr, msg->len, plat->size);
69*4882a593Smuzhiyun return -EINVAL;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun len = msg->len;
72*4882a593Smuzhiyun debug(" %s: msg->len=%d",
73*4882a593Smuzhiyun msg->flags & I2C_M_RD ? "read" : "write",
74*4882a593Smuzhiyun msg->len);
75*4882a593Smuzhiyun if (msg->flags & I2C_M_RD) {
76*4882a593Smuzhiyun if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
77*4882a593Smuzhiyun len = 1;
78*4882a593Smuzhiyun debug(", offset %x, len %x: ", offset, len);
79*4882a593Smuzhiyun memcpy(msg->buf, priv->data + offset, len);
80*4882a593Smuzhiyun memset(msg->buf + len, '\xff', msg->len - len);
81*4882a593Smuzhiyun debug_buffer(0, msg->buf, 1, msg->len, 0);
82*4882a593Smuzhiyun } else if (len >= plat->offset_len) {
83*4882a593Smuzhiyun int i;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun ptr = msg->buf;
86*4882a593Smuzhiyun for (i = 0; i < plat->offset_len; i++, len--)
87*4882a593Smuzhiyun offset = (offset << 8) | *ptr++;
88*4882a593Smuzhiyun debug(", set offset %x: ", offset);
89*4882a593Smuzhiyun debug_buffer(0, msg->buf, 1, msg->len, 0);
90*4882a593Smuzhiyun if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
91*4882a593Smuzhiyun len = min(len, 1);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* For testing, map offsets into our limited buffer */
94*4882a593Smuzhiyun for (i = 24; i > 0; i -= 8) {
95*4882a593Smuzhiyun if (offset > (1 << i)) {
96*4882a593Smuzhiyun offset = (offset >> i) |
97*4882a593Smuzhiyun (offset & ((1 << i) - 1));
98*4882a593Smuzhiyun offset += i;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun memcpy(priv->data + offset, ptr, len);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun debug_buffer(0, priv->data, 1, 16, 0);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun return 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun struct dm_i2c_ops sandbox_i2c_emul_ops = {
110*4882a593Smuzhiyun .xfer = sandbox_i2c_eeprom_xfer,
111*4882a593Smuzhiyun };
112*4882a593Smuzhiyun
sandbox_i2c_eeprom_ofdata_to_platdata(struct udevice * dev)113*4882a593Smuzhiyun static int sandbox_i2c_eeprom_ofdata_to_platdata(struct udevice *dev)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun plat->size = dev_read_u32_default(dev, "sandbox,size", 32);
118*4882a593Smuzhiyun plat->filename = dev_read_string(dev, "sandbox,filename");
119*4882a593Smuzhiyun if (!plat->filename) {
120*4882a593Smuzhiyun debug("%s: No filename for device '%s'\n", __func__,
121*4882a593Smuzhiyun dev->name);
122*4882a593Smuzhiyun return -EINVAL;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun plat->test_mode = SIE_TEST_MODE_NONE;
125*4882a593Smuzhiyun plat->offset_len = 1;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
sandbox_i2c_eeprom_probe(struct udevice * dev)130*4882a593Smuzhiyun static int sandbox_i2c_eeprom_probe(struct udevice *dev)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
133*4882a593Smuzhiyun struct sandbox_i2c_flash *priv = dev_get_priv(dev);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun priv->data = calloc(1, plat->size);
136*4882a593Smuzhiyun if (!priv->data)
137*4882a593Smuzhiyun return -ENOMEM;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun return 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
sandbox_i2c_eeprom_remove(struct udevice * dev)142*4882a593Smuzhiyun static int sandbox_i2c_eeprom_remove(struct udevice *dev)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun struct sandbox_i2c_flash *priv = dev_get_priv(dev);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun free(priv->data);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun return 0;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun static const struct udevice_id sandbox_i2c_ids[] = {
152*4882a593Smuzhiyun { .compatible = "sandbox,i2c-eeprom" },
153*4882a593Smuzhiyun { }
154*4882a593Smuzhiyun };
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun U_BOOT_DRIVER(sandbox_i2c_emul) = {
157*4882a593Smuzhiyun .name = "sandbox_i2c_eeprom_emul",
158*4882a593Smuzhiyun .id = UCLASS_I2C_EMUL,
159*4882a593Smuzhiyun .of_match = sandbox_i2c_ids,
160*4882a593Smuzhiyun .ofdata_to_platdata = sandbox_i2c_eeprom_ofdata_to_platdata,
161*4882a593Smuzhiyun .probe = sandbox_i2c_eeprom_probe,
162*4882a593Smuzhiyun .remove = sandbox_i2c_eeprom_remove,
163*4882a593Smuzhiyun .priv_auto_alloc_size = sizeof(struct sandbox_i2c_flash),
164*4882a593Smuzhiyun .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_flash_plat_data),
165*4882a593Smuzhiyun .ops = &sandbox_i2c_emul_ops,
166*4882a593Smuzhiyun };
167