blob: 5579a1efc155e3cb9d488af81d5262b2d2b19c95 [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>
Haiying Wangc123a382007-02-21 16:52:31 +010039#include <asm/io.h>
wdenkaeba06f2004-06-09 17:34:58 +000040#include <asm/byteorder.h>
wdenkd0245fc2005-04-13 10:02:42 +000041#include <environment.h>
wdenke65527f2004-02-12 00:47:09 +000042#ifdef CFG_FLASH_CFI_DRIVER
wdenke537b3b2004-02-23 23:54:43 +000043
wdenk2cefd152004-02-08 22:55:38 +000044/*
45 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
46 * The width of the port and the width of the chips are determined at initialization.
47 * These widths are used to calculate the address for access CFI data structures.
wdenk2cefd152004-02-08 22:55:38 +000048 *
49 * References
50 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
51 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
52 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
53 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese12797482006-11-13 13:55:24 +010054 * AMD CFI Specification, Release 2.0 December 1, 2001
55 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
56 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk2cefd152004-02-08 22:55:38 +000057 *
Heiko Schocher800db312007-01-19 18:05:26 +010058 * define CFG_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
59 * reading and writing ... (yes there is such a Hardware).
wdenk2cefd152004-02-08 22:55:38 +000060 */
61
wdenke65527f2004-02-12 00:47:09 +000062#ifndef CFG_FLASH_BANKS_LIST
63#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
64#endif
65
wdenk2cefd152004-02-08 22:55:38 +000066#define FLASH_CMD_CFI 0x98
67#define FLASH_CMD_READ_ID 0x90
68#define FLASH_CMD_RESET 0xff
69#define FLASH_CMD_BLOCK_ERASE 0x20
70#define FLASH_CMD_ERASE_CONFIRM 0xD0
71#define FLASH_CMD_WRITE 0x40
72#define FLASH_CMD_PROTECT 0x60
73#define FLASH_CMD_PROTECT_SET 0x01
74#define FLASH_CMD_PROTECT_CLEAR 0xD0
75#define FLASH_CMD_CLEAR_STATUS 0x50
wdenke65527f2004-02-12 00:47:09 +000076#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
77#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk2cefd152004-02-08 22:55:38 +000078
79#define FLASH_STATUS_DONE 0x80
80#define FLASH_STATUS_ESS 0x40
81#define FLASH_STATUS_ECLBS 0x20
82#define FLASH_STATUS_PSLBS 0x10
83#define FLASH_STATUS_VPENS 0x08
84#define FLASH_STATUS_PSS 0x04
85#define FLASH_STATUS_DPS 0x02
86#define FLASH_STATUS_R 0x01
87#define FLASH_STATUS_PROTECT 0x01
88
89#define AMD_CMD_RESET 0xF0
90#define AMD_CMD_WRITE 0xA0
91#define AMD_CMD_ERASE_START 0x80
92#define AMD_CMD_ERASE_SECTOR 0x30
wdenked2ac4b2004-03-14 18:23:55 +000093#define AMD_CMD_UNLOCK_START 0xAA
94#define AMD_CMD_UNLOCK_ACK 0x55
Stefan Roesec865e6c2006-02-28 15:29:58 +010095#define AMD_CMD_WRITE_TO_BUFFER 0x25
96#define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
wdenk2cefd152004-02-08 22:55:38 +000097
98#define AMD_STATUS_TOGGLE 0x40
99#define AMD_STATUS_ERROR 0x20
Stefan Roesec865e6c2006-02-28 15:29:58 +0100100
101#define AMD_ADDR_ERASE_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
102#define AMD_ADDR_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
103#define AMD_ADDR_ACK ((info->portwidth == FLASH_CFI_8BIT) ? 0x555 : 0x2AA)
wdenk2cefd152004-02-08 22:55:38 +0000104
Stefan Roese12797482006-11-13 13:55:24 +0100105#define FLASH_OFFSET_MANUFACTURER_ID 0x00
106#define FLASH_OFFSET_DEVICE_ID 0x01
107#define FLASH_OFFSET_DEVICE_ID2 0x0E
108#define FLASH_OFFSET_DEVICE_ID3 0x0F
wdenk2cefd152004-02-08 22:55:38 +0000109#define FLASH_OFFSET_CFI 0x55
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100110#define FLASH_OFFSET_CFI_ALT 0x555
wdenk2cefd152004-02-08 22:55:38 +0000111#define FLASH_OFFSET_CFI_RESP 0x10
wdenke65527f2004-02-12 00:47:09 +0000112#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
Stefan Roeseefef95b2006-04-01 13:41:03 +0200113#define FLASH_OFFSET_EXT_QUERY_T_P_ADDR 0x15 /* extended query table primary addr */
wdenk2cefd152004-02-08 22:55:38 +0000114#define FLASH_OFFSET_WTOUT 0x1F
wdenke65527f2004-02-12 00:47:09 +0000115#define FLASH_OFFSET_WBTOUT 0x20
wdenk2cefd152004-02-08 22:55:38 +0000116#define FLASH_OFFSET_ETOUT 0x21
wdenke65527f2004-02-12 00:47:09 +0000117#define FLASH_OFFSET_CETOUT 0x22
wdenk2cefd152004-02-08 22:55:38 +0000118#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenke65527f2004-02-12 00:47:09 +0000119#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk2cefd152004-02-08 22:55:38 +0000120#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenke65527f2004-02-12 00:47:09 +0000121#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk2cefd152004-02-08 22:55:38 +0000122#define FLASH_OFFSET_SIZE 0x27
wdenke65527f2004-02-12 00:47:09 +0000123#define FLASH_OFFSET_INTERFACE 0x28
124#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk2cefd152004-02-08 22:55:38 +0000125#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
126#define FLASH_OFFSET_ERASE_REGIONS 0x2D
127#define FLASH_OFFSET_PROTECT 0x02
wdenke65527f2004-02-12 00:47:09 +0000128#define FLASH_OFFSET_USER_PROTECTION 0x85
129#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk2cefd152004-02-08 22:55:38 +0000130
Stefan Roese12797482006-11-13 13:55:24 +0100131#define CFI_CMDSET_NONE 0
132#define CFI_CMDSET_INTEL_EXTENDED 1
133#define CFI_CMDSET_AMD_STANDARD 2
134#define CFI_CMDSET_INTEL_STANDARD 3
135#define CFI_CMDSET_AMD_EXTENDED 4
136#define CFI_CMDSET_MITSU_STANDARD 256
137#define CFI_CMDSET_MITSU_EXTENDED 257
138#define CFI_CMDSET_SST 258
wdenk2cefd152004-02-08 22:55:38 +0000139
wdenk51242782004-12-18 22:35:43 +0000140#ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
141# undef FLASH_CMD_RESET
Stefan Roese12797482006-11-13 13:55:24 +0100142# define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
wdenk51242782004-12-18 22:35:43 +0000143#endif
144
wdenk2cefd152004-02-08 22:55:38 +0000145typedef union {
146 unsigned char c;
147 unsigned short w;
148 unsigned long l;
149 unsigned long long ll;
150} cfiword_t;
151
152typedef union {
wdenke65527f2004-02-12 00:47:09 +0000153 volatile unsigned char *cp;
wdenk2cefd152004-02-08 22:55:38 +0000154 volatile unsigned short *wp;
wdenke65527f2004-02-12 00:47:09 +0000155 volatile unsigned long *lp;
wdenk2cefd152004-02-08 22:55:38 +0000156 volatile unsigned long long *llp;
157} cfiptr_t;
158
Stefan Roese12797482006-11-13 13:55:24 +0100159#define NUM_ERASE_REGIONS 4 /* max. number of erase regions */
wdenk2cefd152004-02-08 22:55:38 +0000160
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100161static uint flash_offset_cfi[2]={FLASH_OFFSET_CFI,FLASH_OFFSET_CFI_ALT};
162
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200163/* use CFG_MAX_FLASH_BANKS_DETECT if defined */
164#ifdef CFG_MAX_FLASH_BANKS_DETECT
165static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
166flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
167#else
wdenk2cefd152004-02-08 22:55:38 +0000168static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200169flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
170#endif
wdenk2cefd152004-02-08 22:55:38 +0000171
Stefan Roesec865e6c2006-02-28 15:29:58 +0100172/*
173 * Check if chip width is defined. If not, start detecting with 8bit.
174 */
175#ifndef CFG_FLASH_CFI_WIDTH
176#define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
177#endif
178
wdenk2cefd152004-02-08 22:55:38 +0000179
180/*-----------------------------------------------------------------------
181 * Functions
182 */
183
184typedef unsigned long flash_sect_t;
185
wdenke65527f2004-02-12 00:47:09 +0000186static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
187static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenke537b3b2004-02-23 23:54:43 +0000188static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000189static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenke537b3b2004-02-23 23:54:43 +0000190static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
191static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
192static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
Stefan Roese12797482006-11-13 13:55:24 +0100193static void flash_read_jedec_ids (flash_info_t * info);
wdenke65527f2004-02-12 00:47:09 +0000194static int flash_detect_cfi (flash_info_t * info);
wdenke537b3b2004-02-23 23:54:43 +0000195static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenke65527f2004-02-12 00:47:09 +0000196static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
197 ulong tout, char *prompt);
Stefan Roese896391f2006-03-01 17:00:49 +0100198ulong flash_get_size (ulong base, int banknum);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200199#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000200static flash_info_t *flash_get_info(ulong base);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200201#endif
wdenk2cefd152004-02-08 22:55:38 +0000202#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke537b3b2004-02-23 23:54:43 +0000203static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk2cefd152004-02-08 22:55:38 +0000204#endif
205
wdenke65527f2004-02-12 00:47:09 +0000206/*-----------------------------------------------------------------------
207 * create an address based on the offset and the port width
208 */
wdenke537b3b2004-02-23 23:54:43 +0000209inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000210{
211 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
212}
213
wdenk2cefd152004-02-08 22:55:38 +0000214#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000215/*-----------------------------------------------------------------------
216 * Debug support
217 */
218void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000219{
220 int i;
221 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000222
223 cp = (unsigned char *) &data;
224 for (i = 0; i < 8; i++)
225 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000226}
wdenke65527f2004-02-12 00:47:09 +0000227static void flash_printqry (flash_info_t * info, flash_sect_t sect)
228{
229 cfiptr_t cptr;
230 int x, y;
231
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200232 for (x = 0; x < 0x40; x += 16U / info->portwidth) {
wdenke65527f2004-02-12 00:47:09 +0000233 cptr.cp =
234 flash_make_addr (info, sect,
235 x + FLASH_OFFSET_CFI_RESP);
236 debug ("%p : ", cptr.cp);
237 for (y = 0; y < 16; y++) {
238 debug ("%2.2x ", cptr.cp[y]);
239 }
240 debug (" ");
241 for (y = 0; y < 16; y++) {
242 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
243 debug ("%c", cptr.cp[y]);
244 } else {
245 debug (".");
246 }
247 }
248 debug ("\n");
249 }
250}
wdenk2cefd152004-02-08 22:55:38 +0000251#endif
252
253
254/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000255 * read a character at a port width address
256 */
wdenke65527f2004-02-12 00:47:09 +0000257inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000258{
259 uchar *cp;
wdenke65527f2004-02-12 00:47:09 +0000260
261 cp = flash_make_addr (info, 0, offset);
Heiko Schocher800db312007-01-19 18:05:26 +0100262#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenke65527f2004-02-12 00:47:09 +0000263 return (cp[0]);
264#else
wdenk2cefd152004-02-08 22:55:38 +0000265 return (cp[info->portwidth - 1]);
wdenke65527f2004-02-12 00:47:09 +0000266#endif
wdenk2cefd152004-02-08 22:55:38 +0000267}
268
269/*-----------------------------------------------------------------------
270 * read a short word by swapping for ppc format.
271 */
wdenke65527f2004-02-12 00:47:09 +0000272ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000273{
wdenke65527f2004-02-12 00:47:09 +0000274 uchar *addr;
275 ushort retval;
wdenk2cefd152004-02-08 22:55:38 +0000276
wdenke65527f2004-02-12 00:47:09 +0000277#ifdef DEBUG
278 int x;
279#endif
280 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000281
wdenke65527f2004-02-12 00:47:09 +0000282#ifdef DEBUG
283 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
284 info->portwidth);
285 for (x = 0; x < 2 * info->portwidth; x++) {
286 debug ("addr[%x] = 0x%x\n", x, addr[x]);
287 }
288#endif
Heiko Schocher800db312007-01-19 18:05:26 +0100289#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenke65527f2004-02-12 00:47:09 +0000290 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
291#else
292 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
293 addr[info->portwidth - 1]);
294#endif
295
296 debug ("retval = 0x%x\n", retval);
297 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000298}
299
300/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +0100301 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000302 * port size word. Swap for ppc format.
303 */
wdenke65527f2004-02-12 00:47:09 +0000304ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000305{
wdenke65527f2004-02-12 00:47:09 +0000306 uchar *addr;
307 ulong retval;
308
309#ifdef DEBUG
310 int x;
311#endif
312 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000313
wdenke65527f2004-02-12 00:47:09 +0000314#ifdef DEBUG
315 debug ("long addr is at %p info->portwidth = %d\n", addr,
316 info->portwidth);
317 for (x = 0; x < 4 * info->portwidth; x++) {
318 debug ("addr[%x] = 0x%x\n", x, addr[x]);
319 }
320#endif
Heiko Schocher800db312007-01-19 18:05:26 +0100321#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenke65527f2004-02-12 00:47:09 +0000322 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenke537b3b2004-02-23 23:54:43 +0000323 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenke65527f2004-02-12 00:47:09 +0000324#else
325 retval = (addr[(2 * info->portwidth) - 1] << 24) |
326 (addr[(info->portwidth) - 1] << 16) |
327 (addr[(4 * info->portwidth) - 1] << 8) |
328 addr[(3 * info->portwidth) - 1];
329#endif
330 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000331}
332
Stefan Roesec865e6c2006-02-28 15:29:58 +0100333
wdenk2cefd152004-02-08 22:55:38 +0000334/*-----------------------------------------------------------------------
335 */
336unsigned long flash_init (void)
337{
338 unsigned long size = 0;
339 int i;
340
Stefan Roeseefef95b2006-04-01 13:41:03 +0200341#ifdef CFG_FLASH_PROTECTION
342 char *s = getenv("unlock");
343#endif
344
wdenk2cefd152004-02-08 22:55:38 +0000345 /* Init: no FLASHes known */
wdenke65527f2004-02-12 00:47:09 +0000346 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000347 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenke65527f2004-02-12 00:47:09 +0000348 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk2cefd152004-02-08 22:55:38 +0000349 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Stefan Roesec443fe92005-11-22 13:20:42 +0100350#ifndef CFG_FLASH_QUIET_TEST
wdenke537b3b2004-02-23 23:54:43 +0000351 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
Wolfgang Denkafa0dd02006-12-27 01:26:13 +0100352 i+1, flash_info[i].size, flash_info[i].size << 20);
Stefan Roesec443fe92005-11-22 13:20:42 +0100353#endif /* CFG_FLASH_QUIET_TEST */
wdenk2cefd152004-02-08 22:55:38 +0000354 }
Stefan Roesec865e6c2006-02-28 15:29:58 +0100355#ifdef CFG_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +0200356 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
357 /*
358 * Only the U-Boot image and it's environment is protected,
359 * all other sectors are unprotected (unlocked) if flash
360 * hardware protection is used (CFG_FLASH_PROTECTION) and
361 * the environment variable "unlock" is set to "yes".
362 */
363 if (flash_info[i].legacy_unlock) {
364 int k;
Stefan Roesec865e6c2006-02-28 15:29:58 +0100365
Stefan Roeseefef95b2006-04-01 13:41:03 +0200366 /*
367 * Disable legacy_unlock temporarily, since
368 * flash_real_protect would relock all other sectors
369 * again otherwise.
370 */
371 flash_info[i].legacy_unlock = 0;
372
373 /*
374 * Legacy unlocking (e.g. Intel J3) -> unlock only one
375 * sector. This will unlock all sectors.
376 */
377 flash_real_protect (&flash_info[i], 0, 0);
378
379 flash_info[i].legacy_unlock = 1;
380
381 /*
382 * Manually mark other sectors as unlocked (unprotected)
383 */
384 for (k = 1; k < flash_info[i].sector_count; k++)
385 flash_info[i].protect[k] = 0;
386 } else {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100387 /*
Stefan Roeseefef95b2006-04-01 13:41:03 +0200388 * No legancy unlocking -> unlock all sectors
Stefan Roesec865e6c2006-02-28 15:29:58 +0100389 */
390 flash_protect (FLAG_PROTECT_CLEAR,
391 flash_info[i].start[0],
392 flash_info[i].start[0] + flash_info[i].size - 1,
393 &flash_info[i]);
394 }
395 }
396#endif /* CFG_FLASH_PROTECTION */
wdenk2cefd152004-02-08 22:55:38 +0000397 }
398
399 /* Monitor protection ON by default */
400#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenke65527f2004-02-12 00:47:09 +0000401 flash_protect (FLAG_PROTECT_SET,
402 CFG_MONITOR_BASE,
wdenk5c71a7a2005-05-16 15:23:22 +0000403 CFG_MONITOR_BASE + monitor_flash_len - 1,
404 flash_get_info(CFG_MONITOR_BASE));
wdenk2cefd152004-02-08 22:55:38 +0000405#endif
406
wdenke85b7a52004-10-10 22:16:06 +0000407 /* Environment protection ON by default */
408#ifdef CFG_ENV_IS_IN_FLASH
409 flash_protect (FLAG_PROTECT_SET,
410 CFG_ENV_ADDR,
411 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000412 flash_get_info(CFG_ENV_ADDR));
wdenke85b7a52004-10-10 22:16:06 +0000413#endif
414
415 /* Redundant environment protection ON by default */
416#ifdef CFG_ENV_ADDR_REDUND
417 flash_protect (FLAG_PROTECT_SET,
418 CFG_ENV_ADDR_REDUND,
419 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000420 flash_get_info(CFG_ENV_ADDR_REDUND));
wdenke85b7a52004-10-10 22:16:06 +0000421#endif
wdenk2cefd152004-02-08 22:55:38 +0000422 return (size);
423}
424
425/*-----------------------------------------------------------------------
426 */
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200427#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000428static flash_info_t *flash_get_info(ulong base)
429{
430 int i;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200431 flash_info_t * info = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000432
433 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
434 info = & flash_info[i];
435 if (info->size && info->start[0] <= base &&
436 base <= info->start[0] + info->size - 1)
437 break;
438 }
439
440 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
441}
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200442#endif
wdenk5c71a7a2005-05-16 15:23:22 +0000443
444/*-----------------------------------------------------------------------
445 */
wdenke65527f2004-02-12 00:47:09 +0000446int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +0000447{
448 int rcode = 0;
449 int prot;
450 flash_sect_t sect;
451
wdenke65527f2004-02-12 00:47:09 +0000452 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000453 puts ("Can't erase unknown flash type - aborted\n");
wdenk2cefd152004-02-08 22:55:38 +0000454 return 1;
455 }
456 if ((s_first < 0) || (s_first > s_last)) {
wdenk42c05472004-03-23 22:14:11 +0000457 puts ("- no sectors to erase\n");
wdenk2cefd152004-02-08 22:55:38 +0000458 return 1;
459 }
460
461 prot = 0;
wdenke65527f2004-02-12 00:47:09 +0000462 for (sect = s_first; sect <= s_last; ++sect) {
wdenk2cefd152004-02-08 22:55:38 +0000463 if (info->protect[sect]) {
464 prot++;
465 }
466 }
467 if (prot) {
wdenke65527f2004-02-12 00:47:09 +0000468 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk2cefd152004-02-08 22:55:38 +0000469 } else {
wdenk42c05472004-03-23 22:14:11 +0000470 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000471 }
472
473
wdenke65527f2004-02-12 00:47:09 +0000474 for (sect = s_first; sect <= s_last; sect++) {
wdenk2cefd152004-02-08 22:55:38 +0000475 if (info->protect[sect] == 0) { /* not protected */
wdenke65527f2004-02-12 00:47:09 +0000476 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000477 case CFI_CMDSET_INTEL_STANDARD:
478 case CFI_CMDSET_INTEL_EXTENDED:
wdenke537b3b2004-02-23 23:54:43 +0000479 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
480 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
481 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk2cefd152004-02-08 22:55:38 +0000482 break;
483 case CFI_CMDSET_AMD_STANDARD:
484 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000485 flash_unlock_seq (info, sect);
wdenked2ac4b2004-03-14 18:23:55 +0000486 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
487 AMD_CMD_ERASE_START);
wdenke65527f2004-02-12 00:47:09 +0000488 flash_unlock_seq (info, sect);
wdenke537b3b2004-02-23 23:54:43 +0000489 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk2cefd152004-02-08 22:55:38 +0000490 break;
491 default:
wdenke65527f2004-02-12 00:47:09 +0000492 debug ("Unkown flash vendor %d\n",
493 info->vendor);
wdenk2cefd152004-02-08 22:55:38 +0000494 break;
495 }
496
wdenke65527f2004-02-12 00:47:09 +0000497 if (flash_full_status_check
498 (info, sect, info->erase_blk_tout, "erase")) {
wdenk2cefd152004-02-08 22:55:38 +0000499 rcode = 1;
500 } else
wdenk42c05472004-03-23 22:14:11 +0000501 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +0000502 }
503 }
wdenk42c05472004-03-23 22:14:11 +0000504 puts (" done\n");
wdenk2cefd152004-02-08 22:55:38 +0000505 return rcode;
506}
507
508/*-----------------------------------------------------------------------
509 */
wdenke65527f2004-02-12 00:47:09 +0000510void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +0000511{
512 int i;
513
514 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000515 puts ("missing or unknown FLASH type\n");
wdenk2cefd152004-02-08 22:55:38 +0000516 return;
517 }
518
wdenke65527f2004-02-12 00:47:09 +0000519 printf ("CFI conformant FLASH (%d x %d)",
520 (info->portwidth << 3), (info->chipwidth << 3));
wdenk2cefd152004-02-08 22:55:38 +0000521 printf (" Size: %ld MB in %d Sectors\n",
522 info->size >> 20, info->sector_count);
Stefan Roese12797482006-11-13 13:55:24 +0100523 printf (" ");
524 switch (info->vendor) {
525 case CFI_CMDSET_INTEL_STANDARD:
526 printf ("Intel Standard");
527 break;
528 case CFI_CMDSET_INTEL_EXTENDED:
529 printf ("Intel Extended");
530 break;
531 case CFI_CMDSET_AMD_STANDARD:
532 printf ("AMD Standard");
533 break;
534 case CFI_CMDSET_AMD_EXTENDED:
535 printf ("AMD Extended");
536 break;
537 default:
538 printf ("Unknown (%d)", info->vendor);
539 break;
540 }
541 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
542 info->manufacturer_id, info->device_id);
543 if (info->device_id == 0x7E) {
544 printf("%04X", info->device_id2);
545 }
546 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
wdenke537b3b2004-02-23 23:54:43 +0000547 info->erase_blk_tout,
Stefan Roese12797482006-11-13 13:55:24 +0100548 info->write_tout);
549 if (info->buffer_size > 1) {
550 printf (" Buffer write timeout: %ld ms, buffer size: %d bytes\n",
wdenke537b3b2004-02-23 23:54:43 +0000551 info->buffer_write_tout,
552 info->buffer_size);
Stefan Roese12797482006-11-13 13:55:24 +0100553 }
wdenk2cefd152004-02-08 22:55:38 +0000554
Stefan Roese12797482006-11-13 13:55:24 +0100555 puts ("\n Sector Start Addresses:");
wdenke65527f2004-02-12 00:47:09 +0000556 for (i = 0; i < info->sector_count; ++i) {
Stefan Roese12797482006-11-13 13:55:24 +0100557 if ((i % 5) == 0)
558 printf ("\n");
wdenk2cefd152004-02-08 22:55:38 +0000559#ifdef CFG_FLASH_EMPTY_INFO
560 int k;
561 int size;
562 int erased;
563 volatile unsigned long *flash;
564
565 /*
566 * Check if whole sector is erased
567 */
wdenke65527f2004-02-12 00:47:09 +0000568 if (i != (info->sector_count - 1))
569 size = info->start[i + 1] - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000570 else
wdenke65527f2004-02-12 00:47:09 +0000571 size = info->start[0] + info->size - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000572 erased = 1;
wdenke65527f2004-02-12 00:47:09 +0000573 flash = (volatile unsigned long *) info->start[i];
574 size = size >> 2; /* divide by 4 for longword access */
575 for (k = 0; k < size; k++) {
576 if (*flash++ != 0xffffffff) {
577 erased = 0;
578 break;
579 }
580 }
wdenk2cefd152004-02-08 22:55:38 +0000581
wdenk2cefd152004-02-08 22:55:38 +0000582 /* print empty and read-only info */
Stefan Roese12797482006-11-13 13:55:24 +0100583 printf (" %08lX %c %s ",
wdenk2cefd152004-02-08 22:55:38 +0000584 info->start[i],
Stefan Roese12797482006-11-13 13:55:24 +0100585 erased ? 'E' : ' ',
586 info->protect[i] ? "RO" : " ");
Wolfgang Denkd3abe5a2005-09-25 00:23:05 +0200587#else /* ! CFG_FLASH_EMPTY_INFO */
Stefan Roese12797482006-11-13 13:55:24 +0100588 printf (" %08lX %s ",
589 info->start[i],
590 info->protect[i] ? "RO" : " ");
wdenk2cefd152004-02-08 22:55:38 +0000591#endif
592 }
wdenk42c05472004-03-23 22:14:11 +0000593 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000594 return;
595}
596
597/*-----------------------------------------------------------------------
598 * Copy memory to flash, returns:
599 * 0 - OK
600 * 1 - write timeout
601 * 2 - Flash not erased
602 */
wdenke65527f2004-02-12 00:47:09 +0000603int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +0000604{
605 ulong wp;
606 ulong cp;
607 int aln;
608 cfiword_t cword;
609 int i, rc;
610
wdenke65527f2004-02-12 00:47:09 +0000611#ifdef CFG_FLASH_USE_BUFFER_WRITE
612 int buffered_size;
613#endif
wdenke65527f2004-02-12 00:47:09 +0000614 /* get lower aligned address */
wdenk2cefd152004-02-08 22:55:38 +0000615 /* get lower aligned address */
616 wp = (addr & ~(info->portwidth - 1));
617
618 /* handle unaligned start */
wdenke65527f2004-02-12 00:47:09 +0000619 if ((aln = addr - wp) != 0) {
wdenk2cefd152004-02-08 22:55:38 +0000620 cword.l = 0;
621 cp = wp;
wdenke65527f2004-02-12 00:47:09 +0000622 for (i = 0; i < aln; ++i, ++cp)
623 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000624
wdenke65527f2004-02-12 00:47:09 +0000625 for (; (i < info->portwidth) && (cnt > 0); i++) {
626 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000627 cnt--;
628 cp++;
629 }
wdenke65527f2004-02-12 00:47:09 +0000630 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
631 flash_add_byte (info, &cword, (*(uchar *) cp));
632 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000633 return rc;
634 wp = cp;
635 }
636
wdenke65527f2004-02-12 00:47:09 +0000637 /* handle the aligned part */
wdenk2cefd152004-02-08 22:55:38 +0000638#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke65527f2004-02-12 00:47:09 +0000639 buffered_size = (info->portwidth / info->chipwidth);
640 buffered_size *= info->buffer_size;
641 while (cnt >= info->portwidth) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100642 /* prohibit buffer write when buffer_size is 1 */
643 if (info->buffer_size == 1) {
644 cword.l = 0;
645 for (i = 0; i < info->portwidth; i++)
646 flash_add_byte (info, &cword, *src++);
647 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
648 return rc;
649 wp += info->portwidth;
650 cnt -= info->portwidth;
651 continue;
652 }
653
654 /* write buffer until next buffered_size aligned boundary */
655 i = buffered_size - (wp % buffered_size);
656 if (i > cnt)
657 i = cnt;
wdenke65527f2004-02-12 00:47:09 +0000658 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk2cefd152004-02-08 22:55:38 +0000659 return rc;
Wolfgang Denke25f0522005-08-12 22:35:59 +0200660 i -= i & (info->portwidth - 1);
wdenk2cefd152004-02-08 22:55:38 +0000661 wp += i;
662 src += i;
wdenke65527f2004-02-12 00:47:09 +0000663 cnt -= i;
wdenk2cefd152004-02-08 22:55:38 +0000664 }
665#else
wdenke65527f2004-02-12 00:47:09 +0000666 while (cnt >= info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000667 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000668 for (i = 0; i < info->portwidth; i++) {
669 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000670 }
wdenke65527f2004-02-12 00:47:09 +0000671 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000672 return rc;
673 wp += info->portwidth;
674 cnt -= info->portwidth;
675 }
676#endif /* CFG_FLASH_USE_BUFFER_WRITE */
677 if (cnt == 0) {
678 return (0);
679 }
680
681 /*
682 * handle unaligned tail bytes
683 */
684 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000685 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
686 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000687 --cnt;
688 }
wdenke65527f2004-02-12 00:47:09 +0000689 for (; i < info->portwidth; ++i, ++cp) {
690 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000691 }
692
wdenke65527f2004-02-12 00:47:09 +0000693 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +0000694}
695
696/*-----------------------------------------------------------------------
697 */
698#ifdef CFG_FLASH_PROTECTION
699
wdenke65527f2004-02-12 00:47:09 +0000700int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +0000701{
702 int retcode = 0;
703
wdenke65527f2004-02-12 00:47:09 +0000704 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
705 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
706 if (prot)
707 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk2cefd152004-02-08 22:55:38 +0000708 else
wdenke65527f2004-02-12 00:47:09 +0000709 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk2cefd152004-02-08 22:55:38 +0000710
wdenke65527f2004-02-12 00:47:09 +0000711 if ((retcode =
712 flash_full_status_check (info, sector, info->erase_blk_tout,
713 prot ? "protect" : "unprotect")) == 0) {
wdenk2cefd152004-02-08 22:55:38 +0000714
715 info->protect[sector] = prot;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200716
717 /*
718 * On some of Intel's flash chips (marked via legacy_unlock)
719 * unprotect unprotects all locking.
720 */
721 if ((prot == 0) && (info->legacy_unlock)) {
wdenk2cefd152004-02-08 22:55:38 +0000722 flash_sect_t i;
wdenke65527f2004-02-12 00:47:09 +0000723
724 for (i = 0; i < info->sector_count; i++) {
725 if (info->protect[i])
726 flash_real_protect (info, i, 1);
wdenk2cefd152004-02-08 22:55:38 +0000727 }
728 }
729 }
wdenk2cefd152004-02-08 22:55:38 +0000730 return retcode;
wdenke65527f2004-02-12 00:47:09 +0000731}
732
wdenk2cefd152004-02-08 22:55:38 +0000733/*-----------------------------------------------------------------------
734 * flash_read_user_serial - read the OneTimeProgramming cells
735 */
wdenke65527f2004-02-12 00:47:09 +0000736void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
737 int len)
wdenk2cefd152004-02-08 22:55:38 +0000738{
wdenke65527f2004-02-12 00:47:09 +0000739 uchar *src;
740 uchar *dst;
wdenk2cefd152004-02-08 22:55:38 +0000741
742 dst = buffer;
wdenke65527f2004-02-12 00:47:09 +0000743 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
744 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
745 memcpy (dst, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200746 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000747}
wdenke65527f2004-02-12 00:47:09 +0000748
wdenk2cefd152004-02-08 22:55:38 +0000749/*
750 * flash_read_factory_serial - read the device Id from the protection area
751 */
wdenke65527f2004-02-12 00:47:09 +0000752void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
753 int len)
wdenk2cefd152004-02-08 22:55:38 +0000754{
wdenke65527f2004-02-12 00:47:09 +0000755 uchar *src;
wdenk6cfa84e2004-02-10 00:03:41 +0000756
wdenke65527f2004-02-12 00:47:09 +0000757 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
758 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
759 memcpy (buffer, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200760 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000761}
762
763#endif /* CFG_FLASH_PROTECTION */
764
wdenke65527f2004-02-12 00:47:09 +0000765/*
766 * flash_is_busy - check to see if the flash is busy
767 * This routine checks the status of the chip and returns true if the chip is busy
768 */
769static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000770{
771 int retval;
wdenke65527f2004-02-12 00:47:09 +0000772
773 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000774 case CFI_CMDSET_INTEL_STANDARD:
775 case CFI_CMDSET_INTEL_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000776 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk2cefd152004-02-08 22:55:38 +0000777 break;
778 case CFI_CMDSET_AMD_STANDARD:
779 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000780 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk2cefd152004-02-08 22:55:38 +0000781 break;
782 default:
783 retval = 0;
784 }
wdenke65527f2004-02-12 00:47:09 +0000785 debug ("flash_is_busy: %d\n", retval);
wdenk2cefd152004-02-08 22:55:38 +0000786 return retval;
787}
wdenke65527f2004-02-12 00:47:09 +0000788
wdenk2cefd152004-02-08 22:55:38 +0000789/*-----------------------------------------------------------------------
790 * wait for XSR.7 to be set. Time out with an error if it does not.
791 * This routine does not set the flash to read-array mode.
792 */
wdenke65527f2004-02-12 00:47:09 +0000793static int flash_status_check (flash_info_t * info, flash_sect_t sector,
794 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000795{
796 ulong start;
797
Stefan Roeseefef95b2006-04-01 13:41:03 +0200798#if CFG_HZ != 1000
799 tout *= CFG_HZ/1000;
800#endif
801
wdenk2cefd152004-02-08 22:55:38 +0000802 /* Wait for command completion */
803 start = get_timer (0);
wdenke65527f2004-02-12 00:47:09 +0000804 while (flash_is_busy (info, sector)) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100805 if (get_timer (start) > tout) {
wdenke65527f2004-02-12 00:47:09 +0000806 printf ("Flash %s timeout at address %lx data %lx\n",
807 prompt, info->start[sector],
808 flash_read_long (info, sector, 0));
809 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000810 return ERR_TIMOUT;
811 }
Wolfgang Denkc9f8d7c2006-06-02 11:46:20 +0200812 udelay (1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000813 }
814 return ERR_OK;
815}
wdenke65527f2004-02-12 00:47:09 +0000816
wdenk2cefd152004-02-08 22:55:38 +0000817/*-----------------------------------------------------------------------
818 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
819 * This routine sets the flash to read-array mode.
820 */
wdenke65527f2004-02-12 00:47:09 +0000821static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
822 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000823{
824 int retcode;
wdenke65527f2004-02-12 00:47:09 +0000825
826 retcode = flash_status_check (info, sector, tout, prompt);
827 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000828 case CFI_CMDSET_INTEL_EXTENDED:
829 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesec865e6c2006-02-28 15:29:58 +0100830 if ((retcode == ERR_OK)
wdenke65527f2004-02-12 00:47:09 +0000831 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk2cefd152004-02-08 22:55:38 +0000832 retcode = ERR_INVAL;
wdenke65527f2004-02-12 00:47:09 +0000833 printf ("Flash %s error at address %lx\n", prompt,
834 info->start[sector]);
wdenke537b3b2004-02-23 23:54:43 +0000835 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000836 puts ("Command Sequence Error.\n");
wdenke537b3b2004-02-23 23:54:43 +0000837 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000838 puts ("Block Erase Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000839 retcode = ERR_NOT_ERASED;
wdenke537b3b2004-02-23 23:54:43 +0000840 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000841 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000842 }
wdenke65527f2004-02-12 00:47:09 +0000843 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk42c05472004-03-23 22:14:11 +0000844 puts ("Block locked.\n");
wdenke65527f2004-02-12 00:47:09 +0000845 retcode = ERR_PROTECTED;
846 }
847 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk42c05472004-03-23 22:14:11 +0000848 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000849 }
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200850 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000851 break;
852 default:
853 break;
854 }
855 return retcode;
856}
wdenke65527f2004-02-12 00:47:09 +0000857
wdenk2cefd152004-02-08 22:55:38 +0000858/*-----------------------------------------------------------------------
859 */
wdenke65527f2004-02-12 00:47:09 +0000860static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000861{
Heiko Schocher800db312007-01-19 18:05:26 +0100862#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk369d43d2004-03-14 14:09:05 +0000863 unsigned short w;
864 unsigned int l;
865 unsigned long long ll;
866#endif
867
wdenke65527f2004-02-12 00:47:09 +0000868 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000869 case FLASH_CFI_8BIT:
870 cword->c = c;
871 break;
872 case FLASH_CFI_16BIT:
Heiko Schocher800db312007-01-19 18:05:26 +0100873#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk369d43d2004-03-14 14:09:05 +0000874 w = c;
875 w <<= 8;
876 cword->w = (cword->w >> 8) | w;
877#else
wdenk2cefd152004-02-08 22:55:38 +0000878 cword->w = (cword->w << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000879#endif
wdenk2cefd152004-02-08 22:55:38 +0000880 break;
881 case FLASH_CFI_32BIT:
Heiko Schocher800db312007-01-19 18:05:26 +0100882#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk369d43d2004-03-14 14:09:05 +0000883 l = c;
884 l <<= 24;
885 cword->l = (cword->l >> 8) | l;
886#else
wdenk2cefd152004-02-08 22:55:38 +0000887 cword->l = (cword->l << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000888#endif
wdenk2cefd152004-02-08 22:55:38 +0000889 break;
890 case FLASH_CFI_64BIT:
Heiko Schocher800db312007-01-19 18:05:26 +0100891#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk369d43d2004-03-14 14:09:05 +0000892 ll = c;
893 ll <<= 56;
894 cword->ll = (cword->ll >> 8) | ll;
895#else
wdenk2cefd152004-02-08 22:55:38 +0000896 cword->ll = (cword->ll << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000897#endif
wdenk2cefd152004-02-08 22:55:38 +0000898 break;
899 }
900}
901
902
903/*-----------------------------------------------------------------------
904 * make a proper sized command based on the port and chip widths
905 */
wdenke65527f2004-02-12 00:47:09 +0000906static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk2cefd152004-02-08 22:55:38 +0000907{
908 int i;
wdenke65527f2004-02-12 00:47:09 +0000909 uchar *cp = (uchar *) cmdbuf;
910
Heiko Schocher800db312007-01-19 18:05:26 +0100911#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
Wolfgang Denk07bdb022005-09-24 23:32:48 +0200912 for (i = info->portwidth; i > 0; i--)
913#else
914 for (i = 1; i <= info->portwidth; i++)
wdenke65527f2004-02-12 00:47:09 +0000915#endif
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200916 *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
wdenk2cefd152004-02-08 22:55:38 +0000917}
918
919/*
920 * Write a proper sized command to the correct address
921 */
wdenke537b3b2004-02-23 23:54:43 +0000922static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000923{
924
925 volatile cfiptr_t addr;
926 cfiword_t cword;
wdenke65527f2004-02-12 00:47:09 +0000927
928 addr.cp = flash_make_addr (info, sect, offset);
929 flash_make_cmd (info, cmd, &cword);
930 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000931 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000932 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
933 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk2cefd152004-02-08 22:55:38 +0000934 *addr.cp = cword.c;
935 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;
941 break;
942 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000943 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
944 cmd, cword.l,
wdenk2cefd152004-02-08 22:55:38 +0000945 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
946 *addr.lp = cword.l;
947 break;
948 case FLASH_CFI_64BIT:
949#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000950 {
wdenk2cefd152004-02-08 22:55:38 +0000951 char str[20];
wdenk6cfa84e2004-02-10 00:03:41 +0000952
wdenke65527f2004-02-12 00:47:09 +0000953 print_longlong (str, cword.ll);
954
955 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
956 addr.llp, cmd, str,
wdenk2cefd152004-02-08 22:55:38 +0000957 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
958 }
959#endif
960 *addr.llp = cword.ll;
961 break;
962 }
Haiying Wangc123a382007-02-21 16:52:31 +0100963
964 /* Ensure all the instructions are fully finished */
965 sync();
wdenk2cefd152004-02-08 22:55:38 +0000966}
967
wdenke65527f2004-02-12 00:47:09 +0000968static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000969{
wdenked2ac4b2004-03-14 18:23:55 +0000970 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
971 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
wdenk2cefd152004-02-08 22:55:38 +0000972}
wdenke65527f2004-02-12 00:47:09 +0000973
wdenk2cefd152004-02-08 22:55:38 +0000974/*-----------------------------------------------------------------------
975 */
wdenke537b3b2004-02-23 23:54:43 +0000976static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000977{
978 cfiptr_t cptr;
979 cfiword_t cword;
980 int retval;
wdenke65527f2004-02-12 00:47:09 +0000981
982 cptr.cp = flash_make_addr (info, sect, offset);
983 flash_make_cmd (info, cmd, &cword);
wdenk2cefd152004-02-08 22:55:38 +0000984
wdenke65527f2004-02-12 00:47:09 +0000985 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
986 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000987 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000988 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk2cefd152004-02-08 22:55:38 +0000989 retval = (cptr.cp[0] == cword.c);
990 break;
991 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000992 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk2cefd152004-02-08 22:55:38 +0000993 retval = (cptr.wp[0] == cword.w);
994 break;
995 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000996 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk2cefd152004-02-08 22:55:38 +0000997 retval = (cptr.lp[0] == cword.l);
998 break;
999 case FLASH_CFI_64BIT:
wdenk6cfa84e2004-02-10 00:03:41 +00001000#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +00001001 {
wdenk2cefd152004-02-08 22:55:38 +00001002 char str1[20];
1003 char str2[20];
wdenke65527f2004-02-12 00:47:09 +00001004
1005 print_longlong (str1, cptr.llp[0]);
1006 print_longlong (str2, cword.ll);
1007 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +00001008 }
1009#endif
1010 retval = (cptr.llp[0] == cword.ll);
1011 break;
1012 default:
1013 retval = 0;
1014 break;
1015 }
1016 return retval;
1017}
wdenke65527f2004-02-12 00:47:09 +00001018
wdenk2cefd152004-02-08 22:55:38 +00001019/*-----------------------------------------------------------------------
1020 */
wdenke537b3b2004-02-23 23:54:43 +00001021static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001022{
1023 cfiptr_t cptr;
1024 cfiword_t cword;
1025 int retval;
wdenke65527f2004-02-12 00:47:09 +00001026
1027 cptr.cp = flash_make_addr (info, sect, offset);
1028 flash_make_cmd (info, cmd, &cword);
1029 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001030 case FLASH_CFI_8BIT:
1031 retval = ((cptr.cp[0] & cword.c) == cword.c);
1032 break;
1033 case FLASH_CFI_16BIT:
1034 retval = ((cptr.wp[0] & cword.w) == cword.w);
1035 break;
1036 case FLASH_CFI_32BIT:
1037 retval = ((cptr.lp[0] & cword.l) == cword.l);
1038 break;
1039 case FLASH_CFI_64BIT:
1040 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenke65527f2004-02-12 00:47:09 +00001041 break;
wdenk2cefd152004-02-08 22:55:38 +00001042 default:
1043 retval = 0;
1044 break;
1045 }
1046 return retval;
1047}
1048
1049/*-----------------------------------------------------------------------
1050 */
wdenke537b3b2004-02-23 23:54:43 +00001051static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001052{
1053 cfiptr_t cptr;
1054 cfiword_t cword;
1055 int retval;
wdenke65527f2004-02-12 00:47:09 +00001056
1057 cptr.cp = flash_make_addr (info, sect, offset);
1058 flash_make_cmd (info, cmd, &cword);
1059 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001060 case FLASH_CFI_8BIT:
1061 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
1062 break;
1063 case FLASH_CFI_16BIT:
1064 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
1065 break;
1066 case FLASH_CFI_32BIT:
1067 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
1068 break;
1069 case FLASH_CFI_64BIT:
wdenke65527f2004-02-12 00:47:09 +00001070 retval = ((cptr.llp[0] & cword.ll) !=
1071 (cptr.llp[0] & cword.ll));
wdenk2cefd152004-02-08 22:55:38 +00001072 break;
1073 default:
1074 retval = 0;
1075 break;
1076 }
1077 return retval;
1078}
1079
1080/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +01001081 * read jedec ids from device and set corresponding fields in info struct
1082 *
1083 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1084 *
1085*/
1086static void flash_read_jedec_ids (flash_info_t * info)
1087{
1088 info->manufacturer_id = 0;
1089 info->device_id = 0;
1090 info->device_id2 = 0;
1091
1092 switch (info->vendor) {
1093 case CFI_CMDSET_INTEL_STANDARD:
1094 case CFI_CMDSET_INTEL_EXTENDED:
1095 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1096 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1097 udelay(1000); /* some flash are slow to respond */
1098 info->manufacturer_id = flash_read_uchar (info,
1099 FLASH_OFFSET_MANUFACTURER_ID);
1100 info->device_id = flash_read_uchar (info,
1101 FLASH_OFFSET_DEVICE_ID);
1102 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1103 break;
1104 case CFI_CMDSET_AMD_STANDARD:
1105 case CFI_CMDSET_AMD_EXTENDED:
1106 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1107 flash_unlock_seq(info, 0);
1108 flash_write_cmd(info, 0, AMD_ADDR_START, FLASH_CMD_READ_ID);
1109 udelay(1000); /* some flash are slow to respond */
1110 info->manufacturer_id = flash_read_uchar (info,
1111 FLASH_OFFSET_MANUFACTURER_ID);
1112 info->device_id = flash_read_uchar (info,
1113 FLASH_OFFSET_DEVICE_ID);
1114 if (info->device_id == 0x7E) {
1115 /* AMD 3-byte (expanded) device ids */
1116 info->device_id2 = flash_read_uchar (info,
1117 FLASH_OFFSET_DEVICE_ID2);
1118 info->device_id2 <<= 8;
1119 info->device_id2 |= flash_read_uchar (info,
1120 FLASH_OFFSET_DEVICE_ID3);
1121 }
1122 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1123 break;
1124 default:
1125 break;
1126 }
1127}
1128
1129/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +00001130 * detect if flash is compatible with the Common Flash Interface (CFI)
1131 * http://www.jedec.org/download/search/jesd68.pdf
1132 *
1133*/
wdenke65527f2004-02-12 00:47:09 +00001134static int flash_detect_cfi (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001135{
Wolfgang Denkafa0dd02006-12-27 01:26:13 +01001136 int cfi_offset;
wdenke65527f2004-02-12 00:47:09 +00001137 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001138
Stefan Roesec865e6c2006-02-28 15:29:58 +01001139 for (info->portwidth = CFG_FLASH_CFI_WIDTH;
wdenke65527f2004-02-12 00:47:09 +00001140 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1141 for (info->chipwidth = FLASH_CFI_BY8;
1142 info->chipwidth <= info->portwidth;
1143 info->chipwidth <<= 1) {
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001144 flash_write_cmd (info, 0, 0, info->cmd_reset);
Wolfgang Denkafa0dd02006-12-27 01:26:13 +01001145 for (cfi_offset=0; cfi_offset < sizeof(flash_offset_cfi)/sizeof(uint); cfi_offset++) {
1146 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], FLASH_CMD_CFI);
1147 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1148 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1149 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1150 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
1151 info->cfi_offset=flash_offset_cfi[cfi_offset];
1152 debug ("device interface is %d\n",
1153 info->interface);
1154 debug ("found port %d chip %d ",
1155 info->portwidth, info->chipwidth);
1156 debug ("port %d bits chip %d bits\n",
1157 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1158 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1159 return 1;
1160 }
wdenk2cefd152004-02-08 22:55:38 +00001161 }
1162 }
1163 }
wdenke65527f2004-02-12 00:47:09 +00001164 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001165 return 0;
1166}
wdenke65527f2004-02-12 00:47:09 +00001167
wdenk2cefd152004-02-08 22:55:38 +00001168/*
1169 * The following code cannot be run from FLASH!
1170 *
1171 */
Marian Balakowicz513b4a12005-10-11 19:09:42 +02001172ulong flash_get_size (ulong base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001173{
wdenke65527f2004-02-12 00:47:09 +00001174 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00001175 int i, j;
1176 flash_sect_t sect_cnt;
1177 unsigned long sector;
1178 unsigned long tmp;
1179 int size_ratio;
1180 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00001181 int erase_region_size;
1182 int erase_region_count;
Stefan Roese12797482006-11-13 13:55:24 +01001183 int geometry_reversed = 0;
1184
1185 info->ext_addr = 0;
1186 info->cfi_version = 0;
Stefan Roeseefef95b2006-04-01 13:41:03 +02001187#ifdef CFG_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02001188 info->legacy_unlock = 0;
1189#endif
wdenk2cefd152004-02-08 22:55:38 +00001190
1191 info->start[0] = base;
1192
wdenke65527f2004-02-12 00:47:09 +00001193 if (flash_detect_cfi (info)) {
Stefan Roese12797482006-11-13 13:55:24 +01001194 info->vendor = flash_read_ushort (info, 0,
1195 FLASH_OFFSET_PRIMARY_VENDOR);
1196 flash_read_jedec_ids (info);
Stefano Babic5f172a82006-12-12 00:22:42 +01001197 flash_write_cmd (info, 0, info->cfi_offset, FLASH_CMD_CFI);
Stefan Roese12797482006-11-13 13:55:24 +01001198 num_erase_regions = flash_read_uchar (info,
1199 FLASH_OFFSET_NUM_ERASE_REGIONS);
1200 info->ext_addr = flash_read_ushort (info, 0,
1201 FLASH_OFFSET_EXT_QUERY_T_P_ADDR);
1202 if (info->ext_addr) {
1203 info->cfi_version = (ushort) flash_read_uchar (info,
1204 info->ext_addr + 3) << 8;
1205 info->cfi_version |= (ushort) flash_read_uchar (info,
1206 info->ext_addr + 4);
1207 }
wdenke65527f2004-02-12 00:47:09 +00001208#ifdef DEBUG
1209 flash_printqry (info, 0);
1210#endif
1211 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001212 case CFI_CMDSET_INTEL_STANDARD:
1213 case CFI_CMDSET_INTEL_EXTENDED:
1214 default:
1215 info->cmd_reset = FLASH_CMD_RESET;
Stefan Roeseefef95b2006-04-01 13:41:03 +02001216#ifdef CFG_FLASH_PROTECTION
1217 /* read legacy lock/unlock bit from intel flash */
Stefan Roese12797482006-11-13 13:55:24 +01001218 if (info->ext_addr) {
1219 info->legacy_unlock = flash_read_uchar (info,
1220 info->ext_addr + 5) & 0x08;
1221 }
Stefan Roeseefef95b2006-04-01 13:41:03 +02001222#endif
wdenk2cefd152004-02-08 22:55:38 +00001223 break;
1224 case CFI_CMDSET_AMD_STANDARD:
1225 case CFI_CMDSET_AMD_EXTENDED:
1226 info->cmd_reset = AMD_CMD_RESET;
Stefan Roese12797482006-11-13 13:55:24 +01001227 /* check if flash geometry needs reversal */
1228 if (num_erase_regions <= 1)
1229 break;
1230 /* reverse geometry if top boot part */
1231 if (info->cfi_version < 0x3131) {
1232 /* CFI < 1.1, try to guess from device id */
1233 if ((info->device_id & 0x80) != 0) {
1234 geometry_reversed = 1;
1235 }
1236 break;
1237 }
1238 /* CFI >= 1.1, deduct from top/bottom flag */
1239 /* note: ext_addr is valid since cfi_version > 0 */
1240 if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1241 geometry_reversed = 1;
1242 }
wdenk2cefd152004-02-08 22:55:38 +00001243 break;
1244 }
wdenk6cfa84e2004-02-10 00:03:41 +00001245
wdenke65527f2004-02-12 00:47:09 +00001246 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese12797482006-11-13 13:55:24 +01001247 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1248 debug ("device id is 0x%x\n", info->device_id);
1249 debug ("device id2 is 0x%x\n", info->device_id2);
1250 debug ("cfi version is 0x%04x\n", info->cfi_version);
1251
wdenk2cefd152004-02-08 22:55:38 +00001252 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00001253 /* if the chip is x8/x16 reduce the ratio by half */
1254 if ((info->interface == FLASH_CFI_X8X16)
1255 && (info->chipwidth == FLASH_CFI_BY8)) {
1256 size_ratio >>= 1;
1257 }
wdenke65527f2004-02-12 00:47:09 +00001258 debug ("size_ratio %d port %d bits chip %d bits\n",
1259 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1260 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1261 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00001262 sect_cnt = 0;
1263 sector = base;
wdenke65527f2004-02-12 00:47:09 +00001264 for (i = 0; i < num_erase_regions; i++) {
1265 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00001266 printf ("%d erase regions found, only %d used\n",
1267 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00001268 break;
1269 }
Stefan Roese12797482006-11-13 13:55:24 +01001270 if (geometry_reversed)
1271 tmp = flash_read_long (info, 0,
1272 FLASH_OFFSET_ERASE_REGIONS +
1273 (num_erase_regions - 1 - i) * 4);
1274 else
1275 tmp = flash_read_long (info, 0,
wdenke65527f2004-02-12 00:47:09 +00001276 FLASH_OFFSET_ERASE_REGIONS +
1277 i * 4);
1278 erase_region_size =
1279 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk2cefd152004-02-08 22:55:38 +00001280 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00001281 erase_region_count = (tmp & 0xffff) + 1;
wdenkaeba06f2004-06-09 17:34:58 +00001282 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00001283 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00001284 for (j = 0; j < erase_region_count; j++) {
wdenk2cefd152004-02-08 22:55:38 +00001285 info->start[sect_cnt] = sector;
1286 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00001287
1288 /*
1289 * Only read protection status from supported devices (intel...)
1290 */
1291 switch (info->vendor) {
1292 case CFI_CMDSET_INTEL_EXTENDED:
1293 case CFI_CMDSET_INTEL_STANDARD:
1294 info->protect[sect_cnt] =
1295 flash_isset (info, sect_cnt,
1296 FLASH_OFFSET_PROTECT,
1297 FLASH_STATUS_PROTECT);
1298 break;
1299 default:
1300 info->protect[sect_cnt] = 0; /* default: not protected */
1301 }
1302
wdenk2cefd152004-02-08 22:55:38 +00001303 sect_cnt++;
1304 }
1305 }
1306
1307 info->sector_count = sect_cnt;
1308 /* multiply the size by the number of chips */
wdenke537b3b2004-02-23 23:54:43 +00001309 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1310 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
wdenke65527f2004-02-12 00:47:09 +00001311 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenke537b3b2004-02-23 23:54:43 +00001312 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
Stefan Roeseefef95b2006-04-01 13:41:03 +02001313 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT)) *
1314 (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT));
1315 info->buffer_write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
Stefan Roesec865e6c2006-02-28 15:29:58 +01001316 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
1317 (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
1318 info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
wdenk2cefd152004-02-08 22:55:38 +00001319 info->flash_id = FLASH_MAN_CFI;
wdenked2ac4b2004-03-14 18:23:55 +00001320 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1321 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1322 }
wdenk2cefd152004-02-08 22:55:38 +00001323 }
1324
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001325 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenke65527f2004-02-12 00:47:09 +00001326 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00001327}
1328
Stefan Roesec865e6c2006-02-28 15:29:58 +01001329/* loop through the sectors from the highest address
1330 * when the passed address is greater or equal to the sector address
1331 * we have a match
1332 */
1333static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1334{
1335 flash_sect_t sector;
1336
1337 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1338 if (addr >= info->start[sector])
1339 break;
1340 }
1341 return sector;
1342}
wdenk2cefd152004-02-08 22:55:38 +00001343
1344/*-----------------------------------------------------------------------
1345 */
wdenke65527f2004-02-12 00:47:09 +00001346static int flash_write_cfiword (flash_info_t * info, ulong dest,
1347 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +00001348{
wdenk2cefd152004-02-08 22:55:38 +00001349 cfiptr_t ctladdr;
1350 cfiptr_t cptr;
1351 int flag;
1352
wdenke65527f2004-02-12 00:47:09 +00001353 ctladdr.cp = flash_make_addr (info, 0, 0);
1354 cptr.cp = (uchar *) dest;
wdenk2cefd152004-02-08 22:55:38 +00001355
wdenk2cefd152004-02-08 22:55:38 +00001356 /* Check if Flash is (sufficiently) erased */
wdenke65527f2004-02-12 00:47:09 +00001357 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001358 case FLASH_CFI_8BIT:
1359 flag = ((cptr.cp[0] & cword.c) == cword.c);
1360 break;
1361 case FLASH_CFI_16BIT:
1362 flag = ((cptr.wp[0] & cword.w) == cword.w);
1363 break;
1364 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +00001365 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk2cefd152004-02-08 22:55:38 +00001366 break;
1367 case FLASH_CFI_64BIT:
wdenk391b5742004-10-10 23:27:33 +00001368 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenk2cefd152004-02-08 22:55:38 +00001369 break;
1370 default:
1371 return 2;
1372 }
wdenke65527f2004-02-12 00:47:09 +00001373 if (!flag)
wdenk2cefd152004-02-08 22:55:38 +00001374 return 2;
1375
1376 /* Disable interrupts which might cause a timeout here */
wdenke65527f2004-02-12 00:47:09 +00001377 flag = disable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001378
wdenke65527f2004-02-12 00:47:09 +00001379 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001380 case CFI_CMDSET_INTEL_EXTENDED:
1381 case CFI_CMDSET_INTEL_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001382 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1383 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001384 break;
1385 case CFI_CMDSET_AMD_EXTENDED:
1386 case CFI_CMDSET_AMD_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001387 flash_unlock_seq (info, 0);
wdenked2ac4b2004-03-14 18:23:55 +00001388 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001389 break;
1390 }
1391
wdenke65527f2004-02-12 00:47:09 +00001392 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001393 case FLASH_CFI_8BIT:
1394 cptr.cp[0] = cword.c;
1395 break;
1396 case FLASH_CFI_16BIT:
1397 cptr.wp[0] = cword.w;
1398 break;
1399 case FLASH_CFI_32BIT:
1400 cptr.lp[0] = cword.l;
1401 break;
1402 case FLASH_CFI_64BIT:
1403 cptr.llp[0] = cword.ll;
1404 break;
1405 }
1406
1407 /* re-enable interrupts if necessary */
wdenke65527f2004-02-12 00:47:09 +00001408 if (flag)
1409 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001410
Stefan Roesec865e6c2006-02-28 15:29:58 +01001411 return flash_full_status_check (info, find_sector (info, dest),
1412 info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +00001413}
1414
1415#ifdef CFG_FLASH_USE_BUFFER_WRITE
1416
wdenke65527f2004-02-12 00:47:09 +00001417static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1418 int len)
wdenk2cefd152004-02-08 22:55:38 +00001419{
1420 flash_sect_t sector;
1421 int cnt;
1422 int retcode;
1423 volatile cfiptr_t src;
1424 volatile cfiptr_t dst;
1425
Stefan Roesec865e6c2006-02-28 15:29:58 +01001426 switch (info->vendor) {
1427 case CFI_CMDSET_INTEL_STANDARD:
1428 case CFI_CMDSET_INTEL_EXTENDED:
1429 src.cp = cp;
1430 dst.cp = (uchar *) dest;
1431 sector = find_sector (info, dest);
1432 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1433 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1434 if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
1435 "write to buffer")) == ERR_OK) {
1436 /* reduce the number of loops by the width of the port */
1437 switch (info->portwidth) {
1438 case FLASH_CFI_8BIT:
1439 cnt = len;
1440 break;
1441 case FLASH_CFI_16BIT:
1442 cnt = len >> 1;
1443 break;
1444 case FLASH_CFI_32BIT:
1445 cnt = len >> 2;
1446 break;
1447 case FLASH_CFI_64BIT:
1448 cnt = len >> 3;
1449 break;
1450 default:
1451 return ERR_INVAL;
1452 break;
1453 }
1454 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1455 while (cnt-- > 0) {
1456 switch (info->portwidth) {
1457 case FLASH_CFI_8BIT:
1458 *dst.cp++ = *src.cp++;
1459 break;
1460 case FLASH_CFI_16BIT:
1461 *dst.wp++ = *src.wp++;
1462 break;
1463 case FLASH_CFI_32BIT:
1464 *dst.lp++ = *src.lp++;
1465 break;
1466 case FLASH_CFI_64BIT:
1467 *dst.llp++ = *src.llp++;
1468 break;
1469 default:
1470 return ERR_INVAL;
1471 break;
1472 }
1473 }
1474 flash_write_cmd (info, sector, 0,
1475 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1476 retcode = flash_full_status_check (info, sector,
1477 info->buffer_write_tout,
1478 "buffer write");
1479 }
1480 return retcode;
1481
1482 case CFI_CMDSET_AMD_STANDARD:
1483 case CFI_CMDSET_AMD_EXTENDED:
1484 src.cp = cp;
1485 dst.cp = (uchar *) dest;
1486 sector = find_sector (info, dest);
1487
1488 flash_unlock_seq(info,0);
1489 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
1490
wdenke65527f2004-02-12 00:47:09 +00001491 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001492 case FLASH_CFI_8BIT:
1493 cnt = len;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001494 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1495 while (cnt-- > 0) *dst.cp++ = *src.cp++;
wdenk2cefd152004-02-08 22:55:38 +00001496 break;
1497 case FLASH_CFI_16BIT:
1498 cnt = len >> 1;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001499 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1500 while (cnt-- > 0) *dst.wp++ = *src.wp++;
wdenk2cefd152004-02-08 22:55:38 +00001501 break;
1502 case FLASH_CFI_32BIT:
1503 cnt = len >> 2;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001504 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1505 while (cnt-- > 0) *dst.lp++ = *src.lp++;
wdenk2cefd152004-02-08 22:55:38 +00001506 break;
1507 case FLASH_CFI_64BIT:
1508 cnt = len >> 3;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001509 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1510 while (cnt-- > 0) *dst.llp++ = *src.llp++;
wdenk2cefd152004-02-08 22:55:38 +00001511 break;
1512 default:
1513 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001514 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01001515
1516 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1517 retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
1518 "buffer write");
1519 return retcode;
1520
1521 default:
1522 debug ("Unknown Command Set\n");
1523 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001524 }
wdenk2cefd152004-02-08 22:55:38 +00001525}
wdenk5b835a32004-09-28 19:00:19 +00001526#endif /* CFG_FLASH_USE_BUFFER_WRITE */
Heiko Schocher3c58a992007-01-11 15:44:44 +01001527
wdenk2cefd152004-02-08 22:55:38 +00001528#endif /* CFG_FLASH_CFI */