blob: 16cdfe3978adfa8bc361a94c617d8394379d37f2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5653fc32004-02-08 22:55:38 +00002/*
wdenkbf9e3b32004-02-12 00:47:09 +00003 * (C) Copyright 2002-2004
wdenk5653fc32004-02-08 22:55:38 +00004 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
5 *
6 * Copyright (C) 2003 Arabella Software Ltd.
7 * Yuli Barcohen <yuli@arabellasw.com>
wdenk5653fc32004-02-08 22:55:38 +00008 *
wdenkbf9e3b32004-02-12 00:47:09 +00009 * Copyright (C) 2004
10 * Ed Okerson
Stefan Roese260421a2006-11-13 13:55:24 +010011 *
12 * Copyright (C) 2006
13 * Tolunay Orkun <listmember@orkun.us>
wdenk5653fc32004-02-08 22:55:38 +000014 */
15
16/* The DEBUG define must be before common to enable debugging */
wdenk2d1a5372004-02-23 19:30:57 +000017/* #define DEBUG */
18
wdenk5653fc32004-02-08 22:55:38 +000019#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -070020#include <console.h>
Thomas Chouf1056912015-11-07 14:31:08 +080021#include <dm.h>
Simon Glass3a7d5572019-08-01 09:46:42 -060022#include <env.h>
Thomas Chouf1056912015-11-07 14:31:08 +080023#include <errno.h>
24#include <fdt_support.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060025#include <flash.h>
Simon Glass691d7192020-05-10 11:40:02 -060026#include <init.h>
Simon Glassc30b7ad2019-11-14 12:57:41 -070027#include <irq_func.h>
wdenk5653fc32004-02-08 22:55:38 +000028#include <asm/processor.h>
Haiying Wang3a197b22007-02-21 16:52:31 +010029#include <asm/io.h>
wdenk4c0d4c32004-06-09 17:34:58 +000030#include <asm/byteorder.h>
Andrew Gabbasovaedadf12013-05-14 12:27:52 -050031#include <asm/unaligned.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060032#include <env_internal.h>
Stefan Roesefa36ae72009-10-27 15:15:55 +010033#include <mtd/cfi_flash.h>
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +010034#include <watchdog.h>
wdenk028ab6b2004-02-23 23:54:43 +000035
wdenk5653fc32004-02-08 22:55:38 +000036/*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010037 * This file implements a Common Flash Interface (CFI) driver for
38 * U-Boot.
39 *
40 * The width of the port and the width of the chips are determined at
41 * initialization. These widths are used to calculate the address for
42 * access CFI data structures.
wdenk5653fc32004-02-08 22:55:38 +000043 *
44 * References
45 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
46 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
47 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
48 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese260421a2006-11-13 13:55:24 +010049 * AMD CFI Specification, Release 2.0 December 1, 2001
50 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
51 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk5653fc32004-02-08 22:55:38 +000052 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020053 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocherd0b6e142007-01-19 18:05:26 +010054 * reading and writing ... (yes there is such a Hardware).
wdenk5653fc32004-02-08 22:55:38 +000055 */
56
Thomas Chouf1056912015-11-07 14:31:08 +080057DECLARE_GLOBAL_DATA_PTR;
58
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010059static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysinger4ffeab22010-12-22 09:41:13 -050060#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +010061static uint flash_verbose = 1;
Mike Frysinger4ffeab22010-12-22 09:41:13 -050062#else
63#define flash_verbose 1
64#endif
Wolfgang Denk92eb7292006-12-27 01:26:13 +010065
Wolfgang Denk2a112b22008-08-08 16:39:54 +020066flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
67
Stefan Roese79b4cda2006-02-28 15:29:58 +010068/*
69 * Check if chip width is defined. If not, start detecting with 8bit.
70 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020071#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
72#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roese79b4cda2006-02-28 15:29:58 +010073#endif
74
Jeroen Hofstee00dcb072014-10-08 22:57:23 +020075#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
76#define __maybe_weak __weak
77#else
78#define __maybe_weak static
79#endif
80
Stefan Roese6f726f92010-10-25 18:31:48 +020081/*
82 * 0xffff is an undefined value for the configuration register. When
83 * this value is returned, the configuration register shall not be
84 * written at all (default mode).
85 */
86static u16 cfi_flash_config_reg(int i)
87{
88#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
89 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
90#else
91 return 0xffff;
92#endif
93}
94
Stefan Roeseca5def32010-08-31 10:00:10 +020095#if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
96int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
Mario Sixd9a35692018-01-26 14:43:56 +010097#else
98int cfi_flash_num_flash_banks;
Stefan Roeseca5def32010-08-31 10:00:10 +020099#endif
100
Thomas Chouf1056912015-11-07 14:31:08 +0800101#ifdef CONFIG_CFI_FLASH /* for driver model */
102static void cfi_flash_init_dm(void)
103{
104 struct udevice *dev;
105
106 cfi_flash_num_flash_banks = 0;
107 /*
108 * The uclass_first_device() will probe the first device and
109 * uclass_next_device() will probe the rest if they exist. So
110 * that cfi_flash_probe() will get called assigning the base
111 * addresses that are available.
112 */
113 for (uclass_first_device(UCLASS_MTD, &dev);
114 dev;
115 uclass_next_device(&dev)) {
116 }
117}
118
Thomas Chouf1056912015-11-07 14:31:08 +0800119phys_addr_t cfi_flash_bank_addr(int i)
120{
Marek Vasut1ec0a372017-09-12 19:09:08 +0200121 return flash_info[i].base;
Thomas Chouf1056912015-11-07 14:31:08 +0800122}
123#else
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200124__weak phys_addr_t cfi_flash_bank_addr(int i)
Stefan Roeseb00e19c2010-08-30 10:11:51 +0200125{
126 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
127}
Thomas Chouf1056912015-11-07 14:31:08 +0800128#endif
Stefan Roeseb00e19c2010-08-30 10:11:51 +0200129
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200130__weak unsigned long cfi_flash_bank_size(int i)
Ilya Yanokec50a8e2010-10-21 17:20:12 +0200131{
132#ifdef CONFIG_SYS_FLASH_BANKS_SIZES
133 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
134#else
135 return 0;
136#endif
137}
Ilya Yanokec50a8e2010-10-21 17:20:12 +0200138
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200139__maybe_weak void flash_write8(u8 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100140{
141 __raw_writeb(value, addr);
142}
143
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200144__maybe_weak void flash_write16(u16 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100145{
146 __raw_writew(value, addr);
147}
148
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200149__maybe_weak void flash_write32(u32 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100150{
151 __raw_writel(value, addr);
152}
153
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200154__maybe_weak void flash_write64(u64 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100155{
156 /* No architectures currently implement __raw_writeq() */
157 *(volatile u64 *)addr = value;
158}
159
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200160__maybe_weak u8 flash_read8(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100161{
162 return __raw_readb(addr);
163}
164
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200165__maybe_weak u16 flash_read16(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100166{
167 return __raw_readw(addr);
168}
169
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200170__maybe_weak u32 flash_read32(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100171{
172 return __raw_readl(addr);
173}
174
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200175__maybe_weak u64 flash_read64(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100176{
177 /* No architectures currently implement __raw_readq() */
178 return *(volatile u64 *)addr;
179}
180
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200181/*-----------------------------------------------------------------------
182 */
Mario Sixddcf0542018-01-26 14:43:54 +0100183#if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || \
Vignesh Raghavendrad75eacf2019-10-23 13:30:00 +0530184 (defined(CONFIG_SYS_MONITOR_BASE) && \
185 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE))
Marek Vasut236c49a2017-08-20 17:20:00 +0200186static flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200187{
188 int i;
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900189 flash_info_t *info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200190
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200191 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Masahiro Yamadae2e273a2013-05-17 14:50:36 +0900192 info = &flash_info[i];
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200193 if (info->size && info->start[0] <= base &&
194 base <= info->start[0] + info->size - 1)
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900195 return info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200196 }
197
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900198 return NULL;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200199}
wdenk5653fc32004-02-08 22:55:38 +0000200#endif
201
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100202unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
203{
204 if (sect != (info->sector_count - 1))
205 return info->start[sect + 1] - info->start[sect];
206 else
207 return info->start[0] + info->size - info->start[sect];
208}
209
wdenk5653fc32004-02-08 22:55:38 +0000210/*-----------------------------------------------------------------------
211 * create an address based on the offset and the port width
212 */
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100213static inline void *
Mario Sixca2b07a2018-01-26 14:43:32 +0100214flash_map(flash_info_t *info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000215{
Stefan Roesee303be22013-04-12 19:04:54 +0200216 unsigned int byte_offset = offset * info->portwidth;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100217
Stefan Roesee303be22013-04-12 19:04:54 +0200218 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100219}
220
221static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
Mario Sixc0350fb2018-01-26 14:43:55 +0100222 unsigned int offset, void *addr)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100223{
wdenk5653fc32004-02-08 22:55:38 +0000224}
wdenkbf9e3b32004-02-12 00:47:09 +0000225
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200226/*-----------------------------------------------------------------------
227 * make a proper sized command based on the port and chip widths
228 */
Sebastian Siewior7288f972008-07-15 13:35:23 +0200229static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200230{
231 int i;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400232 int cword_offset;
233 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200234#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewior340ccb22008-07-16 20:04:49 +0200235 u32 cmd_le = cpu_to_le32(cmd);
236#endif
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400237 uchar val;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200238 uchar *cp = (uchar *) cmdbuf;
239
Mario Sixb1683862018-01-26 14:43:33 +0100240 for (i = info->portwidth; i > 0; i--) {
Mario Six640f4e32018-01-26 14:43:36 +0100241 cword_offset = (info->portwidth - i) % info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200242#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400243 cp_offset = info->portwidth - i;
Mario Sixdb91bb22018-01-26 14:43:34 +0100244 val = *((uchar *)&cmd_le + cword_offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200245#else
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400246 cp_offset = i - 1;
Mario Sixdb91bb22018-01-26 14:43:34 +0100247 val = *((uchar *)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200248#endif
Sebastian Siewior7288f972008-07-15 13:35:23 +0200249 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400250 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200251}
252
wdenkbf9e3b32004-02-12 00:47:09 +0000253#ifdef DEBUG
254/*-----------------------------------------------------------------------
255 * Debug support
256 */
Mario Six188a5562018-01-26 14:43:31 +0100257static void print_longlong(char *str, unsigned long long data)
wdenkbf9e3b32004-02-12 00:47:09 +0000258{
259 int i;
260 char *cp;
261
Mario Six640f4e32018-01-26 14:43:36 +0100262 cp = (char *)&data;
wdenkbf9e3b32004-02-12 00:47:09 +0000263 for (i = 0; i < 8; i++)
Mario Six188a5562018-01-26 14:43:31 +0100264 sprintf(&str[i * 2], "%2.2x", *cp++);
wdenkbf9e3b32004-02-12 00:47:09 +0000265}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200266
Mario Six188a5562018-01-26 14:43:31 +0100267static void flash_printqry(struct cfi_qry *qry)
wdenkbf9e3b32004-02-12 00:47:09 +0000268{
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100269 u8 *p = (u8 *)qry;
wdenkbf9e3b32004-02-12 00:47:09 +0000270 int x, y;
271
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100272 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
273 debug("%02x : ", x);
274 for (y = 0; y < 16; y++)
275 debug("%2.2x ", p[x + y]);
276 debug(" ");
wdenkbf9e3b32004-02-12 00:47:09 +0000277 for (y = 0; y < 16; y++) {
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100278 unsigned char c = p[x + y];
Mario Six7223a8c2018-01-26 14:43:37 +0100279
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100280 if (c >= 0x20 && c <= 0x7e)
281 debug("%c", c);
282 else
283 debug(".");
wdenkbf9e3b32004-02-12 00:47:09 +0000284 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100285 debug("\n");
wdenkbf9e3b32004-02-12 00:47:09 +0000286 }
287}
wdenkbf9e3b32004-02-12 00:47:09 +0000288#endif
289
wdenk5653fc32004-02-08 22:55:38 +0000290/*-----------------------------------------------------------------------
291 * read a character at a port width address
292 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100293static inline uchar flash_read_uchar(flash_info_t *info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000294{
295 uchar *cp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100296 uchar retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000297
Mario Six188a5562018-01-26 14:43:31 +0100298 cp = flash_map(info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200299#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100300 retval = flash_read8(cp);
wdenkbf9e3b32004-02-12 00:47:09 +0000301#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100302 retval = flash_read8(cp + info->portwidth - 1);
wdenkbf9e3b32004-02-12 00:47:09 +0000303#endif
Mario Six188a5562018-01-26 14:43:31 +0100304 flash_unmap(info, 0, offset, cp);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100305 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000306}
307
308/*-----------------------------------------------------------------------
Tor Krill90447ec2008-03-28 11:29:10 +0100309 * read a word at a port width address, assume 16bit bus
310 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100311static inline ushort flash_read_word(flash_info_t *info, uint offset)
Tor Krill90447ec2008-03-28 11:29:10 +0100312{
313 ushort *addr, retval;
314
Mario Six188a5562018-01-26 14:43:31 +0100315 addr = flash_map(info, 0, offset);
316 retval = flash_read16(addr);
317 flash_unmap(info, 0, offset, addr);
Tor Krill90447ec2008-03-28 11:29:10 +0100318 return retval;
319}
320
Tor Krill90447ec2008-03-28 11:29:10 +0100321/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +0100322 * read a long word by picking the least significant byte of each maximum
wdenk5653fc32004-02-08 22:55:38 +0000323 * port size word. Swap for ppc format.
324 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100325static ulong flash_read_long (flash_info_t *info, flash_sect_t sect,
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100326 uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000327{
wdenkbf9e3b32004-02-12 00:47:09 +0000328 uchar *addr;
329 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000330
wdenkbf9e3b32004-02-12 00:47:09 +0000331#ifdef DEBUG
332 int x;
333#endif
Mario Six188a5562018-01-26 14:43:31 +0100334 addr = flash_map(info, sect, offset);
wdenkbf9e3b32004-02-12 00:47:09 +0000335
336#ifdef DEBUG
Mario Six188a5562018-01-26 14:43:31 +0100337 debug("long addr is at %p info->portwidth = %d\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100338 info->portwidth);
Mario Six0412e902018-01-26 14:43:38 +0100339 for (x = 0; x < 4 * info->portwidth; x++)
Mario Six188a5562018-01-26 14:43:31 +0100340 debug("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenkbf9e3b32004-02-12 00:47:09 +0000341#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200342#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100343 retval = ((flash_read8(addr) << 16) |
344 (flash_read8(addr + info->portwidth) << 24) |
345 (flash_read8(addr + 2 * info->portwidth)) |
346 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenkbf9e3b32004-02-12 00:47:09 +0000347#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100348 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
349 (flash_read8(addr + info->portwidth - 1) << 16) |
350 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
351 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenkbf9e3b32004-02-12 00:47:09 +0000352#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100353 flash_unmap(info, sect, offset, addr);
354
wdenkbf9e3b32004-02-12 00:47:09 +0000355 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000356}
357
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200358/*
359 * Write a proper sized command to the correct address
360 */
Marek Vasut236c49a2017-08-20 17:20:00 +0200361static void flash_write_cmd(flash_info_t *info, flash_sect_t sect,
362 uint offset, u32 cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200363{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100364 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200365 cfiword_t cword;
366
Mario Six188a5562018-01-26 14:43:31 +0100367 addr = flash_map(info, sect, offset);
368 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200369 switch (info->portwidth) {
370 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +0100371 debug("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Mario Sixc0350fb2018-01-26 14:43:55 +0100372 cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100373 flash_write8(cword.w8, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200374 break;
375 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +0100376 debug("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100377 cmd, cword.w16,
378 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100379 flash_write16(cword.w16, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200380 break;
381 case FLASH_CFI_32BIT:
Mario Six188a5562018-01-26 14:43:31 +0100382 debug("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100383 cmd, cword.w32,
384 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100385 flash_write32(cword.w32, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200386 break;
387 case FLASH_CFI_64BIT:
388#ifdef DEBUG
389 {
390 char str[20];
391
Mario Six188a5562018-01-26 14:43:31 +0100392 print_longlong(str, cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200393
Mario Six188a5562018-01-26 14:43:31 +0100394 debug("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Mario Sixc0350fb2018-01-26 14:43:55 +0100395 addr, cmd, str,
396 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200397 }
398#endif
Ryan Harkin622b9522015-10-23 16:50:51 +0100399 flash_write64(cword.w64, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200400 break;
401 }
402
403 /* Ensure all the instructions are fully finished */
404 sync();
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100405
406 flash_unmap(info, sect, offset, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200407}
408
Mario Sixca2b07a2018-01-26 14:43:32 +0100409static void flash_unlock_seq(flash_info_t *info, flash_sect_t sect)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200410{
Mario Six188a5562018-01-26 14:43:31 +0100411 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
412 flash_write_cmd(info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200413}
414
415/*-----------------------------------------------------------------------
416 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100417static int flash_isequal(flash_info_t *info, flash_sect_t sect, uint offset,
418 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200419{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100420 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200421 cfiword_t cword;
422 int retval;
423
Mario Six188a5562018-01-26 14:43:31 +0100424 addr = flash_map(info, sect, offset);
425 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200426
Mario Six188a5562018-01-26 14:43:31 +0100427 debug("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200428 switch (info->portwidth) {
429 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +0100430 debug("is= %x %x\n", flash_read8(addr), cword.w8);
Ryan Harkin622b9522015-10-23 16:50:51 +0100431 retval = (flash_read8(addr) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200432 break;
433 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +0100434 debug("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16);
Ryan Harkin622b9522015-10-23 16:50:51 +0100435 retval = (flash_read16(addr) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200436 break;
437 case FLASH_CFI_32BIT:
Mario Six188a5562018-01-26 14:43:31 +0100438 debug("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32);
Ryan Harkin622b9522015-10-23 16:50:51 +0100439 retval = (flash_read32(addr) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200440 break;
441 case FLASH_CFI_64BIT:
442#ifdef DEBUG
443 {
444 char str1[20];
445 char str2[20];
446
Mario Six188a5562018-01-26 14:43:31 +0100447 print_longlong(str1, flash_read64(addr));
448 print_longlong(str2, cword.w64);
449 debug("is= %s %s\n", str1, str2);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200450 }
451#endif
Ryan Harkin622b9522015-10-23 16:50:51 +0100452 retval = (flash_read64(addr) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200453 break;
454 default:
455 retval = 0;
456 break;
457 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100458 flash_unmap(info, sect, offset, addr);
459
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200460 return retval;
461}
462
463/*-----------------------------------------------------------------------
464 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100465static int flash_isset(flash_info_t *info, flash_sect_t sect, uint offset,
466 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200467{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100468 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200469 cfiword_t cword;
470 int retval;
471
Mario Six188a5562018-01-26 14:43:31 +0100472 addr = flash_map(info, sect, offset);
473 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200474 switch (info->portwidth) {
475 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100476 retval = ((flash_read8(addr) & cword.w8) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200477 break;
478 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100479 retval = ((flash_read16(addr) & cword.w16) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200480 break;
481 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100482 retval = ((flash_read32(addr) & cword.w32) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200483 break;
484 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100485 retval = ((flash_read64(addr) & cword.w64) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200486 break;
487 default:
488 retval = 0;
489 break;
490 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100491 flash_unmap(info, sect, offset, addr);
492
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200493 return retval;
494}
495
496/*-----------------------------------------------------------------------
497 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100498static int flash_toggle(flash_info_t *info, flash_sect_t sect, uint offset,
499 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200500{
Mario Six53128382018-01-26 14:43:49 +0100501 u8 *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200502 cfiword_t cword;
503 int retval;
504
Mario Six188a5562018-01-26 14:43:31 +0100505 addr = flash_map(info, sect, offset);
506 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200507 switch (info->portwidth) {
508 case FLASH_CFI_8BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200509 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200510 break;
511 case FLASH_CFI_16BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200512 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200513 break;
514 case FLASH_CFI_32BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200515 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200516 break;
517 case FLASH_CFI_64BIT:
Mario Sixb1683862018-01-26 14:43:33 +0100518 retval = ((flash_read32(addr) != flash_read32(addr)) ||
Mario Six640f4e32018-01-26 14:43:36 +0100519 (flash_read32(addr + 4) != flash_read32(addr + 4)));
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200520 break;
521 default:
522 retval = 0;
523 break;
524 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100525 flash_unmap(info, sect, offset, addr);
526
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200527 return retval;
528}
529
530/*
531 * flash_is_busy - check to see if the flash is busy
532 *
533 * This routine checks the status of the chip and returns true if the
534 * chip is busy.
535 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100536static int flash_is_busy(flash_info_t *info, flash_sect_t sect)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200537{
538 int retval;
539
540 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400541 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200542 case CFI_CMDSET_INTEL_STANDARD:
543 case CFI_CMDSET_INTEL_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +0100544 retval = !flash_isset(info, sect, 0, FLASH_STATUS_DONE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200545 break;
546 case CFI_CMDSET_AMD_STANDARD:
547 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100548#ifdef CONFIG_FLASH_CFI_LEGACY
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200549 case CFI_CMDSET_AMD_LEGACY:
550#endif
Marek Vasut72443c72017-09-12 19:09:31 +0200551 if (info->sr_supported) {
Mario Six188a5562018-01-26 14:43:31 +0100552 flash_write_cmd(info, sect, info->addr_unlock1,
Mario Sixc0350fb2018-01-26 14:43:55 +0100553 FLASH_CMD_READ_STATUS);
Mario Six188a5562018-01-26 14:43:31 +0100554 retval = !flash_isset(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100555 FLASH_STATUS_DONE);
Marek Vasut72443c72017-09-12 19:09:31 +0200556 } else {
Mario Six188a5562018-01-26 14:43:31 +0100557 retval = flash_toggle(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100558 AMD_STATUS_TOGGLE);
Marek Vasut72443c72017-09-12 19:09:31 +0200559 }
560
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200561 break;
562 default:
563 retval = 0;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100564 }
Mario Six38d28312018-01-26 14:43:40 +0100565 debug("%s: %d\n", __func__, retval);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200566 return retval;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100567}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200568
569/*-----------------------------------------------------------------------
570 * wait for XSR.7 to be set. Time out with an error if it does not.
571 * This routine does not set the flash to read-array mode.
572 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100573static int flash_status_check(flash_info_t *info, flash_sect_t sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100574 ulong tout, char *prompt)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200575{
576 ulong start;
577
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200578#if CONFIG_SYS_HZ != 1000
Mario Sixddcf0542018-01-26 14:43:54 +0100579 /* Avoid overflow for large HZ */
Renato Andreolac40c94a2010-03-24 23:00:47 +0800580 if ((ulong)CONFIG_SYS_HZ > 100000)
Mario Sixddcf0542018-01-26 14:43:54 +0100581 tout *= (ulong)CONFIG_SYS_HZ / 1000;
Renato Andreolac40c94a2010-03-24 23:00:47 +0800582 else
583 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200584#endif
585
586 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000587#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800588 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000589#endif
Mario Six188a5562018-01-26 14:43:31 +0100590 start = get_timer(0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100591 WATCHDOG_RESET();
Mario Six188a5562018-01-26 14:43:31 +0100592 while (flash_is_busy(info, sector)) {
593 if (get_timer(start) > tout) {
594 printf("Flash %s timeout at address %lx data %lx\n",
Mario Sixc0350fb2018-01-26 14:43:55 +0100595 prompt, info->start[sector],
596 flash_read_long(info, sector, 0));
Mario Six188a5562018-01-26 14:43:31 +0100597 flash_write_cmd(info, sector, 0, info->cmd_reset);
Stefan Roesee303be22013-04-12 19:04:54 +0200598 udelay(1);
Mario Six9dbaebc2018-01-26 14:43:52 +0100599 return ERR_TIMEOUT;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200600 }
Mario Six188a5562018-01-26 14:43:31 +0100601 udelay(1); /* also triggers watchdog */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200602 }
603 return ERR_OK;
604}
605
606/*-----------------------------------------------------------------------
607 * Wait for XSR.7 to be set, if it times out print an error, otherwise
608 * do a full status check.
609 *
610 * This routine sets the flash to read-array mode.
611 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100612static int flash_full_status_check(flash_info_t *info, flash_sect_t sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100613 ulong tout, char *prompt)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200614{
615 int retcode;
616
Mario Six188a5562018-01-26 14:43:31 +0100617 retcode = flash_status_check(info, sector, tout, prompt);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200618 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400619 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200620 case CFI_CMDSET_INTEL_EXTENDED:
621 case CFI_CMDSET_INTEL_STANDARD:
Mario Six4f89da42018-01-26 14:43:42 +0100622 if (retcode == ERR_OK &&
Mario Sixc0350fb2018-01-26 14:43:55 +0100623 !flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200624 retcode = ERR_INVAL;
Mario Six188a5562018-01-26 14:43:31 +0100625 printf("Flash %s error at address %lx\n", prompt,
Mario Sixc0350fb2018-01-26 14:43:55 +0100626 info->start[sector]);
Mario Six188a5562018-01-26 14:43:31 +0100627 if (flash_isset(info, sector, 0, FLASH_STATUS_ECLBS |
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200628 FLASH_STATUS_PSLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100629 puts("Command Sequence Error.\n");
630 } else if (flash_isset(info, sector, 0,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200631 FLASH_STATUS_ECLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100632 puts("Block Erase Error.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200633 retcode = ERR_NOT_ERASED;
Mario Six188a5562018-01-26 14:43:31 +0100634 } else if (flash_isset(info, sector, 0,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200635 FLASH_STATUS_PSLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100636 puts("Locking Error\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200637 }
Mario Six188a5562018-01-26 14:43:31 +0100638 if (flash_isset(info, sector, 0, FLASH_STATUS_DPS)) {
639 puts("Block locked.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200640 retcode = ERR_PROTECTED;
641 }
Mario Six188a5562018-01-26 14:43:31 +0100642 if (flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
643 puts("Vpp Low Error.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200644 }
Mario Six188a5562018-01-26 14:43:31 +0100645 flash_write_cmd(info, sector, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -0700646 udelay(1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200647 break;
648 default:
649 break;
650 }
651 return retcode;
652}
653
Thomas Choue5720822010-03-26 08:17:00 +0800654static int use_flash_status_poll(flash_info_t *info)
655{
656#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
657 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
658 info->vendor == CFI_CMDSET_AMD_STANDARD)
659 return 1;
660#endif
661 return 0;
662}
663
664static int flash_status_poll(flash_info_t *info, void *src, void *dst,
665 ulong tout, char *prompt)
666{
667#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
668 ulong start;
669 int ready;
670
671#if CONFIG_SYS_HZ != 1000
Mario Sixddcf0542018-01-26 14:43:54 +0100672 /* Avoid overflow for large HZ */
Thomas Choue5720822010-03-26 08:17:00 +0800673 if ((ulong)CONFIG_SYS_HZ > 100000)
Mario Sixddcf0542018-01-26 14:43:54 +0100674 tout *= (ulong)CONFIG_SYS_HZ / 1000;
Thomas Choue5720822010-03-26 08:17:00 +0800675 else
676 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
677#endif
678
679 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000680#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800681 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000682#endif
Thomas Choue5720822010-03-26 08:17:00 +0800683 start = get_timer(0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100684 WATCHDOG_RESET();
Thomas Choue5720822010-03-26 08:17:00 +0800685 while (1) {
686 switch (info->portwidth) {
687 case FLASH_CFI_8BIT:
688 ready = flash_read8(dst) == flash_read8(src);
689 break;
690 case FLASH_CFI_16BIT:
691 ready = flash_read16(dst) == flash_read16(src);
692 break;
693 case FLASH_CFI_32BIT:
694 ready = flash_read32(dst) == flash_read32(src);
695 break;
696 case FLASH_CFI_64BIT:
697 ready = flash_read64(dst) == flash_read64(src);
698 break;
699 default:
700 ready = 0;
701 break;
702 }
703 if (ready)
704 break;
705 if (get_timer(start) > tout) {
706 printf("Flash %s timeout at address %lx data %lx\n",
707 prompt, (ulong)dst, (ulong)flash_read8(dst));
Mario Six9dbaebc2018-01-26 14:43:52 +0100708 return ERR_TIMEOUT;
Thomas Choue5720822010-03-26 08:17:00 +0800709 }
710 udelay(1); /* also triggers watchdog */
711 }
712#endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
713 return ERR_OK;
714}
715
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200716/*-----------------------------------------------------------------------
717 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100718static void flash_add_byte(flash_info_t *info, cfiword_t *cword, uchar c)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200719{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200720#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200721 unsigned short w;
722 unsigned int l;
723 unsigned long long ll;
724#endif
725
726 switch (info->portwidth) {
727 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100728 cword->w8 = c;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200729 break;
730 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200731#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200732 w = c;
733 w <<= 8;
Ryan Harkin622b9522015-10-23 16:50:51 +0100734 cword->w16 = (cword->w16 >> 8) | w;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100735#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100736 cword->w16 = (cword->w16 << 8) | c;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100737#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200738 break;
739 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200740#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200741 l = c;
742 l <<= 24;
Ryan Harkin622b9522015-10-23 16:50:51 +0100743 cword->w32 = (cword->w32 >> 8) | l;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200744#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100745 cword->w32 = (cword->w32 << 8) | c;
Stefan Roese2662b402006-04-01 13:41:03 +0200746#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200747 break;
748 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200749#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200750 ll = c;
751 ll <<= 56;
Ryan Harkin622b9522015-10-23 16:50:51 +0100752 cword->w64 = (cword->w64 >> 8) | ll;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200753#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100754 cword->w64 = (cword->w64 << 8) | c;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200755#endif
756 break;
wdenk5653fc32004-02-08 22:55:38 +0000757 }
wdenk5653fc32004-02-08 22:55:38 +0000758}
759
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100760/*
761 * Loop through the sector table starting from the previously found sector.
762 * Searches forwards or backwards, dependent on the passed address.
wdenk5653fc32004-02-08 22:55:38 +0000763 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100764static flash_sect_t find_sector(flash_info_t *info, ulong addr)
wdenk7680c142005-05-16 15:23:22 +0000765{
Kim Phillips11dc4012012-10-29 13:34:45 +0000766 static flash_sect_t saved_sector; /* previously found sector */
Stefan Roesee303be22013-04-12 19:04:54 +0200767 static flash_info_t *saved_info; /* previously used flash bank */
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100768 flash_sect_t sector = saved_sector;
wdenk7680c142005-05-16 15:23:22 +0000769
Mario Six4f89da42018-01-26 14:43:42 +0100770 if (info != saved_info || sector >= info->sector_count)
Stefan Roesee303be22013-04-12 19:04:54 +0200771 sector = 0;
772
Mario Six5701ba82018-01-26 14:43:53 +0100773 while ((sector < info->sector_count - 1) &&
Mario Sixc0350fb2018-01-26 14:43:55 +0100774 (info->start[sector] < addr))
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100775 sector++;
776 while ((info->start[sector] > addr) && (sector > 0))
777 /*
778 * also decrements the sector in case of an overshot
779 * in the first loop
780 */
781 sector--;
782
783 saved_sector = sector;
Stefan Roesee303be22013-04-12 19:04:54 +0200784 saved_info = info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200785 return sector;
wdenk7680c142005-05-16 15:23:22 +0000786}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200787
788/*-----------------------------------------------------------------------
789 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100790static int flash_write_cfiword(flash_info_t *info, ulong dest, cfiword_t cword)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200791{
Becky Bruce09ce9922009-02-02 16:34:51 -0600792 void *dstaddr = (void *)dest;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200793 int flag;
Jens Gehrleina7292872008-12-16 17:25:54 +0100794 flash_sect_t sect = 0;
795 char sect_found = 0;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200796
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200797 /* Check if Flash is (sufficiently) erased */
798 switch (info->portwidth) {
799 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100800 flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200801 break;
802 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100803 flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200804 break;
805 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100806 flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200807 break;
808 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100809 flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200810 break;
811 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100812 flag = 0;
813 break;
814 }
Becky Bruce09ce9922009-02-02 16:34:51 -0600815 if (!flag)
Stefan Roese0dc80e22007-12-27 07:50:54 +0100816 return ERR_NOT_ERASED;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200817
818 /* Disable interrupts which might cause a timeout here */
Mario Six188a5562018-01-26 14:43:31 +0100819 flag = disable_interrupts();
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200820
821 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400822 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200823 case CFI_CMDSET_INTEL_EXTENDED:
824 case CFI_CMDSET_INTEL_STANDARD:
Mario Six188a5562018-01-26 14:43:31 +0100825 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
826 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200827 break;
828 case CFI_CMDSET_AMD_EXTENDED:
829 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout0d01f662008-10-09 01:26:36 -0500830 sect = find_sector(info, dest);
Mario Six188a5562018-01-26 14:43:31 +0100831 flash_unlock_seq(info, sect);
832 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrleina7292872008-12-16 17:25:54 +0100833 sect_found = 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200834 break;
Po-Yu Chuangb4db4a72009-07-10 18:03:57 +0800835#ifdef CONFIG_FLASH_CFI_LEGACY
836 case CFI_CMDSET_AMD_LEGACY:
837 sect = find_sector(info, dest);
Mario Six188a5562018-01-26 14:43:31 +0100838 flash_unlock_seq(info, 0);
839 flash_write_cmd(info, 0, info->addr_unlock1, AMD_CMD_WRITE);
Po-Yu Chuangb4db4a72009-07-10 18:03:57 +0800840 sect_found = 1;
841 break;
842#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200843 }
844
845 switch (info->portwidth) {
846 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100847 flash_write8(cword.w8, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200848 break;
849 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100850 flash_write16(cword.w16, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200851 break;
852 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100853 flash_write32(cword.w32, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200854 break;
855 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100856 flash_write64(cword.w64, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200857 break;
858 }
859
860 /* re-enable interrupts if necessary */
861 if (flag)
Mario Six188a5562018-01-26 14:43:31 +0100862 enable_interrupts();
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200863
Jens Gehrleina7292872008-12-16 17:25:54 +0100864 if (!sect_found)
Mario Six188a5562018-01-26 14:43:31 +0100865 sect = find_sector(info, dest);
Jens Gehrleina7292872008-12-16 17:25:54 +0100866
Thomas Choue5720822010-03-26 08:17:00 +0800867 if (use_flash_status_poll(info))
868 return flash_status_poll(info, &cword, dstaddr,
869 info->write_tout, "write");
870 else
871 return flash_full_status_check(info, sect,
872 info->write_tout, "write");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200873}
874
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200875#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200876
Mario Sixca2b07a2018-01-26 14:43:32 +0100877static int flash_write_cfibuffer(flash_info_t *info, ulong dest, uchar *cp,
Mario Sixc0350fb2018-01-26 14:43:55 +0100878 int len)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200879{
880 flash_sect_t sector;
881 int cnt;
882 int retcode;
Mario Six53128382018-01-26 14:43:49 +0100883 u8 *src = cp;
884 u8 *dst = (u8 *)dest;
885 u8 *dst2 = dst;
Tao Hou85c344e2012-03-15 23:33:58 +0800886 int flag = 1;
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200887 uint offset = 0;
888 unsigned int shift;
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400889 uchar write_cmd;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100890
Stefan Roese0dc80e22007-12-27 07:50:54 +0100891 switch (info->portwidth) {
892 case FLASH_CFI_8BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200893 shift = 0;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100894 break;
895 case FLASH_CFI_16BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200896 shift = 1;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100897 break;
898 case FLASH_CFI_32BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200899 shift = 2;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100900 break;
901 case FLASH_CFI_64BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200902 shift = 3;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100903 break;
904 default:
905 retcode = ERR_INVAL;
906 goto out_unmap;
907 }
908
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200909 cnt = len >> shift;
910
Tao Hou85c344e2012-03-15 23:33:58 +0800911 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese0dc80e22007-12-27 07:50:54 +0100912 switch (info->portwidth) {
913 case FLASH_CFI_8BIT:
914 flag = ((flash_read8(dst2) & flash_read8(src)) ==
915 flash_read8(src));
916 src += 1, dst2 += 1;
917 break;
918 case FLASH_CFI_16BIT:
919 flag = ((flash_read16(dst2) & flash_read16(src)) ==
920 flash_read16(src));
921 src += 2, dst2 += 2;
922 break;
923 case FLASH_CFI_32BIT:
924 flag = ((flash_read32(dst2) & flash_read32(src)) ==
925 flash_read32(src));
926 src += 4, dst2 += 4;
927 break;
928 case FLASH_CFI_64BIT:
929 flag = ((flash_read64(dst2) & flash_read64(src)) ==
930 flash_read64(src));
931 src += 8, dst2 += 8;
932 break;
933 }
934 }
935 if (!flag) {
936 retcode = ERR_NOT_ERASED;
937 goto out_unmap;
938 }
939
940 src = cp;
Mario Six188a5562018-01-26 14:43:31 +0100941 sector = find_sector(info, dest);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200942
943 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400944 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200945 case CFI_CMDSET_INTEL_STANDARD:
946 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400947 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
Mario Sixddcf0542018-01-26 14:43:54 +0100948 FLASH_CMD_WRITE_BUFFER_PROG :
949 FLASH_CMD_WRITE_TO_BUFFER;
Mario Six188a5562018-01-26 14:43:31 +0100950 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
951 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
952 flash_write_cmd(info, sector, 0, write_cmd);
953 retcode = flash_status_check(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100954 info->buffer_write_tout,
955 "write to buffer");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200956 if (retcode == ERR_OK) {
957 /* reduce the number of loops by the width of
Mario Sixa6d18f22018-01-26 14:43:41 +0100958 * the port
959 */
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200960 cnt = len >> shift;
Mario Six188a5562018-01-26 14:43:31 +0100961 flash_write_cmd(info, sector, 0, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200962 while (cnt-- > 0) {
963 switch (info->portwidth) {
964 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100965 flash_write8(flash_read8(src), dst);
966 src += 1, dst += 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200967 break;
968 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100969 flash_write16(flash_read16(src), dst);
970 src += 2, dst += 2;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200971 break;
972 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100973 flash_write32(flash_read32(src), dst);
974 src += 4, dst += 4;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200975 break;
976 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100977 flash_write64(flash_read64(src), dst);
978 src += 8, dst += 8;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200979 break;
980 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100981 retcode = ERR_INVAL;
982 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200983 }
984 }
Mario Six188a5562018-01-26 14:43:31 +0100985 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100986 FLASH_CMD_WRITE_BUFFER_CONFIRM);
Mario Six188a5562018-01-26 14:43:31 +0100987 retcode = flash_full_status_check(
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200988 info, sector, info->buffer_write_tout,
989 "buffer write");
990 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100991
992 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200993
994 case CFI_CMDSET_AMD_STANDARD:
995 case CFI_CMDSET_AMD_EXTENDED:
Rouven Behr7570a0c2016-04-10 13:38:13 +0200996 flash_unlock_seq(info, sector);
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200997
998#ifdef CONFIG_FLASH_SPANSION_S29WS_N
999 offset = ((unsigned long)dst - info->start[sector]) >> shift;
1000#endif
1001 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
1002 cnt = len >> shift;
John Schmoller7dedefd2009-08-12 10:55:47 -05001003 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001004
1005 switch (info->portwidth) {
1006 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001007 while (cnt-- > 0) {
1008 flash_write8(flash_read8(src), dst);
1009 src += 1, dst += 1;
1010 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001011 break;
1012 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001013 while (cnt-- > 0) {
1014 flash_write16(flash_read16(src), dst);
1015 src += 2, dst += 2;
1016 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001017 break;
1018 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001019 while (cnt-- > 0) {
1020 flash_write32(flash_read32(src), dst);
1021 src += 4, dst += 4;
1022 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001023 break;
1024 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001025 while (cnt-- > 0) {
1026 flash_write64(flash_read64(src), dst);
1027 src += 8, dst += 8;
1028 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001029 break;
1030 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001031 retcode = ERR_INVAL;
1032 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001033 }
1034
Mario Six188a5562018-01-26 14:43:31 +01001035 flash_write_cmd(info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Choue5720822010-03-26 08:17:00 +08001036 if (use_flash_status_poll(info))
1037 retcode = flash_status_poll(info, src - (1 << shift),
1038 dst - (1 << shift),
1039 info->buffer_write_tout,
1040 "buffer write");
1041 else
1042 retcode = flash_full_status_check(info, sector,
1043 info->buffer_write_tout,
1044 "buffer write");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001045 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001046
1047 default:
Mario Six188a5562018-01-26 14:43:31 +01001048 debug("Unknown Command Set\n");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001049 retcode = ERR_INVAL;
1050 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001051 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001052
1053out_unmap:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001054 return retcode;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001055}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001056#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001057
wdenk7680c142005-05-16 15:23:22 +00001058/*-----------------------------------------------------------------------
1059 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001060int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +00001061{
1062 int rcode = 0;
1063 int prot;
1064 flash_sect_t sect;
Thomas Choue5720822010-03-26 08:17:00 +08001065 int st;
wdenk5653fc32004-02-08 22:55:38 +00001066
wdenkbf9e3b32004-02-12 00:47:09 +00001067 if (info->flash_id != FLASH_MAN_CFI) {
Mario Six188a5562018-01-26 14:43:31 +01001068 puts("Can't erase unknown flash type - aborted\n");
wdenk5653fc32004-02-08 22:55:38 +00001069 return 1;
1070 }
Mario Six4f89da42018-01-26 14:43:42 +01001071 if (s_first < 0 || s_first > s_last) {
Mario Six188a5562018-01-26 14:43:31 +01001072 puts("- no sectors to erase\n");
wdenk5653fc32004-02-08 22:55:38 +00001073 return 1;
1074 }
1075
1076 prot = 0;
Mario Six0412e902018-01-26 14:43:38 +01001077 for (sect = s_first; sect <= s_last; ++sect)
1078 if (info->protect[sect])
wdenk5653fc32004-02-08 22:55:38 +00001079 prot++;
wdenk5653fc32004-02-08 22:55:38 +00001080 if (prot) {
Mario Six188a5562018-01-26 14:43:31 +01001081 printf("- Warning: %d protected sectors will not be erased!\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001082 prot);
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001083 } else if (flash_verbose) {
Mario Six188a5562018-01-26 14:43:31 +01001084 putc('\n');
wdenk5653fc32004-02-08 22:55:38 +00001085 }
1086
wdenkbf9e3b32004-02-12 00:47:09 +00001087 for (sect = s_first; sect <= s_last; sect++) {
Joe Hershbergerde15a062012-08-17 15:36:41 -05001088 if (ctrlc()) {
1089 printf("\n");
1090 return 1;
1091 }
1092
wdenk5653fc32004-02-08 22:55:38 +00001093 if (info->protect[sect] == 0) { /* not protected */
Joe Hershberger6822a642012-08-17 15:36:40 -05001094#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1095 int k;
1096 int size;
1097 int erased;
1098 u32 *flash;
1099
1100 /*
1101 * Check if whole sector is erased
1102 */
1103 size = flash_sector_size(info, sect);
1104 erased = 1;
1105 flash = (u32 *)info->start[sect];
1106 /* divide by 4 for longword access */
1107 size = size >> 2;
1108 for (k = 0; k < size; k++) {
1109 if (flash_read32(flash++) != 0xffffffff) {
1110 erased = 0;
1111 break;
1112 }
1113 }
1114 if (erased) {
1115 if (flash_verbose)
1116 putc(',');
1117 continue;
1118 }
1119#endif
wdenkbf9e3b32004-02-12 00:47:09 +00001120 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001121 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00001122 case CFI_CMDSET_INTEL_STANDARD:
1123 case CFI_CMDSET_INTEL_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +01001124 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001125 FLASH_CMD_CLEAR_STATUS);
Mario Six188a5562018-01-26 14:43:31 +01001126 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001127 FLASH_CMD_BLOCK_ERASE);
Mario Six188a5562018-01-26 14:43:31 +01001128 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001129 FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +00001130 break;
1131 case CFI_CMDSET_AMD_STANDARD:
1132 case CFI_CMDSET_AMD_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +01001133 flash_unlock_seq(info, sect);
1134 flash_write_cmd(info, sect,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001135 info->addr_unlock1,
1136 AMD_CMD_ERASE_START);
Mario Six188a5562018-01-26 14:43:31 +01001137 flash_unlock_seq(info, sect);
1138 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001139 info->cmd_erase_sector);
wdenk5653fc32004-02-08 22:55:38 +00001140 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001141#ifdef CONFIG_FLASH_CFI_LEGACY
1142 case CFI_CMDSET_AMD_LEGACY:
Mario Six188a5562018-01-26 14:43:31 +01001143 flash_unlock_seq(info, 0);
1144 flash_write_cmd(info, 0, info->addr_unlock1,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001145 AMD_CMD_ERASE_START);
Mario Six188a5562018-01-26 14:43:31 +01001146 flash_unlock_seq(info, 0);
1147 flash_write_cmd(info, sect, 0,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001148 AMD_CMD_ERASE_SECTOR);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001149 break;
1150#endif
wdenk5653fc32004-02-08 22:55:38 +00001151 default:
Mario Six9f720212018-01-26 14:43:44 +01001152 debug("Unknown flash vendor %d\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001153 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +00001154 break;
1155 }
1156
Thomas Choue5720822010-03-26 08:17:00 +08001157 if (use_flash_status_poll(info)) {
Kim Phillips11dc4012012-10-29 13:34:45 +00001158 cfiword_t cword;
Thomas Choue5720822010-03-26 08:17:00 +08001159 void *dest;
Mario Six7223a8c2018-01-26 14:43:37 +01001160
Ryan Harkin622b9522015-10-23 16:50:51 +01001161 cword.w64 = 0xffffffffffffffffULL;
Thomas Choue5720822010-03-26 08:17:00 +08001162 dest = flash_map(info, sect, 0);
1163 st = flash_status_poll(info, &cword, dest,
Mario Sixddcf0542018-01-26 14:43:54 +01001164 info->erase_blk_tout,
1165 "erase");
Thomas Choue5720822010-03-26 08:17:00 +08001166 flash_unmap(info, sect, 0, dest);
Mario Six12d7fed2018-01-26 14:43:43 +01001167 } else {
Thomas Choue5720822010-03-26 08:17:00 +08001168 st = flash_full_status_check(info, sect,
1169 info->erase_blk_tout,
1170 "erase");
Mario Six12d7fed2018-01-26 14:43:43 +01001171 }
1172
Thomas Choue5720822010-03-26 08:17:00 +08001173 if (st)
wdenk5653fc32004-02-08 22:55:38 +00001174 rcode = 1;
Thomas Choue5720822010-03-26 08:17:00 +08001175 else if (flash_verbose)
Mario Six188a5562018-01-26 14:43:31 +01001176 putc('.');
wdenk5653fc32004-02-08 22:55:38 +00001177 }
1178 }
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001179
1180 if (flash_verbose)
Mario Six188a5562018-01-26 14:43:31 +01001181 puts(" done\n");
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001182
wdenk5653fc32004-02-08 22:55:38 +00001183 return rcode;
1184}
1185
Stefan Roese70084df2010-08-13 09:36:36 +02001186#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1187static int sector_erased(flash_info_t *info, int i)
1188{
1189 int k;
1190 int size;
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001191 u32 *flash;
Stefan Roese70084df2010-08-13 09:36:36 +02001192
1193 /*
1194 * Check if whole sector is erased
1195 */
1196 size = flash_sector_size(info, i);
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001197 flash = (u32 *)info->start[i];
Stefan Roese70084df2010-08-13 09:36:36 +02001198 /* divide by 4 for longword access */
1199 size = size >> 2;
1200
1201 for (k = 0; k < size; k++) {
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001202 if (flash_read32(flash++) != 0xffffffff)
Stefan Roese70084df2010-08-13 09:36:36 +02001203 return 0; /* not erased */
1204 }
1205
1206 return 1; /* erased */
1207}
1208#endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1209
Mario Sixca2b07a2018-01-26 14:43:32 +01001210void flash_print_info(flash_info_t *info)
wdenk5653fc32004-02-08 22:55:38 +00001211{
1212 int i;
1213
1214 if (info->flash_id != FLASH_MAN_CFI) {
Mario Six188a5562018-01-26 14:43:31 +01001215 puts("missing or unknown FLASH type\n");
wdenk5653fc32004-02-08 22:55:38 +00001216 return;
1217 }
1218
Mario Six188a5562018-01-26 14:43:31 +01001219 printf("%s flash (%d x %d)",
Mario Sixc0350fb2018-01-26 14:43:55 +01001220 info->name,
1221 (info->portwidth << 3), (info->chipwidth << 3));
Mario Six640f4e32018-01-26 14:43:36 +01001222 if (info->size < 1024 * 1024)
Mario Six188a5562018-01-26 14:43:31 +01001223 printf(" Size: %ld kB in %d Sectors\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001224 info->size >> 10, info->sector_count);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001225 else
Mario Six188a5562018-01-26 14:43:31 +01001226 printf(" Size: %ld MB in %d Sectors\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001227 info->size >> 20, info->sector_count);
Mario Six188a5562018-01-26 14:43:31 +01001228 printf(" ");
Stefan Roese260421a2006-11-13 13:55:24 +01001229 switch (info->vendor) {
Mario Sixdde09132018-01-26 14:43:35 +01001230 case CFI_CMDSET_INTEL_PROG_REGIONS:
1231 printf("Intel Prog Regions");
1232 break;
1233 case CFI_CMDSET_INTEL_STANDARD:
1234 printf("Intel Standard");
1235 break;
1236 case CFI_CMDSET_INTEL_EXTENDED:
1237 printf("Intel Extended");
1238 break;
1239 case CFI_CMDSET_AMD_STANDARD:
1240 printf("AMD Standard");
1241 break;
1242 case CFI_CMDSET_AMD_EXTENDED:
1243 printf("AMD Extended");
1244 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001245#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Sixdde09132018-01-26 14:43:35 +01001246 case CFI_CMDSET_AMD_LEGACY:
1247 printf("AMD Legacy");
1248 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001249#endif
Mario Sixdde09132018-01-26 14:43:35 +01001250 default:
1251 printf("Unknown (%d)", info->vendor);
1252 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001253 }
Mario Six188a5562018-01-26 14:43:31 +01001254 printf(" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
Mario Sixc0350fb2018-01-26 14:43:55 +01001255 info->manufacturer_id);
Mario Six188a5562018-01-26 14:43:31 +01001256 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
Mario Sixc0350fb2018-01-26 14:43:55 +01001257 info->device_id);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001258 if ((info->device_id & 0xff) == 0x7E) {
1259 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
Mario Sixc0350fb2018-01-26 14:43:55 +01001260 info->device_id2);
Stefan Roese260421a2006-11-13 13:55:24 +01001261 }
Mario Six4f89da42018-01-26 14:43:42 +01001262 if (info->vendor == CFI_CMDSET_AMD_STANDARD && info->legacy_unlock)
Stefan Roesed2af0282012-12-06 15:44:12 +01001263 printf("\n Advanced Sector Protection (PPB) enabled");
Mario Six188a5562018-01-26 14:43:31 +01001264 printf("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001265 info->erase_blk_tout, info->write_tout);
Stefan Roese260421a2006-11-13 13:55:24 +01001266 if (info->buffer_size > 1) {
Mario Six876c52f2018-01-26 14:43:50 +01001267 printf(" Buffer write timeout: %ld ms, ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001268 info->buffer_write_tout);
Mario Six876c52f2018-01-26 14:43:50 +01001269 printf("buffer size: %d bytes\n", info->buffer_size);
Stefan Roese260421a2006-11-13 13:55:24 +01001270 }
wdenk5653fc32004-02-08 22:55:38 +00001271
Mario Six188a5562018-01-26 14:43:31 +01001272 puts("\n Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +00001273 for (i = 0; i < info->sector_count; ++i) {
Kim Phillips2e973942010-07-26 18:35:39 -05001274 if (ctrlc())
Stefan Roese70084df2010-08-13 09:36:36 +02001275 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001276 if ((i % 5) == 0)
Stefan Roese70084df2010-08-13 09:36:36 +02001277 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001278#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
wdenk5653fc32004-02-08 22:55:38 +00001279 /* print empty and read-only info */
Mario Six188a5562018-01-26 14:43:31 +01001280 printf(" %08lX %c %s ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001281 info->start[i],
1282 sector_erased(info, i) ? 'E' : ' ',
1283 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001284#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Mario Six188a5562018-01-26 14:43:31 +01001285 printf(" %08lX %s ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001286 info->start[i],
1287 info->protect[i] ? "RO" : " ");
wdenk5653fc32004-02-08 22:55:38 +00001288#endif
1289 }
Mario Six188a5562018-01-26 14:43:31 +01001290 putc('\n');
wdenk5653fc32004-02-08 22:55:38 +00001291}
1292
1293/*-----------------------------------------------------------------------
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001294 * This is used in a few places in write_buf() to show programming
1295 * progress. Making it a function is nasty because it needs to do side
1296 * effect updates to digit and dots. Repeated code is nasty too, so
1297 * we define it once here.
1298 */
Stefan Roesef0105722008-03-19 07:09:26 +01001299#ifdef CONFIG_FLASH_SHOW_PROGRESS
1300#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001301 if (flash_verbose) { \
1302 dots -= dots_sub; \
Mario Six4f89da42018-01-26 14:43:42 +01001303 if (scale > 0 && dots <= 0) { \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001304 if ((digit % 5) == 0) \
Mario Six188a5562018-01-26 14:43:31 +01001305 printf("%d", digit / 5); \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001306 else \
Mario Six188a5562018-01-26 14:43:31 +01001307 putc('.'); \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001308 digit--; \
1309 dots += scale; \
1310 } \
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001311 }
Stefan Roesef0105722008-03-19 07:09:26 +01001312#else
1313#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1314#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001315
1316/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001317 * Copy memory to flash, returns:
1318 * 0 - OK
1319 * 1 - write timeout
1320 * 2 - Flash not erased
1321 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001322int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +00001323{
1324 ulong wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001325 uchar *p;
wdenk5653fc32004-02-08 22:55:38 +00001326 int aln;
1327 cfiword_t cword;
1328 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001329#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001330 int buffered_size;
1331#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001332#ifdef CONFIG_FLASH_SHOW_PROGRESS
1333 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1334 int scale = 0;
1335 int dots = 0;
1336
1337 /*
1338 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1339 */
1340 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1341 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1342 CONFIG_FLASH_SHOW_PROGRESS);
1343 }
1344#endif
1345
wdenkbf9e3b32004-02-12 00:47:09 +00001346 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +00001347 wp = (addr & ~(info->portwidth - 1));
1348
1349 /* handle unaligned start */
Mario Sixd3525b62018-01-26 14:43:48 +01001350 aln = addr - wp;
1351 if (aln != 0) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001352 cword.w32 = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001353 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001354 for (i = 0; i < aln; ++i)
Mario Six188a5562018-01-26 14:43:31 +01001355 flash_add_byte(info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001356
wdenkbf9e3b32004-02-12 00:47:09 +00001357 for (; (i < info->portwidth) && (cnt > 0); i++) {
Mario Six188a5562018-01-26 14:43:31 +01001358 flash_add_byte(info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001359 cnt--;
wdenk5653fc32004-02-08 22:55:38 +00001360 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001361 for (; (cnt == 0) && (i < info->portwidth); ++i)
Mario Six188a5562018-01-26 14:43:31 +01001362 flash_add_byte(info, &cword, flash_read8(p + i));
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001363
Mario Six188a5562018-01-26 14:43:31 +01001364 rc = flash_write_cfiword(info, wp, cword);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001365 if (rc != 0)
wdenk5653fc32004-02-08 22:55:38 +00001366 return rc;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001367
1368 wp += i;
Stefan Roesef0105722008-03-19 07:09:26 +01001369 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
wdenk5653fc32004-02-08 22:55:38 +00001370 }
1371
wdenkbf9e3b32004-02-12 00:47:09 +00001372 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001373#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001374 buffered_size = (info->portwidth / info->chipwidth);
1375 buffered_size *= info->buffer_size;
1376 while (cnt >= info->portwidth) {
Stefan Roese79b4cda2006-02-28 15:29:58 +01001377 /* prohibit buffer write when buffer_size is 1 */
1378 if (info->buffer_size == 1) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001379 cword.w32 = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01001380 for (i = 0; i < info->portwidth; i++)
Mario Six188a5562018-01-26 14:43:31 +01001381 flash_add_byte(info, &cword, *src++);
Mario Sixd3525b62018-01-26 14:43:48 +01001382 rc = flash_write_cfiword(info, wp, cword);
1383 if (rc != 0)
Stefan Roese79b4cda2006-02-28 15:29:58 +01001384 return rc;
1385 wp += info->portwidth;
1386 cnt -= info->portwidth;
1387 continue;
1388 }
1389
1390 /* write buffer until next buffered_size aligned boundary */
1391 i = buffered_size - (wp % buffered_size);
1392 if (i > cnt)
1393 i = cnt;
Mario Sixd3525b62018-01-26 14:43:48 +01001394 rc = flash_write_cfibuffer(info, wp, src, i);
1395 if (rc != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +00001396 return rc;
Wolfgang Denk8d4ba3d2005-08-12 22:35:59 +02001397 i -= i & (info->portwidth - 1);
wdenk5653fc32004-02-08 22:55:38 +00001398 wp += i;
1399 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +00001400 cnt -= i;
Stefan Roesef0105722008-03-19 07:09:26 +01001401 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001402 /* Only check every once in a while */
1403 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1404 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001405 }
1406#else
wdenkbf9e3b32004-02-12 00:47:09 +00001407 while (cnt >= info->portwidth) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001408 cword.w32 = 0;
Mario Six0412e902018-01-26 14:43:38 +01001409 for (i = 0; i < info->portwidth; i++)
Mario Six188a5562018-01-26 14:43:31 +01001410 flash_add_byte(info, &cword, *src++);
Mario Sixd3525b62018-01-26 14:43:48 +01001411 rc = flash_write_cfiword(info, wp, cword);
1412 if (rc != 0)
wdenk5653fc32004-02-08 22:55:38 +00001413 return rc;
1414 wp += info->portwidth;
1415 cnt -= info->portwidth;
Stefan Roesef0105722008-03-19 07:09:26 +01001416 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001417 /* Only check every once in a while */
1418 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1419 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001420 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001421#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001422
Mario Six0412e902018-01-26 14:43:38 +01001423 if (cnt == 0)
wdenk5653fc32004-02-08 22:55:38 +00001424 return (0);
wdenk5653fc32004-02-08 22:55:38 +00001425
1426 /*
1427 * handle unaligned tail bytes
1428 */
Ryan Harkin622b9522015-10-23 16:50:51 +01001429 cword.w32 = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001430 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001431 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Mario Six188a5562018-01-26 14:43:31 +01001432 flash_add_byte(info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001433 --cnt;
1434 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001435 for (; i < info->portwidth; ++i)
Mario Six188a5562018-01-26 14:43:31 +01001436 flash_add_byte(info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001437
Mario Six188a5562018-01-26 14:43:31 +01001438 return flash_write_cfiword(info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +00001439}
1440
Stefan Roese20043a42012-12-06 15:44:09 +01001441static inline int manufact_match(flash_info_t *info, u32 manu)
1442{
1443 return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
1444}
1445
wdenk5653fc32004-02-08 22:55:38 +00001446/*-----------------------------------------------------------------------
1447 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001448#ifdef CONFIG_SYS_FLASH_PROTECTION
wdenk5653fc32004-02-08 22:55:38 +00001449
Holger Brunck81316a92012-08-09 10:22:41 +02001450static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1451{
Mario Six88ecd8b2018-01-26 14:43:39 +01001452 if (manufact_match(info, INTEL_MANUFACT) &&
Mario Sixc0350fb2018-01-26 14:43:55 +01001453 info->device_id == NUMONYX_256MBIT) {
Holger Brunck81316a92012-08-09 10:22:41 +02001454 /*
1455 * see errata called
1456 * "Numonyx Axcell P33/P30 Specification Update" :)
1457 */
1458 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1459 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1460 prot)) {
1461 /*
1462 * cmd must come before FLASH_CMD_PROTECT + 20us
1463 * Disable interrupts which might cause a timeout here.
1464 */
1465 int flag = disable_interrupts();
1466 unsigned short cmd;
1467
1468 if (prot)
1469 cmd = FLASH_CMD_PROTECT_SET;
1470 else
1471 cmd = FLASH_CMD_PROTECT_CLEAR;
Andre Przywara58eab322016-11-16 00:50:06 +00001472
1473 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
Holger Brunck81316a92012-08-09 10:22:41 +02001474 flash_write_cmd(info, sector, 0, cmd);
1475 /* re-enable interrupts if necessary */
1476 if (flag)
1477 enable_interrupts();
1478 }
1479 return 1;
1480 }
1481 return 0;
1482}
1483
Mario Sixca2b07a2018-01-26 14:43:32 +01001484int flash_real_protect(flash_info_t *info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +00001485{
1486 int retcode = 0;
1487
Rafael Camposbc9019e2008-07-31 10:22:20 +02001488 switch (info->vendor) {
Mario Sixdde09132018-01-26 14:43:35 +01001489 case CFI_CMDSET_INTEL_PROG_REGIONS:
1490 case CFI_CMDSET_INTEL_STANDARD:
1491 case CFI_CMDSET_INTEL_EXTENDED:
1492 if (!cfi_protect_bugfix(info, sector, prot)) {
1493 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001494 FLASH_CMD_CLEAR_STATUS);
Mario Sixdde09132018-01-26 14:43:35 +01001495 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001496 FLASH_CMD_PROTECT);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001497 if (prot)
Mario Sixdde09132018-01-26 14:43:35 +01001498 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001499 FLASH_CMD_PROTECT_SET);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001500 else
Mario Sixdde09132018-01-26 14:43:35 +01001501 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001502 FLASH_CMD_PROTECT_CLEAR);
Mario Sixdde09132018-01-26 14:43:35 +01001503 }
1504 break;
1505 case CFI_CMDSET_AMD_EXTENDED:
1506 case CFI_CMDSET_AMD_STANDARD:
1507 /* U-Boot only checks the first byte */
1508 if (manufact_match(info, ATM_MANUFACT)) {
1509 if (prot) {
1510 flash_unlock_seq(info, 0);
1511 flash_write_cmd(info, 0,
1512 info->addr_unlock1,
1513 ATM_CMD_SOFTLOCK_START);
1514 flash_unlock_seq(info, 0);
1515 flash_write_cmd(info, sector, 0,
1516 ATM_CMD_LOCK_SECT);
1517 } else {
1518 flash_write_cmd(info, 0,
1519 info->addr_unlock1,
1520 AMD_CMD_UNLOCK_START);
1521 if (info->device_id == ATM_ID_BV6416)
1522 flash_write_cmd(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001523 0, ATM_CMD_UNLOCK_SECT);
Mario Sixdde09132018-01-26 14:43:35 +01001524 }
1525 }
1526 if (info->legacy_unlock) {
1527 int flag = disable_interrupts();
1528 int lock_flag;
1529
1530 flash_unlock_seq(info, 0);
1531 flash_write_cmd(info, 0, info->addr_unlock1,
1532 AMD_CMD_SET_PPB_ENTRY);
1533 lock_flag = flash_isset(info, sector, 0, 0x01);
1534 if (prot) {
1535 if (lock_flag) {
1536 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001537 AMD_CMD_PPB_LOCK_BC1);
Mario Sixdde09132018-01-26 14:43:35 +01001538 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001539 AMD_CMD_PPB_LOCK_BC2);
Mario Sixdde09132018-01-26 14:43:35 +01001540 }
1541 debug("sector %ld %slocked\n", sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001542 lock_flag ? "" : "already ");
Mario Sixdde09132018-01-26 14:43:35 +01001543 } else {
1544 if (!lock_flag) {
1545 debug("unlock %ld\n", sector);
1546 flash_write_cmd(info, 0, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001547 AMD_CMD_PPB_UNLOCK_BC1);
Mario Sixdde09132018-01-26 14:43:35 +01001548 flash_write_cmd(info, 0, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001549 AMD_CMD_PPB_UNLOCK_BC2);
Mario Sixdde09132018-01-26 14:43:35 +01001550 }
1551 debug("sector %ld %sunlocked\n", sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001552 !lock_flag ? "" : "already ");
Mario Sixdde09132018-01-26 14:43:35 +01001553 }
1554 if (flag)
1555 enable_interrupts();
1556
1557 if (flash_status_check(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001558 info->erase_blk_tout,
1559 prot ? "protect" : "unprotect"))
Mario Sixdde09132018-01-26 14:43:35 +01001560 printf("status check error\n");
1561
1562 flash_write_cmd(info, 0, 0,
1563 AMD_CMD_SET_PPB_EXIT_BC1);
1564 flash_write_cmd(info, 0, 0,
1565 AMD_CMD_SET_PPB_EXIT_BC2);
1566 }
1567 break;
1568#ifdef CONFIG_FLASH_CFI_LEGACY
1569 case CFI_CMDSET_AMD_LEGACY:
1570 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1571 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
1572 if (prot)
Mario Sixddcf0542018-01-26 14:43:54 +01001573 flash_write_cmd(info, sector, 0,
1574 FLASH_CMD_PROTECT_SET);
Mario Sixdde09132018-01-26 14:43:35 +01001575 else
Mario Sixddcf0542018-01-26 14:43:54 +01001576 flash_write_cmd(info, sector, 0,
1577 FLASH_CMD_PROTECT_CLEAR);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001578#endif
Rafael Camposbc9019e2008-07-31 10:22:20 +02001579 };
wdenk5653fc32004-02-08 22:55:38 +00001580
Stefan Roesedf4e8132010-10-25 18:31:29 +02001581 /*
1582 * Flash needs to be in status register read mode for
1583 * flash_full_status_check() to work correctly
1584 */
1585 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
Mario Sixd3525b62018-01-26 14:43:48 +01001586 retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
Mario Sixc0350fb2018-01-26 14:43:55 +01001587 prot ? "protect" : "unprotect");
Mario Sixd3525b62018-01-26 14:43:48 +01001588 if (retcode == 0) {
wdenk5653fc32004-02-08 22:55:38 +00001589 info->protect[sector] = prot;
Stefan Roese2662b402006-04-01 13:41:03 +02001590
1591 /*
1592 * On some of Intel's flash chips (marked via legacy_unlock)
1593 * unprotect unprotects all locking.
1594 */
Mario Six4f89da42018-01-26 14:43:42 +01001595 if (prot == 0 && info->legacy_unlock) {
wdenk5653fc32004-02-08 22:55:38 +00001596 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +00001597
1598 for (i = 0; i < info->sector_count; i++) {
1599 if (info->protect[i])
Mario Six188a5562018-01-26 14:43:31 +01001600 flash_real_protect(info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +00001601 }
1602 }
1603 }
wdenk5653fc32004-02-08 22:55:38 +00001604 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +00001605}
1606
wdenk5653fc32004-02-08 22:55:38 +00001607/*-----------------------------------------------------------------------
1608 * flash_read_user_serial - read the OneTimeProgramming cells
1609 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001610void flash_read_user_serial(flash_info_t *info, void *buffer, int offset,
Mario Sixc0350fb2018-01-26 14:43:55 +01001611 int len)
wdenk5653fc32004-02-08 22:55:38 +00001612{
wdenkbf9e3b32004-02-12 00:47:09 +00001613 uchar *src;
1614 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +00001615
1616 dst = buffer;
Mario Six188a5562018-01-26 14:43:31 +01001617 src = flash_map(info, 0, FLASH_OFFSET_USER_PROTECTION);
1618 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1619 memcpy(dst, src + offset, len);
1620 flash_write_cmd(info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001621 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001622 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001623}
wdenkbf9e3b32004-02-12 00:47:09 +00001624
wdenk5653fc32004-02-08 22:55:38 +00001625/*
1626 * flash_read_factory_serial - read the device Id from the protection area
1627 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001628void flash_read_factory_serial(flash_info_t *info, void *buffer, int offset,
Mario Sixc0350fb2018-01-26 14:43:55 +01001629 int len)
wdenk5653fc32004-02-08 22:55:38 +00001630{
wdenkbf9e3b32004-02-12 00:47:09 +00001631 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +00001632
Mario Six188a5562018-01-26 14:43:31 +01001633 src = flash_map(info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1634 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1635 memcpy(buffer, src + offset, len);
1636 flash_write_cmd(info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001637 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001638 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001639}
1640
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001641#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00001642
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001643/*-----------------------------------------------------------------------
1644 * Reverse the order of the erase regions in the CFI QRY structure.
1645 * This is needed for chips that are either a) correctly detected as
1646 * top-boot, or b) buggy.
1647 */
1648static void cfi_reverse_geometry(struct cfi_qry *qry)
1649{
1650 unsigned int i, j;
1651 u32 tmp;
1652
1653 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
Mario Six4f89da42018-01-26 14:43:42 +01001654 tmp = get_unaligned(&qry->erase_region_info[i]);
1655 put_unaligned(get_unaligned(&qry->erase_region_info[j]),
1656 &qry->erase_region_info[i]);
1657 put_unaligned(tmp, &qry->erase_region_info[j]);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001658 }
1659}
wdenk5653fc32004-02-08 22:55:38 +00001660
1661/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +01001662 * read jedec ids from device and set corresponding fields in info struct
1663 *
1664 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1665 *
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001666 */
1667static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1668{
1669 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001670 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001671 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1672 udelay(1000); /* some flash are slow to respond */
Mario Six188a5562018-01-26 14:43:31 +01001673 info->manufacturer_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001674 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyterd77c7ac2010-08-10 16:54:52 +02001675 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
Mario Six188a5562018-01-26 14:43:31 +01001676 flash_read_word(info, FLASH_OFFSET_DEVICE_ID) :
1677 flash_read_uchar(info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001678 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1679}
1680
1681static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1682{
1683 info->cmd_reset = FLASH_CMD_RESET;
1684
1685 cmdset_intel_read_jedec_ids(info);
1686 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1687
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001688#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001689 /* read legacy lock/unlock bit from intel flash */
1690 if (info->ext_addr) {
Mario Sixc0350fb2018-01-26 14:43:55 +01001691 info->legacy_unlock =
1692 flash_read_uchar(info, info->ext_addr + 5) & 0x08;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001693 }
1694#endif
1695
1696 return 0;
1697}
1698
1699static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1700{
Mario Sixc8a9a822018-01-26 14:43:51 +01001701 ushort bank_id = 0;
1702 uchar manu_id;
York Sun2544f472017-11-18 11:09:08 -08001703 uchar feature;
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001704
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001705 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1706 flash_unlock_seq(info, 0);
1707 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1708 udelay(1000); /* some flash are slow to respond */
Tor Krill90447ec2008-03-28 11:29:10 +01001709
Mario Sixc8a9a822018-01-26 14:43:51 +01001710 manu_id = flash_read_uchar(info, FLASH_OFFSET_MANUFACTURER_ID);
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001711 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
Mario Sixc8a9a822018-01-26 14:43:51 +01001712 while (manu_id == FLASH_CONTINUATION_CODE && bank_id < 0x800) {
1713 bank_id += 0x100;
1714 manu_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001715 bank_id | FLASH_OFFSET_MANUFACTURER_ID);
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001716 }
Mario Sixc8a9a822018-01-26 14:43:51 +01001717 info->manufacturer_id = manu_id;
Tor Krill90447ec2008-03-28 11:29:10 +01001718
York Sun2544f472017-11-18 11:09:08 -08001719 debug("info->ext_addr = 0x%x, cfi_version = 0x%x\n",
1720 info->ext_addr, info->cfi_version);
1721 if (info->ext_addr && info->cfi_version >= 0x3134) {
1722 /* read software feature (at 0x53) */
1723 feature = flash_read_uchar(info, info->ext_addr + 0x13);
1724 debug("feature = 0x%x\n", feature);
1725 info->sr_supported = feature & 0x1;
1726 }
Marek Vasut72443c72017-09-12 19:09:31 +02001727
Mario Sixb1683862018-01-26 14:43:33 +01001728 switch (info->chipwidth) {
Tor Krill90447ec2008-03-28 11:29:10 +01001729 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +01001730 info->device_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001731 FLASH_OFFSET_DEVICE_ID);
Tor Krill90447ec2008-03-28 11:29:10 +01001732 if (info->device_id == 0x7E) {
1733 /* AMD 3-byte (expanded) device ids */
Mario Six188a5562018-01-26 14:43:31 +01001734 info->device_id2 = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001735 FLASH_OFFSET_DEVICE_ID2);
Tor Krill90447ec2008-03-28 11:29:10 +01001736 info->device_id2 <<= 8;
Mario Six188a5562018-01-26 14:43:31 +01001737 info->device_id2 |= flash_read_uchar(info,
Tor Krill90447ec2008-03-28 11:29:10 +01001738 FLASH_OFFSET_DEVICE_ID3);
1739 }
1740 break;
1741 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +01001742 info->device_id = flash_read_word(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001743 FLASH_OFFSET_DEVICE_ID);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001744 if ((info->device_id & 0xff) == 0x7E) {
1745 /* AMD 3-byte (expanded) device ids */
Mario Six188a5562018-01-26 14:43:31 +01001746 info->device_id2 = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001747 FLASH_OFFSET_DEVICE_ID2);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001748 info->device_id2 <<= 8;
Mario Six188a5562018-01-26 14:43:31 +01001749 info->device_id2 |= flash_read_uchar(info,
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001750 FLASH_OFFSET_DEVICE_ID3);
1751 }
Tor Krill90447ec2008-03-28 11:29:10 +01001752 break;
1753 default:
1754 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001755 }
1756 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001757 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001758}
1759
1760static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1761{
1762 info->cmd_reset = AMD_CMD_RESET;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01001763 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001764
1765 cmdset_amd_read_jedec_ids(info);
1766 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1767
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001768#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseac6b9112012-12-06 15:44:11 +01001769 if (info->ext_addr) {
1770 /* read sector protect/unprotect scheme (at 0x49) */
1771 if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001772 info->legacy_unlock = 1;
1773 }
1774#endif
1775
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001776 return 0;
1777}
1778
1779#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Sixca2b07a2018-01-26 14:43:32 +01001780static void flash_read_jedec_ids(flash_info_t *info)
Stefan Roese260421a2006-11-13 13:55:24 +01001781{
1782 info->manufacturer_id = 0;
1783 info->device_id = 0;
1784 info->device_id2 = 0;
1785
1786 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001787 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese260421a2006-11-13 13:55:24 +01001788 case CFI_CMDSET_INTEL_STANDARD:
1789 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001790 cmdset_intel_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001791 break;
1792 case CFI_CMDSET_AMD_STANDARD:
1793 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001794 cmdset_amd_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001795 break;
1796 default:
1797 break;
1798 }
1799}
1800
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001801/*-----------------------------------------------------------------------
1802 * Call board code to request info about non-CFI flash.
1803 * board_flash_get_legacy needs to fill in at least:
1804 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1805 */
Becky Bruce09ce9922009-02-02 16:34:51 -06001806static int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001807{
1808 flash_info_t *info = &flash_info[banknum];
1809
1810 if (board_flash_get_legacy(base, banknum, info)) {
1811 /* board code may have filled info completely. If not, we
Mario Sixa6d18f22018-01-26 14:43:41 +01001812 * use JEDEC ID probing.
1813 */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001814 if (!info->vendor) {
1815 int modes[] = {
1816 CFI_CMDSET_AMD_STANDARD,
1817 CFI_CMDSET_INTEL_STANDARD
1818 };
1819 int i;
1820
Axel Lin31bf0f52013-06-23 00:56:46 +08001821 for (i = 0; i < ARRAY_SIZE(modes); i++) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001822 info->vendor = modes[i];
Becky Bruce09ce9922009-02-02 16:34:51 -06001823 info->start[0] =
1824 (ulong)map_physmem(base,
Stefan Roesee1fb6d02009-02-05 11:44:52 +01001825 info->portwidth,
Becky Bruce09ce9922009-02-02 16:34:51 -06001826 MAP_NOCACHE);
Mario Six88ecd8b2018-01-26 14:43:39 +01001827 if (info->portwidth == FLASH_CFI_8BIT &&
Mario Sixc0350fb2018-01-26 14:43:55 +01001828 info->interface == FLASH_CFI_X8X16) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001829 info->addr_unlock1 = 0x2AAA;
1830 info->addr_unlock2 = 0x5555;
1831 } else {
1832 info->addr_unlock1 = 0x5555;
1833 info->addr_unlock2 = 0x2AAA;
1834 }
1835 flash_read_jedec_ids(info);
1836 debug("JEDEC PROBE: ID %x %x %x\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001837 info->manufacturer_id,
1838 info->device_id,
1839 info->device_id2);
Becky Bruce09ce9922009-02-02 16:34:51 -06001840 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001841 break;
Mario Six98601372018-01-26 14:43:45 +01001842
1843 unmap_physmem((void *)info->start[0],
1844 info->portwidth);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001845 }
1846 }
1847
Mario Sixb1683862018-01-26 14:43:33 +01001848 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001849 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001850 case CFI_CMDSET_INTEL_STANDARD:
1851 case CFI_CMDSET_INTEL_EXTENDED:
1852 info->cmd_reset = FLASH_CMD_RESET;
1853 break;
1854 case CFI_CMDSET_AMD_STANDARD:
1855 case CFI_CMDSET_AMD_EXTENDED:
1856 case CFI_CMDSET_AMD_LEGACY:
1857 info->cmd_reset = AMD_CMD_RESET;
1858 break;
1859 }
1860 info->flash_id = FLASH_MAN_CFI;
1861 return 1;
1862 }
1863 return 0; /* use CFI */
1864}
1865#else
Becky Bruce09ce9922009-02-02 16:34:51 -06001866static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001867{
1868 return 0; /* use CFI */
1869}
1870#endif
1871
Stefan Roese260421a2006-11-13 13:55:24 +01001872/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001873 * detect if flash is compatible with the Common Flash Interface (CFI)
1874 * http://www.jedec.org/download/search/jesd68.pdf
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001875 */
Mario Sixc0350fb2018-01-26 14:43:55 +01001876static void flash_read_cfi(flash_info_t *info, void *buf, unsigned int start,
1877 size_t len)
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001878{
1879 u8 *p = buf;
1880 unsigned int i;
1881
1882 for (i = 0; i < len; i++)
Stefan Roesee303be22013-04-12 19:04:54 +02001883 p[i] = flash_read_uchar(info, start + i);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001884}
1885
Kim Phillips11dc4012012-10-29 13:34:45 +00001886static void __flash_cmd_reset(flash_info_t *info)
Stefan Roesefa36ae72009-10-27 15:15:55 +01001887{
1888 /*
1889 * We do not yet know what kind of commandset to use, so we issue
1890 * the reset command in both Intel and AMD variants, in the hope
1891 * that AMD flash roms ignore the Intel command.
1892 */
1893 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001894 udelay(1);
Stefan Roesefa36ae72009-10-27 15:15:55 +01001895 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1896}
Mario Six7223a8c2018-01-26 14:43:37 +01001897
Stefan Roesefa36ae72009-10-27 15:15:55 +01001898void flash_cmd_reset(flash_info_t *info)
Mario Six640f4e32018-01-26 14:43:36 +01001899 __attribute__((weak, alias("__flash_cmd_reset")));
Stefan Roesefa36ae72009-10-27 15:15:55 +01001900
Mario Sixca2b07a2018-01-26 14:43:32 +01001901static int __flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
wdenk5653fc32004-02-08 22:55:38 +00001902{
Wolfgang Denk92eb7292006-12-27 01:26:13 +01001903 int cfi_offset;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001904
Stefan Roesee303be22013-04-12 19:04:54 +02001905 /* Issue FLASH reset command */
1906 flash_cmd_reset(info);
1907
Axel Lin31bf0f52013-06-23 00:56:46 +08001908 for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001909 cfi_offset++) {
Mario Six188a5562018-01-26 14:43:31 +01001910 flash_write_cmd(info, 0, flash_offset_cfi[cfi_offset],
Mario Sixc0350fb2018-01-26 14:43:55 +01001911 FLASH_CMD_CFI);
Mario Six88ecd8b2018-01-26 14:43:39 +01001912 if (flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP, 'Q') &&
Mario Sixddcf0542018-01-26 14:43:54 +01001913 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
1914 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Mario Sixc0350fb2018-01-26 14:43:55 +01001915 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1916 sizeof(struct cfi_qry));
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001917 info->interface = le16_to_cpu(qry->interface_desc);
Stefan Roesee303be22013-04-12 19:04:54 +02001918
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001919 info->cfi_offset = flash_offset_cfi[cfi_offset];
Mario Six188a5562018-01-26 14:43:31 +01001920 debug("device interface is %d\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001921 info->interface);
Mario Six188a5562018-01-26 14:43:31 +01001922 debug("found port %d chip %d ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001923 info->portwidth, info->chipwidth);
Mario Six188a5562018-01-26 14:43:31 +01001924 debug("port %d bits chip %d bits\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001925 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1926 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001927
1928 /* calculate command offsets as in the Linux driver */
Stefan Roesee303be22013-04-12 19:04:54 +02001929 info->addr_unlock1 = 0x555;
1930 info->addr_unlock2 = 0x2aa;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001931
1932 /*
1933 * modify the unlock address if we are
1934 * in compatibility mode
1935 */
Mario Sixb1683862018-01-26 14:43:33 +01001936 if (/* x8/x16 in x8 mode */
Mario Six4f89da42018-01-26 14:43:42 +01001937 (info->chipwidth == FLASH_CFI_BY8 &&
1938 info->interface == FLASH_CFI_X8X16) ||
Mario Sixb1683862018-01-26 14:43:33 +01001939 /* x16/x32 in x16 mode */
Mario Six4f89da42018-01-26 14:43:42 +01001940 (info->chipwidth == FLASH_CFI_BY16 &&
Mario Six0cec0a12018-01-26 14:43:46 +01001941 info->interface == FLASH_CFI_X16X32)) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001942 info->addr_unlock1 = 0xaaa;
1943 info->addr_unlock2 = 0x555;
1944 }
1945
1946 info->name = "CFI conformant";
1947 return 1;
1948 }
1949 }
1950
1951 return 0;
1952}
1953
Mario Sixca2b07a2018-01-26 14:43:32 +01001954static int flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001955{
Mario Six188a5562018-01-26 14:43:31 +01001956 debug("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +00001957
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001958 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenkbf9e3b32004-02-12 00:47:09 +00001959 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1960 for (info->chipwidth = FLASH_CFI_BY8;
1961 info->chipwidth <= info->portwidth;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001962 info->chipwidth <<= 1)
Stefan Roesee303be22013-04-12 19:04:54 +02001963 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001964 return 1;
wdenk5653fc32004-02-08 22:55:38 +00001965 }
Mario Six188a5562018-01-26 14:43:31 +01001966 debug("not found\n");
wdenk5653fc32004-02-08 22:55:38 +00001967 return 0;
1968}
wdenkbf9e3b32004-02-12 00:47:09 +00001969
wdenk5653fc32004-02-08 22:55:38 +00001970/*
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001971 * Manufacturer-specific quirks. Add workarounds for geometry
1972 * reversal, etc. here.
1973 */
1974static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1975{
1976 /* check if flash geometry needs reversal */
1977 if (qry->num_erase_regions > 1) {
1978 /* reverse geometry if top boot part */
1979 if (info->cfi_version < 0x3131) {
1980 /* CFI < 1.1, try to guess from device id */
1981 if ((info->device_id & 0x80) != 0)
1982 cfi_reverse_geometry(qry);
Stefan Roesee303be22013-04-12 19:04:54 +02001983 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001984 /* CFI >= 1.1, deduct from top/bottom flag */
1985 /* note: ext_addr is valid since cfi_version > 0 */
1986 cfi_reverse_geometry(qry);
1987 }
1988 }
1989}
1990
1991static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1992{
1993 int reverse_geometry = 0;
1994
1995 /* Check the "top boot" bit in the PRI */
1996 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1997 reverse_geometry = 1;
1998
1999 /* AT49BV6416(T) list the erase regions in the wrong order.
2000 * However, the device ID is identical with the non-broken
Ulf Samuelssoncb82a532009-03-27 23:26:43 +01002001 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002002 */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002003 if (info->device_id == 0xd6 || info->device_id == 0xd2)
2004 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002005
2006 if (reverse_geometry)
2007 cfi_reverse_geometry(qry);
2008}
2009
Richard Retanubune8eac432009-01-14 08:44:26 -05002010static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
2011{
2012 /* check if flash geometry needs reversal */
2013 if (qry->num_erase_regions > 1) {
2014 /* reverse geometry if top boot part */
2015 if (info->cfi_version < 0x3131) {
Mike Frysinger6a011ce2011-04-10 16:06:29 -04002016 /* CFI < 1.1, guess by device id */
2017 if (info->device_id == 0x22CA || /* M29W320DT */
2018 info->device_id == 0x2256 || /* M29W320ET */
2019 info->device_id == 0x22D7) { /* M29W800DT */
Richard Retanubune8eac432009-01-14 08:44:26 -05002020 cfi_reverse_geometry(qry);
2021 }
Mike Frysinger4c2105c2011-05-09 18:33:36 -04002022 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
2023 /* CFI >= 1.1, deduct from top/bottom flag */
2024 /* note: ext_addr is valid since cfi_version > 0 */
2025 cfi_reverse_geometry(qry);
Richard Retanubune8eac432009-01-14 08:44:26 -05002026 }
2027 }
2028}
2029
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002030static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
2031{
2032 /*
2033 * SST, for many recent nor parallel flashes, says they are
2034 * CFI-conformant. This is not true, since qry struct.
2035 * reports a std. AMD command set (0x0002), while SST allows to
2036 * erase two different sector sizes for the same memory.
2037 * 64KB sector (SST call it block) needs 0x30 to be erased.
2038 * 4KB sector (SST call it sector) needs 0x50 to be erased.
2039 * Since CFI query detect the 4KB number of sectors, users expects
2040 * a sector granularity of 4KB, and it is here set.
2041 */
2042 if (info->device_id == 0x5D23 || /* SST39VF3201B */
2043 info->device_id == 0x5C23) { /* SST39VF3202B */
2044 /* set sector granularity to 4KB */
Mario Six640f4e32018-01-26 14:43:36 +01002045 info->cmd_erase_sector = 0x50;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002046 }
2047}
2048
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302049static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry)
2050{
2051 /*
2052 * The M29EW devices seem to report the CFI information wrong
2053 * when it's in 8 bit mode.
2054 * There's an app note from Numonyx on this issue.
2055 * So adjust the buffer size for M29EW while operating in 8-bit mode
2056 */
Mario Six4f89da42018-01-26 14:43:42 +01002057 if (qry->max_buf_write_size > 0x8 &&
Mario Sixc0350fb2018-01-26 14:43:55 +01002058 info->device_id == 0x7E &&
2059 (info->device_id2 == 0x2201 ||
2060 info->device_id2 == 0x2301 ||
2061 info->device_id2 == 0x2801 ||
2062 info->device_id2 == 0x4801)) {
Mario Six876c52f2018-01-26 14:43:50 +01002063 debug("Adjusted buffer size on Numonyx flash");
2064 debug(" M29EW family in 8 bit mode\n");
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302065 qry->max_buf_write_size = 0x8;
2066 }
2067}
2068
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002069/*
wdenk5653fc32004-02-08 22:55:38 +00002070 * The following code cannot be run from FLASH!
2071 *
2072 */
Mario Six188a5562018-01-26 14:43:31 +01002073ulong flash_get_size(phys_addr_t base, int banknum)
wdenk5653fc32004-02-08 22:55:38 +00002074{
wdenkbf9e3b32004-02-12 00:47:09 +00002075 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +00002076 int i, j;
2077 flash_sect_t sect_cnt;
Becky Bruce09ce9922009-02-02 16:34:51 -06002078 phys_addr_t sector;
wdenk5653fc32004-02-08 22:55:38 +00002079 unsigned long tmp;
2080 int size_ratio;
2081 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +00002082 int erase_region_size;
2083 int erase_region_count;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002084 struct cfi_qry qry;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002085 unsigned long max_size;
Stefan Roese260421a2006-11-13 13:55:24 +01002086
Kumar Galaf9796902008-05-15 15:13:08 -05002087 memset(&qry, 0, sizeof(qry));
2088
Stefan Roese260421a2006-11-13 13:55:24 +01002089 info->ext_addr = 0;
2090 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002091#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +02002092 info->legacy_unlock = 0;
2093#endif
wdenk5653fc32004-02-08 22:55:38 +00002094
Becky Bruce09ce9922009-02-02 16:34:51 -06002095 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002096
Mario Six188a5562018-01-26 14:43:31 +01002097 if (flash_detect_cfi(info, &qry)) {
Mario Six4f89da42018-01-26 14:43:42 +01002098 info->vendor = le16_to_cpu(get_unaligned(&qry.p_id));
2099 info->ext_addr = le16_to_cpu(get_unaligned(&qry.p_adr));
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002100 num_erase_regions = qry.num_erase_regions;
2101
Stefan Roese260421a2006-11-13 13:55:24 +01002102 if (info->ext_addr) {
Mario Six640f4e32018-01-26 14:43:36 +01002103 info->cfi_version = (ushort)flash_read_uchar(info,
Stefan Roesee303be22013-04-12 19:04:54 +02002104 info->ext_addr + 3) << 8;
Mario Six640f4e32018-01-26 14:43:36 +01002105 info->cfi_version |= (ushort)flash_read_uchar(info,
Stefan Roesee303be22013-04-12 19:04:54 +02002106 info->ext_addr + 4);
Stefan Roese260421a2006-11-13 13:55:24 +01002107 }
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002108
wdenkbf9e3b32004-02-12 00:47:09 +00002109#ifdef DEBUG
Mario Six188a5562018-01-26 14:43:31 +01002110 flash_printqry(&qry);
wdenkbf9e3b32004-02-12 00:47:09 +00002111#endif
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002112
wdenkbf9e3b32004-02-12 00:47:09 +00002113 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002114 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00002115 case CFI_CMDSET_INTEL_STANDARD:
2116 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002117 cmdset_intel_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002118 break;
2119 case CFI_CMDSET_AMD_STANDARD:
2120 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002121 cmdset_amd_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002122 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002123 default:
2124 printf("CFI: Unknown command set 0x%x\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002125 info->vendor);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002126 /*
2127 * Unfortunately, this means we don't know how
2128 * to get the chip back to Read mode. Might
2129 * as well try an Intel-style reset...
2130 */
2131 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2132 return 0;
wdenk5653fc32004-02-08 22:55:38 +00002133 }
wdenkcd37d9e2004-02-10 00:03:41 +00002134
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002135 /* Do manufacturer-specific fixups */
2136 switch (info->manufacturer_id) {
Mario Schuknecht2c9f48a2011-02-21 13:13:14 +01002137 case 0x0001: /* AMD */
2138 case 0x0037: /* AMIC */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002139 flash_fixup_amd(info, &qry);
2140 break;
2141 case 0x001f:
2142 flash_fixup_atmel(info, &qry);
2143 break;
Richard Retanubune8eac432009-01-14 08:44:26 -05002144 case 0x0020:
2145 flash_fixup_stm(info, &qry);
2146 break;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002147 case 0x00bf: /* SST */
2148 flash_fixup_sst(info, &qry);
2149 break;
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302150 case 0x0089: /* Numonyx */
2151 flash_fixup_num(info, &qry);
2152 break;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002153 }
2154
Mario Six188a5562018-01-26 14:43:31 +01002155 debug("manufacturer is %d\n", info->vendor);
2156 debug("manufacturer id is 0x%x\n", info->manufacturer_id);
2157 debug("device id is 0x%x\n", info->device_id);
2158 debug("device id2 is 0x%x\n", info->device_id2);
2159 debug("cfi version is 0x%04x\n", info->cfi_version);
Stefan Roese260421a2006-11-13 13:55:24 +01002160
wdenk5653fc32004-02-08 22:55:38 +00002161 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00002162 /* if the chip is x8/x16 reduce the ratio by half */
Mario Six4f89da42018-01-26 14:43:42 +01002163 if (info->interface == FLASH_CFI_X8X16 &&
Mario Sixc0350fb2018-01-26 14:43:55 +01002164 info->chipwidth == FLASH_CFI_BY8) {
wdenkbf9e3b32004-02-12 00:47:09 +00002165 size_ratio >>= 1;
2166 }
Mario Six188a5562018-01-26 14:43:31 +01002167 debug("size_ratio %d port %d bits chip %d bits\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002168 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2169 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002170 info->size = 1 << qry.dev_size;
2171 /* multiply the size by the number of chips */
2172 info->size *= size_ratio;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002173 max_size = cfi_flash_bank_size(banknum);
Mario Six4f89da42018-01-26 14:43:42 +01002174 if (max_size && info->size > max_size) {
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002175 debug("[truncated from %ldMiB]", info->size >> 20);
2176 info->size = max_size;
2177 }
Mario Six188a5562018-01-26 14:43:31 +01002178 debug("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00002179 sect_cnt = 0;
2180 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00002181 for (i = 0; i < num_erase_regions; i++) {
2182 if (i > NUM_ERASE_REGIONS) {
Mario Six188a5562018-01-26 14:43:31 +01002183 printf("%d erase regions found, only %d used\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002184 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00002185 break;
2186 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002187
Andrew Gabbasovaedadf12013-05-14 12:27:52 -05002188 tmp = le32_to_cpu(get_unaligned(
Mario Six4f89da42018-01-26 14:43:42 +01002189 &qry.erase_region_info[i]));
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002190 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002191
2192 erase_region_count = (tmp & 0xffff) + 1;
2193 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00002194 erase_region_size =
2195 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
Mario Sixddcf0542018-01-26 14:43:54 +01002196 debug("erase_region_count = %d ", erase_region_count);
2197 debug("erase_region_size = %d\n", erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00002198 for (j = 0; j < erase_region_count; j++) {
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002199 if (sector - base >= info->size)
2200 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002201 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002202 printf("ERROR: too many flash sectors\n");
2203 break;
2204 }
Becky Bruce09ce9922009-02-02 16:34:51 -06002205 info->start[sect_cnt] =
2206 (ulong)map_physmem(sector,
2207 info->portwidth,
2208 MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002209 sector += (erase_region_size * size_ratio);
wdenka1191902005-01-09 17:12:27 +00002210
2211 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002212 * Only read protection status from
2213 * supported devices (intel...)
wdenka1191902005-01-09 17:12:27 +00002214 */
2215 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002216 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenka1191902005-01-09 17:12:27 +00002217 case CFI_CMDSET_INTEL_EXTENDED:
2218 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesedf4e8132010-10-25 18:31:29 +02002219 /*
2220 * Set flash to read-id mode. Otherwise
2221 * reading protected status is not
2222 * guaranteed.
2223 */
2224 flash_write_cmd(info, sect_cnt, 0,
2225 FLASH_CMD_READ_ID);
wdenka1191902005-01-09 17:12:27 +00002226 info->protect[sect_cnt] =
Mario Six188a5562018-01-26 14:43:31 +01002227 flash_isset(info, sect_cnt,
Mario Sixc0350fb2018-01-26 14:43:55 +01002228 FLASH_OFFSET_PROTECT,
2229 FLASH_STATUS_PROTECT);
Vasily Khoruzhickedc498c2016-03-20 18:37:10 -07002230 flash_write_cmd(info, sect_cnt, 0,
2231 FLASH_CMD_RESET);
wdenka1191902005-01-09 17:12:27 +00002232 break;
Stefan Roese03deff42012-12-06 15:44:10 +01002233 case CFI_CMDSET_AMD_EXTENDED:
2234 case CFI_CMDSET_AMD_STANDARD:
Stefan Roeseac6b9112012-12-06 15:44:11 +01002235 if (!info->legacy_unlock) {
Stefan Roese03deff42012-12-06 15:44:10 +01002236 /* default: not protected */
2237 info->protect[sect_cnt] = 0;
2238 break;
2239 }
2240
2241 /* Read protection (PPB) from sector */
2242 flash_write_cmd(info, 0, 0,
2243 info->cmd_reset);
2244 flash_unlock_seq(info, 0);
2245 flash_write_cmd(info, 0,
2246 info->addr_unlock1,
2247 FLASH_CMD_READ_ID);
2248 info->protect[sect_cnt] =
2249 flash_isset(
2250 info, sect_cnt,
2251 FLASH_OFFSET_PROTECT,
2252 FLASH_STATUS_PROTECT);
2253 break;
wdenka1191902005-01-09 17:12:27 +00002254 default:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002255 /* default: not protected */
2256 info->protect[sect_cnt] = 0;
wdenka1191902005-01-09 17:12:27 +00002257 }
2258
wdenk5653fc32004-02-08 22:55:38 +00002259 sect_cnt++;
2260 }
2261 }
2262
2263 info->sector_count = sect_cnt;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002264 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2265 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002266 info->erase_blk_tout = tmp *
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002267 (1 << qry.block_erase_timeout_max);
2268 tmp = (1 << qry.buf_write_timeout_typ) *
2269 (1 << qry.buf_write_timeout_max);
2270
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002271 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002272 info->buffer_write_tout = (tmp + 999) / 1000;
2273 tmp = (1 << qry.word_write_timeout_typ) *
2274 (1 << qry.word_write_timeout_max);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002275 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002276 info->write_tout = (tmp + 999) / 1000;
wdenk5653fc32004-02-08 22:55:38 +00002277 info->flash_id = FLASH_MAN_CFI;
Mario Six4f89da42018-01-26 14:43:42 +01002278 if (info->interface == FLASH_CFI_X8X16 &&
2279 info->chipwidth == FLASH_CFI_BY8) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002280 /* XXX - Need to test on x8/x16 in parallel. */
2281 info->portwidth >>= 1;
wdenk855a4962004-03-14 18:23:55 +00002282 }
Mike Frysinger22159872008-10-02 01:55:38 -04002283
Mario Six188a5562018-01-26 14:43:31 +01002284 flash_write_cmd(info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +00002285 }
2286
wdenkbf9e3b32004-02-12 00:47:09 +00002287 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00002288}
2289
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002290#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002291void flash_set_verbose(uint v)
2292{
2293 flash_verbose = v;
2294}
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002295#endif
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002296
Stefan Roese6f726f92010-10-25 18:31:48 +02002297static void cfi_flash_set_config_reg(u32 base, u16 val)
2298{
2299#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2300 /*
2301 * Only set this config register if really defined
2302 * to a valid value (0xffff is invalid)
2303 */
2304 if (val == 0xffff)
2305 return;
2306
2307 /*
2308 * Set configuration register. Data is "encrypted" in the 16 lower
2309 * address bits.
2310 */
2311 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2312 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2313
2314 /*
2315 * Finally issue reset-command to bring device back to
2316 * read-array mode
2317 */
2318 flash_write16(FLASH_CMD_RESET, (void *)base);
2319#endif
2320}
2321
wdenk5653fc32004-02-08 22:55:38 +00002322/*-----------------------------------------------------------------------
2323 */
Heiko Schocher6ee14162011-04-04 08:10:21 +02002324
Marek Vasut236c49a2017-08-20 17:20:00 +02002325static void flash_protect_default(void)
Heiko Schocher6ee14162011-04-04 08:10:21 +02002326{
Peter Tyser2c519832011-04-13 11:46:56 -05002327#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2328 int i;
2329 struct apl_s {
2330 ulong start;
2331 ulong size;
2332 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2333#endif
2334
Heiko Schocher6ee14162011-04-04 08:10:21 +02002335 /* Monitor protection ON by default */
Vignesh Raghavendrad75eacf2019-10-23 13:30:00 +05302336#if defined(CONFIG_SYS_MONITOR_BASE) && \
2337 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
Heiko Schocher6ee14162011-04-04 08:10:21 +02002338 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2339 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002340 CONFIG_SYS_MONITOR_BASE,
2341 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2342 flash_get_info(CONFIG_SYS_MONITOR_BASE));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002343#endif
2344
2345 /* Environment protection ON by default */
2346#ifdef CONFIG_ENV_IS_IN_FLASH
2347 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002348 CONFIG_ENV_ADDR,
2349 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2350 flash_get_info(CONFIG_ENV_ADDR));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002351#endif
2352
2353 /* Redundant environment protection ON by default */
2354#ifdef CONFIG_ENV_ADDR_REDUND
2355 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002356 CONFIG_ENV_ADDR_REDUND,
2357 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2358 flash_get_info(CONFIG_ENV_ADDR_REDUND));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002359#endif
2360
2361#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
Axel Lin31bf0f52013-06-23 00:56:46 +08002362 for (i = 0; i < ARRAY_SIZE(apl); i++) {
Marek Vasut31d34142011-10-21 14:17:05 +00002363 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocher6ee14162011-04-04 08:10:21 +02002364 apl[i].start, apl[i].start + apl[i].size - 1);
2365 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002366 apl[i].start,
2367 apl[i].start + apl[i].size - 1,
2368 flash_get_info(apl[i].start));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002369 }
2370#endif
2371}
2372
Mario Six188a5562018-01-26 14:43:31 +01002373unsigned long flash_init(void)
wdenk5653fc32004-02-08 22:55:38 +00002374{
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002375 unsigned long size = 0;
2376 int i;
wdenk5653fc32004-02-08 22:55:38 +00002377
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002378#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann3a3baf32009-03-21 09:59:34 -04002379 /* read environment from EEPROM */
2380 char s[64];
Mario Six7223a8c2018-01-26 14:43:37 +01002381
Simon Glass00caae62017-08-03 12:22:12 -06002382 env_get_f("unlock", s, sizeof(s));
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002383#endif
wdenk5653fc32004-02-08 22:55:38 +00002384
Thomas Chouf1056912015-11-07 14:31:08 +08002385#ifdef CONFIG_CFI_FLASH /* for driver model */
2386 cfi_flash_init_dm();
2387#endif
2388
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002389 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002390 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002391 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk5653fc32004-02-08 22:55:38 +00002392
Stefan Roese6f726f92010-10-25 18:31:48 +02002393 /* Optionally write flash configuration register */
2394 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2395 cfi_flash_config_reg(i));
2396
Stefan Roeseb00e19c2010-08-30 10:11:51 +02002397 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002398 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002399 size += flash_info[i].size;
2400 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002401#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Mario Six876c52f2018-01-26 14:43:50 +01002402 printf("## Unknown flash on Bank %d ", i + 1);
2403 printf("- Size = 0x%08lx = %ld MB\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002404 flash_info[i].size,
2405 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002406#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
wdenk5653fc32004-02-08 22:55:38 +00002407 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002408#ifdef CONFIG_SYS_FLASH_PROTECTION
Jeroen Hofsteec15df212014-06-17 22:47:31 +02002409 else if (strcmp(s, "yes") == 0) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002410 /*
2411 * Only the U-Boot image and it's environment
2412 * is protected, all other sectors are
2413 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002414 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002415 * and the environment variable "unlock" is
2416 * set to "yes".
2417 */
2418 if (flash_info[i].legacy_unlock) {
2419 int k;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002420
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002421 /*
2422 * Disable legacy_unlock temporarily,
2423 * since flash_real_protect would
2424 * relock all other sectors again
2425 * otherwise.
2426 */
2427 flash_info[i].legacy_unlock = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002428
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002429 /*
2430 * Legacy unlocking (e.g. Intel J3) ->
2431 * unlock only one sector. This will
2432 * unlock all sectors.
2433 */
Mario Six188a5562018-01-26 14:43:31 +01002434 flash_real_protect(&flash_info[i], 0, 0);
Stefan Roese79b4cda2006-02-28 15:29:58 +01002435
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002436 flash_info[i].legacy_unlock = 1;
2437
2438 /*
2439 * Manually mark other sectors as
2440 * unlocked (unprotected)
2441 */
2442 for (k = 1; k < flash_info[i].sector_count; k++)
2443 flash_info[i].protect[k] = 0;
2444 } else {
2445 /*
2446 * No legancy unlocking -> unlock all sectors
2447 */
Mario Six188a5562018-01-26 14:43:31 +01002448 flash_protect(FLAG_PROTECT_CLEAR,
Mario Sixc0350fb2018-01-26 14:43:55 +01002449 flash_info[i].start[0],
2450 flash_info[i].start[0]
2451 + flash_info[i].size - 1,
2452 &flash_info[i]);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002453 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01002454 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002455#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00002456 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002457
Heiko Schocher6ee14162011-04-04 08:10:21 +02002458 flash_protect_default();
Piotr Ziecik91809ed2008-11-17 15:57:58 +01002459#ifdef CONFIG_FLASH_CFI_MTD
2460 cfi_mtd_init();
2461#endif
2462
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002463 return (size);
wdenk5653fc32004-02-08 22:55:38 +00002464}
Thomas Chouf1056912015-11-07 14:31:08 +08002465
2466#ifdef CONFIG_CFI_FLASH /* for driver model */
2467static int cfi_flash_probe(struct udevice *dev)
2468{
Thomas Chouf1056912015-11-07 14:31:08 +08002469 const fdt32_t *cell;
Mario Six8bfeb332018-03-28 14:38:41 +02002470 int addrc, sizec;
Thomas Chouf1056912015-11-07 14:31:08 +08002471 int len, idx;
2472
Mario Six8bfeb332018-03-28 14:38:41 +02002473 addrc = dev_read_addr_cells(dev);
2474 sizec = dev_read_size_cells(dev);
2475
2476 /* decode regs; there may be multiple reg tuples. */
2477 cell = dev_read_prop(dev, "reg", &len);
Thomas Chouf1056912015-11-07 14:31:08 +08002478 if (!cell)
2479 return -ENOENT;
2480 idx = 0;
2481 len /= sizeof(fdt32_t);
2482 while (idx < len) {
Mario Six8bfeb332018-03-28 14:38:41 +02002483 phys_addr_t addr;
2484
2485 addr = dev_translate_address(dev, cell + idx);
2486
Marek Vasut1ec0a372017-09-12 19:09:08 +02002487 flash_info[cfi_flash_num_flash_banks].dev = dev;
2488 flash_info[cfi_flash_num_flash_banks].base = addr;
2489 cfi_flash_num_flash_banks++;
Mario Six8bfeb332018-03-28 14:38:41 +02002490
Thomas Chouf1056912015-11-07 14:31:08 +08002491 idx += addrc + sizec;
2492 }
Marek Vasut1ec0a372017-09-12 19:09:08 +02002493 gd->bd->bi_flashstart = flash_info[0].base;
Thomas Chouf1056912015-11-07 14:31:08 +08002494
2495 return 0;
2496}
2497
2498static const struct udevice_id cfi_flash_ids[] = {
2499 { .compatible = "cfi-flash" },
2500 { .compatible = "jedec-flash" },
2501 {}
2502};
2503
2504U_BOOT_DRIVER(cfi_flash) = {
2505 .name = "cfi_flash",
2506 .id = UCLASS_MTD,
2507 .of_match = cfi_flash_ids,
2508 .probe = cfi_flash_probe,
2509};
2510#endif /* CONFIG_CFI_FLASH */