3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
32 static ulong
flash_get_size(vu_long
* addr
, flash_info_t
* info
);
33 static int write_byte(flash_info_t
* info
, ulong dest
, uchar data
);
34 static void flash_get_offsets(ulong base
, flash_info_t
* info
);
36 /*-----------------------------------------------------------------------
39 unsigned long flash_init(void)
41 volatile immap_t
*immap
= (immap_t
*) CFG_IMMR
;
42 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
46 /* Init: no FLASHes known */
47 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; ++i
)
48 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
50 size
= flash_get_size((vu_long
*) FLASH_BASE0_PRELIM
, &flash_info
[0]);
52 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
53 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size
, size
<< 20);
56 /* Remap FLASH according to real size */
57 memctl
->memc_or0
= CFG_OR_TIMING_FLASH
| (-size
& 0xFFFF8000);
58 memctl
->memc_br0
= (CFG_FLASH_BASE
& BR_BA_MSK
) | (memctl
->memc_br0
& ~(BR_BA_MSK
));
60 /* Re-do sizing to get full correct info */
61 size
= flash_get_size((vu_long
*) CFG_FLASH_BASE
, &flash_info
[0]);
63 flash_get_offsets(CFG_FLASH_BASE
, &flash_info
[0]);
65 /* monitor protection ON by default */
66 flash_protect(FLAG_PROTECT_SET
,
67 CFG_FLASH_BASE
, CFG_FLASH_BASE
+ monitor_flash_len
- 1,
70 flash_protect ( FLAG_PROTECT_SET
,
72 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1,
75 #ifdef CFG_ENV_ADDR_REDUND
76 flash_protect ( FLAG_PROTECT_SET
,
78 CFG_ENV_ADDR_REDUND
+ CFG_ENV_SIZE_REDUND
- 1,
83 flash_info
[0].size
= size
;
88 /*-----------------------------------------------------------------------
90 static void flash_get_offsets(ulong base
, flash_info_t
* info
)
94 /* set up sector start address table */
95 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
96 for (i
= 0; i
< info
->sector_count
; i
++) {
97 info
->start
[i
] = base
+ (i
* 0x00010000);
99 } else if (info
->flash_id
& FLASH_BTYPE
) {
100 /* set sector offsets for bottom boot block type */
101 info
->start
[0] = base
+ 0x00000000;
102 info
->start
[1] = base
+ 0x00004000;
103 info
->start
[2] = base
+ 0x00006000;
104 info
->start
[3] = base
+ 0x00008000;
105 for (i
= 4; i
< info
->sector_count
; i
++) {
106 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
109 /* set sector offsets for top boot block type */
110 i
= info
->sector_count
- 1;
111 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
112 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
113 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
114 for (; i
>= 0; i
--) {
115 info
->start
[i
] = base
+ i
* 0x00010000;
121 /*-----------------------------------------------------------------------
123 void flash_print_info(flash_info_t
* info
)
127 if (info
->flash_id
== FLASH_UNKNOWN
) {
128 printf("missing or unknown FLASH type\n");
132 switch (info
->flash_id
& FLASH_VENDMASK
) {
143 printf("Unknown Vendor ");
147 switch (info
->flash_id
& FLASH_TYPEMASK
) {
149 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
152 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
155 printf("AM29LV400T (4 Mbit, top boot sector)\n");
158 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
161 printf("AM29LV800T (8 Mbit, top boot sector)\n");
164 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
167 printf("AM29LV160T (16 Mbit, top boot sector)\n");
170 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
173 printf("AM29LV320T (32 Mbit, top boot sector)\n");
176 printf("Unknown Chip Type\n");
180 printf(" Size: %ld MB in %d Sectors\n", info
->size
>> 20, info
->sector_count
);
182 printf(" Sector Start Addresses:");
183 for (i
= 0; i
< info
->sector_count
; ++i
) {
186 printf(" %08lX%s", info
->start
[i
], info
->protect
[i
] ? " (RO)" : " ");
191 /*-----------------------------------------------------------------------
195 /*-----------------------------------------------------------------------
199 * The following code cannot be run from FLASH!
202 static ulong
flash_get_size(vu_long
* addr
, flash_info_t
* info
)
207 vu_char
*caddr
= (vu_char
*) addr
;
208 ulong base
= (ulong
) addr
;
211 /* Write auto select command: read Manufacturer ID */
212 caddr
[0x0555] = 0xAA;
213 caddr
[0x02AA] = 0x55;
214 caddr
[0x0555] = 0x90;
218 case (AMD_MANUFACT
& 0xFF):
219 info
->flash_id
= FLASH_MAN_AMD
;
221 case (FUJ_MANUFACT
& 0xFF):
222 info
->flash_id
= FLASH_MAN_FUJ
;
224 case (MX_MANUFACT
& 0xFF):
225 info
->flash_id
= FLASH_MAN_MX
;
227 case (STM_MANUFACT
& 0xFF):
228 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 pid
= caddr
[1]; /* device ID */
239 case (AMD_ID_LV400T
& 0xFF):
240 info
->flash_id
+= FLASH_AM400T
;
241 info
->sector_count
= 11;
242 info
->size
= 0x00080000;
243 break; /* => 512 kB */
245 case (AMD_ID_LV400B
& 0xFF):
246 info
->flash_id
+= FLASH_AM400B
;
247 info
->sector_count
= 11;
248 info
->size
= 0x00080000;
249 break; /* => 512 kB */
251 case (AMD_ID_LV800T
& 0xFF):
252 info
->flash_id
+= FLASH_AM800T
;
253 info
->sector_count
= 19;
254 info
->size
= 0x00100000;
257 case (AMD_ID_LV800B
& 0xFF):
258 info
->flash_id
+= FLASH_AM800B
;
259 info
->sector_count
= 19;
260 info
->size
= 0x00100000;
263 case (AMD_ID_LV160T
& 0xFF):
264 info
->flash_id
+= FLASH_AM160T
;
265 info
->sector_count
= 35;
266 info
->size
= 0x00200000;
269 case (AMD_ID_LV160B
& 0xFF):
270 info
->flash_id
+= FLASH_AM160B
;
271 info
->sector_count
= 35;
272 info
->size
= 0x00200000;
275 case (AMD_ID_LV040B
& 0xFF):
276 info
->flash_id
+= FLASH_AM040
;
277 info
->sector_count
= 8;
278 info
->size
= 0x00080000;
281 case (STM_ID_M29W040B
& 0xFF):
282 info
->flash_id
+= FLASH_AM040
;
283 info
->sector_count
= 8;
284 info
->size
= 0x00080000;
287 #if 0 /* enable when device IDs are available */
288 case (AMD_ID_LV320T
& 0xFF):
289 info
->flash_id
+= FLASH_AM320T
;
290 info
->sector_count
= 67;
291 info
->size
= 0x00400000;
294 case (AMD_ID_LV320B
& 0xFF):
295 info
->flash_id
+= FLASH_AM320B
;
296 info
->sector_count
= 67;
297 info
->size
= 0x00400000;
301 info
->flash_id
= FLASH_UNKNOWN
;
302 return (0); /* => no or unknown flash */
307 /* set up sector start address table */
308 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
309 for (i
= 0; i
< info
->sector_count
; i
++) {
310 info
->start
[i
] = base
+ (i
* 0x00010000);
312 } else if (info
->flash_id
& FLASH_BTYPE
) {
313 /* set sector offsets for bottom boot block type */
314 info
->start
[0] = base
+ 0x00000000;
315 info
->start
[1] = base
+ 0x00004000;
316 info
->start
[2] = base
+ 0x00006000;
317 info
->start
[3] = base
+ 0x00008000;
318 for (i
= 4; i
< info
->sector_count
; i
++) {
319 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
322 /* set sector offsets for top boot block type */
323 i
= info
->sector_count
- 1;
324 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
325 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
326 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
327 for (; i
>= 0; i
--) {
328 info
->start
[i
] = base
+ i
* 0x00010000;
332 /* check for protected sectors */
333 for (i
= 0; i
< info
->sector_count
; i
++) {
334 /* read sector protection: D0 = 1 if protected */
335 caddr
= (volatile unsigned char *)(info
->start
[i
]);
336 info
->protect
[i
] = caddr
[2] & 1;
340 * Prevent writes to uninitialized FLASH.
342 if (info
->flash_id
!= FLASH_UNKNOWN
) {
343 caddr
= (vu_char
*) info
->start
[0];
345 caddr
[0x0555] = 0xAA;
346 caddr
[0x02AA] = 0x55;
347 caddr
[0x0555] = 0xF0;
356 /*-----------------------------------------------------------------------
359 int flash_erase(flash_info_t
* info
, int s_first
, int s_last
)
361 vu_char
*addr
= (vu_char
*) (info
->start
[0]);
362 int flag
, prot
, sect
, l_sect
;
363 ulong start
, now
, last
;
365 if ((s_first
< 0) || (s_first
> s_last
)) {
366 if (info
->flash_id
== FLASH_UNKNOWN
) {
367 printf("- missing\n");
369 printf("- no sectors to erase\n");
374 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
375 (info
->flash_id
> FLASH_AMD_COMP
)) {
376 printf("Can't erase unknown flash type %08lx - aborted\n", info
->flash_id
);
381 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
382 if (info
->protect
[sect
]) {
388 printf("- Warning: %d protected sectors will not be erased!\n", prot
);
395 /* Disable interrupts which might cause a timeout here */
396 flag
= disable_interrupts();
404 /* Start erase on unprotected sectors */
405 for (sect
= s_first
; sect
<= s_last
; sect
++) {
406 if (info
->protect
[sect
] == 0) { /* not protected */
407 addr
= (vu_char
*) (info
->start
[sect
]);
413 /* re-enable interrupts if necessary */
417 /* wait at least 80us - let's wait 1 ms */
421 * We wait for the last triggered sector
426 start
= get_timer(0);
428 addr
= (vu_char
*) (info
->start
[l_sect
]);
429 while ((addr
[0] & 0x80) != 0x80) {
430 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
434 /* show that we're waiting */
435 if ((now
- last
) > 1000) { /* every second */
442 /* reset to read mode */
443 addr
= (vu_char
*) info
->start
[0];
444 addr
[0] = 0xF0; /* reset bank */
450 /*-----------------------------------------------------------------------
451 * Copy memory to flash, returns:
454 * 2 - Flash not erased
457 int write_buff(flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
462 if ((rc
= write_byte(info
, addr
++, *src
++)) != 0) {
471 /*-----------------------------------------------------------------------
472 * Write a word to Flash, returns:
475 * 2 - Flash not erased
477 static int write_byte(flash_info_t
* info
, ulong dest
, uchar data
)
479 vu_char
*addr
= (vu_char
*) (info
->start
[0]);
483 /* Check if Flash is (sufficiently) erased */
484 if ((*((vu_char
*) dest
) & data
) != data
) {
487 /* Disable interrupts which might cause a timeout here */
488 flag
= disable_interrupts();
494 *((vu_char
*) dest
) = data
;
496 /* re-enable interrupts if necessary */
500 /* data polling for D7 */
501 start
= get_timer(0);
502 while ((*((vu_char
*) dest
) & 0x80) != (data
& 0x80)) {
503 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
510 /*-----------------------------------------------------------------------