blob: f6759a80ea57ad57a8d9c725e0a599326c59d62b [file] [log] [blame]
wdenk2cefd152004-02-08 22:55:38 +00001/*
wdenke65527f2004-02-12 00:47:09 +00002 * (C) Copyright 2002-2004
wdenk2cefd152004-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>
wdenk2cefd152004-02-08 22:55:38 +00007 *
wdenke65527f2004-02-12 00:47:09 +00008 * Copyright (C) 2004
9 * Ed Okerson
Stefan Roese12797482006-11-13 13:55:24 +010010 *
11 * Copyright (C) 2006
12 * Tolunay Orkun <listmember@orkun.us>
wdenke65527f2004-02-12 00:47:09 +000013 *
wdenk2cefd152004-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 *
wdenk2cefd152004-02-08 22:55:38 +000032 */
33
34/* The DEBUG define must be before common to enable debugging */
wdenk2ebee312004-02-23 19:30:57 +000035/* #define DEBUG */
36
wdenk2cefd152004-02-08 22:55:38 +000037#include <common.h>
38#include <asm/processor.h>
Haiying Wangc123a382007-02-21 16:52:31 +010039#include <asm/io.h>
wdenkaeba06f2004-06-09 17:34:58 +000040#include <asm/byteorder.h>
Andrew Gabbasovc1592582013-05-14 12:27:52 -050041#include <asm/unaligned.h>
wdenkd0245fc2005-04-13 10:02:42 +000042#include <environment.h>
Stefan Roese6e83e342009-10-27 15:15:55 +010043#include <mtd/cfi_flash.h>
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +010044#include <watchdog.h>
wdenke537b3b2004-02-23 23:54:43 +000045
wdenk2cefd152004-02-08 22:55:38 +000046/*
Haavard Skinnemoend523e392007-12-13 12:56:28 +010047 * This file implements a Common Flash Interface (CFI) driver for
48 * U-Boot.
49 *
50 * The width of the port and the width of the chips are determined at
51 * initialization. These widths are used to calculate the address for
52 * access CFI data structures.
wdenk2cefd152004-02-08 22:55:38 +000053 *
54 * References
55 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
56 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
57 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
58 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese12797482006-11-13 13:55:24 +010059 * AMD CFI Specification, Release 2.0 December 1, 2001
60 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
61 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk2cefd152004-02-08 22:55:38 +000062 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020063 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocher800db312007-01-19 18:05:26 +010064 * reading and writing ... (yes there is such a Hardware).
wdenk2cefd152004-02-08 22:55:38 +000065 */
66
Haavard Skinnemoend523e392007-12-13 12:56:28 +010067static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysingerc2c093d2010-12-22 09:41:13 -050068#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +010069static uint flash_verbose = 1;
Mike Frysingerc2c093d2010-12-22 09:41:13 -050070#else
71#define flash_verbose 1
72#endif
Wolfgang Denkafa0dd02006-12-27 01:26:13 +010073
Wolfgang Denk9f5fb0f2008-08-08 16:39:54 +020074flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
75
Stefan Roesec865e6c2006-02-28 15:29:58 +010076/*
77 * Check if chip width is defined. If not, start detecting with 8bit.
78 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020079#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
80#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roesec865e6c2006-02-28 15:29:58 +010081#endif
82
Stefan Roeseab935642010-10-25 18:31:48 +020083/*
84 * 0xffff is an undefined value for the configuration register. When
85 * this value is returned, the configuration register shall not be
86 * written at all (default mode).
87 */
88static u16 cfi_flash_config_reg(int i)
89{
90#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
91 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
92#else
93 return 0xffff;
94#endif
95}
96
Stefan Roesefb9a7302010-08-31 10:00:10 +020097#if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
98int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
99#endif
100
Stefan Roese7e7dda82010-08-30 10:11:51 +0200101static phys_addr_t __cfi_flash_bank_addr(int i)
102{
103 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
104}
105phys_addr_t cfi_flash_bank_addr(int i)
106 __attribute__((weak, alias("__cfi_flash_bank_addr")));
107
Ilya Yanok755c1802010-10-21 17:20:12 +0200108static unsigned long __cfi_flash_bank_size(int i)
109{
110#ifdef CONFIG_SYS_FLASH_BANKS_SIZES
111 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
112#else
113 return 0;
114#endif
115}
116unsigned long cfi_flash_bank_size(int i)
117 __attribute__((weak, alias("__cfi_flash_bank_size")));
118
Stefan Roese38ae9822008-11-17 14:45:22 +0100119static void __flash_write8(u8 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100120{
121 __raw_writeb(value, addr);
122}
123
Stefan Roese38ae9822008-11-17 14:45:22 +0100124static void __flash_write16(u16 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100125{
126 __raw_writew(value, addr);
127}
128
Stefan Roese38ae9822008-11-17 14:45:22 +0100129static void __flash_write32(u32 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100130{
131 __raw_writel(value, addr);
132}
133
Stefan Roese38ae9822008-11-17 14:45:22 +0100134static void __flash_write64(u64 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100135{
136 /* No architectures currently implement __raw_writeq() */
137 *(volatile u64 *)addr = value;
138}
139
Stefan Roese38ae9822008-11-17 14:45:22 +0100140static u8 __flash_read8(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100141{
142 return __raw_readb(addr);
143}
144
Stefan Roese38ae9822008-11-17 14:45:22 +0100145static u16 __flash_read16(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100146{
147 return __raw_readw(addr);
148}
149
Stefan Roese38ae9822008-11-17 14:45:22 +0100150static u32 __flash_read32(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100151{
152 return __raw_readl(addr);
153}
154
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100155static u64 __flash_read64(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100156{
157 /* No architectures currently implement __raw_readq() */
158 return *(volatile u64 *)addr;
159}
160
Stefan Roese38ae9822008-11-17 14:45:22 +0100161#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
162void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
163void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
164void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
165void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
166u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
167u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
168u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100169u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
Stefan Roese38ae9822008-11-17 14:45:22 +0100170#else
171#define flash_write8 __flash_write8
172#define flash_write16 __flash_write16
173#define flash_write32 __flash_write32
174#define flash_write64 __flash_write64
175#define flash_read8 __flash_read8
176#define flash_read16 __flash_read16
177#define flash_read32 __flash_read32
178#define flash_read64 __flash_read64
179#endif
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100180
wdenk2cefd152004-02-08 22:55:38 +0000181/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000182 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200183#if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
Heiko Schocher4c0f0052009-02-10 09:53:29 +0100184flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200185{
186 int i;
Stefan Roese6c7c2dc2010-08-30 11:14:38 +0200187 flash_info_t *info = NULL;
wdenk2cefd152004-02-08 22:55:38 +0000188
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200189 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200190 info = & flash_info[i];
191 if (info->size && info->start[0] <= base &&
192 base <= info->start[0] + info->size - 1)
193 break;
194 }
wdenk2cefd152004-02-08 22:55:38 +0000195
Stefan Roese6c7c2dc2010-08-30 11:14:38 +0200196 return info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200197}
wdenk2cefd152004-02-08 22:55:38 +0000198#endif
199
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100200unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
201{
202 if (sect != (info->sector_count - 1))
203 return info->start[sect + 1] - info->start[sect];
204 else
205 return info->start[0] + info->size - info->start[sect];
206}
207
wdenke65527f2004-02-12 00:47:09 +0000208/*-----------------------------------------------------------------------
209 * create an address based on the offset and the port width
210 */
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100211static inline void *
212flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000213{
Stefan Roese70a90b72013-04-12 19:04:54 +0200214 unsigned int byte_offset = offset * info->portwidth;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100215
Stefan Roese70a90b72013-04-12 19:04:54 +0200216 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100217}
218
219static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
220 unsigned int offset, void *addr)
221{
wdenke65527f2004-02-12 00:47:09 +0000222}
223
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200224/*-----------------------------------------------------------------------
225 * make a proper sized command based on the port and chip widths
226 */
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200227static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200228{
229 int i;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400230 int cword_offset;
231 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200232#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200233 u32 cmd_le = cpu_to_le32(cmd);
234#endif
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400235 uchar val;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200236 uchar *cp = (uchar *) cmdbuf;
237
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400238 for (i = info->portwidth; i > 0; i--){
239 cword_offset = (info->portwidth-i)%info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200240#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400241 cp_offset = info->portwidth - i;
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200242 val = *((uchar*)&cmd_le + cword_offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200243#else
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400244 cp_offset = i - 1;
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200245 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200246#endif
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200247 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400248 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200249}
250
wdenk2cefd152004-02-08 22:55:38 +0000251#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000252/*-----------------------------------------------------------------------
253 * Debug support
254 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100255static void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000256{
257 int i;
258 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000259
Wolfgang Denk49f4f6a2009-02-04 09:42:20 +0100260 cp = (char *) &data;
wdenke65527f2004-02-12 00:47:09 +0000261 for (i = 0; i < 8; i++)
262 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000263}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200264
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100265static void flash_printqry (struct cfi_qry *qry)
wdenke65527f2004-02-12 00:47:09 +0000266{
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100267 u8 *p = (u8 *)qry;
wdenke65527f2004-02-12 00:47:09 +0000268 int x, y;
269
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100270 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
271 debug("%02x : ", x);
272 for (y = 0; y < 16; y++)
273 debug("%2.2x ", p[x + y]);
274 debug(" ");
wdenke65527f2004-02-12 00:47:09 +0000275 for (y = 0; y < 16; y++) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100276 unsigned char c = p[x + y];
277 if (c >= 0x20 && c <= 0x7e)
278 debug("%c", c);
279 else
280 debug(".");
wdenke65527f2004-02-12 00:47:09 +0000281 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100282 debug("\n");
wdenke65527f2004-02-12 00:47:09 +0000283 }
284}
wdenk2cefd152004-02-08 22:55:38 +0000285#endif
286
287
288/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000289 * read a character at a port width address
290 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100291static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000292{
293 uchar *cp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100294 uchar retval;
wdenke65527f2004-02-12 00:47:09 +0000295
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100296 cp = flash_map (info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200297#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100298 retval = flash_read8(cp);
wdenke65527f2004-02-12 00:47:09 +0000299#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100300 retval = flash_read8(cp + info->portwidth - 1);
wdenke65527f2004-02-12 00:47:09 +0000301#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100302 flash_unmap (info, 0, offset, cp);
303 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000304}
305
306/*-----------------------------------------------------------------------
Tor Krill7f2a3052008-03-28 11:29:10 +0100307 * read a word at a port width address, assume 16bit bus
308 */
309static inline ushort flash_read_word (flash_info_t * info, uint offset)
310{
311 ushort *addr, retval;
312
313 addr = flash_map (info, 0, offset);
314 retval = flash_read16 (addr);
315 flash_unmap (info, 0, offset, addr);
316 return retval;
317}
318
319
320/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +0100321 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000322 * port size word. Swap for ppc format.
323 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100324static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
325 uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000326{
wdenke65527f2004-02-12 00:47:09 +0000327 uchar *addr;
328 ulong retval;
329
330#ifdef DEBUG
331 int x;
332#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100333 addr = flash_map (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000334
wdenke65527f2004-02-12 00:47:09 +0000335#ifdef DEBUG
336 debug ("long addr is at %p info->portwidth = %d\n", addr,
337 info->portwidth);
338 for (x = 0; x < 4 * info->portwidth; x++) {
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100339 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenke65527f2004-02-12 00:47:09 +0000340 }
341#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200342#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100343 retval = ((flash_read8(addr) << 16) |
344 (flash_read8(addr + info->portwidth) << 24) |
345 (flash_read8(addr + 2 * info->portwidth)) |
346 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenke65527f2004-02-12 00:47:09 +0000347#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100348 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
349 (flash_read8(addr + info->portwidth - 1) << 16) |
350 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
351 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenke65527f2004-02-12 00:47:09 +0000352#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100353 flash_unmap(info, sect, offset, addr);
354
wdenke65527f2004-02-12 00:47:09 +0000355 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000356}
357
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200358/*
359 * Write a proper sized command to the correct address
Michael Schwingen73d044d2007-12-07 23:35:02 +0100360 */
Stefan Roese6e83e342009-10-27 15:15:55 +0100361void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
362 uint offset, u32 cmd)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100363{
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100364
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100365 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200366 cfiword_t cword;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100367
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100368 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200369 flash_make_cmd (info, cmd, &cword);
370 switch (info->portwidth) {
371 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100372 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200373 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100374 flash_write8(cword.c, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200375 break;
376 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100377 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200378 cmd, cword.w,
379 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100380 flash_write16(cword.w, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200381 break;
382 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100383 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200384 cmd, cword.l,
385 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100386 flash_write32(cword.l, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200387 break;
388 case FLASH_CFI_64BIT:
389#ifdef DEBUG
390 {
391 char str[20];
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100392
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200393 print_longlong (str, cword.ll);
394
395 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100396 addr, cmd, str,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200397 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100398 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200399#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100400 flash_write64(cword.ll, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200401 break;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100402 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200403
404 /* Ensure all the instructions are fully finished */
405 sync();
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100406
407 flash_unmap(info, sect, offset, addr);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100408}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200409
410static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100411{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200412 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
413 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100414}
Michael Schwingen73d044d2007-12-07 23:35:02 +0100415
416/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000417 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200418static int flash_isequal (flash_info_t * info, flash_sect_t sect,
419 uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000420{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100421 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200422 cfiword_t cword;
423 int retval;
wdenk2cefd152004-02-08 22:55:38 +0000424
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100425 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200426 flash_make_cmd (info, cmd, &cword);
Stefan Roeseefef95b2006-04-01 13:41:03 +0200427
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100428 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200429 switch (info->portwidth) {
430 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100431 debug ("is= %x %x\n", flash_read8(addr), cword.c);
432 retval = (flash_read8(addr) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200433 break;
434 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100435 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
436 retval = (flash_read16(addr) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200437 break;
438 case FLASH_CFI_32BIT:
Andrew Klossner7ddfafc2008-08-21 07:12:26 -0700439 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100440 retval = (flash_read32(addr) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200441 break;
442 case FLASH_CFI_64BIT:
443#ifdef DEBUG
444 {
445 char str1[20];
446 char str2[20];
Michael Schwingen73d044d2007-12-07 23:35:02 +0100447
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100448 print_longlong (str1, flash_read64(addr));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200449 print_longlong (str2, cword.ll);
450 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +0000451 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200452#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100453 retval = (flash_read64(addr) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200454 break;
455 default:
456 retval = 0;
457 break;
458 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100459 flash_unmap(info, sect, offset, addr);
460
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200461 return retval;
462}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200463
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200464/*-----------------------------------------------------------------------
465 */
466static int flash_isset (flash_info_t * info, flash_sect_t sect,
467 uint offset, uchar cmd)
468{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100469 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200470 cfiword_t cword;
471 int retval;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200472
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100473 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200474 flash_make_cmd (info, cmd, &cword);
475 switch (info->portwidth) {
476 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100477 retval = ((flash_read8(addr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200478 break;
479 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100480 retval = ((flash_read16(addr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200481 break;
482 case FLASH_CFI_32BIT:
Stefan Roesed4e37c02008-01-02 14:05:37 +0100483 retval = ((flash_read32(addr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200484 break;
485 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100486 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200487 break;
488 default:
489 retval = 0;
490 break;
491 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100492 flash_unmap(info, sect, offset, addr);
493
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200494 return retval;
495}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200496
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200497/*-----------------------------------------------------------------------
498 */
499static int flash_toggle (flash_info_t * info, flash_sect_t sect,
500 uint offset, uchar cmd)
501{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100502 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200503 cfiword_t cword;
504 int retval;
wdenke85b7a52004-10-10 22:16:06 +0000505
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100506 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200507 flash_make_cmd (info, cmd, &cword);
508 switch (info->portwidth) {
509 case FLASH_CFI_8BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200510 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200511 break;
512 case FLASH_CFI_16BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200513 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200514 break;
515 case FLASH_CFI_32BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200516 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200517 break;
518 case FLASH_CFI_64BIT:
Wolfgang Denk600e1832008-10-31 01:12:28 +0100519 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
520 (flash_read32(addr+4) != flash_read32(addr+4)) );
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200521 break;
522 default:
523 retval = 0;
524 break;
525 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100526 flash_unmap(info, sect, offset, addr);
527
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200528 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000529}
530
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200531/*
532 * flash_is_busy - check to see if the flash is busy
533 *
534 * This routine checks the status of the chip and returns true if the
535 * chip is busy.
wdenk2cefd152004-02-08 22:55:38 +0000536 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200537static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5c71a7a2005-05-16 15:23:22 +0000538{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200539 int retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000540
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200541 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400542 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200543 case CFI_CMDSET_INTEL_STANDARD:
544 case CFI_CMDSET_INTEL_EXTENDED:
545 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
546 break;
547 case CFI_CMDSET_AMD_STANDARD:
548 case CFI_CMDSET_AMD_EXTENDED:
549#ifdef CONFIG_FLASH_CFI_LEGACY
550 case CFI_CMDSET_AMD_LEGACY:
551#endif
552 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
553 break;
554 default:
555 retval = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000556 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200557 debug ("flash_is_busy: %d\n", retval);
558 return retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000559}
560
561/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200562 * wait for XSR.7 to be set. Time out with an error if it does not.
563 * This routine does not set the flash to read-array mode.
wdenk5c71a7a2005-05-16 15:23:22 +0000564 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200565static int flash_status_check (flash_info_t * info, flash_sect_t sector,
566 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000567{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200568 ulong start;
wdenk2cefd152004-02-08 22:55:38 +0000569
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200570#if CONFIG_SYS_HZ != 1000
Renato Andreolaac6693d2010-03-24 23:00:47 +0800571 if ((ulong)CONFIG_SYS_HZ > 100000)
572 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
573 else
574 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200575#endif
wdenk2cefd152004-02-08 22:55:38 +0000576
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200577 /* Wait for command completion */
Graeme Russ13ec42b2011-07-15 02:18:56 +0000578#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou4b7e6682010-04-01 11:15:05 +0800579 reset_timer();
Graeme Russ13ec42b2011-07-15 02:18:56 +0000580#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200581 start = get_timer (0);
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +0100582 WATCHDOG_RESET();
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200583 while (flash_is_busy (info, sector)) {
584 if (get_timer (start) > tout) {
585 printf ("Flash %s timeout at address %lx data %lx\n",
586 prompt, info->start[sector],
587 flash_read_long (info, sector, 0));
588 flash_write_cmd (info, sector, 0, info->cmd_reset);
Stefan Roese70a90b72013-04-12 19:04:54 +0200589 udelay(1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200590 return ERR_TIMOUT;
wdenk2cefd152004-02-08 22:55:38 +0000591 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200592 udelay (1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000593 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200594 return ERR_OK;
595}
wdenk2cefd152004-02-08 22:55:38 +0000596
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200597/*-----------------------------------------------------------------------
598 * Wait for XSR.7 to be set, if it times out print an error, otherwise
599 * do a full status check.
600 *
601 * This routine sets the flash to read-array mode.
602 */
603static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
604 ulong tout, char *prompt)
605{
606 int retcode;
wdenk2cefd152004-02-08 22:55:38 +0000607
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200608 retcode = flash_status_check (info, sector, tout, prompt);
609 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400610 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200611 case CFI_CMDSET_INTEL_EXTENDED:
612 case CFI_CMDSET_INTEL_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500613 if ((retcode != ERR_OK)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200614 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
615 retcode = ERR_INVAL;
616 printf ("Flash %s error at address %lx\n", prompt,
617 info->start[sector]);
618 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
619 FLASH_STATUS_PSLBS)) {
620 puts ("Command Sequence Error.\n");
621 } else if (flash_isset (info, sector, 0,
622 FLASH_STATUS_ECLBS)) {
623 puts ("Block Erase Error.\n");
624 retcode = ERR_NOT_ERASED;
625 } else if (flash_isset (info, sector, 0,
626 FLASH_STATUS_PSLBS)) {
627 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000628 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200629 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
630 puts ("Block locked.\n");
631 retcode = ERR_PROTECTED;
632 }
633 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
634 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000635 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200636 flash_write_cmd (info, sector, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -0700637 udelay(1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200638 break;
639 default:
640 break;
wdenk2cefd152004-02-08 22:55:38 +0000641 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200642 return retcode;
wdenk2cefd152004-02-08 22:55:38 +0000643}
644
Thomas Chou076767a2010-03-26 08:17:00 +0800645static int use_flash_status_poll(flash_info_t *info)
646{
647#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
648 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
649 info->vendor == CFI_CMDSET_AMD_STANDARD)
650 return 1;
651#endif
652 return 0;
653}
654
655static int flash_status_poll(flash_info_t *info, void *src, void *dst,
656 ulong tout, char *prompt)
657{
658#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
659 ulong start;
660 int ready;
661
662#if CONFIG_SYS_HZ != 1000
663 if ((ulong)CONFIG_SYS_HZ > 100000)
664 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
665 else
666 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
667#endif
668
669 /* Wait for command completion */
Graeme Russ13ec42b2011-07-15 02:18:56 +0000670#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou4b7e6682010-04-01 11:15:05 +0800671 reset_timer();
Graeme Russ13ec42b2011-07-15 02:18:56 +0000672#endif
Thomas Chou076767a2010-03-26 08:17:00 +0800673 start = get_timer(0);
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +0100674 WATCHDOG_RESET();
Thomas Chou076767a2010-03-26 08:17:00 +0800675 while (1) {
676 switch (info->portwidth) {
677 case FLASH_CFI_8BIT:
678 ready = flash_read8(dst) == flash_read8(src);
679 break;
680 case FLASH_CFI_16BIT:
681 ready = flash_read16(dst) == flash_read16(src);
682 break;
683 case FLASH_CFI_32BIT:
684 ready = flash_read32(dst) == flash_read32(src);
685 break;
686 case FLASH_CFI_64BIT:
687 ready = flash_read64(dst) == flash_read64(src);
688 break;
689 default:
690 ready = 0;
691 break;
692 }
693 if (ready)
694 break;
695 if (get_timer(start) > tout) {
696 printf("Flash %s timeout at address %lx data %lx\n",
697 prompt, (ulong)dst, (ulong)flash_read8(dst));
698 return ERR_TIMOUT;
699 }
700 udelay(1); /* also triggers watchdog */
701 }
702#endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
703 return ERR_OK;
704}
705
wdenk2cefd152004-02-08 22:55:38 +0000706/*-----------------------------------------------------------------------
707 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200708static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000709{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200710#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200711 unsigned short w;
712 unsigned int l;
713 unsigned long long ll;
714#endif
wdenk2cefd152004-02-08 22:55:38 +0000715
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200716 switch (info->portwidth) {
717 case FLASH_CFI_8BIT:
718 cword->c = c;
719 break;
720 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200721#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200722 w = c;
723 w <<= 8;
724 cword->w = (cword->w >> 8) | w;
725#else
726 cword->w = (cword->w << 8) | c;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100727#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200728 break;
729 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200730#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200731 l = c;
732 l <<= 24;
733 cword->l = (cword->l >> 8) | l;
734#else
735 cword->l = (cword->l << 8) | c;
736#endif
737 break;
738 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200739#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200740 ll = c;
741 ll <<= 56;
742 cword->ll = (cword->ll >> 8) | ll;
743#else
744 cword->ll = (cword->ll << 8) | c;
745#endif
746 break;
Stefan Roese12797482006-11-13 13:55:24 +0100747 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200748}
wdenk2cefd152004-02-08 22:55:38 +0000749
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100750/*
751 * Loop through the sector table starting from the previously found sector.
752 * Searches forwards or backwards, dependent on the passed address.
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200753 */
754static flash_sect_t find_sector (flash_info_t * info, ulong addr)
755{
Kim Phillipsd303b862012-10-29 13:34:45 +0000756 static flash_sect_t saved_sector; /* previously found sector */
Stefan Roese70a90b72013-04-12 19:04:54 +0200757 static flash_info_t *saved_info; /* previously used flash bank */
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100758 flash_sect_t sector = saved_sector;
759
Stefan Roese70a90b72013-04-12 19:04:54 +0200760 if ((info != saved_info) || (sector >= info->sector_count))
761 sector = 0;
762
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100763 while ((info->start[sector] < addr)
764 && (sector < info->sector_count - 1))
765 sector++;
766 while ((info->start[sector] > addr) && (sector > 0))
767 /*
768 * also decrements the sector in case of an overshot
769 * in the first loop
770 */
771 sector--;
wdenk2cefd152004-02-08 22:55:38 +0000772
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100773 saved_sector = sector;
Stefan Roese70a90b72013-04-12 19:04:54 +0200774 saved_info = info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200775 return sector;
wdenk2cefd152004-02-08 22:55:38 +0000776}
777
778/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000779 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200780static int flash_write_cfiword (flash_info_t * info, ulong dest,
781 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +0000782{
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600783 void *dstaddr = (void *)dest;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200784 int flag;
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100785 flash_sect_t sect = 0;
786 char sect_found = 0;
wdenk2cefd152004-02-08 22:55:38 +0000787
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200788 /* Check if Flash is (sufficiently) erased */
789 switch (info->portwidth) {
790 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100791 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200792 break;
793 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100794 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200795 break;
796 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100797 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200798 break;
799 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100800 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200801 break;
802 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100803 flag = 0;
804 break;
wdenk2cefd152004-02-08 22:55:38 +0000805 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600806 if (!flag)
Stefan Roese707c1462007-12-27 07:50:54 +0100807 return ERR_NOT_ERASED;
wdenk2cefd152004-02-08 22:55:38 +0000808
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200809 /* Disable interrupts which might cause a timeout here */
810 flag = disable_interrupts ();
Stefan Roesec865e6c2006-02-28 15:29:58 +0100811
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200812 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400813 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200814 case CFI_CMDSET_INTEL_EXTENDED:
815 case CFI_CMDSET_INTEL_STANDARD:
816 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
817 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
818 break;
819 case CFI_CMDSET_AMD_EXTENDED:
820 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500821 sect = find_sector(info, dest);
822 flash_unlock_seq (info, sect);
823 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100824 sect_found = 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200825 break;
Po-Yu Chuang3f483402009-07-10 18:03:57 +0800826#ifdef CONFIG_FLASH_CFI_LEGACY
827 case CFI_CMDSET_AMD_LEGACY:
828 sect = find_sector(info, dest);
829 flash_unlock_seq (info, 0);
830 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
831 sect_found = 1;
832 break;
833#endif
wdenk2cefd152004-02-08 22:55:38 +0000834 }
835
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200836 switch (info->portwidth) {
837 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100838 flash_write8(cword.c, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200839 break;
840 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100841 flash_write16(cword.w, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200842 break;
843 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100844 flash_write32(cword.l, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200845 break;
846 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100847 flash_write64(cword.ll, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200848 break;
wdenk2cefd152004-02-08 22:55:38 +0000849 }
850
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200851 /* re-enable interrupts if necessary */
852 if (flag)
853 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +0000854
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100855 if (!sect_found)
856 sect = find_sector (info, dest);
857
Thomas Chou076767a2010-03-26 08:17:00 +0800858 if (use_flash_status_poll(info))
859 return flash_status_poll(info, &cword, dstaddr,
860 info->write_tout, "write");
861 else
862 return flash_full_status_check(info, sect,
863 info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +0000864}
865
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200866#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenk2cefd152004-02-08 22:55:38 +0000867
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200868static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
869 int len)
wdenk2cefd152004-02-08 22:55:38 +0000870{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200871 flash_sect_t sector;
872 int cnt;
873 int retcode;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100874 void *src = cp;
Stefan Roese42c6ace2009-02-05 11:25:57 +0100875 void *dst = (void *)dest;
Stefan Roese707c1462007-12-27 07:50:54 +0100876 void *dst2 = dst;
Tao Houdd3b4552012-03-15 23:33:58 +0800877 int flag = 1;
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200878 uint offset = 0;
879 unsigned int shift;
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400880 uchar write_cmd;
Stefan Roese707c1462007-12-27 07:50:54 +0100881
882 switch (info->portwidth) {
883 case FLASH_CFI_8BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200884 shift = 0;
Stefan Roese707c1462007-12-27 07:50:54 +0100885 break;
886 case FLASH_CFI_16BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200887 shift = 1;
Stefan Roese707c1462007-12-27 07:50:54 +0100888 break;
889 case FLASH_CFI_32BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200890 shift = 2;
Stefan Roese707c1462007-12-27 07:50:54 +0100891 break;
892 case FLASH_CFI_64BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200893 shift = 3;
Stefan Roese707c1462007-12-27 07:50:54 +0100894 break;
895 default:
896 retcode = ERR_INVAL;
897 goto out_unmap;
898 }
899
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200900 cnt = len >> shift;
901
Tao Houdd3b4552012-03-15 23:33:58 +0800902 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese707c1462007-12-27 07:50:54 +0100903 switch (info->portwidth) {
904 case FLASH_CFI_8BIT:
905 flag = ((flash_read8(dst2) & flash_read8(src)) ==
906 flash_read8(src));
907 src += 1, dst2 += 1;
908 break;
909 case FLASH_CFI_16BIT:
910 flag = ((flash_read16(dst2) & flash_read16(src)) ==
911 flash_read16(src));
912 src += 2, dst2 += 2;
913 break;
914 case FLASH_CFI_32BIT:
915 flag = ((flash_read32(dst2) & flash_read32(src)) ==
916 flash_read32(src));
917 src += 4, dst2 += 4;
918 break;
919 case FLASH_CFI_64BIT:
920 flag = ((flash_read64(dst2) & flash_read64(src)) ==
921 flash_read64(src));
922 src += 8, dst2 += 8;
923 break;
924 }
925 }
926 if (!flag) {
927 retcode = ERR_NOT_ERASED;
928 goto out_unmap;
929 }
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100930
Stefan Roese707c1462007-12-27 07:50:54 +0100931 src = cp;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100932 sector = find_sector (info, dest);
wdenke65527f2004-02-12 00:47:09 +0000933
934 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400935 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +0000936 case CFI_CMDSET_INTEL_STANDARD:
937 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400938 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
939 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200940 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400941 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
942 flash_write_cmd (info, sector, 0, write_cmd);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200943 retcode = flash_status_check (info, sector,
944 info->buffer_write_tout,
945 "write to buffer");
946 if (retcode == ERR_OK) {
947 /* reduce the number of loops by the width of
948 * the port */
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200949 cnt = len >> shift;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400950 flash_write_cmd (info, sector, 0, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200951 while (cnt-- > 0) {
952 switch (info->portwidth) {
953 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100954 flash_write8(flash_read8(src), dst);
955 src += 1, dst += 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200956 break;
957 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100958 flash_write16(flash_read16(src), dst);
959 src += 2, dst += 2;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200960 break;
961 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100962 flash_write32(flash_read32(src), dst);
963 src += 4, dst += 4;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200964 break;
965 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100966 flash_write64(flash_read64(src), dst);
967 src += 8, dst += 8;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200968 break;
969 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100970 retcode = ERR_INVAL;
971 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200972 }
973 }
974 flash_write_cmd (info, sector, 0,
975 FLASH_CMD_WRITE_BUFFER_CONFIRM);
976 retcode = flash_full_status_check (
977 info, sector, info->buffer_write_tout,
978 "buffer write");
979 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100980
981 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200982
wdenk2cefd152004-02-08 22:55:38 +0000983 case CFI_CMDSET_AMD_STANDARD:
984 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200985 flash_unlock_seq(info,0);
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200986
987#ifdef CONFIG_FLASH_SPANSION_S29WS_N
988 offset = ((unsigned long)dst - info->start[sector]) >> shift;
989#endif
990 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
991 cnt = len >> shift;
John Schmolleree355882009-08-12 10:55:47 -0500992 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200993
994 switch (info->portwidth) {
995 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100996 while (cnt-- > 0) {
997 flash_write8(flash_read8(src), dst);
998 src += 1, dst += 1;
999 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001000 break;
1001 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001002 while (cnt-- > 0) {
1003 flash_write16(flash_read16(src), dst);
1004 src += 2, dst += 2;
1005 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001006 break;
1007 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001008 while (cnt-- > 0) {
1009 flash_write32(flash_read32(src), dst);
1010 src += 4, dst += 4;
1011 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001012 break;
1013 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001014 while (cnt-- > 0) {
1015 flash_write64(flash_read64(src), dst);
1016 src += 8, dst += 8;
1017 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001018 break;
1019 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001020 retcode = ERR_INVAL;
1021 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001022 }
1023
1024 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Chou076767a2010-03-26 08:17:00 +08001025 if (use_flash_status_poll(info))
1026 retcode = flash_status_poll(info, src - (1 << shift),
1027 dst - (1 << shift),
1028 info->buffer_write_tout,
1029 "buffer write");
1030 else
1031 retcode = flash_full_status_check(info, sector,
1032 info->buffer_write_tout,
1033 "buffer write");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001034 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001035
wdenk2cefd152004-02-08 22:55:38 +00001036 default:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001037 debug ("Unknown Command Set\n");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001038 retcode = ERR_INVAL;
1039 break;
wdenk2cefd152004-02-08 22:55:38 +00001040 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001041
1042out_unmap:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001043 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001044}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001045#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001046
wdenke65527f2004-02-12 00:47:09 +00001047
wdenk2cefd152004-02-08 22:55:38 +00001048/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +00001049 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001050int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +00001051{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001052 int rcode = 0;
1053 int prot;
1054 flash_sect_t sect;
Thomas Chou076767a2010-03-26 08:17:00 +08001055 int st;
wdenk2cefd152004-02-08 22:55:38 +00001056
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001057 if (info->flash_id != FLASH_MAN_CFI) {
1058 puts ("Can't erase unknown flash type - aborted\n");
1059 return 1;
1060 }
1061 if ((s_first < 0) || (s_first > s_last)) {
1062 puts ("- no sectors to erase\n");
1063 return 1;
1064 }
Stefan Roeseefef95b2006-04-01 13:41:03 +02001065
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001066 prot = 0;
1067 for (sect = s_first; sect <= s_last; ++sect) {
1068 if (info->protect[sect]) {
1069 prot++;
wdenk2cefd152004-02-08 22:55:38 +00001070 }
1071 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001072 if (prot) {
1073 printf ("- Warning: %d protected sectors will not be erased!\n",
1074 prot);
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001075 } else if (flash_verbose) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001076 putc ('\n');
1077 }
wdenke65527f2004-02-12 00:47:09 +00001078
wdenke65527f2004-02-12 00:47:09 +00001079
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001080 for (sect = s_first; sect <= s_last; sect++) {
Joe Hershberger497c32f2012-08-17 15:36:41 -05001081 if (ctrlc()) {
1082 printf("\n");
1083 return 1;
1084 }
1085
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001086 if (info->protect[sect] == 0) { /* not protected */
Joe Hershberger7f3c2112012-08-17 15:36:40 -05001087#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1088 int k;
1089 int size;
1090 int erased;
1091 u32 *flash;
1092
1093 /*
1094 * Check if whole sector is erased
1095 */
1096 size = flash_sector_size(info, sect);
1097 erased = 1;
1098 flash = (u32 *)info->start[sect];
1099 /* divide by 4 for longword access */
1100 size = size >> 2;
1101 for (k = 0; k < size; k++) {
1102 if (flash_read32(flash++) != 0xffffffff) {
1103 erased = 0;
1104 break;
1105 }
1106 }
1107 if (erased) {
1108 if (flash_verbose)
1109 putc(',');
1110 continue;
1111 }
1112#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001113 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001114 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001115 case CFI_CMDSET_INTEL_STANDARD:
1116 case CFI_CMDSET_INTEL_EXTENDED:
1117 flash_write_cmd (info, sect, 0,
1118 FLASH_CMD_CLEAR_STATUS);
1119 flash_write_cmd (info, sect, 0,
1120 FLASH_CMD_BLOCK_ERASE);
1121 flash_write_cmd (info, sect, 0,
1122 FLASH_CMD_ERASE_CONFIRM);
1123 break;
1124 case CFI_CMDSET_AMD_STANDARD:
1125 case CFI_CMDSET_AMD_EXTENDED:
1126 flash_unlock_seq (info, sect);
1127 flash_write_cmd (info, sect,
1128 info->addr_unlock1,
1129 AMD_CMD_ERASE_START);
1130 flash_unlock_seq (info, sect);
1131 flash_write_cmd (info, sect, 0,
Angelo Dureghello7ba30282012-12-01 01:14:18 +01001132 info->cmd_erase_sector);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001133 break;
1134#ifdef CONFIG_FLASH_CFI_LEGACY
1135 case CFI_CMDSET_AMD_LEGACY:
1136 flash_unlock_seq (info, 0);
1137 flash_write_cmd (info, 0, info->addr_unlock1,
1138 AMD_CMD_ERASE_START);
1139 flash_unlock_seq (info, 0);
1140 flash_write_cmd (info, sect, 0,
1141 AMD_CMD_ERASE_SECTOR);
1142 break;
1143#endif
1144 default:
1145 debug ("Unkown flash vendor %d\n",
1146 info->vendor);
1147 break;
wdenke65527f2004-02-12 00:47:09 +00001148 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001149
Thomas Chou076767a2010-03-26 08:17:00 +08001150 if (use_flash_status_poll(info)) {
Kim Phillipsd303b862012-10-29 13:34:45 +00001151 cfiword_t cword;
Thomas Chou076767a2010-03-26 08:17:00 +08001152 void *dest;
Kim Phillipsd303b862012-10-29 13:34:45 +00001153 cword.ll = 0xffffffffffffffffULL;
Thomas Chou076767a2010-03-26 08:17:00 +08001154 dest = flash_map(info, sect, 0);
1155 st = flash_status_poll(info, &cword, dest,
1156 info->erase_blk_tout, "erase");
1157 flash_unmap(info, sect, 0, dest);
1158 } else
1159 st = flash_full_status_check(info, sect,
1160 info->erase_blk_tout,
1161 "erase");
1162 if (st)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001163 rcode = 1;
Thomas Chou076767a2010-03-26 08:17:00 +08001164 else if (flash_verbose)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001165 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +00001166 }
wdenk2cefd152004-02-08 22:55:38 +00001167 }
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001168
1169 if (flash_verbose)
1170 puts (" done\n");
1171
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001172 return rcode;
wdenk2cefd152004-02-08 22:55:38 +00001173}
wdenke65527f2004-02-12 00:47:09 +00001174
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001175#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1176static int sector_erased(flash_info_t *info, int i)
1177{
1178 int k;
1179 int size;
Stefan Roesea9153f22010-10-25 18:31:39 +02001180 u32 *flash;
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001181
1182 /*
1183 * Check if whole sector is erased
1184 */
1185 size = flash_sector_size(info, i);
Stefan Roesea9153f22010-10-25 18:31:39 +02001186 flash = (u32 *)info->start[i];
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001187 /* divide by 4 for longword access */
1188 size = size >> 2;
1189
1190 for (k = 0; k < size; k++) {
Stefan Roesea9153f22010-10-25 18:31:39 +02001191 if (flash_read32(flash++) != 0xffffffff)
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001192 return 0; /* not erased */
1193 }
1194
1195 return 1; /* erased */
1196}
1197#endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1198
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001199void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001200{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001201 int i;
wdenk369d43d2004-03-14 14:09:05 +00001202
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001203 if (info->flash_id != FLASH_MAN_CFI) {
1204 puts ("missing or unknown FLASH type\n");
1205 return;
1206 }
1207
Peter Tyser0b5a2632010-12-28 18:12:05 -06001208 printf ("%s flash (%d x %d)",
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001209 info->name,
1210 (info->portwidth << 3), (info->chipwidth << 3));
1211 if (info->size < 1024*1024)
1212 printf (" Size: %ld kB in %d Sectors\n",
1213 info->size >> 10, info->sector_count);
1214 else
1215 printf (" Size: %ld MB in %d Sectors\n",
1216 info->size >> 20, info->sector_count);
1217 printf (" ");
1218 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001219 case CFI_CMDSET_INTEL_PROG_REGIONS:
1220 printf ("Intel Prog Regions");
1221 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001222 case CFI_CMDSET_INTEL_STANDARD:
1223 printf ("Intel Standard");
1224 break;
1225 case CFI_CMDSET_INTEL_EXTENDED:
1226 printf ("Intel Extended");
1227 break;
1228 case CFI_CMDSET_AMD_STANDARD:
1229 printf ("AMD Standard");
1230 break;
1231 case CFI_CMDSET_AMD_EXTENDED:
1232 printf ("AMD Extended");
1233 break;
1234#ifdef CONFIG_FLASH_CFI_LEGACY
1235 case CFI_CMDSET_AMD_LEGACY:
1236 printf ("AMD Legacy");
1237 break;
wdenk369d43d2004-03-14 14:09:05 +00001238#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001239 default:
1240 printf ("Unknown (%d)", info->vendor);
1241 break;
wdenk2cefd152004-02-08 22:55:38 +00001242 }
Philippe De Muyter35bc0812010-08-10 16:54:52 +02001243 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1244 info->manufacturer_id);
1245 printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1246 info->device_id);
Heiko Schocher27cea502011-04-11 14:16:19 +02001247 if ((info->device_id & 0xff) == 0x7E) {
1248 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1249 info->device_id2);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001250 }
Stefan Roesee442a902012-12-06 15:44:12 +01001251 if ((info->vendor == CFI_CMDSET_AMD_STANDARD) && (info->legacy_unlock))
1252 printf("\n Advanced Sector Protection (PPB) enabled");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001253 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1254 info->erase_blk_tout,
1255 info->write_tout);
1256 if (info->buffer_size > 1) {
1257 printf (" Buffer write timeout: %ld ms, "
1258 "buffer size: %d bytes\n",
1259 info->buffer_write_tout,
1260 info->buffer_size);
1261 }
wdenk2cefd152004-02-08 22:55:38 +00001262
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001263 puts ("\n Sector Start Addresses:");
1264 for (i = 0; i < info->sector_count; ++i) {
Kim Phillipsc8836f12010-07-26 18:35:39 -05001265 if (ctrlc())
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001266 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001267 if ((i % 5) == 0)
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001268 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001269#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001270 /* print empty and read-only info */
1271 printf (" %08lX %c %s ",
1272 info->start[i],
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001273 sector_erased(info, i) ? 'E' : ' ',
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001274 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001275#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001276 printf (" %08lX %s ",
1277 info->start[i],
1278 info->protect[i] ? "RO" : " ");
wdenke65527f2004-02-12 00:47:09 +00001279#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001280 }
1281 putc ('\n');
1282 return;
wdenk2cefd152004-02-08 22:55:38 +00001283}
1284
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001285/*-----------------------------------------------------------------------
Jerry Van Barenaae73572008-03-08 13:48:01 -05001286 * This is used in a few places in write_buf() to show programming
1287 * progress. Making it a function is nasty because it needs to do side
1288 * effect updates to digit and dots. Repeated code is nasty too, so
1289 * we define it once here.
1290 */
Stefan Roese7758c162008-03-19 07:09:26 +01001291#ifdef CONFIG_FLASH_SHOW_PROGRESS
1292#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001293 if (flash_verbose) { \
1294 dots -= dots_sub; \
1295 if ((scale > 0) && (dots <= 0)) { \
1296 if ((digit % 5) == 0) \
1297 printf ("%d", digit / 5); \
1298 else \
1299 putc ('.'); \
1300 digit--; \
1301 dots += scale; \
1302 } \
Jerry Van Barenaae73572008-03-08 13:48:01 -05001303 }
Stefan Roese7758c162008-03-19 07:09:26 +01001304#else
1305#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1306#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001307
1308/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001309 * Copy memory to flash, returns:
1310 * 0 - OK
1311 * 1 - write timeout
1312 * 2 - Flash not erased
wdenk2cefd152004-02-08 22:55:38 +00001313 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001314int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +00001315{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001316 ulong wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001317 uchar *p;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001318 int aln;
wdenk2cefd152004-02-08 22:55:38 +00001319 cfiword_t cword;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001320 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001321#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001322 int buffered_size;
wdenk2cefd152004-02-08 22:55:38 +00001323#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001324#ifdef CONFIG_FLASH_SHOW_PROGRESS
1325 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1326 int scale = 0;
1327 int dots = 0;
1328
1329 /*
1330 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1331 */
1332 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1333 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1334 CONFIG_FLASH_SHOW_PROGRESS);
1335 }
1336#endif
1337
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001338 /* get lower aligned address */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001339 wp = (addr & ~(info->portwidth - 1));
Haiying Wangc123a382007-02-21 16:52:31 +01001340
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001341 /* handle unaligned start */
1342 if ((aln = addr - wp) != 0) {
1343 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001344 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001345 for (i = 0; i < aln; ++i)
1346 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk2cefd152004-02-08 22:55:38 +00001347
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001348 for (; (i < info->portwidth) && (cnt > 0); i++) {
1349 flash_add_byte (info, &cword, *src++);
1350 cnt--;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001351 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001352 for (; (cnt == 0) && (i < info->portwidth); ++i)
1353 flash_add_byte (info, &cword, flash_read8(p + i));
1354
1355 rc = flash_write_cfiword (info, wp, cword);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001356 if (rc != 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001357 return rc;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001358
1359 wp += i;
Stefan Roese7758c162008-03-19 07:09:26 +01001360 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001361 }
1362
1363 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001364#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001365 buffered_size = (info->portwidth / info->chipwidth);
1366 buffered_size *= info->buffer_size;
1367 while (cnt >= info->portwidth) {
1368 /* prohibit buffer write when buffer_size is 1 */
1369 if (info->buffer_size == 1) {
1370 cword.l = 0;
1371 for (i = 0; i < info->portwidth; i++)
1372 flash_add_byte (info, &cword, *src++);
1373 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1374 return rc;
1375 wp += info->portwidth;
1376 cnt -= info->portwidth;
1377 continue;
1378 }
1379
1380 /* write buffer until next buffered_size aligned boundary */
1381 i = buffered_size - (wp % buffered_size);
1382 if (i > cnt)
1383 i = cnt;
1384 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1385 return rc;
1386 i -= i & (info->portwidth - 1);
1387 wp += i;
1388 src += i;
1389 cnt -= i;
Stefan Roese7758c162008-03-19 07:09:26 +01001390 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Joe Hershberger497c32f2012-08-17 15:36:41 -05001391 /* Only check every once in a while */
1392 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1393 return ERR_ABORTED;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001394 }
1395#else
1396 while (cnt >= info->portwidth) {
1397 cword.l = 0;
1398 for (i = 0; i < info->portwidth; i++) {
1399 flash_add_byte (info, &cword, *src++);
1400 }
1401 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1402 return rc;
1403 wp += info->portwidth;
1404 cnt -= info->portwidth;
Stefan Roese7758c162008-03-19 07:09:26 +01001405 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Joe Hershberger497c32f2012-08-17 15:36:41 -05001406 /* Only check every once in a while */
1407 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1408 return ERR_ABORTED;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001409 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001410#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Barenaae73572008-03-08 13:48:01 -05001411
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001412 if (cnt == 0) {
1413 return (0);
1414 }
1415
1416 /*
1417 * handle unaligned tail bytes
1418 */
1419 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001420 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001421 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001422 flash_add_byte (info, &cword, *src++);
1423 --cnt;
1424 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001425 for (; i < info->portwidth; ++i)
1426 flash_add_byte (info, &cword, flash_read8(p + i));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001427
1428 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +00001429}
wdenke65527f2004-02-12 00:47:09 +00001430
Stefan Roese92b1bca2012-12-06 15:44:09 +01001431static inline int manufact_match(flash_info_t *info, u32 manu)
1432{
1433 return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
1434}
1435
wdenk2cefd152004-02-08 22:55:38 +00001436/*-----------------------------------------------------------------------
1437 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001438#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001439
Holger Brunck94c302d2012-08-09 10:22:41 +02001440static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1441{
Stefan Roese92b1bca2012-12-06 15:44:09 +01001442 if (manufact_match(info, INTEL_MANUFACT)
Kim Phillipsd303b862012-10-29 13:34:45 +00001443 && info->device_id == NUMONYX_256MBIT) {
Holger Brunck94c302d2012-08-09 10:22:41 +02001444 /*
1445 * see errata called
1446 * "Numonyx Axcell P33/P30 Specification Update" :)
1447 */
1448 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1449 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1450 prot)) {
1451 /*
1452 * cmd must come before FLASH_CMD_PROTECT + 20us
1453 * Disable interrupts which might cause a timeout here.
1454 */
1455 int flag = disable_interrupts();
1456 unsigned short cmd;
1457
1458 if (prot)
1459 cmd = FLASH_CMD_PROTECT_SET;
1460 else
1461 cmd = FLASH_CMD_PROTECT_CLEAR;
1462 flash_write_cmd(info, sector, 0,
1463 FLASH_CMD_PROTECT);
1464 flash_write_cmd(info, sector, 0, cmd);
1465 /* re-enable interrupts if necessary */
1466 if (flag)
1467 enable_interrupts();
1468 }
1469 return 1;
1470 }
1471 return 0;
1472}
1473
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001474int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +00001475{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001476 int retcode = 0;
wdenke65527f2004-02-12 00:47:09 +00001477
Rafael Campos13d2b612008-07-31 10:22:20 +02001478 switch (info->vendor) {
1479 case CFI_CMDSET_INTEL_PROG_REGIONS:
1480 case CFI_CMDSET_INTEL_STANDARD:
Nick Spenceec81b472008-08-19 22:21:16 -07001481 case CFI_CMDSET_INTEL_EXTENDED:
Holger Brunck94c302d2012-08-09 10:22:41 +02001482 if (!cfi_protect_bugfix(info, sector, prot)) {
1483 flash_write_cmd(info, sector, 0,
1484 FLASH_CMD_CLEAR_STATUS);
1485 flash_write_cmd(info, sector, 0,
1486 FLASH_CMD_PROTECT);
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001487 if (prot)
Holger Brunck94c302d2012-08-09 10:22:41 +02001488 flash_write_cmd(info, sector, 0,
1489 FLASH_CMD_PROTECT_SET);
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001490 else
Holger Brunck94c302d2012-08-09 10:22:41 +02001491 flash_write_cmd(info, sector, 0,
1492 FLASH_CMD_PROTECT_CLEAR);
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001493
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001494 }
Rafael Campos13d2b612008-07-31 10:22:20 +02001495 break;
1496 case CFI_CMDSET_AMD_EXTENDED:
1497 case CFI_CMDSET_AMD_STANDARD:
Rafael Campos13d2b612008-07-31 10:22:20 +02001498 /* U-Boot only checks the first byte */
Stefan Roese92b1bca2012-12-06 15:44:09 +01001499 if (manufact_match(info, ATM_MANUFACT)) {
Rafael Campos13d2b612008-07-31 10:22:20 +02001500 if (prot) {
1501 flash_unlock_seq (info, 0);
1502 flash_write_cmd (info, 0,
1503 info->addr_unlock1,
1504 ATM_CMD_SOFTLOCK_START);
1505 flash_unlock_seq (info, 0);
1506 flash_write_cmd (info, sector, 0,
1507 ATM_CMD_LOCK_SECT);
1508 } else {
1509 flash_write_cmd (info, 0,
1510 info->addr_unlock1,
1511 AMD_CMD_UNLOCK_START);
1512 if (info->device_id == ATM_ID_BV6416)
1513 flash_write_cmd (info, sector,
1514 0, ATM_CMD_UNLOCK_SECT);
1515 }
1516 }
Stefan Roese7de65842012-12-06 15:44:11 +01001517 if (info->legacy_unlock) {
Anatolij Gustschin041b04f2012-08-09 08:18:12 +02001518 int flag = disable_interrupts();
1519 int lock_flag;
1520
1521 flash_unlock_seq(info, 0);
1522 flash_write_cmd(info, 0, info->addr_unlock1,
1523 AMD_CMD_SET_PPB_ENTRY);
1524 lock_flag = flash_isset(info, sector, 0, 0x01);
1525 if (prot) {
1526 if (lock_flag) {
1527 flash_write_cmd(info, sector, 0,
1528 AMD_CMD_PPB_LOCK_BC1);
1529 flash_write_cmd(info, sector, 0,
1530 AMD_CMD_PPB_LOCK_BC2);
1531 }
1532 debug("sector %ld %slocked\n", sector,
1533 lock_flag ? "" : "already ");
1534 } else {
1535 if (!lock_flag) {
1536 debug("unlock %ld\n", sector);
1537 flash_write_cmd(info, 0, 0,
1538 AMD_CMD_PPB_UNLOCK_BC1);
1539 flash_write_cmd(info, 0, 0,
1540 AMD_CMD_PPB_UNLOCK_BC2);
1541 }
1542 debug("sector %ld %sunlocked\n", sector,
1543 !lock_flag ? "" : "already ");
1544 }
1545 if (flag)
1546 enable_interrupts();
1547
1548 if (flash_status_check(info, sector,
1549 info->erase_blk_tout,
1550 prot ? "protect" : "unprotect"))
1551 printf("status check error\n");
1552
1553 flash_write_cmd(info, 0, 0,
1554 AMD_CMD_SET_PPB_EXIT_BC1);
1555 flash_write_cmd(info, 0, 0,
1556 AMD_CMD_SET_PPB_EXIT_BC2);
1557 }
Rafael Campos13d2b612008-07-31 10:22:20 +02001558 break;
TsiChung Liewb8c19292008-08-19 16:53:39 +00001559#ifdef CONFIG_FLASH_CFI_LEGACY
1560 case CFI_CMDSET_AMD_LEGACY:
1561 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1562 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1563 if (prot)
1564 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1565 else
1566 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1567#endif
Rafael Campos13d2b612008-07-31 10:22:20 +02001568 };
wdenk2cefd152004-02-08 22:55:38 +00001569
Stefan Roese5215df12010-10-25 18:31:29 +02001570 /*
1571 * Flash needs to be in status register read mode for
1572 * flash_full_status_check() to work correctly
1573 */
1574 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001575 if ((retcode =
1576 flash_full_status_check (info, sector, info->erase_blk_tout,
1577 prot ? "protect" : "unprotect")) == 0) {
wdenke65527f2004-02-12 00:47:09 +00001578
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001579 info->protect[sector] = prot;
1580
1581 /*
1582 * On some of Intel's flash chips (marked via legacy_unlock)
1583 * unprotect unprotects all locking.
1584 */
1585 if ((prot == 0) && (info->legacy_unlock)) {
1586 flash_sect_t i;
1587
1588 for (i = 0; i < info->sector_count; i++) {
1589 if (info->protect[i])
1590 flash_real_protect (info, i, 1);
1591 }
wdenk2cefd152004-02-08 22:55:38 +00001592 }
wdenk2cefd152004-02-08 22:55:38 +00001593 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001594 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001595}
wdenke65527f2004-02-12 00:47:09 +00001596
wdenk2cefd152004-02-08 22:55:38 +00001597/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001598 * flash_read_user_serial - read the OneTimeProgramming cells
wdenk2cefd152004-02-08 22:55:38 +00001599 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001600void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1601 int len)
wdenk2cefd152004-02-08 22:55:38 +00001602{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001603 uchar *src;
1604 uchar *dst;
wdenke65527f2004-02-12 00:47:09 +00001605
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001606 dst = buffer;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001607 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001608 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1609 memcpy (dst, src + offset, len);
1610 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001611 udelay(1);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001612 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001613}
1614
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001615/*
1616 * flash_read_factory_serial - read the device Id from the protection area
wdenk2cefd152004-02-08 22:55:38 +00001617 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001618void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1619 int len)
wdenk2cefd152004-02-08 22:55:38 +00001620{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001621 uchar *src;
wdenke65527f2004-02-12 00:47:09 +00001622
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001623 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001624 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1625 memcpy (buffer, src + offset, len);
1626 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001627 udelay(1);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001628 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001629}
1630
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001631#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001632
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001633/*-----------------------------------------------------------------------
1634 * Reverse the order of the erase regions in the CFI QRY structure.
1635 * This is needed for chips that are either a) correctly detected as
1636 * top-boot, or b) buggy.
1637 */
1638static void cfi_reverse_geometry(struct cfi_qry *qry)
1639{
1640 unsigned int i, j;
1641 u32 tmp;
1642
1643 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
Andrew Gabbasovc1592582013-05-14 12:27:52 -05001644 tmp = get_unaligned(&(qry->erase_region_info[i]));
1645 put_unaligned(get_unaligned(&(qry->erase_region_info[j])),
1646 &(qry->erase_region_info[i]));
1647 put_unaligned(tmp, &(qry->erase_region_info[j]));
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001648 }
1649}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001650
wdenk2cefd152004-02-08 22:55:38 +00001651/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +01001652 * read jedec ids from device and set corresponding fields in info struct
1653 *
1654 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1655 *
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001656 */
1657static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1658{
1659 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001660 udelay(1);
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001661 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1662 udelay(1000); /* some flash are slow to respond */
1663 info->manufacturer_id = flash_read_uchar (info,
1664 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyter35bc0812010-08-10 16:54:52 +02001665 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1666 flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
1667 flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001668 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1669}
1670
1671static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1672{
1673 info->cmd_reset = FLASH_CMD_RESET;
1674
1675 cmdset_intel_read_jedec_ids(info);
1676 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1677
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001678#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001679 /* read legacy lock/unlock bit from intel flash */
1680 if (info->ext_addr) {
1681 info->legacy_unlock = flash_read_uchar (info,
1682 info->ext_addr + 5) & 0x08;
1683 }
1684#endif
1685
1686 return 0;
1687}
1688
1689static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1690{
Niklaus Gigerf447f712009-07-22 17:13:24 +02001691 ushort bankId = 0;
1692 uchar manuId;
1693
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001694 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1695 flash_unlock_seq(info, 0);
1696 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1697 udelay(1000); /* some flash are slow to respond */
Tor Krill7f2a3052008-03-28 11:29:10 +01001698
Niklaus Gigerf447f712009-07-22 17:13:24 +02001699 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1700 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1701 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1702 bankId += 0x100;
1703 manuId = flash_read_uchar (info,
1704 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1705 }
1706 info->manufacturer_id = manuId;
Tor Krill7f2a3052008-03-28 11:29:10 +01001707
1708 switch (info->chipwidth){
1709 case FLASH_CFI_8BIT:
1710 info->device_id = flash_read_uchar (info,
1711 FLASH_OFFSET_DEVICE_ID);
1712 if (info->device_id == 0x7E) {
1713 /* AMD 3-byte (expanded) device ids */
1714 info->device_id2 = flash_read_uchar (info,
1715 FLASH_OFFSET_DEVICE_ID2);
1716 info->device_id2 <<= 8;
1717 info->device_id2 |= flash_read_uchar (info,
1718 FLASH_OFFSET_DEVICE_ID3);
1719 }
1720 break;
1721 case FLASH_CFI_16BIT:
1722 info->device_id = flash_read_word (info,
1723 FLASH_OFFSET_DEVICE_ID);
Heiko Schocher27cea502011-04-11 14:16:19 +02001724 if ((info->device_id & 0xff) == 0x7E) {
1725 /* AMD 3-byte (expanded) device ids */
1726 info->device_id2 = flash_read_uchar (info,
1727 FLASH_OFFSET_DEVICE_ID2);
1728 info->device_id2 <<= 8;
1729 info->device_id2 |= flash_read_uchar (info,
1730 FLASH_OFFSET_DEVICE_ID3);
1731 }
Tor Krill7f2a3052008-03-28 11:29:10 +01001732 break;
1733 default:
1734 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001735 }
1736 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001737 udelay(1);
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001738}
1739
1740static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1741{
1742 info->cmd_reset = AMD_CMD_RESET;
Angelo Dureghello7ba30282012-12-01 01:14:18 +01001743 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001744
1745 cmdset_amd_read_jedec_ids(info);
1746 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1747
Anatolij Gustschin041b04f2012-08-09 08:18:12 +02001748#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roese7de65842012-12-06 15:44:11 +01001749 if (info->ext_addr) {
1750 /* read sector protect/unprotect scheme (at 0x49) */
1751 if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
Anatolij Gustschin041b04f2012-08-09 08:18:12 +02001752 info->legacy_unlock = 1;
1753 }
1754#endif
1755
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001756 return 0;
1757}
1758
1759#ifdef CONFIG_FLASH_CFI_LEGACY
Stefan Roese12797482006-11-13 13:55:24 +01001760static void flash_read_jedec_ids (flash_info_t * info)
1761{
1762 info->manufacturer_id = 0;
1763 info->device_id = 0;
1764 info->device_id2 = 0;
1765
1766 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001767 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese12797482006-11-13 13:55:24 +01001768 case CFI_CMDSET_INTEL_STANDARD:
1769 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001770 cmdset_intel_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001771 break;
1772 case CFI_CMDSET_AMD_STANDARD:
1773 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001774 cmdset_amd_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001775 break;
1776 default:
1777 break;
1778 }
1779}
1780
1781/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001782 * Call board code to request info about non-CFI flash.
1783 * board_flash_get_legacy needs to fill in at least:
1784 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001785 */
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001786static int flash_detect_legacy(phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001787{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001788 flash_info_t *info = &flash_info[banknum];
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001789
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001790 if (board_flash_get_legacy(base, banknum, info)) {
1791 /* board code may have filled info completely. If not, we
1792 use JEDEC ID probing. */
1793 if (!info->vendor) {
1794 int modes[] = {
1795 CFI_CMDSET_AMD_STANDARD,
1796 CFI_CMDSET_INTEL_STANDARD
1797 };
1798 int i;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001799
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001800 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1801 info->vendor = modes[i];
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001802 info->start[0] =
1803 (ulong)map_physmem(base,
Stefan Roeseb8443702009-02-05 11:44:52 +01001804 info->portwidth,
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001805 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001806 if (info->portwidth == FLASH_CFI_8BIT
1807 && info->interface == FLASH_CFI_X8X16) {
1808 info->addr_unlock1 = 0x2AAA;
1809 info->addr_unlock2 = 0x5555;
1810 } else {
1811 info->addr_unlock1 = 0x5555;
1812 info->addr_unlock2 = 0x2AAA;
1813 }
1814 flash_read_jedec_ids(info);
1815 debug("JEDEC PROBE: ID %x %x %x\n",
1816 info->manufacturer_id,
1817 info->device_id,
1818 info->device_id2);
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001819 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001820 break;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001821 else
Stefan Roeseb8443702009-02-05 11:44:52 +01001822 unmap_physmem((void *)info->start[0],
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001823 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001824 }
1825 }
1826
1827 switch(info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001828 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001829 case CFI_CMDSET_INTEL_STANDARD:
1830 case CFI_CMDSET_INTEL_EXTENDED:
1831 info->cmd_reset = FLASH_CMD_RESET;
1832 break;
1833 case CFI_CMDSET_AMD_STANDARD:
1834 case CFI_CMDSET_AMD_EXTENDED:
1835 case CFI_CMDSET_AMD_LEGACY:
1836 info->cmd_reset = AMD_CMD_RESET;
1837 break;
1838 }
1839 info->flash_id = FLASH_MAN_CFI;
1840 return 1;
1841 }
1842 return 0; /* use CFI */
1843}
1844#else
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001845static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001846{
1847 return 0; /* use CFI */
1848}
1849#endif
1850
1851/*-----------------------------------------------------------------------
1852 * detect if flash is compatible with the Common Flash Interface (CFI)
1853 * http://www.jedec.org/download/search/jesd68.pdf
1854 */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001855static void flash_read_cfi (flash_info_t *info, void *buf,
1856 unsigned int start, size_t len)
1857{
1858 u8 *p = buf;
1859 unsigned int i;
1860
1861 for (i = 0; i < len; i++)
Stefan Roese70a90b72013-04-12 19:04:54 +02001862 p[i] = flash_read_uchar(info, start + i);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001863}
Stefan Roese6e83e342009-10-27 15:15:55 +01001864
Kim Phillipsd303b862012-10-29 13:34:45 +00001865static void __flash_cmd_reset(flash_info_t *info)
Stefan Roese6e83e342009-10-27 15:15:55 +01001866{
1867 /*
1868 * We do not yet know what kind of commandset to use, so we issue
1869 * the reset command in both Intel and AMD variants, in the hope
1870 * that AMD flash roms ignore the Intel command.
1871 */
1872 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001873 udelay(1);
Stefan Roese6e83e342009-10-27 15:15:55 +01001874 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1875}
1876void flash_cmd_reset(flash_info_t *info)
1877 __attribute__((weak,alias("__flash_cmd_reset")));
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001878
1879static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001880{
1881 int cfi_offset;
1882
Stefan Roese70a90b72013-04-12 19:04:54 +02001883 /* Issue FLASH reset command */
1884 flash_cmd_reset(info);
1885
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001886 for (cfi_offset=0;
1887 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1888 cfi_offset++) {
1889 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1890 FLASH_CMD_CFI);
Stefan Roese70a90b72013-04-12 19:04:54 +02001891 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1892 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1893 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001894 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1895 sizeof(struct cfi_qry));
1896 info->interface = le16_to_cpu(qry->interface_desc);
Stefan Roese70a90b72013-04-12 19:04:54 +02001897
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001898 info->cfi_offset = flash_offset_cfi[cfi_offset];
1899 debug ("device interface is %d\n",
1900 info->interface);
1901 debug ("found port %d chip %d ",
1902 info->portwidth, info->chipwidth);
1903 debug ("port %d bits chip %d bits\n",
1904 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1905 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1906
1907 /* calculate command offsets as in the Linux driver */
Stefan Roese70a90b72013-04-12 19:04:54 +02001908 info->addr_unlock1 = 0x555;
1909 info->addr_unlock2 = 0x2aa;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001910
1911 /*
1912 * modify the unlock address if we are
1913 * in compatibility mode
1914 */
1915 if ( /* x8/x16 in x8 mode */
1916 ((info->chipwidth == FLASH_CFI_BY8) &&
1917 (info->interface == FLASH_CFI_X8X16)) ||
1918 /* x16/x32 in x16 mode */
1919 ((info->chipwidth == FLASH_CFI_BY16) &&
1920 (info->interface == FLASH_CFI_X16X32)))
1921 {
1922 info->addr_unlock1 = 0xaaa;
1923 info->addr_unlock2 = 0x555;
1924 }
1925
1926 info->name = "CFI conformant";
1927 return 1;
1928 }
1929 }
1930
1931 return 0;
1932}
1933
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001934static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001935{
wdenke65527f2004-02-12 00:47:09 +00001936 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001937
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001938 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenke65527f2004-02-12 00:47:09 +00001939 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1940 for (info->chipwidth = FLASH_CFI_BY8;
1941 info->chipwidth <= info->portwidth;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001942 info->chipwidth <<= 1)
Stefan Roese70a90b72013-04-12 19:04:54 +02001943 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001944 return 1;
wdenk2cefd152004-02-08 22:55:38 +00001945 }
wdenke65527f2004-02-12 00:47:09 +00001946 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001947 return 0;
1948}
wdenke65527f2004-02-12 00:47:09 +00001949
wdenk2cefd152004-02-08 22:55:38 +00001950/*
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001951 * Manufacturer-specific quirks. Add workarounds for geometry
1952 * reversal, etc. here.
1953 */
1954static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1955{
1956 /* check if flash geometry needs reversal */
1957 if (qry->num_erase_regions > 1) {
1958 /* reverse geometry if top boot part */
1959 if (info->cfi_version < 0x3131) {
1960 /* CFI < 1.1, try to guess from device id */
1961 if ((info->device_id & 0x80) != 0)
1962 cfi_reverse_geometry(qry);
Stefan Roese70a90b72013-04-12 19:04:54 +02001963 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001964 /* CFI >= 1.1, deduct from top/bottom flag */
1965 /* note: ext_addr is valid since cfi_version > 0 */
1966 cfi_reverse_geometry(qry);
1967 }
1968 }
1969}
1970
1971static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1972{
1973 int reverse_geometry = 0;
1974
1975 /* Check the "top boot" bit in the PRI */
1976 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1977 reverse_geometry = 1;
1978
1979 /* AT49BV6416(T) list the erase regions in the wrong order.
1980 * However, the device ID is identical with the non-broken
Ulf Samuelsson07f9b4e2009-03-27 23:26:43 +01001981 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001982 */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001983 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1984 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001985
1986 if (reverse_geometry)
1987 cfi_reverse_geometry(qry);
1988}
1989
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001990static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1991{
1992 /* check if flash geometry needs reversal */
1993 if (qry->num_erase_regions > 1) {
1994 /* reverse geometry if top boot part */
1995 if (info->cfi_version < 0x3131) {
Mike Frysinger02a37862011-04-10 16:06:29 -04001996 /* CFI < 1.1, guess by device id */
1997 if (info->device_id == 0x22CA || /* M29W320DT */
1998 info->device_id == 0x2256 || /* M29W320ET */
1999 info->device_id == 0x22D7) { /* M29W800DT */
Richard Retanubunbe4dca22009-01-14 08:44:26 -05002000 cfi_reverse_geometry(qry);
2001 }
Mike Frysinger97dd8992011-05-09 18:33:36 -04002002 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
2003 /* CFI >= 1.1, deduct from top/bottom flag */
2004 /* note: ext_addr is valid since cfi_version > 0 */
2005 cfi_reverse_geometry(qry);
Richard Retanubunbe4dca22009-01-14 08:44:26 -05002006 }
2007 }
2008}
2009
Angelo Dureghello7ba30282012-12-01 01:14:18 +01002010static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
2011{
2012 /*
2013 * SST, for many recent nor parallel flashes, says they are
2014 * CFI-conformant. This is not true, since qry struct.
2015 * reports a std. AMD command set (0x0002), while SST allows to
2016 * erase two different sector sizes for the same memory.
2017 * 64KB sector (SST call it block) needs 0x30 to be erased.
2018 * 4KB sector (SST call it sector) needs 0x50 to be erased.
2019 * Since CFI query detect the 4KB number of sectors, users expects
2020 * a sector granularity of 4KB, and it is here set.
2021 */
2022 if (info->device_id == 0x5D23 || /* SST39VF3201B */
2023 info->device_id == 0x5C23) { /* SST39VF3202B */
2024 /* set sector granularity to 4KB */
2025 info->cmd_erase_sector=0x50;
2026 }
2027}
2028
Jagannadha Sutradharudu Teki130def32013-03-01 16:54:26 +05302029static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry)
2030{
2031 /*
2032 * The M29EW devices seem to report the CFI information wrong
2033 * when it's in 8 bit mode.
2034 * There's an app note from Numonyx on this issue.
2035 * So adjust the buffer size for M29EW while operating in 8-bit mode
2036 */
2037 if (((qry->max_buf_write_size) > 0x8) &&
2038 (info->device_id == 0x7E) &&
2039 (info->device_id2 == 0x2201 ||
2040 info->device_id2 == 0x2301 ||
2041 info->device_id2 == 0x2801 ||
2042 info->device_id2 == 0x4801)) {
2043 debug("Adjusted buffer size on Numonyx flash"
2044 " M29EW family in 8 bit mode\n");
2045 qry->max_buf_write_size = 0x8;
2046 }
2047}
2048
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002049/*
wdenk2cefd152004-02-08 22:55:38 +00002050 * The following code cannot be run from FLASH!
2051 *
2052 */
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002053ulong flash_get_size (phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00002054{
wdenke65527f2004-02-12 00:47:09 +00002055 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00002056 int i, j;
2057 flash_sect_t sect_cnt;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002058 phys_addr_t sector;
wdenk2cefd152004-02-08 22:55:38 +00002059 unsigned long tmp;
2060 int size_ratio;
2061 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00002062 int erase_region_size;
2063 int erase_region_count;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002064 struct cfi_qry qry;
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002065 unsigned long max_size;
Stefan Roese12797482006-11-13 13:55:24 +01002066
Kumar Gala899032b2008-05-15 15:13:08 -05002067 memset(&qry, 0, sizeof(qry));
2068
Stefan Roese12797482006-11-13 13:55:24 +01002069 info->ext_addr = 0;
2070 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002071#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02002072 info->legacy_unlock = 0;
2073#endif
wdenk2cefd152004-02-08 22:55:38 +00002074
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002075 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00002076
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002077 if (flash_detect_cfi (info, &qry)) {
Andrew Gabbasovc1592582013-05-14 12:27:52 -05002078 info->vendor = le16_to_cpu(get_unaligned(&(qry.p_id)));
2079 info->ext_addr = le16_to_cpu(get_unaligned(&(qry.p_adr)));
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002080 num_erase_regions = qry.num_erase_regions;
2081
Stefan Roese12797482006-11-13 13:55:24 +01002082 if (info->ext_addr) {
2083 info->cfi_version = (ushort) flash_read_uchar (info,
Stefan Roese70a90b72013-04-12 19:04:54 +02002084 info->ext_addr + 3) << 8;
Stefan Roese12797482006-11-13 13:55:24 +01002085 info->cfi_version |= (ushort) flash_read_uchar (info,
Stefan Roese70a90b72013-04-12 19:04:54 +02002086 info->ext_addr + 4);
Stefan Roese12797482006-11-13 13:55:24 +01002087 }
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002088
wdenke65527f2004-02-12 00:47:09 +00002089#ifdef DEBUG
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002090 flash_printqry (&qry);
wdenke65527f2004-02-12 00:47:09 +00002091#endif
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002092
wdenke65527f2004-02-12 00:47:09 +00002093 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04002094 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +00002095 case CFI_CMDSET_INTEL_STANDARD:
2096 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002097 cmdset_intel_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00002098 break;
2099 case CFI_CMDSET_AMD_STANDARD:
2100 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002101 cmdset_amd_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00002102 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002103 default:
2104 printf("CFI: Unknown command set 0x%x\n",
2105 info->vendor);
2106 /*
2107 * Unfortunately, this means we don't know how
2108 * to get the chip back to Read mode. Might
2109 * as well try an Intel-style reset...
2110 */
2111 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2112 return 0;
wdenk2cefd152004-02-08 22:55:38 +00002113 }
wdenk6cfa84e2004-02-10 00:03:41 +00002114
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002115 /* Do manufacturer-specific fixups */
2116 switch (info->manufacturer_id) {
Mario Schuknecht5c3579e2011-02-21 13:13:14 +01002117 case 0x0001: /* AMD */
2118 case 0x0037: /* AMIC */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002119 flash_fixup_amd(info, &qry);
2120 break;
2121 case 0x001f:
2122 flash_fixup_atmel(info, &qry);
2123 break;
Richard Retanubunbe4dca22009-01-14 08:44:26 -05002124 case 0x0020:
2125 flash_fixup_stm(info, &qry);
2126 break;
Angelo Dureghello7ba30282012-12-01 01:14:18 +01002127 case 0x00bf: /* SST */
2128 flash_fixup_sst(info, &qry);
2129 break;
Jagannadha Sutradharudu Teki130def32013-03-01 16:54:26 +05302130 case 0x0089: /* Numonyx */
2131 flash_fixup_num(info, &qry);
2132 break;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002133 }
2134
wdenke65527f2004-02-12 00:47:09 +00002135 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese12797482006-11-13 13:55:24 +01002136 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
2137 debug ("device id is 0x%x\n", info->device_id);
2138 debug ("device id2 is 0x%x\n", info->device_id2);
2139 debug ("cfi version is 0x%04x\n", info->cfi_version);
2140
wdenk2cefd152004-02-08 22:55:38 +00002141 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00002142 /* if the chip is x8/x16 reduce the ratio by half */
2143 if ((info->interface == FLASH_CFI_X8X16)
2144 && (info->chipwidth == FLASH_CFI_BY8)) {
2145 size_ratio >>= 1;
2146 }
wdenke65527f2004-02-12 00:47:09 +00002147 debug ("size_ratio %d port %d bits chip %d bits\n",
2148 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2149 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanok755c1802010-10-21 17:20:12 +02002150 info->size = 1 << qry.dev_size;
2151 /* multiply the size by the number of chips */
2152 info->size *= size_ratio;
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002153 max_size = cfi_flash_bank_size(banknum);
Ilya Yanok755c1802010-10-21 17:20:12 +02002154 if (max_size && (info->size > max_size)) {
2155 debug("[truncated from %ldMiB]", info->size >> 20);
2156 info->size = max_size;
2157 }
wdenke65527f2004-02-12 00:47:09 +00002158 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00002159 sect_cnt = 0;
2160 sector = base;
wdenke65527f2004-02-12 00:47:09 +00002161 for (i = 0; i < num_erase_regions; i++) {
2162 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00002163 printf ("%d erase regions found, only %d used\n",
2164 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00002165 break;
2166 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002167
Andrew Gabbasovc1592582013-05-14 12:27:52 -05002168 tmp = le32_to_cpu(get_unaligned(
2169 &(qry.erase_region_info[i])));
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002170 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002171
2172 erase_region_count = (tmp & 0xffff) + 1;
2173 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00002174 erase_region_size =
2175 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenkaeba06f2004-06-09 17:34:58 +00002176 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00002177 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00002178 for (j = 0; j < erase_region_count; j++) {
Ilya Yanok755c1802010-10-21 17:20:12 +02002179 if (sector - base >= info->size)
2180 break;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002181 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen73d044d2007-12-07 23:35:02 +01002182 printf("ERROR: too many flash sectors\n");
2183 break;
2184 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002185 info->start[sect_cnt] =
2186 (ulong)map_physmem(sector,
2187 info->portwidth,
2188 MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00002189 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00002190
2191 /*
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002192 * Only read protection status from
2193 * supported devices (intel...)
wdenk26c58432005-01-09 17:12:27 +00002194 */
2195 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04002196 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk26c58432005-01-09 17:12:27 +00002197 case CFI_CMDSET_INTEL_EXTENDED:
2198 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roese5215df12010-10-25 18:31:29 +02002199 /*
2200 * Set flash to read-id mode. Otherwise
2201 * reading protected status is not
2202 * guaranteed.
2203 */
2204 flash_write_cmd(info, sect_cnt, 0,
2205 FLASH_CMD_READ_ID);
wdenk26c58432005-01-09 17:12:27 +00002206 info->protect[sect_cnt] =
2207 flash_isset (info, sect_cnt,
2208 FLASH_OFFSET_PROTECT,
2209 FLASH_STATUS_PROTECT);
2210 break;
Stefan Roesebcb33442012-12-06 15:44:10 +01002211 case CFI_CMDSET_AMD_EXTENDED:
2212 case CFI_CMDSET_AMD_STANDARD:
Stefan Roese7de65842012-12-06 15:44:11 +01002213 if (!info->legacy_unlock) {
Stefan Roesebcb33442012-12-06 15:44:10 +01002214 /* default: not protected */
2215 info->protect[sect_cnt] = 0;
2216 break;
2217 }
2218
2219 /* Read protection (PPB) from sector */
2220 flash_write_cmd(info, 0, 0,
2221 info->cmd_reset);
2222 flash_unlock_seq(info, 0);
2223 flash_write_cmd(info, 0,
2224 info->addr_unlock1,
2225 FLASH_CMD_READ_ID);
2226 info->protect[sect_cnt] =
2227 flash_isset(
2228 info, sect_cnt,
2229 FLASH_OFFSET_PROTECT,
2230 FLASH_STATUS_PROTECT);
2231 break;
wdenk26c58432005-01-09 17:12:27 +00002232 default:
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002233 /* default: not protected */
2234 info->protect[sect_cnt] = 0;
wdenk26c58432005-01-09 17:12:27 +00002235 }
2236
wdenk2cefd152004-02-08 22:55:38 +00002237 sect_cnt++;
2238 }
2239 }
2240
2241 info->sector_count = sect_cnt;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002242 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2243 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002244 info->erase_blk_tout = tmp *
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002245 (1 << qry.block_erase_timeout_max);
2246 tmp = (1 << qry.buf_write_timeout_typ) *
2247 (1 << qry.buf_write_timeout_max);
2248
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002249 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002250 info->buffer_write_tout = (tmp + 999) / 1000;
2251 tmp = (1 << qry.word_write_timeout_typ) *
2252 (1 << qry.word_write_timeout_max);
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002253 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002254 info->write_tout = (tmp + 999) / 1000;
wdenk2cefd152004-02-08 22:55:38 +00002255 info->flash_id = FLASH_MAN_CFI;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002256 if ((info->interface == FLASH_CFI_X8X16) &&
2257 (info->chipwidth == FLASH_CFI_BY8)) {
2258 /* XXX - Need to test on x8/x16 in parallel. */
2259 info->portwidth >>= 1;
wdenked2ac4b2004-03-14 18:23:55 +00002260 }
Mike Frysinger59404ee2008-10-02 01:55:38 -04002261
2262 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +00002263 }
2264
wdenke65527f2004-02-12 00:47:09 +00002265 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00002266}
2267
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002268#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002269void flash_set_verbose(uint v)
2270{
2271 flash_verbose = v;
2272}
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002273#endif
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002274
Stefan Roeseab935642010-10-25 18:31:48 +02002275static void cfi_flash_set_config_reg(u32 base, u16 val)
2276{
2277#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2278 /*
2279 * Only set this config register if really defined
2280 * to a valid value (0xffff is invalid)
2281 */
2282 if (val == 0xffff)
2283 return;
2284
2285 /*
2286 * Set configuration register. Data is "encrypted" in the 16 lower
2287 * address bits.
2288 */
2289 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2290 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2291
2292 /*
2293 * Finally issue reset-command to bring device back to
2294 * read-array mode
2295 */
2296 flash_write16(FLASH_CMD_RESET, (void *)base);
2297#endif
2298}
2299
wdenk2cefd152004-02-08 22:55:38 +00002300/*-----------------------------------------------------------------------
2301 */
Heiko Schocheref0946a2011-04-04 08:10:21 +02002302
2303void flash_protect_default(void)
2304{
Peter Tyser4f3c60d2011-04-13 11:46:56 -05002305#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2306 int i;
2307 struct apl_s {
2308 ulong start;
2309 ulong size;
2310 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2311#endif
2312
Heiko Schocheref0946a2011-04-04 08:10:21 +02002313 /* Monitor protection ON by default */
2314#if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2315 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2316 flash_protect(FLAG_PROTECT_SET,
2317 CONFIG_SYS_MONITOR_BASE,
2318 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2319 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2320#endif
2321
2322 /* Environment protection ON by default */
2323#ifdef CONFIG_ENV_IS_IN_FLASH
2324 flash_protect(FLAG_PROTECT_SET,
2325 CONFIG_ENV_ADDR,
2326 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2327 flash_get_info(CONFIG_ENV_ADDR));
2328#endif
2329
2330 /* Redundant environment protection ON by default */
2331#ifdef CONFIG_ENV_ADDR_REDUND
2332 flash_protect(FLAG_PROTECT_SET,
2333 CONFIG_ENV_ADDR_REDUND,
2334 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2335 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2336#endif
2337
2338#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2339 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
Marek Vasutcb1622e2011-10-21 14:17:05 +00002340 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocheref0946a2011-04-04 08:10:21 +02002341 apl[i].start, apl[i].start + apl[i].size - 1);
2342 flash_protect(FLAG_PROTECT_SET,
2343 apl[i].start,
2344 apl[i].start + apl[i].size - 1,
2345 flash_get_info(apl[i].start));
2346 }
2347#endif
2348}
2349
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002350unsigned long flash_init (void)
wdenk2cefd152004-02-08 22:55:38 +00002351{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002352 unsigned long size = 0;
2353 int i;
wdenk2cefd152004-02-08 22:55:38 +00002354
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002355#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann8f7ee7d2009-03-21 09:59:34 -04002356 /* read environment from EEPROM */
2357 char s[64];
Wolfgang Denk76af2782010-07-24 21:55:43 +02002358 getenv_f("unlock", s, sizeof(s));
Michael Schwingen73d044d2007-12-07 23:35:02 +01002359#endif
wdenk2cefd152004-02-08 22:55:38 +00002360
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002361 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002362 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002363 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk2cefd152004-02-08 22:55:38 +00002364
Stefan Roeseab935642010-10-25 18:31:48 +02002365 /* Optionally write flash configuration register */
2366 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2367 cfi_flash_config_reg(i));
2368
Stefan Roese7e7dda82010-08-30 10:11:51 +02002369 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002370 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002371 size += flash_info[i].size;
2372 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002373#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Peter Tyser0b5a2632010-12-28 18:12:05 -06002374 printf ("## Unknown flash on Bank %d "
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002375 "- Size = 0x%08lx = %ld MB\n",
2376 i+1, flash_info[i].size,
John Schmoller61665db2010-09-29 13:49:05 -05002377 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002378#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002379 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002380#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002381 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
2382 /*
2383 * Only the U-Boot image and it's environment
2384 * is protected, all other sectors are
2385 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002386 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002387 * and the environment variable "unlock" is
2388 * set to "yes".
2389 */
2390 if (flash_info[i].legacy_unlock) {
2391 int k;
wdenk2cefd152004-02-08 22:55:38 +00002392
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002393 /*
2394 * Disable legacy_unlock temporarily,
2395 * since flash_real_protect would
2396 * relock all other sectors again
2397 * otherwise.
2398 */
2399 flash_info[i].legacy_unlock = 0;
wdenk2cefd152004-02-08 22:55:38 +00002400
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002401 /*
2402 * Legacy unlocking (e.g. Intel J3) ->
2403 * unlock only one sector. This will
2404 * unlock all sectors.
2405 */
2406 flash_real_protect (&flash_info[i], 0, 0);
wdenk2cefd152004-02-08 22:55:38 +00002407
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002408 flash_info[i].legacy_unlock = 1;
wdenk2cefd152004-02-08 22:55:38 +00002409
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002410 /*
2411 * Manually mark other sectors as
2412 * unlocked (unprotected)
2413 */
2414 for (k = 1; k < flash_info[i].sector_count; k++)
2415 flash_info[i].protect[k] = 0;
2416 } else {
2417 /*
2418 * No legancy unlocking -> unlock all sectors
2419 */
2420 flash_protect (FLAG_PROTECT_CLEAR,
2421 flash_info[i].start[0],
2422 flash_info[i].start[0]
2423 + flash_info[i].size - 1,
2424 &flash_info[i]);
Stefan Roesec865e6c2006-02-28 15:29:58 +01002425 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002426 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002427#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002428 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002429
Heiko Schocheref0946a2011-04-04 08:10:21 +02002430 flash_protect_default();
Piotr Ziecik3e939e92008-11-17 15:57:58 +01002431#ifdef CONFIG_FLASH_CFI_MTD
2432 cfi_mtd_init();
2433#endif
2434
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002435 return (size);
wdenk2cefd152004-02-08 22:55:38 +00002436}