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>
26 #include <asm/processor.h>
28 #include <asm/byteorder.h>
29 #include <asm/unaligned.h>
30 #include <env_internal.h>
31 #include <mtd/cfi_flash.h>
35 * This file implements a Common Flash Interface (CFI) driver for
38 * The width of the port and the width of the chips are determined at
39 * initialization. These widths are used to calculate the address for
40 * access CFI data structures.
43 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
44 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
45 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
46 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
47 * AMD CFI Specification, Release 2.0 December 1, 2001
48 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
49 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
51 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
52 * reading and writing ... (yes there is such a Hardware).
55 DECLARE_GLOBAL_DATA_PTR
;
57 static uint flash_offset_cfi
[2] = { FLASH_OFFSET_CFI
, FLASH_OFFSET_CFI_ALT
};
58 #ifdef CONFIG_FLASH_CFI_MTD
59 static uint flash_verbose
= 1;
61 #define flash_verbose 1
64 flash_info_t flash_info
[CFI_MAX_FLASH_BANKS
]; /* FLASH chips info */
67 * Check if chip width is defined. If not, start detecting with 8bit.
69 #ifndef CONFIG_SYS_FLASH_CFI_WIDTH
70 #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
73 #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
74 #define __maybe_weak __weak
76 #define __maybe_weak static
80 * 0xffff is an undefined value for the configuration register. When
81 * this value is returned, the configuration register shall not be
82 * written at all (default mode).
84 static u16
cfi_flash_config_reg(int i
)
86 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
87 return ((u16
[])CONFIG_SYS_CFI_FLASH_CONFIG_REGS
)[i
];
93 #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
94 int cfi_flash_num_flash_banks
= CONFIG_SYS_MAX_FLASH_BANKS_DETECT
;
96 int cfi_flash_num_flash_banks
;
99 #ifdef CONFIG_CFI_FLASH /* for driver model */
100 static void cfi_flash_init_dm(void)
104 cfi_flash_num_flash_banks
= 0;
106 * The uclass_first_device() will probe the first device and
107 * uclass_next_device() will probe the rest if they exist. So
108 * that cfi_flash_probe() will get called assigning the base
109 * addresses that are available.
111 for (uclass_first_device(UCLASS_MTD
, &dev
);
113 uclass_next_device(&dev
)) {
117 phys_addr_t
cfi_flash_bank_addr(int i
)
119 return flash_info
[i
].base
;
122 __weak phys_addr_t
cfi_flash_bank_addr(int i
)
124 return ((phys_addr_t
[])CONFIG_SYS_FLASH_BANKS_LIST
)[i
];
128 __weak
unsigned long cfi_flash_bank_size(int i
)
130 #ifdef CONFIG_SYS_FLASH_BANKS_SIZES
131 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES
)[i
];
137 __maybe_weak
void flash_write8(u8 value
, void *addr
)
139 __raw_writeb(value
, addr
);
142 __maybe_weak
void flash_write16(u16 value
, void *addr
)
144 __raw_writew(value
, addr
);
147 __maybe_weak
void flash_write32(u32 value
, void *addr
)
149 __raw_writel(value
, addr
);
152 __maybe_weak
void flash_write64(u64 value
, void *addr
)
154 /* No architectures currently implement __raw_writeq() */
155 *(volatile u64
*)addr
= value
;
158 __maybe_weak u8
flash_read8(void *addr
)
160 return __raw_readb(addr
);
163 __maybe_weak u16
flash_read16(void *addr
)
165 return __raw_readw(addr
);
168 __maybe_weak u32
flash_read32(void *addr
)
170 return __raw_readl(addr
);
173 __maybe_weak u64
flash_read64(void *addr
)
175 /* No architectures currently implement __raw_readq() */
176 return *(volatile u64
*)addr
;
179 /*-----------------------------------------------------------------------
181 #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || \
182 (defined(CONFIG_SYS_MONITOR_BASE) && \
183 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE))
184 static flash_info_t
*flash_get_info(ulong base
)
189 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
190 info
= &flash_info
[i
];
191 if (info
->size
&& info
->start
[0] <= base
&&
192 base
<= info
->start
[0] + info
->size
- 1)
200 unsigned long flash_sector_size(flash_info_t
*info
, flash_sect_t sect
)
202 if (sect
!= (info
->sector_count
- 1))
203 return info
->start
[sect
+ 1] - info
->start
[sect
];
205 return info
->start
[0] + info
->size
- info
->start
[sect
];
208 /*-----------------------------------------------------------------------
209 * create an address based on the offset and the port width
212 flash_map(flash_info_t
*info
, flash_sect_t sect
, uint offset
)
214 unsigned int byte_offset
= offset
* info
->portwidth
;
216 return (void *)(info
->start
[sect
] + byte_offset
);
219 static inline void flash_unmap(flash_info_t
*info
, flash_sect_t sect
,
220 unsigned int offset
, void *addr
)
224 /*-----------------------------------------------------------------------
225 * make a proper sized command based on the port and chip widths
227 static void flash_make_cmd(flash_info_t
*info
, u32 cmd
, void *cmdbuf
)
232 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
233 u32 cmd_le
= cpu_to_le32(cmd
);
236 uchar
*cp
= (uchar
*) cmdbuf
;
238 for (i
= info
->portwidth
; i
> 0; i
--) {
239 cword_offset
= (info
->portwidth
- i
) % info
->chipwidth
;
240 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
241 cp_offset
= info
->portwidth
- i
;
242 val
= *((uchar
*)&cmd_le
+ cword_offset
);
245 val
= *((uchar
*)&cmd
+ sizeof(u32
) - cword_offset
- 1);
247 cp
[cp_offset
] = (cword_offset
>= sizeof(u32
)) ? 0x00 : val
;
252 /*-----------------------------------------------------------------------
255 static void print_longlong(char *str
, unsigned long long data
)
261 for (i
= 0; i
< 8; i
++)
262 sprintf(&str
[i
* 2], "%2.2x", *cp
++);
265 static void flash_printqry(struct cfi_qry
*qry
)
270 for (x
= 0; x
< sizeof(struct cfi_qry
); x
+= 16) {
272 for (y
= 0; y
< 16; y
++)
273 debug("%2.2x ", p
[x
+ y
]);
275 for (y
= 0; y
< 16; y
++) {
276 unsigned char c
= p
[x
+ y
];
278 if (c
>= 0x20 && c
<= 0x7e)
288 /*-----------------------------------------------------------------------
289 * read a character at a port width address
291 static inline uchar
flash_read_uchar(flash_info_t
*info
, uint offset
)
296 cp
= flash_map(info
, 0, offset
);
297 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
298 retval
= flash_read8(cp
);
300 retval
= flash_read8(cp
+ info
->portwidth
- 1);
302 flash_unmap(info
, 0, offset
, cp
);
306 /*-----------------------------------------------------------------------
307 * read a word at a port width address, assume 16bit bus
309 static inline ushort
flash_read_word(flash_info_t
*info
, uint offset
)
311 ushort
*addr
, retval
;
313 addr
= flash_map(info
, 0, offset
);
314 retval
= flash_read16(addr
);
315 flash_unmap(info
, 0, offset
, addr
);
319 /*-----------------------------------------------------------------------
320 * read a long word by picking the least significant byte of each maximum
321 * port size word. Swap for ppc format.
323 static ulong
flash_read_long (flash_info_t
*info
, flash_sect_t sect
,
332 addr
= flash_map(info
, sect
, offset
);
335 debug("long addr is at %p info->portwidth = %d\n", addr
,
337 for (x
= 0; x
< 4 * info
->portwidth
; x
++)
338 debug("addr[%x] = 0x%x\n", x
, flash_read8(addr
+ x
));
340 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
341 retval
= ((flash_read8(addr
) << 16) |
342 (flash_read8(addr
+ info
->portwidth
) << 24) |
343 (flash_read8(addr
+ 2 * info
->portwidth
)) |
344 (flash_read8(addr
+ 3 * info
->portwidth
) << 8));
346 retval
= ((flash_read8(addr
+ 2 * info
->portwidth
- 1) << 24) |
347 (flash_read8(addr
+ info
->portwidth
- 1) << 16) |
348 (flash_read8(addr
+ 4 * info
->portwidth
- 1) << 8) |
349 (flash_read8(addr
+ 3 * info
->portwidth
- 1)));
351 flash_unmap(info
, sect
, offset
, addr
);
357 * Write a proper sized command to the correct address
359 static void flash_write_cmd(flash_info_t
*info
, flash_sect_t sect
,
360 uint offset
, u32 cmd
)
365 addr
= flash_map(info
, sect
, offset
);
366 flash_make_cmd(info
, cmd
, &cword
);
367 switch (info
->portwidth
) {
369 debug("fwc addr %p cmd %x %x 8bit x %d bit\n", addr
, cmd
,
370 cword
.w8
, info
->chipwidth
<< CFI_FLASH_SHIFT_WIDTH
);
371 flash_write8(cword
.w8
, addr
);
373 case FLASH_CFI_16BIT
:
374 debug("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr
,
376 info
->chipwidth
<< CFI_FLASH_SHIFT_WIDTH
);
377 flash_write16(cword
.w16
, addr
);
379 case FLASH_CFI_32BIT
:
380 debug("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr
,
382 info
->chipwidth
<< CFI_FLASH_SHIFT_WIDTH
);
383 flash_write32(cword
.w32
, addr
);
385 case FLASH_CFI_64BIT
:
390 print_longlong(str
, cword
.w64
);
392 debug("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
394 info
->chipwidth
<< CFI_FLASH_SHIFT_WIDTH
);
397 flash_write64(cword
.w64
, addr
);
401 /* Ensure all the instructions are fully finished */
404 flash_unmap(info
, sect
, offset
, addr
);
407 static void flash_unlock_seq(flash_info_t
*info
, flash_sect_t sect
)
409 flash_write_cmd(info
, sect
, info
->addr_unlock1
, AMD_CMD_UNLOCK_START
);
410 flash_write_cmd(info
, sect
, info
->addr_unlock2
, AMD_CMD_UNLOCK_ACK
);
413 /*-----------------------------------------------------------------------
415 static int flash_isequal(flash_info_t
*info
, flash_sect_t sect
, uint offset
,
422 addr
= flash_map(info
, sect
, offset
);
423 flash_make_cmd(info
, cmd
, &cword
);
425 debug("is= cmd %x(%c) addr %p ", cmd
, cmd
, addr
);
426 switch (info
->portwidth
) {
428 debug("is= %x %x\n", flash_read8(addr
), cword
.w8
);
429 retval
= (flash_read8(addr
) == cword
.w8
);
431 case FLASH_CFI_16BIT
:
432 debug("is= %4.4x %4.4x\n", flash_read16(addr
), cword
.w16
);
433 retval
= (flash_read16(addr
) == cword
.w16
);
435 case FLASH_CFI_32BIT
:
436 debug("is= %8.8x %8.8x\n", flash_read32(addr
), cword
.w32
);
437 retval
= (flash_read32(addr
) == cword
.w32
);
439 case FLASH_CFI_64BIT
:
445 print_longlong(str1
, flash_read64(addr
));
446 print_longlong(str2
, cword
.w64
);
447 debug("is= %s %s\n", str1
, str2
);
450 retval
= (flash_read64(addr
) == cword
.w64
);
456 flash_unmap(info
, sect
, offset
, addr
);
461 /*-----------------------------------------------------------------------
463 static int flash_isset(flash_info_t
*info
, flash_sect_t sect
, uint offset
,
470 addr
= flash_map(info
, sect
, offset
);
471 flash_make_cmd(info
, cmd
, &cword
);
472 switch (info
->portwidth
) {
474 retval
= ((flash_read8(addr
) & cword
.w8
) == cword
.w8
);
476 case FLASH_CFI_16BIT
:
477 retval
= ((flash_read16(addr
) & cword
.w16
) == cword
.w16
);
479 case FLASH_CFI_32BIT
:
480 retval
= ((flash_read32(addr
) & cword
.w32
) == cword
.w32
);
482 case FLASH_CFI_64BIT
:
483 retval
= ((flash_read64(addr
) & cword
.w64
) == cword
.w64
);
489 flash_unmap(info
, sect
, offset
, addr
);
494 /*-----------------------------------------------------------------------
496 static int flash_toggle(flash_info_t
*info
, flash_sect_t sect
, uint offset
,
503 addr
= flash_map(info
, sect
, offset
);
504 flash_make_cmd(info
, cmd
, &cword
);
505 switch (info
->portwidth
) {
507 retval
= flash_read8(addr
) != flash_read8(addr
);
509 case FLASH_CFI_16BIT
:
510 retval
= flash_read16(addr
) != flash_read16(addr
);
512 case FLASH_CFI_32BIT
:
513 retval
= flash_read32(addr
) != flash_read32(addr
);
515 case FLASH_CFI_64BIT
:
516 retval
= ((flash_read32(addr
) != flash_read32(addr
)) ||
517 (flash_read32(addr
+ 4) != flash_read32(addr
+ 4)));
523 flash_unmap(info
, sect
, offset
, addr
);
529 * flash_is_busy - check to see if the flash is busy
531 * This routine checks the status of the chip and returns true if the
534 static int flash_is_busy(flash_info_t
*info
, flash_sect_t sect
)
538 switch (info
->vendor
) {
539 case CFI_CMDSET_INTEL_PROG_REGIONS
:
540 case CFI_CMDSET_INTEL_STANDARD
:
541 case CFI_CMDSET_INTEL_EXTENDED
:
542 retval
= !flash_isset(info
, sect
, 0, FLASH_STATUS_DONE
);
544 case CFI_CMDSET_AMD_STANDARD
:
545 case CFI_CMDSET_AMD_EXTENDED
:
546 #ifdef CONFIG_FLASH_CFI_LEGACY
547 case CFI_CMDSET_AMD_LEGACY
:
549 if (info
->sr_supported
) {
550 flash_write_cmd(info
, sect
, info
->addr_unlock1
,
551 FLASH_CMD_READ_STATUS
);
552 retval
= !flash_isset(info
, sect
, 0,
555 retval
= flash_toggle(info
, sect
, 0,
563 debug("%s: %d\n", __func__
, retval
);
567 /*-----------------------------------------------------------------------
568 * wait for XSR.7 to be set. Time out with an error if it does not.
569 * This routine does not set the flash to read-array mode.
571 static int flash_status_check(flash_info_t
*info
, flash_sect_t sector
,
572 ulong tout
, char *prompt
)
576 #if CONFIG_SYS_HZ != 1000
577 /* Avoid overflow for large HZ */
578 if ((ulong
)CONFIG_SYS_HZ
> 100000)
579 tout
*= (ulong
)CONFIG_SYS_HZ
/ 1000;
581 tout
= DIV_ROUND_UP(tout
* (ulong
)CONFIG_SYS_HZ
, 1000);
584 /* Wait for command completion */
585 #ifdef CONFIG_SYS_LOW_RES_TIMER
588 start
= get_timer(0);
590 while (flash_is_busy(info
, sector
)) {
591 if (get_timer(start
) > tout
) {
592 printf("Flash %s timeout at address %lx data %lx\n",
593 prompt
, info
->start
[sector
],
594 flash_read_long(info
, sector
, 0));
595 flash_write_cmd(info
, sector
, 0, info
->cmd_reset
);
599 udelay(1); /* also triggers watchdog */
604 /*-----------------------------------------------------------------------
605 * Wait for XSR.7 to be set, if it times out print an error, otherwise
606 * do a full status check.
608 * This routine sets the flash to read-array mode.
610 static int flash_full_status_check(flash_info_t
*info
, flash_sect_t sector
,
611 ulong tout
, char *prompt
)
615 retcode
= flash_status_check(info
, sector
, tout
, prompt
);
616 switch (info
->vendor
) {
617 case CFI_CMDSET_INTEL_PROG_REGIONS
:
618 case CFI_CMDSET_INTEL_EXTENDED
:
619 case CFI_CMDSET_INTEL_STANDARD
:
620 if (retcode
== ERR_OK
&&
621 !flash_isset(info
, sector
, 0, FLASH_STATUS_DONE
)) {
623 printf("Flash %s error at address %lx\n", prompt
,
624 info
->start
[sector
]);
625 if (flash_isset(info
, sector
, 0, FLASH_STATUS_ECLBS
|
626 FLASH_STATUS_PSLBS
)) {
627 puts("Command Sequence Error.\n");
628 } else if (flash_isset(info
, sector
, 0,
629 FLASH_STATUS_ECLBS
)) {
630 puts("Block Erase Error.\n");
631 retcode
= ERR_NOT_ERASED
;
632 } else if (flash_isset(info
, sector
, 0,
633 FLASH_STATUS_PSLBS
)) {
634 puts("Locking Error\n");
636 if (flash_isset(info
, sector
, 0, FLASH_STATUS_DPS
)) {
637 puts("Block locked.\n");
638 retcode
= ERR_PROTECTED
;
640 if (flash_isset(info
, sector
, 0, FLASH_STATUS_VPENS
))
641 puts("Vpp Low Error.\n");
643 flash_write_cmd(info
, sector
, 0, info
->cmd_reset
);
652 static int use_flash_status_poll(flash_info_t
*info
)
654 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
655 if (info
->vendor
== CFI_CMDSET_AMD_EXTENDED
||
656 info
->vendor
== CFI_CMDSET_AMD_STANDARD
)
662 static int flash_status_poll(flash_info_t
*info
, void *src
, void *dst
,
663 ulong tout
, char *prompt
)
665 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
669 #if CONFIG_SYS_HZ != 1000
670 /* Avoid overflow for large HZ */
671 if ((ulong
)CONFIG_SYS_HZ
> 100000)
672 tout
*= (ulong
)CONFIG_SYS_HZ
/ 1000;
674 tout
= DIV_ROUND_UP(tout
* (ulong
)CONFIG_SYS_HZ
, 1000);
677 /* Wait for command completion */
678 #ifdef CONFIG_SYS_LOW_RES_TIMER
681 start
= get_timer(0);
684 switch (info
->portwidth
) {
686 ready
= flash_read8(dst
) == flash_read8(src
);
688 case FLASH_CFI_16BIT
:
689 ready
= flash_read16(dst
) == flash_read16(src
);
691 case FLASH_CFI_32BIT
:
692 ready
= flash_read32(dst
) == flash_read32(src
);
694 case FLASH_CFI_64BIT
:
695 ready
= flash_read64(dst
) == flash_read64(src
);
703 if (get_timer(start
) > tout
) {
704 printf("Flash %s timeout at address %lx data %lx\n",
705 prompt
, (ulong
)dst
, (ulong
)flash_read8(dst
));
708 udelay(1); /* also triggers watchdog */
710 #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
714 /*-----------------------------------------------------------------------
716 static void flash_add_byte(flash_info_t
*info
, cfiword_t
*cword
, uchar c
)
718 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
721 unsigned long long ll
;
724 switch (info
->portwidth
) {
728 case FLASH_CFI_16BIT
:
729 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
732 cword
->w16
= (cword
->w16
>> 8) | w
;
734 cword
->w16
= (cword
->w16
<< 8) | c
;
737 case FLASH_CFI_32BIT
:
738 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
741 cword
->w32
= (cword
->w32
>> 8) | l
;
743 cword
->w32
= (cword
->w32
<< 8) | c
;
746 case FLASH_CFI_64BIT
:
747 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
750 cword
->w64
= (cword
->w64
>> 8) | ll
;
752 cword
->w64
= (cword
->w64
<< 8) | c
;
759 * Loop through the sector table starting from the previously found sector.
760 * Searches forwards or backwards, dependent on the passed address.
762 static flash_sect_t
find_sector(flash_info_t
*info
, ulong addr
)
764 static flash_sect_t saved_sector
; /* previously found sector */
765 static flash_info_t
*saved_info
; /* previously used flash bank */
766 flash_sect_t sector
= saved_sector
;
768 if (info
!= saved_info
|| sector
>= info
->sector_count
)
771 while ((sector
< info
->sector_count
- 1) &&
772 (info
->start
[sector
] < addr
))
774 while ((info
->start
[sector
] > addr
) && (sector
> 0))
776 * also decrements the sector in case of an overshot
781 saved_sector
= sector
;
786 /*-----------------------------------------------------------------------
788 static int flash_write_cfiword(flash_info_t
*info
, ulong dest
, cfiword_t cword
)
790 void *dstaddr
= (void *)dest
;
792 flash_sect_t sect
= 0;
795 /* Check if Flash is (sufficiently) erased */
796 switch (info
->portwidth
) {
798 flag
= ((flash_read8(dstaddr
) & cword
.w8
) == cword
.w8
);
800 case FLASH_CFI_16BIT
:
801 flag
= ((flash_read16(dstaddr
) & cword
.w16
) == cword
.w16
);
803 case FLASH_CFI_32BIT
:
804 flag
= ((flash_read32(dstaddr
) & cword
.w32
) == cword
.w32
);
806 case FLASH_CFI_64BIT
:
807 flag
= ((flash_read64(dstaddr
) & cword
.w64
) == cword
.w64
);
814 return ERR_NOT_ERASED
;
816 /* Disable interrupts which might cause a timeout here */
817 flag
= disable_interrupts();
819 switch (info
->vendor
) {
820 case CFI_CMDSET_INTEL_PROG_REGIONS
:
821 case CFI_CMDSET_INTEL_EXTENDED
:
822 case CFI_CMDSET_INTEL_STANDARD
:
823 flash_write_cmd(info
, 0, 0, FLASH_CMD_CLEAR_STATUS
);
824 flash_write_cmd(info
, 0, 0, FLASH_CMD_WRITE
);
826 case CFI_CMDSET_AMD_EXTENDED
:
827 case CFI_CMDSET_AMD_STANDARD
:
828 sect
= find_sector(info
, dest
);
829 flash_unlock_seq(info
, sect
);
830 flash_write_cmd(info
, sect
, info
->addr_unlock1
, AMD_CMD_WRITE
);
833 #ifdef CONFIG_FLASH_CFI_LEGACY
834 case CFI_CMDSET_AMD_LEGACY
:
835 sect
= find_sector(info
, dest
);
836 flash_unlock_seq(info
, 0);
837 flash_write_cmd(info
, 0, info
->addr_unlock1
, AMD_CMD_WRITE
);
843 switch (info
->portwidth
) {
845 flash_write8(cword
.w8
, dstaddr
);
847 case FLASH_CFI_16BIT
:
848 flash_write16(cword
.w16
, dstaddr
);
850 case FLASH_CFI_32BIT
:
851 flash_write32(cword
.w32
, dstaddr
);
853 case FLASH_CFI_64BIT
:
854 flash_write64(cword
.w64
, dstaddr
);
858 /* re-enable interrupts if necessary */
863 sect
= find_sector(info
, dest
);
865 if (use_flash_status_poll(info
))
866 return flash_status_poll(info
, &cword
, dstaddr
,
867 info
->write_tout
, "write");
869 return flash_full_status_check(info
, sect
,
870 info
->write_tout
, "write");
873 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
875 static int flash_write_cfibuffer(flash_info_t
*info
, ulong dest
, uchar
*cp
,
882 u8
*dst
= (u8
*)dest
;
889 switch (info
->portwidth
) {
893 case FLASH_CFI_16BIT
:
896 case FLASH_CFI_32BIT
:
899 case FLASH_CFI_64BIT
:
909 while ((cnt
-- > 0) && (flag
== 1)) {
910 switch (info
->portwidth
) {
912 flag
= ((flash_read8(dst2
) & flash_read8(src
)) ==
916 case FLASH_CFI_16BIT
:
917 flag
= ((flash_read16(dst2
) & flash_read16(src
)) ==
921 case FLASH_CFI_32BIT
:
922 flag
= ((flash_read32(dst2
) & flash_read32(src
)) ==
926 case FLASH_CFI_64BIT
:
927 flag
= ((flash_read64(dst2
) & flash_read64(src
)) ==
934 retcode
= ERR_NOT_ERASED
;
939 sector
= find_sector(info
, dest
);
941 switch (info
->vendor
) {
942 case CFI_CMDSET_INTEL_PROG_REGIONS
:
943 case CFI_CMDSET_INTEL_STANDARD
:
944 case CFI_CMDSET_INTEL_EXTENDED
:
945 write_cmd
= (info
->vendor
== CFI_CMDSET_INTEL_PROG_REGIONS
) ?
946 FLASH_CMD_WRITE_BUFFER_PROG
:
947 FLASH_CMD_WRITE_TO_BUFFER
;
948 flash_write_cmd(info
, sector
, 0, FLASH_CMD_CLEAR_STATUS
);
949 flash_write_cmd(info
, sector
, 0, FLASH_CMD_READ_STATUS
);
950 flash_write_cmd(info
, sector
, 0, write_cmd
);
951 retcode
= flash_status_check(info
, sector
,
952 info
->buffer_write_tout
,
954 if (retcode
== ERR_OK
) {
955 /* reduce the number of loops by the width of
959 flash_write_cmd(info
, sector
, 0, cnt
- 1);
961 switch (info
->portwidth
) {
963 flash_write8(flash_read8(src
), dst
);
966 case FLASH_CFI_16BIT
:
967 flash_write16(flash_read16(src
), dst
);
970 case FLASH_CFI_32BIT
:
971 flash_write32(flash_read32(src
), dst
);
974 case FLASH_CFI_64BIT
:
975 flash_write64(flash_read64(src
), dst
);
983 flash_write_cmd(info
, sector
, 0,
984 FLASH_CMD_WRITE_BUFFER_CONFIRM
);
985 retcode
= flash_full_status_check(
986 info
, sector
, info
->buffer_write_tout
,
992 case CFI_CMDSET_AMD_STANDARD
:
993 case CFI_CMDSET_AMD_EXTENDED
:
994 flash_unlock_seq(info
, sector
);
996 #ifdef CONFIG_FLASH_SPANSION_S29WS_N
997 offset
= ((unsigned long)dst
- info
->start
[sector
]) >> shift
;
999 flash_write_cmd(info
, sector
, offset
, AMD_CMD_WRITE_TO_BUFFER
);
1001 flash_write_cmd(info
, sector
, offset
, cnt
- 1);
1003 switch (info
->portwidth
) {
1004 case FLASH_CFI_8BIT
:
1006 flash_write8(flash_read8(src
), dst
);
1010 case FLASH_CFI_16BIT
:
1012 flash_write16(flash_read16(src
), dst
);
1016 case FLASH_CFI_32BIT
:
1018 flash_write32(flash_read32(src
), dst
);
1022 case FLASH_CFI_64BIT
:
1024 flash_write64(flash_read64(src
), dst
);
1029 retcode
= ERR_INVAL
;
1033 flash_write_cmd(info
, sector
, 0, AMD_CMD_WRITE_BUFFER_CONFIRM
);
1034 if (use_flash_status_poll(info
))
1035 retcode
= flash_status_poll(info
, src
- (1 << shift
),
1037 info
->buffer_write_tout
,
1040 retcode
= flash_full_status_check(info
, sector
,
1041 info
->buffer_write_tout
,
1046 debug("Unknown Command Set\n");
1047 retcode
= ERR_INVAL
;
1054 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1056 /*-----------------------------------------------------------------------
1058 int flash_erase(flash_info_t
*info
, int s_first
, int s_last
)
1065 if (info
->flash_id
!= FLASH_MAN_CFI
) {
1066 puts("Can't erase unknown flash type - aborted\n");
1069 if (s_first
< 0 || s_first
> s_last
) {
1070 puts("- no sectors to erase\n");
1075 for (sect
= s_first
; sect
<= s_last
; ++sect
)
1076 if (info
->protect
[sect
])
1079 printf("- Warning: %d protected sectors will not be erased!\n",
1081 } else if (flash_verbose
) {
1085 for (sect
= s_first
; sect
<= s_last
; sect
++) {
1091 if (info
->protect
[sect
] == 0) { /* not protected */
1092 #ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1099 * Check if whole sector is erased
1101 size
= flash_sector_size(info
, sect
);
1103 flash
= (u32
*)info
->start
[sect
];
1104 /* divide by 4 for longword access */
1106 for (k
= 0; k
< size
; k
++) {
1107 if (flash_read32(flash
++) != 0xffffffff) {
1118 switch (info
->vendor
) {
1119 case CFI_CMDSET_INTEL_PROG_REGIONS
:
1120 case CFI_CMDSET_INTEL_STANDARD
:
1121 case CFI_CMDSET_INTEL_EXTENDED
:
1122 flash_write_cmd(info
, sect
, 0,
1123 FLASH_CMD_CLEAR_STATUS
);
1124 flash_write_cmd(info
, sect
, 0,
1125 FLASH_CMD_BLOCK_ERASE
);
1126 flash_write_cmd(info
, sect
, 0,
1127 FLASH_CMD_ERASE_CONFIRM
);
1129 case CFI_CMDSET_AMD_STANDARD
:
1130 case CFI_CMDSET_AMD_EXTENDED
:
1131 flash_unlock_seq(info
, sect
);
1132 flash_write_cmd(info
, sect
,
1134 AMD_CMD_ERASE_START
);
1135 flash_unlock_seq(info
, sect
);
1136 flash_write_cmd(info
, sect
, 0,
1137 info
->cmd_erase_sector
);
1139 #ifdef CONFIG_FLASH_CFI_LEGACY
1140 case CFI_CMDSET_AMD_LEGACY
:
1141 flash_unlock_seq(info
, 0);
1142 flash_write_cmd(info
, 0, info
->addr_unlock1
,
1143 AMD_CMD_ERASE_START
);
1144 flash_unlock_seq(info
, 0);
1145 flash_write_cmd(info
, sect
, 0,
1146 AMD_CMD_ERASE_SECTOR
);
1150 debug("Unknown flash vendor %d\n",
1155 if (use_flash_status_poll(info
)) {
1159 cword
.w64
= 0xffffffffffffffffULL
;
1160 dest
= flash_map(info
, sect
, 0);
1161 st
= flash_status_poll(info
, &cword
, dest
,
1162 info
->erase_blk_tout
,
1164 flash_unmap(info
, sect
, 0, dest
);
1166 st
= flash_full_status_check(info
, sect
,
1167 info
->erase_blk_tout
,
1173 else if (flash_verbose
)
1184 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1185 static int sector_erased(flash_info_t
*info
, int i
)
1192 * Check if whole sector is erased
1194 size
= flash_sector_size(info
, i
);
1195 flash
= (u32
*)info
->start
[i
];
1196 /* divide by 4 for longword access */
1199 for (k
= 0; k
< size
; k
++) {
1200 if (flash_read32(flash
++) != 0xffffffff)
1201 return 0; /* not erased */
1204 return 1; /* erased */
1206 #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1208 void flash_print_info(flash_info_t
*info
)
1212 if (info
->flash_id
!= FLASH_MAN_CFI
) {
1213 puts("missing or unknown FLASH type\n");
1217 printf("%s flash (%d x %d)",
1219 (info
->portwidth
<< 3), (info
->chipwidth
<< 3));
1220 if (info
->size
< 1024 * 1024)
1221 printf(" Size: %ld kB in %d Sectors\n",
1222 info
->size
>> 10, info
->sector_count
);
1224 printf(" Size: %ld MB in %d Sectors\n",
1225 info
->size
>> 20, info
->sector_count
);
1227 switch (info
->vendor
) {
1228 case CFI_CMDSET_INTEL_PROG_REGIONS
:
1229 printf("Intel Prog Regions");
1231 case CFI_CMDSET_INTEL_STANDARD
:
1232 printf("Intel Standard");
1234 case CFI_CMDSET_INTEL_EXTENDED
:
1235 printf("Intel Extended");
1237 case CFI_CMDSET_AMD_STANDARD
:
1238 printf("AMD Standard");
1240 case CFI_CMDSET_AMD_EXTENDED
:
1241 printf("AMD Extended");
1243 #ifdef CONFIG_FLASH_CFI_LEGACY
1244 case CFI_CMDSET_AMD_LEGACY
:
1245 printf("AMD Legacy");
1249 printf("Unknown (%d)", info
->vendor
);
1252 printf(" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1253 info
->manufacturer_id
);
1254 printf(info
->chipwidth
== FLASH_CFI_16BIT
? "%04X" : "%02X",
1256 if ((info
->device_id
& 0xff) == 0x7E) {
1257 printf(info
->chipwidth
== FLASH_CFI_16BIT
? "%04X" : "%02X",
1260 if (info
->vendor
== CFI_CMDSET_AMD_STANDARD
&& info
->legacy_unlock
)
1261 printf("\n Advanced Sector Protection (PPB) enabled");
1262 printf("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1263 info
->erase_blk_tout
, info
->write_tout
);
1264 if (info
->buffer_size
> 1) {
1265 printf(" Buffer write timeout: %ld ms, ",
1266 info
->buffer_write_tout
);
1267 printf("buffer size: %d bytes\n", info
->buffer_size
);
1270 puts("\n Sector Start Addresses:");
1271 for (i
= 0; i
< info
->sector_count
; ++i
) {
1276 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1277 /* print empty and read-only info */
1278 printf(" %08lX %c %s ",
1280 sector_erased(info
, i
) ? 'E' : ' ',
1281 info
->protect
[i
] ? "RO" : " ");
1282 #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
1283 printf(" %08lX %s ",
1285 info
->protect
[i
] ? "RO" : " ");
1291 /*-----------------------------------------------------------------------
1292 * This is used in a few places in write_buf() to show programming
1293 * progress. Making it a function is nasty because it needs to do side
1294 * effect updates to digit and dots. Repeated code is nasty too, so
1295 * we define it once here.
1297 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1298 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
1299 if (flash_verbose) { \
1301 if (scale > 0 && dots <= 0) { \
1302 if ((digit % 5) == 0) \
1303 printf("%d", digit / 5); \
1311 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1314 /*-----------------------------------------------------------------------
1315 * Copy memory to flash, returns:
1318 * 2 - Flash not erased
1320 int write_buff(flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
1327 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1330 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1331 int digit
= CONFIG_FLASH_SHOW_PROGRESS
;
1336 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1338 if (cnt
>= CONFIG_FLASH_SHOW_PROGRESS
) {
1339 scale
= (int)((cnt
+ CONFIG_FLASH_SHOW_PROGRESS
- 1) /
1340 CONFIG_FLASH_SHOW_PROGRESS
);
1344 /* get lower aligned address */
1345 wp
= (addr
& ~(info
->portwidth
- 1));
1347 /* handle unaligned start */
1352 for (i
= 0; i
< aln
; ++i
)
1353 flash_add_byte(info
, &cword
, flash_read8(p
+ i
));
1355 for (; (i
< info
->portwidth
) && (cnt
> 0); i
++) {
1356 flash_add_byte(info
, &cword
, *src
++);
1359 for (; (cnt
== 0) && (i
< info
->portwidth
); ++i
)
1360 flash_add_byte(info
, &cword
, flash_read8(p
+ i
));
1362 rc
= flash_write_cfiword(info
, wp
, cword
);
1367 FLASH_SHOW_PROGRESS(scale
, dots
, digit
, i
);
1370 /* handle the aligned part */
1371 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1372 buffered_size
= (info
->portwidth
/ info
->chipwidth
);
1373 buffered_size
*= info
->buffer_size
;
1374 while (cnt
>= info
->portwidth
) {
1375 /* prohibit buffer write when buffer_size is 1 */
1376 if (info
->buffer_size
== 1) {
1378 for (i
= 0; i
< info
->portwidth
; i
++)
1379 flash_add_byte(info
, &cword
, *src
++);
1380 rc
= flash_write_cfiword(info
, wp
, cword
);
1383 wp
+= info
->portwidth
;
1384 cnt
-= info
->portwidth
;
1388 /* write buffer until next buffered_size aligned boundary */
1389 i
= buffered_size
- (wp
% buffered_size
);
1392 rc
= flash_write_cfibuffer(info
, wp
, src
, i
);
1395 i
-= i
& (info
->portwidth
- 1);
1399 FLASH_SHOW_PROGRESS(scale
, dots
, digit
, i
);
1400 /* Only check every once in a while */
1401 if ((cnt
& 0xFFFF) < buffered_size
&& ctrlc())
1405 while (cnt
>= info
->portwidth
) {
1407 for (i
= 0; i
< info
->portwidth
; i
++)
1408 flash_add_byte(info
, &cword
, *src
++);
1409 rc
= flash_write_cfiword(info
, wp
, cword
);
1412 wp
+= info
->portwidth
;
1413 cnt
-= info
->portwidth
;
1414 FLASH_SHOW_PROGRESS(scale
, dots
, digit
, info
->portwidth
);
1415 /* Only check every once in a while */
1416 if ((cnt
& 0xFFFF) < info
->portwidth
&& ctrlc())
1419 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1425 * handle unaligned tail bytes
1429 for (i
= 0; (i
< info
->portwidth
) && (cnt
> 0); ++i
) {
1430 flash_add_byte(info
, &cword
, *src
++);
1433 for (; i
< info
->portwidth
; ++i
)
1434 flash_add_byte(info
, &cword
, flash_read8(p
+ i
));
1436 return flash_write_cfiword(info
, wp
, cword
);
1439 static inline int manufact_match(flash_info_t
*info
, u32 manu
)
1441 return info
->manufacturer_id
== ((manu
& FLASH_VENDMASK
) >> 16);
1444 /*-----------------------------------------------------------------------
1446 #ifdef CONFIG_SYS_FLASH_PROTECTION
1448 static int cfi_protect_bugfix(flash_info_t
*info
, long sector
, int prot
)
1450 if (manufact_match(info
, INTEL_MANUFACT
) &&
1451 info
->device_id
== NUMONYX_256MBIT
) {
1454 * "Numonyx Axcell P33/P30 Specification Update" :)
1456 flash_write_cmd(info
, sector
, 0, FLASH_CMD_READ_ID
);
1457 if (!flash_isequal(info
, sector
, FLASH_OFFSET_PROTECT
,
1460 * cmd must come before FLASH_CMD_PROTECT + 20us
1461 * Disable interrupts which might cause a timeout here.
1463 int flag
= disable_interrupts();
1467 cmd
= FLASH_CMD_PROTECT_SET
;
1469 cmd
= FLASH_CMD_PROTECT_CLEAR
;
1471 flash_write_cmd(info
, sector
, 0, FLASH_CMD_PROTECT
);
1472 flash_write_cmd(info
, sector
, 0, cmd
);
1473 /* re-enable interrupts if necessary */
1475 enable_interrupts();
1482 int flash_real_protect(flash_info_t
*info
, long sector
, int prot
)
1486 switch (info
->vendor
) {
1487 case CFI_CMDSET_INTEL_PROG_REGIONS
:
1488 case CFI_CMDSET_INTEL_STANDARD
:
1489 case CFI_CMDSET_INTEL_EXTENDED
:
1490 if (!cfi_protect_bugfix(info
, sector
, prot
)) {
1491 flash_write_cmd(info
, sector
, 0,
1492 FLASH_CMD_CLEAR_STATUS
);
1493 flash_write_cmd(info
, sector
, 0,
1496 flash_write_cmd(info
, sector
, 0,
1497 FLASH_CMD_PROTECT_SET
);
1499 flash_write_cmd(info
, sector
, 0,
1500 FLASH_CMD_PROTECT_CLEAR
);
1503 case CFI_CMDSET_AMD_EXTENDED
:
1504 case CFI_CMDSET_AMD_STANDARD
:
1505 /* U-Boot only checks the first byte */
1506 if (manufact_match(info
, ATM_MANUFACT
)) {
1508 flash_unlock_seq(info
, 0);
1509 flash_write_cmd(info
, 0,
1511 ATM_CMD_SOFTLOCK_START
);
1512 flash_unlock_seq(info
, 0);
1513 flash_write_cmd(info
, sector
, 0,
1516 flash_write_cmd(info
, 0,
1518 AMD_CMD_UNLOCK_START
);
1519 if (info
->device_id
== ATM_ID_BV6416
)
1520 flash_write_cmd(info
, sector
,
1521 0, ATM_CMD_UNLOCK_SECT
);
1524 if (info
->legacy_unlock
) {
1525 int flag
= disable_interrupts();
1528 flash_unlock_seq(info
, 0);
1529 flash_write_cmd(info
, 0, info
->addr_unlock1
,
1530 AMD_CMD_SET_PPB_ENTRY
);
1531 lock_flag
= flash_isset(info
, sector
, 0, 0x01);
1534 flash_write_cmd(info
, sector
, 0,
1535 AMD_CMD_PPB_LOCK_BC1
);
1536 flash_write_cmd(info
, sector
, 0,
1537 AMD_CMD_PPB_LOCK_BC2
);
1539 debug("sector %ld %slocked\n", sector
,
1540 lock_flag
? "" : "already ");
1543 debug("unlock %ld\n", sector
);
1544 flash_write_cmd(info
, 0, 0,
1545 AMD_CMD_PPB_UNLOCK_BC1
);
1546 flash_write_cmd(info
, 0, 0,
1547 AMD_CMD_PPB_UNLOCK_BC2
);
1549 debug("sector %ld %sunlocked\n", sector
,
1550 !lock_flag
? "" : "already ");
1553 enable_interrupts();
1555 if (flash_status_check(info
, sector
,
1556 info
->erase_blk_tout
,
1557 prot
? "protect" : "unprotect"))
1558 printf("status check error\n");
1560 flash_write_cmd(info
, 0, 0,
1561 AMD_CMD_SET_PPB_EXIT_BC1
);
1562 flash_write_cmd(info
, 0, 0,
1563 AMD_CMD_SET_PPB_EXIT_BC2
);
1566 #ifdef CONFIG_FLASH_CFI_LEGACY
1567 case CFI_CMDSET_AMD_LEGACY
:
1568 flash_write_cmd(info
, sector
, 0, FLASH_CMD_CLEAR_STATUS
);
1569 flash_write_cmd(info
, sector
, 0, FLASH_CMD_PROTECT
);
1571 flash_write_cmd(info
, sector
, 0,
1572 FLASH_CMD_PROTECT_SET
);
1574 flash_write_cmd(info
, sector
, 0,
1575 FLASH_CMD_PROTECT_CLEAR
);
1580 * Flash needs to be in status register read mode for
1581 * flash_full_status_check() to work correctly
1583 flash_write_cmd(info
, sector
, 0, FLASH_CMD_READ_STATUS
);
1584 retcode
= flash_full_status_check(info
, sector
, info
->erase_blk_tout
,
1585 prot
? "protect" : "unprotect");
1587 info
->protect
[sector
] = prot
;
1590 * On some of Intel's flash chips (marked via legacy_unlock)
1591 * unprotect unprotects all locking.
1593 if (prot
== 0 && info
->legacy_unlock
) {
1596 for (i
= 0; i
< info
->sector_count
; i
++) {
1597 if (info
->protect
[i
])
1598 flash_real_protect(info
, i
, 1);
1605 /*-----------------------------------------------------------------------
1606 * flash_read_user_serial - read the OneTimeProgramming cells
1608 void flash_read_user_serial(flash_info_t
*info
, void *buffer
, int offset
,
1615 src
= flash_map(info
, 0, FLASH_OFFSET_USER_PROTECTION
);
1616 flash_write_cmd(info
, 0, 0, FLASH_CMD_READ_ID
);
1617 memcpy(dst
, src
+ offset
, len
);
1618 flash_write_cmd(info
, 0, 0, info
->cmd_reset
);
1620 flash_unmap(info
, 0, FLASH_OFFSET_USER_PROTECTION
, src
);
1624 * flash_read_factory_serial - read the device Id from the protection area
1626 void flash_read_factory_serial(flash_info_t
*info
, void *buffer
, int offset
,
1631 src
= flash_map(info
, 0, FLASH_OFFSET_INTEL_PROTECTION
);
1632 flash_write_cmd(info
, 0, 0, FLASH_CMD_READ_ID
);
1633 memcpy(buffer
, src
+ offset
, len
);
1634 flash_write_cmd(info
, 0, 0, info
->cmd_reset
);
1636 flash_unmap(info
, 0, FLASH_OFFSET_INTEL_PROTECTION
, src
);
1639 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1641 /*-----------------------------------------------------------------------
1642 * Reverse the order of the erase regions in the CFI QRY structure.
1643 * This is needed for chips that are either a) correctly detected as
1644 * top-boot, or b) buggy.
1646 static void cfi_reverse_geometry(struct cfi_qry
*qry
)
1651 for (i
= 0, j
= qry
->num_erase_regions
- 1; i
< j
; i
++, j
--) {
1652 tmp
= get_unaligned(&qry
->erase_region_info
[i
]);
1653 put_unaligned(get_unaligned(&qry
->erase_region_info
[j
]),
1654 &qry
->erase_region_info
[i
]);
1655 put_unaligned(tmp
, &qry
->erase_region_info
[j
]);
1659 /*-----------------------------------------------------------------------
1660 * read jedec ids from device and set corresponding fields in info struct
1662 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1665 static void cmdset_intel_read_jedec_ids(flash_info_t
*info
)
1667 flash_write_cmd(info
, 0, 0, FLASH_CMD_RESET
);
1669 flash_write_cmd(info
, 0, 0, FLASH_CMD_READ_ID
);
1670 udelay(1000); /* some flash are slow to respond */
1671 info
->manufacturer_id
= flash_read_uchar(info
,
1672 FLASH_OFFSET_MANUFACTURER_ID
);
1673 info
->device_id
= (info
->chipwidth
== FLASH_CFI_16BIT
) ?
1674 flash_read_word(info
, FLASH_OFFSET_DEVICE_ID
) :
1675 flash_read_uchar(info
, FLASH_OFFSET_DEVICE_ID
);
1676 flash_write_cmd(info
, 0, 0, FLASH_CMD_RESET
);
1679 static int cmdset_intel_init(flash_info_t
*info
, struct cfi_qry
*qry
)
1681 info
->cmd_reset
= FLASH_CMD_RESET
;
1683 cmdset_intel_read_jedec_ids(info
);
1684 flash_write_cmd(info
, 0, info
->cfi_offset
, FLASH_CMD_CFI
);
1686 #ifdef CONFIG_SYS_FLASH_PROTECTION
1687 /* read legacy lock/unlock bit from intel flash */
1688 if (info
->ext_addr
) {
1689 info
->legacy_unlock
=
1690 flash_read_uchar(info
, info
->ext_addr
+ 5) & 0x08;
1697 static void cmdset_amd_read_jedec_ids(flash_info_t
*info
)
1703 flash_write_cmd(info
, 0, 0, AMD_CMD_RESET
);
1704 flash_unlock_seq(info
, 0);
1705 flash_write_cmd(info
, 0, info
->addr_unlock1
, FLASH_CMD_READ_ID
);
1706 udelay(1000); /* some flash are slow to respond */
1708 manu_id
= flash_read_uchar(info
, FLASH_OFFSET_MANUFACTURER_ID
);
1709 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1710 while (manu_id
== FLASH_CONTINUATION_CODE
&& bank_id
< 0x800) {
1712 manu_id
= flash_read_uchar(info
,
1713 bank_id
| FLASH_OFFSET_MANUFACTURER_ID
);
1715 info
->manufacturer_id
= manu_id
;
1717 debug("info->ext_addr = 0x%x, cfi_version = 0x%x\n",
1718 info
->ext_addr
, info
->cfi_version
);
1719 if (info
->ext_addr
&& info
->cfi_version
>= 0x3134) {
1720 /* read software feature (at 0x53) */
1721 feature
= flash_read_uchar(info
, info
->ext_addr
+ 0x13);
1722 debug("feature = 0x%x\n", feature
);
1723 info
->sr_supported
= feature
& 0x1;
1726 switch (info
->chipwidth
) {
1727 case FLASH_CFI_8BIT
:
1728 info
->device_id
= flash_read_uchar(info
,
1729 FLASH_OFFSET_DEVICE_ID
);
1730 if (info
->device_id
== 0x7E) {
1731 /* AMD 3-byte (expanded) device ids */
1732 info
->device_id2
= flash_read_uchar(info
,
1733 FLASH_OFFSET_DEVICE_ID2
);
1734 info
->device_id2
<<= 8;
1735 info
->device_id2
|= flash_read_uchar(info
,
1736 FLASH_OFFSET_DEVICE_ID3
);
1739 case FLASH_CFI_16BIT
:
1740 info
->device_id
= flash_read_word(info
,
1741 FLASH_OFFSET_DEVICE_ID
);
1742 if ((info
->device_id
& 0xff) == 0x7E) {
1743 /* AMD 3-byte (expanded) device ids */
1744 info
->device_id2
= flash_read_uchar(info
,
1745 FLASH_OFFSET_DEVICE_ID2
);
1746 info
->device_id2
<<= 8;
1747 info
->device_id2
|= flash_read_uchar(info
,
1748 FLASH_OFFSET_DEVICE_ID3
);
1754 flash_write_cmd(info
, 0, 0, AMD_CMD_RESET
);
1758 static int cmdset_amd_init(flash_info_t
*info
, struct cfi_qry
*qry
)
1760 info
->cmd_reset
= AMD_CMD_RESET
;
1761 info
->cmd_erase_sector
= AMD_CMD_ERASE_SECTOR
;
1763 cmdset_amd_read_jedec_ids(info
);
1764 flash_write_cmd(info
, 0, info
->cfi_offset
, FLASH_CMD_CFI
);
1766 #ifdef CONFIG_SYS_FLASH_PROTECTION
1767 if (info
->ext_addr
) {
1768 /* read sector protect/unprotect scheme (at 0x49) */
1769 if (flash_read_uchar(info
, info
->ext_addr
+ 9) == 0x8)
1770 info
->legacy_unlock
= 1;
1777 #ifdef CONFIG_FLASH_CFI_LEGACY
1778 static void flash_read_jedec_ids(flash_info_t
*info
)
1780 info
->manufacturer_id
= 0;
1781 info
->device_id
= 0;
1782 info
->device_id2
= 0;
1784 switch (info
->vendor
) {
1785 case CFI_CMDSET_INTEL_PROG_REGIONS
:
1786 case CFI_CMDSET_INTEL_STANDARD
:
1787 case CFI_CMDSET_INTEL_EXTENDED
:
1788 cmdset_intel_read_jedec_ids(info
);
1790 case CFI_CMDSET_AMD_STANDARD
:
1791 case CFI_CMDSET_AMD_EXTENDED
:
1792 cmdset_amd_read_jedec_ids(info
);
1799 /*-----------------------------------------------------------------------
1800 * Call board code to request info about non-CFI flash.
1801 * board_flash_get_legacy needs to fill in at least:
1802 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1804 static int flash_detect_legacy(phys_addr_t base
, int banknum
)
1806 flash_info_t
*info
= &flash_info
[banknum
];
1808 if (board_flash_get_legacy(base
, banknum
, info
)) {
1809 /* board code may have filled info completely. If not, we
1810 * use JEDEC ID probing.
1812 if (!info
->vendor
) {
1814 CFI_CMDSET_AMD_STANDARD
,
1815 CFI_CMDSET_INTEL_STANDARD
1819 for (i
= 0; i
< ARRAY_SIZE(modes
); i
++) {
1820 info
->vendor
= modes
[i
];
1822 (ulong
)map_physmem(base
,
1825 if (info
->portwidth
== FLASH_CFI_8BIT
&&
1826 info
->interface
== FLASH_CFI_X8X16
) {
1827 info
->addr_unlock1
= 0x2AAA;
1828 info
->addr_unlock2
= 0x5555;
1830 info
->addr_unlock1
= 0x5555;
1831 info
->addr_unlock2
= 0x2AAA;
1833 flash_read_jedec_ids(info
);
1834 debug("JEDEC PROBE: ID %x %x %x\n",
1835 info
->manufacturer_id
,
1838 if (jedec_flash_match(info
, info
->start
[0]))
1841 unmap_physmem((void *)info
->start
[0],
1846 switch (info
->vendor
) {
1847 case CFI_CMDSET_INTEL_PROG_REGIONS
:
1848 case CFI_CMDSET_INTEL_STANDARD
:
1849 case CFI_CMDSET_INTEL_EXTENDED
:
1850 info
->cmd_reset
= FLASH_CMD_RESET
;
1852 case CFI_CMDSET_AMD_STANDARD
:
1853 case CFI_CMDSET_AMD_EXTENDED
:
1854 case CFI_CMDSET_AMD_LEGACY
:
1855 info
->cmd_reset
= AMD_CMD_RESET
;
1858 info
->flash_id
= FLASH_MAN_CFI
;
1861 return 0; /* use CFI */
1864 static inline int flash_detect_legacy(phys_addr_t base
, int banknum
)
1866 return 0; /* use CFI */
1870 /*-----------------------------------------------------------------------
1871 * detect if flash is compatible with the Common Flash Interface (CFI)
1872 * http://www.jedec.org/download/search/jesd68.pdf
1874 static void flash_read_cfi(flash_info_t
*info
, void *buf
, unsigned int start
,
1880 for (i
= 0; i
< len
; i
++)
1881 p
[i
] = flash_read_uchar(info
, start
+ i
);
1884 static void __flash_cmd_reset(flash_info_t
*info
)
1887 * We do not yet know what kind of commandset to use, so we issue
1888 * the reset command in both Intel and AMD variants, in the hope
1889 * that AMD flash roms ignore the Intel command.
1891 flash_write_cmd(info
, 0, 0, AMD_CMD_RESET
);
1893 flash_write_cmd(info
, 0, 0, FLASH_CMD_RESET
);
1896 void flash_cmd_reset(flash_info_t
*info
)
1897 __attribute__((weak
, alias("__flash_cmd_reset")));
1899 static int __flash_detect_cfi(flash_info_t
*info
, struct cfi_qry
*qry
)
1903 /* Issue FLASH reset command */
1904 flash_cmd_reset(info
);
1906 for (cfi_offset
= 0; cfi_offset
< ARRAY_SIZE(flash_offset_cfi
);
1908 flash_write_cmd(info
, 0, flash_offset_cfi
[cfi_offset
],
1910 if (flash_isequal(info
, 0, FLASH_OFFSET_CFI_RESP
, 'Q') &&
1911 flash_isequal(info
, 0, FLASH_OFFSET_CFI_RESP
+ 1, 'R') &&
1912 flash_isequal(info
, 0, FLASH_OFFSET_CFI_RESP
+ 2, 'Y')) {
1913 flash_read_cfi(info
, qry
, FLASH_OFFSET_CFI_RESP
,
1914 sizeof(struct cfi_qry
));
1915 info
->interface
= le16_to_cpu(qry
->interface_desc
);
1917 info
->cfi_offset
= flash_offset_cfi
[cfi_offset
];
1918 debug("device interface is %d\n",
1920 debug("found port %d chip %d ",
1921 info
->portwidth
, info
->chipwidth
);
1922 debug("port %d bits chip %d bits\n",
1923 info
->portwidth
<< CFI_FLASH_SHIFT_WIDTH
,
1924 info
->chipwidth
<< CFI_FLASH_SHIFT_WIDTH
);
1926 /* calculate command offsets as in the Linux driver */
1927 info
->addr_unlock1
= 0x555;
1928 info
->addr_unlock2
= 0x2aa;
1931 * modify the unlock address if we are
1932 * in compatibility mode
1934 if (/* x8/x16 in x8 mode */
1935 (info
->chipwidth
== FLASH_CFI_BY8
&&
1936 info
->interface
== FLASH_CFI_X8X16
) ||
1937 /* x16/x32 in x16 mode */
1938 (info
->chipwidth
== FLASH_CFI_BY16
&&
1939 info
->interface
== FLASH_CFI_X16X32
)) {
1940 info
->addr_unlock1
= 0xaaa;
1941 info
->addr_unlock2
= 0x555;
1944 info
->name
= "CFI conformant";
1952 static int flash_detect_cfi(flash_info_t
*info
, struct cfi_qry
*qry
)
1954 debug("flash detect cfi\n");
1956 for (info
->portwidth
= CONFIG_SYS_FLASH_CFI_WIDTH
;
1957 info
->portwidth
<= FLASH_CFI_64BIT
; info
->portwidth
<<= 1) {
1958 for (info
->chipwidth
= FLASH_CFI_BY8
;
1959 info
->chipwidth
<= info
->portwidth
;
1960 info
->chipwidth
<<= 1)
1961 if (__flash_detect_cfi(info
, qry
))
1964 debug("not found\n");
1969 * Manufacturer-specific quirks. Add workarounds for geometry
1970 * reversal, etc. here.
1972 static void flash_fixup_amd(flash_info_t
*info
, struct cfi_qry
*qry
)
1974 /* check if flash geometry needs reversal */
1975 if (qry
->num_erase_regions
> 1) {
1976 /* reverse geometry if top boot part */
1977 if (info
->cfi_version
< 0x3131) {
1978 /* CFI < 1.1, try to guess from device id */
1979 if ((info
->device_id
& 0x80) != 0)
1980 cfi_reverse_geometry(qry
);
1981 } else if (flash_read_uchar(info
, info
->ext_addr
+ 0xf) == 3) {
1982 /* CFI >= 1.1, deduct from top/bottom flag */
1983 /* note: ext_addr is valid since cfi_version > 0 */
1984 cfi_reverse_geometry(qry
);
1989 static void flash_fixup_atmel(flash_info_t
*info
, struct cfi_qry
*qry
)
1991 int reverse_geometry
= 0;
1993 /* Check the "top boot" bit in the PRI */
1994 if (info
->ext_addr
&& !(flash_read_uchar(info
, info
->ext_addr
+ 6) & 1))
1995 reverse_geometry
= 1;
1997 /* AT49BV6416(T) list the erase regions in the wrong order.
1998 * However, the device ID is identical with the non-broken
1999 * AT49BV642D they differ in the high byte.
2001 if (info
->device_id
== 0xd6 || info
->device_id
== 0xd2)
2002 reverse_geometry
= !reverse_geometry
;
2004 if (reverse_geometry
)
2005 cfi_reverse_geometry(qry
);
2008 static void flash_fixup_stm(flash_info_t
*info
, struct cfi_qry
*qry
)
2010 /* check if flash geometry needs reversal */
2011 if (qry
->num_erase_regions
> 1) {
2012 /* reverse geometry if top boot part */
2013 if (info
->cfi_version
< 0x3131) {
2014 /* CFI < 1.1, guess by device id */
2015 if (info
->device_id
== 0x22CA || /* M29W320DT */
2016 info
->device_id
== 0x2256 || /* M29W320ET */
2017 info
->device_id
== 0x22D7) { /* M29W800DT */
2018 cfi_reverse_geometry(qry
);
2020 } else if (flash_read_uchar(info
, info
->ext_addr
+ 0xf) == 3) {
2021 /* CFI >= 1.1, deduct from top/bottom flag */
2022 /* note: ext_addr is valid since cfi_version > 0 */
2023 cfi_reverse_geometry(qry
);
2028 static void flash_fixup_sst(flash_info_t
*info
, struct cfi_qry
*qry
)
2031 * SST, for many recent nor parallel flashes, says they are
2032 * CFI-conformant. This is not true, since qry struct.
2033 * reports a std. AMD command set (0x0002), while SST allows to
2034 * erase two different sector sizes for the same memory.
2035 * 64KB sector (SST call it block) needs 0x30 to be erased.
2036 * 4KB sector (SST call it sector) needs 0x50 to be erased.
2037 * Since CFI query detect the 4KB number of sectors, users expects
2038 * a sector granularity of 4KB, and it is here set.
2040 if (info
->device_id
== 0x5D23 || /* SST39VF3201B */
2041 info
->device_id
== 0x5C23) { /* SST39VF3202B */
2042 /* set sector granularity to 4KB */
2043 info
->cmd_erase_sector
= 0x50;
2047 static void flash_fixup_num(flash_info_t
*info
, struct cfi_qry
*qry
)
2050 * The M29EW devices seem to report the CFI information wrong
2051 * when it's in 8 bit mode.
2052 * There's an app note from Numonyx on this issue.
2053 * So adjust the buffer size for M29EW while operating in 8-bit mode
2055 if (qry
->max_buf_write_size
> 0x8 &&
2056 info
->device_id
== 0x7E &&
2057 (info
->device_id2
== 0x2201 ||
2058 info
->device_id2
== 0x2301 ||
2059 info
->device_id2
== 0x2801 ||
2060 info
->device_id2
== 0x4801)) {
2061 debug("Adjusted buffer size on Numonyx flash");
2062 debug(" M29EW family in 8 bit mode\n");
2063 qry
->max_buf_write_size
= 0x8;
2068 * The following code cannot be run from FLASH!
2071 ulong
flash_get_size(phys_addr_t base
, int banknum
)
2073 flash_info_t
*info
= &flash_info
[banknum
];
2075 flash_sect_t sect_cnt
;
2079 uchar num_erase_regions
;
2080 int erase_region_size
;
2081 int erase_region_count
;
2083 unsigned long max_size
;
2085 memset(&qry
, 0, sizeof(qry
));
2088 info
->cfi_version
= 0;
2089 #ifdef CONFIG_SYS_FLASH_PROTECTION
2090 info
->legacy_unlock
= 0;
2093 info
->start
[0] = (ulong
)map_physmem(base
, info
->portwidth
, MAP_NOCACHE
);
2095 if (flash_detect_cfi(info
, &qry
)) {
2096 info
->vendor
= le16_to_cpu(get_unaligned(&qry
.p_id
));
2097 info
->ext_addr
= le16_to_cpu(get_unaligned(&qry
.p_adr
));
2098 num_erase_regions
= qry
.num_erase_regions
;
2100 if (info
->ext_addr
) {
2101 info
->cfi_version
= (ushort
)flash_read_uchar(info
,
2102 info
->ext_addr
+ 3) << 8;
2103 info
->cfi_version
|= (ushort
)flash_read_uchar(info
,
2104 info
->ext_addr
+ 4);
2108 flash_printqry(&qry
);
2111 switch (info
->vendor
) {
2112 case CFI_CMDSET_INTEL_PROG_REGIONS
:
2113 case CFI_CMDSET_INTEL_STANDARD
:
2114 case CFI_CMDSET_INTEL_EXTENDED
:
2115 cmdset_intel_init(info
, &qry
);
2117 case CFI_CMDSET_AMD_STANDARD
:
2118 case CFI_CMDSET_AMD_EXTENDED
:
2119 cmdset_amd_init(info
, &qry
);
2122 printf("CFI: Unknown command set 0x%x\n",
2125 * Unfortunately, this means we don't know how
2126 * to get the chip back to Read mode. Might
2127 * as well try an Intel-style reset...
2129 flash_write_cmd(info
, 0, 0, FLASH_CMD_RESET
);
2133 /* Do manufacturer-specific fixups */
2134 switch (info
->manufacturer_id
) {
2135 case 0x0001: /* AMD */
2136 case 0x0037: /* AMIC */
2137 flash_fixup_amd(info
, &qry
);
2140 flash_fixup_atmel(info
, &qry
);
2143 flash_fixup_stm(info
, &qry
);
2145 case 0x00bf: /* SST */
2146 flash_fixup_sst(info
, &qry
);
2148 case 0x0089: /* Numonyx */
2149 flash_fixup_num(info
, &qry
);
2153 debug("manufacturer is %d\n", info
->vendor
);
2154 debug("manufacturer id is 0x%x\n", info
->manufacturer_id
);
2155 debug("device id is 0x%x\n", info
->device_id
);
2156 debug("device id2 is 0x%x\n", info
->device_id2
);
2157 debug("cfi version is 0x%04x\n", info
->cfi_version
);
2159 size_ratio
= info
->portwidth
/ info
->chipwidth
;
2160 /* if the chip is x8/x16 reduce the ratio by half */
2161 if (info
->interface
== FLASH_CFI_X8X16
&&
2162 info
->chipwidth
== FLASH_CFI_BY8
) {
2165 debug("size_ratio %d port %d bits chip %d bits\n",
2166 size_ratio
, info
->portwidth
<< CFI_FLASH_SHIFT_WIDTH
,
2167 info
->chipwidth
<< CFI_FLASH_SHIFT_WIDTH
);
2168 info
->size
= 1 << qry
.dev_size
;
2169 /* multiply the size by the number of chips */
2170 info
->size
*= size_ratio
;
2171 max_size
= cfi_flash_bank_size(banknum
);
2172 if (max_size
&& info
->size
> max_size
) {
2173 debug("[truncated from %ldMiB]", info
->size
>> 20);
2174 info
->size
= max_size
;
2176 debug("found %d erase regions\n", num_erase_regions
);
2179 for (i
= 0; i
< num_erase_regions
; i
++) {
2180 if (i
> NUM_ERASE_REGIONS
) {
2181 printf("%d erase regions found, only %d used\n",
2182 num_erase_regions
, NUM_ERASE_REGIONS
);
2186 tmp
= le32_to_cpu(get_unaligned(
2187 &qry
.erase_region_info
[i
]));
2188 debug("erase region %u: 0x%08lx\n", i
, tmp
);
2190 erase_region_count
= (tmp
& 0xffff) + 1;
2193 (tmp
& 0xffff) ? ((tmp
& 0xffff) * 256) : 128;
2194 debug("erase_region_count = %d ", erase_region_count
);
2195 debug("erase_region_size = %d\n", erase_region_size
);
2196 for (j
= 0; j
< erase_region_count
; j
++) {
2197 if (sector
- base
>= info
->size
)
2199 if (sect_cnt
>= CONFIG_SYS_MAX_FLASH_SECT
) {
2200 printf("ERROR: too many flash sectors\n");
2203 info
->start
[sect_cnt
] =
2204 (ulong
)map_physmem(sector
,
2207 sector
+= (erase_region_size
* size_ratio
);
2210 * Only read protection status from
2211 * supported devices (intel...)
2213 switch (info
->vendor
) {
2214 case CFI_CMDSET_INTEL_PROG_REGIONS
:
2215 case CFI_CMDSET_INTEL_EXTENDED
:
2216 case CFI_CMDSET_INTEL_STANDARD
:
2218 * Set flash to read-id mode. Otherwise
2219 * reading protected status is not
2222 flash_write_cmd(info
, sect_cnt
, 0,
2224 info
->protect
[sect_cnt
] =
2225 flash_isset(info
, sect_cnt
,
2226 FLASH_OFFSET_PROTECT
,
2227 FLASH_STATUS_PROTECT
);
2228 flash_write_cmd(info
, sect_cnt
, 0,
2231 case CFI_CMDSET_AMD_EXTENDED
:
2232 case CFI_CMDSET_AMD_STANDARD
:
2233 if (!info
->legacy_unlock
) {
2234 /* default: not protected */
2235 info
->protect
[sect_cnt
] = 0;
2239 /* Read protection (PPB) from sector */
2240 flash_write_cmd(info
, 0, 0,
2242 flash_unlock_seq(info
, 0);
2243 flash_write_cmd(info
, 0,
2246 info
->protect
[sect_cnt
] =
2249 FLASH_OFFSET_PROTECT
,
2250 FLASH_STATUS_PROTECT
);
2253 /* default: not protected */
2254 info
->protect
[sect_cnt
] = 0;
2261 info
->sector_count
= sect_cnt
;
2262 info
->buffer_size
= 1 << le16_to_cpu(qry
.max_buf_write_size
);
2263 tmp
= 1 << qry
.block_erase_timeout_typ
;
2264 info
->erase_blk_tout
= tmp
*
2265 (1 << qry
.block_erase_timeout_max
);
2266 tmp
= (1 << qry
.buf_write_timeout_typ
) *
2267 (1 << qry
.buf_write_timeout_max
);
2269 /* round up when converting to ms */
2270 info
->buffer_write_tout
= (tmp
+ 999) / 1000;
2271 tmp
= (1 << qry
.word_write_timeout_typ
) *
2272 (1 << qry
.word_write_timeout_max
);
2273 /* round up when converting to ms */
2274 info
->write_tout
= (tmp
+ 999) / 1000;
2275 info
->flash_id
= FLASH_MAN_CFI
;
2276 if (info
->interface
== FLASH_CFI_X8X16
&&
2277 info
->chipwidth
== FLASH_CFI_BY8
) {
2278 /* XXX - Need to test on x8/x16 in parallel. */
2279 info
->portwidth
>>= 1;
2282 flash_write_cmd(info
, 0, 0, info
->cmd_reset
);
2285 return (info
->size
);
2288 #ifdef CONFIG_FLASH_CFI_MTD
2289 void flash_set_verbose(uint v
)
2295 static void cfi_flash_set_config_reg(u32 base
, u16 val
)
2297 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2299 * Only set this config register if really defined
2300 * to a valid value (0xffff is invalid)
2306 * Set configuration register. Data is "encrypted" in the 16 lower
2309 flash_write16(FLASH_CMD_SETUP
, (void *)(base
+ (val
<< 1)));
2310 flash_write16(FLASH_CMD_SET_CR_CONFIRM
, (void *)(base
+ (val
<< 1)));
2313 * Finally issue reset-command to bring device back to
2316 flash_write16(FLASH_CMD_RESET
, (void *)base
);
2320 /*-----------------------------------------------------------------------
2323 static void flash_protect_default(void)
2325 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2330 } apl
[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST
;
2333 /* Monitor protection ON by default */
2334 #if defined(CONFIG_SYS_MONITOR_BASE) && \
2335 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2336 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2337 flash_protect(FLAG_PROTECT_SET
,
2338 CONFIG_SYS_MONITOR_BASE
,
2339 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1,
2340 flash_get_info(CONFIG_SYS_MONITOR_BASE
));
2343 /* Environment protection ON by default */
2344 #ifdef CONFIG_ENV_IS_IN_FLASH
2345 flash_protect(FLAG_PROTECT_SET
,
2347 CONFIG_ENV_ADDR
+ CONFIG_ENV_SECT_SIZE
- 1,
2348 flash_get_info(CONFIG_ENV_ADDR
));
2351 /* Redundant environment protection ON by default */
2352 #ifdef CONFIG_ENV_ADDR_REDUND
2353 flash_protect(FLAG_PROTECT_SET
,
2354 CONFIG_ENV_ADDR_REDUND
,
2355 CONFIG_ENV_ADDR_REDUND
+ CONFIG_ENV_SECT_SIZE
- 1,
2356 flash_get_info(CONFIG_ENV_ADDR_REDUND
));
2359 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2360 for (i
= 0; i
< ARRAY_SIZE(apl
); i
++) {
2361 debug("autoprotecting from %08lx to %08lx\n",
2362 apl
[i
].start
, apl
[i
].start
+ apl
[i
].size
- 1);
2363 flash_protect(FLAG_PROTECT_SET
,
2365 apl
[i
].start
+ apl
[i
].size
- 1,
2366 flash_get_info(apl
[i
].start
));
2371 unsigned long flash_init(void)
2373 unsigned long size
= 0;
2376 #ifdef CONFIG_SYS_FLASH_PROTECTION
2377 /* read environment from EEPROM */
2380 env_get_f("unlock", s
, sizeof(s
));
2383 #ifdef CONFIG_CFI_FLASH /* for driver model */
2384 cfi_flash_init_dm();
2387 /* Init: no FLASHes known */
2388 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
2389 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
2391 /* Optionally write flash configuration register */
2392 cfi_flash_set_config_reg(cfi_flash_bank_addr(i
),
2393 cfi_flash_config_reg(i
));
2395 if (!flash_detect_legacy(cfi_flash_bank_addr(i
), i
))
2396 flash_get_size(cfi_flash_bank_addr(i
), i
);
2397 size
+= flash_info
[i
].size
;
2398 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
) {
2399 #ifndef CONFIG_SYS_FLASH_QUIET_TEST
2400 printf("## Unknown flash on Bank %d ", i
+ 1);
2401 printf("- Size = 0x%08lx = %ld MB\n",
2403 flash_info
[i
].size
>> 20);
2404 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
2406 #ifdef CONFIG_SYS_FLASH_PROTECTION
2407 else if (strcmp(s
, "yes") == 0) {
2409 * Only the U-Boot image and it's environment
2410 * is protected, all other sectors are
2411 * unprotected (unlocked) if flash hardware
2412 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
2413 * and the environment variable "unlock" is
2416 if (flash_info
[i
].legacy_unlock
) {
2420 * Disable legacy_unlock temporarily,
2421 * since flash_real_protect would
2422 * relock all other sectors again
2425 flash_info
[i
].legacy_unlock
= 0;
2428 * Legacy unlocking (e.g. Intel J3) ->
2429 * unlock only one sector. This will
2430 * unlock all sectors.
2432 flash_real_protect(&flash_info
[i
], 0, 0);
2434 flash_info
[i
].legacy_unlock
= 1;
2437 * Manually mark other sectors as
2438 * unlocked (unprotected)
2440 for (k
= 1; k
< flash_info
[i
].sector_count
; k
++)
2441 flash_info
[i
].protect
[k
] = 0;
2444 * No legancy unlocking -> unlock all sectors
2446 flash_protect(FLAG_PROTECT_CLEAR
,
2447 flash_info
[i
].start
[0],
2448 flash_info
[i
].start
[0]
2449 + flash_info
[i
].size
- 1,
2453 #endif /* CONFIG_SYS_FLASH_PROTECTION */
2456 flash_protect_default();
2457 #ifdef CONFIG_FLASH_CFI_MTD
2464 #ifdef CONFIG_CFI_FLASH /* for driver model */
2465 static int cfi_flash_probe(struct udevice
*dev
)
2467 const fdt32_t
*cell
;
2471 addrc
= dev_read_addr_cells(dev
);
2472 sizec
= dev_read_size_cells(dev
);
2474 /* decode regs; there may be multiple reg tuples. */
2475 cell
= dev_read_prop(dev
, "reg", &len
);
2479 len
/= sizeof(fdt32_t
);
2483 addr
= dev_translate_address(dev
, cell
+ idx
);
2485 flash_info
[cfi_flash_num_flash_banks
].dev
= dev
;
2486 flash_info
[cfi_flash_num_flash_banks
].base
= addr
;
2487 cfi_flash_num_flash_banks
++;
2489 idx
+= addrc
+ sizec
;
2491 gd
->bd
->bi_flashstart
= flash_info
[0].base
;
2496 static const struct udevice_id cfi_flash_ids
[] = {
2497 { .compatible
= "cfi-flash" },
2498 { .compatible
= "jedec-flash" },
2502 U_BOOT_DRIVER(cfi_flash
) = {
2503 .name
= "cfi_flash",
2505 .of_match
= cfi_flash_ids
,
2506 .probe
= cfi_flash_probe
,
2508 #endif /* CONFIG_CFI_FLASH */