xref: /openbmc/qemu/hw/nvram/eeprom_at24c.c (revision d294ec51)
1 /*
2  * *AT24C* series I2C EEPROM
3  *
4  * Copyright (c) 2015 Michael Davidsaver
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.  See
7  * the LICENSE file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 
12 #include "qapi/error.h"
13 #include "qemu/module.h"
14 #include "hw/i2c/i2c.h"
15 #include "hw/nvram/eeprom_at24c.h"
16 #include "hw/qdev-properties.h"
17 #include "hw/qdev-properties-system.h"
18 #include "sysemu/block-backend.h"
19 #include "qom/object.h"
20 
21 /* #define DEBUG_AT24C */
22 
23 #ifdef DEBUG_AT24C
24 #define DPRINTK(FMT, ...) printf(TYPE_AT24C_EE " : " FMT, ## __VA_ARGS__)
25 #else
26 #define DPRINTK(FMT, ...) do {} while (0)
27 #endif
28 
29 #define ERR(FMT, ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \
30                             ## __VA_ARGS__)
31 
32 #define TYPE_AT24C_EE "at24c-eeprom"
33 typedef struct EEPROMState EEPROMState;
34 DECLARE_INSTANCE_CHECKER(EEPROMState, AT24C_EE,
35                          TYPE_AT24C_EE)
36 
37 struct EEPROMState {
38     I2CSlave parent_obj;
39 
40     /* address counter */
41     uint16_t cur;
42     /* total size in bytes */
43     uint32_t rsize;
44     /*
45      * address byte number
46      *  for  24c01, 24c02 size <= 256 byte, use only 1 byte
47      *  otherwise size > 256, use 2 byte
48      */
49     uint8_t asize;
50 
51     bool writable;
52     /* cells changed since last START? */
53     bool changed;
54     /* during WRITE, # of address bytes transferred */
55     uint8_t haveaddr;
56 
57     uint8_t *mem;
58 
59     BlockBackend *blk;
60 
61     const uint8_t *init_rom;
62     uint32_t init_rom_size;
63 };
64 
65 static
at24c_eeprom_event(I2CSlave * s,enum i2c_event event)66 int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)
67 {
68     EEPROMState *ee = AT24C_EE(s);
69 
70     switch (event) {
71     case I2C_START_SEND:
72     case I2C_FINISH:
73         ee->haveaddr = 0;
74         /* fallthrough */
75     case I2C_START_RECV:
76         DPRINTK("clear\n");
77         if (ee->blk && ee->changed) {
78             int ret = blk_pwrite(ee->blk, 0, ee->rsize, ee->mem, 0);
79             if (ret < 0) {
80                 ERR(TYPE_AT24C_EE
81                         " : failed to write backing file\n");
82             }
83             DPRINTK("Wrote to backing file\n");
84         }
85         ee->changed = false;
86         break;
87     case I2C_NACK:
88         break;
89     default:
90         return -1;
91     }
92     return 0;
93 }
94 
95 static
at24c_eeprom_recv(I2CSlave * s)96 uint8_t at24c_eeprom_recv(I2CSlave *s)
97 {
98     EEPROMState *ee = AT24C_EE(s);
99     uint8_t ret;
100 
101     if (ee->haveaddr > 0 && ee->haveaddr < ee->asize) {
102         /*
103          * Provide behaviour that aligns with NVMe MI 1.2c, section 8.2.
104          *
105          * https://nvmexpress.org/wp-content/uploads/NVM-Express-Management-Interface-Specification-1.2c-2022.10.06-Ratified.pdf
106          *
107          * Otherwise, the clocked-out data is meaningless anyway, and so reading
108          * off memory is as good a behaviour as anything. This also happens to
109          * help the address-width detection heuristic in OpenBMC's userspace.
110          *
111          * https://github.com/openbmc/entity-manager/blob/0422a24bb6033605ce75479f675fedc76abb1167/src/fru_device.cpp#L197-L229
112          */
113         ee->haveaddr = ee->asize;
114         ee->cur %= ee->rsize;
115     }
116 
117     ret = ee->mem[ee->cur];
118 
119     ee->cur = (ee->cur + 1u) % ee->rsize;
120     DPRINTK("Recv %02x %c\n", ret, ret);
121 
122     return ret;
123 }
124 
125 static
at24c_eeprom_send(I2CSlave * s,uint8_t data)126 int at24c_eeprom_send(I2CSlave *s, uint8_t data)
127 {
128     EEPROMState *ee = AT24C_EE(s);
129 
130     if (ee->haveaddr < ee->asize) {
131         ee->cur <<= 8;
132         ee->cur |= data;
133         ee->haveaddr++;
134         if (ee->haveaddr == ee->asize) {
135             ee->cur %= ee->rsize;
136             DPRINTK("Set pointer %04x\n", ee->cur);
137         }
138 
139     } else {
140         if (ee->writable) {
141             DPRINTK("Send %02x\n", data);
142             ee->mem[ee->cur] = data;
143             ee->changed = true;
144         } else {
145             DPRINTK("Send error %02x read-only\n", data);
146         }
147         ee->cur = (ee->cur + 1u) % ee->rsize;
148 
149     }
150 
151     return 0;
152 }
153 
at24c_eeprom_init(I2CBus * bus,uint8_t address,uint32_t rom_size)154 I2CSlave *at24c_eeprom_init(I2CBus *bus, uint8_t address, uint32_t rom_size)
155 {
156     return at24c_eeprom_init_rom(bus, address, rom_size, NULL, 0);
157 }
158 
at24c_eeprom_init_rom(I2CBus * bus,uint8_t address,uint32_t rom_size,const uint8_t * init_rom,uint32_t init_rom_size)159 I2CSlave *at24c_eeprom_init_rom(I2CBus *bus, uint8_t address, uint32_t rom_size,
160                                 const uint8_t *init_rom, uint32_t init_rom_size)
161 {
162     EEPROMState *s;
163 
164     s = AT24C_EE(i2c_slave_new(TYPE_AT24C_EE, address));
165 
166     qdev_prop_set_uint32(DEVICE(s), "rom-size", rom_size);
167 
168     /* TODO: Model init_rom with QOM properties. */
169     s->init_rom = init_rom;
170     s->init_rom_size = init_rom_size;
171 
172     i2c_slave_realize_and_unref(I2C_SLAVE(s), bus, &error_abort);
173 
174     return I2C_SLAVE(s);
175 }
176 
at24c_eeprom_realize(DeviceState * dev,Error ** errp)177 static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
178 {
179     EEPROMState *ee = AT24C_EE(dev);
180 
181     if (ee->init_rom_size > ee->rsize) {
182         error_setg(errp, "%s: init rom is larger than rom: %u > %u",
183                    TYPE_AT24C_EE, ee->init_rom_size, ee->rsize);
184         return;
185     }
186 
187     if (ee->blk) {
188         int64_t len = blk_getlength(ee->blk);
189 
190         if (len != ee->rsize) {
191             error_setg(errp, "%s: Backing file size %" PRId64 " != %u",
192                        TYPE_AT24C_EE, len, ee->rsize);
193             return;
194         }
195 
196         if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
197                          BLK_PERM_ALL, &error_fatal) < 0)
198         {
199             error_setg(errp, "%s: Backing file incorrect permission",
200                        TYPE_AT24C_EE);
201             return;
202         }
203     }
204 
205     ee->mem = g_malloc0(ee->rsize);
206     memset(ee->mem, 0, ee->rsize);
207 
208     if (ee->init_rom) {
209         memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
210     }
211 
212     if (ee->blk) {
213         int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
214 
215         if (ret < 0) {
216             ERR(TYPE_AT24C_EE
217                     " : Failed initial sync with backing file\n");
218         }
219         DPRINTK("Reset read backing file\n");
220     }
221 
222     /*
223      * If address size didn't define with property set
224      *   value is 0 as default, setting it by Rom size detecting.
225      */
226     if (ee->asize == 0) {
227         if (ee->rsize <= 256) {
228             ee->asize = 1;
229         } else {
230             ee->asize = 2;
231         }
232     }
233 }
234 
235 static
at24c_eeprom_reset(DeviceState * state)236 void at24c_eeprom_reset(DeviceState *state)
237 {
238     EEPROMState *ee = AT24C_EE(state);
239 
240     ee->changed = false;
241     ee->cur = 0;
242     ee->haveaddr = 0;
243 }
244 
245 static Property at24c_eeprom_props[] = {
246     DEFINE_PROP_UINT32("rom-size", EEPROMState, rsize, 0),
247     DEFINE_PROP_UINT8("address-size", EEPROMState, asize, 0),
248     DEFINE_PROP_BOOL("writable", EEPROMState, writable, true),
249     DEFINE_PROP_DRIVE("drive", EEPROMState, blk),
250     DEFINE_PROP_END_OF_LIST()
251 };
252 
253 static
at24c_eeprom_class_init(ObjectClass * klass,void * data)254 void at24c_eeprom_class_init(ObjectClass *klass, void *data)
255 {
256     DeviceClass *dc = DEVICE_CLASS(klass);
257     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
258 
259     dc->realize = &at24c_eeprom_realize;
260     k->event = &at24c_eeprom_event;
261     k->recv = &at24c_eeprom_recv;
262     k->send = &at24c_eeprom_send;
263 
264     device_class_set_props(dc, at24c_eeprom_props);
265     dc->reset = at24c_eeprom_reset;
266 }
267 
268 static
269 const TypeInfo at24c_eeprom_type = {
270     .name = TYPE_AT24C_EE,
271     .parent = TYPE_I2C_SLAVE,
272     .instance_size = sizeof(EEPROMState),
273     .class_size = sizeof(I2CSlaveClass),
274     .class_init = at24c_eeprom_class_init,
275 };
276 
at24c_eeprom_register(void)277 static void at24c_eeprom_register(void)
278 {
279     type_register_static(&at24c_eeprom_type);
280 }
281 
282 type_init(at24c_eeprom_register)
283