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