| 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 "qemu/osdep.h" |
| 11 | |
| 12 | #include "qapi/error.h" |
| 13 | #include "qemu/error-report.h" |
| 14 | #include "qemu/module.h" |
| 15 | #include "hw/i2c/i2c.h" |
| 16 | #include "hw/nvram/eeprom_at24c.h" |
| 17 | #include "hw/core/qdev-properties.h" |
| 18 | #include "hw/core/qdev-properties-system.h" |
| 19 | #include "system/block-backend.h" |
| 20 | #include "qom/object.h" |
| 21 | |
| 22 | /* #define DEBUG_AT24C */ |
| 23 | |
| 24 | #ifdef DEBUG_AT24C |
| 25 | #define DPRINTK(FMT, ...) printf(TYPE_AT24C_EE " : " FMT, ## __VA_ARGS__) |
| 26 | #else |
| 27 | #define DPRINTK(FMT, ...) do {} while (0) |
| 28 | #endif |
| 29 | |
| 30 | #define TYPE_AT24C_EE "at24c-eeprom" |
| 31 | OBJECT_DECLARE_SIMPLE_TYPE(EEPROMState, AT24C_EE) |
| 32 | |
| 33 | struct EEPROMState { |
| 34 | I2CSlave parent_obj; |
| 35 | |
| 36 | /* address counter */ |
| 37 | uint16_t cur; |
| 38 | /* total size in bytes */ |
| 39 | uint32_t rsize; |
| 40 | /* |
| 41 | * address byte number |
| 42 | * for 24c01, 24c02 size <= 256 byte, use only 1 byte |
| 43 | * otherwise size > 256, use 2 byte |
| 44 | */ |
| 45 | uint8_t asize; |
| 46 | |
| 47 | bool writable; |
| 48 | /* cells changed since last START? */ |
| 49 | bool changed; |
| 50 | /* during WRITE, # of address bytes transferred */ |
| 51 | uint8_t haveaddr; |
| 52 | |
| 53 | uint8_t *mem; |
| 54 | |
| 55 | BlockBackend *blk; |
| 56 | |
| 57 | const uint8_t *init_rom; |
| 58 | uint32_t init_rom_size; |
| 59 | }; |
| 60 | |
| 61 | static |
| 62 | int at24c_eeprom_event(I2CSlave *s, enum i2c_event event) |
| 63 | { |
| 64 | EEPROMState *ee = AT24C_EE(s); |
| 65 | |
| 66 | switch (event) { |
| 67 | case I2C_START_SEND: |
| 68 | case I2C_FINISH: |
| 69 | ee->haveaddr = 0; |
| 70 | /* fallthrough */ |
| 71 | case I2C_START_RECV: |
| 72 | DPRINTK("clear\n"); |
| 73 | if (ee->blk && ee->changed) { |
| 74 | int ret = blk_pwrite(ee->blk, 0, ee->rsize, ee->mem, 0); |
| 75 | if (ret < 0) { |
| 76 | error_report("%s: failed to write backing file", __func__); |
| 77 | } |
| 78 | DPRINTK("Wrote to backing file\n"); |
| 79 | } |
| 80 | ee->changed = false; |
| 81 | break; |
| 82 | case I2C_NACK: |
| 83 | break; |
| 84 | default: |
| 85 | return -1; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static |
| 91 | uint8_t at24c_eeprom_recv(I2CSlave *s) |
| 92 | { |
| 93 | EEPROMState *ee = AT24C_EE(s); |
| 94 | uint8_t ret; |
| 95 | |
| 96 | /* |
| 97 | * If got the byte address but not completely with address size |
| 98 | * will return the invalid value |
| 99 | */ |
| 100 | if (ee->haveaddr > 0 && ee->haveaddr < ee->asize) { |
| 101 | return 0xff; |
| 102 | } |
| 103 | |
| 104 | ret = ee->mem[ee->cur]; |
| 105 | |
| 106 | ee->cur = (ee->cur + 1u) % ee->rsize; |
| 107 | DPRINTK("Recv %02x %c\n", ret, ret); |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static |
| 113 | int at24c_eeprom_send(I2CSlave *s, uint8_t data) |
| 114 | { |
| 115 | EEPROMState *ee = AT24C_EE(s); |
| 116 | |
| 117 | if (ee->haveaddr < ee->asize) { |
| 118 | ee->cur <<= 8; |
| 119 | ee->cur |= data; |
| 120 | ee->haveaddr++; |
| 121 | if (ee->haveaddr == ee->asize) { |
| 122 | ee->cur %= ee->rsize; |
| 123 | DPRINTK("Set pointer %04x\n", ee->cur); |
| 124 | } |
| 125 | |
| 126 | } else { |
| 127 | if (ee->writable) { |
| 128 | DPRINTK("Send %02x\n", data); |
| 129 | ee->mem[ee->cur] = data; |
| 130 | ee->changed = true; |
| 131 | } else { |
| 132 | DPRINTK("Send error %02x read-only\n", data); |
| 133 | } |
| 134 | ee->cur = (ee->cur + 1u) % ee->rsize; |
| 135 | |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | I2CSlave *at24c_eeprom_init(I2CBus *bus, uint8_t address, uint32_t rom_size) |
| 142 | { |
| 143 | return at24c_eeprom_init_rom(bus, address, rom_size, NULL, 0); |
| 144 | } |
| 145 | |
| 146 | I2CSlave *at24c_eeprom_init_rom(I2CBus *bus, uint8_t address, uint32_t rom_size, |
| 147 | const uint8_t *init_rom, uint32_t init_rom_size) |
| 148 | { |
| 149 | EEPROMState *s; |
| 150 | |
| 151 | s = AT24C_EE(i2c_slave_new(TYPE_AT24C_EE, address)); |
| 152 | |
| 153 | qdev_prop_set_uint32(DEVICE(s), "rom-size", rom_size); |
| 154 | |
| 155 | /* TODO: Model init_rom with QOM properties. */ |
| 156 | s->init_rom = init_rom; |
| 157 | s->init_rom_size = init_rom_size; |
| 158 | |
| 159 | i2c_slave_realize_and_unref(I2C_SLAVE(s), bus, &error_abort); |
| 160 | |
| 161 | return I2C_SLAVE(s); |
| 162 | } |
| 163 | |
| 164 | static void at24c_eeprom_realize(DeviceState *dev, Error **errp) |
| 165 | { |
| 166 | EEPROMState *ee = AT24C_EE(dev); |
| 167 | |
| 168 | if (ee->init_rom_size > ee->rsize) { |
| 169 | error_setg(errp, "%s: init rom is larger than rom: %u > %u", |
| 170 | TYPE_AT24C_EE, ee->init_rom_size, ee->rsize); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | if (ee->blk) { |
| 175 | int64_t len = blk_getlength(ee->blk); |
| 176 | |
| 177 | if (len != ee->rsize) { |
| 178 | error_setg(errp, "%s: Backing file size %" PRId64 " != %u", |
| 179 | TYPE_AT24C_EE, len, ee->rsize); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE, |
| 184 | BLK_PERM_ALL, &error_fatal) < 0) |
| 185 | { |
| 186 | error_setg(errp, "%s: Backing file incorrect permission", |
| 187 | TYPE_AT24C_EE); |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | ee->mem = g_malloc0(ee->rsize); |
| 193 | |
| 194 | if (ee->blk) { |
| 195 | int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0); |
| 196 | |
| 197 | if (ret < 0) { |
| 198 | error_setg(errp, "%s: Failed initial sync with backing file", |
| 199 | TYPE_AT24C_EE); |
| 200 | return; |
| 201 | } |
| 202 | DPRINTK("Reset read backing file\n"); |
| 203 | } else if (ee->init_rom) { |
| 204 | memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize)); |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * If address size didn't define with property set |
| 209 | * value is 0 as default, setting it by Rom size detecting. |
| 210 | */ |
| 211 | if (ee->asize == 0) { |
| 212 | if (ee->rsize <= 256) { |
| 213 | ee->asize = 1; |
| 214 | } else { |
| 215 | ee->asize = 2; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static |
| 221 | void at24c_eeprom_reset(DeviceState *state) |
| 222 | { |
| 223 | EEPROMState *ee = AT24C_EE(state); |
| 224 | |
| 225 | ee->changed = false; |
| 226 | ee->cur = 0; |
| 227 | ee->haveaddr = 0; |
| 228 | } |
| 229 | |
| 230 | static const Property at24c_eeprom_props[] = { |
| 231 | DEFINE_PROP_UINT32("rom-size", EEPROMState, rsize, 0), |
| 232 | DEFINE_PROP_UINT8("address-size", EEPROMState, asize, 0), |
| 233 | DEFINE_PROP_BOOL("writable", EEPROMState, writable, true), |
| 234 | DEFINE_PROP_DRIVE("drive", EEPROMState, blk), |
| 235 | }; |
| 236 | |
| 237 | static |
| 238 | void at24c_eeprom_class_init(ObjectClass *klass, const void *data) |
| 239 | { |
| 240 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 241 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); |
| 242 | |
| 243 | dc->realize = &at24c_eeprom_realize; |
| 244 | k->event = &at24c_eeprom_event; |
| 245 | k->recv = &at24c_eeprom_recv; |
| 246 | k->send = &at24c_eeprom_send; |
| 247 | |
| 248 | device_class_set_props(dc, at24c_eeprom_props); |
| 249 | device_class_set_legacy_reset(dc, at24c_eeprom_reset); |
| 250 | } |
| 251 | |
| 252 | static |
| 253 | const TypeInfo at24c_eeprom_type = { |
| 254 | .name = TYPE_AT24C_EE, |
| 255 | .parent = TYPE_I2C_SLAVE, |
| 256 | .instance_size = sizeof(EEPROMState), |
| 257 | .class_size = sizeof(I2CSlaveClass), |
| 258 | .class_init = at24c_eeprom_class_init, |
| 259 | }; |
| 260 | |
| 261 | static void at24c_eeprom_register(void) |
| 262 | { |
| 263 | type_register_static(&at24c_eeprom_type); |
| 264 | } |
| 265 | |
| 266 | type_init(at24c_eeprom_register) |