3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/processor.h>
11 #include <asm/pci_io.h>
14 #define ROM_CS0_START 0xFF800000
15 #define ROM_CS1_START 0xFF000000
17 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
19 #if defined(CONFIG_ENV_IS_IN_FLASH)
20 # ifndef CONFIG_ENV_ADDR
21 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
23 # ifndef CONFIG_ENV_SIZE
24 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
26 # ifndef CONFIG_ENV_SECT_SIZE
27 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
31 /*-----------------------------------------------------------------------
34 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
36 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
39 /*flash command address offsets*/
51 #define FLASH_WORD_SIZE unsigned char
53 /*-----------------------------------------------------------------------
57 static int byte_parity_odd(unsigned char x
) __attribute__ ((const));
59 static unsigned long flash_id(unsigned char mfct
, unsigned char chip
) __attribute__ ((const));
63 FLASH_WORD_SIZE extval
;
64 unsigned short intval
;
69 byte_parity_odd(unsigned char x
)
74 return (x
& 0x1) != 0;
80 flash_id(unsigned char mfct
, unsigned char chip
)
82 static const map_entry mfct_map
[] =
84 {(FLASH_WORD_SIZE
) AMD_MANUFACT
, (unsigned short) ((unsigned long) FLASH_MAN_AMD
>> 16)},
85 {(FLASH_WORD_SIZE
) FUJ_MANUFACT
, (unsigned short) ((unsigned long) FLASH_MAN_FUJ
>> 16)},
86 {(FLASH_WORD_SIZE
) STM_MANUFACT
, (unsigned short) ((unsigned long) FLASH_MAN_STM
>> 16)},
87 {(FLASH_WORD_SIZE
) MT_MANUFACT
, (unsigned short) ((unsigned long) FLASH_MAN_MT
>> 16)},
88 {(FLASH_WORD_SIZE
) INTEL_MANUFACT
,(unsigned short) ((unsigned long) FLASH_MAN_INTEL
>> 16)},
89 {(FLASH_WORD_SIZE
) INTEL_ALT_MANU
,(unsigned short) ((unsigned long) FLASH_MAN_INTEL
>> 16)}
92 static const map_entry chip_map
[] =
94 {AMD_ID_F040B
, FLASH_AM040
},
95 {(FLASH_WORD_SIZE
) STM_ID_x800AB
, FLASH_STM800AB
}
99 unsigned long result
= FLASH_UNKNOWN
;
102 for(p
= &chip_map
[0]; p
< &chip_map
[sizeof chip_map
/ sizeof chip_map
[0]]; p
++)
103 if(p
->extval
== chip
)
105 result
= FLASH_VENDMASK
| p
->intval
;
110 for(p
= &mfct_map
[0]; p
< &mfct_map
[sizeof mfct_map
/ sizeof mfct_map
[0]]; p
++)
111 if(p
->extval
== mfct
)
113 result
&= ~FLASH_VENDMASK
;
114 result
|= (unsigned long) p
->intval
<< 16;
127 static const ulong flash_banks
[] = CONFIG_SYS_FLASH_BANKS
;
129 /* Init: no FLASHes known */
130 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++)
132 flash_info_t
* const pflinfo
= &flash_info
[i
];
133 pflinfo
->flash_id
= FLASH_UNKNOWN
;
135 pflinfo
->sector_count
= 0;
138 /* Enable writes to Sandpoint flash */
140 register unsigned char temp
;
141 CONFIG_READ_BYTE(CONFIG_SYS_WINBOND_ISA_CFG_ADDR
+ WINBOND_CSCR
, temp
);
142 temp
&= ~0x20; /* clear BIOSWP bit */
143 CONFIG_WRITE_BYTE(CONFIG_SYS_WINBOND_ISA_CFG_ADDR
+ WINBOND_CSCR
, temp
);
146 for(i
= 0; i
< sizeof flash_banks
/ sizeof flash_banks
[0]; i
++)
148 flash_info_t
* const pflinfo
= &flash_info
[i
];
149 const unsigned long base_address
= flash_banks
[i
];
150 volatile FLASH_WORD_SIZE
* const flash
= (FLASH_WORD_SIZE
*) base_address
;
152 volatile FLASH_WORD_SIZE
* addr2
;
155 /* write autoselect sequence */
156 flash
[0x5555] = 0xaa;
157 flash
[0x2aaa] = 0x55;
158 flash
[0x5555] = 0x90;
160 flash
[0xAAA << (3 * i
)] = 0xaa;
161 flash
[0x555 << (3 * i
)] = 0x55;
162 flash
[0xAAA << (3 * i
)] = 0x90;
164 __asm__
__volatile__("sync");
167 pflinfo
->flash_id
= flash_id(flash
[0x0], flash
[0x1]);
169 pflinfo
->flash_id
= flash_id(flash
[0x0], flash
[0x2 + 14 * i
]);
172 switch(pflinfo
->flash_id
& FLASH_TYPEMASK
)
175 pflinfo
->size
= 0x00080000;
176 pflinfo
->sector_count
= 8;
177 for(j
= 0; j
< 8; j
++)
179 pflinfo
->start
[j
] = base_address
+ 0x00010000 * j
;
180 pflinfo
->protect
[j
] = flash
[(j
<< 16) | 0x2];
184 pflinfo
->size
= 0x00100000;
185 pflinfo
->sector_count
= 19;
186 pflinfo
->start
[0] = base_address
;
187 pflinfo
->start
[1] = base_address
+ 0x4000;
188 pflinfo
->start
[2] = base_address
+ 0x6000;
189 pflinfo
->start
[3] = base_address
+ 0x8000;
190 for(j
= 1; j
< 16; j
++)
192 pflinfo
->start
[j
+3] = base_address
+ 0x00010000 * j
;
195 /* check for protected sectors */
196 for (j
= 0; j
< pflinfo
->sector_count
; j
++) {
197 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
198 /* D0 = 1 if protected */
199 addr2
= (volatile FLASH_WORD_SIZE
*)(pflinfo
->start
[j
]);
200 if (pflinfo
->flash_id
& FLASH_MAN_SST
)
201 pflinfo
->protect
[j
] = 0;
203 pflinfo
->protect
[j
] = addr2
[2] & 1;
208 /* Protect monitor and environment sectors
210 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
211 flash_protect(FLAG_PROTECT_SET
,
212 CONFIG_SYS_MONITOR_BASE
,
213 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1,
217 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
218 flash_protect(FLAG_PROTECT_SET
,
220 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
- 1,
224 /* reset device to read mode */
225 flash
[0x0000] = 0xf0;
226 __asm__
__volatile__("sync");
229 return flash_info
[0].size
+ flash_info
[1].size
;
234 flash_get_offsets (ulong base
, flash_info_t
*info
)
238 /* set up sector start address table */
239 if (info
->flash_id
& FLASH_MAN_SST
)
241 for (i
= 0; i
< info
->sector_count
; i
++)
242 info
->start
[i
] = base
+ (i
* 0x00010000);
245 if (info
->flash_id
& FLASH_BTYPE
) {
246 /* set sector offsets for bottom boot block type */
247 info
->start
[0] = base
+ 0x00000000;
248 info
->start
[1] = base
+ 0x00004000;
249 info
->start
[2] = base
+ 0x00006000;
250 info
->start
[3] = base
+ 0x00008000;
251 for (i
= 4; i
< info
->sector_count
; i
++) {
252 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
255 /* set sector offsets for top boot block type */
256 i
= info
->sector_count
- 1;
257 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
258 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
259 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
260 for (; i
>= 0; i
--) {
261 info
->start
[i
] = base
+ i
* 0x00010000;
268 /*-----------------------------------------------------------------------
271 flash_print_info(flash_info_t
*info
)
273 static const char unk
[] = "Unknown";
274 const char *mfct
= unk
, *type
= unk
;
277 if(info
->flash_id
!= FLASH_UNKNOWN
)
279 switch(info
->flash_id
& FLASH_VENDMASK
)
281 case FLASH_MAN_AMD
: mfct
= "AMD"; break;
282 case FLASH_MAN_FUJ
: mfct
= "FUJITSU"; break;
283 case FLASH_MAN_STM
: mfct
= "STM"; break;
284 case FLASH_MAN_SST
: mfct
= "SST"; break;
285 case FLASH_MAN_BM
: mfct
= "Bright Microelectonics"; break;
286 case FLASH_MAN_INTEL
: mfct
= "Intel"; break;
289 switch(info
->flash_id
& FLASH_TYPEMASK
)
291 case FLASH_AM040
: type
= "AM29F040B (512K * 8, uniform sector size)"; break;
292 case FLASH_AM400B
: type
= "AM29LV400B (4 Mbit, bottom boot sect)"; break;
293 case FLASH_AM400T
: type
= "AM29LV400T (4 Mbit, top boot sector)"; break;
294 case FLASH_AM800B
: type
= "AM29LV800B (8 Mbit, bottom boot sect)"; break;
295 case FLASH_AM800T
: type
= "AM29LV800T (8 Mbit, top boot sector)"; break;
296 case FLASH_AM160T
: type
= "AM29LV160T (16 Mbit, top boot sector)"; break;
297 case FLASH_AM320B
: type
= "AM29LV320B (32 Mbit, bottom boot sect)"; break;
298 case FLASH_AM320T
: type
= "AM29LV320T (32 Mbit, top boot sector)"; break;
299 case FLASH_STM800AB
: type
= "M29W800AB (8 Mbit, bottom boot sect)"; break;
300 case FLASH_SST800A
: type
= "SST39LF/VF800 (8 Mbit, uniform sector size)"; break;
301 case FLASH_SST160A
: type
= "SST39LF/VF160 (16 Mbit, uniform sector size)"; break;
306 "\n Brand: %s Type: %s\n"
307 " Size: %lu KB in %d Sectors\n",
314 printf (" Sector Start Addresses:");
316 for (i
= 0; i
< info
->sector_count
; i
++)
320 unsigned long * flash
= (unsigned long *) info
->start
[i
];
323 * Check if whole sector is erased
326 (i
!= (info
->sector_count
- 1)) ?
327 (info
->start
[i
+ 1] - info
->start
[i
]) >> 2 :
328 (info
->start
[0] + info
->size
- info
->start
[i
]) >> 2;
331 flash
= (unsigned long *) info
->start
[i
], erased
= 1;
332 (flash
!= (unsigned long *) info
->start
[i
] + size
) && erased
;
335 erased
= *flash
== ~0x0UL
;
339 (i
% 5) ? "" : "\n ",
342 info
->protect
[i
] ? "RO" : " "
353 * The following code cannot be run from FLASH!
356 flash_get_size (vu_long
*addr
, flash_info_t
*info
)
359 FLASH_WORD_SIZE value
;
360 ulong base
= (ulong
)addr
;
361 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*)addr
;
363 printf("flash_get_size: \n");
364 /* Write auto select command: read Manufacturer ID */
366 addr2
[ADDR0
] = (FLASH_WORD_SIZE
)0xAA;
367 addr2
[ADDR1
] = (FLASH_WORD_SIZE
)0x55;
368 addr2
[ADDR0
] = (FLASH_WORD_SIZE
)0x90;
372 case (FLASH_WORD_SIZE
)AMD_MANUFACT
:
373 info
->flash_id
= FLASH_MAN_AMD
;
375 case (FLASH_WORD_SIZE
)FUJ_MANUFACT
:
376 info
->flash_id
= FLASH_MAN_FUJ
;
378 case (FLASH_WORD_SIZE
)SST_MANUFACT
:
379 info
->flash_id
= FLASH_MAN_SST
;
382 info
->flash_id
= FLASH_UNKNOWN
;
383 info
->sector_count
= 0;
385 return (0); /* no or unknown flash */
387 printf("recognised manufacturer");
389 value
= addr2
[ADDR3
]; /* device ID */
390 debug ("\ndev_code=%x\n", value
);
393 case (FLASH_WORD_SIZE
)AMD_ID_LV400T
:
394 info
->flash_id
+= FLASH_AM400T
;
395 info
->sector_count
= 11;
396 info
->size
= 0x00080000;
397 break; /* => 0.5 MB */
399 case (FLASH_WORD_SIZE
)AMD_ID_LV400B
:
400 info
->flash_id
+= FLASH_AM400B
;
401 info
->sector_count
= 11;
402 info
->size
= 0x00080000;
403 break; /* => 0.5 MB */
405 case (FLASH_WORD_SIZE
)AMD_ID_LV800T
:
406 info
->flash_id
+= FLASH_AM800T
;
407 info
->sector_count
= 19;
408 info
->size
= 0x00100000;
411 case (FLASH_WORD_SIZE
)AMD_ID_LV800B
:
412 info
->flash_id
+= FLASH_AM800B
;
413 info
->sector_count
= 19;
414 info
->size
= 0x00100000;
417 case (FLASH_WORD_SIZE
)AMD_ID_LV160T
:
418 info
->flash_id
+= FLASH_AM160T
;
419 info
->sector_count
= 35;
420 info
->size
= 0x00200000;
423 case (FLASH_WORD_SIZE
)AMD_ID_LV160B
:
424 info
->flash_id
+= FLASH_AM160B
;
425 info
->sector_count
= 35;
426 info
->size
= 0x00200000;
429 case (FLASH_WORD_SIZE
)SST_ID_xF800A
:
430 info
->flash_id
+= FLASH_SST800A
;
431 info
->sector_count
= 16;
432 info
->size
= 0x00100000;
435 case (FLASH_WORD_SIZE
)SST_ID_xF160A
:
436 info
->flash_id
+= FLASH_SST160A
;
437 info
->sector_count
= 32;
438 info
->size
= 0x00200000;
441 case (FLASH_WORD_SIZE
)AMD_ID_F040B
:
442 info
->flash_id
+= FLASH_AM040
;
443 info
->sector_count
= 8;
444 info
->size
= 0x00080000;
445 break; /* => 0.5 MB */
448 info
->flash_id
= FLASH_UNKNOWN
;
449 return (0); /* => no or unknown flash */
453 printf("flash id %lx; sector count %x, size %lx\n", info
->flash_id
,info
->sector_count
,info
->size
);
454 /* set up sector start address table */
455 if (info
->flash_id
& FLASH_MAN_SST
)
457 for (i
= 0; i
< info
->sector_count
; i
++)
458 info
->start
[i
] = base
+ (i
* 0x00010000);
461 if (info
->flash_id
& FLASH_BTYPE
) {
462 /* set sector offsets for bottom boot block type */
463 info
->start
[0] = base
+ 0x00000000;
464 info
->start
[1] = base
+ 0x00004000;
465 info
->start
[2] = base
+ 0x00006000;
466 info
->start
[3] = base
+ 0x00008000;
467 for (i
= 4; i
< info
->sector_count
; i
++) {
468 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
471 /* set sector offsets for top boot block type */
472 i
= info
->sector_count
- 1;
473 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
474 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
475 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
476 for (; i
>= 0; i
--) {
477 info
->start
[i
] = base
+ i
* 0x00010000;
481 /* check for protected sectors */
482 for (i
= 0; i
< info
->sector_count
; i
++) {
483 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
484 /* D0 = 1 if protected */
485 addr2
= (volatile FLASH_WORD_SIZE
*)(info
->start
[i
]);
486 if (info
->flash_id
& FLASH_MAN_SST
)
487 info
->protect
[i
] = 0;
489 info
->protect
[i
] = addr2
[2] & 1;
493 * Prevent writes to uninitialized FLASH.
495 if (info
->flash_id
!= FLASH_UNKNOWN
) {
496 addr2
= (FLASH_WORD_SIZE
*)info
->start
[0];
497 *addr2
= (FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
507 flash_erase(flash_info_t
*info
, int s_first
, int s_last
)
509 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*)(info
->start
[0]);
510 int flag
, prot
, sect
, l_sect
;
511 ulong start
, now
, last
;
514 if ((s_first
< 0) || (s_first
> s_last
)) {
515 if (info
->flash_id
== FLASH_UNKNOWN
) {
516 printf ("- missing\n");
518 printf ("- no sectors to erase\n");
523 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
524 (info
->flash_id
> (FLASH_MAN_STM
| FLASH_AMD_COMP
))) {
525 printf ("Can't erase unknown flash type - aborted\n");
530 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
531 if (info
->protect
[sect
]) {
537 printf ("- Warning: %d protected sectors will not be erased!\n",
545 /* Check the ROM CS */
546 if ((info
->start
[0] >= ROM_CS1_START
) && (info
->start
[0] < ROM_CS0_START
))
551 /* Disable interrupts which might cause a timeout here */
552 flag
= disable_interrupts();
554 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
)0x00AA00AA;
555 addr
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
)0x00550055;
556 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
)0x00800080;
557 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
)0x00AA00AA;
558 addr
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
)0x00550055;
560 /* Start erase on unprotected sectors */
561 for (sect
= s_first
; sect
<=s_last
; sect
++) {
562 if (info
->protect
[sect
] == 0) { /* not protected */
563 addr
= (FLASH_WORD_SIZE
*)(info
->start
[0] + (
564 (info
->start
[sect
] - info
->start
[0]) << sh8b
));
565 if (info
->flash_id
& FLASH_MAN_SST
)
567 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
)0x00AA00AA;
568 addr
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
)0x00550055;
569 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
)0x00800080;
570 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
)0x00AA00AA;
571 addr
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
)0x00550055;
572 addr
[0] = (FLASH_WORD_SIZE
)0x00500050; /* block erase */
573 udelay(30000); /* wait 30 ms */
576 addr
[0] = (FLASH_WORD_SIZE
)0x00300030; /* sector erase */
581 /* re-enable interrupts if necessary */
585 /* wait at least 80us - let's wait 1 ms */
589 * We wait for the last triggered sector
594 start
= get_timer (0);
596 addr
= (FLASH_WORD_SIZE
*)(info
->start
[0] + (
597 (info
->start
[l_sect
] - info
->start
[0]) << sh8b
));
598 while ((addr
[0] & (FLASH_WORD_SIZE
)0x00800080) != (FLASH_WORD_SIZE
)0x00800080) {
599 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
600 printf ("Timeout\n");
603 /* show that we're waiting */
604 if ((now
- last
) > 1000) { /* every second */
611 /* reset to read mode */
612 addr
= (FLASH_WORD_SIZE
*)info
->start
[0];
613 addr
[0] = (FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
619 /*-----------------------------------------------------------------------
620 * Copy memory to flash, returns:
623 * 2 - Flash not erased
626 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
631 wp
= (addr
& ~3); /* get lower word aligned address */
634 * handle unaligned start bytes
636 if ((l
= addr
- wp
) != 0) {
638 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
639 data
= (data
<< 8) | (*(uchar
*)cp
);
641 for (; i
<4 && cnt
>0; ++i
) {
642 data
= (data
<< 8) | *src
++;
646 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
647 data
= (data
<< 8) | (*(uchar
*)cp
);
650 if ((rc
= write_word(info
, wp
, data
)) != 0) {
657 * handle word aligned part
661 for (i
=0; i
<4; ++i
) {
662 data
= (data
<< 8) | *src
++;
664 if ((rc
= write_word(info
, wp
, data
)) != 0) {
676 * handle unaligned tail bytes
679 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
680 data
= (data
<< 8) | *src
++;
683 for (; i
<4; ++i
, ++cp
) {
684 data
= (data
<< 8) | (*(uchar
*)cp
);
687 return (write_word(info
, wp
, data
));
690 /*-----------------------------------------------------------------------
691 * Write a word to Flash, returns:
694 * 2 - Flash not erased
696 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
698 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*)info
->start
[0];
699 volatile FLASH_WORD_SIZE
*dest2
;
700 volatile FLASH_WORD_SIZE
*data2
= (FLASH_WORD_SIZE
*)&data
;
706 /* Check the ROM CS */
707 if ((info
->start
[0] >= ROM_CS1_START
) && (info
->start
[0] < ROM_CS0_START
))
712 dest2
= (FLASH_WORD_SIZE
*)(((dest
- info
->start
[0]) << sh8b
) +
715 /* Check if Flash is (sufficiently) erased */
716 if ((*dest2
& (FLASH_WORD_SIZE
)data
) != (FLASH_WORD_SIZE
)data
) {
719 /* Disable interrupts which might cause a timeout here */
720 flag
= disable_interrupts();
722 for (i
=0; i
<4/sizeof(FLASH_WORD_SIZE
); i
++)
724 addr2
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
)0x00AA00AA;
725 addr2
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
)0x00550055;
726 addr2
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
)0x00A000A0;
728 dest2
[i
<< sh8b
] = data2
[i
];
730 /* re-enable interrupts if necessary */
734 /* data polling for D7 */
735 start
= get_timer (0);
736 while ((dest2
[i
<< sh8b
] & (FLASH_WORD_SIZE
)0x00800080) !=
737 (data2
[i
] & (FLASH_WORD_SIZE
)0x00800080)) {
738 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
747 /*-----------------------------------------------------------------------