2 * (C) Copyright 2001-2004
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_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 query command sequence and test FLASH answer
30 baseaddr[0] = 0x00980098;
31 baseaddr[1] = 0x00980098;
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 or 640c3)
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 /*-----------------------------------------------------------------------
96 unsigned long flash_init (void)
98 unsigned long size_b0 = 0;
101 /* Init: no FLASHes known
103 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
104 flash_info[i].flash_id = FLASH_UNKNOWN;
107 /* Static FLASH Bank configuration here (only one bank) */
109 size_b0 = flash_get_size ((ulong *) CONFIG_SYS_FLASH0_BASE, &flash_info[0]);
110 if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
111 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
112 size_b0, size_b0 >> 20);
115 /* protect monitor and environment sectors
118 #ifndef CONFIG_BOOT_ROM
119 /* If U-Boot is booted from ROM the CONFIG_SYS_MONITOR_BASE > CONFIG_SYS_FLASH0_BASE
120 * but we shouldn't protect it.
123 # if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
124 flash_protect (FLAG_PROTECT_SET,
125 CONFIG_SYS_MONITOR_BASE,
126 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]
129 #endif /* CONFIG_BOOT_ROM */
131 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
132 # ifndef CONFIG_ENV_SIZE
133 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
135 flash_protect (FLAG_PROTECT_SET,
137 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
143 /*-----------------------------------------------------------------------
145 void flash_print_info (flash_info_t * info)
149 if (info->flash_id == FLASH_UNKNOWN) {
150 printf ("missing or unknown FLASH type\n");
154 switch ((info->flash_id >> 16) & 0xff) {
159 printf ("Unknown Vendor ");
163 switch (info->flash_id & FLASH_TYPEMASK) {
164 case FLASH_28F160C3B:
165 printf ("28F160C3B (16 M, bottom sector)\n");
167 case FLASH_28F160F3B:
168 printf ("28F160F3B (16 M, bottom sector)\n");
170 case FLASH_28F640C3B:
171 printf ("28F640C3B (64 M, bottom sector)\n");
174 printf ("Unknown Chip Type\n");
178 printf (" Size: %ld MB in %d Sectors\n",
179 info->size >> 20, info->sector_count);
181 printf (" Sector Start Addresses:");
182 for (i = 0; i < info->sector_count; ++i) {
187 info->protect[i] ? " (RO)" : " "
193 /*-----------------------------------------------------------------------
195 int flash_erase (flash_info_t * info, int s_first, int s_last)
197 int flag, prot, sect;
198 ulong start, now, last;
200 if ((s_first < 0) || (s_first > s_last)) {
201 if (info->flash_id == FLASH_UNKNOWN) {
202 printf ("- missing\n");
204 printf ("- no sectors to erase\n");
210 for (sect = s_first; sect <= s_last; sect++) {
211 if (info->protect[sect])
216 printf ("- Warning: %d protected sectors will not be erased!\n",
222 /* Start erase on unprotected sectors
224 for (sect = s_first; sect <= s_last; sect++) {
225 volatile ulong *addr =
226 (volatile unsigned long *) info->start[sect];
228 start = get_timer (0);
230 if (info->protect[sect] == 0) {
231 /* Disable interrupts which might cause a timeout here
233 flag = disable_interrupts ();
237 addr[0] = 0x00200020;
238 addr[1] = 0x00200020;
239 addr[0] = 0x00D000D0;
240 addr[1] = 0x00D000D0;
242 /* re-enable interrupts if necessary
245 enable_interrupts ();
247 /* wait at least 80us - let's wait 1 ms
252 while ((addr[0] & 0x00800080) != 0x00800080 ||
253 (addr[1] & 0x00800080) != 0x00800080) {
254 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
255 printf ("Timeout (erase suspended!)\n");
258 addr[0] = 0x00B000B0;
259 addr[1] = 0x00B000B0;
262 /* show that we're waiting
264 if ((now - last) > 1000) { /* every second */
269 if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
270 printf ("*** ERROR: erase failed!\n");
274 /* Clear status register and reset to read mode
276 addr[0] = 0x00500050;
277 addr[1] = 0x00500050;
278 addr[0] = 0x00FF00FF;
279 addr[1] = 0x00FF00FF;
288 static int write_word (flash_info_t *, volatile unsigned long *, ulong);
290 /*-----------------------------------------------------------------------
291 * Copy memory to flash, returns:
294 * 2 - Flash not erased
296 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
299 int i, l, cc = cnt, res = 0;
302 for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
306 for (i = 0; i < 4; i++) {
307 v = (v << 8) + (i < l || i - l >= cc ?
308 *((unsigned char *) addr + i) : *src++);
311 if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
318 /*-----------------------------------------------------------------------
319 * Write a word to Flash, returns:
322 * 2 - Flash not erased
324 static int write_word (flash_info_t * info, volatile unsigned long *addr,
330 /* Check if Flash is (sufficiently) erased
332 if ((*addr & data) != data)
335 /* Disable interrupts which might cause a timeout here
337 flag = disable_interrupts ();
342 /* re-enable interrupts if necessary
345 enable_interrupts ();
347 start = get_timer (0);
348 while ((*addr & 0x00800080) != 0x00800080) {
349 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
358 if (*addr & 0x00220022) {
359 printf ("*** ERROR: program failed!\n");
364 /* Clear status register and reset to read mode