2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
13 /*-----------------------------------------------------------------------
16 static ulong
flash_get_size(vu_long
*addr
, flash_info_t
*info
);
17 static int write_word(flash_info_t
*info
, ulong dest
, ulong data
);
18 static void flash_get_offsets(ulong base
, flash_info_t
*info
);
20 /*-----------------------------------------------------------------------
23 unsigned long flash_init(void)
25 unsigned long size_b0
;
28 /* Init: no FLASHes known */
29 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; ++i
)
30 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
33 size_b0
= flash_get_size((vu_long
*)CONFIG_SYS_FLASH_BASE
,
37 flash_get_offsets(CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
39 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
40 /* Monitor protection ON by default */
41 flash_protect(FLAG_PROTECT_SET
,
42 CONFIG_SYS_MONITOR_BASE
,
43 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
47 flash_info
[0].size
= size_b0
;
52 /*-----------------------------------------------------------------------
53 * Fix this to support variable sector sizes
55 static void flash_get_offsets(ulong base
, flash_info_t
*info
)
59 /* set up sector start address table */
60 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
61 /* set sector offsets for bottom boot block type */
62 for (i
= 0; i
< info
->sector_count
; i
++)
63 info
->start
[i
] = base
+ (i
* 0x00010000);
67 /*-----------------------------------------------------------------------
69 void flash_print_info(flash_info_t
*info
)
73 if (info
->flash_id
== FLASH_UNKNOWN
) {
74 puts("missing or unknown FLASH type\n");
78 switch (info
->flash_id
& FLASH_VENDMASK
) {
86 printf("BRIGHT MICRO ");
89 printf("Unknown Vendor ");
93 switch (info
->flash_id
& FLASH_TYPEMASK
) {
95 printf("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
98 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
101 printf("AM29LV400T (4 Mbit, top boot sector)\n");
104 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
107 printf("AM29LV800T (8 Mbit, top boot sector)\n");
110 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
113 printf("AM29LV160T (16 Mbit, top boot sector)\n");
116 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
119 printf("AM29LV320T (32 Mbit, top boot sector)\n");
122 printf("Unknown Chip Type\n");
126 if (info
->size
>> 20) {
127 printf(" Size: %ld MB in %d Sectors\n",
131 printf(" Size: %ld KB in %d Sectors\n",
136 puts(" Sector Start Addresses:");
138 for (i
= 0; i
< info
->sector_count
; ++i
) {
144 info
->protect
[i
] ? " (RO)" : " ");
150 /*-----------------------------------------------------------------------
154 * The following code cannot be run from FLASH!
157 static ulong
flash_get_size(vu_long
*addr
, flash_info_t
*info
)
160 volatile unsigned char *caddr
;
163 caddr
= (volatile unsigned char *)addr
;
165 /* Write auto select command: read Manufacturer ID */
167 debug("Base address is: %8p\n", caddr
);
169 caddr
[0x0555] = 0xAA;
170 caddr
[0x02AA] = 0x55;
171 caddr
[0x0555] = 0x90;
175 debug("Manufact ID: %02x\n", value
);
178 case 0x1: /* AMD_MANUFACT */
179 info
->flash_id
= FLASH_MAN_AMD
;
181 case 0x4: /* FUJ_MANUFACT */
182 info
->flash_id
= FLASH_MAN_FUJ
;
185 info
->flash_id
= FLASH_UNKNOWN
;
186 info
->sector_count
= 0;
191 value
= caddr
[1]; /* device ID */
193 debug("Device ID: %02x\n", value
);
197 info
->flash_id
+= FLASH_AM040
;
198 info
->sector_count
= 8;
199 info
->size
= 0x00080000;
200 break; /* => 512Kb */
203 info
->flash_id
= FLASH_UNKNOWN
;
204 return 0; /* => no or unknown flash */
207 flash_get_offsets((ulong
)addr
, &flash_info
[0]);
209 /* check for protected sectors */
210 for (i
= 0; i
< info
->sector_count
; i
++) {
212 * read sector protection at sector address,
214 * D0 = 1 if protected
216 caddr
= (volatile unsigned char *)(info
->start
[i
]);
217 info
->protect
[i
] = caddr
[2] & 1;
221 * Prevent writes to uninitialized FLASH.
223 if (info
->flash_id
!= FLASH_UNKNOWN
) {
224 caddr
= (volatile unsigned char *)info
->start
[0];
225 *caddr
= 0xF0; /* reset bank */
231 int flash_erase(flash_info_t
*info
, int s_first
, int s_last
)
233 volatile unsigned char *addr
=
234 (volatile unsigned char *)(info
->start
[0]);
235 int flag
, prot
, sect
, l_sect
;
236 ulong start
, now
, last
;
238 if ((s_first
< 0) || (s_first
> s_last
)) {
239 if (info
->flash_id
== FLASH_UNKNOWN
)
240 printf("- missing\n");
242 printf("- no sectors to erase\n");
247 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
248 (info
->flash_id
> FLASH_AMD_COMP
)) {
249 printf("Can't erase unknown flash type - aborted\n");
254 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
255 if (info
->protect
[sect
])
260 printf("- Warning: %d protected sectors will not be erased!\n",
268 /* Disable interrupts which might cause a timeout here */
269 flag
= disable_interrupts();
277 /* Start erase on unprotected sectors */
278 for (sect
= s_first
; sect
<= s_last
; sect
++) {
279 if (info
->protect
[sect
] == 0) { /* not protected */
280 addr
= (volatile unsigned char *)(info
->start
[sect
]);
286 /* re-enable interrupts if necessary */
290 /* wait at least 80us - let's wait 1 ms */
294 * We wait for the last triggered sector
299 start
= get_timer(0);
301 addr
= (volatile unsigned char *)(info
->start
[l_sect
]);
303 while ((addr
[0] & 0xFF) != 0xFF) {
305 now
= get_timer(start
);
307 if (now
> CONFIG_SYS_FLASH_ERASE_TOUT
) {
311 /* show that we're waiting */
312 if ((now
- last
) > 1000) { /* every second */
319 /* reset to read mode */
320 addr
= (volatile unsigned char *)info
->start
[0];
322 addr
[0] = 0xF0; /* reset bank */
328 /*-----------------------------------------------------------------------
329 * Copy memory to flash, returns:
332 * 2 - Flash not erased
335 int write_buff(flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
340 wp
= (addr
& ~3); /* get lower word aligned address */
343 * handle unaligned start bytes
349 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
)
350 data
= (data
<< 8) | (*(uchar
*)cp
);
352 for (; i
< 4 && cnt
> 0; ++i
) {
353 data
= (data
<< 8) | *src
++;
357 for (; cnt
== 0 && i
< 4; ++i
, ++cp
)
358 data
= (data
<< 8) | (*(uchar
*)cp
);
360 rc
= write_word(info
, wp
, data
);
369 * handle word aligned part
373 for (i
= 0; i
< 4; ++i
)
374 data
= (data
<< 8) | *src
++;
376 rc
= write_word(info
, wp
, data
);
389 * handle unaligned tail bytes
392 for (i
= 0, cp
= wp
; i
< 4 && cnt
> 0; ++i
, ++cp
) {
393 data
= (data
<< 8) | *src
++;
396 for (; i
< 4; ++i
, ++cp
)
397 data
= (data
<< 8) | (*(uchar
*)cp
);
399 return write_word(info
, wp
, data
);
402 /*-----------------------------------------------------------------------
403 * Write a word to Flash, returns:
406 * 2 - Flash not erased
408 static int write_word(flash_info_t
*info
, ulong dest
, ulong data
)
410 volatile unsigned char *cdest
, *cdata
;
411 volatile unsigned char *addr
=
412 (volatile unsigned char *)(info
->start
[0]);
414 int flag
, count
= 4 ;
416 cdest
= (volatile unsigned char *)dest
;
417 cdata
= (volatile unsigned char *)&data
;
419 /* Check if Flash is (sufficiently) erased */
420 if ((*((vu_long
*)dest
) & data
) != data
)
425 /* Disable interrupts which might cause a timeout here */
426 flag
= disable_interrupts();
434 /* re-enable interrupts if necessary */
438 /* data polling for D7 */
439 start
= get_timer(0);
440 while ((*cdest
^ *cdata
) & 0x80) {
441 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
)