fsl_elbc_nand: redirect the pointer of bbt pattern to RAM
[oweals/u-boot.git] / drivers / mtd / cfi_flash.c
index 84ff7e80950965dec69a5ede4c216698ca8b6b8d..d0732f53fa002f7c7cb838013b31d9e3dbd1d472 100644 (file)
@@ -273,7 +273,7 @@ u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
 /*-----------------------------------------------------------------------
  */
 #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
-static flash_info_t *flash_get_info(ulong base)
+flash_info_t *flash_get_info(ulong base)
 {
        int i;
        flash_info_t * info = 0;
@@ -305,17 +305,12 @@ flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
 {
        unsigned int byte_offset = offset * info->portwidth;
 
-       return map_physmem(info->start[sect] + byte_offset,
-                       flash_sector_size(info, sect) - byte_offset,
-                       MAP_NOCACHE);
+       return (void *)(info->start[sect] + byte_offset);
 }
 
 static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
                unsigned int offset, void *addr)
 {
-       unsigned int byte_offset = offset * info->portwidth;
-
-       unmap_physmem(addr, flash_sector_size(info, sect) - byte_offset);
 }
 
 /*-----------------------------------------------------------------------
@@ -354,7 +349,7 @@ static void print_longlong (char *str, unsigned long long data)
        int i;
        char *cp;
 
-       cp = (unsigned char *) &data;
+       cp = (char *) &data;
        for (i = 0; i < 8; i++)
                sprintf (&str[i * 2], "%2.2x", *cp++);
 }
@@ -802,13 +797,11 @@ static flash_sect_t find_sector (flash_info_t * info, ulong addr)
 static int flash_write_cfiword (flash_info_t * info, ulong dest,
                                cfiword_t cword)
 {
-       void *dstaddr;
+       void *dstaddr = (void *)dest;
        int flag;
        flash_sect_t sect = 0;
        char sect_found = 0;
 
-       dstaddr = map_physmem(dest, info->portwidth, MAP_NOCACHE);
-
        /* Check if Flash is (sufficiently) erased */
        switch (info->portwidth) {
        case FLASH_CFI_8BIT:
@@ -827,10 +820,8 @@ static int flash_write_cfiword (flash_info_t * info, ulong dest,
                flag = 0;
                break;
        }
-       if (!flag) {
-               unmap_physmem(dstaddr, info->portwidth);
+       if (!flag)
                return ERR_NOT_ERASED;
-       }
 
        /* Disable interrupts which might cause a timeout here */
        flag = disable_interrupts ();
@@ -873,8 +864,6 @@ static int flash_write_cfiword (flash_info_t * info, ulong dest,
        if (flag)
                enable_interrupts ();
 
-       unmap_physmem(dstaddr, info->portwidth);
-
        if (!sect_found)
                sect = find_sector (info, dest);
 
@@ -890,7 +879,7 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
        int cnt;
        int retcode;
        void *src = cp;
-       void *dst = map_physmem(dest, len, MAP_NOCACHE);
+       void *dst = (void *)dest;
        void *dst2 = dst;
        int flag = 0;
        uint offset = 0;
@@ -1052,7 +1041,6 @@ static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
        }
 
 out_unmap:
-       unmap_physmem(dst, len);
        return retcode;
 }
 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
@@ -1301,7 +1289,7 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
        /* handle unaligned start */
        if ((aln = addr - wp) != 0) {
                cword.l = 0;
-               p = map_physmem(wp, info->portwidth, MAP_NOCACHE);
+               p = (uchar *)wp;
                for (i = 0; i < aln; ++i)
                        flash_add_byte (info, &cword, flash_read8(p + i));
 
@@ -1313,7 +1301,6 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
                        flash_add_byte (info, &cword, flash_read8(p + i));
 
                rc = flash_write_cfiword (info, wp, cword);
-               unmap_physmem(p, info->portwidth);
                if (rc != 0)
                        return rc;
 
@@ -1372,14 +1359,13 @@ int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
         * handle unaligned tail bytes
         */
        cword.l = 0;
-       p = map_physmem(wp, info->portwidth, MAP_NOCACHE);
+       p = (uchar *)wp;
        for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
                flash_add_byte (info, &cword, *src++);
                --cnt;
        }
        for (; i < info->portwidth; ++i)
                flash_add_byte (info, &cword, flash_read8(p + i));
-       unmap_physmem(p, info->portwidth);
 
        return flash_write_cfiword (info, wp, cword);
 }
@@ -1618,7 +1604,7 @@ static void flash_read_jedec_ids (flash_info_t * info)
  * board_flash_get_legacy needs to fill in at least:
  * info->portwidth, info->chipwidth and info->interface for Jedec probing.
  */
