blob: 50df9b0dda44059f782567afb3ccaf83026d8a3c [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>
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +010043#include <watchdog.h>
wdenke537b3b2004-02-23 23:54:43 +000044
wdenk2cefd152004-02-08 22:55:38 +000045/*
Haavard Skinnemoend523e392007-12-13 12:56:28 +010046 * This file implements a Common Flash Interface (CFI) driver for
47 * U-Boot.
48 *
49 * The width of the port and the width of the chips are determined at
50 * initialization. These widths are used to calculate the address for
51 * access CFI data structures.
wdenk2cefd152004-02-08 22:55:38 +000052 *
53 * References
54 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
55 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
56 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
57 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese12797482006-11-13 13:55:24 +010058 * AMD CFI Specification, Release 2.0 December 1, 2001
59 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
60 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk2cefd152004-02-08 22:55:38 +000061 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020062 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocher800db312007-01-19 18:05:26 +010063 * reading and writing ... (yes there is such a Hardware).
wdenk2cefd152004-02-08 22:55:38 +000064 */
65
Haavard Skinnemoend523e392007-12-13 12:56:28 +010066static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysingerc2c093d2010-12-22 09:41:13 -050067#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +010068static uint flash_verbose = 1;
Mike Frysingerc2c093d2010-12-22 09:41:13 -050069#else
70#define flash_verbose 1
71#endif
Wolfgang Denkafa0dd02006-12-27 01:26:13 +010072
Wolfgang Denk9f5fb0f2008-08-08 16:39:54 +020073flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
74
Stefan Roesec865e6c2006-02-28 15:29:58 +010075/*
76 * Check if chip width is defined. If not, start detecting with 8bit.
77 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020078#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
79#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roesec865e6c2006-02-28 15:29:58 +010080#endif
81
Stefan Roeseab935642010-10-25 18:31:48 +020082/*
83 * 0xffff is an undefined value for the configuration register. When
84 * this value is returned, the configuration register shall not be
85 * written at all (default mode).
86 */
87static u16 cfi_flash_config_reg(int i)
88{
89#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
90 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
91#else
92 return 0xffff;
93#endif
94}
95
Stefan Roesefb9a7302010-08-31 10:00:10 +020096#if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
97int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
98#endif
99
Stefan Roese7e7dda82010-08-30 10:11:51 +0200100static phys_addr_t __cfi_flash_bank_addr(int i)
101{
102 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
103}
104phys_addr_t cfi_flash_bank_addr(int i)
105 __attribute__((weak, alias("__cfi_flash_bank_addr")));
106
Ilya Yanok755c1802010-10-21 17:20:12 +0200107static unsigned long __cfi_flash_bank_size(int i)
108{
109#ifdef CONFIG_SYS_FLASH_BANKS_SIZES
110 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
111#else
112 return 0;
113#endif
114}
115unsigned long cfi_flash_bank_size(int i)
116 __attribute__((weak, alias("__cfi_flash_bank_size")));
117
Stefan Roese38ae9822008-11-17 14:45:22 +0100118static void __flash_write8(u8 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100119{
120 __raw_writeb(value, addr);
121}
122
Stefan Roese38ae9822008-11-17 14:45:22 +0100123static void __flash_write16(u16 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100124{
125 __raw_writew(value, addr);
126}
127
Stefan Roese38ae9822008-11-17 14:45:22 +0100128static void __flash_write32(u32 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100129{
130 __raw_writel(value, addr);
131}
132
Stefan Roese38ae9822008-11-17 14:45:22 +0100133static void __flash_write64(u64 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100134{
135 /* No architectures currently implement __raw_writeq() */
136 *(volatile u64 *)addr = value;
137}
138
Stefan Roese38ae9822008-11-17 14:45:22 +0100139static u8 __flash_read8(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100140{
141 return __raw_readb(addr);
142}
143
Stefan Roese38ae9822008-11-17 14:45:22 +0100144static u16 __flash_read16(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100145{
146 return __raw_readw(addr);
147}
148
Stefan Roese38ae9822008-11-17 14:45:22 +0100149static u32 __flash_read32(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100150{
151 return __raw_readl(addr);
152}
153
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100154static u64 __flash_read64(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100155{
156 /* No architectures currently implement __raw_readq() */
157 return *(volatile u64 *)addr;
158}
159
Stefan Roese38ae9822008-11-17 14:45:22 +0100160#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
161void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
162void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
163void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
164void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
165u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
166u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
167u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100168u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
Stefan Roese38ae9822008-11-17 14:45:22 +0100169#else
170#define flash_write8 __flash_write8
171#define flash_write16 __flash_write16
172#define flash_write32 __flash_write32
173#define flash_write64 __flash_write64
174#define flash_read8 __flash_read8
175#define flash_read16 __flash_read16
176#define flash_read32 __flash_read32
177#define flash_read64 __flash_read64
178#endif
Daniel Hellstromcfd71382008-03-28 20:40:19 +0100179
wdenk2cefd152004-02-08 22:55:38 +0000180/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000181 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200182#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 +0100183flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200184{
185 int i;
Stefan Roese6c7c2dc2010-08-30 11:14:38 +0200186 flash_info_t *info = NULL;
wdenk2cefd152004-02-08 22:55:38 +0000187
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200188 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200189 info = & flash_info[i];
190 if (info->size && info->start[0] <= base &&
191 base <= info->start[0] + info->size - 1)
192 break;
193 }
wdenk2cefd152004-02-08 22:55:38 +0000194
Stefan Roese6c7c2dc2010-08-30 11:14:38 +0200195 return info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200196}
wdenk2cefd152004-02-08 22:55:38 +0000197#endif
198
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100199unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
200{
201 if (sect != (info->sector_count - 1))
202 return info->start[sect + 1] - info->start[sect];
203 else
204 return info->start[0] + info->size - info->start[sect];
205}
206
wdenke65527f2004-02-12 00:47:09 +0000207/*-----------------------------------------------------------------------
208 * create an address based on the offset and the port width
209 */
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100210static inline void *
211flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000212{
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100213 unsigned int byte_offset = offset * info->portwidth;
214
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600215 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100216}
217
218static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
219 unsigned int offset, void *addr)
220{
wdenke65527f2004-02-12 00:47:09 +0000221}
222
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200223/*-----------------------------------------------------------------------
224 * make a proper sized command based on the port and chip widths
225 */
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200226static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200227{
228 int i;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400229 int cword_offset;
230 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200231#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200232 u32 cmd_le = cpu_to_le32(cmd);
233#endif
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400234 uchar val;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200235 uchar *cp = (uchar *) cmdbuf;
236
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400237 for (i = info->portwidth; i > 0; i--){
238 cword_offset = (info->portwidth-i)%info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200239#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400240 cp_offset = info->portwidth - i;
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200241 val = *((uchar*)&cmd_le + cword_offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200242#else
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400243 cp_offset = i - 1;
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200244 val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200245#endif
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200246 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400247 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200248}
249
wdenk2cefd152004-02-08 22:55:38 +0000250#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000251/*-----------------------------------------------------------------------
252 * Debug support
253 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100254static void print_longlong (char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000255{
256 int i;
257 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000258
Wolfgang Denk49f4f6a2009-02-04 09:42:20 +0100259 cp = (char *) &data;
wdenke65527f2004-02-12 00:47:09 +0000260 for (i = 0; i < 8; i++)
261 sprintf (&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000262}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200263
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100264static void flash_printqry (struct cfi_qry *qry)
wdenke65527f2004-02-12 00:47:09 +0000265{
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100266 u8 *p = (u8 *)qry;
wdenke65527f2004-02-12 00:47:09 +0000267 int x, y;
268
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100269 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
270 debug("%02x : ", x);
271 for (y = 0; y < 16; y++)
272 debug("%2.2x ", p[x + y]);
273 debug(" ");
wdenke65527f2004-02-12 00:47:09 +0000274 for (y = 0; y < 16; y++) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100275 unsigned char c = p[x + y];
276 if (c >= 0x20 && c <= 0x7e)
277 debug("%c", c);
278 else
279 debug(".");
wdenke65527f2004-02-12 00:47:09 +0000280 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100281 debug("\n");
wdenke65527f2004-02-12 00:47:09 +0000282 }
283}
wdenk2cefd152004-02-08 22:55:38 +0000284#endif
285
286
287/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000288 * read a character at a port width address
289 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100290static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000291{
292 uchar *cp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100293 uchar retval;
wdenke65527f2004-02-12 00:47:09 +0000294
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100295 cp = flash_map (info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200296#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100297 retval = flash_read8(cp);
wdenke65527f2004-02-12 00:47:09 +0000298#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100299 retval = flash_read8(cp + info->portwidth - 1);
wdenke65527f2004-02-12 00:47:09 +0000300#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100301 flash_unmap (info, 0, offset, cp);
302 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000303}
304
305/*-----------------------------------------------------------------------
Tor Krill7f2a3052008-03-28 11:29:10 +0100306 * read a word at a port width address, assume 16bit bus
307 */
308static inline ushort flash_read_word (flash_info_t * info, uint offset)
309{
310 ushort *addr, retval;
311
312 addr = flash_map (info, 0, offset);
313 retval = flash_read16 (addr);
314 flash_unmap (info, 0, offset, addr);
315 return retval;
316}
317
318
319/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +0100320 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000321 * port size word. Swap for ppc format.
322 */
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100323static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
324 uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000325{
wdenke65527f2004-02-12 00:47:09 +0000326 uchar *addr;
327 ulong retval;
328
329#ifdef DEBUG
330 int x;
331#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100332 addr = flash_map (info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000333
wdenke65527f2004-02-12 00:47:09 +0000334#ifdef DEBUG
335 debug ("long addr is at %p info->portwidth = %d\n", addr,
336 info->portwidth);
337 for (x = 0; x < 4 * info->portwidth; x++) {
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100338 debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenke65527f2004-02-12 00:47:09 +0000339 }
340#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200341#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100342 retval = ((flash_read8(addr) << 16) |
343 (flash_read8(addr + info->portwidth) << 24) |
344 (flash_read8(addr + 2 * info->portwidth)) |
345 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenke65527f2004-02-12 00:47:09 +0000346#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100347 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
348 (flash_read8(addr + info->portwidth - 1) << 16) |
349 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
350 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenke65527f2004-02-12 00:47:09 +0000351#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100352 flash_unmap(info, sect, offset, addr);
353
wdenke65527f2004-02-12 00:47:09 +0000354 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000355}
356
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200357/*
358 * Write a proper sized command to the correct address
Michael Schwingen73d044d2007-12-07 23:35:02 +0100359 */
Stefan Roese6e83e342009-10-27 15:15:55 +0100360void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
361 uint offset, u32 cmd)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100362{
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100363
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100364 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200365 cfiword_t cword;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100366
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100367 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200368 flash_make_cmd (info, cmd, &cword);
369 switch (info->portwidth) {
370 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100371 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200372 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100373 flash_write8(cword.c, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200374 break;
375 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100376 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200377 cmd, cword.w,
378 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100379 flash_write16(cword.w, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200380 break;
381 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100382 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200383 cmd, cword.l,
384 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100385 flash_write32(cword.l, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200386 break;
387 case FLASH_CFI_64BIT:
388#ifdef DEBUG
389 {
390 char str[20];
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100391
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200392 print_longlong (str, cword.ll);
393
394 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100395 addr, cmd, str,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200396 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100397 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200398#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100399 flash_write64(cword.ll, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200400 break;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100401 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200402
403 /* Ensure all the instructions are fully finished */
404 sync();
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100405
406 flash_unmap(info, sect, offset, addr);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100407}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200408
409static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100410{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200411 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
412 flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100413}
Michael Schwingen73d044d2007-12-07 23:35:02 +0100414
415/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000416 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200417static int flash_isequal (flash_info_t * info, flash_sect_t sect,
418 uint offset, uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000419{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100420 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200421 cfiword_t cword;
422 int retval;
wdenk2cefd152004-02-08 22:55:38 +0000423
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100424 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200425 flash_make_cmd (info, cmd, &cword);
Stefan Roeseefef95b2006-04-01 13:41:03 +0200426
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100427 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200428 switch (info->portwidth) {
429 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100430 debug ("is= %x %x\n", flash_read8(addr), cword.c);
431 retval = (flash_read8(addr) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200432 break;
433 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100434 debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
435 retval = (flash_read16(addr) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200436 break;
437 case FLASH_CFI_32BIT:
Andrew Klossner7ddfafc2008-08-21 07:12:26 -0700438 debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100439 retval = (flash_read32(addr) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200440 break;
441 case FLASH_CFI_64BIT:
442#ifdef DEBUG
443 {
444 char str1[20];
445 char str2[20];
Michael Schwingen73d044d2007-12-07 23:35:02 +0100446
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100447 print_longlong (str1, flash_read64(addr));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200448 print_longlong (str2, cword.ll);
449 debug ("is= %s %s\n", str1, str2);
wdenk2cefd152004-02-08 22:55:38 +0000450 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200451#endif
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100452 retval = (flash_read64(addr) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200453 break;
454 default:
455 retval = 0;
456 break;
457 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100458 flash_unmap(info, sect, offset, addr);
459
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200460 return retval;
461}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200462
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200463/*-----------------------------------------------------------------------
464 */
465static int flash_isset (flash_info_t * info, flash_sect_t sect,
466 uint offset, uchar cmd)
467{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100468 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200469 cfiword_t cword;
470 int retval;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200471
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100472 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200473 flash_make_cmd (info, cmd, &cword);
474 switch (info->portwidth) {
475 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100476 retval = ((flash_read8(addr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200477 break;
478 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100479 retval = ((flash_read16(addr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200480 break;
481 case FLASH_CFI_32BIT:
Stefan Roesed4e37c02008-01-02 14:05:37 +0100482 retval = ((flash_read32(addr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200483 break;
484 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100485 retval = ((flash_read64(addr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200486 break;
487 default:
488 retval = 0;
489 break;
490 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100491 flash_unmap(info, sect, offset, addr);
492
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200493 return retval;
494}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200495
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200496/*-----------------------------------------------------------------------
497 */
498static int flash_toggle (flash_info_t * info, flash_sect_t sect,
499 uint offset, uchar cmd)
500{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100501 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200502 cfiword_t cword;
503 int retval;
wdenke85b7a52004-10-10 22:16:06 +0000504
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100505 addr = flash_map (info, sect, offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200506 flash_make_cmd (info, cmd, &cword);
507 switch (info->portwidth) {
508 case FLASH_CFI_8BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200509 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200510 break;
511 case FLASH_CFI_16BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200512 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200513 break;
514 case FLASH_CFI_32BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200515 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200516 break;
517 case FLASH_CFI_64BIT:
Wolfgang Denk600e1832008-10-31 01:12:28 +0100518 retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
519 (flash_read32(addr+4) != flash_read32(addr+4)) );
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200520 break;
521 default:
522 retval = 0;
523 break;
524 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100525 flash_unmap(info, sect, offset, addr);
526
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200527 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000528}
529
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200530/*
531 * flash_is_busy - check to see if the flash is busy
532 *
533 * This routine checks the status of the chip and returns true if the
534 * chip is busy.
wdenk2cefd152004-02-08 22:55:38 +0000535 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200536static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
wdenk5c71a7a2005-05-16 15:23:22 +0000537{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200538 int retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000539
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200540 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400541 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200542 case CFI_CMDSET_INTEL_STANDARD:
543 case CFI_CMDSET_INTEL_EXTENDED:
544 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
545 break;
546 case CFI_CMDSET_AMD_STANDARD:
547 case CFI_CMDSET_AMD_EXTENDED:
548#ifdef CONFIG_FLASH_CFI_LEGACY
549 case CFI_CMDSET_AMD_LEGACY:
550#endif
551 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
552 break;
553 default:
554 retval = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000555 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200556 debug ("flash_is_busy: %d\n", retval);
557 return retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000558}
559
560/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200561 * wait for XSR.7 to be set. Time out with an error if it does not.
562 * This routine does not set the flash to read-array mode.
wdenk5c71a7a2005-05-16 15:23:22 +0000563 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200564static int flash_status_check (flash_info_t * info, flash_sect_t sector,
565 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000566{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200567 ulong start;
wdenk2cefd152004-02-08 22:55:38 +0000568
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200569#if CONFIG_SYS_HZ != 1000
Renato Andreolaac6693d2010-03-24 23:00:47 +0800570 if ((ulong)CONFIG_SYS_HZ > 100000)
571 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
572 else
573 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200574#endif
wdenk2cefd152004-02-08 22:55:38 +0000575
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200576 /* Wait for command completion */
Graeme Russ13ec42b2011-07-15 02:18:56 +0000577#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou4b7e6682010-04-01 11:15:05 +0800578 reset_timer();
Graeme Russ13ec42b2011-07-15 02:18:56 +0000579#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200580 start = get_timer (0);
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +0100581 WATCHDOG_RESET();
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200582 while (flash_is_busy (info, sector)) {
583 if (get_timer (start) > tout) {
584 printf ("Flash %s timeout at address %lx data %lx\n",
585 prompt, info->start[sector],
586 flash_read_long (info, sector, 0));
587 flash_write_cmd (info, sector, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -0700588 udelay(1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200589 return ERR_TIMOUT;
wdenk2cefd152004-02-08 22:55:38 +0000590 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200591 udelay (1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000592 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200593 return ERR_OK;
594}
wdenk2cefd152004-02-08 22:55:38 +0000595
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200596/*-----------------------------------------------------------------------
597 * Wait for XSR.7 to be set, if it times out print an error, otherwise
598 * do a full status check.
599 *
600 * This routine sets the flash to read-array mode.
601 */
602static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
603 ulong tout, char *prompt)
604{
605 int retcode;
wdenk2cefd152004-02-08 22:55:38 +0000606
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200607 retcode = flash_status_check (info, sector, tout, prompt);
608 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400609 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200610 case CFI_CMDSET_INTEL_EXTENDED:
611 case CFI_CMDSET_INTEL_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500612 if ((retcode != ERR_OK)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200613 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
614 retcode = ERR_INVAL;
615 printf ("Flash %s error at address %lx\n", prompt,
616 info->start[sector]);
617 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
618 FLASH_STATUS_PSLBS)) {
619 puts ("Command Sequence Error.\n");
620 } else if (flash_isset (info, sector, 0,
621 FLASH_STATUS_ECLBS)) {
622 puts ("Block Erase Error.\n");
623 retcode = ERR_NOT_ERASED;
624 } else if (flash_isset (info, sector, 0,
625 FLASH_STATUS_PSLBS)) {
626 puts ("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000627 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200628 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
629 puts ("Block locked.\n");
630 retcode = ERR_PROTECTED;
631 }
632 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
633 puts ("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000634 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200635 flash_write_cmd (info, sector, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -0700636 udelay(1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200637 break;
638 default:
639 break;
wdenk2cefd152004-02-08 22:55:38 +0000640 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200641 return retcode;
wdenk2cefd152004-02-08 22:55:38 +0000642}
643
Thomas Chou076767a2010-03-26 08:17:00 +0800644static int use_flash_status_poll(flash_info_t *info)
645{
646#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
647 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
648 info->vendor == CFI_CMDSET_AMD_STANDARD)
649 return 1;
650#endif
651 return 0;
652}
653
654static int flash_status_poll(flash_info_t *info, void *src, void *dst,
655 ulong tout, char *prompt)
656{
657#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
658 ulong start;
659 int ready;
660
661#if CONFIG_SYS_HZ != 1000
662 if ((ulong)CONFIG_SYS_HZ > 100000)
663 tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
664 else
665 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
666#endif
667
668 /* Wait for command completion */
Graeme Russ13ec42b2011-07-15 02:18:56 +0000669#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou4b7e6682010-04-01 11:15:05 +0800670 reset_timer();
Graeme Russ13ec42b2011-07-15 02:18:56 +0000671#endif
Thomas Chou076767a2010-03-26 08:17:00 +0800672 start = get_timer(0);
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +0100673 WATCHDOG_RESET();
Thomas Chou076767a2010-03-26 08:17:00 +0800674 while (1) {
675 switch (info->portwidth) {
676 case FLASH_CFI_8BIT:
677 ready = flash_read8(dst) == flash_read8(src);
678 break;
679 case FLASH_CFI_16BIT:
680 ready = flash_read16(dst) == flash_read16(src);
681 break;
682 case FLASH_CFI_32BIT:
683 ready = flash_read32(dst) == flash_read32(src);
684 break;
685 case FLASH_CFI_64BIT:
686 ready = flash_read64(dst) == flash_read64(src);
687 break;
688 default:
689 ready = 0;
690 break;
691 }
692 if (ready)
693 break;
694 if (get_timer(start) > tout) {
695 printf("Flash %s timeout at address %lx data %lx\n",
696 prompt, (ulong)dst, (ulong)flash_read8(dst));
697 return ERR_TIMOUT;
698 }
699 udelay(1); /* also triggers watchdog */
700 }
701#endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
702 return ERR_OK;
703}
704
wdenk2cefd152004-02-08 22:55:38 +0000705/*-----------------------------------------------------------------------
706 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200707static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000708{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200709#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200710 unsigned short w;
711 unsigned int l;
712 unsigned long long ll;
713#endif
wdenk2cefd152004-02-08 22:55:38 +0000714
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200715 switch (info->portwidth) {
716 case FLASH_CFI_8BIT:
717 cword->c = c;
718 break;
719 case FLASH_CFI_16BIT:
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 w = c;
722 w <<= 8;
723 cword->w = (cword->w >> 8) | w;
724#else
725 cword->w = (cword->w << 8) | c;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100726#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200727 break;
728 case FLASH_CFI_32BIT:
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 l = c;
731 l <<= 24;
732 cword->l = (cword->l >> 8) | l;
733#else
734 cword->l = (cword->l << 8) | c;
735#endif
736 break;
737 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200738#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200739 ll = c;
740 ll <<= 56;
741 cword->ll = (cword->ll >> 8) | ll;
742#else
743 cword->ll = (cword->ll << 8) | c;
744#endif
745 break;
Stefan Roese12797482006-11-13 13:55:24 +0100746 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200747}
wdenk2cefd152004-02-08 22:55:38 +0000748
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100749/*
750 * Loop through the sector table starting from the previously found sector.
751 * Searches forwards or backwards, dependent on the passed address.
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200752 */
753static flash_sect_t find_sector (flash_info_t * info, ulong addr)
754{
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100755 static flash_sect_t saved_sector = 0; /* previously found sector */
Martin Krausebe9d5682011-03-21 18:07:56 +0100756 static flash_info_t *saved_info = 0; /* previously used flash bank */
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100757 flash_sect_t sector = saved_sector;
758
Martin Krausebe9d5682011-03-21 18:07:56 +0100759 if ((info != saved_info) || (sector >= info->sector_count))
760 sector = 0;
761
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100762 while ((info->start[sector] < addr)
763 && (sector < info->sector_count - 1))
764 sector++;
765 while ((info->start[sector] > addr) && (sector > 0))
766 /*
767 * also decrements the sector in case of an overshot
768 * in the first loop
769 */
770 sector--;
wdenk2cefd152004-02-08 22:55:38 +0000771
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100772 saved_sector = sector;
Martin Krausebe9d5682011-03-21 18:07:56 +0100773 saved_info = info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200774 return sector;
wdenk2cefd152004-02-08 22:55:38 +0000775}
776
777/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000778 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200779static int flash_write_cfiword (flash_info_t * info, ulong dest,
780 cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +0000781{
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600782 void *dstaddr = (void *)dest;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200783 int flag;
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100784 flash_sect_t sect = 0;
785 char sect_found = 0;
wdenk2cefd152004-02-08 22:55:38 +0000786
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200787 /* Check if Flash is (sufficiently) erased */
788 switch (info->portwidth) {
789 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100790 flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200791 break;
792 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100793 flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200794 break;
795 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100796 flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200797 break;
798 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100799 flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200800 break;
801 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100802 flag = 0;
803 break;
wdenk2cefd152004-02-08 22:55:38 +0000804 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600805 if (!flag)
Stefan Roese707c1462007-12-27 07:50:54 +0100806 return ERR_NOT_ERASED;
wdenk2cefd152004-02-08 22:55:38 +0000807
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200808 /* Disable interrupts which might cause a timeout here */
809 flag = disable_interrupts ();
Stefan Roesec865e6c2006-02-28 15:29:58 +0100810
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200811 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400812 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200813 case CFI_CMDSET_INTEL_EXTENDED:
814 case CFI_CMDSET_INTEL_STANDARD:
815 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
816 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
817 break;
818 case CFI_CMDSET_AMD_EXTENDED:
819 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500820 sect = find_sector(info, dest);
821 flash_unlock_seq (info, sect);
822 flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100823 sect_found = 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200824 break;
Po-Yu Chuang3f483402009-07-10 18:03:57 +0800825#ifdef CONFIG_FLASH_CFI_LEGACY
826 case CFI_CMDSET_AMD_LEGACY:
827 sect = find_sector(info, dest);
828 flash_unlock_seq (info, 0);
829 flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
830 sect_found = 1;
831 break;
832#endif
wdenk2cefd152004-02-08 22:55:38 +0000833 }
834
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200835 switch (info->portwidth) {
836 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100837 flash_write8(cword.c, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200838 break;
839 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100840 flash_write16(cword.w, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200841 break;
842 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100843 flash_write32(cword.l, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200844 break;
845 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100846 flash_write64(cword.ll, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200847 break;
wdenk2cefd152004-02-08 22:55:38 +0000848 }
849
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200850 /* re-enable interrupts if necessary */
851 if (flag)
852 enable_interrupts ();
wdenk2cefd152004-02-08 22:55:38 +0000853
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100854 if (!sect_found)
855 sect = find_sector (info, dest);
856
Thomas Chou076767a2010-03-26 08:17:00 +0800857 if (use_flash_status_poll(info))
858 return flash_status_poll(info, &cword, dstaddr,
859 info->write_tout, "write");
860 else
861 return flash_full_status_check(info, sect,
862 info->write_tout, "write");
wdenk2cefd152004-02-08 22:55:38 +0000863}
864
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200865#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenk2cefd152004-02-08 22:55:38 +0000866
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200867static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
868 int len)
wdenk2cefd152004-02-08 22:55:38 +0000869{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200870 flash_sect_t sector;
871 int cnt;
872 int retcode;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100873 void *src = cp;
Stefan Roese42c6ace2009-02-05 11:25:57 +0100874 void *dst = (void *)dest;
Stefan Roese707c1462007-12-27 07:50:54 +0100875 void *dst2 = dst;
Tao Houdd3b4552012-03-15 23:33:58 +0800876 int flag = 1;
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200877 uint offset = 0;
878 unsigned int shift;
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400879 uchar write_cmd;
Stefan Roese707c1462007-12-27 07:50:54 +0100880
881 switch (info->portwidth) {
882 case FLASH_CFI_8BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200883 shift = 0;
Stefan Roese707c1462007-12-27 07:50:54 +0100884 break;
885 case FLASH_CFI_16BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200886 shift = 1;
Stefan Roese707c1462007-12-27 07:50:54 +0100887 break;
888 case FLASH_CFI_32BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200889 shift = 2;
Stefan Roese707c1462007-12-27 07:50:54 +0100890 break;
891 case FLASH_CFI_64BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200892 shift = 3;
Stefan Roese707c1462007-12-27 07:50:54 +0100893 break;
894 default:
895 retcode = ERR_INVAL;
896 goto out_unmap;
897 }
898
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200899 cnt = len >> shift;
900
Tao Houdd3b4552012-03-15 23:33:58 +0800901 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese707c1462007-12-27 07:50:54 +0100902 switch (info->portwidth) {
903 case FLASH_CFI_8BIT:
904 flag = ((flash_read8(dst2) & flash_read8(src)) ==
905 flash_read8(src));
906 src += 1, dst2 += 1;
907 break;
908 case FLASH_CFI_16BIT:
909 flag = ((flash_read16(dst2) & flash_read16(src)) ==
910 flash_read16(src));
911 src += 2, dst2 += 2;
912 break;
913 case FLASH_CFI_32BIT:
914 flag = ((flash_read32(dst2) & flash_read32(src)) ==
915 flash_read32(src));
916 src += 4, dst2 += 4;
917 break;
918 case FLASH_CFI_64BIT:
919 flag = ((flash_read64(dst2) & flash_read64(src)) ==
920 flash_read64(src));
921 src += 8, dst2 += 8;
922 break;
923 }
924 }
925 if (!flag) {
926 retcode = ERR_NOT_ERASED;
927 goto out_unmap;
928 }
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100929
Stefan Roese707c1462007-12-27 07:50:54 +0100930 src = cp;
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100931 sector = find_sector (info, dest);
wdenke65527f2004-02-12 00:47:09 +0000932
933 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400934 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +0000935 case CFI_CMDSET_INTEL_STANDARD:
936 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400937 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
938 FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200939 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400940 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
941 flash_write_cmd (info, sector, 0, write_cmd);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200942 retcode = flash_status_check (info, sector,
943 info->buffer_write_tout,
944 "write to buffer");
945 if (retcode == ERR_OK) {
946 /* reduce the number of loops by the width of
947 * the port */
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200948 cnt = len >> shift;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400949 flash_write_cmd (info, sector, 0, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200950 while (cnt-- > 0) {
951 switch (info->portwidth) {
952 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100953 flash_write8(flash_read8(src), dst);
954 src += 1, dst += 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200955 break;
956 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100957 flash_write16(flash_read16(src), dst);
958 src += 2, dst += 2;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200959 break;
960 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100961 flash_write32(flash_read32(src), dst);
962 src += 4, dst += 4;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200963 break;
964 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100965 flash_write64(flash_read64(src), dst);
966 src += 8, dst += 8;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200967 break;
968 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100969 retcode = ERR_INVAL;
970 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200971 }
972 }
973 flash_write_cmd (info, sector, 0,
974 FLASH_CMD_WRITE_BUFFER_CONFIRM);
975 retcode = flash_full_status_check (
976 info, sector, info->buffer_write_tout,
977 "buffer write");
978 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100979
980 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200981
wdenk2cefd152004-02-08 22:55:38 +0000982 case CFI_CMDSET_AMD_STANDARD:
983 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200984 flash_unlock_seq(info,0);
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200985
986#ifdef CONFIG_FLASH_SPANSION_S29WS_N
987 offset = ((unsigned long)dst - info->start[sector]) >> shift;
988#endif
989 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
990 cnt = len >> shift;
John Schmolleree355882009-08-12 10:55:47 -0500991 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200992
993 switch (info->portwidth) {
994 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100995 while (cnt-- > 0) {
996 flash_write8(flash_read8(src), dst);
997 src += 1, dst += 1;
998 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200999 break;
1000 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001001 while (cnt-- > 0) {
1002 flash_write16(flash_read16(src), dst);
1003 src += 2, dst += 2;
1004 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001005 break;
1006 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001007 while (cnt-- > 0) {
1008 flash_write32(flash_read32(src), dst);
1009 src += 4, dst += 4;
1010 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001011 break;
1012 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001013 while (cnt-- > 0) {
1014 flash_write64(flash_read64(src), dst);
1015 src += 8, dst += 8;
1016 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001017 break;
1018 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001019 retcode = ERR_INVAL;
1020 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001021 }
1022
1023 flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Chou076767a2010-03-26 08:17:00 +08001024 if (use_flash_status_poll(info))
1025 retcode = flash_status_poll(info, src - (1 << shift),
1026 dst - (1 << shift),
1027 info->buffer_write_tout,
1028 "buffer write");
1029 else
1030 retcode = flash_full_status_check(info, sector,
1031 info->buffer_write_tout,
1032 "buffer write");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001033 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001034
wdenk2cefd152004-02-08 22:55:38 +00001035 default:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001036 debug ("Unknown Command Set\n");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001037 retcode = ERR_INVAL;
1038 break;
wdenk2cefd152004-02-08 22:55:38 +00001039 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001040
1041out_unmap:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001042 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001043}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001044#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001045
wdenke65527f2004-02-12 00:47:09 +00001046
wdenk2cefd152004-02-08 22:55:38 +00001047/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +00001048 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001049int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +00001050{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001051 int rcode = 0;
1052 int prot;
1053 flash_sect_t sect;
Thomas Chou076767a2010-03-26 08:17:00 +08001054 int st;
wdenk2cefd152004-02-08 22:55:38 +00001055
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001056 if (info->flash_id != FLASH_MAN_CFI) {
1057 puts ("Can't erase unknown flash type - aborted\n");
1058 return 1;
1059 }
1060 if ((s_first < 0) || (s_first > s_last)) {
1061 puts ("- no sectors to erase\n");
1062 return 1;
1063 }
Stefan Roeseefef95b2006-04-01 13:41:03 +02001064
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001065 prot = 0;
1066 for (sect = s_first; sect <= s_last; ++sect) {
1067 if (info->protect[sect]) {
1068 prot++;
wdenk2cefd152004-02-08 22:55:38 +00001069 }
1070 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001071 if (prot) {
1072 printf ("- Warning: %d protected sectors will not be erased!\n",
1073 prot);
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001074 } else if (flash_verbose) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001075 putc ('\n');
1076 }
wdenke65527f2004-02-12 00:47:09 +00001077
wdenke65527f2004-02-12 00:47:09 +00001078
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001079 for (sect = s_first; sect <= s_last; sect++) {
1080 if (info->protect[sect] == 0) { /* not protected */
1081 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001082 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001083 case CFI_CMDSET_INTEL_STANDARD:
1084 case CFI_CMDSET_INTEL_EXTENDED:
1085 flash_write_cmd (info, sect, 0,
1086 FLASH_CMD_CLEAR_STATUS);
1087 flash_write_cmd (info, sect, 0,
1088 FLASH_CMD_BLOCK_ERASE);
1089 flash_write_cmd (info, sect, 0,
1090 FLASH_CMD_ERASE_CONFIRM);
1091 break;
1092 case CFI_CMDSET_AMD_STANDARD:
1093 case CFI_CMDSET_AMD_EXTENDED:
1094 flash_unlock_seq (info, sect);
1095 flash_write_cmd (info, sect,
1096 info->addr_unlock1,
1097 AMD_CMD_ERASE_START);
1098 flash_unlock_seq (info, sect);
1099 flash_write_cmd (info, sect, 0,
1100 AMD_CMD_ERASE_SECTOR);
1101 break;
1102#ifdef CONFIG_FLASH_CFI_LEGACY
1103 case CFI_CMDSET_AMD_LEGACY:
1104 flash_unlock_seq (info, 0);
1105 flash_write_cmd (info, 0, info->addr_unlock1,
1106 AMD_CMD_ERASE_START);
1107 flash_unlock_seq (info, 0);
1108 flash_write_cmd (info, sect, 0,
1109 AMD_CMD_ERASE_SECTOR);
1110 break;
1111#endif
1112 default:
1113 debug ("Unkown flash vendor %d\n",
1114 info->vendor);
1115 break;
wdenke65527f2004-02-12 00:47:09 +00001116 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001117
Thomas Chou076767a2010-03-26 08:17:00 +08001118 if (use_flash_status_poll(info)) {
1119 cfiword_t cword = (cfiword_t)0xffffffffffffffffULL;
1120 void *dest;
1121 dest = flash_map(info, sect, 0);
1122 st = flash_status_poll(info, &cword, dest,
1123 info->erase_blk_tout, "erase");
1124 flash_unmap(info, sect, 0, dest);
1125 } else
1126 st = flash_full_status_check(info, sect,
1127 info->erase_blk_tout,
1128 "erase");
1129 if (st)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001130 rcode = 1;
Thomas Chou076767a2010-03-26 08:17:00 +08001131 else if (flash_verbose)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001132 putc ('.');
wdenk2cefd152004-02-08 22:55:38 +00001133 }
wdenk2cefd152004-02-08 22:55:38 +00001134 }
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001135
1136 if (flash_verbose)
1137 puts (" done\n");
1138
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001139 return rcode;
wdenk2cefd152004-02-08 22:55:38 +00001140}
wdenke65527f2004-02-12 00:47:09 +00001141
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001142#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1143static int sector_erased(flash_info_t *info, int i)
1144{
1145 int k;
1146 int size;
Stefan Roesea9153f22010-10-25 18:31:39 +02001147 u32 *flash;
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001148
1149 /*
1150 * Check if whole sector is erased
1151 */
1152 size = flash_sector_size(info, i);
Stefan Roesea9153f22010-10-25 18:31:39 +02001153 flash = (u32 *)info->start[i];
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001154 /* divide by 4 for longword access */
1155 size = size >> 2;
1156
1157 for (k = 0; k < size; k++) {
Stefan Roesea9153f22010-10-25 18:31:39 +02001158 if (flash_read32(flash++) != 0xffffffff)
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001159 return 0; /* not erased */
1160 }
1161
1162 return 1; /* erased */
1163}
1164#endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1165
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001166void flash_print_info (flash_info_t * info)
wdenk2cefd152004-02-08 22:55:38 +00001167{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001168 int i;
wdenk369d43d2004-03-14 14:09:05 +00001169
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001170 if (info->flash_id != FLASH_MAN_CFI) {
1171 puts ("missing or unknown FLASH type\n");
1172 return;
1173 }
1174
Peter Tyser0b5a2632010-12-28 18:12:05 -06001175 printf ("%s flash (%d x %d)",
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001176 info->name,
1177 (info->portwidth << 3), (info->chipwidth << 3));
1178 if (info->size < 1024*1024)
1179 printf (" Size: %ld kB in %d Sectors\n",
1180 info->size >> 10, info->sector_count);
1181 else
1182 printf (" Size: %ld MB in %d Sectors\n",
1183 info->size >> 20, info->sector_count);
1184 printf (" ");
1185 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001186 case CFI_CMDSET_INTEL_PROG_REGIONS:
1187 printf ("Intel Prog Regions");
1188 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001189 case CFI_CMDSET_INTEL_STANDARD:
1190 printf ("Intel Standard");
1191 break;
1192 case CFI_CMDSET_INTEL_EXTENDED:
1193 printf ("Intel Extended");
1194 break;
1195 case CFI_CMDSET_AMD_STANDARD:
1196 printf ("AMD Standard");
1197 break;
1198 case CFI_CMDSET_AMD_EXTENDED:
1199 printf ("AMD Extended");
1200 break;
1201#ifdef CONFIG_FLASH_CFI_LEGACY
1202 case CFI_CMDSET_AMD_LEGACY:
1203 printf ("AMD Legacy");
1204 break;
wdenk369d43d2004-03-14 14:09:05 +00001205#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001206 default:
1207 printf ("Unknown (%d)", info->vendor);
1208 break;
wdenk2cefd152004-02-08 22:55:38 +00001209 }
Philippe De Muyter35bc0812010-08-10 16:54:52 +02001210 printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
1211 info->manufacturer_id);
1212 printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1213 info->device_id);
Heiko Schocher27cea502011-04-11 14:16:19 +02001214 if ((info->device_id & 0xff) == 0x7E) {
1215 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
1216 info->device_id2);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001217 }
1218 printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
1219 info->erase_blk_tout,
1220 info->write_tout);
1221 if (info->buffer_size > 1) {
1222 printf (" Buffer write timeout: %ld ms, "
1223 "buffer size: %d bytes\n",
1224 info->buffer_write_tout,
1225 info->buffer_size);
1226 }
wdenk2cefd152004-02-08 22:55:38 +00001227
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001228 puts ("\n Sector Start Addresses:");
1229 for (i = 0; i < info->sector_count; ++i) {
Kim Phillipsc8836f12010-07-26 18:35:39 -05001230 if (ctrlc())
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001231 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001232 if ((i % 5) == 0)
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001233 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001234#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001235 /* print empty and read-only info */
1236 printf (" %08lX %c %s ",
1237 info->start[i],
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001238 sector_erased(info, i) ? 'E' : ' ',
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001239 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001240#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001241 printf (" %08lX %s ",
1242 info->start[i],
1243 info->protect[i] ? "RO" : " ");
wdenke65527f2004-02-12 00:47:09 +00001244#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001245 }
1246 putc ('\n');
1247 return;
wdenk2cefd152004-02-08 22:55:38 +00001248}
1249
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001250/*-----------------------------------------------------------------------
Jerry Van Barenaae73572008-03-08 13:48:01 -05001251 * This is used in a few places in write_buf() to show programming
1252 * progress. Making it a function is nasty because it needs to do side
1253 * effect updates to digit and dots. Repeated code is nasty too, so
1254 * we define it once here.
1255 */
Stefan Roese7758c162008-03-19 07:09:26 +01001256#ifdef CONFIG_FLASH_SHOW_PROGRESS
1257#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001258 if (flash_verbose) { \
1259 dots -= dots_sub; \
1260 if ((scale > 0) && (dots <= 0)) { \
1261 if ((digit % 5) == 0) \
1262 printf ("%d", digit / 5); \
1263 else \
1264 putc ('.'); \
1265 digit--; \
1266 dots += scale; \
1267 } \
Jerry Van Barenaae73572008-03-08 13:48:01 -05001268 }
Stefan Roese7758c162008-03-19 07:09:26 +01001269#else
1270#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1271#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001272
1273/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001274 * Copy memory to flash, returns:
1275 * 0 - OK
1276 * 1 - write timeout
1277 * 2 - Flash not erased
wdenk2cefd152004-02-08 22:55:38 +00001278 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001279int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +00001280{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001281 ulong wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001282 uchar *p;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001283 int aln;
wdenk2cefd152004-02-08 22:55:38 +00001284 cfiword_t cword;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001285 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001286#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001287 int buffered_size;
wdenk2cefd152004-02-08 22:55:38 +00001288#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001289#ifdef CONFIG_FLASH_SHOW_PROGRESS
1290 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1291 int scale = 0;
1292 int dots = 0;
1293
1294 /*
1295 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1296 */
1297 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1298 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1299 CONFIG_FLASH_SHOW_PROGRESS);
1300 }
1301#endif
1302
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001303 /* get lower aligned address */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001304 wp = (addr & ~(info->portwidth - 1));
Haiying Wangc123a382007-02-21 16:52:31 +01001305
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001306 /* handle unaligned start */
1307 if ((aln = addr - wp) != 0) {
1308 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001309 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001310 for (i = 0; i < aln; ++i)
1311 flash_add_byte (info, &cword, flash_read8(p + i));
wdenk2cefd152004-02-08 22:55:38 +00001312
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001313 for (; (i < info->portwidth) && (cnt > 0); i++) {
1314 flash_add_byte (info, &cword, *src++);
1315 cnt--;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001316 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001317 for (; (cnt == 0) && (i < info->portwidth); ++i)
1318 flash_add_byte (info, &cword, flash_read8(p + i));
1319
1320 rc = flash_write_cfiword (info, wp, cword);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001321 if (rc != 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001322 return rc;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001323
1324 wp += i;
Stefan Roese7758c162008-03-19 07:09:26 +01001325 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001326 }
1327
1328 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001329#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001330 buffered_size = (info->portwidth / info->chipwidth);
1331 buffered_size *= info->buffer_size;
1332 while (cnt >= info->portwidth) {
1333 /* prohibit buffer write when buffer_size is 1 */
1334 if (info->buffer_size == 1) {
1335 cword.l = 0;
1336 for (i = 0; i < info->portwidth; i++)
1337 flash_add_byte (info, &cword, *src++);
1338 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1339 return rc;
1340 wp += info->portwidth;
1341 cnt -= info->portwidth;
1342 continue;
1343 }
1344
1345 /* write buffer until next buffered_size aligned boundary */
1346 i = buffered_size - (wp % buffered_size);
1347 if (i > cnt)
1348 i = cnt;
1349 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1350 return rc;
1351 i -= i & (info->portwidth - 1);
1352 wp += i;
1353 src += i;
1354 cnt -= i;
Stefan Roese7758c162008-03-19 07:09:26 +01001355 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001356 }
1357#else
1358 while (cnt >= info->portwidth) {
1359 cword.l = 0;
1360 for (i = 0; i < info->portwidth; i++) {
1361 flash_add_byte (info, &cword, *src++);
1362 }
1363 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1364 return rc;
1365 wp += info->portwidth;
1366 cnt -= info->portwidth;
Stefan Roese7758c162008-03-19 07:09:26 +01001367 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001368 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001369#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Barenaae73572008-03-08 13:48:01 -05001370
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001371 if (cnt == 0) {
1372 return (0);
1373 }
1374
1375 /*
1376 * handle unaligned tail bytes
1377 */
1378 cword.l = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001379 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001380 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001381 flash_add_byte (info, &cword, *src++);
1382 --cnt;
1383 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001384 for (; i < info->portwidth; ++i)
1385 flash_add_byte (info, &cword, flash_read8(p + i));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001386
1387 return flash_write_cfiword (info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +00001388}
wdenke65527f2004-02-12 00:47:09 +00001389
wdenk2cefd152004-02-08 22:55:38 +00001390/*-----------------------------------------------------------------------
1391 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001392#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001393
1394int flash_real_protect (flash_info_t * info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +00001395{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001396 int retcode = 0;
wdenke65527f2004-02-12 00:47:09 +00001397
Rafael Campos13d2b612008-07-31 10:22:20 +02001398 switch (info->vendor) {
1399 case CFI_CMDSET_INTEL_PROG_REGIONS:
1400 case CFI_CMDSET_INTEL_STANDARD:
Nick Spenceec81b472008-08-19 22:21:16 -07001401 case CFI_CMDSET_INTEL_EXTENDED:
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001402 /*
1403 * see errata called
1404 * "Numonyx Axcell P33/P30 Specification Update" :)
1405 */
1406 flash_write_cmd (info, sector, 0, FLASH_CMD_READ_ID);
1407 if (!flash_isequal (info, sector, FLASH_OFFSET_PROTECT,
1408 prot)) {
1409 /*
1410 * cmd must come before FLASH_CMD_PROTECT + 20us
1411 * Disable interrupts which might cause a timeout here.
1412 */
1413 int flag = disable_interrupts ();
1414 unsigned short cmd;
1415
1416 if (prot)
1417 cmd = FLASH_CMD_PROTECT_SET;
1418 else
1419 cmd = FLASH_CMD_PROTECT_CLEAR;
1420
Rafael Campos13d2b612008-07-31 10:22:20 +02001421 flash_write_cmd (info, sector, 0,
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001422 FLASH_CMD_PROTECT);
1423 flash_write_cmd (info, sector, 0, cmd);
1424 /* re-enable interrupts if necessary */
1425 if (flag)
1426 enable_interrupts ();
1427 }
Rafael Campos13d2b612008-07-31 10:22:20 +02001428 break;
1429 case CFI_CMDSET_AMD_EXTENDED:
1430 case CFI_CMDSET_AMD_STANDARD:
Rafael Campos13d2b612008-07-31 10:22:20 +02001431 /* U-Boot only checks the first byte */
1432 if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
1433 if (prot) {
1434 flash_unlock_seq (info, 0);
1435 flash_write_cmd (info, 0,
1436 info->addr_unlock1,
1437 ATM_CMD_SOFTLOCK_START);
1438 flash_unlock_seq (info, 0);
1439 flash_write_cmd (info, sector, 0,
1440 ATM_CMD_LOCK_SECT);
1441 } else {
1442 flash_write_cmd (info, 0,
1443 info->addr_unlock1,
1444 AMD_CMD_UNLOCK_START);
1445 if (info->device_id == ATM_ID_BV6416)
1446 flash_write_cmd (info, sector,
1447 0, ATM_CMD_UNLOCK_SECT);
1448 }
1449 }
Anatolij Gustschin041b04f2012-08-09 08:18:12 +02001450 if (info->manufacturer_id == (uchar)AMD_MANUFACT) {
1451 int flag = disable_interrupts();
1452 int lock_flag;
1453
1454 flash_unlock_seq(info, 0);
1455 flash_write_cmd(info, 0, info->addr_unlock1,
1456 AMD_CMD_SET_PPB_ENTRY);
1457 lock_flag = flash_isset(info, sector, 0, 0x01);
1458 if (prot) {
1459 if (lock_flag) {
1460 flash_write_cmd(info, sector, 0,
1461 AMD_CMD_PPB_LOCK_BC1);
1462 flash_write_cmd(info, sector, 0,
1463 AMD_CMD_PPB_LOCK_BC2);
1464 }
1465 debug("sector %ld %slocked\n", sector,
1466 lock_flag ? "" : "already ");
1467 } else {
1468 if (!lock_flag) {
1469 debug("unlock %ld\n", sector);
1470 flash_write_cmd(info, 0, 0,
1471 AMD_CMD_PPB_UNLOCK_BC1);
1472 flash_write_cmd(info, 0, 0,
1473 AMD_CMD_PPB_UNLOCK_BC2);
1474 }
1475 debug("sector %ld %sunlocked\n", sector,
1476 !lock_flag ? "" : "already ");
1477 }
1478 if (flag)
1479 enable_interrupts();
1480
1481 if (flash_status_check(info, sector,
1482 info->erase_blk_tout,
1483 prot ? "protect" : "unprotect"))
1484 printf("status check error\n");
1485
1486 flash_write_cmd(info, 0, 0,
1487 AMD_CMD_SET_PPB_EXIT_BC1);
1488 flash_write_cmd(info, 0, 0,
1489 AMD_CMD_SET_PPB_EXIT_BC2);
1490 }
Rafael Campos13d2b612008-07-31 10:22:20 +02001491 break;
TsiChung Liewb8c19292008-08-19 16:53:39 +00001492#ifdef CONFIG_FLASH_CFI_LEGACY
1493 case CFI_CMDSET_AMD_LEGACY:
1494 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1495 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1496 if (prot)
1497 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1498 else
1499 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1500#endif
Rafael Campos13d2b612008-07-31 10:22:20 +02001501 };
wdenk2cefd152004-02-08 22:55:38 +00001502
Stefan Roese5215df12010-10-25 18:31:29 +02001503 /*
1504 * Flash needs to be in status register read mode for
1505 * flash_full_status_check() to work correctly
1506 */
1507 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001508 if ((retcode =
1509 flash_full_status_check (info, sector, info->erase_blk_tout,
1510 prot ? "protect" : "unprotect")) == 0) {
wdenke65527f2004-02-12 00:47:09 +00001511
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001512 info->protect[sector] = prot;
1513
1514 /*
1515 * On some of Intel's flash chips (marked via legacy_unlock)
1516 * unprotect unprotects all locking.
1517 */
1518 if ((prot == 0) && (info->legacy_unlock)) {
1519 flash_sect_t i;
1520
1521 for (i = 0; i < info->sector_count; i++) {
1522 if (info->protect[i])
1523 flash_real_protect (info, i, 1);
1524 }
wdenk2cefd152004-02-08 22:55:38 +00001525 }
wdenk2cefd152004-02-08 22:55:38 +00001526 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001527 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001528}
wdenke65527f2004-02-12 00:47:09 +00001529
wdenk2cefd152004-02-08 22:55:38 +00001530/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001531 * flash_read_user_serial - read the OneTimeProgramming cells
wdenk2cefd152004-02-08 22:55:38 +00001532 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001533void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1534 int len)
wdenk2cefd152004-02-08 22:55:38 +00001535{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001536 uchar *src;
1537 uchar *dst;
wdenke65527f2004-02-12 00:47:09 +00001538
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001539 dst = buffer;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001540 src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001541 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1542 memcpy (dst, src + offset, len);
1543 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001544 udelay(1);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001545 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001546}
1547
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001548/*
1549 * flash_read_factory_serial - read the device Id from the protection area
wdenk2cefd152004-02-08 22:55:38 +00001550 */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001551void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1552 int len)
wdenk2cefd152004-02-08 22:55:38 +00001553{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001554 uchar *src;
wdenke65527f2004-02-12 00:47:09 +00001555
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001556 src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001557 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1558 memcpy (buffer, src + offset, len);
1559 flash_write_cmd (info, 0, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001560 udelay(1);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001561 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001562}
1563
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001564#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001565
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001566/*-----------------------------------------------------------------------
1567 * Reverse the order of the erase regions in the CFI QRY structure.
1568 * This is needed for chips that are either a) correctly detected as
1569 * top-boot, or b) buggy.
1570 */
1571static void cfi_reverse_geometry(struct cfi_qry *qry)
1572{
1573 unsigned int i, j;
1574 u32 tmp;
1575
1576 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1577 tmp = qry->erase_region_info[i];
1578 qry->erase_region_info[i] = qry->erase_region_info[j];
1579 qry->erase_region_info[j] = tmp;
1580 }
1581}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001582
wdenk2cefd152004-02-08 22:55:38 +00001583/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +01001584 * read jedec ids from device and set corresponding fields in info struct
1585 *
1586 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1587 *
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001588 */
1589static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1590{
1591 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001592 udelay(1);
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001593 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1594 udelay(1000); /* some flash are slow to respond */
1595 info->manufacturer_id = flash_read_uchar (info,
1596 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyter35bc0812010-08-10 16:54:52 +02001597 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
1598 flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
1599 flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001600 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1601}
1602
1603static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1604{
1605 info->cmd_reset = FLASH_CMD_RESET;
1606
1607 cmdset_intel_read_jedec_ids(info);
1608 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1609
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001610#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001611 /* read legacy lock/unlock bit from intel flash */
1612 if (info->ext_addr) {
1613 info->legacy_unlock = flash_read_uchar (info,
1614 info->ext_addr + 5) & 0x08;
1615 }
1616#endif
1617
1618 return 0;
1619}
1620
1621static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1622{
Niklaus Gigerf447f712009-07-22 17:13:24 +02001623 ushort bankId = 0;
1624 uchar manuId;
1625
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001626 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1627 flash_unlock_seq(info, 0);
1628 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1629 udelay(1000); /* some flash are slow to respond */
Tor Krill7f2a3052008-03-28 11:29:10 +01001630
Niklaus Gigerf447f712009-07-22 17:13:24 +02001631 manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1632 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
1633 while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1634 bankId += 0x100;
1635 manuId = flash_read_uchar (info,
1636 bankId | FLASH_OFFSET_MANUFACTURER_ID);
1637 }
1638 info->manufacturer_id = manuId;
Tor Krill7f2a3052008-03-28 11:29:10 +01001639
1640 switch (info->chipwidth){
1641 case FLASH_CFI_8BIT:
1642 info->device_id = flash_read_uchar (info,
1643 FLASH_OFFSET_DEVICE_ID);
1644 if (info->device_id == 0x7E) {
1645 /* AMD 3-byte (expanded) device ids */
1646 info->device_id2 = flash_read_uchar (info,
1647 FLASH_OFFSET_DEVICE_ID2);
1648 info->device_id2 <<= 8;
1649 info->device_id2 |= flash_read_uchar (info,
1650 FLASH_OFFSET_DEVICE_ID3);
1651 }
1652 break;
1653 case FLASH_CFI_16BIT:
1654 info->device_id = flash_read_word (info,
1655 FLASH_OFFSET_DEVICE_ID);
Heiko Schocher27cea502011-04-11 14:16:19 +02001656 if ((info->device_id & 0xff) == 0x7E) {
1657 /* AMD 3-byte (expanded) device ids */
1658 info->device_id2 = flash_read_uchar (info,
1659 FLASH_OFFSET_DEVICE_ID2);
1660 info->device_id2 <<= 8;
1661 info->device_id2 |= flash_read_uchar (info,
1662 FLASH_OFFSET_DEVICE_ID3);
1663 }
Tor Krill7f2a3052008-03-28 11:29:10 +01001664 break;
1665 default:
1666 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001667 }
1668 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001669 udelay(1);
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001670}
1671
1672static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1673{
1674 info->cmd_reset = AMD_CMD_RESET;
1675
1676 cmdset_amd_read_jedec_ids(info);
1677 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1678
Anatolij Gustschin041b04f2012-08-09 08:18:12 +02001679#ifdef CONFIG_SYS_FLASH_PROTECTION
1680 if (info->ext_addr && info->manufacturer_id == (uchar)AMD_MANUFACT) {
1681 ushort spus;
1682
1683 /* read sector protect/unprotect scheme */
1684 spus = flash_read_uchar(info, info->ext_addr + 9);
1685 if (spus == 0x8)
1686 info->legacy_unlock = 1;
1687 }
1688#endif
1689
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001690 return 0;
1691}
1692
1693#ifdef CONFIG_FLASH_CFI_LEGACY
Stefan Roese12797482006-11-13 13:55:24 +01001694static void flash_read_jedec_ids (flash_info_t * info)
1695{
1696 info->manufacturer_id = 0;
1697 info->device_id = 0;
1698 info->device_id2 = 0;
1699
1700 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001701 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese12797482006-11-13 13:55:24 +01001702 case CFI_CMDSET_INTEL_STANDARD:
1703 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001704 cmdset_intel_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001705 break;
1706 case CFI_CMDSET_AMD_STANDARD:
1707 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001708 cmdset_amd_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001709 break;
1710 default:
1711 break;
1712 }
1713}
1714
1715/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001716 * Call board code to request info about non-CFI flash.
1717 * board_flash_get_legacy needs to fill in at least:
1718 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001719 */
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001720static int flash_detect_legacy(phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001721{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001722 flash_info_t *info = &flash_info[banknum];
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001723
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001724 if (board_flash_get_legacy(base, banknum, info)) {
1725 /* board code may have filled info completely. If not, we
1726 use JEDEC ID probing. */
1727 if (!info->vendor) {
1728 int modes[] = {
1729 CFI_CMDSET_AMD_STANDARD,
1730 CFI_CMDSET_INTEL_STANDARD
1731 };
1732 int i;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001733
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001734 for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1735 info->vendor = modes[i];
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001736 info->start[0] =
1737 (ulong)map_physmem(base,
Stefan Roeseb8443702009-02-05 11:44:52 +01001738 info->portwidth,
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001739 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001740 if (info->portwidth == FLASH_CFI_8BIT
1741 && info->interface == FLASH_CFI_X8X16) {
1742 info->addr_unlock1 = 0x2AAA;
1743 info->addr_unlock2 = 0x5555;
1744 } else {
1745 info->addr_unlock1 = 0x5555;
1746 info->addr_unlock2 = 0x2AAA;
1747 }
1748 flash_read_jedec_ids(info);
1749 debug("JEDEC PROBE: ID %x %x %x\n",
1750 info->manufacturer_id,
1751 info->device_id,
1752 info->device_id2);
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001753 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001754 break;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001755 else
Stefan Roeseb8443702009-02-05 11:44:52 +01001756 unmap_physmem((void *)info->start[0],
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001757 MAP_NOCACHE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001758 }
1759 }
1760
1761 switch(info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001762 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001763 case CFI_CMDSET_INTEL_STANDARD:
1764 case CFI_CMDSET_INTEL_EXTENDED:
1765 info->cmd_reset = FLASH_CMD_RESET;
1766 break;
1767 case CFI_CMDSET_AMD_STANDARD:
1768 case CFI_CMDSET_AMD_EXTENDED:
1769 case CFI_CMDSET_AMD_LEGACY:
1770 info->cmd_reset = AMD_CMD_RESET;
1771 break;
1772 }
1773 info->flash_id = FLASH_MAN_CFI;
1774 return 1;
1775 }
1776 return 0; /* use CFI */
1777}
1778#else
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001779static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001780{
1781 return 0; /* use CFI */
1782}
1783#endif
1784
1785/*-----------------------------------------------------------------------
1786 * detect if flash is compatible with the Common Flash Interface (CFI)
1787 * http://www.jedec.org/download/search/jesd68.pdf
1788 */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001789static void flash_read_cfi (flash_info_t *info, void *buf,
1790 unsigned int start, size_t len)
1791{
1792 u8 *p = buf;
1793 unsigned int i;
1794
1795 for (i = 0; i < len; i++)
1796 p[i] = flash_read_uchar(info, start + i);
1797}
Stefan Roese6e83e342009-10-27 15:15:55 +01001798
1799void __flash_cmd_reset(flash_info_t *info)
1800{
1801 /*
1802 * We do not yet know what kind of commandset to use, so we issue
1803 * the reset command in both Intel and AMD variants, in the hope
1804 * that AMD flash roms ignore the Intel command.
1805 */
1806 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001807 udelay(1);
Stefan Roese6e83e342009-10-27 15:15:55 +01001808 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1809}
1810void flash_cmd_reset(flash_info_t *info)
1811 __attribute__((weak,alias("__flash_cmd_reset")));
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001812
1813static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001814{
1815 int cfi_offset;
1816
Stefan Roese6e83e342009-10-27 15:15:55 +01001817 /* Issue FLASH reset command */
1818 flash_cmd_reset(info);
Michael Schwingen4661cf72008-02-18 23:16:35 +01001819
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001820 for (cfi_offset=0;
1821 cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1822 cfi_offset++) {
1823 flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1824 FLASH_CMD_CFI);
1825 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1826 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1827 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001828 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1829 sizeof(struct cfi_qry));
1830 info->interface = le16_to_cpu(qry->interface_desc);
1831
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001832 info->cfi_offset = flash_offset_cfi[cfi_offset];
1833 debug ("device interface is %d\n",
1834 info->interface);
1835 debug ("found port %d chip %d ",
1836 info->portwidth, info->chipwidth);
1837 debug ("port %d bits chip %d bits\n",
1838 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1839 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1840
1841 /* calculate command offsets as in the Linux driver */
1842 info->addr_unlock1 = 0x555;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001843 info->addr_unlock2 = 0x2aa;
1844
1845 /*
1846 * modify the unlock address if we are
1847 * in compatibility mode
1848 */
1849 if ( /* x8/x16 in x8 mode */
1850 ((info->chipwidth == FLASH_CFI_BY8) &&
1851 (info->interface == FLASH_CFI_X8X16)) ||
1852 /* x16/x32 in x16 mode */
1853 ((info->chipwidth == FLASH_CFI_BY16) &&
1854 (info->interface == FLASH_CFI_X16X32)))
1855 {
1856 info->addr_unlock1 = 0xaaa;
1857 info->addr_unlock2 = 0x555;
1858 }
1859
1860 info->name = "CFI conformant";
1861 return 1;
1862 }
1863 }
1864
1865 return 0;
1866}
1867
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001868static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001869{
wdenke65527f2004-02-12 00:47:09 +00001870 debug ("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001871
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001872 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenke65527f2004-02-12 00:47:09 +00001873 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1874 for (info->chipwidth = FLASH_CFI_BY8;
1875 info->chipwidth <= info->portwidth;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001876 info->chipwidth <<= 1)
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001877 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001878 return 1;
wdenk2cefd152004-02-08 22:55:38 +00001879 }
wdenke65527f2004-02-12 00:47:09 +00001880 debug ("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001881 return 0;
1882}
wdenke65527f2004-02-12 00:47:09 +00001883
wdenk2cefd152004-02-08 22:55:38 +00001884/*
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001885 * Manufacturer-specific quirks. Add workarounds for geometry
1886 * reversal, etc. here.
1887 */
1888static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1889{
1890 /* check if flash geometry needs reversal */
1891 if (qry->num_erase_regions > 1) {
1892 /* reverse geometry if top boot part */
1893 if (info->cfi_version < 0x3131) {
1894 /* CFI < 1.1, try to guess from device id */
1895 if ((info->device_id & 0x80) != 0)
1896 cfi_reverse_geometry(qry);
1897 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1898 /* CFI >= 1.1, deduct from top/bottom flag */
1899 /* note: ext_addr is valid since cfi_version > 0 */
1900 cfi_reverse_geometry(qry);
1901 }
1902 }
1903}
1904
1905static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1906{
1907 int reverse_geometry = 0;
1908
1909 /* Check the "top boot" bit in the PRI */
1910 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1911 reverse_geometry = 1;
1912
1913 /* AT49BV6416(T) list the erase regions in the wrong order.
1914 * However, the device ID is identical with the non-broken
Ulf Samuelsson07f9b4e2009-03-27 23:26:43 +01001915 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001916 */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001917 if (info->device_id == 0xd6 || info->device_id == 0xd2)
1918 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001919
1920 if (reverse_geometry)
1921 cfi_reverse_geometry(qry);
1922}
1923
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001924static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1925{
1926 /* check if flash geometry needs reversal */
1927 if (qry->num_erase_regions > 1) {
1928 /* reverse geometry if top boot part */
1929 if (info->cfi_version < 0x3131) {
Mike Frysinger02a37862011-04-10 16:06:29 -04001930 /* CFI < 1.1, guess by device id */
1931 if (info->device_id == 0x22CA || /* M29W320DT */
1932 info->device_id == 0x2256 || /* M29W320ET */
1933 info->device_id == 0x22D7) { /* M29W800DT */
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001934 cfi_reverse_geometry(qry);
1935 }
Mike Frysinger97dd8992011-05-09 18:33:36 -04001936 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1937 /* CFI >= 1.1, deduct from top/bottom flag */
1938 /* note: ext_addr is valid since cfi_version > 0 */
1939 cfi_reverse_geometry(qry);
Richard Retanubunbe4dca22009-01-14 08:44:26 -05001940 }
1941 }
1942}
1943
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01001944/*
wdenk2cefd152004-02-08 22:55:38 +00001945 * The following code cannot be run from FLASH!
1946 *
1947 */
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01001948ulong flash_get_size (phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001949{
wdenke65527f2004-02-12 00:47:09 +00001950 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00001951 int i, j;
1952 flash_sect_t sect_cnt;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001953 phys_addr_t sector;
wdenk2cefd152004-02-08 22:55:38 +00001954 unsigned long tmp;
1955 int size_ratio;
1956 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00001957 int erase_region_size;
1958 int erase_region_count;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001959 struct cfi_qry qry;
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01001960 unsigned long max_size;
Stefan Roese12797482006-11-13 13:55:24 +01001961
Kumar Gala899032b2008-05-15 15:13:08 -05001962 memset(&qry, 0, sizeof(qry));
1963
Stefan Roese12797482006-11-13 13:55:24 +01001964 info->ext_addr = 0;
1965 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001966#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02001967 info->legacy_unlock = 0;
1968#endif
wdenk2cefd152004-02-08 22:55:38 +00001969
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001970 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00001971
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001972 if (flash_detect_cfi (info, &qry)) {
1973 info->vendor = le16_to_cpu(qry.p_id);
1974 info->ext_addr = le16_to_cpu(qry.p_adr);
1975 num_erase_regions = qry.num_erase_regions;
1976
Stefan Roese12797482006-11-13 13:55:24 +01001977 if (info->ext_addr) {
1978 info->cfi_version = (ushort) flash_read_uchar (info,
1979 info->ext_addr + 3) << 8;
1980 info->cfi_version |= (ushort) flash_read_uchar (info,
1981 info->ext_addr + 4);
1982 }
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001983
wdenke65527f2004-02-12 00:47:09 +00001984#ifdef DEBUG
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001985 flash_printqry (&qry);
wdenke65527f2004-02-12 00:47:09 +00001986#endif
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001987
wdenke65527f2004-02-12 00:47:09 +00001988 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001989 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +00001990 case CFI_CMDSET_INTEL_STANDARD:
1991 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001992 cmdset_intel_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00001993 break;
1994 case CFI_CMDSET_AMD_STANDARD:
1995 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001996 cmdset_amd_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00001997 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001998 default:
1999 printf("CFI: Unknown command set 0x%x\n",
2000 info->vendor);
2001 /*
2002 * Unfortunately, this means we don't know how
2003 * to get the chip back to Read mode. Might
2004 * as well try an Intel-style reset...
2005 */
2006 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2007 return 0;
wdenk2cefd152004-02-08 22:55:38 +00002008 }
wdenk6cfa84e2004-02-10 00:03:41 +00002009
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002010 /* Do manufacturer-specific fixups */
2011 switch (info->manufacturer_id) {
Mario Schuknecht5c3579e2011-02-21 13:13:14 +01002012 case 0x0001: /* AMD */
2013 case 0x0037: /* AMIC */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002014 flash_fixup_amd(info, &qry);
2015 break;
2016 case 0x001f:
2017 flash_fixup_atmel(info, &qry);
2018 break;
Richard Retanubunbe4dca22009-01-14 08:44:26 -05002019 case 0x0020:
2020 flash_fixup_stm(info, &qry);
2021 break;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002022 }
2023
wdenke65527f2004-02-12 00:47:09 +00002024 debug ("manufacturer is %d\n", info->vendor);
Stefan Roese12797482006-11-13 13:55:24 +01002025 debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
2026 debug ("device id is 0x%x\n", info->device_id);
2027 debug ("device id2 is 0x%x\n", info->device_id2);
2028 debug ("cfi version is 0x%04x\n", info->cfi_version);
2029
wdenk2cefd152004-02-08 22:55:38 +00002030 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00002031 /* if the chip is x8/x16 reduce the ratio by half */
2032 if ((info->interface == FLASH_CFI_X8X16)
2033 && (info->chipwidth == FLASH_CFI_BY8)) {
2034 size_ratio >>= 1;
2035 }
wdenke65527f2004-02-12 00:47:09 +00002036 debug ("size_ratio %d port %d bits chip %d bits\n",
2037 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2038 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanok755c1802010-10-21 17:20:12 +02002039 info->size = 1 << qry.dev_size;
2040 /* multiply the size by the number of chips */
2041 info->size *= size_ratio;
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002042 max_size = cfi_flash_bank_size(banknum);
Ilya Yanok755c1802010-10-21 17:20:12 +02002043 if (max_size && (info->size > max_size)) {
2044 debug("[truncated from %ldMiB]", info->size >> 20);
2045 info->size = max_size;
2046 }
wdenke65527f2004-02-12 00:47:09 +00002047 debug ("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00002048 sect_cnt = 0;
2049 sector = base;
wdenke65527f2004-02-12 00:47:09 +00002050 for (i = 0; i < num_erase_regions; i++) {
2051 if (i > NUM_ERASE_REGIONS) {
wdenke537b3b2004-02-23 23:54:43 +00002052 printf ("%d erase regions found, only %d used\n",
2053 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00002054 break;
2055 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002056
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002057 tmp = le32_to_cpu(qry.erase_region_info[i]);
2058 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002059
2060 erase_region_count = (tmp & 0xffff) + 1;
2061 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00002062 erase_region_size =
2063 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
wdenkaeba06f2004-06-09 17:34:58 +00002064 debug ("erase_region_count = %d erase_region_size = %d\n",
wdenke537b3b2004-02-23 23:54:43 +00002065 erase_region_count, erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00002066 for (j = 0; j < erase_region_count; j++) {
Ilya Yanok755c1802010-10-21 17:20:12 +02002067 if (sector - base >= info->size)
2068 break;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002069 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen73d044d2007-12-07 23:35:02 +01002070 printf("ERROR: too many flash sectors\n");
2071 break;
2072 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002073 info->start[sect_cnt] =
2074 (ulong)map_physmem(sector,
2075 info->portwidth,
2076 MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00002077 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00002078
2079 /*
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002080 * Only read protection status from
2081 * supported devices (intel...)
wdenk26c58432005-01-09 17:12:27 +00002082 */
2083 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04002084 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk26c58432005-01-09 17:12:27 +00002085 case CFI_CMDSET_INTEL_EXTENDED:
2086 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roese5215df12010-10-25 18:31:29 +02002087 /*
2088 * Set flash to read-id mode. Otherwise
2089 * reading protected status is not
2090 * guaranteed.
2091 */
2092 flash_write_cmd(info, sect_cnt, 0,
2093 FLASH_CMD_READ_ID);
wdenk26c58432005-01-09 17:12:27 +00002094 info->protect[sect_cnt] =
2095 flash_isset (info, sect_cnt,
2096 FLASH_OFFSET_PROTECT,
2097 FLASH_STATUS_PROTECT);
2098 break;
2099 default:
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002100 /* default: not protected */
2101 info->protect[sect_cnt] = 0;
wdenk26c58432005-01-09 17:12:27 +00002102 }
2103
wdenk2cefd152004-02-08 22:55:38 +00002104 sect_cnt++;
2105 }
2106 }
2107
2108 info->sector_count = sect_cnt;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002109 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2110 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002111 info->erase_blk_tout = tmp *
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002112 (1 << qry.block_erase_timeout_max);
2113 tmp = (1 << qry.buf_write_timeout_typ) *
2114 (1 << qry.buf_write_timeout_max);
2115
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002116 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002117 info->buffer_write_tout = (tmp + 999) / 1000;
2118 tmp = (1 << qry.word_write_timeout_typ) *
2119 (1 << qry.word_write_timeout_max);
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002120 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002121 info->write_tout = (tmp + 999) / 1000;
wdenk2cefd152004-02-08 22:55:38 +00002122 info->flash_id = FLASH_MAN_CFI;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002123 if ((info->interface == FLASH_CFI_X8X16) &&
2124 (info->chipwidth == FLASH_CFI_BY8)) {
2125 /* XXX - Need to test on x8/x16 in parallel. */
2126 info->portwidth >>= 1;
wdenked2ac4b2004-03-14 18:23:55 +00002127 }
Mike Frysinger59404ee2008-10-02 01:55:38 -04002128
2129 flash_write_cmd (info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +00002130 }
2131
wdenke65527f2004-02-12 00:47:09 +00002132 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00002133}
2134
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002135#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002136void flash_set_verbose(uint v)
2137{
2138 flash_verbose = v;
2139}
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002140#endif
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002141
Stefan Roeseab935642010-10-25 18:31:48 +02002142static void cfi_flash_set_config_reg(u32 base, u16 val)
2143{
2144#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2145 /*
2146 * Only set this config register if really defined
2147 * to a valid value (0xffff is invalid)
2148 */
2149 if (val == 0xffff)
2150 return;
2151
2152 /*
2153 * Set configuration register. Data is "encrypted" in the 16 lower
2154 * address bits.
2155 */
2156 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2157 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2158
2159 /*
2160 * Finally issue reset-command to bring device back to
2161 * read-array mode
2162 */
2163 flash_write16(FLASH_CMD_RESET, (void *)base);
2164#endif
2165}
2166
wdenk2cefd152004-02-08 22:55:38 +00002167/*-----------------------------------------------------------------------
2168 */
Heiko Schocheref0946a2011-04-04 08:10:21 +02002169
2170void flash_protect_default(void)
2171{
Peter Tyser4f3c60d2011-04-13 11:46:56 -05002172#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2173 int i;
2174 struct apl_s {
2175 ulong start;
2176 ulong size;
2177 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2178#endif
2179
Heiko Schocheref0946a2011-04-04 08:10:21 +02002180 /* Monitor protection ON by default */
2181#if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2182 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2183 flash_protect(FLAG_PROTECT_SET,
2184 CONFIG_SYS_MONITOR_BASE,
2185 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2186 flash_get_info(CONFIG_SYS_MONITOR_BASE));
2187#endif
2188
2189 /* Environment protection ON by default */
2190#ifdef CONFIG_ENV_IS_IN_FLASH
2191 flash_protect(FLAG_PROTECT_SET,
2192 CONFIG_ENV_ADDR,
2193 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2194 flash_get_info(CONFIG_ENV_ADDR));
2195#endif
2196
2197 /* Redundant environment protection ON by default */
2198#ifdef CONFIG_ENV_ADDR_REDUND
2199 flash_protect(FLAG_PROTECT_SET,
2200 CONFIG_ENV_ADDR_REDUND,
2201 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2202 flash_get_info(CONFIG_ENV_ADDR_REDUND));
2203#endif
2204
2205#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2206 for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
Marek Vasutcb1622e2011-10-21 14:17:05 +00002207 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocheref0946a2011-04-04 08:10:21 +02002208 apl[i].start, apl[i].start + apl[i].size - 1);
2209 flash_protect(FLAG_PROTECT_SET,
2210 apl[i].start,
2211 apl[i].start + apl[i].size - 1,
2212 flash_get_info(apl[i].start));
2213 }
2214#endif
2215}
2216
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002217unsigned long flash_init (void)
wdenk2cefd152004-02-08 22:55:38 +00002218{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002219 unsigned long size = 0;
2220 int i;
wdenk2cefd152004-02-08 22:55:38 +00002221
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002222#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann8f7ee7d2009-03-21 09:59:34 -04002223 /* read environment from EEPROM */
2224 char s[64];
Wolfgang Denk76af2782010-07-24 21:55:43 +02002225 getenv_f("unlock", s, sizeof(s));
Michael Schwingen73d044d2007-12-07 23:35:02 +01002226#endif
wdenk2cefd152004-02-08 22:55:38 +00002227
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002228 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002229 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002230 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk2cefd152004-02-08 22:55:38 +00002231
Stefan Roeseab935642010-10-25 18:31:48 +02002232 /* Optionally write flash configuration register */
2233 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2234 cfi_flash_config_reg(i));
2235
Stefan Roese7e7dda82010-08-30 10:11:51 +02002236 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002237 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002238 size += flash_info[i].size;
2239 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002240#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Peter Tyser0b5a2632010-12-28 18:12:05 -06002241 printf ("## Unknown flash on Bank %d "
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002242 "- Size = 0x%08lx = %ld MB\n",
2243 i+1, flash_info[i].size,
John Schmoller61665db2010-09-29 13:49:05 -05002244 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002245#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002246 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002247#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002248 else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
2249 /*
2250 * Only the U-Boot image and it's environment
2251 * is protected, all other sectors are
2252 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002253 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002254 * and the environment variable "unlock" is
2255 * set to "yes".
2256 */
2257 if (flash_info[i].legacy_unlock) {
2258 int k;
wdenk2cefd152004-02-08 22:55:38 +00002259
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002260 /*
2261 * Disable legacy_unlock temporarily,
2262 * since flash_real_protect would
2263 * relock all other sectors again
2264 * otherwise.
2265 */
2266 flash_info[i].legacy_unlock = 0;
wdenk2cefd152004-02-08 22:55:38 +00002267
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002268 /*
2269 * Legacy unlocking (e.g. Intel J3) ->
2270 * unlock only one sector. This will
2271 * unlock all sectors.
2272 */
2273 flash_real_protect (&flash_info[i], 0, 0);
wdenk2cefd152004-02-08 22:55:38 +00002274
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002275 flash_info[i].legacy_unlock = 1;
wdenk2cefd152004-02-08 22:55:38 +00002276
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002277 /*
2278 * Manually mark other sectors as
2279 * unlocked (unprotected)
2280 */
2281 for (k = 1; k < flash_info[i].sector_count; k++)
2282 flash_info[i].protect[k] = 0;
2283 } else {
2284 /*
2285 * No legancy unlocking -> unlock all sectors
2286 */
2287 flash_protect (FLAG_PROTECT_CLEAR,
2288 flash_info[i].start[0],
2289 flash_info[i].start[0]
2290 + flash_info[i].size - 1,
2291 &flash_info[i]);
Stefan Roesec865e6c2006-02-28 15:29:58 +01002292 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002293 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002294#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002295 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002296
Heiko Schocheref0946a2011-04-04 08:10:21 +02002297 flash_protect_default();
Piotr Ziecik3e939e92008-11-17 15:57:58 +01002298#ifdef CONFIG_FLASH_CFI_MTD
2299 cfi_mtd_init();
2300#endif
2301
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002302 return (size);
wdenk2cefd152004-02-08 22:55:38 +00002303}