3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Gregory E. Allen, gallen@arlut.utexas.edu
7 * Matthew E. Karger, karger@arlut.utexas.edu
8 * Applied Research Laboratories, The University of Texas at Austin
10 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/processor.h>
17 #define ROM_CS0_START 0xFF800000
18 #define ROM_CS1_START 0xFF000000
20 #if defined(CONFIG_ENV_IS_IN_FLASH)
21 # ifndef CONFIG_ENV_ADDR
22 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
24 # ifndef CONFIG_ENV_SIZE
25 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
27 # ifndef CONFIG_ENV_SECT_SIZE
28 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
32 #define FLASH_BANK_SIZE ((uint)(16 * 1024 * 1024)) /* max 16Mbyte */
33 #define MAIN_SECT_SIZE 0x10000
34 #define SECT_SIZE_32KB 0x8000
35 #define SECT_SIZE_8KB 0x2000
37 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
39 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
);
41 static void write_via_fpu (vu_long
* addr
, ulong
* data
);
43 static __inline__
unsigned long get_msr (void);
44 static __inline__
void set_msr (unsigned long msr
);
46 /*flash command address offsets*/
51 #define FLASH_WORD_SIZE unsigned char
53 /*---------------------------------------------------------------------*/
54 /*#define DEBUG_FLASH 1 */
56 /*---------------------------------------------------------------------*/
58 unsigned long flash_init (void)
60 int i
; /* flash bank counter */
61 int j
; /* flash device sector counter */
62 int k
; /* flash size calculation loop counter */
63 int N
; /* pow(2,N) is flash size, but we don't have <math.h> */
64 ulong total_size
= 0, device_size
= 1;
65 unsigned char manuf_id
, device_id
;
67 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
68 vu_char
*addr
= (vu_char
*) (CONFIG_SYS_FLASH_BASE
+ i
* FLASH_BANK_SIZE
);
70 addr
[0x555] = 0xAA; /* get manuf/device info command */
71 addr
[0x2AA] = 0x55; /* 3-cycle command */
74 manuf_id
= addr
[0]; /* read back manuf/device info */
77 addr
[0x55] = 0x98; /* CFI command */
78 N
= addr
[0x27]; /* read back device_size = pow(2,N) */
80 for (k
= 0; k
< N
; k
++) /* calculate device_size = pow(2,N) */
83 flash_info
[i
].size
= device_size
;
84 flash_info
[i
].sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
86 #if defined DEBUG_FLASH
87 printf ("manuf_id = %x, device_id = %x\n", manuf_id
, device_id
);
89 /* find out what kind of flash we are using */
90 if ((manuf_id
== (uchar
) (AMD_MANUFACT
))
91 && (device_id
== AMD_ID_LV033C
)) {
92 flash_info
[i
].flash_id
=
93 ((FLASH_MAN_AMD
& FLASH_VENDMASK
) << 16) |
94 (FLASH_AM033C
& FLASH_TYPEMASK
);
96 /* set individual sector start addresses */
97 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
98 flash_info
[i
].start
[j
] =
99 (CONFIG_SYS_FLASH_BASE
+ i
* FLASH_BANK_SIZE
+
104 else if ((manuf_id
== (uchar
) (AMD_MANUFACT
)) &&
105 (device_id
== AMD_ID_LV116DT
)) {
106 flash_info
[i
].flash_id
=
107 ((FLASH_MAN_AMD
& FLASH_VENDMASK
) << 16) |
108 (FLASH_AM160T
& FLASH_TYPEMASK
);
110 /* set individual sector start addresses */
111 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
112 flash_info
[i
].start
[j
] =
113 (CONFIG_SYS_FLASH_BASE
+ i
* FLASH_BANK_SIZE
+
116 if (j
< (CONFIG_SYS_MAX_FLASH_SECT
- 3)) {
117 flash_info
[i
].start
[j
] =
118 (CONFIG_SYS_FLASH_BASE
+ i
* FLASH_BANK_SIZE
+
120 } else if (j
== (CONFIG_SYS_MAX_FLASH_SECT
- 3)) {
121 flash_info
[i
].start
[j
] =
122 (flash_info
[i
].start
[j
- 1] + SECT_SIZE_32KB
);
125 flash_info
[i
].start
[j
] =
126 (flash_info
[i
].start
[j
- 1] + SECT_SIZE_8KB
);
132 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
137 #if defined DEBUG_FLASH
138 printf ("flash_id = 0x%08lX\n", flash_info
[i
].flash_id
);
143 memset (flash_info
[i
].protect
, 0, CONFIG_SYS_MAX_FLASH_SECT
);
145 total_size
+= flash_info
[i
].size
;
148 /* Protect monitor and environment sectors
150 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
151 flash_protect (FLAG_PROTECT_SET
, CONFIG_SYS_MONITOR_BASE
,
152 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1,
156 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
157 flash_protect (FLAG_PROTECT_SET
, CONFIG_ENV_ADDR
,
158 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
- 1, &flash_info
[0]);
165 /*-----------------------------------------------------------------------
167 void flash_print_info (flash_info_t
* info
)
169 static const char unk
[] = "Unknown";
170 const char *mfct
= unk
, *type
= unk
;
173 if (info
->flash_id
!= FLASH_UNKNOWN
) {
174 switch (info
->flash_id
& FLASH_VENDMASK
) {
188 mfct
= "Bright Microelectonics";
190 case FLASH_MAN_INTEL
:
195 switch (info
->flash_id
& FLASH_TYPEMASK
) {
197 type
= "AM29LV033C (32 Mbit, uniform sector size)";
200 type
= "AM29LV160T (16 Mbit, top boot sector)";
203 type
= "AM29F040B (512K * 8, uniform sector size)";
206 type
= "AM29LV400B (4 Mbit, bottom boot sect)";
209 type
= "AM29LV400T (4 Mbit, top boot sector)";
212 type
= "AM29LV800B (8 Mbit, bottom boot sect)";
215 type
= "AM29LV800T (8 Mbit, top boot sector)";
218 type
= "AM29LV320B (32 Mbit, bottom boot sect)";
221 type
= "AM29LV320T (32 Mbit, top boot sector)";
224 type
= "M29W800AB (8 Mbit, bottom boot sect)";
227 type
= "SST39LF/VF800 (8 Mbit, uniform sector size)";
230 type
= "SST39LF/VF160 (16 Mbit, uniform sector size)";
235 printf ("\n Brand: %s Type: %s\n"
236 " Size: %lu KB in %d Sectors\n",
237 mfct
, type
, info
->size
>> 10, info
->sector_count
);
239 printf (" Sector Start Addresses:");
241 for (i
= 0; i
< info
->sector_count
; i
++) {
244 unsigned long *flash
= (unsigned long *) info
->start
[i
];
247 * Check if whole sector is erased
249 size
= (i
!= (info
->sector_count
- 1)) ?
250 (info
->start
[i
+ 1] - info
->start
[i
]) >> 2 :
251 (info
->start
[0] + info
->size
- info
->start
[i
]) >> 2;
253 for (flash
= (unsigned long *) info
->start
[i
], erased
= 1;
254 (flash
!= (unsigned long *) info
->start
[i
] + size
) && erased
;
256 erased
= *flash
== ~0x0UL
;
258 printf ("%s %08lX %s %s",
259 (i
% 5) ? "" : "\n ",
261 erased
? "E" : " ", info
->protect
[i
] ? "RO" : " ");
268 /*-----------------------------------------------------------------------
271 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
273 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*) (info
->start
[0]);
274 int flag
, prot
, sect
, l_sect
;
275 ulong start
, now
, last
;
278 if ((s_first
< 0) || (s_first
> s_last
)) {
279 if (info
->flash_id
== FLASH_UNKNOWN
) {
280 printf ("- missing\n");
282 printf ("- no sectors to erase\n");
287 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
288 (info
->flash_id
> (FLASH_MAN_STM
| FLASH_AMD_COMP
))) {
289 printf ("Can't erase unknown flash type - aborted\n");
294 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
295 if (info
->protect
[sect
]) {
301 printf ("- Warning: %d protected sectors will not be erased!\n",
309 /* Check the ROM CS */
310 if ((info
->start
[0] >= ROM_CS1_START
)
311 && (info
->start
[0] < ROM_CS0_START
))
316 /* Disable interrupts which might cause a timeout here */
317 flag
= disable_interrupts ();
319 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
320 addr
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00550055;
321 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00800080;
322 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
323 addr
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00550055;
325 /* Start erase on unprotected sectors */
326 for (sect
= s_first
; sect
<= s_last
; sect
++) {
327 if (info
->protect
[sect
] == 0) { /* not protected */
328 addr
= (FLASH_WORD_SIZE
*) (info
->start
[0] + ((info
->
334 if (info
->flash_id
& FLASH_MAN_SST
) {
335 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
336 addr
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00550055;
337 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00800080;
338 addr
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
339 addr
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00550055;
340 addr
[0] = (FLASH_WORD_SIZE
) 0x00500050; /* block erase */
341 udelay (30000); /* wait 30 ms */
343 addr
[0] = (FLASH_WORD_SIZE
) 0x00300030; /* sector erase */
350 /* re-enable interrupts if necessary */
352 enable_interrupts ();
354 /* wait at least 80us - let's wait 1 ms */
358 * We wait for the last triggered sector
363 start
= get_timer (0);
365 addr
= (FLASH_WORD_SIZE
*) (info
->start
[0] + ((info
->start
[l_sect
] -
368 while ((addr
[0] & (FLASH_WORD_SIZE
) 0x00800080) !=
369 (FLASH_WORD_SIZE
) 0x00800080) {
370 if ((now
= get_timer (start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
371 printf ("Timeout\n");
374 /* show that we're waiting */
375 if ((now
- last
) > 1000) { /* every second */
382 /* reset to read mode */
383 addr
= (FLASH_WORD_SIZE
*) info
->start
[0];
384 addr
[0] = (FLASH_WORD_SIZE
) 0x00F000F0; /* reset bank */
391 /*-----------------------------------------------------------------------
392 * Copy memory to flash, returns:
395 * 2 - Flash not erased
398 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
403 wp
= (addr
& ~3); /* get lower word aligned address */
406 * handle unaligned start bytes
408 if ((l
= addr
- wp
) != 0) {
410 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
411 data
= (data
<< 8) | (*(uchar
*) cp
);
413 for (; i
< 4 && cnt
> 0; ++i
) {
414 data
= (data
<< 8) | *src
++;
418 for (; cnt
== 0 && i
< 4; ++i
, ++cp
) {
419 data
= (data
<< 8) | (*(uchar
*) cp
);
422 if ((rc
= write_word (info
, wp
, data
)) != 0) {
429 * handle word aligned part
433 for (i
= 0; i
< 4; ++i
) {
434 data
= (data
<< 8) | *src
++;
436 if ((rc
= write_word (info
, wp
, data
)) != 0) {
448 * handle unaligned tail bytes
451 for (i
= 0, cp
= wp
; i
< 4 && cnt
> 0; ++i
, ++cp
) {
452 data
= (data
<< 8) | *src
++;
455 for (; i
< 4; ++i
, ++cp
) {
456 data
= (data
<< 8) | (*(uchar
*) cp
);
459 return (write_word (info
, wp
, data
));
463 /*-----------------------------------------------------------------------
464 * Write a word to Flash, returns:
467 * 2 - Flash not erased
469 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
471 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*) info
->start
[0];
472 volatile FLASH_WORD_SIZE
*dest2
;
473 volatile FLASH_WORD_SIZE
*data2
= (FLASH_WORD_SIZE
*) & data
;
479 /* Check the ROM CS */
480 if ((info
->start
[0] >= ROM_CS1_START
)
481 && (info
->start
[0] < ROM_CS0_START
))
486 dest2
= (FLASH_WORD_SIZE
*) (((dest
- info
->start
[0]) << sh8b
) +
489 /* Check if Flash is (sufficiently) erased */
490 if ((*dest2
& (FLASH_WORD_SIZE
) data
) != (FLASH_WORD_SIZE
) data
) {
493 /* Disable interrupts which might cause a timeout here */
494 flag
= disable_interrupts ();
496 for (i
= 0; i
< 4 / sizeof (FLASH_WORD_SIZE
); i
++) {
497 addr2
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
498 addr2
[ADDR1
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00550055;
499 addr2
[ADDR0
<< sh8b
] = (FLASH_WORD_SIZE
) 0x00A000A0;
501 dest2
[i
<< sh8b
] = data2
[i
];
503 /* re-enable interrupts if necessary */
505 enable_interrupts ();
507 /* data polling for D7 */
508 start
= get_timer (0);
509 while ((dest2
[i
<< sh8b
] & (FLASH_WORD_SIZE
) 0x00800080) !=
510 (data2
[i
] & (FLASH_WORD_SIZE
) 0x00800080)) {
511 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
520 /*-----------------------------------------------------------------------
523 static void write_via_fpu (vu_long
* addr
, ulong
* data
)
525 __asm__
__volatile__ ("lfd 1, 0(%0)"::"r" (data
));
526 __asm__
__volatile__ ("stfd 1, 0(%0)"::"r" (addr
));
530 /*-----------------------------------------------------------------------
532 static __inline__
unsigned long get_msr (void)
536 __asm__
__volatile__ ("mfmsr %0":"=r" (msr
):);
541 static __inline__
void set_msr (unsigned long msr
)
543 __asm__
__volatile__ ("mtmsr %0"::"r" (msr
));