Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / hw / nvram / eeprom93xx.c
blob7005e23ad8a6591e35b5e560b258fce2a3a7655a
1 /*
2 * QEMU EEPROM 93xx emulation
4 * Copyright (c) 2006-2009 Stefan Weil
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /* Emulation for serial EEPROMs:
21 * NMC93C06 256-Bit (16 x 16)
22 * NMC93C46 1024-Bit (64 x 16)
23 * NMC93C56 2028 Bit (128 x 16)
24 * NMC93C66 4096 Bit (256 x 16)
25 * Compatible devices include FM93C46 and others.
27 * Other drivers use these interface functions:
28 * eeprom93xx_new - add a new EEPROM (with 16, 64 or 256 words)
29 * eeprom93xx_free - destroy EEPROM
30 * eeprom93xx_read - read data from the EEPROM
31 * eeprom93xx_write - write data to the EEPROM
32 * eeprom93xx_data - get EEPROM data array for external manipulation
34 * Hint: This driver always uses host endianness!
36 * Todo list:
37 * - No emulation of EEPROM timings.
40 #include "qemu/osdep.h"
41 #include "hw/hw.h"
42 #include "hw/nvram/eeprom93xx.h"
44 /* Debug EEPROM emulation. */
45 //~ #define DEBUG_EEPROM
47 #ifdef DEBUG_EEPROM
48 #define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
49 #else
50 #define logout(fmt, ...) ((void)0)
51 #endif
53 #define EEPROM_INSTANCE 0
54 #define OLD_EEPROM_VERSION 20061112
55 #define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
57 #if 0
58 typedef enum {
59 eeprom_read = 0x80, /* read register xx */
60 eeprom_write = 0x40, /* write register xx */
61 eeprom_erase = 0xc0, /* erase register xx */
62 eeprom_ewen = 0x30, /* erase / write enable */
63 eeprom_ewds = 0x00, /* erase / write disable */
64 eeprom_eral = 0x20, /* erase all registers */
65 eeprom_wral = 0x10, /* write all registers */
66 eeprom_amask = 0x0f,
67 eeprom_imask = 0xf0
68 } eeprom_instruction_t;
69 #endif
71 #ifdef DEBUG_EEPROM
72 static const char *opstring[] = {
73 "extended", "write", "read", "erase"
75 #endif
77 struct _eeprom_t {
78 uint8_t tick;
79 uint8_t address;
80 uint8_t command;
81 uint8_t writable;
83 uint8_t eecs;
84 uint8_t eesk;
85 uint8_t eedo;
87 uint8_t addrbits;
88 uint16_t size;
89 uint16_t data;
90 uint16_t contents[0];
93 /* Code for saving and restoring of EEPROM state. */
95 /* Restore an uint16_t from an uint8_t
96 This is a Big hack, but it is how the old state did it.
99 static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size)
101 uint16_t *v = pv;
102 *v = qemu_get_ubyte(f);
103 return 0;
106 static void put_unused(QEMUFile *f, void *pv, size_t size)
108 fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n");
109 fprintf(stderr, "Never should be used to write a new state.\n");
110 exit(0);
113 static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
114 .name = "uint16_from_uint8",
115 .get = get_uint16_from_uint8,
116 .put = put_unused,
119 #define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \
120 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
122 static bool is_old_eeprom_version(void *opaque, int version_id)
124 return version_id == OLD_EEPROM_VERSION;
127 static const VMStateDescription vmstate_eeprom = {
128 .name = "eeprom",
129 .version_id = EEPROM_VERSION,
130 .minimum_version_id = OLD_EEPROM_VERSION,
131 .fields = (VMStateField[]) {
132 VMSTATE_UINT8(tick, eeprom_t),
133 VMSTATE_UINT8(address, eeprom_t),
134 VMSTATE_UINT8(command, eeprom_t),
135 VMSTATE_UINT8(writable, eeprom_t),
137 VMSTATE_UINT8(eecs, eeprom_t),
138 VMSTATE_UINT8(eesk, eeprom_t),
139 VMSTATE_UINT8(eedo, eeprom_t),
141 VMSTATE_UINT8(addrbits, eeprom_t),
142 VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
143 VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
144 VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION),
145 VMSTATE_UINT16(data, eeprom_t),
146 VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
147 vmstate_info_uint16, uint16_t),
148 VMSTATE_END_OF_LIST()
152 void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
154 uint8_t tick = eeprom->tick;
155 uint8_t eedo = eeprom->eedo;
156 uint16_t address = eeprom->address;
157 uint8_t command = eeprom->command;
159 logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n",
160 eecs, eesk, eedi, eedo, tick);
162 if (!eeprom->eecs && eecs) {
163 /* Start chip select cycle. */
164 logout("Cycle start, waiting for 1st start bit (0)\n");
165 tick = 0;
166 command = 0x0;
167 address = 0x0;
168 } else if (eeprom->eecs && !eecs) {
169 /* End chip select cycle. This triggers write / erase. */
170 if (eeprom->writable) {
171 uint8_t subcommand = address >> (eeprom->addrbits - 2);
172 if (command == 0 && subcommand == 2) {
173 /* Erase all. */
174 for (address = 0; address < eeprom->size; address++) {
175 eeprom->contents[address] = 0xffff;
177 } else if (command == 3) {
178 /* Erase word. */
179 eeprom->contents[address] = 0xffff;
180 } else if (tick >= 2 + 2 + eeprom->addrbits + 16) {
181 if (command == 1) {
182 /* Write word. */
183 eeprom->contents[address] &= eeprom->data;
184 } else if (command == 0 && subcommand == 1) {
185 /* Write all. */
186 for (address = 0; address < eeprom->size; address++) {
187 eeprom->contents[address] &= eeprom->data;
192 /* Output DO is tristate, read results in 1. */
193 eedo = 1;
194 } else if (eecs && !eeprom->eesk && eesk) {
195 /* Raising edge of clock shifts data in. */
196 if (tick == 0) {
197 /* Wait for 1st start bit. */
198 if (eedi == 0) {
199 logout("Got redundant bit (0), waiting for start bit (1)\n");
200 } else {
201 logout("Got start bit (1), getting command + address\n");
202 tick++;
204 } else if (tick < 1 + 2) {
205 /* Got 1 start bit, transfer 2 opcode bits. */
206 tick++;
207 command <<= 1;
208 if (eedi) {
209 command += 1;
211 } else if (tick < 1 + 2 + eeprom->addrbits) {
212 /* Got 2 start bits and 2 opcode bits, transfer all address bits. */
213 tick++;
214 address = ((address << 1) | eedi);
215 if (tick == 1 + 2 + eeprom->addrbits) {
216 logout("%s command, address = 0x%02x (value 0x%04x)\n",
217 opstring[command], address, eeprom->contents[address]);
218 if (command == 2) {
219 eedo = 0;
221 address = address % eeprom->size;
222 if (command == 0) {
223 /* Command code in upper 2 bits of address. */
224 switch (address >> (eeprom->addrbits - 2)) {
225 case 0:
226 logout("write disable command\n");
227 eeprom->writable = 0;
228 break;
229 case 1:
230 logout("write all command\n");
231 break;
232 case 2:
233 logout("erase all command\n");
234 break;
235 case 3:
236 logout("write enable command\n");
237 eeprom->writable = 1;
238 break;
240 } else {
241 /* Read, write or erase word. */
242 eeprom->data = eeprom->contents[address];
245 } else if (tick < 1 + 2 + eeprom->addrbits + 16) {
246 /* Transfer 16 data bits. */
247 tick++;
248 if (command == 2) {
249 /* Read word. */
250 eedo = ((eeprom->data & 0x8000) != 0);
252 eeprom->data <<= 1;
253 eeprom->data += eedi;
254 } else {
255 logout("additional unneeded tick, not processed\n");
258 /* Save status of EEPROM. */
259 eeprom->tick = tick;
260 eeprom->eecs = eecs;
261 eeprom->eesk = eesk;
262 eeprom->eedo = eedo;
263 eeprom->address = address;
264 eeprom->command = command;
267 uint16_t eeprom93xx_read(eeprom_t *eeprom)
269 /* Return status of pin DO (0 or 1). */
270 logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo);
271 return eeprom->eedo;
274 #if 0
275 void eeprom93xx_reset(eeprom_t *eeprom)
277 /* prepare eeprom */
278 logout("eeprom = 0x%p\n", eeprom);
279 eeprom->tick = 0;
280 eeprom->command = 0;
282 #endif
284 eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords)
286 /* Add a new EEPROM (with 16, 64 or 256 words). */
287 eeprom_t *eeprom;
288 uint8_t addrbits;
290 switch (nwords) {
291 case 16:
292 case 64:
293 addrbits = 6;
294 break;
295 case 128:
296 case 256:
297 addrbits = 8;
298 break;
299 default:
300 assert(!"Unsupported EEPROM size, fallback to 64 words!");
301 nwords = 64;
302 addrbits = 6;
305 eeprom = (eeprom_t *)g_malloc0(sizeof(*eeprom) + nwords * 2);
306 eeprom->size = nwords;
307 eeprom->addrbits = addrbits;
308 /* Output DO is tristate, read results in 1. */
309 eeprom->eedo = 1;
310 logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
311 vmstate_register(dev, 0, &vmstate_eeprom, eeprom);
312 return eeprom;
315 void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom)
317 /* Destroy EEPROM. */
318 logout("eeprom = 0x%p\n", eeprom);
319 vmstate_unregister(dev, &vmstate_eeprom, eeprom);
320 g_free(eeprom);
323 uint16_t *eeprom93xx_data(eeprom_t *eeprom)
325 /* Get EEPROM data array. */
326 return &eeprom->contents[0];
329 /* eof */