wdenk | ba56f62 | 2004-02-06 23:19:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002-2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2002 Jun Gu <jung@artesyncp.com> |
| 6 | * Add support for Am29F016D and dynamic switch setting. |
| 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 | /* |
| 28 | * Modified 4/5/2001 |
| 29 | * Wait for completion of each sector erase command issued |
| 30 | * 4/5/2001 |
| 31 | * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * Ported to XPedite1000, 1/2 mb boot flash only |
| 36 | * Travis B. Sawyer, <travis.sawyer@sandburst.com> |
| 37 | */ |
| 38 | |
| 39 | #include <common.h> |
| 40 | #include <ppc4xx.h> |
| 41 | #include <asm/processor.h> |
| 42 | |
| 43 | |
| 44 | #undef DEBUG |
| 45 | #ifdef DEBUG |
| 46 | #define DEBUGF(x...) printf(x) |
| 47 | #else |
| 48 | #define DEBUGF(x...) |
| 49 | #endif /* DEBUG */ |
| 50 | |
| 51 | #define BOOT_SMALL_FLASH 32 /* 00100000 */ |
| 52 | #define FLASH_ONBD_N 2 /* 00000010 */ |
| 53 | #define FLASH_SRAM_SEL 1 /* 00000001 */ |
| 54 | |
| 55 | #define BOOT_SMALL_FLASH_VAL 4 |
| 56 | #define FLASH_ONBD_N_VAL 2 |
| 57 | #define FLASH_SRAM_SEL_VAL 1 |
| 58 | |
| 59 | |
| 60 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
| 61 | |
| 62 | static unsigned long flash_addr_table[8][CFG_MAX_FLASH_BANKS] = { |
| 63 | {0xfff80000}, /* 0:000: configuraton 3 */ |
| 64 | {0xfff90000}, /* 1:001: configuraton 4 */ |
| 65 | {0xfffa0000}, /* 2:010: configuraton 7 */ |
| 66 | {0xfffb0000}, /* 3:011: configuraton 8 */ |
| 67 | {0xfffc0000}, /* 4:100: configuraton 1 */ |
| 68 | {0xfffd0000}, /* 5:101: configuraton 2 */ |
| 69 | {0xfffe0000}, /* 6:110: configuraton 5 */ |
| 70 | {0xffff0000} /* 7:111: configuraton 6 */ |
| 71 | }; |
| 72 | |
| 73 | /*----------------------------------------------------------------------- |
| 74 | * Functions |
| 75 | */ |
| 76 | static ulong flash_get_size (vu_long *addr, flash_info_t *info); |
| 77 | static int write_word (flash_info_t *info, ulong dest, ulong data); |
| 78 | |
| 79 | |
| 80 | #ifdef CONFIG_XPEDITE1K |
| 81 | #define ADDR0 0x5555 |
| 82 | #define ADDR1 0x2aaa |
| 83 | #define FLASH_WORD_SIZE unsigned char |
| 84 | #endif |
| 85 | |
| 86 | /*----------------------------------------------------------------------- |
| 87 | */ |
| 88 | |
| 89 | unsigned long flash_init (void) |
| 90 | { |
| 91 | unsigned long total_b = 0; |
| 92 | unsigned long size_b[CFG_MAX_FLASH_BANKS]; |
| 93 | unsigned short index = 0; |
| 94 | int i; |
| 95 | |
| 96 | |
| 97 | DEBUGF("\n"); |
| 98 | DEBUGF("FLASH: Index: %d\n", index); |
| 99 | |
| 100 | /* Init: no FLASHes known */ |
| 101 | for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) { |
| 102 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 103 | flash_info[i].sector_count = -1; |
| 104 | flash_info[i].size = 0; |
| 105 | |
| 106 | /* check whether the address is 0 */ |
| 107 | if (flash_addr_table[index][i] == 0) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | /* call flash_get_size() to initialize sector address */ |
| 112 | size_b[i] = flash_get_size( |
| 113 | (vu_long *)flash_addr_table[index][i], &flash_info[i]); |
| 114 | flash_info[i].size = size_b[i]; |
| 115 | if (flash_info[i].flash_id == FLASH_UNKNOWN) { |
| 116 | printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n", |
| 117 | i, size_b[i], size_b[i]<<20); |
| 118 | flash_info[i].sector_count = -1; |
| 119 | flash_info[i].size = 0; |
| 120 | } |
| 121 | |
| 122 | total_b += flash_info[i].size; |
| 123 | } |
| 124 | |
| 125 | return total_b; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /*----------------------------------------------------------------------- |
| 130 | */ |
| 131 | void flash_print_info (flash_info_t *info) |
| 132 | { |
| 133 | int i; |
| 134 | int k; |
| 135 | int size; |
| 136 | int erased; |
| 137 | volatile unsigned long *flash; |
| 138 | |
| 139 | if (info->flash_id == FLASH_UNKNOWN) { |
| 140 | printf ("missing or unknown FLASH type\n"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | switch (info->flash_id & FLASH_VENDMASK) { |
| 145 | case FLASH_MAN_AMD: printf ("AMD "); break; |
| 146 | case FLASH_MAN_FUJ: printf ("FUJITSU "); break; |
| 147 | case FLASH_MAN_SST: printf ("SST "); break; |
| 148 | default: printf ("Unknown Vendor "); break; |
| 149 | } |
| 150 | |
| 151 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 152 | case FLASH_AMD016: printf ("AM29F016D (16 Mbit, uniform sector size)\n"); |
| 153 | break; |
| 154 | case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n"); |
| 155 | break; |
| 156 | case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 157 | break; |
| 158 | case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 159 | break; |
| 160 | case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 161 | break; |
| 162 | case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 163 | break; |
| 164 | case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 165 | break; |
| 166 | case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 167 | break; |
| 168 | case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n"); |
| 169 | break; |
| 170 | case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n"); |
| 171 | break; |
| 172 | case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n"); |
| 173 | break; |
| 174 | case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n"); |
| 175 | break; |
| 176 | default: printf ("Unknown Chip Type\n"); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | printf (" Size: %ld KB in %d Sectors\n", |
| 181 | info->size >> 10, info->sector_count); |
| 182 | |
| 183 | printf (" Sector Start Addresses:"); |
| 184 | for (i=0; i<info->sector_count; ++i) { |
| 185 | /* |
| 186 | * Check if whole sector is erased |
| 187 | */ |
| 188 | if (i != (info->sector_count-1)) |
| 189 | size = info->start[i+1] - info->start[i]; |
| 190 | else |
| 191 | size = info->start[0] + info->size - info->start[i]; |
| 192 | erased = 1; |
| 193 | flash = (volatile unsigned long *)info->start[i]; |
| 194 | size = size >> 2; /* divide by 4 for longword access */ |
| 195 | for (k=0; k<size; k++) |
| 196 | { |
| 197 | if (*flash++ != 0xffffffff) |
| 198 | { |
| 199 | erased = 0; |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if ((i % 5) == 0) |
| 205 | printf ("\n "); |
| 206 | printf (" %08lX%s%s", |
| 207 | info->start[i], |
| 208 | erased ? " E" : " ", |
| 209 | info->protect[i] ? "RO " : " " |
| 210 | ); |
| 211 | } |
| 212 | printf ("\n"); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | /*----------------------------------------------------------------------- |
| 217 | */ |
| 218 | |
| 219 | |
| 220 | /*----------------------------------------------------------------------- |
| 221 | */ |
| 222 | |
| 223 | /* |
| 224 | * The following code cannot be run from FLASH! |
| 225 | */ |
| 226 | static ulong flash_get_size (vu_long *addr, flash_info_t *info) |
| 227 | { |
| 228 | short i; |
| 229 | FLASH_WORD_SIZE value; |
| 230 | ulong base = (ulong)addr; |
| 231 | volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr; |
| 232 | |
| 233 | DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr ); |
| 234 | |
| 235 | /* Write auto select command: read Manufacturer ID */ |
| 236 | udelay(10000); |
| 237 | addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 238 | udelay(1000); |
| 239 | addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 240 | udelay(1000); |
| 241 | addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090; |
| 242 | udelay(1000); |
| 243 | |
| 244 | #ifdef CONFIG_ADCIOP |
| 245 | value = addr2[2]; |
| 246 | #else |
| 247 | value = addr2[0]; |
| 248 | #endif |
| 249 | |
| 250 | DEBUGF("FLASH MANUFACT: %x\n", value); |
| 251 | |
| 252 | switch (value) { |
| 253 | case (FLASH_WORD_SIZE)AMD_MANUFACT: |
| 254 | info->flash_id = FLASH_MAN_AMD; |
| 255 | break; |
| 256 | case (FLASH_WORD_SIZE)FUJ_MANUFACT: |
| 257 | info->flash_id = FLASH_MAN_FUJ; |
| 258 | break; |
| 259 | case (FLASH_WORD_SIZE)SST_MANUFACT: |
| 260 | info->flash_id = FLASH_MAN_SST; |
| 261 | break; |
| 262 | case (FLASH_WORD_SIZE)STM_MANUFACT: |
| 263 | info->flash_id = FLASH_MAN_STM; |
| 264 | break; |
| 265 | default: |
| 266 | info->flash_id = FLASH_UNKNOWN; |
| 267 | info->sector_count = 0; |
| 268 | info->size = 0; |
| 269 | return (0); /* no or unknown flash */ |
| 270 | } |
| 271 | |
| 272 | #ifdef CONFIG_ADCIOP |
| 273 | value = addr2[0]; /* device ID */ |
| 274 | debug ("\ndev_code=%x\n", value); |
| 275 | #else |
| 276 | value = addr2[1]; /* device ID */ |
| 277 | #endif |
| 278 | |
| 279 | DEBUGF("\nFLASH DEVICEID: %x\n", value); |
| 280 | |
| 281 | switch (value) { |
| 282 | case (FLASH_WORD_SIZE)AMD_ID_LV040B: |
| 283 | info->flash_id += FLASH_AM040; |
| 284 | info->sector_count = 8; |
| 285 | info->size = 0x00080000; /* => 512 kb */ |
| 286 | break; |
| 287 | |
| 288 | default: |
| 289 | info->flash_id = FLASH_UNKNOWN; |
| 290 | return (0); /* => no or unknown flash */ |
| 291 | |
| 292 | } |
| 293 | |
| 294 | /* set up sector start address table */ |
| 295 | if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) || |
| 296 | (info->flash_id == FLASH_AM040) || |
| 297 | (info->flash_id == FLASH_AMD016)) { |
| 298 | for (i = 0; i < info->sector_count; i++) |
| 299 | info->start[i] = base + (i * 0x00010000); |
| 300 | } else { |
| 301 | if (info->flash_id & FLASH_BTYPE) { |
| 302 | /* set sector offsets for bottom boot block type */ |
| 303 | info->start[0] = base + 0x00000000; |
| 304 | info->start[1] = base + 0x00004000; |
| 305 | info->start[2] = base + 0x00006000; |
| 306 | info->start[3] = base + 0x00008000; |
| 307 | for (i = 4; i < info->sector_count; i++) { |
| 308 | info->start[i] = base + (i * 0x00010000) - 0x00030000; |
| 309 | } |
| 310 | } else { |
| 311 | /* set sector offsets for top boot block type */ |
| 312 | i = info->sector_count - 1; |
| 313 | info->start[i--] = base + info->size - 0x00004000; |
| 314 | info->start[i--] = base + info->size - 0x00006000; |
| 315 | info->start[i--] = base + info->size - 0x00008000; |
| 316 | for (; i >= 0; i--) { |
| 317 | info->start[i] = base + i * 0x00010000; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* check for protected sectors */ |
| 323 | for (i = 0; i < info->sector_count; i++) { |
| 324 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 325 | /* D0 = 1 if protected */ |
| 326 | #ifdef CONFIG_ADCIOP |
| 327 | addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]); |
| 328 | info->protect[i] = addr2[4] & 1; |
| 329 | #else |
| 330 | addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]); |
| 331 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) |
| 332 | info->protect[i] = 0; |
| 333 | else |
| 334 | info->protect[i] = addr2[2] & 1; |
| 335 | #endif |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Prevent writes to uninitialized FLASH. |
| 340 | */ |
| 341 | if (info->flash_id != FLASH_UNKNOWN) { |
| 342 | #if 0 /* test-only */ |
| 343 | #ifdef CONFIG_ADCIOP |
| 344 | addr2 = (volatile unsigned char *)info->start[0]; |
| 345 | addr2[ADDR0] = 0xAA; |
| 346 | addr2[ADDR1] = 0x55; |
| 347 | addr2[ADDR0] = 0xF0; /* reset bank */ |
| 348 | #else |
| 349 | addr2 = (FLASH_WORD_SIZE *)info->start[0]; |
| 350 | *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */ |
| 351 | #endif |
| 352 | #else /* test-only */ |
| 353 | addr2 = (FLASH_WORD_SIZE *)info->start[0]; |
| 354 | *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */ |
| 355 | #endif /* test-only */ |
| 356 | } |
| 357 | |
| 358 | return (info->size); |
| 359 | } |
| 360 | |
| 361 | int wait_for_DQ7(flash_info_t *info, int sect) |
| 362 | { |
| 363 | ulong start, now, last; |
| 364 | volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]); |
| 365 | |
| 366 | start = get_timer (0); |
| 367 | last = start; |
| 368 | while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) { |
| 369 | if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) { |
| 370 | printf ("Timeout\n"); |
| 371 | return -1; |
| 372 | } |
| 373 | /* show that we're waiting */ |
| 374 | if ((now - last) > 1000) { /* every second */ |
| 375 | putc ('.'); |
| 376 | last = now; |
| 377 | } |
| 378 | } |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | /*----------------------------------------------------------------------- |
| 383 | */ |
| 384 | |
| 385 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 386 | { |
| 387 | volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]); |
| 388 | volatile FLASH_WORD_SIZE *addr2; |
| 389 | int flag, prot, sect, l_sect; |
| 390 | int i; |
| 391 | |
| 392 | if ((s_first < 0) || (s_first > s_last)) { |
| 393 | if (info->flash_id == FLASH_UNKNOWN) { |
| 394 | printf ("- missing\n"); |
| 395 | } else { |
| 396 | printf ("- no sectors to erase\n"); |
| 397 | } |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | if (info->flash_id == FLASH_UNKNOWN) { |
| 402 | printf ("Can't erase unknown flash type - aborted\n"); |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | prot = 0; |
| 407 | for (sect=s_first; sect<=s_last; ++sect) { |
| 408 | if (info->protect[sect]) { |
| 409 | prot++; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (prot) { |
| 414 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 415 | prot); |
| 416 | } else { |
| 417 | printf ("\n"); |
| 418 | } |
| 419 | |
| 420 | l_sect = -1; |
| 421 | |
| 422 | /* Disable interrupts which might cause a timeout here */ |
| 423 | flag = disable_interrupts(); |
| 424 | |
| 425 | /* Start erase on unprotected sectors */ |
| 426 | for (sect = s_first; sect<=s_last; sect++) { |
| 427 | if (info->protect[sect] == 0) { /* not protected */ |
| 428 | addr2 = (FLASH_WORD_SIZE *)(info->start[sect]); |
| 429 | printf("Erasing sector %p\n", addr2); |
| 430 | |
| 431 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) { |
| 432 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 433 | addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 434 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080; |
| 435 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 436 | addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 437 | addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */ |
| 438 | for (i=0; i<50; i++) |
| 439 | udelay(1000); /* wait 1 ms */ |
| 440 | } else { |
| 441 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 442 | addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 443 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080; |
| 444 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 445 | addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 446 | addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */ |
| 447 | } |
| 448 | l_sect = sect; |
| 449 | /* |
| 450 | * Wait for each sector to complete, it's more |
| 451 | * reliable. According to AMD Spec, you must |
| 452 | * issue all erase commands within a specified |
| 453 | * timeout. This has been seen to fail, especially |
| 454 | * if printf()s are included (for debug)!! |
| 455 | */ |
| 456 | wait_for_DQ7(info, sect); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* re-enable interrupts if necessary */ |
| 461 | if (flag) |
| 462 | enable_interrupts(); |
| 463 | |
| 464 | /* wait at least 80us - let's wait 1 ms */ |
| 465 | udelay (1000); |
| 466 | |
| 467 | #if 0 |
| 468 | /* |
| 469 | * We wait for the last triggered sector |
| 470 | */ |
| 471 | if (l_sect < 0) |
| 472 | goto DONE; |
| 473 | wait_for_DQ7(info, l_sect); |
| 474 | |
| 475 | DONE: |
| 476 | #endif |
| 477 | /* reset to read mode */ |
| 478 | addr = (FLASH_WORD_SIZE *)info->start[0]; |
| 479 | addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */ |
| 480 | |
| 481 | printf (" done\n"); |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | /*----------------------------------------------------------------------- |
| 486 | * Copy memory to flash, returns: |
| 487 | * 0 - OK |
| 488 | * 1 - write timeout |
| 489 | * 2 - Flash not erased |
| 490 | */ |
| 491 | |
| 492 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 493 | { |
| 494 | ulong cp, wp, data; |
| 495 | int i, l, rc; |
| 496 | |
| 497 | wp = (addr & ~3); /* get lower word aligned address */ |
| 498 | |
| 499 | /* |
| 500 | * handle unaligned start bytes |
| 501 | */ |
| 502 | if ((l = addr - wp) != 0) { |
| 503 | data = 0; |
| 504 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
| 505 | data = (data << 8) | (*(uchar *)cp); |
| 506 | } |
| 507 | for (; i<4 && cnt>0; ++i) { |
| 508 | data = (data << 8) | *src++; |
| 509 | --cnt; |
| 510 | ++cp; |
| 511 | } |
| 512 | for (; cnt==0 && i<4; ++i, ++cp) { |
| 513 | data = (data << 8) | (*(uchar *)cp); |
| 514 | } |
| 515 | |
| 516 | if ((rc = write_word(info, wp, data)) != 0) { |
| 517 | return (rc); |
| 518 | } |
| 519 | wp += 4; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * handle word aligned part |
| 524 | */ |
| 525 | while (cnt >= 4) { |
| 526 | data = 0; |
| 527 | for (i=0; i<4; ++i) { |
| 528 | data = (data << 8) | *src++; |
| 529 | } |
| 530 | if ((rc = write_word(info, wp, data)) != 0) { |
| 531 | return (rc); |
| 532 | } |
| 533 | wp += 4; |
| 534 | cnt -= 4; |
| 535 | } |
| 536 | |
| 537 | if (cnt == 0) { |
| 538 | return (0); |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * handle unaligned tail bytes |
| 543 | */ |
| 544 | data = 0; |
| 545 | for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) { |
| 546 | data = (data << 8) | *src++; |
| 547 | --cnt; |
| 548 | } |
| 549 | for (; i<4; ++i, ++cp) { |
| 550 | data = (data << 8) | (*(uchar *)cp); |
| 551 | } |
| 552 | |
| 553 | return (write_word(info, wp, data)); |
| 554 | } |
| 555 | |
| 556 | /*----------------------------------------------------------------------- |
| 557 | * Write a word to Flash, returns: |
| 558 | * 0 - OK |
| 559 | * 1 - write timeout |
| 560 | * 2 - Flash not erased |
| 561 | */ |
| 562 | static int write_word (flash_info_t * info, ulong dest, ulong data) |
| 563 | { |
| 564 | volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]); |
| 565 | volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest; |
| 566 | volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data; |
| 567 | ulong start; |
| 568 | int i; |
| 569 | |
| 570 | /* Check if Flash is (sufficiently) erased */ |
| 571 | if ((*((volatile FLASH_WORD_SIZE *) dest) & |
| 572 | (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) { |
| 573 | return (2); |
| 574 | } |
| 575 | |
| 576 | for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) { |
| 577 | int flag; |
| 578 | |
| 579 | /* Disable interrupts which might cause a timeout here */ |
| 580 | flag = disable_interrupts (); |
| 581 | |
| 582 | addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 583 | addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055; |
| 584 | addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0; |
| 585 | |
| 586 | dest2[i] = data2[i]; |
| 587 | |
| 588 | /* re-enable interrupts if necessary */ |
| 589 | if (flag) |
| 590 | enable_interrupts (); |
| 591 | |
| 592 | /* data polling for D7 */ |
| 593 | start = get_timer (0); |
| 594 | while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) != |
| 595 | (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) { |
| 596 | |
| 597 | if (get_timer (start) > CFG_FLASH_WRITE_TOUT) { |
| 598 | return (1); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | return (0); |
| 604 | } |
| 605 | |
| 606 | /*----------------------------------------------------------------------- |
| 607 | */ |