Statistics
| Branch: | Revision:

root / hw / pflash_cfi02.c @ c3203fa5

History | View | Annotate | Download (23.8 kB)

1 29133e9a bellard
/*
2 29133e9a bellard
 *  CFI parallel flash with AMD command set emulation
3 5fafdf24 ths
 *
4 29133e9a bellard
 *  Copyright (c) 2005 Jocelyn Mayer
5 29133e9a bellard
 *
6 29133e9a bellard
 * This library is free software; you can redistribute it and/or
7 29133e9a bellard
 * modify it under the terms of the GNU Lesser General Public
8 29133e9a bellard
 * License as published by the Free Software Foundation; either
9 29133e9a bellard
 * version 2 of the License, or (at your option) any later version.
10 29133e9a bellard
 *
11 29133e9a bellard
 * This library is distributed in the hope that it will be useful,
12 29133e9a bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 29133e9a bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 29133e9a bellard
 * Lesser General Public License for more details.
15 29133e9a bellard
 *
16 29133e9a bellard
 * You should have received a copy of the GNU Lesser General Public
17 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 29133e9a bellard
 */
19 29133e9a bellard
20 29133e9a bellard
/*
21 29133e9a bellard
 * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
22 29133e9a bellard
 * Supported commands/modes are:
23 29133e9a bellard
 * - flash read
24 29133e9a bellard
 * - flash write
25 29133e9a bellard
 * - flash ID read
26 29133e9a bellard
 * - sector erase
27 29133e9a bellard
 * - chip erase
28 29133e9a bellard
 * - unlock bypass command
29 29133e9a bellard
 * - CFI queries
30 29133e9a bellard
 *
31 29133e9a bellard
 * It does not support flash interleaving.
32 29133e9a bellard
 * It does not implement boot blocs with reduced size
33 29133e9a bellard
 * It does not implement software data protection as found in many real chips
34 29133e9a bellard
 * It does not implement erase suspend/resume commands
35 29133e9a bellard
 * It does not implement multiple sectors erase
36 29133e9a bellard
 */
37 29133e9a bellard
38 87ecb68b pbrook
#include "hw.h"
39 87ecb68b pbrook
#include "flash.h"
40 1de7afc9 Paolo Bonzini
#include "qemu/timer.h"
41 737e150e Paolo Bonzini
#include "block/block.h"
42 022c62cb Paolo Bonzini
#include "exec/address-spaces.h"
43 1de7afc9 Paolo Bonzini
#include "qemu/host-utils.h"
44 368a354f Peter Crosthwaite
#include "sysbus.h"
45 29133e9a bellard
46 29133e9a bellard
//#define PFLASH_DEBUG
47 29133e9a bellard
#ifdef PFLASH_DEBUG
48 ec9ea489 Peter Crosthwaite
#define DPRINTF(fmt, ...)                                  \
49 ec9ea489 Peter Crosthwaite
do {                                                       \
50 ec9ea489 Peter Crosthwaite
    fprintf(stderr "PFLASH: " fmt , ## __VA_ARGS__);       \
51 29133e9a bellard
} while (0)
52 29133e9a bellard
#else
53 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) do { } while (0)
54 29133e9a bellard
#endif
55 29133e9a bellard
56 661bfc80 Jan Kiszka
#define PFLASH_LAZY_ROMD_THRESHOLD 42
57 661bfc80 Jan Kiszka
58 c227f099 Anthony Liguori
struct pflash_t {
59 368a354f Peter Crosthwaite
    SysBusDevice busdev;
60 29133e9a bellard
    BlockDriverState *bs;
61 71db710f blueswir1
    uint32_t sector_len;
62 368a354f Peter Crosthwaite
    uint32_t nb_blocs;
63 4fbd24ba balrog
    uint32_t chip_len;
64 368a354f Peter Crosthwaite
    uint8_t mappings;
65 368a354f Peter Crosthwaite
    uint8_t width;
66 368a354f Peter Crosthwaite
    uint8_t be;
67 29133e9a bellard
    int wcycle; /* if 0, the flash is read normally */
68 29133e9a bellard
    int bypass;
69 29133e9a bellard
    int ro;
70 29133e9a bellard
    uint8_t cmd;
71 29133e9a bellard
    uint8_t status;
72 368a354f Peter Crosthwaite
    /* FIXME: implement array device properties */
73 368a354f Peter Crosthwaite
    uint16_t ident0;
74 368a354f Peter Crosthwaite
    uint16_t ident1;
75 368a354f Peter Crosthwaite
    uint16_t ident2;
76 368a354f Peter Crosthwaite
    uint16_t ident3;
77 368a354f Peter Crosthwaite
    uint16_t unlock_addr0;
78 368a354f Peter Crosthwaite
    uint16_t unlock_addr1;
79 29133e9a bellard
    uint8_t cfi_len;
80 29133e9a bellard
    uint8_t cfi_table[0x52];
81 29133e9a bellard
    QEMUTimer *timer;
82 cfe5f011 Avi Kivity
    /* The device replicates the flash memory across its memory space.  Emulate
83 cfe5f011 Avi Kivity
     * that by having a container (.mem) filled with an array of aliases
84 cfe5f011 Avi Kivity
     * (.mem_mappings) pointing to the flash memory (.orig_mem).
85 cfe5f011 Avi Kivity
     */
86 cfe5f011 Avi Kivity
    MemoryRegion mem;
87 cfe5f011 Avi Kivity
    MemoryRegion *mem_mappings;    /* array; one per mapping */
88 cfe5f011 Avi Kivity
    MemoryRegion orig_mem;
89 9c9bb6c8 balrog
    int rom_mode;
90 661bfc80 Jan Kiszka
    int read_counter; /* used for lazy switch-back to rom mode */
91 368a354f Peter Crosthwaite
    char *name;
92 29133e9a bellard
    void *storage;
93 29133e9a bellard
};
94 29133e9a bellard
95 cfe5f011 Avi Kivity
/*
96 cfe5f011 Avi Kivity
 * Set up replicated mappings of the same region.
97 cfe5f011 Avi Kivity
 */
