wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1 | /* |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2 | * (C) Copyright 2002-2004 |
wdenk | 2cefd15 | 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 | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 7 | * |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 8 | * Copyright (C) 2004 |
| 9 | * Ed Okerson |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 |
| 12 | * Tolunay Orkun <listmember@orkun.us> |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 13 | * |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation; either version 2 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 30 | * MA 02111-1307 USA |
| 31 | * |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | /* The DEBUG define must be before common to enable debugging */ |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 35 | /* #define DEBUG */ |
| 36 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 37 | #include <common.h> |
| 38 | #include <asm/processor.h> |
Haiying Wang | c123a38 | 2007-02-21 16:52:31 +0100 | [diff] [blame] | 39 | #include <asm/io.h> |
wdenk | aeba06f | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 40 | #include <asm/byteorder.h> |
wdenk | d0245fc | 2005-04-13 10:02:42 +0000 | [diff] [blame] | 41 | #include <environment.h> |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 42 | #include <mtd/cfi_flash.h> |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 43 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 44 | /* |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 45 | * This file implements a Common Flash Interface (CFI) driver for |
| 46 | * U-Boot. |
| 47 | * |
| 48 | * The width of the port and the width of the chips are determined at |
| 49 | * initialization. These widths are used to calculate the address for |
| 50 | * access CFI data structures. |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 51 | * |
| 52 | * References |
| 53 | * JEDEC Standard JESD68 - Common Flash Interface (CFI) |
| 54 | * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes |
| 55 | * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets |
| 56 | * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 57 | * AMD CFI Specification, Release 2.0 December 1, 2001 |
| 58 | * AMD/Spansion Application Note: Migration from Single-byte to Three-byte |
| 59 | * Device IDs, Publication Number 25538 Revision A, November 8, 2001 |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 60 | * |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 61 | * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between |
Heiko Schocher | 800db31 | 2007-01-19 18:05:26 +0100 | [diff] [blame] | 62 | * reading and writing ... (yes there is such a Hardware). |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 63 | */ |
| 64 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 65 | #ifndef CONFIG_SYS_FLASH_BANKS_LIST |
| 66 | #define CONFIG_SYS_FLASH_BANKS_LIST { CONFIG_SYS_FLASH_BASE } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 69 | static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT }; |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 70 | static uint flash_verbose = 1; |
Wolfgang Denk | afa0dd0 | 2006-12-27 01:26:13 +0100 | [diff] [blame] | 71 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 72 | /* use CONFIG_SYS_MAX_FLASH_BANKS_DETECT if defined */ |
| 73 | #ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT |
| 74 | # define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS_DETECT |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 75 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 76 | # define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS |
Marian Balakowicz | 513b4a1 | 2005-10-11 19:09:42 +0200 | [diff] [blame] | 77 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 78 | |
Wolfgang Denk | 9f5fb0f | 2008-08-08 16:39:54 +0200 | [diff] [blame] | 79 | flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */ |
| 80 | |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 81 | /* |
| 82 | * Check if chip width is defined. If not, start detecting with 8bit. |
| 83 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 84 | #ifndef CONFIG_SYS_FLASH_CFI_WIDTH |
| 85 | #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 86 | #endif |
| 87 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 88 | static void __flash_write8(u8 value, void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 89 | { |
| 90 | __raw_writeb(value, addr); |
| 91 | } |
| 92 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 93 | static void __flash_write16(u16 value, void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 94 | { |
| 95 | __raw_writew(value, addr); |
| 96 | } |
| 97 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 98 | static void __flash_write32(u32 value, void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 99 | { |
| 100 | __raw_writel(value, addr); |
| 101 | } |
| 102 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 103 | static void __flash_write64(u64 value, void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 104 | { |
| 105 | /* No architectures currently implement __raw_writeq() */ |
| 106 | *(volatile u64 *)addr = value; |
| 107 | } |
| 108 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 109 | static u8 __flash_read8(void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 110 | { |
| 111 | return __raw_readb(addr); |
| 112 | } |
| 113 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 114 | static u16 __flash_read16(void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 115 | { |
| 116 | return __raw_readw(addr); |
| 117 | } |
| 118 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 119 | static u32 __flash_read32(void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 120 | { |
| 121 | return __raw_readl(addr); |
| 122 | } |
| 123 | |
Daniel Hellstrom | cfd7138 | 2008-03-28 20:40:19 +0100 | [diff] [blame] | 124 | static u64 __flash_read64(void *addr) |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 125 | { |
| 126 | /* No architectures currently implement __raw_readq() */ |
| 127 | return *(volatile u64 *)addr; |
| 128 | } |
| 129 | |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 130 | #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS |
| 131 | void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8"))); |
| 132 | void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16"))); |
| 133 | void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32"))); |
| 134 | void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64"))); |
| 135 | u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8"))); |
| 136 | u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16"))); |
| 137 | u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32"))); |
Daniel Hellstrom | cfd7138 | 2008-03-28 20:40:19 +0100 | [diff] [blame] | 138 | u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64"))); |
Stefan Roese | 38ae982 | 2008-11-17 14:45:22 +0100 | [diff] [blame] | 139 | #else |
| 140 | #define flash_write8 __flash_write8 |
| 141 | #define flash_write16 __flash_write16 |
| 142 | #define flash_write32 __flash_write32 |
| 143 | #define flash_write64 __flash_write64 |
| 144 | #define flash_read8 __flash_read8 |
| 145 | #define flash_read16 __flash_read16 |
| 146 | #define flash_read32 __flash_read32 |
| 147 | #define flash_read64 __flash_read64 |
| 148 | #endif |
Daniel Hellstrom | cfd7138 | 2008-03-28 20:40:19 +0100 | [diff] [blame] | 149 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 150 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 151 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 152 | #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) |
Heiko Schocher | 4c0f005 | 2009-02-10 09:53:29 +0100 | [diff] [blame] | 153 | flash_info_t *flash_get_info(ulong base) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 154 | { |
| 155 | int i; |
| 156 | flash_info_t * info = 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 157 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 158 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 159 | info = & flash_info[i]; |
| 160 | if (info->size && info->start[0] <= base && |
| 161 | base <= info->start[0] + info->size - 1) |
| 162 | break; |
| 163 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 164 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 165 | return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 166 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 169 | unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect) |
| 170 | { |
| 171 | if (sect != (info->sector_count - 1)) |
| 172 | return info->start[sect + 1] - info->start[sect]; |
| 173 | else |
| 174 | return info->start[0] + info->size - info->start[sect]; |
| 175 | } |
| 176 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 177 | /*----------------------------------------------------------------------- |
| 178 | * create an address based on the offset and the port width |
| 179 | */ |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 180 | static inline void * |
| 181 | flash_map (flash_info_t * info, flash_sect_t sect, uint offset) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 182 | { |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 183 | unsigned int byte_offset = offset * info->portwidth; |
| 184 | |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 185 | return (void *)(info->start[sect] + byte_offset); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static inline void flash_unmap(flash_info_t *info, flash_sect_t sect, |
| 189 | unsigned int offset, void *addr) |
| 190 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 193 | /*----------------------------------------------------------------------- |
| 194 | * make a proper sized command based on the port and chip widths |
| 195 | */ |
Sebastian Siewior | 7746ed8 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 196 | static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 197 | { |
| 198 | int i; |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 199 | int cword_offset; |
| 200 | int cp_offset; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 201 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Sebastian Siewior | d528cd6 | 2008-07-16 20:04:49 +0200 | [diff] [blame] | 202 | u32 cmd_le = cpu_to_le32(cmd); |
| 203 | #endif |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 204 | uchar val; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 205 | uchar *cp = (uchar *) cmdbuf; |
| 206 | |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 207 | for (i = info->portwidth; i > 0; i--){ |
| 208 | cword_offset = (info->portwidth-i)%info->chipwidth; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 209 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 210 | cp_offset = info->portwidth - i; |
Sebastian Siewior | d528cd6 | 2008-07-16 20:04:49 +0200 | [diff] [blame] | 211 | val = *((uchar*)&cmd_le + cword_offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 212 | #else |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 213 | cp_offset = i - 1; |
Sebastian Siewior | 7746ed8 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 214 | val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 215 | #endif |
Sebastian Siewior | 7746ed8 | 2008-07-15 13:35:23 +0200 | [diff] [blame] | 216 | cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val; |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 217 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 218 | } |
| 219 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 220 | #ifdef DEBUG |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 221 | /*----------------------------------------------------------------------- |
| 222 | * Debug support |
| 223 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 224 | static void print_longlong (char *str, unsigned long long data) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 225 | { |
| 226 | int i; |
| 227 | char *cp; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 228 | |
Wolfgang Denk | 49f4f6a | 2009-02-04 09:42:20 +0100 | [diff] [blame] | 229 | cp = (char *) &data; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 230 | for (i = 0; i < 8; i++) |
| 231 | sprintf (&str[i * 2], "%2.2x", *cp++); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 232 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 233 | |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 234 | static void flash_printqry (struct cfi_qry *qry) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 235 | { |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 236 | u8 *p = (u8 *)qry; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 237 | int x, y; |
| 238 | |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 239 | for (x = 0; x < sizeof(struct cfi_qry); x += 16) { |
| 240 | debug("%02x : ", x); |
| 241 | for (y = 0; y < 16; y++) |
| 242 | debug("%2.2x ", p[x + y]); |
| 243 | debug(" "); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 244 | for (y = 0; y < 16; y++) { |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 245 | unsigned char c = p[x + y]; |
| 246 | if (c >= 0x20 && c <= 0x7e) |
| 247 | debug("%c", c); |
| 248 | else |
| 249 | debug("."); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 250 | } |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 251 | debug("\n"); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 254 | #endif |
| 255 | |
| 256 | |
| 257 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 258 | * read a character at a port width address |
| 259 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 260 | static inline uchar flash_read_uchar (flash_info_t * info, uint offset) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 261 | { |
| 262 | uchar *cp; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 263 | uchar retval; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 264 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 265 | cp = flash_map (info, 0, offset); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 266 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 267 | retval = flash_read8(cp); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 268 | #else |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 269 | retval = flash_read8(cp + info->portwidth - 1); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 270 | #endif |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 271 | flash_unmap (info, 0, offset, cp); |
| 272 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /*----------------------------------------------------------------------- |
Tor Krill | 7f2a305 | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 276 | * read a word at a port width address, assume 16bit bus |
| 277 | */ |
| 278 | static inline ushort flash_read_word (flash_info_t * info, uint offset) |
| 279 | { |
| 280 | ushort *addr, retval; |
| 281 | |
| 282 | addr = flash_map (info, 0, offset); |
| 283 | retval = flash_read16 (addr); |
| 284 | flash_unmap (info, 0, offset, addr); |
| 285 | return retval; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /*----------------------------------------------------------------------- |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 290 | * read a long word by picking the least significant byte of each maximum |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 291 | * port size word. Swap for ppc format. |
| 292 | */ |
Haavard Skinnemoen | 670a323 | 2007-12-13 12:56:29 +0100 | [diff] [blame] | 293 | static ulong flash_read_long (flash_info_t * info, flash_sect_t sect, |
| 294 | uint offset) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 295 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 296 | uchar *addr; |
| 297 | ulong retval; |
| 298 | |
| 299 | #ifdef DEBUG |
| 300 | int x; |
| 301 | #endif |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 302 | addr = flash_map (info, sect, offset); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 303 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 304 | #ifdef DEBUG |
| 305 | debug ("long addr is at %p info->portwidth = %d\n", addr, |
| 306 | info->portwidth); |
| 307 | for (x = 0; x < 4 * info->portwidth; x++) { |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 308 | debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x)); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 309 | } |
| 310 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 311 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 312 | retval = ((flash_read8(addr) << 16) | |
| 313 | (flash_read8(addr + info->portwidth) << 24) | |
| 314 | (flash_read8(addr + 2 * info->portwidth)) | |
| 315 | (flash_read8(addr + 3 * info->portwidth) << 8)); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 316 | #else |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 317 | retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) | |
| 318 | (flash_read8(addr + info->portwidth - 1) << 16) | |
| 319 | (flash_read8(addr + 4 * info->portwidth - 1) << 8) | |
| 320 | (flash_read8(addr + 3 * info->portwidth - 1))); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 321 | #endif |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 322 | flash_unmap(info, sect, offset, addr); |
| 323 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 324 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 327 | /* |
| 328 | * Write a proper sized command to the correct address |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 329 | */ |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 330 | void flash_write_cmd (flash_info_t * info, flash_sect_t sect, |
| 331 | uint offset, u32 cmd) |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 332 | { |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 333 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 334 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 335 | cfiword_t cword; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 336 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 337 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 338 | flash_make_cmd (info, cmd, &cword); |
| 339 | switch (info->portwidth) { |
| 340 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 341 | debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 342 | cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 343 | flash_write8(cword.c, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 344 | break; |
| 345 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 346 | debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 347 | cmd, cword.w, |
| 348 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 349 | flash_write16(cword.w, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 350 | break; |
| 351 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 352 | debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 353 | cmd, cword.l, |
| 354 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 355 | flash_write32(cword.l, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 356 | break; |
| 357 | case FLASH_CFI_64BIT: |
| 358 | #ifdef DEBUG |
| 359 | { |
| 360 | char str[20]; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 361 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 362 | print_longlong (str, cword.ll); |
| 363 | |
| 364 | debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n", |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 365 | addr, cmd, str, |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 366 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 367 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 368 | #endif |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 369 | flash_write64(cword.ll, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 370 | break; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 371 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 372 | |
| 373 | /* Ensure all the instructions are fully finished */ |
| 374 | sync(); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 375 | |
| 376 | flash_unmap(info, sect, offset, addr); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 377 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 378 | |
| 379 | static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect) |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 380 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 381 | flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START); |
| 382 | flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 383 | } |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 384 | |
| 385 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 386 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 387 | static int flash_isequal (flash_info_t * info, flash_sect_t sect, |
| 388 | uint offset, uchar cmd) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 389 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 390 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 391 | cfiword_t cword; |
| 392 | int retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 393 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 394 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 395 | flash_make_cmd (info, cmd, &cword); |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 396 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 397 | debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 398 | switch (info->portwidth) { |
| 399 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 400 | debug ("is= %x %x\n", flash_read8(addr), cword.c); |
| 401 | retval = (flash_read8(addr) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 402 | break; |
| 403 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 404 | debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w); |
| 405 | retval = (flash_read16(addr) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 406 | break; |
| 407 | case FLASH_CFI_32BIT: |
Andrew Klossner | 7ddfafc | 2008-08-21 07:12:26 -0700 | [diff] [blame] | 408 | debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l); |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 409 | retval = (flash_read32(addr) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 410 | break; |
| 411 | case FLASH_CFI_64BIT: |
| 412 | #ifdef DEBUG |
| 413 | { |
| 414 | char str1[20]; |
| 415 | char str2[20]; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 416 | |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 417 | print_longlong (str1, flash_read64(addr)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 418 | print_longlong (str2, cword.ll); |
| 419 | debug ("is= %s %s\n", str1, str2); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 420 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 421 | #endif |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 422 | retval = (flash_read64(addr) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 423 | break; |
| 424 | default: |
| 425 | retval = 0; |
| 426 | break; |
| 427 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 428 | flash_unmap(info, sect, offset, addr); |
| 429 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 430 | return retval; |
| 431 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 432 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 433 | /*----------------------------------------------------------------------- |
| 434 | */ |
| 435 | static int flash_isset (flash_info_t * info, flash_sect_t sect, |
| 436 | uint offset, uchar cmd) |
| 437 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 438 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 439 | cfiword_t cword; |
| 440 | int retval; |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 441 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 442 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 443 | flash_make_cmd (info, cmd, &cword); |
| 444 | switch (info->portwidth) { |
| 445 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 446 | retval = ((flash_read8(addr) & cword.c) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 447 | break; |
| 448 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 449 | retval = ((flash_read16(addr) & cword.w) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 450 | break; |
| 451 | case FLASH_CFI_32BIT: |
Stefan Roese | d4e37c0 | 2008-01-02 14:05:37 +0100 | [diff] [blame] | 452 | retval = ((flash_read32(addr) & cword.l) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 453 | break; |
| 454 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 455 | retval = ((flash_read64(addr) & cword.ll) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 456 | break; |
| 457 | default: |
| 458 | retval = 0; |
| 459 | break; |
| 460 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 461 | flash_unmap(info, sect, offset, addr); |
| 462 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 463 | return retval; |
| 464 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 465 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 466 | /*----------------------------------------------------------------------- |
| 467 | */ |
| 468 | static int flash_toggle (flash_info_t * info, flash_sect_t sect, |
| 469 | uint offset, uchar cmd) |
| 470 | { |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 471 | void *addr; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 472 | cfiword_t cword; |
| 473 | int retval; |
wdenk | e85b7a5 | 2004-10-10 22:16:06 +0000 | [diff] [blame] | 474 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 475 | addr = flash_map (info, sect, offset); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 476 | flash_make_cmd (info, cmd, &cword); |
| 477 | switch (info->portwidth) { |
| 478 | case FLASH_CFI_8BIT: |
Stefan Roese | cff2b49 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 479 | retval = flash_read8(addr) != flash_read8(addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 480 | break; |
| 481 | case FLASH_CFI_16BIT: |
Stefan Roese | cff2b49 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 482 | retval = flash_read16(addr) != flash_read16(addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 483 | break; |
| 484 | case FLASH_CFI_32BIT: |
Stefan Roese | cff2b49 | 2008-06-16 10:40:02 +0200 | [diff] [blame] | 485 | retval = flash_read32(addr) != flash_read32(addr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 486 | break; |
| 487 | case FLASH_CFI_64BIT: |
Wolfgang Denk | 600e183 | 2008-10-31 01:12:28 +0100 | [diff] [blame] | 488 | retval = ( (flash_read32( addr ) != flash_read32( addr )) || |
| 489 | (flash_read32(addr+4) != flash_read32(addr+4)) ); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 490 | break; |
| 491 | default: |
| 492 | retval = 0; |
| 493 | break; |
| 494 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 495 | flash_unmap(info, sect, offset, addr); |
| 496 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 497 | return retval; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 500 | /* |
| 501 | * flash_is_busy - check to see if the flash is busy |
| 502 | * |
| 503 | * This routine checks the status of the chip and returns true if the |
| 504 | * chip is busy. |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 505 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 506 | static int flash_is_busy (flash_info_t * info, flash_sect_t sect) |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 507 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 508 | int retval; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 509 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 510 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 511 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 512 | case CFI_CMDSET_INTEL_STANDARD: |
| 513 | case CFI_CMDSET_INTEL_EXTENDED: |
| 514 | retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE); |
| 515 | break; |
| 516 | case CFI_CMDSET_AMD_STANDARD: |
| 517 | case CFI_CMDSET_AMD_EXTENDED: |
| 518 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 519 | case CFI_CMDSET_AMD_LEGACY: |
| 520 | #endif |
| 521 | retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE); |
| 522 | break; |
| 523 | default: |
| 524 | retval = 0; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 525 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 526 | debug ("flash_is_busy: %d\n", retval); |
| 527 | return retval; |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 531 | * wait for XSR.7 to be set. Time out with an error if it does not. |
| 532 | * This routine does not set the flash to read-array mode. |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 533 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 534 | static int flash_status_check (flash_info_t * info, flash_sect_t sector, |
| 535 | ulong tout, char *prompt) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 536 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 537 | ulong start; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 538 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 539 | #if CONFIG_SYS_HZ != 1000 |
Renato Andreola | ac6693d | 2010-03-24 23:00:47 +0800 | [diff] [blame] | 540 | if ((ulong)CONFIG_SYS_HZ > 100000) |
| 541 | tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */ |
| 542 | else |
| 543 | tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 544 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 545 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 546 | /* Wait for command completion */ |
Thomas Chou | 4b7e668 | 2010-04-01 11:15:05 +0800 | [diff] [blame^] | 547 | reset_timer(); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 548 | start = get_timer (0); |
| 549 | while (flash_is_busy (info, sector)) { |
| 550 | if (get_timer (start) > tout) { |
| 551 | printf ("Flash %s timeout at address %lx data %lx\n", |
| 552 | prompt, info->start[sector], |
| 553 | flash_read_long (info, sector, 0)); |
| 554 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
| 555 | return ERR_TIMOUT; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 556 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 557 | udelay (1); /* also triggers watchdog */ |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 558 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 559 | return ERR_OK; |
| 560 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 561 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 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; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 572 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 573 | retcode = flash_status_check (info, sector, tout, prompt); |
| 574 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 575 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 576 | case CFI_CMDSET_INTEL_EXTENDED: |
| 577 | case CFI_CMDSET_INTEL_STANDARD: |
Ed Swarthout | 2da1410 | 2008-10-09 01:26:36 -0500 | [diff] [blame] | 578 | if ((retcode != ERR_OK) |
Haavard Skinnemoen | 6dd763c | 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"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 593 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 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"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 600 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 601 | flash_write_cmd (info, sector, 0, info->cmd_reset); |
| 602 | break; |
| 603 | default: |
| 604 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 605 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 606 | return retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 609 | static int use_flash_status_poll(flash_info_t *info) |
| 610 | { |
| 611 | #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL |
| 612 | if (info->vendor == CFI_CMDSET_AMD_EXTENDED || |
| 613 | info->vendor == CFI_CMDSET_AMD_STANDARD) |
| 614 | return 1; |
| 615 | #endif |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int flash_status_poll(flash_info_t *info, void *src, void *dst, |
| 620 | ulong tout, char *prompt) |
| 621 | { |
| 622 | #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL |
| 623 | ulong start; |
| 624 | int ready; |
| 625 | |
| 626 | #if CONFIG_SYS_HZ != 1000 |
| 627 | if ((ulong)CONFIG_SYS_HZ > 100000) |
| 628 | tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */ |
| 629 | else |
| 630 | tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000); |
| 631 | #endif |
| 632 | |
| 633 | /* Wait for command completion */ |
Thomas Chou | 4b7e668 | 2010-04-01 11:15:05 +0800 | [diff] [blame^] | 634 | reset_timer(); |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 635 | start = get_timer(0); |
| 636 | while (1) { |
| 637 | switch (info->portwidth) { |
| 638 | case FLASH_CFI_8BIT: |
| 639 | ready = flash_read8(dst) == flash_read8(src); |
| 640 | break; |
| 641 | case FLASH_CFI_16BIT: |
| 642 | ready = flash_read16(dst) == flash_read16(src); |
| 643 | break; |
| 644 | case FLASH_CFI_32BIT: |
| 645 | ready = flash_read32(dst) == flash_read32(src); |
| 646 | break; |
| 647 | case FLASH_CFI_64BIT: |
| 648 | ready = flash_read64(dst) == flash_read64(src); |
| 649 | break; |
| 650 | default: |
| 651 | ready = 0; |
| 652 | break; |
| 653 | } |
| 654 | if (ready) |
| 655 | break; |
| 656 | if (get_timer(start) > tout) { |
| 657 | printf("Flash %s timeout at address %lx data %lx\n", |
| 658 | prompt, (ulong)dst, (ulong)flash_read8(dst)); |
| 659 | return ERR_TIMOUT; |
| 660 | } |
| 661 | udelay(1); /* also triggers watchdog */ |
| 662 | } |
| 663 | #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */ |
| 664 | return ERR_OK; |
| 665 | } |
| 666 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 667 | /*----------------------------------------------------------------------- |
| 668 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 669 | static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 670 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 671 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 672 | unsigned short w; |
| 673 | unsigned int l; |
| 674 | unsigned long long ll; |
| 675 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 676 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 677 | switch (info->portwidth) { |
| 678 | case FLASH_CFI_8BIT: |
| 679 | cword->c = c; |
| 680 | break; |
| 681 | case FLASH_CFI_16BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 682 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 683 | w = c; |
| 684 | w <<= 8; |
| 685 | cword->w = (cword->w >> 8) | w; |
| 686 | #else |
| 687 | cword->w = (cword->w << 8) | c; |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 688 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 689 | break; |
| 690 | case FLASH_CFI_32BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 691 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 692 | l = c; |
| 693 | l <<= 24; |
| 694 | cword->l = (cword->l >> 8) | l; |
| 695 | #else |
| 696 | cword->l = (cword->l << 8) | c; |
| 697 | #endif |
| 698 | break; |
| 699 | case FLASH_CFI_64BIT: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 700 | #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 701 | ll = c; |
| 702 | ll <<= 56; |
| 703 | cword->ll = (cword->ll >> 8) | ll; |
| 704 | #else |
| 705 | cword->ll = (cword->ll << 8) | c; |
| 706 | #endif |
| 707 | break; |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 708 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 709 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 710 | |
Jens Gehrlein | 8ee4add | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 711 | /* |
| 712 | * Loop through the sector table starting from the previously found sector. |
| 713 | * Searches forwards or backwards, dependent on the passed address. |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 714 | */ |
| 715 | static flash_sect_t find_sector (flash_info_t * info, ulong addr) |
| 716 | { |
Jens Gehrlein | 8ee4add | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 717 | static flash_sect_t saved_sector = 0; /* previously found sector */ |
| 718 | flash_sect_t sector = saved_sector; |
| 719 | |
| 720 | while ((info->start[sector] < addr) |
| 721 | && (sector < info->sector_count - 1)) |
| 722 | sector++; |
| 723 | while ((info->start[sector] > addr) && (sector > 0)) |
| 724 | /* |
| 725 | * also decrements the sector in case of an overshot |
| 726 | * in the first loop |
| 727 | */ |
| 728 | sector--; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 729 | |
Jens Gehrlein | 8ee4add | 2008-12-16 17:25:55 +0100 | [diff] [blame] | 730 | saved_sector = sector; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 731 | return sector; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 735 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 736 | static int flash_write_cfiword (flash_info_t * info, ulong dest, |
| 737 | cfiword_t cword) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 738 | { |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 739 | void *dstaddr = (void *)dest; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 740 | int flag; |
Jens Gehrlein | 1e814cf | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 741 | flash_sect_t sect = 0; |
| 742 | char sect_found = 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 743 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 744 | /* Check if Flash is (sufficiently) erased */ |
| 745 | switch (info->portwidth) { |
| 746 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 747 | flag = ((flash_read8(dstaddr) & cword.c) == cword.c); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 748 | break; |
| 749 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 750 | flag = ((flash_read16(dstaddr) & cword.w) == cword.w); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 751 | break; |
| 752 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 753 | flag = ((flash_read32(dstaddr) & cword.l) == cword.l); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 754 | break; |
| 755 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 756 | flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 757 | break; |
| 758 | default: |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 759 | flag = 0; |
| 760 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 761 | } |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 762 | if (!flag) |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 763 | return ERR_NOT_ERASED; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 764 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 765 | /* Disable interrupts which might cause a timeout here */ |
| 766 | flag = disable_interrupts (); |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 767 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 768 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 769 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 770 | case CFI_CMDSET_INTEL_EXTENDED: |
| 771 | case CFI_CMDSET_INTEL_STANDARD: |
| 772 | flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS); |
| 773 | flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE); |
| 774 | break; |
| 775 | case CFI_CMDSET_AMD_EXTENDED: |
| 776 | case CFI_CMDSET_AMD_STANDARD: |
Ed Swarthout | 2da1410 | 2008-10-09 01:26:36 -0500 | [diff] [blame] | 777 | sect = find_sector(info, dest); |
| 778 | flash_unlock_seq (info, sect); |
| 779 | flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE); |
Jens Gehrlein | 1e814cf | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 780 | sect_found = 1; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 781 | break; |
Po-Yu Chuang | 3f48340 | 2009-07-10 18:03:57 +0800 | [diff] [blame] | 782 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 783 | case CFI_CMDSET_AMD_LEGACY: |
| 784 | sect = find_sector(info, dest); |
| 785 | flash_unlock_seq (info, 0); |
| 786 | flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE); |
| 787 | sect_found = 1; |
| 788 | break; |
| 789 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 792 | switch (info->portwidth) { |
| 793 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 794 | flash_write8(cword.c, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 795 | break; |
| 796 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 797 | flash_write16(cword.w, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 798 | break; |
| 799 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 800 | flash_write32(cword.l, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 801 | break; |
| 802 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 803 | flash_write64(cword.ll, dstaddr); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 804 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 807 | /* re-enable interrupts if necessary */ |
| 808 | if (flag) |
| 809 | enable_interrupts (); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 810 | |
Jens Gehrlein | 1e814cf | 2008-12-16 17:25:54 +0100 | [diff] [blame] | 811 | if (!sect_found) |
| 812 | sect = find_sector (info, dest); |
| 813 | |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 814 | if (use_flash_status_poll(info)) |
| 815 | return flash_status_poll(info, &cword, dstaddr, |
| 816 | info->write_tout, "write"); |
| 817 | else |
| 818 | return flash_full_status_check(info, sect, |
| 819 | info->write_tout, "write"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 822 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 823 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 824 | static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, |
| 825 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 826 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 827 | flash_sect_t sector; |
| 828 | int cnt; |
| 829 | int retcode; |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 830 | void *src = cp; |
Stefan Roese | 42c6ace | 2009-02-05 11:25:57 +0100 | [diff] [blame] | 831 | void *dst = (void *)dest; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 832 | void *dst2 = dst; |
| 833 | int flag = 0; |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 834 | uint offset = 0; |
| 835 | unsigned int shift; |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 836 | uchar write_cmd; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 837 | |
| 838 | switch (info->portwidth) { |
| 839 | case FLASH_CFI_8BIT: |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 840 | shift = 0; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 841 | break; |
| 842 | case FLASH_CFI_16BIT: |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 843 | shift = 1; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 844 | break; |
| 845 | case FLASH_CFI_32BIT: |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 846 | shift = 2; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 847 | break; |
| 848 | case FLASH_CFI_64BIT: |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 849 | shift = 3; |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 850 | break; |
| 851 | default: |
| 852 | retcode = ERR_INVAL; |
| 853 | goto out_unmap; |
| 854 | } |
| 855 | |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 856 | cnt = len >> shift; |
| 857 | |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 858 | while ((cnt-- > 0) && (flag == 0)) { |
| 859 | switch (info->portwidth) { |
| 860 | case FLASH_CFI_8BIT: |
| 861 | flag = ((flash_read8(dst2) & flash_read8(src)) == |
| 862 | flash_read8(src)); |
| 863 | src += 1, dst2 += 1; |
| 864 | break; |
| 865 | case FLASH_CFI_16BIT: |
| 866 | flag = ((flash_read16(dst2) & flash_read16(src)) == |
| 867 | flash_read16(src)); |
| 868 | src += 2, dst2 += 2; |
| 869 | break; |
| 870 | case FLASH_CFI_32BIT: |
| 871 | flag = ((flash_read32(dst2) & flash_read32(src)) == |
| 872 | flash_read32(src)); |
| 873 | src += 4, dst2 += 4; |
| 874 | break; |
| 875 | case FLASH_CFI_64BIT: |
| 876 | flag = ((flash_read64(dst2) & flash_read64(src)) == |
| 877 | flash_read64(src)); |
| 878 | src += 8, dst2 += 8; |
| 879 | break; |
| 880 | } |
| 881 | } |
| 882 | if (!flag) { |
| 883 | retcode = ERR_NOT_ERASED; |
| 884 | goto out_unmap; |
| 885 | } |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 886 | |
Stefan Roese | 707c146 | 2007-12-27 07:50:54 +0100 | [diff] [blame] | 887 | src = cp; |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 888 | sector = find_sector (info, dest); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 889 | |
| 890 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 891 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 892 | case CFI_CMDSET_INTEL_STANDARD: |
| 893 | case CFI_CMDSET_INTEL_EXTENDED: |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 894 | write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ? |
| 895 | FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 896 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 897 | flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS); |
| 898 | flash_write_cmd (info, sector, 0, write_cmd); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 899 | retcode = flash_status_check (info, sector, |
| 900 | info->buffer_write_tout, |
| 901 | "write to buffer"); |
| 902 | if (retcode == ERR_OK) { |
| 903 | /* reduce the number of loops by the width of |
| 904 | * the port */ |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 905 | cnt = len >> shift; |
Vasiliy Leoenenko | c47c0d4 | 2008-05-07 21:24:44 +0400 | [diff] [blame] | 906 | flash_write_cmd (info, sector, 0, cnt - 1); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 907 | while (cnt-- > 0) { |
| 908 | switch (info->portwidth) { |
| 909 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 910 | flash_write8(flash_read8(src), dst); |
| 911 | src += 1, dst += 1; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 912 | break; |
| 913 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 914 | flash_write16(flash_read16(src), dst); |
| 915 | src += 2, dst += 2; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 916 | break; |
| 917 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 918 | flash_write32(flash_read32(src), dst); |
| 919 | src += 4, dst += 4; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 920 | break; |
| 921 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 922 | flash_write64(flash_read64(src), dst); |
| 923 | src += 8, dst += 8; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 924 | break; |
| 925 | default: |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 926 | retcode = ERR_INVAL; |
| 927 | goto out_unmap; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | flash_write_cmd (info, sector, 0, |
| 931 | FLASH_CMD_WRITE_BUFFER_CONFIRM); |
| 932 | retcode = flash_full_status_check ( |
| 933 | info, sector, info->buffer_write_tout, |
| 934 | "buffer write"); |
| 935 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 936 | |
| 937 | break; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 938 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 939 | case CFI_CMDSET_AMD_STANDARD: |
| 940 | case CFI_CMDSET_AMD_EXTENDED: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 941 | flash_unlock_seq(info,0); |
Guennadi Liakhovetski | 183284f | 2008-04-03 13:36:02 +0200 | [diff] [blame] | 942 | |
| 943 | #ifdef CONFIG_FLASH_SPANSION_S29WS_N |
| 944 | offset = ((unsigned long)dst - info->start[sector]) >> shift; |
| 945 | #endif |
| 946 | flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER); |
| 947 | cnt = len >> shift; |
John Schmoller | ee35588 | 2009-08-12 10:55:47 -0500 | [diff] [blame] | 948 | flash_write_cmd(info, sector, offset, cnt - 1); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 949 | |
| 950 | switch (info->portwidth) { |
| 951 | case FLASH_CFI_8BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 952 | while (cnt-- > 0) { |
| 953 | flash_write8(flash_read8(src), dst); |
| 954 | src += 1, dst += 1; |
| 955 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 956 | break; |
| 957 | case FLASH_CFI_16BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 958 | while (cnt-- > 0) { |
| 959 | flash_write16(flash_read16(src), dst); |
| 960 | src += 2, dst += 2; |
| 961 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 962 | break; |
| 963 | case FLASH_CFI_32BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 964 | while (cnt-- > 0) { |
| 965 | flash_write32(flash_read32(src), dst); |
| 966 | src += 4, dst += 4; |
| 967 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 968 | break; |
| 969 | case FLASH_CFI_64BIT: |
Haavard Skinnemoen | 21b95b4 | 2007-12-13 12:56:32 +0100 | [diff] [blame] | 970 | while (cnt-- > 0) { |
| 971 | flash_write64(flash_read64(src), dst); |
| 972 | src += 8, dst += 8; |
| 973 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 974 | break; |
| 975 | default: |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 976 | retcode = ERR_INVAL; |
| 977 | goto out_unmap; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM); |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 981 | if (use_flash_status_poll(info)) |
| 982 | retcode = flash_status_poll(info, src - (1 << shift), |
| 983 | dst - (1 << shift), |
| 984 | info->buffer_write_tout, |
| 985 | "buffer write"); |
| 986 | else |
| 987 | retcode = flash_full_status_check(info, sector, |
| 988 | info->buffer_write_tout, |
| 989 | "buffer write"); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 990 | break; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 991 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 992 | default: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 993 | debug ("Unknown Command Set\n"); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 994 | retcode = ERR_INVAL; |
| 995 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 996 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 997 | |
| 998 | out_unmap: |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 999 | return retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1000 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1001 | #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1002 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1003 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1004 | /*----------------------------------------------------------------------- |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1005 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1006 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1007 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1008 | int rcode = 0; |
| 1009 | int prot; |
| 1010 | flash_sect_t sect; |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1011 | int st; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1012 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1013 | if (info->flash_id != FLASH_MAN_CFI) { |
| 1014 | puts ("Can't erase unknown flash type - aborted\n"); |
| 1015 | return 1; |
| 1016 | } |
| 1017 | if ((s_first < 0) || (s_first > s_last)) { |
| 1018 | puts ("- no sectors to erase\n"); |
| 1019 | return 1; |
| 1020 | } |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1021 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1022 | prot = 0; |
| 1023 | for (sect = s_first; sect <= s_last; ++sect) { |
| 1024 | if (info->protect[sect]) { |
| 1025 | prot++; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1028 | if (prot) { |
| 1029 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 1030 | prot); |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1031 | } else if (flash_verbose) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1032 | putc ('\n'); |
| 1033 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1034 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1035 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1036 | for (sect = s_first; sect <= s_last; sect++) { |
| 1037 | if (info->protect[sect] == 0) { /* not protected */ |
| 1038 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1039 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1040 | case CFI_CMDSET_INTEL_STANDARD: |
| 1041 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1042 | flash_write_cmd (info, sect, 0, |
| 1043 | FLASH_CMD_CLEAR_STATUS); |
| 1044 | flash_write_cmd (info, sect, 0, |
| 1045 | FLASH_CMD_BLOCK_ERASE); |
| 1046 | flash_write_cmd (info, sect, 0, |
| 1047 | FLASH_CMD_ERASE_CONFIRM); |
| 1048 | break; |
| 1049 | case CFI_CMDSET_AMD_STANDARD: |
| 1050 | case CFI_CMDSET_AMD_EXTENDED: |
| 1051 | flash_unlock_seq (info, sect); |
| 1052 | flash_write_cmd (info, sect, |
| 1053 | info->addr_unlock1, |
| 1054 | AMD_CMD_ERASE_START); |
| 1055 | flash_unlock_seq (info, sect); |
| 1056 | flash_write_cmd (info, sect, 0, |
| 1057 | AMD_CMD_ERASE_SECTOR); |
| 1058 | break; |
| 1059 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1060 | case CFI_CMDSET_AMD_LEGACY: |
| 1061 | flash_unlock_seq (info, 0); |
| 1062 | flash_write_cmd (info, 0, info->addr_unlock1, |
| 1063 | AMD_CMD_ERASE_START); |
| 1064 | flash_unlock_seq (info, 0); |
| 1065 | flash_write_cmd (info, sect, 0, |
| 1066 | AMD_CMD_ERASE_SECTOR); |
| 1067 | break; |
| 1068 | #endif |
| 1069 | default: |
| 1070 | debug ("Unkown flash vendor %d\n", |
| 1071 | info->vendor); |
| 1072 | break; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1073 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1074 | |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1075 | if (use_flash_status_poll(info)) { |
| 1076 | cfiword_t cword = (cfiword_t)0xffffffffffffffffULL; |
| 1077 | void *dest; |
| 1078 | dest = flash_map(info, sect, 0); |
| 1079 | st = flash_status_poll(info, &cword, dest, |
| 1080 | info->erase_blk_tout, "erase"); |
| 1081 | flash_unmap(info, sect, 0, dest); |
| 1082 | } else |
| 1083 | st = flash_full_status_check(info, sect, |
| 1084 | info->erase_blk_tout, |
| 1085 | "erase"); |
| 1086 | if (st) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1087 | rcode = 1; |
Thomas Chou | 076767a | 2010-03-26 08:17:00 +0800 | [diff] [blame] | 1088 | else if (flash_verbose) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1089 | putc ('.'); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1090 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1091 | } |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1092 | |
| 1093 | if (flash_verbose) |
| 1094 | puts (" done\n"); |
| 1095 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1096 | return rcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1097 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1098 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1099 | /*----------------------------------------------------------------------- |
| 1100 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1101 | void flash_print_info (flash_info_t * info) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1102 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1103 | int i; |
wdenk | 369d43d | 2004-03-14 14:09:05 +0000 | [diff] [blame] | 1104 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1105 | if (info->flash_id != FLASH_MAN_CFI) { |
| 1106 | puts ("missing or unknown FLASH type\n"); |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | printf ("%s FLASH (%d x %d)", |
| 1111 | info->name, |
| 1112 | (info->portwidth << 3), (info->chipwidth << 3)); |
| 1113 | if (info->size < 1024*1024) |
| 1114 | printf (" Size: %ld kB in %d Sectors\n", |
| 1115 | info->size >> 10, info->sector_count); |
| 1116 | else |
| 1117 | printf (" Size: %ld MB in %d Sectors\n", |
| 1118 | info->size >> 20, info->sector_count); |
| 1119 | printf (" "); |
| 1120 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1121 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
| 1122 | printf ("Intel Prog Regions"); |
| 1123 | break; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1124 | case CFI_CMDSET_INTEL_STANDARD: |
| 1125 | printf ("Intel Standard"); |
| 1126 | break; |
| 1127 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1128 | printf ("Intel Extended"); |
| 1129 | break; |
| 1130 | case CFI_CMDSET_AMD_STANDARD: |
| 1131 | printf ("AMD Standard"); |
| 1132 | break; |
| 1133 | case CFI_CMDSET_AMD_EXTENDED: |
| 1134 | printf ("AMD Extended"); |
| 1135 | break; |
| 1136 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1137 | case CFI_CMDSET_AMD_LEGACY: |
| 1138 | printf ("AMD Legacy"); |
| 1139 | break; |
wdenk | 369d43d | 2004-03-14 14:09:05 +0000 | [diff] [blame] | 1140 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1141 | default: |
| 1142 | printf ("Unknown (%d)", info->vendor); |
| 1143 | break; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1144 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1145 | printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X", |
| 1146 | info->manufacturer_id, info->device_id); |
| 1147 | if (info->device_id == 0x7E) { |
| 1148 | printf("%04X", info->device_id2); |
| 1149 | } |
| 1150 | printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n", |
| 1151 | info->erase_blk_tout, |
| 1152 | info->write_tout); |
| 1153 | if (info->buffer_size > 1) { |
| 1154 | printf (" Buffer write timeout: %ld ms, " |
| 1155 | "buffer size: %d bytes\n", |
| 1156 | info->buffer_write_tout, |
| 1157 | info->buffer_size); |
| 1158 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1159 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1160 | puts ("\n Sector Start Addresses:"); |
| 1161 | for (i = 0; i < info->sector_count; ++i) { |
| 1162 | if ((i % 5) == 0) |
| 1163 | printf ("\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1164 | #ifdef CONFIG_SYS_FLASH_EMPTY_INFO |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1165 | int k; |
| 1166 | int size; |
| 1167 | int erased; |
| 1168 | volatile unsigned long *flash; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1169 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1170 | /* |
| 1171 | * Check if whole sector is erased |
| 1172 | */ |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1173 | size = flash_sector_size(info, i); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1174 | erased = 1; |
| 1175 | flash = (volatile unsigned long *) info->start[i]; |
| 1176 | size = size >> 2; /* divide by 4 for longword access */ |
| 1177 | for (k = 0; k < size; k++) { |
| 1178 | if (*flash++ != 0xffffffff) { |
| 1179 | erased = 0; |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1183 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1184 | /* print empty and read-only info */ |
| 1185 | printf (" %08lX %c %s ", |
| 1186 | info->start[i], |
| 1187 | erased ? 'E' : ' ', |
| 1188 | info->protect[i] ? "RO" : " "); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1189 | #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1190 | printf (" %08lX %s ", |
| 1191 | info->start[i], |
| 1192 | info->protect[i] ? "RO" : " "); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1193 | #endif |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1194 | } |
| 1195 | putc ('\n'); |
| 1196 | return; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1199 | /*----------------------------------------------------------------------- |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1200 | * This is used in a few places in write_buf() to show programming |
| 1201 | * progress. Making it a function is nasty because it needs to do side |
| 1202 | * effect updates to digit and dots. Repeated code is nasty too, so |
| 1203 | * we define it once here. |
| 1204 | */ |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1205 | #ifdef CONFIG_FLASH_SHOW_PROGRESS |
| 1206 | #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \ |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1207 | if (flash_verbose) { \ |
| 1208 | dots -= dots_sub; \ |
| 1209 | if ((scale > 0) && (dots <= 0)) { \ |
| 1210 | if ((digit % 5) == 0) \ |
| 1211 | printf ("%d", digit / 5); \ |
| 1212 | else \ |
| 1213 | putc ('.'); \ |
| 1214 | digit--; \ |
| 1215 | dots += scale; \ |
| 1216 | } \ |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1217 | } |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1218 | #else |
| 1219 | #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) |
| 1220 | #endif |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1221 | |
| 1222 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1223 | * Copy memory to flash, returns: |
| 1224 | * 0 - OK |
| 1225 | * 1 - write timeout |
| 1226 | * 2 - Flash not erased |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1227 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1228 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1229 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1230 | ulong wp; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1231 | uchar *p; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1232 | int aln; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1233 | cfiword_t cword; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1234 | int i, rc; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1235 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1236 | int buffered_size; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1237 | #endif |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1238 | #ifdef CONFIG_FLASH_SHOW_PROGRESS |
| 1239 | int digit = CONFIG_FLASH_SHOW_PROGRESS; |
| 1240 | int scale = 0; |
| 1241 | int dots = 0; |
| 1242 | |
| 1243 | /* |
| 1244 | * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes. |
| 1245 | */ |
| 1246 | if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) { |
| 1247 | scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) / |
| 1248 | CONFIG_FLASH_SHOW_PROGRESS); |
| 1249 | } |
| 1250 | #endif |
| 1251 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1252 | /* get lower aligned address */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1253 | wp = (addr & ~(info->portwidth - 1)); |
Haiying Wang | c123a38 | 2007-02-21 16:52:31 +0100 | [diff] [blame] | 1254 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1255 | /* handle unaligned start */ |
| 1256 | if ((aln = addr - wp) != 0) { |
| 1257 | cword.l = 0; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1258 | p = (uchar *)wp; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1259 | for (i = 0; i < aln; ++i) |
| 1260 | flash_add_byte (info, &cword, flash_read8(p + i)); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1261 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1262 | for (; (i < info->portwidth) && (cnt > 0); i++) { |
| 1263 | flash_add_byte (info, &cword, *src++); |
| 1264 | cnt--; |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1265 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1266 | for (; (cnt == 0) && (i < info->portwidth); ++i) |
| 1267 | flash_add_byte (info, &cword, flash_read8(p + i)); |
| 1268 | |
| 1269 | rc = flash_write_cfiword (info, wp, cword); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1270 | if (rc != 0) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1271 | return rc; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1272 | |
| 1273 | wp += i; |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1274 | FLASH_SHOW_PROGRESS(scale, dots, digit, i); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | /* handle the aligned part */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1278 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1279 | buffered_size = (info->portwidth / info->chipwidth); |
| 1280 | buffered_size *= info->buffer_size; |
| 1281 | while (cnt >= info->portwidth) { |
| 1282 | /* prohibit buffer write when buffer_size is 1 */ |
| 1283 | if (info->buffer_size == 1) { |
| 1284 | cword.l = 0; |
| 1285 | for (i = 0; i < info->portwidth; i++) |
| 1286 | flash_add_byte (info, &cword, *src++); |
| 1287 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
| 1288 | return rc; |
| 1289 | wp += info->portwidth; |
| 1290 | cnt -= info->portwidth; |
| 1291 | continue; |
| 1292 | } |
| 1293 | |
| 1294 | /* write buffer until next buffered_size aligned boundary */ |
| 1295 | i = buffered_size - (wp % buffered_size); |
| 1296 | if (i > cnt) |
| 1297 | i = cnt; |
| 1298 | if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK) |
| 1299 | return rc; |
| 1300 | i -= i & (info->portwidth - 1); |
| 1301 | wp += i; |
| 1302 | src += i; |
| 1303 | cnt -= i; |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1304 | FLASH_SHOW_PROGRESS(scale, dots, digit, i); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1305 | } |
| 1306 | #else |
| 1307 | while (cnt >= info->portwidth) { |
| 1308 | cword.l = 0; |
| 1309 | for (i = 0; i < info->portwidth; i++) { |
| 1310 | flash_add_byte (info, &cword, *src++); |
| 1311 | } |
| 1312 | if ((rc = flash_write_cfiword (info, wp, cword)) != 0) |
| 1313 | return rc; |
| 1314 | wp += info->portwidth; |
| 1315 | cnt -= info->portwidth; |
Stefan Roese | 7758c16 | 2008-03-19 07:09:26 +0100 | [diff] [blame] | 1316 | FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1317 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1318 | #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */ |
Jerry Van Baren | aae7357 | 2008-03-08 13:48:01 -0500 | [diff] [blame] | 1319 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1320 | if (cnt == 0) { |
| 1321 | return (0); |
| 1322 | } |
| 1323 | |
| 1324 | /* |
| 1325 | * handle unaligned tail bytes |
| 1326 | */ |
| 1327 | cword.l = 0; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1328 | p = (uchar *)wp; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1329 | for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1330 | flash_add_byte (info, &cword, *src++); |
| 1331 | --cnt; |
| 1332 | } |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1333 | for (; i < info->portwidth; ++i) |
| 1334 | flash_add_byte (info, &cword, flash_read8(p + i)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1335 | |
| 1336 | return flash_write_cfiword (info, wp, cword); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1337 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1338 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1339 | /*----------------------------------------------------------------------- |
| 1340 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1341 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1342 | |
| 1343 | int flash_real_protect (flash_info_t * info, long sector, int prot) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1344 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1345 | int retcode = 0; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1346 | |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1347 | switch (info->vendor) { |
| 1348 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
| 1349 | case CFI_CMDSET_INTEL_STANDARD: |
Nick Spence | ec81b47 | 2008-08-19 22:21:16 -0700 | [diff] [blame] | 1350 | case CFI_CMDSET_INTEL_EXTENDED: |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1351 | flash_write_cmd (info, sector, 0, |
| 1352 | FLASH_CMD_CLEAR_STATUS); |
| 1353 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT); |
| 1354 | if (prot) |
| 1355 | flash_write_cmd (info, sector, 0, |
| 1356 | FLASH_CMD_PROTECT_SET); |
| 1357 | else |
| 1358 | flash_write_cmd (info, sector, 0, |
| 1359 | FLASH_CMD_PROTECT_CLEAR); |
| 1360 | break; |
| 1361 | case CFI_CMDSET_AMD_EXTENDED: |
| 1362 | case CFI_CMDSET_AMD_STANDARD: |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1363 | /* U-Boot only checks the first byte */ |
| 1364 | if (info->manufacturer_id == (uchar)ATM_MANUFACT) { |
| 1365 | if (prot) { |
| 1366 | flash_unlock_seq (info, 0); |
| 1367 | flash_write_cmd (info, 0, |
| 1368 | info->addr_unlock1, |
| 1369 | ATM_CMD_SOFTLOCK_START); |
| 1370 | flash_unlock_seq (info, 0); |
| 1371 | flash_write_cmd (info, sector, 0, |
| 1372 | ATM_CMD_LOCK_SECT); |
| 1373 | } else { |
| 1374 | flash_write_cmd (info, 0, |
| 1375 | info->addr_unlock1, |
| 1376 | AMD_CMD_UNLOCK_START); |
| 1377 | if (info->device_id == ATM_ID_BV6416) |
| 1378 | flash_write_cmd (info, sector, |
| 1379 | 0, ATM_CMD_UNLOCK_SECT); |
| 1380 | } |
| 1381 | } |
| 1382 | break; |
TsiChung Liew | b8c1929 | 2008-08-19 16:53:39 +0000 | [diff] [blame] | 1383 | #ifdef CONFIG_FLASH_CFI_LEGACY |
| 1384 | case CFI_CMDSET_AMD_LEGACY: |
| 1385 | flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 1386 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT); |
| 1387 | if (prot) |
| 1388 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET); |
| 1389 | else |
| 1390 | flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR); |
| 1391 | #endif |
Rafael Campos | 13d2b61 | 2008-07-31 10:22:20 +0200 | [diff] [blame] | 1392 | }; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1393 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1394 | if ((retcode = |
| 1395 | flash_full_status_check (info, sector, info->erase_blk_tout, |
| 1396 | prot ? "protect" : "unprotect")) == 0) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1397 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1398 | info->protect[sector] = prot; |
| 1399 | |
| 1400 | /* |
| 1401 | * On some of Intel's flash chips (marked via legacy_unlock) |
| 1402 | * unprotect unprotects all locking. |
| 1403 | */ |
| 1404 | if ((prot == 0) && (info->legacy_unlock)) { |
| 1405 | flash_sect_t i; |
| 1406 | |
| 1407 | for (i = 0; i < info->sector_count; i++) { |
| 1408 | if (info->protect[i]) |
| 1409 | flash_real_protect (info, i, 1); |
| 1410 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1411 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1412 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1413 | return retcode; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1414 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1415 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1416 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1417 | * flash_read_user_serial - read the OneTimeProgramming cells |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1418 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1419 | void flash_read_user_serial (flash_info_t * info, void *buffer, int offset, |
| 1420 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1421 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1422 | uchar *src; |
| 1423 | uchar *dst; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1424 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1425 | dst = buffer; |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1426 | src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1427 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 1428 | memcpy (dst, src + offset, len); |
| 1429 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1430 | flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1433 | /* |
| 1434 | * flash_read_factory_serial - read the device Id from the protection area |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1435 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1436 | void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset, |
| 1437 | int len) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1438 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1439 | uchar *src; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1440 | |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1441 | src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1442 | flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID); |
| 1443 | memcpy (buffer, src + offset, len); |
| 1444 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
Haavard Skinnemoen | e3ea188 | 2007-12-13 12:56:34 +0100 | [diff] [blame] | 1445 | flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1448 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1449 | |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1450 | /*----------------------------------------------------------------------- |
| 1451 | * Reverse the order of the erase regions in the CFI QRY structure. |
| 1452 | * This is needed for chips that are either a) correctly detected as |
| 1453 | * top-boot, or b) buggy. |
| 1454 | */ |
| 1455 | static void cfi_reverse_geometry(struct cfi_qry *qry) |
| 1456 | { |
| 1457 | unsigned int i, j; |
| 1458 | u32 tmp; |
| 1459 | |
| 1460 | for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) { |
| 1461 | tmp = qry->erase_region_info[i]; |
| 1462 | qry->erase_region_info[i] = qry->erase_region_info[j]; |
| 1463 | qry->erase_region_info[j] = tmp; |
| 1464 | } |
| 1465 | } |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1466 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1467 | /*----------------------------------------------------------------------- |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1468 | * read jedec ids from device and set corresponding fields in info struct |
| 1469 | * |
| 1470 | * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct |
| 1471 | * |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1472 | */ |
| 1473 | static void cmdset_intel_read_jedec_ids(flash_info_t *info) |
| 1474 | { |
| 1475 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1476 | flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID); |
| 1477 | udelay(1000); /* some flash are slow to respond */ |
| 1478 | info->manufacturer_id = flash_read_uchar (info, |
| 1479 | FLASH_OFFSET_MANUFACTURER_ID); |
| 1480 | info->device_id = flash_read_uchar (info, |
| 1481 | FLASH_OFFSET_DEVICE_ID); |
| 1482 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1483 | } |
| 1484 | |
| 1485 | static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry) |
| 1486 | { |
| 1487 | info->cmd_reset = FLASH_CMD_RESET; |
| 1488 | |
| 1489 | cmdset_intel_read_jedec_ids(info); |
| 1490 | flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI); |
| 1491 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1492 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1493 | /* read legacy lock/unlock bit from intel flash */ |
| 1494 | if (info->ext_addr) { |
| 1495 | info->legacy_unlock = flash_read_uchar (info, |
| 1496 | info->ext_addr + 5) & 0x08; |
| 1497 | } |
| 1498 | #endif |
| 1499 | |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | static void cmdset_amd_read_jedec_ids(flash_info_t *info) |
| 1504 | { |
Niklaus Giger | f447f71 | 2009-07-22 17:13:24 +0200 | [diff] [blame] | 1505 | ushort bankId = 0; |
| 1506 | uchar manuId; |
| 1507 | |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1508 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
| 1509 | flash_unlock_seq(info, 0); |
| 1510 | flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID); |
| 1511 | udelay(1000); /* some flash are slow to respond */ |
Tor Krill | 7f2a305 | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 1512 | |
Niklaus Giger | f447f71 | 2009-07-22 17:13:24 +0200 | [diff] [blame] | 1513 | manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID); |
| 1514 | /* JEDEC JEP106Z specifies ID codes up to bank 7 */ |
| 1515 | while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) { |
| 1516 | bankId += 0x100; |
| 1517 | manuId = flash_read_uchar (info, |
| 1518 | bankId | FLASH_OFFSET_MANUFACTURER_ID); |
| 1519 | } |
| 1520 | info->manufacturer_id = manuId; |
Tor Krill | 7f2a305 | 2008-03-28 11:29:10 +0100 | [diff] [blame] | 1521 | |
| 1522 | switch (info->chipwidth){ |
| 1523 | case FLASH_CFI_8BIT: |
| 1524 | info->device_id = flash_read_uchar (info, |
| 1525 | FLASH_OFFSET_DEVICE_ID); |
| 1526 | if (info->device_id == 0x7E) { |
| 1527 | /* AMD 3-byte (expanded) device ids */ |
| 1528 | info->device_id2 = flash_read_uchar (info, |
| 1529 | FLASH_OFFSET_DEVICE_ID2); |
| 1530 | info->device_id2 <<= 8; |
| 1531 | info->device_id2 |= flash_read_uchar (info, |
| 1532 | FLASH_OFFSET_DEVICE_ID3); |
| 1533 | } |
| 1534 | break; |
| 1535 | case FLASH_CFI_16BIT: |
| 1536 | info->device_id = flash_read_word (info, |
| 1537 | FLASH_OFFSET_DEVICE_ID); |
| 1538 | break; |
| 1539 | default: |
| 1540 | break; |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1541 | } |
| 1542 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
| 1543 | } |
| 1544 | |
| 1545 | static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry) |
| 1546 | { |
| 1547 | info->cmd_reset = AMD_CMD_RESET; |
| 1548 | |
| 1549 | cmdset_amd_read_jedec_ids(info); |
| 1550 | flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI); |
| 1551 | |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | #ifdef CONFIG_FLASH_CFI_LEGACY |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1556 | static void flash_read_jedec_ids (flash_info_t * info) |
| 1557 | { |
| 1558 | info->manufacturer_id = 0; |
| 1559 | info->device_id = 0; |
| 1560 | info->device_id2 = 0; |
| 1561 | |
| 1562 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1563 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1564 | case CFI_CMDSET_INTEL_STANDARD: |
| 1565 | case CFI_CMDSET_INTEL_EXTENDED: |
Michael Schwingen | 5fb0aa4 | 2008-01-12 20:29:47 +0100 | [diff] [blame] | 1566 | cmdset_intel_read_jedec_ids(info); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1567 | break; |
| 1568 | case CFI_CMDSET_AMD_STANDARD: |
| 1569 | case CFI_CMDSET_AMD_EXTENDED: |
Michael Schwingen | 5fb0aa4 | 2008-01-12 20:29:47 +0100 | [diff] [blame] | 1570 | cmdset_amd_read_jedec_ids(info); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1571 | break; |
| 1572 | default: |
| 1573 | break; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | /*----------------------------------------------------------------------- |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1578 | * Call board code to request info about non-CFI flash. |
| 1579 | * board_flash_get_legacy needs to fill in at least: |
| 1580 | * info->portwidth, info->chipwidth and info->interface for Jedec probing. |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1581 | */ |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1582 | static int flash_detect_legacy(phys_addr_t base, int banknum) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1583 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1584 | flash_info_t *info = &flash_info[banknum]; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1585 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1586 | if (board_flash_get_legacy(base, banknum, info)) { |
| 1587 | /* board code may have filled info completely. If not, we |
| 1588 | use JEDEC ID probing. */ |
| 1589 | if (!info->vendor) { |
| 1590 | int modes[] = { |
| 1591 | CFI_CMDSET_AMD_STANDARD, |
| 1592 | CFI_CMDSET_INTEL_STANDARD |
| 1593 | }; |
| 1594 | int i; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1595 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1596 | for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) { |
| 1597 | info->vendor = modes[i]; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1598 | info->start[0] = |
| 1599 | (ulong)map_physmem(base, |
Stefan Roese | b844370 | 2009-02-05 11:44:52 +0100 | [diff] [blame] | 1600 | info->portwidth, |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1601 | MAP_NOCACHE); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1602 | if (info->portwidth == FLASH_CFI_8BIT |
| 1603 | && info->interface == FLASH_CFI_X8X16) { |
| 1604 | info->addr_unlock1 = 0x2AAA; |
| 1605 | info->addr_unlock2 = 0x5555; |
| 1606 | } else { |
| 1607 | info->addr_unlock1 = 0x5555; |
| 1608 | info->addr_unlock2 = 0x2AAA; |
| 1609 | } |
| 1610 | flash_read_jedec_ids(info); |
| 1611 | debug("JEDEC PROBE: ID %x %x %x\n", |
| 1612 | info->manufacturer_id, |
| 1613 | info->device_id, |
| 1614 | info->device_id2); |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1615 | if (jedec_flash_match(info, info->start[0])) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1616 | break; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1617 | else |
Stefan Roese | b844370 | 2009-02-05 11:44:52 +0100 | [diff] [blame] | 1618 | unmap_physmem((void *)info->start[0], |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1619 | MAP_NOCACHE); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | switch(info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1624 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1625 | case CFI_CMDSET_INTEL_STANDARD: |
| 1626 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1627 | info->cmd_reset = FLASH_CMD_RESET; |
| 1628 | break; |
| 1629 | case CFI_CMDSET_AMD_STANDARD: |
| 1630 | case CFI_CMDSET_AMD_EXTENDED: |
| 1631 | case CFI_CMDSET_AMD_LEGACY: |
| 1632 | info->cmd_reset = AMD_CMD_RESET; |
| 1633 | break; |
| 1634 | } |
| 1635 | info->flash_id = FLASH_MAN_CFI; |
| 1636 | return 1; |
| 1637 | } |
| 1638 | return 0; /* use CFI */ |
| 1639 | } |
| 1640 | #else |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1641 | static inline int flash_detect_legacy(phys_addr_t base, int banknum) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1642 | { |
| 1643 | return 0; /* use CFI */ |
| 1644 | } |
| 1645 | #endif |
| 1646 | |
| 1647 | /*----------------------------------------------------------------------- |
| 1648 | * detect if flash is compatible with the Common Flash Interface (CFI) |
| 1649 | * http://www.jedec.org/download/search/jesd68.pdf |
| 1650 | */ |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1651 | static void flash_read_cfi (flash_info_t *info, void *buf, |
| 1652 | unsigned int start, size_t len) |
| 1653 | { |
| 1654 | u8 *p = buf; |
| 1655 | unsigned int i; |
| 1656 | |
| 1657 | for (i = 0; i < len; i++) |
| 1658 | p[i] = flash_read_uchar(info, start + i); |
| 1659 | } |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 1660 | |
| 1661 | void __flash_cmd_reset(flash_info_t *info) |
| 1662 | { |
| 1663 | /* |
| 1664 | * We do not yet know what kind of commandset to use, so we issue |
| 1665 | * the reset command in both Intel and AMD variants, in the hope |
| 1666 | * that AMD flash roms ignore the Intel command. |
| 1667 | */ |
| 1668 | flash_write_cmd(info, 0, 0, AMD_CMD_RESET); |
| 1669 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1670 | } |
| 1671 | void flash_cmd_reset(flash_info_t *info) |
| 1672 | __attribute__((weak,alias("__flash_cmd_reset"))); |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1673 | |
| 1674 | static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1675 | { |
| 1676 | int cfi_offset; |
| 1677 | |
Stefan Roese | 6e83e34 | 2009-10-27 15:15:55 +0100 | [diff] [blame] | 1678 | /* Issue FLASH reset command */ |
| 1679 | flash_cmd_reset(info); |
Michael Schwingen | 4661cf7 | 2008-02-18 23:16:35 +0100 | [diff] [blame] | 1680 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1681 | for (cfi_offset=0; |
| 1682 | cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint); |
| 1683 | cfi_offset++) { |
| 1684 | flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], |
| 1685 | FLASH_CMD_CFI); |
| 1686 | if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q') |
| 1687 | && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') |
| 1688 | && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) { |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1689 | flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP, |
| 1690 | sizeof(struct cfi_qry)); |
| 1691 | info->interface = le16_to_cpu(qry->interface_desc); |
| 1692 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1693 | info->cfi_offset = flash_offset_cfi[cfi_offset]; |
| 1694 | debug ("device interface is %d\n", |
| 1695 | info->interface); |
| 1696 | debug ("found port %d chip %d ", |
| 1697 | info->portwidth, info->chipwidth); |
| 1698 | debug ("port %d bits chip %d bits\n", |
| 1699 | info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 1700 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 1701 | |
| 1702 | /* calculate command offsets as in the Linux driver */ |
| 1703 | info->addr_unlock1 = 0x555; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1704 | info->addr_unlock2 = 0x2aa; |
| 1705 | |
| 1706 | /* |
| 1707 | * modify the unlock address if we are |
| 1708 | * in compatibility mode |
| 1709 | */ |
| 1710 | if ( /* x8/x16 in x8 mode */ |
| 1711 | ((info->chipwidth == FLASH_CFI_BY8) && |
| 1712 | (info->interface == FLASH_CFI_X8X16)) || |
| 1713 | /* x16/x32 in x16 mode */ |
| 1714 | ((info->chipwidth == FLASH_CFI_BY16) && |
| 1715 | (info->interface == FLASH_CFI_X16X32))) |
| 1716 | { |
| 1717 | info->addr_unlock1 = 0xaaa; |
| 1718 | info->addr_unlock2 = 0x555; |
| 1719 | } |
| 1720 | |
| 1721 | info->name = "CFI conformant"; |
| 1722 | return 1; |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1729 | static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry) |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1730 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1731 | debug ("flash detect cfi\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1732 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1733 | for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1734 | info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) { |
| 1735 | for (info->chipwidth = FLASH_CFI_BY8; |
| 1736 | info->chipwidth <= info->portwidth; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1737 | info->chipwidth <<= 1) |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1738 | if (__flash_detect_cfi(info, qry)) |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1739 | return 1; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1740 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1741 | debug ("not found\n"); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1744 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1745 | /* |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1746 | * Manufacturer-specific quirks. Add workarounds for geometry |
| 1747 | * reversal, etc. here. |
| 1748 | */ |
| 1749 | static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry) |
| 1750 | { |
| 1751 | /* check if flash geometry needs reversal */ |
| 1752 | if (qry->num_erase_regions > 1) { |
| 1753 | /* reverse geometry if top boot part */ |
| 1754 | if (info->cfi_version < 0x3131) { |
| 1755 | /* CFI < 1.1, try to guess from device id */ |
| 1756 | if ((info->device_id & 0x80) != 0) |
| 1757 | cfi_reverse_geometry(qry); |
| 1758 | } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) { |
| 1759 | /* CFI >= 1.1, deduct from top/bottom flag */ |
| 1760 | /* note: ext_addr is valid since cfi_version > 0 */ |
| 1761 | cfi_reverse_geometry(qry); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry) |
| 1767 | { |
| 1768 | int reverse_geometry = 0; |
| 1769 | |
| 1770 | /* Check the "top boot" bit in the PRI */ |
| 1771 | if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1)) |
| 1772 | reverse_geometry = 1; |
| 1773 | |
| 1774 | /* AT49BV6416(T) list the erase regions in the wrong order. |
| 1775 | * However, the device ID is identical with the non-broken |
Ulf Samuelsson | 07f9b4e | 2009-03-27 23:26:43 +0100 | [diff] [blame] | 1776 | * AT49BV642D they differ in the high byte. |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1777 | */ |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1778 | if (info->device_id == 0xd6 || info->device_id == 0xd2) |
| 1779 | reverse_geometry = !reverse_geometry; |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1780 | |
| 1781 | if (reverse_geometry) |
| 1782 | cfi_reverse_geometry(qry); |
| 1783 | } |
| 1784 | |
Richard Retanubun | be4dca2 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1785 | static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry) |
| 1786 | { |
| 1787 | /* check if flash geometry needs reversal */ |
| 1788 | if (qry->num_erase_regions > 1) { |
| 1789 | /* reverse geometry if top boot part */ |
| 1790 | if (info->cfi_version < 0x3131) { |
Richard Retanubun | 47255a5 | 2009-03-06 10:09:37 -0500 | [diff] [blame] | 1791 | /* CFI < 1.1, guess by device id (M29W320{DT,ET} only) */ |
| 1792 | if (info->device_id == 0x22CA || |
| 1793 | info->device_id == 0x2256) { |
Richard Retanubun | be4dca2 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1794 | cfi_reverse_geometry(qry); |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | } |
| 1799 | |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1800 | /* |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1801 | * The following code cannot be run from FLASH! |
| 1802 | * |
| 1803 | */ |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1804 | ulong flash_get_size (phys_addr_t base, int banknum) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1805 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1806 | flash_info_t *info = &flash_info[banknum]; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1807 | int i, j; |
| 1808 | flash_sect_t sect_cnt; |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1809 | phys_addr_t sector; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1810 | unsigned long tmp; |
| 1811 | int size_ratio; |
| 1812 | uchar num_erase_regions; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1813 | int erase_region_size; |
| 1814 | int erase_region_count; |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1815 | struct cfi_qry qry; |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1816 | |
Kumar Gala | 899032b | 2008-05-15 15:13:08 -0500 | [diff] [blame] | 1817 | memset(&qry, 0, sizeof(qry)); |
| 1818 | |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1819 | info->ext_addr = 0; |
| 1820 | info->cfi_version = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1821 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Stefan Roese | efef95b | 2006-04-01 13:41:03 +0200 | [diff] [blame] | 1822 | info->legacy_unlock = 0; |
| 1823 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1824 | |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1825 | info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1826 | |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1827 | if (flash_detect_cfi (info, &qry)) { |
| 1828 | info->vendor = le16_to_cpu(qry.p_id); |
| 1829 | info->ext_addr = le16_to_cpu(qry.p_adr); |
| 1830 | num_erase_regions = qry.num_erase_regions; |
| 1831 | |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1832 | if (info->ext_addr) { |
| 1833 | info->cfi_version = (ushort) flash_read_uchar (info, |
| 1834 | info->ext_addr + 3) << 8; |
| 1835 | info->cfi_version |= (ushort) flash_read_uchar (info, |
| 1836 | info->ext_addr + 4); |
| 1837 | } |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1838 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1839 | #ifdef DEBUG |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1840 | flash_printqry (&qry); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1841 | #endif |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1842 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1843 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1844 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1845 | case CFI_CMDSET_INTEL_STANDARD: |
| 1846 | case CFI_CMDSET_INTEL_EXTENDED: |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1847 | cmdset_intel_init(info, &qry); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1848 | break; |
| 1849 | case CFI_CMDSET_AMD_STANDARD: |
| 1850 | case CFI_CMDSET_AMD_EXTENDED: |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1851 | cmdset_amd_init(info, &qry); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1852 | break; |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1853 | default: |
| 1854 | printf("CFI: Unknown command set 0x%x\n", |
| 1855 | info->vendor); |
| 1856 | /* |
| 1857 | * Unfortunately, this means we don't know how |
| 1858 | * to get the chip back to Read mode. Might |
| 1859 | * as well try an Intel-style reset... |
| 1860 | */ |
| 1861 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 1862 | return 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1863 | } |
wdenk | 6cfa84e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 1864 | |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1865 | /* Do manufacturer-specific fixups */ |
| 1866 | switch (info->manufacturer_id) { |
| 1867 | case 0x0001: |
| 1868 | flash_fixup_amd(info, &qry); |
| 1869 | break; |
| 1870 | case 0x001f: |
| 1871 | flash_fixup_atmel(info, &qry); |
| 1872 | break; |
Richard Retanubun | be4dca2 | 2009-01-14 08:44:26 -0500 | [diff] [blame] | 1873 | case 0x0020: |
| 1874 | flash_fixup_stm(info, &qry); |
| 1875 | break; |
Haavard Skinnemoen | 750ea7f | 2007-12-14 15:36:18 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1878 | debug ("manufacturer is %d\n", info->vendor); |
Stefan Roese | 1279748 | 2006-11-13 13:55:24 +0100 | [diff] [blame] | 1879 | debug ("manufacturer id is 0x%x\n", info->manufacturer_id); |
| 1880 | debug ("device id is 0x%x\n", info->device_id); |
| 1881 | debug ("device id2 is 0x%x\n", info->device_id2); |
| 1882 | debug ("cfi version is 0x%04x\n", info->cfi_version); |
| 1883 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1884 | size_ratio = info->portwidth / info->chipwidth; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1885 | /* if the chip is x8/x16 reduce the ratio by half */ |
| 1886 | if ((info->interface == FLASH_CFI_X8X16) |
| 1887 | && (info->chipwidth == FLASH_CFI_BY8)) { |
| 1888 | size_ratio >>= 1; |
| 1889 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1890 | debug ("size_ratio %d port %d bits chip %d bits\n", |
| 1891 | size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH, |
| 1892 | info->chipwidth << CFI_FLASH_SHIFT_WIDTH); |
| 1893 | debug ("found %d erase regions\n", num_erase_regions); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1894 | sect_cnt = 0; |
| 1895 | sector = base; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1896 | for (i = 0; i < num_erase_regions; i++) { |
| 1897 | if (i > NUM_ERASE_REGIONS) { |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 1898 | printf ("%d erase regions found, only %d used\n", |
| 1899 | num_erase_regions, NUM_ERASE_REGIONS); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1900 | break; |
| 1901 | } |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1902 | |
Haavard Skinnemoen | c4d478b | 2007-12-14 15:36:17 +0100 | [diff] [blame] | 1903 | tmp = le32_to_cpu(qry.erase_region_info[i]); |
| 1904 | debug("erase region %u: 0x%08lx\n", i, tmp); |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1905 | |
| 1906 | erase_region_count = (tmp & 0xffff) + 1; |
| 1907 | tmp >>= 16; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1908 | erase_region_size = |
| 1909 | (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128; |
wdenk | aeba06f | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 1910 | debug ("erase_region_count = %d erase_region_size = %d\n", |
wdenk | e537b3b | 2004-02-23 23:54:43 +0000 | [diff] [blame] | 1911 | erase_region_count, erase_region_size); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1912 | for (j = 0; j < erase_region_count; j++) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1913 | if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) { |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1914 | printf("ERROR: too many flash sectors\n"); |
| 1915 | break; |
| 1916 | } |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1917 | info->start[sect_cnt] = |
| 1918 | (ulong)map_physmem(sector, |
| 1919 | info->portwidth, |
| 1920 | MAP_NOCACHE); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1921 | sector += (erase_region_size * size_ratio); |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 1922 | |
| 1923 | /* |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1924 | * Only read protection status from |
| 1925 | * supported devices (intel...) |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 1926 | */ |
| 1927 | switch (info->vendor) { |
Vasiliy Leoenenko | 7d1794c | 2008-05-07 21:25:33 +0400 | [diff] [blame] | 1928 | case CFI_CMDSET_INTEL_PROG_REGIONS: |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 1929 | case CFI_CMDSET_INTEL_EXTENDED: |
| 1930 | case CFI_CMDSET_INTEL_STANDARD: |
| 1931 | info->protect[sect_cnt] = |
| 1932 | flash_isset (info, sect_cnt, |
| 1933 | FLASH_OFFSET_PROTECT, |
| 1934 | FLASH_STATUS_PROTECT); |
| 1935 | break; |
| 1936 | default: |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1937 | /* default: not protected */ |
| 1938 | info->protect[sect_cnt] = 0; |
wdenk | 26c5843 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1941 | sect_cnt++; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | info->sector_count = sect_cnt; |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1946 | info->size = 1 << qry.dev_size; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1947 | /* multiply the size by the number of chips */ |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1948 | info->size *= size_ratio; |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1949 | info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size); |
| 1950 | tmp = 1 << qry.block_erase_timeout_typ; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1951 | info->erase_blk_tout = tmp * |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1952 | (1 << qry.block_erase_timeout_max); |
| 1953 | tmp = (1 << qry.buf_write_timeout_typ) * |
| 1954 | (1 << qry.buf_write_timeout_max); |
| 1955 | |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1956 | /* round up when converting to ms */ |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1957 | info->buffer_write_tout = (tmp + 999) / 1000; |
| 1958 | tmp = (1 << qry.word_write_timeout_typ) * |
| 1959 | (1 << qry.word_write_timeout_max); |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1960 | /* round up when converting to ms */ |
Haavard Skinnemoen | 53baec7 | 2007-12-14 15:36:16 +0100 | [diff] [blame] | 1961 | info->write_tout = (tmp + 999) / 1000; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1962 | info->flash_id = FLASH_MAN_CFI; |
Haavard Skinnemoen | d523e39 | 2007-12-13 12:56:28 +0100 | [diff] [blame] | 1963 | if ((info->interface == FLASH_CFI_X8X16) && |
| 1964 | (info->chipwidth == FLASH_CFI_BY8)) { |
| 1965 | /* XXX - Need to test on x8/x16 in parallel. */ |
| 1966 | info->portwidth >>= 1; |
wdenk | ed2ac4b | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 1967 | } |
Mike Frysinger | 59404ee | 2008-10-02 01:55:38 -0400 | [diff] [blame] | 1968 | |
| 1969 | flash_write_cmd (info, 0, 0, info->cmd_reset); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1972 | return (info->size); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Piotr Ziecik | 2a7493c | 2008-11-17 15:49:32 +0100 | [diff] [blame] | 1975 | void flash_set_verbose(uint v) |
| 1976 | { |
| 1977 | flash_verbose = v; |
| 1978 | } |
| 1979 | |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1980 | /*----------------------------------------------------------------------- |
| 1981 | */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1982 | unsigned long flash_init (void) |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1983 | { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 1984 | unsigned long size = 0; |
| 1985 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1986 | #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST) |
Matthias Fuchs | 5043152 | 2008-04-18 16:29:40 +0200 | [diff] [blame] | 1987 | struct apl_s { |
| 1988 | ulong start; |
| 1989 | ulong size; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1990 | } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST; |
Matthias Fuchs | 5043152 | 2008-04-18 16:29:40 +0200 | [diff] [blame] | 1991 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1992 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1993 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Eric Schumann | 8f7ee7d | 2009-03-21 09:59:34 -0400 | [diff] [blame] | 1994 | /* read environment from EEPROM */ |
| 1995 | char s[64]; |
| 1996 | getenv_r ("unlock", s, sizeof(s)); |
Michael Schwingen | 73d044d | 2007-12-07 23:35:02 +0100 | [diff] [blame] | 1997 | #endif |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 1998 | |
Becky Bruce | 9d1f6af | 2009-02-02 16:34:51 -0600 | [diff] [blame] | 1999 | #define BANK_BASE(i) (((phys_addr_t [CFI_MAX_FLASH_BANKS])CONFIG_SYS_FLASH_BANKS_LIST)[i]) |
Wolfgang Denk | 9f5fb0f | 2008-08-08 16:39:54 +0200 | [diff] [blame] | 2000 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2001 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2002 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2003 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2004 | |
Wolfgang Denk | 9f5fb0f | 2008-08-08 16:39:54 +0200 | [diff] [blame] | 2005 | if (!flash_detect_legacy (BANK_BASE(i), i)) |
| 2006 | flash_get_size (BANK_BASE(i), i); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2007 | size += flash_info[i].size; |
| 2008 | if (flash_info[i].flash_id == FLASH_UNKNOWN) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2009 | #ifndef CONFIG_SYS_FLASH_QUIET_TEST |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2010 | printf ("## Unknown FLASH on Bank %d " |
| 2011 | "- Size = 0x%08lx = %ld MB\n", |
| 2012 | i+1, flash_info[i].size, |
| 2013 | flash_info[i].size << 20); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2014 | #endif /* CONFIG_SYS_FLASH_QUIET_TEST */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2015 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2016 | #ifdef CONFIG_SYS_FLASH_PROTECTION |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2017 | else if ((s != NULL) && (strcmp(s, "yes") == 0)) { |
| 2018 | /* |
| 2019 | * Only the U-Boot image and it's environment |
| 2020 | * is protected, all other sectors are |
| 2021 | * unprotected (unlocked) if flash hardware |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2022 | * protection is used (CONFIG_SYS_FLASH_PROTECTION) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2023 | * and the environment variable "unlock" is |
| 2024 | * set to "yes". |
| 2025 | */ |
| 2026 | if (flash_info[i].legacy_unlock) { |
| 2027 | int k; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2028 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2029 | /* |
| 2030 | * Disable legacy_unlock temporarily, |
| 2031 | * since flash_real_protect would |
| 2032 | * relock all other sectors again |
| 2033 | * otherwise. |
| 2034 | */ |
| 2035 | flash_info[i].legacy_unlock = 0; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2036 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2037 | /* |
| 2038 | * Legacy unlocking (e.g. Intel J3) -> |
| 2039 | * unlock only one sector. This will |
| 2040 | * unlock all sectors. |
| 2041 | */ |
| 2042 | flash_real_protect (&flash_info[i], 0, 0); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2043 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2044 | flash_info[i].legacy_unlock = 1; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2045 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2046 | /* |
| 2047 | * Manually mark other sectors as |
| 2048 | * unlocked (unprotected) |
| 2049 | */ |
| 2050 | for (k = 1; k < flash_info[i].sector_count; k++) |
| 2051 | flash_info[i].protect[k] = 0; |
| 2052 | } else { |
| 2053 | /* |
| 2054 | * No legancy unlocking -> unlock all sectors |
| 2055 | */ |
| 2056 | flash_protect (FLAG_PROTECT_CLEAR, |
| 2057 | flash_info[i].start[0], |
| 2058 | flash_info[i].start[0] |
| 2059 | + flash_info[i].size - 1, |
| 2060 | &flash_info[i]); |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2061 | } |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2062 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2063 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2064 | } |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2065 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2066 | /* Monitor protection ON by default */ |
Wolfgang Wegner | 0e18d04 | 2010-03-02 10:59:19 +0100 | [diff] [blame] | 2067 | #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \ |
| 2068 | (!defined(CONFIG_MONITOR_IS_IN_RAM)) |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2069 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2070 | CONFIG_SYS_MONITOR_BASE, |
| 2071 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, |
| 2072 | flash_get_info(CONFIG_SYS_MONITOR_BASE)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2073 | #endif |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2074 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2075 | /* Environment protection ON by default */ |
Jean-Christophe PLAGNIOL-VILLARD | 53db4cd | 2008-09-10 22:48:04 +0200 | [diff] [blame] | 2076 | #ifdef CONFIG_ENV_IS_IN_FLASH |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2077 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 2078 | CONFIG_ENV_ADDR, |
| 2079 | CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1, |
| 2080 | flash_get_info(CONFIG_ENV_ADDR)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2081 | #endif |
Stefan Roese | c865e6c | 2006-02-28 15:29:58 +0100 | [diff] [blame] | 2082 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2083 | /* Redundant environment protection ON by default */ |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 2084 | #ifdef CONFIG_ENV_ADDR_REDUND |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2085 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 2086 | CONFIG_ENV_ADDR_REDUND, |
Wolfgang Denk | 4791383 | 2009-05-15 00:16:03 +0200 | [diff] [blame] | 2087 | CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 2088 | flash_get_info(CONFIG_ENV_ADDR_REDUND)); |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2089 | #endif |
Matthias Fuchs | 5043152 | 2008-04-18 16:29:40 +0200 | [diff] [blame] | 2090 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 2091 | #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST) |
Matthias Fuchs | 5043152 | 2008-04-18 16:29:40 +0200 | [diff] [blame] | 2092 | for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) { |
| 2093 | debug("autoprotecting from %08x to %08x\n", |
| 2094 | apl[i].start, apl[i].start + apl[i].size - 1); |
| 2095 | flash_protect (FLAG_PROTECT_SET, |
| 2096 | apl[i].start, |
| 2097 | apl[i].start + apl[i].size - 1, |
| 2098 | flash_get_info(apl[i].start)); |
| 2099 | } |
| 2100 | #endif |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 2101 | |
| 2102 | #ifdef CONFIG_FLASH_CFI_MTD |
| 2103 | cfi_mtd_init(); |
| 2104 | #endif |
| 2105 | |
Haavard Skinnemoen | 6dd763c | 2007-10-06 18:55:36 +0200 | [diff] [blame] | 2106 | return (size); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 2107 | } |