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