blob: 2699cce859af61d6d94e1446e3956c051e3561e3 [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 *
wdenk2cefd152004-02-08 22:55:38 +000057 */
58
wdenke65527f2004-02-12 00:47:09 +000059#ifndef CFG_FLASH_BANKS_LIST
60#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
61#endif
62
wdenk2cefd152004-02-08 22:55:38 +000063#define FLASH_CMD_CFI 0x98
64#define FLASH_CMD_READ_ID 0x90
65#define FLASH_CMD_RESET 0xff
66#define FLASH_CMD_BLOCK_ERASE 0x20
67#define FLASH_CMD_ERASE_CONFIRM 0xD0
68#define FLASH_CMD_WRITE 0x40
69#define FLASH_CMD_PROTECT 0x60
70#define FLASH_CMD_PROTECT_SET 0x01
71#define FLASH_CMD_PROTECT_CLEAR 0xD0
72#define FLASH_CMD_CLEAR_STATUS 0x50
wdenke65527f2004-02-12 00:47:09 +000073#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
74#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk2cefd152004-02-08 22:55:38 +000075
76#define FLASH_STATUS_DONE 0x80
77#define FLASH_STATUS_ESS 0x40
78#define FLASH_STATUS_ECLBS 0x20
79#define FLASH_STATUS_PSLBS 0x10
80#define FLASH_STATUS_VPENS 0x08
81#define FLASH_STATUS_PSS 0x04
82#define FLASH_STATUS_DPS 0x02
83#define FLASH_STATUS_R 0x01
84#define FLASH_STATUS_PROTECT 0x01
85
86#define AMD_CMD_RESET 0xF0
87#define AMD_CMD_WRITE 0xA0
88#define AMD_CMD_ERASE_START 0x80
89#define AMD_CMD_ERASE_SECTOR 0x30
wdenked2ac4b2004-03-14 18:23:55 +000090#define AMD_CMD_UNLOCK_START 0xAA
91#define AMD_CMD_UNLOCK_ACK 0x55
Stefan Roesec865e6c2006-02-28 15:29:58 +010092#define AMD_CMD_WRITE_TO_BUFFER 0x25
93#define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
wdenk2cefd152004-02-08 22:55:38 +000094
95#define AMD_STATUS_TOGGLE 0x40
96#define AMD_STATUS_ERROR 0x20
Stefan Roesec865e6c2006-02-28 15:29:58 +010097
98#define AMD_ADDR_ERASE_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
99#define AMD_ADDR_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
100#define AMD_ADDR_ACK ((info->portwidth == FLASH_CFI_8BIT) ? 0x555 : 0x2AA)
wdenk2cefd152004-02-08 22:55:38 +0000101
Stefan Roese12797482006-11-13 13:55:24 +0100102#define FLASH_OFFSET_MANUFACTURER_ID 0x00
103#define FLASH_OFFSET_DEVICE_ID 0x01
104#define FLASH_OFFSET_DEVICE_ID2 0x0E
105#define FLASH_OFFSET_DEVICE_ID3 0x0F
wdenk2cefd152004-02-08 22:55:38 +0000106#define FLASH_OFFSET_CFI 0x55
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100107#define FLASH_OFFSET_CFI_ALT 0x555
wdenk2cefd152004-02-08 22:55:38 +0000108#define FLASH_OFFSET_CFI_RESP 0x10
wdenke65527f2004-02-12 00:47:09 +0000109#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
Stefan Roeseefef95b2006-04-01 13:41:03 +0200110#define FLASH_OFFSET_EXT_QUERY_T_P_ADDR 0x15 /* extended query table primary addr */
wdenk2cefd152004-02-08 22:55:38 +0000111#define FLASH_OFFSET_WTOUT 0x1F
wdenke65527f2004-02-12 00:47:09 +0000112#define FLASH_OFFSET_WBTOUT 0x20
wdenk2cefd152004-02-08 22:55:38 +0000113#define FLASH_OFFSET_ETOUT 0x21
wdenke65527f2004-02-12 00:47:09 +0000114#define FLASH_OFFSET_CETOUT 0x22
wdenk2cefd152004-02-08 22:55:38 +0000115#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenke65527f2004-02-12 00:47:09 +0000116#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk2cefd152004-02-08 22:55:38 +0000117#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenke65527f2004-02-12 00:47:09 +0000118#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk2cefd152004-02-08 22:55:38 +0000119#define FLASH_OFFSET_SIZE 0x27
wdenke65527f2004-02-12 00:47:09 +0000120#define FLASH_OFFSET_INTERFACE 0x28
121#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk2cefd152004-02-08 22:55:38 +0000122#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
123#define FLASH_OFFSET_ERASE_REGIONS 0x2D
124#define FLASH_OFFSET_PROTECT 0x02
wdenke65527f2004-02-12 00:47:09 +0000125#define FLASH_OFFSET_USER_PROTECTION 0x85
126#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk2cefd152004-02-08 22:55:38 +0000127
Stefan Roese12797482006-11-13 13:55:24 +0100128#define CFI_CMDSET_NONE 0
129#define CFI_CMDSET_INTEL_EXTENDED 1
130#define CFI_CMDSET_AMD_STANDARD 2
131#define CFI_CMDSET_INTEL_STANDARD 3
132#define CFI_CMDSET_AMD_EXTENDED 4
133#define CFI_CMDSET_MITSU_STANDARD 256
134#define CFI_CMDSET_MITSU_EXTENDED 257
135#define CFI_CMDSET_SST 258
wdenk2cefd152004-02-08 22:55:38 +0000136
wdenk51242782004-12-18 22:35:43 +0000137#ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
138# undef FLASH_CMD_RESET
Stefan Roese12797482006-11-13 13:55:24 +0100139# define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
wdenk51242782004-12-18 22:35:43 +0000140#endif
141
wdenk2cefd152004-02-08 22:55:38 +0000142typedef union {
143 unsigned char c;
144 unsigned short w;
145 unsigned long l;
146 unsigned long long ll;
147} cfiword_t;
148
149typedef union {
wdenke65527f2004-02-12 00:47:09 +0000150 volatile unsigned char *cp;
wdenk2cefd152004-02-08 22:55:38 +0000151 volatile unsigned short *wp;
wdenke65527f2004-02-12 00:47:09 +0000152 volatile unsigned long *lp;
wdenk2cefd152004-02-08 22:55:38 +0000153 volatile unsigned long long *llp;
154} cfiptr_t;
155
Stefan Roese12797482006-11-13 13:55:24 +0100156#define NUM_ERASE_REGIONS 4 /* max. number of erase regions */
wdenk2cefd152004-02-08 22:55:38 +0000157
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100158static uint flash_offset_cfi[2]={FLASH_OFFSET_CFI,FLASH_OFFSET_CFI_ALT};
159
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200160/* use CFG_MAX_FLASH_BANKS_DETECT if defined */
161#ifdef CFG_MAX_FLASH_BANKS_DETECT
162static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
163flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
164#else
wdenk2cefd152004-02-08 22:55:38 +0000165static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200166flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
167#endif
wdenk2cefd152004-02-08 22:55:38 +0000168
Stefan Roesec865e6c2006-02-28 15:29:58 +0100169/*
170 * Check if chip width is defined. If not, start detecting with 8bit.
171 */
172#ifndef CFG_FLASH_CFI_WIDTH
173#define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
174#endif
175
wdenk2cefd152004-02-08 22:55:38 +0000176
177/*-----------------------------------------------------------------------
178 * Functions
179 */
180
181typedef unsigned long flash_sect_t;
182
wdenke65527f2004-02-12 00:47:09 +0000183static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
184static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenke537b3b2004-02-23 23:54:43 +0000185static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000186static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenke537b3b2004-02-23 23:54:43 +0000187static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
188static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
189static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
Stefan Roese12797482006-11-13 13:55:24 +0100190static void flash_read_jedec_ids (flash_info_t * info);
wdenke65527f2004-02-12 00:47:09 +0000191static int flash_detect_cfi (flash_info_t * info);
wdenke537b3b2004-02-23 23:54:43 +0000192static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenke65527f2004-02-12 00:47:09 +0000193static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
194 ulong tout, char *prompt);
Stefan Roese896391f2006-03-01 17:00:49 +0100195ulong flash_get_size (ulong base, int banknum);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200196#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000197static flash_info_t *flash_get_info(ulong base);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200198#endif
wdenk2cefd152004-02-08 22:55:38 +0000199#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke537b3b2004-02-23 23:54:43 +0000200static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk2cefd152004-02-08 22:55:38 +0000201#endif
202
wdenke65527f2004-02-12 00:47:09 +0000203/*-----------------------------------------------------------------------
204 * create an address based on the offset and the port width
205 */
wdenke537b3b2004-02-23 23:54:43 +0000206inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000207{
208 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
209}
210
wdenk2cefd152004-02-08 22:55:38 +0000211#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000212/*-----------------------------------------------------------------------
213 * Debug support
214 */
215void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000216{
217 int i;
218 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000219
220 cp = (unsigned char *) &data;
221 for (i = 0; i < 8; i++)
222 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000223}
wdenke65527f2004-02-12 00:47:09 +0000224static void flash_printqry (flash_info_t * info, flash_sect_t sect)
225{
226 cfiptr_t cptr;
227 int x, y;
228
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200229 for (x = 0; x < 0x40; x += 16U / info->portwidth) {
wdenke65527f2004-02-12 00:47:09 +0000230 cptr.cp =
231 flash_make_addr (info, sect,
232 x + FLASH_OFFSET_CFI_RESP);
233 debug ("%p : ", cptr.cp);
234 for (y = 0; y < 16; y++) {
235 debug ("%2.2x ", cptr.cp[y]);
236 }
237 debug (" ");
238 for (y = 0; y < 16; y++) {
239 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
240 debug ("%c", cptr.cp[y]);
241 } else {
242 debug (".");
243 }
244 }
245 debug ("\n");
246 }
247}
wdenk2cefd152004-02-08 22:55:38 +0000248#endif
249
250
251/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000252 * read a character at a port width address
253 */
wdenke65527f2004-02-12 00:47:09 +0000254inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000255{
256 uchar *cp;
wdenke65527f2004-02-12 00:47:09 +0000257
258 cp = flash_make_addr (info, 0, offset);
259#if defined(__LITTLE_ENDIAN)
260 return (cp[0]);
261#else
wdenk2cefd152004-02-08 22:55:38 +0000262 return (cp[info->portwidth - 1]);
wdenke65527f2004-02-12 00:47:09 +0000263#endif
wdenk2cefd152004-02-08 22:55:38 +0000264}
265
266/*-----------------------------------------------------------------------
267 * read a short word by swapping for ppc format.
268 */
wdenke65527f2004-02-12 00:47:09 +0000269ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000270{
wdenke65527f2004-02-12 00:47:09 +0000271 uchar *addr;
272 ushort retval;
wdenk2cefd152004-02-08 22:55:38 +0000273
wdenke65527f2004-02-12 00:47:09 +0000274#ifdef DEBUG
275 int x;
276#endif
277 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000278
wdenke65527f2004-02-12 00:47:09 +0000279#ifdef DEBUG
280 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
281 info->portwidth);
282 for (x = 0; x < 2 * info->portwidth; x++) {
283 debug ("addr[%x] = 0x%x\n", x, addr[x]);
284 }
285#endif
286#if defined(__LITTLE_ENDIAN)
287 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
288#else
289 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
290 addr[info->portwidth - 1]);
291#endif
292
293 debug ("retval = 0x%x\n", retval);
294 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000295}
296
297/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +0100298 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000299 * port size word. Swap for ppc format.
300 */
wdenke65527f2004-02-12 00:47:09 +0000301ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000302{
wdenke65527f2004-02-12 00:47:09 +0000303 uchar *addr;
304 ulong retval;
305
306#ifdef DEBUG
307 int x;
308#endif
309 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000310
wdenke65527f2004-02-12 00:47:09 +0000311#ifdef DEBUG
312 debug ("long addr is at %p info->portwidth = %d\n", addr,
313 info->portwidth);
314 for (x = 0; x < 4 * info->portwidth; x++) {
315 debug ("addr[%x] = 0x%x\n", x, addr[x]);
316 }
317#endif
318#if defined(__LITTLE_ENDIAN)
319 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenke537b3b2004-02-23 23:54:43 +0000320 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenke65527f2004-02-12 00:47:09 +0000321#else
322 retval = (addr[(2 * info->portwidth) - 1] << 24) |
323 (addr[(info->portwidth) - 1] << 16) |
324 (addr[(4 * info->portwidth) - 1] << 8) |
325 addr[(3 * info->portwidth) - 1];
326#endif
327 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000328}
329
Stefan Roesec865e6c2006-02-28 15:29:58 +0100330
wdenk2cefd152004-02-08 22:55:38 +0000331/*-----------------------------------------------------------------------
332 */
333unsigned long flash_init (void)
334{
335 unsigned long size = 0;
336 int i;
337
Stefan Roeseefef95b2006-04-01 13:41:03 +0200338#ifdef CFG_FLASH_PROTECTION
339 char *s = getenv("unlock");
340#endif
341
wdenk2cefd152004-02-08 22:55:38 +0000342 /* Init: no FLASHes known */
wdenke65527f2004-02-12 00:47:09 +0000343 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000344 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenke65527f2004-02-12 00:47:09 +0000345 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk2cefd152004-02-08 22:55:38 +0000346 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Stefan Roesec443fe92005-11-22 13:20:42 +0100347#ifndef CFG_FLASH_QUIET_TEST
wdenke537b3b2004-02-23 23:54:43 +0000348 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100349 i+1, flash_info[i].size, flash_info[i].size << 20);
Stefan Roesec443fe92005-11-22 13:20:42 +0100350#endif /* CFG_FLASH_QUIET_TEST */
wdenk2cefd152004-02-08 22:55:38 +0000351 }
Stefan Roesec865e6c2006-02-28 15:29:58 +0100352#ifdef CFG_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +0200353 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
354 /*
355 * Only the U-Boot image and it's environment is protected,
356 * all other sectors are unprotected (unlocked) if flash
357 * hardware protection is used (CFG_FLASH_PROTECTION) and
358 * the environment variable "unlock" is set to "yes".
359 */
360 if (flash_info[i].legacy_unlock) {
361 int k;
Stefan Roesec865e6c2006-02-28 15:29:58 +0100362
Stefan Roeseefef95b2006-04-01 13:41:03 +0200363 /*
364 * Disable legacy_unlock temporarily, since
365 * flash_real_protect would relock all other sectors
366 * again otherwise.
367 */
368 flash_info[i].legacy_unlock = 0;
369
370 /*
371 * Legacy unlocking (e.g. Intel J3) -> unlock only one
372 * sector. This will unlock all sectors.
373 */
374 flash_real_protect (&flash_info[i], 0, 0);
375
376 flash_info[i].legacy_unlock = 1;
377
378 /*
379 * Manually mark other sectors as unlocked (unprotected)
380 */
381 for (k = 1; k < flash_info[i].sector_count; k++)
382 flash_info[i].protect[k] = 0;
383 } else {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100384 /*
Stefan Roeseefef95b2006-04-01 13:41:03 +0200385 * No legancy unlocking -> unlock all sectors
Stefan Roesec865e6c2006-02-28 15:29:58 +0100386 */
387 flash_protect (FLAG_PROTECT_CLEAR,
388 flash_info[i].start[0],
389 flash_info[i].start[0] + flash_info[i].size - 1,
390 &flash_info[i]);
391 }
392 }
393#endif /* CFG_FLASH_PROTECTION */
wdenk2cefd152004-02-08 22:55:38 +0000394 }
395
396 /* Monitor protection ON by default */
397#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenke65527f2004-02-12 00:47:09 +0000398 flash_protect (FLAG_PROTECT_SET,
399 CFG_MONITOR_BASE,
wdenk5c71a7a2005-05-16 15:23:22 +0000400 CFG_MONITOR_BASE + monitor_flash_len - 1,
401 flash_get_info(CFG_MONITOR_BASE));
wdenk2cefd152004-02-08 22:55:38 +0000402#endif
403
wdenke85b7a52004-10-10 22:16:06 +0000404 /* Environment protection ON by default */
405#ifdef CFG_ENV_IS_IN_FLASH
406 flash_protect (FLAG_PROTECT_SET,
407 CFG_ENV_ADDR,
408 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000409 flash_get_info(CFG_ENV_ADDR));
wdenke85b7a52004-10-10 22:16:06 +0000410#endif
411
412 /* Redundant environment protection ON by default */
413#ifdef CFG_ENV_ADDR_REDUND
414 flash_protect (FLAG_PROTECT_SET,
415 CFG_ENV_ADDR_REDUND,
416 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000417 flash_get_info(CFG_ENV_ADDR_REDUND));
wdenke85b7a52004-10-10 22:16:06 +0000418#endif
wdenk2cefd152004-02-08 22:55:38 +0000419 return (size);
420}
421
422/*-----------------------------------------------------------------------
423 */
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200424#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000425static flash_info_t *flash_get_info(ulong base)
426{
427 int i;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200428 flash_info_t * info = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000429
430 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
431 info = & flash_info[i];
432 if (info->size && info->start[0] <= base &&
433 base <= info->start[0] + info->size - 1)
434 break;
435 }
436
437 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
438}
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200439#endif
wdenk5c71a7a2005-05-16 15:23:22 +0000440
441/*-----------------------------------------------------------------------
442 */
wdenke65527f2004-02-12 00:47:09 +0000443int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +0000444{
445 int rcode = 0;
446 int prot;
447 flash_sect_t sect;
448
wdenke65527f2004-02-12 00:47:09 +0000449 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000450 puts ("Can't erase unknown flash type - aborted\n");
wdenk2cefd152004-02-08 22:55:38 +0000451 return 1;
452 }
453 if ((s_first < 0) || (s_first > s_last)) {
wdenk42c05472004-03-23 22:14:11 +0000454 puts ("- no sectors to erase\n");
wdenk2cefd152004-02-08 22:55:38 +0000455 return 1;
456 }
457
458 prot = 0;
wdenke65527f2004-02-12 00:47:09 +0000459 for (sect = s_first; sect <= s_last; ++sect) {
wdenk2cefd152004-02-08 22:55:38 +0000460 if (info->protect[sect]) {
461 prot++;
462 }
463 }
464 if (prot) {
wdenke65527f2004-02-12 00:47:09 +0000465 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk2cefd152004-02-08 22:55:38 +0000466 } else {
wdenk42c05472004-03-23 22:14:11 +0000467 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000468 }
469
470
wdenke65527f2004-02-12 00:47:09 +0000471 for (sect = s_first; sect <= s_last; sect++) {
wdenk2cefd152004-02-08 22:55:38 +0000472 if (info->protect[sect] == 0) { /* not protected */
wdenke65527f2004-02-12 00:47:09 +0000473 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000474 case CFI_CMDSET_INTEL_STANDARD:
475 case CFI_CMDSET_INTEL_EXTENDED:
wdenke537b3b2004-02-23 23:54:43 +0000476 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
477 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
478 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk2cefd152004-02-08 22:55:38 +0000479 break;
480 case CFI_CMDSET_AMD_STANDARD:
481 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000482 flash_unlock_seq (info, sect);
wdenked2ac4b2004-03-14 18:23:55 +0000483 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
484 AMD_CMD_ERASE_START);
wdenke65527f2004-02-12 00:47:09 +0000485 flash_unlock_seq (info, sect);
wdenke537b3b2004-02-23 23:54:43 +0000486 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk2cefd152004-02-08 22:55:38 +0000487 break;
488 default:
wdenke65527f2004-02-12 00:47:09 +0000489 debug ("Unkown flash vendor %d\n",
490 info->vendor);
wdenk2cefd152004-02-08 22:55:38 +0000491 break;
492 }
493
wdenke65527f2004-02-12 00:47:09 +0000494 if (flash_full_status_check
495 (info, sect, info->erase_blk_tout, "erase")) {
wdenk2cefd152004-02-08 22:55:38 +0000496 rcode = 1;
497 } else
wdenk42c05472004-03-23 22:14:11 +0000498 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +0000499 }
500 }
wdenk42c05472004-03-23 22:14:11 +0000501 puts (" done\n");
wdenk2cefd152004-02-08 22:55:38 +0000502 return rcode;
503}
504
505/*-----------------------------------------------------------------------
506 */
wdenke65527f2004-02-12 00:47:09 +0000507void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +0000508{
509 int i;
510
511 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000512 puts ("missing or unknown FLASH type\n");
wdenk2cefd152004-02-08 22:55:38 +0000513 return;
514 }
515
wdenke65527f2004-02-12 00:47:09 +0000516 printf ("CFI conformant FLASH (%d x %d)",
517 (info->portwidth << 3), (info->chipwidth << 3));
wdenk2cefd152004-02-08 22:55:38 +0000518 printf (" Size: %ld MB in %d Sectors\n",
519 info->size >> 20, info->sector_count);
Stefan Roese12797482006-11-13 13:55:24 +0100520 printf (" ");
521 switch (info->vendor) {
522 case CFI_CMDSET_INTEL_STANDARD:
523 printf ("Intel Standard");
524 break;
525 case CFI_CMDSET_INTEL_EXTENDED:
526 printf ("Intel Extended");
527 break;
528 case CFI_CMDSET_AMD_STANDARD:
529 printf ("AMD Standard");
530 break;
531 case CFI_CMDSET_AMD_EXTENDED:
532 printf ("AMD Extended");
533 break;
534 default:
535 printf ("Unknown (%d)", info->vendor);
536 break;
537 }
538 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
539 info->manufacturer_id, info->device_id);
540 if (info->device_id == 0x7E) {
541 printf("%04X", info->device_id2);
542 }
543 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
wdenke537b3b2004-02-23 23:54:43 +0000544 info->erase_blk_tout,
Stefan Roese12797482006-11-13 13:55:24 +0100545 info->write_tout);
546 if (info->buffer_size > 1) {
547 printf (" Buffer write timeout: %ld ms, buffer size: %d bytes\n",
wdenke537b3b2004-02-23 23:54:43 +0000548 info->buffer_write_tout,
549 info->buffer_size);
Stefan Roese12797482006-11-13 13:55:24 +0100550 }
wdenk2cefd152004-02-08 22:55:38 +0000551
Stefan Roese12797482006-11-13 13:55:24 +0100552 puts ("\n Sector Start Addresses:");
wdenke65527f2004-02-12 00:47:09 +0000553 for (i = 0; i < info->sector_count; ++i) {
Stefan Roese12797482006-11-13 13:55:24 +0100554 if ((i % 5) == 0)
555 printf ("\n");
wdenk2cefd152004-02-08 22:55:38 +0000556#ifdef CFG_FLASH_EMPTY_INFO
557 int k;
558 int size;
559 int erased;
560 volatile unsigned long *flash;
561
562 /*
563 * Check if whole sector is erased
564 */
wdenke65527f2004-02-12 00:47:09 +0000565 if (i != (info->sector_count - 1))
566 size = info->start[i + 1] - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000567 else
wdenke65527f2004-02-12 00:47:09 +0000568 size = info->start[0] + info->size - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000569 erased = 1;
wdenke65527f2004-02-12 00:47:09 +0000570 flash = (volatile unsigned long *) info->start[i];
571 size = size >> 2; /* divide by 4 for longword access */
572 for (k = 0; k < size; k++) {
573 if (*flash++ != 0xffffffff) {
574 erased = 0;
575 break;
576 }
577 }
wdenk2cefd152004-02-08 22:55:38 +0000578
wdenk2cefd152004-02-08 22:55:38 +0000579 /* print empty and read-only info */
Stefan Roese12797482006-11-13 13:55:24 +0100580 printf (" %08lX %c %s ",
wdenk2cefd152004-02-08 22:55:38 +0000581 info->start[i],
Stefan Roese12797482006-11-13 13:55:24 +0100582 erased ? 'E' : ' ',
583 info->protect[i] ? "RO" : " ");
Wolfgang Denkd3abe5a2005-09-25 00:23:05 +0200584#else /* ! CFG_FLASH_EMPTY_INFO */
Stefan Roese12797482006-11-13 13:55:24 +0100585 printf (" %08lX %s ",
586 info->start[i],
587 info->protect[i] ? "RO" : " ");
wdenk2cefd152004-02-08 22:55:38 +0000588#endif
589 }
wdenk42c05472004-03-23 22:14:11 +0000590 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000591 return;
592}
593
594/*-----------------------------------------------------------------------
595 * Copy memory to flash, returns:
596 * 0 - OK
597 * 1 - write timeout
598 * 2 - Flash not erased
599 */
wdenke65527f2004-02-12 00:47:09 +0000600int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +0000601{
602 ulong wp;
603 ulong cp;
604 int aln;
605 cfiword_t cword;
606 int i, rc;
607
wdenke65527f2004-02-12 00:47:09 +0000608#ifdef CFG_FLASH_USE_BUFFER_WRITE
609 int buffered_size;
610#endif
wdenke65527f2004-02-12 00:47:09 +0000611 /* get lower aligned address */
wdenk2cefd152004-02-08 22:55:38 +0000612 /* get lower aligned address */
613 wp = (addr & ~(info->portwidth - 1));
614
615 /* handle unaligned start */
wdenke65527f2004-02-12 00:47:09 +0000616 if ((aln = addr - wp) != 0) {
wdenk2cefd152004-02-08 22:55:38 +0000617 cword.l = 0;
618 cp = wp;
wdenke65527f2004-02-12 00:47:09 +0000619 for (i = 0; i < aln; ++i, ++cp)
620 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000621
wdenke65527f2004-02-12 00:47:09 +0000622 for (; (i < info->portwidth) && (cnt > 0); i++) {
623 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000624 cnt--;
625 cp++;
626 }
wdenke65527f2004-02-12 00:47:09 +0000627 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
628 flash_add_byte (info, &cword, (*(uchar *) cp));
629 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000630 return rc;
631 wp = cp;
632 }
633
wdenke65527f2004-02-12 00:47:09 +0000634 /* handle the aligned part */
wdenk2cefd152004-02-08 22:55:38 +0000635#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke65527f2004-02-12 00:47:09 +0000636 buffered_size = (info->portwidth / info->chipwidth);
637 buffered_size *= info->buffer_size;
638 while (cnt >= info->portwidth) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100639 /* prohibit buffer write when buffer_size is 1 */
640 if (info->buffer_size == 1) {
641 cword.l = 0;
642 for (i = 0; i < info->portwidth; i++)
643 flash_add_byte (info, &cword, *src++);
644 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
645 return rc;
646 wp += info->portwidth;
647 cnt -= info->portwidth;
648 continue;
649 }
650
651 /* write buffer until next buffered_size aligned boundary */
652 i = buffered_size - (wp % buffered_size);
653 if (i > cnt)
654 i = cnt;
wdenke65527f2004-02-12 00:47:09 +0000655 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk2cefd152004-02-08 22:55:38 +0000656 return rc;
Wolfgang Denke25f0522005-08-12 22:35:59 +0200657 i -= i & (info->portwidth - 1);
wdenk2cefd152004-02-08 22:55:38 +0000658 wp += i;
659 src += i;
wdenke65527f2004-02-12 00:47:09 +0000660 cnt -= i;
wdenk2cefd152004-02-08 22:55:38 +0000661 }
662#else
wdenke65527f2004-02-12 00:47:09 +0000663 while (cnt >= info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000664 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000665 for (i = 0; i < info->portwidth; i++) {
666 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000667 }
wdenke65527f2004-02-12 00:47:09 +0000668 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000669 return rc;
670 wp += info->portwidth;
671 cnt -= info->portwidth;
672 }
673#endif /* CFG_FLASH_USE_BUFFER_WRITE */
674 if (cnt == 0) {
675 return (0);
676 }
677
678 /*
679 * handle unaligned tail bytes
680 */
681 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000682 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
683 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000684 --cnt;
685 }
wdenke65527f2004-02-12 00:47:09 +0000686 for (; i < info->portwidth; ++i, ++cp) {
687 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000688 }
689
wdenke65527f2004-02-12 00:47:09 +0000690 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +0000691}
692
693/*-----------------------------------------------------------------------
694 */
695#ifdef CFG_FLASH_PROTECTION
696
wdenke65527f2004-02-12 00:47:09 +0000697int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +0000698{
699 int retcode = 0;
700
wdenke65527f2004-02-12 00:47:09 +0000701 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
702 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
703 if (prot)
704 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk2cefd152004-02-08 22:55:38 +0000705 else
wdenke65527f2004-02-12 00:47:09 +0000706 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk2cefd152004-02-08 22:55:38 +0000707
wdenke65527f2004-02-12 00:47:09 +0000708 if ((retcode =
709 flash_full_status_check (info, sector, info->erase_blk_tout,
710 prot ? "protect" : "unprotect")) == 0) {
wdenk2cefd152004-02-08 22:55:38 +0000711
712 info->protect[sector] = prot;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200713
714 /*
715 * On some of Intel's flash chips (marked via legacy_unlock)
716 * unprotect unprotects all locking.
717 */
718 if ((prot == 0) && (info->legacy_unlock)) {
wdenk2cefd152004-02-08 22:55:38 +0000719 flash_sect_t i;
wdenke65527f2004-02-12 00:47:09 +0000720
721 for (i = 0; i < info->sector_count; i++) {
722 if (info->protect[i])
723 flash_real_protect (info, i, 1);
wdenk2cefd152004-02-08 22:55:38 +0000724 }
725 }
726 }
wdenk2cefd152004-02-08 22:55:38 +0000727 return retcode;
wdenke65527f2004-02-12 00:47:09 +0000728}
729
wdenk2cefd152004-02-08 22:55:38 +0000730/*-----------------------------------------------------------------------
731 * flash_read_user_serial - read the OneTimeProgramming cells
732 */
wdenke65527f2004-02-12 00:47:09 +0000733void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
734 int len)
wdenk2cefd152004-02-08 22:55:38 +0000735{
wdenke65527f2004-02-12 00:47:09 +0000736 uchar *src;
737 uchar *dst;
wdenk2cefd152004-02-08 22:55:38 +0000738
739 dst = buffer;
wdenke65527f2004-02-12 00:47:09 +0000740 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
741 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
742 memcpy (dst, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200743 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000744}
wdenke65527f2004-02-12 00:47:09 +0000745
wdenk2cefd152004-02-08 22:55:38 +0000746/*
747 * flash_read_factory_serial - read the device Id from the protection area
748 */
wdenke65527f2004-02-12 00:47:09 +0000749void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
750 int len)
wdenk2cefd152004-02-08 22:55:38 +0000751{
wdenke65527f2004-02-12 00:47:09 +0000752 uchar *src;
wdenk6cfa84e2004-02-10 00:03:41 +0000753
wdenke65527f2004-02-12 00:47:09 +0000754 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
755 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
756 memcpy (buffer, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200757 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000758}
759
760#endif /* CFG_FLASH_PROTECTION */
761
wdenke65527f2004-02-12 00:47:09 +0000762/*
763 * flash_is_busy - check to see if the flash is busy
764 * This routine checks the status of the chip and returns true if the chip is busy
765 */
766static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000767{
768 int retval;
wdenke65527f2004-02-12 00:47:09 +0000769
770 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000771 case CFI_CMDSET_INTEL_STANDARD:
772 case CFI_CMDSET_INTEL_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000773 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk2cefd152004-02-08 22:55:38 +0000774 break;
775 case CFI_CMDSET_AMD_STANDARD:
776 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000777 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk2cefd152004-02-08 22:55:38 +0000778 break;
779 default:
780 retval = 0;
781 }
wdenke65527f2004-02-12 00:47:09 +0000782 debug ("flash_is_busy: %d\n", retval);
wdenk2cefd152004-02-08 22:55:38 +0000783 return retval;
784}
wdenke65527f2004-02-12 00:47:09 +0000785
wdenk2cefd152004-02-08 22:55:38 +0000786/*-----------------------------------------------------------------------
787 * wait for XSR.7 to be set. Time out with an error if it does not.
788 * This routine does not set the flash to read-array mode.
789 */
wdenke65527f2004-02-12 00:47:09 +0000790static int flash_status_check (flash_info_t * info, flash_sect_t sector,
791 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000792{
793 ulong start;
794
Stefan Roeseefef95b2006-04-01 13:41:03 +0200795#if CFG_HZ != 1000
796 tout *= CFG_HZ/1000;
797#endif
798
wdenk2cefd152004-02-08 22:55:38 +0000799 /* Wait for command completion */
800 start = get_timer (0);
wdenke65527f2004-02-12 00:47:09 +0000801 while (flash_is_busy (info, sector)) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100802 if (get_timer (start) > tout) {
wdenke65527f2004-02-12 00:47:09 +0000803 printf ("Flash %s timeout at address %lx data %lx\n",
804 prompt, info->start[sector],
805 flash_read_long (info, sector, 0));
806 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000807 return ERR_TIMOUT;
808 }
Wolfgang Denkc9f8d7c2006-06-02 11:46:20 +0200809 udelay (1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000810 }
811 return ERR_OK;
812}
wdenke65527f2004-02-12 00:47:09 +0000813
wdenk2cefd152004-02-08 22:55:38 +0000814/*-----------------------------------------------------------------------
815 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
816 * This routine sets the flash to read-array mode.
817 */
wdenke65527f2004-02-12 00:47:09 +0000818static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
819 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000820{
821 int retcode;
wdenke65527f2004-02-12 00:47:09 +0000822
823 retcode = flash_status_check (info, sector, tout, prompt);
824 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000825 case CFI_CMDSET_INTEL_EXTENDED:
826 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesec865e6c2006-02-28 15:29:58 +0100827 if ((retcode == ERR_OK)
wdenke65527f2004-02-12 00:47:09 +0000828 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk2cefd152004-02-08 22:55:38 +0000829 retcode = ERR_INVAL;
wdenke65527f2004-02-12 00:47:09 +0000830 printf ("Flash %s error at address %lx\n", prompt,
831 info->start[sector]);
wdenke537b3b2004-02-23 23:54:43 +0000832 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000833 puts ("Command Sequence Error.\n");
wdenke537b3b2004-02-23 23:54:43 +0000834 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000835 puts ("Block Erase Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000836 retcode = ERR_NOT_ERASED;
wdenke537b3b2004-02-23 23:54:43 +0000837 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000838 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000839 }
wdenke65527f2004-02-12 00:47:09 +0000840 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk42c05472004-03-23 22:14:11 +0000841 puts ("Block locked.\n");
wdenke65527f2004-02-12 00:47:09 +0000842 retcode = ERR_PROTECTED;
843 }
844 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk42c05472004-03-23 22:14:11 +0000845 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000846 }
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200847 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000848 break;
849 default:
850 break;
851 }
852 return retcode;
853}
wdenke65527f2004-02-12 00:47:09 +0000854
wdenk2cefd152004-02-08 22:55:38 +0000855/*-----------------------------------------------------------------------
856 */
wdenke65527f2004-02-12 00:47:09 +0000857static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000858{
wdenk369d43d2004-03-14 14:09:05 +0000859#if defined(__LITTLE_ENDIAN)
860 unsigned short w;
861 unsigned int l;
862 unsigned long long ll;
863#endif
864
wdenke65527f2004-02-12 00:47:09 +0000865 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000866 case FLASH_CFI_8BIT:
867 cword->c = c;
868 break;
869 case FLASH_CFI_16BIT:
wdenk369d43d2004-03-14 14:09:05 +0000870#if defined(__LITTLE_ENDIAN)
871 w = c;
872 w <<= 8;
873 cword->w = (cword->w >> 8) | w;
874#else
wdenk2cefd152004-02-08 22:55:38 +0000875 cword->w = (cword->w << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000876#endif
wdenk2cefd152004-02-08 22:55:38 +0000877 break;
878 case FLASH_CFI_32BIT:
wdenk369d43d2004-03-14 14:09:05 +0000879#if defined(__LITTLE_ENDIAN)
880 l = c;
881 l <<= 24;
882 cword->l = (cword->l >> 8) | l;
883#else
wdenk2cefd152004-02-08 22:55:38 +0000884 cword->l = (cword->l << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000885#endif
wdenk2cefd152004-02-08 22:55:38 +0000886 break;
887 case FLASH_CFI_64BIT:
wdenk369d43d2004-03-14 14:09:05 +0000888#if defined(__LITTLE_ENDIAN)
889 ll = c;
890 ll <<= 56;
891 cword->ll = (cword->ll >> 8) | ll;
892#else
wdenk2cefd152004-02-08 22:55:38 +0000893 cword->ll = (cword->ll << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000894#endif
wdenk2cefd152004-02-08 22:55:38 +0000895 break;
896 }
897}
898
899
900/*-----------------------------------------------------------------------
901 * make a proper sized command based on the port and chip widths
902 */
wdenke65527f2004-02-12 00:47:09 +0000903static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk2cefd152004-02-08 22:55:38 +0000904{
905 int i;
wdenke65527f2004-02-12 00:47:09 +0000906 uchar *cp = (uchar *) cmdbuf;
907
wdenke65527f2004-02-12 00:47:09 +0000908#if defined(__LITTLE_ENDIAN)
Wolfgang Denk07bdb022005-09-24 23:32:48 +0200909 for (i = info->portwidth; i > 0; i--)
910#else
911 for (i = 1; i <= info->portwidth; i++)
wdenke65527f2004-02-12 00:47:09 +0000912#endif
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200913 *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
wdenk2cefd152004-02-08 22:55:38 +0000914}
915
916/*
917 * Write a proper sized command to the correct address
918 */
wdenke537b3b2004-02-23 23:54:43 +0000919static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000920{
921
922 volatile cfiptr_t addr;
923 cfiword_t cword;
wdenke65527f2004-02-12 00:47:09 +0000924
925 addr.cp = flash_make_addr (info, sect, offset);
926 flash_make_cmd (info, cmd, &cword);
927 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000928 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000929 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
930 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk2cefd152004-02-08 22:55:38 +0000931 *addr.cp = cword.c;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100932#ifdef CONFIG_BLACKFIN
933 asm("ssync;");
934#endif
wdenk2cefd152004-02-08 22:55:38 +0000935 break;
936 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000937 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
938 cmd, cword.w,
wdenk2cefd152004-02-08 22:55:38 +0000939 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
940 *addr.wp = cword.w;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100941#ifdef CONFIG_BLACKFIN
942 asm("ssync;");
943#endif
wdenk2cefd152004-02-08 22:55:38 +0000944 break;
945 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000946 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
947 cmd, cword.l,
wdenk2cefd152004-02-08 22:55:38 +0000948 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
949 *addr.lp = cword.l;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100950#ifdef CONFIG_BLACKFIN
951 asm("ssync;");
952#endif
wdenk2cefd152004-02-08 22:55:38 +0000953 break;
954 case FLASH_CFI_64BIT:
955#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000956 {
wdenk2cefd152004-02-08 22:55:38 +0000957 char str[20];
wdenk6cfa84e2004-02-10 00:03:41 +0000958
wdenke65527f2004-02-12 00:47:09 +0000959 print_longlong (str, cword.ll);
960
961 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
962 addr.llp, cmd, str,
wdenk2cefd152004-02-08 22:55:38 +0000963 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
964 }
965#endif
966 *addr.llp = cword.ll;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100967#ifdef CONFIG_BLACKFIN
968 asm("ssync;");
969#endif
wdenk2cefd152004-02-08 22:55:38 +0000970 break;
971 }
972}
973
wdenke65527f2004-02-12 00:47:09 +0000974static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000975{
wdenked2ac4b2004-03-14 18:23:55 +0000976 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
977 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
wdenk2cefd152004-02-08 22:55:38 +0000978}
wdenke65527f2004-02-12 00:47:09 +0000979
wdenk2cefd152004-02-08 22:55:38 +0000980/*-----------------------------------------------------------------------
981 */
wdenke537b3b2004-02-23 23:54:43 +0000982static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000983{
984 cfiptr_t cptr;
985 cfiword_t cword;
986 int retval;
wdenke65527f2004-02-12 00:47:09 +0000987
988 cptr.cp = flash_make_addr (info, sect, offset);
989 flash_make_cmd (info, cmd, &cword);
wdenk2cefd152004-02-08 22:55:38 +0000990
wdenke65527f2004-02-12 00:47:09 +0000991 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
992 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000993 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000994 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk2cefd152004-02-08 22:55:38 +0000995 retval = (cptr.cp[0] == cword.c);
996 break;
997 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000998 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk2cefd152004-02-08 22:55:38 +0000999 retval = (cptr.wp[0] == cword.w);
1000 break;
1001 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +00001002 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk2cefd152004-02-08 22:55:38 +00001003 retval = (cptr.lp[0] == cword.l);
1004 break;
1005 case FLASH_CFI_64BIT:
wdenk6cfa84e2004-02-10 00:03:41 +00001006#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +00001007 {
wdenk2cefd152004-02-08 22:55:38 +00001008 char str1[20];
1009 char str2[20];
wdenke65527f2004-02-12 00:47:09 +00001010
1011 print_longlong (str1, cptr.llp[0]);
1012 print_longlong (str2, cword.ll);
1013 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +00001014 }
1015#endif
1016 retval = (cptr.llp[0] == cword.ll);
1017 break;
1018 default:
1019 retval = 0;
1020 break;
1021 }
1022 return retval;
1023}
wdenke65527f2004-02-12 00:47:09 +00001024
wdenk2cefd152004-02-08 22:55:38 +00001025/*-----------------------------------------------------------------------
1026 */
wdenke537b3b2004-02-23 23:54:43 +00001027static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001028{
1029 cfiptr_t cptr;
1030 cfiword_t cword;
1031 int retval;
wdenke65527f2004-02-12 00:47:09 +00001032
1033 cptr.cp = flash_make_addr (info, sect, offset);
1034 flash_make_cmd (info, cmd, &cword);
1035 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001036 case FLASH_CFI_8BIT:
1037 retval = ((cptr.cp[0] & cword.c) == cword.c);
1038 break;
1039 case FLASH_CFI_16BIT:
1040 retval = ((cptr.wp[0] & cword.w) == cword.w);
1041 break;
1042 case FLASH_CFI_32BIT:
1043 retval = ((cptr.lp[0] & cword.l) == cword.l);
1044 break;
1045 case FLASH_CFI_64BIT:
1046 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenke65527f2004-02-12 00:47:09 +00001047 break;
wdenk2cefd152004-02-08 22:55:38 +00001048 default:
1049 retval = 0;
1050 break;
1051 }
1052 return retval;
1053}
1054
1055/*-----------------------------------------------------------------------
1056 */
wdenke537b3b2004-02-23 23:54:43 +00001057static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001058{
1059 cfiptr_t cptr;
1060 cfiword_t cword;
1061 int retval;
wdenke65527f2004-02-12 00:47:09 +00001062
1063 cptr.cp = flash_make_addr (info, sect, offset);
1064 flash_make_cmd (info, cmd, &cword);
1065 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001066 case FLASH_CFI_8BIT:
1067 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
1068 break;
1069 case FLASH_CFI_16BIT:
1070 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
1071 break;
1072 case FLASH_CFI_32BIT:
1073 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
1074 break;
1075 case FLASH_CFI_64BIT:
wdenke65527f2004-02-12 00:47:09 +00001076 retval = ((cptr.llp[0] & cword.ll) !=
1077 (cptr.llp[0] & cword.ll));
wdenk2cefd152004-02-08 22:55:38 +00001078 break;
1079 default:
1080 retval = 0;
1081 break;
1082 }
1083 return retval;
1084}
1085
1086/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +01001087 * read jedec ids from device and set corresponding fields in info struct
1088 *
1089 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1090 *
1091*/
1092static void flash_read_jedec_ids (flash_info_t * info)
1093{
1094 info->manufacturer_id = 0;
1095 info->device_id = 0;
1096 info->device_id2 = 0;
1097
1098 switch (info->vendor) {
1099 case CFI_CMDSET_INTEL_STANDARD:
1100 case CFI_CMDSET_INTEL_EXTENDED:
1101 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1102 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1103 udelay(1000); /* some flash are slow to respond */
1104 info->manufacturer_id = flash_read_uchar (info,
1105 FLASH_OFFSET_MANUFACTURER_ID);
1106 info->device_id = flash_read_uchar (info,
1107 FLASH_OFFSET_DEVICE_ID);
1108 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1109 break;
1110 case CFI_CMDSET_AMD_STANDARD:
1111 case CFI_CMDSET_AMD_EXTENDED:
1112 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1113 flash_unlock_seq(info, 0);
1114 flash_write_cmd(info, 0, AMD_ADDR_START, FLASH_CMD_READ_ID);
1115 udelay(1000); /* some flash are slow to respond */
1116 info->manufacturer_id = flash_read_uchar (info,
1117 FLASH_OFFSET_MANUFACTURER_ID);
1118 info->device_id = flash_read_uchar (info,
1119 FLASH_OFFSET_DEVICE_ID);
1120 if (info->device_id == 0x7E) {
1121 /* AMD 3-byte (expanded) device ids */
1122 info->device_id2 = flash_read_uchar (info,
1123 FLASH_OFFSET_DEVICE_ID2);
1124 info->device_id2 <<= 8;
1125 info->device_id2 |= flash_read_uchar (info,
1126 FLASH_OFFSET_DEVICE_ID3);
1127 }
1128 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1129 break;
1130 default:
1131 break;
1132 }
1133}
1134
1135/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +00001136 * detect if flash is compatible with the Common Flash Interface (CFI)
1137 * http://www.jedec.org/download/search/jesd68.pdf
1138 *
1139*/
wdenke65527f2004-02-12 00:47:09 +00001140static int flash_detect_cfi (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001141{
Wolfgang Denkafa0dd02006-12-27 01:26:13 +01001142 int cfi_offset;
wdenke65527f2004-02-12 00:47:09 +00001143 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001144
Stefan Roesec865e6c2006-02-28 15:29:58 +01001145 for (info->portwidth = CFG_FLASH_CFI_WIDTH;
wdenke65527f2004-02-12 00:47:09 +00001146 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1147 for (info->chipwidth = FLASH_CFI_BY8;
1148 info->chipwidth <= info->portwidth;
1149 info->chipwidth <<= 1) {
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001150 flash_write_cmd (info, 0, 0, info->cmd_reset);
Wolfgang Denkafa0dd02006-12-27 01:26:13 +01001151 for (cfi_offset=0; cfi_offset < sizeof(flash_offset_cfi)/sizeof(uint); cfi_offset++) {
1152 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], FLASH_CMD_CFI);
1153 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1154 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1155 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1156 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
1157 info->cfi_offset=flash_offset_cfi[cfi_offset];
1158 debug ("device interface is %d\n",
1159 info->interface);
1160 debug ("found port %d chip %d ",
1161 info->portwidth, info->chipwidth);
1162 debug ("port %d bits chip %d bits\n",
1163 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1164 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1165 return 1;
1166 }
wdenk2cefd152004-02-08 22:55:38 +00001167 }
1168 }
1169 }
wdenke65527f2004-02-12 00:47:09 +00001170 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001171 return 0;
1172}
wdenke65527f2004-02-12 00:47:09 +00001173
wdenk2cefd152004-02-08 22:55:38 +00001174/*
1175 * The following code cannot be run from FLASH!
1176 *
1177 */
Marian Balakowicz513b4a12005-10-11 19:09:42 +02001178ulong flash_get_size (ulong base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001179{
wdenke65527f2004-02-12 00:47:09 +00001180 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00001181 int i, j;
1182 flash_sect_t sect_cnt;
1183 unsigned long sector;
1184 unsigned long tmp;
1185 int size_ratio;
1186 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00001187 int erase_region_size;
1188 int erase_region_count;
Stefan Roese12797482006-11-13 13:55:24 +01001189 int geometry_reversed = 0;
1190
1191 info->ext_addr = 0;
1192 info->cfi_version = 0;
Stefan Roeseefef95b2006-04-01 13:41:03 +02001193#ifdef CFG_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02001194 info->legacy_unlock = 0;
1195#endif
wdenk2cefd152004-02-08 22:55:38 +00001196
1197 info->start[0] = base;
1198
wdenke65527f2004-02-12 00:47:09 +00001199 if (flash_detect_cfi (info)) {
Stefan Roese12797482006-11-13 13:55:24 +01001200 info->vendor = flash_read_ushort (info, 0,
1201 FLASH_OFFSET_PRIMARY_VENDOR);
1202 flash_read_jedec_ids (info);
Stefano Babic5f172a82006-12-12 00:22:42 +01001203 flash_write_cmd (info, 0, info->cfi_offset, FLASH_CMD_CFI);
Stefan Roese12797482006-11-13 13:55:24 +01001204 num_erase_regions = flash_read_uchar (info,
1205 FLASH_OFFSET_NUM_ERASE_REGIONS);
1206 info->ext_addr = flash_read_ushort (info, 0,
1207 FLASH_OFFSET_EXT_QUERY_T_P_ADDR);
1208 if (info->ext_addr) {
1209 info->cfi_version = (ushort) flash_read_uchar (info,
1210 info->ext_addr + 3) << 8;
1211 info->cfi_version |= (ushort) flash_read_uchar (info,
1212 info->ext_addr + 4);
1213 }
wdenke65527f2004-02-12 00:47:09 +00001214#ifdef DEBUG
1215 flash_printqry (info, 0);
1216#endif
1217 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001218 case CFI_CMDSET_INTEL_STANDARD:
1219 case CFI_CMDSET_INTEL_EXTENDED:
1220 default:
1221 info->cmd_reset = FLASH_CMD_RESET;
Stefan Roeseefef95b2006-04-01 13:41:03 +02001222#ifdef CFG_FLASH_PROTECTION
1223 /* read legacy lock/unlock bit from intel flash */
Stefan Roese12797482006-11-13 13:55:24 +01001224 if (info->ext_addr) {
1225 info->legacy_unlock = flash_read_uchar (info,
1226 info->ext_addr + 5) & 0x08;
1227 }
Stefan Roeseefef95b2006-04-01 13:41:03 +02001228#endif
wdenk2cefd152004-02-08 22:55:38 +00001229 break;
1230 case CFI_CMDSET_AMD_STANDARD:
1231 case CFI_CMDSET_AMD_EXTENDED:
1232 info->cmd_reset = AMD_CMD_RESET;
Stefan Roese12797482006-11-13 13:55:24 +01001233 /* check if flash geometry needs reversal */
1234 if (num_erase_regions <= 1)
1235 break;
1236 /* reverse geometry if top boot part */
1237 if (info->cfi_version < 0x3131) {
1238 /* CFI < 1.1, try to guess from device id */
1239 if ((info->device_id & 0x80) != 0) {
1240 geometry_reversed = 1;
1241 }
1242 break;
1243 }
1244 /* CFI >= 1.1, deduct from top/bottom flag */
1245 /* note: ext_addr is valid since cfi_version > 0 */
1246 if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1247 geometry_reversed = 1;
1248 }
wdenk2cefd152004-02-08 22:55:38 +00001249 break;
1250 }
wdenk6cfa84e2004-02-10 00:03:41 +00001251
wdenke65527f2004-02-12 00:47:09 +00001252 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese12797482006-11-13 13:55:24 +01001253 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1254 debug ("device id is 0x%x\n", info->device_id);
1255 debug ("device id2 is 0x%x\n", info->device_id2);
1256 debug ("cfi version is 0x%04x\n", info->cfi_version);
1257
wdenk2cefd152004-02-08 22:55:38 +00001258 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00001259 /* if the chip is x8/x16 reduce the ratio by half */
1260 if ((info->interface == FLASH_CFI_X8X16)
1261 && (info->chipwidth == FLASH_CFI_BY8)) {
1262 size_ratio >>= 1;
1263 }
wdenke65527f2004-02-12 00:47:09 +00001264 debug ("size_ratio %d port %d bits chip %d bits\n",
1265 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1266 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1267 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00001268 sect_cnt = 0;
1269 sector = base;
wdenke65527f2004-02-12 00:47:09 +00001270 for (i = 0; i < num_erase_regions; i++) {
1271 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00001272 printf ("%d erase regions found, only %d used\n",
1273 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00001274 break;
1275 }
Stefan Roese12797482006-11-13 13:55:24 +01001276 if (geometry_reversed)
1277 tmp = flash_read_long (info, 0,
1278 FLASH_OFFSET_ERASE_REGIONS +
1279 (num_erase_regions - 1 - i) * 4);
1280 else
1281 tmp = flash_read_long (info, 0,
wdenke65527f2004-02-12 00:47:09 +00001282 FLASH_OFFSET_ERASE_REGIONS +
1283 i * 4);
1284 erase_region_size =
1285 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk2cefd152004-02-08 22:55:38 +00001286 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00001287 erase_region_count = (tmp & 0xffff) + 1;
wdenkaeba06f2004-06-09 17:34:58 +00001288 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00001289 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00001290 for (j = 0; j < erase_region_count; j++) {
wdenk2cefd152004-02-08 22:55:38 +00001291 info->start[sect_cnt] = sector;
1292 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00001293
1294 /*
1295 * Only read protection status from supported devices (intel...)
1296 */
1297 switch (info->vendor) {
1298 case CFI_CMDSET_INTEL_EXTENDED:
1299 case CFI_CMDSET_INTEL_STANDARD:
1300 info->protect[sect_cnt] =
1301 flash_isset (info, sect_cnt,
1302 FLASH_OFFSET_PROTECT,
1303 FLASH_STATUS_PROTECT);
1304 break;
1305 default:
1306 info->protect[sect_cnt] = 0; /* default: not protected */
1307 }
1308
wdenk2cefd152004-02-08 22:55:38 +00001309 sect_cnt++;
1310 }
1311 }
1312
1313 info->sector_count = sect_cnt;
1314 /* multiply the size by the number of chips */
wdenke537b3b2004-02-23 23:54:43 +00001315 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1316 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
wdenke65527f2004-02-12 00:47:09 +00001317 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenke537b3b2004-02-23 23:54:43 +00001318 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
Stefan Roeseefef95b2006-04-01 13:41:03 +02001319 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT)) *
1320 (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT));
1321 info->buffer_write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
Stefan Roesec865e6c2006-02-28 15:29:58 +01001322 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
1323 (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
1324 info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
wdenk2cefd152004-02-08 22:55:38 +00001325 info->flash_id = FLASH_MAN_CFI;
wdenked2ac4b2004-03-14 18:23:55 +00001326 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1327 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1328 }
wdenk2cefd152004-02-08 22:55:38 +00001329 }
1330
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001331 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenke65527f2004-02-12 00:47:09 +00001332 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00001333}
1334
Stefan Roesec865e6c2006-02-28 15:29:58 +01001335/* loop through the sectors from the highest address
1336 * when the passed address is greater or equal to the sector address
1337 * we have a match
1338 */
1339static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1340{
1341 flash_sect_t sector;
1342
1343 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1344 if (addr >= info->start[sector])
1345 break;
1346 }
1347 return sector;
1348}
wdenk2cefd152004-02-08 22:55:38 +00001349
1350/*-----------------------------------------------------------------------
1351 */
wdenke65527f2004-02-12 00:47:09 +00001352static int flash_write_cfiword (flash_info_t * info, ulong dest,
1353 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +00001354{
wdenk2cefd152004-02-08 22:55:38 +00001355 cfiptr_t ctladdr;
1356 cfiptr_t cptr;
1357 int flag;
1358
wdenke65527f2004-02-12 00:47:09 +00001359 ctladdr.cp = flash_make_addr (info, 0, 0);
1360 cptr.cp = (uchar *) dest;
wdenk2cefd152004-02-08 22:55:38 +00001361
1362
1363 /* Check if Flash is (sufficiently) erased */
wdenke65527f2004-02-12 00:47:09 +00001364 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001365 case FLASH_CFI_8BIT:
1366 flag = ((cptr.cp[0] & cword.c) == cword.c);
1367 break;
1368 case FLASH_CFI_16BIT:
1369 flag = ((cptr.wp[0] & cword.w) == cword.w);
1370 break;
1371 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +00001372 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk2cefd152004-02-08 22:55:38 +00001373 break;
1374 case FLASH_CFI_64BIT:
wdenk391b5742004-10-10 23:27:33 +00001375 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenk2cefd152004-02-08 22:55:38 +00001376 break;
1377 default:
1378 return 2;
1379 }
wdenke65527f2004-02-12 00:47:09 +00001380 if (!flag)
wdenk2cefd152004-02-08 22:55:38 +00001381 return 2;
1382
1383 /* Disable interrupts which might cause a timeout here */
wdenke65527f2004-02-12 00:47:09 +00001384 flag = disable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001385
wdenke65527f2004-02-12 00:47:09 +00001386 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001387 case CFI_CMDSET_INTEL_EXTENDED:
1388 case CFI_CMDSET_INTEL_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001389 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1390 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001391 break;
1392 case CFI_CMDSET_AMD_EXTENDED:
1393 case CFI_CMDSET_AMD_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001394 flash_unlock_seq (info, 0);
wdenked2ac4b2004-03-14 18:23:55 +00001395 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001396 break;
1397 }
1398
wdenke65527f2004-02-12 00:47:09 +00001399 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001400 case FLASH_CFI_8BIT:
1401 cptr.cp[0] = cword.c;
1402 break;
1403 case FLASH_CFI_16BIT:
1404 cptr.wp[0] = cword.w;
1405 break;
1406 case FLASH_CFI_32BIT:
1407 cptr.lp[0] = cword.l;
1408 break;
1409 case FLASH_CFI_64BIT:
1410 cptr.llp[0] = cword.ll;
1411 break;
1412 }
1413
1414 /* re-enable interrupts if necessary */
wdenke65527f2004-02-12 00:47:09 +00001415 if (flag)
1416 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001417
Stefan Roesec865e6c2006-02-28 15:29:58 +01001418 return flash_full_status_check (info, find_sector (info, dest),
1419 info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +00001420}
1421
1422#ifdef CFG_FLASH_USE_BUFFER_WRITE
1423
wdenke65527f2004-02-12 00:47:09 +00001424static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1425 int len)
wdenk2cefd152004-02-08 22:55:38 +00001426{
1427 flash_sect_t sector;
1428 int cnt;
1429 int retcode;
1430 volatile cfiptr_t src;
1431 volatile cfiptr_t dst;
1432
Stefan Roesec865e6c2006-02-28 15:29:58 +01001433 switch (info->vendor) {
1434 case CFI_CMDSET_INTEL_STANDARD:
1435 case CFI_CMDSET_INTEL_EXTENDED:
1436 src.cp = cp;
1437 dst.cp = (uchar *) dest;
1438 sector = find_sector (info, dest);
1439 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1440 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1441 if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
1442 "write to buffer")) == ERR_OK) {
1443 /* reduce the number of loops by the width of the port */
1444 switch (info->portwidth) {
1445 case FLASH_CFI_8BIT:
1446 cnt = len;
1447 break;
1448 case FLASH_CFI_16BIT:
1449 cnt = len >> 1;
1450 break;
1451 case FLASH_CFI_32BIT:
1452 cnt = len >> 2;
1453 break;
1454 case FLASH_CFI_64BIT:
1455 cnt = len >> 3;
1456 break;
1457 default:
1458 return ERR_INVAL;
1459 break;
1460 }
1461 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1462 while (cnt-- > 0) {
1463 switch (info->portwidth) {
1464 case FLASH_CFI_8BIT:
1465 *dst.cp++ = *src.cp++;
1466 break;
1467 case FLASH_CFI_16BIT:
1468 *dst.wp++ = *src.wp++;
1469 break;
1470 case FLASH_CFI_32BIT:
1471 *dst.lp++ = *src.lp++;
1472 break;
1473 case FLASH_CFI_64BIT:
1474 *dst.llp++ = *src.llp++;
1475 break;
1476 default:
1477 return ERR_INVAL;
1478 break;
1479 }
1480 }
1481 flash_write_cmd (info, sector, 0,
1482 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1483 retcode = flash_full_status_check (info, sector,
1484 info->buffer_write_tout,
1485 "buffer write");
1486 }
1487 return retcode;
1488
1489 case CFI_CMDSET_AMD_STANDARD:
1490 case CFI_CMDSET_AMD_EXTENDED:
1491 src.cp = cp;
1492 dst.cp = (uchar *) dest;
1493 sector = find_sector (info, dest);
1494
1495 flash_unlock_seq(info,0);
1496 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
1497
wdenke65527f2004-02-12 00:47:09 +00001498 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001499 case FLASH_CFI_8BIT:
1500 cnt = len;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001501 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1502 while (cnt-- > 0) *dst.cp++ = *src.cp++;
wdenk2cefd152004-02-08 22:55:38 +00001503 break;
1504 case FLASH_CFI_16BIT:
1505 cnt = len >> 1;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001506 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1507 while (cnt-- > 0) *dst.wp++ = *src.wp++;
wdenk2cefd152004-02-08 22:55:38 +00001508 break;
1509 case FLASH_CFI_32BIT:
1510 cnt = len >> 2;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001511 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1512 while (cnt-- > 0) *dst.lp++ = *src.lp++;
wdenk2cefd152004-02-08 22:55:38 +00001513 break;
1514 case FLASH_CFI_64BIT:
1515 cnt = len >> 3;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001516 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1517 while (cnt-- > 0) *dst.llp++ = *src.llp++;
wdenk2cefd152004-02-08 22:55:38 +00001518 break;
1519 default:
1520 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001521 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01001522
1523 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1524 retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
1525 "buffer write");
1526 return retcode;
1527
1528 default:
1529 debug ("Unknown Command Set\n");
1530 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001531 }
wdenk2cefd152004-02-08 22:55:38 +00001532}
wdenk5b835a32004-09-28 19:00:19 +00001533#endif /* CFG_FLASH_USE_BUFFER_WRITE */
wdenk2cefd152004-02-08 22:55:38 +00001534#endif /* CFG_FLASH_CFI */