2 * (C) Copyright 2000, 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
6 * Add support the Sharp chips on the mpc8260ads.
7 * I started with board/ip860/flash.c and made changes I found in
8 * the MTD project by David Schleef.
10 * SPDX-License-Identifier: GPL-2.0+
16 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
18 #if defined(CONFIG_ENV_IS_IN_FLASH)
19 # ifndef CONFIG_ENV_ADDR
20 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
22 # ifndef CONFIG_ENV_SIZE
23 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
25 # ifndef CONFIG_ENV_SECT_SIZE
26 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
30 /*-----------------------------------------------------------------------
33 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
34 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
35 static int clear_block_lock_bit(vu_long
* addr
);
37 /*-----------------------------------------------------------------------
40 unsigned long flash_init (void)
42 #ifndef CONFIG_MPC8266ADS
43 volatile immap_t
*immap
= (immap_t
*)CONFIG_SYS_IMMR
;
44 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
45 volatile ip860_bcsr_t
*bcsr
= (ip860_bcsr_t
*)BCSR_BASE
;
50 /* Init: enable write,
51 * or we cannot even write flash commands
53 #ifndef CONFIG_MPC8266ADS
54 bcsr
->bd_ctrl
|= BD_CTRL_FLWE
;
58 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
59 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
61 /* set the default sector offset */
64 /* Static FLASH Bank configuration here - FIXME XXX */
66 size
= flash_get_size((vu_long
*)FLASH_BASE
, &flash_info
[0]);
68 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
69 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
73 #ifndef CONFIG_MPC8266ADS
74 /* Remap FLASH according to real size */
75 memctl
->memc_or1
= CONFIG_SYS_OR_TIMING_FLASH
| (-size
& 0xFFFF8000);
76 memctl
->memc_br1
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) |
77 (memctl
->memc_br1
& ~(BR_BA_MSK
));
79 /* Re-do sizing to get full correct info */
80 size
= flash_get_size((vu_long
*)CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
82 flash_info
[0].size
= size
;
84 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
85 /* monitor protection ON by default */
86 flash_protect(FLAG_PROTECT_SET
,
87 CONFIG_SYS_MONITOR_BASE
,
88 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
92 #ifdef CONFIG_ENV_IS_IN_FLASH
93 /* ENV protection ON by default */
94 flash_protect(FLAG_PROTECT_SET
,
96 CONFIG_ENV_ADDR
+CONFIG_ENV_SECT_SIZE
-1,
102 /*-----------------------------------------------------------------------
104 void flash_print_info (flash_info_t
*info
)
108 if (info
->flash_id
== FLASH_UNKNOWN
) {
109 printf ("missing or unknown FLASH type\n");
113 switch (info
->flash_id
& FLASH_VENDMASK
) {
114 case FLASH_MAN_INTEL
: printf ("Intel "); break;
115 case FLASH_MAN_SHARP
: printf ("Sharp "); break;
116 default: printf ("Unknown Vendor "); break;
119 switch (info
->flash_id
& FLASH_TYPEMASK
) {
120 case FLASH_28F016SV
: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
122 case FLASH_28F160S3
: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
124 case FLASH_28F320S3
: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
126 case FLASH_LH28F016SCT
: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
128 default: printf ("Unknown Chip Type\n");
132 printf (" Size: %ld MB in %d Sectors\n",
133 info
->size
>> 20, info
->sector_count
);
135 printf (" Sector Start Addresses:");
136 for (i
=0; i
<info
->sector_count
; ++i
) {
141 info
->protect
[i
] ? " (RO)" : " "
147 /*-----------------------------------------------------------------------
151 /*-----------------------------------------------------------------------
155 * The following code cannot be run from FLASH!
158 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
162 ulong base
= (ulong
)addr
;
165 /* Write "Intelligent Identifier" command: read Manufacturer ID */
168 value
= addr
[0] & 0x00FF00FF;
170 case MT_MANUFACT
: /* SHARP, MT or => Intel */
172 info
->flash_id
= FLASH_MAN_INTEL
;
175 printf("unknown manufacturer: %x\n", (unsigned int)value
);
176 info
->flash_id
= FLASH_UNKNOWN
;
177 info
->sector_count
= 0;
179 return (0); /* no or unknown flash */
182 value
= addr
[1]; /* device ID */
185 case (INTEL_ID_28F016S
):
186 info
->flash_id
+= FLASH_28F016SV
;
187 info
->sector_count
= 32;
188 info
->size
= 0x00400000;
189 sector_offset
= 0x20000;
190 break; /* => 2x2 MB */
192 case (INTEL_ID_28F160S3
):
193 info
->flash_id
+= FLASH_28F160S3
;
194 info
->sector_count
= 32;
195 info
->size
= 0x00400000;
196 sector_offset
= 0x20000;
197 break; /* => 2x2 MB */
199 case (INTEL_ID_28F320S3
):
200 info
->flash_id
+= FLASH_28F320S3
;
201 info
->sector_count
= 64;
202 info
->size
= 0x00800000;
203 sector_offset
= 0x20000;
204 break; /* => 2x4 MB */
206 case SHARP_ID_28F016SCL
:
207 case SHARP_ID_28F016SCZ
:
208 info
->flash_id
= FLASH_MAN_SHARP
| FLASH_LH28F016SCT
;
209 info
->sector_count
= 32;
210 info
->size
= 0x00800000;
211 sector_offset
= 0x40000;
212 break; /* => 4x2 MB */
216 info
->flash_id
= FLASH_UNKNOWN
;
217 return (0); /* => no or unknown flash */
221 /* set up sector start address table */
222 for (i
= 0; i
< info
->sector_count
; i
++) {
223 info
->start
[i
] = base
;
224 base
+= sector_offset
;
225 /* don't know how to check sector protection */
226 info
->protect
[i
] = 0;
230 * Prevent writes to uninitialized FLASH.
232 if (info
->flash_id
!= FLASH_UNKNOWN
) {
233 addr
= (vu_long
*)info
->start
[0];
235 *addr
= 0xFFFFFF; /* reset bank to read array mode */
242 /*-----------------------------------------------------------------------
245 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
247 int flag
, prot
, sect
;
248 ulong start
, now
, last
;
250 if ((s_first
< 0) || (s_first
> s_last
)) {
251 if (info
->flash_id
== FLASH_UNKNOWN
) {
252 printf ("- missing\n");
254 printf ("- no sectors to erase\n");
259 if ( ((info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_INTEL
)
260 && ((info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_SHARP
) ) {
261 printf ("Can't erase unknown flash type %08lx - aborted\n",
267 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
268 if (info
->protect
[sect
]) {
274 printf ("- Warning: %d protected sectors will not be erased!\n",
280 /* Make Sure Block Lock Bit is not set. */
281 if(clear_block_lock_bit((vu_long
*)(info
->start
[s_first
]))){
286 /* Start erase on unprotected sectors */
287 for (sect
= s_first
; sect
<=s_last
; sect
++) {
288 if (info
->protect
[sect
] == 0) { /* not protected */
289 vu_long
*addr
= (vu_long
*)(info
->start
[sect
]);
291 last
= start
= get_timer (0);
293 /* Disable interrupts which might cause a timeout here */
294 flag
= disable_interrupts();
298 /* Clear Status Register */
300 /* Single Block Erase Command */
305 if((info
->flash_id
& FLASH_TYPEMASK
) != FLASH_LH28F016SCT
) {
306 /* Resume Command, as per errata update */
310 /* re-enable interrupts if necessary */
314 /* wait at least 80us - let's wait 1 ms */
316 while ((*addr
& 0x80808080) != 0x80808080) {
317 if(*addr
& 0x20202020){
318 printf("Error in Block Erase - Lock Bit may be set!\n");
319 printf("Status Register = 0x%X\n", (uint
)*addr
);
320 *addr
= 0xFFFFFFFF; /* reset bank */
323 if ((now
=get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
324 printf ("Timeout\n");
325 *addr
= 0xFFFFFFFF; /* reset bank */
328 /* show that we're waiting */
329 if ((now
- last
) > 1000) { /* every second */
335 /* reset to read mode */
344 /*-----------------------------------------------------------------------
345 * Copy memory to flash, returns:
348 * 2 - Flash not erased
351 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
356 wp
= (addr
& ~3); /* get lower word aligned address */
359 * handle unaligned start bytes
361 if ((l
= addr
- wp
) != 0) {
363 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
364 data
= (data
<< 8) | (*(uchar
*)cp
);
366 for (; i
<4 && cnt
>0; ++i
) {
367 data
= (data
<< 8) | *src
++;
371 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
372 data
= (data
<< 8) | (*(uchar
*)cp
);
375 if ((rc
= write_word(info
, wp
, data
)) != 0) {
382 * handle word aligned part
386 for (i
=0; i
<4; ++i
) {
387 data
= (data
<< 8) | *src
++;
389 if ((rc
= write_word(info
, wp
, data
)) != 0) {
401 * handle unaligned tail bytes
404 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
405 data
= (data
<< 8) | *src
++;
408 for (; i
<4; ++i
, ++cp
) {
409 data
= (data
<< 8) | (*(uchar
*)cp
);
412 return (write_word(info
, wp
, data
));
415 /*-----------------------------------------------------------------------
416 * Write a word to Flash, returns:
419 * 2 - Flash not erased
421 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
423 vu_long
*addr
= (vu_long
*)dest
;
427 /* Check if Flash is (sufficiently) erased */
428 if ((*addr
& data
) != data
) {
431 /* Disable interrupts which might cause a timeout here */
432 flag
= disable_interrupts();
440 /* re-enable interrupts if necessary */
444 /* data polling for D7 */
445 start
= get_timer (0);
447 while (((csr
= *addr
) & 0x80808080) != 0x80808080) {
448 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
453 if (csr
& 0x40404040) {
454 printf ("CSR indicates write error (%08lx) at %08lx\n", csr
, (ulong
)addr
);
458 /* Clear Status Registers Command */
460 /* Reset to read array mode */
466 /*-----------------------------------------------------------------------
467 * Clear Block Lock Bit, returns:
472 static int clear_block_lock_bit(vu_long
* addr
)
478 /* Clear Status Register */
484 start
= get_timer (0);
485 while(*addr
!= 0x80808080){
486 if ((now
=get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
487 printf ("Timeout on clearing Block Lock Bit\n");
488 *addr
= 0xFFFFFFFF; /* reset bank */