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.
10 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "qemu/module.h"
14 #include "hw/i2c/i2c.h"
15 #include "hw/qdev-properties.h"
16 #include "sysemu/block-backend.h"
18 /* #define DEBUG_AT24C */
21 #define DPRINTK(FMT, ...) printf(TYPE_AT24C_EE " : " FMT, ## __VA_ARGS__)
23 #define DPRINTK(FMT, ...) do {} while (0)
26 #define ERR(FMT, ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \
29 #define TYPE_AT24C_EE "at24c-eeprom"
30 #define AT24C_EE(obj) OBJECT_CHECK(EEPROMState, (obj), TYPE_AT24C_EE)
32 typedef struct EEPROMState
{
37 /* total size in bytes */
40 /* cells changed since last START? */
42 /* during WRITE, # of address bytes transfered */
51 int at24c_eeprom_event(I2CSlave
*s
, enum i2c_event event
)
53 EEPROMState
*ee
= container_of(s
, EEPROMState
, parent_obj
);
61 if (ee
->blk
&& ee
->changed
) {
62 int len
= blk_pwrite(ee
->blk
, 0, ee
->mem
, ee
->rsize
, 0);
63 if (len
!= ee
->rsize
) {
65 " : failed to write backing file\n");
67 DPRINTK("Wrote to backing file\n");
78 uint8_t at24c_eeprom_recv(I2CSlave
*s
)
80 EEPROMState
*ee
= AT24C_EE(s
);
83 ret
= ee
->mem
[ee
->cur
];
85 ee
->cur
= (ee
->cur
+ 1u) % ee
->rsize
;
86 DPRINTK("Recv %02x %c\n", ret
, ret
);
92 int at24c_eeprom_send(I2CSlave
*s
, uint8_t data
)
94 EEPROMState
*ee
= AT24C_EE(s
);
96 if (ee
->haveaddr
< 2) {
100 if (ee
->haveaddr
== 2) {
101 ee
->cur
%= ee
->rsize
;
102 DPRINTK("Set pointer %04x\n", ee
->cur
);
107 DPRINTK("Send %02x\n", data
);
108 ee
->mem
[ee
->cur
] = data
;
111 DPRINTK("Send error %02x read-only\n", data
);
113 ee
->cur
= (ee
->cur
+ 1u) % ee
->rsize
;
120 static void at24c_eeprom_realize(DeviceState
*dev
, Error
**errp
)
122 EEPROMState
*ee
= AT24C_EE(dev
);
125 int64_t len
= blk_getlength(ee
->blk
);
127 if (len
!= ee
->rsize
) {
128 error_setg(errp
, "%s: Backing file size %" PRId64
" != %u",
129 TYPE_AT24C_EE
, len
, ee
->rsize
);
133 if (blk_set_perm(ee
->blk
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
,
134 BLK_PERM_ALL
, &error_fatal
) < 0)
136 error_setg(errp
, "%s: Backing file incorrect permission",
142 ee
->mem
= g_malloc0(ee
->rsize
);
146 void at24c_eeprom_reset(DeviceState
*state
)
148 EEPROMState
*ee
= AT24C_EE(state
);
154 memset(ee
->mem
, 0, ee
->rsize
);
157 int len
= blk_pread(ee
->blk
, 0, ee
->mem
, ee
->rsize
);
159 if (len
!= ee
->rsize
) {
161 " : Failed initial sync with backing file\n");
163 DPRINTK("Reset read backing file\n");
167 static Property at24c_eeprom_props
[] = {
168 DEFINE_PROP_UINT32("rom-size", EEPROMState
, rsize
, 0),
169 DEFINE_PROP_BOOL("writable", EEPROMState
, writable
, true),
170 DEFINE_PROP_DRIVE("drive", EEPROMState
, blk
),
171 DEFINE_PROP_END_OF_LIST()
175 void at24c_eeprom_class_init(ObjectClass
*klass
, void *data
)
177 DeviceClass
*dc
= DEVICE_CLASS(klass
);
178 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
180 dc
->realize
= &at24c_eeprom_realize
;
181 k
->event
= &at24c_eeprom_event
;
182 k
->recv
= &at24c_eeprom_recv
;
183 k
->send
= &at24c_eeprom_send
;
185 dc
->props
= at24c_eeprom_props
;
186 dc
->reset
= at24c_eeprom_reset
;
190 const TypeInfo at24c_eeprom_type
= {
191 .name
= TYPE_AT24C_EE
,
192 .parent
= TYPE_I2C_SLAVE
,
193 .instance_size
= sizeof(EEPROMState
),
194 .class_size
= sizeof(I2CSlaveClass
),
195 .class_init
= at24c_eeprom_class_init
,
198 static void at24c_eeprom_register(void)
200 type_register_static(&at24c_eeprom_type
);
203 type_init(at24c_eeprom_register
)