Statistics
| Branch: | Revision:

root / hw / eeprom93xx.c @ c1ded3dc

History | View | Annotate | Download (10.5 kB)

1 663e8e51 ths
/*
2 663e8e51 ths
 * QEMU EEPROM 93xx emulation
3 663e8e51 ths
 *
4 663e8e51 ths
 * Copyright (c) 2006-2007 Stefan Weil
5 663e8e51 ths
 *
6 663e8e51 ths
 * This program is free software; you can redistribute it and/or modify
7 663e8e51 ths
 * it under the terms of the GNU General Public License as published by
8 663e8e51 ths
 * the Free Software Foundation; either version 2 of the License, or
9 663e8e51 ths
 * (at your option) any later version.
10 663e8e51 ths
 *
11 663e8e51 ths
 * This program is distributed in the hope that it will be useful,
12 663e8e51 ths
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 663e8e51 ths
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 663e8e51 ths
 * GNU General Public License for more details.
15 663e8e51 ths
 *
16 663e8e51 ths
 * You should have received a copy of the GNU General Public License
17 8167ee88 Blue Swirl
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 663e8e51 ths
 */
19 663e8e51 ths
20 663e8e51 ths
/* Emulation for serial EEPROMs:
21 663e8e51 ths
 * NMC93C06 256-Bit (16 x 16)
22 663e8e51 ths
 * NMC93C46 1024-Bit (64 x 16)
23 663e8e51 ths
 * NMC93C56 2028 Bit (128 x 16)
24 663e8e51 ths
 * NMC93C66 4096 Bit (256 x 16)
25 663e8e51 ths
 * Compatible devices include FM93C46 and others.
26 663e8e51 ths
 *
27 663e8e51 ths
 * Other drivers use these interface functions:
28 663e8e51 ths
 * eeprom93xx_new   - add a new EEPROM (with 16, 64 or 256 words)
29 663e8e51 ths
 * eeprom93xx_free  - destroy EEPROM
30 663e8e51 ths
 * eeprom93xx_read  - read data from the EEPROM
31 663e8e51 ths
 * eeprom93xx_write - write data to the EEPROM
32 663e8e51 ths
 * eeprom93xx_data  - get EEPROM data array for external manipulation
33 663e8e51 ths
 *
34 663e8e51 ths
 * Todo list:
35 663e8e51 ths
 * - No emulation of EEPROM timings.
36 663e8e51 ths
 */
37 663e8e51 ths
38 87ecb68b pbrook
#include "hw.h"
39 663e8e51 ths
#include "eeprom93xx.h"
40 663e8e51 ths
41 663e8e51 ths
/* Debug EEPROM emulation. */
42 663e8e51 ths
//~ #define DEBUG_EEPROM
43 663e8e51 ths
44 663e8e51 ths
#ifdef DEBUG_EEPROM
45 001faf32 Blue Swirl
#define logout(fmt, ...) fprintf(stderr, "EEPROM\t%-24s" fmt, __func__, ## __VA_ARGS__)
46 663e8e51 ths
#else
47 001faf32 Blue Swirl
#define logout(fmt, ...) ((void)0)
48 663e8e51 ths
#endif
49 663e8e51 ths
50 7ab2589c aurel32
#define EEPROM_INSTANCE  0
51 7ab2589c aurel32
#define OLD_EEPROM_VERSION 20061112
52 7ab2589c aurel32
#define EEPROM_VERSION (OLD_EEPROM_VERSION + 1)
53 663e8e51 ths
54 663e8e51 ths
#if 0
55 663e8e51 ths
typedef enum {
56 663e8e51 ths
  eeprom_read  = 0x80,   /* read register xx */
57 663e8e51 ths
  eeprom_write = 0x40,   /* write register xx */
58 663e8e51 ths
  eeprom_erase = 0xc0,   /* erase register xx */
59 663e8e51 ths
  eeprom_ewen  = 0x30,   /* erase / write enable */
60 663e8e51 ths
  eeprom_ewds  = 0x00,   /* erase / write disable */
61 663e8e51 ths
  eeprom_eral  = 0x20,   /* erase all registers */
62 663e8e51 ths
  eeprom_wral  = 0x10,   /* write all registers */
63 663e8e51 ths
  eeprom_amask = 0x0f,
64 663e8e51 ths
  eeprom_imask = 0xf0
65 663e8e51 ths
} eeprom_instruction_t;
66 663e8e51 ths
#endif
67 663e8e51 ths
68 663e8e51 ths
#ifdef DEBUG_EEPROM
69 663e8e51 ths
static const char *opstring[] = {
70 663e8e51 ths
  "extended", "write", "read", "erase"
71 663e8e51 ths
};
72 663e8e51 ths
#endif
73 663e8e51 ths
74 c227f099 Anthony Liguori
struct _eeprom_t {
75 663e8e51 ths
    uint8_t  tick;
76 663e8e51 ths
    uint8_t  address;
77 663e8e51 ths
    uint8_t  command;
78 663e8e51 ths
    uint8_t  writeable;
79 663e8e51 ths
80 663e8e51 ths
    uint8_t eecs;
81 663e8e51 ths
    uint8_t eesk;
82 663e8e51 ths
    uint8_t eedo;
83 663e8e51 ths
84 663e8e51 ths
    uint8_t  addrbits;
85 7ab2589c aurel32
    uint16_t size;
86 663e8e51 ths
    uint16_t data;
87 663e8e51 ths
    uint16_t contents[0];
88 663e8e51 ths
};
89 663e8e51 ths
90 663e8e51 ths
/* Code for saving and restoring of EEPROM state. */
91 663e8e51 ths
92 c4a0f2d3 Juan Quintela
/* Restore an uint16_t from an uint8_t
93 c4a0f2d3 Juan Quintela
   This is a Big hack, but it is how the old state did it.
94 c4a0f2d3 Juan Quintela
 */
