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