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" 41663e8e51Sths 42663e8e51Sths /* Debug EEPROM emulation. */ 43663e8e51Sths //~ #define DEBUG_EEPROM 44663e8e51Sths 45663e8e51Sths #ifdef DEBUG_EEPROM 46001faf32SBlue Swirl #define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__) 47663e8e51Sths #else 48001faf32SBlue Swirl #define logout(fmt, ...) ((void)0) 49663e8e51Sths #endif 50663e8e51Sths 517ab2589cSaurel32 #define EEPROM_INSTANCE 0 527ab2589cSaurel32 #define OLD_EEPROM_VERSION 20061112 537ab2589cSaurel32 #define EEPROM_VERSION (OLD_EEPROM_VERSION + 1) 54663e8e51Sths 55663e8e51Sths #if 0 56663e8e51Sths typedef enum { 57663e8e51Sths eeprom_read = 0x80, /* read register xx */ 58663e8e51Sths eeprom_write = 0x40, /* write register xx */ 59663e8e51Sths eeprom_erase = 0xc0, /* erase register xx */ 60663e8e51Sths eeprom_ewen = 0x30, /* erase / write enable */ 61663e8e51Sths eeprom_ewds = 0x00, /* erase / write disable */ 62663e8e51Sths eeprom_eral = 0x20, /* erase all registers */ 63663e8e51Sths eeprom_wral = 0x10, /* write all registers */ 64663e8e51Sths eeprom_amask = 0x0f, 65663e8e51Sths eeprom_imask = 0xf0 66663e8e51Sths } eeprom_instruction_t; 67663e8e51Sths #endif 68663e8e51Sths 69663e8e51Sths #ifdef DEBUG_EEPROM 70663e8e51Sths static const char *opstring[] = { 71663e8e51Sths "extended", "write", "read", "erase" 72663e8e51Sths }; 73663e8e51Sths #endif 74663e8e51Sths 75c227f099SAnthony Liguori struct _eeprom_t { 76663e8e51Sths uint8_t tick; 77663e8e51Sths uint8_t address; 78663e8e51Sths uint8_t command; 79ebabb67aSStefan Weil uint8_t writable; 80663e8e51Sths 81663e8e51Sths uint8_t eecs; 82663e8e51Sths uint8_t eesk; 83663e8e51Sths uint8_t eedo; 84663e8e51Sths 85663e8e51Sths uint8_t addrbits; 867ab2589cSaurel32 uint16_t size; 87663e8e51Sths uint16_t data; 88663e8e51Sths uint16_t contents[0]; 89663e8e51Sths }; 90663e8e51Sths 91663e8e51Sths /* Code for saving and restoring of EEPROM state. */ 92663e8e51Sths 93c4a0f2d3SJuan Quintela /* Restore an uint16_t from an uint8_t 94c4a0f2d3SJuan Quintela This is a Big hack, but it is how the old state did it. 95c4a0f2d3SJuan Quintela */ 96c4a0f2d3SJuan Quintela 97*2c21ee76SJianjun Duan static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size, 98*2c21ee76SJianjun Duan VMStateField *field) 99663e8e51Sths { 100c4a0f2d3SJuan Quintela uint16_t *v = pv; 101c4a0f2d3SJuan Quintela *v = qemu_get_ubyte(f); 102c4a0f2d3SJuan Quintela return 0; 103663e8e51Sths } 104663e8e51Sths 105*2c21ee76SJianjun Duan static int put_unused(QEMUFile *f, void *pv, size_t size, VMStateField *field, 106*2c21ee76SJianjun Duan QJSON *vmdesc) 107663e8e51Sths { 108c4a0f2d3SJuan Quintela fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n"); 109c4a0f2d3SJuan Quintela fprintf(stderr, "Never should be used to write a new state.\n"); 110c4a0f2d3SJuan Quintela exit(0); 111*2c21ee76SJianjun Duan 112*2c21ee76SJianjun Duan return 0; 1137ab2589cSaurel32 } 114d4ae799cSaurel32 115d05ac8faSBlue Swirl static const VMStateInfo vmstate_hack_uint16_from_uint8 = { 116c4a0f2d3SJuan Quintela .name = "uint16_from_uint8", 117c4a0f2d3SJuan Quintela .get = get_uint16_from_uint8, 118c4a0f2d3SJuan Quintela .put = put_unused, 119c4a0f2d3SJuan Quintela }; 120c4a0f2d3SJuan Quintela 121c4a0f2d3SJuan Quintela #define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \ 122c4a0f2d3SJuan Quintela VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t) 123c4a0f2d3SJuan Quintela 124c4a0f2d3SJuan Quintela static bool is_old_eeprom_version(void *opaque, int version_id) 125c4a0f2d3SJuan Quintela { 126c4a0f2d3SJuan Quintela return version_id == OLD_EEPROM_VERSION; 127663e8e51Sths } 128c4a0f2d3SJuan Quintela 129c4a0f2d3SJuan Quintela static const VMStateDescription vmstate_eeprom = { 130c4a0f2d3SJuan Quintela .name = "eeprom", 131c4a0f2d3SJuan Quintela .version_id = EEPROM_VERSION, 132c4a0f2d3SJuan Quintela .minimum_version_id = OLD_EEPROM_VERSION, 133c4a0f2d3SJuan Quintela .fields = (VMStateField[]) { 134c4a0f2d3SJuan Quintela VMSTATE_UINT8(tick, eeprom_t), 135c4a0f2d3SJuan Quintela VMSTATE_UINT8(address, eeprom_t), 136c4a0f2d3SJuan Quintela VMSTATE_UINT8(command, eeprom_t), 137ebabb67aSStefan Weil VMSTATE_UINT8(writable, eeprom_t), 138c4a0f2d3SJuan Quintela 139c4a0f2d3SJuan Quintela VMSTATE_UINT8(eecs, eeprom_t), 140c4a0f2d3SJuan Quintela VMSTATE_UINT8(eesk, eeprom_t), 141c4a0f2d3SJuan Quintela VMSTATE_UINT8(eedo, eeprom_t), 142c4a0f2d3SJuan Quintela 143c4a0f2d3SJuan Quintela VMSTATE_UINT8(addrbits, eeprom_t), 144c4a0f2d3SJuan Quintela VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version), 145c4a0f2d3SJuan Quintela VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1), 146c4a0f2d3SJuan Quintela VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION), 147c4a0f2d3SJuan Quintela VMSTATE_UINT16(data, eeprom_t), 148c4a0f2d3SJuan Quintela VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0, 149c4a0f2d3SJuan Quintela vmstate_info_uint16, uint16_t), 150c4a0f2d3SJuan Quintela VMSTATE_END_OF_LIST() 151663e8e51Sths } 152c4a0f2d3SJuan Quintela }; 153663e8e51Sths 154c227f099SAnthony Liguori void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi) 155663e8e51Sths { 156663e8e51Sths uint8_t tick = eeprom->tick; 157663e8e51Sths uint8_t eedo = eeprom->eedo; 158663e8e51Sths uint16_t address = eeprom->address; 159663e8e51Sths uint8_t command = eeprom->command; 160663e8e51Sths 161663e8e51Sths logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n", 162663e8e51Sths eecs, eesk, eedi, eedo, tick); 163663e8e51Sths 164663e8e51Sths if (!eeprom->eecs && eecs) { 165663e8e51Sths /* Start chip select cycle. */ 166663e8e51Sths logout("Cycle start, waiting for 1st start bit (0)\n"); 167663e8e51Sths tick = 0; 168663e8e51Sths command = 0x0; 169663e8e51Sths address = 0x0; 170663e8e51Sths } else if (eeprom->eecs && !eecs) { 171663e8e51Sths /* End chip select cycle. This triggers write / erase. */ 172ebabb67aSStefan Weil if (eeprom->writable) { 173663e8e51Sths uint8_t subcommand = address >> (eeprom->addrbits - 2); 174663e8e51Sths if (command == 0 && subcommand == 2) { 175663e8e51Sths /* Erase all. */ 176663e8e51Sths for (address = 0; address < eeprom->size; address++) { 177663e8e51Sths eeprom->contents[address] = 0xffff; 178663e8e51Sths } 179663e8e51Sths } else if (command == 3) { 180663e8e51Sths /* Erase word. */ 181663e8e51Sths eeprom->contents[address] = 0xffff; 182663e8e51Sths } else if (tick >= 2 + 2 + eeprom->addrbits + 16) { 183663e8e51Sths if (command == 1) { 184663e8e51Sths /* Write word. */ 185663e8e51Sths eeprom->contents[address] &= eeprom->data; 186663e8e51Sths } else if (command == 0 && subcommand == 1) { 187663e8e51Sths /* Write all. */ 188663e8e51Sths for (address = 0; address < eeprom->size; address++) { 189663e8e51Sths eeprom->contents[address] &= eeprom->data; 190663e8e51Sths } 191663e8e51Sths } 192663e8e51Sths } 193663e8e51Sths } 194663e8e51Sths /* Output DO is tristate, read results in 1. */ 195663e8e51Sths eedo = 1; 196663e8e51Sths } else if (eecs && !eeprom->eesk && eesk) { 197663e8e51Sths /* Raising edge of clock shifts data in. */ 198663e8e51Sths if (tick == 0) { 199663e8e51Sths /* Wait for 1st start bit. */ 200663e8e51Sths if (eedi == 0) { 201663e8e51Sths logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n"); 202663e8e51Sths tick++; 203663e8e51Sths } else { 204663e8e51Sths logout("wrong 1st start bit (is 1, should be 0)\n"); 205663e8e51Sths tick = 2; 206663e8e51Sths //~ assert(!"wrong start bit"); 207663e8e51Sths } 208663e8e51Sths } else if (tick == 1) { 209663e8e51Sths /* Wait for 2nd start bit. */ 210663e8e51Sths if (eedi != 0) { 211663e8e51Sths logout("Got correct 2nd start bit, getting command + address\n"); 212663e8e51Sths tick++; 213663e8e51Sths } else { 214663e8e51Sths logout("1st start bit is longer than needed\n"); 215663e8e51Sths } 216663e8e51Sths } else if (tick < 2 + 2) { 217663e8e51Sths /* Got 2 start bits, transfer 2 opcode bits. */ 218663e8e51Sths tick++; 219663e8e51Sths command <<= 1; 220663e8e51Sths if (eedi) { 221663e8e51Sths command += 1; 222663e8e51Sths } 223663e8e51Sths } else if (tick < 2 + 2 + eeprom->addrbits) { 224663e8e51Sths /* Got 2 start bits and 2 opcode bits, transfer all address bits. */ 225663e8e51Sths tick++; 226663e8e51Sths address = ((address << 1) | eedi); 227663e8e51Sths if (tick == 2 + 2 + eeprom->addrbits) { 228663e8e51Sths logout("%s command, address = 0x%02x (value 0x%04x)\n", 229663e8e51Sths opstring[command], address, eeprom->contents[address]); 230663e8e51Sths if (command == 2) { 231663e8e51Sths eedo = 0; 232663e8e51Sths } 233663e8e51Sths address = address % eeprom->size; 234663e8e51Sths if (command == 0) { 235663e8e51Sths /* Command code in upper 2 bits of address. */ 236663e8e51Sths switch (address >> (eeprom->addrbits - 2)) { 237663e8e51Sths case 0: 238663e8e51Sths logout("write disable command\n"); 239ebabb67aSStefan Weil eeprom->writable = 0; 240663e8e51Sths break; 241663e8e51Sths case 1: 242663e8e51Sths logout("write all command\n"); 243663e8e51Sths break; 244663e8e51Sths case 2: 245663e8e51Sths logout("erase all command\n"); 246663e8e51Sths break; 247663e8e51Sths case 3: 248663e8e51Sths logout("write enable command\n"); 249ebabb67aSStefan Weil eeprom->writable = 1; 250663e8e51Sths break; 251663e8e51Sths } 252663e8e51Sths } else { 253663e8e51Sths /* Read, write or erase word. */ 254663e8e51Sths eeprom->data = eeprom->contents[address]; 255663e8e51Sths } 256663e8e51Sths } 257663e8e51Sths } else if (tick < 2 + 2 + eeprom->addrbits + 16) { 258663e8e51Sths /* Transfer 16 data bits. */ 259663e8e51Sths tick++; 260663e8e51Sths if (command == 2) { 261663e8e51Sths /* Read word. */ 262663e8e51Sths eedo = ((eeprom->data & 0x8000) != 0); 263663e8e51Sths } 264663e8e51Sths eeprom->data <<= 1; 265663e8e51Sths eeprom->data += eedi; 266663e8e51Sths } else { 267663e8e51Sths logout("additional unneeded tick, not processed\n"); 268663e8e51Sths } 269663e8e51Sths } 270663e8e51Sths /* Save status of EEPROM. */ 271663e8e51Sths eeprom->tick = tick; 272663e8e51Sths eeprom->eecs = eecs; 273663e8e51Sths eeprom->eesk = eesk; 274663e8e51Sths eeprom->eedo = eedo; 275663e8e51Sths eeprom->address = address; 276663e8e51Sths eeprom->command = command; 277663e8e51Sths } 278663e8e51Sths 279c227f099SAnthony Liguori uint16_t eeprom93xx_read(eeprom_t *eeprom) 280663e8e51Sths { 281663e8e51Sths /* Return status of pin DO (0 or 1). */ 282663e8e51Sths logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo); 2836fedcaa1SAntony Pavlov return eeprom->eedo; 284663e8e51Sths } 285663e8e51Sths 286663e8e51Sths #if 0 287663e8e51Sths void eeprom93xx_reset(eeprom_t *eeprom) 288663e8e51Sths { 289663e8e51Sths /* prepare eeprom */ 290663e8e51Sths logout("eeprom = 0x%p\n", eeprom); 291663e8e51Sths eeprom->tick = 0; 292663e8e51Sths eeprom->command = 0; 293663e8e51Sths } 294663e8e51Sths #endif 295663e8e51Sths 2965fce2b3eSAlex Williamson eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords) 297663e8e51Sths { 298663e8e51Sths /* Add a new EEPROM (with 16, 64 or 256 words). */ 299c227f099SAnthony Liguori eeprom_t *eeprom; 300663e8e51Sths uint8_t addrbits; 301663e8e51Sths 302663e8e51Sths switch (nwords) { 303663e8e51Sths case 16: 304663e8e51Sths case 64: 305663e8e51Sths addrbits = 6; 306663e8e51Sths break; 307663e8e51Sths case 128: 308663e8e51Sths case 256: 309663e8e51Sths addrbits = 8; 310663e8e51Sths break; 311663e8e51Sths default: 312663e8e51Sths assert(!"Unsupported EEPROM size, fallback to 64 words!"); 313663e8e51Sths nwords = 64; 314663e8e51Sths addrbits = 6; 315663e8e51Sths } 316663e8e51Sths 3177267c094SAnthony Liguori eeprom = (eeprom_t *)g_malloc0(sizeof(*eeprom) + nwords * 2); 318663e8e51Sths eeprom->size = nwords; 319663e8e51Sths eeprom->addrbits = addrbits; 320663e8e51Sths /* Output DO is tristate, read results in 1. */ 321663e8e51Sths eeprom->eedo = 1; 322663e8e51Sths logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords); 3235fce2b3eSAlex Williamson vmstate_register(dev, 0, &vmstate_eeprom, eeprom); 324663e8e51Sths return eeprom; 325663e8e51Sths } 326663e8e51Sths 3275fce2b3eSAlex Williamson void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom) 328663e8e51Sths { 329663e8e51Sths /* Destroy EEPROM. */ 330663e8e51Sths logout("eeprom = 0x%p\n", eeprom); 3315fce2b3eSAlex Williamson vmstate_unregister(dev, &vmstate_eeprom, eeprom); 3327267c094SAnthony Liguori g_free(eeprom); 333663e8e51Sths } 334663e8e51Sths 335c227f099SAnthony Liguori uint16_t *eeprom93xx_data(eeprom_t *eeprom) 336663e8e51Sths { 337663e8e51Sths /* Get EEPROM data array. */ 338663e8e51Sths return &eeprom->contents[0]; 339663e8e51Sths } 340663e8e51Sths 341663e8e51Sths /* eof */ 342