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,
23 * Hacked for the Hymod board by Murray.Jensen@csiro.au, 20-Oct-00
28 #include <board/hymod/flash.h>
30 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
32 /*-----------------------------------------------------------------------
35 #define FLAG_PROTECT_SET 0x01
36 #define FLAG_PROTECT_CLEAR 0x02
38 /*-----------------------------------------------------------------------
42 * probe for flash bank at address "base" and store info about it
43 * in the flash_info entry "fip". Fatal error if nothing there.
46 bank_probe (flash_info_t
*fip
, volatile bank_addr_t base
)
48 volatile bank_addr_t addr
;
55 /* put flash into read id mode */
56 *base
= BANK_CMD_RD_ID
;
58 /* check the manufacturer id - must be intel */
59 word
= *BANK_REG_MAN_CODE (base
);
60 if (word
!= BANK_FILL_WORD (INTEL_MANUFACT
&0xff))
61 panic ("\nbad manufacturer's code (0x%08lx) at addr 0x%08lx",
62 (unsigned long)word
, (unsigned long)base
);
64 /* check the device id */
65 word
= *BANK_REG_DEV_CODE (base
);
68 case BANK_FILL_WORD (INTEL_ID_28F320J5
&0xff):
69 fip
->flash_id
= FLASH_MAN_INTEL
| FLASH_28F320J5
;
70 fip
->sector_count
= 32;
73 case BANK_FILL_WORD (INTEL_ID_28F640J5
&0xff):
74 fip
->flash_id
= FLASH_MAN_INTEL
| FLASH_28F640J5
;
75 fip
->sector_count
= 64;
78 case BANK_FILL_WORD (INTEL_ID_28F320J3A
&0xff):
79 fip
->flash_id
= FLASH_MAN_INTEL
| FLASH_28F320J3A
;
80 fip
->sector_count
= 32;
83 case BANK_FILL_WORD (INTEL_ID_28F640J3A
&0xff):
84 fip
->flash_id
= FLASH_MAN_INTEL
| FLASH_28F640J3A
;
85 fip
->sector_count
= 64;
88 case BANK_FILL_WORD (INTEL_ID_28F128J3A
&0xff):
89 fip
->flash_id
= FLASH_MAN_INTEL
| FLASH_28F128J3A
;
90 fip
->sector_count
= 128;
94 panic ("\nbad device code (0x%08lx) at addr 0x%08lx",
95 (unsigned long)word
, (unsigned long)base
);
98 if (fip
->sector_count
>= CONFIG_SYS_MAX_FLASH_SECT
)
99 panic ("\ntoo many sectors (%d) in flash at address 0x%08lx",
100 fip
->sector_count
, (unsigned long)base
);
103 for (i
= 0; i
< fip
->sector_count
; i
++) {
104 fip
->start
[i
] = (unsigned long)addr
;
106 addr
= BANK_ADDR_NEXT_BLK (addr
);
109 fip
->size
= (bank_size_t
)addr
- (bank_size_t
)base
;
111 /* reset the flash */
112 *base
= BANK_CMD_RST
;
116 bank_reset (flash_info_t
*info
, int sect
)
118 volatile bank_addr_t addr
= (bank_addr_t
)info
->start
[sect
];
121 printf ("writing reset cmd to addr 0x%08lx\n", (unsigned long)addr
);
124 *addr
= BANK_CMD_RST
;
128 bank_erase_init (flash_info_t
*info
, int sect
)
130 volatile bank_addr_t addr
= (bank_addr_t
)info
->start
[sect
];
134 printf ("erasing sector %d, addr = 0x%08lx\n",
135 sect
, (unsigned long)addr
);
138 /* Disable intrs which might cause a timeout here */
139 flag
= disable_interrupts ();
142 printf ("writing erase cmd to addr 0x%08lx\n", (unsigned long)addr
);
144 *addr
= BANK_CMD_ERASE1
;
145 *addr
= BANK_CMD_ERASE2
;
147 /* re-enable interrupts if necessary */
149 enable_interrupts ();
153 bank_erase_poll (flash_info_t
*info
, int sect
)
155 volatile bank_addr_t addr
= (bank_addr_t
)info
->start
[sect
];
156 bank_word_t stat
= *addr
;
159 printf ("checking status at addr 0x%08lx [0x%08lx]\n",
160 (unsigned long)addr
, (unsigned long)stat
);
163 if ((stat
& BANK_STAT_RDY
) == BANK_STAT_RDY
) {
164 if ((stat
& BANK_STAT_ERR
) != 0) {
165 printf ("failed on sector %d [0x%08lx] at "
166 "address 0x%08lx\n", sect
,
167 (unsigned long)stat
, (unsigned long)addr
);
168 *addr
= BANK_CMD_CLR_STAT
;
179 bank_write_word (volatile bank_addr_t addr
, bank_word_t value
)
185 /* Disable interrupts which might cause a timeout here */
186 flag
= disable_interrupts ();
188 *addr
= BANK_CMD_PROG
;
192 /* re-enable interrupts if necessary */
194 enable_interrupts ();
198 /* data polling for D7 */
199 start
= get_timer (0);
201 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
206 } while ((stat
& BANK_STAT_RDY
) != BANK_STAT_RDY
);
208 if ((stat
& BANK_STAT_ERR
) != 0) {
209 printf ("flash program failed [0x%08lx] at address 0x%08lx\n",
210 (unsigned long)stat
, (unsigned long)addr
);
211 *addr
= BANK_CMD_CLR_STAT
;
216 /* reset to read mode */
217 *addr
= BANK_CMD_RST
;
222 /*-----------------------------------------------------------------------
230 /* Init: no FLASHes known */
231 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
232 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
235 bank_probe (&flash_info
[0], (bank_addr_t
)CONFIG_SYS_FLASH_BASE
);
238 * protect monitor and environment sectors
241 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
242 (void)flash_protect (FLAG_PROTECT_SET
,
243 CONFIG_SYS_MONITOR_BASE
,
244 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
248 #if defined(CONFIG_SYS_FLASH_ENV_ADDR)
249 (void)flash_protect (FLAG_PROTECT_SET
,
250 CONFIG_SYS_FLASH_ENV_ADDR
,
251 #if defined(CONFIG_SYS_FLASH_ENV_BUF)
252 CONFIG_SYS_FLASH_ENV_ADDR
+ CONFIG_SYS_FLASH_ENV_BUF
- 1,
254 CONFIG_SYS_FLASH_ENV_ADDR
+ CONFIG_SYS_FLASH_ENV_SIZE
- 1,
259 return flash_info
[0].size
;
262 /*-----------------------------------------------------------------------
265 flash_print_info (flash_info_t
*info
)
269 if (info
->flash_id
== FLASH_UNKNOWN
) {
270 printf ("missing or unknown FLASH type\n");
274 switch (info
->flash_id
& FLASH_VENDMASK
) {
275 case FLASH_MAN_INTEL
: printf ("INTEL "); break;
276 default: printf ("Unknown Vendor "); break;
279 switch (info
->flash_id
& FLASH_TYPEMASK
) {
280 case FLASH_28F320J5
: printf ("28F320J5 (32 Mbit, 2 x 16bit)\n");
282 case FLASH_28F640J5
: printf ("28F640J5 (64 Mbit, 2 x 16bit)\n");
284 case FLASH_28F320J3A
: printf ("28F320J3A (32 Mbit, 2 x 16bit)\n");
286 case FLASH_28F640J3A
: printf ("28F640J3A (64 Mbit, 2 x 16bit)\n");
288 case FLASH_28F128J3A
: printf ("28F320J3A (128 Mbit, 2 x 16bit)\n");
290 default: printf ("Unknown Chip Type\n");
294 printf (" Size: %ld MB in %d Sectors\n",
295 info
->size
>> 20, info
->sector_count
);
297 printf (" Sector Start Addresses:");
298 for (i
=0; i
<info
->sector_count
; ++i
) {
303 info
->protect
[i
] ? " (RO)" : " "
311 * The following code cannot be run from FLASH!
314 /*-----------------------------------------------------------------------
318 flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
320 int prot
, sect
, haderr
;
321 ulong start
, now
, last
;
325 printf ("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
326 " Bank # %d: ", s_last
- s_first
+ 1, s_first
, s_last
,
327 (info
- flash_info
) + 1);
328 flash_print_info (info
);
331 if ((s_first
< 0) || (s_first
> s_last
)) {
332 if (info
->flash_id
== FLASH_UNKNOWN
) {
333 printf ("- missing\n");
335 printf ("- no sectors to erase\n");
341 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
342 if (info
->protect
[sect
]) {
348 printf ("- Warning: %d protected sector%s will not be erased\n",
349 prot
, (prot
> 1 ? "s" : ""));
352 start
= get_timer (0);
356 for (sect
= s_first
; sect
<= s_last
; sect
++) {
357 if (info
->protect
[sect
] == 0) { /* not protected */
361 bank_erase_init (info
, sect
);
363 /* wait at least 80us - let's wait 1 ms */
366 estart
= get_timer (start
);
369 now
= get_timer (start
);
371 if (now
- estart
> CONFIG_SYS_FLASH_ERASE_TOUT
) {
372 printf ("Timeout (sect %d)\n", sect
);
379 /* show that we're waiting */
380 if ((now
- last
) > 1000) { /* every second */
386 sectdone
= bank_erase_poll (info
, sect
);
402 printf (" failed\n");
406 /* reset to read mode */
407 for (sect
= s_first
; sect
<= s_last
; sect
++) {
408 if (info
->protect
[sect
] == 0) { /* not protected */
409 bank_reset (info
, sect
);
415 /*-----------------------------------------------------------------------
416 * Write a word to Flash, returns:
419 * 2 - Flash not erased
423 write_word (flash_info_t
*info
, ulong dest
, ulong data
)
425 /* Check if Flash is (sufficiently) erased */
426 if ((*(ulong
*)dest
& data
) != data
)
429 return (bank_write_word ((bank_addr_t
)dest
, (bank_word_t
)data
));
432 /*-----------------------------------------------------------------------
433 * Copy memory to flash, returns:
436 * 2 - Flash not erased
441 write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
446 wp
= (addr
& ~3); /* get lower word aligned address */
449 * handle unaligned start bytes
451 if ((l
= addr
- wp
) != 0) {
453 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
454 data
= (data
<< 8) | (*(uchar
*)cp
);
456 for (; i
<4 && cnt
>0; ++i
) {
457 data
= (data
<< 8) | *src
++;
461 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
462 data
= (data
<< 8) | (*(uchar
*)cp
);
465 if ((rc
= write_word (info
, wp
, data
)) != 0) {
472 * handle word aligned part
476 for (i
=0; i
<4; ++i
) {
477 data
= (data
<< 8) | *src
++;
479 if ((rc
= write_word (info
, wp
, data
)) != 0) {
491 * handle unaligned tail bytes
494 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
495 data
= (data
<< 8) | *src
++;
498 for (; i
<4; ++i
, ++cp
) {
499 data
= (data
<< 8) | (*(uchar
*)cp
);
502 return (write_word (info
, wp
, data
));
505 /*-----------------------------------------------------------------------