Merge git://git.denx.de/u-boot-imx
[oweals/u-boot.git] / drivers / misc / i2c_eeprom_emul.c
1 /*
2  * Simulate an I2C eeprom
3  *
4  * Copyright (c) 2014 Google, Inc
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <i2c.h>
13 #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
22 struct 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 */
27 };
28
29 struct sandbox_i2c_flash {
30         uint8_t *data;
31 };
32
33 void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
34                                       enum sandbox_i2c_eeprom_test_mode mode)
35 {
36         struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
37
38         plat->test_mode = mode;
39 }
40
41 void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
42 {
43         struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
44
45         plat->offset_len = offset_len;
46 }
47
48 static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg,
49                                   int nmsgs)
50 {
51         struct sandbox_i2c_flash *priv = dev_get_priv(emul);
52         uint offset = 0;
53
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);
59                 int len;
60                 u8 *ptr;
61
62                 if (!plat->size)
63                         return -ENODEV;
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);
67                         return -EINVAL;
68                 }
69                 len = msg->len;
70                 debug("   %s: msg->len=%d",
71                       msg->flags & I2C_M_RD ? "read" : "write",
72                       msg->len);
73                 if (msg->flags & I2C_M_RD) {
74                         if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
75                                 len = 1;
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) {
81                         int i;
82
83                         ptr = msg->buf;
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)
89                                 len = min(len, 1);
90
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));
96                                         offset += i;
97                                 }
98                         }
99                         memcpy(priv->data + offset, ptr, len);
100                 }
101         }
102         debug_buffer(0, priv->data, 1, 16, 0);
103
104         return 0;
105 }
106
107 struct dm_i2c_ops sandbox_i2c_emul_ops = {
108         .xfer = sandbox_i2c_eeprom_xfer,
109 };
110
111 static int sandbox_i2c_eeprom_ofdata_to_platdata(struct udevice *dev)
112 {
113         struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
114
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__,
119                       dev->name);
120                 return -EINVAL;
121         }
122         plat->test_mode = SIE_TEST_MODE_NONE;
123         plat->offset_len = 1;
124
125         return 0;
126 }
127
128 static int sandbox_i2c_eeprom_probe(struct udevice *dev)
129 {
130         struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
131         struct sandbox_i2c_flash *priv = dev_get_priv(dev);
132
133         priv->data = calloc(1, plat->size);
134         if (!priv->data)
135                 return -ENOMEM;
136
137         return 0;
138 }
139
140 static int sandbox_i2c_eeprom_remove(struct udevice *dev)
141 {
142         struct sandbox_i2c_flash *priv = dev_get_priv(dev);
143
144         free(priv->data);
145
146         return 0;
147 }
148
149 static const struct udevice_id sandbox_i2c_ids[] = {
150         { .compatible = "sandbox,i2c-eeprom" },
151         { }
152 };
153
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,
164 };