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