Merge branch 'master' of git://git.denx.de/u-boot-nand-flash
[platform/kernel/u-boot.git] / drivers / mtd / cfi_flash.c
index a2d88ea..402d835 100644 (file)
@@ -39,7 +39,6 @@
 #include <asm/io.h>
 #include <asm/byteorder.h>
 #include <environment.h>
-#ifdef CFG_FLASH_CFI_DRIVER
 
 /*
  * This file implements a Common Flash Interface (CFI) driver for
@@ -158,13 +157,13 @@ static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
 
 /* use CFG_MAX_FLASH_BANKS_DETECT if defined */
 #ifdef CFG_MAX_FLASH_BANKS_DETECT
-static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
-flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT];   /* FLASH chips info */
+# define CFI_MAX_FLASH_BANKS   CFG_MAX_FLASH_BANKS_DETECT
 #else
-static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
-flash_info_t flash_info[CFG_MAX_FLASH_BANKS];          /* FLASH chips info */
+# define CFI_MAX_FLASH_BANKS   CFG_MAX_FLASH_BANKS
 #endif
 
+flash_info_t flash_info[CFI_MAX_FLASH_BANKS];  /* FLASH chips info */
+
 /*
  * Check if chip width is defined. If not, start detecting with 8bit.
  */
@@ -301,11 +300,14 @@ static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
 /*-----------------------------------------------------------------------
  * make a proper sized command based on the port and chip widths
  */
-static void flash_make_cmd (flash_info_t * info, ulong cmd, void *cmdbuf)
+static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
 {
        int i;
        int cword_offset;
        int cp_offset;
+#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
+       u32 cmd_le = cpu_to_le32(cmd);
+#endif
        uchar val;
        uchar *cp = (uchar *) cmdbuf;
 
@@ -313,12 +315,12 @@ static void flash_make_cmd (flash_info_t * info, ulong cmd, void *cmdbuf)
                cword_offset = (info->portwidth-i)%info->chipwidth;
 #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
                cp_offset = info->portwidth - i;
-               val = *((uchar*)&cmd + cword_offset);
+               val = *((uchar*)&cmd_le + cword_offset);
 #else
                cp_offset = i - 1;
-               val = *((uchar*)&cmd + sizeof(ulong) - cword_offset - 1);
+               val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
 #endif
-               cp[cp_offset] = (cword_offset >= sizeof(ulong)) ? 0x00 : val;
+               cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
        }
 }
 
@@ -433,7 +435,7 @@ static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
  * Write a proper sized command to the correct address
  */
 static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
-                            uint offset, ulong cmd)
+                            uint offset, u32 cmd)
 {
 
        void *addr;
@@ -581,20 +583,16 @@ static int flash_toggle (flash_info_t * info, flash_sect_t sect,
        flash_make_cmd (info, cmd, &cword);
        switch (info->portwidth) {
        case FLASH_CFI_8BIT:
-               retval = ((flash_read8(addr) & cword.c) !=
-                         (flash_read8(addr) & cword.c));
+               retval = flash_read8(addr) != flash_read8(addr);
                break;
        case FLASH_CFI_16BIT:
-               retval = ((flash_read16(addr) & cword.w) !=
-                         (flash_read16(addr) & cword.w));
+               retval = flash_read16(addr) != flash_read16(addr);
                break;
        case FLASH_CFI_32BIT:
-               retval = ((flash_read32(addr) & cword.l) !=
-                         (flash_read32(addr) & cword.l));
+               retval = flash_read32(addr) != flash_read32(addr);
                break;
        case FLASH_CFI_64BIT:
-               retval = ((flash_read64(addr) & cword.ll) !=
-                         (flash_read64(addr) & cword.ll));
+               retval = flash_read64(addr) != flash_read64(addr);
                break;
        default:
                retval = 0;
@@ -1745,6 +1743,8 @@ ulong flash_get_size (ulong base, int banknum)
        int erase_region_count;
        struct cfi_qry qry;
 
+       memset(&qry, 0, sizeof(qry));
+
        info->ext_addr = 0;
        info->cfi_version = 0;
 #ifdef CFG_FLASH_PROTECTION
@@ -1911,12 +1911,14 @@ unsigned long flash_init (void)
        char *s = getenv("unlock");
 #endif
 
+#define BANK_BASE(i)   (((unsigned long [CFI_MAX_FLASH_BANKS])CFG_FLASH_BANKS_LIST)[i])
+
        /* Init: no FLASHes known */
        for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
                flash_info[i].flash_id = FLASH_UNKNOWN;
 
-               if (!flash_detect_legacy (bank_base[i], i))
-                       flash_get_size (bank_base[i], i);
+               if (!flash_detect_legacy (BANK_BASE(i), i))
+                       flash_get_size (BANK_BASE(i), i);
                size += flash_info[i].size;
                if (flash_info[i].flash_id == FLASH_UNKNOWN) {
 #ifndef CFG_FLASH_QUIET_TEST
@@ -2012,5 +2014,3 @@ unsigned long flash_init (void)
 #endif
        return (size);
 }
-
-#endif /* CFG_FLASH_CFI */