blob: c73b74a4fddbd65d67556d7c34e007ffa4009172 [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>
Haiying Wang3a197b22007-02-21 16:52:31 +010039#include <asm/io.h>
wdenk4c0d4c32004-06-09 17:34:58 +000040#include <asm/byteorder.h>
wdenk2a8af182005-04-13 10:02:42 +000041#include <environment.h>
wdenkbf9e3b32004-02-12 00:47:09 +000042#ifdef CFG_FLASH_CFI_DRIVER
wdenk028ab6b2004-02-23 23:54:43 +000043
wdenk5653fc32004-02-08 22:55:38 +000044/*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010045 * This file implements a Common Flash Interface (CFI) driver for
46 * U-Boot.
47 *
48 * The width of the port and the width of the chips are determined at
49 * initialization. These widths are used to calculate the address for
50 * 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 *
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010061 * Define CFG_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocherd0b6e142007-01-19 18:05:26 +010062 * reading and writing ... (yes there is such a Hardware).
wdenk5653fc32004-02-08 22:55:38 +000063 */
64
wdenkbf9e3b32004-02-12 00:47:09 +000065#ifndef CFG_FLASH_BANKS_LIST
66#define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
67#endif
68
wdenk5653fc32004-02-08 22:55:38 +000069#define FLASH_CMD_CFI 0x98
70#define FLASH_CMD_READ_ID 0x90
71#define FLASH_CMD_RESET 0xff
72#define FLASH_CMD_BLOCK_ERASE 0x20
73#define FLASH_CMD_ERASE_CONFIRM 0xD0
74#define FLASH_CMD_WRITE 0x40
75#define FLASH_CMD_PROTECT 0x60
76#define FLASH_CMD_PROTECT_SET 0x01
77#define FLASH_CMD_PROTECT_CLEAR 0xD0
78#define FLASH_CMD_CLEAR_STATUS 0x50
wdenkbf9e3b32004-02-12 00:47:09 +000079#define FLASH_CMD_WRITE_TO_BUFFER 0xE8
80#define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
wdenk5653fc32004-02-08 22:55:38 +000081
82#define FLASH_STATUS_DONE 0x80
83#define FLASH_STATUS_ESS 0x40
84#define FLASH_STATUS_ECLBS 0x20
85#define FLASH_STATUS_PSLBS 0x10
86#define FLASH_STATUS_VPENS 0x08
87#define FLASH_STATUS_PSS 0x04
88#define FLASH_STATUS_DPS 0x02
89#define FLASH_STATUS_R 0x01
90#define FLASH_STATUS_PROTECT 0x01
91
92#define AMD_CMD_RESET 0xF0
93#define AMD_CMD_WRITE 0xA0
94#define AMD_CMD_ERASE_START 0x80
95#define AMD_CMD_ERASE_SECTOR 0x30
wdenk855a4962004-03-14 18:23:55 +000096#define AMD_CMD_UNLOCK_START 0xAA
97#define AMD_CMD_UNLOCK_ACK 0x55
Stefan Roese79b4cda2006-02-28 15:29:58 +010098#define AMD_CMD_WRITE_TO_BUFFER 0x25
99#define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
wdenk5653fc32004-02-08 22:55:38 +0000100
101#define AMD_STATUS_TOGGLE 0x40
102#define AMD_STATUS_ERROR 0x20
Stefan Roese79b4cda2006-02-28 15:29:58 +0100103
Stefan Roese260421a2006-11-13 13:55:24 +0100104#define FLASH_OFFSET_MANUFACTURER_ID 0x00
105#define FLASH_OFFSET_DEVICE_ID 0x01
106#define FLASH_OFFSET_DEVICE_ID2 0x0E
107#define FLASH_OFFSET_DEVICE_ID3 0x0F
wdenk5653fc32004-02-08 22:55:38 +0000108#define FLASH_OFFSET_CFI 0x55
Wolfgang Denk92eb7292006-12-27 01:26:13 +0100109#define FLASH_OFFSET_CFI_ALT 0x555
wdenk5653fc32004-02-08 22:55:38 +0000110#define FLASH_OFFSET_CFI_RESP 0x10
wdenkbf9e3b32004-02-12 00:47:09 +0000111#define FLASH_OFFSET_PRIMARY_VENDOR 0x13
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100112/* extended query table primary address */
113#define FLASH_OFFSET_EXT_QUERY_T_P_ADDR 0x15
wdenk5653fc32004-02-08 22:55:38 +0000114#define FLASH_OFFSET_WTOUT 0x1F
wdenkbf9e3b32004-02-12 00:47:09 +0000115#define FLASH_OFFSET_WBTOUT 0x20
wdenk5653fc32004-02-08 22:55:38 +0000116#define FLASH_OFFSET_ETOUT 0x21
wdenkbf9e3b32004-02-12 00:47:09 +0000117#define FLASH_OFFSET_CETOUT 0x22
wdenk5653fc32004-02-08 22:55:38 +0000118#define FLASH_OFFSET_WMAX_TOUT 0x23
wdenkbf9e3b32004-02-12 00:47:09 +0000119#define FLASH_OFFSET_WBMAX_TOUT 0x24
wdenk5653fc32004-02-08 22:55:38 +0000120#define FLASH_OFFSET_EMAX_TOUT 0x25
wdenkbf9e3b32004-02-12 00:47:09 +0000121#define FLASH_OFFSET_CEMAX_TOUT 0x26
wdenk5653fc32004-02-08 22:55:38 +0000122#define FLASH_OFFSET_SIZE 0x27
wdenkbf9e3b32004-02-12 00:47:09 +0000123#define FLASH_OFFSET_INTERFACE 0x28
124#define FLASH_OFFSET_BUFFER_SIZE 0x2A
wdenk5653fc32004-02-08 22:55:38 +0000125#define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
126#define FLASH_OFFSET_ERASE_REGIONS 0x2D
127#define FLASH_OFFSET_PROTECT 0x02
wdenkbf9e3b32004-02-12 00:47:09 +0000128#define FLASH_OFFSET_USER_PROTECTION 0x85
129#define FLASH_OFFSET_INTEL_PROTECTION 0x81
wdenk5653fc32004-02-08 22:55:38 +0000130
Stefan Roese260421a2006-11-13 13:55:24 +0100131#define CFI_CMDSET_NONE 0
132#define CFI_CMDSET_INTEL_EXTENDED 1
133#define CFI_CMDSET_AMD_STANDARD 2
134#define CFI_CMDSET_INTEL_STANDARD 3
135#define CFI_CMDSET_AMD_EXTENDED 4
136#define CFI_CMDSET_MITSU_STANDARD 256
137#define CFI_CMDSET_MITSU_EXTENDED 257
138#define CFI_CMDSET_SST 258
wdenk5653fc32004-02-08 22:55:38 +0000139
wdenkf7d15722004-12-18 22:35:43 +0000140#ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
141# undef FLASH_CMD_RESET
Stefan Roese260421a2006-11-13 13:55:24 +0100142# define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
wdenkf7d15722004-12-18 22:35:43 +0000143#endif
144
wdenk5653fc32004-02-08 22:55:38 +0000145typedef union {
146 unsigned char c;
147 unsigned short w;
148 unsigned long l;
149 unsigned long long ll;
150} cfiword_t;
151
152typedef union {
wdenkbf9e3b32004-02-12 00:47:09 +0000153 volatile unsigned char *cp;
wdenk5653fc32004-02-08 22:55:38 +0000154 volatile unsigned short *wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000155 volatile unsigned long *lp;
wdenk5653fc32004-02-08 22:55:38 +0000156 volatile unsigned long long *llp;
157} cfiptr_t;
158
Stefan Roese260421a2006-11-13 13:55:24 +0100159#define NUM_ERASE_REGIONS 4 /* max. number of erase regions */
wdenk5653fc32004-02-08 22:55:38 +0000160
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100161static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Wolfgang Denk92eb7292006-12-27 01:26:13 +0100162
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200163/* use CFG_MAX_FLASH_BANKS_DETECT if defined */
164#ifdef CFG_MAX_FLASH_BANKS_DETECT
165static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
166flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
167#else
wdenk5653fc32004-02-08 22:55:38 +0000168static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200169flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
170#endif
wdenk5653fc32004-02-08 22:55:38 +0000171
Stefan Roese79b4cda2006-02-28 15:29:58 +0100172/*
173 * Check if chip width is defined. If not, start detecting with 8bit.
174 */
175#ifndef CFG_FLASH_CFI_WIDTH
176#define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
177#endif
178
wdenk5653fc32004-02-08 22:55:38 +0000179
180/*-----------------------------------------------------------------------
181 * Functions
182 */
183
184typedef unsigned long flash_sect_t;
185
wdenkbf9e3b32004-02-12 00:47:09 +0000186static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
187static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100188static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
189 uint offset, uchar cmd);
wdenkbf9e3b32004-02-12 00:47:09 +0000190static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100191static int flash_isequal (flash_info_t * info, flash_sect_t sect,
192 uint offset, uchar cmd);
193static int flash_isset (flash_info_t * info, flash_sect_t sect,
194 uint offset, uchar cmd);
195static int flash_toggle (flash_info_t * info, flash_sect_t sect,
196 uint offset, uchar cmd);
Stefan Roese260421a2006-11-13 13:55:24 +0100197static void flash_read_jedec_ids (flash_info_t * info);
wdenkbf9e3b32004-02-12 00:47:09 +0000198static int flash_detect_cfi (flash_info_t * info);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100199static int flash_write_cfiword (flash_info_t * info, ulong dest,
200 cfiword_t cword);
wdenkbf9e3b32004-02-12 00:47:09 +0000201static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
202 ulong tout, char *prompt);
Stefan Roesef18e8742006-03-01 17:00:49 +0100203ulong flash_get_size (ulong base, int banknum);
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200204#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk7680c142005-05-16 15:23:22 +0000205static flash_info_t *flash_get_info(ulong base);
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200206#endif
wdenk5653fc32004-02-08 22:55:38 +0000207#ifdef CFG_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100208static int flash_write_cfibuffer (flash_info_t * info, ulong dest,
209 uchar * cp, int len);
wdenk5653fc32004-02-08 22:55:38 +0000210#endif
211
wdenk5653fc32004-02-08 22:55:38 +0000212/*-----------------------------------------------------------------------
213 * create an address based on the offset and the port width
214 */
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100215inline uchar *
216flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000217{
wdenkbf9e3b32004-02-12 00:47:09 +0000218 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
wdenk5653fc32004-02-08 22:55:38 +0000219}
wdenkbf9e3b32004-02-12 00:47:09 +0000220
221#ifdef DEBUG
222/*-----------------------------------------------------------------------
223 * Debug support
224 */
225void print_longlong (char *str, unsigned long long data)
226{
227 int i;
228 char *cp;
229
230 cp = (unsigned char *) &data;
231 for (i = 0; i < 8; i++)
232 sprintf (&str[i * 2], "%2.2x", *cp++);
233}
234static void flash_printqry (flash_info_t * info, flash_sect_t sect)
235{
236 cfiptr_t cptr;
237 int x, y;
238
Wolfgang Denk47340a42005-10-09 00:25:58 +0200239 for (x = 0; x < 0x40; x += 16U / info->portwidth) {
wdenkbf9e3b32004-02-12 00:47:09 +0000240 cptr.cp =
241 flash_make_addr (info, sect,
242 x + FLASH_OFFSET_CFI_RESP);
243 debug ("%p : ", cptr.cp);
244 for (y = 0; y < 16; y++) {
245 debug ("%2.2x ", cptr.cp[y]);
246 }
247 debug (" ");
248 for (y = 0; y < 16; y++) {
249 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
250 debug ("%c", cptr.cp[y]);
251 } else {
252 debug (".");
253 }
254 }
255 debug ("\n");
256 }
257}
wdenkbf9e3b32004-02-12 00:47:09 +0000258#endif
259
260
wdenk5653fc32004-02-08 22:55:38 +0000261/*-----------------------------------------------------------------------
262 * read a character at a port width address
263 */
wdenkbf9e3b32004-02-12 00:47:09 +0000264inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000265{
266 uchar *cp;
wdenkbf9e3b32004-02-12 00:47:09 +0000267
268 cp = flash_make_addr (info, 0, offset);
Heiko Schocherd0b6e142007-01-19 18:05:26 +0100269#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenkbf9e3b32004-02-12 00:47:09 +0000270 return (cp[0]);
271#else
wdenk5653fc32004-02-08 22:55:38 +0000272 return (cp[info->portwidth - 1]);
wdenkbf9e3b32004-02-12 00:47:09 +0000273#endif
wdenk5653fc32004-02-08 22:55:38 +0000274}
275
276/*-----------------------------------------------------------------------
277 * read a short word by swapping for ppc format.
278 */
wdenkbf9e3b32004-02-12 00:47:09 +0000279ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000280{
wdenkbf9e3b32004-02-12 00:47:09 +0000281 uchar *addr;
282 ushort retval;
wdenk5653fc32004-02-08 22:55:38 +0000283
wdenkbf9e3b32004-02-12 00:47:09 +0000284#ifdef DEBUG
285 int x;
286#endif
287 addr = flash_make_addr (info, sect, offset);
wdenk5653fc32004-02-08 22:55:38 +0000288
wdenkbf9e3b32004-02-12 00:47:09 +0000289#ifdef DEBUG
290 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
291 info->portwidth);
292 for (x = 0; x < 2 * info->portwidth; x++) {
293 debug ("addr[%x] = 0x%x\n", x, addr[x]);
294 }
295#endif
Heiko Schocherd0b6e142007-01-19 18:05:26 +0100296#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenkbf9e3b32004-02-12 00:47:09 +0000297 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
298#else
299 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
300 addr[info->portwidth - 1]);
301#endif
302
303 debug ("retval = 0x%x\n", retval);
304 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000305}
306
307/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +0100308 * read a long word by picking the least significant byte of each maximum
wdenk5653fc32004-02-08 22:55:38 +0000309 * port size word. Swap for ppc format.
310 */
wdenkbf9e3b32004-02-12 00:47:09 +0000311ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000312{
wdenkbf9e3b32004-02-12 00:47:09 +0000313 uchar *addr;
314 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000315
wdenkbf9e3b32004-02-12 00:47:09 +0000316#ifdef DEBUG
317 int x;
318#endif
319 addr = flash_make_addr (info, sect, offset);
320
321#ifdef DEBUG
322 debug ("long addr is at %p info->portwidth = %d\n", addr,
323 info->portwidth);
324 for (x = 0; x < 4 * info->portwidth; x++) {
325 debug ("addr[%x] = 0x%x\n", x, addr[x]);
326 }
327#endif
Heiko Schocherd0b6e142007-01-19 18:05:26 +0100328#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
wdenkbf9e3b32004-02-12 00:47:09 +0000329 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100330 (addr[(2 * info->portwidth)]) |
331 (addr[(3 * info->portwidth)] << 8);
wdenkbf9e3b32004-02-12 00:47:09 +0000332#else
333 retval = (addr[(2 * info->portwidth) - 1] << 24) |
334 (addr[(info->portwidth) - 1] << 16) |
335 (addr[(4 * info->portwidth) - 1] << 8) |
336 addr[(3 * info->portwidth) - 1];
337#endif
338 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000339}
340
Stefan Roese79b4cda2006-02-28 15:29:58 +0100341
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100342#ifdef CONFIG_FLASH_CFI_LEGACY
343/*-----------------------------------------------------------------------
344 * Call board code to request info about non-CFI flash.
345 * board_flash_get_legacy needs to fill in at least:
346 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
347 */
348int flash_detect_legacy(ulong base, int banknum)
349{
350 flash_info_t *info = &flash_info[banknum];
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100351
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100352 if (board_flash_get_legacy(base, banknum, info)) {
353 /* board code may have filled info completely. If not, we
354 use JEDEC ID probing. */
355 if (!info->vendor) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100356 int modes[] = {
357 CFI_CMDSET_AMD_STANDARD,
358 CFI_CMDSET_INTEL_STANDARD
359 };
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100360 int i;
361
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100362 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100363 info->vendor = modes[i];
364 info->start[0] = base;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100365 if (info->portwidth == FLASH_CFI_8BIT
366 && info->interface == FLASH_CFI_X8X16) {
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100367 info->addr_unlock1 = 0x2AAA;
368 info->addr_unlock2 = 0x5555;
369 } else {
370 info->addr_unlock1 = 0x5555;
371 info->addr_unlock2 = 0x2AAA;
372 }
373 flash_read_jedec_ids(info);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100374 debug("JEDEC PROBE: ID %x %x %x\n",
375 info->manufacturer_id,
376 info->device_id,
377 info->device_id2);
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100378 if (jedec_flash_match(info, base))
379 break;
380 }
381 }
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100382
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100383 switch(info->vendor) {
384 case CFI_CMDSET_INTEL_STANDARD:
385 case CFI_CMDSET_INTEL_EXTENDED:
386 info->cmd_reset = FLASH_CMD_RESET;
387 break;
388 case CFI_CMDSET_AMD_STANDARD:
389 case CFI_CMDSET_AMD_EXTENDED:
390 case CFI_CMDSET_AMD_LEGACY:
391 info->cmd_reset = AMD_CMD_RESET;
392 break;
393 }
394 info->flash_id = FLASH_MAN_CFI;
395 return 1;
396 }
397 return 0; /* use CFI */
398}
399#else
400int inline flash_detect_legacy(ulong base, int banknum)
401{
402 return 0; /* use CFI */
403}
404#endif
405
406
wdenk5653fc32004-02-08 22:55:38 +0000407/*-----------------------------------------------------------------------
408 */
409unsigned long flash_init (void)
410{
411 unsigned long size = 0;
412 int i;
413
Stefan Roese2662b402006-04-01 13:41:03 +0200414#ifdef CFG_FLASH_PROTECTION
415 char *s = getenv("unlock");
416#endif
417
wdenk5653fc32004-02-08 22:55:38 +0000418 /* Init: no FLASHes known */
wdenkbf9e3b32004-02-12 00:47:09 +0000419 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenk5653fc32004-02-08 22:55:38 +0000420 flash_info[i].flash_id = FLASH_UNKNOWN;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100421
422 if (!flash_detect_legacy (bank_base[i], i))
423 flash_get_size (bank_base[i], i);
424 size += flash_info[i].size;
wdenk5653fc32004-02-08 22:55:38 +0000425 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Stefan Roese5568e612005-11-22 13:20:42 +0100426#ifndef CFG_FLASH_QUIET_TEST
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100427 printf ("## Unknown FLASH on Bank %d "
428 "- Size = 0x%08lx = %ld MB\n",
429 i+1, flash_info[i].size,
430 flash_info[i].size << 20);
Stefan Roese5568e612005-11-22 13:20:42 +0100431#endif /* CFG_FLASH_QUIET_TEST */
wdenk5653fc32004-02-08 22:55:38 +0000432 }
Stefan Roese79b4cda2006-02-28 15:29:58 +0100433#ifdef CFG_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +0200434 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
435 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100436 * Only the U-Boot image and it's environment
437 * is protected, all other sectors are
438 * unprotected (unlocked) if flash hardware
439 * protection is used (CFG_FLASH_PROTECTION)
440 * and the environment variable "unlock" is
441 * set to "yes".
Stefan Roese2662b402006-04-01 13:41:03 +0200442 */
443 if (flash_info[i].legacy_unlock) {
444 int k;
Stefan Roese79b4cda2006-02-28 15:29:58 +0100445
Stefan Roese79b4cda2006-02-28 15:29:58 +0100446 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100447 * Disable legacy_unlock temporarily,
448 * since flash_real_protect would
449 * relock all other sectors again
450 * otherwise.
Stefan Roese2662b402006-04-01 13:41:03 +0200451 */
452 flash_info[i].legacy_unlock = 0;
453
454 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100455 * Legacy unlocking (e.g. Intel J3) ->
456 * unlock only one sector. This will
457 * unlock all sectors.
Stefan Roese2662b402006-04-01 13:41:03 +0200458 */
459 flash_real_protect (&flash_info[i], 0, 0);
460
461 flash_info[i].legacy_unlock = 1;
462
463 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100464 * Manually mark other sectors as
465 * unlocked (unprotected)
Stefan Roese2662b402006-04-01 13:41:03 +0200466 */
467 for (k = 1; k < flash_info[i].sector_count; k++)
468 flash_info[i].protect[k] = 0;
469 } else {
470 /*
471 * No legancy unlocking -> unlock all sectors
Stefan Roese79b4cda2006-02-28 15:29:58 +0100472 */
473 flash_protect (FLAG_PROTECT_CLEAR,
474 flash_info[i].start[0],
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100475 flash_info[i].start[0]
476 + flash_info[i].size - 1,
Stefan Roese79b4cda2006-02-28 15:29:58 +0100477 &flash_info[i]);
478 }
479 }
480#endif /* CFG_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +0000481 }
482
483 /* Monitor protection ON by default */
484#if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenkbf9e3b32004-02-12 00:47:09 +0000485 flash_protect (FLAG_PROTECT_SET,
486 CFG_MONITOR_BASE,
wdenk7680c142005-05-16 15:23:22 +0000487 CFG_MONITOR_BASE + monitor_flash_len - 1,
488 flash_get_info(CFG_MONITOR_BASE));
wdenk5653fc32004-02-08 22:55:38 +0000489#endif
490
wdenk656658d2004-10-10 22:16:06 +0000491 /* Environment protection ON by default */
492#ifdef CFG_ENV_IS_IN_FLASH
493 flash_protect (FLAG_PROTECT_SET,
494 CFG_ENV_ADDR,
495 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
wdenk7680c142005-05-16 15:23:22 +0000496 flash_get_info(CFG_ENV_ADDR));
wdenk656658d2004-10-10 22:16:06 +0000497#endif
498
499 /* Redundant environment protection ON by default */
500#ifdef CFG_ENV_ADDR_REDUND
501 flash_protect (FLAG_PROTECT_SET,
502 CFG_ENV_ADDR_REDUND,
503 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
wdenk7680c142005-05-16 15:23:22 +0000504 flash_get_info(CFG_ENV_ADDR_REDUND));
wdenk656658d2004-10-10 22:16:06 +0000505#endif
wdenk5653fc32004-02-08 22:55:38 +0000506 return (size);
507}
508
509/*-----------------------------------------------------------------------
510 */
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200511#if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
wdenk7680c142005-05-16 15:23:22 +0000512static flash_info_t *flash_get_info(ulong base)
513{
514 int i;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200515 flash_info_t * info = 0;
wdenk7680c142005-05-16 15:23:22 +0000516
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100517 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
wdenk7680c142005-05-16 15:23:22 +0000518 info = & flash_info[i];
519 if (info->size && info->start[0] <= base &&
520 base <= info->start[0] + info->size - 1)
521 break;
522 }
523
524 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
525}
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200526#endif
wdenk7680c142005-05-16 15:23:22 +0000527
528/*-----------------------------------------------------------------------
529 */
wdenkbf9e3b32004-02-12 00:47:09 +0000530int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +0000531{
532 int rcode = 0;
533 int prot;
534 flash_sect_t sect;
535
wdenkbf9e3b32004-02-12 00:47:09 +0000536 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +0000537 puts ("Can't erase unknown flash type - aborted\n");
wdenk5653fc32004-02-08 22:55:38 +0000538 return 1;
539 }
540 if ((s_first < 0) || (s_first > s_last)) {
wdenk4b9206e2004-03-23 22:14:11 +0000541 puts ("- no sectors to erase\n");
wdenk5653fc32004-02-08 22:55:38 +0000542 return 1;
543 }
544
545 prot = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000546 for (sect = s_first; sect <= s_last; ++sect) {
wdenk5653fc32004-02-08 22:55:38 +0000547 if (info->protect[sect]) {
548 prot++;
549 }
550 }
551 if (prot) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100552 printf ("- Warning: %d protected sectors will not be erased!\n",
553 prot);
wdenk5653fc32004-02-08 22:55:38 +0000554 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000555 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +0000556 }
557
558
wdenkbf9e3b32004-02-12 00:47:09 +0000559 for (sect = s_first; sect <= s_last; sect++) {
wdenk5653fc32004-02-08 22:55:38 +0000560 if (info->protect[sect] == 0) { /* not protected */
wdenkbf9e3b32004-02-12 00:47:09 +0000561 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000562 case CFI_CMDSET_INTEL_STANDARD:
563 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100564 flash_write_cmd (info, sect, 0,
565 FLASH_CMD_CLEAR_STATUS);
566 flash_write_cmd (info, sect, 0,
567 FLASH_CMD_BLOCK_ERASE);
568 flash_write_cmd (info, sect, 0,
569 FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +0000570 break;
571 case CFI_CMDSET_AMD_STANDARD:
572 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000573 flash_unlock_seq (info, sect);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100574 flash_write_cmd (info, sect,
575 info->addr_unlock1,
576 AMD_CMD_ERASE_START);
wdenkbf9e3b32004-02-12 00:47:09 +0000577 flash_unlock_seq (info, sect);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100578 flash_write_cmd (info, sect, 0,
579 AMD_CMD_ERASE_SECTOR);
wdenk5653fc32004-02-08 22:55:38 +0000580 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100581#ifdef CONFIG_FLASH_CFI_LEGACY
582 case CFI_CMDSET_AMD_LEGACY:
583 flash_unlock_seq (info, 0);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100584 flash_write_cmd (info, 0, info->addr_unlock1,
585 AMD_CMD_ERASE_START);
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100586 flash_unlock_seq (info, 0);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100587 flash_write_cmd (info, sect, 0,
588 AMD_CMD_ERASE_SECTOR);
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100589 break;
590#endif
wdenk5653fc32004-02-08 22:55:38 +0000591 default:
wdenkbf9e3b32004-02-12 00:47:09 +0000592 debug ("Unkown flash vendor %d\n",
593 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +0000594 break;
595 }
596
wdenkbf9e3b32004-02-12 00:47:09 +0000597 if (flash_full_status_check
598 (info, sect, info->erase_blk_tout, "erase")) {
wdenk5653fc32004-02-08 22:55:38 +0000599 rcode = 1;
600 } else
wdenk4b9206e2004-03-23 22:14:11 +0000601 putc ('.');
wdenk5653fc32004-02-08 22:55:38 +0000602 }
603 }
wdenk4b9206e2004-03-23 22:14:11 +0000604 puts (" done\n");
wdenk5653fc32004-02-08 22:55:38 +0000605 return rcode;
606}
607
608/*-----------------------------------------------------------------------
609 */
wdenkbf9e3b32004-02-12 00:47:09 +0000610void flash_print_info (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +0000611{
612 int i;
613
614 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +0000615 puts ("missing or unknown FLASH type\n");
wdenk5653fc32004-02-08 22:55:38 +0000616 return;
617 }
618
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100619 printf ("%s FLASH (%d x %d)",
620 info->name,
wdenkbf9e3b32004-02-12 00:47:09 +0000621 (info->portwidth << 3), (info->chipwidth << 3));
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100622 if (info->size < 1024*1024)
623 printf (" Size: %ld kB in %d Sectors\n",
624 info->size >> 10, info->sector_count);
625 else
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100626 printf (" Size: %ld MB in %d Sectors\n",
627 info->size >> 20, info->sector_count);
Stefan Roese260421a2006-11-13 13:55:24 +0100628 printf (" ");
629 switch (info->vendor) {
630 case CFI_CMDSET_INTEL_STANDARD:
631 printf ("Intel Standard");
632 break;
633 case CFI_CMDSET_INTEL_EXTENDED:
634 printf ("Intel Extended");
635 break;
636 case CFI_CMDSET_AMD_STANDARD:
637 printf ("AMD Standard");
638 break;
639 case CFI_CMDSET_AMD_EXTENDED:
640 printf ("AMD Extended");
641 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100642#ifdef CONFIG_FLASH_CFI_LEGACY
643 case CFI_CMDSET_AMD_LEGACY:
644 printf ("AMD Legacy");
645 break;
646#endif
Stefan Roese260421a2006-11-13 13:55:24 +0100647 default:
648 printf ("Unknown (%d)", info->vendor);
649 break;
650 }
651 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
652 info->manufacturer_id, info->device_id);
653 if (info->device_id == 0x7E) {
654 printf("%04X", info->device_id2);
655 }
656 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
wdenk028ab6b2004-02-23 23:54:43 +0000657 info->erase_blk_tout,
Stefan Roese260421a2006-11-13 13:55:24 +0100658 info->write_tout);
659 if (info->buffer_size > 1) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100660 printf (" Buffer write timeout: %ld ms, "
661 "buffer size: %d bytes\n",
wdenk028ab6b2004-02-23 23:54:43 +0000662 info->buffer_write_tout,
663 info->buffer_size);
Stefan Roese260421a2006-11-13 13:55:24 +0100664 }
wdenk5653fc32004-02-08 22:55:38 +0000665
Stefan Roese260421a2006-11-13 13:55:24 +0100666 puts ("\n Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +0000667 for (i = 0; i < info->sector_count; ++i) {
Stefan Roese260421a2006-11-13 13:55:24 +0100668 if ((i % 5) == 0)
669 printf ("\n");
wdenk5653fc32004-02-08 22:55:38 +0000670#ifdef CFG_FLASH_EMPTY_INFO
671 int k;
672 int size;
673 int erased;
674 volatile unsigned long *flash;
675
676 /*
677 * Check if whole sector is erased
678 */
wdenkbf9e3b32004-02-12 00:47:09 +0000679 if (i != (info->sector_count - 1))
680 size = info->start[i + 1] - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000681 else
wdenkbf9e3b32004-02-12 00:47:09 +0000682 size = info->start[0] + info->size - info->start[i];
wdenk5653fc32004-02-08 22:55:38 +0000683 erased = 1;
wdenkbf9e3b32004-02-12 00:47:09 +0000684 flash = (volatile unsigned long *) info->start[i];
685 size = size >> 2; /* divide by 4 for longword access */
686 for (k = 0; k < size; k++) {
687 if (*flash++ != 0xffffffff) {
688 erased = 0;
689 break;
690 }
691 }
wdenk5653fc32004-02-08 22:55:38 +0000692
wdenk5653fc32004-02-08 22:55:38 +0000693 /* print empty and read-only info */
Stefan Roese260421a2006-11-13 13:55:24 +0100694 printf (" %08lX %c %s ",
wdenk5653fc32004-02-08 22:55:38 +0000695 info->start[i],
Stefan Roese260421a2006-11-13 13:55:24 +0100696 erased ? 'E' : ' ',
697 info->protect[i] ? "RO" : " ");
Wolfgang Denkb63de2c2005-09-25 00:23:05 +0200698#else /* ! CFG_FLASH_EMPTY_INFO */
Stefan Roese260421a2006-11-13 13:55:24 +0100699 printf (" %08lX %s ",
700 info->start[i],
701 info->protect[i] ? "RO" : " ");
wdenk5653fc32004-02-08 22:55:38 +0000702#endif
703 }
wdenk4b9206e2004-03-23 22:14:11 +0000704 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +0000705 return;
706}
707
708/*-----------------------------------------------------------------------
709 * Copy memory to flash, returns:
710 * 0 - OK
711 * 1 - write timeout
712 * 2 - Flash not erased
713 */
wdenkbf9e3b32004-02-12 00:47:09 +0000714int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +0000715{
716 ulong wp;
717 ulong cp;
718 int aln;
719 cfiword_t cword;
720 int i, rc;
721
wdenkbf9e3b32004-02-12 00:47:09 +0000722#ifdef CFG_FLASH_USE_BUFFER_WRITE
723 int buffered_size;
724#endif
wdenkbf9e3b32004-02-12 00:47:09 +0000725 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +0000726 /* get lower aligned address */
727 wp = (addr & ~(info->portwidth - 1));
728
729 /* handle unaligned start */
wdenkbf9e3b32004-02-12 00:47:09 +0000730 if ((aln = addr - wp) != 0) {
wdenk5653fc32004-02-08 22:55:38 +0000731 cword.l = 0;
732 cp = wp;
wdenkbf9e3b32004-02-12 00:47:09 +0000733 for (i = 0; i < aln; ++i, ++cp)
734 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000735
wdenkbf9e3b32004-02-12 00:47:09 +0000736 for (; (i < info->portwidth) && (cnt > 0); i++) {
737 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000738 cnt--;
739 cp++;
740 }
wdenkbf9e3b32004-02-12 00:47:09 +0000741 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
742 flash_add_byte (info, &cword, (*(uchar *) cp));
743 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000744 return rc;
745 wp = cp;
746 }
747
wdenkbf9e3b32004-02-12 00:47:09 +0000748 /* handle the aligned part */
wdenk5653fc32004-02-08 22:55:38 +0000749#ifdef CFG_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +0000750 buffered_size = (info->portwidth / info->chipwidth);
751 buffered_size *= info->buffer_size;
752 while (cnt >= info->portwidth) {
Stefan Roese79b4cda2006-02-28 15:29:58 +0100753 /* prohibit buffer write when buffer_size is 1 */
754 if (info->buffer_size == 1) {
755 cword.l = 0;
756 for (i = 0; i < info->portwidth; i++)
757 flash_add_byte (info, &cword, *src++);
758 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
759 return rc;
760 wp += info->portwidth;
761 cnt -= info->portwidth;
762 continue;
763 }
764
765 /* write buffer until next buffered_size aligned boundary */
766 i = buffered_size - (wp % buffered_size);
767 if (i > cnt)
768 i = cnt;
wdenkbf9e3b32004-02-12 00:47:09 +0000769 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +0000770 return rc;
Wolfgang Denk8d4ba3d2005-08-12 22:35:59 +0200771 i -= i & (info->portwidth - 1);
wdenk5653fc32004-02-08 22:55:38 +0000772 wp += i;
773 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +0000774 cnt -= i;
wdenk5653fc32004-02-08 22:55:38 +0000775 }
776#else
wdenkbf9e3b32004-02-12 00:47:09 +0000777 while (cnt >= info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000778 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000779 for (i = 0; i < info->portwidth; i++) {
780 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000781 }
wdenkbf9e3b32004-02-12 00:47:09 +0000782 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +0000783 return rc;
784 wp += info->portwidth;
785 cnt -= info->portwidth;
786 }
787#endif /* CFG_FLASH_USE_BUFFER_WRITE */
788 if (cnt == 0) {
789 return (0);
790 }
791
792 /*
793 * handle unaligned tail bytes
794 */
795 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000796 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
797 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +0000798 --cnt;
799 }
wdenkbf9e3b32004-02-12 00:47:09 +0000800 for (; i < info->portwidth; ++i, ++cp) {
801 flash_add_byte (info, &cword, (*(uchar *) cp));
wdenk5653fc32004-02-08 22:55:38 +0000802 }
803
wdenkbf9e3b32004-02-12 00:47:09 +0000804 return flash_write_cfiword (info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +0000805}
806
807/*-----------------------------------------------------------------------
808 */
809#ifdef CFG_FLASH_PROTECTION
810
wdenkbf9e3b32004-02-12 00:47:09 +0000811int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +0000812{
813 int retcode = 0;
814
wdenkbf9e3b32004-02-12 00:47:09 +0000815 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
816 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
817 if (prot)
818 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
wdenk5653fc32004-02-08 22:55:38 +0000819 else
wdenkbf9e3b32004-02-12 00:47:09 +0000820 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
wdenk5653fc32004-02-08 22:55:38 +0000821
wdenkbf9e3b32004-02-12 00:47:09 +0000822 if ((retcode =
823 flash_full_status_check (info, sector, info->erase_blk_tout,
824 prot ? "protect" : "unprotect")) == 0) {
wdenk5653fc32004-02-08 22:55:38 +0000825
826 info->protect[sector] = prot;
Stefan Roese2662b402006-04-01 13:41:03 +0200827
828 /*
829 * On some of Intel's flash chips (marked via legacy_unlock)
830 * unprotect unprotects all locking.
831 */
832 if ((prot == 0) && (info->legacy_unlock)) {
wdenk5653fc32004-02-08 22:55:38 +0000833 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +0000834
835 for (i = 0; i < info->sector_count; i++) {
836 if (info->protect[i])
837 flash_real_protect (info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +0000838 }
839 }
840 }
wdenk5653fc32004-02-08 22:55:38 +0000841 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000842}
843
wdenk5653fc32004-02-08 22:55:38 +0000844/*-----------------------------------------------------------------------
845 * flash_read_user_serial - read the OneTimeProgramming cells
846 */
wdenkbf9e3b32004-02-12 00:47:09 +0000847void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
848 int len)
wdenk5653fc32004-02-08 22:55:38 +0000849{
wdenkbf9e3b32004-02-12 00:47:09 +0000850 uchar *src;
851 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +0000852
853 dst = buffer;
wdenkbf9e3b32004-02-12 00:47:09 +0000854 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
855 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
856 memcpy (dst, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200857 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000858}
wdenkbf9e3b32004-02-12 00:47:09 +0000859
wdenk5653fc32004-02-08 22:55:38 +0000860/*
861 * flash_read_factory_serial - read the device Id from the protection area
862 */
wdenkbf9e3b32004-02-12 00:47:09 +0000863void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
864 int len)
wdenk5653fc32004-02-08 22:55:38 +0000865{
wdenkbf9e3b32004-02-12 00:47:09 +0000866 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +0000867
wdenkbf9e3b32004-02-12 00:47:09 +0000868 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
869 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
870 memcpy (buffer, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200871 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000872}
873
874#endif /* CFG_FLASH_PROTECTION */
875
wdenkbf9e3b32004-02-12 00:47:09 +0000876/*
877 * flash_is_busy - check to see if the flash is busy
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100878 *
879 * This routine checks the status of the chip and returns true if the
880 * chip is busy.
wdenkbf9e3b32004-02-12 00:47:09 +0000881 */
882static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +0000883{
884 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000885
886 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000887 case CFI_CMDSET_INTEL_STANDARD:
888 case CFI_CMDSET_INTEL_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +0000889 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
wdenk5653fc32004-02-08 22:55:38 +0000890 break;
891 case CFI_CMDSET_AMD_STANDARD:
892 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100893#ifdef CONFIG_FLASH_CFI_LEGACY
894 case CFI_CMDSET_AMD_LEGACY:
895#endif
wdenkbf9e3b32004-02-12 00:47:09 +0000896 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
wdenk5653fc32004-02-08 22:55:38 +0000897 break;
898 default:
899 retval = 0;
900 }
wdenkbf9e3b32004-02-12 00:47:09 +0000901 debug ("flash_is_busy: %d\n", retval);
wdenk5653fc32004-02-08 22:55:38 +0000902 return retval;
903}
wdenkbf9e3b32004-02-12 00:47:09 +0000904
wdenk5653fc32004-02-08 22:55:38 +0000905/*-----------------------------------------------------------------------
906 * wait for XSR.7 to be set. Time out with an error if it does not.
907 * This routine does not set the flash to read-array mode.
908 */
wdenkbf9e3b32004-02-12 00:47:09 +0000909static int flash_status_check (flash_info_t * info, flash_sect_t sector,
910 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000911{
912 ulong start;
913
Stefan Roese2662b402006-04-01 13:41:03 +0200914#if CFG_HZ != 1000
915 tout *= CFG_HZ/1000;
916#endif
917
wdenk5653fc32004-02-08 22:55:38 +0000918 /* Wait for command completion */
919 start = get_timer (0);
wdenkbf9e3b32004-02-12 00:47:09 +0000920 while (flash_is_busy (info, sector)) {
Stefan Roese79b4cda2006-02-28 15:29:58 +0100921 if (get_timer (start) > tout) {
wdenkbf9e3b32004-02-12 00:47:09 +0000922 printf ("Flash %s timeout at address %lx data %lx\n",
923 prompt, info->start[sector],
924 flash_read_long (info, sector, 0));
925 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000926 return ERR_TIMOUT;
927 }
Wolfgang Denk62b8f542006-06-02 11:46:20 +0200928 udelay (1); /* also triggers watchdog */
wdenk5653fc32004-02-08 22:55:38 +0000929 }
930 return ERR_OK;
931}
wdenkbf9e3b32004-02-12 00:47:09 +0000932
wdenk5653fc32004-02-08 22:55:38 +0000933/*-----------------------------------------------------------------------
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100934 * Wait for XSR.7 to be set, if it times out print an error, otherwise
935 * do a full status check.
936 *
wdenk5653fc32004-02-08 22:55:38 +0000937 * This routine sets the flash to read-array mode.
938 */
wdenkbf9e3b32004-02-12 00:47:09 +0000939static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
940 ulong tout, char *prompt)
wdenk5653fc32004-02-08 22:55:38 +0000941{
942 int retcode;
wdenkbf9e3b32004-02-12 00:47:09 +0000943
944 retcode = flash_status_check (info, sector, tout, prompt);
945 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +0000946 case CFI_CMDSET_INTEL_EXTENDED:
947 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roese79b4cda2006-02-28 15:29:58 +0100948 if ((retcode == ERR_OK)
wdenkbf9e3b32004-02-12 00:47:09 +0000949 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
wdenk5653fc32004-02-08 22:55:38 +0000950 retcode = ERR_INVAL;
wdenkbf9e3b32004-02-12 00:47:09 +0000951 printf ("Flash %s error at address %lx\n", prompt,
952 info->start[sector]);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100953 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
954 FLASH_STATUS_PSLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000955 puts ("Command Sequence Error.\n");
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100956 } else if (flash_isset (info, sector, 0,
957 FLASH_STATUS_ECLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000958 puts ("Block Erase Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000959 retcode = ERR_NOT_ERASED;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +0100960 } else if (flash_isset (info, sector, 0,
961 FLASH_STATUS_PSLBS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000962 puts ("Locking Error\n");
wdenk5653fc32004-02-08 22:55:38 +0000963 }
wdenkbf9e3b32004-02-12 00:47:09 +0000964 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
wdenk4b9206e2004-03-23 22:14:11 +0000965 puts ("Block locked.\n");
wdenkbf9e3b32004-02-12 00:47:09 +0000966 retcode = ERR_PROTECTED;
967 }
968 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
wdenk4b9206e2004-03-23 22:14:11 +0000969 puts ("Vpp Low Error.\n");
wdenk5653fc32004-02-08 22:55:38 +0000970 }
Wolfgang Denkdb421e62005-09-25 16:41:22 +0200971 flash_write_cmd (info, sector, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +0000972 break;
973 default:
974 break;
975 }
976 return retcode;
977}
wdenkbf9e3b32004-02-12 00:47:09 +0000978
wdenk5653fc32004-02-08 22:55:38 +0000979/*-----------------------------------------------------------------------
980 */
wdenkbf9e3b32004-02-12 00:47:09 +0000981static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk5653fc32004-02-08 22:55:38 +0000982{
Heiko Schocherd0b6e142007-01-19 18:05:26 +0100983#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk4d13cba2004-03-14 14:09:05 +0000984 unsigned short w;
985 unsigned int l;
986 unsigned long long ll;
987#endif
988
wdenkbf9e3b32004-02-12 00:47:09 +0000989 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +0000990 case FLASH_CFI_8BIT:
991 cword->c = c;
992 break;
993 case FLASH_CFI_16BIT:
Heiko Schocherd0b6e142007-01-19 18:05:26 +0100994#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk4d13cba2004-03-14 14:09:05 +0000995 w = c;
996 w <<= 8;
997 cword->w = (cword->w >> 8) | w;
998#else
wdenk5653fc32004-02-08 22:55:38 +0000999 cword->w = (cword->w << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +00001000#endif
wdenk5653fc32004-02-08 22:55:38 +00001001 break;
1002 case FLASH_CFI_32BIT:
Heiko Schocherd0b6e142007-01-19 18:05:26 +01001003#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk4d13cba2004-03-14 14:09:05 +00001004 l = c;
1005 l <<= 24;
1006 cword->l = (cword->l >> 8) | l;
1007#else
wdenk5653fc32004-02-08 22:55:38 +00001008 cword->l = (cword->l << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +00001009#endif
wdenk5653fc32004-02-08 22:55:38 +00001010 break;
1011 case FLASH_CFI_64BIT:
Heiko Schocherd0b6e142007-01-19 18:05:26 +01001012#if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
wdenk4d13cba2004-03-14 14:09:05 +00001013 ll = c;
1014 ll <<= 56;
1015 cword->ll = (cword->ll >> 8) | ll;
1016#else
wdenk5653fc32004-02-08 22:55:38 +00001017 cword->ll = (cword->ll << 8) | c;
wdenk4d13cba2004-03-14 14:09:05 +00001018#endif
wdenk5653fc32004-02-08 22:55:38 +00001019 break;
1020 }
1021}
1022
1023
1024/*-----------------------------------------------------------------------
1025 * make a proper sized command based on the port and chip widths
1026 */
wdenkbf9e3b32004-02-12 00:47:09 +00001027static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
wdenk5653fc32004-02-08 22:55:38 +00001028{
1029 int i;
wdenkbf9e3b32004-02-12 00:47:09 +00001030 uchar *cp = (uchar *) cmdbuf;
1031
Heiko Schocherd0b6e142007-01-19 18:05:26 +01001032#if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
Wolfgang Denkdafbe372005-09-24 23:32:48 +02001033 for (i = info->portwidth; i > 0; i--)
1034#else
1035 for (i = 1; i <= info->portwidth; i++)
wdenkbf9e3b32004-02-12 00:47:09 +00001036#endif
Wolfgang Denk47340a42005-10-09 00:25:58 +02001037 *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
wdenk5653fc32004-02-08 22:55:38 +00001038}
1039
1040/*
1041 * Write a proper sized command to the correct address
1042 */
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001043static void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
1044 uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +00001045{
1046
1047 volatile cfiptr_t addr;
1048 cfiword_t cword;
wdenkbf9e3b32004-02-12 00:47:09 +00001049
1050 addr.cp = flash_make_addr (info, sect, offset);
1051 flash_make_cmd (info, cmd, &cword);
1052 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001053 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001054 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
1055 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
wdenk5653fc32004-02-08 22:55:38 +00001056 *addr.cp = cword.c;
1057 break;
1058 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001059 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
1060 cmd, cword.w,
wdenk5653fc32004-02-08 22:55:38 +00001061 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1062 *addr.wp = cword.w;
1063 break;
1064 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001065 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
1066 cmd, cword.l,
wdenk5653fc32004-02-08 22:55:38 +00001067 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1068 *addr.lp = cword.l;
1069 break;
1070 case FLASH_CFI_64BIT:
1071#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +00001072 {
wdenk5653fc32004-02-08 22:55:38 +00001073 char str[20];
wdenkcd37d9e2004-02-10 00:03:41 +00001074
wdenkbf9e3b32004-02-12 00:47:09 +00001075 print_longlong (str, cword.ll);
1076
1077 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
1078 addr.llp, cmd, str,
wdenk5653fc32004-02-08 22:55:38 +00001079 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1080 }
1081#endif
1082 *addr.llp = cword.ll;
1083 break;
1084 }
Haiying Wang3a197b22007-02-21 16:52:31 +01001085
1086 /* Ensure all the instructions are fully finished */
1087 sync();
wdenk5653fc32004-02-08 22:55:38 +00001088}
1089
wdenkbf9e3b32004-02-12 00:47:09 +00001090static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
wdenk5653fc32004-02-08 22:55:38 +00001091{
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001092 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
1093 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
wdenk5653fc32004-02-08 22:55:38 +00001094}
wdenkbf9e3b32004-02-12 00:47:09 +00001095
wdenk5653fc32004-02-08 22:55:38 +00001096/*-----------------------------------------------------------------------
1097 */
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001098static int flash_isequal (flash_info_t * info, flash_sect_t sect,
1099 uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +00001100{
1101 cfiptr_t cptr;
1102 cfiword_t cword;
1103 int retval;
wdenk5653fc32004-02-08 22:55:38 +00001104
wdenkbf9e3b32004-02-12 00:47:09 +00001105 cptr.cp = flash_make_addr (info, sect, offset);
1106 flash_make_cmd (info, cmd, &cword);
1107
1108 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
1109 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001110 case FLASH_CFI_8BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001111 debug ("is= %x %x\n", cptr.cp[0], cword.c);
wdenk5653fc32004-02-08 22:55:38 +00001112 retval = (cptr.cp[0] == cword.c);
1113 break;
1114 case FLASH_CFI_16BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001115 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
wdenk5653fc32004-02-08 22:55:38 +00001116 retval = (cptr.wp[0] == cword.w);
1117 break;
1118 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001119 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
wdenk5653fc32004-02-08 22:55:38 +00001120 retval = (cptr.lp[0] == cword.l);
1121 break;
1122 case FLASH_CFI_64BIT:
wdenkcd37d9e2004-02-10 00:03:41 +00001123#ifdef DEBUG
wdenkbf9e3b32004-02-12 00:47:09 +00001124 {
wdenk5653fc32004-02-08 22:55:38 +00001125 char str1[20];
1126 char str2[20];
wdenkbf9e3b32004-02-12 00:47:09 +00001127
1128 print_longlong (str1, cptr.llp[0]);
1129 print_longlong (str2, cword.ll);
1130 debug ("is= %s %s\n", str1, str2);
wdenk5653fc32004-02-08 22:55:38 +00001131 }
1132#endif
1133 retval = (cptr.llp[0] == cword.ll);
1134 break;
1135 default:
1136 retval = 0;
1137 break;
1138 }
1139 return retval;
1140}
wdenkbf9e3b32004-02-12 00:47:09 +00001141
wdenk5653fc32004-02-08 22:55:38 +00001142/*-----------------------------------------------------------------------
1143 */
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001144static int flash_isset (flash_info_t * info, flash_sect_t sect,
1145 uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +00001146{
1147 cfiptr_t cptr;
1148 cfiword_t cword;
1149 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +00001150
1151 cptr.cp = flash_make_addr (info, sect, offset);
1152 flash_make_cmd (info, cmd, &cword);
1153 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001154 case FLASH_CFI_8BIT:
1155 retval = ((cptr.cp[0] & cword.c) == cword.c);
1156 break;
1157 case FLASH_CFI_16BIT:
1158 retval = ((cptr.wp[0] & cword.w) == cword.w);
1159 break;
1160 case FLASH_CFI_32BIT:
1161 retval = ((cptr.lp[0] & cword.l) == cword.l);
1162 break;
1163 case FLASH_CFI_64BIT:
1164 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenkbf9e3b32004-02-12 00:47:09 +00001165 break;
wdenk5653fc32004-02-08 22:55:38 +00001166 default:
1167 retval = 0;
1168 break;
1169 }
1170 return retval;
1171}
1172
1173/*-----------------------------------------------------------------------
1174 */
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001175static int flash_toggle (flash_info_t * info, flash_sect_t sect,
1176 uint offset, uchar cmd)
wdenk5653fc32004-02-08 22:55:38 +00001177{
1178 cfiptr_t cptr;
1179 cfiword_t cword;
1180 int retval;
wdenkbf9e3b32004-02-12 00:47:09 +00001181
1182 cptr.cp = flash_make_addr (info, sect, offset);
1183 flash_make_cmd (info, cmd, &cword);
1184 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001185 case FLASH_CFI_8BIT:
1186 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
1187 break;
1188 case FLASH_CFI_16BIT:
1189 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
1190 break;
1191 case FLASH_CFI_32BIT:
1192 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
1193 break;
1194 case FLASH_CFI_64BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001195 retval = ((cptr.llp[0] & cword.ll) !=
1196 (cptr.llp[0] & cword.ll));
wdenk5653fc32004-02-08 22:55:38 +00001197 break;
1198 default:
1199 retval = 0;
1200 break;
1201 }
1202 return retval;
1203}
1204
1205/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +01001206 * read jedec ids from device and set corresponding fields in info struct
1207 *
1208 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1209 *
1210*/
1211static void flash_read_jedec_ids (flash_info_t * info)
1212{
1213 info->manufacturer_id = 0;
1214 info->device_id = 0;
1215 info->device_id2 = 0;
1216
1217 switch (info->vendor) {
1218 case CFI_CMDSET_INTEL_STANDARD:
1219 case CFI_CMDSET_INTEL_EXTENDED:
1220 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1221 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1222 udelay(1000); /* some flash are slow to respond */
1223 info->manufacturer_id = flash_read_uchar (info,
1224 FLASH_OFFSET_MANUFACTURER_ID);
1225 info->device_id = flash_read_uchar (info,
1226 FLASH_OFFSET_DEVICE_ID);
1227 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1228 break;
1229 case CFI_CMDSET_AMD_STANDARD:
1230 case CFI_CMDSET_AMD_EXTENDED:
1231 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1232 flash_unlock_seq(info, 0);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001233 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
Stefan Roese260421a2006-11-13 13:55:24 +01001234 udelay(1000); /* some flash are slow to respond */
1235 info->manufacturer_id = flash_read_uchar (info,
1236 FLASH_OFFSET_MANUFACTURER_ID);
1237 info->device_id = flash_read_uchar (info,
1238 FLASH_OFFSET_DEVICE_ID);
1239 if (info->device_id == 0x7E) {
1240 /* AMD 3-byte (expanded) device ids */
1241 info->device_id2 = flash_read_uchar (info,
1242 FLASH_OFFSET_DEVICE_ID2);
1243 info->device_id2 <<= 8;
1244 info->device_id2 |= flash_read_uchar (info,
1245 FLASH_OFFSET_DEVICE_ID3);
1246 }
1247 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1248 break;
1249 default:
1250 break;
1251 }
1252}
1253
1254/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001255 * detect if flash is compatible with the Common Flash Interface (CFI)
1256 * http://www.jedec.org/download/search/jesd68.pdf
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001257 */
1258static int __flash_detect_cfi (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +00001259{
Wolfgang Denk92eb7292006-12-27 01:26:13 +01001260 int cfi_offset;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001261
1262 flash_write_cmd (info, 0, 0, info->cmd_reset);
1263 for (cfi_offset=0;
1264 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1265 cfi_offset++) {
1266 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1267 FLASH_CMD_CFI);
1268 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1269 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1270 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1271 info->interface = flash_read_ushort (info, 0,
1272 FLASH_OFFSET_INTERFACE);
1273 info->cfi_offset = flash_offset_cfi[cfi_offset];
1274 debug ("device interface is %d\n",
1275 info->interface);
1276 debug ("found port %d chip %d ",
1277 info->portwidth, info->chipwidth);
1278 debug ("port %d bits chip %d bits\n",
1279 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1280 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1281
1282 /* calculate command offsets as in the Linux driver */
1283 info->addr_unlock1 = 0x555;
1284 info->addr_unlock2 = 0x2aa;
1285
1286 /*
1287 * modify the unlock address if we are
1288 * in compatibility mode
1289 */
1290 if ( /* x8/x16 in x8 mode */
1291 ((info->chipwidth == FLASH_CFI_BY8) &&
1292 (info->interface == FLASH_CFI_X8X16)) ||
1293 /* x16/x32 in x16 mode */
1294 ((info->chipwidth == FLASH_CFI_BY16) &&
1295 (info->interface == FLASH_CFI_X16X32)))
1296 {
1297 info->addr_unlock1 = 0xaaa;
1298 info->addr_unlock2 = 0x555;
1299 }
1300
1301 info->name = "CFI conformant";
1302 return 1;
1303 }
1304 }
1305
1306 return 0;
1307}
1308
1309static int flash_detect_cfi (flash_info_t * info)
1310{
wdenkbf9e3b32004-02-12 00:47:09 +00001311 debug ("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +00001312
Stefan Roese79b4cda2006-02-28 15:29:58 +01001313 for (info->portwidth = CFG_FLASH_CFI_WIDTH;
wdenkbf9e3b32004-02-12 00:47:09 +00001314 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1315 for (info->chipwidth = FLASH_CFI_BY8;
1316 info->chipwidth <= info->portwidth;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001317 info->chipwidth <<= 1)
1318 if (__flash_detect_cfi(info))
1319 return 1;
wdenk5653fc32004-02-08 22:55:38 +00001320 }
wdenkbf9e3b32004-02-12 00:47:09 +00001321 debug ("not found\n");
wdenk5653fc32004-02-08 22:55:38 +00001322 return 0;
1323}
wdenkbf9e3b32004-02-12 00:47:09 +00001324
wdenk5653fc32004-02-08 22:55:38 +00001325/*
1326 * The following code cannot be run from FLASH!
1327 *
1328 */
Marian Balakowicze6f2e902005-10-11 19:09:42 +02001329ulong flash_get_size (ulong base, int banknum)
wdenk5653fc32004-02-08 22:55:38 +00001330{
wdenkbf9e3b32004-02-12 00:47:09 +00001331 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +00001332 int i, j;
1333 flash_sect_t sect_cnt;
1334 unsigned long sector;
1335 unsigned long tmp;
1336 int size_ratio;
1337 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +00001338 int erase_region_size;
1339 int erase_region_count;
Stefan Roese260421a2006-11-13 13:55:24 +01001340 int geometry_reversed = 0;
1341
1342 info->ext_addr = 0;
1343 info->cfi_version = 0;
Stefan Roese2662b402006-04-01 13:41:03 +02001344#ifdef CFG_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +02001345 info->legacy_unlock = 0;
1346#endif
wdenk5653fc32004-02-08 22:55:38 +00001347
1348 info->start[0] = base;
1349
wdenkbf9e3b32004-02-12 00:47:09 +00001350 if (flash_detect_cfi (info)) {
Stefan Roese260421a2006-11-13 13:55:24 +01001351 info->vendor = flash_read_ushort (info, 0,
1352 FLASH_OFFSET_PRIMARY_VENDOR);
1353 flash_read_jedec_ids (info);
Stefano Babicd784fdb2006-12-12 00:22:42 +01001354 flash_write_cmd (info, 0, info->cfi_offset, FLASH_CMD_CFI);
Stefan Roese260421a2006-11-13 13:55:24 +01001355 num_erase_regions = flash_read_uchar (info,
1356 FLASH_OFFSET_NUM_ERASE_REGIONS);
1357 info->ext_addr = flash_read_ushort (info, 0,
1358 FLASH_OFFSET_EXT_QUERY_T_P_ADDR);
1359 if (info->ext_addr) {
1360 info->cfi_version = (ushort) flash_read_uchar (info,
1361 info->ext_addr + 3) << 8;
1362 info->cfi_version |= (ushort) flash_read_uchar (info,
1363 info->ext_addr + 4);
1364 }
wdenkbf9e3b32004-02-12 00:47:09 +00001365#ifdef DEBUG
1366 flash_printqry (info, 0);
1367#endif
1368 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001369 case CFI_CMDSET_INTEL_STANDARD:
1370 case CFI_CMDSET_INTEL_EXTENDED:
1371 default:
1372 info->cmd_reset = FLASH_CMD_RESET;
Stefan Roese2662b402006-04-01 13:41:03 +02001373#ifdef CFG_FLASH_PROTECTION
1374 /* read legacy lock/unlock bit from intel flash */
Stefan Roese260421a2006-11-13 13:55:24 +01001375 if (info->ext_addr) {
1376 info->legacy_unlock = flash_read_uchar (info,
1377 info->ext_addr + 5) & 0x08;
1378 }
Stefan Roese2662b402006-04-01 13:41:03 +02001379#endif
wdenk5653fc32004-02-08 22:55:38 +00001380 break;
1381 case CFI_CMDSET_AMD_STANDARD:
1382 case CFI_CMDSET_AMD_EXTENDED:
1383 info->cmd_reset = AMD_CMD_RESET;
Stefan Roese260421a2006-11-13 13:55:24 +01001384 /* check if flash geometry needs reversal */
1385 if (num_erase_regions <= 1)
1386 break;
1387 /* reverse geometry if top boot part */
1388 if (info->cfi_version < 0x3131) {
1389 /* CFI < 1.1, try to guess from device id */
1390 if ((info->device_id & 0x80) != 0) {
1391 geometry_reversed = 1;
1392 }
1393 break;
1394 }
1395 /* CFI >= 1.1, deduct from top/bottom flag */
1396 /* note: ext_addr is valid since cfi_version > 0 */
1397 if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1398 geometry_reversed = 1;
1399 }
wdenk5653fc32004-02-08 22:55:38 +00001400 break;
1401 }
wdenkcd37d9e2004-02-10 00:03:41 +00001402
wdenkbf9e3b32004-02-12 00:47:09 +00001403 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese260421a2006-11-13 13:55:24 +01001404 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1405 debug ("device id is 0x%x\n", info->device_id);
1406 debug ("device id2 is 0x%x\n", info->device_id2);
1407 debug ("cfi version is 0x%04x\n", info->cfi_version);
1408
wdenk5653fc32004-02-08 22:55:38 +00001409 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00001410 /* if the chip is x8/x16 reduce the ratio by half */
1411 if ((info->interface == FLASH_CFI_X8X16)
1412 && (info->chipwidth == FLASH_CFI_BY8)) {
1413 size_ratio >>= 1;
1414 }
wdenkbf9e3b32004-02-12 00:47:09 +00001415 debug ("size_ratio %d port %d bits chip %d bits\n",
1416 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1417 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1418 debug ("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00001419 sect_cnt = 0;
1420 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00001421 for (i = 0; i < num_erase_regions; i++) {
1422 if (i > NUM_ERASE_REGIONS) {
wdenk028ab6b2004-02-23 23:54:43 +00001423 printf ("%d erase regions found, only %d used\n",
1424 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00001425 break;
1426 }
Stefan Roese260421a2006-11-13 13:55:24 +01001427 if (geometry_reversed)
1428 tmp = flash_read_long (info, 0,
1429 FLASH_OFFSET_ERASE_REGIONS +
1430 (num_erase_regions - 1 - i) * 4);
1431 else
1432 tmp = flash_read_long (info, 0,
wdenkbf9e3b32004-02-12 00:47:09 +00001433 FLASH_OFFSET_ERASE_REGIONS +
1434 i * 4);
1435 erase_region_size =
1436 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk5653fc32004-02-08 22:55:38 +00001437 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00001438 erase_region_count = (tmp & 0xffff) + 1;
wdenk4c0d4c32004-06-09 17:34:58 +00001439 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenk028ab6b2004-02-23 23:54:43 +00001440 erase_region_count, erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00001441 for (j = 0; j < erase_region_count; j++) {
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001442 if (sect_cnt >= CFG_MAX_FLASH_SECT) {
1443 printf("ERROR: too many flash sectors\n");
1444 break;
1445 }
wdenk5653fc32004-02-08 22:55:38 +00001446 info->start[sect_cnt] = sector;
1447 sector += (erase_region_size * size_ratio);
wdenka1191902005-01-09 17:12:27 +00001448
1449 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001450 * Only read protection status from
1451 * supported devices (intel...)
wdenka1191902005-01-09 17:12:27 +00001452 */
1453 switch (info->vendor) {
1454 case CFI_CMDSET_INTEL_EXTENDED:
1455 case CFI_CMDSET_INTEL_STANDARD:
1456 info->protect[sect_cnt] =
1457 flash_isset (info, sect_cnt,
1458 FLASH_OFFSET_PROTECT,
1459 FLASH_STATUS_PROTECT);
1460 break;
1461 default:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001462 /* default: not protected */
1463 info->protect[sect_cnt] = 0;
wdenka1191902005-01-09 17:12:27 +00001464 }
1465
wdenk5653fc32004-02-08 22:55:38 +00001466 sect_cnt++;
1467 }
1468 }
1469
1470 info->sector_count = sect_cnt;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001471 info->size = 1 << flash_read_uchar (info, FLASH_OFFSET_SIZE);
wdenk5653fc32004-02-08 22:55:38 +00001472 /* multiply the size by the number of chips */
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001473 info->size *= size_ratio;
1474 info->buffer_size = 1 << flash_read_ushort (info, 0,
1475 FLASH_OFFSET_BUFFER_SIZE);
wdenkbf9e3b32004-02-12 00:47:09 +00001476 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001477 info->erase_blk_tout = tmp *
1478 (1 << flash_read_uchar (
1479 info, FLASH_OFFSET_EMAX_TOUT));
Stefan Roese2662b402006-04-01 13:41:03 +02001480 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT)) *
1481 (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT));
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001482 /* round up when converting to ms */
1483 info->buffer_write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0);
Stefan Roese79b4cda2006-02-28 15:29:58 +01001484 tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
1485 (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001486 /* round up when converting to ms */
1487 info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0);
wdenk5653fc32004-02-08 22:55:38 +00001488 info->flash_id = FLASH_MAN_CFI;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001489 if ((info->interface == FLASH_CFI_X8X16) &&
1490 (info->chipwidth == FLASH_CFI_BY8)) {
1491 /* XXX - Need to test on x8/x16 in parallel. */
1492 info->portwidth >>= 1;
wdenk855a4962004-03-14 18:23:55 +00001493 }
wdenk5653fc32004-02-08 22:55:38 +00001494 }
1495
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001496 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenkbf9e3b32004-02-12 00:47:09 +00001497 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00001498}
1499
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001500/* loop through the sectors from the highest address when the passed
1501 * address is greater or equal to the sector address we have a match
Stefan Roese79b4cda2006-02-28 15:29:58 +01001502 */
1503static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1504{
1505 flash_sect_t sector;
1506
1507 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1508 if (addr >= info->start[sector])
1509 break;
1510 }
1511 return sector;
1512}
wdenk5653fc32004-02-08 22:55:38 +00001513
1514/*-----------------------------------------------------------------------
1515 */
wdenkbf9e3b32004-02-12 00:47:09 +00001516static int flash_write_cfiword (flash_info_t * info, ulong dest,
1517 cfiword_t cword)
wdenk5653fc32004-02-08 22:55:38 +00001518{
wdenk5653fc32004-02-08 22:55:38 +00001519 cfiptr_t ctladdr;
1520 cfiptr_t cptr;
1521 int flag;
1522
wdenkbf9e3b32004-02-12 00:47:09 +00001523 ctladdr.cp = flash_make_addr (info, 0, 0);
1524 cptr.cp = (uchar *) dest;
wdenk5653fc32004-02-08 22:55:38 +00001525
wdenk5653fc32004-02-08 22:55:38 +00001526 /* Check if Flash is (sufficiently) erased */
wdenkbf9e3b32004-02-12 00:47:09 +00001527 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001528 case FLASH_CFI_8BIT:
1529 flag = ((cptr.cp[0] & cword.c) == cword.c);
1530 break;
1531 case FLASH_CFI_16BIT:
1532 flag = ((cptr.wp[0] & cword.w) == cword.w);
1533 break;
1534 case FLASH_CFI_32BIT:
wdenkbf9e3b32004-02-12 00:47:09 +00001535 flag = ((cptr.lp[0] & cword.l) == cword.l);
wdenk5653fc32004-02-08 22:55:38 +00001536 break;
1537 case FLASH_CFI_64BIT:
wdenke1599e82004-10-10 23:27:33 +00001538 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
wdenk5653fc32004-02-08 22:55:38 +00001539 break;
1540 default:
1541 return 2;
1542 }
wdenkbf9e3b32004-02-12 00:47:09 +00001543 if (!flag)
wdenk5653fc32004-02-08 22:55:38 +00001544 return 2;
1545
1546 /* Disable interrupts which might cause a timeout here */
wdenkbf9e3b32004-02-12 00:47:09 +00001547 flag = disable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001548
wdenkbf9e3b32004-02-12 00:47:09 +00001549 switch (info->vendor) {
wdenk5653fc32004-02-08 22:55:38 +00001550 case CFI_CMDSET_INTEL_EXTENDED:
1551 case CFI_CMDSET_INTEL_STANDARD:
wdenkbf9e3b32004-02-12 00:47:09 +00001552 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1553 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001554 break;
1555 case CFI_CMDSET_AMD_EXTENDED:
1556 case CFI_CMDSET_AMD_STANDARD:
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001557#ifdef CONFIG_FLASH_CFI_LEGACY
1558 case CFI_CMDSET_AMD_LEGACY:
1559#endif
wdenkbf9e3b32004-02-12 00:47:09 +00001560 flash_unlock_seq (info, 0);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001561 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
wdenk5653fc32004-02-08 22:55:38 +00001562 break;
1563 }
1564
wdenkbf9e3b32004-02-12 00:47:09 +00001565 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001566 case FLASH_CFI_8BIT:
1567 cptr.cp[0] = cword.c;
1568 break;
1569 case FLASH_CFI_16BIT:
1570 cptr.wp[0] = cword.w;
1571 break;
1572 case FLASH_CFI_32BIT:
1573 cptr.lp[0] = cword.l;
1574 break;
1575 case FLASH_CFI_64BIT:
1576 cptr.llp[0] = cword.ll;
1577 break;
1578 }
1579
1580 /* re-enable interrupts if necessary */
wdenkbf9e3b32004-02-12 00:47:09 +00001581 if (flag)
1582 enable_interrupts ();
wdenk5653fc32004-02-08 22:55:38 +00001583
Stefan Roese79b4cda2006-02-28 15:29:58 +01001584 return flash_full_status_check (info, find_sector (info, dest),
1585 info->write_tout, "write");
wdenk5653fc32004-02-08 22:55:38 +00001586}
1587
1588#ifdef CFG_FLASH_USE_BUFFER_WRITE
1589
wdenkbf9e3b32004-02-12 00:47:09 +00001590static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1591 int len)
wdenk5653fc32004-02-08 22:55:38 +00001592{
1593 flash_sect_t sector;
1594 int cnt;
1595 int retcode;
1596 volatile cfiptr_t src;
1597 volatile cfiptr_t dst;
1598
Stefan Roese79b4cda2006-02-28 15:29:58 +01001599 switch (info->vendor) {
1600 case CFI_CMDSET_INTEL_STANDARD:
1601 case CFI_CMDSET_INTEL_EXTENDED:
1602 src.cp = cp;
1603 dst.cp = (uchar *) dest;
1604 sector = find_sector (info, dest);
1605 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1606 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001607 retcode = flash_status_check (info, sector,
1608 info->buffer_write_tout,
1609 "write to buffer");
1610 if (retcode == ERR_OK) {
1611 /* reduce the number of loops by the width of
1612 * the port */
wdenkbf9e3b32004-02-12 00:47:09 +00001613 switch (info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001614 case FLASH_CFI_8BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001615 cnt = len;
wdenk5653fc32004-02-08 22:55:38 +00001616 break;
1617 case FLASH_CFI_16BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001618 cnt = len >> 1;
wdenk5653fc32004-02-08 22:55:38 +00001619 break;
1620 case FLASH_CFI_32BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001621 cnt = len >> 2;
wdenk5653fc32004-02-08 22:55:38 +00001622 break;
1623 case FLASH_CFI_64BIT:
Stefan Roese79b4cda2006-02-28 15:29:58 +01001624 cnt = len >> 3;
wdenk5653fc32004-02-08 22:55:38 +00001625 break;
1626 default:
1627 return ERR_INVAL;
1628 break;
1629 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01001630 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1631 while (cnt-- > 0) {
1632 switch (info->portwidth) {
1633 case FLASH_CFI_8BIT:
1634 *dst.cp++ = *src.cp++;
1635 break;
1636 case FLASH_CFI_16BIT:
1637 *dst.wp++ = *src.wp++;
1638 break;
1639 case FLASH_CFI_32BIT:
1640 *dst.lp++ = *src.lp++;
1641 break;
1642 case FLASH_CFI_64BIT:
1643 *dst.llp++ = *src.llp++;
1644 break;
1645 default:
1646 return ERR_INVAL;
1647 break;
1648 }
1649 }
1650 flash_write_cmd (info, sector, 0,
1651 FLASH_CMD_WRITE_BUFFER_CONFIRM);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001652 retcode = flash_full_status_check (
1653 info, sector, info->buffer_write_tout,
1654 "buffer write");
wdenk5653fc32004-02-08 22:55:38 +00001655 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01001656 return retcode;
1657
1658 case CFI_CMDSET_AMD_STANDARD:
1659 case CFI_CMDSET_AMD_EXTENDED:
1660 src.cp = cp;
1661 dst.cp = (uchar *) dest;
1662 sector = find_sector (info, dest);
1663
1664 flash_unlock_seq(info,0);
1665 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
1666
1667 switch (info->portwidth) {
1668 case FLASH_CFI_8BIT:
1669 cnt = len;
1670 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1671 while (cnt-- > 0) *dst.cp++ = *src.cp++;
1672 break;
1673 case FLASH_CFI_16BIT:
1674 cnt = len >> 1;
1675 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1676 while (cnt-- > 0) *dst.wp++ = *src.wp++;
1677 break;
1678 case FLASH_CFI_32BIT:
1679 cnt = len >> 2;
1680 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1681 while (cnt-- > 0) *dst.lp++ = *src.lp++;
1682 break;
1683 case FLASH_CFI_64BIT:
1684 cnt = len >> 3;
1685 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1686 while (cnt-- > 0) *dst.llp++ = *src.llp++;
1687 break;
1688 default:
1689 return ERR_INVAL;
1690 }
1691
1692 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001693 retcode = flash_full_status_check (info, sector,
1694 info->buffer_write_tout,
Stefan Roese79b4cda2006-02-28 15:29:58 +01001695 "buffer write");
1696 return retcode;
1697
1698 default:
1699 debug ("Unknown Command Set\n");
1700 return ERR_INVAL;
wdenk5653fc32004-02-08 22:55:38 +00001701 }
wdenk5653fc32004-02-08 22:55:38 +00001702}
wdenkcce625e2004-09-28 19:00:19 +00001703#endif /* CFG_FLASH_USE_BUFFER_WRITE */
Heiko Schocherca43ba12007-01-11 15:44:44 +01001704
wdenk5653fc32004-02-08 22:55:38 +00001705#endif /* CFG_FLASH_CFI */