blob: b79cf59344c8d2935a252064f6d17042d4e471c2 [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>
wdenke537b3b2004-02-23 23:54:43 +000050#include <linux/byteorder/swab.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
wdenk2cefd152004-02-08 22:55:38 +0000107
108#define AMD_STATUS_TOGGLE 0x40
109#define AMD_STATUS_ERROR 0x20
wdenked2ac4b2004-03-14 18:23:55 +0000110#define AMD_ADDR_ERASE_START 0x555
111#define AMD_ADDR_START 0x555
112#define AMD_ADDR_ACK 0x2AA
wdenk2cefd152004-02-08 22:55:38 +0000113
114#define FLASH_OFFSET_CFI 0x55
115#define FLASH_OFFSET_CFI_RESP 0x10
wdenke65527f2004-02-12 00:47:09 +0000116#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
wdenk2cefd152004-02-08 22:55:38 +0000117#define FLASH_OFFSET_WTOUT 0x1F
wdenke65527f2004-02-12 00:47:09 +0000118#define FLASH_OFFSET_WBTOUT 0x20
wdenk2cefd152004-02-08 22:55:38 +0000119#define FLASH_OFFSET_ETOUT 0x21
wdenke65527f2004-02-12 00:47:09 +0000120#define FLASH_OFFSET_CETOUT 0x22
wdenk2cefd152004-02-08 22:55:38 +0000121#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenke65527f2004-02-12 00:47:09 +0000122#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk2cefd152004-02-08 22:55:38 +0000123#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenke65527f2004-02-12 00:47:09 +0000124#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk2cefd152004-02-08 22:55:38 +0000125#define FLASH_OFFSET_SIZE 0x27
wdenke65527f2004-02-12 00:47:09 +0000126#define FLASH_OFFSET_INTERFACE 0x28
127#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk2cefd152004-02-08 22:55:38 +0000128#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
129#define FLASH_OFFSET_ERASE_REGIONS 0x2D
130#define FLASH_OFFSET_PROTECT 0x02
wdenke65527f2004-02-12 00:47:09 +0000131#define FLASH_OFFSET_USER_PROTECTION 0x85
132#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk2cefd152004-02-08 22:55:38 +0000133
134
135#define FLASH_MAN_CFI 0x01000000
136
wdenke65527f2004-02-12 00:47:09 +0000137#define CFI_CMDSET_NONE 0
wdenk2cefd152004-02-08 22:55:38 +0000138#define CFI_CMDSET_INTEL_EXTENDED 1
wdenke65527f2004-02-12 00:47:09 +0000139#define CFI_CMDSET_AMD_STANDARD 2
wdenk2cefd152004-02-08 22:55:38 +0000140#define CFI_CMDSET_INTEL_STANDARD 3
wdenke65527f2004-02-12 00:47:09 +0000141#define CFI_CMDSET_AMD_EXTENDED 4
wdenk2cefd152004-02-08 22:55:38 +0000142#define CFI_CMDSET_MITSU_STANDARD 256
143#define CFI_CMDSET_MITSU_EXTENDED 257
wdenke65527f2004-02-12 00:47:09 +0000144#define CFI_CMDSET_SST 258
wdenk2cefd152004-02-08 22:55:38 +0000145
146
147typedef union {
148 unsigned char c;
149 unsigned short w;
150 unsigned long l;
151 unsigned long long ll;
152} cfiword_t;
153
154typedef union {
wdenke65527f2004-02-12 00:47:09 +0000155 volatile unsigned char *cp;
wdenk2cefd152004-02-08 22:55:38 +0000156 volatile unsigned short *wp;
wdenke65527f2004-02-12 00:47:09 +0000157 volatile unsigned long *lp;
wdenk2cefd152004-02-08 22:55:38 +0000158 volatile unsigned long long *llp;
159} cfiptr_t;
160
161#define NUM_ERASE_REGIONS 4
162
163static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
164
wdenke65527f2004-02-12 00:47:09 +0000165flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk2cefd152004-02-08 22:55:38 +0000166
167/*-----------------------------------------------------------------------
168 * Functions
169 */
170
171typedef unsigned long flash_sect_t;
172
wdenke65527f2004-02-12 00:47:09 +0000173static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
174static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenke537b3b2004-02-23 23:54:43 +0000175static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000176static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenke537b3b2004-02-23 23:54:43 +0000177static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
178static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
179static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenke65527f2004-02-12 00:47:09 +0000180static int flash_detect_cfi (flash_info_t * info);
wdenk2cefd152004-02-08 22:55:38 +0000181static ulong flash_get_size (ulong base, int banknum);
wdenke537b3b2004-02-23 23:54:43 +0000182static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenke65527f2004-02-12 00:47:09 +0000183static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
184 ulong tout, char *prompt);
wdenk2cefd152004-02-08 22:55:38 +0000185#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke537b3b2004-02-23 23:54:43 +0000186static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk2cefd152004-02-08 22:55:38 +0000187#endif
188
wdenke65527f2004-02-12 00:47:09 +0000189/*-----------------------------------------------------------------------
190 * create an address based on the offset and the port width
191 */
wdenke537b3b2004-02-23 23:54:43 +0000192inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000193{
194 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
195}
196
wdenk2cefd152004-02-08 22:55:38 +0000197#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000198/*-----------------------------------------------------------------------
199 * Debug support
200 */
201void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000202{
203 int i;
204 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000205
206 cp = (unsigned char *) &data;
207 for (i = 0; i < 8; i++)
208 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000209}
wdenke65527f2004-02-12 00:47:09 +0000210static void flash_printqry (flash_info_t * info, flash_sect_t sect)
211{
212 cfiptr_t cptr;
213 int x, y;
214
215 for (x = 0; x < 0x40; x += 16 / info->portwidth) {
216 cptr.cp =
217 flash_make_addr (info, sect,
218 x + FLASH_OFFSET_CFI_RESP);
219 debug ("%p : ", cptr.cp);
220 for (y = 0; y < 16; y++) {
221 debug ("%2.2x ", cptr.cp[y]);
222 }
223 debug (" ");
224 for (y = 0; y < 16; y++) {
225 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
226 debug ("%c", cptr.cp[y]);
227 } else {
228 debug (".");
229 }
230 }
231 debug ("\n");
232 }
233}
wdenk2cefd152004-02-08 22:55:38 +0000234#endif
235
236
237/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000238 * read a character at a port width address
239 */
wdenke65527f2004-02-12 00:47:09 +0000240inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000241{
242 uchar *cp;
wdenke65527f2004-02-12 00:47:09 +0000243
244 cp = flash_make_addr (info, 0, offset);
245#if defined(__LITTLE_ENDIAN)
246 return (cp[0]);
247#else
wdenk2cefd152004-02-08 22:55:38 +0000248 return (cp[info->portwidth - 1]);
wdenke65527f2004-02-12 00:47:09 +0000249#endif
wdenk2cefd152004-02-08 22:55:38 +0000250}
251
252/*-----------------------------------------------------------------------
253 * read a short word by swapping for ppc format.
254 */
wdenke65527f2004-02-12 00:47:09 +0000255ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000256{
wdenke65527f2004-02-12 00:47:09 +0000257 uchar *addr;
258 ushort retval;
wdenk2cefd152004-02-08 22:55:38 +0000259
wdenke65527f2004-02-12 00:47:09 +0000260#ifdef DEBUG
261 int x;
262#endif
263 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000264
wdenke65527f2004-02-12 00:47:09 +0000265#ifdef DEBUG
266 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
267 info->portwidth);
268 for (x = 0; x < 2 * info->portwidth; x++) {
269 debug ("addr[%x] = 0x%x\n", x, addr[x]);
270 }
271#endif
272#if defined(__LITTLE_ENDIAN)
273 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
274#else
275 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
276 addr[info->portwidth - 1]);
277#endif
278
279 debug ("retval = 0x%x\n", retval);
280 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000281}
282
283/*-----------------------------------------------------------------------
284 * read a long word by picking the least significant byte of each maiximum
285 * port size word. Swap for ppc format.
286 */
wdenke65527f2004-02-12 00:47:09 +0000287ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000288{
wdenke65527f2004-02-12 00:47:09 +0000289 uchar *addr;
290 ulong retval;
291
292#ifdef DEBUG
293 int x;
294#endif
295 addr = flash_make_addr (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000296
wdenke65527f2004-02-12 00:47:09 +0000297#ifdef DEBUG
298 debug ("long addr is at %p info->portwidth = %d\n", addr,
299 info->portwidth);
300 for (x = 0; x < 4 * info->portwidth; x++) {
301 debug ("addr[%x] = 0x%x\n", x, addr[x]);
302 }
303#endif
304#if defined(__LITTLE_ENDIAN)
305 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenke537b3b2004-02-23 23:54:43 +0000306 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenke65527f2004-02-12 00:47:09 +0000307#else
308 retval = (addr[(2 * info->portwidth) - 1] << 24) |
309 (addr[(info->portwidth) - 1] << 16) |
310 (addr[(4 * info->portwidth) - 1] << 8) |
311 addr[(3 * info->portwidth) - 1];
312#endif
313 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000314}
315
316/*-----------------------------------------------------------------------
317 */
318unsigned long flash_init (void)
319{
320 unsigned long size = 0;
321 int i;
322
323 /* Init: no FLASHes known */
wdenke65527f2004-02-12 00:47:09 +0000324 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000325 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenke65527f2004-02-12 00:47:09 +0000326 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk2cefd152004-02-08 22:55:38 +0000327 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
wdenke537b3b2004-02-23 23:54:43 +0000328 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
329 i, flash_info[i].size, flash_info[i].size << 20);
wdenk2cefd152004-02-08 22:55:38 +0000330 }
331 }
332
333 /* Monitor protection ON by default */
334#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenke65527f2004-02-12 00:47:09 +0000335 flash_protect (FLAG_PROTECT_SET,
336 CFG_MONITOR_BASE,
337 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
338 &flash_info[0]);
wdenk2cefd152004-02-08 22:55:38 +0000339#endif
340
341 return (size);
342}
343
344/*-----------------------------------------------------------------------
345 */
wdenke65527f2004-02-12 00:47:09 +0000346int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +0000347{
348 int rcode = 0;
349 int prot;
350 flash_sect_t sect;
351
wdenke65527f2004-02-12 00:47:09 +0000352 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000353 puts ("Can't erase unknown flash type - aborted\n");
wdenk2cefd152004-02-08 22:55:38 +0000354 return 1;
355 }
356 if ((s_first < 0) || (s_first > s_last)) {
wdenk42c05472004-03-23 22:14:11 +0000357 puts ("- no sectors to erase\n");
wdenk2cefd152004-02-08 22:55:38 +0000358 return 1;
359 }
360
361 prot = 0;
wdenke65527f2004-02-12 00:47:09 +0000362 for (sect = s_first; sect <= s_last; ++sect) {
wdenk2cefd152004-02-08 22:55:38 +0000363 if (info->protect[sect]) {
364 prot++;
365 }
366 }
367 if (prot) {
wdenke65527f2004-02-12 00:47:09 +0000368 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk2cefd152004-02-08 22:55:38 +0000369 } else {
wdenk42c05472004-03-23 22:14:11 +0000370 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000371 }
372
373
wdenke65527f2004-02-12 00:47:09 +0000374 for (sect = s_first; sect <= s_last; sect++) {
wdenk2cefd152004-02-08 22:55:38 +0000375 if (info->protect[sect] == 0) { /* not protected */
wdenke65527f2004-02-12 00:47:09 +0000376 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000377 case CFI_CMDSET_INTEL_STANDARD:
378 case CFI_CMDSET_INTEL_EXTENDED:
wdenke537b3b2004-02-23 23:54:43 +0000379 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
380 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
381 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk2cefd152004-02-08 22:55:38 +0000382 break;
383 case CFI_CMDSET_AMD_STANDARD:
384 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000385 flash_unlock_seq (info, sect);
wdenked2ac4b2004-03-14 18:23:55 +0000386 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
387 AMD_CMD_ERASE_START);
wdenke65527f2004-02-12 00:47:09 +0000388 flash_unlock_seq (info, sect);
wdenke537b3b2004-02-23 23:54:43 +0000389 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk2cefd152004-02-08 22:55:38 +0000390 break;
391 default:
wdenke65527f2004-02-12 00:47:09 +0000392 debug ("Unkown flash vendor %d\n",
393 info->vendor);
wdenk2cefd152004-02-08 22:55:38 +0000394 break;
395 }
396
wdenke65527f2004-02-12 00:47:09 +0000397 if (flash_full_status_check
398 (info, sect, info->erase_blk_tout, "erase")) {
wdenk2cefd152004-02-08 22:55:38 +0000399 rcode = 1;
400 } else
wdenk42c05472004-03-23 22:14:11 +0000401 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +0000402 }
403 }
wdenk42c05472004-03-23 22:14:11 +0000404 puts (" done\n");
wdenk2cefd152004-02-08 22:55:38 +0000405 return rcode;
406}
407
408/*-----------------------------------------------------------------------
409 */
wdenke65527f2004-02-12 00:47:09 +0000410void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +0000411{
412 int i;
413
414 if (info->flash_id != FLASH_MAN_CFI) {
wdenk42c05472004-03-23 22:14:11 +0000415 puts ("missing or unknown FLASH type\n");
wdenk2cefd152004-02-08 22:55:38 +0000416 return;
417 }
418
wdenke65527f2004-02-12 00:47:09 +0000419 printf ("CFI conformant FLASH (%d x %d)",
420 (info->portwidth << 3), (info->chipwidth << 3));
wdenk2cefd152004-02-08 22:55:38 +0000421 printf (" Size: %ld MB in %d Sectors\n",
422 info->size >> 20, info->sector_count);
wdenke537b3b2004-02-23 23:54:43 +0000423 printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
424 info->erase_blk_tout,
425 info->write_tout,
426 info->buffer_write_tout,
427 info->buffer_size);
wdenk2cefd152004-02-08 22:55:38 +0000428
wdenk42c05472004-03-23 22:14:11 +0000429 puts (" Sector Start Addresses:");
wdenke65527f2004-02-12 00:47:09 +0000430 for (i = 0; i < info->sector_count; ++i) {
wdenk2cefd152004-02-08 22:55:38 +0000431#ifdef CFG_FLASH_EMPTY_INFO
432 int k;
433 int size;
434 int erased;
435 volatile unsigned long *flash;
436
437 /*
438 * Check if whole sector is erased
439 */
wdenke65527f2004-02-12 00:47:09 +0000440 if (i != (info->sector_count - 1))
441 size = info->start[i + 1] - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000442 else
wdenke65527f2004-02-12 00:47:09 +0000443 size = info->start[0] + info->size - info->start[i];
wdenk2cefd152004-02-08 22:55:38 +0000444 erased = 1;
wdenke65527f2004-02-12 00:47:09 +0000445 flash = (volatile unsigned long *) info->start[i];
446 size = size >> 2; /* divide by 4 for longword access */
447 for (k = 0; k < size; k++) {
448 if (*flash++ != 0xffffffff) {
449 erased = 0;
450 break;
451 }
452 }
wdenk2cefd152004-02-08 22:55:38 +0000453
454 if ((i % 5) == 0)
455 printf ("\n");
456 /* print empty and read-only info */
457 printf (" %08lX%s%s",
458 info->start[i],
459 erased ? " E" : " ",
460 info->protect[i] ? "RO " : " ");
461#else
462 if ((i % 5) == 0)
463 printf ("\n ");
464 printf (" %08lX%s",
wdenke65527f2004-02-12 00:47:09 +0000465 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenk2cefd152004-02-08 22:55:38 +0000466#endif
467 }
wdenk42c05472004-03-23 22:14:11 +0000468 putc ('\n');
wdenk2cefd152004-02-08 22:55:38 +0000469 return;
470}
471
472/*-----------------------------------------------------------------------
473 * Copy memory to flash, returns:
474 * 0 - OK
475 * 1 - write timeout
476 * 2 - Flash not erased
477 */
wdenke65527f2004-02-12 00:47:09 +0000478int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +0000479{
480 ulong wp;
481 ulong cp;
482 int aln;
483 cfiword_t cword;
484 int i, rc;
485
wdenke65527f2004-02-12 00:47:09 +0000486#ifdef CFG_FLASH_USE_BUFFER_WRITE
487 int buffered_size;
488#endif
wdenke65527f2004-02-12 00:47:09 +0000489 /* get lower aligned address */
wdenk2cefd152004-02-08 22:55:38 +0000490 /* get lower aligned address */
491 wp = (addr & ~(info->portwidth - 1));
492
493 /* handle unaligned start */
wdenke65527f2004-02-12 00:47:09 +0000494 if ((aln = addr - wp) != 0) {
wdenk2cefd152004-02-08 22:55:38 +0000495 cword.l = 0;
496 cp = wp;
wdenke65527f2004-02-12 00:47:09 +0000497 for (i = 0; i < aln; ++i, ++cp)
498 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000499
wdenke65527f2004-02-12 00:47:09 +0000500 for (; (i < info->portwidth) && (cnt > 0); i++) {
501 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000502 cnt--;
503 cp++;
504 }
wdenke65527f2004-02-12 00:47:09 +0000505 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
506 flash_add_byte (info, &cword, (*(uchar *) cp));
507 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000508 return rc;
509 wp = cp;
510 }
511
wdenke65527f2004-02-12 00:47:09 +0000512 /* handle the aligned part */
wdenk2cefd152004-02-08 22:55:38 +0000513#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenke65527f2004-02-12 00:47:09 +0000514 buffered_size = (info->portwidth / info->chipwidth);
515 buffered_size *= info->buffer_size;
516 while (cnt >= info->portwidth) {
517 i = buffered_size > cnt ? cnt : buffered_size;
518 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk2cefd152004-02-08 22:55:38 +0000519 return rc;
520 wp += i;
521 src += i;
wdenke65527f2004-02-12 00:47:09 +0000522 cnt -= i;
wdenk2cefd152004-02-08 22:55:38 +0000523 }
524#else
wdenke65527f2004-02-12 00:47:09 +0000525 while (cnt >= info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000526 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000527 for (i = 0; i < info->portwidth; i++) {
528 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000529 }
wdenke65527f2004-02-12 00:47:09 +0000530 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk2cefd152004-02-08 22:55:38 +0000531 return rc;
532 wp += info->portwidth;
533 cnt -= info->portwidth;
534 }
535#endif /* CFG_FLASH_USE_BUFFER_WRITE */
536 if (cnt == 0) {
537 return (0);
538 }
539
540 /*
541 * handle unaligned tail bytes
542 */
543 cword.l = 0;
wdenke65527f2004-02-12 00:47:09 +0000544 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
545 flash_add_byte (info, &cword, *src++);
wdenk2cefd152004-02-08 22:55:38 +0000546 --cnt;
547 }
wdenke65527f2004-02-12 00:47:09 +0000548 for (; i < info->portwidth; ++i, ++cp) {
549 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk2cefd152004-02-08 22:55:38 +0000550 }
551
wdenke65527f2004-02-12 00:47:09 +0000552 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +0000553}
554
555/*-----------------------------------------------------------------------
556 */
557#ifdef CFG_FLASH_PROTECTION
558
wdenke65527f2004-02-12 00:47:09 +0000559int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +0000560{
561 int retcode = 0;
562
wdenke65527f2004-02-12 00:47:09 +0000563 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
564 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
565 if (prot)
566 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk2cefd152004-02-08 22:55:38 +0000567 else
wdenke65527f2004-02-12 00:47:09 +0000568 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk2cefd152004-02-08 22:55:38 +0000569
wdenke65527f2004-02-12 00:47:09 +0000570 if ((retcode =
571 flash_full_status_check (info, sector, info->erase_blk_tout,
572 prot ? "protect" : "unprotect")) == 0) {
wdenk2cefd152004-02-08 22:55:38 +0000573
574 info->protect[sector] = prot;
575 /* Intel's unprotect unprotects all locking */
wdenke65527f2004-02-12 00:47:09 +0000576 if (prot == 0) {
wdenk2cefd152004-02-08 22:55:38 +0000577 flash_sect_t i;
wdenke65527f2004-02-12 00:47:09 +0000578
579 for (i = 0; i < info->sector_count; i++) {
580 if (info->protect[i])
581 flash_real_protect (info, i, 1);
wdenk2cefd152004-02-08 22:55:38 +0000582 }
583 }
584 }
wdenk2cefd152004-02-08 22:55:38 +0000585 return retcode;
wdenke65527f2004-02-12 00:47:09 +0000586}
587
wdenk2cefd152004-02-08 22:55:38 +0000588/*-----------------------------------------------------------------------
589 * flash_read_user_serial - read the OneTimeProgramming cells
590 */
wdenke65527f2004-02-12 00:47:09 +0000591void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
592 int len)
wdenk2cefd152004-02-08 22:55:38 +0000593{
wdenke65527f2004-02-12 00:47:09 +0000594 uchar *src;
595 uchar *dst;
wdenk2cefd152004-02-08 22:55:38 +0000596
597 dst = buffer;
wdenke65527f2004-02-12 00:47:09 +0000598 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
599 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
600 memcpy (dst, src + offset, len);
601 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
wdenk2cefd152004-02-08 22:55:38 +0000602}
wdenke65527f2004-02-12 00:47:09 +0000603
wdenk2cefd152004-02-08 22:55:38 +0000604/*
605 * flash_read_factory_serial - read the device Id from the protection area
606 */
wdenke65527f2004-02-12 00:47:09 +0000607void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
608 int len)
wdenk2cefd152004-02-08 22:55:38 +0000609{
wdenke65527f2004-02-12 00:47:09 +0000610 uchar *src;
wdenk6cfa84e2004-02-10 00:03:41 +0000611
wdenke65527f2004-02-12 00:47:09 +0000612 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
613 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
614 memcpy (buffer, src + offset, len);
615 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
wdenk2cefd152004-02-08 22:55:38 +0000616}
617
618#endif /* CFG_FLASH_PROTECTION */
619
wdenke65527f2004-02-12 00:47:09 +0000620/*
621 * flash_is_busy - check to see if the flash is busy
622 * This routine checks the status of the chip and returns true if the chip is busy
623 */
624static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000625{
626 int retval;
wdenke65527f2004-02-12 00:47:09 +0000627
628 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000629 case CFI_CMDSET_INTEL_STANDARD:
630 case CFI_CMDSET_INTEL_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000631 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk2cefd152004-02-08 22:55:38 +0000632 break;
633 case CFI_CMDSET_AMD_STANDARD:
634 case CFI_CMDSET_AMD_EXTENDED:
wdenke65527f2004-02-12 00:47:09 +0000635 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk2cefd152004-02-08 22:55:38 +0000636 break;
637 default:
638 retval = 0;
639 }
wdenke65527f2004-02-12 00:47:09 +0000640 debug ("flash_is_busy: %d\n", retval);
wdenk2cefd152004-02-08 22:55:38 +0000641 return retval;
642}
wdenke65527f2004-02-12 00:47:09 +0000643
wdenk2cefd152004-02-08 22:55:38 +0000644/*-----------------------------------------------------------------------
645 * wait for XSR.7 to be set. Time out with an error if it does not.
646 * This routine does not set the flash to read-array mode.
647 */
wdenke65527f2004-02-12 00:47:09 +0000648static int flash_status_check (flash_info_t * info, flash_sect_t sector,
649 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000650{
651 ulong start;
652
653 /* Wait for command completion */
654 start = get_timer (0);
wdenke65527f2004-02-12 00:47:09 +0000655 while (flash_is_busy (info, sector)) {
656 if (get_timer (start) > info->erase_blk_tout * CFG_HZ) {
657 printf ("Flash %s timeout at address %lx data %lx\n",
658 prompt, info->start[sector],
659 flash_read_long (info, sector, 0));
660 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +0000661 return ERR_TIMOUT;
662 }
663 }
664 return ERR_OK;
665}
wdenke65527f2004-02-12 00:47:09 +0000666
wdenk2cefd152004-02-08 22:55:38 +0000667/*-----------------------------------------------------------------------
668 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
669 * This routine sets the flash to read-array mode.
670 */
wdenke65527f2004-02-12 00:47:09 +0000671static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
672 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000673{
674 int retcode;
wdenke65527f2004-02-12 00:47:09 +0000675
676 retcode = flash_status_check (info, sector, tout, prompt);
677 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +0000678 case CFI_CMDSET_INTEL_EXTENDED:
679 case CFI_CMDSET_INTEL_STANDARD:
wdenke65527f2004-02-12 00:47:09 +0000680 if ((retcode != ERR_OK)
681 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk2cefd152004-02-08 22:55:38 +0000682 retcode = ERR_INVAL;
wdenke65527f2004-02-12 00:47:09 +0000683 printf ("Flash %s error at address %lx\n", prompt,
684 info->start[sector]);
wdenke537b3b2004-02-23 23:54:43 +0000685 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000686 puts ("Command Sequence Error.\n");
wdenke537b3b2004-02-23 23:54:43 +0000687 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000688 puts ("Block Erase Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000689 retcode = ERR_NOT_ERASED;
wdenke537b3b2004-02-23 23:54:43 +0000690 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenk42c05472004-03-23 22:14:11 +0000691 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000692 }
wdenke65527f2004-02-12 00:47:09 +0000693 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk42c05472004-03-23 22:14:11 +0000694 puts ("Block locked.\n");
wdenke65527f2004-02-12 00:47:09 +0000695 retcode = ERR_PROTECTED;
696 }
697 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk42c05472004-03-23 22:14:11 +0000698 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000699 }
wdenke65527f2004-02-12 00:47:09 +0000700 flash_write_cmd (info, sector, 0, FLASH_CMD_RESET);
wdenk2cefd152004-02-08 22:55:38 +0000701 break;
702 default:
703 break;
704 }
705 return retcode;
706}
wdenke65527f2004-02-12 00:47:09 +0000707
wdenk2cefd152004-02-08 22:55:38 +0000708/*-----------------------------------------------------------------------
709 */
wdenke65527f2004-02-12 00:47:09 +0000710static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000711{
wdenk369d43d2004-03-14 14:09:05 +0000712#if defined(__LITTLE_ENDIAN)
713 unsigned short w;
714 unsigned int l;
715 unsigned long long ll;
716#endif
717
wdenke65527f2004-02-12 00:47:09 +0000718 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000719 case FLASH_CFI_8BIT:
720 cword->c = c;
721 break;
722 case FLASH_CFI_16BIT:
wdenk369d43d2004-03-14 14:09:05 +0000723#if defined(__LITTLE_ENDIAN)
724 w = c;
725 w <<= 8;
726 cword->w = (cword->w >> 8) | w;
727#else
wdenk2cefd152004-02-08 22:55:38 +0000728 cword->w = (cword->w << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000729#endif
wdenk2cefd152004-02-08 22:55:38 +0000730 break;
731 case FLASH_CFI_32BIT:
wdenk369d43d2004-03-14 14:09:05 +0000732#if defined(__LITTLE_ENDIAN)
733 l = c;
734 l <<= 24;
735 cword->l = (cword->l >> 8) | l;
736#else
wdenk2cefd152004-02-08 22:55:38 +0000737 cword->l = (cword->l << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000738#endif
wdenk2cefd152004-02-08 22:55:38 +0000739 break;
740 case FLASH_CFI_64BIT:
wdenk369d43d2004-03-14 14:09:05 +0000741#if defined(__LITTLE_ENDIAN)
742 ll = c;
743 ll <<= 56;
744 cword->ll = (cword->ll >> 8) | ll;
745#else
wdenk2cefd152004-02-08 22:55:38 +0000746 cword->ll = (cword->ll << 8) | c;
wdenk369d43d2004-03-14 14:09:05 +0000747#endif
wdenk2cefd152004-02-08 22:55:38 +0000748 break;
749 }
750}
751
752
753/*-----------------------------------------------------------------------
754 * make a proper sized command based on the port and chip widths
755 */
wdenke65527f2004-02-12 00:47:09 +0000756static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk2cefd152004-02-08 22:55:38 +0000757{
758 int i;
wdenke65527f2004-02-12 00:47:09 +0000759
760#if defined(__LITTLE_ENDIAN)
wdenke537b3b2004-02-23 23:54:43 +0000761 ushort stmpw;
762 uint stmpi;
wdenke65527f2004-02-12 00:47:09 +0000763#endif
764 uchar *cp = (uchar *) cmdbuf;
765
766 for (i = 0; i < info->portwidth; i++)
767 *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd;
768#if defined(__LITTLE_ENDIAN)
wdenke537b3b2004-02-23 23:54:43 +0000769 switch (info->portwidth) {
770 case FLASH_CFI_8BIT:
771 break;
772 case FLASH_CFI_16BIT:
773 stmpw = *(ushort *) cmdbuf;
774 *(ushort *) cmdbuf = __swab16 (stmpw);
775 break;
776 case FLASH_CFI_32BIT:
777 stmpi = *(uint *) cmdbuf;
778 *(uint *) cmdbuf = __swab32 (stmpi);
779 break;
780 default:
wdenk42c05472004-03-23 22:14:11 +0000781 puts ("WARNING: flash_make_cmd: unsuppported LittleEndian mode\n");
wdenke537b3b2004-02-23 23:54:43 +0000782 break;
wdenke65527f2004-02-12 00:47:09 +0000783 }
784#endif
wdenk2cefd152004-02-08 22:55:38 +0000785}
786
787/*
788 * Write a proper sized command to the correct address
789 */
wdenke537b3b2004-02-23 23:54:43 +0000790static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000791{
792
793 volatile cfiptr_t addr;
794 cfiword_t cword;
wdenke65527f2004-02-12 00:47:09 +0000795
796 addr.cp = flash_make_addr (info, sect, offset);
797 flash_make_cmd (info, cmd, &cword);
798 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000799 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000800 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
801 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk2cefd152004-02-08 22:55:38 +0000802 *addr.cp = cword.c;
803 break;
804 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000805 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
806 cmd, cword.w,
wdenk2cefd152004-02-08 22:55:38 +0000807 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
808 *addr.wp = cword.w;
809 break;
810 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000811 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
812 cmd, cword.l,
wdenk2cefd152004-02-08 22:55:38 +0000813 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
814 *addr.lp = cword.l;
815 break;
816 case FLASH_CFI_64BIT:
817#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000818 {
wdenk2cefd152004-02-08 22:55:38 +0000819 char str[20];
wdenk6cfa84e2004-02-10 00:03:41 +0000820
wdenke65527f2004-02-12 00:47:09 +0000821 print_longlong (str, cword.ll);
822
823 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
824 addr.llp, cmd, str,
wdenk2cefd152004-02-08 22:55:38 +0000825 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
826 }
827#endif
828 *addr.llp = cword.ll;
829 break;
830 }
831}
832
wdenke65527f2004-02-12 00:47:09 +0000833static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk2cefd152004-02-08 22:55:38 +0000834{
wdenked2ac4b2004-03-14 18:23:55 +0000835 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
836 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
wdenk2cefd152004-02-08 22:55:38 +0000837}
wdenke65527f2004-02-12 00:47:09 +0000838
wdenk2cefd152004-02-08 22:55:38 +0000839/*-----------------------------------------------------------------------
840 */
wdenke537b3b2004-02-23 23:54:43 +0000841static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000842{
843 cfiptr_t cptr;
844 cfiword_t cword;
845 int retval;
wdenke65527f2004-02-12 00:47:09 +0000846
847 cptr.cp = flash_make_addr (info, sect, offset);
848 flash_make_cmd (info, cmd, &cword);
wdenk2cefd152004-02-08 22:55:38 +0000849
wdenke65527f2004-02-12 00:47:09 +0000850 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
851 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000852 case FLASH_CFI_8BIT:
wdenke65527f2004-02-12 00:47:09 +0000853 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk2cefd152004-02-08 22:55:38 +0000854 retval = (cptr.cp[0] == cword.c);
855 break;
856 case FLASH_CFI_16BIT:
wdenke65527f2004-02-12 00:47:09 +0000857 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk2cefd152004-02-08 22:55:38 +0000858 retval = (cptr.wp[0] == cword.w);
859 break;
860 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +0000861 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk2cefd152004-02-08 22:55:38 +0000862 retval = (cptr.lp[0] == cword.l);
863 break;
864 case FLASH_CFI_64BIT:
wdenk6cfa84e2004-02-10 00:03:41 +0000865#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000866 {
wdenk2cefd152004-02-08 22:55:38 +0000867 char str1[20];
868 char str2[20];
wdenke65527f2004-02-12 00:47:09 +0000869
870 print_longlong (str1, cptr.llp[0]);
871 print_longlong (str2, cword.ll);
872 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +0000873 }
874#endif
875 retval = (cptr.llp[0] == cword.ll);
876 break;
877 default:
878 retval = 0;
879 break;
880 }
881 return retval;
882}
wdenke65527f2004-02-12 00:47:09 +0000883
wdenk2cefd152004-02-08 22:55:38 +0000884/*-----------------------------------------------------------------------
885 */
wdenke537b3b2004-02-23 23:54:43 +0000886static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000887{
888 cfiptr_t cptr;
889 cfiword_t cword;
890 int retval;
wdenke65527f2004-02-12 00:47:09 +0000891
892 cptr.cp = flash_make_addr (info, sect, offset);
893 flash_make_cmd (info, cmd, &cword);
894 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000895 case FLASH_CFI_8BIT:
896 retval = ((cptr.cp[0] & cword.c) == cword.c);
897 break;
898 case FLASH_CFI_16BIT:
899 retval = ((cptr.wp[0] & cword.w) == cword.w);
900 break;
901 case FLASH_CFI_32BIT:
902 retval = ((cptr.lp[0] & cword.l) == cword.l);
903 break;
904 case FLASH_CFI_64BIT:
905 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenke65527f2004-02-12 00:47:09 +0000906 break;
wdenk2cefd152004-02-08 22:55:38 +0000907 default:
908 retval = 0;
909 break;
910 }
911 return retval;
912}
913
914/*-----------------------------------------------------------------------
915 */
wdenke537b3b2004-02-23 23:54:43 +0000916static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000917{
918 cfiptr_t cptr;
919 cfiword_t cword;
920 int retval;
wdenke65527f2004-02-12 00:47:09 +0000921
922 cptr.cp = flash_make_addr (info, sect, offset);
923 flash_make_cmd (info, cmd, &cword);
924 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +0000925 case FLASH_CFI_8BIT:
926 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
927 break;
928 case FLASH_CFI_16BIT:
929 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
930 break;
931 case FLASH_CFI_32BIT:
932 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
933 break;
934 case FLASH_CFI_64BIT:
wdenke65527f2004-02-12 00:47:09 +0000935 retval = ((cptr.llp[0] & cword.ll) !=
936 (cptr.llp[0] & cword.ll));
wdenk2cefd152004-02-08 22:55:38 +0000937 break;
938 default:
939 retval = 0;
940 break;
941 }
942 return retval;
943}
944
945/*-----------------------------------------------------------------------
946 * detect if flash is compatible with the Common Flash Interface (CFI)
947 * http://www.jedec.org/download/search/jesd68.pdf
948 *
949*/
wdenke65527f2004-02-12 00:47:09 +0000950static int flash_detect_cfi (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +0000951{
wdenke65527f2004-02-12 00:47:09 +0000952 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +0000953
wdenke65527f2004-02-12 00:47:09 +0000954 for (info->portwidth = FLASH_CFI_8BIT;
955 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
956 for (info->chipwidth = FLASH_CFI_BY8;
957 info->chipwidth <= info->portwidth;
958 info->chipwidth <<= 1) {
959 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
wdenke537b3b2004-02-23 23:54:43 +0000960 flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
961 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
962 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
963 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
964 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
wdenke65527f2004-02-12 00:47:09 +0000965 debug ("device interface is %d\n",
966 info->interface);
967 debug ("found port %d chip %d ",
968 info->portwidth, info->chipwidth);
969 debug ("port %d bits chip %d bits\n",
wdenke537b3b2004-02-23 23:54:43 +0000970 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
971 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk2cefd152004-02-08 22:55:38 +0000972 return 1;
973 }
974 }
975 }
wdenke65527f2004-02-12 00:47:09 +0000976 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +0000977 return 0;
978}
wdenke65527f2004-02-12 00:47:09 +0000979
wdenk2cefd152004-02-08 22:55:38 +0000980/*
981 * The following code cannot be run from FLASH!
982 *
983 */
984static ulong flash_get_size (ulong base, int banknum)
985{
wdenke65527f2004-02-12 00:47:09 +0000986 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +0000987 int i, j;
988 flash_sect_t sect_cnt;
989 unsigned long sector;
990 unsigned long tmp;
991 int size_ratio;
992 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +0000993 int erase_region_size;
994 int erase_region_count;
wdenk2cefd152004-02-08 22:55:38 +0000995
996 info->start[0] = base;
997
wdenke65527f2004-02-12 00:47:09 +0000998 if (flash_detect_cfi (info)) {
wdenke537b3b2004-02-23 23:54:43 +0000999 info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
wdenke65527f2004-02-12 00:47:09 +00001000#ifdef DEBUG
1001 flash_printqry (info, 0);
1002#endif
1003 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001004 case CFI_CMDSET_INTEL_STANDARD:
1005 case CFI_CMDSET_INTEL_EXTENDED:
1006 default:
1007 info->cmd_reset = FLASH_CMD_RESET;
1008 break;
1009 case CFI_CMDSET_AMD_STANDARD:
1010 case CFI_CMDSET_AMD_EXTENDED:
1011 info->cmd_reset = AMD_CMD_RESET;
1012 break;
1013 }
wdenk6cfa84e2004-02-10 00:03:41 +00001014
wdenke65527f2004-02-12 00:47:09 +00001015 debug ("manufacturer is %d\n", info->vendor);
wdenk2cefd152004-02-08 22:55:38 +00001016 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00001017 /* if the chip is x8/x16 reduce the ratio by half */
1018 if ((info->interface == FLASH_CFI_X8X16)
1019 && (info->chipwidth == FLASH_CFI_BY8)) {
1020 size_ratio >>= 1;
1021 }
wdenke537b3b2004-02-23 23:54:43 +00001022 num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
wdenke65527f2004-02-12 00:47:09 +00001023 debug ("size_ratio %d port %d bits chip %d bits\n",
1024 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1025 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1026 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00001027 sect_cnt = 0;
1028 sector = base;
wdenke65527f2004-02-12 00:47:09 +00001029 for (i = 0; i < num_erase_regions; i++) {
1030 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00001031 printf ("%d erase regions found, only %d used\n",
1032 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00001033 break;
1034 }
wdenke65527f2004-02-12 00:47:09 +00001035 tmp = flash_read_long (info, 0,
1036 FLASH_OFFSET_ERASE_REGIONS +
1037 i * 4);
1038 erase_region_size =
1039 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk2cefd152004-02-08 22:55:38 +00001040 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00001041 erase_region_count = (tmp & 0xffff) + 1;
wdenkaeba06f2004-06-09 17:34:58 +00001042 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00001043 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00001044 for (j = 0; j < erase_region_count; j++) {
wdenk2cefd152004-02-08 22:55:38 +00001045 info->start[sect_cnt] = sector;
1046 sector += (erase_region_size * size_ratio);
wdenke65527f2004-02-12 00:47:09 +00001047 info->protect[sect_cnt] =
1048 flash_isset (info, sect_cnt,
1049 FLASH_OFFSET_PROTECT,
1050 FLASH_STATUS_PROTECT);
wdenk2cefd152004-02-08 22:55:38 +00001051 sect_cnt++;
1052 }
1053 }
1054
1055 info->sector_count = sect_cnt;
1056 /* multiply the size by the number of chips */
wdenke537b3b2004-02-23 23:54:43 +00001057 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1058 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
wdenke65527f2004-02-12 00:47:09 +00001059 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenke537b3b2004-02-23 23:54:43 +00001060 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
wdenke65527f2004-02-12 00:47:09 +00001061 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
wdenke537b3b2004-02-23 23:54:43 +00001062 info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
wdenke65527f2004-02-12 00:47:09 +00001063 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT);
wdenke537b3b2004-02-23 23:54:43 +00001064 info->write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT))) / 1000;
wdenk2cefd152004-02-08 22:55:38 +00001065 info->flash_id = FLASH_MAN_CFI;
wdenked2ac4b2004-03-14 18:23:55 +00001066 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1067 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1068 }
wdenk2cefd152004-02-08 22:55:38 +00001069 }
1070
wdenke65527f2004-02-12 00:47:09 +00001071 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
1072 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00001073}
1074
1075
1076/*-----------------------------------------------------------------------
1077 */
wdenke65527f2004-02-12 00:47:09 +00001078static int flash_write_cfiword (flash_info_t * info, ulong dest,
1079 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +00001080{
1081
1082 cfiptr_t ctladdr;
1083 cfiptr_t cptr;
1084 int flag;
1085
wdenke65527f2004-02-12 00:47:09 +00001086 ctladdr.cp = flash_make_addr (info, 0, 0);
1087 cptr.cp = (uchar *) dest;
wdenk2cefd152004-02-08 22:55:38 +00001088
1089
1090 /* Check if Flash is (sufficiently) erased */
wdenke65527f2004-02-12 00:47:09 +00001091 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001092 case FLASH_CFI_8BIT:
1093 flag = ((cptr.cp[0] & cword.c) == cword.c);
1094 break;
1095 case FLASH_CFI_16BIT:
1096 flag = ((cptr.wp[0] & cword.w) == cword.w);
1097 break;
1098 case FLASH_CFI_32BIT:
wdenke65527f2004-02-12 00:47:09 +00001099 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk2cefd152004-02-08 22:55:38 +00001100 break;
1101 case FLASH_CFI_64BIT:
1102 flag = ((cptr.lp[0] & cword.ll) == cword.ll);
1103 break;
1104 default:
1105 return 2;
1106 }
wdenke65527f2004-02-12 00:47:09 +00001107 if (!flag)
wdenk2cefd152004-02-08 22:55:38 +00001108 return 2;
1109
1110 /* Disable interrupts which might cause a timeout here */
wdenke65527f2004-02-12 00:47:09 +00001111 flag = disable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001112
wdenke65527f2004-02-12 00:47:09 +00001113 switch (info->vendor) {
wdenk2cefd152004-02-08 22:55:38 +00001114 case CFI_CMDSET_INTEL_EXTENDED:
1115 case CFI_CMDSET_INTEL_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001116 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1117 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001118 break;
1119 case CFI_CMDSET_AMD_EXTENDED:
1120 case CFI_CMDSET_AMD_STANDARD:
wdenke65527f2004-02-12 00:47:09 +00001121 flash_unlock_seq (info, 0);
wdenked2ac4b2004-03-14 18:23:55 +00001122 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk2cefd152004-02-08 22:55:38 +00001123 break;
1124 }
1125
wdenke65527f2004-02-12 00:47:09 +00001126 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001127 case FLASH_CFI_8BIT:
1128 cptr.cp[0] = cword.c;
1129 break;
1130 case FLASH_CFI_16BIT:
1131 cptr.wp[0] = cword.w;
1132 break;
1133 case FLASH_CFI_32BIT:
1134 cptr.lp[0] = cword.l;
1135 break;
1136 case FLASH_CFI_64BIT:
1137 cptr.llp[0] = cword.ll;
1138 break;
1139 }
1140
1141 /* re-enable interrupts if necessary */
wdenke65527f2004-02-12 00:47:09 +00001142 if (flag)
1143 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +00001144
wdenke65527f2004-02-12 00:47:09 +00001145 return flash_full_status_check (info, 0, info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +00001146}
1147
1148#ifdef CFG_FLASH_USE_BUFFER_WRITE
1149
1150/* loop through the sectors from the highest address
1151 * when the passed address is greater or equal to the sector address
1152 * we have a match
1153 */
wdenke65527f2004-02-12 00:47:09 +00001154static flash_sect_t find_sector (flash_info_t * info, ulong addr)
wdenk2cefd152004-02-08 22:55:38 +00001155{
1156 flash_sect_t sector;
wdenke65527f2004-02-12 00:47:09 +00001157
1158 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1159 if (addr >= info->start[sector])
wdenk2cefd152004-02-08 22:55:38 +00001160 break;
1161 }
1162 return sector;
1163}
1164
wdenke65527f2004-02-12 00:47:09 +00001165static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1166 int len)
wdenk2cefd152004-02-08 22:55:38 +00001167{
1168 flash_sect_t sector;
1169 int cnt;
1170 int retcode;
1171 volatile cfiptr_t src;
1172 volatile cfiptr_t dst;
wdenked2ac4b2004-03-14 18:23:55 +00001173 /* buffered writes in the AMD chip set is not supported yet */
1174 if((info->vendor == CFI_CMDSET_AMD_STANDARD) ||
1175 (info->vendor == CFI_CMDSET_AMD_EXTENDED))
1176 return ERR_INVAL;
wdenk2cefd152004-02-08 22:55:38 +00001177
1178 src.cp = cp;
wdenke65527f2004-02-12 00:47:09 +00001179 dst.cp = (uchar *) dest;
1180 sector = find_sector (info, dest);
1181 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1182 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1183 if ((retcode =
1184 flash_status_check (info, sector, info->buffer_write_tout,
1185 "write to buffer")) == ERR_OK) {
1186 /* reduce the number of loops by the width of the port */
1187 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001188 case FLASH_CFI_8BIT:
1189 cnt = len;
1190 break;
1191 case FLASH_CFI_16BIT:
1192 cnt = len >> 1;
1193 break;
1194 case FLASH_CFI_32BIT:
1195 cnt = len >> 2;
1196 break;
1197 case FLASH_CFI_64BIT:
1198 cnt = len >> 3;
1199 break;
1200 default:
1201 return ERR_INVAL;
1202 break;
1203 }
wdenke65527f2004-02-12 00:47:09 +00001204 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1205 while (cnt-- > 0) {
1206 switch (info->portwidth) {
wdenk2cefd152004-02-08 22:55:38 +00001207 case FLASH_CFI_8BIT:
1208 *dst.cp++ = *src.cp++;
1209 break;
1210 case FLASH_CFI_16BIT:
1211 *dst.wp++ = *src.wp++;
1212 break;
1213 case FLASH_CFI_32BIT:
1214 *dst.lp++ = *src.lp++;
1215 break;
1216 case FLASH_CFI_64BIT:
1217 *dst.llp++ = *src.llp++;
1218 break;
1219 default:
1220 return ERR_INVAL;
1221 break;
1222 }
1223 }
wdenke65527f2004-02-12 00:47:09 +00001224 flash_write_cmd (info, sector, 0,
1225 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1226 retcode =
1227 flash_full_status_check (info, sector,
1228 info->buffer_write_tout,
1229 "buffer write");
wdenk2cefd152004-02-08 22:55:38 +00001230 }
wdenke65527f2004-02-12 00:47:09 +00001231 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
wdenk2cefd152004-02-08 22:55:38 +00001232 return retcode;
1233}
1234#endif /* CFG_USE_FLASH_BUFFER_WRITE */
1235#endif /* CFG_FLASH_CFI */