3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Reinhard Meyer, EMK Elektronik GmbH, r.meyer@emk-elektronik.de
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
31 #if defined (CONFIG_TOP860)
32 typedef unsigned short FLASH_PORT_WIDTH;
33 typedef volatile unsigned short FLASH_PORT_WIDTHV;
34 #define FLASH_ID_MASK 0xFF
36 #define FPW FLASH_PORT_WIDTH
37 #define FPWV FLASH_PORT_WIDTHV
39 #define FLASH_CYCLE1 0x0555
40 #define FLASH_CYCLE2 0x02aa
45 #if defined (CONFIG_TOP5200)
46 typedef unsigned char FLASH_PORT_WIDTH;
47 typedef volatile unsigned char FLASH_PORT_WIDTHV;
48 #define FLASH_ID_MASK 0xFF
50 #define FPW FLASH_PORT_WIDTH
51 #define FPWV FLASH_PORT_WIDTHV
53 #define FLASH_CYCLE1 0x0aaa
54 #define FLASH_CYCLE2 0x0555
59 /*-----------------------------------------------------------------------
62 static ulong flash_get_size(FPWV *addr, flash_info_t *info);
63 static void flash_reset(flash_info_t *info);
64 static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
65 static flash_info_t *flash_get_info(ulong base);
67 /*-----------------------------------------------------------------------
70 * sets up flash_info and returns size of FLASH (bytes)
72 unsigned long flash_init (void)
74 unsigned long size = 0;
76 extern void flash_preinit(void);
77 extern void flash_afterinit(uint, ulong, ulong);
78 ulong flashbase = CFG_FLASH_BASE;
82 /* There is only ONE FLASH device */
83 memset(&flash_info[i], 0, sizeof(flash_info_t));
85 flash_get_size((FPW *)flashbase, &flash_info[i]);
86 size += flash_info[i].size;
88 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
89 /* monitor protection ON by default */
90 flash_protect(FLAG_PROTECT_SET,
92 CFG_MONITOR_BASE+monitor_flash_len-1,
93 flash_get_info(CFG_MONITOR_BASE));
96 #ifdef CFG_ENV_IS_IN_FLASH
97 /* ENV protection ON by default */
98 flash_protect(FLAG_PROTECT_SET,
100 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
101 flash_get_info(CFG_ENV_ADDR));
105 flash_afterinit(i, flash_info[i].start[0], flash_info[i].size);
106 return size ? size : 1;
109 /*-----------------------------------------------------------------------
111 static void flash_reset(flash_info_t *info)
113 FPWV *base = (FPWV *)(info->start[0]);
115 /* Put FLASH back in read mode */
116 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
117 *base = (FPW)0x00FF00FF; /* Intel Read Mode */
118 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
119 *base = (FPW)0x00F000F0; /* AMD Read Mode */
122 /*-----------------------------------------------------------------------
125 static flash_info_t *flash_get_info(ulong base)
130 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
131 info = & flash_info[i];
133 info->start[0] <= base && base <= info->start[0] + info->size - 1)
137 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
140 /*-----------------------------------------------------------------------
143 void flash_print_info (flash_info_t *info)
149 uchar botbootletter[] = "B";
150 uchar topbootletter[] = "T";
151 uchar botboottype[] = "bottom boot sector";
152 uchar topboottype[] = "top boot sector";
154 if (info->flash_id == FLASH_UNKNOWN) {
155 printf ("missing or unknown FLASH type\n");
159 switch (info->flash_id & FLASH_VENDMASK) {
160 case FLASH_MAN_AMD: printf ("AMD "); break;
162 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
163 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
164 case FLASH_MAN_SST: printf ("SST "); break;
165 case FLASH_MAN_STM: printf ("STM "); break;
166 case FLASH_MAN_INTEL: printf ("INTEL "); break;
168 default: printf ("Unknown Vendor "); break;
171 /* check for top or bottom boot, if it applies */
172 if (info->flash_id & FLASH_BTYPE) {
173 boottype = botboottype;
174 bootletter = botbootletter;
177 boottype = topboottype;
178 bootletter = topbootletter;
181 switch (info->flash_id & FLASH_TYPEMASK) {
184 fmt = "29LV160%s (16 Mbit, %s)\n";
187 fmt = "Unknown Chip Type\n";
191 printf (fmt, bootletter, boottype);
193 printf (" Size: %ld MB in %d Sectors\n",
197 printf (" Sector Start Addresses:");
199 for (i=0; i<info->sector_count; ++i) {
202 ulong *flash = (unsigned long *) info->start[i];
209 * Check if whole sector is erased
212 (i != (info->sector_count - 1)) ?
213 (info->start[i + 1] - info->start[i]) >> 2 :
214 (info->start[0] + info->size - info->start[i]) >> 2;
217 flash = (unsigned long *) info->start[i], erased = 1;
218 (flash != (unsigned long *) info->start[i] + size) && erased;
221 erased = *flash == ~0x0UL;
223 printf (" %08lX %s %s",
226 info->protect[i] ? "(RO)" : " ");
232 /*-----------------------------------------------------------------------
236 * The following code cannot be run from FLASH!
239 ulong flash_get_size (FPWV *addr, flash_info_t *info)
244 /* Write auto select command: read Manufacturer ID */
245 /* Write auto select command sequence and test FLASH answer */
246 addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
247 addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
248 addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
250 /* The manufacturer codes are only 1 byte, so just use 1 byte.
251 * This works for any bus width and any FLASH device width.
254 switch (addr[FLASH_ID1] & 0xff) {
256 case (uchar)AMD_MANUFACT:
257 info->flash_id = FLASH_MAN_AMD;
261 case (uchar)INTEL_MANUFACT:
262 info->flash_id = FLASH_MAN_INTEL;
267 printf ("unknown vendor=%x ", addr[FLASH_ID1] & 0xff);
268 info->flash_id = FLASH_UNKNOWN;
269 info->sector_count = 0;
274 /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
275 if (info->flash_id != FLASH_UNKNOWN) switch ((FPW)addr[FLASH_ID2]) {
277 case (FPW)AMD_ID_LV160B:
278 info->flash_id += FLASH_AM160B;
279 info->sector_count = 35;
280 info->size = 0x00200000;
286 info->start[0] = (ulong)addr + offset;
287 info->start[1] = (ulong)addr + offset + 0x4000;
288 info->start[2] = (ulong)addr + offset + 0x6000;
289 info->start[3] = (ulong)addr + offset + 0x8000;
290 for (i = 4; i < info->sector_count; i++)
292 info->start[i] = (ulong)addr + offset + 0x10000 * (i-3);
297 printf ("unknown AMD device=%x ", (FPW)addr[FLASH_ID2]);
298 info->flash_id = FLASH_UNKNOWN;
299 info->sector_count = 0;
301 return (0); /* => no or unknown flash */
304 /* Put FLASH back in read mode */
310 /*-----------------------------------------------------------------------
313 int flash_erase (flash_info_t *info, int s_first, int s_last)
316 int flag, prot, sect;
317 int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
318 ulong start, now, last;
321 if ((s_first < 0) || (s_first > s_last)) {
322 if (info->flash_id == FLASH_UNKNOWN) {
323 printf ("- missing\n");
325 printf ("- no sectors to erase\n");
330 switch (info->flash_id & FLASH_TYPEMASK) {
335 printf ("Can't erase unknown flash type %08lx - aborted\n",
341 for (sect=s_first; sect<=s_last; ++sect) {
342 if (info->protect[sect]) {
348 printf ("- Warning: %d protected sectors will not be erased!\n",
356 /* Start erase on unprotected sectors */
357 for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
359 if (info->protect[sect] != 0) /* protected, skip it */
362 /* Disable interrupts which might cause a timeout here */
363 flag = disable_interrupts();
365 addr = (FPWV *)(info->start[sect]);
367 *addr = (FPW)0x00500050; /* clear status register */
368 *addr = (FPW)0x00200020; /* erase setup */
369 *addr = (FPW)0x00D000D0; /* erase confirm */
372 /* must be AMD style if not Intel */
373 FPWV *base; /* first address in bank */
375 base = (FPWV *)(info->start[0]);
376 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
377 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
378 base[FLASH_CYCLE1] = (FPW)0x00800080; /* erase mode */
379 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
380 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
381 *addr = (FPW)0x00300030; /* erase sector */
384 /* re-enable interrupts if necessary */
388 start = get_timer(0);
390 /* wait at least 50us for AMD, 80us for Intel.
395 while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
396 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
397 printf ("Timeout\n");
401 *addr = (FPW)0x00B000B0;
404 flash_reset(info); /* reset to read mode */
405 rcode = 1; /* failed */
409 /* show that we're waiting */
410 if ((get_timer(last)) > CFG_HZ) {/* every second */
416 /* show that we're waiting */
417 if ((get_timer(last)) > CFG_HZ) { /* every second */
422 flash_reset(info); /* reset to read mode */
429 /*-----------------------------------------------------------------------
430 * Copy memory to flash, returns:
433 * 2 - Flash not erased
435 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
437 FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
438 int bytes; /* number of bytes to program in current word */
439 int left; /* number of bytes left to program */
442 for (left = cnt, res = 0;
443 left > 0 && res == 0;
444 addr += sizeof(data), left -= sizeof(data) - bytes) {
446 bytes = addr & (sizeof(data) - 1);
447 addr &= ~(sizeof(data) - 1);
449 /* combine source and destination data so can program
450 * an entire word of 16 or 32 bits
452 for (i = 0; i < sizeof(data); i++) {
454 if (i < bytes || i - bytes >= left )
455 data += *((uchar *)addr + i);
460 /* write one word to the flash */
461 switch (info->flash_id & FLASH_VENDMASK) {
463 res = write_word_amd(info, (FPWV *)addr, data);
466 /* unknown flash type, error! */
467 printf ("missing or unknown FLASH type\n");
468 res = 1; /* not really a timeout, but gives error */
476 /*-----------------------------------------------------------------------
477 * Write a word to Flash for AMD FLASH
478 * A word is 16 or 32 bits, whichever the bus width of the flash bank
479 * (not an individual chip) is.
484 * 2 - Flash not erased
486 static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
490 int res = 0; /* result, assume success */
491 FPWV *base; /* first address in flash bank */
493 /* Check if Flash is (sufficiently) erased */
494 if ((*dest & data) != data) {
499 base = (FPWV *)(info->start[0]);
501 /* Disable interrupts which might cause a timeout here */
502 flag = disable_interrupts();
504 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
505 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
506 base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
508 *dest = data; /* start programming the data */
510 /* re-enable interrupts if necessary */
514 start = get_timer (0);
516 /* data polling for D7 */
517 while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
518 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
519 *dest = (FPW)0x00F000F0; /* reset bank */