]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/c2mon/flash.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
31 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
33 /*-----------------------------------------------------------------------
36 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
37 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
38 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
40 /*-----------------------------------------------------------------------
43 unsigned long flash_init (void)
45 volatile immap_t
*immap
= (immap_t
*)CFG_IMMR
;
46 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
47 unsigned long size_b0
, size_b1
;
50 /* Init: no FLASHes known */
51 for (i
=0; i
<CFG_MAX_FLASH_BANKS
; ++i
) {
52 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
55 /* Static FLASH Bank configuration here - FIXME XXX */
57 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
59 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
60 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
61 size_b0
, size_b0
<<20);
64 size_b1
= flash_get_size((vu_long
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
66 if (size_b1
> size_b0
) {
68 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
72 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
73 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
74 flash_info
[0].sector_count
= -1;
75 flash_info
[1].sector_count
= -1;
76 flash_info
[0].size
= 0;
77 flash_info
[1].size
= 0;
81 /* Remap FLASH according to real size */
82 memctl
->memc_or0
= CFG_OR_TIMING_FLASH
| (-size_b0
& OR_AM_MSK
);
83 memctl
->memc_br0
= (CFG_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_V
;
85 /* Re-do sizing to get full correct info */
86 size_b0
= flash_get_size((vu_long
*)CFG_FLASH_BASE
, &flash_info
[0]);
88 flash_get_offsets (CFG_FLASH_BASE
, &flash_info
[0]);
90 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
91 /* monitor protection ON by default */
92 flash_protect(FLAG_PROTECT_SET
,
94 CFG_MONITOR_BASE
+monitor_flash_len
-1,
98 #ifdef CONFIG_ENV_IS_IN_FLASH
99 /* ENV protection ON by default */
100 flash_protect(FLAG_PROTECT_SET
,
102 CFG_ENV_ADDR
+CFG_ENV_SIZE
-1,
107 memctl
->memc_or1
= CFG_OR_TIMING_FLASH
| (-size_b1
& 0xFFFF8000);
108 memctl
->memc_br1
= ((CFG_FLASH_BASE
+ size_b0
) & BR_BA_MSK
) |
111 /* Re-do sizing to get full correct info */
112 size_b1
= flash_get_size((vu_long
*)(CFG_FLASH_BASE
+ size_b0
),
115 flash_get_offsets (CFG_FLASH_BASE
+ size_b0
, &flash_info
[1]);
117 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
118 /* monitor protection ON by default */
119 flash_protect(FLAG_PROTECT_SET
,
121 CFG_MONITOR_BASE
+monitor_flash_len
-1,
125 #ifdef CONFIG_ENV_IS_IN_FLASH
126 /* ENV protection ON by default */
127 flash_protect(FLAG_PROTECT_SET
,
129 CFG_ENV_ADDR
+CFG_ENV_SIZE
-1,
133 memctl
->memc_br1
= 0; /* invalidate bank */
135 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
136 flash_info
[1].sector_count
= -1;
139 flash_info
[0].size
= size_b0
;
140 flash_info
[1].size
= size_b1
;
142 return (size_b0
+ size_b1
);
145 /*-----------------------------------------------------------------------
147 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
151 /* set up sector start address table */
152 if (info
->flash_id
& FLASH_BTYPE
) {
153 /* set sector offsets for bottom boot block type */
154 info
->start
[0] = base
+ 0x00000000;
155 info
->start
[1] = base
+ 0x00008000;
156 info
->start
[2] = base
+ 0x0000C000;
157 info
->start
[3] = base
+ 0x00010000;
158 for (i
= 4; i
< info
->sector_count
; i
++) {
159 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
162 /* set sector offsets for top boot block type */
163 i
= info
->sector_count
- 1;
164 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
165 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
166 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
167 for (; i
>= 0; i
--) {
168 info
->start
[i
] = base
+ i
* 0x00020000;
173 /*-----------------------------------------------------------------------
175 void flash_print_info (flash_info_t
*info
)
179 if (info
->flash_id
== FLASH_UNKNOWN
) {
180 printf ("missing or unknown FLASH type\n");
184 switch (info
->flash_id
& FLASH_VENDMASK
) {
185 case FLASH_MAN_AMD
: printf ("AMD "); break;
186 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
187 default: printf ("Unknown Vendor "); break;
190 switch (info
->flash_id
& FLASH_TYPEMASK
) {
191 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
193 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
195 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
197 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
199 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
201 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
203 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
205 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
207 default: printf ("Unknown Chip Type\n");
211 printf (" Size: %ld MB in %d Sectors\n",
212 info
->size
>> 20, info
->sector_count
);
214 printf (" Sector Start Addresses:");
215 for (i
=0; i
<info
->sector_count
; ++i
) {
220 info
->protect
[i
] ? " (RO)" : " "
227 /*-----------------------------------------------------------------------
231 /*-----------------------------------------------------------------------
235 * The following code cannot be run from FLASH!
238 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
242 ulong base
= (ulong
)addr
;
244 /* Write auto select command: read Manufacturer ID */
245 addr
[0x0555] = 0x00AA00AA;
246 addr
[0x02AA] = 0x00550055;
247 addr
[0x0555] = 0x00900090;
253 info
->flash_id
= FLASH_MAN_AMD
;
256 info
->flash_id
= FLASH_MAN_FUJ
;
259 info
->flash_id
= FLASH_UNKNOWN
;
260 info
->sector_count
= 0;
262 return (0); /* no or unknown flash */
265 value
= addr
[1]; /* device ID */
269 info
->flash_id
+= FLASH_AM400T
;
270 info
->sector_count
= 11;
271 info
->size
= 0x00100000;
275 info
->flash_id
+= FLASH_AM400B
;
276 info
->sector_count
= 11;
277 info
->size
= 0x00100000;
281 info
->flash_id
+= FLASH_AM800T
;
282 info
->sector_count
= 19;
283 info
->size
= 0x00200000;
287 info
->flash_id
+= FLASH_AM800B
;
288 info
->sector_count
= 19;
289 info
->size
= 0x00200000;
293 info
->flash_id
+= FLASH_AM160T
;
294 info
->sector_count
= 35;
295 info
->size
= 0x00400000;
299 info
->flash_id
+= FLASH_AM160B
;
300 info
->sector_count
= 35;
301 info
->size
= 0x00400000;
303 #if 0 /* enable when device IDs are available */
305 info
->flash_id
+= FLASH_AM320T
;
306 info
->sector_count
= 67;
307 info
->size
= 0x00800000;
311 info
->flash_id
+= FLASH_AM320B
;
312 info
->sector_count
= 67;
313 info
->size
= 0x00800000;
317 info
->flash_id
= FLASH_UNKNOWN
;
318 return (0); /* => no or unknown flash */
321 /* set up sector start address table */
322 if (info
->flash_id
& FLASH_BTYPE
) {
323 /* set sector offsets for bottom boot block type */
324 info
->start
[0] = base
+ 0x00000000;
325 info
->start
[1] = base
+ 0x00008000;
326 info
->start
[2] = base
+ 0x0000C000;
327 info
->start
[3] = base
+ 0x00010000;
328 for (i
= 4; i
< info
->sector_count
; i
++) {
329 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
332 /* set sector offsets for top boot block type */
333 i
= info
->sector_count
- 1;
334 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
335 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
336 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
337 for (; i
>= 0; i
--) {
338 info
->start
[i
] = base
+ i
* 0x00020000;
342 /* check for protected sectors */
343 for (i
= 0; i
< info
->sector_count
; i
++) {
344 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
345 /* D0 = 1 if protected */
346 addr
= (volatile unsigned long *)(info
->start
[i
]);
347 info
->protect
[i
] = addr
[2] & 1;
351 * Prevent writes to uninitialized FLASH.
353 if (info
->flash_id
!= FLASH_UNKNOWN
) {
354 addr
= (volatile unsigned long *)info
->start
[0];
356 *addr
= 0x00F000F0; /* reset bank */
363 /*-----------------------------------------------------------------------
366 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
368 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
369 int flag
, prot
, sect
, l_sect
;
370 ulong start
, now
, last
;
372 if ((s_first
< 0) || (s_first
> s_last
)) {
373 if (info
->flash_id
== FLASH_UNKNOWN
) {
374 printf ("- missing\n");
376 printf ("- no sectors to erase\n");
381 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
382 (info
->flash_id
> FLASH_AMD_COMP
)) {
383 printf ("Can't erase unknown flash type %08lx - aborted\n",
389 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
390 if (info
->protect
[sect
]) {
396 printf ("- Warning: %d protected sectors will not be erased!\n",
404 /* Disable interrupts which might cause a timeout here */
405 flag
= disable_interrupts();
407 addr
[0x0555] = 0x00AA00AA;
408 addr
[0x02AA] = 0x00550055;
409 addr
[0x0555] = 0x00800080;
410 addr
[0x0555] = 0x00AA00AA;
411 addr
[0x02AA] = 0x00550055;
413 /* Start erase on unprotected sectors */
414 for (sect
= s_first
; sect
<=s_last
; sect
++) {
415 if (info
->protect
[sect
] == 0) { /* not protected */
416 addr
= (vu_long
*)(info
->start
[sect
]);
417 addr
[0] = 0x00300030;
422 /* re-enable interrupts if necessary */
426 /* wait at least 80us - let's wait 1 ms */
430 * We wait for the last triggered sector
435 start
= get_timer (0);
437 addr
= (vu_long
*)(info
->start
[l_sect
]);
438 while ((addr
[0] & 0x00800080) != 0x00800080) {
439 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
440 printf ("Timeout\n");
443 /* show that we're waiting */
444 if ((now
- last
) > 1000) { /* every second */
451 /* reset to read mode */
452 addr
= (volatile unsigned long *)info
->start
[0];
453 addr
[0] = 0x00F000F0; /* reset bank */
459 /*-----------------------------------------------------------------------
460 * Copy memory to flash, returns:
463 * 2 - Flash not erased
466 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
471 wp
= (addr
& ~3); /* get lower word aligned address */
474 * handle unaligned start bytes
476 if ((l
= addr
- wp
) != 0) {
478 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
479 data
= (data
<< 8) | (*(uchar
*)cp
);
481 for (; i
<4 && cnt
>0; ++i
) {
482 data
= (data
<< 8) | *src
++;
486 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
487 data
= (data
<< 8) | (*(uchar
*)cp
);
490 if ((rc
= write_word(info
, wp
, data
)) != 0) {
497 * handle word aligned part
501 for (i
=0; i
<4; ++i
) {
502 data
= (data
<< 8) | *src
++;
504 if ((rc
= write_word(info
, wp
, data
)) != 0) {
516 * handle unaligned tail bytes
519 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
520 data
= (data
<< 8) | *src
++;
523 for (; i
<4; ++i
, ++cp
) {
524 data
= (data
<< 8) | (*(uchar
*)cp
);
527 return (write_word(info
, wp
, data
));
530 /*-----------------------------------------------------------------------
531 * Write a word to Flash, returns:
534 * 2 - Flash not erased
536 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
538 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
542 /* Check if Flash is (sufficiently) erased */
543 if ((*((vu_long
*)dest
) & data
) != data
) {
546 /* Disable interrupts which might cause a timeout here */
547 flag
= disable_interrupts();
549 addr
[0x0555] = 0x00AA00AA;
550 addr
[0x02AA] = 0x00550055;
551 addr
[0x0555] = 0x00A000A0;
553 *((vu_long
*)dest
) = data
;
555 /* re-enable interrupts if necessary */
559 /* data polling for D7 */
560 start
= get_timer (0);
561 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
562 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
569 /*-----------------------------------------------------------------------