Statistics
| Branch: | Revision:

root / hw / pflash_cfi01.c @ 8da3ff18

History | View | Annotate | Download (16.9 kB)

1
/*
2
 *  CFI parallel flash with Intel command set emulation
3
 *
4
 *  Copyright (c) 2006 Thorsten Zitterell
5
 *  Copyright (c) 2005 Jocelyn Mayer
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21

    
22
/*
23
 * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
24
 * Supported commands/modes are:
25
 * - flash read
26
 * - flash write
27
 * - flash ID read
28
 * - sector erase
29
 * - CFI queries
30
 *
31
 * It does not support timings
32
 * It does not support flash interleaving
33
 * It does not implement software data protection as found in many real chips
34
 * It does not implement erase suspend/resume commands
35
 * It does not implement multiple sectors erase
36
 *
37
 * It does not implement much more ...
38
 */
39

    
40
#include "hw.h"
41
#include "flash.h"
42
#include "block.h"
43
#include "qemu-timer.h"
44

    
45
#define PFLASH_BUG(fmt, args...) \
46
do { \
47
    printf("PFLASH: Possible BUG - " fmt, ##args); \
48
    exit(1); \
49
} while(0)
50

    
51
/* #define PFLASH_DEBUG */
52
#ifdef PFLASH_DEBUG
53
#define DPRINTF(fmt, args...)                      \
54
do {                                               \
55
        printf("PFLASH: " fmt , ##args);           \
56
} while (0)
57
#else
58
#define DPRINTF(fmt, args...) do { } while (0)
59
#endif
60

    
61
struct pflash_t {
62
    BlockDriverState *bs;
63
    target_ulong base;
64
    target_ulong sector_len;
65
    target_ulong total_len;
66
    int width;
67
    int wcycle; /* if 0, the flash is read normally */
68
    int bypass;
69
    int ro;
70
    uint8_t cmd;
71
    uint8_t status;
72
    uint16_t ident[4];
73
    uint8_t cfi_len;
74
    uint8_t cfi_table[0x52];
75
    target_ulong counter;
76
    QEMUTimer *timer;
77
    ram_addr_t off;
78
    int fl_mem;
79
    void *storage;
80
};
81

    
82
static void pflash_timer (void *opaque)
83
{
84
    pflash_t *pfl = opaque;
85

    
86
    DPRINTF("%s: command %02x done\n", __func__, pfl->cmd);
87
    /* Reset flash */
88
    pfl->status ^= 0x80;
89
    if (pfl->bypass) {
90
        pfl->wcycle = 2;
91
    } else {
92
        cpu_register_physical_memory(pfl->base, pfl->total_len,
93
                        pfl->off | IO_MEM_ROMD | pfl->fl_mem);
94
        pfl->wcycle = 0;
95
    }
96
    pfl->cmd = 0;
97
}
98

    
99
static uint32_t pflash_read (pflash_t *pfl, target_ulong offset, int width)
100
{
101
    target_ulong boff;
102
    uint32_t ret;
103
    uint8_t *p;
104

    
105
    ret = -1;
106
    boff = offset & 0xFF; /* why this here ?? */
107

    
108
    if (pfl->width == 2)
109
        boff = boff >> 1;
110
    else if (pfl->width == 4)
111
        boff = boff >> 2;
112

    
113
    DPRINTF("%s: reading offset " TARGET_FMT_lx " under cmd %02x width %d\n",
114
            __func__, offset, pfl->cmd, width);
115

    
116
    switch (pfl->cmd) {
117
    case 0x00:
118
        /* Flash area read */
119
        p = pfl->storage;
120
        switch (width) {
121
        case 1:
122
            ret = p[offset];
123
            DPRINTF("%s: data offset " TARGET_FMT_lx " %02x\n",
124
                    __func__, offset, ret);
125
            break;
126
        case 2:
127
#if defined(TARGET_WORDS_BIGENDIAN)
128
            ret = p[offset] << 8;
129
            ret |= p[offset + 1];
130
#else
131
            ret = p[offset];
132
            ret |= p[offset + 1] << 8;
133
#endif
134
            DPRINTF("%s: data offset " TARGET_FMT_lx " %04x\n",
135
                    __func__, offset, ret);
136
            break;
137
        case 4:
138
#if defined(TARGET_WORDS_BIGENDIAN)
139
            ret = p[offset] << 24;
140
            ret |= p[offset + 1] << 16;
141
            ret |= p[offset + 2] << 8;
142
            ret |= p[offset + 3];
143
#else
144
            ret = p[offset];
145
            ret |= p[offset + 1] << 8;
146
            ret |= p[offset + 1] << 8;
147
            ret |= p[offset + 2] << 16;
148
            ret |= p[offset + 3] << 24;
149
#endif
150
            DPRINTF("%s: data offset " TARGET_FMT_lx " %08x\n",
151
                    __func__, offset, ret);
152
            break;
153
        default:
154
            DPRINTF("BUG in %s\n", __func__);
155
        }
156

    
157
        break;
158
    case 0x20: /* Block erase */
159
    case 0x50: /* Clear status register */
160
    case 0x60: /* Block /un)lock */
161
    case 0x70: /* Status Register */
162
    case 0xe8: /* Write block */
163
        /* Status register read */
164
        ret = pfl->status;
165
        DPRINTF("%s: status %x\n", __func__, ret);
166
        break;
167
    case 0x98: /* Query mode */
168
        if (boff > pfl->cfi_len)
169
            ret = 0;
170
        else
171
            ret = pfl->cfi_table[boff];
172
        break;
173
    default:
174
        /* This should never happen : reset state & treat it as a read */
175
        DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
176
        pfl->wcycle = 0;
177
        pfl->cmd = 0;
178
    }
179
    return ret;
180
}
181

    
182
/* update flash content on disk */
183
static void pflash_update(pflash_t *pfl, int offset,
184
                          int size)
185
{
186
    int offset_end;
187
    if (pfl->bs) {
188
        offset_end = offset + size;
189
        /* round to sectors */
190
        offset = offset >> 9;
191
        offset_end = (offset_end + 511) >> 9;
192
        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
193
                   offset_end - offset);
194
    }
195
}
196

    
197
static void pflash_write (pflash_t *pfl, target_ulong offset, uint32_t value,
198
                          int width)
199
{
200
    target_ulong boff;
201
    uint8_t *p;
202
    uint8_t cmd;
203

    
204
    cmd = value;
205

    
206
    DPRINTF("%s: writing offset " TARGET_FMT_lx " value %08x width %d wcycle 0x%x\n",
207
            __func__, offset, value, width, pfl->wcycle);
208

    
209
    /* Set the device in I/O access mode */
210
    cpu_register_physical_memory(pfl->base, pfl->total_len, pfl->fl_mem);
211
    boff = offset & (pfl->sector_len - 1);
212

    
213
    if (pfl->width == 2)
214
        boff = boff >> 1;
215
    else if (pfl->width == 4)
216
        boff = boff >> 2;
217

    
218
    switch (pfl->wcycle) {
219
    case 0:
220
        /* read mode */
221
        switch (cmd) {
222
        case 0x00: /* ??? */
223
            goto reset_flash;
224
        case 0x20: /* Block erase */
225
            p = pfl->storage;
226
            offset &= ~(pfl->sector_len - 1);
227

    
228
            DPRINTF("%s: block erase at " TARGET_FMT_lx " bytes "
229
                    TARGET_FMT_lx "\n",
230
                    __func__, offset, pfl->sector_len);
231

    
232
            memset(p + offset, 0xff, pfl->sector_len);
233
            pflash_update(pfl, offset, pfl->sector_len);
234
            pfl->status |= 0x80; /* Ready! */
235
            break;
236
        case 0x50: /* Clear status bits */
237
            DPRINTF("%s: Clear status bits\n", __func__);
238
            pfl->status = 0x0;
239
            goto reset_flash;
240
        case 0x60: /* Block (un)lock */
241
            DPRINTF("%s: Block unlock\n", __func__);
242
            break;
243
        case 0x70: /* Status Register */
244
            DPRINTF("%s: Read status register\n", __func__);
245
            pfl->cmd = cmd;
246
            return;
247
        case 0x98: /* CFI query */
248
            DPRINTF("%s: CFI query\n", __func__);
249
            break;
250
        case 0xe8: /* Write to buffer */
251
            DPRINTF("%s: Write to buffer\n", __func__);
252
            pfl->status |= 0x80; /* Ready! */
253
            break;
254
        case 0xff: /* Read array mode */
255
            DPRINTF("%s: Read array mode\n", __func__);
256
            goto reset_flash;
257
        default:
258
            goto error_flash;
259
        }
260
        pfl->wcycle++;
261
        pfl->cmd = cmd;
262
        return;
263
    case 1:
264
        switch (pfl->cmd) {
265
        case 0x20: /* Block erase */
266
        case 0x28:
267
            if (cmd == 0xd0) { /* confirm */
268
                pfl->wcycle = 0;
269
                pfl->status |= 0x80;
270
            } else if (cmd == 0xff) { /* read array mode */
271
                goto reset_flash;
272
            } else
273
                goto error_flash;
274

    
275
            break;
276
        case 0xe8:
277
            DPRINTF("%s: block write of %x bytes\n", __func__, value);
278
            pfl->counter = value;
279
            pfl->wcycle++;
280
            break;
281
        case 0x60:
282
            if (cmd == 0xd0) {
283
                pfl->wcycle = 0;
284
                pfl->status |= 0x80;
285
            } else if (cmd == 0x01) {
286
                pfl->wcycle = 0;
287
                pfl->status |= 0x80;
288
            } else if (cmd == 0xff) {
289
                goto reset_flash;
290
            } else {
291
                DPRINTF("%s: Unknown (un)locking command\n", __func__);
292
                goto reset_flash;
293
            }
294
            break;
295
        case 0x98:
296
            if (cmd == 0xff) {
297
                goto reset_flash;
298
            } else {
299
                DPRINTF("%s: leaving query mode\n", __func__);
300
            }
301
            break;
302
        default:
303
            goto error_flash;
304
        }
305
        return;
306
    case 2:
307
        switch (pfl->cmd) {
308
        case 0xe8: /* Block write */
309
            p = pfl->storage;
310
            DPRINTF("%s: block write offset " TARGET_FMT_lx
311
                    " value %x counter " TARGET_FMT_lx "\n",
312
                    __func__, offset, value, pfl->counter);
313
            switch (width) {
314
            case 1:
315
                p[offset] = value;
316
                pflash_update(pfl, offset, 1);
317
                break;
318
            case 2:
319
#if defined(TARGET_WORDS_BIGENDIAN)
320
                p[offset] = value >> 8;
321
                p[offset + 1] = value;
322
#else
323
                p[offset] = value;
324
                p[offset + 1] = value >> 8;
325
#endif
326
                pflash_update(pfl, offset, 2);
327
                break;
328
            case 4:
329
#if defined(TARGET_WORDS_BIGENDIAN)
330
                p[offset] = value >> 24;
331
                p[offset + 1] = value >> 16;
332
                p[offset + 2] = value >> 8;
333
                p[offset + 3] = value;
334
#else
335
                p[offset] = value;
336
                p[offset + 1] = value >> 8;
337
                p[offset + 2] = value >> 16;
338
                p[offset + 3] = value >> 24;
339
#endif
340
                pflash_update(pfl, offset, 4);
341
                break;
342
            }
343

    
344
            pfl->status |= 0x80;
345

    
346
            if (!pfl->counter) {
347
                DPRINTF("%s: block write finished\n", __func__);
348
                pfl->wcycle++;
349
            }
350

    
351
            pfl->counter--;
352
            break;
353
        default:
354
            goto error_flash;
355
        }
356
        return;
357
    case 3: /* Confirm mode */
358
        switch (pfl->cmd) {
359
        case 0xe8: /* Block write */
360
            if (cmd == 0xd0) {
361
                pfl->wcycle = 0;
362
                pfl->status |= 0x80;
363
            } else {
364
                DPRINTF("%s: unknown command for \"write block\"\n", __func__);
365
                PFLASH_BUG("Write block confirm");
366
                goto reset_flash;
367
            }
368
            break;
369
        default:
370
            goto error_flash;
371
        }
372
        return;
373
    default:
374
        /* Should never happen */
375
        DPRINTF("%s: invalid write state\n",  __func__);
376
        goto reset_flash;
377
    }
378
    return;
379

    
380
 error_flash:
381
    printf("%s: Unimplemented flash cmd sequence "
382
           "(offset " TARGET_FMT_lx ", wcycle 0x%x cmd 0x%x value 0x%x)\n",
383
           __func__, offset, pfl->wcycle, pfl->cmd, value);
384

    
385
 reset_flash:
386
    cpu_register_physical_memory(pfl->base, pfl->total_len,
387
                    pfl->off | IO_MEM_ROMD | pfl->fl_mem);
388

    
389
    pfl->bypass = 0;
390
    pfl->wcycle = 0;
391
    pfl->cmd = 0;
392
    return;
393
}
394

    
395

    
396
static uint32_t pflash_readb (void *opaque, target_phys_addr_t addr)
397
{
398
    return pflash_read(opaque, addr, 1);
399
}
400

    
401
static uint32_t pflash_readw (void *opaque, target_phys_addr_t addr)
402
{
403
    pflash_t *pfl = opaque;
404

    
405
    return pflash_read(pfl, addr, 2);
406
}
407

    
408
static uint32_t pflash_readl (void *opaque, target_phys_addr_t addr)
409
{
410
    pflash_t *pfl = opaque;
411

    
412
    return pflash_read(pfl, addr, 4);
413
}
414

    
415
static void pflash_writeb (void *opaque, target_phys_addr_t addr,
416
                           uint32_t value)
417
{
418
    pflash_write(opaque, addr, value, 1);
419
}
420

    
421
static void pflash_writew (void *opaque, target_phys_addr_t addr,
422
                           uint32_t value)
423
{
424
    pflash_t *pfl = opaque;
425

    
426
    pflash_write(pfl, addr, value, 2);
427
}
428

    
429
static void pflash_writel (void *opaque, target_phys_addr_t addr,
430
                           uint32_t value)
431
{
432
    pflash_t *pfl = opaque;
433

    
434
    pflash_write(pfl, addr, value, 4);
435
}
436

    
437
static CPUWriteMemoryFunc *pflash_write_ops[] = {
438
    &pflash_writeb,
439
    &pflash_writew,
440
    &pflash_writel,
441
};
442

    
443
static CPUReadMemoryFunc *pflash_read_ops[] = {
444
    &pflash_readb,
445
    &pflash_readw,
446
    &pflash_readl,
447
};
448

    
449
/* Count trailing zeroes of a 32 bits quantity */
450
static int ctz32 (uint32_t n)
451
{
452
    int ret;
453

    
454
    ret = 0;
455
    if (!(n & 0xFFFF)) {
456
        ret += 16;
457
        n = n >> 16;
458
    }
459
    if (!(n & 0xFF)) {
460
        ret += 8;
461
        n = n >> 8;
462
    }
463
    if (!(n & 0xF)) {
464
        ret += 4;
465
        n = n >> 4;
466
    }
467
    if (!(n & 0x3)) {
468
        ret += 2;
469
        n = n >> 2;
470
    }
471
    if (!(n & 0x1)) {
472
        ret++;
473
        n = n >> 1;
474
    }
475
#if 0 /* This is not necessary as n is never 0 */
476
    if (!n)
477
        ret++;
478
#endif
479

    
480
    return ret;
481
}
482

    
483
pflash_t *pflash_cfi01_register(target_phys_addr_t base, ram_addr_t off,
484
                                BlockDriverState *bs, uint32_t sector_len,
485
                                int nb_blocs, int width,
486
                                uint16_t id0, uint16_t id1,
487
                                uint16_t id2, uint16_t id3)
488
{
489
    pflash_t *pfl;
490
    target_long total_len;
491

    
492
    total_len = sector_len * nb_blocs;
493

    
494
    /* XXX: to be fixed */
495
#if 0
496
    if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) &&
497
        total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024))
