1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2002-2004
4 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
6 * Copyright (C) 2003 Arabella Software Ltd.
7 * Yuli Barcohen <yuli@arabellasw.com>
13 * Tolunay Orkun <listmember@orkun.us>
16 /* The DEBUG define must be before common to enable debugging */
24 #include <fdt_support.h>
28 #include <asm/processor.h>
30 #include <asm/byteorder.h>
31 #include <asm/unaligned.h>
32 #include <env_internal.h>
33 #include <mtd/cfi_flash.h>
37 * This file implements a Common Flash Interface (CFI) driver for
40 * The width of the port and the width of the chips are determined at
41 * initialization. These widths are used to calculate the address for
42 * access CFI data structures.
45 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
46 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
47 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
48 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
49 * AMD CFI Specification, Release 2.0 December 1, 2001
50 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
51 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
53 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
54 * reading and writing ... (yes there is such a Hardware).
57 DECLARE_GLOBAL_DATA_PTR;
59 static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
60 #ifdef CONFIG_FLASH_CFI_MTD
61 static uint flash_verbose = 1;
63 #define flash_verbose 1
66 flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
69 * Check if chip width is defined. If not, start detecting with 8bit.
71 #ifndef CONFIG_SYS_FLASH_CFI_WIDTH
72 #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
75 #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
76 #define __maybe_weak __weak
78 #define __maybe_weak static
82 * 0xffff is an undefined value for the configuration register. When
83 * this value is returned, the configuration register shall not be
84 * written at all (default mode).
86 static u16 cfi_flash_config_reg(int i)
88 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
89 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
95 #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
96 int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
98 int cfi_flash_num_flash_banks;
101 #ifdef CONFIG_CFI_FLASH /* for driver model */
102 static void cfi_flash_init_dm(void)
106 cfi_flash_num_flash_banks = 0;
108 * The uclass_first_device() will probe the first device and
109 * uclass_next_device() will probe the rest if they exist. So
110 * that cfi_flash_probe() will get called assigning the base
111 * addresses that are available.
113 for (uclass_first_device(UCLASS_MTD, &dev);
115 uclass_next_device(&dev)) {
119 phys_addr_t cfi_flash_bank_addr(int i)
121 return flash_info[i].base;
124 __weak phys_addr_t cfi_flash_bank_addr(int i)
126 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
130 __weak unsigned long cfi_flash_bank_size(int i)
132 #ifdef CONFIG_SYS_FLASH_BANKS_SIZES
133 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
139 __maybe_weak void flash_write8(u8 value, void *addr)
141 __raw_writeb(value, addr);
144 __maybe_weak void flash_write16(u16 value, void *addr)
146 __raw_writew(value, addr);
149 __maybe_weak void flash_write32(u32 value, void *addr)
151 __raw_writel(value, addr);
154 __maybe_weak void flash_write64(u64 value, void *addr)
156 /* No architectures currently implement __raw_writeq() */
157 *(volatile u64 *)addr = value;
160 __maybe_weak u8 flash_read8(void *addr)
162 return __raw_readb(addr);
165 __maybe_weak u16 flash_read16(void *addr)
167 return __raw_readw(addr);
170 __maybe_weak u32 flash_read32(void *addr)
172 return __raw_readl(addr);
175 __maybe_weak u64 flash_read64(void *addr)
177 /* No architectures currently implement __raw_readq() */
178 return *(volatile u64 *)addr;
181 /*-----------------------------------------------------------------------
183 #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || \
184 (defined(CONFIG_SYS_MONITOR_BASE) && \
185 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE))
186 static flash_info_t *flash_get_info(ulong base)
191 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
192 info = &flash_info[i];
193 if (info->size && info->start[0] <= base &&
194 base <= info->start[0] + info->size - 1)
202 unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
204 if (sect != (info->sector_count - 1))
205 return info->start[sect + 1] - info->start[sect];
207 return info->start[0] + info->size - info->start[sect];
210 /*-----------------------------------------------------------------------
211 * create an address based on the offset and the port width
214 flash_map(flash_info_t *info, flash_sect_t sect, uint offset)
216 unsigned int byte_offset = offset * info->portwidth;
218 return (void *)(info->start[sect] + byte_offset);
221 static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
222 unsigned int offset, void *addr)
226 /*-----------------------------------------------------------------------
227 * make a proper sized command based on the port and chip widths
229 static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
234 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
235 u32 cmd_le = cpu_to_le32(cmd);
238 uchar *cp = (uchar *) cmdbuf;
240 for (i = info->portwidth; i > 0; i--) {
241 cword_offset = (info->portwidth - i) % info->chipwidth;
242 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
243 cp_offset = info->portwidth - i;
244 val = *((uchar *)&cmd_le + cword_offset);
247 val = *((uchar *)&cmd + sizeof(u32) - cword_offset - 1);
249 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
254 /*-----------------------------------------------------------------------
257 static void print_longlong(char *str, unsigned long long data)
263 for (i = 0; i < 8; i++)
264 sprintf(&str[i * 2], "%2.2x", *cp++);
267 static void flash_printqry(struct cfi_qry *qry)
272 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
274 for (y = 0; y < 16; y++)
275 debug("%2.2x ", p[x + y]);
277 for (y = 0; y < 16; y++) {
278 unsigned char c = p[x + y];
280 if (c >= 0x20 && c <= 0x7e)
290 /*-----------------------------------------------------------------------
291 * read a character at a port width address
293 static inline uchar flash_read_uchar(flash_info_t *info, uint offset)
298 cp = flash_map(info, 0, offset);
299 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
300 retval = flash_read8(cp);
302 retval = flash_read8(cp + info->portwidth - 1);
304 flash_unmap(info, 0, offset, cp);
308 /*-----------------------------------------------------------------------
309 * read a word at a port width address, assume 16bit bus
311 static inline ushort flash_read_word(flash_info_t *info, uint offset)
313 ushort *addr, retval;
315 addr = flash_map(info, 0, offset);
316 retval = flash_read16(addr);
317 flash_unmap(info, 0, offset, addr);
321 /*-----------------------------------------------------------------------
322 * read a long word by picking the least significant byte of each maximum
323 * port size word. Swap for ppc format.
325 static ulong flash_read_long (flash_info_t *info, flash_sect_t sect,
334 addr = flash_map(info, sect, offset);
337 debug("long addr is at %p info->portwidth = %d\n", addr,
339 for (x = 0; x < 4 * info->portwidth; x++)
340 debug("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
342 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
343 retval = ((flash_read8(addr) << 16) |
344 (flash_read8(addr + info->portwidth) << 24) |
345 (flash_read8(addr + 2 * info->portwidth)) |
346 (flash_read8(addr + 3 * info->portwidth) << 8));
348 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
349 (flash_read8(addr + info->portwidth - 1) << 16) |
350 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
351 (flash_read8(addr + 3 * info->portwidth - 1)));
353 flash_unmap(info, sect, offset, addr);
359 * Write a proper sized command to the correct address
361 static void flash_write_cmd(flash_info_t *info, flash_sect_t sect,
362 uint offset, u32 cmd)
367 addr = flash_map(info, sect, offset);
368 flash_make_cmd(info, cmd, &cword);
369 switch (info->portwidth) {
371 debug("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
372 cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
373 flash_write8(cword.w8, addr);
375 case FLASH_CFI_16BIT:
376 debug("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
378 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
379 flash_write16(cword.w16, addr);
381 case FLASH_CFI_32BIT:
382 debug("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr,
384 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
385 flash_write32(cword.w32, addr);
387 case FLASH_CFI_64BIT:
392 print_longlong(str, cword.w64);
394 debug("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
396 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
399 flash_write64(cword.w64, addr);
403 /* Ensure all the instructions are fully finished */
406 flash_unmap(info, sect, offset, addr);
409 static void flash_unlock_seq(flash_info_t *info, flash_sect_t sect)
411 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
412 flash_write_cmd(info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
415 /*-----------------------------------------------------------------------
417 static int flash_isequal(flash_info_t *info, flash_sect_t sect, uint offset,
424 addr = flash_map(info, sect, offset);
425 flash_make_cmd(info, cmd, &cword);
427 debug("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
428 switch (info->portwidth) {
430 debug("is= %x %x\n", flash_read8(addr), cword.w8);
431 retval = (flash_read8(addr) == cword.w8);
433 case FLASH_CFI_16BIT:
434 debug("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16);
435 retval = (flash_read16(addr) == cword.w16);
437 case FLASH_CFI_32BIT:
438 debug("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32);
439 retval = (flash_read32(addr) == cword.w32);
441 case FLASH_CFI_64BIT:
447 print_longlong(str1, flash_read64(addr));
448 print_longlong(str2, cword.w64);
449 debug("is= %s %s\n", str1, str2);
452 retval = (flash_read64(addr) == cword.w64);
458 flash_unmap(info, sect, offset, addr);
463 /*-----------------------------------------------------------------------
465 static int flash_isset(flash_info_t *info, flash_sect_t sect, uint offset,
472 addr = flash_map(info, sect, offset);
473 flash_make_cmd(info, cmd, &cword);
474 switch (info->portwidth) {
476 retval = ((flash_read8(addr) & cword.w8) == cword.w8);
478 case FLASH_CFI_16BIT:
479 retval = ((flash_read16(addr) & cword.w16) == cword.w16);
481 case FLASH_CFI_32BIT:
482 retval = ((flash_read32(addr) & cword.w32) == cword.w32);
484 case FLASH_CFI_64BIT:
485 retval = ((flash_read64(addr) & cword.w64) == cword.w64);
491 flash_unmap(info, sect, offset, addr);
496 /*-----------------------------------------------------------------------
498 static int flash_toggle(flash_info_t *info, flash_sect_t sect, uint offset,
505 addr = flash_map(info, sect, offset);
506 flash_make_cmd(info, cmd, &cword);
507 switch (info->portwidth) {
509 retval = flash_read8(addr) != flash_read8(addr);
511 case FLASH_CFI_16BIT:
512 retval = flash_read16(addr) != flash_read16(addr);
514 case FLASH_CFI_32BIT:
515 retval = flash_read32(addr) != flash_read32(addr);
517 case FLASH_CFI_64BIT:
518 retval = ((flash_read32(addr) != flash_read32(addr)) ||
519 (flash_read32(addr + 4) != flash_read32(addr + 4)));
525 flash_unmap(info, sect, offset, addr);
531 * flash_is_busy - check to see if the flash is busy
533 * This routine checks the status of the chip and returns true if the
536 static int flash_is_busy(flash_info_t *info, flash_sect_t sect)
540 switch (info->vendor) {
541 case CFI_CMDSET_INTEL_PROG_REGIONS:
542 case CFI_CMDSET_INTEL_STANDARD:
543 case CFI_CMDSET_INTEL_EXTENDED:
544 retval = !flash_isset(info, sect, 0, FLASH_STATUS_DONE);
546 case CFI_CMDSET_AMD_STANDARD:
547 case CFI_CMDSET_AMD_EXTENDED:
548 #ifdef CONFIG_FLASH_CFI_LEGACY
549 case CFI_CMDSET_AMD_LEGACY:
551 if (info->sr_supported) {
552 flash_write_cmd(info, sect, info->addr_unlock1,
553 FLASH_CMD_READ_STATUS);
554 retval = !flash_isset(info, sect, 0,
557 retval = flash_toggle(info, sect, 0,
565 debug("%s: %d\n", __func__, retval);
569 /*-----------------------------------------------------------------------
570 * wait for XSR.7 to be set. Time out with an error if it does not.
571 * This routine does not set the flash to read-array mode.
573 static int flash_status_check(flash_info_t *info, flash_sect_t sector,
574 ulong tout, char *prompt)
578 #if CONFIG_SYS_HZ != 1000
579 /* Avoid overflow for large HZ */
580 if ((ulong)CONFIG_SYS_HZ > 100000)
581 tout *= (ulong)CONFIG_SYS_HZ / 1000;
583 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
586 /* Wait for command completion */
587 #ifdef CONFIG_SYS_LOW_RES_TIMER
590 start = get_timer(0);
592 while (flash_is_busy(info, sector)) {
593 if (get_timer(start) > tout) {
594 printf("Flash %s timeout at address %lx data %lx\n",
595 prompt, info->start[sector],
596 flash_read_long(info, sector, 0));
597 flash_write_cmd(info, sector, 0, info->cmd_reset);
601 udelay(1); /* also triggers watchdog */
606 /*-----------------------------------------------------------------------
607 * Wait for XSR.7 to be set, if it times out print an error, otherwise
608 * do a full status check.
610 * This routine sets the flash to read-array mode.
612 static int flash_full_status_check(flash_info_t *info, flash_sect_t sector,
613 ulong tout, char *prompt)
617 retcode = flash_status_check(info, sector, tout, prompt);
618 switch (info->vendor) {
619 case CFI_CMDSET_INTEL_PROG_REGIONS:
620 case CFI_CMDSET_INTEL_EXTENDED:
621 case CFI_CMDSET_INTEL_STANDARD:
622 if (retcode == ERR_OK &&
623 !flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
625 printf("Flash %s error at address %lx\n", prompt,
626 info->start[sector]);
627 if (flash_isset(info, sector, 0, FLASH_STATUS_ECLBS |
628 FLASH_STATUS_PSLBS)) {
629 puts("Command Sequence Error.\n");
630 } else if (flash_isset(info, sector, 0,
631 FLASH_STATUS_ECLBS)) {
632 puts("Block Erase Error.\n");
633 retcode = ERR_NOT_ERASED;
634 } else if (flash_isset(info, sector, 0,
635 FLASH_STATUS_PSLBS)) {
636 puts("Locking Error\n");
638 if (flash_isset(info, sector, 0, FLASH_STATUS_DPS)) {
639 puts("Block locked.\n");
640 retcode = ERR_PROTECTED;
642 if (flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
643 puts("Vpp Low Error.\n");
645 flash_write_cmd(info, sector, 0, info->cmd_reset);
654 static int use_flash_status_poll(flash_info_t *info)
656 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
657 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
658 info->vendor == CFI_CMDSET_AMD_STANDARD)
664 static int flash_status_poll(flash_info_t *info, void *src, void *dst,
665 ulong tout, char *prompt)
667 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
671 #if CONFIG_SYS_HZ != 1000
672 /* Avoid overflow for large HZ */
673 if ((ulong)CONFIG_SYS_HZ > 100000)
674 tout *= (ulong)CONFIG_SYS_HZ / 1000;
676 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
679 /* Wait for command completion */
680 #ifdef CONFIG_SYS_LOW_RES_TIMER
683 start = get_timer(0);
686 switch (info->portwidth) {
688 ready = flash_read8(dst) == flash_read8(src);
690 case FLASH_CFI_16BIT:
691 ready = flash_read16(dst) == flash_read16(src);
693 case FLASH_CFI_32BIT:
694 ready = flash_read32(dst) == flash_read32(src);
696 case FLASH_CFI_64BIT:
697 ready = flash_read64(dst) == flash_read64(src);
705 if (get_timer(start) > tout) {
706 printf("Flash %s timeout at address %lx data %lx\n",
707 prompt, (ulong)dst, (ulong)flash_read8(dst));
710 udelay(1); /* also triggers watchdog */
712 #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
716 /*-----------------------------------------------------------------------
718 static void flash_add_byte(flash_info_t *info, cfiword_t *cword, uchar c)
720 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
723 unsigned long long ll;
726 switch (info->portwidth) {
730 case FLASH_CFI_16BIT:
731 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
734 cword->w16 = (cword->w16 >> 8) | w;
736 cword->w16 = (cword->w16 << 8) | c;
739 case FLASH_CFI_32BIT:
740 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
743 cword->w32 = (cword->w32 >> 8) | l;
745 cword->w32 = (cword->w32 << 8) | c;
748 case FLASH_CFI_64BIT:
749 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
752 cword->w64 = (cword->w64 >> 8) | ll;
754 cword->w64 = (cword->w64 << 8) | c;
761 * Loop through the sector table starting from the previously found sector.
762 * Searches forwards or backwards, dependent on the passed address.
764 static flash_sect_t find_sector(flash_info_t *info, ulong addr)
766 static flash_sect_t saved_sector; /* previously found sector */
767 static flash_info_t *saved_info; /* previously used flash bank */
768 flash_sect_t sector = saved_sector;
770 if (info != saved_info || sector >= info->sector_count)
773 while ((sector < info->sector_count - 1) &&
774 (info->start[sector] < addr))
776 while ((info->start[sector] > addr) && (sector > 0))
778 * also decrements the sector in case of an overshot
783 saved_sector = sector;
788 /*-----------------------------------------------------------------------
790 static int flash_write_cfiword(flash_info_t *info, ulong dest, cfiword_t cword)
792 void *dstaddr = (void *)dest;
794 flash_sect_t sect = 0;
797 /* Check if Flash is (sufficiently) erased */
798 switch (info->portwidth) {
800 flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8);
802 case FLASH_CFI_16BIT:
803 flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16);
805 case FLASH_CFI_32BIT:
806 flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32);
808 case FLASH_CFI_64BIT:
809 flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64);
816 return ERR_NOT_ERASED;
818 /* Disable interrupts which might cause a timeout here */
819 flag = disable_interrupts();
821 switch (info->vendor) {
822 case CFI_CMDSET_INTEL_PROG_REGIONS:
823 case CFI_CMDSET_INTEL_EXTENDED:
824 case CFI_CMDSET_INTEL_STANDARD:
825 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
826 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
828 case CFI_CMDSET_AMD_EXTENDED:
829 case CFI_CMDSET_AMD_STANDARD:
830 sect = find_sector(info, dest);
831 flash_unlock_seq(info, sect);
832 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_WRITE);
835 #ifdef CONFIG_FLASH_CFI_LEGACY
836 case CFI_CMDSET_AMD_LEGACY:
837 sect = find_sector(info, dest);
838 flash_unlock_seq(info, 0);
839 flash_write_cmd(info, 0, info->addr_unlock1, AMD_CMD_WRITE);
845 switch (info->portwidth) {
847 flash_write8(cword.w8, dstaddr);
849 case FLASH_CFI_16BIT:
850 flash_write16(cword.w16, dstaddr);
852 case FLASH_CFI_32BIT:
853 flash_write32(cword.w32, dstaddr);
855 case FLASH_CFI_64BIT:
856 flash_write64(cword.w64, dstaddr);
860 /* re-enable interrupts if necessary */
865 sect = find_sector(info, dest);
867 if (use_flash_status_poll(info))
868 return flash_status_poll(info, &cword, dstaddr,
869 info->write_tout, "write");
871 return flash_full_status_check(info, sect,
872 info->write_tout, "write");
875 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
877 static int flash_write_cfibuffer(flash_info_t *info, ulong dest, uchar *cp,
884 u8 *dst = (u8 *)dest;
891 switch (info->portwidth) {
895 case FLASH_CFI_16BIT:
898 case FLASH_CFI_32BIT:
901 case FLASH_CFI_64BIT:
911 while ((cnt-- > 0) && (flag == 1)) {
912 switch (info->portwidth) {
914 flag = ((flash_read8(dst2) & flash_read8(src)) ==
918 case FLASH_CFI_16BIT:
919 flag = ((flash_read16(dst2) & flash_read16(src)) ==
923 case FLASH_CFI_32BIT:
924 flag = ((flash_read32(dst2) & flash_read32(src)) ==
928 case FLASH_CFI_64BIT:
929 flag = ((flash_read64(dst2) & flash_read64(src)) ==
936 retcode = ERR_NOT_ERASED;
941 sector = find_sector(info, dest);
943 switch (info->vendor) {
944 case CFI_CMDSET_INTEL_PROG_REGIONS:
945 case CFI_CMDSET_INTEL_STANDARD:
946 case CFI_CMDSET_INTEL_EXTENDED:
947 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
948 FLASH_CMD_WRITE_BUFFER_PROG :
949 FLASH_CMD_WRITE_TO_BUFFER;
950 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
951 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
952 flash_write_cmd(info, sector, 0, write_cmd);
953 retcode = flash_status_check(info, sector,
954 info->buffer_write_tout,
956 if (retcode == ERR_OK) {
957 /* reduce the number of loops by the width of
961 flash_write_cmd(info, sector, 0, cnt - 1);
963 switch (info->portwidth) {
965 flash_write8(flash_read8(src), dst);
968 case FLASH_CFI_16BIT:
969 flash_write16(flash_read16(src), dst);
972 case FLASH_CFI_32BIT:
973 flash_write32(flash_read32(src), dst);
976 case FLASH_CFI_64BIT:
977 flash_write64(flash_read64(src), dst);
985 flash_write_cmd(info, sector, 0,
986 FLASH_CMD_WRITE_BUFFER_CONFIRM);
987 retcode = flash_full_status_check(
988 info, sector, info->buffer_write_tout,
994 case CFI_CMDSET_AMD_STANDARD:
995 case CFI_CMDSET_AMD_EXTENDED:
996 flash_unlock_seq(info, sector);
998 #ifdef CONFIG_FLASH_SPANSION_S29WS_N
999 offset = ((unsigned long)dst - info->start[sector]) >> shift;
1001 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
1003 flash_write_cmd(info, sector, offset, cnt - 1);
1005 switch (info->portwidth) {
1006 case FLASH_CFI_8BIT:
1008 flash_write8(flash_read8(src), dst);
1012 case FLASH_CFI_16BIT:
1014 flash_write16(flash_read16(src), dst);
1018 case FLASH_CFI_32BIT:
1020 flash_write32(flash_read32(src), dst);
1024 case FLASH_CFI_64BIT:
1026 flash_write64(flash_read64(src), dst);
1031 retcode = ERR_INVAL;
1035 flash_write_cmd(info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1036 if (use_flash_status_poll(info))
1037 retcode = flash_status_poll(info, src - (1 << shift),
1039 info->buffer_write_tout,
1042 retcode = flash_full_status_check(info, sector,
1043 info->buffer_write_tout,
1048 debug("Unknown Command Set\n");
1049 retcode = ERR_INVAL;
1056 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1058 /*-----------------------------------------------------------------------
1060 int flash_erase(flash_info_t *info, int s_first, int s_last)
1067 if (info->flash_id != FLASH_MAN_CFI) {
1068 puts("Can't erase unknown flash type - aborted\n");
1071 if (s_first < 0 || s_first > s_last) {
1072 puts("- no sectors to erase\n");
1077 for (sect = s_first; sect <= s_last; ++sect)
1078 if (info->protect[sect])
1081 printf("- Warning: %d protected sectors will not be erased!\n",
1083 } else if (flash_verbose) {
1087 for (sect = s_first; sect <= s_last; sect++) {
1093 if (info->protect[sect] == 0) { /* not protected */
1094 #ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1101 * Check if whole sector is erased
1103 size = flash_sector_size(info, sect);
1105 flash = (u32 *)info->start[sect];
1106 /* divide by 4 for longword access */
1108 for (k = 0; k < size; k++) {
1109 if (flash_read32(flash++) != 0xffffffff) {
1120 switch (info->vendor) {
1121 case CFI_CMDSET_INTEL_PROG_REGIONS:
1122 case CFI_CMDSET_INTEL_STANDARD:
1123 case CFI_CMDSET_INTEL_EXTENDED:
1124 flash_write_cmd(info, sect, 0,
1125 FLASH_CMD_CLEAR_STATUS);
1126 flash_write_cmd(info, sect, 0,
1127 FLASH_CMD_BLOCK_ERASE);
1128 flash_write_cmd(info, sect, 0,
1129 FLASH_CMD_ERASE_CONFIRM);
1131 case CFI_CMDSET_AMD_STANDARD:
1132 case CFI_CMDSET_AMD_EXTENDED:
1133 flash_unlock_seq(info, sect);
1134 flash_write_cmd(info, sect,
1136 AMD_CMD_ERASE_START);
1137 flash_unlock_seq(info, sect);
1138 flash_write_cmd(info, sect, 0,
1139 info->cmd_erase_sector);
1141 #ifdef CONFIG_FLASH_CFI_LEGACY
1142 case CFI_CMDSET_AMD_LEGACY:
1143 flash_unlock_seq(info, 0);
1144 flash_write_cmd(info, 0, info->addr_unlock1,
1145 AMD_CMD_ERASE_START);
1146 flash_unlock_seq(info, 0);
1147 flash_write_cmd(info, sect, 0,
1148 AMD_CMD_ERASE_SECTOR);
1152 debug("Unknown flash vendor %d\n",
1157 if (use_flash_status_poll(info)) {
1161 cword.w64 = 0xffffffffffffffffULL;
1162 dest = flash_map(info, sect, 0);
1163 st = flash_status_poll(info, &cword, dest,
1164 info->erase_blk_tout,
1166 flash_unmap(info, sect, 0, dest);
1168 st = flash_full_status_check(info, sect,
1169 info->erase_blk_tout,
1175 else if (flash_verbose)
1186 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1187 static int sector_erased(flash_info_t *info, int i)
1194 * Check if whole sector is erased
1196 size = flash_sector_size(info, i);
1197 flash = (u32 *)info->start[i];
1198 /* divide by 4 for longword access */
1201 for (k = 0; k < size; k++) {
1202 if (flash_read32(flash++) != 0xffffffff)
1203 return 0; /* not erased */
1206 return 1; /* erased */
1208 #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1210 void flash_print_info(flash_info_t *info)
1214 if (info->flash_id != FLASH_MAN_CFI) {
1215 puts("missing or unknown FLASH type\n");
1219 printf("%s flash (%d x %d)",
1221 (info->portwidth << 3), (info->chipwidth << 3));
1222 if (info->size < 1024 * 1024)
1223 printf(" Size: %ld kB in %d Sectors\n",
1224 info->size >> 10, info->sector_count);
1226 printf(" Size: %ld MB in %d Sectors\n",
1227 info->size >> 20, info->sector_count);
1229 switch (info->vendor) {
1230 case CFI_CMDSET_INTEL_PROG_REGIONS:
1231 printf("Intel Prog Regions");
1233 case CFI_CMDSET_INTEL_STANDARD:
1234 printf("Intel Standard");
1236 case CFI_CMDSET_INTEL_EXTENDED:
1237 printf("Intel Extended");
1239 case CFI_CMDSET_AMD_STANDARD:
1240 printf("AMD Standard");
1242 case CFI_CMDSET_AMD_EXTENDED:
1243 printf("AMD Extended");
1245 #ifdef CONFIG_FLASH_CFI_LEGACY
1246 case CFI_CMDSET_AMD_LEGACY:
1247 printf("AMD Legacy");
1251 printf("Unknown (%d)", info->vendor);
1254 printf(" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1255 info->manufacturer_id);
1256 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1258 if ((info->device_id & 0xff) == 0x7E) {
1259 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1262 if (info->vendor == CFI_CMDSET_AMD_STANDARD && info->legacy_unlock)
1263 printf("\n Advanced Sector Protection (PPB) enabled");
1264 printf("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1265 info->erase_blk_tout, info->write_tout);
1266 if (info->buffer_size > 1) {
1267 printf(" Buffer write timeout: %ld ms, ",
1268 info->buffer_write_tout);
1269 printf("buffer size: %d bytes\n", info->buffer_size);
1272 puts("\n Sector Start Addresses:");
1273 for (i = 0; i < info->sector_count; ++i) {
1278 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1279 /* print empty and read-only info */
1280 printf(" %08lX %c %s ",
1282 sector_erased(info, i) ? 'E' : ' ',
1283 info->protect[i] ? "RO" : " ");
1284 #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
1285 printf(" %08lX %s ",
1287 info->protect[i] ? "RO" : " ");
1293 /*-----------------------------------------------------------------------
1294 * This is used in a few places in write_buf() to show programming
1295 * progress. Making it a function is nasty because it needs to do side
1296 * effect updates to digit and dots. Repeated code is nasty too, so
1297 * we define it once here.
1299 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1300 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
1301 if (flash_verbose) { \
1303 if (scale > 0 && dots <= 0) { \
1304 if ((digit % 5) == 0) \
1305 printf("%d", digit / 5); \
1313 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1316 /*-----------------------------------------------------------------------
1317 * Copy memory to flash, returns:
1320 * 2 - Flash not erased
1322 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
1329 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1332 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1333 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1338 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1340 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1341 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1342 CONFIG_FLASH_SHOW_PROGRESS);
1346 /* get lower aligned address */
1347 wp = (addr & ~(info->portwidth - 1));
1349 /* handle unaligned start */
1354 for (i = 0; i < aln; ++i)
1355 flash_add_byte(info, &cword, flash_read8(p + i));
1357 for (; (i < info->portwidth) && (cnt > 0); i++) {
1358 flash_add_byte(info, &cword, *src++);
1361 for (; (cnt == 0) && (i < info->portwidth); ++i)
1362 flash_add_byte(info, &cword, flash_read8(p + i));
1364 rc = flash_write_cfiword(info, wp, cword);
1369 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1372 /* handle the aligned part */
1373 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1374 buffered_size = (info->portwidth / info->chipwidth);
1375 buffered_size *= info->buffer_size;
1376 while (cnt >= info->portwidth) {
1377 /* prohibit buffer write when buffer_size is 1 */
1378 if (info->buffer_size == 1) {
1380 for (i = 0; i < info->portwidth; i++)
1381 flash_add_byte(info, &cword, *src++);
1382 rc = flash_write_cfiword(info, wp, cword);
1385 wp += info->portwidth;
1386 cnt -= info->portwidth;
1390 /* write buffer until next buffered_size aligned boundary */
1391 i = buffered_size - (wp % buffered_size);
1394 rc = flash_write_cfibuffer(info, wp, src, i);
1397 i -= i & (info->portwidth - 1);
1401 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1402 /* Only check every once in a while */
1403 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1407 while (cnt >= info->portwidth) {
1409 for (i = 0; i < info->portwidth; i++)
1410 flash_add_byte(info, &cword, *src++);
1411 rc = flash_write_cfiword(info, wp, cword);
1414 wp += info->portwidth;
1415 cnt -= info->portwidth;
1416 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
1417 /* Only check every once in a while */
1418 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1421 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1427 * handle unaligned tail bytes
1431 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
1432 flash_add_byte(info, &cword, *src++);
1435 for (; i < info->portwidth; ++i)
1436 flash_add_byte(info, &cword, flash_read8(p + i));
1438 return flash_write_cfiword(info, wp, cword);
1441 static inline int manufact_match(flash_info_t *info, u32 manu)
1443 return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
1446 /*-----------------------------------------------------------------------
1448 #ifdef CONFIG_SYS_FLASH_PROTECTION
1450 static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1452 if (manufact_match(info, INTEL_MANUFACT) &&
1453 info->device_id == NUMONYX_256MBIT) {
1456 * "Numonyx Axcell P33/P30 Specification Update" :)
1458 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1459 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1462 * cmd must come before FLASH_CMD_PROTECT + 20us
1463 * Disable interrupts which might cause a timeout here.
1465 int flag = disable_interrupts();
1469 cmd = FLASH_CMD_PROTECT_SET;
1471 cmd = FLASH_CMD_PROTECT_CLEAR;
1473 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
1474 flash_write_cmd(info, sector, 0, cmd);
1475 /* re-enable interrupts if necessary */
1477 enable_interrupts();
1484 int flash_real_protect(flash_info_t *info, long sector, int prot)
1488 switch (info->vendor) {
1489 case CFI_CMDSET_INTEL_PROG_REGIONS:
1490 case CFI_CMDSET_INTEL_STANDARD:
1491 case CFI_CMDSET_INTEL_EXTENDED:
1492 if (!cfi_protect_bugfix(info, sector, prot)) {
1493 flash_write_cmd(info, sector, 0,
1494 FLASH_CMD_CLEAR_STATUS);
1495 flash_write_cmd(info, sector, 0,
1498 flash_write_cmd(info, sector, 0,
1499 FLASH_CMD_PROTECT_SET);
1501 flash_write_cmd(info, sector, 0,
1502 FLASH_CMD_PROTECT_CLEAR);
1505 case CFI_CMDSET_AMD_EXTENDED:
1506 case CFI_CMDSET_AMD_STANDARD:
1507 /* U-Boot only checks the first byte */
1508 if (manufact_match(info, ATM_MANUFACT)) {
1510 flash_unlock_seq(info, 0);
1511 flash_write_cmd(info, 0,
1513 ATM_CMD_SOFTLOCK_START);
1514 flash_unlock_seq(info, 0);
1515 flash_write_cmd(info, sector, 0,
1518 flash_write_cmd(info, 0,
1520 AMD_CMD_UNLOCK_START);
1521 if (info->device_id == ATM_ID_BV6416)
1522 flash_write_cmd(info, sector,
1523 0, ATM_CMD_UNLOCK_SECT);
1526 if (info->legacy_unlock) {
1527 int flag = disable_interrupts();
1530 flash_unlock_seq(info, 0);
1531 flash_write_cmd(info, 0, info->addr_unlock1,
1532 AMD_CMD_SET_PPB_ENTRY);
1533 lock_flag = flash_isset(info, sector, 0, 0x01);
1536 flash_write_cmd(info, sector, 0,
1537 AMD_CMD_PPB_LOCK_BC1);
1538 flash_write_cmd(info, sector, 0,
1539 AMD_CMD_PPB_LOCK_BC2);
1541 debug("sector %ld %slocked\n", sector,
1542 lock_flag ? "" : "already ");
1545 debug("unlock %ld\n", sector);
1546 flash_write_cmd(info, 0, 0,
1547 AMD_CMD_PPB_UNLOCK_BC1);
1548 flash_write_cmd(info, 0, 0,
1549 AMD_CMD_PPB_UNLOCK_BC2);
1551 debug("sector %ld %sunlocked\n", sector,
1552 !lock_flag ? "" : "already ");
1555 enable_interrupts();
1557 if (flash_status_check(info, sector,
1558 info->erase_blk_tout,
1559 prot ? "protect" : "unprotect"))
1560 printf("status check error\n");
1562 flash_write_cmd(info, 0, 0,
1563 AMD_CMD_SET_PPB_EXIT_BC1);
1564 flash_write_cmd(info, 0, 0,
1565 AMD_CMD_SET_PPB_EXIT_BC2);
1568 #ifdef CONFIG_FLASH_CFI_LEGACY
1569 case CFI_CMDSET_AMD_LEGACY:
1570 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1571 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
1573 flash_write_cmd(info, sector, 0,
1574 FLASH_CMD_PROTECT_SET);
1576 flash_write_cmd(info, sector, 0,
1577 FLASH_CMD_PROTECT_CLEAR);
1582 * Flash needs to be in status register read mode for
1583 * flash_full_status_check() to work correctly
1585 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
1586 retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
1587 prot ? "protect" : "unprotect");
1589 info->protect[sector] = prot;
1592 * On some of Intel's flash chips (marked via legacy_unlock)
1593 * unprotect unprotects all locking.
1595 if (prot == 0 && info->legacy_unlock) {
1598 for (i = 0; i < info->sector_count; i++) {
1599 if (info->protect[i])
1600 flash_real_protect(info, i, 1);
1607 /*-----------------------------------------------------------------------
1608 * flash_read_user_serial - read the OneTimeProgramming cells
1610 void flash_read_user_serial(flash_info_t *info, void *buffer, int offset,
1617 src = flash_map(info, 0, FLASH_OFFSET_USER_PROTECTION);
1618 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1619 memcpy(dst, src + offset, len);
1620 flash_write_cmd(info, 0, 0, info->cmd_reset);
1622 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
1626 * flash_read_factory_serial - read the device Id from the protection area
1628 void flash_read_factory_serial(flash_info_t *info, void *buffer, int offset,
1633 src = flash_map(info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1634 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1635 memcpy(buffer, src + offset, len);
1636 flash_write_cmd(info, 0, 0, info->cmd_reset);
1638 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
1641 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1643 /*-----------------------------------------------------------------------
1644 * Reverse the order of the erase regions in the CFI QRY structure.
1645 * This is needed for chips that are either a) correctly detected as
1646 * top-boot, or b) buggy.
1648 static void cfi_reverse_geometry(struct cfi_qry *qry)
1653 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1654 tmp = get_unaligned(&qry->erase_region_info[i]);
1655 put_unaligned(get_unaligned(&qry->erase_region_info[j]),
1656 &qry->erase_region_info[i]);
1657 put_unaligned(tmp, &qry->erase_region_info[j]);
1661 /*-----------------------------------------------------------------------
1662 * read jedec ids from device and set corresponding fields in info struct
1664 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1667 static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1669 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1671 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1672 udelay(1000); /* some flash are slow to respond */
1673 info->manufacturer_id = flash_read_uchar(info,
1674 FLASH_OFFSET_MANUFACTURER_ID);
1675 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1676 flash_read_word(info, FLASH_OFFSET_DEVICE_ID) :
1677 flash_read_uchar(info, FLASH_OFFSET_DEVICE_ID);
1678 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1681 static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1683 info->cmd_reset = FLASH_CMD_RESET;
1685 cmdset_intel_read_jedec_ids(info);
1686 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1688 #ifdef CONFIG_SYS_FLASH_PROTECTION
1689 /* read legacy lock/unlock bit from intel flash */
1690 if (info->ext_addr) {
1691 info->legacy_unlock =
1692 flash_read_uchar(info, info->ext_addr + 5) & 0x08;
1699 static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1705 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1706 flash_unlock_seq(info, 0);
1707 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1708 udelay(1000); /* some flash are slow to respond */
1710 manu_id = flash_read_uchar(info, FLASH_OFFSET_MANUFACTURER_ID);
1711 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1712 while (manu_id == FLASH_CONTINUATION_CODE && bank_id < 0x800) {
1714 manu_id = flash_read_uchar(info,
1715 bank_id | FLASH_OFFSET_MANUFACTURER_ID);
1717 info->manufacturer_id = manu_id;
1719 debug("info->ext_addr = 0x%x, cfi_version = 0x%x\n",
1720 info->ext_addr, info->cfi_version);
1721 if (info->ext_addr && info->cfi_version >= 0x3134) {
1722 /* read software feature (at 0x53) */
1723 feature = flash_read_uchar(info, info->ext_addr + 0x13);
1724 debug("feature = 0x%x\n", feature);
1725 info->sr_supported = feature & 0x1;
1728 switch (info->chipwidth) {
1729 case FLASH_CFI_8BIT:
1730 info->device_id = flash_read_uchar(info,
1731 FLASH_OFFSET_DEVICE_ID);
1732 if (info->device_id == 0x7E) {
1733 /* AMD 3-byte (expanded) device ids */
1734 info->device_id2 = flash_read_uchar(info,
1735 FLASH_OFFSET_DEVICE_ID2);
1736 info->device_id2 <<= 8;
1737 info->device_id2 |= flash_read_uchar(info,
1738 FLASH_OFFSET_DEVICE_ID3);
1741 case FLASH_CFI_16BIT:
1742 info->device_id = flash_read_word(info,
1743 FLASH_OFFSET_DEVICE_ID);
1744 if ((info->device_id & 0xff) == 0x7E) {
1745 /* AMD 3-byte (expanded) device ids */
1746 info->device_id2 = flash_read_uchar(info,
1747 FLASH_OFFSET_DEVICE_ID2);
1748 info->device_id2 <<= 8;
1749 info->device_id2 |= flash_read_uchar(info,
1750 FLASH_OFFSET_DEVICE_ID3);
1756 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1760 static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1762 info->cmd_reset = AMD_CMD_RESET;
1763 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
1765 cmdset_amd_read_jedec_ids(info);
1766 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1768 #ifdef CONFIG_SYS_FLASH_PROTECTION
1769 if (info->ext_addr) {
1770 /* read sector protect/unprotect scheme (at 0x49) */
1771 if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
1772 info->legacy_unlock = 1;
1779 #ifdef CONFIG_FLASH_CFI_LEGACY
1780 static void flash_read_jedec_ids(flash_info_t *info)
1782 info->manufacturer_id = 0;
1783 info->device_id = 0;
1784 info->device_id2 = 0;
1786 switch (info->vendor) {
1787 case CFI_CMDSET_INTEL_PROG_REGIONS:
1788 case CFI_CMDSET_INTEL_STANDARD:
1789 case CFI_CMDSET_INTEL_EXTENDED:
1790 cmdset_intel_read_jedec_ids(info);
1792 case CFI_CMDSET_AMD_STANDARD:
1793 case CFI_CMDSET_AMD_EXTENDED:
1794 cmdset_amd_read_jedec_ids(info);
1801 /*-----------------------------------------------------------------------
1802 * Call board code to request info about non-CFI flash.
1803 * board_flash_get_legacy needs to fill in at least:
1804 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1806 static int flash_detect_legacy(phys_addr_t base, int banknum)
1808 flash_info_t *info = &flash_info[banknum];
1810 if (board_flash_get_legacy(base, banknum, info)) {
1811 /* board code may have filled info completely. If not, we
1812 * use JEDEC ID probing.
1814 if (!info->vendor) {
1816 CFI_CMDSET_AMD_STANDARD,
1817 CFI_CMDSET_INTEL_STANDARD
1821 for (i = 0; i < ARRAY_SIZE(modes); i++) {
1822 info->vendor = modes[i];
1824 (ulong)map_physmem(base,
1827 if (info->portwidth == FLASH_CFI_8BIT &&
1828 info->interface == FLASH_CFI_X8X16) {
1829 info->addr_unlock1 = 0x2AAA;
1830 info->addr_unlock2 = 0x5555;
1832 info->addr_unlock1 = 0x5555;
1833 info->addr_unlock2 = 0x2AAA;
1835 flash_read_jedec_ids(info);
1836 debug("JEDEC PROBE: ID %x %x %x\n",
1837 info->manufacturer_id,
1840 if (jedec_flash_match(info, info->start[0]))
1843 unmap_physmem((void *)info->start[0],
1848 switch (info->vendor) {
1849 case CFI_CMDSET_INTEL_PROG_REGIONS:
1850 case CFI_CMDSET_INTEL_STANDARD:
1851 case CFI_CMDSET_INTEL_EXTENDED:
1852 info->cmd_reset = FLASH_CMD_RESET;
1854 case CFI_CMDSET_AMD_STANDARD:
1855 case CFI_CMDSET_AMD_EXTENDED:
1856 case CFI_CMDSET_AMD_LEGACY:
1857 info->cmd_reset = AMD_CMD_RESET;
1860 info->flash_id = FLASH_MAN_CFI;
1863 return 0; /* use CFI */
1866 static inline int flash_detect_legacy(phys_addr_t base, int banknum)
1868 return 0; /* use CFI */
1872 /*-----------------------------------------------------------------------
1873 * detect if flash is compatible with the Common Flash Interface (CFI)
1874 * http://www.jedec.org/download/search/jesd68.pdf
1876 static void flash_read_cfi(flash_info_t *info, void *buf, unsigned int start,
1882 for (i = 0; i < len; i++)
1883 p[i] = flash_read_uchar(info, start + i);
1886 static void __flash_cmd_reset(flash_info_t *info)
1889 * We do not yet know what kind of commandset to use, so we issue
1890 * the reset command in both Intel and AMD variants, in the hope
1891 * that AMD flash roms ignore the Intel command.
1893 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1895 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1898 void flash_cmd_reset(flash_info_t *info)
1899 __attribute__((weak, alias("__flash_cmd_reset")));
1901 static int __flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
1905 /* Issue FLASH reset command */
1906 flash_cmd_reset(info);
1908 for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
1910 flash_write_cmd(info, 0, flash_offset_cfi[cfi_offset],
1912 if (flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP, 'Q') &&
1913 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
1914 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1915 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1916 sizeof(struct cfi_qry));
1917 info->interface = le16_to_cpu(qry->interface_desc);
1919 info->cfi_offset = flash_offset_cfi[cfi_offset];
1920 debug("device interface is %d\n",
1922 debug("found port %d chip %d ",
1923 info->portwidth, info->chipwidth);
1924 debug("port %d bits chip %d bits\n",
1925 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1926 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1928 /* calculate command offsets as in the Linux driver */
1929 info->addr_unlock1 = 0x555;
1930 info->addr_unlock2 = 0x2aa;
1933 * modify the unlock address if we are
1934 * in compatibility mode
1936 if (/* x8/x16 in x8 mode */
1937 (info->chipwidth == FLASH_CFI_BY8 &&
1938 info->interface == FLASH_CFI_X8X16) ||
1939 /* x16/x32 in x16 mode */
1940 (info->chipwidth == FLASH_CFI_BY16 &&
1941 info->interface == FLASH_CFI_X16X32)) {
1942 info->addr_unlock1 = 0xaaa;
1943 info->addr_unlock2 = 0x555;
1946 info->name = "CFI conformant";
1954 static int flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
1956 debug("flash detect cfi\n");
1958 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
1959 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1960 for (info->chipwidth = FLASH_CFI_BY8;
1961 info->chipwidth <= info->portwidth;
1962 info->chipwidth <<= 1)
1963 if (__flash_detect_cfi(info, qry))
1966 debug("not found\n");
1971 * Manufacturer-specific quirks. Add workarounds for geometry
1972 * reversal, etc. here.
1974 static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1976 /* check if flash geometry needs reversal */
1977 if (qry->num_erase_regions > 1) {
1978 /* reverse geometry if top boot part */
1979 if (info->cfi_version < 0x3131) {
1980 /* CFI < 1.1, try to guess from device id */
1981 if ((info->device_id & 0x80) != 0)
1982 cfi_reverse_geometry(qry);
1983 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1984 /* CFI >= 1.1, deduct from top/bottom flag */
1985 /* note: ext_addr is valid since cfi_version > 0 */
1986 cfi_reverse_geometry(qry);
1991 static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1993 int reverse_geometry = 0;
1995 /* Check the "top boot" bit in the PRI */
1996 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1997 reverse_geometry = 1;
1999 /* AT49BV6416(T) list the erase regions in the wrong order.
2000 * However, the device ID is identical with the non-broken
2001 * AT49BV642D they differ in the high byte.
2003 if (info->device_id == 0xd6 || info->device_id == 0xd2)
2004 reverse_geometry = !reverse_geometry;
2006 if (reverse_geometry)
2007 cfi_reverse_geometry(qry);
2010 static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
2012 /* check if flash geometry needs reversal */
2013 if (qry->num_erase_regions > 1) {
2014 /* reverse geometry if top boot part */
2015 if (info->cfi_version < 0x3131) {
2016 /* CFI < 1.1, guess by device id */
2017 if (info->device_id == 0x22CA || /* M29W320DT */
2018 info->device_id == 0x2256 || /* M29W320ET */
2019 info->device_id == 0x22D7) { /* M29W800DT */
2020 cfi_reverse_geometry(qry);
2022 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
2023 /* CFI >= 1.1, deduct from top/bottom flag */
2024 /* note: ext_addr is valid since cfi_version > 0 */
2025 cfi_reverse_geometry(qry);
2030 static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
2033 * SST, for many recent nor parallel flashes, says they are
2034 * CFI-conformant. This is not true, since qry struct.
2035 * reports a std. AMD command set (0x0002), while SST allows to
2036 * erase two different sector sizes for the same memory.
2037 * 64KB sector (SST call it block) needs 0x30 to be erased.
2038 * 4KB sector (SST call it sector) needs 0x50 to be erased.
2039 * Since CFI query detect the 4KB number of sectors, users expects
2040 * a sector granularity of 4KB, and it is here set.
2042 if (info->device_id == 0x5D23 || /* SST39VF3201B */
2043 info->device_id == 0x5C23) { /* SST39VF3202B */
2044 /* set sector granularity to 4KB */
2045 info->cmd_erase_sector = 0x50;
2049 static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry)
2052 * The M29EW devices seem to report the CFI information wrong
2053 * when it's in 8 bit mode.
2054 * There's an app note from Numonyx on this issue.
2055 * So adjust the buffer size for M29EW while operating in 8-bit mode
2057 if (qry->max_buf_write_size > 0x8 &&
2058 info->device_id == 0x7E &&
2059 (info->device_id2 == 0x2201 ||
2060 info->device_id2 == 0x2301 ||
2061 info->device_id2 == 0x2801 ||
2062 info->device_id2 == 0x4801)) {
2063 debug("Adjusted buffer size on Numonyx flash");
2064 debug(" M29EW family in 8 bit mode\n");
2065 qry->max_buf_write_size = 0x8;
2070 * The following code cannot be run from FLASH!
2073 ulong flash_get_size(phys_addr_t base, int banknum)
2075 flash_info_t *info = &flash_info[banknum];
2077 flash_sect_t sect_cnt;
2081 uchar num_erase_regions;
2082 int erase_region_size;
2083 int erase_region_count;
2085 unsigned long max_size;
2087 memset(&qry, 0, sizeof(qry));
2090 info->cfi_version = 0;
2091 #ifdef CONFIG_SYS_FLASH_PROTECTION
2092 info->legacy_unlock = 0;
2095 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
2097 if (flash_detect_cfi(info, &qry)) {
2098 info->vendor = le16_to_cpu(get_unaligned(&qry.p_id));
2099 info->ext_addr = le16_to_cpu(get_unaligned(&qry.p_adr));
2100 num_erase_regions = qry.num_erase_regions;
2102 if (info->ext_addr) {
2103 info->cfi_version = (ushort)flash_read_uchar(info,
2104 info->ext_addr + 3) << 8;
2105 info->cfi_version |= (ushort)flash_read_uchar(info,
2106 info->ext_addr + 4);
2110 flash_printqry(&qry);
2113 switch (info->vendor) {
2114 case CFI_CMDSET_INTEL_PROG_REGIONS:
2115 case CFI_CMDSET_INTEL_STANDARD:
2116 case CFI_CMDSET_INTEL_EXTENDED:
2117 cmdset_intel_init(info, &qry);
2119 case CFI_CMDSET_AMD_STANDARD:
2120 case CFI_CMDSET_AMD_EXTENDED:
2121 cmdset_amd_init(info, &qry);
2124 printf("CFI: Unknown command set 0x%x\n",
2127 * Unfortunately, this means we don't know how
2128 * to get the chip back to Read mode. Might
2129 * as well try an Intel-style reset...
2131 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2135 /* Do manufacturer-specific fixups */
2136 switch (info->manufacturer_id) {
2137 case 0x0001: /* AMD */
2138 case 0x0037: /* AMIC */
2139 flash_fixup_amd(info, &qry);
2142 flash_fixup_atmel(info, &qry);
2145 flash_fixup_stm(info, &qry);
2147 case 0x00bf: /* SST */
2148 flash_fixup_sst(info, &qry);
2150 case 0x0089: /* Numonyx */
2151 flash_fixup_num(info, &qry);
2155 debug("manufacturer is %d\n", info->vendor);
2156 debug("manufacturer id is 0x%x\n", info->manufacturer_id);
2157 debug("device id is 0x%x\n", info->device_id);
2158 debug("device id2 is 0x%x\n", info->device_id2);
2159 debug("cfi version is 0x%04x\n", info->cfi_version);
2161 size_ratio = info->portwidth / info->chipwidth;
2162 /* if the chip is x8/x16 reduce the ratio by half */
2163 if (info->interface == FLASH_CFI_X8X16 &&
2164 info->chipwidth == FLASH_CFI_BY8) {
2167 debug("size_ratio %d port %d bits chip %d bits\n",
2168 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2169 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
2170 info->size = 1 << qry.dev_size;
2171 /* multiply the size by the number of chips */
2172 info->size *= size_ratio;
2173 max_size = cfi_flash_bank_size(banknum);
2174 if (max_size && info->size > max_size) {
2175 debug("[truncated from %ldMiB]", info->size >> 20);
2176 info->size = max_size;
2178 debug("found %d erase regions\n", num_erase_regions);
2181 for (i = 0; i < num_erase_regions; i++) {
2182 if (i > NUM_ERASE_REGIONS) {
2183 printf("%d erase regions found, only %d used\n",
2184 num_erase_regions, NUM_ERASE_REGIONS);
2188 tmp = le32_to_cpu(get_unaligned(
2189 &qry.erase_region_info[i]));
2190 debug("erase region %u: 0x%08lx\n", i, tmp);
2192 erase_region_count = (tmp & 0xffff) + 1;
2195 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
2196 debug("erase_region_count = %d ", erase_region_count);
2197 debug("erase_region_size = %d\n", erase_region_size);
2198 for (j = 0; j < erase_region_count; j++) {
2199 if (sector - base >= info->size)
2201 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
2202 printf("ERROR: too many flash sectors\n");
2205 info->start[sect_cnt] =
2206 (ulong)map_physmem(sector,
2209 sector += (erase_region_size * size_ratio);
2212 * Only read protection status from
2213 * supported devices (intel...)
2215 switch (info->vendor) {
2216 case CFI_CMDSET_INTEL_PROG_REGIONS:
2217 case CFI_CMDSET_INTEL_EXTENDED:
2218 case CFI_CMDSET_INTEL_STANDARD:
2220 * Set flash to read-id mode. Otherwise
2221 * reading protected status is not
2224 flash_write_cmd(info, sect_cnt, 0,
2226 info->protect[sect_cnt] =
2227 flash_isset(info, sect_cnt,
2228 FLASH_OFFSET_PROTECT,
2229 FLASH_STATUS_PROTECT);
2230 flash_write_cmd(info, sect_cnt, 0,
2233 case CFI_CMDSET_AMD_EXTENDED:
2234 case CFI_CMDSET_AMD_STANDARD:
2235 if (!info->legacy_unlock) {
2236 /* default: not protected */
2237 info->protect[sect_cnt] = 0;
2241 /* Read protection (PPB) from sector */
2242 flash_write_cmd(info, 0, 0,
2244 flash_unlock_seq(info, 0);
2245 flash_write_cmd(info, 0,
2248 info->protect[sect_cnt] =
2251 FLASH_OFFSET_PROTECT,
2252 FLASH_STATUS_PROTECT);
2255 /* default: not protected */
2256 info->protect[sect_cnt] = 0;
2263 info->sector_count = sect_cnt;
2264 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2265 tmp = 1 << qry.block_erase_timeout_typ;
2266 info->erase_blk_tout = tmp *
2267 (1 << qry.block_erase_timeout_max);
2268 tmp = (1 << qry.buf_write_timeout_typ) *
2269 (1 << qry.buf_write_timeout_max);
2271 /* round up when converting to ms */
2272 info->buffer_write_tout = (tmp + 999) / 1000;
2273 tmp = (1 << qry.word_write_timeout_typ) *
2274 (1 << qry.word_write_timeout_max);
2275 /* round up when converting to ms */
2276 info->write_tout = (tmp + 999) / 1000;
2277 info->flash_id = FLASH_MAN_CFI;
2278 if (info->interface == FLASH_CFI_X8X16 &&
2279 info->chipwidth == FLASH_CFI_BY8) {
2280 /* XXX - Need to test on x8/x16 in parallel. */
2281 info->portwidth >>= 1;
2284 flash_write_cmd(info, 0, 0, info->cmd_reset);
2287 return (info->size);
2290 #ifdef CONFIG_FLASH_CFI_MTD
2291 void flash_set_verbose(uint v)
2297 static void cfi_flash_set_config_reg(u32 base, u16 val)
2299 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2301 * Only set this config register if really defined
2302 * to a valid value (0xffff is invalid)
2308 * Set configuration register. Data is "encrypted" in the 16 lower
2311 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2312 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2315 * Finally issue reset-command to bring device back to
2318 flash_write16(FLASH_CMD_RESET, (void *)base);
2322 /*-----------------------------------------------------------------------
2325 static void flash_protect_default(void)
2327 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2332 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2335 /* Monitor protection ON by default */
2336 #if defined(CONFIG_SYS_MONITOR_BASE) && \
2337 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2338 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2339 flash_protect(FLAG_PROTECT_SET,
2340 CONFIG_SYS_MONITOR_BASE,
2341 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2342 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2345 /* Environment protection ON by default */
2346 #ifdef CONFIG_ENV_IS_IN_FLASH
2347 flash_protect(FLAG_PROTECT_SET,
2349 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2350 flash_get_info(CONFIG_ENV_ADDR));
2353 /* Redundant environment protection ON by default */
2354 #ifdef CONFIG_ENV_ADDR_REDUND
2355 flash_protect(FLAG_PROTECT_SET,
2356 CONFIG_ENV_ADDR_REDUND,
2357 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2358 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2361 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2362 for (i = 0; i < ARRAY_SIZE(apl); i++) {
2363 debug("autoprotecting from %08lx to %08lx\n",
2364 apl[i].start, apl[i].start + apl[i].size - 1);
2365 flash_protect(FLAG_PROTECT_SET,
2367 apl[i].start + apl[i].size - 1,
2368 flash_get_info(apl[i].start));
2373 unsigned long flash_init(void)
2375 unsigned long size = 0;
2378 #ifdef CONFIG_SYS_FLASH_PROTECTION
2379 /* read environment from EEPROM */
2382 env_get_f("unlock", s, sizeof(s));
2385 #ifdef CONFIG_CFI_FLASH /* for driver model */
2386 cfi_flash_init_dm();
2389 /* Init: no FLASHes known */
2390 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
2391 flash_info[i].flash_id = FLASH_UNKNOWN;
2393 /* Optionally write flash configuration register */
2394 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2395 cfi_flash_config_reg(i));
2397 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
2398 flash_get_size(cfi_flash_bank_addr(i), i);
2399 size += flash_info[i].size;
2400 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
2401 #ifndef CONFIG_SYS_FLASH_QUIET_TEST
2402 printf("## Unknown flash on Bank %d ", i + 1);
2403 printf("- Size = 0x%08lx = %ld MB\n",
2405 flash_info[i].size >> 20);
2406 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
2408 #ifdef CONFIG_SYS_FLASH_PROTECTION
2409 else if (strcmp(s, "yes") == 0) {
2411 * Only the U-Boot image and it's environment
2412 * is protected, all other sectors are
2413 * unprotected (unlocked) if flash hardware
2414 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
2415 * and the environment variable "unlock" is
2418 if (flash_info[i].legacy_unlock) {
2422 * Disable legacy_unlock temporarily,
2423 * since flash_real_protect would
2424 * relock all other sectors again
2427 flash_info[i].legacy_unlock = 0;
2430 * Legacy unlocking (e.g. Intel J3) ->
2431 * unlock only one sector. This will
2432 * unlock all sectors.
2434 flash_real_protect(&flash_info[i], 0, 0);
2436 flash_info[i].legacy_unlock = 1;
2439 * Manually mark other sectors as
2440 * unlocked (unprotected)
2442 for (k = 1; k < flash_info[i].sector_count; k++)
2443 flash_info[i].protect[k] = 0;
2446 * No legancy unlocking -> unlock all sectors
2448 flash_protect(FLAG_PROTECT_CLEAR,
2449 flash_info[i].start[0],
2450 flash_info[i].start[0]
2451 + flash_info[i].size - 1,
2455 #endif /* CONFIG_SYS_FLASH_PROTECTION */
2458 flash_protect_default();
2459 #ifdef CONFIG_FLASH_CFI_MTD
2466 #ifdef CONFIG_CFI_FLASH /* for driver model */
2467 static int cfi_flash_probe(struct udevice *dev)
2469 const fdt32_t *cell;
2473 addrc = dev_read_addr_cells(dev);
2474 sizec = dev_read_size_cells(dev);
2476 /* decode regs; there may be multiple reg tuples. */
2477 cell = dev_read_prop(dev, "reg", &len);
2481 len /= sizeof(fdt32_t);
2485 addr = dev_translate_address(dev, cell + idx);
2487 flash_info[cfi_flash_num_flash_banks].dev = dev;
2488 flash_info[cfi_flash_num_flash_banks].base = addr;
2489 cfi_flash_num_flash_banks++;
2491 idx += addrc + sizec;
2493 gd->bd->bi_flashstart = flash_info[0].base;
2498 static const struct udevice_id cfi_flash_ids[] = {
2499 { .compatible = "cfi-flash" },
2500 { .compatible = "jedec-flash" },
2504 U_BOOT_DRIVER(cfi_flash) = {
2505 .name = "cfi_flash",
2507 .of_match = cfi_flash_ids,
2508 .probe = cfi_flash_probe,
2510 #endif /* CONFIG_CFI_FLASH */