xref: /openbmc/qemu/hw/nvram/eeprom_at24c.c (revision 21063bce)
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     bool writable;
45     /* cells changed since last START? */
46     bool changed;
47     /* during WRITE, # of address bytes transfered */
48     uint8_t haveaddr;
49 
50     uint8_t *mem;
51 
52     BlockBackend *blk;
53 
54     const uint8_t *init_rom;
55     uint32_t init_rom_size;
56 };
57 
58 static
59 int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)
60 {
61     EEPROMState *ee = AT24C_EE(s);
62 
63     switch (event) {
64     case I2C_START_SEND:
65     case I2C_FINISH:
66         ee->haveaddr = 0;
67         /* fallthrough */
68     case I2C_START_RECV:
69         DPRINTK("clear\n");
70         if (ee->blk && ee->changed) {
71             int ret = blk_pwrite(ee->blk, 0, ee->rsize, ee->mem, 0);
72             if (ret < 0) {
73                 ERR(TYPE_AT24C_EE
74                         " : failed to write backing file\n");
75             }
76             DPRINTK("Wrote to backing file\n");
77         }
78         ee->changed = false;
79         break;
80     case I2C_NACK:
81         break;
82     default:
83         return -1;
84     }
85     return 0;
86 }
87 
88 static
89 uint8_t at24c_eeprom_recv(I2CSlave *s)
90 {
91     EEPROMState *ee = AT24C_EE(s);
92     uint8_t ret;
93 
94     if (ee->haveaddr == 1) {
95         return 0xff;
96     }
97 
98     ret = ee->mem[ee->cur];
99 
100     ee->cur = (ee->cur + 1u) % ee->rsize;
101     DPRINTK("Recv %02x %c\n", ret, ret);
102 
103     return ret;
104 }
105 
106 static
107 int at24c_eeprom_send(I2CSlave *s, uint8_t data)
108 {
109     EEPROMState *ee = AT24C_EE(s);
110 
111     if (ee->haveaddr < 2) {
112         ee->cur <<= 8;
113         ee->cur |= data;
114         ee->haveaddr++;
115         if (ee->haveaddr == 2) {
116             ee->cur %= ee->rsize;
117             DPRINTK("Set pointer %04x\n", ee->cur);
118         }
119 
120     } else {
121         if (ee->writable) {
122             DPRINTK("Send %02x\n", data);
123             ee->mem[ee->cur] = data;
124             ee->changed = true;
125         } else {
126             DPRINTK("Send error %02x read-only\n", data);
127         }
128         ee->cur = (ee->cur + 1u) % ee->rsize;
129 
130     }
131 
132     return 0;
133 }
134 
135 I2CSlave *at24c_eeprom_init(I2CBus *bus, uint8_t address, uint32_t rom_size)
136 {
137     return at24c_eeprom_init_rom(bus, address, rom_size, NULL, 0);
138 }
139 
140 I2CSlave *at24c_eeprom_init_rom(I2CBus *bus, uint8_t address, uint32_t rom_size,
141                                 const uint8_t *init_rom, uint32_t init_rom_size)
142 {
143     EEPROMState *s;
144 
145     s = AT24C_EE(i2c_slave_new(TYPE_AT24C_EE, address));
146 
147     qdev_prop_set_uint32(DEVICE(s), "rom-size", rom_size);
148 
149     /* TODO: Model init_rom with QOM properties. */
150     s->init_rom = init_rom;
151     s->init_rom_size = init_rom_size;
152 
153     i2c_slave_realize_and_unref(I2C_SLAVE(s), bus, &error_abort);
154 
155     return I2C_SLAVE(s);
156 }
157 
158 static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
159 {
160     EEPROMState *ee = AT24C_EE(dev);
161 
162     if (ee->init_rom_size > ee->rsize) {
163         error_setg(errp, "%s: init rom is larger than rom: %u > %u",
164                    TYPE_AT24C_EE, ee->init_rom_size, ee->rsize);
165         return;
166     }
167 
168     if (ee->blk) {
169         int64_t len = blk_getlength(ee->blk);
170 
171         if (len != ee->rsize) {
172             error_setg(errp, "%s: Backing file size %" PRId64 " != %u",
173                        TYPE_AT24C_EE, len, ee->rsize);
174             return;
175         }
176 
177         if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
178                          BLK_PERM_ALL, &error_fatal) < 0)
179         {
180             error_setg(errp, "%s: Backing file incorrect permission",
181                        TYPE_AT24C_EE);
182             return;
183         }
184     }
185 
186     ee->mem = g_malloc0(ee->rsize);
187     memset(ee->mem, 0, ee->rsize);
188 
189     if (ee->init_rom) {
190         memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
191     }
192 
193     if (ee->blk) {
194         int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
195 
196         if (ret < 0) {
197             ERR(TYPE_AT24C_EE
198                     " : Failed initial sync with backing file\n");
199         }
200         DPRINTK("Reset read backing file\n");
201     }
202 }
203 
204 static
205 void at24c_eeprom_reset(DeviceState *state)
206 {
207     EEPROMState *ee = AT24C_EE(state);
208 
209     ee->changed = false;
210     ee->cur = 0;
211     ee->haveaddr = 0;
212 }
213 
214 static Property at24c_eeprom_props[] = {
215     DEFINE_PROP_UINT32("rom-size", EEPROMState, rsize, 0),
216     DEFINE_PROP_BOOL("writable", EEPROMState, writable, true),
217     DEFINE_PROP_DRIVE("drive", EEPROMState, blk),
218     DEFINE_PROP_END_OF_LIST()
219 };
220 
221 static
222 void at24c_eeprom_class_init(ObjectClass *klass, void *data)
223 {
224     DeviceClass *dc = DEVICE_CLASS(klass);
225     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
226 
227     dc->realize = &at24c_eeprom_realize;
228     k->event = &at24c_eeprom_event;
229     k->recv = &at24c_eeprom_recv;
230     k->send = &at24c_eeprom_send;
231 
232     device_class_set_props(dc, at24c_eeprom_props);
233     dc->reset = at24c_eeprom_reset;
234 }
235 
236 static
237 const TypeInfo at24c_eeprom_type = {
238     .name = TYPE_AT24C_EE,
239     .parent = TYPE_I2C_SLAVE,
240     .instance_size = sizeof(EEPROMState),
241     .class_size = sizeof(I2CSlaveClass),
242     .class_init = at24c_eeprom_class_init,
243 };
244 
245 static void at24c_eeprom_register(void)
246 {
247     type_register_static(&at24c_eeprom_type);
248 }
249 
250 type_init(at24c_eeprom_register)
251