3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13 #if defined(CONFIG_ENV_IS_IN_FLASH)
14 # ifndef CONFIG_ENV_ADDR
15 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
17 # ifndef CONFIG_ENV_SIZE
18 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
20 # ifndef CONFIG_ENV_SECT_SIZE
21 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
25 /*-----------------------------------------------------------------------
28 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
29 static int write_data (flash_info_t *info, ulong dest, ulong data);
30 static void flash_get_offsets (ulong base, flash_info_t *info);
32 /*-----------------------------------------------------------------------
35 unsigned long flash_init (void)
37 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
38 volatile memctl8xx_t *memctl = &immap->im_memctl;
39 unsigned long size_b0;
42 /* Init: no FLASHes known */
43 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
44 flash_info[i].flash_id = FLASH_UNKNOWN;
47 /* Static FLASH Bank configuration here - FIXME XXX */
49 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
51 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
52 printf ("## Unknown FLASH on Bank 0: "
53 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
54 flash_info[0].flash_id,
55 size_b0, size_b0<<20);
58 /* Remap FLASH according to real size */
59 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
60 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | \
61 BR_MS_GPCM | BR_PS_16 | BR_V;
63 /* Re-do sizing to get full correct info */
64 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
66 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
68 flash_info[0].size = size_b0;
70 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
71 /* monitor protection ON by default */
72 flash_protect(FLAG_PROTECT_SET,
73 CONFIG_SYS_MONITOR_BASE,
74 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
78 #ifdef CONFIG_ENV_IS_IN_FLASH
79 /* ENV protection ON by default */
80 flash_protect(FLAG_PROTECT_SET,
82 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
89 /*-----------------------------------------------------------------------
91 static void flash_get_offsets (ulong base, flash_info_t *info)
95 if (info->flash_id == FLASH_UNKNOWN) {
99 switch (info->flash_id & FLASH_VENDMASK) {
101 if (info->flash_id & FLASH_BTYPE) {
102 /* set sector offsets for bottom boot block type */
103 info->start[0] = base + 0x00000000;
104 info->start[1] = base + 0x00004000;
105 info->start[2] = base + 0x00006000;
106 info->start[3] = base + 0x00008000;
107 for (i = 4; i < info->sector_count; i++) {
108 info->start[i] = base + ((i-3) * 0x00020000);
111 /* set sector offsets for top boot block type */
112 i = info->sector_count - 1;
113 info->start[i--] = base + info->size - 0x00004000;
114 info->start[i--] = base + info->size - 0x00006000;
115 info->start[i--] = base + info->size - 0x00008000;
116 for (; i >= 0; i--) {
117 info->start[i] = base + i * 0x00020000;
123 for (i = 0; i < info->sector_count; i++) {
124 info->start[i] = base + (i * 0x00002000);
131 /* set up sector start address table */
132 if (info->flash_id & FLASH_BTYPE) {
133 /* set sector offsets for bottom boot block type */
134 info->start[0] = base + 0x00000000;
135 info->start[1] = base + 0x00008000;
136 info->start[2] = base + 0x0000C000;
137 info->start[3] = base + 0x00010000;
138 for (i = 4; i < info->sector_count; i++) {
139 info->start[i] = base + (i * 0x00020000) - 0x00060000;
142 /* set sector offsets for top boot block type */
143 i = info->sector_count - 1;
144 info->start[i--] = base + info->size - 0x00008000;
145 info->start[i--] = base + info->size - 0x0000C000;
146 info->start[i--] = base + info->size - 0x00010000;
147 for (; i >= 0; i--) {
148 info->start[i] = base + i * 0x00020000;
153 printf ("Don't know sector ofsets for flash type 0x%lx\n",
159 /*-----------------------------------------------------------------------
161 void flash_print_info (flash_info_t *info)
165 if (info->flash_id == FLASH_UNKNOWN) {
166 printf ("missing or unknown FLASH type\n");
170 switch (info->flash_id & FLASH_VENDMASK) {
171 case FLASH_MAN_AMD: printf ("AMD "); break;
172 case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
173 case FLASH_MAN_SST: printf ("SST "); break;
174 case FLASH_MAN_STM: printf ("STM "); break;
175 case FLASH_MAN_MT: printf ("MT "); break;
176 case FLASH_MAN_INTEL: printf ("Intel "); break;
177 default: printf ("Unknown Vendor "); break;
180 switch (info->flash_id & FLASH_TYPEMASK) {
181 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
183 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
185 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
187 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
189 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
191 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
193 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
195 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
197 case FLASH_SST200A: printf ("39xF200A (2M = 128K x 16)\n");
199 case FLASH_SST400A: printf ("39xF400A (4M = 256K x 16)\n");
201 case FLASH_SST800A: printf ("39xF800A (8M = 512K x 16)\n");
203 case FLASH_STM800AB: printf ("M29W800AB (8M = 512K x 16)\n");
205 case FLASH_28F008S5: printf ("28F008S5 (1M = 64K x 16)\n");
207 case FLASH_28F400_T: printf ("28F400B3 (4Mbit, top boot sector)\n");
209 case FLASH_28F400_B: printf ("28F400B3 (4Mbit, bottom boot sector)\n");
211 default: printf ("Unknown Chip Type\n");
215 if (info->size >= (1 << 20)) {
220 printf (" Size: %ld %cB in %d Sectors\n",
222 (i == 20) ? 'M' : 'k',
225 printf (" Sector Start Addresses:");
226 for (i=0; i<info->sector_count; ++i) {
231 info->protect[i] ? " (RO)" : " "
238 /*-----------------------------------------------------------------------
242 /*-----------------------------------------------------------------------
246 * The following code cannot be run from FLASH!
249 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
252 vu_short *saddr = (vu_short *)addr;
254 /* Read Manufacturer ID */
259 case (AMD_MANUFACT & 0xFFFF):
260 info->flash_id = FLASH_MAN_AMD;
262 case (FUJ_MANUFACT & 0xFFFF):
263 info->flash_id = FLASH_MAN_FUJ;
265 case (SST_MANUFACT & 0xFFFF):
266 info->flash_id = FLASH_MAN_SST;
268 case (STM_MANUFACT & 0xFFFF):
269 info->flash_id = FLASH_MAN_STM;
271 case (MT_MANUFACT & 0xFFFF):
272 info->flash_id = FLASH_MAN_MT;
275 info->flash_id = FLASH_UNKNOWN;
276 info->sector_count = 0;
278 saddr[0] = 0x00FF; /* restore read mode */
279 return (0); /* no or unknown flash */
282 value = saddr[1]; /* device ID */
285 case (AMD_ID_LV400T & 0xFFFF):
286 info->flash_id += FLASH_AM400T;
287 info->sector_count = 11;
288 info->size = 0x00100000;
291 case (AMD_ID_LV400B & 0xFFFF):
292 info->flash_id += FLASH_AM400B;
293 info->sector_count = 11;
294 info->size = 0x00100000;
297 case (AMD_ID_LV800T & 0xFFFF):
298 info->flash_id += FLASH_AM800T;
299 info->sector_count = 19;
300 info->size = 0x00200000;
303 case (AMD_ID_LV800B & 0xFFFF):
304 info->flash_id += FLASH_AM800B;
305 info->sector_count = 19;
306 info->size = 0x00200000;
309 case (AMD_ID_LV160T & 0xFFFF):
310 info->flash_id += FLASH_AM160T;
311 info->sector_count = 35;
312 info->size = 0x00400000;
315 case (AMD_ID_LV160B & 0xFFFF):
316 info->flash_id += FLASH_AM160B;
317 info->sector_count = 35;
318 info->size = 0x00400000;
320 #if 0 /* enable when device IDs are available */
321 case (AMD_ID_LV320T & 0xFFFF):
322 info->flash_id += FLASH_AM320T;
323 info->sector_count = 67;
324 info->size = 0x00800000;
327 case (AMD_ID_LV320B & 0xFFFF):
328 info->flash_id += FLASH_AM320B;
329 info->sector_count = 67;
330 info->size = 0x00800000;
333 case (SST_ID_xF200A & 0xFFFF):
334 info->flash_id += FLASH_SST200A;
335 info->sector_count = 64; /* 39xF200A ID ( 2M = 128K x 16 ) */
336 info->size = 0x00080000;
338 case (SST_ID_xF400A & 0xFFFF):
339 info->flash_id += FLASH_SST400A;
340 info->sector_count = 128; /* 39xF400A ID ( 4M = 256K x 16 ) */
341 info->size = 0x00100000;
343 case (SST_ID_xF800A & 0xFFFF):
344 info->flash_id += FLASH_SST800A;
345 info->sector_count = 256; /* 39xF800A ID ( 8M = 512K x 16 ) */
346 info->size = 0x00200000;
348 case (STM_ID_x800AB & 0xFFFF):
349 info->flash_id += FLASH_STM800AB;
350 info->sector_count = 19;
351 info->size = 0x00200000;
353 case (MT_ID_28F400_T & 0xFFFF):
354 info->flash_id += FLASH_28F400_T;
355 info->sector_count = 7;
356 info->size = 0x00080000;
357 break; /* => 512 kB */
358 case (MT_ID_28F400_B & 0xFFFF):
359 info->flash_id += FLASH_28F400_B;
360 info->sector_count = 7;
361 info->size = 0x00080000;
362 break; /* => 512 kB */
364 info->flash_id = FLASH_UNKNOWN;
365 saddr[0] = 0x00FF; /* restore read mode */
366 return (0); /* => no or unknown flash */
370 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
371 printf ("** ERROR: sector count %d > max (%d) **\n",
372 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
373 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
376 saddr[0] = 0x00FF; /* restore read mode */
382 /*-----------------------------------------------------------------------
385 int flash_erase (flash_info_t *info, int s_first, int s_last)
387 int flag, prot, sect;
388 ulong start, now, last;
390 if ((s_first < 0) || (s_first > s_last)) {
391 if (info->flash_id == FLASH_UNKNOWN) {
392 printf ("- missing\n");
394 printf ("- no sectors to erase\n");
399 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_MT) {
400 printf ("Can erase only MT flash types - aborted\n");
405 for (sect=s_first; sect<=s_last; ++sect) {
406 if (info->protect[sect]) {
412 printf ("- Warning: %d protected sectors will not be erased!\n",
418 start = get_timer (0);
420 /* Start erase on unprotected sectors */
421 for (sect = s_first; sect<=s_last; sect++) {
422 if (info->protect[sect] == 0) { /* not protected */
423 vu_short *addr = (vu_short *)(info->start[sect]);
424 unsigned short status;
426 /* Disable interrupts which might cause a timeout here */
427 flag = disable_interrupts();
429 *addr = 0x0050; /* clear status register */
430 *addr = 0x0020; /* erase setup */
431 *addr = 0x00D0; /* erase confirm */
433 /* re-enable interrupts if necessary */
437 /* wait at least 80us - let's wait 1 ms */
440 while (((status = *addr) & 0x0080) != 0x0080) {
441 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
442 printf ("Timeout\n");
443 *addr = 0x00FF; /* reset to read mode */
447 /* show that we're waiting */
448 if ((now - last) > 1000) { /* every second */
454 *addr = 0x00FF; /* reset to read mode */
461 /*-----------------------------------------------------------------------
462 * Copy memory to flash, returns:
465 * 2 - Flash not erased
466 * 4 - Flash not identified
469 #define FLASH_WIDTH 2 /* flash bus width in bytes */
471 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
476 if (info->flash_id == FLASH_UNKNOWN) {
480 wp = (addr & ~(FLASH_WIDTH-1)); /* get lower FLASH_WIDTH aligned address */
483 * handle unaligned start bytes
485 if ((l = addr - wp) != 0) {
487 for (i=0, cp=wp; i<l; ++i, ++cp) {
488 data = (data << 8) | (*(uchar *)cp);
490 for (; i<FLASH_WIDTH && cnt>0; ++i) {
491 data = (data << 8) | *src++;
495 for (; cnt==0 && i<FLASH_WIDTH; ++i, ++cp) {
496 data = (data << 8) | (*(uchar *)cp);
499 if ((rc = write_data(info, wp, data)) != 0) {
506 * handle FLASH_WIDTH aligned part
508 while (cnt >= FLASH_WIDTH) {
510 for (i=0; i<FLASH_WIDTH; ++i) {
511 data = (data << 8) | *src++;
513 if ((rc = write_data(info, wp, data)) != 0) {
525 * handle unaligned tail bytes
528 for (i=0, cp=wp; i<FLASH_WIDTH && cnt>0; ++i, ++cp) {
529 data = (data << 8) | *src++;
532 for (; i<FLASH_WIDTH; ++i, ++cp) {
533 data = (data << 8) | (*(uchar *)cp);
536 return (write_data(info, wp, data));
539 /*-----------------------------------------------------------------------
540 * Write a word to Flash, returns:
543 * 2 - Flash not erased
545 static int write_data (flash_info_t *info, ulong dest, ulong data)
547 vu_short *addr = (vu_short *)dest;
548 ushort sdata = (ushort)data;
553 /* Check if Flash is (sufficiently) erased */
554 if ((*addr & sdata) != sdata) {
557 /* Disable interrupts which might cause a timeout here */
558 flag = disable_interrupts();
560 *addr = 0x0040; /* write setup */
563 /* re-enable interrupts if necessary */
567 start = get_timer (0);
569 while (((status = *addr) & 0x0080) != 0x0080) {
570 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
571 *addr = 0x00FF; /* restore read mode */
576 *addr = 0x00FF; /* restore read mode */
581 /*-----------------------------------------------------------------------