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