blob: 5788328ef149c5bb50814ad8101261f11c2a2fb3 [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
Haavard Skinnemoend523e392007-12-13 12:56:28 +010065static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysingerc2c093d2010-12-22 09:41:13 -050066#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +010067static uint flash_verbose = 1;
Mike Frysingerc2c093d2010-12-22 09:41:13 -050068#else
69#define flash_verbose 1
70#endif
Wolfgang Denkafa0dd02006-12-27 01:26:13 +010071
Wolfgang Denk9f5fb0f2008-08-08 16:39:54 +020072flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
73
Stefan Roesec865e6c2006-02-28 15:29:58 +010074/*
75 * Check if chip width is defined. If not, start detecting with 8bit.
76 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020077#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
78#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roesec865e6c2006-02-28 15:29:58 +010079#endif
80
Stefan Roeseab935642010-10-25 18:31:48 +020081/*
82 * 0xffff is an undefined value for the configuration register. When
83 * this value is returned, the configuration register shall not be
84 * written at all (default mode).
85 */
86static u16 cfi_flash_config_reg(int i)
87{
88#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
89 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
90#else
91 return 0xffff;
92#endif
93}
94
Stefan Roesefb9a7302010-08-31 10:00:10 +020095#if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
96int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
97#endif
98
Stefan Roese7e7dda82010-08-30 10:11:51 +020099static phys_addr_t __cfi_flash_bank_addr(int i)
100{
101 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
102}
103phys_addr_t cfi_flash_bank_addr(int i)
104 __attribute__((weak, alias("__cfi_flash_bank_addr")));
105
Ilya Yanok755c1802010-10-21 17:20:12 +0200106static unsigned long __cfi_flash_bank_size(int i)
107{
108#ifdef CONFIG_SYS_FLASH_BANKS_SIZES
109 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
110#else
111 return 0;
112#endif
113}
114unsigned long cfi_flash_bank_size(int i)
115 __attribute__((weak, alias("__cfi_flash_bank_size")));
116
Stefan Roese38ae9822008-11-17 14:45:22 +0100117static void __flash_write8(u8 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100118{
119 __raw_writeb(value, addr);
120}
121
Stefan Roese38ae9822008-11-17 14:45:22 +0100122static void __flash_write16(u16 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100123{
124 __raw_writew(value, addr);
125}
126
Stefan Roese38ae9822008-11-17 14:45:22 +0100127static void __flash_write32(u32 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100128{
129 __raw_writel(value, addr);
130}
131
Stefan Roese38ae9822008-11-17 14:45:22 +0100132static void __flash_write64(u64 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100133{
134 /* No architectures currently implement __raw_writeq() */
135 *(volatile u64 *)addr = value;
136}
137
Stefan Roese38ae9822008-11-17 14:45:22 +0100138static u8 __flash_read8(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100139{
140 return __raw_readb(addr);
141}
142
Stefan Roese38ae9822008-11-17 14:45:22 +0100143static u16 __flash_read16(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100144{
145 return __raw_readw(addr);
146}
147
Stefan Roese38ae9822008-11-17 14:45:22 +0100148static u32 __flash_read32(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100149{
150 return __raw_readl(addr);
151}
152
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100153static u64 __flash_read64(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100154{
155 /* No architectures currently implement __raw_readq() */
156 return *(volatile u64 *)addr;
157}
158
Stefan Roese38ae9822008-11-17 14:45:22 +0100159#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
160void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
161void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
162void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
163void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
164u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
165u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
166u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100167u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
Stefan Roese38ae9822008-11-17 14:45:22 +0100168#else
169#define flash_write8 __flash_write8
170#define flash_write16 __flash_write16
171#define flash_write32 __flash_write32
172#define flash_write64 __flash_write64
173#define flash_read8 __flash_read8
174#define flash_read16 __flash_read16
175#define flash_read32 __flash_read32
176#define flash_read64 __flash_read64
177#endif
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100178
wdenk2cefd152004-02-08 22:55:38 +0000179/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000180 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200181#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 +0100182flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200183{
184 int i;
Stefan Roese6c7c2dc2010-08-30 11:14:38 +0200185 flash_info_t *info = NULL;
wdenk2cefd152004-02-08 22:55:38 +0000186
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200187 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200188 info = & flash_info[i];
189 if (info->size && info->start[0] <= base &&
190 base <= info->start[0] + info->size - 1)
191 break;
192 }
wdenk2cefd152004-02-08 22:55:38 +0000193
Stefan Roese6c7c2dc2010-08-30 11:14:38 +0200194 return info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200195}
wdenk2cefd152004-02-08 22:55:38 +0000196#endif
197
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100198unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
199{
200 if (sect != (info->sector_count - 1))
201 return info->start[sect + 1] - info->start[sect];
202 else
203 return info->start[0] + info->size - info->start[sect];
204}
205
wdenke65527f2004-02-12 00:47:09 +0000206/*-----------------------------------------------------------------------
207 * create an address based on the offset and the port width
208 */
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100209static inline void *
210flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000211{
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100212 unsigned int byte_offset = offset * info->portwidth;
213
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600214 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100215}
216
217static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
218 unsigned int offset, void *addr)
219{
wdenke65527f2004-02-12 00:47:09 +0000220}
221
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200222/*-----------------------------------------------------------------------
223 * make a proper sized command based on the port and chip widths
224 */
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200225static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200226{
227 int i;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400228 int cword_offset;
229 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200230#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200231 u32 cmd_le = cpu_to_le32(cmd);
232#endif
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400233 uchar val;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200234 uchar *cp = (uchar *) cmdbuf;
235
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400236 for (i = info->portwidth; i > 0; i--){
237 cword_offset = (info->portwidth-i)%info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200238#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400239 cp_offset = info->portwidth - i;
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200240 val = *((uchar*)&cmd_le + cword_offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200241#else
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400242 cp_offset = i - 1;
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200243 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200244#endif
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200245 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400246 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200247}
248
wdenk2cefd152004-02-08 22:55:38 +0000249#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000250/*-----------------------------------------------------------------------
251 * Debug support
252 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100253static void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000254{
255 int i;
256 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000257
Wolfgang Denk49f4f6a2009-02-04 09:42:20 +0100258 cp = (char *) &data;
wdenke65527f2004-02-12 00:47:09 +0000259 for (i = 0; i < 8; i++)
260 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000261}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200262
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100263static void flash_printqry (struct cfi_qry *qry)
wdenke65527f2004-02-12 00:47:09 +0000264{
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100265 u8 *p = (u8 *)qry;
wdenke65527f2004-02-12 00:47:09 +0000266 int x, y;
267
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100268 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
269 debug("%02x : ", x);
270 for (y = 0; y < 16; y++)
271 debug("%2.2x ", p[x + y]);
272 debug(" ");
wdenke65527f2004-02-12 00:47:09 +0000273 for (y = 0; y < 16; y++) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100274 unsigned char c = p[x + y];
275 if (c >= 0x20 && c <= 0x7e)
276 debug("%c", c);
277 else
278 debug(".");
wdenke65527f2004-02-12 00:47:09 +0000279 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100280 debug("\n");
wdenke65527f2004-02-12 00:47:09 +0000281 }
282}
wdenk2cefd152004-02-08 22:55:38 +0000283#endif
284
285
286/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000287 * read a character at a port width address
288 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100289static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000290{
291 uchar *cp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100292 uchar retval;
wdenke65527f2004-02-12 00:47:09 +0000293
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100294 cp = flash_map (info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200295#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100296 retval = flash_read8(cp);
wdenke65527f2004-02-12 00:47:09 +0000297#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100298 retval = flash_read8(cp + info->portwidth - 1);
wdenke65527f2004-02-12 00:47:09 +0000299#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100300 flash_unmap (info, 0, offset, cp);
301 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000302}
303
304/*-----------------------------------------------------------------------
Tor Krill7f2a3052008-03-28 11:29:10 +0100305 * read a word at a port width address, assume 16bit bus
306 */
307static inline ushort flash_read_word (flash_info_t * info, uint offset)
308{
309 ushort *addr, retval;
310
311 addr = flash_map (info, 0, offset);
312 retval = flash_read16 (addr);
313 flash_unmap (info, 0, offset, addr);
314 return retval;
315}
316
317
318/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +0100319 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000320 * port size word. Swap for ppc format.
321 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100322static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
323 uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000324{
wdenke65527f2004-02-12 00:47:09 +0000325 uchar *addr;
326 ulong retval;
327
328#ifdef DEBUG
329 int x;
330#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100331 addr = flash_map (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000332
wdenke65527f2004-02-12 00:47:09 +0000333#ifdef DEBUG
334 debug ("long addr is at %p info->portwidth = %d\n", addr,
335 info->portwidth);
336 for (x = 0; x < 4 * info->portwidth; x++) {
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100337 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenke65527f2004-02-12 00:47:09 +0000338 }
339#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200340#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100341 retval = ((flash_read8(addr) << 16) |
342 (flash_read8(addr + info->portwidth) << 24) |
343 (flash_read8(addr + 2 * info->portwidth)) |
344 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenke65527f2004-02-12 00:47:09 +0000345#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100346 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
347 (flash_read8(addr + info->portwidth - 1) << 16) |
348 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
349 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenke65527f2004-02-12 00:47:09 +0000350#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100351 flash_unmap(info, sect, offset, addr);
352
wdenke65527f2004-02-12 00:47:09 +0000353 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000354}
355
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200356/*
357 * Write a proper sized command to the correct address
Michael Schwingen73d044d2007-12-07 23:35:02 +0100358 */
Stefan Roese6e83e342009-10-27 15:15:55 +0100359void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
360 uint offset, u32 cmd)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100361{
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100362
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100363 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200364 cfiword_t cword;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100365
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100366 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200367 flash_make_cmd (info, cmd, &cword);
368 switch (info->portwidth) {
369 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100370 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200371 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100372 flash_write8(cword.c, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200373 break;
374 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100375 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200376 cmd, cword.w,
377 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100378 flash_write16(cword.w, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200379 break;
380 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100381 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200382 cmd, cword.l,
383 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100384 flash_write32(cword.l, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200385 break;
386 case FLASH_CFI_64BIT:
387#ifdef DEBUG
388 {
389 char str[20];
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100390
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200391 print_longlong (str, cword.ll);
392
393 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100394 addr, cmd, str,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200395 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100396 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200397#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100398 flash_write64(cword.ll, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200399 break;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100400 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200401
402 /* Ensure all the instructions are fully finished */
403 sync();
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100404
405 flash_unmap(info, sect, offset, addr);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100406}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200407
408static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100409{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200410 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
411 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100412}
Michael Schwingen73d044d2007-12-07 23:35:02 +0100413
414/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000415 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200416static int flash_isequal (flash_info_t * info, flash_sect_t sect,
417 uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000418{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100419 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200420 cfiword_t cword;
421 int retval;
wdenk2cefd152004-02-08 22:55:38 +0000422
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100423 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200424 flash_make_cmd (info, cmd, &cword);
Stefan Roeseefef95b2006-04-01 13:41:03 +0200425
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100426 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200427 switch (info->portwidth) {
428 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100429 debug ("is= %x %x\n", flash_read8(addr), cword.c);
430 retval = (flash_read8(addr) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200431 break;
432 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100433 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
434 retval = (flash_read16(addr) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200435 break;
436 case FLASH_CFI_32BIT:
Andrew Klossner7ddfafc2008-08-21 07:12:26 -0700437 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100438 retval = (flash_read32(addr) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200439 break;
440 case FLASH_CFI_64BIT:
441#ifdef DEBUG
442 {
443 char str1[20];
444 char str2[20];
Michael Schwingen73d044d2007-12-07 23:35:02 +0100445
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100446 print_longlong (str1, flash_read64(addr));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200447 print_longlong (str2, cword.ll);
448 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +0000449 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200450#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100451 retval = (flash_read64(addr) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200452 break;
453 default:
454 retval = 0;
455 break;
456 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100457 flash_unmap(info, sect, offset, addr);
458
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200459 return retval;
460}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200461
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200462/*-----------------------------------------------------------------------
463 */
464static int flash_isset (flash_info_t * info, flash_sect_t sect,
465 uint offset, uchar cmd)
466{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100467 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200468 cfiword_t cword;
469 int retval;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200470
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100471 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200472 flash_make_cmd (info, cmd, &cword);
473 switch (info->portwidth) {
474 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100475 retval = ((flash_read8(addr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200476 break;
477 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100478 retval = ((flash_read16(addr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200479 break;
480 case FLASH_CFI_32BIT:
Stefan Roesed4e37c02008-01-02 14:05:37 +0100481 retval = ((flash_read32(addr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200482 break;
483 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100484 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200485 break;
486 default:
487 retval = 0;
488 break;
489 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100490 flash_unmap(info, sect, offset, addr);
491
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200492 return retval;
493}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200494
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200495/*-----------------------------------------------------------------------
496 */
497static int flash_toggle (flash_info_t * info, flash_sect_t sect,
498 uint offset, uchar cmd)
499{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100500 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200501 cfiword_t cword;
502 int retval;
wdenke85b7a52004-10-10 22:16:06 +0000503
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100504 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200505 flash_make_cmd (info, cmd, &cword);
506 switch (info->portwidth) {
507 case FLASH_CFI_8BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200508 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200509 break;
510 case FLASH_CFI_16BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200511 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200512 break;
513 case FLASH_CFI_32BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200514 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200515 break;
516 case FLASH_CFI_64BIT:
Wolfgang Denk600e1832008-10-31 01:12:28 +0100517 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
518 (flash_read32(addr+4) != flash_read32(addr+4)) );
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200519 break;
520 default:
521 retval = 0;
522 break;
523 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100524 flash_unmap(info, sect, offset, addr);
525
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200526 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000527}
528
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200529/*
530 * flash_is_busy - check to see if the flash is busy
531 *
532 * This routine checks the status of the chip and returns true if the
533 * chip is busy.
wdenk2cefd152004-02-08 22:55:38 +0000534 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200535static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5c71a7a2005-05-16 15:23:22 +0000536{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200537 int retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000538
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200539 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400540 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200541 case CFI_CMDSET_INTEL_STANDARD:
542 case CFI_CMDSET_INTEL_EXTENDED:
543 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
544 break;
545 case CFI_CMDSET_AMD_STANDARD:
546 case CFI_CMDSET_AMD_EXTENDED:
547#ifdef CONFIG_FLASH_CFI_LEGACY
548 case CFI_CMDSET_AMD_LEGACY:
549#endif
550 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
551 break;
552 default:
553 retval = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000554 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200555 debug ("flash_is_busy: %d\n", retval);
556 return retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000557}
558
559/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200560 * wait for XSR.7 to be set. Time out with an error if it does not.
561 * This routine does not set the flash to read-array mode.
wdenk5c71a7a2005-05-16 15:23:22 +0000562 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200563static int flash_status_check (flash_info_t * info, flash_sect_t sector,
564 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000565{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200566 ulong start;
wdenk2cefd152004-02-08 22:55:38 +0000567
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200568#if CONFIG_SYS_HZ != 1000
Renato Andreolaac6693d2010-03-24 23:00:47 +0800569 if ((ulong)CONFIG_SYS_HZ > 100000)
570 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
571 else
572 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200573#endif
wdenk2cefd152004-02-08 22:55:38 +0000574
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200575 /* Wait for command completion */
Thomas Chou4b7e6682010-04-01 11:15:05 +0800576 reset_timer();
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200577 start = get_timer (0);
578 while (flash_is_busy (info, sector)) {
579 if (get_timer (start) > tout) {
580 printf ("Flash %s timeout at address %lx data %lx\n",
581 prompt, info->start[sector],
582 flash_read_long (info, sector, 0));
583 flash_write_cmd (info, sector, 0, info->cmd_reset);
584 return ERR_TIMOUT;
wdenk2cefd152004-02-08 22:55:38 +0000585 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200586 udelay (1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000587 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200588 return ERR_OK;
589}
wdenk2cefd152004-02-08 22:55:38 +0000590
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200591/*-----------------------------------------------------------------------
592 * Wait for XSR.7 to be set, if it times out print an error, otherwise
593 * do a full status check.
594 *
595 * This routine sets the flash to read-array mode.
596 */
597static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
598 ulong tout, char *prompt)
599{
600 int retcode;
wdenk2cefd152004-02-08 22:55:38 +0000601
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200602 retcode = flash_status_check (info, sector, tout, prompt);
603 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400604 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200605 case CFI_CMDSET_INTEL_EXTENDED:
606 case CFI_CMDSET_INTEL_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500607 if ((retcode != ERR_OK)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200608 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
609 retcode = ERR_INVAL;
610 printf ("Flash %s error at address %lx\n", prompt,
611 info->start[sector]);
612 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
613 FLASH_STATUS_PSLBS)) {
614 puts ("Command Sequence Error.\n");
615 } else if (flash_isset (info, sector, 0,
616 FLASH_STATUS_ECLBS)) {
617 puts ("Block Erase Error.\n");
618 retcode = ERR_NOT_ERASED;
619 } else if (flash_isset (info, sector, 0,
620 FLASH_STATUS_PSLBS)) {
621 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000622 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200623 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
624 puts ("Block locked.\n");
625 retcode = ERR_PROTECTED;
626 }
627 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
628 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000629 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200630 flash_write_cmd (info, sector, 0, info->cmd_reset);
631 break;
632 default:
633 break;
wdenk2cefd152004-02-08 22:55:38 +0000634 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200635 return retcode;
wdenk2cefd152004-02-08 22:55:38 +0000636}
637
Thomas Chou076767a2010-03-26 08:17:00 +0800638static int use_flash_status_poll(flash_info_t *info)
639{
640#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
641 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
642 info->vendor == CFI_CMDSET_AMD_STANDARD)
643 return 1;
644#endif
645 return 0;
646}
647
648static int flash_status_poll(flash_info_t *info, void *src, void *dst,
649 ulong tout, char *prompt)
650{
651#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
652 ulong start;
653 int ready;
654
655#if CONFIG_SYS_HZ != 1000
656 if ((ulong)CONFIG_SYS_HZ > 100000)
657 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
658 else
659 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
660#endif
661
662 /* Wait for command completion */
Thomas Chou4b7e6682010-04-01 11:15:05 +0800663 reset_timer();
Thomas Chou076767a2010-03-26 08:17:00 +0800664 start = get_timer(0);
665 while (1) {
666 switch (info->portwidth) {
667 case FLASH_CFI_8BIT:
668 ready = flash_read8(dst) == flash_read8(src);
669 break;
670 case FLASH_CFI_16BIT:
671 ready = flash_read16(dst) == flash_read16(src);
672 break;
673 case FLASH_CFI_32BIT:
674 ready = flash_read32(dst) == flash_read32(src);
675 break;
676 case FLASH_CFI_64BIT:
677 ready = flash_read64(dst) == flash_read64(src);
678 break;
679 default:
680 ready = 0;
681 break;
682 }
683 if (ready)
684 break;
685 if (get_timer(start) > tout) {
686 printf("Flash %s timeout at address %lx data %lx\n",
687 prompt, (ulong)dst, (ulong)flash_read8(dst));
688 return ERR_TIMOUT;
689 }
690 udelay(1); /* also triggers watchdog */
691 }
692#endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
693 return ERR_OK;
694}
695
wdenk2cefd152004-02-08 22:55:38 +0000696/*-----------------------------------------------------------------------
697 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200698static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000699{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200700#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200701 unsigned short w;
702 unsigned int l;
703 unsigned long long ll;
704#endif
wdenk2cefd152004-02-08 22:55:38 +0000705
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200706 switch (info->portwidth) {
707 case FLASH_CFI_8BIT:
708 cword->c = c;
709 break;
710 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200711#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200712 w = c;
713 w <<= 8;
714 cword->w = (cword->w >> 8) | w;
715#else
716 cword->w = (cword->w << 8) | c;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100717#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200718 break;
719 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200720#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200721 l = c;
722 l <<= 24;
723 cword->l = (cword->l >> 8) | l;
724#else
725 cword->l = (cword->l << 8) | c;
726#endif
727 break;
728 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200729#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200730 ll = c;
731 ll <<= 56;
732 cword->ll = (cword->ll >> 8) | ll;
733#else
734 cword->ll = (cword->ll << 8) | c;
735#endif
736 break;
Stefan Roese12797482006-11-13 13:55:24 +0100737 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200738}
wdenk2cefd152004-02-08 22:55:38 +0000739
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100740/*
741 * Loop through the sector table starting from the previously found sector.
742 * Searches forwards or backwards, dependent on the passed address.
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200743 */
744static flash_sect_t find_sector (flash_info_t * info, ulong addr)
745{
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100746 static flash_sect_t saved_sector = 0; /* previously found sector */
Martin Krausebe9d5682011-03-21 18:07:56 +0100747 static flash_info_t *saved_info = 0; /* previously used flash bank */
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100748 flash_sect_t sector = saved_sector;
749
Martin Krausebe9d5682011-03-21 18:07:56 +0100750 if ((info != saved_info) || (sector >= info->sector_count))
751 sector = 0;
752
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100753 while ((info->start[sector] < addr)
754 && (sector < info->sector_count - 1))
755 sector++;
756 while ((info->start[sector] > addr) && (sector > 0))
757 /*
758 * also decrements the sector in case of an overshot
759 * in the first loop
760 */
761 sector--;
wdenk2cefd152004-02-08 22:55:38 +0000762
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100763 saved_sector = sector;
Martin Krausebe9d5682011-03-21 18:07:56 +0100764 saved_info = info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200765 return sector;
wdenk2cefd152004-02-08 22:55:38 +0000766}
767
768/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000769 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200770static int flash_write_cfiword (flash_info_t * info, ulong dest,
771 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +0000772{
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600773 void *dstaddr = (void *)dest;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200774 int flag;
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100775 flash_sect_t sect = 0;
776 char sect_found = 0;
wdenk2cefd152004-02-08 22:55:38 +0000777
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200778 /* Check if Flash is (sufficiently) erased */
779 switch (info->portwidth) {
780 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100781 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200782 break;
783 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100784 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200785 break;
786 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100787 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200788 break;
789 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100790 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200791 break;
792 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100793 flag = 0;
794 break;
wdenk2cefd152004-02-08 22:55:38 +0000795 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600796 if (!flag)
Stefan Roese707c1462007-12-27 07:50:54 +0100797 return ERR_NOT_ERASED;
wdenk2cefd152004-02-08 22:55:38 +0000798
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200799 /* Disable interrupts which might cause a timeout here */
800 flag = disable_interrupts ();
Stefan Roesec865e6c2006-02-28 15:29:58 +0100801
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200802 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400803 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200804 case CFI_CMDSET_INTEL_EXTENDED:
805 case CFI_CMDSET_INTEL_STANDARD:
806 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
807 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
808 break;
809 case CFI_CMDSET_AMD_EXTENDED:
810 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500811 sect = find_sector(info, dest);
812 flash_unlock_seq (info, sect);
813 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100814 sect_found = 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200815 break;
Po-Yu Chuang3f483402009-07-10 18:03:57 +0800816#ifdef CONFIG_FLASH_CFI_LEGACY
817 case CFI_CMDSET_AMD_LEGACY:
818 sect = find_sector(info, dest);
819 flash_unlock_seq (info, 0);
820 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
821 sect_found = 1;
822 break;
823#endif
wdenk2cefd152004-02-08 22:55:38 +0000824 }
825
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200826 switch (info->portwidth) {
827 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100828 flash_write8(cword.c, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200829 break;
830 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100831 flash_write16(cword.w, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200832 break;
833 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100834 flash_write32(cword.l, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200835 break;
836 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100837 flash_write64(cword.ll, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200838 break;
wdenk2cefd152004-02-08 22:55:38 +0000839 }
840
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200841 /* re-enable interrupts if necessary */
842 if (flag)
843 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +0000844
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100845 if (!sect_found)
846 sect = find_sector (info, dest);
847
Thomas Chou076767a2010-03-26 08:17:00 +0800848 if (use_flash_status_poll(info))
849 return flash_status_poll(info, &cword, dstaddr,
850 info->write_tout, "write");
851 else
852 return flash_full_status_check(info, sect,
853 info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +0000854}
855
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200856#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenk2cefd152004-02-08 22:55:38 +0000857
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200858static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
859 int len)
wdenk2cefd152004-02-08 22:55:38 +0000860{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200861 flash_sect_t sector;
862 int cnt;
863 int retcode;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100864 void *src = cp;
Stefan Roese42c6ace2009-02-05 11:25:57 +0100865 void *dst = (void *)dest;
Stefan Roese707c1462007-12-27 07:50:54 +0100866 void *dst2 = dst;
867 int flag = 0;
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200868 uint offset = 0;
869 unsigned int shift;
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400870 uchar write_cmd;
Stefan Roese707c1462007-12-27 07:50:54 +0100871
872 switch (info->portwidth) {
873 case FLASH_CFI_8BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200874 shift = 0;
Stefan Roese707c1462007-12-27 07:50:54 +0100875 break;
876 case FLASH_CFI_16BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200877 shift = 1;
Stefan Roese707c1462007-12-27 07:50:54 +0100878 break;
879 case FLASH_CFI_32BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200880 shift = 2;
Stefan Roese707c1462007-12-27 07:50:54 +0100881 break;
882 case FLASH_CFI_64BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200883 shift = 3;
Stefan Roese707c1462007-12-27 07:50:54 +0100884 break;
885 default:
886 retcode = ERR_INVAL;
887 goto out_unmap;
888 }
889
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200890 cnt = len >> shift;
891
Stefan Roese707c1462007-12-27 07:50:54 +0100892 while ((cnt-- > 0) && (flag == 0)) {
893 switch (info->portwidth) {
894 case FLASH_CFI_8BIT:
895 flag = ((flash_read8(dst2) & flash_read8(src)) ==
896 flash_read8(src));
897 src += 1, dst2 += 1;
898 break;
899 case FLASH_CFI_16BIT:
900 flag = ((flash_read16(dst2) & flash_read16(src)) ==
901 flash_read16(src));
902 src += 2, dst2 += 2;
903 break;
904 case FLASH_CFI_32BIT:
905 flag = ((flash_read32(dst2) & flash_read32(src)) ==
906 flash_read32(src));
907 src += 4, dst2 += 4;
908 break;
909 case FLASH_CFI_64BIT:
910 flag = ((flash_read64(dst2) & flash_read64(src)) ==
911 flash_read64(src));
912 src += 8, dst2 += 8;
913 break;
914 }
915 }
916 if (!flag) {
917 retcode = ERR_NOT_ERASED;
918 goto out_unmap;
919 }
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100920
Stefan Roese707c1462007-12-27 07:50:54 +0100921 src = cp;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100922 sector = find_sector (info, dest);
wdenke65527f2004-02-12 00:47:09 +0000923
924 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400925 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +0000926 case CFI_CMDSET_INTEL_STANDARD:
927 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400928 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
929 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200930 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400931 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
932 flash_write_cmd (info, sector, 0, write_cmd);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200933 retcode = flash_status_check (info, sector,
934 info->buffer_write_tout,
935 "write to buffer");
936 if (retcode == ERR_OK) {
937 /* reduce the number of loops by the width of
938 * the port */
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200939 cnt = len >> shift;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400940 flash_write_cmd (info, sector, 0, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200941 while (cnt-- > 0) {
942 switch (info->portwidth) {
943 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100944 flash_write8(flash_read8(src), dst);
945 src += 1, dst += 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200946 break;
947 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100948 flash_write16(flash_read16(src), dst);
949 src += 2, dst += 2;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200950 break;
951 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100952 flash_write32(flash_read32(src), dst);
953 src += 4, dst += 4;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200954 break;
955 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100956 flash_write64(flash_read64(src), dst);
957 src += 8, dst += 8;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200958 break;
959 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100960 retcode = ERR_INVAL;
961 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200962 }
963 }
964 flash_write_cmd (info, sector, 0,
965 FLASH_CMD_WRITE_BUFFER_CONFIRM);
966 retcode = flash_full_status_check (
967 info, sector, info->buffer_write_tout,
968 "buffer write");
969 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100970
971 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200972
wdenk2cefd152004-02-08 22:55:38 +0000973 case CFI_CMDSET_AMD_STANDARD:
974 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200975 flash_unlock_seq(info,0);
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200976
977#ifdef CONFIG_FLASH_SPANSION_S29WS_N
978 offset = ((unsigned long)dst - info->start[sector]) >> shift;
979#endif
980 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
981 cnt = len >> shift;
John Schmolleree355882009-08-12 10:55:47 -0500982 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200983
984 switch (info->portwidth) {
985 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100986 while (cnt-- > 0) {
987 flash_write8(flash_read8(src), dst);
988 src += 1, dst += 1;
989 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200990 break;
991 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100992 while (cnt-- > 0) {
993 flash_write16(flash_read16(src), dst);
994 src += 2, dst += 2;
995 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200996 break;
997 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100998 while (cnt-- > 0) {
999 flash_write32(flash_read32(src), dst);
1000 src += 4, dst += 4;
1001 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001002 break;
1003 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001004 while (cnt-- > 0) {
1005 flash_write64(flash_read64(src), dst);
1006 src += 8, dst += 8;
1007 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001008 break;
1009 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001010 retcode = ERR_INVAL;
1011 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001012 }
1013
1014 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Chou076767a2010-03-26 08:17:00 +08001015 if (use_flash_status_poll(info))
1016 retcode = flash_status_poll(info, src - (1 << shift),
1017 dst - (1 << shift),
1018 info->buffer_write_tout,
1019 "buffer write");
1020 else
1021 retcode = flash_full_status_check(info, sector,
1022 info->buffer_write_tout,
1023 "buffer write");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001024 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001025
wdenk2cefd152004-02-08 22:55:38 +00001026 default:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001027 debug ("Unknown Command Set\n");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001028 retcode = ERR_INVAL;
1029 break;
wdenk2cefd152004-02-08 22:55:38 +00001030 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001031
1032out_unmap:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001033 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001034}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001035#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001036
wdenke65527f2004-02-12 00:47:09 +00001037
wdenk2cefd152004-02-08 22:55:38 +00001038/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +00001039 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001040int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +00001041{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001042 int rcode = 0;
1043 int prot;
1044 flash_sect_t sect;
Thomas Chou076767a2010-03-26 08:17:00 +08001045 int st;
wdenk2cefd152004-02-08 22:55:38 +00001046
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001047 if (info->flash_id != FLASH_MAN_CFI) {
1048 puts ("Can't erase unknown flash type - aborted\n");
1049 return 1;
1050 }
1051 if ((s_first < 0) || (s_first > s_last)) {
1052 puts ("- no sectors to erase\n");
1053 return 1;
1054 }
Stefan Roeseefef95b2006-04-01 13:41:03 +02001055
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001056 prot = 0;
1057 for (sect = s_first; sect <= s_last; ++sect) {
1058 if (info->protect[sect]) {
1059 prot++;
wdenk2cefd152004-02-08 22:55:38 +00001060 }
1061 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001062 if (prot) {
1063 printf ("- Warning: %d protected sectors will not be erased!\n",
1064 prot);
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001065 } else if (flash_verbose) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001066 putc ('\n');
1067 }
wdenke65527f2004-02-12 00:47:09 +00001068
wdenke65527f2004-02-12 00:47:09 +00001069
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001070 for (sect = s_first; sect <= s_last; sect++) {
1071 if (info->protect[sect] == 0) { /* not protected */
1072 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001073 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001074 case CFI_CMDSET_INTEL_STANDARD:
1075 case CFI_CMDSET_INTEL_EXTENDED:
1076 flash_write_cmd (info, sect, 0,
1077 FLASH_CMD_CLEAR_STATUS);
1078 flash_write_cmd (info, sect, 0,
1079 FLASH_CMD_BLOCK_ERASE);
1080 flash_write_cmd (info, sect, 0,
1081 FLASH_CMD_ERASE_CONFIRM);
1082 break;
1083 case CFI_CMDSET_AMD_STANDARD:
1084 case CFI_CMDSET_AMD_EXTENDED:
1085 flash_unlock_seq (info, sect);
1086 flash_write_cmd (info, sect,
1087 info->addr_unlock1,
1088 AMD_CMD_ERASE_START);
1089 flash_unlock_seq (info, sect);
1090 flash_write_cmd (info, sect, 0,
1091 AMD_CMD_ERASE_SECTOR);
1092 break;
1093#ifdef CONFIG_FLASH_CFI_LEGACY
1094 case CFI_CMDSET_AMD_LEGACY:
1095 flash_unlock_seq (info, 0);
1096 flash_write_cmd (info, 0, info->addr_unlock1,
1097 AMD_CMD_ERASE_START);
1098 flash_unlock_seq (info, 0);
1099 flash_write_cmd (info, sect, 0,
1100 AMD_CMD_ERASE_SECTOR);
1101 break;
1102#endif
1103 default:
1104 debug ("Unkown flash vendor %d\n",
1105 info->vendor);
1106 break;
wdenke65527f2004-02-12 00:47:09 +00001107 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001108
Thomas Chou076767a2010-03-26 08:17:00 +08001109 if (use_flash_status_poll(info)) {
1110 cfiword_t cword = (cfiword_t)0xffffffffffffffffULL;
1111 void *dest;
1112 dest = flash_map(info, sect, 0);
1113 st = flash_status_poll(info, &cword, dest,
1114 info->erase_blk_tout, "erase");
1115 flash_unmap(info, sect, 0, dest);
1116 } else
1117 st = flash_full_status_check(info, sect,
1118 info->erase_blk_tout,
1119 "erase");
1120 if (st)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001121 rcode = 1;
Thomas Chou076767a2010-03-26 08:17:00 +08001122 else if (flash_verbose)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001123 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +00001124 }
wdenk2cefd152004-02-08 22:55:38 +00001125 }
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001126
1127 if (flash_verbose)
1128 puts (" done\n");
1129
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001130 return rcode;
wdenk2cefd152004-02-08 22:55:38 +00001131}
wdenke65527f2004-02-12 00:47:09 +00001132
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001133#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1134static int sector_erased(flash_info_t *info, int i)
1135{
1136 int k;
1137 int size;
Stefan Roesea9153f22010-10-25 18:31:39 +02001138 u32 *flash;
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001139
1140 /*
1141 * Check if whole sector is erased
1142 */
1143 size = flash_sector_size(info, i);
Stefan Roesea9153f22010-10-25 18:31:39 +02001144 flash = (u32 *)info->start[i];
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001145 /* divide by 4 for longword access */
1146 size = size >> 2;
1147
1148 for (k = 0; k < size; k++) {
Stefan Roesea9153f22010-10-25 18:31:39 +02001149 if (flash_read32(flash++) != 0xffffffff)
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001150 return 0; /* not erased */
1151 }
1152
1153 return 1; /* erased */
1154}
1155#endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1156
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001157void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001158{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001159 int i;
wdenk369d43d2004-03-14 14:09:05 +00001160
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001161 if (info->flash_id != FLASH_MAN_CFI) {
1162 puts ("missing or unknown FLASH type\n");
1163 return;
1164 }
1165
Peter Tyser0b5a2632010-12-28 18:12:05 -06001166 printf ("%s flash (%d x %d)",
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001167 info->name,
1168 (info->portwidth << 3), (info->chipwidth << 3));
1169 if (info->size < 1024*1024)
1170 printf (" Size: %ld kB in %d Sectors\n",
1171 info->size >> 10, info->sector_count);
1172 else
1173 printf (" Size: %ld MB in %d Sectors\n",
1174 info->size >> 20, info->sector_count);
1175 printf (" ");
1176 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001177 case CFI_CMDSET_INTEL_PROG_REGIONS:
1178 printf ("Intel Prog Regions");
1179 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001180 case CFI_CMDSET_INTEL_STANDARD:
1181 printf ("Intel Standard");
1182 break;
1183 case CFI_CMDSET_INTEL_EXTENDED:
1184 printf ("Intel Extended");
1185 break;
1186 case CFI_CMDSET_AMD_STANDARD:
1187 printf ("AMD Standard");
1188 break;
1189 case CFI_CMDSET_AMD_EXTENDED:
1190 printf ("AMD Extended");
1191 break;
1192#ifdef CONFIG_FLASH_CFI_LEGACY
1193 case CFI_CMDSET_AMD_LEGACY:
1194 printf ("AMD Legacy");
1195 break;
wdenk369d43d2004-03-14 14:09:05 +00001196#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001197 default:
1198 printf ("Unknown (%d)", info->vendor);
1199 break;
wdenk2cefd152004-02-08 22:55:38 +00001200 }
Philippe De Muyter35bc0812010-08-10 16:54:52 +02001201 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1202 info->manufacturer_id);
1203 printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1204 info->device_id);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001205 if (info->device_id == 0x7E) {
1206 printf("%04X", info->device_id2);
1207 }
1208 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1209 info->erase_blk_tout,
1210 info->write_tout);
1211 if (info->buffer_size > 1) {
1212 printf (" Buffer write timeout: %ld ms, "
1213 "buffer size: %d bytes\n",
1214 info->buffer_write_tout,
1215 info->buffer_size);
1216 }
wdenk2cefd152004-02-08 22:55:38 +00001217
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001218 puts ("\n Sector Start Addresses:");
1219 for (i = 0; i < info->sector_count; ++i) {
Kim Phillipsc8836f12010-07-26 18:35:39 -05001220 if (ctrlc())
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001221 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001222 if ((i % 5) == 0)
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001223 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001224#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001225 /* print empty and read-only info */
1226 printf (" %08lX %c %s ",
1227 info->start[i],
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001228 sector_erased(info, i) ? 'E' : ' ',
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001229 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001230#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001231 printf (" %08lX %s ",
1232 info->start[i],
1233 info->protect[i] ? "RO" : " ");
wdenke65527f2004-02-12 00:47:09 +00001234#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001235 }
1236 putc ('\n');
1237 return;
wdenk2cefd152004-02-08 22:55:38 +00001238}
1239
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001240/*-----------------------------------------------------------------------
Jerry Van Barenaae73572008-03-08 13:48:01 -05001241 * This is used in a few places in write_buf() to show programming
1242 * progress. Making it a function is nasty because it needs to do side
1243 * effect updates to digit and dots. Repeated code is nasty too, so
1244 * we define it once here.
1245 */
Stefan Roese7758c162008-03-19 07:09:26 +01001246#ifdef CONFIG_FLASH_SHOW_PROGRESS
1247#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001248 if (flash_verbose) { \
1249 dots -= dots_sub; \
1250 if ((scale > 0) && (dots <= 0)) { \
1251 if ((digit % 5) == 0) \
1252 printf ("%d", digit / 5); \
1253 else \
1254 putc ('.'); \
1255 digit--; \
1256 dots += scale; \
1257 } \
Jerry Van Barenaae73572008-03-08 13:48:01 -05001258 }
Stefan Roese7758c162008-03-19 07:09:26 +01001259#else
1260#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1261#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001262
1263/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001264 * Copy memory to flash, returns:
1265 * 0 - OK
1266 * 1 - write timeout
1267 * 2 - Flash not erased
wdenk2cefd152004-02-08 22:55:38 +00001268 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001269int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +00001270{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001271 ulong wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001272 uchar *p;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001273 int aln;
wdenk2cefd152004-02-08 22:55:38 +00001274 cfiword_t cword;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001275 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001276#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001277 int buffered_size;
wdenk2cefd152004-02-08 22:55:38 +00001278#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001279#ifdef CONFIG_FLASH_SHOW_PROGRESS
1280 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1281 int scale = 0;
1282 int dots = 0;
1283
1284 /*
1285 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1286 */
1287 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1288 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1289 CONFIG_FLASH_SHOW_PROGRESS);
1290 }
1291#endif
1292
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001293 /* get lower aligned address */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001294 wp = (addr & ~(info->portwidth - 1));
Haiying Wangc123a382007-02-21 16:52:31 +01001295
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001296 /* handle unaligned start */
1297 if ((aln = addr - wp) != 0) {
1298 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001299 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001300 for (i = 0; i < aln; ++i)
1301 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk2cefd152004-02-08 22:55:38 +00001302
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001303 for (; (i < info->portwidth) && (cnt > 0); i++) {
1304 flash_add_byte (info, &cword, *src++);
1305 cnt--;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001306 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001307 for (; (cnt == 0) && (i < info->portwidth); ++i)
1308 flash_add_byte (info, &cword, flash_read8(p + i));
1309
1310 rc = flash_write_cfiword (info, wp, cword);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001311 if (rc != 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001312 return rc;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001313
1314 wp += i;
Stefan Roese7758c162008-03-19 07:09:26 +01001315 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001316 }
1317
1318 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001319#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001320 buffered_size = (info->portwidth / info->chipwidth);
1321 buffered_size *= info->buffer_size;
1322 while (cnt >= info->portwidth) {
1323 /* prohibit buffer write when buffer_size is 1 */
1324 if (info->buffer_size == 1) {
1325 cword.l = 0;
1326 for (i = 0; i < info->portwidth; i++)
1327 flash_add_byte (info, &cword, *src++);
1328 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1329 return rc;
1330 wp += info->portwidth;
1331 cnt -= info->portwidth;
1332 continue;
1333 }
1334
1335 /* write buffer until next buffered_size aligned boundary */
1336 i = buffered_size - (wp % buffered_size);
1337 if (i > cnt)
1338 i = cnt;
1339 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1340 return rc;
1341 i -= i & (info->portwidth - 1);
1342 wp += i;
1343 src += i;
1344 cnt -= i;
Stefan Roese7758c162008-03-19 07:09:26 +01001345 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001346 }
1347#else
1348 while (cnt >= info->portwidth) {
1349 cword.l = 0;
1350 for (i = 0; i < info->portwidth; i++) {
1351 flash_add_byte (info, &cword, *src++);
1352 }
1353 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1354 return rc;
1355 wp += info->portwidth;
1356 cnt -= info->portwidth;
Stefan Roese7758c162008-03-19 07:09:26 +01001357 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001358 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001359#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Barenaae73572008-03-08 13:48:01 -05001360
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001361 if (cnt == 0) {
1362 return (0);
1363 }
1364
1365 /*
1366 * handle unaligned tail bytes
1367 */
1368 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001369 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001370 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001371 flash_add_byte (info, &cword, *src++);
1372 --cnt;
1373 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001374 for (; i < info->portwidth; ++i)
1375 flash_add_byte (info, &cword, flash_read8(p + i));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001376
1377 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +00001378}
wdenke65527f2004-02-12 00:47:09 +00001379
wdenk2cefd152004-02-08 22:55:38 +00001380/*-----------------------------------------------------------------------
1381 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001382#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001383
1384int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +00001385{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001386 int retcode = 0;
wdenke65527f2004-02-12 00:47:09 +00001387
Rafael Campos13d2b612008-07-31 10:22:20 +02001388 switch (info->vendor) {
1389 case CFI_CMDSET_INTEL_PROG_REGIONS:
1390 case CFI_CMDSET_INTEL_STANDARD:
Nick Spenceec81b472008-08-19 22:21:16 -07001391 case CFI_CMDSET_INTEL_EXTENDED:
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001392 /*
1393 * see errata called
1394 * "Numonyx Axcell P33/P30 Specification Update" :)
1395 */
1396 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_ID);
1397 if (!flash_isequal (info, sector, FLASH_OFFSET_PROTECT,
1398 prot)) {
1399 /*
1400 * cmd must come before FLASH_CMD_PROTECT + 20us
1401 * Disable interrupts which might cause a timeout here.
1402 */
1403 int flag = disable_interrupts ();
1404 unsigned short cmd;
1405
1406 if (prot)
1407 cmd = FLASH_CMD_PROTECT_SET;
1408 else
1409 cmd = FLASH_CMD_PROTECT_CLEAR;
1410
Rafael Campos13d2b612008-07-31 10:22:20 +02001411 flash_write_cmd (info, sector, 0,
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001412 FLASH_CMD_PROTECT);
1413 flash_write_cmd (info, sector, 0, cmd);
1414 /* re-enable interrupts if necessary */
1415 if (flag)
1416 enable_interrupts ();
1417 }
Rafael Campos13d2b612008-07-31 10:22:20 +02001418 break;
1419 case CFI_CMDSET_AMD_EXTENDED:
1420 case CFI_CMDSET_AMD_STANDARD:
Rafael Campos13d2b612008-07-31 10:22:20 +02001421 /* U-Boot only checks the first byte */
1422 if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
1423 if (prot) {
1424 flash_unlock_seq (info, 0);
1425 flash_write_cmd (info, 0,
1426 info->addr_unlock1,
1427 ATM_CMD_SOFTLOCK_START);
1428 flash_unlock_seq (info, 0);
1429 flash_write_cmd (info, sector, 0,
1430 ATM_CMD_LOCK_SECT);
1431 } else {
1432 flash_write_cmd (info, 0,
1433 info->addr_unlock1,
1434 AMD_CMD_UNLOCK_START);
1435 if (info->device_id == ATM_ID_BV6416)
1436 flash_write_cmd (info, sector,
1437 0, ATM_CMD_UNLOCK_SECT);
1438 }
1439 }
1440 break;
TsiChung Liewb8c19292008-08-19 16:53:39 +00001441#ifdef CONFIG_FLASH_CFI_LEGACY
1442 case CFI_CMDSET_AMD_LEGACY:
1443 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1444 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1445 if (prot)
1446 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1447 else
1448 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1449#endif
Rafael Campos13d2b612008-07-31 10:22:20 +02001450 };
wdenk2cefd152004-02-08 22:55:38 +00001451
Stefan Roese5215df12010-10-25 18:31:29 +02001452 /*
1453 * Flash needs to be in status register read mode for
1454 * flash_full_status_check() to work correctly
1455 */
1456 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001457 if ((retcode =
1458 flash_full_status_check (info, sector, info->erase_blk_tout,
1459 prot ? "protect" : "unprotect")) == 0) {
wdenke65527f2004-02-12 00:47:09 +00001460
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001461 info->protect[sector] = prot;
1462
1463 /*
1464 * On some of Intel's flash chips (marked via legacy_unlock)
1465 * unprotect unprotects all locking.
1466 */
1467 if ((prot == 0) && (info->legacy_unlock)) {
1468 flash_sect_t i;
1469
1470 for (i = 0; i < info->sector_count; i++) {
1471 if (info->protect[i])
1472 flash_real_protect (info, i, 1);
1473 }
wdenk2cefd152004-02-08 22:55:38 +00001474 }
wdenk2cefd152004-02-08 22:55:38 +00001475 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001476 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001477}
wdenke65527f2004-02-12 00:47:09 +00001478
wdenk2cefd152004-02-08 22:55:38 +00001479/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001480 * flash_read_user_serial - read the OneTimeProgramming cells
wdenk2cefd152004-02-08 22:55:38 +00001481 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001482void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1483 int len)
wdenk2cefd152004-02-08 22:55:38 +00001484{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001485 uchar *src;
1486 uchar *dst;
wdenke65527f2004-02-12 00:47:09 +00001487
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001488 dst = buffer;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001489 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001490 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1491 memcpy (dst, src + offset, len);
1492 flash_write_cmd (info, 0, 0, info->cmd_reset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001493 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001494}
1495
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001496/*
1497 * flash_read_factory_serial - read the device Id from the protection area
wdenk2cefd152004-02-08 22:55:38 +00001498 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001499void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1500 int len)
wdenk2cefd152004-02-08 22:55:38 +00001501{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001502 uchar *src;
wdenke65527f2004-02-12 00:47:09 +00001503
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001504 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001505 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1506 memcpy (buffer, src + offset, len);
1507 flash_write_cmd (info, 0, 0, info->cmd_reset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001508 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001509}
1510
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001511#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001512
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001513/*-----------------------------------------------------------------------
1514 * Reverse the order of the erase regions in the CFI QRY structure.
1515 * This is needed for chips that are either a) correctly detected as
1516 * top-boot, or b) buggy.
1517 */
1518static void cfi_reverse_geometry(struct cfi_qry *qry)
1519{
1520 unsigned int i, j;
1521 u32 tmp;
1522
1523 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1524 tmp = qry->erase_region_info[i];
1525 qry->erase_region_info[i] = qry->erase_region_info[j];
1526 qry->erase_region_info[j] = tmp;
1527 }
1528}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001529
wdenk2cefd152004-02-08 22:55:38 +00001530/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +01001531 * read jedec ids from device and set corresponding fields in info struct
1532 *
1533 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1534 *
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001535 */
1536static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1537{
1538 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1539 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1540 udelay(1000); /* some flash are slow to respond */
1541 info->manufacturer_id = flash_read_uchar (info,
1542 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyter35bc0812010-08-10 16:54:52 +02001543 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1544 flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
1545 flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001546 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1547}
1548
1549static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1550{
1551 info->cmd_reset = FLASH_CMD_RESET;
1552
1553 cmdset_intel_read_jedec_ids(info);
1554 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1555
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001556#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001557 /* read legacy lock/unlock bit from intel flash */
1558 if (info->ext_addr) {
1559 info->legacy_unlock = flash_read_uchar (info,
1560 info->ext_addr + 5) & 0x08;
1561 }
1562#endif
1563
1564 return 0;
1565}
1566
1567static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1568{
Niklaus Gigerf447f712009-07-22 17:13:24 +02001569 ushort bankId = 0;
1570 uchar manuId;
1571
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001572 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1573 flash_unlock_seq(info, 0);
1574 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1575 udelay(1000); /* some flash are slow to respond */
Tor Krill7f2a3052008-03-28 11:29:10 +01001576
Niklaus Gigerf447f712009-07-22 17:13:24 +02001577 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1578 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1579 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1580 bankId += 0x100;
1581 manuId = flash_read_uchar (info,
1582 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1583 }
1584 info->manufacturer_id = manuId;
Tor Krill7f2a3052008-03-28 11:29:10 +01001585
1586 switch (info->chipwidth){
1587 case FLASH_CFI_8BIT:
1588 info->device_id = flash_read_uchar (info,
1589 FLASH_OFFSET_DEVICE_ID);
1590 if (info->device_id == 0x7E) {
1591 /* AMD 3-byte (expanded) device ids */
1592 info->device_id2 = flash_read_uchar (info,
1593 FLASH_OFFSET_DEVICE_ID2);
1594 info->device_id2 <<= 8;
1595 info->device_id2 |= flash_read_uchar (info,
1596 FLASH_OFFSET_DEVICE_ID3);
1597 }
1598 break;
1599 case FLASH_CFI_16BIT:
1600 info->device_id = flash_read_word (info,
1601 FLASH_OFFSET_DEVICE_ID);
1602 break;
1603 default:
1604 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001605 }
1606 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1607}
1608
1609static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1610{
1611 info->cmd_reset = AMD_CMD_RESET;
1612
1613 cmdset_amd_read_jedec_ids(info);
1614 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1615
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001616 return 0;
1617}
1618
1619#ifdef CONFIG_FLASH_CFI_LEGACY
Stefan Roese12797482006-11-13 13:55:24 +01001620static void flash_read_jedec_ids (flash_info_t * info)
1621{
1622 info->manufacturer_id = 0;
1623 info->device_id = 0;
1624 info->device_id2 = 0;
1625
1626 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001627 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese12797482006-11-13 13:55:24 +01001628 case CFI_CMDSET_INTEL_STANDARD:
1629 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001630 cmdset_intel_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001631 break;
1632 case CFI_CMDSET_AMD_STANDARD:
1633 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001634 cmdset_amd_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001635 break;
1636 default:
1637 break;
1638 }
1639}
1640
1641/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001642 * Call board code to request info about non-CFI flash.
1643 * board_flash_get_legacy needs to fill in at least:
1644 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001645 */
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001646static int flash_detect_legacy(phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001647{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001648 flash_info_t *info = &flash_info[banknum];
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001649
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001650 if (board_flash_get_legacy(base, banknum, info)) {
1651 /* board code may have filled info completely. If not, we
1652 use JEDEC ID probing. */
1653 if (!info->vendor) {
1654 int modes[] = {
1655 CFI_CMDSET_AMD_STANDARD,
1656 CFI_CMDSET_INTEL_STANDARD
1657 };
1658 int i;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001659
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001660 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1661 info->vendor = modes[i];
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001662 info->start[0] =
1663 (ulong)map_physmem(base,
Stefan Roeseb8443702009-02-05 11:44:52 +01001664 info->portwidth,
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001665 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001666 if (info->portwidth == FLASH_CFI_8BIT
1667 && info->interface == FLASH_CFI_X8X16) {
1668 info->addr_unlock1 = 0x2AAA;
1669 info->addr_unlock2 = 0x5555;
1670 } else {
1671 info->addr_unlock1 = 0x5555;
1672 info->addr_unlock2 = 0x2AAA;
1673 }
1674 flash_read_jedec_ids(info);
1675 debug("JEDEC PROBE: ID %x %x %x\n",
1676 info->manufacturer_id,
1677 info->device_id,
1678 info->device_id2);
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001679 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001680 break;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001681 else
Stefan Roeseb8443702009-02-05 11:44:52 +01001682 unmap_physmem((void *)info->start[0],
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001683 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001684 }
1685 }
1686
1687 switch(info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001688 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001689 case CFI_CMDSET_INTEL_STANDARD:
1690 case CFI_CMDSET_INTEL_EXTENDED:
1691 info->cmd_reset = FLASH_CMD_RESET;
1692 break;
1693 case CFI_CMDSET_AMD_STANDARD:
1694 case CFI_CMDSET_AMD_EXTENDED:
1695 case CFI_CMDSET_AMD_LEGACY:
1696 info->cmd_reset = AMD_CMD_RESET;
1697 break;
1698 }
1699 info->flash_id = FLASH_MAN_CFI;
1700 return 1;
1701 }
1702 return 0; /* use CFI */
1703}
1704#else
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001705static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001706{
1707 return 0; /* use CFI */
1708}
1709#endif
1710
1711/*-----------------------------------------------------------------------
1712 * detect if flash is compatible with the Common Flash Interface (CFI)
1713 * http://www.jedec.org/download/search/jesd68.pdf
1714 */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001715static void flash_read_cfi (flash_info_t *info, void *buf,
1716 unsigned int start, size_t len)
1717{
1718 u8 *p = buf;
1719 unsigned int i;
1720
1721 for (i = 0; i < len; i++)
1722 p[i] = flash_read_uchar(info, start + i);
1723}
Stefan Roese6e83e342009-10-27 15:15:55 +01001724
1725void __flash_cmd_reset(flash_info_t *info)
1726{
1727 /*
1728 * We do not yet know what kind of commandset to use, so we issue
1729 * the reset command in both Intel and AMD variants, in the hope
1730 * that AMD flash roms ignore the Intel command.
1731 */
1732 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1733 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1734}
1735void flash_cmd_reset(flash_info_t *info)
1736 __attribute__((weak,alias("__flash_cmd_reset")));
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001737
1738static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001739{
1740 int cfi_offset;
1741
Stefan Roese6e83e342009-10-27 15:15:55 +01001742 /* Issue FLASH reset command */
1743 flash_cmd_reset(info);
Michael Schwingen4661cf72008-02-18 23:16:35 +01001744
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001745 for (cfi_offset=0;
1746 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1747 cfi_offset++) {
1748 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1749 FLASH_CMD_CFI);
1750 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1751 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1752 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001753 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1754 sizeof(struct cfi_qry));
1755 info->interface = le16_to_cpu(qry->interface_desc);
1756
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001757 info->cfi_offset = flash_offset_cfi[cfi_offset];
1758 debug ("device interface is %d\n",
1759 info->interface);
1760 debug ("found port %d chip %d ",
1761 info->portwidth, info->chipwidth);
1762 debug ("port %d bits chip %d bits\n",
1763 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1764 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1765
1766 /* calculate command offsets as in the Linux driver */
1767 info->addr_unlock1 = 0x555;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001768 info->addr_unlock2 = 0x2aa;
1769
1770 /*
1771 * modify the unlock address if we are
1772 * in compatibility mode
1773 */
1774 if ( /* x8/x16 in x8 mode */
1775 ((info->chipwidth == FLASH_CFI_BY8) &&
1776 (info->interface == FLASH_CFI_X8X16)) ||
1777 /* x16/x32 in x16 mode */
1778 ((info->chipwidth == FLASH_CFI_BY16) &&
1779 (info->interface == FLASH_CFI_X16X32)))
1780 {
1781 info->addr_unlock1 = 0xaaa;
1782 info->addr_unlock2 = 0x555;
1783 }
1784
1785 info->name = "CFI conformant";
1786 return 1;
1787 }
1788 }
1789
1790 return 0;
1791}
1792
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001793static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001794{
wdenke65527f2004-02-12 00:47:09 +00001795 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001796
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001797 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenke65527f2004-02-12 00:47:09 +00001798 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1799 for (info->chipwidth = FLASH_CFI_BY8;
1800 info->chipwidth <= info->portwidth;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001801 info->chipwidth <<= 1)
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001802 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001803 return 1;
wdenk2cefd152004-02-08 22:55:38 +00001804 }
wdenke65527f2004-02-12 00:47:09 +00001805 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001806 return 0;
1807}
wdenke65527f2004-02-12 00:47:09 +00001808
wdenk2cefd152004-02-08 22:55:38 +00001809/*
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001810 * Manufacturer-specific quirks. Add workarounds for geometry
1811 * reversal, etc. here.
1812 */
1813static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1814{
1815 /* check if flash geometry needs reversal */
1816 if (qry->num_erase_regions > 1) {
1817 /* reverse geometry if top boot part */
1818 if (info->cfi_version < 0x3131) {
1819 /* CFI < 1.1, try to guess from device id */
1820 if ((info->device_id & 0x80) != 0)
1821 cfi_reverse_geometry(qry);
1822 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1823 /* CFI >= 1.1, deduct from top/bottom flag */
1824 /* note: ext_addr is valid since cfi_version > 0 */
1825 cfi_reverse_geometry(qry);
1826 }
1827 }
1828}
1829
1830static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1831{
1832 int reverse_geometry = 0;
1833
1834 /* Check the "top boot" bit in the PRI */
1835 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1836 reverse_geometry = 1;
1837
1838 /* AT49BV6416(T) list the erase regions in the wrong order.
1839 * However, the device ID is identical with the non-broken
Ulf Samuelsson07f9b4e2009-03-27 23:26:43 +01001840 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001841 */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001842 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1843 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001844
1845 if (reverse_geometry)
1846 cfi_reverse_geometry(qry);
1847}
1848
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001849static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1850{
1851 /* check if flash geometry needs reversal */
1852 if (qry->num_erase_regions > 1) {
1853 /* reverse geometry if top boot part */
1854 if (info->cfi_version < 0x3131) {
Richard Retanubun47255a52009-03-06 10:09:37 -05001855 /* CFI < 1.1, guess by device id (M29W320{DT,ET} only) */
1856 if (info->device_id == 0x22CA ||
1857 info->device_id == 0x2256) {
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001858 cfi_reverse_geometry(qry);
1859 }
1860 }
1861 }
1862}
1863
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001864/*
wdenk2cefd152004-02-08 22:55:38 +00001865 * The following code cannot be run from FLASH!
1866 *
1867 */
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01001868ulong flash_get_size (phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001869{
wdenke65527f2004-02-12 00:47:09 +00001870 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00001871 int i, j;
1872 flash_sect_t sect_cnt;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001873 phys_addr_t sector;
wdenk2cefd152004-02-08 22:55:38 +00001874 unsigned long tmp;
1875 int size_ratio;
1876 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00001877 int erase_region_size;
1878 int erase_region_count;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001879 struct cfi_qry qry;
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01001880 unsigned long max_size;
Stefan Roese12797482006-11-13 13:55:24 +01001881
Kumar Gala899032b2008-05-15 15:13:08 -05001882 memset(&qry, 0, sizeof(qry));
1883
Stefan Roese12797482006-11-13 13:55:24 +01001884 info->ext_addr = 0;
1885 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001886#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02001887 info->legacy_unlock = 0;
1888#endif
wdenk2cefd152004-02-08 22:55:38 +00001889
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001890 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00001891
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001892 if (flash_detect_cfi (info, &qry)) {
1893 info->vendor = le16_to_cpu(qry.p_id);
1894 info->ext_addr = le16_to_cpu(qry.p_adr);
1895 num_erase_regions = qry.num_erase_regions;
1896
Stefan Roese12797482006-11-13 13:55:24 +01001897 if (info->ext_addr) {
1898 info->cfi_version = (ushort) flash_read_uchar (info,
1899 info->ext_addr + 3) << 8;
1900 info->cfi_version |= (ushort) flash_read_uchar (info,
1901 info->ext_addr + 4);
1902 }
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001903
wdenke65527f2004-02-12 00:47:09 +00001904#ifdef DEBUG
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001905 flash_printqry (&qry);
wdenke65527f2004-02-12 00:47:09 +00001906#endif
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001907
wdenke65527f2004-02-12 00:47:09 +00001908 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001909 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +00001910 case CFI_CMDSET_INTEL_STANDARD:
1911 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001912 cmdset_intel_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00001913 break;
1914 case CFI_CMDSET_AMD_STANDARD:
1915 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001916 cmdset_amd_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00001917 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001918 default:
1919 printf("CFI: Unknown command set 0x%x\n",
1920 info->vendor);
1921 /*
1922 * Unfortunately, this means we don't know how
1923 * to get the chip back to Read mode. Might
1924 * as well try an Intel-style reset...
1925 */
1926 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1927 return 0;
wdenk2cefd152004-02-08 22:55:38 +00001928 }
wdenk6cfa84e2004-02-10 00:03:41 +00001929
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001930 /* Do manufacturer-specific fixups */
1931 switch (info->manufacturer_id) {
Mario Schuknecht5c3579e2011-02-21 13:13:14 +01001932 case 0x0001: /* AMD */
1933 case 0x0037: /* AMIC */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001934 flash_fixup_amd(info, &qry);
1935 break;
1936 case 0x001f:
1937 flash_fixup_atmel(info, &qry);
1938 break;
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001939 case 0x0020:
1940 flash_fixup_stm(info, &qry);
1941 break;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001942 }
1943
wdenke65527f2004-02-12 00:47:09 +00001944 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese12797482006-11-13 13:55:24 +01001945 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1946 debug ("device id is 0x%x\n", info->device_id);
1947 debug ("device id2 is 0x%x\n", info->device_id2);
1948 debug ("cfi version is 0x%04x\n", info->cfi_version);
1949
wdenk2cefd152004-02-08 22:55:38 +00001950 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00001951 /* if the chip is x8/x16 reduce the ratio by half */
1952 if ((info->interface == FLASH_CFI_X8X16)
1953 && (info->chipwidth == FLASH_CFI_BY8)) {
1954 size_ratio >>= 1;
1955 }
wdenke65527f2004-02-12 00:47:09 +00001956 debug ("size_ratio %d port %d bits chip %d bits\n",
1957 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1958 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanok755c1802010-10-21 17:20:12 +02001959 info->size = 1 << qry.dev_size;
1960 /* multiply the size by the number of chips */
1961 info->size *= size_ratio;
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01001962 max_size = cfi_flash_bank_size(banknum);
Ilya Yanok755c1802010-10-21 17:20:12 +02001963 if (max_size && (info->size > max_size)) {
1964 debug("[truncated from %ldMiB]", info->size >> 20);
1965 info->size = max_size;
1966 }
wdenke65527f2004-02-12 00:47:09 +00001967 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00001968 sect_cnt = 0;
1969 sector = base;
wdenke65527f2004-02-12 00:47:09 +00001970 for (i = 0; i < num_erase_regions; i++) {
1971 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00001972 printf ("%d erase regions found, only %d used\n",
1973 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00001974 break;
1975 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001976
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001977 tmp = le32_to_cpu(qry.erase_region_info[i]);
1978 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001979
1980 erase_region_count = (tmp & 0xffff) + 1;
1981 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00001982 erase_region_size =
1983 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenkaeba06f2004-06-09 17:34:58 +00001984 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00001985 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00001986 for (j = 0; j < erase_region_count; j++) {
Ilya Yanok755c1802010-10-21 17:20:12 +02001987 if (sector - base >= info->size)
1988 break;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001989 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen73d044d2007-12-07 23:35:02 +01001990 printf("ERROR: too many flash sectors\n");
1991 break;
1992 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001993 info->start[sect_cnt] =
1994 (ulong)map_physmem(sector,
1995 info->portwidth,
1996 MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00001997 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00001998
1999 /*
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002000 * Only read protection status from
2001 * supported devices (intel...)
wdenk26c58432005-01-09 17:12:27 +00002002 */
2003 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04002004 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk26c58432005-01-09 17:12:27 +00002005 case CFI_CMDSET_INTEL_EXTENDED:
2006 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roese5215df12010-10-25 18:31:29 +02002007 /*
2008 * Set flash to read-id mode. Otherwise
2009 * reading protected status is not
2010 * guaranteed.
2011 */
2012 flash_write_cmd(info, sect_cnt, 0,
2013 FLASH_CMD_READ_ID);
wdenk26c58432005-01-09 17:12:27 +00002014 info->protect[sect_cnt] =
2015 flash_isset (info, sect_cnt,
2016 FLASH_OFFSET_PROTECT,
2017 FLASH_STATUS_PROTECT);
2018 break;
2019 default:
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002020 /* default: not protected */
2021 info->protect[sect_cnt] = 0;
wdenk26c58432005-01-09 17:12:27 +00002022 }
2023
wdenk2cefd152004-02-08 22:55:38 +00002024 sect_cnt++;
2025 }
2026 }
2027
2028 info->sector_count = sect_cnt;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002029 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2030 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002031 info->erase_blk_tout = tmp *
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002032 (1 << qry.block_erase_timeout_max);
2033 tmp = (1 << qry.buf_write_timeout_typ) *
2034 (1 << qry.buf_write_timeout_max);
2035
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002036 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002037 info->buffer_write_tout = (tmp + 999) / 1000;
2038 tmp = (1 << qry.word_write_timeout_typ) *
2039 (1 << qry.word_write_timeout_max);
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002040 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002041 info->write_tout = (tmp + 999) / 1000;
wdenk2cefd152004-02-08 22:55:38 +00002042 info->flash_id = FLASH_MAN_CFI;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002043 if ((info->interface == FLASH_CFI_X8X16) &&
2044 (info->chipwidth == FLASH_CFI_BY8)) {
2045 /* XXX - Need to test on x8/x16 in parallel. */
2046 info->portwidth >>= 1;
wdenked2ac4b2004-03-14 18:23:55 +00002047 }
Mike Frysinger59404ee2008-10-02 01:55:38 -04002048
2049 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +00002050 }
2051
wdenke65527f2004-02-12 00:47:09 +00002052 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00002053}
2054
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002055#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002056void flash_set_verbose(uint v)
2057{
2058 flash_verbose = v;
2059}
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002060#endif
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002061
Stefan Roeseab935642010-10-25 18:31:48 +02002062static void cfi_flash_set_config_reg(u32 base, u16 val)
2063{
2064#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2065 /*
2066 * Only set this config register if really defined
2067 * to a valid value (0xffff is invalid)
2068 */
2069 if (val == 0xffff)
2070 return;
2071
2072 /*
2073 * Set configuration register. Data is "encrypted" in the 16 lower
2074 * address bits.
2075 */
2076 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2077 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2078
2079 /*
2080 * Finally issue reset-command to bring device back to
2081 * read-array mode
2082 */
2083 flash_write16(FLASH_CMD_RESET, (void *)base);
2084#endif
2085}
2086
wdenk2cefd152004-02-08 22:55:38 +00002087/*-----------------------------------------------------------------------
2088 */
Heiko Schocheref0946a2011-04-04 08:10:21 +02002089
2090void flash_protect_default(void)
2091{
2092 /* Monitor protection ON by default */
2093#if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2094 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2095 flash_protect(FLAG_PROTECT_SET,
2096 CONFIG_SYS_MONITOR_BASE,
2097 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2098 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2099#endif
2100
2101 /* Environment protection ON by default */
2102#ifdef CONFIG_ENV_IS_IN_FLASH
2103 flash_protect(FLAG_PROTECT_SET,
2104 CONFIG_ENV_ADDR,
2105 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2106 flash_get_info(CONFIG_ENV_ADDR));
2107#endif
2108
2109 /* Redundant environment protection ON by default */
2110#ifdef CONFIG_ENV_ADDR_REDUND
2111 flash_protect(FLAG_PROTECT_SET,
2112 CONFIG_ENV_ADDR_REDUND,
2113 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2114 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2115#endif
2116
2117#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2118 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
2119 debug("autoprotecting from %08x to %08x\n",
2120 apl[i].start, apl[i].start + apl[i].size - 1);
2121 flash_protect(FLAG_PROTECT_SET,
2122 apl[i].start,
2123 apl[i].start + apl[i].size - 1,
2124 flash_get_info(apl[i].start));
2125 }
2126#endif
2127}
2128
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002129unsigned long flash_init (void)
wdenk2cefd152004-02-08 22:55:38 +00002130{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002131 unsigned long size = 0;
2132 int i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002133#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
Matthias Fuchs50431522008-04-18 16:29:40 +02002134 struct apl_s {
2135 ulong start;
2136 ulong size;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002137 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
Matthias Fuchs50431522008-04-18 16:29:40 +02002138#endif
wdenk2cefd152004-02-08 22:55:38 +00002139
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002140#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann8f7ee7d2009-03-21 09:59:34 -04002141 /* read environment from EEPROM */
2142 char s[64];
Wolfgang Denk76af2782010-07-24 21:55:43 +02002143 getenv_f("unlock", s, sizeof(s));
Michael Schwingen73d044d2007-12-07 23:35:02 +01002144#endif
wdenk2cefd152004-02-08 22:55:38 +00002145
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002146 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002147 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002148 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk2cefd152004-02-08 22:55:38 +00002149
Stefan Roeseab935642010-10-25 18:31:48 +02002150 /* Optionally write flash configuration register */
2151 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2152 cfi_flash_config_reg(i));
2153
Stefan Roese7e7dda82010-08-30 10:11:51 +02002154 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002155 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002156 size += flash_info[i].size;
2157 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002158#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Peter Tyser0b5a2632010-12-28 18:12:05 -06002159 printf ("## Unknown flash on Bank %d "
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002160 "- Size = 0x%08lx = %ld MB\n",
2161 i+1, flash_info[i].size,
John Schmoller61665db2010-09-29 13:49:05 -05002162 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002163#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002164 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002165#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002166 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
2167 /*
2168 * Only the U-Boot image and it's environment
2169 * is protected, all other sectors are
2170 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002171 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002172 * and the environment variable "unlock" is
2173 * set to "yes".
2174 */
2175 if (flash_info[i].legacy_unlock) {
2176 int k;
wdenk2cefd152004-02-08 22:55:38 +00002177
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002178 /*
2179 * Disable legacy_unlock temporarily,
2180 * since flash_real_protect would
2181 * relock all other sectors again
2182 * otherwise.
2183 */
2184 flash_info[i].legacy_unlock = 0;
wdenk2cefd152004-02-08 22:55:38 +00002185
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002186 /*
2187 * Legacy unlocking (e.g. Intel J3) ->
2188 * unlock only one sector. This will
2189 * unlock all sectors.
2190 */
2191 flash_real_protect (&flash_info[i], 0, 0);
wdenk2cefd152004-02-08 22:55:38 +00002192
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002193 flash_info[i].legacy_unlock = 1;
wdenk2cefd152004-02-08 22:55:38 +00002194
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002195 /*
2196 * Manually mark other sectors as
2197 * unlocked (unprotected)
2198 */
2199 for (k = 1; k < flash_info[i].sector_count; k++)
2200 flash_info[i].protect[k] = 0;
2201 } else {
2202 /*
2203 * No legancy unlocking -> unlock all sectors
2204 */
2205 flash_protect (FLAG_PROTECT_CLEAR,
2206 flash_info[i].start[0],
2207 flash_info[i].start[0]
2208 + flash_info[i].size - 1,
2209 &flash_info[i]);
Stefan Roesec865e6c2006-02-28 15:29:58 +01002210 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002211 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002212#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002213 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002214
Heiko Schocheref0946a2011-04-04 08:10:21 +02002215 flash_protect_default();
Piotr Ziecik3e939e92008-11-17 15:57:58 +01002216#ifdef CONFIG_FLASH_CFI_MTD
2217 cfi_mtd_init();
2218#endif
2219
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002220 return (size);
wdenk2cefd152004-02-08 22:55:38 +00002221}