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/module.h" 14 #include "hw/i2c/i2c.h" 15 #include "hw/qdev-properties.h" 16 #include "sysemu/block-backend.h" 17 #include "qom/object.h" 18 19 /* #define DEBUG_AT24C */ 20 21 #ifdef DEBUG_AT24C 22 #define DPRINTK(FMT, ...) printf(TYPE_AT24C_EE " : " FMT, ## __VA_ARGS__) 23 #else 24 #define DPRINTK(FMT, ...) do {} while (0) 25 #endif 26 27 #define ERR(FMT, ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \ 28 ## __VA_ARGS__) 29 30 #define TYPE_AT24C_EE "at24c-eeprom" 31 typedef struct EEPROMState EEPROMState; 32 DECLARE_INSTANCE_CHECKER(EEPROMState, AT24C_EE, 33 TYPE_AT24C_EE) 34 35 struct EEPROMState { 36 I2CSlave parent_obj; 37 38 /* address counter */ 39 uint16_t cur; 40 /* total size in bytes */ 41 uint32_t rsize; 42 bool writable; 43 /* cells changed since last START? */ 44 bool changed; 45 /* during WRITE, # of address bytes transfered */ 46 uint8_t haveaddr; 47 48 uint8_t *mem; 49 50 BlockBackend *blk; 51 }; 52 53 static 54 int at24c_eeprom_event(I2CSlave *s, enum i2c_event event) 55 { 56 EEPROMState *ee = container_of(s, EEPROMState, parent_obj); 57 58 switch (event) { 59 case I2C_START_SEND: 60 case I2C_START_RECV: 61 case I2C_FINISH: 62 ee->haveaddr = 0; 63 DPRINTK("clear\n"); 64 if (ee->blk && ee->changed) { 65 int len = blk_pwrite(ee->blk, 0, ee->mem, ee->rsize, 0); 66 if (len != ee->rsize) { 67 ERR(TYPE_AT24C_EE 68 " : failed to write backing file\n"); 69 } 70 DPRINTK("Wrote to backing file\n"); 71 } 72 ee->changed = false; 73 break; 74 case I2C_NACK: 75 break; 76 } 77 return 0; 78 } 79 80 static 81 uint8_t at24c_eeprom_recv(I2CSlave *s) 82 { 83 EEPROMState *ee = AT24C_EE(s); 84 uint8_t ret; 85 86 ret = ee->mem[ee->cur]; 87 88 ee->cur = (ee->cur + 1u) % ee->rsize; 89 DPRINTK("Recv %02x %c\n", ret, ret); 90 91 return ret; 92 } 93 94 static 95 int at24c_eeprom_send(I2CSlave *s, uint8_t data) 96 { 97 EEPROMState *ee = AT24C_EE(s); 98 99 if (ee->haveaddr < 2) { 100 ee->cur <<= 8; 101 ee->cur |= data; 102 ee->haveaddr++; 103 if (ee->haveaddr == 2) { 104 ee->cur %= ee->rsize; 105 DPRINTK("Set pointer %04x\n", ee->cur); 106 } 107 108 } else { 109 if (ee->writable) { 110 DPRINTK("Send %02x\n", data); 111 ee->mem[ee->cur] = data; 112 ee->changed = true; 113 } else { 114 DPRINTK("Send error %02x read-only\n", data); 115 } 116 ee->cur = (ee->cur + 1u) % ee->rsize; 117 118 } 119 120 return 0; 121 } 122 123 static void at24c_eeprom_realize(DeviceState *dev, Error **errp) 124 { 125 EEPROMState *ee = AT24C_EE(dev); 126 127 if (ee->blk) { 128 int64_t len = blk_getlength(ee->blk); 129 130 if (len != ee->rsize) { 131 error_setg(errp, "%s: Backing file size %" PRId64 " != %u", 132 TYPE_AT24C_EE, len, ee->rsize); 133 return; 134 } 135 136 if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE, 137 BLK_PERM_ALL, &error_fatal) < 0) 138 { 139 error_setg(errp, "%s: Backing file incorrect permission", 140 TYPE_AT24C_EE); 141 return; 142 } 143 } 144 145 ee->mem = g_malloc0(ee->rsize); 146 } 147 148 static 149 void at24c_eeprom_reset(DeviceState *state) 150 { 151 EEPROMState *ee = AT24C_EE(state); 152 153 ee->changed = false; 154 ee->cur = 0; 155 ee->haveaddr = 0; 156 157 memset(ee->mem, 0, ee->rsize); 158 159 if (ee->blk) { 160 int len = blk_pread(ee->blk, 0, ee->mem, ee->rsize); 161 162 if (len != ee->rsize) { 163 ERR(TYPE_AT24C_EE 164 " : Failed initial sync with backing file\n"); 165 } 166 DPRINTK("Reset read backing file\n"); 167 } 168 } 169 170 static Property at24c_eeprom_props[] = { 171 DEFINE_PROP_UINT32("rom-size", EEPROMState, rsize, 0), 172 DEFINE_PROP_BOOL("writable", EEPROMState, writable, true), 173 DEFINE_PROP_DRIVE("drive", EEPROMState, blk), 174 DEFINE_PROP_END_OF_LIST() 175 }; 176 177 static 178 void at24c_eeprom_class_init(ObjectClass *klass, void *data) 179 { 180 DeviceClass *dc = DEVICE_CLASS(klass); 181 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); 182 183 dc->realize = &at24c_eeprom_realize; 184 k->event = &at24c_eeprom_event; 185 k->recv = &at24c_eeprom_recv; 186 k->send = &at24c_eeprom_send; 187 188 device_class_set_props(dc, at24c_eeprom_props); 189 dc->reset = at24c_eeprom_reset; 190 } 191 192 static 193 const TypeInfo at24c_eeprom_type = { 194 .name = TYPE_AT24C_EE, 195 .parent = TYPE_I2C_SLAVE, 196 .instance_size = sizeof(EEPROMState), 197 .class_size = sizeof(I2CSlaveClass), 198 .class_init = at24c_eeprom_class_init, 199 }; 200 201 static void at24c_eeprom_register(void) 202 { 203 type_register_static(&at24c_eeprom_type); 204 } 205 206 type_init(at24c_eeprom_register) 207