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>
29 #include <asm/processor.h>
31 #include <asm/byteorder.h>
32 #include <asm/unaligned.h>
33 #include <env_internal.h>
34 #include <mtd/cfi_flash.h>
38 * This file implements a Common Flash Interface (CFI) driver for
41 * The width of the port and the width of the chips are determined at
42 * initialization. These widths are used to calculate the address for
43 * access CFI data structures.
46 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
47 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
48 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
49 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
50 * AMD CFI Specification, Release 2.0 December 1, 2001
51 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
52 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
54 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
55 * reading and writing ... (yes there is such a Hardware).
58 DECLARE_GLOBAL_DATA_PTR;
60 static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
61 #ifdef CONFIG_FLASH_CFI_MTD
62 static uint flash_verbose = 1;
64 #define flash_verbose 1
67 flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
70 * Check if chip width is defined. If not, start detecting with 8bit.
72 #ifndef CONFIG_SYS_FLASH_CFI_WIDTH
73 #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
76 #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
77 #define __maybe_weak __weak
79 #define __maybe_weak static
83 * 0xffff is an undefined value for the configuration register. When
84 * this value is returned, the configuration register shall not be
85 * written at all (default mode).
87 static u16 cfi_flash_config_reg(int i)
89 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
90 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
96 #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
97 int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
99 int cfi_flash_num_flash_banks;
102 #ifdef CONFIG_CFI_FLASH /* for driver model */
103 static void cfi_flash_init_dm(void)
107 cfi_flash_num_flash_banks = 0;
109 * The uclass_first_device() will probe the first device and
110 * uclass_next_device() will probe the rest if they exist. So
111 * that cfi_flash_probe() will get called assigning the base
112 * addresses that are available.
114 for (uclass_first_device(UCLASS_MTD, &dev);
116 uclass_next_device(&dev)) {
120 phys_addr_t cfi_flash_bank_addr(int i)
122 return flash_info[i].base;
125 __weak phys_addr_t cfi_flash_bank_addr(int i)
127 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
131 __weak unsigned long cfi_flash_bank_size(int i)
133 #ifdef CONFIG_SYS_FLASH_BANKS_SIZES
134 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
140 __maybe_weak void flash_write8(u8 value, void *addr)
142 __raw_writeb(value, addr);
145 __maybe_weak void flash_write16(u16 value, void *addr)
147 __raw_writew(value, addr);
150 __maybe_weak void flash_write32(u32 value, void *addr)
152 __raw_writel(value, addr);
155 __maybe_weak void flash_write64(u64 value, void *addr)
157 /* No architectures currently implement __raw_writeq() */
158 *(volatile u64 *)addr = value;
161 __maybe_weak u8 flash_read8(void *addr)
163 return __raw_readb(addr);
166 __maybe_weak u16 flash_read16(void *addr)
168 return __raw_readw(addr);
171 __maybe_weak u32 flash_read32(void *addr)
173 return __raw_readl(addr);
176 __maybe_weak u64 flash_read64(void *addr)
178 /* No architectures currently implement __raw_readq() */
179 return *(volatile u64 *)addr;
182 /*-----------------------------------------------------------------------
184 #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || \
185 (defined(CONFIG_SYS_MONITOR_BASE) && \
186 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE))
187 static flash_info_t *flash_get_info(ulong base)
192 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
193 info = &flash_info[i];
194 if (info->size && info->start[0] <= base &&
195 base <= info->start[0] + info->size - 1)
203 unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
205 if (sect != (info->sector_count - 1))
206 return info->start[sect + 1] - info->start[sect];
208 return info->start[0] + info->size - info->start[sect];
211 /*-----------------------------------------------------------------------
212 * create an address based on the offset and the port width
215 flash_map(flash_info_t *info, flash_sect_t sect, uint offset)
217 unsigned int byte_offset = offset * info->portwidth;
219 return (void *)(info->start[sect] + byte_offset);
222 static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
223 unsigned int offset, void *addr)
227 /*-----------------------------------------------------------------------
228 * make a proper sized command based on the port and chip widths
230 static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
235 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
236 u32 cmd_le = cpu_to_le32(cmd);
239 uchar *cp = (uchar *) cmdbuf;
241 for (i = info->portwidth; i > 0; i--) {
242 cword_offset = (info->portwidth - i) % info->chipwidth;
243 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
244 cp_offset = info->portwidth - i;
245 val = *((uchar *)&cmd_le + cword_offset);
248 val = *((uchar *)&cmd + sizeof(u32) - cword_offset - 1);
250 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
255 /*-----------------------------------------------------------------------
258 static void print_longlong(char *str, unsigned long long data)
264 for (i = 0; i < 8; i++)
265 sprintf(&str[i * 2], "%2.2x", *cp++);
268 static void flash_printqry(struct cfi_qry *qry)
273 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
275 for (y = 0; y < 16; y++)
276 debug("%2.2x ", p[x + y]);
278 for (y = 0; y < 16; y++) {
279 unsigned char c = p[x + y];
281 if (c >= 0x20 && c <= 0x7e)
291 /*-----------------------------------------------------------------------
292 * read a character at a port width address
294 static inline uchar flash_read_uchar(flash_info_t *info, uint offset)
299 cp = flash_map(info, 0, offset);
300 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
301 retval = flash_read8(cp);
303 retval = flash_read8(cp + info->portwidth - 1);
305 flash_unmap(info, 0, offset, cp);
309 /*-----------------------------------------------------------------------
310 * read a word at a port width address, assume 16bit bus
312 static inline ushort flash_read_word(flash_info_t *info, uint offset)
314 ushort *addr, retval;
316 addr = flash_map(info, 0, offset);
317 retval = flash_read16(addr);
318 flash_unmap(info, 0, offset, addr);
322 /*-----------------------------------------------------------------------
323 * read a long word by picking the least significant byte of each maximum
324 * port size word. Swap for ppc format.
326 static ulong flash_read_long (flash_info_t *info, flash_sect_t sect,
335 addr = flash_map(info, sect, offset);
338 debug("long addr is at %p info->portwidth = %d\n", addr,
340 for (x = 0; x < 4 * info->portwidth; x++)
341 debug("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
343 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
344 retval = ((flash_read8(addr) << 16) |
345 (flash_read8(addr + info->portwidth) << 24) |
346 (flash_read8(addr + 2 * info->portwidth)) |
347 (flash_read8(addr + 3 * info->portwidth) << 8));
349 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
350 (flash_read8(addr + info->portwidth - 1) << 16) |
351 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
352 (flash_read8(addr + 3 * info->portwidth - 1)));
354 flash_unmap(info, sect, offset, addr);
360 * Write a proper sized command to the correct address
362 static void flash_write_cmd(flash_info_t *info, flash_sect_t sect,
363 uint offset, u32 cmd)
368 addr = flash_map(info, sect, offset);
369 flash_make_cmd(info, cmd, &cword);
370 switch (info->portwidth) {
372 debug("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
373 cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
374 flash_write8(cword.w8, addr);
376 case FLASH_CFI_16BIT:
377 debug("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
379 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
380 flash_write16(cword.w16, addr);
382 case FLASH_CFI_32BIT:
383 debug("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr,
385 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
386 flash_write32(cword.w32, addr);
388 case FLASH_CFI_64BIT:
393 print_longlong(str, cword.w64);
395 debug("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
397 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
400 flash_write64(cword.w64, addr);
404 /* Ensure all the instructions are fully finished */
407 flash_unmap(info, sect, offset, addr);
410 static void flash_unlock_seq(flash_info_t *info, flash_sect_t sect)
412 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
413 flash_write_cmd(info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
416 /*-----------------------------------------------------------------------
418 static int flash_isequal(flash_info_t *info, flash_sect_t sect, uint offset,
425 addr = flash_map(info, sect, offset);
426 flash_make_cmd(info, cmd, &cword);
428 debug("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
429 switch (info->portwidth) {
431 debug("is= %x %x\n", flash_read8(addr), cword.w8);
432 retval = (flash_read8(addr) == cword.w8);
434 case FLASH_CFI_16BIT:
435 debug("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16);
436 retval = (flash_read16(addr) == cword.w16);
438 case FLASH_CFI_32BIT:
439 debug("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32);
440 retval = (flash_read32(addr) == cword.w32);
442 case FLASH_CFI_64BIT:
448 print_longlong(str1, flash_read64(addr));
449 print_longlong(str2, cword.w64);
450 debug("is= %s %s\n", str1, str2);
453 retval = (flash_read64(addr) == cword.w64);
459 flash_unmap(info, sect, offset, addr);
464 /*-----------------------------------------------------------------------
466 static int flash_isset(flash_info_t *info, flash_sect_t sect, uint offset,
473 addr = flash_map(info, sect, offset);
474 flash_make_cmd(info, cmd, &cword);
475 switch (info->portwidth) {
477 retval = ((flash_read8(addr) & cword.w8) == cword.w8);
479 case FLASH_CFI_16BIT:
480 retval = ((flash_read16(addr) & cword.w16) == cword.w16);
482 case FLASH_CFI_32BIT:
483 retval = ((flash_read32(addr) & cword.w32) == cword.w32);
485 case FLASH_CFI_64BIT:
486 retval = ((flash_read64(addr) & cword.w64) == cword.w64);
492 flash_unmap(info, sect, offset, addr);
497 /*-----------------------------------------------------------------------
499 static int flash_toggle(flash_info_t *info, flash_sect_t sect, uint offset,
506 addr = flash_map(info, sect, offset);
507 flash_make_cmd(info, cmd, &cword);
508 switch (info->portwidth) {
510 retval = flash_read8(addr) != flash_read8(addr);
512 case FLASH_CFI_16BIT:
513 retval = flash_read16(addr) != flash_read16(addr);
515 case FLASH_CFI_32BIT:
516 retval = flash_read32(addr) != flash_read32(addr);
518 case FLASH_CFI_64BIT:
519 retval = ((flash_read32(addr) != flash_read32(addr)) ||
520 (flash_read32(addr + 4) != flash_read32(addr + 4)));
526 flash_unmap(info, sect, offset, addr);
532 * flash_is_busy - check to see if the flash is busy
534 * This routine checks the status of the chip and returns true if the
537 static int flash_is_busy(flash_info_t *info, flash_sect_t sect)
541 switch (info->vendor) {
542 case CFI_CMDSET_INTEL_PROG_REGIONS:
543 case CFI_CMDSET_INTEL_STANDARD:
544 case CFI_CMDSET_INTEL_EXTENDED:
545 retval = !flash_isset(info, sect, 0, FLASH_STATUS_DONE);
547 case CFI_CMDSET_AMD_STANDARD:
548 case CFI_CMDSET_AMD_EXTENDED:
549 #ifdef CONFIG_FLASH_CFI_LEGACY
550 case CFI_CMDSET_AMD_LEGACY:
552 if (info->sr_supported) {
553 flash_write_cmd(info, sect, info->addr_unlock1,
554 FLASH_CMD_READ_STATUS);
555 retval = !flash_isset(info, sect, 0,
558 retval = flash_toggle(info, sect, 0,
566 debug("%s: %d\n", __func__, retval);
570 /*-----------------------------------------------------------------------
571 * wait for XSR.7 to be set. Time out with an error if it does not.
572 * This routine does not set the flash to read-array mode.
574 static int flash_status_check(flash_info_t *info, flash_sect_t sector,
575 ulong tout, char *prompt)
579 #if CONFIG_SYS_HZ != 1000
580 /* Avoid overflow for large HZ */
581 if ((ulong)CONFIG_SYS_HZ > 100000)
582 tout *= (ulong)CONFIG_SYS_HZ / 1000;
584 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
587 /* Wait for command completion */
588 #ifdef CONFIG_SYS_LOW_RES_TIMER
591 start = get_timer(0);
593 while (flash_is_busy(info, sector)) {
594 if (get_timer(start) > tout) {
595 printf("Flash %s timeout at address %lx data %lx\n",
596 prompt, info->start[sector],
597 flash_read_long(info, sector, 0));
598 flash_write_cmd(info, sector, 0, info->cmd_reset);
602 udelay(1); /* also triggers watchdog */
607 /*-----------------------------------------------------------------------
608 * Wait for XSR.7 to be set, if it times out print an error, otherwise
609 * do a full status check.
611 * This routine sets the flash to read-array mode.
613 static int flash_full_status_check(flash_info_t *info, flash_sect_t sector,
614 ulong tout, char *prompt)
618 retcode = flash_status_check(info, sector, tout, prompt);
619 switch (info->vendor) {
620 case CFI_CMDSET_INTEL_PROG_REGIONS:
621 case CFI_CMDSET_INTEL_EXTENDED:
622 case CFI_CMDSET_INTEL_STANDARD:
623 if (retcode == ERR_OK &&
624 !flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
626 printf("Flash %s error at address %lx\n", prompt,
627 info->start[sector]);
628 if (flash_isset(info, sector, 0, FLASH_STATUS_ECLBS |
629 FLASH_STATUS_PSLBS)) {
630 puts("Command Sequence Error.\n");
631 } else if (flash_isset(info, sector, 0,
632 FLASH_STATUS_ECLBS)) {
633 puts("Block Erase Error.\n");
634 retcode = ERR_NOT_ERASED;
635 } else if (flash_isset(info, sector, 0,
636 FLASH_STATUS_PSLBS)) {
637 puts("Locking Error\n");
639 if (flash_isset(info, sector, 0, FLASH_STATUS_DPS)) {
640 puts("Block locked.\n");
641 retcode = ERR_PROTECTED;
643 if (flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
644 puts("Vpp Low Error.\n");
646 flash_write_cmd(info, sector, 0, info->cmd_reset);
655 static int use_flash_status_poll(flash_info_t *info)
657 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
658 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
659 info->vendor == CFI_CMDSET_AMD_STANDARD)
665 static int flash_status_poll(flash_info_t *info, void *src, void *dst,
666 ulong tout, char *prompt)
668 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
672 #if CONFIG_SYS_HZ != 1000
673 /* Avoid overflow for large HZ */
674 if ((ulong)CONFIG_SYS_HZ > 100000)
675 tout *= (ulong)CONFIG_SYS_HZ / 1000;
677 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
680 /* Wait for command completion */
681 #ifdef CONFIG_SYS_LOW_RES_TIMER
684 start = get_timer(0);
687 switch (info->portwidth) {
689 ready = flash_read8(dst) == flash_read8(src);
691 case FLASH_CFI_16BIT:
692 ready = flash_read16(dst) == flash_read16(src);
694 case FLASH_CFI_32BIT:
695 ready = flash_read32(dst) == flash_read32(src);
697 case FLASH_CFI_64BIT:
698 ready = flash_read64(dst) == flash_read64(src);
706 if (get_timer(start) > tout) {
707 printf("Flash %s timeout at address %lx data %lx\n",
708 prompt, (ulong)dst, (ulong)flash_read8(dst));
711 udelay(1); /* also triggers watchdog */
713 #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
717 /*-----------------------------------------------------------------------
719 static void flash_add_byte(flash_info_t *info, cfiword_t *cword, uchar c)
721 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
724 unsigned long long ll;
727 switch (info->portwidth) {
731 case FLASH_CFI_16BIT:
732 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
735 cword->w16 = (cword->w16 >> 8) | w;
737 cword->w16 = (cword->w16 << 8) | c;
740 case FLASH_CFI_32BIT:
741 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
744 cword->w32 = (cword->w32 >> 8) | l;
746 cword->w32 = (cword->w32 << 8) | c;
749 case FLASH_CFI_64BIT:
750 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
753 cword->w64 = (cword->w64 >> 8) | ll;
755 cword->w64 = (cword->w64 << 8) | c;
762 * Loop through the sector table starting from the previously found sector.
763 * Searches forwards or backwards, dependent on the passed address.
765 static flash_sect_t find_sector(flash_info_t *info, ulong addr)
767 static flash_sect_t saved_sector; /* previously found sector */
768 static flash_info_t *saved_info; /* previously used flash bank */
769 flash_sect_t sector = saved_sector;
771 if (info != saved_info || sector >= info->sector_count)
774 while ((sector < info->sector_count - 1) &&
775 (info->start[sector] < addr))
777 while ((info->start[sector] > addr) && (sector > 0))
779 * also decrements the sector in case of an overshot
784 saved_sector = sector;
789 /*-----------------------------------------------------------------------
791 static int flash_write_cfiword(flash_info_t *info, ulong dest, cfiword_t cword)
793 void *dstaddr = (void *)dest;
795 flash_sect_t sect = 0;
798 /* Check if Flash is (sufficiently) erased */
799 switch (info->portwidth) {
801 flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8);
803 case FLASH_CFI_16BIT:
804 flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16);
806 case FLASH_CFI_32BIT:
807 flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32);
809 case FLASH_CFI_64BIT:
810 flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64);
817 return ERR_NOT_ERASED;
819 /* Disable interrupts which might cause a timeout here */
820 flag = disable_interrupts();
822 switch (info->vendor) {
823 case CFI_CMDSET_INTEL_PROG_REGIONS:
824 case CFI_CMDSET_INTEL_EXTENDED:
825 case CFI_CMDSET_INTEL_STANDARD:
826 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
827 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
829 case CFI_CMDSET_AMD_EXTENDED:
830 case CFI_CMDSET_AMD_STANDARD:
831 sect = find_sector(info, dest);
832 flash_unlock_seq(info, sect);
833 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_WRITE);
836 #ifdef CONFIG_FLASH_CFI_LEGACY
837 case CFI_CMDSET_AMD_LEGACY:
838 sect = find_sector(info, dest);
839 flash_unlock_seq(info, 0);
840 flash_write_cmd(info, 0, info->addr_unlock1, AMD_CMD_WRITE);
846 switch (info->portwidth) {
848 flash_write8(cword.w8, dstaddr);
850 case FLASH_CFI_16BIT:
851 flash_write16(cword.w16, dstaddr);
853 case FLASH_CFI_32BIT:
854 flash_write32(cword.w32, dstaddr);
856 case FLASH_CFI_64BIT:
857 flash_write64(cword.w64, dstaddr);
861 /* re-enable interrupts if necessary */
866 sect = find_sector(info, dest);
868 if (use_flash_status_poll(info))
869 return flash_status_poll(info, &cword, dstaddr,
870 info->write_tout, "write");
872 return flash_full_status_check(info, sect,
873 info->write_tout, "write");
876 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
878 static int flash_write_cfibuffer(flash_info_t *info, ulong dest, uchar *cp,
885 u8 *dst = (u8 *)dest;
892 switch (info->portwidth) {
896 case FLASH_CFI_16BIT:
899 case FLASH_CFI_32BIT:
902 case FLASH_CFI_64BIT:
912 while ((cnt-- > 0) && (flag == 1)) {
913 switch (info->portwidth) {
915 flag = ((flash_read8(dst2) & flash_read8(src)) ==
919 case FLASH_CFI_16BIT:
920 flag = ((flash_read16(dst2) & flash_read16(src)) ==
924 case FLASH_CFI_32BIT:
925 flag = ((flash_read32(dst2) & flash_read32(src)) ==
929 case FLASH_CFI_64BIT:
930 flag = ((flash_read64(dst2) & flash_read64(src)) ==
937 retcode = ERR_NOT_ERASED;
942 sector = find_sector(info, dest);
944 switch (info->vendor) {
945 case CFI_CMDSET_INTEL_PROG_REGIONS:
946 case CFI_CMDSET_INTEL_STANDARD:
947 case CFI_CMDSET_INTEL_EXTENDED:
948 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
949 FLASH_CMD_WRITE_BUFFER_PROG :
950 FLASH_CMD_WRITE_TO_BUFFER;
951 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
952 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
953 flash_write_cmd(info, sector, 0, write_cmd);
954 retcode = flash_status_check(info, sector,
955 info->buffer_write_tout,
957 if (retcode == ERR_OK) {
958 /* reduce the number of loops by the width of
962 flash_write_cmd(info, sector, 0, cnt - 1);
964 switch (info->portwidth) {
966 flash_write8(flash_read8(src), dst);
969 case FLASH_CFI_16BIT:
970 flash_write16(flash_read16(src), dst);
973 case FLASH_CFI_32BIT:
974 flash_write32(flash_read32(src), dst);
977 case FLASH_CFI_64BIT:
978 flash_write64(flash_read64(src), dst);
986 flash_write_cmd(info, sector, 0,
987 FLASH_CMD_WRITE_BUFFER_CONFIRM);
988 retcode = flash_full_status_check(
989 info, sector, info->buffer_write_tout,
995 case CFI_CMDSET_AMD_STANDARD:
996 case CFI_CMDSET_AMD_EXTENDED:
997 flash_unlock_seq(info, sector);
999 #ifdef CONFIG_FLASH_SPANSION_S29WS_N
1000 offset = ((unsigned long)dst - info->start[sector]) >> shift;
1002 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
1004 flash_write_cmd(info, sector, offset, cnt - 1);
1006 switch (info->portwidth) {
1007 case FLASH_CFI_8BIT:
1009 flash_write8(flash_read8(src), dst);
1013 case FLASH_CFI_16BIT:
1015 flash_write16(flash_read16(src), dst);
1019 case FLASH_CFI_32BIT:
1021 flash_write32(flash_read32(src), dst);
1025 case FLASH_CFI_64BIT:
1027 flash_write64(flash_read64(src), dst);
1032 retcode = ERR_INVAL;
1036 flash_write_cmd(info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1037 if (use_flash_status_poll(info))
1038 retcode = flash_status_poll(info, src - (1 << shift),
1040 info->buffer_write_tout,
1043 retcode = flash_full_status_check(info, sector,
1044 info->buffer_write_tout,
1049 debug("Unknown Command Set\n");
1050 retcode = ERR_INVAL;
1057 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1059 /*-----------------------------------------------------------------------
1061 int flash_erase(flash_info_t *info, int s_first, int s_last)
1068 if (info->flash_id != FLASH_MAN_CFI) {
1069 puts("Can't erase unknown flash type - aborted\n");
1072 if (s_first < 0 || s_first > s_last) {
1073 puts("- no sectors to erase\n");
1078 for (sect = s_first; sect <= s_last; ++sect)
1079 if (info->protect[sect])
1082 printf("- Warning: %d protected sectors will not be erased!\n",
1084 } else if (flash_verbose) {
1088 for (sect = s_first; sect <= s_last; sect++) {
1094 if (info->protect[sect] == 0) { /* not protected */
1095 #ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1102 * Check if whole sector is erased
1104 size = flash_sector_size(info, sect);
1106 flash = (u32 *)info->start[sect];
1107 /* divide by 4 for longword access */
1109 for (k = 0; k < size; k++) {
1110 if (flash_read32(flash++) != 0xffffffff) {
1121 switch (info->vendor) {
1122 case CFI_CMDSET_INTEL_PROG_REGIONS:
1123 case CFI_CMDSET_INTEL_STANDARD:
1124 case CFI_CMDSET_INTEL_EXTENDED:
1125 flash_write_cmd(info, sect, 0,
1126 FLASH_CMD_CLEAR_STATUS);
1127 flash_write_cmd(info, sect, 0,
1128 FLASH_CMD_BLOCK_ERASE);
1129 flash_write_cmd(info, sect, 0,
1130 FLASH_CMD_ERASE_CONFIRM);
1132 case CFI_CMDSET_AMD_STANDARD:
1133 case CFI_CMDSET_AMD_EXTENDED:
1134 flash_unlock_seq(info, sect);
1135 flash_write_cmd(info, sect,
1137 AMD_CMD_ERASE_START);
1138 flash_unlock_seq(info, sect);
1139 flash_write_cmd(info, sect, 0,
1140 info->cmd_erase_sector);
1142 #ifdef CONFIG_FLASH_CFI_LEGACY
1143 case CFI_CMDSET_AMD_LEGACY:
1144 flash_unlock_seq(info, 0);
1145 flash_write_cmd(info, 0, info->addr_unlock1,
1146 AMD_CMD_ERASE_START);
1147 flash_unlock_seq(info, 0);
1148 flash_write_cmd(info, sect, 0,
1149 AMD_CMD_ERASE_SECTOR);
1153 debug("Unknown flash vendor %d\n",
1158 if (use_flash_status_poll(info)) {
1162 cword.w64 = 0xffffffffffffffffULL;
1163 dest = flash_map(info, sect, 0);
1164 st = flash_status_poll(info, &cword, dest,
1165 info->erase_blk_tout,
1167 flash_unmap(info, sect, 0, dest);
1169 st = flash_full_status_check(info, sect,
1170 info->erase_blk_tout,
1176 else if (flash_verbose)
1187 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1188 static int sector_erased(flash_info_t *info, int i)
1195 * Check if whole sector is erased
1197 size = flash_sector_size(info, i);
1198 flash = (u32 *)info->start[i];
1199 /* divide by 4 for longword access */
1202 for (k = 0; k < size; k++) {
1203 if (flash_read32(flash++) != 0xffffffff)
1204 return 0; /* not erased */
1207 return 1; /* erased */
1209 #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1211 void flash_print_info(flash_info_t *info)
1215 if (info->flash_id != FLASH_MAN_CFI) {
1216 puts("missing or unknown FLASH type\n");
1220 printf("%s flash (%d x %d)",
1222 (info->portwidth << 3), (info->chipwidth << 3));
1223 if (info->size < 1024 * 1024)
1224 printf(" Size: %ld kB in %d Sectors\n",
1225 info->size >> 10, info->sector_count);
1227 printf(" Size: %ld MB in %d Sectors\n",
1228 info->size >> 20, info->sector_count);
1230 switch (info->vendor) {
1231 case CFI_CMDSET_INTEL_PROG_REGIONS:
1232 printf("Intel Prog Regions");
1234 case CFI_CMDSET_INTEL_STANDARD:
1235 printf("Intel Standard");
1237 case CFI_CMDSET_INTEL_EXTENDED:
1238 printf("Intel Extended");
1240 case CFI_CMDSET_AMD_STANDARD:
1241 printf("AMD Standard");
1243 case CFI_CMDSET_AMD_EXTENDED:
1244 printf("AMD Extended");
1246 #ifdef CONFIG_FLASH_CFI_LEGACY
1247 case CFI_CMDSET_AMD_LEGACY:
1248 printf("AMD Legacy");
1252 printf("Unknown (%d)", info->vendor);
1255 printf(" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1256 info->manufacturer_id);
1257 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1259 if ((info->device_id & 0xff) == 0x7E) {
1260 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1263 if (info->vendor == CFI_CMDSET_AMD_STANDARD && info->legacy_unlock)
1264 printf("\n Advanced Sector Protection (PPB) enabled");
1265 printf("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1266 info->erase_blk_tout, info->write_tout);
1267 if (info->buffer_size > 1) {
1268 printf(" Buffer write timeout: %ld ms, ",
1269 info->buffer_write_tout);
1270 printf("buffer size: %d bytes\n", info->buffer_size);
1273 puts("\n Sector Start Addresses:");
1274 for (i = 0; i < info->sector_count; ++i) {
1279 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1280 /* print empty and read-only info */
1281 printf(" %08lX %c %s ",
1283 sector_erased(info, i) ? 'E' : ' ',
1284 info->protect[i] ? "RO" : " ");
1285 #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
1286 printf(" %08lX %s ",
1288 info->protect[i] ? "RO" : " ");
1294 /*-----------------------------------------------------------------------
1295 * This is used in a few places in write_buf() to show programming
1296 * progress. Making it a function is nasty because it needs to do side
1297 * effect updates to digit and dots. Repeated code is nasty too, so
1298 * we define it once here.
1300 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1301 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
1302 if (flash_verbose) { \
1304 if (scale > 0 && dots <= 0) { \
1305 if ((digit % 5) == 0) \
1306 printf("%d", digit / 5); \
1314 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1317 /*-----------------------------------------------------------------------
1318 * Copy memory to flash, returns:
1321 * 2 - Flash not erased
1323 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
1330 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1333 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1334 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1339 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1341 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1342 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1343 CONFIG_FLASH_SHOW_PROGRESS);
1347 /* get lower aligned address */
1348 wp = (addr & ~(info->portwidth - 1));
1350 /* handle unaligned start */
1355 for (i = 0; i < aln; ++i)
1356 flash_add_byte(info, &cword, flash_read8(p + i));
1358 for (; (i < info->portwidth) && (cnt > 0); i++) {
1359 flash_add_byte(info, &cword, *src++);
1362 for (; (cnt == 0) && (i < info->portwidth); ++i)
1363 flash_add_byte(info, &cword, flash_read8(p + i));
1365 rc = flash_write_cfiword(info, wp, cword);
1370 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1373 /* handle the aligned part */
1374 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1375 buffered_size = (info->portwidth / info->chipwidth);
1376 buffered_size *= info->buffer_size;
1377 while (cnt >= info->portwidth) {
1378 /* prohibit buffer write when buffer_size is 1 */
1379 if (info->buffer_size == 1) {
1381 for (i = 0; i < info->portwidth; i++)
1382 flash_add_byte(info, &cword, *src++);
1383 rc = flash_write_cfiword(info, wp, cword);
1386 wp += info->portwidth;
1387 cnt -= info->portwidth;
1391 /* write buffer until next buffered_size aligned boundary */
1392 i = buffered_size - (wp % buffered_size);
1395 rc = flash_write_cfibuffer(info, wp, src, i);
1398 i -= i & (info->portwidth - 1);
1402 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1403 /* Only check every once in a while */
1404 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1408 while (cnt >= info->portwidth) {
1410 for (i = 0; i < info->portwidth; i++)
1411 flash_add_byte(info, &cword, *src++);
1412 rc = flash_write_cfiword(info, wp, cword);
1415 wp += info->portwidth;
1416 cnt -= info->portwidth;
1417 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
1418 /* Only check every once in a while */
1419 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1422 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1428 * handle unaligned tail bytes
1432 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
1433 flash_add_byte(info, &cword, *src++);
1436 for (; i < info->portwidth; ++i)
1437 flash_add_byte(info, &cword, flash_read8(p + i));
1439 return flash_write_cfiword(info, wp, cword);
1442 static inline int manufact_match(flash_info_t *info, u32 manu)
1444 return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
1447 /*-----------------------------------------------------------------------
1449 #ifdef CONFIG_SYS_FLASH_PROTECTION
1451 static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1453 if (manufact_match(info, INTEL_MANUFACT) &&
1454 info->device_id == NUMONYX_256MBIT) {
1457 * "Numonyx Axcell P33/P30 Specification Update" :)
1459 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1460 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1463 * cmd must come before FLASH_CMD_PROTECT + 20us
1464 * Disable interrupts which might cause a timeout here.
1466 int flag = disable_interrupts();
1470 cmd = FLASH_CMD_PROTECT_SET;
1472 cmd = FLASH_CMD_PROTECT_CLEAR;
1474 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
1475 flash_write_cmd(info, sector, 0, cmd);
1476 /* re-enable interrupts if necessary */
1478 enable_interrupts();
1485 int flash_real_protect(flash_info_t *info, long sector, int prot)
1489 switch (info->vendor) {
1490 case CFI_CMDSET_INTEL_PROG_REGIONS:
1491 case CFI_CMDSET_INTEL_STANDARD:
1492 case CFI_CMDSET_INTEL_EXTENDED:
1493 if (!cfi_protect_bugfix(info, sector, prot)) {
1494 flash_write_cmd(info, sector, 0,
1495 FLASH_CMD_CLEAR_STATUS);
1496 flash_write_cmd(info, sector, 0,
1499 flash_write_cmd(info, sector, 0,
1500 FLASH_CMD_PROTECT_SET);
1502 flash_write_cmd(info, sector, 0,
1503 FLASH_CMD_PROTECT_CLEAR);
1506 case CFI_CMDSET_AMD_EXTENDED:
1507 case CFI_CMDSET_AMD_STANDARD:
1508 /* U-Boot only checks the first byte */
1509 if (manufact_match(info, ATM_MANUFACT)) {
1511 flash_unlock_seq(info, 0);
1512 flash_write_cmd(info, 0,
1514 ATM_CMD_SOFTLOCK_START);
1515 flash_unlock_seq(info, 0);
1516 flash_write_cmd(info, sector, 0,
1519 flash_write_cmd(info, 0,
1521 AMD_CMD_UNLOCK_START);
1522 if (info->device_id == ATM_ID_BV6416)
1523 flash_write_cmd(info, sector,
1524 0, ATM_CMD_UNLOCK_SECT);
1527 if (info->legacy_unlock) {
1528 int flag = disable_interrupts();
1531 flash_unlock_seq(info, 0);
1532 flash_write_cmd(info, 0, info->addr_unlock1,
1533 AMD_CMD_SET_PPB_ENTRY);
1534 lock_flag = flash_isset(info, sector, 0, 0x01);
1537 flash_write_cmd(info, sector, 0,
1538 AMD_CMD_PPB_LOCK_BC1);
1539 flash_write_cmd(info, sector, 0,
1540 AMD_CMD_PPB_LOCK_BC2);
1542 debug("sector %ld %slocked\n", sector,
1543 lock_flag ? "" : "already ");
1546 debug("unlock %ld\n", sector);
1547 flash_write_cmd(info, 0, 0,
1548 AMD_CMD_PPB_UNLOCK_BC1);
1549 flash_write_cmd(info, 0, 0,
1550 AMD_CMD_PPB_UNLOCK_BC2);
1552 debug("sector %ld %sunlocked\n", sector,
1553 !lock_flag ? "" : "already ");
1556 enable_interrupts();
1558 if (flash_status_check(info, sector,
1559 info->erase_blk_tout,
1560 prot ? "protect" : "unprotect"))
1561 printf("status check error\n");
1563 flash_write_cmd(info, 0, 0,
1564 AMD_CMD_SET_PPB_EXIT_BC1);
1565 flash_write_cmd(info, 0, 0,
1566 AMD_CMD_SET_PPB_EXIT_BC2);
1569 #ifdef CONFIG_FLASH_CFI_LEGACY
1570 case CFI_CMDSET_AMD_LEGACY:
1571 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1572 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
1574 flash_write_cmd(info, sector, 0,
1575 FLASH_CMD_PROTECT_SET);
1577 flash_write_cmd(info, sector, 0,
1578 FLASH_CMD_PROTECT_CLEAR);
1583 * Flash needs to be in status register read mode for
1584 * flash_full_status_check() to work correctly
1586 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
1587 retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
1588 prot ? "protect" : "unprotect");
1590 info->protect[sector] = prot;
1593 * On some of Intel's flash chips (marked via legacy_unlock)
1594 * unprotect unprotects all locking.
1596 if (prot == 0 && info->legacy_unlock) {
1599 for (i = 0; i < info->sector_count; i++) {
1600 if (info->protect[i])
1601 flash_real_protect(info, i, 1);
1608 /*-----------------------------------------------------------------------
1609 * flash_read_user_serial - read the OneTimeProgramming cells
1611 void flash_read_user_serial(flash_info_t *info, void *buffer, int offset,
1618 src = flash_map(info, 0, FLASH_OFFSET_USER_PROTECTION);
1619 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1620 memcpy(dst, src + offset, len);
1621 flash_write_cmd(info, 0, 0, info->cmd_reset);
1623 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
1627 * flash_read_factory_serial - read the device Id from the protection area
1629 void flash_read_factory_serial(flash_info_t *info, void *buffer, int offset,
1634 src = flash_map(info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1635 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1636 memcpy(buffer, src + offset, len);
1637 flash_write_cmd(info, 0, 0, info->cmd_reset);
1639 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
1642 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1644 /*-----------------------------------------------------------------------
1645 * Reverse the order of the erase regions in the CFI QRY structure.
1646 * This is needed for chips that are either a) correctly detected as
1647 * top-boot, or b) buggy.
1649 static void cfi_reverse_geometry(struct cfi_qry *qry)
1654 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1655 tmp = get_unaligned(&qry->erase_region_info[i]);
1656 put_unaligned(get_unaligned(&qry->erase_region_info[j]),
1657 &qry->erase_region_info[i]);
1658 put_unaligned(tmp, &qry->erase_region_info[j]);
1662 /*-----------------------------------------------------------------------
1663 * read jedec ids from device and set corresponding fields in info struct
1665 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1668 static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1670 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1672 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1673 udelay(1000); /* some flash are slow to respond */
1674 info->manufacturer_id = flash_read_uchar(info,
1675 FLASH_OFFSET_MANUFACTURER_ID);
1676 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1677 flash_read_word(info, FLASH_OFFSET_DEVICE_ID) :
1678 flash_read_uchar(info, FLASH_OFFSET_DEVICE_ID);
1679 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1682 static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1684 info->cmd_reset = FLASH_CMD_RESET;
1686 cmdset_intel_read_jedec_ids(info);
1687 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1689 #ifdef CONFIG_SYS_FLASH_PROTECTION
1690 /* read legacy lock/unlock bit from intel flash */
1691 if (info->ext_addr) {
1692 info->legacy_unlock =
1693 flash_read_uchar(info, info->ext_addr + 5) & 0x08;
1700 static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1706 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1707 flash_unlock_seq(info, 0);
1708 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1709 udelay(1000); /* some flash are slow to respond */
1711 manu_id = flash_read_uchar(info, FLASH_OFFSET_MANUFACTURER_ID);
1712 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1713 while (manu_id == FLASH_CONTINUATION_CODE && bank_id < 0x800) {
1715 manu_id = flash_read_uchar(info,
1716 bank_id | FLASH_OFFSET_MANUFACTURER_ID);
1718 info->manufacturer_id = manu_id;
1720 debug("info->ext_addr = 0x%x, cfi_version = 0x%x\n",
1721 info->ext_addr, info->cfi_version);
1722 if (info->ext_addr && info->cfi_version >= 0x3134) {
1723 /* read software feature (at 0x53) */
1724 feature = flash_read_uchar(info, info->ext_addr + 0x13);
1725 debug("feature = 0x%x\n", feature);
1726 info->sr_supported = feature & 0x1;
1729 switch (info->chipwidth) {
1730 case FLASH_CFI_8BIT:
1731 info->device_id = flash_read_uchar(info,
1732 FLASH_OFFSET_DEVICE_ID);
1733 if (info->device_id == 0x7E) {
1734 /* AMD 3-byte (expanded) device ids */
1735 info->device_id2 = flash_read_uchar(info,
1736 FLASH_OFFSET_DEVICE_ID2);
1737 info->device_id2 <<= 8;
1738 info->device_id2 |= flash_read_uchar(info,
1739 FLASH_OFFSET_DEVICE_ID3);
1742 case FLASH_CFI_16BIT:
1743 info->device_id = flash_read_word(info,
1744 FLASH_OFFSET_DEVICE_ID);
1745 if ((info->device_id & 0xff) == 0x7E) {
1746 /* AMD 3-byte (expanded) device ids */
1747 info->device_id2 = flash_read_uchar(info,
1748 FLASH_OFFSET_DEVICE_ID2);
1749 info->device_id2 <<= 8;
1750 info->device_id2 |= flash_read_uchar(info,
1751 FLASH_OFFSET_DEVICE_ID3);
1757 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1761 static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1763 info->cmd_reset = AMD_CMD_RESET;
1764 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
1766 cmdset_amd_read_jedec_ids(info);
1767 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1769 #ifdef CONFIG_SYS_FLASH_PROTECTION
1770 if (info->ext_addr) {
1771 /* read sector protect/unprotect scheme (at 0x49) */
1772 if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
1773 info->legacy_unlock = 1;
1780 #ifdef CONFIG_FLASH_CFI_LEGACY
1781 static void flash_read_jedec_ids(flash_info_t *info)
1783 info->manufacturer_id = 0;
1784 info->device_id = 0;
1785 info->device_id2 = 0;
1787 switch (info->vendor) {
1788 case CFI_CMDSET_INTEL_PROG_REGIONS:
1789 case CFI_CMDSET_INTEL_STANDARD:
1790 case CFI_CMDSET_INTEL_EXTENDED:
1791 cmdset_intel_read_jedec_ids(info);
1793 case CFI_CMDSET_AMD_STANDARD:
1794 case CFI_CMDSET_AMD_EXTENDED:
1795 cmdset_amd_read_jedec_ids(info);
1802 /*-----------------------------------------------------------------------
1803 * Call board code to request info about non-CFI flash.
1804 * board_flash_get_legacy needs to fill in at least:
1805 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1807 static int flash_detect_legacy(phys_addr_t base, int banknum)
1809 flash_info_t *info = &flash_info[banknum];
1811 if (board_flash_get_legacy(base, banknum, info)) {
1812 /* board code may have filled info completely. If not, we
1813 * use JEDEC ID probing.
1815 if (!info->vendor) {
1817 CFI_CMDSET_AMD_STANDARD,
1818 CFI_CMDSET_INTEL_STANDARD
1822 for (i = 0; i < ARRAY_SIZE(modes); i++) {
1823 info->vendor = modes[i];
1825 (ulong)map_physmem(base,
1828 if (info->portwidth == FLASH_CFI_8BIT &&
1829 info->interface == FLASH_CFI_X8X16) {
1830 info->addr_unlock1 = 0x2AAA;
1831 info->addr_unlock2 = 0x5555;
1833 info->addr_unlock1 = 0x5555;
1834 info->addr_unlock2 = 0x2AAA;
1836 flash_read_jedec_ids(info);
1837 debug("JEDEC PROBE: ID %x %x %x\n",
1838 info->manufacturer_id,
1841 if (jedec_flash_match(info, info->start[0]))
1844 unmap_physmem((void *)info->start[0],
1849 switch (info->vendor) {
1850 case CFI_CMDSET_INTEL_PROG_REGIONS:
1851 case CFI_CMDSET_INTEL_STANDARD:
1852 case CFI_CMDSET_INTEL_EXTENDED:
1853 info->cmd_reset = FLASH_CMD_RESET;
1855 case CFI_CMDSET_AMD_STANDARD:
1856 case CFI_CMDSET_AMD_EXTENDED:
1857 case CFI_CMDSET_AMD_LEGACY:
1858 info->cmd_reset = AMD_CMD_RESET;
1861 info->flash_id = FLASH_MAN_CFI;
1864 return 0; /* use CFI */
1867 static inline int flash_detect_legacy(phys_addr_t base, int banknum)
1869 return 0; /* use CFI */
1873 /*-----------------------------------------------------------------------
1874 * detect if flash is compatible with the Common Flash Interface (CFI)
1875 * http://www.jedec.org/download/search/jesd68.pdf
1877 static void flash_read_cfi(flash_info_t *info, void *buf, unsigned int start,
1883 for (i = 0; i < len; i++)
1884 p[i] = flash_read_uchar(info, start + i);
1887 static void __flash_cmd_reset(flash_info_t *info)
1890 * We do not yet know what kind of commandset to use, so we issue
1891 * the reset command in both Intel and AMD variants, in the hope
1892 * that AMD flash roms ignore the Intel command.
1894 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1896 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1899 void flash_cmd_reset(flash_info_t *info)
1900 __attribute__((weak, alias("__flash_cmd_reset")));
1902 static int __flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
1906 /* Issue FLASH reset command */
1907 flash_cmd_reset(info);
1909 for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
1911 flash_write_cmd(info, 0, flash_offset_cfi[cfi_offset],
1913 if (flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP, 'Q') &&
1914 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
1915 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1916 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1917 sizeof(struct cfi_qry));
1918 info->interface = le16_to_cpu(qry->interface_desc);
1920 info->cfi_offset = flash_offset_cfi[cfi_offset];
1921 debug("device interface is %d\n",
1923 debug("found port %d chip %d ",
1924 info->portwidth, info->chipwidth);
1925 debug("port %d bits chip %d bits\n",
1926 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1927 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1929 /* calculate command offsets as in the Linux driver */
1930 info->addr_unlock1 = 0x555;
1931 info->addr_unlock2 = 0x2aa;
1934 * modify the unlock address if we are
1935 * in compatibility mode
1937 if (/* x8/x16 in x8 mode */
1938 (info->chipwidth == FLASH_CFI_BY8 &&
1939 info->interface == FLASH_CFI_X8X16) ||
1940 /* x16/x32 in x16 mode */
1941 (info->chipwidth == FLASH_CFI_BY16 &&
1942 info->interface == FLASH_CFI_X16X32)) {
1943 info->addr_unlock1 = 0xaaa;
1944 info->addr_unlock2 = 0x555;
1947 info->name = "CFI conformant";
1955 static int flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
1957 debug("flash detect cfi\n");
1959 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
1960 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1961 for (info->chipwidth = FLASH_CFI_BY8;
1962 info->chipwidth <= info->portwidth;
1963 info->chipwidth <<= 1)
1964 if (__flash_detect_cfi(info, qry))
1967 debug("not found\n");
1972 * Manufacturer-specific quirks. Add workarounds for geometry
1973 * reversal, etc. here.
1975 static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1977 /* check if flash geometry needs reversal */
1978 if (qry->num_erase_regions > 1) {
1979 /* reverse geometry if top boot part */
1980 if (info->cfi_version < 0x3131) {
1981 /* CFI < 1.1, try to guess from device id */
1982 if ((info->device_id & 0x80) != 0)
1983 cfi_reverse_geometry(qry);
1984 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1985 /* CFI >= 1.1, deduct from top/bottom flag */
1986 /* note: ext_addr is valid since cfi_version > 0 */
1987 cfi_reverse_geometry(qry);
1992 static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1994 int reverse_geometry = 0;
1996 /* Check the "top boot" bit in the PRI */
1997 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1998 reverse_geometry = 1;
2000 /* AT49BV6416(T) list the erase regions in the wrong order.
2001 * However, the device ID is identical with the non-broken
2002 * AT49BV642D they differ in the high byte.
2004 if (info->device_id == 0xd6 || info->device_id == 0xd2)
2005 reverse_geometry = !reverse_geometry;
2007 if (reverse_geometry)
2008 cfi_reverse_geometry(qry);
2011 static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
2013 /* check if flash geometry needs reversal */
2014 if (qry->num_erase_regions > 1) {
2015 /* reverse geometry if top boot part */
2016 if (info->cfi_version < 0x3131) {
2017 /* CFI < 1.1, guess by device id */
2018 if (info->device_id == 0x22CA || /* M29W320DT */
2019 info->device_id == 0x2256 || /* M29W320ET */
2020 info->device_id == 0x22D7) { /* M29W800DT */
2021 cfi_reverse_geometry(qry);
2023 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
2024 /* CFI >= 1.1, deduct from top/bottom flag */
2025 /* note: ext_addr is valid since cfi_version > 0 */
2026 cfi_reverse_geometry(qry);
2031 static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
2034 * SST, for many recent nor parallel flashes, says they are
2035 * CFI-conformant. This is not true, since qry struct.
2036 * reports a std. AMD command set (0x0002), while SST allows to
2037 * erase two different sector sizes for the same memory.
2038 * 64KB sector (SST call it block) needs 0x30 to be erased.
2039 * 4KB sector (SST call it sector) needs 0x50 to be erased.
2040 * Since CFI query detect the 4KB number of sectors, users expects
2041 * a sector granularity of 4KB, and it is here set.
2043 if (info->device_id == 0x5D23 || /* SST39VF3201B */
2044 info->device_id == 0x5C23) { /* SST39VF3202B */
2045 /* set sector granularity to 4KB */
2046 info->cmd_erase_sector = 0x50;
2050 static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry)
2053 * The M29EW devices seem to report the CFI information wrong
2054 * when it's in 8 bit mode.
2055 * There's an app note from Numonyx on this issue.
2056 * So adjust the buffer size for M29EW while operating in 8-bit mode
2058 if (qry->max_buf_write_size > 0x8 &&
2059 info->device_id == 0x7E &&
2060 (info->device_id2 == 0x2201 ||
2061 info->device_id2 == 0x2301 ||
2062 info->device_id2 == 0x2801 ||
2063 info->device_id2 == 0x4801)) {
2064 debug("Adjusted buffer size on Numonyx flash");
2065 debug(" M29EW family in 8 bit mode\n");
2066 qry->max_buf_write_size = 0x8;
2071 * The following code cannot be run from FLASH!
2074 ulong flash_get_size(phys_addr_t base, int banknum)
2076 flash_info_t *info = &flash_info[banknum];
2078 flash_sect_t sect_cnt;
2082 uchar num_erase_regions;
2083 int erase_region_size;
2084 int erase_region_count;
2086 unsigned long max_size;
2088 memset(&qry, 0, sizeof(qry));
2091 info->cfi_version = 0;
2092 #ifdef CONFIG_SYS_FLASH_PROTECTION
2093 info->legacy_unlock = 0;
2096 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
2098 if (flash_detect_cfi(info, &qry)) {
2099 info->vendor = le16_to_cpu(get_unaligned(&qry.p_id));
2100 info->ext_addr = le16_to_cpu(get_unaligned(&qry.p_adr));
2101 num_erase_regions = qry.num_erase_regions;
2103 if (info->ext_addr) {
2104 info->cfi_version = (ushort)flash_read_uchar(info,
2105 info->ext_addr + 3) << 8;
2106 info->cfi_version |= (ushort)flash_read_uchar(info,
2107 info->ext_addr + 4);
2111 flash_printqry(&qry);
2114 switch (info->vendor) {
2115 case CFI_CMDSET_INTEL_PROG_REGIONS:
2116 case CFI_CMDSET_INTEL_STANDARD:
2117 case CFI_CMDSET_INTEL_EXTENDED:
2118 cmdset_intel_init(info, &qry);
2120 case CFI_CMDSET_AMD_STANDARD:
2121 case CFI_CMDSET_AMD_EXTENDED:
2122 cmdset_amd_init(info, &qry);
2125 printf("CFI: Unknown command set 0x%x\n",
2128 * Unfortunately, this means we don't know how
2129 * to get the chip back to Read mode. Might
2130 * as well try an Intel-style reset...
2132 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2136 /* Do manufacturer-specific fixups */
2137 switch (info->manufacturer_id) {
2138 case 0x0001: /* AMD */
2139 case 0x0037: /* AMIC */
2140 flash_fixup_amd(info, &qry);
2143 flash_fixup_atmel(info, &qry);
2146 flash_fixup_stm(info, &qry);
2148 case 0x00bf: /* SST */
2149 flash_fixup_sst(info, &qry);
2151 case 0x0089: /* Numonyx */
2152 flash_fixup_num(info, &qry);
2156 debug("manufacturer is %d\n", info->vendor);
2157 debug("manufacturer id is 0x%x\n", info->manufacturer_id);
2158 debug("device id is 0x%x\n", info->device_id);
2159 debug("device id2 is 0x%x\n", info->device_id2);
2160 debug("cfi version is 0x%04x\n", info->cfi_version);
2162 size_ratio = info->portwidth / info->chipwidth;
2163 /* if the chip is x8/x16 reduce the ratio by half */
2164 if (info->interface == FLASH_CFI_X8X16 &&
2165 info->chipwidth == FLASH_CFI_BY8) {
2168 debug("size_ratio %d port %d bits chip %d bits\n",
2169 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2170 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
2171 info->size = 1 << qry.dev_size;
2172 /* multiply the size by the number of chips */
2173 info->size *= size_ratio;
2174 max_size = cfi_flash_bank_size(banknum);
2175 if (max_size && info->size > max_size) {
2176 debug("[truncated from %ldMiB]", info->size >> 20);
2177 info->size = max_size;
2179 debug("found %d erase regions\n", num_erase_regions);
2182 for (i = 0; i < num_erase_regions; i++) {
2183 if (i > NUM_ERASE_REGIONS) {
2184 printf("%d erase regions found, only %d used\n",
2185 num_erase_regions, NUM_ERASE_REGIONS);
2189 tmp = le32_to_cpu(get_unaligned(
2190 &qry.erase_region_info[i]));
2191 debug("erase region %u: 0x%08lx\n", i, tmp);
2193 erase_region_count = (tmp & 0xffff) + 1;
2196 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
2197 debug("erase_region_count = %d ", erase_region_count);
2198 debug("erase_region_size = %d\n", erase_region_size);
2199 for (j = 0; j < erase_region_count; j++) {
2200 if (sector - base >= info->size)
2202 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
2203 printf("ERROR: too many flash sectors\n");
2206 info->start[sect_cnt] =
2207 (ulong)map_physmem(sector,
2210 sector += (erase_region_size * size_ratio);
2213 * Only read protection status from
2214 * supported devices (intel...)
2216 switch (info->vendor) {
2217 case CFI_CMDSET_INTEL_PROG_REGIONS:
2218 case CFI_CMDSET_INTEL_EXTENDED:
2219 case CFI_CMDSET_INTEL_STANDARD:
2221 * Set flash to read-id mode. Otherwise
2222 * reading protected status is not
2225 flash_write_cmd(info, sect_cnt, 0,
2227 info->protect[sect_cnt] =
2228 flash_isset(info, sect_cnt,
2229 FLASH_OFFSET_PROTECT,
2230 FLASH_STATUS_PROTECT);
2231 flash_write_cmd(info, sect_cnt, 0,
2234 case CFI_CMDSET_AMD_EXTENDED:
2235 case CFI_CMDSET_AMD_STANDARD:
2236 if (!info->legacy_unlock) {
2237 /* default: not protected */
2238 info->protect[sect_cnt] = 0;
2242 /* Read protection (PPB) from sector */
2243 flash_write_cmd(info, 0, 0,
2245 flash_unlock_seq(info, 0);
2246 flash_write_cmd(info, 0,
2249 info->protect[sect_cnt] =
2252 FLASH_OFFSET_PROTECT,
2253 FLASH_STATUS_PROTECT);
2256 /* default: not protected */
2257 info->protect[sect_cnt] = 0;
2264 info->sector_count = sect_cnt;
2265 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2266 tmp = 1 << qry.block_erase_timeout_typ;
2267 info->erase_blk_tout = tmp *
2268 (1 << qry.block_erase_timeout_max);
2269 tmp = (1 << qry.buf_write_timeout_typ) *
2270 (1 << qry.buf_write_timeout_max);
2272 /* round up when converting to ms */
2273 info->buffer_write_tout = (tmp + 999) / 1000;
2274 tmp = (1 << qry.word_write_timeout_typ) *
2275 (1 << qry.word_write_timeout_max);
2276 /* round up when converting to ms */
2277 info->write_tout = (tmp + 999) / 1000;
2278 info->flash_id = FLASH_MAN_CFI;
2279 if (info->interface == FLASH_CFI_X8X16 &&
2280 info->chipwidth == FLASH_CFI_BY8) {
2281 /* XXX - Need to test on x8/x16 in parallel. */
2282 info->portwidth >>= 1;
2285 flash_write_cmd(info, 0, 0, info->cmd_reset);
2288 return (info->size);
2291 #ifdef CONFIG_FLASH_CFI_MTD
2292 void flash_set_verbose(uint v)
2298 static void cfi_flash_set_config_reg(u32 base, u16 val)
2300 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2302 * Only set this config register if really defined
2303 * to a valid value (0xffff is invalid)
2309 * Set configuration register. Data is "encrypted" in the 16 lower
2312 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2313 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2316 * Finally issue reset-command to bring device back to
2319 flash_write16(FLASH_CMD_RESET, (void *)base);
2323 /*-----------------------------------------------------------------------
2326 static void flash_protect_default(void)
2328 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2333 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2336 /* Monitor protection ON by default */
2337 #if defined(CONFIG_SYS_MONITOR_BASE) && \
2338 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2339 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2340 flash_protect(FLAG_PROTECT_SET,
2341 CONFIG_SYS_MONITOR_BASE,
2342 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2343 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2346 /* Environment protection ON by default */
2347 #ifdef CONFIG_ENV_IS_IN_FLASH
2348 flash_protect(FLAG_PROTECT_SET,
2350 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2351 flash_get_info(CONFIG_ENV_ADDR));
2354 /* Redundant environment protection ON by default */
2355 #ifdef CONFIG_ENV_ADDR_REDUND
2356 flash_protect(FLAG_PROTECT_SET,
2357 CONFIG_ENV_ADDR_REDUND,
2358 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2359 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2362 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2363 for (i = 0; i < ARRAY_SIZE(apl); i++) {
2364 debug("autoprotecting from %08lx to %08lx\n",
2365 apl[i].start, apl[i].start + apl[i].size - 1);
2366 flash_protect(FLAG_PROTECT_SET,
2368 apl[i].start + apl[i].size - 1,
2369 flash_get_info(apl[i].start));
2374 unsigned long flash_init(void)
2376 unsigned long size = 0;
2379 #ifdef CONFIG_SYS_FLASH_PROTECTION
2380 /* read environment from EEPROM */
2383 env_get_f("unlock", s, sizeof(s));
2386 #ifdef CONFIG_CFI_FLASH /* for driver model */
2387 cfi_flash_init_dm();
2390 /* Init: no FLASHes known */
2391 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
2392 flash_info[i].flash_id = FLASH_UNKNOWN;
2394 /* Optionally write flash configuration register */
2395 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2396 cfi_flash_config_reg(i));
2398 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
2399 flash_get_size(cfi_flash_bank_addr(i), i);
2400 size += flash_info[i].size;
2401 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
2402 #ifndef CONFIG_SYS_FLASH_QUIET_TEST
2403 printf("## Unknown flash on Bank %d ", i + 1);
2404 printf("- Size = 0x%08lx = %ld MB\n",
2406 flash_info[i].size >> 20);
2407 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
2409 #ifdef CONFIG_SYS_FLASH_PROTECTION
2410 else if (strcmp(s, "yes") == 0) {
2412 * Only the U-Boot image and it's environment
2413 * is protected, all other sectors are
2414 * unprotected (unlocked) if flash hardware
2415 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
2416 * and the environment variable "unlock" is
2419 if (flash_info[i].legacy_unlock) {
2423 * Disable legacy_unlock temporarily,
2424 * since flash_real_protect would
2425 * relock all other sectors again
2428 flash_info[i].legacy_unlock = 0;
2431 * Legacy unlocking (e.g. Intel J3) ->
2432 * unlock only one sector. This will
2433 * unlock all sectors.
2435 flash_real_protect(&flash_info[i], 0, 0);
2437 flash_info[i].legacy_unlock = 1;
2440 * Manually mark other sectors as
2441 * unlocked (unprotected)
2443 for (k = 1; k < flash_info[i].sector_count; k++)
2444 flash_info[i].protect[k] = 0;
2447 * No legancy unlocking -> unlock all sectors
2449 flash_protect(FLAG_PROTECT_CLEAR,
2450 flash_info[i].start[0],
2451 flash_info[i].start[0]
2452 + flash_info[i].size - 1,
2456 #endif /* CONFIG_SYS_FLASH_PROTECTION */
2459 flash_protect_default();
2460 #ifdef CONFIG_FLASH_CFI_MTD
2467 #ifdef CONFIG_CFI_FLASH /* for driver model */
2468 static int cfi_flash_probe(struct udevice *dev)
2470 const fdt32_t *cell;
2474 addrc = dev_read_addr_cells(dev);
2475 sizec = dev_read_size_cells(dev);
2477 /* decode regs; there may be multiple reg tuples. */
2478 cell = dev_read_prop(dev, "reg", &len);
2482 len /= sizeof(fdt32_t);
2486 addr = dev_translate_address(dev, cell + idx);
2488 flash_info[cfi_flash_num_flash_banks].dev = dev;
2489 flash_info[cfi_flash_num_flash_banks].base = addr;
2490 cfi_flash_num_flash_banks++;
2492 idx += addrc + sizec;
2494 gd->bd->bi_flashstart = flash_info[0].base;
2499 static const struct udevice_id cfi_flash_ids[] = {
2500 { .compatible = "cfi-flash" },
2501 { .compatible = "jedec-flash" },
2505 U_BOOT_DRIVER(cfi_flash) = {
2506 .name = "cfi_flash",
2508 .of_match = cfi_flash_ids,
2509 .probe = cfi_flash_probe,
2511 #endif /* CONFIG_CFI_FLASH */