blob: c6c2751d6432220f30920fa2cc28e9b265e43fca [file] [log] [blame]
wdenk5653fc32004-02-08 22:55:38 +00001/*
wdenkbf9e3b32004-02-12 00:47:09 +00002 * (C) Copyright 2002-2004
wdenk5653fc32004-02-08 22:55:38 +00003 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4 *
5 * Copyright (C) 2003 Arabella Software Ltd.
6 * Yuli Barcohen <yuli@arabellasw.com>
7 * Modified to work with AMD flashes
8 *
wdenkbf9e3b32004-02-12 00:47:09 +00009 * Copyright (C) 2004
10 * Ed Okerson
11 * Modified to work with little-endian systems.
12 *
wdenk5653fc32004-02-08 22:55:38 +000013 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 *
31 * History
32 * 01/20/2004 - combined variants of original driver.
wdenkbf9e3b32004-02-12 00:47:09 +000033 * 01/22/2004 - Write performance enhancements for parallel chips (Tolunay)
34 * 01/23/2004 - Support for x8/x16 chips (Rune Raknerud)
35 * 01/27/2004 - Little endian support Ed Okerson
wdenk5653fc32004-02-08 22:55:38 +000036 *
37 * Tested Architectures
wdenkbf9e3b32004-02-12 00:47:09 +000038 * Port Width Chip Width # of banks Flash Chip Board
39 * 32 16 1 23F128J3 seranoa/eagle
40 * 64 16 1 23F128J3 seranoa/falcon
wdenkcd37d9e2004-02-10 00:03:41 +000041 *
wdenk5653fc32004-02-08 22:55:38 +000042 */
43
44/* The DEBUG define must be before common to enable debugging */
wdenkbf9e3b32004-02-12 00:47:09 +000045#undef DEBUG
wdenk5653fc32004-02-08 22:55:38 +000046#include <common.h>
47#include <asm/processor.h>
wdenkbf9e3b32004-02-12 00:47:09 +000048#ifdef CFG_FLASH_CFI_DRIVER
wdenk5653fc32004-02-08 22:55:38 +000049/*
50 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
51 * The width of the port and the width of the chips are determined at initialization.
52 * These widths are used to calculate the address for access CFI data structures.
53 * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
54 *
55 * References
56 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
57 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
58 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
59 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
60 *
61 * TODO
62 *
63 * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
64 * Table (ALT) to determine if protection is available
65 *
66 * Add support for other command sets Use the PRI and ALT to determine command set
67 * Verify erase and program timeouts.
68 */
69
wdenkbf9e3b32004-02-12 00:47:09 +000070#ifndef CFG_FLASH_BANKS_LIST
71#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
72#endif
73
wdenk5653fc32004-02-08 22:55:38 +000074#define FLASH_CMD_CFI 0x98
75#define FLASH_CMD_READ_ID 0x90
76#define FLASH_CMD_RESET 0xff
77#define FLASH_CMD_BLOCK_ERASE 0x20
78#define FLASH_CMD_ERASE_CONFIRM 0xD0
79#define FLASH_CMD_WRITE 0x40
80#define FLASH_CMD_PROTECT 0x60
81#define FLASH_CMD_PROTECT_SET 0x01
82#define FLASH_CMD_PROTECT_CLEAR 0xD0
83#define FLASH_CMD_CLEAR_STATUS 0x50
wdenkbf9e3b32004-02-12 00:47:09 +000084#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
85#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk5653fc32004-02-08 22:55:38 +000086
87#define FLASH_STATUS_DONE 0x80
88#define FLASH_STATUS_ESS 0x40
89#define FLASH_STATUS_ECLBS 0x20
90#define FLASH_STATUS_PSLBS 0x10
91#define FLASH_STATUS_VPENS 0x08
92#define FLASH_STATUS_PSS 0x04
93#define FLASH_STATUS_DPS 0x02
94#define FLASH_STATUS_R 0x01
95#define FLASH_STATUS_PROTECT 0x01
96
97#define AMD_CMD_RESET 0xF0
98#define AMD_CMD_WRITE 0xA0
99#define AMD_CMD_ERASE_START 0x80
100#define AMD_CMD_ERASE_SECTOR 0x30
101
102#define AMD_STATUS_TOGGLE 0x40
103#define AMD_STATUS_ERROR 0x20
104
105#define FLASH_OFFSET_CFI 0x55
106#define FLASH_OFFSET_CFI_RESP 0x10
wdenkbf9e3b32004-02-12 00:47:09 +0000107#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
wdenk5653fc32004-02-08 22:55:38 +0000108#define FLASH_OFFSET_WTOUT 0x1F
wdenkbf9e3b32004-02-12 00:47:09 +0000109#define FLASH_OFFSET_WBTOUT 0x20
wdenk5653fc32004-02-08 22:55:38 +0000110#define FLASH_OFFSET_ETOUT 0x21
wdenkbf9e3b32004-02-12 00:47:09 +0000111#define FLASH_OFFSET_CETOUT 0x22
wdenk5653fc32004-02-08 22:55:38 +0000112#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenkbf9e3b32004-02-12 00:47:09 +0000113#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk5653fc32004-02-08 22:55:38 +0000114#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenkbf9e3b32004-02-12 00:47:09 +0000115#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk5653fc32004-02-08 22:55:38 +0000116#define FLASH_OFFSET_SIZE 0x27
wdenkbf9e3b32004-02-12 00:47:09 +0000117#define FLASH_OFFSET_INTERFACE 0x28
118#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk5653fc32004-02-08 22:55:38 +0000119#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
120#define FLASH_OFFSET_ERASE_REGIONS 0x2D
121#define FLASH_OFFSET_PROTECT 0x02
wdenkbf9e3b32004-02-12 00:47:09 +0000122#define FLASH_OFFSET_USER_PROTECTION 0x85
123#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk5653fc32004-02-08 22:55:38 +0000124
125
126#define FLASH_MAN_CFI 0x01000000
127
wdenkbf9e3b32004-02-12 00:47:09 +0000128#define CFI_CMDSET_NONE 0
wdenk5653fc32004-02-08 22:55:38 +0000129#define CFI_CMDSET_INTEL_EXTENDED 1
wdenkbf9e3b32004-02-12 00:47:09 +0000130#define CFI_CMDSET_AMD_STANDARD 2
wdenk5653fc32004-02-08 22:55:38 +0000131#define CFI_CMDSET_INTEL_STANDARD 3
wdenkbf9e3b32004-02-12 00:47:09 +0000132#define CFI_CMDSET_AMD_EXTENDED 4
wdenk5653fc32004-02-08 22:55:38 +0000133#define CFI_CMDSET_MITSU_STANDARD 256
134#define CFI_CMDSET_MITSU_EXTENDED 257
wdenkbf9e3b32004-02-12 00:47:09 +0000135#define CFI_CMDSET_SST 258
wdenk5653fc32004-02-08 22:55:38 +0000136
137
138typedef union {
139 unsigned char c;
140 unsigned short w;
141 unsigned long l;
142 unsigned long long ll;
143} cfiword_t;
144
145typedef union {
wdenkbf9e3b32004-02-12 00:47:09 +0000146 volatile unsigned char *cp;
wdenk5653fc32004-02-08 22:55:38 +0000147 volatile unsigned short *wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000148 volatile unsigned long *lp;
wdenk5653fc32004-02-08 22:55:38 +0000149 volatile unsigned long long *llp;
150} cfiptr_t;
151
152#define NUM_ERASE_REGIONS 4
153
154static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
155
wdenkbf9e3b32004-02-12 00:47:09 +0000156flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk5653fc32004-02-08 22:55:38 +0000157
158/*-----------------------------------------------------------------------
159 * Functions
160 */
161
162typedef unsigned long flash_sect_t;
163
wdenkbf9e3b32004-02-12 00:47:09 +0000164static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
165static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
166static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
167 uint offset, uchar cmd);
168static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
169static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset,
170 uchar cmd);
171static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset,
172 uchar cmd);
173static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset,
174 uchar cmd);
175static int flash_detect_cfi (flash_info_t * info);
wdenk5653fc32004-02-08 22:55:38 +0000176static ulong flash_get_size (ulong base, int banknum);
wdenkbf9e3b32004-02-12 00:47:09 +0000177static int flash_write_cfiword (flash_info_t * info, ulong dest,
178 cfiword_t cword);
179static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
180 ulong tout, char *prompt);
wdenk5653fc32004-02-08 22:55:38 +0000181#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +0000182static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
183 int len);
wdenk5653fc32004-02-08 22:55:38 +0000184#endif
185
wdenk5653fc32004-02-08 22:55:38 +0000186/*-----------------------------------------------------------------------
187 * create an address based on the offset and the port width
188 */
wdenkbf9e3b32004-02-12 00:47:09 +0000189inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect,
190 uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000191{
wdenkbf9e3b32004-02-12 00:47:09 +0000192 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
wdenk5653fc32004-02-08 22:55:38 +0000193}
wdenkbf9e3b32004-02-12 00:47:09 +0000194
195#ifdef DEBUG
196/*-----------------------------------------------------------------------
197 * Debug support
198 */
199void print_longlong (char *str, unsigned long long data)
200{
201 int i;
202 char *cp;
203
204 cp = (unsigned char *) &data;
205 for (i = 0; i < 8; i++)
206 sprintf (&str[i * 2], "%2.2x", *cp++);
207}
208static void flash_printqry (flash_info_t * info, flash_sect_t sect)
209{
210 cfiptr_t cptr;
211 int x, y;
212
213 for (x = 0; x < 0x40; x += 16 / info->portwidth) {
214 cptr.cp =
215 flash_make_addr (info, sect,
216 x + FLASH_OFFSET_CFI_RESP);
217 debug ("%p : ", cptr.cp);
218 for (y = 0; y < 16; y++) {
219 debug ("%2.2x ", cptr.cp[y]);
220 }
221 debug (" ");
222 for (y = 0; y < 16; y++) {
223 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
224 debug ("%c", cptr.cp[y]);
225 } else {
226 debug (".");
227 }
228 }
229 debug ("\n");
230 }
231}
232
233#endif
234
235
wdenk5653fc32004-02-08 22:55:38 +0000236/*-----------------------------------------------------------------------
237 * read a character at a port width address
238 */
wdenkbf9e3b32004-02-12 00:47:09 +0000239inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000240{
241 uchar *cp;
wdenkbf9e3b32004-02-12 00:47:09 +0000242
243 cp = flash_make_addr (info, 0, offset);
244#if defined(__LITTLE_ENDIAN)
245 return (cp[0]);
246#else
wdenk5653fc32004-02-08 22:55:38 +0000247 return (cp[info->portwidth - 1]);
wdenkbf9e3b32004-02-12 00:47:09 +0000248#endif
wdenk5653fc32004-02-08 22:55:38 +0000249}
250
251/*-----------------------------------------------------------------------
252 * read a short word by swapping for ppc format.
253 */
wdenkbf9e3b32004-02-12 00:47:09 +0000254ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000255{
wdenkbf9e3b32004-02-12 00:47:09 +0000256 uchar *addr;
257 ushort retval;
wdenk5653fc32004-02-08 22:55:38 +0000258
wdenkbf9e3b32004-02-12 00:47:09 +0000259#ifdef DEBUG
260 int x;
261#endif
262 addr = flash_make_addr (info, sect, offset);
wdenk5653fc32004-02-08 22:55:38 +0000263
wdenkbf9e3b32004-02-12 00:47:09 +0000264#ifdef DEBUG
265 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
266 info->portwidth);
267 for (x = 0; x < 2 * info->portwidth; x++) {
268 debug ("addr[%x] = 0x%x\n", x, addr[x]);
269 }
270#endif
271#if defined(__LITTLE_ENDIAN)
272 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
273#else
274 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
275 addr[info->portwidth - 1]);
276#endif
277
278 debug ("retval = 0x%x\n", retval);
279 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000280}
281
282/*-----------------------------------------------------------------------
283 * read a long word by picking the least significant byte of each maiximum
284 * port size word. Swap for ppc format.
285 */
wdenkbf9e3b32004-02-12 00:47:09 +0000286ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000287{
wdenk5653fc32004-02-08 22:55:38 +0000288
wdenkbf9e3b32004-02-12 00:47:09 +0000289 uchar *addr;
290 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000291
wdenkbf9e3b32004-02-12 00:47:09 +0000292#ifdef DEBUG
293 int x;
294#endif
295 addr = flash_make_addr (info, sect, offset);
296
297#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) |
306 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)]
307 << 8);
308#else
309 retval = (addr[(2 * info->portwidth) - 1] << 24) |
310 (addr[(info->portwidth) - 1] << 16) |
311 (addr[(4 * info->portwidth) - 1] << 8) |
312 addr[(3 * info->portwidth) - 1];
313#endif
314 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000315}
316
317/*-----------------------------------------------------------------------
318 */
319unsigned long flash_init (void)
320{
321 unsigned long size = 0;
322 int i;
323
324 /* Init: no FLASHes known */
wdenkbf9e3b32004-02-12 00:47:09 +0000325 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk5653fc32004-02-08 22:55:38 +0000326 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenkbf9e3b32004-02-12 00:47:09 +0000327 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk5653fc32004-02-08 22:55:38 +0000328 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
wdenkbf9e3b32004-02-12 00:47:09 +0000329 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n", i, flash_info[i].size, flash_info[i].size << 20);
wdenk5653fc32004-02-08 22:55:38 +0000330 }
331 }
332
333 /* Monitor protection ON by default */
334#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenkbf9e3b32004-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]);
wdenk5653fc32004-02-08 22:55:38 +0000339#endif
340
341 return (size);
342}
343
344/*-----------------------------------------------------------------------
345 */
wdenkbf9e3b32004-02-12 00:47:09 +0000346int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +0000347{
348 int rcode = 0;
349 int prot;
350 flash_sect_t sect;
351
wdenkbf9e3b32004-02-12 00:47:09 +0000352 if (info->flash_id != FLASH_MAN_CFI) {
wdenk5653fc32004-02-08 22:55:38 +0000353 printf ("Can't erase unknown flash type - aborted\n");
354 return 1;
355 }
356 if ((s_first < 0) || (s_first > s_last)) {
357 printf ("- no sectors to erase\n");
358 return 1;
359 }
360
361 prot = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000362 for (sect = s_first; sect <= s_last; ++sect) {
wdenk5653fc32004-02-08 22:55:38 +0000363 if (info->protect[sect]) {
364 prot++;
365 }
366 }
367 if (prot) {
wdenkbf9e3b32004-02-12 00:47:09 +0000368 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk5653fc32004-02-08 22:55:38 +0000369 } else {
370 printf ("\n");
371 }
372
373
wdenkbf9e3b32004-02-12 00:47:09 +0000374 for (sect = s_first; sect <= s_last; sect++) {
wdenk5653fc32004-02-08 22:55:38 +0000375 if (info->protect[sect] == 0) { /* not protected */
wdenkbf9e3b32004-02-12 00:47:09 +0000376 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000377 case CFI_CMDSET_INTEL_STANDARD:
378 case CFI_CMDSET_INTEL_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000379 flash_write_cmd (info, sect, 0,
380 FLASH_CMD_CLEAR_STATUS);
381 flash_write_cmd (info, sect, 0,
382 FLASH_CMD_BLOCK_ERASE);
383 flash_write_cmd (info, sect, 0,
384 FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +0000385 break;
386 case CFI_CMDSET_AMD_STANDARD:
387 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000388 flash_unlock_seq (info, sect);
389 flash_write_cmd (info, sect, 0x555,
390 AMD_CMD_ERASE_START);
391 flash_unlock_seq (info, sect);
392 flash_write_cmd (info, sect, 0,
393 AMD_CMD_ERASE_SECTOR);
wdenk5653fc32004-02-08 22:55:38 +0000394 break;
395 default:
wdenkbf9e3b32004-02-12 00:47:09 +0000396 debug ("Unkown flash vendor %d\n",
397 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +0000398 break;
399 }
400
wdenkbf9e3b32004-02-12 00:47:09 +0000401 if (flash_full_status_check
402 (info, sect, info->erase_blk_tout, "erase")) {
wdenk5653fc32004-02-08 22:55:38 +0000403 rcode = 1;
404 } else
wdenkbf9e3b32004-02-12 00:47:09 +0000405 printf (".");
wdenk5653fc32004-02-08 22:55:38 +0000406 }
407 }
408 printf (" done\n");
409 return rcode;
410}
411
412/*-----------------------------------------------------------------------
413 */
wdenkbf9e3b32004-02-12 00:47:09 +0000414void flash_print_info (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +0000415{
416 int i;
417
418 if (info->flash_id != FLASH_MAN_CFI) {
419 printf ("missing or unknown FLASH type\n");
420 return;
421 }
422
wdenkbf9e3b32004-02-12 00:47:09 +0000423 printf ("CFI conformant FLASH (%d x %d)",
424 (info->portwidth << 3), (info->chipwidth << 3));
wdenk5653fc32004-02-08 22:55:38 +0000425 printf (" Size: %ld MB in %d Sectors\n",
426 info->size >> 20, info->sector_count);
wdenkbf9e3b32004-02-12 00:47:09 +0000427 printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n", info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
wdenk5653fc32004-02-08 22:55:38 +0000428
429 printf (" Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +0000430 for (i = 0; i < info->sector_count; ++i) {
wdenk5653fc32004-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 */
wdenkbf9e3b32004-02-12 00:47:09 +0000440 if (i != (info->sector_count - 1))
441 size = info->start[i + 1] - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000442 else
wdenkbf9e3b32004-02-12 00:47:09 +0000443 size = info->start[0] + info->size - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000444 erased = 1;
wdenkbf9e3b32004-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 }
wdenk5653fc32004-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",
wdenkbf9e3b32004-02-12 00:47:09 +0000465 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenk5653fc32004-02-08 22:55:38 +0000466#endif
467 }
468 printf ("\n");
469 return;
470}
471
472/*-----------------------------------------------------------------------
473 * Copy memory to flash, returns:
474 * 0 - OK
475 * 1 - write timeout
476 * 2 - Flash not erased
477 */
wdenkbf9e3b32004-02-12 00:47:09 +0000478int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +0000479{
480 ulong wp;
481 ulong cp;
482 int aln;
483 cfiword_t cword;
484 int i, rc;
485
wdenkbf9e3b32004-02-12 00:47:09 +0000486#ifdef CFG_FLASH_USE_BUFFER_WRITE
487 int buffered_size;
488#endif
489 int x8mode = 0;
490
491 /* special handling of 16 bit devices in 8 bit mode */
492 if ((info->interface == FLASH_CFI_X8X16)
493 && (info->chipwidth == FLASH_CFI_BY8)) {
494 switch (info->vendor) {
495 case CFI_CMDSET_INTEL_STANDARD:
496 case CFI_CMDSET_INTEL_EXTENDED:
497 x8mode = info->portwidth;
498 info->portwidth >>= 1; /* XXX - Need to test on x9/x16 in parallel. */
499 /*info->portwidth = FLASH_CFI_8BIT; */ /* XXX - Need to test on x9/x16 in parallel. */
500 break;
501 case CFI_CMDSET_AMD_STANDARD:
502 case CFI_CMDSET_AMD_EXTENDED:
503 default:
504 break;
505 }
506 }
507 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +0000508 /* get lower aligned address */
509 wp = (addr & ~(info->portwidth - 1));
510
511 /* handle unaligned start */
wdenkbf9e3b32004-02-12 00:47:09 +0000512 if ((aln = addr - wp) != 0) {
wdenk5653fc32004-02-08 22:55:38 +0000513 cword.l = 0;
514 cp = wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000515 for (i = 0; i < aln; ++i, ++cp)
516 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000517
wdenkbf9e3b32004-02-12 00:47:09 +0000518 for (; (i < info->portwidth) && (cnt > 0); i++) {
519 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000520 cnt--;
521 cp++;
522 }
wdenkbf9e3b32004-02-12 00:47:09 +0000523 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
524 flash_add_byte (info, &cword, (*(uchar *) cp));
525 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000526 return rc;
527 wp = cp;
528 }
529
wdenkbf9e3b32004-02-12 00:47:09 +0000530 /* handle the aligned part */
wdenk5653fc32004-02-08 22:55:38 +0000531#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +0000532 buffered_size = (info->portwidth / info->chipwidth);
533 buffered_size *= info->buffer_size;
534 while (cnt >= info->portwidth) {
535 i = buffered_size > cnt ? cnt : buffered_size;
536 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +0000537 return rc;
538 wp += i;
539 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +0000540 cnt -= i;
wdenk5653fc32004-02-08 22:55:38 +0000541 }
542#else
wdenkbf9e3b32004-02-12 00:47:09 +0000543 while (cnt >= info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000544 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000545 for (i = 0; i < info->portwidth; i++) {
546 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000547 }
wdenkbf9e3b32004-02-12 00:47:09 +0000548 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000549 return rc;
550 wp += info->portwidth;
551 cnt -= info->portwidth;
552 }
553#endif /* CFG_FLASH_USE_BUFFER_WRITE */
554 if (cnt == 0) {
555 return (0);
556 }
557
558 /*
559 * handle unaligned tail bytes
560 */
561 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000562 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
563 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000564 --cnt;
565 }
wdenkbf9e3b32004-02-12 00:47:09 +0000566 for (; i < info->portwidth; ++i, ++cp) {
567 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000568 }
569
wdenkbf9e3b32004-02-12 00:47:09 +0000570 /* special handling of 16 bit devices in 8 bit mode */
571 if (x8mode) {
572 info->portwidth = x8mode;;
573 }
574 return flash_write_cfiword (info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +0000575}
576
577/*-----------------------------------------------------------------------
578 */
579#ifdef CFG_FLASH_PROTECTION
580
wdenkbf9e3b32004-02-12 00:47:09 +0000581int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +0000582{
583 int retcode = 0;
584
wdenkbf9e3b32004-02-12 00:47:09 +0000585 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
586 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
587 if (prot)
588 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk5653fc32004-02-08 22:55:38 +0000589 else
wdenkbf9e3b32004-02-12 00:47:09 +0000590 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk5653fc32004-02-08 22:55:38 +0000591
wdenkbf9e3b32004-02-12 00:47:09 +0000592 if ((retcode =
593 flash_full_status_check (info, sector, info->erase_blk_tout,
594 prot ? "protect" : "unprotect")) == 0) {
wdenk5653fc32004-02-08 22:55:38 +0000595
596 info->protect[sector] = prot;
597 /* Intel's unprotect unprotects all locking */
wdenkbf9e3b32004-02-12 00:47:09 +0000598 if (prot == 0) {
wdenk5653fc32004-02-08 22:55:38 +0000599 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +0000600
601 for (i = 0; i < info->sector_count; i++) {
602 if (info->protect[i])
603 flash_real_protect (info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +0000604 }
605 }
606 }
607
608 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000609}
610
wdenk5653fc32004-02-08 22:55:38 +0000611/*-----------------------------------------------------------------------
612 * flash_read_user_serial - read the OneTimeProgramming cells
613 */
wdenkbf9e3b32004-02-12 00:47:09 +0000614void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
615 int len)
wdenk5653fc32004-02-08 22:55:38 +0000616{
wdenkbf9e3b32004-02-12 00:47:09 +0000617 uchar *src;
618 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +0000619
620 dst = buffer;
wdenkbf9e3b32004-02-12 00:47:09 +0000621 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
622 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
623 memcpy (dst, src + offset, len);
624 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
wdenk5653fc32004-02-08 22:55:38 +0000625}
wdenkbf9e3b32004-02-12 00:47:09 +0000626
wdenk5653fc32004-02-08 22:55:38 +0000627/*
628 * flash_read_factory_serial - read the device Id from the protection area
629 */
wdenkbf9e3b32004-02-12 00:47:09 +0000630void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
631 int len)
wdenk5653fc32004-02-08 22:55:38 +0000632{
wdenkbf9e3b32004-02-12 00:47:09 +0000633 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +0000634
wdenkbf9e3b32004-02-12 00:47:09 +0000635 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
636 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
637 memcpy (buffer, src + offset, len);
638 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
wdenk5653fc32004-02-08 22:55:38 +0000639}
640
641#endif /* CFG_FLASH_PROTECTION */
642
wdenkbf9e3b32004-02-12 00:47:09 +0000643/*
644 * flash_is_busy - check to see if the flash is busy
645 * This routine checks the status of the chip and returns true if the chip is busy
646 */
647static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000648{
649 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000650
651 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000652 case CFI_CMDSET_INTEL_STANDARD:
653 case CFI_CMDSET_INTEL_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000654 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk5653fc32004-02-08 22:55:38 +0000655 break;
656 case CFI_CMDSET_AMD_STANDARD:
657 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000658 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk5653fc32004-02-08 22:55:38 +0000659 break;
660 default:
661 retval = 0;
662 }
wdenkbf9e3b32004-02-12 00:47:09 +0000663 debug ("flash_is_busy: %d\n", retval);
wdenk5653fc32004-02-08 22:55:38 +0000664 return retval;
665}
wdenkbf9e3b32004-02-12 00:47:09 +0000666
wdenk5653fc32004-02-08 22:55:38 +0000667/*-----------------------------------------------------------------------
668 * wait for XSR.7 to be set. Time out with an error if it does not.
669 * This routine does not set the flash to read-array mode.
670 */
wdenkbf9e3b32004-02-12 00:47:09 +0000671static int flash_status_check (flash_info_t * info, flash_sect_t sector,
672 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000673{
674 ulong start;
675
676 /* Wait for command completion */
677 start = get_timer (0);
wdenkbf9e3b32004-02-12 00:47:09 +0000678 while (flash_is_busy (info, sector)) {
679 if (get_timer (start) > info->erase_blk_tout * CFG_HZ) {
680 printf ("Flash %s timeout at address %lx data %lx\n",
681 prompt, info->start[sector],
682 flash_read_long (info, sector, 0));
683 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000684 return ERR_TIMOUT;
685 }
686 }
687 return ERR_OK;
688}
wdenkbf9e3b32004-02-12 00:47:09 +0000689
wdenk5653fc32004-02-08 22:55:38 +0000690/*-----------------------------------------------------------------------
691 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
692 * This routine sets the flash to read-array mode.
693 */
wdenkbf9e3b32004-02-12 00:47:09 +0000694static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
695 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000696{
697 int retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000698
699 retcode = flash_status_check (info, sector, tout, prompt);
700 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000701 case CFI_CMDSET_INTEL_EXTENDED:
702 case CFI_CMDSET_INTEL_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +0000703 if ((retcode != ERR_OK)
704 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk5653fc32004-02-08 22:55:38 +0000705 retcode = ERR_INVAL;
wdenkbf9e3b32004-02-12 00:47:09 +0000706 printf ("Flash %s error at address %lx\n", prompt,
707 info->start[sector]);
708 if (flash_isset
709 (info, sector, 0,
710 FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
711 printf ("Command Sequence Error.\n");
712 } else if (flash_isset
713 (info, sector, 0, FLASH_STATUS_ECLBS)) {
714 printf ("Block Erase Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000715 retcode = ERR_NOT_ERASED;
wdenkbf9e3b32004-02-12 00:47:09 +0000716 } else if (flash_isset
717 (info, sector, 0, FLASH_STATUS_PSLBS)) {
718 printf ("Locking Error\n");
wdenk5653fc32004-02-08 22:55:38 +0000719 }
wdenkbf9e3b32004-02-12 00:47:09 +0000720 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
721 printf ("Block locked.\n");
722 retcode = ERR_PROTECTED;
723 }
724 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
725 printf ("Vpp Low Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000726 }
wdenkbf9e3b32004-02-12 00:47:09 +0000727 flash_write_cmd (info, sector, 0, FLASH_CMD_RESET);
wdenk5653fc32004-02-08 22:55:38 +0000728 break;
729 default:
730 break;
731 }
732 return retcode;
733}
wdenkbf9e3b32004-02-12 00:47:09 +0000734
wdenk5653fc32004-02-08 22:55:38 +0000735/*-----------------------------------------------------------------------
736 */
wdenkbf9e3b32004-02-12 00:47:09 +0000737static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk5653fc32004-02-08 22:55:38 +0000738{
wdenkbf9e3b32004-02-12 00:47:09 +0000739 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000740 case FLASH_CFI_8BIT:
741 cword->c = c;
742 break;
743 case FLASH_CFI_16BIT:
744 cword->w = (cword->w << 8) | c;
745 break;
746 case FLASH_CFI_32BIT:
747 cword->l = (cword->l << 8) | c;
748 break;
749 case FLASH_CFI_64BIT:
750 cword->ll = (cword->ll << 8) | c;
751 break;
752 }
753}
754
755
756/*-----------------------------------------------------------------------
757 * make a proper sized command based on the port and chip widths
758 */
wdenkbf9e3b32004-02-12 00:47:09 +0000759static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk5653fc32004-02-08 22:55:38 +0000760{
761 int i;
wdenkbf9e3b32004-02-12 00:47:09 +0000762
763#if defined(__LITTLE_ENDIAN)
764 ushort stmp;
765#endif
766 uchar *cp = (uchar *) cmdbuf;
767
768 for (i = 0; i < info->portwidth; i++)
769 *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd;
770#if defined(__LITTLE_ENDIAN)
771 if (info->portwidth == 2) {
772 stmp = *(ushort *) cmdbuf;
773 *(ushort *) cmdbuf = swab16 (stmp);
774 }
775#endif
wdenk5653fc32004-02-08 22:55:38 +0000776}
777
778/*
779 * Write a proper sized command to the correct address
780 */
wdenkbf9e3b32004-02-12 00:47:09 +0000781static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
782 uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000783{
784
785 volatile cfiptr_t addr;
786 cfiword_t cword;
wdenkbf9e3b32004-02-12 00:47:09 +0000787
788 addr.cp = flash_make_addr (info, sect, offset);
789 flash_make_cmd (info, cmd, &cword);
790 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000791 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000792 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
793 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk5653fc32004-02-08 22:55:38 +0000794 *addr.cp = cword.c;
795 break;
796 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000797 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
798 cmd, cword.w,
wdenk5653fc32004-02-08 22:55:38 +0000799 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
800 *addr.wp = cword.w;
801 break;
802 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000803 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
804 cmd, cword.l,
wdenk5653fc32004-02-08 22:55:38 +0000805 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
806 *addr.lp = cword.l;
807 break;
808 case FLASH_CFI_64BIT:
809#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +0000810 {
wdenk5653fc32004-02-08 22:55:38 +0000811 char str[20];
wdenkcd37d9e2004-02-10 00:03:41 +0000812
wdenkbf9e3b32004-02-12 00:47:09 +0000813 print_longlong (str, cword.ll);
814
815 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
816 addr.llp, cmd, str,
wdenk5653fc32004-02-08 22:55:38 +0000817 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
818 }
819#endif
820 *addr.llp = cword.ll;
821 break;
822 }
823}
824
wdenkbf9e3b32004-02-12 00:47:09 +0000825static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000826{
wdenkbf9e3b32004-02-12 00:47:09 +0000827 flash_write_cmd (info, sect, 0x555, 0xAA);
828 flash_write_cmd (info, sect, 0x2AA, 0x55);
wdenk5653fc32004-02-08 22:55:38 +0000829}
wdenkbf9e3b32004-02-12 00:47:09 +0000830
wdenk5653fc32004-02-08 22:55:38 +0000831/*-----------------------------------------------------------------------
832 */
wdenkbf9e3b32004-02-12 00:47:09 +0000833static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset,
834 uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000835{
836 cfiptr_t cptr;
837 cfiword_t cword;
838 int retval;
wdenk5653fc32004-02-08 22:55:38 +0000839
wdenkbf9e3b32004-02-12 00:47:09 +0000840 cptr.cp = flash_make_addr (info, sect, offset);
841 flash_make_cmd (info, cmd, &cword);
842
843 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
844 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000845 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000846 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk5653fc32004-02-08 22:55:38 +0000847 retval = (cptr.cp[0] == cword.c);
848 break;
849 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000850 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk5653fc32004-02-08 22:55:38 +0000851 retval = (cptr.wp[0] == cword.w);
852 break;
853 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000854 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk5653fc32004-02-08 22:55:38 +0000855 retval = (cptr.lp[0] == cword.l);
856 break;
857 case FLASH_CFI_64BIT:
wdenkcd37d9e2004-02-10 00:03:41 +0000858#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +0000859 {
wdenk5653fc32004-02-08 22:55:38 +0000860 char str1[20];
861 char str2[20];
wdenkbf9e3b32004-02-12 00:47:09 +0000862
863 print_longlong (str1, cptr.llp[0]);
864 print_longlong (str2, cword.ll);
865 debug ("is= %s %s\n", str1, str2);
wdenk5653fc32004-02-08 22:55:38 +0000866 }
867#endif
868 retval = (cptr.llp[0] == cword.ll);
869 break;
870 default:
871 retval = 0;
872 break;
873 }
874 return retval;
875}
wdenkbf9e3b32004-02-12 00:47:09 +0000876
wdenk5653fc32004-02-08 22:55:38 +0000877/*-----------------------------------------------------------------------
878 */
wdenkbf9e3b32004-02-12 00:47:09 +0000879static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset,
880 uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000881{
882 cfiptr_t cptr;
883 cfiword_t cword;
884 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000885
886 cptr.cp = flash_make_addr (info, sect, offset);
887 flash_make_cmd (info, cmd, &cword);
888 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000889 case FLASH_CFI_8BIT:
890 retval = ((cptr.cp[0] & cword.c) == cword.c);
891 break;
892 case FLASH_CFI_16BIT:
893 retval = ((cptr.wp[0] & cword.w) == cword.w);
894 break;
895 case FLASH_CFI_32BIT:
896 retval = ((cptr.lp[0] & cword.l) == cword.l);
897 break;
898 case FLASH_CFI_64BIT:
899 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenkbf9e3b32004-02-12 00:47:09 +0000900 break;
wdenk5653fc32004-02-08 22:55:38 +0000901 default:
902 retval = 0;
903 break;
904 }
905 return retval;
906}
907
908/*-----------------------------------------------------------------------
909 */
wdenkbf9e3b32004-02-12 00:47:09 +0000910static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset,
911 uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000912{
913 cfiptr_t cptr;
914 cfiword_t cword;
915 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000916
917 cptr.cp = flash_make_addr (info, sect, offset);
918 flash_make_cmd (info, cmd, &cword);
919 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000920 case FLASH_CFI_8BIT:
921 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
922 break;
923 case FLASH_CFI_16BIT:
924 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
925 break;
926 case FLASH_CFI_32BIT:
927 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
928 break;
929 case FLASH_CFI_64BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000930 retval = ((cptr.llp[0] & cword.ll) !=
931 (cptr.llp[0] & cword.ll));
wdenk5653fc32004-02-08 22:55:38 +0000932 break;
933 default:
934 retval = 0;
935 break;
936 }
937 return retval;
938}
939
940/*-----------------------------------------------------------------------
941 * detect if flash is compatible with the Common Flash Interface (CFI)
942 * http://www.jedec.org/download/search/jesd68.pdf
943 *
944*/
wdenkbf9e3b32004-02-12 00:47:09 +0000945static int flash_detect_cfi (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +0000946{
wdenkbf9e3b32004-02-12 00:47:09 +0000947 debug ("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +0000948
wdenkbf9e3b32004-02-12 00:47:09 +0000949 for (info->portwidth = FLASH_CFI_8BIT;
950 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
951 for (info->chipwidth = FLASH_CFI_BY8;
952 info->chipwidth <= info->portwidth;
953 info->chipwidth <<= 1) {
954 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
955 flash_write_cmd (info, 0, FLASH_OFFSET_CFI,
956 FLASH_CMD_CFI);
957 if (flash_isequal
958 (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
959 && flash_isequal (info, 0,
960 FLASH_OFFSET_CFI_RESP + 1, 'R')
961 && flash_isequal (info, 0,
962 FLASH_OFFSET_CFI_RESP + 2,
963 'Y')) {
964 info->interface =
965 flash_read_ushort (info, 0,
966 FLASH_OFFSET_INTERFACE);
967 debug ("device interface is %d\n",
968 info->interface);
969 debug ("found port %d chip %d ",
970 info->portwidth, info->chipwidth);
971 debug ("port %d bits chip %d bits\n",
972 info->
973 portwidth << CFI_FLASH_SHIFT_WIDTH,
974 info->
975 chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk5653fc32004-02-08 22:55:38 +0000976 return 1;
977 }
978 }
979 }
wdenkbf9e3b32004-02-12 00:47:09 +0000980 debug ("not found\n");
wdenk5653fc32004-02-08 22:55:38 +0000981 return 0;
982}
wdenkbf9e3b32004-02-12 00:47:09 +0000983
wdenk5653fc32004-02-08 22:55:38 +0000984/*
985 * The following code cannot be run from FLASH!
986 *
987 */
988static ulong flash_get_size (ulong base, int banknum)
989{
wdenkbf9e3b32004-02-12 00:47:09 +0000990 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +0000991 int i, j;
992 flash_sect_t sect_cnt;
993 unsigned long sector;
994 unsigned long tmp;
995 int size_ratio;
996 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +0000997 int erase_region_size;
998 int erase_region_count;
wdenk5653fc32004-02-08 22:55:38 +0000999
1000 info->start[0] = base;
1001
wdenkbf9e3b32004-02-12 00:47:09 +00001002 if (flash_detect_cfi (info)) {
1003 info->vendor =
1004 flash_read_ushort (info, 0,
1005 FLASH_OFFSET_PRIMARY_VENDOR);
1006#ifdef DEBUG
1007 flash_printqry (info, 0);
1008#endif
1009 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001010 case CFI_CMDSET_INTEL_STANDARD:
1011 case CFI_CMDSET_INTEL_EXTENDED:
1012 default:
1013 info->cmd_reset = FLASH_CMD_RESET;
1014 break;
1015 case CFI_CMDSET_AMD_STANDARD:
1016 case CFI_CMDSET_AMD_EXTENDED:
1017 info->cmd_reset = AMD_CMD_RESET;
1018 break;
1019 }
wdenkcd37d9e2004-02-10 00:03:41 +00001020
wdenkbf9e3b32004-02-12 00:47:09 +00001021 debug ("manufacturer is %d\n", info->vendor);
wdenk5653fc32004-02-08 22:55:38 +00001022 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00001023 /* if the chip is x8/x16 reduce the ratio by half */
1024 if ((info->interface == FLASH_CFI_X8X16)
1025 && (info->chipwidth == FLASH_CFI_BY8)) {
1026 size_ratio >>= 1;
1027 }
1028 num_erase_regions =
1029 flash_read_uchar (info,
1030 FLASH_OFFSET_NUM_ERASE_REGIONS);
1031 debug ("size_ratio %d port %d bits chip %d bits\n",
1032 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1033 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1034 debug ("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00001035 sect_cnt = 0;
1036 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00001037 for (i = 0; i < num_erase_regions; i++) {
1038 if (i > NUM_ERASE_REGIONS) {
1039 printf ("%d erase regions found, only %d used\n", num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00001040 break;
1041 }
wdenkbf9e3b32004-02-12 00:47:09 +00001042 tmp = flash_read_long (info, 0,
1043 FLASH_OFFSET_ERASE_REGIONS +
1044 i * 4);
1045 erase_region_size =
1046 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk5653fc32004-02-08 22:55:38 +00001047 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00001048 erase_region_count = (tmp & 0xffff) + 1;
1049 printf ("erase_region_count = %d erase_region_size = %d\n", erase_region_count, erase_region_size);
1050 for (j = 0; j < erase_region_count; j++) {
wdenk5653fc32004-02-08 22:55:38 +00001051 info->start[sect_cnt] = sector;
1052 sector += (erase_region_size * size_ratio);
wdenkbf9e3b32004-02-12 00:47:09 +00001053 info->protect[sect_cnt] =
1054 flash_isset (info, sect_cnt,
1055 FLASH_OFFSET_PROTECT,
1056 FLASH_STATUS_PROTECT);
wdenk5653fc32004-02-08 22:55:38 +00001057 sect_cnt++;
1058 }
1059 }
1060
1061 info->sector_count = sect_cnt;
1062 /* multiply the size by the number of chips */
wdenkbf9e3b32004-02-12 00:47:09 +00001063 info->size =
1064 (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) *
1065 size_ratio;
1066 info->buffer_size =
1067 (1 <<
1068 flash_read_ushort (info, 0,
1069 FLASH_OFFSET_BUFFER_SIZE));
1070 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
1071 info->erase_blk_tout =
1072 (tmp *
1073 (1 <<
1074 flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
1075 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
1076 info->buffer_write_tout =
1077 (tmp *
1078 (1 <<
1079 flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
1080 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT);
1081 info->write_tout =
1082 (tmp *
1083 (1 <<
1084 flash_read_uchar (info,
1085 FLASH_OFFSET_WMAX_TOUT))) / 1000;
wdenk5653fc32004-02-08 22:55:38 +00001086 info->flash_id = FLASH_MAN_CFI;
1087 }
1088
wdenkbf9e3b32004-02-12 00:47:09 +00001089 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
1090 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00001091}
1092
1093
1094/*-----------------------------------------------------------------------
1095 */
wdenkbf9e3b32004-02-12 00:47:09 +00001096static int flash_write_cfiword (flash_info_t * info, ulong dest,
1097 cfiword_t cword)
wdenk5653fc32004-02-08 22:55:38 +00001098{
1099
1100 cfiptr_t ctladdr;
1101 cfiptr_t cptr;
1102 int flag;
1103
wdenkbf9e3b32004-02-12 00:47:09 +00001104 ctladdr.cp = flash_make_addr (info, 0, 0);
1105 cptr.cp = (uchar *) dest;
wdenk5653fc32004-02-08 22:55:38 +00001106
1107
1108 /* Check if Flash is (sufficiently) erased */
wdenkbf9e3b32004-02-12 00:47:09 +00001109 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001110 case FLASH_CFI_8BIT:
1111 flag = ((cptr.cp[0] & cword.c) == cword.c);
1112 break;
1113 case FLASH_CFI_16BIT:
1114 flag = ((cptr.wp[0] & cword.w) == cword.w);
1115 break;
1116 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001117 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk5653fc32004-02-08 22:55:38 +00001118 break;
1119 case FLASH_CFI_64BIT:
1120 flag = ((cptr.lp[0] & cword.ll) == cword.ll);
1121 break;
1122 default:
1123 return 2;
1124 }
wdenkbf9e3b32004-02-12 00:47:09 +00001125 if (!flag)
wdenk5653fc32004-02-08 22:55:38 +00001126 return 2;
1127
1128 /* Disable interrupts which might cause a timeout here */
wdenkbf9e3b32004-02-12 00:47:09 +00001129 flag = disable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001130
wdenkbf9e3b32004-02-12 00:47:09 +00001131 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001132 case CFI_CMDSET_INTEL_EXTENDED:
1133 case CFI_CMDSET_INTEL_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001134 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1135 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001136 break;
1137 case CFI_CMDSET_AMD_EXTENDED:
1138 case CFI_CMDSET_AMD_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001139 flash_unlock_seq (info, 0);
1140 flash_write_cmd (info, 0, 0x555, AMD_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001141 break;
1142 }
1143
wdenkbf9e3b32004-02-12 00:47:09 +00001144 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001145 case FLASH_CFI_8BIT:
1146 cptr.cp[0] = cword.c;
1147 break;
1148 case FLASH_CFI_16BIT:
1149 cptr.wp[0] = cword.w;
1150 break;
1151 case FLASH_CFI_32BIT:
1152 cptr.lp[0] = cword.l;
1153 break;
1154 case FLASH_CFI_64BIT:
1155 cptr.llp[0] = cword.ll;
1156 break;
1157 }
1158
1159 /* re-enable interrupts if necessary */
wdenkbf9e3b32004-02-12 00:47:09 +00001160 if (flag)
1161 enable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001162
wdenkbf9e3b32004-02-12 00:47:09 +00001163 return flash_full_status_check (info, 0, info->write_tout, "write");
wdenk5653fc32004-02-08 22:55:38 +00001164}
1165
1166#ifdef CFG_FLASH_USE_BUFFER_WRITE
1167
1168/* loop through the sectors from the highest address
1169 * when the passed address is greater or equal to the sector address
1170 * we have a match
1171 */
wdenkbf9e3b32004-02-12 00:47:09 +00001172static flash_sect_t find_sector (flash_info_t * info, ulong addr)
wdenk5653fc32004-02-08 22:55:38 +00001173{
1174 flash_sect_t sector;
wdenkbf9e3b32004-02-12 00:47:09 +00001175
1176 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1177 if (addr >= info->start[sector])
wdenk5653fc32004-02-08 22:55:38 +00001178 break;
1179 }
1180 return sector;
1181}
1182
wdenkbf9e3b32004-02-12 00:47:09 +00001183static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1184 int len)
wdenk5653fc32004-02-08 22:55:38 +00001185{
1186 flash_sect_t sector;
1187 int cnt;
1188 int retcode;
1189 volatile cfiptr_t src;
1190 volatile cfiptr_t dst;
1191
1192 src.cp = cp;
wdenkbf9e3b32004-02-12 00:47:09 +00001193 dst.cp = (uchar *) dest;
1194 sector = find_sector (info, dest);
1195 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1196 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1197 if ((retcode =
1198 flash_status_check (info, sector, info->buffer_write_tout,
1199 "write to buffer")) == ERR_OK) {
1200 /* reduce the number of loops by the width of the port */
1201 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001202 case FLASH_CFI_8BIT:
1203 cnt = len;
1204 break;
1205 case FLASH_CFI_16BIT:
1206 cnt = len >> 1;
1207 break;
1208 case FLASH_CFI_32BIT:
1209 cnt = len >> 2;
1210 break;
1211 case FLASH_CFI_64BIT:
1212 cnt = len >> 3;
1213 break;
1214 default:
1215 return ERR_INVAL;
1216 break;
1217 }
wdenkbf9e3b32004-02-12 00:47:09 +00001218 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1219 while (cnt-- > 0) {
1220 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001221 case FLASH_CFI_8BIT:
1222 *dst.cp++ = *src.cp++;
1223 break;
1224 case FLASH_CFI_16BIT:
1225 *dst.wp++ = *src.wp++;
1226 break;
1227 case FLASH_CFI_32BIT:
1228 *dst.lp++ = *src.lp++;
1229 break;
1230 case FLASH_CFI_64BIT:
1231 *dst.llp++ = *src.llp++;
1232 break;
1233 default:
1234 return ERR_INVAL;
1235 break;
1236 }
1237 }
wdenkbf9e3b32004-02-12 00:47:09 +00001238 flash_write_cmd (info, sector, 0,
1239 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1240 retcode =
1241 flash_full_status_check (info, sector,
1242 info->buffer_write_tout,
1243 "buffer write");
wdenk5653fc32004-02-08 22:55:38 +00001244 }
wdenkbf9e3b32004-02-12 00:47:09 +00001245 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
wdenk5653fc32004-02-08 22:55:38 +00001246 return retcode;
1247}
1248#endif /* CFG_USE_FLASH_BUFFER_WRITE */
1249#endif /* CFG_FLASH_CFI */