2017-11-20 06:24:17 +03:00
|
|
|
/*
|
|
|
|
* *AT24C* series I2C EEPROM
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Michael Davidsaver
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the LICENSE file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2018-02-01 14:18:29 +03:00
|
|
|
|
2017-11-20 06:24:17 +03:00
|
|
|
#include "qapi/error.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2017-11-20 06:24:17 +03:00
|
|
|
#include "hw/i2c/i2c.h"
|
2023-02-07 11:02:04 +03:00
|
|
|
#include "hw/nvram/eeprom_at24c.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2020-12-12 01:05:12 +03:00
|
|
|
#include "hw/qdev-properties-system.h"
|
2017-11-20 06:24:17 +03:00
|
|
|
#include "sysemu/block-backend.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2017-11-20 06:24:17 +03:00
|
|
|
|
|
|
|
/* #define DEBUG_AT24C */
|
|
|
|
|
|
|
|
#ifdef DEBUG_AT24C
|
|
|
|
#define DPRINTK(FMT, ...) printf(TYPE_AT24C_EE " : " FMT, ## __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DPRINTK(FMT, ...) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ERR(FMT, ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \
|
|
|
|
## __VA_ARGS__)
|
|
|
|
|
|
|
|
#define TYPE_AT24C_EE "at24c-eeprom"
|
2020-09-03 23:43:22 +03:00
|
|
|
typedef struct EEPROMState EEPROMState;
|
2020-09-01 00:07:33 +03:00
|
|
|
DECLARE_INSTANCE_CHECKER(EEPROMState, AT24C_EE,
|
|
|
|
TYPE_AT24C_EE)
|
2017-11-20 06:24:17 +03:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct EEPROMState {
|
2017-11-20 06:24:17 +03:00
|
|
|
I2CSlave parent_obj;
|
|
|
|
|
|
|
|
/* address counter */
|
|
|
|
uint16_t cur;
|
|
|
|
/* total size in bytes */
|
|
|
|
uint32_t rsize;
|
|
|
|
bool writable;
|
|
|
|
/* cells changed since last START? */
|
|
|
|
bool changed;
|
|
|
|
/* during WRITE, # of address bytes transfered */
|
|
|
|
uint8_t haveaddr;
|
|
|
|
|
|
|
|
uint8_t *mem;
|
|
|
|
|
|
|
|
BlockBackend *blk;
|
2023-02-07 11:02:04 +03:00
|
|
|
|
|
|
|
const uint8_t *init_rom;
|
|
|
|
uint32_t init_rom_size;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2017-11-20 06:24:17 +03:00
|
|
|
|
|
|
|
static
|
|
|
|
int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)
|
|
|
|
{
|
2022-01-20 00:43:29 +03:00
|
|
|
EEPROMState *ee = AT24C_EE(s);
|
2017-11-20 06:24:17 +03:00
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case I2C_START_SEND:
|
|
|
|
case I2C_FINISH:
|
|
|
|
ee->haveaddr = 0;
|
2021-12-21 00:21:37 +03:00
|
|
|
/* fallthrough */
|
|
|
|
case I2C_START_RECV:
|
2017-11-20 06:24:17 +03:00
|
|
|
DPRINTK("clear\n");
|
|
|
|
if (ee->blk && ee->changed) {
|
block: Change blk_{pread,pwrite}() param order
Swap 'buf' and 'bytes' around for consistency with
blk_co_{pread,pwrite}(), and in preparation to implement these functions
using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pread(blk, offset, buf, bytes, flags)
+ blk_pread(blk, offset, bytes, buf, flags)
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pwrite(blk, offset, buf, bytes, flags)
+ blk_pwrite(blk, offset, bytes, buf, flags)
It had no effect on hw/block/nand.c, presumably due to the #if, so that
file was updated manually.
Overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20220705161527.1054072-4-afaria@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-07-05 19:15:11 +03:00
|
|
|
int ret = blk_pwrite(ee->blk, 0, ee->rsize, ee->mem, 0);
|
2022-07-05 19:15:09 +03:00
|
|
|
if (ret < 0) {
|
2017-11-20 06:24:17 +03:00
|
|
|
ERR(TYPE_AT24C_EE
|
|
|
|
" : failed to write backing file\n");
|
|
|
|
}
|
|
|
|
DPRINTK("Wrote to backing file\n");
|
|
|
|
}
|
|
|
|
ee->changed = false;
|
|
|
|
break;
|
|
|
|
case I2C_NACK:
|
|
|
|
break;
|
2022-06-30 10:21:14 +03:00
|
|
|
default:
|
|
|
|
return -1;
|
2017-11-20 06:24:17 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2018-11-14 20:50:50 +03:00
|
|
|
uint8_t at24c_eeprom_recv(I2CSlave *s)
|
2017-11-20 06:24:17 +03:00
|
|
|
{
|
|
|
|
EEPROMState *ee = AT24C_EE(s);
|
2018-11-14 20:50:50 +03:00
|
|
|
uint8_t ret;
|
2017-11-20 06:24:17 +03:00
|
|
|
|
2021-12-21 00:21:37 +03:00
|
|
|
if (ee->haveaddr == 1) {
|
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:24:17 +03:00
|
|
|
ret = ee->mem[ee->cur];
|
|
|
|
|
|
|
|
ee->cur = (ee->cur + 1u) % ee->rsize;
|
|
|
|
DPRINTK("Recv %02x %c\n", ret, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int at24c_eeprom_send(I2CSlave *s, uint8_t data)
|
|
|
|
{
|
|
|
|
EEPROMState *ee = AT24C_EE(s);
|
|
|
|
|
|
|
|
if (ee->haveaddr < 2) {
|
|
|
|
ee->cur <<= 8;
|
|
|
|
ee->cur |= data;
|
|
|
|
ee->haveaddr++;
|
|
|
|
if (ee->haveaddr == 2) {
|
|
|
|
ee->cur %= ee->rsize;
|
|
|
|
DPRINTK("Set pointer %04x\n", ee->cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (ee->writable) {
|
|
|
|
DPRINTK("Send %02x\n", data);
|
|
|
|
ee->mem[ee->cur] = data;
|
|
|
|
ee->changed = true;
|
|
|
|
} else {
|
|
|
|
DPRINTK("Send error %02x read-only\n", data);
|
|
|
|
}
|
|
|
|
ee->cur = (ee->cur + 1u) % ee->rsize;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-07 11:02:04 +03:00
|
|
|
I2CSlave *at24c_eeprom_init(I2CBus *bus, uint8_t address, uint32_t rom_size)
|
|
|
|
{
|
2023-02-07 11:02:04 +03:00
|
|
|
return at24c_eeprom_init_rom(bus, address, rom_size, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CSlave *at24c_eeprom_init_rom(I2CBus *bus, uint8_t address, uint32_t rom_size,
|
|
|
|
const uint8_t *init_rom, uint32_t init_rom_size)
|
|
|
|
{
|
|
|
|
EEPROMState *s;
|
|
|
|
|
|
|
|
s = AT24C_EE(i2c_slave_new(TYPE_AT24C_EE, address));
|
|
|
|
|
|
|
|
qdev_prop_set_uint32(DEVICE(s), "rom-size", rom_size);
|
2023-02-07 11:02:04 +03:00
|
|
|
|
2023-02-07 11:02:04 +03:00
|
|
|
/* TODO: Model init_rom with QOM properties. */
|
|
|
|
s->init_rom = init_rom;
|
|
|
|
s->init_rom_size = init_rom_size;
|
2023-02-07 11:02:04 +03:00
|
|
|
|
2023-02-07 11:02:04 +03:00
|
|
|
i2c_slave_realize_and_unref(I2C_SLAVE(s), bus, &error_abort);
|
|
|
|
|
|
|
|
return I2C_SLAVE(s);
|
2023-02-07 11:02:04 +03:00
|
|
|
}
|
|
|
|
|
2018-05-28 17:45:07 +03:00
|
|
|
static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
|
2017-11-20 06:24:17 +03:00
|
|
|
{
|
2018-05-28 17:45:07 +03:00
|
|
|
EEPROMState *ee = AT24C_EE(dev);
|
2017-11-20 06:24:17 +03:00
|
|
|
|
2023-02-07 11:02:04 +03:00
|
|
|
if (ee->init_rom_size > ee->rsize) {
|
|
|
|
error_setg(errp, "%s: init rom is larger than rom: %u > %u",
|
|
|
|
TYPE_AT24C_EE, ee->init_rom_size, ee->rsize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:24:17 +03:00
|
|
|
if (ee->blk) {
|
|
|
|
int64_t len = blk_getlength(ee->blk);
|
|
|
|
|
|
|
|
if (len != ee->rsize) {
|
2018-05-28 17:45:07 +03:00
|
|
|
error_setg(errp, "%s: Backing file size %" PRId64 " != %u",
|
|
|
|
TYPE_AT24C_EE, len, ee->rsize);
|
|
|
|
return;
|
2017-11-20 06:24:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
|
|
|
|
BLK_PERM_ALL, &error_fatal) < 0)
|
|
|
|
{
|
2018-05-28 17:45:07 +03:00
|
|
|
error_setg(errp, "%s: Backing file incorrect permission",
|
|
|
|
TYPE_AT24C_EE);
|
|
|
|
return;
|
2017-11-20 06:24:17 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-28 17:45:07 +03:00
|
|
|
|
|
|
|
ee->mem = g_malloc0(ee->rsize);
|
2017-11-20 06:24:17 +03:00
|
|
|
memset(ee->mem, 0, ee->rsize);
|
|
|
|
|
2023-02-07 11:02:04 +03:00
|
|
|
if (ee->init_rom) {
|
|
|
|
memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:24:17 +03:00
|
|
|
if (ee->blk) {
|
block: Change blk_{pread,pwrite}() param order
Swap 'buf' and 'bytes' around for consistency with
blk_co_{pread,pwrite}(), and in preparation to implement these functions
using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pread(blk, offset, buf, bytes, flags)
+ blk_pread(blk, offset, bytes, buf, flags)
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pwrite(blk, offset, buf, bytes, flags)
+ blk_pwrite(blk, offset, bytes, buf, flags)
It had no effect on hw/block/nand.c, presumably due to the #if, so that
file was updated manually.
Overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20220705161527.1054072-4-afaria@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-07-05 19:15:11 +03:00
|
|
|
int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
|
2017-11-20 06:24:17 +03:00
|
|
|
|
2022-07-05 19:15:09 +03:00
|
|
|
if (ret < 0) {
|
2017-11-20 06:24:17 +03:00
|
|
|
ERR(TYPE_AT24C_EE
|
|
|
|
" : Failed initial sync with backing file\n");
|
|
|
|
}
|
|
|
|
DPRINTK("Reset read backing file\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 11:02:05 +03:00
|
|
|
static
|
|
|
|
void at24c_eeprom_reset(DeviceState *state)
|
|
|
|
{
|
|
|
|
EEPROMState *ee = AT24C_EE(state);
|
|
|
|
|
|
|
|
ee->changed = false;
|
|
|
|
ee->cur = 0;
|
|
|
|
ee->haveaddr = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:24:17 +03:00
|
|
|
static Property at24c_eeprom_props[] = {
|
|
|
|
DEFINE_PROP_UINT32("rom-size", EEPROMState, rsize, 0),
|
|
|
|
DEFINE_PROP_BOOL("writable", EEPROMState, writable, true),
|
|
|
|
DEFINE_PROP_DRIVE("drive", EEPROMState, blk),
|
|
|
|
DEFINE_PROP_END_OF_LIST()
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
void at24c_eeprom_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
|
|
|
|
|
2018-05-28 17:45:07 +03:00
|
|
|
dc->realize = &at24c_eeprom_realize;
|
2017-11-20 06:24:17 +03:00
|
|
|
k->event = &at24c_eeprom_event;
|
|
|
|
k->recv = &at24c_eeprom_recv;
|
|
|
|
k->send = &at24c_eeprom_send;
|
|
|
|
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, at24c_eeprom_props);
|
2017-11-20 06:24:17 +03:00
|
|
|
dc->reset = at24c_eeprom_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
const TypeInfo at24c_eeprom_type = {
|
|
|
|
.name = TYPE_AT24C_EE,
|
|
|
|
.parent = TYPE_I2C_SLAVE,
|
|
|
|
.instance_size = sizeof(EEPROMState),
|
|
|
|
.class_size = sizeof(I2CSlaveClass),
|
|
|
|
.class_init = at24c_eeprom_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void at24c_eeprom_register(void)
|
|
|
|
{
|
|
|
|
type_register_static(&at24c_eeprom_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(at24c_eeprom_register)
|