blob: 37172379b9875ed3628092d4b894f7aa402c979a [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>
7 * Modified to work with AMD flashes
8 *
wdenke65527f2004-02-12 00:47:09 +00009 * Copyright (C) 2004
10 * Ed Okerson
11 * Modified to work with little-endian systems.
12 *
wdenk2cefd152004-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.
wdenke65527f2004-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
wdenk2cefd152004-02-08 22:55:38 +000036 *
37 * Tested Architectures
wdenke65527f2004-02-12 00:47:09 +000038 * Port Width Chip Width # of banks Flash Chip Board
wdenk2ebee312004-02-23 19:30:57 +000039 * 32 16 1 28F128J3 seranoa/eagle
40 * 64 16 1 28F128J3 seranoa/falcon
wdenk6cfa84e2004-02-10 00:03:41 +000041 *
wdenk2cefd152004-02-08 22:55:38 +000042 */
43
44/* The DEBUG define must be before common to enable debugging */
wdenk2ebee312004-02-23 19:30:57 +000045/* #define DEBUG */
46
wdenk2cefd152004-02-08 22:55:38 +000047#include <common.h>
Wolfgang Denkda06a332006-05-09 13:49:12 +020048#include <watchdog.h>
wdenk2cefd152004-02-08 22:55:38 +000049#include <asm/processor.h>
wdenkaeba06f2004-06-09 17:34:58 +000050#include <asm/byteorder.h>
wdenkd0245fc2005-04-13 10:02:42 +000051#include <environment.h>
wdenke65527f2004-02-12 00:47:09 +000052#ifdef CFG_FLASH_CFI_DRIVER
wdenke537b3b2004-02-23 23:54:43 +000053
wdenk2cefd152004-02-08 22:55:38 +000054/*
55 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
56 * The width of the port and the width of the chips are determined at initialization.
57 * These widths are used to calculate the address for access CFI data structures.
58 * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
59 *
60 * References
61 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
62 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
63 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
64 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
65 *
66 * TODO
67 *
68 * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
69 * Table (ALT) to determine if protection is available
70 *
71 * Add support for other command sets Use the PRI and ALT to determine command set
72 * Verify erase and program timeouts.
73 */
74
wdenke65527f2004-02-12 00:47:09 +000075#ifndef CFG_FLASH_BANKS_LIST
76#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
77#endif
78
wdenk2cefd152004-02-08 22:55:38 +000079#define FLASH_CMD_CFI 0x98
80#define FLASH_CMD_READ_ID 0x90
81#define FLASH_CMD_RESET 0xff
82#define FLASH_CMD_BLOCK_ERASE 0x20
83#define FLASH_CMD_ERASE_CONFIRM 0xD0
84#define FLASH_CMD_WRITE 0x40
85#define FLASH_CMD_PROTECT 0x60
86#define FLASH_CMD_PROTECT_SET 0x01
87#define FLASH_CMD_PROTECT_CLEAR 0xD0
88#define FLASH_CMD_CLEAR_STATUS 0x50
wdenke65527f2004-02-12 00:47:09 +000089#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
90#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk2cefd152004-02-08 22:55:38 +000091
92#define FLASH_STATUS_DONE 0x80
93#define FLASH_STATUS_ESS 0x40
94#define FLASH_STATUS_ECLBS 0x20
95#define FLASH_STATUS_PSLBS 0x10
96#define FLASH_STATUS_VPENS 0x08
97#define FLASH_STATUS_PSS 0x04
98#define FLASH_STATUS_DPS 0x02
99#define FLASH_STATUS_R 0x01
100#define FLASH_STATUS_PROTECT 0x01
101
102#define AMD_CMD_RESET 0xF0
103#define AMD_CMD_WRITE 0xA0
104#define AMD_CMD_ERASE_START 0x80
105#define AMD_CMD_ERASE_SECTOR 0x30
wdenked2ac4b2004-03-14 18:23:55 +0000106#define AMD_CMD_UNLOCK_START 0xAA
107#define AMD_CMD_UNLOCK_ACK 0x55
Stefan Roesec865e6c2006-02-28 15:29:58 +0100108#define AMD_CMD_WRITE_TO_BUFFER 0x25
109#define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
wdenk2cefd152004-02-08 22:55:38 +0000110
111#define AMD_STATUS_TOGGLE 0x40
112#define AMD_STATUS_ERROR 0x20
Stefan Roesec865e6c2006-02-28 15:29:58 +0100113
114#define AMD_ADDR_ERASE_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
115#define AMD_ADDR_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
116#define AMD_ADDR_ACK ((info->portwidth == FLASH_CFI_8BIT) ? 0x555 : 0x2AA)
wdenk2cefd152004-02-08 22:55:38 +0000117
118#define FLASH_OFFSET_CFI 0x55
119#define FLASH_OFFSET_CFI_RESP 0x10
wdenke65527f2004-02-12 00:47:09 +0000120#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
Stefan Roeseefef95b2006-04-01 13:41:03 +0200121#define FLASH_OFFSET_EXT_QUERY_T_P_ADDR 0x15 /* extended query table primary addr */
wdenk2cefd152004-02-08 22:55:38 +0000122#define FLASH_OFFSET_WTOUT 0x1F
wdenke65527f2004-02-12 00:47:09 +0000123#define FLASH_OFFSET_WBTOUT 0x20
wdenk2cefd152004-02-08 22:55:38 +0000124#define FLASH_OFFSET_ETOUT 0x21
wdenke65527f2004-02-12 00:47:09 +0000125#define FLASH_OFFSET_CETOUT 0x22
wdenk2cefd152004-02-08 22:55:38 +0000126#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenke65527f2004-02-12 00:47:09 +0000127#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk2cefd152004-02-08 22:55:38 +0000128#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenke65527f2004-02-12 00:47:09 +0000129#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk2cefd152004-02-08 22:55:38 +0000130#define FLASH_OFFSET_SIZE 0x27
wdenke65527f2004-02-12 00:47:09 +0000131#define FLASH_OFFSET_INTERFACE 0x28
132#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk2cefd152004-02-08 22:55:38 +0000133#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
134#define FLASH_OFFSET_ERASE_REGIONS 0x2D
135#define FLASH_OFFSET_PROTECT 0x02
wdenke65527f2004-02-12 00:47:09 +0000136#define FLASH_OFFSET_USER_PROTECTION 0x85
137#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk2cefd152004-02-08 22:55:38 +0000138
139
140#define FLASH_MAN_CFI 0x01000000
141
wdenke65527f2004-02-12 00:47:09 +0000142#define CFI_CMDSET_NONE 0
wdenk2cefd152004-02-08 22:55:38 +0000143#define CFI_CMDSET_INTEL_EXTENDED 1
wdenke65527f2004-02-12 00:47:09 +0000144#define CFI_CMDSET_AMD_STANDARD 2
wdenk2cefd152004-02-08 22:55:38 +0000145#define CFI_CMDSET_INTEL_STANDARD 3
wdenke65527f2004-02-12 00:47:09 +0000146#define CFI_CMDSET_AMD_EXTENDED 4
wdenk2cefd152004-02-08 22:55:38 +0000147#define CFI_CMDSET_MITSU_STANDARD 256
148#define CFI_CMDSET_MITSU_EXTENDED 257
wdenke65527f2004-02-12 00:47:09 +0000149#define CFI_CMDSET_SST 258
wdenk2cefd152004-02-08 22:55:38 +0000150
151
wdenk51242782004-12-18 22:35:43 +0000152#ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
153# undef FLASH_CMD_RESET
154# define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
155#endif
156
157
wdenk2cefd152004-02-08 22:55:38 +0000158typedef union {
159 unsigned char c;
160 unsigned short w;
161 unsigned long l;
162 unsigned long long ll;
163} cfiword_t;
164
165typedef union {
wdenke65527f2004-02-12 00:47:09 +0000166 volatile unsigned char *cp;
wdenk2cefd152004-02-08 22:55:38 +0000167 volatile unsigned short *wp;
wdenke65527f2004-02-12 00:47:09 +0000168 volatile unsigned long *lp;
wdenk2cefd152004-02-08 22:55:38 +0000169 volatile unsigned long long *llp;
170} cfiptr_t;
171
172#define NUM_ERASE_REGIONS 4
173
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200174/* use CFG_MAX_FLASH_BANKS_DETECT if defined */
175#ifdef CFG_MAX_FLASH_BANKS_DETECT
176static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
177flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
178#else
wdenk2cefd152004-02-08 22:55:38 +0000179static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200180flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
181#endif
wdenk2cefd152004-02-08 22:55:38 +0000182
Stefan Roesec865e6c2006-02-28 15:29:58 +0100183/*
184 * Check if chip width is defined. If not, start detecting with 8bit.
185 */
186#ifndef CFG_FLASH_CFI_WIDTH
187#define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
188#endif
189
wdenk2cefd152004-02-08 22:55:38 +0000190
191/*-----------------------------------------------------------------------
192 * Functions
193 */
194
195typedef unsigned long flash_sect_t;
196
wdenke65527f2004-02-12 00:47:09 +0000197static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
198static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenke537b3b2004-02-23 23:54:43 +0000199static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000200static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenke537b3b2004-02-23 23:54:43 +0000201static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
202static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
203static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000204static int flash_detect_cfi (flash_info_t * info);
wdenke537b3b2004-02-23 23:54:43 +0000205static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenke65527f2004-02-12 00:47:09 +0000206static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
207 ulong tout, char *prompt);
Stefan Roese896391f2006-03-01 17:00:49 +0100208ulong flash_get_size (ulong base, int banknum);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200209#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000210static flash_info_t *flash_get_info(ulong base);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200211#endif
wdenk2cefd152004-02-08 22:55:38 +0000212#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke537b3b2004-02-23 23:54:43 +0000213static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk2cefd152004-02-08 22:55:38 +0000214#endif
215
wdenke65527f2004-02-12 00:47:09 +0000216/*-----------------------------------------------------------------------
217 * create an address based on the offset and the port width
218 */
wdenke537b3b2004-02-23 23:54:43 +0000219inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000220{
221 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
222}
223
wdenk2cefd152004-02-08 22:55:38 +0000224#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000225/*-----------------------------------------------------------------------
226 * Debug support
227 */
228void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000229{
230 int i;
231 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000232
233 cp = (unsigned char *) &data;
234 for (i = 0; i < 8; i++)
235 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000236}
wdenke65527f2004-02-12 00:47:09 +0000237static void flash_printqry (flash_info_t * info, flash_sect_t sect)
238{
239 cfiptr_t cptr;
240 int x, y;
241
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200242 for (x = 0; x < 0x40; x += 16U / info->portwidth) {
wdenke65527f2004-02-12 00:47:09 +0000243 cptr.cp =
244 flash_make_addr (info, sect,
245 x + FLASH_OFFSET_CFI_RESP);
246 debug ("%p : ", cptr.cp);
247 for (y = 0; y < 16; y++) {
248 debug ("%2.2x ", cptr.cp[y]);
249 }
250 debug (" ");
251 for (y = 0; y < 16; y++) {
252 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
253 debug ("%c", cptr.cp[y]);
254 } else {
255 debug (".");
256 }
257 }
258 debug ("\n");
259 }
260}
wdenk2cefd152004-02-08 22:55:38 +0000261#endif
262
263
264/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000265 * read a character at a port width address
266 */
wdenke65527f2004-02-12 00:47:09 +0000267inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000268{
269 uchar *cp;
wdenke65527f2004-02-12 00:47:09 +0000270
271 cp = flash_make_addr (info, 0, offset);
272#if defined(__LITTLE_ENDIAN)
273 return (cp[0]);
274#else
wdenk2cefd152004-02-08 22:55:38 +0000275 return (cp[info->portwidth - 1]);
wdenke65527f2004-02-12 00:47:09 +0000276#endif
wdenk2cefd152004-02-08 22:55:38 +0000277}
278
279/*-----------------------------------------------------------------------
280 * read a short word by swapping for ppc format.
281 */
wdenke65527f2004-02-12 00:47:09 +0000282ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000283{
wdenke65527f2004-02-12 00:47:09 +0000284 uchar *addr;
285 ushort retval;
wdenk2cefd152004-02-08 22:55:38 +0000286
wdenke65527f2004-02-12 00:47:09 +0000287#ifdef DEBUG
288 int x;
289#endif
290 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000291
wdenke65527f2004-02-12 00:47:09 +0000292#ifdef DEBUG
293 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
294 info->portwidth);
295 for (x = 0; x < 2 * info->portwidth; x++) {
296 debug ("addr[%x] = 0x%x\n", x, addr[x]);
297 }
298#endif
299#if defined(__LITTLE_ENDIAN)
300 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
301#else
302 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
303 addr[info->portwidth - 1]);
304#endif
305
306 debug ("retval = 0x%x\n", retval);
307 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000308}
309
310/*-----------------------------------------------------------------------
311 * read a long word by picking the least significant byte of each maiximum
312 * port size word. Swap for ppc format.
313 */
wdenke65527f2004-02-12 00:47:09 +0000314ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000315{
wdenke65527f2004-02-12 00:47:09 +0000316 uchar *addr;
317 ulong retval;
318
319#ifdef DEBUG
320 int x;
321#endif
322 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000323
wdenke65527f2004-02-12 00:47:09 +0000324#ifdef DEBUG
325 debug ("long addr is at %p info->portwidth = %d\n", addr,
326 info->portwidth);
327 for (x = 0; x < 4 * info->portwidth; x++) {
328 debug ("addr[%x] = 0x%x\n", x, addr[x]);
329 }
330#endif
331#if defined(__LITTLE_ENDIAN)
332 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenke537b3b2004-02-23 23:54:43 +0000333 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenke65527f2004-02-12 00:47:09 +0000334#else
335 retval = (addr[(2 * info->portwidth) - 1] << 24) |
336 (addr[(info->portwidth) - 1] << 16) |
337 (addr[(4 * info->portwidth) - 1] << 8) |
338 addr[(3 * info->portwidth) - 1];
339#endif
340 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000341}
342
Stefan Roesec865e6c2006-02-28 15:29:58 +0100343
wdenk2cefd152004-02-08 22:55:38 +0000344/*-----------------------------------------------------------------------
345 */
346unsigned long flash_init (void)
347{
348 unsigned long size = 0;
349 int i;
350
Stefan Roeseefef95b2006-04-01 13:41:03 +0200351#ifdef CFG_FLASH_PROTECTION
352 char *s = getenv("unlock");
353#endif
354
wdenk2cefd152004-02-08 22:55:38 +0000355 /* Init: no FLASHes known */
wdenke65527f2004-02-12 00:47:09 +0000356 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000357 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenke65527f2004-02-12 00:47:09 +0000358 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk2cefd152004-02-08 22:55:38 +0000359 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Stefan Roesec443fe92005-11-22 13:20:42 +0100360#ifndef CFG_FLASH_QUIET_TEST
wdenke537b3b2004-02-23 23:54:43 +0000361 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
362 i, flash_info[i].size, flash_info[i].size << 20);
Stefan Roesec443fe92005-11-22 13:20:42 +0100363#endif /* CFG_FLASH_QUIET_TEST */
wdenk2cefd152004-02-08 22:55:38 +0000364 }
Stefan Roesec865e6c2006-02-28 15:29:58 +0100365#ifdef CFG_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +0200366 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
367 /*
368 * Only the U-Boot image and it's environment is protected,
369 * all other sectors are unprotected (unlocked) if flash
370 * hardware protection is used (CFG_FLASH_PROTECTION) and
371 * the environment variable "unlock" is set to "yes".
372 */
373 if (flash_info[i].legacy_unlock) {
374 int k;
Stefan Roesec865e6c2006-02-28 15:29:58 +0100375
Stefan Roeseefef95b2006-04-01 13:41:03 +0200376 /*
377 * Disable legacy_unlock temporarily, since
378 * flash_real_protect would relock all other sectors
379 * again otherwise.
380 */
381 flash_info[i].legacy_unlock = 0;
382
383 /*
384 * Legacy unlocking (e.g. Intel J3) -> unlock only one
385 * sector. This will unlock all sectors.
386 */
387 flash_real_protect (&flash_info[i], 0, 0);
388
389 flash_info[i].legacy_unlock = 1;
390
391 /*
392 * Manually mark other sectors as unlocked (unprotected)
393 */
394 for (k = 1; k < flash_info[i].sector_count; k++)
395 flash_info[i].protect[k] = 0;
396 } else {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100397 /*
Stefan Roeseefef95b2006-04-01 13:41:03 +0200398 * No legancy unlocking -> unlock all sectors
Stefan Roesec865e6c2006-02-28 15:29:58 +0100399 */
400 flash_protect (FLAG_PROTECT_CLEAR,
401 flash_info[i].start[0],
402 flash_info[i].start[0] + flash_info[i].size - 1,
403 &flash_info[i]);
404 }
405 }
406#endif /* CFG_FLASH_PROTECTION */
wdenk2cefd152004-02-08 22:55:38 +0000407 }
408
409 /* Monitor protection ON by default */
410#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenke65527f2004-02-12 00:47:09 +0000411 flash_protect (FLAG_PROTECT_SET,
412 CFG_MONITOR_BASE,
wdenk5c71a7a2005-05-16 15:23:22 +0000413 CFG_MONITOR_BASE + monitor_flash_len - 1,
414 flash_get_info(CFG_MONITOR_BASE));
wdenk2cefd152004-02-08 22:55:38 +0000415#endif
416
wdenke85b7a52004-10-10 22:16:06 +0000417 /* Environment protection ON by default */
418#ifdef CFG_ENV_IS_IN_FLASH
419 flash_protect (FLAG_PROTECT_SET,
420 CFG_ENV_ADDR,
421 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000422 flash_get_info(CFG_ENV_ADDR));
wdenke85b7a52004-10-10 22:16:06 +0000423#endif
424
425 /* Redundant environment protection ON by default */
426#ifdef CFG_ENV_ADDR_REDUND
427 flash_protect (FLAG_PROTECT_SET,
428 CFG_ENV_ADDR_REDUND,
429 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000430 flash_get_info(CFG_ENV_ADDR_REDUND));
wdenke85b7a52004-10-10 22:16:06 +0000431#endif
wdenk2cefd152004-02-08 22:55:38 +0000432 return (size);
433}
434
435/*-----------------------------------------------------------------------
436 */
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200437#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000438static flash_info_t *flash_get_info(ulong base)
439{
440 int i;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200441 flash_info_t * info = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000442
443 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
444 info = & flash_info[i];
445 if (info->size && info->start[0] <= base &&
446 base <= info->start[0] + info->size - 1)
447 break;
448 }
449
450 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
451}
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200452#endif
wdenk5c71a7a2005-05-16 15:23:22 +0000453
454/*-----------------------------------------------------------------------
455 */
wdenke65527f2004-02-12 00:47:09 +0000456int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +0000457{
458 int rcode = 0;
459 int prot;
460 flash_sect_t sect;
461
wdenke65527f2004-02-12 00:47:09 +0000462 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000463 puts ("Can't erase unknown flash type - aborted\n");
wdenk2cefd152004-02-08 22:55:38 +0000464 return 1;
465 }
466 if ((s_first < 0) || (s_first > s_last)) {
wdenk42c05472004-03-23 22:14:11 +0000467 puts ("- no sectors to erase\n");
wdenk2cefd152004-02-08 22:55:38 +0000468 return 1;
469 }
470
471 prot = 0;
wdenke65527f2004-02-12 00:47:09 +0000472 for (sect = s_first; sect <= s_last; ++sect) {
wdenk2cefd152004-02-08 22:55:38 +0000473 if (info->protect[sect]) {
474 prot++;
475 }
476 }
477 if (prot) {
wdenke65527f2004-02-12 00:47:09 +0000478 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk2cefd152004-02-08 22:55:38 +0000479 } else {
wdenk42c05472004-03-23 22:14:11 +0000480 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000481 }
482
483
wdenke65527f2004-02-12 00:47:09 +0000484 for (sect = s_first; sect <= s_last; sect++) {
wdenk2cefd152004-02-08 22:55:38 +0000485 if (info->protect[sect] == 0) { /* not protected */
wdenke65527f2004-02-12 00:47:09 +0000486 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000487 case CFI_CMDSET_INTEL_STANDARD:
488 case CFI_CMDSET_INTEL_EXTENDED:
wdenke537b3b2004-02-23 23:54:43 +0000489 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
490 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
491 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk2cefd152004-02-08 22:55:38 +0000492 break;
493 case CFI_CMDSET_AMD_STANDARD:
494 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000495 flash_unlock_seq (info, sect);
wdenked2ac4b2004-03-14 18:23:55 +0000496 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
497 AMD_CMD_ERASE_START);
wdenke65527f2004-02-12 00:47:09 +0000498 flash_unlock_seq (info, sect);
wdenke537b3b2004-02-23 23:54:43 +0000499 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk2cefd152004-02-08 22:55:38 +0000500 break;
501 default:
wdenke65527f2004-02-12 00:47:09 +0000502 debug ("Unkown flash vendor %d\n",
503 info->vendor);
wdenk2cefd152004-02-08 22:55:38 +0000504 break;
505 }
506
wdenke65527f2004-02-12 00:47:09 +0000507 if (flash_full_status_check
508 (info, sect, info->erase_blk_tout, "erase")) {
wdenk2cefd152004-02-08 22:55:38 +0000509 rcode = 1;
510 } else
wdenk42c05472004-03-23 22:14:11 +0000511 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +0000512 }
513 }
wdenk42c05472004-03-23 22:14:11 +0000514 puts (" done\n");
wdenk2cefd152004-02-08 22:55:38 +0000515 return rcode;
516}
517
518/*-----------------------------------------------------------------------
519 */
wdenke65527f2004-02-12 00:47:09 +0000520void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +0000521{
522 int i;
523
524 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000525 puts ("missing or unknown FLASH type\n");
wdenk2cefd152004-02-08 22:55:38 +0000526 return;
527 }
528
wdenke65527f2004-02-12 00:47:09 +0000529 printf ("CFI conformant FLASH (%d x %d)",
530 (info->portwidth << 3), (info->chipwidth << 3));
wdenk2cefd152004-02-08 22:55:38 +0000531 printf (" Size: %ld MB in %d Sectors\n",
532 info->size >> 20, info->sector_count);
wdenke537b3b2004-02-23 23:54:43 +0000533 printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
534 info->erase_blk_tout,
535 info->write_tout,
536 info->buffer_write_tout,
537 info->buffer_size);
wdenk2cefd152004-02-08 22:55:38 +0000538
wdenk42c05472004-03-23 22:14:11 +0000539 puts (" Sector Start Addresses:");
wdenke65527f2004-02-12 00:47:09 +0000540 for (i = 0; i < info->sector_count; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000541#ifdef CFG_FLASH_EMPTY_INFO
542 int k;
543 int size;
544 int erased;
545 volatile unsigned long *flash;
546
547 /*
548 * Check if whole sector is erased
549 */
wdenke65527f2004-02-12 00:47:09 +0000550 if (i != (info->sector_count - 1))
551 size = info->start[i + 1] - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000552 else
wdenke65527f2004-02-12 00:47:09 +0000553 size = info->start[0] + info->size - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000554 erased = 1;
wdenke65527f2004-02-12 00:47:09 +0000555 flash = (volatile unsigned long *) info->start[i];
556 size = size >> 2; /* divide by 4 for longword access */
557 for (k = 0; k < size; k++) {
558 if (*flash++ != 0xffffffff) {
559 erased = 0;
560 break;
561 }
562 }
wdenk2cefd152004-02-08 22:55:38 +0000563
564 if ((i % 5) == 0)
565 printf ("\n");
566 /* print empty and read-only info */
567 printf (" %08lX%s%s",
568 info->start[i],
569 erased ? " E" : " ",
570 info->protect[i] ? "RO " : " ");
Wolfgang Denkd3abe5a2005-09-25 00:23:05 +0200571#else /* ! CFG_FLASH_EMPTY_INFO */
wdenk2cefd152004-02-08 22:55:38 +0000572 if ((i % 5) == 0)
573 printf ("\n ");
574 printf (" %08lX%s",
Wolfgang Denkd3abe5a2005-09-25 00:23:05 +0200575 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenk2cefd152004-02-08 22:55:38 +0000576#endif
577 }
wdenk42c05472004-03-23 22:14:11 +0000578 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000579 return;
580}
581
582/*-----------------------------------------------------------------------
583 * Copy memory to flash, returns:
584 * 0 - OK
585 * 1 - write timeout
586 * 2 - Flash not erased
587 */
wdenke65527f2004-02-12 00:47:09 +0000588int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +0000589{
590 ulong wp;
591 ulong cp;
592 int aln;
593 cfiword_t cword;
594 int i, rc;
595
wdenke65527f2004-02-12 00:47:09 +0000596#ifdef CFG_FLASH_USE_BUFFER_WRITE
597 int buffered_size;
598#endif
wdenke65527f2004-02-12 00:47:09 +0000599 /* get lower aligned address */
wdenk2cefd152004-02-08 22:55:38 +0000600 /* get lower aligned address */
601 wp = (addr & ~(info->portwidth - 1));
602
603 /* handle unaligned start */
wdenke65527f2004-02-12 00:47:09 +0000604 if ((aln = addr - wp) != 0) {
wdenk2cefd152004-02-08 22:55:38 +0000605 cword.l = 0;
606 cp = wp;
wdenke65527f2004-02-12 00:47:09 +0000607 for (i = 0; i < aln; ++i, ++cp)
608 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000609
wdenke65527f2004-02-12 00:47:09 +0000610 for (; (i < info->portwidth) && (cnt > 0); i++) {
611 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000612 cnt--;
613 cp++;
614 }
wdenke65527f2004-02-12 00:47:09 +0000615 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
616 flash_add_byte (info, &cword, (*(uchar *) cp));
617 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000618 return rc;
619 wp = cp;
620 }
621
wdenke65527f2004-02-12 00:47:09 +0000622 /* handle the aligned part */
wdenk2cefd152004-02-08 22:55:38 +0000623#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke65527f2004-02-12 00:47:09 +0000624 buffered_size = (info->portwidth / info->chipwidth);
625 buffered_size *= info->buffer_size;
626 while (cnt >= info->portwidth) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100627 /* prohibit buffer write when buffer_size is 1 */
628 if (info->buffer_size == 1) {
629 cword.l = 0;
630 for (i = 0; i < info->portwidth; i++)
631 flash_add_byte (info, &cword, *src++);
632 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
633 return rc;
634 wp += info->portwidth;
635 cnt -= info->portwidth;
636 continue;
637 }
638
639 /* write buffer until next buffered_size aligned boundary */
640 i = buffered_size - (wp % buffered_size);
641 if (i > cnt)
642 i = cnt;
wdenke65527f2004-02-12 00:47:09 +0000643 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk2cefd152004-02-08 22:55:38 +0000644 return rc;
Wolfgang Denke25f0522005-08-12 22:35:59 +0200645 i -= i & (info->portwidth - 1);
wdenk2cefd152004-02-08 22:55:38 +0000646 wp += i;
647 src += i;
wdenke65527f2004-02-12 00:47:09 +0000648 cnt -= i;
wdenk2cefd152004-02-08 22:55:38 +0000649 }
650#else
wdenke65527f2004-02-12 00:47:09 +0000651 while (cnt >= info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000652 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000653 for (i = 0; i < info->portwidth; i++) {
654 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000655 }
wdenke65527f2004-02-12 00:47:09 +0000656 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000657 return rc;
658 wp += info->portwidth;
659 cnt -= info->portwidth;
660 }
661#endif /* CFG_FLASH_USE_BUFFER_WRITE */
662 if (cnt == 0) {
663 return (0);
664 }
665
666 /*
667 * handle unaligned tail bytes
668 */
669 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000670 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
671 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000672 --cnt;
673 }
wdenke65527f2004-02-12 00:47:09 +0000674 for (; i < info->portwidth; ++i, ++cp) {
675 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000676 }
677
wdenke65527f2004-02-12 00:47:09 +0000678 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +0000679}
680
681/*-----------------------------------------------------------------------
682 */
683#ifdef CFG_FLASH_PROTECTION
684
wdenke65527f2004-02-12 00:47:09 +0000685int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +0000686{
687 int retcode = 0;
688
wdenke65527f2004-02-12 00:47:09 +0000689 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
690 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
691 if (prot)
692 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk2cefd152004-02-08 22:55:38 +0000693 else
wdenke65527f2004-02-12 00:47:09 +0000694 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk2cefd152004-02-08 22:55:38 +0000695
wdenke65527f2004-02-12 00:47:09 +0000696 if ((retcode =
697 flash_full_status_check (info, sector, info->erase_blk_tout,
698 prot ? "protect" : "unprotect")) == 0) {
wdenk2cefd152004-02-08 22:55:38 +0000699
700 info->protect[sector] = prot;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200701
702 /*
703 * On some of Intel's flash chips (marked via legacy_unlock)
704 * unprotect unprotects all locking.
705 */
706 if ((prot == 0) && (info->legacy_unlock)) {
wdenk2cefd152004-02-08 22:55:38 +0000707 flash_sect_t i;
wdenke65527f2004-02-12 00:47:09 +0000708
709 for (i = 0; i < info->sector_count; i++) {
710 if (info->protect[i])
711 flash_real_protect (info, i, 1);
wdenk2cefd152004-02-08 22:55:38 +0000712 }
713 }
714 }
wdenk2cefd152004-02-08 22:55:38 +0000715 return retcode;
wdenke65527f2004-02-12 00:47:09 +0000716}
717
wdenk2cefd152004-02-08 22:55:38 +0000718/*-----------------------------------------------------------------------
719 * flash_read_user_serial - read the OneTimeProgramming cells
720 */
wdenke65527f2004-02-12 00:47:09 +0000721void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
722 int len)
wdenk2cefd152004-02-08 22:55:38 +0000723{
wdenke65527f2004-02-12 00:47:09 +0000724 uchar *src;
725 uchar *dst;
wdenk2cefd152004-02-08 22:55:38 +0000726
727 dst = buffer;
wdenke65527f2004-02-12 00:47:09 +0000728 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
729 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
730 memcpy (dst, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200731 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000732}
wdenke65527f2004-02-12 00:47:09 +0000733
wdenk2cefd152004-02-08 22:55:38 +0000734/*
735 * flash_read_factory_serial - read the device Id from the protection area
736 */
wdenke65527f2004-02-12 00:47:09 +0000737void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
738 int len)
wdenk2cefd152004-02-08 22:55:38 +0000739{
wdenke65527f2004-02-12 00:47:09 +0000740 uchar *src;
wdenk6cfa84e2004-02-10 00:03:41 +0000741
wdenke65527f2004-02-12 00:47:09 +0000742 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
743 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
744 memcpy (buffer, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200745 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000746}
747
748#endif /* CFG_FLASH_PROTECTION */
749
wdenke65527f2004-02-12 00:47:09 +0000750/*
751 * flash_is_busy - check to see if the flash is busy
752 * This routine checks the status of the chip and returns true if the chip is busy
753 */
754static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000755{
756 int retval;
wdenke65527f2004-02-12 00:47:09 +0000757
758 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000759 case CFI_CMDSET_INTEL_STANDARD:
760 case CFI_CMDSET_INTEL_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000761 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk2cefd152004-02-08 22:55:38 +0000762 break;
763 case CFI_CMDSET_AMD_STANDARD:
764 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000765 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk2cefd152004-02-08 22:55:38 +0000766 break;
767 default:
768 retval = 0;
769 }
wdenke65527f2004-02-12 00:47:09 +0000770 debug ("flash_is_busy: %d\n", retval);
wdenk2cefd152004-02-08 22:55:38 +0000771 return retval;
772}
wdenke65527f2004-02-12 00:47:09 +0000773
wdenk2cefd152004-02-08 22:55:38 +0000774/*-----------------------------------------------------------------------
775 * wait for XSR.7 to be set. Time out with an error if it does not.
776 * This routine does not set the flash to read-array mode.
777 */
wdenke65527f2004-02-12 00:47:09 +0000778static int flash_status_check (flash_info_t * info, flash_sect_t sector,
779 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000780{
781 ulong start;
782
Stefan Roeseefef95b2006-04-01 13:41:03 +0200783#if CFG_HZ != 1000
784 tout *= CFG_HZ/1000;
785#endif
786
wdenk2cefd152004-02-08 22:55:38 +0000787 /* Wait for command completion */
788 start = get_timer (0);
wdenke65527f2004-02-12 00:47:09 +0000789 while (flash_is_busy (info, sector)) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100790 if (get_timer (start) > tout) {
wdenke65527f2004-02-12 00:47:09 +0000791 printf ("Flash %s timeout at address %lx data %lx\n",
792 prompt, info->start[sector],
793 flash_read_long (info, sector, 0));
794 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000795 return ERR_TIMOUT;
796 }
797 }
798 return ERR_OK;
799}
wdenke65527f2004-02-12 00:47:09 +0000800
wdenk2cefd152004-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 */
wdenke65527f2004-02-12 00:47:09 +0000805static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
806 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000807{
808 int retcode;
wdenke65527f2004-02-12 00:47:09 +0000809
810 retcode = flash_status_check (info, sector, tout, prompt);
811 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000812 case CFI_CMDSET_INTEL_EXTENDED:
813 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesec865e6c2006-02-28 15:29:58 +0100814 if ((retcode == ERR_OK)
wdenke65527f2004-02-12 00:47:09 +0000815 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk2cefd152004-02-08 22:55:38 +0000816 retcode = ERR_INVAL;
wdenke65527f2004-02-12 00:47:09 +0000817 printf ("Flash %s error at address %lx\n", prompt,
818 info->start[sector]);
wdenke537b3b2004-02-23 23:54:43 +0000819 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000820 puts ("Command Sequence Error.\n");
wdenke537b3b2004-02-23 23:54:43 +0000821 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000822 puts ("Block Erase Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000823 retcode = ERR_NOT_ERASED;
wdenke537b3b2004-02-23 23:54:43 +0000824 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000825 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000826 }
wdenke65527f2004-02-12 00:47:09 +0000827 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk42c05472004-03-23 22:14:11 +0000828 puts ("Block locked.\n");
wdenke65527f2004-02-12 00:47:09 +0000829 retcode = ERR_PROTECTED;
830 }
831 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk42c05472004-03-23 22:14:11 +0000832 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000833 }
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200834 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000835 break;
836 default:
837 break;
838 }
839 return retcode;
840}
wdenke65527f2004-02-12 00:47:09 +0000841
wdenk2cefd152004-02-08 22:55:38 +0000842/*-----------------------------------------------------------------------
843 */
wdenke65527f2004-02-12 00:47:09 +0000844static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000845{
wdenk369d43d2004-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
wdenke65527f2004-02-12 00:47:09 +0000852 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000853 case FLASH_CFI_8BIT:
854 cword->c = c;
855 break;
856 case FLASH_CFI_16BIT:
wdenk369d43d2004-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
wdenk2cefd152004-02-08 22:55:38 +0000862 cword->w = (cword->w << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000863#endif
wdenk2cefd152004-02-08 22:55:38 +0000864 break;
865 case FLASH_CFI_32BIT:
wdenk369d43d2004-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
wdenk2cefd152004-02-08 22:55:38 +0000871 cword->l = (cword->l << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000872#endif
wdenk2cefd152004-02-08 22:55:38 +0000873 break;
874 case FLASH_CFI_64BIT:
wdenk369d43d2004-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
wdenk2cefd152004-02-08 22:55:38 +0000880 cword->ll = (cword->ll << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000881#endif
wdenk2cefd152004-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 */
wdenke65527f2004-02-12 00:47:09 +0000890static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk2cefd152004-02-08 22:55:38 +0000891{
892 int i;
wdenke65527f2004-02-12 00:47:09 +0000893 uchar *cp = (uchar *) cmdbuf;
894
wdenke65527f2004-02-12 00:47:09 +0000895#if defined(__LITTLE_ENDIAN)
Wolfgang Denk07bdb022005-09-24 23:32:48 +0200896 for (i = info->portwidth; i > 0; i--)
897#else
898 for (i = 1; i <= info->portwidth; i++)
wdenke65527f2004-02-12 00:47:09 +0000899#endif
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200900 *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
wdenk2cefd152004-02-08 22:55:38 +0000901}
902
903/*
904 * Write a proper sized command to the correct address
905 */
wdenke537b3b2004-02-23 23:54:43 +0000906static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000907{
908
909 volatile cfiptr_t addr;
910 cfiword_t cword;
wdenke65527f2004-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) {
wdenk2cefd152004-02-08 22:55:38 +0000915 case FLASH_CFI_8BIT:
wdenke65527f2004-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);
wdenk2cefd152004-02-08 22:55:38 +0000918 *addr.cp = cword.c;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100919#ifdef CONFIG_BLACKFIN
920 asm("ssync;");
921#endif
wdenk2cefd152004-02-08 22:55:38 +0000922 break;
923 case FLASH_CFI_16BIT:
wdenke65527f2004-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,
wdenk2cefd152004-02-08 22:55:38 +0000926 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
927 *addr.wp = cword.w;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100928#ifdef CONFIG_BLACKFIN
929 asm("ssync;");
930#endif
wdenk2cefd152004-02-08 22:55:38 +0000931 break;
932 case FLASH_CFI_32BIT:
wdenke65527f2004-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,
wdenk2cefd152004-02-08 22:55:38 +0000935 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
936 *addr.lp = cword.l;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100937#ifdef CONFIG_BLACKFIN
938 asm("ssync;");
939#endif
wdenk2cefd152004-02-08 22:55:38 +0000940 break;
941 case FLASH_CFI_64BIT:
942#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000943 {
wdenk2cefd152004-02-08 22:55:38 +0000944 char str[20];
wdenk6cfa84e2004-02-10 00:03:41 +0000945
wdenke65527f2004-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,
wdenk2cefd152004-02-08 22:55:38 +0000950 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
951 }
952#endif
953 *addr.llp = cword.ll;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100954#ifdef CONFIG_BLACKFIN
955 asm("ssync;");
956#endif
wdenk2cefd152004-02-08 22:55:38 +0000957 break;
958 }
959}
960
wdenke65527f2004-02-12 00:47:09 +0000961static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000962{
wdenked2ac4b2004-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);
wdenk2cefd152004-02-08 22:55:38 +0000965}
wdenke65527f2004-02-12 00:47:09 +0000966
wdenk2cefd152004-02-08 22:55:38 +0000967/*-----------------------------------------------------------------------
968 */
wdenke537b3b2004-02-23 23:54:43 +0000969static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000970{
971 cfiptr_t cptr;
972 cfiword_t cword;
973 int retval;
wdenke65527f2004-02-12 00:47:09 +0000974
975 cptr.cp = flash_make_addr (info, sect, offset);
976 flash_make_cmd (info, cmd, &cword);
wdenk2cefd152004-02-08 22:55:38 +0000977
wdenke65527f2004-02-12 00:47:09 +0000978 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
979 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000980 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000981 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk2cefd152004-02-08 22:55:38 +0000982 retval = (cptr.cp[0] == cword.c);
983 break;
984 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000985 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk2cefd152004-02-08 22:55:38 +0000986 retval = (cptr.wp[0] == cword.w);
987 break;
988 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000989 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk2cefd152004-02-08 22:55:38 +0000990 retval = (cptr.lp[0] == cword.l);
991 break;
992 case FLASH_CFI_64BIT:
wdenk6cfa84e2004-02-10 00:03:41 +0000993#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000994 {
wdenk2cefd152004-02-08 22:55:38 +0000995 char str1[20];
996 char str2[20];
wdenke65527f2004-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);
wdenk2cefd152004-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}
wdenke65527f2004-02-12 00:47:09 +00001011
wdenk2cefd152004-02-08 22:55:38 +00001012/*-----------------------------------------------------------------------
1013 */
wdenke537b3b2004-02-23 23:54:43 +00001014static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001015{
1016 cfiptr_t cptr;
1017 cfiword_t cword;
1018 int retval;
wdenke65527f2004-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) {
wdenk2cefd152004-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);
wdenke65527f2004-02-12 00:47:09 +00001034 break;
wdenk2cefd152004-02-08 22:55:38 +00001035 default:
1036 retval = 0;
1037 break;
1038 }
1039 return retval;
1040}
1041
1042/*-----------------------------------------------------------------------
1043 */
wdenke537b3b2004-02-23 23:54:43 +00001044static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001045{
1046 cfiptr_t cptr;
1047 cfiword_t cword;
1048 int retval;
wdenke65527f2004-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) {
wdenk2cefd152004-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:
wdenke65527f2004-02-12 00:47:09 +00001063 retval = ((cptr.llp[0] & cword.ll) !=
1064 (cptr.llp[0] & cword.ll));
wdenk2cefd152004-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*/
wdenke65527f2004-02-12 00:47:09 +00001078static int flash_detect_cfi (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001079{
wdenke65527f2004-02-12 00:47:09 +00001080 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001081
Stefan Roesec865e6c2006-02-28 15:29:58 +01001082 for (info->portwidth = CFG_FLASH_CFI_WIDTH;
wdenke65527f2004-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 Denka205a8f2005-09-25 16:41:22 +02001087 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenke537b3b2004-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);
wdenke65527f2004-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",
wdenke537b3b2004-02-23 23:54:43 +00001098 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1099 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk2cefd152004-02-08 22:55:38 +00001100 return 1;
1101 }
1102 }
1103 }
wdenke65527f2004-02-12 00:47:09 +00001104 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001105 return 0;
1106}
wdenke65527f2004-02-12 00:47:09 +00001107
wdenk2cefd152004-02-08 22:55:38 +00001108/*
1109 * The following code cannot be run from FLASH!
1110 *
1111 */
Marian Balakowicz513b4a12005-10-11 19:09:42 +02001112ulong flash_get_size (ulong base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001113{
wdenke65527f2004-02-12 00:47:09 +00001114 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-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;
wdenke65527f2004-02-12 00:47:09 +00001121 int erase_region_size;
1122 int erase_region_count;
Stefan Roeseefef95b2006-04-01 13:41:03 +02001123#ifdef CFG_FLASH_PROTECTION
1124 int ext_addr;
1125 info->legacy_unlock = 0;
1126#endif
wdenk2cefd152004-02-08 22:55:38 +00001127
1128 info->start[0] = base;
1129
wdenke65527f2004-02-12 00:47:09 +00001130 if (flash_detect_cfi (info)) {
wdenke537b3b2004-02-23 23:54:43 +00001131 info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
wdenke65527f2004-02-12 00:47:09 +00001132#ifdef DEBUG
1133 flash_printqry (info, 0);
1134#endif
1135 switch (info->vendor) {
wdenk2cefd152004-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 Roeseefef95b2006-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
wdenk2cefd152004-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 }
wdenk6cfa84e2004-02-10 00:03:41 +00001153
wdenke65527f2004-02-12 00:47:09 +00001154 debug ("manufacturer is %d\n", info->vendor);
wdenk2cefd152004-02-08 22:55:38 +00001155 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-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 }
wdenke537b3b2004-02-23 23:54:43 +00001161 num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
wdenke65527f2004-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);
wdenk2cefd152004-02-08 22:55:38 +00001166 sect_cnt = 0;
1167 sector = base;
wdenke65527f2004-02-12 00:47:09 +00001168 for (i = 0; i < num_erase_regions; i++) {
1169 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00001170 printf ("%d erase regions found, only %d used\n",
1171 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00001172 break;
1173 }
wdenke65527f2004-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;
wdenk2cefd152004-02-08 22:55:38 +00001179 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00001180 erase_region_count = (tmp & 0xffff) + 1;
wdenkaeba06f2004-06-09 17:34:58 +00001181 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00001182 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00001183 for (j = 0; j < erase_region_count; j++) {
wdenk2cefd152004-02-08 22:55:38 +00001184 info->start[sect_cnt] = sector;
1185 sector += (erase_region_size * size_ratio);
wdenk26c58432005-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
wdenk2cefd152004-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 */
wdenke537b3b2004-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));
wdenke65527f2004-02-12 00:47:09 +00001210 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenke537b3b2004-02-23 23:54:43 +00001211 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
Stefan Roeseefef95b2006-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 Roesec865e6c2006-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 */
wdenk2cefd152004-02-08 22:55:38 +00001218 info->flash_id = FLASH_MAN_CFI;
wdenked2ac4b2004-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 }
wdenk2cefd152004-02-08 22:55:38 +00001222 }
1223
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001224 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenke65527f2004-02-12 00:47:09 +00001225 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00001226}
1227
Stefan Roesec865e6c2006-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}
wdenk2cefd152004-02-08 22:55:38 +00001242
1243/*-----------------------------------------------------------------------
1244 */
wdenke65527f2004-02-12 00:47:09 +00001245static int flash_write_cfiword (flash_info_t * info, ulong dest,
1246 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +00001247{
wdenk2cefd152004-02-08 22:55:38 +00001248 cfiptr_t ctladdr;
1249 cfiptr_t cptr;
1250 int flag;
1251
wdenke65527f2004-02-12 00:47:09 +00001252 ctladdr.cp = flash_make_addr (info, 0, 0);
1253 cptr.cp = (uchar *) dest;
wdenk2cefd152004-02-08 22:55:38 +00001254
1255
1256 /* Check if Flash is (sufficiently) erased */
wdenke65527f2004-02-12 00:47:09 +00001257 switch (info->portwidth) {
wdenk2cefd152004-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:
wdenke65527f2004-02-12 00:47:09 +00001265 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk2cefd152004-02-08 22:55:38 +00001266 break;
1267 case FLASH_CFI_64BIT:
wdenk391b5742004-10-10 23:27:33 +00001268 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenk2cefd152004-02-08 22:55:38 +00001269 break;
1270 default:
1271 return 2;
1272 }
wdenke65527f2004-02-12 00:47:09 +00001273 if (!flag)
wdenk2cefd152004-02-08 22:55:38 +00001274 return 2;
1275
1276 /* Disable interrupts which might cause a timeout here */
wdenke65527f2004-02-12 00:47:09 +00001277 flag = disable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001278
wdenke65527f2004-02-12 00:47:09 +00001279 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001280 case CFI_CMDSET_INTEL_EXTENDED:
1281 case CFI_CMDSET_INTEL_STANDARD:
wdenke65527f2004-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);
wdenk2cefd152004-02-08 22:55:38 +00001284 break;
1285 case CFI_CMDSET_AMD_EXTENDED:
1286 case CFI_CMDSET_AMD_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001287 flash_unlock_seq (info, 0);
wdenked2ac4b2004-03-14 18:23:55 +00001288 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001289 break;
1290 }
1291
wdenke65527f2004-02-12 00:47:09 +00001292 switch (info->portwidth) {
wdenk2cefd152004-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 */
wdenke65527f2004-02-12 00:47:09 +00001308 if (flag)
1309 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001310
Wolfgang Denkda06a332006-05-09 13:49:12 +02001311#if defined(CONFIG_MCF52x2)
1312 WATCHDOG_RESET();
1313#endif
1314
Stefan Roesec865e6c2006-02-28 15:29:58 +01001315 return flash_full_status_check (info, find_sector (info, dest),
1316 info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +00001317}
1318
1319#ifdef CFG_FLASH_USE_BUFFER_WRITE
1320
wdenke65527f2004-02-12 00:47:09 +00001321static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1322 int len)
wdenk2cefd152004-02-08 22:55:38 +00001323{
1324 flash_sect_t sector;
1325 int cnt;
1326 int retcode;
1327 volatile cfiptr_t src;
1328 volatile cfiptr_t dst;
1329
Stefan Roesec865e6c2006-02-28 15:29:58 +01001330 switch (info->vendor) {
1331 case CFI_CMDSET_INTEL_STANDARD:
1332 case CFI_CMDSET_INTEL_EXTENDED:
1333 src.cp = cp;
1334 dst.cp = (uchar *) dest;
1335 sector = find_sector (info, dest);
1336 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1337 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1338 if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
1339 "write to buffer")) == ERR_OK) {
1340 /* reduce the number of loops by the width of the port */
1341 switch (info->portwidth) {
1342 case FLASH_CFI_8BIT:
1343 cnt = len;
1344 break;
1345 case FLASH_CFI_16BIT:
1346 cnt = len >> 1;
1347 break;
1348 case FLASH_CFI_32BIT:
1349 cnt = len >> 2;
1350 break;
1351 case FLASH_CFI_64BIT:
1352 cnt = len >> 3;
1353 break;
1354 default:
1355 return ERR_INVAL;
1356 break;
1357 }
1358 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1359 while (cnt-- > 0) {
1360 switch (info->portwidth) {
1361 case FLASH_CFI_8BIT:
1362 *dst.cp++ = *src.cp++;
1363 break;
1364 case FLASH_CFI_16BIT:
1365 *dst.wp++ = *src.wp++;
1366 break;
1367 case FLASH_CFI_32BIT:
1368 *dst.lp++ = *src.lp++;
1369 break;
1370 case FLASH_CFI_64BIT:
1371 *dst.llp++ = *src.llp++;
1372 break;
1373 default:
1374 return ERR_INVAL;
1375 break;
1376 }
1377 }
1378 flash_write_cmd (info, sector, 0,
1379 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1380 retcode = flash_full_status_check (info, sector,
1381 info->buffer_write_tout,
1382 "buffer write");
1383 }
1384 return retcode;
1385
1386 case CFI_CMDSET_AMD_STANDARD:
1387 case CFI_CMDSET_AMD_EXTENDED:
1388 src.cp = cp;
1389 dst.cp = (uchar *) dest;
1390 sector = find_sector (info, dest);
1391
1392 flash_unlock_seq(info,0);
1393 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
1394
wdenke65527f2004-02-12 00:47:09 +00001395 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001396 case FLASH_CFI_8BIT:
1397 cnt = len;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001398 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1399 while (cnt-- > 0) *dst.cp++ = *src.cp++;
wdenk2cefd152004-02-08 22:55:38 +00001400 break;
1401 case FLASH_CFI_16BIT:
1402 cnt = len >> 1;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001403 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1404 while (cnt-- > 0) *dst.wp++ = *src.wp++;
wdenk2cefd152004-02-08 22:55:38 +00001405 break;
1406 case FLASH_CFI_32BIT:
1407 cnt = len >> 2;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001408 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1409 while (cnt-- > 0) *dst.lp++ = *src.lp++;
wdenk2cefd152004-02-08 22:55:38 +00001410 break;
1411 case FLASH_CFI_64BIT:
1412 cnt = len >> 3;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001413 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1414 while (cnt-- > 0) *dst.llp++ = *src.llp++;
wdenk2cefd152004-02-08 22:55:38 +00001415 break;
1416 default:
1417 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001418 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01001419
1420 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1421 retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
1422 "buffer write");
1423 return retcode;
1424
1425 default:
1426 debug ("Unknown Command Set\n");
1427 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001428 }
wdenk2cefd152004-02-08 22:55:38 +00001429}
wdenk5b835a32004-09-28 19:00:19 +00001430#endif /* CFG_FLASH_USE_BUFFER_WRITE */
wdenk2cefd152004-02-08 22:55:38 +00001431#endif /* CFG_FLASH_CFI */