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 #ifndef CONFIG_SYS_FLASH_BANKS_LIST
66 #define CONFIG_SYS_FLASH_BANKS_LIST { CONFIG_SYS_FLASH_BASE }
69 static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
70 static uint flash_verbose = 1;
72 /* use CONFIG_SYS_MAX_FLASH_BANKS_DETECT if defined */
73 #ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
74 # define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS_DETECT
76 # define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS
79 flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
82 * Check if chip width is defined. If not, start detecting with 8bit.
84 #ifndef CONFIG_SYS_FLASH_CFI_WIDTH
85 #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
88 static void __flash_write8(u8 value, void *addr)
90 __raw_writeb(value, addr);
93 static void __flash_write16(u16 value, void *addr)
95 __raw_writew(value, addr);
98 static void __flash_write32(u32 value, void *addr)
100 __raw_writel(value, addr);
103 static void __flash_write64(u64 value, void *addr)
105 /* No architectures currently implement __raw_writeq() */
106 *(volatile u64 *)addr = value;
109 static u8 __flash_read8(void *addr)
111 return __raw_readb(addr);
114 static u16 __flash_read16(void *addr)
116 return __raw_readw(addr);
119 static u32 __flash_read32(void *addr)
121 return __raw_readl(addr);
124 static u64 __flash_read64(void *addr)
126 /* No architectures currently implement __raw_readq() */
127 return *(volatile u64 *)addr;
130 #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
131 void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
132 void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
133 void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
134 void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
135 u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
136 u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
137 u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
138 u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
140 #define flash_write8 __flash_write8
141 #define flash_write16 __flash_write16
142 #define flash_write32 __flash_write32
143 #define flash_write64 __flash_write64
144 #define flash_read8 __flash_read8
145 #define flash_read16 __flash_read16
146 #define flash_read32 __flash_read32
147 #define flash_read64 __flash_read64
150 /*-----------------------------------------------------------------------
152 #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
153 flash_info_t *flash_get_info(ulong base)
156 flash_info_t * info = 0;
158 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
159 info = & flash_info[i];
160 if (info->size && info->start[0] <= base &&
161 base <= info->start[0] + info->size - 1)
165 return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
169 unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
171 if (sect != (info->sector_count - 1))
172 return info->start[sect + 1] - info->start[sect];
174 return info->start[0] + info->size - info->start[sect];
177 /*-----------------------------------------------------------------------
178 * create an address based on the offset and the port width
181 flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
183 unsigned int byte_offset = offset * info->portwidth;
185 return (void *)(info->start[sect] + byte_offset);
188 static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
189 unsigned int offset, void *addr)
193 /*-----------------------------------------------------------------------
194 * make a proper sized command based on the port and chip widths
196 static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
201 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
202 u32 cmd_le = cpu_to_le32(cmd);
205 uchar *cp = (uchar *) cmdbuf;
207 for (i = info->portwidth; i > 0; i--){
208 cword_offset = (info->portwidth-i)%info->chipwidth;
209 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
210 cp_offset = info->portwidth - i;
211 val = *((uchar*)&cmd_le + cword_offset);
214 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
216 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
221 /*-----------------------------------------------------------------------
224 static void print_longlong (char *str, unsigned long long data)
230 for (i = 0; i < 8; i++)
231 sprintf (&str[i * 2], "%2.2x", *cp++);
234 static void flash_printqry (struct cfi_qry *qry)
239 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
241 for (y = 0; y < 16; y++)
242 debug("%2.2x ", p[x + y]);
244 for (y = 0; y < 16; y++) {
245 unsigned char c = p[x + y];
246 if (c >= 0x20 && c <= 0x7e)
257 /*-----------------------------------------------------------------------
258 * read a character at a port width address
260 static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
265 cp = flash_map (info, 0, offset);
266 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
267 retval = flash_read8(cp);
269 retval = flash_read8(cp + info->portwidth - 1);
271 flash_unmap (info, 0, offset, cp);
275 /*-----------------------------------------------------------------------
276 * read a word at a port width address, assume 16bit bus
278 static inline ushort flash_read_word (flash_info_t * info, uint offset)
280 ushort *addr, retval;
282 addr = flash_map (info, 0, offset);
283 retval = flash_read16 (addr);
284 flash_unmap (info, 0, offset, addr);
289 /*-----------------------------------------------------------------------
290 * read a long word by picking the least significant byte of each maximum
291 * port size word. Swap for ppc format.
293 static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
302 addr = flash_map (info, sect, offset);
305 debug ("long addr is at %p info->portwidth = %d\n", addr,
307 for (x = 0; x < 4 * info->portwidth; x++) {
308 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
311 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
312 retval = ((flash_read8(addr) << 16) |
313 (flash_read8(addr + info->portwidth) << 24) |
314 (flash_read8(addr + 2 * info->portwidth)) |
315 (flash_read8(addr + 3 * info->portwidth) << 8));
317 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
318 (flash_read8(addr + info->portwidth - 1) << 16) |
319 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
320 (flash_read8(addr + 3 * info->portwidth - 1)));
322 flash_unmap(info, sect, offset, addr);
328 * Write a proper sized command to the correct address
330 void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
331 uint offset, u32 cmd)
337 addr = flash_map (info, sect, offset);
338 flash_make_cmd (info, cmd, &cword);
339 switch (info->portwidth) {
341 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
342 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
343 flash_write8(cword.c, addr);
345 case FLASH_CFI_16BIT:
346 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
348 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
349 flash_write16(cword.w, addr);
351 case FLASH_CFI_32BIT:
352 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
354 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
355 flash_write32(cword.l, addr);
357 case FLASH_CFI_64BIT:
362 print_longlong (str, cword.ll);
364 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
366 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
369 flash_write64(cword.ll, addr);
373 /* Ensure all the instructions are fully finished */
376 flash_unmap(info, sect, offset, addr);
379 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
381 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
382 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
385 /*-----------------------------------------------------------------------
387 static int flash_isequal (flash_info_t * info, flash_sect_t sect,
388 uint offset, uchar cmd)
394 addr = flash_map (info, sect, offset);
395 flash_make_cmd (info, cmd, &cword);
397 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
398 switch (info->portwidth) {
400 debug ("is= %x %x\n", flash_read8(addr), cword.c);
401 retval = (flash_read8(addr) == cword.c);
403 case FLASH_CFI_16BIT:
404 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
405 retval = (flash_read16(addr) == cword.w);
407 case FLASH_CFI_32BIT:
408 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
409 retval = (flash_read32(addr) == cword.l);
411 case FLASH_CFI_64BIT:
417 print_longlong (str1, flash_read64(addr));
418 print_longlong (str2, cword.ll);
419 debug ("is= %s %s\n", str1, str2);
422 retval = (flash_read64(addr) == cword.ll);
428 flash_unmap(info, sect, offset, addr);
433 /*-----------------------------------------------------------------------
435 static int flash_isset (flash_info_t * info, flash_sect_t sect,
436 uint offset, uchar cmd)
442 addr = flash_map (info, sect, offset);
443 flash_make_cmd (info, cmd, &cword);
444 switch (info->portwidth) {
446 retval = ((flash_read8(addr) & cword.c) == cword.c);
448 case FLASH_CFI_16BIT:
449 retval = ((flash_read16(addr) & cword.w) == cword.w);
451 case FLASH_CFI_32BIT:
452 retval = ((flash_read32(addr) & cword.l) == cword.l);
454 case FLASH_CFI_64BIT:
455 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
461 flash_unmap(info, sect, offset, addr);
466 /*-----------------------------------------------------------------------
468 static int flash_toggle (flash_info_t * info, flash_sect_t sect,
469 uint offset, uchar cmd)
475 addr = flash_map (info, sect, offset);
476 flash_make_cmd (info, cmd, &cword);
477 switch (info->portwidth) {
479 retval = flash_read8(addr) != flash_read8(addr);
481 case FLASH_CFI_16BIT:
482 retval = flash_read16(addr) != flash_read16(addr);
484 case FLASH_CFI_32BIT:
485 retval = flash_read32(addr) != flash_read32(addr);
487 case FLASH_CFI_64BIT:
488 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
489 (flash_read32(addr+4) != flash_read32(addr+4)) );
495 flash_unmap(info, sect, offset, addr);
501 * flash_is_busy - check to see if the flash is busy
503 * This routine checks the status of the chip and returns true if the
506 static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
510 switch (info->vendor) {
511 case CFI_CMDSET_INTEL_PROG_REGIONS:
512 case CFI_CMDSET_INTEL_STANDARD:
513 case CFI_CMDSET_INTEL_EXTENDED:
514 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
516 case CFI_CMDSET_AMD_STANDARD:
517 case CFI_CMDSET_AMD_EXTENDED:
518 #ifdef CONFIG_FLASH_CFI_LEGACY
519 case CFI_CMDSET_AMD_LEGACY:
521 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
526 debug ("flash_is_busy: %d\n", retval);
530 /*-----------------------------------------------------------------------
531 * wait for XSR.7 to be set. Time out with an error if it does not.
532 * This routine does not set the flash to read-array mode.
534 static int flash_status_check (flash_info_t * info, flash_sect_t sector,
535 ulong tout, char *prompt)
539 #if CONFIG_SYS_HZ != 1000
540 if ((ulong)CONFIG_SYS_HZ > 100000)
541 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
543 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
546 /* Wait for command completion */
547 start = get_timer (0);
548 while (flash_is_busy (info, sector)) {
549 if (get_timer (start) > tout) {
550 printf ("Flash %s timeout at address %lx data %lx\n",
551 prompt, info->start[sector],
552 flash_read_long (info, sector, 0));
553 flash_write_cmd (info, sector, 0, info->cmd_reset);
556 udelay (1); /* also triggers watchdog */
561 /*-----------------------------------------------------------------------
562 * Wait for XSR.7 to be set, if it times out print an error, otherwise
563 * do a full status check.
565 * This routine sets the flash to read-array mode.
567 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
568 ulong tout, char *prompt)
572 retcode = flash_status_check (info, sector, tout, prompt);
573 switch (info->vendor) {
574 case CFI_CMDSET_INTEL_PROG_REGIONS:
575 case CFI_CMDSET_INTEL_EXTENDED:
576 case CFI_CMDSET_INTEL_STANDARD:
577 if ((retcode != ERR_OK)
578 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
580 printf ("Flash %s error at address %lx\n", prompt,
581 info->start[sector]);
582 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
583 FLASH_STATUS_PSLBS)) {
584 puts ("Command Sequence Error.\n");
585 } else if (flash_isset (info, sector, 0,
586 FLASH_STATUS_ECLBS)) {
587 puts ("Block Erase Error.\n");
588 retcode = ERR_NOT_ERASED;
589 } else if (flash_isset (info, sector, 0,
590 FLASH_STATUS_PSLBS)) {
591 puts ("Locking Error\n");
593 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
594 puts ("Block locked.\n");
595 retcode = ERR_PROTECTED;
597 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
598 puts ("Vpp Low Error.\n");
600 flash_write_cmd (info, sector, 0, info->cmd_reset);
608 /*-----------------------------------------------------------------------
610 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
612 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
615 unsigned long long ll;
618 switch (info->portwidth) {
622 case FLASH_CFI_16BIT:
623 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
626 cword->w = (cword->w >> 8) | w;
628 cword->w = (cword->w << 8) | c;
631 case FLASH_CFI_32BIT:
632 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
635 cword->l = (cword->l >> 8) | l;
637 cword->l = (cword->l << 8) | c;
640 case FLASH_CFI_64BIT:
641 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
644 cword->ll = (cword->ll >> 8) | ll;
646 cword->ll = (cword->ll << 8) | c;
653 * Loop through the sector table starting from the previously found sector.
654 * Searches forwards or backwards, dependent on the passed address.
656 static flash_sect_t find_sector (flash_info_t * info, ulong addr)
658 static flash_sect_t saved_sector = 0; /* previously found sector */
659 flash_sect_t sector = saved_sector;
661 while ((info->start[sector] < addr)
662 && (sector < info->sector_count - 1))
664 while ((info->start[sector] > addr) && (sector > 0))
666 * also decrements the sector in case of an overshot
671 saved_sector = sector;
675 /*-----------------------------------------------------------------------
677 static int flash_write_cfiword (flash_info_t * info, ulong dest,
680 void *dstaddr = (void *)dest;
682 flash_sect_t sect = 0;
685 /* Check if Flash is (sufficiently) erased */
686 switch (info->portwidth) {
688 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
690 case FLASH_CFI_16BIT:
691 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
693 case FLASH_CFI_32BIT:
694 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
696 case FLASH_CFI_64BIT:
697 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
704 return ERR_NOT_ERASED;
706 /* Disable interrupts which might cause a timeout here */
707 flag = disable_interrupts ();
709 switch (info->vendor) {
710 case CFI_CMDSET_INTEL_PROG_REGIONS:
711 case CFI_CMDSET_INTEL_EXTENDED:
712 case CFI_CMDSET_INTEL_STANDARD:
713 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
714 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
716 case CFI_CMDSET_AMD_EXTENDED:
717 case CFI_CMDSET_AMD_STANDARD:
718 sect = find_sector(info, dest);
719 flash_unlock_seq (info, sect);
720 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
723 #ifdef CONFIG_FLASH_CFI_LEGACY
724 case CFI_CMDSET_AMD_LEGACY:
725 sect = find_sector(info, dest);
726 flash_unlock_seq (info, 0);
727 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
733 switch (info->portwidth) {
735 flash_write8(cword.c, dstaddr);
737 case FLASH_CFI_16BIT:
738 flash_write16(cword.w, dstaddr);
740 case FLASH_CFI_32BIT:
741 flash_write32(cword.l, dstaddr);
743 case FLASH_CFI_64BIT:
744 flash_write64(cword.ll, dstaddr);
748 /* re-enable interrupts if necessary */
750 enable_interrupts ();
753 sect = find_sector (info, dest);
755 return flash_full_status_check (info, sect, info->write_tout, "write");
758 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
760 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
767 void *dst = (void *)dest;
774 switch (info->portwidth) {
778 case FLASH_CFI_16BIT:
781 case FLASH_CFI_32BIT:
784 case FLASH_CFI_64BIT:
794 while ((cnt-- > 0) && (flag == 0)) {
795 switch (info->portwidth) {
797 flag = ((flash_read8(dst2) & flash_read8(src)) ==
801 case FLASH_CFI_16BIT:
802 flag = ((flash_read16(dst2) & flash_read16(src)) ==
806 case FLASH_CFI_32BIT:
807 flag = ((flash_read32(dst2) & flash_read32(src)) ==
811 case FLASH_CFI_64BIT:
812 flag = ((flash_read64(dst2) & flash_read64(src)) ==
819 retcode = ERR_NOT_ERASED;
824 sector = find_sector (info, dest);
826 switch (info->vendor) {
827 case CFI_CMDSET_INTEL_PROG_REGIONS:
828 case CFI_CMDSET_INTEL_STANDARD:
829 case CFI_CMDSET_INTEL_EXTENDED:
830 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
831 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
832 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
833 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
834 flash_write_cmd (info, sector, 0, write_cmd);
835 retcode = flash_status_check (info, sector,
836 info->buffer_write_tout,
838 if (retcode == ERR_OK) {
839 /* reduce the number of loops by the width of
842 flash_write_cmd (info, sector, 0, cnt - 1);
844 switch (info->portwidth) {
846 flash_write8(flash_read8(src), dst);
849 case FLASH_CFI_16BIT:
850 flash_write16(flash_read16(src), dst);
853 case FLASH_CFI_32BIT:
854 flash_write32(flash_read32(src), dst);
857 case FLASH_CFI_64BIT:
858 flash_write64(flash_read64(src), dst);
866 flash_write_cmd (info, sector, 0,
867 FLASH_CMD_WRITE_BUFFER_CONFIRM);
868 retcode = flash_full_status_check (
869 info, sector, info->buffer_write_tout,
875 case CFI_CMDSET_AMD_STANDARD:
876 case CFI_CMDSET_AMD_EXTENDED:
877 flash_unlock_seq(info,0);
879 #ifdef CONFIG_FLASH_SPANSION_S29WS_N
880 offset = ((unsigned long)dst - info->start[sector]) >> shift;
882 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
884 flash_write_cmd(info, sector, offset, cnt - 1);
886 switch (info->portwidth) {
889 flash_write8(flash_read8(src), dst);
893 case FLASH_CFI_16BIT:
895 flash_write16(flash_read16(src), dst);
899 case FLASH_CFI_32BIT:
901 flash_write32(flash_read32(src), dst);
905 case FLASH_CFI_64BIT:
907 flash_write64(flash_read64(src), dst);
916 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
917 retcode = flash_full_status_check (info, sector,
918 info->buffer_write_tout,
923 debug ("Unknown Command Set\n");
931 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
934 /*-----------------------------------------------------------------------
936 int flash_erase (flash_info_t * info, int s_first, int s_last)
942 if (info->flash_id != FLASH_MAN_CFI) {
943 puts ("Can't erase unknown flash type - aborted\n");
946 if ((s_first < 0) || (s_first > s_last)) {
947 puts ("- no sectors to erase\n");
952 for (sect = s_first; sect <= s_last; ++sect) {
953 if (info->protect[sect]) {
958 printf ("- Warning: %d protected sectors will not be erased!\n",
960 } else if (flash_verbose) {
965 for (sect = s_first; sect <= s_last; sect++) {
966 if (info->protect[sect] == 0) { /* not protected */
967 switch (info->vendor) {
968 case CFI_CMDSET_INTEL_PROG_REGIONS:
969 case CFI_CMDSET_INTEL_STANDARD:
970 case CFI_CMDSET_INTEL_EXTENDED:
971 flash_write_cmd (info, sect, 0,
972 FLASH_CMD_CLEAR_STATUS);
973 flash_write_cmd (info, sect, 0,
974 FLASH_CMD_BLOCK_ERASE);
975 flash_write_cmd (info, sect, 0,
976 FLASH_CMD_ERASE_CONFIRM);
978 case CFI_CMDSET_AMD_STANDARD:
979 case CFI_CMDSET_AMD_EXTENDED:
980 flash_unlock_seq (info, sect);
981 flash_write_cmd (info, sect,
983 AMD_CMD_ERASE_START);
984 flash_unlock_seq (info, sect);
985 flash_write_cmd (info, sect, 0,
986 AMD_CMD_ERASE_SECTOR);
988 #ifdef CONFIG_FLASH_CFI_LEGACY
989 case CFI_CMDSET_AMD_LEGACY:
990 flash_unlock_seq (info, 0);
991 flash_write_cmd (info, 0, info->addr_unlock1,
992 AMD_CMD_ERASE_START);
993 flash_unlock_seq (info, 0);
994 flash_write_cmd (info, sect, 0,
995 AMD_CMD_ERASE_SECTOR);
999 debug ("Unkown flash vendor %d\n",
1004 if (flash_full_status_check
1005 (info, sect, info->erase_blk_tout, "erase")) {
1007 } else if (flash_verbose)
1018 /*-----------------------------------------------------------------------
1020 void flash_print_info (flash_info_t * info)
1024 if (info->flash_id != FLASH_MAN_CFI) {
1025 puts ("missing or unknown FLASH type\n");
1029 printf ("%s FLASH (%d x %d)",
1031 (info->portwidth << 3), (info->chipwidth << 3));
1032 if (info->size < 1024*1024)
1033 printf (" Size: %ld kB in %d Sectors\n",
1034 info->size >> 10, info->sector_count);
1036 printf (" Size: %ld MB in %d Sectors\n",
1037 info->size >> 20, info->sector_count);
1039 switch (info->vendor) {
1040 case CFI_CMDSET_INTEL_PROG_REGIONS:
1041 printf ("Intel Prog Regions");
1043 case CFI_CMDSET_INTEL_STANDARD:
1044 printf ("Intel Standard");
1046 case CFI_CMDSET_INTEL_EXTENDED:
1047 printf ("Intel Extended");
1049 case CFI_CMDSET_AMD_STANDARD:
1050 printf ("AMD Standard");
1052 case CFI_CMDSET_AMD_EXTENDED:
1053 printf ("AMD Extended");
1055 #ifdef CONFIG_FLASH_CFI_LEGACY
1056 case CFI_CMDSET_AMD_LEGACY:
1057 printf ("AMD Legacy");
1061 printf ("Unknown (%d)", info->vendor);
1064 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
1065 info->manufacturer_id, info->device_id);
1066 if (info->device_id == 0x7E) {
1067 printf("%04X", info->device_id2);
1069 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1070 info->erase_blk_tout,
1072 if (info->buffer_size > 1) {
1073 printf (" Buffer write timeout: %ld ms, "
1074 "buffer size: %d bytes\n",
1075 info->buffer_write_tout,
1079 puts ("\n Sector Start Addresses:");
1080 for (i = 0; i < info->sector_count; ++i) {
1083 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1087 volatile unsigned long *flash;
1090 * Check if whole sector is erased
1092 size = flash_sector_size(info, i);
1094 flash = (volatile unsigned long *) info->start[i];
1095 size = size >> 2; /* divide by 4 for longword access */
1096 for (k = 0; k < size; k++) {
1097 if (*flash++ != 0xffffffff) {
1103 /* print empty and read-only info */
1104 printf (" %08lX %c %s ",
1107 info->protect[i] ? "RO" : " ");
1108 #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
1109 printf (" %08lX %s ",
1111 info->protect[i] ? "RO" : " ");
1118 /*-----------------------------------------------------------------------
1119 * This is used in a few places in write_buf() to show programming
1120 * progress. Making it a function is nasty because it needs to do side
1121 * effect updates to digit and dots. Repeated code is nasty too, so
1122 * we define it once here.
1124 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1125 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
1126 if (flash_verbose) { \
1128 if ((scale > 0) && (dots <= 0)) { \
1129 if ((digit % 5) == 0) \
1130 printf ("%d", digit / 5); \
1138 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1141 /*-----------------------------------------------------------------------
1142 * Copy memory to flash, returns:
1145 * 2 - Flash not erased
1147 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
1154 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1157 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1158 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1163 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1165 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1166 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1167 CONFIG_FLASH_SHOW_PROGRESS);
1171 /* get lower aligned address */
1172 wp = (addr & ~(info->portwidth - 1));
1174 /* handle unaligned start */
1175 if ((aln = addr - wp) != 0) {
1178 for (i = 0; i < aln; ++i)
1179 flash_add_byte (info, &cword, flash_read8(p + i));
1181 for (; (i < info->portwidth) && (cnt > 0); i++) {
1182 flash_add_byte (info, &cword, *src++);
1185 for (; (cnt == 0) && (i < info->portwidth); ++i)
1186 flash_add_byte (info, &cword, flash_read8(p + i));
1188 rc = flash_write_cfiword (info, wp, cword);
1193 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1196 /* handle the aligned part */
1197 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1198 buffered_size = (info->portwidth / info->chipwidth);
1199 buffered_size *= info->buffer_size;
1200 while (cnt >= info->portwidth) {
1201 /* prohibit buffer write when buffer_size is 1 */
1202 if (info->buffer_size == 1) {
1204 for (i = 0; i < info->portwidth; i++)
1205 flash_add_byte (info, &cword, *src++);
1206 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1208 wp += info->portwidth;
1209 cnt -= info->portwidth;
1213 /* write buffer until next buffered_size aligned boundary */
1214 i = buffered_size - (wp % buffered_size);
1217 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1219 i -= i & (info->portwidth - 1);
1223 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1226 while (cnt >= info->portwidth) {
1228 for (i = 0; i < info->portwidth; i++) {
1229 flash_add_byte (info, &cword, *src++);
1231 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1233 wp += info->portwidth;
1234 cnt -= info->portwidth;
1235 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
1237 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1244 * handle unaligned tail bytes
1248 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
1249 flash_add_byte (info, &cword, *src++);
1252 for (; i < info->portwidth; ++i)
1253 flash_add_byte (info, &cword, flash_read8(p + i));
1255 return flash_write_cfiword (info, wp, cword);
1258 /*-----------------------------------------------------------------------
1260 #ifdef CONFIG_SYS_FLASH_PROTECTION
1262 int flash_real_protect (flash_info_t * info, long sector, int prot)
1266 switch (info->vendor) {
1267 case CFI_CMDSET_INTEL_PROG_REGIONS:
1268 case CFI_CMDSET_INTEL_STANDARD:
1269 case CFI_CMDSET_INTEL_EXTENDED:
1270 flash_write_cmd (info, sector, 0,
1271 FLASH_CMD_CLEAR_STATUS);
1272 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1274 flash_write_cmd (info, sector, 0,
1275 FLASH_CMD_PROTECT_SET);
1277 flash_write_cmd (info, sector, 0,
1278 FLASH_CMD_PROTECT_CLEAR);
1280 case CFI_CMDSET_AMD_EXTENDED:
1281 case CFI_CMDSET_AMD_STANDARD:
1282 /* U-Boot only checks the first byte */
1283 if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
1285 flash_unlock_seq (info, 0);
1286 flash_write_cmd (info, 0,
1288 ATM_CMD_SOFTLOCK_START);
1289 flash_unlock_seq (info, 0);
1290 flash_write_cmd (info, sector, 0,
1293 flash_write_cmd (info, 0,
1295 AMD_CMD_UNLOCK_START);
1296 if (info->device_id == ATM_ID_BV6416)
1297 flash_write_cmd (info, sector,
1298 0, ATM_CMD_UNLOCK_SECT);
1302 #ifdef CONFIG_FLASH_CFI_LEGACY
1303 case CFI_CMDSET_AMD_LEGACY:
1304 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1305 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1307 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1309 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1314 flash_full_status_check (info, sector, info->erase_blk_tout,
1315 prot ? "protect" : "unprotect")) == 0) {
1317 info->protect[sector] = prot;
1320 * On some of Intel's flash chips (marked via legacy_unlock)
1321 * unprotect unprotects all locking.
1323 if ((prot == 0) && (info->legacy_unlock)) {
1326 for (i = 0; i < info->sector_count; i++) {
1327 if (info->protect[i])
1328 flash_real_protect (info, i, 1);
1335 /*-----------------------------------------------------------------------
1336 * flash_read_user_serial - read the OneTimeProgramming cells
1338 void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1345 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
1346 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1347 memcpy (dst, src + offset, len);
1348 flash_write_cmd (info, 0, 0, info->cmd_reset);
1349 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
1353 * flash_read_factory_serial - read the device Id from the protection area
1355 void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1360 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1361 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1362 memcpy (buffer, src + offset, len);
1363 flash_write_cmd (info, 0, 0, info->cmd_reset);
1364 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
1367 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1369 /*-----------------------------------------------------------------------
1370 * Reverse the order of the erase regions in the CFI QRY structure.
1371 * This is needed for chips that are either a) correctly detected as
1372 * top-boot, or b) buggy.
1374 static void cfi_reverse_geometry(struct cfi_qry *qry)
1379 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1380 tmp = qry->erase_region_info[i];
1381 qry->erase_region_info[i] = qry->erase_region_info[j];
1382 qry->erase_region_info[j] = tmp;
1386 /*-----------------------------------------------------------------------
1387 * read jedec ids from device and set corresponding fields in info struct
1389 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1392 static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1394 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1395 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1396 udelay(1000); /* some flash are slow to respond */
1397 info->manufacturer_id = flash_read_uchar (info,
1398 FLASH_OFFSET_MANUFACTURER_ID);
1399 info->device_id = flash_read_uchar (info,
1400 FLASH_OFFSET_DEVICE_ID);
1401 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1404 static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1406 info->cmd_reset = FLASH_CMD_RESET;
1408 cmdset_intel_read_jedec_ids(info);
1409 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1411 #ifdef CONFIG_SYS_FLASH_PROTECTION
1412 /* read legacy lock/unlock bit from intel flash */
1413 if (info->ext_addr) {
1414 info->legacy_unlock = flash_read_uchar (info,
1415 info->ext_addr + 5) & 0x08;
1422 static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1427 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1428 flash_unlock_seq(info, 0);
1429 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1430 udelay(1000); /* some flash are slow to respond */
1432 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1433 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1434 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1436 manuId = flash_read_uchar (info,
1437 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1439 info->manufacturer_id = manuId;
1441 switch (info->chipwidth){
1442 case FLASH_CFI_8BIT:
1443 info->device_id = flash_read_uchar (info,
1444 FLASH_OFFSET_DEVICE_ID);
1445 if (info->device_id == 0x7E) {
1446 /* AMD 3-byte (expanded) device ids */
1447 info->device_id2 = flash_read_uchar (info,
1448 FLASH_OFFSET_DEVICE_ID2);
1449 info->device_id2 <<= 8;
1450 info->device_id2 |= flash_read_uchar (info,
1451 FLASH_OFFSET_DEVICE_ID3);
1454 case FLASH_CFI_16BIT:
1455 info->device_id = flash_read_word (info,
1456 FLASH_OFFSET_DEVICE_ID);
1461 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1464 static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1466 info->cmd_reset = AMD_CMD_RESET;
1468 cmdset_amd_read_jedec_ids(info);
1469 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1474 #ifdef CONFIG_FLASH_CFI_LEGACY
1475 static void flash_read_jedec_ids (flash_info_t * info)
1477 info->manufacturer_id = 0;
1478 info->device_id = 0;
1479 info->device_id2 = 0;
1481 switch (info->vendor) {
1482 case CFI_CMDSET_INTEL_PROG_REGIONS:
1483 case CFI_CMDSET_INTEL_STANDARD:
1484 case CFI_CMDSET_INTEL_EXTENDED:
1485 cmdset_intel_read_jedec_ids(info);
1487 case CFI_CMDSET_AMD_STANDARD:
1488 case CFI_CMDSET_AMD_EXTENDED:
1489 cmdset_amd_read_jedec_ids(info);
1496 /*-----------------------------------------------------------------------
1497 * Call board code to request info about non-CFI flash.
1498 * board_flash_get_legacy needs to fill in at least:
1499 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1501 static int flash_detect_legacy(phys_addr_t base, int banknum)
1503 flash_info_t *info = &flash_info[banknum];
1505 if (board_flash_get_legacy(base, banknum, info)) {
1506 /* board code may have filled info completely. If not, we
1507 use JEDEC ID probing. */
1508 if (!info->vendor) {
1510 CFI_CMDSET_AMD_STANDARD,
1511 CFI_CMDSET_INTEL_STANDARD
1515 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1516 info->vendor = modes[i];
1518 (ulong)map_physmem(base,
1521 if (info->portwidth == FLASH_CFI_8BIT
1522 && info->interface == FLASH_CFI_X8X16) {
1523 info->addr_unlock1 = 0x2AAA;
1524 info->addr_unlock2 = 0x5555;
1526 info->addr_unlock1 = 0x5555;
1527 info->addr_unlock2 = 0x2AAA;
1529 flash_read_jedec_ids(info);
1530 debug("JEDEC PROBE: ID %x %x %x\n",
1531 info->manufacturer_id,
1534 if (jedec_flash_match(info, info->start[0]))
1537 unmap_physmem((void *)info->start[0],
1542 switch(info->vendor) {
1543 case CFI_CMDSET_INTEL_PROG_REGIONS:
1544 case CFI_CMDSET_INTEL_STANDARD:
1545 case CFI_CMDSET_INTEL_EXTENDED:
1546 info->cmd_reset = FLASH_CMD_RESET;
1548 case CFI_CMDSET_AMD_STANDARD:
1549 case CFI_CMDSET_AMD_EXTENDED:
1550 case CFI_CMDSET_AMD_LEGACY:
1551 info->cmd_reset = AMD_CMD_RESET;
1554 info->flash_id = FLASH_MAN_CFI;
1557 return 0; /* use CFI */
1560 static inline int flash_detect_legacy(phys_addr_t base, int banknum)
1562 return 0; /* use CFI */
1566 /*-----------------------------------------------------------------------
1567 * detect if flash is compatible with the Common Flash Interface (CFI)
1568 * http://www.jedec.org/download/search/jesd68.pdf
1570 static void flash_read_cfi (flash_info_t *info, void *buf,
1571 unsigned int start, size_t len)
1576 for (i = 0; i < len; i++)
1577 p[i] = flash_read_uchar(info, start + i);
1580 void __flash_cmd_reset(flash_info_t *info)
1583 * We do not yet know what kind of commandset to use, so we issue
1584 * the reset command in both Intel and AMD variants, in the hope
1585 * that AMD flash roms ignore the Intel command.
1587 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1588 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1590 void flash_cmd_reset(flash_info_t *info)
1591 __attribute__((weak,alias("__flash_cmd_reset")));
1593 static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1597 /* Issue FLASH reset command */
1598 flash_cmd_reset(info);
1601 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1603 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1605 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1606 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1607 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1608 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1609 sizeof(struct cfi_qry));
1610 info->interface = le16_to_cpu(qry->interface_desc);
1612 info->cfi_offset = flash_offset_cfi[cfi_offset];
1613 debug ("device interface is %d\n",
1615 debug ("found port %d chip %d ",
1616 info->portwidth, info->chipwidth);
1617 debug ("port %d bits chip %d bits\n",
1618 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1619 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1621 /* calculate command offsets as in the Linux driver */
1622 info->addr_unlock1 = 0x555;
1623 info->addr_unlock2 = 0x2aa;
1626 * modify the unlock address if we are
1627 * in compatibility mode
1629 if ( /* x8/x16 in x8 mode */
1630 ((info->chipwidth == FLASH_CFI_BY8) &&
1631 (info->interface == FLASH_CFI_X8X16)) ||
1632 /* x16/x32 in x16 mode */
1633 ((info->chipwidth == FLASH_CFI_BY16) &&
1634 (info->interface == FLASH_CFI_X16X32)))
1636 info->addr_unlock1 = 0xaaa;
1637 info->addr_unlock2 = 0x555;
1640 info->name = "CFI conformant";
1648 static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1650 debug ("flash detect cfi\n");
1652 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
1653 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1654 for (info->chipwidth = FLASH_CFI_BY8;
1655 info->chipwidth <= info->portwidth;
1656 info->chipwidth <<= 1)
1657 if (__flash_detect_cfi(info, qry))
1660 debug ("not found\n");
1665 * Manufacturer-specific quirks. Add workarounds for geometry
1666 * reversal, etc. here.
1668 static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1670 /* check if flash geometry needs reversal */
1671 if (qry->num_erase_regions > 1) {
1672 /* reverse geometry if top boot part */
1673 if (info->cfi_version < 0x3131) {
1674 /* CFI < 1.1, try to guess from device id */
1675 if ((info->device_id & 0x80) != 0)
1676 cfi_reverse_geometry(qry);
1677 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1678 /* CFI >= 1.1, deduct from top/bottom flag */
1679 /* note: ext_addr is valid since cfi_version > 0 */
1680 cfi_reverse_geometry(qry);
1685 static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1687 int reverse_geometry = 0;
1689 /* Check the "top boot" bit in the PRI */
1690 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1691 reverse_geometry = 1;
1693 /* AT49BV6416(T) list the erase regions in the wrong order.
1694 * However, the device ID is identical with the non-broken
1695 * AT49BV642D they differ in the high byte.
1697 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1698 reverse_geometry = !reverse_geometry;
1700 if (reverse_geometry)
1701 cfi_reverse_geometry(qry);
1704 static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1706 /* check if flash geometry needs reversal */
1707 if (qry->num_erase_regions > 1) {
1708 /* reverse geometry if top boot part */
1709 if (info->cfi_version < 0x3131) {
1710 /* CFI < 1.1, guess by device id (M29W320{DT,ET} only) */
1711 if (info->device_id == 0x22CA ||
1712 info->device_id == 0x2256) {
1713 cfi_reverse_geometry(qry);
1720 * The following code cannot be run from FLASH!
1723 ulong flash_get_size (phys_addr_t base, int banknum)
1725 flash_info_t *info = &flash_info[banknum];
1727 flash_sect_t sect_cnt;
1731 uchar num_erase_regions;
1732 int erase_region_size;
1733 int erase_region_count;
1736 memset(&qry, 0, sizeof(qry));
1739 info->cfi_version = 0;
1740 #ifdef CONFIG_SYS_FLASH_PROTECTION
1741 info->legacy_unlock = 0;
1744 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
1746 if (flash_detect_cfi (info, &qry)) {
1747 info->vendor = le16_to_cpu(qry.p_id);
1748 info->ext_addr = le16_to_cpu(qry.p_adr);
1749 num_erase_regions = qry.num_erase_regions;
1751 if (info->ext_addr) {
1752 info->cfi_version = (ushort) flash_read_uchar (info,
1753 info->ext_addr + 3) << 8;
1754 info->cfi_version |= (ushort) flash_read_uchar (info,
1755 info->ext_addr + 4);
1759 flash_printqry (&qry);
1762 switch (info->vendor) {
1763 case CFI_CMDSET_INTEL_PROG_REGIONS:
1764 case CFI_CMDSET_INTEL_STANDARD:
1765 case CFI_CMDSET_INTEL_EXTENDED:
1766 cmdset_intel_init(info, &qry);
1768 case CFI_CMDSET_AMD_STANDARD:
1769 case CFI_CMDSET_AMD_EXTENDED:
1770 cmdset_amd_init(info, &qry);
1773 printf("CFI: Unknown command set 0x%x\n",
1776 * Unfortunately, this means we don't know how
1777 * to get the chip back to Read mode. Might
1778 * as well try an Intel-style reset...
1780 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1784 /* Do manufacturer-specific fixups */
1785 switch (info->manufacturer_id) {
1787 flash_fixup_amd(info, &qry);
1790 flash_fixup_atmel(info, &qry);
1793 flash_fixup_stm(info, &qry);
1797 debug ("manufacturer is %d\n", info->vendor);
1798 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1799 debug ("device id is 0x%x\n", info->device_id);
1800 debug ("device id2 is 0x%x\n", info->device_id2);
1801 debug ("cfi version is 0x%04x\n", info->cfi_version);
1803 size_ratio = info->portwidth / info->chipwidth;
1804 /* if the chip is x8/x16 reduce the ratio by half */
1805 if ((info->interface == FLASH_CFI_X8X16)
1806 && (info->chipwidth == FLASH_CFI_BY8)) {
1809 debug ("size_ratio %d port %d bits chip %d bits\n",
1810 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1811 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1812 debug ("found %d erase regions\n", num_erase_regions);
1815 for (i = 0; i < num_erase_regions; i++) {
1816 if (i > NUM_ERASE_REGIONS) {
1817 printf ("%d erase regions found, only %d used\n",
1818 num_erase_regions, NUM_ERASE_REGIONS);
1822 tmp = le32_to_cpu(qry.erase_region_info[i]);
1823 debug("erase region %u: 0x%08lx\n", i, tmp);
1825 erase_region_count = (tmp & 0xffff) + 1;
1828 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
1829 debug ("erase_region_count = %d erase_region_size = %d\n",
1830 erase_region_count, erase_region_size);
1831 for (j = 0; j < erase_region_count; j++) {
1832 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
1833 printf("ERROR: too many flash sectors\n");
1836 info->start[sect_cnt] =
1837 (ulong)map_physmem(sector,
1840 sector += (erase_region_size * size_ratio);
1843 * Only read protection status from
1844 * supported devices (intel...)
1846 switch (info->vendor) {
1847 case CFI_CMDSET_INTEL_PROG_REGIONS:
1848 case CFI_CMDSET_INTEL_EXTENDED:
1849 case CFI_CMDSET_INTEL_STANDARD:
1850 info->protect[sect_cnt] =
1851 flash_isset (info, sect_cnt,
1852 FLASH_OFFSET_PROTECT,
1853 FLASH_STATUS_PROTECT);
1856 /* default: not protected */
1857 info->protect[sect_cnt] = 0;
1864 info->sector_count = sect_cnt;
1865 info->size = 1 << qry.dev_size;
1866 /* multiply the size by the number of chips */
1867 info->size *= size_ratio;
1868 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
1869 tmp = 1 << qry.block_erase_timeout_typ;
1870 info->erase_blk_tout = tmp *
1871 (1 << qry.block_erase_timeout_max);
1872 tmp = (1 << qry.buf_write_timeout_typ) *
1873 (1 << qry.buf_write_timeout_max);
1875 /* round up when converting to ms */
1876 info->buffer_write_tout = (tmp + 999) / 1000;
1877 tmp = (1 << qry.word_write_timeout_typ) *
1878 (1 << qry.word_write_timeout_max);
1879 /* round up when converting to ms */
1880 info->write_tout = (tmp + 999) / 1000;
1881 info->flash_id = FLASH_MAN_CFI;
1882 if ((info->interface == FLASH_CFI_X8X16) &&
1883 (info->chipwidth == FLASH_CFI_BY8)) {
1884 /* XXX - Need to test on x8/x16 in parallel. */
1885 info->portwidth >>= 1;
1888 flash_write_cmd (info, 0, 0, info->cmd_reset);
1891 return (info->size);
1894 void flash_set_verbose(uint v)
1899 /*-----------------------------------------------------------------------
1901 unsigned long flash_init (void)
1903 unsigned long size = 0;
1905 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
1909 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
1912 #ifdef CONFIG_SYS_FLASH_PROTECTION
1913 /* read environment from EEPROM */
1915 getenv_r ("unlock", s, sizeof(s));
1918 #define BANK_BASE(i) (((phys_addr_t [CFI_MAX_FLASH_BANKS])CONFIG_SYS_FLASH_BANKS_LIST)[i])
1920 /* Init: no FLASHes known */
1921 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
1922 flash_info[i].flash_id = FLASH_UNKNOWN;
1924 if (!flash_detect_legacy (BANK_BASE(i), i))
1925 flash_get_size (BANK_BASE(i), i);
1926 size += flash_info[i].size;
1927 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
1928 #ifndef CONFIG_SYS_FLASH_QUIET_TEST
1929 printf ("## Unknown FLASH on Bank %d "
1930 "- Size = 0x%08lx = %ld MB\n",
1931 i+1, flash_info[i].size,
1932 flash_info[i].size << 20);
1933 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
1935 #ifdef CONFIG_SYS_FLASH_PROTECTION
1936 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
1938 * Only the U-Boot image and it's environment
1939 * is protected, all other sectors are
1940 * unprotected (unlocked) if flash hardware
1941 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
1942 * and the environment variable "unlock" is
1945 if (flash_info[i].legacy_unlock) {
1949 * Disable legacy_unlock temporarily,
1950 * since flash_real_protect would
1951 * relock all other sectors again
1954 flash_info[i].legacy_unlock = 0;
1957 * Legacy unlocking (e.g. Intel J3) ->
1958 * unlock only one sector. This will
1959 * unlock all sectors.
1961 flash_real_protect (&flash_info[i], 0, 0);
1963 flash_info[i].legacy_unlock = 1;
1966 * Manually mark other sectors as
1967 * unlocked (unprotected)
1969 for (k = 1; k < flash_info[i].sector_count; k++)
1970 flash_info[i].protect[k] = 0;
1973 * No legancy unlocking -> unlock all sectors
1975 flash_protect (FLAG_PROTECT_CLEAR,
1976 flash_info[i].start[0],
1977 flash_info[i].start[0]
1978 + flash_info[i].size - 1,
1982 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1985 /* Monitor protection ON by default */
1986 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
1987 (!defined(CONFIG_MONITOR_IS_IN_RAM))
1988 flash_protect (FLAG_PROTECT_SET,
1989 CONFIG_SYS_MONITOR_BASE,
1990 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
1991 flash_get_info(CONFIG_SYS_MONITOR_BASE));
1994 /* Environment protection ON by default */
1995 #ifdef CONFIG_ENV_IS_IN_FLASH
1996 flash_protect (FLAG_PROTECT_SET,
1998 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
1999 flash_get_info(CONFIG_ENV_ADDR));
2002 /* Redundant environment protection ON by default */
2003 #ifdef CONFIG_ENV_ADDR_REDUND
2004 flash_protect (FLAG_PROTECT_SET,
2005 CONFIG_ENV_ADDR_REDUND,
2006 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2007 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2010 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2011 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
2012 debug("autoprotecting from %08x to %08x\n",
2013 apl[i].start, apl[i].start + apl[i].size - 1);
2014 flash_protect (FLAG_PROTECT_SET,
2016 apl[i].start + apl[i].size - 1,
2017 flash_get_info(apl[i].start));
2021 #ifdef CONFIG_FLASH_CFI_MTD