blob: 4ce183b6f313fcdf4898a070e04b5fa6f091f954 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5653fc32004-02-08 22:55:38 +00002/*
wdenkbf9e3b32004-02-12 00:47:09 +00003 * (C) Copyright 2002-2004
wdenk5653fc32004-02-08 22:55:38 +00004 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
5 *
6 * Copyright (C) 2003 Arabella Software Ltd.
7 * Yuli Barcohen <yuli@arabellasw.com>
wdenk5653fc32004-02-08 22:55:38 +00008 *
wdenkbf9e3b32004-02-12 00:47:09 +00009 * Copyright (C) 2004
10 * Ed Okerson
Stefan Roese260421a2006-11-13 13:55:24 +010011 *
12 * Copyright (C) 2006
13 * Tolunay Orkun <listmember@orkun.us>
wdenk5653fc32004-02-08 22:55:38 +000014 */
15
16/* The DEBUG define must be before common to enable debugging */
wdenk2d1a5372004-02-23 19:30:57 +000017/* #define DEBUG */
18
wdenk5653fc32004-02-08 22:55:38 +000019#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -070020#include <console.h>
Thomas Chouf1056912015-11-07 14:31:08 +080021#include <dm.h>
Simon Glass3a7d5572019-08-01 09:46:42 -060022#include <env.h>
Thomas Chouf1056912015-11-07 14:31:08 +080023#include <errno.h>
24#include <fdt_support.h>
Simon Glassc30b7ad2019-11-14 12:57:41 -070025#include <irq_func.h>
wdenk5653fc32004-02-08 22:55:38 +000026#include <asm/processor.h>
Haiying Wang3a197b22007-02-21 16:52:31 +010027#include <asm/io.h>
wdenk4c0d4c32004-06-09 17:34:58 +000028#include <asm/byteorder.h>
Andrew Gabbasovaedadf12013-05-14 12:27:52 -050029#include <asm/unaligned.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060030#include <env_internal.h>
Stefan Roesefa36ae72009-10-27 15:15:55 +010031#include <mtd/cfi_flash.h>
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +010032#include <watchdog.h>
wdenk028ab6b2004-02-23 23:54:43 +000033
wdenk5653fc32004-02-08 22:55:38 +000034/*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010035 * This file implements a Common Flash Interface (CFI) driver for
36 * U-Boot.
37 *
38 * The width of the port and the width of the chips are determined at
39 * initialization. These widths are used to calculate the address for
40 * access CFI data structures.
wdenk5653fc32004-02-08 22:55:38 +000041 *
42 * References
43 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
44 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
45 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
46 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese260421a2006-11-13 13:55:24 +010047 * AMD CFI Specification, Release 2.0 December 1, 2001
48 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
49 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk5653fc32004-02-08 22:55:38 +000050 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocherd0b6e142007-01-19 18:05:26 +010052 * reading and writing ... (yes there is such a Hardware).
wdenk5653fc32004-02-08 22:55:38 +000053 */
54
Thomas Chouf1056912015-11-07 14:31:08 +080055DECLARE_GLOBAL_DATA_PTR;
56
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010057static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysinger4ffeab22010-12-22 09:41:13 -050058#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +010059static uint flash_verbose = 1;
Mike Frysinger4ffeab22010-12-22 09:41:13 -050060#else
61#define flash_verbose 1
62#endif
Wolfgang Denk92eb7292006-12-27 01:26:13 +010063
Wolfgang Denk2a112b22008-08-08 16:39:54 +020064flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
65
Stefan Roese79b4cda2006-02-28 15:29:58 +010066/*
67 * Check if chip width is defined. If not, start detecting with 8bit.
68 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020069#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
70#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roese79b4cda2006-02-28 15:29:58 +010071#endif
72
Jeroen Hofstee00dcb072014-10-08 22:57:23 +020073#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
74#define __maybe_weak __weak
75#else
76#define __maybe_weak static
77#endif
78
Stefan Roese6f726f92010-10-25 18:31:48 +020079/*
80 * 0xffff is an undefined value for the configuration register. When
81 * this value is returned, the configuration register shall not be
82 * written at all (default mode).
83 */
84static u16 cfi_flash_config_reg(int i)
85{
86#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
87 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
88#else
89 return 0xffff;
90#endif
91}
92
Stefan Roeseca5def32010-08-31 10:00:10 +020093#if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
94int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
Mario Sixd9a35692018-01-26 14:43:56 +010095#else
96int cfi_flash_num_flash_banks;
Stefan Roeseca5def32010-08-31 10:00:10 +020097#endif
98
Thomas Chouf1056912015-11-07 14:31:08 +080099#ifdef CONFIG_CFI_FLASH /* for driver model */
100static void cfi_flash_init_dm(void)
101{
102 struct udevice *dev;
103
104 cfi_flash_num_flash_banks = 0;
105 /*
106 * The uclass_first_device() will probe the first device and
107 * uclass_next_device() will probe the rest if they exist. So
108 * that cfi_flash_probe() will get called assigning the base
109 * addresses that are available.
110 */
111 for (uclass_first_device(UCLASS_MTD, &dev);
112 dev;
113 uclass_next_device(&dev)) {
114 }
115}
116
Thomas Chouf1056912015-11-07 14:31:08 +0800117phys_addr_t cfi_flash_bank_addr(int i)
118{
Marek Vasut1ec0a372017-09-12 19:09:08 +0200119 return flash_info[i].base;
Thomas Chouf1056912015-11-07 14:31:08 +0800120}
121#else
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200122__weak phys_addr_t cfi_flash_bank_addr(int i)
Stefan Roeseb00e19c2010-08-30 10:11:51 +0200123{
124 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
125}
Thomas Chouf1056912015-11-07 14:31:08 +0800126#endif
Stefan Roeseb00e19c2010-08-30 10:11:51 +0200127
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200128__weak unsigned long cfi_flash_bank_size(int i)
Ilya Yanokec50a8e2010-10-21 17:20:12 +0200129{
130#ifdef CONFIG_SYS_FLASH_BANKS_SIZES
131 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
132#else
133 return 0;
134#endif
135}
Ilya Yanokec50a8e2010-10-21 17:20:12 +0200136
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200137__maybe_weak void flash_write8(u8 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100138{
139 __raw_writeb(value, addr);
140}
141
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200142__maybe_weak void flash_write16(u16 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100143{
144 __raw_writew(value, addr);
145}
146
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200147__maybe_weak void flash_write32(u32 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100148{
149 __raw_writel(value, addr);
150}
151
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200152__maybe_weak void flash_write64(u64 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100153{
154 /* No architectures currently implement __raw_writeq() */
155 *(volatile u64 *)addr = value;
156}
157
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200158__maybe_weak u8 flash_read8(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100159{
160 return __raw_readb(addr);
161}
162
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200163__maybe_weak u16 flash_read16(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100164{
165 return __raw_readw(addr);
166}
167
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200168__maybe_weak u32 flash_read32(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100169{
170 return __raw_readl(addr);
171}
172
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200173__maybe_weak u64 flash_read64(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100174{
175 /* No architectures currently implement __raw_readq() */
176 return *(volatile u64 *)addr;
177}
178
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200179/*-----------------------------------------------------------------------
180 */
Mario Sixddcf0542018-01-26 14:43:54 +0100181#if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || \
Vignesh Raghavendrad75eacf2019-10-23 13:30:00 +0530182 (defined(CONFIG_SYS_MONITOR_BASE) && \
183 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE))
Marek Vasut236c49a2017-08-20 17:20:00 +0200184static flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200185{
186 int i;
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900187 flash_info_t *info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200188
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200189 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Masahiro Yamadae2e273a2013-05-17 14:50:36 +0900190 info = &flash_info[i];
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200191 if (info->size && info->start[0] <= base &&
192 base <= info->start[0] + info->size - 1)
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900193 return info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200194 }
195
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900196 return NULL;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200197}
wdenk5653fc32004-02-08 22:55:38 +0000198#endif
199
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100200unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
201{
202 if (sect != (info->sector_count - 1))
203 return info->start[sect + 1] - info->start[sect];
204 else
205 return info->start[0] + info->size - info->start[sect];
206}
207
wdenk5653fc32004-02-08 22:55:38 +0000208/*-----------------------------------------------------------------------
209 * create an address based on the offset and the port width
210 */
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100211static inline void *
Mario Sixca2b07a2018-01-26 14:43:32 +0100212flash_map(flash_info_t *info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000213{
Stefan Roesee303be22013-04-12 19:04:54 +0200214 unsigned int byte_offset = offset * info->portwidth;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100215
Stefan Roesee303be22013-04-12 19:04:54 +0200216 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100217}
218
219static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
Mario Sixc0350fb2018-01-26 14:43:55 +0100220 unsigned int offset, void *addr)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100221{
wdenk5653fc32004-02-08 22:55:38 +0000222}
wdenkbf9e3b32004-02-12 00:47:09 +0000223
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200224/*-----------------------------------------------------------------------
225 * make a proper sized command based on the port and chip widths
226 */
Sebastian Siewior7288f972008-07-15 13:35:23 +0200227static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200228{
229 int i;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400230 int cword_offset;
231 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200232#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewior340ccb22008-07-16 20:04:49 +0200233 u32 cmd_le = cpu_to_le32(cmd);
234#endif
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400235 uchar val;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200236 uchar *cp = (uchar *) cmdbuf;
237
Mario Sixb1683862018-01-26 14:43:33 +0100238 for (i = info->portwidth; i > 0; i--) {
Mario Six640f4e32018-01-26 14:43:36 +0100239 cword_offset = (info->portwidth - i) % info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200240#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400241 cp_offset = info->portwidth - i;
Mario Sixdb91bb22018-01-26 14:43:34 +0100242 val = *((uchar *)&cmd_le + cword_offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200243#else
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400244 cp_offset = i - 1;
Mario Sixdb91bb22018-01-26 14:43:34 +0100245 val = *((uchar *)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200246#endif
Sebastian Siewior7288f972008-07-15 13:35:23 +0200247 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400248 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200249}
250
wdenkbf9e3b32004-02-12 00:47:09 +0000251#ifdef DEBUG
252/*-----------------------------------------------------------------------
253 * Debug support
254 */
Mario Six188a5562018-01-26 14:43:31 +0100255static void print_longlong(char *str, unsigned long long data)
wdenkbf9e3b32004-02-12 00:47:09 +0000256{
257 int i;
258 char *cp;
259
Mario Six640f4e32018-01-26 14:43:36 +0100260 cp = (char *)&data;
wdenkbf9e3b32004-02-12 00:47:09 +0000261 for (i = 0; i < 8; i++)
Mario Six188a5562018-01-26 14:43:31 +0100262 sprintf(&str[i * 2], "%2.2x", *cp++);
wdenkbf9e3b32004-02-12 00:47:09 +0000263}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200264
Mario Six188a5562018-01-26 14:43:31 +0100265static void flash_printqry(struct cfi_qry *qry)
wdenkbf9e3b32004-02-12 00:47:09 +0000266{
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100267 u8 *p = (u8 *)qry;
wdenkbf9e3b32004-02-12 00:47:09 +0000268 int x, y;
269
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100270 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
271 debug("%02x : ", x);
272 for (y = 0; y < 16; y++)
273 debug("%2.2x ", p[x + y]);
274 debug(" ");
wdenkbf9e3b32004-02-12 00:47:09 +0000275 for (y = 0; y < 16; y++) {
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100276 unsigned char c = p[x + y];
Mario Six7223a8c2018-01-26 14:43:37 +0100277
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100278 if (c >= 0x20 && c <= 0x7e)
279 debug("%c", c);
280 else
281 debug(".");
wdenkbf9e3b32004-02-12 00:47:09 +0000282 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100283 debug("\n");
wdenkbf9e3b32004-02-12 00:47:09 +0000284 }
285}
wdenkbf9e3b32004-02-12 00:47:09 +0000286#endif
287
wdenk5653fc32004-02-08 22:55:38 +0000288/*-----------------------------------------------------------------------
289 * read a character at a port width address
290 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100291static inline uchar flash_read_uchar(flash_info_t *info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000292{
293 uchar *cp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100294 uchar retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000295
Mario Six188a5562018-01-26 14:43:31 +0100296 cp = flash_map(info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200297#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100298 retval = flash_read8(cp);
wdenkbf9e3b32004-02-12 00:47:09 +0000299#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100300 retval = flash_read8(cp + info->portwidth - 1);
wdenkbf9e3b32004-02-12 00:47:09 +0000301#endif
Mario Six188a5562018-01-26 14:43:31 +0100302 flash_unmap(info, 0, offset, cp);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100303 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000304}
305
306/*-----------------------------------------------------------------------
Tor Krill90447ec2008-03-28 11:29:10 +0100307 * read a word at a port width address, assume 16bit bus
308 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100309static inline ushort flash_read_word(flash_info_t *info, uint offset)
Tor Krill90447ec2008-03-28 11:29:10 +0100310{
311 ushort *addr, retval;
312
Mario Six188a5562018-01-26 14:43:31 +0100313 addr = flash_map(info, 0, offset);
314 retval = flash_read16(addr);
315 flash_unmap(info, 0, offset, addr);
Tor Krill90447ec2008-03-28 11:29:10 +0100316 return retval;
317}
318
Tor Krill90447ec2008-03-28 11:29:10 +0100319/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +0100320 * read a long word by picking the least significant byte of each maximum
wdenk5653fc32004-02-08 22:55:38 +0000321 * port size word. Swap for ppc format.
322 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100323static ulong flash_read_long (flash_info_t *info, flash_sect_t sect,
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100324 uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000325{
wdenkbf9e3b32004-02-12 00:47:09 +0000326 uchar *addr;
327 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000328
wdenkbf9e3b32004-02-12 00:47:09 +0000329#ifdef DEBUG
330 int x;
331#endif
Mario Six188a5562018-01-26 14:43:31 +0100332 addr = flash_map(info, sect, offset);
wdenkbf9e3b32004-02-12 00:47:09 +0000333
334#ifdef DEBUG
Mario Six188a5562018-01-26 14:43:31 +0100335 debug("long addr is at %p info->portwidth = %d\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100336 info->portwidth);
Mario Six0412e902018-01-26 14:43:38 +0100337 for (x = 0; x < 4 * info->portwidth; x++)
Mario Six188a5562018-01-26 14:43:31 +0100338 debug("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenkbf9e3b32004-02-12 00:47:09 +0000339#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200340#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100341 retval = ((flash_read8(addr) << 16) |
342 (flash_read8(addr + info->portwidth) << 24) |
343 (flash_read8(addr + 2 * info->portwidth)) |
344 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenkbf9e3b32004-02-12 00:47:09 +0000345#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100346 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
347 (flash_read8(addr + info->portwidth - 1) << 16) |
348 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
349 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenkbf9e3b32004-02-12 00:47:09 +0000350#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100351 flash_unmap(info, sect, offset, addr);
352
wdenkbf9e3b32004-02-12 00:47:09 +0000353 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000354}
355
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200356/*
357 * Write a proper sized command to the correct address
358 */
Marek Vasut236c49a2017-08-20 17:20:00 +0200359static void flash_write_cmd(flash_info_t *info, flash_sect_t sect,
360 uint offset, u32 cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200361{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100362 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200363 cfiword_t cword;
364
Mario Six188a5562018-01-26 14:43:31 +0100365 addr = flash_map(info, sect, offset);
366 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200367 switch (info->portwidth) {
368 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +0100369 debug("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Mario Sixc0350fb2018-01-26 14:43:55 +0100370 cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100371 flash_write8(cword.w8, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200372 break;
373 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +0100374 debug("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100375 cmd, cword.w16,
376 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100377 flash_write16(cword.w16, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200378 break;
379 case FLASH_CFI_32BIT:
Mario Six188a5562018-01-26 14:43:31 +0100380 debug("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100381 cmd, cword.w32,
382 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100383 flash_write32(cword.w32, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200384 break;
385 case FLASH_CFI_64BIT:
386#ifdef DEBUG
387 {
388 char str[20];
389
Mario Six188a5562018-01-26 14:43:31 +0100390 print_longlong(str, cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200391
Mario Six188a5562018-01-26 14:43:31 +0100392 debug("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Mario Sixc0350fb2018-01-26 14:43:55 +0100393 addr, cmd, str,
394 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200395 }
396#endif
Ryan Harkin622b9522015-10-23 16:50:51 +0100397 flash_write64(cword.w64, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200398 break;
399 }
400
401 /* Ensure all the instructions are fully finished */
402 sync();
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100403
404 flash_unmap(info, sect, offset, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200405}
406
Mario Sixca2b07a2018-01-26 14:43:32 +0100407static void flash_unlock_seq(flash_info_t *info, flash_sect_t sect)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200408{
Mario Six188a5562018-01-26 14:43:31 +0100409 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
410 flash_write_cmd(info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200411}
412
413/*-----------------------------------------------------------------------
414 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100415static int flash_isequal(flash_info_t *info, flash_sect_t sect, uint offset,
416 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200417{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100418 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200419 cfiword_t cword;
420 int retval;
421
Mario Six188a5562018-01-26 14:43:31 +0100422 addr = flash_map(info, sect, offset);
423 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200424
Mario Six188a5562018-01-26 14:43:31 +0100425 debug("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200426 switch (info->portwidth) {
427 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +0100428 debug("is= %x %x\n", flash_read8(addr), cword.w8);
Ryan Harkin622b9522015-10-23 16:50:51 +0100429 retval = (flash_read8(addr) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200430 break;
431 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +0100432 debug("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16);
Ryan Harkin622b9522015-10-23 16:50:51 +0100433 retval = (flash_read16(addr) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200434 break;
435 case FLASH_CFI_32BIT:
Mario Six188a5562018-01-26 14:43:31 +0100436 debug("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32);
Ryan Harkin622b9522015-10-23 16:50:51 +0100437 retval = (flash_read32(addr) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200438 break;
439 case FLASH_CFI_64BIT:
440#ifdef DEBUG
441 {
442 char str1[20];
443 char str2[20];
444
Mario Six188a5562018-01-26 14:43:31 +0100445 print_longlong(str1, flash_read64(addr));
446 print_longlong(str2, cword.w64);
447 debug("is= %s %s\n", str1, str2);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200448 }
449#endif
Ryan Harkin622b9522015-10-23 16:50:51 +0100450 retval = (flash_read64(addr) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200451 break;
452 default:
453 retval = 0;
454 break;
455 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100456 flash_unmap(info, sect, offset, addr);
457
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200458 return retval;
459}
460
461/*-----------------------------------------------------------------------
462 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100463static int flash_isset(flash_info_t *info, flash_sect_t sect, uint offset,
464 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200465{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100466 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200467 cfiword_t cword;
468 int retval;
469
Mario Six188a5562018-01-26 14:43:31 +0100470 addr = flash_map(info, sect, offset);
471 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200472 switch (info->portwidth) {
473 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100474 retval = ((flash_read8(addr) & cword.w8) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200475 break;
476 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100477 retval = ((flash_read16(addr) & cword.w16) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200478 break;
479 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100480 retval = ((flash_read32(addr) & cword.w32) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200481 break;
482 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100483 retval = ((flash_read64(addr) & cword.w64) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200484 break;
485 default:
486 retval = 0;
487 break;
488 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100489 flash_unmap(info, sect, offset, addr);
490
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200491 return retval;
492}
493
494/*-----------------------------------------------------------------------
495 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100496static int flash_toggle(flash_info_t *info, flash_sect_t sect, uint offset,
497 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200498{
Mario Six53128382018-01-26 14:43:49 +0100499 u8 *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200500 cfiword_t cword;
501 int retval;
502
Mario Six188a5562018-01-26 14:43:31 +0100503 addr = flash_map(info, sect, offset);
504 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200505 switch (info->portwidth) {
506 case FLASH_CFI_8BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200507 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200508 break;
509 case FLASH_CFI_16BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200510 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200511 break;
512 case FLASH_CFI_32BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200513 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200514 break;
515 case FLASH_CFI_64BIT:
Mario Sixb1683862018-01-26 14:43:33 +0100516 retval = ((flash_read32(addr) != flash_read32(addr)) ||
Mario Six640f4e32018-01-26 14:43:36 +0100517 (flash_read32(addr + 4) != flash_read32(addr + 4)));
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200518 break;
519 default:
520 retval = 0;
521 break;
522 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100523 flash_unmap(info, sect, offset, addr);
524
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200525 return retval;
526}
527
528/*
529 * flash_is_busy - check to see if the flash is busy
530 *
531 * This routine checks the status of the chip and returns true if the
532 * chip is busy.
533 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100534static int flash_is_busy(flash_info_t *info, flash_sect_t sect)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200535{
536 int retval;
537
538 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400539 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200540 case CFI_CMDSET_INTEL_STANDARD:
541 case CFI_CMDSET_INTEL_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +0100542 retval = !flash_isset(info, sect, 0, FLASH_STATUS_DONE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200543 break;
544 case CFI_CMDSET_AMD_STANDARD:
545 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100546#ifdef CONFIG_FLASH_CFI_LEGACY
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200547 case CFI_CMDSET_AMD_LEGACY:
548#endif
Marek Vasut72443c72017-09-12 19:09:31 +0200549 if (info->sr_supported) {
Mario Six188a5562018-01-26 14:43:31 +0100550 flash_write_cmd(info, sect, info->addr_unlock1,
Mario Sixc0350fb2018-01-26 14:43:55 +0100551 FLASH_CMD_READ_STATUS);
Mario Six188a5562018-01-26 14:43:31 +0100552 retval = !flash_isset(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100553 FLASH_STATUS_DONE);
Marek Vasut72443c72017-09-12 19:09:31 +0200554 } else {
Mario Six188a5562018-01-26 14:43:31 +0100555 retval = flash_toggle(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100556 AMD_STATUS_TOGGLE);
Marek Vasut72443c72017-09-12 19:09:31 +0200557 }
558
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200559 break;
560 default:
561 retval = 0;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100562 }
Mario Six38d28312018-01-26 14:43:40 +0100563 debug("%s: %d\n", __func__, retval);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200564 return retval;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100565}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200566
567/*-----------------------------------------------------------------------
568 * wait for XSR.7 to be set. Time out with an error if it does not.
569 * This routine does not set the flash to read-array mode.
570 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100571static int flash_status_check(flash_info_t *info, flash_sect_t sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100572 ulong tout, char *prompt)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200573{
574 ulong start;
575
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200576#if CONFIG_SYS_HZ != 1000
Mario Sixddcf0542018-01-26 14:43:54 +0100577 /* Avoid overflow for large HZ */
Renato Andreolac40c94a2010-03-24 23:00:47 +0800578 if ((ulong)CONFIG_SYS_HZ > 100000)
Mario Sixddcf0542018-01-26 14:43:54 +0100579 tout *= (ulong)CONFIG_SYS_HZ / 1000;
Renato Andreolac40c94a2010-03-24 23:00:47 +0800580 else
581 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200582#endif
583
584 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000585#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800586 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000587#endif
Mario Six188a5562018-01-26 14:43:31 +0100588 start = get_timer(0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100589 WATCHDOG_RESET();
Mario Six188a5562018-01-26 14:43:31 +0100590 while (flash_is_busy(info, sector)) {
591 if (get_timer(start) > tout) {
592 printf("Flash %s timeout at address %lx data %lx\n",
Mario Sixc0350fb2018-01-26 14:43:55 +0100593 prompt, info->start[sector],
594 flash_read_long(info, sector, 0));
Mario Six188a5562018-01-26 14:43:31 +0100595 flash_write_cmd(info, sector, 0, info->cmd_reset);
Stefan Roesee303be22013-04-12 19:04:54 +0200596 udelay(1);
Mario Six9dbaebc2018-01-26 14:43:52 +0100597 return ERR_TIMEOUT;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200598 }
Mario Six188a5562018-01-26 14:43:31 +0100599 udelay(1); /* also triggers watchdog */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200600 }
601 return ERR_OK;
602}
603
604/*-----------------------------------------------------------------------
605 * Wait for XSR.7 to be set, if it times out print an error, otherwise
606 * do a full status check.
607 *
608 * This routine sets the flash to read-array mode.
609 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100610static int flash_full_status_check(flash_info_t *info, flash_sect_t sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100611 ulong tout, char *prompt)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200612{
613 int retcode;
614
Mario Six188a5562018-01-26 14:43:31 +0100615 retcode = flash_status_check(info, sector, tout, prompt);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200616 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400617 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200618 case CFI_CMDSET_INTEL_EXTENDED:
619 case CFI_CMDSET_INTEL_STANDARD:
Mario Six4f89da42018-01-26 14:43:42 +0100620 if (retcode == ERR_OK &&
Mario Sixc0350fb2018-01-26 14:43:55 +0100621 !flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200622 retcode = ERR_INVAL;
Mario Six188a5562018-01-26 14:43:31 +0100623 printf("Flash %s error at address %lx\n", prompt,
Mario Sixc0350fb2018-01-26 14:43:55 +0100624 info->start[sector]);
Mario Six188a5562018-01-26 14:43:31 +0100625 if (flash_isset(info, sector, 0, FLASH_STATUS_ECLBS |
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200626 FLASH_STATUS_PSLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100627 puts("Command Sequence Error.\n");
628 } else if (flash_isset(info, sector, 0,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200629 FLASH_STATUS_ECLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100630 puts("Block Erase Error.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200631 retcode = ERR_NOT_ERASED;
Mario Six188a5562018-01-26 14:43:31 +0100632 } else if (flash_isset(info, sector, 0,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200633 FLASH_STATUS_PSLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100634 puts("Locking Error\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200635 }
Mario Six188a5562018-01-26 14:43:31 +0100636 if (flash_isset(info, sector, 0, FLASH_STATUS_DPS)) {
637 puts("Block locked.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200638 retcode = ERR_PROTECTED;
639 }
Mario Six188a5562018-01-26 14:43:31 +0100640 if (flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
641 puts("Vpp Low Error.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200642 }
Mario Six188a5562018-01-26 14:43:31 +0100643 flash_write_cmd(info, sector, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -0700644 udelay(1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200645 break;
646 default:
647 break;
648 }
649 return retcode;
650}
651
Thomas Choue5720822010-03-26 08:17:00 +0800652static int use_flash_status_poll(flash_info_t *info)
653{
654#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
655 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
656 info->vendor == CFI_CMDSET_AMD_STANDARD)
657 return 1;
658#endif
659 return 0;
660}
661
662static int flash_status_poll(flash_info_t *info, void *src, void *dst,
663 ulong tout, char *prompt)
664{
665#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
666 ulong start;
667 int ready;
668
669#if CONFIG_SYS_HZ != 1000
Mario Sixddcf0542018-01-26 14:43:54 +0100670 /* Avoid overflow for large HZ */
Thomas Choue5720822010-03-26 08:17:00 +0800671 if ((ulong)CONFIG_SYS_HZ > 100000)
Mario Sixddcf0542018-01-26 14:43:54 +0100672 tout *= (ulong)CONFIG_SYS_HZ / 1000;
Thomas Choue5720822010-03-26 08:17:00 +0800673 else
674 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
675#endif
676
677 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000678#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800679 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000680#endif
Thomas Choue5720822010-03-26 08:17:00 +0800681 start = get_timer(0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100682 WATCHDOG_RESET();
Thomas Choue5720822010-03-26 08:17:00 +0800683 while (1) {
684 switch (info->portwidth) {
685 case FLASH_CFI_8BIT:
686 ready = flash_read8(dst) == flash_read8(src);
687 break;
688 case FLASH_CFI_16BIT:
689 ready = flash_read16(dst) == flash_read16(src);
690 break;
691 case FLASH_CFI_32BIT:
692 ready = flash_read32(dst) == flash_read32(src);
693 break;
694 case FLASH_CFI_64BIT:
695 ready = flash_read64(dst) == flash_read64(src);
696 break;
697 default:
698 ready = 0;
699 break;
700 }
701 if (ready)
702 break;
703 if (get_timer(start) > tout) {
704 printf("Flash %s timeout at address %lx data %lx\n",
705 prompt, (ulong)dst, (ulong)flash_read8(dst));
Mario Six9dbaebc2018-01-26 14:43:52 +0100706 return ERR_TIMEOUT;
Thomas Choue5720822010-03-26 08:17:00 +0800707 }
708 udelay(1); /* also triggers watchdog */
709 }
710#endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
711 return ERR_OK;
712}
713
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200714/*-----------------------------------------------------------------------
715 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100716static void flash_add_byte(flash_info_t *info, cfiword_t *cword, uchar c)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200717{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200718#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200719 unsigned short w;
720 unsigned int l;
721 unsigned long long ll;
722#endif
723
724 switch (info->portwidth) {
725 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100726 cword->w8 = c;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200727 break;
728 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200729#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200730 w = c;
731 w <<= 8;
Ryan Harkin622b9522015-10-23 16:50:51 +0100732 cword->w16 = (cword->w16 >> 8) | w;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100733#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100734 cword->w16 = (cword->w16 << 8) | c;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100735#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200736 break;
737 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200738#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200739 l = c;
740 l <<= 24;
Ryan Harkin622b9522015-10-23 16:50:51 +0100741 cword->w32 = (cword->w32 >> 8) | l;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200742#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100743 cword->w32 = (cword->w32 << 8) | c;
Stefan Roese2662b402006-04-01 13:41:03 +0200744#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200745 break;
746 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200747#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200748 ll = c;
749 ll <<= 56;
Ryan Harkin622b9522015-10-23 16:50:51 +0100750 cword->w64 = (cword->w64 >> 8) | ll;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200751#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100752 cword->w64 = (cword->w64 << 8) | c;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200753#endif
754 break;
wdenk5653fc32004-02-08 22:55:38 +0000755 }
wdenk5653fc32004-02-08 22:55:38 +0000756}
757
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100758/*
759 * Loop through the sector table starting from the previously found sector.
760 * Searches forwards or backwards, dependent on the passed address.
wdenk5653fc32004-02-08 22:55:38 +0000761 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100762static flash_sect_t find_sector(flash_info_t *info, ulong addr)
wdenk7680c142005-05-16 15:23:22 +0000763{
Kim Phillips11dc4012012-10-29 13:34:45 +0000764 static flash_sect_t saved_sector; /* previously found sector */
Stefan Roesee303be22013-04-12 19:04:54 +0200765 static flash_info_t *saved_info; /* previously used flash bank */
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100766 flash_sect_t sector = saved_sector;
wdenk7680c142005-05-16 15:23:22 +0000767
Mario Six4f89da42018-01-26 14:43:42 +0100768 if (info != saved_info || sector >= info->sector_count)
Stefan Roesee303be22013-04-12 19:04:54 +0200769 sector = 0;
770
Mario Six5701ba82018-01-26 14:43:53 +0100771 while ((sector < info->sector_count - 1) &&
Mario Sixc0350fb2018-01-26 14:43:55 +0100772 (info->start[sector] < addr))
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100773 sector++;
774 while ((info->start[sector] > addr) && (sector > 0))
775 /*
776 * also decrements the sector in case of an overshot
777 * in the first loop
778 */
779 sector--;
780
781 saved_sector = sector;
Stefan Roesee303be22013-04-12 19:04:54 +0200782 saved_info = info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200783 return sector;
wdenk7680c142005-05-16 15:23:22 +0000784}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200785
786/*-----------------------------------------------------------------------
787 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100788static int flash_write_cfiword(flash_info_t *info, ulong dest, cfiword_t cword)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200789{
Becky Bruce09ce9922009-02-02 16:34:51 -0600790 void *dstaddr = (void *)dest;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200791 int flag;
Jens Gehrleina7292872008-12-16 17:25:54 +0100792 flash_sect_t sect = 0;
793 char sect_found = 0;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200794
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200795 /* Check if Flash is (sufficiently) erased */
796 switch (info->portwidth) {
797 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100798 flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200799 break;
800 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100801 flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200802 break;
803 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100804 flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200805 break;
806 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100807 flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200808 break;
809 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100810 flag = 0;
811 break;
812 }
Becky Bruce09ce9922009-02-02 16:34:51 -0600813 if (!flag)
Stefan Roese0dc80e22007-12-27 07:50:54 +0100814 return ERR_NOT_ERASED;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200815
816 /* Disable interrupts which might cause a timeout here */
Mario Six188a5562018-01-26 14:43:31 +0100817 flag = disable_interrupts();
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200818
819 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400820 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200821 case CFI_CMDSET_INTEL_EXTENDED:
822 case CFI_CMDSET_INTEL_STANDARD:
Mario Six188a5562018-01-26 14:43:31 +0100823 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
824 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200825 break;
826 case CFI_CMDSET_AMD_EXTENDED:
827 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout0d01f662008-10-09 01:26:36 -0500828 sect = find_sector(info, dest);
Mario Six188a5562018-01-26 14:43:31 +0100829 flash_unlock_seq(info, sect);
830 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrleina7292872008-12-16 17:25:54 +0100831 sect_found = 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200832 break;
Po-Yu Chuangb4db4a72009-07-10 18:03:57 +0800833#ifdef CONFIG_FLASH_CFI_LEGACY
834 case CFI_CMDSET_AMD_LEGACY:
835 sect = find_sector(info, dest);
Mario Six188a5562018-01-26 14:43:31 +0100836 flash_unlock_seq(info, 0);
837 flash_write_cmd(info, 0, info->addr_unlock1, AMD_CMD_WRITE);
Po-Yu Chuangb4db4a72009-07-10 18:03:57 +0800838 sect_found = 1;
839 break;
840#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200841 }
842
843 switch (info->portwidth) {
844 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100845 flash_write8(cword.w8, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200846 break;
847 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100848 flash_write16(cword.w16, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200849 break;
850 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100851 flash_write32(cword.w32, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200852 break;
853 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100854 flash_write64(cword.w64, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200855 break;
856 }
857
858 /* re-enable interrupts if necessary */
859 if (flag)
Mario Six188a5562018-01-26 14:43:31 +0100860 enable_interrupts();
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200861
Jens Gehrleina7292872008-12-16 17:25:54 +0100862 if (!sect_found)
Mario Six188a5562018-01-26 14:43:31 +0100863 sect = find_sector(info, dest);
Jens Gehrleina7292872008-12-16 17:25:54 +0100864
Thomas Choue5720822010-03-26 08:17:00 +0800865 if (use_flash_status_poll(info))
866 return flash_status_poll(info, &cword, dstaddr,
867 info->write_tout, "write");
868 else
869 return flash_full_status_check(info, sect,
870 info->write_tout, "write");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200871}
872
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200873#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200874
Mario Sixca2b07a2018-01-26 14:43:32 +0100875static int flash_write_cfibuffer(flash_info_t *info, ulong dest, uchar *cp,
Mario Sixc0350fb2018-01-26 14:43:55 +0100876 int len)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200877{
878 flash_sect_t sector;
879 int cnt;
880 int retcode;
Mario Six53128382018-01-26 14:43:49 +0100881 u8 *src = cp;
882 u8 *dst = (u8 *)dest;
883 u8 *dst2 = dst;
Tao Hou85c344e2012-03-15 23:33:58 +0800884 int flag = 1;
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200885 uint offset = 0;
886 unsigned int shift;
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400887 uchar write_cmd;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100888
Stefan Roese0dc80e22007-12-27 07:50:54 +0100889 switch (info->portwidth) {
890 case FLASH_CFI_8BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200891 shift = 0;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100892 break;
893 case FLASH_CFI_16BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200894 shift = 1;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100895 break;
896 case FLASH_CFI_32BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200897 shift = 2;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100898 break;
899 case FLASH_CFI_64BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200900 shift = 3;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100901 break;
902 default:
903 retcode = ERR_INVAL;
904 goto out_unmap;
905 }
906
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200907 cnt = len >> shift;
908
Tao Hou85c344e2012-03-15 23:33:58 +0800909 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese0dc80e22007-12-27 07:50:54 +0100910 switch (info->portwidth) {
911 case FLASH_CFI_8BIT:
912 flag = ((flash_read8(dst2) & flash_read8(src)) ==
913 flash_read8(src));
914 src += 1, dst2 += 1;
915 break;
916 case FLASH_CFI_16BIT:
917 flag = ((flash_read16(dst2) & flash_read16(src)) ==
918 flash_read16(src));
919 src += 2, dst2 += 2;
920 break;
921 case FLASH_CFI_32BIT:
922 flag = ((flash_read32(dst2) & flash_read32(src)) ==
923 flash_read32(src));
924 src += 4, dst2 += 4;
925 break;
926 case FLASH_CFI_64BIT:
927 flag = ((flash_read64(dst2) & flash_read64(src)) ==
928 flash_read64(src));
929 src += 8, dst2 += 8;
930 break;
931 }
932 }
933 if (!flag) {
934 retcode = ERR_NOT_ERASED;
935 goto out_unmap;
936 }
937
938 src = cp;
Mario Six188a5562018-01-26 14:43:31 +0100939 sector = find_sector(info, dest);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200940
941 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400942 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200943 case CFI_CMDSET_INTEL_STANDARD:
944 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400945 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
Mario Sixddcf0542018-01-26 14:43:54 +0100946 FLASH_CMD_WRITE_BUFFER_PROG :
947 FLASH_CMD_WRITE_TO_BUFFER;
Mario Six188a5562018-01-26 14:43:31 +0100948 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
949 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
950 flash_write_cmd(info, sector, 0, write_cmd);
951 retcode = flash_status_check(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100952 info->buffer_write_tout,
953 "write to buffer");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200954 if (retcode == ERR_OK) {
955 /* reduce the number of loops by the width of
Mario Sixa6d18f22018-01-26 14:43:41 +0100956 * the port
957 */
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200958 cnt = len >> shift;
Mario Six188a5562018-01-26 14:43:31 +0100959 flash_write_cmd(info, sector, 0, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200960 while (cnt-- > 0) {
961 switch (info->portwidth) {
962 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100963 flash_write8(flash_read8(src), dst);
964 src += 1, dst += 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200965 break;
966 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100967 flash_write16(flash_read16(src), dst);
968 src += 2, dst += 2;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200969 break;
970 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100971 flash_write32(flash_read32(src), dst);
972 src += 4, dst += 4;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200973 break;
974 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100975 flash_write64(flash_read64(src), dst);
976 src += 8, dst += 8;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200977 break;
978 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100979 retcode = ERR_INVAL;
980 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200981 }
982 }
Mario Six188a5562018-01-26 14:43:31 +0100983 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100984 FLASH_CMD_WRITE_BUFFER_CONFIRM);
Mario Six188a5562018-01-26 14:43:31 +0100985 retcode = flash_full_status_check(
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200986 info, sector, info->buffer_write_tout,
987 "buffer write");
988 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100989
990 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200991
992 case CFI_CMDSET_AMD_STANDARD:
993 case CFI_CMDSET_AMD_EXTENDED:
Rouven Behr7570a0c2016-04-10 13:38:13 +0200994 flash_unlock_seq(info, sector);
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200995
996#ifdef CONFIG_FLASH_SPANSION_S29WS_N
997 offset = ((unsigned long)dst - info->start[sector]) >> shift;
998#endif
999 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
1000 cnt = len >> shift;
John Schmoller7dedefd2009-08-12 10:55:47 -05001001 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001002
1003 switch (info->portwidth) {
1004 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001005 while (cnt-- > 0) {
1006 flash_write8(flash_read8(src), dst);
1007 src += 1, dst += 1;
1008 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001009 break;
1010 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001011 while (cnt-- > 0) {
1012 flash_write16(flash_read16(src), dst);
1013 src += 2, dst += 2;
1014 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001015 break;
1016 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001017 while (cnt-- > 0) {
1018 flash_write32(flash_read32(src), dst);
1019 src += 4, dst += 4;
1020 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001021 break;
1022 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001023 while (cnt-- > 0) {
1024 flash_write64(flash_read64(src), dst);
1025 src += 8, dst += 8;
1026 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001027 break;
1028 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001029 retcode = ERR_INVAL;
1030 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001031 }
1032
Mario Six188a5562018-01-26 14:43:31 +01001033 flash_write_cmd(info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Choue5720822010-03-26 08:17:00 +08001034 if (use_flash_status_poll(info))
1035 retcode = flash_status_poll(info, src - (1 << shift),
1036 dst - (1 << shift),
1037 info->buffer_write_tout,
1038 "buffer write");
1039 else
1040 retcode = flash_full_status_check(info, sector,
1041 info->buffer_write_tout,
1042 "buffer write");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001043 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001044
1045 default:
Mario Six188a5562018-01-26 14:43:31 +01001046 debug("Unknown Command Set\n");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001047 retcode = ERR_INVAL;
1048 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001049 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001050
1051out_unmap:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001052 return retcode;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001053}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001054#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001055
wdenk7680c142005-05-16 15:23:22 +00001056/*-----------------------------------------------------------------------
1057 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001058int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +00001059{
1060 int rcode = 0;
1061 int prot;
1062 flash_sect_t sect;
Thomas Choue5720822010-03-26 08:17:00 +08001063 int st;
wdenk5653fc32004-02-08 22:55:38 +00001064
wdenkbf9e3b32004-02-12 00:47:09 +00001065 if (info->flash_id != FLASH_MAN_CFI) {
Mario Six188a5562018-01-26 14:43:31 +01001066 puts("Can't erase unknown flash type - aborted\n");
wdenk5653fc32004-02-08 22:55:38 +00001067 return 1;
1068 }
Mario Six4f89da42018-01-26 14:43:42 +01001069 if (s_first < 0 || s_first > s_last) {
Mario Six188a5562018-01-26 14:43:31 +01001070 puts("- no sectors to erase\n");
wdenk5653fc32004-02-08 22:55:38 +00001071 return 1;
1072 }
1073
1074 prot = 0;
Mario Six0412e902018-01-26 14:43:38 +01001075 for (sect = s_first; sect <= s_last; ++sect)
1076 if (info->protect[sect])
wdenk5653fc32004-02-08 22:55:38 +00001077 prot++;
wdenk5653fc32004-02-08 22:55:38 +00001078 if (prot) {
Mario Six188a5562018-01-26 14:43:31 +01001079 printf("- Warning: %d protected sectors will not be erased!\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001080 prot);
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001081 } else if (flash_verbose) {
Mario Six188a5562018-01-26 14:43:31 +01001082 putc('\n');
wdenk5653fc32004-02-08 22:55:38 +00001083 }
1084
wdenkbf9e3b32004-02-12 00:47:09 +00001085 for (sect = s_first; sect <= s_last; sect++) {
Joe Hershbergerde15a062012-08-17 15:36:41 -05001086 if (ctrlc()) {
1087 printf("\n");
1088 return 1;
1089 }
1090
wdenk5653fc32004-02-08 22:55:38 +00001091 if (info->protect[sect] == 0) { /* not protected */
Joe Hershberger6822a642012-08-17 15:36:40 -05001092#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1093 int k;
1094 int size;
1095 int erased;
1096 u32 *flash;
1097
1098 /*
1099 * Check if whole sector is erased
1100 */
1101 size = flash_sector_size(info, sect);
1102 erased = 1;
1103 flash = (u32 *)info->start[sect];
1104 /* divide by 4 for longword access */
1105 size = size >> 2;
1106 for (k = 0; k < size; k++) {
1107 if (flash_read32(flash++) != 0xffffffff) {
1108 erased = 0;
1109 break;
1110 }
1111 }
1112 if (erased) {
1113 if (flash_verbose)
1114 putc(',');
1115 continue;
1116 }
1117#endif
wdenkbf9e3b32004-02-12 00:47:09 +00001118 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001119 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00001120 case CFI_CMDSET_INTEL_STANDARD:
1121 case CFI_CMDSET_INTEL_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +01001122 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001123 FLASH_CMD_CLEAR_STATUS);
Mario Six188a5562018-01-26 14:43:31 +01001124 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001125 FLASH_CMD_BLOCK_ERASE);
Mario Six188a5562018-01-26 14:43:31 +01001126 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001127 FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +00001128 break;
1129 case CFI_CMDSET_AMD_STANDARD:
1130 case CFI_CMDSET_AMD_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +01001131 flash_unlock_seq(info, sect);
1132 flash_write_cmd(info, sect,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001133 info->addr_unlock1,
1134 AMD_CMD_ERASE_START);
Mario Six188a5562018-01-26 14:43:31 +01001135 flash_unlock_seq(info, sect);
1136 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001137 info->cmd_erase_sector);
wdenk5653fc32004-02-08 22:55:38 +00001138 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001139#ifdef CONFIG_FLASH_CFI_LEGACY
1140 case CFI_CMDSET_AMD_LEGACY:
Mario Six188a5562018-01-26 14:43:31 +01001141 flash_unlock_seq(info, 0);
1142 flash_write_cmd(info, 0, info->addr_unlock1,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001143 AMD_CMD_ERASE_START);
Mario Six188a5562018-01-26 14:43:31 +01001144 flash_unlock_seq(info, 0);
1145 flash_write_cmd(info, sect, 0,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001146 AMD_CMD_ERASE_SECTOR);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001147 break;
1148#endif
wdenk5653fc32004-02-08 22:55:38 +00001149 default:
Mario Six9f720212018-01-26 14:43:44 +01001150 debug("Unknown flash vendor %d\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001151 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +00001152 break;
1153 }
1154
Thomas Choue5720822010-03-26 08:17:00 +08001155 if (use_flash_status_poll(info)) {
Kim Phillips11dc4012012-10-29 13:34:45 +00001156 cfiword_t cword;
Thomas Choue5720822010-03-26 08:17:00 +08001157 void *dest;
Mario Six7223a8c2018-01-26 14:43:37 +01001158
Ryan Harkin622b9522015-10-23 16:50:51 +01001159 cword.w64 = 0xffffffffffffffffULL;
Thomas Choue5720822010-03-26 08:17:00 +08001160 dest = flash_map(info, sect, 0);
1161 st = flash_status_poll(info, &cword, dest,
Mario Sixddcf0542018-01-26 14:43:54 +01001162 info->erase_blk_tout,
1163 "erase");
Thomas Choue5720822010-03-26 08:17:00 +08001164 flash_unmap(info, sect, 0, dest);
Mario Six12d7fed2018-01-26 14:43:43 +01001165 } else {
Thomas Choue5720822010-03-26 08:17:00 +08001166 st = flash_full_status_check(info, sect,
1167 info->erase_blk_tout,
1168 "erase");
Mario Six12d7fed2018-01-26 14:43:43 +01001169 }
1170
Thomas Choue5720822010-03-26 08:17:00 +08001171 if (st)
wdenk5653fc32004-02-08 22:55:38 +00001172 rcode = 1;
Thomas Choue5720822010-03-26 08:17:00 +08001173 else if (flash_verbose)
Mario Six188a5562018-01-26 14:43:31 +01001174 putc('.');
wdenk5653fc32004-02-08 22:55:38 +00001175 }
1176 }
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001177
1178 if (flash_verbose)
Mario Six188a5562018-01-26 14:43:31 +01001179 puts(" done\n");
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001180
wdenk5653fc32004-02-08 22:55:38 +00001181 return rcode;
1182}
1183
Stefan Roese70084df2010-08-13 09:36:36 +02001184#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1185static int sector_erased(flash_info_t *info, int i)
1186{
1187 int k;
1188 int size;
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001189 u32 *flash;
Stefan Roese70084df2010-08-13 09:36:36 +02001190
1191 /*
1192 * Check if whole sector is erased
1193 */
1194 size = flash_sector_size(info, i);
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001195 flash = (u32 *)info->start[i];
Stefan Roese70084df2010-08-13 09:36:36 +02001196 /* divide by 4 for longword access */
1197 size = size >> 2;
1198
1199 for (k = 0; k < size; k++) {
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001200 if (flash_read32(flash++) != 0xffffffff)
Stefan Roese70084df2010-08-13 09:36:36 +02001201 return 0; /* not erased */
1202 }
1203
1204 return 1; /* erased */
1205}
1206#endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1207
Mario Sixca2b07a2018-01-26 14:43:32 +01001208void flash_print_info(flash_info_t *info)
wdenk5653fc32004-02-08 22:55:38 +00001209{
1210 int i;
1211
1212 if (info->flash_id != FLASH_MAN_CFI) {
Mario Six188a5562018-01-26 14:43:31 +01001213 puts("missing or unknown FLASH type\n");
wdenk5653fc32004-02-08 22:55:38 +00001214 return;
1215 }
1216
Mario Six188a5562018-01-26 14:43:31 +01001217 printf("%s flash (%d x %d)",
Mario Sixc0350fb2018-01-26 14:43:55 +01001218 info->name,
1219 (info->portwidth << 3), (info->chipwidth << 3));
Mario Six640f4e32018-01-26 14:43:36 +01001220 if (info->size < 1024 * 1024)
Mario Six188a5562018-01-26 14:43:31 +01001221 printf(" Size: %ld kB in %d Sectors\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001222 info->size >> 10, info->sector_count);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001223 else
Mario Six188a5562018-01-26 14:43:31 +01001224 printf(" Size: %ld MB in %d Sectors\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001225 info->size >> 20, info->sector_count);
Mario Six188a5562018-01-26 14:43:31 +01001226 printf(" ");
Stefan Roese260421a2006-11-13 13:55:24 +01001227 switch (info->vendor) {
Mario Sixdde09132018-01-26 14:43:35 +01001228 case CFI_CMDSET_INTEL_PROG_REGIONS:
1229 printf("Intel Prog Regions");
1230 break;
1231 case CFI_CMDSET_INTEL_STANDARD:
1232 printf("Intel Standard");
1233 break;
1234 case CFI_CMDSET_INTEL_EXTENDED:
1235 printf("Intel Extended");
1236 break;
1237 case CFI_CMDSET_AMD_STANDARD:
1238 printf("AMD Standard");
1239 break;
1240 case CFI_CMDSET_AMD_EXTENDED:
1241 printf("AMD Extended");
1242 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001243#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Sixdde09132018-01-26 14:43:35 +01001244 case CFI_CMDSET_AMD_LEGACY:
1245 printf("AMD Legacy");
1246 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001247#endif
Mario Sixdde09132018-01-26 14:43:35 +01001248 default:
1249 printf("Unknown (%d)", info->vendor);
1250 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001251 }
Mario Six188a5562018-01-26 14:43:31 +01001252 printf(" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
Mario Sixc0350fb2018-01-26 14:43:55 +01001253 info->manufacturer_id);
Mario Six188a5562018-01-26 14:43:31 +01001254 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
Mario Sixc0350fb2018-01-26 14:43:55 +01001255 info->device_id);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001256 if ((info->device_id & 0xff) == 0x7E) {
1257 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
Mario Sixc0350fb2018-01-26 14:43:55 +01001258 info->device_id2);
Stefan Roese260421a2006-11-13 13:55:24 +01001259 }
Mario Six4f89da42018-01-26 14:43:42 +01001260 if (info->vendor == CFI_CMDSET_AMD_STANDARD && info->legacy_unlock)
Stefan Roesed2af0282012-12-06 15:44:12 +01001261 printf("\n Advanced Sector Protection (PPB) enabled");
Mario Six188a5562018-01-26 14:43:31 +01001262 printf("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001263 info->erase_blk_tout, info->write_tout);
Stefan Roese260421a2006-11-13 13:55:24 +01001264 if (info->buffer_size > 1) {
Mario Six876c52f2018-01-26 14:43:50 +01001265 printf(" Buffer write timeout: %ld ms, ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001266 info->buffer_write_tout);
Mario Six876c52f2018-01-26 14:43:50 +01001267 printf("buffer size: %d bytes\n", info->buffer_size);
Stefan Roese260421a2006-11-13 13:55:24 +01001268 }
wdenk5653fc32004-02-08 22:55:38 +00001269
Mario Six188a5562018-01-26 14:43:31 +01001270 puts("\n Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +00001271 for (i = 0; i < info->sector_count; ++i) {
Kim Phillips2e973942010-07-26 18:35:39 -05001272 if (ctrlc())
Stefan Roese70084df2010-08-13 09:36:36 +02001273 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001274 if ((i % 5) == 0)
Stefan Roese70084df2010-08-13 09:36:36 +02001275 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001276#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
wdenk5653fc32004-02-08 22:55:38 +00001277 /* print empty and read-only info */
Mario Six188a5562018-01-26 14:43:31 +01001278 printf(" %08lX %c %s ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001279 info->start[i],
1280 sector_erased(info, i) ? 'E' : ' ',
1281 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001282#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Mario Six188a5562018-01-26 14:43:31 +01001283 printf(" %08lX %s ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001284 info->start[i],
1285 info->protect[i] ? "RO" : " ");
wdenk5653fc32004-02-08 22:55:38 +00001286#endif
1287 }
Mario Six188a5562018-01-26 14:43:31 +01001288 putc('\n');
wdenk5653fc32004-02-08 22:55:38 +00001289}
1290
1291/*-----------------------------------------------------------------------
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001292 * This is used in a few places in write_buf() to show programming
1293 * progress. Making it a function is nasty because it needs to do side
1294 * effect updates to digit and dots. Repeated code is nasty too, so
1295 * we define it once here.
1296 */
Stefan Roesef0105722008-03-19 07:09:26 +01001297#ifdef CONFIG_FLASH_SHOW_PROGRESS
1298#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001299 if (flash_verbose) { \
1300 dots -= dots_sub; \
Mario Six4f89da42018-01-26 14:43:42 +01001301 if (scale > 0 && dots <= 0) { \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001302 if ((digit % 5) == 0) \
Mario Six188a5562018-01-26 14:43:31 +01001303 printf("%d", digit / 5); \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001304 else \
Mario Six188a5562018-01-26 14:43:31 +01001305 putc('.'); \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001306 digit--; \
1307 dots += scale; \
1308 } \
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001309 }
Stefan Roesef0105722008-03-19 07:09:26 +01001310#else
1311#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1312#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001313
1314/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001315 * Copy memory to flash, returns:
1316 * 0 - OK
1317 * 1 - write timeout
1318 * 2 - Flash not erased
1319 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001320int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +00001321{
1322 ulong wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001323 uchar *p;
wdenk5653fc32004-02-08 22:55:38 +00001324 int aln;
1325 cfiword_t cword;
1326 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001327#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001328 int buffered_size;
1329#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001330#ifdef CONFIG_FLASH_SHOW_PROGRESS
1331 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1332 int scale = 0;
1333 int dots = 0;
1334
1335 /*
1336 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1337 */
1338 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1339 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1340 CONFIG_FLASH_SHOW_PROGRESS);
1341 }
1342#endif
1343
wdenkbf9e3b32004-02-12 00:47:09 +00001344 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +00001345 wp = (addr & ~(info->portwidth - 1));
1346
1347 /* handle unaligned start */
Mario Sixd3525b62018-01-26 14:43:48 +01001348 aln = addr - wp;
1349 if (aln != 0) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001350 cword.w32 = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001351 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001352 for (i = 0; i < aln; ++i)
Mario Six188a5562018-01-26 14:43:31 +01001353 flash_add_byte(info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001354
wdenkbf9e3b32004-02-12 00:47:09 +00001355 for (; (i < info->portwidth) && (cnt > 0); i++) {
Mario Six188a5562018-01-26 14:43:31 +01001356 flash_add_byte(info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001357 cnt--;
wdenk5653fc32004-02-08 22:55:38 +00001358 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001359 for (; (cnt == 0) && (i < info->portwidth); ++i)
Mario Six188a5562018-01-26 14:43:31 +01001360 flash_add_byte(info, &cword, flash_read8(p + i));
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001361
Mario Six188a5562018-01-26 14:43:31 +01001362 rc = flash_write_cfiword(info, wp, cword);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001363 if (rc != 0)
wdenk5653fc32004-02-08 22:55:38 +00001364 return rc;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001365
1366 wp += i;
Stefan Roesef0105722008-03-19 07:09:26 +01001367 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
wdenk5653fc32004-02-08 22:55:38 +00001368 }
1369
wdenkbf9e3b32004-02-12 00:47:09 +00001370 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001371#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001372 buffered_size = (info->portwidth / info->chipwidth);
1373 buffered_size *= info->buffer_size;
1374 while (cnt >= info->portwidth) {
Stefan Roese79b4cda2006-02-28 15:29:58 +01001375 /* prohibit buffer write when buffer_size is 1 */
1376 if (info->buffer_size == 1) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001377 cword.w32 = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01001378 for (i = 0; i < info->portwidth; i++)
Mario Six188a5562018-01-26 14:43:31 +01001379 flash_add_byte(info, &cword, *src++);
Mario Sixd3525b62018-01-26 14:43:48 +01001380 rc = flash_write_cfiword(info, wp, cword);
1381 if (rc != 0)
Stefan Roese79b4cda2006-02-28 15:29:58 +01001382 return rc;
1383 wp += info->portwidth;
1384 cnt -= info->portwidth;
1385 continue;
1386 }
1387
1388 /* write buffer until next buffered_size aligned boundary */
1389 i = buffered_size - (wp % buffered_size);
1390 if (i > cnt)
1391 i = cnt;
Mario Sixd3525b62018-01-26 14:43:48 +01001392 rc = flash_write_cfibuffer(info, wp, src, i);
1393 if (rc != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +00001394 return rc;
Wolfgang Denk8d4ba3d2005-08-12 22:35:59 +02001395 i -= i & (info->portwidth - 1);
wdenk5653fc32004-02-08 22:55:38 +00001396 wp += i;
1397 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +00001398 cnt -= i;
Stefan Roesef0105722008-03-19 07:09:26 +01001399 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001400 /* Only check every once in a while */
1401 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1402 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001403 }
1404#else
wdenkbf9e3b32004-02-12 00:47:09 +00001405 while (cnt >= info->portwidth) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001406 cword.w32 = 0;
Mario Six0412e902018-01-26 14:43:38 +01001407 for (i = 0; i < info->portwidth; i++)
Mario Six188a5562018-01-26 14:43:31 +01001408 flash_add_byte(info, &cword, *src++);
Mario Sixd3525b62018-01-26 14:43:48 +01001409 rc = flash_write_cfiword(info, wp, cword);
1410 if (rc != 0)
wdenk5653fc32004-02-08 22:55:38 +00001411 return rc;
1412 wp += info->portwidth;
1413 cnt -= info->portwidth;
Stefan Roesef0105722008-03-19 07:09:26 +01001414 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001415 /* Only check every once in a while */
1416 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1417 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001418 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001419#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001420
Mario Six0412e902018-01-26 14:43:38 +01001421 if (cnt == 0)
wdenk5653fc32004-02-08 22:55:38 +00001422 return (0);
wdenk5653fc32004-02-08 22:55:38 +00001423
1424 /*
1425 * handle unaligned tail bytes
1426 */
Ryan Harkin622b9522015-10-23 16:50:51 +01001427 cword.w32 = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001428 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001429 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Mario Six188a5562018-01-26 14:43:31 +01001430 flash_add_byte(info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001431 --cnt;
1432 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001433 for (; i < info->portwidth; ++i)
Mario Six188a5562018-01-26 14:43:31 +01001434 flash_add_byte(info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001435
Mario Six188a5562018-01-26 14:43:31 +01001436 return flash_write_cfiword(info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +00001437}
1438
Stefan Roese20043a42012-12-06 15:44:09 +01001439static inline int manufact_match(flash_info_t *info, u32 manu)
1440{
1441 return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
1442}
1443
wdenk5653fc32004-02-08 22:55:38 +00001444/*-----------------------------------------------------------------------
1445 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001446#ifdef CONFIG_SYS_FLASH_PROTECTION
wdenk5653fc32004-02-08 22:55:38 +00001447
Holger Brunck81316a92012-08-09 10:22:41 +02001448static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1449{
Mario Six88ecd8b2018-01-26 14:43:39 +01001450 if (manufact_match(info, INTEL_MANUFACT) &&
Mario Sixc0350fb2018-01-26 14:43:55 +01001451 info->device_id == NUMONYX_256MBIT) {
Holger Brunck81316a92012-08-09 10:22:41 +02001452 /*
1453 * see errata called
1454 * "Numonyx Axcell P33/P30 Specification Update" :)
1455 */
1456 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1457 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1458 prot)) {
1459 /*
1460 * cmd must come before FLASH_CMD_PROTECT + 20us
1461 * Disable interrupts which might cause a timeout here.
1462 */
1463 int flag = disable_interrupts();
1464 unsigned short cmd;
1465
1466 if (prot)
1467 cmd = FLASH_CMD_PROTECT_SET;
1468 else
1469 cmd = FLASH_CMD_PROTECT_CLEAR;
Andre Przywara58eab322016-11-16 00:50:06 +00001470
1471 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
Holger Brunck81316a92012-08-09 10:22:41 +02001472 flash_write_cmd(info, sector, 0, cmd);
1473 /* re-enable interrupts if necessary */
1474 if (flag)
1475 enable_interrupts();
1476 }
1477 return 1;
1478 }
1479 return 0;
1480}
1481
Mario Sixca2b07a2018-01-26 14:43:32 +01001482int flash_real_protect(flash_info_t *info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +00001483{
1484 int retcode = 0;
1485
Rafael Camposbc9019e2008-07-31 10:22:20 +02001486 switch (info->vendor) {
Mario Sixdde09132018-01-26 14:43:35 +01001487 case CFI_CMDSET_INTEL_PROG_REGIONS:
1488 case CFI_CMDSET_INTEL_STANDARD:
1489 case CFI_CMDSET_INTEL_EXTENDED:
1490 if (!cfi_protect_bugfix(info, sector, prot)) {
1491 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001492 FLASH_CMD_CLEAR_STATUS);
Mario Sixdde09132018-01-26 14:43:35 +01001493 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001494 FLASH_CMD_PROTECT);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001495 if (prot)
Mario Sixdde09132018-01-26 14:43:35 +01001496 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001497 FLASH_CMD_PROTECT_SET);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001498 else
Mario Sixdde09132018-01-26 14:43:35 +01001499 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001500 FLASH_CMD_PROTECT_CLEAR);
Mario Sixdde09132018-01-26 14:43:35 +01001501 }
1502 break;
1503 case CFI_CMDSET_AMD_EXTENDED:
1504 case CFI_CMDSET_AMD_STANDARD:
1505 /* U-Boot only checks the first byte */
1506 if (manufact_match(info, ATM_MANUFACT)) {
1507 if (prot) {
1508 flash_unlock_seq(info, 0);
1509 flash_write_cmd(info, 0,
1510 info->addr_unlock1,
1511 ATM_CMD_SOFTLOCK_START);
1512 flash_unlock_seq(info, 0);
1513 flash_write_cmd(info, sector, 0,
1514 ATM_CMD_LOCK_SECT);
1515 } else {
1516 flash_write_cmd(info, 0,
1517 info->addr_unlock1,
1518 AMD_CMD_UNLOCK_START);
1519 if (info->device_id == ATM_ID_BV6416)
1520 flash_write_cmd(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001521 0, ATM_CMD_UNLOCK_SECT);
Mario Sixdde09132018-01-26 14:43:35 +01001522 }
1523 }
1524 if (info->legacy_unlock) {
1525 int flag = disable_interrupts();
1526 int lock_flag;
1527
1528 flash_unlock_seq(info, 0);
1529 flash_write_cmd(info, 0, info->addr_unlock1,
1530 AMD_CMD_SET_PPB_ENTRY);
1531 lock_flag = flash_isset(info, sector, 0, 0x01);
1532 if (prot) {
1533 if (lock_flag) {
1534 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001535 AMD_CMD_PPB_LOCK_BC1);
Mario Sixdde09132018-01-26 14:43:35 +01001536 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001537 AMD_CMD_PPB_LOCK_BC2);
Mario Sixdde09132018-01-26 14:43:35 +01001538 }
1539 debug("sector %ld %slocked\n", sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001540 lock_flag ? "" : "already ");
Mario Sixdde09132018-01-26 14:43:35 +01001541 } else {
1542 if (!lock_flag) {
1543 debug("unlock %ld\n", sector);
1544 flash_write_cmd(info, 0, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001545 AMD_CMD_PPB_UNLOCK_BC1);
Mario Sixdde09132018-01-26 14:43:35 +01001546 flash_write_cmd(info, 0, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001547 AMD_CMD_PPB_UNLOCK_BC2);
Mario Sixdde09132018-01-26 14:43:35 +01001548 }
1549 debug("sector %ld %sunlocked\n", sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001550 !lock_flag ? "" : "already ");
Mario Sixdde09132018-01-26 14:43:35 +01001551 }
1552 if (flag)
1553 enable_interrupts();
1554
1555 if (flash_status_check(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001556 info->erase_blk_tout,
1557 prot ? "protect" : "unprotect"))
Mario Sixdde09132018-01-26 14:43:35 +01001558 printf("status check error\n");
1559
1560 flash_write_cmd(info, 0, 0,
1561 AMD_CMD_SET_PPB_EXIT_BC1);
1562 flash_write_cmd(info, 0, 0,
1563 AMD_CMD_SET_PPB_EXIT_BC2);
1564 }
1565 break;
1566#ifdef CONFIG_FLASH_CFI_LEGACY
1567 case CFI_CMDSET_AMD_LEGACY:
1568 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1569 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
1570 if (prot)
Mario Sixddcf0542018-01-26 14:43:54 +01001571 flash_write_cmd(info, sector, 0,
1572 FLASH_CMD_PROTECT_SET);
Mario Sixdde09132018-01-26 14:43:35 +01001573 else
Mario Sixddcf0542018-01-26 14:43:54 +01001574 flash_write_cmd(info, sector, 0,
1575 FLASH_CMD_PROTECT_CLEAR);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001576#endif
Rafael Camposbc9019e2008-07-31 10:22:20 +02001577 };
wdenk5653fc32004-02-08 22:55:38 +00001578
Stefan Roesedf4e8132010-10-25 18:31:29 +02001579 /*
1580 * Flash needs to be in status register read mode for
1581 * flash_full_status_check() to work correctly
1582 */
1583 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
Mario Sixd3525b62018-01-26 14:43:48 +01001584 retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
Mario Sixc0350fb2018-01-26 14:43:55 +01001585 prot ? "protect" : "unprotect");
Mario Sixd3525b62018-01-26 14:43:48 +01001586 if (retcode == 0) {
wdenk5653fc32004-02-08 22:55:38 +00001587 info->protect[sector] = prot;
Stefan Roese2662b402006-04-01 13:41:03 +02001588
1589 /*
1590 * On some of Intel's flash chips (marked via legacy_unlock)
1591 * unprotect unprotects all locking.
1592 */
Mario Six4f89da42018-01-26 14:43:42 +01001593 if (prot == 0 && info->legacy_unlock) {
wdenk5653fc32004-02-08 22:55:38 +00001594 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +00001595
1596 for (i = 0; i < info->sector_count; i++) {
1597 if (info->protect[i])
Mario Six188a5562018-01-26 14:43:31 +01001598 flash_real_protect(info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +00001599 }
1600 }
1601 }
wdenk5653fc32004-02-08 22:55:38 +00001602 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +00001603}
1604
wdenk5653fc32004-02-08 22:55:38 +00001605/*-----------------------------------------------------------------------
1606 * flash_read_user_serial - read the OneTimeProgramming cells
1607 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001608void flash_read_user_serial(flash_info_t *info, void *buffer, int offset,
Mario Sixc0350fb2018-01-26 14:43:55 +01001609 int len)
wdenk5653fc32004-02-08 22:55:38 +00001610{
wdenkbf9e3b32004-02-12 00:47:09 +00001611 uchar *src;
1612 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +00001613
1614 dst = buffer;
Mario Six188a5562018-01-26 14:43:31 +01001615 src = flash_map(info, 0, FLASH_OFFSET_USER_PROTECTION);
1616 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1617 memcpy(dst, src + offset, len);
1618 flash_write_cmd(info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001619 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001620 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001621}
wdenkbf9e3b32004-02-12 00:47:09 +00001622
wdenk5653fc32004-02-08 22:55:38 +00001623/*
1624 * flash_read_factory_serial - read the device Id from the protection area
1625 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001626void flash_read_factory_serial(flash_info_t *info, void *buffer, int offset,
Mario Sixc0350fb2018-01-26 14:43:55 +01001627 int len)
wdenk5653fc32004-02-08 22:55:38 +00001628{
wdenkbf9e3b32004-02-12 00:47:09 +00001629 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +00001630
Mario Six188a5562018-01-26 14:43:31 +01001631 src = flash_map(info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1632 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1633 memcpy(buffer, src + offset, len);
1634 flash_write_cmd(info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001635 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001636 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001637}
1638
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001639#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00001640
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001641/*-----------------------------------------------------------------------
1642 * Reverse the order of the erase regions in the CFI QRY structure.
1643 * This is needed for chips that are either a) correctly detected as
1644 * top-boot, or b) buggy.
1645 */
1646static void cfi_reverse_geometry(struct cfi_qry *qry)
1647{
1648 unsigned int i, j;
1649 u32 tmp;
1650
1651 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
Mario Six4f89da42018-01-26 14:43:42 +01001652 tmp = get_unaligned(&qry->erase_region_info[i]);
1653 put_unaligned(get_unaligned(&qry->erase_region_info[j]),
1654 &qry->erase_region_info[i]);
1655 put_unaligned(tmp, &qry->erase_region_info[j]);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001656 }
1657}
wdenk5653fc32004-02-08 22:55:38 +00001658
1659/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +01001660 * read jedec ids from device and set corresponding fields in info struct
1661 *
1662 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1663 *
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001664 */
1665static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1666{
1667 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001668 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001669 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1670 udelay(1000); /* some flash are slow to respond */
Mario Six188a5562018-01-26 14:43:31 +01001671 info->manufacturer_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001672 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyterd77c7ac2010-08-10 16:54:52 +02001673 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
Mario Six188a5562018-01-26 14:43:31 +01001674 flash_read_word(info, FLASH_OFFSET_DEVICE_ID) :
1675 flash_read_uchar(info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001676 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1677}
1678
1679static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1680{
1681 info->cmd_reset = FLASH_CMD_RESET;
1682
1683 cmdset_intel_read_jedec_ids(info);
1684 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1685
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001686#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001687 /* read legacy lock/unlock bit from intel flash */
1688 if (info->ext_addr) {
Mario Sixc0350fb2018-01-26 14:43:55 +01001689 info->legacy_unlock =
1690 flash_read_uchar(info, info->ext_addr + 5) & 0x08;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001691 }
1692#endif
1693
1694 return 0;
1695}
1696
1697static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1698{
Mario Sixc8a9a822018-01-26 14:43:51 +01001699 ushort bank_id = 0;
1700 uchar manu_id;
York Sun2544f472017-11-18 11:09:08 -08001701 uchar feature;
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001702
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001703 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1704 flash_unlock_seq(info, 0);
1705 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1706 udelay(1000); /* some flash are slow to respond */
Tor Krill90447ec2008-03-28 11:29:10 +01001707
Mario Sixc8a9a822018-01-26 14:43:51 +01001708 manu_id = flash_read_uchar(info, FLASH_OFFSET_MANUFACTURER_ID);
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001709 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
Mario Sixc8a9a822018-01-26 14:43:51 +01001710 while (manu_id == FLASH_CONTINUATION_CODE && bank_id < 0x800) {
1711 bank_id += 0x100;
1712 manu_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001713 bank_id | FLASH_OFFSET_MANUFACTURER_ID);
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001714 }
Mario Sixc8a9a822018-01-26 14:43:51 +01001715 info->manufacturer_id = manu_id;
Tor Krill90447ec2008-03-28 11:29:10 +01001716
York Sun2544f472017-11-18 11:09:08 -08001717 debug("info->ext_addr = 0x%x, cfi_version = 0x%x\n",
1718 info->ext_addr, info->cfi_version);
1719 if (info->ext_addr && info->cfi_version >= 0x3134) {
1720 /* read software feature (at 0x53) */
1721 feature = flash_read_uchar(info, info->ext_addr + 0x13);
1722 debug("feature = 0x%x\n", feature);
1723 info->sr_supported = feature & 0x1;
1724 }
Marek Vasut72443c72017-09-12 19:09:31 +02001725
Mario Sixb1683862018-01-26 14:43:33 +01001726 switch (info->chipwidth) {
Tor Krill90447ec2008-03-28 11:29:10 +01001727 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +01001728 info->device_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001729 FLASH_OFFSET_DEVICE_ID);
Tor Krill90447ec2008-03-28 11:29:10 +01001730 if (info->device_id == 0x7E) {
1731 /* AMD 3-byte (expanded) device ids */
Mario Six188a5562018-01-26 14:43:31 +01001732 info->device_id2 = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001733 FLASH_OFFSET_DEVICE_ID2);
Tor Krill90447ec2008-03-28 11:29:10 +01001734 info->device_id2 <<= 8;
Mario Six188a5562018-01-26 14:43:31 +01001735 info->device_id2 |= flash_read_uchar(info,
Tor Krill90447ec2008-03-28 11:29:10 +01001736 FLASH_OFFSET_DEVICE_ID3);
1737 }
1738 break;
1739 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +01001740 info->device_id = flash_read_word(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001741 FLASH_OFFSET_DEVICE_ID);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001742 if ((info->device_id & 0xff) == 0x7E) {
1743 /* AMD 3-byte (expanded) device ids */
Mario Six188a5562018-01-26 14:43:31 +01001744 info->device_id2 = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001745 FLASH_OFFSET_DEVICE_ID2);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001746 info->device_id2 <<= 8;
Mario Six188a5562018-01-26 14:43:31 +01001747 info->device_id2 |= flash_read_uchar(info,
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001748 FLASH_OFFSET_DEVICE_ID3);
1749 }
Tor Krill90447ec2008-03-28 11:29:10 +01001750 break;
1751 default:
1752 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001753 }
1754 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001755 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001756}
1757
1758static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1759{
1760 info->cmd_reset = AMD_CMD_RESET;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01001761 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001762
1763 cmdset_amd_read_jedec_ids(info);
1764 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1765
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001766#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseac6b9112012-12-06 15:44:11 +01001767 if (info->ext_addr) {
1768 /* read sector protect/unprotect scheme (at 0x49) */
1769 if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001770 info->legacy_unlock = 1;
1771 }
1772#endif
1773
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001774 return 0;
1775}
1776
1777#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Sixca2b07a2018-01-26 14:43:32 +01001778static void flash_read_jedec_ids(flash_info_t *info)
Stefan Roese260421a2006-11-13 13:55:24 +01001779{
1780 info->manufacturer_id = 0;
1781 info->device_id = 0;
1782 info->device_id2 = 0;
1783
1784 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001785 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese260421a2006-11-13 13:55:24 +01001786 case CFI_CMDSET_INTEL_STANDARD:
1787 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001788 cmdset_intel_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001789 break;
1790 case CFI_CMDSET_AMD_STANDARD:
1791 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001792 cmdset_amd_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001793 break;
1794 default:
1795 break;
1796 }
1797}
1798
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001799/*-----------------------------------------------------------------------
1800 * Call board code to request info about non-CFI flash.
1801 * board_flash_get_legacy needs to fill in at least:
1802 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1803 */
Becky Bruce09ce9922009-02-02 16:34:51 -06001804static int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001805{
1806 flash_info_t *info = &flash_info[banknum];
1807
1808 if (board_flash_get_legacy(base, banknum, info)) {
1809 /* board code may have filled info completely. If not, we
Mario Sixa6d18f22018-01-26 14:43:41 +01001810 * use JEDEC ID probing.
1811 */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001812 if (!info->vendor) {
1813 int modes[] = {
1814 CFI_CMDSET_AMD_STANDARD,
1815 CFI_CMDSET_INTEL_STANDARD
1816 };
1817 int i;
1818
Axel Lin31bf0f52013-06-23 00:56:46 +08001819 for (i = 0; i < ARRAY_SIZE(modes); i++) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001820 info->vendor = modes[i];
Becky Bruce09ce9922009-02-02 16:34:51 -06001821 info->start[0] =
1822 (ulong)map_physmem(base,
Stefan Roesee1fb6d02009-02-05 11:44:52 +01001823 info->portwidth,
Becky Bruce09ce9922009-02-02 16:34:51 -06001824 MAP_NOCACHE);
Mario Six88ecd8b2018-01-26 14:43:39 +01001825 if (info->portwidth == FLASH_CFI_8BIT &&
Mario Sixc0350fb2018-01-26 14:43:55 +01001826 info->interface == FLASH_CFI_X8X16) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001827 info->addr_unlock1 = 0x2AAA;
1828 info->addr_unlock2 = 0x5555;
1829 } else {
1830 info->addr_unlock1 = 0x5555;
1831 info->addr_unlock2 = 0x2AAA;
1832 }
1833 flash_read_jedec_ids(info);
1834 debug("JEDEC PROBE: ID %x %x %x\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001835 info->manufacturer_id,
1836 info->device_id,
1837 info->device_id2);
Becky Bruce09ce9922009-02-02 16:34:51 -06001838 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001839 break;
Mario Six98601372018-01-26 14:43:45 +01001840
1841 unmap_physmem((void *)info->start[0],
1842 info->portwidth);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001843 }
1844 }
1845
Mario Sixb1683862018-01-26 14:43:33 +01001846 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001847 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001848 case CFI_CMDSET_INTEL_STANDARD:
1849 case CFI_CMDSET_INTEL_EXTENDED:
1850 info->cmd_reset = FLASH_CMD_RESET;
1851 break;
1852 case CFI_CMDSET_AMD_STANDARD:
1853 case CFI_CMDSET_AMD_EXTENDED:
1854 case CFI_CMDSET_AMD_LEGACY:
1855 info->cmd_reset = AMD_CMD_RESET;
1856 break;
1857 }
1858 info->flash_id = FLASH_MAN_CFI;
1859 return 1;
1860 }
1861 return 0; /* use CFI */
1862}
1863#else
Becky Bruce09ce9922009-02-02 16:34:51 -06001864static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001865{
1866 return 0; /* use CFI */
1867}
1868#endif
1869
Stefan Roese260421a2006-11-13 13:55:24 +01001870/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001871 * detect if flash is compatible with the Common Flash Interface (CFI)
1872 * http://www.jedec.org/download/search/jesd68.pdf
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001873 */
Mario Sixc0350fb2018-01-26 14:43:55 +01001874static void flash_read_cfi(flash_info_t *info, void *buf, unsigned int start,
1875 size_t len)
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001876{
1877 u8 *p = buf;
1878 unsigned int i;
1879
1880 for (i = 0; i < len; i++)
Stefan Roesee303be22013-04-12 19:04:54 +02001881 p[i] = flash_read_uchar(info, start + i);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001882}
1883
Kim Phillips11dc4012012-10-29 13:34:45 +00001884static void __flash_cmd_reset(flash_info_t *info)
Stefan Roesefa36ae72009-10-27 15:15:55 +01001885{
1886 /*
1887 * We do not yet know what kind of commandset to use, so we issue
1888 * the reset command in both Intel and AMD variants, in the hope
1889 * that AMD flash roms ignore the Intel command.
1890 */
1891 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001892 udelay(1);
Stefan Roesefa36ae72009-10-27 15:15:55 +01001893 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1894}
Mario Six7223a8c2018-01-26 14:43:37 +01001895
Stefan Roesefa36ae72009-10-27 15:15:55 +01001896void flash_cmd_reset(flash_info_t *info)
Mario Six640f4e32018-01-26 14:43:36 +01001897 __attribute__((weak, alias("__flash_cmd_reset")));
Stefan Roesefa36ae72009-10-27 15:15:55 +01001898
Mario Sixca2b07a2018-01-26 14:43:32 +01001899static int __flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
wdenk5653fc32004-02-08 22:55:38 +00001900{
Wolfgang Denk92eb7292006-12-27 01:26:13 +01001901 int cfi_offset;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001902
Stefan Roesee303be22013-04-12 19:04:54 +02001903 /* Issue FLASH reset command */
1904 flash_cmd_reset(info);
1905
Axel Lin31bf0f52013-06-23 00:56:46 +08001906 for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001907 cfi_offset++) {
Mario Six188a5562018-01-26 14:43:31 +01001908 flash_write_cmd(info, 0, flash_offset_cfi[cfi_offset],
Mario Sixc0350fb2018-01-26 14:43:55 +01001909 FLASH_CMD_CFI);
Mario Six88ecd8b2018-01-26 14:43:39 +01001910 if (flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP, 'Q') &&
Mario Sixddcf0542018-01-26 14:43:54 +01001911 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
1912 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Mario Sixc0350fb2018-01-26 14:43:55 +01001913 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1914 sizeof(struct cfi_qry));
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001915 info->interface = le16_to_cpu(qry->interface_desc);
Stefan Roesee303be22013-04-12 19:04:54 +02001916
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001917 info->cfi_offset = flash_offset_cfi[cfi_offset];
Mario Six188a5562018-01-26 14:43:31 +01001918 debug("device interface is %d\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001919 info->interface);
Mario Six188a5562018-01-26 14:43:31 +01001920 debug("found port %d chip %d ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001921 info->portwidth, info->chipwidth);
Mario Six188a5562018-01-26 14:43:31 +01001922 debug("port %d bits chip %d bits\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001923 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1924 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001925
1926 /* calculate command offsets as in the Linux driver */
Stefan Roesee303be22013-04-12 19:04:54 +02001927 info->addr_unlock1 = 0x555;
1928 info->addr_unlock2 = 0x2aa;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001929
1930 /*
1931 * modify the unlock address if we are
1932 * in compatibility mode
1933 */
Mario Sixb1683862018-01-26 14:43:33 +01001934 if (/* x8/x16 in x8 mode */
Mario Six4f89da42018-01-26 14:43:42 +01001935 (info->chipwidth == FLASH_CFI_BY8 &&
1936 info->interface == FLASH_CFI_X8X16) ||
Mario Sixb1683862018-01-26 14:43:33 +01001937 /* x16/x32 in x16 mode */
Mario Six4f89da42018-01-26 14:43:42 +01001938 (info->chipwidth == FLASH_CFI_BY16 &&
Mario Six0cec0a12018-01-26 14:43:46 +01001939 info->interface == FLASH_CFI_X16X32)) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001940 info->addr_unlock1 = 0xaaa;
1941 info->addr_unlock2 = 0x555;
1942 }
1943
1944 info->name = "CFI conformant";
1945 return 1;
1946 }
1947 }
1948
1949 return 0;
1950}
1951
Mario Sixca2b07a2018-01-26 14:43:32 +01001952static int flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001953{
Mario Six188a5562018-01-26 14:43:31 +01001954 debug("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +00001955
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001956 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenkbf9e3b32004-02-12 00:47:09 +00001957 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1958 for (info->chipwidth = FLASH_CFI_BY8;
1959 info->chipwidth <= info->portwidth;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001960 info->chipwidth <<= 1)
Stefan Roesee303be22013-04-12 19:04:54 +02001961 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001962 return 1;
wdenk5653fc32004-02-08 22:55:38 +00001963 }
Mario Six188a5562018-01-26 14:43:31 +01001964 debug("not found\n");
wdenk5653fc32004-02-08 22:55:38 +00001965 return 0;
1966}
wdenkbf9e3b32004-02-12 00:47:09 +00001967
wdenk5653fc32004-02-08 22:55:38 +00001968/*
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001969 * Manufacturer-specific quirks. Add workarounds for geometry
1970 * reversal, etc. here.
1971 */
1972static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1973{
1974 /* check if flash geometry needs reversal */
1975 if (qry->num_erase_regions > 1) {
1976 /* reverse geometry if top boot part */
1977 if (info->cfi_version < 0x3131) {
1978 /* CFI < 1.1, try to guess from device id */
1979 if ((info->device_id & 0x80) != 0)
1980 cfi_reverse_geometry(qry);
Stefan Roesee303be22013-04-12 19:04:54 +02001981 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001982 /* CFI >= 1.1, deduct from top/bottom flag */
1983 /* note: ext_addr is valid since cfi_version > 0 */
1984 cfi_reverse_geometry(qry);
1985 }
1986 }
1987}
1988
1989static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1990{
1991 int reverse_geometry = 0;
1992
1993 /* Check the "top boot" bit in the PRI */
1994 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1995 reverse_geometry = 1;
1996
1997 /* AT49BV6416(T) list the erase regions in the wrong order.
1998 * However, the device ID is identical with the non-broken
Ulf Samuelssoncb82a532009-03-27 23:26:43 +01001999 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002000 */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002001 if (info->device_id == 0xd6 || info->device_id == 0xd2)
2002 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002003
2004 if (reverse_geometry)
2005 cfi_reverse_geometry(qry);
2006}
2007
Richard Retanubune8eac432009-01-14 08:44:26 -05002008static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
2009{
2010 /* check if flash geometry needs reversal */
2011 if (qry->num_erase_regions > 1) {
2012 /* reverse geometry if top boot part */
2013 if (info->cfi_version < 0x3131) {
Mike Frysinger6a011ce2011-04-10 16:06:29 -04002014 /* CFI < 1.1, guess by device id */
2015 if (info->device_id == 0x22CA || /* M29W320DT */
2016 info->device_id == 0x2256 || /* M29W320ET */
2017 info->device_id == 0x22D7) { /* M29W800DT */
Richard Retanubune8eac432009-01-14 08:44:26 -05002018 cfi_reverse_geometry(qry);
2019 }
Mike Frysinger4c2105c2011-05-09 18:33:36 -04002020 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
2021 /* CFI >= 1.1, deduct from top/bottom flag */
2022 /* note: ext_addr is valid since cfi_version > 0 */
2023 cfi_reverse_geometry(qry);
Richard Retanubune8eac432009-01-14 08:44:26 -05002024 }
2025 }
2026}
2027
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002028static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
2029{
2030 /*
2031 * SST, for many recent nor parallel flashes, says they are
2032 * CFI-conformant. This is not true, since qry struct.
2033 * reports a std. AMD command set (0x0002), while SST allows to
2034 * erase two different sector sizes for the same memory.
2035 * 64KB sector (SST call it block) needs 0x30 to be erased.
2036 * 4KB sector (SST call it sector) needs 0x50 to be erased.
2037 * Since CFI query detect the 4KB number of sectors, users expects
2038 * a sector granularity of 4KB, and it is here set.
2039 */
2040 if (info->device_id == 0x5D23 || /* SST39VF3201B */
2041 info->device_id == 0x5C23) { /* SST39VF3202B */
2042 /* set sector granularity to 4KB */
Mario Six640f4e32018-01-26 14:43:36 +01002043 info->cmd_erase_sector = 0x50;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002044 }
2045}
2046
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302047static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry)
2048{
2049 /*
2050 * The M29EW devices seem to report the CFI information wrong
2051 * when it's in 8 bit mode.
2052 * There's an app note from Numonyx on this issue.
2053 * So adjust the buffer size for M29EW while operating in 8-bit mode
2054 */
Mario Six4f89da42018-01-26 14:43:42 +01002055 if (qry->max_buf_write_size > 0x8 &&
Mario Sixc0350fb2018-01-26 14:43:55 +01002056 info->device_id == 0x7E &&
2057 (info->device_id2 == 0x2201 ||
2058 info->device_id2 == 0x2301 ||
2059 info->device_id2 == 0x2801 ||
2060 info->device_id2 == 0x4801)) {
Mario Six876c52f2018-01-26 14:43:50 +01002061 debug("Adjusted buffer size on Numonyx flash");
2062 debug(" M29EW family in 8 bit mode\n");
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302063 qry->max_buf_write_size = 0x8;
2064 }
2065}
2066
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002067/*
wdenk5653fc32004-02-08 22:55:38 +00002068 * The following code cannot be run from FLASH!
2069 *
2070 */
Mario Six188a5562018-01-26 14:43:31 +01002071ulong flash_get_size(phys_addr_t base, int banknum)
wdenk5653fc32004-02-08 22:55:38 +00002072{
wdenkbf9e3b32004-02-12 00:47:09 +00002073 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +00002074 int i, j;
2075 flash_sect_t sect_cnt;
Becky Bruce09ce9922009-02-02 16:34:51 -06002076 phys_addr_t sector;
wdenk5653fc32004-02-08 22:55:38 +00002077 unsigned long tmp;
2078 int size_ratio;
2079 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +00002080 int erase_region_size;
2081 int erase_region_count;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002082 struct cfi_qry qry;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002083 unsigned long max_size;
Stefan Roese260421a2006-11-13 13:55:24 +01002084
Kumar Galaf9796902008-05-15 15:13:08 -05002085 memset(&qry, 0, sizeof(qry));
2086
Stefan Roese260421a2006-11-13 13:55:24 +01002087 info->ext_addr = 0;
2088 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002089#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +02002090 info->legacy_unlock = 0;
2091#endif
wdenk5653fc32004-02-08 22:55:38 +00002092
Becky Bruce09ce9922009-02-02 16:34:51 -06002093 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002094
Mario Six188a5562018-01-26 14:43:31 +01002095 if (flash_detect_cfi(info, &qry)) {
Mario Six4f89da42018-01-26 14:43:42 +01002096 info->vendor = le16_to_cpu(get_unaligned(&qry.p_id));
2097 info->ext_addr = le16_to_cpu(get_unaligned(&qry.p_adr));
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002098 num_erase_regions = qry.num_erase_regions;
2099
Stefan Roese260421a2006-11-13 13:55:24 +01002100 if (info->ext_addr) {
Mario Six640f4e32018-01-26 14:43:36 +01002101 info->cfi_version = (ushort)flash_read_uchar(info,
Stefan Roesee303be22013-04-12 19:04:54 +02002102 info->ext_addr + 3) << 8;
Mario Six640f4e32018-01-26 14:43:36 +01002103 info->cfi_version |= (ushort)flash_read_uchar(info,
Stefan Roesee303be22013-04-12 19:04:54 +02002104 info->ext_addr + 4);
Stefan Roese260421a2006-11-13 13:55:24 +01002105 }
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002106
wdenkbf9e3b32004-02-12 00:47:09 +00002107#ifdef DEBUG
Mario Six188a5562018-01-26 14:43:31 +01002108 flash_printqry(&qry);
wdenkbf9e3b32004-02-12 00:47:09 +00002109#endif
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002110
wdenkbf9e3b32004-02-12 00:47:09 +00002111 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002112 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00002113 case CFI_CMDSET_INTEL_STANDARD:
2114 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002115 cmdset_intel_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002116 break;
2117 case CFI_CMDSET_AMD_STANDARD:
2118 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002119 cmdset_amd_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002120 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002121 default:
2122 printf("CFI: Unknown command set 0x%x\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002123 info->vendor);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002124 /*
2125 * Unfortunately, this means we don't know how
2126 * to get the chip back to Read mode. Might
2127 * as well try an Intel-style reset...
2128 */
2129 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2130 return 0;
wdenk5653fc32004-02-08 22:55:38 +00002131 }
wdenkcd37d9e2004-02-10 00:03:41 +00002132
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002133 /* Do manufacturer-specific fixups */
2134 switch (info->manufacturer_id) {
Mario Schuknecht2c9f48a2011-02-21 13:13:14 +01002135 case 0x0001: /* AMD */
2136 case 0x0037: /* AMIC */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002137 flash_fixup_amd(info, &qry);
2138 break;
2139 case 0x001f:
2140 flash_fixup_atmel(info, &qry);
2141 break;
Richard Retanubune8eac432009-01-14 08:44:26 -05002142 case 0x0020:
2143 flash_fixup_stm(info, &qry);
2144 break;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002145 case 0x00bf: /* SST */
2146 flash_fixup_sst(info, &qry);
2147 break;
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302148 case 0x0089: /* Numonyx */
2149 flash_fixup_num(info, &qry);
2150 break;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002151 }
2152
Mario Six188a5562018-01-26 14:43:31 +01002153 debug("manufacturer is %d\n", info->vendor);
2154 debug("manufacturer id is 0x%x\n", info->manufacturer_id);
2155 debug("device id is 0x%x\n", info->device_id);
2156 debug("device id2 is 0x%x\n", info->device_id2);
2157 debug("cfi version is 0x%04x\n", info->cfi_version);
Stefan Roese260421a2006-11-13 13:55:24 +01002158
wdenk5653fc32004-02-08 22:55:38 +00002159 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00002160 /* if the chip is x8/x16 reduce the ratio by half */
Mario Six4f89da42018-01-26 14:43:42 +01002161 if (info->interface == FLASH_CFI_X8X16 &&
Mario Sixc0350fb2018-01-26 14:43:55 +01002162 info->chipwidth == FLASH_CFI_BY8) {
wdenkbf9e3b32004-02-12 00:47:09 +00002163 size_ratio >>= 1;
2164 }
Mario Six188a5562018-01-26 14:43:31 +01002165 debug("size_ratio %d port %d bits chip %d bits\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002166 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2167 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002168 info->size = 1 << qry.dev_size;
2169 /* multiply the size by the number of chips */
2170 info->size *= size_ratio;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002171 max_size = cfi_flash_bank_size(banknum);
Mario Six4f89da42018-01-26 14:43:42 +01002172 if (max_size && info->size > max_size) {
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002173 debug("[truncated from %ldMiB]", info->size >> 20);
2174 info->size = max_size;
2175 }
Mario Six188a5562018-01-26 14:43:31 +01002176 debug("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00002177 sect_cnt = 0;
2178 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00002179 for (i = 0; i < num_erase_regions; i++) {
2180 if (i > NUM_ERASE_REGIONS) {
Mario Six188a5562018-01-26 14:43:31 +01002181 printf("%d erase regions found, only %d used\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002182 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00002183 break;
2184 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002185
Andrew Gabbasovaedadf12013-05-14 12:27:52 -05002186 tmp = le32_to_cpu(get_unaligned(
Mario Six4f89da42018-01-26 14:43:42 +01002187 &qry.erase_region_info[i]));
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002188 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002189
2190 erase_region_count = (tmp & 0xffff) + 1;
2191 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00002192 erase_region_size =
2193 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
Mario Sixddcf0542018-01-26 14:43:54 +01002194 debug("erase_region_count = %d ", erase_region_count);
2195 debug("erase_region_size = %d\n", erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00002196 for (j = 0; j < erase_region_count; j++) {
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002197 if (sector - base >= info->size)
2198 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002199 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002200 printf("ERROR: too many flash sectors\n");
2201 break;
2202 }
Becky Bruce09ce9922009-02-02 16:34:51 -06002203 info->start[sect_cnt] =
2204 (ulong)map_physmem(sector,
2205 info->portwidth,
2206 MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002207 sector += (erase_region_size * size_ratio);
wdenka1191902005-01-09 17:12:27 +00002208
2209 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002210 * Only read protection status from
2211 * supported devices (intel...)
wdenka1191902005-01-09 17:12:27 +00002212 */
2213 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002214 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenka1191902005-01-09 17:12:27 +00002215 case CFI_CMDSET_INTEL_EXTENDED:
2216 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesedf4e8132010-10-25 18:31:29 +02002217 /*
2218 * Set flash to read-id mode. Otherwise
2219 * reading protected status is not
2220 * guaranteed.
2221 */
2222 flash_write_cmd(info, sect_cnt, 0,
2223 FLASH_CMD_READ_ID);
wdenka1191902005-01-09 17:12:27 +00002224 info->protect[sect_cnt] =
Mario Six188a5562018-01-26 14:43:31 +01002225 flash_isset(info, sect_cnt,
Mario Sixc0350fb2018-01-26 14:43:55 +01002226 FLASH_OFFSET_PROTECT,
2227 FLASH_STATUS_PROTECT);
Vasily Khoruzhickedc498c2016-03-20 18:37:10 -07002228 flash_write_cmd(info, sect_cnt, 0,
2229 FLASH_CMD_RESET);
wdenka1191902005-01-09 17:12:27 +00002230 break;
Stefan Roese03deff42012-12-06 15:44:10 +01002231 case CFI_CMDSET_AMD_EXTENDED:
2232 case CFI_CMDSET_AMD_STANDARD:
Stefan Roeseac6b9112012-12-06 15:44:11 +01002233 if (!info->legacy_unlock) {
Stefan Roese03deff42012-12-06 15:44:10 +01002234 /* default: not protected */
2235 info->protect[sect_cnt] = 0;
2236 break;
2237 }
2238
2239 /* Read protection (PPB) from sector */
2240 flash_write_cmd(info, 0, 0,
2241 info->cmd_reset);
2242 flash_unlock_seq(info, 0);
2243 flash_write_cmd(info, 0,
2244 info->addr_unlock1,
2245 FLASH_CMD_READ_ID);
2246 info->protect[sect_cnt] =
2247 flash_isset(
2248 info, sect_cnt,
2249 FLASH_OFFSET_PROTECT,
2250 FLASH_STATUS_PROTECT);
2251 break;
wdenka1191902005-01-09 17:12:27 +00002252 default:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002253 /* default: not protected */
2254 info->protect[sect_cnt] = 0;
wdenka1191902005-01-09 17:12:27 +00002255 }
2256
wdenk5653fc32004-02-08 22:55:38 +00002257 sect_cnt++;
2258 }
2259 }
2260
2261 info->sector_count = sect_cnt;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002262 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2263 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002264 info->erase_blk_tout = tmp *
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002265 (1 << qry.block_erase_timeout_max);
2266 tmp = (1 << qry.buf_write_timeout_typ) *
2267 (1 << qry.buf_write_timeout_max);
2268
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002269 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002270 info->buffer_write_tout = (tmp + 999) / 1000;
2271 tmp = (1 << qry.word_write_timeout_typ) *
2272 (1 << qry.word_write_timeout_max);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002273 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002274 info->write_tout = (tmp + 999) / 1000;
wdenk5653fc32004-02-08 22:55:38 +00002275 info->flash_id = FLASH_MAN_CFI;
Mario Six4f89da42018-01-26 14:43:42 +01002276 if (info->interface == FLASH_CFI_X8X16 &&
2277 info->chipwidth == FLASH_CFI_BY8) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002278 /* XXX - Need to test on x8/x16 in parallel. */
2279 info->portwidth >>= 1;
wdenk855a4962004-03-14 18:23:55 +00002280 }
Mike Frysinger22159872008-10-02 01:55:38 -04002281
Mario Six188a5562018-01-26 14:43:31 +01002282 flash_write_cmd(info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +00002283 }
2284
wdenkbf9e3b32004-02-12 00:47:09 +00002285 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00002286}
2287
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002288#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002289void flash_set_verbose(uint v)
2290{
2291 flash_verbose = v;
2292}
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002293#endif
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002294
Stefan Roese6f726f92010-10-25 18:31:48 +02002295static void cfi_flash_set_config_reg(u32 base, u16 val)
2296{
2297#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2298 /*
2299 * Only set this config register if really defined
2300 * to a valid value (0xffff is invalid)
2301 */
2302 if (val == 0xffff)
2303 return;
2304
2305 /*
2306 * Set configuration register. Data is "encrypted" in the 16 lower
2307 * address bits.
2308 */
2309 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2310 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2311
2312 /*
2313 * Finally issue reset-command to bring device back to
2314 * read-array mode
2315 */
2316 flash_write16(FLASH_CMD_RESET, (void *)base);
2317#endif
2318}
2319
wdenk5653fc32004-02-08 22:55:38 +00002320/*-----------------------------------------------------------------------
2321 */
Heiko Schocher6ee14162011-04-04 08:10:21 +02002322
Marek Vasut236c49a2017-08-20 17:20:00 +02002323static void flash_protect_default(void)
Heiko Schocher6ee14162011-04-04 08:10:21 +02002324{
Peter Tyser2c519832011-04-13 11:46:56 -05002325#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2326 int i;
2327 struct apl_s {
2328 ulong start;
2329 ulong size;
2330 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2331#endif
2332
Heiko Schocher6ee14162011-04-04 08:10:21 +02002333 /* Monitor protection ON by default */
Vignesh Raghavendrad75eacf2019-10-23 13:30:00 +05302334#if defined(CONFIG_SYS_MONITOR_BASE) && \
2335 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
Heiko Schocher6ee14162011-04-04 08:10:21 +02002336 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2337 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002338 CONFIG_SYS_MONITOR_BASE,
2339 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2340 flash_get_info(CONFIG_SYS_MONITOR_BASE));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002341#endif
2342
2343 /* Environment protection ON by default */
2344#ifdef CONFIG_ENV_IS_IN_FLASH
2345 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002346 CONFIG_ENV_ADDR,
2347 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2348 flash_get_info(CONFIG_ENV_ADDR));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002349#endif
2350
2351 /* Redundant environment protection ON by default */
2352#ifdef CONFIG_ENV_ADDR_REDUND
2353 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002354 CONFIG_ENV_ADDR_REDUND,
2355 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2356 flash_get_info(CONFIG_ENV_ADDR_REDUND));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002357#endif
2358
2359#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
Axel Lin31bf0f52013-06-23 00:56:46 +08002360 for (i = 0; i < ARRAY_SIZE(apl); i++) {
Marek Vasut31d34142011-10-21 14:17:05 +00002361 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocher6ee14162011-04-04 08:10:21 +02002362 apl[i].start, apl[i].start + apl[i].size - 1);
2363 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002364 apl[i].start,
2365 apl[i].start + apl[i].size - 1,
2366 flash_get_info(apl[i].start));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002367 }
2368#endif
2369}
2370
Mario Six188a5562018-01-26 14:43:31 +01002371unsigned long flash_init(void)
wdenk5653fc32004-02-08 22:55:38 +00002372{
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002373 unsigned long size = 0;
2374 int i;
wdenk5653fc32004-02-08 22:55:38 +00002375
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002376#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann3a3baf32009-03-21 09:59:34 -04002377 /* read environment from EEPROM */
2378 char s[64];
Mario Six7223a8c2018-01-26 14:43:37 +01002379
Simon Glass00caae62017-08-03 12:22:12 -06002380 env_get_f("unlock", s, sizeof(s));
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002381#endif
wdenk5653fc32004-02-08 22:55:38 +00002382
Thomas Chouf1056912015-11-07 14:31:08 +08002383#ifdef CONFIG_CFI_FLASH /* for driver model */
2384 cfi_flash_init_dm();
2385#endif
2386
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002387 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002388 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002389 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk5653fc32004-02-08 22:55:38 +00002390
Stefan Roese6f726f92010-10-25 18:31:48 +02002391 /* Optionally write flash configuration register */
2392 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2393 cfi_flash_config_reg(i));
2394
Stefan Roeseb00e19c2010-08-30 10:11:51 +02002395 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002396 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002397 size += flash_info[i].size;
2398 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002399#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Mario Six876c52f2018-01-26 14:43:50 +01002400 printf("## Unknown flash on Bank %d ", i + 1);
2401 printf("- Size = 0x%08lx = %ld MB\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002402 flash_info[i].size,
2403 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002404#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
wdenk5653fc32004-02-08 22:55:38 +00002405 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002406#ifdef CONFIG_SYS_FLASH_PROTECTION
Jeroen Hofsteec15df212014-06-17 22:47:31 +02002407 else if (strcmp(s, "yes") == 0) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002408 /*
2409 * Only the U-Boot image and it's environment
2410 * is protected, all other sectors are
2411 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002412 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002413 * and the environment variable "unlock" is
2414 * set to "yes".
2415 */
2416 if (flash_info[i].legacy_unlock) {
2417 int k;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002418
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002419 /*
2420 * Disable legacy_unlock temporarily,
2421 * since flash_real_protect would
2422 * relock all other sectors again
2423 * otherwise.
2424 */
2425 flash_info[i].legacy_unlock = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002426
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002427 /*
2428 * Legacy unlocking (e.g. Intel J3) ->
2429 * unlock only one sector. This will
2430 * unlock all sectors.
2431 */
Mario Six188a5562018-01-26 14:43:31 +01002432 flash_real_protect(&flash_info[i], 0, 0);
Stefan Roese79b4cda2006-02-28 15:29:58 +01002433
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002434 flash_info[i].legacy_unlock = 1;
2435
2436 /*
2437 * Manually mark other sectors as
2438 * unlocked (unprotected)
2439 */
2440 for (k = 1; k < flash_info[i].sector_count; k++)
2441 flash_info[i].protect[k] = 0;
2442 } else {
2443 /*
2444 * No legancy unlocking -> unlock all sectors
2445 */
Mario Six188a5562018-01-26 14:43:31 +01002446 flash_protect(FLAG_PROTECT_CLEAR,
Mario Sixc0350fb2018-01-26 14:43:55 +01002447 flash_info[i].start[0],
2448 flash_info[i].start[0]
2449 + flash_info[i].size - 1,
2450 &flash_info[i]);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002451 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01002452 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002453#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00002454 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002455
Heiko Schocher6ee14162011-04-04 08:10:21 +02002456 flash_protect_default();
Piotr Ziecik91809ed2008-11-17 15:57:58 +01002457#ifdef CONFIG_FLASH_CFI_MTD
2458 cfi_mtd_init();
2459#endif
2460
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002461 return (size);
wdenk5653fc32004-02-08 22:55:38 +00002462}
Thomas Chouf1056912015-11-07 14:31:08 +08002463
2464#ifdef CONFIG_CFI_FLASH /* for driver model */
2465static int cfi_flash_probe(struct udevice *dev)
2466{
Thomas Chouf1056912015-11-07 14:31:08 +08002467 const fdt32_t *cell;
Mario Six8bfeb332018-03-28 14:38:41 +02002468 int addrc, sizec;
Thomas Chouf1056912015-11-07 14:31:08 +08002469 int len, idx;
2470
Mario Six8bfeb332018-03-28 14:38:41 +02002471 addrc = dev_read_addr_cells(dev);
2472 sizec = dev_read_size_cells(dev);
2473
2474 /* decode regs; there may be multiple reg tuples. */
2475 cell = dev_read_prop(dev, "reg", &len);
Thomas Chouf1056912015-11-07 14:31:08 +08002476 if (!cell)
2477 return -ENOENT;
2478 idx = 0;
2479 len /= sizeof(fdt32_t);
2480 while (idx < len) {
Mario Six8bfeb332018-03-28 14:38:41 +02002481 phys_addr_t addr;
2482
2483 addr = dev_translate_address(dev, cell + idx);
2484
Marek Vasut1ec0a372017-09-12 19:09:08 +02002485 flash_info[cfi_flash_num_flash_banks].dev = dev;
2486 flash_info[cfi_flash_num_flash_banks].base = addr;
2487 cfi_flash_num_flash_banks++;
Mario Six8bfeb332018-03-28 14:38:41 +02002488
Thomas Chouf1056912015-11-07 14:31:08 +08002489 idx += addrc + sizec;
2490 }
Marek Vasut1ec0a372017-09-12 19:09:08 +02002491 gd->bd->bi_flashstart = flash_info[0].base;
Thomas Chouf1056912015-11-07 14:31:08 +08002492
2493 return 0;
2494}
2495
2496static const struct udevice_id cfi_flash_ids[] = {
2497 { .compatible = "cfi-flash" },
2498 { .compatible = "jedec-flash" },
2499 {}
2500};
2501
2502U_BOOT_DRIVER(cfi_flash) = {
2503 .name = "cfi_flash",
2504 .id = UCLASS_MTD,
2505 .of_match = cfi_flash_ids,
2506 .probe = cfi_flash_probe,
2507};
2508#endif /* CONFIG_CFI_FLASH */