498
        return NULL;
499
#endif
500

    
501
    pfl = qemu_mallocz(sizeof(pflash_t));
502

    
503
    if (pfl == NULL)
504
        return NULL;
505
    pfl->storage = phys_ram_base + off;
506
    pfl->fl_mem = cpu_register_io_memory(0,
507
                    pflash_read_ops, pflash_write_ops, pfl);
508
    pfl->off = off;
509
    cpu_register_physical_memory(base, total_len,
510
                    off | pfl->fl_mem | IO_MEM_ROMD);
511

    
512
    pfl->bs = bs;
513
    if (pfl->bs) {
514
        /* read the initial flash content */
515
        bdrv_read(pfl->bs, 0, pfl->storage, total_len >> 9);
516
    }
517
#if 0 /* XXX: there should be a bit to set up read-only,
518
       *      the same way the hardware does (with WP pin).
519
       */
520
    pfl->ro = 1;
521
#else
522
    pfl->ro = 0;
523
#endif
524
    pfl->timer = qemu_new_timer(vm_clock, pflash_timer, pfl);
525
    pfl->base = base;
526
    pfl->sector_len = sector_len;
527
    pfl->total_len = total_len;
528
    pfl->width = width;
529
    pfl->wcycle = 0;
530
    pfl->cmd = 0;
