uboot/board/iphase4539/flash.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2001
   3 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
   4 *
   5 * (C) Copyright 2002
   6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   7 *
   8 * Adapted for Interphase 4539 by Wolfgang Grandegger <wg@denx.de>.
   9 *
  10 * See file CREDITS for list of people who contributed to this
  11 * project.
  12 *
  13 * This program is free software; you can redistribute it and/or
  14 * modify it under the terms of the GNU General Public License as
  15 * published by the Free Software Foundation; either version 2 of
  16 * the License, or (at your option) any later version.
  17 *
  18 * This program is distributed in the hope that it will be useful,
  19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 * GNU General Public License for more details.
  22 *
  23 * You should have received a copy of the GNU General Public License
  24 * along with this program; if not, write to the Free Software
  25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26 * MA 02111-1307 USA
  27 */
  28
  29#include <config.h>
  30#include <common.h>
  31#include <flash.h>
  32#include <asm/io.h>
  33
  34flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  35
  36extern int hwc_flash_size(void);
  37static ulong flash_get_size (u32 addr, flash_info_t *info);
  38static int flash_get_offsets (u32 base, flash_info_t *info);
  39static int write_word (flash_info_t *info, ulong dest, ulong data);
  40static void flash_reset (u32 addr);
  41
  42#define out8(a,v) *(volatile unsigned char*)(a) = v
  43#define in8(a)    *(volatile unsigned char*)(a)
  44#define in32(a)   *(volatile unsigned long*)(a)
  45#define iobarrier_rw() eieio()
  46
  47unsigned long flash_init (void)
  48{
  49        unsigned int i;
  50        unsigned long flash_size = 0;
  51        unsigned long bank_size;
  52        unsigned int bank = 0;
  53
  54        /* Init: no FLASHes known */
  55        for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  56                flash_info[i].flash_id = FLASH_UNKNOWN;
  57                flash_info[i].sector_count = 0;
  58                flash_info[i].size = 0;
  59        }
  60
  61        /* Initialise the BOOT Flash */
  62        if (bank == CONFIG_SYS_MAX_FLASH_BANKS) {
  63                puts ("Warning: not all Flashes are initialised !");
  64                return flash_size;
  65        }
  66
  67        bank_size = flash_get_size (CONFIG_SYS_FLASH_BASE, flash_info + bank);
  68        if (bank_size) {
  69#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE && \
  70    CONFIG_SYS_MONITOR_BASE < CONFIG_SYS_FLASH_BASE + CONFIG_SYS_MAX_FLASH_SIZE
  71                /* monitor protection ON by default */
  72                flash_protect(FLAG_PROTECT_SET,
  73                              CONFIG_SYS_MONITOR_BASE,
  74                              CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  75                              flash_info + bank);
  76#endif
  77
  78#ifdef CONFIG_ENV_IS_IN_FLASH
  79                /* ENV protection ON by default */
  80                flash_protect(FLAG_PROTECT_SET,
  81                              CONFIG_ENV_ADDR,
  82                              CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
  83                              flash_info + bank);
  84#endif
  85
  86                /* HWC protection ON by default */
  87                flash_protect(FLAG_PROTECT_SET,
  88                              CONFIG_SYS_FLASH_BASE,
  89                              CONFIG_SYS_FLASH_BASE + 0x10000 - 1,
  90                              flash_info + bank);
  91
  92                flash_size += bank_size;
  93                bank++;
  94        } else {
  95                puts ("Warning: the BOOT Flash is not initialised !");
  96        }
  97
  98        return flash_size;
  99}
 100
 101/*
 102 * The following code cannot be run from FLASH!
 103 */
 104static ulong flash_get_size (u32 addr, flash_info_t *info)
 105{
 106        volatile uchar value;
 107#if 0
 108        int i;
 109#endif
 110
 111        /* Write auto select command: read Manufacturer ID */
 112        out8(addr + 0x0555, 0xAA);
 113        iobarrier_rw();
 114        udelay(10);
 115        out8(addr + 0x02AA, 0x55);
 116        iobarrier_rw();
 117        udelay(10);
 118        out8(addr + 0x0555, 0x90);
 119        iobarrier_rw();
 120        udelay(10);
 121
 122        value = in8(addr);
 123        iobarrier_rw();
 124        udelay(10);
 125        switch (value | (value << 16)) {
 126        case AMD_MANUFACT:
 127                info->flash_id = FLASH_MAN_AMD;
 128                break;
 129
 130        case FUJ_MANUFACT:
 131                info->flash_id = FLASH_MAN_FUJ;
 132                break;
 133
 134        default:
 135                info->flash_id = FLASH_UNKNOWN;
 136                flash_reset (addr);
 137                return 0;
 138        }
 139
 140        value = in8(addr + 1);                  /* device ID            */
 141        iobarrier_rw();
 142
 143        switch (value) {
 144        case AMD_ID_LV033C:
 145                info->flash_id += FLASH_AM033C;
 146                info->size = hwc_flash_size();
 147                if (info->size > CONFIG_SYS_MAX_FLASH_SIZE) {
 148                        printf("U-Boot supports only %d MB\n",
 149                               CONFIG_SYS_MAX_FLASH_SIZE);
 150                        info->size = CONFIG_SYS_MAX_FLASH_SIZE;
 151                }
 152                info->sector_count = info->size / 0x10000;
 153                break;                          /* => 4 MB              */
 154
 155        default:
 156                info->flash_id = FLASH_UNKNOWN;
 157                flash_reset (addr);
 158                return (0);                     /* => no or unknown flash */
 159
 160        }
 161
 162        if (!flash_get_offsets (addr, info)) {
 163                flash_reset (addr);
 164                return 0;
 165        }
 166
 167#if 0
 168        /* check for protected sectors */
 169        for (i = 0; i < info->sector_count; i++) {
 170                /* read sector protection at sector address, (A7 .. A0) = 0x02 */
 171                /* D0 = 1 if protected */
 172                value = in8(info->start[i] + 2);
 173                iobarrier_rw();
 174                info->protect[i] = (value & 1) != 0;
 175        }
 176#endif
 177
 178        /*
 179         * Reset bank to read mode
 180         */
 181        flash_reset (addr);
 182
 183        return (info->size);
 184}
 185
 186static int flash_get_offsets (u32 base, flash_info_t *info)
 187{
 188        unsigned int i, size;
 189
 190        switch (info->flash_id & FLASH_TYPEMASK) {
 191        case FLASH_AM033C:
 192                /* set sector offsets for uniform sector type   */
 193                size = info->size / info->sector_count;
 194                for (i = 0; i < info->sector_count; i++) {
 195                        info->start[i] = base + i * size;
 196                }
 197                break;
 198        default:
 199                return 0;
 200        }
 201
 202        return 1;
 203}
 204
 205int flash_erase (flash_info_t *info, int s_first, int s_last)
 206{
 207        volatile u32 addr = info->start[0];
 208        int flag, prot, sect, l_sect;
 209        ulong start, now, last;
 210
 211        if (s_first < 0 || s_first > s_last) {
 212                if (info->flash_id == FLASH_UNKNOWN) {
 213                        printf ("- missing\n");
 214                } else {
 215                        printf ("- no sectors to erase\n");
 216                }
 217                return 1;
 218        }
 219
 220        if (info->flash_id == FLASH_UNKNOWN ||
 221            info->flash_id > FLASH_AMD_COMP) {
 222                printf ("Can't erase unknown flash type %08lx - aborted\n",
 223                        info->flash_id);
 224                return 1;
 225        }
 226
 227        prot = 0;
 228        for (sect=s_first; sect<=s_last; ++sect) {
 229                if (info->protect[sect]) {
 230                        prot++;
 231                }
 232        }
 233
 234        if (prot) {
 235                printf ("- Warning: %d protected sectors will not be erased!\n",
 236                        prot);
 237        } else {
 238                printf ("\n");
 239        }
 240
 241        l_sect = -1;
 242
 243        /* Disable interrupts which might cause a timeout here */
 244        flag = disable_interrupts();
 245
 246        out8(addr + 0x555, 0xAA);
 247        iobarrier_rw();
 248        out8(addr + 0x2AA, 0x55);
 249        iobarrier_rw();
 250        out8(addr + 0x555, 0x80);
 251        iobarrier_rw();
 252        out8(addr + 0x555, 0xAA);
 253        iobarrier_rw();
 254        out8(addr + 0x2AA, 0x55);
 255        iobarrier_rw();
 256
 257        /* Start erase on unprotected sectors */
 258        for (sect = s_first; sect<=s_last; sect++) {
 259                if (info->protect[sect] == 0) { /* not protected */
 260                        addr = info->start[sect];
 261                        out8(addr, 0x30);
 262                        iobarrier_rw();
 263                        l_sect = sect;
 264                }
 265        }
 266
 267        /* re-enable interrupts if necessary */
 268        if (flag)
 269                enable_interrupts();
 270
 271        /* wait at least 80us - let's wait 1 ms */
 272        udelay (1000);
 273
 274        /*
 275         * We wait for the last triggered sector
 276         */
 277        if (l_sect < 0)
 278                goto DONE;
 279
 280        start = get_timer (0);
 281        last  = start;
 282        addr = info->start[l_sect];
 283        while ((in8(addr) & 0x80) != 0x80) {
 284                if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
 285                        printf ("Timeout\n");
 286                        return 1;
 287                }
 288                /* show that we're waiting */
 289                if ((now - last) > 1000) {      /* every second */
 290                        putc ('.');
 291                        last = now;
 292                }
 293                iobarrier_rw();
 294        }
 295
 296DONE:
 297        /* reset to read mode */
 298        flash_reset (info->start[0]);
 299
 300        printf (" done\n");
 301        return 0;
 302}
 303
 304/*
 305 * Copy memory to flash, returns:
 306 * 0 - OK
 307 * 1 - write timeout
 308 * 2 - Flash not erased
 309 */
 310int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 311{
 312        ulong cp, wp, data;
 313        int i, l, rc;
 314
 315        wp = (addr & ~3);       /* get lower word aligned address */
 316
 317        /*
 318         * handle unaligned start bytes
 319         */
 320        if ((l = addr - wp) != 0) {
 321                data = 0;
 322                for (i=0, cp=wp; i<l; ++i, ++cp) {
 323                        data = (data << 8) | (*(uchar *)cp);
 324                }
 325                for (; i<4 && cnt>0; ++i) {
 326                        data = (data << 8) | *src++;
 327                        --cnt;
 328                        ++cp;
 329                }
 330                for (; cnt==0 && i<4; ++i, ++cp) {
 331                        data = (data << 8) | (*(uchar *)cp);
 332                }
 333
 334                if ((rc = write_word(info, wp, data)) != 0) {
 335                        return (rc);
 336                }
 337                wp += 4;
 338        }
 339
 340        /*
 341         * handle word aligned part
 342         */
 343        while (cnt >= 4) {
 344                data = 0;
 345                for (i=0; i<4; ++i) {
 346                        data = (data << 8) | *src++;
 347                }
 348                if ((rc = write_word(info, wp, data)) != 0) {
 349                        return (rc);
 350                }
 351                wp  += 4;
 352                cnt -= 4;
 353        }
 354
 355        if (cnt == 0) {
 356                return (0);
 357        }
 358
 359        /*
 360         * handle unaligned tail bytes
 361         */
 362        data = 0;
 363        for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
 364                data = (data << 8) | *src++;
 365                --cnt;
 366        }
 367        for (; i<4; ++i, ++cp) {
 368                data = (data << 8) | (*(uchar *)cp);
 369        }
 370
 371        return (write_word(info, wp, data));
 372}
 373
 374/*
 375 * Write a word to Flash, returns:
 376 * 0 - OK
 377 * 1 - write timeout
 378 * 2 - Flash not erased
 379 */
 380static int write_word (flash_info_t *info, ulong dest, ulong data)
 381{
 382        volatile u32 addr = info->start[0];
 383        ulong start;
 384        int flag, i;
 385
 386        /* Check if Flash is (sufficiently) erased */
 387        if ((in32(dest) & data) != data) {
 388                return (2);
 389        }
 390        /* Disable interrupts which might cause a timeout here */
 391        flag = disable_interrupts();
 392
 393        /* first, perform an unlock bypass command to speed up flash writes */
 394        out8(addr + 0x555, 0xAA);
 395        iobarrier_rw();
 396        out8(addr + 0x2AA, 0x55);
 397        iobarrier_rw();
 398        out8(addr + 0x555, 0x20);
 399        iobarrier_rw();
 400
 401        /* write each byte out */
 402        for (i = 0; i < 4; i++) {
 403                char *data_ch = (char *)&data;
 404                out8(addr, 0xA0);
 405                iobarrier_rw();
 406                out8(dest+i, data_ch[i]);
 407                iobarrier_rw();
 408                udelay(10); /* XXX */
 409        }
 410
 411        /* we're done, now do an unlock bypass reset */
 412        out8(addr, 0x90);
 413        iobarrier_rw();
 414        out8(addr, 0x00);
 415        iobarrier_rw();
 416
 417        /* re-enable interrupts if necessary */
 418        if (flag)
 419                enable_interrupts();
 420
 421        /* data polling for D7 */
 422        start = get_timer (0);
 423        while ((in32(dest) & 0x80808080) != (data & 0x80808080)) {
 424                if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
 425                        return (1);
 426                }
 427                iobarrier_rw();
 428        }
 429
 430        flash_reset (addr);
 431
 432        return (0);
 433}
 434
 435/*
 436 * Reset bank to read mode
 437 */
 438static void flash_reset (u32 addr)
 439{
 440        out8(addr, 0xF0);       /* reset bank */
 441        iobarrier_rw();
 442}
 443
 444void flash_print_info (flash_info_t *info)
 445{
 446        int i;
 447
 448        if (info->flash_id == FLASH_UNKNOWN) {
 449                printf ("missing or unknown FLASH type\n");
 450                return;
 451        }
 452
 453        switch (info->flash_id & FLASH_VENDMASK) {
 454        case FLASH_MAN_AMD:     printf ("AMD ");                break;
 455        case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
 456        case FLASH_MAN_BM:      printf ("BRIGHT MICRO ");       break;
 457        default:                printf ("Unknown Vendor ");     break;
 458        }
 459
 460        switch (info->flash_id & FLASH_TYPEMASK) {
 461        case FLASH_AM033C:      printf ("AM29LV033C (32 Mbit, uniform sectors)\n");
 462                                break;
 463        default:                printf ("Unknown Chip Type\n");
 464                                break;
 465        }
 466
 467        if (info->size % 0x100000 == 0) {
 468                printf ("  Size: %ld MB in %d Sectors\n",
 469                        info->size / 0x100000, info->sector_count);
 470        }
 471        else if (info->size % 0x400 == 0) {
 472                printf ("  Size: %ld KB in %d Sectors\n",
 473                        info->size / 0x400, info->sector_count);
 474        }
 475        else {
 476                printf ("  Size: %ld B in %d Sectors\n",
 477                        info->size, info->sector_count);
 478        }
 479
 480        printf ("  Sector Start Addresses:");
 481        for (i=0; i<info->sector_count; ++i) {
 482                if ((i % 5) == 0)
 483                        printf ("\n   ");
 484                printf (" %08lX%s",
 485                        info->start[i],
 486                        info->protect[i] ? " (RO)" : "     "
 487                );
 488        }
 489        printf ("\n");
 490}
 491