blob: 0574fa63a44d2579a87f1ef0186accbb921568db [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>
wdenk5653fc32004-02-08 22:55:38 +000025#include <asm/processor.h>
Haiying Wang3a197b22007-02-21 16:52:31 +010026#include <asm/io.h>
wdenk4c0d4c32004-06-09 17:34:58 +000027#include <asm/byteorder.h>
Andrew Gabbasovaedadf12013-05-14 12:27:52 -050028#include <asm/unaligned.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060029#include <env_internal.h>
Stefan Roesefa36ae72009-10-27 15:15:55 +010030#include <mtd/cfi_flash.h>
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +010031#include <watchdog.h>
wdenk028ab6b2004-02-23 23:54:43 +000032
wdenk5653fc32004-02-08 22:55:38 +000033/*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010034 * This file implements a Common Flash Interface (CFI) driver for
35 * U-Boot.
36 *
37 * The width of the port and the width of the chips are determined at
38 * initialization. These widths are used to calculate the address for
39 * access CFI data structures.
wdenk5653fc32004-02-08 22:55:38 +000040 *
41 * References
42 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
43 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
44 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
45 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
Stefan Roese260421a2006-11-13 13:55:24 +010046 * AMD CFI Specification, Release 2.0 December 1, 2001
47 * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
48 * Device IDs, Publication Number 25538 Revision A, November 8, 2001
wdenk5653fc32004-02-08 22:55:38 +000049 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050 * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
Heiko Schocherd0b6e142007-01-19 18:05:26 +010051 * reading and writing ... (yes there is such a Hardware).
wdenk5653fc32004-02-08 22:55:38 +000052 */
53
Thomas Chouf1056912015-11-07 14:31:08 +080054DECLARE_GLOBAL_DATA_PTR;
55
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +010056static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
Mike Frysinger4ffeab22010-12-22 09:41:13 -050057#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +010058static uint flash_verbose = 1;
Mike Frysinger4ffeab22010-12-22 09:41:13 -050059#else
60#define flash_verbose 1
61#endif
Wolfgang Denk92eb7292006-12-27 01:26:13 +010062
Wolfgang Denk2a112b22008-08-08 16:39:54 +020063flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
64
Stefan Roese79b4cda2006-02-28 15:29:58 +010065/*
66 * Check if chip width is defined. If not, start detecting with 8bit.
67 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068#ifndef CONFIG_SYS_FLASH_CFI_WIDTH
69#define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
Stefan Roese79b4cda2006-02-28 15:29:58 +010070#endif
71
Jeroen Hofstee00dcb072014-10-08 22:57:23 +020072#ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
73#define __maybe_weak __weak
74#else
75#define __maybe_weak static
76#endif
77
Stefan Roese6f726f92010-10-25 18:31:48 +020078/*
79 * 0xffff is an undefined value for the configuration register. When
80 * this value is returned, the configuration register shall not be
81 * written at all (default mode).
82 */
83static u16 cfi_flash_config_reg(int i)
84{
85#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
86 return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
87#else
88 return 0xffff;
89#endif
90}
91
Stefan Roeseca5def32010-08-31 10:00:10 +020092#if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
93int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
Mario Sixd9a35692018-01-26 14:43:56 +010094#else
95int cfi_flash_num_flash_banks;
Stefan Roeseca5def32010-08-31 10:00:10 +020096#endif
97
Thomas Chouf1056912015-11-07 14:31:08 +080098#ifdef CONFIG_CFI_FLASH /* for driver model */
99static void cfi_flash_init_dm(void)
100{
101 struct udevice *dev;
102
103 cfi_flash_num_flash_banks = 0;
104 /*
105 * The uclass_first_device() will probe the first device and
106 * uclass_next_device() will probe the rest if they exist. So
107 * that cfi_flash_probe() will get called assigning the base
108 * addresses that are available.
109 */
110 for (uclass_first_device(UCLASS_MTD, &dev);
111 dev;
112 uclass_next_device(&dev)) {
113 }
114}
115
Thomas Chouf1056912015-11-07 14:31:08 +0800116phys_addr_t cfi_flash_bank_addr(int i)
117{
Marek Vasut1ec0a372017-09-12 19:09:08 +0200118 return flash_info[i].base;
Thomas Chouf1056912015-11-07 14:31:08 +0800119}
120#else
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200121__weak phys_addr_t cfi_flash_bank_addr(int i)
Stefan Roeseb00e19c2010-08-30 10:11:51 +0200122{
123 return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
124}
Thomas Chouf1056912015-11-07 14:31:08 +0800125#endif
Stefan Roeseb00e19c2010-08-30 10:11:51 +0200126
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200127__weak unsigned long cfi_flash_bank_size(int i)
Ilya Yanokec50a8e2010-10-21 17:20:12 +0200128{
129#ifdef CONFIG_SYS_FLASH_BANKS_SIZES
130 return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
131#else
132 return 0;
133#endif
134}
Ilya Yanokec50a8e2010-10-21 17:20:12 +0200135
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200136__maybe_weak void flash_write8(u8 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100137{
138 __raw_writeb(value, addr);
139}
140
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200141__maybe_weak void flash_write16(u16 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100142{
143 __raw_writew(value, addr);
144}
145
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200146__maybe_weak void flash_write32(u32 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100147{
148 __raw_writel(value, addr);
149}
150
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200151__maybe_weak void flash_write64(u64 value, void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100152{
153 /* No architectures currently implement __raw_writeq() */
154 *(volatile u64 *)addr = value;
155}
156
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200157__maybe_weak u8 flash_read8(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100158{
159 return __raw_readb(addr);
160}
161
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200162__maybe_weak u16 flash_read16(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100163{
164 return __raw_readw(addr);
165}
166
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200167__maybe_weak u32 flash_read32(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100168{
169 return __raw_readl(addr);
170}
171
Jeroen Hofstee00dcb072014-10-08 22:57:23 +0200172__maybe_weak u64 flash_read64(void *addr)
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100173{
174 /* No architectures currently implement __raw_readq() */
175 return *(volatile u64 *)addr;
176}
177
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200178/*-----------------------------------------------------------------------
179 */
Mario Sixddcf0542018-01-26 14:43:54 +0100180#if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || \
Vignesh Raghavendrad75eacf2019-10-23 13:30:00 +0530181 (defined(CONFIG_SYS_MONITOR_BASE) && \
182 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE))
Marek Vasut236c49a2017-08-20 17:20:00 +0200183static flash_info_t *flash_get_info(ulong base)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200184{
185 int i;
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900186 flash_info_t *info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200187
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200188 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Masahiro Yamadae2e273a2013-05-17 14:50:36 +0900189 info = &flash_info[i];
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200190 if (info->size && info->start[0] <= base &&
191 base <= info->start[0] + info->size - 1)
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900192 return info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200193 }
194
Masahiro Yamada24c185c2013-05-17 14:50:37 +0900195 return NULL;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200196}
wdenk5653fc32004-02-08 22:55:38 +0000197#endif
198
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100199unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
200{
201 if (sect != (info->sector_count - 1))
202 return info->start[sect + 1] - info->start[sect];
203 else
204 return info->start[0] + info->size - info->start[sect];
205}
206
wdenk5653fc32004-02-08 22:55:38 +0000207/*-----------------------------------------------------------------------
208 * create an address based on the offset and the port width
209 */
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100210static inline void *
Mario Sixca2b07a2018-01-26 14:43:32 +0100211flash_map(flash_info_t *info, flash_sect_t sect, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000212{
Stefan Roesee303be22013-04-12 19:04:54 +0200213 unsigned int byte_offset = offset * info->portwidth;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100214
Stefan Roesee303be22013-04-12 19:04:54 +0200215 return (void *)(info->start[sect] + byte_offset);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100216}
217
218static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
Mario Sixc0350fb2018-01-26 14:43:55 +0100219 unsigned int offset, void *addr)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100220{
wdenk5653fc32004-02-08 22:55:38 +0000221}
wdenkbf9e3b32004-02-12 00:47:09 +0000222
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200223/*-----------------------------------------------------------------------
224 * make a proper sized command based on the port and chip widths
225 */
Sebastian Siewior7288f972008-07-15 13:35:23 +0200226static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200227{
228 int i;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400229 int cword_offset;
230 int cp_offset;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200231#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Sebastian Siewior340ccb22008-07-16 20:04:49 +0200232 u32 cmd_le = cpu_to_le32(cmd);
233#endif
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400234 uchar val;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200235 uchar *cp = (uchar *) cmdbuf;
236
Mario Sixb1683862018-01-26 14:43:33 +0100237 for (i = info->portwidth; i > 0; i--) {
Mario Six640f4e32018-01-26 14:43:36 +0100238 cword_offset = (info->portwidth - i) % info->chipwidth;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200239#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400240 cp_offset = info->portwidth - i;
Mario Sixdb91bb22018-01-26 14:43:34 +0100241 val = *((uchar *)&cmd_le + cword_offset);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200242#else
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400243 cp_offset = i - 1;
Mario Sixdb91bb22018-01-26 14:43:34 +0100244 val = *((uchar *)&cmd + sizeof(u32) - cword_offset - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200245#endif
Sebastian Siewior7288f972008-07-15 13:35:23 +0200246 cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
Vasiliy Leoenenko93c56f22008-05-07 21:24:44 +0400247 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200248}
249
wdenkbf9e3b32004-02-12 00:47:09 +0000250#ifdef DEBUG
251/*-----------------------------------------------------------------------
252 * Debug support
253 */
Mario Six188a5562018-01-26 14:43:31 +0100254static void print_longlong(char *str, unsigned long long data)
wdenkbf9e3b32004-02-12 00:47:09 +0000255{
256 int i;
257 char *cp;
258
Mario Six640f4e32018-01-26 14:43:36 +0100259 cp = (char *)&data;
wdenkbf9e3b32004-02-12 00:47:09 +0000260 for (i = 0; i < 8; i++)
Mario Six188a5562018-01-26 14:43:31 +0100261 sprintf(&str[i * 2], "%2.2x", *cp++);
wdenkbf9e3b32004-02-12 00:47:09 +0000262}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200263
Mario Six188a5562018-01-26 14:43:31 +0100264static void flash_printqry(struct cfi_qry *qry)
wdenkbf9e3b32004-02-12 00:47:09 +0000265{
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100266 u8 *p = (u8 *)qry;
wdenkbf9e3b32004-02-12 00:47:09 +0000267 int x, y;
268
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100269 for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
270 debug("%02x : ", x);
271 for (y = 0; y < 16; y++)
272 debug("%2.2x ", p[x + y]);
273 debug(" ");
wdenkbf9e3b32004-02-12 00:47:09 +0000274 for (y = 0; y < 16; y++) {
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100275 unsigned char c = p[x + y];
Mario Six7223a8c2018-01-26 14:43:37 +0100276
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100277 if (c >= 0x20 && c <= 0x7e)
278 debug("%c", c);
279 else
280 debug(".");
wdenkbf9e3b32004-02-12 00:47:09 +0000281 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +0100282 debug("\n");
wdenkbf9e3b32004-02-12 00:47:09 +0000283 }
284}
wdenkbf9e3b32004-02-12 00:47:09 +0000285#endif
286
wdenk5653fc32004-02-08 22:55:38 +0000287/*-----------------------------------------------------------------------
288 * read a character at a port width address
289 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100290static inline uchar flash_read_uchar(flash_info_t *info, uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000291{
292 uchar *cp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100293 uchar retval;
wdenkbf9e3b32004-02-12 00:47:09 +0000294
Mario Six188a5562018-01-26 14:43:31 +0100295 cp = flash_map(info, 0, offset);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200296#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100297 retval = flash_read8(cp);
wdenkbf9e3b32004-02-12 00:47:09 +0000298#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100299 retval = flash_read8(cp + info->portwidth - 1);
wdenkbf9e3b32004-02-12 00:47:09 +0000300#endif
Mario Six188a5562018-01-26 14:43:31 +0100301 flash_unmap(info, 0, offset, cp);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100302 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000303}
304
305/*-----------------------------------------------------------------------
Tor Krill90447ec2008-03-28 11:29:10 +0100306 * read a word at a port width address, assume 16bit bus
307 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100308static inline ushort flash_read_word(flash_info_t *info, uint offset)
Tor Krill90447ec2008-03-28 11:29:10 +0100309{
310 ushort *addr, retval;
311
Mario Six188a5562018-01-26 14:43:31 +0100312 addr = flash_map(info, 0, offset);
313 retval = flash_read16(addr);
314 flash_unmap(info, 0, offset, addr);
Tor Krill90447ec2008-03-28 11:29:10 +0100315 return retval;
316}
317
Tor Krill90447ec2008-03-28 11:29:10 +0100318/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +0100319 * read a long word by picking the least significant byte of each maximum
wdenk5653fc32004-02-08 22:55:38 +0000320 * port size word. Swap for ppc format.
321 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100322static ulong flash_read_long (flash_info_t *info, flash_sect_t sect,
Haavard Skinnemoen30557932007-12-13 12:56:29 +0100323 uint offset)
wdenk5653fc32004-02-08 22:55:38 +0000324{
wdenkbf9e3b32004-02-12 00:47:09 +0000325 uchar *addr;
326 ulong retval;
wdenk5653fc32004-02-08 22:55:38 +0000327
wdenkbf9e3b32004-02-12 00:47:09 +0000328#ifdef DEBUG
329 int x;
330#endif
Mario Six188a5562018-01-26 14:43:31 +0100331 addr = flash_map(info, sect, offset);
wdenkbf9e3b32004-02-12 00:47:09 +0000332
333#ifdef DEBUG
Mario Six188a5562018-01-26 14:43:31 +0100334 debug("long addr is at %p info->portwidth = %d\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100335 info->portwidth);
Mario Six0412e902018-01-26 14:43:38 +0100336 for (x = 0; x < 4 * info->portwidth; x++)
Mario Six188a5562018-01-26 14:43:31 +0100337 debug("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
wdenkbf9e3b32004-02-12 00:47:09 +0000338#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200339#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100340 retval = ((flash_read8(addr) << 16) |
341 (flash_read8(addr + info->portwidth) << 24) |
342 (flash_read8(addr + 2 * info->portwidth)) |
343 (flash_read8(addr + 3 * info->portwidth) << 8));
wdenkbf9e3b32004-02-12 00:47:09 +0000344#else
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100345 retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
346 (flash_read8(addr + info->portwidth - 1) << 16) |
347 (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
348 (flash_read8(addr + 3 * info->portwidth - 1)));
wdenkbf9e3b32004-02-12 00:47:09 +0000349#endif
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100350 flash_unmap(info, sect, offset, addr);
351
wdenkbf9e3b32004-02-12 00:47:09 +0000352 return retval;
wdenk5653fc32004-02-08 22:55:38 +0000353}
354
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200355/*
356 * Write a proper sized command to the correct address
357 */
Marek Vasut236c49a2017-08-20 17:20:00 +0200358static void flash_write_cmd(flash_info_t *info, flash_sect_t sect,
359 uint offset, u32 cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200360{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100361 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200362 cfiword_t cword;
363
Mario Six188a5562018-01-26 14:43:31 +0100364 addr = flash_map(info, sect, offset);
365 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200366 switch (info->portwidth) {
367 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +0100368 debug("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
Mario Sixc0350fb2018-01-26 14:43:55 +0100369 cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100370 flash_write8(cword.w8, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200371 break;
372 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +0100373 debug("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100374 cmd, cword.w16,
375 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100376 flash_write16(cword.w16, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200377 break;
378 case FLASH_CFI_32BIT:
Mario Six188a5562018-01-26 14:43:31 +0100379 debug("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr,
Mario Sixc0350fb2018-01-26 14:43:55 +0100380 cmd, cword.w32,
381 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ryan Harkin622b9522015-10-23 16:50:51 +0100382 flash_write32(cword.w32, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200383 break;
384 case FLASH_CFI_64BIT:
385#ifdef DEBUG
386 {
387 char str[20];
388
Mario Six188a5562018-01-26 14:43:31 +0100389 print_longlong(str, cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200390
Mario Six188a5562018-01-26 14:43:31 +0100391 debug("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
Mario Sixc0350fb2018-01-26 14:43:55 +0100392 addr, cmd, str,
393 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200394 }
395#endif
Ryan Harkin622b9522015-10-23 16:50:51 +0100396 flash_write64(cword.w64, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200397 break;
398 }
399
400 /* Ensure all the instructions are fully finished */
401 sync();
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100402
403 flash_unmap(info, sect, offset, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200404}
405
Mario Sixca2b07a2018-01-26 14:43:32 +0100406static void flash_unlock_seq(flash_info_t *info, flash_sect_t sect)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200407{
Mario Six188a5562018-01-26 14:43:31 +0100408 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
409 flash_write_cmd(info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200410}
411
412/*-----------------------------------------------------------------------
413 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100414static int flash_isequal(flash_info_t *info, flash_sect_t sect, uint offset,
415 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200416{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100417 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200418 cfiword_t cword;
419 int retval;
420
Mario Six188a5562018-01-26 14:43:31 +0100421 addr = flash_map(info, sect, offset);
422 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200423
Mario Six188a5562018-01-26 14:43:31 +0100424 debug("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200425 switch (info->portwidth) {
426 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +0100427 debug("is= %x %x\n", flash_read8(addr), cword.w8);
Ryan Harkin622b9522015-10-23 16:50:51 +0100428 retval = (flash_read8(addr) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200429 break;
430 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +0100431 debug("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16);
Ryan Harkin622b9522015-10-23 16:50:51 +0100432 retval = (flash_read16(addr) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200433 break;
434 case FLASH_CFI_32BIT:
Mario Six188a5562018-01-26 14:43:31 +0100435 debug("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32);
Ryan Harkin622b9522015-10-23 16:50:51 +0100436 retval = (flash_read32(addr) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200437 break;
438 case FLASH_CFI_64BIT:
439#ifdef DEBUG
440 {
441 char str1[20];
442 char str2[20];
443
Mario Six188a5562018-01-26 14:43:31 +0100444 print_longlong(str1, flash_read64(addr));
445 print_longlong(str2, cword.w64);
446 debug("is= %s %s\n", str1, str2);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200447 }
448#endif
Ryan Harkin622b9522015-10-23 16:50:51 +0100449 retval = (flash_read64(addr) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200450 break;
451 default:
452 retval = 0;
453 break;
454 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100455 flash_unmap(info, sect, offset, addr);
456
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200457 return retval;
458}
459
460/*-----------------------------------------------------------------------
461 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100462static int flash_isset(flash_info_t *info, flash_sect_t sect, uint offset,
463 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200464{
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100465 void *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200466 cfiword_t cword;
467 int retval;
468
Mario Six188a5562018-01-26 14:43:31 +0100469 addr = flash_map(info, sect, offset);
470 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200471 switch (info->portwidth) {
472 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100473 retval = ((flash_read8(addr) & cword.w8) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200474 break;
475 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100476 retval = ((flash_read16(addr) & cword.w16) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200477 break;
478 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100479 retval = ((flash_read32(addr) & cword.w32) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200480 break;
481 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100482 retval = ((flash_read64(addr) & cword.w64) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200483 break;
484 default:
485 retval = 0;
486 break;
487 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100488 flash_unmap(info, sect, offset, addr);
489
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200490 return retval;
491}
492
493/*-----------------------------------------------------------------------
494 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100495static int flash_toggle(flash_info_t *info, flash_sect_t sect, uint offset,
496 uchar cmd)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200497{
Mario Six53128382018-01-26 14:43:49 +0100498 u8 *addr;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200499 cfiword_t cword;
500 int retval;
501
Mario Six188a5562018-01-26 14:43:31 +0100502 addr = flash_map(info, sect, offset);
503 flash_make_cmd(info, cmd, &cword);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200504 switch (info->portwidth) {
505 case FLASH_CFI_8BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200506 retval = flash_read8(addr) != flash_read8(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200507 break;
508 case FLASH_CFI_16BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200509 retval = flash_read16(addr) != flash_read16(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200510 break;
511 case FLASH_CFI_32BIT:
Stefan Roesefb8c0612008-06-16 10:40:02 +0200512 retval = flash_read32(addr) != flash_read32(addr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200513 break;
514 case FLASH_CFI_64BIT:
Mario Sixb1683862018-01-26 14:43:33 +0100515 retval = ((flash_read32(addr) != flash_read32(addr)) ||
Mario Six640f4e32018-01-26 14:43:36 +0100516 (flash_read32(addr + 4) != flash_read32(addr + 4)));
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200517 break;
518 default:
519 retval = 0;
520 break;
521 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100522 flash_unmap(info, sect, offset, addr);
523
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200524 return retval;
525}
526
527/*
528 * flash_is_busy - check to see if the flash is busy
529 *
530 * This routine checks the status of the chip and returns true if the
531 * chip is busy.
532 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100533static int flash_is_busy(flash_info_t *info, flash_sect_t sect)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200534{
535 int retval;
536
537 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400538 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200539 case CFI_CMDSET_INTEL_STANDARD:
540 case CFI_CMDSET_INTEL_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +0100541 retval = !flash_isset(info, sect, 0, FLASH_STATUS_DONE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200542 break;
543 case CFI_CMDSET_AMD_STANDARD:
544 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100545#ifdef CONFIG_FLASH_CFI_LEGACY
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200546 case CFI_CMDSET_AMD_LEGACY:
547#endif
Marek Vasut72443c72017-09-12 19:09:31 +0200548 if (info->sr_supported) {
Mario Six188a5562018-01-26 14:43:31 +0100549 flash_write_cmd(info, sect, info->addr_unlock1,
Mario Sixc0350fb2018-01-26 14:43:55 +0100550 FLASH_CMD_READ_STATUS);
Mario Six188a5562018-01-26 14:43:31 +0100551 retval = !flash_isset(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100552 FLASH_STATUS_DONE);
Marek Vasut72443c72017-09-12 19:09:31 +0200553 } else {
Mario Six188a5562018-01-26 14:43:31 +0100554 retval = flash_toggle(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100555 AMD_STATUS_TOGGLE);
Marek Vasut72443c72017-09-12 19:09:31 +0200556 }
557
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200558 break;
559 default:
560 retval = 0;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100561 }
Mario Six38d28312018-01-26 14:43:40 +0100562 debug("%s: %d\n", __func__, retval);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200563 return retval;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100564}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200565
566/*-----------------------------------------------------------------------
567 * wait for XSR.7 to be set. Time out with an error if it does not.
568 * This routine does not set the flash to read-array mode.
569 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100570static int flash_status_check(flash_info_t *info, flash_sect_t sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100571 ulong tout, char *prompt)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200572{
573 ulong start;
574
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200575#if CONFIG_SYS_HZ != 1000
Mario Sixddcf0542018-01-26 14:43:54 +0100576 /* Avoid overflow for large HZ */
Renato Andreolac40c94a2010-03-24 23:00:47 +0800577 if ((ulong)CONFIG_SYS_HZ > 100000)
Mario Sixddcf0542018-01-26 14:43:54 +0100578 tout *= (ulong)CONFIG_SYS_HZ / 1000;
Renato Andreolac40c94a2010-03-24 23:00:47 +0800579 else
580 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200581#endif
582
583 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000584#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800585 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000586#endif
Mario Six188a5562018-01-26 14:43:31 +0100587 start = get_timer(0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100588 WATCHDOG_RESET();
Mario Six188a5562018-01-26 14:43:31 +0100589 while (flash_is_busy(info, sector)) {
590 if (get_timer(start) > tout) {
591 printf("Flash %s timeout at address %lx data %lx\n",
Mario Sixc0350fb2018-01-26 14:43:55 +0100592 prompt, info->start[sector],
593 flash_read_long(info, sector, 0));
Mario Six188a5562018-01-26 14:43:31 +0100594 flash_write_cmd(info, sector, 0, info->cmd_reset);
Stefan Roesee303be22013-04-12 19:04:54 +0200595 udelay(1);
Mario Six9dbaebc2018-01-26 14:43:52 +0100596 return ERR_TIMEOUT;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200597 }
Mario Six188a5562018-01-26 14:43:31 +0100598 udelay(1); /* also triggers watchdog */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200599 }
600 return ERR_OK;
601}
602
603/*-----------------------------------------------------------------------
604 * Wait for XSR.7 to be set, if it times out print an error, otherwise
605 * do a full status check.
606 *
607 * This routine sets the flash to read-array mode.
608 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100609static int flash_full_status_check(flash_info_t *info, flash_sect_t sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100610 ulong tout, char *prompt)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200611{
612 int retcode;
613
Mario Six188a5562018-01-26 14:43:31 +0100614 retcode = flash_status_check(info, sector, tout, prompt);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200615 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400616 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200617 case CFI_CMDSET_INTEL_EXTENDED:
618 case CFI_CMDSET_INTEL_STANDARD:
Mario Six4f89da42018-01-26 14:43:42 +0100619 if (retcode == ERR_OK &&
Mario Sixc0350fb2018-01-26 14:43:55 +0100620 !flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200621 retcode = ERR_INVAL;
Mario Six188a5562018-01-26 14:43:31 +0100622 printf("Flash %s error at address %lx\n", prompt,
Mario Sixc0350fb2018-01-26 14:43:55 +0100623 info->start[sector]);
Mario Six188a5562018-01-26 14:43:31 +0100624 if (flash_isset(info, sector, 0, FLASH_STATUS_ECLBS |
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200625 FLASH_STATUS_PSLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100626 puts("Command Sequence Error.\n");
627 } else if (flash_isset(info, sector, 0,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200628 FLASH_STATUS_ECLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100629 puts("Block Erase Error.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200630 retcode = ERR_NOT_ERASED;
Mario Six188a5562018-01-26 14:43:31 +0100631 } else if (flash_isset(info, sector, 0,
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200632 FLASH_STATUS_PSLBS)) {
Mario Six188a5562018-01-26 14:43:31 +0100633 puts("Locking Error\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200634 }
Mario Six188a5562018-01-26 14:43:31 +0100635 if (flash_isset(info, sector, 0, FLASH_STATUS_DPS)) {
636 puts("Block locked.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200637 retcode = ERR_PROTECTED;
638 }
Mario Six188a5562018-01-26 14:43:31 +0100639 if (flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
640 puts("Vpp Low Error.\n");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200641 }
Mario Six188a5562018-01-26 14:43:31 +0100642 flash_write_cmd(info, sector, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -0700643 udelay(1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200644 break;
645 default:
646 break;
647 }
648 return retcode;
649}
650
Thomas Choue5720822010-03-26 08:17:00 +0800651static int use_flash_status_poll(flash_info_t *info)
652{
653#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
654 if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
655 info->vendor == CFI_CMDSET_AMD_STANDARD)
656 return 1;
657#endif
658 return 0;
659}
660
661static int flash_status_poll(flash_info_t *info, void *src, void *dst,
662 ulong tout, char *prompt)
663{
664#ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
665 ulong start;
666 int ready;
667
668#if CONFIG_SYS_HZ != 1000
Mario Sixddcf0542018-01-26 14:43:54 +0100669 /* Avoid overflow for large HZ */
Thomas Choue5720822010-03-26 08:17:00 +0800670 if ((ulong)CONFIG_SYS_HZ > 100000)
Mario Sixddcf0542018-01-26 14:43:54 +0100671 tout *= (ulong)CONFIG_SYS_HZ / 1000;
Thomas Choue5720822010-03-26 08:17:00 +0800672 else
673 tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
674#endif
675
676 /* Wait for command completion */
Graeme Russe110c4f2011-07-15 02:18:56 +0000677#ifdef CONFIG_SYS_LOW_RES_TIMER
Thomas Chou22d6c8f2010-04-01 11:15:05 +0800678 reset_timer();
Graeme Russe110c4f2011-07-15 02:18:56 +0000679#endif
Thomas Choue5720822010-03-26 08:17:00 +0800680 start = get_timer(0);
Jens Scharsig (BuS Elektronik)a9f5fab2012-01-27 09:29:53 +0100681 WATCHDOG_RESET();
Thomas Choue5720822010-03-26 08:17:00 +0800682 while (1) {
683 switch (info->portwidth) {
684 case FLASH_CFI_8BIT:
685 ready = flash_read8(dst) == flash_read8(src);
686 break;
687 case FLASH_CFI_16BIT:
688 ready = flash_read16(dst) == flash_read16(src);
689 break;
690 case FLASH_CFI_32BIT:
691 ready = flash_read32(dst) == flash_read32(src);
692 break;
693 case FLASH_CFI_64BIT:
694 ready = flash_read64(dst) == flash_read64(src);
695 break;
696 default:
697 ready = 0;
698 break;
699 }
700 if (ready)
701 break;
702 if (get_timer(start) > tout) {
703 printf("Flash %s timeout at address %lx data %lx\n",
704 prompt, (ulong)dst, (ulong)flash_read8(dst));
Mario Six9dbaebc2018-01-26 14:43:52 +0100705 return ERR_TIMEOUT;
Thomas Choue5720822010-03-26 08:17:00 +0800706 }
707 udelay(1); /* also triggers watchdog */
708 }
709#endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
710 return ERR_OK;
711}
712
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200713/*-----------------------------------------------------------------------
714 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100715static void flash_add_byte(flash_info_t *info, cfiword_t *cword, uchar c)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200716{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200717#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200718 unsigned short w;
719 unsigned int l;
720 unsigned long long ll;
721#endif
722
723 switch (info->portwidth) {
724 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100725 cword->w8 = c;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200726 break;
727 case FLASH_CFI_16BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200728#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200729 w = c;
730 w <<= 8;
Ryan Harkin622b9522015-10-23 16:50:51 +0100731 cword->w16 = (cword->w16 >> 8) | w;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100732#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100733 cword->w16 = (cword->w16 << 8) | c;
Michael Schwingen81b20cc2007-12-07 23:35:02 +0100734#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200735 break;
736 case FLASH_CFI_32BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200737#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200738 l = c;
739 l <<= 24;
Ryan Harkin622b9522015-10-23 16:50:51 +0100740 cword->w32 = (cword->w32 >> 8) | l;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200741#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100742 cword->w32 = (cword->w32 << 8) | c;
Stefan Roese2662b402006-04-01 13:41:03 +0200743#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200744 break;
745 case FLASH_CFI_64BIT:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200746#if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200747 ll = c;
748 ll <<= 56;
Ryan Harkin622b9522015-10-23 16:50:51 +0100749 cword->w64 = (cword->w64 >> 8) | ll;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200750#else
Ryan Harkin622b9522015-10-23 16:50:51 +0100751 cword->w64 = (cword->w64 << 8) | c;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200752#endif
753 break;
wdenk5653fc32004-02-08 22:55:38 +0000754 }
wdenk5653fc32004-02-08 22:55:38 +0000755}
756
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100757/*
758 * Loop through the sector table starting from the previously found sector.
759 * Searches forwards or backwards, dependent on the passed address.
wdenk5653fc32004-02-08 22:55:38 +0000760 */
Mario Sixca2b07a2018-01-26 14:43:32 +0100761static flash_sect_t find_sector(flash_info_t *info, ulong addr)
wdenk7680c142005-05-16 15:23:22 +0000762{
Kim Phillips11dc4012012-10-29 13:34:45 +0000763 static flash_sect_t saved_sector; /* previously found sector */
Stefan Roesee303be22013-04-12 19:04:54 +0200764 static flash_info_t *saved_info; /* previously used flash bank */
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100765 flash_sect_t sector = saved_sector;
wdenk7680c142005-05-16 15:23:22 +0000766
Mario Six4f89da42018-01-26 14:43:42 +0100767 if (info != saved_info || sector >= info->sector_count)
Stefan Roesee303be22013-04-12 19:04:54 +0200768 sector = 0;
769
Mario Six5701ba82018-01-26 14:43:53 +0100770 while ((sector < info->sector_count - 1) &&
Mario Sixc0350fb2018-01-26 14:43:55 +0100771 (info->start[sector] < addr))
Jens Gehrlein0f8e8512008-12-16 17:25:55 +0100772 sector++;
773 while ((info->start[sector] > addr) && (sector > 0))
774 /*
775 * also decrements the sector in case of an overshot
776 * in the first loop
777 */
778 sector--;
779
780 saved_sector = sector;
Stefan Roesee303be22013-04-12 19:04:54 +0200781 saved_info = info;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200782 return sector;
wdenk7680c142005-05-16 15:23:22 +0000783}
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200784
785/*-----------------------------------------------------------------------
786 */
Mario Sixc0350fb2018-01-26 14:43:55 +0100787static int flash_write_cfiword(flash_info_t *info, ulong dest, cfiword_t cword)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200788{
Becky Bruce09ce9922009-02-02 16:34:51 -0600789 void *dstaddr = (void *)dest;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200790 int flag;
Jens Gehrleina7292872008-12-16 17:25:54 +0100791 flash_sect_t sect = 0;
792 char sect_found = 0;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200793
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200794 /* Check if Flash is (sufficiently) erased */
795 switch (info->portwidth) {
796 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100797 flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200798 break;
799 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100800 flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200801 break;
802 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100803 flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200804 break;
805 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100806 flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200807 break;
808 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100809 flag = 0;
810 break;
811 }
Becky Bruce09ce9922009-02-02 16:34:51 -0600812 if (!flag)
Stefan Roese0dc80e22007-12-27 07:50:54 +0100813 return ERR_NOT_ERASED;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200814
815 /* Disable interrupts which might cause a timeout here */
Mario Six188a5562018-01-26 14:43:31 +0100816 flag = disable_interrupts();
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200817
818 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400819 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200820 case CFI_CMDSET_INTEL_EXTENDED:
821 case CFI_CMDSET_INTEL_STANDARD:
Mario Six188a5562018-01-26 14:43:31 +0100822 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
823 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200824 break;
825 case CFI_CMDSET_AMD_EXTENDED:
826 case CFI_CMDSET_AMD_STANDARD:
Ed Swarthout0d01f662008-10-09 01:26:36 -0500827 sect = find_sector(info, dest);
Mario Six188a5562018-01-26 14:43:31 +0100828 flash_unlock_seq(info, sect);
829 flash_write_cmd(info, sect, info->addr_unlock1, AMD_CMD_WRITE);
Jens Gehrleina7292872008-12-16 17:25:54 +0100830 sect_found = 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200831 break;
Po-Yu Chuangb4db4a72009-07-10 18:03:57 +0800832#ifdef CONFIG_FLASH_CFI_LEGACY
833 case CFI_CMDSET_AMD_LEGACY:
834 sect = find_sector(info, dest);
Mario Six188a5562018-01-26 14:43:31 +0100835 flash_unlock_seq(info, 0);
836 flash_write_cmd(info, 0, info->addr_unlock1, AMD_CMD_WRITE);
Po-Yu Chuangb4db4a72009-07-10 18:03:57 +0800837 sect_found = 1;
838 break;
839#endif
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200840 }
841
842 switch (info->portwidth) {
843 case FLASH_CFI_8BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100844 flash_write8(cword.w8, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200845 break;
846 case FLASH_CFI_16BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100847 flash_write16(cword.w16, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200848 break;
849 case FLASH_CFI_32BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100850 flash_write32(cword.w32, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200851 break;
852 case FLASH_CFI_64BIT:
Ryan Harkin622b9522015-10-23 16:50:51 +0100853 flash_write64(cword.w64, dstaddr);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200854 break;
855 }
856
857 /* re-enable interrupts if necessary */
858 if (flag)
Mario Six188a5562018-01-26 14:43:31 +0100859 enable_interrupts();
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200860
Jens Gehrleina7292872008-12-16 17:25:54 +0100861 if (!sect_found)
Mario Six188a5562018-01-26 14:43:31 +0100862 sect = find_sector(info, dest);
Jens Gehrleina7292872008-12-16 17:25:54 +0100863
Thomas Choue5720822010-03-26 08:17:00 +0800864 if (use_flash_status_poll(info))
865 return flash_status_poll(info, &cword, dstaddr,
866 info->write_tout, "write");
867 else
868 return flash_full_status_check(info, sect,
869 info->write_tout, "write");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200870}
871
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200872#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200873
Mario Sixca2b07a2018-01-26 14:43:32 +0100874static int flash_write_cfibuffer(flash_info_t *info, ulong dest, uchar *cp,
Mario Sixc0350fb2018-01-26 14:43:55 +0100875 int len)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200876{
877 flash_sect_t sector;
878 int cnt;
879 int retcode;
Mario Six53128382018-01-26 14:43:49 +0100880 u8 *src = cp;
881 u8 *dst = (u8 *)dest;
882 u8 *dst2 = dst;
Tao Hou85c344e2012-03-15 23:33:58 +0800883 int flag = 1;
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200884 uint offset = 0;
885 unsigned int shift;
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400886 uchar write_cmd;
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100887
Stefan Roese0dc80e22007-12-27 07:50:54 +0100888 switch (info->portwidth) {
889 case FLASH_CFI_8BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200890 shift = 0;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100891 break;
892 case FLASH_CFI_16BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200893 shift = 1;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100894 break;
895 case FLASH_CFI_32BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200896 shift = 2;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100897 break;
898 case FLASH_CFI_64BIT:
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200899 shift = 3;
Stefan Roese0dc80e22007-12-27 07:50:54 +0100900 break;
901 default:
902 retcode = ERR_INVAL;
903 goto out_unmap;
904 }
905
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200906 cnt = len >> shift;
907
Tao Hou85c344e2012-03-15 23:33:58 +0800908 while ((cnt-- > 0) && (flag == 1)) {
Stefan Roese0dc80e22007-12-27 07:50:54 +0100909 switch (info->portwidth) {
910 case FLASH_CFI_8BIT:
911 flag = ((flash_read8(dst2) & flash_read8(src)) ==
912 flash_read8(src));
913 src += 1, dst2 += 1;
914 break;
915 case FLASH_CFI_16BIT:
916 flag = ((flash_read16(dst2) & flash_read16(src)) ==
917 flash_read16(src));
918 src += 2, dst2 += 2;
919 break;
920 case FLASH_CFI_32BIT:
921 flag = ((flash_read32(dst2) & flash_read32(src)) ==
922 flash_read32(src));
923 src += 4, dst2 += 4;
924 break;
925 case FLASH_CFI_64BIT:
926 flag = ((flash_read64(dst2) & flash_read64(src)) ==
927 flash_read64(src));
928 src += 8, dst2 += 8;
929 break;
930 }
931 }
932 if (!flag) {
933 retcode = ERR_NOT_ERASED;
934 goto out_unmap;
935 }
936
937 src = cp;
Mario Six188a5562018-01-26 14:43:31 +0100938 sector = find_sector(info, dest);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200939
940 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400941 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200942 case CFI_CMDSET_INTEL_STANDARD:
943 case CFI_CMDSET_INTEL_EXTENDED:
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +0400944 write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
Mario Sixddcf0542018-01-26 14:43:54 +0100945 FLASH_CMD_WRITE_BUFFER_PROG :
946 FLASH_CMD_WRITE_TO_BUFFER;
Mario Six188a5562018-01-26 14:43:31 +0100947 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
948 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
949 flash_write_cmd(info, sector, 0, write_cmd);
950 retcode = flash_status_check(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +0100951 info->buffer_write_tout,
952 "write to buffer");
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200953 if (retcode == ERR_OK) {
954 /* reduce the number of loops by the width of
Mario Sixa6d18f22018-01-26 14:43:41 +0100955 * the port
956 */
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200957 cnt = len >> shift;
Mario Six188a5562018-01-26 14:43:31 +0100958 flash_write_cmd(info, sector, 0, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200959 while (cnt-- > 0) {
960 switch (info->portwidth) {
961 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100962 flash_write8(flash_read8(src), dst);
963 src += 1, dst += 1;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200964 break;
965 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100966 flash_write16(flash_read16(src), dst);
967 src += 2, dst += 2;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200968 break;
969 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100970 flash_write32(flash_read32(src), dst);
971 src += 4, dst += 4;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200972 break;
973 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +0100974 flash_write64(flash_read64(src), dst);
975 src += 8, dst += 8;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200976 break;
977 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100978 retcode = ERR_INVAL;
979 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200980 }
981 }
Mario Six188a5562018-01-26 14:43:31 +0100982 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +0100983 FLASH_CMD_WRITE_BUFFER_CONFIRM);
Mario Six188a5562018-01-26 14:43:31 +0100984 retcode = flash_full_status_check(
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200985 info, sector, info->buffer_write_tout,
986 "buffer write");
987 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +0100988
989 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +0200990
991 case CFI_CMDSET_AMD_STANDARD:
992 case CFI_CMDSET_AMD_EXTENDED:
Rouven Behr7570a0c2016-04-10 13:38:13 +0200993 flash_unlock_seq(info, sector);
Guennadi Liakhovetski96ef8312008-04-03 13:36:02 +0200994
995#ifdef CONFIG_FLASH_SPANSION_S29WS_N
996 offset = ((unsigned long)dst - info->start[sector]) >> shift;
997#endif
998 flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
999 cnt = len >> shift;
John Schmoller7dedefd2009-08-12 10:55:47 -05001000 flash_write_cmd(info, sector, offset, cnt - 1);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001001
1002 switch (info->portwidth) {
1003 case FLASH_CFI_8BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001004 while (cnt-- > 0) {
1005 flash_write8(flash_read8(src), dst);
1006 src += 1, dst += 1;
1007 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001008 break;
1009 case FLASH_CFI_16BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001010 while (cnt-- > 0) {
1011 flash_write16(flash_read16(src), dst);
1012 src += 2, dst += 2;
1013 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001014 break;
1015 case FLASH_CFI_32BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001016 while (cnt-- > 0) {
1017 flash_write32(flash_read32(src), dst);
1018 src += 4, dst += 4;
1019 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001020 break;
1021 case FLASH_CFI_64BIT:
Haavard Skinnemoencdbaefb2007-12-13 12:56:32 +01001022 while (cnt-- > 0) {
1023 flash_write64(flash_read64(src), dst);
1024 src += 8, dst += 8;
1025 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001026 break;
1027 default:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001028 retcode = ERR_INVAL;
1029 goto out_unmap;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001030 }
1031
Mario Six188a5562018-01-26 14:43:31 +01001032 flash_write_cmd(info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
Thomas Choue5720822010-03-26 08:17:00 +08001033 if (use_flash_status_poll(info))
1034 retcode = flash_status_poll(info, src - (1 << shift),
1035 dst - (1 << shift),
1036 info->buffer_write_tout,
1037 "buffer write");
1038 else
1039 retcode = flash_full_status_check(info, sector,
1040 info->buffer_write_tout,
1041 "buffer write");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001042 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001043
1044 default:
Mario Six188a5562018-01-26 14:43:31 +01001045 debug("Unknown Command Set\n");
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001046 retcode = ERR_INVAL;
1047 break;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001048 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001049
1050out_unmap:
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001051 return retcode;
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001052}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001053#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001054
wdenk7680c142005-05-16 15:23:22 +00001055/*-----------------------------------------------------------------------
1056 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001057int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenk5653fc32004-02-08 22:55:38 +00001058{
1059 int rcode = 0;
1060 int prot;
1061 flash_sect_t sect;
Thomas Choue5720822010-03-26 08:17:00 +08001062 int st;
wdenk5653fc32004-02-08 22:55:38 +00001063
wdenkbf9e3b32004-02-12 00:47:09 +00001064 if (info->flash_id != FLASH_MAN_CFI) {
Mario Six188a5562018-01-26 14:43:31 +01001065 puts("Can't erase unknown flash type - aborted\n");
wdenk5653fc32004-02-08 22:55:38 +00001066 return 1;
1067 }
Mario Six4f89da42018-01-26 14:43:42 +01001068 if (s_first < 0 || s_first > s_last) {
Mario Six188a5562018-01-26 14:43:31 +01001069 puts("- no sectors to erase\n");
wdenk5653fc32004-02-08 22:55:38 +00001070 return 1;
1071 }
1072
1073 prot = 0;
Mario Six0412e902018-01-26 14:43:38 +01001074 for (sect = s_first; sect <= s_last; ++sect)
1075 if (info->protect[sect])
wdenk5653fc32004-02-08 22:55:38 +00001076 prot++;
wdenk5653fc32004-02-08 22:55:38 +00001077 if (prot) {
Mario Six188a5562018-01-26 14:43:31 +01001078 printf("- Warning: %d protected sectors will not be erased!\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001079 prot);
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001080 } else if (flash_verbose) {
Mario Six188a5562018-01-26 14:43:31 +01001081 putc('\n');
wdenk5653fc32004-02-08 22:55:38 +00001082 }
1083
wdenkbf9e3b32004-02-12 00:47:09 +00001084 for (sect = s_first; sect <= s_last; sect++) {
Joe Hershbergerde15a062012-08-17 15:36:41 -05001085 if (ctrlc()) {
1086 printf("\n");
1087 return 1;
1088 }
1089
wdenk5653fc32004-02-08 22:55:38 +00001090 if (info->protect[sect] == 0) { /* not protected */
Joe Hershberger6822a642012-08-17 15:36:40 -05001091#ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
1092 int k;
1093 int size;
1094 int erased;
1095 u32 *flash;
1096
1097 /*
1098 * Check if whole sector is erased
1099 */
1100 size = flash_sector_size(info, sect);
1101 erased = 1;
1102 flash = (u32 *)info->start[sect];
1103 /* divide by 4 for longword access */
1104 size = size >> 2;
1105 for (k = 0; k < size; k++) {
1106 if (flash_read32(flash++) != 0xffffffff) {
1107 erased = 0;
1108 break;
1109 }
1110 }
1111 if (erased) {
1112 if (flash_verbose)
1113 putc(',');
1114 continue;
1115 }
1116#endif
wdenkbf9e3b32004-02-12 00:47:09 +00001117 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001118 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00001119 case CFI_CMDSET_INTEL_STANDARD:
1120 case CFI_CMDSET_INTEL_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +01001121 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001122 FLASH_CMD_CLEAR_STATUS);
Mario Six188a5562018-01-26 14:43:31 +01001123 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001124 FLASH_CMD_BLOCK_ERASE);
Mario Six188a5562018-01-26 14:43:31 +01001125 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001126 FLASH_CMD_ERASE_CONFIRM);
wdenk5653fc32004-02-08 22:55:38 +00001127 break;
1128 case CFI_CMDSET_AMD_STANDARD:
1129 case CFI_CMDSET_AMD_EXTENDED:
Mario Six188a5562018-01-26 14:43:31 +01001130 flash_unlock_seq(info, sect);
1131 flash_write_cmd(info, sect,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001132 info->addr_unlock1,
1133 AMD_CMD_ERASE_START);
Mario Six188a5562018-01-26 14:43:31 +01001134 flash_unlock_seq(info, sect);
1135 flash_write_cmd(info, sect, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001136 info->cmd_erase_sector);
wdenk5653fc32004-02-08 22:55:38 +00001137 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001138#ifdef CONFIG_FLASH_CFI_LEGACY
1139 case CFI_CMDSET_AMD_LEGACY:
Mario Six188a5562018-01-26 14:43:31 +01001140 flash_unlock_seq(info, 0);
1141 flash_write_cmd(info, 0, info->addr_unlock1,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001142 AMD_CMD_ERASE_START);
Mario Six188a5562018-01-26 14:43:31 +01001143 flash_unlock_seq(info, 0);
1144 flash_write_cmd(info, sect, 0,
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001145 AMD_CMD_ERASE_SECTOR);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001146 break;
1147#endif
wdenk5653fc32004-02-08 22:55:38 +00001148 default:
Mario Six9f720212018-01-26 14:43:44 +01001149 debug("Unknown flash vendor %d\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001150 info->vendor);
wdenk5653fc32004-02-08 22:55:38 +00001151 break;
1152 }
1153
Thomas Choue5720822010-03-26 08:17:00 +08001154 if (use_flash_status_poll(info)) {
Kim Phillips11dc4012012-10-29 13:34:45 +00001155 cfiword_t cword;
Thomas Choue5720822010-03-26 08:17:00 +08001156 void *dest;
Mario Six7223a8c2018-01-26 14:43:37 +01001157
Ryan Harkin622b9522015-10-23 16:50:51 +01001158 cword.w64 = 0xffffffffffffffffULL;
Thomas Choue5720822010-03-26 08:17:00 +08001159 dest = flash_map(info, sect, 0);
1160 st = flash_status_poll(info, &cword, dest,
Mario Sixddcf0542018-01-26 14:43:54 +01001161 info->erase_blk_tout,
1162 "erase");
Thomas Choue5720822010-03-26 08:17:00 +08001163 flash_unmap(info, sect, 0, dest);
Mario Six12d7fed2018-01-26 14:43:43 +01001164 } else {
Thomas Choue5720822010-03-26 08:17:00 +08001165 st = flash_full_status_check(info, sect,
1166 info->erase_blk_tout,
1167 "erase");
Mario Six12d7fed2018-01-26 14:43:43 +01001168 }
1169
Thomas Choue5720822010-03-26 08:17:00 +08001170 if (st)
wdenk5653fc32004-02-08 22:55:38 +00001171 rcode = 1;
Thomas Choue5720822010-03-26 08:17:00 +08001172 else if (flash_verbose)
Mario Six188a5562018-01-26 14:43:31 +01001173 putc('.');
wdenk5653fc32004-02-08 22:55:38 +00001174 }
1175 }
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001176
1177 if (flash_verbose)
Mario Six188a5562018-01-26 14:43:31 +01001178 puts(" done\n");
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001179
wdenk5653fc32004-02-08 22:55:38 +00001180 return rcode;
1181}
1182
Stefan Roese70084df2010-08-13 09:36:36 +02001183#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1184static int sector_erased(flash_info_t *info, int i)
1185{
1186 int k;
1187 int size;
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001188 u32 *flash;
Stefan Roese70084df2010-08-13 09:36:36 +02001189
1190 /*
1191 * Check if whole sector is erased
1192 */
1193 size = flash_sector_size(info, i);
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001194 flash = (u32 *)info->start[i];
Stefan Roese70084df2010-08-13 09:36:36 +02001195 /* divide by 4 for longword access */
1196 size = size >> 2;
1197
1198 for (k = 0; k < size; k++) {
Stefan Roese4d2ca9d2010-10-25 18:31:39 +02001199 if (flash_read32(flash++) != 0xffffffff)
Stefan Roese70084df2010-08-13 09:36:36 +02001200 return 0; /* not erased */
1201 }
1202
1203 return 1; /* erased */
1204}
1205#endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
1206
Mario Sixca2b07a2018-01-26 14:43:32 +01001207void flash_print_info(flash_info_t *info)
wdenk5653fc32004-02-08 22:55:38 +00001208{
1209 int i;
1210
1211 if (info->flash_id != FLASH_MAN_CFI) {
Mario Six188a5562018-01-26 14:43:31 +01001212 puts("missing or unknown FLASH type\n");
wdenk5653fc32004-02-08 22:55:38 +00001213 return;
1214 }
1215
Mario Six188a5562018-01-26 14:43:31 +01001216 printf("%s flash (%d x %d)",
Mario Sixc0350fb2018-01-26 14:43:55 +01001217 info->name,
1218 (info->portwidth << 3), (info->chipwidth << 3));
Mario Six640f4e32018-01-26 14:43:36 +01001219 if (info->size < 1024 * 1024)
Mario Six188a5562018-01-26 14:43:31 +01001220 printf(" Size: %ld kB in %d Sectors\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001221 info->size >> 10, info->sector_count);
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001222 else
Mario Six188a5562018-01-26 14:43:31 +01001223 printf(" Size: %ld MB in %d Sectors\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001224 info->size >> 20, info->sector_count);
Mario Six188a5562018-01-26 14:43:31 +01001225 printf(" ");
Stefan Roese260421a2006-11-13 13:55:24 +01001226 switch (info->vendor) {
Mario Sixdde09132018-01-26 14:43:35 +01001227 case CFI_CMDSET_INTEL_PROG_REGIONS:
1228 printf("Intel Prog Regions");
1229 break;
1230 case CFI_CMDSET_INTEL_STANDARD:
1231 printf("Intel Standard");
1232 break;
1233 case CFI_CMDSET_INTEL_EXTENDED:
1234 printf("Intel Extended");
1235 break;
1236 case CFI_CMDSET_AMD_STANDARD:
1237 printf("AMD Standard");
1238 break;
1239 case CFI_CMDSET_AMD_EXTENDED:
1240 printf("AMD Extended");
1241 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001242#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Sixdde09132018-01-26 14:43:35 +01001243 case CFI_CMDSET_AMD_LEGACY:
1244 printf("AMD Legacy");
1245 break;
Michael Schwingen81b20cc2007-12-07 23:35:02 +01001246#endif
Mario Sixdde09132018-01-26 14:43:35 +01001247 default:
1248 printf("Unknown (%d)", info->vendor);
1249 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001250 }
Mario Six188a5562018-01-26 14:43:31 +01001251 printf(" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
Mario Sixc0350fb2018-01-26 14:43:55 +01001252 info->manufacturer_id);
Mario Six188a5562018-01-26 14:43:31 +01001253 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
Mario Sixc0350fb2018-01-26 14:43:55 +01001254 info->device_id);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001255 if ((info->device_id & 0xff) == 0x7E) {
1256 printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
Mario Sixc0350fb2018-01-26 14:43:55 +01001257 info->device_id2);
Stefan Roese260421a2006-11-13 13:55:24 +01001258 }
Mario Six4f89da42018-01-26 14:43:42 +01001259 if (info->vendor == CFI_CMDSET_AMD_STANDARD && info->legacy_unlock)
Stefan Roesed2af0282012-12-06 15:44:12 +01001260 printf("\n Advanced Sector Protection (PPB) enabled");
Mario Six188a5562018-01-26 14:43:31 +01001261 printf("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001262 info->erase_blk_tout, info->write_tout);
Stefan Roese260421a2006-11-13 13:55:24 +01001263 if (info->buffer_size > 1) {
Mario Six876c52f2018-01-26 14:43:50 +01001264 printf(" Buffer write timeout: %ld ms, ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001265 info->buffer_write_tout);
Mario Six876c52f2018-01-26 14:43:50 +01001266 printf("buffer size: %d bytes\n", info->buffer_size);
Stefan Roese260421a2006-11-13 13:55:24 +01001267 }
wdenk5653fc32004-02-08 22:55:38 +00001268
Mario Six188a5562018-01-26 14:43:31 +01001269 puts("\n Sector Start Addresses:");
wdenkbf9e3b32004-02-12 00:47:09 +00001270 for (i = 0; i < info->sector_count; ++i) {
Kim Phillips2e973942010-07-26 18:35:39 -05001271 if (ctrlc())
Stefan Roese70084df2010-08-13 09:36:36 +02001272 break;
Stefan Roese260421a2006-11-13 13:55:24 +01001273 if ((i % 5) == 0)
Stefan Roese70084df2010-08-13 09:36:36 +02001274 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001275#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
wdenk5653fc32004-02-08 22:55:38 +00001276 /* print empty and read-only info */
Mario Six188a5562018-01-26 14:43:31 +01001277 printf(" %08lX %c %s ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001278 info->start[i],
1279 sector_erased(info, i) ? 'E' : ' ',
1280 info->protect[i] ? "RO" : " ");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001281#else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
Mario Six188a5562018-01-26 14:43:31 +01001282 printf(" %08lX %s ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001283 info->start[i],
1284 info->protect[i] ? "RO" : " ");
wdenk5653fc32004-02-08 22:55:38 +00001285#endif
1286 }
Mario Six188a5562018-01-26 14:43:31 +01001287 putc('\n');
wdenk5653fc32004-02-08 22:55:38 +00001288}
1289
1290/*-----------------------------------------------------------------------
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001291 * This is used in a few places in write_buf() to show programming
1292 * progress. Making it a function is nasty because it needs to do side
1293 * effect updates to digit and dots. Repeated code is nasty too, so
1294 * we define it once here.
1295 */
Stefan Roesef0105722008-03-19 07:09:26 +01001296#ifdef CONFIG_FLASH_SHOW_PROGRESS
1297#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001298 if (flash_verbose) { \
1299 dots -= dots_sub; \
Mario Six4f89da42018-01-26 14:43:42 +01001300 if (scale > 0 && dots <= 0) { \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001301 if ((digit % 5) == 0) \
Mario Six188a5562018-01-26 14:43:31 +01001302 printf("%d", digit / 5); \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001303 else \
Mario Six188a5562018-01-26 14:43:31 +01001304 putc('.'); \
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01001305 digit--; \
1306 dots += scale; \
1307 } \
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001308 }
Stefan Roesef0105722008-03-19 07:09:26 +01001309#else
1310#define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1311#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001312
1313/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001314 * Copy memory to flash, returns:
1315 * 0 - OK
1316 * 1 - write timeout
1317 * 2 - Flash not erased
1318 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001319int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenk5653fc32004-02-08 22:55:38 +00001320{
1321 ulong wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001322 uchar *p;
wdenk5653fc32004-02-08 22:55:38 +00001323 int aln;
1324 cfiword_t cword;
1325 int i, rc;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001326#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001327 int buffered_size;
1328#endif
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001329#ifdef CONFIG_FLASH_SHOW_PROGRESS
1330 int digit = CONFIG_FLASH_SHOW_PROGRESS;
1331 int scale = 0;
1332 int dots = 0;
1333
1334 /*
1335 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1336 */
1337 if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1338 scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1339 CONFIG_FLASH_SHOW_PROGRESS);
1340 }
1341#endif
1342
wdenkbf9e3b32004-02-12 00:47:09 +00001343 /* get lower aligned address */
wdenk5653fc32004-02-08 22:55:38 +00001344 wp = (addr & ~(info->portwidth - 1));
1345
1346 /* handle unaligned start */
Mario Sixd3525b62018-01-26 14:43:48 +01001347 aln = addr - wp;
1348 if (aln != 0) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001349 cword.w32 = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001350 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001351 for (i = 0; i < aln; ++i)
Mario Six188a5562018-01-26 14:43:31 +01001352 flash_add_byte(info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001353
wdenkbf9e3b32004-02-12 00:47:09 +00001354 for (; (i < info->portwidth) && (cnt > 0); i++) {
Mario Six188a5562018-01-26 14:43:31 +01001355 flash_add_byte(info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001356 cnt--;
wdenk5653fc32004-02-08 22:55:38 +00001357 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001358 for (; (cnt == 0) && (i < info->portwidth); ++i)
Mario Six188a5562018-01-26 14:43:31 +01001359 flash_add_byte(info, &cword, flash_read8(p + i));
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001360
Mario Six188a5562018-01-26 14:43:31 +01001361 rc = flash_write_cfiword(info, wp, cword);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001362 if (rc != 0)
wdenk5653fc32004-02-08 22:55:38 +00001363 return rc;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001364
1365 wp += i;
Stefan Roesef0105722008-03-19 07:09:26 +01001366 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
wdenk5653fc32004-02-08 22:55:38 +00001367 }
1368
wdenkbf9e3b32004-02-12 00:47:09 +00001369 /* handle the aligned part */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001370#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
wdenkbf9e3b32004-02-12 00:47:09 +00001371 buffered_size = (info->portwidth / info->chipwidth);
1372 buffered_size *= info->buffer_size;
1373 while (cnt >= info->portwidth) {
Stefan Roese79b4cda2006-02-28 15:29:58 +01001374 /* prohibit buffer write when buffer_size is 1 */
1375 if (info->buffer_size == 1) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001376 cword.w32 = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01001377 for (i = 0; i < info->portwidth; i++)
Mario Six188a5562018-01-26 14:43:31 +01001378 flash_add_byte(info, &cword, *src++);
Mario Sixd3525b62018-01-26 14:43:48 +01001379 rc = flash_write_cfiword(info, wp, cword);
1380 if (rc != 0)
Stefan Roese79b4cda2006-02-28 15:29:58 +01001381 return rc;
1382 wp += info->portwidth;
1383 cnt -= info->portwidth;
1384 continue;
1385 }
1386
1387 /* write buffer until next buffered_size aligned boundary */
1388 i = buffered_size - (wp % buffered_size);
1389 if (i > cnt)
1390 i = cnt;
Mario Sixd3525b62018-01-26 14:43:48 +01001391 rc = flash_write_cfibuffer(info, wp, src, i);
1392 if (rc != ERR_OK)
wdenk5653fc32004-02-08 22:55:38 +00001393 return rc;
Wolfgang Denk8d4ba3d2005-08-12 22:35:59 +02001394 i -= i & (info->portwidth - 1);
wdenk5653fc32004-02-08 22:55:38 +00001395 wp += i;
1396 src += i;
wdenkbf9e3b32004-02-12 00:47:09 +00001397 cnt -= i;
Stefan Roesef0105722008-03-19 07:09:26 +01001398 FLASH_SHOW_PROGRESS(scale, dots, digit, i);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001399 /* Only check every once in a while */
1400 if ((cnt & 0xFFFF) < buffered_size && ctrlc())
1401 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001402 }
1403#else
wdenkbf9e3b32004-02-12 00:47:09 +00001404 while (cnt >= info->portwidth) {
Ryan Harkin622b9522015-10-23 16:50:51 +01001405 cword.w32 = 0;
Mario Six0412e902018-01-26 14:43:38 +01001406 for (i = 0; i < info->portwidth; i++)
Mario Six188a5562018-01-26 14:43:31 +01001407 flash_add_byte(info, &cword, *src++);
Mario Sixd3525b62018-01-26 14:43:48 +01001408 rc = flash_write_cfiword(info, wp, cword);
1409 if (rc != 0)
wdenk5653fc32004-02-08 22:55:38 +00001410 return rc;
1411 wp += info->portwidth;
1412 cnt -= info->portwidth;
Stefan Roesef0105722008-03-19 07:09:26 +01001413 FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
Joe Hershbergerde15a062012-08-17 15:36:41 -05001414 /* Only check every once in a while */
1415 if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
1416 return ERR_ABORTED;
wdenk5653fc32004-02-08 22:55:38 +00001417 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001418#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
Jerry Van Baren9a042e92008-03-08 13:48:01 -05001419
Mario Six0412e902018-01-26 14:43:38 +01001420 if (cnt == 0)
wdenk5653fc32004-02-08 22:55:38 +00001421 return (0);
wdenk5653fc32004-02-08 22:55:38 +00001422
1423 /*
1424 * handle unaligned tail bytes
1425 */
Ryan Harkin622b9522015-10-23 16:50:51 +01001426 cword.w32 = 0;
Becky Bruce09ce9922009-02-02 16:34:51 -06001427 p = (uchar *)wp;
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001428 for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
Mario Six188a5562018-01-26 14:43:31 +01001429 flash_add_byte(info, &cword, *src++);
wdenk5653fc32004-02-08 22:55:38 +00001430 --cnt;
1431 }
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001432 for (; i < info->portwidth; ++i)
Mario Six188a5562018-01-26 14:43:31 +01001433 flash_add_byte(info, &cword, flash_read8(p + i));
wdenk5653fc32004-02-08 22:55:38 +00001434
Mario Six188a5562018-01-26 14:43:31 +01001435 return flash_write_cfiword(info, wp, cword);
wdenk5653fc32004-02-08 22:55:38 +00001436}
1437
Stefan Roese20043a42012-12-06 15:44:09 +01001438static inline int manufact_match(flash_info_t *info, u32 manu)
1439{
1440 return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
1441}
1442
wdenk5653fc32004-02-08 22:55:38 +00001443/*-----------------------------------------------------------------------
1444 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001445#ifdef CONFIG_SYS_FLASH_PROTECTION
wdenk5653fc32004-02-08 22:55:38 +00001446
Holger Brunck81316a92012-08-09 10:22:41 +02001447static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
1448{
Mario Six88ecd8b2018-01-26 14:43:39 +01001449 if (manufact_match(info, INTEL_MANUFACT) &&
Mario Sixc0350fb2018-01-26 14:43:55 +01001450 info->device_id == NUMONYX_256MBIT) {
Holger Brunck81316a92012-08-09 10:22:41 +02001451 /*
1452 * see errata called
1453 * "Numonyx Axcell P33/P30 Specification Update" :)
1454 */
1455 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
1456 if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
1457 prot)) {
1458 /*
1459 * cmd must come before FLASH_CMD_PROTECT + 20us
1460 * Disable interrupts which might cause a timeout here.
1461 */
1462 int flag = disable_interrupts();
1463 unsigned short cmd;
1464
1465 if (prot)
1466 cmd = FLASH_CMD_PROTECT_SET;
1467 else
1468 cmd = FLASH_CMD_PROTECT_CLEAR;
Andre Przywara58eab322016-11-16 00:50:06 +00001469
1470 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
Holger Brunck81316a92012-08-09 10:22:41 +02001471 flash_write_cmd(info, sector, 0, cmd);
1472 /* re-enable interrupts if necessary */
1473 if (flag)
1474 enable_interrupts();
1475 }
1476 return 1;
1477 }
1478 return 0;
1479}
1480
Mario Sixca2b07a2018-01-26 14:43:32 +01001481int flash_real_protect(flash_info_t *info, long sector, int prot)
wdenk5653fc32004-02-08 22:55:38 +00001482{
1483 int retcode = 0;
1484
Rafael Camposbc9019e2008-07-31 10:22:20 +02001485 switch (info->vendor) {
Mario Sixdde09132018-01-26 14:43:35 +01001486 case CFI_CMDSET_INTEL_PROG_REGIONS:
1487 case CFI_CMDSET_INTEL_STANDARD:
1488 case CFI_CMDSET_INTEL_EXTENDED:
1489 if (!cfi_protect_bugfix(info, sector, prot)) {
1490 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001491 FLASH_CMD_CLEAR_STATUS);
Mario Sixdde09132018-01-26 14:43:35 +01001492 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001493 FLASH_CMD_PROTECT);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001494 if (prot)
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_SET);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001497 else
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_CLEAR);
Mario Sixdde09132018-01-26 14:43:35 +01001500 }
1501 break;
1502 case CFI_CMDSET_AMD_EXTENDED:
1503 case CFI_CMDSET_AMD_STANDARD:
1504 /* U-Boot only checks the first byte */
1505 if (manufact_match(info, ATM_MANUFACT)) {
1506 if (prot) {
1507 flash_unlock_seq(info, 0);
1508 flash_write_cmd(info, 0,
1509 info->addr_unlock1,
1510 ATM_CMD_SOFTLOCK_START);
1511 flash_unlock_seq(info, 0);
1512 flash_write_cmd(info, sector, 0,
1513 ATM_CMD_LOCK_SECT);
1514 } else {
1515 flash_write_cmd(info, 0,
1516 info->addr_unlock1,
1517 AMD_CMD_UNLOCK_START);
1518 if (info->device_id == ATM_ID_BV6416)
1519 flash_write_cmd(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001520 0, ATM_CMD_UNLOCK_SECT);
Mario Sixdde09132018-01-26 14:43:35 +01001521 }
1522 }
1523 if (info->legacy_unlock) {
1524 int flag = disable_interrupts();
1525 int lock_flag;
1526
1527 flash_unlock_seq(info, 0);
1528 flash_write_cmd(info, 0, info->addr_unlock1,
1529 AMD_CMD_SET_PPB_ENTRY);
1530 lock_flag = flash_isset(info, sector, 0, 0x01);
1531 if (prot) {
1532 if (lock_flag) {
1533 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001534 AMD_CMD_PPB_LOCK_BC1);
Mario Sixdde09132018-01-26 14:43:35 +01001535 flash_write_cmd(info, sector, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001536 AMD_CMD_PPB_LOCK_BC2);
Mario Sixdde09132018-01-26 14:43:35 +01001537 }
1538 debug("sector %ld %slocked\n", sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001539 lock_flag ? "" : "already ");
Mario Sixdde09132018-01-26 14:43:35 +01001540 } else {
1541 if (!lock_flag) {
1542 debug("unlock %ld\n", sector);
1543 flash_write_cmd(info, 0, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001544 AMD_CMD_PPB_UNLOCK_BC1);
Mario Sixdde09132018-01-26 14:43:35 +01001545 flash_write_cmd(info, 0, 0,
Mario Sixc0350fb2018-01-26 14:43:55 +01001546 AMD_CMD_PPB_UNLOCK_BC2);
Mario Sixdde09132018-01-26 14:43:35 +01001547 }
1548 debug("sector %ld %sunlocked\n", sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001549 !lock_flag ? "" : "already ");
Mario Sixdde09132018-01-26 14:43:35 +01001550 }
1551 if (flag)
1552 enable_interrupts();
1553
1554 if (flash_status_check(info, sector,
Mario Sixc0350fb2018-01-26 14:43:55 +01001555 info->erase_blk_tout,
1556 prot ? "protect" : "unprotect"))
Mario Sixdde09132018-01-26 14:43:35 +01001557 printf("status check error\n");
1558
1559 flash_write_cmd(info, 0, 0,
1560 AMD_CMD_SET_PPB_EXIT_BC1);
1561 flash_write_cmd(info, 0, 0,
1562 AMD_CMD_SET_PPB_EXIT_BC2);
1563 }
1564 break;
1565#ifdef CONFIG_FLASH_CFI_LEGACY
1566 case CFI_CMDSET_AMD_LEGACY:
1567 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1568 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
1569 if (prot)
Mario Sixddcf0542018-01-26 14:43:54 +01001570 flash_write_cmd(info, sector, 0,
1571 FLASH_CMD_PROTECT_SET);
Mario Sixdde09132018-01-26 14:43:35 +01001572 else
Mario Sixddcf0542018-01-26 14:43:54 +01001573 flash_write_cmd(info, sector, 0,
1574 FLASH_CMD_PROTECT_CLEAR);
TsiChung Liew4e00acd2008-08-19 16:53:39 +00001575#endif
Rafael Camposbc9019e2008-07-31 10:22:20 +02001576 };
wdenk5653fc32004-02-08 22:55:38 +00001577
Stefan Roesedf4e8132010-10-25 18:31:29 +02001578 /*
1579 * Flash needs to be in status register read mode for
1580 * flash_full_status_check() to work correctly
1581 */
1582 flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
Mario Sixd3525b62018-01-26 14:43:48 +01001583 retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
Mario Sixc0350fb2018-01-26 14:43:55 +01001584 prot ? "protect" : "unprotect");
Mario Sixd3525b62018-01-26 14:43:48 +01001585 if (retcode == 0) {
wdenk5653fc32004-02-08 22:55:38 +00001586 info->protect[sector] = prot;
Stefan Roese2662b402006-04-01 13:41:03 +02001587
1588 /*
1589 * On some of Intel's flash chips (marked via legacy_unlock)
1590 * unprotect unprotects all locking.
1591 */
Mario Six4f89da42018-01-26 14:43:42 +01001592 if (prot == 0 && info->legacy_unlock) {
wdenk5653fc32004-02-08 22:55:38 +00001593 flash_sect_t i;
wdenkbf9e3b32004-02-12 00:47:09 +00001594
1595 for (i = 0; i < info->sector_count; i++) {
1596 if (info->protect[i])
Mario Six188a5562018-01-26 14:43:31 +01001597 flash_real_protect(info, i, 1);
wdenk5653fc32004-02-08 22:55:38 +00001598 }
1599 }
1600 }
wdenk5653fc32004-02-08 22:55:38 +00001601 return retcode;
wdenkbf9e3b32004-02-12 00:47:09 +00001602}
1603
wdenk5653fc32004-02-08 22:55:38 +00001604/*-----------------------------------------------------------------------
1605 * flash_read_user_serial - read the OneTimeProgramming cells
1606 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001607void flash_read_user_serial(flash_info_t *info, void *buffer, int offset,
Mario Sixc0350fb2018-01-26 14:43:55 +01001608 int len)
wdenk5653fc32004-02-08 22:55:38 +00001609{
wdenkbf9e3b32004-02-12 00:47:09 +00001610 uchar *src;
1611 uchar *dst;
wdenk5653fc32004-02-08 22:55:38 +00001612
1613 dst = buffer;
Mario Six188a5562018-01-26 14:43:31 +01001614 src = flash_map(info, 0, FLASH_OFFSET_USER_PROTECTION);
1615 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1616 memcpy(dst, src + offset, len);
1617 flash_write_cmd(info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001618 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001619 flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001620}
wdenkbf9e3b32004-02-12 00:47:09 +00001621
wdenk5653fc32004-02-08 22:55:38 +00001622/*
1623 * flash_read_factory_serial - read the device Id from the protection area
1624 */
Mario Sixca2b07a2018-01-26 14:43:32 +01001625void flash_read_factory_serial(flash_info_t *info, void *buffer, int offset,
Mario Sixc0350fb2018-01-26 14:43:55 +01001626 int len)
wdenk5653fc32004-02-08 22:55:38 +00001627{
wdenkbf9e3b32004-02-12 00:47:09 +00001628 uchar *src;
wdenkcd37d9e2004-02-10 00:03:41 +00001629
Mario Six188a5562018-01-26 14:43:31 +01001630 src = flash_map(info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1631 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1632 memcpy(buffer, src + offset, len);
1633 flash_write_cmd(info, 0, 0, info->cmd_reset);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001634 udelay(1);
Haavard Skinnemoen12d30aa2007-12-13 12:56:34 +01001635 flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
wdenk5653fc32004-02-08 22:55:38 +00001636}
1637
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001638#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00001639
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001640/*-----------------------------------------------------------------------
1641 * Reverse the order of the erase regions in the CFI QRY structure.
1642 * This is needed for chips that are either a) correctly detected as
1643 * top-boot, or b) buggy.
1644 */
1645static void cfi_reverse_geometry(struct cfi_qry *qry)
1646{
1647 unsigned int i, j;
1648 u32 tmp;
1649
1650 for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
Mario Six4f89da42018-01-26 14:43:42 +01001651 tmp = get_unaligned(&qry->erase_region_info[i]);
1652 put_unaligned(get_unaligned(&qry->erase_region_info[j]),
1653 &qry->erase_region_info[i]);
1654 put_unaligned(tmp, &qry->erase_region_info[j]);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001655 }
1656}
wdenk5653fc32004-02-08 22:55:38 +00001657
1658/*-----------------------------------------------------------------------
Stefan Roese260421a2006-11-13 13:55:24 +01001659 * read jedec ids from device and set corresponding fields in info struct
1660 *
1661 * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1662 *
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001663 */
1664static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1665{
1666 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001667 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001668 flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1669 udelay(1000); /* some flash are slow to respond */
Mario Six188a5562018-01-26 14:43:31 +01001670 info->manufacturer_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001671 FLASH_OFFSET_MANUFACTURER_ID);
Philippe De Muyterd77c7ac2010-08-10 16:54:52 +02001672 info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
Mario Six188a5562018-01-26 14:43:31 +01001673 flash_read_word(info, FLASH_OFFSET_DEVICE_ID) :
1674 flash_read_uchar(info, FLASH_OFFSET_DEVICE_ID);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001675 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1676}
1677
1678static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1679{
1680 info->cmd_reset = FLASH_CMD_RESET;
1681
1682 cmdset_intel_read_jedec_ids(info);
1683 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1684
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001685#ifdef CONFIG_SYS_FLASH_PROTECTION
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001686 /* read legacy lock/unlock bit from intel flash */
1687 if (info->ext_addr) {
Mario Sixc0350fb2018-01-26 14:43:55 +01001688 info->legacy_unlock =
1689 flash_read_uchar(info, info->ext_addr + 5) & 0x08;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001690 }
1691#endif
1692
1693 return 0;
1694}
1695
1696static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1697{
Mario Sixc8a9a822018-01-26 14:43:51 +01001698 ushort bank_id = 0;
1699 uchar manu_id;
York Sun2544f472017-11-18 11:09:08 -08001700 uchar feature;
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001701
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001702 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1703 flash_unlock_seq(info, 0);
1704 flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1705 udelay(1000); /* some flash are slow to respond */
Tor Krill90447ec2008-03-28 11:29:10 +01001706
Mario Sixc8a9a822018-01-26 14:43:51 +01001707 manu_id = flash_read_uchar(info, FLASH_OFFSET_MANUFACTURER_ID);
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001708 /* JEDEC JEP106Z specifies ID codes up to bank 7 */
Mario Sixc8a9a822018-01-26 14:43:51 +01001709 while (manu_id == FLASH_CONTINUATION_CODE && bank_id < 0x800) {
1710 bank_id += 0x100;
1711 manu_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001712 bank_id | FLASH_OFFSET_MANUFACTURER_ID);
Niklaus Giger3a7b2c22009-07-22 17:13:24 +02001713 }
Mario Sixc8a9a822018-01-26 14:43:51 +01001714 info->manufacturer_id = manu_id;
Tor Krill90447ec2008-03-28 11:29:10 +01001715
York Sun2544f472017-11-18 11:09:08 -08001716 debug("info->ext_addr = 0x%x, cfi_version = 0x%x\n",
1717 info->ext_addr, info->cfi_version);
1718 if (info->ext_addr && info->cfi_version >= 0x3134) {
1719 /* read software feature (at 0x53) */
1720 feature = flash_read_uchar(info, info->ext_addr + 0x13);
1721 debug("feature = 0x%x\n", feature);
1722 info->sr_supported = feature & 0x1;
1723 }
Marek Vasut72443c72017-09-12 19:09:31 +02001724
Mario Sixb1683862018-01-26 14:43:33 +01001725 switch (info->chipwidth) {
Tor Krill90447ec2008-03-28 11:29:10 +01001726 case FLASH_CFI_8BIT:
Mario Six188a5562018-01-26 14:43:31 +01001727 info->device_id = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001728 FLASH_OFFSET_DEVICE_ID);
Tor Krill90447ec2008-03-28 11:29:10 +01001729 if (info->device_id == 0x7E) {
1730 /* AMD 3-byte (expanded) device ids */
Mario Six188a5562018-01-26 14:43:31 +01001731 info->device_id2 = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001732 FLASH_OFFSET_DEVICE_ID2);
Tor Krill90447ec2008-03-28 11:29:10 +01001733 info->device_id2 <<= 8;
Mario Six188a5562018-01-26 14:43:31 +01001734 info->device_id2 |= flash_read_uchar(info,
Tor Krill90447ec2008-03-28 11:29:10 +01001735 FLASH_OFFSET_DEVICE_ID3);
1736 }
1737 break;
1738 case FLASH_CFI_16BIT:
Mario Six188a5562018-01-26 14:43:31 +01001739 info->device_id = flash_read_word(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001740 FLASH_OFFSET_DEVICE_ID);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001741 if ((info->device_id & 0xff) == 0x7E) {
1742 /* AMD 3-byte (expanded) device ids */
Mario Six188a5562018-01-26 14:43:31 +01001743 info->device_id2 = flash_read_uchar(info,
Mario Sixc0350fb2018-01-26 14:43:55 +01001744 FLASH_OFFSET_DEVICE_ID2);
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001745 info->device_id2 <<= 8;
Mario Six188a5562018-01-26 14:43:31 +01001746 info->device_id2 |= flash_read_uchar(info,
Heiko Schocher5b448ad2011-04-11 14:16:19 +02001747 FLASH_OFFSET_DEVICE_ID3);
1748 }
Tor Krill90447ec2008-03-28 11:29:10 +01001749 break;
1750 default:
1751 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001752 }
1753 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001754 udelay(1);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001755}
1756
1757static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1758{
1759 info->cmd_reset = AMD_CMD_RESET;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01001760 info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001761
1762 cmdset_amd_read_jedec_ids(info);
1763 flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1764
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001765#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roeseac6b9112012-12-06 15:44:11 +01001766 if (info->ext_addr) {
1767 /* read sector protect/unprotect scheme (at 0x49) */
1768 if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
Anatolij Gustschin66863b02012-08-09 08:18:12 +02001769 info->legacy_unlock = 1;
1770 }
1771#endif
1772
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01001773 return 0;
1774}
1775
1776#ifdef CONFIG_FLASH_CFI_LEGACY
Mario Sixca2b07a2018-01-26 14:43:32 +01001777static void flash_read_jedec_ids(flash_info_t *info)
Stefan Roese260421a2006-11-13 13:55:24 +01001778{
1779 info->manufacturer_id = 0;
1780 info->device_id = 0;
1781 info->device_id2 = 0;
1782
1783 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001784 case CFI_CMDSET_INTEL_PROG_REGIONS:
Stefan Roese260421a2006-11-13 13:55:24 +01001785 case CFI_CMDSET_INTEL_STANDARD:
1786 case CFI_CMDSET_INTEL_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001787 cmdset_intel_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001788 break;
1789 case CFI_CMDSET_AMD_STANDARD:
1790 case CFI_CMDSET_AMD_EXTENDED:
Michael Schwingen8225d1e2008-01-12 20:29:47 +01001791 cmdset_amd_read_jedec_ids(info);
Stefan Roese260421a2006-11-13 13:55:24 +01001792 break;
1793 default:
1794 break;
1795 }
1796}
1797
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001798/*-----------------------------------------------------------------------
1799 * Call board code to request info about non-CFI flash.
1800 * board_flash_get_legacy needs to fill in at least:
1801 * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1802 */
Becky Bruce09ce9922009-02-02 16:34:51 -06001803static int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001804{
1805 flash_info_t *info = &flash_info[banknum];
1806
1807 if (board_flash_get_legacy(base, banknum, info)) {
1808 /* board code may have filled info completely. If not, we
Mario Sixa6d18f22018-01-26 14:43:41 +01001809 * use JEDEC ID probing.
1810 */
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001811 if (!info->vendor) {
1812 int modes[] = {
1813 CFI_CMDSET_AMD_STANDARD,
1814 CFI_CMDSET_INTEL_STANDARD
1815 };
1816 int i;
1817
Axel Lin31bf0f52013-06-23 00:56:46 +08001818 for (i = 0; i < ARRAY_SIZE(modes); i++) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001819 info->vendor = modes[i];
Becky Bruce09ce9922009-02-02 16:34:51 -06001820 info->start[0] =
1821 (ulong)map_physmem(base,
Stefan Roesee1fb6d02009-02-05 11:44:52 +01001822 info->portwidth,
Becky Bruce09ce9922009-02-02 16:34:51 -06001823 MAP_NOCACHE);
Mario Six88ecd8b2018-01-26 14:43:39 +01001824 if (info->portwidth == FLASH_CFI_8BIT &&
Mario Sixc0350fb2018-01-26 14:43:55 +01001825 info->interface == FLASH_CFI_X8X16) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001826 info->addr_unlock1 = 0x2AAA;
1827 info->addr_unlock2 = 0x5555;
1828 } else {
1829 info->addr_unlock1 = 0x5555;
1830 info->addr_unlock2 = 0x2AAA;
1831 }
1832 flash_read_jedec_ids(info);
1833 debug("JEDEC PROBE: ID %x %x %x\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001834 info->manufacturer_id,
1835 info->device_id,
1836 info->device_id2);
Becky Bruce09ce9922009-02-02 16:34:51 -06001837 if (jedec_flash_match(info, info->start[0]))
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001838 break;
Mario Six98601372018-01-26 14:43:45 +01001839
1840 unmap_physmem((void *)info->start[0],
1841 info->portwidth);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001842 }
1843 }
1844
Mario Sixb1683862018-01-26 14:43:33 +01001845 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04001846 case CFI_CMDSET_INTEL_PROG_REGIONS:
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001847 case CFI_CMDSET_INTEL_STANDARD:
1848 case CFI_CMDSET_INTEL_EXTENDED:
1849 info->cmd_reset = FLASH_CMD_RESET;
1850 break;
1851 case CFI_CMDSET_AMD_STANDARD:
1852 case CFI_CMDSET_AMD_EXTENDED:
1853 case CFI_CMDSET_AMD_LEGACY:
1854 info->cmd_reset = AMD_CMD_RESET;
1855 break;
1856 }
1857 info->flash_id = FLASH_MAN_CFI;
1858 return 1;
1859 }
1860 return 0; /* use CFI */
1861}
1862#else
Becky Bruce09ce9922009-02-02 16:34:51 -06001863static inline int flash_detect_legacy(phys_addr_t base, int banknum)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02001864{
1865 return 0; /* use CFI */
1866}
1867#endif
1868
Stefan Roese260421a2006-11-13 13:55:24 +01001869/*-----------------------------------------------------------------------
wdenk5653fc32004-02-08 22:55:38 +00001870 * detect if flash is compatible with the Common Flash Interface (CFI)
1871 * http://www.jedec.org/download/search/jesd68.pdf
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001872 */
Mario Sixc0350fb2018-01-26 14:43:55 +01001873static void flash_read_cfi(flash_info_t *info, void *buf, unsigned int start,
1874 size_t len)
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001875{
1876 u8 *p = buf;
1877 unsigned int i;
1878
1879 for (i = 0; i < len; i++)
Stefan Roesee303be22013-04-12 19:04:54 +02001880 p[i] = flash_read_uchar(info, start + i);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001881}
1882
Kim Phillips11dc4012012-10-29 13:34:45 +00001883static void __flash_cmd_reset(flash_info_t *info)
Stefan Roesefa36ae72009-10-27 15:15:55 +01001884{
1885 /*
1886 * We do not yet know what kind of commandset to use, so we issue
1887 * the reset command in both Intel and AMD variants, in the hope
1888 * that AMD flash roms ignore the Intel command.
1889 */
1890 flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
Aaron Williamsa90b9572011-04-12 00:59:04 -07001891 udelay(1);
Stefan Roesefa36ae72009-10-27 15:15:55 +01001892 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1893}
Mario Six7223a8c2018-01-26 14:43:37 +01001894
Stefan Roesefa36ae72009-10-27 15:15:55 +01001895void flash_cmd_reset(flash_info_t *info)
Mario Six640f4e32018-01-26 14:43:36 +01001896 __attribute__((weak, alias("__flash_cmd_reset")));
Stefan Roesefa36ae72009-10-27 15:15:55 +01001897
Mario Sixca2b07a2018-01-26 14:43:32 +01001898static int __flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
wdenk5653fc32004-02-08 22:55:38 +00001899{
Wolfgang Denk92eb7292006-12-27 01:26:13 +01001900 int cfi_offset;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001901
Stefan Roesee303be22013-04-12 19:04:54 +02001902 /* Issue FLASH reset command */
1903 flash_cmd_reset(info);
1904
Axel Lin31bf0f52013-06-23 00:56:46 +08001905 for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001906 cfi_offset++) {
Mario Six188a5562018-01-26 14:43:31 +01001907 flash_write_cmd(info, 0, flash_offset_cfi[cfi_offset],
Mario Sixc0350fb2018-01-26 14:43:55 +01001908 FLASH_CMD_CFI);
Mario Six88ecd8b2018-01-26 14:43:39 +01001909 if (flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP, 'Q') &&
Mario Sixddcf0542018-01-26 14:43:54 +01001910 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
1911 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
Mario Sixc0350fb2018-01-26 14:43:55 +01001912 flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1913 sizeof(struct cfi_qry));
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01001914 info->interface = le16_to_cpu(qry->interface_desc);
Stefan Roesee303be22013-04-12 19:04:54 +02001915
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001916 info->cfi_offset = flash_offset_cfi[cfi_offset];
Mario Six188a5562018-01-26 14:43:31 +01001917 debug("device interface is %d\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001918 info->interface);
Mario Six188a5562018-01-26 14:43:31 +01001919 debug("found port %d chip %d ",
Mario Sixc0350fb2018-01-26 14:43:55 +01001920 info->portwidth, info->chipwidth);
Mario Six188a5562018-01-26 14:43:31 +01001921 debug("port %d bits chip %d bits\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01001922 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1923 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001924
1925 /* calculate command offsets as in the Linux driver */
Stefan Roesee303be22013-04-12 19:04:54 +02001926 info->addr_unlock1 = 0x555;
1927 info->addr_unlock2 = 0x2aa;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001928
1929 /*
1930 * modify the unlock address if we are
1931 * in compatibility mode
1932 */
Mario Sixb1683862018-01-26 14:43:33 +01001933 if (/* x8/x16 in x8 mode */
Mario Six4f89da42018-01-26 14:43:42 +01001934 (info->chipwidth == FLASH_CFI_BY8 &&
1935 info->interface == FLASH_CFI_X8X16) ||
Mario Sixb1683862018-01-26 14:43:33 +01001936 /* x16/x32 in x16 mode */
Mario Six4f89da42018-01-26 14:43:42 +01001937 (info->chipwidth == FLASH_CFI_BY16 &&
Mario Six0cec0a12018-01-26 14:43:46 +01001938 info->interface == FLASH_CFI_X16X32)) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001939 info->addr_unlock1 = 0xaaa;
1940 info->addr_unlock2 = 0x555;
1941 }
1942
1943 info->name = "CFI conformant";
1944 return 1;
1945 }
1946 }
1947
1948 return 0;
1949}
1950
Mario Sixca2b07a2018-01-26 14:43:32 +01001951static int flash_detect_cfi(flash_info_t *info, struct cfi_qry *qry)
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001952{
Mario Six188a5562018-01-26 14:43:31 +01001953 debug("flash detect cfi\n");
wdenk5653fc32004-02-08 22:55:38 +00001954
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001955 for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
wdenkbf9e3b32004-02-12 00:47:09 +00001956 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1957 for (info->chipwidth = FLASH_CFI_BY8;
1958 info->chipwidth <= info->portwidth;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001959 info->chipwidth <<= 1)
Stefan Roesee303be22013-04-12 19:04:54 +02001960 if (__flash_detect_cfi(info, qry))
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01001961 return 1;
wdenk5653fc32004-02-08 22:55:38 +00001962 }
Mario Six188a5562018-01-26 14:43:31 +01001963 debug("not found\n");
wdenk5653fc32004-02-08 22:55:38 +00001964 return 0;
1965}
wdenkbf9e3b32004-02-12 00:47:09 +00001966
wdenk5653fc32004-02-08 22:55:38 +00001967/*
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001968 * Manufacturer-specific quirks. Add workarounds for geometry
1969 * reversal, etc. here.
1970 */
1971static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1972{
1973 /* check if flash geometry needs reversal */
1974 if (qry->num_erase_regions > 1) {
1975 /* reverse geometry if top boot part */
1976 if (info->cfi_version < 0x3131) {
1977 /* CFI < 1.1, try to guess from device id */
1978 if ((info->device_id & 0x80) != 0)
1979 cfi_reverse_geometry(qry);
Stefan Roesee303be22013-04-12 19:04:54 +02001980 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001981 /* CFI >= 1.1, deduct from top/bottom flag */
1982 /* note: ext_addr is valid since cfi_version > 0 */
1983 cfi_reverse_geometry(qry);
1984 }
1985 }
1986}
1987
1988static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1989{
1990 int reverse_geometry = 0;
1991
1992 /* Check the "top boot" bit in the PRI */
1993 if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1994 reverse_geometry = 1;
1995
1996 /* AT49BV6416(T) list the erase regions in the wrong order.
1997 * However, the device ID is identical with the non-broken
Ulf Samuelssoncb82a532009-03-27 23:26:43 +01001998 * AT49BV642D they differ in the high byte.
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01001999 */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002000 if (info->device_id == 0xd6 || info->device_id == 0xd2)
2001 reverse_geometry = !reverse_geometry;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002002
2003 if (reverse_geometry)
2004 cfi_reverse_geometry(qry);
2005}
2006
Richard Retanubune8eac432009-01-14 08:44:26 -05002007static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
2008{
2009 /* check if flash geometry needs reversal */
2010 if (qry->num_erase_regions > 1) {
2011 /* reverse geometry if top boot part */
2012 if (info->cfi_version < 0x3131) {
Mike Frysinger6a011ce2011-04-10 16:06:29 -04002013 /* CFI < 1.1, guess by device id */
2014 if (info->device_id == 0x22CA || /* M29W320DT */
2015 info->device_id == 0x2256 || /* M29W320ET */
2016 info->device_id == 0x22D7) { /* M29W800DT */
Richard Retanubune8eac432009-01-14 08:44:26 -05002017 cfi_reverse_geometry(qry);
2018 }
Mike Frysinger4c2105c2011-05-09 18:33:36 -04002019 } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
2020 /* CFI >= 1.1, deduct from top/bottom flag */
2021 /* note: ext_addr is valid since cfi_version > 0 */
2022 cfi_reverse_geometry(qry);
Richard Retanubune8eac432009-01-14 08:44:26 -05002023 }
2024 }
2025}
2026
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002027static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
2028{
2029 /*
2030 * SST, for many recent nor parallel flashes, says they are
2031 * CFI-conformant. This is not true, since qry struct.
2032 * reports a std. AMD command set (0x0002), while SST allows to
2033 * erase two different sector sizes for the same memory.
2034 * 64KB sector (SST call it block) needs 0x30 to be erased.
2035 * 4KB sector (SST call it sector) needs 0x50 to be erased.
2036 * Since CFI query detect the 4KB number of sectors, users expects
2037 * a sector granularity of 4KB, and it is here set.
2038 */
2039 if (info->device_id == 0x5D23 || /* SST39VF3201B */
2040 info->device_id == 0x5C23) { /* SST39VF3202B */
2041 /* set sector granularity to 4KB */
Mario Six640f4e32018-01-26 14:43:36 +01002042 info->cmd_erase_sector = 0x50;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002043 }
2044}
2045
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302046static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry)
2047{
2048 /*
2049 * The M29EW devices seem to report the CFI information wrong
2050 * when it's in 8 bit mode.
2051 * There's an app note from Numonyx on this issue.
2052 * So adjust the buffer size for M29EW while operating in 8-bit mode
2053 */
Mario Six4f89da42018-01-26 14:43:42 +01002054 if (qry->max_buf_write_size > 0x8 &&
Mario Sixc0350fb2018-01-26 14:43:55 +01002055 info->device_id == 0x7E &&
2056 (info->device_id2 == 0x2201 ||
2057 info->device_id2 == 0x2301 ||
2058 info->device_id2 == 0x2801 ||
2059 info->device_id2 == 0x4801)) {
Mario Six876c52f2018-01-26 14:43:50 +01002060 debug("Adjusted buffer size on Numonyx flash");
2061 debug(" M29EW family in 8 bit mode\n");
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302062 qry->max_buf_write_size = 0x8;
2063 }
2064}
2065
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002066/*
wdenk5653fc32004-02-08 22:55:38 +00002067 * The following code cannot be run from FLASH!
2068 *
2069 */
Mario Six188a5562018-01-26 14:43:31 +01002070ulong flash_get_size(phys_addr_t base, int banknum)
wdenk5653fc32004-02-08 22:55:38 +00002071{
wdenkbf9e3b32004-02-12 00:47:09 +00002072 flash_info_t *info = &flash_info[banknum];
wdenk5653fc32004-02-08 22:55:38 +00002073 int i, j;
2074 flash_sect_t sect_cnt;
Becky Bruce09ce9922009-02-02 16:34:51 -06002075 phys_addr_t sector;
wdenk5653fc32004-02-08 22:55:38 +00002076 unsigned long tmp;
2077 int size_ratio;
2078 uchar num_erase_regions;
wdenkbf9e3b32004-02-12 00:47:09 +00002079 int erase_region_size;
2080 int erase_region_count;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002081 struct cfi_qry qry;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002082 unsigned long max_size;
Stefan Roese260421a2006-11-13 13:55:24 +01002083
Kumar Galaf9796902008-05-15 15:13:08 -05002084 memset(&qry, 0, sizeof(qry));
2085
Stefan Roese260421a2006-11-13 13:55:24 +01002086 info->ext_addr = 0;
2087 info->cfi_version = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002088#ifdef CONFIG_SYS_FLASH_PROTECTION
Stefan Roese2662b402006-04-01 13:41:03 +02002089 info->legacy_unlock = 0;
2090#endif
wdenk5653fc32004-02-08 22:55:38 +00002091
Becky Bruce09ce9922009-02-02 16:34:51 -06002092 info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002093
Mario Six188a5562018-01-26 14:43:31 +01002094 if (flash_detect_cfi(info, &qry)) {
Mario Six4f89da42018-01-26 14:43:42 +01002095 info->vendor = le16_to_cpu(get_unaligned(&qry.p_id));
2096 info->ext_addr = le16_to_cpu(get_unaligned(&qry.p_adr));
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002097 num_erase_regions = qry.num_erase_regions;
2098
Stefan Roese260421a2006-11-13 13:55:24 +01002099 if (info->ext_addr) {
Mario Six640f4e32018-01-26 14:43:36 +01002100 info->cfi_version = (ushort)flash_read_uchar(info,
Stefan Roesee303be22013-04-12 19:04:54 +02002101 info->ext_addr + 3) << 8;
Mario Six640f4e32018-01-26 14:43:36 +01002102 info->cfi_version |= (ushort)flash_read_uchar(info,
Stefan Roesee303be22013-04-12 19:04:54 +02002103 info->ext_addr + 4);
Stefan Roese260421a2006-11-13 13:55:24 +01002104 }
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002105
wdenkbf9e3b32004-02-12 00:47:09 +00002106#ifdef DEBUG
Mario Six188a5562018-01-26 14:43:31 +01002107 flash_printqry(&qry);
wdenkbf9e3b32004-02-12 00:47:09 +00002108#endif
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002109
wdenkbf9e3b32004-02-12 00:47:09 +00002110 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002111 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenk5653fc32004-02-08 22:55:38 +00002112 case CFI_CMDSET_INTEL_STANDARD:
2113 case CFI_CMDSET_INTEL_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002114 cmdset_intel_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002115 break;
2116 case CFI_CMDSET_AMD_STANDARD:
2117 case CFI_CMDSET_AMD_EXTENDED:
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002118 cmdset_amd_init(info, &qry);
wdenk5653fc32004-02-08 22:55:38 +00002119 break;
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002120 default:
2121 printf("CFI: Unknown command set 0x%x\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002122 info->vendor);
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002123 /*
2124 * Unfortunately, this means we don't know how
2125 * to get the chip back to Read mode. Might
2126 * as well try an Intel-style reset...
2127 */
2128 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
2129 return 0;
wdenk5653fc32004-02-08 22:55:38 +00002130 }
wdenkcd37d9e2004-02-10 00:03:41 +00002131
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002132 /* Do manufacturer-specific fixups */
2133 switch (info->manufacturer_id) {
Mario Schuknecht2c9f48a2011-02-21 13:13:14 +01002134 case 0x0001: /* AMD */
2135 case 0x0037: /* AMIC */
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002136 flash_fixup_amd(info, &qry);
2137 break;
2138 case 0x001f:
2139 flash_fixup_atmel(info, &qry);
2140 break;
Richard Retanubune8eac432009-01-14 08:44:26 -05002141 case 0x0020:
2142 flash_fixup_stm(info, &qry);
2143 break;
Angelo Dureghello07b2c5c2012-12-01 01:14:18 +01002144 case 0x00bf: /* SST */
2145 flash_fixup_sst(info, &qry);
2146 break;
Jagannadha Sutradharudu Tekic5023212013-03-01 16:54:26 +05302147 case 0x0089: /* Numonyx */
2148 flash_fixup_num(info, &qry);
2149 break;
Haavard Skinnemoen467bcee2007-12-14 15:36:18 +01002150 }
2151
Mario Six188a5562018-01-26 14:43:31 +01002152 debug("manufacturer is %d\n", info->vendor);
2153 debug("manufacturer id is 0x%x\n", info->manufacturer_id);
2154 debug("device id is 0x%x\n", info->device_id);
2155 debug("device id2 is 0x%x\n", info->device_id2);
2156 debug("cfi version is 0x%04x\n", info->cfi_version);
Stefan Roese260421a2006-11-13 13:55:24 +01002157
wdenk5653fc32004-02-08 22:55:38 +00002158 size_ratio = info->portwidth / info->chipwidth;
wdenkbf9e3b32004-02-12 00:47:09 +00002159 /* if the chip is x8/x16 reduce the ratio by half */
Mario Six4f89da42018-01-26 14:43:42 +01002160 if (info->interface == FLASH_CFI_X8X16 &&
Mario Sixc0350fb2018-01-26 14:43:55 +01002161 info->chipwidth == FLASH_CFI_BY8) {
wdenkbf9e3b32004-02-12 00:47:09 +00002162 size_ratio >>= 1;
2163 }
Mario Six188a5562018-01-26 14:43:31 +01002164 debug("size_ratio %d port %d bits chip %d bits\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002165 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
2166 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002167 info->size = 1 << qry.dev_size;
2168 /* multiply the size by the number of chips */
2169 info->size *= size_ratio;
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002170 max_size = cfi_flash_bank_size(banknum);
Mario Six4f89da42018-01-26 14:43:42 +01002171 if (max_size && info->size > max_size) {
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002172 debug("[truncated from %ldMiB]", info->size >> 20);
2173 info->size = max_size;
2174 }
Mario Six188a5562018-01-26 14:43:31 +01002175 debug("found %d erase regions\n", num_erase_regions);
wdenk5653fc32004-02-08 22:55:38 +00002176 sect_cnt = 0;
2177 sector = base;
wdenkbf9e3b32004-02-12 00:47:09 +00002178 for (i = 0; i < num_erase_regions; i++) {
2179 if (i > NUM_ERASE_REGIONS) {
Mario Six188a5562018-01-26 14:43:31 +01002180 printf("%d erase regions found, only %d used\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002181 num_erase_regions, NUM_ERASE_REGIONS);
wdenk5653fc32004-02-08 22:55:38 +00002182 break;
2183 }
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002184
Andrew Gabbasovaedadf12013-05-14 12:27:52 -05002185 tmp = le32_to_cpu(get_unaligned(
Mario Six4f89da42018-01-26 14:43:42 +01002186 &qry.erase_region_info[i]));
Haavard Skinnemoen0ddf06d2007-12-14 15:36:17 +01002187 debug("erase region %u: 0x%08lx\n", i, tmp);
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002188
2189 erase_region_count = (tmp & 0xffff) + 1;
2190 tmp >>= 16;
wdenkbf9e3b32004-02-12 00:47:09 +00002191 erase_region_size =
2192 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
Mario Sixddcf0542018-01-26 14:43:54 +01002193 debug("erase_region_count = %d ", erase_region_count);
2194 debug("erase_region_size = %d\n", erase_region_size);
wdenkbf9e3b32004-02-12 00:47:09 +00002195 for (j = 0; j < erase_region_count; j++) {
Ilya Yanokec50a8e2010-10-21 17:20:12 +02002196 if (sector - base >= info->size)
2197 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002198 if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002199 printf("ERROR: too many flash sectors\n");
2200 break;
2201 }
Becky Bruce09ce9922009-02-02 16:34:51 -06002202 info->start[sect_cnt] =
2203 (ulong)map_physmem(sector,
2204 info->portwidth,
2205 MAP_NOCACHE);
wdenk5653fc32004-02-08 22:55:38 +00002206 sector += (erase_region_size * size_ratio);
wdenka1191902005-01-09 17:12:27 +00002207
2208 /*
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002209 * Only read protection status from
2210 * supported devices (intel...)
wdenka1191902005-01-09 17:12:27 +00002211 */
2212 switch (info->vendor) {
Vasiliy Leoenenko9c048b52008-05-07 21:25:33 +04002213 case CFI_CMDSET_INTEL_PROG_REGIONS:
wdenka1191902005-01-09 17:12:27 +00002214 case CFI_CMDSET_INTEL_EXTENDED:
2215 case CFI_CMDSET_INTEL_STANDARD:
Stefan Roesedf4e8132010-10-25 18:31:29 +02002216 /*
2217 * Set flash to read-id mode. Otherwise
2218 * reading protected status is not
2219 * guaranteed.
2220 */
2221 flash_write_cmd(info, sect_cnt, 0,
2222 FLASH_CMD_READ_ID);
wdenka1191902005-01-09 17:12:27 +00002223 info->protect[sect_cnt] =
Mario Six188a5562018-01-26 14:43:31 +01002224 flash_isset(info, sect_cnt,
Mario Sixc0350fb2018-01-26 14:43:55 +01002225 FLASH_OFFSET_PROTECT,
2226 FLASH_STATUS_PROTECT);
Vasily Khoruzhickedc498c2016-03-20 18:37:10 -07002227 flash_write_cmd(info, sect_cnt, 0,
2228 FLASH_CMD_RESET);
wdenka1191902005-01-09 17:12:27 +00002229 break;
Stefan Roese03deff42012-12-06 15:44:10 +01002230 case CFI_CMDSET_AMD_EXTENDED:
2231 case CFI_CMDSET_AMD_STANDARD:
Stefan Roeseac6b9112012-12-06 15:44:11 +01002232 if (!info->legacy_unlock) {
Stefan Roese03deff42012-12-06 15:44:10 +01002233 /* default: not protected */
2234 info->protect[sect_cnt] = 0;
2235 break;
2236 }
2237
2238 /* Read protection (PPB) from sector */
2239 flash_write_cmd(info, 0, 0,
2240 info->cmd_reset);
2241 flash_unlock_seq(info, 0);
2242 flash_write_cmd(info, 0,
2243 info->addr_unlock1,
2244 FLASH_CMD_READ_ID);
2245 info->protect[sect_cnt] =
2246 flash_isset(
2247 info, sect_cnt,
2248 FLASH_OFFSET_PROTECT,
2249 FLASH_STATUS_PROTECT);
2250 break;
wdenka1191902005-01-09 17:12:27 +00002251 default:
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002252 /* default: not protected */
2253 info->protect[sect_cnt] = 0;
wdenka1191902005-01-09 17:12:27 +00002254 }
2255
wdenk5653fc32004-02-08 22:55:38 +00002256 sect_cnt++;
2257 }
2258 }
2259
2260 info->sector_count = sect_cnt;
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002261 info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
2262 tmp = 1 << qry.block_erase_timeout_typ;
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002263 info->erase_blk_tout = tmp *
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002264 (1 << qry.block_erase_timeout_max);
2265 tmp = (1 << qry.buf_write_timeout_typ) *
2266 (1 << qry.buf_write_timeout_max);
2267
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002268 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002269 info->buffer_write_tout = (tmp + 999) / 1000;
2270 tmp = (1 << qry.word_write_timeout_typ) *
2271 (1 << qry.word_write_timeout_max);
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002272 /* round up when converting to ms */
Haavard Skinnemoene23741f2007-12-14 15:36:16 +01002273 info->write_tout = (tmp + 999) / 1000;
wdenk5653fc32004-02-08 22:55:38 +00002274 info->flash_id = FLASH_MAN_CFI;
Mario Six4f89da42018-01-26 14:43:42 +01002275 if (info->interface == FLASH_CFI_X8X16 &&
2276 info->chipwidth == FLASH_CFI_BY8) {
Haavard Skinnemoen7e5b9b42007-12-13 12:56:28 +01002277 /* XXX - Need to test on x8/x16 in parallel. */
2278 info->portwidth >>= 1;
wdenk855a4962004-03-14 18:23:55 +00002279 }
Mike Frysinger22159872008-10-02 01:55:38 -04002280
Mario Six188a5562018-01-26 14:43:31 +01002281 flash_write_cmd(info, 0, 0, info->cmd_reset);
wdenk5653fc32004-02-08 22:55:38 +00002282 }
2283
wdenkbf9e3b32004-02-12 00:47:09 +00002284 return (info->size);
wdenk5653fc32004-02-08 22:55:38 +00002285}
2286
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002287#ifdef CONFIG_FLASH_CFI_MTD
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002288void flash_set_verbose(uint v)
2289{
2290 flash_verbose = v;
2291}
Mike Frysinger4ffeab22010-12-22 09:41:13 -05002292#endif
Piotr Ziecik6ea808e2008-11-17 15:49:32 +01002293
Stefan Roese6f726f92010-10-25 18:31:48 +02002294static void cfi_flash_set_config_reg(u32 base, u16 val)
2295{
2296#ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
2297 /*
2298 * Only set this config register if really defined
2299 * to a valid value (0xffff is invalid)
2300 */
2301 if (val == 0xffff)
2302 return;
2303
2304 /*
2305 * Set configuration register. Data is "encrypted" in the 16 lower
2306 * address bits.
2307 */
2308 flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
2309 flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
2310
2311 /*
2312 * Finally issue reset-command to bring device back to
2313 * read-array mode
2314 */
2315 flash_write16(FLASH_CMD_RESET, (void *)base);
2316#endif
2317}
2318
wdenk5653fc32004-02-08 22:55:38 +00002319/*-----------------------------------------------------------------------
2320 */
Heiko Schocher6ee14162011-04-04 08:10:21 +02002321
Marek Vasut236c49a2017-08-20 17:20:00 +02002322static void flash_protect_default(void)
Heiko Schocher6ee14162011-04-04 08:10:21 +02002323{
Peter Tyser2c519832011-04-13 11:46:56 -05002324#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2325 int i;
2326 struct apl_s {
2327 ulong start;
2328 ulong size;
2329 } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
2330#endif
2331
Heiko Schocher6ee14162011-04-04 08:10:21 +02002332 /* Monitor protection ON by default */
Vignesh Raghavendrad75eacf2019-10-23 13:30:00 +05302333#if defined(CONFIG_SYS_MONITOR_BASE) && \
2334 (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
Heiko Schocher6ee14162011-04-04 08:10:21 +02002335 (!defined(CONFIG_MONITOR_IS_IN_RAM))
2336 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002337 CONFIG_SYS_MONITOR_BASE,
2338 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
2339 flash_get_info(CONFIG_SYS_MONITOR_BASE));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002340#endif
2341
2342 /* Environment protection ON by default */
2343#ifdef CONFIG_ENV_IS_IN_FLASH
2344 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002345 CONFIG_ENV_ADDR,
2346 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2347 flash_get_info(CONFIG_ENV_ADDR));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002348#endif
2349
2350 /* Redundant environment protection ON by default */
2351#ifdef CONFIG_ENV_ADDR_REDUND
2352 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002353 CONFIG_ENV_ADDR_REDUND,
2354 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2355 flash_get_info(CONFIG_ENV_ADDR_REDUND));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002356#endif
2357
2358#if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
Axel Lin31bf0f52013-06-23 00:56:46 +08002359 for (i = 0; i < ARRAY_SIZE(apl); i++) {
Marek Vasut31d34142011-10-21 14:17:05 +00002360 debug("autoprotecting from %08lx to %08lx\n",
Heiko Schocher6ee14162011-04-04 08:10:21 +02002361 apl[i].start, apl[i].start + apl[i].size - 1);
2362 flash_protect(FLAG_PROTECT_SET,
Mario Sixc0350fb2018-01-26 14:43:55 +01002363 apl[i].start,
2364 apl[i].start + apl[i].size - 1,
2365 flash_get_info(apl[i].start));
Heiko Schocher6ee14162011-04-04 08:10:21 +02002366 }
2367#endif
2368}
2369
Mario Six188a5562018-01-26 14:43:31 +01002370unsigned long flash_init(void)
wdenk5653fc32004-02-08 22:55:38 +00002371{
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002372 unsigned long size = 0;
2373 int i;
wdenk5653fc32004-02-08 22:55:38 +00002374
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002375#ifdef CONFIG_SYS_FLASH_PROTECTION
Eric Schumann3a3baf32009-03-21 09:59:34 -04002376 /* read environment from EEPROM */
2377 char s[64];
Mario Six7223a8c2018-01-26 14:43:37 +01002378
Simon Glass00caae62017-08-03 12:22:12 -06002379 env_get_f("unlock", s, sizeof(s));
Michael Schwingen81b20cc2007-12-07 23:35:02 +01002380#endif
wdenk5653fc32004-02-08 22:55:38 +00002381
Thomas Chouf1056912015-11-07 14:31:08 +08002382#ifdef CONFIG_CFI_FLASH /* for driver model */
2383 cfi_flash_init_dm();
2384#endif
2385
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002386 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002387 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002388 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk5653fc32004-02-08 22:55:38 +00002389
Stefan Roese6f726f92010-10-25 18:31:48 +02002390 /* Optionally write flash configuration register */
2391 cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
2392 cfi_flash_config_reg(i));
2393
Stefan Roeseb00e19c2010-08-30 10:11:51 +02002394 if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
Anatolij Gustschin34bbb8f2010-11-28 02:13:33 +01002395 flash_get_size(cfi_flash_bank_addr(i), i);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002396 size += flash_info[i].size;
2397 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002398#ifndef CONFIG_SYS_FLASH_QUIET_TEST
Mario Six876c52f2018-01-26 14:43:50 +01002399 printf("## Unknown flash on Bank %d ", i + 1);
2400 printf("- Size = 0x%08lx = %ld MB\n",
Mario Sixc0350fb2018-01-26 14:43:55 +01002401 flash_info[i].size,
2402 flash_info[i].size >> 20);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002403#endif /* CONFIG_SYS_FLASH_QUIET_TEST */
wdenk5653fc32004-02-08 22:55:38 +00002404 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002405#ifdef CONFIG_SYS_FLASH_PROTECTION
Jeroen Hofsteec15df212014-06-17 22:47:31 +02002406 else if (strcmp(s, "yes") == 0) {
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002407 /*
2408 * Only the U-Boot image and it's environment
2409 * is protected, all other sectors are
2410 * unprotected (unlocked) if flash hardware
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002411 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002412 * and the environment variable "unlock" is
2413 * set to "yes".
2414 */
2415 if (flash_info[i].legacy_unlock) {
2416 int k;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002417
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002418 /*
2419 * Disable legacy_unlock temporarily,
2420 * since flash_real_protect would
2421 * relock all other sectors again
2422 * otherwise.
2423 */
2424 flash_info[i].legacy_unlock = 0;
Stefan Roese79b4cda2006-02-28 15:29:58 +01002425
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002426 /*
2427 * Legacy unlocking (e.g. Intel J3) ->
2428 * unlock only one sector. This will
2429 * unlock all sectors.
2430 */
Mario Six188a5562018-01-26 14:43:31 +01002431 flash_real_protect(&flash_info[i], 0, 0);
Stefan Roese79b4cda2006-02-28 15:29:58 +01002432
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002433 flash_info[i].legacy_unlock = 1;
2434
2435 /*
2436 * Manually mark other sectors as
2437 * unlocked (unprotected)
2438 */
2439 for (k = 1; k < flash_info[i].sector_count; k++)
2440 flash_info[i].protect[k] = 0;
2441 } else {
2442 /*
2443 * No legancy unlocking -> unlock all sectors
2444 */
Mario Six188a5562018-01-26 14:43:31 +01002445 flash_protect(FLAG_PROTECT_CLEAR,
Mario Sixc0350fb2018-01-26 14:43:55 +01002446 flash_info[i].start[0],
2447 flash_info[i].start[0]
2448 + flash_info[i].size - 1,
2449 &flash_info[i]);
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002450 }
Stefan Roese79b4cda2006-02-28 15:29:58 +01002451 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02002452#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk5653fc32004-02-08 22:55:38 +00002453 }
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002454
Heiko Schocher6ee14162011-04-04 08:10:21 +02002455 flash_protect_default();
Piotr Ziecik91809ed2008-11-17 15:57:58 +01002456#ifdef CONFIG_FLASH_CFI_MTD
2457 cfi_mtd_init();
2458#endif
2459
Haavard Skinnemoenbe60a902007-10-06 18:55:36 +02002460 return (size);
wdenk5653fc32004-02-08 22:55:38 +00002461}
Thomas Chouf1056912015-11-07 14:31:08 +08002462
2463#ifdef CONFIG_CFI_FLASH /* for driver model */
2464static int cfi_flash_probe(struct udevice *dev)
2465{
Thomas Chouf1056912015-11-07 14:31:08 +08002466 const fdt32_t *cell;
Mario Six8bfeb332018-03-28 14:38:41 +02002467 int addrc, sizec;
Thomas Chouf1056912015-11-07 14:31:08 +08002468 int len, idx;
2469
Mario Six8bfeb332018-03-28 14:38:41 +02002470 addrc = dev_read_addr_cells(dev);
2471 sizec = dev_read_size_cells(dev);
2472
2473 /* decode regs; there may be multiple reg tuples. */
2474 cell = dev_read_prop(dev, "reg", &len);
Thomas Chouf1056912015-11-07 14:31:08 +08002475 if (!cell)
2476 return -ENOENT;
2477 idx = 0;
2478 len /= sizeof(fdt32_t);
2479 while (idx < len) {
Mario Six8bfeb332018-03-28 14:38:41 +02002480 phys_addr_t addr;
2481
2482 addr = dev_translate_address(dev, cell + idx);
2483
Marek Vasut1ec0a372017-09-12 19:09:08 +02002484 flash_info[cfi_flash_num_flash_banks].dev = dev;
2485 flash_info[cfi_flash_num_flash_banks].base = addr;
2486 cfi_flash_num_flash_banks++;
Mario Six8bfeb332018-03-28 14:38:41 +02002487
Thomas Chouf1056912015-11-07 14:31:08 +08002488 idx += addrc + sizec;
2489 }
Marek Vasut1ec0a372017-09-12 19:09:08 +02002490 gd->bd->bi_flashstart = flash_info[0].base;
Thomas Chouf1056912015-11-07 14:31:08 +08002491
2492 return 0;
2493}
2494
2495static const struct udevice_id cfi_flash_ids[] = {
2496 { .compatible = "cfi-flash" },
2497 { .compatible = "jedec-flash" },
2498 {}
2499};
2500
2501U_BOOT_DRIVER(cfi_flash) = {
2502 .name = "cfi_flash",
2503 .id = UCLASS_MTD,
2504 .of_match = cfi_flash_ids,
2505 .probe = cfi_flash_probe,
2506};
2507#endif /* CONFIG_CFI_FLASH */