531
    pfl->status = 0;
532
    pfl->ident[0] = id0;
533
    pfl->ident[1] = id1;
534
    pfl->ident[2] = id2;
535
    pfl->ident[3] = id3;
536
    /* Hardcoded CFI table */
537
    pfl->cfi_len = 0x52;
538
    /* Standard "QRY" string */
539
    pfl->cfi_table[0x10] = 'Q';
540
    pfl->cfi_table[0x11] = 'R';
541
    pfl->cfi_table[0x12] = 'Y';
542
    /* Command set (Intel) */
543
    pfl->cfi_table[0x13] = 0x01;
544
    pfl->cfi_table[0x14] = 0x00;
545
    /* Primary extended table address (none) */
546
    pfl->cfi_table[0x15] = 0x31;
547
    pfl->cfi_table[0x16] = 0x00;
548
    /* Alternate command set (none) */
549
    pfl->cfi_table[0x17] = 0x00;
550
    pfl->cfi_table[0x18] = 0x00;
551
    /* Alternate extended table (none) */
552
    pfl->cfi_table[0x19] = 0x00;
553
    pfl->cfi_table[0x1A] = 0x00;
554
    /* Vcc min */
555
    pfl->cfi_table[0x1B] = 0x45;
556
    /* Vcc max */
557
    pfl->cfi_table[0x1C] = 0x55;
