blob: fd0a186828d4be171f6cfeb11e95133de214245d [file] [log] [blame]
wdenk5653fc32004-02-08 22:55:38 +00001/*
wdenkbf9e3b32004-02-12 00:47:09 +00002 * (C) Copyright 2002-2004
wdenk5653fc32004-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>
7 * Modified to work with AMD flashes
8 *
wdenkbf9e3b32004-02-12 00:47:09 +00009 * Copyright (C) 2004
10 * Ed Okerson
11 * Modified to work with little-endian systems.
12 *
wdenk5653fc32004-02-08 22:55:38 +000013 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 *
31 * History
32 * 01/20/2004 - combined variants of original driver.
wdenkbf9e3b32004-02-12 00:47:09 +000033 * 01/22/2004 - Write performance enhancements for parallel chips (Tolunay)
34 * 01/23/2004 - Support for x8/x16 chips (Rune Raknerud)
35 * 01/27/2004 - Little endian support Ed Okerson
wdenk5653fc32004-02-08 22:55:38 +000036 *
37 * Tested Architectures
wdenkbf9e3b32004-02-12 00:47:09 +000038 * Port Width Chip Width # of banks Flash Chip Board
wdenk2d1a5372004-02-23 19:30:57 +000039 * 32 16 1 28F128J3 seranoa/eagle
40 * 64 16 1 28F128J3 seranoa/falcon
wdenkcd37d9e2004-02-10 00:03:41 +000041 *
wdenk5653fc32004-02-08 22:55:38 +000042 */
43
44/* The DEBUG define must be before common to enable debugging */
wdenk2d1a5372004-02-23 19:30:57 +000045/* #define DEBUG */
46
wdenk5653fc32004-02-08 22:55:38 +000047#include <common.h>
48#include <asm/processor.h>
wdenk4c0d4c32004-06-09 17:34:58 +000049#include <asm/byteorder.h>
wdenk2a8af182005-04-13 10:02:42 +000050#include <environment.h>
wdenkbf9e3b32004-02-12 00:47:09 +000051#ifdef CFG_FLASH_CFI_DRIVER
wdenk028ab6b2004-02-23 23:54:43 +000052
wdenk5653fc32004-02-08 22:55:38 +000053/*
54 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
55 * The width of the port and the width of the chips are determined at initialization.
56 * These widths are used to calculate the address for access CFI data structures.
57 * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
58 *
59 * References
60 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
61 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
62 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
63 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
64 *
65 * TODO
66 *
67 * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
68 * Table (ALT) to determine if protection is available
69 *
70 * Add support for other command sets Use the PRI and ALT to determine command set
71 * Verify erase and program timeouts.
72 */
73
wdenkbf9e3b32004-02-12 00:47:09 +000074#ifndef CFG_FLASH_BANKS_LIST
75#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
76#endif
77
wdenk5653fc32004-02-08 22:55:38 +000078#define FLASH_CMD_CFI 0x98
79#define FLASH_CMD_READ_ID 0x90
80#define FLASH_CMD_RESET 0xff
81#define FLASH_CMD_BLOCK_ERASE 0x20
82#define FLASH_CMD_ERASE_CONFIRM 0xD0
83#define FLASH_CMD_WRITE 0x40
84#define FLASH_CMD_PROTECT 0x60
85#define FLASH_CMD_PROTECT_SET 0x01
86#define FLASH_CMD_PROTECT_CLEAR 0xD0
87#define FLASH_CMD_CLEAR_STATUS 0x50
wdenkbf9e3b32004-02-12 00:47:09 +000088#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
89#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk5653fc32004-02-08 22:55:38 +000090
91#define FLASH_STATUS_DONE 0x80
92#define FLASH_STATUS_ESS 0x40
93#define FLASH_STATUS_ECLBS 0x20
94#define FLASH_STATUS_PSLBS 0x10
95#define FLASH_STATUS_VPENS 0x08
96#define FLASH_STATUS_PSS 0x04
97#define FLASH_STATUS_DPS 0x02
98#define FLASH_STATUS_R 0x01
99#define FLASH_STATUS_PROTECT 0x01
100
101#define AMD_CMD_RESET 0xF0
102#define AMD_CMD_WRITE 0xA0
103#define AMD_CMD_ERASE_START 0x80
104#define AMD_CMD_ERASE_SECTOR 0x30
wdenk855a4962004-03-14 18:23:55 +0000105#define AMD_CMD_UNLOCK_START 0xAA
106#define AMD_CMD_UNLOCK_ACK 0x55
Stefan Roese79b4cda2006-02-28 15:29:58 +0100107#define AMD_CMD_WRITE_TO_BUFFER 0x25
108#define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
wdenk5653fc32004-02-08 22:55:38 +0000109
110#define AMD_STATUS_TOGGLE 0x40
111#define AMD_STATUS_ERROR 0x20
Stefan Roese79b4cda2006-02-28 15:29:58 +0100112
113#define AMD_ADDR_ERASE_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
114#define AMD_ADDR_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
115#define AMD_ADDR_ACK ((info->portwidth == FLASH_CFI_8BIT) ? 0x555 : 0x2AA)
wdenk5653fc32004-02-08 22:55:38 +0000116
117#define FLASH_OFFSET_CFI 0x55
118#define FLASH_OFFSET_CFI_RESP 0x10
wdenkbf9e3b32004-02-12 00:47:09 +0000119#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
Stefan Roese2662b402006-04-01 13:41:03 +0200120#define FLASH_OFFSET_EXT_QUERY_T_P_ADDR 0x15 /* extended query table primary addr */
wdenk5653fc32004-02-08 22:55:38 +0000121#define FLASH_OFFSET_WTOUT 0x1F
wdenkbf9e3b32004-02-12 00:47:09 +0000122#define FLASH_OFFSET_WBTOUT 0x20
wdenk5653fc32004-02-08 22:55:38 +0000123#define FLASH_OFFSET_ETOUT 0x21
wdenkbf9e3b32004-02-12 00:47:09 +0000124#define FLASH_OFFSET_CETOUT 0x22
wdenk5653fc32004-02-08 22:55:38 +0000125#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenkbf9e3b32004-02-12 00:47:09 +0000126#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk5653fc32004-02-08 22:55:38 +0000127#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenkbf9e3b32004-02-12 00:47:09 +0000128#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk5653fc32004-02-08 22:55:38 +0000129#define FLASH_OFFSET_SIZE 0x27
wdenkbf9e3b32004-02-12 00:47:09 +0000130#define FLASH_OFFSET_INTERFACE 0x28
131#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk5653fc32004-02-08 22:55:38 +0000132#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
133#define FLASH_OFFSET_ERASE_REGIONS 0x2D
134#define FLASH_OFFSET_PROTECT 0x02
wdenkbf9e3b32004-02-12 00:47:09 +0000135#define FLASH_OFFSET_USER_PROTECTION 0x85
136#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk5653fc32004-02-08 22:55:38 +0000137
138
139#define FLASH_MAN_CFI 0x01000000
140
wdenkbf9e3b32004-02-12 00:47:09 +0000141#define CFI_CMDSET_NONE 0
wdenk5653fc32004-02-08 22:55:38 +0000142#define CFI_CMDSET_INTEL_EXTENDED 1
wdenkbf9e3b32004-02-12 00:47:09 +0000143#define CFI_CMDSET_AMD_STANDARD 2
wdenk5653fc32004-02-08 22:55:38 +0000144#define CFI_CMDSET_INTEL_STANDARD 3
wdenkbf9e3b32004-02-12 00:47:09 +0000145#define CFI_CMDSET_AMD_EXTENDED 4
wdenk5653fc32004-02-08 22:55:38 +0000146#define CFI_CMDSET_MITSU_STANDARD 256
147#define CFI_CMDSET_MITSU_EXTENDED 257
wdenkbf9e3b32004-02-12 00:47:09 +0000148#define CFI_CMDSET_SST 258
wdenk5653fc32004-02-08 22:55:38 +0000149
150
wdenkf7d15722004-12-18 22:35:43 +0000151#ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
152# undef FLASH_CMD_RESET
153# define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
154#endif
155
156
wdenk5653fc32004-02-08 22:55:38 +0000157typedef union {
158 unsigned char c;
159 unsigned short w;
160 unsigned long l;
161 unsigned long long ll;
162} cfiword_t;
163
164typedef union {
wdenkbf9e3b32004-02-12 00:47:09 +0000165 volatile unsigned char *cp;
wdenk5653fc32004-02-08 22:55:38 +0000166 volatile unsigned short *wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000167 volatile unsigned long *lp;
wdenk5653fc32004-02-08 22:55:38 +0000168 volatile unsigned long long *llp;
169} cfiptr_t;
170
171#define NUM_ERASE_REGIONS 4
172
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200173/* use CFG_MAX_FLASH_BANKS_DETECT if defined */
174#ifdef CFG_MAX_FLASH_BANKS_DETECT
175static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
176flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
177#else
wdenk5653fc32004-02-08 22:55:38 +0000178static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200179flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
180#endif
wdenk5653fc32004-02-08 22:55:38 +0000181
Stefan Roese79b4cda2006-02-28 15:29:58 +0100182/*
183 * Check if chip width is defined. If not, start detecting with 8bit.
184 */
185#ifndef CFG_FLASH_CFI_WIDTH
186#define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
187#endif
188
wdenk5653fc32004-02-08 22:55:38 +0000189
190/*-----------------------------------------------------------------------
191 * Functions
192 */
193
194typedef unsigned long flash_sect_t;
195
wdenkbf9e3b32004-02-12 00:47:09 +0000196static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
197static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenk028ab6b2004-02-23 23:54:43 +0000198static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenkbf9e3b32004-02-12 00:47:09 +0000199static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenk028ab6b2004-02-23 23:54:43 +0000200static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
201static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
202static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenkbf9e3b32004-02-12 00:47:09 +0000203static int flash_detect_cfi (flash_info_t * info);
wdenk028ab6b2004-02-23 23:54:43 +0000204static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenkbf9e3b32004-02-12 00:47:09 +0000205static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
206 ulong tout, char *prompt);
Stefan Roesef18e8742006-03-01 17:00:49 +0100207ulong flash_get_size (ulong base, int banknum);
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200208#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk7680c142005-05-16 15:23:22 +0000209static flash_info_t *flash_get_info(ulong base);
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200210#endif
wdenk5653fc32004-02-08 22:55:38 +0000211#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenk028ab6b2004-02-23 23:54:43 +0000212static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk5653fc32004-02-08 22:55:38 +0000213#endif
214
wdenk5653fc32004-02-08 22:55:38 +0000215/*-----------------------------------------------------------------------
216 * create an address based on the offset and the port width
217 */
wdenk028ab6b2004-02-23 23:54:43 +0000218inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000219{
wdenkbf9e3b32004-02-12 00:47:09 +0000220 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
wdenk5653fc32004-02-08 22:55:38 +0000221}
wdenkbf9e3b32004-02-12 00:47:09 +0000222
223#ifdef DEBUG
224/*-----------------------------------------------------------------------
225 * Debug support
226 */
227void print_longlong (char *str, unsigned long long data)
228{
229 int i;
230 char *cp;
231
232 cp = (unsigned char *) &data;
233 for (i = 0; i < 8; i++)
234 sprintf (&str[i * 2], "%2.2x", *cp++);
235}
236static void flash_printqry (flash_info_t * info, flash_sect_t sect)
237{
238 cfiptr_t cptr;
239 int x, y;
240
Wolfgang Denk47340a42005-10-09 00:25:58 +0200241 for (x = 0; x < 0x40; x += 16U / info->portwidth) {
wdenkbf9e3b32004-02-12 00:47:09 +0000242 cptr.cp =
243 flash_make_addr (info, sect,
244 x + FLASH_OFFSET_CFI_RESP);
245 debug ("%p : ", cptr.cp);
246 for (y = 0; y < 16; y++) {
247 debug ("%2.2x ", cptr.cp[y]);
248 }
249 debug (" ");
250 for (y = 0; y < 16; y++) {
251 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
252 debug ("%c", cptr.cp[y]);
253 } else {
254 debug (".");
255 }
256 }
257 debug ("\n");
258 }
259}
wdenkbf9e3b32004-02-12 00:47:09 +0000260#endif
261
262
wdenk5653fc32004-02-08 22:55:38 +0000263/*-----------------------------------------------------------------------
264 * read a character at a port width address
265 */
wdenkbf9e3b32004-02-12 00:47:09 +0000266inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000267{
268 uchar *cp;
wdenkbf9e3b32004-02-12 00:47:09 +0000269
270 cp = flash_make_addr (info, 0, offset);
271#if defined(__LITTLE_ENDIAN)
272 return (cp[0]);
273#else
wdenk5653fc32004-02-08 22:55:38 +0000274 return (cp[info->portwidth - 1]);
wdenkbf9e3b32004-02-12 00:47:09 +0000275#endif
wdenk5653fc32004-02-08 22:55:38 +0000276}
277
278/*-----------------------------------------------------------------------
279 * read a short word by swapping for ppc format.
280 */
wdenkbf9e3b32004-02-12 00:47:09 +0000281ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000282{
wdenkbf9e3b32004-02-12 00:47:09 +0000283 uchar *addr;
284 ushort retval;
wdenk5653fc32004-02-08 22:55:38 +0000285
wdenkbf9e3b32004-02-12 00:47:09 +0000286#ifdef DEBUG
287 int x;
288#endif
289 addr = flash_make_addr (info, sect, offset);
wdenk5653fc32004-02-08 22:55:38 +0000290
wdenkbf9e3b32004-02-12 00:47:09 +0000291#ifdef DEBUG
292 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
293 info->portwidth);
294 for (x = 0; x < 2 * info->portwidth; x++) {
295 debug ("addr[%x] = 0x%x\n", x, addr[x]);
296 }
297#endif
298#if defined(__LITTLE_ENDIAN)
299 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
300#else
301 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
302 addr[info->portwidth - 1]);
303#endif
304
305 debug ("retval = 0x%x\n", retval);
306 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000307}
308
309/*-----------------------------------------------------------------------
310 * read a long word by picking the least significant byte of each maiximum
311 * port size word. Swap for ppc format.
312 */
wdenkbf9e3b32004-02-12 00:47:09 +0000313ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000314{
wdenkbf9e3b32004-02-12 00:47:09 +0000315 uchar *addr;
316 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000317
wdenkbf9e3b32004-02-12 00:47:09 +0000318#ifdef DEBUG
319 int x;
320#endif
321 addr = flash_make_addr (info, sect, offset);
322
323#ifdef DEBUG
324 debug ("long addr is at %p info->portwidth = %d\n", addr,
325 info->portwidth);
326 for (x = 0; x < 4 * info->portwidth; x++) {
327 debug ("addr[%x] = 0x%x\n", x, addr[x]);
328 }
329#endif
330#if defined(__LITTLE_ENDIAN)
331 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenk028ab6b2004-02-23 23:54:43 +0000332 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenkbf9e3b32004-02-12 00:47:09 +0000333#else
334 retval = (addr[(2 * info->portwidth) - 1] << 24) |
335 (addr[(info->portwidth) - 1] << 16) |
336 (addr[(4 * info->portwidth) - 1] << 8) |
337 addr[(3 * info->portwidth) - 1];
338#endif
339 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000340}
341
Stefan Roese79b4cda2006-02-28 15:29:58 +0100342
wdenk5653fc32004-02-08 22:55:38 +0000343/*-----------------------------------------------------------------------
344 */
345unsigned long flash_init (void)
346{
347 unsigned long size = 0;
348 int i;
349
Stefan Roese2662b402006-04-01 13:41:03 +0200350#ifdef CFG_FLASH_PROTECTION
351 char *s = getenv("unlock");
352#endif
353
wdenk5653fc32004-02-08 22:55:38 +0000354 /* Init: no FLASHes known */
wdenkbf9e3b32004-02-12 00:47:09 +0000355 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk5653fc32004-02-08 22:55:38 +0000356 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenkbf9e3b32004-02-12 00:47:09 +0000357 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk5653fc32004-02-08 22:55:38 +0000358 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Stefan Roese5568e612005-11-22 13:20:42 +0100359#ifndef CFG_FLASH_QUIET_TEST
wdenk028ab6b2004-02-23 23:54:43 +0000360 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
361 i, flash_info[i].size, flash_info[i].size << 20);
Stefan Roese5568e612005-11-22 13:20:42 +0100362#endif /* CFG_FLASH_QUIET_TEST */
wdenk5653fc32004-02-08 22:55:38 +0000363 }
Stefan Roese79b4cda2006-02-28 15:29:58 +0100364#ifdef CFG_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +0200365 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
366 /*
367 * Only the U-Boot image and it's environment is protected,
368 * all other sectors are unprotected (unlocked) if flash
369 * hardware protection is used (CFG_FLASH_PROTECTION) and
370 * the environment variable "unlock" is set to "yes".
371 */
372 if (flash_info[i].legacy_unlock) {
373 int k;
Stefan Roese79b4cda2006-02-28 15:29:58 +0100374
Stefan Roese79b4cda2006-02-28 15:29:58 +0100375 /*
Stefan Roese2662b402006-04-01 13:41:03 +0200376 * Disable legacy_unlock temporarily, since
377 * flash_real_protect would relock all other sectors
378 * again otherwise.
379 */
380 flash_info[i].legacy_unlock = 0;
381
382 /*
383 * Legacy unlocking (e.g. Intel J3) -> unlock only one
384 * sector. This will unlock all sectors.
385 */
386 flash_real_protect (&flash_info[i], 0, 0);
387
388 flash_info[i].legacy_unlock = 1;
389
390 /*
391 * Manually mark other sectors as unlocked (unprotected)
392 */
393 for (k = 1; k < flash_info[i].sector_count; k++)
394 flash_info[i].protect[k] = 0;
395 } else {
396 /*
397 * No legancy unlocking -> unlock all sectors
Stefan Roese79b4cda2006-02-28 15:29:58 +0100398 */
399 flash_protect (FLAG_PROTECT_CLEAR,
400 flash_info[i].start[0],
401 flash_info[i].start[0] + flash_info[i].size - 1,
402 &flash_info[i]);
403 }
404 }
405#endif /* CFG_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +0000406 }
407
408 /* Monitor protection ON by default */
409#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenkbf9e3b32004-02-12 00:47:09 +0000410 flash_protect (FLAG_PROTECT_SET,
411 CFG_MONITOR_BASE,
wdenk7680c142005-05-16 15:23:22 +0000412 CFG_MONITOR_BASE + monitor_flash_len - 1,
413 flash_get_info(CFG_MONITOR_BASE));
wdenk5653fc32004-02-08 22:55:38 +0000414#endif
415
wdenk656658d2004-10-10 22:16:06 +0000416 /* Environment protection ON by default */
417#ifdef CFG_ENV_IS_IN_FLASH
418 flash_protect (FLAG_PROTECT_SET,
419 CFG_ENV_ADDR,
420 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
wdenk7680c142005-05-16 15:23:22 +0000421 flash_get_info(CFG_ENV_ADDR));
wdenk656658d2004-10-10 22:16:06 +0000422#endif
423
424 /* Redundant environment protection ON by default */
425#ifdef CFG_ENV_ADDR_REDUND
426 flash_protect (FLAG_PROTECT_SET,
427 CFG_ENV_ADDR_REDUND,
428 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
wdenk7680c142005-05-16 15:23:22 +0000429 flash_get_info(CFG_ENV_ADDR_REDUND));
wdenk656658d2004-10-10 22:16:06 +0000430#endif
wdenk5653fc32004-02-08 22:55:38 +0000431 return (size);
432}
433
434/*-----------------------------------------------------------------------
435 */
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200436#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk7680c142005-05-16 15:23:22 +0000437static flash_info_t *flash_get_info(ulong base)
438{
439 int i;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200440 flash_info_t * info = 0;
wdenk7680c142005-05-16 15:23:22 +0000441
442 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
443 info = & flash_info[i];
444 if (info->size && info->start[0] <= base &&
445 base <= info->start[0] + info->size - 1)
446 break;
447 }
448
449 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
450}
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200451#endif
wdenk7680c142005-05-16 15:23:22 +0000452
453/*-----------------------------------------------------------------------
454 */
wdenkbf9e3b32004-02-12 00:47:09 +0000455int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +0000456{
457 int rcode = 0;
458 int prot;
459 flash_sect_t sect;
460
wdenkbf9e3b32004-02-12 00:47:09 +0000461 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +0000462 puts ("Can't erase unknown flash type - aborted\n");
wdenk5653fc32004-02-08 22:55:38 +0000463 return 1;
464 }
465 if ((s_first < 0) || (s_first > s_last)) {
wdenk4b9206e2004-03-23 22:14:11 +0000466 puts ("- no sectors to erase\n");
wdenk5653fc32004-02-08 22:55:38 +0000467 return 1;
468 }
469
470 prot = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000471 for (sect = s_first; sect <= s_last; ++sect) {
wdenk5653fc32004-02-08 22:55:38 +0000472 if (info->protect[sect]) {
473 prot++;
474 }
475 }
476 if (prot) {
wdenkbf9e3b32004-02-12 00:47:09 +0000477 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk5653fc32004-02-08 22:55:38 +0000478 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000479 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +0000480 }
481
482
wdenkbf9e3b32004-02-12 00:47:09 +0000483 for (sect = s_first; sect <= s_last; sect++) {
wdenk5653fc32004-02-08 22:55:38 +0000484 if (info->protect[sect] == 0) { /* not protected */
wdenkbf9e3b32004-02-12 00:47:09 +0000485 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000486 case CFI_CMDSET_INTEL_STANDARD:
487 case CFI_CMDSET_INTEL_EXTENDED:
wdenk028ab6b2004-02-23 23:54:43 +0000488 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
489 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
490 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +0000491 break;
492 case CFI_CMDSET_AMD_STANDARD:
493 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000494 flash_unlock_seq (info, sect);
wdenk855a4962004-03-14 18:23:55 +0000495 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
496 AMD_CMD_ERASE_START);
wdenkbf9e3b32004-02-12 00:47:09 +0000497 flash_unlock_seq (info, sect);
wdenk028ab6b2004-02-23 23:54:43 +0000498 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk5653fc32004-02-08 22:55:38 +0000499 break;
500 default:
wdenkbf9e3b32004-02-12 00:47:09 +0000501 debug ("Unkown flash vendor %d\n",
502 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +0000503 break;
504 }
505
wdenkbf9e3b32004-02-12 00:47:09 +0000506 if (flash_full_status_check
507 (info, sect, info->erase_blk_tout, "erase")) {
wdenk5653fc32004-02-08 22:55:38 +0000508 rcode = 1;
509 } else
wdenk4b9206e2004-03-23 22:14:11 +0000510 putc ('.');
wdenk5653fc32004-02-08 22:55:38 +0000511 }
512 }
wdenk4b9206e2004-03-23 22:14:11 +0000513 puts (" done\n");
wdenk5653fc32004-02-08 22:55:38 +0000514 return rcode;
515}
516
517/*-----------------------------------------------------------------------
518 */
wdenkbf9e3b32004-02-12 00:47:09 +0000519void flash_print_info (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +0000520{
521 int i;
522
523 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +0000524 puts ("missing or unknown FLASH type\n");
wdenk5653fc32004-02-08 22:55:38 +0000525 return;
526 }
527
wdenkbf9e3b32004-02-12 00:47:09 +0000528 printf ("CFI conformant FLASH (%d x %d)",
529 (info->portwidth << 3), (info->chipwidth << 3));
wdenk5653fc32004-02-08 22:55:38 +0000530 printf (" Size: %ld MB in %d Sectors\n",
531 info->size >> 20, info->sector_count);
wdenk028ab6b2004-02-23 23:54:43 +0000532 printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
533 info->erase_blk_tout,
534 info->write_tout,
535 info->buffer_write_tout,
536 info->buffer_size);
wdenk5653fc32004-02-08 22:55:38 +0000537
wdenk4b9206e2004-03-23 22:14:11 +0000538 puts (" Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +0000539 for (i = 0; i < info->sector_count; ++i) {
wdenk5653fc32004-02-08 22:55:38 +0000540#ifdef CFG_FLASH_EMPTY_INFO
541 int k;
542 int size;
543 int erased;
544 volatile unsigned long *flash;
545
546 /*
547 * Check if whole sector is erased
548 */
wdenkbf9e3b32004-02-12 00:47:09 +0000549 if (i != (info->sector_count - 1))
550 size = info->start[i + 1] - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000551 else
wdenkbf9e3b32004-02-12 00:47:09 +0000552 size = info->start[0] + info->size - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000553 erased = 1;
wdenkbf9e3b32004-02-12 00:47:09 +0000554 flash = (volatile unsigned long *) info->start[i];
555 size = size >> 2; /* divide by 4 for longword access */
556 for (k = 0; k < size; k++) {
557 if (*flash++ != 0xffffffff) {
558 erased = 0;
559 break;
560 }
561 }
wdenk5653fc32004-02-08 22:55:38 +0000562
563 if ((i % 5) == 0)
564 printf ("\n");
565 /* print empty and read-only info */
566 printf (" %08lX%s%s",
567 info->start[i],
568 erased ? " E" : " ",
569 info->protect[i] ? "RO " : " ");
Wolfgang Denkb63de2c2005-09-25 00:23:05 +0200570#else /* ! CFG_FLASH_EMPTY_INFO */
wdenk5653fc32004-02-08 22:55:38 +0000571 if ((i % 5) == 0)
572 printf ("\n ");
573 printf (" %08lX%s",
Wolfgang Denkb63de2c2005-09-25 00:23:05 +0200574 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenk5653fc32004-02-08 22:55:38 +0000575#endif
576 }
wdenk4b9206e2004-03-23 22:14:11 +0000577 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +0000578 return;
579}
580
581/*-----------------------------------------------------------------------
582 * Copy memory to flash, returns:
583 * 0 - OK
584 * 1 - write timeout
585 * 2 - Flash not erased
586 */
wdenkbf9e3b32004-02-12 00:47:09 +0000587int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +0000588{
589 ulong wp;
590 ulong cp;
591 int aln;
592 cfiword_t cword;
593 int i, rc;
594
wdenkbf9e3b32004-02-12 00:47:09 +0000595#ifdef CFG_FLASH_USE_BUFFER_WRITE
596 int buffered_size;
597#endif
wdenkbf9e3b32004-02-12 00:47:09 +0000598 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +0000599 /* get lower aligned address */
600 wp = (addr & ~(info->portwidth - 1));
601
602 /* handle unaligned start */
wdenkbf9e3b32004-02-12 00:47:09 +0000603 if ((aln = addr - wp) != 0) {
wdenk5653fc32004-02-08 22:55:38 +0000604 cword.l = 0;
605 cp = wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000606 for (i = 0; i < aln; ++i, ++cp)
607 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000608
wdenkbf9e3b32004-02-12 00:47:09 +0000609 for (; (i < info->portwidth) && (cnt > 0); i++) {
610 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000611 cnt--;
612 cp++;
613 }
wdenkbf9e3b32004-02-12 00:47:09 +0000614 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
615 flash_add_byte (info, &cword, (*(uchar *) cp));
616 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000617 return rc;
618 wp = cp;
619 }
620
wdenkbf9e3b32004-02-12 00:47:09 +0000621 /* handle the aligned part */
wdenk5653fc32004-02-08 22:55:38 +0000622#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +0000623 buffered_size = (info->portwidth / info->chipwidth);
624 buffered_size *= info->buffer_size;
625 while (cnt >= info->portwidth) {
Stefan Roese79b4cda2006-02-28 15:29:58 +0100626 /* prohibit buffer write when buffer_size is 1 */
627 if (info->buffer_size == 1) {
628 cword.l = 0;
629 for (i = 0; i < info->portwidth; i++)
630 flash_add_byte (info, &cword, *src++);
631 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
632 return rc;
633 wp += info->portwidth;
634 cnt -= info->portwidth;
635 continue;
636 }
637
638 /* write buffer until next buffered_size aligned boundary */
639 i = buffered_size - (wp % buffered_size);
640 if (i > cnt)
641 i = cnt;
wdenkbf9e3b32004-02-12 00:47:09 +0000642 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +0000643 return rc;
Wolfgang Denk8d4ba3d2005-08-12 22:35:59 +0200644 i -= i & (info->portwidth - 1);
wdenk5653fc32004-02-08 22:55:38 +0000645 wp += i;
646 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +0000647 cnt -= i;
wdenk5653fc32004-02-08 22:55:38 +0000648 }
649#else
wdenkbf9e3b32004-02-12 00:47:09 +0000650 while (cnt >= info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000651 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000652 for (i = 0; i < info->portwidth; i++) {
653 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000654 }
wdenkbf9e3b32004-02-12 00:47:09 +0000655 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000656 return rc;
657 wp += info->portwidth;
658 cnt -= info->portwidth;
659 }
660#endif /* CFG_FLASH_USE_BUFFER_WRITE */
661 if (cnt == 0) {
662 return (0);
663 }
664
665 /*
666 * handle unaligned tail bytes
667 */
668 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000669 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
670 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000671 --cnt;
672 }
wdenkbf9e3b32004-02-12 00:47:09 +0000673 for (; i < info->portwidth; ++i, ++cp) {
674 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000675 }
676
wdenkbf9e3b32004-02-12 00:47:09 +0000677 return flash_write_cfiword (info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +0000678}
679
680/*-----------------------------------------------------------------------
681 */
682#ifdef CFG_FLASH_PROTECTION
683
wdenkbf9e3b32004-02-12 00:47:09 +0000684int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +0000685{
686 int retcode = 0;
687
wdenkbf9e3b32004-02-12 00:47:09 +0000688 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
689 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
690 if (prot)
691 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk5653fc32004-02-08 22:55:38 +0000692 else
wdenkbf9e3b32004-02-12 00:47:09 +0000693 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk5653fc32004-02-08 22:55:38 +0000694
wdenkbf9e3b32004-02-12 00:47:09 +0000695 if ((retcode =
696 flash_full_status_check (info, sector, info->erase_blk_tout,
697 prot ? "protect" : "unprotect")) == 0) {
wdenk5653fc32004-02-08 22:55:38 +0000698
699 info->protect[sector] = prot;
Stefan Roese2662b402006-04-01 13:41:03 +0200700
701 /*
702 * On some of Intel's flash chips (marked via legacy_unlock)
703 * unprotect unprotects all locking.
704 */
705 if ((prot == 0) && (info->legacy_unlock)) {
wdenk5653fc32004-02-08 22:55:38 +0000706 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +0000707
708 for (i = 0; i < info->sector_count; i++) {
709 if (info->protect[i])
710 flash_real_protect (info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +0000711 }
712 }
713 }
wdenk5653fc32004-02-08 22:55:38 +0000714 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000715}
716
wdenk5653fc32004-02-08 22:55:38 +0000717/*-----------------------------------------------------------------------
718 * flash_read_user_serial - read the OneTimeProgramming cells
719 */
wdenkbf9e3b32004-02-12 00:47:09 +0000720void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
721 int len)
wdenk5653fc32004-02-08 22:55:38 +0000722{
wdenkbf9e3b32004-02-12 00:47:09 +0000723 uchar *src;
724 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +0000725
726 dst = buffer;
wdenkbf9e3b32004-02-12 00:47:09 +0000727 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
728 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
729 memcpy (dst, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200730 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000731}
wdenkbf9e3b32004-02-12 00:47:09 +0000732
wdenk5653fc32004-02-08 22:55:38 +0000733/*
734 * flash_read_factory_serial - read the device Id from the protection area
735 */
wdenkbf9e3b32004-02-12 00:47:09 +0000736void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
737 int len)
wdenk5653fc32004-02-08 22:55:38 +0000738{
wdenkbf9e3b32004-02-12 00:47:09 +0000739 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +0000740
wdenkbf9e3b32004-02-12 00:47:09 +0000741 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
742 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
743 memcpy (buffer, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200744 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000745}
746
747#endif /* CFG_FLASH_PROTECTION */
748
wdenkbf9e3b32004-02-12 00:47:09 +0000749/*
750 * flash_is_busy - check to see if the flash is busy
751 * This routine checks the status of the chip and returns true if the chip is busy
752 */
753static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000754{
755 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000756
757 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000758 case CFI_CMDSET_INTEL_STANDARD:
759 case CFI_CMDSET_INTEL_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000760 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk5653fc32004-02-08 22:55:38 +0000761 break;
762 case CFI_CMDSET_AMD_STANDARD:
763 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000764 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk5653fc32004-02-08 22:55:38 +0000765 break;
766 default:
767 retval = 0;
768 }
wdenkbf9e3b32004-02-12 00:47:09 +0000769 debug ("flash_is_busy: %d\n", retval);
wdenk5653fc32004-02-08 22:55:38 +0000770 return retval;
771}
wdenkbf9e3b32004-02-12 00:47:09 +0000772
wdenk5653fc32004-02-08 22:55:38 +0000773/*-----------------------------------------------------------------------
774 * wait for XSR.7 to be set. Time out with an error if it does not.
775 * This routine does not set the flash to read-array mode.
776 */
wdenkbf9e3b32004-02-12 00:47:09 +0000777static int flash_status_check (flash_info_t * info, flash_sect_t sector,
778 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000779{
780 ulong start;
781
Stefan Roese2662b402006-04-01 13:41:03 +0200782#if CFG_HZ != 1000
783 tout *= CFG_HZ/1000;
784#endif
785
wdenk5653fc32004-02-08 22:55:38 +0000786 /* Wait for command completion */
787 start = get_timer (0);
wdenkbf9e3b32004-02-12 00:47:09 +0000788 while (flash_is_busy (info, sector)) {
Stefan Roese79b4cda2006-02-28 15:29:58 +0100789 if (get_timer (start) > tout) {
wdenkbf9e3b32004-02-12 00:47:09 +0000790 printf ("Flash %s timeout at address %lx data %lx\n",
791 prompt, info->start[sector],
792 flash_read_long (info, sector, 0));
793 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000794 return ERR_TIMOUT;
795 }
Wolfgang Denk62b8f542006-06-02 11:46:20 +0200796 udelay (1); /* also triggers watchdog */
wdenk5653fc32004-02-08 22:55:38 +0000797 }
798 return ERR_OK;
799}
wdenkbf9e3b32004-02-12 00:47:09 +0000800
wdenk5653fc32004-02-08 22:55:38 +0000801/*-----------------------------------------------------------------------
802 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
803 * This routine sets the flash to read-array mode.
804 */
wdenkbf9e3b32004-02-12 00:47:09 +0000805static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
806 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000807{
808 int retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000809
810 retcode = flash_status_check (info, sector, tout, prompt);
811 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000812 case CFI_CMDSET_INTEL_EXTENDED:
813 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roese79b4cda2006-02-28 15:29:58 +0100814 if ((retcode == ERR_OK)
wdenkbf9e3b32004-02-12 00:47:09 +0000815 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk5653fc32004-02-08 22:55:38 +0000816 retcode = ERR_INVAL;
wdenkbf9e3b32004-02-12 00:47:09 +0000817 printf ("Flash %s error at address %lx\n", prompt,
818 info->start[sector]);
wdenk028ab6b2004-02-23 23:54:43 +0000819 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000820 puts ("Command Sequence Error.\n");
wdenk028ab6b2004-02-23 23:54:43 +0000821 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000822 puts ("Block Erase Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000823 retcode = ERR_NOT_ERASED;
wdenk028ab6b2004-02-23 23:54:43 +0000824 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000825 puts ("Locking Error\n");
wdenk5653fc32004-02-08 22:55:38 +0000826 }
wdenkbf9e3b32004-02-12 00:47:09 +0000827 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000828 puts ("Block locked.\n");
wdenkbf9e3b32004-02-12 00:47:09 +0000829 retcode = ERR_PROTECTED;
830 }
831 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk4b9206e2004-03-23 22:14:11 +0000832 puts ("Vpp Low Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000833 }
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200834 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000835 break;
836 default:
837 break;
838 }
839 return retcode;
840}
wdenkbf9e3b32004-02-12 00:47:09 +0000841
wdenk5653fc32004-02-08 22:55:38 +0000842/*-----------------------------------------------------------------------
843 */
wdenkbf9e3b32004-02-12 00:47:09 +0000844static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk5653fc32004-02-08 22:55:38 +0000845{
wdenk4d13cba2004-03-14 14:09:05 +0000846#if defined(__LITTLE_ENDIAN)
847 unsigned short w;
848 unsigned int l;
849 unsigned long long ll;
850#endif
851
wdenkbf9e3b32004-02-12 00:47:09 +0000852 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000853 case FLASH_CFI_8BIT:
854 cword->c = c;
855 break;
856 case FLASH_CFI_16BIT:
wdenk4d13cba2004-03-14 14:09:05 +0000857#if defined(__LITTLE_ENDIAN)
858 w = c;
859 w <<= 8;
860 cword->w = (cword->w >> 8) | w;
861#else
wdenk5653fc32004-02-08 22:55:38 +0000862 cword->w = (cword->w << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000863#endif
wdenk5653fc32004-02-08 22:55:38 +0000864 break;
865 case FLASH_CFI_32BIT:
wdenk4d13cba2004-03-14 14:09:05 +0000866#if defined(__LITTLE_ENDIAN)
867 l = c;
868 l <<= 24;
869 cword->l = (cword->l >> 8) | l;
870#else
wdenk5653fc32004-02-08 22:55:38 +0000871 cword->l = (cword->l << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000872#endif
wdenk5653fc32004-02-08 22:55:38 +0000873 break;
874 case FLASH_CFI_64BIT:
wdenk4d13cba2004-03-14 14:09:05 +0000875#if defined(__LITTLE_ENDIAN)
876 ll = c;
877 ll <<= 56;
878 cword->ll = (cword->ll >> 8) | ll;
879#else
wdenk5653fc32004-02-08 22:55:38 +0000880 cword->ll = (cword->ll << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000881#endif
wdenk5653fc32004-02-08 22:55:38 +0000882 break;
883 }
884}
885
886
887/*-----------------------------------------------------------------------
888 * make a proper sized command based on the port and chip widths
889 */
wdenkbf9e3b32004-02-12 00:47:09 +0000890static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk5653fc32004-02-08 22:55:38 +0000891{
892 int i;
wdenkbf9e3b32004-02-12 00:47:09 +0000893 uchar *cp = (uchar *) cmdbuf;
894
wdenkbf9e3b32004-02-12 00:47:09 +0000895#if defined(__LITTLE_ENDIAN)
Wolfgang Denkdafbe372005-09-24 23:32:48 +0200896 for (i = info->portwidth; i > 0; i--)
897#else
898 for (i = 1; i <= info->portwidth; i++)
wdenkbf9e3b32004-02-12 00:47:09 +0000899#endif
Wolfgang Denk47340a42005-10-09 00:25:58 +0200900 *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
wdenk5653fc32004-02-08 22:55:38 +0000901}
902
903/*
904 * Write a proper sized command to the correct address
905 */
wdenk028ab6b2004-02-23 23:54:43 +0000906static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000907{
908
909 volatile cfiptr_t addr;
910 cfiword_t cword;
wdenkbf9e3b32004-02-12 00:47:09 +0000911
912 addr.cp = flash_make_addr (info, sect, offset);
913 flash_make_cmd (info, cmd, &cword);
914 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000915 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000916 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
917 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk5653fc32004-02-08 22:55:38 +0000918 *addr.cp = cword.c;
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100919#ifdef CONFIG_BLACKFIN
920 asm("ssync;");
921#endif
wdenk5653fc32004-02-08 22:55:38 +0000922 break;
923 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000924 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
925 cmd, cword.w,
wdenk5653fc32004-02-08 22:55:38 +0000926 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
927 *addr.wp = cword.w;
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100928#ifdef CONFIG_BLACKFIN
929 asm("ssync;");
930#endif
wdenk5653fc32004-02-08 22:55:38 +0000931 break;
932 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000933 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
934 cmd, cword.l,
wdenk5653fc32004-02-08 22:55:38 +0000935 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
936 *addr.lp = cword.l;
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100937#ifdef CONFIG_BLACKFIN
938 asm("ssync;");
939#endif
wdenk5653fc32004-02-08 22:55:38 +0000940 break;
941 case FLASH_CFI_64BIT:
942#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +0000943 {
wdenk5653fc32004-02-08 22:55:38 +0000944 char str[20];
wdenkcd37d9e2004-02-10 00:03:41 +0000945
wdenkbf9e3b32004-02-12 00:47:09 +0000946 print_longlong (str, cword.ll);
947
948 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
949 addr.llp, cmd, str,
wdenk5653fc32004-02-08 22:55:38 +0000950 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
951 }
952#endif
953 *addr.llp = cword.ll;
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100954#ifdef CONFIG_BLACKFIN
955 asm("ssync;");
956#endif
wdenk5653fc32004-02-08 22:55:38 +0000957 break;
958 }
959}
960
wdenkbf9e3b32004-02-12 00:47:09 +0000961static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000962{
wdenk855a4962004-03-14 18:23:55 +0000963 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
964 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
wdenk5653fc32004-02-08 22:55:38 +0000965}
wdenkbf9e3b32004-02-12 00:47:09 +0000966
wdenk5653fc32004-02-08 22:55:38 +0000967/*-----------------------------------------------------------------------
968 */
wdenk028ab6b2004-02-23 23:54:43 +0000969static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000970{
971 cfiptr_t cptr;
972 cfiword_t cword;
973 int retval;
wdenk5653fc32004-02-08 22:55:38 +0000974
wdenkbf9e3b32004-02-12 00:47:09 +0000975 cptr.cp = flash_make_addr (info, sect, offset);
976 flash_make_cmd (info, cmd, &cword);
977
978 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
979 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000980 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000981 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk5653fc32004-02-08 22:55:38 +0000982 retval = (cptr.cp[0] == cword.c);
983 break;
984 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000985 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk5653fc32004-02-08 22:55:38 +0000986 retval = (cptr.wp[0] == cword.w);
987 break;
988 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000989 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk5653fc32004-02-08 22:55:38 +0000990 retval = (cptr.lp[0] == cword.l);
991 break;
992 case FLASH_CFI_64BIT:
wdenkcd37d9e2004-02-10 00:03:41 +0000993#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +0000994 {
wdenk5653fc32004-02-08 22:55:38 +0000995 char str1[20];
996 char str2[20];
wdenkbf9e3b32004-02-12 00:47:09 +0000997
998 print_longlong (str1, cptr.llp[0]);
999 print_longlong (str2, cword.ll);
1000 debug ("is= %s %s\n", str1, str2);
wdenk5653fc32004-02-08 22:55:38 +00001001 }
1002#endif
1003 retval = (cptr.llp[0] == cword.ll);
1004 break;
1005 default:
1006 retval = 0;
1007 break;
1008 }
1009 return retval;
1010}
wdenkbf9e3b32004-02-12 00:47:09 +00001011
wdenk5653fc32004-02-08 22:55:38 +00001012/*-----------------------------------------------------------------------
1013 */
wdenk028ab6b2004-02-23 23:54:43 +00001014static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +00001015{
1016 cfiptr_t cptr;
1017 cfiword_t cword;
1018 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +00001019
1020 cptr.cp = flash_make_addr (info, sect, offset);
1021 flash_make_cmd (info, cmd, &cword);
1022 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001023 case FLASH_CFI_8BIT:
1024 retval = ((cptr.cp[0] & cword.c) == cword.c);
1025 break;
1026 case FLASH_CFI_16BIT:
1027 retval = ((cptr.wp[0] & cword.w) == cword.w);
1028 break;
1029 case FLASH_CFI_32BIT:
1030 retval = ((cptr.lp[0] & cword.l) == cword.l);
1031 break;
1032 case FLASH_CFI_64BIT:
1033 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenkbf9e3b32004-02-12 00:47:09 +00001034 break;
wdenk5653fc32004-02-08 22:55:38 +00001035 default:
1036 retval = 0;
1037 break;
1038 }
1039 return retval;
1040}
1041
1042/*-----------------------------------------------------------------------
1043 */
wdenk028ab6b2004-02-23 23:54:43 +00001044static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +00001045{
1046 cfiptr_t cptr;
1047 cfiword_t cword;
1048 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +00001049
1050 cptr.cp = flash_make_addr (info, sect, offset);
1051 flash_make_cmd (info, cmd, &cword);
1052 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001053 case FLASH_CFI_8BIT:
1054 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
1055 break;
1056 case FLASH_CFI_16BIT:
1057 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
1058 break;
1059 case FLASH_CFI_32BIT:
1060 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
1061 break;
1062 case FLASH_CFI_64BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001063 retval = ((cptr.llp[0] & cword.ll) !=
1064 (cptr.llp[0] & cword.ll));
wdenk5653fc32004-02-08 22:55:38 +00001065 break;
1066 default:
1067 retval = 0;
1068 break;
1069 }
1070 return retval;
1071}
1072
1073/*-----------------------------------------------------------------------
1074 * detect if flash is compatible with the Common Flash Interface (CFI)
1075 * http://www.jedec.org/download/search/jesd68.pdf
1076 *
1077*/
wdenkbf9e3b32004-02-12 00:47:09 +00001078static int flash_detect_cfi (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +00001079{
wdenkbf9e3b32004-02-12 00:47:09 +00001080 debug ("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +00001081
Stefan Roese79b4cda2006-02-28 15:29:58 +01001082 for (info->portwidth = CFG_FLASH_CFI_WIDTH;
wdenkbf9e3b32004-02-12 00:47:09 +00001083 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1084 for (info->chipwidth = FLASH_CFI_BY8;
1085 info->chipwidth <= info->portwidth;
1086 info->chipwidth <<= 1) {
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001087 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk028ab6b2004-02-23 23:54:43 +00001088 flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
1089 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1090 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1091 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1092 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
wdenkbf9e3b32004-02-12 00:47:09 +00001093 debug ("device interface is %d\n",
1094 info->interface);
1095 debug ("found port %d chip %d ",
1096 info->portwidth, info->chipwidth);
1097 debug ("port %d bits chip %d bits\n",
wdenk028ab6b2004-02-23 23:54:43 +00001098 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1099 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk5653fc32004-02-08 22:55:38 +00001100 return 1;
1101 }
1102 }
1103 }
wdenkbf9e3b32004-02-12 00:47:09 +00001104 debug ("not found\n");
wdenk5653fc32004-02-08 22:55:38 +00001105 return 0;
1106}
wdenkbf9e3b32004-02-12 00:47:09 +00001107
wdenk5653fc32004-02-08 22:55:38 +00001108/*
1109 * The following code cannot be run from FLASH!
1110 *
1111 */
Marian Balakowicze6f2e902005-10-11 19:09:42 +02001112ulong flash_get_size (ulong base, int banknum)
wdenk5653fc32004-02-08 22:55:38 +00001113{
wdenkbf9e3b32004-02-12 00:47:09 +00001114 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +00001115 int i, j;
1116 flash_sect_t sect_cnt;
1117 unsigned long sector;
1118 unsigned long tmp;
1119 int size_ratio;
1120 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +00001121 int erase_region_size;
1122 int erase_region_count;
Stefan Roese2662b402006-04-01 13:41:03 +02001123#ifdef CFG_FLASH_PROTECTION
1124 int ext_addr;
1125 info->legacy_unlock = 0;
1126#endif
wdenk5653fc32004-02-08 22:55:38 +00001127
1128 info->start[0] = base;
1129
wdenkbf9e3b32004-02-12 00:47:09 +00001130 if (flash_detect_cfi (info)) {
wdenk028ab6b2004-02-23 23:54:43 +00001131 info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
wdenkbf9e3b32004-02-12 00:47:09 +00001132#ifdef DEBUG
1133 flash_printqry (info, 0);
1134#endif
1135 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001136 case CFI_CMDSET_INTEL_STANDARD:
1137 case CFI_CMDSET_INTEL_EXTENDED:
1138 default:
1139 info->cmd_reset = FLASH_CMD_RESET;
Stefan Roese2662b402006-04-01 13:41:03 +02001140#ifdef CFG_FLASH_PROTECTION
1141 /* read legacy lock/unlock bit from intel flash */
1142 ext_addr = flash_read_ushort (info, 0,
1143 FLASH_OFFSET_EXT_QUERY_T_P_ADDR);
1144 info->legacy_unlock =
1145 flash_read_uchar (info, ext_addr + 5) & 0x08;
1146#endif
wdenk5653fc32004-02-08 22:55:38 +00001147 break;
1148 case CFI_CMDSET_AMD_STANDARD:
1149 case CFI_CMDSET_AMD_EXTENDED:
1150 info->cmd_reset = AMD_CMD_RESET;
1151 break;
1152 }
wdenkcd37d9e2004-02-10 00:03:41 +00001153
wdenkbf9e3b32004-02-12 00:47:09 +00001154 debug ("manufacturer is %d\n", info->vendor);
wdenk5653fc32004-02-08 22:55:38 +00001155 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00001156 /* if the chip is x8/x16 reduce the ratio by half */
1157 if ((info->interface == FLASH_CFI_X8X16)
1158 && (info->chipwidth == FLASH_CFI_BY8)) {
1159 size_ratio >>= 1;
1160 }
wdenk028ab6b2004-02-23 23:54:43 +00001161 num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
wdenkbf9e3b32004-02-12 00:47:09 +00001162 debug ("size_ratio %d port %d bits chip %d bits\n",
1163 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1164 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1165 debug ("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00001166 sect_cnt = 0;
1167 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00001168 for (i = 0; i < num_erase_regions; i++) {
1169 if (i > NUM_ERASE_REGIONS) {
wdenk028ab6b2004-02-23 23:54:43 +00001170 printf ("%d erase regions found, only %d used\n",
1171 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00001172 break;
1173 }
wdenkbf9e3b32004-02-12 00:47:09 +00001174 tmp = flash_read_long (info, 0,
1175 FLASH_OFFSET_ERASE_REGIONS +
1176 i * 4);
1177 erase_region_size =
1178 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk5653fc32004-02-08 22:55:38 +00001179 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00001180 erase_region_count = (tmp & 0xffff) + 1;
wdenk4c0d4c32004-06-09 17:34:58 +00001181 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenk028ab6b2004-02-23 23:54:43 +00001182 erase_region_count, erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00001183 for (j = 0; j < erase_region_count; j++) {
wdenk5653fc32004-02-08 22:55:38 +00001184 info->start[sect_cnt] = sector;
1185 sector += (erase_region_size * size_ratio);
wdenka1191902005-01-09 17:12:27 +00001186
1187 /*
1188 * Only read protection status from supported devices (intel...)
1189 */
1190 switch (info->vendor) {
1191 case CFI_CMDSET_INTEL_EXTENDED:
1192 case CFI_CMDSET_INTEL_STANDARD:
1193 info->protect[sect_cnt] =
1194 flash_isset (info, sect_cnt,
1195 FLASH_OFFSET_PROTECT,
1196 FLASH_STATUS_PROTECT);
1197 break;
1198 default:
1199 info->protect[sect_cnt] = 0; /* default: not protected */
1200 }
1201
wdenk5653fc32004-02-08 22:55:38 +00001202 sect_cnt++;
1203 }
1204 }
1205
1206 info->sector_count = sect_cnt;
1207 /* multiply the size by the number of chips */
wdenk028ab6b2004-02-23 23:54:43 +00001208 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1209 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
wdenkbf9e3b32004-02-12 00:47:09 +00001210 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenk028ab6b2004-02-23 23:54:43 +00001211 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
Stefan Roese2662b402006-04-01 13:41:03 +02001212 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT)) *
1213 (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT));
1214 info->buffer_write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
Stefan Roese79b4cda2006-02-28 15:29:58 +01001215 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
1216 (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
1217 info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
wdenk5653fc32004-02-08 22:55:38 +00001218 info->flash_id = FLASH_MAN_CFI;
wdenk855a4962004-03-14 18:23:55 +00001219 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1220 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1221 }
wdenk5653fc32004-02-08 22:55:38 +00001222 }
1223
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001224 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenkbf9e3b32004-02-12 00:47:09 +00001225 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00001226}
1227
Stefan Roese79b4cda2006-02-28 15:29:58 +01001228/* loop through the sectors from the highest address
1229 * when the passed address is greater or equal to the sector address
1230 * we have a match
1231 */
1232static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1233{
1234 flash_sect_t sector;
1235
1236 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1237 if (addr >= info->start[sector])
1238 break;
1239 }
1240 return sector;
1241}
wdenk5653fc32004-02-08 22:55:38 +00001242
1243/*-----------------------------------------------------------------------
1244 */
wdenkbf9e3b32004-02-12 00:47:09 +00001245static int flash_write_cfiword (flash_info_t * info, ulong dest,
1246 cfiword_t cword)
wdenk5653fc32004-02-08 22:55:38 +00001247{
wdenk5653fc32004-02-08 22:55:38 +00001248 cfiptr_t ctladdr;
1249 cfiptr_t cptr;
1250 int flag;
1251
wdenkbf9e3b32004-02-12 00:47:09 +00001252 ctladdr.cp = flash_make_addr (info, 0, 0);
1253 cptr.cp = (uchar *) dest;
wdenk5653fc32004-02-08 22:55:38 +00001254
1255
1256 /* Check if Flash is (sufficiently) erased */
wdenkbf9e3b32004-02-12 00:47:09 +00001257 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001258 case FLASH_CFI_8BIT:
1259 flag = ((cptr.cp[0] & cword.c) == cword.c);
1260 break;
1261 case FLASH_CFI_16BIT:
1262 flag = ((cptr.wp[0] & cword.w) == cword.w);
1263 break;
1264 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001265 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk5653fc32004-02-08 22:55:38 +00001266 break;
1267 case FLASH_CFI_64BIT:
wdenke1599e82004-10-10 23:27:33 +00001268 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenk5653fc32004-02-08 22:55:38 +00001269 break;
1270 default:
1271 return 2;
1272 }
wdenkbf9e3b32004-02-12 00:47:09 +00001273 if (!flag)
wdenk5653fc32004-02-08 22:55:38 +00001274 return 2;
1275
1276 /* Disable interrupts which might cause a timeout here */
wdenkbf9e3b32004-02-12 00:47:09 +00001277 flag = disable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001278
wdenkbf9e3b32004-02-12 00:47:09 +00001279 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001280 case CFI_CMDSET_INTEL_EXTENDED:
1281 case CFI_CMDSET_INTEL_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001282 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1283 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001284 break;
1285 case CFI_CMDSET_AMD_EXTENDED:
1286 case CFI_CMDSET_AMD_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001287 flash_unlock_seq (info, 0);
wdenk855a4962004-03-14 18:23:55 +00001288 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001289 break;
1290 }
1291
wdenkbf9e3b32004-02-12 00:47:09 +00001292 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001293 case FLASH_CFI_8BIT:
1294 cptr.cp[0] = cword.c;
1295 break;
1296 case FLASH_CFI_16BIT:
1297 cptr.wp[0] = cword.w;
1298 break;
1299 case FLASH_CFI_32BIT:
1300 cptr.lp[0] = cword.l;
1301 break;
1302 case FLASH_CFI_64BIT:
1303 cptr.llp[0] = cword.ll;
1304 break;
1305 }
1306
1307 /* re-enable interrupts if necessary */
wdenkbf9e3b32004-02-12 00:47:09 +00001308 if (flag)
1309 enable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001310
Stefan Roese79b4cda2006-02-28 15:29:58 +01001311 return flash_full_status_check (info, find_sector (info, dest),
1312 info->write_tout, "write");
wdenk5653fc32004-02-08 22:55:38 +00001313}
1314
1315#ifdef CFG_FLASH_USE_BUFFER_WRITE
1316
wdenkbf9e3b32004-02-12 00:47:09 +00001317static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1318 int len)
wdenk5653fc32004-02-08 22:55:38 +00001319{
1320 flash_sect_t sector;
1321 int cnt;
1322 int retcode;
1323 volatile cfiptr_t src;
1324 volatile cfiptr_t dst;
1325
Stefan Roese79b4cda2006-02-28 15:29:58 +01001326 switch (info->vendor) {
1327 case CFI_CMDSET_INTEL_STANDARD:
1328 case CFI_CMDSET_INTEL_EXTENDED:
1329 src.cp = cp;
1330 dst.cp = (uchar *) dest;
1331 sector = find_sector (info, dest);
1332 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1333 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1334 if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
1335 "write to buffer")) == ERR_OK) {
1336 /* reduce the number of loops by the width of the port */
wdenkbf9e3b32004-02-12 00:47:09 +00001337 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001338 case FLASH_CFI_8BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001339 cnt = len;
wdenk5653fc32004-02-08 22:55:38 +00001340 break;
1341 case FLASH_CFI_16BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001342 cnt = len >> 1;
wdenk5653fc32004-02-08 22:55:38 +00001343 break;
1344 case FLASH_CFI_32BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001345 cnt = len >> 2;
wdenk5653fc32004-02-08 22:55:38 +00001346 break;
1347 case FLASH_CFI_64BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001348 cnt = len >> 3;
wdenk5653fc32004-02-08 22:55:38 +00001349 break;
1350 default:
1351 return ERR_INVAL;
1352 break;
1353 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01001354 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1355 while (cnt-- > 0) {
1356 switch (info->portwidth) {
1357 case FLASH_CFI_8BIT:
1358 *dst.cp++ = *src.cp++;
1359 break;
1360 case FLASH_CFI_16BIT:
1361 *dst.wp++ = *src.wp++;
1362 break;
1363 case FLASH_CFI_32BIT:
1364 *dst.lp++ = *src.lp++;
1365 break;
1366 case FLASH_CFI_64BIT:
1367 *dst.llp++ = *src.llp++;
1368 break;
1369 default:
1370 return ERR_INVAL;
1371 break;
1372 }
1373 }
1374 flash_write_cmd (info, sector, 0,
1375 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1376 retcode = flash_full_status_check (info, sector,
1377 info->buffer_write_tout,
1378 "buffer write");
wdenk5653fc32004-02-08 22:55:38 +00001379 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01001380 return retcode;
1381
1382 case CFI_CMDSET_AMD_STANDARD:
1383 case CFI_CMDSET_AMD_EXTENDED:
1384 src.cp = cp;
1385 dst.cp = (uchar *) dest;
1386 sector = find_sector (info, dest);
1387
1388 flash_unlock_seq(info,0);
1389 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
1390
1391 switch (info->portwidth) {
1392 case FLASH_CFI_8BIT:
1393 cnt = len;
1394 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1395 while (cnt-- > 0) *dst.cp++ = *src.cp++;
1396 break;
1397 case FLASH_CFI_16BIT:
1398 cnt = len >> 1;
1399 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1400 while (cnt-- > 0) *dst.wp++ = *src.wp++;
1401 break;
1402 case FLASH_CFI_32BIT:
1403 cnt = len >> 2;
1404 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1405 while (cnt-- > 0) *dst.lp++ = *src.lp++;
1406 break;
1407 case FLASH_CFI_64BIT:
1408 cnt = len >> 3;
1409 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1410 while (cnt-- > 0) *dst.llp++ = *src.llp++;
1411 break;
1412 default:
1413 return ERR_INVAL;
1414 }
1415
1416 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1417 retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
1418 "buffer write");
1419 return retcode;
1420
1421 default:
1422 debug ("Unknown Command Set\n");
1423 return ERR_INVAL;
wdenk5653fc32004-02-08 22:55:38 +00001424 }
wdenk5653fc32004-02-08 22:55:38 +00001425}
wdenkcce625e2004-09-28 19:00:19 +00001426#endif /* CFG_FLASH_USE_BUFFER_WRITE */
wdenk5653fc32004-02-08 22:55:38 +00001427#endif /* CFG_FLASH_CFI */