98 cfe5f011 Avi Kivity
static void pflash_setup_mappings(pflash_t *pfl)
99 c8a50e59 Avi Kivity
{
100 cfe5f011 Avi Kivity
    unsigned i;
101 a8170e5e Avi Kivity
    hwaddr size = memory_region_size(&pfl->orig_mem);
102 cfe5f011 Avi Kivity
103 cfe5f011 Avi Kivity
    memory_region_init(&pfl->mem, "pflash", pfl->mappings * size);
104 cfe5f011 Avi Kivity
    pfl->mem_mappings = g_new(MemoryRegion, pfl->mappings);
105 cfe5f011 Avi Kivity
    for (i = 0; i < pfl->mappings; ++i) {
106 cfe5f011 Avi Kivity
        memory_region_init_alias(&pfl->mem_mappings[i], "pflash-alias",
107 cfe5f011 Avi Kivity
                                 &pfl->orig_mem, 0, size);
108 cfe5f011 Avi Kivity
        memory_region_add_subregion(&pfl->mem, i * size, &pfl->mem_mappings[i]);
109 cfe5f011 Avi Kivity
    }
110 cfe5f011 Avi Kivity
}
111 01e0451a Anthony Liguori
112 cfe5f011 Avi Kivity
static void pflash_register_memory(pflash_t *pfl, int rom_mode)
113 cfe5f011 Avi Kivity
{
114 cfe5f011 Avi Kivity
    memory_region_rom_device_set_readable(&pfl->orig_mem, rom_mode);
115 bda254da Jan Kiszka
    pfl->rom_mode = rom_mode;
116 4fbd24ba balrog
}
117 4fbd24ba balrog
118 29133e9a bellard
static void pflash_timer (void *opaque)
119 29133e9a bellard
{
120 c227f099 Anthony Liguori
    pflash_t *pfl = opaque;
121 29133e9a bellard
122 29133e9a bellard
    DPRINTF("%s: command %02x done\n", __func__, pfl->cmd);
123 29133e9a bellard
    /* Reset flash */
124 29133e9a bellard
    pfl->status ^= 0x80;
125 29133e9a bellard
    if (pfl->bypass) {
126 29133e9a bellard
        pfl->wcycle = 2;
127 29133e9a bellard
    } else {
128 4fbd24ba balrog
        pflash_register_memory(pfl, 1);
129 29133e9a bellard
        pfl->wcycle = 0;
130 29133e9a bellard
    }
131 29133e9a bellard
    pfl->cmd = 0;
132 29133e9a bellard
}
133 29133e9a bellard
134 a8170e5e Avi Kivity
static uint32_t pflash_read (pflash_t *pfl, hwaddr offset,
135 5f9fc5ad Blue Swirl
                             int width, int be)
136 29133e9a bellard
{
137 a8170e5e Avi Kivity
    hwaddr boff;
138 29133e9a bellard
    uint32_t ret;
139 29133e9a bellard
    uint8_t *p;
140 29133e9a bellard
141 f8be67ee Blue Swirl
    DPRINTF("%s: offset " TARGET_FMT_plx "\n", __func__, offset);
142 29133e9a bellard
    ret = -1;
143 661bfc80 Jan Kiszka
    /* Lazy reset to ROMD mode after a certain amount of read accesses */
144 661bfc80 Jan Kiszka
    if (!pfl->rom_mode && pfl->wcycle == 0 &&
145 661bfc80 Jan Kiszka
        ++pfl->read_counter > PFLASH_LAZY_ROMD_THRESHOLD) {
146 661bfc80 Jan Kiszka
        pflash_register_memory(pfl, 1);
147 0f459d16 pbrook
    }
148 4fbd24ba balrog
    offset &= pfl->chip_len - 1;
149 29133e9a bellard
    boff = offset & 0xFF;
150 29133e9a bellard
    if (pfl->width == 2)
151 29133e9a bellard
        boff = boff >> 1;
152 29133e9a bellard
    else if (pfl->width == 4)
153 29133e9a bellard
        boff = boff >> 2;
154 29133e9a bellard
    switch (pfl->cmd) {
155 29133e9a bellard
    default:
156 29133e9a bellard
        /* This should never happen : reset state & treat it as a read*/
157 29133e9a bellard
        DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
158 29133e9a bellard
        pfl->wcycle = 0;
159 29133e9a bellard
        pfl->cmd = 0;
160 29133e9a bellard
    case 0x80:
161 29133e9a bellard
        /* We accept reads during second unlock sequence... */
162 29133e9a bellard
    case 0x00:
163 29133e9a bellard
    flash_read:
164 29133e9a bellard
        /* Flash area read */
165 29133e9a bellard
        p = pfl->storage;
166 29133e9a bellard
        switch (width) {
167 29133e9a bellard
        case 1:
168 29133e9a bellard
            ret = p[offset];
169 29133e9a bellard
//            DPRINTF("%s: data offset %08x %02x\n", __func__, offset, ret);
170 29133e9a bellard
            break;
171 29133e9a bellard
        case 2:
172 5f9fc5ad Blue Swirl
            if (be) {
173 5f9fc5ad Blue Swirl
                ret = p[offset] << 8;
174 5f9fc5ad Blue Swirl
                ret |= p[offset + 1];
175 5f9fc5ad Blue Swirl
            } else {
176 5f9fc5ad Blue Swirl
                ret = p[offset];
177 5f9fc5ad Blue Swirl
                ret |= p[offset + 1] << 8;
178 5f9fc5ad Blue Swirl
            }
179 29133e9a bellard
//            DPRINTF("%s: data offset %08x %04x\n", __func__, offset, ret);
180 29133e9a bellard
            break;
181 29133e9a bellard
        case 4:
182 5f9fc5ad Blue Swirl
            if (be) {
183 5f9fc5ad Blue Swirl
                ret = p[offset] << 24;
184 5f9fc5ad Blue Swirl
                ret |= p[offset + 1] << 16;
185 5f9fc5ad Blue Swirl
                ret |= p[offset + 2] << 8;
186 5f9fc5ad Blue Swirl
                ret |= p[offset + 3];
187 5f9fc5ad Blue Swirl
            } else {
188 5f9fc5ad Blue Swirl
                ret = p[offset];
189 5f9fc5ad Blue Swirl
                ret |= p[offset + 1] << 8;
190 5f9fc5ad Blue Swirl
                ret |= p[offset + 2] << 16;
191 5f9fc5ad Blue Swirl
                ret |= p[offset + 3] << 24;
192 5f9fc5ad Blue Swirl
            }
193 29133e9a bellard
//            DPRINTF("%s: data offset %08x %08x\n", __func__, offset, ret);
194 29133e9a bellard
            break;
195 29133e9a bellard
        }
196 29133e9a bellard
        break;
197 29133e9a bellard
    case 0x90:
198 29133e9a bellard
        /* flash ID read */
199 29133e9a bellard
        switch (boff) {
200 29133e9a bellard
        case 0x00:
201 29133e9a bellard
        case 0x01:
202 368a354f Peter Crosthwaite
            ret = boff & 0x01 ? pfl->ident1 : pfl->ident0;
203 29133e9a bellard
            break;
204 29133e9a bellard
        case 0x02:
205 29133e9a bellard
            ret = 0x00; /* Pretend all sectors are unprotected */
206 29133e9a bellard
            break;
207 29133e9a bellard
        case 0x0E:
208 29133e9a bellard
        case 0x0F:
209 368a354f Peter Crosthwaite
            ret = boff & 0x01 ? pfl->ident3 : pfl->ident2;
210 368a354f Peter Crosthwaite
            if (ret == (uint8_t)-1) {
211 29133e9a bellard
                goto flash_read;
212 368a354f Peter Crosthwaite
            }
213 29133e9a bellard
            break;
214 29133e9a bellard
        default:
215 29133e9a bellard
            goto flash_read;
216 29133e9a bellard
        }
217 b9055c3c Stefan Weil
        DPRINTF("%s: ID " TARGET_FMT_plx " %x\n", __func__, boff, ret);
218 29133e9a bellard
        break;
219 29133e9a bellard
    case 0xA0:
220 29133e9a bellard
    case 0x10:
221 29133e9a bellard
    case 0x30:
222 29133e9a bellard
        /* Status register read */
223 29133e9a bellard
        ret = pfl->status;
224 29133e9a bellard
        DPRINTF("%s: status %x\n", __func__, ret);
225 29133e9a bellard
        /* Toggle bit 6 */
226 29133e9a bellard
        pfl->status ^= 0x40;
227 29133e9a bellard
        break;
228 29133e9a bellard
    case 0x98:
229 29133e9a bellard
        /* CFI query mode */
230 29133e9a bellard
        if (boff > pfl->cfi_len)
231 29133e9a bellard
            ret = 0;
232 29133e9a bellard
        else
233 29133e9a bellard
            ret = pfl->cfi_table[boff];
234 29133e9a bellard
        break;
235 29133e9a bellard
    }
236 29133e9a bellard
237 29133e9a bellard
    return ret;
238 29133e9a bellard
}
239 29133e9a bellard
240 29133e9a bellard
/* update flash content on disk */
241 c227f099 Anthony Liguori
static void pflash_update(pflash_t *pfl, int offset,
242 29133e9a bellard
                          int size)
