wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1 | /* |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2 | * (C) Copyright 2002-2004 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 3 | * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com |
| 4 | * |
| 5 | * Copyright (C) 2003 Arabella Software Ltd. |
| 6 | * Yuli Barcohen <yuli@arabellasw.com> |
| 7 | * Modified to work with AMD flashes |
| 8 | * |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 9 | * Copyright (C) 2004 |
| 10 | * Ed Okerson |
| 11 | * Modified to work with little-endian systems. |
| 12 | * |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 13 | * See file CREDITS for list of people who contributed to this |
| 14 | * project. |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License as |
| 18 | * published by the Free Software Foundation; either version 2 of |
| 19 | * the License, or (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 29 | * MA 02111-1307 USA |
| 30 | * |
| 31 | * History |
| 32 | * 01/20/2004 - combined variants of original driver. |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 33 | * 01/22/2004 - Write performance enhancements for parallel chips (Tolunay) |
| 34 | * 01/23/2004 - Support for x8/x16 chips (Rune Raknerud) |
| 35 | * 01/27/2004 - Little endian support Ed Okerson |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 36 | * |
| 37 | * Tested Architectures |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 38 | * Port Width Chip Width # of banks Flash Chip Board |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame^] | 39 | * 32 16 1 28F128J3 seranoa/eagle |
| 40 | * 64 16 1 28F128J3 seranoa/falcon |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 41 | * |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 42 | */ |
| 43 | |
| 44 | /* The DEBUG define must be before common to enable debugging */ |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame^] | 45 | /* #define DEBUG */ |
| 46 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 47 | #include <common.h> |
| 48 | #include <asm/processor.h> |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 49 | #ifdef CFG_FLASH_CFI_DRIVER |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 50 | /* |
| 51 | * This file implements a Common Flash Interface (CFI) driver for U-Boot. |
| 52 | * The width of the port and the width of the chips are determined at initialization. |
| 53 | * These widths are used to calculate the address for access CFI data structures. |
| 54 | * It has been tested on an Intel Strataflash implementation and AMD 29F016D. |
| 55 | * |
| 56 | * References |
| 57 | * JEDEC Standard JESD68 - Common Flash Interface (CFI) |
| 58 | * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes |
| 59 | * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets |
| 60 | * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet |
| 61 | * |
| 62 | * TODO |
| 63 | * |
| 64 | * Use Primary Extended Query table (PRI) and Alternate Algorithm Query |
| 65 | * Table (ALT) to determine if protection is available |
| 66 | * |
| 67 | * Add support for other command sets Use the PRI and ALT to determine command set |
| 68 | * Verify erase and program timeouts. |
| 69 | */ |
| 70 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 71 | #ifndef CFG_FLASH_BANKS_LIST |
| 72 | #define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE } |
| 73 | #endif |
| 74 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 75 | #define FLASH_CMD_CFI 0x98 |
| 76 | #define FLASH_CMD_READ_ID 0x90 |
| 77 | #define FLASH_CMD_RESET 0xff |
| 78 | #define FLASH_CMD_BLOCK_ERASE 0x20 |
| 79 | #define FLASH_CMD_ERASE_CONFIRM 0xD0 |
| 80 | #define FLASH_CMD_WRITE 0x40 |
| 81 | #define FLASH_CMD_PROTECT 0x60 |
| 82 | #define FLASH_CMD_PROTECT_SET 0x01 |
| 83 | #define FLASH_CMD_PROTECT_CLEAR 0xD0 |
| 84 | #define FLASH_CMD_CLEAR_STATUS 0x50 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 85 | #define FLASH_CMD_WRITE_TO_BUFFER 0xE8 |
| 86 | #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 87 | |
| 88 | #define FLASH_STATUS_DONE 0x80 |
| 89 | #define FLASH_STATUS_ESS 0x40 |
| 90 | #define FLASH_STATUS_ECLBS 0x20 |
| 91 | #define FLASH_STATUS_PSLBS 0x10 |
| 92 | #define FLASH_STATUS_VPENS 0x08 |
| 93 | #define FLASH_STATUS_PSS 0x04 |
| 94 | #define FLASH_STATUS_DPS 0x02 |
| 95 | #define FLASH_STATUS_R 0x01 |
| 96 | #define FLASH_STATUS_PROTECT 0x01 |
| 97 | |
| 98 | #define AMD_CMD_RESET 0xF0 |
| 99 | #define AMD_CMD_WRITE 0xA0 |
| 100 | #define AMD_CMD_ERASE_START 0x80 |
| 101 | #define AMD_CMD_ERASE_SECTOR 0x30 |
| 102 | |
| 103 | #define AMD_STATUS_TOGGLE 0x40 |
| 104 | #define AMD_STATUS_ERROR 0x20 |
| 105 | |
| 106 | #define FLASH_OFFSET_CFI 0x55 |
| 107 | #define FLASH_OFFSET_CFI_RESP 0x10 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 108 | #define FLASH_OFFSET_PRIMARY_VENDOR 0x13 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 109 | #define FLASH_OFFSET_WTOUT 0x1F |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 110 | #define FLASH_OFFSET_WBTOUT 0x20 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 111 | #define FLASH_OFFSET_ETOUT 0x21 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 112 | #define FLASH_OFFSET_CETOUT 0x22 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 113 | #define FLASH_OFFSET_WMAX_TOUT 0x23 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 114 | #define FLASH_OFFSET_WBMAX_TOUT 0x24 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 115 | #define FLASH_OFFSET_EMAX_TOUT 0x25 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 116 | #define FLASH_OFFSET_CEMAX_TOUT 0x26 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 117 | #define FLASH_OFFSET_SIZE 0x27 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 118 | #define FLASH_OFFSET_INTERFACE 0x28 |
| 119 | #define FLASH_OFFSET_BUFFER_SIZE 0x2A |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 120 | #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C |
| 121 | #define FLASH_OFFSET_ERASE_REGIONS 0x2D |
| 122 | #define FLASH_OFFSET_PROTECT 0x02 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 123 | #define FLASH_OFFSET_USER_PROTECTION 0x85 |
| 124 | #define FLASH_OFFSET_INTEL_PROTECTION 0x81 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | #define FLASH_MAN_CFI 0x01000000 |
| 128 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 129 | #define CFI_CMDSET_NONE 0 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 130 | #define CFI_CMDSET_INTEL_EXTENDED 1 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 131 | #define CFI_CMDSET_AMD_STANDARD 2 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 132 | #define CFI_CMDSET_INTEL_STANDARD 3 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 133 | #define CFI_CMDSET_AMD_EXTENDED 4 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 134 | #define CFI_CMDSET_MITSU_STANDARD 256 |
| 135 | #define CFI_CMDSET_MITSU_EXTENDED 257 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 136 | #define CFI_CMDSET_SST 258 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | typedef union { |
| 140 | unsigned char c; |
| 141 | unsigned short w; |
| 142 | unsigned long l; |
| 143 | unsigned long long ll; |
| 144 | } cfiword_t; |
| 145 | |
| 146 | typedef union { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 147 | volatile unsigned char *cp; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 148 | volatile unsigned short *wp; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 149 | volatile unsigned long *lp; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 150 | volatile unsigned long long *llp; |
| 151 | } cfiptr_t; |
| 152 | |
| 153 | #define NUM_ERASE_REGIONS 4 |
| 154 | |
| 155 | static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST; |
| 156 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 157 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 158 | |
| 159 | /*----------------------------------------------------------------------- |
| 160 | * Functions |
| 161 | */ |
| 162 | |
| 163 | typedef unsigned long flash_sect_t; |
| 164 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 165 | static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c); |
| 166 | static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf); |
| 167 | static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, |
| 168 | uint offset, uchar cmd); |
| 169 | static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect); |
| 170 | static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, |
| 171 | uchar cmd); |
| 172 | static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, |
| 173 | uchar cmd); |
| 174 | static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, |
| 175 | uchar cmd); |
| 176 | static int flash_detect_cfi (flash_info_t * info); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 177 | static ulong flash_get_size (ulong base, int banknum); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 178 | static int flash_write_cfiword (flash_info_t * info, ulong dest, |
| 179 | cfiword_t cword); |
| 180 | static int flash_full_status_check (flash_info_t * info, flash_sect_t sector, |
| 181 | ulong tout, char *prompt); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 182 | #ifdef CFG_FLASH_USE_BUFFER_WRITE |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 183 | static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, |
| 184 | int len); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 185 | #endif |
| 186 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 187 | /*----------------------------------------------------------------------- |
| 188 | * create an address based on the offset and the port width |
| 189 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 190 | inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, |
| 191 | uint offset) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 192 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 193 | return ((uchar *) (info->start[sect] + (offset * info->portwidth))); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 194 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 195 | |
| 196 | #ifdef DEBUG |
| 197 | /*----------------------------------------------------------------------- |
| 198 | * Debug support |
| 199 | */ |
| 200 | void print_longlong (char *str, unsigned long long data) |
| 201 | { |
| 202 | int i; |
| 203 | char *cp; |
| 204 | |
| 205 | cp = (unsigned char *) &data; |
| 206 | for (i = 0; i < 8; i++) |
| 207 | sprintf (&str[i * 2], "%2.2x", *cp++); |
| 208 | } |
| 209 | static void flash_printqry (flash_info_t * info, flash_sect_t sect) |
| 210 | { |
| 211 | cfiptr_t cptr; |
| 212 | int x, y; |
| 213 | |
| 214 | for (x = 0; x < 0x40; x += 16 / info->portwidth) { |
| 215 | cptr.cp = |
| 216 | flash_make_addr (info, sect, |
| 217 | x + FLASH_OFFSET_CFI_RESP); |
| 218 | debug ("%p : ", cptr.cp); |
| 219 | for (y = 0; y < 16; y++) { |
| 220 | debug ("%2.2x ", cptr.cp[y]); |
| 221 | } |
| 222 | debug (" "); |
| 223 | for (y = 0; y < 16; y++) { |
| 224 | if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) { |
| 225 | debug ("%c", cptr.cp[y]); |
| 226 | } else { |
| 227 | debug ("."); |
| 228 | } |
| 229 | } |
| 230 | debug ("\n"); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | #endif |
| 235 | |
| 236 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 237 | /*----------------------------------------------------------------------- |
| 238 | * read a character at a port width address |
| 239 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 240 | inline uchar flash_read_uchar (flash_info_t * info, uint offset) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 241 | { |
| 242 | uchar *cp; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 243 | |
| 244 | cp = flash_make_addr (info, 0, offset); |
| 245 | #if defined(__LITTLE_ENDIAN) |
| 246 | return (cp[0]); |
| 247 | #else |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 248 | return (cp[info->portwidth - 1]); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 249 | #endif |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /*----------------------------------------------------------------------- |
| 253 | * read a short word by swapping for ppc format. |
| 254 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 255 | ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 256 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 257 | uchar *addr; |
| 258 | ushort retval; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 259 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 260 | #ifdef DEBUG |
| 261 | int x; |
| 262 | #endif |
| 263 | addr = flash_make_addr (info, sect, offset); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 264 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 265 | #ifdef DEBUG |
| 266 | debug ("ushort addr is at %p info->portwidth = %d\n", addr, |
| 267 | info->portwidth); |
| 268 | for (x = 0; x < 2 * info->portwidth; x++) { |
| 269 | debug ("addr[%x] = 0x%x\n", x, addr[x]); |
| 270 | } |
| 271 | #endif |
| 272 | #if defined(__LITTLE_ENDIAN) |
| 273 | retval = ((addr[(info->portwidth)] << 8) | addr[0]); |
| 274 | #else |
| 275 | retval = ((addr[(2 * info->portwidth) - 1] << 8) | |
| 276 | addr[info->portwidth - 1]); |
| 277 | #endif |
| 278 | |
| 279 | debug ("retval = 0x%x\n", retval); |
| 280 | return retval; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /*----------------------------------------------------------------------- |
| 284 | * read a long word by picking the least significant byte of each maiximum |
| 285 | * port size word. Swap for ppc format. |
| 286 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 287 | ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 288 | { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 289 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 290 | uchar *addr; |
| 291 | ulong retval; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 292 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 293 | #ifdef DEBUG |
| 294 | int x; |
| 295 | #endif |
| 296 | addr = flash_make_addr (info, sect, offset); |
| 297 | |
| 298 | #ifdef DEBUG |
| 299 | debug ("long addr is at %p info->portwidth = %d\n", addr, |
| 300 | info->portwidth); |
| 301 | for (x = 0; x < 4 * info->portwidth; x++) { |
| 302 | debug ("addr[%x] = 0x%x\n", x, addr[x]); |
| 303 | } |
| 304 | #endif |
| 305 | #if defined(__LITTLE_ENDIAN) |
| 306 | retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) | |
| 307 | (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] |
| 308 | << 8); |
| 309 | #else |
| 310 | retval = (addr[(2 * info->portwidth) - 1] << 24) | |
| 311 | (addr[(info->portwidth) - 1] << 16) | |
| 312 | (addr[(4 * info->portwidth) - 1] << 8) | |
| 313 | addr[(3 * info->portwidth) - 1]; |
| 314 | #endif |
| 315 | return retval; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /*----------------------------------------------------------------------- |
| 319 | */ |
| 320 | unsigned long flash_init (void) |
| 321 | { |
| 322 | unsigned long size = 0; |
| 323 | int i; |
| 324 | |
| 325 | /* Init: no FLASHes known */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 326 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 327 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 328 | size += flash_info[i].size = flash_get_size (bank_base[i], i); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 329 | if (flash_info[i].flash_id == FLASH_UNKNOWN) { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 330 | printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n", i, flash_info[i].size, flash_info[i].size << 20); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | /* Monitor protection ON by default */ |
| 335 | #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE) |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 336 | flash_protect (FLAG_PROTECT_SET, |
| 337 | CFG_MONITOR_BASE, |
| 338 | CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1, |
| 339 | &flash_info[0]); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 340 | #endif |
| 341 | |
| 342 | return (size); |
| 343 | } |
| 344 | |
| 345 | /*----------------------------------------------------------------------- |
| 346 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 347 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 348 | { |
| 349 | int rcode = 0; |
| 350 | int prot; |
| 351 | flash_sect_t sect; |
| 352 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 353 | if (info->flash_id != FLASH_MAN_CFI) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 354 | printf ("Can't erase unknown flash type - aborted\n"); |
| 355 | return 1; |
| 356 | } |
| 357 | if ((s_first < 0) || (s_first > s_last)) { |
| 358 | printf ("- no sectors to erase\n"); |
| 359 | return 1; |
| 360 | } |
| 361 | |
| 362 | prot = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 363 | for (sect = s_first; sect <= s_last; ++sect) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 364 | if (info->protect[sect]) { |
| 365 | prot++; |
| 366 | } |
| 367 | } |
| 368 | if (prot) { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 369 | printf ("- Warning: %d protected sectors will not be erased!\n", prot); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 370 | } else { |
| 371 | printf ("\n"); |
| 372 | } |
| 373 | |
| 374 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 375 | for (sect = s_first; sect <= s_last; sect++) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 376 | if (info->protect[sect] == 0) { /* not protected */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 377 | switch (info->vendor) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 378 | case CFI_CMDSET_INTEL_STANDARD: |
| 379 | case CFI_CMDSET_INTEL_EXTENDED: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 380 | flash_write_cmd (info, sect, 0, |
| 381 | FLASH_CMD_CLEAR_STATUS); |
| 382 | flash_write_cmd (info, sect, 0, |
| 383 | FLASH_CMD_BLOCK_ERASE); |
| 384 | flash_write_cmd (info, sect, 0, |
| 385 | FLASH_CMD_ERASE_CONFIRM); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 386 | break; |
| 387 | case CFI_CMDSET_AMD_STANDARD: |
| 388 | case CFI_CMDSET_AMD_EXTENDED: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 389 | flash_unlock_seq (info, sect); |
| 390 | flash_write_cmd (info, sect, 0x555, |
| 391 | AMD_CMD_ERASE_START); |
| 392 | flash_unlock_seq (info, sect); |
| 393 | flash_write_cmd (info, sect, 0, |
| 394 | AMD_CMD_ERASE_SECTOR); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 395 | break; |
| 396 | default: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 397 | debug ("Unkown flash vendor %d\n", |
| 398 | info->vendor); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 399 | break; |
| 400 | } |
| 401 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 402 | if (flash_full_status_check |
| 403 | (info, sect, info->erase_blk_tout, "erase")) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 404 | rcode = 1; |
| 405 | } else |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 406 | printf ("."); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | printf (" done\n"); |
| 410 | return rcode; |
| 411 | } |
| 412 | |
| 413 | /*----------------------------------------------------------------------- |
| 414 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 415 | void flash_print_info (flash_info_t * info) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 416 | { |
| 417 | int i; |
| 418 | |
| 419 | if (info->flash_id != FLASH_MAN_CFI) { |
| 420 | printf ("missing or unknown FLASH type\n"); |
| 421 | return; |
| 422 | } |
| 423 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 424 | printf ("CFI conformant FLASH (%d x %d)", |
| 425 | (info->portwidth << 3), (info->chipwidth << 3)); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 426 | printf (" Size: %ld MB in %d Sectors\n", |
| 427 | info->size >> 20, info->sector_count); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 428 | printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n", info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 429 | |
| 430 | printf (" Sector Start Addresses:"); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 431 | for (i = 0; i < info->sector_count; ++i) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 432 | #ifdef CFG_FLASH_EMPTY_INFO |
| 433 | int k; |
| 434 | int size; |
| 435 | int erased; |
| 436 | volatile unsigned long *flash; |
| 437 | |
| 438 | /* |
| 439 | * Check if whole sector is erased |
| 440 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 441 | if (i != (info->sector_count - 1)) |
| 442 | size = info->start[i + 1] - info->start[i]; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 443 | else |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 444 | size = info->start[0] + info->size - info->start[i]; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 445 | erased = 1; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 446 | flash = (volatile unsigned long *) info->start[i]; |
| 447 | size = size >> 2; /* divide by 4 for longword access */ |
| 448 | for (k = 0; k < size; k++) { |
| 449 | if (*flash++ != 0xffffffff) { |
| 450 | erased = 0; |
| 451 | break; |
| 452 | } |
| 453 | } |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 454 | |
| 455 | if ((i % 5) == 0) |
| 456 | printf ("\n"); |
| 457 | /* print empty and read-only info */ |
| 458 | printf (" %08lX%s%s", |
| 459 | info->start[i], |
| 460 | erased ? " E" : " ", |
| 461 | info->protect[i] ? "RO " : " "); |
| 462 | #else |
| 463 | if ((i % 5) == 0) |
| 464 | printf ("\n "); |
| 465 | printf (" %08lX%s", |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 466 | info->start[i], info->protect[i] ? " (RO)" : " "); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 467 | #endif |
| 468 | } |
| 469 | printf ("\n"); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | /*----------------------------------------------------------------------- |
| 474 | * Copy memory to flash, returns: |
| 475 | * 0 - OK |
| 476 | * 1 - write timeout |
| 477 | * 2 - Flash not erased |
| 478 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 479 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 480 | { |
| 481 | ulong wp; |
| 482 | ulong cp; |
| 483 | int aln; |
| 484 | cfiword_t cword; |
| 485 | int i, rc; |
| 486 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 487 | #ifdef CFG_FLASH_USE_BUFFER_WRITE |
| 488 | int buffered_size; |
| 489 | #endif |
| 490 | int x8mode = 0; |
| 491 | |
| 492 | /* special handling of 16 bit devices in 8 bit mode */ |
| 493 | if ((info->interface == FLASH_CFI_X8X16) |
| 494 | && (info->chipwidth == FLASH_CFI_BY8)) { |
| 495 | switch (info->vendor) { |
| 496 | case CFI_CMDSET_INTEL_STANDARD: |
| 497 | case CFI_CMDSET_INTEL_EXTENDED: |
| 498 | x8mode = info->portwidth; |
| 499 | info->portwidth >>= 1; /* XXX - Need to test on x9/x16 in parallel. */ |
| 500 | /*info->portwidth = FLASH_CFI_8BIT; */ /* XXX - Need to test on x9/x16 in parallel. */ |
| 501 | break; |
| 502 | case CFI_CMDSET_AMD_STANDARD: |
| 503 | case CFI_CMDSET_AMD_EXTENDED: |
| 504 | default: |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | /* get lower aligned address */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 509 | /* get lower aligned address */ |
| 510 | wp = (addr & ~(info->portwidth - 1)); |
| 511 | |
| 512 | /* handle unaligned start */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 513 | if ((aln = addr - wp) != 0) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 514 | cword.l = 0; |
| 515 | cp = wp; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 516 | for (i = 0; i < aln; ++i, ++cp) |
| 517 | flash_add_byte (info, &cword, (*(uchar *) cp)); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 518 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 519 | for (; (i < info->portwidth) && (cnt > 0); i++) { |
| 520 | flash_add_byte (info, &cword, *src++); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 521 | cnt--; |
| 522 | cp++; |
| 523 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 524 | for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp) |
| 525 | flash_add_byte (info, &cword, (*(uchar *) cp)); |
| 526 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 527 | return rc; |
| 528 | wp = cp; |
| 529 | } |
| 530 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 531 | /* handle the aligned part */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 532 | #ifdef CFG_FLASH_USE_BUFFER_WRITE |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 533 | buffered_size = (info->portwidth / info->chipwidth); |
| 534 | buffered_size *= info->buffer_size; |
| 535 | while (cnt >= info->portwidth) { |
| 536 | i = buffered_size > cnt ? cnt : buffered_size; |
| 537 | if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 538 | return rc; |
| 539 | wp += i; |
| 540 | src += i; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 541 | cnt -= i; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 542 | } |
| 543 | #else |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 544 | while (cnt >= info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 545 | cword.l = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 546 | for (i = 0; i < info->portwidth; i++) { |
| 547 | flash_add_byte (info, &cword, *src++); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 548 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 549 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 550 | return rc; |
| 551 | wp += info->portwidth; |
| 552 | cnt -= info->portwidth; |
| 553 | } |
| 554 | #endif /* CFG_FLASH_USE_BUFFER_WRITE */ |
| 555 | if (cnt == 0) { |
| 556 | return (0); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * handle unaligned tail bytes |
| 561 | */ |
| 562 | cword.l = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 563 | for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) { |
| 564 | flash_add_byte (info, &cword, *src++); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 565 | --cnt; |
| 566 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 567 | for (; i < info->portwidth; ++i, ++cp) { |
| 568 | flash_add_byte (info, &cword, (*(uchar *) cp)); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 569 | } |
| 570 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 571 | /* special handling of 16 bit devices in 8 bit mode */ |
| 572 | if (x8mode) { |
| 573 | info->portwidth = x8mode;; |
| 574 | } |
| 575 | return flash_write_cfiword (info, wp, cword); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /*----------------------------------------------------------------------- |
| 579 | */ |
| 580 | #ifdef CFG_FLASH_PROTECTION |
| 581 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 582 | int flash_real_protect (flash_info_t * info, long sector, int prot) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 583 | { |
| 584 | int retcode = 0; |
| 585 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 586 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 587 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT); |
| 588 | if (prot) |
| 589 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 590 | else |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 591 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 592 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 593 | if ((retcode = |
| 594 | flash_full_status_check (info, sector, info->erase_blk_tout, |
| 595 | prot ? "protect" : "unprotect")) == 0) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 596 | |
| 597 | info->protect[sector] = prot; |
| 598 | /* Intel's unprotect unprotects all locking */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 599 | if (prot == 0) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 600 | flash_sect_t i; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 601 | |
| 602 | for (i = 0; i < info->sector_count; i++) { |
| 603 | if (info->protect[i]) |
| 604 | flash_real_protect (info, i, 1); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return retcode; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 610 | } |
| 611 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 612 | /*----------------------------------------------------------------------- |
| 613 | * flash_read_user_serial - read the OneTimeProgramming cells |
| 614 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 615 | void flash_read_user_serial (flash_info_t * info, void *buffer, int offset, |
| 616 | int len) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 617 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 618 | uchar *src; |
| 619 | uchar *dst; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 620 | |
| 621 | dst = buffer; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 622 | src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION); |
| 623 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 624 | memcpy (dst, src + offset, len); |
| 625 | flash_write_cmd (info, 0, 0, FLASH_CMD_RESET); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 626 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 627 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 628 | /* |
| 629 | * flash_read_factory_serial - read the device Id from the protection area |
| 630 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 631 | void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset, |
| 632 | int len) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 633 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 634 | uchar *src; |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 635 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 636 | src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION); |
| 637 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 638 | memcpy (buffer, src + offset, len); |
| 639 | flash_write_cmd (info, 0, 0, FLASH_CMD_RESET); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | #endif /* CFG_FLASH_PROTECTION */ |
| 643 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 644 | /* |
| 645 | * flash_is_busy - check to see if the flash is busy |
| 646 | * This routine checks the status of the chip and returns true if the chip is busy |
| 647 | */ |
| 648 | static int flash_is_busy (flash_info_t * info, flash_sect_t sect) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 649 | { |
| 650 | int retval; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 651 | |
| 652 | switch (info->vendor) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 653 | case CFI_CMDSET_INTEL_STANDARD: |
| 654 | case CFI_CMDSET_INTEL_EXTENDED: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 655 | retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 656 | break; |
| 657 | case CFI_CMDSET_AMD_STANDARD: |
| 658 | case CFI_CMDSET_AMD_EXTENDED: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 659 | retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 660 | break; |
| 661 | default: |
| 662 | retval = 0; |
| 663 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 664 | debug ("flash_is_busy: %d\n", retval); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 665 | return retval; |
| 666 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 667 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 668 | /*----------------------------------------------------------------------- |
| 669 | * wait for XSR.7 to be set. Time out with an error if it does not. |
| 670 | * This routine does not set the flash to read-array mode. |
| 671 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 672 | static int flash_status_check (flash_info_t * info, flash_sect_t sector, |
| 673 | ulong tout, char *prompt) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 674 | { |
| 675 | ulong start; |
| 676 | |
| 677 | /* Wait for command completion */ |
| 678 | start = get_timer (0); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 679 | while (flash_is_busy (info, sector)) { |
| 680 | if (get_timer (start) > info->erase_blk_tout * CFG_HZ) { |
| 681 | printf ("Flash %s timeout at address %lx data %lx\n", |
| 682 | prompt, info->start[sector], |
| 683 | flash_read_long (info, sector, 0)); |
| 684 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 685 | return ERR_TIMOUT; |
| 686 | } |
| 687 | } |
| 688 | return ERR_OK; |
| 689 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 690 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 691 | /*----------------------------------------------------------------------- |
| 692 | * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check. |
| 693 | * This routine sets the flash to read-array mode. |
| 694 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 695 | static int flash_full_status_check (flash_info_t * info, flash_sect_t sector, |
| 696 | ulong tout, char *prompt) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 697 | { |
| 698 | int retcode; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 699 | |
| 700 | retcode = flash_status_check (info, sector, tout, prompt); |
| 701 | switch (info->vendor) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 702 | case CFI_CMDSET_INTEL_EXTENDED: |
| 703 | case CFI_CMDSET_INTEL_STANDARD: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 704 | if ((retcode != ERR_OK) |
| 705 | && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 706 | retcode = ERR_INVAL; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 707 | printf ("Flash %s error at address %lx\n", prompt, |
| 708 | info->start[sector]); |
| 709 | if (flash_isset |
| 710 | (info, sector, 0, |
| 711 | FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) { |
| 712 | printf ("Command Sequence Error.\n"); |
| 713 | } else if (flash_isset |
| 714 | (info, sector, 0, FLASH_STATUS_ECLBS)) { |
| 715 | printf ("Block Erase Error.\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 716 | retcode = ERR_NOT_ERASED; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 717 | } else if (flash_isset |
| 718 | (info, sector, 0, FLASH_STATUS_PSLBS)) { |
| 719 | printf ("Locking Error\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 720 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 721 | if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) { |
| 722 | printf ("Block locked.\n"); |
| 723 | retcode = ERR_PROTECTED; |
| 724 | } |
| 725 | if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS)) |
| 726 | printf ("Vpp Low Error.\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 727 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 728 | flash_write_cmd (info, sector, 0, FLASH_CMD_RESET); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 729 | break; |
| 730 | default: |
| 731 | break; |
| 732 | } |
| 733 | return retcode; |
| 734 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 735 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 736 | /*----------------------------------------------------------------------- |
| 737 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 738 | static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 739 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 740 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 741 | case FLASH_CFI_8BIT: |
| 742 | cword->c = c; |
| 743 | break; |
| 744 | case FLASH_CFI_16BIT: |
| 745 | cword->w = (cword->w << 8) | c; |
| 746 | break; |
| 747 | case FLASH_CFI_32BIT: |
| 748 | cword->l = (cword->l << 8) | c; |
| 749 | break; |
| 750 | case FLASH_CFI_64BIT: |
| 751 | cword->ll = (cword->ll << 8) | c; |
| 752 | break; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | |
| 757 | /*----------------------------------------------------------------------- |
| 758 | * make a proper sized command based on the port and chip widths |
| 759 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 760 | static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 761 | { |
| 762 | int i; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 763 | |
| 764 | #if defined(__LITTLE_ENDIAN) |
| 765 | ushort stmp; |
| 766 | #endif |
| 767 | uchar *cp = (uchar *) cmdbuf; |
| 768 | |
| 769 | for (i = 0; i < info->portwidth; i++) |
| 770 | *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd; |
| 771 | #if defined(__LITTLE_ENDIAN) |
| 772 | if (info->portwidth == 2) { |
| 773 | stmp = *(ushort *) cmdbuf; |
| 774 | *(ushort *) cmdbuf = swab16 (stmp); |
| 775 | } |
| 776 | #endif |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Write a proper sized command to the correct address |
| 781 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 782 | static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, |
| 783 | uint offset, uchar cmd) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 784 | { |
| 785 | |
| 786 | volatile cfiptr_t addr; |
| 787 | cfiword_t cword; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 788 | |
| 789 | addr.cp = flash_make_addr (info, sect, offset); |
| 790 | flash_make_cmd (info, cmd, &cword); |
| 791 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 792 | case FLASH_CFI_8BIT: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 793 | debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd, |
| 794 | cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 795 | *addr.cp = cword.c; |
| 796 | break; |
| 797 | case FLASH_CFI_16BIT: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 798 | debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp, |
| 799 | cmd, cword.w, |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 800 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 801 | *addr.wp = cword.w; |
| 802 | break; |
| 803 | case FLASH_CFI_32BIT: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 804 | debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp, |
| 805 | cmd, cword.l, |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 806 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 807 | *addr.lp = cword.l; |
| 808 | break; |
| 809 | case FLASH_CFI_64BIT: |
| 810 | #ifdef DEBUG |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 811 | { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 812 | char str[20]; |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 813 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 814 | print_longlong (str, cword.ll); |
| 815 | |
| 816 | debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n", |
| 817 | addr.llp, cmd, str, |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 818 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 819 | } |
| 820 | #endif |
| 821 | *addr.llp = cword.ll; |
| 822 | break; |
| 823 | } |
| 824 | } |
| 825 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 826 | static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 827 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 828 | flash_write_cmd (info, sect, 0x555, 0xAA); |
| 829 | flash_write_cmd (info, sect, 0x2AA, 0x55); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 830 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 831 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 832 | /*----------------------------------------------------------------------- |
| 833 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 834 | static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, |
| 835 | uchar cmd) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 836 | { |
| 837 | cfiptr_t cptr; |
| 838 | cfiword_t cword; |
| 839 | int retval; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 840 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 841 | cptr.cp = flash_make_addr (info, sect, offset); |
| 842 | flash_make_cmd (info, cmd, &cword); |
| 843 | |
| 844 | debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp); |
| 845 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 846 | case FLASH_CFI_8BIT: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 847 | debug ("is= %x %x\n", cptr.cp[0], cword.c); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 848 | retval = (cptr.cp[0] == cword.c); |
| 849 | break; |
| 850 | case FLASH_CFI_16BIT: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 851 | debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 852 | retval = (cptr.wp[0] == cword.w); |
| 853 | break; |
| 854 | case FLASH_CFI_32BIT: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 855 | debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 856 | retval = (cptr.lp[0] == cword.l); |
| 857 | break; |
| 858 | case FLASH_CFI_64BIT: |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 859 | #ifdef DEBUG |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 860 | { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 861 | char str1[20]; |
| 862 | char str2[20]; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 863 | |
| 864 | print_longlong (str1, cptr.llp[0]); |
| 865 | print_longlong (str2, cword.ll); |
| 866 | debug ("is= %s %s\n", str1, str2); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 867 | } |
| 868 | #endif |
| 869 | retval = (cptr.llp[0] == cword.ll); |
| 870 | break; |
| 871 | default: |
| 872 | retval = 0; |
| 873 | break; |
| 874 | } |
| 875 | return retval; |
| 876 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 877 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 878 | /*----------------------------------------------------------------------- |
| 879 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 880 | static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, |
| 881 | uchar cmd) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 882 | { |
| 883 | cfiptr_t cptr; |
| 884 | cfiword_t cword; |
| 885 | int retval; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 886 | |
| 887 | cptr.cp = flash_make_addr (info, sect, offset); |
| 888 | flash_make_cmd (info, cmd, &cword); |
| 889 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 890 | case FLASH_CFI_8BIT: |
| 891 | retval = ((cptr.cp[0] & cword.c) == cword.c); |
| 892 | break; |
| 893 | case FLASH_CFI_16BIT: |
| 894 | retval = ((cptr.wp[0] & cword.w) == cword.w); |
| 895 | break; |
| 896 | case FLASH_CFI_32BIT: |
| 897 | retval = ((cptr.lp[0] & cword.l) == cword.l); |
| 898 | break; |
| 899 | case FLASH_CFI_64BIT: |
| 900 | retval = ((cptr.llp[0] & cword.ll) == cword.ll); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 901 | break; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 902 | default: |
| 903 | retval = 0; |
| 904 | break; |
| 905 | } |
| 906 | return retval; |
| 907 | } |
| 908 | |
| 909 | /*----------------------------------------------------------------------- |
| 910 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 911 | static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, |
| 912 | uchar cmd) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 913 | { |
| 914 | cfiptr_t cptr; |
| 915 | cfiword_t cword; |
| 916 | int retval; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 917 | |
| 918 | cptr.cp = flash_make_addr (info, sect, offset); |
| 919 | flash_make_cmd (info, cmd, &cword); |
| 920 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 921 | case FLASH_CFI_8BIT: |
| 922 | retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c)); |
| 923 | break; |
| 924 | case FLASH_CFI_16BIT: |
| 925 | retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w)); |
| 926 | break; |
| 927 | case FLASH_CFI_32BIT: |
| 928 | retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l)); |
| 929 | break; |
| 930 | case FLASH_CFI_64BIT: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 931 | retval = ((cptr.llp[0] & cword.ll) != |
| 932 | (cptr.llp[0] & cword.ll)); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 933 | break; |
| 934 | default: |
| 935 | retval = 0; |
| 936 | break; |
| 937 | } |
| 938 | return retval; |
| 939 | } |
| 940 | |
| 941 | /*----------------------------------------------------------------------- |
| 942 | * detect if flash is compatible with the Common Flash Interface (CFI) |
| 943 | * http://www.jedec.org/download/search/jesd68.pdf |
| 944 | * |
| 945 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 946 | static int flash_detect_cfi (flash_info_t * info) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 947 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 948 | debug ("flash detect cfi\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 949 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 950 | for (info->portwidth = FLASH_CFI_8BIT; |
| 951 | info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) { |
| 952 | for (info->chipwidth = FLASH_CFI_BY8; |
| 953 | info->chipwidth <= info->portwidth; |
| 954 | info->chipwidth <<= 1) { |
| 955 | flash_write_cmd (info, 0, 0, FLASH_CMD_RESET); |
| 956 | flash_write_cmd (info, 0, FLASH_OFFSET_CFI, |
| 957 | FLASH_CMD_CFI); |
| 958 | if (flash_isequal |
| 959 | (info, 0, FLASH_OFFSET_CFI_RESP, 'Q') |
| 960 | && flash_isequal (info, 0, |
| 961 | FLASH_OFFSET_CFI_RESP + 1, 'R') |
| 962 | && flash_isequal (info, 0, |
| 963 | FLASH_OFFSET_CFI_RESP + 2, |
| 964 | 'Y')) { |
| 965 | info->interface = |
| 966 | flash_read_ushort (info, 0, |
| 967 | FLASH_OFFSET_INTERFACE); |
| 968 | debug ("device interface is %d\n", |
| 969 | info->interface); |
| 970 | debug ("found port %d chip %d ", |
| 971 | info->portwidth, info->chipwidth); |
| 972 | debug ("port %d bits chip %d bits\n", |
| 973 | info-> |
| 974 | portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 975 | info-> |
| 976 | chipwidth << CFI_FLASH_SHIFT_WIDTH); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 977 | return 1; |
| 978 | } |
| 979 | } |
| 980 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 981 | debug ("not found\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 982 | return 0; |
| 983 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 984 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 985 | /* |
| 986 | * The following code cannot be run from FLASH! |
| 987 | * |
| 988 | */ |
| 989 | static ulong flash_get_size (ulong base, int banknum) |
| 990 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 991 | flash_info_t *info = &flash_info[banknum]; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 992 | int i, j; |
| 993 | flash_sect_t sect_cnt; |
| 994 | unsigned long sector; |
| 995 | unsigned long tmp; |
| 996 | int size_ratio; |
| 997 | uchar num_erase_regions; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 998 | int erase_region_size; |
| 999 | int erase_region_count; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1000 | |
| 1001 | info->start[0] = base; |
| 1002 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1003 | if (flash_detect_cfi (info)) { |
| 1004 | info->vendor = |
| 1005 | flash_read_ushort (info, 0, |
| 1006 | FLASH_OFFSET_PRIMARY_VENDOR); |
| 1007 | #ifdef DEBUG |
| 1008 | flash_printqry (info, 0); |
| 1009 | #endif |
| 1010 | switch (info->vendor) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1011 | case CFI_CMDSET_INTEL_STANDARD: |
| 1012 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1013 | default: |
| 1014 | info->cmd_reset = FLASH_CMD_RESET; |
| 1015 | break; |
| 1016 | case CFI_CMDSET_AMD_STANDARD: |
| 1017 | case CFI_CMDSET_AMD_EXTENDED: |
| 1018 | info->cmd_reset = AMD_CMD_RESET; |
| 1019 | break; |
| 1020 | } |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 1021 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1022 | debug ("manufacturer is %d\n", info->vendor); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1023 | size_ratio = info->portwidth / info->chipwidth; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1024 | /* if the chip is x8/x16 reduce the ratio by half */ |
| 1025 | if ((info->interface == FLASH_CFI_X8X16) |
| 1026 | && (info->chipwidth == FLASH_CFI_BY8)) { |
| 1027 | size_ratio >>= 1; |
| 1028 | } |
| 1029 | num_erase_regions = |
| 1030 | flash_read_uchar (info, |
| 1031 | FLASH_OFFSET_NUM_ERASE_REGIONS); |
| 1032 | debug ("size_ratio %d port %d bits chip %d bits\n", |
| 1033 | size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 1034 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 1035 | debug ("found %d erase regions\n", num_erase_regions); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1036 | sect_cnt = 0; |
| 1037 | sector = base; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1038 | for (i = 0; i < num_erase_regions; i++) { |
| 1039 | if (i > NUM_ERASE_REGIONS) { |
| 1040 | printf ("%d erase regions found, only %d used\n", num_erase_regions, NUM_ERASE_REGIONS); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1041 | break; |
| 1042 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1043 | tmp = flash_read_long (info, 0, |
| 1044 | FLASH_OFFSET_ERASE_REGIONS + |
| 1045 | i * 4); |
| 1046 | erase_region_size = |
| 1047 | (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1048 | tmp >>= 16; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1049 | erase_region_count = (tmp & 0xffff) + 1; |
| 1050 | printf ("erase_region_count = %d erase_region_size = %d\n", erase_region_count, erase_region_size); |
| 1051 | for (j = 0; j < erase_region_count; j++) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1052 | info->start[sect_cnt] = sector; |
| 1053 | sector += (erase_region_size * size_ratio); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1054 | info->protect[sect_cnt] = |
| 1055 | flash_isset (info, sect_cnt, |
| 1056 | FLASH_OFFSET_PROTECT, |
| 1057 | FLASH_STATUS_PROTECT); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1058 | sect_cnt++; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | info->sector_count = sect_cnt; |
| 1063 | /* multiply the size by the number of chips */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1064 | info->size = |
| 1065 | (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * |
| 1066 | size_ratio; |
| 1067 | info->buffer_size = |
| 1068 | (1 << |
| 1069 | flash_read_ushort (info, 0, |
| 1070 | FLASH_OFFSET_BUFFER_SIZE)); |
| 1071 | tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT); |
| 1072 | info->erase_blk_tout = |
| 1073 | (tmp * |
| 1074 | (1 << |
| 1075 | flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT))); |
| 1076 | tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT); |
| 1077 | info->buffer_write_tout = |
| 1078 | (tmp * |
| 1079 | (1 << |
| 1080 | flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT))); |
| 1081 | tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT); |
| 1082 | info->write_tout = |
| 1083 | (tmp * |
| 1084 | (1 << |
| 1085 | flash_read_uchar (info, |
| 1086 | FLASH_OFFSET_WMAX_TOUT))) / 1000; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1087 | info->flash_id = FLASH_MAN_CFI; |
| 1088 | } |
| 1089 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1090 | flash_write_cmd (info, 0, 0, FLASH_CMD_RESET); |
| 1091 | return (info->size); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | /*----------------------------------------------------------------------- |
| 1096 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1097 | static int flash_write_cfiword (flash_info_t * info, ulong dest, |
| 1098 | cfiword_t cword) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1099 | { |
| 1100 | |
| 1101 | cfiptr_t ctladdr; |
| 1102 | cfiptr_t cptr; |
| 1103 | int flag; |
| 1104 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1105 | ctladdr.cp = flash_make_addr (info, 0, 0); |
| 1106 | cptr.cp = (uchar *) dest; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1107 | |
| 1108 | |
| 1109 | /* Check if Flash is (sufficiently) erased */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1110 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1111 | case FLASH_CFI_8BIT: |
| 1112 | flag = ((cptr.cp[0] & cword.c) == cword.c); |
| 1113 | break; |
| 1114 | case FLASH_CFI_16BIT: |
| 1115 | flag = ((cptr.wp[0] & cword.w) == cword.w); |
| 1116 | break; |
| 1117 | case FLASH_CFI_32BIT: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1118 | flag = ((cptr.lp[0] & cword.l) == cword.l); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1119 | break; |
| 1120 | case FLASH_CFI_64BIT: |
| 1121 | flag = ((cptr.lp[0] & cword.ll) == cword.ll); |
| 1122 | break; |
| 1123 | default: |
| 1124 | return 2; |
| 1125 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1126 | if (!flag) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1127 | return 2; |
| 1128 | |
| 1129 | /* Disable interrupts which might cause a timeout here */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1130 | flag = disable_interrupts (); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1131 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1132 | switch (info->vendor) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1133 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1134 | case CFI_CMDSET_INTEL_STANDARD: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1135 | flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS); |
| 1136 | flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1137 | break; |
| 1138 | case CFI_CMDSET_AMD_EXTENDED: |
| 1139 | case CFI_CMDSET_AMD_STANDARD: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1140 | flash_unlock_seq (info, 0); |
| 1141 | flash_write_cmd (info, 0, 0x555, AMD_CMD_WRITE); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1142 | break; |
| 1143 | } |
| 1144 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1145 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1146 | case FLASH_CFI_8BIT: |
| 1147 | cptr.cp[0] = cword.c; |
| 1148 | break; |
| 1149 | case FLASH_CFI_16BIT: |
| 1150 | cptr.wp[0] = cword.w; |
| 1151 | break; |
| 1152 | case FLASH_CFI_32BIT: |
| 1153 | cptr.lp[0] = cword.l; |
| 1154 | break; |
| 1155 | case FLASH_CFI_64BIT: |
| 1156 | cptr.llp[0] = cword.ll; |
| 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | /* re-enable interrupts if necessary */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1161 | if (flag) |
| 1162 | enable_interrupts (); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1163 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1164 | return flash_full_status_check (info, 0, info->write_tout, "write"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | #ifdef CFG_FLASH_USE_BUFFER_WRITE |
| 1168 | |
| 1169 | /* loop through the sectors from the highest address |
| 1170 | * when the passed address is greater or equal to the sector address |
| 1171 | * we have a match |
| 1172 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1173 | static flash_sect_t find_sector (flash_info_t * info, ulong addr) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1174 | { |
| 1175 | flash_sect_t sector; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1176 | |
| 1177 | for (sector = info->sector_count - 1; sector >= 0; sector--) { |
| 1178 | if (addr >= info->start[sector]) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1179 | break; |
| 1180 | } |
| 1181 | return sector; |
| 1182 | } |
| 1183 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1184 | static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, |
| 1185 | int len) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1186 | { |
| 1187 | flash_sect_t sector; |
| 1188 | int cnt; |
| 1189 | int retcode; |
| 1190 | volatile cfiptr_t src; |
| 1191 | volatile cfiptr_t dst; |
| 1192 | |
| 1193 | src.cp = cp; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1194 | dst.cp = (uchar *) dest; |
| 1195 | sector = find_sector (info, dest); |
| 1196 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 1197 | flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER); |
| 1198 | if ((retcode = |
| 1199 | flash_status_check (info, sector, info->buffer_write_tout, |
| 1200 | "write to buffer")) == ERR_OK) { |
| 1201 | /* reduce the number of loops by the width of the port */ |
| 1202 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1203 | case FLASH_CFI_8BIT: |
| 1204 | cnt = len; |
| 1205 | break; |
| 1206 | case FLASH_CFI_16BIT: |
| 1207 | cnt = len >> 1; |
| 1208 | break; |
| 1209 | case FLASH_CFI_32BIT: |
| 1210 | cnt = len >> 2; |
| 1211 | break; |
| 1212 | case FLASH_CFI_64BIT: |
| 1213 | cnt = len >> 3; |
| 1214 | break; |
| 1215 | default: |
| 1216 | return ERR_INVAL; |
| 1217 | break; |
| 1218 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1219 | flash_write_cmd (info, sector, 0, (uchar) cnt - 1); |
| 1220 | while (cnt-- > 0) { |
| 1221 | switch (info->portwidth) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1222 | case FLASH_CFI_8BIT: |
| 1223 | *dst.cp++ = *src.cp++; |
| 1224 | break; |
| 1225 | case FLASH_CFI_16BIT: |
| 1226 | *dst.wp++ = *src.wp++; |
| 1227 | break; |
| 1228 | case FLASH_CFI_32BIT: |
| 1229 | *dst.lp++ = *src.lp++; |
| 1230 | break; |
| 1231 | case FLASH_CFI_64BIT: |
| 1232 | *dst.llp++ = *src.llp++; |
| 1233 | break; |
| 1234 | default: |
| 1235 | return ERR_INVAL; |
| 1236 | break; |
| 1237 | } |
| 1238 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1239 | flash_write_cmd (info, sector, 0, |
| 1240 | FLASH_CMD_WRITE_BUFFER_CONFIRM); |
| 1241 | retcode = |
| 1242 | flash_full_status_check (info, sector, |
| 1243 | info->buffer_write_tout, |
| 1244 | "buffer write"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1245 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1246 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1247 | return retcode; |
| 1248 | } |
| 1249 | #endif /* CFG_USE_FLASH_BUFFER_WRITE */ |
| 1250 | #endif /* CFG_FLASH_CFI */ |