]> git.proxmox.com Git - mirror_qemu.git/blame - hw/nvram/eeprom93xx.c
Include migration/vmstate.h less
[mirror_qemu.git] / hw / nvram / eeprom93xx.c
CommitLineData
663e8e51
TS
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
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
663e8e51
TS
18 */
19
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.
26 *
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
33 *
34 * Todo list:
35 * - No emulation of EEPROM timings.
36 */
37
0430891c 38#include "qemu/osdep.h"
83c9f4ca 39#include "hw/hw.h"
0d09e41a 40#include "hw/nvram/eeprom93xx.h"
ca77ee28 41#include "migration/qemu-file-types.h"
d6454270 42#include "migration/vmstate.h"
663e8e51
TS
43
44/* Debug EEPROM emulation. */
45//~ #define DEBUG_EEPROM
46
47#ifdef DEBUG_EEPROM
001faf32 48#define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
663e8e51 49#else
001faf32 50#define logout(fmt, ...) ((void)0)
663e8e51
TS
51#endif
52
7ab2589c
AJ
53#define EEPROM_INSTANCE 0
54#define OLD_EEPROM_VERSION 20061112
55#define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
663e8e51
TS
56
57#if 0
58typedef 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
70
71#ifdef DEBUG_EEPROM
72static const char *opstring[] = {
73 "extended", "write", "read", "erase"
74};
75#endif
76
c227f099 77struct _eeprom_t {
663e8e51
TS
78 uint8_t tick;
79 uint8_t address;
80 uint8_t command;
ebabb67a 81 uint8_t writable;
663e8e51
TS
82
83 uint8_t eecs;
84 uint8_t eesk;
85 uint8_t eedo;
86
87 uint8_t addrbits;
7ab2589c 88 uint16_t size;
663e8e51
TS
89 uint16_t data;
90 uint16_t contents[0];
91};
92
93/* Code for saving and restoring of EEPROM state. */
94
c4a0f2d3
JQ
95/* Restore an uint16_t from an uint8_t
96 This is a Big hack, but it is how the old state did it.
97 */
98
2c21ee76 99static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size,
03fee66f 100 const VMStateField *field)
663e8e51 101{
c4a0f2d3
JQ
102 uint16_t *v = pv;
103 *v = qemu_get_ubyte(f);
104 return 0;
105}
d4ae799c 106
03fee66f
MAL
107static int put_unused(QEMUFile *f, void *pv, size_t size,
108 const VMStateField *field, QJSON *vmdesc)
c4a0f2d3
JQ
109{
110 fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n");
111 fprintf(stderr, "Never should be used to write a new state.\n");
112 exit(0);
2c21ee76
JD
113
114 return 0;
c4a0f2d3 115}
d4ae799c 116
d05ac8fa 117static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
c4a0f2d3
JQ
118 .name = "uint16_from_uint8",
119 .get = get_uint16_from_uint8,
120 .put = put_unused,
121};
d4ae799c 122
c4a0f2d3
JQ
123#define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \
124 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
663e8e51 125
c4a0f2d3 126static bool is_old_eeprom_version(void *opaque, int version_id)
663e8e51 127{
c4a0f2d3
JQ
128 return version_id == OLD_EEPROM_VERSION;
129}
d4ae799c 130
c4a0f2d3
JQ
131static const VMStateDescription vmstate_eeprom = {
132 .name = "eeprom",
133 .version_id = EEPROM_VERSION,
134 .minimum_version_id = OLD_EEPROM_VERSION,
d49805ae 135 .fields = (VMStateField[]) {
c4a0f2d3
JQ
136 VMSTATE_UINT8(tick, eeprom_t),
137 VMSTATE_UINT8(address, eeprom_t),
138 VMSTATE_UINT8(command, eeprom_t),
ebabb67a 139 VMSTATE_UINT8(writable, eeprom_t),
d4ae799c 140
c4a0f2d3
JQ
141 VMSTATE_UINT8(eecs, eeprom_t),
142 VMSTATE_UINT8(eesk, eeprom_t),
143 VMSTATE_UINT8(eedo, eeprom_t),
d4ae799c 144
c4a0f2d3
JQ
145 VMSTATE_UINT8(addrbits, eeprom_t),
146 VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
147 VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
d2164ad3 148 VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION, NULL),
c4a0f2d3
JQ
149 VMSTATE_UINT16(data, eeprom_t),
150 VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
151 vmstate_info_uint16, uint16_t),
152 VMSTATE_END_OF_LIST()
663e8e51 153 }
c4a0f2d3 154};
663e8e51 155
c227f099 156void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
663e8e51
TS
157{
158 uint8_t tick = eeprom->tick;
159 uint8_t eedo = eeprom->eedo;
160 uint16_t address = eeprom->address;
161 uint8_t command = eeprom->command;
162
163 logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n",
164 eecs, eesk, eedi, eedo, tick);
165
6fedcaa1 166 if (!eeprom->eecs && eecs) {
663e8e51
TS
167 /* Start chip select cycle. */
168 logout("Cycle start, waiting for 1st start bit (0)\n");
169 tick = 0;
170 command = 0x0;
171 address = 0x0;
6fedcaa1 172 } else if (eeprom->eecs && !eecs) {
663e8e51 173 /* End chip select cycle. This triggers write / erase. */
ebabb67a 174 if (eeprom->writable) {
663e8e51
TS
175 uint8_t subcommand = address >> (eeprom->addrbits - 2);
176 if (command == 0 && subcommand == 2) {
177 /* Erase all. */
178 for (address = 0; address < eeprom->size; address++) {
179 eeprom->contents[address] = 0xffff;
180 }
181 } else if (command == 3) {
182 /* Erase word. */
183 eeprom->contents[address] = 0xffff;
184 } else if (tick >= 2 + 2 + eeprom->addrbits + 16) {
185 if (command == 1) {
186 /* Write word. */
187 eeprom->contents[address] &= eeprom->data;
188 } else if (command == 0 && subcommand == 1) {
189 /* Write all. */
190 for (address = 0; address < eeprom->size; address++) {
191 eeprom->contents[address] &= eeprom->data;
192 }
193 }
194 }
195 }
196 /* Output DO is tristate, read results in 1. */
197 eedo = 1;
6fedcaa1 198 } else if (eecs && !eeprom->eesk && eesk) {
663e8e51
TS
199 /* Raising edge of clock shifts data in. */
200 if (tick == 0) {
201 /* Wait for 1st start bit. */
202 if (eedi == 0) {
203 logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n");
204 tick++;
205 } else {
206 logout("wrong 1st start bit (is 1, should be 0)\n");
207 tick = 2;
208 //~ assert(!"wrong start bit");
209 }
210 } else if (tick == 1) {
211 /* Wait for 2nd start bit. */
212 if (eedi != 0) {
213 logout("Got correct 2nd start bit, getting command + address\n");
214 tick++;
215 } else {
216 logout("1st start bit is longer than needed\n");
217 }
218 } else if (tick < 2 + 2) {
219 /* Got 2 start bits, transfer 2 opcode bits. */
220 tick++;
221 command <<= 1;
222 if (eedi) {
223 command += 1;
224 }
225 } else if (tick < 2 + 2 + eeprom->addrbits) {
226 /* Got 2 start bits and 2 opcode bits, transfer all address bits. */
227 tick++;
228 address = ((address << 1) | eedi);
229 if (tick == 2 + 2 + eeprom->addrbits) {
230 logout("%s command, address = 0x%02x (value 0x%04x)\n",
231 opstring[command], address, eeprom->contents[address]);
232 if (command == 2) {
233 eedo = 0;
234 }
235 address = address % eeprom->size;
236 if (command == 0) {
237 /* Command code in upper 2 bits of address. */
238 switch (address >> (eeprom->addrbits - 2)) {
6fedcaa1
AP
239 case 0:
240 logout("write disable command\n");
241 eeprom->writable = 0;
242 break;
243 case 1:
244 logout("write all command\n");
245 break;
246 case 2:
247 logout("erase all command\n");
248 break;
249 case 3:
250 logout("write enable command\n");
251 eeprom->writable = 1;
252 break;
663e8e51
TS
253 }
254 } else {
255 /* Read, write or erase word. */
256 eeprom->data = eeprom->contents[address];
257 }
258 }
259 } else if (tick < 2 + 2 + eeprom->addrbits + 16) {
260 /* Transfer 16 data bits. */
261 tick++;
262 if (command == 2) {
263 /* Read word. */
264 eedo = ((eeprom->data & 0x8000) != 0);
265 }
266 eeprom->data <<= 1;
267 eeprom->data += eedi;
268 } else {
269 logout("additional unneeded tick, not processed\n");
270 }
271 }
272 /* Save status of EEPROM. */
273 eeprom->tick = tick;
274 eeprom->eecs = eecs;
275 eeprom->eesk = eesk;
276 eeprom->eedo = eedo;
277 eeprom->address = address;
278 eeprom->command = command;
279}
280
c227f099 281uint16_t eeprom93xx_read(eeprom_t *eeprom)
663e8e51
TS
282{
283 /* Return status of pin DO (0 or 1). */
284 logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo);
6fedcaa1 285 return eeprom->eedo;
663e8e51
TS
286}
287
288#if 0
289void eeprom93xx_reset(eeprom_t *eeprom)
290{
291 /* prepare eeprom */
292 logout("eeprom = 0x%p\n", eeprom);
293 eeprom->tick = 0;
294 eeprom->command = 0;
295}
296#endif
297
5fce2b3e 298eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords)
663e8e51
TS
299{
300 /* Add a new EEPROM (with 16, 64 or 256 words). */
c227f099 301 eeprom_t *eeprom;
663e8e51
TS
302 uint8_t addrbits;
303
304 switch (nwords) {
6fedcaa1
AP
305 case 16:
306 case 64:
307 addrbits = 6;
308 break;
309 case 128:
310 case 256:
311 addrbits = 8;
312 break;
313 default:
314 assert(!"Unsupported EEPROM size, fallback to 64 words!");
315 nwords = 64;
316 addrbits = 6;
663e8e51
TS
317 }
318
7267c094 319 eeprom = (eeprom_t *)g_malloc0(sizeof(*eeprom) + nwords * 2);
663e8e51
TS
320 eeprom->size = nwords;
321 eeprom->addrbits = addrbits;
322 /* Output DO is tristate, read results in 1. */
323 eeprom->eedo = 1;
324 logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
5fce2b3e 325 vmstate_register(dev, 0, &vmstate_eeprom, eeprom);
663e8e51
TS
326 return eeprom;
327}
328
5fce2b3e 329void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom)
663e8e51
TS
330{
331 /* Destroy EEPROM. */
332 logout("eeprom = 0x%p\n", eeprom);
5fce2b3e 333 vmstate_unregister(dev, &vmstate_eeprom, eeprom);
7267c094 334 g_free(eeprom);
663e8e51
TS
335}
336
c227f099 337uint16_t *eeprom93xx_data(eeprom_t *eeprom)
663e8e51
TS
338{
339 /* Get EEPROM data array. */
340 return &eeprom->contents[0];
341}
342
343/* eof */