243 29133e9a bellard
{
244 29133e9a bellard
    int offset_end;
245 29133e9a bellard
    if (pfl->bs) {
246 29133e9a bellard
        offset_end = offset + size;
247 29133e9a bellard
        /* round to sectors */
248 29133e9a bellard
        offset = offset >> 9;
249 29133e9a bellard
        offset_end = (offset_end + 511) >> 9;
250 5fafdf24 ths
        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
251 29133e9a bellard
                   offset_end - offset);
252 29133e9a bellard
    }
253 29133e9a bellard
}
254 29133e9a bellard
255 a8170e5e Avi Kivity
static void pflash_write (pflash_t *pfl, hwaddr offset,
256 5f9fc5ad Blue Swirl
                          uint32_t value, int width, int be)
257 29133e9a bellard
{
258 a8170e5e Avi Kivity
    hwaddr boff;
259 29133e9a bellard
    uint8_t *p;
260 29133e9a bellard
    uint8_t cmd;
261 29133e9a bellard
262 95d1f3ed j_mayer
    cmd = value;
263 95d1f3ed j_mayer
    if (pfl->cmd != 0xA0 && cmd == 0xF0) {
264 95d1f3ed j_mayer
#if 0
265 95d1f3ed j_mayer
        DPRINTF("%s: flash reset asked (%02x %02x)\n",
266 95d1f3ed j_mayer
                __func__, pfl->cmd, cmd);
267 95d1f3ed j_mayer
#endif
268 95d1f3ed j_mayer
        goto reset_flash;
269 95d1f3ed j_mayer
    }
270 f8be67ee Blue Swirl
    DPRINTF("%s: offset " TARGET_FMT_plx " %08x %d %d\n", __func__,
271 95d1f3ed j_mayer
            offset, value, width, pfl->wcycle);
272 4fbd24ba balrog
    offset &= pfl->chip_len - 1;
273 3b46e624 ths
274 f8be67ee Blue Swirl
    DPRINTF("%s: offset " TARGET_FMT_plx " %08x %d\n", __func__,
275 e96efcfc j_mayer
            offset, value, width);
276 29133e9a bellard
    boff = offset & (pfl->sector_len - 1);
277 29133e9a bellard
    if (pfl->width == 2)
278 29133e9a bellard
        boff = boff >> 1;
279 29133e9a bellard
    else if (pfl->width == 4)
280 29133e9a bellard
        boff = boff >> 2;
281 29133e9a bellard
    switch (pfl->wcycle) {
282 29133e9a bellard
    case 0:
283 9c9bb6c8 balrog
        /* Set the device in I/O access mode if required */
284 9c9bb6c8 balrog
        if (pfl->rom_mode)
285 9c9bb6c8 balrog
            pflash_register_memory(pfl, 0);
286 661bfc80 Jan Kiszka
        pfl->read_counter = 0;
287 29133e9a bellard
        /* We're in read mode */
288 29133e9a bellard
    check_unlock0:
289 29133e9a bellard
        if (boff == 0x55 && cmd == 0x98) {
290 29133e9a bellard
        enter_CFI_mode:
291 29133e9a bellard
            /* Enter CFI query mode */
292 29133e9a bellard
            pfl->wcycle = 7;
293 29133e9a bellard
            pfl->cmd = 0x98;
294 29133e9a bellard
            return;
295 29133e9a bellard
        }
296 368a354f Peter Crosthwaite
        if (boff != pfl->unlock_addr0 || cmd != 0xAA) {
297 f8be67ee Blue Swirl
            DPRINTF("%s: unlock0 failed " TARGET_FMT_plx " %02x %04x\n",
298 368a354f Peter Crosthwaite
                    __func__, boff, cmd, pfl->unlock_addr0);
299 29133e9a bellard
            goto reset_flash;
300 29133e9a bellard
        }
301 29133e9a bellard
        DPRINTF("%s: unlock sequence started\n", __func__);
302 29133e9a bellard
        break;
303 29133e9a bellard
    case 1:
304 29133e9a bellard
        /* We started an unlock sequence */
305 29133e9a bellard
    check_unlock1:
306 368a354f Peter Crosthwaite
        if (boff != pfl->unlock_addr1 || cmd != 0x55) {
307 f8be67ee Blue Swirl
            DPRINTF("%s: unlock1 failed " TARGET_FMT_plx " %02x\n", __func__,
308 e96efcfc j_mayer
                    boff, cmd);
309 29133e9a bellard
            goto reset_flash;
310 29133e9a bellard
        }
311 29133e9a bellard
        DPRINTF("%s: unlock sequence done\n", __func__);
312 29133e9a bellard
        break;
313 29133e9a bellard
    case 2:
314 29133e9a bellard
        /* We finished an unlock sequence */
315 368a354f Peter Crosthwaite
        if (!pfl->bypass && boff != pfl->unlock_addr0) {
316 f8be67ee Blue Swirl
            DPRINTF("%s: command failed " TARGET_FMT_plx " %02x\n", __func__,
317 e96efcfc j_mayer
                    boff, cmd);
318 29133e9a bellard
            goto reset_flash;
319 29133e9a bellard
        }
320 29133e9a bellard
        switch (cmd) {
321 29133e9a bellard
        case 0x20:
322 29133e9a bellard
            pfl->bypass = 1;
323 29133e9a bellard
            goto do_bypass;
324 29133e9a bellard
        case 0x80:
325 29133e9a bellard
        case 0x90:
326 29133e9a bellard
        case 0xA0:
327 29133e9a bellard
            pfl->cmd = cmd;
328 29133e9a bellard
            DPRINTF("%s: starting command %02x\n", __func__, cmd);
329 29133e9a bellard
            break;
330 29133e9a bellard
        default:
331 29133e9a bellard
            DPRINTF("%s: unknown command %02x\n", __func__, cmd);
332 29133e9a bellard
            goto reset_flash;
333 29133e9a bellard
        }
334 29133e9a bellard
        break;
335 29133e9a bellard
    case 3:
336 29133e9a bellard
        switch (pfl->cmd) {
337 29133e9a bellard
        case 0x80:
338 29133e9a bellard
            /* We need another unlock sequence */
339 29133e9a bellard
            goto check_unlock0;
340 29133e9a bellard
        case 0xA0:
341 f8be67ee Blue Swirl
            DPRINTF("%s: write data offset " TARGET_FMT_plx " %08x %d\n",
342 29133e9a bellard
                    __func__, offset, value, width);
343 29133e9a bellard
            p = pfl->storage;
344 de8efe8f Jordan Justen
            if (!pfl->ro) {
345 de8efe8f Jordan Justen
                switch (width) {
346 de8efe8f Jordan Justen
                case 1:
347 5f9fc5ad Blue Swirl
                    p[offset] &= value;
348 de8efe8f Jordan Justen
                    pflash_update(pfl, offset, 1);
349 de8efe8f Jordan Justen
                    break;
350 de8efe8f Jordan Justen
                case 2:
351 de8efe8f Jordan Justen
                    if (be) {
352 de8efe8f Jordan Justen
                        p[offset] &= value >> 8;
353 de8efe8f Jordan Justen
                        p[offset + 1] &= value;
354 de8efe8f Jordan Justen
                    } else {
355 de8efe8f Jordan Justen
                        p[offset] &= value;
356 de8efe8f Jordan Justen
                        p[offset + 1] &= value >> 8;
357 de8efe8f Jordan Justen
                    }
358 de8efe8f Jordan Justen
                    pflash_update(pfl, offset, 2);
359 de8efe8f Jordan Justen
                    break;
360 de8efe8f Jordan Justen
                case 4:
361 de8efe8f Jordan Justen
                    if (be) {
362 de8efe8f Jordan Justen
                        p[offset] &= value >> 24;
363 de8efe8f Jordan Justen
                        p[offset + 1] &= value >> 16;
364 de8efe8f Jordan Justen
                        p[offset + 2] &= value >> 8;
365 de8efe8f Jordan Justen
                        p[offset + 3] &= value;
366 de8efe8f Jordan Justen
                    } else {
367 de8efe8f Jordan Justen
                        p[offset] &= value;
368 de8efe8f Jordan Justen
                        p[offset + 1] &= value >> 8;
369 de8efe8f Jordan Justen
                        p[offset + 2] &= value >> 16;
370 de8efe8f Jordan Justen
                        p[offset + 3] &= value >> 24;
371 de8efe8f Jordan Justen
                    }
372 de8efe8f Jordan Justen
                    pflash_update(pfl, offset, 4);
373 de8efe8f Jordan Justen
                    break;
374 5f9fc5ad Blue Swirl
                }
375 29133e9a bellard
            }
376 29133e9a bellard
            pfl->status = 0x00 | ~(value & 0x80);
377 29133e9a bellard
            /* Let's pretend write is immediate */
378 29133e9a bellard
            if (pfl->bypass)
379 29133e9a bellard
                goto do_bypass;
380 29133e9a bellard
            goto reset_flash;
381 29133e9a bellard
        case 0x90:
382 29133e9a bellard
            if (pfl->bypass && cmd == 0x00) {
383 29133e9a bellard
                /* Unlock bypass reset */
384 29133e9a bellard
                goto reset_flash;
385 29133e9a bellard
            }
386 29133e9a bellard
            /* We can enter CFI query mode from autoselect mode */
387 29133e9a bellard
            if (boff == 0x55 && cmd == 0x98)
388 29133e9a bellard
                goto enter_CFI_mode;
389 29133e9a bellard
            /* No break here */
390 29133e9a bellard
        default:
391 29133e9a bellard
            DPRINTF("%s: invalid write for command %02x\n",
392 29133e9a bellard
                    __func__, pfl->cmd);
393 29133e9a bellard
            goto reset_flash;
394 29133e9a bellard
        }
395 29133e9a bellard
    case 4:
396 29133e9a bellard
        switch (pfl->cmd) {
397 29133e9a bellard
        case 0xA0:
398 a1c7273b Stefan Weil
            /* Ignore writes while flash data write is occurring */
399 29133e9a bellard
            /* As we suppose write is immediate, this should never happen */
400 29133e9a bellard
            return;
401 29133e9a bellard
        case 0x80:
402 29133e9a bellard
            goto check_unlock1;
403 29133e9a bellard
        default:
404 29133e9a bellard
            /* Should never happen */
405 29133e9a bellard
            DPRINTF("%s: invalid command state %02x (wc 4)\n",
406 29133e9a bellard
                    __func__, pfl->cmd);
407 29133e9a bellard
            goto reset_flash;
408 29133e9a bellard
        }
409 29133e9a bellard
        break;
410 29133e9a bellard
    case 5:
411 29133e9a bellard
        switch (cmd) {
412 29133e9a bellard
        case 0x10:
413 368a354f Peter Crosthwaite
            if (boff != pfl->unlock_addr0) {
414 f8be67ee Blue Swirl
                DPRINTF("%s: chip erase: invalid address " TARGET_FMT_plx "\n",
415 29133e9a bellard
                        __func__, offset);
416 29133e9a bellard
                goto reset_flash;
417 29133e9a bellard
            }
418 29133e9a bellard
            /* Chip erase */
419 29133e9a bellard
            DPRINTF("%s: start chip erase\n", __func__);
420 de8efe8f Jordan Justen
            if (!pfl->ro) {
421 de8efe8f Jordan Justen
                memset(pfl->storage, 0xFF, pfl->chip_len);
422 de8efe8f Jordan Justen
                pflash_update(pfl, 0, pfl->chip_len);
423 de8efe8f Jordan Justen
            }
424 29133e9a bellard
            pfl->status = 0x00;
425 29133e9a bellard
            /* Let's wait 5 seconds before chip erase is done */
426 5fafdf24 ths
            qemu_mod_timer(pfl->timer,
427 74475455 Paolo Bonzini
                           qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() * 5));
428 29133e9a bellard
            break;
429 29133e9a bellard
        case 0x30:
430 29133e9a bellard
            /* Sector erase */
431 29133e9a bellard
            p = pfl->storage;
432 29133e9a bellard
            offset &= ~(pfl->sector_len - 1);
433 f8be67ee Blue Swirl
            DPRINTF("%s: start sector erase at " TARGET_FMT_plx "\n", __func__,
434 e96efcfc j_mayer
                    offset);
435 de8efe8f Jordan Justen
            if (!pfl->ro) {
436 de8efe8f Jordan Justen
                memset(p + offset, 0xFF, pfl->sector_len);
437 de8efe8f Jordan Justen
                pflash_update(pfl, offset, pfl->sector_len);
438 de8efe8f Jordan Justen
            }
439 29133e9a bellard
            pfl->status = 0x00;
440 29133e9a bellard
            /* Let's wait 1/2 second before sector erase is done */
441 5fafdf24 ths
            qemu_mod_timer(pfl->timer,
442 74475455 Paolo Bonzini
                           qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() / 2));
443 29133e9a bellard
            break;
444 29133e9a bellard
        default:
445 29133e9a bellard
            DPRINTF("%s: invalid command %02x (wc 5)\n", __func__, cmd);
446 29133e9a bellard
            goto reset_flash;
447 29133e9a bellard
        }
