]>
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 | ||
39 | #include <assert.h> | |
87ecb68b | 40 | #include "hw.h" |
663e8e51 TS |
41 | #include "eeprom93xx.h" |
42 | ||
43 | /* Debug EEPROM emulation. */ | |
44 | //~ #define DEBUG_EEPROM | |
45 | ||
46 | #ifdef DEBUG_EEPROM | |
47 | #define logout(fmt, args...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ##args) | |
48 | #else | |
49 | #define logout(fmt, args...) ((void)0) | |
50 | #endif | |
51 | ||
52 | static int eeprom_instance = 0; | |
53 | static const int eeprom_version = 20061112; | |
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; | |
86 | uint8_t size; | |
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); | |
109 | qemu_put_byte(f, eeprom->size); | |
110 | qemu_put_byte(f, 0); /* padding for compatiblity */ | |
663e8e51 TS |
111 | qemu_put_be16(f, eeprom->data); |
112 | for (address = 0; address < eeprom->size; address++) { | |
113 | qemu_put_be16(f, eeprom->contents[address]); | |
114 | } | |
115 | } | |
116 | ||
117 | static int eeprom_load(QEMUFile *f, void *opaque, int version_id) | |
118 | { | |
119 | /* Load EEPROM data from saved data if version and EEPROM size | |
120 | of data and current EEPROM are identical. */ | |
121 | eeprom_t *eeprom = (eeprom_t *)opaque; | |
122 | int result = -EINVAL; | |
123 | if (version_id == eeprom_version) { | |
124 | unsigned address; | |
125 | uint8_t size = eeprom->size; | |
d4ae799c AJ |
126 | |
127 | eeprom->tick = qemu_get_byte(f); | |
128 | eeprom->address = qemu_get_byte(f); | |
129 | eeprom->command = qemu_get_byte(f); | |
130 | eeprom->writeable = qemu_get_byte(f); | |
131 | ||
132 | eeprom->eecs = qemu_get_byte(f); | |
133 | eeprom->eesk = qemu_get_byte(f); | |
134 | eeprom->eedo = qemu_get_byte(f); | |
135 | ||
136 | eeprom->addrbits = qemu_get_byte(f); | |
137 | eeprom->size = qemu_get_byte(f); | |
138 | qemu_get_byte(f); /* skip padding byte */ | |
139 | ||
663e8e51 TS |
140 | if (eeprom->size == size) { |
141 | eeprom->data = qemu_get_be16(f); | |
142 | for (address = 0; address < eeprom->size; address++) { | |
143 | eeprom->contents[address] = qemu_get_be16(f); | |
144 | } | |
145 | result = 0; | |
146 | } | |
147 | } | |
148 | return result; | |
149 | } | |
150 | ||
151 | void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi) | |
152 | { | |
153 | uint8_t tick = eeprom->tick; | |
154 | uint8_t eedo = eeprom->eedo; | |
155 | uint16_t address = eeprom->address; | |
156 | uint8_t command = eeprom->command; | |
157 | ||
158 | logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n", | |
159 | eecs, eesk, eedi, eedo, tick); | |
160 | ||
161 | if (! eeprom->eecs && eecs) { | |
162 | /* Start chip select cycle. */ | |
163 | logout("Cycle start, waiting for 1st start bit (0)\n"); | |
164 | tick = 0; | |
165 | command = 0x0; | |
166 | address = 0x0; | |
167 | } else if (eeprom->eecs && ! eecs) { | |
168 | /* End chip select cycle. This triggers write / erase. */ | |
169 | if (eeprom->writeable) { | |
170 | uint8_t subcommand = address >> (eeprom->addrbits - 2); | |
171 | if (command == 0 && subcommand == 2) { | |
172 | /* Erase all. */ | |
173 | for (address = 0; address < eeprom->size; address++) { | |
174 | eeprom->contents[address] = 0xffff; | |
175 | } | |
176 | } else if (command == 3) { | |
177 | /* Erase word. */ | |
178 | eeprom->contents[address] = 0xffff; | |
179 | } else if (tick >= 2 + 2 + eeprom->addrbits + 16) { | |
180 | if (command == 1) { | |
181 | /* Write word. */ | |
182 | eeprom->contents[address] &= eeprom->data; | |
183 | } else if (command == 0 && subcommand == 1) { | |
184 | /* Write all. */ | |
185 | for (address = 0; address < eeprom->size; address++) { | |
186 | eeprom->contents[address] &= eeprom->data; | |
187 | } | |
188 | } | |
189 | } | |
190 | } | |
191 | /* Output DO is tristate, read results in 1. */ | |
192 | eedo = 1; | |
193 | } else if (eecs && ! eeprom->eesk && eesk) { | |
194 | /* Raising edge of clock shifts data in. */ | |
195 | if (tick == 0) { | |
196 | /* Wait for 1st start bit. */ | |
197 | if (eedi == 0) { | |
198 | logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n"); | |
199 | tick++; | |
200 | } else { | |
201 | logout("wrong 1st start bit (is 1, should be 0)\n"); | |
202 | tick = 2; | |
203 | //~ assert(!"wrong start bit"); | |
204 | } | |
205 | } else if (tick == 1) { | |
206 | /* Wait for 2nd start bit. */ | |
207 | if (eedi != 0) { | |
208 | logout("Got correct 2nd start bit, getting command + address\n"); | |
209 | tick++; | |
210 | } else { | |
211 | logout("1st start bit is longer than needed\n"); | |
212 | } | |
213 | } else if (tick < 2 + 2) { | |
214 | /* Got 2 start bits, transfer 2 opcode bits. */ | |
215 | tick++; | |
216 | command <<= 1; | |
217 | if (eedi) { | |
218 | command += 1; | |
219 | } | |
220 | } else if (tick < 2 + 2 + eeprom->addrbits) { | |
221 | /* Got 2 start bits and 2 opcode bits, transfer all address bits. */ | |
222 | tick++; | |
223 | address = ((address << 1) | eedi); | |
224 | if (tick == 2 + 2 + eeprom->addrbits) { | |
225 | logout("%s command, address = 0x%02x (value 0x%04x)\n", | |
226 | opstring[command], address, eeprom->contents[address]); | |
227 | if (command == 2) { | |
228 | eedo = 0; | |
229 | } | |
230 | address = address % eeprom->size; | |
231 | if (command == 0) { | |
232 | /* Command code in upper 2 bits of address. */ | |
233 | switch (address >> (eeprom->addrbits - 2)) { | |
234 | case 0: | |
235 | logout("write disable command\n"); | |
236 | eeprom->writeable = 0; | |
237 | break; | |
238 | case 1: | |
239 | logout("write all command\n"); | |
240 | break; | |
241 | case 2: | |
242 | logout("erase all command\n"); | |
243 | break; | |
244 | case 3: | |
245 | logout("write enable command\n"); | |
246 | eeprom->writeable = 1; | |
247 | break; | |
248 | } | |
249 | } else { | |
250 | /* Read, write or erase word. */ | |
251 | eeprom->data = eeprom->contents[address]; | |
252 | } | |
253 | } | |
254 | } else if (tick < 2 + 2 + eeprom->addrbits + 16) { | |
255 | /* Transfer 16 data bits. */ | |
256 | tick++; | |
257 | if (command == 2) { | |
258 | /* Read word. */ | |
259 | eedo = ((eeprom->data & 0x8000) != 0); | |
260 | } | |
261 | eeprom->data <<= 1; | |
262 | eeprom->data += eedi; | |
263 | } else { | |
264 | logout("additional unneeded tick, not processed\n"); | |
265 | } | |
266 | } | |
267 | /* Save status of EEPROM. */ | |
268 | eeprom->tick = tick; | |
269 | eeprom->eecs = eecs; | |
270 | eeprom->eesk = eesk; | |
271 | eeprom->eedo = eedo; | |
272 | eeprom->address = address; | |
273 | eeprom->command = command; | |
274 | } | |
275 | ||
276 | uint16_t eeprom93xx_read(eeprom_t *eeprom) | |
277 | { | |
278 | /* Return status of pin DO (0 or 1). */ | |
279 | logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo); | |
280 | return (eeprom->eedo); | |
281 | } | |
282 | ||
283 | #if 0 | |
284 | void eeprom93xx_reset(eeprom_t *eeprom) | |
285 | { | |
286 | /* prepare eeprom */ | |
287 | logout("eeprom = 0x%p\n", eeprom); | |
288 | eeprom->tick = 0; | |
289 | eeprom->command = 0; | |
290 | } | |
291 | #endif | |
292 | ||
293 | eeprom_t *eeprom93xx_new(uint16_t nwords) | |
294 | { | |
295 | /* Add a new EEPROM (with 16, 64 or 256 words). */ | |
296 | eeprom_t *eeprom; | |
297 | uint8_t addrbits; | |
298 | ||
299 | switch (nwords) { | |
300 | case 16: | |
301 | case 64: | |
302 | addrbits = 6; | |
303 | break; | |
304 | case 128: | |
305 | case 256: | |
306 | addrbits = 8; | |
307 | break; | |
308 | default: | |
309 | assert(!"Unsupported EEPROM size, fallback to 64 words!"); | |
310 | nwords = 64; | |
311 | addrbits = 6; | |
312 | } | |
313 | ||
314 | eeprom = (eeprom_t *)qemu_mallocz(sizeof(*eeprom) + nwords * 2); | |
315 | eeprom->size = nwords; | |
316 | eeprom->addrbits = addrbits; | |
317 | /* Output DO is tristate, read results in 1. */ | |
318 | eeprom->eedo = 1; | |
319 | logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords); | |
320 | register_savevm("eeprom", eeprom_instance, eeprom_version, | |
321 | eeprom_save, eeprom_load, eeprom); | |
322 | return eeprom; | |
323 | } | |
324 | ||
325 | void eeprom93xx_free(eeprom_t *eeprom) | |
326 | { | |
327 | /* Destroy EEPROM. */ | |
328 | logout("eeprom = 0x%p\n", eeprom); | |
329 | qemu_free(eeprom); | |
330 | } | |
331 | ||
332 | uint16_t *eeprom93xx_data(eeprom_t *eeprom) | |
333 | { | |
334 | /* Get EEPROM data array. */ | |
335 | return &eeprom->contents[0]; | |
336 | } | |
337 | ||
338 | /* eof */ |