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