1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include "qemu/osdep.h"
39#include "hw/hw.h"
40#include "hw/nvram/eeprom93xx.h"
41
42
43
44
45#ifdef DEBUG_EEPROM
46#define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
47#else
48#define logout(fmt, ...) ((void)0)
49#endif
50
51#define EEPROM_INSTANCE 0
52#define OLD_EEPROM_VERSION 20061112
53#define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
54
55#if 0
56typedef enum {
57 eeprom_read = 0x80,
58 eeprom_write = 0x40,
59 eeprom_erase = 0xc0,
60 eeprom_ewen = 0x30,
61 eeprom_ewds = 0x00,
62 eeprom_eral = 0x20,
63 eeprom_wral = 0x10,
64 eeprom_amask = 0x0f,
65 eeprom_imask = 0xf0
66} eeprom_instruction_t;
67#endif
68
69#ifdef DEBUG_EEPROM
70static const char *opstring[] = {
71 "extended", "write", "read", "erase"
72};
73#endif
74
75struct _eeprom_t {
76 uint8_t tick;
77 uint8_t address;
78 uint8_t command;
79 uint8_t writable;
80
81 uint8_t eecs;
82 uint8_t eesk;
83 uint8_t eedo;
84
85 uint8_t addrbits;
86 uint16_t size;
87 uint16_t data;
88 uint16_t contents[0];
89};
90
91
92
93
94
95
96
97static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size,
98 VMStateField *field)
99{
100 uint16_t *v = pv;
101 *v = qemu_get_ubyte(f);
102 return 0;
103}
104
105static int put_unused(QEMUFile *f, void *pv, size_t size, VMStateField *field,
106 QJSON *vmdesc)
107{
108 fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n");
109 fprintf(stderr, "Never should be used to write a new state.\n");
110 exit(0);
111
112 return 0;
113}
114
115static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
116 .name = "uint16_from_uint8",
117 .get = get_uint16_from_uint8,
118 .put = put_unused,
119};
120
121#define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \
122 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
123
124static bool is_old_eeprom_version(void *opaque, int version_id)
125{
126 return version_id == OLD_EEPROM_VERSION;
127}
128
129static const VMStateDescription vmstate_eeprom = {
130 .name = "eeprom",
131 .version_id = EEPROM_VERSION,
132 .minimum_version_id = OLD_EEPROM_VERSION,
133 .fields = (VMStateField[]) {
134 VMSTATE_UINT8(tick, eeprom_t),
135 VMSTATE_UINT8(address, eeprom_t),
136 VMSTATE_UINT8(command, eeprom_t),
137 VMSTATE_UINT8(writable, eeprom_t),
138
139 VMSTATE_UINT8(eecs, eeprom_t),
140 VMSTATE_UINT8(eesk, eeprom_t),
141 VMSTATE_UINT8(eedo, eeprom_t),
142
143 VMSTATE_UINT8(addrbits, eeprom_t),
144 VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
145 VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
146 VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION, NULL),
147 VMSTATE_UINT16(data, eeprom_t),
148 VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
149 vmstate_info_uint16, uint16_t),
150 VMSTATE_END_OF_LIST()
151 }
152};
153
154void 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
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
172 if (eeprom->writable) {
173 uint8_t subcommand = address >> (eeprom->addrbits - 2);
174 if (command == 0 && subcommand == 2) {
175
176 for (address = 0; address < eeprom->size; address++) {
177 eeprom->contents[address] = 0xffff;
178 }
179 } else if (command == 3) {
180
181 eeprom->contents[address] = 0xffff;
182 } else if (tick >= 2 + 2 + eeprom->addrbits + 16) {
183 if (command == 1) {
184
185 eeprom->contents[address] &= eeprom->data;
186 } else if (command == 0 && subcommand == 1) {
187
188 for (address = 0; address < eeprom->size; address++) {
189 eeprom->contents[address] &= eeprom->data;
190 }
191 }
192 }
193 }
194
195 eedo = 1;
196 } else if (eecs && !eeprom->eesk && eesk) {
197
198 if (tick == 0) {
199
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
207 }
208 } else if (tick == 1) {
209
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
218 tick++;
219 command <<= 1;
220 if (eedi) {
221 command += 1;
222 }
223 } else if (tick < 2 + 2 + eeprom->addrbits) {
224
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
236 switch (address >> (eeprom->addrbits - 2)) {
237 case 0:
238 logout("write disable command\n");
239 eeprom->writable = 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->writable = 1;
250 break;
251 }
252 } else {
253
254 eeprom->data = eeprom->contents[address];
255 }
256 }
257 } else if (tick < 2 + 2 + eeprom->addrbits + 16) {
258
259 tick++;
260 if (command == 2) {
261
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
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
279uint16_t eeprom93xx_read(eeprom_t *eeprom)
280{
281
282 logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo);
283 return eeprom->eedo;
284}
285
286#if 0
287void eeprom93xx_reset(eeprom_t *eeprom)
288{
289
290 logout("eeprom = 0x%p\n", eeprom);
291 eeprom->tick = 0;
292 eeprom->command = 0;
293}
294#endif
295
296eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords)
297{
298
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 *)g_malloc0(sizeof(*eeprom) + nwords * 2);
318 eeprom->size = nwords;
319 eeprom->addrbits = addrbits;
320
321 eeprom->eedo = 1;
322 logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
323 vmstate_register(dev, 0, &vmstate_eeprom, eeprom);
324 return eeprom;
325}
326
327void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom)
328{
329
330 logout("eeprom = 0x%p\n", eeprom);
331 vmstate_unregister(dev, &vmstate_eeprom, eeprom);
332 g_free(eeprom);
333}
334
335uint16_t *eeprom93xx_data(eeprom_t *eeprom)
336{
337
338 return &eeprom->contents[0];
339}
340
341
342