blob: 43140f36479cbee9b0b894c567c4a9588fe8b36f [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>
Stefan Roesefa36ae72009-10-27 15:15:55 +010042#include <mtd/cfi_flash.h>
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +010043#include <watchdog.h>
wdenk028ab6b2004-02-23 23:54:43 +000044
wdenk5653fc32004-02-08 22:55:38 +000045/*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010046 * This file implements a Common Flash Interface (CFI) driver for
47 * U-Boot.
48 *
49 * The width of the port and the width of the chips are determined at
50 * initialization. These widths are used to calculate the address for
51 * access CFI data structures.
wdenk5653fc32004-02-08 22:55:38 +000052 *
53 * References
54 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
55 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
56 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
57 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese260421a2006-11-13 13:55:24 +010058 * AMD CFI Specification, Release 2.0 December 1, 2001
59 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
60 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk5653fc32004-02-08 22:55:38 +000061 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocherd0b6e142007-01-19 18:05:26 +010063 * reading and writing ... (yes there is such a Hardware).
wdenk5653fc32004-02-08 22:55:38 +000064 */
65
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010066static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysinger4ffeab22010-12-22 09:41:13 -050067#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +010068static uint flash_verbose = 1;
Mike Frysinger4ffeab22010-12-22 09:41:13 -050069#else
70#define flash_verbose 1
71#endif
Wolfgang Denk92eb7292006-12-27 01:26:13 +010072
Wolfgang Denk2a112b22008-08-08 16:39:54 +020073flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
74
Stefan Roese79b4cda2006-02-28 15:29:58 +010075/*
76 * Check if chip width is defined. If not, start detecting with 8bit.
77 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020078#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
79#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roese79b4cda2006-02-28 15:29:58 +010080#endif
81
Stefan Roese6f726f92010-10-25 18:31:48 +020082/*
83 * 0xffff is an undefined value for the configuration register. When
84 * this value is returned, the configuration register shall not be
85 * written at all (default mode).
86 */
87static u16 cfi_flash_config_reg(int i)
88{
89#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
90 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
91#else
92 return 0xffff;
93#endif
94}
95
Stefan Roeseca5def32010-08-31 10:00:10 +020096#if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
97int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
98#endif
99
Stefan Roeseb00e19c2010-08-30 10:11:51 +0200100static phys_addr_t __cfi_flash_bank_addr(int i)
101{
102 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
103}
104phys_addr_t cfi_flash_bank_addr(int i)
105 __attribute__((weak, alias("__cfi_flash_bank_addr")));
106
Ilya Yanokec50a8e2010-10-21 17:20:12 +0200107static unsigned long __cfi_flash_bank_size(int i)
108{
109#ifdef CONFIG_SYS_FLASH_BANKS_SIZES
110 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
111#else
112 return 0;
113#endif
114}
115unsigned long cfi_flash_bank_size(int i)
116 __attribute__((weak, alias("__cfi_flash_bank_size")));
117
Stefan Roese45aa5a72008-11-17 14:45:22 +0100118static void __flash_write8(u8 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100119{
120 __raw_writeb(value, addr);
121}
122
Stefan Roese45aa5a72008-11-17 14:45:22 +0100123static void __flash_write16(u16 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100124{
125 __raw_writew(value, addr);
126}
127
Stefan Roese45aa5a72008-11-17 14:45:22 +0100128static void __flash_write32(u32 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100129{
130 __raw_writel(value, addr);
131}
132
Stefan Roese45aa5a72008-11-17 14:45:22 +0100133static void __flash_write64(u64 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100134{
135 /* No architectures currently implement __raw_writeq() */
136 *(volatile u64 *)addr = value;
137}
138
Stefan Roese45aa5a72008-11-17 14:45:22 +0100139static u8 __flash_read8(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100140{
141 return __raw_readb(addr);
142}
143
Stefan Roese45aa5a72008-11-17 14:45:22 +0100144static u16 __flash_read16(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100145{
146 return __raw_readw(addr);
147}
148
Stefan Roese45aa5a72008-11-17 14:45:22 +0100149static u32 __flash_read32(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100150{
151 return __raw_readl(addr);
152}
153
Daniel Hellstrom97bf85d2008-03-28 20:40:19 +0100154static u64 __flash_read64(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100155{
156 /* No architectures currently implement __raw_readq() */
157 return *(volatile u64 *)addr;
158}
159
Stefan Roese45aa5a72008-11-17 14:45:22 +0100160#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
161void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
162void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
163void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
164void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
165u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
166u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
167u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
Daniel Hellstrom97bf85d2008-03-28 20:40:19 +0100168u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
Stefan Roese45aa5a72008-11-17 14:45:22 +0100169#else
170#define flash_write8 __flash_write8
171#define flash_write16 __flash_write16
172#define flash_write32 __flash_write32
173#define flash_write64 __flash_write64
174#define flash_read8 __flash_read8
175#define flash_read16 __flash_read16
176#define flash_read32 __flash_read32
177#define flash_read64 __flash_read64
178#endif
Daniel Hellstrom97bf85d2008-03-28 20:40:19 +0100179
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200180/*-----------------------------------------------------------------------
181 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200182#if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
Heiko Schocher4f975672009-02-10 09:53:29 +0100183flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200184{
185 int i;
Stefan Roesecba34aa2010-08-30 11:14:38 +0200186 flash_info_t *info = NULL;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200187
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200188 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200189 info = & flash_info[i];
190 if (info->size && info->start[0] <= base &&
191 base <= info->start[0] + info->size - 1)
192 break;
193 }
194
Stefan Roesecba34aa2010-08-30 11:14:38 +0200195 return info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200196}
wdenk5653fc32004-02-08 22:55:38 +0000197#endif
198
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100199unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
200{
201 if (sect != (info->sector_count - 1))
202 return info->start[sect + 1] - info->start[sect];
203 else
204 return info->start[0] + info->size - info->start[sect];
205}
206
wdenk5653fc32004-02-08 22:55:38 +0000207/*-----------------------------------------------------------------------
208 * create an address based on the offset and the port width
209 */
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100210static inline void *
211flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000212{
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100213 unsigned int byte_offset = offset * info->portwidth;
214
Becky Bruce09ce9922009-02-02 16:34:51 -0600215 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100216}
217
218static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
219 unsigned int offset, void *addr)
220{
wdenk5653fc32004-02-08 22:55:38 +0000221}
wdenkbf9e3b32004-02-12 00:47:09 +0000222
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200223/*-----------------------------------------------------------------------
224 * make a proper sized command based on the port and chip widths
225 */
Sebastian Siewior7288f972008-07-15 13:35:23 +0200226static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200227{
228 int i;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400229 int cword_offset;
230 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200231#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewior340ccb22008-07-16 20:04:49 +0200232 u32 cmd_le = cpu_to_le32(cmd);
233#endif
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400234 uchar val;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200235 uchar *cp = (uchar *) cmdbuf;
236
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400237 for (i = info->portwidth; i > 0; i--){
238 cword_offset = (info->portwidth-i)%info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200239#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400240 cp_offset = info->portwidth - i;
Sebastian Siewior340ccb22008-07-16 20:04:49 +0200241 val = *((uchar*)&cmd_le + cword_offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200242#else
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400243 cp_offset = i - 1;
Sebastian Siewior7288f972008-07-15 13:35:23 +0200244 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200245#endif
Sebastian Siewior7288f972008-07-15 13:35:23 +0200246 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400247 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200248}
249
wdenkbf9e3b32004-02-12 00:47:09 +0000250#ifdef DEBUG
251/*-----------------------------------------------------------------------
252 * Debug support
253 */
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100254static void print_longlong (char *str, unsigned long long data)
wdenkbf9e3b32004-02-12 00:47:09 +0000255{
256 int i;
257 char *cp;
258
Wolfgang Denk657f2062009-02-04 09:42:20 +0100259 cp = (char *) &data;
wdenkbf9e3b32004-02-12 00:47:09 +0000260 for (i = 0; i < 8; i++)
261 sprintf (&str[i * 2], "%2.2x", *cp++);
262}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200263
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100264static void flash_printqry (struct cfi_qry *qry)
wdenkbf9e3b32004-02-12 00:47:09 +0000265{
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100266 u8 *p = (u8 *)qry;
wdenkbf9e3b32004-02-12 00:47:09 +0000267 int x, y;
268
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100269 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
270 debug("%02x : ", x);
271 for (y = 0; y < 16; y++)
272 debug("%2.2x ", p[x + y]);
273 debug(" ");
wdenkbf9e3b32004-02-12 00:47:09 +0000274 for (y = 0; y < 16; y++) {
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100275 unsigned char c = p[x + y];
276 if (c >= 0x20 && c <= 0x7e)
277 debug("%c", c);
278 else
279 debug(".");
wdenkbf9e3b32004-02-12 00:47:09 +0000280 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100281 debug("\n");
wdenkbf9e3b32004-02-12 00:47:09 +0000282 }
283}
wdenkbf9e3b32004-02-12 00:47:09 +0000284#endif
285
286
wdenk5653fc32004-02-08 22:55:38 +0000287/*-----------------------------------------------------------------------
288 * read a character at a port width address
289 */
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100290static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000291{
292 uchar *cp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100293 uchar retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000294
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100295 cp = flash_map (info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200296#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100297 retval = flash_read8(cp);
wdenkbf9e3b32004-02-12 00:47:09 +0000298#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100299 retval = flash_read8(cp + info->portwidth - 1);
wdenkbf9e3b32004-02-12 00:47:09 +0000300#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100301 flash_unmap (info, 0, offset, cp);
302 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000303}
304
305/*-----------------------------------------------------------------------
Tor Krill90447ec2008-03-28 11:29:10 +0100306 * read a word at a port width address, assume 16bit bus
307 */
308static inline ushort flash_read_word (flash_info_t * info, uint offset)
309{
310 ushort *addr, retval;
311
312 addr = flash_map (info, 0, offset);
313 retval = flash_read16 (addr);
314 flash_unmap (info, 0, offset, addr);
315 return retval;
316}
317
318
319/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +0100320 * read a long word by picking the least significant byte of each maximum
wdenk5653fc32004-02-08 22:55:38 +0000321 * port size word. Swap for ppc format.
322 */
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100323static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
324 uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000325{
wdenkbf9e3b32004-02-12 00:47:09 +0000326 uchar *addr;
327 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000328
wdenkbf9e3b32004-02-12 00:47:09 +0000329#ifdef DEBUG
330 int x;
331#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100332 addr = flash_map (info, sect, offset);
wdenkbf9e3b32004-02-12 00:47:09 +0000333
334#ifdef DEBUG
335 debug ("long addr is at %p info->portwidth = %d\n", addr,
336 info->portwidth);
337 for (x = 0; x < 4 * info->portwidth; x++) {
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100338 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenkbf9e3b32004-02-12 00:47:09 +0000339 }
340#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200341#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100342 retval = ((flash_read8(addr) << 16) |
343 (flash_read8(addr + info->portwidth) << 24) |
344 (flash_read8(addr + 2 * info->portwidth)) |
345 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenkbf9e3b32004-02-12 00:47:09 +0000346#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100347 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
348 (flash_read8(addr + info->portwidth - 1) << 16) |
349 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
350 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenkbf9e3b32004-02-12 00:47:09 +0000351#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100352 flash_unmap(info, sect, offset, addr);
353
wdenkbf9e3b32004-02-12 00:47:09 +0000354 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000355}
356
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200357/*
358 * Write a proper sized command to the correct address
359 */
Stefan Roesefa36ae72009-10-27 15:15:55 +0100360void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
361 uint offset, u32 cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200362{
Stefan Roese79b4cda2006-02-28 15:29:58 +0100363
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100364 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200365 cfiword_t cword;
366
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100367 addr = flash_map (info, sect, offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200368 flash_make_cmd (info, cmd, &cword);
369 switch (info->portwidth) {
370 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100371 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200372 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100373 flash_write8(cword.c, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200374 break;
375 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100376 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200377 cmd, cword.w,
378 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100379 flash_write16(cword.w, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200380 break;
381 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100382 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200383 cmd, cword.l,
384 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100385 flash_write32(cword.l, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200386 break;
387 case FLASH_CFI_64BIT:
388#ifdef DEBUG
389 {
390 char str[20];
391
392 print_longlong (str, cword.ll);
393
394 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100395 addr, cmd, str,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200396 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
397 }
398#endif
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100399 flash_write64(cword.ll, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200400 break;
401 }
402
403 /* Ensure all the instructions are fully finished */
404 sync();
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100405
406 flash_unmap(info, sect, offset, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200407}
408
409static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
410{
411 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
412 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
413}
414
415/*-----------------------------------------------------------------------
416 */
417static int flash_isequal (flash_info_t * info, flash_sect_t sect,
418 uint offset, uchar cmd)
419{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100420 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200421 cfiword_t cword;
422 int retval;
423
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100424 addr = flash_map (info, sect, offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200425 flash_make_cmd (info, cmd, &cword);
426
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100427 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200428 switch (info->portwidth) {
429 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100430 debug ("is= %x %x\n", flash_read8(addr), cword.c);
431 retval = (flash_read8(addr) == cword.c);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200432 break;
433 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100434 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
435 retval = (flash_read16(addr) == cword.w);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200436 break;
437 case FLASH_CFI_32BIT:
Andrew Klossner52514692008-08-21 07:12:26 -0700438 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100439 retval = (flash_read32(addr) == cword.l);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200440 break;
441 case FLASH_CFI_64BIT:
442#ifdef DEBUG
443 {
444 char str1[20];
445 char str2[20];
446
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100447 print_longlong (str1, flash_read64(addr));
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200448 print_longlong (str2, cword.ll);
449 debug ("is= %s %s\n", str1, str2);
450 }
451#endif
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100452 retval = (flash_read64(addr) == cword.ll);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200453 break;
454 default:
455 retval = 0;
456 break;
457 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100458 flash_unmap(info, sect, offset, addr);
459
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200460 return retval;
461}
462
463/*-----------------------------------------------------------------------
464 */
465static int flash_isset (flash_info_t * info, flash_sect_t sect,
466 uint offset, uchar cmd)
467{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100468 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200469 cfiword_t cword;
470 int retval;
471
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100472 addr = flash_map (info, sect, offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200473 flash_make_cmd (info, cmd, &cword);
474 switch (info->portwidth) {
475 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100476 retval = ((flash_read8(addr) & cword.c) == cword.c);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200477 break;
478 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100479 retval = ((flash_read16(addr) & cword.w) == cword.w);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200480 break;
481 case FLASH_CFI_32BIT:
Stefan Roese47cc23c2008-01-02 14:05:37 +0100482 retval = ((flash_read32(addr) & cword.l) == cword.l);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200483 break;
484 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100485 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200486 break;
487 default:
488 retval = 0;
489 break;
490 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100491 flash_unmap(info, sect, offset, addr);
492
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200493 return retval;
494}
495
496/*-----------------------------------------------------------------------
497 */
498static int flash_toggle (flash_info_t * info, flash_sect_t sect,
499 uint offset, uchar cmd)
500{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100501 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200502 cfiword_t cword;
503 int retval;
504
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100505 addr = flash_map (info, sect, offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200506 flash_make_cmd (info, cmd, &cword);
507 switch (info->portwidth) {
508 case FLASH_CFI_8BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200509 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200510 break;
511 case FLASH_CFI_16BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200512 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200513 break;
514 case FLASH_CFI_32BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200515 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200516 break;
517 case FLASH_CFI_64BIT:
Wolfgang Denk9abda6b2008-10-31 01:12:28 +0100518 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
519 (flash_read32(addr+4) != flash_read32(addr+4)) );
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200520 break;
521 default:
522 retval = 0;
523 break;
524 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100525 flash_unmap(info, sect, offset, addr);
526
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200527 return retval;
528}
529
530/*
531 * flash_is_busy - check to see if the flash is busy
532 *
533 * This routine checks the status of the chip and returns true if the
534 * chip is busy.
535 */
536static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
537{
538 int retval;
539
540 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400541 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200542 case CFI_CMDSET_INTEL_STANDARD:
543 case CFI_CMDSET_INTEL_EXTENDED:
544 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
545 break;
546 case CFI_CMDSET_AMD_STANDARD:
547 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100548#ifdef CONFIG_FLASH_CFI_LEGACY
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200549 case CFI_CMDSET_AMD_LEGACY:
550#endif
551 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
552 break;
553 default:
554 retval = 0;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100555 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200556 debug ("flash_is_busy: %d\n", retval);
557 return retval;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100558}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200559
560/*-----------------------------------------------------------------------
561 * wait for XSR.7 to be set. Time out with an error if it does not.
562 * This routine does not set the flash to read-array mode.
563 */
564static int flash_status_check (flash_info_t * info, flash_sect_t sector,
565 ulong tout, char *prompt)
566{
567 ulong start;
568
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200569#if CONFIG_SYS_HZ != 1000
Renato Andreolac40c94a2010-03-24 23:00:47 +0800570 if ((ulong)CONFIG_SYS_HZ > 100000)
571 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
572 else
573 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200574#endif
575
576 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000577#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800578 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000579#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200580 start = get_timer (0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100581 WATCHDOG_RESET();
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200582 while (flash_is_busy (info, sector)) {
583 if (get_timer (start) > tout) {
584 printf ("Flash %s timeout at address %lx data %lx\n",
585 prompt, info->start[sector],
586 flash_read_long (info, sector, 0));
587 flash_write_cmd (info, sector, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -0700588 udelay(1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200589 return ERR_TIMOUT;
590 }
591 udelay (1); /* also triggers watchdog */
592 }
593 return ERR_OK;
594}
595
596/*-----------------------------------------------------------------------
597 * Wait for XSR.7 to be set, if it times out print an error, otherwise
598 * do a full status check.
599 *
600 * This routine sets the flash to read-array mode.
601 */
602static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
603 ulong tout, char *prompt)
604{
605 int retcode;
606
607 retcode = flash_status_check (info, sector, tout, prompt);
608 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400609 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200610 case CFI_CMDSET_INTEL_EXTENDED:
611 case CFI_CMDSET_INTEL_STANDARD:
Ed Swarthout0d01f662008-10-09 01:26:36 -0500612 if ((retcode != ERR_OK)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200613 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
614 retcode = ERR_INVAL;
615 printf ("Flash %s error at address %lx\n", prompt,
616 info->start[sector]);
617 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
618 FLASH_STATUS_PSLBS)) {
619 puts ("Command Sequence Error.\n");
620 } else if (flash_isset (info, sector, 0,
621 FLASH_STATUS_ECLBS)) {
622 puts ("Block Erase Error.\n");
623 retcode = ERR_NOT_ERASED;
624 } else if (flash_isset (info, sector, 0,
625 FLASH_STATUS_PSLBS)) {
626 puts ("Locking Error\n");
627 }
628 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
629 puts ("Block locked.\n");
630 retcode = ERR_PROTECTED;
631 }
632 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
633 puts ("Vpp Low Error.\n");
634 }
635 flash_write_cmd (info, sector, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -0700636 udelay(1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200637 break;
638 default:
639 break;
640 }
641 return retcode;
642}
643
Thomas Choue5720822010-03-26 08:17:00 +0800644static int use_flash_status_poll(flash_info_t *info)
645{
646#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
647 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
648 info->vendor == CFI_CMDSET_AMD_STANDARD)
649 return 1;
650#endif
651 return 0;
652}
653
654static int flash_status_poll(flash_info_t *info, void *src, void *dst,
655 ulong tout, char *prompt)
656{
657#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
658 ulong start;
659 int ready;
660
661#if CONFIG_SYS_HZ != 1000
662 if ((ulong)CONFIG_SYS_HZ > 100000)
663 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
664 else
665 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
666#endif
667
668 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000669#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800670 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000671#endif
Thomas Choue5720822010-03-26 08:17:00 +0800672 start = get_timer(0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100673 WATCHDOG_RESET();
Thomas Choue5720822010-03-26 08:17:00 +0800674 while (1) {
675 switch (info->portwidth) {
676 case FLASH_CFI_8BIT:
677 ready = flash_read8(dst) == flash_read8(src);
678 break;
679 case FLASH_CFI_16BIT:
680 ready = flash_read16(dst) == flash_read16(src);
681 break;
682 case FLASH_CFI_32BIT:
683 ready = flash_read32(dst) == flash_read32(src);
684 break;
685 case FLASH_CFI_64BIT:
686 ready = flash_read64(dst) == flash_read64(src);
687 break;
688 default:
689 ready = 0;
690 break;
691 }
692 if (ready)
693 break;
694 if (get_timer(start) > tout) {
695 printf("Flash %s timeout at address %lx data %lx\n",
696 prompt, (ulong)dst, (ulong)flash_read8(dst));
697 return ERR_TIMOUT;
698 }
699 udelay(1); /* also triggers watchdog */
700 }
701#endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
702 return ERR_OK;
703}
704
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200705/*-----------------------------------------------------------------------
706 */
707static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
708{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200709#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200710 unsigned short w;
711 unsigned int l;
712 unsigned long long ll;
713#endif
714
715 switch (info->portwidth) {
716 case FLASH_CFI_8BIT:
717 cword->c = c;
718 break;
719 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200720#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200721 w = c;
722 w <<= 8;
723 cword->w = (cword->w >> 8) | w;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100724#else
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200725 cword->w = (cword->w << 8) | c;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100726#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200727 break;
728 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200729#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200730 l = c;
731 l <<= 24;
732 cword->l = (cword->l >> 8) | l;
733#else
734 cword->l = (cword->l << 8) | c;
Stefan Roese2662b402006-04-01 13:41:03 +0200735#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200736 break;
737 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200738#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200739 ll = c;
740 ll <<= 56;
741 cword->ll = (cword->ll >> 8) | ll;
742#else
743 cword->ll = (cword->ll << 8) | c;
744#endif
745 break;
wdenk5653fc32004-02-08 22:55:38 +0000746 }
wdenk5653fc32004-02-08 22:55:38 +0000747}
748
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100749/*
750 * Loop through the sector table starting from the previously found sector.
751 * Searches forwards or backwards, dependent on the passed address.
wdenk5653fc32004-02-08 22:55:38 +0000752 */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200753static flash_sect_t find_sector (flash_info_t * info, ulong addr)
wdenk7680c142005-05-16 15:23:22 +0000754{
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100755 static flash_sect_t saved_sector = 0; /* previously found sector */
Martin Krauseaf567302011-03-21 18:07:56 +0100756 static flash_info_t *saved_info = 0; /* previously used flash bank */
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100757 flash_sect_t sector = saved_sector;
wdenk7680c142005-05-16 15:23:22 +0000758
Martin Krauseaf567302011-03-21 18:07:56 +0100759 if ((info != saved_info) || (sector >= info->sector_count))
760 sector = 0;
761
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100762 while ((info->start[sector] < addr)
763 && (sector < info->sector_count - 1))
764 sector++;
765 while ((info->start[sector] > addr) && (sector > 0))
766 /*
767 * also decrements the sector in case of an overshot
768 * in the first loop
769 */
770 sector--;
771
772 saved_sector = sector;
Martin Krauseaf567302011-03-21 18:07:56 +0100773 saved_info = info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200774 return sector;
wdenk7680c142005-05-16 15:23:22 +0000775}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200776
777/*-----------------------------------------------------------------------
778 */
779static int flash_write_cfiword (flash_info_t * info, ulong dest,
780 cfiword_t cword)
781{
Becky Bruce09ce9922009-02-02 16:34:51 -0600782 void *dstaddr = (void *)dest;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200783 int flag;
Jens Gehrleina7292872008-12-16 17:25:54 +0100784 flash_sect_t sect = 0;
785 char sect_found = 0;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200786
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200787 /* Check if Flash is (sufficiently) erased */
788 switch (info->portwidth) {
789 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100790 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200791 break;
792 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100793 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200794 break;
795 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100796 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200797 break;
798 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100799 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200800 break;
801 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100802 flag = 0;
803 break;
804 }
Becky Bruce09ce9922009-02-02 16:34:51 -0600805 if (!flag)
Stefan Roese0dc80e22007-12-27 07:50:54 +0100806 return ERR_NOT_ERASED;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200807
808 /* Disable interrupts which might cause a timeout here */
809 flag = disable_interrupts ();
810
811 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400812 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200813 case CFI_CMDSET_INTEL_EXTENDED:
814 case CFI_CMDSET_INTEL_STANDARD:
815 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
816 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
817 break;
818 case CFI_CMDSET_AMD_EXTENDED:
819 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout0d01f662008-10-09 01:26:36 -0500820 sect = find_sector(info, dest);
821 flash_unlock_seq (info, sect);
822 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrleina7292872008-12-16 17:25:54 +0100823 sect_found = 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200824 break;
Po-Yu Chuangb4db4a72009-07-10 18:03:57 +0800825#ifdef CONFIG_FLASH_CFI_LEGACY
826 case CFI_CMDSET_AMD_LEGACY:
827 sect = find_sector(info, dest);
828 flash_unlock_seq (info, 0);
829 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
830 sect_found = 1;
831 break;
832#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200833 }
834
835 switch (info->portwidth) {
836 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100837 flash_write8(cword.c, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200838 break;
839 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100840 flash_write16(cword.w, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200841 break;
842 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100843 flash_write32(cword.l, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200844 break;
845 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100846 flash_write64(cword.ll, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200847 break;
848 }
849
850 /* re-enable interrupts if necessary */
851 if (flag)
852 enable_interrupts ();
853
Jens Gehrleina7292872008-12-16 17:25:54 +0100854 if (!sect_found)
855 sect = find_sector (info, dest);
856
Thomas Choue5720822010-03-26 08:17:00 +0800857 if (use_flash_status_poll(info))
858 return flash_status_poll(info, &cword, dstaddr,
859 info->write_tout, "write");
860 else
861 return flash_full_status_check(info, sect,
862 info->write_tout, "write");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200863}
864
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200865#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200866
867static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
868 int len)
869{
870 flash_sect_t sector;
871 int cnt;
872 int retcode;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100873 void *src = cp;
Stefan Roeseec21d5c2009-02-05 11:25:57 +0100874 void *dst = (void *)dest;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100875 void *dst2 = dst;
Tao Hou85c344e2012-03-15 23:33:58 +0800876 int flag = 1;
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200877 uint offset = 0;
878 unsigned int shift;
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400879 uchar write_cmd;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100880
Stefan Roese0dc80e22007-12-27 07:50:54 +0100881 switch (info->portwidth) {
882 case FLASH_CFI_8BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200883 shift = 0;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100884 break;
885 case FLASH_CFI_16BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200886 shift = 1;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100887 break;
888 case FLASH_CFI_32BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200889 shift = 2;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100890 break;
891 case FLASH_CFI_64BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200892 shift = 3;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100893 break;
894 default:
895 retcode = ERR_INVAL;
896 goto out_unmap;
897 }
898
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200899 cnt = len >> shift;
900
Tao Hou85c344e2012-03-15 23:33:58 +0800901 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese0dc80e22007-12-27 07:50:54 +0100902 switch (info->portwidth) {
903 case FLASH_CFI_8BIT:
904 flag = ((flash_read8(dst2) & flash_read8(src)) ==
905 flash_read8(src));
906 src += 1, dst2 += 1;
907 break;
908 case FLASH_CFI_16BIT:
909 flag = ((flash_read16(dst2) & flash_read16(src)) ==
910 flash_read16(src));
911 src += 2, dst2 += 2;
912 break;
913 case FLASH_CFI_32BIT:
914 flag = ((flash_read32(dst2) & flash_read32(src)) ==
915 flash_read32(src));
916 src += 4, dst2 += 4;
917 break;
918 case FLASH_CFI_64BIT:
919 flag = ((flash_read64(dst2) & flash_read64(src)) ==
920 flash_read64(src));
921 src += 8, dst2 += 8;
922 break;
923 }
924 }
925 if (!flag) {
926 retcode = ERR_NOT_ERASED;
927 goto out_unmap;
928 }
929
930 src = cp;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100931 sector = find_sector (info, dest);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200932
933 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400934 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200935 case CFI_CMDSET_INTEL_STANDARD:
936 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400937 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
938 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200939 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400940 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
941 flash_write_cmd (info, sector, 0, write_cmd);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200942 retcode = flash_status_check (info, sector,
943 info->buffer_write_tout,
944 "write to buffer");
945 if (retcode == ERR_OK) {
946 /* reduce the number of loops by the width of
947 * the port */
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200948 cnt = len >> shift;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400949 flash_write_cmd (info, sector, 0, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200950 while (cnt-- > 0) {
951 switch (info->portwidth) {
952 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100953 flash_write8(flash_read8(src), dst);
954 src += 1, dst += 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200955 break;
956 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100957 flash_write16(flash_read16(src), dst);
958 src += 2, dst += 2;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200959 break;
960 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100961 flash_write32(flash_read32(src), dst);
962 src += 4, dst += 4;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200963 break;
964 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100965 flash_write64(flash_read64(src), dst);
966 src += 8, dst += 8;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200967 break;
968 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100969 retcode = ERR_INVAL;
970 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200971 }
972 }
973 flash_write_cmd (info, sector, 0,
974 FLASH_CMD_WRITE_BUFFER_CONFIRM);
975 retcode = flash_full_status_check (
976 info, sector, info->buffer_write_tout,
977 "buffer write");
978 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100979
980 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200981
982 case CFI_CMDSET_AMD_STANDARD:
983 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200984 flash_unlock_seq(info,0);
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200985
986#ifdef CONFIG_FLASH_SPANSION_S29WS_N
987 offset = ((unsigned long)dst - info->start[sector]) >> shift;
988#endif
989 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
990 cnt = len >> shift;
John Schmoller7dedefd2009-08-12 10:55:47 -0500991 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200992
993 switch (info->portwidth) {
994 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100995 while (cnt-- > 0) {
996 flash_write8(flash_read8(src), dst);
997 src += 1, dst += 1;
998 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200999 break;
1000 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001001 while (cnt-- > 0) {
1002 flash_write16(flash_read16(src), dst);
1003 src += 2, dst += 2;
1004 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001005 break;
1006 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001007 while (cnt-- > 0) {
1008 flash_write32(flash_read32(src), dst);
1009 src += 4, dst += 4;
1010 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001011 break;
1012 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001013 while (cnt-- > 0) {
1014 flash_write64(flash_read64(src), dst);
1015 src += 8, dst += 8;
1016 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001017 break;
1018 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001019 retcode = ERR_INVAL;
1020 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001021 }
1022
1023 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Choue5720822010-03-26 08:17:00 +08001024 if (use_flash_status_poll(info))
1025 retcode = flash_status_poll(info, src - (1 << shift),
1026 dst - (1 << shift),
1027 info->buffer_write_tout,
1028 "buffer write");
1029 else
1030 retcode = flash_full_status_check(info, sector,
1031 info->buffer_write_tout,
1032 "buffer write");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001033 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001034
1035 default:
1036 debug ("Unknown Command Set\n");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001037 retcode = ERR_INVAL;
1038 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001039 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001040
1041out_unmap:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001042 return retcode;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001043}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001044#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001045
wdenk7680c142005-05-16 15:23:22 +00001046
1047/*-----------------------------------------------------------------------
1048 */
wdenkbf9e3b32004-02-12 00:47:09 +00001049int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +00001050{
1051 int rcode = 0;
1052 int prot;
1053 flash_sect_t sect;
Thomas Choue5720822010-03-26 08:17:00 +08001054 int st;
wdenk5653fc32004-02-08 22:55:38 +00001055
wdenkbf9e3b32004-02-12 00:47:09 +00001056 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +00001057 puts ("Can't erase unknown flash type - aborted\n");
wdenk5653fc32004-02-08 22:55:38 +00001058 return 1;
1059 }
1060 if ((s_first < 0) || (s_first > s_last)) {
wdenk4b9206e2004-03-23 22:14:11 +00001061 puts ("- no sectors to erase\n");
wdenk5653fc32004-02-08 22:55:38 +00001062 return 1;
1063 }
1064
1065 prot = 0;
wdenkbf9e3b32004-02-12 00:47:09 +00001066 for (sect = s_first; sect <= s_last; ++sect) {
wdenk5653fc32004-02-08 22:55:38 +00001067 if (info->protect[sect]) {
1068 prot++;
1069 }
1070 }
1071 if (prot) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001072 printf ("- Warning: %d protected sectors will not be erased!\n",
1073 prot);
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001074 } else if (flash_verbose) {
wdenk4b9206e2004-03-23 22:14:11 +00001075 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +00001076 }
1077
1078
wdenkbf9e3b32004-02-12 00:47:09 +00001079 for (sect = s_first; sect <= s_last; sect++) {
Joe Hershbergerde15a062012-08-17 15:36:41 -05001080 if (ctrlc()) {
1081 printf("\n");
1082 return 1;
1083 }
1084
wdenk5653fc32004-02-08 22:55:38 +00001085 if (info->protect[sect] == 0) { /* not protected */
Joe Hershberger6822a642012-08-17 15:36:40 -05001086#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1087 int k;
1088 int size;
1089 int erased;
1090 u32 *flash;
1091
1092 /*
1093 * Check if whole sector is erased
1094 */
1095 size = flash_sector_size(info, sect);
1096 erased = 1;
1097 flash = (u32 *)info->start[sect];
1098 /* divide by 4 for longword access */
1099 size = size >> 2;
1100 for (k = 0; k < size; k++) {
1101 if (flash_read32(flash++) != 0xffffffff) {
1102 erased = 0;
1103 break;
1104 }
1105 }
1106 if (erased) {
1107 if (flash_verbose)
1108 putc(',');
1109 continue;
1110 }
1111#endif
wdenkbf9e3b32004-02-12 00:47:09 +00001112 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001113 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00001114 case CFI_CMDSET_INTEL_STANDARD:
1115 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001116 flash_write_cmd (info, sect, 0,
1117 FLASH_CMD_CLEAR_STATUS);
1118 flash_write_cmd (info, sect, 0,
1119 FLASH_CMD_BLOCK_ERASE);
1120 flash_write_cmd (info, sect, 0,
1121 FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +00001122 break;
1123 case CFI_CMDSET_AMD_STANDARD:
1124 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +00001125 flash_unlock_seq (info, sect);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001126 flash_write_cmd (info, sect,
1127 info->addr_unlock1,
1128 AMD_CMD_ERASE_START);
wdenkbf9e3b32004-02-12 00:47:09 +00001129 flash_unlock_seq (info, sect);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001130 flash_write_cmd (info, sect, 0,
1131 AMD_CMD_ERASE_SECTOR);
wdenk5653fc32004-02-08 22:55:38 +00001132 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001133#ifdef CONFIG_FLASH_CFI_LEGACY
1134 case CFI_CMDSET_AMD_LEGACY:
1135 flash_unlock_seq (info, 0);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001136 flash_write_cmd (info, 0, info->addr_unlock1,
1137 AMD_CMD_ERASE_START);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001138 flash_unlock_seq (info, 0);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001139 flash_write_cmd (info, sect, 0,
1140 AMD_CMD_ERASE_SECTOR);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001141 break;
1142#endif
wdenk5653fc32004-02-08 22:55:38 +00001143 default:
wdenkbf9e3b32004-02-12 00:47:09 +00001144 debug ("Unkown flash vendor %d\n",
1145 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +00001146 break;
1147 }
1148
Thomas Choue5720822010-03-26 08:17:00 +08001149 if (use_flash_status_poll(info)) {
1150 cfiword_t cword = (cfiword_t)0xffffffffffffffffULL;
1151 void *dest;
1152 dest = flash_map(info, sect, 0);
1153 st = flash_status_poll(info, &cword, dest,
1154 info->erase_blk_tout, "erase");
1155 flash_unmap(info, sect, 0, dest);
1156 } else
1157 st = flash_full_status_check(info, sect,
1158 info->erase_blk_tout,
1159 "erase");
1160 if (st)
wdenk5653fc32004-02-08 22:55:38 +00001161 rcode = 1;
Thomas Choue5720822010-03-26 08:17:00 +08001162 else if (flash_verbose)
wdenk4b9206e2004-03-23 22:14:11 +00001163 putc ('.');
wdenk5653fc32004-02-08 22:55:38 +00001164 }
1165 }
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001166
1167 if (flash_verbose)
1168 puts (" done\n");
1169
wdenk5653fc32004-02-08 22:55:38 +00001170 return rcode;
1171}
1172
Stefan Roese70084df2010-08-13 09:36:36 +02001173#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1174static int sector_erased(flash_info_t *info, int i)
1175{
1176 int k;
1177 int size;
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001178 u32 *flash;
Stefan Roese70084df2010-08-13 09:36:36 +02001179
1180 /*
1181 * Check if whole sector is erased
1182 */
1183 size = flash_sector_size(info, i);
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001184 flash = (u32 *)info->start[i];
Stefan Roese70084df2010-08-13 09:36:36 +02001185 /* divide by 4 for longword access */
1186 size = size >> 2;
1187
1188 for (k = 0; k < size; k++) {
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001189 if (flash_read32(flash++) != 0xffffffff)
Stefan Roese70084df2010-08-13 09:36:36 +02001190 return 0; /* not erased */
1191 }
1192
1193 return 1; /* erased */
1194}
1195#endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1196
wdenkbf9e3b32004-02-12 00:47:09 +00001197void flash_print_info (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +00001198{
1199 int i;
1200
1201 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +00001202 puts ("missing or unknown FLASH type\n");
wdenk5653fc32004-02-08 22:55:38 +00001203 return;
1204 }
1205
Peter Tysereddf52b2010-12-28 18:12:05 -06001206 printf ("%s flash (%d x %d)",
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001207 info->name,
wdenkbf9e3b32004-02-12 00:47:09 +00001208 (info->portwidth << 3), (info->chipwidth << 3));
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001209 if (info->size < 1024*1024)
1210 printf (" Size: %ld kB in %d Sectors\n",
1211 info->size >> 10, info->sector_count);
1212 else
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001213 printf (" Size: %ld MB in %d Sectors\n",
1214 info->size >> 20, info->sector_count);
Stefan Roese260421a2006-11-13 13:55:24 +01001215 printf (" ");
1216 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001217 case CFI_CMDSET_INTEL_PROG_REGIONS:
1218 printf ("Intel Prog Regions");
1219 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001220 case CFI_CMDSET_INTEL_STANDARD:
1221 printf ("Intel Standard");
1222 break;
1223 case CFI_CMDSET_INTEL_EXTENDED:
1224 printf ("Intel Extended");
1225 break;
1226 case CFI_CMDSET_AMD_STANDARD:
1227 printf ("AMD Standard");
1228 break;
1229 case CFI_CMDSET_AMD_EXTENDED:
1230 printf ("AMD Extended");
1231 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001232#ifdef CONFIG_FLASH_CFI_LEGACY
1233 case CFI_CMDSET_AMD_LEGACY:
1234 printf ("AMD Legacy");
1235 break;
1236#endif
Stefan Roese260421a2006-11-13 13:55:24 +01001237 default:
1238 printf ("Unknown (%d)", info->vendor);
1239 break;
1240 }
Philippe De Muyterd77c7ac2010-08-10 16:54:52 +02001241 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1242 info->manufacturer_id);
1243 printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1244 info->device_id);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001245 if ((info->device_id & 0xff) == 0x7E) {
1246 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1247 info->device_id2);
Stefan Roese260421a2006-11-13 13:55:24 +01001248 }
1249 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
wdenk028ab6b2004-02-23 23:54:43 +00001250 info->erase_blk_tout,
Stefan Roese260421a2006-11-13 13:55:24 +01001251 info->write_tout);
1252 if (info->buffer_size > 1) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001253 printf (" Buffer write timeout: %ld ms, "
1254 "buffer size: %d bytes\n",
wdenk028ab6b2004-02-23 23:54:43 +00001255 info->buffer_write_tout,
1256 info->buffer_size);
Stefan Roese260421a2006-11-13 13:55:24 +01001257 }
wdenk5653fc32004-02-08 22:55:38 +00001258
Stefan Roese260421a2006-11-13 13:55:24 +01001259 puts ("\n Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +00001260 for (i = 0; i < info->sector_count; ++i) {
Kim Phillips2e973942010-07-26 18:35:39 -05001261 if (ctrlc())
Stefan Roese70084df2010-08-13 09:36:36 +02001262 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001263 if ((i % 5) == 0)
Stefan Roese70084df2010-08-13 09:36:36 +02001264 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001265#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
wdenk5653fc32004-02-08 22:55:38 +00001266 /* print empty and read-only info */
Stefan Roese260421a2006-11-13 13:55:24 +01001267 printf (" %08lX %c %s ",
wdenk5653fc32004-02-08 22:55:38 +00001268 info->start[i],
Stefan Roese70084df2010-08-13 09:36:36 +02001269 sector_erased(info, i) ? 'E' : ' ',
Stefan Roese260421a2006-11-13 13:55:24 +01001270 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001271#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Stefan Roese260421a2006-11-13 13:55:24 +01001272 printf (" %08lX %s ",
1273 info->start[i],
1274 info->protect[i] ? "RO" : " ");
wdenk5653fc32004-02-08 22:55:38 +00001275#endif
1276 }
wdenk4b9206e2004-03-23 22:14:11 +00001277 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +00001278 return;
1279}
1280
1281/*-----------------------------------------------------------------------
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001282 * This is used in a few places in write_buf() to show programming
1283 * progress. Making it a function is nasty because it needs to do side
1284 * effect updates to digit and dots. Repeated code is nasty too, so
1285 * we define it once here.
1286 */
Stefan Roesef0105722008-03-19 07:09:26 +01001287#ifdef CONFIG_FLASH_SHOW_PROGRESS
1288#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001289 if (flash_verbose) { \
1290 dots -= dots_sub; \
1291 if ((scale > 0) && (dots <= 0)) { \
1292 if ((digit % 5) == 0) \
1293 printf ("%d", digit / 5); \
1294 else \
1295 putc ('.'); \
1296 digit--; \
1297 dots += scale; \
1298 } \
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001299 }
Stefan Roesef0105722008-03-19 07:09:26 +01001300#else
1301#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1302#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001303
1304/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001305 * Copy memory to flash, returns:
1306 * 0 - OK
1307 * 1 - write timeout
1308 * 2 - Flash not erased
1309 */
wdenkbf9e3b32004-02-12 00:47:09 +00001310int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +00001311{
1312 ulong wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001313 uchar *p;
wdenk5653fc32004-02-08 22:55:38 +00001314 int aln;
1315 cfiword_t cword;
1316 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001317#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001318 int buffered_size;
1319#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001320#ifdef CONFIG_FLASH_SHOW_PROGRESS
1321 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1322 int scale = 0;
1323 int dots = 0;
1324
1325 /*
1326 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1327 */
1328 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1329 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1330 CONFIG_FLASH_SHOW_PROGRESS);
1331 }
1332#endif
1333
wdenkbf9e3b32004-02-12 00:47:09 +00001334 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +00001335 wp = (addr & ~(info->portwidth - 1));
1336
1337 /* handle unaligned start */
wdenkbf9e3b32004-02-12 00:47:09 +00001338 if ((aln = addr - wp) != 0) {
wdenk5653fc32004-02-08 22:55:38 +00001339 cword.l = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001340 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001341 for (i = 0; i < aln; ++i)
1342 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001343
wdenkbf9e3b32004-02-12 00:47:09 +00001344 for (; (i < info->portwidth) && (cnt > 0); i++) {
1345 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001346 cnt--;
wdenk5653fc32004-02-08 22:55:38 +00001347 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001348 for (; (cnt == 0) && (i < info->portwidth); ++i)
1349 flash_add_byte (info, &cword, flash_read8(p + i));
1350
1351 rc = flash_write_cfiword (info, wp, cword);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001352 if (rc != 0)
wdenk5653fc32004-02-08 22:55:38 +00001353 return rc;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001354
1355 wp += i;
Stefan Roesef0105722008-03-19 07:09:26 +01001356 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
wdenk5653fc32004-02-08 22:55:38 +00001357 }
1358
wdenkbf9e3b32004-02-12 00:47:09 +00001359 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001360#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001361 buffered_size = (info->portwidth / info->chipwidth);
1362 buffered_size *= info->buffer_size;
1363 while (cnt >= info->portwidth) {
Stefan Roese79b4cda2006-02-28 15:29:58 +01001364 /* prohibit buffer write when buffer_size is 1 */
1365 if (info->buffer_size == 1) {
1366 cword.l = 0;
1367 for (i = 0; i < info->portwidth; i++)
1368 flash_add_byte (info, &cword, *src++);
1369 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1370 return rc;
1371 wp += info->portwidth;
1372 cnt -= info->portwidth;
1373 continue;
1374 }
1375
1376 /* write buffer until next buffered_size aligned boundary */
1377 i = buffered_size - (wp % buffered_size);
1378 if (i > cnt)
1379 i = cnt;
wdenkbf9e3b32004-02-12 00:47:09 +00001380 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +00001381 return rc;
Wolfgang Denk8d4ba3d2005-08-12 22:35:59 +02001382 i -= i & (info->portwidth - 1);
wdenk5653fc32004-02-08 22:55:38 +00001383 wp += i;
1384 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +00001385 cnt -= i;
Stefan Roesef0105722008-03-19 07:09:26 +01001386 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001387 /* Only check every once in a while */
1388 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1389 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001390 }
1391#else
wdenkbf9e3b32004-02-12 00:47:09 +00001392 while (cnt >= info->portwidth) {
wdenk5653fc32004-02-08 22:55:38 +00001393 cword.l = 0;
wdenkbf9e3b32004-02-12 00:47:09 +00001394 for (i = 0; i < info->portwidth; i++) {
1395 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001396 }
wdenkbf9e3b32004-02-12 00:47:09 +00001397 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +00001398 return rc;
1399 wp += info->portwidth;
1400 cnt -= info->portwidth;
Stefan Roesef0105722008-03-19 07:09:26 +01001401 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001402 /* Only check every once in a while */
1403 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1404 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001405 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001406#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001407
wdenk5653fc32004-02-08 22:55:38 +00001408 if (cnt == 0) {
1409 return (0);
1410 }
1411
1412 /*
1413 * handle unaligned tail bytes
1414 */
1415 cword.l = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001416 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001417 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
wdenkbf9e3b32004-02-12 00:47:09 +00001418 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001419 --cnt;
1420 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001421 for (; i < info->portwidth; ++i)
1422 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001423
wdenkbf9e3b32004-02-12 00:47:09 +00001424 return flash_write_cfiword (info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +00001425}
1426
1427/*-----------------------------------------------------------------------
1428 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001429#ifdef CONFIG_SYS_FLASH_PROTECTION
wdenk5653fc32004-02-08 22:55:38 +00001430
Holger Brunck81316a92012-08-09 10:22:41 +02001431static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1432{
1433 if ((info->manufacturer_id == (uchar)INTEL_MANUFACT) &&
1434 (info->device_id == NUMONYX_256MBIT)) {
1435 /*
1436 * see errata called
1437 * "Numonyx Axcell P33/P30 Specification Update" :)
1438 */
1439 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1440 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1441 prot)) {
1442 /*
1443 * cmd must come before FLASH_CMD_PROTECT + 20us
1444 * Disable interrupts which might cause a timeout here.
1445 */
1446 int flag = disable_interrupts();
1447 unsigned short cmd;
1448
1449 if (prot)
1450 cmd = FLASH_CMD_PROTECT_SET;
1451 else
1452 cmd = FLASH_CMD_PROTECT_CLEAR;
1453 flash_write_cmd(info, sector, 0,
1454 FLASH_CMD_PROTECT);
1455 flash_write_cmd(info, sector, 0, cmd);
1456 /* re-enable interrupts if necessary */
1457 if (flag)
1458 enable_interrupts();
1459 }
1460 return 1;
1461 }
1462 return 0;
1463}
1464
wdenkbf9e3b32004-02-12 00:47:09 +00001465int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +00001466{
1467 int retcode = 0;
1468
Rafael Camposbc9019e2008-07-31 10:22:20 +02001469 switch (info->vendor) {
1470 case CFI_CMDSET_INTEL_PROG_REGIONS:
1471 case CFI_CMDSET_INTEL_STANDARD:
Nick Spence9e8e63c2008-08-19 22:21:16 -07001472 case CFI_CMDSET_INTEL_EXTENDED:
Holger Brunck81316a92012-08-09 10:22:41 +02001473 if (!cfi_protect_bugfix(info, sector, prot)) {
1474 flash_write_cmd(info, sector, 0,
1475 FLASH_CMD_CLEAR_STATUS);
1476 flash_write_cmd(info, sector, 0,
1477 FLASH_CMD_PROTECT);
Philippe De Muyter54652992010-08-17 18:40:25 +02001478 if (prot)
Holger Brunck81316a92012-08-09 10:22:41 +02001479 flash_write_cmd(info, sector, 0,
1480 FLASH_CMD_PROTECT_SET);
Philippe De Muyter54652992010-08-17 18:40:25 +02001481 else
Holger Brunck81316a92012-08-09 10:22:41 +02001482 flash_write_cmd(info, sector, 0,
1483 FLASH_CMD_PROTECT_CLEAR);
Philippe De Muyter54652992010-08-17 18:40:25 +02001484
Philippe De Muyter54652992010-08-17 18:40:25 +02001485 }
Rafael Camposbc9019e2008-07-31 10:22:20 +02001486 break;
1487 case CFI_CMDSET_AMD_EXTENDED:
1488 case CFI_CMDSET_AMD_STANDARD:
Rafael Camposbc9019e2008-07-31 10:22:20 +02001489 /* U-Boot only checks the first byte */
1490 if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
1491 if (prot) {
1492 flash_unlock_seq (info, 0);
1493 flash_write_cmd (info, 0,
1494 info->addr_unlock1,
1495 ATM_CMD_SOFTLOCK_START);
1496 flash_unlock_seq (info, 0);
1497 flash_write_cmd (info, sector, 0,
1498 ATM_CMD_LOCK_SECT);
1499 } else {
1500 flash_write_cmd (info, 0,
1501 info->addr_unlock1,
1502 AMD_CMD_UNLOCK_START);
1503 if (info->device_id == ATM_ID_BV6416)
1504 flash_write_cmd (info, sector,
1505 0, ATM_CMD_UNLOCK_SECT);
1506 }
1507 }
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001508 if (info->manufacturer_id == (uchar)AMD_MANUFACT) {
1509 int flag = disable_interrupts();
1510 int lock_flag;
1511
1512 flash_unlock_seq(info, 0);
1513 flash_write_cmd(info, 0, info->addr_unlock1,
1514 AMD_CMD_SET_PPB_ENTRY);
1515 lock_flag = flash_isset(info, sector, 0, 0x01);
1516 if (prot) {
1517 if (lock_flag) {
1518 flash_write_cmd(info, sector, 0,
1519 AMD_CMD_PPB_LOCK_BC1);
1520 flash_write_cmd(info, sector, 0,
1521 AMD_CMD_PPB_LOCK_BC2);
1522 }
1523 debug("sector %ld %slocked\n", sector,
1524 lock_flag ? "" : "already ");
1525 } else {
1526 if (!lock_flag) {
1527 debug("unlock %ld\n", sector);
1528 flash_write_cmd(info, 0, 0,
1529 AMD_CMD_PPB_UNLOCK_BC1);
1530 flash_write_cmd(info, 0, 0,
1531 AMD_CMD_PPB_UNLOCK_BC2);
1532 }
1533 debug("sector %ld %sunlocked\n", sector,
1534 !lock_flag ? "" : "already ");
1535 }
1536 if (flag)
1537 enable_interrupts();
1538
1539 if (flash_status_check(info, sector,
1540 info->erase_blk_tout,
1541 prot ? "protect" : "unprotect"))
1542 printf("status check error\n");
1543
1544 flash_write_cmd(info, 0, 0,
1545 AMD_CMD_SET_PPB_EXIT_BC1);
1546 flash_write_cmd(info, 0, 0,
1547 AMD_CMD_SET_PPB_EXIT_BC2);
1548 }
Rafael Camposbc9019e2008-07-31 10:22:20 +02001549 break;
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001550#ifdef CONFIG_FLASH_CFI_LEGACY
1551 case CFI_CMDSET_AMD_LEGACY:
1552 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1553 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1554 if (prot)
1555 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1556 else
1557 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1558#endif
Rafael Camposbc9019e2008-07-31 10:22:20 +02001559 };
wdenk5653fc32004-02-08 22:55:38 +00001560
Stefan Roesedf4e8132010-10-25 18:31:29 +02001561 /*
1562 * Flash needs to be in status register read mode for
1563 * flash_full_status_check() to work correctly
1564 */
1565 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
wdenkbf9e3b32004-02-12 00:47:09 +00001566 if ((retcode =
1567 flash_full_status_check (info, sector, info->erase_blk_tout,
1568 prot ? "protect" : "unprotect")) == 0) {
wdenk5653fc32004-02-08 22:55:38 +00001569
1570 info->protect[sector] = prot;
Stefan Roese2662b402006-04-01 13:41:03 +02001571
1572 /*
1573 * On some of Intel's flash chips (marked via legacy_unlock)
1574 * unprotect unprotects all locking.
1575 */
1576 if ((prot == 0) && (info->legacy_unlock)) {
wdenk5653fc32004-02-08 22:55:38 +00001577 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +00001578
1579 for (i = 0; i < info->sector_count; i++) {
1580 if (info->protect[i])
1581 flash_real_protect (info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +00001582 }
1583 }
1584 }
wdenk5653fc32004-02-08 22:55:38 +00001585 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +00001586}
1587
wdenk5653fc32004-02-08 22:55:38 +00001588/*-----------------------------------------------------------------------
1589 * flash_read_user_serial - read the OneTimeProgramming cells
1590 */
wdenkbf9e3b32004-02-12 00:47:09 +00001591void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1592 int len)
wdenk5653fc32004-02-08 22:55:38 +00001593{
wdenkbf9e3b32004-02-12 00:47:09 +00001594 uchar *src;
1595 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +00001596
1597 dst = buffer;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001598 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
wdenkbf9e3b32004-02-12 00:47:09 +00001599 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1600 memcpy (dst, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001601 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001602 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001603 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001604}
wdenkbf9e3b32004-02-12 00:47:09 +00001605
wdenk5653fc32004-02-08 22:55:38 +00001606/*
1607 * flash_read_factory_serial - read the device Id from the protection area
1608 */
wdenkbf9e3b32004-02-12 00:47:09 +00001609void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1610 int len)
wdenk5653fc32004-02-08 22:55:38 +00001611{
wdenkbf9e3b32004-02-12 00:47:09 +00001612 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +00001613
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001614 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
wdenkbf9e3b32004-02-12 00:47:09 +00001615 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1616 memcpy (buffer, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001617 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001618 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001619 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001620}
1621
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001622#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00001623
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001624/*-----------------------------------------------------------------------
1625 * Reverse the order of the erase regions in the CFI QRY structure.
1626 * This is needed for chips that are either a) correctly detected as
1627 * top-boot, or b) buggy.
1628 */
1629static void cfi_reverse_geometry(struct cfi_qry *qry)
1630{
1631 unsigned int i, j;
1632 u32 tmp;
1633
1634 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1635 tmp = qry->erase_region_info[i];
1636 qry->erase_region_info[i] = qry->erase_region_info[j];
1637 qry->erase_region_info[j] = tmp;
1638 }
1639}
wdenk5653fc32004-02-08 22:55:38 +00001640
1641/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +01001642 * read jedec ids from device and set corresponding fields in info struct
1643 *
1644 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1645 *
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001646 */
1647static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1648{
1649 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001650 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001651 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1652 udelay(1000); /* some flash are slow to respond */
1653 info->manufacturer_id = flash_read_uchar (info,
1654 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyterd77c7ac2010-08-10 16:54:52 +02001655 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1656 flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
1657 flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001658 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1659}
1660
1661static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1662{
1663 info->cmd_reset = FLASH_CMD_RESET;
1664
1665 cmdset_intel_read_jedec_ids(info);
1666 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1667
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001668#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001669 /* read legacy lock/unlock bit from intel flash */
1670 if (info->ext_addr) {
1671 info->legacy_unlock = flash_read_uchar (info,
1672 info->ext_addr + 5) & 0x08;
1673 }
1674#endif
1675
1676 return 0;
1677}
1678
1679static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1680{
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001681 ushort bankId = 0;
1682 uchar manuId;
1683
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001684 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1685 flash_unlock_seq(info, 0);
1686 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1687 udelay(1000); /* some flash are slow to respond */
Tor Krill90447ec2008-03-28 11:29:10 +01001688
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001689 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1690 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1691 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1692 bankId += 0x100;
1693 manuId = flash_read_uchar (info,
1694 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1695 }
1696 info->manufacturer_id = manuId;
Tor Krill90447ec2008-03-28 11:29:10 +01001697
1698 switch (info->chipwidth){
1699 case FLASH_CFI_8BIT:
1700 info->device_id = flash_read_uchar (info,
1701 FLASH_OFFSET_DEVICE_ID);
1702 if (info->device_id == 0x7E) {
1703 /* AMD 3-byte (expanded) device ids */
1704 info->device_id2 = flash_read_uchar (info,
1705 FLASH_OFFSET_DEVICE_ID2);
1706 info->device_id2 <<= 8;
1707 info->device_id2 |= flash_read_uchar (info,
1708 FLASH_OFFSET_DEVICE_ID3);
1709 }
1710 break;
1711 case FLASH_CFI_16BIT:
1712 info->device_id = flash_read_word (info,
1713 FLASH_OFFSET_DEVICE_ID);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001714 if ((info->device_id & 0xff) == 0x7E) {
1715 /* AMD 3-byte (expanded) device ids */
1716 info->device_id2 = flash_read_uchar (info,
1717 FLASH_OFFSET_DEVICE_ID2);
1718 info->device_id2 <<= 8;
1719 info->device_id2 |= flash_read_uchar (info,
1720 FLASH_OFFSET_DEVICE_ID3);
1721 }
Tor Krill90447ec2008-03-28 11:29:10 +01001722 break;
1723 default:
1724 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001725 }
1726 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001727 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001728}
1729
1730static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1731{
1732 info->cmd_reset = AMD_CMD_RESET;
1733
1734 cmdset_amd_read_jedec_ids(info);
1735 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1736
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001737#ifdef CONFIG_SYS_FLASH_PROTECTION
1738 if (info->ext_addr && info->manufacturer_id == (uchar)AMD_MANUFACT) {
1739 ushort spus;
1740
1741 /* read sector protect/unprotect scheme */
1742 spus = flash_read_uchar(info, info->ext_addr + 9);
1743 if (spus == 0x8)
1744 info->legacy_unlock = 1;
1745 }
1746#endif
1747
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001748 return 0;
1749}
1750
1751#ifdef CONFIG_FLASH_CFI_LEGACY
Stefan Roese260421a2006-11-13 13:55:24 +01001752static void flash_read_jedec_ids (flash_info_t * info)
1753{
1754 info->manufacturer_id = 0;
1755 info->device_id = 0;
1756 info->device_id2 = 0;
1757
1758 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001759 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese260421a2006-11-13 13:55:24 +01001760 case CFI_CMDSET_INTEL_STANDARD:
1761 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001762 cmdset_intel_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001763 break;
1764 case CFI_CMDSET_AMD_STANDARD:
1765 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001766 cmdset_amd_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001767 break;
1768 default:
1769 break;
1770 }
1771}
1772
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001773/*-----------------------------------------------------------------------
1774 * Call board code to request info about non-CFI flash.
1775 * board_flash_get_legacy needs to fill in at least:
1776 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1777 */
Becky Bruce09ce9922009-02-02 16:34:51 -06001778static int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001779{
1780 flash_info_t *info = &flash_info[banknum];
1781
1782 if (board_flash_get_legacy(base, banknum, info)) {
1783 /* board code may have filled info completely. If not, we
1784 use JEDEC ID probing. */
1785 if (!info->vendor) {
1786 int modes[] = {
1787 CFI_CMDSET_AMD_STANDARD,
1788 CFI_CMDSET_INTEL_STANDARD
1789 };
1790 int i;
1791
1792 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1793 info->vendor = modes[i];
Becky Bruce09ce9922009-02-02 16:34:51 -06001794 info->start[0] =
1795 (ulong)map_physmem(base,
Stefan Roesee1fb6d02009-02-05 11:44:52 +01001796 info->portwidth,
Becky Bruce09ce9922009-02-02 16:34:51 -06001797 MAP_NOCACHE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001798 if (info->portwidth == FLASH_CFI_8BIT
1799 && info->interface == FLASH_CFI_X8X16) {
1800 info->addr_unlock1 = 0x2AAA;
1801 info->addr_unlock2 = 0x5555;
1802 } else {
1803 info->addr_unlock1 = 0x5555;
1804 info->addr_unlock2 = 0x2AAA;
1805 }
1806 flash_read_jedec_ids(info);
1807 debug("JEDEC PROBE: ID %x %x %x\n",
1808 info->manufacturer_id,
1809 info->device_id,
1810 info->device_id2);
Becky Bruce09ce9922009-02-02 16:34:51 -06001811 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001812 break;
Becky Bruce09ce9922009-02-02 16:34:51 -06001813 else
Stefan Roesee1fb6d02009-02-05 11:44:52 +01001814 unmap_physmem((void *)info->start[0],
Becky Bruce09ce9922009-02-02 16:34:51 -06001815 MAP_NOCACHE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001816 }
1817 }
1818
1819 switch(info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001820 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001821 case CFI_CMDSET_INTEL_STANDARD:
1822 case CFI_CMDSET_INTEL_EXTENDED:
1823 info->cmd_reset = FLASH_CMD_RESET;
1824 break;
1825 case CFI_CMDSET_AMD_STANDARD:
1826 case CFI_CMDSET_AMD_EXTENDED:
1827 case CFI_CMDSET_AMD_LEGACY:
1828 info->cmd_reset = AMD_CMD_RESET;
1829 break;
1830 }
1831 info->flash_id = FLASH_MAN_CFI;
1832 return 1;
1833 }
1834 return 0; /* use CFI */
1835}
1836#else
Becky Bruce09ce9922009-02-02 16:34:51 -06001837static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001838{
1839 return 0; /* use CFI */
1840}
1841#endif
1842
Stefan Roese260421a2006-11-13 13:55:24 +01001843/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001844 * detect if flash is compatible with the Common Flash Interface (CFI)
1845 * http://www.jedec.org/download/search/jesd68.pdf
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001846 */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001847static void flash_read_cfi (flash_info_t *info, void *buf,
1848 unsigned int start, size_t len)
1849{
1850 u8 *p = buf;
1851 unsigned int i;
1852
1853 for (i = 0; i < len; i++)
1854 p[i] = flash_read_uchar(info, start + i);
1855}
1856
Stefan Roesefa36ae72009-10-27 15:15:55 +01001857void __flash_cmd_reset(flash_info_t *info)
1858{
1859 /*
1860 * We do not yet know what kind of commandset to use, so we issue
1861 * the reset command in both Intel and AMD variants, in the hope
1862 * that AMD flash roms ignore the Intel command.
1863 */
1864 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001865 udelay(1);
Stefan Roesefa36ae72009-10-27 15:15:55 +01001866 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1867}
1868void flash_cmd_reset(flash_info_t *info)
1869 __attribute__((weak,alias("__flash_cmd_reset")));
1870
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001871static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
wdenk5653fc32004-02-08 22:55:38 +00001872{
Wolfgang Denk92eb7292006-12-27 01:26:13 +01001873 int cfi_offset;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001874
Stefan Roesefa36ae72009-10-27 15:15:55 +01001875 /* Issue FLASH reset command */
1876 flash_cmd_reset(info);
Michael Schwingen1ba639d2008-02-18 23:16:35 +01001877
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001878 for (cfi_offset=0;
1879 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1880 cfi_offset++) {
1881 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1882 FLASH_CMD_CFI);
1883 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1884 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1885 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001886 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1887 sizeof(struct cfi_qry));
1888 info->interface = le16_to_cpu(qry->interface_desc);
1889
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001890 info->cfi_offset = flash_offset_cfi[cfi_offset];
1891 debug ("device interface is %d\n",
1892 info->interface);
1893 debug ("found port %d chip %d ",
1894 info->portwidth, info->chipwidth);
1895 debug ("port %d bits chip %d bits\n",
1896 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1897 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1898
1899 /* calculate command offsets as in the Linux driver */
1900 info->addr_unlock1 = 0x555;
1901 info->addr_unlock2 = 0x2aa;
1902
1903 /*
1904 * modify the unlock address if we are
1905 * in compatibility mode
1906 */
1907 if ( /* x8/x16 in x8 mode */
1908 ((info->chipwidth == FLASH_CFI_BY8) &&
1909 (info->interface == FLASH_CFI_X8X16)) ||
1910 /* x16/x32 in x16 mode */
1911 ((info->chipwidth == FLASH_CFI_BY16) &&
1912 (info->interface == FLASH_CFI_X16X32)))
1913 {
1914 info->addr_unlock1 = 0xaaa;
1915 info->addr_unlock2 = 0x555;
1916 }
1917
1918 info->name = "CFI conformant";
1919 return 1;
1920 }
1921 }
1922
1923 return 0;
1924}
1925
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001926static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001927{
wdenkbf9e3b32004-02-12 00:47:09 +00001928 debug ("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +00001929
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001930 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenkbf9e3b32004-02-12 00:47:09 +00001931 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1932 for (info->chipwidth = FLASH_CFI_BY8;
1933 info->chipwidth <= info->portwidth;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001934 info->chipwidth <<= 1)
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001935 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001936 return 1;
wdenk5653fc32004-02-08 22:55:38 +00001937 }
wdenkbf9e3b32004-02-12 00:47:09 +00001938 debug ("not found\n");
wdenk5653fc32004-02-08 22:55:38 +00001939 return 0;
1940}
wdenkbf9e3b32004-02-12 00:47:09 +00001941
wdenk5653fc32004-02-08 22:55:38 +00001942/*
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001943 * Manufacturer-specific quirks. Add workarounds for geometry
1944 * reversal, etc. here.
1945 */
1946static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1947{
1948 /* check if flash geometry needs reversal */
1949 if (qry->num_erase_regions > 1) {
1950 /* reverse geometry if top boot part */
1951 if (info->cfi_version < 0x3131) {
1952 /* CFI < 1.1, try to guess from device id */
1953 if ((info->device_id & 0x80) != 0)
1954 cfi_reverse_geometry(qry);
1955 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1956 /* CFI >= 1.1, deduct from top/bottom flag */
1957 /* note: ext_addr is valid since cfi_version > 0 */
1958 cfi_reverse_geometry(qry);
1959 }
1960 }
1961}
1962
1963static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1964{
1965 int reverse_geometry = 0;
1966
1967 /* Check the "top boot" bit in the PRI */
1968 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1969 reverse_geometry = 1;
1970
1971 /* AT49BV6416(T) list the erase regions in the wrong order.
1972 * However, the device ID is identical with the non-broken
Ulf Samuelssoncb82a532009-03-27 23:26:43 +01001973 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001974 */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001975 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1976 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001977
1978 if (reverse_geometry)
1979 cfi_reverse_geometry(qry);
1980}
1981
Richard Retanubune8eac432009-01-14 08:44:26 -05001982static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1983{
1984 /* check if flash geometry needs reversal */
1985 if (qry->num_erase_regions > 1) {
1986 /* reverse geometry if top boot part */
1987 if (info->cfi_version < 0x3131) {
Mike Frysinger6a011ce2011-04-10 16:06:29 -04001988 /* CFI < 1.1, guess by device id */
1989 if (info->device_id == 0x22CA || /* M29W320DT */
1990 info->device_id == 0x2256 || /* M29W320ET */
1991 info->device_id == 0x22D7) { /* M29W800DT */
Richard Retanubune8eac432009-01-14 08:44:26 -05001992 cfi_reverse_geometry(qry);
1993 }
Mike Frysinger4c2105c2011-05-09 18:33:36 -04001994 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1995 /* CFI >= 1.1, deduct from top/bottom flag */
1996 /* note: ext_addr is valid since cfi_version > 0 */
1997 cfi_reverse_geometry(qry);
Richard Retanubune8eac432009-01-14 08:44:26 -05001998 }
1999 }
2000}
2001
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002002/*
wdenk5653fc32004-02-08 22:55:38 +00002003 * The following code cannot be run from FLASH!
2004 *
2005 */
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002006ulong flash_get_size (phys_addr_t base, int banknum)
wdenk5653fc32004-02-08 22:55:38 +00002007{
wdenkbf9e3b32004-02-12 00:47:09 +00002008 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +00002009 int i, j;
2010 flash_sect_t sect_cnt;
Becky Bruce09ce9922009-02-02 16:34:51 -06002011 phys_addr_t sector;
wdenk5653fc32004-02-08 22:55:38 +00002012 unsigned long tmp;
2013 int size_ratio;
2014 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +00002015 int erase_region_size;
2016 int erase_region_count;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002017 struct cfi_qry qry;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002018 unsigned long max_size;
Stefan Roese260421a2006-11-13 13:55:24 +01002019
Kumar Galaf9796902008-05-15 15:13:08 -05002020 memset(&qry, 0, sizeof(qry));
2021
Stefan Roese260421a2006-11-13 13:55:24 +01002022 info->ext_addr = 0;
2023 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002024#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +02002025 info->legacy_unlock = 0;
2026#endif
wdenk5653fc32004-02-08 22:55:38 +00002027
Becky Bruce09ce9922009-02-02 16:34:51 -06002028 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002029
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002030 if (flash_detect_cfi (info, &qry)) {
2031 info->vendor = le16_to_cpu(qry.p_id);
2032 info->ext_addr = le16_to_cpu(qry.p_adr);
2033 num_erase_regions = qry.num_erase_regions;
2034
Stefan Roese260421a2006-11-13 13:55:24 +01002035 if (info->ext_addr) {
2036 info->cfi_version = (ushort) flash_read_uchar (info,
2037 info->ext_addr + 3) << 8;
2038 info->cfi_version |= (ushort) flash_read_uchar (info,
2039 info->ext_addr + 4);
2040 }
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002041
wdenkbf9e3b32004-02-12 00:47:09 +00002042#ifdef DEBUG
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002043 flash_printqry (&qry);
wdenkbf9e3b32004-02-12 00:47:09 +00002044#endif
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002045
wdenkbf9e3b32004-02-12 00:47:09 +00002046 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002047 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00002048 case CFI_CMDSET_INTEL_STANDARD:
2049 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002050 cmdset_intel_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002051 break;
2052 case CFI_CMDSET_AMD_STANDARD:
2053 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002054 cmdset_amd_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002055 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002056 default:
2057 printf("CFI: Unknown command set 0x%x\n",
2058 info->vendor);
2059 /*
2060 * Unfortunately, this means we don't know how
2061 * to get the chip back to Read mode. Might
2062 * as well try an Intel-style reset...
2063 */
2064 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2065 return 0;
wdenk5653fc32004-02-08 22:55:38 +00002066 }
wdenkcd37d9e2004-02-10 00:03:41 +00002067
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002068 /* Do manufacturer-specific fixups */
2069 switch (info->manufacturer_id) {
Mario Schuknecht2c9f48a2011-02-21 13:13:14 +01002070 case 0x0001: /* AMD */
2071 case 0x0037: /* AMIC */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002072 flash_fixup_amd(info, &qry);
2073 break;
2074 case 0x001f:
2075 flash_fixup_atmel(info, &qry);
2076 break;
Richard Retanubune8eac432009-01-14 08:44:26 -05002077 case 0x0020:
2078 flash_fixup_stm(info, &qry);
2079 break;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002080 }
2081
wdenkbf9e3b32004-02-12 00:47:09 +00002082 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese260421a2006-11-13 13:55:24 +01002083 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
2084 debug ("device id is 0x%x\n", info->device_id);
2085 debug ("device id2 is 0x%x\n", info->device_id2);
2086 debug ("cfi version is 0x%04x\n", info->cfi_version);
2087
wdenk5653fc32004-02-08 22:55:38 +00002088 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00002089 /* if the chip is x8/x16 reduce the ratio by half */
2090 if ((info->interface == FLASH_CFI_X8X16)
2091 && (info->chipwidth == FLASH_CFI_BY8)) {
2092 size_ratio >>= 1;
2093 }
wdenkbf9e3b32004-02-12 00:47:09 +00002094 debug ("size_ratio %d port %d bits chip %d bits\n",
2095 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2096 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002097 info->size = 1 << qry.dev_size;
2098 /* multiply the size by the number of chips */
2099 info->size *= size_ratio;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002100 max_size = cfi_flash_bank_size(banknum);
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002101 if (max_size && (info->size > max_size)) {
2102 debug("[truncated from %ldMiB]", info->size >> 20);
2103 info->size = max_size;
2104 }
wdenkbf9e3b32004-02-12 00:47:09 +00002105 debug ("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00002106 sect_cnt = 0;
2107 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00002108 for (i = 0; i < num_erase_regions; i++) {
2109 if (i > NUM_ERASE_REGIONS) {
wdenk028ab6b2004-02-23 23:54:43 +00002110 printf ("%d erase regions found, only %d used\n",
2111 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00002112 break;
2113 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002114
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002115 tmp = le32_to_cpu(qry.erase_region_info[i]);
2116 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002117
2118 erase_region_count = (tmp & 0xffff) + 1;
2119 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00002120 erase_region_size =
2121 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk4c0d4c32004-06-09 17:34:58 +00002122 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenk028ab6b2004-02-23 23:54:43 +00002123 erase_region_count, erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00002124 for (j = 0; j < erase_region_count; j++) {
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002125 if (sector - base >= info->size)
2126 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002127 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002128 printf("ERROR: too many flash sectors\n");
2129 break;
2130 }
Becky Bruce09ce9922009-02-02 16:34:51 -06002131 info->start[sect_cnt] =
2132 (ulong)map_physmem(sector,
2133 info->portwidth,
2134 MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002135 sector += (erase_region_size * size_ratio);
wdenka1191902005-01-09 17:12:27 +00002136
2137 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002138 * Only read protection status from
2139 * supported devices (intel...)
wdenka1191902005-01-09 17:12:27 +00002140 */
2141 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002142 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenka1191902005-01-09 17:12:27 +00002143 case CFI_CMDSET_INTEL_EXTENDED:
2144 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesedf4e8132010-10-25 18:31:29 +02002145 /*
2146 * Set flash to read-id mode. Otherwise
2147 * reading protected status is not
2148 * guaranteed.
2149 */
2150 flash_write_cmd(info, sect_cnt, 0,
2151 FLASH_CMD_READ_ID);
wdenka1191902005-01-09 17:12:27 +00002152 info->protect[sect_cnt] =
2153 flash_isset (info, sect_cnt,
2154 FLASH_OFFSET_PROTECT,
2155 FLASH_STATUS_PROTECT);
2156 break;
2157 default:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002158 /* default: not protected */
2159 info->protect[sect_cnt] = 0;
wdenka1191902005-01-09 17:12:27 +00002160 }
2161
wdenk5653fc32004-02-08 22:55:38 +00002162 sect_cnt++;
2163 }
2164 }
2165
2166 info->sector_count = sect_cnt;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002167 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2168 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002169 info->erase_blk_tout = tmp *
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002170 (1 << qry.block_erase_timeout_max);
2171 tmp = (1 << qry.buf_write_timeout_typ) *
2172 (1 << qry.buf_write_timeout_max);
2173
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002174 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002175 info->buffer_write_tout = (tmp + 999) / 1000;
2176 tmp = (1 << qry.word_write_timeout_typ) *
2177 (1 << qry.word_write_timeout_max);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002178 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002179 info->write_tout = (tmp + 999) / 1000;
wdenk5653fc32004-02-08 22:55:38 +00002180 info->flash_id = FLASH_MAN_CFI;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002181 if ((info->interface == FLASH_CFI_X8X16) &&
2182 (info->chipwidth == FLASH_CFI_BY8)) {
2183 /* XXX - Need to test on x8/x16 in parallel. */
2184 info->portwidth >>= 1;
wdenk855a4962004-03-14 18:23:55 +00002185 }
Mike Frysinger22159872008-10-02 01:55:38 -04002186
2187 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +00002188 }
2189
wdenkbf9e3b32004-02-12 00:47:09 +00002190 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00002191}
2192
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002193#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002194void flash_set_verbose(uint v)
2195{
2196 flash_verbose = v;
2197}
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002198#endif
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002199
Stefan Roese6f726f92010-10-25 18:31:48 +02002200static void cfi_flash_set_config_reg(u32 base, u16 val)
2201{
2202#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2203 /*
2204 * Only set this config register if really defined
2205 * to a valid value (0xffff is invalid)
2206 */
2207 if (val == 0xffff)
2208 return;
2209
2210 /*
2211 * Set configuration register. Data is "encrypted" in the 16 lower
2212 * address bits.
2213 */
2214 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2215 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2216
2217 /*
2218 * Finally issue reset-command to bring device back to
2219 * read-array mode
2220 */
2221 flash_write16(FLASH_CMD_RESET, (void *)base);
2222#endif
2223}
2224
wdenk5653fc32004-02-08 22:55:38 +00002225/*-----------------------------------------------------------------------
2226 */
Heiko Schocher6ee14162011-04-04 08:10:21 +02002227
2228void flash_protect_default(void)
2229{
Peter Tyser2c519832011-04-13 11:46:56 -05002230#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2231 int i;
2232 struct apl_s {
2233 ulong start;
2234 ulong size;
2235 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2236#endif
2237
Heiko Schocher6ee14162011-04-04 08:10:21 +02002238 /* Monitor protection ON by default */
2239#if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2240 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2241 flash_protect(FLAG_PROTECT_SET,
2242 CONFIG_SYS_MONITOR_BASE,
2243 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2244 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2245#endif
2246
2247 /* Environment protection ON by default */
2248#ifdef CONFIG_ENV_IS_IN_FLASH
2249 flash_protect(FLAG_PROTECT_SET,
2250 CONFIG_ENV_ADDR,
2251 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2252 flash_get_info(CONFIG_ENV_ADDR));
2253#endif
2254
2255 /* Redundant environment protection ON by default */
2256#ifdef CONFIG_ENV_ADDR_REDUND
2257 flash_protect(FLAG_PROTECT_SET,
2258 CONFIG_ENV_ADDR_REDUND,
2259 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2260 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2261#endif
2262
2263#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2264 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
Marek Vasut31d34142011-10-21 14:17:05 +00002265 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocher6ee14162011-04-04 08:10:21 +02002266 apl[i].start, apl[i].start + apl[i].size - 1);
2267 flash_protect(FLAG_PROTECT_SET,
2268 apl[i].start,
2269 apl[i].start + apl[i].size - 1,
2270 flash_get_info(apl[i].start));
2271 }
2272#endif
2273}
2274
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002275unsigned long flash_init (void)
wdenk5653fc32004-02-08 22:55:38 +00002276{
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002277 unsigned long size = 0;
2278 int i;
wdenk5653fc32004-02-08 22:55:38 +00002279
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002280#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann3a3baf32009-03-21 09:59:34 -04002281 /* read environment from EEPROM */
2282 char s[64];
Wolfgang Denkcdb74972010-07-24 21:55:43 +02002283 getenv_f("unlock", s, sizeof(s));
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002284#endif
wdenk5653fc32004-02-08 22:55:38 +00002285
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002286 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002287 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002288 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk5653fc32004-02-08 22:55:38 +00002289
Stefan Roese6f726f92010-10-25 18:31:48 +02002290 /* Optionally write flash configuration register */
2291 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2292 cfi_flash_config_reg(i));
2293
Stefan Roeseb00e19c2010-08-30 10:11:51 +02002294 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002295 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002296 size += flash_info[i].size;
2297 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002298#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Peter Tysereddf52b2010-12-28 18:12:05 -06002299 printf ("## Unknown flash on Bank %d "
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002300 "- Size = 0x%08lx = %ld MB\n",
2301 i+1, flash_info[i].size,
John Schmoller0e3fa012010-09-29 13:49:05 -05002302 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002303#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
wdenk5653fc32004-02-08 22:55:38 +00002304 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002305#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002306 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
2307 /*
2308 * Only the U-Boot image and it's environment
2309 * is protected, all other sectors are
2310 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002311 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002312 * and the environment variable "unlock" is
2313 * set to "yes".
2314 */
2315 if (flash_info[i].legacy_unlock) {
2316 int k;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002317
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002318 /*
2319 * Disable legacy_unlock temporarily,
2320 * since flash_real_protect would
2321 * relock all other sectors again
2322 * otherwise.
2323 */
2324 flash_info[i].legacy_unlock = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002325
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002326 /*
2327 * Legacy unlocking (e.g. Intel J3) ->
2328 * unlock only one sector. This will
2329 * unlock all sectors.
2330 */
2331 flash_real_protect (&flash_info[i], 0, 0);
Stefan Roese79b4cda2006-02-28 15:29:58 +01002332
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002333 flash_info[i].legacy_unlock = 1;
2334
2335 /*
2336 * Manually mark other sectors as
2337 * unlocked (unprotected)
2338 */
2339 for (k = 1; k < flash_info[i].sector_count; k++)
2340 flash_info[i].protect[k] = 0;
2341 } else {
2342 /*
2343 * No legancy unlocking -> unlock all sectors
2344 */
2345 flash_protect (FLAG_PROTECT_CLEAR,
2346 flash_info[i].start[0],
2347 flash_info[i].start[0]
2348 + flash_info[i].size - 1,
2349 &flash_info[i]);
2350 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01002351 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002352#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00002353 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002354
Heiko Schocher6ee14162011-04-04 08:10:21 +02002355 flash_protect_default();
Piotr Ziecik91809ed2008-11-17 15:57:58 +01002356#ifdef CONFIG_FLASH_CFI_MTD
2357 cfi_mtd_init();
2358#endif
2359
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002360 return (size);
wdenk5653fc32004-02-08 22:55:38 +00002361}