2 * (C) Copyright 2001, 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Flash Routines for Intel devices
7 *--------------------------------------------------------------------
8 * SPDX-License-Identifier: GPL-2.0+
15 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
17 /*-----------------------------------------------------------------------
19 ulong flash_int_get_size (volatile unsigned long *baseaddr,
23 unsigned long flashtest_h, flashtest_l;
25 info->sector_count = info->size = 0;
26 info->flash_id = FLASH_UNKNOWN;
28 /* Write identify command sequence and test FLASH answer
30 baseaddr[0] = 0x00900090;
31 baseaddr[1] = 0x00900090;
33 flashtest_h = baseaddr[0]; /* manufacturer ID */
34 flashtest_l = baseaddr[1];
36 if (flashtest_h != INTEL_MANUFACT || flashtest_l != INTEL_MANUFACT)
37 return (0); /* no or unknown flash */
39 flashtest_h = baseaddr[2]; /* device ID */
40 flashtest_l = baseaddr[3];
42 if (flashtest_h != flashtest_l)
45 switch (flashtest_h) {
46 case INTEL_ID_28F160C3B:
47 info->flash_id = FLASH_28F160C3B;
48 info->sector_count = 39;
49 info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
51 case INTEL_ID_28F160F3B:
52 info->flash_id = FLASH_28F160F3B;
53 info->sector_count = 39;
54 info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
57 return (0); /* no or unknown flash */
60 info->flash_id |= INTEL_MANUFACT << 16; /* set manufacturer offset */
62 if (info->flash_id & FLASH_BTYPE) {
63 volatile unsigned long *tmp = baseaddr;
65 /* set up sector start adress table (bottom sector type)
66 * AND unlock the sectors (if our chip is 160C3)
68 for (i = 0; i < info->sector_count; i++) {
69 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F160C3B) {
75 info->start[i] = (uint) tmp;
76 tmp += i < 8 ? 0x2000 : 0x10000; /* pointer arith */
80 memset (info->protect, 0, info->sector_count);
82 baseaddr[0] = 0x00FF00FF;
83 baseaddr[1] = 0x00FF00FF;
88 static ulong flash_amd_get_size (vu_char *addr, flash_info_t *info)
92 ulong base = (ulong)addr;
94 /* Write auto select command: read Manufacturer ID */
102 devid = addr[1] & 0xff;
104 /* only support AMD */
105 if (vendor != 0x01) {
112 if (devid == AMD_ID_F040B) {
113 info->flash_id = vendor << 16 | devid;
114 info->sector_count = 8;
115 info->size = info->sector_count * 0x10000;
117 else if (devid == AMD_ID_F080B) {
118 info->flash_id = vendor << 16 | devid;
119 info->sector_count = 16;
120 info->size = 4 * info->sector_count * 0x10000;
122 else if (devid == AMD_ID_F016D) {
123 info->flash_id = vendor << 16 | devid;
124 info->sector_count = 32;
125 info->size = 4 * info->sector_count * 0x10000;
128 printf ("## Unknown Flash Type: %02x\n", devid);
132 /* check for protected sectors */
133 for (i = 0; i < info->sector_count; i++) {
134 /* sector base address */
135 info->start[i] = base + i * (info->size / info->sector_count);
136 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
137 /* D0 = 1 if protected */
138 addr = (volatile unsigned char *)(info->start[i]);
139 info->protect[i] = addr[2] & 1;
143 * Prevent writes to uninitialized FLASH.
145 if (info->flash_id != FLASH_UNKNOWN) {
146 addr = (vu_char *)info->start[0];
147 addr[0] = 0xF0; /* reset bank */
154 /*-----------------------------------------------------------------------
156 unsigned long flash_init (void)
158 unsigned long size_b0 = 0;
159 unsigned long size_b1 = 0;
162 /* Init: no FLASHes known
164 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
165 flash_info[i].flash_id = FLASH_UNKNOWN;
168 /* Disable flash protection */
169 CPU86_BCR |= (CPU86_BCR_FWPT | CPU86_BCR_FWRE);
171 /* Static FLASH Bank configuration here (only one bank) */
173 size_b0 = flash_int_get_size ((ulong *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
174 size_b1 = flash_amd_get_size ((uchar *) CONFIG_SYS_BOOTROM_BASE, &flash_info[1]);
176 if (size_b0 > 0 || size_b1 > 0) {
182 print_size (size_b0, (size_b1 > 0) ? ", " : ") ");
187 print_size (size_b1, ") ");
191 printf ("## No FLASH found.\n");
194 /* protect monitor and environment sectors
197 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_BOOTROM_BASE
199 /* If U-Boot is booted from ROM the CONFIG_SYS_MONITOR_BASE > CONFIG_SYS_FLASH_BASE
200 * but we shouldn't protect it.
203 flash_protect (FLAG_PROTECT_SET,
204 CONFIG_SYS_MONITOR_BASE,
205 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[1]
209 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
210 flash_protect (FLAG_PROTECT_SET,
211 CONFIG_SYS_MONITOR_BASE,
212 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]
217 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
218 # ifndef CONFIG_ENV_SIZE
219 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
221 # if CONFIG_ENV_ADDR >= CONFIG_SYS_BOOTROM_BASE
223 flash_protect (FLAG_PROTECT_SET,
225 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[1]);
228 flash_protect (FLAG_PROTECT_SET,
230 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
234 return (size_b0 + size_b1);
237 /*-----------------------------------------------------------------------
239 void flash_print_info (flash_info_t * info)
243 if (info->flash_id == FLASH_UNKNOWN) {
244 printf ("missing or unknown FLASH type\n");
248 switch ((info->flash_id >> 16) & 0xff) {
256 printf ("Unknown Vendor ");
260 switch (info->flash_id & FLASH_TYPEMASK) {
261 case FLASH_28F160C3B:
262 printf ("28F160C3B (16 Mbit, bottom sector)\n");
264 case FLASH_28F160F3B:
265 printf ("28F160F3B (16 Mbit, bottom sector)\n");
268 printf ("AM29F040B (4 Mbit)\n");
271 printf ("Unknown Chip Type\n");
275 if (info->size < 0x100000)
276 printf (" Size: %ld KB in %d Sectors\n",
277 info->size >> 10, info->sector_count);
279 printf (" Size: %ld MB in %d Sectors\n",
280 info->size >> 20, info->sector_count);
282 printf (" Sector Start Addresses:");
283 for (i = 0; i < info->sector_count; ++i) {
288 info->protect[i] ? " (RO)" : " "
294 /*-----------------------------------------------------------------------
296 int flash_erase (flash_info_t * info, int s_first, int s_last)
298 vu_char *addr = (vu_char *)(info->start[0]);
299 int flag, prot, sect, l_sect;
300 ulong start, now, last;
302 if ((s_first < 0) || (s_first > s_last)) {
303 if (info->flash_id == FLASH_UNKNOWN) {
304 printf ("- missing\n");
306 printf ("- no sectors to erase\n");
312 for (sect = s_first; sect <= s_last; sect++) {
313 if (info->protect[sect])
318 printf ("- Warning: %d protected sectors will not be erased!\n",
324 /* Check the type of erased flash
326 if (info->flash_id >> 16 == 0x1) {
331 /* Disable interrupts which might cause a timeout here */
332 flag = disable_interrupts();
340 /* wait at least 80us - let's wait 1 ms */
343 /* Start erase on unprotected sectors */
344 for (sect = s_first; sect<=s_last; sect++) {
345 if (info->protect[sect] == 0) { /* not protected */
346 addr = (vu_char *)(info->start[sect]);
352 /* re-enable interrupts if necessary */
356 /* wait at least 80us - let's wait 1 ms */
360 * We wait for the last triggered sector
365 start = get_timer (0);
367 addr = (vu_char *)(info->start[l_sect]);
368 while ((addr[0] & 0x80) != 0x80) {
369 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
370 printf ("Timeout\n");
373 /* show that we're waiting */
374 if ((now - last) > 1000) { /* every second */
381 /* reset to read mode */
382 addr = (volatile unsigned char *)info->start[0];
383 addr[0] = 0xF0; /* reset bank */
389 /* Start erase on unprotected sectors
391 for (sect = s_first; sect <= s_last; sect++) {
392 volatile ulong *addr =
393 (volatile unsigned long *) info->start[sect];
395 start = get_timer (0);
397 if (info->protect[sect] == 0) {
398 /* Disable interrupts which might cause a timeout here
400 flag = disable_interrupts ();
404 addr[0] = 0x00200020;
405 addr[1] = 0x00200020;
406 addr[0] = 0x00D000D0;
407 addr[1] = 0x00D000D0;
409 /* re-enable interrupts if necessary
412 enable_interrupts ();
414 /* wait at least 80us - let's wait 1 ms
419 while ((addr[0] & 0x00800080) != 0x00800080 ||
420 (addr[1] & 0x00800080) != 0x00800080) {
421 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
422 printf ("Timeout (erase suspended!)\n");
425 addr[0] = 0x00B000B0;
426 addr[1] = 0x00B000B0;
429 /* show that we're waiting
431 if ((now - last) > 1000) { /* every second */
436 if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
437 printf ("*** ERROR: erase failed!\n");
441 /* Clear status register and reset to read mode
443 addr[0] = 0x00500050;
444 addr[1] = 0x00500050;
445 addr[0] = 0x00FF00FF;
446 addr[1] = 0x00FF00FF;
456 static int write_word (flash_info_t *, volatile unsigned long *, ulong);
457 static int write_byte (flash_info_t *info, ulong dest, uchar data);
459 /*-----------------------------------------------------------------------
460 * Copy memory to flash, returns:
463 * 2 - Flash not erased
465 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
468 int i, l, rc, cc = cnt, res = 0;
470 if (info->flash_id >> 16 == 0x1) {
472 /* Write to AMD 8-bit flash
475 if ((rc = write_byte(info, addr, *src)) != 0) {
486 /* Write to Intel 64-bit flash
488 for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
492 for (i = 0; i < 4; i++) {
493 v = (v << 8) + (i < l || i - l >= cc ?
494 *((unsigned char *) addr + i) : *src++);
497 if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
505 /*-----------------------------------------------------------------------
506 * Write a word to Flash, returns:
509 * 2 - Flash not erased
511 static int write_word (flash_info_t * info, volatile unsigned long *addr,
517 /* Check if Flash is (sufficiently) erased
519 if ((*addr & data) != data)
522 /* Disable interrupts which might cause a timeout here
524 flag = disable_interrupts ();
529 /* re-enable interrupts if necessary
532 enable_interrupts ();
534 start = get_timer (0);
535 while ((*addr & 0x00800080) != 0x00800080) {
536 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
545 if (*addr & 0x00220022) {
546 printf ("*** ERROR: program failed!\n");
551 /* Clear status register and reset to read mode
559 /*-----------------------------------------------------------------------
560 * Write a byte to Flash, returns:
563 * 2 - Flash not erased
565 static int write_byte (flash_info_t *info, ulong dest, uchar data)
567 vu_char *addr = (vu_char *)(info->start[0]);
571 /* Check if Flash is (sufficiently) erased */
572 if ((*((vu_char *)dest) & data) != data) {
575 /* Disable interrupts which might cause a timeout here */
576 flag = disable_interrupts();
582 *((vu_char *)dest) = data;
584 /* re-enable interrupts if necessary */
588 /* data polling for D7 */
589 start = get_timer (0);
590 while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
591 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
598 /*-----------------------------------------------------------------------