2 * CFI parallel flash with AMD command set emulation
4 * Copyright (c) 2005 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
22 * Supported commands/modes are:
28 * - unlock bypass command
31 * It does not support flash interleaving.
32 * It does not implement boot blocs with reduced size
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
38 #include "qemu/osdep.h"
40 #include "hw/block/flash.h"
41 #include "qapi/error.h"
42 #include "qemu/timer.h"
43 #include "sysemu/block-backend.h"
44 #include "exec/address-spaces.h"
45 #include "qemu/host-utils.h"
46 #include "hw/sysbus.h"
48 //#define PFLASH_DEBUG
50 #define DPRINTF(fmt, ...) \
52 fprintf(stderr, "PFLASH: " fmt , ## __VA_ARGS__); \
55 #define DPRINTF(fmt, ...) do { } while (0)
58 #define PFLASH_LAZY_ROMD_THRESHOLD 42
60 #define CFI_PFLASH02(obj) OBJECT_CHECK(pflash_t, (obj), TYPE_CFI_PFLASH02)
64 SysBusDevice parent_obj
;
74 int wcycle
; /* if 0, the flash is read normally */
79 /* FIXME: implement array device properties */
84 uint16_t unlock_addr0
;
85 uint16_t unlock_addr1
;
87 uint8_t cfi_table
[0x52];
89 /* The device replicates the flash memory across its memory space. Emulate
90 * that by having a container (.mem) filled with an array of aliases
91 * (.mem_mappings) pointing to the flash memory (.orig_mem).
94 MemoryRegion
*mem_mappings
; /* array; one per mapping */
95 MemoryRegion orig_mem
;
97 int read_counter
; /* used for lazy switch-back to rom mode */
103 * Set up replicated mappings of the same region.
105 static void pflash_setup_mappings(pflash_t
*pfl
)
108 hwaddr size
= memory_region_size(&pfl
->orig_mem
);
110 memory_region_init(&pfl
->mem
, OBJECT(pfl
), "pflash", pfl
->mappings
* size
);
111 pfl
->mem_mappings
= g_new(MemoryRegion
, pfl
->mappings
);
112 for (i
= 0; i
< pfl
->mappings
; ++i
) {
113 memory_region_init_alias(&pfl
->mem_mappings
[i
], OBJECT(pfl
),
114 "pflash-alias", &pfl
->orig_mem
, 0, size
);
115 memory_region_add_subregion(&pfl
->mem
, i
* size
, &pfl
->mem_mappings
[i
]);
119 static void pflash_register_memory(pflash_t
*pfl
, int rom_mode
)
121 memory_region_rom_device_set_romd(&pfl
->orig_mem
, rom_mode
);
122 pfl
->rom_mode
= rom_mode
;
125 static void pflash_timer (void *opaque
)
127 pflash_t
*pfl
= opaque
;
129 DPRINTF("%s: command %02x done\n", __func__
, pfl
->cmd
);
135 pflash_register_memory(pfl
, 1);
141 static uint32_t pflash_read (pflash_t
*pfl
, hwaddr offset
,
148 DPRINTF("%s: offset " TARGET_FMT_plx
"\n", __func__
, offset
);
150 /* Lazy reset to ROMD mode after a certain amount of read accesses */
151 if (!pfl
->rom_mode
&& pfl
->wcycle
== 0 &&
152 ++pfl
->read_counter
> PFLASH_LAZY_ROMD_THRESHOLD
) {
153 pflash_register_memory(pfl
, 1);
155 offset
&= pfl
->chip_len
- 1;
156 boff
= offset
& 0xFF;
159 else if (pfl
->width
== 4)
163 /* This should never happen : reset state & treat it as a read*/
164 DPRINTF("%s: unknown command state: %x\n", __func__
, pfl
->cmd
);
167 /* fall through to the read code */
169 /* We accept reads during second unlock sequence... */
172 /* Flash area read */
177 // DPRINTF("%s: data offset %08x %02x\n", __func__, offset, ret);
181 ret
= p
[offset
] << 8;
182 ret
|= p
[offset
+ 1];
185 ret
|= p
[offset
+ 1] << 8;
187 // DPRINTF("%s: data offset %08x %04x\n", __func__, offset, ret);
191 ret
= p
[offset
] << 24;
192 ret
|= p
[offset
+ 1] << 16;
193 ret
|= p
[offset
+ 2] << 8;
194 ret
|= p
[offset
+ 3];
197 ret
|= p
[offset
+ 1] << 8;
198 ret
|= p
[offset
+ 2] << 16;
199 ret
|= p
[offset
+ 3] << 24;
201 // DPRINTF("%s: data offset %08x %08x\n", __func__, offset, ret);
210 ret
= boff
& 0x01 ? pfl
->ident1
: pfl
->ident0
;
213 ret
= 0x00; /* Pretend all sectors are unprotected */
217 ret
= boff
& 0x01 ? pfl
->ident3
: pfl
->ident2
;
218 if (ret
== (uint8_t)-1) {
225 DPRINTF("%s: ID " TARGET_FMT_plx
" %x\n", __func__
, boff
, ret
);
230 /* Status register read */
232 DPRINTF("%s: status %x\n", __func__
, ret
);
238 if (boff
> pfl
->cfi_len
)
241 ret
= pfl
->cfi_table
[boff
];
248 /* update flash content on disk */
249 static void pflash_update(pflash_t
*pfl
, int offset
,
254 offset_end
= offset
+ size
;
255 /* widen to sector boundaries */
256 offset
= QEMU_ALIGN_DOWN(offset
, BDRV_SECTOR_SIZE
);
257 offset_end
= QEMU_ALIGN_UP(offset_end
, BDRV_SECTOR_SIZE
);
258 blk_pwrite(pfl
->blk
, offset
, pfl
->storage
+ offset
,
259 offset_end
- offset
, 0);
263 static void pflash_write (pflash_t
*pfl
, hwaddr offset
,
264 uint32_t value
, int width
, int be
)
271 if (pfl
->cmd
!= 0xA0 && cmd
== 0xF0) {
273 DPRINTF("%s: flash reset asked (%02x %02x)\n",
274 __func__
, pfl
->cmd
, cmd
);
278 DPRINTF("%s: offset " TARGET_FMT_plx
" %08x %d %d\n", __func__
,
279 offset
, value
, width
, pfl
->wcycle
);
280 offset
&= pfl
->chip_len
- 1;
282 DPRINTF("%s: offset " TARGET_FMT_plx
" %08x %d\n", __func__
,
283 offset
, value
, width
);
284 boff
= offset
& (pfl
->sector_len
- 1);
287 else if (pfl
->width
== 4)
289 switch (pfl
->wcycle
) {
291 /* Set the device in I/O access mode if required */
293 pflash_register_memory(pfl
, 0);
294 pfl
->read_counter
= 0;
295 /* We're in read mode */
297 if (boff
== 0x55 && cmd
== 0x98) {
299 /* Enter CFI query mode */
304 if (boff
!= pfl
->unlock_addr0
|| cmd
!= 0xAA) {
305 DPRINTF("%s: unlock0 failed " TARGET_FMT_plx
" %02x %04x\n",
306 __func__
, boff
, cmd
, pfl
->unlock_addr0
);
309 DPRINTF("%s: unlock sequence started\n", __func__
);
312 /* We started an unlock sequence */
314 if (boff
!= pfl
->unlock_addr1
|| cmd
!= 0x55) {
315 DPRINTF("%s: unlock1 failed " TARGET_FMT_plx
" %02x\n", __func__
,
319 DPRINTF("%s: unlock sequence done\n", __func__
);
322 /* We finished an unlock sequence */
323 if (!pfl
->bypass
&& boff
!= pfl
->unlock_addr0
) {
324 DPRINTF("%s: command failed " TARGET_FMT_plx
" %02x\n", __func__
,
336 DPRINTF("%s: starting command %02x\n", __func__
, cmd
);
339 DPRINTF("%s: unknown command %02x\n", __func__
, cmd
);
346 /* We need another unlock sequence */
349 DPRINTF("%s: write data offset " TARGET_FMT_plx
" %08x %d\n",
350 __func__
, offset
, value
, width
);
356 pflash_update(pfl
, offset
, 1);
360 p
[offset
] &= value
>> 8;
361 p
[offset
+ 1] &= value
;
364 p
[offset
+ 1] &= value
>> 8;
366 pflash_update(pfl
, offset
, 2);
370 p
[offset
] &= value
>> 24;
371 p
[offset
+ 1] &= value
>> 16;
372 p
[offset
+ 2] &= value
>> 8;
373 p
[offset
+ 3] &= value
;
376 p
[offset
+ 1] &= value
>> 8;
377 p
[offset
+ 2] &= value
>> 16;
378 p
[offset
+ 3] &= value
>> 24;
380 pflash_update(pfl
, offset
, 4);
384 pfl
->status
= 0x00 | ~(value
& 0x80);
385 /* Let's pretend write is immediate */
390 if (pfl
->bypass
&& cmd
== 0x00) {
391 /* Unlock bypass reset */
394 /* We can enter CFI query mode from autoselect mode */
395 if (boff
== 0x55 && cmd
== 0x98)
399 DPRINTF("%s: invalid write for command %02x\n",
406 /* Ignore writes while flash data write is occurring */
407 /* As we suppose write is immediate, this should never happen */
412 /* Should never happen */
413 DPRINTF("%s: invalid command state %02x (wc 4)\n",
421 if (boff
!= pfl
->unlock_addr0
) {
422 DPRINTF("%s: chip erase: invalid address " TARGET_FMT_plx
"\n",
427 DPRINTF("%s: start chip erase\n", __func__
);
429 memset(pfl
->storage
, 0xFF, pfl
->chip_len
);
430 pflash_update(pfl
, 0, pfl
->chip_len
);
433 /* Let's wait 5 seconds before chip erase is done */
434 timer_mod(pfl
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
435 (NANOSECONDS_PER_SECOND
* 5));
440 offset
&= ~(pfl
->sector_len
- 1);
441 DPRINTF("%s: start sector erase at " TARGET_FMT_plx
"\n", __func__
,
444 memset(p
+ offset
, 0xFF, pfl
->sector_len
);
445 pflash_update(pfl
, offset
, pfl
->sector_len
);
448 /* Let's wait 1/2 second before sector erase is done */
449 timer_mod(pfl
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
450 (NANOSECONDS_PER_SECOND
/ 2));
453 DPRINTF("%s: invalid command %02x (wc 5)\n", __func__
, cmd
);
461 /* Ignore writes during chip erase */
464 /* Ignore writes during sector erase */
467 /* Should never happen */
468 DPRINTF("%s: invalid command state %02x (wc 6)\n",
473 case 7: /* Special value for CFI queries */
474 DPRINTF("%s: invalid write in CFI query mode\n", __func__
);
477 /* Should never happen */
478 DPRINTF("%s: invalid write state (wc 7)\n", __func__
);
498 static uint32_t pflash_readb_be(void *opaque
, hwaddr addr
)
500 return pflash_read(opaque
, addr
, 1, 1);
503 static uint32_t pflash_readb_le(void *opaque
, hwaddr addr
)
505 return pflash_read(opaque
, addr
, 1, 0);
508 static uint32_t pflash_readw_be(void *opaque
, hwaddr addr
)
510 pflash_t
*pfl
= opaque
;
512 return pflash_read(pfl
, addr
, 2, 1);
515 static uint32_t pflash_readw_le(void *opaque
, hwaddr addr
)
517 pflash_t
*pfl
= opaque
;
519 return pflash_read(pfl
, addr
, 2, 0);
522 static uint32_t pflash_readl_be(void *opaque
, hwaddr addr
)
524 pflash_t
*pfl
= opaque
;
526 return pflash_read(pfl
, addr
, 4, 1);
529 static uint32_t pflash_readl_le(void *opaque
, hwaddr addr
)
531 pflash_t
*pfl
= opaque
;
533 return pflash_read(pfl
, addr
, 4, 0);
536 static void pflash_writeb_be(void *opaque
, hwaddr addr
,
539 pflash_write(opaque
, addr
, value
, 1, 1);
542 static void pflash_writeb_le(void *opaque
, hwaddr addr
,
545 pflash_write(opaque
, addr
, value
, 1, 0);
548 static void pflash_writew_be(void *opaque
, hwaddr addr
,
551 pflash_t
*pfl
= opaque
;
553 pflash_write(pfl
, addr
, value
, 2, 1);
556 static void pflash_writew_le(void *opaque
, hwaddr addr
,
559 pflash_t
*pfl
= opaque
;
561 pflash_write(pfl
, addr
, value
, 2, 0);
564 static void pflash_writel_be(void *opaque
, hwaddr addr
,
567 pflash_t
*pfl
= opaque
;
569 pflash_write(pfl
, addr
, value
, 4, 1);
572 static void pflash_writel_le(void *opaque
, hwaddr addr
,
575 pflash_t
*pfl
= opaque
;
577 pflash_write(pfl
, addr
, value
, 4, 0);
580 static const MemoryRegionOps pflash_cfi02_ops_be
= {
582 .read
= { pflash_readb_be
, pflash_readw_be
, pflash_readl_be
, },
583 .write
= { pflash_writeb_be
, pflash_writew_be
, pflash_writel_be
, },
585 .endianness
= DEVICE_NATIVE_ENDIAN
,
588 static const MemoryRegionOps pflash_cfi02_ops_le
= {
590 .read
= { pflash_readb_le
, pflash_readw_le
, pflash_readl_le
, },
591 .write
= { pflash_writeb_le
, pflash_writew_le
, pflash_writel_le
, },
593 .endianness
= DEVICE_NATIVE_ENDIAN
,
596 static void pflash_cfi02_realize(DeviceState
*dev
, Error
**errp
)
598 pflash_t
*pfl
= CFI_PFLASH02(dev
);
601 Error
*local_err
= NULL
;
603 if (pfl
->sector_len
== 0) {
604 error_setg(errp
, "attribute \"sector-length\" not specified or zero.");
607 if (pfl
->nb_blocs
== 0) {
608 error_setg(errp
, "attribute \"num-blocks\" not specified or zero.");
611 if (pfl
->name
== NULL
) {
612 error_setg(errp
, "attribute \"name\" not specified.");
616 chip_len
= pfl
->sector_len
* pfl
->nb_blocs
;
617 /* XXX: to be fixed */
619 if (total_len
!= (8 * 1024 * 1024) && total_len
!= (16 * 1024 * 1024) &&
620 total_len
!= (32 * 1024 * 1024) && total_len
!= (64 * 1024 * 1024))
624 memory_region_init_rom_device(&pfl
->orig_mem
, OBJECT(pfl
), pfl
->be
?
625 &pflash_cfi02_ops_be
: &pflash_cfi02_ops_le
,
626 pfl
, pfl
->name
, chip_len
, &local_err
);
628 error_propagate(errp
, local_err
);
632 vmstate_register_ram(&pfl
->orig_mem
, DEVICE(pfl
));
633 pfl
->storage
= memory_region_get_ram_ptr(&pfl
->orig_mem
);
634 pfl
->chip_len
= chip_len
;
638 pfl
->ro
= blk_is_read_only(pfl
->blk
);
639 perm
= BLK_PERM_CONSISTENT_READ
| (pfl
->ro
? 0 : BLK_PERM_WRITE
);
640 ret
= blk_set_perm(pfl
->blk
, perm
, BLK_PERM_ALL
, errp
);
649 /* read the initial flash content */
650 ret
= blk_pread(pfl
->blk
, 0, pfl
->storage
, chip_len
);
652 vmstate_unregister_ram(&pfl
->orig_mem
, DEVICE(pfl
));
653 error_setg(errp
, "failed to read the initial flash content");
658 pflash_setup_mappings(pfl
);
660 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &pfl
->mem
);
662 pfl
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, pflash_timer
, pfl
);
666 /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
668 /* Standard "QRY" string */
669 pfl
->cfi_table
[0x10] = 'Q';
670 pfl
->cfi_table
[0x11] = 'R';
671 pfl
->cfi_table
[0x12] = 'Y';
672 /* Command set (AMD/Fujitsu) */
673 pfl
->cfi_table
[0x13] = 0x02;
674 pfl
->cfi_table
[0x14] = 0x00;
675 /* Primary extended table address */
676 pfl
->cfi_table
[0x15] = 0x31;
677 pfl
->cfi_table
[0x16] = 0x00;
678 /* Alternate command set (none) */
679 pfl
->cfi_table
[0x17] = 0x00;
680 pfl
->cfi_table
[0x18] = 0x00;
681 /* Alternate extended table (none) */
682 pfl
->cfi_table
[0x19] = 0x00;
683 pfl
->cfi_table
[0x1A] = 0x00;
685 pfl
->cfi_table
[0x1B] = 0x27;
687 pfl
->cfi_table
[0x1C] = 0x36;
688 /* Vpp min (no Vpp pin) */
689 pfl
->cfi_table
[0x1D] = 0x00;
690 /* Vpp max (no Vpp pin) */
691 pfl
->cfi_table
[0x1E] = 0x00;
693 pfl
->cfi_table
[0x1F] = 0x07;
694 /* Timeout for min size buffer write (NA) */
695 pfl
->cfi_table
[0x20] = 0x00;
696 /* Typical timeout for block erase (512 ms) */
697 pfl
->cfi_table
[0x21] = 0x09;
698 /* Typical timeout for full chip erase (4096 ms) */
699 pfl
->cfi_table
[0x22] = 0x0C;
701 pfl
->cfi_table
[0x23] = 0x01;
702 /* Max timeout for buffer write (NA) */
703 pfl
->cfi_table
[0x24] = 0x00;
704 /* Max timeout for block erase */
705 pfl
->cfi_table
[0x25] = 0x0A;
706 /* Max timeout for chip erase */
707 pfl
->cfi_table
[0x26] = 0x0D;
709 pfl
->cfi_table
[0x27] = ctz32(chip_len
);
710 /* Flash device interface (8 & 16 bits) */
711 pfl
->cfi_table
[0x28] = 0x02;
712 pfl
->cfi_table
[0x29] = 0x00;
713 /* Max number of bytes in multi-bytes write */
714 /* XXX: disable buffered write as it's not supported */
715 // pfl->cfi_table[0x2A] = 0x05;
716 pfl
->cfi_table
[0x2A] = 0x00;
717 pfl
->cfi_table
[0x2B] = 0x00;
718 /* Number of erase block regions (uniform) */
719 pfl
->cfi_table
[0x2C] = 0x01;
720 /* Erase block region 1 */
721 pfl
->cfi_table
[0x2D] = pfl
->nb_blocs
- 1;
722 pfl
->cfi_table
[0x2E] = (pfl
->nb_blocs
- 1) >> 8;
723 pfl
->cfi_table
[0x2F] = pfl
->sector_len
>> 8;
724 pfl
->cfi_table
[0x30] = pfl
->sector_len
>> 16;
727 pfl
->cfi_table
[0x31] = 'P';
728 pfl
->cfi_table
[0x32] = 'R';
729 pfl
->cfi_table
[0x33] = 'I';
731 pfl
->cfi_table
[0x34] = '1';
732 pfl
->cfi_table
[0x35] = '0';
734 pfl
->cfi_table
[0x36] = 0x00;
735 pfl
->cfi_table
[0x37] = 0x00;
736 pfl
->cfi_table
[0x38] = 0x00;
737 pfl
->cfi_table
[0x39] = 0x00;
739 pfl
->cfi_table
[0x3a] = 0x00;
741 pfl
->cfi_table
[0x3b] = 0x00;
742 pfl
->cfi_table
[0x3c] = 0x00;
745 static Property pflash_cfi02_properties
[] = {
746 DEFINE_PROP_DRIVE("drive", struct pflash_t
, blk
),
747 DEFINE_PROP_UINT32("num-blocks", struct pflash_t
, nb_blocs
, 0),
748 DEFINE_PROP_UINT32("sector-length", struct pflash_t
, sector_len
, 0),
749 DEFINE_PROP_UINT8("width", struct pflash_t
, width
, 0),
750 DEFINE_PROP_UINT8("mappings", struct pflash_t
, mappings
, 0),
751 DEFINE_PROP_UINT8("big-endian", struct pflash_t
, be
, 0),
752 DEFINE_PROP_UINT16("id0", struct pflash_t
, ident0
, 0),
753 DEFINE_PROP_UINT16("id1", struct pflash_t
, ident1
, 0),
754 DEFINE_PROP_UINT16("id2", struct pflash_t
, ident2
, 0),
755 DEFINE_PROP_UINT16("id3", struct pflash_t
, ident3
, 0),
756 DEFINE_PROP_UINT16("unlock-addr0", struct pflash_t
, unlock_addr0
, 0),
757 DEFINE_PROP_UINT16("unlock-addr1", struct pflash_t
, unlock_addr1
, 0),
758 DEFINE_PROP_STRING("name", struct pflash_t
, name
),
759 DEFINE_PROP_END_OF_LIST(),
762 static void pflash_cfi02_class_init(ObjectClass
*klass
, void *data
)
764 DeviceClass
*dc
= DEVICE_CLASS(klass
);
766 dc
->realize
= pflash_cfi02_realize
;
767 dc
->props
= pflash_cfi02_properties
;
768 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
771 static const TypeInfo pflash_cfi02_info
= {
772 .name
= TYPE_CFI_PFLASH02
,
773 .parent
= TYPE_SYS_BUS_DEVICE
,
774 .instance_size
= sizeof(struct pflash_t
),
775 .class_init
= pflash_cfi02_class_init
,
778 static void pflash_cfi02_register_types(void)
780 type_register_static(&pflash_cfi02_info
);
783 type_init(pflash_cfi02_register_types
)
785 pflash_t
*pflash_cfi02_register(hwaddr base
,
786 DeviceState
*qdev
, const char *name
,
788 BlockBackend
*blk
, uint32_t sector_len
,
789 int nb_blocs
, int nb_mappings
, int width
,
790 uint16_t id0
, uint16_t id1
,
791 uint16_t id2
, uint16_t id3
,
792 uint16_t unlock_addr0
, uint16_t unlock_addr1
,
795 DeviceState
*dev
= qdev_create(NULL
, TYPE_CFI_PFLASH02
);
798 qdev_prop_set_drive(dev
, "drive", blk
, &error_abort
);
800 qdev_prop_set_uint32(dev
, "num-blocks", nb_blocs
);
801 qdev_prop_set_uint32(dev
, "sector-length", sector_len
);
802 qdev_prop_set_uint8(dev
, "width", width
);
803 qdev_prop_set_uint8(dev
, "mappings", nb_mappings
);
804 qdev_prop_set_uint8(dev
, "big-endian", !!be
);
805 qdev_prop_set_uint16(dev
, "id0", id0
);
806 qdev_prop_set_uint16(dev
, "id1", id1
);
807 qdev_prop_set_uint16(dev
, "id2", id2
);
808 qdev_prop_set_uint16(dev
, "id3", id3
);
809 qdev_prop_set_uint16(dev
, "unlock-addr0", unlock_addr0
);
810 qdev_prop_set_uint16(dev
, "unlock-addr1", unlock_addr1
);
811 qdev_prop_set_string(dev
, "name", name
);
812 qdev_init_nofail(dev
);
814 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);
815 return CFI_PFLASH02(dev
);