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>
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;
215 return (void *)(info->start[sect] + byte_offset);
218 static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
219 unsigned int offset, void *addr)
223 /*-----------------------------------------------------------------------
224 * make a proper sized command based on the port and chip widths
226 static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
231 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
232 u32 cmd_le = cpu_to_le32(cmd);
235 uchar *cp = (uchar *) cmdbuf;
237 for (i = info->portwidth; i > 0; i--){
238 cword_offset = (info->portwidth-i)%info->chipwidth;
239 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
240 cp_offset = info->portwidth - i;
241 val = *((uchar*)&cmd_le + cword_offset);
244 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
246 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
251 /*-----------------------------------------------------------------------
254 static void print_longlong (char *str, unsigned long long data)
260 for (i = 0; i < 8; i++)
261 sprintf (&str[i * 2], "%2.2x", *cp++);
264 static void flash_printqry (struct cfi_qry *qry)
269 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
271 for (y = 0; y < 16; y++)
272 debug("%2.2x ", p[x + y]);
274 for (y = 0; y < 16; y++) {
275 unsigned char c = p[x + y];
276 if (c >= 0x20 && c <= 0x7e)
287 /*-----------------------------------------------------------------------
288 * read a character at a port width address
290 static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
295 cp = flash_map (info, 0, offset);
296 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
297 retval = flash_read8(cp);
299 retval = flash_read8(cp + info->portwidth - 1);
301 flash_unmap (info, 0, offset, cp);
305 /*-----------------------------------------------------------------------
306 * read a word at a port width address, assume 16bit bus
308 static inline ushort flash_read_word (flash_info_t * info, uint offset)
310 ushort *addr, retval;
312 addr = flash_map (info, 0, offset);
313 retval = flash_read16 (addr);
314 flash_unmap (info, 0, offset, addr);
319 /*-----------------------------------------------------------------------
320 * read a long word by picking the least significant byte of each maximum
321 * port size word. Swap for ppc format.
323 static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
332 addr = flash_map (info, sect, offset);
335 debug ("long addr is at %p info->portwidth = %d\n", addr,
337 for (x = 0; x < 4 * info->portwidth; x++) {
338 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
341 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
342 retval = ((flash_read8(addr) << 16) |
343 (flash_read8(addr + info->portwidth) << 24) |
344 (flash_read8(addr + 2 * info->portwidth)) |
345 (flash_read8(addr + 3 * info->portwidth) << 8));
347 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
348 (flash_read8(addr + info->portwidth - 1) << 16) |
349 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
350 (flash_read8(addr + 3 * info->portwidth - 1)));
352 flash_unmap(info, sect, offset, addr);
358 * Write a proper sized command to the correct address
360 void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
361 uint offset, u32 cmd)
367 addr = flash_map (info, sect, offset);
368 flash_make_cmd (info, cmd, &cword);
369 switch (info->portwidth) {
371 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
372 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
373 flash_write8(cword.c, addr);
375 case FLASH_CFI_16BIT:
376 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
378 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
379 flash_write16(cword.w, addr);
381 case FLASH_CFI_32BIT:
382 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
384 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
385 flash_write32(cword.l, addr);
387 case FLASH_CFI_64BIT:
392 print_longlong (str, cword.ll);
394 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
396 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
399 flash_write64(cword.ll, addr);
403 /* Ensure all the instructions are fully finished */
406 flash_unmap(info, sect, offset, addr);
409 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
411 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
412 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
415 /*-----------------------------------------------------------------------
417 static int flash_isequal (flash_info_t * info, flash_sect_t sect,
418 uint offset, uchar cmd)
424 addr = flash_map (info, sect, offset);
425 flash_make_cmd (info, cmd, &cword);
427 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
428 switch (info->portwidth) {
430 debug ("is= %x %x\n", flash_read8(addr), cword.c);
431 retval = (flash_read8(addr) == cword.c);
433 case FLASH_CFI_16BIT:
434 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
435 retval = (flash_read16(addr) == cword.w);
437 case FLASH_CFI_32BIT:
438 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
439 retval = (flash_read32(addr) == cword.l);
441 case FLASH_CFI_64BIT:
447 print_longlong (str1, flash_read64(addr));
448 print_longlong (str2, cword.ll);
449 debug ("is= %s %s\n", str1, str2);
452 retval = (flash_read64(addr) == cword.ll);
458 flash_unmap(info, sect, offset, addr);
463 /*-----------------------------------------------------------------------
465 static int flash_isset (flash_info_t * info, flash_sect_t sect,
466 uint offset, uchar cmd)
472 addr = flash_map (info, sect, offset);
473 flash_make_cmd (info, cmd, &cword);
474 switch (info->portwidth) {
476 retval = ((flash_read8(addr) & cword.c) == cword.c);
478 case FLASH_CFI_16BIT:
479 retval = ((flash_read16(addr) & cword.w) == cword.w);
481 case FLASH_CFI_32BIT:
482 retval = ((flash_read32(addr) & cword.l) == cword.l);
484 case FLASH_CFI_64BIT:
485 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
491 flash_unmap(info, sect, offset, addr);
496 /*-----------------------------------------------------------------------
498 static int flash_toggle (flash_info_t * info, flash_sect_t sect,
499 uint offset, uchar cmd)
505 addr = flash_map (info, sect, offset);
506 flash_make_cmd (info, cmd, &cword);
507 switch (info->portwidth) {
509 retval = flash_read8(addr) != flash_read8(addr);
511 case FLASH_CFI_16BIT:
512 retval = flash_read16(addr) != flash_read16(addr);
514 case FLASH_CFI_32BIT:
515 retval = flash_read32(addr) != flash_read32(addr);
517 case FLASH_CFI_64BIT:
518 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
519 (flash_read32(addr+4) != flash_read32(addr+4)) );
525 flash_unmap(info, sect, offset, addr);
531 * flash_is_busy - check to see if the flash is busy
533 * This routine checks the status of the chip and returns true if the
536 static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
540 switch (info->vendor) {
541 case CFI_CMDSET_INTEL_PROG_REGIONS:
542 case CFI_CMDSET_INTEL_STANDARD:
543 case CFI_CMDSET_INTEL_EXTENDED:
544 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
546 case CFI_CMDSET_AMD_STANDARD:
547 case CFI_CMDSET_AMD_EXTENDED:
548 #ifdef CONFIG_FLASH_CFI_LEGACY
549 case CFI_CMDSET_AMD_LEGACY:
551 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
556 debug ("flash_is_busy: %d\n", retval);
560 /*-----------------------------------------------------------------------
561 * wait for XSR.7 to be set. Time out with an error if it does not.
562 * This routine does not set the flash to read-array mode.
564 static int flash_status_check (flash_info_t * info, flash_sect_t sector,
565 ulong tout, char *prompt)
569 #if CONFIG_SYS_HZ != 1000
570 if ((ulong)CONFIG_SYS_HZ > 100000)
571 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
573 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
576 /* Wait for command completion */
577 #ifdef CONFIG_SYS_LOW_RES_TIMER
580 start = get_timer (0);
582 while (flash_is_busy (info, sector)) {
583 if (get_timer (start) > tout) {
584 printf ("Flash %s timeout at address %lx data %lx\n",
585 prompt, info->start[sector],
586 flash_read_long (info, sector, 0));
587 flash_write_cmd (info, sector, 0, info->cmd_reset);
591 udelay (1); /* also triggers watchdog */
596 /*-----------------------------------------------------------------------
597 * Wait for XSR.7 to be set, if it times out print an error, otherwise
598 * do a full status check.
600 * This routine sets the flash to read-array mode.
602 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
603 ulong tout, char *prompt)
607 retcode = flash_status_check (info, sector, tout, prompt);
608 switch (info->vendor) {
609 case CFI_CMDSET_INTEL_PROG_REGIONS:
610 case CFI_CMDSET_INTEL_EXTENDED:
611 case CFI_CMDSET_INTEL_STANDARD:
612 if ((retcode != ERR_OK)
613 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
615 printf ("Flash %s error at address %lx\n", prompt,
616 info->start[sector]);
617 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
618 FLASH_STATUS_PSLBS)) {
619 puts ("Command Sequence Error.\n");
620 } else if (flash_isset (info, sector, 0,
621 FLASH_STATUS_ECLBS)) {
622 puts ("Block Erase Error.\n");
623 retcode = ERR_NOT_ERASED;
624 } else if (flash_isset (info, sector, 0,
625 FLASH_STATUS_PSLBS)) {
626 puts ("Locking Error\n");
628 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
629 puts ("Block locked.\n");
630 retcode = ERR_PROTECTED;
632 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
633 puts ("Vpp Low Error.\n");
635 flash_write_cmd (info, sector, 0, info->cmd_reset);
644 static int use_flash_status_poll(flash_info_t *info)
646 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
647 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
648 info->vendor == CFI_CMDSET_AMD_STANDARD)
654 static int flash_status_poll(flash_info_t *info, void *src, void *dst,
655 ulong tout, char *prompt)
657 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
661 #if CONFIG_SYS_HZ != 1000
662 if ((ulong)CONFIG_SYS_HZ > 100000)
663 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
665 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
668 /* Wait for command completion */
669 #ifdef CONFIG_SYS_LOW_RES_TIMER
672 start = get_timer(0);
675 switch (info->portwidth) {
677 ready = flash_read8(dst) == flash_read8(src);
679 case FLASH_CFI_16BIT:
680 ready = flash_read16(dst) == flash_read16(src);
682 case FLASH_CFI_32BIT:
683 ready = flash_read32(dst) == flash_read32(src);
685 case FLASH_CFI_64BIT:
686 ready = flash_read64(dst) == flash_read64(src);
694 if (get_timer(start) > tout) {
695 printf("Flash %s timeout at address %lx data %lx\n",
696 prompt, (ulong)dst, (ulong)flash_read8(dst));
699 udelay(1); /* also triggers watchdog */
701 #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
705 /*-----------------------------------------------------------------------
707 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
709 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
712 unsigned long long ll;
715 switch (info->portwidth) {
719 case FLASH_CFI_16BIT:
720 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
723 cword->w = (cword->w >> 8) | w;
725 cword->w = (cword->w << 8) | c;
728 case FLASH_CFI_32BIT:
729 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
732 cword->l = (cword->l >> 8) | l;
734 cword->l = (cword->l << 8) | c;
737 case FLASH_CFI_64BIT:
738 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
741 cword->ll = (cword->ll >> 8) | ll;
743 cword->ll = (cword->ll << 8) | c;
750 * Loop through the sector table starting from the previously found sector.
751 * Searches forwards or backwards, dependent on the passed address.
753 static flash_sect_t find_sector (flash_info_t * info, ulong addr)
755 static flash_sect_t saved_sector = 0; /* previously found sector */
756 static flash_info_t *saved_info = 0; /* previously used flash bank */
757 flash_sect_t sector = saved_sector;
759 if ((info != saved_info) || (sector >= info->sector_count))
762 while ((info->start[sector] < addr)
763 && (sector < info->sector_count - 1))
765 while ((info->start[sector] > addr) && (sector > 0))
767 * also decrements the sector in case of an overshot
772 saved_sector = sector;
777 /*-----------------------------------------------------------------------
779 static int flash_write_cfiword (flash_info_t * info, ulong dest,
782 void *dstaddr = (void *)dest;
784 flash_sect_t sect = 0;
787 /* Check if Flash is (sufficiently) erased */
788 switch (info->portwidth) {
790 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
792 case FLASH_CFI_16BIT:
793 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
795 case FLASH_CFI_32BIT:
796 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
798 case FLASH_CFI_64BIT:
799 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
806 return ERR_NOT_ERASED;
808 /* Disable interrupts which might cause a timeout here */
809 flag = disable_interrupts ();
811 switch (info->vendor) {
812 case CFI_CMDSET_INTEL_PROG_REGIONS:
813 case CFI_CMDSET_INTEL_EXTENDED:
814 case CFI_CMDSET_INTEL_STANDARD:
815 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
816 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
818 case CFI_CMDSET_AMD_EXTENDED:
819 case CFI_CMDSET_AMD_STANDARD:
820 sect = find_sector(info, dest);
821 flash_unlock_seq (info, sect);
822 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
825 #ifdef CONFIG_FLASH_CFI_LEGACY
826 case CFI_CMDSET_AMD_LEGACY:
827 sect = find_sector(info, dest);
828 flash_unlock_seq (info, 0);
829 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
835 switch (info->portwidth) {
837 flash_write8(cword.c, dstaddr);
839 case FLASH_CFI_16BIT:
840 flash_write16(cword.w, dstaddr);
842 case FLASH_CFI_32BIT:
843 flash_write32(cword.l, dstaddr);
845 case FLASH_CFI_64BIT:
846 flash_write64(cword.ll, dstaddr);
850 /* re-enable interrupts if necessary */
852 enable_interrupts ();
855 sect = find_sector (info, dest);
857 if (use_flash_status_poll(info))
858 return flash_status_poll(info, &cword, dstaddr,
859 info->write_tout, "write");
861 return flash_full_status_check(info, sect,
862 info->write_tout, "write");
865 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
867 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
874 void *dst = (void *)dest;
881 switch (info->portwidth) {
885 case FLASH_CFI_16BIT:
888 case FLASH_CFI_32BIT:
891 case FLASH_CFI_64BIT:
901 while ((cnt-- > 0) && (flag == 1)) {
902 switch (info->portwidth) {
904 flag = ((flash_read8(dst2) & flash_read8(src)) ==
908 case FLASH_CFI_16BIT:
909 flag = ((flash_read16(dst2) & flash_read16(src)) ==
913 case FLASH_CFI_32BIT:
914 flag = ((flash_read32(dst2) & flash_read32(src)) ==
918 case FLASH_CFI_64BIT:
919 flag = ((flash_read64(dst2) & flash_read64(src)) ==
926 retcode = ERR_NOT_ERASED;
931 sector = find_sector (info, dest);
933 switch (info->vendor) {
934 case CFI_CMDSET_INTEL_PROG_REGIONS:
935 case CFI_CMDSET_INTEL_STANDARD:
936 case CFI_CMDSET_INTEL_EXTENDED:
937 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
938 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
939 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
940 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
941 flash_write_cmd (info, sector, 0, write_cmd);
942 retcode = flash_status_check (info, sector,
943 info->buffer_write_tout,
945 if (retcode == ERR_OK) {
946 /* reduce the number of loops by the width of
949 flash_write_cmd (info, sector, 0, cnt - 1);
951 switch (info->portwidth) {
953 flash_write8(flash_read8(src), dst);
956 case FLASH_CFI_16BIT:
957 flash_write16(flash_read16(src), dst);
960 case FLASH_CFI_32BIT:
961 flash_write32(flash_read32(src), dst);
964 case FLASH_CFI_64BIT:
965 flash_write64(flash_read64(src), dst);
973 flash_write_cmd (info, sector, 0,
974 FLASH_CMD_WRITE_BUFFER_CONFIRM);
975 retcode = flash_full_status_check (
976 info, sector, info->buffer_write_tout,
982 case CFI_CMDSET_AMD_STANDARD:
983 case CFI_CMDSET_AMD_EXTENDED:
984 flash_unlock_seq(info,0);
986 #ifdef CONFIG_FLASH_SPANSION_S29WS_N
987 offset = ((unsigned long)dst - info->start[sector]) >> shift;
989 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
991 flash_write_cmd(info, sector, offset, cnt - 1);
993 switch (info->portwidth) {
996 flash_write8(flash_read8(src), dst);
1000 case FLASH_CFI_16BIT:
1002 flash_write16(flash_read16(src), dst);
1006 case FLASH_CFI_32BIT:
1008 flash_write32(flash_read32(src), dst);
1012 case FLASH_CFI_64BIT:
1014 flash_write64(flash_read64(src), dst);
1019 retcode = ERR_INVAL;
1023 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1024 if (use_flash_status_poll(info))
1025 retcode = flash_status_poll(info, src - (1 << shift),
1027 info->buffer_write_tout,
1030 retcode = flash_full_status_check(info, sector,
1031 info->buffer_write_tout,
1036 debug ("Unknown Command Set\n");
1037 retcode = ERR_INVAL;
1044 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1047 /*-----------------------------------------------------------------------
1049 int flash_erase (flash_info_t * info, int s_first, int s_last)
1056 if (info->flash_id != FLASH_MAN_CFI) {
1057 puts ("Can't erase unknown flash type - aborted\n");
1060 if ((s_first < 0) || (s_first > s_last)) {
1061 puts ("- no sectors to erase\n");
1066 for (sect = s_first; sect <= s_last; ++sect) {
1067 if (info->protect[sect]) {
1072 printf ("- Warning: %d protected sectors will not be erased!\n",
1074 } else if (flash_verbose) {
1079 for (sect = s_first; sect <= s_last; sect++) {
1080 if (info->protect[sect] == 0) { /* not protected */
1081 #ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1088 * Check if whole sector is erased
1090 size = flash_sector_size(info, sect);
1092 flash = (u32 *)info->start[sect];
1093 /* divide by 4 for longword access */
1095 for (k = 0; k < size; k++) {
1096 if (flash_read32(flash++) != 0xffffffff) {
1107 switch (info->vendor) {
1108 case CFI_CMDSET_INTEL_PROG_REGIONS:
1109 case CFI_CMDSET_INTEL_STANDARD:
1110 case CFI_CMDSET_INTEL_EXTENDED:
1111 flash_write_cmd (info, sect, 0,
1112 FLASH_CMD_CLEAR_STATUS);
1113 flash_write_cmd (info, sect, 0,
1114 FLASH_CMD_BLOCK_ERASE);
1115 flash_write_cmd (info, sect, 0,
1116 FLASH_CMD_ERASE_CONFIRM);
1118 case CFI_CMDSET_AMD_STANDARD:
1119 case CFI_CMDSET_AMD_EXTENDED:
1120 flash_unlock_seq (info, sect);
1121 flash_write_cmd (info, sect,
1123 AMD_CMD_ERASE_START);
1124 flash_unlock_seq (info, sect);
1125 flash_write_cmd (info, sect, 0,
1126 AMD_CMD_ERASE_SECTOR);
1128 #ifdef CONFIG_FLASH_CFI_LEGACY
1129 case CFI_CMDSET_AMD_LEGACY:
1130 flash_unlock_seq (info, 0);
1131 flash_write_cmd (info, 0, info->addr_unlock1,
1132 AMD_CMD_ERASE_START);
1133 flash_unlock_seq (info, 0);
1134 flash_write_cmd (info, sect, 0,
1135 AMD_CMD_ERASE_SECTOR);
1139 debug ("Unkown flash vendor %d\n",
1144 if (use_flash_status_poll(info)) {
1145 cfiword_t cword = (cfiword_t)0xffffffffffffffffULL;
1147 dest = flash_map(info, sect, 0);
1148 st = flash_status_poll(info, &cword, dest,
1149 info->erase_blk_tout, "erase");
1150 flash_unmap(info, sect, 0, dest);
1152 st = flash_full_status_check(info, sect,
1153 info->erase_blk_tout,
1157 else if (flash_verbose)
1168 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1169 static int sector_erased(flash_info_t *info, int i)
1176 * Check if whole sector is erased
1178 size = flash_sector_size(info, i);
1179 flash = (u32 *)info->start[i];
1180 /* divide by 4 for longword access */
1183 for (k = 0; k < size; k++) {
1184 if (flash_read32(flash++) != 0xffffffff)
1185 return 0; /* not erased */
1188 return 1; /* erased */
1190 #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1192 void flash_print_info (flash_info_t * info)
1196 if (info->flash_id != FLASH_MAN_CFI) {
1197 puts ("missing or unknown FLASH type\n");
1201 printf ("%s flash (%d x %d)",
1203 (info->portwidth << 3), (info->chipwidth << 3));
1204 if (info->size < 1024*1024)
1205 printf (" Size: %ld kB in %d Sectors\n",
1206 info->size >> 10, info->sector_count);
1208 printf (" Size: %ld MB in %d Sectors\n",
1209 info->size >> 20, info->sector_count);
1211 switch (info->vendor) {
1212 case CFI_CMDSET_INTEL_PROG_REGIONS:
1213 printf ("Intel Prog Regions");
1215 case CFI_CMDSET_INTEL_STANDARD:
1216 printf ("Intel Standard");
1218 case CFI_CMDSET_INTEL_EXTENDED:
1219 printf ("Intel Extended");
1221 case CFI_CMDSET_AMD_STANDARD:
1222 printf ("AMD Standard");
1224 case CFI_CMDSET_AMD_EXTENDED:
1225 printf ("AMD Extended");
1227 #ifdef CONFIG_FLASH_CFI_LEGACY
1228 case CFI_CMDSET_AMD_LEGACY:
1229 printf ("AMD Legacy");
1233 printf ("Unknown (%d)", info->vendor);
1236 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1237 info->manufacturer_id);
1238 printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1240 if ((info->device_id & 0xff) == 0x7E) {
1241 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1244 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1245 info->erase_blk_tout,
1247 if (info->buffer_size > 1) {
1248 printf (" Buffer write timeout: %ld ms, "
1249 "buffer size: %d bytes\n",
1250 info->buffer_write_tout,
1254 puts ("\n Sector Start Addresses:");
1255 for (i = 0; i < info->sector_count; ++i) {
1260 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1261 /* print empty and read-only info */
1262 printf (" %08lX %c %s ",
1264 sector_erased(info, i) ? 'E' : ' ',
1265 info->protect[i] ? "RO" : " ");
1266 #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
1267 printf (" %08lX %s ",
1269 info->protect[i] ? "RO" : " ");
1276 /*-----------------------------------------------------------------------
1277 * This is used in a few places in write_buf() to show programming
1278 * progress. Making it a function is nasty because it needs to do side
1279 * effect updates to digit and dots. Repeated code is nasty too, so
1280 * we define it once here.
1282 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1283 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
1284 if (flash_verbose) { \
1286 if ((scale > 0) && (dots <= 0)) { \
1287 if ((digit % 5) == 0) \
1288 printf ("%d", digit / 5); \
1296 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1299 /*-----------------------------------------------------------------------
1300 * Copy memory to flash, returns:
1303 * 2 - Flash not erased
1305 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
1312 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1315 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1316 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1321 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1323 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1324 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1325 CONFIG_FLASH_SHOW_PROGRESS);
1329 /* get lower aligned address */
1330 wp = (addr & ~(info->portwidth - 1));
1332 /* handle unaligned start */
1333 if ((aln = addr - wp) != 0) {
1336 for (i = 0; i < aln; ++i)
1337 flash_add_byte (info, &cword, flash_read8(p + i));
1339 for (; (i < info->portwidth) && (cnt > 0); i++) {
1340 flash_add_byte (info, &cword, *src++);
1343 for (; (cnt == 0) && (i < info->portwidth); ++i)
1344 flash_add_byte (info, &cword, flash_read8(p + i));
1346 rc = flash_write_cfiword (info, wp, cword);
1351 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1354 /* handle the aligned part */
1355 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1356 buffered_size = (info->portwidth / info->chipwidth);
1357 buffered_size *= info->buffer_size;
1358 while (cnt >= info->portwidth) {
1359 /* prohibit buffer write when buffer_size is 1 */
1360 if (info->buffer_size == 1) {
1362 for (i = 0; i < info->portwidth; i++)
1363 flash_add_byte (info, &cword, *src++);
1364 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1366 wp += info->portwidth;
1367 cnt -= info->portwidth;
1371 /* write buffer until next buffered_size aligned boundary */
1372 i = buffered_size - (wp % buffered_size);
1375 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1377 i -= i & (info->portwidth - 1);
1381 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1384 while (cnt >= info->portwidth) {
1386 for (i = 0; i < info->portwidth; i++) {
1387 flash_add_byte (info, &cword, *src++);
1389 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1391 wp += info->portwidth;
1392 cnt -= info->portwidth;
1393 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
1395 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1402 * handle unaligned tail bytes
1406 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
1407 flash_add_byte (info, &cword, *src++);
1410 for (; i < info->portwidth; ++i)
1411 flash_add_byte (info, &cword, flash_read8(p + i));
1413 return flash_write_cfiword (info, wp, cword);
1416 /*-----------------------------------------------------------------------
1418 #ifdef CONFIG_SYS_FLASH_PROTECTION
1420 static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1422 if ((info->manufacturer_id == (uchar)INTEL_MANUFACT) &&
1423 (info->device_id == NUMONYX_256MBIT)) {
1426 * "Numonyx Axcell P33/P30 Specification Update" :)
1428 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1429 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1432 * cmd must come before FLASH_CMD_PROTECT + 20us
1433 * Disable interrupts which might cause a timeout here.
1435 int flag = disable_interrupts();
1439 cmd = FLASH_CMD_PROTECT_SET;
1441 cmd = FLASH_CMD_PROTECT_CLEAR;
1442 flash_write_cmd(info, sector, 0,
1444 flash_write_cmd(info, sector, 0, cmd);
1445 /* re-enable interrupts if necessary */
1447 enable_interrupts();
1454 int flash_real_protect (flash_info_t * info, long sector, int prot)
1458 switch (info->vendor) {
1459 case CFI_CMDSET_INTEL_PROG_REGIONS:
1460 case CFI_CMDSET_INTEL_STANDARD:
1461 case CFI_CMDSET_INTEL_EXTENDED:
1462 if (!cfi_protect_bugfix(info, sector, prot)) {
1463 flash_write_cmd(info, sector, 0,
1464 FLASH_CMD_CLEAR_STATUS);
1465 flash_write_cmd(info, sector, 0,
1468 flash_write_cmd(info, sector, 0,
1469 FLASH_CMD_PROTECT_SET);
1471 flash_write_cmd(info, sector, 0,
1472 FLASH_CMD_PROTECT_CLEAR);
1476 case CFI_CMDSET_AMD_EXTENDED:
1477 case CFI_CMDSET_AMD_STANDARD:
1478 /* U-Boot only checks the first byte */
1479 if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
1481 flash_unlock_seq (info, 0);
1482 flash_write_cmd (info, 0,
1484 ATM_CMD_SOFTLOCK_START);
1485 flash_unlock_seq (info, 0);
1486 flash_write_cmd (info, sector, 0,
1489 flash_write_cmd (info, 0,
1491 AMD_CMD_UNLOCK_START);
1492 if (info->device_id == ATM_ID_BV6416)
1493 flash_write_cmd (info, sector,
1494 0, ATM_CMD_UNLOCK_SECT);
1497 if (info->manufacturer_id == (uchar)AMD_MANUFACT) {
1498 int flag = disable_interrupts();
1501 flash_unlock_seq(info, 0);
1502 flash_write_cmd(info, 0, info->addr_unlock1,
1503 AMD_CMD_SET_PPB_ENTRY);
1504 lock_flag = flash_isset(info, sector, 0, 0x01);
1507 flash_write_cmd(info, sector, 0,
1508 AMD_CMD_PPB_LOCK_BC1);
1509 flash_write_cmd(info, sector, 0,
1510 AMD_CMD_PPB_LOCK_BC2);
1512 debug("sector %ld %slocked\n", sector,
1513 lock_flag ? "" : "already ");
1516 debug("unlock %ld\n", sector);
1517 flash_write_cmd(info, 0, 0,
1518 AMD_CMD_PPB_UNLOCK_BC1);
1519 flash_write_cmd(info, 0, 0,
1520 AMD_CMD_PPB_UNLOCK_BC2);
1522 debug("sector %ld %sunlocked\n", sector,
1523 !lock_flag ? "" : "already ");
1526 enable_interrupts();
1528 if (flash_status_check(info, sector,
1529 info->erase_blk_tout,
1530 prot ? "protect" : "unprotect"))
1531 printf("status check error\n");
1533 flash_write_cmd(info, 0, 0,
1534 AMD_CMD_SET_PPB_EXIT_BC1);
1535 flash_write_cmd(info, 0, 0,
1536 AMD_CMD_SET_PPB_EXIT_BC2);
1539 #ifdef CONFIG_FLASH_CFI_LEGACY
1540 case CFI_CMDSET_AMD_LEGACY:
1541 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1542 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1544 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1546 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1551 * Flash needs to be in status register read mode for
1552 * flash_full_status_check() to work correctly
1554 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
1556 flash_full_status_check (info, sector, info->erase_blk_tout,
1557 prot ? "protect" : "unprotect")) == 0) {
1559 info->protect[sector] = prot;
1562 * On some of Intel's flash chips (marked via legacy_unlock)
1563 * unprotect unprotects all locking.
1565 if ((prot == 0) && (info->legacy_unlock)) {
1568 for (i = 0; i < info->sector_count; i++) {
1569 if (info->protect[i])
1570 flash_real_protect (info, i, 1);
1577 /*-----------------------------------------------------------------------
1578 * flash_read_user_serial - read the OneTimeProgramming cells
1580 void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1587 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
1588 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1589 memcpy (dst, src + offset, len);
1590 flash_write_cmd (info, 0, 0, info->cmd_reset);
1592 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
1596 * flash_read_factory_serial - read the device Id from the protection area
1598 void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1603 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1604 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1605 memcpy (buffer, src + offset, len);
1606 flash_write_cmd (info, 0, 0, info->cmd_reset);
1608 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
1611 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1613 /*-----------------------------------------------------------------------
1614 * Reverse the order of the erase regions in the CFI QRY structure.
1615 * This is needed for chips that are either a) correctly detected as
1616 * top-boot, or b) buggy.
1618 static void cfi_reverse_geometry(struct cfi_qry *qry)
1623 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1624 tmp = qry->erase_region_info[i];
1625 qry->erase_region_info[i] = qry->erase_region_info[j];
1626 qry->erase_region_info[j] = tmp;
1630 /*-----------------------------------------------------------------------
1631 * read jedec ids from device and set corresponding fields in info struct
1633 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1636 static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1638 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1640 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1641 udelay(1000); /* some flash are slow to respond */
1642 info->manufacturer_id = flash_read_uchar (info,
1643 FLASH_OFFSET_MANUFACTURER_ID);
1644 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1645 flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
1646 flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
1647 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1650 static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1652 info->cmd_reset = FLASH_CMD_RESET;
1654 cmdset_intel_read_jedec_ids(info);
1655 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1657 #ifdef CONFIG_SYS_FLASH_PROTECTION
1658 /* read legacy lock/unlock bit from intel flash */
1659 if (info->ext_addr) {
1660 info->legacy_unlock = flash_read_uchar (info,
1661 info->ext_addr + 5) & 0x08;
1668 static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1673 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1674 flash_unlock_seq(info, 0);
1675 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1676 udelay(1000); /* some flash are slow to respond */
1678 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1679 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1680 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1682 manuId = flash_read_uchar (info,
1683 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1685 info->manufacturer_id = manuId;
1687 switch (info->chipwidth){
1688 case FLASH_CFI_8BIT:
1689 info->device_id = flash_read_uchar (info,
1690 FLASH_OFFSET_DEVICE_ID);
1691 if (info->device_id == 0x7E) {
1692 /* AMD 3-byte (expanded) device ids */
1693 info->device_id2 = flash_read_uchar (info,
1694 FLASH_OFFSET_DEVICE_ID2);
1695 info->device_id2 <<= 8;
1696 info->device_id2 |= flash_read_uchar (info,
1697 FLASH_OFFSET_DEVICE_ID3);
1700 case FLASH_CFI_16BIT:
1701 info->device_id = flash_read_word (info,
1702 FLASH_OFFSET_DEVICE_ID);
1703 if ((info->device_id & 0xff) == 0x7E) {
1704 /* AMD 3-byte (expanded) device ids */
1705 info->device_id2 = flash_read_uchar (info,
1706 FLASH_OFFSET_DEVICE_ID2);
1707 info->device_id2 <<= 8;
1708 info->device_id2 |= flash_read_uchar (info,
1709 FLASH_OFFSET_DEVICE_ID3);
1715 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1719 static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1721 info->cmd_reset = AMD_CMD_RESET;
1723 cmdset_amd_read_jedec_ids(info);
1724 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1726 #ifdef CONFIG_SYS_FLASH_PROTECTION
1727 if (info->ext_addr && info->manufacturer_id == (uchar)AMD_MANUFACT) {
1730 /* read sector protect/unprotect scheme */
1731 spus = flash_read_uchar(info, info->ext_addr + 9);
1733 info->legacy_unlock = 1;
1740 #ifdef CONFIG_FLASH_CFI_LEGACY
1741 static void flash_read_jedec_ids (flash_info_t * info)
1743 info->manufacturer_id = 0;
1744 info->device_id = 0;
1745 info->device_id2 = 0;
1747 switch (info->vendor) {
1748 case CFI_CMDSET_INTEL_PROG_REGIONS:
1749 case CFI_CMDSET_INTEL_STANDARD:
1750 case CFI_CMDSET_INTEL_EXTENDED:
1751 cmdset_intel_read_jedec_ids(info);
1753 case CFI_CMDSET_AMD_STANDARD:
1754 case CFI_CMDSET_AMD_EXTENDED:
1755 cmdset_amd_read_jedec_ids(info);
1762 /*-----------------------------------------------------------------------
1763 * Call board code to request info about non-CFI flash.
1764 * board_flash_get_legacy needs to fill in at least:
1765 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1767 static int flash_detect_legacy(phys_addr_t base, int banknum)
1769 flash_info_t *info = &flash_info[banknum];
1771 if (board_flash_get_legacy(base, banknum, info)) {
1772 /* board code may have filled info completely. If not, we
1773 use JEDEC ID probing. */
1774 if (!info->vendor) {
1776 CFI_CMDSET_AMD_STANDARD,
1777 CFI_CMDSET_INTEL_STANDARD
1781 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1782 info->vendor = modes[i];
1784 (ulong)map_physmem(base,
1787 if (info->portwidth == FLASH_CFI_8BIT
1788 && info->interface == FLASH_CFI_X8X16) {
1789 info->addr_unlock1 = 0x2AAA;
1790 info->addr_unlock2 = 0x5555;
1792 info->addr_unlock1 = 0x5555;
1793 info->addr_unlock2 = 0x2AAA;
1795 flash_read_jedec_ids(info);
1796 debug("JEDEC PROBE: ID %x %x %x\n",
1797 info->manufacturer_id,
1800 if (jedec_flash_match(info, info->start[0]))
1803 unmap_physmem((void *)info->start[0],
1808 switch(info->vendor) {
1809 case CFI_CMDSET_INTEL_PROG_REGIONS:
1810 case CFI_CMDSET_INTEL_STANDARD:
1811 case CFI_CMDSET_INTEL_EXTENDED:
1812 info->cmd_reset = FLASH_CMD_RESET;
1814 case CFI_CMDSET_AMD_STANDARD:
1815 case CFI_CMDSET_AMD_EXTENDED:
1816 case CFI_CMDSET_AMD_LEGACY:
1817 info->cmd_reset = AMD_CMD_RESET;
1820 info->flash_id = FLASH_MAN_CFI;
1823 return 0; /* use CFI */
1826 static inline int flash_detect_legacy(phys_addr_t base, int banknum)
1828 return 0; /* use CFI */
1832 /*-----------------------------------------------------------------------
1833 * detect if flash is compatible with the Common Flash Interface (CFI)
1834 * http://www.jedec.org/download/search/jesd68.pdf
1836 static void flash_read_cfi (flash_info_t *info, void *buf,
1837 unsigned int start, size_t len)
1842 for (i = 0; i < len; i++)
1843 p[i] = flash_read_uchar(info, start + i);
1846 void __flash_cmd_reset(flash_info_t *info)
1849 * We do not yet know what kind of commandset to use, so we issue
1850 * the reset command in both Intel and AMD variants, in the hope
1851 * that AMD flash roms ignore the Intel command.
1853 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1855 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1857 void flash_cmd_reset(flash_info_t *info)
1858 __attribute__((weak,alias("__flash_cmd_reset")));
1860 static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1864 /* Issue FLASH reset command */
1865 flash_cmd_reset(info);
1868 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1870 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1872 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1873 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1874 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1875 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1876 sizeof(struct cfi_qry));
1877 info->interface = le16_to_cpu(qry->interface_desc);
1879 info->cfi_offset = flash_offset_cfi[cfi_offset];
1880 debug ("device interface is %d\n",
1882 debug ("found port %d chip %d ",
1883 info->portwidth, info->chipwidth);
1884 debug ("port %d bits chip %d bits\n",
1885 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1886 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1888 /* calculate command offsets as in the Linux driver */
1889 info->addr_unlock1 = 0x555;
1890 info->addr_unlock2 = 0x2aa;
1893 * modify the unlock address if we are
1894 * in compatibility mode
1896 if ( /* x8/x16 in x8 mode */
1897 ((info->chipwidth == FLASH_CFI_BY8) &&
1898 (info->interface == FLASH_CFI_X8X16)) ||
1899 /* x16/x32 in x16 mode */
1900 ((info->chipwidth == FLASH_CFI_BY16) &&
1901 (info->interface == FLASH_CFI_X16X32)))
1903 info->addr_unlock1 = 0xaaa;
1904 info->addr_unlock2 = 0x555;
1907 info->name = "CFI conformant";
1915 static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1917 debug ("flash detect cfi\n");
1919 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
1920 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1921 for (info->chipwidth = FLASH_CFI_BY8;
1922 info->chipwidth <= info->portwidth;
1923 info->chipwidth <<= 1)
1924 if (__flash_detect_cfi(info, qry))
1927 debug ("not found\n");
1932 * Manufacturer-specific quirks. Add workarounds for geometry
1933 * reversal, etc. here.
1935 static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1937 /* check if flash geometry needs reversal */
1938 if (qry->num_erase_regions > 1) {
1939 /* reverse geometry if top boot part */
1940 if (info->cfi_version < 0x3131) {
1941 /* CFI < 1.1, try to guess from device id */
1942 if ((info->device_id & 0x80) != 0)
1943 cfi_reverse_geometry(qry);
1944 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1945 /* CFI >= 1.1, deduct from top/bottom flag */
1946 /* note: ext_addr is valid since cfi_version > 0 */
1947 cfi_reverse_geometry(qry);
1952 static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1954 int reverse_geometry = 0;
1956 /* Check the "top boot" bit in the PRI */
1957 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1958 reverse_geometry = 1;
1960 /* AT49BV6416(T) list the erase regions in the wrong order.
1961 * However, the device ID is identical with the non-broken
1962 * AT49BV642D they differ in the high byte.
1964 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1965 reverse_geometry = !reverse_geometry;
1967 if (reverse_geometry)
1968 cfi_reverse_geometry(qry);
1971 static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1973 /* check if flash geometry needs reversal */
1974 if (qry->num_erase_regions > 1) {
1975 /* reverse geometry if top boot part */
1976 if (info->cfi_version < 0x3131) {
1977 /* CFI < 1.1, guess by device id */
1978 if (info->device_id == 0x22CA || /* M29W320DT */
1979 info->device_id == 0x2256 || /* M29W320ET */
1980 info->device_id == 0x22D7) { /* M29W800DT */
1981 cfi_reverse_geometry(qry);
1983 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1984 /* CFI >= 1.1, deduct from top/bottom flag */
1985 /* note: ext_addr is valid since cfi_version > 0 */
1986 cfi_reverse_geometry(qry);
1992 * The following code cannot be run from FLASH!
1995 ulong flash_get_size (phys_addr_t base, int banknum)
1997 flash_info_t *info = &flash_info[banknum];
1999 flash_sect_t sect_cnt;
2003 uchar num_erase_regions;
2004 int erase_region_size;
2005 int erase_region_count;
2007 unsigned long max_size;
2009 memset(&qry, 0, sizeof(qry));
2012 info->cfi_version = 0;
2013 #ifdef CONFIG_SYS_FLASH_PROTECTION
2014 info->legacy_unlock = 0;
2017 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
2019 if (flash_detect_cfi (info, &qry)) {
2020 info->vendor = le16_to_cpu(qry.p_id);
2021 info->ext_addr = le16_to_cpu(qry.p_adr);
2022 num_erase_regions = qry.num_erase_regions;
2024 if (info->ext_addr) {
2025 info->cfi_version = (ushort) flash_read_uchar (info,
2026 info->ext_addr + 3) << 8;
2027 info->cfi_version |= (ushort) flash_read_uchar (info,
2028 info->ext_addr + 4);
2032 flash_printqry (&qry);
2035 switch (info->vendor) {
2036 case CFI_CMDSET_INTEL_PROG_REGIONS:
2037 case CFI_CMDSET_INTEL_STANDARD:
2038 case CFI_CMDSET_INTEL_EXTENDED:
2039 cmdset_intel_init(info, &qry);
2041 case CFI_CMDSET_AMD_STANDARD:
2042 case CFI_CMDSET_AMD_EXTENDED:
2043 cmdset_amd_init(info, &qry);
2046 printf("CFI: Unknown command set 0x%x\n",
2049 * Unfortunately, this means we don't know how
2050 * to get the chip back to Read mode. Might
2051 * as well try an Intel-style reset...
2053 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2057 /* Do manufacturer-specific fixups */
2058 switch (info->manufacturer_id) {
2059 case 0x0001: /* AMD */
2060 case 0x0037: /* AMIC */
2061 flash_fixup_amd(info, &qry);
2064 flash_fixup_atmel(info, &qry);
2067 flash_fixup_stm(info, &qry);
2071 debug ("manufacturer is %d\n", info->vendor);
2072 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
2073 debug ("device id is 0x%x\n", info->device_id);
2074 debug ("device id2 is 0x%x\n", info->device_id2);
2075 debug ("cfi version is 0x%04x\n", info->cfi_version);
2077 size_ratio = info->portwidth / info->chipwidth;
2078 /* if the chip is x8/x16 reduce the ratio by half */
2079 if ((info->interface == FLASH_CFI_X8X16)
2080 && (info->chipwidth == FLASH_CFI_BY8)) {
2083 debug ("size_ratio %d port %d bits chip %d bits\n",
2084 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2085 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
2086 info->size = 1 << qry.dev_size;
2087 /* multiply the size by the number of chips */
2088 info->size *= size_ratio;
2089 max_size = cfi_flash_bank_size(banknum);
2090 if (max_size && (info->size > max_size)) {
2091 debug("[truncated from %ldMiB]", info->size >> 20);
2092 info->size = max_size;
2094 debug ("found %d erase regions\n", num_erase_regions);
2097 for (i = 0; i < num_erase_regions; i++) {
2098 if (i > NUM_ERASE_REGIONS) {
2099 printf ("%d erase regions found, only %d used\n",
2100 num_erase_regions, NUM_ERASE_REGIONS);
2104 tmp = le32_to_cpu(qry.erase_region_info[i]);
2105 debug("erase region %u: 0x%08lx\n", i, tmp);
2107 erase_region_count = (tmp & 0xffff) + 1;
2110 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
2111 debug ("erase_region_count = %d erase_region_size = %d\n",
2112 erase_region_count, erase_region_size);
2113 for (j = 0; j < erase_region_count; j++) {
2114 if (sector - base >= info->size)
2116 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
2117 printf("ERROR: too many flash sectors\n");
2120 info->start[sect_cnt] =
2121 (ulong)map_physmem(sector,
2124 sector += (erase_region_size * size_ratio);
2127 * Only read protection status from
2128 * supported devices (intel...)
2130 switch (info->vendor) {
2131 case CFI_CMDSET_INTEL_PROG_REGIONS:
2132 case CFI_CMDSET_INTEL_EXTENDED:
2133 case CFI_CMDSET_INTEL_STANDARD:
2135 * Set flash to read-id mode. Otherwise
2136 * reading protected status is not
2139 flash_write_cmd(info, sect_cnt, 0,
2141 info->protect[sect_cnt] =
2142 flash_isset (info, sect_cnt,
2143 FLASH_OFFSET_PROTECT,
2144 FLASH_STATUS_PROTECT);
2147 /* default: not protected */
2148 info->protect[sect_cnt] = 0;
2155 info->sector_count = sect_cnt;
2156 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2157 tmp = 1 << qry.block_erase_timeout_typ;
2158 info->erase_blk_tout = tmp *
2159 (1 << qry.block_erase_timeout_max);
2160 tmp = (1 << qry.buf_write_timeout_typ) *
2161 (1 << qry.buf_write_timeout_max);
2163 /* round up when converting to ms */
2164 info->buffer_write_tout = (tmp + 999) / 1000;
2165 tmp = (1 << qry.word_write_timeout_typ) *
2166 (1 << qry.word_write_timeout_max);
2167 /* round up when converting to ms */
2168 info->write_tout = (tmp + 999) / 1000;
2169 info->flash_id = FLASH_MAN_CFI;
2170 if ((info->interface == FLASH_CFI_X8X16) &&
2171 (info->chipwidth == FLASH_CFI_BY8)) {
2172 /* XXX - Need to test on x8/x16 in parallel. */
2173 info->portwidth >>= 1;
2176 flash_write_cmd (info, 0, 0, info->cmd_reset);
2179 return (info->size);
2182 #ifdef CONFIG_FLASH_CFI_MTD
2183 void flash_set_verbose(uint v)
2189 static void cfi_flash_set_config_reg(u32 base, u16 val)
2191 #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2193 * Only set this config register if really defined
2194 * to a valid value (0xffff is invalid)
2200 * Set configuration register. Data is "encrypted" in the 16 lower
2203 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2204 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2207 * Finally issue reset-command to bring device back to
2210 flash_write16(FLASH_CMD_RESET, (void *)base);
2214 /*-----------------------------------------------------------------------
2217 void flash_protect_default(void)
2219 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2224 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2227 /* Monitor protection ON by default */
2228 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2229 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2230 flash_protect(FLAG_PROTECT_SET,
2231 CONFIG_SYS_MONITOR_BASE,
2232 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2233 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2236 /* Environment protection ON by default */
2237 #ifdef CONFIG_ENV_IS_IN_FLASH
2238 flash_protect(FLAG_PROTECT_SET,
2240 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2241 flash_get_info(CONFIG_ENV_ADDR));
2244 /* Redundant environment protection ON by default */
2245 #ifdef CONFIG_ENV_ADDR_REDUND
2246 flash_protect(FLAG_PROTECT_SET,
2247 CONFIG_ENV_ADDR_REDUND,
2248 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2249 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2252 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2253 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
2254 debug("autoprotecting from %08lx to %08lx\n",
2255 apl[i].start, apl[i].start + apl[i].size - 1);
2256 flash_protect(FLAG_PROTECT_SET,
2258 apl[i].start + apl[i].size - 1,
2259 flash_get_info(apl[i].start));
2264 unsigned long flash_init (void)
2266 unsigned long size = 0;
2269 #ifdef CONFIG_SYS_FLASH_PROTECTION
2270 /* read environment from EEPROM */
2272 getenv_f("unlock", s, sizeof(s));
2275 /* Init: no FLASHes known */
2276 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
2277 flash_info[i].flash_id = FLASH_UNKNOWN;
2279 /* Optionally write flash configuration register */
2280 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2281 cfi_flash_config_reg(i));
2283 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
2284 flash_get_size(cfi_flash_bank_addr(i), i);
2285 size += flash_info[i].size;
2286 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
2287 #ifndef CONFIG_SYS_FLASH_QUIET_TEST
2288 printf ("## Unknown flash on Bank %d "
2289 "- Size = 0x%08lx = %ld MB\n",
2290 i+1, flash_info[i].size,
2291 flash_info[i].size >> 20);
2292 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
2294 #ifdef CONFIG_SYS_FLASH_PROTECTION
2295 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
2297 * Only the U-Boot image and it's environment
2298 * is protected, all other sectors are
2299 * unprotected (unlocked) if flash hardware
2300 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
2301 * and the environment variable "unlock" is
2304 if (flash_info[i].legacy_unlock) {
2308 * Disable legacy_unlock temporarily,
2309 * since flash_real_protect would
2310 * relock all other sectors again
2313 flash_info[i].legacy_unlock = 0;
2316 * Legacy unlocking (e.g. Intel J3) ->
2317 * unlock only one sector. This will
2318 * unlock all sectors.
2320 flash_real_protect (&flash_info[i], 0, 0);
2322 flash_info[i].legacy_unlock = 1;
2325 * Manually mark other sectors as
2326 * unlocked (unprotected)
2328 for (k = 1; k < flash_info[i].sector_count; k++)
2329 flash_info[i].protect[k] = 0;
2332 * No legancy unlocking -> unlock all sectors
2334 flash_protect (FLAG_PROTECT_CLEAR,
2335 flash_info[i].start[0],
2336 flash_info[i].start[0]
2337 + flash_info[i].size - 1,
2341 #endif /* CONFIG_SYS_FLASH_PROTECTION */
2344 flash_protect_default();
2345 #ifdef CONFIG_FLASH_CFI_MTD