2 **=====================================================================
4 ** Copyright (C) 2000, 2001, 2002, 2003
5 ** The LEOX team <team@leox.org>, http://www.leox.org
7 ** LEOX.org is about the development of free hardware and software resources
10 ** Description: U-Boot port on the LEOX's ELPT860 CPU board
13 **=====================================================================
15 * SPDX-License-Identifier: GPL-2.0+
17 **=====================================================================
21 ** Note 1: In this file, you have to provide the following variable:
23 ** flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]
24 ** 'flash_info_t' structure is defined into 'include/flash.h'
25 ** and defined as extern into 'common/cmd_flash.c'
27 ** Note 2: In this file, you have to provide the following functions:
29 ** unsigned long flash_init(void)
30 ** called from 'board_init_r()' into 'common/board.c'
32 ** void flash_print_info(flash_info_t *info)
33 ** called from 'do_flinfo()' into 'common/cmd_flash.c'
35 ** int flash_erase(flash_info_t *info,
38 ** called from 'do_flerase()' & 'flash_sect_erase()' into 'common/cmd_flash.c'
40 ** int write_buff (flash_info_t *info,
44 ** called from 'flash_write()' into 'common/cmd_flash.c'
51 #ifndef CONFIG_ENV_ADDR
52 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
55 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
57 /*-----------------------------------------------------------------------
60 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
61 static ulong
flash_get_size (volatile unsigned char *addr
, flash_info_t
*info
);
63 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
64 static int write_byte (flash_info_t
*info
, ulong dest
, uchar data
);
66 /*-----------------------------------------------------------------------
72 volatile immap_t
*immap
= (immap_t
*)CONFIG_SYS_IMMR
;
73 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
74 unsigned long size_b0
;
77 /* Init: no FLASHes known */
78 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
)
80 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
83 /* Static FLASH Bank configuration here - FIXME XXX */
85 size_b0
= flash_get_size ((volatile unsigned char *)FLASH_BASE0_PRELIM
,
88 if ( flash_info
[0].flash_id
== FLASH_UNKNOWN
)
90 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
91 size_b0
, size_b0
<<20);
94 /* Remap FLASH according to real size */
95 memctl
->memc_or0
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b0
& OR_AM_MSK
);
96 memctl
->memc_br0
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_PS_8
| BR_V
;
98 /* Re-do sizing to get full correct info */
99 size_b0
= flash_get_size ((volatile unsigned char *)CONFIG_SYS_FLASH_BASE
,
102 flash_get_offsets (CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
104 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
105 /* monitor protection ON by default */
106 flash_protect (FLAG_PROTECT_SET
,
107 CONFIG_SYS_MONITOR_BASE
,
108 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
-1,
112 #ifdef CONFIG_ENV_IS_IN_FLASH
113 /* ENV protection ON by default */
114 flash_protect(FLAG_PROTECT_SET
,
116 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
-1,
120 flash_info
[0].size
= size_b0
;
125 /*-----------------------------------------------------------------------
128 flash_get_offsets (ulong base
,
133 #define SECTOR_64KB 0x00010000
135 /* set up sector start adress table */
136 for (i
= 0; i
< info
->sector_count
; i
++)
138 info
->start
[i
] = base
+ (i
* SECTOR_64KB
);
142 /*-----------------------------------------------------------------------
145 flash_print_info (flash_info_t
*info
)
149 if ( info
->flash_id
== FLASH_UNKNOWN
)
151 printf ("missing or unknown FLASH type\n");
155 switch ( info
->flash_id
& FLASH_VENDMASK
)
157 case FLASH_MAN_AMD
: printf ("AMD "); break;
158 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
159 case FLASH_MAN_STM
: printf ("STM (Thomson) "); break;
160 default: printf ("Unknown Vendor "); break;
163 switch ( info
->flash_id
& FLASH_TYPEMASK
)
165 case FLASH_AM040
: printf ("AM29F040 (4 Mbits)\n");
167 default: printf ("Unknown Chip Type\n");
171 printf (" Size: %ld KB in %d Sectors\n",
172 info
->size
>> 10, info
->sector_count
);
174 printf (" Sector Start Addresses:");
175 for (i
=0; i
<info
->sector_count
; ++i
)
181 info
->protect
[i
] ? " (RO)" : " "
189 /*-----------------------------------------------------------------------
193 /*-----------------------------------------------------------------------
197 * The following code cannot be run from FLASH!
201 flash_get_size (volatile unsigned char *addr
,
206 ulong base
= (ulong
)addr
;
208 /* Write auto select command: read Manufacturer ID */
217 /* case AMD_MANUFACT: */
219 info
->flash_id
= FLASH_MAN_AMD
;
221 /* case FUJ_MANUFACT: */
223 info
->flash_id
= FLASH_MAN_FUJ
;
225 /* case STM_MANUFACT: */
227 info
->flash_id
= FLASH_MAN_STM
;
231 info
->flash_id
= FLASH_UNKNOWN
;
232 info
->sector_count
= 0;
234 return (0); /* no or unknown flash */
237 value
= addr
[1]; /* device ID */
243 info
->flash_id
+= FLASH_AM040
; /* 4 Mbits = 512k * 8 */
244 info
->sector_count
= 8;
245 info
->size
= 0x00080000;
249 info
->flash_id
= FLASH_UNKNOWN
;
250 return (0); /* => no or unknown flash */
253 /* set up sector start adress table */
254 for (i
= 0; i
< info
->sector_count
; i
++)
256 info
->start
[i
] = base
+ (i
* 0x00010000);
259 /* check for protected sectors */
260 for (i
= 0; i
< info
->sector_count
; i
++)
262 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
263 /* D0 = 1 if protected */
264 addr
= (volatile unsigned char *)(info
->start
[i
]);
265 info
->protect
[i
] = addr
[2] & 1;
269 * Prevent writes to uninitialized FLASH.
271 if ( info
->flash_id
!= FLASH_UNKNOWN
)
273 addr
= (volatile unsigned char *)info
->start
[0];
275 *addr
= 0xF0; /* reset bank */
282 /*-----------------------------------------------------------------------
286 flash_erase (flash_info_t
*info
,
290 volatile unsigned char *addr
= (volatile unsigned char *)(info
->start
[0]);
291 int flag
, prot
, sect
, l_sect
;
292 ulong start
, now
, last
;
294 if ( (s_first
< 0) || (s_first
> s_last
) )
296 if ( info
->flash_id
== FLASH_UNKNOWN
)
298 printf ("- missing\n");
302 printf ("- no sectors to erase\n");
307 if ( (info
->flash_id
== FLASH_UNKNOWN
) ||
308 (info
->flash_id
> FLASH_AMD_COMP
) )
310 printf ("Can't erase unknown flash type %08lx - aborted\n",
316 for (sect
=s_first
; sect
<=s_last
; ++sect
)
318 if ( info
->protect
[sect
] )
326 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
335 /* Disable interrupts which might cause a timeout here */
336 flag
= disable_interrupts();
344 /* Start erase on unprotected sectors */
345 for (sect
= s_first
; sect
<=s_last
; sect
++)
347 if (info
->protect
[sect
] == 0) /* not protected */
349 addr
= (volatile unsigned char *)(info
->start
[sect
]);
355 /* re-enable interrupts if necessary */
359 /* wait at least 80us - let's wait 1 ms */
363 * We wait for the last triggered sector
368 start
= get_timer (0);
370 addr
= (volatile unsigned char *)(info
->start
[l_sect
]);
371 while ( (addr
[0] & 0x80) != 0x80 )
373 if ( (now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
)
375 printf ("Timeout\n");
378 /* show that we're waiting */
379 if ( (now
- last
) > 1000 ) /* every second */
387 /* reset to read mode */
388 addr
= (volatile unsigned char *)info
->start
[0];
389 addr
[0] = 0xF0; /* reset bank */
396 /*-----------------------------------------------------------------------
397 * Copy memory to flash, returns:
400 * 2 - Flash not erased
404 write_buff (flash_info_t
*info
,
413 if ( (info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
)
415 /* Width of the data bus: 8 bits */
423 if ( (rc
= write_byte(info
, wp
, bdata
)) != 0 )
436 /* Width of the data bus: 32 bits */
438 wp
= (addr
& ~3); /* get lower word aligned address */
441 * handle unaligned start bytes
443 if ( (l
= addr
- wp
) != 0 )
446 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
)
448 data
= (data
<< 8) | (*(uchar
*)cp
);
450 for (; i
<4 && cnt
>0; ++i
)
452 data
= (data
<< 8) | *src
++;
456 for (; cnt
==0 && i
<4; ++i
, ++cp
)
458 data
= (data
<< 8) | (*(uchar
*)cp
);
461 if ( (rc
= write_word(info
, wp
, data
)) != 0 )
469 * handle word aligned part
476 data
= (data
<< 8) | *src
++;
478 if ( (rc
= write_word(info
, wp
, data
)) != 0 )
492 * handle unaligned tail bytes
495 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
)
497 data
= (data
<< 8) | *src
++;
500 for (; i
<4; ++i
, ++cp
)
502 data
= (data
<< 8) | (*(uchar
*)cp
);
505 return (write_word(info
, wp
, data
));
509 /*-----------------------------------------------------------------------
510 * Write a word to Flash, returns:
513 * 2 - Flash not erased
516 write_word (flash_info_t
*info
,
520 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
524 /* Check if Flash is (sufficiently) erased */
525 if ( (*((vu_long
*)dest
) & data
) != data
)
529 /* Disable interrupts which might cause a timeout here */
530 flag
= disable_interrupts();
532 addr
[0x0555] = 0x00AA00AA;
533 addr
[0x02AA] = 0x00550055;
534 addr
[0x0555] = 0x00A000A0;
536 *((vu_long
*)dest
) = data
;
538 /* re-enable interrupts if necessary */
542 /* data polling for D7 */
543 start
= get_timer (0);
544 while ( (*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080) )
546 if ( get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
)
555 /*-----------------------------------------------------------------------
556 * Write a byte to Flash, returns:
559 * 2 - Flash not erased
562 write_byte (flash_info_t
*info
,
566 volatile unsigned char *addr
= (volatile unsigned char *)(info
->start
[0]);
570 /* Check if Flash is (sufficiently) erased */
571 if ( (*((volatile unsigned char *)dest
) & data
) != data
)
575 /* Disable interrupts which might cause a timeout here */
576 flag
= disable_interrupts();
582 *((volatile unsigned char *)dest
) = data
;
584 /* re-enable interrupts if necessary */
588 /* data polling for D7 */
589 start
= get_timer (0);
590 while ( (*((volatile unsigned char *)dest
) & 0x80) != (data
& 0x80) )
592 if ( get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
)
601 /*-----------------------------------------------------------------------