wdenk | efa329c | 2004-03-23 20:18:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 4 | * |
| 5 | * (C) Copyright 2001-2004 |
| 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <linux/byteorder/swab.h> |
| 29 | |
| 30 | |
| 31 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
| 32 | |
| 33 | /* Board support for 1 or 2 flash devices */ |
| 34 | #define FLASH_PORT_WIDTH32 |
| 35 | #undef FLASH_PORT_WIDTH16 |
| 36 | |
| 37 | #ifdef FLASH_PORT_WIDTH16 |
| 38 | #define FLASH_PORT_WIDTH ushort |
| 39 | #define FLASH_PORT_WIDTHV vu_short |
| 40 | #define SWAP(x) (x) |
| 41 | #else |
| 42 | #define FLASH_PORT_WIDTH ulong |
| 43 | #define FLASH_PORT_WIDTHV vu_long |
| 44 | #define SWAP(x) (x) |
| 45 | #endif |
| 46 | |
| 47 | /* Intel-compatible flash ID */ |
| 48 | #define INTEL_COMPAT 0x00890089 |
| 49 | #define INTEL_ALT 0x00B000B0 |
| 50 | |
| 51 | /* Intel-compatible flash commands */ |
| 52 | #define INTEL_PROGRAM 0x00100010 |
| 53 | #define INTEL_ERASE 0x00200020 |
| 54 | #define INTEL_CLEAR 0x00500050 |
| 55 | #define INTEL_LOCKBIT 0x00600060 |
| 56 | #define INTEL_PROTECT 0x00010001 |
| 57 | #define INTEL_STATUS 0x00700070 |
| 58 | #define INTEL_READID 0x00900090 |
| 59 | #define INTEL_CONFIRM 0x00D000D0 |
| 60 | #define INTEL_RESET 0xFFFFFFFF |
| 61 | |
| 62 | /* Intel-compatible flash status bits */ |
| 63 | #define INTEL_FINISHED 0x00800080 |
| 64 | #define INTEL_OK 0x00800080 |
| 65 | |
| 66 | #define FPW FLASH_PORT_WIDTH |
| 67 | #define FPWV FLASH_PORT_WIDTHV |
| 68 | |
| 69 | #define mb() __asm__ __volatile__ ("" : : : "memory") |
| 70 | |
| 71 | /*----------------------------------------------------------------------- |
| 72 | * Functions |
| 73 | */ |
| 74 | static ulong flash_get_size (FPW *addr, flash_info_t *info); |
| 75 | static int write_data (flash_info_t *info, ulong dest, FPW data); |
| 76 | static void flash_get_offsets (ulong base, flash_info_t *info); |
| 77 | void inline spin_wheel (void); |
| 78 | |
| 79 | /*----------------------------------------------------------------------- |
| 80 | */ |
| 81 | |
| 82 | unsigned long flash_init (void) |
| 83 | { |
| 84 | int i; |
| 85 | ulong size = 0; |
| 86 | |
| 87 | for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) { |
| 88 | switch (i) { |
| 89 | case 0: |
| 90 | flash_get_size ((FPW *) CFG_FLASH_BASE, &flash_info[i]); |
| 91 | flash_get_offsets (CFG_FLASH_BASE, &flash_info[i]); |
| 92 | break; |
| 93 | default: |
| 94 | panic ("configured to many flash banks!\n"); |
| 95 | break; |
| 96 | } |
| 97 | size += flash_info[i].size; |
| 98 | } |
| 99 | |
| 100 | /* Protect monitor and environment sectors |
| 101 | */ |
| 102 | flash_protect ( FLAG_PROTECT_SET, |
| 103 | CFG_MONITOR_BASE, |
| 104 | CFG_MONITOR_BASE + monitor_flash_len - 1, |
| 105 | &flash_info[0] ); |
| 106 | |
| 107 | flash_protect ( FLAG_PROTECT_SET, |
| 108 | CFG_ENV_ADDR, |
| 109 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] ); |
| 110 | |
| 111 | return size; |
| 112 | } |
| 113 | |
| 114 | /*----------------------------------------------------------------------- |
| 115 | */ |
| 116 | static void flash_get_offsets (ulong base, flash_info_t *info) |
| 117 | { |
| 118 | int i; |
| 119 | |
| 120 | if (info->flash_id == FLASH_UNKNOWN) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) { |
| 125 | for (i = 0; i < info->sector_count; i++) { |
| 126 | info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE); |
| 127 | info->protect[i] = 0; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /*----------------------------------------------------------------------- |
| 133 | */ |
| 134 | void flash_print_info (flash_info_t *info) |
| 135 | { |
| 136 | int i; |
| 137 | |
| 138 | if (info->flash_id == FLASH_UNKNOWN) { |
| 139 | printf ("missing or unknown FLASH type\n"); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | switch (info->flash_id & FLASH_VENDMASK) { |
| 144 | case FLASH_MAN_INTEL: |
| 145 | printf ("INTEL "); |
| 146 | break; |
| 147 | default: |
| 148 | printf ("Unknown Vendor "); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 153 | case FLASH_28F128J3A: |
| 154 | printf ("28F128J3A\n"); |
| 155 | break; |
| 156 | |
| 157 | case FLASH_28F640J3A: |
| 158 | printf ("28F640J3A\n"); |
| 159 | break; |
| 160 | |
| 161 | case FLASH_28F320J3A: |
| 162 | printf ("28F320J3A\n"); |
| 163 | break; |
| 164 | |
| 165 | default: |
| 166 | printf ("Unknown Chip Type\n"); |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | printf (" Size: %ld MB in %d Sectors\n", |
| 171 | info->size >> 20, info->sector_count); |
| 172 | |
| 173 | printf (" Sector Start Addresses:"); |
| 174 | for (i = 0; i < info->sector_count; ++i) { |
| 175 | if ((i % 5) == 0) |
| 176 | printf ("\n "); |
| 177 | printf (" %08lX%s", |
| 178 | info->start[i], |
| 179 | info->protect[i] ? " (RO)" : " "); |
| 180 | } |
| 181 | printf ("\n"); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * The following code cannot be run from FLASH! |
| 187 | */ |
| 188 | static ulong flash_get_size (FPW *addr, flash_info_t *info) |
| 189 | { |
| 190 | volatile FPW value; |
| 191 | |
| 192 | /* Write auto select command: read Manufacturer ID */ |
| 193 | addr[0x5555] = (FPW) 0x00AA00AA; |
| 194 | addr[0x2AAA] = (FPW) 0x00550055; |
| 195 | addr[0x5555] = (FPW) 0x00900090; |
| 196 | |
| 197 | mb (); |
| 198 | value = addr[0]; |
| 199 | |
| 200 | switch (value) { |
| 201 | |
| 202 | case (FPW) INTEL_MANUFACT: |
| 203 | info->flash_id = FLASH_MAN_INTEL; |
| 204 | break; |
| 205 | |
| 206 | default: |
| 207 | info->flash_id = FLASH_UNKNOWN; |
| 208 | info->sector_count = 0; |
| 209 | info->size = 0; |
| 210 | addr[0] = (FPW) 0x00FF00FF; /* restore read mode */ |
| 211 | return (0); /* no or unknown flash */ |
| 212 | } |
| 213 | |
| 214 | mb (); |
| 215 | value = addr[1]; /* device ID */ |
| 216 | |
| 217 | switch (value) { |
| 218 | |
| 219 | case (FPW) INTEL_ID_28F128J3A: |
| 220 | info->flash_id += FLASH_28F128J3A; |
| 221 | info->sector_count = 128; |
| 222 | info->size = 0x02000000; |
| 223 | break; /* => 32 MB */ |
| 224 | |
| 225 | case (FPW) INTEL_ID_28F640J3A: |
| 226 | info->flash_id += FLASH_28F640J3A; |
| 227 | info->sector_count = 64; |
| 228 | info->size = 0x01000000; |
| 229 | break; /* => 16 MB */ |
| 230 | |
| 231 | case (FPW) INTEL_ID_28F320J3A: |
| 232 | info->flash_id += FLASH_28F320J3A; |
| 233 | info->sector_count = 32; |
| 234 | info->size = 0x00800000; |
| 235 | break; /* => 8 MB */ |
| 236 | |
| 237 | default: |
| 238 | info->flash_id = FLASH_UNKNOWN; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | if (info->sector_count > CFG_MAX_FLASH_SECT) { |
| 243 | printf ("** ERROR: sector count %d > max (%d) **\n", |
| 244 | info->sector_count, CFG_MAX_FLASH_SECT); |
| 245 | info->sector_count = CFG_MAX_FLASH_SECT; |
| 246 | } |
| 247 | |
| 248 | addr[0] = (FPW) 0x00FF00FF; /* restore read mode */ |
| 249 | |
| 250 | return (info->size); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /*----------------------------------------------------------------------- |
| 255 | */ |
| 256 | |
| 257 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 258 | { |
| 259 | int flag, prot, sect; |
| 260 | ulong type, start, last; |
| 261 | int rcode = 0; |
| 262 | |
| 263 | if ((s_first < 0) || (s_first > s_last)) { |
| 264 | if (info->flash_id == FLASH_UNKNOWN) { |
| 265 | printf ("- missing\n"); |
| 266 | } else { |
| 267 | printf ("- no sectors to erase\n"); |
| 268 | } |
| 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | type = (info->flash_id & FLASH_VENDMASK); |
| 273 | if ((type != FLASH_MAN_INTEL)) { |
| 274 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
| 275 | info->flash_id); |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | prot = 0; |
| 280 | for (sect = s_first; sect <= s_last; ++sect) { |
| 281 | if (info->protect[sect]) { |
| 282 | prot++; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | if (prot) { |
| 287 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 288 | prot); |
| 289 | } else { |
| 290 | printf ("\n"); |
| 291 | } |
| 292 | |
| 293 | start = get_timer (0); |
| 294 | last = start; |
| 295 | |
| 296 | /* Disable interrupts which might cause a timeout here */ |
| 297 | flag = disable_interrupts (); |
| 298 | |
| 299 | /* Start erase on unprotected sectors */ |
| 300 | for (sect = s_first; sect <= s_last; sect++) { |
| 301 | if (info->protect[sect] == 0) { /* not protected */ |
| 302 | FPWV *addr = (FPWV *) (info->start[sect]); |
| 303 | FPW status; |
| 304 | |
| 305 | printf ("Erasing sector %2d ... ", sect); |
| 306 | |
| 307 | /* arm simple, non interrupt dependent timer */ |
| 308 | start = get_timer(0); |
| 309 | |
| 310 | *addr = (FPW) 0x00500050; /* clear status register */ |
| 311 | *addr = (FPW) 0x00200020; /* erase setup */ |
| 312 | *addr = (FPW) 0x00D000D0; /* erase confirm */ |
| 313 | |
| 314 | while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) { |
| 315 | if (get_timer(start) > CFG_FLASH_ERASE_TOUT) { |
| 316 | printf ("Timeout\n"); |
| 317 | *addr = (FPW) 0x00B000B0; /* suspend erase */ |
| 318 | *addr = (FPW) 0x00FF00FF; /* reset to read mode */ |
| 319 | rcode = 1; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | *addr = 0x00500050; /* clear status register cmd. */ |
| 325 | *addr = 0x00FF00FF; /* resest to read mode */ |
| 326 | |
| 327 | printf (" done\n"); |
| 328 | } |
| 329 | } |
| 330 | return rcode; |
| 331 | } |
| 332 | |
| 333 | /*----------------------------------------------------------------------- |
| 334 | * Copy memory to flash, returns: |
| 335 | * 0 - OK |
| 336 | * 1 - write timeout |
| 337 | * 2 - Flash not erased |
| 338 | * 4 - Flash not identified |
| 339 | */ |
| 340 | |
| 341 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 342 | { |
| 343 | ulong cp, wp; |
| 344 | FPW data; |
| 345 | int count, i, l, rc, port_width; |
| 346 | |
| 347 | if (info->flash_id == FLASH_UNKNOWN) { |
| 348 | return 4; |
| 349 | } |
| 350 | /* get lower word aligned address */ |
| 351 | #ifdef FLASH_PORT_WIDTH16 |
| 352 | wp = (addr & ~1); |
| 353 | port_width = 2; |
| 354 | #else |
| 355 | wp = (addr & ~3); |
| 356 | port_width = 4; |
| 357 | #endif |
| 358 | |
| 359 | /* |
| 360 | * handle unaligned start bytes |
| 361 | */ |
| 362 | if ((l = addr - wp) != 0) { |
| 363 | data = 0; |
| 364 | for (i = 0, cp = wp; i < l; ++i, ++cp) { |
| 365 | data = (data << 8) | (*(uchar *) cp); |
| 366 | } |
| 367 | for (; i < port_width && cnt > 0; ++i) { |
| 368 | data = (data << 8) | *src++; |
| 369 | --cnt; |
| 370 | ++cp; |
| 371 | } |
| 372 | for (; cnt == 0 && i < port_width; ++i, ++cp) { |
| 373 | data = (data << 8) | (*(uchar *) cp); |
| 374 | } |
| 375 | |
| 376 | if ((rc = write_data (info, wp, SWAP (data))) != 0) { |
| 377 | return (rc); |
| 378 | } |
| 379 | wp += port_width; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * handle word aligned part |
| 384 | */ |
| 385 | count = 0; |
| 386 | while (cnt >= port_width) { |
| 387 | data = 0; |
| 388 | for (i = 0; i < port_width; ++i) { |
| 389 | data = (data << 8) | *src++; |
| 390 | } |
| 391 | if ((rc = write_data (info, wp, SWAP (data))) != 0) { |
| 392 | return (rc); |
| 393 | } |
| 394 | wp += port_width; |
| 395 | cnt -= port_width; |
| 396 | if (count++ > 0x800) { |
| 397 | spin_wheel (); |
| 398 | count = 0; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (cnt == 0) { |
| 403 | return (0); |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * handle unaligned tail bytes |
| 408 | */ |
| 409 | data = 0; |
| 410 | for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) { |
| 411 | data = (data << 8) | *src++; |
| 412 | --cnt; |
| 413 | } |
| 414 | for (; i < port_width; ++i, ++cp) { |
| 415 | data = (data << 8) | (*(uchar *) cp); |
| 416 | } |
| 417 | |
| 418 | return (write_data (info, wp, SWAP (data))); |
| 419 | } |
| 420 | |
| 421 | /*----------------------------------------------------------------------- |
| 422 | * Write a word or halfword to Flash, returns: |
| 423 | * 0 - OK |
| 424 | * 1 - write timeout |
| 425 | * 2 - Flash not erased |
| 426 | */ |
| 427 | static int write_data (flash_info_t *info, ulong dest, FPW data) |
| 428 | { |
| 429 | FPWV *addr = (FPWV *) dest; |
| 430 | ulong status; |
| 431 | ulong start; |
| 432 | int flag; |
| 433 | |
| 434 | /* Check if Flash is (sufficiently) erased */ |
| 435 | if ((*addr & data) != data) { |
| 436 | printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr); |
| 437 | return (2); |
| 438 | } |
| 439 | /* Disable interrupts which might cause a timeout here */ |
| 440 | flag = disable_interrupts (); |
| 441 | |
| 442 | *addr = (FPW) 0x00400040; /* write setup */ |
| 443 | *addr = data; |
| 444 | |
| 445 | /* arm simple, non interrupt dependent timer */ |
| 446 | start = get_timer(0); |
| 447 | |
| 448 | /* wait while polling the status register */ |
| 449 | while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) { |
| 450 | if (get_timer(start) > CFG_FLASH_WRITE_TOUT) { |
| 451 | *addr = (FPW) 0x00FF00FF; /* restore read mode */ |
| 452 | return (1); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | *addr = (FPW) 0x00FF00FF; /* restore read mode */ |
| 457 | |
| 458 | return (0); |
| 459 | } |
| 460 | |
| 461 | void inline spin_wheel (void) |
| 462 | { |
| 463 | static int p = 0; |
| 464 | static char w[] = "\\/-"; |
| 465 | |
| 466 | printf ("\010%c", w[p]); |
| 467 | (++p == 3) ? (p = 0) : 0; |
| 468 | } |
| 469 | |
| 470 | /*----------------------------------------------------------------------- |
| 471 | * Set/Clear sector's lock bit, returns: |
| 472 | * 0 - OK |
| 473 | * 1 - Error (timeout, voltage problems, etc.) |
| 474 | */ |
| 475 | int flash_real_protect(flash_info_t *info, long sector, int prot) |
| 476 | { |
| 477 | ulong start; |
| 478 | int i; |
| 479 | int rc = 0; |
| 480 | vu_long *addr = (vu_long *)(info->start[sector]); |
| 481 | int flag = disable_interrupts(); |
| 482 | |
| 483 | *addr = INTEL_CLEAR; /* Clear status register */ |
| 484 | if (prot) { /* Set sector lock bit */ |
| 485 | *addr = INTEL_LOCKBIT; /* Sector lock bit */ |
| 486 | *addr = INTEL_PROTECT; /* set */ |
| 487 | } |
| 488 | else { /* Clear sector lock bit */ |
| 489 | *addr = INTEL_LOCKBIT; /* All sectors lock bits */ |
| 490 | *addr = INTEL_CONFIRM; /* clear */ |
| 491 | } |
| 492 | |
| 493 | start = get_timer(0); |
| 494 | |
| 495 | while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) { |
| 496 | if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) { |
| 497 | printf("Flash lock bit operation timed out\n"); |
| 498 | rc = 1; |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (*addr != INTEL_OK) { |
| 504 | printf("Flash lock bit operation failed at %08X, CSR=%08X\n", |
| 505 | (uint)addr, (uint)*addr); |
| 506 | rc = 1; |
| 507 | } |
| 508 | |
| 509 | if (!rc) |
| 510 | info->protect[sector] = prot; |
| 511 | |
| 512 | /* |
| 513 | * Clear lock bit command clears all sectors lock bits, so |
| 514 | * we have to restore lock bits of protected sectors. |
| 515 | */ |
| 516 | if (!prot) |
| 517 | { |
| 518 | for (i = 0; i < info->sector_count; i++) |
| 519 | { |
| 520 | if (info->protect[i]) |
| 521 | { |
| 522 | start = get_timer(0); |
| 523 | addr = (vu_long *)(info->start[i]); |
| 524 | *addr = INTEL_LOCKBIT; /* Sector lock bit */ |
| 525 | *addr = INTEL_PROTECT; /* set */ |
| 526 | while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) |
| 527 | { |
| 528 | if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) |
| 529 | { |
| 530 | printf("Flash lock bit operation timed out\n"); |
| 531 | rc = 1; |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (flag) |
| 540 | enable_interrupts(); |
| 541 | |
| 542 | *addr = INTEL_RESET; /* Reset to read array mode */ |
| 543 | |
| 544 | return rc; |
| 545 | } |