Merge branch 'master' of git://www.denx.de/git/u-boot-mpc83xx
[platform/kernel/u-boot.git] / drivers / mtd / cfi_flash.c
index effcce4..40fddcd 100644 (file)
@@ -239,12 +239,14 @@ static u32 flash_read32(void *addr)
        return __raw_readl(addr);
 }
 
-static u64 flash_read64(void *addr)
+static u64 __flash_read64(void *addr)
 {
        /* No architectures currently implement __raw_readq() */
        return *(volatile u64 *)addr;
 }
 
+u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
+
 /*-----------------------------------------------------------------------
  */
 #if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
@@ -365,6 +367,20 @@ static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
 }
 
 /*-----------------------------------------------------------------------
+ * read a word at a port width address, assume 16bit bus
+ */
+static inline ushort flash_read_word (flash_info_t * info, uint offset)
+{
+       ushort *addr, retval;
+
+       addr = flash_map (info, 0, offset);
+       retval = flash_read16 (addr);
+       flash_unmap (info, 0, offset, addr);
+       return retval;
+}
+
+
+/*-----------------------------------------------------------------------
  * read a long word by picking the least significant byte of each maximum
  * port size word. Swap for ppc format.
  */
@@ -527,7 +543,7 @@ static int flash_isset (flash_info_t * info, flash_sect_t sect,
                retval = ((flash_read16(addr) & cword.w) == cword.w);
                break;
        case FLASH_CFI_32BIT:
-               retval = ((flash_read16(addr) & cword.l) == cword.l);
+               retval = ((flash_read32(addr) & cword.l) == cword.l);
                break;
        case FLASH_CFI_64BIT:
                retval = ((flash_read64(addr) & cword.ll) == cword.ll);
@@ -769,7 +785,7 @@ static int flash_write_cfiword (flash_info_t * info, ulong dest,
        }
        if (!flag) {
                unmap_physmem(dstaddr, info->portwidth);
-               return 2;
+               return ERR_NOT_ERASED;
        }
 
        /* Disable interrupts which might cause a timeout here */
@@ -826,7 +842,57 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
        int retcode;
        void *src = cp;
        void *dst = map_physmem(dest, len, MAP_NOCACHE);
+       void *dst2 = dst;
+       int flag = 0;
+
+       switch (info->portwidth) {
+       case FLASH_CFI_8BIT:
+               cnt = len;
+               break;
+       case FLASH_CFI_16BIT:
+               cnt = len >> 1;
+               break;
+       case FLASH_CFI_32BIT:
+               cnt = len >> 2;
+               break;
+       case FLASH_CFI_64BIT:
+               cnt = len >> 3;
+               break;
+       default:
+               retcode = ERR_INVAL;
+               goto out_unmap;
+       }
+
+       while ((cnt-- > 0) && (flag == 0)) {
+               switch (info->portwidth) {
+               case FLASH_CFI_8BIT:
+                       flag = ((flash_read8(dst2) & flash_read8(src)) ==
+                               flash_read8(src));
+                       src += 1, dst2 += 1;
+                       break;
+               case FLASH_CFI_16BIT:
+                       flag = ((flash_read16(dst2) & flash_read16(src)) ==
+                               flash_read16(src));
+                       src += 2, dst2 += 2;
+                       break;
+               case FLASH_CFI_32BIT:
+                       flag = ((flash_read32(dst2) & flash_read32(src)) ==
+                               flash_read32(src));
+                       src += 4, dst2 += 4;
+                       break;
+               case FLASH_CFI_64BIT:
+                       flag = ((flash_read64(dst2) & flash_read64(src)) ==
+                               flash_read64(src));
+                       src += 8, dst2 += 8;
+                       break;
+               }
+       }
+       if (!flag) {
+               retcode = ERR_NOT_ERASED;
+               goto out_unmap;
+       }
 
+       src = cp;
        sector = find_sector (info, dest);
 
        switch (info->vendor) {
@@ -1130,6 +1196,27 @@ void flash_print_info (flash_info_t * info)
 }
 
 /*-----------------------------------------------------------------------
+ * This is used in a few places in write_buf() to show programming
+ * progress.  Making it a function is nasty because it needs to do side
+ * effect updates to digit and dots.  Repeated code is nasty too, so
+ * we define it once here.
+ */
+#ifdef CONFIG_FLASH_SHOW_PROGRESS
+#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
+       dots -= dots_sub; \
+       if ((scale > 0) && (dots <= 0)) { \
+               if ((digit % 5) == 0) \
+                       printf ("%d", digit / 5); \
+               else \
+                       putc ('.'); \
+               digit--; \
+               dots += scale; \
+       }
+#else
+#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
+#endif
+
+/*-----------------------------------------------------------------------
  * Copy memory to flash, returns:
  * 0 - OK
  * 1 - write timeout
@@ -1142,10 +1229,23 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
        int aln;
        cfiword_t cword;
        int i, rc;
-
 #ifdef CFG_FLASH_USE_BUFFER_WRITE
        int buffered_size;
 #endif
+#ifdef CONFIG_FLASH_SHOW_PROGRESS
+       int digit = CONFIG_FLASH_SHOW_PROGRESS;
+       int scale = 0;
+       int dots  = 0;
+
+       /*
+        * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
+        */
+       if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
+               scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
+                       CONFIG_FLASH_SHOW_PROGRESS);
+       }
+#endif
+
        /* get lower aligned address */
        wp = (addr & ~(info->portwidth - 1));
 
