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