wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1 | /* |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2 | * (C) Copyright 2002-2004 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 3 | * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com |
| 4 | * |
| 5 | * Copyright (C) 2003 Arabella Software Ltd. |
| 6 | * Yuli Barcohen <yuli@arabellasw.com> |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 7 | * |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 8 | * Copyright (C) 2004 |
| 9 | * Ed Okerson |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 |
| 12 | * Tolunay Orkun <listmember@orkun.us> |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 13 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* The DEBUG define must be before common to enable debugging */ |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 18 | /* #define DEBUG */ |
| 19 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 20 | #include <common.h> |
| 21 | #include <asm/processor.h> |
Haiying Wang | 3a197b2 | 2007-02-21 16:52:31 +0100 | [diff] [blame] | 22 | #include <asm/io.h> |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 23 | #include <asm/byteorder.h> |
Andrew Gabbasov | aedadf1 | 2013-05-14 12:27:52 -0500 | [diff] [blame] | 24 | #include <asm/unaligned.h> |
wdenk | 2a8af18 | 2005-04-13 10:02:42 +0000 | [diff] [blame] | 25 | #include <environment.h> |
Stefan Roese | fa36ae7 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 26 | #include <mtd/cfi_flash.h> |
Jens Scharsig (BuS Elektronik) | a9f5fab | 2012-01-27 09:29:53 +0100 | [diff] [blame] | 27 | #include <watchdog.h> |
wdenk | 028ab6b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 28 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 29 | /* |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 30 | * This file implements a Common Flash Interface (CFI) driver for |
| 31 | * U-Boot. |
| 32 | * |
| 33 | * The width of the port and the width of the chips are determined at |
| 34 | * initialization. These widths are used to calculate the address for |
| 35 | * access CFI data structures. |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 36 | * |
| 37 | * References |
| 38 | * JEDEC Standard JESD68 - Common Flash Interface (CFI) |
| 39 | * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes |
| 40 | * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets |
| 41 | * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 42 | * AMD CFI Specification, Release 2.0 December 1, 2001 |
| 43 | * AMD/Spansion Application Note: Migration from Single-byte to Three-byte |
| 44 | * Device IDs, Publication Number 25538 Revision A, November 8, 2001 |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 45 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between |
Heiko Schocher | d0b6e14 | 2007-01-19 18:05:26 +0100 | [diff] [blame] | 47 | * reading and writing ... (yes there is such a Hardware). |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 50 | static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT }; |
Mike Frysinger | 4ffeab2 | 2010-12-22 09:41:13 -0500 | [diff] [blame] | 51 | #ifdef CONFIG_FLASH_CFI_MTD |
Piotr Ziecik | 6ea808e | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 52 | static uint flash_verbose = 1; |
Mike Frysinger | 4ffeab2 | 2010-12-22 09:41:13 -0500 | [diff] [blame] | 53 | #else |
| 54 | #define flash_verbose 1 |
| 55 | #endif |
Wolfgang Denk | 92eb729 | 2006-12-27 01:26:13 +0100 | [diff] [blame] | 56 | |
Wolfgang Denk | 2a112b2 | 2008-08-08 16:39:54 +0200 | [diff] [blame] | 57 | flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */ |
| 58 | |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 59 | /* |
| 60 | * Check if chip width is defined. If not, start detecting with 8bit. |
| 61 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | #ifndef CONFIG_SYS_FLASH_CFI_WIDTH |
| 63 | #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 64 | #endif |
| 65 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 66 | #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS |
| 67 | #define __maybe_weak __weak |
| 68 | #else |
| 69 | #define __maybe_weak static |
| 70 | #endif |
| 71 | |
Stefan Roese | 6f726f9 | 2010-10-25 18:31:48 +0200 | [diff] [blame] | 72 | /* |
| 73 | * 0xffff is an undefined value for the configuration register. When |
| 74 | * this value is returned, the configuration register shall not be |
| 75 | * written at all (default mode). |
| 76 | */ |
| 77 | static u16 cfi_flash_config_reg(int i) |
| 78 | { |
| 79 | #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS |
| 80 | return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i]; |
| 81 | #else |
| 82 | return 0xffff; |
| 83 | #endif |
| 84 | } |
| 85 | |
Stefan Roese | ca5def3 | 2010-08-31 10:00:10 +0200 | [diff] [blame] | 86 | #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT) |
| 87 | int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT; |
| 88 | #endif |
| 89 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 90 | __weak phys_addr_t cfi_flash_bank_addr(int i) |
Stefan Roese | b00e19c | 2010-08-30 10:11:51 +0200 | [diff] [blame] | 91 | { |
| 92 | return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i]; |
| 93 | } |
Stefan Roese | b00e19c | 2010-08-30 10:11:51 +0200 | [diff] [blame] | 94 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 95 | __weak unsigned long cfi_flash_bank_size(int i) |
Ilya Yanok | ec50a8e | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 96 | { |
| 97 | #ifdef CONFIG_SYS_FLASH_BANKS_SIZES |
| 98 | return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i]; |
| 99 | #else |
| 100 | return 0; |
| 101 | #endif |
| 102 | } |
Ilya Yanok | ec50a8e | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 103 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 104 | __maybe_weak void flash_write8(u8 value, void *addr) |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 105 | { |
| 106 | __raw_writeb(value, addr); |
| 107 | } |
| 108 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 109 | __maybe_weak void flash_write16(u16 value, void *addr) |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 110 | { |
| 111 | __raw_writew(value, addr); |
| 112 | } |
| 113 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 114 | __maybe_weak void flash_write32(u32 value, void *addr) |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 115 | { |
| 116 | __raw_writel(value, addr); |
| 117 | } |
| 118 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 119 | __maybe_weak void flash_write64(u64 value, void *addr) |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 120 | { |
| 121 | /* No architectures currently implement __raw_writeq() */ |
| 122 | *(volatile u64 *)addr = value; |
| 123 | } |
| 124 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 125 | __maybe_weak u8 flash_read8(void *addr) |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 126 | { |
| 127 | return __raw_readb(addr); |
| 128 | } |
| 129 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 130 | __maybe_weak u16 flash_read16(void *addr) |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 131 | { |
| 132 | return __raw_readw(addr); |
| 133 | } |
| 134 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 135 | __maybe_weak u32 flash_read32(void *addr) |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 136 | { |
| 137 | return __raw_readl(addr); |
| 138 | } |
| 139 | |
Jeroen Hofstee | 00dcb07 | 2014-10-08 22:57:23 +0200 | [diff] [blame] | 140 | __maybe_weak u64 flash_read64(void *addr) |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 141 | { |
| 142 | /* No architectures currently implement __raw_readq() */ |
| 143 | return *(volatile u64 *)addr; |
| 144 | } |
| 145 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 146 | /*----------------------------------------------------------------------- |
| 147 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 148 | #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) |
Heiko Schocher | 4f97567 | 2009-02-10 09:53:29 +0100 | [diff] [blame] | 149 | flash_info_t *flash_get_info(ulong base) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 150 | { |
| 151 | int i; |
Masahiro Yamada | 24c185c | 2013-05-17 14:50:37 +0900 | [diff] [blame] | 152 | flash_info_t *info; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 153 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 154 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
Masahiro Yamada | e2e273a | 2013-05-17 14:50:36 +0900 | [diff] [blame] | 155 | info = &flash_info[i]; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 156 | if (info->size && info->start[0] <= base && |
| 157 | base <= info->start[0] + info->size - 1) |
Masahiro Yamada | 24c185c | 2013-05-17 14:50:37 +0900 | [diff] [blame] | 158 | return info; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Masahiro Yamada | 24c185c | 2013-05-17 14:50:37 +0900 | [diff] [blame] | 161 | return NULL; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 162 | } |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 163 | #endif |
| 164 | |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 165 | unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect) |
| 166 | { |
| 167 | if (sect != (info->sector_count - 1)) |
| 168 | return info->start[sect + 1] - info->start[sect]; |
| 169 | else |
| 170 | return info->start[0] + info->size - info->start[sect]; |
| 171 | } |
| 172 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 173 | /*----------------------------------------------------------------------- |
| 174 | * create an address based on the offset and the port width |
| 175 | */ |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 176 | static inline void * |
| 177 | flash_map (flash_info_t * info, flash_sect_t sect, uint offset) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 178 | { |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 179 | unsigned int byte_offset = offset * info->portwidth; |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 180 | |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 181 | return (void *)(info->start[sect] + byte_offset); |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static inline void flash_unmap(flash_info_t *info, flash_sect_t sect, |
| 185 | unsigned int offset, void *addr) |
| 186 | { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 187 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 188 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 189 | /*----------------------------------------------------------------------- |
| 190 | * make a proper sized command based on the port and chip widths |
| 191 | */ |
Sebastian Siewior | 7288f97 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 192 | static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 193 | { |
| 194 | int i; |
Vasiliy Leoenenko | 93c56f2 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 195 | int cword_offset; |
| 196 | int cp_offset; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 197 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Sebastian Siewior | 340ccb2 | 2008-07-16 20:04:49 +0200 | [diff] [blame] | 198 | u32 cmd_le = cpu_to_le32(cmd); |
| 199 | #endif |
Vasiliy Leoenenko | 93c56f2 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 200 | uchar val; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 201 | uchar *cp = (uchar *) cmdbuf; |
| 202 | |
Vasiliy Leoenenko | 93c56f2 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 203 | for (i = info->portwidth; i > 0; i--){ |
| 204 | cword_offset = (info->portwidth-i)%info->chipwidth; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 205 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Vasiliy Leoenenko | 93c56f2 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 206 | cp_offset = info->portwidth - i; |
Sebastian Siewior | 340ccb2 | 2008-07-16 20:04:49 +0200 | [diff] [blame] | 207 | val = *((uchar*)&cmd_le + cword_offset); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 208 | #else |
Vasiliy Leoenenko | 93c56f2 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 209 | cp_offset = i - 1; |
Sebastian Siewior | 7288f97 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 210 | val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 211 | #endif |
Sebastian Siewior | 7288f97 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 212 | cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val; |
Vasiliy Leoenenko | 93c56f2 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 213 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 214 | } |
| 215 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 216 | #ifdef DEBUG |
| 217 | /*----------------------------------------------------------------------- |
| 218 | * Debug support |
| 219 | */ |
Haavard Skinnemoen | 3055793 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 220 | static void print_longlong (char *str, unsigned long long data) |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 221 | { |
| 222 | int i; |
| 223 | char *cp; |
| 224 | |
Wolfgang Denk | 657f206 | 2009-02-04 09:42:20 +0100 | [diff] [blame] | 225 | cp = (char *) &data; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 226 | for (i = 0; i < 8; i++) |
| 227 | sprintf (&str[i * 2], "%2.2x", *cp++); |
| 228 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 229 | |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 230 | static void flash_printqry (struct cfi_qry *qry) |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 231 | { |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 232 | u8 *p = (u8 *)qry; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 233 | int x, y; |
| 234 | |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 235 | for (x = 0; x < sizeof(struct cfi_qry); x += 16) { |
| 236 | debug("%02x : ", x); |
| 237 | for (y = 0; y < 16; y++) |
| 238 | debug("%2.2x ", p[x + y]); |
| 239 | debug(" "); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 240 | for (y = 0; y < 16; y++) { |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 241 | unsigned char c = p[x + y]; |
| 242 | if (c >= 0x20 && c <= 0x7e) |
| 243 | debug("%c", c); |
| 244 | else |
| 245 | debug("."); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 246 | } |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 247 | debug("\n"); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 250 | #endif |
| 251 | |
| 252 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 253 | /*----------------------------------------------------------------------- |
| 254 | * read a character at a port width address |
| 255 | */ |
Haavard Skinnemoen | 3055793 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 256 | static inline uchar flash_read_uchar (flash_info_t * info, uint offset) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 257 | { |
| 258 | uchar *cp; |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 259 | uchar retval; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 260 | |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 261 | cp = flash_map (info, 0, offset); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 262 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 263 | retval = flash_read8(cp); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 264 | #else |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 265 | retval = flash_read8(cp + info->portwidth - 1); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 266 | #endif |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 267 | flash_unmap (info, 0, offset, cp); |
| 268 | return retval; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /*----------------------------------------------------------------------- |
Tor Krill | 90447ec | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 272 | * read a word at a port width address, assume 16bit bus |
| 273 | */ |
| 274 | static inline ushort flash_read_word (flash_info_t * info, uint offset) |
| 275 | { |
| 276 | ushort *addr, retval; |
| 277 | |
| 278 | addr = flash_map (info, 0, offset); |
| 279 | retval = flash_read16 (addr); |
| 280 | flash_unmap (info, 0, offset, addr); |
| 281 | return retval; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /*----------------------------------------------------------------------- |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 286 | * read a long word by picking the least significant byte of each maximum |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 287 | * port size word. Swap for ppc format. |
| 288 | */ |
Haavard Skinnemoen | 3055793 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 289 | static ulong flash_read_long (flash_info_t * info, flash_sect_t sect, |
| 290 | uint offset) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 291 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 292 | uchar *addr; |
| 293 | ulong retval; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 294 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 295 | #ifdef DEBUG |
| 296 | int x; |
| 297 | #endif |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 298 | addr = flash_map (info, sect, offset); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 299 | |
| 300 | #ifdef DEBUG |
| 301 | debug ("long addr is at %p info->portwidth = %d\n", addr, |
| 302 | info->portwidth); |
| 303 | for (x = 0; x < 4 * info->portwidth; x++) { |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 304 | debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x)); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 305 | } |
| 306 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 307 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 308 | retval = ((flash_read8(addr) << 16) | |
| 309 | (flash_read8(addr + info->portwidth) << 24) | |
| 310 | (flash_read8(addr + 2 * info->portwidth)) | |
| 311 | (flash_read8(addr + 3 * info->portwidth) << 8)); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 312 | #else |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 313 | retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) | |
| 314 | (flash_read8(addr + info->portwidth - 1) << 16) | |
| 315 | (flash_read8(addr + 4 * info->portwidth - 1) << 8) | |
| 316 | (flash_read8(addr + 3 * info->portwidth - 1))); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 317 | #endif |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 318 | flash_unmap(info, sect, offset, addr); |
| 319 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 320 | return retval; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 323 | /* |
| 324 | * Write a proper sized command to the correct address |
| 325 | */ |
Stefan Roese | fa36ae7 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 326 | void flash_write_cmd (flash_info_t * info, flash_sect_t sect, |
| 327 | uint offset, u32 cmd) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 328 | { |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 329 | |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 330 | void *addr; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 331 | cfiword_t cword; |
| 332 | |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 333 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 334 | flash_make_cmd (info, cmd, &cword); |
| 335 | switch (info->portwidth) { |
| 336 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 337 | debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd, |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 338 | cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 339 | flash_write8(cword.w8, addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 340 | break; |
| 341 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 342 | debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr, |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 343 | cmd, cword.w16, |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 344 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 345 | flash_write16(cword.w16, addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 346 | break; |
| 347 | case FLASH_CFI_32BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 348 | debug ("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr, |
| 349 | cmd, cword.w32, |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 350 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 351 | flash_write32(cword.w32, addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 352 | break; |
| 353 | case FLASH_CFI_64BIT: |
| 354 | #ifdef DEBUG |
| 355 | { |
| 356 | char str[20]; |
| 357 | |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 358 | print_longlong (str, cword.w64); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 359 | |
| 360 | debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n", |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 361 | addr, cmd, str, |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 362 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 363 | } |
| 364 | #endif |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 365 | flash_write64(cword.w64, addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 366 | break; |
| 367 | } |
| 368 | |
| 369 | /* Ensure all the instructions are fully finished */ |
| 370 | sync(); |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 371 | |
| 372 | flash_unmap(info, sect, offset, addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect) |
| 376 | { |
| 377 | flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START); |
| 378 | flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK); |
| 379 | } |
| 380 | |
| 381 | /*----------------------------------------------------------------------- |
| 382 | */ |
| 383 | static int flash_isequal (flash_info_t * info, flash_sect_t sect, |
| 384 | uint offset, uchar cmd) |
| 385 | { |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 386 | void *addr; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 387 | cfiword_t cword; |
| 388 | int retval; |
| 389 | |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 390 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 391 | flash_make_cmd (info, cmd, &cword); |
| 392 | |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 393 | debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 394 | switch (info->portwidth) { |
| 395 | case FLASH_CFI_8BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 396 | debug ("is= %x %x\n", flash_read8(addr), cword.w8); |
| 397 | retval = (flash_read8(addr) == cword.w8); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 398 | break; |
| 399 | case FLASH_CFI_16BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 400 | debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16); |
| 401 | retval = (flash_read16(addr) == cword.w16); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 402 | break; |
| 403 | case FLASH_CFI_32BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 404 | debug ("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32); |
| 405 | retval = (flash_read32(addr) == cword.w32); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 406 | break; |
| 407 | case FLASH_CFI_64BIT: |
| 408 | #ifdef DEBUG |
| 409 | { |
| 410 | char str1[20]; |
| 411 | char str2[20]; |
| 412 | |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 413 | print_longlong (str1, flash_read64(addr)); |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 414 | print_longlong (str2, cword.w64); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 415 | debug ("is= %s %s\n", str1, str2); |
| 416 | } |
| 417 | #endif |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 418 | retval = (flash_read64(addr) == cword.w64); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 419 | break; |
| 420 | default: |
| 421 | retval = 0; |
| 422 | break; |
| 423 | } |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 424 | flash_unmap(info, sect, offset, addr); |
| 425 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 426 | return retval; |
| 427 | } |
| 428 | |
| 429 | /*----------------------------------------------------------------------- |
| 430 | */ |
| 431 | static int flash_isset (flash_info_t * info, flash_sect_t sect, |
| 432 | uint offset, uchar cmd) |
| 433 | { |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 434 | void *addr; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 435 | cfiword_t cword; |
| 436 | int retval; |
| 437 | |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 438 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 439 | flash_make_cmd (info, cmd, &cword); |
| 440 | switch (info->portwidth) { |
| 441 | case FLASH_CFI_8BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 442 | retval = ((flash_read8(addr) & cword.w8) == cword.w8); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 443 | break; |
| 444 | case FLASH_CFI_16BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 445 | retval = ((flash_read16(addr) & cword.w16) == cword.w16); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 446 | break; |
| 447 | case FLASH_CFI_32BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 448 | retval = ((flash_read32(addr) & cword.w32) == cword.w32); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 449 | break; |
| 450 | case FLASH_CFI_64BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 451 | retval = ((flash_read64(addr) & cword.w64) == cword.w64); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 452 | break; |
| 453 | default: |
| 454 | retval = 0; |
| 455 | break; |
| 456 | } |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 457 | flash_unmap(info, sect, offset, addr); |
| 458 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 459 | return retval; |
| 460 | } |
| 461 | |
| 462 | /*----------------------------------------------------------------------- |
| 463 | */ |
| 464 | static int flash_toggle (flash_info_t * info, flash_sect_t sect, |
| 465 | uint offset, uchar cmd) |
| 466 | { |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 467 | void *addr; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 468 | cfiword_t cword; |
| 469 | int retval; |
| 470 | |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 471 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 472 | flash_make_cmd (info, cmd, &cword); |
| 473 | switch (info->portwidth) { |
| 474 | case FLASH_CFI_8BIT: |
Stefan Roese | fb8c061 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 475 | retval = flash_read8(addr) != flash_read8(addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 476 | break; |
| 477 | case FLASH_CFI_16BIT: |
Stefan Roese | fb8c061 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 478 | retval = flash_read16(addr) != flash_read16(addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 479 | break; |
| 480 | case FLASH_CFI_32BIT: |
Stefan Roese | fb8c061 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 481 | retval = flash_read32(addr) != flash_read32(addr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 482 | break; |
| 483 | case FLASH_CFI_64BIT: |
Wolfgang Denk | 9abda6b | 2008-10-31 01:12:28 +0100 | [diff] [blame] | 484 | retval = ( (flash_read32( addr ) != flash_read32( addr )) || |
| 485 | (flash_read32(addr+4) != flash_read32(addr+4)) ); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 486 | break; |
| 487 | default: |
| 488 | retval = 0; |
| 489 | break; |
| 490 | } |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 491 | flash_unmap(info, sect, offset, addr); |
| 492 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 493 | return retval; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * flash_is_busy - check to see if the flash is busy |
| 498 | * |
| 499 | * This routine checks the status of the chip and returns true if the |
| 500 | * chip is busy. |
| 501 | */ |
| 502 | static int flash_is_busy (flash_info_t * info, flash_sect_t sect) |
| 503 | { |
| 504 | int retval; |
| 505 | |
| 506 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 507 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 508 | case CFI_CMDSET_INTEL_STANDARD: |
| 509 | case CFI_CMDSET_INTEL_EXTENDED: |
| 510 | retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE); |
| 511 | break; |
| 512 | case CFI_CMDSET_AMD_STANDARD: |
| 513 | case CFI_CMDSET_AMD_EXTENDED: |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 514 | #ifdef CONFIG_FLASH_CFI_LEGACY |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 515 | case CFI_CMDSET_AMD_LEGACY: |
| 516 | #endif |
| 517 | retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE); |
| 518 | break; |
| 519 | default: |
| 520 | retval = 0; |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 521 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 522 | debug ("flash_is_busy: %d\n", retval); |
| 523 | return retval; |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 524 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 525 | |
| 526 | /*----------------------------------------------------------------------- |
| 527 | * wait for XSR.7 to be set. Time out with an error if it does not. |
| 528 | * This routine does not set the flash to read-array mode. |
| 529 | */ |
| 530 | static int flash_status_check (flash_info_t * info, flash_sect_t sector, |
| 531 | ulong tout, char *prompt) |
| 532 | { |
| 533 | ulong start; |
| 534 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 535 | #if CONFIG_SYS_HZ != 1000 |
Renato Andreola | c40c94a | 2010-03-24 23:00:47 +0800 | [diff] [blame] | 536 | if ((ulong)CONFIG_SYS_HZ > 100000) |
| 537 | tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */ |
| 538 | else |
| 539 | tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 540 | #endif |
| 541 | |
| 542 | /* Wait for command completion */ |
Graeme Russ | e110c4f | 2011-07-15 02:18:56 +0000 | [diff] [blame] | 543 | #ifdef CONFIG_SYS_LOW_RES_TIMER |
Thomas Chou | 22d6c8f | 2010-04-01 11:15:05 +0800 | [diff] [blame] | 544 | reset_timer(); |
Graeme Russ | e110c4f | 2011-07-15 02:18:56 +0000 | [diff] [blame] | 545 | #endif |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 546 | start = get_timer (0); |
Jens Scharsig (BuS Elektronik) | a9f5fab | 2012-01-27 09:29:53 +0100 | [diff] [blame] | 547 | WATCHDOG_RESET(); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 548 | while (flash_is_busy (info, sector)) { |
| 549 | if (get_timer (start) > tout) { |
| 550 | printf ("Flash %s timeout at address %lx data %lx\n", |
| 551 | prompt, info->start[sector], |
| 552 | flash_read_long (info, sector, 0)); |
| 553 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 554 | udelay(1); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 555 | return ERR_TIMOUT; |
| 556 | } |
| 557 | udelay (1); /* also triggers watchdog */ |
| 558 | } |
| 559 | return ERR_OK; |
| 560 | } |
| 561 | |
| 562 | /*----------------------------------------------------------------------- |
| 563 | * Wait for XSR.7 to be set, if it times out print an error, otherwise |
| 564 | * do a full status check. |
| 565 | * |
| 566 | * This routine sets the flash to read-array mode. |
| 567 | */ |
| 568 | static int flash_full_status_check (flash_info_t * info, flash_sect_t sector, |
| 569 | ulong tout, char *prompt) |
| 570 | { |
| 571 | int retcode; |
| 572 | |
| 573 | retcode = flash_status_check (info, sector, tout, prompt); |
| 574 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 575 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 576 | case CFI_CMDSET_INTEL_EXTENDED: |
| 577 | case CFI_CMDSET_INTEL_STANDARD: |
Baruch Siach | 9169305 | 2014-09-04 12:23:09 +0300 | [diff] [blame] | 578 | if ((retcode == ERR_OK) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 579 | && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) { |
| 580 | retcode = ERR_INVAL; |
| 581 | printf ("Flash %s error at address %lx\n", prompt, |
| 582 | info->start[sector]); |
| 583 | if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | |
| 584 | FLASH_STATUS_PSLBS)) { |
| 585 | puts ("Command Sequence Error.\n"); |
| 586 | } else if (flash_isset (info, sector, 0, |
| 587 | FLASH_STATUS_ECLBS)) { |
| 588 | puts ("Block Erase Error.\n"); |
| 589 | retcode = ERR_NOT_ERASED; |
| 590 | } else if (flash_isset (info, sector, 0, |
| 591 | FLASH_STATUS_PSLBS)) { |
| 592 | puts ("Locking Error\n"); |
| 593 | } |
| 594 | if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) { |
| 595 | puts ("Block locked.\n"); |
| 596 | retcode = ERR_PROTECTED; |
| 597 | } |
| 598 | if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS)) |
| 599 | puts ("Vpp Low Error.\n"); |
| 600 | } |
| 601 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
Aaron Williams | a90b957 | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 602 | udelay(1); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 603 | break; |
| 604 | default: |
| 605 | break; |
| 606 | } |
| 607 | return retcode; |
| 608 | } |
| 609 | |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 610 | static int use_flash_status_poll(flash_info_t *info) |
| 611 | { |
| 612 | #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL |
| 613 | if (info->vendor == CFI_CMDSET_AMD_EXTENDED || |
| 614 | info->vendor == CFI_CMDSET_AMD_STANDARD) |
| 615 | return 1; |
| 616 | #endif |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int flash_status_poll(flash_info_t *info, void *src, void *dst, |
| 621 | ulong tout, char *prompt) |
| 622 | { |
| 623 | #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL |
| 624 | ulong start; |
| 625 | int ready; |
| 626 | |
| 627 | #if CONFIG_SYS_HZ != 1000 |
| 628 | if ((ulong)CONFIG_SYS_HZ > 100000) |
| 629 | tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */ |
| 630 | else |
| 631 | tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000); |
| 632 | #endif |
| 633 | |
| 634 | /* Wait for command completion */ |
Graeme Russ | e110c4f | 2011-07-15 02:18:56 +0000 | [diff] [blame] | 635 | #ifdef CONFIG_SYS_LOW_RES_TIMER |
Thomas Chou | 22d6c8f | 2010-04-01 11:15:05 +0800 | [diff] [blame] | 636 | reset_timer(); |
Graeme Russ | e110c4f | 2011-07-15 02:18:56 +0000 | [diff] [blame] | 637 | #endif |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 638 | start = get_timer(0); |
Jens Scharsig (BuS Elektronik) | a9f5fab | 2012-01-27 09:29:53 +0100 | [diff] [blame] | 639 | WATCHDOG_RESET(); |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 640 | while (1) { |
| 641 | switch (info->portwidth) { |
| 642 | case FLASH_CFI_8BIT: |
| 643 | ready = flash_read8(dst) == flash_read8(src); |
| 644 | break; |
| 645 | case FLASH_CFI_16BIT: |
| 646 | ready = flash_read16(dst) == flash_read16(src); |
| 647 | break; |
| 648 | case FLASH_CFI_32BIT: |
| 649 | ready = flash_read32(dst) == flash_read32(src); |
| 650 | break; |
| 651 | case FLASH_CFI_64BIT: |
| 652 | ready = flash_read64(dst) == flash_read64(src); |
| 653 | break; |
| 654 | default: |
| 655 | ready = 0; |
| 656 | break; |
| 657 | } |
| 658 | if (ready) |
| 659 | break; |
| 660 | if (get_timer(start) > tout) { |
| 661 | printf("Flash %s timeout at address %lx data %lx\n", |
| 662 | prompt, (ulong)dst, (ulong)flash_read8(dst)); |
| 663 | return ERR_TIMOUT; |
| 664 | } |
| 665 | udelay(1); /* also triggers watchdog */ |
| 666 | } |
| 667 | #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */ |
| 668 | return ERR_OK; |
| 669 | } |
| 670 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 671 | /*----------------------------------------------------------------------- |
| 672 | */ |
| 673 | static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c) |
| 674 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 675 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 676 | unsigned short w; |
| 677 | unsigned int l; |
| 678 | unsigned long long ll; |
| 679 | #endif |
| 680 | |
| 681 | switch (info->portwidth) { |
| 682 | case FLASH_CFI_8BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 683 | cword->w8 = c; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 684 | break; |
| 685 | case FLASH_CFI_16BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 686 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 687 | w = c; |
| 688 | w <<= 8; |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 689 | cword->w16 = (cword->w16 >> 8) | w; |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 690 | #else |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 691 | cword->w16 = (cword->w16 << 8) | c; |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 692 | #endif |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 693 | break; |
| 694 | case FLASH_CFI_32BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 695 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 696 | l = c; |
| 697 | l <<= 24; |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 698 | cword->w32 = (cword->w32 >> 8) | l; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 699 | #else |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 700 | cword->w32 = (cword->w32 << 8) | c; |
Stefan Roese | 2662b40 | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 701 | #endif |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 702 | break; |
| 703 | case FLASH_CFI_64BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 704 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 705 | ll = c; |
| 706 | ll <<= 56; |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 707 | cword->w64 = (cword->w64 >> 8) | ll; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 708 | #else |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 709 | cword->w64 = (cword->w64 << 8) | c; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 710 | #endif |
| 711 | break; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 712 | } |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Jens Gehrlein | 0f8e851 | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 715 | /* |
| 716 | * Loop through the sector table starting from the previously found sector. |
| 717 | * Searches forwards or backwards, dependent on the passed address. |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 718 | */ |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 719 | static flash_sect_t find_sector (flash_info_t * info, ulong addr) |
wdenk | 7680c14 | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 720 | { |
Kim Phillips | 11dc401 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 721 | static flash_sect_t saved_sector; /* previously found sector */ |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 722 | static flash_info_t *saved_info; /* previously used flash bank */ |
Jens Gehrlein | 0f8e851 | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 723 | flash_sect_t sector = saved_sector; |
wdenk | 7680c14 | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 724 | |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 725 | if ((info != saved_info) || (sector >= info->sector_count)) |
| 726 | sector = 0; |
| 727 | |
Jens Gehrlein | 0f8e851 | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 728 | while ((info->start[sector] < addr) |
| 729 | && (sector < info->sector_count - 1)) |
| 730 | sector++; |
| 731 | while ((info->start[sector] > addr) && (sector > 0)) |
| 732 | /* |
| 733 | * also decrements the sector in case of an overshot |
| 734 | * in the first loop |
| 735 | */ |
| 736 | sector--; |
| 737 | |
| 738 | saved_sector = sector; |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 739 | saved_info = info; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 740 | return sector; |
wdenk | 7680c14 | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 741 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 742 | |
| 743 | /*----------------------------------------------------------------------- |
| 744 | */ |
| 745 | static int flash_write_cfiword (flash_info_t * info, ulong dest, |
| 746 | cfiword_t cword) |
| 747 | { |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 748 | void *dstaddr = (void *)dest; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 749 | int flag; |
Jens Gehrlein | a729287 | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 750 | flash_sect_t sect = 0; |
| 751 | char sect_found = 0; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 752 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 753 | /* Check if Flash is (sufficiently) erased */ |
| 754 | switch (info->portwidth) { |
| 755 | case FLASH_CFI_8BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 756 | flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 757 | break; |
| 758 | case FLASH_CFI_16BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 759 | flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 760 | break; |
| 761 | case FLASH_CFI_32BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 762 | flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 763 | break; |
| 764 | case FLASH_CFI_64BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 765 | flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 766 | break; |
| 767 | default: |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 768 | flag = 0; |
| 769 | break; |
| 770 | } |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 771 | if (!flag) |
Stefan Roese | 0dc80e2 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 772 | return ERR_NOT_ERASED; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 773 | |
| 774 | /* Disable interrupts which might cause a timeout here */ |
| 775 | flag = disable_interrupts (); |
| 776 | |
| 777 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 778 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 779 | case CFI_CMDSET_INTEL_EXTENDED: |
| 780 | case CFI_CMDSET_INTEL_STANDARD: |
| 781 | flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS); |
| 782 | flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE); |
| 783 | break; |
| 784 | case CFI_CMDSET_AMD_EXTENDED: |
| 785 | case CFI_CMDSET_AMD_STANDARD: |
Ed Swarthout | 0d01f66 | 2008-10-09 01:26:36 -0500 | [diff] [blame] | 786 | sect = find_sector(info, dest); |
| 787 | flash_unlock_seq (info, sect); |
| 788 | flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE); |
Jens Gehrlein | a729287 | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 789 | sect_found = 1; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 790 | break; |
Po-Yu Chuang | b4db4a7 | 2009-07-10 18:03:57 +0800 | [diff] [blame] | 791 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 792 | case CFI_CMDSET_AMD_LEGACY: |
| 793 | sect = find_sector(info, dest); |
| 794 | flash_unlock_seq (info, 0); |
| 795 | flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE); |
| 796 | sect_found = 1; |
| 797 | break; |
| 798 | #endif |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | switch (info->portwidth) { |
| 802 | case FLASH_CFI_8BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 803 | flash_write8(cword.w8, dstaddr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 804 | break; |
| 805 | case FLASH_CFI_16BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 806 | flash_write16(cword.w16, dstaddr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 807 | break; |
| 808 | case FLASH_CFI_32BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 809 | flash_write32(cword.w32, dstaddr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 810 | break; |
| 811 | case FLASH_CFI_64BIT: |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 812 | flash_write64(cword.w64, dstaddr); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 813 | break; |
| 814 | } |
| 815 | |
| 816 | /* re-enable interrupts if necessary */ |
| 817 | if (flag) |
| 818 | enable_interrupts (); |
| 819 | |
Jens Gehrlein | a729287 | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 820 | if (!sect_found) |
| 821 | sect = find_sector (info, dest); |
| 822 | |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 823 | if (use_flash_status_poll(info)) |
| 824 | return flash_status_poll(info, &cword, dstaddr, |
| 825 | info->write_tout, "write"); |
| 826 | else |
| 827 | return flash_full_status_check(info, sect, |
| 828 | info->write_tout, "write"); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 831 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 832 | |
| 833 | static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, |
| 834 | int len) |
| 835 | { |
| 836 | flash_sect_t sector; |
| 837 | int cnt; |
| 838 | int retcode; |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 839 | void *src = cp; |
Stefan Roese | ec21d5c | 2009-02-05 11:25:57 +0100 | [diff] [blame] | 840 | void *dst = (void *)dest; |
Stefan Roese | 0dc80e2 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 841 | void *dst2 = dst; |
Tao Hou | 85c344e | 2012-03-15 23:33:58 +0800 | [diff] [blame] | 842 | int flag = 1; |
Guennadi Liakhovetski | 96ef831 | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 843 | uint offset = 0; |
| 844 | unsigned int shift; |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 845 | uchar write_cmd; |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 846 | |
Stefan Roese | 0dc80e2 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 847 | switch (info->portwidth) { |
| 848 | case FLASH_CFI_8BIT: |
Guennadi Liakhovetski | 96ef831 | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 849 | shift = 0; |
Stefan Roese | 0dc80e2 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 850 | break; |
| 851 | case FLASH_CFI_16BIT: |
Guennadi Liakhovetski | 96ef831 | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 852 | shift = 1; |
Stefan Roese | 0dc80e2 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 853 | break; |
| 854 | case FLASH_CFI_32BIT: |
Guennadi Liakhovetski | 96ef831 | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 855 | shift = 2; |
Stefan Roese | 0dc80e2 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 856 | break; |
| 857 | case FLASH_CFI_64BIT: |
Guennadi Liakhovetski | 96ef831 | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 858 | shift = 3; |
Stefan Roese | 0dc80e2 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 859 | break; |
| 860 | default: |
| 861 | retcode = ERR_INVAL; |
| 862 | goto out_unmap; |
| 863 | } |
| 864 | |
Guennadi Liakhovetski | 96ef831 | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 865 | cnt = len >> shift; |
| 866 | |
Tao Hou | 85c344e | 2012-03-15 23:33:58 +0800 | [diff] [blame] | 867 | while ((cnt-- > 0) && (flag == 1)) { |
Stefan Roese | 0dc80e2 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 868 | switch (info->portwidth) { |
| 869 | case FLASH_CFI_8BIT: |
| 870 | flag = ((flash_read8(dst2) & flash_read8(src)) == |
| 871 | flash_read8(src)); |
| 872 | src += 1, dst2 += 1; |
| 873 | break; |
| 874 | case FLASH_CFI_16BIT: |
| 875 | flag = ((flash_read16(dst2) & flash_read16(src)) == |
| 876 | flash_read16(src)); |
| 877 | src += 2, dst2 += 2; |
| 878 | break; |
| 879 | case FLASH_CFI_32BIT: |
| 880 | flag = ((flash_read32(dst2) & flash_read32(src)) == |
| 881 | flash_read32(src)); |
| 882 | src += 4, dst2 += 4; |
| 883 | break; |
| 884 | case FLASH_CFI_64BIT: |
| 885 | flag = ((flash_read64(dst2) & flash_read64(src)) == |
| 886 | flash_read64(src)); |
| 887 | src += 8, dst2 += 8; |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | if (!flag) { |
| 892 | retcode = ERR_NOT_ERASED; |
| 893 | goto out_unmap; |
| 894 | } |
| 895 | |
| 896 | src = cp; |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 897 | sector = find_sector (info, dest); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 898 | |
| 899 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 900 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 901 | case CFI_CMDSET_INTEL_STANDARD: |
| 902 | case CFI_CMDSET_INTEL_EXTENDED: |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 903 | write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ? |
| 904 | FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 905 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 906 | flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS); |
| 907 | flash_write_cmd (info, sector, 0, write_cmd); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 908 | retcode = flash_status_check (info, sector, |
| 909 | info->buffer_write_tout, |
| 910 | "write to buffer"); |
| 911 | if (retcode == ERR_OK) { |
| 912 | /* reduce the number of loops by the width of |
| 913 | * the port */ |
Guennadi Liakhovetski | 96ef831 | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 914 | cnt = len >> shift; |
Vasiliy Leoenenko | 93c56f2 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 915 | flash_write_cmd (info, sector, 0, cnt - 1); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 916 | while (cnt-- > 0) { |
| 917 | switch (info->portwidth) { |
| 918 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 919 | flash_write8(flash_read8(src), dst); |
| 920 | src += 1, dst += 1; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 921 | break; |
| 922 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 923 | flash_write16(flash_read16(src), dst); |
| 924 | src += 2, dst += 2; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 925 | break; |
| 926 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 927 | flash_write32(flash_read32(src), dst); |
| 928 | src += 4, dst += 4; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 929 | break; |
| 930 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 931 | flash_write64(flash_read64(src), dst); |
| 932 | src += 8, dst += 8; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 933 | break; |
| 934 | default: |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 935 | retcode = ERR_INVAL; |
| 936 | goto out_unmap; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | flash_write_cmd (info, sector, 0, |
| 940 | FLASH_CMD_WRITE_BUFFER_CONFIRM); |
| 941 | retcode = flash_full_status_check ( |
| 942 | info, sector, info->buffer_write_tout, |
| 943 | "buffer write"); |
| 944 | } |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 945 | |
| 946 | break; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 947 | |
| 948 | case CFI_CMDSET_AMD_STANDARD: |
| 949 | case CFI_CMDSET_AMD_EXTENDED: |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 950 | flash_unlock_seq(info,0); |
Guennadi Liakhovetski | 96ef831 | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 951 | |
| 952 | #ifdef CONFIG_FLASH_SPANSION_S29WS_N |
| 953 | offset = ((unsigned long)dst - info->start[sector]) >> shift; |
| 954 | #endif |
| 955 | flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER); |
| 956 | cnt = len >> shift; |
John Schmoller | 7dedefd | 2009-08-12 10:55:47 -0500 | [diff] [blame] | 957 | flash_write_cmd(info, sector, offset, cnt - 1); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 958 | |
| 959 | switch (info->portwidth) { |
| 960 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 961 | while (cnt-- > 0) { |
| 962 | flash_write8(flash_read8(src), dst); |
| 963 | src += 1, dst += 1; |
| 964 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 965 | break; |
| 966 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 967 | while (cnt-- > 0) { |
| 968 | flash_write16(flash_read16(src), dst); |
| 969 | src += 2, dst += 2; |
| 970 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 971 | break; |
| 972 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 973 | while (cnt-- > 0) { |
| 974 | flash_write32(flash_read32(src), dst); |
| 975 | src += 4, dst += 4; |
| 976 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 977 | break; |
| 978 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | cdbaefb | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 979 | while (cnt-- > 0) { |
| 980 | flash_write64(flash_read64(src), dst); |
| 981 | src += 8, dst += 8; |
| 982 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 983 | break; |
| 984 | default: |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 985 | retcode = ERR_INVAL; |
| 986 | goto out_unmap; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM); |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 990 | if (use_flash_status_poll(info)) |
| 991 | retcode = flash_status_poll(info, src - (1 << shift), |
| 992 | dst - (1 << shift), |
| 993 | info->buffer_write_tout, |
| 994 | "buffer write"); |
| 995 | else |
| 996 | retcode = flash_full_status_check(info, sector, |
| 997 | info->buffer_write_tout, |
| 998 | "buffer write"); |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 999 | break; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1000 | |
| 1001 | default: |
| 1002 | debug ("Unknown Command Set\n"); |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1003 | retcode = ERR_INVAL; |
| 1004 | break; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1005 | } |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1006 | |
| 1007 | out_unmap: |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1008 | return retcode; |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1009 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1010 | #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */ |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1011 | |
wdenk | 7680c14 | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 1012 | |
| 1013 | /*----------------------------------------------------------------------- |
| 1014 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1015 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1016 | { |
| 1017 | int rcode = 0; |
| 1018 | int prot; |
| 1019 | flash_sect_t sect; |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1020 | int st; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1021 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1022 | if (info->flash_id != FLASH_MAN_CFI) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1023 | puts ("Can't erase unknown flash type - aborted\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1024 | return 1; |
| 1025 | } |
| 1026 | if ((s_first < 0) || (s_first > s_last)) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1027 | puts ("- no sectors to erase\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1028 | return 1; |
| 1029 | } |
| 1030 | |
| 1031 | prot = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1032 | for (sect = s_first; sect <= s_last; ++sect) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1033 | if (info->protect[sect]) { |
| 1034 | prot++; |
| 1035 | } |
| 1036 | } |
| 1037 | if (prot) { |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1038 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 1039 | prot); |
Piotr Ziecik | 6ea808e | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1040 | } else if (flash_verbose) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1041 | putc ('\n'); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1045 | for (sect = s_first; sect <= s_last; sect++) { |
Joe Hershberger | de15a06 | 2012-08-17 15:36:41 -0500 | [diff] [blame] | 1046 | if (ctrlc()) { |
| 1047 | printf("\n"); |
| 1048 | return 1; |
| 1049 | } |
| 1050 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1051 | if (info->protect[sect] == 0) { /* not protected */ |
Joe Hershberger | 6822a64 | 2012-08-17 15:36:40 -0500 | [diff] [blame] | 1052 | #ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE |
| 1053 | int k; |
| 1054 | int size; |
| 1055 | int erased; |
| 1056 | u32 *flash; |
| 1057 | |
| 1058 | /* |
| 1059 | * Check if whole sector is erased |
| 1060 | */ |
| 1061 | size = flash_sector_size(info, sect); |
| 1062 | erased = 1; |
| 1063 | flash = (u32 *)info->start[sect]; |
| 1064 | /* divide by 4 for longword access */ |
| 1065 | size = size >> 2; |
| 1066 | for (k = 0; k < size; k++) { |
| 1067 | if (flash_read32(flash++) != 0xffffffff) { |
| 1068 | erased = 0; |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | if (erased) { |
| 1073 | if (flash_verbose) |
| 1074 | putc(','); |
| 1075 | continue; |
| 1076 | } |
| 1077 | #endif |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1078 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1079 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1080 | case CFI_CMDSET_INTEL_STANDARD: |
| 1081 | case CFI_CMDSET_INTEL_EXTENDED: |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1082 | flash_write_cmd (info, sect, 0, |
| 1083 | FLASH_CMD_CLEAR_STATUS); |
| 1084 | flash_write_cmd (info, sect, 0, |
| 1085 | FLASH_CMD_BLOCK_ERASE); |
| 1086 | flash_write_cmd (info, sect, 0, |
| 1087 | FLASH_CMD_ERASE_CONFIRM); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1088 | break; |
| 1089 | case CFI_CMDSET_AMD_STANDARD: |
| 1090 | case CFI_CMDSET_AMD_EXTENDED: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1091 | flash_unlock_seq (info, sect); |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1092 | flash_write_cmd (info, sect, |
| 1093 | info->addr_unlock1, |
| 1094 | AMD_CMD_ERASE_START); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1095 | flash_unlock_seq (info, sect); |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1096 | flash_write_cmd (info, sect, 0, |
Angelo Dureghello | 07b2c5c | 2012-12-01 01:14:18 +0100 | [diff] [blame] | 1097 | info->cmd_erase_sector); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1098 | break; |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1099 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1100 | case CFI_CMDSET_AMD_LEGACY: |
| 1101 | flash_unlock_seq (info, 0); |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1102 | flash_write_cmd (info, 0, info->addr_unlock1, |
| 1103 | AMD_CMD_ERASE_START); |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1104 | flash_unlock_seq (info, 0); |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1105 | flash_write_cmd (info, sect, 0, |
| 1106 | AMD_CMD_ERASE_SECTOR); |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1107 | break; |
| 1108 | #endif |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1109 | default: |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1110 | debug ("Unkown flash vendor %d\n", |
| 1111 | info->vendor); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1112 | break; |
| 1113 | } |
| 1114 | |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1115 | if (use_flash_status_poll(info)) { |
Kim Phillips | 11dc401 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 1116 | cfiword_t cword; |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1117 | void *dest; |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 1118 | cword.w64 = 0xffffffffffffffffULL; |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1119 | dest = flash_map(info, sect, 0); |
| 1120 | st = flash_status_poll(info, &cword, dest, |
| 1121 | info->erase_blk_tout, "erase"); |
| 1122 | flash_unmap(info, sect, 0, dest); |
| 1123 | } else |
| 1124 | st = flash_full_status_check(info, sect, |
| 1125 | info->erase_blk_tout, |
| 1126 | "erase"); |
| 1127 | if (st) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1128 | rcode = 1; |
Thomas Chou | e572082 | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1129 | else if (flash_verbose) |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1130 | putc ('.'); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1131 | } |
| 1132 | } |
Piotr Ziecik | 6ea808e | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1133 | |
| 1134 | if (flash_verbose) |
| 1135 | puts (" done\n"); |
| 1136 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1137 | return rcode; |
| 1138 | } |
| 1139 | |
Stefan Roese | 70084df | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1140 | #ifdef CONFIG_SYS_FLASH_EMPTY_INFO |
| 1141 | static int sector_erased(flash_info_t *info, int i) |
| 1142 | { |
| 1143 | int k; |
| 1144 | int size; |
Stefan Roese | 4d2ca9d | 2010-10-25 18:31:39 +0200 | [diff] [blame] | 1145 | u32 *flash; |
Stefan Roese | 70084df | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1146 | |
| 1147 | /* |
| 1148 | * Check if whole sector is erased |
| 1149 | */ |
| 1150 | size = flash_sector_size(info, i); |
Stefan Roese | 4d2ca9d | 2010-10-25 18:31:39 +0200 | [diff] [blame] | 1151 | flash = (u32 *)info->start[i]; |
Stefan Roese | 70084df | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1152 | /* divide by 4 for longword access */ |
| 1153 | size = size >> 2; |
| 1154 | |
| 1155 | for (k = 0; k < size; k++) { |
Stefan Roese | 4d2ca9d | 2010-10-25 18:31:39 +0200 | [diff] [blame] | 1156 | if (flash_read32(flash++) != 0xffffffff) |
Stefan Roese | 70084df | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1157 | return 0; /* not erased */ |
| 1158 | } |
| 1159 | |
| 1160 | return 1; /* erased */ |
| 1161 | } |
| 1162 | #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */ |
| 1163 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1164 | void flash_print_info (flash_info_t * info) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1165 | { |
| 1166 | int i; |
| 1167 | |
| 1168 | if (info->flash_id != FLASH_MAN_CFI) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1169 | puts ("missing or unknown FLASH type\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1170 | return; |
| 1171 | } |
| 1172 | |
Peter Tyser | eddf52b | 2010-12-28 18:12:05 -0600 | [diff] [blame] | 1173 | printf ("%s flash (%d x %d)", |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1174 | info->name, |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1175 | (info->portwidth << 3), (info->chipwidth << 3)); |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1176 | if (info->size < 1024*1024) |
| 1177 | printf (" Size: %ld kB in %d Sectors\n", |
| 1178 | info->size >> 10, info->sector_count); |
| 1179 | else |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1180 | printf (" Size: %ld MB in %d Sectors\n", |
| 1181 | info->size >> 20, info->sector_count); |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1182 | printf (" "); |
| 1183 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1184 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
| 1185 | printf ("Intel Prog Regions"); |
| 1186 | break; |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1187 | case CFI_CMDSET_INTEL_STANDARD: |
| 1188 | printf ("Intel Standard"); |
| 1189 | break; |
| 1190 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1191 | printf ("Intel Extended"); |
| 1192 | break; |
| 1193 | case CFI_CMDSET_AMD_STANDARD: |
| 1194 | printf ("AMD Standard"); |
| 1195 | break; |
| 1196 | case CFI_CMDSET_AMD_EXTENDED: |
| 1197 | printf ("AMD Extended"); |
| 1198 | break; |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1199 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1200 | case CFI_CMDSET_AMD_LEGACY: |
| 1201 | printf ("AMD Legacy"); |
| 1202 | break; |
| 1203 | #endif |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1204 | default: |
| 1205 | printf ("Unknown (%d)", info->vendor); |
| 1206 | break; |
| 1207 | } |
Philippe De Muyter | d77c7ac | 2010-08-10 16:54:52 +0200 | [diff] [blame] | 1208 | printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x", |
| 1209 | info->manufacturer_id); |
| 1210 | printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X", |
| 1211 | info->device_id); |
Heiko Schocher | 5b448ad | 2011-04-11 14:16:19 +0200 | [diff] [blame] | 1212 | if ((info->device_id & 0xff) == 0x7E) { |
| 1213 | printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X", |
| 1214 | info->device_id2); |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1215 | } |
Stefan Roese | d2af028 | 2012-12-06 15:44:12 +0100 | [diff] [blame] | 1216 | if ((info->vendor == CFI_CMDSET_AMD_STANDARD) && (info->legacy_unlock)) |
| 1217 | printf("\n Advanced Sector Protection (PPB) enabled"); |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1218 | printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n", |
wdenk | 028ab6b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 1219 | info->erase_blk_tout, |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1220 | info->write_tout); |
| 1221 | if (info->buffer_size > 1) { |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1222 | printf (" Buffer write timeout: %ld ms, " |
| 1223 | "buffer size: %d bytes\n", |
wdenk | 028ab6b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 1224 | info->buffer_write_tout, |
| 1225 | info->buffer_size); |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1226 | } |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1227 | |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1228 | puts ("\n Sector Start Addresses:"); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1229 | for (i = 0; i < info->sector_count; ++i) { |
Kim Phillips | 2e97394 | 2010-07-26 18:35:39 -0500 | [diff] [blame] | 1230 | if (ctrlc()) |
Stefan Roese | 70084df | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1231 | break; |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1232 | if ((i % 5) == 0) |
Stefan Roese | 70084df | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1233 | putc('\n'); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1234 | #ifdef CONFIG_SYS_FLASH_EMPTY_INFO |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1235 | /* print empty and read-only info */ |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1236 | printf (" %08lX %c %s ", |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1237 | info->start[i], |
Stefan Roese | 70084df | 2010-08-13 09:36:36 +0200 | [diff] [blame] | 1238 | sector_erased(info, i) ? 'E' : ' ', |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1239 | info->protect[i] ? "RO" : " "); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1240 | #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */ |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1241 | printf (" %08lX %s ", |
| 1242 | info->start[i], |
| 1243 | info->protect[i] ? "RO" : " "); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1244 | #endif |
| 1245 | } |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1246 | putc ('\n'); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | /*----------------------------------------------------------------------- |
Jerry Van Baren | 9a042e9 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1251 | * This is used in a few places in write_buf() to show programming |
| 1252 | * progress. Making it a function is nasty because it needs to do side |
| 1253 | * effect updates to digit and dots. Repeated code is nasty too, so |
| 1254 | * we define it once here. |
| 1255 | */ |
Stefan Roese | f010572 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1256 | #ifdef CONFIG_FLASH_SHOW_PROGRESS |
| 1257 | #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \ |
Piotr Ziecik | 6ea808e | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1258 | if (flash_verbose) { \ |
| 1259 | dots -= dots_sub; \ |
| 1260 | if ((scale > 0) && (dots <= 0)) { \ |
| 1261 | if ((digit % 5) == 0) \ |
| 1262 | printf ("%d", digit / 5); \ |
| 1263 | else \ |
| 1264 | putc ('.'); \ |
| 1265 | digit--; \ |
| 1266 | dots += scale; \ |
| 1267 | } \ |
Jerry Van Baren | 9a042e9 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1268 | } |
Stefan Roese | f010572 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1269 | #else |
| 1270 | #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) |
| 1271 | #endif |
Jerry Van Baren | 9a042e9 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1272 | |
| 1273 | /*----------------------------------------------------------------------- |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1274 | * Copy memory to flash, returns: |
| 1275 | * 0 - OK |
| 1276 | * 1 - write timeout |
| 1277 | * 2 - Flash not erased |
| 1278 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1279 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1280 | { |
| 1281 | ulong wp; |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1282 | uchar *p; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1283 | int aln; |
| 1284 | cfiword_t cword; |
| 1285 | int i, rc; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1286 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1287 | int buffered_size; |
| 1288 | #endif |
Jerry Van Baren | 9a042e9 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1289 | #ifdef CONFIG_FLASH_SHOW_PROGRESS |
| 1290 | int digit = CONFIG_FLASH_SHOW_PROGRESS; |
| 1291 | int scale = 0; |
| 1292 | int dots = 0; |
| 1293 | |
| 1294 | /* |
| 1295 | * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes. |
| 1296 | */ |
| 1297 | if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) { |
| 1298 | scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) / |
| 1299 | CONFIG_FLASH_SHOW_PROGRESS); |
| 1300 | } |
| 1301 | #endif |
| 1302 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1303 | /* get lower aligned address */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1304 | wp = (addr & ~(info->portwidth - 1)); |
| 1305 | |
| 1306 | /* handle unaligned start */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1307 | if ((aln = addr - wp) != 0) { |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 1308 | cword.w32 = 0; |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1309 | p = (uchar *)wp; |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1310 | for (i = 0; i < aln; ++i) |
| 1311 | flash_add_byte (info, &cword, flash_read8(p + i)); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1312 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1313 | for (; (i < info->portwidth) && (cnt > 0); i++) { |
| 1314 | flash_add_byte (info, &cword, *src++); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1315 | cnt--; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1316 | } |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1317 | for (; (cnt == 0) && (i < info->portwidth); ++i) |
| 1318 | flash_add_byte (info, &cword, flash_read8(p + i)); |
| 1319 | |
| 1320 | rc = flash_write_cfiword (info, wp, cword); |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1321 | if (rc != 0) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1322 | return rc; |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1323 | |
| 1324 | wp += i; |
Stefan Roese | f010572 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1325 | FLASH_SHOW_PROGRESS(scale, dots, digit, i); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1328 | /* handle the aligned part */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1329 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1330 | buffered_size = (info->portwidth / info->chipwidth); |
| 1331 | buffered_size *= info->buffer_size; |
| 1332 | while (cnt >= info->portwidth) { |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1333 | /* prohibit buffer write when buffer_size is 1 */ |
| 1334 | if (info->buffer_size == 1) { |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 1335 | cword.w32 = 0; |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 1336 | for (i = 0; i < info->portwidth; i++) |
| 1337 | flash_add_byte (info, &cword, *src++); |
| 1338 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
| 1339 | return rc; |
| 1340 | wp += info->portwidth; |
| 1341 | cnt -= info->portwidth; |
| 1342 | continue; |
| 1343 | } |
| 1344 | |
| 1345 | /* write buffer until next buffered_size aligned boundary */ |
| 1346 | i = buffered_size - (wp % buffered_size); |
| 1347 | if (i > cnt) |
| 1348 | i = cnt; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1349 | if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1350 | return rc; |
Wolfgang Denk | 8d4ba3d | 2005-08-12 22:35:59 +0200 | [diff] [blame] | 1351 | i -= i & (info->portwidth - 1); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1352 | wp += i; |
| 1353 | src += i; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1354 | cnt -= i; |
Stefan Roese | f010572 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1355 | FLASH_SHOW_PROGRESS(scale, dots, digit, i); |
Joe Hershberger | de15a06 | 2012-08-17 15:36:41 -0500 | [diff] [blame] | 1356 | /* Only check every once in a while */ |
| 1357 | if ((cnt & 0xFFFF) < buffered_size && ctrlc()) |
| 1358 | return ERR_ABORTED; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1359 | } |
| 1360 | #else |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1361 | while (cnt >= info->portwidth) { |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 1362 | cword.w32 = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1363 | for (i = 0; i < info->portwidth; i++) { |
| 1364 | flash_add_byte (info, &cword, *src++); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1365 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1366 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1367 | return rc; |
| 1368 | wp += info->portwidth; |
| 1369 | cnt -= info->portwidth; |
Stefan Roese | f010572 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1370 | FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth); |
Joe Hershberger | de15a06 | 2012-08-17 15:36:41 -0500 | [diff] [blame] | 1371 | /* Only check every once in a while */ |
| 1372 | if ((cnt & 0xFFFF) < info->portwidth && ctrlc()) |
| 1373 | return ERR_ABORTED; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1374 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1375 | #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */ |
Jerry Van Baren | 9a042e9 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1376 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1377 | if (cnt == 0) { |
| 1378 | return (0); |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * handle unaligned tail bytes |
| 1383 | */ |
Ryan Harkin | 622b952 | 2015-10-23 16:50:51 +0100 | [diff] [blame] | 1384 | cword.w32 = 0; |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1385 | p = (uchar *)wp; |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1386 | for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1387 | flash_add_byte (info, &cword, *src++); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1388 | --cnt; |
| 1389 | } |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1390 | for (; i < info->portwidth; ++i) |
| 1391 | flash_add_byte (info, &cword, flash_read8(p + i)); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1392 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1393 | return flash_write_cfiword (info, wp, cword); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Stefan Roese | 20043a4 | 2012-12-06 15:44:09 +0100 | [diff] [blame] | 1396 | static inline int manufact_match(flash_info_t *info, u32 manu) |
| 1397 | { |
| 1398 | return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16); |
| 1399 | } |
| 1400 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1401 | /*----------------------------------------------------------------------- |
| 1402 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1403 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1404 | |
Holger Brunck | 81316a9 | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1405 | static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot) |
| 1406 | { |
Stefan Roese | 20043a4 | 2012-12-06 15:44:09 +0100 | [diff] [blame] | 1407 | if (manufact_match(info, INTEL_MANUFACT) |
Kim Phillips | 11dc401 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 1408 | && info->device_id == NUMONYX_256MBIT) { |
Holger Brunck | 81316a9 | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1409 | /* |
| 1410 | * see errata called |
| 1411 | * "Numonyx Axcell P33/P30 Specification Update" :) |
| 1412 | */ |
| 1413 | flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID); |
| 1414 | if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT, |
| 1415 | prot)) { |
| 1416 | /* |
| 1417 | * cmd must come before FLASH_CMD_PROTECT + 20us |
| 1418 | * Disable interrupts which might cause a timeout here. |
| 1419 | */ |
| 1420 | int flag = disable_interrupts(); |
| 1421 | unsigned short cmd; |
| 1422 | |
| 1423 | if (prot) |
| 1424 | cmd = FLASH_CMD_PROTECT_SET; |
| 1425 | else |
| 1426 | cmd = FLASH_CMD_PROTECT_CLEAR; |
| 1427 | flash_write_cmd(info, sector, 0, |
| 1428 | FLASH_CMD_PROTECT); |
| 1429 | flash_write_cmd(info, sector, 0, cmd); |
| 1430 | /* re-enable interrupts if necessary */ |
| 1431 | if (flag) |
| 1432 | enable_interrupts(); |
| 1433 | } |
| 1434 | return 1; |
| 1435 | } |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1439 | int flash_real_protect (flash_info_t * info, long sector, int prot) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1440 | { |
| 1441 | int retcode = 0; |
| 1442 | |
Rafael Campos | bc9019e | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1443 | switch (info->vendor) { |
| 1444 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
| 1445 | case CFI_CMDSET_INTEL_STANDARD: |
Nick Spence | 9e8e63c | 2008-08-19 22:21:16 -0700 | [diff] [blame] | 1446 | case CFI_CMDSET_INTEL_EXTENDED: |
Holger Brunck | 81316a9 | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1447 | if (!cfi_protect_bugfix(info, sector, prot)) { |
| 1448 | flash_write_cmd(info, sector, 0, |
| 1449 | FLASH_CMD_CLEAR_STATUS); |
| 1450 | flash_write_cmd(info, sector, 0, |
| 1451 | FLASH_CMD_PROTECT); |
Philippe De Muyter | 5465299 | 2010-08-17 18:40:25 +0200 | [diff] [blame] | 1452 | if (prot) |
Holger Brunck | 81316a9 | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1453 | flash_write_cmd(info, sector, 0, |
| 1454 | FLASH_CMD_PROTECT_SET); |
Philippe De Muyter | 5465299 | 2010-08-17 18:40:25 +0200 | [diff] [blame] | 1455 | else |
Holger Brunck | 81316a9 | 2012-08-09 10:22:41 +0200 | [diff] [blame] | 1456 | flash_write_cmd(info, sector, 0, |
| 1457 | FLASH_CMD_PROTECT_CLEAR); |
Philippe De Muyter | 5465299 | 2010-08-17 18:40:25 +0200 | [diff] [blame] | 1458 | |
Philippe De Muyter | 5465299 | 2010-08-17 18:40:25 +0200 | [diff] [blame] | 1459 | } |
Rafael Campos | bc9019e | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1460 | break; |
| 1461 | case CFI_CMDSET_AMD_EXTENDED: |
| 1462 | case CFI_CMDSET_AMD_STANDARD: |
Rafael Campos | bc9019e | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1463 | /* U-Boot only checks the first byte */ |
Stefan Roese | 20043a4 | 2012-12-06 15:44:09 +0100 | [diff] [blame] | 1464 | if (manufact_match(info, ATM_MANUFACT)) { |
Rafael Campos | bc9019e | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1465 | if (prot) { |
| 1466 | flash_unlock_seq (info, 0); |
| 1467 | flash_write_cmd (info, 0, |
| 1468 | info->addr_unlock1, |
| 1469 | ATM_CMD_SOFTLOCK_START); |
| 1470 | flash_unlock_seq (info, 0); |
| 1471 | flash_write_cmd (info, sector, 0, |
| 1472 | ATM_CMD_LOCK_SECT); |
| 1473 | } else { |
| 1474 | flash_write_cmd (info, 0, |
| 1475 | info->addr_unlock1, |
| 1476 | AMD_CMD_UNLOCK_START); |
| 1477 | if (info->device_id == ATM_ID_BV6416) |
| 1478 | flash_write_cmd (info, sector, |
| 1479 | 0, ATM_CMD_UNLOCK_SECT); |
| 1480 | } |
| 1481 | } |
Stefan Roese | ac6b911 | 2012-12-06 15:44:11 +0100 | [diff] [blame] | 1482 | if (info->legacy_unlock) { |
Anatolij Gustschin | 66863b0 | 2012-08-09 08:18:12 +0200 | [diff] [blame] | 1483 | int flag = disable_interrupts(); |
| 1484 | int lock_flag; |
| 1485 | |
| 1486 | flash_unlock_seq(info, 0); |
| 1487 | flash_write_cmd(info, 0, info->addr_unlock1, |
| 1488 | AMD_CMD_SET_PPB_ENTRY); |
| 1489 | lock_flag = flash_isset(info, sector, 0, 0x01); |
| 1490 | if (prot) { |
| 1491 | if (lock_flag) { |
| 1492 | flash_write_cmd(info, sector, 0, |
| 1493 | AMD_CMD_PPB_LOCK_BC1); |
| 1494 | flash_write_cmd(info, sector, 0, |
| 1495 | AMD_CMD_PPB_LOCK_BC2); |
| 1496 | } |
| 1497 | debug("sector %ld %slocked\n", sector, |
| 1498 | lock_flag ? "" : "already "); |
| 1499 | } else { |
| 1500 | if (!lock_flag) { |
| 1501 | debug("unlock %ld\n", sector); |
| 1502 | flash_write_cmd(info, 0, 0, |
| 1503 | AMD_CMD_PPB_UNLOCK_BC1); |
| 1504 | flash_write_cmd(info, 0, 0, |
| 1505 | AMD_CMD_PPB_UNLOCK_BC2); |
| 1506 | } |
| 1507 | debug("sector %ld %sunlocked\n", sector, |
| 1508 | !lock_flag ? "" : "already "); |
| 1509 | } |
| 1510 | if (flag) |
| 1511 | enable_interrupts(); |
| 1512 | |
| 1513 | if (flash_status_check(info, sector, |
| 1514 | info->erase_blk_tout, |
| 1515 | prot ? "protect" : "unprotect")) |
| 1516 | printf("status check error\n"); |
| 1517 | |
| 1518 | flash_write_cmd(info, 0, 0, |
| 1519 | AMD_CMD_SET_PPB_EXIT_BC1); |
| 1520 | flash_write_cmd(info, 0, 0, |
| 1521 | AMD_CMD_SET_PPB_EXIT_BC2); |
| 1522 | } |
Rafael Campos | bc9019e | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1523 | break; |
TsiChung Liew | 4e00acd | 2008-08-19 16:53:39 +0000 | [diff] [blame] | 1524 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1525 | case CFI_CMDSET_AMD_LEGACY: |
| 1526 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 1527 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT); |
| 1528 | if (prot) |
| 1529 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET); |
| 1530 | else |
| 1531 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR); |
| 1532 | #endif |
Rafael Campos | bc9019e | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1533 | }; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1534 | |
Stefan Roese | df4e813 | 2010-10-25 18:31:29 +0200 | [diff] [blame] | 1535 | /* |
| 1536 | * Flash needs to be in status register read mode for |
| 1537 | * flash_full_status_check() to work correctly |
| 1538 | */ |
| 1539 | flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1540 | if ((retcode = |
| 1541 | flash_full_status_check (info, sector, info->erase_blk_tout, |
| 1542 | prot ? "protect" : "unprotect")) == 0) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1543 | |
| 1544 | info->protect[sector] = prot; |
Stefan Roese | 2662b40 | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1545 | |
| 1546 | /* |
| 1547 | * On some of Intel's flash chips (marked via legacy_unlock) |
| 1548 | * unprotect unprotects all locking. |
| 1549 | */ |
| 1550 | if ((prot == 0) && (info->legacy_unlock)) { |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1551 | flash_sect_t i; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1552 | |
| 1553 | for (i = 0; i < info->sector_count; i++) { |
| 1554 | if (info->protect[i]) |
| 1555 | flash_real_protect (info, i, 1); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | } |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1559 | return retcode; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1562 | /*----------------------------------------------------------------------- |
| 1563 | * flash_read_user_serial - read the OneTimeProgramming cells |
| 1564 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1565 | void flash_read_user_serial (flash_info_t * info, void *buffer, int offset, |
| 1566 | int len) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1567 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1568 | uchar *src; |
| 1569 | uchar *dst; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1570 | |
| 1571 | dst = buffer; |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1572 | src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1573 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 1574 | memcpy (dst, src + offset, len); |
Wolfgang Denk | db421e6 | 2005-09-25 16:41:22 +0200 | [diff] [blame] | 1575 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
Aaron Williams | a90b957 | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1576 | udelay(1); |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1577 | flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1578 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1579 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1580 | /* |
| 1581 | * flash_read_factory_serial - read the device Id from the protection area |
| 1582 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1583 | void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset, |
| 1584 | int len) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1585 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1586 | uchar *src; |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 1587 | |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1588 | src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1589 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 1590 | memcpy (buffer, src + offset, len); |
Wolfgang Denk | db421e6 | 2005-09-25 16:41:22 +0200 | [diff] [blame] | 1591 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
Aaron Williams | a90b957 | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1592 | udelay(1); |
Haavard Skinnemoen | 12d30aa | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1593 | flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1596 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1597 | |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1598 | /*----------------------------------------------------------------------- |
| 1599 | * Reverse the order of the erase regions in the CFI QRY structure. |
| 1600 | * This is needed for chips that are either a) correctly detected as |
| 1601 | * top-boot, or b) buggy. |
| 1602 | */ |
| 1603 | static void cfi_reverse_geometry(struct cfi_qry *qry) |
| 1604 | { |
| 1605 | unsigned int i, j; |
| 1606 | u32 tmp; |
| 1607 | |
| 1608 | for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) { |
Andrew Gabbasov | aedadf1 | 2013-05-14 12:27:52 -0500 | [diff] [blame] | 1609 | tmp = get_unaligned(&(qry->erase_region_info[i])); |
| 1610 | put_unaligned(get_unaligned(&(qry->erase_region_info[j])), |
| 1611 | &(qry->erase_region_info[i])); |
| 1612 | put_unaligned(tmp, &(qry->erase_region_info[j])); |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1613 | } |
| 1614 | } |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1615 | |
| 1616 | /*----------------------------------------------------------------------- |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1617 | * read jedec ids from device and set corresponding fields in info struct |
| 1618 | * |
| 1619 | * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct |
| 1620 | * |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1621 | */ |
| 1622 | static void cmdset_intel_read_jedec_ids(flash_info_t *info) |
| 1623 | { |
| 1624 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
Aaron Williams | a90b957 | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1625 | udelay(1); |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1626 | flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID); |
| 1627 | udelay(1000); /* some flash are slow to respond */ |
| 1628 | info->manufacturer_id = flash_read_uchar (info, |
| 1629 | FLASH_OFFSET_MANUFACTURER_ID); |
Philippe De Muyter | d77c7ac | 2010-08-10 16:54:52 +0200 | [diff] [blame] | 1630 | info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ? |
| 1631 | flash_read_word (info, FLASH_OFFSET_DEVICE_ID) : |
| 1632 | flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID); |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1633 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1634 | } |
| 1635 | |
| 1636 | static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry) |
| 1637 | { |
| 1638 | info->cmd_reset = FLASH_CMD_RESET; |
| 1639 | |
| 1640 | cmdset_intel_read_jedec_ids(info); |
| 1641 | flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI); |
| 1642 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1643 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1644 | /* read legacy lock/unlock bit from intel flash */ |
| 1645 | if (info->ext_addr) { |
| 1646 | info->legacy_unlock = flash_read_uchar (info, |
| 1647 | info->ext_addr + 5) & 0x08; |
| 1648 | } |
| 1649 | #endif |
| 1650 | |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | static void cmdset_amd_read_jedec_ids(flash_info_t *info) |
| 1655 | { |
Niklaus Giger | 3a7b2c2 | 2009-07-22 17:13:24 +0200 | [diff] [blame] | 1656 | ushort bankId = 0; |
| 1657 | uchar manuId; |
| 1658 | |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1659 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
| 1660 | flash_unlock_seq(info, 0); |
| 1661 | flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID); |
| 1662 | udelay(1000); /* some flash are slow to respond */ |
Tor Krill | 90447ec | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 1663 | |
Niklaus Giger | 3a7b2c2 | 2009-07-22 17:13:24 +0200 | [diff] [blame] | 1664 | manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID); |
| 1665 | /* JEDEC JEP106Z specifies ID codes up to bank 7 */ |
| 1666 | while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) { |
| 1667 | bankId += 0x100; |
| 1668 | manuId = flash_read_uchar (info, |
| 1669 | bankId | FLASH_OFFSET_MANUFACTURER_ID); |
| 1670 | } |
| 1671 | info->manufacturer_id = manuId; |
Tor Krill | 90447ec | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 1672 | |
| 1673 | switch (info->chipwidth){ |
| 1674 | case FLASH_CFI_8BIT: |
| 1675 | info->device_id = flash_read_uchar (info, |
| 1676 | FLASH_OFFSET_DEVICE_ID); |
| 1677 | if (info->device_id == 0x7E) { |
| 1678 | /* AMD 3-byte (expanded) device ids */ |
| 1679 | info->device_id2 = flash_read_uchar (info, |
| 1680 | FLASH_OFFSET_DEVICE_ID2); |
| 1681 | info->device_id2 <<= 8; |
| 1682 | info->device_id2 |= flash_read_uchar (info, |
| 1683 | FLASH_OFFSET_DEVICE_ID3); |
| 1684 | } |
| 1685 | break; |
| 1686 | case FLASH_CFI_16BIT: |
| 1687 | info->device_id = flash_read_word (info, |
| 1688 | FLASH_OFFSET_DEVICE_ID); |
Heiko Schocher | 5b448ad | 2011-04-11 14:16:19 +0200 | [diff] [blame] | 1689 | if ((info->device_id & 0xff) == 0x7E) { |
| 1690 | /* AMD 3-byte (expanded) device ids */ |
| 1691 | info->device_id2 = flash_read_uchar (info, |
| 1692 | FLASH_OFFSET_DEVICE_ID2); |
| 1693 | info->device_id2 <<= 8; |
| 1694 | info->device_id2 |= flash_read_uchar (info, |
| 1695 | FLASH_OFFSET_DEVICE_ID3); |
| 1696 | } |
Tor Krill | 90447ec | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 1697 | break; |
| 1698 | default: |
| 1699 | break; |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1700 | } |
| 1701 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
Aaron Williams | a90b957 | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1702 | udelay(1); |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry) |
| 1706 | { |
| 1707 | info->cmd_reset = AMD_CMD_RESET; |
Angelo Dureghello | 07b2c5c | 2012-12-01 01:14:18 +0100 | [diff] [blame] | 1708 | info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR; |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1709 | |
| 1710 | cmdset_amd_read_jedec_ids(info); |
| 1711 | flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI); |
| 1712 | |
Anatolij Gustschin | 66863b0 | 2012-08-09 08:18:12 +0200 | [diff] [blame] | 1713 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Stefan Roese | ac6b911 | 2012-12-06 15:44:11 +0100 | [diff] [blame] | 1714 | if (info->ext_addr) { |
| 1715 | /* read sector protect/unprotect scheme (at 0x49) */ |
| 1716 | if (flash_read_uchar(info, info->ext_addr + 9) == 0x8) |
Anatolij Gustschin | 66863b0 | 2012-08-09 08:18:12 +0200 | [diff] [blame] | 1717 | info->legacy_unlock = 1; |
| 1718 | } |
| 1719 | #endif |
| 1720 | |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1721 | return 0; |
| 1722 | } |
| 1723 | |
| 1724 | #ifdef CONFIG_FLASH_CFI_LEGACY |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1725 | static void flash_read_jedec_ids (flash_info_t * info) |
| 1726 | { |
| 1727 | info->manufacturer_id = 0; |
| 1728 | info->device_id = 0; |
| 1729 | info->device_id2 = 0; |
| 1730 | |
| 1731 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1732 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1733 | case CFI_CMDSET_INTEL_STANDARD: |
| 1734 | case CFI_CMDSET_INTEL_EXTENDED: |
Michael Schwingen | 8225d1e | 2008-01-12 20:29:47 +0100 | [diff] [blame] | 1735 | cmdset_intel_read_jedec_ids(info); |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1736 | break; |
| 1737 | case CFI_CMDSET_AMD_STANDARD: |
| 1738 | case CFI_CMDSET_AMD_EXTENDED: |
Michael Schwingen | 8225d1e | 2008-01-12 20:29:47 +0100 | [diff] [blame] | 1739 | cmdset_amd_read_jedec_ids(info); |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1740 | break; |
| 1741 | default: |
| 1742 | break; |
| 1743 | } |
| 1744 | } |
| 1745 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1746 | /*----------------------------------------------------------------------- |
| 1747 | * Call board code to request info about non-CFI flash. |
| 1748 | * board_flash_get_legacy needs to fill in at least: |
| 1749 | * info->portwidth, info->chipwidth and info->interface for Jedec probing. |
| 1750 | */ |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1751 | static int flash_detect_legacy(phys_addr_t base, int banknum) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1752 | { |
| 1753 | flash_info_t *info = &flash_info[banknum]; |
| 1754 | |
| 1755 | if (board_flash_get_legacy(base, banknum, info)) { |
| 1756 | /* board code may have filled info completely. If not, we |
| 1757 | use JEDEC ID probing. */ |
| 1758 | if (!info->vendor) { |
| 1759 | int modes[] = { |
| 1760 | CFI_CMDSET_AMD_STANDARD, |
| 1761 | CFI_CMDSET_INTEL_STANDARD |
| 1762 | }; |
| 1763 | int i; |
| 1764 | |
Axel Lin | 31bf0f5 | 2013-06-23 00:56:46 +0800 | [diff] [blame] | 1765 | for (i = 0; i < ARRAY_SIZE(modes); i++) { |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1766 | info->vendor = modes[i]; |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1767 | info->start[0] = |
| 1768 | (ulong)map_physmem(base, |
Stefan Roese | e1fb6d0 | 2009-02-05 11:44:52 +0100 | [diff] [blame] | 1769 | info->portwidth, |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1770 | MAP_NOCACHE); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1771 | if (info->portwidth == FLASH_CFI_8BIT |
| 1772 | && info->interface == FLASH_CFI_X8X16) { |
| 1773 | info->addr_unlock1 = 0x2AAA; |
| 1774 | info->addr_unlock2 = 0x5555; |
| 1775 | } else { |
| 1776 | info->addr_unlock1 = 0x5555; |
| 1777 | info->addr_unlock2 = 0x2AAA; |
| 1778 | } |
| 1779 | flash_read_jedec_ids(info); |
| 1780 | debug("JEDEC PROBE: ID %x %x %x\n", |
| 1781 | info->manufacturer_id, |
| 1782 | info->device_id, |
| 1783 | info->device_id2); |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1784 | if (jedec_flash_match(info, info->start[0])) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1785 | break; |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1786 | else |
Stefan Roese | e1fb6d0 | 2009-02-05 11:44:52 +0100 | [diff] [blame] | 1787 | unmap_physmem((void *)info->start[0], |
Kuo-Jung Su | d8b57c0 | 2013-07-04 11:40:36 +0800 | [diff] [blame] | 1788 | info->portwidth); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | switch(info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1793 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1794 | case CFI_CMDSET_INTEL_STANDARD: |
| 1795 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1796 | info->cmd_reset = FLASH_CMD_RESET; |
| 1797 | break; |
| 1798 | case CFI_CMDSET_AMD_STANDARD: |
| 1799 | case CFI_CMDSET_AMD_EXTENDED: |
| 1800 | case CFI_CMDSET_AMD_LEGACY: |
| 1801 | info->cmd_reset = AMD_CMD_RESET; |
| 1802 | break; |
| 1803 | } |
| 1804 | info->flash_id = FLASH_MAN_CFI; |
| 1805 | return 1; |
| 1806 | } |
| 1807 | return 0; /* use CFI */ |
| 1808 | } |
| 1809 | #else |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1810 | static inline int flash_detect_legacy(phys_addr_t base, int banknum) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1811 | { |
| 1812 | return 0; /* use CFI */ |
| 1813 | } |
| 1814 | #endif |
| 1815 | |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1816 | /*----------------------------------------------------------------------- |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1817 | * detect if flash is compatible with the Common Flash Interface (CFI) |
| 1818 | * http://www.jedec.org/download/search/jesd68.pdf |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1819 | */ |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1820 | static void flash_read_cfi (flash_info_t *info, void *buf, |
| 1821 | unsigned int start, size_t len) |
| 1822 | { |
| 1823 | u8 *p = buf; |
| 1824 | unsigned int i; |
| 1825 | |
| 1826 | for (i = 0; i < len; i++) |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1827 | p[i] = flash_read_uchar(info, start + i); |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
Kim Phillips | 11dc401 | 2012-10-29 13:34:45 +0000 | [diff] [blame] | 1830 | static void __flash_cmd_reset(flash_info_t *info) |
Stefan Roese | fa36ae7 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 1831 | { |
| 1832 | /* |
| 1833 | * We do not yet know what kind of commandset to use, so we issue |
| 1834 | * the reset command in both Intel and AMD variants, in the hope |
| 1835 | * that AMD flash roms ignore the Intel command. |
| 1836 | */ |
| 1837 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
Aaron Williams | a90b957 | 2011-04-12 00:59:04 -0700 | [diff] [blame] | 1838 | udelay(1); |
Stefan Roese | fa36ae7 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 1839 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1840 | } |
| 1841 | void flash_cmd_reset(flash_info_t *info) |
| 1842 | __attribute__((weak,alias("__flash_cmd_reset"))); |
| 1843 | |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1844 | static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1845 | { |
Wolfgang Denk | 92eb729 | 2006-12-27 01:26:13 +0100 | [diff] [blame] | 1846 | int cfi_offset; |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1847 | |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1848 | /* Issue FLASH reset command */ |
| 1849 | flash_cmd_reset(info); |
| 1850 | |
Axel Lin | 31bf0f5 | 2013-06-23 00:56:46 +0800 | [diff] [blame] | 1851 | for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi); |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1852 | cfi_offset++) { |
| 1853 | flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], |
| 1854 | FLASH_CMD_CFI); |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1855 | if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q') |
| 1856 | && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') |
| 1857 | && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) { |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1858 | flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP, |
| 1859 | sizeof(struct cfi_qry)); |
| 1860 | info->interface = le16_to_cpu(qry->interface_desc); |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1861 | |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1862 | info->cfi_offset = flash_offset_cfi[cfi_offset]; |
| 1863 | debug ("device interface is %d\n", |
| 1864 | info->interface); |
| 1865 | debug ("found port %d chip %d ", |
| 1866 | info->portwidth, info->chipwidth); |
| 1867 | debug ("port %d bits chip %d bits\n", |
| 1868 | info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 1869 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 1870 | |
| 1871 | /* calculate command offsets as in the Linux driver */ |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1872 | info->addr_unlock1 = 0x555; |
| 1873 | info->addr_unlock2 = 0x2aa; |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1874 | |
| 1875 | /* |
| 1876 | * modify the unlock address if we are |
| 1877 | * in compatibility mode |
| 1878 | */ |
| 1879 | if ( /* x8/x16 in x8 mode */ |
| 1880 | ((info->chipwidth == FLASH_CFI_BY8) && |
| 1881 | (info->interface == FLASH_CFI_X8X16)) || |
| 1882 | /* x16/x32 in x16 mode */ |
| 1883 | ((info->chipwidth == FLASH_CFI_BY16) && |
| 1884 | (info->interface == FLASH_CFI_X16X32))) |
| 1885 | { |
| 1886 | info->addr_unlock1 = 0xaaa; |
| 1887 | info->addr_unlock2 = 0x555; |
| 1888 | } |
| 1889 | |
| 1890 | info->name = "CFI conformant"; |
| 1891 | return 1; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1898 | static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry) |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1899 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1900 | debug ("flash detect cfi\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1901 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1902 | for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1903 | info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) { |
| 1904 | for (info->chipwidth = FLASH_CFI_BY8; |
| 1905 | info->chipwidth <= info->portwidth; |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1906 | info->chipwidth <<= 1) |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1907 | if (__flash_detect_cfi(info, qry)) |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1908 | return 1; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1909 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1910 | debug ("not found\n"); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1911 | return 0; |
| 1912 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1913 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1914 | /* |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1915 | * Manufacturer-specific quirks. Add workarounds for geometry |
| 1916 | * reversal, etc. here. |
| 1917 | */ |
| 1918 | static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry) |
| 1919 | { |
| 1920 | /* check if flash geometry needs reversal */ |
| 1921 | if (qry->num_erase_regions > 1) { |
| 1922 | /* reverse geometry if top boot part */ |
| 1923 | if (info->cfi_version < 0x3131) { |
| 1924 | /* CFI < 1.1, try to guess from device id */ |
| 1925 | if ((info->device_id & 0x80) != 0) |
| 1926 | cfi_reverse_geometry(qry); |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 1927 | } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) { |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1928 | /* CFI >= 1.1, deduct from top/bottom flag */ |
| 1929 | /* note: ext_addr is valid since cfi_version > 0 */ |
| 1930 | cfi_reverse_geometry(qry); |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry) |
| 1936 | { |
| 1937 | int reverse_geometry = 0; |
| 1938 | |
| 1939 | /* Check the "top boot" bit in the PRI */ |
| 1940 | if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1)) |
| 1941 | reverse_geometry = 1; |
| 1942 | |
| 1943 | /* AT49BV6416(T) list the erase regions in the wrong order. |
| 1944 | * However, the device ID is identical with the non-broken |
Ulf Samuelsson | cb82a53 | 2009-03-27 23:26:43 +0100 | [diff] [blame] | 1945 | * AT49BV642D they differ in the high byte. |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1946 | */ |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1947 | if (info->device_id == 0xd6 || info->device_id == 0xd2) |
| 1948 | reverse_geometry = !reverse_geometry; |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1949 | |
| 1950 | if (reverse_geometry) |
| 1951 | cfi_reverse_geometry(qry); |
| 1952 | } |
| 1953 | |
Richard Retanubun | e8eac43 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1954 | static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry) |
| 1955 | { |
| 1956 | /* check if flash geometry needs reversal */ |
| 1957 | if (qry->num_erase_regions > 1) { |
| 1958 | /* reverse geometry if top boot part */ |
| 1959 | if (info->cfi_version < 0x3131) { |
Mike Frysinger | 6a011ce | 2011-04-10 16:06:29 -0400 | [diff] [blame] | 1960 | /* CFI < 1.1, guess by device id */ |
| 1961 | if (info->device_id == 0x22CA || /* M29W320DT */ |
| 1962 | info->device_id == 0x2256 || /* M29W320ET */ |
| 1963 | info->device_id == 0x22D7) { /* M29W800DT */ |
Richard Retanubun | e8eac43 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1964 | cfi_reverse_geometry(qry); |
| 1965 | } |
Mike Frysinger | 4c2105c | 2011-05-09 18:33:36 -0400 | [diff] [blame] | 1966 | } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) { |
| 1967 | /* CFI >= 1.1, deduct from top/bottom flag */ |
| 1968 | /* note: ext_addr is valid since cfi_version > 0 */ |
| 1969 | cfi_reverse_geometry(qry); |
Richard Retanubun | e8eac43 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1970 | } |
| 1971 | } |
| 1972 | } |
| 1973 | |
Angelo Dureghello | 07b2c5c | 2012-12-01 01:14:18 +0100 | [diff] [blame] | 1974 | static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry) |
| 1975 | { |
| 1976 | /* |
| 1977 | * SST, for many recent nor parallel flashes, says they are |
| 1978 | * CFI-conformant. This is not true, since qry struct. |
| 1979 | * reports a std. AMD command set (0x0002), while SST allows to |
| 1980 | * erase two different sector sizes for the same memory. |
| 1981 | * 64KB sector (SST call it block) needs 0x30 to be erased. |
| 1982 | * 4KB sector (SST call it sector) needs 0x50 to be erased. |
| 1983 | * Since CFI query detect the 4KB number of sectors, users expects |
| 1984 | * a sector granularity of 4KB, and it is here set. |
| 1985 | */ |
| 1986 | if (info->device_id == 0x5D23 || /* SST39VF3201B */ |
| 1987 | info->device_id == 0x5C23) { /* SST39VF3202B */ |
| 1988 | /* set sector granularity to 4KB */ |
| 1989 | info->cmd_erase_sector=0x50; |
| 1990 | } |
| 1991 | } |
| 1992 | |
Jagannadha Sutradharudu Teki | c502321 | 2013-03-01 16:54:26 +0530 | [diff] [blame] | 1993 | static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry) |
| 1994 | { |
| 1995 | /* |
| 1996 | * The M29EW devices seem to report the CFI information wrong |
| 1997 | * when it's in 8 bit mode. |
| 1998 | * There's an app note from Numonyx on this issue. |
| 1999 | * So adjust the buffer size for M29EW while operating in 8-bit mode |
| 2000 | */ |
| 2001 | if (((qry->max_buf_write_size) > 0x8) && |
| 2002 | (info->device_id == 0x7E) && |
| 2003 | (info->device_id2 == 0x2201 || |
| 2004 | info->device_id2 == 0x2301 || |
| 2005 | info->device_id2 == 0x2801 || |
| 2006 | info->device_id2 == 0x4801)) { |
| 2007 | debug("Adjusted buffer size on Numonyx flash" |
| 2008 | " M29EW family in 8 bit mode\n"); |
| 2009 | qry->max_buf_write_size = 0x8; |
| 2010 | } |
| 2011 | } |
| 2012 | |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 2013 | /* |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2014 | * The following code cannot be run from FLASH! |
| 2015 | * |
| 2016 | */ |
Anatolij Gustschin | 34bbb8f | 2010-11-28 02:13:33 +0100 | [diff] [blame] | 2017 | ulong flash_get_size (phys_addr_t base, int banknum) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2018 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2019 | flash_info_t *info = &flash_info[banknum]; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2020 | int i, j; |
| 2021 | flash_sect_t sect_cnt; |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 2022 | phys_addr_t sector; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2023 | unsigned long tmp; |
| 2024 | int size_ratio; |
| 2025 | uchar num_erase_regions; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2026 | int erase_region_size; |
| 2027 | int erase_region_count; |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2028 | struct cfi_qry qry; |
Anatolij Gustschin | 34bbb8f | 2010-11-28 02:13:33 +0100 | [diff] [blame] | 2029 | unsigned long max_size; |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2030 | |
Kumar Gala | f979690 | 2008-05-15 15:13:08 -0500 | [diff] [blame] | 2031 | memset(&qry, 0, sizeof(qry)); |
| 2032 | |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2033 | info->ext_addr = 0; |
| 2034 | info->cfi_version = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2035 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Stefan Roese | 2662b40 | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 2036 | info->legacy_unlock = 0; |
| 2037 | #endif |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2038 | |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 2039 | info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2040 | |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2041 | if (flash_detect_cfi (info, &qry)) { |
Andrew Gabbasov | aedadf1 | 2013-05-14 12:27:52 -0500 | [diff] [blame] | 2042 | info->vendor = le16_to_cpu(get_unaligned(&(qry.p_id))); |
| 2043 | info->ext_addr = le16_to_cpu(get_unaligned(&(qry.p_adr))); |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2044 | num_erase_regions = qry.num_erase_regions; |
| 2045 | |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2046 | if (info->ext_addr) { |
| 2047 | info->cfi_version = (ushort) flash_read_uchar (info, |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 2048 | info->ext_addr + 3) << 8; |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2049 | info->cfi_version |= (ushort) flash_read_uchar (info, |
Stefan Roese | e303be2 | 2013-04-12 19:04:54 +0200 | [diff] [blame] | 2050 | info->ext_addr + 4); |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2051 | } |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2052 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2053 | #ifdef DEBUG |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2054 | flash_printqry (&qry); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2055 | #endif |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2056 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2057 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 2058 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2059 | case CFI_CMDSET_INTEL_STANDARD: |
| 2060 | case CFI_CMDSET_INTEL_EXTENDED: |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2061 | cmdset_intel_init(info, &qry); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2062 | break; |
| 2063 | case CFI_CMDSET_AMD_STANDARD: |
| 2064 | case CFI_CMDSET_AMD_EXTENDED: |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2065 | cmdset_amd_init(info, &qry); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2066 | break; |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2067 | default: |
| 2068 | printf("CFI: Unknown command set 0x%x\n", |
| 2069 | info->vendor); |
| 2070 | /* |
| 2071 | * Unfortunately, this means we don't know how |
| 2072 | * to get the chip back to Read mode. Might |
| 2073 | * as well try an Intel-style reset... |
| 2074 | */ |
| 2075 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 2076 | return 0; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2077 | } |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 2078 | |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 2079 | /* Do manufacturer-specific fixups */ |
| 2080 | switch (info->manufacturer_id) { |
Mario Schuknecht | 2c9f48a | 2011-02-21 13:13:14 +0100 | [diff] [blame] | 2081 | case 0x0001: /* AMD */ |
| 2082 | case 0x0037: /* AMIC */ |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 2083 | flash_fixup_amd(info, &qry); |
| 2084 | break; |
| 2085 | case 0x001f: |
| 2086 | flash_fixup_atmel(info, &qry); |
| 2087 | break; |
Richard Retanubun | e8eac43 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 2088 | case 0x0020: |
| 2089 | flash_fixup_stm(info, &qry); |
| 2090 | break; |
Angelo Dureghello | 07b2c5c | 2012-12-01 01:14:18 +0100 | [diff] [blame] | 2091 | case 0x00bf: /* SST */ |
| 2092 | flash_fixup_sst(info, &qry); |
| 2093 | break; |
Jagannadha Sutradharudu Teki | c502321 | 2013-03-01 16:54:26 +0530 | [diff] [blame] | 2094 | case 0x0089: /* Numonyx */ |
| 2095 | flash_fixup_num(info, &qry); |
| 2096 | break; |
Haavard Skinnemoen | 467bcee | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 2097 | } |
| 2098 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2099 | debug ("manufacturer is %d\n", info->vendor); |
Stefan Roese | 260421a | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 2100 | debug ("manufacturer id is 0x%x\n", info->manufacturer_id); |
| 2101 | debug ("device id is 0x%x\n", info->device_id); |
| 2102 | debug ("device id2 is 0x%x\n", info->device_id2); |
| 2103 | debug ("cfi version is 0x%04x\n", info->cfi_version); |
| 2104 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2105 | size_ratio = info->portwidth / info->chipwidth; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2106 | /* if the chip is x8/x16 reduce the ratio by half */ |
| 2107 | if ((info->interface == FLASH_CFI_X8X16) |
| 2108 | && (info->chipwidth == FLASH_CFI_BY8)) { |
| 2109 | size_ratio >>= 1; |
| 2110 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2111 | debug ("size_ratio %d port %d bits chip %d bits\n", |
| 2112 | size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 2113 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Ilya Yanok | ec50a8e | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 2114 | info->size = 1 << qry.dev_size; |
| 2115 | /* multiply the size by the number of chips */ |
| 2116 | info->size *= size_ratio; |
Anatolij Gustschin | 34bbb8f | 2010-11-28 02:13:33 +0100 | [diff] [blame] | 2117 | max_size = cfi_flash_bank_size(banknum); |
Ilya Yanok | ec50a8e | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 2118 | if (max_size && (info->size > max_size)) { |
| 2119 | debug("[truncated from %ldMiB]", info->size >> 20); |
| 2120 | info->size = max_size; |
| 2121 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2122 | debug ("found %d erase regions\n", num_erase_regions); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2123 | sect_cnt = 0; |
| 2124 | sector = base; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2125 | for (i = 0; i < num_erase_regions; i++) { |
| 2126 | if (i > NUM_ERASE_REGIONS) { |
wdenk | 028ab6b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 2127 | printf ("%d erase regions found, only %d used\n", |
| 2128 | num_erase_regions, NUM_ERASE_REGIONS); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2129 | break; |
| 2130 | } |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2131 | |
Andrew Gabbasov | aedadf1 | 2013-05-14 12:27:52 -0500 | [diff] [blame] | 2132 | tmp = le32_to_cpu(get_unaligned( |
| 2133 | &(qry.erase_region_info[i]))); |
Haavard Skinnemoen | 0ddf06d | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 2134 | debug("erase region %u: 0x%08lx\n", i, tmp); |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2135 | |
| 2136 | erase_region_count = (tmp & 0xffff) + 1; |
| 2137 | tmp >>= 16; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2138 | erase_region_size = |
| 2139 | (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128; |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 2140 | debug ("erase_region_count = %d erase_region_size = %d\n", |
wdenk | 028ab6b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 2141 | erase_region_count, erase_region_size); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2142 | for (j = 0; j < erase_region_count; j++) { |
Ilya Yanok | ec50a8e | 2010-10-21 17:20:12 +0200 | [diff] [blame] | 2143 | if (sector - base >= info->size) |
| 2144 | break; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2145 | if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) { |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 2146 | printf("ERROR: too many flash sectors\n"); |
| 2147 | break; |
| 2148 | } |
Becky Bruce | 09ce992 | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 2149 | info->start[sect_cnt] = |
| 2150 | (ulong)map_physmem(sector, |
| 2151 | info->portwidth, |
| 2152 | MAP_NOCACHE); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2153 | sector += (erase_region_size * size_ratio); |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2154 | |
| 2155 | /* |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2156 | * Only read protection status from |
| 2157 | * supported devices (intel...) |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2158 | */ |
| 2159 | switch (info->vendor) { |
Vasiliy Leoenenko | 9c048b5 | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 2160 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2161 | case CFI_CMDSET_INTEL_EXTENDED: |
| 2162 | case CFI_CMDSET_INTEL_STANDARD: |
Stefan Roese | df4e813 | 2010-10-25 18:31:29 +0200 | [diff] [blame] | 2163 | /* |
| 2164 | * Set flash to read-id mode. Otherwise |
| 2165 | * reading protected status is not |
| 2166 | * guaranteed. |
| 2167 | */ |
| 2168 | flash_write_cmd(info, sect_cnt, 0, |
| 2169 | FLASH_CMD_READ_ID); |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2170 | info->protect[sect_cnt] = |
| 2171 | flash_isset (info, sect_cnt, |
| 2172 | FLASH_OFFSET_PROTECT, |
| 2173 | FLASH_STATUS_PROTECT); |
| 2174 | break; |
Stefan Roese | 03deff4 | 2012-12-06 15:44:10 +0100 | [diff] [blame] | 2175 | case CFI_CMDSET_AMD_EXTENDED: |
| 2176 | case CFI_CMDSET_AMD_STANDARD: |
Stefan Roese | ac6b911 | 2012-12-06 15:44:11 +0100 | [diff] [blame] | 2177 | if (!info->legacy_unlock) { |
Stefan Roese | 03deff4 | 2012-12-06 15:44:10 +0100 | [diff] [blame] | 2178 | /* default: not protected */ |
| 2179 | info->protect[sect_cnt] = 0; |
| 2180 | break; |
| 2181 | } |
| 2182 | |
| 2183 | /* Read protection (PPB) from sector */ |
| 2184 | flash_write_cmd(info, 0, 0, |
| 2185 | info->cmd_reset); |
| 2186 | flash_unlock_seq(info, 0); |
| 2187 | flash_write_cmd(info, 0, |
| 2188 | info->addr_unlock1, |
| 2189 | FLASH_CMD_READ_ID); |
| 2190 | info->protect[sect_cnt] = |
| 2191 | flash_isset( |
| 2192 | info, sect_cnt, |
| 2193 | FLASH_OFFSET_PROTECT, |
| 2194 | FLASH_STATUS_PROTECT); |
| 2195 | break; |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2196 | default: |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2197 | /* default: not protected */ |
| 2198 | info->protect[sect_cnt] = 0; |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2201 | sect_cnt++; |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | info->sector_count = sect_cnt; |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2206 | info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size); |
| 2207 | tmp = 1 << qry.block_erase_timeout_typ; |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2208 | info->erase_blk_tout = tmp * |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2209 | (1 << qry.block_erase_timeout_max); |
| 2210 | tmp = (1 << qry.buf_write_timeout_typ) * |
| 2211 | (1 << qry.buf_write_timeout_max); |
| 2212 | |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2213 | /* round up when converting to ms */ |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2214 | info->buffer_write_tout = (tmp + 999) / 1000; |
| 2215 | tmp = (1 << qry.word_write_timeout_typ) * |
| 2216 | (1 << qry.word_write_timeout_max); |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2217 | /* round up when converting to ms */ |
Haavard Skinnemoen | e23741f | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 2218 | info->write_tout = (tmp + 999) / 1000; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2219 | info->flash_id = FLASH_MAN_CFI; |
Haavard Skinnemoen | 7e5b9b4 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 2220 | if ((info->interface == FLASH_CFI_X8X16) && |
| 2221 | (info->chipwidth == FLASH_CFI_BY8)) { |
| 2222 | /* XXX - Need to test on x8/x16 in parallel. */ |
| 2223 | info->portwidth >>= 1; |
wdenk | 855a496 | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 2224 | } |
Mike Frysinger | 2215987 | 2008-10-02 01:55:38 -0400 | [diff] [blame] | 2225 | |
| 2226 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2229 | return (info->size); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Mike Frysinger | 4ffeab2 | 2010-12-22 09:41:13 -0500 | [diff] [blame] | 2232 | #ifdef CONFIG_FLASH_CFI_MTD |
Piotr Ziecik | 6ea808e | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 2233 | void flash_set_verbose(uint v) |
| 2234 | { |
| 2235 | flash_verbose = v; |
| 2236 | } |
Mike Frysinger | 4ffeab2 | 2010-12-22 09:41:13 -0500 | [diff] [blame] | 2237 | #endif |
Piotr Ziecik | 6ea808e | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 2238 | |
Stefan Roese | 6f726f9 | 2010-10-25 18:31:48 +0200 | [diff] [blame] | 2239 | static void cfi_flash_set_config_reg(u32 base, u16 val) |
| 2240 | { |
| 2241 | #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS |
| 2242 | /* |
| 2243 | * Only set this config register if really defined |
| 2244 | * to a valid value (0xffff is invalid) |
| 2245 | */ |
| 2246 | if (val == 0xffff) |
| 2247 | return; |
| 2248 | |
| 2249 | /* |
| 2250 | * Set configuration register. Data is "encrypted" in the 16 lower |
| 2251 | * address bits. |
| 2252 | */ |
| 2253 | flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1))); |
| 2254 | flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1))); |
| 2255 | |
| 2256 | /* |
| 2257 | * Finally issue reset-command to bring device back to |
| 2258 | * read-array mode |
| 2259 | */ |
| 2260 | flash_write16(FLASH_CMD_RESET, (void *)base); |
| 2261 | #endif |
| 2262 | } |
| 2263 | |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2264 | /*----------------------------------------------------------------------- |
| 2265 | */ |
Heiko Schocher | 6ee1416 | 2011-04-04 08:10:21 +0200 | [diff] [blame] | 2266 | |
| 2267 | void flash_protect_default(void) |
| 2268 | { |
Peter Tyser | 2c51983 | 2011-04-13 11:46:56 -0500 | [diff] [blame] | 2269 | #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST) |
| 2270 | int i; |
| 2271 | struct apl_s { |
| 2272 | ulong start; |
| 2273 | ulong size; |
| 2274 | } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST; |
| 2275 | #endif |
| 2276 | |
Heiko Schocher | 6ee1416 | 2011-04-04 08:10:21 +0200 | [diff] [blame] | 2277 | /* Monitor protection ON by default */ |
| 2278 | #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \ |
| 2279 | (!defined(CONFIG_MONITOR_IS_IN_RAM)) |
| 2280 | flash_protect(FLAG_PROTECT_SET, |
| 2281 | CONFIG_SYS_MONITOR_BASE, |
| 2282 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, |
| 2283 | flash_get_info(CONFIG_SYS_MONITOR_BASE)); |
| 2284 | #endif |
| 2285 | |
| 2286 | /* Environment protection ON by default */ |
| 2287 | #ifdef CONFIG_ENV_IS_IN_FLASH |
| 2288 | flash_protect(FLAG_PROTECT_SET, |
| 2289 | CONFIG_ENV_ADDR, |
| 2290 | CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1, |
| 2291 | flash_get_info(CONFIG_ENV_ADDR)); |
| 2292 | #endif |
| 2293 | |
| 2294 | /* Redundant environment protection ON by default */ |
| 2295 | #ifdef CONFIG_ENV_ADDR_REDUND |
| 2296 | flash_protect(FLAG_PROTECT_SET, |
| 2297 | CONFIG_ENV_ADDR_REDUND, |
| 2298 | CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1, |
| 2299 | flash_get_info(CONFIG_ENV_ADDR_REDUND)); |
| 2300 | #endif |
| 2301 | |
| 2302 | #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST) |
Axel Lin | 31bf0f5 | 2013-06-23 00:56:46 +0800 | [diff] [blame] | 2303 | for (i = 0; i < ARRAY_SIZE(apl); i++) { |
Marek Vasut | 31d3414 | 2011-10-21 14:17:05 +0000 | [diff] [blame] | 2304 | debug("autoprotecting from %08lx to %08lx\n", |
Heiko Schocher | 6ee1416 | 2011-04-04 08:10:21 +0200 | [diff] [blame] | 2305 | apl[i].start, apl[i].start + apl[i].size - 1); |
| 2306 | flash_protect(FLAG_PROTECT_SET, |
| 2307 | apl[i].start, |
| 2308 | apl[i].start + apl[i].size - 1, |
| 2309 | flash_get_info(apl[i].start)); |
| 2310 | } |
| 2311 | #endif |
| 2312 | } |
| 2313 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2314 | unsigned long flash_init (void) |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2315 | { |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2316 | unsigned long size = 0; |
| 2317 | int i; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2318 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2319 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Eric Schumann | 3a3baf3 | 2009-03-21 09:59:34 -0400 | [diff] [blame] | 2320 | /* read environment from EEPROM */ |
| 2321 | char s[64]; |
Wolfgang Denk | cdb7497 | 2010-07-24 21:55:43 +0200 | [diff] [blame] | 2322 | getenv_f("unlock", s, sizeof(s)); |
Michael Schwingen | 81b20cc | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 2323 | #endif |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2324 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2325 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2326 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2327 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2328 | |
Stefan Roese | 6f726f9 | 2010-10-25 18:31:48 +0200 | [diff] [blame] | 2329 | /* Optionally write flash configuration register */ |
| 2330 | cfi_flash_set_config_reg(cfi_flash_bank_addr(i), |
| 2331 | cfi_flash_config_reg(i)); |
| 2332 | |
Stefan Roese | b00e19c | 2010-08-30 10:11:51 +0200 | [diff] [blame] | 2333 | if (!flash_detect_legacy(cfi_flash_bank_addr(i), i)) |
Anatolij Gustschin | 34bbb8f | 2010-11-28 02:13:33 +0100 | [diff] [blame] | 2334 | flash_get_size(cfi_flash_bank_addr(i), i); |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2335 | size += flash_info[i].size; |
| 2336 | if (flash_info[i].flash_id == FLASH_UNKNOWN) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2337 | #ifndef CONFIG_SYS_FLASH_QUIET_TEST |
Peter Tyser | eddf52b | 2010-12-28 18:12:05 -0600 | [diff] [blame] | 2338 | printf ("## Unknown flash on Bank %d " |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2339 | "- Size = 0x%08lx = %ld MB\n", |
| 2340 | i+1, flash_info[i].size, |
John Schmoller | 0e3fa01 | 2010-09-29 13:49:05 -0500 | [diff] [blame] | 2341 | flash_info[i].size >> 20); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2342 | #endif /* CONFIG_SYS_FLASH_QUIET_TEST */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2343 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2344 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Jeroen Hofstee | c15df21 | 2014-06-17 22:47:31 +0200 | [diff] [blame] | 2345 | else if (strcmp(s, "yes") == 0) { |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2346 | /* |
| 2347 | * Only the U-Boot image and it's environment |
| 2348 | * is protected, all other sectors are |
| 2349 | * unprotected (unlocked) if flash hardware |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2350 | * protection is used (CONFIG_SYS_FLASH_PROTECTION) |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2351 | * and the environment variable "unlock" is |
| 2352 | * set to "yes". |
| 2353 | */ |
| 2354 | if (flash_info[i].legacy_unlock) { |
| 2355 | int k; |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2356 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2357 | /* |
| 2358 | * Disable legacy_unlock temporarily, |
| 2359 | * since flash_real_protect would |
| 2360 | * relock all other sectors again |
| 2361 | * otherwise. |
| 2362 | */ |
| 2363 | flash_info[i].legacy_unlock = 0; |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2364 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2365 | /* |
| 2366 | * Legacy unlocking (e.g. Intel J3) -> |
| 2367 | * unlock only one sector. This will |
| 2368 | * unlock all sectors. |
| 2369 | */ |
| 2370 | flash_real_protect (&flash_info[i], 0, 0); |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2371 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2372 | flash_info[i].legacy_unlock = 1; |
| 2373 | |
| 2374 | /* |
| 2375 | * Manually mark other sectors as |
| 2376 | * unlocked (unprotected) |
| 2377 | */ |
| 2378 | for (k = 1; k < flash_info[i].sector_count; k++) |
| 2379 | flash_info[i].protect[k] = 0; |
| 2380 | } else { |
| 2381 | /* |
| 2382 | * No legancy unlocking -> unlock all sectors |
| 2383 | */ |
| 2384 | flash_protect (FLAG_PROTECT_CLEAR, |
| 2385 | flash_info[i].start[0], |
| 2386 | flash_info[i].start[0] |
| 2387 | + flash_info[i].size - 1, |
| 2388 | &flash_info[i]); |
| 2389 | } |
Stefan Roese | 79b4cda | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2390 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2391 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2392 | } |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2393 | |
Heiko Schocher | 6ee1416 | 2011-04-04 08:10:21 +0200 | [diff] [blame] | 2394 | flash_protect_default(); |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 2395 | #ifdef CONFIG_FLASH_CFI_MTD |
| 2396 | cfi_mtd_init(); |
| 2397 | #endif |
| 2398 | |
Haavard Skinnemoen | be60a90 | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2399 | return (size); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2400 | } |