2 * (C) Copyright 2002-2004
3 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
5 * Copyright (C) 2003 Arabella Software Ltd.
6 * Yuli Barcohen <yuli@arabellasw.com>
12 * Tolunay Orkun <listmember@orkun.us>
14 * See file CREDITS for list of people who contributed to this
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 /* The DEBUG define must be before common to enable debugging */
38 #include <asm/processor.h>
40 #include <asm/byteorder.h>
41 #include <environment.h>
42 #include <mtd/cfi_flash.h>
45 * This file implements a Common Flash Interface (CFI) driver for
48 * The width of the port and the width of the chips are determined at
49 * initialization. These widths are used to calculate the address for
50 * access CFI data structures.
53 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
54 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
55 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
56 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
57 * AMD CFI Specification, Release 2.0 December 1, 2001
58 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
59 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
61 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
62 * reading and writing ... (yes there is such a Hardware).
65 static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
66 #ifdef CONFIG_FLASH_CFI_MTD
67 static uint flash_verbose = 1;
69 #define flash_verbose 1
72 flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
75 * Check if chip width is defined. If not, start detecting with 8bit.
77 #ifndef CONFIG_SYS_FLASH_CFI_WIDTH
78 #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
82 * 0xffff is an undefined value for the configuration register. When
83 * this value is returned, the configuration register shall not be
84 * written at all (default mode).
86 static u16 cfi_flash_config_reg(int i)
88 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
89 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
95 #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
96 int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
99 static phys_addr_t __cfi_flash_bank_addr(int i)
101 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
103 phys_addr_t cfi_flash_bank_addr(int i)
104 __attribute__((weak, alias("__cfi_flash_bank_addr")));
106 static unsigned long __cfi_flash_bank_size(int i)
108 #ifdef CONFIG_SYS_FLASH_BANKS_SIZES
109 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
114 unsigned long cfi_flash_bank_size(int i)
115 __attribute__((weak, alias("__cfi_flash_bank_size")));
117 static void __flash_write8(u8 value, void *addr)
119 __raw_writeb(value, addr);
122 static void __flash_write16(u16 value, void *addr)
124 __raw_writew(value, addr);
127 static void __flash_write32(u32 value, void *addr)
129 __raw_writel(value, addr);
132 static void __flash_write64(u64 value, void *addr)
134 /* No architectures currently implement __raw_writeq() */
135 *(volatile u64 *)addr = value;
138 static u8 __flash_read8(void *addr)
140 return __raw_readb(addr);
143 static u16 __flash_read16(void *addr)
145 return __raw_readw(addr);
148 static u32 __flash_read32(void *addr)
150 return __raw_readl(addr);
153 static u64 __flash_read64(void *addr)
155 /* No architectures currently implement __raw_readq() */
156 return *(volatile u64 *)addr;
159 #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
160 void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
161 void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
162 void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
163 void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
164 u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
165 u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
166 u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
167 u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
169 #define flash_write8 __flash_write8
170 #define flash_write16 __flash_write16
171 #define flash_write32 __flash_write32
172 #define flash_write64 __flash_write64
173 #define flash_read8 __flash_read8
174 #define flash_read16 __flash_read16
175 #define flash_read32 __flash_read32
176 #define flash_read64 __flash_read64
179 /*-----------------------------------------------------------------------
181 #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
182 flash_info_t *flash_get_info(ulong base)
185 flash_info_t *info = NULL;
187 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
188 info = & flash_info[i];
189 if (info->size && info->start[0] <= base &&
190 base <= info->start[0] + info->size - 1)
198 unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
200 if (sect != (info->sector_count - 1))
201 return info->start[sect + 1] - info->start[sect];
203 return info->start[0] + info->size - info->start[sect];
206 /*-----------------------------------------------------------------------
207 * create an address based on the offset and the port width
210 flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
212 unsigned int byte_offset = offset * info->portwidth;
214 return (void *)(info->start[sect] + byte_offset);
217 static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
218 unsigned int offset, void *addr)
222 /*-----------------------------------------------------------------------
223 * make a proper sized command based on the port and chip widths
225 static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
230 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
231 u32 cmd_le = cpu_to_le32(cmd);
234 uchar *cp = (uchar *) cmdbuf;
236 for (i = info->portwidth; i > 0; i--){
237 cword_offset = (info->portwidth-i)%info->chipwidth;
238 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
239 cp_offset = info->portwidth - i;
240 val = *((uchar*)&cmd_le + cword_offset);
243 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
245 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
250 /*-----------------------------------------------------------------------
253 static void print_longlong (char *str, unsigned long long data)
259 for (i = 0; i < 8; i++)
260 sprintf (&str[i * 2], "%2.2x", *cp++);
263 static void flash_printqry (struct cfi_qry *qry)
268 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
270 for (y = 0; y < 16; y++)
271 debug("%2.2x ", p[x + y]);
273 for (y = 0; y < 16; y++) {
274 unsigned char c = p[x + y];
275 if (c >= 0x20 && c <= 0x7e)
286 /*-----------------------------------------------------------------------
287 * read a character at a port width address
289 static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
294 cp = flash_map (info, 0, offset);
295 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
296 retval = flash_read8(cp);
298 retval = flash_read8(cp + info->portwidth - 1);
300 flash_unmap (info, 0, offset, cp);
304 /*-----------------------------------------------------------------------
305 * read a word at a port width address, assume 16bit bus
307 static inline ushort flash_read_word (flash_info_t * info, uint offset)
309 ushort *addr, retval;
311 addr = flash_map (info, 0, offset);
312 retval = flash_read16 (addr);
313 flash_unmap (info, 0, offset, addr);
318 /*-----------------------------------------------------------------------
319 * read a long word by picking the least significant byte of each maximum
320 * port size word. Swap for ppc format.
322 static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
331 addr = flash_map (info, sect, offset);
334 debug ("long addr is at %p info->portwidth = %d\n", addr,
336 for (x = 0; x < 4 * info->portwidth; x++) {
337 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 void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
360 uint offset, u32 cmd)
366 addr = flash_map (info, sect, offset);
367 flash_make_cmd (info, cmd, &cword);
368 switch (info->portwidth) {
370 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
371 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
372 flash_write8(cword.c, addr);
374 case FLASH_CFI_16BIT:
375 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
377 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
378 flash_write16(cword.w, addr);
380 case FLASH_CFI_32BIT:
381 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
383 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
384 flash_write32(cword.l, addr);
386 case FLASH_CFI_64BIT:
391 print_longlong (str, cword.ll);
393 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
395 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
398 flash_write64(cword.ll, addr);
402 /* Ensure all the instructions are fully finished */
405 flash_unmap(info, sect, offset, addr);
408 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
410 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
411 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
414 /*-----------------------------------------------------------------------
416 static int flash_isequal (flash_info_t * info, flash_sect_t sect,
417 uint offset, uchar cmd)
423 addr = flash_map (info, sect, offset);
424 flash_make_cmd (info, cmd, &cword);
426 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
427 switch (info->portwidth) {
429 debug ("is= %x %x\n", flash_read8(addr), cword.c);
430 retval = (flash_read8(addr) == cword.c);
432 case FLASH_CFI_16BIT:
433 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
434 retval = (flash_read16(addr) == cword.w);
436 case FLASH_CFI_32BIT:
437 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
438 retval = (flash_read32(addr) == cword.l);
440 case FLASH_CFI_64BIT:
446 print_longlong (str1, flash_read64(addr));
447 print_longlong (str2, cword.ll);
448 debug ("is= %s %s\n", str1, str2);
451 retval = (flash_read64(addr) == cword.ll);
457 flash_unmap(info, sect, offset, addr);
462 /*-----------------------------------------------------------------------
464 static int flash_isset (flash_info_t * info, flash_sect_t sect,
465 uint offset, uchar cmd)
471 addr = flash_map (info, sect, offset);
472 flash_make_cmd (info, cmd, &cword);
473 switch (info->portwidth) {
475 retval = ((flash_read8(addr) & cword.c) == cword.c);
477 case FLASH_CFI_16BIT:
478 retval = ((flash_read16(addr) & cword.w) == cword.w);
480 case FLASH_CFI_32BIT:
481 retval = ((flash_read32(addr) & cword.l) == cword.l);
483 case FLASH_CFI_64BIT:
484 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
490 flash_unmap(info, sect, offset, addr);
495 /*-----------------------------------------------------------------------
497 static int flash_toggle (flash_info_t * info, flash_sect_t sect,
498 uint offset, uchar cmd)
504 addr = flash_map (info, sect, offset);
505 flash_make_cmd (info, cmd, &cword);
506 switch (info->portwidth) {
508 retval = flash_read8(addr) != flash_read8(addr);
510 case FLASH_CFI_16BIT:
511 retval = flash_read16(addr) != flash_read16(addr);
513 case FLASH_CFI_32BIT:
514 retval = flash_read32(addr) != flash_read32(addr);
516 case FLASH_CFI_64BIT:
517 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
518 (flash_read32(addr+4) != flash_read32(addr+4)) );
524 flash_unmap(info, sect, offset, addr);
530 * flash_is_busy - check to see if the flash is busy
532 * This routine checks the status of the chip and returns true if the
535 static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
539 switch (info->vendor) {
540 case CFI_CMDSET_INTEL_PROG_REGIONS:
541 case CFI_CMDSET_INTEL_STANDARD:
542 case CFI_CMDSET_INTEL_EXTENDED:
543 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
545 case CFI_CMDSET_AMD_STANDARD:
546 case CFI_CMDSET_AMD_EXTENDED:
547 #ifdef CONFIG_FLASH_CFI_LEGACY
548 case CFI_CMDSET_AMD_LEGACY:
550 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
555 debug ("flash_is_busy: %d\n", retval);
559 /*-----------------------------------------------------------------------
560 * wait for XSR.7 to be set. Time out with an error if it does not.
561 * This routine does not set the flash to read-array mode.
563 static int flash_status_check (flash_info_t * info, flash_sect_t sector,
564 ulong tout, char *prompt)
568 #if CONFIG_SYS_HZ != 1000
569 if ((ulong)CONFIG_SYS_HZ > 100000)
570 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
572 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
575 /* Wait for command completion */
576 #ifdef CONFIG_SYS_LOW_RES_TIMER
579 start = get_timer (0);
580 while (flash_is_busy (info, sector)) {
581 if (get_timer (start) > tout) {
582 printf ("Flash %s timeout at address %lx data %lx\n",
583 prompt, info->start[sector],
584 flash_read_long (info, sector, 0));
585 flash_write_cmd (info, sector, 0, info->cmd_reset);
589 udelay (1); /* also triggers watchdog */
594 /*-----------------------------------------------------------------------
595 * Wait for XSR.7 to be set, if it times out print an error, otherwise
596 * do a full status check.
598 * This routine sets the flash to read-array mode.
600 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
601 ulong tout, char *prompt)
605 retcode = flash_status_check (info, sector, tout, prompt);
606 switch (info->vendor) {
607 case CFI_CMDSET_INTEL_PROG_REGIONS:
608 case CFI_CMDSET_INTEL_EXTENDED:
609 case CFI_CMDSET_INTEL_STANDARD:
610 if ((retcode != ERR_OK)
611 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
613 printf ("Flash %s error at address %lx\n", prompt,
614 info->start[sector]);
615 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
616 FLASH_STATUS_PSLBS)) {
617 puts ("Command Sequence Error.\n");
618 } else if (flash_isset (info, sector, 0,
619 FLASH_STATUS_ECLBS)) {
620 puts ("Block Erase Error.\n");
621 retcode = ERR_NOT_ERASED;
622 } else if (flash_isset (info, sector, 0,
623 FLASH_STATUS_PSLBS)) {
624 puts ("Locking Error\n");
626 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
627 puts ("Block locked.\n");
628 retcode = ERR_PROTECTED;
630 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
631 puts ("Vpp Low Error.\n");
633 flash_write_cmd (info, sector, 0, info->cmd_reset);
642 static int use_flash_status_poll(flash_info_t *info)
644 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
645 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
646 info->vendor == CFI_CMDSET_AMD_STANDARD)
652 static int flash_status_poll(flash_info_t *info, void *src, void *dst,
653 ulong tout, char *prompt)
655 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
659 #if CONFIG_SYS_HZ != 1000
660 if ((ulong)CONFIG_SYS_HZ > 100000)
661 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
663 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
666 /* Wait for command completion */
667 #ifdef CONFIG_SYS_LOW_RES_TIMER
670 start = get_timer(0);
672 switch (info->portwidth) {
674 ready = flash_read8(dst) == flash_read8(src);
676 case FLASH_CFI_16BIT:
677 ready = flash_read16(dst) == flash_read16(src);
679 case FLASH_CFI_32BIT:
680 ready = flash_read32(dst) == flash_read32(src);
682 case FLASH_CFI_64BIT:
683 ready = flash_read64(dst) == flash_read64(src);
691 if (get_timer(start) > tout) {
692 printf("Flash %s timeout at address %lx data %lx\n",
693 prompt, (ulong)dst, (ulong)flash_read8(dst));
696 udelay(1); /* also triggers watchdog */
698 #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
702 /*-----------------------------------------------------------------------
704 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
706 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
709 unsigned long long ll;
712 switch (info->portwidth) {
716 case FLASH_CFI_16BIT:
717 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
720 cword->w = (cword->w >> 8) | w;
722 cword->w = (cword->w << 8) | c;
725 case FLASH_CFI_32BIT:
726 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
729 cword->l = (cword->l >> 8) | l;
731 cword->l = (cword->l << 8) | c;
734 case FLASH_CFI_64BIT:
735 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
738 cword->ll = (cword->ll >> 8) | ll;
740 cword->ll = (cword->ll << 8) | c;
747 * Loop through the sector table starting from the previously found sector.
748 * Searches forwards or backwards, dependent on the passed address.
750 static flash_sect_t find_sector (flash_info_t * info, ulong addr)
752 static flash_sect_t saved_sector = 0; /* previously found sector */
753 static flash_info_t *saved_info = 0; /* previously used flash bank */
754 flash_sect_t sector = saved_sector;
756 if ((info != saved_info) || (sector >= info->sector_count))
759 while ((info->start[sector] < addr)
760 && (sector < info->sector_count - 1))
762 while ((info->start[sector] > addr) && (sector > 0))
764 * also decrements the sector in case of an overshot
769 saved_sector = sector;
774 /*-----------------------------------------------------------------------
776 static int flash_write_cfiword (flash_info_t * info, ulong dest,
779 void *dstaddr = (void *)dest;
781 flash_sect_t sect = 0;
784 /* Check if Flash is (sufficiently) erased */
785 switch (info->portwidth) {
787 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
789 case FLASH_CFI_16BIT:
790 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
792 case FLASH_CFI_32BIT:
793 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
795 case FLASH_CFI_64BIT:
796 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
803 return ERR_NOT_ERASED;
805 /* Disable interrupts which might cause a timeout here */
806 flag = disable_interrupts ();
808 switch (info->vendor) {
809 case CFI_CMDSET_INTEL_PROG_REGIONS:
810 case CFI_CMDSET_INTEL_EXTENDED:
811 case CFI_CMDSET_INTEL_STANDARD:
812 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
813 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
815 case CFI_CMDSET_AMD_EXTENDED:
816 case CFI_CMDSET_AMD_STANDARD:
817 sect = find_sector(info, dest);
818 flash_unlock_seq (info, sect);
819 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
822 #ifdef CONFIG_FLASH_CFI_LEGACY
823 case CFI_CMDSET_AMD_LEGACY:
824 sect = find_sector(info, dest);
825 flash_unlock_seq (info, 0);
826 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
832 switch (info->portwidth) {
834 flash_write8(cword.c, dstaddr);
836 case FLASH_CFI_16BIT:
837 flash_write16(cword.w, dstaddr);
839 case FLASH_CFI_32BIT:
840 flash_write32(cword.l, dstaddr);
842 case FLASH_CFI_64BIT:
843 flash_write64(cword.ll, dstaddr);
847 /* re-enable interrupts if necessary */
849 enable_interrupts ();
852 sect = find_sector (info, dest);
854 if (use_flash_status_poll(info))
855 return flash_status_poll(info, &cword, dstaddr,
856 info->write_tout, "write");
858 return flash_full_status_check(info, sect,
859 info->write_tout, "write");
862 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
864 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
871 void *dst = (void *)dest;
878 switch (info->portwidth) {
882 case FLASH_CFI_16BIT:
885 case FLASH_CFI_32BIT:
888 case FLASH_CFI_64BIT:
898 while ((cnt-- > 0) && (flag == 0)) {
899 switch (info->portwidth) {
901 flag = ((flash_read8(dst2) & flash_read8(src)) ==
905 case FLASH_CFI_16BIT:
906 flag = ((flash_read16(dst2) & flash_read16(src)) ==
910 case FLASH_CFI_32BIT:
911 flag = ((flash_read32(dst2) & flash_read32(src)) ==
915 case FLASH_CFI_64BIT:
916 flag = ((flash_read64(dst2) & flash_read64(src)) ==
923 retcode = ERR_NOT_ERASED;
928 sector = find_sector (info, dest);
930 switch (info->vendor) {
931 case CFI_CMDSET_INTEL_PROG_REGIONS:
932 case CFI_CMDSET_INTEL_STANDARD:
933 case CFI_CMDSET_INTEL_EXTENDED:
934 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
935 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
936 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
937 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
938 flash_write_cmd (info, sector, 0, write_cmd);
939 retcode = flash_status_check (info, sector,
940 info->buffer_write_tout,
942 if (retcode == ERR_OK) {
943 /* reduce the number of loops by the width of
946 flash_write_cmd (info, sector, 0, cnt - 1);
948 switch (info->portwidth) {
950 flash_write8(flash_read8(src), dst);
953 case FLASH_CFI_16BIT:
954 flash_write16(flash_read16(src), dst);
957 case FLASH_CFI_32BIT:
958 flash_write32(flash_read32(src), dst);
961 case FLASH_CFI_64BIT:
962 flash_write64(flash_read64(src), dst);
970 flash_write_cmd (info, sector, 0,
971 FLASH_CMD_WRITE_BUFFER_CONFIRM);
972 retcode = flash_full_status_check (
973 info, sector, info->buffer_write_tout,
979 case CFI_CMDSET_AMD_STANDARD:
980 case CFI_CMDSET_AMD_EXTENDED:
981 flash_unlock_seq(info,0);
983 #ifdef CONFIG_FLASH_SPANSION_S29WS_N
984 offset = ((unsigned long)dst - info->start[sector]) >> shift;
986 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
988 flash_write_cmd(info, sector, offset, cnt - 1);
990 switch (info->portwidth) {
993 flash_write8(flash_read8(src), dst);
997 case FLASH_CFI_16BIT:
999 flash_write16(flash_read16(src), dst);
1003 case FLASH_CFI_32BIT:
1005 flash_write32(flash_read32(src), dst);
1009 case FLASH_CFI_64BIT:
1011 flash_write64(flash_read64(src), dst);
1016 retcode = ERR_INVAL;
1020 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1021 if (use_flash_status_poll(info))
1022 retcode = flash_status_poll(info, src - (1 << shift),
1024 info->buffer_write_tout,
1027 retcode = flash_full_status_check(info, sector,
1028 info->buffer_write_tout,
1033 debug ("Unknown Command Set\n");
1034 retcode = ERR_INVAL;
1041 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1044 /*-----------------------------------------------------------------------
1046 int flash_erase (flash_info_t * info, int s_first, int s_last)
1053 if (info->flash_id != FLASH_MAN_CFI) {
1054 puts ("Can't erase unknown flash type - aborted\n");
1057 if ((s_first < 0) || (s_first > s_last)) {
1058 puts ("- no sectors to erase\n");
1063 for (sect = s_first; sect <= s_last; ++sect) {
1064 if (info->protect[sect]) {
1069 printf ("- Warning: %d protected sectors will not be erased!\n",
1071 } else if (flash_verbose) {
1076 for (sect = s_first; sect <= s_last; sect++) {
1077 if (info->protect[sect] == 0) { /* not protected */
1078 switch (info->vendor) {
1079 case CFI_CMDSET_INTEL_PROG_REGIONS:
1080 case CFI_CMDSET_INTEL_STANDARD:
1081 case CFI_CMDSET_INTEL_EXTENDED:
1082 flash_write_cmd (info, sect, 0,
1083 FLASH_CMD_CLEAR_STATUS);
1084 flash_write_cmd (info, sect, 0,
1085 FLASH_CMD_BLOCK_ERASE);
1086 flash_write_cmd (info, sect, 0,
1087 FLASH_CMD_ERASE_CONFIRM);
1089 case CFI_CMDSET_AMD_STANDARD:
1090 case CFI_CMDSET_AMD_EXTENDED:
1091 flash_unlock_seq (info, sect);
1092 flash_write_cmd (info, sect,
1094 AMD_CMD_ERASE_START);
1095 flash_unlock_seq (info, sect);
1096 flash_write_cmd (info, sect, 0,
1097 AMD_CMD_ERASE_SECTOR);
1099 #ifdef CONFIG_FLASH_CFI_LEGACY
1100 case CFI_CMDSET_AMD_LEGACY:
1101 flash_unlock_seq (info, 0);
1102 flash_write_cmd (info, 0, info->addr_unlock1,
1103 AMD_CMD_ERASE_START);
1104 flash_unlock_seq (info, 0);
1105 flash_write_cmd (info, sect, 0,
1106 AMD_CMD_ERASE_SECTOR);
1110 debug ("Unkown flash vendor %d\n",
1115 if (use_flash_status_poll(info)) {
1116 cfiword_t cword = (cfiword_t)0xffffffffffffffffULL;
1118 dest = flash_map(info, sect, 0);
1119 st = flash_status_poll(info, &cword, dest,
1120 info->erase_blk_tout, "erase");
1121 flash_unmap(info, sect, 0, dest);
1123 st = flash_full_status_check(info, sect,
1124 info->erase_blk_tout,
1128 else if (flash_verbose)
1139 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1140 static int sector_erased(flash_info_t *info, int i)
1147 * Check if whole sector is erased
1149 size = flash_sector_size(info, i);
1150 flash = (u32 *)info->start[i];
1151 /* divide by 4 for longword access */
1154 for (k = 0; k < size; k++) {
1155 if (flash_read32(flash++) != 0xffffffff)
1156 return 0; /* not erased */
1159 return 1; /* erased */
1161 #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1163 void flash_print_info (flash_info_t * info)
1167 if (info->flash_id != FLASH_MAN_CFI) {
1168 puts ("missing or unknown FLASH type\n");
1172 printf ("%s flash (%d x %d)",
1174 (info->portwidth << 3), (info->chipwidth << 3));
1175 if (info->size < 1024*1024)
1176 printf (" Size: %ld kB in %d Sectors\n",
1177 info->size >> 10, info->sector_count);
1179 printf (" Size: %ld MB in %d Sectors\n",
1180 info->size >> 20, info->sector_count);
1182 switch (info->vendor) {
1183 case CFI_CMDSET_INTEL_PROG_REGIONS:
1184 printf ("Intel Prog Regions");
1186 case CFI_CMDSET_INTEL_STANDARD:
1187 printf ("Intel Standard");
1189 case CFI_CMDSET_INTEL_EXTENDED:
1190 printf ("Intel Extended");
1192 case CFI_CMDSET_AMD_STANDARD:
1193 printf ("AMD Standard");
1195 case CFI_CMDSET_AMD_EXTENDED:
1196 printf ("AMD Extended");
1198 #ifdef CONFIG_FLASH_CFI_LEGACY
1199 case CFI_CMDSET_AMD_LEGACY:
1200 printf ("AMD Legacy");
1204 printf ("Unknown (%d)", info->vendor);
1207 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1208 info->manufacturer_id);
1209 printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1211 if ((info->device_id & 0xff) == 0x7E) {
1212 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1215 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1216 info->erase_blk_tout,
1218 if (info->buffer_size > 1) {
1219 printf (" Buffer write timeout: %ld ms, "
1220 "buffer size: %d bytes\n",
1221 info->buffer_write_tout,
1225 puts ("\n Sector Start Addresses:");
1226 for (i = 0; i < info->sector_count; ++i) {
1231 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1232 /* print empty and read-only info */
1233 printf (" %08lX %c %s ",
1235 sector_erased(info, i) ? 'E' : ' ',
1236 info->protect[i] ? "RO" : " ");
1237 #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
1238 printf (" %08lX %s ",
1240 info->protect[i] ? "RO" : " ");
1247 /*-----------------------------------------------------------------------
1248 * This is used in a few places in write_buf() to show programming
1249 * progress. Making it a function is nasty because it needs to do side
1250 * effect updates to digit and dots. Repeated code is nasty too, so
1251 * we define it once here.
1253 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1254 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
1255 if (flash_verbose) { \
1257 if ((scale > 0) && (dots <= 0)) { \
1258 if ((digit % 5) == 0) \
1259 printf ("%d", digit / 5); \
1267 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1270 /*-----------------------------------------------------------------------
1271 * Copy memory to flash, returns:
1274 * 2 - Flash not erased
1276 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
1283 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1286 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1287 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1292 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1294 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1295 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1296 CONFIG_FLASH_SHOW_PROGRESS);
1300 /* get lower aligned address */
1301 wp = (addr & ~(info->portwidth - 1));
1303 /* handle unaligned start */
1304 if ((aln = addr - wp) != 0) {
1307 for (i = 0; i < aln; ++i)
1308 flash_add_byte (info, &cword, flash_read8(p + i));
1310 for (; (i < info->portwidth) && (cnt > 0); i++) {
1311 flash_add_byte (info, &cword, *src++);
1314 for (; (cnt == 0) && (i < info->portwidth); ++i)
1315 flash_add_byte (info, &cword, flash_read8(p + i));
1317 rc = flash_write_cfiword (info, wp, cword);
1322 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1325 /* handle the aligned part */
1326 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1327 buffered_size = (info->portwidth / info->chipwidth);
1328 buffered_size *= info->buffer_size;
1329 while (cnt >= info->portwidth) {
1330 /* prohibit buffer write when buffer_size is 1 */
1331 if (info->buffer_size == 1) {
1333 for (i = 0; i < info->portwidth; i++)
1334 flash_add_byte (info, &cword, *src++);
1335 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1337 wp += info->portwidth;
1338 cnt -= info->portwidth;
1342 /* write buffer until next buffered_size aligned boundary */
1343 i = buffered_size - (wp % buffered_size);
1346 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1348 i -= i & (info->portwidth - 1);
1352 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1355 while (cnt >= info->portwidth) {
1357 for (i = 0; i < info->portwidth; i++) {
1358 flash_add_byte (info, &cword, *src++);
1360 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1362 wp += info->portwidth;
1363 cnt -= info->portwidth;
1364 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
1366 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1373 * handle unaligned tail bytes
1377 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
1378 flash_add_byte (info, &cword, *src++);
1381 for (; i < info->portwidth; ++i)
1382 flash_add_byte (info, &cword, flash_read8(p + i));
1384 return flash_write_cfiword (info, wp, cword);
1387 /*-----------------------------------------------------------------------
1389 #ifdef CONFIG_SYS_FLASH_PROTECTION
1391 int flash_real_protect (flash_info_t * info, long sector, int prot)
1395 switch (info->vendor) {
1396 case CFI_CMDSET_INTEL_PROG_REGIONS:
1397 case CFI_CMDSET_INTEL_STANDARD:
1398 case CFI_CMDSET_INTEL_EXTENDED:
1401 * "Numonyx Axcell P33/P30 Specification Update" :)
1403 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_ID);
1404 if (!flash_isequal (info, sector, FLASH_OFFSET_PROTECT,
1407 * cmd must come before FLASH_CMD_PROTECT + 20us
1408 * Disable interrupts which might cause a timeout here.
1410 int flag = disable_interrupts ();
1414 cmd = FLASH_CMD_PROTECT_SET;
1416 cmd = FLASH_CMD_PROTECT_CLEAR;
1418 flash_write_cmd (info, sector, 0,
1420 flash_write_cmd (info, sector, 0, cmd);
1421 /* re-enable interrupts if necessary */
1423 enable_interrupts ();
1426 case CFI_CMDSET_AMD_EXTENDED:
1427 case CFI_CMDSET_AMD_STANDARD:
1428 /* U-Boot only checks the first byte */
1429 if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
1431 flash_unlock_seq (info, 0);
1432 flash_write_cmd (info, 0,
1434 ATM_CMD_SOFTLOCK_START);
1435 flash_unlock_seq (info, 0);
1436 flash_write_cmd (info, sector, 0,
1439 flash_write_cmd (info, 0,
1441 AMD_CMD_UNLOCK_START);
1442 if (info->device_id == ATM_ID_BV6416)
1443 flash_write_cmd (info, sector,
1444 0, ATM_CMD_UNLOCK_SECT);
1448 #ifdef CONFIG_FLASH_CFI_LEGACY
1449 case CFI_CMDSET_AMD_LEGACY:
1450 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1451 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1453 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1455 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1460 * Flash needs to be in status register read mode for
1461 * flash_full_status_check() to work correctly
1463 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
1465 flash_full_status_check (info, sector, info->erase_blk_tout,
1466 prot ? "protect" : "unprotect")) == 0) {
1468 info->protect[sector] = prot;
1471 * On some of Intel's flash chips (marked via legacy_unlock)
1472 * unprotect unprotects all locking.
1474 if ((prot == 0) && (info->legacy_unlock)) {
1477 for (i = 0; i < info->sector_count; i++) {
1478 if (info->protect[i])
1479 flash_real_protect (info, i, 1);
1486 /*-----------------------------------------------------------------------
1487 * flash_read_user_serial - read the OneTimeProgramming cells
1489 void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1496 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
1497 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1498 memcpy (dst, src + offset, len);
1499 flash_write_cmd (info, 0, 0, info->cmd_reset);
1501 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
1505 * flash_read_factory_serial - read the device Id from the protection area
1507 void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1512 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1513 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1514 memcpy (buffer, src + offset, len);
1515 flash_write_cmd (info, 0, 0, info->cmd_reset);
1517 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
1520 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1522 /*-----------------------------------------------------------------------
1523 * Reverse the order of the erase regions in the CFI QRY structure.
1524 * This is needed for chips that are either a) correctly detected as
1525 * top-boot, or b) buggy.
1527 static void cfi_reverse_geometry(struct cfi_qry *qry)
1532 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1533 tmp = qry->erase_region_info[i];
1534 qry->erase_region_info[i] = qry->erase_region_info[j];
1535 qry->erase_region_info[j] = tmp;
1539 /*-----------------------------------------------------------------------
1540 * read jedec ids from device and set corresponding fields in info struct
1542 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1545 static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1547 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1549 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1550 udelay(1000); /* some flash are slow to respond */
1551 info->manufacturer_id = flash_read_uchar (info,
1552 FLASH_OFFSET_MANUFACTURER_ID);
1553 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1554 flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
1555 flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
1556 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1559 static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1561 info->cmd_reset = FLASH_CMD_RESET;
1563 cmdset_intel_read_jedec_ids(info);
1564 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1566 #ifdef CONFIG_SYS_FLASH_PROTECTION
1567 /* read legacy lock/unlock bit from intel flash */
1568 if (info->ext_addr) {
1569 info->legacy_unlock = flash_read_uchar (info,
1570 info->ext_addr + 5) & 0x08;
1577 static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1582 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1583 flash_unlock_seq(info, 0);
1584 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1585 udelay(1000); /* some flash are slow to respond */
1587 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1588 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1589 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1591 manuId = flash_read_uchar (info,
1592 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1594 info->manufacturer_id = manuId;
1596 switch (info->chipwidth){
1597 case FLASH_CFI_8BIT:
1598 info->device_id = flash_read_uchar (info,
1599 FLASH_OFFSET_DEVICE_ID);
1600 if (info->device_id == 0x7E) {
1601 /* AMD 3-byte (expanded) device ids */
1602 info->device_id2 = flash_read_uchar (info,
1603 FLASH_OFFSET_DEVICE_ID2);
1604 info->device_id2 <<= 8;
1605 info->device_id2 |= flash_read_uchar (info,
1606 FLASH_OFFSET_DEVICE_ID3);
1609 case FLASH_CFI_16BIT:
1610 info->device_id = flash_read_word (info,
1611 FLASH_OFFSET_DEVICE_ID);
1612 if ((info->device_id & 0xff) == 0x7E) {
1613 /* AMD 3-byte (expanded) device ids */
1614 info->device_id2 = flash_read_uchar (info,
1615 FLASH_OFFSET_DEVICE_ID2);
1616 info->device_id2 <<= 8;
1617 info->device_id2 |= flash_read_uchar (info,
1618 FLASH_OFFSET_DEVICE_ID3);
1624 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1628 static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1630 info->cmd_reset = AMD_CMD_RESET;
1632 cmdset_amd_read_jedec_ids(info);
1633 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1638 #ifdef CONFIG_FLASH_CFI_LEGACY
1639 static void flash_read_jedec_ids (flash_info_t * info)
1641 info->manufacturer_id = 0;
1642 info->device_id = 0;
1643 info->device_id2 = 0;
1645 switch (info->vendor) {
1646 case CFI_CMDSET_INTEL_PROG_REGIONS:
1647 case CFI_CMDSET_INTEL_STANDARD:
1648 case CFI_CMDSET_INTEL_EXTENDED:
1649 cmdset_intel_read_jedec_ids(info);
1651 case CFI_CMDSET_AMD_STANDARD:
1652 case CFI_CMDSET_AMD_EXTENDED:
1653 cmdset_amd_read_jedec_ids(info);
1660 /*-----------------------------------------------------------------------
1661 * Call board code to request info about non-CFI flash.
1662 * board_flash_get_legacy needs to fill in at least:
1663 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1665 static int flash_detect_legacy(phys_addr_t base, int banknum)
1667 flash_info_t *info = &flash_info[banknum];
1669 if (board_flash_get_legacy(base, banknum, info)) {
1670 /* board code may have filled info completely. If not, we
1671 use JEDEC ID probing. */
1672 if (!info->vendor) {
1674 CFI_CMDSET_AMD_STANDARD,
1675 CFI_CMDSET_INTEL_STANDARD
1679 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1680 info->vendor = modes[i];
1682 (ulong)map_physmem(base,
1685 if (info->portwidth == FLASH_CFI_8BIT
1686 && info->interface == FLASH_CFI_X8X16) {
1687 info->addr_unlock1 = 0x2AAA;
1688 info->addr_unlock2 = 0x5555;
1690 info->addr_unlock1 = 0x5555;
1691 info->addr_unlock2 = 0x2AAA;
1693 flash_read_jedec_ids(info);
1694 debug("JEDEC PROBE: ID %x %x %x\n",
1695 info->manufacturer_id,
1698 if (jedec_flash_match(info, info->start[0]))
1701 unmap_physmem((void *)info->start[0],
1706 switch(info->vendor) {
1707 case CFI_CMDSET_INTEL_PROG_REGIONS:
1708 case CFI_CMDSET_INTEL_STANDARD:
1709 case CFI_CMDSET_INTEL_EXTENDED:
1710 info->cmd_reset = FLASH_CMD_RESET;
1712 case CFI_CMDSET_AMD_STANDARD:
1713 case CFI_CMDSET_AMD_EXTENDED:
1714 case CFI_CMDSET_AMD_LEGACY:
1715 info->cmd_reset = AMD_CMD_RESET;
1718 info->flash_id = FLASH_MAN_CFI;
1721 return 0; /* use CFI */
1724 static inline int flash_detect_legacy(phys_addr_t base, int banknum)
1726 return 0; /* use CFI */
1730 /*-----------------------------------------------------------------------
1731 * detect if flash is compatible with the Common Flash Interface (CFI)
1732 * http://www.jedec.org/download/search/jesd68.pdf
1734 static void flash_read_cfi (flash_info_t *info, void *buf,
1735 unsigned int start, size_t len)
1740 for (i = 0; i < len; i++)
1741 p[i] = flash_read_uchar(info, start + i);
1744 void __flash_cmd_reset(flash_info_t *info)
1747 * We do not yet know what kind of commandset to use, so we issue
1748 * the reset command in both Intel and AMD variants, in the hope
1749 * that AMD flash roms ignore the Intel command.
1751 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1753 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1755 void flash_cmd_reset(flash_info_t *info)
1756 __attribute__((weak,alias("__flash_cmd_reset")));
1758 static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1762 /* Issue FLASH reset command */
1763 flash_cmd_reset(info);
1766 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1768 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1770 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1771 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1772 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1773 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1774 sizeof(struct cfi_qry));
1775 info->interface = le16_to_cpu(qry->interface_desc);
1777 info->cfi_offset = flash_offset_cfi[cfi_offset];
1778 debug ("device interface is %d\n",
1780 debug ("found port %d chip %d ",
1781 info->portwidth, info->chipwidth);
1782 debug ("port %d bits chip %d bits\n",
1783 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1784 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1786 /* calculate command offsets as in the Linux driver */
1787 info->addr_unlock1 = 0x555;
1788 info->addr_unlock2 = 0x2aa;
1791 * modify the unlock address if we are
1792 * in compatibility mode
1794 if ( /* x8/x16 in x8 mode */
1795 ((info->chipwidth == FLASH_CFI_BY8) &&
1796 (info->interface == FLASH_CFI_X8X16)) ||
1797 /* x16/x32 in x16 mode */
1798 ((info->chipwidth == FLASH_CFI_BY16) &&
1799 (info->interface == FLASH_CFI_X16X32)))
1801 info->addr_unlock1 = 0xaaa;
1802 info->addr_unlock2 = 0x555;
1805 info->name = "CFI conformant";
1813 static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1815 debug ("flash detect cfi\n");
1817 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
1818 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1819 for (info->chipwidth = FLASH_CFI_BY8;
1820 info->chipwidth <= info->portwidth;
1821 info->chipwidth <<= 1)
1822 if (__flash_detect_cfi(info, qry))
1825 debug ("not found\n");
1830 * Manufacturer-specific quirks. Add workarounds for geometry
1831 * reversal, etc. here.
1833 static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1835 /* check if flash geometry needs reversal */
1836 if (qry->num_erase_regions > 1) {
1837 /* reverse geometry if top boot part */
1838 if (info->cfi_version < 0x3131) {
1839 /* CFI < 1.1, try to guess from device id */
1840 if ((info->device_id & 0x80) != 0)
1841 cfi_reverse_geometry(qry);
1842 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1843 /* CFI >= 1.1, deduct from top/bottom flag */
1844 /* note: ext_addr is valid since cfi_version > 0 */
1845 cfi_reverse_geometry(qry);
1850 static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1852 int reverse_geometry = 0;
1854 /* Check the "top boot" bit in the PRI */
1855 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1856 reverse_geometry = 1;
1858 /* AT49BV6416(T) list the erase regions in the wrong order.
1859 * However, the device ID is identical with the non-broken
1860 * AT49BV642D they differ in the high byte.
1862 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1863 reverse_geometry = !reverse_geometry;
1865 if (reverse_geometry)
1866 cfi_reverse_geometry(qry);
1869 static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1871 /* check if flash geometry needs reversal */
1872 if (qry->num_erase_regions > 1) {
1873 /* reverse geometry if top boot part */
1874 if (info->cfi_version < 0x3131) {
1875 /* CFI < 1.1, guess by device id */
1876 if (info->device_id == 0x22CA || /* M29W320DT */
1877 info->device_id == 0x2256 || /* M29W320ET */
1878 info->device_id == 0x22D7) { /* M29W800DT */
1879 cfi_reverse_geometry(qry);
1881 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1882 /* CFI >= 1.1, deduct from top/bottom flag */
1883 /* note: ext_addr is valid since cfi_version > 0 */
1884 cfi_reverse_geometry(qry);
1890 * The following code cannot be run from FLASH!
1893 ulong flash_get_size (phys_addr_t base, int banknum)
1895 flash_info_t *info = &flash_info[banknum];
1897 flash_sect_t sect_cnt;
1901 uchar num_erase_regions;
1902 int erase_region_size;
1903 int erase_region_count;
1905 unsigned long max_size;
1907 memset(&qry, 0, sizeof(qry));
1910 info->cfi_version = 0;
1911 #ifdef CONFIG_SYS_FLASH_PROTECTION
1912 info->legacy_unlock = 0;
1915 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
1917 if (flash_detect_cfi (info, &qry)) {
1918 info->vendor = le16_to_cpu(qry.p_id);
1919 info->ext_addr = le16_to_cpu(qry.p_adr);
1920 num_erase_regions = qry.num_erase_regions;
1922 if (info->ext_addr) {
1923 info->cfi_version = (ushort) flash_read_uchar (info,
1924 info->ext_addr + 3) << 8;
1925 info->cfi_version |= (ushort) flash_read_uchar (info,
1926 info->ext_addr + 4);
1930 flash_printqry (&qry);
1933 switch (info->vendor) {
1934 case CFI_CMDSET_INTEL_PROG_REGIONS:
1935 case CFI_CMDSET_INTEL_STANDARD:
1936 case CFI_CMDSET_INTEL_EXTENDED:
1937 cmdset_intel_init(info, &qry);
1939 case CFI_CMDSET_AMD_STANDARD:
1940 case CFI_CMDSET_AMD_EXTENDED:
1941 cmdset_amd_init(info, &qry);
1944 printf("CFI: Unknown command set 0x%x\n",
1947 * Unfortunately, this means we don't know how
1948 * to get the chip back to Read mode. Might
1949 * as well try an Intel-style reset...
1951 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1955 /* Do manufacturer-specific fixups */
1956 switch (info->manufacturer_id) {
1957 case 0x0001: /* AMD */
1958 case 0x0037: /* AMIC */
1959 flash_fixup_amd(info, &qry);
1962 flash_fixup_atmel(info, &qry);
1965 flash_fixup_stm(info, &qry);
1969 debug ("manufacturer is %d\n", info->vendor);
1970 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1971 debug ("device id is 0x%x\n", info->device_id);
1972 debug ("device id2 is 0x%x\n", info->device_id2);
1973 debug ("cfi version is 0x%04x\n", info->cfi_version);
1975 size_ratio = info->portwidth / info->chipwidth;
1976 /* if the chip is x8/x16 reduce the ratio by half */
1977 if ((info->interface == FLASH_CFI_X8X16)
1978 && (info->chipwidth == FLASH_CFI_BY8)) {
1981 debug ("size_ratio %d port %d bits chip %d bits\n",
1982 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1983 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1984 info->size = 1 << qry.dev_size;
1985 /* multiply the size by the number of chips */
1986 info->size *= size_ratio;
1987 max_size = cfi_flash_bank_size(banknum);
1988 if (max_size && (info->size > max_size)) {
1989 debug("[truncated from %ldMiB]", info->size >> 20);
1990 info->size = max_size;
1992 debug ("found %d erase regions\n", num_erase_regions);
1995 for (i = 0; i < num_erase_regions; i++) {
1996 if (i > NUM_ERASE_REGIONS) {
1997 printf ("%d erase regions found, only %d used\n",
1998 num_erase_regions, NUM_ERASE_REGIONS);
2002 tmp = le32_to_cpu(qry.erase_region_info[i]);
2003 debug("erase region %u: 0x%08lx\n", i, tmp);
2005 erase_region_count = (tmp & 0xffff) + 1;
2008 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
2009 debug ("erase_region_count = %d erase_region_size = %d\n",
2010 erase_region_count, erase_region_size);
2011 for (j = 0; j < erase_region_count; j++) {
2012 if (sector - base >= info->size)
2014 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
2015 printf("ERROR: too many flash sectors\n");
2018 info->start[sect_cnt] =
2019 (ulong)map_physmem(sector,
2022 sector += (erase_region_size * size_ratio);
2025 * Only read protection status from
2026 * supported devices (intel...)
2028 switch (info->vendor) {
2029 case CFI_CMDSET_INTEL_PROG_REGIONS:
2030 case CFI_CMDSET_INTEL_EXTENDED:
2031 case CFI_CMDSET_INTEL_STANDARD:
2033 * Set flash to read-id mode. Otherwise
2034 * reading protected status is not
2037 flash_write_cmd(info, sect_cnt, 0,
2039 info->protect[sect_cnt] =
2040 flash_isset (info, sect_cnt,
2041 FLASH_OFFSET_PROTECT,
2042 FLASH_STATUS_PROTECT);
2045 /* default: not protected */
2046 info->protect[sect_cnt] = 0;
2053 info->sector_count = sect_cnt;
2054 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2055 tmp = 1 << qry.block_erase_timeout_typ;
2056 info->erase_blk_tout = tmp *
2057 (1 << qry.block_erase_timeout_max);
2058 tmp = (1 << qry.buf_write_timeout_typ) *
2059 (1 << qry.buf_write_timeout_max);
2061 /* round up when converting to ms */
2062 info->buffer_write_tout = (tmp + 999) / 1000;
2063 tmp = (1 << qry.word_write_timeout_typ) *
2064 (1 << qry.word_write_timeout_max);
2065 /* round up when converting to ms */
2066 info->write_tout = (tmp + 999) / 1000;
2067 info->flash_id = FLASH_MAN_CFI;
2068 if ((info->interface == FLASH_CFI_X8X16) &&
2069 (info->chipwidth == FLASH_CFI_BY8)) {
2070 /* XXX - Need to test on x8/x16 in parallel. */
2071 info->portwidth >>= 1;
2074 flash_write_cmd (info, 0, 0, info->cmd_reset);
2077 return (info->size);
2080 #ifdef CONFIG_FLASH_CFI_MTD
2081 void flash_set_verbose(uint v)
2087 static void cfi_flash_set_config_reg(u32 base, u16 val)
2089 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2091 * Only set this config register if really defined
2092 * to a valid value (0xffff is invalid)
2098 * Set configuration register. Data is "encrypted" in the 16 lower
2101 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2102 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2105 * Finally issue reset-command to bring device back to
2108 flash_write16(FLASH_CMD_RESET, (void *)base);
2112 /*-----------------------------------------------------------------------
2115 void flash_protect_default(void)
2117 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2122 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2125 /* Monitor protection ON by default */
2126 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2127 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2128 flash_protect(FLAG_PROTECT_SET,
2129 CONFIG_SYS_MONITOR_BASE,
2130 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2131 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2134 /* Environment protection ON by default */
2135 #ifdef CONFIG_ENV_IS_IN_FLASH
2136 flash_protect(FLAG_PROTECT_SET,
2138 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2139 flash_get_info(CONFIG_ENV_ADDR));
2142 /* Redundant environment protection ON by default */
2143 #ifdef CONFIG_ENV_ADDR_REDUND
2144 flash_protect(FLAG_PROTECT_SET,
2145 CONFIG_ENV_ADDR_REDUND,
2146 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2147 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2150 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2151 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
2152 debug("autoprotecting from %08lx to %08lx\n",
2153 apl[i].start, apl[i].start + apl[i].size - 1);
2154 flash_protect(FLAG_PROTECT_SET,
2156 apl[i].start + apl[i].size - 1,
2157 flash_get_info(apl[i].start));
2162 unsigned long flash_init (void)
2164 unsigned long size = 0;
2167 #ifdef CONFIG_SYS_FLASH_PROTECTION
2168 /* read environment from EEPROM */
2170 getenv_f("unlock", s, sizeof(s));
2173 /* Init: no FLASHes known */
2174 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
2175 flash_info[i].flash_id = FLASH_UNKNOWN;
2177 /* Optionally write flash configuration register */
2178 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2179 cfi_flash_config_reg(i));
2181 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
2182 flash_get_size(cfi_flash_bank_addr(i), i);
2183 size += flash_info[i].size;
2184 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
2185 #ifndef CONFIG_SYS_FLASH_QUIET_TEST
2186 printf ("## Unknown flash on Bank %d "
2187 "- Size = 0x%08lx = %ld MB\n",
2188 i+1, flash_info[i].size,
2189 flash_info[i].size >> 20);
2190 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
2192 #ifdef CONFIG_SYS_FLASH_PROTECTION
2193 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
2195 * Only the U-Boot image and it's environment
2196 * is protected, all other sectors are
2197 * unprotected (unlocked) if flash hardware
2198 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
2199 * and the environment variable "unlock" is
2202 if (flash_info[i].legacy_unlock) {
2206 * Disable legacy_unlock temporarily,
2207 * since flash_real_protect would
2208 * relock all other sectors again
2211 flash_info[i].legacy_unlock = 0;
2214 * Legacy unlocking (e.g. Intel J3) ->
2215 * unlock only one sector. This will
2216 * unlock all sectors.
2218 flash_real_protect (&flash_info[i], 0, 0);
2220 flash_info[i].legacy_unlock = 1;
2223 * Manually mark other sectors as
2224 * unlocked (unprotected)
2226 for (k = 1; k < flash_info[i].sector_count; k++)
2227 flash_info[i].protect[k] = 0;
2230 * No legancy unlocking -> unlock all sectors
2232 flash_protect (FLAG_PROTECT_CLEAR,
2233 flash_info[i].start[0],
2234 flash_info[i].start[0]
2235 + flash_info[i].size - 1,
2239 #endif /* CONFIG_SYS_FLASH_PROTECTION */
2242 flash_protect_default();
2243 #ifdef CONFIG_FLASH_CFI_MTD