558
    /* Vpp min (no Vpp pin) */
559
    pfl->cfi_table[0x1D] = 0x00;
560
    /* Vpp max (no Vpp pin) */
561
    pfl->cfi_table[0x1E] = 0x00;
562
    /* Reserved */
563
    pfl->cfi_table[0x1F] = 0x07;
564
    /* Timeout for min size buffer write */
565
    pfl->cfi_table[0x20] = 0x07;
566
    /* Typical timeout for block erase */
567
    pfl->cfi_table[0x21] = 0x0a;
568
    /* Typical timeout for full chip erase (4096 ms) */
569
    pfl->cfi_table[0x22] = 0x00;
570
    /* Reserved */
571
    pfl->cfi_table[0x23] = 0x04;
572
    /* Max timeout for buffer write */
573
    pfl->cfi_table[0x24] = 0x04;
574
    /* Max timeout for block erase */
575
    pfl->cfi_table[0x25] = 0x04;
576
    /* Max timeout for chip erase */
577
    pfl->cfi_table[0x26] = 0x00;
578
    /* Device size */
579
    pfl->cfi_table[0x27] = ctz32(total_len); // + 1;
580
    /* Flash device interface (8 & 16 bits) */
581
    pfl->cfi_table[0x28] = 0x02;
582
    pfl->cfi_table[0x29] = 0x00;