95 c4a0f2d3 Juan Quintela
96 c4a0f2d3 Juan Quintela
static int get_uint16_from_uint8(QEMUFile *f, void *pv, size_t size)
97 663e8e51 ths
{
98 c4a0f2d3 Juan Quintela
    uint16_t *v = pv;
99 c4a0f2d3 Juan Quintela
    *v = qemu_get_ubyte(f);
100 c4a0f2d3 Juan Quintela
    return 0;
101 c4a0f2d3 Juan Quintela
}
102 d4ae799c aurel32
103 c4a0f2d3 Juan Quintela
static void put_unused(QEMUFile *f, void *pv, size_t size)
104 c4a0f2d3 Juan Quintela
{
105 c4a0f2d3 Juan Quintela
    fprintf(stderr, "uint16_from_uint8 is used only for backwards compatibility.\n");
106 c4a0f2d3 Juan Quintela
    fprintf(stderr, "Never should be used to write a new state.\n");
107 c4a0f2d3 Juan Quintela
    exit(0);
108 c4a0f2d3 Juan Quintela
}
109 d4ae799c aurel32
110 d05ac8fa Blue Swirl
static const VMStateInfo vmstate_hack_uint16_from_uint8 = {
111 c4a0f2d3 Juan Quintela
    .name = "uint16_from_uint8",
112 c4a0f2d3 Juan Quintela
    .get  = get_uint16_from_uint8,
113 c4a0f2d3 Juan Quintela
    .put  = put_unused,
114 c4a0f2d3 Juan Quintela
};
115 d4ae799c aurel32
116 c4a0f2d3 Juan Quintela
#define VMSTATE_UINT16_HACK_TEST(_f, _s, _t)                           \
117 c4a0f2d3 Juan Quintela
    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint16_from_uint8, uint16_t)
