wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1 | /* |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2 | * (C) Copyright 2002-2004 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 3 | * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com |
| 4 | * |
| 5 | * Copyright (C) 2003 Arabella Software Ltd. |
| 6 | * Yuli Barcohen <yuli@arabellasw.com> |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 7 | * |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 8 | * Copyright (C) 2004 |
| 9 | * Ed Okerson |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 |
| 12 | * Tolunay Orkun <listmember@orkun.us> |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 13 | * |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation; either version 2 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 30 | * MA 02111-1307 USA |
| 31 | * |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | /* The DEBUG define must be before common to enable debugging */ |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 35 | /* #define DEBUG */ |
| 36 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 37 | #include <common.h> |
| 38 | #include <asm/processor.h> |
Haiying Wang | c123a38 | 2007-02-21 16:52:31 +0100 | [diff] [blame] | 39 | #include <asm/io.h> |
wdenk | aeba06f | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 40 | #include <asm/byteorder.h> |
wdenk | d0245fc | 2005-04-13 10:02:42 +0000 | [diff] [blame] | 41 | #include <environment.h> |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 42 | #ifdef CFG_FLASH_CFI_DRIVER |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 43 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 44 | /* |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 45 | * This file implements a Common Flash Interface (CFI) driver for |
| 46 | * U-Boot. |
| 47 | * |
| 48 | * The width of the port and the width of the chips are determined at |
| 49 | * initialization. These widths are used to calculate the address for |
| 50 | * access CFI data structures. |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 51 | * |
| 52 | * References |
| 53 | * JEDEC Standard JESD68 - Common Flash Interface (CFI) |
| 54 | * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes |
| 55 | * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets |
| 56 | * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 57 | * AMD CFI Specification, Release 2.0 December 1, 2001 |
| 58 | * AMD/Spansion Application Note: Migration from Single-byte to Three-byte |
| 59 | * Device IDs, Publication Number 25538 Revision A, November 8, 2001 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 60 | * |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 61 | * Define CFG_WRITE_SWAPPED_DATA, if you have to swap the Bytes between |
Heiko Schocher | 800db31 | 2007-01-19 18:05:26 +0100 | [diff] [blame] | 62 | * reading and writing ... (yes there is such a Hardware). |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 63 | */ |
| 64 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 65 | #ifndef CFG_FLASH_BANKS_LIST |
| 66 | #define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE } |
| 67 | #endif |
| 68 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 69 | #define FLASH_CMD_CFI 0x98 |
| 70 | #define FLASH_CMD_READ_ID 0x90 |
| 71 | #define FLASH_CMD_RESET 0xff |
| 72 | #define FLASH_CMD_BLOCK_ERASE 0x20 |
| 73 | #define FLASH_CMD_ERASE_CONFIRM 0xD0 |
| 74 | #define FLASH_CMD_WRITE 0x40 |
| 75 | #define FLASH_CMD_PROTECT 0x60 |
| 76 | #define FLASH_CMD_PROTECT_SET 0x01 |
| 77 | #define FLASH_CMD_PROTECT_CLEAR 0xD0 |
| 78 | #define FLASH_CMD_CLEAR_STATUS 0x50 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 79 | #define FLASH_CMD_WRITE_TO_BUFFER 0xE8 |
| 80 | #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 81 | |
| 82 | #define FLASH_STATUS_DONE 0x80 |
| 83 | #define FLASH_STATUS_ESS 0x40 |
| 84 | #define FLASH_STATUS_ECLBS 0x20 |
| 85 | #define FLASH_STATUS_PSLBS 0x10 |
| 86 | #define FLASH_STATUS_VPENS 0x08 |
| 87 | #define FLASH_STATUS_PSS 0x04 |
| 88 | #define FLASH_STATUS_DPS 0x02 |
| 89 | #define FLASH_STATUS_R 0x01 |
| 90 | #define FLASH_STATUS_PROTECT 0x01 |
| 91 | |
| 92 | #define AMD_CMD_RESET 0xF0 |
| 93 | #define AMD_CMD_WRITE 0xA0 |
| 94 | #define AMD_CMD_ERASE_START 0x80 |
| 95 | #define AMD_CMD_ERASE_SECTOR 0x30 |
wdenk | ed2ac4b | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 96 | #define AMD_CMD_UNLOCK_START 0xAA |
| 97 | #define AMD_CMD_UNLOCK_ACK 0x55 |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 98 | #define AMD_CMD_WRITE_TO_BUFFER 0x25 |
| 99 | #define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 100 | |
| 101 | #define AMD_STATUS_TOGGLE 0x40 |
| 102 | #define AMD_STATUS_ERROR 0x20 |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 103 | |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 104 | #define FLASH_OFFSET_MANUFACTURER_ID 0x00 |
| 105 | #define FLASH_OFFSET_DEVICE_ID 0x01 |
| 106 | #define FLASH_OFFSET_DEVICE_ID2 0x0E |
| 107 | #define FLASH_OFFSET_DEVICE_ID3 0x0F |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 108 | #define FLASH_OFFSET_CFI 0x55 |
Wolfgang Denk | afa0dd0 | 2006-12-27 01:26:13 +0100 | [diff] [blame] | 109 | #define FLASH_OFFSET_CFI_ALT 0x555 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 110 | #define FLASH_OFFSET_CFI_RESP 0x10 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 111 | #define FLASH_OFFSET_PRIMARY_VENDOR 0x13 |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 112 | /* extended query table primary address */ |
| 113 | #define FLASH_OFFSET_EXT_QUERY_T_P_ADDR 0x15 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 114 | #define FLASH_OFFSET_WTOUT 0x1F |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 115 | #define FLASH_OFFSET_WBTOUT 0x20 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 116 | #define FLASH_OFFSET_ETOUT 0x21 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 117 | #define FLASH_OFFSET_CETOUT 0x22 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 118 | #define FLASH_OFFSET_WMAX_TOUT 0x23 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 119 | #define FLASH_OFFSET_WBMAX_TOUT 0x24 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 120 | #define FLASH_OFFSET_EMAX_TOUT 0x25 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 121 | #define FLASH_OFFSET_CEMAX_TOUT 0x26 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 122 | #define FLASH_OFFSET_SIZE 0x27 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 123 | #define FLASH_OFFSET_INTERFACE 0x28 |
| 124 | #define FLASH_OFFSET_BUFFER_SIZE 0x2A |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 125 | #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C |
| 126 | #define FLASH_OFFSET_ERASE_REGIONS 0x2D |
| 127 | #define FLASH_OFFSET_PROTECT 0x02 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 128 | #define FLASH_OFFSET_USER_PROTECTION 0x85 |
| 129 | #define FLASH_OFFSET_INTEL_PROTECTION 0x81 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 130 | |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 131 | #define CFI_CMDSET_NONE 0 |
| 132 | #define CFI_CMDSET_INTEL_EXTENDED 1 |
| 133 | #define CFI_CMDSET_AMD_STANDARD 2 |
| 134 | #define CFI_CMDSET_INTEL_STANDARD 3 |
| 135 | #define CFI_CMDSET_AMD_EXTENDED 4 |
| 136 | #define CFI_CMDSET_MITSU_STANDARD 256 |
| 137 | #define CFI_CMDSET_MITSU_EXTENDED 257 |
| 138 | #define CFI_CMDSET_SST 258 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 139 | |
wdenk | 5124278 | 2004-12-18 22:35:43 +0000 | [diff] [blame] | 140 | #ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */ |
| 141 | # undef FLASH_CMD_RESET |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 142 | # define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */ |
wdenk | 5124278 | 2004-12-18 22:35:43 +0000 | [diff] [blame] | 143 | #endif |
| 144 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 145 | typedef union { |
| 146 | unsigned char c; |
| 147 | unsigned short w; |
| 148 | unsigned long l; |
| 149 | unsigned long long ll; |
| 150 | } cfiword_t; |
| 151 | |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 152 | #define NUM_ERASE_REGIONS 4 /* max. number of erase regions */ |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 153 | |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 154 | static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT }; |
Wolfgang Denk | afa0dd0 | 2006-12-27 01:26:13 +0100 | [diff] [blame] | 155 | |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 156 | /* use CFG_MAX_FLASH_BANKS_DETECT if defined */ |
| 157 | #ifdef CFG_MAX_FLASH_BANKS_DETECT |
| 158 | static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST; |
| 159 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */ |
| 160 | #else |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 161 | static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST; |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 162 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */ |
| 163 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 164 | |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 165 | /* |
| 166 | * Check if chip width is defined. If not, start detecting with 8bit. |
| 167 | */ |
| 168 | #ifndef CFG_FLASH_CFI_WIDTH |
| 169 | #define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT |
| 170 | #endif |
| 171 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 172 | typedef unsigned long flash_sect_t; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 173 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 174 | static void flash_write8(u8 value, void *addr) |
| 175 | { |
| 176 | __raw_writeb(value, addr); |
| 177 | } |
| 178 | |
| 179 | static void flash_write16(u16 value, void *addr) |
| 180 | { |
| 181 | __raw_writew(value, addr); |
| 182 | } |
| 183 | |
| 184 | static void flash_write32(u32 value, void *addr) |
| 185 | { |
| 186 | __raw_writel(value, addr); |
| 187 | } |
| 188 | |
| 189 | static void flash_write64(u64 value, void *addr) |
| 190 | { |
| 191 | /* No architectures currently implement __raw_writeq() */ |
| 192 | *(volatile u64 *)addr = value; |
| 193 | } |
| 194 | |
| 195 | static u8 flash_read8(void *addr) |
| 196 | { |
| 197 | return __raw_readb(addr); |
| 198 | } |
| 199 | |
| 200 | static u16 flash_read16(void *addr) |
| 201 | { |
| 202 | return __raw_readw(addr); |
| 203 | } |
| 204 | |
| 205 | static u32 flash_read32(void *addr) |
| 206 | { |
| 207 | return __raw_readl(addr); |
| 208 | } |
| 209 | |
| 210 | static u64 flash_read64(void *addr) |
| 211 | { |
| 212 | /* No architectures currently implement __raw_readq() */ |
| 213 | return *(volatile u64 *)addr; |
| 214 | } |
| 215 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 216 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 217 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 218 | #if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE) |
| 219 | static flash_info_t *flash_get_info(ulong base) |
| 220 | { |
| 221 | int i; |
| 222 | flash_info_t * info = 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 223 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 224 | for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) { |
| 225 | info = & flash_info[i]; |
| 226 | if (info->size && info->start[0] <= base && |
| 227 | base <= info->start[0] + info->size - 1) |
| 228 | break; |
| 229 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 230 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 231 | return i == CFG_MAX_FLASH_BANKS ? 0 : info; |
| 232 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 233 | #endif |
| 234 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 235 | /*----------------------------------------------------------------------- |
| 236 | * create an address based on the offset and the port width |
| 237 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 238 | static inline uchar * |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 239 | flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 240 | { |
| 241 | return ((uchar *) (info->start[sect] + (offset * info->portwidth))); |
| 242 | } |
| 243 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 244 | /*----------------------------------------------------------------------- |
| 245 | * make a proper sized command based on the port and chip widths |
| 246 | */ |
| 247 | static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf) |
| 248 | { |
| 249 | int i; |
| 250 | uchar *cp = (uchar *) cmdbuf; |
| 251 | |
| 252 | #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA) |
| 253 | for (i = info->portwidth; i > 0; i--) |
| 254 | #else |
| 255 | for (i = 1; i <= info->portwidth; i++) |
| 256 | #endif |
| 257 | *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd; |
| 258 | } |
| 259 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 260 | #ifdef DEBUG |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 261 | /*----------------------------------------------------------------------- |
| 262 | * Debug support |
| 263 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 264 | static void print_longlong (char *str, unsigned long long data) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 265 | { |
| 266 | int i; |
| 267 | char *cp; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 268 | |
| 269 | cp = (unsigned char *) &data; |
| 270 | for (i = 0; i < 8; i++) |
| 271 | sprintf (&str[i * 2], "%2.2x", *cp++); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 272 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 273 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 274 | static void flash_printqry (flash_info_t * info, flash_sect_t sect) |
| 275 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 276 | void *addr; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 277 | int x, y; |
| 278 | |
Wolfgang Denk | 23f0d17 | 2005-10-09 00:25:58 +0200 | [diff] [blame] | 279 | for (x = 0; x < 0x40; x += 16U / info->portwidth) { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 280 | addr = flash_make_addr (info, sect, |
| 281 | x + FLASH_OFFSET_CFI_RESP); |
| 282 | debug ("%p : ", addr); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 283 | for (y = 0; y < 16; y++) { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 284 | debug ("%2.2x ", flash_read8(addr + y)); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 285 | } |
| 286 | debug (" "); |
| 287 | for (y = 0; y < 16; y++) { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 288 | unsigned char c = flash_read8(addr + y); |
| 289 | if (c >= 0x20 && c <= 0x7e) { |
| 290 | debug ("%c", c); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 291 | } else { |
| 292 | debug ("."); |
| 293 | } |
| 294 | } |
| 295 | debug ("\n"); |
| 296 | } |
| 297 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 298 | #endif |
| 299 | |
| 300 | |
| 301 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 302 | * read a character at a port width address |
| 303 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 304 | static inline uchar flash_read_uchar (flash_info_t * info, uint offset) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 305 | { |
| 306 | uchar *cp; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 307 | |
| 308 | cp = flash_make_addr (info, 0, offset); |
Heiko Schocher | 800db31 | 2007-01-19 18:05:26 +0100 | [diff] [blame] | 309 | #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 310 | return (cp[0]); |
| 311 | #else |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 312 | return (cp[info->portwidth - 1]); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 313 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /*----------------------------------------------------------------------- |
| 317 | * read a short word by swapping for ppc format. |
| 318 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 319 | static ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, |
| 320 | uint offset) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 321 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 322 | uchar *addr; |
| 323 | ushort retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 324 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 325 | #ifdef DEBUG |
| 326 | int x; |
| 327 | #endif |
| 328 | addr = flash_make_addr (info, sect, offset); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 329 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 330 | #ifdef DEBUG |
| 331 | debug ("ushort addr is at %p info->portwidth = %d\n", addr, |
| 332 | info->portwidth); |
| 333 | for (x = 0; x < 2 * info->portwidth; x++) { |
| 334 | debug ("addr[%x] = 0x%x\n", x, addr[x]); |
| 335 | } |
| 336 | #endif |
Heiko Schocher | 800db31 | 2007-01-19 18:05:26 +0100 | [diff] [blame] | 337 | #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 338 | retval = ((addr[(info->portwidth)] << 8) | addr[0]); |
| 339 | #else |
| 340 | retval = ((addr[(2 * info->portwidth) - 1] << 8) | |
| 341 | addr[info->portwidth - 1]); |
| 342 | #endif |
| 343 | |
| 344 | debug ("retval = 0x%x\n", retval); |
| 345 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /*----------------------------------------------------------------------- |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 349 | * read a long word by picking the least significant byte of each maximum |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 350 | * port size word. Swap for ppc format. |
| 351 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 352 | static ulong flash_read_long (flash_info_t * info, flash_sect_t sect, |
| 353 | uint offset) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 354 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 355 | uchar *addr; |
| 356 | ulong retval; |
| 357 | |
| 358 | #ifdef DEBUG |
| 359 | int x; |
| 360 | #endif |
| 361 | addr = flash_make_addr (info, sect, offset); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 362 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 363 | #ifdef DEBUG |
| 364 | debug ("long addr is at %p info->portwidth = %d\n", addr, |
| 365 | info->portwidth); |
| 366 | for (x = 0; x < 4 * info->portwidth; x++) { |
| 367 | debug ("addr[%x] = 0x%x\n", x, addr[x]); |
| 368 | } |
| 369 | #endif |
Heiko Schocher | 800db31 | 2007-01-19 18:05:26 +0100 | [diff] [blame] | 370 | #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 371 | retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) | |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 372 | (addr[(2 * info->portwidth)]) | |
| 373 | (addr[(3 * info->portwidth)] << 8); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 374 | #else |
| 375 | retval = (addr[(2 * info->portwidth) - 1] << 24) | |
| 376 | (addr[(info->portwidth) - 1] << 16) | |
| 377 | (addr[(4 * info->portwidth) - 1] << 8) | |
| 378 | addr[(3 * info->portwidth) - 1]; |
| 379 | #endif |
| 380 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 383 | /* |
| 384 | * Write a proper sized command to the correct address |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 385 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 386 | static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, |
| 387 | uint offset, uchar cmd) |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 388 | { |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 389 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 390 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 391 | cfiword_t cword; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 392 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 393 | addr = flash_make_addr (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 394 | flash_make_cmd (info, cmd, &cword); |
| 395 | switch (info->portwidth) { |
| 396 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 397 | debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 398 | cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 399 | flash_write8(cword.c, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 400 | break; |
| 401 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 402 | debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 403 | cmd, cword.w, |
| 404 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 405 | flash_write16(cword.w, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 406 | break; |
| 407 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 408 | debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 409 | cmd, cword.l, |
| 410 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 411 | flash_write32(cword.l, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 412 | break; |
| 413 | case FLASH_CFI_64BIT: |
| 414 | #ifdef DEBUG |
| 415 | { |
| 416 | char str[20]; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 417 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 418 | print_longlong (str, cword.ll); |
| 419 | |
| 420 | debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n", |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 421 | addr, cmd, str, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 422 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 423 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 424 | #endif |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 425 | flash_write64(cword.ll, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 426 | break; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 427 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 428 | |
| 429 | /* Ensure all the instructions are fully finished */ |
| 430 | sync(); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 431 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 432 | |
| 433 | static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect) |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 434 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 435 | flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START); |
| 436 | flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 437 | } |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 438 | |
| 439 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 440 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 441 | static int flash_isequal (flash_info_t * info, flash_sect_t sect, |
| 442 | uint offset, uchar cmd) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 443 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 444 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 445 | cfiword_t cword; |
| 446 | int retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 447 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 448 | addr = flash_make_addr (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 449 | flash_make_cmd (info, cmd, &cword); |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 450 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 451 | debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 452 | switch (info->portwidth) { |
| 453 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 454 | debug ("is= %x %x\n", flash_read8(addr), cword.c); |
| 455 | retval = (flash_read8(addr) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 456 | break; |
| 457 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 458 | debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w); |
| 459 | retval = (flash_read16(addr) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 460 | break; |
| 461 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 462 | debug ("is= %8.8lx %8.8lx\n", flash_read32(addr), cword.l); |
| 463 | retval = (flash_read32(addr) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 464 | break; |
| 465 | case FLASH_CFI_64BIT: |
| 466 | #ifdef DEBUG |
| 467 | { |
| 468 | char str1[20]; |
| 469 | char str2[20]; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 470 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 471 | print_longlong (str1, flash_read64(addr)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 472 | print_longlong (str2, cword.ll); |
| 473 | debug ("is= %s %s\n", str1, str2); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 474 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 475 | #endif |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 476 | retval = (flash_read64(addr) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 477 | break; |
| 478 | default: |
| 479 | retval = 0; |
| 480 | break; |
| 481 | } |
| 482 | return retval; |
| 483 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 484 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 485 | /*----------------------------------------------------------------------- |
| 486 | */ |
| 487 | static int flash_isset (flash_info_t * info, flash_sect_t sect, |
| 488 | uint offset, uchar cmd) |
| 489 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 490 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 491 | cfiword_t cword; |
| 492 | int retval; |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 493 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 494 | addr = flash_make_addr (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 495 | flash_make_cmd (info, cmd, &cword); |
| 496 | switch (info->portwidth) { |
| 497 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 498 | retval = ((flash_read8(addr) & cword.c) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 499 | break; |
| 500 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 501 | retval = ((flash_read16(addr) & cword.w) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 502 | break; |
| 503 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 504 | retval = ((flash_read16(addr) & cword.l) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 505 | break; |
| 506 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 507 | retval = ((flash_read64(addr) & cword.ll) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 508 | break; |
| 509 | default: |
| 510 | retval = 0; |
| 511 | break; |
| 512 | } |
| 513 | return retval; |
| 514 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 515 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 516 | /*----------------------------------------------------------------------- |
| 517 | */ |
| 518 | static int flash_toggle (flash_info_t * info, flash_sect_t sect, |
| 519 | uint offset, uchar cmd) |
| 520 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 521 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 522 | cfiword_t cword; |
| 523 | int retval; |
wdenk | e85b7a5 | 2004-10-10 22:16:06 +0000 | [diff] [blame] | 524 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 525 | addr = flash_make_addr (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 526 | flash_make_cmd (info, cmd, &cword); |
| 527 | switch (info->portwidth) { |
| 528 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 529 | retval = ((flash_read8(addr) & cword.c) != |
| 530 | (flash_read8(addr) & cword.c)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 531 | break; |
| 532 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 533 | retval = ((flash_read16(addr) & cword.w) != |
| 534 | (flash_read16(addr) & cword.w)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 535 | break; |
| 536 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 537 | retval = ((flash_read32(addr) & cword.l) != |
| 538 | (flash_read32(addr) & cword.l)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 539 | break; |
| 540 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 541 | retval = ((flash_read64(addr) & cword.ll) != |
| 542 | (flash_read64(addr) & cword.ll)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 543 | break; |
| 544 | default: |
| 545 | retval = 0; |
| 546 | break; |
| 547 | } |
| 548 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 551 | /* |
| 552 | * flash_is_busy - check to see if the flash is busy |
| 553 | * |
| 554 | * This routine checks the status of the chip and returns true if the |
| 555 | * chip is busy. |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 556 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 557 | static int flash_is_busy (flash_info_t * info, flash_sect_t sect) |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 558 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 559 | int retval; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 560 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 561 | switch (info->vendor) { |
| 562 | case CFI_CMDSET_INTEL_STANDARD: |
| 563 | case CFI_CMDSET_INTEL_EXTENDED: |
| 564 | retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE); |
| 565 | break; |
| 566 | case CFI_CMDSET_AMD_STANDARD: |
| 567 | case CFI_CMDSET_AMD_EXTENDED: |
| 568 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 569 | case CFI_CMDSET_AMD_LEGACY: |
| 570 | #endif |
| 571 | retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE); |
| 572 | break; |
| 573 | default: |
| 574 | retval = 0; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 575 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 576 | debug ("flash_is_busy: %d\n", retval); |
| 577 | return retval; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 581 | * wait for XSR.7 to be set. Time out with an error if it does not. |
| 582 | * This routine does not set the flash to read-array mode. |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 583 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 584 | static int flash_status_check (flash_info_t * info, flash_sect_t sector, |
| 585 | ulong tout, char *prompt) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 586 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 587 | ulong start; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 588 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 589 | #if CFG_HZ != 1000 |
| 590 | tout *= CFG_HZ/1000; |
| 591 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 592 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 593 | /* Wait for command completion */ |
| 594 | start = get_timer (0); |
| 595 | while (flash_is_busy (info, sector)) { |
| 596 | if (get_timer (start) > tout) { |
| 597 | printf ("Flash %s timeout at address %lx data %lx\n", |
| 598 | prompt, info->start[sector], |
| 599 | flash_read_long (info, sector, 0)); |
| 600 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
| 601 | return ERR_TIMOUT; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 602 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 603 | udelay (1); /* also triggers watchdog */ |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 604 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 605 | return ERR_OK; |
| 606 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 607 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 608 | /*----------------------------------------------------------------------- |
| 609 | * Wait for XSR.7 to be set, if it times out print an error, otherwise |
| 610 | * do a full status check. |
| 611 | * |
| 612 | * This routine sets the flash to read-array mode. |
| 613 | */ |
| 614 | static int flash_full_status_check (flash_info_t * info, flash_sect_t sector, |
| 615 | ulong tout, char *prompt) |
| 616 | { |
| 617 | int retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 618 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 619 | retcode = flash_status_check (info, sector, tout, prompt); |
| 620 | switch (info->vendor) { |
| 621 | case CFI_CMDSET_INTEL_EXTENDED: |
| 622 | case CFI_CMDSET_INTEL_STANDARD: |
| 623 | if ((retcode == ERR_OK) |
| 624 | && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) { |
| 625 | retcode = ERR_INVAL; |
| 626 | printf ("Flash %s error at address %lx\n", prompt, |
| 627 | info->start[sector]); |
| 628 | if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | |
| 629 | FLASH_STATUS_PSLBS)) { |
| 630 | puts ("Command Sequence Error.\n"); |
| 631 | } else if (flash_isset (info, sector, 0, |
| 632 | FLASH_STATUS_ECLBS)) { |
| 633 | puts ("Block Erase Error.\n"); |
| 634 | retcode = ERR_NOT_ERASED; |
| 635 | } else if (flash_isset (info, sector, 0, |
| 636 | FLASH_STATUS_PSLBS)) { |
| 637 | puts ("Locking Error\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 638 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 639 | if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) { |
| 640 | puts ("Block locked.\n"); |
| 641 | retcode = ERR_PROTECTED; |
| 642 | } |
| 643 | if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS)) |
| 644 | puts ("Vpp Low Error.\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 645 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 646 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
| 647 | break; |
| 648 | default: |
| 649 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 650 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 651 | return retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /*----------------------------------------------------------------------- |
| 655 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 656 | static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 657 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 658 | #if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA) |
| 659 | unsigned short w; |
| 660 | unsigned int l; |
| 661 | unsigned long long ll; |
| 662 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 663 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 664 | switch (info->portwidth) { |
| 665 | case FLASH_CFI_8BIT: |
| 666 | cword->c = c; |
| 667 | break; |
| 668 | case FLASH_CFI_16BIT: |
| 669 | #if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA) |
| 670 | w = c; |
| 671 | w <<= 8; |
| 672 | cword->w = (cword->w >> 8) | w; |
| 673 | #else |
| 674 | cword->w = (cword->w << 8) | c; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 675 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 676 | break; |
| 677 | case FLASH_CFI_32BIT: |
| 678 | #if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA) |
| 679 | l = c; |
| 680 | l <<= 24; |
| 681 | cword->l = (cword->l >> 8) | l; |
| 682 | #else |
| 683 | cword->l = (cword->l << 8) | c; |
| 684 | #endif |
| 685 | break; |
| 686 | case FLASH_CFI_64BIT: |
| 687 | #if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA) |
| 688 | ll = c; |
| 689 | ll <<= 56; |
| 690 | cword->ll = (cword->ll >> 8) | ll; |
| 691 | #else |
| 692 | cword->ll = (cword->ll << 8) | c; |
| 693 | #endif |
| 694 | break; |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 695 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 696 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 697 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 698 | /* loop through the sectors from the highest address when the passed |
| 699 | * address is greater or equal to the sector address we have a match |
| 700 | */ |
| 701 | static flash_sect_t find_sector (flash_info_t * info, ulong addr) |
| 702 | { |
| 703 | flash_sect_t sector; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 704 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 705 | for (sector = info->sector_count - 1; sector >= 0; sector--) { |
| 706 | if (addr >= info->start[sector]) |
| 707 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 708 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 709 | return sector; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 713 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 714 | static int flash_write_cfiword (flash_info_t * info, ulong dest, |
| 715 | cfiword_t cword) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 716 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 717 | void *ctladdr; |
| 718 | void *dstaddr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 719 | int flag; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 720 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 721 | ctladdr = flash_make_addr (info, 0, 0); |
| 722 | dstaddr = (uchar *)dest; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 723 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 724 | /* Check if Flash is (sufficiently) erased */ |
| 725 | switch (info->portwidth) { |
| 726 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 727 | flag = ((flash_read8(dstaddr) & cword.c) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 728 | break; |
| 729 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 730 | flag = ((flash_read16(dstaddr) & cword.w) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 731 | break; |
| 732 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 733 | flag = ((flash_read32(dstaddr) & cword.l) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 734 | break; |
| 735 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 736 | flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 737 | break; |
| 738 | default: |
| 739 | return 2; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 740 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 741 | if (!flag) |
| 742 | return 2; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 743 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 744 | /* Disable interrupts which might cause a timeout here */ |
| 745 | flag = disable_interrupts (); |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 746 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 747 | switch (info->vendor) { |
| 748 | case CFI_CMDSET_INTEL_EXTENDED: |
| 749 | case CFI_CMDSET_INTEL_STANDARD: |
| 750 | flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS); |
| 751 | flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE); |
| 752 | break; |
| 753 | case CFI_CMDSET_AMD_EXTENDED: |
| 754 | case CFI_CMDSET_AMD_STANDARD: |
| 755 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 756 | case CFI_CMDSET_AMD_LEGACY: |
| 757 | #endif |
| 758 | flash_unlock_seq (info, 0); |
| 759 | flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE); |
| 760 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 763 | switch (info->portwidth) { |
| 764 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 765 | flash_write8(cword.c, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 766 | break; |
| 767 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 768 | flash_write16(cword.w, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 769 | break; |
| 770 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 771 | flash_write32(cword.l, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 772 | break; |
| 773 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 774 | flash_write64(cword.ll, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 775 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 778 | /* re-enable interrupts if necessary */ |
| 779 | if (flag) |
| 780 | enable_interrupts (); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 781 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 782 | return flash_full_status_check (info, find_sector (info, dest), |
| 783 | info->write_tout, "write"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 786 | #ifdef CFG_FLASH_USE_BUFFER_WRITE |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 787 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 788 | static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, |
| 789 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 790 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 791 | flash_sect_t sector; |
| 792 | int cnt; |
| 793 | int retcode; |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 794 | void *src = cp; |
| 795 | void *dst = (void *)dest; |
| 796 | |
| 797 | sector = find_sector (info, dest); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 798 | |
| 799 | switch (info->vendor) { |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 800 | case CFI_CMDSET_INTEL_STANDARD: |
| 801 | case CFI_CMDSET_INTEL_EXTENDED: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 802 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 803 | flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER); |
| 804 | retcode = flash_status_check (info, sector, |
| 805 | info->buffer_write_tout, |
| 806 | "write to buffer"); |
| 807 | if (retcode == ERR_OK) { |
| 808 | /* reduce the number of loops by the width of |
| 809 | * the port */ |
| 810 | switch (info->portwidth) { |
| 811 | case FLASH_CFI_8BIT: |
| 812 | cnt = len; |
| 813 | break; |
| 814 | case FLASH_CFI_16BIT: |
| 815 | cnt = len >> 1; |
| 816 | break; |
| 817 | case FLASH_CFI_32BIT: |
| 818 | cnt = len >> 2; |
| 819 | break; |
| 820 | case FLASH_CFI_64BIT: |
| 821 | cnt = len >> 3; |
| 822 | break; |
| 823 | default: |
| 824 | return ERR_INVAL; |
| 825 | break; |
| 826 | } |
| 827 | flash_write_cmd (info, sector, 0, (uchar) cnt - 1); |
| 828 | while (cnt-- > 0) { |
| 829 | switch (info->portwidth) { |
| 830 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 831 | flash_write8(flash_read8(src), dst); |
| 832 | src += 1, dst += 1; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 833 | break; |
| 834 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 835 | flash_write16(flash_read16(src), dst); |
| 836 | src += 2, dst += 2; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 837 | break; |
| 838 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 839 | flash_write32(flash_read32(src), dst); |
| 840 | src += 4, dst += 4; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 841 | break; |
| 842 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 843 | flash_write64(flash_read64(src), dst); |
| 844 | src += 8, dst += 8; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 845 | break; |
| 846 | default: |
| 847 | return ERR_INVAL; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | flash_write_cmd (info, sector, 0, |
| 852 | FLASH_CMD_WRITE_BUFFER_CONFIRM); |
| 853 | retcode = flash_full_status_check ( |
| 854 | info, sector, info->buffer_write_tout, |
| 855 | "buffer write"); |
| 856 | } |
| 857 | return retcode; |
| 858 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 859 | case CFI_CMDSET_AMD_STANDARD: |
| 860 | case CFI_CMDSET_AMD_EXTENDED: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 861 | flash_unlock_seq(info,0); |
| 862 | flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER); |
| 863 | |
| 864 | switch (info->portwidth) { |
| 865 | case FLASH_CFI_8BIT: |
| 866 | cnt = len; |
| 867 | flash_write_cmd (info, sector, 0, (uchar) cnt - 1); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 868 | while (cnt-- > 0) { |
| 869 | flash_write8(flash_read8(src), dst); |
| 870 | src += 1, dst += 1; |
| 871 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 872 | break; |
| 873 | case FLASH_CFI_16BIT: |
| 874 | cnt = len >> 1; |
| 875 | flash_write_cmd (info, sector, 0, (uchar) cnt - 1); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 876 | while (cnt-- > 0) { |
| 877 | flash_write16(flash_read16(src), dst); |
| 878 | src += 2, dst += 2; |
| 879 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 880 | break; |
| 881 | case FLASH_CFI_32BIT: |
| 882 | cnt = len >> 2; |
| 883 | flash_write_cmd (info, sector, 0, (uchar) cnt - 1); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 884 | while (cnt-- > 0) { |
| 885 | flash_write32(flash_read32(src), dst); |
| 886 | src += 4, dst += 4; |
| 887 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 888 | break; |
| 889 | case FLASH_CFI_64BIT: |
| 890 | cnt = len >> 3; |
| 891 | flash_write_cmd (info, sector, 0, (uchar) cnt - 1); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame^] | 892 | while (cnt-- > 0) { |
| 893 | flash_write64(flash_read64(src), dst); |
| 894 | src += 8, dst += 8; |
| 895 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 896 | break; |
| 897 | default: |
| 898 | return ERR_INVAL; |
| 899 | } |
| 900 | |
| 901 | flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM); |
| 902 | retcode = flash_full_status_check (info, sector, |
| 903 | info->buffer_write_tout, |
| 904 | "buffer write"); |
| 905 | return retcode; |
| 906 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 907 | default: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 908 | debug ("Unknown Command Set\n"); |
| 909 | return ERR_INVAL; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 910 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 911 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 912 | #endif /* CFG_FLASH_USE_BUFFER_WRITE */ |
| 913 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 914 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 915 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 916 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 917 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 918 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 919 | int rcode = 0; |
| 920 | int prot; |
| 921 | flash_sect_t sect; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 922 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 923 | if (info->flash_id != FLASH_MAN_CFI) { |
| 924 | puts ("Can't erase unknown flash type - aborted\n"); |
| 925 | return 1; |
| 926 | } |
| 927 | if ((s_first < 0) || (s_first > s_last)) { |
| 928 | puts ("- no sectors to erase\n"); |
| 929 | return 1; |
| 930 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 931 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 932 | prot = 0; |
| 933 | for (sect = s_first; sect <= s_last; ++sect) { |
| 934 | if (info->protect[sect]) { |
| 935 | prot++; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 938 | if (prot) { |
| 939 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 940 | prot); |
| 941 | } else { |
| 942 | putc ('\n'); |
| 943 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 944 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 945 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 946 | for (sect = s_first; sect <= s_last; sect++) { |
| 947 | if (info->protect[sect] == 0) { /* not protected */ |
| 948 | switch (info->vendor) { |
| 949 | case CFI_CMDSET_INTEL_STANDARD: |
| 950 | case CFI_CMDSET_INTEL_EXTENDED: |
| 951 | flash_write_cmd (info, sect, 0, |
| 952 | FLASH_CMD_CLEAR_STATUS); |
| 953 | flash_write_cmd (info, sect, 0, |
| 954 | FLASH_CMD_BLOCK_ERASE); |
| 955 | flash_write_cmd (info, sect, 0, |
| 956 | FLASH_CMD_ERASE_CONFIRM); |
| 957 | break; |
| 958 | case CFI_CMDSET_AMD_STANDARD: |
| 959 | case CFI_CMDSET_AMD_EXTENDED: |
| 960 | flash_unlock_seq (info, sect); |
| 961 | flash_write_cmd (info, sect, |
| 962 | info->addr_unlock1, |
| 963 | AMD_CMD_ERASE_START); |
| 964 | flash_unlock_seq (info, sect); |
| 965 | flash_write_cmd (info, sect, 0, |
| 966 | AMD_CMD_ERASE_SECTOR); |
| 967 | break; |
| 968 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 969 | case CFI_CMDSET_AMD_LEGACY: |
| 970 | flash_unlock_seq (info, 0); |
| 971 | flash_write_cmd (info, 0, info->addr_unlock1, |
| 972 | AMD_CMD_ERASE_START); |
| 973 | flash_unlock_seq (info, 0); |
| 974 | flash_write_cmd (info, sect, 0, |
| 975 | AMD_CMD_ERASE_SECTOR); |
| 976 | break; |
| 977 | #endif |
| 978 | default: |
| 979 | debug ("Unkown flash vendor %d\n", |
| 980 | info->vendor); |
| 981 | break; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 982 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 983 | |
| 984 | if (flash_full_status_check |
| 985 | (info, sect, info->erase_blk_tout, "erase")) { |
| 986 | rcode = 1; |
| 987 | } else |
| 988 | putc ('.'); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 989 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 990 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 991 | puts (" done\n"); |
| 992 | return rcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 993 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 994 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 995 | /*----------------------------------------------------------------------- |
| 996 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 997 | void flash_print_info (flash_info_t * info) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 998 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 999 | int i; |
wdenk | 369d43d | 2004-03-14 14:09:05 +0000 | [diff] [blame] | 1000 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1001 | if (info->flash_id != FLASH_MAN_CFI) { |
| 1002 | puts ("missing or unknown FLASH type\n"); |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | printf ("%s FLASH (%d x %d)", |
| 1007 | info->name, |
| 1008 | (info->portwidth << 3), (info->chipwidth << 3)); |
| 1009 | if (info->size < 1024*1024) |
| 1010 | printf (" Size: %ld kB in %d Sectors\n", |
| 1011 | info->size >> 10, info->sector_count); |
| 1012 | else |
| 1013 | printf (" Size: %ld MB in %d Sectors\n", |
| 1014 | info->size >> 20, info->sector_count); |
| 1015 | printf (" "); |
| 1016 | switch (info->vendor) { |
| 1017 | case CFI_CMDSET_INTEL_STANDARD: |
| 1018 | printf ("Intel Standard"); |
| 1019 | break; |
| 1020 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1021 | printf ("Intel Extended"); |
| 1022 | break; |
| 1023 | case CFI_CMDSET_AMD_STANDARD: |
| 1024 | printf ("AMD Standard"); |
| 1025 | break; |
| 1026 | case CFI_CMDSET_AMD_EXTENDED: |
| 1027 | printf ("AMD Extended"); |
| 1028 | break; |
| 1029 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1030 | case CFI_CMDSET_AMD_LEGACY: |
| 1031 | printf ("AMD Legacy"); |
| 1032 | break; |
wdenk | 369d43d | 2004-03-14 14:09:05 +0000 | [diff] [blame] | 1033 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1034 | default: |
| 1035 | printf ("Unknown (%d)", info->vendor); |
| 1036 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1037 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1038 | printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X", |
| 1039 | info->manufacturer_id, info->device_id); |
| 1040 | if (info->device_id == 0x7E) { |
| 1041 | printf("%04X", info->device_id2); |
| 1042 | } |
| 1043 | printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n", |
| 1044 | info->erase_blk_tout, |
| 1045 | info->write_tout); |
| 1046 | if (info->buffer_size > 1) { |
| 1047 | printf (" Buffer write timeout: %ld ms, " |
| 1048 | "buffer size: %d bytes\n", |
| 1049 | info->buffer_write_tout, |
| 1050 | info->buffer_size); |
| 1051 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1052 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1053 | puts ("\n Sector Start Addresses:"); |
| 1054 | for (i = 0; i < info->sector_count; ++i) { |
| 1055 | if ((i % 5) == 0) |
| 1056 | printf ("\n"); |
| 1057 | #ifdef CFG_FLASH_EMPTY_INFO |
| 1058 | int k; |
| 1059 | int size; |
| 1060 | int erased; |
| 1061 | volatile unsigned long *flash; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1062 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1063 | /* |
| 1064 | * Check if whole sector is erased |
| 1065 | */ |
| 1066 | if (i != (info->sector_count - 1)) |
| 1067 | size = info->start[i + 1] - info->start[i]; |
| 1068 | else |
| 1069 | size = info->start[0] + info->size - info->start[i]; |
| 1070 | erased = 1; |
| 1071 | flash = (volatile unsigned long *) info->start[i]; |
| 1072 | size = size >> 2; /* divide by 4 for longword access */ |
| 1073 | for (k = 0; k < size; k++) { |
| 1074 | if (*flash++ != 0xffffffff) { |
| 1075 | erased = 0; |
| 1076 | break; |
| 1077 | } |
| 1078 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1079 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1080 | /* print empty and read-only info */ |
| 1081 | printf (" %08lX %c %s ", |
| 1082 | info->start[i], |
| 1083 | erased ? 'E' : ' ', |
| 1084 | info->protect[i] ? "RO" : " "); |
| 1085 | #else /* ! CFG_FLASH_EMPTY_INFO */ |
| 1086 | printf (" %08lX %s ", |
| 1087 | info->start[i], |
| 1088 | info->protect[i] ? "RO" : " "); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1089 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1090 | } |
| 1091 | putc ('\n'); |
| 1092 | return; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1095 | /*----------------------------------------------------------------------- |
| 1096 | * Copy memory to flash, returns: |
| 1097 | * 0 - OK |
| 1098 | * 1 - write timeout |
| 1099 | * 2 - Flash not erased |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1100 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1101 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1102 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1103 | ulong wp; |
| 1104 | ulong cp; |
| 1105 | int aln; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1106 | cfiword_t cword; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1107 | int i, rc; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1108 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1109 | #ifdef CFG_FLASH_USE_BUFFER_WRITE |
| 1110 | int buffered_size; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1111 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1112 | /* get lower aligned address */ |
| 1113 | /* get lower aligned address */ |
| 1114 | wp = (addr & ~(info->portwidth - 1)); |
Haiying Wang | c123a38 | 2007-02-21 16:52:31 +0100 | [diff] [blame] | 1115 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1116 | /* handle unaligned start */ |
| 1117 | if ((aln = addr - wp) != 0) { |
| 1118 | cword.l = 0; |
| 1119 | cp = wp; |
| 1120 | for (i = 0; i < aln; ++i, ++cp) |
| 1121 | flash_add_byte (info, &cword, (*(uchar *) cp)); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1122 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1123 | for (; (i < info->portwidth) && (cnt > 0); i++) { |
| 1124 | flash_add_byte (info, &cword, *src++); |
| 1125 | cnt--; |
| 1126 | cp++; |
| 1127 | } |
| 1128 | for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp) |
| 1129 | flash_add_byte (info, &cword, (*(uchar *) cp)); |
| 1130 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
| 1131 | return rc; |
| 1132 | wp = cp; |
| 1133 | } |
| 1134 | |
| 1135 | /* handle the aligned part */ |
| 1136 | #ifdef CFG_FLASH_USE_BUFFER_WRITE |
| 1137 | buffered_size = (info->portwidth / info->chipwidth); |
| 1138 | buffered_size *= info->buffer_size; |
| 1139 | while (cnt >= info->portwidth) { |
| 1140 | /* prohibit buffer write when buffer_size is 1 */ |
| 1141 | if (info->buffer_size == 1) { |
| 1142 | cword.l = 0; |
| 1143 | for (i = 0; i < info->portwidth; i++) |
| 1144 | flash_add_byte (info, &cword, *src++); |
| 1145 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
| 1146 | return rc; |
| 1147 | wp += info->portwidth; |
| 1148 | cnt -= info->portwidth; |
| 1149 | continue; |
| 1150 | } |
| 1151 | |
| 1152 | /* write buffer until next buffered_size aligned boundary */ |
| 1153 | i = buffered_size - (wp % buffered_size); |
| 1154 | if (i > cnt) |
| 1155 | i = cnt; |
| 1156 | if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK) |
| 1157 | return rc; |
| 1158 | i -= i & (info->portwidth - 1); |
| 1159 | wp += i; |
| 1160 | src += i; |
| 1161 | cnt -= i; |
| 1162 | } |
| 1163 | #else |
| 1164 | while (cnt >= info->portwidth) { |
| 1165 | cword.l = 0; |
| 1166 | for (i = 0; i < info->portwidth; i++) { |
| 1167 | flash_add_byte (info, &cword, *src++); |
| 1168 | } |
| 1169 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
| 1170 | return rc; |
| 1171 | wp += info->portwidth; |
| 1172 | cnt -= info->portwidth; |
| 1173 | } |
| 1174 | #endif /* CFG_FLASH_USE_BUFFER_WRITE */ |
| 1175 | if (cnt == 0) { |
| 1176 | return (0); |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * handle unaligned tail bytes |
| 1181 | */ |
| 1182 | cword.l = 0; |
| 1183 | for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) { |
| 1184 | flash_add_byte (info, &cword, *src++); |
| 1185 | --cnt; |
| 1186 | } |
| 1187 | for (; i < info->portwidth; ++i, ++cp) { |
| 1188 | flash_add_byte (info, &cword, (*(uchar *) cp)); |
| 1189 | } |
| 1190 | |
| 1191 | return flash_write_cfiword (info, wp, cword); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1192 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1193 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1194 | /*----------------------------------------------------------------------- |
| 1195 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1196 | #ifdef CFG_FLASH_PROTECTION |
| 1197 | |
| 1198 | int flash_real_protect (flash_info_t * info, long sector, int prot) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1199 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1200 | int retcode = 0; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1201 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1202 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 1203 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT); |
| 1204 | if (prot) |
| 1205 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET); |
| 1206 | else |
| 1207 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1208 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1209 | if ((retcode = |
| 1210 | flash_full_status_check (info, sector, info->erase_blk_tout, |
| 1211 | prot ? "protect" : "unprotect")) == 0) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1212 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1213 | info->protect[sector] = prot; |
| 1214 | |
| 1215 | /* |
| 1216 | * On some of Intel's flash chips (marked via legacy_unlock) |
| 1217 | * unprotect unprotects all locking. |
| 1218 | */ |
| 1219 | if ((prot == 0) && (info->legacy_unlock)) { |
| 1220 | flash_sect_t i; |
| 1221 | |
| 1222 | for (i = 0; i < info->sector_count; i++) { |
| 1223 | if (info->protect[i]) |
| 1224 | flash_real_protect (info, i, 1); |
| 1225 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1226 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1227 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1228 | return retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1229 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1230 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1231 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1232 | * flash_read_user_serial - read the OneTimeProgramming cells |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1233 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1234 | void flash_read_user_serial (flash_info_t * info, void *buffer, int offset, |
| 1235 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1236 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1237 | uchar *src; |
| 1238 | uchar *dst; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1239 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1240 | dst = buffer; |
| 1241 | src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION); |
| 1242 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 1243 | memcpy (dst, src + offset, len); |
| 1244 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1247 | /* |
| 1248 | * flash_read_factory_serial - read the device Id from the protection area |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1249 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1250 | void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset, |
| 1251 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1252 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1253 | uchar *src; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1254 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1255 | src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION); |
| 1256 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 1257 | memcpy (buffer, src + offset, len); |
| 1258 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1261 | #endif /* CFG_FLASH_PROTECTION */ |
| 1262 | |
| 1263 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1264 | /*----------------------------------------------------------------------- |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1265 | * read jedec ids from device and set corresponding fields in info struct |
| 1266 | * |
| 1267 | * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct |
| 1268 | * |
| 1269 | */ |
| 1270 | static void flash_read_jedec_ids (flash_info_t * info) |
| 1271 | { |
| 1272 | info->manufacturer_id = 0; |
| 1273 | info->device_id = 0; |
| 1274 | info->device_id2 = 0; |
| 1275 | |
| 1276 | switch (info->vendor) { |
| 1277 | case CFI_CMDSET_INTEL_STANDARD: |
| 1278 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1279 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1280 | flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID); |
| 1281 | udelay(1000); /* some flash are slow to respond */ |
| 1282 | info->manufacturer_id = flash_read_uchar (info, |
| 1283 | FLASH_OFFSET_MANUFACTURER_ID); |
| 1284 | info->device_id = flash_read_uchar (info, |
| 1285 | FLASH_OFFSET_DEVICE_ID); |
| 1286 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1287 | break; |
| 1288 | case CFI_CMDSET_AMD_STANDARD: |
| 1289 | case CFI_CMDSET_AMD_EXTENDED: |
| 1290 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
| 1291 | flash_unlock_seq(info, 0); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1292 | flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1293 | udelay(1000); /* some flash are slow to respond */ |
| 1294 | info->manufacturer_id = flash_read_uchar (info, |
| 1295 | FLASH_OFFSET_MANUFACTURER_ID); |
| 1296 | info->device_id = flash_read_uchar (info, |
| 1297 | FLASH_OFFSET_DEVICE_ID); |
| 1298 | if (info->device_id == 0x7E) { |
| 1299 | /* AMD 3-byte (expanded) device ids */ |
| 1300 | info->device_id2 = flash_read_uchar (info, |
| 1301 | FLASH_OFFSET_DEVICE_ID2); |
| 1302 | info->device_id2 <<= 8; |
| 1303 | info->device_id2 |= flash_read_uchar (info, |
| 1304 | FLASH_OFFSET_DEVICE_ID3); |
| 1305 | } |
| 1306 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
| 1307 | break; |
| 1308 | default: |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
| 1312 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1313 | #ifdef CONFIG_FLASH_CFI_LEGACY |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1314 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1315 | * Call board code to request info about non-CFI flash. |
| 1316 | * board_flash_get_legacy needs to fill in at least: |
| 1317 | * info->portwidth, info->chipwidth and info->interface for Jedec probing. |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1318 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1319 | static int flash_detect_legacy(ulong base, int banknum) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1320 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1321 | flash_info_t *info = &flash_info[banknum]; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1322 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1323 | if (board_flash_get_legacy(base, banknum, info)) { |
| 1324 | /* board code may have filled info completely. If not, we |
| 1325 | use JEDEC ID probing. */ |
| 1326 | if (!info->vendor) { |
| 1327 | int modes[] = { |
| 1328 | CFI_CMDSET_AMD_STANDARD, |
| 1329 | CFI_CMDSET_INTEL_STANDARD |
| 1330 | }; |
| 1331 | int i; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1332 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1333 | for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) { |
| 1334 | info->vendor = modes[i]; |
| 1335 | info->start[0] = base; |
| 1336 | if (info->portwidth == FLASH_CFI_8BIT |
| 1337 | && info->interface == FLASH_CFI_X8X16) { |
| 1338 | info->addr_unlock1 = 0x2AAA; |
| 1339 | info->addr_unlock2 = 0x5555; |
| 1340 | } else { |
| 1341 | info->addr_unlock1 = 0x5555; |
| 1342 | info->addr_unlock2 = 0x2AAA; |
| 1343 | } |
| 1344 | flash_read_jedec_ids(info); |
| 1345 | debug("JEDEC PROBE: ID %x %x %x\n", |
| 1346 | info->manufacturer_id, |
| 1347 | info->device_id, |
| 1348 | info->device_id2); |
| 1349 | if (jedec_flash_match(info, base)) |
| 1350 | break; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | switch(info->vendor) { |
| 1355 | case CFI_CMDSET_INTEL_STANDARD: |
| 1356 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1357 | info->cmd_reset = FLASH_CMD_RESET; |
| 1358 | break; |
| 1359 | case CFI_CMDSET_AMD_STANDARD: |
| 1360 | case CFI_CMDSET_AMD_EXTENDED: |
| 1361 | case CFI_CMDSET_AMD_LEGACY: |
| 1362 | info->cmd_reset = AMD_CMD_RESET; |
| 1363 | break; |
| 1364 | } |
| 1365 | info->flash_id = FLASH_MAN_CFI; |
| 1366 | return 1; |
| 1367 | } |
| 1368 | return 0; /* use CFI */ |
| 1369 | } |
| 1370 | #else |
| 1371 | static inline int flash_detect_legacy(ulong base, int banknum) |
| 1372 | { |
| 1373 | return 0; /* use CFI */ |
| 1374 | } |
| 1375 | #endif |
| 1376 | |
| 1377 | /*----------------------------------------------------------------------- |
| 1378 | * detect if flash is compatible with the Common Flash Interface (CFI) |
| 1379 | * http://www.jedec.org/download/search/jesd68.pdf |
| 1380 | */ |
| 1381 | static int __flash_detect_cfi (flash_info_t * info) |
| 1382 | { |
| 1383 | int cfi_offset; |
| 1384 | |
| 1385 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
| 1386 | for (cfi_offset=0; |
| 1387 | cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint); |
| 1388 | cfi_offset++) { |
| 1389 | flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], |
| 1390 | FLASH_CMD_CFI); |
| 1391 | if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q') |
| 1392 | && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') |
| 1393 | && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) { |
| 1394 | info->interface = flash_read_ushort (info, 0, |
| 1395 | FLASH_OFFSET_INTERFACE); |
| 1396 | info->cfi_offset = flash_offset_cfi[cfi_offset]; |
| 1397 | debug ("device interface is %d\n", |
| 1398 | info->interface); |
| 1399 | debug ("found port %d chip %d ", |
| 1400 | info->portwidth, info->chipwidth); |
| 1401 | debug ("port %d bits chip %d bits\n", |
| 1402 | info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 1403 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 1404 | |
| 1405 | /* calculate command offsets as in the Linux driver */ |
| 1406 | info->addr_unlock1 = 0x555; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1407 | info->addr_unlock2 = 0x2aa; |
| 1408 | |
| 1409 | /* |
| 1410 | * modify the unlock address if we are |
| 1411 | * in compatibility mode |
| 1412 | */ |
| 1413 | if ( /* x8/x16 in x8 mode */ |
| 1414 | ((info->chipwidth == FLASH_CFI_BY8) && |
| 1415 | (info->interface == FLASH_CFI_X8X16)) || |
| 1416 | /* x16/x32 in x16 mode */ |
| 1417 | ((info->chipwidth == FLASH_CFI_BY16) && |
| 1418 | (info->interface == FLASH_CFI_X16X32))) |
| 1419 | { |
| 1420 | info->addr_unlock1 = 0xaaa; |
| 1421 | info->addr_unlock2 = 0x555; |
| 1422 | } |
| 1423 | |
| 1424 | info->name = "CFI conformant"; |
| 1425 | return 1; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | static int flash_detect_cfi (flash_info_t * info) |
| 1433 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1434 | debug ("flash detect cfi\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1435 | |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1436 | for (info->portwidth = CFG_FLASH_CFI_WIDTH; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1437 | info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) { |
| 1438 | for (info->chipwidth = FLASH_CFI_BY8; |
| 1439 | info->chipwidth <= info->portwidth; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1440 | info->chipwidth <<= 1) |
| 1441 | if (__flash_detect_cfi(info)) |
| 1442 | return 1; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1443 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1444 | debug ("not found\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1445 | return 0; |
| 1446 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1447 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1448 | /* |
| 1449 | * The following code cannot be run from FLASH! |
| 1450 | * |
| 1451 | */ |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 1452 | ulong flash_get_size (ulong base, int banknum) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1453 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1454 | flash_info_t *info = &flash_info[banknum]; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1455 | int i, j; |
| 1456 | flash_sect_t sect_cnt; |
| 1457 | unsigned long sector; |
| 1458 | unsigned long tmp; |
| 1459 | int size_ratio; |
| 1460 | uchar num_erase_regions; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1461 | int erase_region_size; |
| 1462 | int erase_region_count; |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1463 | int geometry_reversed = 0; |
| 1464 | |
| 1465 | info->ext_addr = 0; |
| 1466 | info->cfi_version = 0; |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1467 | #ifdef CFG_FLASH_PROTECTION |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1468 | info->legacy_unlock = 0; |
| 1469 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1470 | |
| 1471 | info->start[0] = base; |
| 1472 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1473 | if (flash_detect_cfi (info)) { |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1474 | info->vendor = flash_read_ushort (info, 0, |
| 1475 | FLASH_OFFSET_PRIMARY_VENDOR); |
| 1476 | flash_read_jedec_ids (info); |
Stefano Babic | 5f172a8 | 2006-12-12 00:22:42 +0100 | [diff] [blame] | 1477 | flash_write_cmd (info, 0, info->cfi_offset, FLASH_CMD_CFI); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1478 | num_erase_regions = flash_read_uchar (info, |
| 1479 | FLASH_OFFSET_NUM_ERASE_REGIONS); |
| 1480 | info->ext_addr = flash_read_ushort (info, 0, |
| 1481 | FLASH_OFFSET_EXT_QUERY_T_P_ADDR); |
| 1482 | if (info->ext_addr) { |
| 1483 | info->cfi_version = (ushort) flash_read_uchar (info, |
| 1484 | info->ext_addr + 3) << 8; |
| 1485 | info->cfi_version |= (ushort) flash_read_uchar (info, |
| 1486 | info->ext_addr + 4); |
| 1487 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1488 | #ifdef DEBUG |
| 1489 | flash_printqry (info, 0); |
| 1490 | #endif |
| 1491 | switch (info->vendor) { |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1492 | case CFI_CMDSET_INTEL_STANDARD: |
| 1493 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1494 | default: |
| 1495 | info->cmd_reset = FLASH_CMD_RESET; |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1496 | #ifdef CFG_FLASH_PROTECTION |
| 1497 | /* read legacy lock/unlock bit from intel flash */ |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1498 | if (info->ext_addr) { |
| 1499 | info->legacy_unlock = flash_read_uchar (info, |
| 1500 | info->ext_addr + 5) & 0x08; |
| 1501 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1502 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1503 | break; |
| 1504 | case CFI_CMDSET_AMD_STANDARD: |
| 1505 | case CFI_CMDSET_AMD_EXTENDED: |
| 1506 | info->cmd_reset = AMD_CMD_RESET; |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1507 | /* check if flash geometry needs reversal */ |
| 1508 | if (num_erase_regions <= 1) |
| 1509 | break; |
| 1510 | /* reverse geometry if top boot part */ |
| 1511 | if (info->cfi_version < 0x3131) { |
| 1512 | /* CFI < 1.1, try to guess from device id */ |
| 1513 | if ((info->device_id & 0x80) != 0) { |
| 1514 | geometry_reversed = 1; |
| 1515 | } |
| 1516 | break; |
| 1517 | } |
| 1518 | /* CFI >= 1.1, deduct from top/bottom flag */ |
| 1519 | /* note: ext_addr is valid since cfi_version > 0 */ |
| 1520 | if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) { |
| 1521 | geometry_reversed = 1; |
| 1522 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1523 | break; |
| 1524 | } |
wdenk | 6cfa84e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 1525 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1526 | debug ("manufacturer is %d\n", info->vendor); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1527 | debug ("manufacturer id is 0x%x\n", info->manufacturer_id); |
| 1528 | debug ("device id is 0x%x\n", info->device_id); |
| 1529 | debug ("device id2 is 0x%x\n", info->device_id2); |
| 1530 | debug ("cfi version is 0x%04x\n", info->cfi_version); |
| 1531 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1532 | size_ratio = info->portwidth / info->chipwidth; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1533 | /* if the chip is x8/x16 reduce the ratio by half */ |
| 1534 | if ((info->interface == FLASH_CFI_X8X16) |
| 1535 | && (info->chipwidth == FLASH_CFI_BY8)) { |
| 1536 | size_ratio >>= 1; |
| 1537 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1538 | debug ("size_ratio %d port %d bits chip %d bits\n", |
| 1539 | size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 1540 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 1541 | debug ("found %d erase regions\n", num_erase_regions); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1542 | sect_cnt = 0; |
| 1543 | sector = base; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1544 | for (i = 0; i < num_erase_regions; i++) { |
| 1545 | if (i > NUM_ERASE_REGIONS) { |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 1546 | printf ("%d erase regions found, only %d used\n", |
| 1547 | num_erase_regions, NUM_ERASE_REGIONS); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1548 | break; |
| 1549 | } |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1550 | if (geometry_reversed) |
| 1551 | tmp = flash_read_long (info, 0, |
| 1552 | FLASH_OFFSET_ERASE_REGIONS + |
| 1553 | (num_erase_regions - 1 - i) * 4); |
| 1554 | else |
| 1555 | tmp = flash_read_long (info, 0, |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1556 | FLASH_OFFSET_ERASE_REGIONS + |
| 1557 | i * 4); |
| 1558 | erase_region_size = |
| 1559 | (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1560 | tmp >>= 16; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1561 | erase_region_count = (tmp & 0xffff) + 1; |
wdenk | aeba06f | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 1562 | debug ("erase_region_count = %d erase_region_size = %d\n", |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 1563 | erase_region_count, erase_region_size); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1564 | for (j = 0; j < erase_region_count; j++) { |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1565 | if (sect_cnt >= CFG_MAX_FLASH_SECT) { |
| 1566 | printf("ERROR: too many flash sectors\n"); |
| 1567 | break; |
| 1568 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1569 | info->start[sect_cnt] = sector; |
| 1570 | sector += (erase_region_size * size_ratio); |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1573 | * Only read protection status from |
| 1574 | * supported devices (intel...) |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 1575 | */ |
| 1576 | switch (info->vendor) { |
| 1577 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1578 | case CFI_CMDSET_INTEL_STANDARD: |
| 1579 | info->protect[sect_cnt] = |
| 1580 | flash_isset (info, sect_cnt, |
| 1581 | FLASH_OFFSET_PROTECT, |
| 1582 | FLASH_STATUS_PROTECT); |
| 1583 | break; |
| 1584 | default: |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1585 | /* default: not protected */ |
| 1586 | info->protect[sect_cnt] = 0; |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1589 | sect_cnt++; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | info->sector_count = sect_cnt; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1594 | info->size = 1 << flash_read_uchar (info, FLASH_OFFSET_SIZE); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1595 | /* multiply the size by the number of chips */ |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1596 | info->size *= size_ratio; |
| 1597 | info->buffer_size = 1 << flash_read_ushort (info, 0, |
| 1598 | FLASH_OFFSET_BUFFER_SIZE); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1599 | tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT); |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1600 | info->erase_blk_tout = tmp * |
| 1601 | (1 << flash_read_uchar ( |
| 1602 | info, FLASH_OFFSET_EMAX_TOUT)); |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1603 | tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT)) * |
| 1604 | (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)); |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1605 | /* round up when converting to ms */ |
| 1606 | info->buffer_write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1607 | tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) * |
| 1608 | (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT)); |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1609 | /* round up when converting to ms */ |
| 1610 | info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1611 | info->flash_id = FLASH_MAN_CFI; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1612 | if ((info->interface == FLASH_CFI_X8X16) && |
| 1613 | (info->chipwidth == FLASH_CFI_BY8)) { |
| 1614 | /* XXX - Need to test on x8/x16 in parallel. */ |
| 1615 | info->portwidth >>= 1; |
wdenk | ed2ac4b | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 1616 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Wolfgang Denk | a205a8f | 2005-09-25 16:41:22 +0200 | [diff] [blame] | 1619 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1620 | return (info->size); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1623 | /*----------------------------------------------------------------------- |
| 1624 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1625 | unsigned long flash_init (void) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1626 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1627 | unsigned long size = 0; |
| 1628 | int i; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1629 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1630 | #ifdef CFG_FLASH_PROTECTION |
| 1631 | char *s = getenv("unlock"); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1632 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1633 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1634 | /* Init: no FLASHes known */ |
| 1635 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) { |
| 1636 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1637 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1638 | if (!flash_detect_legacy (bank_base[i], i)) |
| 1639 | flash_get_size (bank_base[i], i); |
| 1640 | size += flash_info[i].size; |
| 1641 | if (flash_info[i].flash_id == FLASH_UNKNOWN) { |
| 1642 | #ifndef CFG_FLASH_QUIET_TEST |
| 1643 | printf ("## Unknown FLASH on Bank %d " |
| 1644 | "- Size = 0x%08lx = %ld MB\n", |
| 1645 | i+1, flash_info[i].size, |
| 1646 | flash_info[i].size << 20); |
| 1647 | #endif /* CFG_FLASH_QUIET_TEST */ |
| 1648 | } |
| 1649 | #ifdef CFG_FLASH_PROTECTION |
| 1650 | else if ((s != NULL) && (strcmp(s, "yes") == 0)) { |
| 1651 | /* |
| 1652 | * Only the U-Boot image and it's environment |
| 1653 | * is protected, all other sectors are |
| 1654 | * unprotected (unlocked) if flash hardware |
| 1655 | * protection is used (CFG_FLASH_PROTECTION) |
| 1656 | * and the environment variable "unlock" is |
| 1657 | * set to "yes". |
| 1658 | */ |
| 1659 | if (flash_info[i].legacy_unlock) { |
| 1660 | int k; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1661 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1662 | /* |
| 1663 | * Disable legacy_unlock temporarily, |
| 1664 | * since flash_real_protect would |
| 1665 | * relock all other sectors again |
| 1666 | * otherwise. |
| 1667 | */ |
| 1668 | flash_info[i].legacy_unlock = 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1669 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1670 | /* |
| 1671 | * Legacy unlocking (e.g. Intel J3) -> |
| 1672 | * unlock only one sector. This will |
| 1673 | * unlock all sectors. |
| 1674 | */ |
| 1675 | flash_real_protect (&flash_info[i], 0, 0); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1676 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1677 | flash_info[i].legacy_unlock = 1; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1678 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1679 | /* |
| 1680 | * Manually mark other sectors as |
| 1681 | * unlocked (unprotected) |
| 1682 | */ |
| 1683 | for (k = 1; k < flash_info[i].sector_count; k++) |
| 1684 | flash_info[i].protect[k] = 0; |
| 1685 | } else { |
| 1686 | /* |
| 1687 | * No legancy unlocking -> unlock all sectors |
| 1688 | */ |
| 1689 | flash_protect (FLAG_PROTECT_CLEAR, |
| 1690 | flash_info[i].start[0], |
| 1691 | flash_info[i].start[0] |
| 1692 | + flash_info[i].size - 1, |
| 1693 | &flash_info[i]); |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1694 | } |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1695 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1696 | #endif /* CFG_FLASH_PROTECTION */ |
| 1697 | } |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1698 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1699 | /* Monitor protection ON by default */ |
| 1700 | #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE) |
| 1701 | flash_protect (FLAG_PROTECT_SET, |
| 1702 | CFG_MONITOR_BASE, |
| 1703 | CFG_MONITOR_BASE + monitor_flash_len - 1, |
| 1704 | flash_get_info(CFG_MONITOR_BASE)); |
| 1705 | #endif |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1706 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1707 | /* Environment protection ON by default */ |
| 1708 | #ifdef CFG_ENV_IS_IN_FLASH |
| 1709 | flash_protect (FLAG_PROTECT_SET, |
| 1710 | CFG_ENV_ADDR, |
| 1711 | CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1, |
| 1712 | flash_get_info(CFG_ENV_ADDR)); |
| 1713 | #endif |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1714 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1715 | /* Redundant environment protection ON by default */ |
| 1716 | #ifdef CFG_ENV_ADDR_REDUND |
| 1717 | flash_protect (FLAG_PROTECT_SET, |
| 1718 | CFG_ENV_ADDR_REDUND, |
| 1719 | CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1, |
| 1720 | flash_get_info(CFG_ENV_ADDR_REDUND)); |
| 1721 | #endif |
| 1722 | return (size); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1723 | } |
Heiko Schocher | 3c58a99 | 2007-01-11 15:44:44 +0100 | [diff] [blame] | 1724 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1725 | #endif /* CFG_FLASH_CFI */ |