1663e8e51Sths /* 2663e8e51Sths * QEMU EEPROM 93xx emulation 3663e8e51Sths * 4663e8e51Sths * Copyright (c) 2006-2007 Stefan Weil 5663e8e51Sths * 6663e8e51Sths * This program is free software; you can redistribute it and/or modify 7663e8e51Sths * it under the terms of the GNU General Public License as published by 8663e8e51Sths * the Free Software Foundation; either version 2 of the License, or 9663e8e51Sths * (at your option) any later version. 10663e8e51Sths * 11663e8e51Sths * This program is distributed in the hope that it will be useful, 12663e8e51Sths * but WITHOUT ANY WARRANTY; without even the implied warranty of 13663e8e51Sths * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14663e8e51Sths * GNU General Public License for more details. 15663e8e51Sths * 16663e8e51Sths * You should have received a copy of the GNU General Public License 178167ee88SBlue Swirl * along with this program; if not, see <http://www.gnu.org/licenses/>. 18663e8e51Sths */ 19663e8e51Sths 20663e8e51Sths /* Emulation for serial EEPROMs: 21663e8e51Sths * NMC93C06 256-Bit (16 x 16) 22663e8e51Sths * NMC93C46 1024-Bit (64 x 16) 23663e8e51Sths * NMC93C56 2028 Bit (128 x 16) 24663e8e51Sths * NMC93C66 4096 Bit (256 x 16) 25663e8e51Sths * Compatible devices include FM93C46 and others. 26663e8e51Sths * 27663e8e51Sths * Other drivers use these interface functions: 28663e8e51Sths * eeprom93xx_new - add a new EEPROM (with 16, 64 or 256 words) 29663e8e51Sths * eeprom93xx_free - destroy EEPROM 30663e8e51Sths * eeprom93xx_read - read data from the EEPROM 31663e8e51Sths * eeprom93xx_write - write data to the EEPROM 32663e8e51Sths * eeprom93xx_data - get EEPROM data array for external manipulation 33663e8e51Sths * 34663e8e51Sths * Todo list: 35663e8e51Sths * - No emulation of EEPROM timings. 36663e8e51Sths */ 37663e8e51Sths 380430891cSPeter Maydell #include "qemu/osdep.h" 3983c9f4caSPaolo Bonzini #include "hw/hw.h" 400d09e41aSPaolo Bonzini #include "hw/nvram/eeprom93xx.h" 41*ca77ee28SMarkus Armbruster #include "migration/qemu-file-types.h" 42663e8e51Sths 43663e8e51Sths /* Debug EEPROM emulation. */ 44663e8e51Sths //~ #define DEBUG_EEPROM 45663e8e51Sths 46663e8e51Sths #ifdef DEBUG_EEPROM 47001faf32SBlue Swirl #define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__) 48663e8e51Sths #else 49001faf32SBlue Swirl #define logout(fmt, ...) ((void)0) 50663e8e51Sths #endif 51663e8e51Sths 527ab2589cSaurel32 #define EEPROM_INSTANCE 0 537ab2589cSaurel32 #define OLD_EEPROM_VERSION 20061112 547ab2589cSaurel32 #define EEPROM_VERSION (OLD_EEPROM_VERSION + 1) 55663e8e51Sths 56663e8e51Sths #if 0 57663e8e51Sths typedef enum { 58663e8e51Sths eeprom_read = 0x80, /* read register xx */ 59663e8e51Sths eeprom_write = 0x40, /* write register xx */ 60663e8e51Sths eeprom_erase = 0xc0, /* erase register xx */ 61663e8e51Sths eeprom_ewen = 0x30, /* erase / write enable */ 62663e8e51Sths eeprom_ewds = 0x00, /* erase / write disable */ 63663e8e51Sths eeprom_eral = 0x20, /* erase all registers */ 64663e8e51Sths eeprom_wral = 0x10, /* write all registers */ 65663e8e51Sths eeprom_amask = 0x0f, 66663e8e51Sths eeprom_imask = 0xf0 67663e8e51Sths } eeprom_instruction_t; 68663e8e51Sths #endif 69663e8e51Sths 70663e8e51Sths #ifdef DEBUG_EEPROM 71663e8e51Sths static const char *opstring[] = { 72663e8e51Sths "extended", "write", "read", "erase" 73663e8e51Sths }; 74663e8e51Sths #endif 75663e8e51Sths 76c227f099SAnthony Liguori struct _eeprom_t { 77663e8e51Sths uint8_t tick; 78663e8e51Sths uint8_t address; 79663e8e51Sths uint8_t command; 80ebabb67aSStefan Weil uint8_t writable; 81663e8e51Sths 82663e8e51Sths uint8_t eecs; 83663e8e51Sths uint8_t eesk; 84663e8e51Sths uint8_t eedo; 85663e8e51Sths 86663e8e51Sths uint8_t addrbits; 877ab2589cSaurel32 uint16_t size; 88663e8e51Sths uint16_t data; 89663e8e51Sths uint16_t contents[0]; 90663e8e51Sths }; 91663e8e51Sths 92663e8e51Sths /* Code for saving and restoring of EEPROM state. */ 93663e8e51Sths 94c4a0f2d3SJuan Quintela /* Restore an uint16_t from an uint8_t 95c4a0f2d3SJuan Quintela This is a Big hack, but it is how the old state did it. 96c4a0f2d3SJuan Quintela */ 97c4a0f2d3SJuan Quintela 982c21ee76SJianjun Duan static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size, 9903fee66fSMarc-André Lureau const VMStateField *field) 100663e8e51Sths { 101c4a0f2d3SJuan Quintela uint16_t *v = pv; 102c4a0f2d3SJuan Quintela *v = qemu_get_ubyte(f); 103c4a0f2d3SJuan Quintela return 0; 104663e8e51Sths } 105663e8e51Sths 10603fee66fSMarc-André Lureau static int put_unused(QEMUFile *f, void *pv, size_t size, 10703fee66fSMarc-André Lureau const VMStateField *field, QJSON *vmdesc) 108663e8e51Sths { 109c4a0f2d3SJuan Quintela fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n"); 110c4a0f2d3SJuan Quintela fprintf(stderr, "Never should be used to write a new state.\n"); 111c4a0f2d3SJuan Quintela exit(0); 1122c21ee76SJianjun Duan 1132c21ee76SJianjun Duan return 0; 1147ab2589cSaurel32 } 115d4ae799cSaurel32 116d05ac8faSBlue Swirl static const VMStateInfo vmstate_hack_uint16_from_uint8 = { 117c4a0f2d3SJuan Quintela .name = "uint16_from_uint8", 118c4a0f2d3SJuan Quintela .get = get_uint16_from_uint8, 119c4a0f2d3SJuan Quintela .put = put_unused, 120c4a0f2d3SJuan Quintela }; 121c4a0f2d3SJuan Quintela 122c4a0f2d3SJuan Quintela #define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \ 123c4a0f2d3SJuan Quintela VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t) 124c4a0f2d3SJuan Quintela 125c4a0f2d3SJuan Quintela static bool is_old_eeprom_version(void *opaque, int version_id) 126c4a0f2d3SJuan Quintela { 127c4a0f2d3SJuan Quintela return version_id == OLD_EEPROM_VERSION; 128663e8e51Sths } 129c4a0f2d3SJuan Quintela 130c4a0f2d3SJuan Quintela static const VMStateDescription vmstate_eeprom = { 131c4a0f2d3SJuan Quintela .name = "eeprom", 132c4a0f2d3SJuan Quintela .version_id = EEPROM_VERSION, 133c4a0f2d3SJuan Quintela .minimum_version_id = OLD_EEPROM_VERSION, 134c4a0f2d3SJuan Quintela .fields = (VMStateField[]) { 135c4a0f2d3SJuan Quintela VMSTATE_UINT8(tick, eeprom_t), 136c4a0f2d3SJuan Quintela VMSTATE_UINT8(address, eeprom_t), 137c4a0f2d3SJuan Quintela VMSTATE_UINT8(command, eeprom_t), 138ebabb67aSStefan Weil VMSTATE_UINT8(writable, eeprom_t), 139c4a0f2d3SJuan Quintela 140c4a0f2d3SJuan Quintela VMSTATE_UINT8(eecs, eeprom_t), 141c4a0f2d3SJuan Quintela VMSTATE_UINT8(eesk, eeprom_t), 142c4a0f2d3SJuan Quintela VMSTATE_UINT8(eedo, eeprom_t), 143c4a0f2d3SJuan Quintela 144c4a0f2d3SJuan Quintela VMSTATE_UINT8(addrbits, eeprom_t), 145c4a0f2d3SJuan Quintela VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version), 146c4a0f2d3SJuan Quintela VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1), 147d2164ad3SHalil Pasic VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION, NULL), 148c4a0f2d3SJuan Quintela VMSTATE_UINT16(data, eeprom_t), 149c4a0f2d3SJuan Quintela VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0, 150c4a0f2d3SJuan Quintela vmstate_info_uint16, uint16_t), 151c4a0f2d3SJuan Quintela VMSTATE_END_OF_LIST() 152663e8e51Sths } 153c4a0f2d3SJuan Quintela }; 154663e8e51Sths 155c227f099SAnthony Liguori void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi) 156663e8e51Sths { 157663e8e51Sths uint8_t tick = eeprom->tick; 158663e8e51Sths uint8_t eedo = eeprom->eedo; 159663e8e51Sths uint16_t address = eeprom->address; 160663e8e51Sths uint8_t command = eeprom->command; 161663e8e51Sths 162663e8e51Sths logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n", 163663e8e51Sths eecs, eesk, eedi, eedo, tick); 164663e8e51Sths 165663e8e51Sths if (!eeprom->eecs && eecs) { 166663e8e51Sths /* Start chip select cycle. */ 167663e8e51Sths logout("Cycle start, waiting for 1st start bit (0)\n"); 168663e8e51Sths tick = 0; 169663e8e51Sths command = 0x0; 170663e8e51Sths address = 0x0; 171663e8e51Sths } else if (eeprom->eecs && !eecs) { 172663e8e51Sths /* End chip select cycle. This triggers write / erase. */ 173ebabb67aSStefan Weil if (eeprom->writable) { 174663e8e51Sths uint8_t subcommand = address >> (eeprom->addrbits - 2); 175663e8e51Sths if (command == 0 && subcommand == 2) { 176663e8e51Sths /* Erase all. */ 177663e8e51Sths for (address = 0; address < eeprom->size; address++) { 178663e8e51Sths eeprom->contents[address] = 0xffff; 179663e8e51Sths } 180663e8e51Sths } else if (command == 3) { 181663e8e51Sths /* Erase word. */ 182663e8e51Sths eeprom->contents[address] = 0xffff; 183663e8e51Sths } else if (tick >= 2 + 2 + eeprom->addrbits + 16) { 184663e8e51Sths if (command == 1) { 185663e8e51Sths /* Write word. */ 186663e8e51Sths eeprom->contents[address] &= eeprom->data; 187663e8e51Sths } else if (command == 0 && subcommand == 1) { 188663e8e51Sths /* Write all. */ 189663e8e51Sths for (address = 0; address < eeprom->size; address++) { 190663e8e51Sths eeprom->contents[address] &= eeprom->data; 191663e8e51Sths } 192663e8e51Sths } 193663e8e51Sths } 194663e8e51Sths } 195663e8e51Sths /* Output DO is tristate, read results in 1. */ 196663e8e51Sths eedo = 1; 197663e8e51Sths } else if (eecs && !eeprom->eesk && eesk) { 198663e8e51Sths /* Raising edge of clock shifts data in. */ 199663e8e51Sths if (tick == 0) { 200663e8e51Sths /* Wait for 1st start bit. */ 201663e8e51Sths if (eedi == 0) { 202663e8e51Sths logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n"); 203663e8e51Sths tick++; 204663e8e51Sths } else { 205663e8e51Sths logout("wrong 1st start bit (is 1, should be 0)\n"); 206663e8e51Sths tick = 2; 207663e8e51Sths //~ assert(!"wrong start bit"); 208663e8e51Sths } 209663e8e51Sths } else if (tick == 1) { 210663e8e51Sths /* Wait for 2nd start bit. */ 211663e8e51Sths if (eedi != 0) { 212663e8e51Sths logout("Got correct 2nd start bit, getting command + address\n"); 213663e8e51Sths tick++; 214663e8e51Sths } else { 215663e8e51Sths logout("1st start bit is longer than needed\n"); 216663e8e51Sths } 217663e8e51Sths } else if (tick < 2 + 2) { 218663e8e51Sths /* Got 2 start bits, transfer 2 opcode bits. */ 219663e8e51Sths tick++; 220663e8e51Sths command <<= 1; 221663e8e51Sths if (eedi) { 222663e8e51Sths command += 1; 223663e8e51Sths } 224663e8e51Sths } else if (tick < 2 + 2 + eeprom->addrbits) { 225663e8e51Sths /* Got 2 start bits and 2 opcode bits, transfer all address bits. */ 226663e8e51Sths tick++; 227663e8e51Sths address = ((address << 1) | eedi); 228663e8e51Sths if (tick == 2 + 2 + eeprom->addrbits) { 229663e8e51Sths logout("%s command, address = 0x%02x (value 0x%04x)\n", 230663e8e51Sths opstring[command], address, eeprom->contents[address]); 231663e8e51Sths if (command == 2) { 232663e8e51Sths eedo = 0; 233663e8e51Sths } 234663e8e51Sths address = address % eeprom->size; 235663e8e51Sths if (command == 0) { 236663e8e51Sths /* Command code in upper 2 bits of address. */ 237663e8e51Sths switch (address >> (eeprom->addrbits - 2)) { 238663e8e51Sths case 0: 239663e8e51Sths logout("write disable command\n"); 240ebabb67aSStefan Weil eeprom->writable = 0; 241663e8e51Sths break; 242663e8e51Sths case 1: 243663e8e51Sths logout("write all command\n"); 244663e8e51Sths break; 245663e8e51Sths case 2: 246663e8e51Sths logout("erase all command\n"); 247663e8e51Sths break; 248663e8e51Sths case 3: 249663e8e51Sths logout("write enable command\n"); 250ebabb67aSStefan Weil eeprom->writable = 1; 251663e8e51Sths break; 252663e8e51Sths } 253663e8e51Sths } else { 254663e8e51Sths /* Read, write or erase word. */ 255663e8e51Sths eeprom->data = eeprom->contents[address]; 256663e8e51Sths } 257663e8e51Sths } 258663e8e51Sths } else if (tick < 2 + 2 + eeprom->addrbits + 16) { 259663e8e51Sths /* Transfer 16 data bits. */ 260663e8e51Sths tick++; 261663e8e51Sths if (command == 2) { 262663e8e51Sths /* Read word. */ 263663e8e51Sths eedo = ((eeprom->data & 0x8000) != 0); 264663e8e51Sths } 265663e8e51Sths eeprom->data <<= 1; 266663e8e51Sths eeprom->data += eedi; 267663e8e51Sths } else { 268663e8e51Sths logout("additional unneeded tick, not processed\n"); 269663e8e51Sths } 270663e8e51Sths } 271663e8e51Sths /* Save status of EEPROM. */ 272663e8e51Sths eeprom->tick = tick; 273663e8e51Sths eeprom->eecs = eecs; 274663e8e51Sths eeprom->eesk = eesk; 275663e8e51Sths eeprom->eedo = eedo; 276663e8e51Sths eeprom->address = address; 277663e8e51Sths eeprom->command = command; 278663e8e51Sths } 279663e8e51Sths 280c227f099SAnthony Liguori uint16_t eeprom93xx_read(eeprom_t *eeprom) 281663e8e51Sths { 282663e8e51Sths /* Return status of pin DO (0 or 1). */ 283663e8e51Sths logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo); 2846fedcaa1SAntony Pavlov return eeprom->eedo; 285663e8e51Sths } 286663e8e51Sths 287663e8e51Sths #if 0 288663e8e51Sths void eeprom93xx_reset(eeprom_t *eeprom) 289663e8e51Sths { 290663e8e51Sths /* prepare eeprom */ 291663e8e51Sths logout("eeprom = 0x%p\n", eeprom); 292663e8e51Sths eeprom->tick = 0; 293663e8e51Sths eeprom->command = 0; 294663e8e51Sths } 295663e8e51Sths #endif 296663e8e51Sths 2975fce2b3eSAlex Williamson eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords) 298663e8e51Sths { 299663e8e51Sths /* Add a new EEPROM (with 16, 64 or 256 words). */ 300c227f099SAnthony Liguori eeprom_t *eeprom; 301663e8e51Sths uint8_t addrbits; 302663e8e51Sths 303663e8e51Sths switch (nwords) { 304663e8e51Sths case 16: 305663e8e51Sths case 64: 306663e8e51Sths addrbits = 6; 307663e8e51Sths break; 308663e8e51Sths case 128: 309663e8e51Sths case 256: 310663e8e51Sths addrbits = 8; 311663e8e51Sths break; 312663e8e51Sths default: 313663e8e51Sths assert(!"Unsupported EEPROM size, fallback to 64 words!"); 314663e8e51Sths nwords = 64; 315663e8e51Sths addrbits = 6; 316663e8e51Sths } 317663e8e51Sths 3187267c094SAnthony Liguori eeprom = (eeprom_t *)g_malloc0(sizeof(*eeprom) + nwords * 2); 319663e8e51Sths eeprom->size = nwords; 320663e8e51Sths eeprom->addrbits = addrbits; 321663e8e51Sths /* Output DO is tristate, read results in 1. */ 322663e8e51Sths eeprom->eedo = 1; 323663e8e51Sths logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords); 3245fce2b3eSAlex Williamson vmstate_register(dev, 0, &vmstate_eeprom, eeprom); 325663e8e51Sths return eeprom; 326663e8e51Sths } 327663e8e51Sths 3285fce2b3eSAlex Williamson void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom) 329663e8e51Sths { 330663e8e51Sths /* Destroy EEPROM. */ 331663e8e51Sths logout("eeprom = 0x%p\n", eeprom); 3325fce2b3eSAlex Williamson vmstate_unregister(dev, &vmstate_eeprom, eeprom); 3337267c094SAnthony Liguori g_free(eeprom); 334663e8e51Sths } 335663e8e51Sths 336c227f099SAnthony Liguori uint16_t *eeprom93xx_data(eeprom_t *eeprom) 337663e8e51Sths { 338663e8e51Sths /* Get EEPROM data array. */ 339663e8e51Sths return &eeprom->contents[0]; 340663e8e51Sths } 341663e8e51Sths 342663e8e51Sths /* eof */ 343