448 29133e9a bellard
        pfl->cmd = cmd;
449 29133e9a bellard
        break;
450 29133e9a bellard
    case 6:
451 29133e9a bellard
        switch (pfl->cmd) {
452 29133e9a bellard
        case 0x10:
453 29133e9a bellard
            /* Ignore writes during chip erase */
454 29133e9a bellard
            return;
455 29133e9a bellard
        case 0x30:
456 29133e9a bellard
            /* Ignore writes during sector erase */
457 29133e9a bellard
            return;
458 29133e9a bellard
        default:
459 29133e9a bellard
            /* Should never happen */
460 29133e9a bellard
            DPRINTF("%s: invalid command state %02x (wc 6)\n",
461 29133e9a bellard
                    __func__, pfl->cmd);
462 29133e9a bellard
            goto reset_flash;
463 29133e9a bellard
        }
464 29133e9a bellard
        break;
465 29133e9a bellard
    case 7: /* Special value for CFI queries */
466 29133e9a bellard
        DPRINTF("%s: invalid write in CFI query mode\n", __func__);
467 29133e9a bellard
        goto reset_flash;
468 29133e9a bellard
    default:
469 29133e9a bellard
        /* Should never happen */
470 29133e9a bellard
        DPRINTF("%s: invalid write state (wc 7)\n",  __func__);
