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"
14 #include "hw/i2c/i2c.h"
15 #include "sysemu/block-backend.h"
17 /* #define DEBUG_AT24C */
20 #define DPRINTK(FMT, ...) printf(TYPE_AT24C_EE " : " FMT, ## __VA_ARGS__)
22 #define DPRINTK(FMT, ...) do {} while (0)
25 #define ERR(FMT, ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \
28 #define TYPE_AT24C_EE "at24c-eeprom"
29 #define AT24C_EE(obj) OBJECT_CHECK(EEPROMState, (obj), TYPE_AT24C_EE)
31 typedef struct EEPROMState
{
36 /* total size in bytes */
39 /* cells changed since last START? */
41 /* during WRITE, # of address bytes transfered */
50 int at24c_eeprom_event(I2CSlave
*s
, enum i2c_event event
)
52 EEPROMState
*ee
= container_of(s
, EEPROMState
, parent_obj
);
60 if (ee
->blk
&& ee
->changed
) {
61 int len
= blk_pwrite(ee
->blk
, 0, ee
->mem
, ee
->rsize
, 0);
62 if (len
!= ee
->rsize
) {
64 " : failed to write backing file\n");
66 DPRINTK("Wrote to backing file\n");
77 uint8_t at24c_eeprom_recv(I2CSlave
*s
)
79 EEPROMState
*ee
= AT24C_EE(s
);
82 ret
= ee
->mem
[ee
->cur
];
84 ee
->cur
= (ee
->cur
+ 1u) % ee
->rsize
;
85 DPRINTK("Recv %02x %c\n", ret
, ret
);
91 int at24c_eeprom_send(I2CSlave
*s
, uint8_t data
)
93 EEPROMState
*ee
= AT24C_EE(s
);
95 if (ee
->haveaddr
< 2) {
99 if (ee
->haveaddr
== 2) {
100 ee
->cur
%= ee
->rsize
;
101 DPRINTK("Set pointer %04x\n", ee
->cur
);
106 DPRINTK("Send %02x\n", data
);
107 ee
->mem
[ee
->cur
] = data
;
110 DPRINTK("Send error %02x read-only\n", data
);
112 ee
->cur
= (ee
->cur
+ 1u) % ee
->rsize
;
119 static void at24c_eeprom_realize(DeviceState
*dev
, Error
**errp
)
121 EEPROMState
*ee
= AT24C_EE(dev
);
124 int64_t len
= blk_getlength(ee
->blk
);
126 if (len
!= ee
->rsize
) {
127 error_setg(errp
, "%s: Backing file size %" PRId64
" != %u",
128 TYPE_AT24C_EE
, len
, ee
->rsize
);
132 if (blk_set_perm(ee
->blk
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
,
133 BLK_PERM_ALL
, &error_fatal
) < 0)
135 error_setg(errp
, "%s: Backing file incorrect permission",
141 ee
->mem
= g_malloc0(ee
->rsize
);
145 void at24c_eeprom_reset(DeviceState
*state
)
147 EEPROMState
*ee
= AT24C_EE(state
);
153 memset(ee
->mem
, 0, ee
->rsize
);
156 int len
= blk_pread(ee
->blk
, 0, ee
->mem
, ee
->rsize
);
158 if (len
!= ee
->rsize
) {
160 " : Failed initial sync with backing file\n");
162 DPRINTK("Reset read backing file\n");
166 static Property at24c_eeprom_props
[] = {
167 DEFINE_PROP_UINT32("rom-size", EEPROMState
, rsize
, 0),
168 DEFINE_PROP_BOOL("writable", EEPROMState
, writable
, true),
169 DEFINE_PROP_DRIVE("drive", EEPROMState
, blk
),
170 DEFINE_PROP_END_OF_LIST()
174 void at24c_eeprom_class_init(ObjectClass
*klass
, void *data
)
176 DeviceClass
*dc
= DEVICE_CLASS(klass
);
177 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
179 dc
->realize
= &at24c_eeprom_realize
;
180 k
->event
= &at24c_eeprom_event
;
181 k
->recv
= &at24c_eeprom_recv
;
182 k
->send
= &at24c_eeprom_send
;
184 dc
->props
= at24c_eeprom_props
;
185 dc
->reset
= at24c_eeprom_reset
;
189 const TypeInfo at24c_eeprom_type
= {
190 .name
= TYPE_AT24C_EE
,
191 .parent
= TYPE_I2C_SLAVE
,
192 .instance_size
= sizeof(EEPROMState
),
193 .class_size
= sizeof(I2CSlaveClass
),
194 .class_init
= at24c_eeprom_class_init
,
197 static void at24c_eeprom_register(void)
199 type_register_static(&at24c_eeprom_type
);
202 type_init(at24c_eeprom_register
)