uboot/board/westel/amx860/flash.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2001
   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#include <common.h>
  25#include <mpc8xx.h>
  26
  27flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  28
  29#if defined(CONFIG_ENV_IS_IN_FLASH)
  30# ifndef  CONFIG_ENV_ADDR
  31#  define CONFIG_ENV_ADDR       (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  32# endif
  33# ifndef  CONFIG_ENV_SIZE
  34#  define CONFIG_ENV_SIZE       CONFIG_ENV_SECT_SIZE
  35# endif
  36# ifndef  CONFIG_ENV_SECT_SIZE
  37#  define CONFIG_ENV_SECT_SIZE  CONFIG_ENV_SIZE
  38# endif
  39#endif
  40
  41/*---------------------------------------------------------------------*/
  42#undef DEBUG_FLASH
  43
  44#ifdef DEBUG_FLASH
  45#define DEBUGF(fmt,args...) printf(fmt ,##args)
  46#else
  47#define DEBUGF(fmt,args...)
  48#endif
  49/*---------------------------------------------------------------------*/
  50
  51/*-----------------------------------------------------------------------
  52 * Functions
  53 */
  54static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  55static int write_word (flash_info_t *info, ulong dest, ulong data);
  56static void flash_get_offsets (ulong base, flash_info_t *info);
  57
  58/*-----------------------------------------------------------------------
  59 */
  60
  61unsigned long flash_init (void)
  62{
  63        volatile immap_t     *immap  = (immap_t *)CONFIG_SYS_IMMR;
  64        volatile memctl8xx_t *memctl = &immap->im_memctl;
  65        unsigned long size_b0, size_b1;
  66        int i;
  67
  68        /* Init: no FLASHes known */
  69        for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  70                flash_info[i].flash_id = FLASH_UNKNOWN;
  71        }
  72
  73        /* Static FLASH Bank configuration here - FIXME XXX */
  74
  75        DEBUGF("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
  76
  77        size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  78
  79        if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  80                printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  81                        size_b0, size_b0<<20);
  82        }
  83
  84#if defined(FLASH_BASE1_PRELIM) && (FLASH_BASE1_PRELIM != 0)
  85        DEBUGF("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
  86
  87        size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  88
  89        if (size_b1 > size_b0) {
  90                printf ("## ERROR: "
  91                        "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
  92                        size_b1, size_b1<<20,
  93                        size_b0, size_b0<<20
  94                );
  95                flash_info[0].flash_id  = FLASH_UNKNOWN;
  96                flash_info[1].flash_id  = FLASH_UNKNOWN;
  97                flash_info[0].sector_count      = -1;
  98                flash_info[1].sector_count      = -1;
  99                flash_info[0].size              = 0;
 100                flash_info[1].size              = 0;
 101                return (0);
 102        }
 103#else
 104        size_b1 = 0;
 105#endif  /* FLASH_BASE1_PRELIM */
 106
 107        DEBUGF("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
 108
 109        DEBUGF ("## Before remap: "
 110                "BR0: 0x%08x    OR0: 0x%08x    "
 111                "BR1: 0x%08x    OR1: 0x%08x\n",
 112                memctl->memc_br0, memctl->memc_or0,
 113                memctl->memc_br1, memctl->memc_or1);
 114
 115        /* Remap FLASH according to real size */
 116        memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
 117        memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
 118
 119        DEBUGF("## BR0: 0x%08x    OR0: 0x%08x\n",
 120                memctl->memc_br0, memctl->memc_or0);
 121
 122        /* Re-do sizing to get full correct info */
 123        size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
 124
 125        flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
 126
 127        flash_info[0].size = size_b0;
 128
 129#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
 130        /* monitor protection ON by default */
 131        flash_protect(FLAG_PROTECT_SET,
 132                      CONFIG_SYS_MONITOR_BASE,
 133                      CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
 134                      &flash_info[0]);
 135#endif
 136
 137#ifdef  CONFIG_ENV_IS_IN_FLASH
 138        /* ENV protection ON by default */
 139        flash_protect(FLAG_PROTECT_SET,
 140                      CONFIG_ENV_ADDR,
 141                      CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
 142                      &flash_info[0]);
 143#endif
 144
 145        if (size_b1) {
 146                memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b1 & OR_AM_MSK);
 147                memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
 148                                   BR_MS_GPCM | BR_V;
 149
 150                DEBUGF("## BR1: 0x%08x    OR1: 0x%08x\n",
 151                        memctl->memc_br1, memctl->memc_or1);
 152
 153                /* Re-do sizing to get full correct info */
 154                size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE + size_b0),
 155                                          &flash_info[1]);
 156
 157                flash_info[1].size = size_b1;
 158
 159                flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
 160
 161# if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
 162                /* monitor protection ON by default */
 163                flash_protect(FLAG_PROTECT_SET,
 164                              CONFIG_SYS_MONITOR_BASE,
 165                              CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
 166                              &flash_info[1]);
 167# endif
 168
 169# ifdef CONFIG_ENV_IS_IN_FLASH
 170                /* ENV protection ON by default */
 171                flash_protect(FLAG_PROTECT_SET,
 172                              CONFIG_ENV_ADDR,
 173                              CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
 174                              &flash_info[1]);
 175#endif
 176        } else {
 177#ifndef CONFIG_AMX_RAM_EXT
 178                memctl->memc_br1 = 0;           /* invalidate bank */
 179                memctl->memc_or1 = 0;           /* invalidate bank */
 180#endif
 181
 182                DEBUGF("## DISABLE BR1: 0x%08x    OR1: 0x%08x\n",
 183                        memctl->memc_br1, memctl->memc_or1);
 184
 185                flash_info[1].flash_id = FLASH_UNKNOWN;
 186                flash_info[1].sector_count = -1;
 187                flash_info[1].size = 0;
 188        }
 189
 190        DEBUGF("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
 191
 192        return (size_b0 + size_b1);
 193}
 194
 195/*-----------------------------------------------------------------------
 196 */
 197static void flash_get_offsets (ulong base, flash_info_t *info)
 198{
 199        int i;
 200
 201        /* set up sector start address table */
 202        if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
 203                /* set sector offsets for uniform sector type   */
 204                for (i = 0; i < info->sector_count; i++) {
 205                        info->start[i] = base + (i * 0x00040000);
 206                }
 207        } else if (info->flash_id & FLASH_BTYPE) {
 208                /* set sector offsets for bottom boot block type        */
 209                info->start[0] = base + 0x00000000;
 210                info->start[1] = base + 0x00008000;
 211                info->start[2] = base + 0x0000C000;
 212                info->start[3] = base + 0x00010000;
 213                for (i = 4; i < info->sector_count; i++) {
 214                        info->start[i] = base + (i * 0x00020000) - 0x00060000;
 215                }
 216        } else {
 217                /* set sector offsets for top boot block type           */
 218                i = info->sector_count - 1;
 219                info->start[i--] = base + info->size - 0x00008000;
 220                info->start[i--] = base + info->size - 0x0000C000;
 221                info->start[i--] = base + info->size - 0x00010000;
 222                for (; i >= 0; i--) {
 223                        info->start[i] = base + i * 0x00020000;
 224                }
 225        }
 226}
 227
 228/*-----------------------------------------------------------------------
 229 */
 230void flash_print_info  (flash_info_t *info)
 231{
 232        int i;
 233
 234        if (info->flash_id == FLASH_UNKNOWN) {
 235                printf ("missing or unknown FLASH type\n");
 236                return;
 237        }
 238
 239        switch (info->flash_id & FLASH_VENDMASK) {
 240        case FLASH_MAN_AMD:     printf ("AMD ");                break;
 241        case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
 242        case FLASH_MAN_BM:      printf ("BRIGHT MICRO ");       break;
 243        default:                printf ("Unknown Vendor ");     break;
 244        }
 245
 246        switch (info->flash_id & FLASH_TYPEMASK) {
 247        case FLASH_AM040:       printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
 248                                break;
 249        case FLASH_AM400B:      printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
 250                                break;
 251        case FLASH_AM400T:      printf ("AM29LV400T (4 Mbit, top boot sector)\n");
 252                                break;
 253        case FLASH_AM800B:      printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
 254                                break;
 255        case FLASH_AM800T:      printf ("AM29LV800T (8 Mbit, top boot sector)\n");
 256                                break;
 257        case FLASH_AM160B:      printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
 258                                break;
 259        case FLASH_AM160T:      printf ("AM29LV160T (16 Mbit, top boot sector)\n");
 260                                break;
 261        case FLASH_AM320B:      printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
 262                                break;
 263        case FLASH_AM320T:      printf ("AM29LV320T (32 Mbit, top boot sector)\n");
 264                                break;
 265        default:                printf ("Unknown Chip Type\n");
 266                                break;
 267        }
 268
 269        printf ("  Size: %ld MB in %d Sectors\n",
 270                info->size >> 20, info->sector_count);
 271
 272        printf ("  Sector Start Addresses:");
 273        for (i=0; i<info->sector_count; ++i) {
 274                if ((i % 5) == 0)
 275                        printf ("\n   ");
 276                printf (" %08lX%s",
 277                        info->start[i],
 278                        info->protect[i] ? " (RO)" : "     "
 279                );
 280        }
 281        printf ("\n");
 282}
 283
 284/*-----------------------------------------------------------------------
 285 */
 286
 287
 288/*-----------------------------------------------------------------------
 289 */
 290
 291/*
 292 * The following code cannot be run from FLASH!
 293 */
 294
 295static ulong flash_get_size (vu_long *addr, flash_info_t *info)
 296{
 297        short i;
 298        ulong value;
 299        ulong base = (ulong)addr;
 300
 301        /* Write auto select command: read Manufacturer ID */
 302        addr[0x0555] = 0x00AA00AA;
 303        addr[0x02AA] = 0x00550055;
 304        addr[0x0555] = 0x00900090;
 305
 306        value = addr[0];
 307
 308        DEBUGF("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
 309
 310        switch (value) {
 311        case AMD_MANUFACT:
 312                info->flash_id = FLASH_MAN_AMD;
 313                break;
 314        case FUJ_MANUFACT:
 315                info->flash_id = FLASH_MAN_FUJ;
 316                break;
 317        default:
 318                info->flash_id = FLASH_UNKNOWN;
 319                info->sector_count = 0;
 320                info->size = 0;
 321                return (0);                     /* no or unknown flash  */
 322        }
 323
 324        value = addr[1];                        /* device ID            */
 325
 326        DEBUGF("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
 327
 328        switch (value) {
 329        case AMD_ID_F040B:
 330                info->flash_id += FLASH_AM040;
 331                info->sector_count = 8;
 332                info->size = 0x00200000;
 333                break;                          /* => 2 MB              */
 334
 335        case AMD_ID_LV400T:
 336                info->flash_id += FLASH_AM400T;
 337                info->sector_count = 11;
 338                info->size = 0x00100000;
 339                break;                          /* => 1 MB              */
 340
 341        case AMD_ID_LV400B:
 342                info->flash_id += FLASH_AM400B;
 343                info->sector_count = 11;
 344                info->size = 0x00100000;
 345                break;                          /* => 1 MB              */
 346
 347        case AMD_ID_LV800T:
 348                info->flash_id += FLASH_AM800T;
 349                info->sector_count = 19;
 350                info->size = 0x00200000;
 351                break;                          /* => 2 MB              */
 352
 353        case AMD_ID_LV800B:
 354                info->flash_id += FLASH_AM800B;
 355                info->sector_count = 19;
 356                info->size = 0x00200000;
 357                break;                          /* => 2 MB              */
 358
 359        case AMD_ID_LV160T:
 360                info->flash_id += FLASH_AM160T;
 361                info->sector_count = 35;
 362                info->size = 0x00400000;
 363                break;                          /* => 4 MB              */
 364
 365        case AMD_ID_LV160B:
 366                info->flash_id += FLASH_AM160B;
 367                info->sector_count = 35;
 368                info->size = 0x00400000;
 369                break;                          /* => 4 MB              */
 370#if 0   /* enable when device IDs are available */
 371        case AMD_ID_LV320T:
 372                info->flash_id += FLASH_AM320T;
 373                info->sector_count = 67;
 374                info->size = 0x00800000;
 375                break;                          /* => 8 MB              */
 376
 377        case AMD_ID_LV320B:
 378                info->flash_id += FLASH_AM320B;
 379                info->sector_count = 67;
 380                info->size = 0x00800000;
 381                break;                          /* => 8 MB              */
 382#endif
 383        default:
 384                info->flash_id = FLASH_UNKNOWN;
 385                return (0);                     /* => no or unknown flash */
 386        }
 387
 388        /* set up sector start address table */
 389        if (info->flash_id & FLASH_BTYPE) {
 390                /* set sector offsets for bottom boot block type        */
 391                info->start[0] = base + 0x00000000;
 392                info->start[1] = base + 0x00008000;
 393                info->start[2] = base + 0x0000C000;
 394                info->start[3] = base + 0x00010000;
 395                for (i = 4; i < info->sector_count; i++) {
 396                        info->start[i] = base + (i * 0x00020000) - 0x00060000;
 397                }
 398        } else {
 399                /* set sector offsets for top boot block type           */
 400                i = info->sector_count - 1;
 401                info->start[i--] = base + info->size - 0x00008000;
 402                info->start[i--] = base + info->size - 0x0000C000;
 403                info->start[i--] = base + info->size - 0x00010000;
 404                for (; i >= 0; i--) {
 405                        info->start[i] = base + i * 0x00020000;
 406                }
 407        }
 408
 409        /* check for protected sectors */
 410        for (i = 0; i < info->sector_count; i++) {
 411                /* read sector protection at sector address, (A7 .. A0) = 0x02 */
 412                /* D0 = 1 if protected */
 413                addr = (volatile unsigned long *)(info->start[i]);
 414                info->protect[i] = addr[2] & 1;
 415        }
 416
 417        /*
 418         * Prevent writes to uninitialized FLASH.
 419         */
 420        if (info->flash_id != FLASH_UNKNOWN) {
 421                addr = (volatile unsigned long *)info->start[0];
 422
 423                *addr = 0x00F000F0;     /* reset bank */
 424        }
 425
 426        return (info->size);
 427}
 428
 429
 430/*-----------------------------------------------------------------------
 431 */
 432
 433int     flash_erase (flash_info_t *info, int s_first, int s_last)
 434{
 435        vu_long *addr = (vu_long*)(info->start[0]);
 436        int flag, prot, sect, l_sect;
 437        ulong start, now, last;
 438
 439        if ((s_first < 0) || (s_first > s_last)) {
 440                if (info->flash_id == FLASH_UNKNOWN) {
 441                        printf ("- missing\n");
 442                } else {
 443                        printf ("- no sectors to erase\n");
 444                }
 445                return 1;
 446        }
 447
 448        if ((info->flash_id == FLASH_UNKNOWN) ||
 449            (info->flash_id > FLASH_AMD_COMP)) {
 450                printf ("Can't erase unknown flash type %08lx - aborted\n",
 451                        info->flash_id);
 452                return 1;
 453        }
 454
 455        prot = 0;
 456        for (sect=s_first; sect<=s_last; ++sect) {
 457                if (info->protect[sect]) {
 458                        prot++;
 459                }
 460        }
 461
 462        if (prot) {
 463                printf ("- Warning: %d protected sectors will not be erased!\n",
 464                        prot);
 465        } else {
 466                printf ("\n");
 467        }
 468
 469        l_sect = -1;
 470
 471        /* Disable interrupts which might cause a timeout here */
 472        flag = disable_interrupts();
 473
 474        addr[0x0555] = 0x00AA00AA;
 475        addr[0x02AA] = 0x00550055;
 476        addr[0x0555] = 0x00800080;
 477        addr[0x0555] = 0x00AA00AA;
 478        addr[0x02AA] = 0x00550055;
 479
 480        /* Start erase on unprotected sectors */
 481        for (sect = s_first; sect<=s_last; sect++) {
 482                if (info->protect[sect] == 0) { /* not protected */
 483                        addr = (vu_long*)(info->start[sect]);
 484                        addr[0] = 0x00300030;
 485                        l_sect = sect;
 486                }
 487        }
 488
 489        /* re-enable interrupts if necessary */
 490        if (flag)
 491                enable_interrupts();
 492
 493        /* wait at least 80us - let's wait 1 ms */
 494        udelay (1000);
 495
 496        /*
 497         * We wait for the last triggered sector
 498         */
 499        if (l_sect < 0)
 500                goto DONE;
 501
 502        start = get_timer (0);
 503        last  = start;
 504        addr = (vu_long*)(info->start[l_sect]);
 505        while ((addr[0] & 0x00800080) != 0x00800080) {
 506                if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
 507                        printf ("Timeout\n");
 508                        return 1;
 509                }
 510                /* show that we're waiting */
 511                if ((now - last) > 1000) {      /* every second */
 512                        putc ('.');
 513                        last = now;
 514                }
 515        }
 516
 517DONE:
 518        /* reset to read mode */
 519        addr = (volatile unsigned long *)info->start[0];
 520        addr[0] = 0x00F000F0;   /* reset bank */
 521
 522        printf (" done\n");
 523        return 0;
 524}
 525
 526/*-----------------------------------------------------------------------
 527 * Copy memory to flash, returns:
 528 * 0 - OK
 529 * 1 - write timeout
 530 * 2 - Flash not erased
 531 */
 532
 533int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 534{
 535        ulong cp, wp, data;
 536        int i, l, rc;
 537
 538        wp = (addr & ~3);       /* get lower word aligned address */
 539
 540        /*
 541         * handle unaligned start bytes
 542         */
 543        if ((l = addr - wp) != 0) {
 544                data = 0;
 545                for (i=0, cp=wp; i<l; ++i, ++cp) {
 546                        data = (data << 8) | (*(uchar *)cp);
 547                }
 548                for (; i<4 && cnt>0; ++i) {
 549                        data = (data << 8) | *src++;
 550                        --cnt;
 551                        ++cp;
 552                }
 553                for (; cnt==0 && i<4; ++i, ++cp) {
 554                        data = (data << 8) | (*(uchar *)cp);
 555                }
 556
 557                if ((rc = write_word(info, wp, data)) != 0) {
 558                        return (rc);
 559                }
 560                wp += 4;
 561        }
 562
 563        /*
 564         * handle word aligned part
 565         */
 566        while (cnt >= 4) {
 567                data = 0;
 568                for (i=0; i<4; ++i) {
 569                        data = (data << 8) | *src++;
 570                }
 571                if ((rc = write_word(info, wp, data)) != 0) {
 572                        return (rc);
 573                }
 574                wp  += 4;
 575                cnt -= 4;
 576        }
 577
 578        if (cnt == 0) {
 579                return (0);
 580        }
 581
 582        /*
 583         * handle unaligned tail bytes
 584         */
 585        data = 0;
 586        for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
 587                data = (data << 8) | *src++;
 588                --cnt;
 589        }
 590        for (; i<4; ++i, ++cp) {
 591                data = (data << 8) | (*(uchar *)cp);
 592        }
 593
 594        return (write_word(info, wp, data));
 595}
 596
 597/*-----------------------------------------------------------------------
 598 * Write a word to Flash, returns:
 599 * 0 - OK
 600 * 1 - write timeout
 601 * 2 - Flash not erased
 602 */
 603static int write_word (flash_info_t *info, ulong dest, ulong data)
 604{
 605        vu_long *addr = (vu_long*)(info->start[0]);
 606        ulong start;
 607        int flag;
 608
 609        /* Check if Flash is (sufficiently) erased */
 610        if ((*((vu_long *)dest) & data) != data) {
 611                return (2);
 612        }
 613        /* Disable interrupts which might cause a timeout here */
 614        flag = disable_interrupts();
 615
 616        addr[0x0555] = 0x00AA00AA;
 617        addr[0x02AA] = 0x00550055;
 618        addr[0x0555] = 0x00A000A0;
 619
 620        *((vu_long *)dest) = data;
 621
 622        /* re-enable interrupts if necessary */
 623        if (flag)
 624                enable_interrupts();
 625
 626        /* data polling for D7 */
 627        start = get_timer (0);
 628        while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
 629                if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
 630                        return (1);
 631                }
 632        }
 633        return (0);
 634}
 635
 636/*-----------------------------------------------------------------------
 637 */
 638