xref: /openbmc/qemu/hw/nvram/eeprom_at24c.c (revision 04a37d4c)
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/qdev-properties.h"
16 #include "hw/qdev-properties-system.h"
17 #include "sysemu/block-backend.h"
18 #include "qom/object.h"
19 
20 /* #define DEBUG_AT24C */
21 
22 #ifdef DEBUG_AT24C
23 #define DPRINTK(FMT, ...) printf(TYPE_AT24C_EE " : " FMT, ## __VA_ARGS__)
24 #else
25 #define DPRINTK(FMT, ...) do {} while (0)
26 #endif
27 
28 #define ERR(FMT, ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \
29                             ## __VA_ARGS__)
30 
31 #define TYPE_AT24C_EE "at24c-eeprom"
32 typedef struct EEPROMState EEPROMState;
33 DECLARE_INSTANCE_CHECKER(EEPROMState, AT24C_EE,
34                          TYPE_AT24C_EE)
35 
36 struct EEPROMState {
37     I2CSlave parent_obj;
38 
39     /* address counter */
40     uint16_t cur;
41     /* total size in bytes */
42     uint32_t rsize;
43     bool writable;
44     /* cells changed since last START? */
45     bool changed;
46     /* during WRITE, # of address bytes transfered */
47     uint8_t haveaddr;
48 
49     uint8_t *mem;
50 
51     BlockBackend *blk;
52 };
53 
54 static
55 int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)
56 {
57     EEPROMState *ee = container_of(s, EEPROMState, parent_obj);
58 
59     switch (event) {
60     case I2C_START_SEND:
61     case I2C_START_RECV:
62     case I2C_FINISH:
63         ee->haveaddr = 0;
64         DPRINTK("clear\n");
65         if (ee->blk && ee->changed) {
66             int len = blk_pwrite(ee->blk, 0, ee->mem, ee->rsize, 0);
67             if (len != ee->rsize) {
68                 ERR(TYPE_AT24C_EE
69                         " : failed to write backing file\n");
70             }
71             DPRINTK("Wrote to backing file\n");
72         }
73         ee->changed = false;
74         break;
75     case I2C_NACK:
76         break;
77     }
78     return 0;
79 }
80 
81 static
82 uint8_t at24c_eeprom_recv(I2CSlave *s)
83 {
84     EEPROMState *ee = AT24C_EE(s);
85     uint8_t ret;
86 
87     ret = ee->mem[ee->cur];
88 
89     ee->cur = (ee->cur + 1u) % ee->rsize;
90     DPRINTK("Recv %02x %c\n", ret, ret);
91 
92     return ret;
93 }
94 
95 static
96 int at24c_eeprom_send(I2CSlave *s, uint8_t data)
97 {
98     EEPROMState *ee = AT24C_EE(s);
99 
100     if (ee->haveaddr < 2) {
101         ee->cur <<= 8;
102         ee->cur |= data;
103         ee->haveaddr++;
104         if (ee->haveaddr == 2) {
105             ee->cur %= ee->rsize;
106             DPRINTK("Set pointer %04x\n", ee->cur);
107         }
108 
109     } else {
110         if (ee->writable) {
111             DPRINTK("Send %02x\n", data);
112             ee->mem[ee->cur] = data;
113             ee->changed = true;
114         } else {
115             DPRINTK("Send error %02x read-only\n", data);
116         }
117         ee->cur = (ee->cur + 1u) % ee->rsize;
118 
119     }
120 
121     return 0;
122 }
123 
124 static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
125 {
126     EEPROMState *ee = AT24C_EE(dev);
127 
128     if (ee->blk) {
129         int64_t len = blk_getlength(ee->blk);
130 
131         if (len != ee->rsize) {
132             error_setg(errp, "%s: Backing file size %" PRId64 " != %u",
133                        TYPE_AT24C_EE, len, ee->rsize);
134             return;
135         }
136 
137         if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
138                          BLK_PERM_ALL, &error_fatal) < 0)
139         {
140             error_setg(errp, "%s: Backing file incorrect permission",
141                        TYPE_AT24C_EE);
142             return;
143         }
144     }
145 
146     ee->mem = g_malloc0(ee->rsize);
147 }
148 
149 static
150 void at24c_eeprom_reset(DeviceState *state)
151 {
152     EEPROMState *ee = AT24C_EE(state);
153 
154     ee->changed = false;
155     ee->cur = 0;
156     ee->haveaddr = 0;
157 
158     memset(ee->mem, 0, ee->rsize);
159 
160     if (ee->blk) {
161         int len = blk_pread(ee->blk, 0, ee->mem, ee->rsize);
162 
163         if (len != ee->rsize) {
164             ERR(TYPE_AT24C_EE
165                     " : Failed initial sync with backing file\n");
166         }
167         DPRINTK("Reset read backing file\n");
168     }
169 }
170 
171 static Property at24c_eeprom_props[] = {
172     DEFINE_PROP_UINT32("rom-size", EEPROMState, rsize, 0),
173     DEFINE_PROP_BOOL("writable", EEPROMState, writable, true),
174     DEFINE_PROP_DRIVE("drive", EEPROMState, blk),
175     DEFINE_PROP_END_OF_LIST()
176 };
177 
178 static
179 void at24c_eeprom_class_init(ObjectClass *klass, void *data)
180 {
181     DeviceClass *dc = DEVICE_CLASS(klass);
182     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
183 
184     dc->realize = &at24c_eeprom_realize;
185     k->event = &at24c_eeprom_event;
186     k->recv = &at24c_eeprom_recv;
187     k->send = &at24c_eeprom_send;
188 
189     device_class_set_props(dc, at24c_eeprom_props);
190     dc->reset = at24c_eeprom_reset;
191 }
192 
193 static
194 const TypeInfo at24c_eeprom_type = {
195     .name = TYPE_AT24C_EE,
196     .parent = TYPE_I2C_SLAVE,
197     .instance_size = sizeof(EEPROMState),
198     .class_size = sizeof(I2CSlaveClass),
199     .class_init = at24c_eeprom_class_init,
200 };
201 
202 static void at24c_eeprom_register(void)
203 {
204     type_register_static(&at24c_eeprom_type);
205 }
206 
207 type_init(at24c_eeprom_register)
208