471 29133e9a bellard
        goto reset_flash;
472 29133e9a bellard
    }
473 29133e9a bellard
    pfl->wcycle++;
474 29133e9a bellard
475 29133e9a bellard
    return;
476 29133e9a bellard
477 29133e9a bellard
    /* Reset flash */
478 29133e9a bellard
 reset_flash:
479 29133e9a bellard
    pfl->bypass = 0;
480 29133e9a bellard
    pfl->wcycle = 0;
481 29133e9a bellard
    pfl->cmd = 0;
482 29133e9a bellard
    return;
483 29133e9a bellard
484 29133e9a bellard
 do_bypass:
485 29133e9a bellard
    pfl->wcycle = 2;
486 29133e9a bellard
    pfl->cmd = 0;
487 29133e9a bellard
}
488 29133e9a bellard
489 29133e9a bellard
490 a8170e5e Avi Kivity
static uint32_t pflash_readb_be(void *opaque, hwaddr addr)
491 5f9fc5ad Blue Swirl
{
492 5f9fc5ad Blue Swirl
    return pflash_read(opaque, addr, 1, 1);
493 5f9fc5ad Blue Swirl
}
494 5f9fc5ad Blue Swirl
495 a8170e5e Avi Kivity
static uint32_t pflash_readb_le(void *opaque, hwaddr addr)
496 5f9fc5ad Blue Swirl
{
497 5f9fc5ad Blue Swirl
    return pflash_read(opaque, addr, 1, 0);
498 5f9fc5ad Blue Swirl
}
499 5f9fc5ad Blue Swirl
500 a8170e5e Avi Kivity
static uint32_t pflash_readw_be(void *opaque, hwaddr addr)
501 5f9fc5ad Blue Swirl
{
502 5f9fc5ad Blue Swirl
    pflash_t *pfl = opaque;
503 5f9fc5ad Blue Swirl
504 5f9fc5ad Blue Swirl
    return pflash_read(pfl, addr, 2, 1);
505 5f9fc5ad Blue Swirl
}
506 5f9fc5ad Blue Swirl
507 a8170e5e Avi Kivity
static uint32_t pflash_readw_le(void *opaque, hwaddr addr)
508 5f9fc5ad Blue Swirl
{
509 5f9fc5ad Blue Swirl
    pflash_t *pfl = opaque;
510 5f9fc5ad Blue Swirl
511 5f9fc5ad Blue Swirl
    return pflash_read(pfl, addr, 2, 0);
512 5f9fc5ad Blue Swirl
}
513 5f9fc5ad Blue Swirl
514 a8170e5e Avi Kivity
static uint32_t pflash_readl_be(void *opaque, hwaddr addr)
515 29133e9a bellard
{
516 5f9fc5ad Blue Swirl
    pflash_t *pfl = opaque;
517 5f9fc5ad Blue Swirl
518 5f9fc5ad Blue Swirl
    return pflash_read(pfl, addr, 4, 1);
519 29133e9a bellard
}
520 29133e9a bellard
521 a8170e5e Avi Kivity
static uint32_t pflash_readl_le(void *opaque, hwaddr addr)
522 29133e9a bellard
{
523 c227f099 Anthony Liguori
    pflash_t *pfl = opaque;
524 29133e9a bellard
525 5f9fc5ad Blue Swirl
    return pflash_read(pfl, addr, 4, 0);
526 5f9fc5ad Blue Swirl
}
527 5f9fc5ad Blue Swirl
528 a8170e5e Avi Kivity
static void pflash_writeb_be(void *opaque, hwaddr addr,
529 5f9fc5ad Blue Swirl
                             uint32_t value)
