2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 #ifndef CONFIG_ENV_ADDR
12 #define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
15 #define CONFIG_FLASH_16BIT
17 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
19 /*-----------------------------------------------------------------------
22 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
23 static int write_word (flash_info_t *info, ulong dest, ulong data);
25 /*-----------------------------------------------------------------------
28 unsigned long flash_init (void)
30 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
31 volatile memctl8xx_t *memctl = &immap->im_memctl;
32 unsigned long size_b0;
35 /* Init: no FLASHes known */
36 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
37 flash_info[i].flash_id = FLASH_UNKNOWN;
40 /* Static FLASH Bank configuration here - FIXME XXX */
42 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
44 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
45 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
46 size_b0, size_b0<<20);
50 /* Remap FLASH according to real size */
51 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
52 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16;
54 /* Re-do sizing to get full correct info */
55 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
57 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
58 /* monitor protection ON by default */
59 flash_protect(FLAG_PROTECT_SET,
60 CONFIG_SYS_MONITOR_BASE,
61 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
65 #ifdef CONFIG_ENV_IS_IN_FLASH
66 /* ENV protection ON by default */
67 flash_protect(FLAG_PROTECT_SET,
69 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
73 flash_info[0].size = size_b0;
78 /*-----------------------------------------------------------------------
80 void flash_print_info (flash_info_t *info)
84 if (info->flash_id == FLASH_UNKNOWN) {
85 printf ("missing or unknown FLASH type\n");
89 switch (info->flash_id & FLASH_VENDMASK) {
90 case FLASH_MAN_AMD: printf ("AMD "); break;
91 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
92 default: printf ("Unknown Vendor "); break;
95 switch (info->flash_id & FLASH_TYPEMASK) {
96 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
98 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
100 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
102 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
104 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
106 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
108 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
110 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
112 default: printf ("Unknown Chip Type\n");
116 printf (" Size: %ld MB in %d Sectors\n",
117 info->size >> 20, info->sector_count);
119 printf (" Sector Start Addresses:");
120 for (i=0; i<info->sector_count; ++i) {
125 info->protect[i] ? " (RO)" : " "
132 /*-----------------------------------------------------------------------
136 /*-----------------------------------------------------------------------
140 * The following code cannot be run from FLASH!
143 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
147 ulong base = (ulong)addr;
149 /* Write auto select command: read Manufacturer ID */
150 vu_short *s_addr=(vu_short*)addr;
151 s_addr[0x5555] = 0x00AA;
152 s_addr[0x2AAA] = 0x0055;
153 s_addr[0x5555] = 0x0090;
156 value = value|(value<<16);
160 info->flash_id = FLASH_MAN_AMD;
163 info->flash_id = FLASH_MAN_FUJ;
166 info->flash_id = FLASH_UNKNOWN;
167 info->sector_count = 0;
169 return (0); /* no or unknown flash */
173 value = value|(value<<16);
176 case FUJI_ID_29F800BA:
177 info->flash_id += FLASH_AM400T;
178 info->sector_count = 19;
179 info->size = 0x00100000;
182 info->flash_id += FLASH_AM800T;
183 info->sector_count = 19;
184 info->size = 0x00100000;
187 info->flash_id += FLASH_AM800B;
188 info->sector_count = 19;
189 info->size = 0x00100000;
192 info->flash_id = FLASH_UNKNOWN;
193 return (0); /* => no or unknown flash */
196 /* set up sector start address table */
197 /* set sector offsets for bottom boot block type */
198 info->start[0] = base + 0x00000000;
199 info->start[1] = base + 0x00004000;
200 info->start[2] = base + 0x00006000;
201 info->start[3] = base + 0x00008000;
202 for (i = 4; i < info->sector_count; i++) {
203 info->start[i] = base + (i * 0x00010000) - 0x00030000;
207 /* check for protected sectors */
208 for (i = 0; i < info->sector_count; i++) {
209 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
210 /* D0 = 1 if protected */
211 s_addr = (volatile unsigned short *)(info->start[i]);
212 info->protect[i] = s_addr[2] & 1;
216 * Prevent writes to uninitialized FLASH.
218 if (info->flash_id != FLASH_UNKNOWN) {
219 s_addr = (volatile unsigned short *)info->start[0];
220 *s_addr = 0x00F0; /* reset bank */
226 /*-----------------------------------------------------------------------
230 int flash_erase (flash_info_t *info, int s_first, int s_last)
232 vu_long *addr = (vu_long*)(info->start[0]);
233 int flag, prot, sect;
234 ulong start, now, last;
235 #ifdef CONFIG_FLASH_16BIT
236 vu_short *s_addr = (vu_short*)addr;
239 if ((s_first < 0) || (s_first > s_last)) {
240 if (info->flash_id == FLASH_UNKNOWN) {
241 printf ("- missing\n");
243 printf ("- no sectors to erase\n");
247 /*#ifndef CONFIG_FLASH_16BIT
249 type = (info->flash_id & FLASH_VENDMASK);
250 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
251 printf ("Can't erase unknown flash type %08lx - aborted\n",
257 for (sect=s_first; sect<=s_last; ++sect) {
258 if (info->protect[sect]) {
264 printf ("- Warning: %d protected sectors will not be erased!\n",
270 start = get_timer (0);
272 /* Start erase on unprotected sectors */
273 for (sect = s_first; sect<=s_last; sect++) {
274 if (info->protect[sect] == 0) { /* not protected */
275 #ifdef CONFIG_FLASH_16BIT
276 vu_short *s_sect_addr = (vu_short*)(info->start[sect]);
278 vu_long *sect_addr = (vu_long*)(info->start[sect]);
280 /* Disable interrupts which might cause a timeout here */
281 flag = disable_interrupts();
283 #ifdef CONFIG_FLASH_16BIT
285 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
286 s_addr[0x5555] = 0x00AA;
287 s_addr[0x2AAA] = 0x0055;
288 s_addr[0x5555] = 0x0080;
289 s_addr[0x5555] = 0x00AA;
290 s_addr[0x2AAA] = 0x0055;
291 s_sect_addr[0] = 0x0030;
293 addr[0x5555] = 0x00AA00AA;
294 addr[0x2AAA] = 0x00550055;
295 addr[0x5555] = 0x00800080;
296 addr[0x5555] = 0x00AA00AA;
297 addr[0x2AAA] = 0x00550055;
298 sect_addr[0] = 0x00300030;
300 /* re-enable interrupts if necessary */
304 /* wait at least 80us - let's wait 1 ms */
307 #ifdef CONFIG_FLASH_16BIT
308 while ((s_sect_addr[0] & 0x0080) != 0x0080) {
310 while ((sect_addr[0] & 0x00800080) != 0x00800080) {
312 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
313 printf ("Timeout\n");
316 /* show that we're waiting */
317 if ((now - last) > 1000) { /* every second */
325 /* reset to read mode */
326 addr = (volatile unsigned long *)info->start[0];
327 #ifdef CONFIG_FLASH_16BIT
328 s_addr[0] = 0x00F0; /* reset bank */
330 addr[0] = 0x00F000F0; /* reset bank */
338 /*-----------------------------------------------------------------------
339 * Copy memory to flash, returns:
342 * 2 - Flash not erased
345 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
350 wp = (addr & ~3); /* get lower word aligned address */
353 * handle unaligned start bytes
355 if ((l = addr - wp) != 0) {
357 for (i=0, cp=wp; i<l; ++i, ++cp) {
358 data = (data << 8) | (*(uchar *)cp);
360 for (; i<4 && cnt>0; ++i) {
361 data = (data << 8) | *src++;
365 for (; cnt==0 && i<4; ++i, ++cp) {
366 data = (data << 8) | (*(uchar *)cp);
369 if ((rc = write_word(info, wp, data)) != 0) {
376 * handle word aligned part
380 for (i=0; i<4; ++i) {
381 data = (data << 8) | *src++;
383 if ((rc = write_word(info, wp, data)) != 0) {
395 * handle unaligned tail bytes
398 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
399 data = (data << 8) | *src++;
402 for (; i<4; ++i, ++cp) {
403 data = (data << 8) | (*(uchar *)cp);
406 return (write_word(info, wp, data));
410 /*-----------------------------------------------------------------------
411 * Write a word to Flash, returns:
414 * 2 - Flash not erased
416 static int write_word (flash_info_t *info, ulong dest, ulong data)
418 vu_long *addr = (vu_long*)(info->start[0]);
420 #ifdef CONFIG_FLASH_16BIT
423 vu_short *s_addr = (vu_short*)addr;
428 /* Check if Flash is (sufficiently) erased */
429 if ((*((vu_long *)dest) & data) != data) {
433 #ifdef CONFIG_FLASH_16BIT
434 /* Write the 16 higher-bits */
435 /* Disable interrupts which might cause a timeout here */
436 flag = disable_interrupts();
438 high_data = ((data>>16) & 0x0000ffff);
440 s_addr[0x5555] = 0x00AA;
441 s_addr[0x2AAA] = 0x0055;
442 s_addr[0x5555] = 0x00A0;
444 *((vu_short *)dest) = high_data;
447 /* re-enable interrupts if necessary */
451 /* data polling for D7 */
452 start = get_timer (0);
453 while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
454 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
460 /* Write the 16 lower-bits */
463 /* Disable interrupts which might cause a timeout here */
464 flag = disable_interrupts();
465 #ifdef CONFIG_FLASH_16BIT
467 low_data = (data & 0x0000ffff);
469 s_addr[0x5555] = 0x00AA;
470 s_addr[0x2AAA] = 0x0055;
471 s_addr[0x5555] = 0x00A0;
472 *((vu_short *)dest) = low_data;
475 addr[0x5555] = 0x00AA00AA;
476 addr[0x2AAA] = 0x00550055;
477 addr[0x5555] = 0x00A000A0;
478 *((vu_long *)dest) = data;
481 /* re-enable interrupts if necessary */
485 /* data polling for D7 */
486 start = get_timer (0);
488 #ifdef CONFIG_FLASH_16BIT
489 while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
491 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
494 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {