xref: /qemu/hw/nvram/eeprom93xx.c (revision 0430891ce162b986c6e02a7729a942ecd2a32ca4)
1663e8e51Sths /*
2663e8e51Sths  * QEMU EEPROM 93xx emulation
3663e8e51Sths  *
4663e8e51Sths  * Copyright (c) 2006-2007 Stefan Weil
5663e8e51Sths  *
6663e8e51Sths  * This program is free software; you can redistribute it and/or modify
7663e8e51Sths  * it under the terms of the GNU General Public License as published by
8663e8e51Sths  * the Free Software Foundation; either version 2 of the License, or
9663e8e51Sths  * (at your option) any later version.
10663e8e51Sths  *
11663e8e51Sths  * This program is distributed in the hope that it will be useful,
12663e8e51Sths  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13663e8e51Sths  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14663e8e51Sths  * GNU General Public License for more details.
15663e8e51Sths  *
16663e8e51Sths  * You should have received a copy of the GNU General Public License
178167ee88SBlue Swirl  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18663e8e51Sths  */
19663e8e51Sths 
20663e8e51Sths /* Emulation for serial EEPROMs:
21663e8e51Sths  * NMC93C06 256-Bit (16 x 16)
22663e8e51Sths  * NMC93C46 1024-Bit (64 x 16)
23663e8e51Sths  * NMC93C56 2028 Bit (128 x 16)
24663e8e51Sths  * NMC93C66 4096 Bit (256 x 16)
25663e8e51Sths  * Compatible devices include FM93C46 and others.
26663e8e51Sths  *
27663e8e51Sths  * Other drivers use these interface functions:
28663e8e51Sths  * eeprom93xx_new   - add a new EEPROM (with 16, 64 or 256 words)
29663e8e51Sths  * eeprom93xx_free  - destroy EEPROM
30663e8e51Sths  * eeprom93xx_read  - read data from the EEPROM
31663e8e51Sths  * eeprom93xx_write - write data to the EEPROM
32663e8e51Sths  * eeprom93xx_data  - get EEPROM data array for external manipulation
33663e8e51Sths  *
34663e8e51Sths  * Todo list:
35663e8e51Sths  * - No emulation of EEPROM timings.
36663e8e51Sths  */
37663e8e51Sths 
38*0430891cSPeter Maydell #include "qemu/osdep.h"
3983c9f4caSPaolo Bonzini #include "hw/hw.h"
400d09e41aSPaolo Bonzini #include "hw/nvram/eeprom93xx.h"
41663e8e51Sths 
42663e8e51Sths /* Debug EEPROM emulation. */
43663e8e51Sths //~ #define DEBUG_EEPROM
44663e8e51Sths 
45663e8e51Sths #ifdef DEBUG_EEPROM
46001faf32SBlue Swirl #define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
47663e8e51Sths #else
48001faf32SBlue Swirl #define logout(fmt, ...) ((void)0)
49663e8e51Sths #endif
50663e8e51Sths 
517ab2589cSaurel32 #define EEPROM_INSTANCE  0
527ab2589cSaurel32 #define OLD_EEPROM_VERSION 20061112
537ab2589cSaurel32 #define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
54663e8e51Sths 
55663e8e51Sths #if 0
56663e8e51Sths typedef enum {
57663e8e51Sths   eeprom_read  = 0x80,   /* read register xx */
58663e8e51Sths   eeprom_write = 0x40,   /* write register xx */
59663e8e51Sths   eeprom_erase = 0xc0,   /* erase register xx */
60663e8e51Sths   eeprom_ewen  = 0x30,   /* erase / write enable */
61663e8e51Sths   eeprom_ewds  = 0x00,   /* erase / write disable */
62663e8e51Sths   eeprom_eral  = 0x20,   /* erase all registers */
63663e8e51Sths   eeprom_wral  = 0x10,   /* write all registers */
64663e8e51Sths   eeprom_amask = 0x0f,
65663e8e51Sths   eeprom_imask = 0xf0
66663e8e51Sths } eeprom_instruction_t;
67663e8e51Sths #endif
68663e8e51Sths 
69663e8e51Sths #ifdef DEBUG_EEPROM
70663e8e51Sths static const char *opstring[] = {
71663e8e51Sths   "extended", "write", "read", "erase"
72663e8e51Sths };
73663e8e51Sths #endif
74663e8e51Sths 
75c227f099SAnthony Liguori struct _eeprom_t {
76663e8e51Sths     uint8_t  tick;
77663e8e51Sths     uint8_t  address;
78663e8e51Sths     uint8_t  command;
79ebabb67aSStefan Weil     uint8_t  writable;
80663e8e51Sths 
81663e8e51Sths     uint8_t eecs;
82663e8e51Sths     uint8_t eesk;
83663e8e51Sths     uint8_t eedo;
84663e8e51Sths 
85663e8e51Sths     uint8_t  addrbits;
867ab2589cSaurel32     uint16_t size;
87663e8e51Sths     uint16_t data;
88663e8e51Sths     uint16_t contents[0];
89663e8e51Sths };
90663e8e51Sths 
91663e8e51Sths /* Code for saving and restoring of EEPROM state. */
92663e8e51Sths 
93c4a0f2d3SJuan Quintela /* Restore an uint16_t from an uint8_t
94c4a0f2d3SJuan Quintela    This is a Big hack, but it is how the old state did it.
95c4a0f2d3SJuan Quintela  */
96c4a0f2d3SJuan Quintela 
97c4a0f2d3SJuan Quintela static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size)
98663e8e51Sths {
99c4a0f2d3SJuan Quintela     uint16_t *v = pv;
100c4a0f2d3SJuan Quintela     *v = qemu_get_ubyte(f);
101c4a0f2d3SJuan Quintela     return 0;
102663e8e51Sths }
103663e8e51Sths 
104c4a0f2d3SJuan Quintela static void put_unused(QEMUFile *f, void *pv, size_t size)
105663e8e51Sths {
106c4a0f2d3SJuan Quintela     fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n");
107c4a0f2d3SJuan Quintela     fprintf(stderr, "Never should be used to write a new state.\n");
108c4a0f2d3SJuan Quintela     exit(0);
1097ab2589cSaurel32 }
110d4ae799cSaurel32 
111d05ac8faSBlue Swirl static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
112c4a0f2d3SJuan Quintela     .name = "uint16_from_uint8",
113c4a0f2d3SJuan Quintela     .get  = get_uint16_from_uint8,
114c4a0f2d3SJuan Quintela     .put  = put_unused,
115c4a0f2d3SJuan Quintela };
116c4a0f2d3SJuan Quintela 
117c4a0f2d3SJuan Quintela #define VMSTATE_UINT16_HACK_TEST(_f, _s, _t)                           \
118c4a0f2d3SJuan Quintela     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
119c4a0f2d3SJuan Quintela 
120c4a0f2d3SJuan Quintela static bool is_old_eeprom_version(void *opaque, int version_id)
121c4a0f2d3SJuan Quintela {
122c4a0f2d3SJuan Quintela     return version_id == OLD_EEPROM_VERSION;
123663e8e51Sths }
124c4a0f2d3SJuan Quintela 
125c4a0f2d3SJuan Quintela static const VMStateDescription vmstate_eeprom = {
126c4a0f2d3SJuan Quintela     .name = "eeprom",
127c4a0f2d3SJuan Quintela     .version_id = EEPROM_VERSION,
128c4a0f2d3SJuan Quintela     .minimum_version_id = OLD_EEPROM_VERSION,
129c4a0f2d3SJuan Quintela     .fields = (VMStateField[]) {
130c4a0f2d3SJuan Quintela         VMSTATE_UINT8(tick, eeprom_t),
131c4a0f2d3SJuan Quintela         VMSTATE_UINT8(address, eeprom_t),
132c4a0f2d3SJuan Quintela         VMSTATE_UINT8(command, eeprom_t),
133ebabb67aSStefan Weil         VMSTATE_UINT8(writable, eeprom_t),
134c4a0f2d3SJuan Quintela 
135c4a0f2d3SJuan Quintela         VMSTATE_UINT8(eecs, eeprom_t),
136c4a0f2d3SJuan Quintela         VMSTATE_UINT8(eesk, eeprom_t),
137c4a0f2d3SJuan Quintela         VMSTATE_UINT8(eedo, eeprom_t),
138c4a0f2d3SJuan Quintela 
139c4a0f2d3SJuan Quintela         VMSTATE_UINT8(addrbits, eeprom_t),
140c4a0f2d3SJuan Quintela         VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
141c4a0f2d3SJuan Quintela         VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
142c4a0f2d3SJuan Quintela         VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION),
143c4a0f2d3SJuan Quintela         VMSTATE_UINT16(data, eeprom_t),
144c4a0f2d3SJuan Quintela         VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
145c4a0f2d3SJuan Quintela                                      vmstate_info_uint16, uint16_t),
146c4a0f2d3SJuan Quintela         VMSTATE_END_OF_LIST()
147663e8e51Sths     }
148c4a0f2d3SJuan Quintela };
149663e8e51Sths 
150c227f099SAnthony Liguori void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
151663e8e51Sths {
152663e8e51Sths     uint8_t tick = eeprom->tick;
153663e8e51Sths     uint8_t eedo = eeprom->eedo;
154663e8e51Sths     uint16_t address = eeprom->address;
155663e8e51Sths     uint8_t command = eeprom->command;
156663e8e51Sths 
157663e8e51Sths     logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n",
158663e8e51Sths            eecs, eesk, eedi, eedo, tick);
159663e8e51Sths 
160663e8e51Sths     if (!eeprom->eecs && eecs) {
161663e8e51Sths         /* Start chip select cycle. */
162663e8e51Sths         logout("Cycle start, waiting for 1st start bit (0)\n");
163663e8e51Sths         tick = 0;
164663e8e51Sths         command = 0x0;
165663e8e51Sths         address = 0x0;
166663e8e51Sths     } else if (eeprom->eecs && !eecs) {
167663e8e51Sths         /* End chip select cycle. This triggers write / erase. */
168ebabb67aSStefan Weil         if (eeprom->writable) {
169663e8e51Sths             uint8_t subcommand = address >> (eeprom->addrbits - 2);
170663e8e51Sths             if (command == 0 && subcommand == 2) {
171663e8e51Sths                 /* Erase all. */
172663e8e51Sths                 for (address = 0; address < eeprom->size; address++) {
173663e8e51Sths                     eeprom->contents[address] = 0xffff;
174663e8e51Sths                 }
175663e8e51Sths             } else if (command == 3) {
176663e8e51Sths                 /* Erase word. */
177663e8e51Sths                 eeprom->contents[address] = 0xffff;
178663e8e51Sths             } else if (tick >= 2 + 2 + eeprom->addrbits + 16) {
179663e8e51Sths                 if (command == 1) {
180663e8e51Sths                     /* Write word. */
181663e8e51Sths                     eeprom->contents[address] &= eeprom->data;
182663e8e51Sths                 } else if (command == 0 && subcommand == 1) {
183663e8e51Sths                     /* Write all. */
184663e8e51Sths                     for (address = 0; address < eeprom->size; address++) {
185663e8e51Sths                         eeprom->contents[address] &= eeprom->data;
186663e8e51Sths                     }
187663e8e51Sths                 }
188663e8e51Sths             }
189663e8e51Sths         }
190663e8e51Sths         /* Output DO is tristate, read results in 1. */
191663e8e51Sths         eedo = 1;
192663e8e51Sths     } else if (eecs && !eeprom->eesk && eesk) {
193663e8e51Sths         /* Raising edge of clock shifts data in. */
194663e8e51Sths         if (tick == 0) {
195663e8e51Sths             /* Wait for 1st start bit. */
196663e8e51Sths             if (eedi == 0) {
197663e8e51Sths                 logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n");
198663e8e51Sths                 tick++;
199663e8e51Sths             } else {
200663e8e51Sths                 logout("wrong 1st start bit (is 1, should be 0)\n");
201663e8e51Sths                 tick = 2;
202663e8e51Sths                 //~ assert(!"wrong start bit");
203663e8e51Sths             }
204663e8e51Sths         } else if (tick == 1) {
205663e8e51Sths             /* Wait for 2nd start bit. */
206663e8e51Sths             if (eedi != 0) {
207663e8e51Sths                 logout("Got correct 2nd start bit, getting command + address\n");
208663e8e51Sths                 tick++;
209663e8e51Sths             } else {
210663e8e51Sths                 logout("1st start bit is longer than needed\n");
211663e8e51Sths             }
212663e8e51Sths         } else if (tick < 2 + 2) {
213663e8e51Sths             /* Got 2 start bits, transfer 2 opcode bits. */
214663e8e51Sths             tick++;
215663e8e51Sths             command <<= 1;
216663e8e51Sths             if (eedi) {
217663e8e51Sths                 command += 1;
218663e8e51Sths             }
219663e8e51Sths         } else if (tick < 2 + 2 + eeprom->addrbits) {
220663e8e51Sths             /* Got 2 start bits and 2 opcode bits, transfer all address bits. */
221663e8e51Sths             tick++;
222663e8e51Sths             address = ((address << 1) | eedi);
223663e8e51Sths             if (tick == 2 + 2 + eeprom->addrbits) {
224663e8e51Sths                 logout("%s command, address = 0x%02x (value 0x%04x)\n",
225663e8e51Sths                        opstring[command], address, eeprom->contents[address]);
226663e8e51Sths                 if (command == 2) {
227663e8e51Sths                     eedo = 0;
228663e8e51Sths                 }
229663e8e51Sths                 address = address % eeprom->size;
230663e8e51Sths                 if (command == 0) {
231663e8e51Sths                     /* Command code in upper 2 bits of address. */
232663e8e51Sths                     switch (address >> (eeprom->addrbits - 2)) {
233663e8e51Sths                     case 0:
234663e8e51Sths                         logout("write disable command\n");
235ebabb67aSStefan Weil                         eeprom->writable = 0;
236663e8e51Sths                         break;
237663e8e51Sths                     case 1:
238663e8e51Sths                         logout("write all command\n");
239663e8e51Sths                         break;
240663e8e51Sths                     case 2:
241663e8e51Sths                         logout("erase all command\n");
242663e8e51Sths                         break;
243663e8e51Sths                     case 3:
244663e8e51Sths                         logout("write enable command\n");
245ebabb67aSStefan Weil                         eeprom->writable = 1;
246663e8e51Sths                         break;
247663e8e51Sths                     }
248663e8e51Sths                 } else {
249663e8e51Sths                     /* Read, write or erase word. */
250663e8e51Sths                     eeprom->data = eeprom->contents[address];
251663e8e51Sths                 }
252663e8e51Sths             }
253663e8e51Sths         } else if (tick < 2 + 2 + eeprom->addrbits + 16) {
254663e8e51Sths             /* Transfer 16 data bits. */
255663e8e51Sths             tick++;
256663e8e51Sths             if (command == 2) {
257663e8e51Sths                 /* Read word. */
258663e8e51Sths                 eedo = ((eeprom->data & 0x8000) != 0);
259663e8e51Sths             }
260663e8e51Sths             eeprom->data <<= 1;
261663e8e51Sths             eeprom->data += eedi;
262663e8e51Sths         } else {
263663e8e51Sths             logout("additional unneeded tick, not processed\n");
264663e8e51Sths         }
265663e8e51Sths     }
266663e8e51Sths     /* Save status of EEPROM. */
267663e8e51Sths     eeprom->tick = tick;
268663e8e51Sths     eeprom->eecs = eecs;
269663e8e51Sths     eeprom->eesk = eesk;
270663e8e51Sths     eeprom->eedo = eedo;
271663e8e51Sths     eeprom->address = address;
272663e8e51Sths     eeprom->command = command;
273663e8e51Sths }
274663e8e51Sths 
275c227f099SAnthony Liguori uint16_t eeprom93xx_read(eeprom_t *eeprom)
276663e8e51Sths {
277663e8e51Sths     /* Return status of pin DO (0 or 1). */
278663e8e51Sths     logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo);
2796fedcaa1SAntony Pavlov     return eeprom->eedo;
280663e8e51Sths }
281663e8e51Sths 
282663e8e51Sths #if 0
283663e8e51Sths void eeprom93xx_reset(eeprom_t *eeprom)
284663e8e51Sths {
285663e8e51Sths     /* prepare eeprom */
286663e8e51Sths     logout("eeprom = 0x%p\n", eeprom);
287663e8e51Sths     eeprom->tick = 0;
288663e8e51Sths     eeprom->command = 0;
289663e8e51Sths }
290663e8e51Sths #endif
291663e8e51Sths 
2925fce2b3eSAlex Williamson eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords)
293663e8e51Sths {
294663e8e51Sths     /* Add a new EEPROM (with 16, 64 or 256 words). */
295c227f099SAnthony Liguori     eeprom_t *eeprom;
296663e8e51Sths     uint8_t addrbits;
297663e8e51Sths 
298663e8e51Sths     switch (nwords) {
299663e8e51Sths     case 16:
300663e8e51Sths     case 64:
301663e8e51Sths         addrbits = 6;
302663e8e51Sths         break;
303663e8e51Sths     case 128:
304663e8e51Sths     case 256:
305663e8e51Sths         addrbits = 8;
306663e8e51Sths         break;
307663e8e51Sths     default:
308663e8e51Sths         assert(!"Unsupported EEPROM size, fallback to 64 words!");
309663e8e51Sths         nwords = 64;
310663e8e51Sths         addrbits = 6;
311663e8e51Sths     }
312663e8e51Sths 
3137267c094SAnthony Liguori     eeprom = (eeprom_t *)g_malloc0(sizeof(*eeprom) + nwords * 2);
314663e8e51Sths     eeprom->size = nwords;
315663e8e51Sths     eeprom->addrbits = addrbits;
316663e8e51Sths     /* Output DO is tristate, read results in 1. */
317663e8e51Sths     eeprom->eedo = 1;
318663e8e51Sths     logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
3195fce2b3eSAlex Williamson     vmstate_register(dev, 0, &vmstate_eeprom, eeprom);
320663e8e51Sths     return eeprom;
321663e8e51Sths }
322663e8e51Sths 
3235fce2b3eSAlex Williamson void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom)
324663e8e51Sths {
325663e8e51Sths     /* Destroy EEPROM. */
326663e8e51Sths     logout("eeprom = 0x%p\n", eeprom);
3275fce2b3eSAlex Williamson     vmstate_unregister(dev, &vmstate_eeprom, eeprom);
3287267c094SAnthony Liguori     g_free(eeprom);
329663e8e51Sths }
330663e8e51Sths 
331c227f099SAnthony Liguori uint16_t *eeprom93xx_data(eeprom_t *eeprom)
332663e8e51Sths {
333663e8e51Sths     /* Get EEPROM data array. */
334663e8e51Sths     return &eeprom->contents[0];
335663e8e51Sths }
336663e8e51Sths 
337663e8e51Sths /* eof */
338