blob: 24eb33f0f6e053357b0baa7d82980c618a9217af [file] [log] [blame]
wdenk2cefd152004-02-08 22:55:38 +00001/*
wdenke65527f2004-02-12 00:47:09 +00002 * (C) Copyright 2002-2004
wdenk2cefd152004-02-08 22:55:38 +00003 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4 *
5 * Copyright (C) 2003 Arabella Software Ltd.
6 * Yuli Barcohen <yuli@arabellasw.com>
wdenk2cefd152004-02-08 22:55:38 +00007 *
wdenke65527f2004-02-12 00:47:09 +00008 * Copyright (C) 2004
9 * Ed Okerson
Stefan Roese12797482006-11-13 13:55:24 +010010 *
11 * Copyright (C) 2006
12 * Tolunay Orkun <listmember@orkun.us>
wdenke65527f2004-02-12 00:47:09 +000013 *
wdenk2cefd152004-02-08 22:55:38 +000014 * See file CREDITS for list of people who contributed to this
15 * project.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * MA 02111-1307 USA
31 *
wdenk2cefd152004-02-08 22:55:38 +000032 */
33
34/* The DEBUG define must be before common to enable debugging */
wdenk2ebee312004-02-23 19:30:57 +000035/* #define DEBUG */
36
wdenk2cefd152004-02-08 22:55:38 +000037#include <common.h>
38#include <asm/processor.h>
Haiying Wangc123a382007-02-21 16:52:31 +010039#include <asm/io.h>
wdenkaeba06f2004-06-09 17:34:58 +000040#include <asm/byteorder.h>
wdenkd0245fc2005-04-13 10:02:42 +000041#include <environment.h>
Stefan Roese6e83e342009-10-27 15:15:55 +010042#include <mtd/cfi_flash.h>
wdenke537b3b2004-02-23 23:54:43 +000043
wdenk2cefd152004-02-08 22:55:38 +000044/*
Haavard Skinnemoend523e392007-12-13 12:56:28 +010045 * This file implements a Common Flash Interface (CFI) driver for
46 * U-Boot.
47 *
48 * The width of the port and the width of the chips are determined at
49 * initialization. These widths are used to calculate the address for
50 * access CFI data structures.
wdenk2cefd152004-02-08 22:55:38 +000051 *
52 * References
53 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
54 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
55 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
56 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese12797482006-11-13 13:55:24 +010057 * AMD CFI Specification, Release 2.0 December 1, 2001
58 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
59 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk2cefd152004-02-08 22:55:38 +000060 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020061 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocher800db312007-01-19 18:05:26 +010062 * reading and writing ... (yes there is such a Hardware).
wdenk2cefd152004-02-08 22:55:38 +000063 */
64
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020065#ifndef CONFIG_SYS_FLASH_BANKS_LIST
66#define CONFIG_SYS_FLASH_BANKS_LIST { CONFIG_SYS_FLASH_BASE }
wdenke65527f2004-02-12 00:47:09 +000067#endif
68
Haavard Skinnemoend523e392007-12-13 12:56:28 +010069static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Piotr Ziecik2a7493c2008-11-17 15:49:32 +010070static uint flash_verbose = 1;
Wolfgang Denkafa0dd02006-12-27 01:26:13 +010071
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020072/* use CONFIG_SYS_MAX_FLASH_BANKS_DETECT if defined */
73#ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
74# define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS_DETECT
Marian Balakowicz513b4a12005-10-11 19:09:42 +020075#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020076# define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS
Marian Balakowicz513b4a12005-10-11 19:09:42 +020077#endif
wdenk2cefd152004-02-08 22:55:38 +000078
Wolfgang Denk9f5fb0f2008-08-08 16:39:54 +020079flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
80
Stefan Roesec865e6c2006-02-28 15:29:58 +010081/*
82 * Check if chip width is defined. If not, start detecting with 8bit.
83 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020084#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
85#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roesec865e6c2006-02-28 15:29:58 +010086#endif
87
Stefan Roese38ae9822008-11-17 14:45:22 +010088static void __flash_write8(u8 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +010089{
90 __raw_writeb(value, addr);
91}
92
Stefan Roese38ae9822008-11-17 14:45:22 +010093static void __flash_write16(u16 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +010094{
95 __raw_writew(value, addr);
96}
97
Stefan Roese38ae9822008-11-17 14:45:22 +010098static void __flash_write32(u32 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +010099{
100 __raw_writel(value, addr);
101}
102
Stefan Roese38ae9822008-11-17 14:45:22 +0100103static void __flash_write64(u64 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100104{
105 /* No architectures currently implement __raw_writeq() */
106 *(volatile u64 *)addr = value;
107}
108
Stefan Roese38ae9822008-11-17 14:45:22 +0100109static u8 __flash_read8(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100110{
111 return __raw_readb(addr);
112}
113
Stefan Roese38ae9822008-11-17 14:45:22 +0100114static u16 __flash_read16(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100115{
116 return __raw_readw(addr);
117}
118
Stefan Roese38ae9822008-11-17 14:45:22 +0100119static u32 __flash_read32(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100120{
121 return __raw_readl(addr);
122}
123
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100124static u64 __flash_read64(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100125{
126 /* No architectures currently implement __raw_readq() */
127 return *(volatile u64 *)addr;
128}
129
Stefan Roese38ae9822008-11-17 14:45:22 +0100130#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
131void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
132void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
133void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
134void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
135u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
136u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
137u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100138u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
Stefan Roese38ae9822008-11-17 14:45:22 +0100139#else
140#define flash_write8 __flash_write8
141#define flash_write16 __flash_write16
142#define flash_write32 __flash_write32
143#define flash_write64 __flash_write64
144#define flash_read8 __flash_read8
145#define flash_read16 __flash_read16
146#define flash_read32 __flash_read32
147#define flash_read64 __flash_read64
148#endif
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100149
wdenk2cefd152004-02-08 22:55:38 +0000150/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000151 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200152#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 +0100153flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200154{
155 int i;
156 flash_info_t * info = 0;
wdenk2cefd152004-02-08 22:55:38 +0000157
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200158 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200159 info = & flash_info[i];
160 if (info->size && info->start[0] <= base &&
161 base <= info->start[0] + info->size - 1)
162 break;
163 }
wdenk2cefd152004-02-08 22:55:38 +0000164
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200165 return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200166}
wdenk2cefd152004-02-08 22:55:38 +0000167#endif
168
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100169unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
170{
171 if (sect != (info->sector_count - 1))
172 return info->start[sect + 1] - info->start[sect];
173 else
174 return info->start[0] + info->size - info->start[sect];
175}
176
wdenke65527f2004-02-12 00:47:09 +0000177/*-----------------------------------------------------------------------
178 * create an address based on the offset and the port width
179 */
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100180static inline void *
181flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000182{
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100183 unsigned int byte_offset = offset * info->portwidth;
184
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600185 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100186}
187
188static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
189 unsigned int offset, void *addr)
190{
wdenke65527f2004-02-12 00:47:09 +0000191}
192
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200193/*-----------------------------------------------------------------------
194 * make a proper sized command based on the port and chip widths
195 */
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200196static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200197{
198 int i;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400199 int cword_offset;
200 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200201#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200202 u32 cmd_le = cpu_to_le32(cmd);
203#endif
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400204 uchar val;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200205 uchar *cp = (uchar *) cmdbuf;
206
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400207 for (i = info->portwidth; i > 0; i--){
208 cword_offset = (info->portwidth-i)%info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200209#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400210 cp_offset = info->portwidth - i;
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200211 val = *((uchar*)&cmd_le + cword_offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200212#else
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400213 cp_offset = i - 1;
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200214 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200215#endif
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200216 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400217 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200218}
219
wdenk2cefd152004-02-08 22:55:38 +0000220#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000221/*-----------------------------------------------------------------------
222 * Debug support
223 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100224static void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000225{
226 int i;
227 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000228
Wolfgang Denk49f4f6a2009-02-04 09:42:20 +0100229 cp = (char *) &data;
wdenke65527f2004-02-12 00:47:09 +0000230 for (i = 0; i < 8; i++)
231 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000232}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200233
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100234static void flash_printqry (struct cfi_qry *qry)
wdenke65527f2004-02-12 00:47:09 +0000235{
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100236 u8 *p = (u8 *)qry;
wdenke65527f2004-02-12 00:47:09 +0000237 int x, y;
238
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100239 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
240 debug("%02x : ", x);
241 for (y = 0; y < 16; y++)
242 debug("%2.2x ", p[x + y]);
243 debug(" ");
wdenke65527f2004-02-12 00:47:09 +0000244 for (y = 0; y < 16; y++) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100245 unsigned char c = p[x + y];
246 if (c >= 0x20 && c <= 0x7e)
247 debug("%c", c);
248 else
249 debug(".");
wdenke65527f2004-02-12 00:47:09 +0000250 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100251 debug("\n");
wdenke65527f2004-02-12 00:47:09 +0000252 }
253}
wdenk2cefd152004-02-08 22:55:38 +0000254#endif
255
256
257/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000258 * read a character at a port width address
259 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100260static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000261{
262 uchar *cp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100263 uchar retval;
wdenke65527f2004-02-12 00:47:09 +0000264
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100265 cp = flash_map (info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200266#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100267 retval = flash_read8(cp);
wdenke65527f2004-02-12 00:47:09 +0000268#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100269 retval = flash_read8(cp + info->portwidth - 1);
wdenke65527f2004-02-12 00:47:09 +0000270#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100271 flash_unmap (info, 0, offset, cp);
272 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000273}
274
275/*-----------------------------------------------------------------------
Tor Krill7f2a3052008-03-28 11:29:10 +0100276 * read a word at a port width address, assume 16bit bus
277 */
278static inline ushort flash_read_word (flash_info_t * info, uint offset)
279{
280 ushort *addr, retval;
281
282 addr = flash_map (info, 0, offset);
283 retval = flash_read16 (addr);
284 flash_unmap (info, 0, offset, addr);
285 return retval;
286}
287
288
289/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +0100290 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000291 * port size word. Swap for ppc format.
292 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100293static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
294 uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000295{
wdenke65527f2004-02-12 00:47:09 +0000296 uchar *addr;
297 ulong retval;
298
299#ifdef DEBUG
300 int x;
301#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100302 addr = flash_map (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000303
wdenke65527f2004-02-12 00:47:09 +0000304#ifdef DEBUG
305 debug ("long addr is at %p info->portwidth = %d\n", addr,
306 info->portwidth);
307 for (x = 0; x < 4 * info->portwidth; x++) {
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100308 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenke65527f2004-02-12 00:47:09 +0000309 }
310#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200311#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100312 retval = ((flash_read8(addr) << 16) |
313 (flash_read8(addr + info->portwidth) << 24) |
314 (flash_read8(addr + 2 * info->portwidth)) |
315 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenke65527f2004-02-12 00:47:09 +0000316#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100317 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
318 (flash_read8(addr + info->portwidth - 1) << 16) |
319 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
320 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenke65527f2004-02-12 00:47:09 +0000321#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100322 flash_unmap(info, sect, offset, addr);
323
wdenke65527f2004-02-12 00:47:09 +0000324 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000325}
326
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200327/*
328 * Write a proper sized command to the correct address
Michael Schwingen73d044d2007-12-07 23:35:02 +0100329 */
Stefan Roese6e83e342009-10-27 15:15:55 +0100330void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
331 uint offset, u32 cmd)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100332{
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100333
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100334 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200335 cfiword_t cword;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100336
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100337 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200338 flash_make_cmd (info, cmd, &cword);
339 switch (info->portwidth) {
340 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100341 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200342 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100343 flash_write8(cword.c, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200344 break;
345 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100346 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200347 cmd, cword.w,
348 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100349 flash_write16(cword.w, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200350 break;
351 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100352 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200353 cmd, cword.l,
354 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100355 flash_write32(cword.l, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200356 break;
357 case FLASH_CFI_64BIT:
358#ifdef DEBUG
359 {
360 char str[20];
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100361
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200362 print_longlong (str, cword.ll);
363
364 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100365 addr, cmd, str,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200366 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100367 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200368#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100369 flash_write64(cword.ll, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200370 break;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100371 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200372
373 /* Ensure all the instructions are fully finished */
374 sync();
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100375
376 flash_unmap(info, sect, offset, addr);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100377}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200378
379static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100380{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200381 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
382 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100383}
Michael Schwingen73d044d2007-12-07 23:35:02 +0100384
385/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000386 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200387static int flash_isequal (flash_info_t * info, flash_sect_t sect,
388 uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000389{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100390 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200391 cfiword_t cword;
392 int retval;
wdenk2cefd152004-02-08 22:55:38 +0000393
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100394 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200395 flash_make_cmd (info, cmd, &cword);
Stefan Roeseefef95b2006-04-01 13:41:03 +0200396
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100397 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200398 switch (info->portwidth) {
399 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100400 debug ("is= %x %x\n", flash_read8(addr), cword.c);
401 retval = (flash_read8(addr) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200402 break;
403 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100404 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
405 retval = (flash_read16(addr) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200406 break;
407 case FLASH_CFI_32BIT:
Andrew Klossner7ddfafc2008-08-21 07:12:26 -0700408 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100409 retval = (flash_read32(addr) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200410 break;
411 case FLASH_CFI_64BIT:
412#ifdef DEBUG
413 {
414 char str1[20];
415 char str2[20];
Michael Schwingen73d044d2007-12-07 23:35:02 +0100416
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100417 print_longlong (str1, flash_read64(addr));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200418 print_longlong (str2, cword.ll);
419 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +0000420 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200421#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100422 retval = (flash_read64(addr) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200423 break;
424 default:
425 retval = 0;
426 break;
427 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100428 flash_unmap(info, sect, offset, addr);
429
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200430 return retval;
431}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200432
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200433/*-----------------------------------------------------------------------
434 */
435static int flash_isset (flash_info_t * info, flash_sect_t sect,
436 uint offset, uchar cmd)
437{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100438 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200439 cfiword_t cword;
440 int retval;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200441
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100442 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200443 flash_make_cmd (info, cmd, &cword);
444 switch (info->portwidth) {
445 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100446 retval = ((flash_read8(addr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200447 break;
448 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100449 retval = ((flash_read16(addr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200450 break;
451 case FLASH_CFI_32BIT:
Stefan Roesed4e37c02008-01-02 14:05:37 +0100452 retval = ((flash_read32(addr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200453 break;
454 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100455 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200456 break;
457 default:
458 retval = 0;
459 break;
460 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100461 flash_unmap(info, sect, offset, addr);
462
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200463 return retval;
464}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200465
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200466/*-----------------------------------------------------------------------
467 */
468static int flash_toggle (flash_info_t * info, flash_sect_t sect,
469 uint offset, uchar cmd)
470{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100471 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200472 cfiword_t cword;
473 int retval;
wdenke85b7a52004-10-10 22:16:06 +0000474
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100475 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200476 flash_make_cmd (info, cmd, &cword);
477 switch (info->portwidth) {
478 case FLASH_CFI_8BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200479 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200480 break;
481 case FLASH_CFI_16BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200482 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200483 break;
484 case FLASH_CFI_32BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200485 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200486 break;
487 case FLASH_CFI_64BIT:
Wolfgang Denk600e1832008-10-31 01:12:28 +0100488 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
489 (flash_read32(addr+4) != flash_read32(addr+4)) );
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200490 break;
491 default:
492 retval = 0;
493 break;
494 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100495 flash_unmap(info, sect, offset, addr);
496
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200497 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000498}
499
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200500/*
501 * flash_is_busy - check to see if the flash is busy
502 *
503 * This routine checks the status of the chip and returns true if the
504 * chip is busy.
wdenk2cefd152004-02-08 22:55:38 +0000505 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200506static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5c71a7a2005-05-16 15:23:22 +0000507{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200508 int retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000509
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200510 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400511 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200512 case CFI_CMDSET_INTEL_STANDARD:
513 case CFI_CMDSET_INTEL_EXTENDED:
514 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
515 break;
516 case CFI_CMDSET_AMD_STANDARD:
517 case CFI_CMDSET_AMD_EXTENDED:
518#ifdef CONFIG_FLASH_CFI_LEGACY
519 case CFI_CMDSET_AMD_LEGACY:
520#endif
521 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
522 break;
523 default:
524 retval = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000525 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200526 debug ("flash_is_busy: %d\n", retval);
527 return retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000528}
529
530/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200531 * wait for XSR.7 to be set. Time out with an error if it does not.
532 * This routine does not set the flash to read-array mode.
wdenk5c71a7a2005-05-16 15:23:22 +0000533 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200534static int flash_status_check (flash_info_t * info, flash_sect_t sector,
535 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000536{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200537 ulong start;
wdenk2cefd152004-02-08 22:55:38 +0000538
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200539#if CONFIG_SYS_HZ != 1000
540 tout *= CONFIG_SYS_HZ/1000;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200541#endif
wdenk2cefd152004-02-08 22:55:38 +0000542
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200543 /* Wait for command completion */
544 start = get_timer (0);
545 while (flash_is_busy (info, sector)) {
546 if (get_timer (start) > tout) {
547 printf ("Flash %s timeout at address %lx data %lx\n",
548 prompt, info->start[sector],
549 flash_read_long (info, sector, 0));
550 flash_write_cmd (info, sector, 0, info->cmd_reset);
551 return ERR_TIMOUT;
wdenk2cefd152004-02-08 22:55:38 +0000552 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200553 udelay (1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000554 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200555 return ERR_OK;
556}
wdenk2cefd152004-02-08 22:55:38 +0000557
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200558/*-----------------------------------------------------------------------
559 * Wait for XSR.7 to be set, if it times out print an error, otherwise
560 * do a full status check.
561 *
562 * This routine sets the flash to read-array mode.
563 */
564static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
565 ulong tout, char *prompt)
566{
567 int retcode;
wdenk2cefd152004-02-08 22:55:38 +0000568
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200569 retcode = flash_status_check (info, sector, tout, prompt);
570 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400571 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200572 case CFI_CMDSET_INTEL_EXTENDED:
573 case CFI_CMDSET_INTEL_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500574 if ((retcode != ERR_OK)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200575 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
576 retcode = ERR_INVAL;
577 printf ("Flash %s error at address %lx\n", prompt,
578 info->start[sector]);
579 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
580 FLASH_STATUS_PSLBS)) {
581 puts ("Command Sequence Error.\n");
582 } else if (flash_isset (info, sector, 0,
583 FLASH_STATUS_ECLBS)) {
584 puts ("Block Erase Error.\n");
585 retcode = ERR_NOT_ERASED;
586 } else if (flash_isset (info, sector, 0,
587 FLASH_STATUS_PSLBS)) {
588 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000589 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200590 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
591 puts ("Block locked.\n");
592 retcode = ERR_PROTECTED;
593 }
594 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
595 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000596 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200597 flash_write_cmd (info, sector, 0, info->cmd_reset);
598 break;
599 default:
600 break;
wdenk2cefd152004-02-08 22:55:38 +0000601 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200602 return retcode;
wdenk2cefd152004-02-08 22:55:38 +0000603}
604
605/*-----------------------------------------------------------------------
606 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200607static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000608{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200609#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200610 unsigned short w;
611 unsigned int l;
612 unsigned long long ll;
613#endif
wdenk2cefd152004-02-08 22:55:38 +0000614
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200615 switch (info->portwidth) {
616 case FLASH_CFI_8BIT:
617 cword->c = c;
618 break;
619 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200620#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200621 w = c;
622 w <<= 8;
623 cword->w = (cword->w >> 8) | w;
624#else
625 cword->w = (cword->w << 8) | c;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100626#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200627 break;
628 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200629#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200630 l = c;
631 l <<= 24;
632 cword->l = (cword->l >> 8) | l;
633#else
634 cword->l = (cword->l << 8) | c;
635#endif
636 break;
637 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200638#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200639 ll = c;
640 ll <<= 56;
641 cword->ll = (cword->ll >> 8) | ll;
642#else
643 cword->ll = (cword->ll << 8) | c;
644#endif
645 break;
Stefan Roese12797482006-11-13 13:55:24 +0100646 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200647}
wdenk2cefd152004-02-08 22:55:38 +0000648
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100649/*
650 * Loop through the sector table starting from the previously found sector.
651 * Searches forwards or backwards, dependent on the passed address.
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200652 */
653static flash_sect_t find_sector (flash_info_t * info, ulong addr)
654{
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100655 static flash_sect_t saved_sector = 0; /* previously found sector */
656 flash_sect_t sector = saved_sector;
657
658 while ((info->start[sector] < addr)
659 && (sector < info->sector_count - 1))
660 sector++;
661 while ((info->start[sector] > addr) && (sector > 0))
662 /*
663 * also decrements the sector in case of an overshot
664 * in the first loop
665 */
666 sector--;
wdenk2cefd152004-02-08 22:55:38 +0000667
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100668 saved_sector = sector;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200669 return sector;
wdenk2cefd152004-02-08 22:55:38 +0000670}
671
672/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000673 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200674static int flash_write_cfiword (flash_info_t * info, ulong dest,
675 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +0000676{
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600677 void *dstaddr = (void *)dest;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200678 int flag;
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100679 flash_sect_t sect = 0;
680 char sect_found = 0;
wdenk2cefd152004-02-08 22:55:38 +0000681
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200682 /* Check if Flash is (sufficiently) erased */
683 switch (info->portwidth) {
684 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100685 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200686 break;
687 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100688 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200689 break;
690 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100691 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200692 break;
693 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100694 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200695 break;
696 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100697 flag = 0;
698 break;
wdenk2cefd152004-02-08 22:55:38 +0000699 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600700 if (!flag)
Stefan Roese707c1462007-12-27 07:50:54 +0100701 return ERR_NOT_ERASED;
wdenk2cefd152004-02-08 22:55:38 +0000702
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200703 /* Disable interrupts which might cause a timeout here */
704 flag = disable_interrupts ();
Stefan Roesec865e6c2006-02-28 15:29:58 +0100705
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200706 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400707 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200708 case CFI_CMDSET_INTEL_EXTENDED:
709 case CFI_CMDSET_INTEL_STANDARD:
710 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
711 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
712 break;
713 case CFI_CMDSET_AMD_EXTENDED:
714 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500715 sect = find_sector(info, dest);
716 flash_unlock_seq (info, sect);
717 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100718 sect_found = 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200719 break;
Po-Yu Chuang3f483402009-07-10 18:03:57 +0800720#ifdef CONFIG_FLASH_CFI_LEGACY
721 case CFI_CMDSET_AMD_LEGACY:
722 sect = find_sector(info, dest);
723 flash_unlock_seq (info, 0);
724 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
725 sect_found = 1;
726 break;
727#endif
wdenk2cefd152004-02-08 22:55:38 +0000728 }
729
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200730 switch (info->portwidth) {
731 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100732 flash_write8(cword.c, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200733 break;
734 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100735 flash_write16(cword.w, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200736 break;
737 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100738 flash_write32(cword.l, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200739 break;
740 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100741 flash_write64(cword.ll, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200742 break;
wdenk2cefd152004-02-08 22:55:38 +0000743 }
744
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200745 /* re-enable interrupts if necessary */
746 if (flag)
747 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +0000748
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100749 if (!sect_found)
750 sect = find_sector (info, dest);
751
752 return flash_full_status_check (info, sect, info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +0000753}
754
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200755#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenk2cefd152004-02-08 22:55:38 +0000756
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200757static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
758 int len)
wdenk2cefd152004-02-08 22:55:38 +0000759{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200760 flash_sect_t sector;
761 int cnt;
762 int retcode;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100763 void *src = cp;
Stefan Roese42c6ace2009-02-05 11:25:57 +0100764 void *dst = (void *)dest;
Stefan Roese707c1462007-12-27 07:50:54 +0100765 void *dst2 = dst;
766 int flag = 0;
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200767 uint offset = 0;
768 unsigned int shift;
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400769 uchar write_cmd;
Stefan Roese707c1462007-12-27 07:50:54 +0100770
771 switch (info->portwidth) {
772 case FLASH_CFI_8BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200773 shift = 0;
Stefan Roese707c1462007-12-27 07:50:54 +0100774 break;
775 case FLASH_CFI_16BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200776 shift = 1;
Stefan Roese707c1462007-12-27 07:50:54 +0100777 break;
778 case FLASH_CFI_32BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200779 shift = 2;
Stefan Roese707c1462007-12-27 07:50:54 +0100780 break;
781 case FLASH_CFI_64BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200782 shift = 3;
Stefan Roese707c1462007-12-27 07:50:54 +0100783 break;
784 default:
785 retcode = ERR_INVAL;
786 goto out_unmap;
787 }
788
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200789 cnt = len >> shift;
790
Stefan Roese707c1462007-12-27 07:50:54 +0100791 while ((cnt-- > 0) && (flag == 0)) {
792 switch (info->portwidth) {
793 case FLASH_CFI_8BIT:
794 flag = ((flash_read8(dst2) & flash_read8(src)) ==
795 flash_read8(src));
796 src += 1, dst2 += 1;
797 break;
798 case FLASH_CFI_16BIT:
799 flag = ((flash_read16(dst2) & flash_read16(src)) ==
800 flash_read16(src));
801 src += 2, dst2 += 2;
802 break;
803 case FLASH_CFI_32BIT:
804 flag = ((flash_read32(dst2) & flash_read32(src)) ==
805 flash_read32(src));
806 src += 4, dst2 += 4;
807 break;
808 case FLASH_CFI_64BIT:
809 flag = ((flash_read64(dst2) & flash_read64(src)) ==
810 flash_read64(src));
811 src += 8, dst2 += 8;
812 break;
813 }
814 }
815 if (!flag) {
816 retcode = ERR_NOT_ERASED;
817 goto out_unmap;
818 }
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100819
Stefan Roese707c1462007-12-27 07:50:54 +0100820 src = cp;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100821 sector = find_sector (info, dest);
wdenke65527f2004-02-12 00:47:09 +0000822
823 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400824 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +0000825 case CFI_CMDSET_INTEL_STANDARD:
826 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400827 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
828 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200829 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400830 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
831 flash_write_cmd (info, sector, 0, write_cmd);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200832 retcode = flash_status_check (info, sector,
833 info->buffer_write_tout,
834 "write to buffer");
835 if (retcode == ERR_OK) {
836 /* reduce the number of loops by the width of
837 * the port */
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200838 cnt = len >> shift;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400839 flash_write_cmd (info, sector, 0, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200840 while (cnt-- > 0) {
841 switch (info->portwidth) {
842 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100843 flash_write8(flash_read8(src), dst);
844 src += 1, dst += 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200845 break;
846 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100847 flash_write16(flash_read16(src), dst);
848 src += 2, dst += 2;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200849 break;
850 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100851 flash_write32(flash_read32(src), dst);
852 src += 4, dst += 4;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200853 break;
854 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100855 flash_write64(flash_read64(src), dst);
856 src += 8, dst += 8;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200857 break;
858 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100859 retcode = ERR_INVAL;
860 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200861 }
862 }
863 flash_write_cmd (info, sector, 0,
864 FLASH_CMD_WRITE_BUFFER_CONFIRM);
865 retcode = flash_full_status_check (
866 info, sector, info->buffer_write_tout,
867 "buffer write");
868 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100869
870 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200871
wdenk2cefd152004-02-08 22:55:38 +0000872 case CFI_CMDSET_AMD_STANDARD:
873 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200874 flash_unlock_seq(info,0);
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200875
876#ifdef CONFIG_FLASH_SPANSION_S29WS_N
877 offset = ((unsigned long)dst - info->start[sector]) >> shift;
878#endif
879 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
880 cnt = len >> shift;
John Schmolleree355882009-08-12 10:55:47 -0500881 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200882
883 switch (info->portwidth) {
884 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100885 while (cnt-- > 0) {
886 flash_write8(flash_read8(src), dst);
887 src += 1, dst += 1;
888 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200889 break;
890 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100891 while (cnt-- > 0) {
892 flash_write16(flash_read16(src), dst);
893 src += 2, dst += 2;
894 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200895 break;
896 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100897 while (cnt-- > 0) {
898 flash_write32(flash_read32(src), dst);
899 src += 4, dst += 4;
900 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200901 break;
902 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100903 while (cnt-- > 0) {
904 flash_write64(flash_read64(src), dst);
905 src += 8, dst += 8;
906 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200907 break;
908 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100909 retcode = ERR_INVAL;
910 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200911 }
912
913 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
914 retcode = flash_full_status_check (info, sector,
915 info->buffer_write_tout,
916 "buffer write");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100917 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200918
wdenk2cefd152004-02-08 22:55:38 +0000919 default:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200920 debug ("Unknown Command Set\n");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100921 retcode = ERR_INVAL;
922 break;
wdenk2cefd152004-02-08 22:55:38 +0000923 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100924
925out_unmap:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100926 return retcode;
wdenk2cefd152004-02-08 22:55:38 +0000927}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200928#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200929
wdenke65527f2004-02-12 00:47:09 +0000930
wdenk2cefd152004-02-08 22:55:38 +0000931/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000932 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200933int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +0000934{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200935 int rcode = 0;
936 int prot;
937 flash_sect_t sect;
wdenk2cefd152004-02-08 22:55:38 +0000938
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200939 if (info->flash_id != FLASH_MAN_CFI) {
940 puts ("Can't erase unknown flash type - aborted\n");
941 return 1;
942 }
943 if ((s_first < 0) || (s_first > s_last)) {
944 puts ("- no sectors to erase\n");
945 return 1;
946 }
Stefan Roeseefef95b2006-04-01 13:41:03 +0200947
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200948 prot = 0;
949 for (sect = s_first; sect <= s_last; ++sect) {
950 if (info->protect[sect]) {
951 prot++;
wdenk2cefd152004-02-08 22:55:38 +0000952 }
953 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200954 if (prot) {
955 printf ("- Warning: %d protected sectors will not be erased!\n",
956 prot);
Piotr Ziecik2a7493c2008-11-17 15:49:32 +0100957 } else if (flash_verbose) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200958 putc ('\n');
959 }
wdenke65527f2004-02-12 00:47:09 +0000960
wdenke65527f2004-02-12 00:47:09 +0000961
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200962 for (sect = s_first; sect <= s_last; sect++) {
963 if (info->protect[sect] == 0) { /* not protected */
964 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400965 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200966 case CFI_CMDSET_INTEL_STANDARD:
967 case CFI_CMDSET_INTEL_EXTENDED:
968 flash_write_cmd (info, sect, 0,
969 FLASH_CMD_CLEAR_STATUS);
970 flash_write_cmd (info, sect, 0,
971 FLASH_CMD_BLOCK_ERASE);
972 flash_write_cmd (info, sect, 0,
973 FLASH_CMD_ERASE_CONFIRM);
974 break;
975 case CFI_CMDSET_AMD_STANDARD:
976 case CFI_CMDSET_AMD_EXTENDED:
977 flash_unlock_seq (info, sect);
978 flash_write_cmd (info, sect,
979 info->addr_unlock1,
980 AMD_CMD_ERASE_START);
981 flash_unlock_seq (info, sect);
982 flash_write_cmd (info, sect, 0,
983 AMD_CMD_ERASE_SECTOR);
984 break;
985#ifdef CONFIG_FLASH_CFI_LEGACY
986 case CFI_CMDSET_AMD_LEGACY:
987 flash_unlock_seq (info, 0);
988 flash_write_cmd (info, 0, info->addr_unlock1,
989 AMD_CMD_ERASE_START);
990 flash_unlock_seq (info, 0);
991 flash_write_cmd (info, sect, 0,
992 AMD_CMD_ERASE_SECTOR);
993 break;
994#endif
995 default:
996 debug ("Unkown flash vendor %d\n",
997 info->vendor);
998 break;
wdenke65527f2004-02-12 00:47:09 +0000999 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001000
1001 if (flash_full_status_check
1002 (info, sect, info->erase_blk_tout, "erase")) {
1003 rcode = 1;
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001004 } else if (flash_verbose)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001005 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +00001006 }
wdenk2cefd152004-02-08 22:55:38 +00001007 }
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001008
1009 if (flash_verbose)
1010 puts (" done\n");
1011
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001012 return rcode;
wdenk2cefd152004-02-08 22:55:38 +00001013}
wdenke65527f2004-02-12 00:47:09 +00001014
wdenk2cefd152004-02-08 22:55:38 +00001015/*-----------------------------------------------------------------------
1016 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001017void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001018{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001019 int i;
wdenk369d43d2004-03-14 14:09:05 +00001020
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001021 if (info->flash_id != FLASH_MAN_CFI) {
1022 puts ("missing or unknown FLASH type\n");
1023 return;
1024 }
1025
1026 printf ("%s FLASH (%d x %d)",
1027 info->name,
1028 (info->portwidth << 3), (info->chipwidth << 3));
1029 if (info->size < 1024*1024)
1030 printf (" Size: %ld kB in %d Sectors\n",
1031 info->size >> 10, info->sector_count);
1032 else
1033 printf (" Size: %ld MB in %d Sectors\n",
1034 info->size >> 20, info->sector_count);
1035 printf (" ");
1036 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001037 case CFI_CMDSET_INTEL_PROG_REGIONS:
1038 printf ("Intel Prog Regions");
1039 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001040 case CFI_CMDSET_INTEL_STANDARD:
1041 printf ("Intel Standard");
1042 break;
1043 case CFI_CMDSET_INTEL_EXTENDED:
1044 printf ("Intel Extended");
1045 break;
1046 case CFI_CMDSET_AMD_STANDARD:
1047 printf ("AMD Standard");
1048 break;
1049 case CFI_CMDSET_AMD_EXTENDED:
1050 printf ("AMD Extended");
1051 break;
1052#ifdef CONFIG_FLASH_CFI_LEGACY
1053 case CFI_CMDSET_AMD_LEGACY:
1054 printf ("AMD Legacy");
1055 break;
wdenk369d43d2004-03-14 14:09:05 +00001056#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001057 default:
1058 printf ("Unknown (%d)", info->vendor);
1059 break;
wdenk2cefd152004-02-08 22:55:38 +00001060 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001061 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
1062 info->manufacturer_id, info->device_id);
1063 if (info->device_id == 0x7E) {
1064 printf("%04X", info->device_id2);
1065 }
1066 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1067 info->erase_blk_tout,
1068 info->write_tout);
1069 if (info->buffer_size > 1) {
1070 printf (" Buffer write timeout: %ld ms, "
1071 "buffer size: %d bytes\n",
1072 info->buffer_write_tout,
1073 info->buffer_size);
1074 }
wdenk2cefd152004-02-08 22:55:38 +00001075
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001076 puts ("\n Sector Start Addresses:");
1077 for (i = 0; i < info->sector_count; ++i) {
1078 if ((i % 5) == 0)
1079 printf ("\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001080#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001081 int k;
1082 int size;
1083 int erased;
1084 volatile unsigned long *flash;
wdenk2cefd152004-02-08 22:55:38 +00001085
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001086 /*
1087 * Check if whole sector is erased
1088 */
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001089 size = flash_sector_size(info, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001090 erased = 1;
1091 flash = (volatile unsigned long *) info->start[i];
1092 size = size >> 2; /* divide by 4 for longword access */
1093 for (k = 0; k < size; k++) {
1094 if (*flash++ != 0xffffffff) {
1095 erased = 0;
1096 break;
1097 }
1098 }
wdenke65527f2004-02-12 00:47:09 +00001099
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001100 /* print empty and read-only info */
1101 printf (" %08lX %c %s ",
1102 info->start[i],
1103 erased ? 'E' : ' ',
1104 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001105#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001106 printf (" %08lX %s ",
1107 info->start[i],
1108 info->protect[i] ? "RO" : " ");
wdenke65527f2004-02-12 00:47:09 +00001109#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001110 }
1111 putc ('\n');
1112 return;
wdenk2cefd152004-02-08 22:55:38 +00001113}
1114
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001115/*-----------------------------------------------------------------------
Jerry Van Barenaae73572008-03-08 13:48:01 -05001116 * This is used in a few places in write_buf() to show programming
1117 * progress. Making it a function is nasty because it needs to do side
1118 * effect updates to digit and dots. Repeated code is nasty too, so
1119 * we define it once here.
1120 */
Stefan Roese7758c162008-03-19 07:09:26 +01001121#ifdef CONFIG_FLASH_SHOW_PROGRESS
1122#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001123 if (flash_verbose) { \
1124 dots -= dots_sub; \
1125 if ((scale > 0) && (dots <= 0)) { \
1126 if ((digit % 5) == 0) \
1127 printf ("%d", digit / 5); \
1128 else \
1129 putc ('.'); \
1130 digit--; \
1131 dots += scale; \
1132 } \
Jerry Van Barenaae73572008-03-08 13:48:01 -05001133 }
Stefan Roese7758c162008-03-19 07:09:26 +01001134#else
1135#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1136#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001137
1138/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001139 * Copy memory to flash, returns:
1140 * 0 - OK
1141 * 1 - write timeout
1142 * 2 - Flash not erased
wdenk2cefd152004-02-08 22:55:38 +00001143 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001144int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +00001145{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001146 ulong wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001147 uchar *p;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001148 int aln;
wdenk2cefd152004-02-08 22:55:38 +00001149 cfiword_t cword;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001150 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001151#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001152 int buffered_size;
wdenk2cefd152004-02-08 22:55:38 +00001153#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001154#ifdef CONFIG_FLASH_SHOW_PROGRESS
1155 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1156 int scale = 0;
1157 int dots = 0;
1158
1159 /*
1160 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1161 */
1162 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1163 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1164 CONFIG_FLASH_SHOW_PROGRESS);
1165 }
1166#endif
1167
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001168 /* get lower aligned address */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001169 wp = (addr & ~(info->portwidth - 1));
Haiying Wangc123a382007-02-21 16:52:31 +01001170
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001171 /* handle unaligned start */
1172 if ((aln = addr - wp) != 0) {
1173 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001174 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001175 for (i = 0; i < aln; ++i)
1176 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk2cefd152004-02-08 22:55:38 +00001177
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001178 for (; (i < info->portwidth) && (cnt > 0); i++) {
1179 flash_add_byte (info, &cword, *src++);
1180 cnt--;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001181 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001182 for (; (cnt == 0) && (i < info->portwidth); ++i)
1183 flash_add_byte (info, &cword, flash_read8(p + i));
1184
1185 rc = flash_write_cfiword (info, wp, cword);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001186 if (rc != 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001187 return rc;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001188
1189 wp += i;
Stefan Roese7758c162008-03-19 07:09:26 +01001190 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001191 }
1192
1193 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001194#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001195 buffered_size = (info->portwidth / info->chipwidth);
1196 buffered_size *= info->buffer_size;
1197 while (cnt >= info->portwidth) {
1198 /* prohibit buffer write when buffer_size is 1 */
1199 if (info->buffer_size == 1) {
1200 cword.l = 0;
1201 for (i = 0; i < info->portwidth; i++)
1202 flash_add_byte (info, &cword, *src++);
1203 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1204 return rc;
1205 wp += info->portwidth;
1206 cnt -= info->portwidth;
1207 continue;
1208 }
1209
1210 /* write buffer until next buffered_size aligned boundary */
1211 i = buffered_size - (wp % buffered_size);
1212 if (i > cnt)
1213 i = cnt;
1214 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1215 return rc;
1216 i -= i & (info->portwidth - 1);
1217 wp += i;
1218 src += i;
1219 cnt -= i;
Stefan Roese7758c162008-03-19 07:09:26 +01001220 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001221 }
1222#else
1223 while (cnt >= info->portwidth) {
1224 cword.l = 0;
1225 for (i = 0; i < info->portwidth; i++) {
1226 flash_add_byte (info, &cword, *src++);
1227 }
1228 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1229 return rc;
1230 wp += info->portwidth;
1231 cnt -= info->portwidth;
Stefan Roese7758c162008-03-19 07:09:26 +01001232 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001233 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001234#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Barenaae73572008-03-08 13:48:01 -05001235
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001236 if (cnt == 0) {
1237 return (0);
1238 }
1239
1240 /*
1241 * handle unaligned tail bytes
1242 */
1243 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001244 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001245 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001246 flash_add_byte (info, &cword, *src++);
1247 --cnt;
1248 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001249 for (; i < info->portwidth; ++i)
1250 flash_add_byte (info, &cword, flash_read8(p + i));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001251
1252 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +00001253}
wdenke65527f2004-02-12 00:47:09 +00001254
wdenk2cefd152004-02-08 22:55:38 +00001255/*-----------------------------------------------------------------------
1256 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001257#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001258
1259int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +00001260{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001261 int retcode = 0;
wdenke65527f2004-02-12 00:47:09 +00001262
Rafael Campos13d2b612008-07-31 10:22:20 +02001263 switch (info->vendor) {
1264 case CFI_CMDSET_INTEL_PROG_REGIONS:
1265 case CFI_CMDSET_INTEL_STANDARD:
Nick Spenceec81b472008-08-19 22:21:16 -07001266 case CFI_CMDSET_INTEL_EXTENDED:
Rafael Campos13d2b612008-07-31 10:22:20 +02001267 flash_write_cmd (info, sector, 0,
1268 FLASH_CMD_CLEAR_STATUS);
1269 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1270 if (prot)
1271 flash_write_cmd (info, sector, 0,
1272 FLASH_CMD_PROTECT_SET);
1273 else
1274 flash_write_cmd (info, sector, 0,
1275 FLASH_CMD_PROTECT_CLEAR);
1276 break;
1277 case CFI_CMDSET_AMD_EXTENDED:
1278 case CFI_CMDSET_AMD_STANDARD:
Rafael Campos13d2b612008-07-31 10:22:20 +02001279 /* U-Boot only checks the first byte */
1280 if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
1281 if (prot) {
1282 flash_unlock_seq (info, 0);
1283 flash_write_cmd (info, 0,
1284 info->addr_unlock1,
1285 ATM_CMD_SOFTLOCK_START);
1286 flash_unlock_seq (info, 0);
1287 flash_write_cmd (info, sector, 0,
1288 ATM_CMD_LOCK_SECT);
1289 } else {
1290 flash_write_cmd (info, 0,
1291 info->addr_unlock1,
1292 AMD_CMD_UNLOCK_START);
1293 if (info->device_id == ATM_ID_BV6416)
1294 flash_write_cmd (info, sector,
1295 0, ATM_CMD_UNLOCK_SECT);
1296 }
1297 }
1298 break;
TsiChung Liewb8c19292008-08-19 16:53:39 +00001299#ifdef CONFIG_FLASH_CFI_LEGACY
1300 case CFI_CMDSET_AMD_LEGACY:
1301 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1302 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1303 if (prot)
1304 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1305 else
1306 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1307#endif
Rafael Campos13d2b612008-07-31 10:22:20 +02001308 };
wdenk2cefd152004-02-08 22:55:38 +00001309
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001310 if ((retcode =
1311 flash_full_status_check (info, sector, info->erase_blk_tout,
1312 prot ? "protect" : "unprotect")) == 0) {
wdenke65527f2004-02-12 00:47:09 +00001313
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001314 info->protect[sector] = prot;
1315
1316 /*
1317 * On some of Intel's flash chips (marked via legacy_unlock)
1318 * unprotect unprotects all locking.
1319 */
1320 if ((prot == 0) && (info->legacy_unlock)) {
1321 flash_sect_t i;
1322
1323 for (i = 0; i < info->sector_count; i++) {
1324 if (info->protect[i])
1325 flash_real_protect (info, i, 1);
1326 }
wdenk2cefd152004-02-08 22:55:38 +00001327 }
wdenk2cefd152004-02-08 22:55:38 +00001328 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001329 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001330}
wdenke65527f2004-02-12 00:47:09 +00001331
wdenk2cefd152004-02-08 22:55:38 +00001332/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001333 * flash_read_user_serial - read the OneTimeProgramming cells
wdenk2cefd152004-02-08 22:55:38 +00001334 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001335void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1336 int len)
wdenk2cefd152004-02-08 22:55:38 +00001337{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001338 uchar *src;
1339 uchar *dst;
wdenke65527f2004-02-12 00:47:09 +00001340
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001341 dst = buffer;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001342 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001343 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1344 memcpy (dst, src + offset, len);
1345 flash_write_cmd (info, 0, 0, info->cmd_reset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001346 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001347}
1348
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001349/*
1350 * flash_read_factory_serial - read the device Id from the protection area
wdenk2cefd152004-02-08 22:55:38 +00001351 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001352void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1353 int len)
wdenk2cefd152004-02-08 22:55:38 +00001354{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001355 uchar *src;
wdenke65527f2004-02-12 00:47:09 +00001356
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001357 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001358 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1359 memcpy (buffer, src + offset, len);
1360 flash_write_cmd (info, 0, 0, info->cmd_reset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001361 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001362}
1363
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001364#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001365
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001366/*-----------------------------------------------------------------------
1367 * Reverse the order of the erase regions in the CFI QRY structure.
1368 * This is needed for chips that are either a) correctly detected as
1369 * top-boot, or b) buggy.
1370 */
1371static void cfi_reverse_geometry(struct cfi_qry *qry)
1372{
1373 unsigned int i, j;
1374 u32 tmp;
1375
1376 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1377 tmp = qry->erase_region_info[i];
1378 qry->erase_region_info[i] = qry->erase_region_info[j];
1379 qry->erase_region_info[j] = tmp;
1380 }
1381}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001382
wdenk2cefd152004-02-08 22:55:38 +00001383/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +01001384 * read jedec ids from device and set corresponding fields in info struct
1385 *
1386 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1387 *
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001388 */
1389static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1390{
1391 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1392 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1393 udelay(1000); /* some flash are slow to respond */
1394 info->manufacturer_id = flash_read_uchar (info,
1395 FLASH_OFFSET_MANUFACTURER_ID);
1396 info->device_id = flash_read_uchar (info,
1397 FLASH_OFFSET_DEVICE_ID);
1398 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1399}
1400
1401static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1402{
1403 info->cmd_reset = FLASH_CMD_RESET;
1404
1405 cmdset_intel_read_jedec_ids(info);
1406 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1407
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001408#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001409 /* read legacy lock/unlock bit from intel flash */
1410 if (info->ext_addr) {
1411 info->legacy_unlock = flash_read_uchar (info,
1412 info->ext_addr + 5) & 0x08;
1413 }
1414#endif
1415
1416 return 0;
1417}
1418
1419static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1420{
Niklaus Gigerf447f712009-07-22 17:13:24 +02001421 ushort bankId = 0;
1422 uchar manuId;
1423
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001424 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1425 flash_unlock_seq(info, 0);
1426 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1427 udelay(1000); /* some flash are slow to respond */
Tor Krill7f2a3052008-03-28 11:29:10 +01001428
Niklaus Gigerf447f712009-07-22 17:13:24 +02001429 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1430 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1431 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1432 bankId += 0x100;
1433 manuId = flash_read_uchar (info,
1434 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1435 }
1436 info->manufacturer_id = manuId;
Tor Krill7f2a3052008-03-28 11:29:10 +01001437
1438 switch (info->chipwidth){
1439 case FLASH_CFI_8BIT:
1440 info->device_id = flash_read_uchar (info,
1441 FLASH_OFFSET_DEVICE_ID);
1442 if (info->device_id == 0x7E) {
1443 /* AMD 3-byte (expanded) device ids */
1444 info->device_id2 = flash_read_uchar (info,
1445 FLASH_OFFSET_DEVICE_ID2);
1446 info->device_id2 <<= 8;
1447 info->device_id2 |= flash_read_uchar (info,
1448 FLASH_OFFSET_DEVICE_ID3);
1449 }
1450 break;
1451 case FLASH_CFI_16BIT:
1452 info->device_id = flash_read_word (info,
1453 FLASH_OFFSET_DEVICE_ID);
1454 break;
1455 default:
1456 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001457 }
1458 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1459}
1460
1461static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1462{
1463 info->cmd_reset = AMD_CMD_RESET;
1464
1465 cmdset_amd_read_jedec_ids(info);
1466 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1467
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001468 return 0;
1469}
1470
1471#ifdef CONFIG_FLASH_CFI_LEGACY
Stefan Roese12797482006-11-13 13:55:24 +01001472static void flash_read_jedec_ids (flash_info_t * info)
1473{
1474 info->manufacturer_id = 0;
1475 info->device_id = 0;
1476 info->device_id2 = 0;
1477
1478 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001479 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese12797482006-11-13 13:55:24 +01001480 case CFI_CMDSET_INTEL_STANDARD:
1481 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001482 cmdset_intel_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001483 break;
1484 case CFI_CMDSET_AMD_STANDARD:
1485 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001486 cmdset_amd_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001487 break;
1488 default:
1489 break;
1490 }
1491}
1492
1493/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001494 * Call board code to request info about non-CFI flash.
1495 * board_flash_get_legacy needs to fill in at least:
1496 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001497 */
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001498static int flash_detect_legacy(phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001499{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001500 flash_info_t *info = &flash_info[banknum];
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001501
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001502 if (board_flash_get_legacy(base, banknum, info)) {
1503 /* board code may have filled info completely. If not, we
1504 use JEDEC ID probing. */
1505 if (!info->vendor) {
1506 int modes[] = {
1507 CFI_CMDSET_AMD_STANDARD,
1508 CFI_CMDSET_INTEL_STANDARD
1509 };
1510 int i;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001511
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001512 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1513 info->vendor = modes[i];
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001514 info->start[0] =
1515 (ulong)map_physmem(base,
Stefan Roeseb8443702009-02-05 11:44:52 +01001516 info->portwidth,
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001517 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001518 if (info->portwidth == FLASH_CFI_8BIT
1519 && info->interface == FLASH_CFI_X8X16) {
1520 info->addr_unlock1 = 0x2AAA;
1521 info->addr_unlock2 = 0x5555;
1522 } else {
1523 info->addr_unlock1 = 0x5555;
1524 info->addr_unlock2 = 0x2AAA;
1525 }
1526 flash_read_jedec_ids(info);
1527 debug("JEDEC PROBE: ID %x %x %x\n",
1528 info->manufacturer_id,
1529 info->device_id,
1530 info->device_id2);
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001531 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001532 break;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001533 else
Stefan Roeseb8443702009-02-05 11:44:52 +01001534 unmap_physmem((void *)info->start[0],
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001535 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001536 }
1537 }
1538
1539 switch(info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001540 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001541 case CFI_CMDSET_INTEL_STANDARD:
1542 case CFI_CMDSET_INTEL_EXTENDED:
1543 info->cmd_reset = FLASH_CMD_RESET;
1544 break;
1545 case CFI_CMDSET_AMD_STANDARD:
1546 case CFI_CMDSET_AMD_EXTENDED:
1547 case CFI_CMDSET_AMD_LEGACY:
1548 info->cmd_reset = AMD_CMD_RESET;
1549 break;
1550 }
1551 info->flash_id = FLASH_MAN_CFI;
1552 return 1;
1553 }
1554 return 0; /* use CFI */
1555}
1556#else
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001557static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001558{
1559 return 0; /* use CFI */
1560}
1561#endif
1562
1563/*-----------------------------------------------------------------------
1564 * detect if flash is compatible with the Common Flash Interface (CFI)
1565 * http://www.jedec.org/download/search/jesd68.pdf
1566 */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001567static void flash_read_cfi (flash_info_t *info, void *buf,
1568 unsigned int start, size_t len)
1569{
1570 u8 *p = buf;
1571 unsigned int i;
1572
1573 for (i = 0; i < len; i++)
1574 p[i] = flash_read_uchar(info, start + i);
1575}
Stefan Roese6e83e342009-10-27 15:15:55 +01001576
1577void __flash_cmd_reset(flash_info_t *info)
1578{
1579 /*
1580 * We do not yet know what kind of commandset to use, so we issue
1581 * the reset command in both Intel and AMD variants, in the hope
1582 * that AMD flash roms ignore the Intel command.
1583 */
1584 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1585 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1586}
1587void flash_cmd_reset(flash_info_t *info)
1588 __attribute__((weak,alias("__flash_cmd_reset")));
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001589
1590static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001591{
1592 int cfi_offset;
1593
Stefan Roese6e83e342009-10-27 15:15:55 +01001594 /* Issue FLASH reset command */
1595 flash_cmd_reset(info);
Michael Schwingen4661cf72008-02-18 23:16:35 +01001596
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001597 for (cfi_offset=0;
1598 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1599 cfi_offset++) {
1600 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1601 FLASH_CMD_CFI);
1602 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1603 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1604 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001605 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1606 sizeof(struct cfi_qry));
1607 info->interface = le16_to_cpu(qry->interface_desc);
1608
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001609 info->cfi_offset = flash_offset_cfi[cfi_offset];
1610 debug ("device interface is %d\n",
1611 info->interface);
1612 debug ("found port %d chip %d ",
1613 info->portwidth, info->chipwidth);
1614 debug ("port %d bits chip %d bits\n",
1615 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1616 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1617
1618 /* calculate command offsets as in the Linux driver */
1619 info->addr_unlock1 = 0x555;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001620 info->addr_unlock2 = 0x2aa;
1621
1622 /*
1623 * modify the unlock address if we are
1624 * in compatibility mode
1625 */
1626 if ( /* x8/x16 in x8 mode */
1627 ((info->chipwidth == FLASH_CFI_BY8) &&
1628 (info->interface == FLASH_CFI_X8X16)) ||
1629 /* x16/x32 in x16 mode */
1630 ((info->chipwidth == FLASH_CFI_BY16) &&
1631 (info->interface == FLASH_CFI_X16X32)))
1632 {
1633 info->addr_unlock1 = 0xaaa;
1634 info->addr_unlock2 = 0x555;
1635 }
1636
1637 info->name = "CFI conformant";
1638 return 1;
1639 }
1640 }
1641
1642 return 0;
1643}
1644
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001645static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001646{
wdenke65527f2004-02-12 00:47:09 +00001647 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001648
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001649 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenke65527f2004-02-12 00:47:09 +00001650 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1651 for (info->chipwidth = FLASH_CFI_BY8;
1652 info->chipwidth <= info->portwidth;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001653 info->chipwidth <<= 1)
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001654 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001655 return 1;
wdenk2cefd152004-02-08 22:55:38 +00001656 }
wdenke65527f2004-02-12 00:47:09 +00001657 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001658 return 0;
1659}
wdenke65527f2004-02-12 00:47:09 +00001660
wdenk2cefd152004-02-08 22:55:38 +00001661/*
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001662 * Manufacturer-specific quirks. Add workarounds for geometry
1663 * reversal, etc. here.
1664 */
1665static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1666{
1667 /* check if flash geometry needs reversal */
1668 if (qry->num_erase_regions > 1) {
1669 /* reverse geometry if top boot part */
1670 if (info->cfi_version < 0x3131) {
1671 /* CFI < 1.1, try to guess from device id */
1672 if ((info->device_id & 0x80) != 0)
1673 cfi_reverse_geometry(qry);
1674 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1675 /* CFI >= 1.1, deduct from top/bottom flag */
1676 /* note: ext_addr is valid since cfi_version > 0 */
1677 cfi_reverse_geometry(qry);
1678 }
1679 }
1680}
1681
1682static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1683{
1684 int reverse_geometry = 0;
1685
1686 /* Check the "top boot" bit in the PRI */
1687 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1688 reverse_geometry = 1;
1689
1690 /* AT49BV6416(T) list the erase regions in the wrong order.
1691 * However, the device ID is identical with the non-broken
Ulf Samuelsson07f9b4e2009-03-27 23:26:43 +01001692 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001693 */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001694 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1695 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001696
1697 if (reverse_geometry)
1698 cfi_reverse_geometry(qry);
1699}
1700
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001701static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1702{
1703 /* check if flash geometry needs reversal */
1704 if (qry->num_erase_regions > 1) {
1705 /* reverse geometry if top boot part */
1706 if (info->cfi_version < 0x3131) {
Richard Retanubun47255a52009-03-06 10:09:37 -05001707 /* CFI < 1.1, guess by device id (M29W320{DT,ET} only) */
1708 if (info->device_id == 0x22CA ||
1709 info->device_id == 0x2256) {
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001710 cfi_reverse_geometry(qry);
1711 }
1712 }
1713 }
1714}
1715
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001716/*
wdenk2cefd152004-02-08 22:55:38 +00001717 * The following code cannot be run from FLASH!
1718 *
1719 */
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001720ulong flash_get_size (phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001721{
wdenke65527f2004-02-12 00:47:09 +00001722 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00001723 int i, j;
1724 flash_sect_t sect_cnt;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001725 phys_addr_t sector;
wdenk2cefd152004-02-08 22:55:38 +00001726 unsigned long tmp;
1727 int size_ratio;
1728 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00001729 int erase_region_size;
1730 int erase_region_count;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001731 struct cfi_qry qry;
Stefan Roese12797482006-11-13 13:55:24 +01001732
Kumar Gala899032b2008-05-15 15:13:08 -05001733 memset(&qry, 0, sizeof(qry));
1734
Stefan Roese12797482006-11-13 13:55:24 +01001735 info->ext_addr = 0;
1736 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001737#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02001738 info->legacy_unlock = 0;
1739#endif
wdenk2cefd152004-02-08 22:55:38 +00001740
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001741 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00001742
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001743 if (flash_detect_cfi (info, &qry)) {
1744 info->vendor = le16_to_cpu(qry.p_id);
1745 info->ext_addr = le16_to_cpu(qry.p_adr);
1746 num_erase_regions = qry.num_erase_regions;
1747
Stefan Roese12797482006-11-13 13:55:24 +01001748 if (info->ext_addr) {
1749 info->cfi_version = (ushort) flash_read_uchar (info,
1750 info->ext_addr + 3) << 8;
1751 info->cfi_version |= (ushort) flash_read_uchar (info,
1752 info->ext_addr + 4);
1753 }
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001754
wdenke65527f2004-02-12 00:47:09 +00001755#ifdef DEBUG
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001756 flash_printqry (&qry);
wdenke65527f2004-02-12 00:47:09 +00001757#endif
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001758
wdenke65527f2004-02-12 00:47:09 +00001759 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001760 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +00001761 case CFI_CMDSET_INTEL_STANDARD:
1762 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001763 cmdset_intel_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00001764 break;
1765 case CFI_CMDSET_AMD_STANDARD:
1766 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001767 cmdset_amd_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00001768 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001769 default:
1770 printf("CFI: Unknown command set 0x%x\n",
1771 info->vendor);
1772 /*
1773 * Unfortunately, this means we don't know how
1774 * to get the chip back to Read mode. Might
1775 * as well try an Intel-style reset...
1776 */
1777 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1778 return 0;
wdenk2cefd152004-02-08 22:55:38 +00001779 }
wdenk6cfa84e2004-02-10 00:03:41 +00001780
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001781 /* Do manufacturer-specific fixups */
1782 switch (info->manufacturer_id) {
1783 case 0x0001:
1784 flash_fixup_amd(info, &qry);
1785 break;
1786 case 0x001f:
1787 flash_fixup_atmel(info, &qry);
1788 break;
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001789 case 0x0020:
1790 flash_fixup_stm(info, &qry);
1791 break;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001792 }
1793
wdenke65527f2004-02-12 00:47:09 +00001794 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese12797482006-11-13 13:55:24 +01001795 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1796 debug ("device id is 0x%x\n", info->device_id);
1797 debug ("device id2 is 0x%x\n", info->device_id2);
1798 debug ("cfi version is 0x%04x\n", info->cfi_version);
1799
wdenk2cefd152004-02-08 22:55:38 +00001800 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00001801 /* if the chip is x8/x16 reduce the ratio by half */
1802 if ((info->interface == FLASH_CFI_X8X16)
1803 && (info->chipwidth == FLASH_CFI_BY8)) {
1804 size_ratio >>= 1;
1805 }
wdenke65527f2004-02-12 00:47:09 +00001806 debug ("size_ratio %d port %d bits chip %d bits\n",
1807 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1808 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1809 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00001810 sect_cnt = 0;
1811 sector = base;
wdenke65527f2004-02-12 00:47:09 +00001812 for (i = 0; i < num_erase_regions; i++) {
1813 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00001814 printf ("%d erase regions found, only %d used\n",
1815 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00001816 break;
1817 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001818
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001819 tmp = le32_to_cpu(qry.erase_region_info[i]);
1820 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001821
1822 erase_region_count = (tmp & 0xffff) + 1;
1823 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00001824 erase_region_size =
1825 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenkaeba06f2004-06-09 17:34:58 +00001826 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00001827 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00001828 for (j = 0; j < erase_region_count; j++) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001829 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen73d044d2007-12-07 23:35:02 +01001830 printf("ERROR: too many flash sectors\n");
1831 break;
1832 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001833 info->start[sect_cnt] =
1834 (ulong)map_physmem(sector,
1835 info->portwidth,
1836 MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00001837 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00001838
1839 /*
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001840 * Only read protection status from
1841 * supported devices (intel...)
wdenk26c58432005-01-09 17:12:27 +00001842 */
1843 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001844 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk26c58432005-01-09 17:12:27 +00001845 case CFI_CMDSET_INTEL_EXTENDED:
1846 case CFI_CMDSET_INTEL_STANDARD:
1847 info->protect[sect_cnt] =
1848 flash_isset (info, sect_cnt,
1849 FLASH_OFFSET_PROTECT,
1850 FLASH_STATUS_PROTECT);
1851 break;
1852 default:
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001853 /* default: not protected */
1854 info->protect[sect_cnt] = 0;
wdenk26c58432005-01-09 17:12:27 +00001855 }
1856
wdenk2cefd152004-02-08 22:55:38 +00001857 sect_cnt++;
1858 }
1859 }
1860
1861 info->sector_count = sect_cnt;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001862 info->size = 1 << qry.dev_size;
wdenk2cefd152004-02-08 22:55:38 +00001863 /* multiply the size by the number of chips */
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001864 info->size *= size_ratio;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001865 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
1866 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001867 info->erase_blk_tout = tmp *
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001868 (1 << qry.block_erase_timeout_max);
1869 tmp = (1 << qry.buf_write_timeout_typ) *
1870 (1 << qry.buf_write_timeout_max);
1871
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001872 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001873 info->buffer_write_tout = (tmp + 999) / 1000;
1874 tmp = (1 << qry.word_write_timeout_typ) *
1875 (1 << qry.word_write_timeout_max);
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001876 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001877 info->write_tout = (tmp + 999) / 1000;
wdenk2cefd152004-02-08 22:55:38 +00001878 info->flash_id = FLASH_MAN_CFI;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001879 if ((info->interface == FLASH_CFI_X8X16) &&
1880 (info->chipwidth == FLASH_CFI_BY8)) {
1881 /* XXX - Need to test on x8/x16 in parallel. */
1882 info->portwidth >>= 1;
wdenked2ac4b2004-03-14 18:23:55 +00001883 }
Mike Frysinger59404ee2008-10-02 01:55:38 -04001884
1885 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +00001886 }
1887
wdenke65527f2004-02-12 00:47:09 +00001888 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00001889}
1890
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001891void flash_set_verbose(uint v)
1892{
1893 flash_verbose = v;
1894}
1895
wdenk2cefd152004-02-08 22:55:38 +00001896/*-----------------------------------------------------------------------
1897 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001898unsigned long flash_init (void)
wdenk2cefd152004-02-08 22:55:38 +00001899{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001900 unsigned long size = 0;
1901 int i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001902#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
Matthias Fuchs50431522008-04-18 16:29:40 +02001903 struct apl_s {
1904 ulong start;
1905 ulong size;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001906 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
Matthias Fuchs50431522008-04-18 16:29:40 +02001907#endif
wdenk2cefd152004-02-08 22:55:38 +00001908
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001909#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann8f7ee7d2009-03-21 09:59:34 -04001910 /* read environment from EEPROM */
1911 char s[64];
1912 getenv_r ("unlock", s, sizeof(s));
Michael Schwingen73d044d2007-12-07 23:35:02 +01001913#endif
wdenk2cefd152004-02-08 22:55:38 +00001914
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001915#define BANK_BASE(i) (((phys_addr_t [CFI_MAX_FLASH_BANKS])CONFIG_SYS_FLASH_BANKS_LIST)[i])
Wolfgang Denk9f5fb0f2008-08-08 16:39:54 +02001916
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001917 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001918 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001919 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk2cefd152004-02-08 22:55:38 +00001920
Wolfgang Denk9f5fb0f2008-08-08 16:39:54 +02001921 if (!flash_detect_legacy (BANK_BASE(i), i))
1922 flash_get_size (BANK_BASE(i), i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001923 size += flash_info[i].size;
1924 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001925#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001926 printf ("## Unknown FLASH on Bank %d "
1927 "- Size = 0x%08lx = %ld MB\n",
1928 i+1, flash_info[i].size,
1929 flash_info[i].size << 20);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001930#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001931 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001932#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001933 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
1934 /*
1935 * Only the U-Boot image and it's environment
1936 * is protected, all other sectors are
1937 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001938 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001939 * and the environment variable "unlock" is
1940 * set to "yes".
1941 */
1942 if (flash_info[i].legacy_unlock) {
1943 int k;
wdenk2cefd152004-02-08 22:55:38 +00001944
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001945 /*
1946 * Disable legacy_unlock temporarily,
1947 * since flash_real_protect would
1948 * relock all other sectors again
1949 * otherwise.
1950 */
1951 flash_info[i].legacy_unlock = 0;
wdenk2cefd152004-02-08 22:55:38 +00001952
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001953 /*
1954 * Legacy unlocking (e.g. Intel J3) ->
1955 * unlock only one sector. This will
1956 * unlock all sectors.
1957 */
1958 flash_real_protect (&flash_info[i], 0, 0);
wdenk2cefd152004-02-08 22:55:38 +00001959
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001960 flash_info[i].legacy_unlock = 1;
wdenk2cefd152004-02-08 22:55:38 +00001961
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001962 /*
1963 * Manually mark other sectors as
1964 * unlocked (unprotected)
1965 */
1966 for (k = 1; k < flash_info[i].sector_count; k++)
1967 flash_info[i].protect[k] = 0;
1968 } else {
1969 /*
1970 * No legancy unlocking -> unlock all sectors
1971 */
1972 flash_protect (FLAG_PROTECT_CLEAR,
1973 flash_info[i].start[0],
1974 flash_info[i].start[0]
1975 + flash_info[i].size - 1,
1976 &flash_info[i]);
Stefan Roesec865e6c2006-02-28 15:29:58 +01001977 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01001978 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001979#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001980 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01001981
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001982 /* Monitor protection ON by default */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001983#if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001984 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001985 CONFIG_SYS_MONITOR_BASE,
1986 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
1987 flash_get_info(CONFIG_SYS_MONITOR_BASE));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001988#endif
Stefan Roesec865e6c2006-02-28 15:29:58 +01001989
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001990 /* Environment protection ON by default */
Jean-Christophe PLAGNIOL-VILLARD53db4cd2008-09-10 22:48:04 +02001991#ifdef CONFIG_ENV_IS_IN_FLASH
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001992 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +02001993 CONFIG_ENV_ADDR,
1994 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
1995 flash_get_info(CONFIG_ENV_ADDR));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001996#endif
Stefan Roesec865e6c2006-02-28 15:29:58 +01001997
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001998 /* Redundant environment protection ON by default */
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +02001999#ifdef CONFIG_ENV_ADDR_REDUND
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002000 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +02002001 CONFIG_ENV_ADDR_REDUND,
Wolfgang Denk47913832009-05-15 00:16:03 +02002002 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +02002003 flash_get_info(CONFIG_ENV_ADDR_REDUND));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002004#endif
Matthias Fuchs50431522008-04-18 16:29:40 +02002005
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002006#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
Matthias Fuchs50431522008-04-18 16:29:40 +02002007 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
2008 debug("autoprotecting from %08x to %08x\n",
2009 apl[i].start, apl[i].start + apl[i].size - 1);
2010 flash_protect (FLAG_PROTECT_SET,
2011 apl[i].start,
2012 apl[i].start + apl[i].size - 1,
2013 flash_get_info(apl[i].start));
2014 }
2015#endif
Piotr Ziecik3e939e92008-11-17 15:57:58 +01002016
2017#ifdef CONFIG_FLASH_CFI_MTD
2018 cfi_mtd_init();
2019#endif
2020
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002021 return (size);
wdenk2cefd152004-02-08 22:55:38 +00002022}