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/block.h"
41 #include "hw/block/flash.h"
42 #include "qapi/error.h"
43 #include "qemu/timer.h"
44 #include "sysemu/block-backend.h"
45 #include "qemu/host-utils.h"
46 #include "qemu/module.h"
47 #include "hw/sysbus.h"
50 #define PFLASH_DEBUG false
51 #define DPRINTF(fmt, ...) \
54 fprintf(stderr, "PFLASH: " fmt, ## __VA_ARGS__); \
58 #define PFLASH_LAZY_ROMD_THRESHOLD 42
60 /* Special write cycles for CFI queries. */
67 SysBusDevice parent_obj
;
77 int wcycle
; /* if 0, the flash is read normally */
82 /* FIXME: implement array device properties */
87 uint16_t unlock_addr0
;
88 uint16_t unlock_addr1
;
89 uint8_t cfi_table
[0x52];
91 /* The device replicates the flash memory across its memory space. Emulate
92 * that by having a container (.mem) filled with an array of aliases
93 * (.mem_mappings) pointing to the flash memory (.orig_mem).
96 MemoryRegion
*mem_mappings
; /* array; one per mapping */
97 MemoryRegion orig_mem
;
99 int read_counter
; /* used for lazy switch-back to rom mode */
105 * Toggle status bit DQ7.
107 static inline void toggle_dq7(PFlashCFI02
*pfl
)
113 * Set status bit DQ7 to bit 7 of value.
115 static inline void set_dq7(PFlashCFI02
*pfl
, uint8_t value
)
118 pfl
->status
|= value
& 0x80;
122 * Toggle status bit DQ6.
124 static inline void toggle_dq6(PFlashCFI02
*pfl
)
130 * Set up replicated mappings of the same region.
132 static void pflash_setup_mappings(PFlashCFI02
*pfl
)
135 hwaddr size
= memory_region_size(&pfl
->orig_mem
);
137 memory_region_init(&pfl
->mem
, OBJECT(pfl
), "pflash", pfl
->mappings
* size
);
138 pfl
->mem_mappings
= g_new(MemoryRegion
, pfl
->mappings
);
139 for (i
= 0; i
< pfl
->mappings
; ++i
) {
140 memory_region_init_alias(&pfl
->mem_mappings
[i
], OBJECT(pfl
),
141 "pflash-alias", &pfl
->orig_mem
, 0, size
);
142 memory_region_add_subregion(&pfl
->mem
, i
* size
, &pfl
->mem_mappings
[i
]);
146 static void pflash_register_memory(PFlashCFI02
*pfl
, int rom_mode
)
148 memory_region_rom_device_set_romd(&pfl
->orig_mem
, rom_mode
);
149 pfl
->rom_mode
= rom_mode
;
152 static void pflash_timer (void *opaque
)
154 PFlashCFI02
*pfl
= opaque
;
156 trace_pflash_timer_expired(pfl
->cmd
);
162 pflash_register_memory(pfl
, 1);
169 * Read data from flash.
171 static uint64_t pflash_data_read(PFlashCFI02
*pfl
, hwaddr offset
,
174 uint8_t *p
= (uint8_t *)pfl
->storage
+ offset
;
175 uint64_t ret
= pfl
->be
? ldn_be_p(p
, width
) : ldn_le_p(p
, width
);
176 trace_pflash_data_read(offset
, width
<< 1, ret
);
180 static uint64_t pflash_read(void *opaque
, hwaddr offset
, unsigned int width
)
182 PFlashCFI02
*pfl
= opaque
;
187 /* Lazy reset to ROMD mode after a certain amount of read accesses */
188 if (!pfl
->rom_mode
&& pfl
->wcycle
== 0 &&
189 ++pfl
->read_counter
> PFLASH_LAZY_ROMD_THRESHOLD
) {
190 pflash_register_memory(pfl
, 1);
192 offset
&= pfl
->chip_len
- 1;
193 boff
= offset
& 0xFF;
196 else if (pfl
->width
== 4)
200 /* This should never happen : reset state & treat it as a read*/
201 DPRINTF("%s: unknown command state: %x\n", __func__
, pfl
->cmd
);
204 /* fall through to the read code */
206 /* We accept reads during second unlock sequence... */
208 /* Flash area read */
209 ret
= pflash_data_read(pfl
, offset
, width
);
216 ret
= boff
& 0x01 ? pfl
->ident1
: pfl
->ident0
;
219 ret
= 0x00; /* Pretend all sectors are unprotected */
223 ret
= boff
& 0x01 ? pfl
->ident3
: pfl
->ident2
;
224 if (ret
!= (uint8_t)-1) {
227 /* Fall through to data read. */
229 ret
= pflash_data_read(pfl
, offset
, width
);
231 DPRINTF("%s: ID " TARGET_FMT_plx
" %" PRIx64
"\n", __func__
, boff
, ret
);
236 /* Status register read */
238 DPRINTF("%s: status %" PRIx64
"\n", __func__
, ret
);
243 if (boff
< sizeof(pfl
->cfi_table
)) {
244 ret
= pfl
->cfi_table
[boff
];
250 trace_pflash_io_read(offset
, width
, width
<< 1, ret
, pfl
->cmd
, pfl
->wcycle
);
255 /* update flash content on disk */
256 static void pflash_update(PFlashCFI02
*pfl
, int offset
, int size
)
260 offset_end
= offset
+ size
;
261 /* widen to sector boundaries */
262 offset
= QEMU_ALIGN_DOWN(offset
, BDRV_SECTOR_SIZE
);
263 offset_end
= QEMU_ALIGN_UP(offset_end
, BDRV_SECTOR_SIZE
);
264 blk_pwrite(pfl
->blk
, offset
, pfl
->storage
+ offset
,
265 offset_end
- offset
, 0);
269 static void pflash_write(void *opaque
, hwaddr offset
, uint64_t value
,
272 PFlashCFI02
*pfl
= opaque
;
277 trace_pflash_io_write(offset
, width
, width
<< 1, value
, pfl
->wcycle
);
279 if (pfl
->cmd
!= 0xA0 && cmd
== 0xF0) {
282 offset
&= pfl
->chip_len
- 1;
287 else if (pfl
->width
== 4)
289 /* Only the least-significant 11 bits are used in most cases. */
291 switch (pfl
->wcycle
) {
293 /* Set the device in I/O access mode if required */
295 pflash_register_memory(pfl
, 0);
296 pfl
->read_counter
= 0;
297 /* We're in read mode */
299 if (boff
== 0x55 && cmd
== 0x98) {
301 /* Enter CFI query mode */
302 pfl
->wcycle
= WCYCLE_CFI
;
306 if (boff
!= pfl
->unlock_addr0
|| cmd
!= 0xAA) {
307 DPRINTF("%s: unlock0 failed " TARGET_FMT_plx
" %02x %04x\n",
308 __func__
, boff
, cmd
, pfl
->unlock_addr0
);
311 DPRINTF("%s: unlock sequence started\n", __func__
);
314 /* We started an unlock sequence */
316 if (boff
!= pfl
->unlock_addr1
|| cmd
!= 0x55) {
317 DPRINTF("%s: unlock1 failed " TARGET_FMT_plx
" %02x\n", __func__
,
321 DPRINTF("%s: unlock sequence done\n", __func__
);
324 /* We finished an unlock sequence */
325 if (!pfl
->bypass
&& boff
!= pfl
->unlock_addr0
) {
326 DPRINTF("%s: command failed " TARGET_FMT_plx
" %02x\n", __func__
,
338 DPRINTF("%s: starting command %02x\n", __func__
, cmd
);
341 DPRINTF("%s: unknown command %02x\n", __func__
, cmd
);
348 /* We need another unlock sequence */
351 trace_pflash_data_write(offset
, width
<< 1, value
, 0);
353 p
= (uint8_t *)pfl
->storage
+ offset
;
355 uint64_t current
= ldn_be_p(p
, width
);
356 stn_be_p(p
, width
, current
& value
);
358 uint64_t current
= ldn_le_p(p
, width
);
359 stn_le_p(p
, width
, current
& value
);
361 pflash_update(pfl
, offset
, width
);
364 * While programming, status bit DQ7 should hold the opposite
365 * value from how it was programmed.
367 set_dq7(pfl
, ~value
);
368 /* Let's pretend write is immediate */
373 if (pfl
->bypass
&& cmd
== 0x00) {
374 /* Unlock bypass reset */
377 /* We can enter CFI query mode from autoselect mode */
378 if (boff
== 0x55 && cmd
== 0x98)
382 DPRINTF("%s: invalid write for command %02x\n",
389 /* Ignore writes while flash data write is occurring */
390 /* As we suppose write is immediate, this should never happen */
395 /* Should never happen */
396 DPRINTF("%s: invalid command state %02x (wc 4)\n",
404 if (boff
!= pfl
->unlock_addr0
) {
405 DPRINTF("%s: chip erase: invalid address " TARGET_FMT_plx
"\n",
410 DPRINTF("%s: start chip erase\n", __func__
);
412 memset(pfl
->storage
, 0xFF, pfl
->chip_len
);
413 pflash_update(pfl
, 0, pfl
->chip_len
);
416 /* Let's wait 5 seconds before chip erase is done */
417 timer_mod(&pfl
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
418 (NANOSECONDS_PER_SECOND
* 5));
423 offset
&= ~(pfl
->sector_len
- 1);
424 DPRINTF("%s: start sector erase at " TARGET_FMT_plx
"\n", __func__
,
427 memset(p
+ offset
, 0xFF, pfl
->sector_len
);
428 pflash_update(pfl
, offset
, pfl
->sector_len
);
431 /* Let's wait 1/2 second before sector erase is done */
432 timer_mod(&pfl
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
433 (NANOSECONDS_PER_SECOND
/ 2));
436 DPRINTF("%s: invalid command %02x (wc 5)\n", __func__
, cmd
);
444 /* Ignore writes during chip erase */
447 /* Ignore writes during sector erase */
450 /* Should never happen */
451 DPRINTF("%s: invalid command state %02x (wc 6)\n",
456 /* Special values for CFI queries */
458 DPRINTF("%s: invalid write in CFI query mode\n", __func__
);
461 /* Should never happen */
462 DPRINTF("%s: invalid write state (wc 7)\n", __func__
);
471 trace_pflash_reset();
482 static const MemoryRegionOps pflash_cfi02_ops
= {
484 .write
= pflash_write
,
485 .valid
.min_access_size
= 1,
486 .valid
.max_access_size
= 4,
487 .endianness
= DEVICE_NATIVE_ENDIAN
,
490 static void pflash_cfi02_realize(DeviceState
*dev
, Error
**errp
)
492 PFlashCFI02
*pfl
= PFLASH_CFI02(dev
);
495 Error
*local_err
= NULL
;
497 if (pfl
->sector_len
== 0) {
498 error_setg(errp
, "attribute \"sector-length\" not specified or zero.");
501 if (pfl
->nb_blocs
== 0) {
502 error_setg(errp
, "attribute \"num-blocks\" not specified or zero.");
505 if (pfl
->name
== NULL
) {
506 error_setg(errp
, "attribute \"name\" not specified.");
510 chip_len
= pfl
->sector_len
* pfl
->nb_blocs
;
512 memory_region_init_rom_device(&pfl
->orig_mem
, OBJECT(pfl
),
513 &pflash_cfi02_ops
, pfl
, pfl
->name
,
514 chip_len
, &local_err
);
516 error_propagate(errp
, local_err
);
520 pfl
->storage
= memory_region_get_ram_ptr(&pfl
->orig_mem
);
521 pfl
->chip_len
= chip_len
;
525 pfl
->ro
= blk_is_read_only(pfl
->blk
);
526 perm
= BLK_PERM_CONSISTENT_READ
| (pfl
->ro
? 0 : BLK_PERM_WRITE
);
527 ret
= blk_set_perm(pfl
->blk
, perm
, BLK_PERM_ALL
, errp
);
536 if (!blk_check_size_and_read_all(pfl
->blk
, pfl
->storage
, chip_len
,
538 vmstate_unregister_ram(&pfl
->orig_mem
, DEVICE(pfl
));
543 /* Only 11 bits are used in the comparison. */
544 pfl
->unlock_addr0
&= 0x7FF;
545 pfl
->unlock_addr1
&= 0x7FF;
547 pflash_setup_mappings(pfl
);
549 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &pfl
->mem
);
551 timer_init_ns(&pfl
->timer
, QEMU_CLOCK_VIRTUAL
, pflash_timer
, pfl
);
555 /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
556 /* Standard "QRY" string */
557 pfl
->cfi_table
[0x10] = 'Q';
558 pfl
->cfi_table
[0x11] = 'R';
559 pfl
->cfi_table
[0x12] = 'Y';
560 /* Command set (AMD/Fujitsu) */
561 pfl
->cfi_table
[0x13] = 0x02;
562 pfl
->cfi_table
[0x14] = 0x00;
563 /* Primary extended table address */
564 pfl
->cfi_table
[0x15] = 0x31;
565 pfl
->cfi_table
[0x16] = 0x00;
566 /* Alternate command set (none) */
567 pfl
->cfi_table
[0x17] = 0x00;
568 pfl
->cfi_table
[0x18] = 0x00;
569 /* Alternate extended table (none) */
570 pfl
->cfi_table
[0x19] = 0x00;
571 pfl
->cfi_table
[0x1A] = 0x00;
573 pfl
->cfi_table
[0x1B] = 0x27;
575 pfl
->cfi_table
[0x1C] = 0x36;
576 /* Vpp min (no Vpp pin) */
577 pfl
->cfi_table
[0x1D] = 0x00;
578 /* Vpp max (no Vpp pin) */
579 pfl
->cfi_table
[0x1E] = 0x00;
581 pfl
->cfi_table
[0x1F] = 0x07;
582 /* Timeout for min size buffer write (NA) */
583 pfl
->cfi_table
[0x20] = 0x00;
584 /* Typical timeout for block erase (512 ms) */
585 pfl
->cfi_table
[0x21] = 0x09;
586 /* Typical timeout for full chip erase (4096 ms) */
587 pfl
->cfi_table
[0x22] = 0x0C;
589 pfl
->cfi_table
[0x23] = 0x01;
590 /* Max timeout for buffer write (NA) */
591 pfl
->cfi_table
[0x24] = 0x00;
592 /* Max timeout for block erase */
593 pfl
->cfi_table
[0x25] = 0x0A;
594 /* Max timeout for chip erase */
595 pfl
->cfi_table
[0x26] = 0x0D;
597 pfl
->cfi_table
[0x27] = ctz32(chip_len
);
598 /* Flash device interface (8 & 16 bits) */
599 pfl
->cfi_table
[0x28] = 0x02;
600 pfl
->cfi_table
[0x29] = 0x00;
601 /* Max number of bytes in multi-bytes write */
602 /* XXX: disable buffered write as it's not supported */
603 // pfl->cfi_table[0x2A] = 0x05;
604 pfl
->cfi_table
[0x2A] = 0x00;
605 pfl
->cfi_table
[0x2B] = 0x00;
606 /* Number of erase block regions (uniform) */
607 pfl
->cfi_table
[0x2C] = 0x01;
608 /* Erase block region 1 */
609 pfl
->cfi_table
[0x2D] = pfl
->nb_blocs
- 1;
610 pfl
->cfi_table
[0x2E] = (pfl
->nb_blocs
- 1) >> 8;
611 pfl
->cfi_table
[0x2F] = pfl
->sector_len
>> 8;
612 pfl
->cfi_table
[0x30] = pfl
->sector_len
>> 16;
615 pfl
->cfi_table
[0x31] = 'P';
616 pfl
->cfi_table
[0x32] = 'R';
617 pfl
->cfi_table
[0x33] = 'I';
619 pfl
->cfi_table
[0x34] = '1';
620 pfl
->cfi_table
[0x35] = '0';
622 pfl
->cfi_table
[0x36] = 0x00;
623 pfl
->cfi_table
[0x37] = 0x00;
624 pfl
->cfi_table
[0x38] = 0x00;
625 pfl
->cfi_table
[0x39] = 0x00;
627 pfl
->cfi_table
[0x3a] = 0x00;
629 pfl
->cfi_table
[0x3b] = 0x00;
630 pfl
->cfi_table
[0x3c] = 0x00;
633 static Property pflash_cfi02_properties
[] = {
634 DEFINE_PROP_DRIVE("drive", PFlashCFI02
, blk
),
635 DEFINE_PROP_UINT32("num-blocks", PFlashCFI02
, nb_blocs
, 0),
636 DEFINE_PROP_UINT32("sector-length", PFlashCFI02
, sector_len
, 0),
637 DEFINE_PROP_UINT8("width", PFlashCFI02
, width
, 0),
638 DEFINE_PROP_UINT8("mappings", PFlashCFI02
, mappings
, 0),
639 DEFINE_PROP_UINT8("big-endian", PFlashCFI02
, be
, 0),
640 DEFINE_PROP_UINT16("id0", PFlashCFI02
, ident0
, 0),
641 DEFINE_PROP_UINT16("id1", PFlashCFI02
, ident1
, 0),
642 DEFINE_PROP_UINT16("id2", PFlashCFI02
, ident2
, 0),
643 DEFINE_PROP_UINT16("id3", PFlashCFI02
, ident3
, 0),
644 DEFINE_PROP_UINT16("unlock-addr0", PFlashCFI02
, unlock_addr0
, 0),
645 DEFINE_PROP_UINT16("unlock-addr1", PFlashCFI02
, unlock_addr1
, 0),
646 DEFINE_PROP_STRING("name", PFlashCFI02
, name
),
647 DEFINE_PROP_END_OF_LIST(),
650 static void pflash_cfi02_unrealize(DeviceState
*dev
, Error
**errp
)
652 PFlashCFI02
*pfl
= PFLASH_CFI02(dev
);
653 timer_del(&pfl
->timer
);
656 static void pflash_cfi02_class_init(ObjectClass
*klass
, void *data
)
658 DeviceClass
*dc
= DEVICE_CLASS(klass
);
660 dc
->realize
= pflash_cfi02_realize
;
661 dc
->unrealize
= pflash_cfi02_unrealize
;
662 dc
->props
= pflash_cfi02_properties
;
663 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
666 static const TypeInfo pflash_cfi02_info
= {
667 .name
= TYPE_PFLASH_CFI02
,
668 .parent
= TYPE_SYS_BUS_DEVICE
,
669 .instance_size
= sizeof(PFlashCFI02
),
670 .class_init
= pflash_cfi02_class_init
,
673 static void pflash_cfi02_register_types(void)
675 type_register_static(&pflash_cfi02_info
);
678 type_init(pflash_cfi02_register_types
)
680 PFlashCFI02
*pflash_cfi02_register(hwaddr base
,
685 int nb_mappings
, int width
,
686 uint16_t id0
, uint16_t id1
,
687 uint16_t id2
, uint16_t id3
,
688 uint16_t unlock_addr0
,
689 uint16_t unlock_addr1
,
692 DeviceState
*dev
= qdev_create(NULL
, TYPE_PFLASH_CFI02
);
695 qdev_prop_set_drive(dev
, "drive", blk
, &error_abort
);
697 assert(size
% sector_len
== 0);
698 qdev_prop_set_uint32(dev
, "num-blocks", size
/ sector_len
);
699 qdev_prop_set_uint32(dev
, "sector-length", sector_len
);
700 qdev_prop_set_uint8(dev
, "width", width
);
701 qdev_prop_set_uint8(dev
, "mappings", nb_mappings
);
702 qdev_prop_set_uint8(dev
, "big-endian", !!be
);
703 qdev_prop_set_uint16(dev
, "id0", id0
);
704 qdev_prop_set_uint16(dev
, "id1", id1
);
705 qdev_prop_set_uint16(dev
, "id2", id2
);
706 qdev_prop_set_uint16(dev
, "id3", id3
);
707 qdev_prop_set_uint16(dev
, "unlock-addr0", unlock_addr0
);
708 qdev_prop_set_uint16(dev
, "unlock-addr1", unlock_addr1
);
709 qdev_prop_set_string(dev
, "name", name
);
710 qdev_init_nofail(dev
);
712 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);
713 return PFLASH_CFI02(dev
);