3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
28 typedef unsigned char FLASH_PORT_WIDTH;
29 typedef volatile unsigned char FLASH_PORT_WIDTHV;
30 #define FLASH_ID_MASK 0xFF
32 #define FPW FLASH_PORT_WIDTH
33 #define FPWV FLASH_PORT_WIDTHV
35 #define FLASH_CYCLE1 0x0aaa
36 #define FLASH_CYCLE2 0x0555
38 /*-----------------------------------------------------------------------
41 static ulong flash_get_size(FPWV *addr, flash_info_t *info);
42 static void flash_reset(flash_info_t *info);
43 static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
44 static flash_info_t *flash_get_info(ulong base);
46 /*-----------------------------------------------------------------------
49 * sets up flash_info and returns size of FLASH (bytes)
51 unsigned long flash_init (void)
53 unsigned long size = 0;
55 extern void flash_preinit(void);
56 extern void flash_afterinit(uint, ulong, ulong);
57 ulong flashbase = CFG_FLASH_BASE;
61 /* There is only ONE FLASH device */
62 memset(&flash_info[0], 0, sizeof(flash_info_t));
64 flash_get_size((FPW *)flashbase, &flash_info[0]);
65 size += flash_info[0].size;
67 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
68 /* monitor protection ON by default */
69 flash_protect(FLAG_PROTECT_SET,
71 CFG_MONITOR_BASE+monitor_flash_len-1,
72 flash_get_info(CFG_MONITOR_BASE));
75 #ifdef CFG_ENV_IS_IN_FLASH
76 /* ENV protection ON by default */
77 flash_protect(FLAG_PROTECT_SET,
79 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
80 flash_get_info(CFG_ENV_ADDR));
84 flash_afterinit(0, flash_info[0].start[0], flash_info[0].size);
85 return size ? size : 1;
88 /*-----------------------------------------------------------------------
90 static void flash_reset(flash_info_t *info)
92 FPWV *base = (FPWV *)(info->start[0]);
94 /* Put FLASH back in read mode */
95 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
96 *base = (FPW)0x00FF00FF; /* Intel Read Mode */
97 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
98 *base = (FPW)0x00F000F0; /* AMD Read Mode */
101 /*-----------------------------------------------------------------------
104 static flash_info_t *flash_get_info(ulong base)
109 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
110 info = & flash_info[i];
112 info->start[0] <= base && base <= info->start[0] + info->size - 1)
116 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
119 /*-----------------------------------------------------------------------
122 void flash_print_info (flash_info_t *info)
128 uchar botbootletter[] = "B";
129 uchar topbootletter[] = "T";
130 uchar botboottype[] = "bottom boot sector";
131 uchar topboottype[] = "top boot sector";
133 if (info->flash_id == FLASH_UNKNOWN) {
134 printf ("missing or unknown FLASH type\n");
138 switch (info->flash_id & FLASH_VENDMASK) {
139 case FLASH_MAN_AMD: printf ("AMD "); break;
141 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
142 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
143 case FLASH_MAN_SST: printf ("SST "); break;
144 case FLASH_MAN_STM: printf ("STM "); break;
145 case FLASH_MAN_INTEL: printf ("INTEL "); break;
147 default: printf ("Unknown Vendor "); break;
150 /* check for top or bottom boot, if it applies */
151 if (info->flash_id & FLASH_BTYPE) {
152 boottype = botboottype;
153 bootletter = botbootletter;
155 boottype = topboottype;
156 bootletter = topbootletter;
159 switch (info->flash_id & FLASH_TYPEMASK) {
162 fmt = "29LV160%s (16 Mbit, %s)\n";
164 case FLASH_AMDLV065D:
165 fmt = "29LV065 (64 Mbit, uniform sectors)\n";
168 fmt = "Unknown Chip Type\n";
172 printf (fmt, bootletter, boottype);
174 printf (" Size: %ld MB in %d Sectors\n",
178 printf (" Sector Start Addresses:");
180 for (i=0; i<info->sector_count; ++i) {
185 printf (" %08lX%s", info->start[i],
186 info->protect[i] ? " (RO)" : " ");
192 /*-----------------------------------------------------------------------
196 * The following code cannot be run from FLASH!
199 ulong flash_get_size (FPWV *addr, flash_info_t *info)
204 /* Write auto select command: read Manufacturer ID */
205 /* Write auto select command sequence and test FLASH answer */
206 addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
207 addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
208 addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
210 /* The manufacturer codes are only 1 byte, so just use 1 byte.
211 * This works for any bus width and any FLASH device width.
214 switch (addr[0] & 0xff) {
216 case (uchar)AMD_MANUFACT:
217 info->flash_id = FLASH_MAN_AMD;
221 case (uchar)INTEL_MANUFACT:
222 info->flash_id = FLASH_MAN_INTEL;
227 printf ("unknown vendor=%x ", addr[0] & 0xff);
228 info->flash_id = FLASH_UNKNOWN;
229 info->sector_count = 0;
234 /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
235 if (info->flash_id != FLASH_UNKNOWN) switch ((FPW)addr[2]) {
237 case (FPW)AMD_ID_LV160B:
238 info->flash_id += FLASH_AM160B;
239 info->sector_count = 35;
240 info->size = 0x00200000;
242 info->start[0] = (ulong)addr + offset;
243 info->start[1] = (ulong)addr + offset + 0x4000;
244 info->start[2] = (ulong)addr + offset + 0x6000;
245 info->start[3] = (ulong)addr + offset + 0x8000;
246 for (i = 4; i < info->sector_count; i++) {
247 info->start[i] = (ulong)addr + offset + 0x10000 * (i-3);
251 case (FPW)AMD_ID_LV065D:
252 info->flash_id += FLASH_AMDLV065D;
253 info->sector_count = 128;
254 info->size = 0x00800000;
256 for (i = 0; i < info->sector_count; i++)
257 info->start[i] = (ulong)addr + offset + (i * 0x10000);
258 break; /* => 8 or 16 MB */
261 printf ("unknown AMD device=%x ", (FPW)addr[2]);
262 info->flash_id = FLASH_UNKNOWN;
263 info->sector_count = 0;
265 return (0); /* => no or unknown flash */
268 /* Put FLASH back in read mode */
274 /*-----------------------------------------------------------------------
277 int flash_erase (flash_info_t *info, int s_first, int s_last)
280 int flag, prot, sect;
281 int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
282 ulong start, now, last;
285 if ((s_first < 0) || (s_first > s_last)) {
286 if (info->flash_id == FLASH_UNKNOWN) {
287 printf ("- missing\n");
289 printf ("- no sectors to erase\n");
294 switch (info->flash_id & FLASH_TYPEMASK) {
295 case FLASH_AMDLV065D:
299 printf ("Can't erase unknown flash type %08lx - aborted\n",
305 for (sect=s_first; sect<=s_last; ++sect) {
306 if (info->protect[sect]) {
312 printf ("- Warning: %d protected sectors will not be erased!\n",
320 /* Start erase on unprotected sectors */
321 for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
323 if (info->protect[sect] != 0) /* protected, skip it */
326 /* Disable interrupts which might cause a timeout here */
327 flag = disable_interrupts();
329 addr = (FPWV *)(info->start[sect]);
331 *addr = (FPW)0x00500050; /* clear status register */
332 *addr = (FPW)0x00200020; /* erase setup */
333 *addr = (FPW)0x00D000D0; /* erase confirm */
336 /* must be AMD style if not Intel */
337 FPWV *base; /* first address in bank */
339 base = (FPWV *)(info->start[0]);
340 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
341 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
342 base[FLASH_CYCLE1] = (FPW)0x00800080; /* erase mode */
343 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
344 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
345 *addr = (FPW)0x00300030; /* erase sector */
348 /* re-enable interrupts if necessary */
352 start = get_timer(0);
354 /* wait at least 50us for AMD, 80us for Intel.
359 while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
360 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
361 printf ("Timeout\n");
365 *addr = (FPW)0x00B000B0;
368 flash_reset(info); /* reset to read mode */
369 rcode = 1; /* failed */
373 /* show that we're waiting */
374 if ((get_timer(last)) > CFG_HZ) {/* every second */
380 /* show that we're waiting */
381 if ((get_timer(last)) > CFG_HZ) { /* every second */
386 flash_reset(info); /* reset to read mode */
393 /*-----------------------------------------------------------------------
394 * Copy memory to flash, returns:
397 * 2 - Flash not erased
399 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
401 FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
402 int bytes; /* number of bytes to program in current word */
403 int left; /* number of bytes left to program */
406 for (left = cnt, res = 0;
407 left > 0 && res == 0;
408 addr += sizeof(data), left -= sizeof(data) - bytes) {
410 bytes = addr & (sizeof(data) - 1);
411 addr &= ~(sizeof(data) - 1);
413 /* combine source and destination data so can program
414 * an entire word of 16 or 32 bits
416 for (i = 0; i < sizeof(data); i++) {
418 if (i < bytes || i - bytes >= left )
419 data += *((uchar *)addr + i);
424 /* write one word to the flash */
425 switch (info->flash_id & FLASH_VENDMASK) {
427 res = write_word_amd(info, (FPWV *)addr, data);
430 /* unknown flash type, error! */
431 printf ("missing or unknown FLASH type\n");
432 res = 1; /* not really a timeout, but gives error */
440 /*-----------------------------------------------------------------------
441 * Write a word to Flash for AMD FLASH
442 * A word is 16 or 32 bits, whichever the bus width of the flash bank
443 * (not an individual chip) is.
448 * 2 - Flash not erased
450 static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
454 int res = 0; /* result, assume success */
455 FPWV *base; /* first address in flash bank */
457 /* Check if Flash is (sufficiently) erased */
458 if ((*dest & data) != data) {
463 base = (FPWV *)(info->start[0]);
465 /* Disable interrupts which might cause a timeout here */
466 flag = disable_interrupts();
468 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
469 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
470 base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
472 *dest = data; /* start programming the data */
474 /* re-enable interrupts if necessary */
478 start = get_timer (0);
480 /* data polling for D7 */
481 while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
482 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
483 *dest = (FPW)0x00F000F0; /* reset bank */