583
    /* Max number of bytes in multi-bytes write */
584
    pfl->cfi_table[0x2A] = 0x0B;
585
    pfl->cfi_table[0x2B] = 0x00;
586
    /* Number of erase block regions (uniform) */
587
    pfl->cfi_table[0x2C] = 0x01;
588
    /* Erase block region 1 */
589
    pfl->cfi_table[0x2D] = nb_blocs - 1;
590
    pfl->cfi_table[0x2E] = (nb_blocs - 1) >> 8;
591
    pfl->cfi_table[0x2F] = sector_len >> 8;
592
    pfl->cfi_table[0x30] = sector_len >> 16;
593

    
594
    /* Extended */
595
    pfl->cfi_table[0x31] = 'P';
596
    pfl->cfi_table[0x32] = 'R';
597
    pfl->cfi_table[0x33] = 'I';
598

    
599
    pfl->cfi_table[0x34] = '1';
600
    pfl->cfi_table[0x35] = '1';
601

    
602
    pfl->cfi_table[0x36] = 0x00;
603
    pfl->cfi_table[0x37] = 0x00;
604
    pfl->cfi_table[0x38] = 0x00;
605
    pfl->cfi_table[0x39] = 0x00;
606

    
607
    pfl->cfi_table[0x3a] = 0x00;
608

    
609
    pfl->cfi_table[0x3b] = 0x00;
610
    pfl->cfi_table[0x3c] = 0x00;
611

    
612
    return pfl;
613
}