118 663e8e51 ths
119 c4a0f2d3 Juan Quintela
static bool is_old_eeprom_version(void *opaque, int version_id)
120 663e8e51 ths
{
121 c4a0f2d3 Juan Quintela
    return version_id == OLD_EEPROM_VERSION;
122 c4a0f2d3 Juan Quintela
}
123 d4ae799c aurel32
124 c4a0f2d3 Juan Quintela
static const VMStateDescription vmstate_eeprom = {
125 c4a0f2d3 Juan Quintela
    .name = "eeprom",
126 c4a0f2d3 Juan Quintela
    .version_id = EEPROM_VERSION,
127 c4a0f2d3 Juan Quintela
    .minimum_version_id = OLD_EEPROM_VERSION,
128 c4a0f2d3 Juan Quintela
    .minimum_version_id_old = OLD_EEPROM_VERSION,
129 c4a0f2d3 Juan Quintela
    .fields      = (VMStateField []) {
130 c4a0f2d3 Juan Quintela
        VMSTATE_UINT8(tick, eeprom_t),
131 c4a0f2d3 Juan Quintela
        VMSTATE_UINT8(address, eeprom_t),
132 c4a0f2d3 Juan Quintela
        VMSTATE_UINT8(command, eeprom_t),
133 c4a0f2d3 Juan Quintela
        VMSTATE_UINT8(writeable, eeprom_t),
134 d4ae799c aurel32
135 c4a0f2d3 Juan Quintela
        VMSTATE_UINT8(eecs, eeprom_t),
136 c4a0f2d3 Juan Quintela
        VMSTATE_UINT8(eesk, eeprom_t),
137 c4a0f2d3 Juan Quintela
        VMSTATE_UINT8(eedo, eeprom_t),
138 d4ae799c aurel32
139 c4a0f2d3 Juan Quintela
        VMSTATE_UINT8(addrbits, eeprom_t),
140 c4a0f2d3 Juan Quintela
        VMSTATE_UINT16_HACK_TEST(size, eeprom_t, is_old_eeprom_version),
141 c4a0f2d3 Juan Quintela
        VMSTATE_UNUSED_TEST(is_old_eeprom_version, 1),
142 c4a0f2d3 Juan Quintela
        VMSTATE_UINT16_EQUAL_V(size, eeprom_t, EEPROM_VERSION),
143 c4a0f2d3 Juan Quintela
        VMSTATE_UINT16(data, eeprom_t),
144 c4a0f2d3 Juan Quintela
        VMSTATE_VARRAY_UINT16_UNSAFE(contents, eeprom_t, size, 0,
145 c4a0f2d3 Juan Quintela
                                     vmstate_info_uint16, uint16_t),
146 c4a0f2d3 Juan Quintela
        VMSTATE_END_OF_LIST()
147 663e8e51 ths
    }
148 c4a0f2d3 Juan Quintela
};
149 663e8e51 ths
150 c227f099 Anthony Liguori
void eeprom93xx_write(eeprom_t *eeprom, int eecs, int eesk, int eedi)
151 663e8e51 ths
{
152 663e8e51 ths
    uint8_t tick = eeprom->tick;
153 663e8e51 ths
    uint8_t eedo = eeprom->eedo;
154 663e8e51 ths
    uint16_t address = eeprom->address;
155 663e8e51 ths
    uint8_t command = eeprom->command;
156 663e8e51 ths
157 663e8e51 ths
    logout("CS=%u SK=%u DI=%u DO=%u, tick = %u\n",
158 663e8e51 ths
           eecs, eesk, eedi, eedo, tick);
159 663e8e51 ths
160 663e8e51 ths
    if (! eeprom->eecs && eecs) {
161 663e8e51 ths
        /* Start chip select cycle. */
162 663e8e51 ths
        logout("Cycle start, waiting for 1st start bit (0)\n");
163 663e8e51 ths
        tick = 0;
164 663e8e51 ths
        command = 0x0;
165 663e8e51 ths
        address = 0x0;
166 663e8e51 ths
    } else if (eeprom->eecs && ! eecs) {
167 663e8e51 ths
        /* End chip select cycle. This triggers write / erase. */
168 663e8e51 ths
        if (eeprom->writeable) {
169 663e8e51 ths
            uint8_t subcommand = address >> (eeprom->addrbits - 2);
170 663e8e51 ths
            if (command == 0 && subcommand == 2) {
171 663e8e51 ths
                /* Erase all. */
172 663e8e51 ths
                for (address = 0; address < eeprom->size; address++) {
173 663e8e51 ths
                    eeprom->contents[address] = 0xffff;
174 663e8e51 ths
                }
175 663e8e51 ths
            } else if (command == 3) {
176 663e8e51 ths
                /* Erase word. */
177 663e8e51 ths
                eeprom->contents[address] = 0xffff;
178 663e8e51 ths
            } else if (tick >= 2 + 2 + eeprom->addrbits + 16) {
179 663e8e51 ths
                if (command == 1) {
180 663e8e51 ths
                    /* Write word. */
181 663e8e51 ths
                    eeprom->contents[address] &= eeprom->data;
182 663e8e51 ths
                } else if (command == 0 && subcommand == 1) {
183 663e8e51 ths
                    /* Write all. */
184 663e8e51 ths
                    for (address = 0; address < eeprom->size; address++) {
185 663e8e51 ths
                        eeprom->contents[address] &= eeprom->data;
186 663e8e51 ths
                    }
187 663e8e51 ths
                }
188 663e8e51 ths
            }
189 663e8e51 ths
        }
190 663e8e51 ths
        /* Output DO is tristate, read results in 1. */
191 663e8e51 ths
        eedo = 1;
192 663e8e51 ths
    } else if (eecs && ! eeprom->eesk && eesk) {
193 663e8e51 ths
        /* Raising edge of clock shifts data in. */
194 663e8e51 ths
        if (tick == 0) {
195 663e8e51 ths
            /* Wait for 1st start bit. */
196 663e8e51 ths
            if (eedi == 0) {
197 663e8e51 ths
                logout("Got correct 1st start bit, waiting for 2nd start bit (1)\n");
198 663e8e51 ths
                tick++;
199 663e8e51 ths
            } else {
200 663e8e51 ths
                logout("wrong 1st start bit (is 1, should be 0)\n");
201 663e8e51 ths
                tick = 2;
202 663e8e51 ths
                //~ assert(!"wrong start bit");
203 663e8e51 ths
            }
204 663e8e51 ths
        } else if (tick == 1) {
205 663e8e51 ths
            /* Wait for 2nd start bit. */
206 663e8e51 ths
            if (eedi != 0) {
207 663e8e51 ths
                logout("Got correct 2nd start bit, getting command + address\n");
208 663e8e51 ths
                tick++;
209 663e8e51 ths
            } else {
210 663e8e51 ths
                logout("1st start bit is longer than needed\n");
211 663e8e51 ths
            }
212 663e8e51 ths
        } else if (tick < 2 + 2) {
213 663e8e51 ths
            /* Got 2 start bits, transfer 2 opcode bits. */
214 663e8e51 ths
            tick++;
215 663e8e51 ths
            command <<= 1;
216 663e8e51 ths
            if (eedi) {
217 663e8e51 ths
                command += 1;
218 663e8e51 ths
            }
219 663e8e51 ths
        } else if (tick < 2 + 2 + eeprom->addrbits) {
220 663e8e51 ths
            /* Got 2 start bits and 2 opcode bits, transfer all address bits. */
221 663e8e51 ths
            tick++;
222 663e8e51 ths
            address = ((address << 1) | eedi);
223 663e8e51 ths
            if (tick == 2 + 2 + eeprom->addrbits) {
224 663e8e51 ths
                logout("%s command, address = 0x%02x (value 0x%04x)\n",
225 663e8e51 ths
                       opstring[command], address, eeprom->contents[address]);
226 663e8e51 ths
                if (command == 2) {
227 663e8e51 ths
                    eedo = 0;
228 663e8e51 ths
                }
229 663e8e51 ths
                address = address % eeprom->size;
230 663e8e51 ths
                if (command == 0) {
231 663e8e51 ths
                    /* Command code in upper 2 bits of address. */
232 663e8e51 ths
                    switch (address >> (eeprom->addrbits - 2)) {
233 663e8e51 ths
                        case 0:
234 663e8e51 ths
                            logout("write disable command\n");
235 663e8e51 ths
                            eeprom->writeable = 0;
236 663e8e51 ths
                            break;
237 663e8e51 ths
                        case 1:
238 663e8e51 ths
                            logout("write all command\n");
239 663e8e51 ths
                            break;
240 663e8e51 ths
                        case 2:
241 663e8e51 ths
                            logout("erase all command\n");
242 663e8e51 ths
                            break;
243 663e8e51 ths
                        case 3:
244 663e8e51 ths
                            logout("write enable command\n");
245 663e8e51 ths
                            eeprom->writeable = 1;
246 663e8e51 ths
                            break;
247 663e8e51 ths
                    }
248 663e8e51 ths
                } else {
249 663e8e51 ths
                    /* Read, write or erase word. */
250 663e8e51 ths
                    eeprom->data = eeprom->contents[address];
251 663e8e51 ths
                }
252 663e8e51 ths
            }
253 663e8e51 ths
        } else if (tick < 2 + 2 + eeprom->addrbits + 16) {
254 663e8e51 ths
            /* Transfer 16 data bits. */
255 663e8e51 ths
            tick++;
256 663e8e51 ths
            if (command == 2) {
257 663e8e51 ths
                /* Read word. */
258 663e8e51 ths
                eedo = ((eeprom->data & 0x8000) != 0);
259 663e8e51 ths
            }
260 663e8e51 ths
            eeprom->data <<= 1;
261 663e8e51 ths
            eeprom->data += eedi;
262 663e8e51 ths
        } else {
263 663e8e51 ths
            logout("additional unneeded tick, not processed\n");
264 663e8e51 ths
        }
265 663e8e51 ths
    }
266 663e8e51 ths
    /* Save status of EEPROM. */
267 663e8e51 ths
    eeprom->tick = tick;
268 663e8e51 ths
    eeprom->eecs = eecs;
269 663e8e51 ths
    eeprom->eesk = eesk;
270 663e8e51 ths
    eeprom->eedo = eedo;
271 663e8e51 ths
    eeprom->address = address;
272 663e8e51 ths
    eeprom->command = command;
273 663e8e51 ths
}
274 663e8e51 ths
275 c227f099 Anthony Liguori
uint16_t eeprom93xx_read(eeprom_t *eeprom)
276 663e8e51 ths
{
277 663e8e51 ths
    /* Return status of pin DO (0 or 1). */
278 663e8e51 ths
    logout("CS=%u DO=%u\n", eeprom->eecs, eeprom->eedo);
279 663e8e51 ths
    return (eeprom->eedo);
280 663e8e51 ths
}
281 663e8e51 ths
282 663e8e51 ths
#if 0
283 663e8e51 ths
void eeprom93xx_reset(eeprom_t *eeprom)
284 663e8e51 ths
{
285 663e8e51 ths
    /* prepare eeprom */
286 663e8e51 ths
    logout("eeprom = 0x%p\n", eeprom);
287 663e8e51 ths
    eeprom->tick = 0;
288 663e8e51 ths
    eeprom->command = 0;
289 663e8e51 ths
}
290 663e8e51 ths
#endif
291 663e8e51 ths
292 5fce2b3e Alex Williamson
eeprom_t *eeprom93xx_new(DeviceState *dev, uint16_t nwords)
293 663e8e51 ths
{
294 663e8e51 ths
    /* Add a new EEPROM (with 16, 64 or 256 words). */
295 c227f099 Anthony Liguori
    eeprom_t *eeprom;
296 663e8e51 ths
    uint8_t addrbits;
297 663e8e51 ths
298 663e8e51 ths
    switch (nwords) {
299 663e8e51 ths
        case 16:
300 663e8e51 ths
        case 64:
301 663e8e51 ths
            addrbits = 6;
302 663e8e51 ths
            break;
303 663e8e51 ths
        case 128:
304 663e8e51 ths
        case 256:
305 663e8e51 ths
            addrbits = 8;
306 663e8e51 ths
            break;
307 663e8e51 ths
        default:
308 663e8e51 ths
            assert(!"Unsupported EEPROM size, fallback to 64 words!");
309 663e8e51 ths
            nwords = 64;
310 663e8e51 ths
            addrbits = 6;
311 663e8e51 ths
    }
312 663e8e51 ths
313 c227f099 Anthony Liguori
    eeprom = (eeprom_t *)qemu_mallocz(sizeof(*eeprom) + nwords * 2);
314 663e8e51 ths
    eeprom->size = nwords;
315 663e8e51 ths
    eeprom->addrbits = addrbits;
316 663e8e51 ths
    /* Output DO is tristate, read results in 1. */
317 663e8e51 ths
    eeprom->eedo = 1;
318 663e8e51 ths
    logout("eeprom = 0x%p, nwords = %u\n", eeprom, nwords);
319 5fce2b3e Alex Williamson
    vmstate_register(dev, 0, &vmstate_eeprom, eeprom);
320 663e8e51 ths
    return eeprom;
321 663e8e51 ths
}
322 663e8e51 ths
323 5fce2b3e Alex Williamson
void eeprom93xx_free(DeviceState *dev, eeprom_t *eeprom)
324 663e8e51 ths
{
325 663e8e51 ths
    /* Destroy EEPROM. */
326 663e8e51 ths
    logout("eeprom = 0x%p\n", eeprom);
327 5fce2b3e Alex Williamson
    vmstate_unregister(dev, &vmstate_eeprom, eeprom);
328 663e8e51 ths
    qemu_free(eeprom);
329 663e8e51 ths
}
330 663e8e51 ths
331 c227f099 Anthony Liguori
uint16_t *eeprom93xx_data(eeprom_t *eeprom)
332 663e8e51 ths
{
333 663e8e51 ths
    /* Get EEPROM data array. */
334 663e8e51 ths
    return &eeprom->contents[0];
335 663e8e51 ths
}
336 663e8e51 ths
337 663e8e51 ths
/* eof */