blob: df6cb73fa35187f34808a0a28cfaaf8c42fb35df [file] [log] [blame]
wdenk5653fc32004-02-08 22:55:38 +00001/*
wdenkbf9e3b32004-02-12 00:47:09 +00002 * (C) Copyright 2002-2004
wdenk5653fc32004-02-08 22:55:38 +00003 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4 *
5 * Copyright (C) 2003 Arabella Software Ltd.
6 * Yuli Barcohen <yuli@arabellasw.com>
7 * Modified to work with AMD flashes
8 *
wdenkbf9e3b32004-02-12 00:47:09 +00009 * Copyright (C) 2004
10 * Ed Okerson
11 * Modified to work with little-endian systems.
12 *
wdenk5653fc32004-02-08 22:55:38 +000013 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 *
31 * History
32 * 01/20/2004 - combined variants of original driver.
wdenkbf9e3b32004-02-12 00:47:09 +000033 * 01/22/2004 - Write performance enhancements for parallel chips (Tolunay)
34 * 01/23/2004 - Support for x8/x16 chips (Rune Raknerud)
35 * 01/27/2004 - Little endian support Ed Okerson
wdenk5653fc32004-02-08 22:55:38 +000036 *
37 * Tested Architectures
wdenkbf9e3b32004-02-12 00:47:09 +000038 * Port Width Chip Width # of banks Flash Chip Board
wdenk2d1a5372004-02-23 19:30:57 +000039 * 32 16 1 28F128J3 seranoa/eagle
40 * 64 16 1 28F128J3 seranoa/falcon
wdenkcd37d9e2004-02-10 00:03:41 +000041 *
wdenk5653fc32004-02-08 22:55:38 +000042 */
43
44/* The DEBUG define must be before common to enable debugging */
wdenk2d1a5372004-02-23 19:30:57 +000045/* #define DEBUG */
46
wdenk5653fc32004-02-08 22:55:38 +000047#include <common.h>
48#include <asm/processor.h>
wdenk028ab6b2004-02-23 23:54:43 +000049#include <linux/byteorder/swab.h>
wdenkbf9e3b32004-02-12 00:47:09 +000050#ifdef CFG_FLASH_CFI_DRIVER
wdenk028ab6b2004-02-23 23:54:43 +000051
wdenk5653fc32004-02-08 22:55:38 +000052/*
53 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
54 * The width of the port and the width of the chips are determined at initialization.
55 * These widths are used to calculate the address for access CFI data structures.
56 * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
57 *
58 * References
59 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
60 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
61 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
62 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
63 *
64 * TODO
65 *
66 * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
67 * Table (ALT) to determine if protection is available
68 *
69 * Add support for other command sets Use the PRI and ALT to determine command set
70 * Verify erase and program timeouts.
71 */
72
wdenkbf9e3b32004-02-12 00:47:09 +000073#ifndef CFG_FLASH_BANKS_LIST
74#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
75#endif
76
wdenk5653fc32004-02-08 22:55:38 +000077#define FLASH_CMD_CFI 0x98
78#define FLASH_CMD_READ_ID 0x90
79#define FLASH_CMD_RESET 0xff
80#define FLASH_CMD_BLOCK_ERASE 0x20
81#define FLASH_CMD_ERASE_CONFIRM 0xD0
82#define FLASH_CMD_WRITE 0x40
83#define FLASH_CMD_PROTECT 0x60
84#define FLASH_CMD_PROTECT_SET 0x01
85#define FLASH_CMD_PROTECT_CLEAR 0xD0
86#define FLASH_CMD_CLEAR_STATUS 0x50
wdenkbf9e3b32004-02-12 00:47:09 +000087#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
88#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk5653fc32004-02-08 22:55:38 +000089
90#define FLASH_STATUS_DONE 0x80
91#define FLASH_STATUS_ESS 0x40
92#define FLASH_STATUS_ECLBS 0x20
93#define FLASH_STATUS_PSLBS 0x10
94#define FLASH_STATUS_VPENS 0x08
95#define FLASH_STATUS_PSS 0x04
96#define FLASH_STATUS_DPS 0x02
97#define FLASH_STATUS_R 0x01
98#define FLASH_STATUS_PROTECT 0x01
99
100#define AMD_CMD_RESET 0xF0
101#define AMD_CMD_WRITE 0xA0
102#define AMD_CMD_ERASE_START 0x80
103#define AMD_CMD_ERASE_SECTOR 0x30
wdenk855a4962004-03-14 18:23:55 +0000104#define AMD_CMD_UNLOCK_START 0xAA
105#define AMD_CMD_UNLOCK_ACK 0x55
wdenk5653fc32004-02-08 22:55:38 +0000106
107#define AMD_STATUS_TOGGLE 0x40
108#define AMD_STATUS_ERROR 0x20
wdenk855a4962004-03-14 18:23:55 +0000109#define AMD_ADDR_ERASE_START 0x555
110#define AMD_ADDR_START 0x555
111#define AMD_ADDR_ACK 0x2AA
wdenk5653fc32004-02-08 22:55:38 +0000112
113#define FLASH_OFFSET_CFI 0x55
114#define FLASH_OFFSET_CFI_RESP 0x10
wdenkbf9e3b32004-02-12 00:47:09 +0000115#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
wdenk5653fc32004-02-08 22:55:38 +0000116#define FLASH_OFFSET_WTOUT 0x1F
wdenkbf9e3b32004-02-12 00:47:09 +0000117#define FLASH_OFFSET_WBTOUT 0x20
wdenk5653fc32004-02-08 22:55:38 +0000118#define FLASH_OFFSET_ETOUT 0x21
wdenkbf9e3b32004-02-12 00:47:09 +0000119#define FLASH_OFFSET_CETOUT 0x22
wdenk5653fc32004-02-08 22:55:38 +0000120#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenkbf9e3b32004-02-12 00:47:09 +0000121#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk5653fc32004-02-08 22:55:38 +0000122#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenkbf9e3b32004-02-12 00:47:09 +0000123#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk5653fc32004-02-08 22:55:38 +0000124#define FLASH_OFFSET_SIZE 0x27
wdenkbf9e3b32004-02-12 00:47:09 +0000125#define FLASH_OFFSET_INTERFACE 0x28
126#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk5653fc32004-02-08 22:55:38 +0000127#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
128#define FLASH_OFFSET_ERASE_REGIONS 0x2D
129#define FLASH_OFFSET_PROTECT 0x02
wdenkbf9e3b32004-02-12 00:47:09 +0000130#define FLASH_OFFSET_USER_PROTECTION 0x85
131#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk5653fc32004-02-08 22:55:38 +0000132
133
134#define FLASH_MAN_CFI 0x01000000
135
wdenkbf9e3b32004-02-12 00:47:09 +0000136#define CFI_CMDSET_NONE 0
wdenk5653fc32004-02-08 22:55:38 +0000137#define CFI_CMDSET_INTEL_EXTENDED 1
wdenkbf9e3b32004-02-12 00:47:09 +0000138#define CFI_CMDSET_AMD_STANDARD 2
wdenk5653fc32004-02-08 22:55:38 +0000139#define CFI_CMDSET_INTEL_STANDARD 3
wdenkbf9e3b32004-02-12 00:47:09 +0000140#define CFI_CMDSET_AMD_EXTENDED 4
wdenk5653fc32004-02-08 22:55:38 +0000141#define CFI_CMDSET_MITSU_STANDARD 256
142#define CFI_CMDSET_MITSU_EXTENDED 257
wdenkbf9e3b32004-02-12 00:47:09 +0000143#define CFI_CMDSET_SST 258
wdenk5653fc32004-02-08 22:55:38 +0000144
145
146typedef union {
147 unsigned char c;
148 unsigned short w;
149 unsigned long l;
150 unsigned long long ll;
151} cfiword_t;
152
153typedef union {
wdenkbf9e3b32004-02-12 00:47:09 +0000154 volatile unsigned char *cp;
wdenk5653fc32004-02-08 22:55:38 +0000155 volatile unsigned short *wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000156 volatile unsigned long *lp;
wdenk5653fc32004-02-08 22:55:38 +0000157 volatile unsigned long long *llp;
158} cfiptr_t;
159
160#define NUM_ERASE_REGIONS 4
161
162static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
163
wdenkbf9e3b32004-02-12 00:47:09 +0000164flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk5653fc32004-02-08 22:55:38 +0000165
166/*-----------------------------------------------------------------------
167 * Functions
168 */
169
170typedef unsigned long flash_sect_t;
171
wdenkbf9e3b32004-02-12 00:47:09 +0000172static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
173static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenk028ab6b2004-02-23 23:54:43 +0000174static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenkbf9e3b32004-02-12 00:47:09 +0000175static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenk028ab6b2004-02-23 23:54:43 +0000176static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
177static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
178static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenkbf9e3b32004-02-12 00:47:09 +0000179static int flash_detect_cfi (flash_info_t * info);
wdenk5653fc32004-02-08 22:55:38 +0000180static ulong flash_get_size (ulong base, int banknum);
wdenk028ab6b2004-02-23 23:54:43 +0000181static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenkbf9e3b32004-02-12 00:47:09 +0000182static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
183 ulong tout, char *prompt);
wdenk5653fc32004-02-08 22:55:38 +0000184#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenk028ab6b2004-02-23 23:54:43 +0000185static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk5653fc32004-02-08 22:55:38 +0000186#endif
187
wdenk5653fc32004-02-08 22:55:38 +0000188/*-----------------------------------------------------------------------
189 * create an address based on the offset and the port width
190 */
wdenk028ab6b2004-02-23 23:54:43 +0000191inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000192{
wdenkbf9e3b32004-02-12 00:47:09 +0000193 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
wdenk5653fc32004-02-08 22:55:38 +0000194}
wdenkbf9e3b32004-02-12 00:47:09 +0000195
196#ifdef DEBUG
197/*-----------------------------------------------------------------------
198 * Debug support
199 */
200void print_longlong (char *str, unsigned long long data)
201{
202 int i;
203 char *cp;
204
205 cp = (unsigned char *) &data;
206 for (i = 0; i < 8; i++)
207 sprintf (&str[i * 2], "%2.2x", *cp++);
208}
209static void flash_printqry (flash_info_t * info, flash_sect_t sect)
210{
211 cfiptr_t cptr;
212 int x, y;
213
214 for (x = 0; x < 0x40; x += 16 / info->portwidth) {
215 cptr.cp =
216 flash_make_addr (info, sect,
217 x + FLASH_OFFSET_CFI_RESP);
218 debug ("%p : ", cptr.cp);
219 for (y = 0; y < 16; y++) {
220 debug ("%2.2x ", cptr.cp[y]);
221 }
222 debug (" ");
223 for (y = 0; y < 16; y++) {
224 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
225 debug ("%c", cptr.cp[y]);
226 } else {
227 debug (".");
228 }
229 }
230 debug ("\n");
231 }
232}
wdenkbf9e3b32004-02-12 00:47:09 +0000233#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{
wdenkbf9e3b32004-02-12 00:47:09 +0000288 uchar *addr;
289 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000290
wdenkbf9e3b32004-02-12 00:47:09 +0000291#ifdef DEBUG
292 int x;
293#endif
294 addr = flash_make_addr (info, sect, offset);
295
296#ifdef DEBUG
297 debug ("long addr is at %p info->portwidth = %d\n", addr,
298 info->portwidth);
299 for (x = 0; x < 4 * info->portwidth; x++) {
300 debug ("addr[%x] = 0x%x\n", x, addr[x]);
301 }
302#endif
303#if defined(__LITTLE_ENDIAN)
304 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenk028ab6b2004-02-23 23:54:43 +0000305 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenkbf9e3b32004-02-12 00:47:09 +0000306#else
307 retval = (addr[(2 * info->portwidth) - 1] << 24) |
308 (addr[(info->portwidth) - 1] << 16) |
309 (addr[(4 * info->portwidth) - 1] << 8) |
310 addr[(3 * info->portwidth) - 1];
311#endif
312 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000313}
314
315/*-----------------------------------------------------------------------
316 */
317unsigned long flash_init (void)
318{
319 unsigned long size = 0;
320 int i;
321
322 /* Init: no FLASHes known */
wdenkbf9e3b32004-02-12 00:47:09 +0000323 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk5653fc32004-02-08 22:55:38 +0000324 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenkbf9e3b32004-02-12 00:47:09 +0000325 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk5653fc32004-02-08 22:55:38 +0000326 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
wdenk028ab6b2004-02-23 23:54:43 +0000327 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
328 i, flash_info[i].size, flash_info[i].size << 20);
wdenk5653fc32004-02-08 22:55:38 +0000329 }
330 }
331
332 /* Monitor protection ON by default */
333#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenkbf9e3b32004-02-12 00:47:09 +0000334 flash_protect (FLAG_PROTECT_SET,
335 CFG_MONITOR_BASE,
336 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
337 &flash_info[0]);
wdenk5653fc32004-02-08 22:55:38 +0000338#endif
339
340 return (size);
341}
342
343/*-----------------------------------------------------------------------
344 */
wdenkbf9e3b32004-02-12 00:47:09 +0000345int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +0000346{
347 int rcode = 0;
348 int prot;
349 flash_sect_t sect;
350
wdenkbf9e3b32004-02-12 00:47:09 +0000351 if (info->flash_id != FLASH_MAN_CFI) {
wdenk5653fc32004-02-08 22:55:38 +0000352 printf ("Can't erase unknown flash type - aborted\n");
353 return 1;
354 }
355 if ((s_first < 0) || (s_first > s_last)) {
356 printf ("- no sectors to erase\n");
357 return 1;
358 }
359
360 prot = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000361 for (sect = s_first; sect <= s_last; ++sect) {
wdenk5653fc32004-02-08 22:55:38 +0000362 if (info->protect[sect]) {
363 prot++;
364 }
365 }
366 if (prot) {
wdenkbf9e3b32004-02-12 00:47:09 +0000367 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk5653fc32004-02-08 22:55:38 +0000368 } else {
369 printf ("\n");
370 }
371
372
wdenkbf9e3b32004-02-12 00:47:09 +0000373 for (sect = s_first; sect <= s_last; sect++) {
wdenk5653fc32004-02-08 22:55:38 +0000374 if (info->protect[sect] == 0) { /* not protected */
wdenkbf9e3b32004-02-12 00:47:09 +0000375 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000376 case CFI_CMDSET_INTEL_STANDARD:
377 case CFI_CMDSET_INTEL_EXTENDED:
wdenk028ab6b2004-02-23 23:54:43 +0000378 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
379 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
380 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +0000381 break;
382 case CFI_CMDSET_AMD_STANDARD:
383 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000384 flash_unlock_seq (info, sect);
wdenk855a4962004-03-14 18:23:55 +0000385 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
386 AMD_CMD_ERASE_START);
wdenkbf9e3b32004-02-12 00:47:09 +0000387 flash_unlock_seq (info, sect);
wdenk028ab6b2004-02-23 23:54:43 +0000388 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk5653fc32004-02-08 22:55:38 +0000389 break;
390 default:
wdenkbf9e3b32004-02-12 00:47:09 +0000391 debug ("Unkown flash vendor %d\n",
392 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +0000393 break;
394 }
395
wdenkbf9e3b32004-02-12 00:47:09 +0000396 if (flash_full_status_check
397 (info, sect, info->erase_blk_tout, "erase")) {
wdenk5653fc32004-02-08 22:55:38 +0000398 rcode = 1;
399 } else
wdenkbf9e3b32004-02-12 00:47:09 +0000400 printf (".");
wdenk5653fc32004-02-08 22:55:38 +0000401 }
402 }
403 printf (" done\n");
404 return rcode;
405}
406
407/*-----------------------------------------------------------------------
408 */
wdenkbf9e3b32004-02-12 00:47:09 +0000409void flash_print_info (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +0000410{
411 int i;
412
413 if (info->flash_id != FLASH_MAN_CFI) {
414 printf ("missing or unknown FLASH type\n");
415 return;
416 }
417
wdenkbf9e3b32004-02-12 00:47:09 +0000418 printf ("CFI conformant FLASH (%d x %d)",
419 (info->portwidth << 3), (info->chipwidth << 3));
wdenk5653fc32004-02-08 22:55:38 +0000420 printf (" Size: %ld MB in %d Sectors\n",
421 info->size >> 20, info->sector_count);
wdenk028ab6b2004-02-23 23:54:43 +0000422 printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
423 info->erase_blk_tout,
424 info->write_tout,
425 info->buffer_write_tout,
426 info->buffer_size);
wdenk5653fc32004-02-08 22:55:38 +0000427
428 printf (" Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +0000429 for (i = 0; i < info->sector_count; ++i) {
wdenk5653fc32004-02-08 22:55:38 +0000430#ifdef CFG_FLASH_EMPTY_INFO
431 int k;
432 int size;
433 int erased;
434 volatile unsigned long *flash;
435
436 /*
437 * Check if whole sector is erased
438 */
wdenkbf9e3b32004-02-12 00:47:09 +0000439 if (i != (info->sector_count - 1))
440 size = info->start[i + 1] - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000441 else
wdenkbf9e3b32004-02-12 00:47:09 +0000442 size = info->start[0] + info->size - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000443 erased = 1;
wdenkbf9e3b32004-02-12 00:47:09 +0000444 flash = (volatile unsigned long *) info->start[i];
445 size = size >> 2; /* divide by 4 for longword access */
446 for (k = 0; k < size; k++) {
447 if (*flash++ != 0xffffffff) {
448 erased = 0;
449 break;
450 }
451 }
wdenk5653fc32004-02-08 22:55:38 +0000452
453 if ((i % 5) == 0)
454 printf ("\n");
455 /* print empty and read-only info */
456 printf (" %08lX%s%s",
457 info->start[i],
458 erased ? " E" : " ",
459 info->protect[i] ? "RO " : " ");
460#else
461 if ((i % 5) == 0)
462 printf ("\n ");
463 printf (" %08lX%s",
wdenkbf9e3b32004-02-12 00:47:09 +0000464 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenk5653fc32004-02-08 22:55:38 +0000465#endif
466 }
467 printf ("\n");
468 return;
469}
470
471/*-----------------------------------------------------------------------
472 * Copy memory to flash, returns:
473 * 0 - OK
474 * 1 - write timeout
475 * 2 - Flash not erased
476 */
wdenkbf9e3b32004-02-12 00:47:09 +0000477int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +0000478{
479 ulong wp;
480 ulong cp;
481 int aln;
482 cfiword_t cword;
483 int i, rc;
484
wdenkbf9e3b32004-02-12 00:47:09 +0000485#ifdef CFG_FLASH_USE_BUFFER_WRITE
486 int buffered_size;
487#endif
wdenkbf9e3b32004-02-12 00:47:09 +0000488 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +0000489 /* get lower aligned address */
490 wp = (addr & ~(info->portwidth - 1));
491
492 /* handle unaligned start */
wdenkbf9e3b32004-02-12 00:47:09 +0000493 if ((aln = addr - wp) != 0) {
wdenk5653fc32004-02-08 22:55:38 +0000494 cword.l = 0;
495 cp = wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000496 for (i = 0; i < aln; ++i, ++cp)
497 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000498
wdenkbf9e3b32004-02-12 00:47:09 +0000499 for (; (i < info->portwidth) && (cnt > 0); i++) {
500 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000501 cnt--;
502 cp++;
503 }
wdenkbf9e3b32004-02-12 00:47:09 +0000504 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
505 flash_add_byte (info, &cword, (*(uchar *) cp));
506 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000507 return rc;
508 wp = cp;
509 }
510
wdenkbf9e3b32004-02-12 00:47:09 +0000511 /* handle the aligned part */
wdenk5653fc32004-02-08 22:55:38 +0000512#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +0000513 buffered_size = (info->portwidth / info->chipwidth);
514 buffered_size *= info->buffer_size;
515 while (cnt >= info->portwidth) {
516 i = buffered_size > cnt ? cnt : buffered_size;
517 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +0000518 return rc;
519 wp += i;
520 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +0000521 cnt -= i;
wdenk5653fc32004-02-08 22:55:38 +0000522 }
523#else
wdenkbf9e3b32004-02-12 00:47:09 +0000524 while (cnt >= info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000525 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000526 for (i = 0; i < info->portwidth; i++) {
527 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000528 }
wdenkbf9e3b32004-02-12 00:47:09 +0000529 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000530 return rc;
531 wp += info->portwidth;
532 cnt -= info->portwidth;
533 }
534#endif /* CFG_FLASH_USE_BUFFER_WRITE */
535 if (cnt == 0) {
536 return (0);
537 }
538
539 /*
540 * handle unaligned tail bytes
541 */
542 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000543 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
544 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000545 --cnt;
546 }
wdenkbf9e3b32004-02-12 00:47:09 +0000547 for (; i < info->portwidth; ++i, ++cp) {
548 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000549 }
550
wdenkbf9e3b32004-02-12 00:47:09 +0000551 return flash_write_cfiword (info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +0000552}
553
554/*-----------------------------------------------------------------------
555 */
556#ifdef CFG_FLASH_PROTECTION
557
wdenkbf9e3b32004-02-12 00:47:09 +0000558int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +0000559{
560 int retcode = 0;
561
wdenkbf9e3b32004-02-12 00:47:09 +0000562 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
563 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
564 if (prot)
565 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk5653fc32004-02-08 22:55:38 +0000566 else
wdenkbf9e3b32004-02-12 00:47:09 +0000567 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk5653fc32004-02-08 22:55:38 +0000568
wdenkbf9e3b32004-02-12 00:47:09 +0000569 if ((retcode =
570 flash_full_status_check (info, sector, info->erase_blk_tout,
571 prot ? "protect" : "unprotect")) == 0) {
wdenk5653fc32004-02-08 22:55:38 +0000572
573 info->protect[sector] = prot;
574 /* Intel's unprotect unprotects all locking */
wdenkbf9e3b32004-02-12 00:47:09 +0000575 if (prot == 0) {
wdenk5653fc32004-02-08 22:55:38 +0000576 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +0000577
578 for (i = 0; i < info->sector_count; i++) {
579 if (info->protect[i])
580 flash_real_protect (info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +0000581 }
582 }
583 }
wdenk5653fc32004-02-08 22:55:38 +0000584 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000585}
586
wdenk5653fc32004-02-08 22:55:38 +0000587/*-----------------------------------------------------------------------
588 * flash_read_user_serial - read the OneTimeProgramming cells
589 */
wdenkbf9e3b32004-02-12 00:47:09 +0000590void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
591 int len)
wdenk5653fc32004-02-08 22:55:38 +0000592{
wdenkbf9e3b32004-02-12 00:47:09 +0000593 uchar *src;
594 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +0000595
596 dst = buffer;
wdenkbf9e3b32004-02-12 00:47:09 +0000597 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
598 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
599 memcpy (dst, src + offset, len);
600 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
wdenk5653fc32004-02-08 22:55:38 +0000601}
wdenkbf9e3b32004-02-12 00:47:09 +0000602
wdenk5653fc32004-02-08 22:55:38 +0000603/*
604 * flash_read_factory_serial - read the device Id from the protection area
605 */
wdenkbf9e3b32004-02-12 00:47:09 +0000606void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
607 int len)
wdenk5653fc32004-02-08 22:55:38 +0000608{
wdenkbf9e3b32004-02-12 00:47:09 +0000609 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +0000610
wdenkbf9e3b32004-02-12 00:47:09 +0000611 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
612 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
613 memcpy (buffer, src + offset, len);
614 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
wdenk5653fc32004-02-08 22:55:38 +0000615}
616
617#endif /* CFG_FLASH_PROTECTION */
618
wdenkbf9e3b32004-02-12 00:47:09 +0000619/*
620 * flash_is_busy - check to see if the flash is busy
621 * This routine checks the status of the chip and returns true if the chip is busy
622 */
623static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000624{
625 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000626
627 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000628 case CFI_CMDSET_INTEL_STANDARD:
629 case CFI_CMDSET_INTEL_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000630 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk5653fc32004-02-08 22:55:38 +0000631 break;
632 case CFI_CMDSET_AMD_STANDARD:
633 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000634 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk5653fc32004-02-08 22:55:38 +0000635 break;
636 default:
637 retval = 0;
638 }
wdenkbf9e3b32004-02-12 00:47:09 +0000639 debug ("flash_is_busy: %d\n", retval);
wdenk5653fc32004-02-08 22:55:38 +0000640 return retval;
641}
wdenkbf9e3b32004-02-12 00:47:09 +0000642
wdenk5653fc32004-02-08 22:55:38 +0000643/*-----------------------------------------------------------------------
644 * wait for XSR.7 to be set. Time out with an error if it does not.
645 * This routine does not set the flash to read-array mode.
646 */
wdenkbf9e3b32004-02-12 00:47:09 +0000647static int flash_status_check (flash_info_t * info, flash_sect_t sector,
648 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000649{
650 ulong start;
651
652 /* Wait for command completion */
653 start = get_timer (0);
wdenkbf9e3b32004-02-12 00:47:09 +0000654 while (flash_is_busy (info, sector)) {
655 if (get_timer (start) > info->erase_blk_tout * CFG_HZ) {
656 printf ("Flash %s timeout at address %lx data %lx\n",
657 prompt, info->start[sector],
658 flash_read_long (info, sector, 0));
659 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000660 return ERR_TIMOUT;
661 }
662 }
663 return ERR_OK;
664}
wdenkbf9e3b32004-02-12 00:47:09 +0000665
wdenk5653fc32004-02-08 22:55:38 +0000666/*-----------------------------------------------------------------------
667 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
668 * This routine sets the flash to read-array mode.
669 */
wdenkbf9e3b32004-02-12 00:47:09 +0000670static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
671 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000672{
673 int retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000674
675 retcode = flash_status_check (info, sector, tout, prompt);
676 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000677 case CFI_CMDSET_INTEL_EXTENDED:
678 case CFI_CMDSET_INTEL_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +0000679 if ((retcode != ERR_OK)
680 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk5653fc32004-02-08 22:55:38 +0000681 retcode = ERR_INVAL;
wdenkbf9e3b32004-02-12 00:47:09 +0000682 printf ("Flash %s error at address %lx\n", prompt,
683 info->start[sector]);
wdenk028ab6b2004-02-23 23:54:43 +0000684 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenkbf9e3b32004-02-12 00:47:09 +0000685 printf ("Command Sequence Error.\n");
wdenk028ab6b2004-02-23 23:54:43 +0000686 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenkbf9e3b32004-02-12 00:47:09 +0000687 printf ("Block Erase Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000688 retcode = ERR_NOT_ERASED;
wdenk028ab6b2004-02-23 23:54:43 +0000689 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenkbf9e3b32004-02-12 00:47:09 +0000690 printf ("Locking Error\n");
wdenk5653fc32004-02-08 22:55:38 +0000691 }
wdenkbf9e3b32004-02-12 00:47:09 +0000692 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
693 printf ("Block locked.\n");
694 retcode = ERR_PROTECTED;
695 }
696 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
697 printf ("Vpp Low Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000698 }
wdenkbf9e3b32004-02-12 00:47:09 +0000699 flash_write_cmd (info, sector, 0, FLASH_CMD_RESET);
wdenk5653fc32004-02-08 22:55:38 +0000700 break;
701 default:
702 break;
703 }
704 return retcode;
705}
wdenkbf9e3b32004-02-12 00:47:09 +0000706
wdenk5653fc32004-02-08 22:55:38 +0000707/*-----------------------------------------------------------------------
708 */
wdenkbf9e3b32004-02-12 00:47:09 +0000709static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk5653fc32004-02-08 22:55:38 +0000710{
wdenk4d13cba2004-03-14 14:09:05 +0000711#if defined(__LITTLE_ENDIAN)
712 unsigned short w;
713 unsigned int l;
714 unsigned long long ll;
715#endif
716
wdenkbf9e3b32004-02-12 00:47:09 +0000717 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000718 case FLASH_CFI_8BIT:
719 cword->c = c;
720 break;
721 case FLASH_CFI_16BIT:
wdenk4d13cba2004-03-14 14:09:05 +0000722#if defined(__LITTLE_ENDIAN)
723 w = c;
724 w <<= 8;
725 cword->w = (cword->w >> 8) | w;
726#else
wdenk5653fc32004-02-08 22:55:38 +0000727 cword->w = (cword->w << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000728#endif
wdenk5653fc32004-02-08 22:55:38 +0000729 break;
730 case FLASH_CFI_32BIT:
wdenk4d13cba2004-03-14 14:09:05 +0000731#if defined(__LITTLE_ENDIAN)
732 l = c;
733 l <<= 24;
734 cword->l = (cword->l >> 8) | l;
735#else
wdenk5653fc32004-02-08 22:55:38 +0000736 cword->l = (cword->l << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000737#endif
wdenk5653fc32004-02-08 22:55:38 +0000738 break;
739 case FLASH_CFI_64BIT:
wdenk4d13cba2004-03-14 14:09:05 +0000740#if defined(__LITTLE_ENDIAN)
741 ll = c;
742 ll <<= 56;
743 cword->ll = (cword->ll >> 8) | ll;
744#else
wdenk5653fc32004-02-08 22:55:38 +0000745 cword->ll = (cword->ll << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000746#endif
wdenk5653fc32004-02-08 22:55:38 +0000747 break;
748 }
749}
750
751
752/*-----------------------------------------------------------------------
753 * make a proper sized command based on the port and chip widths
754 */
wdenkbf9e3b32004-02-12 00:47:09 +0000755static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk5653fc32004-02-08 22:55:38 +0000756{
757 int i;
wdenkbf9e3b32004-02-12 00:47:09 +0000758
759#if defined(__LITTLE_ENDIAN)
wdenk028ab6b2004-02-23 23:54:43 +0000760 ushort stmpw;
761 uint stmpi;
wdenkbf9e3b32004-02-12 00:47:09 +0000762#endif
763 uchar *cp = (uchar *) cmdbuf;
764
765 for (i = 0; i < info->portwidth; i++)
766 *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd;
767#if defined(__LITTLE_ENDIAN)
wdenk028ab6b2004-02-23 23:54:43 +0000768 switch (info->portwidth) {
769 case FLASH_CFI_8BIT:
770 break;
771 case FLASH_CFI_16BIT:
772 stmpw = *(ushort *) cmdbuf;
773 *(ushort *) cmdbuf = __swab16 (stmpw);
774 break;
775 case FLASH_CFI_32BIT:
776 stmpi = *(uint *) cmdbuf;
777 *(uint *) cmdbuf = __swab32 (stmpi);
778 break;
779 default:
780 printf("WARNING: flash_make_cmd: unsuppported LittleEndian mode\n");
781 break;
wdenkbf9e3b32004-02-12 00:47:09 +0000782 }
783#endif
wdenk5653fc32004-02-08 22:55:38 +0000784}
785
786/*
787 * Write a proper sized command to the correct address
788 */
wdenk028ab6b2004-02-23 23:54:43 +0000789static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000790{
791
792 volatile cfiptr_t addr;
793 cfiword_t cword;
wdenkbf9e3b32004-02-12 00:47:09 +0000794
795 addr.cp = flash_make_addr (info, sect, offset);
796 flash_make_cmd (info, cmd, &cword);
797 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000798 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000799 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
800 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk5653fc32004-02-08 22:55:38 +0000801 *addr.cp = cword.c;
802 break;
803 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000804 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
805 cmd, cword.w,
wdenk5653fc32004-02-08 22:55:38 +0000806 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
807 *addr.wp = cword.w;
808 break;
809 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000810 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
811 cmd, cword.l,
wdenk5653fc32004-02-08 22:55:38 +0000812 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
813 *addr.lp = cword.l;
814 break;
815 case FLASH_CFI_64BIT:
816#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +0000817 {
wdenk5653fc32004-02-08 22:55:38 +0000818 char str[20];
wdenkcd37d9e2004-02-10 00:03:41 +0000819
wdenkbf9e3b32004-02-12 00:47:09 +0000820 print_longlong (str, cword.ll);
821
822 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
823 addr.llp, cmd, str,
wdenk5653fc32004-02-08 22:55:38 +0000824 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
825 }
826#endif
827 *addr.llp = cword.ll;
828 break;
829 }
830}
831
wdenkbf9e3b32004-02-12 00:47:09 +0000832static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000833{
wdenk855a4962004-03-14 18:23:55 +0000834 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
835 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
wdenk5653fc32004-02-08 22:55:38 +0000836}
wdenkbf9e3b32004-02-12 00:47:09 +0000837
wdenk5653fc32004-02-08 22:55:38 +0000838/*-----------------------------------------------------------------------
839 */
wdenk028ab6b2004-02-23 23:54:43 +0000840static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000841{
842 cfiptr_t cptr;
843 cfiword_t cword;
844 int retval;
wdenk5653fc32004-02-08 22:55:38 +0000845
wdenkbf9e3b32004-02-12 00:47:09 +0000846 cptr.cp = flash_make_addr (info, sect, offset);
847 flash_make_cmd (info, cmd, &cword);
848
849 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
850 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000851 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000852 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk5653fc32004-02-08 22:55:38 +0000853 retval = (cptr.cp[0] == cword.c);
854 break;
855 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000856 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk5653fc32004-02-08 22:55:38 +0000857 retval = (cptr.wp[0] == cword.w);
858 break;
859 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000860 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk5653fc32004-02-08 22:55:38 +0000861 retval = (cptr.lp[0] == cword.l);
862 break;
863 case FLASH_CFI_64BIT:
wdenkcd37d9e2004-02-10 00:03:41 +0000864#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +0000865 {
wdenk5653fc32004-02-08 22:55:38 +0000866 char str1[20];
867 char str2[20];
wdenkbf9e3b32004-02-12 00:47:09 +0000868
869 print_longlong (str1, cptr.llp[0]);
870 print_longlong (str2, cword.ll);
871 debug ("is= %s %s\n", str1, str2);
wdenk5653fc32004-02-08 22:55:38 +0000872 }
873#endif
874 retval = (cptr.llp[0] == cword.ll);
875 break;
876 default:
877 retval = 0;
878 break;
879 }
880 return retval;
881}
wdenkbf9e3b32004-02-12 00:47:09 +0000882
wdenk5653fc32004-02-08 22:55:38 +0000883/*-----------------------------------------------------------------------
884 */
wdenk028ab6b2004-02-23 23:54:43 +0000885static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000886{
887 cfiptr_t cptr;
888 cfiword_t cword;
889 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000890
891 cptr.cp = flash_make_addr (info, sect, offset);
892 flash_make_cmd (info, cmd, &cword);
893 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000894 case FLASH_CFI_8BIT:
895 retval = ((cptr.cp[0] & cword.c) == cword.c);
896 break;
897 case FLASH_CFI_16BIT:
898 retval = ((cptr.wp[0] & cword.w) == cword.w);
899 break;
900 case FLASH_CFI_32BIT:
901 retval = ((cptr.lp[0] & cword.l) == cword.l);
902 break;
903 case FLASH_CFI_64BIT:
904 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenkbf9e3b32004-02-12 00:47:09 +0000905 break;
wdenk5653fc32004-02-08 22:55:38 +0000906 default:
907 retval = 0;
908 break;
909 }
910 return retval;
911}
912
913/*-----------------------------------------------------------------------
914 */
wdenk028ab6b2004-02-23 23:54:43 +0000915static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000916{
917 cfiptr_t cptr;
918 cfiword_t cword;
919 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000920
921 cptr.cp = flash_make_addr (info, sect, offset);
922 flash_make_cmd (info, cmd, &cword);
923 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000924 case FLASH_CFI_8BIT:
925 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
926 break;
927 case FLASH_CFI_16BIT:
928 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
929 break;
930 case FLASH_CFI_32BIT:
931 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
932 break;
933 case FLASH_CFI_64BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000934 retval = ((cptr.llp[0] & cword.ll) !=
935 (cptr.llp[0] & cword.ll));
wdenk5653fc32004-02-08 22:55:38 +0000936 break;
937 default:
938 retval = 0;
939 break;
940 }
941 return retval;
942}
943
944/*-----------------------------------------------------------------------
945 * detect if flash is compatible with the Common Flash Interface (CFI)
946 * http://www.jedec.org/download/search/jesd68.pdf
947 *
948*/
wdenkbf9e3b32004-02-12 00:47:09 +0000949static int flash_detect_cfi (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +0000950{
wdenkbf9e3b32004-02-12 00:47:09 +0000951 debug ("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +0000952
wdenkbf9e3b32004-02-12 00:47:09 +0000953 for (info->portwidth = FLASH_CFI_8BIT;
954 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
955 for (info->chipwidth = FLASH_CFI_BY8;
956 info->chipwidth <= info->portwidth;
957 info->chipwidth <<= 1) {
958 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
wdenk028ab6b2004-02-23 23:54:43 +0000959 flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
960 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
961 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
962 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
963 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
wdenkbf9e3b32004-02-12 00:47:09 +0000964 debug ("device interface is %d\n",
965 info->interface);
966 debug ("found port %d chip %d ",
967 info->portwidth, info->chipwidth);
968 debug ("port %d bits chip %d bits\n",
wdenk028ab6b2004-02-23 23:54:43 +0000969 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
970 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk5653fc32004-02-08 22:55:38 +0000971 return 1;
972 }
973 }
974 }
wdenkbf9e3b32004-02-12 00:47:09 +0000975 debug ("not found\n");
wdenk5653fc32004-02-08 22:55:38 +0000976 return 0;
977}
wdenkbf9e3b32004-02-12 00:47:09 +0000978
wdenk5653fc32004-02-08 22:55:38 +0000979/*
980 * The following code cannot be run from FLASH!
981 *
982 */
983static ulong flash_get_size (ulong base, int banknum)
984{
wdenkbf9e3b32004-02-12 00:47:09 +0000985 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +0000986 int i, j;
987 flash_sect_t sect_cnt;
988 unsigned long sector;
989 unsigned long tmp;
990 int size_ratio;
991 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +0000992 int erase_region_size;
993 int erase_region_count;
wdenk5653fc32004-02-08 22:55:38 +0000994
995 info->start[0] = base;
996
wdenkbf9e3b32004-02-12 00:47:09 +0000997 if (flash_detect_cfi (info)) {
wdenk028ab6b2004-02-23 23:54:43 +0000998 info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
wdenkbf9e3b32004-02-12 00:47:09 +0000999#ifdef DEBUG
1000 flash_printqry (info, 0);
1001#endif
1002 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001003 case CFI_CMDSET_INTEL_STANDARD:
1004 case CFI_CMDSET_INTEL_EXTENDED:
1005 default:
1006 info->cmd_reset = FLASH_CMD_RESET;
1007 break;
1008 case CFI_CMDSET_AMD_STANDARD:
1009 case CFI_CMDSET_AMD_EXTENDED:
1010 info->cmd_reset = AMD_CMD_RESET;
1011 break;
1012 }
wdenkcd37d9e2004-02-10 00:03:41 +00001013
wdenkbf9e3b32004-02-12 00:47:09 +00001014 debug ("manufacturer is %d\n", info->vendor);
wdenk5653fc32004-02-08 22:55:38 +00001015 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00001016 /* if the chip is x8/x16 reduce the ratio by half */
1017 if ((info->interface == FLASH_CFI_X8X16)
1018 && (info->chipwidth == FLASH_CFI_BY8)) {
1019 size_ratio >>= 1;
1020 }
wdenk028ab6b2004-02-23 23:54:43 +00001021 num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
wdenkbf9e3b32004-02-12 00:47:09 +00001022 debug ("size_ratio %d port %d bits chip %d bits\n",
1023 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1024 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1025 debug ("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00001026 sect_cnt = 0;
1027 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00001028 for (i = 0; i < num_erase_regions; i++) {
1029 if (i > NUM_ERASE_REGIONS) {
wdenk028ab6b2004-02-23 23:54:43 +00001030 printf ("%d erase regions found, only %d used\n",
1031 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00001032 break;
1033 }
wdenkbf9e3b32004-02-12 00:47:09 +00001034 tmp = flash_read_long (info, 0,
1035 FLASH_OFFSET_ERASE_REGIONS +
1036 i * 4);
1037 erase_region_size =
1038 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk5653fc32004-02-08 22:55:38 +00001039 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00001040 erase_region_count = (tmp & 0xffff) + 1;
wdenk028ab6b2004-02-23 23:54:43 +00001041 printf ("erase_region_count = %d erase_region_size = %d\n",
1042 erase_region_count, erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00001043 for (j = 0; j < erase_region_count; j++) {
wdenk5653fc32004-02-08 22:55:38 +00001044 info->start[sect_cnt] = sector;
1045 sector += (erase_region_size * size_ratio);
wdenkbf9e3b32004-02-12 00:47:09 +00001046 info->protect[sect_cnt] =
1047 flash_isset (info, sect_cnt,
1048 FLASH_OFFSET_PROTECT,
1049 FLASH_STATUS_PROTECT);
wdenk5653fc32004-02-08 22:55:38 +00001050 sect_cnt++;
1051 }
1052 }
1053
1054 info->sector_count = sect_cnt;
1055 /* multiply the size by the number of chips */
wdenk028ab6b2004-02-23 23:54:43 +00001056 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1057 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
wdenkbf9e3b32004-02-12 00:47:09 +00001058 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenk028ab6b2004-02-23 23:54:43 +00001059 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
wdenkbf9e3b32004-02-12 00:47:09 +00001060 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
wdenk028ab6b2004-02-23 23:54:43 +00001061 info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
wdenkbf9e3b32004-02-12 00:47:09 +00001062 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT);
wdenk028ab6b2004-02-23 23:54:43 +00001063 info->write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT))) / 1000;
wdenk5653fc32004-02-08 22:55:38 +00001064 info->flash_id = FLASH_MAN_CFI;
wdenk855a4962004-03-14 18:23:55 +00001065 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1066 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1067 }
wdenk5653fc32004-02-08 22:55:38 +00001068 }
1069
wdenkbf9e3b32004-02-12 00:47:09 +00001070 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
1071 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00001072}
1073
1074
1075/*-----------------------------------------------------------------------
1076 */
wdenkbf9e3b32004-02-12 00:47:09 +00001077static int flash_write_cfiword (flash_info_t * info, ulong dest,
1078 cfiword_t cword)
wdenk5653fc32004-02-08 22:55:38 +00001079{
1080
1081 cfiptr_t ctladdr;
1082 cfiptr_t cptr;
1083 int flag;
1084
wdenkbf9e3b32004-02-12 00:47:09 +00001085 ctladdr.cp = flash_make_addr (info, 0, 0);
1086 cptr.cp = (uchar *) dest;
wdenk5653fc32004-02-08 22:55:38 +00001087
1088
1089 /* Check if Flash is (sufficiently) erased */
wdenkbf9e3b32004-02-12 00:47:09 +00001090 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001091 case FLASH_CFI_8BIT:
1092 flag = ((cptr.cp[0] & cword.c) == cword.c);
1093 break;
1094 case FLASH_CFI_16BIT:
1095 flag = ((cptr.wp[0] & cword.w) == cword.w);
1096 break;
1097 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001098 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk5653fc32004-02-08 22:55:38 +00001099 break;
1100 case FLASH_CFI_64BIT:
1101 flag = ((cptr.lp[0] & cword.ll) == cword.ll);
1102 break;
1103 default:
1104 return 2;
1105 }
wdenkbf9e3b32004-02-12 00:47:09 +00001106 if (!flag)
wdenk5653fc32004-02-08 22:55:38 +00001107 return 2;
1108
1109 /* Disable interrupts which might cause a timeout here */
wdenkbf9e3b32004-02-12 00:47:09 +00001110 flag = disable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001111
wdenkbf9e3b32004-02-12 00:47:09 +00001112 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001113 case CFI_CMDSET_INTEL_EXTENDED:
1114 case CFI_CMDSET_INTEL_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001115 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1116 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001117 break;
1118 case CFI_CMDSET_AMD_EXTENDED:
1119 case CFI_CMDSET_AMD_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001120 flash_unlock_seq (info, 0);
wdenk855a4962004-03-14 18:23:55 +00001121 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001122 break;
1123 }
1124
wdenkbf9e3b32004-02-12 00:47:09 +00001125 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001126 case FLASH_CFI_8BIT:
1127 cptr.cp[0] = cword.c;
1128 break;
1129 case FLASH_CFI_16BIT:
1130 cptr.wp[0] = cword.w;
1131 break;
1132 case FLASH_CFI_32BIT:
1133 cptr.lp[0] = cword.l;
1134 break;
1135 case FLASH_CFI_64BIT:
1136 cptr.llp[0] = cword.ll;
1137 break;
1138 }
1139
1140 /* re-enable interrupts if necessary */
wdenkbf9e3b32004-02-12 00:47:09 +00001141 if (flag)
1142 enable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001143
wdenkbf9e3b32004-02-12 00:47:09 +00001144 return flash_full_status_check (info, 0, info->write_tout, "write");
wdenk5653fc32004-02-08 22:55:38 +00001145}
1146
1147#ifdef CFG_FLASH_USE_BUFFER_WRITE
1148
1149/* loop through the sectors from the highest address
1150 * when the passed address is greater or equal to the sector address
1151 * we have a match
1152 */
wdenkbf9e3b32004-02-12 00:47:09 +00001153static flash_sect_t find_sector (flash_info_t * info, ulong addr)
wdenk5653fc32004-02-08 22:55:38 +00001154{
1155 flash_sect_t sector;
wdenkbf9e3b32004-02-12 00:47:09 +00001156
1157 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1158 if (addr >= info->start[sector])
wdenk5653fc32004-02-08 22:55:38 +00001159 break;
1160 }
1161 return sector;
1162}
1163
wdenkbf9e3b32004-02-12 00:47:09 +00001164static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1165 int len)
wdenk5653fc32004-02-08 22:55:38 +00001166{
1167 flash_sect_t sector;
1168 int cnt;
1169 int retcode;
1170 volatile cfiptr_t src;
1171 volatile cfiptr_t dst;
wdenk855a4962004-03-14 18:23:55 +00001172 /* buffered writes in the AMD chip set is not supported yet */
1173 if((info->vendor == CFI_CMDSET_AMD_STANDARD) ||
1174 (info->vendor == CFI_CMDSET_AMD_EXTENDED))
1175 return ERR_INVAL;
wdenk5653fc32004-02-08 22:55:38 +00001176
1177 src.cp = cp;
wdenkbf9e3b32004-02-12 00:47:09 +00001178 dst.cp = (uchar *) dest;
1179 sector = find_sector (info, dest);
1180 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1181 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1182 if ((retcode =
1183 flash_status_check (info, sector, info->buffer_write_tout,
1184 "write to buffer")) == ERR_OK) {
1185 /* reduce the number of loops by the width of the port */
1186 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001187 case FLASH_CFI_8BIT:
1188 cnt = len;
1189 break;
1190 case FLASH_CFI_16BIT:
1191 cnt = len >> 1;
1192 break;
1193 case FLASH_CFI_32BIT:
1194 cnt = len >> 2;
1195 break;
1196 case FLASH_CFI_64BIT:
1197 cnt = len >> 3;
1198 break;
1199 default:
1200 return ERR_INVAL;
1201 break;
1202 }
wdenkbf9e3b32004-02-12 00:47:09 +00001203 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1204 while (cnt-- > 0) {
1205 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001206 case FLASH_CFI_8BIT:
1207 *dst.cp++ = *src.cp++;
1208 break;
1209 case FLASH_CFI_16BIT:
1210 *dst.wp++ = *src.wp++;
1211 break;
1212 case FLASH_CFI_32BIT:
1213 *dst.lp++ = *src.lp++;
1214 break;
1215 case FLASH_CFI_64BIT:
1216 *dst.llp++ = *src.llp++;
1217 break;
1218 default:
1219 return ERR_INVAL;
1220 break;
1221 }
1222 }
wdenkbf9e3b32004-02-12 00:47:09 +00001223 flash_write_cmd (info, sector, 0,
1224 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1225 retcode =
1226 flash_full_status_check (info, sector,
1227 info->buffer_write_tout,
1228 "buffer write");
wdenk5653fc32004-02-08 22:55:38 +00001229 }
wdenkbf9e3b32004-02-12 00:47:09 +00001230 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
wdenk5653fc32004-02-08 22:55:38 +00001231 return retcode;
1232}
1233#endif /* CFG_USE_FLASH_BUFFER_WRITE */
1234#endif /* CFG_FLASH_CFI */