Update GitHub action for new Meson based build
[qemu/ar7.git] / hw / nvram / eeprom93xx.c
blobfba1260b8156506b7e9ee0344247e4465d640b4d
1 /*
2 * QEMU EEPROM 93xx emulation
4 * Copyright (c) 2006-2009 Stefan Weil
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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.
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
34 * Hint: This driver always uses host endianness!
36 * Todo list:
37 * - No emulation of EEPROM timings.
40 #include "qemu/osdep.h"
41 #include "hw/nvram/eeprom93xx.h"
42 #include "migration/qemu-file-types.h"
43 #include "migration/vmstate.h"
45 /* Debug EEPROM emulation. */
46 //~ #define DEBUG_EEPROM
48 #ifdef DEBUG_EEPROM
49 #define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
50 #else
51 #define logout(fmt, ...) ((void)0)
52 #endif
54 #define EEPROM_INSTANCE 0
55 #define OLD_EEPROM_VERSION 20061112
56 #define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
58 #if 0
59 typedef enum {
60 eeprom_read = 0x80, /* read register xx */
61 eeprom_write = 0x40, /* write register xx */
62 eeprom_erase = 0xc0, /* erase register xx */
63 eeprom_ewen = 0x30, /* erase / write enable */
64 eeprom_ewds = 0x00, /* erase / write disable */
65 eeprom_eral = 0x20, /* erase all registers */
66 eeprom_wral = 0x10, /* write all registers */
67 eeprom_amask = 0x0f,
68 eeprom_imask = 0xf0
69 } eeprom_instruction_t;
70 #endif
72 #ifdef DEBUG_EEPROM
73 static const char *opstring[] = {
74 "extended", "write", "read", "erase"
76 #endif
78 struct _eeprom_t {
79 uint8_t tick;
80 uint8_t address;
81 uint8_t command;
82 uint8_t writable;
84 uint8_t eecs;
85 uint8_t eesk;
86 uint8_t eedo;
88 uint8_t addrbits;
89 uint16_t size;
90 uint16_t data;
91 uint16_t contents[];
94 /* Code for saving and restoring of EEPROM state. */
96 /* Restore an uint16_t from an uint8_t
97 This is a Big hack, but it is how the old state did it.
100 static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size,
101 const VMStateField *field)
103 uint16_t *v = pv;
104 *v = qemu_get_ubyte(f);
105 return 0;
108 static int put_unused(QEMUFile *f, void *pv, size_t size,
109 const VMStateField *field, QJSON *vmdesc)
111 fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n");
112 fprintf(stderr, "Never should be used to write a new state.\n");
113 exit(0);
115 return 0;
118 static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
119 .name = "uint16_from_uint8",
120 .get = get_uint16_from_uint8,
121 .put = put_unused,
124 #define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \
125 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
127 static bool is_old_eeprom_version(void *opaque, int version_id)
129 return version_id == OLD_EEPROM_VERSION;
132 static const VMStateDescription vmstate_eeprom = {
133 .name = "eeprom",
134 .version_id = EEPROM_VERSION,
135 .minimum_version_id = OLD_EEPROM_VERSION,
136 .fields = (VMStateField[]) {
137 VMSTATE_UINT8(tick, eeprom_t),
138 VMSTATE_UINT8(address, eeprom_t),
139 VMSTATE_UINT8(command, eeprom_t),
140 VMSTATE_UINT8(writable, eeprom_t),
142 VMSTATE_UINT8(eecs, eeprom_t),
143 VMSTATE_UINT8(eesk, eeprom_t),
144 VMSTATE_UINT8(eedo, eeprom_t),
146 VMSTATE_UINT8(addrbits, eeprom_t),
147 VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
148 VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
149 VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION, NULL),
150 VMSTATE_UINT16(data, eeprom_t),
151 VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
152 vmstate_info_uint16, uint16_t),
153 VMSTATE_END_OF_LIST()
157 void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
159 uint8_t tick = eeprom->tick;
160 uint8_t eedo = eeprom->eedo;
161 uint16_t address = eeprom->address;
162 uint8_t command = eeprom->command;
164 logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n",
165 eecs, eesk, eedi, eedo, tick);
167 if (!eeprom->eecs && eecs) {
168 /* Start chip select cycle. */
169 logout("Cycle start, waiting for 1st start bit (0)\n");
170 tick = 0;
171 command = 0x0;
172 address = 0x0;
173 } else if (eeprom->eecs && !eecs) {
174 /* End chip select cycle. This triggers write / erase. */
175 if (eeprom->writable) {
176 uint8_t subcommand = address >> (eeprom->addrbits - 2);
177 if (command == 0 && subcommand == 2) {
178 /* Erase all. */
179 for (address = 0; address < eeprom->size; address++) {
180 eeprom->contents[address] = 0xffff;
182 } else if (command == 3) {
183 /* Erase word. */
184 eeprom->contents[address] = 0xffff;
185 } else if (tick >= 2 + 2 + eeprom->addrbits + 16) {
186 if (command == 1) {
187 /* Write word. */
188 eeprom->contents[address] &= eeprom->data;
189 } else if (command == 0 && subcommand == 1) {
190 /* Write all. */
191 for (address = 0; address < eeprom->size; address++) {
192 eeprom->contents[address] &= eeprom->data;
197 /* Output DO is tristate, read results in 1. */
198 eedo = 1;
199 } else if (eecs && !eeprom->eesk && eesk) {
200 /* Raising edge of clock shifts data in. */
201 if (tick == 0) {
202 /* Wait for 1st start bit. */
203 if (eedi == 0) {
204 logout("Got redundant bit (0), waiting for start bit (1)\n");
205 } else {
206 logout("Got start bit (1), getting command + address\n");
207 tick++;
209 } else if (tick < 1 + 2) {
210 /* Got 1 start bit, transfer 2 opcode bits. */
211 tick++;
212 command <<= 1;
213 if (eedi) {
214 command += 1;
216 } else if (tick < 1 + 2 + eeprom->addrbits) {
217 /* Got 2 start bits and 2 opcode bits, transfer all address bits. */
218 tick++;
219 address = ((address << 1) | eedi);
220 if (tick == 1 + 2 + eeprom->addrbits) {
221 logout("%s command, address = 0x%02x (value 0x%04x)\n",
222 opstring[command], address, eeprom->contents[address]);
223 if (command == 2) {
224 eedo = 0;
226 address = address % eeprom->size;
227 if (command == 0) {
228 /* Command code in upper 2 bits of address. */
229 switch (address >> (eeprom->addrbits - 2)) {
230 case 0:
231 logout("write disable command\n");
232 eeprom->writable = 0;
233 break;
234 case 1:
235 logout("write all command\n");
236 break;
237 case 2:
238 logout("erase all command\n");
239 break;
240 case 3:
241 logout("write enable command\n");
242 eeprom->writable = 1;
243 break;
245 } else {
246 /* Read, write or erase word. */
247 eeprom->data = eeprom->contents[address];
250 } else if (tick < 1 + 2 + eeprom->addrbits + 16) {
251 /* Transfer 16 data bits. */
252 tick++;
253 if (command == 2) {
254 /* Read word. */
255 eedo = ((eeprom->data & 0x8000) != 0);
257 eeprom->data <<= 1;
258 eeprom->data += eedi;
259 } else {
260 logout("additional unneeded tick, not processed\n");
263 /* Save status of EEPROM. */
264 eeprom->tick = tick;
265 eeprom->eecs = eecs;
266 eeprom->eesk = eesk;
267 eeprom->eedo = eedo;
268 eeprom->address = address;
269 eeprom->command = command;
272 uint16_t eeprom93xx_read(eeprom_t *eeprom)
274 /* Return status of pin DO (0 or 1). */
275 logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo);
276 return eeprom->eedo;
279 #if 0
280 void eeprom93xx_reset(eeprom_t *eeprom)
282 /* prepare eeprom */
283 logout("eeprom = 0x%p\n", eeprom);
284 eeprom->tick = 0;
285 eeprom->command = 0;
287 #endif
289 eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords)
291 /* Add a new EEPROM (with 16, 64 or 256 words). */
292 eeprom_t *eeprom;
293 uint8_t addrbits;
295 switch (nwords) {
296 case 16:
297 case 64:
298 addrbits = 6;
299 break;
300 case 128:
301 case 256:
302 addrbits = 8;
303 break;
304 default:
305 assert(!"Unsupported EEPROM size, fallback to 64 words!");
306 nwords = 64;
307 addrbits = 6;
310 eeprom = (eeprom_t *)g_malloc0(sizeof(*eeprom) + nwords * 2);
311 eeprom->size = nwords;
312 eeprom->addrbits = addrbits;
313 /* Output DO is tristate, read results in 1. */
314 eeprom->eedo = 1;
315 logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
316 vmstate_register(VMSTATE_IF(dev), 0, &vmstate_eeprom, eeprom);
317 return eeprom;
320 void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom)
322 /* Destroy EEPROM. */
323 logout("eeprom = 0x%p\n", eeprom);
324 vmstate_unregister(VMSTATE_IF(dev), &vmstate_eeprom, eeprom);
325 g_free(eeprom);
328 uint16_t *eeprom93xx_data(eeprom_t *eeprom)
330 /* Get EEPROM data array. */
331 return &eeprom->contents[0];
334 /* eof */