hw/arm: Extract at24c_eeprom_init helper from Aspeed and Nuvoton boards
[qemu/kevin.git] / hw / nvram / eeprom_at24c.c
blob98857e3626b98274967a5a5a6d65d42f7ef84472
1 /*
2 * *AT24C* series I2C EEPROM
4 * Copyright (c) 2015 Michael Davidsaver
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 */
10 #include "qemu/osdep.h"
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"
21 /* #define DEBUG_AT24C */
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
29 #define ERR(FMT, ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \
30 ## __VA_ARGS__)
32 #define TYPE_AT24C_EE "at24c-eeprom"
33 typedef struct EEPROMState EEPROMState;
34 DECLARE_INSTANCE_CHECKER(EEPROMState, AT24C_EE,
35 TYPE_AT24C_EE)
37 struct EEPROMState {
38 I2CSlave parent_obj;
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;
50 uint8_t *mem;
52 BlockBackend *blk;
55 static
56 int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)
58 EEPROMState *ee = AT24C_EE(s);
60 switch (event) {
61 case I2C_START_SEND:
62 case I2C_FINISH:
63 ee->haveaddr = 0;
64 /* fallthrough */
65 case I2C_START_RECV:
66 DPRINTK("clear\n");
67 if (ee->blk && ee->changed) {
68 int ret = blk_pwrite(ee->blk, 0, ee->rsize, ee->mem, 0);
69 if (ret < 0) {
70 ERR(TYPE_AT24C_EE
71 " : failed to write backing file\n");
73 DPRINTK("Wrote to backing file\n");
75 ee->changed = false;
76 break;
77 case I2C_NACK:
78 break;
79 default:
80 return -1;
82 return 0;
85 static
86 uint8_t at24c_eeprom_recv(I2CSlave *s)
88 EEPROMState *ee = AT24C_EE(s);
89 uint8_t ret;
91 if (ee->haveaddr == 1) {
92 return 0xff;
95 ret = ee->mem[ee->cur];
97 ee->cur = (ee->cur + 1u) % ee->rsize;
98 DPRINTK("Recv %02x %c\n", ret, ret);
100 return ret;
103 static
104 int at24c_eeprom_send(I2CSlave *s, uint8_t data)
106 EEPROMState *ee = AT24C_EE(s);
108 if (ee->haveaddr < 2) {
109 ee->cur <<= 8;
110 ee->cur |= data;
111 ee->haveaddr++;
112 if (ee->haveaddr == 2) {
113 ee->cur %= ee->rsize;
114 DPRINTK("Set pointer %04x\n", ee->cur);
117 } else {
118 if (ee->writable) {
119 DPRINTK("Send %02x\n", data);
120 ee->mem[ee->cur] = data;
121 ee->changed = true;
122 } else {
123 DPRINTK("Send error %02x read-only\n", data);
125 ee->cur = (ee->cur + 1u) % ee->rsize;
129 return 0;
132 I2CSlave *at24c_eeprom_init(I2CBus *bus, uint8_t address, uint32_t rom_size)
134 I2CSlave *i2c_dev = i2c_slave_new(TYPE_AT24C_EE, address);
135 DeviceState *dev = DEVICE(i2c_dev);
137 qdev_prop_set_uint32(dev, "rom-size", rom_size);
138 i2c_slave_realize_and_unref(i2c_dev, bus, &error_abort);
140 return i2c_dev;
143 static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
145 EEPROMState *ee = AT24C_EE(dev);
147 if (ee->blk) {
148 int64_t len = blk_getlength(ee->blk);
150 if (len != ee->rsize) {
151 error_setg(errp, "%s: Backing file size %" PRId64 " != %u",
152 TYPE_AT24C_EE, len, ee->rsize);
153 return;
156 if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
157 BLK_PERM_ALL, &error_fatal) < 0)
159 error_setg(errp, "%s: Backing file incorrect permission",
160 TYPE_AT24C_EE);
161 return;
165 ee->mem = g_malloc0(ee->rsize);
168 static
169 void at24c_eeprom_reset(DeviceState *state)
171 EEPROMState *ee = AT24C_EE(state);
173 ee->changed = false;
174 ee->cur = 0;
175 ee->haveaddr = 0;
177 memset(ee->mem, 0, ee->rsize);
179 if (ee->blk) {
180 int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
182 if (ret < 0) {
183 ERR(TYPE_AT24C_EE
184 " : Failed initial sync with backing file\n");
186 DPRINTK("Reset read backing file\n");
190 static Property at24c_eeprom_props[] = {
191 DEFINE_PROP_UINT32("rom-size", EEPROMState, rsize, 0),
192 DEFINE_PROP_BOOL("writable", EEPROMState, writable, true),
193 DEFINE_PROP_DRIVE("drive", EEPROMState, blk),
194 DEFINE_PROP_END_OF_LIST()
197 static
198 void at24c_eeprom_class_init(ObjectClass *klass, void *data)
200 DeviceClass *dc = DEVICE_CLASS(klass);
201 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
203 dc->realize = &at24c_eeprom_realize;
204 k->event = &at24c_eeprom_event;
205 k->recv = &at24c_eeprom_recv;
206 k->send = &at24c_eeprom_send;
208 device_class_set_props(dc, at24c_eeprom_props);
209 dc->reset = at24c_eeprom_reset;
212 static
213 const TypeInfo at24c_eeprom_type = {
214 .name = TYPE_AT24C_EE,
215 .parent = TYPE_I2C_SLAVE,
216 .instance_size = sizeof(EEPROMState),
217 .class_size = sizeof(I2CSlaveClass),
218 .class_init = at24c_eeprom_class_init,
221 static void at24c_eeprom_register(void)
223 type_register_static(&at24c_eeprom_type);
226 type_init(at24c_eeprom_register)