wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1 | /* |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2 | * (C) Copyright 2002-2004 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 3 | * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com |
| 4 | * |
| 5 | * Copyright (C) 2003 Arabella Software Ltd. |
| 6 | * Yuli Barcohen <yuli@arabellasw.com> |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 7 | * |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 8 | * Copyright (C) 2004 |
| 9 | * Ed Okerson |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 |
| 12 | * Tolunay Orkun <listmember@orkun.us> |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 13 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* The DEBUG define must be before common to enable debugging */ |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 18 | /* #define DEBUG */ |
| 19 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 20 | #include <common.h> |
| 21 | #include <asm/processor.h> |
Haiying Wang | c123a38 | 2007-02-21 16:52:31 +0100 | [diff] [blame] | 22 | #include <asm/io.h> |
wdenk | aeba06f | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 23 | #include <asm/byteorder.h> |
Andrew Gabbasov | c159258 | 2013-05-14 12:27:52 -0500 | [diff] [blame] | 24 | #include <asm/unaligned.h> |
wdenk | d0245fc | 2005-04-13 10:02:42 +0000 | [diff] [blame] | 25 | #include <environment.h> |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 26 | #include <mtd/cfi_flash.h> |
Jens Scharsig (BuS Elektronik) | 287a158 | 2012-01-27 09:29:53 +0100 | [diff] [blame] | 27 | #include <watchdog.h> |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 28 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 29 | /* |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 30 | * This file implements a Common Flash Interface (CFI) driver for |
| 31 | * U-Boot. |
| 32 | * |
| 33 | * The width of the port and the width of the chips are determined at |
| 34 | * initialization. These widths are used to calculate the address for |
| 35 | * access CFI data structures. |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 36 | * |
| 37 | * References |
| 38 | * JEDEC Standard JESD68 - Common Flash Interface (CFI) |
| 39 | * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes |
| 40 | * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets |
| 41 | * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 42 | * AMD CFI Specification, Release 2.0 December 1, 2001 |
| 43 | * AMD/Spansion Application Note: Migration from Single-byte to Three-byte |
| 44 | * Device IDs, Publication Number 25538 Revision A, November 8, 2001 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 45 | * |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between |
Heiko Schocher | 800db31 | 2007-01-19 18:05:26 +0100 | [diff] [blame] | 47 | * reading and writing ... (yes there is such a Hardware). |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 50 | static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT }; |
Mike Frysinger | c2c093d | 2010-12-22 09:41:13 -0500 | [diff] [blame] | 51 | #ifdef CONFIG_FLASH_CFI_MTD |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 52 | static uint flash_verbose = 1; |
Mike Frysinger | c2c093d | 2010-12-22 09:41:13 -0500 | [diff] [blame] | 53 | #else |
| 54 | #define flash_verbose 1 |
| 55 | #endif |
Wolfgang Denk | afa0dd0 | 2006-12-27 01:26:13 +0100 | [diff] [blame] | 56 | |
Wolfgang Denk | 9f5fb0f | 2008-08-08 16:39:54 +0200 | [diff] [blame] | 57 | flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */ |
| 58 | |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 59 | /* |
| 60 | * Check if chip width is defined. If not, start detecting with 8bit. |
| 61 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | #ifndef CONFIG_SYS_FLASH_CFI_WIDTH |
| 63 | #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 64 | #endif |
| 65 | |
Stefan Roese | ab93564 | 2010-10-25 18:31:48 +0200 | [diff] [blame] | 66 | /* |
| 67 | * 0xffff is an undefined value for the configuration register. When |
| 68 | * this value is returned, the configuration register shall not be |
| 69 | * written at all (default mode). |
| 70 | */ |
| 71 | static u16 cfi_flash_config_reg(int i) |
| 72 | { |
| 73 | #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS |
| 74 | return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i]; |
| 75 | #else |
| 76 | return 0xffff; |
| 77 | #endif |
| 78 | } |
| 79 | |
Stefan Roese | fb9a730 | 2010-08-31 10:00:10 +0200 | [diff] [blame] | 80 | #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT) |
| 81 | int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT; |
| 82 | #endif |
| 83 | |
Stefan Roese | 7e7dda8 | 2010-08-30 10:11:51 +0200 | [diff] [blame] | 84 | static phys_addr_t __cfi_flash_bank_addr(int i) |
| 85 | { |
| 86 | return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i]; |
| 87 | } |
| 88 | phys_addr_t cfi_flash_bank_addr(int i) |
| 89 | __attribute__((weak, alias("__cfi_flash_bank_addr"))); |
| 90 | |
Ilya Yanok | 755c180 | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 91 | static unsigned long __cfi_flash_bank_size(int i) |
| 92 | { |
| 93 | #ifdef CONFIG_SYS_FLASH_BANKS_SIZES |
| 94 | return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i]; |
| 95 | #else |
| 96 | return 0; |
| 97 | #endif |
| 98 | } |
| 99 | unsigned long cfi_flash_bank_size(int i) |
| 100 | __attribute__((weak, alias("__cfi_flash_bank_size"))); |
| 101 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 102 | static void __flash_write8(u8 value, void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 103 | { |
| 104 | __raw_writeb(value, addr); |
| 105 | } |
| 106 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 107 | static void __flash_write16(u16 value, void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 108 | { |
| 109 | __raw_writew(value, addr); |
| 110 | } |
| 111 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 112 | static void __flash_write32(u32 value, void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 113 | { |
| 114 | __raw_writel(value, addr); |
| 115 | } |
| 116 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 117 | static void __flash_write64(u64 value, void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 118 | { |
| 119 | /* No architectures currently implement __raw_writeq() */ |
| 120 | *(volatile u64 *)addr = value; |
| 121 | } |
| 122 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 123 | static u8 __flash_read8(void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 124 | { |
| 125 | return __raw_readb(addr); |
| 126 | } |
| 127 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 128 | static u16 __flash_read16(void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 129 | { |
| 130 | return __raw_readw(addr); |
| 131 | } |
| 132 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 133 | static u32 __flash_read32(void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 134 | { |
| 135 | return __raw_readl(addr); |
| 136 | } |
| 137 | |
Daniel Hellstrom | cfd7138 | 2008-03-28 20:40:19 +0100 | [diff] [blame] | 138 | static u64 __flash_read64(void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 139 | { |
| 140 | /* No architectures currently implement __raw_readq() */ |
| 141 | return *(volatile u64 *)addr; |
| 142 | } |
| 143 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 144 | #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS |
| 145 | void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8"))); |
| 146 | void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16"))); |
| 147 | void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32"))); |
| 148 | void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64"))); |
| 149 | u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8"))); |
| 150 | u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16"))); |
| 151 | u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32"))); |
Daniel Hellstrom | cfd7138 | 2008-03-28 20:40:19 +0100 | [diff] [blame] | 152 | u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64"))); |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 153 | #else |
| 154 | #define flash_write8 __flash_write8 |
| 155 | #define flash_write16 __flash_write16 |
| 156 | #define flash_write32 __flash_write32 |
| 157 | #define flash_write64 __flash_write64 |
| 158 | #define flash_read8 __flash_read8 |
| 159 | #define flash_read16 __flash_read16 |
| 160 | #define flash_read32 __flash_read32 |
| 161 | #define flash_read64 __flash_read64 |
| 162 | #endif |
Daniel Hellstrom | cfd7138 | 2008-03-28 20:40:19 +0100 | [diff] [blame] | 163 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 164 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 165 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 166 | #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) |
Heiko Schocher | 4c0f005 | 2009-02-10 09:53:29 +0100 | [diff] [blame] | 167 | flash_info_t *flash_get_info(ulong base) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 168 | { |
| 169 | int i; |
Masahiro Yamada | c663b9a | 2013-05-17 14:50:37 +0900 | [diff] [blame] | 170 | flash_info_t *info; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 171 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 172 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
Masahiro Yamada | 44049f3 | 2013-05-17 14:50:36 +0900 | [diff] [blame] | 173 | info = &flash_info[i]; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 174 | if (info->size && info->start[0] <= base && |
| 175 | base <= info->start[0] + info->size - 1) |
Masahiro Yamada | c663b9a | 2013-05-17 14:50:37 +0900 | [diff] [blame] | 176 | return info; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 177 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 178 | |
Masahiro Yamada | c663b9a | 2013-05-17 14:50:37 +0900 | [diff] [blame] | 179 | return NULL; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 180 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 181 | #endif |
| 182 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 183 | unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect) |
| 184 | { |
| 185 | if (sect != (info->sector_count - 1)) |
| 186 | return info->start[sect + 1] - info->start[sect]; |
| 187 | else |
| 188 | return info->start[0] + info->size - info->start[sect]; |
| 189 | } |
| 190 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 191 | /*----------------------------------------------------------------------- |
| 192 | * create an address based on the offset and the port width |
| 193 | */ |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 194 | static inline void * |
| 195 | flash_map (flash_info_t * info, flash_sect_t sect, uint offset) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 196 | { |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 197 | unsigned int byte_offset = offset * info->portwidth; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 198 | |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 199 | return (void *)(info->start[sect] + byte_offset); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static inline void flash_unmap(flash_info_t *info, flash_sect_t sect, |
| 203 | unsigned int offset, void *addr) |
| 204 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 207 | /*----------------------------------------------------------------------- |
| 208 | * make a proper sized command based on the port and chip widths |
| 209 | */ |
Sebastian Siewior | 7746ed8 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 210 | static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 211 | { |
| 212 | int i; |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 213 | int cword_offset; |
| 214 | int cp_offset; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 215 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Sebastian Siewior | d528cd6 | 2008-07-16 20:04:49 +0200 | [diff] [blame] | 216 | u32 cmd_le = cpu_to_le32(cmd); |
| 217 | #endif |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 218 | uchar val; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 219 | uchar *cp = (uchar *) cmdbuf; |
| 220 | |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 221 | for (i = info->portwidth; i > 0; i--){ |
| 222 | cword_offset = (info->portwidth-i)%info->chipwidth; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 223 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 224 | cp_offset = info->portwidth - i; |
Sebastian Siewior | d528cd6 | 2008-07-16 20:04:49 +0200 | [diff] [blame] | 225 | val = *((uchar*)&cmd_le + cword_offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 226 | #else |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 227 | cp_offset = i - 1; |
Sebastian Siewior | 7746ed8 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 228 | val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 229 | #endif |
Sebastian Siewior | 7746ed8 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 230 | cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val; |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 231 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 232 | } |
| 233 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 234 | #ifdef DEBUG |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 235 | /*----------------------------------------------------------------------- |
| 236 | * Debug support |
| 237 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 238 | static void print_longlong (char *str, unsigned long long data) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 239 | { |
| 240 | int i; |
| 241 | char *cp; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 242 | |
Wolfgang Denk | 49f4f6a | 2009-02-04 09:42:20 +0100 | [diff] [blame] | 243 | cp = (char *) &data; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 244 | for (i = 0; i < 8; i++) |
| 245 | sprintf (&str[i * 2], "%2.2x", *cp++); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 246 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 247 | |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 248 | static void flash_printqry (struct cfi_qry *qry) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 249 | { |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 250 | u8 *p = (u8 *)qry; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 251 | int x, y; |
| 252 | |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 253 | for (x = 0; x < sizeof(struct cfi_qry); x += 16) { |
| 254 | debug("%02x : ", x); |
| 255 | for (y = 0; y < 16; y++) |
| 256 | debug("%2.2x ", p[x + y]); |
| 257 | debug(" "); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 258 | for (y = 0; y < 16; y++) { |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 259 | unsigned char c = p[x + y]; |
| 260 | if (c >= 0x20 && c <= 0x7e) |
| 261 | debug("%c", c); |
| 262 | else |
| 263 | debug("."); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 264 | } |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 265 | debug("\n"); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 268 | #endif |
| 269 | |
| 270 | |
| 271 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 272 | * read a character at a port width address |
| 273 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 274 | static inline uchar flash_read_uchar (flash_info_t * info, uint offset) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 275 | { |
| 276 | uchar *cp; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 277 | uchar retval; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 278 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 279 | cp = flash_map (info, 0, offset); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 280 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 281 | retval = flash_read8(cp); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 282 | #else |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 283 | retval = flash_read8(cp + info->portwidth - 1); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 284 | #endif |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 285 | flash_unmap (info, 0, offset, cp); |
| 286 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /*----------------------------------------------------------------------- |
Tor Krill | 7f2a305 | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 290 | * read a word at a port width address, assume 16bit bus |
| 291 | */ |
| 292 | static inline ushort flash_read_word (flash_info_t * info, uint offset) |
| 293 | { |
| 294 | ushort *addr, retval; |
| 295 | |
| 296 | addr = flash_map (info, 0, offset); |
| 297 | retval = flash_read16 (addr); |
| 298 | flash_unmap (info, 0, offset, addr); |
| 299 | return retval; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /*----------------------------------------------------------------------- |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 304 | * read a long word by picking the least significant byte of each maximum |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 305 | * port size word. Swap for ppc format. |
| 306 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 307 | static ulong flash_read_long (flash_info_t * info, flash_sect_t sect, |
| 308 | uint offset) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 309 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 310 | uchar *addr; |
| 311 | ulong retval; |
| 312 | |
| 313 | #ifdef DEBUG |
| 314 | int x; |
| 315 | #endif |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 316 | addr = flash_map (info, sect, offset); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 317 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 318 | #ifdef DEBUG |
| 319 | debug ("long addr is at %p info->portwidth = %d\n", addr, |
| 320 | info->portwidth); |
| 321 | for (x = 0; x < 4 * info->portwidth; x++) { |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 322 | debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x)); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 323 | } |
| 324 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 325 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 326 | retval = ((flash_read8(addr) << 16) | |
| 327 | (flash_read8(addr + info->portwidth) << 24) | |
| 328 | (flash_read8(addr + 2 * info->portwidth)) | |
| 329 | (flash_read8(addr + 3 * info->portwidth) << 8)); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 330 | #else |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 331 | retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) | |
| 332 | (flash_read8(addr + info->portwidth - 1) << 16) | |
| 333 | (flash_read8(addr + 4 * info->portwidth - 1) << 8) | |
| 334 | (flash_read8(addr + 3 * info->portwidth - 1))); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 335 | #endif |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 336 | flash_unmap(info, sect, offset, addr); |
| 337 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 338 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 341 | /* |
| 342 | * Write a proper sized command to the correct address |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 343 | */ |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 344 | void flash_write_cmd (flash_info_t * info, flash_sect_t sect, |
| 345 | uint offset, u32 cmd) |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 346 | { |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 347 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 348 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 349 | cfiword_t cword; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 350 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 351 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 352 | flash_make_cmd (info, cmd, &cword); |
| 353 | switch (info->portwidth) { |
| 354 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 355 | debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 356 | cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 357 | flash_write8(cword.c, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 358 | break; |
| 359 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 360 | debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 361 | cmd, cword.w, |
| 362 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 363 | flash_write16(cword.w, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 364 | break; |
| 365 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 366 | debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 367 | cmd, cword.l, |
| 368 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 369 | flash_write32(cword.l, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 370 | break; |
| 371 | case FLASH_CFI_64BIT: |
| 372 | #ifdef DEBUG |
| 373 | { |
| 374 | char str[20]; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 375 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 376 | print_longlong (str, cword.ll); |
| 377 | |
| 378 | debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n", |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 379 | addr, cmd, str, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 380 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 381 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 382 | #endif |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 383 | flash_write64(cword.ll, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 384 | break; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 385 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 386 | |
| 387 | /* Ensure all the instructions are fully finished */ |
| 388 | sync(); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 389 | |
| 390 | flash_unmap(info, sect, offset, addr); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 391 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 392 | |
| 393 | static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect) |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 394 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 395 | flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START); |
| 396 | flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 397 | } |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 398 | |
| 399 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 400 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 401 | static int flash_isequal (flash_info_t * info, flash_sect_t sect, |
| 402 | uint offset, uchar cmd) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 403 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 404 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 405 | cfiword_t cword; |
| 406 | int retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 407 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 408 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 409 | flash_make_cmd (info, cmd, &cword); |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 410 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 411 | debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 412 | switch (info->portwidth) { |
| 413 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 414 | debug ("is= %x %x\n", flash_read8(addr), cword.c); |
| 415 | retval = (flash_read8(addr) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 416 | break; |
| 417 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 418 | debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w); |
| 419 | retval = (flash_read16(addr) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 420 | break; |
| 421 | case FLASH_CFI_32BIT: |
Andrew Klossner | 7ddfafc | 2008-08-21 07:12:26 -0700 | [diff] [blame] | 422 | debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 423 | retval = (flash_read32(addr) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 424 | break; |
| 425 | case FLASH_CFI_64BIT: |
| 426 | #ifdef DEBUG |
| 427 | { |
| 428 | char str1[20]; |
| 429 | char str2[20]; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 430 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 431 | print_longlong (str1, flash_read64(addr)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 432 | print_longlong (str2, cword.ll); |
| 433 | debug ("is= %s %s\n", str1, str2); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 434 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 435 | #endif |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 436 | retval = (flash_read64(addr) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 437 | break; |
| 438 | default: |
| 439 | retval = 0; |
| 440 | break; |
| 441 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 442 | flash_unmap(info, sect, offset, addr); |
| 443 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 444 | return retval; |
| 445 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 446 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 447 | /*----------------------------------------------------------------------- |
| 448 | */ |
| 449 | static int flash_isset (flash_info_t * info, flash_sect_t sect, |
| 450 | uint offset, uchar cmd) |
| 451 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 452 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 453 | cfiword_t cword; |
| 454 | int retval; |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 455 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 456 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 457 | flash_make_cmd (info, cmd, &cword); |
| 458 | switch (info->portwidth) { |
| 459 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 460 | retval = ((flash_read8(addr) & cword.c) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 461 | break; |
| 462 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 463 | retval = ((flash_read16(addr) & cword.w) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 464 | break; |
| 465 | case FLASH_CFI_32BIT: |
Stefan Roese | d4e37c0 | 2008-01-02 14:05:37 +0100 | [diff] [blame] | 466 | retval = ((flash_read32(addr) & cword.l) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 467 | break; |
| 468 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 469 | retval = ((flash_read64(addr) & cword.ll) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 470 | break; |
| 471 | default: |
| 472 | retval = 0; |
| 473 | break; |
| 474 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 475 | flash_unmap(info, sect, offset, addr); |
| 476 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 477 | return retval; |
| 478 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 479 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 480 | /*----------------------------------------------------------------------- |
| 481 | */ |
| 482 | static int flash_toggle (flash_info_t * info, flash_sect_t sect, |
| 483 | uint offset, uchar cmd) |
| 484 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 485 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 486 | cfiword_t cword; |
| 487 | int retval; |
wdenk | e85b7a5 | 2004-10-10 22:16:06 +0000 | [diff] [blame] | 488 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 489 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 490 | flash_make_cmd (info, cmd, &cword); |
| 491 | switch (info->portwidth) { |
| 492 | case FLASH_CFI_8BIT: |
Stefan Roese | cff2b49 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 493 | retval = flash_read8(addr) != flash_read8(addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 494 | break; |
| 495 | case FLASH_CFI_16BIT: |
Stefan Roese | cff2b49 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 496 | retval = flash_read16(addr) != flash_read16(addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 497 | break; |
| 498 | case FLASH_CFI_32BIT: |
Stefan Roese | cff2b49 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 499 | retval = flash_read32(addr) != flash_read32(addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 500 | break; |
| 501 | case FLASH_CFI_64BIT: |
Wolfgang Denk | 600e183 | 2008-10-31 01:12:28 +0100 | [diff] [blame] | 502 | retval = ( (flash_read32( addr ) != flash_read32( addr )) || |
| 503 | (flash_read32(addr+4) != flash_read32(addr+4)) ); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 504 | break; |
| 505 | default: |
| 506 | retval = 0; |
| 507 | break; |
| 508 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 509 | flash_unmap(info, sect, offset, addr); |
| 510 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 511 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 514 | /* |
| 515 | * flash_is_busy - check to see if the flash is busy |
| 516 | * |
| 517 | * This routine checks the status of the chip and returns true if the |
| 518 | * chip is busy. |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 519 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 520 | static int flash_is_busy (flash_info_t * info, flash_sect_t sect) |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 521 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 522 | int retval; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 523 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 524 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 525 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 526 | case CFI_CMDSET_INTEL_STANDARD: |
| 527 | case CFI_CMDSET_INTEL_EXTENDED: |
| 528 | retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE); |
| 529 | break; |
| 530 | case CFI_CMDSET_AMD_STANDARD: |
| 531 | case CFI_CMDSET_AMD_EXTENDED: |
| 532 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 533 | case CFI_CMDSET_AMD_LEGACY: |
| 534 | #endif |
| 535 | retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE); |
| 536 | break; |
| 537 | default: |
| 538 | retval = 0; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 539 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 540 | debug ("flash_is_busy: %d\n", retval); |
| 541 | return retval; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 545 | * wait for XSR.7 to be set. Time out with an error if it does not. |
| 546 | * This routine does not set the flash to read-array mode. |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 547 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 548 | static int flash_status_check (flash_info_t * info, flash_sect_t sector, |
| 549 | ulong tout, char *prompt) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 550 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 551 | ulong start; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 552 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 553 | #if CONFIG_SYS_HZ != 1000 |
Renato Andreola | ac6693d | 2010-03-24 23:00:47 +0800 | [diff] [blame] | 554 | if ((ulong)CONFIG_SYS_HZ > 100000) |
| 555 | tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */ |
| 556 | else |
| 557 | tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 558 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 559 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 560 | /* Wait for command completion */ |
Graeme Russ | 13ec42b | 2011-07-15 02:18:56 +0000 | [diff] [blame] | 561 | #ifdef CONFIG_SYS_LOW_RES_TIMER |
Thomas Chou | 4b7e668 | 2010-04-01 11:15:05 +0800 | [diff] [blame] | 562 | reset_timer(); |
Graeme Russ | 13ec42b | 2011-07-15 02:18:56 +0000 | [diff] [blame] | 563 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 564 | start = get_timer (0); |
Jens Scharsig (BuS Elektronik) | 287a158 | 2012-01-27 09:29:53 +0100 | [diff] [blame] | 565 | WATCHDOG_RESET(); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 566 | while (flash_is_busy (info, sector)) { |
| 567 | if (get_timer (start) > tout) { |
| 568 | printf ("Flash %s timeout at address %lx data %lx\n", |
| 569 | prompt, info->start[sector], |
| 570 | flash_read_long (info, sector, 0)); |
| 571 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 572 | udelay(1); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 573 | return ERR_TIMOUT; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 574 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 575 | udelay (1); /* also triggers watchdog */ |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 576 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 577 | return ERR_OK; |
| 578 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 579 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 580 | /*----------------------------------------------------------------------- |
| 581 | * Wait for XSR.7 to be set, if it times out print an error, otherwise |
| 582 | * do a full status check. |
| 583 | * |
| 584 | * This routine sets the flash to read-array mode. |
| 585 | */ |
| 586 | static int flash_full_status_check (flash_info_t * info, flash_sect_t sector, |
| 587 | ulong tout, char *prompt) |
| 588 | { |
| 589 | int retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 590 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 591 | retcode = flash_status_check (info, sector, tout, prompt); |
| 592 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 593 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 594 | case CFI_CMDSET_INTEL_EXTENDED: |
| 595 | case CFI_CMDSET_INTEL_STANDARD: |
Ed Swarthout | 2da1410 | 2008-10-09 01:26:36 -0500 | [diff] [blame] | 596 | if ((retcode != ERR_OK) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 597 | && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) { |
| 598 | retcode = ERR_INVAL; |
| 599 | printf ("Flash %s error at address %lx\n", prompt, |
| 600 | info->start[sector]); |
| 601 | if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | |
| 602 | FLASH_STATUS_PSLBS)) { |
| 603 | puts ("Command Sequence Error.\n"); |
| 604 | } else if (flash_isset (info, sector, 0, |
| 605 | FLASH_STATUS_ECLBS)) { |
| 606 | puts ("Block Erase Error.\n"); |
| 607 | retcode = ERR_NOT_ERASED; |
| 608 | } else if (flash_isset (info, sector, 0, |
| 609 | FLASH_STATUS_PSLBS)) { |
| 610 | puts ("Locking Error\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 611 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 612 | if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) { |
| 613 | puts ("Block locked.\n"); |
| 614 | retcode = ERR_PROTECTED; |
| 615 | } |
| 616 | if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS)) |
| 617 | puts ("Vpp Low Error.\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 618 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 619 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
Aaron Williams | ffcef2f | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 620 | udelay(1); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 621 | break; |
| 622 | default: |
| 623 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 624 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 625 | return retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 628 | static int use_flash_status_poll(flash_info_t *info) |
| 629 | { |
| 630 | #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL |
| 631 | if (info->vendor == CFI_CMDSET_AMD_EXTENDED || |
| 632 | info->vendor == CFI_CMDSET_AMD_STANDARD) |
| 633 | return 1; |
| 634 | #endif |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | static int flash_status_poll(flash_info_t *info, void *src, void *dst, |
| 639 | ulong tout, char *prompt) |
| 640 | { |
| 641 | #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL |
| 642 | ulong start; |
| 643 | int ready; |
| 644 | |
| 645 | #if CONFIG_SYS_HZ != 1000 |
| 646 | if ((ulong)CONFIG_SYS_HZ > 100000) |
| 647 | tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */ |
| 648 | else |
| 649 | tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000); |
| 650 | #endif |
| 651 | |
| 652 | /* Wait for command completion */ |
Graeme Russ | 13ec42b | 2011-07-15 02:18:56 +0000 | [diff] [blame] | 653 | #ifdef CONFIG_SYS_LOW_RES_TIMER |
Thomas Chou | 4b7e668 | 2010-04-01 11:15:05 +0800 | [diff] [blame] | 654 | reset_timer(); |
Graeme Russ | 13ec42b | 2011-07-15 02:18:56 +0000 | [diff] [blame] | 655 | #endif |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 656 | start = get_timer(0); |
Jens Scharsig (BuS Elektronik) | 287a158 | 2012-01-27 09:29:53 +0100 | [diff] [blame] | 657 | WATCHDOG_RESET(); |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 658 | while (1) { |
| 659 | switch (info->portwidth) { |
| 660 | case FLASH_CFI_8BIT: |
| 661 | ready = flash_read8(dst) == flash_read8(src); |
| 662 | break; |
| 663 | case FLASH_CFI_16BIT: |
| 664 | ready = flash_read16(dst) == flash_read16(src); |
| 665 | break; |
| 666 | case FLASH_CFI_32BIT: |
| 667 | ready = flash_read32(dst) == flash_read32(src); |
| 668 | break; |
| 669 | case FLASH_CFI_64BIT: |
| 670 | ready = flash_read64(dst) == flash_read64(src); |
| 671 | break; |
| 672 | default: |
| 673 | ready = 0; |
| 674 | break; |
| 675 | } |
| 676 | if (ready) |
| 677 | break; |
| 678 | if (get_timer(start) > tout) { |
| 679 | printf("Flash %s timeout at address %lx data %lx\n", |
| 680 | prompt, (ulong)dst, (ulong)flash_read8(dst)); |
| 681 | return ERR_TIMOUT; |
| 682 | } |
| 683 | udelay(1); /* also triggers watchdog */ |
| 684 | } |
| 685 | #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */ |
| 686 | return ERR_OK; |
| 687 | } |
| 688 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 689 | /*----------------------------------------------------------------------- |
| 690 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 691 | static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 692 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 693 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 694 | unsigned short w; |
| 695 | unsigned int l; |
| 696 | unsigned long long ll; |
| 697 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 698 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 699 | switch (info->portwidth) { |
| 700 | case FLASH_CFI_8BIT: |
| 701 | cword->c = c; |
| 702 | break; |
| 703 | case FLASH_CFI_16BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 704 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 705 | w = c; |
| 706 | w <<= 8; |
| 707 | cword->w = (cword->w >> 8) | w; |
| 708 | #else |
| 709 | cword->w = (cword->w << 8) | c; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 710 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 711 | break; |
| 712 | case FLASH_CFI_32BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 713 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 714 | l = c; |
| 715 | l <<= 24; |
| 716 | cword->l = (cword->l >> 8) | l; |
| 717 | #else |
| 718 | cword->l = (cword->l << 8) | c; |
| 719 | #endif |
| 720 | break; |
| 721 | case FLASH_CFI_64BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 722 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 723 | ll = c; |
| 724 | ll <<= 56; |
| 725 | cword->ll = (cword->ll >> 8) | ll; |
| 726 | #else |
| 727 | cword->ll = (cword->ll << 8) | c; |
| 728 | #endif |
| 729 | break; |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 730 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 731 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 732 | |
Jens Gehrlein | 8ee4add | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 733 | /* |
| 734 | * Loop through the sector table starting from the previously found sector. |
| 735 | * Searches forwards or backwards, dependent on the passed address. |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 736 | */ |
| 737 | static flash_sect_t find_sector (flash_info_t * info, ulong addr) |
| 738 | { |
Kim Phillips | d303b86 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 739 | static flash_sect_t saved_sector; /* previously found sector */ |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 740 | static flash_info_t *saved_info; /* previously used flash bank */ |
Jens Gehrlein | 8ee4add | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 741 | flash_sect_t sector = saved_sector; |
| 742 | |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 743 | if ((info != saved_info) || (sector >= info->sector_count)) |
| 744 | sector = 0; |
| 745 | |
Jens Gehrlein | 8ee4add | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 746 | while ((info->start[sector] < addr) |
| 747 | && (sector < info->sector_count - 1)) |
| 748 | sector++; |
| 749 | while ((info->start[sector] > addr) && (sector > 0)) |
| 750 | /* |
| 751 | * also decrements the sector in case of an overshot |
| 752 | * in the first loop |
| 753 | */ |
| 754 | sector--; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 755 | |
Jens Gehrlein | 8ee4add | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 756 | saved_sector = sector; |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 757 | saved_info = info; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 758 | return sector; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 762 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 763 | static int flash_write_cfiword (flash_info_t * info, ulong dest, |
| 764 | cfiword_t cword) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 765 | { |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 766 | void *dstaddr = (void *)dest; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 767 | int flag; |
Jens Gehrlein | 1e814cf | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 768 | flash_sect_t sect = 0; |
| 769 | char sect_found = 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 770 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 771 | /* Check if Flash is (sufficiently) erased */ |
| 772 | switch (info->portwidth) { |
| 773 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 774 | flag = ((flash_read8(dstaddr) & cword.c) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 775 | break; |
| 776 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 777 | flag = ((flash_read16(dstaddr) & cword.w) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 778 | break; |
| 779 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 780 | flag = ((flash_read32(dstaddr) & cword.l) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 781 | break; |
| 782 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 783 | flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 784 | break; |
| 785 | default: |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 786 | flag = 0; |
| 787 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 788 | } |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 789 | if (!flag) |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 790 | return ERR_NOT_ERASED; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 791 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 792 | /* Disable interrupts which might cause a timeout here */ |
| 793 | flag = disable_interrupts (); |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 794 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 795 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 796 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 797 | case CFI_CMDSET_INTEL_EXTENDED: |
| 798 | case CFI_CMDSET_INTEL_STANDARD: |
| 799 | flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS); |
| 800 | flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE); |
| 801 | break; |
| 802 | case CFI_CMDSET_AMD_EXTENDED: |
| 803 | case CFI_CMDSET_AMD_STANDARD: |
Ed Swarthout | 2da1410 | 2008-10-09 01:26:36 -0500 | [diff] [blame] | 804 | sect = find_sector(info, dest); |
| 805 | flash_unlock_seq (info, sect); |
| 806 | flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE); |
Jens Gehrlein | 1e814cf | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 807 | sect_found = 1; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 808 | break; |
Po-Yu Chuang | 3f48340 | 2009-07-10 18:03:57 +0800 | [diff] [blame] | 809 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 810 | case CFI_CMDSET_AMD_LEGACY: |
| 811 | sect = find_sector(info, dest); |
| 812 | flash_unlock_seq (info, 0); |
| 813 | flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE); |
| 814 | sect_found = 1; |
| 815 | break; |
| 816 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 819 | switch (info->portwidth) { |
| 820 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 821 | flash_write8(cword.c, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 822 | break; |
| 823 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 824 | flash_write16(cword.w, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 825 | break; |
| 826 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 827 | flash_write32(cword.l, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 828 | break; |
| 829 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 830 | flash_write64(cword.ll, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 831 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 834 | /* re-enable interrupts if necessary */ |
| 835 | if (flag) |
| 836 | enable_interrupts (); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 837 | |
Jens Gehrlein | 1e814cf | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 838 | if (!sect_found) |
| 839 | sect = find_sector (info, dest); |
| 840 | |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 841 | if (use_flash_status_poll(info)) |
| 842 | return flash_status_poll(info, &cword, dstaddr, |
| 843 | info->write_tout, "write"); |
| 844 | else |
| 845 | return flash_full_status_check(info, sect, |
| 846 | info->write_tout, "write"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 849 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 850 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 851 | static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, |
| 852 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 853 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 854 | flash_sect_t sector; |
| 855 | int cnt; |
| 856 | int retcode; |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 857 | void *src = cp; |
Stefan Roese | 42c6ace | 2009-02-05 11:25:57 +0100 | [diff] [blame] | 858 | void *dst = (void *)dest; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 859 | void *dst2 = dst; |
Tao Hou | dd3b455 | 2012-03-15 23:33:58 +0800 | [diff] [blame] | 860 | int flag = 1; |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 861 | uint offset = 0; |
| 862 | unsigned int shift; |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 863 | uchar write_cmd; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 864 | |
| 865 | switch (info->portwidth) { |
| 866 | case FLASH_CFI_8BIT: |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 867 | shift = 0; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 868 | break; |
| 869 | case FLASH_CFI_16BIT: |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 870 | shift = 1; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 871 | break; |
| 872 | case FLASH_CFI_32BIT: |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 873 | shift = 2; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 874 | break; |
| 875 | case FLASH_CFI_64BIT: |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 876 | shift = 3; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 877 | break; |
| 878 | default: |
| 879 | retcode = ERR_INVAL; |
| 880 | goto out_unmap; |
| 881 | } |
| 882 | |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 883 | cnt = len >> shift; |
| 884 | |
Tao Hou | dd3b455 | 2012-03-15 23:33:58 +0800 | [diff] [blame] | 885 | while ((cnt-- > 0) && (flag == 1)) { |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 886 | switch (info->portwidth) { |
| 887 | case FLASH_CFI_8BIT: |
| 888 | flag = ((flash_read8(dst2) & flash_read8(src)) == |
| 889 | flash_read8(src)); |
| 890 | src += 1, dst2 += 1; |
| 891 | break; |
| 892 | case FLASH_CFI_16BIT: |
| 893 | flag = ((flash_read16(dst2) & flash_read16(src)) == |
| 894 | flash_read16(src)); |
| 895 | src += 2, dst2 += 2; |
| 896 | break; |
| 897 | case FLASH_CFI_32BIT: |
| 898 | flag = ((flash_read32(dst2) & flash_read32(src)) == |
| 899 | flash_read32(src)); |
| 900 | src += 4, dst2 += 4; |
| 901 | break; |
| 902 | case FLASH_CFI_64BIT: |
| 903 | flag = ((flash_read64(dst2) & flash_read64(src)) == |
| 904 | flash_read64(src)); |
| 905 | src += 8, dst2 += 8; |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | if (!flag) { |
| 910 | retcode = ERR_NOT_ERASED; |
| 911 | goto out_unmap; |
| 912 | } |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 913 | |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 914 | src = cp; |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 915 | sector = find_sector (info, dest); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 916 | |
| 917 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 918 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 919 | case CFI_CMDSET_INTEL_STANDARD: |
| 920 | case CFI_CMDSET_INTEL_EXTENDED: |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 921 | write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ? |
| 922 | FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 923 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 924 | flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS); |
| 925 | flash_write_cmd (info, sector, 0, write_cmd); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 926 | retcode = flash_status_check (info, sector, |
| 927 | info->buffer_write_tout, |
| 928 | "write to buffer"); |
| 929 | if (retcode == ERR_OK) { |
| 930 | /* reduce the number of loops by the width of |
| 931 | * the port */ |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 932 | cnt = len >> shift; |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 933 | flash_write_cmd (info, sector, 0, cnt - 1); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 934 | while (cnt-- > 0) { |
| 935 | switch (info->portwidth) { |
| 936 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 937 | flash_write8(flash_read8(src), dst); |
| 938 | src += 1, dst += 1; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 939 | break; |
| 940 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 941 | flash_write16(flash_read16(src), dst); |
| 942 | src += 2, dst += 2; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 943 | break; |
| 944 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 945 | flash_write32(flash_read32(src), dst); |
| 946 | src += 4, dst += 4; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 947 | break; |
| 948 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 949 | flash_write64(flash_read64(src), dst); |
| 950 | src += 8, dst += 8; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 951 | break; |
| 952 | default: |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 953 | retcode = ERR_INVAL; |
| 954 | goto out_unmap; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | flash_write_cmd (info, sector, 0, |
| 958 | FLASH_CMD_WRITE_BUFFER_CONFIRM); |
| 959 | retcode = flash_full_status_check ( |
| 960 | info, sector, info->buffer_write_tout, |
| 961 | "buffer write"); |
| 962 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 963 | |
| 964 | break; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 965 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 966 | case CFI_CMDSET_AMD_STANDARD: |
| 967 | case CFI_CMDSET_AMD_EXTENDED: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 968 | flash_unlock_seq(info,0); |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 969 | |
| 970 | #ifdef CONFIG_FLASH_SPANSION_S29WS_N |
| 971 | offset = ((unsigned long)dst - info->start[sector]) >> shift; |
| 972 | #endif |
| 973 | flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER); |
| 974 | cnt = len >> shift; |
John Schmoller | ee35588 | 2009-08-12 10:55:47 -0500 | [diff] [blame] | 975 | flash_write_cmd(info, sector, offset, cnt - 1); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 976 | |
| 977 | switch (info->portwidth) { |
| 978 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 979 | while (cnt-- > 0) { |
| 980 | flash_write8(flash_read8(src), dst); |
| 981 | src += 1, dst += 1; |
| 982 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 983 | break; |
| 984 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 985 | while (cnt-- > 0) { |
| 986 | flash_write16(flash_read16(src), dst); |
| 987 | src += 2, dst += 2; |
| 988 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 989 | break; |
| 990 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 991 | while (cnt-- > 0) { |
| 992 | flash_write32(flash_read32(src), dst); |
| 993 | src += 4, dst += 4; |
| 994 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 995 | break; |
| 996 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 997 | while (cnt-- > 0) { |
| 998 | flash_write64(flash_read64(src), dst); |
| 999 | src += 8, dst += 8; |
| 1000 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1001 | break; |
| 1002 | default: |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1003 | retcode = ERR_INVAL; |
| 1004 | goto out_unmap; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM); |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1008 | if (use_flash_status_poll(info)) |
| 1009 | retcode = flash_status_poll(info, src - (1 << shift), |
| 1010 | dst - (1 << shift), |
| 1011 | info->buffer_write_tout, |
| 1012 | "buffer write"); |
| 1013 | else |
| 1014 | retcode = flash_full_status_check(info, sector, |
| 1015 | info->buffer_write_tout, |
| 1016 | "buffer write"); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1017 | break; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1018 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1019 | default: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1020 | debug ("Unknown Command Set\n"); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1021 | retcode = ERR_INVAL; |
| 1022 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1023 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1024 | |
| 1025 | out_unmap: |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1026 | return retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1027 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1028 | #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1029 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1030 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1031 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1032 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1033 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1034 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1035 | int rcode = 0; |
| 1036 | int prot; |
| 1037 | flash_sect_t sect; |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1038 | int st; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1039 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1040 | if (info->flash_id != FLASH_MAN_CFI) { |
| 1041 | puts ("Can't erase unknown flash type - aborted\n"); |
| 1042 | return 1; |
| 1043 | } |
| 1044 | if ((s_first < 0) || (s_first > s_last)) { |
| 1045 | puts ("- no sectors to erase\n"); |
| 1046 | return 1; |
| 1047 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1048 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1049 | prot = 0; |
| 1050 | for (sect = s_first; sect <= s_last; ++sect) { |
| 1051 | if (info->protect[sect]) { |
| 1052 | prot++; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1055 | if (prot) { |
| 1056 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 1057 | prot); |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1058 | } else if (flash_verbose) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1059 | putc ('\n'); |
| 1060 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1061 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1062 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1063 | for (sect = s_first; sect <= s_last; sect++) { |
Joe Hershberger | 497c32f | 2012-08-17 15:36:41 -0500 | [diff] [blame] | 1064 | if (ctrlc()) { |
| 1065 | printf("\n"); |
| 1066 | return 1; |
| 1067 | } |
| 1068 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1069 | if (info->protect[sect] == 0) { /* not protected */ |
Joe Hershberger | 7f3c211 | 2012-08-17 15:36:40 -0500 | [diff] [blame] | 1070 | #ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE |
| 1071 | int k; |
| 1072 | int size; |
| 1073 | int erased; |
| 1074 | u32 *flash; |
| 1075 | |
| 1076 | /* |
| 1077 | * Check if whole sector is erased |
| 1078 | */ |
| 1079 | size = flash_sector_size(info, sect); |
| 1080 | erased = 1; |
| 1081 | flash = (u32 *)info->start[sect]; |
| 1082 | /* divide by 4 for longword access */ |
| 1083 | size = size >> 2; |
| 1084 | for (k = 0; k < size; k++) { |
| 1085 | if (flash_read32(flash++) != 0xffffffff) { |
| 1086 | erased = 0; |
| 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | if (erased) { |
| 1091 | if (flash_verbose) |
| 1092 | putc(','); |
| 1093 | continue; |
| 1094 | } |
| 1095 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1096 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1097 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1098 | case CFI_CMDSET_INTEL_STANDARD: |
| 1099 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1100 | flash_write_cmd (info, sect, 0, |
| 1101 | FLASH_CMD_CLEAR_STATUS); |
| 1102 | flash_write_cmd (info, sect, 0, |
| 1103 | FLASH_CMD_BLOCK_ERASE); |
| 1104 | flash_write_cmd (info, sect, 0, |
| 1105 | FLASH_CMD_ERASE_CONFIRM); |
| 1106 | break; |
| 1107 | case CFI_CMDSET_AMD_STANDARD: |
| 1108 | case CFI_CMDSET_AMD_EXTENDED: |
| 1109 | flash_unlock_seq (info, sect); |
| 1110 | flash_write_cmd (info, sect, |
| 1111 | info->addr_unlock1, |
| 1112 | AMD_CMD_ERASE_START); |
| 1113 | flash_unlock_seq (info, sect); |
| 1114 | flash_write_cmd (info, sect, 0, |
Angelo Dureghello | 7ba3028 | 2012-12-01 01:14:18 +0100 | [diff] [blame] | 1115 | info->cmd_erase_sector); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1116 | break; |
| 1117 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1118 | case CFI_CMDSET_AMD_LEGACY: |
| 1119 | flash_unlock_seq (info, 0); |
| 1120 | flash_write_cmd (info, 0, info->addr_unlock1, |
| 1121 | AMD_CMD_ERASE_START); |
| 1122 | flash_unlock_seq (info, 0); |
| 1123 | flash_write_cmd (info, sect, 0, |
| 1124 | AMD_CMD_ERASE_SECTOR); |
| 1125 | break; |
| 1126 | #endif |
| 1127 | default: |
| 1128 | debug ("Unkown flash vendor %d\n", |
| 1129 | info->vendor); |
| 1130 | break; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1131 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1132 | |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1133 | if (use_flash_status_poll(info)) { |
Kim Phillips | d303b86 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 1134 | cfiword_t cword; |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1135 | void *dest; |
Kim Phillips | d303b86 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 1136 | cword.ll = 0xffffffffffffffffULL; |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1137 | dest = flash_map(info, sect, 0); |
| 1138 | st = flash_status_poll(info, &cword, dest, |
| 1139 | info->erase_blk_tout, "erase"); |
| 1140 | flash_unmap(info, sect, 0, dest); |
| 1141 | } else |
| 1142 | st = flash_full_status_check(info, sect, |
| 1143 | info->erase_blk_tout, |
| 1144 | "erase"); |
| 1145 | if (st) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1146 | rcode = 1; |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1147 | else if (flash_verbose) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1148 | putc ('.'); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1149 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1150 | } |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1151 | |
| 1152 | if (flash_verbose) |
| 1153 | puts (" done\n"); |
| 1154 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1155 | return rcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1156 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1157 | |
Stefan Roese | b6f508d | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1158 | #ifdef CONFIG_SYS_FLASH_EMPTY_INFO |
| 1159 | static int sector_erased(flash_info_t *info, int i) |
| 1160 | { |
| 1161 | int k; |
| 1162 | int size; |
Stefan Roese | a9153f2 | 2010-10-25 18:31:39 +0200 | [diff] [blame] | 1163 | u32 *flash; |
Stefan Roese | b6f508d | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1164 | |
| 1165 | /* |
| 1166 | * Check if whole sector is erased |
| 1167 | */ |
| 1168 | size = flash_sector_size(info, i); |
Stefan Roese | a9153f2 | 2010-10-25 18:31:39 +0200 | [diff] [blame] | 1169 | flash = (u32 *)info->start[i]; |
Stefan Roese | b6f508d | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1170 | /* divide by 4 for longword access */ |
| 1171 | size = size >> 2; |
| 1172 | |
| 1173 | for (k = 0; k < size; k++) { |
Stefan Roese | a9153f2 | 2010-10-25 18:31:39 +0200 | [diff] [blame] | 1174 | if (flash_read32(flash++) != 0xffffffff) |
Stefan Roese | b6f508d | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1175 | return 0; /* not erased */ |
| 1176 | } |
| 1177 | |
| 1178 | return 1; /* erased */ |
| 1179 | } |
| 1180 | #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */ |
| 1181 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1182 | void flash_print_info (flash_info_t * info) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1183 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1184 | int i; |
wdenk | 369d43d | 2004-03-14 14:09:05 +0000 | [diff] [blame] | 1185 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1186 | if (info->flash_id != FLASH_MAN_CFI) { |
| 1187 | puts ("missing or unknown FLASH type\n"); |
| 1188 | return; |
| 1189 | } |
| 1190 | |
Peter Tyser | 0b5a263 | 2010-12-28 18:12:05 -0600 | [diff] [blame] | 1191 | printf ("%s flash (%d x %d)", |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1192 | info->name, |
| 1193 | (info->portwidth << 3), (info->chipwidth << 3)); |
| 1194 | if (info->size < 1024*1024) |
| 1195 | printf (" Size: %ld kB in %d Sectors\n", |
| 1196 | info->size >> 10, info->sector_count); |
| 1197 | else |
| 1198 | printf (" Size: %ld MB in %d Sectors\n", |
| 1199 | info->size >> 20, info->sector_count); |
| 1200 | printf (" "); |
| 1201 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1202 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
| 1203 | printf ("Intel Prog Regions"); |
| 1204 | break; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1205 | case CFI_CMDSET_INTEL_STANDARD: |
| 1206 | printf ("Intel Standard"); |
| 1207 | break; |
| 1208 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1209 | printf ("Intel Extended"); |
| 1210 | break; |
| 1211 | case CFI_CMDSET_AMD_STANDARD: |
| 1212 | printf ("AMD Standard"); |
| 1213 | break; |
| 1214 | case CFI_CMDSET_AMD_EXTENDED: |
| 1215 | printf ("AMD Extended"); |
| 1216 | break; |
| 1217 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1218 | case CFI_CMDSET_AMD_LEGACY: |
| 1219 | printf ("AMD Legacy"); |
| 1220 | break; |
wdenk | 369d43d | 2004-03-14 14:09:05 +0000 | [diff] [blame] | 1221 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1222 | default: |
| 1223 | printf ("Unknown (%d)", info->vendor); |
| 1224 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1225 | } |
Philippe De Muyter | 35bc081 | 2010-08-10 16:54:52 +0200 | [diff] [blame] | 1226 | printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x", |
| 1227 | info->manufacturer_id); |
| 1228 | printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X", |
| 1229 | info->device_id); |
Heiko Schocher | 27cea50 | 2011-04-11 14:16:19 +0200 | [diff] [blame] | 1230 | if ((info->device_id & 0xff) == 0x7E) { |
| 1231 | printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X", |
| 1232 | info->device_id2); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1233 | } |
Stefan Roese | e442a90 | 2012-12-06 15:44:12 +0100 | [diff] [blame] | 1234 | if ((info->vendor == CFI_CMDSET_AMD_STANDARD) && (info->legacy_unlock)) |
| 1235 | printf("\n Advanced Sector Protection (PPB) enabled"); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1236 | printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n", |
| 1237 | info->erase_blk_tout, |
| 1238 | info->write_tout); |
| 1239 | if (info->buffer_size > 1) { |
| 1240 | printf (" Buffer write timeout: %ld ms, " |
| 1241 | "buffer size: %d bytes\n", |
| 1242 | info->buffer_write_tout, |
| 1243 | info->buffer_size); |
| 1244 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1245 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1246 | puts ("\n Sector Start Addresses:"); |
| 1247 | for (i = 0; i < info->sector_count; ++i) { |
Kim Phillips | c8836f1 | 2010-07-26 18:35:39 -0500 | [diff] [blame] | 1248 | if (ctrlc()) |
Stefan Roese | b6f508d | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1249 | break; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1250 | if ((i % 5) == 0) |
Stefan Roese | b6f508d | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1251 | putc('\n'); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1252 | #ifdef CONFIG_SYS_FLASH_EMPTY_INFO |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1253 | /* print empty and read-only info */ |
| 1254 | printf (" %08lX %c %s ", |
| 1255 | info->start[i], |
Stefan Roese | b6f508d | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1256 | sector_erased(info, i) ? 'E' : ' ', |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1257 | info->protect[i] ? "RO" : " "); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1258 | #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1259 | printf (" %08lX %s ", |
| 1260 | info->start[i], |
| 1261 | info->protect[i] ? "RO" : " "); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1262 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1263 | } |
| 1264 | putc ('\n'); |
| 1265 | return; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1268 | /*----------------------------------------------------------------------- |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1269 | * This is used in a few places in write_buf() to show programming |
| 1270 | * progress. Making it a function is nasty because it needs to do side |
| 1271 | * effect updates to digit and dots. Repeated code is nasty too, so |
| 1272 | * we define it once here. |
| 1273 | */ |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1274 | #ifdef CONFIG_FLASH_SHOW_PROGRESS |
| 1275 | #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \ |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1276 | if (flash_verbose) { \ |
| 1277 | dots -= dots_sub; \ |
| 1278 | if ((scale > 0) && (dots <= 0)) { \ |
| 1279 | if ((digit % 5) == 0) \ |
| 1280 | printf ("%d", digit / 5); \ |
| 1281 | else \ |
| 1282 | putc ('.'); \ |
| 1283 | digit--; \ |
| 1284 | dots += scale; \ |
| 1285 | } \ |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1286 | } |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1287 | #else |
| 1288 | #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) |
| 1289 | #endif |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1290 | |
| 1291 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1292 | * Copy memory to flash, returns: |
| 1293 | * 0 - OK |
| 1294 | * 1 - write timeout |
| 1295 | * 2 - Flash not erased |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1296 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1297 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1298 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1299 | ulong wp; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1300 | uchar *p; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1301 | int aln; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1302 | cfiword_t cword; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1303 | int i, rc; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1304 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1305 | int buffered_size; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1306 | #endif |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1307 | #ifdef CONFIG_FLASH_SHOW_PROGRESS |
| 1308 | int digit = CONFIG_FLASH_SHOW_PROGRESS; |
| 1309 | int scale = 0; |
| 1310 | int dots = 0; |
| 1311 | |
| 1312 | /* |
| 1313 | * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes. |
| 1314 | */ |
| 1315 | if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) { |
| 1316 | scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) / |
| 1317 | CONFIG_FLASH_SHOW_PROGRESS); |
| 1318 | } |
| 1319 | #endif |
| 1320 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1321 | /* get lower aligned address */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1322 | wp = (addr & ~(info->portwidth - 1)); |
Haiying Wang | c123a38 | 2007-02-21 16:52:31 +0100 | [diff] [blame] | 1323 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1324 | /* handle unaligned start */ |
| 1325 | if ((aln = addr - wp) != 0) { |
| 1326 | cword.l = 0; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1327 | p = (uchar *)wp; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1328 | for (i = 0; i < aln; ++i) |
| 1329 | flash_add_byte (info, &cword, flash_read8(p + i)); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1330 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1331 | for (; (i < info->portwidth) && (cnt > 0); i++) { |
| 1332 | flash_add_byte (info, &cword, *src++); |
| 1333 | cnt--; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1334 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1335 | for (; (cnt == 0) && (i < info->portwidth); ++i) |
| 1336 | flash_add_byte (info, &cword, flash_read8(p + i)); |
| 1337 | |
| 1338 | rc = flash_write_cfiword (info, wp, cword); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1339 | if (rc != 0) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1340 | return rc; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1341 | |
| 1342 | wp += i; |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1343 | FLASH_SHOW_PROGRESS(scale, dots, digit, i); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* handle the aligned part */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1347 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1348 | buffered_size = (info->portwidth / info->chipwidth); |
| 1349 | buffered_size *= info->buffer_size; |
| 1350 | while (cnt >= info->portwidth) { |
| 1351 | /* prohibit buffer write when buffer_size is 1 */ |
| 1352 | if (info->buffer_size == 1) { |
| 1353 | cword.l = 0; |
| 1354 | for (i = 0; i < info->portwidth; i++) |
| 1355 | flash_add_byte (info, &cword, *src++); |
| 1356 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
| 1357 | return rc; |
| 1358 | wp += info->portwidth; |
| 1359 | cnt -= info->portwidth; |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | /* write buffer until next buffered_size aligned boundary */ |
| 1364 | i = buffered_size - (wp % buffered_size); |
| 1365 | if (i > cnt) |
| 1366 | i = cnt; |
| 1367 | if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK) |
| 1368 | return rc; |
| 1369 | i -= i & (info->portwidth - 1); |
| 1370 | wp += i; |
| 1371 | src += i; |
| 1372 | cnt -= i; |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1373 | FLASH_SHOW_PROGRESS(scale, dots, digit, i); |
Joe Hershberger | 497c32f | 2012-08-17 15:36:41 -0500 | [diff] [blame] | 1374 | /* Only check every once in a while */ |
| 1375 | if ((cnt & 0xFFFF) < buffered_size && ctrlc()) |
| 1376 | return ERR_ABORTED; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1377 | } |
| 1378 | #else |
| 1379 | while (cnt >= info->portwidth) { |
| 1380 | cword.l = 0; |
| 1381 | for (i = 0; i < info->portwidth; i++) { |
| 1382 | flash_add_byte (info, &cword, *src++); |
| 1383 | } |
| 1384 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
| 1385 | return rc; |
| 1386 | wp += info->portwidth; |
| 1387 | cnt -= info->portwidth; |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1388 | FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth); |
Joe Hershberger | 497c32f | 2012-08-17 15:36:41 -0500 | [diff] [blame] | 1389 | /* Only check every once in a while */ |
| 1390 | if ((cnt & 0xFFFF) < info->portwidth && ctrlc()) |
| 1391 | return ERR_ABORTED; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1392 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1393 | #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */ |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1394 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1395 | if (cnt == 0) { |
| 1396 | return (0); |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * handle unaligned tail bytes |
| 1401 | */ |
| 1402 | cword.l = 0; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1403 | p = (uchar *)wp; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1404 | for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1405 | flash_add_byte (info, &cword, *src++); |
| 1406 | --cnt; |
| 1407 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1408 | for (; i < info->portwidth; ++i) |
| 1409 | flash_add_byte (info, &cword, flash_read8(p + i)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1410 | |
| 1411 | return flash_write_cfiword (info, wp, cword); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1412 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1413 | |
Stefan Roese | 92b1bca | 2012-12-06 15:44:09 +0100 | [diff] [blame] | 1414 | static inline int manufact_match(flash_info_t *info, u32 manu) |
| 1415 | { |
| 1416 | return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16); |
| 1417 | } |
| 1418 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1419 | /*----------------------------------------------------------------------- |
| 1420 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1421 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1422 | |
Holger Brunck | 94c302d | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1423 | static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot) |
| 1424 | { |
Stefan Roese | 92b1bca | 2012-12-06 15:44:09 +0100 | [diff] [blame] | 1425 | if (manufact_match(info, INTEL_MANUFACT) |
Kim Phillips | d303b86 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 1426 | && info->device_id == NUMONYX_256MBIT) { |
Holger Brunck | 94c302d | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1427 | /* |
| 1428 | * see errata called |
| 1429 | * "Numonyx Axcell P33/P30 Specification Update" :) |
| 1430 | */ |
| 1431 | flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID); |
| 1432 | if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT, |
| 1433 | prot)) { |
| 1434 | /* |
| 1435 | * cmd must come before FLASH_CMD_PROTECT + 20us |
| 1436 | * Disable interrupts which might cause a timeout here. |
| 1437 | */ |
| 1438 | int flag = disable_interrupts(); |
| 1439 | unsigned short cmd; |
| 1440 | |
| 1441 | if (prot) |
| 1442 | cmd = FLASH_CMD_PROTECT_SET; |
| 1443 | else |
| 1444 | cmd = FLASH_CMD_PROTECT_CLEAR; |
| 1445 | flash_write_cmd(info, sector, 0, |
| 1446 | FLASH_CMD_PROTECT); |
| 1447 | flash_write_cmd(info, sector, 0, cmd); |
| 1448 | /* re-enable interrupts if necessary */ |
| 1449 | if (flag) |
| 1450 | enable_interrupts(); |
| 1451 | } |
| 1452 | return 1; |
| 1453 | } |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1457 | int flash_real_protect (flash_info_t * info, long sector, int prot) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1458 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1459 | int retcode = 0; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1460 | |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1461 | switch (info->vendor) { |
| 1462 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
| 1463 | case CFI_CMDSET_INTEL_STANDARD: |
Nick Spence | ec81b47 | 2008-08-19 22:21:16 -0700 | [diff] [blame] | 1464 | case CFI_CMDSET_INTEL_EXTENDED: |
Holger Brunck | 94c302d | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1465 | if (!cfi_protect_bugfix(info, sector, prot)) { |
| 1466 | flash_write_cmd(info, sector, 0, |
| 1467 | FLASH_CMD_CLEAR_STATUS); |
| 1468 | flash_write_cmd(info, sector, 0, |
| 1469 | FLASH_CMD_PROTECT); |
Philippe De Muyter | ca6cd16 | 2010-08-17 18:40:25 +0200 | [diff] [blame] | 1470 | if (prot) |
Holger Brunck | 94c302d | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1471 | flash_write_cmd(info, sector, 0, |
| 1472 | FLASH_CMD_PROTECT_SET); |
Philippe De Muyter | ca6cd16 | 2010-08-17 18:40:25 +0200 | [diff] [blame] | 1473 | else |
Holger Brunck | 94c302d | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1474 | flash_write_cmd(info, sector, 0, |
| 1475 | FLASH_CMD_PROTECT_CLEAR); |
Philippe De Muyter | ca6cd16 | 2010-08-17 18:40:25 +0200 | [diff] [blame] | 1476 | |
Philippe De Muyter | ca6cd16 | 2010-08-17 18:40:25 +0200 | [diff] [blame] | 1477 | } |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1478 | break; |
| 1479 | case CFI_CMDSET_AMD_EXTENDED: |
| 1480 | case CFI_CMDSET_AMD_STANDARD: |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1481 | /* U-Boot only checks the first byte */ |
Stefan Roese | 92b1bca | 2012-12-06 15:44:09 +0100 | [diff] [blame] | 1482 | if (manufact_match(info, ATM_MANUFACT)) { |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1483 | if (prot) { |
| 1484 | flash_unlock_seq (info, 0); |
| 1485 | flash_write_cmd (info, 0, |
| 1486 | info->addr_unlock1, |
| 1487 | ATM_CMD_SOFTLOCK_START); |
| 1488 | flash_unlock_seq (info, 0); |
| 1489 | flash_write_cmd (info, sector, 0, |
| 1490 | ATM_CMD_LOCK_SECT); |
| 1491 | } else { |
| 1492 | flash_write_cmd (info, 0, |
| 1493 | info->addr_unlock1, |
| 1494 | AMD_CMD_UNLOCK_START); |
| 1495 | if (info->device_id == ATM_ID_BV6416) |
| 1496 | flash_write_cmd (info, sector, |
| 1497 | 0, ATM_CMD_UNLOCK_SECT); |
| 1498 | } |
| 1499 | } |
Stefan Roese | 7de6584 | 2012-12-06 15:44:11 +0100 | [diff] [blame] | 1500 | if (info->legacy_unlock) { |
Anatolij Gustschin | 041b04f | 2012-08-09 08:18:12 +0200 | [diff] [blame] | 1501 | int flag = disable_interrupts(); |
| 1502 | int lock_flag; |
| 1503 | |
| 1504 | flash_unlock_seq(info, 0); |
| 1505 | flash_write_cmd(info, 0, info->addr_unlock1, |
| 1506 | AMD_CMD_SET_PPB_ENTRY); |
| 1507 | lock_flag = flash_isset(info, sector, 0, 0x01); |
| 1508 | if (prot) { |
| 1509 | if (lock_flag) { |
| 1510 | flash_write_cmd(info, sector, 0, |
| 1511 | AMD_CMD_PPB_LOCK_BC1); |
| 1512 | flash_write_cmd(info, sector, 0, |
| 1513 | AMD_CMD_PPB_LOCK_BC2); |
| 1514 | } |
| 1515 | debug("sector %ld %slocked\n", sector, |
| 1516 | lock_flag ? "" : "already "); |
| 1517 | } else { |
| 1518 | if (!lock_flag) { |
| 1519 | debug("unlock %ld\n", sector); |
| 1520 | flash_write_cmd(info, 0, 0, |
| 1521 | AMD_CMD_PPB_UNLOCK_BC1); |
| 1522 | flash_write_cmd(info, 0, 0, |
| 1523 | AMD_CMD_PPB_UNLOCK_BC2); |
| 1524 | } |
| 1525 | debug("sector %ld %sunlocked\n", sector, |
| 1526 | !lock_flag ? "" : "already "); |
| 1527 | } |
| 1528 | if (flag) |
| 1529 | enable_interrupts(); |
| 1530 | |
| 1531 | if (flash_status_check(info, sector, |
| 1532 | info->erase_blk_tout, |
| 1533 | prot ? "protect" : "unprotect")) |
| 1534 | printf("status check error\n"); |
| 1535 | |
| 1536 | flash_write_cmd(info, 0, 0, |
| 1537 | AMD_CMD_SET_PPB_EXIT_BC1); |
| 1538 | flash_write_cmd(info, 0, 0, |
| 1539 | AMD_CMD_SET_PPB_EXIT_BC2); |
| 1540 | } |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1541 | break; |
TsiChung Liew | b8c1929 | 2008-08-19 16:53:39 +0000 | [diff] [blame] | 1542 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1543 | case CFI_CMDSET_AMD_LEGACY: |
| 1544 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 1545 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT); |
| 1546 | if (prot) |
| 1547 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET); |
| 1548 | else |
| 1549 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR); |
| 1550 | #endif |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1551 | }; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1552 | |
Stefan Roese | 5215df1 | 2010-10-25 18:31:29 +0200 | [diff] [blame] | 1553 | /* |
| 1554 | * Flash needs to be in status register read mode for |
| 1555 | * flash_full_status_check() to work correctly |
| 1556 | */ |
| 1557 | flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1558 | if ((retcode = |
| 1559 | flash_full_status_check (info, sector, info->erase_blk_tout, |
| 1560 | prot ? "protect" : "unprotect")) == 0) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1561 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1562 | info->protect[sector] = prot; |
| 1563 | |
| 1564 | /* |
| 1565 | * On some of Intel's flash chips (marked via legacy_unlock) |
| 1566 | * unprotect unprotects all locking. |
| 1567 | */ |
| 1568 | if ((prot == 0) && (info->legacy_unlock)) { |
| 1569 | flash_sect_t i; |
| 1570 | |
| 1571 | for (i = 0; i < info->sector_count; i++) { |
| 1572 | if (info->protect[i]) |
| 1573 | flash_real_protect (info, i, 1); |
| 1574 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1575 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1576 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1577 | return retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1578 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1579 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1580 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1581 | * flash_read_user_serial - read the OneTimeProgramming cells |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1582 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1583 | void flash_read_user_serial (flash_info_t * info, void *buffer, int offset, |
| 1584 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1585 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1586 | uchar *src; |
| 1587 | uchar *dst; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1588 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1589 | dst = buffer; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1590 | src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1591 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 1592 | memcpy (dst, src + offset, len); |
| 1593 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
Aaron Williams | ffcef2f | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1594 | udelay(1); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1595 | flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1598 | /* |
| 1599 | * flash_read_factory_serial - read the device Id from the protection area |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1600 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1601 | void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset, |
| 1602 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1603 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1604 | uchar *src; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1605 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1606 | src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1607 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 1608 | memcpy (buffer, src + offset, len); |
| 1609 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
Aaron Williams | ffcef2f | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1610 | udelay(1); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1611 | flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1614 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1615 | |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1616 | /*----------------------------------------------------------------------- |
| 1617 | * Reverse the order of the erase regions in the CFI QRY structure. |
| 1618 | * This is needed for chips that are either a) correctly detected as |
| 1619 | * top-boot, or b) buggy. |
| 1620 | */ |
| 1621 | static void cfi_reverse_geometry(struct cfi_qry *qry) |
| 1622 | { |
| 1623 | unsigned int i, j; |
| 1624 | u32 tmp; |
| 1625 | |
| 1626 | for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) { |
Andrew Gabbasov | c159258 | 2013-05-14 12:27:52 -0500 | [diff] [blame] | 1627 | tmp = get_unaligned(&(qry->erase_region_info[i])); |
| 1628 | put_unaligned(get_unaligned(&(qry->erase_region_info[j])), |
| 1629 | &(qry->erase_region_info[i])); |
| 1630 | put_unaligned(tmp, &(qry->erase_region_info[j])); |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1631 | } |
| 1632 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1633 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1634 | /*----------------------------------------------------------------------- |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1635 | * read jedec ids from device and set corresponding fields in info struct |
| 1636 | * |
| 1637 | * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct |
| 1638 | * |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1639 | */ |
| 1640 | static void cmdset_intel_read_jedec_ids(flash_info_t *info) |
| 1641 | { |
| 1642 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
Aaron Williams | ffcef2f | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1643 | udelay(1); |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1644 | flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID); |
| 1645 | udelay(1000); /* some flash are slow to respond */ |
| 1646 | info->manufacturer_id = flash_read_uchar (info, |
| 1647 | FLASH_OFFSET_MANUFACTURER_ID); |
Philippe De Muyter | 35bc081 | 2010-08-10 16:54:52 +0200 | [diff] [blame] | 1648 | info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ? |
| 1649 | flash_read_word (info, FLASH_OFFSET_DEVICE_ID) : |
| 1650 | flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID); |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1651 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1652 | } |
| 1653 | |
| 1654 | static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry) |
| 1655 | { |
| 1656 | info->cmd_reset = FLASH_CMD_RESET; |
| 1657 | |
| 1658 | cmdset_intel_read_jedec_ids(info); |
| 1659 | flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI); |
| 1660 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1661 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1662 | /* read legacy lock/unlock bit from intel flash */ |
| 1663 | if (info->ext_addr) { |
| 1664 | info->legacy_unlock = flash_read_uchar (info, |
| 1665 | info->ext_addr + 5) & 0x08; |
| 1666 | } |
| 1667 | #endif |
| 1668 | |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | static void cmdset_amd_read_jedec_ids(flash_info_t *info) |
| 1673 | { |
Niklaus Giger | f447f71 | 2009-07-22 17:13:24 +0200 | [diff] [blame] | 1674 | ushort bankId = 0; |
| 1675 | uchar manuId; |
| 1676 | |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1677 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
| 1678 | flash_unlock_seq(info, 0); |
| 1679 | flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID); |
| 1680 | udelay(1000); /* some flash are slow to respond */ |
Tor Krill | 7f2a305 | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 1681 | |
Niklaus Giger | f447f71 | 2009-07-22 17:13:24 +0200 | [diff] [blame] | 1682 | manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID); |
| 1683 | /* JEDEC JEP106Z specifies ID codes up to bank 7 */ |
| 1684 | while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) { |
| 1685 | bankId += 0x100; |
| 1686 | manuId = flash_read_uchar (info, |
| 1687 | bankId | FLASH_OFFSET_MANUFACTURER_ID); |
| 1688 | } |
| 1689 | info->manufacturer_id = manuId; |
Tor Krill | 7f2a305 | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 1690 | |
| 1691 | switch (info->chipwidth){ |
| 1692 | case FLASH_CFI_8BIT: |
| 1693 | info->device_id = flash_read_uchar (info, |
| 1694 | FLASH_OFFSET_DEVICE_ID); |
| 1695 | if (info->device_id == 0x7E) { |
| 1696 | /* AMD 3-byte (expanded) device ids */ |
| 1697 | info->device_id2 = flash_read_uchar (info, |
| 1698 | FLASH_OFFSET_DEVICE_ID2); |
| 1699 | info->device_id2 <<= 8; |
| 1700 | info->device_id2 |= flash_read_uchar (info, |
| 1701 | FLASH_OFFSET_DEVICE_ID3); |
| 1702 | } |
| 1703 | break; |
| 1704 | case FLASH_CFI_16BIT: |
| 1705 | info->device_id = flash_read_word (info, |
| 1706 | FLASH_OFFSET_DEVICE_ID); |
Heiko Schocher | 27cea50 | 2011-04-11 14:16:19 +0200 | [diff] [blame] | 1707 | if ((info->device_id & 0xff) == 0x7E) { |
| 1708 | /* AMD 3-byte (expanded) device ids */ |
| 1709 | info->device_id2 = flash_read_uchar (info, |
| 1710 | FLASH_OFFSET_DEVICE_ID2); |
| 1711 | info->device_id2 <<= 8; |
| 1712 | info->device_id2 |= flash_read_uchar (info, |
| 1713 | FLASH_OFFSET_DEVICE_ID3); |
| 1714 | } |
Tor Krill | 7f2a305 | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 1715 | break; |
| 1716 | default: |
| 1717 | break; |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1718 | } |
| 1719 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
Aaron Williams | ffcef2f | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1720 | udelay(1); |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry) |
| 1724 | { |
| 1725 | info->cmd_reset = AMD_CMD_RESET; |
Angelo Dureghello | 7ba3028 | 2012-12-01 01:14:18 +0100 | [diff] [blame] | 1726 | info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR; |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1727 | |
| 1728 | cmdset_amd_read_jedec_ids(info); |
| 1729 | flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI); |
| 1730 | |
Anatolij Gustschin | 041b04f | 2012-08-09 08:18:12 +0200 | [diff] [blame] | 1731 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Stefan Roese | 7de6584 | 2012-12-06 15:44:11 +0100 | [diff] [blame] | 1732 | if (info->ext_addr) { |
| 1733 | /* read sector protect/unprotect scheme (at 0x49) */ |
| 1734 | if (flash_read_uchar(info, info->ext_addr + 9) == 0x8) |
Anatolij Gustschin | 041b04f | 2012-08-09 08:18:12 +0200 | [diff] [blame] | 1735 | info->legacy_unlock = 1; |
| 1736 | } |
| 1737 | #endif |
| 1738 | |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | #ifdef CONFIG_FLASH_CFI_LEGACY |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1743 | static void flash_read_jedec_ids (flash_info_t * info) |
| 1744 | { |
| 1745 | info->manufacturer_id = 0; |
| 1746 | info->device_id = 0; |
| 1747 | info->device_id2 = 0; |
| 1748 | |
| 1749 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1750 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1751 | case CFI_CMDSET_INTEL_STANDARD: |
| 1752 | case CFI_CMDSET_INTEL_EXTENDED: |
Michael Schwingen | 5fb0aa4 | 2008-01-12 20:29:47 +0100 | [diff] [blame] | 1753 | cmdset_intel_read_jedec_ids(info); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1754 | break; |
| 1755 | case CFI_CMDSET_AMD_STANDARD: |
| 1756 | case CFI_CMDSET_AMD_EXTENDED: |
Michael Schwingen | 5fb0aa4 | 2008-01-12 20:29:47 +0100 | [diff] [blame] | 1757 | cmdset_amd_read_jedec_ids(info); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1758 | break; |
| 1759 | default: |
| 1760 | break; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1765 | * Call board code to request info about non-CFI flash. |
| 1766 | * board_flash_get_legacy needs to fill in at least: |
| 1767 | * info->portwidth, info->chipwidth and info->interface for Jedec probing. |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1768 | */ |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1769 | static int flash_detect_legacy(phys_addr_t base, int banknum) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1770 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1771 | flash_info_t *info = &flash_info[banknum]; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1772 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1773 | if (board_flash_get_legacy(base, banknum, info)) { |
| 1774 | /* board code may have filled info completely. If not, we |
| 1775 | use JEDEC ID probing. */ |
| 1776 | if (!info->vendor) { |
| 1777 | int modes[] = { |
| 1778 | CFI_CMDSET_AMD_STANDARD, |
| 1779 | CFI_CMDSET_INTEL_STANDARD |
| 1780 | }; |
| 1781 | int i; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1782 | |
Axel Lin | 85706c8 | 2013-06-23 00:56:46 +0800 | [diff] [blame] | 1783 | for (i = 0; i < ARRAY_SIZE(modes); i++) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1784 | info->vendor = modes[i]; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1785 | info->start[0] = |
| 1786 | (ulong)map_physmem(base, |
Stefan Roese | b844370 | 2009-02-05 11:44:52 +0100 | [diff] [blame] | 1787 | info->portwidth, |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1788 | MAP_NOCACHE); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1789 | if (info->portwidth == FLASH_CFI_8BIT |
| 1790 | && info->interface == FLASH_CFI_X8X16) { |
| 1791 | info->addr_unlock1 = 0x2AAA; |
| 1792 | info->addr_unlock2 = 0x5555; |
| 1793 | } else { |
| 1794 | info->addr_unlock1 = 0x5555; |
| 1795 | info->addr_unlock2 = 0x2AAA; |
| 1796 | } |
| 1797 | flash_read_jedec_ids(info); |
| 1798 | debug("JEDEC PROBE: ID %x %x %x\n", |
| 1799 | info->manufacturer_id, |
| 1800 | info->device_id, |
| 1801 | info->device_id2); |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1802 | if (jedec_flash_match(info, info->start[0])) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1803 | break; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1804 | else |
Stefan Roese | b844370 | 2009-02-05 11:44:52 +0100 | [diff] [blame] | 1805 | unmap_physmem((void *)info->start[0], |
Kuo-Jung Su | 7a939fe | 2013-07-04 11:40:36 +0800 | [diff] [blame] | 1806 | info->portwidth); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | switch(info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1811 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1812 | case CFI_CMDSET_INTEL_STANDARD: |
| 1813 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1814 | info->cmd_reset = FLASH_CMD_RESET; |
| 1815 | break; |
| 1816 | case CFI_CMDSET_AMD_STANDARD: |
| 1817 | case CFI_CMDSET_AMD_EXTENDED: |
| 1818 | case CFI_CMDSET_AMD_LEGACY: |
| 1819 | info->cmd_reset = AMD_CMD_RESET; |
| 1820 | break; |
| 1821 | } |
| 1822 | info->flash_id = FLASH_MAN_CFI; |
| 1823 | return 1; |
| 1824 | } |
| 1825 | return 0; /* use CFI */ |
| 1826 | } |
| 1827 | #else |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1828 | static inline int flash_detect_legacy(phys_addr_t base, int banknum) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1829 | { |
| 1830 | return 0; /* use CFI */ |
| 1831 | } |
| 1832 | #endif |
| 1833 | |
| 1834 | /*----------------------------------------------------------------------- |
| 1835 | * detect if flash is compatible with the Common Flash Interface (CFI) |
| 1836 | * http://www.jedec.org/download/search/jesd68.pdf |
| 1837 | */ |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1838 | static void flash_read_cfi (flash_info_t *info, void *buf, |
| 1839 | unsigned int start, size_t len) |
| 1840 | { |
| 1841 | u8 *p = buf; |
| 1842 | unsigned int i; |
| 1843 | |
| 1844 | for (i = 0; i < len; i++) |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1845 | p[i] = flash_read_uchar(info, start + i); |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1846 | } |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 1847 | |
Kim Phillips | d303b86 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 1848 | static void __flash_cmd_reset(flash_info_t *info) |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 1849 | { |
| 1850 | /* |
| 1851 | * We do not yet know what kind of commandset to use, so we issue |
| 1852 | * the reset command in both Intel and AMD variants, in the hope |
| 1853 | * that AMD flash roms ignore the Intel command. |
| 1854 | */ |
| 1855 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
Aaron Williams | ffcef2f | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1856 | udelay(1); |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 1857 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1858 | } |
| 1859 | void flash_cmd_reset(flash_info_t *info) |
| 1860 | __attribute__((weak,alias("__flash_cmd_reset"))); |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1861 | |
| 1862 | static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1863 | { |
| 1864 | int cfi_offset; |
| 1865 | |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1866 | /* Issue FLASH reset command */ |
| 1867 | flash_cmd_reset(info); |
| 1868 | |
Axel Lin | 85706c8 | 2013-06-23 00:56:46 +0800 | [diff] [blame] | 1869 | for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1870 | cfi_offset++) { |
| 1871 | flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], |
| 1872 | FLASH_CMD_CFI); |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1873 | if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q') |
| 1874 | && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') |
| 1875 | && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) { |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1876 | flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP, |
| 1877 | sizeof(struct cfi_qry)); |
| 1878 | info->interface = le16_to_cpu(qry->interface_desc); |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1879 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1880 | info->cfi_offset = flash_offset_cfi[cfi_offset]; |
| 1881 | debug ("device interface is %d\n", |
| 1882 | info->interface); |
| 1883 | debug ("found port %d chip %d ", |
| 1884 | info->portwidth, info->chipwidth); |
| 1885 | debug ("port %d bits chip %d bits\n", |
| 1886 | info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 1887 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 1888 | |
| 1889 | /* calculate command offsets as in the Linux driver */ |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1890 | info->addr_unlock1 = 0x555; |
| 1891 | info->addr_unlock2 = 0x2aa; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1892 | |
| 1893 | /* |
| 1894 | * modify the unlock address if we are |
| 1895 | * in compatibility mode |
| 1896 | */ |
| 1897 | if ( /* x8/x16 in x8 mode */ |
| 1898 | ((info->chipwidth == FLASH_CFI_BY8) && |
| 1899 | (info->interface == FLASH_CFI_X8X16)) || |
| 1900 | /* x16/x32 in x16 mode */ |
| 1901 | ((info->chipwidth == FLASH_CFI_BY16) && |
| 1902 | (info->interface == FLASH_CFI_X16X32))) |
| 1903 | { |
| 1904 | info->addr_unlock1 = 0xaaa; |
| 1905 | info->addr_unlock2 = 0x555; |
| 1906 | } |
| 1907 | |
| 1908 | info->name = "CFI conformant"; |
| 1909 | return 1; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | return 0; |
| 1914 | } |
| 1915 | |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1916 | static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry) |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1917 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1918 | debug ("flash detect cfi\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1919 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1920 | for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1921 | info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) { |
| 1922 | for (info->chipwidth = FLASH_CFI_BY8; |
| 1923 | info->chipwidth <= info->portwidth; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1924 | info->chipwidth <<= 1) |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1925 | if (__flash_detect_cfi(info, qry)) |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1926 | return 1; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1927 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1928 | debug ("not found\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1929 | return 0; |
| 1930 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1931 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1932 | /* |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1933 | * Manufacturer-specific quirks. Add workarounds for geometry |
| 1934 | * reversal, etc. here. |
| 1935 | */ |
| 1936 | static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry) |
| 1937 | { |
| 1938 | /* check if flash geometry needs reversal */ |
| 1939 | if (qry->num_erase_regions > 1) { |
| 1940 | /* reverse geometry if top boot part */ |
| 1941 | if (info->cfi_version < 0x3131) { |
| 1942 | /* CFI < 1.1, try to guess from device id */ |
| 1943 | if ((info->device_id & 0x80) != 0) |
| 1944 | cfi_reverse_geometry(qry); |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1945 | } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) { |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1946 | /* CFI >= 1.1, deduct from top/bottom flag */ |
| 1947 | /* note: ext_addr is valid since cfi_version > 0 */ |
| 1948 | cfi_reverse_geometry(qry); |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry) |
| 1954 | { |
| 1955 | int reverse_geometry = 0; |
| 1956 | |
| 1957 | /* Check the "top boot" bit in the PRI */ |
| 1958 | if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1)) |
| 1959 | reverse_geometry = 1; |
| 1960 | |
| 1961 | /* AT49BV6416(T) list the erase regions in the wrong order. |
| 1962 | * However, the device ID is identical with the non-broken |
Ulf Samuelsson | 07f9b4e | 2009-03-27 23:26:43 +0100 | [diff] [blame] | 1963 | * AT49BV642D they differ in the high byte. |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1964 | */ |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1965 | if (info->device_id == 0xd6 || info->device_id == 0xd2) |
| 1966 | reverse_geometry = !reverse_geometry; |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1967 | |
| 1968 | if (reverse_geometry) |
| 1969 | cfi_reverse_geometry(qry); |
| 1970 | } |
| 1971 | |
Richard Retanubun | be4dca2 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1972 | static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry) |
| 1973 | { |
| 1974 | /* check if flash geometry needs reversal */ |
| 1975 | if (qry->num_erase_regions > 1) { |
| 1976 | /* reverse geometry if top boot part */ |
| 1977 | if (info->cfi_version < 0x3131) { |
Mike Frysinger | 02a3786 | 2011-04-10 16:06:29 -0400 | [diff] [blame] | 1978 | /* CFI < 1.1, guess by device id */ |
| 1979 | if (info->device_id == 0x22CA || /* M29W320DT */ |
| 1980 | info->device_id == 0x2256 || /* M29W320ET */ |
| 1981 | info->device_id == 0x22D7) { /* M29W800DT */ |
Richard Retanubun | be4dca2 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1982 | cfi_reverse_geometry(qry); |
| 1983 | } |
Mike Frysinger | 97dd899 | 2011-05-09 18:33:36 -0400 | [diff] [blame] | 1984 | } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) { |
| 1985 | /* CFI >= 1.1, deduct from top/bottom flag */ |
| 1986 | /* note: ext_addr is valid since cfi_version > 0 */ |
| 1987 | cfi_reverse_geometry(qry); |
Richard Retanubun | be4dca2 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 | |
Angelo Dureghello | 7ba3028 | 2012-12-01 01:14:18 +0100 | [diff] [blame] | 1992 | static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry) |
| 1993 | { |
| 1994 | /* |
| 1995 | * SST, for many recent nor parallel flashes, says they are |
| 1996 | * CFI-conformant. This is not true, since qry struct. |
| 1997 | * reports a std. AMD command set (0x0002), while SST allows to |
| 1998 | * erase two different sector sizes for the same memory. |
| 1999 | * 64KB sector (SST call it block) needs 0x30 to be erased. |
| 2000 | * 4KB sector (SST call it sector) needs 0x50 to be erased. |
| 2001 | * Since CFI query detect the 4KB number of sectors, users expects |
| 2002 | * a sector granularity of 4KB, and it is here set. |
| 2003 | */ |
| 2004 | if (info->device_id == 0x5D23 || /* SST39VF3201B */ |
| 2005 | info->device_id == 0x5C23) { /* SST39VF3202B */ |
| 2006 | /* set sector granularity to 4KB */ |
| 2007 | info->cmd_erase_sector=0x50; |
| 2008 | } |
| 2009 | } |
| 2010 | |
Jagannadha Sutradharudu Teki | 130def3 | 2013-03-01 16:54:26 +0530 | [diff] [blame] | 2011 | static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry) |
| 2012 | { |
| 2013 | /* |
| 2014 | * The M29EW devices seem to report the CFI information wrong |
| 2015 | * when it's in 8 bit mode. |
| 2016 | * There's an app note from Numonyx on this issue. |
| 2017 | * So adjust the buffer size for M29EW while operating in 8-bit mode |
| 2018 | */ |
| 2019 | if (((qry->max_buf_write_size) > 0x8) && |
| 2020 | (info->device_id == 0x7E) && |
| 2021 | (info->device_id2 == 0x2201 || |
| 2022 | info->device_id2 == 0x2301 || |
| 2023 | info->device_id2 == 0x2801 || |
| 2024 | info->device_id2 == 0x4801)) { |
| 2025 | debug("Adjusted buffer size on Numonyx flash" |
| 2026 | " M29EW family in 8 bit mode\n"); |
| 2027 | qry->max_buf_write_size = 0x8; |
| 2028 | } |
| 2029 | } |
| 2030 | |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 2031 | /* |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2032 | * The following code cannot be run from FLASH! |
| 2033 | * |
| 2034 | */ |
Anatolij Gustschin | 6acb77b | 2010-11-28 02:13:33 +0100 | [diff] [blame] | 2035 | ulong flash_get_size (phys_addr_t base, int banknum) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2036 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2037 | flash_info_t *info = &flash_info[banknum]; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2038 | int i, j; |
| 2039 | flash_sect_t sect_cnt; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 2040 | phys_addr_t sector; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2041 | unsigned long tmp; |
| 2042 | int size_ratio; |
| 2043 | uchar num_erase_regions; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2044 | int erase_region_size; |
| 2045 | int erase_region_count; |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2046 | struct cfi_qry qry; |
Anatolij Gustschin | 6acb77b | 2010-11-28 02:13:33 +0100 | [diff] [blame] | 2047 | unsigned long max_size; |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2048 | |
Kumar Gala | 899032b | 2008-05-15 15:13:08 -0500 | [diff] [blame] | 2049 | memset(&qry, 0, sizeof(qry)); |
| 2050 | |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2051 | info->ext_addr = 0; |
| 2052 | info->cfi_version = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2053 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 2054 | info->legacy_unlock = 0; |
| 2055 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2056 | |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 2057 | info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2058 | |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2059 | if (flash_detect_cfi (info, &qry)) { |
Andrew Gabbasov | c159258 | 2013-05-14 12:27:52 -0500 | [diff] [blame] | 2060 | info->vendor = le16_to_cpu(get_unaligned(&(qry.p_id))); |
| 2061 | info->ext_addr = le16_to_cpu(get_unaligned(&(qry.p_adr))); |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2062 | num_erase_regions = qry.num_erase_regions; |
| 2063 | |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2064 | if (info->ext_addr) { |
| 2065 | info->cfi_version = (ushort) flash_read_uchar (info, |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 2066 | info->ext_addr + 3) << 8; |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2067 | info->cfi_version |= (ushort) flash_read_uchar (info, |
Stefan Roese | 70a90b7 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 2068 | info->ext_addr + 4); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2069 | } |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2070 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2071 | #ifdef DEBUG |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2072 | flash_printqry (&qry); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2073 | #endif |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2074 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2075 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 2076 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2077 | case CFI_CMDSET_INTEL_STANDARD: |
| 2078 | case CFI_CMDSET_INTEL_EXTENDED: |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2079 | cmdset_intel_init(info, &qry); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2080 | break; |
| 2081 | case CFI_CMDSET_AMD_STANDARD: |
| 2082 | case CFI_CMDSET_AMD_EXTENDED: |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2083 | cmdset_amd_init(info, &qry); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2084 | break; |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2085 | default: |
| 2086 | printf("CFI: Unknown command set 0x%x\n", |
| 2087 | info->vendor); |
| 2088 | /* |
| 2089 | * Unfortunately, this means we don't know how |
| 2090 | * to get the chip back to Read mode. Might |
| 2091 | * as well try an Intel-style reset... |
| 2092 | */ |
| 2093 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 2094 | return 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2095 | } |
wdenk | 6cfa84e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 2096 | |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 2097 | /* Do manufacturer-specific fixups */ |
| 2098 | switch (info->manufacturer_id) { |
Mario Schuknecht | 5c3579e | 2011-02-21 13:13:14 +0100 | [diff] [blame] | 2099 | case 0x0001: /* AMD */ |
| 2100 | case 0x0037: /* AMIC */ |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 2101 | flash_fixup_amd(info, &qry); |
| 2102 | break; |
| 2103 | case 0x001f: |
| 2104 | flash_fixup_atmel(info, &qry); |
| 2105 | break; |
Richard Retanubun | be4dca2 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 2106 | case 0x0020: |
| 2107 | flash_fixup_stm(info, &qry); |
| 2108 | break; |
Angelo Dureghello | 7ba3028 | 2012-12-01 01:14:18 +0100 | [diff] [blame] | 2109 | case 0x00bf: /* SST */ |
| 2110 | flash_fixup_sst(info, &qry); |
| 2111 | break; |
Jagannadha Sutradharudu Teki | 130def3 | 2013-03-01 16:54:26 +0530 | [diff] [blame] | 2112 | case 0x0089: /* Numonyx */ |
| 2113 | flash_fixup_num(info, &qry); |
| 2114 | break; |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 2115 | } |
| 2116 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2117 | debug ("manufacturer is %d\n", info->vendor); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2118 | debug ("manufacturer id is 0x%x\n", info->manufacturer_id); |
| 2119 | debug ("device id is 0x%x\n", info->device_id); |
| 2120 | debug ("device id2 is 0x%x\n", info->device_id2); |
| 2121 | debug ("cfi version is 0x%04x\n", info->cfi_version); |
| 2122 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2123 | size_ratio = info->portwidth / info->chipwidth; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2124 | /* if the chip is x8/x16 reduce the ratio by half */ |
| 2125 | if ((info->interface == FLASH_CFI_X8X16) |
| 2126 | && (info->chipwidth == FLASH_CFI_BY8)) { |
| 2127 | size_ratio >>= 1; |
| 2128 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2129 | debug ("size_ratio %d port %d bits chip %d bits\n", |
| 2130 | size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 2131 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Ilya Yanok | 755c180 | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 2132 | info->size = 1 << qry.dev_size; |
| 2133 | /* multiply the size by the number of chips */ |
| 2134 | info->size *= size_ratio; |
Anatolij Gustschin | 6acb77b | 2010-11-28 02:13:33 +0100 | [diff] [blame] | 2135 | max_size = cfi_flash_bank_size(banknum); |
Ilya Yanok | 755c180 | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 2136 | if (max_size && (info->size > max_size)) { |
| 2137 | debug("[truncated from %ldMiB]", info->size >> 20); |
| 2138 | info->size = max_size; |
| 2139 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2140 | debug ("found %d erase regions\n", num_erase_regions); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2141 | sect_cnt = 0; |
| 2142 | sector = base; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2143 | for (i = 0; i < num_erase_regions; i++) { |
| 2144 | if (i > NUM_ERASE_REGIONS) { |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 2145 | printf ("%d erase regions found, only %d used\n", |
| 2146 | num_erase_regions, NUM_ERASE_REGIONS); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2147 | break; |
| 2148 | } |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2149 | |
Andrew Gabbasov | c159258 | 2013-05-14 12:27:52 -0500 | [diff] [blame] | 2150 | tmp = le32_to_cpu(get_unaligned( |
| 2151 | &(qry.erase_region_info[i]))); |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2152 | debug("erase region %u: 0x%08lx\n", i, tmp); |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2153 | |
| 2154 | erase_region_count = (tmp & 0xffff) + 1; |
| 2155 | tmp >>= 16; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2156 | erase_region_size = |
| 2157 | (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128; |
wdenk | aeba06f | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 2158 | debug ("erase_region_count = %d erase_region_size = %d\n", |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 2159 | erase_region_count, erase_region_size); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2160 | for (j = 0; j < erase_region_count; j++) { |
Ilya Yanok | 755c180 | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 2161 | if (sector - base >= info->size) |
| 2162 | break; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2163 | if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) { |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 2164 | printf("ERROR: too many flash sectors\n"); |
| 2165 | break; |
| 2166 | } |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 2167 | info->start[sect_cnt] = |
| 2168 | (ulong)map_physmem(sector, |
| 2169 | info->portwidth, |
| 2170 | MAP_NOCACHE); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2171 | sector += (erase_region_size * size_ratio); |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2172 | |
| 2173 | /* |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2174 | * Only read protection status from |
| 2175 | * supported devices (intel...) |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2176 | */ |
| 2177 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 2178 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2179 | case CFI_CMDSET_INTEL_EXTENDED: |
| 2180 | case CFI_CMDSET_INTEL_STANDARD: |
Stefan Roese | 5215df1 | 2010-10-25 18:31:29 +0200 | [diff] [blame] | 2181 | /* |
| 2182 | * Set flash to read-id mode. Otherwise |
| 2183 | * reading protected status is not |
| 2184 | * guaranteed. |
| 2185 | */ |
| 2186 | flash_write_cmd(info, sect_cnt, 0, |
| 2187 | FLASH_CMD_READ_ID); |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2188 | info->protect[sect_cnt] = |
| 2189 | flash_isset (info, sect_cnt, |
| 2190 | FLASH_OFFSET_PROTECT, |
| 2191 | FLASH_STATUS_PROTECT); |
| 2192 | break; |
Stefan Roese | bcb3344 | 2012-12-06 15:44:10 +0100 | [diff] [blame] | 2193 | case CFI_CMDSET_AMD_EXTENDED: |
| 2194 | case CFI_CMDSET_AMD_STANDARD: |
Stefan Roese | 7de6584 | 2012-12-06 15:44:11 +0100 | [diff] [blame] | 2195 | if (!info->legacy_unlock) { |
Stefan Roese | bcb3344 | 2012-12-06 15:44:10 +0100 | [diff] [blame] | 2196 | /* default: not protected */ |
| 2197 | info->protect[sect_cnt] = 0; |
| 2198 | break; |
| 2199 | } |
| 2200 | |
| 2201 | /* Read protection (PPB) from sector */ |
| 2202 | flash_write_cmd(info, 0, 0, |
| 2203 | info->cmd_reset); |
| 2204 | flash_unlock_seq(info, 0); |
| 2205 | flash_write_cmd(info, 0, |
| 2206 | info->addr_unlock1, |
| 2207 | FLASH_CMD_READ_ID); |
| 2208 | info->protect[sect_cnt] = |
| 2209 | flash_isset( |
| 2210 | info, sect_cnt, |
| 2211 | FLASH_OFFSET_PROTECT, |
| 2212 | FLASH_STATUS_PROTECT); |
| 2213 | break; |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2214 | default: |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2215 | /* default: not protected */ |
| 2216 | info->protect[sect_cnt] = 0; |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2219 | sect_cnt++; |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | info->sector_count = sect_cnt; |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2224 | info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size); |
| 2225 | tmp = 1 << qry.block_erase_timeout_typ; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2226 | info->erase_blk_tout = tmp * |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2227 | (1 << qry.block_erase_timeout_max); |
| 2228 | tmp = (1 << qry.buf_write_timeout_typ) * |
| 2229 | (1 << qry.buf_write_timeout_max); |
| 2230 | |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2231 | /* round up when converting to ms */ |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2232 | info->buffer_write_tout = (tmp + 999) / 1000; |
| 2233 | tmp = (1 << qry.word_write_timeout_typ) * |
| 2234 | (1 << qry.word_write_timeout_max); |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2235 | /* round up when converting to ms */ |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2236 | info->write_tout = (tmp + 999) / 1000; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2237 | info->flash_id = FLASH_MAN_CFI; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2238 | if ((info->interface == FLASH_CFI_X8X16) && |
| 2239 | (info->chipwidth == FLASH_CFI_BY8)) { |
| 2240 | /* XXX - Need to test on x8/x16 in parallel. */ |
| 2241 | info->portwidth >>= 1; |
wdenk | ed2ac4b | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 2242 | } |
Mike Frysinger | 59404ee | 2008-10-02 01:55:38 -0400 | [diff] [blame] | 2243 | |
| 2244 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2247 | return (info->size); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Mike Frysinger | c2c093d | 2010-12-22 09:41:13 -0500 | [diff] [blame] | 2250 | #ifdef CONFIG_FLASH_CFI_MTD |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 2251 | void flash_set_verbose(uint v) |
| 2252 | { |
| 2253 | flash_verbose = v; |
| 2254 | } |
Mike Frysinger | c2c093d | 2010-12-22 09:41:13 -0500 | [diff] [blame] | 2255 | #endif |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 2256 | |
Stefan Roese | ab93564 | 2010-10-25 18:31:48 +0200 | [diff] [blame] | 2257 | static void cfi_flash_set_config_reg(u32 base, u16 val) |
| 2258 | { |
| 2259 | #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS |
| 2260 | /* |
| 2261 | * Only set this config register if really defined |
| 2262 | * to a valid value (0xffff is invalid) |
| 2263 | */ |
| 2264 | if (val == 0xffff) |
| 2265 | return; |
| 2266 | |
| 2267 | /* |
| 2268 | * Set configuration register. Data is "encrypted" in the 16 lower |
| 2269 | * address bits. |
| 2270 | */ |
| 2271 | flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1))); |
| 2272 | flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1))); |
| 2273 | |
| 2274 | /* |
| 2275 | * Finally issue reset-command to bring device back to |
| 2276 | * read-array mode |
| 2277 | */ |
| 2278 | flash_write16(FLASH_CMD_RESET, (void *)base); |
| 2279 | #endif |
| 2280 | } |
| 2281 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2282 | /*----------------------------------------------------------------------- |
| 2283 | */ |
Heiko Schocher | ef0946a | 2011-04-04 08:10:21 +0200 | [diff] [blame] | 2284 | |
| 2285 | void flash_protect_default(void) |
| 2286 | { |
Peter Tyser | 4f3c60d | 2011-04-13 11:46:56 -0500 | [diff] [blame] | 2287 | #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST) |
| 2288 | int i; |
| 2289 | struct apl_s { |
| 2290 | ulong start; |
| 2291 | ulong size; |
| 2292 | } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST; |
| 2293 | #endif |
| 2294 | |
Heiko Schocher | ef0946a | 2011-04-04 08:10:21 +0200 | [diff] [blame] | 2295 | /* Monitor protection ON by default */ |
| 2296 | #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \ |
| 2297 | (!defined(CONFIG_MONITOR_IS_IN_RAM)) |
| 2298 | flash_protect(FLAG_PROTECT_SET, |
| 2299 | CONFIG_SYS_MONITOR_BASE, |
| 2300 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, |
| 2301 | flash_get_info(CONFIG_SYS_MONITOR_BASE)); |
| 2302 | #endif |
| 2303 | |
| 2304 | /* Environment protection ON by default */ |
| 2305 | #ifdef CONFIG_ENV_IS_IN_FLASH |
| 2306 | flash_protect(FLAG_PROTECT_SET, |
| 2307 | CONFIG_ENV_ADDR, |
| 2308 | CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1, |
| 2309 | flash_get_info(CONFIG_ENV_ADDR)); |
| 2310 | #endif |
| 2311 | |
| 2312 | /* Redundant environment protection ON by default */ |
| 2313 | #ifdef CONFIG_ENV_ADDR_REDUND |
| 2314 | flash_protect(FLAG_PROTECT_SET, |
| 2315 | CONFIG_ENV_ADDR_REDUND, |
| 2316 | CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1, |
| 2317 | flash_get_info(CONFIG_ENV_ADDR_REDUND)); |
| 2318 | #endif |
| 2319 | |
| 2320 | #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST) |
Axel Lin | 85706c8 | 2013-06-23 00:56:46 +0800 | [diff] [blame] | 2321 | for (i = 0; i < ARRAY_SIZE(apl); i++) { |
Marek Vasut | cb1622e | 2011-10-21 14:17:05 +0000 | [diff] [blame] | 2322 | debug("autoprotecting from %08lx to %08lx\n", |
Heiko Schocher | ef0946a | 2011-04-04 08:10:21 +0200 | [diff] [blame] | 2323 | apl[i].start, apl[i].start + apl[i].size - 1); |
| 2324 | flash_protect(FLAG_PROTECT_SET, |
| 2325 | apl[i].start, |
| 2326 | apl[i].start + apl[i].size - 1, |
| 2327 | flash_get_info(apl[i].start)); |
| 2328 | } |
| 2329 | #endif |
| 2330 | } |
| 2331 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2332 | unsigned long flash_init (void) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2333 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2334 | unsigned long size = 0; |
| 2335 | int i; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2336 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2337 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Eric Schumann | 8f7ee7d | 2009-03-21 09:59:34 -0400 | [diff] [blame] | 2338 | /* read environment from EEPROM */ |
| 2339 | char s[64]; |
Wolfgang Denk | 76af278 | 2010-07-24 21:55:43 +0200 | [diff] [blame] | 2340 | getenv_f("unlock", s, sizeof(s)); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 2341 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2342 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2343 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2344 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2345 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2346 | |
Stefan Roese | ab93564 | 2010-10-25 18:31:48 +0200 | [diff] [blame] | 2347 | /* Optionally write flash configuration register */ |
| 2348 | cfi_flash_set_config_reg(cfi_flash_bank_addr(i), |
| 2349 | cfi_flash_config_reg(i)); |
| 2350 | |
Stefan Roese | 7e7dda8 | 2010-08-30 10:11:51 +0200 | [diff] [blame] | 2351 | if (!flash_detect_legacy(cfi_flash_bank_addr(i), i)) |
Anatolij Gustschin | 6acb77b | 2010-11-28 02:13:33 +0100 | [diff] [blame] | 2352 | flash_get_size(cfi_flash_bank_addr(i), i); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2353 | size += flash_info[i].size; |
| 2354 | if (flash_info[i].flash_id == FLASH_UNKNOWN) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2355 | #ifndef CONFIG_SYS_FLASH_QUIET_TEST |
Peter Tyser | 0b5a263 | 2010-12-28 18:12:05 -0600 | [diff] [blame] | 2356 | printf ("## Unknown flash on Bank %d " |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2357 | "- Size = 0x%08lx = %ld MB\n", |
| 2358 | i+1, flash_info[i].size, |
John Schmoller | 61665db | 2010-09-29 13:49:05 -0500 | [diff] [blame] | 2359 | flash_info[i].size >> 20); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2360 | #endif /* CONFIG_SYS_FLASH_QUIET_TEST */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2361 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2362 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Jeroen Hofstee | 5a85e89 | 2014-06-17 22:47:31 +0200 | [diff] [blame] | 2363 | else if (strcmp(s, "yes") == 0) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2364 | /* |
| 2365 | * Only the U-Boot image and it's environment |
| 2366 | * is protected, all other sectors are |
| 2367 | * unprotected (unlocked) if flash hardware |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2368 | * protection is used (CONFIG_SYS_FLASH_PROTECTION) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2369 | * and the environment variable "unlock" is |
| 2370 | * set to "yes". |
| 2371 | */ |
| 2372 | if (flash_info[i].legacy_unlock) { |
| 2373 | int k; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2374 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2375 | /* |
| 2376 | * Disable legacy_unlock temporarily, |
| 2377 | * since flash_real_protect would |
| 2378 | * relock all other sectors again |
| 2379 | * otherwise. |
| 2380 | */ |
| 2381 | flash_info[i].legacy_unlock = 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2382 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2383 | /* |
| 2384 | * Legacy unlocking (e.g. Intel J3) -> |
| 2385 | * unlock only one sector. This will |
| 2386 | * unlock all sectors. |
| 2387 | */ |
| 2388 | flash_real_protect (&flash_info[i], 0, 0); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2389 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2390 | flash_info[i].legacy_unlock = 1; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2391 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2392 | /* |
| 2393 | * Manually mark other sectors as |
| 2394 | * unlocked (unprotected) |
| 2395 | */ |
| 2396 | for (k = 1; k < flash_info[i].sector_count; k++) |
| 2397 | flash_info[i].protect[k] = 0; |
| 2398 | } else { |
| 2399 | /* |
| 2400 | * No legancy unlocking -> unlock all sectors |
| 2401 | */ |
| 2402 | flash_protect (FLAG_PROTECT_CLEAR, |
| 2403 | flash_info[i].start[0], |
| 2404 | flash_info[i].start[0] |
| 2405 | + flash_info[i].size - 1, |
| 2406 | &flash_info[i]); |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2407 | } |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2408 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2409 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2410 | } |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2411 | |
Heiko Schocher | ef0946a | 2011-04-04 08:10:21 +0200 | [diff] [blame] | 2412 | flash_protect_default(); |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 2413 | #ifdef CONFIG_FLASH_CFI_MTD |
| 2414 | cfi_mtd_init(); |
| 2415 | #endif |
| 2416 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2417 | return (size); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2418 | } |