-static int flash_detect_legacy(ulong base, int banknum)
+static int flash_detect_legacy(phys_addr_t base, int banknum)
 {
        flash_info_t *info = &flash_info[banknum];
 
@@ -1634,7 +1620,10 @@ static int flash_detect_legacy(ulong base, int banknum)
 
                        for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
                                info->vendor = modes[i];
-                               info->start[0] = base;
+                               info->start[0] =
+                                       (ulong)map_physmem(base,
+                                                          info->portwidth,
+                                                          MAP_NOCACHE);
                                if (info->portwidth == FLASH_CFI_8BIT
                                        && info->interface == FLASH_CFI_X8X16) {
                                        info->addr_unlock1 = 0x2AAA;
@@ -1648,8 +1637,11 @@ static int flash_detect_legacy(ulong base, int banknum)
                                                info->manufacturer_id,
                                                info->device_id,
                                                info->device_id2);
-                               if (jedec_flash_match(info, base))
+                               if (jedec_flash_match(info, info->start[0]))
                                        break;
+                               else
+                                       unmap_physmem((void *)info->start[0],
+                                                     MAP_NOCACHE);
                        }
                }
 
@@ -1671,7 +1663,7 @@ static int flash_detect_legacy(ulong base, int banknum)
        return 0; /* use CFI */
 }
 #else
-static inline int flash_detect_legacy(ulong base, int banknum)
+static inline int flash_detect_legacy(phys_addr_t base, int banknum)
 {
        return 0; /* use CFI */
 }
@@ -1796,13 +1788,10 @@ static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
 
        /* 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.
+        * AT49BV642D they differ in the high byte.
         */
-#if 0
        if (info->device_id == 0xd6 || info->device_id == 0xd2)
                reverse_geometry = !reverse_geometry;
-#endif
 
        if (reverse_geometry)
                cfi_reverse_geometry(qry);
@@ -1814,8 +1803,9 @@ static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
        if (qry->num_erase_regions > 1) {
                /* reverse geometry if top boot part */
                if (info->cfi_version < 0x3131) {
-                       /* CFI < 1.1, guess by device id (only M29W320ET now) */
-                       if (info->device_id == 0x2256) {
+                       /* CFI < 1.1, guess by device id (M29W320{DT,ET} only) */
+                       if (info->device_id == 0x22CA ||
+                           info->device_id == 0x2256) {
                                cfi_reverse_geometry(qry);
                        }
                }
@@ -1826,12 +1816,12 @@ static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
  * The following code cannot be run from FLASH!
  *
  */
-ulong flash_get_size (ulong base, int banknum)
+ulong flash_get_size (phys_addr_t base, int banknum)
 {
        flash_info_t *info = &flash_info[banknum];
        int i, j;
        flash_sect_t sect_cnt;
-       unsigned long sector;
+       phys_addr_t sector;
        unsigned long tmp;
        int size_ratio;
        uchar num_erase_regions;
@@ -1847,7 +1837,7 @@ ulong flash_get_size (ulong base, int banknum)
        info->legacy_unlock = 0;
 #endif
 
-       info->start[0] = base;
+       info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
 
        if (flash_detect_cfi (info, &qry)) {
                info->vendor = le16_to_cpu(qry.p_id);
@@ -1939,7 +1929,10 @@ ulong flash_get_size (ulong base, int banknum)
                                        printf("ERROR: too many flash sectors\n");
                                        break;
                                }
-                               info->start[sect_cnt] = sector;
+                               info->start[sect_cnt] =
+                                       (ulong)map_physmem(sector,
+                                                          info->portwidth,
+                                                          MAP_NOCACHE);
                                sector += (erase_region_size * size_ratio);
 
                                /*
@@ -2013,10 +2006,12 @@ unsigned long flash_init (void)
 #endif
 
 #ifdef CONFIG_SYS_FLASH_PROTECTION
-       char *s = getenv("unlock");
+       /* read environment from EEPROM */
+       char s[64];
+       getenv_r ("unlock", s, sizeof(s));
 #endif
 
-#define BANK_BASE(i)   (((unsigned long [CFI_MAX_FLASH_BANKS])CONFIG_SYS_FLASH_BANKS_LIST)[i])
+#define BANK_BASE(i)   (((phys_addr_t [CFI_MAX_FLASH_BANKS])CONFIG_SYS_FLASH_BANKS_LIST)[i])
 
        /* Init: no FLASHes known */
        for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
@@ -2103,7 +2098,7 @@ unsigned long flash_init (void)
 #ifdef CONFIG_ENV_ADDR_REDUND
        flash_protect (FLAG_PROTECT_SET,
                       CONFIG_ENV_ADDR_REDUND,
-                      CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SIZE_REDUND - 1,
+                      CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
                       flash_get_info(CONFIG_ENV_ADDR_REDUND));
 #endif