wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 4 | * |
| 5 | * (C) Copyright 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> |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 29 | #include <environment.h> |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 30 | |
| 31 | #define PHYS_FLASH_SECT_SIZE 0x00020000 /* 256 KB sectors (x2) */ |
| 32 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
| 33 | |
| 34 | /* Board support for 1 or 2 flash devices */ |
| 35 | #undef FLASH_PORT_WIDTH32 |
| 36 | #define FLASH_PORT_WIDTH16 |
| 37 | |
| 38 | #ifdef FLASH_PORT_WIDTH16 |
| 39 | #define FLASH_PORT_WIDTH ushort |
| 40 | #define FLASH_PORT_WIDTHV vu_short |
| 41 | #define SWAP(x) __swab16(x) |
| 42 | #else |
| 43 | #define FLASH_PORT_WIDTH ulong |
| 44 | #define FLASH_PORT_WIDTHV vu_long |
| 45 | #define SWAP(x) __swab32(x) |
| 46 | #endif |
| 47 | |
| 48 | #define FPW FLASH_PORT_WIDTH |
| 49 | #define FPWV FLASH_PORT_WIDTHV |
| 50 | |
| 51 | #define mb() __asm__ __volatile__ ("" : : : "memory") |
| 52 | |
| 53 | /*----------------------------------------------------------------------- |
| 54 | * Functions |
| 55 | */ |
| 56 | static ulong flash_get_size (FPW * addr, flash_info_t * info); |
| 57 | static int write_data (flash_info_t * info, ulong dest, FPW data); |
| 58 | static void flash_get_offsets (ulong base, flash_info_t * info); |
| 59 | void inline spin_wheel (void); |
| 60 | |
| 61 | /*----------------------------------------------------------------------- |
| 62 | */ |
| 63 | |
| 64 | unsigned long flash_init (void) |
| 65 | { |
| 66 | int i; |
| 67 | ulong size = 0; |
| 68 | |
| 69 | for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) { |
| 70 | switch (i) { |
| 71 | case 0: |
| 72 | flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]); |
| 73 | flash_get_offsets (PHYS_FLASH_1, &flash_info[i]); |
| 74 | break; |
| 75 | case 1: |
| 76 | flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]); |
| 77 | flash_get_offsets (PHYS_FLASH_2, &flash_info[i]); |
| 78 | break; |
| 79 | default: |
| 80 | panic ("configured too many flash banks!\n"); |
| 81 | break; |
| 82 | } |
| 83 | size += flash_info[i].size; |
| 84 | } |
| 85 | |
| 86 | /* Protect monitor and environment sectors |
| 87 | */ |
| 88 | flash_protect ( FLAG_PROTECT_SET, |
| 89 | CFG_FLASH_BASE, |
| 90 | CFG_FLASH_BASE + monitor_flash_len - 1, |
| 91 | &flash_info[0]); |
| 92 | |
| 93 | flash_protect ( FLAG_PROTECT_SET, |
| 94 | CFG_ENV_ADDR, |
| 95 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, |
| 96 | &flash_info[0]); |
| 97 | |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 98 | #ifdef CFG_ENV_ADDR_REDUND |
| 99 | flash_protect ( FLAG_PROTECT_SET, |
| 100 | CFG_ENV_ADDR_REDUND, |
| 101 | CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1, |
| 102 | &flash_info[0]); |
| 103 | #endif |
| 104 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 105 | return size; |
| 106 | } |
| 107 | |
| 108 | /*----------------------------------------------------------------------- |
| 109 | */ |
| 110 | static void flash_get_offsets (ulong base, flash_info_t * info) |
| 111 | { |
| 112 | int i; |
| 113 | |
| 114 | if (info->flash_id == FLASH_UNKNOWN) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) { |
| 119 | for (i = 0; i < info->sector_count; i++) { |
| 120 | info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE); |
| 121 | info->protect[i] = 0; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /*----------------------------------------------------------------------- |
| 127 | */ |
| 128 | void flash_print_info (flash_info_t * info) |
| 129 | { |
| 130 | int i; |
| 131 | |
| 132 | if (info->flash_id == FLASH_UNKNOWN) { |
| 133 | printf ("missing or unknown FLASH type\n"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | switch (info->flash_id & FLASH_VENDMASK) { |
| 138 | case FLASH_MAN_INTEL: |
| 139 | printf ("INTEL "); |
| 140 | break; |
| 141 | default: |
| 142 | printf ("Unknown Vendor "); |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 147 | case FLASH_28F128J3A: |
| 148 | printf ("28F128J3A\n"); |
| 149 | break; |
| 150 | case FLASH_28F640J3A: |
| 151 | printf ("28F640J3A\n"); |
| 152 | break; |
| 153 | default: |
| 154 | printf ("Unknown Chip Type\n"); |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | printf (" Size: %ld MB in %d Sectors\n", |
| 159 | info->size >> 20, info->sector_count); |
| 160 | |
| 161 | printf (" Sector Start Addresses:"); |
| 162 | for (i = 0; i < info->sector_count; ++i) { |
| 163 | if ((i % 5) == 0) |
| 164 | printf ("\n "); |
| 165 | printf (" %08lX%s", |
| 166 | info->start[i], |
| 167 | info->protect[i] ? " (RO)" : " "); |
| 168 | } |
| 169 | printf ("\n"); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * The following code cannot be run from FLASH! |
| 175 | */ |
| 176 | static ulong flash_get_size (FPW * addr, flash_info_t * info) |
| 177 | { |
| 178 | volatile FPW value; |
| 179 | |
| 180 | /* Write auto select command: read Manufacturer ID */ |
| 181 | addr[0x5555] = (FPW) 0x00AA00AA; |
| 182 | addr[0x2AAA] = (FPW) 0x00550055; |
| 183 | addr[0x5555] = (FPW) 0x00900090; |
| 184 | |
| 185 | mb (); |
| 186 | value = addr[0]; |
| 187 | |
| 188 | switch (value) { |
| 189 | case (FPW) INTEL_MANUFACT: |
| 190 | info->flash_id = FLASH_MAN_INTEL; |
| 191 | break; |
| 192 | default: |
| 193 | info->flash_id = FLASH_UNKNOWN; |
| 194 | info->sector_count = 0; |
| 195 | info->size = 0; |
| 196 | addr[0] = (FPW) 0x00FF00FF; /* restore read mode */ |
| 197 | return (0); /* no or unknown flash */ |
| 198 | } |
| 199 | |
| 200 | mb (); |
| 201 | value = addr[1]; /* device ID */ |
| 202 | switch (value) { |
| 203 | case (FPW) INTEL_ID_28F128J3A: |
| 204 | info->flash_id += FLASH_28F128J3A; |
| 205 | info->sector_count = 128; |
| 206 | info->size = 0x01000000; |
| 207 | break; /* => 16 MB */ |
| 208 | case (FPW) INTEL_ID_28F640J3A: |
| 209 | info->flash_id += FLASH_28F640J3A; |
| 210 | info->sector_count = 64; |
| 211 | info->size = 0x00800000; |
| 212 | break; /* => 8 MB */ |
| 213 | default: |
| 214 | info->flash_id = FLASH_UNKNOWN; |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | if (info->sector_count > CFG_MAX_FLASH_SECT) { |
| 219 | printf ("** ERROR: sector count %d > max (%d) **\n", |
| 220 | info->sector_count, CFG_MAX_FLASH_SECT); |
| 221 | info->sector_count = CFG_MAX_FLASH_SECT; |
| 222 | } |
| 223 | |
| 224 | addr[0] = (FPW) 0x00FF00FF; /* restore read mode */ |
| 225 | |
| 226 | return (info->size); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /*----------------------------------------------------------------------- |
| 231 | */ |
| 232 | |
| 233 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
| 234 | { |
| 235 | int flag, prot, sect; |
| 236 | ulong type, start, last; |
| 237 | int rcode = 0; |
| 238 | |
| 239 | if ((s_first < 0) || (s_first > s_last)) { |
| 240 | if (info->flash_id == FLASH_UNKNOWN) { |
| 241 | printf ("- missing\n"); |
| 242 | } else { |
| 243 | printf ("- no sectors to erase\n"); |
| 244 | } |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | type = (info->flash_id & FLASH_VENDMASK); |
| 249 | if ((type != FLASH_MAN_INTEL)) { |
| 250 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
| 251 | info->flash_id); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | prot = 0; |
| 256 | for (sect = s_first; sect <= s_last; ++sect) { |
| 257 | if (info->protect[sect]) { |
| 258 | prot++; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (prot) { |
| 263 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 264 | prot); |
| 265 | } else { |
| 266 | printf ("\n"); |
| 267 | } |
| 268 | |
| 269 | /*start = get_timer (0); */ |
| 270 | last = start; |
| 271 | |
| 272 | /* Disable interrupts which might cause a timeout here */ |
| 273 | flag = disable_interrupts (); |
| 274 | |
| 275 | /* Start erase on unprotected sectors */ |
| 276 | for (sect = s_first; sect <= s_last; sect++) { |
| 277 | if (info->protect[sect] == 0) { /* not protected */ |
| 278 | FPWV *addr = (FPWV *) (info->start[sect]); |
| 279 | FPW status; |
| 280 | |
| 281 | printf ("Erasing sector %2d ... ", sect); |
| 282 | |
| 283 | /* arm simple, non interrupt dependent timer */ |
| 284 | reset_timer_masked (); |
| 285 | |
| 286 | *addr = (FPW) 0x00500050; /* clear status register */ |
| 287 | *addr = (FPW) 0x00200020; /* erase setup */ |
| 288 | *addr = (FPW) 0x00D000D0; /* erase confirm */ |
| 289 | |
| 290 | while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) { |
| 291 | if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) { |
| 292 | printf ("Timeout\n"); |
| 293 | *addr = (FPW) 0x00B000B0; /* suspend erase */ |
| 294 | *addr = (FPW) 0x00FF00FF; /* reset to read mode */ |
| 295 | rcode = 1; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | *addr = (FPW) 0x00500050; /* clear status register cmd. */ |
| 301 | *addr = (FPW) 0x00FF00FF; /* resest to read mode */ |
| 302 | |
| 303 | printf (" done\n"); |
| 304 | } |
| 305 | } |
| 306 | return rcode; |
| 307 | } |
| 308 | |
| 309 | /*----------------------------------------------------------------------- |
| 310 | * Copy memory to flash, returns: |
| 311 | * 0 - OK |
| 312 | * 1 - write timeout |
| 313 | * 2 - Flash not erased |
| 314 | * 4 - Flash not identified |
| 315 | */ |
| 316 | |
| 317 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
| 318 | { |
| 319 | ulong cp, wp; |
| 320 | FPW data; |
| 321 | int count, i, l, rc, port_width; |
| 322 | |
| 323 | if (info->flash_id == FLASH_UNKNOWN) { |
| 324 | return 4; |
| 325 | } |
| 326 | /* get lower word aligned address */ |
| 327 | #ifdef FLASH_PORT_WIDTH16 |
| 328 | wp = (addr & ~1); |
| 329 | port_width = 2; |
| 330 | #else |
| 331 | wp = (addr & ~3); |
| 332 | port_width = 4; |
| 333 | #endif |
| 334 | |
| 335 | /* |
| 336 | * handle unaligned start bytes |
| 337 | */ |
| 338 | if ((l = addr - wp) != 0) { |
| 339 | data = 0; |
| 340 | for (i = 0, cp = wp; i < l; ++i, ++cp) { |
| 341 | data = (data << 8) | (*(uchar *) cp); |
| 342 | } |
| 343 | for (; i < port_width && cnt > 0; ++i) { |
| 344 | data = (data << 8) | *src++; |
| 345 | --cnt; |
| 346 | ++cp; |
| 347 | } |
| 348 | for (; cnt == 0 && i < port_width; ++i, ++cp) { |
| 349 | data = (data << 8) | (*(uchar *) cp); |
| 350 | } |
| 351 | |
| 352 | if ((rc = write_data (info, wp, SWAP (data))) != 0) { |
| 353 | return (rc); |
| 354 | } |
| 355 | wp += port_width; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * handle word aligned part |
| 360 | */ |
| 361 | count = 0; |
| 362 | while (cnt >= port_width) { |
| 363 | data = 0; |
| 364 | for (i = 0; i < port_width; ++i) { |
| 365 | data = (data << 8) | *src++; |
| 366 | } |
| 367 | if ((rc = write_data (info, wp, SWAP (data))) != 0) { |
| 368 | return (rc); |
| 369 | } |
| 370 | wp += port_width; |
| 371 | cnt -= port_width; |
| 372 | if (count++ > 0x800) { |
| 373 | spin_wheel (); |
| 374 | count = 0; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (cnt == 0) { |
| 379 | return (0); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * handle unaligned tail bytes |
| 384 | */ |
| 385 | data = 0; |
| 386 | for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) { |
| 387 | data = (data << 8) | *src++; |
| 388 | --cnt; |
| 389 | } |
| 390 | for (; i < port_width; ++i, ++cp) { |
| 391 | data = (data << 8) | (*(uchar *) cp); |
| 392 | } |
| 393 | |
| 394 | return (write_data (info, wp, SWAP (data))); |
| 395 | } |
| 396 | |
| 397 | /*----------------------------------------------------------------------- |
| 398 | * Write a word or halfword to Flash, returns: |
| 399 | * 0 - OK |
| 400 | * 1 - write timeout |
| 401 | * 2 - Flash not erased |
| 402 | */ |
| 403 | static int write_data (flash_info_t * info, ulong dest, FPW data) |
| 404 | { |
| 405 | FPWV *addr = (FPWV *) dest; |
| 406 | ulong status; |
| 407 | int flag; |
| 408 | |
| 409 | /* Check if Flash is (sufficiently) erased */ |
| 410 | if ((*addr & data) != data) { |
| 411 | printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr); |
| 412 | return (2); |
| 413 | } |
| 414 | /* Disable interrupts which might cause a timeout here */ |
| 415 | flag = disable_interrupts (); |
| 416 | |
| 417 | *addr = (FPW) 0x00400040; /* write setup */ |
| 418 | *addr = data; |
| 419 | |
| 420 | /* arm simple, non interrupt dependent timer */ |
| 421 | reset_timer_masked (); |
| 422 | |
| 423 | /* wait while polling the status register */ |
| 424 | while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) { |
| 425 | |
| 426 | if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) { |
| 427 | *addr = (FPW) 0x00FF00FF; /* restore read mode */ |
| 428 | return (1); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | *addr = (FPW) 0x00FF00FF; /* restore read mode */ |
| 433 | |
| 434 | return (0); |
| 435 | } |
| 436 | |
| 437 | void inline spin_wheel (void) |
| 438 | { |
| 439 | static int p = 0; |
| 440 | static char w[] = "\\/-"; |
| 441 | |
| 442 | printf ("\010%c", w[p]); |
| 443 | (++p == 3) ? (p = 0) : 0; |
| 444 | } |