530 5f9fc5ad Blue Swirl
{
531 5f9fc5ad Blue Swirl
    pflash_write(opaque, addr, value, 1, 1);
532 29133e9a bellard
}
533 29133e9a bellard
534 a8170e5e Avi Kivity
static void pflash_writeb_le(void *opaque, hwaddr addr,
535 5f9fc5ad Blue Swirl
                             uint32_t value)
536 5f9fc5ad Blue Swirl
{
537 5f9fc5ad Blue Swirl
    pflash_write(opaque, addr, value, 1, 0);
538 5f9fc5ad Blue Swirl
}
539 5f9fc5ad Blue Swirl
540 a8170e5e Avi Kivity
static void pflash_writew_be(void *opaque, hwaddr addr,
541 5f9fc5ad Blue Swirl
                             uint32_t value)
542 29133e9a bellard
{
543 c227f099 Anthony Liguori
    pflash_t *pfl = opaque;
544 29133e9a bellard
545 5f9fc5ad Blue Swirl
    pflash_write(pfl, addr, value, 2, 1);
546 29133e9a bellard
}
547 29133e9a bellard
548 a8170e5e Avi Kivity
static void pflash_writew_le(void *opaque, hwaddr addr,
549 5f9fc5ad Blue Swirl
                             uint32_t value)
550 29133e9a bellard
{
551 5f9fc5ad Blue Swirl
    pflash_t *pfl = opaque;
552 5f9fc5ad Blue Swirl
553 5f9fc5ad Blue Swirl
    pflash_write(pfl, addr, value, 2, 0);
554 29133e9a bellard
}
555 29133e9a bellard
556 a8170e5e Avi Kivity
static void pflash_writel_be(void *opaque, hwaddr addr,
557 5f9fc5ad Blue Swirl
                             uint32_t value)
558 29133e9a bellard
{
559 c227f099 Anthony Liguori
    pflash_t *pfl = opaque;
560 29133e9a bellard
561 5f9fc5ad Blue Swirl
    pflash_write(pfl, addr, value, 4, 1);
562 29133e9a bellard
}
563 29133e9a bellard
564 a8170e5e Avi Kivity
static void pflash_writel_le(void *opaque, hwaddr addr,
565 5f9fc5ad Blue Swirl
                             uint32_t value)
566 29133e9a bellard
{
567 c227f099 Anthony Liguori
    pflash_t *pfl = opaque;
568 29133e9a bellard
569 5f9fc5ad Blue Swirl
    pflash_write(pfl, addr, value, 4, 0);
570 29133e9a bellard
}
571 29133e9a bellard
572 cfe5f011 Avi Kivity
static const MemoryRegionOps pflash_cfi02_ops_be = {
573 cfe5f011 Avi Kivity
    .old_mmio = {
574 cfe5f011 Avi Kivity
        .read = { pflash_readb_be, pflash_readw_be, pflash_readl_be, },
575 cfe5f011 Avi Kivity
        .write = { pflash_writeb_be, pflash_writew_be, pflash_writel_be, },
576 cfe5f011 Avi Kivity
    },
577 cfe5f011 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
578 5f9fc5ad Blue Swirl
};
579 5f9fc5ad Blue Swirl
580 cfe5f011 Avi Kivity
static const MemoryRegionOps pflash_cfi02_ops_le = {
581 cfe5f011 Avi Kivity
    .old_mmio = {
582 cfe5f011 Avi Kivity
        .read = { pflash_readb_le, pflash_readw_le, pflash_readl_le, },
583 cfe5f011 Avi Kivity
        .write = { pflash_writeb_le, pflash_writew_le, pflash_writel_le, },
584 cfe5f011 Avi Kivity
    },
585 cfe5f011 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
586 29133e9a bellard
};
587 29133e9a bellard
588 368a354f Peter Crosthwaite
static int pflash_cfi02_init(SysBusDevice *dev)
589 29133e9a bellard
{
590 368a354f Peter Crosthwaite
    pflash_t *pfl = FROM_SYSBUS(typeof(*pfl), dev);
591 368a354f Peter Crosthwaite
    uint32_t chip_len;
592 d0e7605e Vijay Kumar
    int ret;
593 29133e9a bellard
594 368a354f Peter Crosthwaite
    chip_len = pfl->sector_len * pfl->nb_blocs;
595 29133e9a bellard
    /* XXX: to be fixed */
596 95d1f3ed j_mayer
#if 0
597 29133e9a bellard
    if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) &&
598 29133e9a bellard
        total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024))
599 29133e9a bellard
        return NULL;
600 95d1f3ed j_mayer
#endif
601 368a354f Peter Crosthwaite
602 368a354f Peter Crosthwaite
    memory_region_init_rom_device(&pfl->orig_mem, pfl->be ?
603 368a354f Peter Crosthwaite
                                  &pflash_cfi02_ops_be : &pflash_cfi02_ops_le,
604 368a354f Peter Crosthwaite
                                  pfl, pfl->name, chip_len);
605 368a354f Peter Crosthwaite
    vmstate_register_ram(&pfl->orig_mem, DEVICE(pfl));
606 cfe5f011 Avi Kivity
    pfl->storage = memory_region_get_ram_ptr(&pfl->orig_mem);
607 4fbd24ba balrog
    pfl->chip_len = chip_len;
608 29133e9a bellard
    if (pfl->bs) {
609 29133e9a bellard
        /* read the initial flash content */
610 d0e7605e Vijay Kumar
        ret = bdrv_read(pfl->bs, 0, pfl->storage, chip_len >> 9);
611 d0e7605e Vijay Kumar
        if (ret < 0) {
612 7267c094 Anthony Liguori
            g_free(pfl);
613 368a354f Peter Crosthwaite
            return 1;
614 d0e7605e Vijay Kumar
        }
615 29133e9a bellard
    }
616 de8efe8f Jordan Justen
617 cfe5f011 Avi Kivity
    pflash_setup_mappings(pfl);
618 cfe5f011 Avi Kivity
    pfl->rom_mode = 1;
619 368a354f Peter Crosthwaite
    sysbus_init_mmio(dev, &pfl->mem);
620 de8efe8f Jordan Justen
621 de8efe8f Jordan Justen
    if (pfl->bs) {
622 de8efe8f Jordan Justen
        pfl->ro = bdrv_is_read_only(pfl->bs);
623 de8efe8f Jordan Justen
    } else {
624 de8efe8f Jordan Justen
        pfl->ro = 0;
625 de8efe8f Jordan Justen
    }
626 de8efe8f Jordan Justen
627 74475455 Paolo Bonzini
    pfl->timer = qemu_new_timer_ns(vm_clock, pflash_timer, pfl);
628 29133e9a bellard
    pfl->wcycle = 0;
629 29133e9a bellard
    pfl->cmd = 0;
630 29133e9a bellard
    pfl->status = 0;
631 29133e9a bellard
    /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
632 29133e9a bellard
    pfl->cfi_len = 0x52;
633 29133e9a bellard
    /* Standard "QRY" string */
634 29133e9a bellard
    pfl->cfi_table[0x10] = 'Q';
635 29133e9a bellard
    pfl->cfi_table[0x11] = 'R';
636 29133e9a bellard
    pfl->cfi_table[0x12] = 'Y';
637 29133e9a bellard
    /* Command set (AMD/Fujitsu) */
638 29133e9a bellard
    pfl->cfi_table[0x13] = 0x02;
639 29133e9a bellard
    pfl->cfi_table[0x14] = 0x00;
640 78556820 edgar_igl
    /* Primary extended table address */
641 78556820 edgar_igl
    pfl->cfi_table[0x15] = 0x31;
642 29133e9a bellard
    pfl->cfi_table[0x16] = 0x00;
643 29133e9a bellard
    /* Alternate command set (none) */
644 29133e9a bellard
    pfl->cfi_table[0x17] = 0x00;
645 29133e9a bellard
    pfl->cfi_table[0x18] = 0x00;
646 29133e9a bellard
    /* Alternate extended table (none) */
647 29133e9a bellard
    pfl->cfi_table[0x19] = 0x00;
648 29133e9a bellard
    pfl->cfi_table[0x1A] = 0x00;
649 29133e9a bellard
    /* Vcc min */
650 29133e9a bellard
    pfl->cfi_table[0x1B] = 0x27;
651 29133e9a bellard
    /* Vcc max */
652 29133e9a bellard
    pfl->cfi_table[0x1C] = 0x36;
653 29133e9a bellard
    /* Vpp min (no Vpp pin) */
654 29133e9a bellard
    pfl->cfi_table[0x1D] = 0x00;
655 29133e9a bellard
    /* Vpp max (no Vpp pin) */
656 29133e9a bellard
    pfl->cfi_table[0x1E] = 0x00;
657 29133e9a bellard
    /* Reserved */
658 29133e9a bellard
    pfl->cfi_table[0x1F] = 0x07;
659 78556820 edgar_igl
    /* Timeout for min size buffer write (NA) */
660 78556820 edgar_igl
    pfl->cfi_table[0x20] = 0x00;
661 29133e9a bellard
    /* Typical timeout for block erase (512 ms) */
662 29133e9a bellard
    pfl->cfi_table[0x21] = 0x09;
663 29133e9a bellard
    /* Typical timeout for full chip erase (4096 ms) */
664 29133e9a bellard
    pfl->cfi_table[0x22] = 0x0C;
665 29133e9a bellard
    /* Reserved */
666 29133e9a bellard
    pfl->cfi_table[0x23] = 0x01;
667 78556820 edgar_igl
    /* Max timeout for buffer write (NA) */
668 78556820 edgar_igl
    pfl->cfi_table[0x24] = 0x00;
669 29133e9a bellard
    /* Max timeout for block erase */
670 29133e9a bellard
    pfl->cfi_table[0x25] = 0x0A;
671 29133e9a bellard
    /* Max timeout for chip erase */
672 29133e9a bellard
    pfl->cfi_table[0x26] = 0x0D;
673 29133e9a bellard
    /* Device size */
674 78556820 edgar_igl
    pfl->cfi_table[0x27] = ctz32(chip_len);
675 29133e9a bellard
    /* Flash device interface (8 & 16 bits) */
676 29133e9a bellard
    pfl->cfi_table[0x28] = 0x02;
677 29133e9a bellard
    pfl->cfi_table[0x29] = 0x00;
678 29133e9a bellard
    /* Max number of bytes in multi-bytes write */
679 95d1f3ed j_mayer
    /* XXX: disable buffered write as it's not supported */
680 95d1f3ed j_mayer
    //    pfl->cfi_table[0x2A] = 0x05;
681 95d1f3ed j_mayer
    pfl->cfi_table[0x2A] = 0x00;
682 29133e9a bellard
    pfl->cfi_table[0x2B] = 0x00;
683 29133e9a bellard
    /* Number of erase block regions (uniform) */
684 29133e9a bellard
    pfl->cfi_table[0x2C] = 0x01;
685 29133e9a bellard
    /* Erase block region 1 */
686 368a354f Peter Crosthwaite
    pfl->cfi_table[0x2D] = pfl->nb_blocs - 1;
687 368a354f Peter Crosthwaite
    pfl->cfi_table[0x2E] = (pfl->nb_blocs - 1) >> 8;
688 368a354f Peter Crosthwaite
    pfl->cfi_table[0x2F] = pfl->sector_len >> 8;
689 368a354f Peter Crosthwaite
    pfl->cfi_table[0x30] = pfl->sector_len >> 16;
690 29133e9a bellard
691 78556820 edgar_igl
    /* Extended */
692 78556820 edgar_igl
    pfl->cfi_table[0x31] = 'P';
693 78556820 edgar_igl
    pfl->cfi_table[0x32] = 'R';
694 78556820 edgar_igl
    pfl->cfi_table[0x33] = 'I';
695 78556820 edgar_igl
696 78556820 edgar_igl
    pfl->cfi_table[0x34] = '1';
697 78556820 edgar_igl
    pfl->cfi_table[0x35] = '0';
698 78556820 edgar_igl
699 78556820 edgar_igl
    pfl->cfi_table[0x36] = 0x00;
700 78556820 edgar_igl
    pfl->cfi_table[0x37] = 0x00;
701 78556820 edgar_igl
    pfl->cfi_table[0x38] = 0x00;
702 78556820 edgar_igl
    pfl->cfi_table[0x39] = 0x00;
703 78556820 edgar_igl
704 78556820 edgar_igl
    pfl->cfi_table[0x3a] = 0x00;
