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