2 * QEMU EEPROM 93xx emulation
4 * Copyright (c) 2006-2007 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 2 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
35 * - No emulation of EEPROM timings.
39 #include "hw/nvram/eeprom93xx.h"
41 /* Debug EEPROM emulation. */
42 //~ #define DEBUG_EEPROM
45 #define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
47 #define logout(fmt, ...) ((void)0)
50 #define EEPROM_INSTANCE 0
51 #define OLD_EEPROM_VERSION 20061112
52 #define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
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 */
65 } eeprom_instruction_t
;
69 static const char *opstring
[] = {
70 "extended", "write", "read", "erase"
90 /* Code for saving and restoring of EEPROM state. */
92 /* Restore an uint16_t from an uint8_t
93 This is a Big hack, but it is how the old state did it.
96 static int get_uint16_from_uint8(QEMUFile
*f
, void *pv
, size_t size
)
99 *v
= qemu_get_ubyte(f
);
103 static void put_unused(QEMUFile
*f
, void *pv
, size_t size
)
105 fprintf(stderr
, "uint16_from_uint8 is used only for backwards compatibility.\n");
106 fprintf(stderr
, "Never should be used to write a new state.\n");
110 static const VMStateInfo vmstate_hack_uint16_from_uint8
= {
111 .name
= "uint16_from_uint8",
112 .get
= get_uint16_from_uint8
,
116 #define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \
117 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
119 static bool is_old_eeprom_version(void *opaque
, int version_id
)
121 return version_id
== OLD_EEPROM_VERSION
;
124 static const VMStateDescription vmstate_eeprom
= {
126 .version_id
= EEPROM_VERSION
,
127 .minimum_version_id
= OLD_EEPROM_VERSION
,
128 .fields
= (VMStateField
[]) {
129 VMSTATE_UINT8(tick
, eeprom_t
),
130 VMSTATE_UINT8(address
, eeprom_t
),
131 VMSTATE_UINT8(command
, eeprom_t
),
132 VMSTATE_UINT8(writable
, eeprom_t
),
134 VMSTATE_UINT8(eecs
, eeprom_t
),
135 VMSTATE_UINT8(eesk
, eeprom_t
),
136 VMSTATE_UINT8(eedo
, eeprom_t
),
138 VMSTATE_UINT8(addrbits
, eeprom_t
),
139 VMSTATE_UINT16_HACK_TEST(size
, eeprom_t
, is_old_eeprom_version
),
140 VMSTATE_UNUSED_TEST(is_old_eeprom_version
, 1),
141 VMSTATE_UINT16_EQUAL_V(size
, eeprom_t
, EEPROM_VERSION
),
142 VMSTATE_UINT16(data
, eeprom_t
),
143 VMSTATE_VARRAY_UINT16_UNSAFE(contents
, eeprom_t
, size
, 0,
144 vmstate_info_uint16
, uint16_t),
145 VMSTATE_END_OF_LIST()
149 void eeprom93xx_write(eeprom_t
*eeprom
, int eecs
, int eesk
, int eedi
)
151 uint8_t tick
= eeprom
->tick
;
152 uint8_t eedo
= eeprom
->eedo
;
153 uint16_t address
= eeprom
->address
;
154 uint8_t command
= eeprom
->command
;
156 logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n",
157 eecs
, eesk
, eedi
, eedo
, tick
);
159 if (!eeprom
->eecs
&& eecs
) {
160 /* Start chip select cycle. */
161 logout("Cycle start, waiting for 1st start bit (0)\n");
165 } else if (eeprom
->eecs
&& !eecs
) {
166 /* End chip select cycle. This triggers write / erase. */
167 if (eeprom
->writable
) {
168 uint8_t subcommand
= address
>> (eeprom
->addrbits
- 2);
169 if (command
== 0 && subcommand
== 2) {
171 for (address
= 0; address
< eeprom
->size
; address
++) {
172 eeprom
->contents
[address
] = 0xffff;
174 } else if (command
== 3) {
176 eeprom
->contents
[address
] = 0xffff;
177 } else if (tick
>= 2 + 2 + eeprom
->addrbits
+ 16) {
180 eeprom
->contents
[address
] &= eeprom
->data
;
181 } else if (command
== 0 && subcommand
== 1) {
183 for (address
= 0; address
< eeprom
->size
; address
++) {
184 eeprom
->contents
[address
] &= eeprom
->data
;
189 /* Output DO is tristate, read results in 1. */
191 } else if (eecs
&& !eeprom
->eesk
&& eesk
) {
192 /* Raising edge of clock shifts data in. */
194 /* Wait for 1st start bit. */
196 logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n");
199 logout("wrong 1st start bit (is 1, should be 0)\n");
201 //~ assert(!"wrong start bit");
203 } else if (tick
== 1) {
204 /* Wait for 2nd start bit. */
206 logout("Got correct 2nd start bit, getting command + address\n");
209 logout("1st start bit is longer than needed\n");
211 } else if (tick
< 2 + 2) {
212 /* Got 2 start bits, transfer 2 opcode bits. */
218 } else if (tick
< 2 + 2 + eeprom
->addrbits
) {
219 /* Got 2 start bits and 2 opcode bits, transfer all address bits. */
221 address
= ((address
<< 1) | eedi
);
222 if (tick
== 2 + 2 + eeprom
->addrbits
) {
223 logout("%s command, address = 0x%02x (value 0x%04x)\n",
224 opstring
[command
], address
, eeprom
->contents
[address
]);
228 address
= address
% eeprom
->size
;
230 /* Command code in upper 2 bits of address. */
231 switch (address
>> (eeprom
->addrbits
- 2)) {
233 logout("write disable command\n");
234 eeprom
->writable
= 0;
237 logout("write all command\n");
240 logout("erase all command\n");
243 logout("write enable command\n");
244 eeprom
->writable
= 1;
248 /* Read, write or erase word. */
249 eeprom
->data
= eeprom
->contents
[address
];
252 } else if (tick
< 2 + 2 + eeprom
->addrbits
+ 16) {
253 /* Transfer 16 data bits. */
257 eedo
= ((eeprom
->data
& 0x8000) != 0);
260 eeprom
->data
+= eedi
;
262 logout("additional unneeded tick, not processed\n");
265 /* Save status of EEPROM. */
270 eeprom
->address
= address
;
271 eeprom
->command
= command
;
274 uint16_t eeprom93xx_read(eeprom_t
*eeprom
)
276 /* Return status of pin DO (0 or 1). */
277 logout("CS=%u DO=%u\n", eeprom
->eecs
, eeprom
->eedo
);
282 void eeprom93xx_reset(eeprom_t
*eeprom
)
285 logout("eeprom = 0x%p\n", eeprom
);
291 eeprom_t
*eeprom93xx_new(DeviceState
*dev
, uint16_t nwords
)
293 /* Add a new EEPROM (with 16, 64 or 256 words). */
307 assert(!"Unsupported EEPROM size, fallback to 64 words!");
312 eeprom
= (eeprom_t
*)g_malloc0(sizeof(*eeprom
) + nwords
* 2);
313 eeprom
->size
= nwords
;
314 eeprom
->addrbits
= addrbits
;
315 /* Output DO is tristate, read results in 1. */
317 logout("eeprom = 0x%p, nwords = %u\n", eeprom
, nwords
);
318 vmstate_register(dev
, 0, &vmstate_eeprom
, eeprom
);
322 void eeprom93xx_free(DeviceState
*dev
, eeprom_t
*eeprom
)
324 /* Destroy EEPROM. */
325 logout("eeprom = 0x%p\n", eeprom
);
326 vmstate_unregister(dev
, &vmstate_eeprom
, eeprom
);
330 uint16_t *eeprom93xx_data(eeprom_t
*eeprom
)
332 /* Get EEPROM data array. */
333 return &eeprom
->contents
[0];