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
]; /* info for FLASH chips */
13 /*-----------------------------------------------------------------------
16 static ulong
flash_get_size(vu_long
* addr
, flash_info_t
* info
);
17 static int write_byte(flash_info_t
* info
, ulong dest
, uchar data
);
18 static void flash_get_offsets(ulong base
, flash_info_t
* info
);
20 /*-----------------------------------------------------------------------
23 unsigned long flash_init(void)
25 volatile immap_t
*immap
= (immap_t
*) CONFIG_SYS_IMMR
;
26 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
30 /* Init: no FLASHes known */
31 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; ++i
)
32 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
34 size
= flash_get_size((vu_long
*) FLASH_BASE0_PRELIM
, &flash_info
[0]);
36 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
37 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size
, size
<< 20);
40 /* Remap FLASH according to real size */
41 memctl
->memc_or0
= CONFIG_SYS_OR_TIMING_FLASH
| (-size
& 0xFFFF8000);
42 memctl
->memc_br0
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) | (memctl
->memc_br0
& ~(BR_BA_MSK
));
44 /* Re-do sizing to get full correct info */
45 size
= flash_get_size((vu_long
*) CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
47 flash_get_offsets(CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
49 /* monitor protection ON by default */
50 flash_protect(FLAG_PROTECT_SET
,
51 CONFIG_SYS_FLASH_BASE
, CONFIG_SYS_FLASH_BASE
+ monitor_flash_len
- 1,
54 flash_protect ( FLAG_PROTECT_SET
,
56 CONFIG_ENV_ADDR
+ CONFIG_ENV_SECT_SIZE
- 1,
59 #ifdef CONFIG_ENV_ADDR_REDUND
60 flash_protect ( FLAG_PROTECT_SET
,
61 CONFIG_ENV_ADDR_REDUND
,
62 CONFIG_ENV_ADDR_REDUND
+ CONFIG_ENV_SECT_SIZE
- 1,
67 flash_info
[0].size
= size
;
72 /*-----------------------------------------------------------------------
74 static void flash_get_offsets(ulong base
, flash_info_t
* info
)
78 /* set up sector start address table */
79 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
80 for (i
= 0; i
< info
->sector_count
; i
++) {
81 info
->start
[i
] = base
+ (i
* 0x00010000);
83 } else if (info
->flash_id
& FLASH_BTYPE
) {
84 /* set sector offsets for bottom boot block type */
85 info
->start
[0] = base
+ 0x00000000;
86 info
->start
[1] = base
+ 0x00004000;
87 info
->start
[2] = base
+ 0x00006000;
88 info
->start
[3] = base
+ 0x00008000;
89 for (i
= 4; i
< info
->sector_count
; i
++) {
90 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
93 /* set sector offsets for top boot block type */
94 i
= info
->sector_count
- 1;
95 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
96 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
97 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
99 info
->start
[i
] = base
+ i
* 0x00010000;
105 /*-----------------------------------------------------------------------
107 void flash_print_info(flash_info_t
* info
)
111 if (info
->flash_id
== FLASH_UNKNOWN
) {
112 printf("missing or unknown FLASH type\n");
116 switch (info
->flash_id
& FLASH_VENDMASK
) {
127 printf("Unknown Vendor ");
131 switch (info
->flash_id
& FLASH_TYPEMASK
) {
133 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
136 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
139 printf("AM29LV400T (4 Mbit, top boot sector)\n");
142 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
145 printf("AM29LV800T (8 Mbit, top boot sector)\n");
148 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
151 printf("AM29LV160T (16 Mbit, top boot sector)\n");
154 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
157 printf("AM29LV320T (32 Mbit, top boot sector)\n");
160 printf("Unknown Chip Type\n");
164 printf(" Size: %ld MB in %d Sectors\n", info
->size
>> 20, info
->sector_count
);
166 printf(" Sector Start Addresses:");
167 for (i
= 0; i
< info
->sector_count
; ++i
) {
170 printf(" %08lX%s", info
->start
[i
], info
->protect
[i
] ? " (RO)" : " ");
175 /*-----------------------------------------------------------------------
179 /*-----------------------------------------------------------------------
183 * The following code cannot be run from FLASH!
186 static ulong
flash_get_size(vu_long
* addr
, flash_info_t
* info
)
191 vu_char
*caddr
= (vu_char
*) addr
;
192 ulong base
= (ulong
) addr
;
195 /* Write auto select command: read Manufacturer ID */
196 caddr
[0x0555] = 0xAA;
197 caddr
[0x02AA] = 0x55;
198 caddr
[0x0555] = 0x90;
202 case (AMD_MANUFACT
& 0xFF):
203 info
->flash_id
= FLASH_MAN_AMD
;
205 case (FUJ_MANUFACT
& 0xFF):
206 info
->flash_id
= FLASH_MAN_FUJ
;
208 case (MX_MANUFACT
& 0xFF):
209 info
->flash_id
= FLASH_MAN_MX
;
211 case (STM_MANUFACT
& 0xFF):
212 info
->flash_id
= FLASH_MAN_STM
;
215 info
->flash_id
= FLASH_UNKNOWN
;
216 info
->sector_count
= 0;
218 return (0); /* no or unknown flash */
221 pid
= caddr
[1]; /* device ID */
223 case (AMD_ID_LV400T
& 0xFF):
224 info
->flash_id
+= FLASH_AM400T
;
225 info
->sector_count
= 11;
226 info
->size
= 0x00080000;
227 break; /* => 512 kB */
229 case (AMD_ID_LV400B
& 0xFF):
230 info
->flash_id
+= FLASH_AM400B
;
231 info
->sector_count
= 11;
232 info
->size
= 0x00080000;
233 break; /* => 512 kB */
235 case (AMD_ID_LV800T
& 0xFF):
236 info
->flash_id
+= FLASH_AM800T
;
237 info
->sector_count
= 19;
238 info
->size
= 0x00100000;
241 case (AMD_ID_LV800B
& 0xFF):
242 info
->flash_id
+= FLASH_AM800B
;
243 info
->sector_count
= 19;
244 info
->size
= 0x00100000;
247 case (AMD_ID_LV160T
& 0xFF):
248 info
->flash_id
+= FLASH_AM160T
;
249 info
->sector_count
= 35;
250 info
->size
= 0x00200000;
253 case (AMD_ID_LV160B
& 0xFF):
254 info
->flash_id
+= FLASH_AM160B
;
255 info
->sector_count
= 35;
256 info
->size
= 0x00200000;
259 case (AMD_ID_LV040B
& 0xFF):
260 info
->flash_id
+= FLASH_AM040
;
261 info
->sector_count
= 8;
262 info
->size
= 0x00080000;
265 case (STM_ID_M29W040B
& 0xFF):
266 info
->flash_id
+= FLASH_AM040
;
267 info
->sector_count
= 8;
268 info
->size
= 0x00080000;
271 #if 0 /* enable when device IDs are available */
272 case (AMD_ID_LV320T
& 0xFF):
273 info
->flash_id
+= FLASH_AM320T
;
274 info
->sector_count
= 67;
275 info
->size
= 0x00400000;
278 case (AMD_ID_LV320B
& 0xFF):
279 info
->flash_id
+= FLASH_AM320B
;
280 info
->sector_count
= 67;
281 info
->size
= 0x00400000;
285 info
->flash_id
= FLASH_UNKNOWN
;
286 return (0); /* => no or unknown flash */
291 /* set up sector start address table */
292 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
293 for (i
= 0; i
< info
->sector_count
; i
++) {
294 info
->start
[i
] = base
+ (i
* 0x00010000);
296 } else if (info
->flash_id
& FLASH_BTYPE
) {
297 /* set sector offsets for bottom boot block type */
298 info
->start
[0] = base
+ 0x00000000;
299 info
->start
[1] = base
+ 0x00004000;
300 info
->start
[2] = base
+ 0x00006000;
301 info
->start
[3] = base
+ 0x00008000;
302 for (i
= 4; i
< info
->sector_count
; i
++) {
303 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
306 /* set sector offsets for top boot block type */
307 i
= info
->sector_count
- 1;
308 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
309 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
310 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
311 for (; i
>= 0; i
--) {
312 info
->start
[i
] = base
+ i
* 0x00010000;
316 /* check for protected sectors */
317 for (i
= 0; i
< info
->sector_count
; i
++) {
318 /* read sector protection: D0 = 1 if protected */
319 caddr
= (volatile unsigned char *)(info
->start
[i
]);
320 info
->protect
[i
] = caddr
[2] & 1;
324 * Prevent writes to uninitialized FLASH.
326 if (info
->flash_id
!= FLASH_UNKNOWN
) {
327 caddr
= (vu_char
*) info
->start
[0];
329 caddr
[0x0555] = 0xAA;
330 caddr
[0x02AA] = 0x55;
331 caddr
[0x0555] = 0xF0;
340 /*-----------------------------------------------------------------------
343 int flash_erase(flash_info_t
* info
, int s_first
, int s_last
)
345 vu_char
*addr
= (vu_char
*) (info
->start
[0]);
346 int flag
, prot
, sect
, l_sect
;
347 ulong start
, now
, last
;
349 if ((s_first
< 0) || (s_first
> s_last
)) {
350 if (info
->flash_id
== FLASH_UNKNOWN
) {
351 printf("- missing\n");
353 printf("- no sectors to erase\n");
358 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
359 (info
->flash_id
> FLASH_AMD_COMP
)) {
360 printf("Can't erase unknown flash type %08lx - aborted\n", info
->flash_id
);
365 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
366 if (info
->protect
[sect
]) {
372 printf("- Warning: %d protected sectors will not be erased!\n", prot
);
379 /* Disable interrupts which might cause a timeout here */
380 flag
= disable_interrupts();
388 /* Start erase on unprotected sectors */
389 for (sect
= s_first
; sect
<= s_last
; sect
++) {
390 if (info
->protect
[sect
] == 0) { /* not protected */
391 addr
= (vu_char
*) (info
->start
[sect
]);
397 /* re-enable interrupts if necessary */
401 /* wait at least 80us - let's wait 1 ms */
405 * We wait for the last triggered sector
410 start
= get_timer(0);
412 addr
= (vu_char
*) (info
->start
[l_sect
]);
413 while ((addr
[0] & 0x80) != 0x80) {
414 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
418 /* show that we're waiting */
419 if ((now
- last
) > 1000) { /* every second */
426 /* reset to read mode */
427 addr
= (vu_char
*) info
->start
[0];
428 addr
[0] = 0xF0; /* reset bank */
434 /*-----------------------------------------------------------------------
435 * Copy memory to flash, returns:
438 * 2 - Flash not erased
441 int write_buff(flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
446 if ((rc
= write_byte(info
, addr
++, *src
++)) != 0) {
455 /*-----------------------------------------------------------------------
456 * Write a word to Flash, returns:
459 * 2 - Flash not erased
461 static int write_byte(flash_info_t
* info
, ulong dest
, uchar data
)
463 vu_char
*addr
= (vu_char
*) (info
->start
[0]);
467 /* Check if Flash is (sufficiently) erased */
468 if ((*((vu_char
*) dest
) & data
) != data
) {
471 /* Disable interrupts which might cause a timeout here */
472 flag
= disable_interrupts();
478 *((vu_char
*) dest
) = data
;
480 /* re-enable interrupts if necessary */
484 /* data polling for D7 */
485 start
= get_timer(0);
486 while ((*((vu_char
*) dest
) & 0x80) != (data
& 0x80)) {
487 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
494 /*-----------------------------------------------------------------------