2 * (C) Copyright 2001-2005
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 */
56 case INTEL_ID_28F640C3B:
57 info->flash_id = FLASH_28F640C3B;
58 info->sector_count = 135;
59 info->size = 0x02000000; /* 16 * 2 MB = 32 MB */
62 return (0); /* no or unknown flash */
65 info->flash_id |= INTEL_MANUFACT << 16; /* set manufacturer offset */
67 if (info->flash_id & FLASH_BTYPE) {
68 volatile unsigned long *tmp = baseaddr;
70 /* set up sector start adress table (bottom sector type)
71 * AND unlock the sectors (if our chip is 160C3)
73 for (i = 0; i < info->sector_count; i++) {
74 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_28F160C3B) ||
75 ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F640C3B)) {
81 info->start[i] = (uint) tmp;
82 tmp += i < 8 ? 0x2000 : 0x10000; /* pointer arith */
86 memset (info->protect, 0, info->sector_count);
88 baseaddr[0] = 0x00FF00FF;
89 baseaddr[1] = 0x00FF00FF;
94 static ulong flash_amd_get_size (vu_char *addr, flash_info_t *info)
98 ulong base = (ulong)addr;
100 /* Write auto select command: read Manufacturer ID */
108 devid = addr[1] & 0xff;
110 /* only support AMD */
111 if (vendor != 0x01) {
118 if (devid == AMD_ID_F040B) {
119 info->flash_id = vendor << 16 | devid;
120 info->sector_count = 8;
121 info->size = info->sector_count * 0x10000;
123 else if (devid == AMD_ID_F080B) {
124 info->flash_id = vendor << 16 | devid;
125 info->sector_count = 16;
126 info->size = 4 * info->sector_count * 0x10000;
128 else if (devid == AMD_ID_F016D) {
129 info->flash_id = vendor << 16 | devid;
130 info->sector_count = 32;
131 info->size = 4 * info->sector_count * 0x10000;
134 printf ("## Unknown Flash Type: %02x\n", devid);
138 /* check for protected sectors */
139 for (i = 0; i < info->sector_count; i++) {
140 /* sector base address */
141 info->start[i] = base + i * (info->size / info->sector_count);
142 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
143 /* D0 = 1 if protected */
144 addr = (volatile unsigned char *)(info->start[i]);
145 info->protect[i] = addr[2] & 1;
149 * Prevent writes to uninitialized FLASH.
151 if (info->flash_id != FLASH_UNKNOWN) {
152 addr = (vu_char *)info->start[0];
153 addr[0] = 0xF0; /* reset bank */
160 /*-----------------------------------------------------------------------
162 unsigned long flash_init (void)
164 unsigned long size_b0 = 0;
165 unsigned long size_b1 = 0;
168 /* Init: no FLASHes known
170 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
171 flash_info[i].flash_id = FLASH_UNKNOWN;
174 /* Disable flash protection */
175 CPU86_BCR |= (CPU86_BCR_FWPT | CPU86_BCR_FWRE);
177 /* Static FLASH Bank configuration here (only one bank) */
179 size_b0 = flash_int_get_size ((ulong *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
180 size_b1 = flash_amd_get_size ((uchar *) CONFIG_SYS_BOOTROM_BASE, &flash_info[1]);
182 if (size_b0 > 0 || size_b1 > 0) {
188 print_size (size_b0, (size_b1 > 0) ? ", " : ") ");
193 print_size (size_b1, ") ");
197 printf ("## No FLASH found.\n");
200 /* protect monitor and environment sectors
203 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_BOOTROM_BASE
205 /* If U-Boot is booted from ROM the CONFIG_SYS_MONITOR_BASE > CONFIG_SYS_FLASH_BASE
206 * but we shouldn't protect it.
209 flash_protect (FLAG_PROTECT_SET,
210 CONFIG_SYS_MONITOR_BASE,
211 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[1]
215 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
216 flash_protect (FLAG_PROTECT_SET,
217 CONFIG_SYS_MONITOR_BASE,
218 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]
223 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
224 # ifndef CONFIG_ENV_SIZE
225 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
227 # if CONFIG_ENV_ADDR >= CONFIG_SYS_BOOTROM_BASE
229 flash_protect (FLAG_PROTECT_SET,
231 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[1]);
234 flash_protect (FLAG_PROTECT_SET,
236 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
240 return (size_b0 + size_b1);
243 /*-----------------------------------------------------------------------
245 void flash_print_info (flash_info_t * info)
249 if (info->flash_id == FLASH_UNKNOWN) {
250 printf ("missing or unknown FLASH type\n");
254 switch ((info->flash_id >> 16) & 0xff) {
262 printf ("Unknown Vendor ");
266 switch (info->flash_id & FLASH_TYPEMASK) {
267 case FLASH_28F160C3B:
268 printf ("28F160C3B (16 Mbit, bottom sector)\n");
270 case FLASH_28F160F3B:
271 printf ("28F160F3B (16 Mbit, bottom sector)\n");
273 case FLASH_28F640C3B:
274 printf ("28F640C3B (64 M, bottom sector)\n");
277 printf ("AM29F040B (4 Mbit)\n");
280 printf ("Unknown Chip Type\n");
284 if (info->size < 0x100000)
285 printf (" Size: %ld KB in %d Sectors\n",
286 info->size >> 10, info->sector_count);
288 printf (" Size: %ld MB in %d Sectors\n",
289 info->size >> 20, info->sector_count);
291 printf (" Sector Start Addresses:");
292 for (i = 0; i < info->sector_count; ++i) {
297 info->protect[i] ? " (RO)" : " "
303 /*-----------------------------------------------------------------------
305 int flash_erase (flash_info_t * info, int s_first, int s_last)
307 vu_char *addr = (vu_char *)(info->start[0]);
308 int flag, prot, sect, l_sect;
309 ulong start, now, last;
311 if ((s_first < 0) || (s_first > s_last)) {
312 if (info->flash_id == FLASH_UNKNOWN) {
313 printf ("- missing\n");
315 printf ("- no sectors to erase\n");
321 for (sect = s_first; sect <= s_last; sect++) {
322 if (info->protect[sect])
327 printf ("- Warning: %d protected sectors will not be erased!\n",
333 /* Check the type of erased flash
335 if (info->flash_id >> 16 == 0x1) {
340 /* Disable interrupts which might cause a timeout here */
341 flag = disable_interrupts();
349 /* wait at least 80us - let's wait 1 ms */
352 /* Start erase on unprotected sectors */
353 for (sect = s_first; sect<=s_last; sect++) {
354 if (info->protect[sect] == 0) { /* not protected */
355 addr = (vu_char *)(info->start[sect]);
361 /* re-enable interrupts if necessary */
365 /* wait at least 80us - let's wait 1 ms */
369 * We wait for the last triggered sector
374 start = get_timer (0);
376 addr = (vu_char *)(info->start[l_sect]);
377 while ((addr[0] & 0x80) != 0x80) {
378 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
379 printf ("Timeout\n");
382 /* show that we're waiting */
383 if ((now - last) > 1000) { /* every second */
390 /* reset to read mode */
391 addr = (volatile unsigned char *)info->start[0];
392 addr[0] = 0xF0; /* reset bank */
398 /* Start erase on unprotected sectors
400 for (sect = s_first; sect <= s_last; sect++) {
401 volatile ulong *addr =
402 (volatile unsigned long *) info->start[sect];
404 start = get_timer (0);
406 if (info->protect[sect] == 0) {
407 /* Disable interrupts which might cause a timeout here
409 flag = disable_interrupts ();
413 addr[0] = 0x00200020;
414 addr[1] = 0x00200020;
415 addr[0] = 0x00D000D0;
416 addr[1] = 0x00D000D0;
418 /* re-enable interrupts if necessary
421 enable_interrupts ();
423 /* wait at least 80us - let's wait 1 ms
428 while ((addr[0] & 0x00800080) != 0x00800080 ||
429 (addr[1] & 0x00800080) != 0x00800080) {
430 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
431 printf ("Timeout (erase suspended!)\n");
434 addr[0] = 0x00B000B0;
435 addr[1] = 0x00B000B0;
438 /* show that we're waiting
440 if ((now - last) > 1000) { /* every second */
445 if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
446 printf ("*** ERROR: erase failed!\n");
450 /* Clear status register and reset to read mode
452 addr[0] = 0x00500050;
453 addr[1] = 0x00500050;
454 addr[0] = 0x00FF00FF;
455 addr[1] = 0x00FF00FF;
465 static int write_word (flash_info_t *, volatile unsigned long *, ulong);
466 static int write_byte (flash_info_t *info, ulong dest, uchar data);
468 /*-----------------------------------------------------------------------
469 * Copy memory to flash, returns:
472 * 2 - Flash not erased
474 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
477 int i, l, rc, cc = cnt, res = 0;
479 if (info->flash_id >> 16 == 0x1) {
481 /* Write to AMD 8-bit flash
484 if ((rc = write_byte(info, addr, *src)) != 0) {
495 /* Write to Intel 64-bit flash
497 for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
501 for (i = 0; i < 4; i++) {
502 v = (v << 8) + (i < l || i - l >= cc ?
503 *((unsigned char *) addr + i) : *src++);
506 if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
514 /*-----------------------------------------------------------------------
515 * Write a word to Flash, returns:
518 * 2 - Flash not erased
520 static int write_word (flash_info_t * info, volatile unsigned long *addr,
526 /* Check if Flash is (sufficiently) erased
528 if ((*addr & data) != data)
531 /* Disable interrupts which might cause a timeout here
533 flag = disable_interrupts ();
538 /* re-enable interrupts if necessary
541 enable_interrupts ();
543 start = get_timer (0);
544 while ((*addr & 0x00800080) != 0x00800080) {
545 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
554 if (*addr & 0x00220022) {
555 printf ("*** ERROR: program failed!\n");
560 /* Clear status register and reset to read mode
568 /*-----------------------------------------------------------------------
569 * Write a byte to Flash, returns:
572 * 2 - Flash not erased
574 static int write_byte (flash_info_t *info, ulong dest, uchar data)
576 vu_char *addr = (vu_char *)(info->start[0]);
580 /* Check if Flash is (sufficiently) erased */
581 if ((*((vu_char *)dest) & data) != data) {
584 /* Disable interrupts which might cause a timeout here */
585 flag = disable_interrupts();
591 *((vu_char *)dest) = data;
593 /* re-enable interrupts if necessary */
597 /* data polling for D7 */
598 start = get_timer (0);
599 while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
600 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
607 /*-----------------------------------------------------------------------