wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com. |
| 4 | * Based on code by: |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Stefan Roese | 247e9d7 | 2010-09-09 19:18:00 +0200 | [diff] [blame] | 11 | #include <asm/ppc4xx.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 12 | #include <asm/processor.h> |
| 13 | |
| 14 | #include <watchdog.h> |
| 15 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 16 | /* info for FLASH chips */ |
| 17 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 18 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 19 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 20 | * Functions |
| 21 | */ |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 22 | static ulong flash_get_size(vu_long *addr, flash_info_t *info); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 23 | static int write_word8(flash_info_t *info, ulong dest, ulong data); |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 24 | static int write_word32(flash_info_t *info, ulong dest, ulong data); |
| 25 | static void flash_get_offsets(ulong base, flash_info_t *info); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 26 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 27 | unsigned long flash_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 28 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 29 | int i; |
| 30 | unsigned long size_b0, base_b0; |
Wolfgang Denk | fe57a28 | 2011-12-07 12:19:26 +0000 | [diff] [blame] | 31 | unsigned long size_b1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 33 | /* Init: no FLASHes known */ |
| 34 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) |
| 35 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 36 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 37 | /* Get Size of Boot and Main Flashes */ |
| 38 | size_b0 = flash_get_size((vu_long *) FLASH_BASE0_PRELIM, |
| 39 | &flash_info[0]); |
| 40 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { |
| 41 | printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
| 42 | size_b0, size_b0 << 20); |
| 43 | return 0; |
| 44 | } |
| 45 | size_b1 = |
| 46 | flash_get_size((vu_long *) FLASH_BASE1_PRELIM, |
| 47 | &flash_info[1]); |
| 48 | if (flash_info[1].flash_id == FLASH_UNKNOWN) { |
| 49 | printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n", |
| 50 | size_b1, size_b1 << 20); |
| 51 | return 0; |
| 52 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 53 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 54 | /* Calculate base addresses */ |
| 55 | base_b0 = -size_b0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 57 | /* Setup offsets for Boot Flash */ |
| 58 | flash_get_offsets(base_b0, &flash_info[0]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 59 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 60 | /* Protect board level data */ |
| 61 | (void) flash_protect(FLAG_PROTECT_SET, |
| 62 | base_b0, |
| 63 | flash_info[0].start[1] - 1, &flash_info[0]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 64 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 65 | /* Monitor protection ON by default */ |
| 66 | (void) flash_protect(FLAG_PROTECT_SET, |
| 67 | base_b0 + size_b0 - monitor_flash_len, |
| 68 | base_b0 + size_b0 - 1, &flash_info[0]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 69 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 70 | /* Protect the FPGA image */ |
| 71 | (void) flash_protect(FLAG_PROTECT_SET, |
| 72 | FLASH_BASE1_PRELIM, |
| 73 | FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN - |
| 74 | 1, &flash_info[1]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 75 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 76 | /* Protect the default boot image */ |
| 77 | (void) flash_protect(FLAG_PROTECT_SET, |
| 78 | FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN, |
| 79 | FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN + |
| 80 | 0x600000 - 1, &flash_info[1]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 81 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 82 | /* Setup offsets for Main Flash */ |
| 83 | flash_get_offsets(FLASH_BASE1_PRELIM, &flash_info[1]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 84 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 85 | return size_b0 + size_b1; |
| 86 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 87 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 88 | static void flash_get_offsets(ulong base, flash_info_t *info) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 89 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 90 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 91 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 92 | /* set up sector start address table - FOR BOOT ROM ONLY!!! */ |
| 93 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) { |
| 94 | for (i = 0; i < info->sector_count; i++) |
| 95 | info->start[i] = base + (i * 0x00010000); |
| 96 | } |
| 97 | } /* end flash_get_offsets() */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 98 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 99 | void flash_print_info(flash_info_t *info) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 101 | int i; |
| 102 | int k; |
| 103 | int size; |
| 104 | int erased; |
| 105 | volatile unsigned long *flash; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 107 | if (info->flash_id == FLASH_UNKNOWN) { |
| 108 | printf("missing or unknown FLASH type\n"); |
| 109 | return; |
| 110 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 112 | switch (info->flash_id & FLASH_VENDMASK) { |
| 113 | case FLASH_MAN_AMD: |
| 114 | printf("1 x AMD "); |
| 115 | break; |
| 116 | case FLASH_MAN_STM: |
| 117 | printf("1 x STM "); |
| 118 | break; |
| 119 | case FLASH_MAN_INTEL: |
| 120 | printf("2 x Intel "); |
| 121 | break; |
| 122 | default: |
| 123 | printf("Unknown Vendor "); |
| 124 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 125 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 126 | switch (info->flash_id & FLASH_TYPEMASK) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 127 | case FLASH_AM040: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 128 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) |
| 129 | printf("AM29LV040 (4096 Kbit, uniform sector size)\n"); |
| 130 | else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM) |
| 131 | printf("M29W040B (4096 Kbit, uniform block size)\n"); |
| 132 | else |
| 133 | printf("UNKNOWN 29x040x (4096 Kbit, uniform sector size)\n"); |
| 134 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 135 | case FLASH_28F320J3A: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 136 | printf("28F320J3A (32 Mbit = 128K x 32)\n"); |
| 137 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 138 | case FLASH_28F640J3A: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 139 | printf("28F640J3A (64 Mbit = 128K x 64)\n"); |
| 140 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 141 | case FLASH_28F128J3A: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 142 | printf("28F128J3A (128 Mbit = 128K x 128)\n"); |
| 143 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 144 | default: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 145 | printf("Unknown Chip Type\n"); |
| 146 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 148 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM) { |
| 149 | printf(" Size: %ld KB in %d Blocks\n", |
| 150 | info->size >> 10, info->sector_count); |
| 151 | } else { |
| 152 | printf(" Size: %ld KB in %d Sectors\n", |
| 153 | info->size >> 10, info->sector_count); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 154 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 156 | printf(" Sector Start Addresses:"); |
| 157 | for (i = 0; i < info->sector_count; ++i) { |
| 158 | /* |
| 159 | * Check if whole sector is erased |
| 160 | */ |
| 161 | if (i != (info->sector_count - 1)) |
| 162 | size = info->start[i + 1] - info->start[i]; |
| 163 | else |
| 164 | size = info->start[0] + info->size - info->start[i]; |
| 165 | erased = 1; |
| 166 | flash = (volatile unsigned long *) info->start[i]; |
| 167 | size = size >> 2; /* divide by 4 for longword access */ |
| 168 | for (k = 0; k < size; k++) { |
| 169 | if (*flash++ != 0xffffffff) { |
| 170 | erased = 0; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if ((i % 5) == 0) |
| 176 | printf("\n "); |
| 177 | printf(" %08lX%s%s", |
| 178 | info->start[i], |
| 179 | erased ? " E" : " ", |
| 180 | info->protect[i] ? "RO " : " "); |
| 181 | } |
| 182 | printf("\n"); |
| 183 | } /* end flash_print_info() */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * The following code cannot be run from FLASH! |
| 187 | */ |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 188 | static ulong flash_get_size(vu_long *addr, flash_info_t *info) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 189 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 190 | short i; |
| 191 | ulong base = (ulong) addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 192 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 193 | /* Setup default type */ |
| 194 | info->flash_id = FLASH_UNKNOWN; |
| 195 | info->sector_count = 0; |
| 196 | info->size = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 197 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 198 | /* Test for Boot Flash */ |
| 199 | if (base == FLASH_BASE0_PRELIM) { |
| 200 | unsigned char value; |
| 201 | volatile unsigned char *addr2 = (unsigned char *) addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 202 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 203 | /* Write auto select command: read Manufacturer ID */ |
| 204 | *(addr2 + 0x555) = 0xaa; |
| 205 | *(addr2 + 0x2aa) = 0x55; |
| 206 | *(addr2 + 0x555) = 0x90; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 207 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 208 | /* Manufacture ID */ |
| 209 | value = *addr2; |
| 210 | switch (value) { |
| 211 | case (unsigned char) AMD_MANUFACT: |
| 212 | info->flash_id = FLASH_MAN_AMD; |
| 213 | break; |
| 214 | case (unsigned char) STM_MANUFACT: |
| 215 | info->flash_id = FLASH_MAN_STM; |
| 216 | break; |
| 217 | default: |
| 218 | *addr2 = 0xf0; /* no or unknown flash */ |
| 219 | return 0; |
| 220 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 221 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 222 | /* Device ID */ |
| 223 | value = *(addr2 + 1); |
| 224 | switch (value) { |
| 225 | case (unsigned char) AMD_ID_LV040B: |
| 226 | case (unsigned char) STM_ID_29W040B: |
| 227 | info->flash_id += FLASH_AM040; |
| 228 | info->sector_count = 8; |
| 229 | info->size = 0x00080000; |
| 230 | break; /* => 512Kb */ |
| 231 | default: |
| 232 | *addr2 = 0xf0; /* => no or unknown flash */ |
| 233 | return 0; |
| 234 | } |
| 235 | } else { /* MAIN Flash */ |
| 236 | unsigned long value; |
| 237 | volatile unsigned long *addr2 = (unsigned long *) addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 239 | /* Write auto select command: read Manufacturer ID */ |
| 240 | *addr2 = 0x90909090; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 242 | /* Manufacture ID */ |
| 243 | value = *addr2; |
| 244 | switch (value) { |
| 245 | case (unsigned long) INTEL_MANUFACT: |
| 246 | info->flash_id = FLASH_MAN_INTEL; |
| 247 | break; |
| 248 | default: |
| 249 | *addr2 = 0xff; /* no or unknown flash */ |
| 250 | return 0; |
| 251 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 252 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 253 | /* Device ID - This shit is interleaved... */ |
| 254 | value = *(addr2 + 1); |
| 255 | switch (value) { |
| 256 | case (unsigned long) INTEL_ID_28F320J3A: |
| 257 | info->flash_id += FLASH_28F320J3A; |
| 258 | info->sector_count = 32; |
| 259 | info->size = 0x00400000 * 2; |
| 260 | break; /* => 2 X 4 MB */ |
| 261 | case (unsigned long) INTEL_ID_28F640J3A: |
| 262 | info->flash_id += FLASH_28F640J3A; |
| 263 | info->sector_count = 64; |
| 264 | info->size = 0x00800000 * 2; |
| 265 | break; /* => 2 X 8 MB */ |
| 266 | case (unsigned long) INTEL_ID_28F128J3A: |
| 267 | info->flash_id += FLASH_28F128J3A; |
| 268 | info->sector_count = 128; |
| 269 | info->size = 0x01000000 * 2; |
| 270 | break; /* => 2 X 16 MB */ |
| 271 | default: |
| 272 | *addr2 = 0xff; /* => no or unknown flash */ |
| 273 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 274 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 275 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 276 | /* Make sure we don't exceed CONFIG_SYS_MAX_FLASH_SECT */ |
| 277 | if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) { |
| 278 | printf("** ERROR: sector count %d > max (%d) **\n", |
| 279 | info->sector_count, CONFIG_SYS_MAX_FLASH_SECT); |
| 280 | info->sector_count = CONFIG_SYS_MAX_FLASH_SECT; |
| 281 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 282 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 283 | /* set up sector start address table */ |
| 284 | switch (info->flash_id & FLASH_TYPEMASK) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 285 | case FLASH_AM040: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 286 | for (i = 0; i < info->sector_count; i++) |
| 287 | info->start[i] = base + (i * 0x00010000); |
| 288 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 289 | case FLASH_28F320J3A: |
| 290 | case FLASH_28F640J3A: |
| 291 | case FLASH_28F128J3A: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 292 | for (i = 0; i < info->sector_count; i++) |
| 293 | info->start[i] = base + |
| 294 | (i * 0x00020000 * 2); /* 2 Banks */ |
| 295 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 296 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 297 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 298 | /* Test for Boot Flash */ |
| 299 | if (base == FLASH_BASE0_PRELIM) { |
| 300 | volatile unsigned char *addr2; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 301 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 302 | /* check for protected sectors */ |
| 303 | for (i = 0; i < info->sector_count; i++) { |
| 304 | /* |
| 305 | * read sector protection at sector address, |
| 306 | * (AX .. A0) = 0x02 |
| 307 | * D0 = 1 if protected |
| 308 | */ |
| 309 | addr2 = (volatile unsigned char *) (info->start[i]); |
| 310 | info->protect[i] = *(addr2 + 2) & 1; |
| 311 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 312 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 313 | /* Restore read mode */ |
| 314 | *(unsigned char *) base = 0xF0; /* Reset NORMAL Flash */ |
| 315 | } else { /* Main Flash */ |
| 316 | volatile unsigned long *addr2; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 317 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 318 | /* check for protected sectors */ |
| 319 | for (i = 0; i < info->sector_count; i++) { |
| 320 | /* |
| 321 | * read sector protection at sector address, |
| 322 | * (AX .. A0) = 0x02 |
| 323 | * D0 = 1 if protected |
| 324 | */ |
| 325 | addr2 = (volatile unsigned long *) (info->start[i]); |
| 326 | info->protect[i] = *(addr2 + 2) & 0x1; |
| 327 | } |
| 328 | |
| 329 | /* Restore read mode */ |
| 330 | *(unsigned long *) base = 0xFFFFFFFF; /* Reset Flash */ |
| 331 | } |
| 332 | |
| 333 | return info->size; |
| 334 | } /* end flash_get_size() */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 335 | |
| 336 | static int wait_for_DQ7(ulong addr, uchar cmp_val, ulong tout) |
| 337 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 338 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 339 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 340 | volatile uchar *vaddr = (uchar *) addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 341 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 342 | /* Loop X times */ |
| 343 | for (i = 1; i <= (100 * tout); i++) { /* Wait up to tout ms */ |
| 344 | udelay(10); |
| 345 | /* Pause 10 us */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 346 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 347 | /* Check for completion */ |
| 348 | if ((vaddr[0] & 0x80) == (cmp_val & 0x80)) |
| 349 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 350 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 351 | /* KEEP THE LUSER HAPPY - Print a dot every 1.1 seconds */ |
| 352 | if (!(i % 110000)) |
| 353 | putc('.'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 354 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 355 | /* Kick the dog if needed */ |
| 356 | WATCHDOG_RESET(); |
| 357 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 358 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 359 | return 1; |
| 360 | } /* wait_for_DQ7() */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 361 | |
| 362 | static int flash_erase8(flash_info_t *info, int s_first, int s_last) |
| 363 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 364 | int tcode, rcode = 0; |
| 365 | volatile uchar *addr = (uchar *) (info->start[0]); |
| 366 | volatile uchar *sector_addr; |
| 367 | int flag, prot, sect; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 368 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 369 | /* Validate arguments */ |
| 370 | if ((s_first < 0) || (s_first > s_last)) { |
| 371 | if (info->flash_id == FLASH_UNKNOWN) |
| 372 | printf("- missing\n"); |
| 373 | else |
| 374 | printf("- no sectors to erase\n"); |
| 375 | return 1; |
| 376 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 377 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 378 | /* Check for KNOWN flash type */ |
| 379 | if (info->flash_id == FLASH_UNKNOWN) { |
| 380 | printf("Can't erase unknown flash type - aborted\n"); |
| 381 | return 1; |
| 382 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 383 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 384 | /* Check for protected sectors */ |
| 385 | prot = 0; |
| 386 | for (sect = s_first; sect <= s_last; ++sect) { |
| 387 | if (info->protect[sect]) |
| 388 | prot++; |
| 389 | } |
| 390 | if (prot) { |
| 391 | printf("- Warning: %d protected sectors will not be erased!\n", |
| 392 | prot); |
| 393 | } else { |
| 394 | printf("\n"); |
| 395 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 396 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 397 | /* Start erase on unprotected sectors */ |
| 398 | for (sect = s_first; sect <= s_last; sect++) { |
| 399 | if (info->protect[sect] == 0) { /* not protected */ |
| 400 | sector_addr = (uchar *) (info->start[sect]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 401 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 402 | if ((info->flash_id & FLASH_VENDMASK) == |
| 403 | FLASH_MAN_STM) |
| 404 | printf("Erasing block %p\n", sector_addr); |
| 405 | else |
| 406 | printf("Erasing sector %p\n", sector_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 407 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 408 | /* Disable interrupts which might cause timeout */ |
| 409 | flag = disable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 410 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 411 | *(addr + 0x555) = (uchar) 0xAA; |
| 412 | *(addr + 0x2aa) = (uchar) 0x55; |
| 413 | *(addr + 0x555) = (uchar) 0x80; |
| 414 | *(addr + 0x555) = (uchar) 0xAA; |
| 415 | *(addr + 0x2aa) = (uchar) 0x55; |
| 416 | *sector_addr = (uchar) 0x30; /* sector erase */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 417 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 418 | /* |
| 419 | * Wait for each sector to complete, it's more |
| 420 | * reliable. According to AMD Spec, you must |
| 421 | * issue all erase commands within a specified |
| 422 | * timeout. This has been seen to fail, especially |
| 423 | * if printf()s are included (for debug)!! |
| 424 | * Takes up to 6 seconds. |
| 425 | */ |
| 426 | tcode = wait_for_DQ7((ulong) sector_addr, 0x80, 6000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 427 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 428 | /* re-enable interrupts if necessary */ |
| 429 | if (flag) |
| 430 | enable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 431 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 432 | /* Make sure we didn't timeout */ |
| 433 | if (tcode) { |
| 434 | printf("Timeout\n"); |
| 435 | rcode = 1; |
| 436 | } |
| 437 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 438 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 439 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 440 | /* wait at least 80us - let's wait 1 ms */ |
| 441 | udelay(1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 442 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 443 | /* reset to read mode */ |
| 444 | addr = (uchar *) info->start[0]; |
| 445 | *addr = (uchar) 0xF0; /* reset bank */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 446 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 447 | printf(" done\n"); |
| 448 | return rcode; |
| 449 | } /* end flash_erase8() */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 450 | |
| 451 | static int flash_erase32(flash_info_t *info, int s_first, int s_last) |
| 452 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 453 | int flag, sect; |
| 454 | ulong start, now, last; |
| 455 | int prot = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 456 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 457 | /* Validate arguments */ |
| 458 | if ((s_first < 0) || (s_first > s_last)) { |
| 459 | if (info->flash_id == FLASH_UNKNOWN) |
| 460 | printf("- missing\n"); |
| 461 | else |
| 462 | printf("- no sectors to erase\n"); |
| 463 | return 1; |
| 464 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 465 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 466 | /* Check for KNOWN flash type */ |
| 467 | if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) { |
| 468 | printf("Can erase only Intel flash types - aborted\n"); |
| 469 | return 1; |
| 470 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 471 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 472 | /* Check for protected sectors */ |
| 473 | for (sect = s_first; sect <= s_last; ++sect) { |
| 474 | if (info->protect[sect]) |
| 475 | prot++; |
| 476 | } |
| 477 | if (prot) { |
| 478 | printf("- Warning: %d protected sectors will not be erased!\n", |
| 479 | prot); |
| 480 | } else { |
| 481 | printf("\n"); |
| 482 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 483 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 484 | start = get_timer(0); |
| 485 | last = start; |
| 486 | /* Start erase on unprotected sectors */ |
| 487 | for (sect = s_first; sect <= s_last; sect++) { |
| 488 | WATCHDOG_RESET(); |
| 489 | if (info->protect[sect] == 0) { /* not protected */ |
| 490 | vu_long *addr = (vu_long *) (info->start[sect]); |
| 491 | unsigned long status; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 492 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 493 | /* Disable interrupts which might cause a timeout */ |
| 494 | flag = disable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 495 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 496 | *addr = 0x00500050; /* clear status register */ |
| 497 | *addr = 0x00200020; /* erase setup */ |
| 498 | *addr = 0x00D000D0; /* erase confirm */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 499 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 500 | /* re-enable interrupts if necessary */ |
| 501 | if (flag) |
| 502 | enable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 503 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 504 | /* Wait at least 80us - let's wait 1 ms */ |
| 505 | udelay(1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 506 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 507 | while (((status = *addr) & 0x00800080) != 0x00800080) { |
| 508 | now = get_timer(start); |
| 509 | if (now > CONFIG_SYS_FLASH_ERASE_TOUT) { |
| 510 | printf("Timeout\n"); |
| 511 | /* suspend erase */ |
| 512 | *addr = 0x00B000B0; |
| 513 | /* reset to read mode */ |
| 514 | *addr = 0x00FF00FF; |
| 515 | return 1; |
| 516 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 517 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 518 | /* |
| 519 | * show that we're waiting |
| 520 | * every second (?) |
| 521 | */ |
| 522 | if ((now - last) > 990) { |
| 523 | putc('.'); |
| 524 | last = now; |
| 525 | } |
| 526 | } |
| 527 | *addr = 0x00FF00FF; /* reset to read mode */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 528 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 529 | } |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 530 | printf(" done\n"); |
| 531 | return 0; |
| 532 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 533 | |
| 534 | int flash_erase(flash_info_t *info, int s_first, int s_last) |
| 535 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 536 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) |
| 537 | return flash_erase8(info, s_first, s_last); |
| 538 | else |
| 539 | return flash_erase32(info, s_first, s_last); |
| 540 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 541 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 542 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 543 | * Copy memory to flash, returns: |
| 544 | * 0 - OK |
| 545 | * 1 - write timeout |
| 546 | * 2 - Flash not erased |
| 547 | */ |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 548 | static int write_buff8(flash_info_t *info, uchar *src, ulong addr, |
| 549 | ulong cnt) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 550 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 551 | ulong cp, wp, data; |
| 552 | ulong start; |
| 553 | int i, l, rc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 554 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 555 | start = get_timer(0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 556 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 557 | wp = (addr & ~3); /* get lower word |
| 558 | aligned address */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 559 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 560 | /* |
| 561 | * handle unaligned start bytes |
| 562 | */ |
| 563 | l = addr - wp; |
| 564 | if (l != 0) { |
| 565 | data = 0; |
| 566 | for (i = 0, cp = wp; i < l; ++i, ++cp) |
| 567 | data = (data << 8) | (*(uchar *) cp); |
| 568 | |
| 569 | for (; i < 4 && cnt > 0; ++i) { |
| 570 | data = (data << 8) | *src++; |
| 571 | --cnt; |
| 572 | ++cp; |
| 573 | } |
| 574 | |
| 575 | for (; cnt == 0 && i < 4; ++i, ++cp) |
| 576 | data = (data << 8) | (*(uchar *) cp); |
| 577 | |
| 578 | rc = write_word8(info, wp, data); |
| 579 | if (rc != 0) |
| 580 | return rc; |
| 581 | |
| 582 | wp += 4; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 583 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 584 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 585 | /* |
| 586 | * handle word aligned part |
| 587 | */ |
| 588 | while (cnt >= 4) { |
| 589 | data = 0; |
| 590 | for (i = 0; i < 4; ++i) |
| 591 | data = (data << 8) | *src++; |
| 592 | |
| 593 | rc = write_word8(info, wp, data); |
| 594 | if (rc != 0) |
| 595 | return rc; |
| 596 | |
| 597 | wp += 4; |
| 598 | cnt -= 4; |
| 599 | if (get_timer(start) > 1000) { /* every second */ |
| 600 | WATCHDOG_RESET(); |
| 601 | putc('.'); |
| 602 | start = get_timer(0); |
| 603 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 604 | } |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 605 | |
| 606 | if (cnt == 0) |
| 607 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 608 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 609 | /* |
| 610 | * handle unaligned tail bytes |
| 611 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 612 | data = 0; |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 613 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
| 614 | data = (data << 8) | *src++; |
| 615 | --cnt; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 616 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 617 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 618 | for (; i < 4; ++i, ++cp) |
| 619 | data = (data << 8) | (*(uchar *) cp); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 620 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 621 | return write_word8(info, wp, data); |
| 622 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 623 | |
| 624 | #define FLASH_WIDTH 4 /* flash bus width in bytes */ |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 625 | static int write_buff32(flash_info_t *info, uchar *src, ulong addr, |
| 626 | ulong cnt) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 627 | { |
| 628 | ulong cp, wp, data; |
| 629 | int i, l, rc; |
| 630 | ulong start; |
| 631 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 632 | start = get_timer(0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 633 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 634 | if (info->flash_id == FLASH_UNKNOWN) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 635 | return 4; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 636 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 637 | /* get lower FLASH_WIDTH aligned address */ |
| 638 | wp = (addr & ~(FLASH_WIDTH - 1)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * handle unaligned start bytes |
| 642 | */ |
| 643 | if ((l = addr - wp) != 0) { |
| 644 | data = 0; |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 645 | for (i = 0, cp = wp; i < l; ++i, ++cp) |
| 646 | data = (data << 8) | (*(uchar *) cp); |
| 647 | |
| 648 | for (; i < FLASH_WIDTH && cnt > 0; ++i) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 649 | data = (data << 8) | *src++; |
| 650 | --cnt; |
| 651 | ++cp; |
| 652 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 653 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 654 | for (; cnt == 0 && i < FLASH_WIDTH; ++i, ++cp) |
| 655 | data = (data << 8) | (*(uchar *) cp); |
| 656 | |
| 657 | rc = write_word32(info, wp, data); |
| 658 | if (rc != 0) |
| 659 | return rc; |
| 660 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 661 | wp += FLASH_WIDTH; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * handle FLASH_WIDTH aligned part |
| 666 | */ |
| 667 | while (cnt >= FLASH_WIDTH) { |
| 668 | data = 0; |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 669 | for (i = 0; i < FLASH_WIDTH; ++i) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 670 | data = (data << 8) | *src++; |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 671 | |
| 672 | rc = write_word32(info, wp, data); |
| 673 | if (rc != 0) |
| 674 | return rc; |
| 675 | |
| 676 | wp += FLASH_WIDTH; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 677 | cnt -= FLASH_WIDTH; |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 678 | if (get_timer(start) > 990) { /* every second */ |
| 679 | putc('.'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 680 | start = get_timer(0); |
| 681 | } |
| 682 | } |
| 683 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 684 | if (cnt == 0) |
| 685 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 686 | |
| 687 | /* |
| 688 | * handle unaligned tail bytes |
| 689 | */ |
| 690 | data = 0; |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 691 | for (i = 0, cp = wp; i < FLASH_WIDTH && cnt > 0; ++i, ++cp) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 692 | data = (data << 8) | *src++; |
| 693 | --cnt; |
| 694 | } |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 695 | |
| 696 | for (; i < FLASH_WIDTH; ++i, ++cp) |
| 697 | data = (data << 8) | (*(uchar *) cp); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 698 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 699 | return write_word32(info, wp, data); |
| 700 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 701 | |
| 702 | int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 703 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 704 | int retval; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 705 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 706 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) |
| 707 | retval = write_buff8(info, src, addr, cnt); |
| 708 | else |
| 709 | retval = write_buff32(info, src, addr, cnt); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 710 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 711 | return retval; |
| 712 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 713 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 714 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 715 | * Write a word to Flash, returns: |
| 716 | * 0 - OK |
| 717 | * 1 - write timeout |
| 718 | * 2 - Flash not erased |
| 719 | */ |
| 720 | |
| 721 | static int write_word8(flash_info_t *info, ulong dest, ulong data) |
| 722 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 723 | volatile uchar *addr2 = (uchar *) (info->start[0]); |
| 724 | volatile uchar *dest2 = (uchar *) dest; |
| 725 | volatile uchar *data2 = (uchar *) &data; |
| 726 | int flag; |
| 727 | int i, tcode, rcode = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 728 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 729 | /* Check if Flash is (sufficently) erased */ |
| 730 | if ((*((volatile uchar *)dest) & (uchar)data) != (uchar)data) |
| 731 | return 2; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 732 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 733 | for (i = 0; i < (4 / sizeof(uchar)); i++) { |
| 734 | /* Disable interrupts which might cause a timeout here */ |
| 735 | flag = disable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 736 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 737 | *(addr2 + 0x555) = (uchar) 0xAA; |
| 738 | *(addr2 + 0x2aa) = (uchar) 0x55; |
| 739 | *(addr2 + 0x555) = (uchar) 0xA0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 740 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 741 | dest2[i] = data2[i]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 742 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 743 | /* Wait for write to complete, up to 1ms */ |
| 744 | tcode = wait_for_DQ7((ulong) &dest2[i], data2[i], 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 745 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 746 | /* re-enable interrupts if necessary */ |
| 747 | if (flag) |
| 748 | enable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 749 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 750 | /* Make sure we didn't timeout */ |
| 751 | if (tcode) |
| 752 | rcode = 1; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 753 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 754 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 755 | return rcode; |
| 756 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 757 | |
| 758 | static int write_word32(flash_info_t *info, ulong dest, ulong data) |
| 759 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 760 | vu_long *addr = (vu_long *) dest; |
| 761 | ulong status; |
| 762 | ulong start; |
| 763 | int flag; |
| 764 | |
| 765 | /* Check if Flash is (sufficiently) erased */ |
| 766 | if ((*addr & data) != data) |
| 767 | return 2; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 768 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 769 | /* Disable interrupts which might cause a timeout here */ |
| 770 | flag = disable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 771 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 772 | *addr = 0x00400040; /* write setup */ |
| 773 | *addr = data; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 774 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 775 | /* re-enable interrupts if necessary */ |
| 776 | if (flag) |
| 777 | enable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 778 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 779 | start = get_timer(0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 780 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 781 | while (((status = *addr) & 0x00800080) != 0x00800080) { |
| 782 | WATCHDOG_RESET(); |
| 783 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
| 784 | *addr = 0x00FF00FF; /* restore read mode */ |
| 785 | return 1; |
| 786 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 787 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 788 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 789 | *addr = 0x00FF00FF; /* restore read mode */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 790 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 791 | return 0; |
| 792 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 793 | |
| 794 | static int _flash_protect(flash_info_t *info, long sector) |
| 795 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 796 | int i; |
| 797 | int flag; |
| 798 | ulong status; |
| 799 | int rcode = 0; |
| 800 | volatile long *addr = (long *)sector; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 801 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 802 | switch (info->flash_id & FLASH_TYPEMASK) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 803 | case FLASH_28F320J3A: |
| 804 | case FLASH_28F640J3A: |
| 805 | case FLASH_28F128J3A: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 806 | /* Disable interrupts which might cause Flash to timeout */ |
| 807 | flag = disable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 808 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 809 | /* Issue command */ |
| 810 | *addr = 0x00500050L; /* Clear the status register */ |
| 811 | *addr = 0x00600060L; /* Set lock bit setup */ |
| 812 | *addr = 0x00010001L; /* Set lock bit confirm */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 813 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 814 | /* Wait for command completion */ |
| 815 | for (i = 0; i < 10; i++) { /* 75us timeout, wait 100us */ |
| 816 | udelay(10); |
| 817 | if ((*addr & 0x00800080L) == 0x00800080L) |
| 818 | break; |
| 819 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 820 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 821 | /* Not successful? */ |
| 822 | status = *addr; |
| 823 | if (status != 0x00800080L) { |
| 824 | printf("Protect %x sector failed: %x\n", |
| 825 | (uint) sector, (uint) status); |
| 826 | rcode = 1; |
| 827 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 828 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 829 | /* Restore read mode */ |
| 830 | *addr = 0x00ff00ffL; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 831 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 832 | /* re-enable interrupts if necessary */ |
| 833 | if (flag) |
| 834 | enable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 835 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 836 | break; |
| 837 | case FLASH_AM040: /* No soft sector protection */ |
| 838 | break; |
| 839 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 840 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 841 | /* Turn protection on for this sector */ |
| 842 | for (i = 0; i < info->sector_count; i++) { |
| 843 | if (info->start[i] == sector) { |
| 844 | info->protect[i] = 1; |
| 845 | break; |
| 846 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 847 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 848 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 849 | return rcode; |
| 850 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 851 | |
| 852 | static int _flash_unprotect(flash_info_t *info, long sector) |
| 853 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 854 | int i; |
| 855 | int flag; |
| 856 | ulong status; |
| 857 | int rcode = 0; |
| 858 | volatile long *addr = (long *) sector; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 859 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 860 | switch (info->flash_id & FLASH_TYPEMASK) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 861 | case FLASH_28F320J3A: |
| 862 | case FLASH_28F640J3A: |
| 863 | case FLASH_28F128J3A: |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 864 | /* Disable interrupts which might cause Flash to timeout */ |
| 865 | flag = disable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 866 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 867 | *addr = 0x00500050L; /* Clear the status register */ |
| 868 | *addr = 0x00600060L; /* Clear lock bit setup */ |
| 869 | *addr = 0x00D000D0L; /* Clear lock bit confirm */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 870 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 871 | /* Wait for command completion */ |
| 872 | for (i = 0; i < 80; i++) { /* 700ms timeout, wait 800 */ |
| 873 | udelay(10000); /* Delay 10ms */ |
| 874 | if ((*addr & 0x00800080L) == 0x00800080L) |
| 875 | break; |
| 876 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 877 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 878 | /* Not successful? */ |
| 879 | status = *addr; |
| 880 | if (status != 0x00800080L) { |
| 881 | printf("Un-protect %x sector failed: %x\n", |
| 882 | (uint) sector, (uint) status); |
| 883 | *addr = 0x00ff00ffL; |
| 884 | rcode = 1; |
| 885 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 886 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 887 | /* restore read mode */ |
| 888 | *addr = 0x00ff00ffL; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 889 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 890 | /* re-enable interrupts if necessary */ |
| 891 | if (flag) |
| 892 | enable_interrupts(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 893 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 894 | break; |
| 895 | case FLASH_AM040: /* No soft sector protection */ |
| 896 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 897 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 898 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 899 | /* |
| 900 | * Fix Intel's little red wagon. Reprotect |
| 901 | * sectors that were protected before we undid |
| 902 | * protection on a specific sector. |
| 903 | */ |
| 904 | for (i = 0; i < info->sector_count; i++) { |
| 905 | if (info->start[i] != sector) { |
| 906 | if (info->protect[i]) { |
| 907 | if (_flash_protect(info, info->start[i])) |
| 908 | rcode = 1; |
| 909 | } |
| 910 | } else /* Turn protection off for this sector */ |
| 911 | info->protect[i] = 0; |
| 912 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 913 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 914 | return rcode; |
| 915 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 916 | |
| 917 | int flash_real_protect(flash_info_t *info, long sector, int prot) |
| 918 | { |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 919 | int rcode; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 920 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 921 | if (prot) |
| 922 | rcode = _flash_protect(info, info->start[sector]); |
| 923 | else |
| 924 | rcode = _flash_unprotect(info, info->start[sector]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 925 | |
Wolfgang Denk | ce3c218 | 2011-12-07 12:19:25 +0000 | [diff] [blame] | 926 | return rcode; |
| 927 | } |