705 78556820 edgar_igl
706 78556820 edgar_igl
    pfl->cfi_table[0x3b] = 0x00;
707 78556820 edgar_igl
    pfl->cfi_table[0x3c] = 0x00;
708 78556820 edgar_igl
709 368a354f Peter Crosthwaite
    return 0;
710 368a354f Peter Crosthwaite
}
711 368a354f Peter Crosthwaite
712 368a354f Peter Crosthwaite
static Property pflash_cfi02_properties[] = {
713 368a354f Peter Crosthwaite
    DEFINE_PROP_DRIVE("drive", struct pflash_t, bs),
714 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT32("num-blocks", struct pflash_t, nb_blocs, 0),
715 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT32("sector-length", struct pflash_t, sector_len, 0),
716 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT8("width", struct pflash_t, width, 0),
717 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT8("mappings", struct pflash_t, mappings, 0),
718 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT8("big-endian", struct pflash_t, be, 0),
719 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT16("id0", struct pflash_t, ident0, 0),
720 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT16("id1", struct pflash_t, ident1, 0),
721 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT16("id2", struct pflash_t, ident2, 0),
722 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT16("id3", struct pflash_t, ident3, 0),
723 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT16("unlock-addr0", struct pflash_t, unlock_addr0, 0),
724 368a354f Peter Crosthwaite
    DEFINE_PROP_UINT16("unlock-addr1", struct pflash_t, unlock_addr1, 0),
725 368a354f Peter Crosthwaite
    DEFINE_PROP_STRING("name", struct pflash_t, name),
726 368a354f Peter Crosthwaite
    DEFINE_PROP_END_OF_LIST(),
727 368a354f Peter Crosthwaite
};
728 368a354f Peter Crosthwaite
729 368a354f Peter Crosthwaite
static void pflash_cfi02_class_init(ObjectClass *klass, void *data)
730 368a354f Peter Crosthwaite
{
731 368a354f Peter Crosthwaite
    DeviceClass *dc = DEVICE_CLASS(klass);
732 368a354f Peter Crosthwaite
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
733 368a354f Peter Crosthwaite
734 368a354f Peter Crosthwaite
    k->init = pflash_cfi02_init;
735 368a354f Peter Crosthwaite
    dc->props = pflash_cfi02_properties;
736 368a354f Peter Crosthwaite
}
737 368a354f Peter Crosthwaite
738 368a354f Peter Crosthwaite
static const TypeInfo pflash_cfi02_info = {
739 368a354f Peter Crosthwaite
    .name           = "cfi.pflash02",
740 368a354f Peter Crosthwaite
    .parent         = TYPE_SYS_BUS_DEVICE,
741 368a354f Peter Crosthwaite
    .instance_size  = sizeof(struct pflash_t),
742 368a354f Peter Crosthwaite
    .class_init     = pflash_cfi02_class_init,
743 368a354f Peter Crosthwaite
};
744 368a354f Peter Crosthwaite
745 368a354f Peter Crosthwaite
static void pflash_cfi02_register_types(void)
746 368a354f Peter Crosthwaite
{
747 368a354f Peter Crosthwaite
    type_register_static(&pflash_cfi02_info);
748 368a354f Peter Crosthwaite
}
749 368a354f Peter Crosthwaite
750 368a354f Peter Crosthwaite
type_init(pflash_cfi02_register_types)
751 368a354f Peter Crosthwaite
752 368a354f Peter Crosthwaite
pflash_t *pflash_cfi02_register(hwaddr base,
753 368a354f Peter Crosthwaite
                                DeviceState *qdev, const char *name,
754 368a354f Peter Crosthwaite
                                hwaddr size,
755 368a354f Peter Crosthwaite
                                BlockDriverState *bs, uint32_t sector_len,
756 368a354f Peter Crosthwaite
                                int nb_blocs, int nb_mappings, int width,
757 368a354f Peter Crosthwaite
                                uint16_t id0, uint16_t id1,
758 368a354f Peter Crosthwaite
                                uint16_t id2, uint16_t id3,
759 368a354f Peter Crosthwaite
                                uint16_t unlock_addr0, uint16_t unlock_addr1,
760 368a354f Peter Crosthwaite
                                int be)
761 368a354f Peter Crosthwaite
{
762 368a354f Peter Crosthwaite
    DeviceState *dev = qdev_create(NULL, "cfi.pflash02");
763 368a354f Peter Crosthwaite
    SysBusDevice *busdev = sysbus_from_qdev(dev);
764 368a354f Peter Crosthwaite
    pflash_t *pfl = (pflash_t *)object_dynamic_cast(OBJECT(dev),
765 368a354f Peter Crosthwaite
                                                    "cfi.pflash02");
766 368a354f Peter Crosthwaite
767 368a354f Peter Crosthwaite
    if (bs && qdev_prop_set_drive(dev, "drive", bs)) {
768 368a354f Peter Crosthwaite
        abort();
769 368a354f Peter Crosthwaite
    }
770 368a354f Peter Crosthwaite
    qdev_prop_set_uint32(dev, "num-blocks", nb_blocs);
771 368a354f Peter Crosthwaite
    qdev_prop_set_uint32(dev, "sector-length", sector_len);
772 368a354f Peter Crosthwaite
    qdev_prop_set_uint8(dev, "width", width);
773 368a354f Peter Crosthwaite
    qdev_prop_set_uint8(dev, "mappings", nb_mappings);
774 368a354f Peter Crosthwaite
    qdev_prop_set_uint8(dev, "big-endian", !!be);
775 368a354f Peter Crosthwaite
    qdev_prop_set_uint16(dev, "id0", id0);
776 368a354f Peter Crosthwaite
    qdev_prop_set_uint16(dev, "id1", id1);
777 368a354f Peter Crosthwaite
    qdev_prop_set_uint16(dev, "id2", id2);
778 368a354f Peter Crosthwaite
    qdev_prop_set_uint16(dev, "id3", id3);
779 368a354f Peter Crosthwaite
    qdev_prop_set_uint16(dev, "unlock-addr0", unlock_addr0);
780 368a354f Peter Crosthwaite
    qdev_prop_set_uint16(dev, "unlock-addr1", unlock_addr1);
781 368a354f Peter Crosthwaite
    qdev_prop_set_string(dev, "name", name);
782 368a354f Peter Crosthwaite
    qdev_init_nofail(dev);
783 368a354f Peter Crosthwaite
784 368a354f Peter Crosthwaite
    sysbus_mmio_map(busdev, 0, base);
785 29133e9a bellard
    return pfl;
786 29133e9a bellard
}