blob: 8dc44997b8e2f609335f60455b3d5675a07e4a65 [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>
wdenk5653fc32004-02-08 22:55:38 +00007 *
wdenkbf9e3b32004-02-12 00:47:09 +00008 * Copyright (C) 2004
9 * Ed Okerson
Stefan Roese260421a2006-11-13 13:55:24 +010010 *
11 * Copyright (C) 2006
12 * Tolunay Orkun <listmember@orkun.us>
wdenkbf9e3b32004-02-12 00:47:09 +000013 *
wdenk5653fc32004-02-08 22:55:38 +000014 * See file CREDITS for list of people who contributed to this
15 * project.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * MA 02111-1307 USA
31 *
wdenk5653fc32004-02-08 22:55:38 +000032 */
33
34/* The DEBUG define must be before common to enable debugging */
wdenk2d1a5372004-02-23 19:30:57 +000035/* #define DEBUG */
36
wdenk5653fc32004-02-08 22:55:38 +000037#include <common.h>
38#include <asm/processor.h>
wdenk4c0d4c32004-06-09 17:34:58 +000039#include <asm/byteorder.h>
wdenk2a8af182005-04-13 10:02:42 +000040#include <environment.h>
wdenkbf9e3b32004-02-12 00:47:09 +000041#ifdef CFG_FLASH_CFI_DRIVER
wdenk028ab6b2004-02-23 23:54:43 +000042
Heiko Schocherca43ba12007-01-11 15:44:44 +010043#if defined(CONFIG_SOLIDCARD3)
44#define __LITTLE_ENDIAN
45#endif
46
wdenk5653fc32004-02-08 22:55:38 +000047/*
48 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
49 * The width of the port and the width of the chips are determined at initialization.
50 * These widths are used to calculate the address for access CFI data structures.
wdenk5653fc32004-02-08 22:55:38 +000051 *
52 * References
53 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
54 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
55 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
56 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese260421a2006-11-13 13:55:24 +010057 * AMD CFI Specification, Release 2.0 December 1, 2001
58 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
59 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk5653fc32004-02-08 22:55:38 +000060 *
wdenk5653fc32004-02-08 22:55:38 +000061 */
62
wdenkbf9e3b32004-02-12 00:47:09 +000063#ifndef CFG_FLASH_BANKS_LIST
64#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
65#endif
66
wdenk5653fc32004-02-08 22:55:38 +000067#define FLASH_CMD_CFI 0x98
68#define FLASH_CMD_READ_ID 0x90
69#define FLASH_CMD_RESET 0xff
70#define FLASH_CMD_BLOCK_ERASE 0x20
71#define FLASH_CMD_ERASE_CONFIRM 0xD0
72#define FLASH_CMD_WRITE 0x40
73#define FLASH_CMD_PROTECT 0x60
74#define FLASH_CMD_PROTECT_SET 0x01
75#define FLASH_CMD_PROTECT_CLEAR 0xD0
76#define FLASH_CMD_CLEAR_STATUS 0x50
wdenkbf9e3b32004-02-12 00:47:09 +000077#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
78#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk5653fc32004-02-08 22:55:38 +000079
80#define FLASH_STATUS_DONE 0x80
81#define FLASH_STATUS_ESS 0x40
82#define FLASH_STATUS_ECLBS 0x20
83#define FLASH_STATUS_PSLBS 0x10
84#define FLASH_STATUS_VPENS 0x08
85#define FLASH_STATUS_PSS 0x04
86#define FLASH_STATUS_DPS 0x02
87#define FLASH_STATUS_R 0x01
88#define FLASH_STATUS_PROTECT 0x01
89
90#define AMD_CMD_RESET 0xF0
91#define AMD_CMD_WRITE 0xA0
92#define AMD_CMD_ERASE_START 0x80
93#define AMD_CMD_ERASE_SECTOR 0x30
wdenk855a4962004-03-14 18:23:55 +000094#define AMD_CMD_UNLOCK_START 0xAA
95#define AMD_CMD_UNLOCK_ACK 0x55
Stefan Roese79b4cda2006-02-28 15:29:58 +010096#define AMD_CMD_WRITE_TO_BUFFER 0x25
97#define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
wdenk5653fc32004-02-08 22:55:38 +000098
99#define AMD_STATUS_TOGGLE 0x40
100#define AMD_STATUS_ERROR 0x20
Stefan Roese79b4cda2006-02-28 15:29:58 +0100101
102#define AMD_ADDR_ERASE_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
103#define AMD_ADDR_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
104#define AMD_ADDR_ACK ((info->portwidth == FLASH_CFI_8BIT) ? 0x555 : 0x2AA)
wdenk5653fc32004-02-08 22:55:38 +0000105
Stefan Roese260421a2006-11-13 13:55:24 +0100106#define FLASH_OFFSET_MANUFACTURER_ID 0x00
107#define FLASH_OFFSET_DEVICE_ID 0x01
108#define FLASH_OFFSET_DEVICE_ID2 0x0E
109#define FLASH_OFFSET_DEVICE_ID3 0x0F
wdenk5653fc32004-02-08 22:55:38 +0000110#define FLASH_OFFSET_CFI 0x55
Wolfgang Denk92eb7292006-12-27 01:26:13 +0100111#define FLASH_OFFSET_CFI_ALT 0x555
wdenk5653fc32004-02-08 22:55:38 +0000112#define FLASH_OFFSET_CFI_RESP 0x10
wdenkbf9e3b32004-02-12 00:47:09 +0000113#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
Stefan Roese2662b402006-04-01 13:41:03 +0200114#define FLASH_OFFSET_EXT_QUERY_T_P_ADDR 0x15 /* extended query table primary addr */
wdenk5653fc32004-02-08 22:55:38 +0000115#define FLASH_OFFSET_WTOUT 0x1F
wdenkbf9e3b32004-02-12 00:47:09 +0000116#define FLASH_OFFSET_WBTOUT 0x20
wdenk5653fc32004-02-08 22:55:38 +0000117#define FLASH_OFFSET_ETOUT 0x21
wdenkbf9e3b32004-02-12 00:47:09 +0000118#define FLASH_OFFSET_CETOUT 0x22
wdenk5653fc32004-02-08 22:55:38 +0000119#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenkbf9e3b32004-02-12 00:47:09 +0000120#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk5653fc32004-02-08 22:55:38 +0000121#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenkbf9e3b32004-02-12 00:47:09 +0000122#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk5653fc32004-02-08 22:55:38 +0000123#define FLASH_OFFSET_SIZE 0x27
wdenkbf9e3b32004-02-12 00:47:09 +0000124#define FLASH_OFFSET_INTERFACE 0x28
125#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk5653fc32004-02-08 22:55:38 +0000126#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
127#define FLASH_OFFSET_ERASE_REGIONS 0x2D
128#define FLASH_OFFSET_PROTECT 0x02
wdenkbf9e3b32004-02-12 00:47:09 +0000129#define FLASH_OFFSET_USER_PROTECTION 0x85
130#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk5653fc32004-02-08 22:55:38 +0000131
Stefan Roese260421a2006-11-13 13:55:24 +0100132#define CFI_CMDSET_NONE 0
133#define CFI_CMDSET_INTEL_EXTENDED 1
134#define CFI_CMDSET_AMD_STANDARD 2
135#define CFI_CMDSET_INTEL_STANDARD 3
136#define CFI_CMDSET_AMD_EXTENDED 4
137#define CFI_CMDSET_MITSU_STANDARD 256
138#define CFI_CMDSET_MITSU_EXTENDED 257
139#define CFI_CMDSET_SST 258
wdenk5653fc32004-02-08 22:55:38 +0000140
wdenkf7d15722004-12-18 22:35:43 +0000141#ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
142# undef FLASH_CMD_RESET
Stefan Roese260421a2006-11-13 13:55:24 +0100143# define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
wdenkf7d15722004-12-18 22:35:43 +0000144#endif
145
wdenk5653fc32004-02-08 22:55:38 +0000146typedef 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
Stefan Roese260421a2006-11-13 13:55:24 +0100160#define NUM_ERASE_REGIONS 4 /* max. number of erase regions */
wdenk5653fc32004-02-08 22:55:38 +0000161
Wolfgang Denk92eb7292006-12-27 01:26:13 +0100162static uint flash_offset_cfi[2]={FLASH_OFFSET_CFI,FLASH_OFFSET_CFI_ALT};
163
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200164/* use CFG_MAX_FLASH_BANKS_DETECT if defined */
165#ifdef CFG_MAX_FLASH_BANKS_DETECT
166static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
167flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
168#else
wdenk5653fc32004-02-08 22:55:38 +0000169static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200170flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
171#endif
wdenk5653fc32004-02-08 22:55:38 +0000172
Stefan Roese79b4cda2006-02-28 15:29:58 +0100173/*
174 * Check if chip width is defined. If not, start detecting with 8bit.
175 */
176#ifndef CFG_FLASH_CFI_WIDTH
177#define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
178#endif
179
wdenk5653fc32004-02-08 22:55:38 +0000180
181/*-----------------------------------------------------------------------
182 * Functions
183 */
184
185typedef unsigned long flash_sect_t;
186
wdenkbf9e3b32004-02-12 00:47:09 +0000187static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
188static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
wdenk028ab6b2004-02-23 23:54:43 +0000189static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
wdenkbf9e3b32004-02-12 00:47:09 +0000190static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
wdenk028ab6b2004-02-23 23:54:43 +0000191static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
192static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
193static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
Stefan Roese260421a2006-11-13 13:55:24 +0100194static void flash_read_jedec_ids (flash_info_t * info);
wdenkbf9e3b32004-02-12 00:47:09 +0000195static int flash_detect_cfi (flash_info_t * info);
wdenk028ab6b2004-02-23 23:54:43 +0000196static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
wdenkbf9e3b32004-02-12 00:47:09 +0000197static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
198 ulong tout, char *prompt);
Stefan Roesef18e8742006-03-01 17:00:49 +0100199ulong flash_get_size (ulong base, int banknum);
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200200#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk7680c142005-05-16 15:23:22 +0000201static flash_info_t *flash_get_info(ulong base);
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200202#endif
wdenk5653fc32004-02-08 22:55:38 +0000203#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenk028ab6b2004-02-23 23:54:43 +0000204static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
wdenk5653fc32004-02-08 22:55:38 +0000205#endif
206
wdenk5653fc32004-02-08 22:55:38 +0000207/*-----------------------------------------------------------------------
208 * create an address based on the offset and the port width
209 */
wdenk028ab6b2004-02-23 23:54:43 +0000210inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000211{
wdenkbf9e3b32004-02-12 00:47:09 +0000212 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
wdenk5653fc32004-02-08 22:55:38 +0000213}
wdenkbf9e3b32004-02-12 00:47:09 +0000214
215#ifdef DEBUG
216/*-----------------------------------------------------------------------
217 * Debug support
218 */
219void print_longlong (char *str, unsigned long long data)
220{
221 int i;
222 char *cp;
223
224 cp = (unsigned char *) &data;
225 for (i = 0; i < 8; i++)
226 sprintf (&str[i * 2], "%2.2x", *cp++);
227}
228static void flash_printqry (flash_info_t * info, flash_sect_t sect)
229{
230 cfiptr_t cptr;
231 int x, y;
232
Wolfgang Denk47340a42005-10-09 00:25:58 +0200233 for (x = 0; x < 0x40; x += 16U / info->portwidth) {
wdenkbf9e3b32004-02-12 00:47:09 +0000234 cptr.cp =
235 flash_make_addr (info, sect,
236 x + FLASH_OFFSET_CFI_RESP);
237 debug ("%p : ", cptr.cp);
238 for (y = 0; y < 16; y++) {
239 debug ("%2.2x ", cptr.cp[y]);
240 }
241 debug (" ");
242 for (y = 0; y < 16; y++) {
243 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
244 debug ("%c", cptr.cp[y]);
245 } else {
246 debug (".");
247 }
248 }
249 debug ("\n");
250 }
251}
wdenkbf9e3b32004-02-12 00:47:09 +0000252#endif
253
254
wdenk5653fc32004-02-08 22:55:38 +0000255/*-----------------------------------------------------------------------
256 * read a character at a port width address
257 */
wdenkbf9e3b32004-02-12 00:47:09 +0000258inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000259{
260 uchar *cp;
wdenkbf9e3b32004-02-12 00:47:09 +0000261
262 cp = flash_make_addr (info, 0, offset);
263#if defined(__LITTLE_ENDIAN)
264 return (cp[0]);
265#else
wdenk5653fc32004-02-08 22:55:38 +0000266 return (cp[info->portwidth - 1]);
wdenkbf9e3b32004-02-12 00:47:09 +0000267#endif
wdenk5653fc32004-02-08 22:55:38 +0000268}
269
270/*-----------------------------------------------------------------------
271 * read a short word by swapping for ppc format.
272 */
wdenkbf9e3b32004-02-12 00:47:09 +0000273ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000274{
wdenkbf9e3b32004-02-12 00:47:09 +0000275 uchar *addr;
276 ushort retval;
wdenk5653fc32004-02-08 22:55:38 +0000277
wdenkbf9e3b32004-02-12 00:47:09 +0000278#ifdef DEBUG
279 int x;
280#endif
281 addr = flash_make_addr (info, sect, offset);
wdenk5653fc32004-02-08 22:55:38 +0000282
wdenkbf9e3b32004-02-12 00:47:09 +0000283#ifdef DEBUG
284 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
285 info->portwidth);
286 for (x = 0; x < 2 * info->portwidth; x++) {
287 debug ("addr[%x] = 0x%x\n", x, addr[x]);
288 }
289#endif
290#if defined(__LITTLE_ENDIAN)
291 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
292#else
293 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
294 addr[info->portwidth - 1]);
295#endif
296
297 debug ("retval = 0x%x\n", retval);
298 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000299}
300
301/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +0100302 * read a long word by picking the least significant byte of each maximum
wdenk5653fc32004-02-08 22:55:38 +0000303 * port size word. Swap for ppc format.
304 */
wdenkbf9e3b32004-02-12 00:47:09 +0000305ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000306{
wdenkbf9e3b32004-02-12 00:47:09 +0000307 uchar *addr;
308 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000309
wdenkbf9e3b32004-02-12 00:47:09 +0000310#ifdef DEBUG
311 int x;
312#endif
313 addr = flash_make_addr (info, sect, offset);
314
315#ifdef DEBUG
316 debug ("long addr is at %p info->portwidth = %d\n", addr,
317 info->portwidth);
318 for (x = 0; x < 4 * info->portwidth; x++) {
319 debug ("addr[%x] = 0x%x\n", x, addr[x]);
320 }
321#endif
322#if defined(__LITTLE_ENDIAN)
323 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
wdenk028ab6b2004-02-23 23:54:43 +0000324 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
wdenkbf9e3b32004-02-12 00:47:09 +0000325#else
326 retval = (addr[(2 * info->portwidth) - 1] << 24) |
327 (addr[(info->portwidth) - 1] << 16) |
328 (addr[(4 * info->portwidth) - 1] << 8) |
329 addr[(3 * info->portwidth) - 1];
330#endif
331 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000332}
333
Stefan Roese79b4cda2006-02-28 15:29:58 +0100334
wdenk5653fc32004-02-08 22:55:38 +0000335/*-----------------------------------------------------------------------
336 */
337unsigned long flash_init (void)
338{
339 unsigned long size = 0;
340 int i;
341
Stefan Roese2662b402006-04-01 13:41:03 +0200342#ifdef CFG_FLASH_PROTECTION
343 char *s = getenv("unlock");
344#endif
345
wdenk5653fc32004-02-08 22:55:38 +0000346 /* Init: no FLASHes known */
wdenkbf9e3b32004-02-12 00:47:09 +0000347 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk5653fc32004-02-08 22:55:38 +0000348 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenkbf9e3b32004-02-12 00:47:09 +0000349 size += flash_info[i].size = flash_get_size (bank_base[i], i);
wdenk5653fc32004-02-08 22:55:38 +0000350 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Stefan Roese5568e612005-11-22 13:20:42 +0100351#ifndef CFG_FLASH_QUIET_TEST
wdenk028ab6b2004-02-23 23:54:43 +0000352 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
Wolfgang Denk92eb7292006-12-27 01:26:13 +0100353 i+1, flash_info[i].size, flash_info[i].size << 20);
Stefan Roese5568e612005-11-22 13:20:42 +0100354#endif /* CFG_FLASH_QUIET_TEST */
wdenk5653fc32004-02-08 22:55:38 +0000355 }
Stefan Roese79b4cda2006-02-28 15:29:58 +0100356#ifdef CFG_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +0200357 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
358 /*
359 * Only the U-Boot image and it's environment is protected,
360 * all other sectors are unprotected (unlocked) if flash
361 * hardware protection is used (CFG_FLASH_PROTECTION) and
362 * the environment variable "unlock" is set to "yes".
363 */
364 if (flash_info[i].legacy_unlock) {
365 int k;
Stefan Roese79b4cda2006-02-28 15:29:58 +0100366
Stefan Roese79b4cda2006-02-28 15:29:58 +0100367 /*
Stefan Roese2662b402006-04-01 13:41:03 +0200368 * Disable legacy_unlock temporarily, since
369 * flash_real_protect would relock all other sectors
370 * again otherwise.
371 */
372 flash_info[i].legacy_unlock = 0;
373
374 /*
375 * Legacy unlocking (e.g. Intel J3) -> unlock only one
376 * sector. This will unlock all sectors.
377 */
378 flash_real_protect (&flash_info[i], 0, 0);
379
380 flash_info[i].legacy_unlock = 1;
381
382 /*
383 * Manually mark other sectors as unlocked (unprotected)
384 */
385 for (k = 1; k < flash_info[i].sector_count; k++)
386 flash_info[i].protect[k] = 0;
387 } else {
388 /*
389 * No legancy unlocking -> unlock all sectors
Stefan Roese79b4cda2006-02-28 15:29:58 +0100390 */
391 flash_protect (FLAG_PROTECT_CLEAR,
392 flash_info[i].start[0],
393 flash_info[i].start[0] + flash_info[i].size - 1,
394 &flash_info[i]);
395 }
396 }
397#endif /* CFG_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +0000398 }
399
400 /* Monitor protection ON by default */
401#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenkbf9e3b32004-02-12 00:47:09 +0000402 flash_protect (FLAG_PROTECT_SET,
403 CFG_MONITOR_BASE,
wdenk7680c142005-05-16 15:23:22 +0000404 CFG_MONITOR_BASE + monitor_flash_len - 1,
405 flash_get_info(CFG_MONITOR_BASE));
wdenk5653fc32004-02-08 22:55:38 +0000406#endif
407
wdenk656658d2004-10-10 22:16:06 +0000408 /* Environment protection ON by default */
409#ifdef CFG_ENV_IS_IN_FLASH
410 flash_protect (FLAG_PROTECT_SET,
411 CFG_ENV_ADDR,
412 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
wdenk7680c142005-05-16 15:23:22 +0000413 flash_get_info(CFG_ENV_ADDR));
wdenk656658d2004-10-10 22:16:06 +0000414#endif
415
416 /* Redundant environment protection ON by default */
417#ifdef CFG_ENV_ADDR_REDUND
418 flash_protect (FLAG_PROTECT_SET,
419 CFG_ENV_ADDR_REDUND,
420 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
wdenk7680c142005-05-16 15:23:22 +0000421 flash_get_info(CFG_ENV_ADDR_REDUND));
wdenk656658d2004-10-10 22:16:06 +0000422#endif
wdenk5653fc32004-02-08 22:55:38 +0000423 return (size);
424}
425
426/*-----------------------------------------------------------------------
427 */
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200428#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk7680c142005-05-16 15:23:22 +0000429static flash_info_t *flash_get_info(ulong base)
430{
431 int i;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200432 flash_info_t * info = 0;
wdenk7680c142005-05-16 15:23:22 +0000433
434 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
435 info = & flash_info[i];
436 if (info->size && info->start[0] <= base &&
437 base <= info->start[0] + info->size - 1)
438 break;
439 }
440
441 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
442}
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200443#endif
wdenk7680c142005-05-16 15:23:22 +0000444
445/*-----------------------------------------------------------------------
446 */
wdenkbf9e3b32004-02-12 00:47:09 +0000447int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +0000448{
449 int rcode = 0;
450 int prot;
451 flash_sect_t sect;
452
wdenkbf9e3b32004-02-12 00:47:09 +0000453 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +0000454 puts ("Can't erase unknown flash type - aborted\n");
wdenk5653fc32004-02-08 22:55:38 +0000455 return 1;
456 }
457 if ((s_first < 0) || (s_first > s_last)) {
wdenk4b9206e2004-03-23 22:14:11 +0000458 puts ("- no sectors to erase\n");
wdenk5653fc32004-02-08 22:55:38 +0000459 return 1;
460 }
461
462 prot = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000463 for (sect = s_first; sect <= s_last; ++sect) {
wdenk5653fc32004-02-08 22:55:38 +0000464 if (info->protect[sect]) {
465 prot++;
466 }
467 }
468 if (prot) {
wdenkbf9e3b32004-02-12 00:47:09 +0000469 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk5653fc32004-02-08 22:55:38 +0000470 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000471 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +0000472 }
473
474
wdenkbf9e3b32004-02-12 00:47:09 +0000475 for (sect = s_first; sect <= s_last; sect++) {
wdenk5653fc32004-02-08 22:55:38 +0000476 if (info->protect[sect] == 0) { /* not protected */
wdenkbf9e3b32004-02-12 00:47:09 +0000477 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000478 case CFI_CMDSET_INTEL_STANDARD:
479 case CFI_CMDSET_INTEL_EXTENDED:
wdenk028ab6b2004-02-23 23:54:43 +0000480 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
481 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
482 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +0000483 break;
484 case CFI_CMDSET_AMD_STANDARD:
485 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000486 flash_unlock_seq (info, sect);
wdenk855a4962004-03-14 18:23:55 +0000487 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
488 AMD_CMD_ERASE_START);
wdenkbf9e3b32004-02-12 00:47:09 +0000489 flash_unlock_seq (info, sect);
wdenk028ab6b2004-02-23 23:54:43 +0000490 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
wdenk5653fc32004-02-08 22:55:38 +0000491 break;
492 default:
wdenkbf9e3b32004-02-12 00:47:09 +0000493 debug ("Unkown flash vendor %d\n",
494 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +0000495 break;
496 }
497
wdenkbf9e3b32004-02-12 00:47:09 +0000498 if (flash_full_status_check
499 (info, sect, info->erase_blk_tout, "erase")) {
wdenk5653fc32004-02-08 22:55:38 +0000500 rcode = 1;
501 } else
wdenk4b9206e2004-03-23 22:14:11 +0000502 putc ('.');
wdenk5653fc32004-02-08 22:55:38 +0000503 }
504 }
wdenk4b9206e2004-03-23 22:14:11 +0000505 puts (" done\n");
wdenk5653fc32004-02-08 22:55:38 +0000506 return rcode;
507}
508
509/*-----------------------------------------------------------------------
510 */
wdenkbf9e3b32004-02-12 00:47:09 +0000511void flash_print_info (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +0000512{
513 int i;
514
515 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +0000516 puts ("missing or unknown FLASH type\n");
wdenk5653fc32004-02-08 22:55:38 +0000517 return;
518 }
519
wdenkbf9e3b32004-02-12 00:47:09 +0000520 printf ("CFI conformant FLASH (%d x %d)",
521 (info->portwidth << 3), (info->chipwidth << 3));
wdenk5653fc32004-02-08 22:55:38 +0000522 printf (" Size: %ld MB in %d Sectors\n",
523 info->size >> 20, info->sector_count);
Stefan Roese260421a2006-11-13 13:55:24 +0100524 printf (" ");
525 switch (info->vendor) {
526 case CFI_CMDSET_INTEL_STANDARD:
527 printf ("Intel Standard");
528 break;
529 case CFI_CMDSET_INTEL_EXTENDED:
530 printf ("Intel Extended");
531 break;
532 case CFI_CMDSET_AMD_STANDARD:
533 printf ("AMD Standard");
534 break;
535 case CFI_CMDSET_AMD_EXTENDED:
536 printf ("AMD Extended");
537 break;
538 default:
539 printf ("Unknown (%d)", info->vendor);
540 break;
541 }
542 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
543 info->manufacturer_id, info->device_id);
544 if (info->device_id == 0x7E) {
545 printf("%04X", info->device_id2);
546 }
547 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
wdenk028ab6b2004-02-23 23:54:43 +0000548 info->erase_blk_tout,
Stefan Roese260421a2006-11-13 13:55:24 +0100549 info->write_tout);
550 if (info->buffer_size > 1) {
551 printf (" Buffer write timeout: %ld ms, buffer size: %d bytes\n",
wdenk028ab6b2004-02-23 23:54:43 +0000552 info->buffer_write_tout,
553 info->buffer_size);
Stefan Roese260421a2006-11-13 13:55:24 +0100554 }
wdenk5653fc32004-02-08 22:55:38 +0000555
Stefan Roese260421a2006-11-13 13:55:24 +0100556 puts ("\n Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +0000557 for (i = 0; i < info->sector_count; ++i) {
Stefan Roese260421a2006-11-13 13:55:24 +0100558 if ((i % 5) == 0)
559 printf ("\n");
wdenk5653fc32004-02-08 22:55:38 +0000560#ifdef CFG_FLASH_EMPTY_INFO
561 int k;
562 int size;
563 int erased;
564 volatile unsigned long *flash;
565
566 /*
567 * Check if whole sector is erased
568 */
wdenkbf9e3b32004-02-12 00:47:09 +0000569 if (i != (info->sector_count - 1))
570 size = info->start[i + 1] - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000571 else
wdenkbf9e3b32004-02-12 00:47:09 +0000572 size = info->start[0] + info->size - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000573 erased = 1;
wdenkbf9e3b32004-02-12 00:47:09 +0000574 flash = (volatile unsigned long *) info->start[i];
575 size = size >> 2; /* divide by 4 for longword access */
576 for (k = 0; k < size; k++) {
577 if (*flash++ != 0xffffffff) {
578 erased = 0;
579 break;
580 }
581 }
wdenk5653fc32004-02-08 22:55:38 +0000582
wdenk5653fc32004-02-08 22:55:38 +0000583 /* print empty and read-only info */
Stefan Roese260421a2006-11-13 13:55:24 +0100584 printf (" %08lX %c %s ",
wdenk5653fc32004-02-08 22:55:38 +0000585 info->start[i],
Stefan Roese260421a2006-11-13 13:55:24 +0100586 erased ? 'E' : ' ',
587 info->protect[i] ? "RO" : " ");
Wolfgang Denkb63de2c2005-09-25 00:23:05 +0200588#else /* ! CFG_FLASH_EMPTY_INFO */
Stefan Roese260421a2006-11-13 13:55:24 +0100589 printf (" %08lX %s ",
590 info->start[i],
591 info->protect[i] ? "RO" : " ");
wdenk5653fc32004-02-08 22:55:38 +0000592#endif
593 }
wdenk4b9206e2004-03-23 22:14:11 +0000594 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +0000595 return;
596}
597
598/*-----------------------------------------------------------------------
599 * Copy memory to flash, returns:
600 * 0 - OK
601 * 1 - write timeout
602 * 2 - Flash not erased
603 */
wdenkbf9e3b32004-02-12 00:47:09 +0000604int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +0000605{
606 ulong wp;
607 ulong cp;
608 int aln;
609 cfiword_t cword;
610 int i, rc;
611
wdenkbf9e3b32004-02-12 00:47:09 +0000612#ifdef CFG_FLASH_USE_BUFFER_WRITE
613 int buffered_size;
614#endif
wdenkbf9e3b32004-02-12 00:47:09 +0000615 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +0000616 /* get lower aligned address */
617 wp = (addr & ~(info->portwidth - 1));
618
619 /* handle unaligned start */
wdenkbf9e3b32004-02-12 00:47:09 +0000620 if ((aln = addr - wp) != 0) {
wdenk5653fc32004-02-08 22:55:38 +0000621 cword.l = 0;
622 cp = wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000623 for (i = 0; i < aln; ++i, ++cp)
624 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000625
wdenkbf9e3b32004-02-12 00:47:09 +0000626 for (; (i < info->portwidth) && (cnt > 0); i++) {
627 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000628 cnt--;
629 cp++;
630 }
wdenkbf9e3b32004-02-12 00:47:09 +0000631 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
632 flash_add_byte (info, &cword, (*(uchar *) cp));
633 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000634 return rc;
635 wp = cp;
636 }
637
wdenkbf9e3b32004-02-12 00:47:09 +0000638 /* handle the aligned part */
wdenk5653fc32004-02-08 22:55:38 +0000639#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +0000640 buffered_size = (info->portwidth / info->chipwidth);
641 buffered_size *= info->buffer_size;
642 while (cnt >= info->portwidth) {
Stefan Roese79b4cda2006-02-28 15:29:58 +0100643 /* prohibit buffer write when buffer_size is 1 */
644 if (info->buffer_size == 1) {
645 cword.l = 0;
646 for (i = 0; i < info->portwidth; i++)
647 flash_add_byte (info, &cword, *src++);
648 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
649 return rc;
650 wp += info->portwidth;
651 cnt -= info->portwidth;
652 continue;
653 }
654
655 /* write buffer until next buffered_size aligned boundary */
656 i = buffered_size - (wp % buffered_size);
657 if (i > cnt)
658 i = cnt;
wdenkbf9e3b32004-02-12 00:47:09 +0000659 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +0000660 return rc;
Wolfgang Denk8d4ba3d2005-08-12 22:35:59 +0200661 i -= i & (info->portwidth - 1);
wdenk5653fc32004-02-08 22:55:38 +0000662 wp += i;
663 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +0000664 cnt -= i;
wdenk5653fc32004-02-08 22:55:38 +0000665 }
666#else
wdenkbf9e3b32004-02-12 00:47:09 +0000667 while (cnt >= info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000668 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000669 for (i = 0; i < info->portwidth; i++) {
670 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000671 }
wdenkbf9e3b32004-02-12 00:47:09 +0000672 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000673 return rc;
674 wp += info->portwidth;
675 cnt -= info->portwidth;
676 }
677#endif /* CFG_FLASH_USE_BUFFER_WRITE */
678 if (cnt == 0) {
679 return (0);
680 }
681
682 /*
683 * handle unaligned tail bytes
684 */
685 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000686 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
687 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000688 --cnt;
689 }
wdenkbf9e3b32004-02-12 00:47:09 +0000690 for (; i < info->portwidth; ++i, ++cp) {
691 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000692 }
693
wdenkbf9e3b32004-02-12 00:47:09 +0000694 return flash_write_cfiword (info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +0000695}
696
697/*-----------------------------------------------------------------------
698 */
699#ifdef CFG_FLASH_PROTECTION
700
wdenkbf9e3b32004-02-12 00:47:09 +0000701int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +0000702{
703 int retcode = 0;
704
wdenkbf9e3b32004-02-12 00:47:09 +0000705 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
706 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
707 if (prot)
708 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk5653fc32004-02-08 22:55:38 +0000709 else
wdenkbf9e3b32004-02-12 00:47:09 +0000710 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk5653fc32004-02-08 22:55:38 +0000711
wdenkbf9e3b32004-02-12 00:47:09 +0000712 if ((retcode =
713 flash_full_status_check (info, sector, info->erase_blk_tout,
714 prot ? "protect" : "unprotect")) == 0) {
wdenk5653fc32004-02-08 22:55:38 +0000715
716 info->protect[sector] = prot;
Stefan Roese2662b402006-04-01 13:41:03 +0200717
718 /*
719 * On some of Intel's flash chips (marked via legacy_unlock)
720 * unprotect unprotects all locking.
721 */
722 if ((prot == 0) && (info->legacy_unlock)) {
wdenk5653fc32004-02-08 22:55:38 +0000723 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +0000724
725 for (i = 0; i < info->sector_count; i++) {
726 if (info->protect[i])
727 flash_real_protect (info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +0000728 }
729 }
730 }
wdenk5653fc32004-02-08 22:55:38 +0000731 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000732}
733
wdenk5653fc32004-02-08 22:55:38 +0000734/*-----------------------------------------------------------------------
735 * flash_read_user_serial - read the OneTimeProgramming cells
736 */
wdenkbf9e3b32004-02-12 00:47:09 +0000737void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
738 int len)
wdenk5653fc32004-02-08 22:55:38 +0000739{
wdenkbf9e3b32004-02-12 00:47:09 +0000740 uchar *src;
741 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +0000742
743 dst = buffer;
wdenkbf9e3b32004-02-12 00:47:09 +0000744 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
745 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
746 memcpy (dst, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200747 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000748}
wdenkbf9e3b32004-02-12 00:47:09 +0000749
wdenk5653fc32004-02-08 22:55:38 +0000750/*
751 * flash_read_factory_serial - read the device Id from the protection area
752 */
wdenkbf9e3b32004-02-12 00:47:09 +0000753void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
754 int len)
wdenk5653fc32004-02-08 22:55:38 +0000755{
wdenkbf9e3b32004-02-12 00:47:09 +0000756 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +0000757
wdenkbf9e3b32004-02-12 00:47:09 +0000758 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
759 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
760 memcpy (buffer, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200761 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000762}
763
764#endif /* CFG_FLASH_PROTECTION */
765
wdenkbf9e3b32004-02-12 00:47:09 +0000766/*
767 * flash_is_busy - check to see if the flash is busy
768 * This routine checks the status of the chip and returns true if the chip is busy
769 */
770static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000771{
772 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000773
774 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000775 case CFI_CMDSET_INTEL_STANDARD:
776 case CFI_CMDSET_INTEL_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000777 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk5653fc32004-02-08 22:55:38 +0000778 break;
779 case CFI_CMDSET_AMD_STANDARD:
780 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000781 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk5653fc32004-02-08 22:55:38 +0000782 break;
783 default:
784 retval = 0;
785 }
wdenkbf9e3b32004-02-12 00:47:09 +0000786 debug ("flash_is_busy: %d\n", retval);
wdenk5653fc32004-02-08 22:55:38 +0000787 return retval;
788}
wdenkbf9e3b32004-02-12 00:47:09 +0000789
wdenk5653fc32004-02-08 22:55:38 +0000790/*-----------------------------------------------------------------------
791 * wait for XSR.7 to be set. Time out with an error if it does not.
792 * This routine does not set the flash to read-array mode.
793 */
wdenkbf9e3b32004-02-12 00:47:09 +0000794static int flash_status_check (flash_info_t * info, flash_sect_t sector,
795 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000796{
797 ulong start;
798
Stefan Roese2662b402006-04-01 13:41:03 +0200799#if CFG_HZ != 1000
800 tout *= CFG_HZ/1000;
801#endif
802
wdenk5653fc32004-02-08 22:55:38 +0000803 /* Wait for command completion */
804 start = get_timer (0);
wdenkbf9e3b32004-02-12 00:47:09 +0000805 while (flash_is_busy (info, sector)) {
Stefan Roese79b4cda2006-02-28 15:29:58 +0100806 if (get_timer (start) > tout) {
wdenkbf9e3b32004-02-12 00:47:09 +0000807 printf ("Flash %s timeout at address %lx data %lx\n",
808 prompt, info->start[sector],
809 flash_read_long (info, sector, 0));
810 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000811 return ERR_TIMOUT;
812 }
Wolfgang Denk62b8f542006-06-02 11:46:20 +0200813 udelay (1); /* also triggers watchdog */
wdenk5653fc32004-02-08 22:55:38 +0000814 }
815 return ERR_OK;
816}
wdenkbf9e3b32004-02-12 00:47:09 +0000817
wdenk5653fc32004-02-08 22:55:38 +0000818/*-----------------------------------------------------------------------
819 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
820 * This routine sets the flash to read-array mode.
821 */
wdenkbf9e3b32004-02-12 00:47:09 +0000822static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
823 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000824{
825 int retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000826
827 retcode = flash_status_check (info, sector, tout, prompt);
828 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000829 case CFI_CMDSET_INTEL_EXTENDED:
830 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roese79b4cda2006-02-28 15:29:58 +0100831 if ((retcode == ERR_OK)
wdenkbf9e3b32004-02-12 00:47:09 +0000832 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk5653fc32004-02-08 22:55:38 +0000833 retcode = ERR_INVAL;
wdenkbf9e3b32004-02-12 00:47:09 +0000834 printf ("Flash %s error at address %lx\n", prompt,
835 info->start[sector]);
wdenk028ab6b2004-02-23 23:54:43 +0000836 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000837 puts ("Command Sequence Error.\n");
wdenk028ab6b2004-02-23 23:54:43 +0000838 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000839 puts ("Block Erase Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000840 retcode = ERR_NOT_ERASED;
wdenk028ab6b2004-02-23 23:54:43 +0000841 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000842 puts ("Locking Error\n");
wdenk5653fc32004-02-08 22:55:38 +0000843 }
wdenkbf9e3b32004-02-12 00:47:09 +0000844 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000845 puts ("Block locked.\n");
wdenkbf9e3b32004-02-12 00:47:09 +0000846 retcode = ERR_PROTECTED;
847 }
848 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk4b9206e2004-03-23 22:14:11 +0000849 puts ("Vpp Low Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000850 }
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200851 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000852 break;
853 default:
854 break;
855 }
856 return retcode;
857}
wdenkbf9e3b32004-02-12 00:47:09 +0000858
wdenk5653fc32004-02-08 22:55:38 +0000859/*-----------------------------------------------------------------------
860 */
wdenkbf9e3b32004-02-12 00:47:09 +0000861static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk5653fc32004-02-08 22:55:38 +0000862{
wdenk4d13cba2004-03-14 14:09:05 +0000863#if defined(__LITTLE_ENDIAN)
864 unsigned short w;
865 unsigned int l;
866 unsigned long long ll;
867#endif
868
wdenkbf9e3b32004-02-12 00:47:09 +0000869 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000870 case FLASH_CFI_8BIT:
871 cword->c = c;
872 break;
873 case FLASH_CFI_16BIT:
Heiko Schocherca43ba12007-01-11 15:44:44 +0100874#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SOLIDCARD3)
wdenk4d13cba2004-03-14 14:09:05 +0000875 w = c;
876 w <<= 8;
877 cword->w = (cword->w >> 8) | w;
878#else
wdenk5653fc32004-02-08 22:55:38 +0000879 cword->w = (cword->w << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000880#endif
wdenk5653fc32004-02-08 22:55:38 +0000881 break;
882 case FLASH_CFI_32BIT:
wdenk4d13cba2004-03-14 14:09:05 +0000883#if defined(__LITTLE_ENDIAN)
884 l = c;
885 l <<= 24;
886 cword->l = (cword->l >> 8) | l;
887#else
wdenk5653fc32004-02-08 22:55:38 +0000888 cword->l = (cword->l << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000889#endif
wdenk5653fc32004-02-08 22:55:38 +0000890 break;
891 case FLASH_CFI_64BIT:
wdenk4d13cba2004-03-14 14:09:05 +0000892#if defined(__LITTLE_ENDIAN)
893 ll = c;
894 ll <<= 56;
895 cword->ll = (cword->ll >> 8) | ll;
896#else
wdenk5653fc32004-02-08 22:55:38 +0000897 cword->ll = (cword->ll << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +0000898#endif
wdenk5653fc32004-02-08 22:55:38 +0000899 break;
900 }
901}
902
903
904/*-----------------------------------------------------------------------
905 * make a proper sized command based on the port and chip widths
906 */
wdenkbf9e3b32004-02-12 00:47:09 +0000907static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk5653fc32004-02-08 22:55:38 +0000908{
909 int i;
wdenkbf9e3b32004-02-12 00:47:09 +0000910 uchar *cp = (uchar *) cmdbuf;
911
wdenkbf9e3b32004-02-12 00:47:09 +0000912#if defined(__LITTLE_ENDIAN)
Wolfgang Denkdafbe372005-09-24 23:32:48 +0200913 for (i = info->portwidth; i > 0; i--)
914#else
915 for (i = 1; i <= info->portwidth; i++)
wdenkbf9e3b32004-02-12 00:47:09 +0000916#endif
Wolfgang Denk47340a42005-10-09 00:25:58 +0200917 *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
wdenk5653fc32004-02-08 22:55:38 +0000918}
919
920/*
921 * Write a proper sized command to the correct address
922 */
wdenk028ab6b2004-02-23 23:54:43 +0000923static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000924{
925
926 volatile cfiptr_t addr;
927 cfiword_t cword;
wdenkbf9e3b32004-02-12 00:47:09 +0000928
929 addr.cp = flash_make_addr (info, sect, offset);
930 flash_make_cmd (info, cmd, &cword);
931 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000932 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000933 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
934 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk5653fc32004-02-08 22:55:38 +0000935 *addr.cp = cword.c;
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100936#ifdef CONFIG_BLACKFIN
937 asm("ssync;");
938#endif
wdenk5653fc32004-02-08 22:55:38 +0000939 break;
940 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000941 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
942 cmd, cword.w,
wdenk5653fc32004-02-08 22:55:38 +0000943 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
944 *addr.wp = cword.w;
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100945#ifdef CONFIG_BLACKFIN
946 asm("ssync;");
947#endif
wdenk5653fc32004-02-08 22:55:38 +0000948 break;
949 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000950 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
951 cmd, cword.l,
wdenk5653fc32004-02-08 22:55:38 +0000952 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
953 *addr.lp = cword.l;
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100954#ifdef CONFIG_BLACKFIN
955 asm("ssync;");
956#endif
wdenk5653fc32004-02-08 22:55:38 +0000957 break;
958 case FLASH_CFI_64BIT:
959#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +0000960 {
wdenk5653fc32004-02-08 22:55:38 +0000961 char str[20];
wdenkcd37d9e2004-02-10 00:03:41 +0000962
wdenkbf9e3b32004-02-12 00:47:09 +0000963 print_longlong (str, cword.ll);
964
965 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
966 addr.llp, cmd, str,
wdenk5653fc32004-02-08 22:55:38 +0000967 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
968 }
969#endif
970 *addr.llp = cword.ll;
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100971#ifdef CONFIG_BLACKFIN
972 asm("ssync;");
973#endif
wdenk5653fc32004-02-08 22:55:38 +0000974 break;
975 }
976}
977
wdenkbf9e3b32004-02-12 00:47:09 +0000978static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000979{
wdenk855a4962004-03-14 18:23:55 +0000980 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
981 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
wdenk5653fc32004-02-08 22:55:38 +0000982}
wdenkbf9e3b32004-02-12 00:47:09 +0000983
wdenk5653fc32004-02-08 22:55:38 +0000984/*-----------------------------------------------------------------------
985 */
wdenk028ab6b2004-02-23 23:54:43 +0000986static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +0000987{
988 cfiptr_t cptr;
989 cfiword_t cword;
990 int retval;
wdenk5653fc32004-02-08 22:55:38 +0000991
wdenkbf9e3b32004-02-12 00:47:09 +0000992 cptr.cp = flash_make_addr (info, sect, offset);
993 flash_make_cmd (info, cmd, &cword);
994
995 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
996 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000997 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +0000998 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk5653fc32004-02-08 22:55:38 +0000999 retval = (cptr.cp[0] == cword.c);
1000 break;
1001 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001002 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk5653fc32004-02-08 22:55:38 +00001003 retval = (cptr.wp[0] == cword.w);
1004 break;
1005 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001006 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk5653fc32004-02-08 22:55:38 +00001007 retval = (cptr.lp[0] == cword.l);
1008 break;
1009 case FLASH_CFI_64BIT:
wdenkcd37d9e2004-02-10 00:03:41 +00001010#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +00001011 {
wdenk5653fc32004-02-08 22:55:38 +00001012 char str1[20];
1013 char str2[20];
wdenkbf9e3b32004-02-12 00:47:09 +00001014
1015 print_longlong (str1, cptr.llp[0]);
1016 print_longlong (str2, cword.ll);
1017 debug ("is= %s %s\n", str1, str2);
wdenk5653fc32004-02-08 22:55:38 +00001018 }
1019#endif
1020 retval = (cptr.llp[0] == cword.ll);
1021 break;
1022 default:
1023 retval = 0;
1024 break;
1025 }
1026 return retval;
1027}
wdenkbf9e3b32004-02-12 00:47:09 +00001028
wdenk5653fc32004-02-08 22:55:38 +00001029/*-----------------------------------------------------------------------
1030 */
wdenk028ab6b2004-02-23 23:54:43 +00001031static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +00001032{
1033 cfiptr_t cptr;
1034 cfiword_t cword;
1035 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +00001036
1037 cptr.cp = flash_make_addr (info, sect, offset);
1038 flash_make_cmd (info, cmd, &cword);
1039 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001040 case FLASH_CFI_8BIT:
1041 retval = ((cptr.cp[0] & cword.c) == cword.c);
1042 break;
1043 case FLASH_CFI_16BIT:
1044 retval = ((cptr.wp[0] & cword.w) == cword.w);
1045 break;
1046 case FLASH_CFI_32BIT:
1047 retval = ((cptr.lp[0] & cword.l) == cword.l);
1048 break;
1049 case FLASH_CFI_64BIT:
1050 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenkbf9e3b32004-02-12 00:47:09 +00001051 break;
wdenk5653fc32004-02-08 22:55:38 +00001052 default:
1053 retval = 0;
1054 break;
1055 }
1056 return retval;
1057}
1058
1059/*-----------------------------------------------------------------------
1060 */
wdenk028ab6b2004-02-23 23:54:43 +00001061static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +00001062{
1063 cfiptr_t cptr;
1064 cfiword_t cword;
1065 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +00001066
1067 cptr.cp = flash_make_addr (info, sect, offset);
1068 flash_make_cmd (info, cmd, &cword);
1069 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001070 case FLASH_CFI_8BIT:
1071 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
1072 break;
1073 case FLASH_CFI_16BIT:
1074 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
1075 break;
1076 case FLASH_CFI_32BIT:
1077 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
1078 break;
1079 case FLASH_CFI_64BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001080 retval = ((cptr.llp[0] & cword.ll) !=
1081 (cptr.llp[0] & cword.ll));
wdenk5653fc32004-02-08 22:55:38 +00001082 break;
1083 default:
1084 retval = 0;
1085 break;
1086 }
1087 return retval;
1088}
1089
1090/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +01001091 * read jedec ids from device and set corresponding fields in info struct
1092 *
1093 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1094 *
1095*/
1096static void flash_read_jedec_ids (flash_info_t * info)
1097{
1098 info->manufacturer_id = 0;
1099 info->device_id = 0;
1100 info->device_id2 = 0;
1101
1102 switch (info->vendor) {
1103 case CFI_CMDSET_INTEL_STANDARD:
1104 case CFI_CMDSET_INTEL_EXTENDED:
1105 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1106 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1107 udelay(1000); /* some flash are slow to respond */
1108 info->manufacturer_id = flash_read_uchar (info,
1109 FLASH_OFFSET_MANUFACTURER_ID);
1110 info->device_id = flash_read_uchar (info,
1111 FLASH_OFFSET_DEVICE_ID);
1112 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1113 break;
1114 case CFI_CMDSET_AMD_STANDARD:
1115 case CFI_CMDSET_AMD_EXTENDED:
1116 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1117 flash_unlock_seq(info, 0);
1118 flash_write_cmd(info, 0, AMD_ADDR_START, FLASH_CMD_READ_ID);
1119 udelay(1000); /* some flash are slow to respond */
1120 info->manufacturer_id = flash_read_uchar (info,
1121 FLASH_OFFSET_MANUFACTURER_ID);
1122 info->device_id = flash_read_uchar (info,
1123 FLASH_OFFSET_DEVICE_ID);
1124 if (info->device_id == 0x7E) {
1125 /* AMD 3-byte (expanded) device ids */
1126 info->device_id2 = flash_read_uchar (info,
1127 FLASH_OFFSET_DEVICE_ID2);
1128 info->device_id2 <<= 8;
1129 info->device_id2 |= flash_read_uchar (info,
1130 FLASH_OFFSET_DEVICE_ID3);
1131 }
1132 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1133 break;
1134 default:
1135 break;
1136 }
1137}
1138
1139/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001140 * detect if flash is compatible with the Common Flash Interface (CFI)
1141 * http://www.jedec.org/download/search/jesd68.pdf
1142 *
1143*/
wdenkbf9e3b32004-02-12 00:47:09 +00001144static int flash_detect_cfi (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +00001145{
Wolfgang Denk92eb7292006-12-27 01:26:13 +01001146 int cfi_offset;
wdenkbf9e3b32004-02-12 00:47:09 +00001147 debug ("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +00001148
Stefan Roese79b4cda2006-02-28 15:29:58 +01001149 for (info->portwidth = CFG_FLASH_CFI_WIDTH;
wdenkbf9e3b32004-02-12 00:47:09 +00001150 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1151 for (info->chipwidth = FLASH_CFI_BY8;
1152 info->chipwidth <= info->portwidth;
1153 info->chipwidth <<= 1) {
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001154 flash_write_cmd (info, 0, 0, info->cmd_reset);
Wolfgang Denk92eb7292006-12-27 01:26:13 +01001155 for (cfi_offset=0; cfi_offset < sizeof(flash_offset_cfi)/sizeof(uint); cfi_offset++) {
1156 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], FLASH_CMD_CFI);
1157 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1158 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1159 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1160 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
1161 info->cfi_offset=flash_offset_cfi[cfi_offset];
1162 debug ("device interface is %d\n",
1163 info->interface);
1164 debug ("found port %d chip %d ",
1165 info->portwidth, info->chipwidth);
1166 debug ("port %d bits chip %d bits\n",
1167 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1168 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1169 return 1;
1170 }
wdenk5653fc32004-02-08 22:55:38 +00001171 }
1172 }
1173 }
wdenkbf9e3b32004-02-12 00:47:09 +00001174 debug ("not found\n");
wdenk5653fc32004-02-08 22:55:38 +00001175 return 0;
1176}
wdenkbf9e3b32004-02-12 00:47:09 +00001177
wdenk5653fc32004-02-08 22:55:38 +00001178/*
1179 * The following code cannot be run from FLASH!
1180 *
1181 */
Marian Balakowicze6f2e902005-10-11 19:09:42 +02001182ulong flash_get_size (ulong base, int banknum)
wdenk5653fc32004-02-08 22:55:38 +00001183{
wdenkbf9e3b32004-02-12 00:47:09 +00001184 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +00001185 int i, j;
1186 flash_sect_t sect_cnt;
1187 unsigned long sector;
1188 unsigned long tmp;
1189 int size_ratio;
1190 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +00001191 int erase_region_size;
1192 int erase_region_count;
Stefan Roese260421a2006-11-13 13:55:24 +01001193 int geometry_reversed = 0;
1194
1195 info->ext_addr = 0;
1196 info->cfi_version = 0;
Stefan Roese2662b402006-04-01 13:41:03 +02001197#ifdef CFG_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +02001198 info->legacy_unlock = 0;
1199#endif
wdenk5653fc32004-02-08 22:55:38 +00001200
1201 info->start[0] = base;
1202
wdenkbf9e3b32004-02-12 00:47:09 +00001203 if (flash_detect_cfi (info)) {
Stefan Roese260421a2006-11-13 13:55:24 +01001204 info->vendor = flash_read_ushort (info, 0,
1205 FLASH_OFFSET_PRIMARY_VENDOR);
1206 flash_read_jedec_ids (info);
Stefano Babicd784fdb2006-12-12 00:22:42 +01001207 flash_write_cmd (info, 0, info->cfi_offset, FLASH_CMD_CFI);
Stefan Roese260421a2006-11-13 13:55:24 +01001208 num_erase_regions = flash_read_uchar (info,
1209 FLASH_OFFSET_NUM_ERASE_REGIONS);
1210 info->ext_addr = flash_read_ushort (info, 0,
1211 FLASH_OFFSET_EXT_QUERY_T_P_ADDR);
1212 if (info->ext_addr) {
1213 info->cfi_version = (ushort) flash_read_uchar (info,
1214 info->ext_addr + 3) << 8;
1215 info->cfi_version |= (ushort) flash_read_uchar (info,
1216 info->ext_addr + 4);
1217 }
wdenkbf9e3b32004-02-12 00:47:09 +00001218#ifdef DEBUG
1219 flash_printqry (info, 0);
1220#endif
1221 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001222 case CFI_CMDSET_INTEL_STANDARD:
1223 case CFI_CMDSET_INTEL_EXTENDED:
1224 default:
1225 info->cmd_reset = FLASH_CMD_RESET;
Stefan Roese2662b402006-04-01 13:41:03 +02001226#ifdef CFG_FLASH_PROTECTION
1227 /* read legacy lock/unlock bit from intel flash */
Stefan Roese260421a2006-11-13 13:55:24 +01001228 if (info->ext_addr) {
1229 info->legacy_unlock = flash_read_uchar (info,
1230 info->ext_addr + 5) & 0x08;
1231 }
Stefan Roese2662b402006-04-01 13:41:03 +02001232#endif
wdenk5653fc32004-02-08 22:55:38 +00001233 break;
1234 case CFI_CMDSET_AMD_STANDARD:
1235 case CFI_CMDSET_AMD_EXTENDED:
1236 info->cmd_reset = AMD_CMD_RESET;
Stefan Roese260421a2006-11-13 13:55:24 +01001237 /* check if flash geometry needs reversal */
1238 if (num_erase_regions <= 1)
1239 break;
1240 /* reverse geometry if top boot part */
1241 if (info->cfi_version < 0x3131) {
1242 /* CFI < 1.1, try to guess from device id */
1243 if ((info->device_id & 0x80) != 0) {
1244 geometry_reversed = 1;
1245 }
1246 break;
1247 }
1248 /* CFI >= 1.1, deduct from top/bottom flag */
1249 /* note: ext_addr is valid since cfi_version > 0 */
1250 if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1251 geometry_reversed = 1;
1252 }
wdenk5653fc32004-02-08 22:55:38 +00001253 break;
1254 }
wdenkcd37d9e2004-02-10 00:03:41 +00001255
wdenkbf9e3b32004-02-12 00:47:09 +00001256 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese260421a2006-11-13 13:55:24 +01001257 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1258 debug ("device id is 0x%x\n", info->device_id);
1259 debug ("device id2 is 0x%x\n", info->device_id2);
1260 debug ("cfi version is 0x%04x\n", info->cfi_version);
1261
wdenk5653fc32004-02-08 22:55:38 +00001262 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00001263 /* if the chip is x8/x16 reduce the ratio by half */
1264 if ((info->interface == FLASH_CFI_X8X16)
1265 && (info->chipwidth == FLASH_CFI_BY8)) {
1266 size_ratio >>= 1;
1267 }
wdenkbf9e3b32004-02-12 00:47:09 +00001268 debug ("size_ratio %d port %d bits chip %d bits\n",
1269 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1270 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1271 debug ("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00001272 sect_cnt = 0;
1273 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00001274 for (i = 0; i < num_erase_regions; i++) {
1275 if (i > NUM_ERASE_REGIONS) {
wdenk028ab6b2004-02-23 23:54:43 +00001276 printf ("%d erase regions found, only %d used\n",
1277 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00001278 break;
1279 }
Stefan Roese260421a2006-11-13 13:55:24 +01001280 if (geometry_reversed)
1281 tmp = flash_read_long (info, 0,
1282 FLASH_OFFSET_ERASE_REGIONS +
1283 (num_erase_regions - 1 - i) * 4);
1284 else
1285 tmp = flash_read_long (info, 0,
wdenkbf9e3b32004-02-12 00:47:09 +00001286 FLASH_OFFSET_ERASE_REGIONS +
1287 i * 4);
1288 erase_region_size =
1289 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk5653fc32004-02-08 22:55:38 +00001290 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00001291 erase_region_count = (tmp & 0xffff) + 1;
wdenk4c0d4c32004-06-09 17:34:58 +00001292 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenk028ab6b2004-02-23 23:54:43 +00001293 erase_region_count, erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00001294 for (j = 0; j < erase_region_count; j++) {
wdenk5653fc32004-02-08 22:55:38 +00001295 info->start[sect_cnt] = sector;
1296 sector += (erase_region_size * size_ratio);
wdenka1191902005-01-09 17:12:27 +00001297
1298 /*
1299 * Only read protection status from supported devices (intel...)
1300 */
1301 switch (info->vendor) {
1302 case CFI_CMDSET_INTEL_EXTENDED:
1303 case CFI_CMDSET_INTEL_STANDARD:
1304 info->protect[sect_cnt] =
1305 flash_isset (info, sect_cnt,
1306 FLASH_OFFSET_PROTECT,
1307 FLASH_STATUS_PROTECT);
1308 break;
1309 default:
1310 info->protect[sect_cnt] = 0; /* default: not protected */
1311 }
1312
wdenk5653fc32004-02-08 22:55:38 +00001313 sect_cnt++;
1314 }
1315 }
1316
1317 info->sector_count = sect_cnt;
1318 /* multiply the size by the number of chips */
wdenk028ab6b2004-02-23 23:54:43 +00001319 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1320 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
wdenkbf9e3b32004-02-12 00:47:09 +00001321 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
wdenk028ab6b2004-02-23 23:54:43 +00001322 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
Stefan Roese2662b402006-04-01 13:41:03 +02001323 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT)) *
1324 (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT));
1325 info->buffer_write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
Stefan Roese79b4cda2006-02-28 15:29:58 +01001326 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
1327 (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
1328 info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
wdenk5653fc32004-02-08 22:55:38 +00001329 info->flash_id = FLASH_MAN_CFI;
wdenk855a4962004-03-14 18:23:55 +00001330 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1331 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1332 }
wdenk5653fc32004-02-08 22:55:38 +00001333 }
1334
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001335 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenkbf9e3b32004-02-12 00:47:09 +00001336 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00001337}
1338
Stefan Roese79b4cda2006-02-28 15:29:58 +01001339/* loop through the sectors from the highest address
1340 * when the passed address is greater or equal to the sector address
1341 * we have a match
1342 */
1343static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1344{
1345 flash_sect_t sector;
1346
1347 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1348 if (addr >= info->start[sector])
1349 break;
1350 }
1351 return sector;
1352}
wdenk5653fc32004-02-08 22:55:38 +00001353
1354/*-----------------------------------------------------------------------
1355 */
wdenkbf9e3b32004-02-12 00:47:09 +00001356static int flash_write_cfiword (flash_info_t * info, ulong dest,
1357 cfiword_t cword)
wdenk5653fc32004-02-08 22:55:38 +00001358{
wdenk5653fc32004-02-08 22:55:38 +00001359 cfiptr_t ctladdr;
1360 cfiptr_t cptr;
1361 int flag;
1362
wdenkbf9e3b32004-02-12 00:47:09 +00001363 ctladdr.cp = flash_make_addr (info, 0, 0);
1364 cptr.cp = (uchar *) dest;
wdenk5653fc32004-02-08 22:55:38 +00001365
wdenk5653fc32004-02-08 22:55:38 +00001366 /* Check if Flash is (sufficiently) erased */
wdenkbf9e3b32004-02-12 00:47:09 +00001367 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001368 case FLASH_CFI_8BIT:
1369 flag = ((cptr.cp[0] & cword.c) == cword.c);
1370 break;
1371 case FLASH_CFI_16BIT:
1372 flag = ((cptr.wp[0] & cword.w) == cword.w);
1373 break;
1374 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001375 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk5653fc32004-02-08 22:55:38 +00001376 break;
1377 case FLASH_CFI_64BIT:
wdenke1599e82004-10-10 23:27:33 +00001378 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenk5653fc32004-02-08 22:55:38 +00001379 break;
1380 default:
1381 return 2;
1382 }
wdenkbf9e3b32004-02-12 00:47:09 +00001383 if (!flag)
wdenk5653fc32004-02-08 22:55:38 +00001384 return 2;
1385
1386 /* Disable interrupts which might cause a timeout here */
wdenkbf9e3b32004-02-12 00:47:09 +00001387 flag = disable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001388
wdenkbf9e3b32004-02-12 00:47:09 +00001389 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001390 case CFI_CMDSET_INTEL_EXTENDED:
1391 case CFI_CMDSET_INTEL_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001392 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1393 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001394 break;
1395 case CFI_CMDSET_AMD_EXTENDED:
1396 case CFI_CMDSET_AMD_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001397 flash_unlock_seq (info, 0);
wdenk855a4962004-03-14 18:23:55 +00001398 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001399 break;
1400 }
1401
wdenkbf9e3b32004-02-12 00:47:09 +00001402 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001403 case FLASH_CFI_8BIT:
1404 cptr.cp[0] = cword.c;
1405 break;
1406 case FLASH_CFI_16BIT:
1407 cptr.wp[0] = cword.w;
1408 break;
1409 case FLASH_CFI_32BIT:
1410 cptr.lp[0] = cword.l;
1411 break;
1412 case FLASH_CFI_64BIT:
1413 cptr.llp[0] = cword.ll;
1414 break;
1415 }
1416
1417 /* re-enable interrupts if necessary */
wdenkbf9e3b32004-02-12 00:47:09 +00001418 if (flag)
1419 enable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001420
Stefan Roese79b4cda2006-02-28 15:29:58 +01001421 return flash_full_status_check (info, find_sector (info, dest),
1422 info->write_tout, "write");
wdenk5653fc32004-02-08 22:55:38 +00001423}
1424
1425#ifdef CFG_FLASH_USE_BUFFER_WRITE
1426
wdenkbf9e3b32004-02-12 00:47:09 +00001427static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1428 int len)
wdenk5653fc32004-02-08 22:55:38 +00001429{
1430 flash_sect_t sector;
1431 int cnt;
1432 int retcode;
1433 volatile cfiptr_t src;
1434 volatile cfiptr_t dst;
1435
Stefan Roese79b4cda2006-02-28 15:29:58 +01001436 switch (info->vendor) {
1437 case CFI_CMDSET_INTEL_STANDARD:
1438 case CFI_CMDSET_INTEL_EXTENDED:
1439 src.cp = cp;
1440 dst.cp = (uchar *) dest;
1441 sector = find_sector (info, dest);
1442 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1443 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1444 if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
1445 "write to buffer")) == ERR_OK) {
1446 /* reduce the number of loops by the width of the port */
wdenkbf9e3b32004-02-12 00:47:09 +00001447 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001448 case FLASH_CFI_8BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001449 cnt = len;
wdenk5653fc32004-02-08 22:55:38 +00001450 break;
1451 case FLASH_CFI_16BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001452 cnt = len >> 1;
wdenk5653fc32004-02-08 22:55:38 +00001453 break;
1454 case FLASH_CFI_32BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001455 cnt = len >> 2;
wdenk5653fc32004-02-08 22:55:38 +00001456 break;
1457 case FLASH_CFI_64BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001458 cnt = len >> 3;
wdenk5653fc32004-02-08 22:55:38 +00001459 break;
1460 default:
1461 return ERR_INVAL;
1462 break;
1463 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01001464 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1465 while (cnt-- > 0) {
1466 switch (info->portwidth) {
1467 case FLASH_CFI_8BIT:
1468 *dst.cp++ = *src.cp++;
1469 break;
1470 case FLASH_CFI_16BIT:
1471 *dst.wp++ = *src.wp++;
1472 break;
1473 case FLASH_CFI_32BIT:
1474 *dst.lp++ = *src.lp++;
1475 break;
1476 case FLASH_CFI_64BIT:
1477 *dst.llp++ = *src.llp++;
1478 break;
1479 default:
1480 return ERR_INVAL;
1481 break;
1482 }
1483 }
1484 flash_write_cmd (info, sector, 0,
1485 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1486 retcode = flash_full_status_check (info, sector,
1487 info->buffer_write_tout,
1488 "buffer write");
wdenk5653fc32004-02-08 22:55:38 +00001489 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01001490 return retcode;
1491
1492 case CFI_CMDSET_AMD_STANDARD:
1493 case CFI_CMDSET_AMD_EXTENDED:
1494 src.cp = cp;
1495 dst.cp = (uchar *) dest;
1496 sector = find_sector (info, dest);
1497
1498 flash_unlock_seq(info,0);
1499 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
1500
1501 switch (info->portwidth) {
1502 case FLASH_CFI_8BIT:
1503 cnt = len;
1504 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1505 while (cnt-- > 0) *dst.cp++ = *src.cp++;
1506 break;
1507 case FLASH_CFI_16BIT:
1508 cnt = len >> 1;
1509 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1510 while (cnt-- > 0) *dst.wp++ = *src.wp++;
1511 break;
1512 case FLASH_CFI_32BIT:
1513 cnt = len >> 2;
1514 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1515 while (cnt-- > 0) *dst.lp++ = *src.lp++;
1516 break;
1517 case FLASH_CFI_64BIT:
1518 cnt = len >> 3;
1519 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1520 while (cnt-- > 0) *dst.llp++ = *src.llp++;
1521 break;
1522 default:
1523 return ERR_INVAL;
1524 }
1525
1526 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
1527 retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
1528 "buffer write");
1529 return retcode;
1530
1531 default:
1532 debug ("Unknown Command Set\n");
1533 return ERR_INVAL;
wdenk5653fc32004-02-08 22:55:38 +00001534 }
wdenk5653fc32004-02-08 22:55:38 +00001535}
wdenkcce625e2004-09-28 19:00:19 +00001536#endif /* CFG_FLASH_USE_BUFFER_WRITE */
Heiko Schocherca43ba12007-01-11 15:44:44 +01001537
1538#if defined(CONFIG_SOLIDCARD3)
1539#undef __LITTLE_ENDIAN
1540#endif
1541
wdenk5653fc32004-02-08 22:55:38 +00001542#endif /* CFG_FLASH_CFI */