wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2004 |
| 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 | |
| 27 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
| 28 | |
| 29 | /*----------------------------------------------------------------------- |
| 30 | * Functions |
| 31 | */ |
| 32 | static ulong flash_get_size(vu_long * addr, flash_info_t * info); |
| 33 | static int write_byte(flash_info_t * info, ulong dest, uchar data); |
| 34 | static void flash_get_offsets(ulong base, flash_info_t * info); |
| 35 | |
| 36 | /*----------------------------------------------------------------------- |
| 37 | */ |
| 38 | |
| 39 | unsigned long flash_init(void) |
| 40 | { |
| 41 | volatile immap_t *immap = (immap_t *) CFG_IMMR; |
| 42 | volatile memctl8xx_t *memctl = &immap->im_memctl; |
| 43 | unsigned long size; |
| 44 | int i; |
| 45 | |
| 46 | /* Init: no FLASHes known */ |
| 47 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) |
| 48 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 49 | |
| 50 | size = flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]); |
| 51 | |
| 52 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { |
| 53 | printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size, size << 20); |
| 54 | } |
| 55 | |
| 56 | /* Remap FLASH according to real size */ |
| 57 | memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000); |
| 58 | memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK)); |
| 59 | |
| 60 | /* Re-do sizing to get full correct info */ |
| 61 | size = flash_get_size((vu_long *) CFG_FLASH_BASE, &flash_info[0]); |
| 62 | |
| 63 | flash_get_offsets(CFG_FLASH_BASE, &flash_info[0]); |
| 64 | |
| 65 | /* monitor protection ON by default */ |
| 66 | flash_protect(FLAG_PROTECT_SET, |
| 67 | CFG_FLASH_BASE, CFG_FLASH_BASE + monitor_flash_len - 1, |
| 68 | &flash_info[0]); |
| 69 | |
| 70 | flash_protect ( FLAG_PROTECT_SET, |
| 71 | CFG_ENV_ADDR, |
| 72 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, |
| 73 | &flash_info[0]); |
| 74 | |
| 75 | #ifdef CFG_ENV_ADDR_REDUND |
| 76 | flash_protect ( FLAG_PROTECT_SET, |
| 77 | CFG_ENV_ADDR_REDUND, |
| 78 | CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1, |
| 79 | &flash_info[0]); |
| 80 | #endif |
| 81 | |
| 82 | |
| 83 | flash_info[0].size = size; |
| 84 | |
| 85 | return (size); |
| 86 | } |
| 87 | |
| 88 | /*----------------------------------------------------------------------- |
| 89 | */ |
| 90 | static void flash_get_offsets(ulong base, flash_info_t * info) |
| 91 | { |
| 92 | int i; |
| 93 | |
| 94 | /* set up sector start address table */ |
| 95 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) { |
| 96 | for (i = 0; i < info->sector_count; i++) { |
| 97 | info->start[i] = base + (i * 0x00010000); |
| 98 | } |
| 99 | } else if (info->flash_id & FLASH_BTYPE) { |
| 100 | /* set sector offsets for bottom boot block type */ |
| 101 | info->start[0] = base + 0x00000000; |
| 102 | info->start[1] = base + 0x00004000; |
| 103 | info->start[2] = base + 0x00006000; |
| 104 | info->start[3] = base + 0x00008000; |
| 105 | for (i = 4; i < info->sector_count; i++) { |
| 106 | info->start[i] = base + (i * 0x00010000) - 0x00030000; |
| 107 | } |
| 108 | } else { |
| 109 | /* set sector offsets for top boot block type */ |
| 110 | i = info->sector_count - 1; |
| 111 | info->start[i--] = base + info->size - 0x00004000; |
| 112 | info->start[i--] = base + info->size - 0x00006000; |
| 113 | info->start[i--] = base + info->size - 0x00008000; |
| 114 | for (; i >= 0; i--) { |
| 115 | info->start[i] = base + i * 0x00010000; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |
| 121 | /*----------------------------------------------------------------------- |
| 122 | */ |
| 123 | void flash_print_info(flash_info_t * info) |
| 124 | { |
| 125 | int i; |
| 126 | |
| 127 | if (info->flash_id == FLASH_UNKNOWN) { |
| 128 | printf("missing or unknown FLASH type\n"); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | switch (info->flash_id & FLASH_VENDMASK) { |
| 133 | case FLASH_MAN_AMD: |
| 134 | printf("AMD "); |
| 135 | break; |
| 136 | case FLASH_MAN_FUJ: |
| 137 | printf("FUJITSU "); |
| 138 | break; |
| 139 | case FLASH_MAN_MX: |
| 140 | printf("MXIC "); |
| 141 | break; |
| 142 | default: |
| 143 | printf("Unknown Vendor "); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 148 | case FLASH_AM040: |
| 149 | printf("AM29LV040B (4 Mbit, bottom boot sect)\n"); |
| 150 | break; |
| 151 | case FLASH_AM400B: |
| 152 | printf("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 153 | break; |
| 154 | case FLASH_AM400T: |
| 155 | printf("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 156 | break; |
| 157 | case FLASH_AM800B: |
| 158 | printf("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 159 | break; |
| 160 | case FLASH_AM800T: |
| 161 | printf("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 162 | break; |
| 163 | case FLASH_AM160B: |
| 164 | printf("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 165 | break; |
| 166 | case FLASH_AM160T: |
| 167 | printf("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 168 | break; |
| 169 | case FLASH_AM320B: |
| 170 | printf("AM29LV320B (32 Mbit, bottom boot sect)\n"); |
| 171 | break; |
| 172 | case FLASH_AM320T: |
| 173 | printf("AM29LV320T (32 Mbit, top boot sector)\n"); |
| 174 | break; |
| 175 | default: |
| 176 | printf("Unknown Chip Type\n"); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count); |
| 181 | |
| 182 | printf(" Sector Start Addresses:"); |
| 183 | for (i = 0; i < info->sector_count; ++i) { |
| 184 | if ((i % 5) == 0) |
| 185 | printf("\n "); |
| 186 | printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " "); |
| 187 | } |
| 188 | printf("\n"); |
| 189 | } |
| 190 | |
| 191 | /*----------------------------------------------------------------------- |
| 192 | */ |
| 193 | |
| 194 | |
| 195 | /*----------------------------------------------------------------------- |
| 196 | */ |
| 197 | |
| 198 | /* |
| 199 | * The following code cannot be run from FLASH! |
| 200 | */ |
| 201 | |
| 202 | static ulong flash_get_size(vu_long * addr, flash_info_t * info) |
| 203 | { |
| 204 | short i; |
| 205 | uchar mid; |
| 206 | uchar pid; |
| 207 | vu_char *caddr = (vu_char *) addr; |
| 208 | ulong base = (ulong) addr; |
| 209 | |
| 210 | |
| 211 | /* Write auto select command: read Manufacturer ID */ |
| 212 | caddr[0x0555] = 0xAA; |
| 213 | caddr[0x02AA] = 0x55; |
| 214 | caddr[0x0555] = 0x90; |
| 215 | |
| 216 | mid = caddr[0]; |
| 217 | switch (mid) { |
| 218 | case (AMD_MANUFACT & 0xFF): |
| 219 | info->flash_id = FLASH_MAN_AMD; |
| 220 | break; |
| 221 | case (FUJ_MANUFACT & 0xFF): |
| 222 | info->flash_id = FLASH_MAN_FUJ; |
| 223 | break; |
| 224 | case (MX_MANUFACT & 0xFF): |
| 225 | info->flash_id = FLASH_MAN_MX; |
| 226 | break; |
| 227 | case (STM_MANUFACT & 0xFF): |
| 228 | info->flash_id = FLASH_MAN_STM; |
| 229 | break; |
| 230 | default: |
| 231 | info->flash_id = FLASH_UNKNOWN; |
| 232 | info->sector_count = 0; |
| 233 | info->size = 0; |
| 234 | return (0); /* no or unknown flash */ |
| 235 | } |
| 236 | |
| 237 | pid = caddr[1]; /* device ID */ |
| 238 | switch (pid) { |
| 239 | case (AMD_ID_LV400T & 0xFF): |
| 240 | info->flash_id += FLASH_AM400T; |
| 241 | info->sector_count = 11; |
| 242 | info->size = 0x00080000; |
| 243 | break; /* => 512 kB */ |
| 244 | |
| 245 | case (AMD_ID_LV400B & 0xFF): |
| 246 | info->flash_id += FLASH_AM400B; |
| 247 | info->sector_count = 11; |
| 248 | info->size = 0x00080000; |
| 249 | break; /* => 512 kB */ |
| 250 | |
| 251 | case (AMD_ID_LV800T & 0xFF): |
| 252 | info->flash_id += FLASH_AM800T; |
| 253 | info->sector_count = 19; |
| 254 | info->size = 0x00100000; |
| 255 | break; /* => 1 MB */ |
| 256 | |
| 257 | case (AMD_ID_LV800B & 0xFF): |
| 258 | info->flash_id += FLASH_AM800B; |
| 259 | info->sector_count = 19; |
| 260 | info->size = 0x00100000; |
| 261 | break; /* => 1 MB */ |
| 262 | |
| 263 | case (AMD_ID_LV160T & 0xFF): |
| 264 | info->flash_id += FLASH_AM160T; |
| 265 | info->sector_count = 35; |
| 266 | info->size = 0x00200000; |
| 267 | break; /* => 2 MB */ |
| 268 | |
| 269 | case (AMD_ID_LV160B & 0xFF): |
| 270 | info->flash_id += FLASH_AM160B; |
| 271 | info->sector_count = 35; |
| 272 | info->size = 0x00200000; |
| 273 | break; /* => 2 MB */ |
| 274 | |
| 275 | case (AMD_ID_LV040B & 0xFF): |
| 276 | info->flash_id += FLASH_AM040; |
| 277 | info->sector_count = 8; |
| 278 | info->size = 0x00080000; |
| 279 | break; |
| 280 | |
| 281 | case (STM_ID_M29W040B & 0xFF): |
| 282 | info->flash_id += FLASH_AM040; |
| 283 | info->sector_count = 8; |
| 284 | info->size = 0x00080000; |
| 285 | break; |
| 286 | |
| 287 | #if 0 /* enable when device IDs are available */ |
| 288 | case (AMD_ID_LV320T & 0xFF): |
| 289 | info->flash_id += FLASH_AM320T; |
| 290 | info->sector_count = 67; |
| 291 | info->size = 0x00400000; |
| 292 | break; /* => 4 MB */ |
| 293 | |
| 294 | case (AMD_ID_LV320B & 0xFF): |
| 295 | info->flash_id += FLASH_AM320B; |
| 296 | info->sector_count = 67; |
| 297 | info->size = 0x00400000; |
| 298 | break; /* => 4 MB */ |
| 299 | #endif |
| 300 | default: |
| 301 | info->flash_id = FLASH_UNKNOWN; |
| 302 | return (0); /* => no or unknown flash */ |
| 303 | |
| 304 | } |
| 305 | |
| 306 | printf(" "); |
| 307 | /* set up sector start address table */ |
| 308 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) { |
| 309 | for (i = 0; i < info->sector_count; i++) { |
| 310 | info->start[i] = base + (i * 0x00010000); |
| 311 | } |
| 312 | } else if (info->flash_id & FLASH_BTYPE) { |
| 313 | /* set sector offsets for bottom boot block type */ |
| 314 | info->start[0] = base + 0x00000000; |
| 315 | info->start[1] = base + 0x00004000; |
| 316 | info->start[2] = base + 0x00006000; |
| 317 | info->start[3] = base + 0x00008000; |
| 318 | for (i = 4; i < info->sector_count; i++) { |
| 319 | info->start[i] = base + (i * 0x00010000) - 0x00030000; |
| 320 | } |
| 321 | } else { |
| 322 | /* set sector offsets for top boot block type */ |
| 323 | i = info->sector_count - 1; |
| 324 | info->start[i--] = base + info->size - 0x00004000; |
| 325 | info->start[i--] = base + info->size - 0x00006000; |
| 326 | info->start[i--] = base + info->size - 0x00008000; |
| 327 | for (; i >= 0; i--) { |
| 328 | info->start[i] = base + i * 0x00010000; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* check for protected sectors */ |
| 333 | for (i = 0; i < info->sector_count; i++) { |
| 334 | /* read sector protection: D0 = 1 if protected */ |
| 335 | caddr = (volatile unsigned char *)(info->start[i]); |
| 336 | info->protect[i] = caddr[2] & 1; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Prevent writes to uninitialized FLASH. |
| 341 | */ |
| 342 | if (info->flash_id != FLASH_UNKNOWN) { |
| 343 | caddr = (vu_char *) info->start[0]; |
| 344 | |
| 345 | caddr[0x0555] = 0xAA; |
| 346 | caddr[0x02AA] = 0x55; |
| 347 | caddr[0x0555] = 0xF0; |
| 348 | |
| 349 | udelay(20000); |
| 350 | } |
| 351 | |
| 352 | return (info->size); |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /*----------------------------------------------------------------------- |
| 357 | */ |
| 358 | |
| 359 | int flash_erase(flash_info_t * info, int s_first, int s_last) |
| 360 | { |
| 361 | vu_char *addr = (vu_char *) (info->start[0]); |
| 362 | int flag, prot, sect, l_sect; |
| 363 | ulong start, now, last; |
| 364 | |
| 365 | if ((s_first < 0) || (s_first > s_last)) { |
| 366 | if (info->flash_id == FLASH_UNKNOWN) { |
| 367 | printf("- missing\n"); |
| 368 | } else { |
| 369 | printf("- no sectors to erase\n"); |
| 370 | } |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | if ((info->flash_id == FLASH_UNKNOWN) || |
| 375 | (info->flash_id > FLASH_AMD_COMP)) { |
| 376 | printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id); |
| 377 | return 1; |
| 378 | } |
| 379 | |
| 380 | prot = 0; |
| 381 | for (sect = s_first; sect <= s_last; ++sect) { |
| 382 | if (info->protect[sect]) { |
| 383 | prot++; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (prot) { |
| 388 | printf("- Warning: %d protected sectors will not be erased!\n", prot); |
| 389 | } else { |
| 390 | printf("\n"); |
| 391 | } |
| 392 | |
| 393 | l_sect = -1; |
| 394 | |
| 395 | /* Disable interrupts which might cause a timeout here */ |
| 396 | flag = disable_interrupts(); |
| 397 | |
| 398 | addr[0x0555] = 0xAA; |
| 399 | addr[0x02AA] = 0x55; |
| 400 | addr[0x0555] = 0x80; |
| 401 | addr[0x0555] = 0xAA; |
| 402 | addr[0x02AA] = 0x55; |
| 403 | |
| 404 | /* Start erase on unprotected sectors */ |
| 405 | for (sect = s_first; sect <= s_last; sect++) { |
| 406 | if (info->protect[sect] == 0) { /* not protected */ |
| 407 | addr = (vu_char *) (info->start[sect]); |
| 408 | addr[0] = 0x30; |
| 409 | l_sect = sect; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* re-enable interrupts if necessary */ |
| 414 | if (flag) |
| 415 | enable_interrupts(); |
| 416 | |
| 417 | /* wait at least 80us - let's wait 1 ms */ |
| 418 | udelay(1000); |
| 419 | |
| 420 | /* |
| 421 | * We wait for the last triggered sector |
| 422 | */ |
| 423 | if (l_sect < 0) |
| 424 | goto DONE; |
| 425 | |
| 426 | start = get_timer(0); |
| 427 | last = start; |
| 428 | addr = (vu_char *) (info->start[l_sect]); |
| 429 | while ((addr[0] & 0x80) != 0x80) { |
| 430 | if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) { |
| 431 | printf("Timeout\n"); |
| 432 | return 1; |
| 433 | } |
| 434 | /* show that we're waiting */ |
| 435 | if ((now - last) > 1000) { /* every second */ |
| 436 | putc('.'); |
| 437 | last = now; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | DONE: |
| 442 | /* reset to read mode */ |
| 443 | addr = (vu_char *) info->start[0]; |
| 444 | addr[0] = 0xF0; /* reset bank */ |
| 445 | |
| 446 | printf(" done\n"); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | /*----------------------------------------------------------------------- |
| 451 | * Copy memory to flash, returns: |
| 452 | * 0 - OK |
| 453 | * 1 - write timeout |
| 454 | * 2 - Flash not erased |
| 455 | */ |
| 456 | |
| 457 | int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
| 458 | { |
| 459 | int rc; |
| 460 | |
| 461 | while (cnt > 0) { |
| 462 | if ((rc = write_byte(info, addr++, *src++)) != 0) { |
| 463 | return (rc); |
| 464 | } |
| 465 | --cnt; |
| 466 | } |
| 467 | |
| 468 | return (0); |
| 469 | } |
| 470 | |
| 471 | /*----------------------------------------------------------------------- |
| 472 | * Write a word to Flash, returns: |
| 473 | * 0 - OK |
| 474 | * 1 - write timeout |
| 475 | * 2 - Flash not erased |
| 476 | */ |
| 477 | static int write_byte(flash_info_t * info, ulong dest, uchar data) |
| 478 | { |
| 479 | vu_char *addr = (vu_char *) (info->start[0]); |
| 480 | ulong start; |
| 481 | int flag; |
| 482 | |
| 483 | /* Check if Flash is (sufficiently) erased */ |
| 484 | if ((*((vu_char *) dest) & data) != data) { |
| 485 | return (2); |
| 486 | } |
| 487 | /* Disable interrupts which might cause a timeout here */ |
| 488 | flag = disable_interrupts(); |
| 489 | |
| 490 | addr[0x0555] = 0xAA; |
| 491 | addr[0x02AA] = 0x55; |
| 492 | addr[0x0555] = 0xA0; |
| 493 | |
| 494 | *((vu_char *) dest) = data; |
| 495 | |
| 496 | /* re-enable interrupts if necessary */ |
| 497 | if (flag) |
| 498 | enable_interrupts(); |
| 499 | |
| 500 | /* data polling for D7 */ |
| 501 | start = get_timer(0); |
| 502 | while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) { |
| 503 | if (get_timer(start) > CFG_FLASH_WRITE_TOUT) { |
| 504 | return (1); |
| 505 | } |
| 506 | } |
| 507 | return (0); |
| 508 | } |