@@ -1169,6 +1269,7 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
                        return rc;
 
                wp += i;
+               FLASH_SHOW_PROGRESS(scale, dots, digit, i);
        }
 
        /* handle the aligned part */
@@ -1198,6 +1299,7 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
                wp += i;
                src += i;
                cnt -= i;
+               FLASH_SHOW_PROGRESS(scale, dots, digit, i);
        }
 #else
        while (cnt >= info->portwidth) {
@@ -1209,8 +1311,10 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
                        return rc;
                wp += info->portwidth;
                cnt -= info->portwidth;
+               FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
        }
 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
+
        if (cnt == 0) {
                return (0);
        }
@@ -1361,17 +1465,29 @@ static void cmdset_amd_read_jedec_ids(flash_info_t *info)
        flash_unlock_seq(info, 0);
        flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
        udelay(1000); /* some flash are slow to respond */
+
        info->manufacturer_id = flash_read_uchar (info,
                                        FLASH_OFFSET_MANUFACTURER_ID);
-       info->device_id = flash_read_uchar (info,
-                                       FLASH_OFFSET_DEVICE_ID);
-       if (info->device_id == 0x7E) {
-               /* AMD 3-byte (expanded) device ids */
-               info->device_id2 = flash_read_uchar (info,
-                                       FLASH_OFFSET_DEVICE_ID2);
-               info->device_id2 <<= 8;
-               info->device_id2 |= flash_read_uchar (info,
-                                       FLASH_OFFSET_DEVICE_ID3);
+
+       switch (info->chipwidth){
+       case FLASH_CFI_8BIT:
+               info->device_id = flash_read_uchar (info,
+                                               FLASH_OFFSET_DEVICE_ID);
+               if (info->device_id == 0x7E) {
+                       /* AMD 3-byte (expanded) device ids */
+                       info->device_id2 = flash_read_uchar (info,
+                                               FLASH_OFFSET_DEVICE_ID2);
+                       info->device_id2 <<= 8;
+                       info->device_id2 |= flash_read_uchar (info,
+                                               FLASH_OFFSET_DEVICE_ID3);
+               }
+               break;
+       case FLASH_CFI_16BIT:
+               info->device_id = flash_read_word (info,
+                                               FLASH_OFFSET_DEVICE_ID);
+               break;
+       default:
+               break;
        }
        flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
 }
