3 * Stefan Roese, DENX Software Engineering, sr@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,
25 #include <asm/processor.h>
27 #ifndef CONFIG_SYS_FLASH_READ0
28 #define CONFIG_SYS_FLASH_READ0 0x0000 /* 0 is standard */
29 #define CONFIG_SYS_FLASH_READ1 0x0001 /* 1 is standard */
30 #define CONFIG_SYS_FLASH_READ2 0x0002 /* 2 is standard */
33 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
38 static int write_word(flash_info_t
*info
, ulong dest
, ulong data
);
39 static ulong
flash_get_size(vu_long
*addr
, flash_info_t
*info
);
41 unsigned long flash_init(void)
43 unsigned long size_b0
, size_b1
;
45 unsigned long base_b0
, base_b1
;
47 /* Init: no FLASHes known */
48 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
49 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
52 /* Static FLASH Bank configuration here - FIXME XXX */
54 base_b0
= FLASH_BASE0_PRELIM
;
55 size_b0
= flash_get_size ((vu_long
*) base_b0
, &flash_info
[0]);
57 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
58 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
59 size_b0
, size_b0
<< 20);
62 base_b1
= FLASH_BASE1_PRELIM
;
63 size_b1
= flash_get_size ((vu_long
*) base_b1
, &flash_info
[1]);
65 return (size_b0
+ size_b1
);
68 void flash_print_info(flash_info_t
*info
)
74 volatile unsigned long *flash
;
76 if (info
->flash_id
== FLASH_UNKNOWN
) {
77 printf ("missing or unknown FLASH type\n");
81 switch (info
->flash_id
& FLASH_VENDMASK
) {
82 case FLASH_MAN_AMD
: printf ("AMD "); break;
83 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
84 case FLASH_MAN_SST
: printf ("SST "); break;
85 case FLASH_MAN_STM
: printf ("ST Micro"); break;
86 case FLASH_MAN_EXCEL
: printf ("Excel Semiconductor "); break;
87 case FLASH_MAN_MX
: printf ("MXIC "); break;
88 default: printf ("Unknown Vendor "); break;
91 switch (info
->flash_id
& FLASH_TYPEMASK
) {
92 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
94 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
96 case FLASH_AM040
: printf ("AM29LV040B (4 Mbit, uniform sector size)\n");
98 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
100 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
102 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
104 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
106 case FLASH_AM320T
: printf ("AM29LV320T (32 M, top sector)\n");
108 case FLASH_AM320B
: printf ("AM29LV320B (32 M, bottom sector)\n");
110 case FLASH_AMDL322T
: printf ("AM29DL322T (32 M, top sector)\n");
112 case FLASH_AMDL322B
: printf ("AM29DL322B (32 M, bottom sector)\n");
114 case FLASH_AMDL323T
: printf ("AM29DL323T (32 M, top sector)\n");
116 case FLASH_AMDL323B
: printf ("AM29DL323B (32 M, bottom sector)\n");
118 case FLASH_SST020
: printf ("SST39LF/VF020 (2 Mbit, uniform sector size)\n");
120 case FLASH_SST040
: printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
122 case STM_ID_M29W040B
: printf ("ST Micro M29W040B (4 Mbit, uniform sector size)\n");
124 default: printf ("Unknown Chip Type\n");
128 printf (" Size: %ld MB in %d Sectors\n",
129 info
->size
>> 20, info
->sector_count
);
131 printf (" Sector Start Addresses:");
132 for (i
=0; i
<info
->sector_count
; ++i
) {
133 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
135 * Check if whole sector is erased
137 if (i
!= (info
->sector_count
-1))
138 size
= info
->start
[i
+1] - info
->start
[i
];
140 size
= info
->start
[0] + info
->size
- info
->start
[i
];
142 flash
= (volatile unsigned long *)info
->start
[i
];
143 size
= size
>> 2; /* divide by 4 for longword access */
144 for (k
=0; k
<size
; k
++) {
145 if (*flash
++ != 0xffffffff) {
153 /* print empty and read-only info */
154 printf (" %08lX%s%s",
157 info
->protect
[i
] ? "RO " : " ");
163 info
->protect
[i
] ? " (RO)" : " ");
172 * The following code cannot be run from FLASH!
174 static ulong
flash_get_size(vu_long
*addr
, flash_info_t
*info
)
178 volatile CONFIG_SYS_FLASH_WORD_SIZE value
;
179 ulong base
= (ulong
)addr
;
180 volatile CONFIG_SYS_FLASH_WORD_SIZE
*addr2
= (volatile CONFIG_SYS_FLASH_WORD_SIZE
*)addr
;
182 /* Write auto select command: read Manufacturer ID */
183 addr2
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00AA00AA;
184 addr2
[CONFIG_SYS_FLASH_ADDR1
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00550055;
185 addr2
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00900090;
187 value
= addr2
[CONFIG_SYS_FLASH_READ0
];
190 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_MANUFACT
:
191 info
->flash_id
= FLASH_MAN_AMD
;
193 case (CONFIG_SYS_FLASH_WORD_SIZE
)FUJ_MANUFACT
:
194 info
->flash_id
= FLASH_MAN_FUJ
;
196 case (CONFIG_SYS_FLASH_WORD_SIZE
)SST_MANUFACT
:
197 info
->flash_id
= FLASH_MAN_SST
;
199 case (CONFIG_SYS_FLASH_WORD_SIZE
)STM_MANUFACT
:
200 info
->flash_id
= FLASH_MAN_STM
;
202 case (CONFIG_SYS_FLASH_WORD_SIZE
)EXCEL_MANUFACT
:
203 info
->flash_id
= FLASH_MAN_EXCEL
;
205 case (CONFIG_SYS_FLASH_WORD_SIZE
)MX_MANUFACT
:
206 info
->flash_id
= FLASH_MAN_MX
;
209 info
->flash_id
= FLASH_UNKNOWN
;
210 info
->sector_count
= 0;
212 return (0); /* no or unknown flash */
215 value
= addr2
[CONFIG_SYS_FLASH_READ1
]; /* device ID */
218 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV400T
:
219 info
->flash_id
+= FLASH_AM400T
;
220 info
->sector_count
= 11;
221 info
->size
= 0x00080000;
222 break; /* => 0.5 MB */
224 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV400B
:
225 info
->flash_id
+= FLASH_AM400B
;
226 info
->sector_count
= 11;
227 info
->size
= 0x00080000;
228 break; /* => 0.5 MB */
230 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV040B
:
231 info
->flash_id
+= FLASH_AM040
;
232 info
->sector_count
= 8;
233 info
->size
= 0x0080000; /* => 0.5 MB */
235 case (CONFIG_SYS_FLASH_WORD_SIZE
)STM_ID_M29W040B
:
236 info
->flash_id
+= FLASH_AM040
;
237 info
->sector_count
= 8;
238 info
->size
= 0x0080000; /* => 0,5 MB */
241 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV800T
:
242 info
->flash_id
+= FLASH_AM800T
;
243 info
->sector_count
= 19;
244 info
->size
= 0x00100000;
247 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV800B
:
248 info
->flash_id
+= FLASH_AM800B
;
249 info
->sector_count
= 19;
250 info
->size
= 0x00100000;
253 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV160T
:
254 info
->flash_id
+= FLASH_AM160T
;
255 info
->sector_count
= 35;
256 info
->size
= 0x00200000;
259 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV160B
:
260 info
->flash_id
+= FLASH_AM160B
;
261 info
->sector_count
= 35;
262 info
->size
= 0x00200000;
265 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV320T
:
266 info
->flash_id
+= FLASH_AM320T
;
267 info
->sector_count
= 71;
268 info
->size
= 0x00400000;
271 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_LV320B
:
272 info
->flash_id
+= FLASH_AM320B
;
273 info
->sector_count
= 71;
274 info
->size
= 0x00400000;
277 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_DL322T
:
278 info
->flash_id
+= FLASH_AMDL322T
;
279 info
->sector_count
= 71;
280 info
->size
= 0x00400000;
283 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_DL322B
:
284 info
->flash_id
+= FLASH_AMDL322B
;
285 info
->sector_count
= 71;
286 info
->size
= 0x00400000;
289 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_DL323T
:
290 info
->flash_id
+= FLASH_AMDL323T
;
291 info
->sector_count
= 71;
292 info
->size
= 0x00400000;
295 case (CONFIG_SYS_FLASH_WORD_SIZE
)AMD_ID_DL323B
:
296 info
->flash_id
+= FLASH_AMDL323B
;
297 info
->sector_count
= 71;
298 info
->size
= 0x00400000;
301 case (CONFIG_SYS_FLASH_WORD_SIZE
)SST_ID_xF020
:
302 info
->flash_id
+= FLASH_SST020
;
303 info
->sector_count
= 64;
304 info
->size
= 0x00040000;
305 break; /* => 256 kB */
307 case (CONFIG_SYS_FLASH_WORD_SIZE
)SST_ID_xF040
:
308 info
->flash_id
+= FLASH_SST040
;
309 info
->sector_count
= 128;
310 info
->size
= 0x00080000;
311 break; /* => 512 kB */
314 info
->flash_id
= FLASH_UNKNOWN
;
315 return (0); /* => no or unknown flash */
319 /* set up sector start address table */
320 if (((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) ||
321 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM640U
)) {
322 for (i
= 0; i
< info
->sector_count
; i
++)
323 info
->start
[i
] = base
+ (i
* 0x00001000);
324 } else if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
325 for (i
= 0; i
< info
->sector_count
; i
++)
326 info
->start
[i
] = base
+ (i
* 0x00010000);
327 } else if (((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AMDL322B
) ||
328 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AMDL323B
) ||
329 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM320B
) ||
330 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AMDL324B
)) {
331 /* set sector offsets for bottom boot block type */
332 for (i
=0; i
<8; ++i
) { /* 8 x 8k boot sectors */
333 info
->start
[i
] = base
;
336 while (i
< info
->sector_count
) { /* 64k regular sectors */
337 info
->start
[i
] = base
;
341 } else if (((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AMDL322T
) ||
342 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AMDL323T
) ||
343 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM320T
) ||
344 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AMDL324T
)) {
345 /* set sector offsets for top boot block type */
347 i
= info
->sector_count
;
348 for (n
=0; n
<8; ++n
) { /* 8 x 8k boot sectors */
351 info
->start
[i
] = base
;
353 while (i
> 0) { /* 64k regular sectors */
356 info
->start
[i
] = base
;
359 if (info
->flash_id
& FLASH_BTYPE
) {
360 /* set sector offsets for bottom boot block type */
361 info
->start
[0] = base
+ 0x00000000;
362 info
->start
[1] = base
+ 0x00004000;
363 info
->start
[2] = base
+ 0x00006000;
364 info
->start
[3] = base
+ 0x00008000;
365 for (i
= 4; i
< info
->sector_count
; i
++) {
366 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
369 /* set sector offsets for top boot block type */
370 i
= info
->sector_count
- 1;
371 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
372 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
373 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
374 for (; i
>= 0; i
--) {
375 info
->start
[i
] = base
+ i
* 0x00010000;
380 /* check for protected sectors */
381 for (i
= 0; i
< info
->sector_count
; i
++) {
382 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
383 /* D0 = 1 if protected */
384 addr2
= (volatile CONFIG_SYS_FLASH_WORD_SIZE
*)(info
->start
[i
]);
385 if ((info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_AMD
)
386 info
->protect
[i
] = 0;
388 info
->protect
[i
] = addr2
[CONFIG_SYS_FLASH_READ2
] & 1;
392 * Prevent writes to uninitialized FLASH.
394 if (info
->flash_id
!= FLASH_UNKNOWN
) {
395 addr2
= (CONFIG_SYS_FLASH_WORD_SIZE
*)info
->start
[0];
396 *addr2
= (CONFIG_SYS_FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
403 int flash_erase(flash_info_t
*info
, int s_first
, int s_last
)
405 volatile CONFIG_SYS_FLASH_WORD_SIZE
*addr
= (CONFIG_SYS_FLASH_WORD_SIZE
*)(info
->start
[0]);
406 volatile CONFIG_SYS_FLASH_WORD_SIZE
*addr2
;
407 int flag
, prot
, sect
, l_sect
;
408 ulong start
, now
, last
;
410 if ((s_first
< 0) || (s_first
> s_last
)) {
411 if (info
->flash_id
== FLASH_UNKNOWN
)
412 printf ("- missing\n");
414 printf ("- no sectors to erase\n");
418 if (info
->flash_id
== FLASH_UNKNOWN
) {
419 printf ("Can't erase unknown flash type - aborted\n");
424 for (sect
=s_first
; sect
<=s_last
; ++sect
)
425 if (info
->protect
[sect
])
429 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
435 /* Disable interrupts which might cause a timeout here */
436 flag
= disable_interrupts();
438 /* Start erase on unprotected sectors */
439 for (sect
= s_first
; sect
<=s_last
; sect
++) {
440 if (info
->protect
[sect
] == 0) { /* not protected */
441 addr2
= (CONFIG_SYS_FLASH_WORD_SIZE
*)(info
->start
[sect
]);
442 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) {
443 addr
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00AA00AA;
444 addr
[CONFIG_SYS_FLASH_ADDR1
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00550055;
445 addr
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00800080;
446 addr
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00AA00AA;
447 addr
[CONFIG_SYS_FLASH_ADDR1
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00550055;
448 addr2
[0] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00300030; /* sector erase */
450 /* re-enable interrupts if necessary */
456 /* data polling for D7 */
457 start
= get_timer (0);
458 while ((addr2
[0] & (CONFIG_SYS_FLASH_WORD_SIZE
)0x00800080) !=
459 (CONFIG_SYS_FLASH_WORD_SIZE
)0x00800080) {
460 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
)
464 if (sect
== s_first
) {
465 addr
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00AA00AA;
466 addr
[CONFIG_SYS_FLASH_ADDR1
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00550055;
467 addr
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00800080;
468 addr
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00AA00AA;
469 addr
[CONFIG_SYS_FLASH_ADDR1
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00550055;
471 addr2
[0] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00300030; /* sector erase */
477 /* re-enable interrupts if necessary */
481 /* wait at least 80us - let's wait 1 ms */
485 * We wait for the last triggered sector
490 start
= get_timer (0);
492 addr
= (CONFIG_SYS_FLASH_WORD_SIZE
*)(info
->start
[l_sect
]);
493 while ((addr
[0] & (CONFIG_SYS_FLASH_WORD_SIZE
)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE
)0x00800080) {
494 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
495 printf ("Timeout\n");
498 /* show that we're waiting */
499 if ((now
- last
) > 1000) { /* every second */
506 /* reset to read mode */
507 addr
= (CONFIG_SYS_FLASH_WORD_SIZE
*)info
->start
[0];
508 addr
[0] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
515 * Copy memory to flash, returns:
518 * 2 - Flash not erased
520 int write_buff(flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
525 wp
= (addr
& ~3); /* get lower word aligned address */
528 * handle unaligned start bytes
530 if ((l
= addr
- wp
) != 0) {
532 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
533 data
= (data
<< 8) | (*(uchar
*)cp
);
535 for (; i
<4 && cnt
>0; ++i
) {
536 data
= (data
<< 8) | *src
++;
540 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
541 data
= (data
<< 8) | (*(uchar
*)cp
);
544 if ((rc
= write_word(info
, wp
, data
)) != 0) {
551 * handle word aligned part
556 data
= (data
<< 8) | *src
++;
557 if ((rc
= write_word(info
, wp
, data
)) != 0)
567 * handle unaligned tail bytes
570 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
571 data
= (data
<< 8) | *src
++;
574 for (; i
<4; ++i
, ++cp
)
575 data
= (data
<< 8) | (*(uchar
*)cp
);
577 return (write_word(info
, wp
, data
));
581 * Write a word to Flash, returns:
584 * 2 - Flash not erased
586 static int write_word(flash_info_t
*info
, ulong dest
, ulong data
)
588 volatile CONFIG_SYS_FLASH_WORD_SIZE
*addr2
= (CONFIG_SYS_FLASH_WORD_SIZE
*)(info
->start
[0]);
589 volatile CONFIG_SYS_FLASH_WORD_SIZE
*dest2
= (CONFIG_SYS_FLASH_WORD_SIZE
*)dest
;
590 volatile CONFIG_SYS_FLASH_WORD_SIZE
*data2
= (CONFIG_SYS_FLASH_WORD_SIZE
*)&data
;
595 /* Check if Flash is (sufficiently) erased */
596 if ((*((vu_long
*)dest
) & data
) != data
)
599 /* Disable interrupts which might cause a timeout here */
600 flag
= disable_interrupts();
602 for (i
=0; i
<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE
); i
++) {
603 addr2
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00AA00AA;
604 addr2
[CONFIG_SYS_FLASH_ADDR1
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00550055;
605 addr2
[CONFIG_SYS_FLASH_ADDR0
] = (CONFIG_SYS_FLASH_WORD_SIZE
)0x00A000A0;
609 /* re-enable interrupts if necessary */
613 /* data polling for D7 */
614 start
= get_timer (0);
615 while ((dest2
[i
] & (CONFIG_SYS_FLASH_WORD_SIZE
)0x00800080) !=
616 (data2
[i
] & (CONFIG_SYS_FLASH_WORD_SIZE
)0x00800080)) {
617 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
)