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