@@ -1383,20 +1499,6 @@ static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
        cmdset_amd_read_jedec_ids(info);
        flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
 
-       /* check if flash geometry needs reversal */
-       if (qry->num_erase_regions > 1) {
-               /* reverse geometry if top boot part */
-               if (info->cfi_version < 0x3131) {
-                       /* CFI < 1.1, try to guess from device id */
-                       if ((info->device_id & 0x80) != 0)
-                               cfi_reverse_geometry(qry);
-               } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
-                       /* CFI >= 1.1, deduct from top/bottom flag */
-                       /* note: ext_addr is valid since cfi_version > 0 */
-                       cfi_reverse_geometry(qry);
-               }
-       }
-
        return 0;
 }
 
@@ -1410,11 +1512,11 @@ static void flash_read_jedec_ids (flash_info_t * info)
        switch (info->vendor) {
        case CFI_CMDSET_INTEL_STANDARD:
        case CFI_CMDSET_INTEL_EXTENDED:
-               flash_read_jedec_ids_intel(info);
+               cmdset_intel_read_jedec_ids(info);
                break;
        case CFI_CMDSET_AMD_STANDARD:
        case CFI_CMDSET_AMD_EXTENDED:
-               flash_read_jedec_ids_amd(info);
+               cmdset_amd_read_jedec_ids(info);
                break;
        default:
                break;
@@ -1502,7 +1604,12 @@ static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
 {
        int cfi_offset;
 
-       flash_write_cmd (info, 0, 0, info->cmd_reset);
+       /* We do not yet know what kind of commandset to use, so we issue
+          the reset command in both Intel and AMD variants, in the hope
+          that AMD flash roms ignore the Intel command. */
+       flash_write_cmd (info, 0, 0, AMD_CMD_RESET);
+       flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
+
        for (cfi_offset=0;
             cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
             cfi_offset++) {
@@ -1568,6 +1675,49 @@ static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
 }
 
 /*
+ * Manufacturer-specific quirks. Add workarounds for geometry
+ * reversal, etc. here.
+ */
+static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
+{
+       /* check if flash geometry needs reversal */
+       if (qry->num_erase_regions > 1) {
+               /* reverse geometry if top boot part */
+               if (info->cfi_version < 0x3131) {
+                       /* CFI < 1.1, try to guess from device id */
+                       if ((info->device_id & 0x80) != 0)
+                               cfi_reverse_geometry(qry);
+               } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
+                       /* CFI >= 1.1, deduct from top/bottom flag */
+                       /* note: ext_addr is valid since cfi_version > 0 */
+                       cfi_reverse_geometry(qry);
+               }
+       }
+}
+
+static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
+{
+       int reverse_geometry = 0;
+
+       /* Check the "top boot" bit in the PRI */
+       if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
+               reverse_geometry = 1;
+
+       /* AT49BV6416(T) list the erase regions in the wrong order.
+        * However, the device ID is identical with the non-broken
+        * AT49BV642D since u-boot only reads the low byte (they
+        * differ in the high byte.) So leave out this fixup for now.
+        */
+#if 0
+       if (info->device_id == 0xd6 || info->device_id == 0xd2)
+               reverse_geometry = !reverse_geometry;
+#endif
+
+       if (reverse_geometry)
+               cfi_reverse_geometry(qry);
+}
+
+/*
  * The following code cannot be run from FLASH!
  *
  */
@@ -1629,6 +1779,16 @@ ulong flash_get_size (ulong base, int banknum)
                        return 0;
                }
 
+               /* Do manufacturer-specific fixups */
+               switch (info->manufacturer_id) {
+               case 0x0001:
+                       flash_fixup_amd(info, &qry);
+                       break;
+               case 0x001f:
+                       flash_fixup_atmel(info, &qry);
+                       break;
+               }
+
                debug ("manufacturer is %d\n", info->vendor);
                debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
                debug ("device id is 0x%x\n", info->device_id);