blob: 30a35340ad0bab175f3f617d067f4e8833afbacd [file] [log] [blame]
ths663e8e52007-04-02 12:35:34 +00001/*
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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
ths663e8e52007-04-02 12:35:34 +000018 */
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
pbrook87ecb682007-11-17 17:14:51 +000038#include "hw.h"
ths663e8e52007-04-02 12:35:34 +000039#include "eeprom93xx.h"
40
41/* Debug EEPROM emulation. */
42//~ #define DEBUG_EEPROM
43
44#ifdef DEBUG_EEPROM
Blue Swirl001faf32009-05-13 17:53:17 +000045#define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
ths663e8e52007-04-02 12:35:34 +000046#else
Blue Swirl001faf32009-05-13 17:53:17 +000047#define logout(fmt, ...) ((void)0)
ths663e8e52007-04-02 12:35:34 +000048#endif
49
aurel327ab25892009-03-28 23:14:52 +000050#define EEPROM_INSTANCE 0
51#define OLD_EEPROM_VERSION 20061112
52#define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
ths663e8e52007-04-02 12:35:34 +000053
54#if 0
55typedef 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
69static const char *opstring[] = {
70 "extended", "write", "read", "erase"
71};
72#endif
73
Anthony Liguoric227f092009-10-01 16:12:16 -050074struct _eeprom_t {
ths663e8e52007-04-02 12:35:34 +000075 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;
aurel327ab25892009-03-28 23:14:52 +000085 uint16_t size;
ths663e8e52007-04-02 12:35:34 +000086 uint16_t data;
87 uint16_t contents[0];
88};
89
90/* Code for saving and restoring of EEPROM state. */
91
Juan Quintelac4a0f2d2009-10-16 12:13:53 +020092/* Restore an uint16_t from an uint8_t
93 This is a Big hack, but it is how the old state did it.
94 */
95
96static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size)
ths663e8e52007-04-02 12:35:34 +000097{
Juan Quintelac4a0f2d2009-10-16 12:13:53 +020098 uint16_t *v = pv;
99 *v = qemu_get_ubyte(f);
100 return 0;
ths663e8e52007-04-02 12:35:34 +0000101}
102
Juan Quintelac4a0f2d2009-10-16 12:13:53 +0200103static void put_unused(QEMUFile *f, void *pv, size_t size)
ths663e8e52007-04-02 12:35:34 +0000104{
Juan Quintelac4a0f2d2009-10-16 12:13:53 +0200105 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");
107 exit(0);
ths663e8e52007-04-02 12:35:34 +0000108}
109
Blue Swirld05ac8f2009-12-04 20:44:44 +0000110static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
Juan Quintelac4a0f2d2009-10-16 12:13:53 +0200111 .name = "uint16_from_uint8",
112 .get = get_uint16_from_uint8,
113 .put = put_unused,
114};
115
116#define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \
117 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
118
119static bool is_old_eeprom_version(void *opaque, int version_id)
120{
121 return version_id == OLD_EEPROM_VERSION;
122}
123
124static const VMStateDescription vmstate_eeprom = {
125 .name = "eeprom",
126 .version_id = EEPROM_VERSION,
127 .minimum_version_id = OLD_EEPROM_VERSION,
128 .minimum_version_id_old = OLD_EEPROM_VERSION,
129 .fields = (VMStateField []) {
130 VMSTATE_UINT8(tick, eeprom_t),
131 VMSTATE_UINT8(address, eeprom_t),
132 VMSTATE_UINT8(command, eeprom_t),
133 VMSTATE_UINT8(writeable, eeprom_t),
134
135 VMSTATE_UINT8(eecs, eeprom_t),
136 VMSTATE_UINT8(eesk, eeprom_t),
137 VMSTATE_UINT8(eedo, eeprom_t),
138
139 VMSTATE_UINT8(addrbits, eeprom_t),
140 VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
141 VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
142 VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION),
143 VMSTATE_UINT16(data, eeprom_t),
144 VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
145 vmstate_info_uint16, uint16_t),
146 VMSTATE_END_OF_LIST()
147 }
148};
149
Anthony Liguoric227f092009-10-01 16:12:16 -0500150void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
ths663e8e52007-04-02 12:35:34 +0000151{
152 uint8_t tick = eeprom->tick;
153 uint8_t eedo = eeprom->eedo;
154 uint16_t address = eeprom->address;
155 uint8_t command = eeprom->command;
156
157 logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n",
158 eecs, eesk, eedi, eedo, tick);
159
160 if (! eeprom->eecs && eecs) {
161 /* Start chip select cycle. */
162 logout("Cycle start, waiting for 1st start bit (0)\n");
163 tick = 0;
164 command = 0x0;
165 address = 0x0;
166 } else if (eeprom->eecs && ! eecs) {
167 /* End chip select cycle. This triggers write / erase. */
168 if (eeprom->writeable) {
169 uint8_t subcommand = address >> (eeprom->addrbits - 2);
170 if (command == 0 && subcommand == 2) {
171 /* Erase all. */
172 for (address = 0; address < eeprom->size; address++) {
173 eeprom->contents[address] = 0xffff;
174 }
175 } else if (command == 3) {
176 /* Erase word. */
177 eeprom->contents[address] = 0xffff;
178 } else if (tick >= 2 + 2 + eeprom->addrbits + 16) {
179 if (command == 1) {
180 /* Write word. */
181 eeprom->contents[address] &= eeprom->data;
182 } else if (command == 0 && subcommand == 1) {
183 /* Write all. */
184 for (address = 0; address < eeprom->size; address++) {
185 eeprom->contents[address] &= eeprom->data;
186 }
187 }
188 }
189 }
190 /* Output DO is tristate, read results in 1. */
191 eedo = 1;
192 } else if (eecs && ! eeprom->eesk && eesk) {
193 /* Raising edge of clock shifts data in. */
194 if (tick == 0) {
195 /* Wait for 1st start bit. */
196 if (eedi == 0) {
197 logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n");
198 tick++;
199 } else {
200 logout("wrong 1st start bit (is 1, should be 0)\n");
201 tick = 2;
202 //~ assert(!"wrong start bit");
203 }
204 } else if (tick == 1) {
205 /* Wait for 2nd start bit. */
206 if (eedi != 0) {
207 logout("Got correct 2nd start bit, getting command + address\n");
208 tick++;
209 } else {
210 logout("1st start bit is longer than needed\n");
211 }
212 } else if (tick < 2 + 2) {
213 /* Got 2 start bits, transfer 2 opcode bits. */
214 tick++;
215 command <<= 1;
216 if (eedi) {
217 command += 1;
218 }
219 } else if (tick < 2 + 2 + eeprom->addrbits) {
220 /* Got 2 start bits and 2 opcode bits, transfer all address bits. */
221 tick++;
222 address = ((address << 1) | eedi);
223 if (tick == 2 + 2 + eeprom->addrbits) {
224 logout("%s command, address = 0x%02x (value 0x%04x)\n",
225 opstring[command], address, eeprom->contents[address]);
226 if (command == 2) {
227 eedo = 0;
228 }
229 address = address % eeprom->size;
230 if (command == 0) {
231 /* Command code in upper 2 bits of address. */
232 switch (address >> (eeprom->addrbits - 2)) {
233 case 0:
234 logout("write disable command\n");
235 eeprom->writeable = 0;
236 break;
237 case 1:
238 logout("write all command\n");
239 break;
240 case 2:
241 logout("erase all command\n");
242 break;
243 case 3:
244 logout("write enable command\n");
245 eeprom->writeable = 1;
246 break;
247 }
248 } else {
249 /* Read, write or erase word. */
250 eeprom->data = eeprom->contents[address];
251 }
252 }
253 } else if (tick < 2 + 2 + eeprom->addrbits + 16) {
254 /* Transfer 16 data bits. */
255 tick++;
256 if (command == 2) {
257 /* Read word. */
258 eedo = ((eeprom->data & 0x8000) != 0);
259 }
260 eeprom->data <<= 1;
261 eeprom->data += eedi;
262 } else {
263 logout("additional unneeded tick, not processed\n");
264 }
265 }
266 /* Save status of EEPROM. */
267 eeprom->tick = tick;
268 eeprom->eecs = eecs;
269 eeprom->eesk = eesk;
270 eeprom->eedo = eedo;
271 eeprom->address = address;
272 eeprom->command = command;
273}
274
Anthony Liguoric227f092009-10-01 16:12:16 -0500275uint16_t eeprom93xx_read(eeprom_t *eeprom)
ths663e8e52007-04-02 12:35:34 +0000276{
277 /* Return status of pin DO (0 or 1). */
278 logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo);
279 return (eeprom->eedo);
280}
281
282#if 0
283void eeprom93xx_reset(eeprom_t *eeprom)
284{
285 /* prepare eeprom */
286 logout("eeprom = 0x%p\n", eeprom);
287 eeprom->tick = 0;
288 eeprom->command = 0;
289}
290#endif
291
Anthony Liguoric227f092009-10-01 16:12:16 -0500292eeprom_t *eeprom93xx_new(uint16_t nwords)
ths663e8e52007-04-02 12:35:34 +0000293{
294 /* Add a new EEPROM (with 16, 64 or 256 words). */
Anthony Liguoric227f092009-10-01 16:12:16 -0500295 eeprom_t *eeprom;
ths663e8e52007-04-02 12:35:34 +0000296 uint8_t addrbits;
297
298 switch (nwords) {
299 case 16:
300 case 64:
301 addrbits = 6;
302 break;
303 case 128:
304 case 256:
305 addrbits = 8;
306 break;
307 default:
308 assert(!"Unsupported EEPROM size, fallback to 64 words!");
309 nwords = 64;
310 addrbits = 6;
311 }
312
Anthony Liguoric227f092009-10-01 16:12:16 -0500313 eeprom = (eeprom_t *)qemu_mallocz(sizeof(*eeprom) + nwords * 2);
ths663e8e52007-04-02 12:35:34 +0000314 eeprom->size = nwords;
315 eeprom->addrbits = addrbits;
316 /* Output DO is tristate, read results in 1. */
317 eeprom->eedo = 1;
318 logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
Juan Quintelac4a0f2d2009-10-16 12:13:53 +0200319 vmstate_register(0, &vmstate_eeprom, eeprom);
ths663e8e52007-04-02 12:35:34 +0000320 return eeprom;
321}
322
Anthony Liguoric227f092009-10-01 16:12:16 -0500323void eeprom93xx_free(eeprom_t *eeprom)
ths663e8e52007-04-02 12:35:34 +0000324{
325 /* Destroy EEPROM. */
326 logout("eeprom = 0x%p\n", eeprom);
Juan Quintelac4a0f2d2009-10-16 12:13:53 +0200327 vmstate_unregister(&vmstate_eeprom, eeprom);
ths663e8e52007-04-02 12:35:34 +0000328 qemu_free(eeprom);
329}
330
Anthony Liguoric227f092009-10-01 16:12:16 -0500331uint16_t *eeprom93xx_data(eeprom_t *eeprom)
ths663e8e52007-04-02 12:35:34 +0000332{
333 /* Get EEPROM data array. */
334 return &eeprom->contents[0];
335}
336
337/* eof */