2 * (C) Copyright 2002-2004
5 * Copyright (C) 2003 Arabella Software Ltd.
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>
46 * This file implements a Common Flash Interface (CFI) driver for
49 * The width of the port and the width of the chips are determined at
50 * initialization. These widths are used to calculate the address for
51 * access CFI data structures.
54 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
55 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
56 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
57 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
58 * AMD CFI Specification, Release 2.0 December 1, 2001
59 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
60 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
62 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
63 * reading and writing ... (yes there is such a Hardware).
66 static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
67 #ifdef CONFIG_FLASH_CFI_MTD
68 static uint flash_verbose = 1;
70 #define flash_verbose 1
73 flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
76 * Check if chip width is defined. If not, start detecting with 8bit.
78 #ifndef CONFIG_SYS_FLASH_CFI_WIDTH
79 #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
83 * 0xffff is an undefined value for the configuration register. When
84 * this value is returned, the configuration register shall not be
85 * written at all (default mode).
87 static u16 cfi_flash_config_reg(int i)
89 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
90 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
96 #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
97 int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
100 static phys_addr_t __cfi_flash_bank_addr(int i)
102 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
104 phys_addr_t cfi_flash_bank_addr(int i)
105 __attribute__((weak, alias("__cfi_flash_bank_addr")));
107 static unsigned long __cfi_flash_bank_size(int i)
109 #ifdef CONFIG_SYS_FLASH_BANKS_SIZES
110 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
115 unsigned long cfi_flash_bank_size(int i)
116 __attribute__((weak, alias("__cfi_flash_bank_size")));
118 static void __flash_write8(u8 value, void *addr)
120 __raw_writeb(value, addr);
123 static void __flash_write16(u16 value, void *addr)
125 __raw_writew(value, addr);
128 static void __flash_write32(u32 value, void *addr)
130 __raw_writel(value, addr);
133 static void __flash_write64(u64 value, void *addr)
135 /* No architectures currently implement __raw_writeq() */
136 *(volatile u64 *)addr = value;
139 static u8 __flash_read8(void *addr)
141 return __raw_readb(addr);
144 static u16 __flash_read16(void *addr)
146 return __raw_readw(addr);
149 static u32 __flash_read32(void *addr)
151 return __raw_readl(addr);
154 static u64 __flash_read64(void *addr)
156 /* No architectures currently implement __raw_readq() */
157 return *(volatile u64 *)addr;
160 #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
161 void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
162 void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
163 void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
164 void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
165 u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
166 u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
167 u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
168 u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
170 #define flash_write8 __flash_write8
171 #define flash_write16 __flash_write16
172 #define flash_write32 __flash_write32
173 #define flash_write64 __flash_write64
174 #define flash_read8 __flash_read8
175 #define flash_read16 __flash_read16
176 #define flash_read32 __flash_read32
177 #define flash_read64 __flash_read64
180 /*-----------------------------------------------------------------------
182 #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
183 flash_info_t *flash_get_info(ulong base)
186 flash_info_t *info = NULL;
188 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
189 info = & flash_info[i];
190 if (info->size && info->start[0] <= base &&
191 base <= info->start[0] + info->size - 1)
199 unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
201 if (sect != (info->sector_count - 1))
202 return info->start[sect + 1] - info->start[sect];
204 return info->start[0] + info->size - info->start[sect];
207 /*-----------------------------------------------------------------------
208 * create an address based on the offset and the port width
211 flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
213 unsigned int byte_offset = offset * info->portwidth / info->chipwidth;
214 unsigned int addr = (info->start[sect] + byte_offset);
215 unsigned int mask = 0xffffffff << (info->portwidth - 1);
217 return (void *)(addr & mask);
220 static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
221 unsigned int offset, void *addr)
225 /*-----------------------------------------------------------------------
226 * make a proper sized command based on the port and chip widths
228 static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
233 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
234 u32 cmd_le = cpu_to_le32(cmd);
237 uchar *cp = (uchar *) cmdbuf;
239 for (i = info->portwidth; i > 0; i--){
240 cword_offset = (info->portwidth-i)%info->chipwidth;
241 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
242 cp_offset = info->portwidth - i;
243 val = *((uchar*)&cmd_le + cword_offset);
246 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
248 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
253 /*-----------------------------------------------------------------------
256 static void print_longlong (char *str, unsigned long long data)
262 for (i = 0; i < 8; i++)
263 sprintf (&str[i * 2], "%2.2x", *cp++);
266 static void flash_printqry (struct cfi_qry *qry)
271 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
273 for (y = 0; y < 16; y++)
274 debug("%2.2x ", p[x + y]);
276 for (y = 0; y < 16; y++) {
277 unsigned char c = p[x + y];
278 if (c >= 0x20 && c <= 0x7e)
289 /*-----------------------------------------------------------------------
290 * read a character at a port width address
292 static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
297 cp = flash_map (info, 0, offset);
298 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
299 retval = flash_read8(cp);
301 retval = flash_read8(cp + info->portwidth - 1);
303 flash_unmap (info, 0, offset, cp);
307 /*-----------------------------------------------------------------------
308 * read a word at a port width address, assume 16bit bus
310 static inline ushort flash_read_word (flash_info_t * info, uint offset)
312 ushort *addr, retval;
314 addr = flash_map (info, 0, offset);
315 retval = flash_read16 (addr);
316 flash_unmap (info, 0, offset, addr);
321 /*-----------------------------------------------------------------------
322 * read a long word by picking the least significant byte of each maximum
323 * port size word. Swap for ppc format.
325 static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
334 addr = flash_map (info, sect, offset);
337 debug ("long addr is at %p info->portwidth = %d\n", addr,
339 for (x = 0; x < 4 * info->portwidth; x++) {
340 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
343 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
344 retval = ((flash_read8(addr) << 16) |
345 (flash_read8(addr + info->portwidth) << 24) |
346 (flash_read8(addr + 2 * info->portwidth)) |
347 (flash_read8(addr + 3 * info->portwidth) << 8));
349 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
350 (flash_read8(addr + info->portwidth - 1) << 16) |
351 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
352 (flash_read8(addr + 3 * info->portwidth - 1)));
354 flash_unmap(info, sect, offset, addr);
360 * Write a proper sized command to the correct address
362 void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
363 uint offset, u32 cmd)
369 addr = flash_map (info, sect, offset);
370 flash_make_cmd (info, cmd, &cword);
371 switch (info->portwidth) {
373 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
374 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
375 flash_write8(cword.c, addr);
377 case FLASH_CFI_16BIT:
378 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
380 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
381 flash_write16(cword.w, addr);
383 case FLASH_CFI_32BIT:
384 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
386 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
387 flash_write32(cword.l, addr);
389 case FLASH_CFI_64BIT:
394 print_longlong (str, cword.ll);
396 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
398 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
401 flash_write64(cword.ll, addr);
404 printf("fwc: Unknown port width %d\n", info->portwidth);
407 /* Ensure all the instructions are fully finished */
410 flash_unmap(info, sect, offset, addr);
413 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
415 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
416 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
419 /*-----------------------------------------------------------------------
421 static int flash_isequal (flash_info_t * info, flash_sect_t sect,
422 uint offset, uchar cmd)
428 addr = flash_map (info, sect, offset);
429 flash_make_cmd (info, cmd, &cword);
431 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
432 switch (info->portwidth) {
434 debug ("is= %x %x\n", flash_read8(addr), cword.c);
435 retval = (flash_read8(addr) == cword.c);
437 case FLASH_CFI_16BIT:
438 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
439 retval = (flash_read16(addr) == cword.w);
441 case FLASH_CFI_32BIT:
442 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
443 retval = (flash_read32(addr) == cword.l);
445 case FLASH_CFI_64BIT:
451 print_longlong (str1, flash_read64(addr));
452 print_longlong (str2, cword.ll);
453 debug ("is= %s %s\n", str1, str2);
456 retval = (flash_read64(addr) == cword.ll);
462 flash_unmap(info, sect, offset, addr);
467 /*-----------------------------------------------------------------------
469 static int flash_isset (flash_info_t * info, flash_sect_t sect,
470 uint offset, uchar cmd)
476 addr = flash_map (info, sect, offset);
477 flash_make_cmd (info, cmd, &cword);
478 switch (info->portwidth) {
480 retval = ((flash_read8(addr) & cword.c) == cword.c);
482 case FLASH_CFI_16BIT:
483 retval = ((flash_read16(addr) & cword.w) == cword.w);
485 case FLASH_CFI_32BIT:
486 retval = ((flash_read32(addr) & cword.l) == cword.l);
488 case FLASH_CFI_64BIT:
489 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
495 flash_unmap(info, sect, offset, addr);
500 /*-----------------------------------------------------------------------
502 static int flash_toggle (flash_info_t * info, flash_sect_t sect,
503 uint offset, uchar cmd)
509 addr = flash_map (info, sect, offset);
510 flash_make_cmd (info, cmd, &cword);
511 switch (info->portwidth) {
513 retval = flash_read8(addr) != flash_read8(addr);
515 case FLASH_CFI_16BIT:
516 retval = flash_read16(addr) != flash_read16(addr);
518 case FLASH_CFI_32BIT:
519 retval = flash_read32(addr) != flash_read32(addr);
521 case FLASH_CFI_64BIT:
522 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
523 (flash_read32(addr+4) != flash_read32(addr+4)) );
529 flash_unmap(info, sect, offset, addr);
535 * flash_is_busy - check to see if the flash is busy
537 * This routine checks the status of the chip and returns true if the
540 static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
544 switch (info->vendor) {
545 case CFI_CMDSET_INTEL_PROG_REGIONS:
546 case CFI_CMDSET_INTEL_STANDARD:
547 case CFI_CMDSET_INTEL_EXTENDED:
548 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
550 case CFI_CMDSET_AMD_STANDARD:
551 case CFI_CMDSET_AMD_EXTENDED:
552 #ifdef CONFIG_FLASH_CFI_LEGACY
553 case CFI_CMDSET_AMD_LEGACY:
555 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
560 debug ("flash_is_busy: %d\n", retval);
564 /*-----------------------------------------------------------------------
565 * wait for XSR.7 to be set. Time out with an error if it does not.
566 * This routine does not set the flash to read-array mode.
568 static int flash_status_check (flash_info_t * info, flash_sect_t sector,
569 ulong tout, char *prompt)
573 #if CONFIG_SYS_HZ != 1000
574 if ((ulong)CONFIG_SYS_HZ > 100000)
575 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
577 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
580 /* Wait for command completion */
581 #ifdef CONFIG_SYS_LOW_RES_TIMER
584 start = get_timer (0);
586 while (flash_is_busy (info, sector)) {
587 if (get_timer (start) > tout) {
588 printf ("Flash %s timeout at address %lx data %lx\n",
589 prompt, info->start[sector],
590 flash_read_long (info, sector, 0));
591 flash_write_cmd (info, sector, 0, info->cmd_reset);
594 udelay (1); /* also triggers watchdog */
599 /*-----------------------------------------------------------------------
600 * Wait for XSR.7 to be set, if it times out print an error, otherwise
601 * do a full status check.
603 * This routine sets the flash to read-array mode.
605 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
606 ulong tout, char *prompt)
610 retcode = flash_status_check (info, sector, tout, prompt);
611 switch (info->vendor) {
612 case CFI_CMDSET_INTEL_PROG_REGIONS:
613 case CFI_CMDSET_INTEL_EXTENDED:
614 case CFI_CMDSET_INTEL_STANDARD:
615 if ((retcode != ERR_OK)
616 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
618 printf ("Flash %s error at address %lx\n", prompt,
619 info->start[sector]);
620 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
621 FLASH_STATUS_PSLBS)) {
622 puts ("Command Sequence Error.\n");
623 } else if (flash_isset (info, sector, 0,
624 FLASH_STATUS_ECLBS)) {
625 puts ("Block Erase Error.\n");
626 retcode = ERR_NOT_ERASED;
627 } else if (flash_isset (info, sector, 0,
628 FLASH_STATUS_PSLBS)) {
629 puts ("Locking Error\n");
631 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
632 puts ("Block locked.\n");
633 retcode = ERR_PROTECTED;
635 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
636 puts ("Vpp Low Error.\n");
638 flash_write_cmd (info, sector, 0, info->cmd_reset);
647 static int use_flash_status_poll(flash_info_t *info)
649 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
650 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
651 info->vendor == CFI_CMDSET_AMD_STANDARD)
657 static int flash_status_poll(flash_info_t *info, void *src, void *dst,
658 ulong tout, char *prompt)
660 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
664 #if CONFIG_SYS_HZ != 1000
665 if ((ulong)CONFIG_SYS_HZ > 100000)
666 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
668 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
671 /* Wait for command completion */
672 #ifdef CONFIG_SYS_LOW_RES_TIMER
675 start = get_timer(0);
678 switch (info->portwidth) {
680 ready = flash_read8(dst) == flash_read8(src);
682 case FLASH_CFI_16BIT:
683 ready = flash_read16(dst) == flash_read16(src);
685 case FLASH_CFI_32BIT:
686 ready = flash_read32(dst) == flash_read32(src);
688 case FLASH_CFI_64BIT:
689 ready = flash_read64(dst) == flash_read64(src);
697 if (get_timer(start) > tout) {
698 printf("Flash %s timeout at address %lx data %lx\n",
699 prompt, (ulong)dst, (ulong)flash_read8(dst));
702 udelay(1); /* also triggers watchdog */
704 #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
708 /*-----------------------------------------------------------------------
710 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
712 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
715 unsigned long long ll;
718 switch (info->portwidth) {
722 case FLASH_CFI_16BIT:
723 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
726 cword->w = (cword->w >> 8) | w;
728 cword->w = (cword->w << 8) | c;
731 case FLASH_CFI_32BIT:
732 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
735 cword->l = (cword->l >> 8) | l;
737 cword->l = (cword->l << 8) | c;
740 case FLASH_CFI_64BIT:
741 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
744 cword->ll = (cword->ll >> 8) | ll;
746 cword->ll = (cword->ll << 8) | c;
753 * Loop through the sector table starting from the previously found sector.
754 * Searches forwards or backwards, dependent on the passed address.
756 static flash_sect_t find_sector (flash_info_t * info, ulong addr)
758 static flash_sect_t saved_sector; /* previously found sector */
759 flash_sect_t sector = saved_sector;
761 while ((info->start[sector] < addr)
762 && (sector < info->sector_count - 1))
764 while ((info->start[sector] > addr) && (sector > 0))
766 * also decrements the sector in case of an overshot
771 saved_sector = sector;
775 /*-----------------------------------------------------------------------
777 static int flash_write_cfiword (flash_info_t * info, ulong dest,
780 void *dstaddr = (void *)dest;
782 flash_sect_t sect = 0;
785 /* Check if Flash is (sufficiently) erased */
786 switch (info->portwidth) {
788 debug("%s: 8-bit 0x%02x\n", __func__, cword.c);
789 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
791 case FLASH_CFI_16BIT:
792 debug("%s: 16-bit 0x%04x\n", __func__, cword.w);
793 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
795 case FLASH_CFI_32BIT:
796 debug("%s: 32-bit 0x%08lx\n", __func__, cword.l);
797 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
799 case FLASH_CFI_64BIT:
800 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
807 return ERR_NOT_ERASED;
809 /* Disable interrupts which might cause a timeout here */
810 flag = disable_interrupts ();
812 switch (info->vendor) {
813 case CFI_CMDSET_INTEL_PROG_REGIONS:
814 case CFI_CMDSET_INTEL_EXTENDED:
815 case CFI_CMDSET_INTEL_STANDARD:
816 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
817 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
819 case CFI_CMDSET_AMD_EXTENDED:
820 case CFI_CMDSET_AMD_STANDARD:
821 sect = find_sector(info, dest);
822 flash_unlock_seq (info, sect);
823 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
826 #ifdef CONFIG_FLASH_CFI_LEGACY
827 case CFI_CMDSET_AMD_LEGACY:
828 sect = find_sector(info, dest);
829 flash_unlock_seq (info, 0);
830 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
836 switch (info->portwidth) {
838 flash_write8(cword.c, dstaddr);
840 case FLASH_CFI_16BIT:
841 flash_write16(cword.w, dstaddr);
843 case FLASH_CFI_32BIT:
844 flash_write32(cword.l, dstaddr);
846 case FLASH_CFI_64BIT:
847 flash_write64(cword.ll, dstaddr);
851 /* re-enable interrupts if necessary */
853 enable_interrupts ();
856 sect = find_sector (info, dest);
858 if (use_flash_status_poll(info))
859 return flash_status_poll(info, &cword, dstaddr,
860 info->write_tout, "write");
862 return flash_full_status_check(info, sect,
863 info->write_tout, "write");
866 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
868 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
875 void *dst = (void *)dest;
882 switch (info->portwidth) {
886 case FLASH_CFI_16BIT:
889 case FLASH_CFI_32BIT:
892 case FLASH_CFI_64BIT:
902 while ((cnt-- > 0) && (flag == 1)) {
903 switch (info->portwidth) {
905 flag = ((flash_read8(dst2) & flash_read8(src)) ==
909 case FLASH_CFI_16BIT:
910 flag = ((flash_read16(dst2) & flash_read16(src)) ==
914 case FLASH_CFI_32BIT:
915 flag = ((flash_read32(dst2) & flash_read32(src)) ==
919 case FLASH_CFI_64BIT:
920 flag = ((flash_read64(dst2) & flash_read64(src)) ==
927 retcode = ERR_NOT_ERASED;
932 sector = find_sector (info, dest);
934 switch (info->vendor) {
935 case CFI_CMDSET_INTEL_PROG_REGIONS:
936 case CFI_CMDSET_INTEL_STANDARD:
937 case CFI_CMDSET_INTEL_EXTENDED:
938 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
939 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
940 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
941 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
942 flash_write_cmd (info, sector, 0, write_cmd);
943 retcode = flash_status_check (info, sector,
944 info->buffer_write_tout,
946 if (retcode == ERR_OK) {
947 /* reduce the number of loops by the width of
950 flash_write_cmd (info, sector, 0, cnt - 1);
952 switch (info->portwidth) {
954 flash_write8(flash_read8(src), dst);
957 case FLASH_CFI_16BIT:
958 flash_write16(flash_read16(src), dst);
961 case FLASH_CFI_32BIT:
962 flash_write32(flash_read32(src), dst);
965 case FLASH_CFI_64BIT:
966 flash_write64(flash_read64(src), dst);
974 flash_write_cmd (info, sector, 0,
975 FLASH_CMD_WRITE_BUFFER_CONFIRM);
976 retcode = flash_full_status_check (
977 info, sector, info->buffer_write_tout,
983 case CFI_CMDSET_AMD_STANDARD:
984 case CFI_CMDSET_AMD_EXTENDED:
985 flash_unlock_seq(info,0);
987 #ifdef CONFIG_FLASH_SPANSION_S29WS_N
988 offset = ((unsigned long)dst - info->start[sector]) >> shift;
990 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
992 flash_write_cmd(info, sector, offset, cnt - 1);
994 switch (info->portwidth) {
997 flash_write8(flash_read8(src), dst);
1001 case FLASH_CFI_16BIT:
1003 flash_write16(flash_read16(src), dst);
1007 case FLASH_CFI_32BIT:
1009 flash_write32(flash_read32(src), dst);
1013 case FLASH_CFI_64BIT:
1015 flash_write64(flash_read64(src), dst);
1020 retcode = ERR_INVAL;
1024 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1025 if (use_flash_status_poll(info))
1026 retcode = flash_status_poll(info, src - (1 << shift),
1028 info->buffer_write_tout,
1031 retcode = flash_full_status_check(info, sector,
1032 info->buffer_write_tout,
1037 debug ("Unknown Command Set\n");
1038 retcode = ERR_INVAL;
1045 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1048 /*-----------------------------------------------------------------------
1050 int flash_erase (flash_info_t * info, int s_first, int s_last)
1057 debug("%s: erasing sectors %d to %d\n", __func__, s_first, s_last);
1059 if (info->flash_id != FLASH_MAN_CFI) {
1060 puts ("Can't erase unknown flash type - aborted\n");
1063 if ((s_first < 0) || (s_first > s_last)) {
1064 puts ("- no sectors to erase\n");
1069 for (sect = s_first; sect <= s_last; ++sect) {
1070 if (info->protect[sect]) {
1075 printf ("- Warning: %d protected sectors will not be erased!\n",
1077 } else if (flash_verbose) {
1082 for (sect = s_first; sect <= s_last; sect++) {
1088 if (info->protect[sect] == 0) { /* not protected */
1089 #ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1096 * Check if whole sector is erased
1098 size = flash_sector_size(info, sect);
1100 flash = (u32 *)info->start[sect];
1101 /* divide by 4 for longword access */
1103 for (k = 0; k < size; k++) {
1104 if (flash_read32(flash++) != 0xffffffff) {
1115 switch (info->vendor) {
1116 case CFI_CMDSET_INTEL_PROG_REGIONS:
1117 case CFI_CMDSET_INTEL_STANDARD:
1118 case CFI_CMDSET_INTEL_EXTENDED:
1119 flash_write_cmd (info, sect, 0,
1120 FLASH_CMD_CLEAR_STATUS);
1121 flash_write_cmd (info, sect, 0,
1122 FLASH_CMD_BLOCK_ERASE);
1123 flash_write_cmd (info, sect, 0,
1124 FLASH_CMD_ERASE_CONFIRM);
1126 case CFI_CMDSET_AMD_STANDARD:
1127 case CFI_CMDSET_AMD_EXTENDED:
1128 flash_unlock_seq (info, sect);
1129 flash_write_cmd (info, sect,
1131 AMD_CMD_ERASE_START);
1132 flash_unlock_seq (info, sect);
1133 flash_write_cmd (info, sect, 0,
1134 info->cmd_erase_sector);
1136 #ifdef CONFIG_FLASH_CFI_LEGACY
1137 case CFI_CMDSET_AMD_LEGACY:
1138 flash_unlock_seq (info, 0);
1139 flash_write_cmd (info, 0, info->addr_unlock1,
1140 AMD_CMD_ERASE_START);
1141 flash_unlock_seq (info, 0);
1142 flash_write_cmd (info, sect, 0,
1143 AMD_CMD_ERASE_SECTOR);
1147 debug ("Unkown flash vendor %d\n",
1152 if (use_flash_status_poll(info)) {
1155 cword.ll = 0xffffffffffffffffULL;
1156 dest = flash_map(info, sect, 0);
1157 st = flash_status_poll(info, &cword, dest,
1158 info->erase_blk_tout, "erase");
1159 flash_unmap(info, sect, 0, dest);
1161 st = flash_full_status_check(info, sect,
1162 info->erase_blk_tout,
1166 else if (flash_verbose)
1169 debug("\nSector %d is protected.\n",
1170 info->protect[sect]);
1180 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1181 static int sector_erased(flash_info_t *info, int i)
1188 * Check if whole sector is erased
1190 size = flash_sector_size(info, i);
1191 flash = (u32 *)info->start[i];
1192 /* divide by 4 for longword access */
1195 for (k = 0; k < size; k++) {
1196 if (flash_read32(flash++) != 0xffffffff)
1197 return 0; /* not erased */
1200 return 1; /* erased */
1202 #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1204 void flash_print_info (flash_info_t * info)
1208 if (info->flash_id != FLASH_MAN_CFI) {
1209 puts ("missing or unknown FLASH type\n");
1213 printf ("%s flash (%d x %d)",
1215 (info->portwidth << 3), (info->chipwidth << 3));
1216 if (info->size < 1024*1024)
1217 printf (" Size: %ld kB in %d Sectors\n",
1218 info->size >> 10, info->sector_count);
1220 printf (" Size: %ld MB in %d Sectors\n",
1221 info->size >> 20, info->sector_count);
1223 switch (info->vendor) {
1224 case CFI_CMDSET_INTEL_PROG_REGIONS:
1225 printf ("Intel Prog Regions");
1227 case CFI_CMDSET_INTEL_STANDARD:
1228 printf ("Intel Standard");
1230 case CFI_CMDSET_INTEL_EXTENDED:
1231 printf ("Intel Extended");
1233 case CFI_CMDSET_AMD_STANDARD:
1234 printf ("AMD Standard");
1236 case CFI_CMDSET_AMD_EXTENDED:
1237 printf ("AMD Extended");
1239 #ifdef CONFIG_FLASH_CFI_LEGACY
1240 case CFI_CMDSET_AMD_LEGACY:
1241 printf ("AMD Legacy");
1245 printf ("Unknown (%d)", info->vendor);
1248 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1249 info->manufacturer_id);
1250 printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1252 if ((info->device_id & 0xff) == 0x7E) {
1253 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1256 if ((info->vendor == CFI_CMDSET_AMD_STANDARD) && (info->legacy_unlock))
1257 printf("\n Advanced Sector Protection (PPB) enabled");
1258 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1259 info->erase_blk_tout,
1261 if (info->buffer_size > 1) {
1262 printf (" Buffer write timeout: %ld ms, "
1263 "buffer size: %d bytes\n",
1264 info->buffer_write_tout,
1268 puts ("\n Sector Start Addresses:");
1269 for (i = 0; i < info->sector_count; ++i) {
1274 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1275 /* print empty and read-only info */
1276 printf (" %08lX %c %s ",
1278 sector_erased(info, i) ? 'E' : ' ',
1279 info->protect[i] ? "RO" : " ");
1280 #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
1281 printf (" %08lX %s ",
1283 info->protect[i] ? "RO" : " ");
1290 /*-----------------------------------------------------------------------
1291 * This is used in a few places in write_buf() to show programming
1292 * progress. Making it a function is nasty because it needs to do side
1293 * effect updates to digit and dots. Repeated code is nasty too, so
1294 * we define it once here.
1296 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1297 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
1298 if (flash_verbose) { \
1300 if ((scale > 0) && (dots <= 0)) { \
1301 if ((digit % 5) == 0) \
1302 printf ("%d", digit / 5); \
1310 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1313 /*-----------------------------------------------------------------------
1314 * Copy memory to flash, returns:
1317 * 2 - Flash not erased
1319 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
1326 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1329 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1330 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1335 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1337 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1338 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1339 CONFIG_FLASH_SHOW_PROGRESS);
1343 /* get lower aligned address */
1344 wp = (addr & ~(info->portwidth - 1));
1346 /* handle unaligned start */
1347 if ((aln = addr - wp) != 0) {
1350 for (i = 0; i < aln; ++i)
1351 flash_add_byte (info, &cword, flash_read8(p + i));
1353 for (; (i < info->portwidth) && (cnt > 0); i++) {
1354 flash_add_byte (info, &cword, *src++);
1357 for (; (cnt == 0) && (i < info->portwidth); ++i)
1358 flash_add_byte (info, &cword, flash_read8(p + i));
1360 rc = flash_write_cfiword (info, wp, cword);
1365 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1368 /* handle the aligned part */
1369 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1370 buffered_size = (info->portwidth / info->chipwidth);
1371 buffered_size *= info->buffer_size;
1372 while (cnt >= info->portwidth) {
1373 /* prohibit buffer write when buffer_size is 1 */
1374 if (info->buffer_size == 1) {
1376 for (i = 0; i < info->portwidth; i++)
1377 flash_add_byte (info, &cword, *src++);
1378 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1380 wp += info->portwidth;
1381 cnt -= info->portwidth;
1385 /* write buffer until next buffered_size aligned boundary */
1386 i = buffered_size - (wp % buffered_size);
1389 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1391 i -= i & (info->portwidth - 1);
1395 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1396 /* Only check every once in a while */
1397 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1401 while (cnt >= info->portwidth) {
1403 for (i = 0; i < info->portwidth; i++) {
1404 flash_add_byte (info, &cword, *src++);
1406 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1408 wp += info->portwidth;
1409 cnt -= info->portwidth;
1410 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
1411 /* Only check every once in a while */
1412 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1415 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1422 * handle unaligned tail bytes
1426 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
1427 flash_add_byte (info, &cword, *src++);
1430 for (; i < info->portwidth; ++i)
1431 flash_add_byte (info, &cword, flash_read8(p + i));
1433 return flash_write_cfiword (info, wp, cword);
1436 static inline int manufact_match(flash_info_t *info, u32 manu)
1438 return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
1441 /*-----------------------------------------------------------------------
1443 #ifdef CONFIG_SYS_FLASH_PROTECTION
1445 static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1447 if (manufact_match(info, INTEL_MANUFACT)
1448 && info->device_id == NUMONYX_256MBIT) {
1451 * "Numonyx Axcell P33/P30 Specification Update" :)
1453 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1454 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1457 * cmd must come before FLASH_CMD_PROTECT + 20us
1458 * Disable interrupts which might cause a timeout here.
1460 int flag = disable_interrupts();
1464 cmd = FLASH_CMD_PROTECT_SET;
1466 cmd = FLASH_CMD_PROTECT_CLEAR;
1467 flash_write_cmd(info, sector, 0,
1469 flash_write_cmd(info, sector, 0, cmd);
1470 /* re-enable interrupts if necessary */
1472 enable_interrupts();
1479 int flash_real_protect (flash_info_t * info, long sector, int prot)
1483 switch (info->vendor) {
1484 case CFI_CMDSET_INTEL_PROG_REGIONS:
1485 case CFI_CMDSET_INTEL_STANDARD:
1486 case CFI_CMDSET_INTEL_EXTENDED:
1487 if (!cfi_protect_bugfix(info, sector, prot)) {
1488 flash_write_cmd(info, sector, 0,
1489 FLASH_CMD_CLEAR_STATUS);
1490 flash_write_cmd(info, sector, 0,
1493 flash_write_cmd(info, sector, 0,
1494 FLASH_CMD_PROTECT_SET);
1496 flash_write_cmd(info, sector, 0,
1497 FLASH_CMD_PROTECT_CLEAR);
1501 case CFI_CMDSET_AMD_EXTENDED:
1502 case CFI_CMDSET_AMD_STANDARD:
1503 /* U-Boot only checks the first byte */
1504 if (manufact_match(info, ATM_MANUFACT)) {
1506 flash_unlock_seq (info, 0);
1507 flash_write_cmd (info, 0,
1509 ATM_CMD_SOFTLOCK_START);
1510 flash_unlock_seq (info, 0);
1511 flash_write_cmd (info, sector, 0,
1514 flash_write_cmd (info, 0,
1516 AMD_CMD_UNLOCK_START);
1517 if (info->device_id == ATM_ID_BV6416)
1518 flash_write_cmd (info, sector,
1519 0, ATM_CMD_UNLOCK_SECT);
1522 if (info->legacy_unlock) {
1523 int flag = disable_interrupts();
1526 flash_unlock_seq(info, 0);
1527 flash_write_cmd(info, 0, info->addr_unlock1,
1528 AMD_CMD_SET_PPB_ENTRY);
1529 lock_flag = flash_isset(info, sector, 0, 0x01);
1532 flash_write_cmd(info, sector, 0,
1533 AMD_CMD_PPB_LOCK_BC1);
1534 flash_write_cmd(info, sector, 0,
1535 AMD_CMD_PPB_LOCK_BC2);
1537 debug("sector %ld %slocked\n", sector,
1538 lock_flag ? "" : "already ");
1541 debug("unlock %ld\n", sector);
1542 flash_write_cmd(info, 0, 0,
1543 AMD_CMD_PPB_UNLOCK_BC1);
1544 flash_write_cmd(info, 0, 0,
1545 AMD_CMD_PPB_UNLOCK_BC2);
1547 debug("sector %ld %sunlocked\n", sector,
1548 !lock_flag ? "" : "already ");
1551 enable_interrupts();
1553 if (flash_status_check(info, sector,
1554 info->erase_blk_tout,
1555 prot ? "protect" : "unprotect"))
1556 printf("status check error\n");
1558 flash_write_cmd(info, 0, 0,
1559 AMD_CMD_SET_PPB_EXIT_BC1);
1560 flash_write_cmd(info, 0, 0,
1561 AMD_CMD_SET_PPB_EXIT_BC2);
1564 #ifdef CONFIG_FLASH_CFI_LEGACY
1565 case CFI_CMDSET_AMD_LEGACY:
1566 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1567 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1569 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1571 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1576 * Flash needs to be in status register read mode for
1577 * flash_full_status_check() to work correctly
1579 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
1581 flash_full_status_check (info, sector, info->erase_blk_tout,
1582 prot ? "protect" : "unprotect")) == 0) {
1584 info->protect[sector] = prot;
1587 * On some of Intel's flash chips (marked via legacy_unlock)
1588 * unprotect unprotects all locking.
1590 if ((prot == 0) && (info->legacy_unlock)) {
1593 for (i = 0; i < info->sector_count; i++) {
1594 if (info->protect[i])
1595 flash_real_protect (info, i, 1);
1602 /*-----------------------------------------------------------------------
1603 * flash_read_user_serial - read the OneTimeProgramming cells
1605 void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1612 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
1613 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1614 memcpy (dst, src + offset, len);
1615 flash_write_cmd (info, 0, 0, info->cmd_reset);
1617 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
1621 * flash_read_factory_serial - read the device Id from the protection area
1623 void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1628 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1629 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1630 memcpy (buffer, src + offset, len);
1631 flash_write_cmd (info, 0, 0, info->cmd_reset);
1633 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
1636 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1638 /*-----------------------------------------------------------------------
1639 * Reverse the order of the erase regions in the CFI QRY structure.
1640 * This is needed for chips that are either a) correctly detected as
1641 * top-boot, or b) buggy.
1643 static void cfi_reverse_geometry(struct cfi_qry *qry)
1648 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1649 tmp = qry->erase_region_info[i];
1650 qry->erase_region_info[i] = qry->erase_region_info[j];
1651 qry->erase_region_info[j] = tmp;
1655 /*-----------------------------------------------------------------------
1656 * read jedec ids from device and set corresponding fields in info struct
1658 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1661 static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1663 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1665 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1666 udelay(1000); /* some flash are slow to respond */
1667 info->manufacturer_id = flash_read_uchar (info,
1668 FLASH_OFFSET_MANUFACTURER_ID);
1669 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1670 flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
1671 flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
1672 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1675 static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1677 info->cmd_reset = FLASH_CMD_RESET;
1679 cmdset_intel_read_jedec_ids(info);
1680 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1682 #ifdef CONFIG_SYS_FLASH_PROTECTION
1683 /* read legacy lock/unlock bit from intel flash */
1684 if (info->ext_addr) {
1685 info->legacy_unlock = flash_read_uchar (info,
1686 info->ext_addr + 5) & 0x08;
1693 static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1698 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1699 flash_unlock_seq(info, 0);
1700 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1701 udelay(1000); /* some flash are slow to respond */
1703 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1704 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1705 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1707 manuId = flash_read_uchar (info,
1708 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1710 info->manufacturer_id = manuId;
1712 switch (info->chipwidth){
1713 case FLASH_CFI_8BIT:
1714 info->device_id = flash_read_uchar (info,
1715 FLASH_OFFSET_DEVICE_ID);
1716 if (info->device_id == 0x7E) {
1717 /* AMD 3-byte (expanded) device ids */
1718 info->device_id2 = flash_read_uchar (info,
1719 FLASH_OFFSET_DEVICE_ID2);
1720 info->device_id2 <<= 8;
1721 info->device_id2 |= flash_read_uchar (info,
1722 FLASH_OFFSET_DEVICE_ID3);
1725 case FLASH_CFI_16BIT:
1726 info->device_id = flash_read_word (info,
1727 FLASH_OFFSET_DEVICE_ID);
1728 if ((info->device_id & 0xff) == 0x7E) {
1729 /* AMD 3-byte (expanded) device ids */
1730 info->device_id2 = flash_read_uchar (info,
1731 FLASH_OFFSET_DEVICE_ID2);
1732 info->device_id2 <<= 8;
1733 info->device_id2 |= flash_read_uchar (info,
1734 FLASH_OFFSET_DEVICE_ID3);
1740 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1744 static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1746 info->cmd_reset = AMD_CMD_RESET;
1747 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
1749 cmdset_amd_read_jedec_ids(info);
1750 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1752 #ifdef CONFIG_SYS_FLASH_PROTECTION
1753 if (info->ext_addr) {
1754 /* read sector protect/unprotect scheme (at 0x49) */
1755 if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
1756 info->legacy_unlock = 1;
1763 #ifdef CONFIG_FLASH_CFI_LEGACY
1764 static void flash_read_jedec_ids (flash_info_t * info)
1766 info->manufacturer_id = 0;
1767 info->device_id = 0;
1768 info->device_id2 = 0;
1770 switch (info->vendor) {
1771 case CFI_CMDSET_INTEL_PROG_REGIONS:
1772 case CFI_CMDSET_INTEL_STANDARD:
1773 case CFI_CMDSET_INTEL_EXTENDED:
1774 cmdset_intel_read_jedec_ids(info);
1776 case CFI_CMDSET_AMD_STANDARD:
1777 case CFI_CMDSET_AMD_EXTENDED:
1778 cmdset_amd_read_jedec_ids(info);
1785 /*-----------------------------------------------------------------------
1786 * Call board code to request info about non-CFI flash.
1787 * board_flash_get_legacy needs to fill in at least:
1788 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1790 static int flash_detect_legacy(phys_addr_t base, int banknum)
1792 flash_info_t *info = &flash_info[banknum];
1794 if (board_flash_get_legacy(base, banknum, info)) {
1795 /* board code may have filled info completely. If not, we
1796 use JEDEC ID probing. */
1797 if (!info->vendor) {
1799 CFI_CMDSET_AMD_STANDARD,
1800 CFI_CMDSET_INTEL_STANDARD
1804 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1805 info->vendor = modes[i];
1807 (ulong)map_physmem(base,
1810 if (info->portwidth == FLASH_CFI_8BIT
1811 && info->interface == FLASH_CFI_X8X16) {
1812 info->addr_unlock1 = 0x2AAA;
1813 info->addr_unlock2 = 0x5555;
1815 info->addr_unlock1 = 0x5555;
1816 info->addr_unlock2 = 0x2AAA;
1818 flash_read_jedec_ids(info);
1819 debug("JEDEC PROBE: ID %x %x %x\n",
1820 info->manufacturer_id,
1823 if (jedec_flash_match(info, info->start[0]))
1826 unmap_physmem((void *)info->start[0],
1831 switch(info->vendor) {
1832 case CFI_CMDSET_INTEL_PROG_REGIONS:
1833 case CFI_CMDSET_INTEL_STANDARD:
1834 case CFI_CMDSET_INTEL_EXTENDED:
1835 info->cmd_reset = FLASH_CMD_RESET;
1837 case CFI_CMDSET_AMD_STANDARD:
1838 case CFI_CMDSET_AMD_EXTENDED:
1839 case CFI_CMDSET_AMD_LEGACY:
1840 info->cmd_reset = AMD_CMD_RESET;
1843 info->flash_id = FLASH_MAN_CFI;
1846 return 0; /* use CFI */
1849 static inline int flash_detect_legacy(phys_addr_t base, int banknum)
1851 return 0; /* use CFI */
1855 /*-----------------------------------------------------------------------
1856 * detect if flash is compatible with the Common Flash Interface (CFI)
1857 * http://www.jedec.org/download/search/jesd68.pdf
1859 static void flash_read_cfi (flash_info_t *info, void *buf,
1860 unsigned int start, size_t len)
1865 for (i = 0; i < len; i++)
1866 p[i] = flash_read_uchar(info, start + (i * 2));
1869 static void __flash_cmd_reset(flash_info_t *info)
1872 * We do not yet know what kind of commandset to use, so we issue
1873 * the reset command in both Intel and AMD variants, in the hope
1874 * that AMD flash roms ignore the Intel command.
1876 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1878 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1880 void flash_cmd_reset(flash_info_t *info)
1881 __attribute__((weak,alias("__flash_cmd_reset")));
1883 static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1888 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1890 /* Issue FLASH reset command */
1891 flash_cmd_reset(info);
1892 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1894 if (flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP, 'Q') &&
1895 flash_isequal(info, 0,
1896 FLASH_OFFSET_CFI_RESP + 2, 'R') &&
1897 flash_isequal(info, 0,
1898 FLASH_OFFSET_CFI_RESP + 4, 'Y')) {
1899 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1900 sizeof(struct cfi_qry));
1901 #ifdef CONFIG_SYS_FLASH_INTERFACE_WIDTH
1902 info->interface = CONFIG_SYS_FLASH_INTERFACE_WIDTH;
1904 info->interface = le16_to_cpu(qry->interface_desc);
1905 /* Some flash chips can support multiple bus widths.
1906 * In this case, override the interface width and
1907 * limit it to the port width.
1909 if ((info->interface == FLASH_CFI_X8X16) &&
1910 (info->portwidth == FLASH_CFI_8BIT)) {
1911 debug("Overriding 16-bit interface"
1912 " width to 8-bit port width.\n");
1913 info->interface = FLASH_CFI_X8;
1914 } else if ((info->interface == FLASH_CFI_X16X32) &&
1915 (info->portwidth == FLASH_CFI_16BIT)) {
1916 debug("Overriding 16-bit interface"
1917 " width to 16-bit port width.\n");
1918 info->interface = FLASH_CFI_X16;
1921 info->cfi_offset = flash_offset_cfi[cfi_offset];
1922 debug ("device interface is %d\n",
1924 debug ("found port %d chip %d ",
1925 info->portwidth, info->chipwidth);
1926 debug ("port %d bits chip %d bits\n",
1927 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1928 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1930 /* calculate command offsets as in the Linux driver */
1931 info->addr_unlock1 = 0xaaa;
1932 info->addr_unlock2 = 0x555;
1935 * modify the unlock address if we are
1936 * in compatibility mode
1938 if ( /* x8/x16 in x8 mode */
1939 ((info->chipwidth == FLASH_CFI_BY8) &&
1940 (info->interface == FLASH_CFI_X8X16)) ||
1941 /* x16/x32 in x16 mode */
1942 ((info->chipwidth == FLASH_CFI_BY16) &&
1943 (info->interface == FLASH_CFI_X16X32)))
1945 info->addr_unlock1 = 0xaaa;
1946 info->addr_unlock2 = 0x555;
1949 info->name = "CFI conformant";
1957 static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1959 debug ("flash detect cfi\n");
1961 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
1962 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1963 for (info->chipwidth = FLASH_CFI_BY8;
1964 info->chipwidth <= info->portwidth;
1965 info->chipwidth <<= 1)
1966 if (__flash_detect_cfi(info, qry)) {
1967 debug("Found CFI flash, portwidth %d,"
1969 info->portwidth, info->chipwidth);
1973 debug ("not found\n");
1978 * Manufacturer-specific quirks. Add workarounds for geometry
1979 * reversal, etc. here.
1981 static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1983 /* check if flash geometry needs reversal */
1984 if (qry->num_erase_regions > 1) {
1985 /* reverse geometry if top boot part */
1986 if (info->cfi_version < 0x3131) {
1987 /* CFI < 1.1, try to guess from device id */
1988 if ((info->device_id & 0x80) != 0)
1989 cfi_reverse_geometry(qry);
1990 } else if (flash_read_uchar(info, info->ext_addr + 0x1e) == 3) {
1991 /* CFI >= 1.1, deduct from top/bottom flag */
1992 /* note: ext_addr is valid since cfi_version > 0 */
1993 cfi_reverse_geometry(qry);
1998 static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
2000 int reverse_geometry = 0;
2002 /* Check the "top boot" bit in the PRI */
2003 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
2004 reverse_geometry = 1;
2006 /* AT49BV6416(T) list the erase regions in the wrong order.
2007 * However, the device ID is identical with the non-broken
2008 * AT49BV642D they differ in the high byte.
2010 if (info->device_id == 0xd6 || info->device_id == 0xd2)
2011 reverse_geometry = !reverse_geometry;
2013 if (reverse_geometry)
2014 cfi_reverse_geometry(qry);
2017 static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
2019 /* check if flash geometry needs reversal */
2020 if (qry->num_erase_regions > 1) {
2021 /* reverse geometry if top boot part */
2022 if (info->cfi_version < 0x3131) {
2023 /* CFI < 1.1, guess by device id */
2024 if (info->device_id == 0x22CA || /* M29W320DT */
2025 info->device_id == 0x2256 || /* M29W320ET */
2026 info->device_id == 0x22D7) { /* M29W800DT */
2027 cfi_reverse_geometry(qry);
2029 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
2030 /* CFI >= 1.1, deduct from top/bottom flag */
2031 /* note: ext_addr is valid since cfi_version > 0 */
2032 cfi_reverse_geometry(qry);
2037 static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
2040 * SST, for many recent nor parallel flashes, says they are
2041 * CFI-conformant. This is not true, since qry struct.
2042 * reports a std. AMD command set (0x0002), while SST allows to
2043 * erase two different sector sizes for the same memory.
2044 * 64KB sector (SST call it block) needs 0x30 to be erased.
2045 * 4KB sector (SST call it sector) needs 0x50 to be erased.
2046 * Since CFI query detect the 4KB number of sectors, users expects
2047 * a sector granularity of 4KB, and it is here set.
2049 if (info->device_id == 0x5D23 || /* SST39VF3201B */
2050 info->device_id == 0x5C23) { /* SST39VF3202B */
2051 /* set sector granularity to 4KB */
2052 info->cmd_erase_sector=0x50;
2057 * The following code cannot be run from FLASH!
2060 ulong flash_get_size (phys_addr_t base, int banknum)
2062 flash_info_t *info = &flash_info[banknum];
2064 flash_sect_t sect_cnt;
2068 uchar num_erase_regions;
2069 int erase_region_size;
2070 int erase_region_count;
2072 unsigned long max_size;
2074 memset(&qry, 0, sizeof(qry));
2077 info->cfi_version = 0;
2078 #ifdef CONFIG_SYS_FLASH_PROTECTION
2079 info->legacy_unlock = 0;
2082 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
2084 if (flash_detect_cfi (info, &qry)) {
2085 info->vendor = le16_to_cpu(qry.p_id);
2086 info->ext_addr = le16_to_cpu(qry.p_adr) * 2;
2087 debug("extended address is 0x%x\n", info->ext_addr);
2088 num_erase_regions = qry.num_erase_regions;
2090 if (info->ext_addr) {
2091 info->cfi_version = (ushort) flash_read_uchar (info,
2092 info->ext_addr + 6) << 8;
2093 info->cfi_version |= (ushort) flash_read_uchar (info,
2094 info->ext_addr + 8);
2098 flash_printqry (&qry);
2101 switch (info->vendor) {
2102 case CFI_CMDSET_INTEL_PROG_REGIONS:
2103 case CFI_CMDSET_INTEL_STANDARD:
2104 case CFI_CMDSET_INTEL_EXTENDED:
2105 cmdset_intel_init(info, &qry);
2107 case CFI_CMDSET_AMD_STANDARD:
2108 case CFI_CMDSET_AMD_EXTENDED:
2109 cmdset_amd_init(info, &qry);
2112 printf("CFI: Unknown command set 0x%x\n",
2115 * Unfortunately, this means we don't know how
2116 * to get the chip back to Read mode. Might
2117 * as well try an Intel-style reset...
2119 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2123 /* Do manufacturer-specific fixups */
2124 switch (info->manufacturer_id) {
2125 case 0x0001: /* AMD */
2126 case 0x0037: /* AMIC */
2127 flash_fixup_amd(info, &qry);
2130 flash_fixup_atmel(info, &qry);
2133 flash_fixup_stm(info, &qry);
2135 case 0x00bf: /* SST */
2136 flash_fixup_sst(info, &qry);
2140 debug ("manufacturer is %d\n", info->vendor);
2141 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
2142 debug ("device id is 0x%x\n", info->device_id);
2143 debug ("device id2 is 0x%x\n", info->device_id2);
2144 debug ("cfi version is 0x%04x\n", info->cfi_version);
2145 debug("port width: %d, chipwidth: %d, interface: %d\n",
2146 info->portwidth, info->chipwidth, info->interface);
2148 size_ratio = info->portwidth / info->chipwidth;
2149 /* if the chip is x8/x16 reduce the ratio by half */
2150 if ((info->interface == FLASH_CFI_X8X16)
2151 && (info->chipwidth == FLASH_CFI_BY8)) {
2154 debug ("size_ratio %d port %d bits chip %d bits\n",
2155 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2156 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
2157 info->size = 1 << qry.dev_size;
2158 /* multiply the size by the number of chips */
2159 info->size *= size_ratio;
2160 max_size = cfi_flash_bank_size(banknum);
2161 if (max_size && (info->size > max_size)) {
2162 debug("[truncated from %ldMiB]", info->size >> 20);
2163 info->size = max_size;
2165 debug ("found %d erase regions\n", num_erase_regions);
2168 for (i = 0; i < num_erase_regions; i++) {
2169 if (i > NUM_ERASE_REGIONS) {
2170 printf ("%d erase regions found, only %d used\n",
2171 num_erase_regions, NUM_ERASE_REGIONS);
2175 tmp = le32_to_cpu(qry.erase_region_info[i]);
2176 debug("erase region %u: 0x%08lx\n", i, tmp);
2178 erase_region_count = (tmp & 0xffff) + 1;
2181 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
2182 debug ("erase_region_count = %d erase_region_size = %d\n",
2183 erase_region_count, erase_region_size);
2184 for (j = 0; j < erase_region_count; j++) {
2185 if (sector - base >= info->size)
2187 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
2188 printf("ERROR: too many flash sectors\n");
2191 info->start[sect_cnt] =
2192 (ulong)map_physmem(sector,
2195 sector += (erase_region_size * size_ratio);
2198 * Only read protection status from
2199 * supported devices (intel...)
2201 switch (info->vendor) {
2202 case CFI_CMDSET_INTEL_PROG_REGIONS:
2203 case CFI_CMDSET_INTEL_EXTENDED:
2204 case CFI_CMDSET_INTEL_STANDARD:
2206 * Set flash to read-id mode. Otherwise
2207 * reading protected status is not
2210 flash_write_cmd(info, sect_cnt, 0,
2212 info->protect[sect_cnt] =
2213 flash_isset (info, sect_cnt,
2214 FLASH_OFFSET_PROTECT,
2215 FLASH_STATUS_PROTECT);
2217 case CFI_CMDSET_AMD_EXTENDED:
2218 case CFI_CMDSET_AMD_STANDARD:
2219 if (!info->legacy_unlock) {
2220 /* default: not protected */
2221 info->protect[sect_cnt] = 0;
2225 /* Read protection (PPB) from sector */
2226 flash_write_cmd(info, 0, 0,
2228 flash_unlock_seq(info, 0);
2229 flash_write_cmd(info, 0,
2232 info->protect[sect_cnt] =
2235 FLASH_OFFSET_PROTECT,
2236 FLASH_STATUS_PROTECT);
2239 /* default: not protected */
2240 info->protect[sect_cnt] = 0;
2247 info->sector_count = sect_cnt;
2248 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2249 tmp = 1 << qry.block_erase_timeout_typ;
2250 info->erase_blk_tout = tmp *
2251 (1 << qry.block_erase_timeout_max);
2252 tmp = (1 << qry.buf_write_timeout_typ) *
2253 (1 << qry.buf_write_timeout_max);
2255 /* round up when converting to ms */
2256 info->buffer_write_tout = (tmp + 999) / 1000;
2257 tmp = (1 << qry.word_write_timeout_typ) *
2258 (1 << qry.word_write_timeout_max);
2259 /* round up when converting to ms */
2260 info->write_tout = (tmp + 999) / 1000;
2261 info->flash_id = FLASH_MAN_CFI;
2262 if ((info->interface == FLASH_CFI_X8X16) &&
2263 (info->chipwidth == FLASH_CFI_BY8)) {
2264 /* XXX - Need to test on x8/x16 in parallel. */
2265 info->portwidth >>= 1;
2268 flash_write_cmd (info, 0, 0, info->cmd_reset);
2271 return (info->size);
2274 #ifdef CONFIG_FLASH_CFI_MTD
2275 void flash_set_verbose(uint v)
2281 static void cfi_flash_set_config_reg(u32 base, u16 val)
2283 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2285 * Only set this config register if really defined
2286 * to a valid value (0xffff is invalid)
2292 * Set configuration register. Data is "encrypted" in the 16 lower
2295 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2296 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2299 * Finally issue reset-command to bring device back to
2302 flash_write16(FLASH_CMD_RESET, (void *)base);
2306 /*-----------------------------------------------------------------------
2309 void flash_protect_default(void)
2311 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2316 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2319 /* Monitor protection ON by default */
2320 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2321 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2322 flash_protect(FLAG_PROTECT_SET,
2323 CONFIG_SYS_MONITOR_BASE,
2324 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2325 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2328 /* Environment protection ON by default */
2329 #ifdef CONFIG_ENV_IS_IN_FLASH
2330 flash_protect(FLAG_PROTECT_SET,
2332 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2333 flash_get_info(CONFIG_ENV_ADDR));
2336 /* Redundant environment protection ON by default */
2337 #ifdef CONFIG_ENV_ADDR_REDUND
2338 flash_protect(FLAG_PROTECT_SET,
2339 CONFIG_ENV_ADDR_REDUND,
2340 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2341 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2344 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2345 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
2346 debug("autoprotecting from %08lx to %08lx\n",
2347 apl[i].start, apl[i].start + apl[i].size - 1);
2348 flash_protect(FLAG_PROTECT_SET,
2350 apl[i].start + apl[i].size - 1,
2351 flash_get_info(apl[i].start));
2356 unsigned long flash_init (void)
2358 unsigned long size = 0;
2361 #ifdef CONFIG_SYS_FLASH_PROTECTION
2362 /* read environment from EEPROM */
2364 getenv_f("unlock", s, sizeof(s));
2367 /* Init: no FLASHes known */
2368 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
2369 flash_info[i].flash_id = FLASH_UNKNOWN;
2371 /* Optionally write flash configuration register */
2372 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2373 cfi_flash_config_reg(i));
2375 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
2376 flash_get_size(cfi_flash_bank_addr(i), i);
2377 size += flash_info[i].size;
2378 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
2379 #ifndef CONFIG_SYS_FLASH_QUIET_TEST
2380 printf ("## Unknown flash on Bank %d "
2381 "- Size = 0x%08lx = %ld MB\n",
2382 i+1, flash_info[i].size,
2383 flash_info[i].size >> 20);
2384 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
2386 #ifdef CONFIG_SYS_FLASH_PROTECTION
2387 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
2389 * Only the U-Boot image and it's environment
2390 * is protected, all other sectors are
2391 * unprotected (unlocked) if flash hardware
2392 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
2393 * and the environment variable "unlock" is
2396 if (flash_info[i].legacy_unlock) {
2400 * Disable legacy_unlock temporarily,
2401 * since flash_real_protect would
2402 * relock all other sectors again
2405 flash_info[i].legacy_unlock = 0;
2408 * Legacy unlocking (e.g. Intel J3) ->
2409 * unlock only one sector. This will
2410 * unlock all sectors.
2412 flash_real_protect (&flash_info[i], 0, 0);
2414 flash_info[i].legacy_unlock = 1;
2417 * Manually mark other sectors as
2418 * unlocked (unprotected)
2420 for (k = 1; k < flash_info[i].sector_count; k++)
2421 flash_info[i].protect[k] = 0;
2424 * No legancy unlocking -> unlock all sectors
2426 flash_protect (FLAG_PROTECT_CLEAR,
2427 flash_info[i].start[0],
2428 flash_info[i].start[0]
2429 + flash_info[i].size - 1,
2433 #endif /* CONFIG_SYS_FLASH_PROTECTION */
2436 flash_protect_default();
2437 #ifdef CONFIG_FLASH_CFI_MTD