]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - board/ocotea/flash.c
2 * (C) Copyright 2004-2005
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
6 * Add support for Am29F016D and dynamic switch setting.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * Wait for completion of each sector erase command issued
31 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
36 #include <asm/processor.h>
41 #define DEBUGF(x...) printf(x)
46 #define BOOT_SMALL_FLASH 0x40 /* 01000000 */
47 #define FLASH_ONBD_N 2 /* 00000010 */
48 #define FLASH_SRAM_SEL 1 /* 00000001 */
49 #define FLASH_ONBD_N 2 /* 00000010 */
50 #define FLASH_SRAM_SEL 1 /* 00000001 */
52 #define BOOT_SMALL_FLASH_VAL 4
53 #define FLASH_ONBD_N_VAL 2
54 #define FLASH_SRAM_SEL_VAL 1
57 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
59 static unsigned long flash_addr_table
[8][CFG_MAX_FLASH_BANKS
] = {
60 {0xFF800000, 0xFF880000, 0xFFC00000}, /* 0:000: configuraton 4 */
61 {0xFF900000, 0xFF980000, 0xFFC00000}, /* 1:001: configuraton 3 */
62 {0x00000000, 0x00000000, 0x00000000}, /* 2:010: configuraton 8 */
63 {0x00000000, 0x00000000, 0x00000000}, /* 3:011: configuraton 7 */
64 {0xFFE00000, 0xFFF00000, 0xFF800000}, /* 4:100: configuraton 2 */
65 {0xFFF00000, 0xFFF80000, 0xFF800000}, /* 5:101: configuraton 1 */
66 {0x00000000, 0x00000000, 0x00000000}, /* 6:110: configuraton 6 */
67 {0x00000000, 0x00000000, 0x00000000} /* 7:111: configuraton 5 */
70 /*-----------------------------------------------------------------------
73 static ulong
flash_get_size(vu_long
* addr
, flash_info_t
* info
);
74 static int write_word(flash_info_t
* info
, ulong dest
, ulong data
);
80 #define FLASH_WORD_SIZE unsigned char
83 /*-----------------------------------------------------------------------
86 unsigned long flash_init(void)
88 unsigned long total_b
= 0;
89 unsigned long size_b
[CFG_MAX_FLASH_BANKS
];
90 unsigned char *fpga_base
= (unsigned char *) CFG_FPGA_BASE
;
91 unsigned char switch_status
;
92 unsigned short index
= 0;
95 /* read FPGA base register FPGA_REG0 */
96 switch_status
= *fpga_base
;
98 /* check the bitmap of switch status */
99 if (switch_status
& BOOT_SMALL_FLASH
) {
100 index
+= BOOT_SMALL_FLASH_VAL
;
102 if (switch_status
& FLASH_ONBD_N
) {
103 index
+= FLASH_ONBD_N_VAL
;
105 if (switch_status
& FLASH_SRAM_SEL
) {
106 index
+= FLASH_SRAM_SEL_VAL
;
110 DEBUGF("FLASH: Index: %d\n", index
);
112 /* Init: no FLASHes known */
113 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; ++i
) {
114 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
115 flash_info
[i
].sector_count
= -1;
116 flash_info
[i
].size
= 0;
118 /* check whether the address is 0 */
119 if (flash_addr_table
[index
][i
] == 0) {
123 /* call flash_get_size() to initialize sector address */
124 size_b
[i
] = flash_get_size((vu_long
*) flash_addr_table
[index
][i
], &flash_info
[i
]);
125 flash_info
[i
].size
= size_b
[i
];
126 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
) {
127 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
128 i
, size_b
[i
], size_b
[i
] << 20);
129 flash_info
[i
].sector_count
= -1;
130 flash_info
[i
].size
= 0;
133 total_b
+= flash_info
[i
].size
;
136 /* Monitor protection ON by default */
137 (void)flash_protect(FLAG_PROTECT_SET
,
145 /*-----------------------------------------------------------------------
147 void flash_print_info(flash_info_t
* info
)
153 volatile unsigned long *flash
;
155 if (info
->flash_id
== FLASH_UNKNOWN
) {
156 printf("missing or unknown FLASH type\n");
160 switch (info
->flash_id
& FLASH_VENDMASK
) {
174 printf("Unknown Vendor ");
178 switch (info
->flash_id
& FLASH_TYPEMASK
) {
180 printf("AM29F040 (512 Kbit, uniform sector size)\n");
183 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
186 printf("AM29LV400T (4 Mbit, top boot sector)\n");
189 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
192 printf("AM29LV800T (8 Mbit, top boot sector)\n");
195 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
198 printf("AM29LV160T (16 Mbit, top boot sector)\n");
201 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
204 printf("AM29LV320T (32 Mbit, top boot sector)\n");
206 case FLASH_AMDLV033C
:
207 printf("AM29LV033C (32 Mbit, top boot sector)\n");
210 printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
213 printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
216 printf("Unknown Chip Type\n");
220 printf(" Size: %ld KB in %d Sectors\n",
221 info
->size
>> 10, info
->sector_count
);
223 printf(" Sector Start Addresses:");
224 for (i
= 0; i
< info
->sector_count
; ++i
) {
226 * Check if whole sector is erased
228 if (i
!= (info
->sector_count
- 1))
229 size
= info
->start
[i
+ 1] - info
->start
[i
];
231 size
= info
->start
[0] + info
->size
- info
->start
[i
];
233 flash
= (volatile unsigned long *) info
->start
[i
];
234 size
= size
>> 2; /* divide by 4 for longword access */
235 for (k
= 0; k
< size
; k
++) {
236 if (*flash
++ != 0xffffffff) {
246 erased
? " E" : " ", info
->protect
[i
] ? "RO " : " ");
252 /*-----------------------------------------------------------------------
256 * The following code cannot be run from FLASH!
258 static ulong
flash_get_size(vu_long
* addr
, flash_info_t
* info
)
261 FLASH_WORD_SIZE value
;
262 ulong base
= (ulong
) addr
;
263 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*) addr
;
265 DEBUGF("FLASH ADDR: %08x\n", (unsigned) addr
);
267 /* Write auto select command: read Manufacturer ID */
269 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
271 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
273 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00900090;
277 DEBUGF("FLASH MANUFACT: %x\n", value
);
280 case (FLASH_WORD_SIZE
) AMD_MANUFACT
:
281 info
->flash_id
= FLASH_MAN_AMD
;
283 case (FLASH_WORD_SIZE
) FUJ_MANUFACT
:
284 info
->flash_id
= FLASH_MAN_FUJ
;
286 case (FLASH_WORD_SIZE
) SST_MANUFACT
:
287 info
->flash_id
= FLASH_MAN_SST
;
289 case (FLASH_WORD_SIZE
) STM_MANUFACT
:
290 info
->flash_id
= FLASH_MAN_STM
;
293 info
->flash_id
= FLASH_UNKNOWN
;
294 info
->sector_count
= 0;
296 return (0); /* no or unknown flash */
299 value
= addr2
[1]; /* device ID */
301 DEBUGF("\nFLASH DEVICEID: %x\n", value
);
304 case (FLASH_WORD_SIZE
) AMD_ID_LV040B
:
305 info
->flash_id
+= FLASH_AM040
;
306 info
->sector_count
= 8;
307 info
->size
= 0x0080000; /* => 512 ko */
309 case (FLASH_WORD_SIZE
) AMD_ID_F040B
:
310 info
->flash_id
+= FLASH_AM040
;
311 info
->sector_count
= 8;
312 info
->size
= 0x0080000; /* => 512 ko */
314 case (FLASH_WORD_SIZE
) STM_ID_M29W040B
:
315 info
->flash_id
+= FLASH_AM040
;
316 info
->sector_count
= 8;
317 info
->size
= 0x0080000; /* => 512 ko */
319 case (FLASH_WORD_SIZE
) AMD_ID_LV033C
:
320 info
->flash_id
+= FLASH_AMDLV033C
;
321 info
->sector_count
= 64;
322 info
->size
= 0x00400000;
325 info
->flash_id
= FLASH_UNKNOWN
;
326 return (0); /* => no or unknown flash */
329 /* set up sector start address table */
330 if (((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) ||
331 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) ||
332 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AMD016
)) {
333 for (i
= 0; i
< info
->sector_count
; i
++)
334 info
->start
[i
] = base
+ (i
* 0x00010000);
336 if (info
->flash_id
& FLASH_BTYPE
) {
337 /* set sector offsets for bottom boot block type */
338 info
->start
[0] = base
+ 0x00000000;
339 info
->start
[1] = base
+ 0x00004000;
340 info
->start
[2] = base
+ 0x00006000;
341 info
->start
[3] = base
+ 0x00008000;
342 for (i
= 4; i
< info
->sector_count
; i
++) {
343 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
346 /* set sector offsets for top boot block type */
347 i
= info
->sector_count
- 1;
348 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
349 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
350 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
351 for (; i
>= 0; i
--) {
352 info
->start
[i
] = base
+ i
* 0x00010000;
357 /* check for protected sectors */
358 for (i
= 0; i
< info
->sector_count
; i
++) {
359 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
360 /* D0 = 1 if protected */
361 addr2
= (volatile FLASH_WORD_SIZE
*) (info
->start
[i
]);
363 /* For AMD29033C flash we need to resend the command of *
364 * reading flash protection for upper 8 Mb of flash */
366 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0xAAAAAAAA;
367 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x55555555;
368 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x90909090;
371 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
)
372 info
->protect
[i
] = 0;
374 info
->protect
[i
] = addr2
[2] & 1;
377 /* issue bank reset to return to read mode */
378 addr2
[0] = (FLASH_WORD_SIZE
) 0x00F000F0;
381 * Prevent writes to uninitialized FLASH.
383 if (info
->flash_id
!= FLASH_UNKNOWN
) {
390 int wait_for_DQ7(flash_info_t
* info
, int sect
)
392 ulong start
, now
, last
;
393 volatile FLASH_WORD_SIZE
*addr
=
394 (FLASH_WORD_SIZE
*) (info
->start
[sect
]);
396 start
= get_timer(0);
398 while ((addr
[0] & (FLASH_WORD_SIZE
) 0x00800080) !=
399 (FLASH_WORD_SIZE
) 0x00800080) {
400 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
404 /* show that we're waiting */
405 if ((now
- last
) > 1000) { /* every second */
413 /*-----------------------------------------------------------------------
416 int flash_erase(flash_info_t
* info
, int s_first
, int s_last
)
418 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*) (info
->start
[0]);
419 volatile FLASH_WORD_SIZE
*addr2
;
420 int flag
, prot
, sect
, l_sect
;
423 if ((s_first
< 0) || (s_first
> s_last
)) {
424 if (info
->flash_id
== FLASH_UNKNOWN
) {
425 printf("- missing\n");
427 printf("- no sectors to erase\n");
432 if (info
->flash_id
== FLASH_UNKNOWN
) {
433 printf("Can't erase unknown flash type - aborted\n");
438 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
439 if (info
->protect
[sect
]) {
445 printf("- Warning: %d protected sectors will not be erased!\n",
453 /* Disable interrupts which might cause a timeout here */
454 flag
= disable_interrupts();
456 /* Start erase on unprotected sectors */
457 for (sect
= s_first
; sect
<= s_last
; sect
++) {
458 if (info
->protect
[sect
] == 0) { /* not protected */
459 addr2
= (FLASH_WORD_SIZE
*) (info
->start
[sect
]);
461 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) {
462 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
463 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
464 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00800080;
465 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
466 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
467 addr2
[0] = (FLASH_WORD_SIZE
) 0x00500050; /* block erase */
468 for (i
= 0; i
< 50; i
++)
469 udelay(1000); /* wait 1 ms */
471 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
472 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
473 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00800080;
474 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
475 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
476 addr2
[0] = (FLASH_WORD_SIZE
) 0x00300030; /* sector erase */
480 * Wait for each sector to complete, it's more
481 * reliable. According to AMD Spec, you must
482 * issue all erase commands within a specified
483 * timeout. This has been seen to fail, especially
484 * if printf()s are included (for debug)!!
486 wait_for_DQ7(info
, sect
);
490 /* re-enable interrupts if necessary */
494 /* wait at least 80us - let's wait 1 ms */
497 /* reset to read mode */
498 addr
= (FLASH_WORD_SIZE
*) info
->start
[0];
499 addr
[0] = (FLASH_WORD_SIZE
) 0x00F000F0; /* reset bank */
505 /*-----------------------------------------------------------------------
506 * Copy memory to flash, returns:
509 * 2 - Flash not erased
511 int write_buff(flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
516 wp
= (addr
& ~3); /* get lower word aligned address */
519 * handle unaligned start bytes
521 if ((l
= addr
- wp
) != 0) {
523 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
524 data
= (data
<< 8) | (*(uchar
*) cp
);
526 for (; i
< 4 && cnt
> 0; ++i
) {
527 data
= (data
<< 8) | *src
++;
531 for (; cnt
== 0 && i
< 4; ++i
, ++cp
) {
532 data
= (data
<< 8) | (*(uchar
*) cp
);
535 if ((rc
= write_word(info
, wp
, data
)) != 0) {
542 * handle word aligned part
546 for (i
= 0; i
< 4; ++i
) {
547 data
= (data
<< 8) | *src
++;
549 if ((rc
= write_word(info
, wp
, data
)) != 0) {
561 * handle unaligned tail bytes
564 for (i
= 0, cp
= wp
; i
< 4 && cnt
> 0; ++i
, ++cp
) {
565 data
= (data
<< 8) | *src
++;
568 for (; i
< 4; ++i
, ++cp
) {
569 data
= (data
<< 8) | (*(uchar
*) cp
);
572 return (write_word(info
, wp
, data
));
575 /*-----------------------------------------------------------------------
576 * Write a word to Flash, returns:
579 * 2 - Flash not erased
581 static int write_word(flash_info_t
* info
, ulong dest
, ulong data
)
583 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*) (info
->start
[0]);
584 volatile FLASH_WORD_SIZE
*dest2
= (FLASH_WORD_SIZE
*) dest
;
585 volatile FLASH_WORD_SIZE
*data2
= (FLASH_WORD_SIZE
*) & data
;
589 /* Check if Flash is (sufficiently) erased */
590 if ((*((volatile FLASH_WORD_SIZE
*) dest
) &
591 (FLASH_WORD_SIZE
) data
) != (FLASH_WORD_SIZE
) data
) {
595 for (i
= 0; i
< 4 / sizeof(FLASH_WORD_SIZE
); i
++) {
598 /* Disable interrupts which might cause a timeout here */
599 flag
= disable_interrupts();
601 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
602 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
603 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00A000A0;
607 /* re-enable interrupts if necessary */
611 /* data polling for D7 */
612 start
= get_timer(0);
613 while ((dest2
[i
] & (FLASH_WORD_SIZE
) 0x00800080) !=
614 (data2
[i
] & (FLASH_WORD_SIZE
) 0x00800080)) {
616 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {