blob: a989d346629e8c5ff046b40500d15e3a3c2f1d32 [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>
48#include <asm/processor.h>
wdenkaeba06f2004-06-09 17:34:58 +000049#include <asm/byteorder.h>
wdenkd0245fc2005-04-13 10:02:42 +000050#include <environment.h>
wdenke65527f2004-02-12 00:47:09 +000051#ifdef CFG_FLASH_CFI_DRIVER
wdenke537b3b2004-02-23 23:54:43 +000052
wdenk2cefd152004-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
wdenke65527f2004-02-12 00:47:09 +000074#ifndef CFG_FLASH_BANKS_LIST
75#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
76#endif
77
wdenk2cefd152004-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
wdenke65527f2004-02-12 00:47:09 +000088#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
89#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk2cefd152004-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
wdenked2ac4b2004-03-14 18:23:55 +0000105#define AMD_CMD_UNLOCK_START 0xAA
106#define AMD_CMD_UNLOCK_ACK 0x55
Stefan Roesec865e6c2006-02-28 15:29:58 +0100107#define AMD_CMD_WRITE_TO_BUFFER 0x25
108#define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
wdenk2cefd152004-02-08 22:55:38 +0000109
110#define AMD_STATUS_TOGGLE 0x40
111#define AMD_STATUS_ERROR 0x20
Stefan Roesec865e6c2006-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)
wdenk2cefd152004-02-08 22:55:38 +0000116
117#define FLASH_OFFSET_CFI 0x55
118#define FLASH_OFFSET_CFI_RESP 0x10
wdenke65527f2004-02-12 00:47:09 +0000119#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
wdenk2cefd152004-02-08 22:55:38 +0000120#define FLASH_OFFSET_WTOUT 0x1F
wdenke65527f2004-02-12 00:47:09 +0000121#define FLASH_OFFSET_WBTOUT 0x20
wdenk2cefd152004-02-08 22:55:38 +0000122#define FLASH_OFFSET_ETOUT 0x21
wdenke65527f2004-02-12 00:47:09 +0000123#define FLASH_OFFSET_CETOUT 0x22
wdenk2cefd152004-02-08 22:55:38 +0000124#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenke65527f2004-02-12 00:47:09 +0000125#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk2cefd152004-02-08 22:55:38 +0000126#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenke65527f2004-02-12 00:47:09 +0000127#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk2cefd152004-02-08 22:55:38 +0000128#define FLASH_OFFSET_SIZE 0x27
wdenke65527f2004-02-12 00:47:09 +0000129#define FLASH_OFFSET_INTERFACE 0x28
130#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk2cefd152004-02-08 22:55:38 +0000131#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
132#define FLASH_OFFSET_ERASE_REGIONS 0x2D
133#define FLASH_OFFSET_PROTECT 0x02
wdenke65527f2004-02-12 00:47:09 +0000134#define FLASH_OFFSET_USER_PROTECTION 0x85
135#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk2cefd152004-02-08 22:55:38 +0000136
137
138#define FLASH_MAN_CFI 0x01000000
139
wdenke65527f2004-02-12 00:47:09 +0000140#define CFI_CMDSET_NONE 0
wdenk2cefd152004-02-08 22:55:38 +0000141#define CFI_CMDSET_INTEL_EXTENDED 1
wdenke65527f2004-02-12 00:47:09 +0000142#define CFI_CMDSET_AMD_STANDARD 2
wdenk2cefd152004-02-08 22:55:38 +0000143#define CFI_CMDSET_INTEL_STANDARD 3
wdenke65527f2004-02-12 00:47:09 +0000144#define CFI_CMDSET_AMD_EXTENDED 4
wdenk2cefd152004-02-08 22:55:38 +0000145#define CFI_CMDSET_MITSU_STANDARD 256
146#define CFI_CMDSET_MITSU_EXTENDED 257
wdenke65527f2004-02-12 00:47:09 +0000147#define CFI_CMDSET_SST 258
wdenk2cefd152004-02-08 22:55:38 +0000148
149
wdenk51242782004-12-18 22:35:43 +0000150#ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
151# undef FLASH_CMD_RESET
152# define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
153#endif
154
155
wdenk2cefd152004-02-08 22:55:38 +0000156typedef union {
157 unsigned char c;
158 unsigned short w;
159 unsigned long l;
160 unsigned long long ll;
161} cfiword_t;
162
163typedef union {
wdenke65527f2004-02-12 00:47:09 +0000164 volatile unsigned char *cp;
wdenk2cefd152004-02-08 22:55:38 +0000165 volatile unsigned short *wp;
wdenke65527f2004-02-12 00:47:09 +0000166 volatile unsigned long *lp;
wdenk2cefd152004-02-08 22:55:38 +0000167 volatile unsigned long long *llp;
168} cfiptr_t;
169
170#define NUM_ERASE_REGIONS 4
171
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200172/* use CFG_MAX_FLASH_BANKS_DETECT if defined */
173#ifdef CFG_MAX_FLASH_BANKS_DETECT
174static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
175flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
176#else
wdenk2cefd152004-02-08 22:55:38 +0000177static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200178flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
179#endif
wdenk2cefd152004-02-08 22:55:38 +0000180
Stefan Roesec865e6c2006-02-28 15:29:58 +0100181/*
182 * Check if chip width is defined. If not, start detecting with 8bit.
183 */
184#ifndef CFG_FLASH_CFI_WIDTH
185#define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
186#endif
187
wdenk2cefd152004-02-08 22:55:38 +0000188
189/*-----------------------------------------------------------------------
190 * Functions
191 */
192
193typedef unsigned long flash_sect_t;
194
wdenke65527f2004-02-12 00:47:09 +0000195static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
196static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenke537b3b2004-02-23 23:54:43 +0000197static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000198static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenke537b3b2004-02-23 23:54:43 +0000199static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
200static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
201static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000202static int flash_detect_cfi (flash_info_t * info);
wdenke537b3b2004-02-23 23:54:43 +0000203static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenke65527f2004-02-12 00:47:09 +0000204static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
205 ulong tout, char *prompt);
Stefan Roese896391f2006-03-01 17:00:49 +0100206ulong flash_get_size (ulong base, int banknum);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200207#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000208static flash_info_t *flash_get_info(ulong base);
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200209#endif
wdenk2cefd152004-02-08 22:55:38 +0000210#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke537b3b2004-02-23 23:54:43 +0000211static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk2cefd152004-02-08 22:55:38 +0000212#endif
213
wdenke65527f2004-02-12 00:47:09 +0000214/*-----------------------------------------------------------------------
215 * create an address based on the offset and the port width
216 */
wdenke537b3b2004-02-23 23:54:43 +0000217inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000218{
219 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
220}
221
wdenk2cefd152004-02-08 22:55:38 +0000222#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000223/*-----------------------------------------------------------------------
224 * Debug support
225 */
226void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000227{
228 int i;
229 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000230
231 cp = (unsigned char *) &data;
232 for (i = 0; i < 8; i++)
233 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000234}
wdenke65527f2004-02-12 00:47:09 +0000235static void flash_printqry (flash_info_t * info, flash_sect_t sect)
236{
237 cfiptr_t cptr;
238 int x, y;
239
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200240 for (x = 0; x < 0x40; x += 16U / info->portwidth) {
wdenke65527f2004-02-12 00:47:09 +0000241 cptr.cp =
242 flash_make_addr (info, sect,
243 x + FLASH_OFFSET_CFI_RESP);
244 debug ("%p : ", cptr.cp);
245 for (y = 0; y < 16; y++) {
246 debug ("%2.2x ", cptr.cp[y]);
247 }
248 debug (" ");
249 for (y = 0; y < 16; y++) {
250 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
251 debug ("%c", cptr.cp[y]);
252 } else {
253 debug (".");
254 }
255 }
256 debug ("\n");
257 }
258}
wdenk2cefd152004-02-08 22:55:38 +0000259#endif
260
261
262/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000263 * read a character at a port width address
264 */
wdenke65527f2004-02-12 00:47:09 +0000265inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000266{
267 uchar *cp;
wdenke65527f2004-02-12 00:47:09 +0000268
269 cp = flash_make_addr (info, 0, offset);
270#if defined(__LITTLE_ENDIAN)
271 return (cp[0]);
272#else
wdenk2cefd152004-02-08 22:55:38 +0000273 return (cp[info->portwidth - 1]);
wdenke65527f2004-02-12 00:47:09 +0000274#endif
wdenk2cefd152004-02-08 22:55:38 +0000275}
276
277/*-----------------------------------------------------------------------
278 * read a short word by swapping for ppc format.
279 */
wdenke65527f2004-02-12 00:47:09 +0000280ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000281{
wdenke65527f2004-02-12 00:47:09 +0000282 uchar *addr;
283 ushort retval;
wdenk2cefd152004-02-08 22:55:38 +0000284
wdenke65527f2004-02-12 00:47:09 +0000285#ifdef DEBUG
286 int x;
287#endif
288 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000289
wdenke65527f2004-02-12 00:47:09 +0000290#ifdef DEBUG
291 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
292 info->portwidth);
293 for (x = 0; x < 2 * info->portwidth; x++) {
294 debug ("addr[%x] = 0x%x\n", x, addr[x]);
295 }
296#endif
297#if defined(__LITTLE_ENDIAN)
298 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
299#else
300 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
301 addr[info->portwidth - 1]);
302#endif
303
304 debug ("retval = 0x%x\n", retval);
305 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000306}
307
308/*-----------------------------------------------------------------------
309 * read a long word by picking the least significant byte of each maiximum
310 * port size word. Swap for ppc format.
311 */
wdenke65527f2004-02-12 00:47:09 +0000312ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000313{
wdenke65527f2004-02-12 00:47:09 +0000314 uchar *addr;
315 ulong retval;
316
317#ifdef DEBUG
318 int x;
319#endif
320 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000321
wdenke65527f2004-02-12 00:47:09 +0000322#ifdef DEBUG
323 debug ("long addr is at %p info->portwidth = %d\n", addr,
324 info->portwidth);
325 for (x = 0; x < 4 * info->portwidth; x++) {
326 debug ("addr[%x] = 0x%x\n", x, addr[x]);
327 }
328#endif
329#if defined(__LITTLE_ENDIAN)
330 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenke537b3b2004-02-23 23:54:43 +0000331 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenke65527f2004-02-12 00:47:09 +0000332#else
333 retval = (addr[(2 * info->portwidth) - 1] << 24) |
334 (addr[(info->portwidth) - 1] << 16) |
335 (addr[(4 * info->portwidth) - 1] << 8) |
336 addr[(3 * info->portwidth) - 1];
337#endif
338 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000339}
340
Stefan Roesec865e6c2006-02-28 15:29:58 +0100341
wdenk2cefd152004-02-08 22:55:38 +0000342/*-----------------------------------------------------------------------
343 */
344unsigned long flash_init (void)
345{
346 unsigned long size = 0;
347 int i;
348
349 /* Init: no FLASHes known */
wdenke65527f2004-02-12 00:47:09 +0000350 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000351 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenke65527f2004-02-12 00:47:09 +0000352 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk2cefd152004-02-08 22:55:38 +0000353 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Stefan Roesec443fe92005-11-22 13:20:42 +0100354#ifndef CFG_FLASH_QUIET_TEST
wdenke537b3b2004-02-23 23:54:43 +0000355 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
356 i, flash_info[i].size, flash_info[i].size << 20);
Stefan Roesec443fe92005-11-22 13:20:42 +0100357#endif /* CFG_FLASH_QUIET_TEST */
wdenk2cefd152004-02-08 22:55:38 +0000358 }
Stefan Roesec865e6c2006-02-28 15:29:58 +0100359#ifdef CFG_FLASH_PROTECTION
360 else {
361 char *s = getenv("unlock");
362
363 if (((s = getenv("unlock")) != NULL) && (strcmp(s, "yes") == 0)) {
364 /*
365 * Only the U-Boot image and it's environment is protected,
366 * all other sectors are unprotected (unlocked) if flash
367 * hardware protection is used (CFG_FLASH_PROTECTION) and
368 * the environment variable "unlock" is set to "yes".
369 */
370 flash_protect (FLAG_PROTECT_CLEAR,
371 flash_info[i].start[0],
372 flash_info[i].start[0] + flash_info[i].size - 1,
373 &flash_info[i]);
374 }
375 }
376#endif /* CFG_FLASH_PROTECTION */
wdenk2cefd152004-02-08 22:55:38 +0000377 }
378
379 /* Monitor protection ON by default */
380#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenke65527f2004-02-12 00:47:09 +0000381 flash_protect (FLAG_PROTECT_SET,
382 CFG_MONITOR_BASE,
wdenk5c71a7a2005-05-16 15:23:22 +0000383 CFG_MONITOR_BASE + monitor_flash_len - 1,
384 flash_get_info(CFG_MONITOR_BASE));
wdenk2cefd152004-02-08 22:55:38 +0000385#endif
386
wdenke85b7a52004-10-10 22:16:06 +0000387 /* Environment protection ON by default */
388#ifdef CFG_ENV_IS_IN_FLASH
389 flash_protect (FLAG_PROTECT_SET,
390 CFG_ENV_ADDR,
391 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000392 flash_get_info(CFG_ENV_ADDR));
wdenke85b7a52004-10-10 22:16:06 +0000393#endif
394
395 /* Redundant environment protection ON by default */
396#ifdef CFG_ENV_ADDR_REDUND
397 flash_protect (FLAG_PROTECT_SET,
398 CFG_ENV_ADDR_REDUND,
399 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
wdenk5c71a7a2005-05-16 15:23:22 +0000400 flash_get_info(CFG_ENV_ADDR_REDUND));
wdenke85b7a52004-10-10 22:16:06 +0000401#endif
wdenk2cefd152004-02-08 22:55:38 +0000402 return (size);
403}
404
405/*-----------------------------------------------------------------------
406 */
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200407#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk5c71a7a2005-05-16 15:23:22 +0000408static flash_info_t *flash_get_info(ulong base)
409{
410 int i;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200411 flash_info_t * info = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000412
413 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
414 info = & flash_info[i];
415 if (info->size && info->start[0] <= base &&
416 base <= info->start[0] + info->size - 1)
417 break;
418 }
419
420 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
421}
Wolfgang Denkbc650fa2005-10-05 01:51:29 +0200422#endif
wdenk5c71a7a2005-05-16 15:23:22 +0000423
424/*-----------------------------------------------------------------------
425 */
wdenke65527f2004-02-12 00:47:09 +0000426int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +0000427{
428 int rcode = 0;
429 int prot;
430 flash_sect_t sect;
431
wdenke65527f2004-02-12 00:47:09 +0000432 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000433 puts ("Can't erase unknown flash type - aborted\n");
wdenk2cefd152004-02-08 22:55:38 +0000434 return 1;
435 }
436 if ((s_first < 0) || (s_first > s_last)) {
wdenk42c05472004-03-23 22:14:11 +0000437 puts ("- no sectors to erase\n");
wdenk2cefd152004-02-08 22:55:38 +0000438 return 1;
439 }
440
441 prot = 0;
wdenke65527f2004-02-12 00:47:09 +0000442 for (sect = s_first; sect <= s_last; ++sect) {
wdenk2cefd152004-02-08 22:55:38 +0000443 if (info->protect[sect]) {
444 prot++;
445 }
446 }
447 if (prot) {
wdenke65527f2004-02-12 00:47:09 +0000448 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk2cefd152004-02-08 22:55:38 +0000449 } else {
wdenk42c05472004-03-23 22:14:11 +0000450 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000451 }
452
453
wdenke65527f2004-02-12 00:47:09 +0000454 for (sect = s_first; sect <= s_last; sect++) {
wdenk2cefd152004-02-08 22:55:38 +0000455 if (info->protect[sect] == 0) { /* not protected */
wdenke65527f2004-02-12 00:47:09 +0000456 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000457 case CFI_CMDSET_INTEL_STANDARD:
458 case CFI_CMDSET_INTEL_EXTENDED:
wdenke537b3b2004-02-23 23:54:43 +0000459 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
460 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
461 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk2cefd152004-02-08 22:55:38 +0000462 break;
463 case CFI_CMDSET_AMD_STANDARD:
464 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000465 flash_unlock_seq (info, sect);
wdenked2ac4b2004-03-14 18:23:55 +0000466 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
467 AMD_CMD_ERASE_START);
wdenke65527f2004-02-12 00:47:09 +0000468 flash_unlock_seq (info, sect);
wdenke537b3b2004-02-23 23:54:43 +0000469 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk2cefd152004-02-08 22:55:38 +0000470 break;
471 default:
wdenke65527f2004-02-12 00:47:09 +0000472 debug ("Unkown flash vendor %d\n",
473 info->vendor);
wdenk2cefd152004-02-08 22:55:38 +0000474 break;
475 }
476
wdenke65527f2004-02-12 00:47:09 +0000477 if (flash_full_status_check
478 (info, sect, info->erase_blk_tout, "erase")) {
wdenk2cefd152004-02-08 22:55:38 +0000479 rcode = 1;
480 } else
wdenk42c05472004-03-23 22:14:11 +0000481 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +0000482 }
483 }
wdenk42c05472004-03-23 22:14:11 +0000484 puts (" done\n");
wdenk2cefd152004-02-08 22:55:38 +0000485 return rcode;
486}
487
488/*-----------------------------------------------------------------------
489 */
wdenke65527f2004-02-12 00:47:09 +0000490void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +0000491{
492 int i;
493
494 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000495 puts ("missing or unknown FLASH type\n");
wdenk2cefd152004-02-08 22:55:38 +0000496 return;
497 }
498
wdenke65527f2004-02-12 00:47:09 +0000499 printf ("CFI conformant FLASH (%d x %d)",
500 (info->portwidth << 3), (info->chipwidth << 3));
wdenk2cefd152004-02-08 22:55:38 +0000501 printf (" Size: %ld MB in %d Sectors\n",
502 info->size >> 20, info->sector_count);
wdenke537b3b2004-02-23 23:54:43 +0000503 printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
504 info->erase_blk_tout,
505 info->write_tout,
506 info->buffer_write_tout,
507 info->buffer_size);
wdenk2cefd152004-02-08 22:55:38 +0000508
wdenk42c05472004-03-23 22:14:11 +0000509 puts (" Sector Start Addresses:");
wdenke65527f2004-02-12 00:47:09 +0000510 for (i = 0; i < info->sector_count; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000511#ifdef CFG_FLASH_EMPTY_INFO
512 int k;
513 int size;
514 int erased;
515 volatile unsigned long *flash;
516
517 /*
518 * Check if whole sector is erased
519 */
wdenke65527f2004-02-12 00:47:09 +0000520 if (i != (info->sector_count - 1))
521 size = info->start[i + 1] - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000522 else
wdenke65527f2004-02-12 00:47:09 +0000523 size = info->start[0] + info->size - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000524 erased = 1;
wdenke65527f2004-02-12 00:47:09 +0000525 flash = (volatile unsigned long *) info->start[i];
526 size = size >> 2; /* divide by 4 for longword access */
527 for (k = 0; k < size; k++) {
528 if (*flash++ != 0xffffffff) {
529 erased = 0;
530 break;
531 }
532 }
wdenk2cefd152004-02-08 22:55:38 +0000533
534 if ((i % 5) == 0)
535 printf ("\n");
536 /* print empty and read-only info */
537 printf (" %08lX%s%s",
538 info->start[i],
539 erased ? " E" : " ",
540 info->protect[i] ? "RO " : " ");
Wolfgang Denkd3abe5a2005-09-25 00:23:05 +0200541#else /* ! CFG_FLASH_EMPTY_INFO */
wdenk2cefd152004-02-08 22:55:38 +0000542 if ((i % 5) == 0)
543 printf ("\n ");
544 printf (" %08lX%s",
Wolfgang Denkd3abe5a2005-09-25 00:23:05 +0200545 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenk2cefd152004-02-08 22:55:38 +0000546#endif
547 }
wdenk42c05472004-03-23 22:14:11 +0000548 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000549 return;
550}
551
552/*-----------------------------------------------------------------------
553 * Copy memory to flash, returns:
554 * 0 - OK
555 * 1 - write timeout
556 * 2 - Flash not erased
557 */
wdenke65527f2004-02-12 00:47:09 +0000558int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +0000559{
560 ulong wp;
561 ulong cp;
562 int aln;
563 cfiword_t cword;
564 int i, rc;
565
wdenke65527f2004-02-12 00:47:09 +0000566#ifdef CFG_FLASH_USE_BUFFER_WRITE
567 int buffered_size;
568#endif
wdenke65527f2004-02-12 00:47:09 +0000569 /* get lower aligned address */
wdenk2cefd152004-02-08 22:55:38 +0000570 /* get lower aligned address */
571 wp = (addr & ~(info->portwidth - 1));
572
573 /* handle unaligned start */
wdenke65527f2004-02-12 00:47:09 +0000574 if ((aln = addr - wp) != 0) {
wdenk2cefd152004-02-08 22:55:38 +0000575 cword.l = 0;
576 cp = wp;
wdenke65527f2004-02-12 00:47:09 +0000577 for (i = 0; i < aln; ++i, ++cp)
578 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000579
wdenke65527f2004-02-12 00:47:09 +0000580 for (; (i < info->portwidth) && (cnt > 0); i++) {
581 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000582 cnt--;
583 cp++;
584 }
wdenke65527f2004-02-12 00:47:09 +0000585 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
586 flash_add_byte (info, &cword, (*(uchar *) cp));
587 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000588 return rc;
589 wp = cp;
590 }
591
wdenke65527f2004-02-12 00:47:09 +0000592 /* handle the aligned part */
wdenk2cefd152004-02-08 22:55:38 +0000593#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke65527f2004-02-12 00:47:09 +0000594 buffered_size = (info->portwidth / info->chipwidth);
595 buffered_size *= info->buffer_size;
596 while (cnt >= info->portwidth) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100597 /* prohibit buffer write when buffer_size is 1 */
598 if (info->buffer_size == 1) {
599 cword.l = 0;
600 for (i = 0; i < info->portwidth; i++)
601 flash_add_byte (info, &cword, *src++);
602 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
603 return rc;
604 wp += info->portwidth;
605 cnt -= info->portwidth;
606 continue;
607 }
608
609 /* write buffer until next buffered_size aligned boundary */
610 i = buffered_size - (wp % buffered_size);
611 if (i > cnt)
612 i = cnt;
wdenke65527f2004-02-12 00:47:09 +0000613 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk2cefd152004-02-08 22:55:38 +0000614 return rc;
Wolfgang Denke25f0522005-08-12 22:35:59 +0200615 i -= i & (info->portwidth - 1);
wdenk2cefd152004-02-08 22:55:38 +0000616 wp += i;
617 src += i;
wdenke65527f2004-02-12 00:47:09 +0000618 cnt -= i;
wdenk2cefd152004-02-08 22:55:38 +0000619 }
620#else
wdenke65527f2004-02-12 00:47:09 +0000621 while (cnt >= info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000622 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000623 for (i = 0; i < info->portwidth; i++) {
624 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000625 }
wdenke65527f2004-02-12 00:47:09 +0000626 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000627 return rc;
628 wp += info->portwidth;
629 cnt -= info->portwidth;
630 }
631#endif /* CFG_FLASH_USE_BUFFER_WRITE */
632 if (cnt == 0) {
633 return (0);
634 }
635
636 /*
637 * handle unaligned tail bytes
638 */
639 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000640 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
641 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000642 --cnt;
643 }
wdenke65527f2004-02-12 00:47:09 +0000644 for (; i < info->portwidth; ++i, ++cp) {
645 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000646 }
647
wdenke65527f2004-02-12 00:47:09 +0000648 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +0000649}
650
651/*-----------------------------------------------------------------------
652 */
653#ifdef CFG_FLASH_PROTECTION
654
wdenke65527f2004-02-12 00:47:09 +0000655int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +0000656{
657 int retcode = 0;
658
wdenke65527f2004-02-12 00:47:09 +0000659 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
660 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
661 if (prot)
662 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk2cefd152004-02-08 22:55:38 +0000663 else
wdenke65527f2004-02-12 00:47:09 +0000664 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk2cefd152004-02-08 22:55:38 +0000665
wdenke65527f2004-02-12 00:47:09 +0000666 if ((retcode =
667 flash_full_status_check (info, sector, info->erase_blk_tout,
668 prot ? "protect" : "unprotect")) == 0) {
wdenk2cefd152004-02-08 22:55:38 +0000669
670 info->protect[sector] = prot;
671 /* Intel's unprotect unprotects all locking */
wdenke65527f2004-02-12 00:47:09 +0000672 if (prot == 0) {
wdenk2cefd152004-02-08 22:55:38 +0000673 flash_sect_t i;
wdenke65527f2004-02-12 00:47:09 +0000674
675 for (i = 0; i < info->sector_count; i++) {
676 if (info->protect[i])
677 flash_real_protect (info, i, 1);
wdenk2cefd152004-02-08 22:55:38 +0000678 }
679 }
680 }
wdenk2cefd152004-02-08 22:55:38 +0000681 return retcode;
wdenke65527f2004-02-12 00:47:09 +0000682}
683
wdenk2cefd152004-02-08 22:55:38 +0000684/*-----------------------------------------------------------------------
685 * flash_read_user_serial - read the OneTimeProgramming cells
686 */
wdenke65527f2004-02-12 00:47:09 +0000687void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
688 int len)
wdenk2cefd152004-02-08 22:55:38 +0000689{
wdenke65527f2004-02-12 00:47:09 +0000690 uchar *src;
691 uchar *dst;
wdenk2cefd152004-02-08 22:55:38 +0000692
693 dst = buffer;
wdenke65527f2004-02-12 00:47:09 +0000694 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
695 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
696 memcpy (dst, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200697 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000698}
wdenke65527f2004-02-12 00:47:09 +0000699
wdenk2cefd152004-02-08 22:55:38 +0000700/*
701 * flash_read_factory_serial - read the device Id from the protection area
702 */
wdenke65527f2004-02-12 00:47:09 +0000703void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
704 int len)
wdenk2cefd152004-02-08 22:55:38 +0000705{
wdenke65527f2004-02-12 00:47:09 +0000706 uchar *src;
wdenk6cfa84e2004-02-10 00:03:41 +0000707
wdenke65527f2004-02-12 00:47:09 +0000708 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
709 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
710 memcpy (buffer, src + offset, len);
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200711 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000712}
713
714#endif /* CFG_FLASH_PROTECTION */
715
wdenke65527f2004-02-12 00:47:09 +0000716/*
717 * flash_is_busy - check to see if the flash is busy
718 * This routine checks the status of the chip and returns true if the chip is busy
719 */
720static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000721{
722 int retval;
wdenke65527f2004-02-12 00:47:09 +0000723
724 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000725 case CFI_CMDSET_INTEL_STANDARD:
726 case CFI_CMDSET_INTEL_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000727 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk2cefd152004-02-08 22:55:38 +0000728 break;
729 case CFI_CMDSET_AMD_STANDARD:
730 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000731 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk2cefd152004-02-08 22:55:38 +0000732 break;
733 default:
734 retval = 0;
735 }
wdenke65527f2004-02-12 00:47:09 +0000736 debug ("flash_is_busy: %d\n", retval);
wdenk2cefd152004-02-08 22:55:38 +0000737 return retval;
738}
wdenke65527f2004-02-12 00:47:09 +0000739
wdenk2cefd152004-02-08 22:55:38 +0000740/*-----------------------------------------------------------------------
741 * wait for XSR.7 to be set. Time out with an error if it does not.
742 * This routine does not set the flash to read-array mode.
743 */
wdenke65527f2004-02-12 00:47:09 +0000744static int flash_status_check (flash_info_t * info, flash_sect_t sector,
745 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000746{
747 ulong start;
748
749 /* Wait for command completion */
750 start = get_timer (0);
wdenke65527f2004-02-12 00:47:09 +0000751 while (flash_is_busy (info, sector)) {
Stefan Roesec865e6c2006-02-28 15:29:58 +0100752 if (get_timer (start) > tout) {
wdenke65527f2004-02-12 00:47:09 +0000753 printf ("Flash %s timeout at address %lx data %lx\n",
754 prompt, info->start[sector],
755 flash_read_long (info, sector, 0));
756 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000757 return ERR_TIMOUT;
758 }
759 }
760 return ERR_OK;
761}
wdenke65527f2004-02-12 00:47:09 +0000762
wdenk2cefd152004-02-08 22:55:38 +0000763/*-----------------------------------------------------------------------
764 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
765 * This routine sets the flash to read-array mode.
766 */
wdenke65527f2004-02-12 00:47:09 +0000767static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
768 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000769{
770 int retcode;
wdenke65527f2004-02-12 00:47:09 +0000771
772 retcode = flash_status_check (info, sector, tout, prompt);
773 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000774 case CFI_CMDSET_INTEL_EXTENDED:
775 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesec865e6c2006-02-28 15:29:58 +0100776 if ((retcode == ERR_OK)
wdenke65527f2004-02-12 00:47:09 +0000777 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk2cefd152004-02-08 22:55:38 +0000778 retcode = ERR_INVAL;
wdenke65527f2004-02-12 00:47:09 +0000779 printf ("Flash %s error at address %lx\n", prompt,
780 info->start[sector]);
wdenke537b3b2004-02-23 23:54:43 +0000781 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000782 puts ("Command Sequence Error.\n");
wdenke537b3b2004-02-23 23:54:43 +0000783 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000784 puts ("Block Erase Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000785 retcode = ERR_NOT_ERASED;
wdenke537b3b2004-02-23 23:54:43 +0000786 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000787 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000788 }
wdenke65527f2004-02-12 00:47:09 +0000789 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk42c05472004-03-23 22:14:11 +0000790 puts ("Block locked.\n");
wdenke65527f2004-02-12 00:47:09 +0000791 retcode = ERR_PROTECTED;
792 }
793 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk42c05472004-03-23 22:14:11 +0000794 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000795 }
Wolfgang Denka205a8f2005-09-25 16:41:22 +0200796 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000797 break;
798 default:
799 break;
800 }
801 return retcode;
802}
wdenke65527f2004-02-12 00:47:09 +0000803
wdenk2cefd152004-02-08 22:55:38 +0000804/*-----------------------------------------------------------------------
805 */
wdenke65527f2004-02-12 00:47:09 +0000806static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000807{
wdenk369d43d2004-03-14 14:09:05 +0000808#if defined(__LITTLE_ENDIAN)
809 unsigned short w;
810 unsigned int l;
811 unsigned long long ll;
812#endif
813
wdenke65527f2004-02-12 00:47:09 +0000814 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000815 case FLASH_CFI_8BIT:
816 cword->c = c;
817 break;
818 case FLASH_CFI_16BIT:
wdenk369d43d2004-03-14 14:09:05 +0000819#if defined(__LITTLE_ENDIAN)
820 w = c;
821 w <<= 8;
822 cword->w = (cword->w >> 8) | w;
823#else
wdenk2cefd152004-02-08 22:55:38 +0000824 cword->w = (cword->w << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000825#endif
wdenk2cefd152004-02-08 22:55:38 +0000826 break;
827 case FLASH_CFI_32BIT:
wdenk369d43d2004-03-14 14:09:05 +0000828#if defined(__LITTLE_ENDIAN)
829 l = c;
830 l <<= 24;
831 cword->l = (cword->l >> 8) | l;
832#else
wdenk2cefd152004-02-08 22:55:38 +0000833 cword->l = (cword->l << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000834#endif
wdenk2cefd152004-02-08 22:55:38 +0000835 break;
836 case FLASH_CFI_64BIT:
wdenk369d43d2004-03-14 14:09:05 +0000837#if defined(__LITTLE_ENDIAN)
838 ll = c;
839 ll <<= 56;
840 cword->ll = (cword->ll >> 8) | ll;
841#else
wdenk2cefd152004-02-08 22:55:38 +0000842 cword->ll = (cword->ll << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000843#endif
wdenk2cefd152004-02-08 22:55:38 +0000844 break;
845 }
846}
847
848
849/*-----------------------------------------------------------------------
850 * make a proper sized command based on the port and chip widths
851 */
wdenke65527f2004-02-12 00:47:09 +0000852static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk2cefd152004-02-08 22:55:38 +0000853{
854 int i;
wdenke65527f2004-02-12 00:47:09 +0000855 uchar *cp = (uchar *) cmdbuf;
856
wdenke65527f2004-02-12 00:47:09 +0000857#if defined(__LITTLE_ENDIAN)
Wolfgang Denk07bdb022005-09-24 23:32:48 +0200858 for (i = info->portwidth; i > 0; i--)
859#else
860 for (i = 1; i <= info->portwidth; i++)
wdenke65527f2004-02-12 00:47:09 +0000861#endif
Wolfgang Denk23f0d172005-10-09 00:25:58 +0200862 *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
wdenk2cefd152004-02-08 22:55:38 +0000863}
864
865/*
866 * Write a proper sized command to the correct address
867 */
wdenke537b3b2004-02-23 23:54:43 +0000868static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000869{
870
871 volatile cfiptr_t addr;
872 cfiword_t cword;
wdenke65527f2004-02-12 00:47:09 +0000873
874 addr.cp = flash_make_addr (info, sect, offset);
875 flash_make_cmd (info, cmd, &cword);
876 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000877 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000878 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
879 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk2cefd152004-02-08 22:55:38 +0000880 *addr.cp = cword.c;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100881#ifdef CONFIG_BLACKFIN
882 asm("ssync;");
883#endif
wdenk2cefd152004-02-08 22:55:38 +0000884 break;
885 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000886 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
887 cmd, cword.w,
wdenk2cefd152004-02-08 22:55:38 +0000888 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
889 *addr.wp = cword.w;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100890#ifdef CONFIG_BLACKFIN
891 asm("ssync;");
892#endif
wdenk2cefd152004-02-08 22:55:38 +0000893 break;
894 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000895 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
896 cmd, cword.l,
wdenk2cefd152004-02-08 22:55:38 +0000897 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
898 *addr.lp = cword.l;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100899#ifdef CONFIG_BLACKFIN
900 asm("ssync;");
901#endif
wdenk2cefd152004-02-08 22:55:38 +0000902 break;
903 case FLASH_CFI_64BIT:
904#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000905 {
wdenk2cefd152004-02-08 22:55:38 +0000906 char str[20];
wdenk6cfa84e2004-02-10 00:03:41 +0000907
wdenke65527f2004-02-12 00:47:09 +0000908 print_longlong (str, cword.ll);
909
910 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
911 addr.llp, cmd, str,
wdenk2cefd152004-02-08 22:55:38 +0000912 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
913 }
914#endif
915 *addr.llp = cword.ll;
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100916#ifdef CONFIG_BLACKFIN
917 asm("ssync;");
918#endif
wdenk2cefd152004-02-08 22:55:38 +0000919 break;
920 }
921}
922
wdenke65527f2004-02-12 00:47:09 +0000923static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000924{
wdenked2ac4b2004-03-14 18:23:55 +0000925 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
926 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
wdenk2cefd152004-02-08 22:55:38 +0000927}
wdenke65527f2004-02-12 00:47:09 +0000928
wdenk2cefd152004-02-08 22:55:38 +0000929/*-----------------------------------------------------------------------
930 */
wdenke537b3b2004-02-23 23:54:43 +0000931static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000932{
933 cfiptr_t cptr;
934 cfiword_t cword;
935 int retval;
wdenke65527f2004-02-12 00:47:09 +0000936
937 cptr.cp = flash_make_addr (info, sect, offset);
938 flash_make_cmd (info, cmd, &cword);
wdenk2cefd152004-02-08 22:55:38 +0000939
wdenke65527f2004-02-12 00:47:09 +0000940 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
941 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000942 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000943 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk2cefd152004-02-08 22:55:38 +0000944 retval = (cptr.cp[0] == cword.c);
945 break;
946 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000947 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk2cefd152004-02-08 22:55:38 +0000948 retval = (cptr.wp[0] == cword.w);
949 break;
950 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000951 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk2cefd152004-02-08 22:55:38 +0000952 retval = (cptr.lp[0] == cword.l);
953 break;
954 case FLASH_CFI_64BIT:
wdenk6cfa84e2004-02-10 00:03:41 +0000955#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000956 {
wdenk2cefd152004-02-08 22:55:38 +0000957 char str1[20];
958 char str2[20];
wdenke65527f2004-02-12 00:47:09 +0000959
960 print_longlong (str1, cptr.llp[0]);
961 print_longlong (str2, cword.ll);
962 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +0000963 }
964#endif
965 retval = (cptr.llp[0] == cword.ll);
966 break;
967 default:
968 retval = 0;
969 break;
970 }
971 return retval;
972}
wdenke65527f2004-02-12 00:47:09 +0000973
wdenk2cefd152004-02-08 22:55:38 +0000974/*-----------------------------------------------------------------------
975 */
wdenke537b3b2004-02-23 23:54:43 +0000976static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000977{
978 cfiptr_t cptr;
979 cfiword_t cword;
980 int retval;
wdenke65527f2004-02-12 00:47:09 +0000981
982 cptr.cp = flash_make_addr (info, sect, offset);
983 flash_make_cmd (info, cmd, &cword);
984 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000985 case FLASH_CFI_8BIT:
986 retval = ((cptr.cp[0] & cword.c) == cword.c);
987 break;
988 case FLASH_CFI_16BIT:
989 retval = ((cptr.wp[0] & cword.w) == cword.w);
990 break;
991 case FLASH_CFI_32BIT:
992 retval = ((cptr.lp[0] & cword.l) == cword.l);
993 break;
994 case FLASH_CFI_64BIT:
995 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenke65527f2004-02-12 00:47:09 +0000996 break;
wdenk2cefd152004-02-08 22:55:38 +0000997 default:
998 retval = 0;
999 break;
1000 }
1001 return retval;
1002}
1003
1004/*-----------------------------------------------------------------------
1005 */
wdenke537b3b2004-02-23 23:54:43 +00001006static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +00001007{
1008 cfiptr_t cptr;
1009 cfiword_t cword;
1010 int retval;
wdenke65527f2004-02-12 00:47:09 +00001011
1012 cptr.cp = flash_make_addr (info, sect, offset);
1013 flash_make_cmd (info, cmd, &cword);
1014 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001015 case FLASH_CFI_8BIT:
1016 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
1017 break;
1018 case FLASH_CFI_16BIT:
1019 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
1020 break;
1021 case FLASH_CFI_32BIT:
1022 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
1023 break;
1024 case FLASH_CFI_64BIT:
wdenke65527f2004-02-12 00:47:09 +00001025 retval = ((cptr.llp[0] & cword.ll) !=
1026 (cptr.llp[0] & cword.ll));
wdenk2cefd152004-02-08 22:55:38 +00001027 break;
1028 default:
1029 retval = 0;
1030 break;
1031 }
1032 return retval;
1033}
1034
1035/*-----------------------------------------------------------------------
1036 * detect if flash is compatible with the Common Flash Interface (CFI)
1037 * http://www.jedec.org/download/search/jesd68.pdf
1038 *
1039*/
wdenke65527f2004-02-12 00:47:09 +00001040static int flash_detect_cfi (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001041{
wdenke65527f2004-02-12 00:47:09 +00001042 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001043
Stefan Roesec865e6c2006-02-28 15:29:58 +01001044 for (info->portwidth = CFG_FLASH_CFI_WIDTH;
wdenke65527f2004-02-12 00:47:09 +00001045 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1046 for (info->chipwidth = FLASH_CFI_BY8;
1047 info->chipwidth <= info->portwidth;
1048 info->chipwidth <<= 1) {
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001049 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenke537b3b2004-02-23 23:54:43 +00001050 flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
1051 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1052 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1053 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1054 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
wdenke65527f2004-02-12 00:47:09 +00001055 debug ("device interface is %d\n",
1056 info->interface);
1057 debug ("found port %d chip %d ",
1058 info->portwidth, info->chipwidth);
1059 debug ("port %d bits chip %d bits\n",
wdenke537b3b2004-02-23 23:54:43 +00001060 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1061 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk2cefd152004-02-08 22:55:38 +00001062 return 1;
1063 }
1064 }
1065 }
wdenke65527f2004-02-12 00:47:09 +00001066 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001067 return 0;
1068}
wdenke65527f2004-02-12 00:47:09 +00001069
wdenk2cefd152004-02-08 22:55:38 +00001070/*
1071 * The following code cannot be run from FLASH!
1072 *
1073 */
Marian Balakowicz513b4a12005-10-11 19:09:42 +02001074ulong flash_get_size (ulong base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001075{
wdenke65527f2004-02-12 00:47:09 +00001076 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00001077 int i, j;
1078 flash_sect_t sect_cnt;
1079 unsigned long sector;
1080 unsigned long tmp;
1081 int size_ratio;
1082 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00001083 int erase_region_size;
1084 int erase_region_count;
wdenk2cefd152004-02-08 22:55:38 +00001085
1086 info->start[0] = base;
1087
wdenke65527f2004-02-12 00:47:09 +00001088 if (flash_detect_cfi (info)) {
wdenke537b3b2004-02-23 23:54:43 +00001089 info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
wdenke65527f2004-02-12 00:47:09 +00001090#ifdef DEBUG
1091 flash_printqry (info, 0);
1092#endif
1093 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001094 case CFI_CMDSET_INTEL_STANDARD:
1095 case CFI_CMDSET_INTEL_EXTENDED:
1096 default:
1097 info->cmd_reset = FLASH_CMD_RESET;
1098 break;
1099 case CFI_CMDSET_AMD_STANDARD:
1100 case CFI_CMDSET_AMD_EXTENDED:
1101 info->cmd_reset = AMD_CMD_RESET;
1102 break;
1103 }
wdenk6cfa84e2004-02-10 00:03:41 +00001104
wdenke65527f2004-02-12 00:47:09 +00001105 debug ("manufacturer is %d\n", info->vendor);
wdenk2cefd152004-02-08 22:55:38 +00001106 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00001107 /* if the chip is x8/x16 reduce the ratio by half */
1108 if ((info->interface == FLASH_CFI_X8X16)
1109 && (info->chipwidth == FLASH_CFI_BY8)) {
1110 size_ratio >>= 1;
1111 }
wdenke537b3b2004-02-23 23:54:43 +00001112 num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
wdenke65527f2004-02-12 00:47:09 +00001113 debug ("size_ratio %d port %d bits chip %d bits\n",
1114 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1115 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1116 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00001117 sect_cnt = 0;
1118 sector = base;
wdenke65527f2004-02-12 00:47:09 +00001119 for (i = 0; i < num_erase_regions; i++) {
1120 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00001121 printf ("%d erase regions found, only %d used\n",
1122 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00001123 break;
1124 }
wdenke65527f2004-02-12 00:47:09 +00001125 tmp = flash_read_long (info, 0,
1126 FLASH_OFFSET_ERASE_REGIONS +
1127 i * 4);
1128 erase_region_size =
1129 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk2cefd152004-02-08 22:55:38 +00001130 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00001131 erase_region_count = (tmp & 0xffff) + 1;
wdenkaeba06f2004-06-09 17:34:58 +00001132 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00001133 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00001134 for (j = 0; j < erase_region_count; j++) {
wdenk2cefd152004-02-08 22:55:38 +00001135 info->start[sect_cnt] = sector;
1136 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00001137
1138 /*
1139 * Only read protection status from supported devices (intel...)
1140 */
1141 switch (info->vendor) {
1142 case CFI_CMDSET_INTEL_EXTENDED:
1143 case CFI_CMDSET_INTEL_STANDARD:
1144 info->protect[sect_cnt] =
1145 flash_isset (info, sect_cnt,
1146 FLASH_OFFSET_PROTECT,
1147 FLASH_STATUS_PROTECT);
1148 break;
1149 default:
1150 info->protect[sect_cnt] = 0; /* default: not protected */
1151 }
1152
wdenk2cefd152004-02-08 22:55:38 +00001153 sect_cnt++;
1154 }
1155 }
1156
1157 info->sector_count = sect_cnt;
1158 /* multiply the size by the number of chips */
wdenke537b3b2004-02-23 23:54:43 +00001159 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1160 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
wdenke65527f2004-02-12 00:47:09 +00001161 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenke537b3b2004-02-23 23:54:43 +00001162 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
wdenke65527f2004-02-12 00:47:09 +00001163 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
wdenke537b3b2004-02-23 23:54:43 +00001164 info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
Stefan Roesec865e6c2006-02-28 15:29:58 +01001165 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
1166 (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
1167 info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
wdenk2cefd152004-02-08 22:55:38 +00001168 info->flash_id = FLASH_MAN_CFI;
wdenked2ac4b2004-03-14 18:23:55 +00001169 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1170 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1171 }
wdenk2cefd152004-02-08 22:55:38 +00001172 }
1173
Wolfgang Denka205a8f2005-09-25 16:41:22 +02001174 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenke65527f2004-02-12 00:47:09 +00001175 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00001176}
1177
Stefan Roesec865e6c2006-02-28 15:29:58 +01001178/* loop through the sectors from the highest address
1179 * when the passed address is greater or equal to the sector address
1180 * we have a match
1181 */
1182static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1183{
1184 flash_sect_t sector;
1185
1186 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1187 if (addr >= info->start[sector])
1188 break;
1189 }
1190 return sector;
1191}
wdenk2cefd152004-02-08 22:55:38 +00001192
1193/*-----------------------------------------------------------------------
1194 */
wdenke65527f2004-02-12 00:47:09 +00001195static int flash_write_cfiword (flash_info_t * info, ulong dest,
1196 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +00001197{
wdenk2cefd152004-02-08 22:55:38 +00001198 cfiptr_t ctladdr;
1199 cfiptr_t cptr;
1200 int flag;
1201
wdenke65527f2004-02-12 00:47:09 +00001202 ctladdr.cp = flash_make_addr (info, 0, 0);
1203 cptr.cp = (uchar *) dest;
wdenk2cefd152004-02-08 22:55:38 +00001204
1205
1206 /* Check if Flash is (sufficiently) erased */
wdenke65527f2004-02-12 00:47:09 +00001207 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001208 case FLASH_CFI_8BIT:
1209 flag = ((cptr.cp[0] & cword.c) == cword.c);
1210 break;
1211 case FLASH_CFI_16BIT:
1212 flag = ((cptr.wp[0] & cword.w) == cword.w);
1213 break;
1214 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +00001215 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk2cefd152004-02-08 22:55:38 +00001216 break;
1217 case FLASH_CFI_64BIT:
wdenk391b5742004-10-10 23:27:33 +00001218 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenk2cefd152004-02-08 22:55:38 +00001219 break;
1220 default:
1221 return 2;
1222 }
wdenke65527f2004-02-12 00:47:09 +00001223 if (!flag)
wdenk2cefd152004-02-08 22:55:38 +00001224 return 2;
1225
1226 /* Disable interrupts which might cause a timeout here */
wdenke65527f2004-02-12 00:47:09 +00001227 flag = disable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001228
wdenke65527f2004-02-12 00:47:09 +00001229 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001230 case CFI_CMDSET_INTEL_EXTENDED:
1231 case CFI_CMDSET_INTEL_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001232 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1233 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001234 break;
1235 case CFI_CMDSET_AMD_EXTENDED:
1236 case CFI_CMDSET_AMD_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001237 flash_unlock_seq (info, 0);
wdenked2ac4b2004-03-14 18:23:55 +00001238 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001239 break;
1240 }
1241
wdenke65527f2004-02-12 00:47:09 +00001242 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001243 case FLASH_CFI_8BIT:
1244 cptr.cp[0] = cword.c;
1245 break;
1246 case FLASH_CFI_16BIT:
1247 cptr.wp[0] = cword.w;
1248 break;
1249 case FLASH_CFI_32BIT:
1250 cptr.lp[0] = cword.l;
1251 break;
1252 case FLASH_CFI_64BIT:
1253 cptr.llp[0] = cword.ll;
1254 break;
1255 }
1256
1257 /* re-enable interrupts if necessary */
wdenke65527f2004-02-12 00:47:09 +00001258 if (flag)
1259 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001260
Stefan Roesec865e6c2006-02-28 15:29:58 +01001261 return flash_full_status_check (info, find_sector (info, dest),
1262 info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +00001263}
1264
1265#ifdef CFG_FLASH_USE_BUFFER_WRITE
1266
wdenke65527f2004-02-12 00:47:09 +00001267static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1268 int len)
wdenk2cefd152004-02-08 22:55:38 +00001269{
1270 flash_sect_t sector;
1271 int cnt;
1272 int retcode;
1273 volatile cfiptr_t src;
1274 volatile cfiptr_t dst;
1275
Stefan Roesec865e6c2006-02-28 15:29:58 +01001276 switch (info->vendor) {
1277 case CFI_CMDSET_INTEL_STANDARD:
1278 case CFI_CMDSET_INTEL_EXTENDED:
1279 src.cp = cp;
1280 dst.cp = (uchar *) dest;
1281 sector = find_sector (info, dest);
1282 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1283 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1284 if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
1285 "write to buffer")) == ERR_OK) {
1286 /* reduce the number of loops by the width of the port */
1287 switch (info->portwidth) {
1288 case FLASH_CFI_8BIT:
1289 cnt = len;
1290 break;
1291 case FLASH_CFI_16BIT:
1292 cnt = len >> 1;
1293 break;
1294 case FLASH_CFI_32BIT:
1295 cnt = len >> 2;
1296 break;
1297 case FLASH_CFI_64BIT:
1298 cnt = len >> 3;
1299 break;
1300 default:
1301 return ERR_INVAL;
1302 break;
1303 }
1304 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1305 while (cnt-- > 0) {
1306 switch (info->portwidth) {
1307 case FLASH_CFI_8BIT:
1308 *dst.cp++ = *src.cp++;
1309 break;
1310 case FLASH_CFI_16BIT:
1311 *dst.wp++ = *src.wp++;
1312 break;
1313 case FLASH_CFI_32BIT:
1314 *dst.lp++ = *src.lp++;
1315 break;
1316 case FLASH_CFI_64BIT:
1317 *dst.llp++ = *src.llp++;
1318 break;
1319 default:
1320 return ERR_INVAL;
1321 break;
1322 }
1323 }
1324 flash_write_cmd (info, sector, 0,
1325 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1326 retcode = flash_full_status_check (info, sector,
1327 info->buffer_write_tout,
1328 "buffer write");
1329 }
1330 return retcode;
1331
1332 case CFI_CMDSET_AMD_STANDARD:
1333 case CFI_CMDSET_AMD_EXTENDED:
1334 src.cp = cp;
1335 dst.cp = (uchar *) dest;
1336 sector = find_sector (info, dest);
1337
1338 flash_unlock_seq(info,0);
1339 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
1340
wdenke65527f2004-02-12 00:47:09 +00001341 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001342 case FLASH_CFI_8BIT:
1343 cnt = len;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001344 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1345 while (cnt-- > 0) *dst.cp++ = *src.cp++;
wdenk2cefd152004-02-08 22:55:38 +00001346 break;
1347 case FLASH_CFI_16BIT:
1348 cnt = len >> 1;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001349 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1350 while (cnt-- > 0) *dst.wp++ = *src.wp++;
wdenk2cefd152004-02-08 22:55:38 +00001351 break;
1352 case FLASH_CFI_32BIT:
1353 cnt = len >> 2;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001354 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1355 while (cnt-- > 0) *dst.lp++ = *src.lp++;
wdenk2cefd152004-02-08 22:55:38 +00001356 break;
1357 case FLASH_CFI_64BIT:
1358 cnt = len >> 3;
Stefan Roesec865e6c2006-02-28 15:29:58 +01001359 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1360 while (cnt-- > 0) *dst.llp++ = *src.llp++;
wdenk2cefd152004-02-08 22:55:38 +00001361 break;
1362 default:
1363 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001364 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01001365
1366 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1367 retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
1368 "buffer write");
1369 return retcode;
1370
1371 default:
1372 debug ("Unknown Command Set\n");
1373 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001374 }
wdenk2cefd152004-02-08 22:55:38 +00001375}
wdenk5b835a32004-09-28 19:00:19 +00001376#endif /* CFG_FLASH_USE_BUFFER_WRITE */
wdenk2cefd152004-02-08 22:55:38 +00001377#endif /* CFG_FLASH_CFI */