2 * CFI parallel flash with AMD / Fujitsu command set emulation
4 * Copyright (c) 2005 Jocelyn Mayer
5 * Copyright (c) 2006 Stefan Weil (ES29LV160DB emulation)
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.
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.
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
23 * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
24 * Supported commands/modes are:
30 * - unlock bypass command
33 * It does not support flash interleaving.
34 * It does not implement boot blocs with reduced size
35 * It does not implement software data protection as found in many real chips
36 * It does not implement erase suspend/resume commands
37 * It does not implement multiple sectors erase
40 * Support little endianness.
43 #include "qemu/osdep.h"
46 #include "block/block.h"
48 #include "pflash.h" /* pflash_amd_register */
49 #include "qemu&timer.h"
55 #define logfile stderr
56 #define DPRINTF(fmt, ...) \
57 ((loglevel) ? fprintf(logfile, "PFLASH\t%-24s" fmt , __func__, ##__VA_ARGS__) : (void)0)
59 #define DPRINTF(fmt, ...) do { } while (0)
75 int wcycle
; /* if 0, the flash is read normally */
82 uint8_t cfi_table
[0x52];
87 static void pflash_io_mode(pflash_t
*pfl
)
89 if (pfl
->mode
!= io_mode
) {
90 DPRINTF("switch to i/o mode\n");
91 memory_region_rom_device_set_readable(&pfl
->mem
, false);
92 //~ DPRINTF("0x%08x 0x%08x\n", pfl->base, pfl->total_len);
97 static void pflash_rom_mode(pflash_t
*pfl
)
99 if (pfl
->mode
!= rom_mode
) {
100 DPRINTF("switch to rom mode\n");
101 memory_region_rom_device_set_readable(&pfl
->mem
, true);
102 pfl
->mode
= rom_mode
;
106 static void pflash_timer (void *opaque
)
108 pflash_t
*pfl
= opaque
;
110 DPRINTF("command %02x done\n", pfl
->cmd
);
116 pflash_rom_mode(pfl
);
123 static uint32_t pflash_read (pflash_t
*pfl
, uint32_t offset
, int width
)
131 DPRINTF("offset %08x\n", offset
);
132 boff
= offset
& 0xFF;
135 else if (pfl
->width
== 4)
139 /* This should never happen : reset state & treat it as a read*/
140 DPRINTF("unknown command state: %x\n", pfl
->cmd
);
144 /* We accept reads during second unlock sequence... */
147 /* Flash area read */
152 // DPRINTF("data offset %08x %02x\n", offset, ret);
155 #if defined(TARGET_WORDS_BIGENDIAN)
156 ret
= p
[offset
] << 8;
157 ret
|= p
[offset
+ 1];
160 ret
|= p
[offset
+ 1] << 8;
162 // DPRINTF("data offset %08x %04x\n", offset, ret);
165 #if defined(TARGET_WORDS_BIGENDIAN)
166 ret
= p
[offset
] << 24;
167 ret
|= p
[offset
+ 1] << 16;
168 ret
|= p
[offset
+ 2] << 8;
169 ret
|= p
[offset
+ 3];
172 ret
|= p
[offset
+ 1] << 8;
173 ret
|= p
[offset
+ 2] << 16;
174 ret
|= p
[offset
+ 3] << 24;
176 // DPRINTF("data offset %08x %08x\n", offset, ret);
185 ret
= pfl
->ident
[boff
& 0x01];
188 ret
= 0x00; /* Pretend all sectors are unprotected */
192 if (pfl
->ident
[2 + (boff
& 0x01)] == (uint8_t)-1)
194 ret
= pfl
->ident
[2 + (boff
& 0x01)];
199 DPRINTF("ID %d %x\n", boff
, ret
);
204 /* Status register read */
206 DPRINTF("status %x\n", ret
);
212 if (boff
< sizeof(pfl
->cfi_table
)) {
213 ret
= pfl
->cfi_table
[boff
];
220 DPRINTF("offset %08x %08x %d\n", offset
, ret
, width
);
224 /* update flash content on disk */
225 static void pflash_update(pflash_t
*pfl
, int offset
,
230 offset_end
= offset
+ size
;
231 /* round to sectors */
232 offset
= offset
>> 9;
233 offset_end
= (offset_end
+ 511) >> 9;
234 bdrv_write(pfl
->bs
, offset
, pfl
->storage
+ (offset
<< 9),
235 offset_end
- offset
);
239 static void pflash_write(pflash_t
*pfl
, uint32_t offset
, uint32_t value
,
245 uint32_t sector_len
= pfl
->sector_len
;
247 /* WARNING: when the memory area is in ROMD mode, the offset is a
248 ram offset, not a physical address */
249 if (pfl
->mode
== rom_mode
) {
250 // TODO: next line raises a compiler warning, needs fix.
251 //~ offset -= (uint32_t)pfl->storage;
257 DPRINTF("offset %08x %08x %d\n", offset
, value
, width
);
258 if (pfl
->cmd
!= 0xA0 && cmd
== 0xf0) {
259 DPRINTF("flash reset asked (%02x %02x)\n", pfl
->cmd
, cmd
);
262 if (pfl
->cmd
!= 0xA0 && cmd
== 0xff) {
263 /* Intel command (read array mode). */
264 DPRINTF("read array asked (%02x %02x)\n", pfl
->cmd
, cmd
);
267 //~ !!! next code only for certain flash chips
268 if (offset
< 0x004000) {
270 } else if (offset
< 0x008000) {
272 } else if (offset
< 0x010000) {
275 boff
= offset
& (sector_len
- 1);
278 else if (pfl
->width
== 4)
280 switch (pfl
->wcycle
) {
282 /* We're in read mode */
283 if (boff
== 0x55 && cmd
== 0x98) {
285 /* Enter CFI query mode */
288 /* Set the device in I/O access mode */
293 if ((boff
!= 0x555 && offset
!= 0xaaaa) || cmd
!= 0xAA) {
294 DPRINTF("unlock0 failed %04x %02x %04x\n", boff
, cmd
, 0x555);
297 DPRINTF("unlock sequence started\n");
298 /* Set the device in I/O access mode */
302 /* We started an unlock sequence */
304 if ((boff
!= 0x2AA && offset
!= 0x5554) || cmd
!= 0x55) {
305 DPRINTF("unlock1 failed %04x %02x\n", boff
, cmd
);
308 DPRINTF("unlock sequence done\n");
311 /* We finished an unlock sequence */
312 if (!pfl
->bypass
&& boff
!= 0x555 && offset
!= 0xaaaa) {
313 DPRINTF("command failed %04x %02x\n", boff
, cmd
);
324 DPRINTF("starting command %02x\n", cmd
);
327 DPRINTF("unknown command %02x\n", cmd
);
334 /* We need another unlock sequence */
337 DPRINTF("write data offset %08x %08x %d\n", offset
, value
, width
);
342 pflash_update(pfl
, offset
, 1);
345 #if defined(TARGET_WORDS_BIGENDIAN)
346 p
[offset
] &= value
>> 8;
347 p
[offset
+ 1] &= value
;
350 p
[offset
+ 1] &= value
>> 8;
352 pflash_update(pfl
, offset
, 2);
355 #if defined(TARGET_WORDS_BIGENDIAN)
356 p
[offset
] &= value
>> 24;
357 p
[offset
+ 1] &= value
>> 16;
358 p
[offset
+ 2] &= value
>> 8;
359 p
[offset
+ 3] &= value
;
362 p
[offset
+ 1] &= value
>> 8;
363 p
[offset
+ 2] &= value
>> 16;
364 p
[offset
+ 3] &= value
>> 24;
366 pflash_update(pfl
, offset
, 4);
369 pfl
->status
= 0x00 | ~(value
& 0x80);
370 /* Let's pretend write is immediate */
375 if (pfl
->bypass
&& cmd
== 0x00) {
376 /* Unlock bypass reset */
379 /* We can enter CFI query mode from autoselect mode */
380 if (boff
== 0x55 && cmd
== 0x98)
384 DPRINTF("invalid write for command %02x\n", pfl
->cmd
);
390 /* Ignore writes while flash data write is occurring */
391 /* As we suppose write is immediate, this should never happen */
396 /* Should never happen */
397 DPRINTF("invalid command state %02x (wc 4)\n", pfl
->cmd
);
405 DPRINTF("chip erase: invalid address %04x\n", offset
);
409 DPRINTF("start chip erase\n");
410 memset(pfl
->storage
, 0xFF, pfl
->total_len
);
412 pflash_update(pfl
, 0, pfl
->total_len
);
413 /* Let's wait 5 seconds before chip erase is done */
414 timer_mod(pfl
->timer
,
415 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + (NANOSECONDS_PER_SECOND
* 5));
420 offset
&= ~(sector_len
- 1);
421 DPRINTF("start sector erase at %08x\n", offset
);
422 memset(p
+ offset
, 0xFF, sector_len
);
423 pflash_update(pfl
, offset
, sector_len
);
425 /* Let's wait 1/2 second before sector erase is done */
426 timer_mod(pfl
->timer
,
427 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + (NANOSECONDS_PER_SECOND
/ 2));
430 DPRINTF("invalid command %02x (wc 5)\n", cmd
);
438 /* Ignore writes during chip erase */
441 /* Ignore writes during sector erase */
444 /* Should never happen */
445 DPRINTF("invalid command state %02x (wc 6)\n", pfl
->cmd
);
449 case 7: /* Special value for CFI queries */
450 DPRINTF("invalid write in CFI query mode\n");
453 /* Should never happen */
454 DPRINTF("invalid write state (wc 7)\n");
463 pflash_rom_mode(pfl
);
476 static uint32_t pflash_readb_be(void *opaque
, hwaddr addr
)
478 return pflash_read(opaque
, addr
, 1);
481 static uint32_t pflash_readw_be(void *opaque
, hwaddr addr
)
483 pflash_t
*pfl
= opaque
;
485 return pflash_read(pfl
, addr
, 2);
488 static uint32_t pflash_readl_be(void *opaque
, hwaddr addr
)
490 pflash_t
*pfl
= opaque
;
492 return pflash_read(pfl
, addr
, 4);
495 static void pflash_writeb_be(void *opaque
, hwaddr addr
,
498 pflash_write(opaque
, addr
, value
, 1, true);
501 static void pflash_writew_be(void *opaque
, hwaddr addr
,
504 pflash_t
*pfl
= opaque
;
506 pflash_write(pfl
, addr
, value
, 2, true);
509 static void pflash_writel_be(void *opaque
, hwaddr addr
,
512 pflash_t
*pfl
= opaque
;
514 pflash_write(pfl
, addr
, value
, 4, true);
517 static const MemoryRegionOps pflash_cfi01_ops_be
= {
519 .read
= { pflash_readb_be
, pflash_readw_be
, pflash_readl_be
, },
520 .write
= { pflash_writeb_be
, pflash_writew_be
, pflash_writel_be
, },
522 .endianness
= DEVICE_NATIVE_ENDIAN
,
525 /* Count trailing zeroes of a 32 bits quantity */
526 static int ctz32 (uint32_t n
)
551 #if 0 /* This is not necessary as n is never 0 */
559 static void flash_reset(void *opaque
)
561 pflash_t
*pfl
= (pflash_t
*)opaque
;
562 DPRINTF("%s:%u\n", __FILE__
, __LINE__
);
563 pflash_rom_mode(pfl
);
569 pflash_t
*pflash_amd_register (hwaddr base
, ram_addr_t off
,
570 BlockDriverState
*bs
,
571 uint32_t sector_len
, int nb_blocs
, int width
,
572 uint16_t id0
, uint16_t id1
,
573 uint16_t id2
, uint16_t id3
)
576 target_long total_len
;
579 if (getenv("DEBUG_FLASH")) {
580 traceflag
= strtoul(getenv("DEBUG_FLASH"), 0, 0);
582 DPRINTF("Logging enabled for FLASH in %s\n", __func__
);
585 total_len
= sector_len
* nb_blocs
;
587 DPRINTF("flash size " TARGET_FMT_lu
" MiB (" TARGET_FMT_lu
" x %u bytes)\n",
588 total_len
/ MiB
, total_len
/ width
, width
);
590 /* XXX: to be fixed */
591 if (total_len
!= (2 * MiB
) && total_len
!= (4 * MiB
) &&
592 total_len
!= (8 * MiB
) && total_len
!= (16 * MiB
) &&
593 total_len
!= (32 * MiB
) && total_len
!= (64 * MiB
))
596 pfl
= g_new0(pflash_t
, 1);
598 memory_region_init_rom_device(
599 &pfl
->mem
, be
? &pflash_cfi01_ops_be
: &pflash_cfi01_ops_le
, pfl
,
601 pfl
->storage
= memory_region_get_ram_ptr(&pfl
->mem
);
602 memory_region_add_subregion(get_system_memory(), base
, &pfl
->mem
);
605 pfl
->sector_len
= sector_len
;
606 pfl
->total_len
= total_len
;
607 pflash_rom_mode(pfl
);
610 /* read the initial flash content */
611 bdrv_read(pfl
->bs
, 0, pfl
->storage
, total_len
>> 9);
613 memory_region_del_subregion(get_system_memory(), &pfl
->mem
);
614 memory_region_destroy(&pfl
->mem
);
619 #if 0 /* XXX: there should be a bit to set up read-only,
620 * the same way the hardware does (with WP pin).
626 pfl
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, pflash_timer
, pfl
);
636 /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
637 /* Standard "QRY" string */
638 pfl
->cfi_table
[0x10] = 'Q';
639 pfl
->cfi_table
[0x11] = 'R';
640 pfl
->cfi_table
[0x12] = 'Y';
641 /* Command set (AMD/Fujitsu) */
642 pfl
->cfi_table
[0x13] = P_ID_AMD_STD
;
643 pfl
->cfi_table
[0x14] = 0x00;
644 /* Primary extended table address (none) */
645 pfl
->cfi_table
[0x15] = 0x00;
646 pfl
->cfi_table
[0x16] = 0x00;
647 /* Alternate command set (none) */
648 pfl
->cfi_table
[0x17] = 0x00;
649 pfl
->cfi_table
[0x18] = 0x00;
650 /* Alternate extended table (none) */
651 pfl
->cfi_table
[0x19] = 0x00;
652 pfl
->cfi_table
[0x1A] = 0x00;
654 pfl
->cfi_table
[0x1B] = 0x27;
656 pfl
->cfi_table
[0x1C] = 0x36;
657 /* Vpp min (no Vpp pin) */
658 pfl
->cfi_table
[0x1D] = 0x00;
659 /* Vpp max (no Vpp pin) */
660 pfl
->cfi_table
[0x1E] = 0x00;
662 pfl
->cfi_table
[0x1F] = 0x07;
663 /* Timeout for min size buffer write (16 µs) */
664 pfl
->cfi_table
[0x20] = 0x04;
665 /* Typical timeout for block erase (512 ms) */
666 pfl
->cfi_table
[0x21] = 0x09;
667 /* Typical timeout for full chip erase (4096 ms) */
668 pfl
->cfi_table
[0x22] = 0x0C;
670 pfl
->cfi_table
[0x23] = 0x01;
671 /* Max timeout for buffer write */
672 pfl
->cfi_table
[0x24] = 0x04;
673 /* Max timeout for block erase */
674 pfl
->cfi_table
[0x25] = 0x0A;
675 /* Max timeout for chip erase */
676 pfl
->cfi_table
[0x26] = 0x0D;
678 //~ pfl->cfi_table[0x27] = ctz32(total_len) + 1;
679 pfl
->cfi_table
[0x27] = ctz32(total_len
); // !!! 0x15
680 /* Flash device interface (8 & 16 bits) */
681 pfl
->cfi_table
[0x28] = 0x02;
682 pfl
->cfi_table
[0x29] = 0x00;
683 /* Max number of bytes in multi-bytes write */
684 pfl
->cfi_table
[0x2A] = 0x05;
685 pfl
->cfi_table
[0x2B] = 0x00;
686 /* Number of erase block regions (uniform) */
687 pfl
->cfi_table
[0x2C] = 0x01;
688 /* Erase block region 1 */
689 pfl
->cfi_table
[0x2D] = nb_blocs
- 1;
690 pfl
->cfi_table
[0x2E] = (nb_blocs
- 1) >> 8;
691 pfl
->cfi_table
[0x2F] = sector_len
>> 8;
692 pfl
->cfi_table
[0x30] = sector_len
>> 16;
696 } else if ((id0
== MANUFACTURER_AMD
&& id1
== AM29LV160DB
) ||
697 (id0
== MANUFACTURER_004A
&& id1
== ES29LV160DB
) ||
698 (id0
== MANUFACTURER_SPANSION
&& id1
== S29AL016DB
)) {
699 static const uint8_t data
[] = {
700 /* 0x10 */ 'Q', 'R', 'Y', 0x02, 0x00, 0x40, 0x00, 0x00,
701 /* 0x18 */ 0x00, 0x00, 0x00, 0x27, 0x36, 0x00, 0x00, 0x04,
702 /* 0x20 */ 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x00, 0x15,
703 /* 0x28 */ 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x40,
704 /* 0x30 */ 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x80,
705 /* 0x38 */ 0x00, 0x1e, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
706 /* 0x40 */ 'P', 'R', 'I', '1', '0', 0x00, 0x02, 0x01,
707 /* 0x48 */ 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
709 memcpy(&pfl
->cfi_table
[0x10], data
, sizeof(data
));
710 //~ pfl->cfi_table[0x39] = nb_blocs - 2; // 0x1e
711 //~ pfl->cfi_table[0x3a] = (nb_blocs - 2) >> 8; // 0x00
712 //~ pfl->cfi_table[0x3b] = (sector_len / 256); // 0x01
713 //~ pfl->cfi_table[0x3c] = (sector_len / 256) >> 8; // 0x00
714 } else if (id0
== MANUFACTURER_MACRONIX
&& (id1
== MX29LV320CB
||
715 id1
== MX29LV320CT
|| id1
== MX29LV640BB
|| id1
== MX29LV640BT
)) {
716 static const uint8_t data
[] = {
717 /* 0x10 */ 'Q', 'R', 'Y', 0x02, 0x00, 0x40, 0x00, 0x00,
718 /* 0x18 */ 0x00, 0x00, 0x00, 0x27, 0x36, 0x00, 0x00, 0x04,
719 /* 0x20 */ 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x00, 0x16,
720 /* 0x28 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x07, 0x00, 0x20,
721 /* 0x30 */ 0x00, 0x3e, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
722 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
723 /* 0x40 */ 'P', 'R', 'I', '1', '1', 0x00, 0x02, 0x04,
724 /* 0x48 */ 0x01, 0x04, 0x00, 0x00, 0x00, 0xb5, 0xc5, 0x02,
726 memcpy(&pfl
->cfi_table
[0x10], data
, sizeof(data
));
727 if (id1
== MX29LV640BB
|| id1
== MX29LV640BT
) {
728 pfl
->cfi_table
[0x27] = 0x17;
729 pfl
->cfi_table
[0x31] = 0x7e;
731 if (id1
== MX29LV320CT
|| id1
== MX29LV640BT
) {
732 pfl
->cfi_table
[0x4f] = 0x03;
735 /* SG29 Spansion flash */
736 static const uint8_t data
[] = {
737 /* 0x10 */ 'Q', 'R', 'Y', 0x02, 0x00, 0x00, 0x00, 0x00,
738 /* 0x18 */ 0x00, 0x00, 0x00, 0x27, 0x36, 0x00, 0x00, 0x07,
739 /* 0x20 */ 0x04, 0x09, 0x0c, 0x01, 0x04, 0x0a, 0x0d, 0x16,
740 /* 0x28 */ 0x02, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x40,
742 memcpy(&pfl
->cfi_table
[0x10], data
, sizeof(data
));
743 pfl
->cfi_table
[0x27] = ctz32(total_len
);
744 pfl
->cfi_table
[0x2D] = nb_blocs
- 1;
745 pfl
->cfi_table
[0x2E] = (nb_blocs
- 1) >> 8;
746 pfl
->cfi_table
[0x2F] = sector_len
>> 8;
747 pfl
->cfi_table
[0x30] = sector_len
>> 16;
750 qemu_register_reset(flash_reset
, pfl
);
756 MemoryRegion
*pflash_amd_get_memory(pflash_t
*fl
)