blob: 50983b837b2238b781477f13b559502047b1ad6e [file] [log] [blame]
wdenk2cefd152004-02-08 22:55:38 +00001/*
wdenke65527f2004-02-12 00:47:09 +00002 * (C) Copyright 2002-2004
wdenk2cefd152004-02-08 22:55:38 +00003 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4 *
5 * Copyright (C) 2003 Arabella Software Ltd.
6 * Yuli Barcohen <yuli@arabellasw.com>
wdenk2cefd152004-02-08 22:55:38 +00007 *
wdenke65527f2004-02-12 00:47:09 +00008 * Copyright (C) 2004
9 * Ed Okerson
Stefan Roese12797482006-11-13 13:55:24 +010010 *
11 * Copyright (C) 2006
12 * Tolunay Orkun <listmember@orkun.us>
wdenke65527f2004-02-12 00:47:09 +000013 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
wdenk2cefd152004-02-08 22:55:38 +000015 */
16
17/* The DEBUG define must be before common to enable debugging */
wdenk2ebee312004-02-23 19:30:57 +000018/* #define DEBUG */
19
wdenk2cefd152004-02-08 22:55:38 +000020#include <common.h>
21#include <asm/processor.h>
Haiying Wangc123a382007-02-21 16:52:31 +010022#include <asm/io.h>
wdenkaeba06f2004-06-09 17:34:58 +000023#include <asm/byteorder.h>
Andrew Gabbasovc1592582013-05-14 12:27:52 -050024#include <asm/unaligned.h>
wdenkd0245fc2005-04-13 10:02:42 +000025#include <environment.h>
Stefan Roese6e83e342009-10-27 15:15:55 +010026#include <mtd/cfi_flash.h>
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +010027#include <watchdog.h>
wdenke537b3b2004-02-23 23:54:43 +000028
wdenk2cefd152004-02-08 22:55:38 +000029/*
Haavard Skinnemoend523e392007-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.
wdenk2cefd152004-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 Roese12797482006-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
wdenk2cefd152004-02-08 22:55:38 +000045 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocher800db312007-01-19 18:05:26 +010047 * reading and writing ... (yes there is such a Hardware).
wdenk2cefd152004-02-08 22:55:38 +000048 */
49
Haavard Skinnemoend523e392007-12-13 12:56:28 +010050static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysingerc2c093d2010-12-22 09:41:13 -050051#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +010052static uint flash_verbose = 1;
Mike Frysingerc2c093d2010-12-22 09:41:13 -050053#else
54#define flash_verbose 1
55#endif
Wolfgang Denkafa0dd02006-12-27 01:26:13 +010056
Wolfgang Denk9f5fb0f2008-08-08 16:39:54 +020057flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
58
Stefan Roesec865e6c2006-02-28 15:29:58 +010059/*
60 * Check if chip width is defined. If not, start detecting with 8bit.
61 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020062#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
63#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roesec865e6c2006-02-28 15:29:58 +010064#endif
65
Jeroen Hofstee4f517e62014-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 Roeseab935642010-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 Roesefb9a7302010-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 Hofstee4f517e62014-10-08 22:57:23 +020090__weak phys_addr_t cfi_flash_bank_addr(int i)
Stefan Roese7e7dda82010-08-30 10:11:51 +020091{
92 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
93}
Stefan Roese7e7dda82010-08-30 10:11:51 +020094
Jeroen Hofstee4f517e62014-10-08 22:57:23 +020095__weak unsigned long cfi_flash_bank_size(int i)
Ilya Yanok755c1802010-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 Yanok755c1802010-10-21 17:20:12 +0200103
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200104__maybe_weak void flash_write8(u8 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100105{
106 __raw_writeb(value, addr);
107}
108
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200109__maybe_weak void flash_write16(u16 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100110{
111 __raw_writew(value, addr);
112}
113
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200114__maybe_weak void flash_write32(u32 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100115{
116 __raw_writel(value, addr);
117}
118
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200119__maybe_weak void flash_write64(u64 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100120{
121 /* No architectures currently implement __raw_writeq() */
122 *(volatile u64 *)addr = value;
123}
124
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200125__maybe_weak u8 flash_read8(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100126{
127 return __raw_readb(addr);
128}
129
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200130__maybe_weak u16 flash_read16(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100131{
132 return __raw_readw(addr);
133}
134
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200135__maybe_weak u32 flash_read32(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100136{
137 return __raw_readl(addr);
138}
139
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200140__maybe_weak u64 flash_read64(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100141{
142 /* No architectures currently implement __raw_readq() */
143 return *(volatile u64 *)addr;
144}
145
wdenk2cefd152004-02-08 22:55:38 +0000146/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000147 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-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 Schocher4c0f0052009-02-10 09:53:29 +0100149flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200150{
151 int i;
Masahiro Yamadac663b9a2013-05-17 14:50:37 +0900152 flash_info_t *info;
wdenk2cefd152004-02-08 22:55:38 +0000153
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200154 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Masahiro Yamada44049f32013-05-17 14:50:36 +0900155 info = &flash_info[i];
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200156 if (info->size && info->start[0] <= base &&
157 base <= info->start[0] + info->size - 1)
Masahiro Yamadac663b9a2013-05-17 14:50:37 +0900158 return info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200159 }
wdenk2cefd152004-02-08 22:55:38 +0000160
Masahiro Yamadac663b9a2013-05-17 14:50:37 +0900161 return NULL;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200162}
wdenk2cefd152004-02-08 22:55:38 +0000163#endif
164
Haavard Skinnemoene3ea1882007-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
wdenke65527f2004-02-12 00:47:09 +0000173/*-----------------------------------------------------------------------
174 * create an address based on the offset and the port width
175 */
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100176static inline void *
177flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000178{
Stefan Roese70a90b72013-04-12 19:04:54 +0200179 unsigned int byte_offset = offset * info->portwidth;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100180
Stefan Roese70a90b72013-04-12 19:04:54 +0200181 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoene3ea1882007-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{
wdenke65527f2004-02-12 00:47:09 +0000187}
188
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200189/*-----------------------------------------------------------------------
190 * make a proper sized command based on the port and chip widths
191 */
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200192static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200193{
194 int i;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400195 int cword_offset;
196 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200197#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200198 u32 cmd_le = cpu_to_le32(cmd);
199#endif
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400200 uchar val;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200201 uchar *cp = (uchar *) cmdbuf;
202
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400203 for (i = info->portwidth; i > 0; i--){
204 cword_offset = (info->portwidth-i)%info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200205#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400206 cp_offset = info->portwidth - i;
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200207 val = *((uchar*)&cmd_le + cword_offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200208#else
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400209 cp_offset = i - 1;
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200210 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200211#endif
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200212 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400213 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200214}
215
wdenk2cefd152004-02-08 22:55:38 +0000216#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000217/*-----------------------------------------------------------------------
218 * Debug support
219 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100220static void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000221{
222 int i;
223 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000224
Wolfgang Denk49f4f6a2009-02-04 09:42:20 +0100225 cp = (char *) &data;
wdenke65527f2004-02-12 00:47:09 +0000226 for (i = 0; i < 8; i++)
227 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000228}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200229
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100230static void flash_printqry (struct cfi_qry *qry)
wdenke65527f2004-02-12 00:47:09 +0000231{
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100232 u8 *p = (u8 *)qry;
wdenke65527f2004-02-12 00:47:09 +0000233 int x, y;
234
Haavard Skinnemoen53baec72007-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(" ");
wdenke65527f2004-02-12 00:47:09 +0000240 for (y = 0; y < 16; y++) {
Haavard Skinnemoen53baec72007-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(".");
wdenke65527f2004-02-12 00:47:09 +0000246 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100247 debug("\n");
wdenke65527f2004-02-12 00:47:09 +0000248 }
249}
wdenk2cefd152004-02-08 22:55:38 +0000250#endif
251
252
253/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000254 * read a character at a port width address
255 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100256static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000257{
258 uchar *cp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100259 uchar retval;
wdenke65527f2004-02-12 00:47:09 +0000260
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100261 cp = flash_map (info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200262#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100263 retval = flash_read8(cp);
wdenke65527f2004-02-12 00:47:09 +0000264#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100265 retval = flash_read8(cp + info->portwidth - 1);
wdenke65527f2004-02-12 00:47:09 +0000266#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100267 flash_unmap (info, 0, offset, cp);
268 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000269}
270
271/*-----------------------------------------------------------------------
Tor Krill7f2a3052008-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 Roese12797482006-11-13 13:55:24 +0100286 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000287 * port size word. Swap for ppc format.
288 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100289static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
290 uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000291{
wdenke65527f2004-02-12 00:47:09 +0000292 uchar *addr;
293 ulong retval;
294
295#ifdef DEBUG
296 int x;
297#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100298 addr = flash_map (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000299
wdenke65527f2004-02-12 00:47:09 +0000300#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 Skinnemoene3ea1882007-12-13 12:56:34 +0100304 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenke65527f2004-02-12 00:47:09 +0000305 }
306#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200307#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-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));
wdenke65527f2004-02-12 00:47:09 +0000312#else
Haavard Skinnemoene3ea1882007-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)));
wdenke65527f2004-02-12 00:47:09 +0000317#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100318 flash_unmap(info, sect, offset, addr);
319
wdenke65527f2004-02-12 00:47:09 +0000320 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000321}
322
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200323/*
324 * Write a proper sized command to the correct address
Michael Schwingen73d044d2007-12-07 23:35:02 +0100325 */
Stefan Roese6e83e342009-10-27 15:15:55 +0100326void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
327 uint offset, u32 cmd)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100328{
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100329
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100330 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200331 cfiword_t cword;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100332
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100333 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200334 flash_make_cmd (info, cmd, &cword);
335 switch (info->portwidth) {
336 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100337 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200338 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100339 flash_write8(cword.c, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200340 break;
341 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100342 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200343 cmd, cword.w,
344 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100345 flash_write16(cword.w, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200346 break;
347 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100348 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200349 cmd, cword.l,
350 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100351 flash_write32(cword.l, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200352 break;
353 case FLASH_CFI_64BIT:
354#ifdef DEBUG
355 {
356 char str[20];
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100357
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200358 print_longlong (str, cword.ll);
359
360 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100361 addr, cmd, str,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200362 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100363 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200364#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100365 flash_write64(cword.ll, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200366 break;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100367 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200368
369 /* Ensure all the instructions are fully finished */
370 sync();
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100371
372 flash_unmap(info, sect, offset, addr);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100373}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200374
375static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100376{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200377 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);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100379}
Michael Schwingen73d044d2007-12-07 23:35:02 +0100380
381/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000382 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200383static int flash_isequal (flash_info_t * info, flash_sect_t sect,
384 uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000385{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100386 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200387 cfiword_t cword;
388 int retval;
wdenk2cefd152004-02-08 22:55:38 +0000389
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100390 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200391 flash_make_cmd (info, cmd, &cword);
Stefan Roeseefef95b2006-04-01 13:41:03 +0200392
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100393 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200394 switch (info->portwidth) {
395 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100396 debug ("is= %x %x\n", flash_read8(addr), cword.c);
397 retval = (flash_read8(addr) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200398 break;
399 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100400 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
401 retval = (flash_read16(addr) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200402 break;
403 case FLASH_CFI_32BIT:
Andrew Klossner7ddfafc2008-08-21 07:12:26 -0700404 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100405 retval = (flash_read32(addr) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200406 break;
407 case FLASH_CFI_64BIT:
408#ifdef DEBUG
409 {
410 char str1[20];
411 char str2[20];
Michael Schwingen73d044d2007-12-07 23:35:02 +0100412
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100413 print_longlong (str1, flash_read64(addr));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200414 print_longlong (str2, cword.ll);
415 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +0000416 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200417#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100418 retval = (flash_read64(addr) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200419 break;
420 default:
421 retval = 0;
422 break;
423 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100424 flash_unmap(info, sect, offset, addr);
425
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200426 return retval;
427}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200428
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200429/*-----------------------------------------------------------------------
430 */
431static int flash_isset (flash_info_t * info, flash_sect_t sect,
432 uint offset, uchar cmd)
433{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100434 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200435 cfiword_t cword;
436 int retval;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200437
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100438 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200439 flash_make_cmd (info, cmd, &cword);
440 switch (info->portwidth) {
441 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100442 retval = ((flash_read8(addr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200443 break;
444 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100445 retval = ((flash_read16(addr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200446 break;
447 case FLASH_CFI_32BIT:
Stefan Roesed4e37c02008-01-02 14:05:37 +0100448 retval = ((flash_read32(addr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200449 break;
450 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100451 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200452 break;
453 default:
454 retval = 0;
455 break;
456 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100457 flash_unmap(info, sect, offset, addr);
458
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200459 return retval;
460}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200461
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200462/*-----------------------------------------------------------------------
463 */
464static int flash_toggle (flash_info_t * info, flash_sect_t sect,
465 uint offset, uchar cmd)
466{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100467 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200468 cfiword_t cword;
469 int retval;
wdenke85b7a52004-10-10 22:16:06 +0000470
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100471 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200472 flash_make_cmd (info, cmd, &cword);
473 switch (info->portwidth) {
474 case FLASH_CFI_8BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200475 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200476 break;
477 case FLASH_CFI_16BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200478 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200479 break;
480 case FLASH_CFI_32BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200481 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200482 break;
483 case FLASH_CFI_64BIT:
Wolfgang Denk600e1832008-10-31 01:12:28 +0100484 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
485 (flash_read32(addr+4) != flash_read32(addr+4)) );
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200486 break;
487 default:
488 retval = 0;
489 break;
490 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100491 flash_unmap(info, sect, offset, addr);
492
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200493 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000494}
495
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200496/*
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.
wdenk2cefd152004-02-08 22:55:38 +0000501 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200502static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5c71a7a2005-05-16 15:23:22 +0000503{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200504 int retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000505
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200506 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400507 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-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:
514#ifdef CONFIG_FLASH_CFI_LEGACY
515 case CFI_CMDSET_AMD_LEGACY:
516#endif
517 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
518 break;
519 default:
520 retval = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000521 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200522 debug ("flash_is_busy: %d\n", retval);
523 return retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000524}
525
526/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200527 * 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.
wdenk5c71a7a2005-05-16 15:23:22 +0000529 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200530static int flash_status_check (flash_info_t * info, flash_sect_t sector,
531 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000532{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200533 ulong start;
wdenk2cefd152004-02-08 22:55:38 +0000534
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200535#if CONFIG_SYS_HZ != 1000
Renato Andreolaac6693d2010-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 Skinnemoen6dd763c2007-10-06 18:55:36 +0200540#endif
wdenk2cefd152004-02-08 22:55:38 +0000541
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200542 /* Wait for command completion */
Graeme Russ13ec42b2011-07-15 02:18:56 +0000543#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou4b7e6682010-04-01 11:15:05 +0800544 reset_timer();
Graeme Russ13ec42b2011-07-15 02:18:56 +0000545#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200546 start = get_timer (0);
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +0100547 WATCHDOG_RESET();
Haavard Skinnemoen6dd763c2007-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 Roese70a90b72013-04-12 19:04:54 +0200554 udelay(1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200555 return ERR_TIMOUT;
wdenk2cefd152004-02-08 22:55:38 +0000556 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200557 udelay (1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000558 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200559 return ERR_OK;
560}
wdenk2cefd152004-02-08 22:55:38 +0000561
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200562/*-----------------------------------------------------------------------
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;
wdenk2cefd152004-02-08 22:55:38 +0000572
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200573 retcode = flash_status_check (info, sector, tout, prompt);
574 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400575 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200576 case CFI_CMDSET_INTEL_EXTENDED:
577 case CFI_CMDSET_INTEL_STANDARD:
Baruch Siach9e2712d2014-09-04 12:23:09 +0300578 if ((retcode == ERR_OK)
Haavard Skinnemoen6dd763c2007-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");
wdenk2cefd152004-02-08 22:55:38 +0000593 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200594 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");
wdenk2cefd152004-02-08 22:55:38 +0000600 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200601 flash_write_cmd (info, sector, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -0700602 udelay(1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200603 break;
604 default:
605 break;
wdenk2cefd152004-02-08 22:55:38 +0000606 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200607 return retcode;
wdenk2cefd152004-02-08 22:55:38 +0000608}
609
Thomas Chou076767a2010-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 Russ13ec42b2011-07-15 02:18:56 +0000635#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou4b7e6682010-04-01 11:15:05 +0800636 reset_timer();
Graeme Russ13ec42b2011-07-15 02:18:56 +0000637#endif
Thomas Chou076767a2010-03-26 08:17:00 +0800638 start = get_timer(0);
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +0100639 WATCHDOG_RESET();
Thomas Chou076767a2010-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
wdenk2cefd152004-02-08 22:55:38 +0000671/*-----------------------------------------------------------------------
672 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200673static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000674{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200675#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200676 unsigned short w;
677 unsigned int l;
678 unsigned long long ll;
679#endif
wdenk2cefd152004-02-08 22:55:38 +0000680
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200681 switch (info->portwidth) {
682 case FLASH_CFI_8BIT:
683 cword->c = c;
684 break;
685 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200686#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200687 w = c;
688 w <<= 8;
689 cword->w = (cword->w >> 8) | w;
690#else
691 cword->w = (cword->w << 8) | c;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100692#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200693 break;
694 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200695#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200696 l = c;
697 l <<= 24;
698 cword->l = (cword->l >> 8) | l;
699#else
700 cword->l = (cword->l << 8) | c;
701#endif
702 break;
703 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200704#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200705 ll = c;
706 ll <<= 56;
707 cword->ll = (cword->ll >> 8) | ll;
708#else
709 cword->ll = (cword->ll << 8) | c;
710#endif
711 break;
Stefan Roese12797482006-11-13 13:55:24 +0100712 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200713}
wdenk2cefd152004-02-08 22:55:38 +0000714
Jens Gehrlein8ee4add2008-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.
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200718 */
719static flash_sect_t find_sector (flash_info_t * info, ulong addr)
720{
Kim Phillipsd303b862012-10-29 13:34:45 +0000721 static flash_sect_t saved_sector; /* previously found sector */
Stefan Roese70a90b72013-04-12 19:04:54 +0200722 static flash_info_t *saved_info; /* previously used flash bank */
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100723 flash_sect_t sector = saved_sector;
724
Stefan Roese70a90b72013-04-12 19:04:54 +0200725 if ((info != saved_info) || (sector >= info->sector_count))
726 sector = 0;
727
Jens Gehrlein8ee4add2008-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--;
wdenk2cefd152004-02-08 22:55:38 +0000737
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100738 saved_sector = sector;
Stefan Roese70a90b72013-04-12 19:04:54 +0200739 saved_info = info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200740 return sector;
wdenk2cefd152004-02-08 22:55:38 +0000741}
742
743/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000744 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200745static int flash_write_cfiword (flash_info_t * info, ulong dest,
746 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +0000747{
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600748 void *dstaddr = (void *)dest;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200749 int flag;
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100750 flash_sect_t sect = 0;
751 char sect_found = 0;
wdenk2cefd152004-02-08 22:55:38 +0000752
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200753 /* Check if Flash is (sufficiently) erased */
754 switch (info->portwidth) {
755 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100756 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200757 break;
758 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100759 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200760 break;
761 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100762 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200763 break;
764 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100765 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200766 break;
767 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100768 flag = 0;
769 break;
wdenk2cefd152004-02-08 22:55:38 +0000770 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600771 if (!flag)
Stefan Roese707c1462007-12-27 07:50:54 +0100772 return ERR_NOT_ERASED;
wdenk2cefd152004-02-08 22:55:38 +0000773
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200774 /* Disable interrupts which might cause a timeout here */
775 flag = disable_interrupts ();
Stefan Roesec865e6c2006-02-28 15:29:58 +0100776
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200777 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400778 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-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 Swarthout2da14102008-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 Gehrlein1e814cf2008-12-16 17:25:54 +0100789 sect_found = 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200790 break;
Po-Yu Chuang3f483402009-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
wdenk2cefd152004-02-08 22:55:38 +0000799 }
800
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200801 switch (info->portwidth) {
802 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100803 flash_write8(cword.c, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200804 break;
805 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100806 flash_write16(cword.w, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200807 break;
808 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100809 flash_write32(cword.l, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200810 break;
811 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100812 flash_write64(cword.ll, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200813 break;
wdenk2cefd152004-02-08 22:55:38 +0000814 }
815
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200816 /* re-enable interrupts if necessary */
817 if (flag)
818 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +0000819
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100820 if (!sect_found)
821 sect = find_sector (info, dest);
822
Thomas Chou076767a2010-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");
wdenk2cefd152004-02-08 22:55:38 +0000829}
830
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200831#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenk2cefd152004-02-08 22:55:38 +0000832
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200833static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
834 int len)
wdenk2cefd152004-02-08 22:55:38 +0000835{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200836 flash_sect_t sector;
837 int cnt;
838 int retcode;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100839 void *src = cp;
Stefan Roese42c6ace2009-02-05 11:25:57 +0100840 void *dst = (void *)dest;
Stefan Roese707c1462007-12-27 07:50:54 +0100841 void *dst2 = dst;
Tao Houdd3b4552012-03-15 23:33:58 +0800842 int flag = 1;
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200843 uint offset = 0;
844 unsigned int shift;
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400845 uchar write_cmd;
Stefan Roese707c1462007-12-27 07:50:54 +0100846
847 switch (info->portwidth) {
848 case FLASH_CFI_8BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200849 shift = 0;
Stefan Roese707c1462007-12-27 07:50:54 +0100850 break;
851 case FLASH_CFI_16BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200852 shift = 1;
Stefan Roese707c1462007-12-27 07:50:54 +0100853 break;
854 case FLASH_CFI_32BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200855 shift = 2;
Stefan Roese707c1462007-12-27 07:50:54 +0100856 break;
857 case FLASH_CFI_64BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200858 shift = 3;
Stefan Roese707c1462007-12-27 07:50:54 +0100859 break;
860 default:
861 retcode = ERR_INVAL;
862 goto out_unmap;
863 }
864
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200865 cnt = len >> shift;
866
Tao Houdd3b4552012-03-15 23:33:58 +0800867 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese707c1462007-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 }
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100895
Stefan Roese707c1462007-12-27 07:50:54 +0100896 src = cp;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100897 sector = find_sector (info, dest);
wdenke65527f2004-02-12 00:47:09 +0000898
899 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400900 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +0000901 case CFI_CMDSET_INTEL_STANDARD:
902 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko7d1794c2008-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 Skinnemoen6dd763c2007-10-06 18:55:36 +0200905 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
Vasiliy Leoenenko7d1794c2008-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 Skinnemoen6dd763c2007-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 Liakhovetski183284f2008-04-03 13:36:02 +0200914 cnt = len >> shift;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400915 flash_write_cmd (info, sector, 0, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200916 while (cnt-- > 0) {
917 switch (info->portwidth) {
918 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100919 flash_write8(flash_read8(src), dst);
920 src += 1, dst += 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200921 break;
922 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100923 flash_write16(flash_read16(src), dst);
924 src += 2, dst += 2;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200925 break;
926 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100927 flash_write32(flash_read32(src), dst);
928 src += 4, dst += 4;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200929 break;
930 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100931 flash_write64(flash_read64(src), dst);
932 src += 8, dst += 8;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200933 break;
934 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100935 retcode = ERR_INVAL;
936 goto out_unmap;
Haavard Skinnemoen6dd763c2007-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 Skinnemoene3ea1882007-12-13 12:56:34 +0100945
946 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200947
wdenk2cefd152004-02-08 22:55:38 +0000948 case CFI_CMDSET_AMD_STANDARD:
949 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200950 flash_unlock_seq(info,0);
Guennadi Liakhovetski183284f2008-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 Schmolleree355882009-08-12 10:55:47 -0500957 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200958
959 switch (info->portwidth) {
960 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100961 while (cnt-- > 0) {
962 flash_write8(flash_read8(src), dst);
963 src += 1, dst += 1;
964 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200965 break;
966 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100967 while (cnt-- > 0) {
968 flash_write16(flash_read16(src), dst);
969 src += 2, dst += 2;
970 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200971 break;
972 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100973 while (cnt-- > 0) {
974 flash_write32(flash_read32(src), dst);
975 src += 4, dst += 4;
976 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200977 break;
978 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100979 while (cnt-- > 0) {
980 flash_write64(flash_read64(src), dst);
981 src += 8, dst += 8;
982 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200983 break;
984 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100985 retcode = ERR_INVAL;
986 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200987 }
988
989 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Chou076767a2010-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 Skinnemoene3ea1882007-12-13 12:56:34 +0100999 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001000
wdenk2cefd152004-02-08 22:55:38 +00001001 default:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001002 debug ("Unknown Command Set\n");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001003 retcode = ERR_INVAL;
1004 break;
wdenk2cefd152004-02-08 22:55:38 +00001005 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001006
1007out_unmap:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001008 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001009}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001010#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001011
wdenke65527f2004-02-12 00:47:09 +00001012
wdenk2cefd152004-02-08 22:55:38 +00001013/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +00001014 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001015int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +00001016{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001017 int rcode = 0;
1018 int prot;
1019 flash_sect_t sect;
Thomas Chou076767a2010-03-26 08:17:00 +08001020 int st;
wdenk2cefd152004-02-08 22:55:38 +00001021
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001022 if (info->flash_id != FLASH_MAN_CFI) {
1023 puts ("Can't erase unknown flash type - aborted\n");
1024 return 1;
1025 }
1026 if ((s_first < 0) || (s_first > s_last)) {
1027 puts ("- no sectors to erase\n");
1028 return 1;
1029 }
Stefan Roeseefef95b2006-04-01 13:41:03 +02001030
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001031 prot = 0;
1032 for (sect = s_first; sect <= s_last; ++sect) {
1033 if (info->protect[sect]) {
1034 prot++;
wdenk2cefd152004-02-08 22:55:38 +00001035 }
1036 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001037 if (prot) {
1038 printf ("- Warning: %d protected sectors will not be erased!\n",
1039 prot);
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001040 } else if (flash_verbose) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001041 putc ('\n');
1042 }
wdenke65527f2004-02-12 00:47:09 +00001043
wdenke65527f2004-02-12 00:47:09 +00001044
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001045 for (sect = s_first; sect <= s_last; sect++) {
Joe Hershberger497c32f2012-08-17 15:36:41 -05001046 if (ctrlc()) {
1047 printf("\n");
1048 return 1;
1049 }
1050
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001051 if (info->protect[sect] == 0) { /* not protected */
Joe Hershberger7f3c2112012-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
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001078 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001079 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001080 case CFI_CMDSET_INTEL_STANDARD:
1081 case CFI_CMDSET_INTEL_EXTENDED:
1082 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);
1088 break;
1089 case CFI_CMDSET_AMD_STANDARD:
1090 case CFI_CMDSET_AMD_EXTENDED:
1091 flash_unlock_seq (info, sect);
1092 flash_write_cmd (info, sect,
1093 info->addr_unlock1,
1094 AMD_CMD_ERASE_START);
1095 flash_unlock_seq (info, sect);
1096 flash_write_cmd (info, sect, 0,
Angelo Dureghello7ba30282012-12-01 01:14:18 +01001097 info->cmd_erase_sector);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001098 break;
1099#ifdef CONFIG_FLASH_CFI_LEGACY
1100 case CFI_CMDSET_AMD_LEGACY:
1101 flash_unlock_seq (info, 0);
1102 flash_write_cmd (info, 0, info->addr_unlock1,
1103 AMD_CMD_ERASE_START);
1104 flash_unlock_seq (info, 0);
1105 flash_write_cmd (info, sect, 0,
1106 AMD_CMD_ERASE_SECTOR);
1107 break;
1108#endif
1109 default:
1110 debug ("Unkown flash vendor %d\n",
1111 info->vendor);
1112 break;
wdenke65527f2004-02-12 00:47:09 +00001113 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001114
Thomas Chou076767a2010-03-26 08:17:00 +08001115 if (use_flash_status_poll(info)) {
Kim Phillipsd303b862012-10-29 13:34:45 +00001116 cfiword_t cword;
Thomas Chou076767a2010-03-26 08:17:00 +08001117 void *dest;
Kim Phillipsd303b862012-10-29 13:34:45 +00001118 cword.ll = 0xffffffffffffffffULL;
Thomas Chou076767a2010-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)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001128 rcode = 1;
Thomas Chou076767a2010-03-26 08:17:00 +08001129 else if (flash_verbose)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001130 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +00001131 }
wdenk2cefd152004-02-08 22:55:38 +00001132 }
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001133
1134 if (flash_verbose)
1135 puts (" done\n");
1136
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001137 return rcode;
wdenk2cefd152004-02-08 22:55:38 +00001138}
wdenke65527f2004-02-12 00:47:09 +00001139
Stefan Roeseb6f508d2010-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 Roesea9153f22010-10-25 18:31:39 +02001145 u32 *flash;
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001146
1147 /*
1148 * Check if whole sector is erased
1149 */
1150 size = flash_sector_size(info, i);
Stefan Roesea9153f22010-10-25 18:31:39 +02001151 flash = (u32 *)info->start[i];
Stefan Roeseb6f508d2010-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 Roesea9153f22010-10-25 18:31:39 +02001156 if (flash_read32(flash++) != 0xffffffff)
Stefan Roeseb6f508d2010-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
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001164void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001165{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001166 int i;
wdenk369d43d2004-03-14 14:09:05 +00001167
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001168 if (info->flash_id != FLASH_MAN_CFI) {
1169 puts ("missing or unknown FLASH type\n");
1170 return;
1171 }
1172
Peter Tyser0b5a2632010-12-28 18:12:05 -06001173 printf ("%s flash (%d x %d)",
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001174 info->name,
1175 (info->portwidth << 3), (info->chipwidth << 3));
1176 if (info->size < 1024*1024)
1177 printf (" Size: %ld kB in %d Sectors\n",
1178 info->size >> 10, info->sector_count);
1179 else
1180 printf (" Size: %ld MB in %d Sectors\n",
1181 info->size >> 20, info->sector_count);
1182 printf (" ");
1183 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001184 case CFI_CMDSET_INTEL_PROG_REGIONS:
1185 printf ("Intel Prog Regions");
1186 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001187 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;
1199#ifdef CONFIG_FLASH_CFI_LEGACY
1200 case CFI_CMDSET_AMD_LEGACY:
1201 printf ("AMD Legacy");
1202 break;
wdenk369d43d2004-03-14 14:09:05 +00001203#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001204 default:
1205 printf ("Unknown (%d)", info->vendor);
1206 break;
wdenk2cefd152004-02-08 22:55:38 +00001207 }
Philippe De Muyter35bc0812010-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 Schocher27cea502011-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);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001215 }
Stefan Roesee442a902012-12-06 15:44:12 +01001216 if ((info->vendor == CFI_CMDSET_AMD_STANDARD) && (info->legacy_unlock))
1217 printf("\n Advanced Sector Protection (PPB) enabled");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001218 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1219 info->erase_blk_tout,
1220 info->write_tout);
1221 if (info->buffer_size > 1) {
1222 printf (" Buffer write timeout: %ld ms, "
1223 "buffer size: %d bytes\n",
1224 info->buffer_write_tout,
1225 info->buffer_size);
1226 }
wdenk2cefd152004-02-08 22:55:38 +00001227
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001228 puts ("\n Sector Start Addresses:");
1229 for (i = 0; i < info->sector_count; ++i) {
Kim Phillipsc8836f12010-07-26 18:35:39 -05001230 if (ctrlc())
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001231 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001232 if ((i % 5) == 0)
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001233 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001234#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001235 /* print empty and read-only info */
1236 printf (" %08lX %c %s ",
1237 info->start[i],
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001238 sector_erased(info, i) ? 'E' : ' ',
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001239 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001240#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001241 printf (" %08lX %s ",
1242 info->start[i],
1243 info->protect[i] ? "RO" : " ");
wdenke65527f2004-02-12 00:47:09 +00001244#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001245 }
1246 putc ('\n');
1247 return;
wdenk2cefd152004-02-08 22:55:38 +00001248}
1249
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001250/*-----------------------------------------------------------------------
Jerry Van Barenaae73572008-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 Roese7758c162008-03-19 07:09:26 +01001256#ifdef CONFIG_FLASH_SHOW_PROGRESS
1257#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik2a7493c2008-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 Barenaae73572008-03-08 13:48:01 -05001268 }
Stefan Roese7758c162008-03-19 07:09:26 +01001269#else
1270#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1271#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001272
1273/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001274 * Copy memory to flash, returns:
1275 * 0 - OK
1276 * 1 - write timeout
1277 * 2 - Flash not erased
wdenk2cefd152004-02-08 22:55:38 +00001278 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001279int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +00001280{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001281 ulong wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001282 uchar *p;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001283 int aln;
wdenk2cefd152004-02-08 22:55:38 +00001284 cfiword_t cword;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001285 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001286#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001287 int buffered_size;
wdenk2cefd152004-02-08 22:55:38 +00001288#endif
Jerry Van Barenaae73572008-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
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001303 /* get lower aligned address */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001304 wp = (addr & ~(info->portwidth - 1));
Haiying Wangc123a382007-02-21 16:52:31 +01001305
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001306 /* handle unaligned start */
1307 if ((aln = addr - wp) != 0) {
1308 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001309 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001310 for (i = 0; i < aln; ++i)
1311 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk2cefd152004-02-08 22:55:38 +00001312
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001313 for (; (i < info->portwidth) && (cnt > 0); i++) {
1314 flash_add_byte (info, &cword, *src++);
1315 cnt--;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001316 }
Haavard Skinnemoene3ea1882007-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 Skinnemoene3ea1882007-12-13 12:56:34 +01001321 if (rc != 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001322 return rc;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001323
1324 wp += i;
Stefan Roese7758c162008-03-19 07:09:26 +01001325 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001326 }
1327
1328 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001329#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001330 buffered_size = (info->portwidth / info->chipwidth);
1331 buffered_size *= info->buffer_size;
1332 while (cnt >= info->portwidth) {
1333 /* prohibit buffer write when buffer_size is 1 */
1334 if (info->buffer_size == 1) {
1335 cword.l = 0;
1336 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;
1349 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1350 return rc;
1351 i -= i & (info->portwidth - 1);
1352 wp += i;
1353 src += i;
1354 cnt -= i;
Stefan Roese7758c162008-03-19 07:09:26 +01001355 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Joe Hershberger497c32f2012-08-17 15:36:41 -05001356 /* Only check every once in a while */
1357 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1358 return ERR_ABORTED;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001359 }
1360#else
1361 while (cnt >= info->portwidth) {
1362 cword.l = 0;
1363 for (i = 0; i < info->portwidth; i++) {
1364 flash_add_byte (info, &cword, *src++);
1365 }
1366 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1367 return rc;
1368 wp += info->portwidth;
1369 cnt -= info->portwidth;
Stefan Roese7758c162008-03-19 07:09:26 +01001370 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Joe Hershberger497c32f2012-08-17 15:36:41 -05001371 /* Only check every once in a while */
1372 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1373 return ERR_ABORTED;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001374 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001375#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Barenaae73572008-03-08 13:48:01 -05001376
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001377 if (cnt == 0) {
1378 return (0);
1379 }
1380
1381 /*
1382 * handle unaligned tail bytes
1383 */
1384 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001385 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001386 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001387 flash_add_byte (info, &cword, *src++);
1388 --cnt;
1389 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001390 for (; i < info->portwidth; ++i)
1391 flash_add_byte (info, &cword, flash_read8(p + i));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001392
1393 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +00001394}
wdenke65527f2004-02-12 00:47:09 +00001395
Stefan Roese92b1bca2012-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
wdenk2cefd152004-02-08 22:55:38 +00001401/*-----------------------------------------------------------------------
1402 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001403#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001404
Holger Brunck94c302d2012-08-09 10:22:41 +02001405static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1406{
Stefan Roese92b1bca2012-12-06 15:44:09 +01001407 if (manufact_match(info, INTEL_MANUFACT)
Kim Phillipsd303b862012-10-29 13:34:45 +00001408 && info->device_id == NUMONYX_256MBIT) {
Holger Brunck94c302d2012-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
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001439int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +00001440{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001441 int retcode = 0;
wdenke65527f2004-02-12 00:47:09 +00001442
Rafael Campos13d2b612008-07-31 10:22:20 +02001443 switch (info->vendor) {
1444 case CFI_CMDSET_INTEL_PROG_REGIONS:
1445 case CFI_CMDSET_INTEL_STANDARD:
Nick Spenceec81b472008-08-19 22:21:16 -07001446 case CFI_CMDSET_INTEL_EXTENDED:
Holger Brunck94c302d2012-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 Muyterca6cd162010-08-17 18:40:25 +02001452 if (prot)
Holger Brunck94c302d2012-08-09 10:22:41 +02001453 flash_write_cmd(info, sector, 0,
1454 FLASH_CMD_PROTECT_SET);
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001455 else
Holger Brunck94c302d2012-08-09 10:22:41 +02001456 flash_write_cmd(info, sector, 0,
1457 FLASH_CMD_PROTECT_CLEAR);
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001458
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001459 }
Rafael Campos13d2b612008-07-31 10:22:20 +02001460 break;
1461 case CFI_CMDSET_AMD_EXTENDED:
1462 case CFI_CMDSET_AMD_STANDARD:
Rafael Campos13d2b612008-07-31 10:22:20 +02001463 /* U-Boot only checks the first byte */
Stefan Roese92b1bca2012-12-06 15:44:09 +01001464 if (manufact_match(info, ATM_MANUFACT)) {
Rafael Campos13d2b612008-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 Roese7de65842012-12-06 15:44:11 +01001482 if (info->legacy_unlock) {
Anatolij Gustschin041b04f2012-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 Campos13d2b612008-07-31 10:22:20 +02001523 break;
TsiChung Liewb8c19292008-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 Campos13d2b612008-07-31 10:22:20 +02001533 };
wdenk2cefd152004-02-08 22:55:38 +00001534
Stefan Roese5215df12010-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);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001540 if ((retcode =
1541 flash_full_status_check (info, sector, info->erase_blk_tout,
1542 prot ? "protect" : "unprotect")) == 0) {
wdenke65527f2004-02-12 00:47:09 +00001543
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001544 info->protect[sector] = prot;
1545
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)) {
1551 flash_sect_t i;
1552
1553 for (i = 0; i < info->sector_count; i++) {
1554 if (info->protect[i])
1555 flash_real_protect (info, i, 1);
1556 }
wdenk2cefd152004-02-08 22:55:38 +00001557 }
wdenk2cefd152004-02-08 22:55:38 +00001558 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001559 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001560}
wdenke65527f2004-02-12 00:47:09 +00001561
wdenk2cefd152004-02-08 22:55:38 +00001562/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001563 * flash_read_user_serial - read the OneTimeProgramming cells
wdenk2cefd152004-02-08 22:55:38 +00001564 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001565void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1566 int len)
wdenk2cefd152004-02-08 22:55:38 +00001567{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001568 uchar *src;
1569 uchar *dst;
wdenke65527f2004-02-12 00:47:09 +00001570
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001571 dst = buffer;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001572 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001573 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1574 memcpy (dst, src + offset, len);
1575 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001576 udelay(1);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001577 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001578}
1579
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001580/*
1581 * flash_read_factory_serial - read the device Id from the protection area
wdenk2cefd152004-02-08 22:55:38 +00001582 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001583void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1584 int len)
wdenk2cefd152004-02-08 22:55:38 +00001585{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001586 uchar *src;
wdenke65527f2004-02-12 00:47:09 +00001587
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001588 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001589 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1590 memcpy (buffer, src + offset, len);
1591 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001592 udelay(1);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001593 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001594}
1595
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001596#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001597
Haavard Skinnemoenc4d478b2007-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 Gabbasovc1592582013-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 Skinnemoenc4d478b2007-12-14 15:36:17 +01001613 }
1614}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001615
wdenk2cefd152004-02-08 22:55:38 +00001616/*-----------------------------------------------------------------------
Stefan Roese12797482006-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 Skinnemoenc4d478b2007-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 Williamsffcef2f2011-04-12 00:59:04 -07001625 udelay(1);
Haavard Skinnemoenc4d478b2007-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 Muyter35bc0812010-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 Skinnemoenc4d478b2007-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-VILLARD03836942008-10-16 15:01:15 +02001643#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoenc4d478b2007-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 Gigerf447f712009-07-22 17:13:24 +02001656 ushort bankId = 0;
1657 uchar manuId;
1658
Haavard Skinnemoenc4d478b2007-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 Krill7f2a3052008-03-28 11:29:10 +01001663
Niklaus Gigerf447f712009-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 Krill7f2a3052008-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 Schocher27cea502011-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 Krill7f2a3052008-03-28 11:29:10 +01001697 break;
1698 default:
1699 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001700 }
1701 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001702 udelay(1);
Haavard Skinnemoenc4d478b2007-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 Dureghello7ba30282012-12-01 01:14:18 +01001708 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
Haavard Skinnemoenc4d478b2007-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 Gustschin041b04f2012-08-09 08:18:12 +02001713#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roese7de65842012-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 Gustschin041b04f2012-08-09 08:18:12 +02001717 info->legacy_unlock = 1;
1718 }
1719#endif
1720
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001721 return 0;
1722}
1723
1724#ifdef CONFIG_FLASH_CFI_LEGACY
Stefan Roese12797482006-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 Leoenenko7d1794c2008-05-07 21:25:33 +04001732 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese12797482006-11-13 13:55:24 +01001733 case CFI_CMDSET_INTEL_STANDARD:
1734 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001735 cmdset_intel_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001736 break;
1737 case CFI_CMDSET_AMD_STANDARD:
1738 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001739 cmdset_amd_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001740 break;
1741 default:
1742 break;
1743 }
1744}
1745
1746/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001747 * 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.
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001750 */
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001751static int flash_detect_legacy(phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001752{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001753 flash_info_t *info = &flash_info[banknum];
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001754
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001755 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;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001764
Axel Lin85706c82013-06-23 00:56:46 +08001765 for (i = 0; i < ARRAY_SIZE(modes); i++) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001766 info->vendor = modes[i];
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001767 info->start[0] =
1768 (ulong)map_physmem(base,
Stefan Roeseb8443702009-02-05 11:44:52 +01001769 info->portwidth,
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001770 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-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 Bruce9d1f6af2009-02-02 16:34:51 -06001784 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001785 break;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001786 else
Stefan Roeseb8443702009-02-05 11:44:52 +01001787 unmap_physmem((void *)info->start[0],
Kuo-Jung Su7a939fe2013-07-04 11:40:36 +08001788 info->portwidth);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001789 }
1790 }
1791
1792 switch(info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001793 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-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 Bruce9d1f6af2009-02-02 16:34:51 -06001810static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001811{
1812 return 0; /* use CFI */
1813}
1814#endif
1815
1816/*-----------------------------------------------------------------------
1817 * detect if flash is compatible with the Common Flash Interface (CFI)
1818 * http://www.jedec.org/download/search/jesd68.pdf
1819 */
Haavard Skinnemoen53baec72007-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 Roese70a90b72013-04-12 19:04:54 +02001827 p[i] = flash_read_uchar(info, start + i);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001828}
Stefan Roese6e83e342009-10-27 15:15:55 +01001829
Kim Phillipsd303b862012-10-29 13:34:45 +00001830static void __flash_cmd_reset(flash_info_t *info)
Stefan Roese6e83e342009-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 Williamsffcef2f2011-04-12 00:59:04 -07001838 udelay(1);
Stefan Roese6e83e342009-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")));
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001843
1844static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001845{
1846 int cfi_offset;
1847
Stefan Roese70a90b72013-04-12 19:04:54 +02001848 /* Issue FLASH reset command */
1849 flash_cmd_reset(info);
1850
Axel Lin85706c82013-06-23 00:56:46 +08001851 for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001852 cfi_offset++) {
1853 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1854 FLASH_CMD_CFI);
Stefan Roese70a90b72013-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 Skinnemoen53baec72007-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 Roese70a90b72013-04-12 19:04:54 +02001861
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001862 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 Roese70a90b72013-04-12 19:04:54 +02001872 info->addr_unlock1 = 0x555;
1873 info->addr_unlock2 = 0x2aa;
Haavard Skinnemoend523e392007-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 Skinnemoen53baec72007-12-14 15:36:16 +01001898static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001899{
wdenke65527f2004-02-12 00:47:09 +00001900 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001901
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001902 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenke65527f2004-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 Skinnemoend523e392007-12-13 12:56:28 +01001906 info->chipwidth <<= 1)
Stefan Roese70a90b72013-04-12 19:04:54 +02001907 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001908 return 1;
wdenk2cefd152004-02-08 22:55:38 +00001909 }
wdenke65527f2004-02-12 00:47:09 +00001910 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001911 return 0;
1912}
wdenke65527f2004-02-12 00:47:09 +00001913
wdenk2cefd152004-02-08 22:55:38 +00001914/*
Haavard Skinnemoen750ea7f2007-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 Roese70a90b72013-04-12 19:04:54 +02001927 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
Haavard Skinnemoen750ea7f2007-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 Samuelsson07f9b4e2009-03-27 23:26:43 +01001945 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001946 */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001947 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1948 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001949
1950 if (reverse_geometry)
1951 cfi_reverse_geometry(qry);
1952}
1953
Richard Retanubunbe4dca22009-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 Frysinger02a37862011-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 Retanubunbe4dca22009-01-14 08:44:26 -05001964 cfi_reverse_geometry(qry);
1965 }
Mike Frysinger97dd8992011-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 Retanubunbe4dca22009-01-14 08:44:26 -05001970 }
1971 }
1972}
1973
Angelo Dureghello7ba30282012-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 Teki130def32013-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 Skinnemoen750ea7f2007-12-14 15:36:18 +01002013/*
wdenk2cefd152004-02-08 22:55:38 +00002014 * The following code cannot be run from FLASH!
2015 *
2016 */
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002017ulong flash_get_size (phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00002018{
wdenke65527f2004-02-12 00:47:09 +00002019 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00002020 int i, j;
2021 flash_sect_t sect_cnt;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002022 phys_addr_t sector;
wdenk2cefd152004-02-08 22:55:38 +00002023 unsigned long tmp;
2024 int size_ratio;
2025 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00002026 int erase_region_size;
2027 int erase_region_count;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002028 struct cfi_qry qry;
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002029 unsigned long max_size;
Stefan Roese12797482006-11-13 13:55:24 +01002030
Kumar Gala899032b2008-05-15 15:13:08 -05002031 memset(&qry, 0, sizeof(qry));
2032
Stefan Roese12797482006-11-13 13:55:24 +01002033 info->ext_addr = 0;
2034 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002035#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02002036 info->legacy_unlock = 0;
2037#endif
wdenk2cefd152004-02-08 22:55:38 +00002038
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002039 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00002040
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002041 if (flash_detect_cfi (info, &qry)) {
Andrew Gabbasovc1592582013-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 Skinnemoen53baec72007-12-14 15:36:16 +01002044 num_erase_regions = qry.num_erase_regions;
2045
Stefan Roese12797482006-11-13 13:55:24 +01002046 if (info->ext_addr) {
2047 info->cfi_version = (ushort) flash_read_uchar (info,
Stefan Roese70a90b72013-04-12 19:04:54 +02002048 info->ext_addr + 3) << 8;
Stefan Roese12797482006-11-13 13:55:24 +01002049 info->cfi_version |= (ushort) flash_read_uchar (info,
Stefan Roese70a90b72013-04-12 19:04:54 +02002050 info->ext_addr + 4);
Stefan Roese12797482006-11-13 13:55:24 +01002051 }
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002052
wdenke65527f2004-02-12 00:47:09 +00002053#ifdef DEBUG
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002054 flash_printqry (&qry);
wdenke65527f2004-02-12 00:47:09 +00002055#endif
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002056
wdenke65527f2004-02-12 00:47:09 +00002057 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04002058 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +00002059 case CFI_CMDSET_INTEL_STANDARD:
2060 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002061 cmdset_intel_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00002062 break;
2063 case CFI_CMDSET_AMD_STANDARD:
2064 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002065 cmdset_amd_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00002066 break;
Haavard Skinnemoenc4d478b2007-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;
wdenk2cefd152004-02-08 22:55:38 +00002077 }
wdenk6cfa84e2004-02-10 00:03:41 +00002078
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002079 /* Do manufacturer-specific fixups */
2080 switch (info->manufacturer_id) {
Mario Schuknecht5c3579e2011-02-21 13:13:14 +01002081 case 0x0001: /* AMD */
2082 case 0x0037: /* AMIC */
Haavard Skinnemoen750ea7f2007-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 Retanubunbe4dca22009-01-14 08:44:26 -05002088 case 0x0020:
2089 flash_fixup_stm(info, &qry);
2090 break;
Angelo Dureghello7ba30282012-12-01 01:14:18 +01002091 case 0x00bf: /* SST */
2092 flash_fixup_sst(info, &qry);
2093 break;
Jagannadha Sutradharudu Teki130def32013-03-01 16:54:26 +05302094 case 0x0089: /* Numonyx */
2095 flash_fixup_num(info, &qry);
2096 break;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002097 }
2098
wdenke65527f2004-02-12 00:47:09 +00002099 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese12797482006-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
wdenk2cefd152004-02-08 22:55:38 +00002105 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-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 }
wdenke65527f2004-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 Yanok755c1802010-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 Gustschin6acb77b2010-11-28 02:13:33 +01002117 max_size = cfi_flash_bank_size(banknum);
Ilya Yanok755c1802010-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 }
wdenke65527f2004-02-12 00:47:09 +00002122 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00002123 sect_cnt = 0;
2124 sector = base;
wdenke65527f2004-02-12 00:47:09 +00002125 for (i = 0; i < num_erase_regions; i++) {
2126 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00002127 printf ("%d erase regions found, only %d used\n",
2128 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00002129 break;
2130 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002131
Andrew Gabbasovc1592582013-05-14 12:27:52 -05002132 tmp = le32_to_cpu(get_unaligned(
2133 &(qry.erase_region_info[i])));
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002134 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002135
2136 erase_region_count = (tmp & 0xffff) + 1;
2137 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00002138 erase_region_size =
2139 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenkaeba06f2004-06-09 17:34:58 +00002140 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00002141 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00002142 for (j = 0; j < erase_region_count; j++) {
Ilya Yanok755c1802010-10-21 17:20:12 +02002143 if (sector - base >= info->size)
2144 break;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002145 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen73d044d2007-12-07 23:35:02 +01002146 printf("ERROR: too many flash sectors\n");
2147 break;
2148 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002149 info->start[sect_cnt] =
2150 (ulong)map_physmem(sector,
2151 info->portwidth,
2152 MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00002153 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00002154
2155 /*
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002156 * Only read protection status from
2157 * supported devices (intel...)
wdenk26c58432005-01-09 17:12:27 +00002158 */
2159 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04002160 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk26c58432005-01-09 17:12:27 +00002161 case CFI_CMDSET_INTEL_EXTENDED:
2162 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roese5215df12010-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);
wdenk26c58432005-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 Roesebcb33442012-12-06 15:44:10 +01002175 case CFI_CMDSET_AMD_EXTENDED:
2176 case CFI_CMDSET_AMD_STANDARD:
Stefan Roese7de65842012-12-06 15:44:11 +01002177 if (!info->legacy_unlock) {
Stefan Roesebcb33442012-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;
wdenk26c58432005-01-09 17:12:27 +00002196 default:
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002197 /* default: not protected */
2198 info->protect[sect_cnt] = 0;
wdenk26c58432005-01-09 17:12:27 +00002199 }
2200
wdenk2cefd152004-02-08 22:55:38 +00002201 sect_cnt++;
2202 }
2203 }
2204
2205 info->sector_count = sect_cnt;
Haavard Skinnemoen53baec72007-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 Skinnemoend523e392007-12-13 12:56:28 +01002208 info->erase_blk_tout = tmp *
Haavard Skinnemoen53baec72007-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 Skinnemoend523e392007-12-13 12:56:28 +01002213 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-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 Skinnemoend523e392007-12-13 12:56:28 +01002217 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002218 info->write_tout = (tmp + 999) / 1000;
wdenk2cefd152004-02-08 22:55:38 +00002219 info->flash_id = FLASH_MAN_CFI;
Haavard Skinnemoend523e392007-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;
wdenked2ac4b2004-03-14 18:23:55 +00002224 }
Mike Frysinger59404ee2008-10-02 01:55:38 -04002225
2226 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +00002227 }
2228
wdenke65527f2004-02-12 00:47:09 +00002229 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00002230}
2231
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002232#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002233void flash_set_verbose(uint v)
2234{
2235 flash_verbose = v;
2236}
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002237#endif
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002238
Stefan Roeseab935642010-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
wdenk2cefd152004-02-08 22:55:38 +00002264/*-----------------------------------------------------------------------
2265 */
Heiko Schocheref0946a2011-04-04 08:10:21 +02002266
2267void flash_protect_default(void)
2268{
Peter Tyser4f3c60d2011-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 Schocheref0946a2011-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 Lin85706c82013-06-23 00:56:46 +08002303 for (i = 0; i < ARRAY_SIZE(apl); i++) {
Marek Vasutcb1622e2011-10-21 14:17:05 +00002304 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocheref0946a2011-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 Skinnemoen6dd763c2007-10-06 18:55:36 +02002314unsigned long flash_init (void)
wdenk2cefd152004-02-08 22:55:38 +00002315{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002316 unsigned long size = 0;
2317 int i;
wdenk2cefd152004-02-08 22:55:38 +00002318
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002319#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann8f7ee7d2009-03-21 09:59:34 -04002320 /* read environment from EEPROM */
2321 char s[64];
Wolfgang Denk76af2782010-07-24 21:55:43 +02002322 getenv_f("unlock", s, sizeof(s));
Michael Schwingen73d044d2007-12-07 23:35:02 +01002323#endif
wdenk2cefd152004-02-08 22:55:38 +00002324
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002325 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002326 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002327 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk2cefd152004-02-08 22:55:38 +00002328
Stefan Roeseab935642010-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 Roese7e7dda82010-08-30 10:11:51 +02002333 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002334 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002335 size += flash_info[i].size;
2336 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002337#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Peter Tyser0b5a2632010-12-28 18:12:05 -06002338 printf ("## Unknown flash on Bank %d "
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002339 "- Size = 0x%08lx = %ld MB\n",
2340 i+1, flash_info[i].size,
John Schmoller61665db2010-09-29 13:49:05 -05002341 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002342#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002343 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002344#ifdef CONFIG_SYS_FLASH_PROTECTION
Jeroen Hofstee5a85e892014-06-17 22:47:31 +02002345 else if (strcmp(s, "yes") == 0) {
Haavard Skinnemoen6dd763c2007-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-VILLARD03836942008-10-16 15:01:15 +02002350 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoen6dd763c2007-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;
wdenk2cefd152004-02-08 22:55:38 +00002356
Haavard Skinnemoen6dd763c2007-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;
wdenk2cefd152004-02-08 22:55:38 +00002364
Haavard Skinnemoen6dd763c2007-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);
wdenk2cefd152004-02-08 22:55:38 +00002371
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002372 flash_info[i].legacy_unlock = 1;
wdenk2cefd152004-02-08 22:55:38 +00002373
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002374 /*
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]);
Stefan Roesec865e6c2006-02-28 15:29:58 +01002389 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002390 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002391#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002392 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002393
Heiko Schocheref0946a2011-04-04 08:10:21 +02002394 flash_protect_default();
Piotr Ziecik3e939e92008-11-17 15:57:58 +01002395#ifdef CONFIG_FLASH_CFI_MTD
2396 cfi_mtd_init();
2397#endif
2398
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002399 return (size);
wdenk2cefd152004-02-08 22:55:38 +00002400}