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