wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 4 | * |
| 5 | * (C) Copyright 2001 |
| 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 <mpc8xx.h> |
| 29 | |
Mike Frysinger | 5a3d56d | 2009-02-11 19:36:20 -0500 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 32 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 33 | |
| 34 | /*----------------------------------------------------------------------- |
| 35 | * Protection Flags: |
| 36 | */ |
| 37 | #define FLAG_PROTECT_SET 0x01 |
| 38 | #define FLAG_PROTECT_CLEAR 0x02 |
| 39 | |
| 40 | /* Board support for 1 or 2 flash devices */ |
| 41 | #undef FLASH_PORT_WIDTH32 |
| 42 | #define FLASH_PORT_WIDTH16 |
| 43 | |
| 44 | #ifdef FLASH_PORT_WIDTH16 |
| 45 | #define FLASH_PORT_WIDTH ushort |
| 46 | #define FLASH_PORT_WIDTHV vu_short |
| 47 | #else |
| 48 | #define FLASH_PORT_WIDTH ulong |
| 49 | #define FLASH_PORT_WIDTHV vu_long |
| 50 | #endif |
| 51 | |
| 52 | #define FPW FLASH_PORT_WIDTH |
| 53 | #define FPWV FLASH_PORT_WIDTHV |
| 54 | |
| 55 | /*----------------------------------------------------------------------- |
| 56 | * Functions |
| 57 | */ |
| 58 | static ulong flash_get_size (FPW *addr, flash_info_t *info); |
| 59 | static int write_data (flash_info_t *info, ulong dest, FPW data); |
| 60 | static void flash_get_offsets (ulong base, flash_info_t *info); |
| 61 | |
| 62 | /*----------------------------------------------------------------------- |
| 63 | */ |
| 64 | |
| 65 | unsigned long flash_init (void) |
| 66 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 67 | volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 68 | volatile memctl8xx_t *memctl = &immap->im_memctl; |
| 69 | unsigned long size_b0; |
| 70 | int i; |
| 71 | |
| 72 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 73 | for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 74 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 75 | } |
| 76 | |
| 77 | /* Static FLASH Bank configuration here - FIXME XXX */ |
| 78 | size_b0 = flash_get_size((FPW *)FLASH_BASE0_PRELIM, &flash_info[0]); |
| 79 | |
| 80 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { |
| 81 | printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
| 82 | size_b0, size_b0<<20); |
| 83 | } |
| 84 | |
| 85 | /* Remap FLASH according to real size */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000); |
| 87 | memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 88 | |
| 89 | /* Re-do sizing to get full correct info */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 90 | size_b0 = flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 91 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 92 | flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 93 | |
| 94 | /* monitor protection ON by default */ |
| 95 | (void)flash_protect(FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | CONFIG_SYS_FLASH_BASE, |
| 97 | CONFIG_SYS_FLASH_BASE+monitor_flash_len-1, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 98 | &flash_info[0]); |
| 99 | |
| 100 | flash_info[0].size = size_b0; |
| 101 | |
| 102 | return (size_b0); |
| 103 | } |
| 104 | |
| 105 | /*----------------------------------------------------------------------- |
| 106 | */ |
| 107 | static void flash_get_offsets (ulong base, flash_info_t *info) |
| 108 | { |
| 109 | int i; |
| 110 | |
| 111 | if (info->flash_id == FLASH_UNKNOWN) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) { |
| 116 | for (i = 0; i < info->sector_count; i++) { |
| 117 | info->start[i] = base + (i * 0x00020000); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /*----------------------------------------------------------------------- |
| 123 | */ |
| 124 | void flash_print_info (flash_info_t *info) |
| 125 | { |
| 126 | int i; |
| 127 | |
| 128 | if (info->flash_id == FLASH_UNKNOWN) { |
| 129 | printf ("missing or unknown FLASH type\n"); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | switch (info->flash_id & FLASH_VENDMASK) { |
| 134 | case FLASH_MAN_INTEL: printf ("INTEL "); break; |
| 135 | default: printf ("Unknown Vendor "); break; |
| 136 | } |
| 137 | |
| 138 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 139 | case FLASH_28F320J3A: |
| 140 | printf ("28F320J3A\n"); break; |
| 141 | case FLASH_28F640J3A: |
| 142 | printf ("28F640J3A\n"); break; |
| 143 | case FLASH_28F128J3A: |
| 144 | printf ("28F128J3A\n"); break; |
| 145 | default: printf ("Unknown Chip Type\n"); break; |
| 146 | } |
| 147 | |
| 148 | printf (" Size: %ld MB in %d Sectors\n", |
| 149 | info->size >> 20, info->sector_count); |
| 150 | |
| 151 | printf (" Sector Start Addresses:"); |
| 152 | for (i=0; i<info->sector_count; ++i) { |
| 153 | if ((i % 5) == 0) |
| 154 | printf ("\n "); |
| 155 | printf (" %08lX%s", |
| 156 | info->start[i], |
| 157 | info->protect[i] ? " (RO)" : " " |
| 158 | ); |
| 159 | } |
| 160 | printf ("\n"); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | /*----------------------------------------------------------------------- |
| 165 | */ |
| 166 | |
| 167 | |
| 168 | /*----------------------------------------------------------------------- |
| 169 | */ |
| 170 | |
| 171 | /* |
| 172 | * The following code cannot be run from FLASH! |
| 173 | */ |
| 174 | |
| 175 | static ulong flash_get_size (FPW *addr, flash_info_t *info) |
| 176 | { |
| 177 | FPW value; |
| 178 | |
| 179 | /* Write auto select command: read Manufacturer ID */ |
| 180 | addr[0x5555] = (FPW)0x00AA00AA; |
| 181 | addr[0x2AAA] = (FPW)0x00550055; |
| 182 | addr[0x5555] = (FPW)0x00900090; |
| 183 | |
| 184 | value = addr[0]; |
| 185 | |
| 186 | switch (value) { |
| 187 | case (FPW)INTEL_MANUFACT: |
| 188 | info->flash_id = FLASH_MAN_INTEL; |
| 189 | break; |
| 190 | default: |
| 191 | info->flash_id = FLASH_UNKNOWN; |
| 192 | info->sector_count = 0; |
| 193 | info->size = 0; |
| 194 | addr[0] = (FPW)0x00FF00FF; /* restore read mode */ |
| 195 | return (0); /* no or unknown flash */ |
| 196 | } |
| 197 | |
| 198 | value = addr[1]; /* device ID */ |
| 199 | |
| 200 | switch (value) { |
| 201 | case (FPW)INTEL_ID_28F320J3A: |
| 202 | info->flash_id += FLASH_28F320J3A; |
| 203 | info->sector_count = 32; |
| 204 | info->size = 0x00400000; |
| 205 | break; /* => 4 MB */ |
| 206 | |
| 207 | case (FPW)INTEL_ID_28F640J3A: |
| 208 | info->flash_id += FLASH_28F640J3A; |
| 209 | info->sector_count = 64; |
| 210 | info->size = 0x00800000; |
| 211 | break; /* => 8 MB */ |
| 212 | |
| 213 | case (FPW)INTEL_ID_28F128J3A: |
| 214 | info->flash_id += FLASH_28F128J3A; |
| 215 | info->sector_count = 128; |
| 216 | info->size = 0x01000000; |
| 217 | break; /* => 16 MB */ |
| 218 | |
| 219 | default: |
| 220 | info->flash_id = FLASH_UNKNOWN; |
| 221 | break; |
| 222 | } |
| 223 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 224 | if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 225 | printf ("** ERROR: sector count %d > max (%d) **\n", |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 226 | info->sector_count, CONFIG_SYS_MAX_FLASH_SECT); |
| 227 | info->sector_count = CONFIG_SYS_MAX_FLASH_SECT; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | addr[0] = (FPW)0x00FF00FF; /* restore read mode */ |
| 231 | |
| 232 | return (info->size); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /*----------------------------------------------------------------------- |
| 237 | */ |
| 238 | |
| 239 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 240 | { |
| 241 | int flag, prot, sect; |
| 242 | ulong type, start, now, last; |
| 243 | int rcode = 0; |
| 244 | |
| 245 | if ((s_first < 0) || (s_first > s_last)) { |
| 246 | if (info->flash_id == FLASH_UNKNOWN) { |
| 247 | printf ("- missing\n"); |
| 248 | } else { |
| 249 | printf ("- no sectors to erase\n"); |
| 250 | } |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | type = (info->flash_id & FLASH_VENDMASK); |
| 255 | if ((type != FLASH_MAN_INTEL)) { |
| 256 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
| 257 | info->flash_id); |
| 258 | return 1; |
| 259 | } |
| 260 | |
| 261 | prot = 0; |
| 262 | for (sect=s_first; sect<=s_last; ++sect) { |
| 263 | if (info->protect[sect]) { |
| 264 | prot++; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (prot) { |
| 269 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 270 | prot); |
| 271 | } else { |
| 272 | printf ("\n"); |
| 273 | } |
| 274 | |
| 275 | start = get_timer (0); |
| 276 | last = start; |
| 277 | /* Start erase on unprotected sectors */ |
| 278 | for (sect = s_first; sect<=s_last; sect++) { |
| 279 | if (info->protect[sect] == 0) { /* not protected */ |
| 280 | FPWV *addr = (FPWV *)(info->start[sect]); |
| 281 | FPW status; |
| 282 | |
| 283 | /* Disable interrupts which might cause a timeout here */ |
| 284 | flag = disable_interrupts(); |
| 285 | |
| 286 | *addr = (FPW)0x00500050; /* clear status register */ |
| 287 | *addr = (FPW)0x00200020; /* erase setup */ |
| 288 | *addr = (FPW)0x00D000D0; /* erase confirm */ |
| 289 | |
| 290 | /* re-enable interrupts if necessary */ |
| 291 | if (flag) |
| 292 | enable_interrupts(); |
| 293 | |
| 294 | /* wait at least 80us - let's wait 1 ms */ |
| 295 | udelay (1000); |
| 296 | |
| 297 | while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 298 | if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 299 | printf ("Timeout\n"); |
| 300 | *addr = (FPW)0x00B000B0; /* suspend erase */ |
| 301 | *addr = (FPW)0x00FF00FF; /* reset to read mode */ |
| 302 | rcode = 1; |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | /* show that we're waiting */ |
| 307 | if ((now - last) > 1000) { /* every second */ |
| 308 | putc ('.'); |
| 309 | last = now; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | *addr = (FPW)0x00FF00FF; /* reset to read mode */ |
| 314 | printf (" done\n"); |
| 315 | } |
| 316 | } |
| 317 | return rcode; |
| 318 | } |
| 319 | |
| 320 | /*----------------------------------------------------------------------- |
| 321 | * Copy memory to flash, returns: |
| 322 | * 0 - OK |
| 323 | * 1 - write timeout |
| 324 | * 2 - Flash not erased |
| 325 | * 4 - Flash not identified |
| 326 | */ |
| 327 | |
| 328 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 329 | { |
| 330 | ulong cp, wp; |
| 331 | FPW data; |
| 332 | int count, i, l, rc, port_width; |
| 333 | |
| 334 | if (info->flash_id == FLASH_UNKNOWN) { |
| 335 | return 4; |
| 336 | } |
| 337 | /* get lower word aligned address */ |
| 338 | #ifdef FLASH_PORT_WIDTH16 |
| 339 | wp = (addr & ~1); |
| 340 | port_width = 2; |
| 341 | #else |
| 342 | wp = (addr & ~3); |
| 343 | port_width = 4; |
| 344 | #endif |
| 345 | |
| 346 | /* save sernum if needed */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 347 | if (addr >= CONFIG_SYS_FLASH_SN_SECTOR && addr < CONFIG_SYS_FLASH_SN_BASE) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 348 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 349 | u_long dest = CONFIG_SYS_FLASH_SN_BASE; |
Mike Frysinger | 5a3d56d | 2009-02-11 19:36:20 -0500 | [diff] [blame] | 350 | u_short *sn = (u_short *)gd->bd->bi_sernum; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 351 | |
| 352 | printf("(saving sernum)"); |
| 353 | for (i=0; i<4; i++) |
| 354 | { |
| 355 | if ((rc = write_data(info, dest, sn[i])) != 0) { |
| 356 | return (rc); |
| 357 | } |
| 358 | dest += port_width; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * handle unaligned start bytes |
| 364 | */ |
| 365 | if ((l = addr - wp) != 0) { |
| 366 | data = 0; |
| 367 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
| 368 | data = (data << 8) | (*(uchar *)cp); |
| 369 | } |
| 370 | for (; i<port_width && cnt>0; ++i) { |
| 371 | data = (data << 8) | *src++; |
| 372 | --cnt; |
| 373 | ++cp; |
| 374 | } |
| 375 | for (; cnt==0 && i<port_width; ++i, ++cp) { |
| 376 | data = (data << 8) | (*(uchar *)cp); |
| 377 | } |
| 378 | |
| 379 | if ((rc = write_data(info, wp, data)) != 0) { |
| 380 | return (rc); |
| 381 | } |
| 382 | wp += port_width; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * handle word aligned part |
| 387 | */ |
| 388 | count = 0; |
| 389 | while (cnt >= port_width) { |
| 390 | data = 0; |
| 391 | for (i=0; i<port_width; ++i) { |
| 392 | data = (data << 8) | *src++; |
| 393 | } |
| 394 | if ((rc = write_data(info, wp, data)) != 0) { |
| 395 | return (rc); |
| 396 | } |
| 397 | wp += port_width; |
| 398 | cnt -= port_width; |
| 399 | if (count++ > 0x800) |
| 400 | { |
| 401 | putc('.'); |
| 402 | count = 0; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if (cnt == 0) { |
| 407 | return (0); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * handle unaligned tail bytes |
| 412 | */ |
| 413 | data = 0; |
| 414 | for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) { |
| 415 | data = (data << 8) | *src++; |
| 416 | --cnt; |
| 417 | } |
| 418 | for (; i<port_width; ++i, ++cp) { |
| 419 | data = (data << 8) | (*(uchar *)cp); |
| 420 | } |
| 421 | |
| 422 | return (write_data(info, wp, data)); |
| 423 | } |
| 424 | |
| 425 | /*----------------------------------------------------------------------- |
| 426 | * Write a word or halfword to Flash, returns: |
| 427 | * 0 - OK |
| 428 | * 1 - write timeout |
| 429 | * 2 - Flash not erased |
| 430 | */ |
| 431 | static int write_data (flash_info_t *info, ulong dest, FPW data) |
| 432 | { |
| 433 | FPWV *addr = (FPWV *)dest; |
| 434 | ulong status; |
| 435 | ulong start; |
| 436 | int flag; |
| 437 | |
| 438 | /* Check if Flash is (sufficiently) erased */ |
| 439 | if ((*addr & data) != data) { |
| 440 | printf("not erased at %08lx (%x)\n",(ulong)addr,*addr); |
| 441 | return (2); |
| 442 | } |
| 443 | /* Disable interrupts which might cause a timeout here */ |
| 444 | flag = disable_interrupts(); |
| 445 | |
| 446 | *addr = (FPW)0x00400040; /* write setup */ |
| 447 | *addr = data; |
| 448 | |
| 449 | /* re-enable interrupts if necessary */ |
| 450 | if (flag) |
| 451 | enable_interrupts(); |
| 452 | |
| 453 | start = get_timer (0); |
| 454 | |
| 455 | while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 456 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 457 | *addr = (FPW)0x00FF00FF; /* restore read mode */ |
| 458 | return (1); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | *addr = (FPW)0x00FF00FF; /* restore read mode */ |
| 463 | |
| 464 | return (0); |
| 465 | } |