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 "hw/hw.h"
39#include "hw/nvram/eeprom93xx.h"
40
41
42
43
44#ifdef DEBUG_EEPROM
45#define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
46#else
47#define logout(fmt, ...) ((void)0)
48#endif
49
50#define EEPROM_INSTANCE 0
51#define OLD_EEPROM_VERSION 20061112
52#define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
53
54#if 0
55typedef enum {
56 eeprom_read = 0x80,
57 eeprom_write = 0x40,
58 eeprom_erase = 0xc0,
59 eeprom_ewen = 0x30,
60 eeprom_ewds = 0x00,
61 eeprom_eral = 0x20,
62 eeprom_wral = 0x10,
63 eeprom_amask = 0x0f,
64 eeprom_imask = 0xf0
65} eeprom_instruction_t;
66#endif
67
68#ifdef DEBUG_EEPROM
69static const char *opstring[] = {
70 "extended", "write", "read", "erase"
71};
72#endif
73
74struct _eeprom_t {
75 uint8_t tick;
76 uint8_t address;
77 uint8_t command;
78 uint8_t writable;
79
80 uint8_t eecs;
81 uint8_t eesk;
82 uint8_t eedo;
83
84 uint8_t addrbits;
85 uint16_t size;
86 uint16_t data;
87 uint16_t contents[0];
88};
89
90
91
92
93
94
95
96static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size)
97{
98 uint16_t *v = pv;
99 *v = qemu_get_ubyte(f);
100 return 0;
101}
102
103static void put_unused(QEMUFile *f, void *pv, size_t size)
104{
105 fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n");
106 fprintf(stderr, "Never should be used to write a new state.\n");
107 exit(0);
108}
109
110static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
111 .name = "uint16_from_uint8",
112 .get = get_uint16_from_uint8,
113 .put = put_unused,
114};
115
116#define VMSTATE_UINT16_HACK_TEST(_f, _s, _t) \
117 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
118
119static bool is_old_eeprom_version(void *opaque, int version_id)
120{
121 return version_id == OLD_EEPROM_VERSION;
122}
123
124static const VMStateDescription vmstate_eeprom = {
125 .name = "eeprom",
126 .version_id = EEPROM_VERSION,
127 .minimum_version_id = OLD_EEPROM_VERSION,
128 .fields = (VMStateField[]) {
129 VMSTATE_UINT8(tick, eeprom_t),
130 VMSTATE_UINT8(address, eeprom_t),
131 VMSTATE_UINT8(command, eeprom_t),
132 VMSTATE_UINT8(writable, eeprom_t),
133
134 VMSTATE_UINT8(eecs, eeprom_t),
135 VMSTATE_UINT8(eesk, eeprom_t),
136 VMSTATE_UINT8(eedo, eeprom_t),
137
138 VMSTATE_UINT8(addrbits, eeprom_t),
139 VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
140 VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
141 VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION),
142 VMSTATE_UINT16(data, eeprom_t),
143 VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
144 vmstate_info_uint16, uint16_t),
145 VMSTATE_END_OF_LIST()
146 }
147};
148
149void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
150{
151 uint8_t tick = eeprom->tick;
152 uint8_t eedo = eeprom->eedo;
153 uint16_t address = eeprom->address;
154 uint8_t command = eeprom->command;
155
156 logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n",
157 eecs, eesk, eedi, eedo, tick);
158
159 if (!eeprom->eecs && eecs) {
160
161 logout("Cycle start, waiting for 1st start bit (0)\n");
162 tick = 0;
163 command = 0x0;
164 address = 0x0;
165 } else if (eeprom->eecs && !eecs) {
166
167 if (eeprom->writable) {
168 uint8_t subcommand = address >> (eeprom->addrbits - 2);
169 if (command == 0 && subcommand == 2) {
170
171 for (address = 0; address < eeprom->size; address++) {
172 eeprom->contents[address] = 0xffff;
173 }
174 } else if (command == 3) {
175
176 eeprom->contents[address] = 0xffff;
177 } else if (tick >= 2 + 2 + eeprom->addrbits + 16) {
178 if (command == 1) {
179
180 eeprom->contents[address] &= eeprom->data;
181 } else if (command == 0 && subcommand == 1) {
182
183 for (address = 0; address < eeprom->size; address++) {
184 eeprom->contents[address] &= eeprom->data;
185 }
186 }
187 }
188 }
189
190 eedo = 1;
191 } else if (eecs && !eeprom->eesk && eesk) {
192
193 if (tick == 0) {
194
195 if (eedi == 0) {
196 logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n");
197 tick++;
198 } else {
199 logout("wrong 1st start bit (is 1, should be 0)\n");
200 tick = 2;
201
202 }
203 } else if (tick == 1) {
204
205 if (eedi != 0) {
206 logout("Got correct 2nd start bit, getting command + address\n");
207 tick++;
208 } else {
209 logout("1st start bit is longer than needed\n");
210 }
211 } else if (tick < 2 + 2) {
212
213 tick++;
214 command <<= 1;
215 if (eedi) {
216 command += 1;
217 }
218 } else if (tick < 2 + 2 + eeprom->addrbits) {
219
220 tick++;
221 address = ((address << 1) | eedi);
222 if (tick == 2 + 2 + eeprom->addrbits) {
223 logout("%s command, address = 0x%02x (value 0x%04x)\n",
224 opstring[command], address, eeprom->contents[address]);
225 if (command == 2) {
226 eedo = 0;
227 }
228 address = address % eeprom->size;
229 if (command == 0) {
230
231 switch (address >> (eeprom->addrbits - 2)) {
232 case 0:
233 logout("write disable command\n");
234 eeprom->writable = 0;
235 break;
236 case 1:
237 logout("write all command\n");
238 break;
239 case 2:
240 logout("erase all command\n");
241 break;
242 case 3:
243 logout("write enable command\n");
244 eeprom->writable = 1;
245 break;
246 }
247 } else {
248
249 eeprom->data = eeprom->contents[address];
250 }
251 }
252 } else if (tick < 2 + 2 + eeprom->addrbits + 16) {
253
254 tick++;
255 if (command == 2) {
256
257 eedo = ((eeprom->data & 0x8000) != 0);
258 }
259 eeprom->data <<= 1;
260 eeprom->data += eedi;
261 } else {
262 logout("additional unneeded tick, not processed\n");
263 }
264 }
265
266 eeprom->tick = tick;
267 eeprom->eecs = eecs;
268 eeprom->eesk = eesk;
269 eeprom->eedo = eedo;
270 eeprom->address = address;
271 eeprom->command = command;
272}
273
274uint16_t eeprom93xx_read(eeprom_t *eeprom)
275{
276
277 logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo);
278 return eeprom->eedo;
279}
280
281#if 0
282void eeprom93xx_reset(eeprom_t *eeprom)
283{
284
285 logout("eeprom = 0x%p\n", eeprom);
286 eeprom->tick = 0;
287 eeprom->command = 0;
288}
289#endif
290
291eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords)
292{
293
294 eeprom_t *eeprom;
295 uint8_t addrbits;
296
297 switch (nwords) {
298 case 16:
299 case 64:
300 addrbits = 6;
301 break;
302 case 128:
303 case 256:
304 addrbits = 8;
305 break;
306 default:
307 assert(!"Unsupported EEPROM size, fallback to 64 words!");
308 nwords = 64;
309 addrbits = 6;
310 }
311
312 eeprom = (eeprom_t *)g_malloc0(sizeof(*eeprom) + nwords * 2);
313 eeprom->size = nwords;
314 eeprom->addrbits = addrbits;
315
316 eeprom->eedo = 1;
317 logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
318 vmstate_register(dev, 0, &vmstate_eeprom, eeprom);
319 return eeprom;
320}
321
322void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom)
323{
324
325 logout("eeprom = 0x%p\n", eeprom);
326 vmstate_unregister(dev, &vmstate_eeprom, eeprom);
327 g_free(eeprom);
328}
329
330uint16_t *eeprom93xx_data(eeprom_t *eeprom)
331{
332
333 return &eeprom->contents[0];
334}
335
336
337