2 * (C) Copyright 2001, 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Flash Routines for Intel devices
7 *--------------------------------------------------------------------
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,
31 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
33 /*-----------------------------------------------------------------------
35 ulong flash_int_get_size (volatile unsigned long *baseaddr,
39 unsigned long flashtest_h, flashtest_l;
41 info->sector_count = info->size = 0;
42 info->flash_id = FLASH_UNKNOWN;
44 /* Write identify command sequence and test FLASH answer
46 baseaddr[0] = 0x00900090;
47 baseaddr[1] = 0x00900090;
49 flashtest_h = baseaddr[0]; /* manufacturer ID */
50 flashtest_l = baseaddr[1];
52 if (flashtest_h != INTEL_MANUFACT || flashtest_l != INTEL_MANUFACT)
53 return (0); /* no or unknown flash */
55 flashtest_h = baseaddr[2]; /* device ID */
56 flashtest_l = baseaddr[3];
58 if (flashtest_h != flashtest_l)
61 switch (flashtest_h) {
62 case INTEL_ID_28F160C3B:
63 info->flash_id = FLASH_28F160C3B;
64 info->sector_count = 39;
65 info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
67 case INTEL_ID_28F160F3B:
68 info->flash_id = FLASH_28F160F3B;
69 info->sector_count = 39;
70 info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
73 return (0); /* no or unknown flash */
76 info->flash_id |= INTEL_MANUFACT << 16; /* set manufacturer offset */
78 if (info->flash_id & FLASH_BTYPE) {
79 volatile unsigned long *tmp = baseaddr;
81 /* set up sector start adress table (bottom sector type)
82 * AND unlock the sectors (if our chip is 160C3)
84 for (i = 0; i < info->sector_count; i++) {
85 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F160C3B) {
91 info->start[i] = (uint) tmp;
92 tmp += i < 8 ? 0x2000 : 0x10000; /* pointer arith */
96 memset (info->protect, 0, info->sector_count);
98 baseaddr[0] = 0x00FF00FF;
99 baseaddr[1] = 0x00FF00FF;
104 static ulong flash_amd_get_size (vu_char *addr, flash_info_t *info)
108 ulong base = (ulong)addr;
110 /* Write auto select command: read Manufacturer ID */
118 devid = addr[1] & 0xff;
120 /* only support AMD */
121 if (vendor != 0x01) {
128 if (devid == AMD_ID_F040B) {
129 info->flash_id = vendor << 16 | devid;
130 info->sector_count = 8;
131 info->size = info->sector_count * 0x10000;
133 else if (devid == AMD_ID_F080B) {
134 info->flash_id = vendor << 16 | devid;
135 info->sector_count = 16;
136 info->size = 4 * info->sector_count * 0x10000;
138 else if (devid == AMD_ID_F016D) {
139 info->flash_id = vendor << 16 | devid;
140 info->sector_count = 32;
141 info->size = 4 * info->sector_count * 0x10000;
144 printf ("## Unknown Flash Type: %02x\n", devid);
148 /* check for protected sectors */
149 for (i = 0; i < info->sector_count; i++) {
150 /* sector base address */
151 info->start[i] = base + i * (info->size / info->sector_count);
152 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
153 /* D0 = 1 if protected */
154 addr = (volatile unsigned char *)(info->start[i]);
155 info->protect[i] = addr[2] & 1;
159 * Prevent writes to uninitialized FLASH.
161 if (info->flash_id != FLASH_UNKNOWN) {
162 addr = (vu_char *)info->start[0];
163 addr[0] = 0xF0; /* reset bank */
170 /*-----------------------------------------------------------------------
172 unsigned long flash_init (void)
174 unsigned long size_b0 = 0;
175 unsigned long size_b1 = 0;
178 /* Init: no FLASHes known
180 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
181 flash_info[i].flash_id = FLASH_UNKNOWN;
184 /* Disable flash protection */
185 CPU86_BCR |= (CPU86_BCR_FWPT | CPU86_BCR_FWRE);
187 /* Static FLASH Bank configuration here (only one bank) */
189 size_b0 = flash_int_get_size ((ulong *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
190 size_b1 = flash_amd_get_size ((uchar *) CONFIG_SYS_BOOTROM_BASE, &flash_info[1]);
192 if (size_b0 > 0 || size_b1 > 0) {
198 print_size (size_b0, (size_b1 > 0) ? ", " : ") ");
203 print_size (size_b1, ") ");
207 printf ("## No FLASH found.\n");
210 /* protect monitor and environment sectors
213 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_BOOTROM_BASE
215 /* If U-Boot is booted from ROM the CONFIG_SYS_MONITOR_BASE > CONFIG_SYS_FLASH_BASE
216 * but we shouldn't protect it.
219 flash_protect (FLAG_PROTECT_SET,
220 CONFIG_SYS_MONITOR_BASE,
221 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[1]
225 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
226 flash_protect (FLAG_PROTECT_SET,
227 CONFIG_SYS_MONITOR_BASE,
228 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]
233 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
234 # ifndef CONFIG_ENV_SIZE
235 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
237 # if CONFIG_ENV_ADDR >= CONFIG_SYS_BOOTROM_BASE
239 flash_protect (FLAG_PROTECT_SET,
241 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[1]);
244 flash_protect (FLAG_PROTECT_SET,
246 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
250 return (size_b0 + size_b1);
253 /*-----------------------------------------------------------------------
255 void flash_print_info (flash_info_t * info)
259 if (info->flash_id == FLASH_UNKNOWN) {
260 printf ("missing or unknown FLASH type\n");
264 switch ((info->flash_id >> 16) & 0xff) {
272 printf ("Unknown Vendor ");
276 switch (info->flash_id & FLASH_TYPEMASK) {
277 case FLASH_28F160C3B:
278 printf ("28F160C3B (16 Mbit, bottom sector)\n");
280 case FLASH_28F160F3B:
281 printf ("28F160F3B (16 Mbit, bottom sector)\n");
284 printf ("AM29F040B (4 Mbit)\n");
287 printf ("Unknown Chip Type\n");
291 if (info->size < 0x100000)
292 printf (" Size: %ld KB in %d Sectors\n",
293 info->size >> 10, info->sector_count);
295 printf (" Size: %ld MB in %d Sectors\n",
296 info->size >> 20, info->sector_count);
298 printf (" Sector Start Addresses:");
299 for (i = 0; i < info->sector_count; ++i) {
304 info->protect[i] ? " (RO)" : " "
310 /*-----------------------------------------------------------------------
312 int flash_erase (flash_info_t * info, int s_first, int s_last)
314 vu_char *addr = (vu_char *)(info->start[0]);
315 int flag, prot, sect, l_sect;
316 ulong start, now, last;
318 if ((s_first < 0) || (s_first > s_last)) {
319 if (info->flash_id == FLASH_UNKNOWN) {
320 printf ("- missing\n");
322 printf ("- no sectors to erase\n");
328 for (sect = s_first; sect <= s_last; sect++) {
329 if (info->protect[sect])
334 printf ("- Warning: %d protected sectors will not be erased!\n",
340 /* Check the type of erased flash
342 if (info->flash_id >> 16 == 0x1) {
347 /* Disable interrupts which might cause a timeout here */
348 flag = disable_interrupts();
356 /* wait at least 80us - let's wait 1 ms */
359 /* Start erase on unprotected sectors */
360 for (sect = s_first; sect<=s_last; sect++) {
361 if (info->protect[sect] == 0) { /* not protected */
362 addr = (vu_char *)(info->start[sect]);
368 /* re-enable interrupts if necessary */
372 /* wait at least 80us - let's wait 1 ms */
376 * We wait for the last triggered sector
381 start = get_timer (0);
383 addr = (vu_char *)(info->start[l_sect]);
384 while ((addr[0] & 0x80) != 0x80) {
385 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
386 printf ("Timeout\n");
389 /* show that we're waiting */
390 if ((now - last) > 1000) { /* every second */
397 /* reset to read mode */
398 addr = (volatile unsigned char *)info->start[0];
399 addr[0] = 0xF0; /* reset bank */
405 /* Start erase on unprotected sectors
407 for (sect = s_first; sect <= s_last; sect++) {
408 volatile ulong *addr =
409 (volatile unsigned long *) info->start[sect];
411 start = get_timer (0);
413 if (info->protect[sect] == 0) {
414 /* Disable interrupts which might cause a timeout here
416 flag = disable_interrupts ();
420 addr[0] = 0x00200020;
421 addr[1] = 0x00200020;
422 addr[0] = 0x00D000D0;
423 addr[1] = 0x00D000D0;
425 /* re-enable interrupts if necessary
428 enable_interrupts ();
430 /* wait at least 80us - let's wait 1 ms
435 while ((addr[0] & 0x00800080) != 0x00800080 ||
436 (addr[1] & 0x00800080) != 0x00800080) {
437 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
438 printf ("Timeout (erase suspended!)\n");
441 addr[0] = 0x00B000B0;
442 addr[1] = 0x00B000B0;
445 /* show that we're waiting
447 if ((now - last) > 1000) { /* every second */
452 if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
453 printf ("*** ERROR: erase failed!\n");
457 /* Clear status register and reset to read mode
459 addr[0] = 0x00500050;
460 addr[1] = 0x00500050;
461 addr[0] = 0x00FF00FF;
462 addr[1] = 0x00FF00FF;
472 static int write_word (flash_info_t *, volatile unsigned long *, ulong);
473 static int write_byte (flash_info_t *info, ulong dest, uchar data);
475 /*-----------------------------------------------------------------------
476 * Copy memory to flash, returns:
479 * 2 - Flash not erased
481 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
484 int i, l, rc, cc = cnt, res = 0;
486 if (info->flash_id >> 16 == 0x1) {
488 /* Write to AMD 8-bit flash
491 if ((rc = write_byte(info, addr, *src)) != 0) {
502 /* Write to Intel 64-bit flash
504 for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
508 for (i = 0; i < 4; i++) {
509 v = (v << 8) + (i < l || i - l >= cc ?
510 *((unsigned char *) addr + i) : *src++);
513 if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
521 /*-----------------------------------------------------------------------
522 * Write a word to Flash, returns:
525 * 2 - Flash not erased
527 static int write_word (flash_info_t * info, volatile unsigned long *addr,
533 /* Check if Flash is (sufficiently) erased
535 if ((*addr & data) != data)
538 /* Disable interrupts which might cause a timeout here
540 flag = disable_interrupts ();
545 /* re-enable interrupts if necessary
548 enable_interrupts ();
550 start = get_timer (0);
551 while ((*addr & 0x00800080) != 0x00800080) {
552 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
561 if (*addr & 0x00220022) {
562 printf ("*** ERROR: program failed!\n");
567 /* Clear status register and reset to read mode
575 /*-----------------------------------------------------------------------
576 * Write a byte to Flash, returns:
579 * 2 - Flash not erased
581 static int write_byte (flash_info_t *info, ulong dest, uchar data)
583 vu_char *addr = (vu_char *)(info->start[0]);
587 /* Check if Flash is (sufficiently) erased */
588 if ((*((vu_char *)dest) & data) != data) {
591 /* Disable interrupts which might cause a timeout here */
592 flag = disable_interrupts();
598 *((vu_char *)dest) = data;
600 /* re-enable interrupts if necessary */
604 /* data polling for D7 */
605 start = get_timer (0);
606 while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
607 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
614 /*-----------------------------------------------------------------------