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