blob: 696f9a47ada93a25b65a918adf7d32da720945f0 [file] [log] [blame]
wdenk2cefd152004-02-08 22:55:38 +00001/*
wdenke65527f2004-02-12 00:47:09 +00002 * (C) Copyright 2002-2004
wdenk2cefd152004-02-08 22:55:38 +00003 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4 *
5 * Copyright (C) 2003 Arabella Software Ltd.
6 * Yuli Barcohen <yuli@arabellasw.com>
wdenk2cefd152004-02-08 22:55:38 +00007 *
wdenke65527f2004-02-12 00:47:09 +00008 * Copyright (C) 2004
9 * Ed Okerson
Stefan Roese12797482006-11-13 13:55:24 +010010 *
11 * Copyright (C) 2006
12 * Tolunay Orkun <listmember@orkun.us>
wdenke65527f2004-02-12 00:47:09 +000013 *
wdenk2cefd152004-02-08 22:55:38 +000014 * 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 *
wdenk2cefd152004-02-08 22:55:38 +000032 */
33
34/* The DEBUG define must be before common to enable debugging */
wdenk2ebee312004-02-23 19:30:57 +000035/* #define DEBUG */
36
wdenk2cefd152004-02-08 22:55:38 +000037#include <common.h>
38#include <asm/processor.h>
wdenkaeba06f2004-06-09 17:34:58 +000039#include <asm/byteorder.h>
wdenkd0245fc2005-04-13 10:02:42 +000040#include <environment.h>
wdenke65527f2004-02-12 00:47:09 +000041#ifdef CFG_FLASH_CFI_DRIVER
wdenke537b3b2004-02-23 23:54:43 +000042
wdenk2cefd152004-02-08 22:55:38 +000043/*
44 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
45 * The width of the port and the width of the chips are determined at initialization.
46 * These widths are used to calculate the address for access CFI data structures.
wdenk2cefd152004-02-08 22:55:38 +000047 *
48 * References
49 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
50 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
51 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
52 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese12797482006-11-13 13:55:24 +010053 * AMD CFI Specification, Release 2.0 December 1, 2001
54 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
55 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk2cefd152004-02-08 22:55:38 +000056 *
Heiko Schocher800db312007-01-19 18:05:26 +010057 * define CFG_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
58 * reading and writing ... (yes there is such a Hardware).
wdenk2cefd152004-02-08 22:55:38 +000059 */
60
wdenke65527f2004-02-12 00:47:09 +000061#ifndef CFG_FLASH_BANKS_LIST
62#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
63#endif
64
wdenk2cefd152004-02-08 22:55:38 +000065#define FLASH_CMD_CFI 0x98
66#define FLASH_CMD_READ_ID 0x90
67#define FLASH_CMD_RESET 0xff
68#define FLASH_CMD_BLOCK_ERASE 0x20
69#define FLASH_CMD_ERASE_CONFIRM 0xD0
70#define FLASH_CMD_WRITE 0x40
71#define FLASH_CMD_PROTECT 0x60
72#define FLASH_CMD_PROTECT_SET 0x01
73#define FLASH_CMD_PROTECT_CLEAR 0xD0
74#define FLASH_CMD_CLEAR_STATUS 0x50
wdenke65527f2004-02-12 00:47:09 +000075#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
76#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk2cefd152004-02-08 22:55:38 +000077
78#define FLASH_STATUS_DONE 0x80
79#define FLASH_STATUS_ESS 0x40
80#define FLASH_STATUS_ECLBS 0x20
81#define FLASH_STATUS_PSLBS 0x10
82#define FLASH_STATUS_VPENS 0x08
83#define FLASH_STATUS_PSS 0x04
84#define FLASH_STATUS_DPS 0x02
85#define FLASH_STATUS_R 0x01
86#define FLASH_STATUS_PROTECT 0x01
87
88#define AMD_CMD_RESET 0xF0
89#define AMD_CMD_WRITE 0xA0
90#define AMD_CMD_ERASE_START 0x80
91#define AMD_CMD_ERASE_SECTOR 0x30
wdenked2ac4b2004-03-14 18:23:55 +000092#define AMD_CMD_UNLOCK_START 0xAA
93#define AMD_CMD_UNLOCK_ACK 0x55
Stefan Roesec865e6c2006-02-28 15:29:58 +010094#define AMD_CMD_WRITE_TO_BUFFER 0x25
95#define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
wdenk2cefd152004-02-08 22:55:38 +000096
97#define AMD_STATUS_TOGGLE 0x40
98#define AMD_STATUS_ERROR 0x20
Stefan Roesec865e6c2006-02-28 15:29:58 +010099
100#define AMD_ADDR_ERASE_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
101#define AMD_ADDR_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
102#define AMD_ADDR_ACK ((info->portwidth == FLASH_CFI_8BIT) ? 0x555 : 0x2AA)
wdenk2cefd152004-02-08 22:55:38 +0000103
Stefan Roese12797482006-11-13 13:55:24 +0100104#define FLASH_OFFSET_MANUFACTURER_ID 0x00
105#define FLASH_OFFSET_DEVICE_ID 0x01
106#define FLASH_OFFSET_DEVICE_ID2 0x0E
107#define FLASH_OFFSET_DEVICE_ID3 0x0F
wdenk2cefd152004-02-08 22:55:38 +0000108#define FLASH_OFFSET_CFI 0x55
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100109#define FLASH_OFFSET_CFI_ALT 0x555
wdenk2cefd152004-02-08 22:55:38 +0000110#define FLASH_OFFSET_CFI_RESP 0x10
wdenke65527f2004-02-12 00:47:09 +0000111#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
Stefan Roeseefef95b2006-04-01 13:41:03 +0200112#define FLASH_OFFSET_EXT_QUERY_T_P_ADDR 0x15 /* extended query table primary addr */
wdenk2cefd152004-02-08 22:55:38 +0000113#define FLASH_OFFSET_WTOUT 0x1F
wdenke65527f2004-02-12 00:47:09 +0000114#define FLASH_OFFSET_WBTOUT 0x20
wdenk2cefd152004-02-08 22:55:38 +0000115#define FLASH_OFFSET_ETOUT 0x21
wdenke65527f2004-02-12 00:47:09 +0000116#define FLASH_OFFSET_CETOUT 0x22
wdenk2cefd152004-02-08 22:55:38 +0000117#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenke65527f2004-02-12 00:47:09 +0000118#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk2cefd152004-02-08 22:55:38 +0000119#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenke65527f2004-02-12 00:47:09 +0000120#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk2cefd152004-02-08 22:55:38 +0000121#define FLASH_OFFSET_SIZE 0x27
wdenke65527f2004-02-12 00:47:09 +0000122#define FLASH_OFFSET_INTERFACE 0x28
123#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk2cefd152004-02-08 22:55:38 +0000124#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
125#define FLASH_OFFSET_ERASE_REGIONS 0x2D
126#define FLASH_OFFSET_PROTECT 0x02
wdenke65527f2004-02-12 00:47:09 +0000127#define FLASH_OFFSET_USER_PROTECTION 0x85
128#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk2cefd152004-02-08 22:55:38 +0000129
Stefan Roese12797482006-11-13 13:55:24 +0100130#define CFI_CMDSET_NONE 0
131#define CFI_CMDSET_INTEL_EXTENDED 1
132#define CFI_CMDSET_AMD_STANDARD 2
133#define CFI_CMDSET_INTEL_STANDARD 3
134#define CFI_CMDSET_AMD_EXTENDED 4
135#define CFI_CMDSET_MITSU_STANDARD 256
136#define CFI_CMDSET_MITSU_EXTENDED 257
137#define CFI_CMDSET_SST 258
wdenk2cefd152004-02-08 22:55:38 +0000138
wdenk51242782004-12-18 22:35:43 +0000139#ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
140# undef FLASH_CMD_RESET
Stefan Roese12797482006-11-13 13:55:24 +0100141# define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
wdenk51242782004-12-18 22:35:43 +0000142#endif
143
wdenk2cefd152004-02-08 22:55:38 +0000144typedef union {
145 unsigned char c;
146 unsigned short w;
147 unsigned long l;
148 unsigned long long ll;
149} cfiword_t;
150
151typedef union {
wdenke65527f2004-02-12 00:47:09 +0000152 volatile unsigned char *cp;
wdenk2cefd152004-02-08 22:55:38 +0000153 volatile unsigned short *wp;
wdenke65527f2004-02-12 00:47:09 +0000154 volatile unsigned long *lp;
wdenk2cefd152004-02-08 22:55:38 +0000155 volatile unsigned long long *llp;
156} cfiptr_t;
157
Stefan Roese12797482006-11-13 13:55:24 +0100158#define NUM_ERASE_REGIONS 4 /* max. number of erase regions */
wdenk2cefd152004-02-08 22:55:38 +0000159
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100160static uint flash_offset_cfi[2]={FLASH_OFFSET_CFI,FLASH_OFFSET_CFI_ALT};
161
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200162/* use CFG_MAX_FLASH_BANKS_DETECT if defined */
163#ifdef CFG_MAX_FLASH_BANKS_DETECT
164static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
165flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
166#else
wdenk2cefd152004-02-08 22:55:38 +0000167static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200168flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
169#endif
wdenk2cefd152004-02-08 22:55:38 +0000170
Stefan Roesec865e6c2006-02-28 15:29:58 +0100171/*
172 * Check if chip width is defined. If not, start detecting with 8bit.
173 */
174#ifndef CFG_FLASH_CFI_WIDTH
175#define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
176#endif
177
wdenk2cefd152004-02-08 22:55:38 +0000178
179/*-----------------------------------------------------------------------
180 * Functions
181 */
182
183typedef unsigned long flash_sect_t;
184
wdenke65527f2004-02-12 00:47:09 +0000185static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
186static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenke537b3b2004-02-23 23:54:43 +0000187static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000188static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenke537b3b2004-02-23 23:54:43 +0000189static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
190static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
191static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
Stefan Roese12797482006-11-13 13:55:24 +0100192static void flash_read_jedec_ids (flash_info_t * info);
wdenke65527f2004-02-12 00:47:09 +0000193static int flash_detect_cfi (flash_info_t * info);
wdenke537b3b2004-02-23 23:54:43 +0000194static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenke65527f2004-02-12 00:47:09 +0000195static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
196 ulong tout, char *prompt);
Stefan Roese896391f2006-03-01 17:00:49 +0100197ulong flash_get_size (ulong base, int banknum);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200198#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000199static flash_info_t *flash_get_info(ulong base);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200200#endif
wdenk2cefd152004-02-08 22:55:38 +0000201#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke537b3b2004-02-23 23:54:43 +0000202static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk2cefd152004-02-08 22:55:38 +0000203#endif
204
wdenke65527f2004-02-12 00:47:09 +0000205/*-----------------------------------------------------------------------
206 * create an address based on the offset and the port width
207 */
wdenke537b3b2004-02-23 23:54:43 +0000208inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000209{
210 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
211}
212
wdenk2cefd152004-02-08 22:55:38 +0000213#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000214/*-----------------------------------------------------------------------
215 * Debug support
216 */
217void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000218{
219 int i;
220 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000221
222 cp = (unsigned char *) &data;
223 for (i = 0; i < 8; i++)
224 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000225}
wdenke65527f2004-02-12 00:47:09 +0000226static void flash_printqry (flash_info_t * info, flash_sect_t sect)
227{
228 cfiptr_t cptr;
229 int x, y;
230
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200231 for (x = 0; x < 0x40; x += 16U / info->portwidth) {
wdenke65527f2004-02-12 00:47:09 +0000232 cptr.cp =
233 flash_make_addr (info, sect,
234 x + FLASH_OFFSET_CFI_RESP);
235 debug ("%p : ", cptr.cp);
236 for (y = 0; y < 16; y++) {
237 debug ("%2.2x ", cptr.cp[y]);
238 }
239 debug (" ");
240 for (y = 0; y < 16; y++) {
241 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
242 debug ("%c", cptr.cp[y]);
243 } else {
244 debug (".");
245 }
246 }
247 debug ("\n");
248 }
249}
wdenk2cefd152004-02-08 22:55:38 +0000250#endif
251
252
253/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000254 * read a character at a port width address
255 */
wdenke65527f2004-02-12 00:47:09 +0000256inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000257{
258 uchar *cp;
wdenke65527f2004-02-12 00:47:09 +0000259
260 cp = flash_make_addr (info, 0, offset);
Heiko Schocher800db312007-01-19 18:05:26 +0100261#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenke65527f2004-02-12 00:47:09 +0000262 return (cp[0]);
263#else
wdenk2cefd152004-02-08 22:55:38 +0000264 return (cp[info->portwidth - 1]);
wdenke65527f2004-02-12 00:47:09 +0000265#endif
wdenk2cefd152004-02-08 22:55:38 +0000266}
267
268/*-----------------------------------------------------------------------
269 * read a short word by swapping for ppc format.
270 */
wdenke65527f2004-02-12 00:47:09 +0000271ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000272{
wdenke65527f2004-02-12 00:47:09 +0000273 uchar *addr;
274 ushort retval;
wdenk2cefd152004-02-08 22:55:38 +0000275
wdenke65527f2004-02-12 00:47:09 +0000276#ifdef DEBUG
277 int x;
278#endif
279 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000280
wdenke65527f2004-02-12 00:47:09 +0000281#ifdef DEBUG
282 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
283 info->portwidth);
284 for (x = 0; x < 2 * info->portwidth; x++) {
285 debug ("addr[%x] = 0x%x\n", x, addr[x]);
286 }
287#endif
Heiko Schocher800db312007-01-19 18:05:26 +0100288#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenke65527f2004-02-12 00:47:09 +0000289 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
290#else
291 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
292 addr[info->portwidth - 1]);
293#endif
294
295 debug ("retval = 0x%x\n", retval);
296 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000297}
298
299/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +0100300 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000301 * port size word. Swap for ppc format.
302 */
wdenke65527f2004-02-12 00:47:09 +0000303ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000304{
wdenke65527f2004-02-12 00:47:09 +0000305 uchar *addr;
306 ulong retval;
307
308#ifdef DEBUG
309 int x;
310#endif
311 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000312
wdenke65527f2004-02-12 00:47:09 +0000313#ifdef DEBUG
314 debug ("long addr is at %p info->portwidth = %d\n", addr,
315 info->portwidth);
316 for (x = 0; x < 4 * info->portwidth; x++) {
317 debug ("addr[%x] = 0x%x\n", x, addr[x]);
318 }
319#endif
Heiko Schocher800db312007-01-19 18:05:26 +0100320#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenke65527f2004-02-12 00:47:09 +0000321 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenke537b3b2004-02-23 23:54:43 +0000322 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenke65527f2004-02-12 00:47:09 +0000323#else
324 retval = (addr[(2 * info->portwidth) - 1] << 24) |
325 (addr[(info->portwidth) - 1] << 16) |
326 (addr[(4 * info->portwidth) - 1] << 8) |
327 addr[(3 * info->portwidth) - 1];
328#endif
329 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000330}
331
Stefan Roesec865e6c2006-02-28 15:29:58 +0100332
wdenk2cefd152004-02-08 22:55:38 +0000333/*-----------------------------------------------------------------------
334 */
335unsigned long flash_init (void)
336{
337 unsigned long size = 0;
338 int i;
339
Stefan Roeseefef95b2006-04-01 13:41:03 +0200340#ifdef CFG_FLASH_PROTECTION
341 char *s = getenv("unlock");
342#endif
343
wdenk2cefd152004-02-08 22:55:38 +0000344 /* Init: no FLASHes known */
wdenke65527f2004-02-12 00:47:09 +0000345 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000346 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenke65527f2004-02-12 00:47:09 +0000347 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk2cefd152004-02-08 22:55:38 +0000348 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Stefan Roesec443fe92005-11-22 13:20:42 +0100349#ifndef CFG_FLASH_QUIET_TEST
wdenke537b3b2004-02-23 23:54:43 +0000350 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100351 i+1, flash_info[i].size, flash_info[i].size << 20);
Stefan Roesec443fe92005-11-22 13:20:42 +0100352#endif /* CFG_FLASH_QUIET_TEST */
wdenk2cefd152004-02-08 22:55:38 +0000353 }
Stefan Roesec865e6c2006-02-28 15:29:58 +0100354#ifdef CFG_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +0200355 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
356 /*
357 * Only the U-Boot image and it's environment is protected,
358 * all other sectors are unprotected (unlocked) if flash
359 * hardware protection is used (CFG_FLASH_PROTECTION) and
360 * the environment variable "unlock" is set to "yes".
361 */
362 if (flash_info[i].legacy_unlock) {
363 int k;
Stefan Roesec865e6c2006-02-28 15:29:58 +0100364
Stefan Roeseefef95b2006-04-01 13:41:03 +0200365 /*
366 * Disable legacy_unlock temporarily, since
367 * flash_real_protect would relock all other sectors
368 * again otherwise.
369 */
370 flash_info[i].legacy_unlock = 0;
371
372 /*
373 * Legacy unlocking (e.g. Intel J3) -> unlock only one
374 * sector. This will unlock all sectors.
375 */
376 flash_real_protect (&flash_info[i], 0, 0);
377
378 flash_info[i].legacy_unlock = 1;
379
380 /*
381 * Manually mark other sectors as unlocked (unprotected)
382 */
383 for (k = 1; k < flash_info[i].sector_count; k++)
384 flash_info[i].protect[k] = 0;
385 } else {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100386 /*
Stefan Roeseefef95b2006-04-01 13:41:03 +0200387 * No legancy unlocking -> unlock all sectors
Stefan Roesec865e6c2006-02-28 15:29:58 +0100388 */
389 flash_protect (FLAG_PROTECT_CLEAR,
390 flash_info[i].start[0],
391 flash_info[i].start[0] + flash_info[i].size - 1,
392 &flash_info[i]);
393 }
394 }
395#endif /* CFG_FLASH_PROTECTION */
wdenk2cefd152004-02-08 22:55:38 +0000396 }
397
398 /* Monitor protection ON by default */
399#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenke65527f2004-02-12 00:47:09 +0000400 flash_protect (FLAG_PROTECT_SET,
401 CFG_MONITOR_BASE,
wdenk5c71a7a2005-05-16 15:23:22 +0000402 CFG_MONITOR_BASE + monitor_flash_len - 1,
403 flash_get_info(CFG_MONITOR_BASE));
wdenk2cefd152004-02-08 22:55:38 +0000404#endif
405
wdenke85b7a52004-10-10 22:16:06 +0000406 /* Environment protection ON by default */
407#ifdef CFG_ENV_IS_IN_FLASH
408 flash_protect (FLAG_PROTECT_SET,
409 CFG_ENV_ADDR,
410 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000411 flash_get_info(CFG_ENV_ADDR));
wdenke85b7a52004-10-10 22:16:06 +0000412#endif
413
414 /* Redundant environment protection ON by default */
415#ifdef CFG_ENV_ADDR_REDUND
416 flash_protect (FLAG_PROTECT_SET,
417 CFG_ENV_ADDR_REDUND,
418 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000419 flash_get_info(CFG_ENV_ADDR_REDUND));
wdenke85b7a52004-10-10 22:16:06 +0000420#endif
wdenk2cefd152004-02-08 22:55:38 +0000421 return (size);
422}
423
424/*-----------------------------------------------------------------------
425 */
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200426#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000427static flash_info_t *flash_get_info(ulong base)
428{
429 int i;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200430 flash_info_t * info = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000431
432 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
433 info = & flash_info[i];
434 if (info->size && info->start[0] <= base &&
435 base <= info->start[0] + info->size - 1)
436 break;
437 }
438
439 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
440}
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200441#endif
wdenk5c71a7a2005-05-16 15:23:22 +0000442
443/*-----------------------------------------------------------------------
444 */
wdenke65527f2004-02-12 00:47:09 +0000445int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +0000446{
447 int rcode = 0;
448 int prot;
449 flash_sect_t sect;
450
wdenke65527f2004-02-12 00:47:09 +0000451 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000452 puts ("Can't erase unknown flash type - aborted\n");
wdenk2cefd152004-02-08 22:55:38 +0000453 return 1;
454 }
455 if ((s_first < 0) || (s_first > s_last)) {
wdenk42c05472004-03-23 22:14:11 +0000456 puts ("- no sectors to erase\n");
wdenk2cefd152004-02-08 22:55:38 +0000457 return 1;
458 }
459
460 prot = 0;
wdenke65527f2004-02-12 00:47:09 +0000461 for (sect = s_first; sect <= s_last; ++sect) {
wdenk2cefd152004-02-08 22:55:38 +0000462 if (info->protect[sect]) {
463 prot++;
464 }
465 }
466 if (prot) {
wdenke65527f2004-02-12 00:47:09 +0000467 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk2cefd152004-02-08 22:55:38 +0000468 } else {
wdenk42c05472004-03-23 22:14:11 +0000469 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000470 }
471
472
wdenke65527f2004-02-12 00:47:09 +0000473 for (sect = s_first; sect <= s_last; sect++) {
wdenk2cefd152004-02-08 22:55:38 +0000474 if (info->protect[sect] == 0) { /* not protected */
wdenke65527f2004-02-12 00:47:09 +0000475 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000476 case CFI_CMDSET_INTEL_STANDARD:
477 case CFI_CMDSET_INTEL_EXTENDED:
wdenke537b3b2004-02-23 23:54:43 +0000478 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
479 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
480 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk2cefd152004-02-08 22:55:38 +0000481 break;
482 case CFI_CMDSET_AMD_STANDARD:
483 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000484 flash_unlock_seq (info, sect);
wdenked2ac4b2004-03-14 18:23:55 +0000485 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
486 AMD_CMD_ERASE_START);
wdenke65527f2004-02-12 00:47:09 +0000487 flash_unlock_seq (info, sect);
wdenke537b3b2004-02-23 23:54:43 +0000488 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk2cefd152004-02-08 22:55:38 +0000489 break;
490 default:
wdenke65527f2004-02-12 00:47:09 +0000491 debug ("Unkown flash vendor %d\n",
492 info->vendor);
wdenk2cefd152004-02-08 22:55:38 +0000493 break;
494 }
495
wdenke65527f2004-02-12 00:47:09 +0000496 if (flash_full_status_check
497 (info, sect, info->erase_blk_tout, "erase")) {
wdenk2cefd152004-02-08 22:55:38 +0000498 rcode = 1;
499 } else
wdenk42c05472004-03-23 22:14:11 +0000500 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +0000501 }
502 }
wdenk42c05472004-03-23 22:14:11 +0000503 puts (" done\n");
wdenk2cefd152004-02-08 22:55:38 +0000504 return rcode;
505}
506
507/*-----------------------------------------------------------------------
508 */
wdenke65527f2004-02-12 00:47:09 +0000509void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +0000510{
511 int i;
512
513 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000514 puts ("missing or unknown FLASH type\n");
wdenk2cefd152004-02-08 22:55:38 +0000515 return;
516 }
517
wdenke65527f2004-02-12 00:47:09 +0000518 printf ("CFI conformant FLASH (%d x %d)",
519 (info->portwidth << 3), (info->chipwidth << 3));
wdenk2cefd152004-02-08 22:55:38 +0000520 printf (" Size: %ld MB in %d Sectors\n",
521 info->size >> 20, info->sector_count);
Stefan Roese12797482006-11-13 13:55:24 +0100522 printf (" ");
523 switch (info->vendor) {
524 case CFI_CMDSET_INTEL_STANDARD:
525 printf ("Intel Standard");
526 break;
527 case CFI_CMDSET_INTEL_EXTENDED:
528 printf ("Intel Extended");
529 break;
530 case CFI_CMDSET_AMD_STANDARD:
531 printf ("AMD Standard");
532 break;
533 case CFI_CMDSET_AMD_EXTENDED:
534 printf ("AMD Extended");
535 break;
536 default:
537 printf ("Unknown (%d)", info->vendor);
538 break;
539 }
540 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
541 info->manufacturer_id, info->device_id);
542 if (info->device_id == 0x7E) {
543 printf("%04X", info->device_id2);
544 }
545 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
wdenke537b3b2004-02-23 23:54:43 +0000546 info->erase_blk_tout,
Stefan Roese12797482006-11-13 13:55:24 +0100547 info->write_tout);
548 if (info->buffer_size > 1) {
549 printf (" Buffer write timeout: %ld ms, buffer size: %d bytes\n",
wdenke537b3b2004-02-23 23:54:43 +0000550 info->buffer_write_tout,
551 info->buffer_size);
Stefan Roese12797482006-11-13 13:55:24 +0100552 }
wdenk2cefd152004-02-08 22:55:38 +0000553
Stefan Roese12797482006-11-13 13:55:24 +0100554 puts ("\n Sector Start Addresses:");
wdenke65527f2004-02-12 00:47:09 +0000555 for (i = 0; i < info->sector_count; ++i) {
Stefan Roese12797482006-11-13 13:55:24 +0100556 if ((i % 5) == 0)
557 printf ("\n");
wdenk2cefd152004-02-08 22:55:38 +0000558#ifdef CFG_FLASH_EMPTY_INFO
559 int k;
560 int size;
561 int erased;
562 volatile unsigned long *flash;
563
564 /*
565 * Check if whole sector is erased
566 */
wdenke65527f2004-02-12 00:47:09 +0000567 if (i != (info->sector_count - 1))
568 size = info->start[i + 1] - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000569 else
wdenke65527f2004-02-12 00:47:09 +0000570 size = info->start[0] + info->size - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000571 erased = 1;
wdenke65527f2004-02-12 00:47:09 +0000572 flash = (volatile unsigned long *) info->start[i];
573 size = size >> 2; /* divide by 4 for longword access */
574 for (k = 0; k < size; k++) {
575 if (*flash++ != 0xffffffff) {
576 erased = 0;
577 break;
578 }
579 }
wdenk2cefd152004-02-08 22:55:38 +0000580
wdenk2cefd152004-02-08 22:55:38 +0000581 /* print empty and read-only info */
Stefan Roese12797482006-11-13 13:55:24 +0100582 printf (" %08lX %c %s ",
wdenk2cefd152004-02-08 22:55:38 +0000583 info->start[i],
Stefan Roese12797482006-11-13 13:55:24 +0100584 erased ? 'E' : ' ',
585 info->protect[i] ? "RO" : " ");
Wolfgang Denkd3abe5a2005-09-25 00:23:05 +0200586#else /* ! CFG_FLASH_EMPTY_INFO */
Stefan Roese12797482006-11-13 13:55:24 +0100587 printf (" %08lX %s ",
588 info->start[i],
589 info->protect[i] ? "RO" : " ");
wdenk2cefd152004-02-08 22:55:38 +0000590#endif
591 }
wdenk42c05472004-03-23 22:14:11 +0000592 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000593 return;
594}
595
596/*-----------------------------------------------------------------------
597 * Copy memory to flash, returns:
598 * 0 - OK
599 * 1 - write timeout
600 * 2 - Flash not erased
601 */
wdenke65527f2004-02-12 00:47:09 +0000602int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +0000603{
604 ulong wp;
605 ulong cp;
606 int aln;
607 cfiword_t cword;
608 int i, rc;
609
wdenke65527f2004-02-12 00:47:09 +0000610#ifdef CFG_FLASH_USE_BUFFER_WRITE
611 int buffered_size;
612#endif
wdenke65527f2004-02-12 00:47:09 +0000613 /* get lower aligned address */
wdenk2cefd152004-02-08 22:55:38 +0000614 /* get lower aligned address */
615 wp = (addr & ~(info->portwidth - 1));
616
617 /* handle unaligned start */
wdenke65527f2004-02-12 00:47:09 +0000618 if ((aln = addr - wp) != 0) {
wdenk2cefd152004-02-08 22:55:38 +0000619 cword.l = 0;
620 cp = wp;
wdenke65527f2004-02-12 00:47:09 +0000621 for (i = 0; i < aln; ++i, ++cp)
622 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000623
wdenke65527f2004-02-12 00:47:09 +0000624 for (; (i < info->portwidth) && (cnt > 0); i++) {
625 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000626 cnt--;
627 cp++;
628 }
wdenke65527f2004-02-12 00:47:09 +0000629 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
630 flash_add_byte (info, &cword, (*(uchar *) cp));
631 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000632 return rc;
633 wp = cp;
634 }
635
wdenke65527f2004-02-12 00:47:09 +0000636 /* handle the aligned part */
wdenk2cefd152004-02-08 22:55:38 +0000637#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke65527f2004-02-12 00:47:09 +0000638 buffered_size = (info->portwidth / info->chipwidth);
639 buffered_size *= info->buffer_size;
640 while (cnt >= info->portwidth) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100641 /* prohibit buffer write when buffer_size is 1 */
642 if (info->buffer_size == 1) {
643 cword.l = 0;
644 for (i = 0; i < info->portwidth; i++)
645 flash_add_byte (info, &cword, *src++);
646 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
647 return rc;
648 wp += info->portwidth;
649 cnt -= info->portwidth;
650 continue;
651 }
652
653 /* write buffer until next buffered_size aligned boundary */
654 i = buffered_size - (wp % buffered_size);
655 if (i > cnt)
656 i = cnt;
wdenke65527f2004-02-12 00:47:09 +0000657 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk2cefd152004-02-08 22:55:38 +0000658 return rc;
Wolfgang Denke25f0522005-08-12 22:35:59 +0200659 i -= i & (info->portwidth - 1);
wdenk2cefd152004-02-08 22:55:38 +0000660 wp += i;
661 src += i;
wdenke65527f2004-02-12 00:47:09 +0000662 cnt -= i;
wdenk2cefd152004-02-08 22:55:38 +0000663 }
664#else
wdenke65527f2004-02-12 00:47:09 +0000665 while (cnt >= info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000666 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000667 for (i = 0; i < info->portwidth; i++) {
668 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000669 }
wdenke65527f2004-02-12 00:47:09 +0000670 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000671 return rc;
672 wp += info->portwidth;
673 cnt -= info->portwidth;
674 }
675#endif /* CFG_FLASH_USE_BUFFER_WRITE */
676 if (cnt == 0) {
677 return (0);
678 }
679
680 /*
681 * handle unaligned tail bytes
682 */
683 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000684 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
685 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000686 --cnt;
687 }
wdenke65527f2004-02-12 00:47:09 +0000688 for (; i < info->portwidth; ++i, ++cp) {
689 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000690 }
691
wdenke65527f2004-02-12 00:47:09 +0000692 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +0000693}
694
695/*-----------------------------------------------------------------------
696 */
697#ifdef CFG_FLASH_PROTECTION
698
wdenke65527f2004-02-12 00:47:09 +0000699int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +0000700{
701 int retcode = 0;
702
wdenke65527f2004-02-12 00:47:09 +0000703 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
704 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
705 if (prot)
706 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk2cefd152004-02-08 22:55:38 +0000707 else
wdenke65527f2004-02-12 00:47:09 +0000708 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk2cefd152004-02-08 22:55:38 +0000709
wdenke65527f2004-02-12 00:47:09 +0000710 if ((retcode =
711 flash_full_status_check (info, sector, info->erase_blk_tout,
712 prot ? "protect" : "unprotect")) == 0) {
wdenk2cefd152004-02-08 22:55:38 +0000713
714 info->protect[sector] = prot;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200715
716 /*
717 * On some of Intel's flash chips (marked via legacy_unlock)
718 * unprotect unprotects all locking.
719 */
720 if ((prot == 0) && (info->legacy_unlock)) {
wdenk2cefd152004-02-08 22:55:38 +0000721 flash_sect_t i;
wdenke65527f2004-02-12 00:47:09 +0000722
723 for (i = 0; i < info->sector_count; i++) {
724 if (info->protect[i])
725 flash_real_protect (info, i, 1);
wdenk2cefd152004-02-08 22:55:38 +0000726 }
727 }
728 }
wdenk2cefd152004-02-08 22:55:38 +0000729 return retcode;
wdenke65527f2004-02-12 00:47:09 +0000730}
731
wdenk2cefd152004-02-08 22:55:38 +0000732/*-----------------------------------------------------------------------
733 * flash_read_user_serial - read the OneTimeProgramming cells
734 */
wdenke65527f2004-02-12 00:47:09 +0000735void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
736 int len)
wdenk2cefd152004-02-08 22:55:38 +0000737{
wdenke65527f2004-02-12 00:47:09 +0000738 uchar *src;
739 uchar *dst;
wdenk2cefd152004-02-08 22:55:38 +0000740
741 dst = buffer;
wdenke65527f2004-02-12 00:47:09 +0000742 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
743 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
744 memcpy (dst, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200745 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000746}
wdenke65527f2004-02-12 00:47:09 +0000747
wdenk2cefd152004-02-08 22:55:38 +0000748/*
749 * flash_read_factory_serial - read the device Id from the protection area
750 */
wdenke65527f2004-02-12 00:47:09 +0000751void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
752 int len)
wdenk2cefd152004-02-08 22:55:38 +0000753{
wdenke65527f2004-02-12 00:47:09 +0000754 uchar *src;
wdenk6cfa84e2004-02-10 00:03:41 +0000755
wdenke65527f2004-02-12 00:47:09 +0000756 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
757 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
758 memcpy (buffer, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200759 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000760}
761
762#endif /* CFG_FLASH_PROTECTION */
763
wdenke65527f2004-02-12 00:47:09 +0000764/*
765 * flash_is_busy - check to see if the flash is busy
766 * This routine checks the status of the chip and returns true if the chip is busy
767 */
768static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000769{
770 int retval;
wdenke65527f2004-02-12 00:47:09 +0000771
772 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000773 case CFI_CMDSET_INTEL_STANDARD:
774 case CFI_CMDSET_INTEL_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000775 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk2cefd152004-02-08 22:55:38 +0000776 break;
777 case CFI_CMDSET_AMD_STANDARD:
778 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000779 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk2cefd152004-02-08 22:55:38 +0000780 break;
781 default:
782 retval = 0;
783 }
wdenke65527f2004-02-12 00:47:09 +0000784 debug ("flash_is_busy: %d\n", retval);
wdenk2cefd152004-02-08 22:55:38 +0000785 return retval;
786}
wdenke65527f2004-02-12 00:47:09 +0000787
wdenk2cefd152004-02-08 22:55:38 +0000788/*-----------------------------------------------------------------------
789 * wait for XSR.7 to be set. Time out with an error if it does not.
790 * This routine does not set the flash to read-array mode.
791 */
wdenke65527f2004-02-12 00:47:09 +0000792static int flash_status_check (flash_info_t * info, flash_sect_t sector,
793 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000794{
795 ulong start;
796
Stefan Roeseefef95b2006-04-01 13:41:03 +0200797#if CFG_HZ != 1000
798 tout *= CFG_HZ/1000;
799#endif
800
wdenk2cefd152004-02-08 22:55:38 +0000801 /* Wait for command completion */
802 start = get_timer (0);
wdenke65527f2004-02-12 00:47:09 +0000803 while (flash_is_busy (info, sector)) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100804 if (get_timer (start) > tout) {
wdenke65527f2004-02-12 00:47:09 +0000805 printf ("Flash %s timeout at address %lx data %lx\n",
806 prompt, info->start[sector],
807 flash_read_long (info, sector, 0));
808 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000809 return ERR_TIMOUT;
810 }
Wolfgang Denkc9f8d7c2006-06-02 11:46:20 +0200811 udelay (1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000812 }
813 return ERR_OK;
814}
wdenke65527f2004-02-12 00:47:09 +0000815
wdenk2cefd152004-02-08 22:55:38 +0000816/*-----------------------------------------------------------------------
817 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
818 * This routine sets the flash to read-array mode.
819 */
wdenke65527f2004-02-12 00:47:09 +0000820static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
821 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000822{
823 int retcode;
wdenke65527f2004-02-12 00:47:09 +0000824
825 retcode = flash_status_check (info, sector, tout, prompt);
826 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000827 case CFI_CMDSET_INTEL_EXTENDED:
828 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesec865e6c2006-02-28 15:29:58 +0100829 if ((retcode == ERR_OK)
wdenke65527f2004-02-12 00:47:09 +0000830 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk2cefd152004-02-08 22:55:38 +0000831 retcode = ERR_INVAL;
wdenke65527f2004-02-12 00:47:09 +0000832 printf ("Flash %s error at address %lx\n", prompt,
833 info->start[sector]);
wdenke537b3b2004-02-23 23:54:43 +0000834 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000835 puts ("Command Sequence Error.\n");
wdenke537b3b2004-02-23 23:54:43 +0000836 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000837 puts ("Block Erase Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000838 retcode = ERR_NOT_ERASED;
wdenke537b3b2004-02-23 23:54:43 +0000839 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000840 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000841 }
wdenke65527f2004-02-12 00:47:09 +0000842 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk42c05472004-03-23 22:14:11 +0000843 puts ("Block locked.\n");
wdenke65527f2004-02-12 00:47:09 +0000844 retcode = ERR_PROTECTED;
845 }
846 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk42c05472004-03-23 22:14:11 +0000847 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000848 }
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200849 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000850 break;
851 default:
852 break;
853 }
854 return retcode;
855}
wdenke65527f2004-02-12 00:47:09 +0000856
wdenk2cefd152004-02-08 22:55:38 +0000857/*-----------------------------------------------------------------------
858 */
wdenke65527f2004-02-12 00:47:09 +0000859static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000860{
Heiko Schocher800db312007-01-19 18:05:26 +0100861#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk369d43d2004-03-14 14:09:05 +0000862 unsigned short w;
863 unsigned int l;
864 unsigned long long ll;
865#endif
866
wdenke65527f2004-02-12 00:47:09 +0000867 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000868 case FLASH_CFI_8BIT:
869 cword->c = c;
870 break;
871 case FLASH_CFI_16BIT:
Heiko Schocher800db312007-01-19 18:05:26 +0100872#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk369d43d2004-03-14 14:09:05 +0000873 w = c;
874 w <<= 8;
875 cword->w = (cword->w >> 8) | w;
876#else
wdenk2cefd152004-02-08 22:55:38 +0000877 cword->w = (cword->w << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000878#endif
wdenk2cefd152004-02-08 22:55:38 +0000879 break;
880 case FLASH_CFI_32BIT:
Heiko Schocher800db312007-01-19 18:05:26 +0100881#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk369d43d2004-03-14 14:09:05 +0000882 l = c;
883 l <<= 24;
884 cword->l = (cword->l >> 8) | l;
885#else
wdenk2cefd152004-02-08 22:55:38 +0000886 cword->l = (cword->l << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000887#endif
wdenk2cefd152004-02-08 22:55:38 +0000888 break;
889 case FLASH_CFI_64BIT:
Heiko Schocher800db312007-01-19 18:05:26 +0100890#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk369d43d2004-03-14 14:09:05 +0000891 ll = c;
892 ll <<= 56;
893 cword->ll = (cword->ll >> 8) | ll;
894#else
wdenk2cefd152004-02-08 22:55:38 +0000895 cword->ll = (cword->ll << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000896#endif
wdenk2cefd152004-02-08 22:55:38 +0000897 break;
898 }
899}
900
901
902/*-----------------------------------------------------------------------
903 * make a proper sized command based on the port and chip widths
904 */
wdenke65527f2004-02-12 00:47:09 +0000905static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk2cefd152004-02-08 22:55:38 +0000906{
907 int i;
wdenke65527f2004-02-12 00:47:09 +0000908 uchar *cp = (uchar *) cmdbuf;
909
Heiko Schocher800db312007-01-19 18:05:26 +0100910#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
Wolfgang Denk07bdb022005-09-24 23:32:48 +0200911 for (i = info->portwidth; i > 0; i--)
912#else
913 for (i = 1; i <= info->portwidth; i++)
wdenke65527f2004-02-12 00:47:09 +0000914#endif
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200915 *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
wdenk2cefd152004-02-08 22:55:38 +0000916}
917
918/*
919 * Write a proper sized command to the correct address
920 */
wdenke537b3b2004-02-23 23:54:43 +0000921static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000922{
923
924 volatile cfiptr_t addr;
925 cfiword_t cword;
wdenke65527f2004-02-12 00:47:09 +0000926
927 addr.cp = flash_make_addr (info, sect, offset);
928 flash_make_cmd (info, cmd, &cword);
929 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000930 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000931 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
932 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk2cefd152004-02-08 22:55:38 +0000933 *addr.cp = cword.c;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100934#ifdef CONFIG_BLACKFIN
935 asm("ssync;");
936#endif
wdenk2cefd152004-02-08 22:55:38 +0000937 break;
938 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000939 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
940 cmd, cword.w,
wdenk2cefd152004-02-08 22:55:38 +0000941 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
942 *addr.wp = cword.w;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100943#ifdef CONFIG_BLACKFIN
944 asm("ssync;");
945#endif
wdenk2cefd152004-02-08 22:55:38 +0000946 break;
947 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000948 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
949 cmd, cword.l,
wdenk2cefd152004-02-08 22:55:38 +0000950 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
951 *addr.lp = cword.l;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100952#ifdef CONFIG_BLACKFIN
953 asm("ssync;");
954#endif
wdenk2cefd152004-02-08 22:55:38 +0000955 break;
956 case FLASH_CFI_64BIT:
957#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000958 {
wdenk2cefd152004-02-08 22:55:38 +0000959 char str[20];
wdenk6cfa84e2004-02-10 00:03:41 +0000960
wdenke65527f2004-02-12 00:47:09 +0000961 print_longlong (str, cword.ll);
962
963 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
964 addr.llp, cmd, str,
wdenk2cefd152004-02-08 22:55:38 +0000965 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
966 }
967#endif
968 *addr.llp = cword.ll;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100969#ifdef CONFIG_BLACKFIN
970 asm("ssync;");
971#endif
wdenk2cefd152004-02-08 22:55:38 +0000972 break;
973 }
974}
975
wdenke65527f2004-02-12 00:47:09 +0000976static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000977{
wdenked2ac4b2004-03-14 18:23:55 +0000978 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
979 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
wdenk2cefd152004-02-08 22:55:38 +0000980}
wdenke65527f2004-02-12 00:47:09 +0000981
wdenk2cefd152004-02-08 22:55:38 +0000982/*-----------------------------------------------------------------------
983 */
wdenke537b3b2004-02-23 23:54:43 +0000984static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000985{
986 cfiptr_t cptr;
987 cfiword_t cword;
988 int retval;
wdenke65527f2004-02-12 00:47:09 +0000989
990 cptr.cp = flash_make_addr (info, sect, offset);
991 flash_make_cmd (info, cmd, &cword);
wdenk2cefd152004-02-08 22:55:38 +0000992
wdenke65527f2004-02-12 00:47:09 +0000993 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
994 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000995 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000996 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk2cefd152004-02-08 22:55:38 +0000997 retval = (cptr.cp[0] == cword.c);
998 break;
999 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +00001000 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk2cefd152004-02-08 22:55:38 +00001001 retval = (cptr.wp[0] == cword.w);
1002 break;
1003 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +00001004 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk2cefd152004-02-08 22:55:38 +00001005 retval = (cptr.lp[0] == cword.l);
1006 break;
1007 case FLASH_CFI_64BIT:
wdenk6cfa84e2004-02-10 00:03:41 +00001008#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +00001009 {
wdenk2cefd152004-02-08 22:55:38 +00001010 char str1[20];
1011 char str2[20];
wdenke65527f2004-02-12 00:47:09 +00001012
1013 print_longlong (str1, cptr.llp[0]);
1014 print_longlong (str2, cword.ll);
1015 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +00001016 }
1017#endif
1018 retval = (cptr.llp[0] == cword.ll);
1019 break;
1020 default:
1021 retval = 0;
1022 break;
1023 }
1024 return retval;
1025}
wdenke65527f2004-02-12 00:47:09 +00001026
wdenk2cefd152004-02-08 22:55:38 +00001027/*-----------------------------------------------------------------------
1028 */
wdenke537b3b2004-02-23 23:54:43 +00001029static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001030{
1031 cfiptr_t cptr;
1032 cfiword_t cword;
1033 int retval;
wdenke65527f2004-02-12 00:47:09 +00001034
1035 cptr.cp = flash_make_addr (info, sect, offset);
1036 flash_make_cmd (info, cmd, &cword);
1037 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001038 case FLASH_CFI_8BIT:
1039 retval = ((cptr.cp[0] & cword.c) == cword.c);
1040 break;
1041 case FLASH_CFI_16BIT:
1042 retval = ((cptr.wp[0] & cword.w) == cword.w);
1043 break;
1044 case FLASH_CFI_32BIT:
1045 retval = ((cptr.lp[0] & cword.l) == cword.l);
1046 break;
1047 case FLASH_CFI_64BIT:
1048 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenke65527f2004-02-12 00:47:09 +00001049 break;
wdenk2cefd152004-02-08 22:55:38 +00001050 default:
1051 retval = 0;
1052 break;
1053 }
1054 return retval;
1055}
1056
1057/*-----------------------------------------------------------------------
1058 */
wdenke537b3b2004-02-23 23:54:43 +00001059static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001060{
1061 cfiptr_t cptr;
1062 cfiword_t cword;
1063 int retval;
wdenke65527f2004-02-12 00:47:09 +00001064
1065 cptr.cp = flash_make_addr (info, sect, offset);
1066 flash_make_cmd (info, cmd, &cword);
1067 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001068 case FLASH_CFI_8BIT:
1069 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
1070 break;
1071 case FLASH_CFI_16BIT:
1072 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
1073 break;
1074 case FLASH_CFI_32BIT:
1075 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
1076 break;
1077 case FLASH_CFI_64BIT:
wdenke65527f2004-02-12 00:47:09 +00001078 retval = ((cptr.llp[0] & cword.ll) !=
1079 (cptr.llp[0] & cword.ll));
wdenk2cefd152004-02-08 22:55:38 +00001080 break;
1081 default:
1082 retval = 0;
1083 break;
1084 }
1085 return retval;
1086}
1087
1088/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +01001089 * read jedec ids from device and set corresponding fields in info struct
1090 *
1091 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1092 *
1093*/
1094static void flash_read_jedec_ids (flash_info_t * info)
1095{
1096 info->manufacturer_id = 0;
1097 info->device_id = 0;
1098 info->device_id2 = 0;
1099
1100 switch (info->vendor) {
1101 case CFI_CMDSET_INTEL_STANDARD:
1102 case CFI_CMDSET_INTEL_EXTENDED:
1103 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1104 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1105 udelay(1000); /* some flash are slow to respond */
1106 info->manufacturer_id = flash_read_uchar (info,
1107 FLASH_OFFSET_MANUFACTURER_ID);
1108 info->device_id = flash_read_uchar (info,
1109 FLASH_OFFSET_DEVICE_ID);
1110 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1111 break;
1112 case CFI_CMDSET_AMD_STANDARD:
1113 case CFI_CMDSET_AMD_EXTENDED:
1114 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1115 flash_unlock_seq(info, 0);
1116 flash_write_cmd(info, 0, AMD_ADDR_START, FLASH_CMD_READ_ID);
1117 udelay(1000); /* some flash are slow to respond */
1118 info->manufacturer_id = flash_read_uchar (info,
1119 FLASH_OFFSET_MANUFACTURER_ID);
1120 info->device_id = flash_read_uchar (info,
1121 FLASH_OFFSET_DEVICE_ID);
1122 if (info->device_id == 0x7E) {
1123 /* AMD 3-byte (expanded) device ids */
1124 info->device_id2 = flash_read_uchar (info,
1125 FLASH_OFFSET_DEVICE_ID2);
1126 info->device_id2 <<= 8;
1127 info->device_id2 |= flash_read_uchar (info,
1128 FLASH_OFFSET_DEVICE_ID3);
1129 }
1130 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1131 break;
1132 default:
1133 break;
1134 }
1135}
1136
1137/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +00001138 * detect if flash is compatible with the Common Flash Interface (CFI)
1139 * http://www.jedec.org/download/search/jesd68.pdf
1140 *
1141*/
wdenke65527f2004-02-12 00:47:09 +00001142static int flash_detect_cfi (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001143{
Wolfgang Denkafa0dd02006-12-27 01:26:13 +01001144 int cfi_offset;
wdenke65527f2004-02-12 00:47:09 +00001145 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001146
Stefan Roesec865e6c2006-02-28 15:29:58 +01001147 for (info->portwidth = CFG_FLASH_CFI_WIDTH;
wdenke65527f2004-02-12 00:47:09 +00001148 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1149 for (info->chipwidth = FLASH_CFI_BY8;
1150 info->chipwidth <= info->portwidth;
1151 info->chipwidth <<= 1) {
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001152 flash_write_cmd (info, 0, 0, info->cmd_reset);
Wolfgang Denkafa0dd02006-12-27 01:26:13 +01001153 for (cfi_offset=0; cfi_offset < sizeof(flash_offset_cfi)/sizeof(uint); cfi_offset++) {
1154 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], FLASH_CMD_CFI);
1155 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1156 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1157 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1158 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
1159 info->cfi_offset=flash_offset_cfi[cfi_offset];
1160 debug ("device interface is %d\n",
1161 info->interface);
1162 debug ("found port %d chip %d ",
1163 info->portwidth, info->chipwidth);
1164 debug ("port %d bits chip %d bits\n",
1165 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1166 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1167 return 1;
1168 }
wdenk2cefd152004-02-08 22:55:38 +00001169 }
1170 }
1171 }
wdenke65527f2004-02-12 00:47:09 +00001172 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001173 return 0;
1174}
wdenke65527f2004-02-12 00:47:09 +00001175
wdenk2cefd152004-02-08 22:55:38 +00001176/*
1177 * The following code cannot be run from FLASH!
1178 *
1179 */
Marian Balakowicz513b4a12005-10-11 19:09:42 +02001180ulong flash_get_size (ulong base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001181{
wdenke65527f2004-02-12 00:47:09 +00001182 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00001183 int i, j;
1184 flash_sect_t sect_cnt;
1185 unsigned long sector;
1186 unsigned long tmp;
1187 int size_ratio;
1188 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00001189 int erase_region_size;
1190 int erase_region_count;
Stefan Roese12797482006-11-13 13:55:24 +01001191 int geometry_reversed = 0;
1192
1193 info->ext_addr = 0;
1194 info->cfi_version = 0;
Stefan Roeseefef95b2006-04-01 13:41:03 +02001195#ifdef CFG_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02001196 info->legacy_unlock = 0;
1197#endif
wdenk2cefd152004-02-08 22:55:38 +00001198
1199 info->start[0] = base;
1200
wdenke65527f2004-02-12 00:47:09 +00001201 if (flash_detect_cfi (info)) {
Stefan Roese12797482006-11-13 13:55:24 +01001202 info->vendor = flash_read_ushort (info, 0,
1203 FLASH_OFFSET_PRIMARY_VENDOR);
1204 flash_read_jedec_ids (info);
Stefano Babic5f172a82006-12-12 00:22:42 +01001205 flash_write_cmd (info, 0, info->cfi_offset, FLASH_CMD_CFI);
Stefan Roese12797482006-11-13 13:55:24 +01001206 num_erase_regions = flash_read_uchar (info,
1207 FLASH_OFFSET_NUM_ERASE_REGIONS);
1208 info->ext_addr = flash_read_ushort (info, 0,
1209 FLASH_OFFSET_EXT_QUERY_T_P_ADDR);
1210 if (info->ext_addr) {
1211 info->cfi_version = (ushort) flash_read_uchar (info,
1212 info->ext_addr + 3) << 8;
1213 info->cfi_version |= (ushort) flash_read_uchar (info,
1214 info->ext_addr + 4);
1215 }
wdenke65527f2004-02-12 00:47:09 +00001216#ifdef DEBUG
1217 flash_printqry (info, 0);
1218#endif
1219 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001220 case CFI_CMDSET_INTEL_STANDARD:
1221 case CFI_CMDSET_INTEL_EXTENDED:
1222 default:
1223 info->cmd_reset = FLASH_CMD_RESET;
Stefan Roeseefef95b2006-04-01 13:41:03 +02001224#ifdef CFG_FLASH_PROTECTION
1225 /* read legacy lock/unlock bit from intel flash */
Stefan Roese12797482006-11-13 13:55:24 +01001226 if (info->ext_addr) {
1227 info->legacy_unlock = flash_read_uchar (info,
1228 info->ext_addr + 5) & 0x08;
1229 }
Stefan Roeseefef95b2006-04-01 13:41:03 +02001230#endif
wdenk2cefd152004-02-08 22:55:38 +00001231 break;
1232 case CFI_CMDSET_AMD_STANDARD:
1233 case CFI_CMDSET_AMD_EXTENDED:
1234 info->cmd_reset = AMD_CMD_RESET;
Stefan Roese12797482006-11-13 13:55:24 +01001235 /* check if flash geometry needs reversal */
1236 if (num_erase_regions <= 1)
1237 break;
1238 /* reverse geometry if top boot part */
1239 if (info->cfi_version < 0x3131) {
1240 /* CFI < 1.1, try to guess from device id */
1241 if ((info->device_id & 0x80) != 0) {
1242 geometry_reversed = 1;
1243 }
1244 break;
1245 }
1246 /* CFI >= 1.1, deduct from top/bottom flag */
1247 /* note: ext_addr is valid since cfi_version > 0 */
1248 if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1249 geometry_reversed = 1;
1250 }
wdenk2cefd152004-02-08 22:55:38 +00001251 break;
1252 }
wdenk6cfa84e2004-02-10 00:03:41 +00001253
wdenke65527f2004-02-12 00:47:09 +00001254 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese12797482006-11-13 13:55:24 +01001255 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1256 debug ("device id is 0x%x\n", info->device_id);
1257 debug ("device id2 is 0x%x\n", info->device_id2);
1258 debug ("cfi version is 0x%04x\n", info->cfi_version);
1259
wdenk2cefd152004-02-08 22:55:38 +00001260 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00001261 /* if the chip is x8/x16 reduce the ratio by half */
1262 if ((info->interface == FLASH_CFI_X8X16)
1263 && (info->chipwidth == FLASH_CFI_BY8)) {
1264 size_ratio >>= 1;
1265 }
wdenke65527f2004-02-12 00:47:09 +00001266 debug ("size_ratio %d port %d bits chip %d bits\n",
1267 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1268 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1269 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00001270 sect_cnt = 0;
1271 sector = base;
wdenke65527f2004-02-12 00:47:09 +00001272 for (i = 0; i < num_erase_regions; i++) {
1273 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00001274 printf ("%d erase regions found, only %d used\n",
1275 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00001276 break;
1277 }
Stefan Roese12797482006-11-13 13:55:24 +01001278 if (geometry_reversed)
1279 tmp = flash_read_long (info, 0,
1280 FLASH_OFFSET_ERASE_REGIONS +
1281 (num_erase_regions - 1 - i) * 4);
1282 else
1283 tmp = flash_read_long (info, 0,
wdenke65527f2004-02-12 00:47:09 +00001284 FLASH_OFFSET_ERASE_REGIONS +
1285 i * 4);
1286 erase_region_size =
1287 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk2cefd152004-02-08 22:55:38 +00001288 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00001289 erase_region_count = (tmp & 0xffff) + 1;
wdenkaeba06f2004-06-09 17:34:58 +00001290 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00001291 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00001292 for (j = 0; j < erase_region_count; j++) {
wdenk2cefd152004-02-08 22:55:38 +00001293 info->start[sect_cnt] = sector;
1294 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00001295
1296 /*
1297 * Only read protection status from supported devices (intel...)
1298 */
1299 switch (info->vendor) {
1300 case CFI_CMDSET_INTEL_EXTENDED:
1301 case CFI_CMDSET_INTEL_STANDARD:
1302 info->protect[sect_cnt] =
1303 flash_isset (info, sect_cnt,
1304 FLASH_OFFSET_PROTECT,
1305 FLASH_STATUS_PROTECT);
1306 break;
1307 default:
1308 info->protect[sect_cnt] = 0; /* default: not protected */
1309 }
1310
wdenk2cefd152004-02-08 22:55:38 +00001311 sect_cnt++;
1312 }
1313 }
1314
1315 info->sector_count = sect_cnt;
1316 /* multiply the size by the number of chips */
wdenke537b3b2004-02-23 23:54:43 +00001317 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1318 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
wdenke65527f2004-02-12 00:47:09 +00001319 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenke537b3b2004-02-23 23:54:43 +00001320 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
Stefan Roeseefef95b2006-04-01 13:41:03 +02001321 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT)) *
1322 (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT));
1323 info->buffer_write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
Stefan Roesec865e6c2006-02-28 15:29:58 +01001324 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
1325 (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
1326 info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
wdenk2cefd152004-02-08 22:55:38 +00001327 info->flash_id = FLASH_MAN_CFI;
wdenked2ac4b2004-03-14 18:23:55 +00001328 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1329 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1330 }
wdenk2cefd152004-02-08 22:55:38 +00001331 }
1332
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001333 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenke65527f2004-02-12 00:47:09 +00001334 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00001335}
1336
Stefan Roesec865e6c2006-02-28 15:29:58 +01001337/* loop through the sectors from the highest address
1338 * when the passed address is greater or equal to the sector address
1339 * we have a match
1340 */
1341static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1342{
1343 flash_sect_t sector;
1344
1345 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1346 if (addr >= info->start[sector])
1347 break;
1348 }
1349 return sector;
1350}
wdenk2cefd152004-02-08 22:55:38 +00001351
1352/*-----------------------------------------------------------------------
1353 */
wdenke65527f2004-02-12 00:47:09 +00001354static int flash_write_cfiword (flash_info_t * info, ulong dest,
1355 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +00001356{
wdenk2cefd152004-02-08 22:55:38 +00001357 cfiptr_t ctladdr;
1358 cfiptr_t cptr;
1359 int flag;
1360
wdenke65527f2004-02-12 00:47:09 +00001361 ctladdr.cp = flash_make_addr (info, 0, 0);
1362 cptr.cp = (uchar *) dest;
wdenk2cefd152004-02-08 22:55:38 +00001363
wdenk2cefd152004-02-08 22:55:38 +00001364 /* Check if Flash is (sufficiently) erased */
wdenke65527f2004-02-12 00:47:09 +00001365 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001366 case FLASH_CFI_8BIT:
1367 flag = ((cptr.cp[0] & cword.c) == cword.c);
1368 break;
1369 case FLASH_CFI_16BIT:
1370 flag = ((cptr.wp[0] & cword.w) == cword.w);
1371 break;
1372 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +00001373 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk2cefd152004-02-08 22:55:38 +00001374 break;
1375 case FLASH_CFI_64BIT:
wdenk391b5742004-10-10 23:27:33 +00001376 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenk2cefd152004-02-08 22:55:38 +00001377 break;
1378 default:
1379 return 2;
1380 }
wdenke65527f2004-02-12 00:47:09 +00001381 if (!flag)
wdenk2cefd152004-02-08 22:55:38 +00001382 return 2;
1383
1384 /* Disable interrupts which might cause a timeout here */
wdenke65527f2004-02-12 00:47:09 +00001385 flag = disable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001386
wdenke65527f2004-02-12 00:47:09 +00001387 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001388 case CFI_CMDSET_INTEL_EXTENDED:
1389 case CFI_CMDSET_INTEL_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001390 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1391 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001392 break;
1393 case CFI_CMDSET_AMD_EXTENDED:
1394 case CFI_CMDSET_AMD_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001395 flash_unlock_seq (info, 0);
wdenked2ac4b2004-03-14 18:23:55 +00001396 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001397 break;
1398 }
1399
wdenke65527f2004-02-12 00:47:09 +00001400 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001401 case FLASH_CFI_8BIT:
1402 cptr.cp[0] = cword.c;
1403 break;
1404 case FLASH_CFI_16BIT:
1405 cptr.wp[0] = cword.w;
1406 break;
1407 case FLASH_CFI_32BIT:
1408 cptr.lp[0] = cword.l;
1409 break;
1410 case FLASH_CFI_64BIT:
1411 cptr.llp[0] = cword.ll;
1412 break;
1413 }
1414
1415 /* re-enable interrupts if necessary */
wdenke65527f2004-02-12 00:47:09 +00001416 if (flag)
1417 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001418
Stefan Roesec865e6c2006-02-28 15:29:58 +01001419 return flash_full_status_check (info, find_sector (info, dest),
1420 info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +00001421}
1422
1423#ifdef CFG_FLASH_USE_BUFFER_WRITE
1424
wdenke65527f2004-02-12 00:47:09 +00001425static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1426 int len)
wdenk2cefd152004-02-08 22:55:38 +00001427{
1428 flash_sect_t sector;
1429 int cnt;
1430 int retcode;
1431 volatile cfiptr_t src;
1432 volatile cfiptr_t dst;
1433
Stefan Roesec865e6c2006-02-28 15:29:58 +01001434 switch (info->vendor) {
1435 case CFI_CMDSET_INTEL_STANDARD:
1436 case CFI_CMDSET_INTEL_EXTENDED:
1437 src.cp = cp;
1438 dst.cp = (uchar *) dest;
1439 sector = find_sector (info, dest);
1440 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1441 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1442 if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
1443 "write to buffer")) == ERR_OK) {
1444 /* reduce the number of loops by the width of the port */
1445 switch (info->portwidth) {
1446 case FLASH_CFI_8BIT:
1447 cnt = len;
1448 break;
1449 case FLASH_CFI_16BIT:
1450 cnt = len >> 1;
1451 break;
1452 case FLASH_CFI_32BIT:
1453 cnt = len >> 2;
1454 break;
1455 case FLASH_CFI_64BIT:
1456 cnt = len >> 3;
1457 break;
1458 default:
1459 return ERR_INVAL;
1460 break;
1461 }
1462 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1463 while (cnt-- > 0) {
1464 switch (info->portwidth) {
1465 case FLASH_CFI_8BIT:
1466 *dst.cp++ = *src.cp++;
1467 break;
1468 case FLASH_CFI_16BIT:
1469 *dst.wp++ = *src.wp++;
1470 break;
1471 case FLASH_CFI_32BIT:
1472 *dst.lp++ = *src.lp++;
1473 break;
1474 case FLASH_CFI_64BIT:
1475 *dst.llp++ = *src.llp++;
1476 break;
1477 default:
1478 return ERR_INVAL;
1479 break;
1480 }
1481 }
1482 flash_write_cmd (info, sector, 0,
1483 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1484 retcode = flash_full_status_check (info, sector,
1485 info->buffer_write_tout,
1486 "buffer write");
1487 }
1488 return retcode;
1489
1490 case CFI_CMDSET_AMD_STANDARD:
1491 case CFI_CMDSET_AMD_EXTENDED:
1492 src.cp = cp;
1493 dst.cp = (uchar *) dest;
1494 sector = find_sector (info, dest);
1495
1496 flash_unlock_seq(info,0);
1497 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
1498
wdenke65527f2004-02-12 00:47:09 +00001499 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001500 case FLASH_CFI_8BIT:
1501 cnt = len;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001502 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1503 while (cnt-- > 0) *dst.cp++ = *src.cp++;
wdenk2cefd152004-02-08 22:55:38 +00001504 break;
1505 case FLASH_CFI_16BIT:
1506 cnt = len >> 1;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001507 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1508 while (cnt-- > 0) *dst.wp++ = *src.wp++;
wdenk2cefd152004-02-08 22:55:38 +00001509 break;
1510 case FLASH_CFI_32BIT:
1511 cnt = len >> 2;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001512 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1513 while (cnt-- > 0) *dst.lp++ = *src.lp++;
wdenk2cefd152004-02-08 22:55:38 +00001514 break;
1515 case FLASH_CFI_64BIT:
1516 cnt = len >> 3;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001517 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1518 while (cnt-- > 0) *dst.llp++ = *src.llp++;
wdenk2cefd152004-02-08 22:55:38 +00001519 break;
1520 default:
1521 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001522 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01001523
1524 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1525 retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
1526 "buffer write");
1527 return retcode;
1528
1529 default:
1530 debug ("Unknown Command Set\n");
1531 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001532 }
wdenk2cefd152004-02-08 22:55:38 +00001533}
wdenk5b835a32004-09-28 19:00:19 +00001534#endif /* CFG_FLASH_USE_BUFFER_WRITE */
Heiko Schocher3c58a992007-01-11 15:44:44 +01001535
wdenk2cefd152004-02-08 22:55:38 +00001536#endif /* CFG_FLASH_CFI */