3 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
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,
25 * flash.c - flash support for the 512k, 8bit boot flash
26 and the 8MB 32bit extra flash on the DB64360
27 * most of this file was based on the existing U-Boot
30 * written or collected and sometimes rewritten by
31 * Ingo Assmus <ingo.assmus@keymile.com>
37 #include "../include/mv_gen_reg.h"
38 #include "../include/memory.h"
39 #include "intel_flash.h"
41 #define FLASH_ROM 0xFFFD /* unknown flash type */
42 #define FLASH_RAM 0xFFFE /* unknown flash type */
43 #define FLASH_MAN_UNKNOWN 0xFFFF0000
47 /* Intel flash commands */
48 int flash_erase_intel (flash_info_t
* info
, int s_first
, int s_last
);
49 int write_word_intel (bank_addr_t addr
, bank_word_t value
);
51 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
53 /*-----------------------------------------------------------------------
56 static ulong
flash_get_size (int portwidth
, vu_long
* addr
,
58 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
);
59 static void flash_get_offsets (ulong base
, flash_info_t
* info
);
61 /*-----------------------------------------------------------------------
64 unsigned long flash_init (void)
67 unsigned long size_b0
= 0, size_b1
= 0;
68 unsigned long base
, flash_size
;
70 /* Init: no FLASHes known */
71 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
72 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
76 base
= CONFIG_SYS_FLASH_BASE
;
78 flash_get_size (CONFIG_SYS_BOOT_FLASH_WIDTH
, (vu_long
*) base
,
81 printf ("[%ldkB@%lx] ", size_b0
/ 1024, base
);
83 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
84 printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n", base
, size_b0
, size_b0
<< 20);
87 base
= memoryGetDeviceBaseAddress (CONFIG_SYS_EXTRA_FLASH_DEVICE
);
88 /* base = memoryGetDeviceBaseAddress(DEV_CS3_BASE_ADDR);*/
89 for (i
= 1; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
91 flash_get_size (CONFIG_SYS_EXTRA_FLASH_WIDTH
,
92 (vu_long
*) base
, &flash_info
[i
]);
94 printf ("[%ldMB@%lx] ", size
>> 20, base
);
96 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
) {
98 printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n", base
, size_b1
, size_b1
<< 20);
106 flash_size
= size_b0
+ size_b1
;
110 /*-----------------------------------------------------------------------
112 static void flash_get_offsets (ulong base
, flash_info_t
* info
)
117 if (!info
->sector_count
)
120 /* set up sector start address table */
121 switch (info
->flash_id
& FLASH_TYPEMASK
) {
123 case FLASH_28F128J3A
:
124 case FLASH_28F640J3A
:
126 /* this chip has uniformly spaced sectors */
127 sector_size
= info
->size
/ info
->sector_count
;
128 for (i
= 0; i
< info
->sector_count
; i
++)
129 info
->start
[i
] = base
+ (i
* sector_size
);
132 if (info
->flash_id
& FLASH_BTYPE
) {
133 /* set sector offsets for bottom boot block type */
134 info
->start
[0] = base
+ 0x00000000;
135 info
->start
[1] = base
+ 0x00008000;
136 info
->start
[2] = base
+ 0x0000C000;
137 info
->start
[3] = base
+ 0x00010000;
138 for (i
= 4; i
< info
->sector_count
; i
++) {
140 base
+ (i
* 0x00020000) - 0x00060000;
143 /* set sector offsets for top boot block type */
144 i
= info
->sector_count
- 1;
145 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
146 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
147 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
148 for (; i
>= 0; i
--) {
149 info
->start
[i
] = base
+ i
* 0x00020000;
155 /*-----------------------------------------------------------------------
157 void flash_print_info (flash_info_t
* info
)
161 if (info
->flash_id
== FLASH_UNKNOWN
) {
162 printf ("missing or unknown FLASH type\n");
166 switch (info
->flash_id
& FLASH_VENDMASK
) {
176 case FLASH_MAN_INTEL
:
180 printf ("Unknown Vendor ");
184 switch (info
->flash_id
& FLASH_TYPEMASK
) {
186 printf ("AM29LV040B (4 Mbit, bottom boot sect)\n");
189 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
192 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
195 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
198 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
201 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
204 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
207 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
210 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
212 case FLASH_28F640J3A
:
213 printf ("28F640J3A (64 Mbit)\n");
215 case FLASH_28F128J3A
:
216 printf ("28F128J3A (128 Mbit)\n");
225 printf ("Unknown Chip Type\n");
229 if ((info
->size
>> 20) > 0) {
230 printf (" Size: %ld MB in %d Sectors\n",
231 info
->size
>> 20, info
->sector_count
);
233 printf (" Size: %ld kB in %d Sectors\n",
234 info
->size
>> 10, info
->sector_count
);
237 printf (" Sector Start Addresses:");
238 for (i
= 0; i
< info
->sector_count
; ++i
) {
242 info
->start
[i
], info
->protect
[i
] ? " (RO)" : " ");
248 /*-----------------------------------------------------------------------
252 /*-----------------------------------------------------------------------
256 * The following code cannot be run from FLASH!
259 static inline void flash_cmd (int width
, volatile unsigned char *addr
,
260 int offset
, unsigned char cmd
)
262 /* supports 1x8, 1x16, and 2x16 */
263 /* 2x8 and 4x8 are not supported */
265 /* assuming chips are in 16 bit mode */
267 unsigned long cmd32
= (cmd
<< 16) | cmd
;
269 *(volatile unsigned long *) (addr
+ offset
* 2) = cmd32
;
272 *(volatile unsigned char *) (addr
+ offset
) = cmd
;
277 flash_get_size (int portwidth
, vu_long
* addr
, flash_info_t
* info
)
280 volatile unsigned char *caddr
= (unsigned char *) addr
;
281 volatile unsigned short *saddr
= (unsigned short *) addr
;
282 volatile unsigned long *laddr
= (unsigned long *) addr
;
284 ulong id
= 0, manu
= 0, base
= (ulong
) addr
;
287 printf ("%s: enter\n", __FUNCTION__
);
289 info
->portwidth
= portwidth
;
293 flash_cmd (portwidth
, caddr
, 0, 0xf0);
294 flash_cmd (portwidth
, caddr
, 0, 0xf0);
302 if (old
[0] != 0xf0) {
303 flash_cmd (portwidth
, caddr
, 0, 0xf0);
304 flash_cmd (portwidth
, caddr
, 0, 0xf0);
308 if (*caddr
== 0xf0) {
309 /* this area is ROM */
311 info
->flash_id
= FLASH_ROM
+ FLASH_MAN_UNKNOWN
;
312 info
->sector_count
= 8;
313 info
->size
= 0x80000;
314 flash_get_offsets (base
, info
);
323 /* this area is RAM */
325 info
->flash_id
= FLASH_RAM
+ FLASH_MAN_UNKNOWN
;
326 info
->sector_count
= 8;
327 info
->size
= 0x80000;
328 flash_get_offsets (base
, info
);
331 flash_cmd (portwidth
, caddr
, 0, 0xf0);
336 /* Write auto select command: read Manufacturer ID */
337 flash_cmd (portwidth
, caddr
, 0x555, 0xAA);
338 flash_cmd (portwidth
, caddr
, 0x2AA, 0x55);
339 flash_cmd (portwidth
, caddr
, 0x555, 0x90);
343 if ((caddr
[0] == old
[0]) && (caddr
[1] == old
[1])) {
345 /* this area is ROM */
346 info
->flash_id
= FLASH_ROM
+ FLASH_MAN_UNKNOWN
;
347 info
->sector_count
= 8;
348 info
->size
= 0x80000;
349 flash_get_offsets (base
, info
);
353 printf ("%px%d: %02x:%02x -> %02x:%02x\n",
354 caddr
, portwidth
, old
[0], old
[1], caddr
[0], caddr
[1]);
377 flash_cmd (portwidth
, caddr
, 0, 0xf0);
379 printf ("\n%08lx:%08lx:%08lx\n", base
, manu
, id
);
380 printf ("%08lx %08lx %08lx %08lx\n",
381 laddr
[0], laddr
[1], laddr
[2], laddr
[3]);
386 info
->flash_id
= FLASH_MAN_STM
;
389 info
->flash_id
= FLASH_MAN_AMD
;
392 info
->flash_id
= FLASH_MAN_FUJ
;
395 info
->flash_id
= FLASH_MAN_INTEL
;
398 flash_cmd (portwidth
, caddr
, 0, 0xf0);
400 printf ("Unknown Mfr [%08lx]:%08lx\n", manu
, id
);
401 info
->flash_id
= FLASH_UNKNOWN
;
402 info
->sector_count
= 0;
404 return (0); /* no or unknown flash */
409 info
->flash_id
+= FLASH_AM400T
;
410 info
->sector_count
= 11;
411 info
->size
= 0x00100000;
416 info
->flash_id
+= FLASH_AM400B
;
417 info
->sector_count
= 11;
418 info
->size
= 0x00100000;
423 info
->flash_id
+= FLASH_AM800T
;
424 info
->sector_count
= 19;
425 info
->size
= 0x00200000;
430 info
->flash_id
+= FLASH_AM800B
;
431 info
->sector_count
= 19;
432 info
->size
= 0x00200000;
437 info
->flash_id
+= FLASH_AM160T
;
438 info
->sector_count
= 35;
439 info
->size
= 0x00400000;
444 info
->flash_id
+= FLASH_AM160B
;
445 info
->sector_count
= 35;
446 info
->size
= 0x00400000;
449 #if 0 /* enable when device IDs are available */
451 info
->flash_id
+= FLASH_AM320T
;
452 info
->sector_count
= 67;
453 info
->size
= 0x00800000;
457 info
->flash_id
+= FLASH_AM320B
;
458 info
->sector_count
= 67;
459 info
->size
= 0x00800000;
463 info
->flash_id
+= FLASH_AM040
;
464 info
->sector_count
= 8;
465 info
->size
= 0x80000;
467 break; /* => 512 kB */
469 case INTEL_ID_28F640J3A
:
470 info
->flash_id
+= FLASH_28F640J3A
;
471 info
->sector_count
= 64;
472 info
->size
= 128 * 1024 * 64; /* 128kbytes x 64 blocks */
475 info
->size
*= 2; /* 2x16 */
478 case INTEL_ID_28F128J3A
:
479 info
->flash_id
+= FLASH_28F128J3A
;
480 info
->sector_count
= 128;
481 info
->size
= 128 * 1024 * 128; /* 128kbytes x 128 blocks */
484 info
->size
*= 2; /* 2x16 */
488 flash_cmd (portwidth
, caddr
, 0, 0xf0);
490 printf ("Unknown id %lx:[%lx]\n", manu
, id
);
491 info
->flash_id
= FLASH_UNKNOWN
;
493 return (0); /* => no or unknown flash */
497 flash_get_offsets (base
, info
);
500 /* check for protected sectors */
501 for (i
= 0; i
< info
->sector_count
; i
++) {
502 /* read sector protection at sector address, (A7 .. A0)=0x02 */
503 /* D0 = 1 if protected */
504 caddr
= (volatile unsigned char *) (info
->start
[i
]);
505 saddr
= (volatile unsigned short *) (info
->start
[i
]);
506 laddr
= (volatile unsigned long *) (info
->start
[i
]);
508 info
->protect
[i
] = caddr
[2] & 1;
509 else if (portwidth
== 2)
510 info
->protect
[i
] = saddr
[2] & 1;
512 info
->protect
[i
] = laddr
[2] & 1;
516 * Prevent writes to uninitialized FLASH.
518 if (info
->flash_id
!= FLASH_UNKNOWN
) {
519 caddr
= (volatile unsigned char *) info
->start
[0];
521 flash_cmd (portwidth
, caddr
, 0, 0xF0); /* reset bank */
527 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
529 volatile unsigned char *addr
= (uchar
*) (info
->start
[0]);
530 int flag
, prot
, sect
, l_sect
;
531 ulong start
, now
, last
;
533 /* modified to support 2x16 Intel flash */
534 /* Note that the code will not exit on a flash erasure error or timeout */
535 /* but will print and error message and continue processing sectors */
536 /* until they are all erased. */
537 /* 10-16-2002 P. Marchese */
541 if (info
->portwidth
== 4)
543 printf ("- Warning: erasing of 32Bit (2*16Bit i.e. 2*28F640J3A) not supported yet !!!! \n");
547 /* make sure it's Intel flash */
548 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
549 /* yup! it's an Intel flash */
550 /* is it 16-bits wide? */
551 if (info
->chipwidth
== 2) {
552 /* yup! it's 16-bits wide */
553 /* are there any sectors to process? */
554 if ((s_first
< 0) || (s_first
> s_last
)) {
555 printf ("Error: There are no sectors to erase\n");
556 printf ("Either sector %d is less than zero\n", s_first
);
557 printf ("or sector %d is greater than sector %d\n", s_first
, s_last
);
560 /* check for protected sectors */
562 for (sect
= s_first
; sect
<= s_last
; ++sect
)
563 if (info
->protect
[sect
])
565 /* if variable "prot" is nonzero, there are protected sectors */
567 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
568 /* reset the flash */
569 flash_cmd (info
->portwidth
, addr
, 0,
571 /* Disable interrupts which might cause a timeout here */
572 flag
= disable_interrupts ();
573 /* Clear the status register */
574 flash_cmd (info
->portwidth
, addr
, 0,
576 flash_cmd (info
->portwidth
, addr
, 0,
578 /* Start erase on unprotected sectors */
579 for (sect
= s_first
; sect
<= s_last
; sect
++) {
580 /* is the sector unprotected? */
581 if (info
->protect
[sect
] == 0) { /* not protected */
582 /* issue the single block erase command, 0x20 */
583 flash_cmd (info
->portwidth
,
588 /* issue the erase confirm command, 0xD0 */
589 flash_cmd (info
->portwidth
,
595 /* re-enable interrupts if necessary */
597 enable_interrupts ();
598 /* poll for erasure completion */
599 /* put flash into read status mode by writing 0x70 to it */
600 flash_cmd (info
->portwidth
,
603 /* setup the status register mask */
604 mask
= CHIP_STAT_RDY
|
605 (CHIP_STAT_RDY
<< 16);
606 /* init. the timeout counter */
607 start
= get_timer (0);
608 /* keep looping while the flash is not ready */
609 /* exit the loop by timing out or the flash */
610 /* becomes ready again */
615 start
[sect
] & mask
) !=
617 /* has the timeout limit been reached? */
618 if (get_timer (start
)
620 CONFIG_SYS_FLASH_ERASE_TOUT
)
622 /* timeout limit reached */
623 printf ("Time out limit reached erasing sector at address %08lx\n", info
->start
[sect
]);
624 printf ("Continuing with next sector\n");
626 goto timed_out_error
;
628 /* put flash into read status mode by writing 0x70 to it */
634 /* did we timeout? */
635 timed_out_error
:if (timeout
== 0)
637 /* didn't timeout, so check the status register */
638 /* create the status mask to check for errors */
639 mask
= CHIP_STAT_ECLBS
;
640 mask
= mask
| (mask
<<
642 /* put flash into read status mode by writing 0x70 to it */
647 /* are there any errors? */
654 /* We got an erasure error */
655 printf ("Flash erasure error at address 0x%08lx\n", info
->start
[sect
]);
656 printf ("Continuing with next sector\n");
657 /* reset the flash */
666 /* erasure completed without errors */
667 /* reset the flash */
668 flash_cmd (info
->portwidth
,
671 } /* end if not protected */
673 printf ("Flash erasure done\n");
676 /* The Intel flash is not 16-bit wide */
677 /* print and error message and return */
678 /* NOTE: you can add routines here to handle other size flash */
679 printf ("Error: Intel flash device is only %d-bits wide\n", info
->chipwidth
* 8);
680 printf ("The erasure code only handles Intel 16-bit wide flash memory\n");
684 /* Not Intel flash so return an error as a write timeout */
685 /* NOTE: if it's another type flash, stick its routine here */
686 printf ("Error: The flash device is not Intel type\n");
687 printf ("The erasure code only supports Intel flash in a 32-bit port width\n");
692 /* end 32-bit wide flash code */
693 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_ROM
)
694 return 1; /* Rom can not be erased */
695 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_RAM
) { /* RAM just copy 0s to RAM */
696 for (sect
= s_first
; sect
<= s_last
; sect
++) {
697 int sector_size
= info
->size
/ info
->sector_count
;
699 addr
= (uchar
*) (info
->start
[sect
]);
700 memset ((void *) addr
, 0, sector_size
);
705 if ((s_first
< 0) || (s_first
> s_last
)) {
706 if (info
->flash_id
== FLASH_UNKNOWN
) {
707 printf ("- missing\n");
709 printf ("- no sectors to erase\n");
714 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) { /* Intel works spezial */
715 return flash_erase_intel (info
,
716 (unsigned short) s_first
,
717 (unsigned short) s_last
);
720 if ((info
->flash_id
== FLASH_UNKNOWN
) || /* Flash is unknown to PPCBoot */
721 (info
->flash_id
> FLASH_AMD_COMP
)) {
722 printf ("Can't erase unknown flash type %08lx - aborted\n",
729 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
730 if (info
->protect
[sect
]) {
736 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
743 /* Disable interrupts which might cause a timeout here */
744 flag
= disable_interrupts ();
746 flash_cmd (info
->portwidth
, addr
, 0x555, 0xAA); /* start erase routine */
747 flash_cmd (info
->portwidth
, addr
, 0x2AA, 0x55);
748 flash_cmd (info
->portwidth
, addr
, 0x555, 0x80);
749 flash_cmd (info
->portwidth
, addr
, 0x555, 0xAA);
750 flash_cmd (info
->portwidth
, addr
, 0x2AA, 0x55);
752 /* Start erase on unprotected sectors */
753 for (sect
= s_first
; sect
<= s_last
; sect
++) {
754 if (info
->protect
[sect
] == 0) { /* not protected */
755 addr
= (uchar
*) (info
->start
[sect
]);
756 flash_cmd (info
->portwidth
, addr
, 0, 0x30);
761 /* re-enable interrupts if necessary */
763 enable_interrupts ();
765 /* wait at least 80us - let's wait 1 ms */
769 * We wait for the last triggered sector
774 start
= get_timer (0);
776 addr
= (volatile unsigned char *) (info
->start
[l_sect
]);
777 /* broken for 2x16: TODO */
778 while ((addr
[0] & 0x80) != 0x80) {
779 if ((now
= get_timer (start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
780 printf ("Timeout\n");
783 /* show that we're waiting */
784 if ((now
- last
) > 1000) { /* every second */
791 /* reset to read mode */
792 addr
= (volatile unsigned char *) info
->start
[0];
793 flash_cmd (info
->portwidth
, addr
, 0, 0xf0);
794 flash_cmd (info
->portwidth
, addr
, 0, 0xf0);
800 /*-----------------------------------------------------------------------
801 * Copy memory to flash, returns:
804 * 2 - Flash not erased
807 /* broken for 2x16: TODO */
808 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
813 /* Commented out since the below code should work for 32-bit(2x 16 flash) */
814 /* 10-16-2002 P. Marchese */
815 /* if(info->portwidth==4) return 1; */
816 /* if(info->portwidth==4) {
817 printf ("- Warning: writting of 32Bit (2*16Bit i.e. 2*28F640J3A) not supported yet !!!! \n");
821 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_ROM
)
823 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_RAM
) {
824 memcpy ((void *) addr
, src
, cnt
);
828 wp
= (addr
& ~3); /* get lower word aligned address */
831 * handle unaligned start bytes
833 if ((l
= addr
- wp
) != 0) {
835 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
836 data
= (data
<< 8) | (*(uchar
*) cp
);
838 for (; i
< 4 && cnt
> 0; ++i
) {
839 data
= (data
<< 8) | *src
++;
843 for (; cnt
== 0 && i
< 4; ++i
, ++cp
) {
844 data
= (data
<< 8) | (*(uchar
*) cp
);
847 if ((rc
= write_word (info
, wp
, data
)) != 0) {
854 * handle word aligned part
858 for (i
= 0; i
< 4; ++i
) {
859 data
= (data
<< 8) | *src
++;
861 if ((rc
= write_word (info
, wp
, data
)) != 0) {
873 * handle unaligned tail bytes
876 for (i
= 0, cp
= wp
; i
< 4 && cnt
> 0; ++i
, ++cp
) {
877 data
= (data
<< 8) | *src
++;
880 for (; i
< 4; ++i
, ++cp
) {
881 data
= (data
<< 8) | (*(uchar
*) cp
);
884 return (write_word (info
, wp
, data
));
887 /*-----------------------------------------------------------------------
888 * Write a word to Flash, returns:
891 * 2 - Flash not erased
893 /* broken for 2x16: TODO */
894 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
896 volatile unsigned char *addr
= (uchar
*) (info
->start
[0]);
901 /* modified so that it handles 32-bit(2x16 Intel flash programming */
902 /* 10-16-2002 P. Marchese */
904 if (info
->portwidth
== 4)
906 printf ("- Warning: writting of 32Bit (2*16Bit i.e. 2*28F640J3A) not supported yet !!!! \n");
910 /* make sure it's Intel flash */
911 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
912 /* yup! it's an Intel flash */
913 /* is it 16-bits wide? */
914 if (info
->chipwidth
== 2) {
915 /* yup! it's 16-bits wide */
916 /* so we know how to program it */
917 /* reset the flash */
918 flash_cmd (info
->portwidth
, addr
, 0,
920 /* Disable interrupts which might cause a timeout here */
921 flag
= disable_interrupts ();
922 /* Clear the status register */
923 flash_cmd (info
->portwidth
, addr
, 0,
925 flash_cmd (info
->portwidth
, addr
, 0,
927 /* 1st cycle of word/byte program */
928 /* write 0x40 to the location to program */
929 flash_cmd (info
->portwidth
, (uchar
*) dest
, 0,
931 /* 2nd cycle of word/byte program */
932 /* write the data to the destination address */
933 *(ulong
*) dest
= data
;
934 /* re-enable interrupts if necessary */
936 enable_interrupts ();
937 /* setup the status register mask */
938 mask
= CHIP_STAT_RDY
| (CHIP_STAT_RDY
<< 16);
939 /* put flash into read status mode by writing 0x70 to it */
940 flash_cmd (info
->portwidth
, addr
, 0,
942 /* init. the timeout counter */
943 start
= get_timer (0);
944 /* keep looping while the flash is not ready */
945 /* exit the loop by timing out or the flash */
946 /* becomes ready again */
947 /* 11-13-2002 Paul Marchese */
948 /* modified while loop conditional statement */
949 /* because we were always timing out. */
950 /* there is a type mismatch, "addr[0]" */
951 /* returns a byte but "mask" is a 32-bit value */
952 while ((*(volatile unsigned long *) info
->
953 start
[0] & mask
) != mask
)
955 /* while (addr[0] & mask) != mask) */
957 /* has the timeout limit been reached? */
958 if (get_timer (start
) >
959 CONFIG_SYS_FLASH_WRITE_TOUT
) {
960 /* timeout limit reached */
961 printf ("Time out limit reached programming address %08lx with data %08lx\n", dest
, data
);
962 /* reset the flash */
963 flash_cmd (info
->portwidth
,
968 /* put flash into read status mode by writing 0x70 to it */
969 flash_cmd (info
->portwidth
, addr
, 0,
972 /* flash is ready, so check the status */
973 /* create the status mask to check for errors */
974 mask
= CHIP_STAT_DPS
| CHIP_STAT_VPPS
|
976 mask
= mask
| (mask
<< 16);
977 /* put flash into read status mode by writing 0x70 to it */
978 flash_cmd (info
->portwidth
, addr
, 0,
980 /* are there any errors? */
981 if ((addr
[0] & mask
) != 0) {
982 /* We got a one of the following errors: */
983 /* Voltage range, Device protect, or programming */
984 /* return the error as a device timeout */
985 /* put flash into read status mode by writing 0x70 to it */
986 flash_cmd (info
->portwidth
, addr
, 0,
988 printf ("Flash programming error at address 0x%08lx\n", dest
);
989 printf ("Flash status register contains 0x%08lx\n", (unsigned long) addr
[0]);
990 /* reset the flash */
991 flash_cmd (info
->portwidth
, addr
, 0,
995 /* write completed without errors */
996 /* reset the flash */
997 flash_cmd (info
->portwidth
, addr
, 0,
1001 /* it's not 16-bits wide, so return an error as a write timeout */
1002 /* NOTE: you can add routines here to handle other size flash */
1003 printf ("Error: Intel flash device is only %d-bits wide\n", info
->chipwidth
* 8);
1004 printf ("The write code only handles Intel 16-bit wide flash memory\n");
1008 /* not Intel flash so return an error as a write timeout */
1009 /* NOTE: if it's another type flash, stick its routine here */
1010 printf ("Error: The flash device is not Intel type\n");
1011 printf ("The code only supports Intel flash in a 32-bit port width\n");
1016 /* end of 32-bit flash code */
1017 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_ROM
)
1019 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_RAM
) {
1020 *(unsigned long *) dest
= data
;
1023 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
1024 unsigned short low
= data
& 0xffff;
1025 unsigned short hi
= (data
>> 16) & 0xffff;
1026 int ret
= write_word_intel ((bank_addr_t
) dest
, hi
);
1029 ret
= write_word_intel ((bank_addr_t
) (dest
+ 2),
1035 /* Check if Flash is (sufficiently) erased */
1036 if ((*((vu_long
*) dest
) & data
) != data
) {
1039 /* Disable interrupts which might cause a timeout here */
1040 flag
= disable_interrupts ();
1042 /* first, perform an unlock bypass command to speed up flash writes */
1047 /* write each byte out */
1048 for (i
= 0; i
< 4; i
++) {
1049 char *data_ch
= (char *) &data
;
1052 *(((char *) dest
) + i
) = data_ch
[i
];
1053 udelay (10); /* XXX */
1056 /* we're done, now do an unlock bypass reset */
1060 /* re-enable interrupts if necessary */
1062 enable_interrupts ();
1064 /* data polling for D7 */
1065 start
= get_timer (0);
1066 while ((*((vu_long
*) dest
) & 0x00800080) != (data
& 0x00800080)) {
1067 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {