wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <mpc8xx.h> |
| 26 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 27 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 28 | |
| 29 | #if defined(CFG_ENV_IS_IN_FLASH) |
| 30 | # ifndef CFG_ENV_ADDR |
| 31 | # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET) |
| 32 | # endif |
| 33 | # ifndef CFG_ENV_SIZE |
| 34 | # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE |
| 35 | # endif |
| 36 | # ifndef CFG_ENV_SECT_SIZE |
| 37 | # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE |
| 38 | # endif |
| 39 | #endif |
| 40 | |
| 41 | /*----------------------------------------------------------------------- |
| 42 | * Functions |
| 43 | */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 44 | static ulong flash_get_size (vu_long * addr, flash_info_t * info); |
| 45 | static int write_word (flash_info_t * info, ulong dest, ulong data); |
| 46 | static void flash_get_offsets (ulong base, flash_info_t * info); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 47 | |
| 48 | /*----------------------------------------------------------------------- |
| 49 | */ |
| 50 | |
| 51 | unsigned long flash_init (void) |
| 52 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 53 | volatile immap_t *immap = (immap_t *) CFG_IMMR; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 54 | volatile memctl8xx_t *memctl = &immap->im_memctl; |
| 55 | unsigned long total_size; |
| 56 | unsigned long size_b0, size_b1; |
| 57 | int i; |
| 58 | |
| 59 | /* Init: no FLASHes known */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 60 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 62 | } |
| 63 | |
| 64 | total_size = 0; |
| 65 | size_b0 = 0xffffffff; |
| 66 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 67 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) { |
| 68 | size_b1 = |
| 69 | flash_get_size ((vu_long *) (CFG_FLASH_BASE + |
| 70 | total_size), |
| 71 | &flash_info[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 72 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 73 | if (flash_info[i].flash_id == FLASH_UNKNOWN) { |
| 74 | printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n", i, size_b1, size_b1 >> 20); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* Is this really needed ? - LP */ |
| 78 | if (size_b1 > size_b0) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 79 | printf ("## ERROR: Bank %d (0x%08lx = %ld MB) > Bank %d (0x%08lx = %ld MB)\n", i, size_b1, size_b1 >> 20, i - 1, size_b0, size_b0 >> 20); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 80 | goto out_error; |
| 81 | } |
| 82 | size_b0 = size_b1; |
| 83 | total_size += size_b1; |
| 84 | } |
| 85 | |
| 86 | /* Compute the Address Mask */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 87 | for (i = 0; (total_size >> i) != 0; ++i) { |
| 88 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 89 | i--; |
| 90 | |
| 91 | if (total_size != (1 << i)) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 92 | printf ("## WARNING: Total FLASH size (0x%08lx = %ld MB) is not a power of 2\n", total_size, total_size >> 20); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* Remap FLASH according to real size */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 96 | memctl->memc_or0 = |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame^] | 97 | ((((unsigned long) ~0) << i) & OR_AM_MSK) | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 98 | CFG_OR_TIMING_FLASH; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 99 | memctl->memc_br0 = CFG_BR0_PRELIM; |
| 100 | |
| 101 | total_size = 0; |
| 102 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 103 | for (i = 0; i < CFG_MAX_FLASH_BANKS && flash_info[i].size != 0; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 104 | /* Re-do sizing to get full correct info */ |
| 105 | /* Why ? - LP */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 106 | size_b1 = |
| 107 | flash_get_size ((vu_long *) (CFG_FLASH_BASE + |
| 108 | total_size), |
| 109 | &flash_info[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 110 | |
| 111 | /* This is done by flash_get_size - LP */ |
| 112 | /* flash_get_offsets (CFG_FLASH_BASE + total_size, &flash_info[i]); */ |
| 113 | |
| 114 | #if CFG_MONITOR_BASE >= CFG_FLASH_BASE |
| 115 | /* monitor protection ON by default */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 116 | flash_protect (FLAG_PROTECT_SET, |
| 117 | CFG_MONITOR_BASE, |
| 118 | CFG_MONITOR_BASE + monitor_flash_len - 1, |
| 119 | &flash_info[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | #ifdef CFG_ENV_IS_IN_FLASH |
| 123 | /* ENV protection ON by default */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 124 | flash_protect (FLAG_PROTECT_SET, |
| 125 | CFG_ENV_ADDR, |
| 126 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, |
| 127 | &flash_info[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
| 130 | total_size += size_b1; |
| 131 | } |
| 132 | |
| 133 | return (total_size); |
| 134 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 135 | out_error: |
| 136 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) { |
| 137 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 138 | flash_info[i].sector_count = -1; |
| 139 | flash_info[i].size = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return (0); |
| 143 | } |
| 144 | |
| 145 | /*----------------------------------------------------------------------- |
| 146 | */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 147 | static void flash_get_offsets (ulong base, flash_info_t * info) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 148 | { |
| 149 | int i; |
| 150 | |
| 151 | /* set up sector start address table */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 152 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040 |
| 153 | || (info->flash_id & FLASH_TYPEMASK) == FLASH_AM080) { |
| 154 | /* set sector offsets for uniform sector type */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 155 | for (i = 0; i < info->sector_count; i++) { |
| 156 | info->start[i] = base + (i * 0x00040000); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /*----------------------------------------------------------------------- |
| 162 | */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 163 | void flash_print_info (flash_info_t * info) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 164 | { |
| 165 | int i; |
| 166 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 167 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 168 | printf ("missing or unknown FLASH type\n"); |
| 169 | return; |
| 170 | } |
| 171 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 172 | switch (info->flash_id & FLASH_VENDMASK) { |
| 173 | case FLASH_MAN_AMD: |
| 174 | printf ("AMD "); |
| 175 | break; |
| 176 | case FLASH_MAN_FUJ: |
| 177 | printf ("FUJITSU "); |
| 178 | break; |
| 179 | case FLASH_MAN_BM: |
| 180 | printf ("BRIGHT MICRO "); |
| 181 | break; |
| 182 | default: |
| 183 | printf ("Unknown Vendor "); |
| 184 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 185 | } |
| 186 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 187 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 188 | case FLASH_AM040: |
| 189 | printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n"); |
| 190 | break; |
| 191 | case FLASH_AM080: |
| 192 | printf ("29F080 or 29LV080 (8 Mbit, uniform sectors)\n"); |
| 193 | break; |
| 194 | case FLASH_AM400B: |
| 195 | printf ("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 196 | break; |
| 197 | case FLASH_AM400T: |
| 198 | printf ("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 199 | break; |
| 200 | case FLASH_AM800B: |
| 201 | printf ("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 202 | break; |
| 203 | case FLASH_AM800T: |
| 204 | printf ("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 205 | break; |
| 206 | case FLASH_AM160B: |
| 207 | printf ("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 208 | break; |
| 209 | case FLASH_AM160T: |
| 210 | printf ("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 211 | break; |
| 212 | case FLASH_AM320B: |
| 213 | printf ("AM29LV320B (32 Mbit, bottom boot sect)\n"); |
| 214 | break; |
| 215 | case FLASH_AM320T: |
| 216 | printf ("AM29LV320T (32 Mbit, top boot sector)\n"); |
| 217 | break; |
| 218 | default: |
| 219 | printf ("Unknown Chip Type\n"); |
| 220 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 221 | } |
| 222 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 223 | printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, |
| 224 | info->sector_count); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 225 | |
| 226 | printf (" Sector Start Addresses:"); |
| 227 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 228 | for (i = 0; i < info->sector_count; ++i) { |
| 229 | if ((i % 5) == 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 230 | printf ("\n "); |
| 231 | } |
| 232 | |
| 233 | printf (" %08lX%s", |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 234 | info->start[i], info->protect[i] ? " (RO)" : " "); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | printf ("\n"); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | /*----------------------------------------------------------------------- |
| 242 | */ |
| 243 | |
| 244 | |
| 245 | /*----------------------------------------------------------------------- |
| 246 | */ |
| 247 | |
| 248 | /* |
| 249 | * The following code cannot be run from FLASH! |
| 250 | */ |
| 251 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 252 | static ulong flash_get_size (vu_long * addr, flash_info_t * info) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 253 | { |
| 254 | short i; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 255 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 256 | #if 0 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 257 | ulong base = (ulong) addr; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 258 | #endif |
| 259 | uchar value; |
| 260 | |
| 261 | /* Write auto select command: read Manufacturer ID */ |
| 262 | #if 0 |
| 263 | addr[0x0555] = 0x00AA00AA; |
| 264 | addr[0x02AA] = 0x00550055; |
| 265 | addr[0x0555] = 0x00900090; |
| 266 | #else |
| 267 | addr[0x0555] = 0xAAAAAAAA; |
| 268 | addr[0x02AA] = 0x55555555; |
| 269 | addr[0x0555] = 0x90909090; |
| 270 | #endif |
| 271 | |
| 272 | value = addr[0]; |
| 273 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 274 | switch (value + (value << 16)) { |
| 275 | case AMD_MANUFACT: |
| 276 | info->flash_id = FLASH_MAN_AMD; |
| 277 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 278 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 279 | case FUJ_MANUFACT: |
| 280 | info->flash_id = FLASH_MAN_FUJ; |
| 281 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 282 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 283 | default: |
| 284 | info->flash_id = FLASH_UNKNOWN; |
| 285 | info->sector_count = 0; |
| 286 | info->size = 0; |
| 287 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 288 | } |
| 289 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 290 | value = addr[1]; /* device ID */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 291 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 292 | switch (value) { |
| 293 | case AMD_ID_F040B: |
| 294 | info->flash_id += FLASH_AM040; |
| 295 | info->sector_count = 8; |
| 296 | info->size = 0x00200000; |
| 297 | break; /* => 2 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 298 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 299 | case AMD_ID_F080B: |
| 300 | info->flash_id += FLASH_AM080; |
| 301 | info->sector_count = 16; |
| 302 | info->size = 0x00400000; |
| 303 | break; /* => 4 MB */ |
wdenk | bf2f8c9 | 2003-05-22 22:52:13 +0000 | [diff] [blame] | 304 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 305 | case AMD_ID_LV400T: |
| 306 | info->flash_id += FLASH_AM400T; |
| 307 | info->sector_count = 11; |
| 308 | info->size = 0x00100000; |
| 309 | break; /* => 1 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 310 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 311 | case AMD_ID_LV400B: |
| 312 | info->flash_id += FLASH_AM400B; |
| 313 | info->sector_count = 11; |
| 314 | info->size = 0x00100000; |
| 315 | break; /* => 1 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 316 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 317 | case AMD_ID_LV800T: |
| 318 | info->flash_id += FLASH_AM800T; |
| 319 | info->sector_count = 19; |
| 320 | info->size = 0x00200000; |
| 321 | break; /* => 2 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 322 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 323 | case AMD_ID_LV800B: |
| 324 | info->flash_id += FLASH_AM800B; |
| 325 | info->sector_count = 19; |
| 326 | info->size = 0x00200000; |
| 327 | break; /* => 2 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 328 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 329 | case AMD_ID_LV160T: |
| 330 | info->flash_id += FLASH_AM160T; |
| 331 | info->sector_count = 35; |
| 332 | info->size = 0x00400000; |
| 333 | break; /* => 4 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 334 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 335 | case AMD_ID_LV160B: |
| 336 | info->flash_id += FLASH_AM160B; |
| 337 | info->sector_count = 35; |
| 338 | info->size = 0x00400000; |
| 339 | break; /* => 4 MB */ |
| 340 | #if 0 /* enable when device IDs are available */ |
| 341 | case AMD_ID_LV320T: |
| 342 | info->flash_id += FLASH_AM320T; |
| 343 | info->sector_count = 67; |
| 344 | info->size = 0x00800000; |
| 345 | break; /* => 8 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 346 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 347 | case AMD_ID_LV320B: |
| 348 | info->flash_id += FLASH_AM320B; |
| 349 | info->sector_count = 67; |
| 350 | info->size = 0x00800000; |
| 351 | break; /* => 8 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 352 | #endif |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 353 | default: |
| 354 | info->flash_id = FLASH_UNKNOWN; |
| 355 | return (0); /* => no or unknown flash */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 356 | |
| 357 | } |
| 358 | |
| 359 | #if 0 |
| 360 | /* set up sector start address table */ |
| 361 | if (info->flash_id & FLASH_BTYPE) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 362 | /* set sector offsets for bottom boot block type */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 363 | info->start[0] = base + 0x00000000; |
| 364 | info->start[1] = base + 0x00008000; |
| 365 | info->start[2] = base + 0x0000C000; |
| 366 | info->start[3] = base + 0x00010000; |
| 367 | for (i = 4; i < info->sector_count; i++) { |
| 368 | info->start[i] = base + (i * 0x00020000) - 0x00060000; |
| 369 | } |
| 370 | } else { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 371 | /* set sector offsets for top boot block type */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 372 | i = info->sector_count - 1; |
| 373 | info->start[i--] = base + info->size - 0x00008000; |
| 374 | info->start[i--] = base + info->size - 0x0000C000; |
| 375 | info->start[i--] = base + info->size - 0x00010000; |
| 376 | for (; i >= 0; i--) { |
| 377 | info->start[i] = base + i * 0x00020000; |
| 378 | } |
| 379 | } |
| 380 | #else |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 381 | flash_get_offsets ((ulong) addr, info); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 382 | #endif |
| 383 | |
| 384 | /* check for protected sectors */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 385 | for (i = 0; i < info->sector_count; i++) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 386 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 387 | /* D0 = 1 if protected */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 388 | addr = (volatile unsigned long *) (info->start[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 389 | info->protect[i] = addr[2] & 1; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Prevent writes to uninitialized FLASH. |
| 394 | */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 395 | if (info->flash_id != FLASH_UNKNOWN) { |
| 396 | addr = (volatile unsigned long *) info->start[0]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 397 | #if 0 |
| 398 | *addr = 0x00F000F0; /* reset bank */ |
| 399 | #else |
| 400 | *addr = 0xF0F0F0F0; /* reset bank */ |
| 401 | #endif |
| 402 | } |
| 403 | |
| 404 | return (info->size); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /*----------------------------------------------------------------------- |
| 409 | */ |
| 410 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 411 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 412 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 413 | vu_long *addr = (vu_long *) (info->start[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 414 | int flag, prot, sect, l_sect; |
| 415 | ulong start, now, last; |
| 416 | |
| 417 | if ((s_first < 0) || (s_first > s_last)) { |
| 418 | if (info->flash_id == FLASH_UNKNOWN) { |
| 419 | printf ("- missing\n"); |
| 420 | } else { |
| 421 | printf ("- no sectors to erase\n"); |
| 422 | } |
| 423 | return 1; |
| 424 | } |
| 425 | |
| 426 | if ((info->flash_id == FLASH_UNKNOWN) || |
| 427 | (info->flash_id > FLASH_AMD_COMP)) { |
| 428 | printf ("Can't erase unknown flash type - aborted\n"); |
| 429 | return 1; |
| 430 | } |
| 431 | |
| 432 | prot = 0; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 433 | for (sect = s_first; sect <= s_last; ++sect) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 434 | if (info->protect[sect]) { |
| 435 | prot++; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (prot) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 440 | printf ("- Warning: %d protected sectors will not be erased!\n", prot); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 441 | } else { |
| 442 | printf ("\n"); |
| 443 | } |
| 444 | |
| 445 | l_sect = -1; |
| 446 | |
| 447 | /* Disable interrupts which might cause a timeout here */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 448 | flag = disable_interrupts (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 449 | |
| 450 | #if 0 |
| 451 | addr[0x0555] = 0x00AA00AA; |
| 452 | addr[0x02AA] = 0x00550055; |
| 453 | addr[0x0555] = 0x00800080; |
| 454 | addr[0x0555] = 0x00AA00AA; |
| 455 | addr[0x02AA] = 0x00550055; |
| 456 | #else |
| 457 | addr[0x0555] = 0xAAAAAAAA; |
| 458 | addr[0x02AA] = 0x55555555; |
| 459 | addr[0x0555] = 0x80808080; |
| 460 | addr[0x0555] = 0xAAAAAAAA; |
| 461 | addr[0x02AA] = 0x55555555; |
| 462 | #endif |
| 463 | |
| 464 | /* Start erase on unprotected sectors */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 465 | for (sect = s_first; sect <= s_last; sect++) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 466 | if (info->protect[sect] == 0) { /* not protected */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 467 | addr = (vu_long *) (info->start[sect]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 468 | #if 0 |
| 469 | addr[0] = 0x00300030; |
| 470 | #else |
| 471 | addr[0] = 0x30303030; |
| 472 | #endif |
| 473 | l_sect = sect; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* re-enable interrupts if necessary */ |
| 478 | if (flag) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 479 | enable_interrupts (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 480 | |
| 481 | /* wait at least 80us - let's wait 1 ms */ |
| 482 | udelay (1000); |
| 483 | |
| 484 | /* |
| 485 | * We wait for the last triggered sector |
| 486 | */ |
| 487 | if (l_sect < 0) |
| 488 | goto DONE; |
| 489 | |
| 490 | start = get_timer (0); |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 491 | last = start; |
| 492 | addr = (vu_long *) (info->start[l_sect]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 493 | #if 0 |
| 494 | while ((addr[0] & 0x00800080) != 0x00800080) |
| 495 | #else |
| 496 | while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF) |
| 497 | #endif |
| 498 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 499 | if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 500 | printf ("Timeout\n"); |
| 501 | return 1; |
| 502 | } |
| 503 | /* show that we're waiting */ |
| 504 | if ((now - last) > 1000) { /* every second */ |
| 505 | putc ('.'); |
| 506 | last = now; |
| 507 | } |
| 508 | } |
| 509 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 510 | DONE: |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 511 | /* reset to read mode */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 512 | addr = (volatile unsigned long *) info->start[0]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 513 | #if 0 |
| 514 | addr[0] = 0x00F000F0; /* reset bank */ |
| 515 | #else |
| 516 | addr[0] = 0xF0F0F0F0; /* reset bank */ |
| 517 | #endif |
| 518 | |
| 519 | printf (" done\n"); |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /*----------------------------------------------------------------------- |
| 524 | * Copy memory to flash, returns: |
| 525 | * 0 - OK |
| 526 | * 1 - write timeout |
| 527 | * 2 - Flash not erased |
| 528 | */ |
| 529 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 530 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 531 | { |
| 532 | ulong cp, wp, data; |
| 533 | int i, l, rc; |
| 534 | |
| 535 | wp = (addr & ~3); /* get lower word aligned address */ |
| 536 | |
| 537 | /* |
| 538 | * handle unaligned start bytes |
| 539 | */ |
| 540 | if ((l = addr - wp) != 0) { |
| 541 | data = 0; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 542 | for (i = 0, cp = wp; i < l; ++i, ++cp) { |
| 543 | data = (data << 8) | (*(uchar *) cp); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 544 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 545 | for (; i < 4 && cnt > 0; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 546 | data = (data << 8) | *src++; |
| 547 | --cnt; |
| 548 | ++cp; |
| 549 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 550 | for (; cnt == 0 && i < 4; ++i, ++cp) { |
| 551 | data = (data << 8) | (*(uchar *) cp); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 552 | } |
| 553 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 554 | if ((rc = write_word (info, wp, data)) != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 555 | return (rc); |
| 556 | } |
| 557 | wp += 4; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * handle word aligned part |
| 562 | */ |
| 563 | while (cnt >= 4) { |
| 564 | data = 0; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 565 | for (i = 0; i < 4; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 566 | data = (data << 8) | *src++; |
| 567 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 568 | if ((rc = write_word (info, wp, data)) != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 569 | return (rc); |
| 570 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 571 | wp += 4; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 572 | cnt -= 4; |
| 573 | } |
| 574 | |
| 575 | if (cnt == 0) { |
| 576 | return (0); |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * handle unaligned tail bytes |
| 581 | */ |
| 582 | data = 0; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 583 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 584 | data = (data << 8) | *src++; |
| 585 | --cnt; |
| 586 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 587 | for (; i < 4; ++i, ++cp) { |
| 588 | data = (data << 8) | (*(uchar *) cp); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 589 | } |
| 590 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 591 | return (write_word (info, wp, data)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /*----------------------------------------------------------------------- |
| 595 | * Write a word to Flash, returns: |
| 596 | * 0 - OK |
| 597 | * 1 - write timeout |
| 598 | * 2 - Flash not erased |
| 599 | */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 600 | static int write_word (flash_info_t * info, ulong dest, ulong data) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 601 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 602 | vu_long *addr = (vu_long *) (info->start[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 603 | ulong start; |
| 604 | int flag; |
| 605 | |
| 606 | /* Check if Flash is (sufficiently) erased */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 607 | if ((*((vu_long *) dest) & data) != data) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 608 | return (2); |
| 609 | } |
| 610 | /* Disable interrupts which might cause a timeout here */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 611 | flag = disable_interrupts (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 612 | |
| 613 | #if 0 |
| 614 | addr[0x0555] = 0x00AA00AA; |
| 615 | addr[0x02AA] = 0x00550055; |
| 616 | addr[0x0555] = 0x00A000A0; |
| 617 | #else |
| 618 | addr[0x0555] = 0xAAAAAAAA; |
| 619 | addr[0x02AA] = 0x55555555; |
| 620 | addr[0x0555] = 0xA0A0A0A0; |
| 621 | #endif |
| 622 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 623 | *((vu_long *) dest) = data; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 624 | |
| 625 | /* re-enable interrupts if necessary */ |
| 626 | if (flag) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 627 | enable_interrupts (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 628 | |
| 629 | /* data polling for D7 */ |
| 630 | start = get_timer (0); |
| 631 | #if 0 |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 632 | while ((*((vu_long *) dest) & 0x00800080) != (data & 0x00800080)) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 633 | #else |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 634 | while ((*((vu_long *) dest) & 0x80808080) != (data & 0x80808080)) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 635 | #endif |
| 636 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 637 | if (get_timer (start) > CFG_FLASH_WRITE_TOUT) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 638 | return (1); |
| 639 | } |
| 640 | } |
| 641 | return (0); |
| 642 | } |
| 643 | |
| 644 | /*----------------------------------------------------------------------- |
| 645 | */ |