]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/tqm8xx/flash.c
2 * (C) Copyright 2000-2002
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
);
39 /*-----------------------------------------------------------------------
42 unsigned long flash_init (void)
44 volatile immap_t
*immap
= (immap_t
*)CFG_IMMR
;
45 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
46 unsigned long size_b0
, size_b1
;
49 /* Init: no FLASHes known */
50 for (i
=0; i
<CFG_MAX_FLASH_BANKS
; ++i
) {
51 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
54 /* Static FLASH Bank configuration here - FIXME XXX */
56 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
58 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
59 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
60 size_b0
, size_b0
<<20);
63 size_b1
= flash_get_size((vu_long
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
65 if (size_b1
> size_b0
) {
67 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
71 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
72 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
73 flash_info
[0].sector_count
= -1;
74 flash_info
[1].sector_count
= -1;
75 flash_info
[0].size
= 0;
76 flash_info
[1].size
= 0;
80 /* Remap FLASH according to real size */
81 memctl
->memc_or0
= CFG_OR_TIMING_FLASH
| (-size_b0
& OR_AM_MSK
);
82 memctl
->memc_br0
= (CFG_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_V
;
84 /* Re-do sizing to get full correct info */
85 size_b0
= flash_get_size((vu_long
*)CFG_FLASH_BASE
, &flash_info
[0]);
87 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
88 /* monitor protection ON by default */
89 flash_protect(FLAG_PROTECT_SET
,
91 CFG_MONITOR_BASE
+monitor_flash_len
-1,
95 #ifdef CFG_ENV_IS_IN_FLASH
96 /* ENV protection ON by default */
97 flash_protect(FLAG_PROTECT_SET
,
99 CFG_ENV_ADDR
+CFG_ENV_SIZE
-1,
104 memctl
->memc_or1
= CFG_OR_TIMING_FLASH
| (-size_b1
& 0xFFFF8000);
105 memctl
->memc_br1
= ((CFG_FLASH_BASE
+ size_b0
) & BR_BA_MSK
) |
108 /* Re-do sizing to get full correct info */
109 size_b1
= flash_get_size((vu_long
*)(CFG_FLASH_BASE
+ size_b0
),
112 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
113 /* monitor protection ON by default */
114 flash_protect(FLAG_PROTECT_SET
,
116 CFG_MONITOR_BASE
+monitor_flash_len
-1,
120 #ifdef CFG_ENV_IS_IN_FLASH
121 /* ENV protection ON by default */
122 flash_protect(FLAG_PROTECT_SET
,
124 CFG_ENV_ADDR
+CFG_ENV_SIZE
-1,
128 memctl
->memc_br1
= 0; /* invalidate bank */
130 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
131 flash_info
[1].sector_count
= -1;
134 flash_info
[0].size
= size_b0
;
135 flash_info
[1].size
= size_b1
;
137 return (size_b0
+ size_b1
);
140 /*-----------------------------------------------------------------------
142 void flash_print_info (flash_info_t
*info
)
146 if (info
->flash_id
== FLASH_UNKNOWN
) {
147 printf ("missing or unknown FLASH type\n");
151 switch (info
->flash_id
& FLASH_VENDMASK
) {
152 case FLASH_MAN_AMD
: printf ("AMD "); break;
153 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
154 default: printf ("Unknown Vendor "); break;
157 switch (info
->flash_id
& FLASH_TYPEMASK
) {
158 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
160 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
162 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
164 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
166 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
168 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
170 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
172 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
174 default: printf ("Unknown Chip Type\n");
178 printf (" Size: %ld MB in %d Sectors\n",
179 info
->size
>> 20, info
->sector_count
);
181 printf (" Sector Start Addresses:");
182 for (i
=0; i
<info
->sector_count
; ++i
) {
187 info
->protect
[i
] ? " (RO)" : " "
194 /*-----------------------------------------------------------------------
198 /*-----------------------------------------------------------------------
202 * The following code cannot be run from FLASH!
205 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
209 ulong base
= (ulong
)addr
;
211 /* Write auto select command: read Manufacturer ID */
212 addr
[0x0555] = 0x00AA00AA;
213 addr
[0x02AA] = 0x00550055;
214 addr
[0x0555] = 0x00900090;
220 info
->flash_id
= FLASH_MAN_AMD
;
223 info
->flash_id
= FLASH_MAN_FUJ
;
226 info
->flash_id
= FLASH_UNKNOWN
;
227 info
->sector_count
= 0;
229 return (0); /* no or unknown flash */
232 value
= addr
[1]; /* device ID */
236 info
->flash_id
+= FLASH_AM400T
;
237 info
->sector_count
= 11;
238 info
->size
= 0x00100000;
242 info
->flash_id
+= FLASH_AM400B
;
243 info
->sector_count
= 11;
244 info
->size
= 0x00100000;
248 info
->flash_id
+= FLASH_AM800T
;
249 info
->sector_count
= 19;
250 info
->size
= 0x00200000;
254 info
->flash_id
+= FLASH_AM800B
;
255 info
->sector_count
= 19;
256 info
->size
= 0x00200000;
260 info
->flash_id
+= FLASH_AM160T
;
261 info
->sector_count
= 35;
262 info
->size
= 0x00400000;
266 info
->flash_id
+= FLASH_AM160B
;
267 info
->sector_count
= 35;
268 info
->size
= 0x00400000;
271 info
->flash_id
+= FLASH_AM320T
;
272 info
->sector_count
= 71;
273 info
->size
= 0x00800000;
277 info
->flash_id
+= FLASH_AM320B
;
278 info
->sector_count
= 71;
279 info
->size
= 0x00800000;
282 info
->flash_id
= FLASH_UNKNOWN
;
283 return (0); /* => no or unknown flash */
286 /* set up sector start address table */
291 /* set sector offsets for bottom boot block type */
292 info
->start
[0] = base
+ 0x00000000;
293 info
->start
[1] = base
+ 0x00008000;
294 info
->start
[2] = base
+ 0x0000C000;
295 info
->start
[3] = base
+ 0x00010000;
296 for (i
= 4; i
< info
->sector_count
; i
++) {
297 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
303 /* set sector offsets for top boot block type */
304 i
= info
->sector_count
- 1;
305 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
306 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
307 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
308 for (; i
>= 0; i
--) {
309 info
->start
[i
] = base
+ i
* 0x00020000;
313 for (i
= 0; i
< info
->sector_count
; i
++) {
314 info
->start
[i
] = base
;
316 * The first 8 sectors are 8 kB,
317 * all the other ones are 64 kB
325 for (i
= 0; i
< info
->sector_count
; i
++) {
326 info
->start
[i
] = base
;
328 * The last 8 sectors are 8 kB,
329 * all the other ones are 64 kB
331 base
+= (i
< (info
->sector_count
- 8))
341 /* check for protected sectors */
342 for (i
= 0; i
< info
->sector_count
; i
++) {
343 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
344 /* D0 = 1 if protected */
345 addr
= (volatile unsigned long *)(info
->start
[i
]);
346 info
->protect
[i
] = addr
[2] & 1;
350 * Prevent writes to uninitialized FLASH.
352 if (info
->flash_id
!= FLASH_UNKNOWN
) {
353 addr
= (volatile unsigned long *)info
->start
[0];
355 *addr
= 0x00F000F0; /* reset bank */
362 /*-----------------------------------------------------------------------
365 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
367 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
368 int flag
, prot
, sect
, l_sect
;
369 ulong start
, now
, last
;
371 if ((s_first
< 0) || (s_first
> s_last
)) {
372 if (info
->flash_id
== FLASH_UNKNOWN
) {
373 printf ("- missing\n");
375 printf ("- no sectors to erase\n");
380 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
381 (info
->flash_id
> FLASH_AMD_COMP
)) {
382 printf ("Can't erase unknown flash type %08lx - aborted\n",
388 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
389 if (info
->protect
[sect
]) {
395 printf ("- Warning: %d protected sectors will not be erased!\n",
403 /* Disable interrupts which might cause a timeout here */
404 flag
= disable_interrupts();
406 addr
[0x0555] = 0x00AA00AA;
407 addr
[0x02AA] = 0x00550055;
408 addr
[0x0555] = 0x00800080;
409 addr
[0x0555] = 0x00AA00AA;
410 addr
[0x02AA] = 0x00550055;
412 /* Start erase on unprotected sectors */
413 for (sect
= s_first
; sect
<=s_last
; sect
++) {
414 if (info
->protect
[sect
] == 0) { /* not protected */
415 addr
= (vu_long
*)(info
->start
[sect
]);
416 addr
[0] = 0x00300030;
421 /* re-enable interrupts if necessary */
425 /* wait at least 80us - let's wait 1 ms */
429 * We wait for the last triggered sector
434 start
= get_timer (0);
436 addr
= (vu_long
*)(info
->start
[l_sect
]);
437 while ((addr
[0] & 0x00800080) != 0x00800080) {
438 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
439 printf ("Timeout\n");
442 /* show that we're waiting */
443 if ((now
- last
) > 1000) { /* every second */
450 /* reset to read mode */
451 addr
= (volatile unsigned long *)info
->start
[0];
452 addr
[0] = 0x00F000F0; /* reset bank */
458 /*-----------------------------------------------------------------------
459 * Copy memory to flash, returns:
462 * 2 - Flash not erased
465 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
470 wp
= (addr
& ~3); /* get lower word aligned address */
473 * handle unaligned start bytes
475 if ((l
= addr
- wp
) != 0) {
477 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
478 data
= (data
<< 8) | (*(uchar
*)cp
);
480 for (; i
<4 && cnt
>0; ++i
) {
481 data
= (data
<< 8) | *src
++;
485 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
486 data
= (data
<< 8) | (*(uchar
*)cp
);
489 if ((rc
= write_word(info
, wp
, data
)) != 0) {
496 * handle word aligned part
500 for (i
=0; i
<4; ++i
) {
501 data
= (data
<< 8) | *src
++;
503 if ((rc
= write_word(info
, wp
, data
)) != 0) {
515 * handle unaligned tail bytes
518 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
519 data
= (data
<< 8) | *src
++;
522 for (; i
<4; ++i
, ++cp
) {
523 data
= (data
<< 8) | (*(uchar
*)cp
);
526 return (write_word(info
, wp
, data
));
529 /*-----------------------------------------------------------------------
530 * Write a word to Flash, returns:
533 * 2 - Flash not erased
535 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
537 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
541 /* Check if Flash is (sufficiently) erased */
542 if ((*((vu_long
*)dest
) & data
) != data
) {
545 /* Disable interrupts which might cause a timeout here */
546 flag
= disable_interrupts();
548 addr
[0x0555] = 0x00AA00AA;
549 addr
[0x02AA] = 0x00550055;
550 addr
[0x0555] = 0x00A000A0;
552 *((vu_long
*)dest
) = data
;
554 /* re-enable interrupts if necessary */
558 /* data polling for D7 */
559 start
= get_timer (0);
560 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
561 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
568 /*-----------------------------------------------------------------------