ths | 663e8e5 | 2007-04-02 12:35:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU EEPROM 93xx emulation |
| 3 | * |
| 4 | * Copyright (c) 2006-2007 Stefan Weil |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | /* Emulation for serial EEPROMs: |
| 22 | * NMC93C06 256-Bit (16 x 16) |
| 23 | * NMC93C46 1024-Bit (64 x 16) |
| 24 | * NMC93C56 2028 Bit (128 x 16) |
| 25 | * NMC93C66 4096 Bit (256 x 16) |
| 26 | * Compatible devices include FM93C46 and others. |
| 27 | * |
| 28 | * Other drivers use these interface functions: |
| 29 | * eeprom93xx_new - add a new EEPROM (with 16, 64 or 256 words) |
| 30 | * eeprom93xx_free - destroy EEPROM |
| 31 | * eeprom93xx_read - read data from the EEPROM |
| 32 | * eeprom93xx_write - write data to the EEPROM |
| 33 | * eeprom93xx_data - get EEPROM data array for external manipulation |
| 34 | * |
| 35 | * Todo list: |
| 36 | * - No emulation of EEPROM timings. |
| 37 | */ |
| 38 | |
| 39 | #include <assert.h> |
| 40 | #include "eeprom93xx.h" |
| 41 | |
| 42 | /* Debug EEPROM emulation. */ |
| 43 | //~ #define DEBUG_EEPROM |
| 44 | |
| 45 | #ifdef DEBUG_EEPROM |
| 46 | #define logout(fmt, args...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ##args) |
| 47 | #else |
| 48 | #define logout(fmt, args...) ((void)0) |
| 49 | #endif |
| 50 | |
| 51 | static int eeprom_instance = 0; |
| 52 | static const int eeprom_version = 20061112; |
| 53 | |
| 54 | #if 0 |
| 55 | typedef enum { |
| 56 | eeprom_read = 0x80, /* read register xx */ |
| 57 | eeprom_write = 0x40, /* write register xx */ |
| 58 | eeprom_erase = 0xc0, /* erase register xx */ |
| 59 | eeprom_ewen = 0x30, /* erase / write enable */ |
| 60 | eeprom_ewds = 0x00, /* erase / write disable */ |
| 61 | eeprom_eral = 0x20, /* erase all registers */ |
| 62 | eeprom_wral = 0x10, /* write all registers */ |
| 63 | eeprom_amask = 0x0f, |
| 64 | eeprom_imask = 0xf0 |
| 65 | } eeprom_instruction_t; |
| 66 | #endif |
| 67 | |
| 68 | #ifdef DEBUG_EEPROM |
| 69 | static const char *opstring[] = { |
| 70 | "extended", "write", "read", "erase" |
| 71 | }; |
| 72 | #endif |
| 73 | |
| 74 | struct _eeprom_t { |
| 75 | uint8_t tick; |
| 76 | uint8_t address; |
| 77 | uint8_t command; |
| 78 | uint8_t writeable; |
| 79 | |
| 80 | uint8_t eecs; |
| 81 | uint8_t eesk; |
| 82 | uint8_t eedo; |
| 83 | |
| 84 | uint8_t addrbits; |
| 85 | uint8_t size; |
| 86 | uint16_t data; |
| 87 | uint16_t contents[0]; |
| 88 | }; |
| 89 | |
| 90 | /* Code for saving and restoring of EEPROM state. */ |
| 91 | |
| 92 | static void eeprom_save(QEMUFile *f, void *opaque) |
| 93 | { |
| 94 | /* Save EEPROM data. */ |
| 95 | unsigned address; |
| 96 | eeprom_t *eeprom = (eeprom_t *)opaque; |
| 97 | qemu_put_buffer(f, (uint8_t *)eeprom, sizeof(*eeprom) - 2); |
| 98 | qemu_put_be16(f, eeprom->data); |
| 99 | for (address = 0; address < eeprom->size; address++) { |
| 100 | qemu_put_be16(f, eeprom->contents[address]); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static int eeprom_load(QEMUFile *f, void *opaque, int version_id) |
| 105 | { |
| 106 | /* Load EEPROM data from saved data if version and EEPROM size |
| 107 | of data and current EEPROM are identical. */ |
| 108 | eeprom_t *eeprom = (eeprom_t *)opaque; |
| 109 | int result = -EINVAL; |
| 110 | if (version_id == eeprom_version) { |
| 111 | unsigned address; |
| 112 | uint8_t size = eeprom->size; |
| 113 | qemu_get_buffer(f, (uint8_t *)eeprom, sizeof(*eeprom) - 2); |
| 114 | if (eeprom->size == size) { |
| 115 | eeprom->data = qemu_get_be16(f); |
| 116 | for (address = 0; address < eeprom->size; address++) { |
| 117 | eeprom->contents[address] = qemu_get_be16(f); |
| 118 | } |
| 119 | result = 0; |
| 120 | } |
| 121 | } |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi) |
| 126 | { |
| 127 | uint8_t tick = eeprom->tick; |
| 128 | uint8_t eedo = eeprom->eedo; |
| 129 | uint16_t address = eeprom->address; |
| 130 | uint8_t command = eeprom->command; |
| 131 | |
| 132 | logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n", |
| 133 | eecs, eesk, eedi, eedo, tick); |
| 134 | |
| 135 | if (! eeprom->eecs && eecs) { |
| 136 | /* Start chip select cycle. */ |
| 137 | logout("Cycle start, waiting for 1st start bit (0)\n"); |
| 138 | tick = 0; |
| 139 | command = 0x0; |
| 140 | address = 0x0; |
| 141 | } else if (eeprom->eecs && ! eecs) { |
| 142 | /* End chip select cycle. This triggers write / erase. */ |
| 143 | if (eeprom->writeable) { |
| 144 | uint8_t subcommand = address >> (eeprom->addrbits - 2); |
| 145 | if (command == 0 && subcommand == 2) { |
| 146 | /* Erase all. */ |
| 147 | for (address = 0; address < eeprom->size; address++) { |
| 148 | eeprom->contents[address] = 0xffff; |
| 149 | } |
| 150 | } else if (command == 3) { |
| 151 | /* Erase word. */ |
| 152 | eeprom->contents[address] = 0xffff; |
| 153 | } else if (tick >= 2 + 2 + eeprom->addrbits + 16) { |
| 154 | if (command == 1) { |
| 155 | /* Write word. */ |
| 156 | eeprom->contents[address] &= eeprom->data; |
| 157 | } else if (command == 0 && subcommand == 1) { |
| 158 | /* Write all. */ |
| 159 | for (address = 0; address < eeprom->size; address++) { |
| 160 | eeprom->contents[address] &= eeprom->data; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | /* Output DO is tristate, read results in 1. */ |
| 166 | eedo = 1; |
| 167 | } else if (eecs && ! eeprom->eesk && eesk) { |
| 168 | /* Raising edge of clock shifts data in. */ |
| 169 | if (tick == 0) { |
| 170 | /* Wait for 1st start bit. */ |
| 171 | if (eedi == 0) { |
| 172 | logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n"); |
| 173 | tick++; |
| 174 | } else { |
| 175 | logout("wrong 1st start bit (is 1, should be 0)\n"); |
| 176 | tick = 2; |
| 177 | //~ assert(!"wrong start bit"); |
| 178 | } |
| 179 | } else if (tick == 1) { |
| 180 | /* Wait for 2nd start bit. */ |
| 181 | if (eedi != 0) { |
| 182 | logout("Got correct 2nd start bit, getting command + address\n"); |
| 183 | tick++; |
| 184 | } else { |
| 185 | logout("1st start bit is longer than needed\n"); |
| 186 | } |
| 187 | } else if (tick < 2 + 2) { |
| 188 | /* Got 2 start bits, transfer 2 opcode bits. */ |
| 189 | tick++; |
| 190 | command <<= 1; |
| 191 | if (eedi) { |
| 192 | command += 1; |
| 193 | } |
| 194 | } else if (tick < 2 + 2 + eeprom->addrbits) { |
| 195 | /* Got 2 start bits and 2 opcode bits, transfer all address bits. */ |
| 196 | tick++; |
| 197 | address = ((address << 1) | eedi); |
| 198 | if (tick == 2 + 2 + eeprom->addrbits) { |
| 199 | logout("%s command, address = 0x%02x (value 0x%04x)\n", |
| 200 | opstring[command], address, eeprom->contents[address]); |
| 201 | if (command == 2) { |
| 202 | eedo = 0; |
| 203 | } |
| 204 | address = address % eeprom->size; |
| 205 | if (command == 0) { |
| 206 | /* Command code in upper 2 bits of address. */ |
| 207 | switch (address >> (eeprom->addrbits - 2)) { |
| 208 | case 0: |
| 209 | logout("write disable command\n"); |
| 210 | eeprom->writeable = 0; |
| 211 | break; |
| 212 | case 1: |
| 213 | logout("write all command\n"); |
| 214 | break; |
| 215 | case 2: |
| 216 | logout("erase all command\n"); |
| 217 | break; |
| 218 | case 3: |
| 219 | logout("write enable command\n"); |
| 220 | eeprom->writeable = 1; |
| 221 | break; |
| 222 | } |
| 223 | } else { |
| 224 | /* Read, write or erase word. */ |
| 225 | eeprom->data = eeprom->contents[address]; |
| 226 | } |
| 227 | } |
| 228 | } else if (tick < 2 + 2 + eeprom->addrbits + 16) { |
| 229 | /* Transfer 16 data bits. */ |
| 230 | tick++; |
| 231 | if (command == 2) { |
| 232 | /* Read word. */ |
| 233 | eedo = ((eeprom->data & 0x8000) != 0); |
| 234 | } |
| 235 | eeprom->data <<= 1; |
| 236 | eeprom->data += eedi; |
| 237 | } else { |
| 238 | logout("additional unneeded tick, not processed\n"); |
| 239 | } |
| 240 | } |
| 241 | /* Save status of EEPROM. */ |
| 242 | eeprom->tick = tick; |
| 243 | eeprom->eecs = eecs; |
| 244 | eeprom->eesk = eesk; |
| 245 | eeprom->eedo = eedo; |
| 246 | eeprom->address = address; |
| 247 | eeprom->command = command; |
| 248 | } |
| 249 | |
| 250 | uint16_t eeprom93xx_read(eeprom_t *eeprom) |
| 251 | { |
| 252 | /* Return status of pin DO (0 or 1). */ |
| 253 | logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo); |
| 254 | return (eeprom->eedo); |
| 255 | } |
| 256 | |
| 257 | #if 0 |
| 258 | void eeprom93xx_reset(eeprom_t *eeprom) |
| 259 | { |
| 260 | /* prepare eeprom */ |
| 261 | logout("eeprom = 0x%p\n", eeprom); |
| 262 | eeprom->tick = 0; |
| 263 | eeprom->command = 0; |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | eeprom_t *eeprom93xx_new(uint16_t nwords) |
| 268 | { |
| 269 | /* Add a new EEPROM (with 16, 64 or 256 words). */ |
| 270 | eeprom_t *eeprom; |
| 271 | uint8_t addrbits; |
| 272 | |
| 273 | switch (nwords) { |
| 274 | case 16: |
| 275 | case 64: |
| 276 | addrbits = 6; |
| 277 | break; |
| 278 | case 128: |
| 279 | case 256: |
| 280 | addrbits = 8; |
| 281 | break; |
| 282 | default: |
| 283 | assert(!"Unsupported EEPROM size, fallback to 64 words!"); |
| 284 | nwords = 64; |
| 285 | addrbits = 6; |
| 286 | } |
| 287 | |
| 288 | eeprom = (eeprom_t *)qemu_mallocz(sizeof(*eeprom) + nwords * 2); |
| 289 | eeprom->size = nwords; |
| 290 | eeprom->addrbits = addrbits; |
| 291 | /* Output DO is tristate, read results in 1. */ |
| 292 | eeprom->eedo = 1; |
| 293 | logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords); |
| 294 | register_savevm("eeprom", eeprom_instance, eeprom_version, |
| 295 | eeprom_save, eeprom_load, eeprom); |
| 296 | return eeprom; |
| 297 | } |
| 298 | |
| 299 | void eeprom93xx_free(eeprom_t *eeprom) |
| 300 | { |
| 301 | /* Destroy EEPROM. */ |
| 302 | logout("eeprom = 0x%p\n", eeprom); |
| 303 | qemu_free(eeprom); |
| 304 | } |
| 305 | |
| 306 | uint16_t *eeprom93xx_data(eeprom_t *eeprom) |
| 307 | { |
| 308 | /* Get EEPROM data array. */ |
| 309 | return &eeprom->contents[0]; |
| 310 | } |
| 311 | |
| 312 | /* eof */ |