wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Yusdi Santoso, Adaptec Inc., yusdi_santoso@adaptec.com |
| 4 | * |
| 5 | * (C) Copyright 2000-2005 |
| 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <mpc824x.h> |
| 13 | #include <asm/processor.h> |
| 14 | #include <asm/pci_io.h> |
| 15 | #include <w83c553f.h> |
| 16 | |
| 17 | #define ROM_CS0_START 0xFF800000 |
| 18 | #define ROM_CS1_START 0xFF000000 |
| 19 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 20 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 21 | |
Jean-Christophe PLAGNIOL-VILLARD | 5a1aceb | 2008-09-10 22:48:04 +0200 | [diff] [blame] | 22 | #if defined(CONFIG_ENV_IS_IN_FLASH) |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 23 | # ifndef CONFIG_ENV_ADDR |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 24 | # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 25 | # endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 26 | # ifndef CONFIG_ENV_SIZE |
| 27 | # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 28 | # endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 29 | # ifndef CONFIG_ENV_SECT_SIZE |
| 30 | # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 31 | # endif |
| 32 | #endif |
| 33 | |
| 34 | /*----------------------------------------------------------------------- |
| 35 | * Functions |
| 36 | */ |
| 37 | static int write_word (flash_info_t *info, ulong dest, ulong data); |
| 38 | |
| 39 | /*flash command address offsets*/ |
| 40 | |
| 41 | #define ADDR0 (0xAAA) |
| 42 | #define ADDR1 (0x555) |
| 43 | #define ADDR3 (0x001) |
| 44 | |
| 45 | #define FLASH_WORD_SIZE unsigned char |
| 46 | |
| 47 | /*----------------------------------------------------------------------- |
| 48 | */ |
| 49 | |
| 50 | static unsigned long flash_id (unsigned char mfct, unsigned char chip) |
| 51 | __attribute__ ((const)); |
| 52 | |
| 53 | typedef struct { |
| 54 | FLASH_WORD_SIZE extval; |
| 55 | unsigned short intval; |
| 56 | } map_entry; |
| 57 | |
| 58 | static unsigned long flash_id (unsigned char mfct, unsigned char chip) |
| 59 | { |
| 60 | static const map_entry mfct_map[] = { |
| 61 | {(FLASH_WORD_SIZE) AMD_MANUFACT, |
| 62 | (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)}, |
| 63 | {(FLASH_WORD_SIZE) FUJ_MANUFACT, |
| 64 | (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)}, |
| 65 | {(FLASH_WORD_SIZE) STM_MANUFACT, |
| 66 | (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)}, |
| 67 | {(FLASH_WORD_SIZE) MT_MANUFACT, |
| 68 | (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)}, |
| 69 | {(FLASH_WORD_SIZE) INTEL_MANUFACT, |
| 70 | (unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}, |
| 71 | {(FLASH_WORD_SIZE) INTEL_ALT_MANU, |
| 72 | (unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)} |
| 73 | }; |
| 74 | |
| 75 | static const map_entry chip_map[] = { |
| 76 | {AMD_ID_F040B, FLASH_AM040}, |
| 77 | {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB} |
| 78 | }; |
| 79 | |
| 80 | const map_entry *p; |
| 81 | unsigned long result = FLASH_UNKNOWN; |
| 82 | |
| 83 | /* find chip id */ |
| 84 | for (p = &chip_map[0]; |
| 85 | p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++) |
| 86 | if (p->extval == chip) { |
| 87 | result = FLASH_VENDMASK | p->intval; |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | /* find vendor id */ |
| 92 | for (p = &mfct_map[0]; |
| 93 | p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++) |
| 94 | if (p->extval == mfct) { |
| 95 | result &= ~FLASH_VENDMASK; |
| 96 | result |= (unsigned long) p->intval << 16; |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | unsigned long flash_init (void) |
| 104 | { |
| 105 | unsigned long i; |
| 106 | unsigned char j; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 107 | static const ulong flash_banks[] = CONFIG_SYS_FLASH_BANKS; |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 108 | |
| 109 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 110 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 111 | flash_info_t *const pflinfo = &flash_info[i]; |
| 112 | |
| 113 | pflinfo->flash_id = FLASH_UNKNOWN; |
| 114 | pflinfo->size = 0; |
| 115 | pflinfo->sector_count = 0; |
| 116 | } |
| 117 | |
| 118 | /* Enable writes to Hidden Dragon flash */ |
| 119 | { |
| 120 | register unsigned char temp; |
| 121 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 122 | CONFIG_READ_BYTE (CONFIG_SYS_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR, |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 123 | temp); |
| 124 | temp &= ~0x20; /* clear BIOSWP bit */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 125 | CONFIG_WRITE_BYTE (CONFIG_SYS_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR, |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 126 | temp); |
| 127 | } |
| 128 | |
| 129 | for (i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++) { |
| 130 | flash_info_t *const pflinfo = &flash_info[i]; |
| 131 | const unsigned long base_address = flash_banks[i]; |
| 132 | volatile FLASH_WORD_SIZE *const flash = |
| 133 | (FLASH_WORD_SIZE *) base_address; |
| 134 | |
| 135 | flash[0xAAA << (3 * i)] = 0xaa; |
| 136 | flash[0x555 << (3 * i)] = 0x55; |
| 137 | flash[0xAAA << (3 * i)] = 0x90; |
| 138 | __asm__ __volatile__ ("sync"); |
| 139 | |
| 140 | pflinfo->flash_id = |
| 141 | flash_id (flash[0x0], flash[0x2 + 14 * i]); |
| 142 | |
| 143 | switch (pflinfo->flash_id & FLASH_TYPEMASK) { |
| 144 | case FLASH_AM040: |
| 145 | pflinfo->size = 0x00080000; |
| 146 | pflinfo->sector_count = 8; |
| 147 | for (j = 0; j < 8; j++) { |
| 148 | pflinfo->start[j] = |
| 149 | base_address + 0x00010000 * j; |
| 150 | pflinfo->protect[j] = flash[(j << 16) | 0x2]; |
| 151 | } |
| 152 | break; |
| 153 | case FLASH_STM800AB: |
| 154 | pflinfo->size = 0x00100000; |
| 155 | pflinfo->sector_count = 19; |
| 156 | pflinfo->start[0] = base_address; |
| 157 | pflinfo->start[1] = base_address + 0x4000; |
| 158 | pflinfo->start[2] = base_address + 0x6000; |
| 159 | pflinfo->start[3] = base_address + 0x8000; |
| 160 | for (j = 1; j < 16; j++) { |
| 161 | pflinfo->start[j + 3] = |
| 162 | base_address + 0x00010000 * j; |
| 163 | } |
| 164 | break; |
| 165 | default: |
| 166 | /* The chip used is not listed in flash_id |
| 167 | TODO: Change this to explicitly detect the flash type |
| 168 | */ |
| 169 | { |
| 170 | int sector_addr = base_address; |
| 171 | |
| 172 | pflinfo->size = 0x00200000; |
| 173 | pflinfo->sector_count = 35; |
| 174 | pflinfo->start[0] = sector_addr; |
| 175 | sector_addr += 0x4000; /* 16K */ |
| 176 | pflinfo->start[1] = sector_addr; |
| 177 | sector_addr += 0x2000; /* 8K */ |
| 178 | pflinfo->start[2] = sector_addr; |
| 179 | sector_addr += 0x2000; /* 8K */ |
| 180 | pflinfo->start[3] = sector_addr; |
| 181 | sector_addr += 0x8000; /* 32K */ |
| 182 | |
| 183 | for (j = 4; j < 35; j++) { |
| 184 | pflinfo->start[j] = sector_addr; |
| 185 | sector_addr += 0x10000; /* 64K */ |
| 186 | } |
| 187 | } |
| 188 | break; |
| 189 | } |
| 190 | /* Protect monitor and environment sectors |
| 191 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 192 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 193 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 194 | CONFIG_SYS_MONITOR_BASE, |
| 195 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 196 | &flash_info[0]); |
| 197 | #endif |
| 198 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 199 | #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 200 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 201 | CONFIG_ENV_ADDR, |
| 202 | CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 203 | &flash_info[0]); |
| 204 | #endif |
| 205 | |
| 206 | /* reset device to read mode */ |
| 207 | flash[0x0000] = 0xf0; |
| 208 | __asm__ __volatile__ ("sync"); |
| 209 | } |
| 210 | |
| 211 | /* only have 1 bank */ |
| 212 | return flash_info[0].size; |
| 213 | } |
| 214 | |
| 215 | /*----------------------------------------------------------------------- |
| 216 | */ |
| 217 | void flash_print_info (flash_info_t * info) |
| 218 | { |
| 219 | static const char unk[] = "Unknown"; |
| 220 | const char *mfct = unk, *type = unk; |
| 221 | unsigned int i; |
| 222 | |
| 223 | if (info->flash_id != FLASH_UNKNOWN) { |
| 224 | switch (info->flash_id & FLASH_VENDMASK) { |
| 225 | case FLASH_MAN_AMD: |
| 226 | mfct = "AMD"; |
| 227 | break; |
| 228 | case FLASH_MAN_FUJ: |
| 229 | mfct = "FUJITSU"; |
| 230 | break; |
| 231 | case FLASH_MAN_STM: |
| 232 | mfct = "STM"; |
| 233 | break; |
| 234 | case FLASH_MAN_SST: |
| 235 | mfct = "SST"; |
| 236 | break; |
| 237 | case FLASH_MAN_BM: |
| 238 | mfct = "Bright Microelectonics"; |
| 239 | break; |
| 240 | case FLASH_MAN_INTEL: |
| 241 | mfct = "Intel"; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 246 | case FLASH_AM040: |
| 247 | type = "AM29F040B (512K * 8, uniform sector size)"; |
| 248 | break; |
| 249 | case FLASH_AM400B: |
| 250 | type = "AM29LV400B (4 Mbit, bottom boot sect)"; |
| 251 | break; |
| 252 | case FLASH_AM400T: |
| 253 | type = "AM29LV400T (4 Mbit, top boot sector)"; |
| 254 | break; |
| 255 | case FLASH_AM800B: |
| 256 | type = "AM29LV800B (8 Mbit, bottom boot sect)"; |
| 257 | break; |
| 258 | case FLASH_AM800T: |
| 259 | type = "AM29LV800T (8 Mbit, top boot sector)"; |
| 260 | break; |
| 261 | case FLASH_AM160T: |
| 262 | type = "AM29LV160T (16 Mbit, top boot sector)"; |
| 263 | break; |
| 264 | case FLASH_AM320B: |
| 265 | type = "AM29LV320B (32 Mbit, bottom boot sect)"; |
| 266 | break; |
| 267 | case FLASH_AM320T: |
| 268 | type = "AM29LV320T (32 Mbit, top boot sector)"; |
| 269 | break; |
| 270 | case FLASH_STM800AB: |
| 271 | type = "M29W800AB (8 Mbit, bottom boot sect)"; |
| 272 | break; |
| 273 | case FLASH_SST800A: |
| 274 | type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; |
| 275 | break; |
| 276 | case FLASH_SST160A: |
| 277 | type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | printf ("\n Brand: %s Type: %s\n" |
| 283 | " Size: %lu KB in %d Sectors\n", |
| 284 | mfct, type, info->size >> 10, info->sector_count); |
| 285 | |
| 286 | printf (" Sector Start Addresses:"); |
| 287 | |
| 288 | for (i = 0; i < info->sector_count; i++) { |
| 289 | unsigned long size; |
| 290 | unsigned int erased; |
| 291 | unsigned long *flash = (unsigned long *) info->start[i]; |
| 292 | |
| 293 | /* |
| 294 | * Check if whole sector is erased |
| 295 | */ |
| 296 | size = (i != (info->sector_count - 1)) ? |
| 297 | (info->start[i + 1] - info->start[i]) >> 2 : |
| 298 | (info->start[0] + info->size - info->start[i]) >> 2; |
| 299 | |
| 300 | for (flash = (unsigned long *) info->start[i], erased = 1; |
| 301 | (flash != (unsigned long *) info->start[i] + size) |
| 302 | && erased; flash++) |
| 303 | erased = *flash == ~0x0UL; |
| 304 | |
| 305 | printf ("%s %08lX %s %s", |
| 306 | (i % 5) ? "" : "\n ", |
| 307 | info->start[i], |
| 308 | erased ? "E" : " ", info->protect[i] ? "RO" : " "); |
| 309 | } |
| 310 | |
| 311 | puts ("\n"); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
| 316 | { |
| 317 | volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]); |
| 318 | int flag, prot, sect, l_sect; |
| 319 | ulong start, now, last; |
| 320 | unsigned char sh8b; |
| 321 | |
| 322 | if ((s_first < 0) || (s_first > s_last)) { |
| 323 | if (info->flash_id == FLASH_UNKNOWN) { |
| 324 | printf ("- missing\n"); |
| 325 | } else { |
| 326 | printf ("- no sectors to erase\n"); |
| 327 | } |
| 328 | return 1; |
| 329 | } |
| 330 | |
| 331 | if ((info->flash_id == FLASH_UNKNOWN) || |
| 332 | (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) { |
| 333 | printf ("Can't erase unknown flash type - aborted\n"); |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | prot = 0; |
| 338 | for (sect = s_first; sect <= s_last; ++sect) { |
| 339 | if (info->protect[sect]) { |
| 340 | prot++; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (prot) { |
| 345 | printf ("- Warning: %d protected sectors will not be erased!\n", prot); |
| 346 | } else { |
| 347 | printf ("\n"); |
| 348 | } |
| 349 | |
| 350 | l_sect = -1; |
| 351 | |
| 352 | /* Check the ROM CS */ |
| 353 | if ((info->start[0] >= ROM_CS1_START) |
| 354 | && (info->start[0] < ROM_CS0_START)) |
| 355 | sh8b = 3; |
| 356 | else |
| 357 | sh8b = 0; |
| 358 | |
| 359 | /* Disable interrupts which might cause a timeout here */ |
| 360 | flag = disable_interrupts (); |
| 361 | |
| 362 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 363 | addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055; |
| 364 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080; |
| 365 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 366 | addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055; |
| 367 | |
| 368 | /* Start erase on unprotected sectors */ |
| 369 | for (sect = s_first; sect <= s_last; sect++) { |
| 370 | if (info->protect[sect] == 0) { /* not protected */ |
| 371 | addr = (FLASH_WORD_SIZE *) (info->start[0] + |
| 372 | ((info->start[sect] - |
| 373 | info->start[0]) << sh8b)); |
| 374 | if (info->flash_id & FLASH_MAN_SST) { |
| 375 | addr[ADDR0 << sh8b] = |
| 376 | (FLASH_WORD_SIZE) 0x00AA00AA; |
| 377 | addr[ADDR1 << sh8b] = |
| 378 | (FLASH_WORD_SIZE) 0x00550055; |
| 379 | addr[ADDR0 << sh8b] = |
| 380 | (FLASH_WORD_SIZE) 0x00800080; |
| 381 | addr[ADDR0 << sh8b] = |
| 382 | (FLASH_WORD_SIZE) 0x00AA00AA; |
| 383 | addr[ADDR1 << sh8b] = |
| 384 | (FLASH_WORD_SIZE) 0x00550055; |
| 385 | addr[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */ |
| 386 | udelay (30000); /* wait 30 ms */ |
| 387 | } else |
| 388 | addr[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */ |
| 389 | l_sect = sect; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* re-enable interrupts if necessary */ |
| 394 | if (flag) |
| 395 | enable_interrupts (); |
| 396 | |
| 397 | /* wait at least 80us - let's wait 1 ms */ |
| 398 | udelay (1000); |
| 399 | |
| 400 | /* |
| 401 | * We wait for the last triggered sector |
| 402 | */ |
| 403 | if (l_sect < 0) |
| 404 | goto DONE; |
| 405 | |
| 406 | start = get_timer (0); |
| 407 | last = start; |
| 408 | addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->start[l_sect] - |
| 409 | info-> |
| 410 | start[0]) << sh8b)); |
| 411 | while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) != |
| 412 | (FLASH_WORD_SIZE) 0x00800080) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 413 | if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 414 | printf ("Timeout\n"); |
| 415 | return 1; |
| 416 | } |
| 417 | /* show that we're waiting */ |
| 418 | if ((now - last) > 1000) { /* every second */ |
| 419 | serial_putc ('.'); |
| 420 | last = now; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | DONE: |
| 425 | /* reset to read mode */ |
| 426 | addr = (FLASH_WORD_SIZE *) info->start[0]; |
| 427 | addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */ |
| 428 | |
| 429 | printf (" done\n"); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /*----------------------------------------------------------------------- |
| 434 | * Copy memory to flash, returns: |
| 435 | * 0 - OK |
| 436 | * 1 - write timeout |
| 437 | * 2 - Flash not erased |
| 438 | */ |
| 439 | |
| 440 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
| 441 | { |
| 442 | ulong cp, wp, data; |
| 443 | int i, l, rc; |
| 444 | |
| 445 | wp = (addr & ~3); /* get lower word aligned address */ |
| 446 | |
| 447 | /* |
| 448 | * handle unaligned start bytes |
| 449 | */ |
| 450 | if ((l = addr - wp) != 0) { |
| 451 | data = 0; |
| 452 | for (i = 0, cp = wp; i < l; ++i, ++cp) { |
| 453 | data = (data << 8) | (*(uchar *) cp); |
| 454 | } |
| 455 | for (; i < 4 && cnt > 0; ++i) { |
| 456 | data = (data << 8) | *src++; |
| 457 | --cnt; |
| 458 | ++cp; |
| 459 | } |
| 460 | for (; cnt == 0 && i < 4; ++i, ++cp) { |
| 461 | data = (data << 8) | (*(uchar *) cp); |
| 462 | } |
| 463 | |
| 464 | if ((rc = write_word (info, wp, data)) != 0) { |
| 465 | return (rc); |
| 466 | } |
| 467 | wp += 4; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * handle word aligned part |
| 472 | */ |
| 473 | while (cnt >= 4) { |
| 474 | data = 0; |
| 475 | for (i = 0; i < 4; ++i) { |
| 476 | data = (data << 8) | *src++; |
| 477 | } |
| 478 | if ((rc = write_word (info, wp, data)) != 0) { |
| 479 | return (rc); |
| 480 | } |
| 481 | wp += 4; |
| 482 | cnt -= 4; |
| 483 | } |
| 484 | |
| 485 | if (cnt == 0) { |
| 486 | return (0); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * handle unaligned tail bytes |
| 491 | */ |
| 492 | data = 0; |
| 493 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
| 494 | data = (data << 8) | *src++; |
| 495 | --cnt; |
| 496 | } |
| 497 | for (; i < 4; ++i, ++cp) { |
| 498 | data = (data << 8) | (*(uchar *) cp); |
| 499 | } |
| 500 | |
| 501 | return (write_word (info, wp, data)); |
| 502 | } |
| 503 | |
| 504 | /*----------------------------------------------------------------------- |
| 505 | * Write a word to Flash, returns: |
| 506 | * 0 - OK |
| 507 | * 1 - write timeout |
| 508 | * 2 - Flash not erased |
| 509 | */ |
| 510 | static int write_word (flash_info_t * info, ulong dest, ulong data) |
| 511 | { |
| 512 | volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) info->start[0]; |
| 513 | volatile FLASH_WORD_SIZE *dest2; |
| 514 | volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data; |
| 515 | ulong start; |
| 516 | int flag; |
| 517 | int i; |
| 518 | unsigned char sh8b; |
| 519 | |
| 520 | /* Check the ROM CS */ |
| 521 | if ((info->start[0] >= ROM_CS1_START) |
| 522 | && (info->start[0] < ROM_CS0_START)) |
| 523 | sh8b = 3; |
| 524 | else |
| 525 | sh8b = 0; |
| 526 | |
| 527 | dest2 = (FLASH_WORD_SIZE *) (((dest - info->start[0]) << sh8b) + |
| 528 | info->start[0]); |
| 529 | |
| 530 | /* Check if Flash is (sufficiently) erased */ |
| 531 | if ((*dest2 & (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) { |
| 532 | return (2); |
| 533 | } |
| 534 | /* Disable interrupts which might cause a timeout here */ |
| 535 | flag = disable_interrupts (); |
| 536 | |
| 537 | for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) { |
| 538 | addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 539 | addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055; |
| 540 | addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00A000A0; |
| 541 | |
| 542 | dest2[i << sh8b] = data2[i]; |
| 543 | |
| 544 | /* re-enable interrupts if necessary */ |
| 545 | if (flag) |
| 546 | enable_interrupts (); |
| 547 | |
| 548 | /* data polling for D7 */ |
| 549 | start = get_timer (0); |
| 550 | while ((dest2[i << sh8b] & (FLASH_WORD_SIZE) 0x00800080) != |
| 551 | (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 552 | if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 553 | return (1); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | return (0); |
| 559 | } |