2 * (C) Copyright 2000-2004
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
;
28 #if CONFIG_NETPHONE_VERSION == 2
33 /* Init: no FLASHes known */
34 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; ++i
)
35 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
37 size
= flash_get_size((vu_long
*) FLASH_BASE0_PRELIM
, &flash_info
[0]);
39 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
40 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
44 /* Remap FLASH according to real size */
45 memctl
->memc_or0
= CONFIG_SYS_OR_TIMING_FLASH
| (-size
& 0xFFFF8000);
46 memctl
->memc_br0
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) | (memctl
->memc_br0
& ~(BR_BA_MSK
));
48 /* Re-do sizing to get full correct info */
49 size
= flash_get_size((vu_long
*) CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
51 flash_get_offsets(CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
53 /* monitor protection ON by default */
54 flash_protect(FLAG_PROTECT_SET
,
55 CONFIG_SYS_FLASH_BASE
, CONFIG_SYS_FLASH_BASE
+ monitor_flash_len
- 1,
58 flash_protect ( FLAG_PROTECT_SET
,
60 CONFIG_ENV_ADDR
+ CONFIG_ENV_SECT_SIZE
- 1,
63 #ifdef CONFIG_ENV_ADDR_REDUND
64 flash_protect ( FLAG_PROTECT_SET
,
65 CONFIG_ENV_ADDR_REDUND
,
66 CONFIG_ENV_ADDR_REDUND
+ CONFIG_ENV_SECT_SIZE
- 1,
70 flash_info
[0].size
= size
;
72 #if CONFIG_NETPHONE_VERSION == 2
73 size1
= flash_get_size((vu_long
*) FLASH_BASE4_PRELIM
, &flash_info
[1]);
75 if (flash_info
[1].flash_id
== FLASH_UNKNOWN
)
76 printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n", size1
, size1
<< 20);
78 /* Remap FLASH according to real size */
79 memctl
->memc_or4
= CONFIG_SYS_OR_TIMING_FLASH
| (-size1
& 0xFFFF8000);
80 memctl
->memc_br4
= (CONFIG_SYS_FLASH_BASE4
& BR_BA_MSK
) | (memctl
->memc_br4
& ~(BR_BA_MSK
));
82 /* Re-do sizing to get full correct info */
83 size1
= flash_get_size((vu_long
*) CONFIG_SYS_FLASH_BASE4
, &flash_info
[1]);
85 flash_get_offsets(CONFIG_SYS_FLASH_BASE4
, &flash_info
[1]);
89 memctl
->memc_br4
&= ~BR_V
;
95 /*-----------------------------------------------------------------------
97 static void flash_get_offsets(ulong base
, flash_info_t
* info
)
101 /* set up sector start address table */
102 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
103 for (i
= 0; i
< info
->sector_count
; i
++) {
104 info
->start
[i
] = base
+ (i
* 0x00010000);
106 } else if (info
->flash_id
& FLASH_BTYPE
) {
107 /* set sector offsets for bottom boot block type */
108 info
->start
[0] = base
+ 0x00000000;
109 info
->start
[1] = base
+ 0x00004000;
110 info
->start
[2] = base
+ 0x00006000;
111 info
->start
[3] = base
+ 0x00008000;
112 for (i
= 4; i
< info
->sector_count
; i
++) {
113 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
116 /* set sector offsets for top boot block type */
117 i
= info
->sector_count
- 1;
118 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
119 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
120 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
121 for (; i
>= 0; i
--) {
122 info
->start
[i
] = base
+ i
* 0x00010000;
127 /*-----------------------------------------------------------------------
129 void flash_print_info(flash_info_t
* info
)
133 if (info
->flash_id
== FLASH_UNKNOWN
) {
134 printf("missing or unknown FLASH type\n");
138 switch (info
->flash_id
& FLASH_VENDMASK
) {
149 printf("Unknown Vendor ");
153 switch (info
->flash_id
& FLASH_TYPEMASK
) {
155 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
158 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
161 printf("AM29LV400T (4 Mbit, top boot sector)\n");
164 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
167 printf("AM29LV800T (8 Mbit, top boot sector)\n");
170 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
173 printf("AM29LV160T (16 Mbit, top boot sector)\n");
176 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
179 printf("AM29LV320T (32 Mbit, top boot sector)\n");
182 printf("Unknown Chip Type\n");
186 printf(" Size: %ld MB in %d Sectors\n", info
->size
>> 20, info
->sector_count
);
188 printf(" Sector Start Addresses:");
189 for (i
= 0; i
< info
->sector_count
; ++i
) {
192 printf(" %08lX%s", info
->start
[i
], info
->protect
[i
] ? " (RO)" : " ");
197 /*-----------------------------------------------------------------------
201 /*-----------------------------------------------------------------------
205 * The following code cannot be run from FLASH!
208 static ulong
flash_get_size(vu_long
* addr
, flash_info_t
* info
)
213 vu_char
*caddr
= (vu_char
*) addr
;
214 ulong base
= (ulong
) addr
;
216 /* Write auto select command: read Manufacturer ID */
217 caddr
[0x0555] = 0xAA;
218 caddr
[0x02AA] = 0x55;
219 caddr
[0x0555] = 0x90;
223 case (AMD_MANUFACT
& 0xFF):
224 info
->flash_id
= FLASH_MAN_AMD
;
226 case (FUJ_MANUFACT
& 0xFF):
227 info
->flash_id
= FLASH_MAN_FUJ
;
229 case (MX_MANUFACT
& 0xFF):
230 info
->flash_id
= FLASH_MAN_MX
;
232 case (STM_MANUFACT
& 0xFF):
233 info
->flash_id
= FLASH_MAN_STM
;
236 info
->flash_id
= FLASH_UNKNOWN
;
237 info
->sector_count
= 0;
239 return (0); /* no or unknown flash */
242 pid
= caddr
[1]; /* device ID */
244 case (AMD_ID_LV400T
& 0xFF):
245 info
->flash_id
+= FLASH_AM400T
;
246 info
->sector_count
= 11;
247 info
->size
= 0x00080000;
248 break; /* => 512 kB */
250 case (AMD_ID_LV400B
& 0xFF):
251 info
->flash_id
+= FLASH_AM400B
;
252 info
->sector_count
= 11;
253 info
->size
= 0x00080000;
254 break; /* => 512 kB */
256 case (AMD_ID_LV800T
& 0xFF):
257 info
->flash_id
+= FLASH_AM800T
;
258 info
->sector_count
= 19;
259 info
->size
= 0x00100000;
262 case (AMD_ID_LV800B
& 0xFF):
263 info
->flash_id
+= FLASH_AM800B
;
264 info
->sector_count
= 19;
265 info
->size
= 0x00100000;
268 case (AMD_ID_LV160T
& 0xFF):
269 info
->flash_id
+= FLASH_AM160T
;
270 info
->sector_count
= 35;
271 info
->size
= 0x00200000;
274 case (AMD_ID_LV160B
& 0xFF):
275 info
->flash_id
+= FLASH_AM160B
;
276 info
->sector_count
= 35;
277 info
->size
= 0x00200000;
280 case (AMD_ID_LV040B
& 0xFF):
281 info
->flash_id
+= FLASH_AM040
;
282 info
->sector_count
= 8;
283 info
->size
= 0x00080000;
286 case (STM_ID_M29W040B
& 0xFF):
287 info
->flash_id
+= FLASH_AM040
;
288 info
->sector_count
= 8;
289 info
->size
= 0x00080000;
292 #if 0 /* enable when device IDs are available */
293 case (AMD_ID_LV320T
& 0xFF):
294 info
->flash_id
+= FLASH_AM320T
;
295 info
->sector_count
= 67;
296 info
->size
= 0x00400000;
299 case (AMD_ID_LV320B
& 0xFF):
300 info
->flash_id
+= FLASH_AM320B
;
301 info
->sector_count
= 67;
302 info
->size
= 0x00400000;
306 info
->flash_id
= FLASH_UNKNOWN
;
307 return (0); /* => no or unknown flash */
312 /* set up sector start address table */
313 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
314 for (i
= 0; i
< info
->sector_count
; i
++) {
315 info
->start
[i
] = base
+ (i
* 0x00010000);
317 } else if (info
->flash_id
& FLASH_BTYPE
) {
318 /* set sector offsets for bottom boot block type */
319 info
->start
[0] = base
+ 0x00000000;
320 info
->start
[1] = base
+ 0x00004000;
321 info
->start
[2] = base
+ 0x00006000;
322 info
->start
[3] = base
+ 0x00008000;
323 for (i
= 4; i
< info
->sector_count
; i
++) {
324 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
327 /* set sector offsets for top boot block type */
328 i
= info
->sector_count
- 1;
329 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
330 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
331 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
332 for (; i
>= 0; i
--) {
333 info
->start
[i
] = base
+ i
* 0x00010000;
337 /* check for protected sectors */
338 for (i
= 0; i
< info
->sector_count
; i
++) {
339 /* read sector protection: D0 = 1 if protected */
340 caddr
= (volatile unsigned char *)(info
->start
[i
]);
341 info
->protect
[i
] = caddr
[2] & 1;
345 * Prevent writes to uninitialized FLASH.
347 if (info
->flash_id
!= FLASH_UNKNOWN
) {
348 caddr
= (vu_char
*) info
->start
[0];
350 caddr
[0x0555] = 0xAA;
351 caddr
[0x02AA] = 0x55;
352 caddr
[0x0555] = 0xF0;
361 /*-----------------------------------------------------------------------
364 int flash_erase(flash_info_t
* info
, int s_first
, int s_last
)
366 vu_char
*addr
= (vu_char
*) (info
->start
[0]);
367 int flag
, prot
, sect
, l_sect
;
368 ulong start
, now
, last
;
370 if ((s_first
< 0) || (s_first
> s_last
)) {
371 if (info
->flash_id
== FLASH_UNKNOWN
) {
372 printf("- missing\n");
374 printf("- no sectors to erase\n");
379 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
380 (info
->flash_id
> FLASH_AMD_COMP
)) {
381 printf("Can't erase unknown flash type %08lx - aborted\n", info
->flash_id
);
386 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
387 if (info
->protect
[sect
]) {
393 printf("- Warning: %d protected sectors will not be erased!\n", prot
);
400 /* Disable interrupts which might cause a timeout here */
401 flag
= disable_interrupts();
409 /* Start erase on unprotected sectors */
410 for (sect
= s_first
; sect
<= s_last
; sect
++) {
411 if (info
->protect
[sect
] == 0) { /* not protected */
412 addr
= (vu_char
*) (info
->start
[sect
]);
418 /* re-enable interrupts if necessary */
422 /* wait at least 80us - let's wait 1 ms */
426 * We wait for the last triggered sector
431 start
= get_timer(0);
433 addr
= (vu_char
*) (info
->start
[l_sect
]);
434 while ((addr
[0] & 0x80) != 0x80) {
435 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
439 /* show that we're waiting */
440 if ((now
- last
) > 1000) { /* every second */
447 /* reset to read mode */
448 addr
= (vu_char
*) info
->start
[0];
449 addr
[0] = 0xF0; /* reset bank */
455 /*-----------------------------------------------------------------------
456 * Copy memory to flash, returns:
459 * 2 - Flash not erased
462 int write_buff(flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
467 if ((rc
= write_byte(info
, addr
++, *src
++)) != 0) {
476 /*-----------------------------------------------------------------------
477 * Write a word to Flash, returns:
480 * 2 - Flash not erased
482 static int write_byte(flash_info_t
* info
, ulong dest
, uchar data
)
484 vu_char
*addr
= (vu_char
*) (info
->start
[0]);
488 /* Check if Flash is (sufficiently) erased */
489 if ((*((vu_char
*) dest
) & data
) != data
) {
492 /* Disable interrupts which might cause a timeout here */
493 flag
= disable_interrupts();
499 *((vu_char
*) dest
) = data
;
501 /* re-enable interrupts if necessary */
505 /* data polling for D7 */
506 start
= get_timer(0);
507 while ((*((vu_char
*) dest
) & 0x80) != (data
& 0x80)) {
508 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {