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