blob: 4ce183b6f313fcdf4898a070e04b5fa6f091f954 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk2cefd152004-02-08 22:55:38 +00002/*
wdenke65527f2004-02-12 00:47:09 +00003 * (C) Copyright 2002-2004
wdenk2cefd152004-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>
wdenk2cefd152004-02-08 22:55:38 +00008 *
wdenke65527f2004-02-12 00:47:09 +00009 * Copyright (C) 2004
10 * Ed Okerson
Stefan Roese12797482006-11-13 13:55:24 +010011 *
12 * Copyright (C) 2006
13 * Tolunay Orkun <listmember@orkun.us>
wdenk2cefd152004-02-08 22:55:38 +000014 */
15
16/* The DEBUG define must be before common to enable debugging */
wdenk2ebee312004-02-23 19:30:57 +000017/* #define DEBUG */
18
wdenk2cefd152004-02-08 22:55:38 +000019#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070020#include <console.h>
Thomas Chou47eae232015-11-07 14:31:08 +080021#include <dm.h>
Simon Glassdb229612019-08-01 09:46:42 -060022#include <env.h>
Thomas Chou47eae232015-11-07 14:31:08 +080023#include <errno.h>
24#include <fdt_support.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070025#include <irq_func.h>
wdenk2cefd152004-02-08 22:55:38 +000026#include <asm/processor.h>
Haiying Wangc123a382007-02-21 16:52:31 +010027#include <asm/io.h>
wdenkaeba06f2004-06-09 17:34:58 +000028#include <asm/byteorder.h>
Andrew Gabbasovc1592582013-05-14 12:27:52 -050029#include <asm/unaligned.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060030#include <env_internal.h>
Stefan Roese6e83e342009-10-27 15:15:55 +010031#include <mtd/cfi_flash.h>
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +010032#include <watchdog.h>
wdenke537b3b2004-02-23 23:54:43 +000033
wdenk2cefd152004-02-08 22:55:38 +000034/*
Haavard Skinnemoend523e392007-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.
wdenk2cefd152004-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 Roese12797482006-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
wdenk2cefd152004-02-08 22:55:38 +000050 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020051 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocher800db312007-01-19 18:05:26 +010052 * reading and writing ... (yes there is such a Hardware).
wdenk2cefd152004-02-08 22:55:38 +000053 */
54
Thomas Chou47eae232015-11-07 14:31:08 +080055DECLARE_GLOBAL_DATA_PTR;
56
Haavard Skinnemoend523e392007-12-13 12:56:28 +010057static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysingerc2c093d2010-12-22 09:41:13 -050058#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +010059static uint flash_verbose = 1;
Mike Frysingerc2c093d2010-12-22 09:41:13 -050060#else
61#define flash_verbose 1
62#endif
Wolfgang Denkafa0dd02006-12-27 01:26:13 +010063
Wolfgang Denk9f5fb0f2008-08-08 16:39:54 +020064flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
65
Stefan Roesec865e6c2006-02-28 15:29:58 +010066/*
67 * Check if chip width is defined. If not, start detecting with 8bit.
68 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020069#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
70#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roesec865e6c2006-02-28 15:29:58 +010071#endif
72
Jeroen Hofstee4f517e62014-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 Roeseab935642010-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 Roesefb9a7302010-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 Six28f18982018-01-26 14:43:56 +010095#else
96int cfi_flash_num_flash_banks;
Stefan Roesefb9a7302010-08-31 10:00:10 +020097#endif
98
Thomas Chou47eae232015-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 Chou47eae232015-11-07 14:31:08 +0800117phys_addr_t cfi_flash_bank_addr(int i)
118{
Marek Vasut970940f2017-09-12 19:09:08 +0200119 return flash_info[i].base;
Thomas Chou47eae232015-11-07 14:31:08 +0800120}
121#else
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200122__weak phys_addr_t cfi_flash_bank_addr(int i)
Stefan Roese7e7dda82010-08-30 10:11:51 +0200123{
124 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
125}
Thomas Chou47eae232015-11-07 14:31:08 +0800126#endif
Stefan Roese7e7dda82010-08-30 10:11:51 +0200127
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200128__weak unsigned long cfi_flash_bank_size(int i)
Ilya Yanok755c1802010-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 Yanok755c1802010-10-21 17:20:12 +0200136
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200137__maybe_weak void flash_write8(u8 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100138{
139 __raw_writeb(value, addr);
140}
141
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200142__maybe_weak void flash_write16(u16 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100143{
144 __raw_writew(value, addr);
145}
146
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200147__maybe_weak void flash_write32(u32 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100148{
149 __raw_writel(value, addr);
150}
151
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200152__maybe_weak void flash_write64(u64 value, void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100153{
154 /* No architectures currently implement __raw_writeq() */
155 *(volatile u64 *)addr = value;
156}
157
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200158__maybe_weak u8 flash_read8(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100159{
160 return __raw_readb(addr);
161}
162
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200163__maybe_weak u16 flash_read16(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100164{
165 return __raw_readw(addr);
166}
167
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200168__maybe_weak u32 flash_read32(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100169{
170 return __raw_readl(addr);
171}
172
Jeroen Hofstee4f517e62014-10-08 22:57:23 +0200173__maybe_weak u64 flash_read64(void *addr)
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100174{
175 /* No architectures currently implement __raw_readq() */
176 return *(volatile u64 *)addr;
177}
178
wdenk2cefd152004-02-08 22:55:38 +0000179/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000180 */
Mario Sixbc762c12018-01-26 14:43:54 +0100181#if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || \
Vignesh Raghavendra5b9d8002019-10-23 13:30:00 +0530182 (defined(CONFIG_SYS_MONITOR_BASE) && \
183 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE))
Marek Vasuta26162d2017-08-20 17:20:00 +0200184static flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200185{
186 int i;
Masahiro Yamadac663b9a2013-05-17 14:50:37 +0900187 flash_info_t *info;
wdenk2cefd152004-02-08 22:55:38 +0000188
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200189 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Masahiro Yamada44049f32013-05-17 14:50:36 +0900190 info = &flash_info[i];
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200191 if (info->size && info->start[0] <= base &&
192 base <= info->start[0] + info->size - 1)
Masahiro Yamadac663b9a2013-05-17 14:50:37 +0900193 return info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200194 }
wdenk2cefd152004-02-08 22:55:38 +0000195
Masahiro Yamadac663b9a2013-05-17 14:50:37 +0900196 return NULL;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200197}
wdenk2cefd152004-02-08 22:55:38 +0000198#endif
199
Haavard Skinnemoene3ea1882007-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
wdenke65527f2004-02-12 00:47:09 +0000208/*-----------------------------------------------------------------------
209 * create an address based on the offset and the port width
210 */
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100211static inline void *
Mario Six88f439f2018-01-26 14:43:32 +0100212flash_map(flash_info_t *info, flash_sect_t sect, uint offset)
wdenke65527f2004-02-12 00:47:09 +0000213{
Stefan Roese70a90b72013-04-12 19:04:54 +0200214 unsigned int byte_offset = offset * info->portwidth;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100215
Stefan Roese70a90b72013-04-12 19:04:54 +0200216 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100217}
218
219static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
Mario Sixdde85502018-01-26 14:43:55 +0100220 unsigned int offset, void *addr)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100221{
wdenke65527f2004-02-12 00:47:09 +0000222}
223
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200224/*-----------------------------------------------------------------------
225 * make a proper sized command based on the port and chip widths
226 */
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200227static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200228{
229 int i;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400230 int cword_offset;
231 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200232#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewiord528cd62008-07-16 20:04:49 +0200233 u32 cmd_le = cpu_to_le32(cmd);
234#endif
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400235 uchar val;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200236 uchar *cp = (uchar *) cmdbuf;
237
Mario Sixe2c07462018-01-26 14:43:33 +0100238 for (i = info->portwidth; i > 0; i--) {
Mario Sixa828c1e2018-01-26 14:43:36 +0100239 cword_offset = (info->portwidth - i) % info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200240#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400241 cp_offset = info->portwidth - i;
Mario Sixae0b9c72018-01-26 14:43:34 +0100242 val = *((uchar *)&cmd_le + cword_offset);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200243#else
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400244 cp_offset = i - 1;
Mario Sixae0b9c72018-01-26 14:43:34 +0100245 val = *((uchar *)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200246#endif
Sebastian Siewior7746ed82008-07-15 13:35:23 +0200247 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenkoc47c0d42008-05-07 21:24:44 +0400248 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200249}
250
wdenk2cefd152004-02-08 22:55:38 +0000251#ifdef DEBUG
wdenke65527f2004-02-12 00:47:09 +0000252/*-----------------------------------------------------------------------
253 * Debug support
254 */
Mario Sixfa290692018-01-26 14:43:31 +0100255static void print_longlong(char *str, unsigned long long data)
wdenk2cefd152004-02-08 22:55:38 +0000256{
257 int i;
258 char *cp;
wdenke65527f2004-02-12 00:47:09 +0000259
Mario Sixa828c1e2018-01-26 14:43:36 +0100260 cp = (char *)&data;
wdenke65527f2004-02-12 00:47:09 +0000261 for (i = 0; i < 8; i++)
Mario Sixfa290692018-01-26 14:43:31 +0100262 sprintf(&str[i * 2], "%2.2x", *cp++);
wdenk2cefd152004-02-08 22:55:38 +0000263}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200264
Mario Sixfa290692018-01-26 14:43:31 +0100265static void flash_printqry(struct cfi_qry *qry)
wdenke65527f2004-02-12 00:47:09 +0000266{
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100267 u8 *p = (u8 *)qry;
wdenke65527f2004-02-12 00:47:09 +0000268 int x, y;
269
Haavard Skinnemoen53baec72007-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(" ");
wdenke65527f2004-02-12 00:47:09 +0000275 for (y = 0; y < 16; y++) {
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100276 unsigned char c = p[x + y];
Mario Sixc7e359e2018-01-26 14:43:37 +0100277
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100278 if (c >= 0x20 && c <= 0x7e)
279 debug("%c", c);
280 else
281 debug(".");
wdenke65527f2004-02-12 00:47:09 +0000282 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +0100283 debug("\n");
wdenke65527f2004-02-12 00:47:09 +0000284 }
285}
wdenk2cefd152004-02-08 22:55:38 +0000286#endif
287
wdenk2cefd152004-02-08 22:55:38 +0000288/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000289 * read a character at a port width address
290 */
Mario Six88f439f2018-01-26 14:43:32 +0100291static inline uchar flash_read_uchar(flash_info_t *info, uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000292{
293 uchar *cp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100294 uchar retval;
wdenke65527f2004-02-12 00:47:09 +0000295
Mario Sixfa290692018-01-26 14:43:31 +0100296 cp = flash_map(info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200297#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100298 retval = flash_read8(cp);
wdenke65527f2004-02-12 00:47:09 +0000299#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100300 retval = flash_read8(cp + info->portwidth - 1);
wdenke65527f2004-02-12 00:47:09 +0000301#endif
Mario Sixfa290692018-01-26 14:43:31 +0100302 flash_unmap(info, 0, offset, cp);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100303 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000304}
305
306/*-----------------------------------------------------------------------
Tor Krill7f2a3052008-03-28 11:29:10 +0100307 * read a word at a port width address, assume 16bit bus
308 */
Mario Six88f439f2018-01-26 14:43:32 +0100309static inline ushort flash_read_word(flash_info_t *info, uint offset)
Tor Krill7f2a3052008-03-28 11:29:10 +0100310{
311 ushort *addr, retval;
312
Mario Sixfa290692018-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 Krill7f2a3052008-03-28 11:29:10 +0100316 return retval;
317}
318
Tor Krill7f2a3052008-03-28 11:29:10 +0100319/*-----------------------------------------------------------------------
Stefan Roese12797482006-11-13 13:55:24 +0100320 * read a long word by picking the least significant byte of each maximum
wdenk2cefd152004-02-08 22:55:38 +0000321 * port size word. Swap for ppc format.
322 */
Mario Six88f439f2018-01-26 14:43:32 +0100323static ulong flash_read_long (flash_info_t *info, flash_sect_t sect,
Haavard Skinnemoen670a3232007-12-13 12:56:29 +0100324 uint offset)
wdenk2cefd152004-02-08 22:55:38 +0000325{
wdenke65527f2004-02-12 00:47:09 +0000326 uchar *addr;
327 ulong retval;
328
329#ifdef DEBUG
330 int x;
331#endif
Mario Sixfa290692018-01-26 14:43:31 +0100332 addr = flash_map(info, sect, offset);
wdenk2cefd152004-02-08 22:55:38 +0000333
wdenke65527f2004-02-12 00:47:09 +0000334#ifdef DEBUG
Mario Sixfa290692018-01-26 14:43:31 +0100335 debug("long addr is at %p info->portwidth = %d\n", addr,
Mario Sixdde85502018-01-26 14:43:55 +0100336 info->portwidth);
Mario Sixcbe41ca2018-01-26 14:43:38 +0100337 for (x = 0; x < 4 * info->portwidth; x++)
Mario Sixfa290692018-01-26 14:43:31 +0100338 debug("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenke65527f2004-02-12 00:47:09 +0000339#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200340#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100341 retval = ((flash_read8(addr) << 16) |
342 (flash_read8(addr + info->portwidth) << 24) |
343 (flash_read8(addr + 2 * info->portwidth)) |
344 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenke65527f2004-02-12 00:47:09 +0000345#else
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100346 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
347 (flash_read8(addr + info->portwidth - 1) << 16) |
348 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
349 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenke65527f2004-02-12 00:47:09 +0000350#endif
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100351 flash_unmap(info, sect, offset, addr);
352
wdenke65527f2004-02-12 00:47:09 +0000353 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000354}
355
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200356/*
357 * Write a proper sized command to the correct address
Michael Schwingen73d044d2007-12-07 23:35:02 +0100358 */
Marek Vasuta26162d2017-08-20 17:20:00 +0200359static void flash_write_cmd(flash_info_t *info, flash_sect_t sect,
360 uint offset, u32 cmd)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100361{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100362 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200363 cfiword_t cword;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100364
Mario Sixfa290692018-01-26 14:43:31 +0100365 addr = flash_map(info, sect, offset);
366 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200367 switch (info->portwidth) {
368 case FLASH_CFI_8BIT:
Mario Sixfa290692018-01-26 14:43:31 +0100369 debug("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Mario Sixdde85502018-01-26 14:43:55 +0100370 cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin316870c2015-10-23 16:50:51 +0100371 flash_write8(cword.w8, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200372 break;
373 case FLASH_CFI_16BIT:
Mario Sixfa290692018-01-26 14:43:31 +0100374 debug("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Mario Sixdde85502018-01-26 14:43:55 +0100375 cmd, cword.w16,
376 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin316870c2015-10-23 16:50:51 +0100377 flash_write16(cword.w16, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200378 break;
379 case FLASH_CFI_32BIT:
Mario Sixfa290692018-01-26 14:43:31 +0100380 debug("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr,
Mario Sixdde85502018-01-26 14:43:55 +0100381 cmd, cword.w32,
382 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin316870c2015-10-23 16:50:51 +0100383 flash_write32(cword.w32, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200384 break;
385 case FLASH_CFI_64BIT:
386#ifdef DEBUG
387 {
388 char str[20];
Haavard Skinnemoend523e392007-12-13 12:56:28 +0100389
Mario Sixfa290692018-01-26 14:43:31 +0100390 print_longlong(str, cword.w64);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200391
Mario Sixfa290692018-01-26 14:43:31 +0100392 debug("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Mario Sixdde85502018-01-26 14:43:55 +0100393 addr, cmd, str,
394 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100395 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200396#endif
Ryan Harkin316870c2015-10-23 16:50:51 +0100397 flash_write64(cword.w64, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200398 break;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100399 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200400
401 /* Ensure all the instructions are fully finished */
402 sync();
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100403
404 flash_unmap(info, sect, offset, addr);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100405}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200406
Mario Six88f439f2018-01-26 14:43:32 +0100407static void flash_unlock_seq(flash_info_t *info, flash_sect_t sect)
Michael Schwingen73d044d2007-12-07 23:35:02 +0100408{
Mario Sixfa290692018-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);
Michael Schwingen73d044d2007-12-07 23:35:02 +0100411}
Michael Schwingen73d044d2007-12-07 23:35:02 +0100412
413/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000414 */
Mario Sixdde85502018-01-26 14:43:55 +0100415static int flash_isequal(flash_info_t *info, flash_sect_t sect, uint offset,
416 uchar cmd)
wdenk2cefd152004-02-08 22:55:38 +0000417{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100418 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200419 cfiword_t cword;
420 int retval;
wdenk2cefd152004-02-08 22:55:38 +0000421
Mario Sixfa290692018-01-26 14:43:31 +0100422 addr = flash_map(info, sect, offset);
423 flash_make_cmd(info, cmd, &cword);
Stefan Roeseefef95b2006-04-01 13:41:03 +0200424
Mario Sixfa290692018-01-26 14:43:31 +0100425 debug("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200426 switch (info->portwidth) {
427 case FLASH_CFI_8BIT:
Mario Sixfa290692018-01-26 14:43:31 +0100428 debug("is= %x %x\n", flash_read8(addr), cword.w8);
Ryan Harkin316870c2015-10-23 16:50:51 +0100429 retval = (flash_read8(addr) == cword.w8);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200430 break;
431 case FLASH_CFI_16BIT:
Mario Sixfa290692018-01-26 14:43:31 +0100432 debug("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16);
Ryan Harkin316870c2015-10-23 16:50:51 +0100433 retval = (flash_read16(addr) == cword.w16);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200434 break;
435 case FLASH_CFI_32BIT:
Mario Sixfa290692018-01-26 14:43:31 +0100436 debug("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32);
Ryan Harkin316870c2015-10-23 16:50:51 +0100437 retval = (flash_read32(addr) == cword.w32);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200438 break;
439 case FLASH_CFI_64BIT:
440#ifdef DEBUG
441 {
442 char str1[20];
443 char str2[20];
Michael Schwingen73d044d2007-12-07 23:35:02 +0100444
Mario Sixfa290692018-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);
wdenk2cefd152004-02-08 22:55:38 +0000448 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200449#endif
Ryan Harkin316870c2015-10-23 16:50:51 +0100450 retval = (flash_read64(addr) == cword.w64);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200451 break;
452 default:
453 retval = 0;
454 break;
455 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100456 flash_unmap(info, sect, offset, addr);
457
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200458 return retval;
459}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200460
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200461/*-----------------------------------------------------------------------
462 */
Mario Sixdde85502018-01-26 14:43:55 +0100463static int flash_isset(flash_info_t *info, flash_sect_t sect, uint offset,
464 uchar cmd)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200465{
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100466 void *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200467 cfiword_t cword;
468 int retval;
Stefan Roeseefef95b2006-04-01 13:41:03 +0200469
Mario Sixfa290692018-01-26 14:43:31 +0100470 addr = flash_map(info, sect, offset);
471 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200472 switch (info->portwidth) {
473 case FLASH_CFI_8BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100474 retval = ((flash_read8(addr) & cword.w8) == cword.w8);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200475 break;
476 case FLASH_CFI_16BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100477 retval = ((flash_read16(addr) & cword.w16) == cword.w16);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200478 break;
479 case FLASH_CFI_32BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100480 retval = ((flash_read32(addr) & cword.w32) == cword.w32);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200481 break;
482 case FLASH_CFI_64BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100483 retval = ((flash_read64(addr) & cword.w64) == cword.w64);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200484 break;
485 default:
486 retval = 0;
487 break;
488 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100489 flash_unmap(info, sect, offset, addr);
490
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200491 return retval;
492}
Stefan Roeseefef95b2006-04-01 13:41:03 +0200493
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200494/*-----------------------------------------------------------------------
495 */
Mario Sixdde85502018-01-26 14:43:55 +0100496static int flash_toggle(flash_info_t *info, flash_sect_t sect, uint offset,
497 uchar cmd)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200498{
Mario Sixf4bab852018-01-26 14:43:49 +0100499 u8 *addr;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200500 cfiword_t cword;
501 int retval;
wdenke85b7a52004-10-10 22:16:06 +0000502
Mario Sixfa290692018-01-26 14:43:31 +0100503 addr = flash_map(info, sect, offset);
504 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200505 switch (info->portwidth) {
506 case FLASH_CFI_8BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200507 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200508 break;
509 case FLASH_CFI_16BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200510 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200511 break;
512 case FLASH_CFI_32BIT:
Stefan Roesecff2b492008-06-16 10:40:02 +0200513 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200514 break;
515 case FLASH_CFI_64BIT:
Mario Sixe2c07462018-01-26 14:43:33 +0100516 retval = ((flash_read32(addr) != flash_read32(addr)) ||
Mario Sixa828c1e2018-01-26 14:43:36 +0100517 (flash_read32(addr + 4) != flash_read32(addr + 4)));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200518 break;
519 default:
520 retval = 0;
521 break;
522 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100523 flash_unmap(info, sect, offset, addr);
524
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200525 return retval;
wdenk2cefd152004-02-08 22:55:38 +0000526}
527
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200528/*
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.
wdenk2cefd152004-02-08 22:55:38 +0000533 */
Mario Six88f439f2018-01-26 14:43:32 +0100534static int flash_is_busy(flash_info_t *info, flash_sect_t sect)
wdenk5c71a7a2005-05-16 15:23:22 +0000535{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200536 int retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000537
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200538 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400539 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200540 case CFI_CMDSET_INTEL_STANDARD:
541 case CFI_CMDSET_INTEL_EXTENDED:
Mario Sixfa290692018-01-26 14:43:31 +0100542 retval = !flash_isset(info, sect, 0, FLASH_STATUS_DONE);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200543 break;
544 case CFI_CMDSET_AMD_STANDARD:
545 case CFI_CMDSET_AMD_EXTENDED:
546#ifdef CONFIG_FLASH_CFI_LEGACY
547 case CFI_CMDSET_AMD_LEGACY:
548#endif
Marek Vasut9b718472017-09-12 19:09:31 +0200549 if (info->sr_supported) {
Mario Sixfa290692018-01-26 14:43:31 +0100550 flash_write_cmd(info, sect, info->addr_unlock1,
Mario Sixdde85502018-01-26 14:43:55 +0100551 FLASH_CMD_READ_STATUS);
Mario Sixfa290692018-01-26 14:43:31 +0100552 retval = !flash_isset(info, sect, 0,
Mario Sixdde85502018-01-26 14:43:55 +0100553 FLASH_STATUS_DONE);
Marek Vasut9b718472017-09-12 19:09:31 +0200554 } else {
Mario Sixfa290692018-01-26 14:43:31 +0100555 retval = flash_toggle(info, sect, 0,
Mario Sixdde85502018-01-26 14:43:55 +0100556 AMD_STATUS_TOGGLE);
Marek Vasut9b718472017-09-12 19:09:31 +0200557 }
558
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200559 break;
560 default:
561 retval = 0;
wdenk5c71a7a2005-05-16 15:23:22 +0000562 }
Mario Six9355d552018-01-26 14:43:40 +0100563 debug("%s: %d\n", __func__, retval);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200564 return retval;
wdenk5c71a7a2005-05-16 15:23:22 +0000565}
566
567/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200568 * 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.
wdenk5c71a7a2005-05-16 15:23:22 +0000570 */
Mario Six88f439f2018-01-26 14:43:32 +0100571static int flash_status_check(flash_info_t *info, flash_sect_t sector,
Mario Sixdde85502018-01-26 14:43:55 +0100572 ulong tout, char *prompt)
wdenk2cefd152004-02-08 22:55:38 +0000573{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200574 ulong start;
wdenk2cefd152004-02-08 22:55:38 +0000575
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200576#if CONFIG_SYS_HZ != 1000
Mario Sixbc762c12018-01-26 14:43:54 +0100577 /* Avoid overflow for large HZ */
Renato Andreolaac6693d2010-03-24 23:00:47 +0800578 if ((ulong)CONFIG_SYS_HZ > 100000)
Mario Sixbc762c12018-01-26 14:43:54 +0100579 tout *= (ulong)CONFIG_SYS_HZ / 1000;
Renato Andreolaac6693d2010-03-24 23:00:47 +0800580 else
581 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200582#endif
wdenk2cefd152004-02-08 22:55:38 +0000583
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200584 /* Wait for command completion */
Graeme Russ13ec42b2011-07-15 02:18:56 +0000585#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou4b7e6682010-04-01 11:15:05 +0800586 reset_timer();
Graeme Russ13ec42b2011-07-15 02:18:56 +0000587#endif
Mario Sixfa290692018-01-26 14:43:31 +0100588 start = get_timer(0);
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +0100589 WATCHDOG_RESET();
Mario Sixfa290692018-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 Sixdde85502018-01-26 14:43:55 +0100593 prompt, info->start[sector],
594 flash_read_long(info, sector, 0));
Mario Sixfa290692018-01-26 14:43:31 +0100595 flash_write_cmd(info, sector, 0, info->cmd_reset);
Stefan Roese70a90b72013-04-12 19:04:54 +0200596 udelay(1);
Mario Six324b9402018-01-26 14:43:52 +0100597 return ERR_TIMEOUT;
wdenk2cefd152004-02-08 22:55:38 +0000598 }
Mario Sixfa290692018-01-26 14:43:31 +0100599 udelay(1); /* also triggers watchdog */
wdenk2cefd152004-02-08 22:55:38 +0000600 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200601 return ERR_OK;
602}
wdenk2cefd152004-02-08 22:55:38 +0000603
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200604/*-----------------------------------------------------------------------
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 Six88f439f2018-01-26 14:43:32 +0100610static int flash_full_status_check(flash_info_t *info, flash_sect_t sector,
Mario Sixdde85502018-01-26 14:43:55 +0100611 ulong tout, char *prompt)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200612{
613 int retcode;
wdenk2cefd152004-02-08 22:55:38 +0000614
Mario Sixfa290692018-01-26 14:43:31 +0100615 retcode = flash_status_check(info, sector, tout, prompt);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200616 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400617 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200618 case CFI_CMDSET_INTEL_EXTENDED:
619 case CFI_CMDSET_INTEL_STANDARD:
Mario Sixd1141c52018-01-26 14:43:42 +0100620 if (retcode == ERR_OK &&
Mario Sixdde85502018-01-26 14:43:55 +0100621 !flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200622 retcode = ERR_INVAL;
Mario Sixfa290692018-01-26 14:43:31 +0100623 printf("Flash %s error at address %lx\n", prompt,
Mario Sixdde85502018-01-26 14:43:55 +0100624 info->start[sector]);
Mario Sixfa290692018-01-26 14:43:31 +0100625 if (flash_isset(info, sector, 0, FLASH_STATUS_ECLBS |
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200626 FLASH_STATUS_PSLBS)) {
Mario Sixfa290692018-01-26 14:43:31 +0100627 puts("Command Sequence Error.\n");
628 } else if (flash_isset(info, sector, 0,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200629 FLASH_STATUS_ECLBS)) {
Mario Sixfa290692018-01-26 14:43:31 +0100630 puts("Block Erase Error.\n");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200631 retcode = ERR_NOT_ERASED;
Mario Sixfa290692018-01-26 14:43:31 +0100632 } else if (flash_isset(info, sector, 0,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200633 FLASH_STATUS_PSLBS)) {
Mario Sixfa290692018-01-26 14:43:31 +0100634 puts("Locking Error\n");
wdenk2cefd152004-02-08 22:55:38 +0000635 }
Mario Sixfa290692018-01-26 14:43:31 +0100636 if (flash_isset(info, sector, 0, FLASH_STATUS_DPS)) {
637 puts("Block locked.\n");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200638 retcode = ERR_PROTECTED;
639 }
Mario Sixfa290692018-01-26 14:43:31 +0100640 if (flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
641 puts("Vpp Low Error.\n");
wdenk2cefd152004-02-08 22:55:38 +0000642 }
Mario Sixfa290692018-01-26 14:43:31 +0100643 flash_write_cmd(info, sector, 0, info->cmd_reset);
Aaron Williamsffcef2f2011-04-12 00:59:04 -0700644 udelay(1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200645 break;
646 default:
647 break;
wdenk2cefd152004-02-08 22:55:38 +0000648 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200649 return retcode;
wdenk2cefd152004-02-08 22:55:38 +0000650}
651
Thomas Chou076767a2010-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 Sixbc762c12018-01-26 14:43:54 +0100670 /* Avoid overflow for large HZ */
Thomas Chou076767a2010-03-26 08:17:00 +0800671 if ((ulong)CONFIG_SYS_HZ > 100000)
Mario Sixbc762c12018-01-26 14:43:54 +0100672 tout *= (ulong)CONFIG_SYS_HZ / 1000;
Thomas Chou076767a2010-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 Russ13ec42b2011-07-15 02:18:56 +0000678#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou4b7e6682010-04-01 11:15:05 +0800679 reset_timer();
Graeme Russ13ec42b2011-07-15 02:18:56 +0000680#endif
Thomas Chou076767a2010-03-26 08:17:00 +0800681 start = get_timer(0);
Jens Scharsig (BuS Elektronik)287a1582012-01-27 09:29:53 +0100682 WATCHDOG_RESET();
Thomas Chou076767a2010-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 Six324b9402018-01-26 14:43:52 +0100706 return ERR_TIMEOUT;
Thomas Chou076767a2010-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
wdenk2cefd152004-02-08 22:55:38 +0000714/*-----------------------------------------------------------------------
715 */
Mario Six88f439f2018-01-26 14:43:32 +0100716static void flash_add_byte(flash_info_t *info, cfiword_t *cword, uchar c)
wdenk2cefd152004-02-08 22:55:38 +0000717{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200718#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200719 unsigned short w;
720 unsigned int l;
721 unsigned long long ll;
722#endif
wdenk2cefd152004-02-08 22:55:38 +0000723
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200724 switch (info->portwidth) {
725 case FLASH_CFI_8BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100726 cword->w8 = c;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200727 break;
728 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200729#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200730 w = c;
731 w <<= 8;
Ryan Harkin316870c2015-10-23 16:50:51 +0100732 cword->w16 = (cword->w16 >> 8) | w;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200733#else
Ryan Harkin316870c2015-10-23 16:50:51 +0100734 cword->w16 = (cword->w16 << 8) | c;
Michael Schwingen73d044d2007-12-07 23:35:02 +0100735#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200736 break;
737 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200738#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200739 l = c;
740 l <<= 24;
Ryan Harkin316870c2015-10-23 16:50:51 +0100741 cword->w32 = (cword->w32 >> 8) | l;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200742#else
Ryan Harkin316870c2015-10-23 16:50:51 +0100743 cword->w32 = (cword->w32 << 8) | c;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200744#endif
745 break;
746 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200747#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200748 ll = c;
749 ll <<= 56;
Ryan Harkin316870c2015-10-23 16:50:51 +0100750 cword->w64 = (cword->w64 >> 8) | ll;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200751#else
Ryan Harkin316870c2015-10-23 16:50:51 +0100752 cword->w64 = (cword->w64 << 8) | c;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200753#endif
754 break;
Stefan Roese12797482006-11-13 13:55:24 +0100755 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200756}
wdenk2cefd152004-02-08 22:55:38 +0000757
Jens Gehrlein8ee4add2008-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.
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200761 */
Mario Six88f439f2018-01-26 14:43:32 +0100762static flash_sect_t find_sector(flash_info_t *info, ulong addr)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200763{
Kim Phillipsd303b862012-10-29 13:34:45 +0000764 static flash_sect_t saved_sector; /* previously found sector */
Stefan Roese70a90b72013-04-12 19:04:54 +0200765 static flash_info_t *saved_info; /* previously used flash bank */
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100766 flash_sect_t sector = saved_sector;
767
Mario Sixd1141c52018-01-26 14:43:42 +0100768 if (info != saved_info || sector >= info->sector_count)
Stefan Roese70a90b72013-04-12 19:04:54 +0200769 sector = 0;
770
Mario Sixaaf1a4a2018-01-26 14:43:53 +0100771 while ((sector < info->sector_count - 1) &&
Mario Sixdde85502018-01-26 14:43:55 +0100772 (info->start[sector] < addr))
Jens Gehrlein8ee4add2008-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--;
wdenk2cefd152004-02-08 22:55:38 +0000780
Jens Gehrlein8ee4add2008-12-16 17:25:55 +0100781 saved_sector = sector;
Stefan Roese70a90b72013-04-12 19:04:54 +0200782 saved_info = info;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200783 return sector;
wdenk2cefd152004-02-08 22:55:38 +0000784}
785
786/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +0000787 */
Mario Sixdde85502018-01-26 14:43:55 +0100788static int flash_write_cfiword(flash_info_t *info, ulong dest, cfiword_t cword)
wdenk2cefd152004-02-08 22:55:38 +0000789{
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600790 void *dstaddr = (void *)dest;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200791 int flag;
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100792 flash_sect_t sect = 0;
793 char sect_found = 0;
wdenk2cefd152004-02-08 22:55:38 +0000794
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200795 /* Check if Flash is (sufficiently) erased */
796 switch (info->portwidth) {
797 case FLASH_CFI_8BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100798 flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200799 break;
800 case FLASH_CFI_16BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100801 flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200802 break;
803 case FLASH_CFI_32BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100804 flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200805 break;
806 case FLASH_CFI_64BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100807 flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200808 break;
809 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100810 flag = 0;
811 break;
wdenk2cefd152004-02-08 22:55:38 +0000812 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -0600813 if (!flag)
Stefan Roese707c1462007-12-27 07:50:54 +0100814 return ERR_NOT_ERASED;
wdenk2cefd152004-02-08 22:55:38 +0000815
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200816 /* Disable interrupts which might cause a timeout here */
Mario Sixfa290692018-01-26 14:43:31 +0100817 flag = disable_interrupts();
Stefan Roesec865e6c2006-02-28 15:29:58 +0100818
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200819 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400820 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200821 case CFI_CMDSET_INTEL_EXTENDED:
822 case CFI_CMDSET_INTEL_STANDARD:
Mario Sixfa290692018-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 Skinnemoen6dd763c2007-10-06 18:55:36 +0200825 break;
826 case CFI_CMDSET_AMD_EXTENDED:
827 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout2da14102008-10-09 01:26:36 -0500828 sect = find_sector(info, dest);
Mario Sixfa290692018-01-26 14:43:31 +0100829 flash_unlock_seq(info, sect);
830 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100831 sect_found = 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200832 break;
Po-Yu Chuang3f483402009-07-10 18:03:57 +0800833#ifdef CONFIG_FLASH_CFI_LEGACY
834 case CFI_CMDSET_AMD_LEGACY:
835 sect = find_sector(info, dest);
Mario Sixfa290692018-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 Chuang3f483402009-07-10 18:03:57 +0800838 sect_found = 1;
839 break;
840#endif
wdenk2cefd152004-02-08 22:55:38 +0000841 }
842
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200843 switch (info->portwidth) {
844 case FLASH_CFI_8BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100845 flash_write8(cword.w8, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200846 break;
847 case FLASH_CFI_16BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100848 flash_write16(cword.w16, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200849 break;
850 case FLASH_CFI_32BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100851 flash_write32(cword.w32, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200852 break;
853 case FLASH_CFI_64BIT:
Ryan Harkin316870c2015-10-23 16:50:51 +0100854 flash_write64(cword.w64, dstaddr);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200855 break;
wdenk2cefd152004-02-08 22:55:38 +0000856 }
857
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200858 /* re-enable interrupts if necessary */
859 if (flag)
Mario Sixfa290692018-01-26 14:43:31 +0100860 enable_interrupts();
wdenk2cefd152004-02-08 22:55:38 +0000861
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100862 if (!sect_found)
Mario Sixfa290692018-01-26 14:43:31 +0100863 sect = find_sector(info, dest);
Jens Gehrlein1e814cf2008-12-16 17:25:54 +0100864
Thomas Chou076767a2010-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");
wdenk2cefd152004-02-08 22:55:38 +0000871}
872
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200873#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenk2cefd152004-02-08 22:55:38 +0000874
Mario Six88f439f2018-01-26 14:43:32 +0100875static int flash_write_cfibuffer(flash_info_t *info, ulong dest, uchar *cp,
Mario Sixdde85502018-01-26 14:43:55 +0100876 int len)
wdenk2cefd152004-02-08 22:55:38 +0000877{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200878 flash_sect_t sector;
879 int cnt;
880 int retcode;
Mario Sixf4bab852018-01-26 14:43:49 +0100881 u8 *src = cp;
882 u8 *dst = (u8 *)dest;
883 u8 *dst2 = dst;
Tao Houdd3b4552012-03-15 23:33:58 +0800884 int flag = 1;
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200885 uint offset = 0;
886 unsigned int shift;
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400887 uchar write_cmd;
Stefan Roese707c1462007-12-27 07:50:54 +0100888
889 switch (info->portwidth) {
890 case FLASH_CFI_8BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200891 shift = 0;
Stefan Roese707c1462007-12-27 07:50:54 +0100892 break;
893 case FLASH_CFI_16BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200894 shift = 1;
Stefan Roese707c1462007-12-27 07:50:54 +0100895 break;
896 case FLASH_CFI_32BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200897 shift = 2;
Stefan Roese707c1462007-12-27 07:50:54 +0100898 break;
899 case FLASH_CFI_64BIT:
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200900 shift = 3;
Stefan Roese707c1462007-12-27 07:50:54 +0100901 break;
902 default:
903 retcode = ERR_INVAL;
904 goto out_unmap;
905 }
906
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200907 cnt = len >> shift;
908
Tao Houdd3b4552012-03-15 23:33:58 +0800909 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese707c1462007-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 }
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100937
Stefan Roese707c1462007-12-27 07:50:54 +0100938 src = cp;
Mario Sixfa290692018-01-26 14:43:31 +0100939 sector = find_sector(info, dest);
wdenke65527f2004-02-12 00:47:09 +0000940
941 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400942 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +0000943 case CFI_CMDSET_INTEL_STANDARD:
944 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +0400945 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
Mario Sixbc762c12018-01-26 14:43:54 +0100946 FLASH_CMD_WRITE_BUFFER_PROG :
947 FLASH_CMD_WRITE_TO_BUFFER;
Mario Sixfa290692018-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 Sixdde85502018-01-26 14:43:55 +0100952 info->buffer_write_tout,
953 "write to buffer");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200954 if (retcode == ERR_OK) {
955 /* reduce the number of loops by the width of
Mario Six1ec6d342018-01-26 14:43:41 +0100956 * the port
957 */
Guennadi Liakhovetski183284f2008-04-03 13:36:02 +0200958 cnt = len >> shift;
Mario Sixfa290692018-01-26 14:43:31 +0100959 flash_write_cmd(info, sector, 0, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200960 while (cnt-- > 0) {
961 switch (info->portwidth) {
962 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100963 flash_write8(flash_read8(src), dst);
964 src += 1, dst += 1;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200965 break;
966 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100967 flash_write16(flash_read16(src), dst);
968 src += 2, dst += 2;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200969 break;
970 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100971 flash_write32(flash_read32(src), dst);
972 src += 4, dst += 4;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200973 break;
974 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +0100975 flash_write64(flash_read64(src), dst);
976 src += 8, dst += 8;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200977 break;
978 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100979 retcode = ERR_INVAL;
980 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200981 }
982 }
Mario Sixfa290692018-01-26 14:43:31 +0100983 flash_write_cmd(info, sector, 0,
Mario Sixdde85502018-01-26 14:43:55 +0100984 FLASH_CMD_WRITE_BUFFER_CONFIRM);
Mario Sixfa290692018-01-26 14:43:31 +0100985 retcode = flash_full_status_check(
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200986 info, sector, info->buffer_write_tout,
987 "buffer write");
988 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +0100989
990 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +0200991
wdenk2cefd152004-02-08 22:55:38 +0000992 case CFI_CMDSET_AMD_STANDARD:
993 case CFI_CMDSET_AMD_EXTENDED:
Rouven Behrb0805e22016-04-10 13:38:13 +0200994 flash_unlock_seq(info, sector);
Guennadi Liakhovetski183284f2008-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 Schmolleree355882009-08-12 10:55:47 -05001001 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001002
1003 switch (info->portwidth) {
1004 case FLASH_CFI_8BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001005 while (cnt-- > 0) {
1006 flash_write8(flash_read8(src), dst);
1007 src += 1, dst += 1;
1008 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001009 break;
1010 case FLASH_CFI_16BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001011 while (cnt-- > 0) {
1012 flash_write16(flash_read16(src), dst);
1013 src += 2, dst += 2;
1014 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001015 break;
1016 case FLASH_CFI_32BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001017 while (cnt-- > 0) {
1018 flash_write32(flash_read32(src), dst);
1019 src += 4, dst += 4;
1020 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001021 break;
1022 case FLASH_CFI_64BIT:
Haavard Skinnemoen21b95b42007-12-13 12:56:32 +01001023 while (cnt-- > 0) {
1024 flash_write64(flash_read64(src), dst);
1025 src += 8, dst += 8;
1026 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001027 break;
1028 default:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001029 retcode = ERR_INVAL;
1030 goto out_unmap;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001031 }
1032
Mario Sixfa290692018-01-26 14:43:31 +01001033 flash_write_cmd(info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Chou076767a2010-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 Skinnemoene3ea1882007-12-13 12:56:34 +01001043 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001044
wdenk2cefd152004-02-08 22:55:38 +00001045 default:
Mario Sixfa290692018-01-26 14:43:31 +01001046 debug("Unknown Command Set\n");
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001047 retcode = ERR_INVAL;
1048 break;
wdenk2cefd152004-02-08 22:55:38 +00001049 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001050
1051out_unmap:
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001052 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001053}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001054#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001055
wdenk2cefd152004-02-08 22:55:38 +00001056/*-----------------------------------------------------------------------
wdenk2cefd152004-02-08 22:55:38 +00001057 */
Mario Six88f439f2018-01-26 14:43:32 +01001058int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenk2cefd152004-02-08 22:55:38 +00001059{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001060 int rcode = 0;
1061 int prot;
1062 flash_sect_t sect;
Thomas Chou076767a2010-03-26 08:17:00 +08001063 int st;
wdenk2cefd152004-02-08 22:55:38 +00001064
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001065 if (info->flash_id != FLASH_MAN_CFI) {
Mario Sixfa290692018-01-26 14:43:31 +01001066 puts("Can't erase unknown flash type - aborted\n");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001067 return 1;
1068 }
Mario Sixd1141c52018-01-26 14:43:42 +01001069 if (s_first < 0 || s_first > s_last) {
Mario Sixfa290692018-01-26 14:43:31 +01001070 puts("- no sectors to erase\n");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001071 return 1;
1072 }
Stefan Roeseefef95b2006-04-01 13:41:03 +02001073
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001074 prot = 0;
Mario Sixcbe41ca2018-01-26 14:43:38 +01001075 for (sect = s_first; sect <= s_last; ++sect)
1076 if (info->protect[sect])
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001077 prot++;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001078 if (prot) {
Mario Sixfa290692018-01-26 14:43:31 +01001079 printf("- Warning: %d protected sectors will not be erased!\n",
Mario Sixdde85502018-01-26 14:43:55 +01001080 prot);
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001081 } else if (flash_verbose) {
Mario Sixfa290692018-01-26 14:43:31 +01001082 putc('\n');
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001083 }
wdenke65527f2004-02-12 00:47:09 +00001084
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001085 for (sect = s_first; sect <= s_last; sect++) {
Joe Hershberger497c32f2012-08-17 15:36:41 -05001086 if (ctrlc()) {
1087 printf("\n");
1088 return 1;
1089 }
1090
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001091 if (info->protect[sect] == 0) { /* not protected */
Joe Hershberger7f3c2112012-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
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001118 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001119 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001120 case CFI_CMDSET_INTEL_STANDARD:
1121 case CFI_CMDSET_INTEL_EXTENDED:
Mario Sixfa290692018-01-26 14:43:31 +01001122 flash_write_cmd(info, sect, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001123 FLASH_CMD_CLEAR_STATUS);
Mario Sixfa290692018-01-26 14:43:31 +01001124 flash_write_cmd(info, sect, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001125 FLASH_CMD_BLOCK_ERASE);
Mario Sixfa290692018-01-26 14:43:31 +01001126 flash_write_cmd(info, sect, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001127 FLASH_CMD_ERASE_CONFIRM);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001128 break;
1129 case CFI_CMDSET_AMD_STANDARD:
1130 case CFI_CMDSET_AMD_EXTENDED:
Mario Sixfa290692018-01-26 14:43:31 +01001131 flash_unlock_seq(info, sect);
1132 flash_write_cmd(info, sect,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001133 info->addr_unlock1,
1134 AMD_CMD_ERASE_START);
Mario Sixfa290692018-01-26 14:43:31 +01001135 flash_unlock_seq(info, sect);
1136 flash_write_cmd(info, sect, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001137 info->cmd_erase_sector);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001138 break;
1139#ifdef CONFIG_FLASH_CFI_LEGACY
1140 case CFI_CMDSET_AMD_LEGACY:
Mario Sixfa290692018-01-26 14:43:31 +01001141 flash_unlock_seq(info, 0);
1142 flash_write_cmd(info, 0, info->addr_unlock1,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001143 AMD_CMD_ERASE_START);
Mario Sixfa290692018-01-26 14:43:31 +01001144 flash_unlock_seq(info, 0);
1145 flash_write_cmd(info, sect, 0,
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001146 AMD_CMD_ERASE_SECTOR);
1147 break;
1148#endif
1149 default:
Mario Sixacf12082018-01-26 14:43:44 +01001150 debug("Unknown flash vendor %d\n",
Mario Sixdde85502018-01-26 14:43:55 +01001151 info->vendor);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001152 break;
wdenke65527f2004-02-12 00:47:09 +00001153 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001154
Thomas Chou076767a2010-03-26 08:17:00 +08001155 if (use_flash_status_poll(info)) {
Kim Phillipsd303b862012-10-29 13:34:45 +00001156 cfiword_t cword;
Thomas Chou076767a2010-03-26 08:17:00 +08001157 void *dest;
Mario Sixc7e359e2018-01-26 14:43:37 +01001158
Ryan Harkin316870c2015-10-23 16:50:51 +01001159 cword.w64 = 0xffffffffffffffffULL;
Thomas Chou076767a2010-03-26 08:17:00 +08001160 dest = flash_map(info, sect, 0);
1161 st = flash_status_poll(info, &cword, dest,
Mario Sixbc762c12018-01-26 14:43:54 +01001162 info->erase_blk_tout,
1163 "erase");
Thomas Chou076767a2010-03-26 08:17:00 +08001164 flash_unmap(info, sect, 0, dest);
Mario Six179b8d62018-01-26 14:43:43 +01001165 } else {
Thomas Chou076767a2010-03-26 08:17:00 +08001166 st = flash_full_status_check(info, sect,
1167 info->erase_blk_tout,
1168 "erase");
Mario Six179b8d62018-01-26 14:43:43 +01001169 }
1170
Thomas Chou076767a2010-03-26 08:17:00 +08001171 if (st)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001172 rcode = 1;
Thomas Chou076767a2010-03-26 08:17:00 +08001173 else if (flash_verbose)
Mario Sixfa290692018-01-26 14:43:31 +01001174 putc('.');
wdenk2cefd152004-02-08 22:55:38 +00001175 }
wdenk2cefd152004-02-08 22:55:38 +00001176 }
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001177
1178 if (flash_verbose)
Mario Sixfa290692018-01-26 14:43:31 +01001179 puts(" done\n");
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001180
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001181 return rcode;
wdenk2cefd152004-02-08 22:55:38 +00001182}
wdenke65527f2004-02-12 00:47:09 +00001183
Stefan Roeseb6f508d2010-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 Roesea9153f22010-10-25 18:31:39 +02001189 u32 *flash;
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001190
1191 /*
1192 * Check if whole sector is erased
1193 */
1194 size = flash_sector_size(info, i);
Stefan Roesea9153f22010-10-25 18:31:39 +02001195 flash = (u32 *)info->start[i];
Stefan Roeseb6f508d2010-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 Roesea9153f22010-10-25 18:31:39 +02001200 if (flash_read32(flash++) != 0xffffffff)
Stefan Roeseb6f508d2010-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 Six88f439f2018-01-26 14:43:32 +01001208void flash_print_info(flash_info_t *info)
wdenk2cefd152004-02-08 22:55:38 +00001209{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001210 int i;
wdenk369d43d2004-03-14 14:09:05 +00001211
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001212 if (info->flash_id != FLASH_MAN_CFI) {
Mario Sixfa290692018-01-26 14:43:31 +01001213 puts("missing or unknown FLASH type\n");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001214 return;
1215 }
1216
Mario Sixfa290692018-01-26 14:43:31 +01001217 printf("%s flash (%d x %d)",
Mario Sixdde85502018-01-26 14:43:55 +01001218 info->name,
1219 (info->portwidth << 3), (info->chipwidth << 3));
Mario Sixa828c1e2018-01-26 14:43:36 +01001220 if (info->size < 1024 * 1024)
Mario Sixfa290692018-01-26 14:43:31 +01001221 printf(" Size: %ld kB in %d Sectors\n",
Mario Sixdde85502018-01-26 14:43:55 +01001222 info->size >> 10, info->sector_count);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001223 else
Mario Sixfa290692018-01-26 14:43:31 +01001224 printf(" Size: %ld MB in %d Sectors\n",
Mario Sixdde85502018-01-26 14:43:55 +01001225 info->size >> 20, info->sector_count);
Mario Sixfa290692018-01-26 14:43:31 +01001226 printf(" ");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001227 switch (info->vendor) {
Mario Six76857f02018-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;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001243#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Six76857f02018-01-26 14:43:35 +01001244 case CFI_CMDSET_AMD_LEGACY:
1245 printf("AMD Legacy");
1246 break;
wdenk369d43d2004-03-14 14:09:05 +00001247#endif
Mario Six76857f02018-01-26 14:43:35 +01001248 default:
1249 printf("Unknown (%d)", info->vendor);
1250 break;
wdenk2cefd152004-02-08 22:55:38 +00001251 }
Mario Sixfa290692018-01-26 14:43:31 +01001252 printf(" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
Mario Sixdde85502018-01-26 14:43:55 +01001253 info->manufacturer_id);
Mario Sixfa290692018-01-26 14:43:31 +01001254 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
Mario Sixdde85502018-01-26 14:43:55 +01001255 info->device_id);
Heiko Schocher27cea502011-04-11 14:16:19 +02001256 if ((info->device_id & 0xff) == 0x7E) {
1257 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
Mario Sixdde85502018-01-26 14:43:55 +01001258 info->device_id2);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001259 }
Mario Sixd1141c52018-01-26 14:43:42 +01001260 if (info->vendor == CFI_CMDSET_AMD_STANDARD && info->legacy_unlock)
Stefan Roesee442a902012-12-06 15:44:12 +01001261 printf("\n Advanced Sector Protection (PPB) enabled");
Mario Sixfa290692018-01-26 14:43:31 +01001262 printf("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
Mario Sixdde85502018-01-26 14:43:55 +01001263 info->erase_blk_tout, info->write_tout);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001264 if (info->buffer_size > 1) {
Mario Six246e5062018-01-26 14:43:50 +01001265 printf(" Buffer write timeout: %ld ms, ",
Mario Sixdde85502018-01-26 14:43:55 +01001266 info->buffer_write_tout);
Mario Six246e5062018-01-26 14:43:50 +01001267 printf("buffer size: %d bytes\n", info->buffer_size);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001268 }
wdenk2cefd152004-02-08 22:55:38 +00001269
Mario Sixfa290692018-01-26 14:43:31 +01001270 puts("\n Sector Start Addresses:");
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001271 for (i = 0; i < info->sector_count; ++i) {
Kim Phillipsc8836f12010-07-26 18:35:39 -05001272 if (ctrlc())
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001273 break;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001274 if ((i % 5) == 0)
Stefan Roeseb6f508d2010-08-13 09:36:36 +02001275 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001276#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001277 /* print empty and read-only info */
Mario Sixfa290692018-01-26 14:43:31 +01001278 printf(" %08lX %c %s ",
Mario Sixdde85502018-01-26 14:43:55 +01001279 info->start[i],
1280 sector_erased(info, i) ? 'E' : ' ',
1281 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001282#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Mario Sixfa290692018-01-26 14:43:31 +01001283 printf(" %08lX %s ",
Mario Sixdde85502018-01-26 14:43:55 +01001284 info->start[i],
1285 info->protect[i] ? "RO" : " ");
wdenke65527f2004-02-12 00:47:09 +00001286#endif
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001287 }
Mario Sixfa290692018-01-26 14:43:31 +01001288 putc('\n');
wdenk2cefd152004-02-08 22:55:38 +00001289}
1290
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001291/*-----------------------------------------------------------------------
Jerry Van Barenaae73572008-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 Roese7758c162008-03-19 07:09:26 +01001297#ifdef CONFIG_FLASH_SHOW_PROGRESS
1298#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001299 if (flash_verbose) { \
1300 dots -= dots_sub; \
Mario Sixd1141c52018-01-26 14:43:42 +01001301 if (scale > 0 && dots <= 0) { \
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001302 if ((digit % 5) == 0) \
Mario Sixfa290692018-01-26 14:43:31 +01001303 printf("%d", digit / 5); \
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001304 else \
Mario Sixfa290692018-01-26 14:43:31 +01001305 putc('.'); \
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01001306 digit--; \
1307 dots += scale; \
1308 } \
Jerry Van Barenaae73572008-03-08 13:48:01 -05001309 }
Stefan Roese7758c162008-03-19 07:09:26 +01001310#else
1311#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1312#endif
Jerry Van Barenaae73572008-03-08 13:48:01 -05001313
1314/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001315 * Copy memory to flash, returns:
1316 * 0 - OK
1317 * 1 - write timeout
1318 * 2 - Flash not erased
wdenk2cefd152004-02-08 22:55:38 +00001319 */
Mario Six88f439f2018-01-26 14:43:32 +01001320int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenk2cefd152004-02-08 22:55:38 +00001321{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001322 ulong wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001323 uchar *p;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001324 int aln;
wdenk2cefd152004-02-08 22:55:38 +00001325 cfiword_t cword;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001326 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001327#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001328 int buffered_size;
wdenk2cefd152004-02-08 22:55:38 +00001329#endif
Jerry Van Barenaae73572008-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
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001344 /* get lower aligned address */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001345 wp = (addr & ~(info->portwidth - 1));
Haiying Wangc123a382007-02-21 16:52:31 +01001346
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001347 /* handle unaligned start */
Mario Six6cf55742018-01-26 14:43:48 +01001348 aln = addr - wp;
1349 if (aln != 0) {
Ryan Harkin316870c2015-10-23 16:50:51 +01001350 cword.w32 = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001351 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001352 for (i = 0; i < aln; ++i)
Mario Sixfa290692018-01-26 14:43:31 +01001353 flash_add_byte(info, &cword, flash_read8(p + i));
wdenk2cefd152004-02-08 22:55:38 +00001354
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001355 for (; (i < info->portwidth) && (cnt > 0); i++) {
Mario Sixfa290692018-01-26 14:43:31 +01001356 flash_add_byte(info, &cword, *src++);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001357 cnt--;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001358 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001359 for (; (cnt == 0) && (i < info->portwidth); ++i)
Mario Sixfa290692018-01-26 14:43:31 +01001360 flash_add_byte(info, &cword, flash_read8(p + i));
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001361
Mario Sixfa290692018-01-26 14:43:31 +01001362 rc = flash_write_cfiword(info, wp, cword);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001363 if (rc != 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001364 return rc;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001365
1366 wp += i;
Stefan Roese7758c162008-03-19 07:09:26 +01001367 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001368 }
1369
1370 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001371#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001372 buffered_size = (info->portwidth / info->chipwidth);
1373 buffered_size *= info->buffer_size;
1374 while (cnt >= info->portwidth) {
1375 /* prohibit buffer write when buffer_size is 1 */
1376 if (info->buffer_size == 1) {
Ryan Harkin316870c2015-10-23 16:50:51 +01001377 cword.w32 = 0;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001378 for (i = 0; i < info->portwidth; i++)
Mario Sixfa290692018-01-26 14:43:31 +01001379 flash_add_byte(info, &cword, *src++);
Mario Six6cf55742018-01-26 14:43:48 +01001380 rc = flash_write_cfiword(info, wp, cword);
1381 if (rc != 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001382 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 Six6cf55742018-01-26 14:43:48 +01001392 rc = flash_write_cfibuffer(info, wp, src, i);
1393 if (rc != ERR_OK)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001394 return rc;
1395 i -= i & (info->portwidth - 1);
1396 wp += i;
1397 src += i;
1398 cnt -= i;
Stefan Roese7758c162008-03-19 07:09:26 +01001399 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Joe Hershberger497c32f2012-08-17 15:36:41 -05001400 /* Only check every once in a while */
1401 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1402 return ERR_ABORTED;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001403 }
1404#else
1405 while (cnt >= info->portwidth) {
Ryan Harkin316870c2015-10-23 16:50:51 +01001406 cword.w32 = 0;
Mario Sixcbe41ca2018-01-26 14:43:38 +01001407 for (i = 0; i < info->portwidth; i++)
Mario Sixfa290692018-01-26 14:43:31 +01001408 flash_add_byte(info, &cword, *src++);
Mario Six6cf55742018-01-26 14:43:48 +01001409 rc = flash_write_cfiword(info, wp, cword);
1410 if (rc != 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001411 return rc;
1412 wp += info->portwidth;
1413 cnt -= info->portwidth;
Stefan Roese7758c162008-03-19 07:09:26 +01001414 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Joe Hershberger497c32f2012-08-17 15:36:41 -05001415 /* Only check every once in a while */
1416 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1417 return ERR_ABORTED;
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001418 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001419#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Barenaae73572008-03-08 13:48:01 -05001420
Mario Sixcbe41ca2018-01-26 14:43:38 +01001421 if (cnt == 0)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001422 return (0);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001423
1424 /*
1425 * handle unaligned tail bytes
1426 */
Ryan Harkin316870c2015-10-23 16:50:51 +01001427 cword.w32 = 0;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001428 p = (uchar *)wp;
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001429 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Mario Sixfa290692018-01-26 14:43:31 +01001430 flash_add_byte(info, &cword, *src++);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001431 --cnt;
1432 }
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001433 for (; i < info->portwidth; ++i)
Mario Sixfa290692018-01-26 14:43:31 +01001434 flash_add_byte(info, &cword, flash_read8(p + i));
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001435
Mario Sixfa290692018-01-26 14:43:31 +01001436 return flash_write_cfiword(info, wp, cword);
wdenk2cefd152004-02-08 22:55:38 +00001437}
wdenke65527f2004-02-12 00:47:09 +00001438
Stefan Roese92b1bca2012-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
wdenk2cefd152004-02-08 22:55:38 +00001444/*-----------------------------------------------------------------------
1445 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001446#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001447
Holger Brunck94c302d2012-08-09 10:22:41 +02001448static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1449{
Mario Six0c9be972018-01-26 14:43:39 +01001450 if (manufact_match(info, INTEL_MANUFACT) &&
Mario Sixdde85502018-01-26 14:43:55 +01001451 info->device_id == NUMONYX_256MBIT) {
Holger Brunck94c302d2012-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 Przywara90723f62016-11-16 00:50:06 +00001470
1471 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
Holger Brunck94c302d2012-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 Six88f439f2018-01-26 14:43:32 +01001482int flash_real_protect(flash_info_t *info, long sector, int prot)
wdenk2cefd152004-02-08 22:55:38 +00001483{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001484 int retcode = 0;
wdenke65527f2004-02-12 00:47:09 +00001485
Rafael Campos13d2b612008-07-31 10:22:20 +02001486 switch (info->vendor) {
Mario Six76857f02018-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 Sixdde85502018-01-26 14:43:55 +01001492 FLASH_CMD_CLEAR_STATUS);
Mario Six76857f02018-01-26 14:43:35 +01001493 flash_write_cmd(info, sector, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001494 FLASH_CMD_PROTECT);
Mario Six76857f02018-01-26 14:43:35 +01001495 if (prot)
Holger Brunck94c302d2012-08-09 10:22:41 +02001496 flash_write_cmd(info, sector, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001497 FLASH_CMD_PROTECT_SET);
Mario Six76857f02018-01-26 14:43:35 +01001498 else
Holger Brunck94c302d2012-08-09 10:22:41 +02001499 flash_write_cmd(info, sector, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001500 FLASH_CMD_PROTECT_CLEAR);
Mario Six76857f02018-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 Sixdde85502018-01-26 14:43:55 +01001521 0, ATM_CMD_UNLOCK_SECT);
Philippe De Muyterca6cd162010-08-17 18:40:25 +02001522 }
Mario Six76857f02018-01-26 14:43:35 +01001523 }
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 Sixdde85502018-01-26 14:43:55 +01001535 AMD_CMD_PPB_LOCK_BC1);
Mario Sixfa290692018-01-26 14:43:31 +01001536 flash_write_cmd(info, sector, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001537 AMD_CMD_PPB_LOCK_BC2);
Rafael Campos13d2b612008-07-31 10:22:20 +02001538 }
Mario Six76857f02018-01-26 14:43:35 +01001539 debug("sector %ld %slocked\n", sector,
Mario Sixdde85502018-01-26 14:43:55 +01001540 lock_flag ? "" : "already ");
Mario Six76857f02018-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 Sixdde85502018-01-26 14:43:55 +01001545 AMD_CMD_PPB_UNLOCK_BC1);
Mario Six76857f02018-01-26 14:43:35 +01001546 flash_write_cmd(info, 0, 0,
Mario Sixdde85502018-01-26 14:43:55 +01001547 AMD_CMD_PPB_UNLOCK_BC2);
Anatolij Gustschin041b04f2012-08-09 08:18:12 +02001548 }
Mario Six76857f02018-01-26 14:43:35 +01001549 debug("sector %ld %sunlocked\n", sector,
Mario Sixdde85502018-01-26 14:43:55 +01001550 !lock_flag ? "" : "already ");
Mario Six76857f02018-01-26 14:43:35 +01001551 }
1552 if (flag)
1553 enable_interrupts();
Anatolij Gustschin041b04f2012-08-09 08:18:12 +02001554
Mario Six76857f02018-01-26 14:43:35 +01001555 if (flash_status_check(info, sector,
Mario Sixdde85502018-01-26 14:43:55 +01001556 info->erase_blk_tout,
1557 prot ? "protect" : "unprotect"))
Mario Six76857f02018-01-26 14:43:35 +01001558 printf("status check error\n");
Anatolij Gustschin041b04f2012-08-09 08:18:12 +02001559
Mario Six76857f02018-01-26 14:43:35 +01001560 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;
TsiChung Liewb8c19292008-08-19 16:53:39 +00001566#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Six76857f02018-01-26 14:43:35 +01001567 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 Sixbc762c12018-01-26 14:43:54 +01001571 flash_write_cmd(info, sector, 0,
1572 FLASH_CMD_PROTECT_SET);
Mario Six76857f02018-01-26 14:43:35 +01001573 else
Mario Sixbc762c12018-01-26 14:43:54 +01001574 flash_write_cmd(info, sector, 0,
1575 FLASH_CMD_PROTECT_CLEAR);
TsiChung Liewb8c19292008-08-19 16:53:39 +00001576#endif
Rafael Campos13d2b612008-07-31 10:22:20 +02001577 };
wdenk2cefd152004-02-08 22:55:38 +00001578
Stefan Roese5215df12010-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 Six6cf55742018-01-26 14:43:48 +01001584 retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
Mario Sixdde85502018-01-26 14:43:55 +01001585 prot ? "protect" : "unprotect");
Mario Six6cf55742018-01-26 14:43:48 +01001586 if (retcode == 0) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001587 info->protect[sector] = prot;
1588
1589 /*
1590 * On some of Intel's flash chips (marked via legacy_unlock)
1591 * unprotect unprotects all locking.
1592 */
Mario Sixd1141c52018-01-26 14:43:42 +01001593 if (prot == 0 && info->legacy_unlock) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001594 flash_sect_t i;
1595
1596 for (i = 0; i < info->sector_count; i++) {
1597 if (info->protect[i])
Mario Sixfa290692018-01-26 14:43:31 +01001598 flash_real_protect(info, i, 1);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001599 }
wdenk2cefd152004-02-08 22:55:38 +00001600 }
wdenk2cefd152004-02-08 22:55:38 +00001601 }
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001602 return retcode;
wdenk2cefd152004-02-08 22:55:38 +00001603}
wdenke65527f2004-02-12 00:47:09 +00001604
wdenk2cefd152004-02-08 22:55:38 +00001605/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001606 * flash_read_user_serial - read the OneTimeProgramming cells
wdenk2cefd152004-02-08 22:55:38 +00001607 */
Mario Six88f439f2018-01-26 14:43:32 +01001608void flash_read_user_serial(flash_info_t *info, void *buffer, int offset,
Mario Sixdde85502018-01-26 14:43:55 +01001609 int len)
wdenk2cefd152004-02-08 22:55:38 +00001610{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001611 uchar *src;
1612 uchar *dst;
wdenke65527f2004-02-12 00:47:09 +00001613
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001614 dst = buffer;
Mario Sixfa290692018-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 Williamsffcef2f2011-04-12 00:59:04 -07001619 udelay(1);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001620 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001621}
1622
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001623/*
1624 * flash_read_factory_serial - read the device Id from the protection area
wdenk2cefd152004-02-08 22:55:38 +00001625 */
Mario Six88f439f2018-01-26 14:43:32 +01001626void flash_read_factory_serial(flash_info_t *info, void *buffer, int offset,
Mario Sixdde85502018-01-26 14:43:55 +01001627 int len)
wdenk2cefd152004-02-08 22:55:38 +00001628{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001629 uchar *src;
wdenke65527f2004-02-12 00:47:09 +00001630
Mario Sixfa290692018-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 Williamsffcef2f2011-04-12 00:59:04 -07001635 udelay(1);
Haavard Skinnemoene3ea1882007-12-13 12:56:34 +01001636 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk2cefd152004-02-08 22:55:38 +00001637}
1638
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001639#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001640
Haavard Skinnemoenc4d478b2007-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 Sixd1141c52018-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 Skinnemoenc4d478b2007-12-14 15:36:17 +01001656 }
1657}
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001658
wdenk2cefd152004-02-08 22:55:38 +00001659/*-----------------------------------------------------------------------
Stefan Roese12797482006-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 Skinnemoenc4d478b2007-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 Williamsffcef2f2011-04-12 00:59:04 -07001668 udelay(1);
Haavard Skinnemoenc4d478b2007-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 Sixfa290692018-01-26 14:43:31 +01001671 info->manufacturer_id = flash_read_uchar(info,
Mario Sixdde85502018-01-26 14:43:55 +01001672 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyter35bc0812010-08-10 16:54:52 +02001673 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
Mario Sixfa290692018-01-26 14:43:31 +01001674 flash_read_word(info, FLASH_OFFSET_DEVICE_ID) :
1675 flash_read_uchar(info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoenc4d478b2007-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-VILLARD03836942008-10-16 15:01:15 +02001686#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001687 /* read legacy lock/unlock bit from intel flash */
1688 if (info->ext_addr) {
Mario Sixdde85502018-01-26 14:43:55 +01001689 info->legacy_unlock =
1690 flash_read_uchar(info, info->ext_addr + 5) & 0x08;
Haavard Skinnemoenc4d478b2007-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 Sixf8783d22018-01-26 14:43:51 +01001699 ushort bank_id = 0;
1700 uchar manu_id;
York Sunde067cd2017-11-18 11:09:08 -08001701 uchar feature;
Niklaus Gigerf447f712009-07-22 17:13:24 +02001702
Haavard Skinnemoenc4d478b2007-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 Krill7f2a3052008-03-28 11:29:10 +01001707
Mario Sixf8783d22018-01-26 14:43:51 +01001708 manu_id = flash_read_uchar(info, FLASH_OFFSET_MANUFACTURER_ID);
Niklaus Gigerf447f712009-07-22 17:13:24 +02001709 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
Mario Sixf8783d22018-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 Sixdde85502018-01-26 14:43:55 +01001713 bank_id | FLASH_OFFSET_MANUFACTURER_ID);
Niklaus Gigerf447f712009-07-22 17:13:24 +02001714 }
Mario Sixf8783d22018-01-26 14:43:51 +01001715 info->manufacturer_id = manu_id;
Tor Krill7f2a3052008-03-28 11:29:10 +01001716
York Sunde067cd2017-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 Vasut9b718472017-09-12 19:09:31 +02001725
Mario Sixe2c07462018-01-26 14:43:33 +01001726 switch (info->chipwidth) {
Tor Krill7f2a3052008-03-28 11:29:10 +01001727 case FLASH_CFI_8BIT:
Mario Sixfa290692018-01-26 14:43:31 +01001728 info->device_id = flash_read_uchar(info,
Mario Sixdde85502018-01-26 14:43:55 +01001729 FLASH_OFFSET_DEVICE_ID);
Tor Krill7f2a3052008-03-28 11:29:10 +01001730 if (info->device_id == 0x7E) {
1731 /* AMD 3-byte (expanded) device ids */
Mario Sixfa290692018-01-26 14:43:31 +01001732 info->device_id2 = flash_read_uchar(info,
Mario Sixdde85502018-01-26 14:43:55 +01001733 FLASH_OFFSET_DEVICE_ID2);
Tor Krill7f2a3052008-03-28 11:29:10 +01001734 info->device_id2 <<= 8;
Mario Sixfa290692018-01-26 14:43:31 +01001735 info->device_id2 |= flash_read_uchar(info,
Tor Krill7f2a3052008-03-28 11:29:10 +01001736 FLASH_OFFSET_DEVICE_ID3);
1737 }
1738 break;
1739 case FLASH_CFI_16BIT:
Mario Sixfa290692018-01-26 14:43:31 +01001740 info->device_id = flash_read_word(info,
Mario Sixdde85502018-01-26 14:43:55 +01001741 FLASH_OFFSET_DEVICE_ID);
Heiko Schocher27cea502011-04-11 14:16:19 +02001742 if ((info->device_id & 0xff) == 0x7E) {
1743 /* AMD 3-byte (expanded) device ids */
Mario Sixfa290692018-01-26 14:43:31 +01001744 info->device_id2 = flash_read_uchar(info,
Mario Sixdde85502018-01-26 14:43:55 +01001745 FLASH_OFFSET_DEVICE_ID2);
Heiko Schocher27cea502011-04-11 14:16:19 +02001746 info->device_id2 <<= 8;
Mario Sixfa290692018-01-26 14:43:31 +01001747 info->device_id2 |= flash_read_uchar(info,
Heiko Schocher27cea502011-04-11 14:16:19 +02001748 FLASH_OFFSET_DEVICE_ID3);
1749 }
Tor Krill7f2a3052008-03-28 11:29:10 +01001750 break;
1751 default:
1752 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001753 }
1754 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsffcef2f2011-04-12 00:59:04 -07001755 udelay(1);
Haavard Skinnemoenc4d478b2007-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 Dureghello7ba30282012-12-01 01:14:18 +01001761 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
Haavard Skinnemoenc4d478b2007-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 Gustschin041b04f2012-08-09 08:18:12 +02001766#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roese7de65842012-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 Gustschin041b04f2012-08-09 08:18:12 +02001770 info->legacy_unlock = 1;
1771 }
1772#endif
1773
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01001774 return 0;
1775}
1776
1777#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Six88f439f2018-01-26 14:43:32 +01001778static void flash_read_jedec_ids(flash_info_t *info)
Stefan Roese12797482006-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 Leoenenko7d1794c2008-05-07 21:25:33 +04001785 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese12797482006-11-13 13:55:24 +01001786 case CFI_CMDSET_INTEL_STANDARD:
1787 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001788 cmdset_intel_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001789 break;
1790 case CFI_CMDSET_AMD_STANDARD:
1791 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen5fb0aa42008-01-12 20:29:47 +01001792 cmdset_amd_read_jedec_ids(info);
Stefan Roese12797482006-11-13 13:55:24 +01001793 break;
1794 default:
1795 break;
1796 }
1797}
1798
1799/*-----------------------------------------------------------------------
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001800 * 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.
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001803 */
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001804static int flash_detect_legacy(phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00001805{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001806 flash_info_t *info = &flash_info[banknum];
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001807
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001808 if (board_flash_get_legacy(base, banknum, info)) {
1809 /* board code may have filled info completely. If not, we
Mario Six1ec6d342018-01-26 14:43:41 +01001810 * use JEDEC ID probing.
1811 */
Haavard Skinnemoen6dd763c2007-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;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001818
Axel Lin85706c82013-06-23 00:56:46 +08001819 for (i = 0; i < ARRAY_SIZE(modes); i++) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001820 info->vendor = modes[i];
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001821 info->start[0] =
1822 (ulong)map_physmem(base,
Stefan Roeseb8443702009-02-05 11:44:52 +01001823 info->portwidth,
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001824 MAP_NOCACHE);
Mario Six0c9be972018-01-26 14:43:39 +01001825 if (info->portwidth == FLASH_CFI_8BIT &&
Mario Sixdde85502018-01-26 14:43:55 +01001826 info->interface == FLASH_CFI_X8X16) {
Haavard Skinnemoen6dd763c2007-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 Sixdde85502018-01-26 14:43:55 +01001835 info->manufacturer_id,
1836 info->device_id,
1837 info->device_id2);
Becky Bruce9d1f6af2009-02-02 16:34:51 -06001838 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001839 break;
Mario Six411ede32018-01-26 14:43:45 +01001840
1841 unmap_physmem((void *)info->start[0],
1842 info->portwidth);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001843 }
1844 }
1845
Mario Sixe2c07462018-01-26 14:43:33 +01001846 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04001847 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoen6dd763c2007-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 Bruce9d1f6af2009-02-02 16:34:51 -06001864static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001865{
1866 return 0; /* use CFI */
1867}
1868#endif
1869
1870/*-----------------------------------------------------------------------
1871 * detect if flash is compatible with the Common Flash Interface (CFI)
1872 * http://www.jedec.org/download/search/jesd68.pdf
1873 */
Mario Sixdde85502018-01-26 14:43:55 +01001874static void flash_read_cfi(flash_info_t *info, void *buf, unsigned int start,
1875 size_t len)
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001876{
1877 u8 *p = buf;
1878 unsigned int i;
1879
1880 for (i = 0; i < len; i++)
Stefan Roese70a90b72013-04-12 19:04:54 +02001881 p[i] = flash_read_uchar(info, start + i);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001882}
Stefan Roese6e83e342009-10-27 15:15:55 +01001883
Kim Phillipsd303b862012-10-29 13:34:45 +00001884static void __flash_cmd_reset(flash_info_t *info)
Stefan Roese6e83e342009-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 Williamsffcef2f2011-04-12 00:59:04 -07001892 udelay(1);
Stefan Roese6e83e342009-10-27 15:15:55 +01001893 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1894}
Mario Sixc7e359e2018-01-26 14:43:37 +01001895
Stefan Roese6e83e342009-10-27 15:15:55 +01001896void flash_cmd_reset(flash_info_t *info)
Mario Sixa828c1e2018-01-26 14:43:36 +01001897 __attribute__((weak, alias("__flash_cmd_reset")));
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001898
Mario Six88f439f2018-01-26 14:43:32 +01001899static int __flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001900{
1901 int cfi_offset;
1902
Stefan Roese70a90b72013-04-12 19:04:54 +02001903 /* Issue FLASH reset command */
1904 flash_cmd_reset(info);
1905
Axel Lin85706c82013-06-23 00:56:46 +08001906 for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001907 cfi_offset++) {
Mario Sixfa290692018-01-26 14:43:31 +01001908 flash_write_cmd(info, 0, flash_offset_cfi[cfi_offset],
Mario Sixdde85502018-01-26 14:43:55 +01001909 FLASH_CMD_CFI);
Mario Six0c9be972018-01-26 14:43:39 +01001910 if (flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP, 'Q') &&
Mario Sixbc762c12018-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 Sixdde85502018-01-26 14:43:55 +01001913 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1914 sizeof(struct cfi_qry));
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01001915 info->interface = le16_to_cpu(qry->interface_desc);
Stefan Roese70a90b72013-04-12 19:04:54 +02001916
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001917 info->cfi_offset = flash_offset_cfi[cfi_offset];
Mario Sixfa290692018-01-26 14:43:31 +01001918 debug("device interface is %d\n",
Mario Sixdde85502018-01-26 14:43:55 +01001919 info->interface);
Mario Sixfa290692018-01-26 14:43:31 +01001920 debug("found port %d chip %d ",
Mario Sixdde85502018-01-26 14:43:55 +01001921 info->portwidth, info->chipwidth);
Mario Sixfa290692018-01-26 14:43:31 +01001922 debug("port %d bits chip %d bits\n",
Mario Sixdde85502018-01-26 14:43:55 +01001923 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1924 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02001925
1926 /* calculate command offsets as in the Linux driver */
Stefan Roese70a90b72013-04-12 19:04:54 +02001927 info->addr_unlock1 = 0x555;
1928 info->addr_unlock2 = 0x2aa;
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001929
1930 /*
1931 * modify the unlock address if we are
1932 * in compatibility mode
1933 */
Mario Sixe2c07462018-01-26 14:43:33 +01001934 if (/* x8/x16 in x8 mode */
Mario Sixd1141c52018-01-26 14:43:42 +01001935 (info->chipwidth == FLASH_CFI_BY8 &&
1936 info->interface == FLASH_CFI_X8X16) ||
Mario Sixe2c07462018-01-26 14:43:33 +01001937 /* x16/x32 in x16 mode */
Mario Sixd1141c52018-01-26 14:43:42 +01001938 (info->chipwidth == FLASH_CFI_BY16 &&
Mario Sixc4b85c32018-01-26 14:43:46 +01001939 info->interface == FLASH_CFI_X16X32)) {
Haavard Skinnemoend523e392007-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 Six88f439f2018-01-26 14:43:32 +01001952static int flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001953{
Mario Sixfa290692018-01-26 14:43:31 +01001954 debug("flash detect cfi\n");
wdenk2cefd152004-02-08 22:55:38 +00001955
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001956 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenke65527f2004-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 Skinnemoend523e392007-12-13 12:56:28 +01001960 info->chipwidth <<= 1)
Stefan Roese70a90b72013-04-12 19:04:54 +02001961 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoend523e392007-12-13 12:56:28 +01001962 return 1;
wdenk2cefd152004-02-08 22:55:38 +00001963 }
Mario Sixfa290692018-01-26 14:43:31 +01001964 debug("not found\n");
wdenk2cefd152004-02-08 22:55:38 +00001965 return 0;
1966}
wdenke65527f2004-02-12 00:47:09 +00001967
wdenk2cefd152004-02-08 22:55:38 +00001968/*
Haavard Skinnemoen750ea7f2007-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 Roese70a90b72013-04-12 19:04:54 +02001981 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
Haavard Skinnemoen750ea7f2007-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 Samuelsson07f9b4e2009-03-27 23:26:43 +01001999 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002000 */
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002001 if (info->device_id == 0xd6 || info->device_id == 0xd2)
2002 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002003
2004 if (reverse_geometry)
2005 cfi_reverse_geometry(qry);
2006}
2007
Richard Retanubunbe4dca22009-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 Frysinger02a37862011-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 Retanubunbe4dca22009-01-14 08:44:26 -05002018 cfi_reverse_geometry(qry);
2019 }
Mike Frysinger97dd8992011-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 Retanubunbe4dca22009-01-14 08:44:26 -05002024 }
2025 }
2026}
2027
Angelo Dureghello7ba30282012-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 Sixa828c1e2018-01-26 14:43:36 +01002043 info->cmd_erase_sector = 0x50;
Angelo Dureghello7ba30282012-12-01 01:14:18 +01002044 }
2045}
2046
Jagannadha Sutradharudu Teki130def32013-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 Sixd1141c52018-01-26 14:43:42 +01002055 if (qry->max_buf_write_size > 0x8 &&
Mario Sixdde85502018-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 Six246e5062018-01-26 14:43:50 +01002061 debug("Adjusted buffer size on Numonyx flash");
2062 debug(" M29EW family in 8 bit mode\n");
Jagannadha Sutradharudu Teki130def32013-03-01 16:54:26 +05302063 qry->max_buf_write_size = 0x8;
2064 }
2065}
2066
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002067/*
wdenk2cefd152004-02-08 22:55:38 +00002068 * The following code cannot be run from FLASH!
2069 *
2070 */
Mario Sixfa290692018-01-26 14:43:31 +01002071ulong flash_get_size(phys_addr_t base, int banknum)
wdenk2cefd152004-02-08 22:55:38 +00002072{
wdenke65527f2004-02-12 00:47:09 +00002073 flash_info_t *info = &flash_info[banknum];
wdenk2cefd152004-02-08 22:55:38 +00002074 int i, j;
2075 flash_sect_t sect_cnt;
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002076 phys_addr_t sector;
wdenk2cefd152004-02-08 22:55:38 +00002077 unsigned long tmp;
2078 int size_ratio;
2079 uchar num_erase_regions;
wdenke65527f2004-02-12 00:47:09 +00002080 int erase_region_size;
2081 int erase_region_count;
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002082 struct cfi_qry qry;
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002083 unsigned long max_size;
Stefan Roese12797482006-11-13 13:55:24 +01002084
Kumar Gala899032b2008-05-15 15:13:08 -05002085 memset(&qry, 0, sizeof(qry));
2086
Stefan Roese12797482006-11-13 13:55:24 +01002087 info->ext_addr = 0;
2088 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002089#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseefef95b2006-04-01 13:41:03 +02002090 info->legacy_unlock = 0;
2091#endif
wdenk2cefd152004-02-08 22:55:38 +00002092
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002093 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00002094
Mario Sixfa290692018-01-26 14:43:31 +01002095 if (flash_detect_cfi(info, &qry)) {
Mario Sixd1141c52018-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 Skinnemoen53baec72007-12-14 15:36:16 +01002098 num_erase_regions = qry.num_erase_regions;
2099
Stefan Roese12797482006-11-13 13:55:24 +01002100 if (info->ext_addr) {
Mario Sixa828c1e2018-01-26 14:43:36 +01002101 info->cfi_version = (ushort)flash_read_uchar(info,
Stefan Roese70a90b72013-04-12 19:04:54 +02002102 info->ext_addr + 3) << 8;
Mario Sixa828c1e2018-01-26 14:43:36 +01002103 info->cfi_version |= (ushort)flash_read_uchar(info,
Stefan Roese70a90b72013-04-12 19:04:54 +02002104 info->ext_addr + 4);
Stefan Roese12797482006-11-13 13:55:24 +01002105 }
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002106
wdenke65527f2004-02-12 00:47:09 +00002107#ifdef DEBUG
Mario Sixfa290692018-01-26 14:43:31 +01002108 flash_printqry(&qry);
wdenke65527f2004-02-12 00:47:09 +00002109#endif
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002110
wdenke65527f2004-02-12 00:47:09 +00002111 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04002112 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk2cefd152004-02-08 22:55:38 +00002113 case CFI_CMDSET_INTEL_STANDARD:
2114 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002115 cmdset_intel_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00002116 break;
2117 case CFI_CMDSET_AMD_STANDARD:
2118 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002119 cmdset_amd_init(info, &qry);
wdenk2cefd152004-02-08 22:55:38 +00002120 break;
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002121 default:
2122 printf("CFI: Unknown command set 0x%x\n",
Mario Sixdde85502018-01-26 14:43:55 +01002123 info->vendor);
Haavard Skinnemoenc4d478b2007-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;
wdenk2cefd152004-02-08 22:55:38 +00002131 }
wdenk6cfa84e2004-02-10 00:03:41 +00002132
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002133 /* Do manufacturer-specific fixups */
2134 switch (info->manufacturer_id) {
Mario Schuknecht5c3579e2011-02-21 13:13:14 +01002135 case 0x0001: /* AMD */
2136 case 0x0037: /* AMIC */
Haavard Skinnemoen750ea7f2007-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 Retanubunbe4dca22009-01-14 08:44:26 -05002142 case 0x0020:
2143 flash_fixup_stm(info, &qry);
2144 break;
Angelo Dureghello7ba30282012-12-01 01:14:18 +01002145 case 0x00bf: /* SST */
2146 flash_fixup_sst(info, &qry);
2147 break;
Jagannadha Sutradharudu Teki130def32013-03-01 16:54:26 +05302148 case 0x0089: /* Numonyx */
2149 flash_fixup_num(info, &qry);
2150 break;
Haavard Skinnemoen750ea7f2007-12-14 15:36:18 +01002151 }
2152
Mario Sixfa290692018-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 Roese12797482006-11-13 13:55:24 +01002158
wdenk2cefd152004-02-08 22:55:38 +00002159 size_ratio = info->portwidth / info->chipwidth;
wdenke65527f2004-02-12 00:47:09 +00002160 /* if the chip is x8/x16 reduce the ratio by half */
Mario Sixd1141c52018-01-26 14:43:42 +01002161 if (info->interface == FLASH_CFI_X8X16 &&
Mario Sixdde85502018-01-26 14:43:55 +01002162 info->chipwidth == FLASH_CFI_BY8) {
wdenke65527f2004-02-12 00:47:09 +00002163 size_ratio >>= 1;
2164 }
Mario Sixfa290692018-01-26 14:43:31 +01002165 debug("size_ratio %d port %d bits chip %d bits\n",
Mario Sixdde85502018-01-26 14:43:55 +01002166 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2167 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanok755c1802010-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 Gustschin6acb77b2010-11-28 02:13:33 +01002171 max_size = cfi_flash_bank_size(banknum);
Mario Sixd1141c52018-01-26 14:43:42 +01002172 if (max_size && info->size > max_size) {
Ilya Yanok755c1802010-10-21 17:20:12 +02002173 debug("[truncated from %ldMiB]", info->size >> 20);
2174 info->size = max_size;
2175 }
Mario Sixfa290692018-01-26 14:43:31 +01002176 debug("found %d erase regions\n", num_erase_regions);
wdenk2cefd152004-02-08 22:55:38 +00002177 sect_cnt = 0;
2178 sector = base;
wdenke65527f2004-02-12 00:47:09 +00002179 for (i = 0; i < num_erase_regions; i++) {
2180 if (i > NUM_ERASE_REGIONS) {
Mario Sixfa290692018-01-26 14:43:31 +01002181 printf("%d erase regions found, only %d used\n",
Mario Sixdde85502018-01-26 14:43:55 +01002182 num_erase_regions, NUM_ERASE_REGIONS);
wdenk2cefd152004-02-08 22:55:38 +00002183 break;
2184 }
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002185
Andrew Gabbasovc1592582013-05-14 12:27:52 -05002186 tmp = le32_to_cpu(get_unaligned(
Mario Sixd1141c52018-01-26 14:43:42 +01002187 &qry.erase_region_info[i]));
Haavard Skinnemoenc4d478b2007-12-14 15:36:17 +01002188 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002189
2190 erase_region_count = (tmp & 0xffff) + 1;
2191 tmp >>= 16;
wdenke65527f2004-02-12 00:47:09 +00002192 erase_region_size =
2193 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
Mario Sixbc762c12018-01-26 14:43:54 +01002194 debug("erase_region_count = %d ", erase_region_count);
2195 debug("erase_region_size = %d\n", erase_region_size);
wdenke65527f2004-02-12 00:47:09 +00002196 for (j = 0; j < erase_region_count; j++) {
Ilya Yanok755c1802010-10-21 17:20:12 +02002197 if (sector - base >= info->size)
2198 break;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002199 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen73d044d2007-12-07 23:35:02 +01002200 printf("ERROR: too many flash sectors\n");
2201 break;
2202 }
Becky Bruce9d1f6af2009-02-02 16:34:51 -06002203 info->start[sect_cnt] =
2204 (ulong)map_physmem(sector,
2205 info->portwidth,
2206 MAP_NOCACHE);
wdenk2cefd152004-02-08 22:55:38 +00002207 sector += (erase_region_size * size_ratio);
wdenk26c58432005-01-09 17:12:27 +00002208
2209 /*
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002210 * Only read protection status from
2211 * supported devices (intel...)
wdenk26c58432005-01-09 17:12:27 +00002212 */
2213 switch (info->vendor) {
Vasiliy Leoenenko7d1794c2008-05-07 21:25:33 +04002214 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk26c58432005-01-09 17:12:27 +00002215 case CFI_CMDSET_INTEL_EXTENDED:
2216 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roese5215df12010-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);
wdenk26c58432005-01-09 17:12:27 +00002224 info->protect[sect_cnt] =
Mario Sixfa290692018-01-26 14:43:31 +01002225 flash_isset(info, sect_cnt,
Mario Sixdde85502018-01-26 14:43:55 +01002226 FLASH_OFFSET_PROTECT,
2227 FLASH_STATUS_PROTECT);
Vasily Khoruzhickcf464002016-03-20 18:37:10 -07002228 flash_write_cmd(info, sect_cnt, 0,
2229 FLASH_CMD_RESET);
wdenk26c58432005-01-09 17:12:27 +00002230 break;
Stefan Roesebcb33442012-12-06 15:44:10 +01002231 case CFI_CMDSET_AMD_EXTENDED:
2232 case CFI_CMDSET_AMD_STANDARD:
Stefan Roese7de65842012-12-06 15:44:11 +01002233 if (!info->legacy_unlock) {
Stefan Roesebcb33442012-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;
wdenk26c58432005-01-09 17:12:27 +00002252 default:
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002253 /* default: not protected */
2254 info->protect[sect_cnt] = 0;
wdenk26c58432005-01-09 17:12:27 +00002255 }
2256
wdenk2cefd152004-02-08 22:55:38 +00002257 sect_cnt++;
2258 }
2259 }
2260
2261 info->sector_count = sect_cnt;
Haavard Skinnemoen53baec72007-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 Skinnemoend523e392007-12-13 12:56:28 +01002264 info->erase_blk_tout = tmp *
Haavard Skinnemoen53baec72007-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 Skinnemoend523e392007-12-13 12:56:28 +01002269 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-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 Skinnemoend523e392007-12-13 12:56:28 +01002273 /* round up when converting to ms */
Haavard Skinnemoen53baec72007-12-14 15:36:16 +01002274 info->write_tout = (tmp + 999) / 1000;
wdenk2cefd152004-02-08 22:55:38 +00002275 info->flash_id = FLASH_MAN_CFI;
Mario Sixd1141c52018-01-26 14:43:42 +01002276 if (info->interface == FLASH_CFI_X8X16 &&
2277 info->chipwidth == FLASH_CFI_BY8) {
Haavard Skinnemoend523e392007-12-13 12:56:28 +01002278 /* XXX - Need to test on x8/x16 in parallel. */
2279 info->portwidth >>= 1;
wdenked2ac4b2004-03-14 18:23:55 +00002280 }
Mike Frysinger59404ee2008-10-02 01:55:38 -04002281
Mario Sixfa290692018-01-26 14:43:31 +01002282 flash_write_cmd(info, 0, 0, info->cmd_reset);
wdenk2cefd152004-02-08 22:55:38 +00002283 }
2284
wdenke65527f2004-02-12 00:47:09 +00002285 return (info->size);
wdenk2cefd152004-02-08 22:55:38 +00002286}
2287
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002288#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002289void flash_set_verbose(uint v)
2290{
2291 flash_verbose = v;
2292}
Mike Frysingerc2c093d2010-12-22 09:41:13 -05002293#endif
Piotr Ziecik2a7493c2008-11-17 15:49:32 +01002294
Stefan Roeseab935642010-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
wdenk2cefd152004-02-08 22:55:38 +00002320/*-----------------------------------------------------------------------
2321 */
Heiko Schocheref0946a2011-04-04 08:10:21 +02002322
Marek Vasuta26162d2017-08-20 17:20:00 +02002323static void flash_protect_default(void)
Heiko Schocheref0946a2011-04-04 08:10:21 +02002324{
Peter Tyser4f3c60d2011-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 Schocheref0946a2011-04-04 08:10:21 +02002333 /* Monitor protection ON by default */
Vignesh Raghavendra5b9d8002019-10-23 13:30:00 +05302334#if defined(CONFIG_SYS_MONITOR_BASE) && \
2335 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
Heiko Schocheref0946a2011-04-04 08:10:21 +02002336 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2337 flash_protect(FLAG_PROTECT_SET,
Mario Sixdde85502018-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 Schocheref0946a2011-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 Sixdde85502018-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 Schocheref0946a2011-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 Sixdde85502018-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 Schocheref0946a2011-04-04 08:10:21 +02002357#endif
2358
2359#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
Axel Lin85706c82013-06-23 00:56:46 +08002360 for (i = 0; i < ARRAY_SIZE(apl); i++) {
Marek Vasutcb1622e2011-10-21 14:17:05 +00002361 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocheref0946a2011-04-04 08:10:21 +02002362 apl[i].start, apl[i].start + apl[i].size - 1);
2363 flash_protect(FLAG_PROTECT_SET,
Mario Sixdde85502018-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 Schocheref0946a2011-04-04 08:10:21 +02002367 }
2368#endif
2369}
2370
Mario Sixfa290692018-01-26 14:43:31 +01002371unsigned long flash_init(void)
wdenk2cefd152004-02-08 22:55:38 +00002372{
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002373 unsigned long size = 0;
2374 int i;
wdenk2cefd152004-02-08 22:55:38 +00002375
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002376#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann8f7ee7d2009-03-21 09:59:34 -04002377 /* read environment from EEPROM */
2378 char s[64];
Mario Sixc7e359e2018-01-26 14:43:37 +01002379
Simon Glass64b723f2017-08-03 12:22:12 -06002380 env_get_f("unlock", s, sizeof(s));
Michael Schwingen73d044d2007-12-07 23:35:02 +01002381#endif
wdenk2cefd152004-02-08 22:55:38 +00002382
Thomas Chou47eae232015-11-07 14:31:08 +08002383#ifdef CONFIG_CFI_FLASH /* for driver model */
2384 cfi_flash_init_dm();
2385#endif
2386
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002387 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002388 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002389 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk2cefd152004-02-08 22:55:38 +00002390
Stefan Roeseab935642010-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 Roese7e7dda82010-08-30 10:11:51 +02002395 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin6acb77b2010-11-28 02:13:33 +01002396 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002397 size += flash_info[i].size;
2398 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002399#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Mario Six246e5062018-01-26 14:43:50 +01002400 printf("## Unknown flash on Bank %d ", i + 1);
2401 printf("- Size = 0x%08lx = %ld MB\n",
Mario Sixdde85502018-01-26 14:43:55 +01002402 flash_info[i].size,
2403 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002404#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002405 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002406#ifdef CONFIG_SYS_FLASH_PROTECTION
Jeroen Hofstee5a85e892014-06-17 22:47:31 +02002407 else if (strcmp(s, "yes") == 0) {
Haavard Skinnemoen6dd763c2007-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-VILLARD03836942008-10-16 15:01:15 +02002412 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoen6dd763c2007-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;
wdenk2cefd152004-02-08 22:55:38 +00002418
Haavard Skinnemoen6dd763c2007-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;
wdenk2cefd152004-02-08 22:55:38 +00002426
Haavard Skinnemoen6dd763c2007-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 Sixfa290692018-01-26 14:43:31 +01002432 flash_real_protect(&flash_info[i], 0, 0);
wdenk2cefd152004-02-08 22:55:38 +00002433
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002434 flash_info[i].legacy_unlock = 1;
wdenk2cefd152004-02-08 22:55:38 +00002435
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002436 /*
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 Sixfa290692018-01-26 14:43:31 +01002446 flash_protect(FLAG_PROTECT_CLEAR,
Mario Sixdde85502018-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]);
Stefan Roesec865e6c2006-02-28 15:29:58 +01002451 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002452 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002453#endif /* CONFIG_SYS_FLASH_PROTECTION */
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002454 }
Stefan Roesec865e6c2006-02-28 15:29:58 +01002455
Heiko Schocheref0946a2011-04-04 08:10:21 +02002456 flash_protect_default();
Piotr Ziecik3e939e92008-11-17 15:57:58 +01002457#ifdef CONFIG_FLASH_CFI_MTD
2458 cfi_mtd_init();
2459#endif
2460
Haavard Skinnemoen6dd763c2007-10-06 18:55:36 +02002461 return (size);
wdenk2cefd152004-02-08 22:55:38 +00002462}
Thomas Chou47eae232015-11-07 14:31:08 +08002463
2464#ifdef CONFIG_CFI_FLASH /* for driver model */
2465static int cfi_flash_probe(struct udevice *dev)
2466{
Thomas Chou47eae232015-11-07 14:31:08 +08002467 const fdt32_t *cell;
Mario Six4d5b82d2018-03-28 14:38:41 +02002468 int addrc, sizec;
Thomas Chou47eae232015-11-07 14:31:08 +08002469 int len, idx;
2470
Mario Six4d5b82d2018-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 Chou47eae232015-11-07 14:31:08 +08002476 if (!cell)
2477 return -ENOENT;
2478 idx = 0;
2479 len /= sizeof(fdt32_t);
2480 while (idx < len) {
Mario Six4d5b82d2018-03-28 14:38:41 +02002481 phys_addr_t addr;
2482
2483 addr = dev_translate_address(dev, cell + idx);
2484
Marek Vasut970940f2017-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 Six4d5b82d2018-03-28 14:38:41 +02002488
Thomas Chou47eae232015-11-07 14:31:08 +08002489 idx += addrc + sizec;
2490 }
Marek Vasut970940f2017-09-12 19:09:08 +02002491 gd->bd->bi_flashstart = flash_info[0].base;
Thomas Chou47eae232015-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 */