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