blob: fc7a878d595c564d01b8214ea3236675aadf7655 [file] [log] [blame]
wdenk5653fc32004-02-08 22:55:38 +00001/*
wdenkbf9e3b32004-02-12 00:47:09 +00002 * (C) Copyright 2002-2004
wdenk5653fc32004-02-08 22:55:38 +00003 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4 *
5 * Copyright (C) 2003 Arabella Software Ltd.
6 * Yuli Barcohen <yuli@arabellasw.com>
wdenk5653fc32004-02-08 22:55:38 +00007 *
wdenkbf9e3b32004-02-12 00:47:09 +00008 * Copyright (C) 2004
9 * Ed Okerson
Stefan Roese260421a2006-11-13 13:55:24 +010010 *
11 * Copyright (C) 2006
12 * Tolunay Orkun <listmember@orkun.us>
wdenkbf9e3b32004-02-12 00:47:09 +000013 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
wdenk5653fc32004-02-08 22:55:38 +000015 */
16
17/* The DEBUG define must be before common to enable debugging */
wdenk2d1a5372004-02-23 19:30:57 +000018/* #define DEBUG */
19
wdenk5653fc32004-02-08 22:55:38 +000020#include <common.h>
21#include <asm/processor.h>
Haiying Wang3a197b22007-02-21 16:52:31 +010022#include <asm/io.h>
wdenk4c0d4c32004-06-09 17:34:58 +000023#include <asm/byteorder.h>
Andrew Gabbasovaedadf12013-05-14 12:27:52 -050024#include <asm/unaligned.h>
wdenk2a8af182005-04-13 10:02:42 +000025#include <environment.h>
Stefan Roesefa36ae72009-10-27 15:15:55 +010026#include <mtd/cfi_flash.h>
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +010027#include <watchdog.h>
wdenk028ab6b2004-02-23 23:54:43 +000028
wdenk5653fc32004-02-08 22:55:38 +000029/*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010030 * This file implements a Common Flash Interface (CFI) driver for
31 * U-Boot.
32 *
33 * The width of the port and the width of the chips are determined at
34 * initialization. These widths are used to calculate the address for
35 * access CFI data structures.
wdenk5653fc32004-02-08 22:55:38 +000036 *
37 * References
38 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
39 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
40 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
41 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese260421a2006-11-13 13:55:24 +010042 * AMD CFI Specification, Release 2.0 December 1, 2001
43 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
44 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk5653fc32004-02-08 22:55:38 +000045 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020046 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocherd0b6e142007-01-19 18:05:26 +010047 * reading and writing ... (yes there is such a Hardware).
wdenk5653fc32004-02-08 22:55:38 +000048 */
49
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010050static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysinger4ffeab22010-12-22 09:41:13 -050051#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +010052static uint flash_verbose = 1;
Mike Frysinger4ffeab22010-12-22 09:41:13 -050053#else
54#define flash_verbose 1
55#endif
Wolfgang Denk92eb7292006-12-27 01:26:13 +010056
Wolfgang Denk2a112b22008-08-08 16:39:54 +020057flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
58
Stefan Roese79b4cda2006-02-28 15:29:58 +010059/*
60 * Check if chip width is defined. If not, start detecting with 8bit.
61 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
63#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roese79b4cda2006-02-28 15:29:58 +010064#endif
65
Jeroen Hofstee00dcb072014-10-08 22:57:23 +020066#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
67#define __maybe_weak __weak
68#else
69#define __maybe_weak static
70#endif
71
Stefan Roese6f726f92010-10-25 18:31:48 +020072/*
73 * 0xffff is an undefined value for the configuration register. When
74 * this value is returned, the configuration register shall not be
75 * written at all (default mode).
76 */
77static u16 cfi_flash_config_reg(int i)
78{
79#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
80 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
81#else
82 return 0xffff;
83#endif
84}
85
Stefan Roeseca5def32010-08-31 10:00:10 +020086#if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
87int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
88#endif
89
Jeroen Hofstee00dcb072014-10-08 22:57:23 +020090__weak phys_addr_t cfi_flash_bank_addr(int i)
Stefan Roeseb00e19c2010-08-30 10:11:51 +020091{
92 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
93}
Stefan Roeseb00e19c2010-08-30 10:11:51 +020094
Jeroen Hofstee00dcb072014-10-08 22:57:23 +020095__weak unsigned long cfi_flash_bank_size(int i)
Ilya Yanokec50a8e2010-10-21 17:20:12 +020096{
97#ifdef CONFIG_SYS_FLASH_BANKS_SIZES
98 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
99#else
100 return 0;
101#endif
102}
Ilya Yanokec50a8e2010-10-21 17:20:12 +0200103
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200104__maybe_weak void flash_write8(u8 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100105{
106 __raw_writeb(value, addr);
107}
108
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200109__maybe_weak void flash_write16(u16 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100110{
111 __raw_writew(value, addr);
112}
113
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200114__maybe_weak void flash_write32(u32 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100115{
116 __raw_writel(value, addr);
117}
118
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200119__maybe_weak void flash_write64(u64 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100120{
121 /* No architectures currently implement __raw_writeq() */
122 *(volatile u64 *)addr = value;
123}
124
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200125__maybe_weak u8 flash_read8(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100126{
127 return __raw_readb(addr);
128}
129
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200130__maybe_weak u16 flash_read16(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100131{
132 return __raw_readw(addr);
133}
134
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200135__maybe_weak u32 flash_read32(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100136{
137 return __raw_readl(addr);
138}
139
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200140__maybe_weak u64 flash_read64(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100141{
142 /* No architectures currently implement __raw_readq() */
143 return *(volatile u64 *)addr;
144}
145
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200146/*-----------------------------------------------------------------------
147 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200148#if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
Heiko Schocher4f975672009-02-10 09:53:29 +0100149flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200150{
151 int i;
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900152 flash_info_t *info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200153
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Masahiro Yamadae2e273a2013-05-17 14:50:36 +0900155 info = &flash_info[i];
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200156 if (info->size && info->start[0] <= base &&
157 base <= info->start[0] + info->size - 1)
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900158 return info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200159 }
160
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900161 return NULL;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200162}
wdenk5653fc32004-02-08 22:55:38 +0000163#endif
164
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100165unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
166{
167 if (sect != (info->sector_count - 1))
168 return info->start[sect + 1] - info->start[sect];
169 else
170 return info->start[0] + info->size - info->start[sect];
171}
172
wdenk5653fc32004-02-08 22:55:38 +0000173/*-----------------------------------------------------------------------
174 * create an address based on the offset and the port width
175 */
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100176static inline void *
177flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000178{
Stefan Roesee303be22013-04-12 19:04:54 +0200179 unsigned int byte_offset = offset * info->portwidth;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100180
Stefan Roesee303be22013-04-12 19:04:54 +0200181 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100182}
183
184static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
185 unsigned int offset, void *addr)
186{
wdenk5653fc32004-02-08 22:55:38 +0000187}
wdenkbf9e3b32004-02-12 00:47:09 +0000188
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200189/*-----------------------------------------------------------------------
190 * make a proper sized command based on the port and chip widths
191 */
Sebastian Siewior7288f972008-07-15 13:35:23 +0200192static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200193{
194 int i;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400195 int cword_offset;
196 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200197#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewior340ccb22008-07-16 20:04:49 +0200198 u32 cmd_le = cpu_to_le32(cmd);
199#endif
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400200 uchar val;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200201 uchar *cp = (uchar *) cmdbuf;
202
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400203 for (i = info->portwidth; i > 0; i--){
204 cword_offset = (info->portwidth-i)%info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200205#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400206 cp_offset = info->portwidth - i;
Sebastian Siewior340ccb22008-07-16 20:04:49 +0200207 val = *((uchar*)&cmd_le + cword_offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200208#else
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400209 cp_offset = i - 1;
Sebastian Siewior7288f972008-07-15 13:35:23 +0200210 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200211#endif
Sebastian Siewior7288f972008-07-15 13:35:23 +0200212 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400213 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200214}
215
wdenkbf9e3b32004-02-12 00:47:09 +0000216#ifdef DEBUG
217/*-----------------------------------------------------------------------
218 * Debug support
219 */
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100220static void print_longlong (char *str, unsigned long long data)
wdenkbf9e3b32004-02-12 00:47:09 +0000221{
222 int i;
223 char *cp;
224
Wolfgang Denk657f2062009-02-04 09:42:20 +0100225 cp = (char *) &data;
wdenkbf9e3b32004-02-12 00:47:09 +0000226 for (i = 0; i < 8; i++)
227 sprintf (&str[i * 2], "%2.2x", *cp++);
228}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200229
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100230static void flash_printqry (struct cfi_qry *qry)
wdenkbf9e3b32004-02-12 00:47:09 +0000231{
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100232 u8 *p = (u8 *)qry;
wdenkbf9e3b32004-02-12 00:47:09 +0000233 int x, y;
234
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100235 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
236 debug("%02x : ", x);
237 for (y = 0; y < 16; y++)
238 debug("%2.2x ", p[x + y]);
239 debug(" ");
wdenkbf9e3b32004-02-12 00:47:09 +0000240 for (y = 0; y < 16; y++) {
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100241 unsigned char c = p[x + y];
242 if (c >= 0x20 && c <= 0x7e)
243 debug("%c", c);
244 else
245 debug(".");
wdenkbf9e3b32004-02-12 00:47:09 +0000246 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100247 debug("\n");
wdenkbf9e3b32004-02-12 00:47:09 +0000248 }
249}
wdenkbf9e3b32004-02-12 00:47:09 +0000250#endif
251
252
wdenk5653fc32004-02-08 22:55:38 +0000253/*-----------------------------------------------------------------------
254 * read a character at a port width address
255 */
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100256static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000257{
258 uchar *cp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100259 uchar retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000260
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100261 cp = flash_map (info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200262#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100263 retval = flash_read8(cp);
wdenkbf9e3b32004-02-12 00:47:09 +0000264#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100265 retval = flash_read8(cp + info->portwidth - 1);
wdenkbf9e3b32004-02-12 00:47:09 +0000266#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100267 flash_unmap (info, 0, offset, cp);
268 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000269}
270
271/*-----------------------------------------------------------------------
Tor Krill90447ec2008-03-28 11:29:10 +0100272 * read a word at a port width address, assume 16bit bus
273 */
274static inline ushort flash_read_word (flash_info_t * info, uint offset)
275{
276 ushort *addr, retval;
277
278 addr = flash_map (info, 0, offset);
279 retval = flash_read16 (addr);
280 flash_unmap (info, 0, offset, addr);
281 return retval;
282}
283
284
285/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +0100286 * read a long word by picking the least significant byte of each maximum
wdenk5653fc32004-02-08 22:55:38 +0000287 * port size word. Swap for ppc format.
288 */
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100289static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
290 uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000291{
wdenkbf9e3b32004-02-12 00:47:09 +0000292 uchar *addr;
293 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000294
wdenkbf9e3b32004-02-12 00:47:09 +0000295#ifdef DEBUG
296 int x;
297#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100298 addr = flash_map (info, sect, offset);
wdenkbf9e3b32004-02-12 00:47:09 +0000299
300#ifdef DEBUG
301 debug ("long addr is at %p info->portwidth = %d\n", addr,
302 info->portwidth);
303 for (x = 0; x < 4 * info->portwidth; x++) {
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100304 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenkbf9e3b32004-02-12 00:47:09 +0000305 }
306#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200307#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100308 retval = ((flash_read8(addr) << 16) |
309 (flash_read8(addr + info->portwidth) << 24) |
310 (flash_read8(addr + 2 * info->portwidth)) |
311 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenkbf9e3b32004-02-12 00:47:09 +0000312#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100313 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
314 (flash_read8(addr + info->portwidth - 1) << 16) |
315 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
316 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenkbf9e3b32004-02-12 00:47:09 +0000317#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100318 flash_unmap(info, sect, offset, addr);
319
wdenkbf9e3b32004-02-12 00:47:09 +0000320 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000321}
322
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200323/*
324 * Write a proper sized command to the correct address
325 */
Stefan Roesefa36ae72009-10-27 15:15:55 +0100326void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
327 uint offset, u32 cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200328{
Stefan Roese79b4cda2006-02-28 15:29:58 +0100329
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100330 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200331 cfiword_t cword;
332
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100333 addr = flash_map (info, sect, offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200334 flash_make_cmd (info, cmd, &cword);
335 switch (info->portwidth) {
336 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100337 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Ryan Harkin622b9522015-10-23 16:50:51 +0100338 cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
339 flash_write8(cword.w8, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200340 break;
341 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100342 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Ryan Harkin622b9522015-10-23 16:50:51 +0100343 cmd, cword.w16,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200344 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100345 flash_write16(cword.w16, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200346 break;
347 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100348 debug ("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr,
349 cmd, cword.w32,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200350 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100351 flash_write32(cword.w32, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200352 break;
353 case FLASH_CFI_64BIT:
354#ifdef DEBUG
355 {
356 char str[20];
357
Ryan Harkin622b9522015-10-23 16:50:51 +0100358 print_longlong (str, cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200359
360 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100361 addr, cmd, str,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200362 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
363 }
364#endif
Ryan Harkin622b9522015-10-23 16:50:51 +0100365 flash_write64(cword.w64, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200366 break;
367 }
368
369 /* Ensure all the instructions are fully finished */
370 sync();
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100371
372 flash_unmap(info, sect, offset, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200373}
374
375static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
376{
377 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
378 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
379}
380
381/*-----------------------------------------------------------------------
382 */
383static int flash_isequal (flash_info_t * info, flash_sect_t sect,
384 uint offset, uchar cmd)
385{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100386 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200387 cfiword_t cword;
388 int retval;
389
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100390 addr = flash_map (info, sect, offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200391 flash_make_cmd (info, cmd, &cword);
392
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100393 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200394 switch (info->portwidth) {
395 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100396 debug ("is= %x %x\n", flash_read8(addr), cword.w8);
397 retval = (flash_read8(addr) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200398 break;
399 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100400 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16);
401 retval = (flash_read16(addr) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200402 break;
403 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100404 debug ("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32);
405 retval = (flash_read32(addr) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200406 break;
407 case FLASH_CFI_64BIT:
408#ifdef DEBUG
409 {
410 char str1[20];
411 char str2[20];
412
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100413 print_longlong (str1, flash_read64(addr));
Ryan Harkin622b9522015-10-23 16:50:51 +0100414 print_longlong (str2, cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200415 debug ("is= %s %s\n", str1, str2);
416 }
417#endif
Ryan Harkin622b9522015-10-23 16:50:51 +0100418 retval = (flash_read64(addr) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200419 break;
420 default:
421 retval = 0;
422 break;
423 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100424 flash_unmap(info, sect, offset, addr);
425
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200426 return retval;
427}
428
429/*-----------------------------------------------------------------------
430 */
431static int flash_isset (flash_info_t * info, flash_sect_t sect,
432 uint offset, uchar cmd)
433{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100434 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200435 cfiword_t cword;
436 int retval;
437
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100438 addr = flash_map (info, sect, offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200439 flash_make_cmd (info, cmd, &cword);
440 switch (info->portwidth) {
441 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100442 retval = ((flash_read8(addr) & cword.w8) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200443 break;
444 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100445 retval = ((flash_read16(addr) & cword.w16) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200446 break;
447 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100448 retval = ((flash_read32(addr) & cword.w32) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200449 break;
450 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100451 retval = ((flash_read64(addr) & cword.w64) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200452 break;
453 default:
454 retval = 0;
455 break;
456 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100457 flash_unmap(info, sect, offset, addr);
458
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200459 return retval;
460}
461
462/*-----------------------------------------------------------------------
463 */
464static int flash_toggle (flash_info_t * info, flash_sect_t sect,
465 uint offset, uchar cmd)
466{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100467 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200468 cfiword_t cword;
469 int retval;
470
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100471 addr = flash_map (info, sect, offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200472 flash_make_cmd (info, cmd, &cword);
473 switch (info->portwidth) {
474 case FLASH_CFI_8BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200475 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200476 break;
477 case FLASH_CFI_16BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200478 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200479 break;
480 case FLASH_CFI_32BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200481 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200482 break;
483 case FLASH_CFI_64BIT:
Wolfgang Denk9abda6b2008-10-31 01:12:28 +0100484 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
485 (flash_read32(addr+4) != flash_read32(addr+4)) );
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200486 break;
487 default:
488 retval = 0;
489 break;
490 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100491 flash_unmap(info, sect, offset, addr);
492
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200493 return retval;
494}
495
496/*
497 * flash_is_busy - check to see if the flash is busy
498 *
499 * This routine checks the status of the chip and returns true if the
500 * chip is busy.
501 */
502static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
503{
504 int retval;
505
506 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400507 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200508 case CFI_CMDSET_INTEL_STANDARD:
509 case CFI_CMDSET_INTEL_EXTENDED:
510 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
511 break;
512 case CFI_CMDSET_AMD_STANDARD:
513 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100514#ifdef CONFIG_FLASH_CFI_LEGACY
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200515 case CFI_CMDSET_AMD_LEGACY:
516#endif
517 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
518 break;
519 default:
520 retval = 0;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100521 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200522 debug ("flash_is_busy: %d\n", retval);
523 return retval;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100524}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200525
526/*-----------------------------------------------------------------------
527 * wait for XSR.7 to be set. Time out with an error if it does not.
528 * This routine does not set the flash to read-array mode.
529 */
530static int flash_status_check (flash_info_t * info, flash_sect_t sector,
531 ulong tout, char *prompt)
532{
533 ulong start;
534
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200535#if CONFIG_SYS_HZ != 1000
Renato Andreolac40c94a2010-03-24 23:00:47 +0800536 if ((ulong)CONFIG_SYS_HZ > 100000)
537 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
538 else
539 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200540#endif
541
542 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000543#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800544 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000545#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200546 start = get_timer (0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100547 WATCHDOG_RESET();
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200548 while (flash_is_busy (info, sector)) {
549 if (get_timer (start) > tout) {
550 printf ("Flash %s timeout at address %lx data %lx\n",
551 prompt, info->start[sector],
552 flash_read_long (info, sector, 0));
553 flash_write_cmd (info, sector, 0, info->cmd_reset);
Stefan Roesee303be22013-04-12 19:04:54 +0200554 udelay(1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200555 return ERR_TIMOUT;
556 }
557 udelay (1); /* also triggers watchdog */
558 }
559 return ERR_OK;
560}
561
562/*-----------------------------------------------------------------------
563 * Wait for XSR.7 to be set, if it times out print an error, otherwise
564 * do a full status check.
565 *
566 * This routine sets the flash to read-array mode.
567 */
568static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
569 ulong tout, char *prompt)
570{
571 int retcode;
572
573 retcode = flash_status_check (info, sector, tout, prompt);
574 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400575 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200576 case CFI_CMDSET_INTEL_EXTENDED:
577 case CFI_CMDSET_INTEL_STANDARD:
Baruch Siach91693052014-09-04 12:23:09 +0300578 if ((retcode == ERR_OK)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200579 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
580 retcode = ERR_INVAL;
581 printf ("Flash %s error at address %lx\n", prompt,
582 info->start[sector]);
583 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
584 FLASH_STATUS_PSLBS)) {
585 puts ("Command Sequence Error.\n");
586 } else if (flash_isset (info, sector, 0,
587 FLASH_STATUS_ECLBS)) {
588 puts ("Block Erase Error.\n");
589 retcode = ERR_NOT_ERASED;
590 } else if (flash_isset (info, sector, 0,
591 FLASH_STATUS_PSLBS)) {
592 puts ("Locking Error\n");
593 }
594 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
595 puts ("Block locked.\n");
596 retcode = ERR_PROTECTED;
597 }
598 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
599 puts ("Vpp Low Error.\n");
600 }
601 flash_write_cmd (info, sector, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -0700602 udelay(1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200603 break;
604 default:
605 break;
606 }
607 return retcode;
608}
609
Thomas Choue5720822010-03-26 08:17:00 +0800610static int use_flash_status_poll(flash_info_t *info)
611{
612#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
613 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
614 info->vendor == CFI_CMDSET_AMD_STANDARD)
615 return 1;
616#endif
617 return 0;
618}
619
620static int flash_status_poll(flash_info_t *info, void *src, void *dst,
621 ulong tout, char *prompt)
622{
623#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
624 ulong start;
625 int ready;
626
627#if CONFIG_SYS_HZ != 1000
628 if ((ulong)CONFIG_SYS_HZ > 100000)
629 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
630 else
631 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
632#endif
633
634 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000635#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800636 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000637#endif
Thomas Choue5720822010-03-26 08:17:00 +0800638 start = get_timer(0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100639 WATCHDOG_RESET();
Thomas Choue5720822010-03-26 08:17:00 +0800640 while (1) {
641 switch (info->portwidth) {
642 case FLASH_CFI_8BIT:
643 ready = flash_read8(dst) == flash_read8(src);
644 break;
645 case FLASH_CFI_16BIT:
646 ready = flash_read16(dst) == flash_read16(src);
647 break;
648 case FLASH_CFI_32BIT:
649 ready = flash_read32(dst) == flash_read32(src);
650 break;
651 case FLASH_CFI_64BIT:
652 ready = flash_read64(dst) == flash_read64(src);
653 break;
654 default:
655 ready = 0;
656 break;
657 }
658 if (ready)
659 break;
660 if (get_timer(start) > tout) {
661 printf("Flash %s timeout at address %lx data %lx\n",
662 prompt, (ulong)dst, (ulong)flash_read8(dst));
663 return ERR_TIMOUT;
664 }
665 udelay(1); /* also triggers watchdog */
666 }
667#endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
668 return ERR_OK;
669}
670
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200671/*-----------------------------------------------------------------------
672 */
673static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
674{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200675#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200676 unsigned short w;
677 unsigned int l;
678 unsigned long long ll;
679#endif
680
681 switch (info->portwidth) {
682 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100683 cword->w8 = c;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200684 break;
685 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200686#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200687 w = c;
688 w <<= 8;
Ryan Harkin622b9522015-10-23 16:50:51 +0100689 cword->w16 = (cword->w16 >> 8) | w;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100690#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100691 cword->w16 = (cword->w16 << 8) | c;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100692#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200693 break;
694 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200695#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200696 l = c;
697 l <<= 24;
Ryan Harkin622b9522015-10-23 16:50:51 +0100698 cword->w32 = (cword->w32 >> 8) | l;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200699#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100700 cword->w32 = (cword->w32 << 8) | c;
Stefan Roese2662b402006-04-01 13:41:03 +0200701#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200702 break;
703 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200704#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200705 ll = c;
706 ll <<= 56;
Ryan Harkin622b9522015-10-23 16:50:51 +0100707 cword->w64 = (cword->w64 >> 8) | ll;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200708#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100709 cword->w64 = (cword->w64 << 8) | c;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200710#endif
711 break;
wdenk5653fc32004-02-08 22:55:38 +0000712 }
wdenk5653fc32004-02-08 22:55:38 +0000713}
714
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100715/*
716 * Loop through the sector table starting from the previously found sector.
717 * Searches forwards or backwards, dependent on the passed address.
wdenk5653fc32004-02-08 22:55:38 +0000718 */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200719static flash_sect_t find_sector (flash_info_t * info, ulong addr)
wdenk7680c142005-05-16 15:23:22 +0000720{
Kim Phillips11dc4012012-10-29 13:34:45 +0000721 static flash_sect_t saved_sector; /* previously found sector */
Stefan Roesee303be22013-04-12 19:04:54 +0200722 static flash_info_t *saved_info; /* previously used flash bank */
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100723 flash_sect_t sector = saved_sector;
wdenk7680c142005-05-16 15:23:22 +0000724
Stefan Roesee303be22013-04-12 19:04:54 +0200725 if ((info != saved_info) || (sector >= info->sector_count))
726 sector = 0;
727
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100728 while ((info->start[sector] < addr)
729 && (sector < info->sector_count - 1))
730 sector++;
731 while ((info->start[sector] > addr) && (sector > 0))
732 /*
733 * also decrements the sector in case of an overshot
734 * in the first loop
735 */
736 sector--;
737
738 saved_sector = sector;
Stefan Roesee303be22013-04-12 19:04:54 +0200739 saved_info = info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200740 return sector;
wdenk7680c142005-05-16 15:23:22 +0000741}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200742
743/*-----------------------------------------------------------------------
744 */
745static int flash_write_cfiword (flash_info_t * info, ulong dest,
746 cfiword_t cword)
747{
Becky Bruce09ce9922009-02-02 16:34:51 -0600748 void *dstaddr = (void *)dest;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200749 int flag;
Jens Gehrleina7292872008-12-16 17:25:54 +0100750 flash_sect_t sect = 0;
751 char sect_found = 0;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200752
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200753 /* Check if Flash is (sufficiently) erased */
754 switch (info->portwidth) {
755 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100756 flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200757 break;
758 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100759 flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200760 break;
761 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100762 flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200763 break;
764 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100765 flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200766 break;
767 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100768 flag = 0;
769 break;
770 }
Becky Bruce09ce9922009-02-02 16:34:51 -0600771 if (!flag)
Stefan Roese0dc80e22007-12-27 07:50:54 +0100772 return ERR_NOT_ERASED;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200773
774 /* Disable interrupts which might cause a timeout here */
775 flag = disable_interrupts ();
776
777 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400778 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200779 case CFI_CMDSET_INTEL_EXTENDED:
780 case CFI_CMDSET_INTEL_STANDARD:
781 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
782 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
783 break;
784 case CFI_CMDSET_AMD_EXTENDED:
785 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout0d01f662008-10-09 01:26:36 -0500786 sect = find_sector(info, dest);
787 flash_unlock_seq (info, sect);
788 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrleina7292872008-12-16 17:25:54 +0100789 sect_found = 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200790 break;
Po-Yu Chuangb4db4a72009-07-10 18:03:57 +0800791#ifdef CONFIG_FLASH_CFI_LEGACY
792 case CFI_CMDSET_AMD_LEGACY:
793 sect = find_sector(info, dest);
794 flash_unlock_seq (info, 0);
795 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
796 sect_found = 1;
797 break;
798#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200799 }
800
801 switch (info->portwidth) {
802 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100803 flash_write8(cword.w8, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200804 break;
805 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100806 flash_write16(cword.w16, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200807 break;
808 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100809 flash_write32(cword.w32, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200810 break;
811 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100812 flash_write64(cword.w64, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200813 break;
814 }
815
816 /* re-enable interrupts if necessary */
817 if (flag)
818 enable_interrupts ();
819
Jens Gehrleina7292872008-12-16 17:25:54 +0100820 if (!sect_found)
821 sect = find_sector (info, dest);
822
Thomas Choue5720822010-03-26 08:17:00 +0800823 if (use_flash_status_poll(info))
824 return flash_status_poll(info, &cword, dstaddr,
825 info->write_tout, "write");
826 else
827 return flash_full_status_check(info, sect,
828 info->write_tout, "write");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200829}
830
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200831#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200832
833static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
834 int len)
835{
836 flash_sect_t sector;
837 int cnt;
838 int retcode;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100839 void *src = cp;
Stefan Roeseec21d5c2009-02-05 11:25:57 +0100840 void *dst = (void *)dest;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100841 void *dst2 = dst;
Tao Hou85c344e2012-03-15 23:33:58 +0800842 int flag = 1;
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200843 uint offset = 0;
844 unsigned int shift;
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400845 uchar write_cmd;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100846
Stefan Roese0dc80e22007-12-27 07:50:54 +0100847 switch (info->portwidth) {
848 case FLASH_CFI_8BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200849 shift = 0;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100850 break;
851 case FLASH_CFI_16BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200852 shift = 1;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100853 break;
854 case FLASH_CFI_32BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200855 shift = 2;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100856 break;
857 case FLASH_CFI_64BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200858 shift = 3;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100859 break;
860 default:
861 retcode = ERR_INVAL;
862 goto out_unmap;
863 }
864
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200865 cnt = len >> shift;
866
Tao Hou85c344e2012-03-15 23:33:58 +0800867 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese0dc80e22007-12-27 07:50:54 +0100868 switch (info->portwidth) {
869 case FLASH_CFI_8BIT:
870 flag = ((flash_read8(dst2) & flash_read8(src)) ==
871 flash_read8(src));
872 src += 1, dst2 += 1;
873 break;
874 case FLASH_CFI_16BIT:
875 flag = ((flash_read16(dst2) & flash_read16(src)) ==
876 flash_read16(src));
877 src += 2, dst2 += 2;
878 break;
879 case FLASH_CFI_32BIT:
880 flag = ((flash_read32(dst2) & flash_read32(src)) ==
881 flash_read32(src));
882 src += 4, dst2 += 4;
883 break;
884 case FLASH_CFI_64BIT:
885 flag = ((flash_read64(dst2) & flash_read64(src)) ==
886 flash_read64(src));
887 src += 8, dst2 += 8;
888 break;
889 }
890 }
891 if (!flag) {
892 retcode = ERR_NOT_ERASED;
893 goto out_unmap;
894 }
895
896 src = cp;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100897 sector = find_sector (info, dest);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200898
899 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400900 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200901 case CFI_CMDSET_INTEL_STANDARD:
902 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400903 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
904 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200905 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400906 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
907 flash_write_cmd (info, sector, 0, write_cmd);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200908 retcode = flash_status_check (info, sector,
909 info->buffer_write_tout,
910 "write to buffer");
911 if (retcode == ERR_OK) {
912 /* reduce the number of loops by the width of
913 * the port */
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200914 cnt = len >> shift;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400915 flash_write_cmd (info, sector, 0, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200916 while (cnt-- > 0) {
917 switch (info->portwidth) {
918 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100919 flash_write8(flash_read8(src), dst);
920 src += 1, dst += 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200921 break;
922 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100923 flash_write16(flash_read16(src), dst);
924 src += 2, dst += 2;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200925 break;
926 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100927 flash_write32(flash_read32(src), dst);
928 src += 4, dst += 4;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200929 break;
930 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100931 flash_write64(flash_read64(src), dst);
932 src += 8, dst += 8;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200933 break;
934 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100935 retcode = ERR_INVAL;
936 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200937 }
938 }
939 flash_write_cmd (info, sector, 0,
940 FLASH_CMD_WRITE_BUFFER_CONFIRM);
941 retcode = flash_full_status_check (
942 info, sector, info->buffer_write_tout,
943 "buffer write");
944 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100945
946 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200947
948 case CFI_CMDSET_AMD_STANDARD:
949 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200950 flash_unlock_seq(info,0);
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200951
952#ifdef CONFIG_FLASH_SPANSION_S29WS_N
953 offset = ((unsigned long)dst - info->start[sector]) >> shift;
954#endif
955 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
956 cnt = len >> shift;
John Schmoller7dedefd2009-08-12 10:55:47 -0500957 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200958
959 switch (info->portwidth) {
960 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100961 while (cnt-- > 0) {
962 flash_write8(flash_read8(src), dst);
963 src += 1, dst += 1;
964 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200965 break;
966 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100967 while (cnt-- > 0) {
968 flash_write16(flash_read16(src), dst);
969 src += 2, dst += 2;
970 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200971 break;
972 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100973 while (cnt-- > 0) {
974 flash_write32(flash_read32(src), dst);
975 src += 4, dst += 4;
976 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200977 break;
978 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100979 while (cnt-- > 0) {
980 flash_write64(flash_read64(src), dst);
981 src += 8, dst += 8;
982 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200983 break;
984 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100985 retcode = ERR_INVAL;
986 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200987 }
988
989 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Choue5720822010-03-26 08:17:00 +0800990 if (use_flash_status_poll(info))
991 retcode = flash_status_poll(info, src - (1 << shift),
992 dst - (1 << shift),
993 info->buffer_write_tout,
994 "buffer write");
995 else
996 retcode = flash_full_status_check(info, sector,
997 info->buffer_write_tout,
998 "buffer write");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100999 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001000
1001 default:
1002 debug ("Unknown Command Set\n");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001003 retcode = ERR_INVAL;
1004 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001005 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001006
1007out_unmap:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001008 return retcode;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001009}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001010#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001011
wdenk7680c142005-05-16 15:23:22 +00001012
1013/*-----------------------------------------------------------------------
1014 */
wdenkbf9e3b32004-02-12 00:47:09 +00001015int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +00001016{
1017 int rcode = 0;
1018 int prot;
1019 flash_sect_t sect;
Thomas Choue5720822010-03-26 08:17:00 +08001020 int st;
wdenk5653fc32004-02-08 22:55:38 +00001021
wdenkbf9e3b32004-02-12 00:47:09 +00001022 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +00001023 puts ("Can't erase unknown flash type - aborted\n");
wdenk5653fc32004-02-08 22:55:38 +00001024 return 1;
1025 }
1026 if ((s_first < 0) || (s_first > s_last)) {
wdenk4b9206e2004-03-23 22:14:11 +00001027 puts ("- no sectors to erase\n");
wdenk5653fc32004-02-08 22:55:38 +00001028 return 1;
1029 }
1030
1031 prot = 0;
wdenkbf9e3b32004-02-12 00:47:09 +00001032 for (sect = s_first; sect <= s_last; ++sect) {
wdenk5653fc32004-02-08 22:55:38 +00001033 if (info->protect[sect]) {
1034 prot++;
1035 }
1036 }
1037 if (prot) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001038 printf ("- Warning: %d protected sectors will not be erased!\n",
1039 prot);
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001040 } else if (flash_verbose) {
wdenk4b9206e2004-03-23 22:14:11 +00001041 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +00001042 }
1043
1044
wdenkbf9e3b32004-02-12 00:47:09 +00001045 for (sect = s_first; sect <= s_last; sect++) {
Joe Hershbergerde15a062012-08-17 15:36:41 -05001046 if (ctrlc()) {
1047 printf("\n");
1048 return 1;
1049 }
1050
wdenk5653fc32004-02-08 22:55:38 +00001051 if (info->protect[sect] == 0) { /* not protected */
Joe Hershberger6822a642012-08-17 15:36:40 -05001052#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1053 int k;
1054 int size;
1055 int erased;
1056 u32 *flash;
1057
1058 /*
1059 * Check if whole sector is erased
1060 */
1061 size = flash_sector_size(info, sect);
1062 erased = 1;
1063 flash = (u32 *)info->start[sect];
1064 /* divide by 4 for longword access */
1065 size = size >> 2;
1066 for (k = 0; k < size; k++) {
1067 if (flash_read32(flash++) != 0xffffffff) {
1068 erased = 0;
1069 break;
1070 }
1071 }
1072 if (erased) {
1073 if (flash_verbose)
1074 putc(',');
1075 continue;
1076 }
1077#endif
wdenkbf9e3b32004-02-12 00:47:09 +00001078 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001079 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00001080 case CFI_CMDSET_INTEL_STANDARD:
1081 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001082 flash_write_cmd (info, sect, 0,
1083 FLASH_CMD_CLEAR_STATUS);
1084 flash_write_cmd (info, sect, 0,
1085 FLASH_CMD_BLOCK_ERASE);
1086 flash_write_cmd (info, sect, 0,
1087 FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +00001088 break;
1089 case CFI_CMDSET_AMD_STANDARD:
1090 case CFI_CMDSET_AMD_EXTENDED:
wdenkbf9e3b32004-02-12 00:47:09 +00001091 flash_unlock_seq (info, sect);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001092 flash_write_cmd (info, sect,
1093 info->addr_unlock1,
1094 AMD_CMD_ERASE_START);
wdenkbf9e3b32004-02-12 00:47:09 +00001095 flash_unlock_seq (info, sect);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001096 flash_write_cmd (info, sect, 0,
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01001097 info->cmd_erase_sector);
wdenk5653fc32004-02-08 22:55:38 +00001098 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001099#ifdef CONFIG_FLASH_CFI_LEGACY
1100 case CFI_CMDSET_AMD_LEGACY:
1101 flash_unlock_seq (info, 0);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001102 flash_write_cmd (info, 0, info->addr_unlock1,
1103 AMD_CMD_ERASE_START);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001104 flash_unlock_seq (info, 0);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001105 flash_write_cmd (info, sect, 0,
1106 AMD_CMD_ERASE_SECTOR);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001107 break;
1108#endif
wdenk5653fc32004-02-08 22:55:38 +00001109 default:
wdenkbf9e3b32004-02-12 00:47:09 +00001110 debug ("Unkown flash vendor %d\n",
1111 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +00001112 break;
1113 }
1114
Thomas Choue5720822010-03-26 08:17:00 +08001115 if (use_flash_status_poll(info)) {
Kim Phillips11dc4012012-10-29 13:34:45 +00001116 cfiword_t cword;
Thomas Choue5720822010-03-26 08:17:00 +08001117 void *dest;
Ryan Harkin622b9522015-10-23 16:50:51 +01001118 cword.w64 = 0xffffffffffffffffULL;
Thomas Choue5720822010-03-26 08:17:00 +08001119 dest = flash_map(info, sect, 0);
1120 st = flash_status_poll(info, &cword, dest,
1121 info->erase_blk_tout, "erase");
1122 flash_unmap(info, sect, 0, dest);
1123 } else
1124 st = flash_full_status_check(info, sect,
1125 info->erase_blk_tout,
1126 "erase");
1127 if (st)
wdenk5653fc32004-02-08 22:55:38 +00001128 rcode = 1;
Thomas Choue5720822010-03-26 08:17:00 +08001129 else if (flash_verbose)
wdenk4b9206e2004-03-23 22:14:11 +00001130 putc ('.');
wdenk5653fc32004-02-08 22:55:38 +00001131 }
1132 }
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001133
1134 if (flash_verbose)
1135 puts (" done\n");
1136
wdenk5653fc32004-02-08 22:55:38 +00001137 return rcode;
1138}
1139
Stefan Roese70084df2010-08-13 09:36:36 +02001140#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1141static int sector_erased(flash_info_t *info, int i)
1142{
1143 int k;
1144 int size;
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001145 u32 *flash;
Stefan Roese70084df2010-08-13 09:36:36 +02001146
1147 /*
1148 * Check if whole sector is erased
1149 */
1150 size = flash_sector_size(info, i);
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001151 flash = (u32 *)info->start[i];
Stefan Roese70084df2010-08-13 09:36:36 +02001152 /* divide by 4 for longword access */
1153 size = size >> 2;
1154
1155 for (k = 0; k < size; k++) {
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001156 if (flash_read32(flash++) != 0xffffffff)
Stefan Roese70084df2010-08-13 09:36:36 +02001157 return 0; /* not erased */
1158 }
1159
1160 return 1; /* erased */
1161}
1162#endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1163
wdenkbf9e3b32004-02-12 00:47:09 +00001164void flash_print_info (flash_info_t * info)
wdenk5653fc32004-02-08 22:55:38 +00001165{
1166 int i;
1167
1168 if (info->flash_id != FLASH_MAN_CFI) {
wdenk4b9206e2004-03-23 22:14:11 +00001169 puts ("missing or unknown FLASH type\n");
wdenk5653fc32004-02-08 22:55:38 +00001170 return;
1171 }
1172
Peter Tysereddf52b2010-12-28 18:12:05 -06001173 printf ("%s flash (%d x %d)",
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001174 info->name,
wdenkbf9e3b32004-02-12 00:47:09 +00001175 (info->portwidth << 3), (info->chipwidth << 3));
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001176 if (info->size < 1024*1024)
1177 printf (" Size: %ld kB in %d Sectors\n",
1178 info->size >> 10, info->sector_count);
1179 else
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001180 printf (" Size: %ld MB in %d Sectors\n",
1181 info->size >> 20, info->sector_count);
Stefan Roese260421a2006-11-13 13:55:24 +01001182 printf (" ");
1183 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001184 case CFI_CMDSET_INTEL_PROG_REGIONS:
1185 printf ("Intel Prog Regions");
1186 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001187 case CFI_CMDSET_INTEL_STANDARD:
1188 printf ("Intel Standard");
1189 break;
1190 case CFI_CMDSET_INTEL_EXTENDED:
1191 printf ("Intel Extended");
1192 break;
1193 case CFI_CMDSET_AMD_STANDARD:
1194 printf ("AMD Standard");
1195 break;
1196 case CFI_CMDSET_AMD_EXTENDED:
1197 printf ("AMD Extended");
1198 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001199#ifdef CONFIG_FLASH_CFI_LEGACY
1200 case CFI_CMDSET_AMD_LEGACY:
1201 printf ("AMD Legacy");
1202 break;
1203#endif
Stefan Roese260421a2006-11-13 13:55:24 +01001204 default:
1205 printf ("Unknown (%d)", info->vendor);
1206 break;
1207 }
Philippe De Muyterd77c7ac2010-08-10 16:54:52 +02001208 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1209 info->manufacturer_id);
1210 printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1211 info->device_id);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001212 if ((info->device_id & 0xff) == 0x7E) {
1213 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1214 info->device_id2);
Stefan Roese260421a2006-11-13 13:55:24 +01001215 }
Stefan Roesed2af0282012-12-06 15:44:12 +01001216 if ((info->vendor == CFI_CMDSET_AMD_STANDARD) && (info->legacy_unlock))
1217 printf("\n Advanced Sector Protection (PPB) enabled");
Stefan Roese260421a2006-11-13 13:55:24 +01001218 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
wdenk028ab6b2004-02-23 23:54:43 +00001219 info->erase_blk_tout,
Stefan Roese260421a2006-11-13 13:55:24 +01001220 info->write_tout);
1221 if (info->buffer_size > 1) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001222 printf (" Buffer write timeout: %ld ms, "
1223 "buffer size: %d bytes\n",
wdenk028ab6b2004-02-23 23:54:43 +00001224 info->buffer_write_tout,
1225 info->buffer_size);
Stefan Roese260421a2006-11-13 13:55:24 +01001226 }
wdenk5653fc32004-02-08 22:55:38 +00001227
Stefan Roese260421a2006-11-13 13:55:24 +01001228 puts ("\n Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +00001229 for (i = 0; i < info->sector_count; ++i) {
Kim Phillips2e973942010-07-26 18:35:39 -05001230 if (ctrlc())
Stefan Roese70084df2010-08-13 09:36:36 +02001231 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001232 if ((i % 5) == 0)
Stefan Roese70084df2010-08-13 09:36:36 +02001233 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001234#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
wdenk5653fc32004-02-08 22:55:38 +00001235 /* print empty and read-only info */
Stefan Roese260421a2006-11-13 13:55:24 +01001236 printf (" %08lX %c %s ",
wdenk5653fc32004-02-08 22:55:38 +00001237 info->start[i],
Stefan Roese70084df2010-08-13 09:36:36 +02001238 sector_erased(info, i) ? 'E' : ' ',
Stefan Roese260421a2006-11-13 13:55:24 +01001239 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001240#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Stefan Roese260421a2006-11-13 13:55:24 +01001241 printf (" %08lX %s ",
1242 info->start[i],
1243 info->protect[i] ? "RO" : " ");
wdenk5653fc32004-02-08 22:55:38 +00001244#endif
1245 }
wdenk4b9206e2004-03-23 22:14:11 +00001246 putc ('\n');
wdenk5653fc32004-02-08 22:55:38 +00001247 return;
1248}
1249
1250/*-----------------------------------------------------------------------
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001251 * This is used in a few places in write_buf() to show programming
1252 * progress. Making it a function is nasty because it needs to do side
1253 * effect updates to digit and dots. Repeated code is nasty too, so
1254 * we define it once here.
1255 */
Stefan Roesef0105722008-03-19 07:09:26 +01001256#ifdef CONFIG_FLASH_SHOW_PROGRESS
1257#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001258 if (flash_verbose) { \
1259 dots -= dots_sub; \
1260 if ((scale > 0) && (dots <= 0)) { \
1261 if ((digit % 5) == 0) \
1262 printf ("%d", digit / 5); \
1263 else \
1264 putc ('.'); \
1265 digit--; \
1266 dots += scale; \
1267 } \
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001268 }
Stefan Roesef0105722008-03-19 07:09:26 +01001269#else
1270#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1271#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001272
1273/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001274 * Copy memory to flash, returns:
1275 * 0 - OK
1276 * 1 - write timeout
1277 * 2 - Flash not erased
1278 */
wdenkbf9e3b32004-02-12 00:47:09 +00001279int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +00001280{
1281 ulong wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001282 uchar *p;
wdenk5653fc32004-02-08 22:55:38 +00001283 int aln;
1284 cfiword_t cword;
1285 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001286#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001287 int buffered_size;
1288#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001289#ifdef CONFIG_FLASH_SHOW_PROGRESS
1290 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1291 int scale = 0;
1292 int dots = 0;
1293
1294 /*
1295 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1296 */
1297 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1298 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1299 CONFIG_FLASH_SHOW_PROGRESS);
1300 }
1301#endif
1302
wdenkbf9e3b32004-02-12 00:47:09 +00001303 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +00001304 wp = (addr & ~(info->portwidth - 1));
1305
1306 /* handle unaligned start */
wdenkbf9e3b32004-02-12 00:47:09 +00001307 if ((aln = addr - wp) != 0) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001308 cword.w32 = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001309 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001310 for (i = 0; i < aln; ++i)
1311 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001312
wdenkbf9e3b32004-02-12 00:47:09 +00001313 for (; (i < info->portwidth) && (cnt > 0); i++) {
1314 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001315 cnt--;
wdenk5653fc32004-02-08 22:55:38 +00001316 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001317 for (; (cnt == 0) && (i < info->portwidth); ++i)
1318 flash_add_byte (info, &cword, flash_read8(p + i));
1319
1320 rc = flash_write_cfiword (info, wp, cword);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001321 if (rc != 0)
wdenk5653fc32004-02-08 22:55:38 +00001322 return rc;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001323
1324 wp += i;
Stefan Roesef0105722008-03-19 07:09:26 +01001325 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
wdenk5653fc32004-02-08 22:55:38 +00001326 }
1327
wdenkbf9e3b32004-02-12 00:47:09 +00001328 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001329#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001330 buffered_size = (info->portwidth / info->chipwidth);
1331 buffered_size *= info->buffer_size;
1332 while (cnt >= info->portwidth) {
Stefan Roese79b4cda2006-02-28 15:29:58 +01001333 /* prohibit buffer write when buffer_size is 1 */
1334 if (info->buffer_size == 1) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001335 cword.w32 = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01001336 for (i = 0; i < info->portwidth; i++)
1337 flash_add_byte (info, &cword, *src++);
1338 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1339 return rc;
1340 wp += info->portwidth;
1341 cnt -= info->portwidth;
1342 continue;
1343 }
1344
1345 /* write buffer until next buffered_size aligned boundary */
1346 i = buffered_size - (wp % buffered_size);
1347 if (i > cnt)
1348 i = cnt;
wdenkbf9e3b32004-02-12 00:47:09 +00001349 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +00001350 return rc;
Wolfgang Denk8d4ba3d2005-08-12 22:35:59 +02001351 i -= i & (info->portwidth - 1);
wdenk5653fc32004-02-08 22:55:38 +00001352 wp += i;
1353 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +00001354 cnt -= i;
Stefan Roesef0105722008-03-19 07:09:26 +01001355 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001356 /* Only check every once in a while */
1357 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1358 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001359 }
1360#else
wdenkbf9e3b32004-02-12 00:47:09 +00001361 while (cnt >= info->portwidth) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001362 cword.w32 = 0;
wdenkbf9e3b32004-02-12 00:47:09 +00001363 for (i = 0; i < info->portwidth; i++) {
1364 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001365 }
wdenkbf9e3b32004-02-12 00:47:09 +00001366 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
wdenk5653fc32004-02-08 22:55:38 +00001367 return rc;
1368 wp += info->portwidth;
1369 cnt -= info->portwidth;
Stefan Roesef0105722008-03-19 07:09:26 +01001370 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001371 /* Only check every once in a while */
1372 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1373 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001374 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001375#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001376
wdenk5653fc32004-02-08 22:55:38 +00001377 if (cnt == 0) {
1378 return (0);
1379 }
1380
1381 /*
1382 * handle unaligned tail bytes
1383 */
Ryan Harkin622b9522015-10-23 16:50:51 +01001384 cword.w32 = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001385 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001386 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
wdenkbf9e3b32004-02-12 00:47:09 +00001387 flash_add_byte (info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001388 --cnt;
1389 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001390 for (; i < info->portwidth; ++i)
1391 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001392
wdenkbf9e3b32004-02-12 00:47:09 +00001393 return flash_write_cfiword (info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +00001394}
1395
Stefan Roese20043a42012-12-06 15:44:09 +01001396static inline int manufact_match(flash_info_t *info, u32 manu)
1397{
1398 return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
1399}
1400
wdenk5653fc32004-02-08 22:55:38 +00001401/*-----------------------------------------------------------------------
1402 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001403#ifdef CONFIG_SYS_FLASH_PROTECTION
wdenk5653fc32004-02-08 22:55:38 +00001404
Holger Brunck81316a92012-08-09 10:22:41 +02001405static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1406{
Stefan Roese20043a42012-12-06 15:44:09 +01001407 if (manufact_match(info, INTEL_MANUFACT)
Kim Phillips11dc4012012-10-29 13:34:45 +00001408 && info->device_id == NUMONYX_256MBIT) {
Holger Brunck81316a92012-08-09 10:22:41 +02001409 /*
1410 * see errata called
1411 * "Numonyx Axcell P33/P30 Specification Update" :)
1412 */
1413 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1414 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1415 prot)) {
1416 /*
1417 * cmd must come before FLASH_CMD_PROTECT + 20us
1418 * Disable interrupts which might cause a timeout here.
1419 */
1420 int flag = disable_interrupts();
1421 unsigned short cmd;
1422
1423 if (prot)
1424 cmd = FLASH_CMD_PROTECT_SET;
1425 else
1426 cmd = FLASH_CMD_PROTECT_CLEAR;
1427 flash_write_cmd(info, sector, 0,
1428 FLASH_CMD_PROTECT);
1429 flash_write_cmd(info, sector, 0, cmd);
1430 /* re-enable interrupts if necessary */
1431 if (flag)
1432 enable_interrupts();
1433 }
1434 return 1;
1435 }
1436 return 0;
1437}
1438
wdenkbf9e3b32004-02-12 00:47:09 +00001439int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +00001440{
1441 int retcode = 0;
1442
Rafael Camposbc9019e2008-07-31 10:22:20 +02001443 switch (info->vendor) {
1444 case CFI_CMDSET_INTEL_PROG_REGIONS:
1445 case CFI_CMDSET_INTEL_STANDARD:
Nick Spence9e8e63c2008-08-19 22:21:16 -07001446 case CFI_CMDSET_INTEL_EXTENDED:
Holger Brunck81316a92012-08-09 10:22:41 +02001447 if (!cfi_protect_bugfix(info, sector, prot)) {
1448 flash_write_cmd(info, sector, 0,
1449 FLASH_CMD_CLEAR_STATUS);
1450 flash_write_cmd(info, sector, 0,
1451 FLASH_CMD_PROTECT);
Philippe De Muyter54652992010-08-17 18:40:25 +02001452 if (prot)
Holger Brunck81316a92012-08-09 10:22:41 +02001453 flash_write_cmd(info, sector, 0,
1454 FLASH_CMD_PROTECT_SET);
Philippe De Muyter54652992010-08-17 18:40:25 +02001455 else
Holger Brunck81316a92012-08-09 10:22:41 +02001456 flash_write_cmd(info, sector, 0,
1457 FLASH_CMD_PROTECT_CLEAR);
Philippe De Muyter54652992010-08-17 18:40:25 +02001458
Philippe De Muyter54652992010-08-17 18:40:25 +02001459 }
Rafael Camposbc9019e2008-07-31 10:22:20 +02001460 break;
1461 case CFI_CMDSET_AMD_EXTENDED:
1462 case CFI_CMDSET_AMD_STANDARD:
Rafael Camposbc9019e2008-07-31 10:22:20 +02001463 /* U-Boot only checks the first byte */
Stefan Roese20043a42012-12-06 15:44:09 +01001464 if (manufact_match(info, ATM_MANUFACT)) {
Rafael Camposbc9019e2008-07-31 10:22:20 +02001465 if (prot) {
1466 flash_unlock_seq (info, 0);
1467 flash_write_cmd (info, 0,
1468 info->addr_unlock1,
1469 ATM_CMD_SOFTLOCK_START);
1470 flash_unlock_seq (info, 0);
1471 flash_write_cmd (info, sector, 0,
1472 ATM_CMD_LOCK_SECT);
1473 } else {
1474 flash_write_cmd (info, 0,
1475 info->addr_unlock1,
1476 AMD_CMD_UNLOCK_START);
1477 if (info->device_id == ATM_ID_BV6416)
1478 flash_write_cmd (info, sector,
1479 0, ATM_CMD_UNLOCK_SECT);
1480 }
1481 }
Stefan Roeseac6b9112012-12-06 15:44:11 +01001482 if (info->legacy_unlock) {
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001483 int flag = disable_interrupts();
1484 int lock_flag;
1485
1486 flash_unlock_seq(info, 0);
1487 flash_write_cmd(info, 0, info->addr_unlock1,
1488 AMD_CMD_SET_PPB_ENTRY);
1489 lock_flag = flash_isset(info, sector, 0, 0x01);
1490 if (prot) {
1491 if (lock_flag) {
1492 flash_write_cmd(info, sector, 0,
1493 AMD_CMD_PPB_LOCK_BC1);
1494 flash_write_cmd(info, sector, 0,
1495 AMD_CMD_PPB_LOCK_BC2);
1496 }
1497 debug("sector %ld %slocked\n", sector,
1498 lock_flag ? "" : "already ");
1499 } else {
1500 if (!lock_flag) {
1501 debug("unlock %ld\n", sector);
1502 flash_write_cmd(info, 0, 0,
1503 AMD_CMD_PPB_UNLOCK_BC1);
1504 flash_write_cmd(info, 0, 0,
1505 AMD_CMD_PPB_UNLOCK_BC2);
1506 }
1507 debug("sector %ld %sunlocked\n", sector,
1508 !lock_flag ? "" : "already ");
1509 }
1510 if (flag)
1511 enable_interrupts();
1512
1513 if (flash_status_check(info, sector,
1514 info->erase_blk_tout,
1515 prot ? "protect" : "unprotect"))
1516 printf("status check error\n");
1517
1518 flash_write_cmd(info, 0, 0,
1519 AMD_CMD_SET_PPB_EXIT_BC1);
1520 flash_write_cmd(info, 0, 0,
1521 AMD_CMD_SET_PPB_EXIT_BC2);
1522 }
Rafael Camposbc9019e2008-07-31 10:22:20 +02001523 break;
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001524#ifdef CONFIG_FLASH_CFI_LEGACY
1525 case CFI_CMDSET_AMD_LEGACY:
1526 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1527 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1528 if (prot)
1529 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1530 else
1531 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1532#endif
Rafael Camposbc9019e2008-07-31 10:22:20 +02001533 };
wdenk5653fc32004-02-08 22:55:38 +00001534
Stefan Roesedf4e8132010-10-25 18:31:29 +02001535 /*
1536 * Flash needs to be in status register read mode for
1537 * flash_full_status_check() to work correctly
1538 */
1539 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
wdenkbf9e3b32004-02-12 00:47:09 +00001540 if ((retcode =
1541 flash_full_status_check (info, sector, info->erase_blk_tout,
1542 prot ? "protect" : "unprotect")) == 0) {
wdenk5653fc32004-02-08 22:55:38 +00001543
1544 info->protect[sector] = prot;
Stefan Roese2662b402006-04-01 13:41:03 +02001545
1546 /*
1547 * On some of Intel's flash chips (marked via legacy_unlock)
1548 * unprotect unprotects all locking.
1549 */
1550 if ((prot == 0) && (info->legacy_unlock)) {
wdenk5653fc32004-02-08 22:55:38 +00001551 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +00001552
1553 for (i = 0; i < info->sector_count; i++) {
1554 if (info->protect[i])
1555 flash_real_protect (info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +00001556 }
1557 }
1558 }
wdenk5653fc32004-02-08 22:55:38 +00001559 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +00001560}
1561
wdenk5653fc32004-02-08 22:55:38 +00001562/*-----------------------------------------------------------------------
1563 * flash_read_user_serial - read the OneTimeProgramming cells
1564 */
wdenkbf9e3b32004-02-12 00:47:09 +00001565void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1566 int len)
wdenk5653fc32004-02-08 22:55:38 +00001567{
wdenkbf9e3b32004-02-12 00:47:09 +00001568 uchar *src;
1569 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +00001570
1571 dst = buffer;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001572 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
wdenkbf9e3b32004-02-12 00:47:09 +00001573 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1574 memcpy (dst, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001575 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001576 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001577 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001578}
wdenkbf9e3b32004-02-12 00:47:09 +00001579
wdenk5653fc32004-02-08 22:55:38 +00001580/*
1581 * flash_read_factory_serial - read the device Id from the protection area
1582 */
wdenkbf9e3b32004-02-12 00:47:09 +00001583void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1584 int len)
wdenk5653fc32004-02-08 22:55:38 +00001585{
wdenkbf9e3b32004-02-12 00:47:09 +00001586 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +00001587
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001588 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
wdenkbf9e3b32004-02-12 00:47:09 +00001589 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1590 memcpy (buffer, src + offset, len);
Wolfgang Denkdb421e62005-09-25 16:41:22 +02001591 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001592 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001593 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001594}
1595
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001596#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00001597
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001598/*-----------------------------------------------------------------------
1599 * Reverse the order of the erase regions in the CFI QRY structure.
1600 * This is needed for chips that are either a) correctly detected as
1601 * top-boot, or b) buggy.
1602 */
1603static void cfi_reverse_geometry(struct cfi_qry *qry)
1604{
1605 unsigned int i, j;
1606 u32 tmp;
1607
1608 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
Andrew Gabbasovaedadf12013-05-14 12:27:52 -05001609 tmp = get_unaligned(&(qry->erase_region_info[i]));
1610 put_unaligned(get_unaligned(&(qry->erase_region_info[j])),
1611 &(qry->erase_region_info[i]));
1612 put_unaligned(tmp, &(qry->erase_region_info[j]));
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001613 }
1614}
wdenk5653fc32004-02-08 22:55:38 +00001615
1616/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +01001617 * read jedec ids from device and set corresponding fields in info struct
1618 *
1619 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1620 *
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001621 */
1622static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1623{
1624 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001625 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001626 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1627 udelay(1000); /* some flash are slow to respond */
1628 info->manufacturer_id = flash_read_uchar (info,
1629 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyterd77c7ac2010-08-10 16:54:52 +02001630 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1631 flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
1632 flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001633 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1634}
1635
1636static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1637{
1638 info->cmd_reset = FLASH_CMD_RESET;
1639
1640 cmdset_intel_read_jedec_ids(info);
1641 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1642
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001643#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001644 /* read legacy lock/unlock bit from intel flash */
1645 if (info->ext_addr) {
1646 info->legacy_unlock = flash_read_uchar (info,
1647 info->ext_addr + 5) & 0x08;
1648 }
1649#endif
1650
1651 return 0;
1652}
1653
1654static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1655{
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001656 ushort bankId = 0;
1657 uchar manuId;
1658
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001659 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1660 flash_unlock_seq(info, 0);
1661 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1662 udelay(1000); /* some flash are slow to respond */
Tor Krill90447ec2008-03-28 11:29:10 +01001663
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001664 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1665 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1666 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1667 bankId += 0x100;
1668 manuId = flash_read_uchar (info,
1669 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1670 }
1671 info->manufacturer_id = manuId;
Tor Krill90447ec2008-03-28 11:29:10 +01001672
1673 switch (info->chipwidth){
1674 case FLASH_CFI_8BIT:
1675 info->device_id = flash_read_uchar (info,
1676 FLASH_OFFSET_DEVICE_ID);
1677 if (info->device_id == 0x7E) {
1678 /* AMD 3-byte (expanded) device ids */
1679 info->device_id2 = flash_read_uchar (info,
1680 FLASH_OFFSET_DEVICE_ID2);
1681 info->device_id2 <<= 8;
1682 info->device_id2 |= flash_read_uchar (info,
1683 FLASH_OFFSET_DEVICE_ID3);
1684 }
1685 break;
1686 case FLASH_CFI_16BIT:
1687 info->device_id = flash_read_word (info,
1688 FLASH_OFFSET_DEVICE_ID);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001689 if ((info->device_id & 0xff) == 0x7E) {
1690 /* AMD 3-byte (expanded) device ids */
1691 info->device_id2 = flash_read_uchar (info,
1692 FLASH_OFFSET_DEVICE_ID2);
1693 info->device_id2 <<= 8;
1694 info->device_id2 |= flash_read_uchar (info,
1695 FLASH_OFFSET_DEVICE_ID3);
1696 }
Tor Krill90447ec2008-03-28 11:29:10 +01001697 break;
1698 default:
1699 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001700 }
1701 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001702 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001703}
1704
1705static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1706{
1707 info->cmd_reset = AMD_CMD_RESET;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01001708 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001709
1710 cmdset_amd_read_jedec_ids(info);
1711 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1712
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001713#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseac6b9112012-12-06 15:44:11 +01001714 if (info->ext_addr) {
1715 /* read sector protect/unprotect scheme (at 0x49) */
1716 if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001717 info->legacy_unlock = 1;
1718 }
1719#endif
1720
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001721 return 0;
1722}
1723
1724#ifdef CONFIG_FLASH_CFI_LEGACY
Stefan Roese260421a2006-11-13 13:55:24 +01001725static void flash_read_jedec_ids (flash_info_t * info)
1726{
1727 info->manufacturer_id = 0;
1728 info->device_id = 0;
1729 info->device_id2 = 0;
1730
1731 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001732 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese260421a2006-11-13 13:55:24 +01001733 case CFI_CMDSET_INTEL_STANDARD:
1734 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001735 cmdset_intel_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001736 break;
1737 case CFI_CMDSET_AMD_STANDARD:
1738 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001739 cmdset_amd_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001740 break;
1741 default:
1742 break;
1743 }
1744}
1745
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001746/*-----------------------------------------------------------------------
1747 * Call board code to request info about non-CFI flash.
1748 * board_flash_get_legacy needs to fill in at least:
1749 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1750 */
Becky Bruce09ce9922009-02-02 16:34:51 -06001751static int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001752{
1753 flash_info_t *info = &flash_info[banknum];
1754
1755 if (board_flash_get_legacy(base, banknum, info)) {
1756 /* board code may have filled info completely. If not, we
1757 use JEDEC ID probing. */
1758 if (!info->vendor) {
1759 int modes[] = {
1760 CFI_CMDSET_AMD_STANDARD,
1761 CFI_CMDSET_INTEL_STANDARD
1762 };
1763 int i;
1764
Axel Lin31bf0f52013-06-23 00:56:46 +08001765 for (i = 0; i < ARRAY_SIZE(modes); i++) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001766 info->vendor = modes[i];
Becky Bruce09ce9922009-02-02 16:34:51 -06001767 info->start[0] =
1768 (ulong)map_physmem(base,
Stefan Roesee1fb6d02009-02-05 11:44:52 +01001769 info->portwidth,
Becky Bruce09ce9922009-02-02 16:34:51 -06001770 MAP_NOCACHE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001771 if (info->portwidth == FLASH_CFI_8BIT
1772 && info->interface == FLASH_CFI_X8X16) {
1773 info->addr_unlock1 = 0x2AAA;
1774 info->addr_unlock2 = 0x5555;
1775 } else {
1776 info->addr_unlock1 = 0x5555;
1777 info->addr_unlock2 = 0x2AAA;
1778 }
1779 flash_read_jedec_ids(info);
1780 debug("JEDEC PROBE: ID %x %x %x\n",
1781 info->manufacturer_id,
1782 info->device_id,
1783 info->device_id2);
Becky Bruce09ce9922009-02-02 16:34:51 -06001784 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001785 break;
Becky Bruce09ce9922009-02-02 16:34:51 -06001786 else
Stefan Roesee1fb6d02009-02-05 11:44:52 +01001787 unmap_physmem((void *)info->start[0],
Kuo-Jung Sud8b57c02013-07-04 11:40:36 +08001788 info->portwidth);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001789 }
1790 }
1791
1792 switch(info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001793 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001794 case CFI_CMDSET_INTEL_STANDARD:
1795 case CFI_CMDSET_INTEL_EXTENDED:
1796 info->cmd_reset = FLASH_CMD_RESET;
1797 break;
1798 case CFI_CMDSET_AMD_STANDARD:
1799 case CFI_CMDSET_AMD_EXTENDED:
1800 case CFI_CMDSET_AMD_LEGACY:
1801 info->cmd_reset = AMD_CMD_RESET;
1802 break;
1803 }
1804 info->flash_id = FLASH_MAN_CFI;
1805 return 1;
1806 }
1807 return 0; /* use CFI */
1808}
1809#else
Becky Bruce09ce9922009-02-02 16:34:51 -06001810static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001811{
1812 return 0; /* use CFI */
1813}
1814#endif
1815
Stefan Roese260421a2006-11-13 13:55:24 +01001816/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001817 * detect if flash is compatible with the Common Flash Interface (CFI)
1818 * http://www.jedec.org/download/search/jesd68.pdf
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001819 */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001820static void flash_read_cfi (flash_info_t *info, void *buf,
1821 unsigned int start, size_t len)
1822{
1823 u8 *p = buf;
1824 unsigned int i;
1825
1826 for (i = 0; i < len; i++)
Stefan Roesee303be22013-04-12 19:04:54 +02001827 p[i] = flash_read_uchar(info, start + i);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001828}
1829
Kim Phillips11dc4012012-10-29 13:34:45 +00001830static void __flash_cmd_reset(flash_info_t *info)
Stefan Roesefa36ae72009-10-27 15:15:55 +01001831{
1832 /*
1833 * We do not yet know what kind of commandset to use, so we issue
1834 * the reset command in both Intel and AMD variants, in the hope
1835 * that AMD flash roms ignore the Intel command.
1836 */
1837 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001838 udelay(1);
Stefan Roesefa36ae72009-10-27 15:15:55 +01001839 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1840}
1841void flash_cmd_reset(flash_info_t *info)
1842 __attribute__((weak,alias("__flash_cmd_reset")));
1843
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001844static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
wdenk5653fc32004-02-08 22:55:38 +00001845{
Wolfgang Denk92eb7292006-12-27 01:26:13 +01001846 int cfi_offset;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001847
Stefan Roesee303be22013-04-12 19:04:54 +02001848 /* Issue FLASH reset command */
1849 flash_cmd_reset(info);
1850
Axel Lin31bf0f52013-06-23 00:56:46 +08001851 for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001852 cfi_offset++) {
1853 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1854 FLASH_CMD_CFI);
Stefan Roesee303be22013-04-12 19:04:54 +02001855 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1856 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1857 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001858 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1859 sizeof(struct cfi_qry));
1860 info->interface = le16_to_cpu(qry->interface_desc);
Stefan Roesee303be22013-04-12 19:04:54 +02001861
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001862 info->cfi_offset = flash_offset_cfi[cfi_offset];
1863 debug ("device interface is %d\n",
1864 info->interface);
1865 debug ("found port %d chip %d ",
1866 info->portwidth, info->chipwidth);
1867 debug ("port %d bits chip %d bits\n",
1868 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1869 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1870
1871 /* calculate command offsets as in the Linux driver */
Stefan Roesee303be22013-04-12 19:04:54 +02001872 info->addr_unlock1 = 0x555;
1873 info->addr_unlock2 = 0x2aa;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001874
1875 /*
1876 * modify the unlock address if we are
1877 * in compatibility mode
1878 */
1879 if ( /* x8/x16 in x8 mode */
1880 ((info->chipwidth == FLASH_CFI_BY8) &&
1881 (info->interface == FLASH_CFI_X8X16)) ||
1882 /* x16/x32 in x16 mode */
1883 ((info->chipwidth == FLASH_CFI_BY16) &&
1884 (info->interface == FLASH_CFI_X16X32)))
1885 {
1886 info->addr_unlock1 = 0xaaa;
1887 info->addr_unlock2 = 0x555;
1888 }
1889
1890 info->name = "CFI conformant";
1891 return 1;
1892 }
1893 }
1894
1895 return 0;
1896}
1897
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001898static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001899{
wdenkbf9e3b32004-02-12 00:47:09 +00001900 debug ("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +00001901
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001902 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenkbf9e3b32004-02-12 00:47:09 +00001903 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1904 for (info->chipwidth = FLASH_CFI_BY8;
1905 info->chipwidth <= info->portwidth;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001906 info->chipwidth <<= 1)
Stefan Roesee303be22013-04-12 19:04:54 +02001907 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001908 return 1;
wdenk5653fc32004-02-08 22:55:38 +00001909 }
wdenkbf9e3b32004-02-12 00:47:09 +00001910 debug ("not found\n");
wdenk5653fc32004-02-08 22:55:38 +00001911 return 0;
1912}
wdenkbf9e3b32004-02-12 00:47:09 +00001913
wdenk5653fc32004-02-08 22:55:38 +00001914/*
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001915 * Manufacturer-specific quirks. Add workarounds for geometry
1916 * reversal, etc. here.
1917 */
1918static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1919{
1920 /* check if flash geometry needs reversal */
1921 if (qry->num_erase_regions > 1) {
1922 /* reverse geometry if top boot part */
1923 if (info->cfi_version < 0x3131) {
1924 /* CFI < 1.1, try to guess from device id */
1925 if ((info->device_id & 0x80) != 0)
1926 cfi_reverse_geometry(qry);
Stefan Roesee303be22013-04-12 19:04:54 +02001927 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001928 /* CFI >= 1.1, deduct from top/bottom flag */
1929 /* note: ext_addr is valid since cfi_version > 0 */
1930 cfi_reverse_geometry(qry);
1931 }
1932 }
1933}
1934
1935static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1936{
1937 int reverse_geometry = 0;
1938
1939 /* Check the "top boot" bit in the PRI */
1940 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1941 reverse_geometry = 1;
1942
1943 /* AT49BV6416(T) list the erase regions in the wrong order.
1944 * However, the device ID is identical with the non-broken
Ulf Samuelssoncb82a532009-03-27 23:26:43 +01001945 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001946 */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001947 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1948 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001949
1950 if (reverse_geometry)
1951 cfi_reverse_geometry(qry);
1952}
1953
Richard Retanubune8eac432009-01-14 08:44:26 -05001954static void flash_fixup_stm(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) {
Mike Frysinger6a011ce2011-04-10 16:06:29 -04001960 /* CFI < 1.1, guess by device id */
1961 if (info->device_id == 0x22CA || /* M29W320DT */
1962 info->device_id == 0x2256 || /* M29W320ET */
1963 info->device_id == 0x22D7) { /* M29W800DT */
Richard Retanubune8eac432009-01-14 08:44:26 -05001964 cfi_reverse_geometry(qry);
1965 }
Mike Frysinger4c2105c2011-05-09 18:33:36 -04001966 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1967 /* CFI >= 1.1, deduct from top/bottom flag */
1968 /* note: ext_addr is valid since cfi_version > 0 */
1969 cfi_reverse_geometry(qry);
Richard Retanubune8eac432009-01-14 08:44:26 -05001970 }
1971 }
1972}
1973
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01001974static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
1975{
1976 /*
1977 * SST, for many recent nor parallel flashes, says they are
1978 * CFI-conformant. This is not true, since qry struct.
1979 * reports a std. AMD command set (0x0002), while SST allows to
1980 * erase two different sector sizes for the same memory.
1981 * 64KB sector (SST call it block) needs 0x30 to be erased.
1982 * 4KB sector (SST call it sector) needs 0x50 to be erased.
1983 * Since CFI query detect the 4KB number of sectors, users expects
1984 * a sector granularity of 4KB, and it is here set.
1985 */
1986 if (info->device_id == 0x5D23 || /* SST39VF3201B */
1987 info->device_id == 0x5C23) { /* SST39VF3202B */
1988 /* set sector granularity to 4KB */
1989 info->cmd_erase_sector=0x50;
1990 }
1991}
1992
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05301993static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry)
1994{
1995 /*
1996 * The M29EW devices seem to report the CFI information wrong
1997 * when it's in 8 bit mode.
1998 * There's an app note from Numonyx on this issue.
1999 * So adjust the buffer size for M29EW while operating in 8-bit mode
2000 */
2001 if (((qry->max_buf_write_size) > 0x8) &&
2002 (info->device_id == 0x7E) &&
2003 (info->device_id2 == 0x2201 ||
2004 info->device_id2 == 0x2301 ||
2005 info->device_id2 == 0x2801 ||
2006 info->device_id2 == 0x4801)) {
2007 debug("Adjusted buffer size on Numonyx flash"
2008 " M29EW family in 8 bit mode\n");
2009 qry->max_buf_write_size = 0x8;
2010 }
2011}
2012
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002013/*
wdenk5653fc32004-02-08 22:55:38 +00002014 * The following code cannot be run from FLASH!
2015 *
2016 */
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002017ulong flash_get_size (phys_addr_t base, int banknum)
wdenk5653fc32004-02-08 22:55:38 +00002018{
wdenkbf9e3b32004-02-12 00:47:09 +00002019 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +00002020 int i, j;
2021 flash_sect_t sect_cnt;
Becky Bruce09ce9922009-02-02 16:34:51 -06002022 phys_addr_t sector;
wdenk5653fc32004-02-08 22:55:38 +00002023 unsigned long tmp;
2024 int size_ratio;
2025 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +00002026 int erase_region_size;
2027 int erase_region_count;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002028 struct cfi_qry qry;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002029 unsigned long max_size;
Stefan Roese260421a2006-11-13 13:55:24 +01002030
Kumar Galaf9796902008-05-15 15:13:08 -05002031 memset(&qry, 0, sizeof(qry));
2032
Stefan Roese260421a2006-11-13 13:55:24 +01002033 info->ext_addr = 0;
2034 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002035#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +02002036 info->legacy_unlock = 0;
2037#endif
wdenk5653fc32004-02-08 22:55:38 +00002038
Becky Bruce09ce9922009-02-02 16:34:51 -06002039 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002040
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002041 if (flash_detect_cfi (info, &qry)) {
Andrew Gabbasovaedadf12013-05-14 12:27:52 -05002042 info->vendor = le16_to_cpu(get_unaligned(&(qry.p_id)));
2043 info->ext_addr = le16_to_cpu(get_unaligned(&(qry.p_adr)));
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002044 num_erase_regions = qry.num_erase_regions;
2045
Stefan Roese260421a2006-11-13 13:55:24 +01002046 if (info->ext_addr) {
2047 info->cfi_version = (ushort) flash_read_uchar (info,
Stefan Roesee303be22013-04-12 19:04:54 +02002048 info->ext_addr + 3) << 8;
Stefan Roese260421a2006-11-13 13:55:24 +01002049 info->cfi_version |= (ushort) flash_read_uchar (info,
Stefan Roesee303be22013-04-12 19:04:54 +02002050 info->ext_addr + 4);
Stefan Roese260421a2006-11-13 13:55:24 +01002051 }
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002052
wdenkbf9e3b32004-02-12 00:47:09 +00002053#ifdef DEBUG
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002054 flash_printqry (&qry);
wdenkbf9e3b32004-02-12 00:47:09 +00002055#endif
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002056
wdenkbf9e3b32004-02-12 00:47:09 +00002057 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002058 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00002059 case CFI_CMDSET_INTEL_STANDARD:
2060 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002061 cmdset_intel_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002062 break;
2063 case CFI_CMDSET_AMD_STANDARD:
2064 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002065 cmdset_amd_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002066 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002067 default:
2068 printf("CFI: Unknown command set 0x%x\n",
2069 info->vendor);
2070 /*
2071 * Unfortunately, this means we don't know how
2072 * to get the chip back to Read mode. Might
2073 * as well try an Intel-style reset...
2074 */
2075 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2076 return 0;
wdenk5653fc32004-02-08 22:55:38 +00002077 }
wdenkcd37d9e2004-02-10 00:03:41 +00002078
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002079 /* Do manufacturer-specific fixups */
2080 switch (info->manufacturer_id) {
Mario Schuknecht2c9f48a2011-02-21 13:13:14 +01002081 case 0x0001: /* AMD */
2082 case 0x0037: /* AMIC */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002083 flash_fixup_amd(info, &qry);
2084 break;
2085 case 0x001f:
2086 flash_fixup_atmel(info, &qry);
2087 break;
Richard Retanubune8eac432009-01-14 08:44:26 -05002088 case 0x0020:
2089 flash_fixup_stm(info, &qry);
2090 break;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002091 case 0x00bf: /* SST */
2092 flash_fixup_sst(info, &qry);
2093 break;
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302094 case 0x0089: /* Numonyx */
2095 flash_fixup_num(info, &qry);
2096 break;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002097 }
2098
wdenkbf9e3b32004-02-12 00:47:09 +00002099 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese260421a2006-11-13 13:55:24 +01002100 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
2101 debug ("device id is 0x%x\n", info->device_id);
2102 debug ("device id2 is 0x%x\n", info->device_id2);
2103 debug ("cfi version is 0x%04x\n", info->cfi_version);
2104
wdenk5653fc32004-02-08 22:55:38 +00002105 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00002106 /* if the chip is x8/x16 reduce the ratio by half */
2107 if ((info->interface == FLASH_CFI_X8X16)
2108 && (info->chipwidth == FLASH_CFI_BY8)) {
2109 size_ratio >>= 1;
2110 }
wdenkbf9e3b32004-02-12 00:47:09 +00002111 debug ("size_ratio %d port %d bits chip %d bits\n",
2112 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2113 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002114 info->size = 1 << qry.dev_size;
2115 /* multiply the size by the number of chips */
2116 info->size *= size_ratio;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002117 max_size = cfi_flash_bank_size(banknum);
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002118 if (max_size && (info->size > max_size)) {
2119 debug("[truncated from %ldMiB]", info->size >> 20);
2120 info->size = max_size;
2121 }
wdenkbf9e3b32004-02-12 00:47:09 +00002122 debug ("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00002123 sect_cnt = 0;
2124 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00002125 for (i = 0; i < num_erase_regions; i++) {
2126 if (i > NUM_ERASE_REGIONS) {
wdenk028ab6b2004-02-23 23:54:43 +00002127 printf ("%d erase regions found, only %d used\n",
2128 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00002129 break;
2130 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002131
Andrew Gabbasovaedadf12013-05-14 12:27:52 -05002132 tmp = le32_to_cpu(get_unaligned(
2133 &(qry.erase_region_info[i])));
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002134 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002135
2136 erase_region_count = (tmp & 0xffff) + 1;
2137 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00002138 erase_region_size =
2139 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenk4c0d4c32004-06-09 17:34:58 +00002140 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenk028ab6b2004-02-23 23:54:43 +00002141 erase_region_count, erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00002142 for (j = 0; j < erase_region_count; j++) {
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002143 if (sector - base >= info->size)
2144 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002145 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002146 printf("ERROR: too many flash sectors\n");
2147 break;
2148 }
Becky Bruce09ce9922009-02-02 16:34:51 -06002149 info->start[sect_cnt] =
2150 (ulong)map_physmem(sector,
2151 info->portwidth,
2152 MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002153 sector += (erase_region_size * size_ratio);
wdenka1191902005-01-09 17:12:27 +00002154
2155 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002156 * Only read protection status from
2157 * supported devices (intel...)
wdenka1191902005-01-09 17:12:27 +00002158 */
2159 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002160 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenka1191902005-01-09 17:12:27 +00002161 case CFI_CMDSET_INTEL_EXTENDED:
2162 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesedf4e8132010-10-25 18:31:29 +02002163 /*
2164 * Set flash to read-id mode. Otherwise
2165 * reading protected status is not
2166 * guaranteed.
2167 */
2168 flash_write_cmd(info, sect_cnt, 0,
2169 FLASH_CMD_READ_ID);
wdenka1191902005-01-09 17:12:27 +00002170 info->protect[sect_cnt] =
2171 flash_isset (info, sect_cnt,
2172 FLASH_OFFSET_PROTECT,
2173 FLASH_STATUS_PROTECT);
2174 break;
Stefan Roese03deff42012-12-06 15:44:10 +01002175 case CFI_CMDSET_AMD_EXTENDED:
2176 case CFI_CMDSET_AMD_STANDARD:
Stefan Roeseac6b9112012-12-06 15:44:11 +01002177 if (!info->legacy_unlock) {
Stefan Roese03deff42012-12-06 15:44:10 +01002178 /* default: not protected */
2179 info->protect[sect_cnt] = 0;
2180 break;
2181 }
2182
2183 /* Read protection (PPB) from sector */
2184 flash_write_cmd(info, 0, 0,
2185 info->cmd_reset);
2186 flash_unlock_seq(info, 0);
2187 flash_write_cmd(info, 0,
2188 info->addr_unlock1,
2189 FLASH_CMD_READ_ID);
2190 info->protect[sect_cnt] =
2191 flash_isset(
2192 info, sect_cnt,
2193 FLASH_OFFSET_PROTECT,
2194 FLASH_STATUS_PROTECT);
2195 break;
wdenka1191902005-01-09 17:12:27 +00002196 default:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002197 /* default: not protected */
2198 info->protect[sect_cnt] = 0;
wdenka1191902005-01-09 17:12:27 +00002199 }
2200
wdenk5653fc32004-02-08 22:55:38 +00002201 sect_cnt++;
2202 }
2203 }
2204
2205 info->sector_count = sect_cnt;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002206 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2207 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002208 info->erase_blk_tout = tmp *
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002209 (1 << qry.block_erase_timeout_max);
2210 tmp = (1 << qry.buf_write_timeout_typ) *
2211 (1 << qry.buf_write_timeout_max);
2212
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002213 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002214 info->buffer_write_tout = (tmp + 999) / 1000;
2215 tmp = (1 << qry.word_write_timeout_typ) *
2216 (1 << qry.word_write_timeout_max);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002217 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002218 info->write_tout = (tmp + 999) / 1000;
wdenk5653fc32004-02-08 22:55:38 +00002219 info->flash_id = FLASH_MAN_CFI;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002220 if ((info->interface == FLASH_CFI_X8X16) &&
2221 (info->chipwidth == FLASH_CFI_BY8)) {
2222 /* XXX - Need to test on x8/x16 in parallel. */
2223 info->portwidth >>= 1;
wdenk855a4962004-03-14 18:23:55 +00002224 }
Mike Frysinger22159872008-10-02 01:55:38 -04002225
2226 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +00002227 }
2228
wdenkbf9e3b32004-02-12 00:47:09 +00002229 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00002230}
2231
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002232#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002233void flash_set_verbose(uint v)
2234{
2235 flash_verbose = v;
2236}
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002237#endif
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002238
Stefan Roese6f726f92010-10-25 18:31:48 +02002239static void cfi_flash_set_config_reg(u32 base, u16 val)
2240{
2241#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2242 /*
2243 * Only set this config register if really defined
2244 * to a valid value (0xffff is invalid)
2245 */
2246 if (val == 0xffff)
2247 return;
2248
2249 /*
2250 * Set configuration register. Data is "encrypted" in the 16 lower
2251 * address bits.
2252 */
2253 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2254 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2255
2256 /*
2257 * Finally issue reset-command to bring device back to
2258 * read-array mode
2259 */
2260 flash_write16(FLASH_CMD_RESET, (void *)base);
2261#endif
2262}
2263
wdenk5653fc32004-02-08 22:55:38 +00002264/*-----------------------------------------------------------------------
2265 */
Heiko Schocher6ee14162011-04-04 08:10:21 +02002266
2267void flash_protect_default(void)
2268{
Peter Tyser2c519832011-04-13 11:46:56 -05002269#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2270 int i;
2271 struct apl_s {
2272 ulong start;
2273 ulong size;
2274 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2275#endif
2276
Heiko Schocher6ee14162011-04-04 08:10:21 +02002277 /* Monitor protection ON by default */
2278#if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2279 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2280 flash_protect(FLAG_PROTECT_SET,
2281 CONFIG_SYS_MONITOR_BASE,
2282 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2283 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2284#endif
2285
2286 /* Environment protection ON by default */
2287#ifdef CONFIG_ENV_IS_IN_FLASH
2288 flash_protect(FLAG_PROTECT_SET,
2289 CONFIG_ENV_ADDR,
2290 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2291 flash_get_info(CONFIG_ENV_ADDR));
2292#endif
2293
2294 /* Redundant environment protection ON by default */
2295#ifdef CONFIG_ENV_ADDR_REDUND
2296 flash_protect(FLAG_PROTECT_SET,
2297 CONFIG_ENV_ADDR_REDUND,
2298 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2299 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2300#endif
2301
2302#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
Axel Lin31bf0f52013-06-23 00:56:46 +08002303 for (i = 0; i < ARRAY_SIZE(apl); i++) {
Marek Vasut31d34142011-10-21 14:17:05 +00002304 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocher6ee14162011-04-04 08:10:21 +02002305 apl[i].start, apl[i].start + apl[i].size - 1);
2306 flash_protect(FLAG_PROTECT_SET,
2307 apl[i].start,
2308 apl[i].start + apl[i].size - 1,
2309 flash_get_info(apl[i].start));
2310 }
2311#endif
2312}
2313
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002314unsigned long flash_init (void)
wdenk5653fc32004-02-08 22:55:38 +00002315{
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002316 unsigned long size = 0;
2317 int i;
wdenk5653fc32004-02-08 22:55:38 +00002318
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002319#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann3a3baf32009-03-21 09:59:34 -04002320 /* read environment from EEPROM */
2321 char s[64];
Wolfgang Denkcdb74972010-07-24 21:55:43 +02002322 getenv_f("unlock", s, sizeof(s));
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002323#endif
wdenk5653fc32004-02-08 22:55:38 +00002324
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002325 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002326 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002327 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk5653fc32004-02-08 22:55:38 +00002328
Stefan Roese6f726f92010-10-25 18:31:48 +02002329 /* Optionally write flash configuration register */
2330 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2331 cfi_flash_config_reg(i));
2332
Stefan Roeseb00e19c2010-08-30 10:11:51 +02002333 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002334 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002335 size += flash_info[i].size;
2336 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002337#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Peter Tysereddf52b2010-12-28 18:12:05 -06002338 printf ("## Unknown flash on Bank %d "
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002339 "- Size = 0x%08lx = %ld MB\n",
2340 i+1, flash_info[i].size,
John Schmoller0e3fa012010-09-29 13:49:05 -05002341 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002342#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
wdenk5653fc32004-02-08 22:55:38 +00002343 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002344#ifdef CONFIG_SYS_FLASH_PROTECTION
Jeroen Hofsteec15df212014-06-17 22:47:31 +02002345 else if (strcmp(s, "yes") == 0) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002346 /*
2347 * Only the U-Boot image and it's environment
2348 * is protected, all other sectors are
2349 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002350 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002351 * and the environment variable "unlock" is
2352 * set to "yes".
2353 */
2354 if (flash_info[i].legacy_unlock) {
2355 int k;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002356
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002357 /*
2358 * Disable legacy_unlock temporarily,
2359 * since flash_real_protect would
2360 * relock all other sectors again
2361 * otherwise.
2362 */
2363 flash_info[i].legacy_unlock = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002364
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002365 /*
2366 * Legacy unlocking (e.g. Intel J3) ->
2367 * unlock only one sector. This will
2368 * unlock all sectors.
2369 */
2370 flash_real_protect (&flash_info[i], 0, 0);
Stefan Roese79b4cda2006-02-28 15:29:58 +01002371
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002372 flash_info[i].legacy_unlock = 1;
2373
2374 /*
2375 * Manually mark other sectors as
2376 * unlocked (unprotected)
2377 */
2378 for (k = 1; k < flash_info[i].sector_count; k++)
2379 flash_info[i].protect[k] = 0;
2380 } else {
2381 /*
2382 * No legancy unlocking -> unlock all sectors
2383 */
2384 flash_protect (FLAG_PROTECT_CLEAR,
2385 flash_info[i].start[0],
2386 flash_info[i].start[0]
2387 + flash_info[i].size - 1,
2388 &flash_info[i]);
2389 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01002390 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002391#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00002392 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002393
Heiko Schocher6ee14162011-04-04 08:10:21 +02002394 flash_protect_default();
Piotr Ziecik91809ed2008-11-17 15:57:58 +01002395#ifdef CONFIG_FLASH_CFI_MTD
2396 cfi_mtd_init();
2397#endif
2398
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002399 return (size);
wdenk5653fc32004-02-08 22:55:38 +00002400}