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,
25 #include <board/cogent/flash.h>
27 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
29 #if defined(CONFIG_ENV_IS_IN_FLASH)
30 # ifndef CONFIG_ENV_ADDR
31 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
33 # ifndef CONFIG_ENV_SIZE
34 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
36 # ifndef CONFIG_ENV_SECT_SIZE
37 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
41 /*-----------------------------------------------------------------------
44 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
46 /*-----------------------------------------------------------------------
50 #if defined(CONFIG_CMA302)
53 * probe for the existence of flash at address "addr"
54 * 0 = yes, 1 = bad Manufacturer's Id, 2 = bad Device Id
57 c302f_probe_word(c302f_addr_t addr
)
60 *addr
= C302F_BNK_CMD_RST
;
62 /* check the manufacturer id */
63 *addr
= C302F_BNK_CMD_RD_ID
;
64 if (*C302F_BNK_ADDR_MAN(addr
) != C302F_BNK_RD_ID_MAN
)
67 /* check the device id */
68 *addr
= C302F_BNK_CMD_RD_ID
;
69 if (*C302F_BNK_ADDR_DEV(addr
) != C302F_BNK_RD_ID_DEV
)
76 printf("\nMaster Lock Config = 0x%08lx\n",
77 *C302F_BNK_ADDR_CFGM(addr
));
78 for (i
= 0; i
< C302F_BNK_NBLOCKS
; i
++)
79 printf("Block %2d Lock Config = 0x%08lx\n",
80 i
, *C302F_BNK_ADDR_CFG(i
, addr
));
84 /* reset the flash again */
85 *addr
= C302F_BNK_CMD_RST
;
91 * probe for Cogent CMA302 flash module at address "base" and store
92 * info for any found into flash_info entry "fip". Must find at least
96 c302f_probe(flash_info_t
*fip
, c302f_addr_t base
)
98 c302f_addr_t addr
, eaddr
;
102 fip
->sector_count
= 0;
105 eaddr
= C302F_BNK_ADDR_BASE(addr
, C302F_MAX_BANKS
);
108 while (addr
< eaddr
) {
109 c302f_addr_t addrw
, eaddrw
, addrb
;
113 eaddrw
= C302F_BNK_ADDR_NEXT_WORD(addrw
);
115 while (addrw
< eaddrw
)
116 if (c302f_probe_word(addrw
++) != 0)
119 /* bank exists - append info for this bank to *fip */
120 fip
->flash_id
= FLASH_MAN_INTEL
|FLASH_28F008S5
;
121 fip
->size
+= C302F_BNK_SIZE
;
122 osc
= fip
->sector_count
;
123 fip
->sector_count
+= C302F_BNK_NBLOCKS
;
124 if ((nsc
= fip
->sector_count
) >= CONFIG_SYS_MAX_FLASH_SECT
)
125 panic("Too many sectors in flash at address 0x%08lx\n",
126 (unsigned long)base
);
129 for (i
= osc
; i
< nsc
; i
++) {
130 fip
->start
[i
] = (ulong
)addrb
;
132 addrb
= C302F_BNK_ADDR_NEXT_BLK(addrb
);
135 addr
= C302F_BNK_ADDR_NEXT_BNK(addr
);
141 panic("ERROR: no flash found at address 0x%08lx\n",
142 (unsigned long)base
);
146 c302f_reset(flash_info_t
*info
, int sect
)
148 c302f_addr_t addrw
, eaddrw
;
150 addrw
= (c302f_addr_t
)info
->start
[sect
];
151 eaddrw
= C302F_BNK_ADDR_NEXT_WORD(addrw
);
153 while (addrw
< eaddrw
) {
155 printf(" writing reset cmd to addr 0x%08lx\n",
156 (unsigned long)addrw
);
158 *addrw
= C302F_BNK_CMD_RST
;
164 c302f_erase_init(flash_info_t
*info
, int sect
)
166 c302f_addr_t addrw
, saddrw
, eaddrw
;
170 printf("0x%08lx C302F_BNK_CMD_PROG\n", C302F_BNK_CMD_PROG
);
171 printf("0x%08lx C302F_BNK_CMD_ERASE1\n", C302F_BNK_CMD_ERASE1
);
172 printf("0x%08lx C302F_BNK_CMD_ERASE2\n", C302F_BNK_CMD_ERASE2
);
173 printf("0x%08lx C302F_BNK_CMD_CLR_STAT\n", C302F_BNK_CMD_CLR_STAT
);
174 printf("0x%08lx C302F_BNK_CMD_RST\n", C302F_BNK_CMD_RST
);
175 printf("0x%08lx C302F_BNK_STAT_RDY\n", C302F_BNK_STAT_RDY
);
176 printf("0x%08lx C302F_BNK_STAT_ERR\n", C302F_BNK_STAT_ERR
);
179 saddrw
= (c302f_addr_t
)info
->start
[sect
];
180 eaddrw
= C302F_BNK_ADDR_NEXT_WORD(saddrw
);
183 printf("erasing sector %d, start addr = 0x%08lx "
184 "(bank next word addr = 0x%08lx)\n", sect
,
185 (unsigned long)saddrw
, (unsigned long)eaddrw
);
188 /* Disable intrs which might cause a timeout here */
189 flag
= disable_interrupts();
191 for (addrw
= saddrw
; addrw
< eaddrw
; addrw
++) {
193 printf(" writing erase cmd to addr 0x%08lx\n",
194 (unsigned long)addrw
);
196 *addrw
= C302F_BNK_CMD_ERASE1
;
197 *addrw
= C302F_BNK_CMD_ERASE2
;
200 /* re-enable interrupts if necessary */
206 c302f_erase_poll(flash_info_t
*info
, int sect
)
208 c302f_addr_t addrw
, saddrw
, eaddrw
;
209 int sectdone
, haderr
;
211 saddrw
= (c302f_addr_t
)info
->start
[sect
];
212 eaddrw
= C302F_BNK_ADDR_NEXT_WORD(saddrw
);
217 for (addrw
= saddrw
; addrw
< eaddrw
; addrw
++) {
218 c302f_word_t stat
= *addrw
;
221 printf(" checking status at addr "
222 "0x%08lx [0x%08lx]\n",
223 (unsigned long)addrw
, stat
);
225 if ((stat
& C302F_BNK_STAT_RDY
) != C302F_BNK_STAT_RDY
)
227 else if ((stat
& C302F_BNK_STAT_ERR
) != 0) {
228 printf(" failed on sector %d "
229 "(stat = 0x%08lx) at "
232 (unsigned long)addrw
);
233 *addrw
= C302F_BNK_CMD_CLR_STAT
;
245 c302f_write_word(c302f_addr_t addr
, c302f_word_t value
)
251 /* Disable interrupts which might cause a timeout here */
252 flag
= disable_interrupts();
254 *addr
= C302F_BNK_CMD_PROG
;
258 /* re-enable interrupts if necessary */
264 /* data polling for D7 */
265 start
= get_timer (0);
267 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
272 } while ((stat
& C302F_BNK_STAT_RDY
) != C302F_BNK_STAT_RDY
);
274 if ((stat
& C302F_BNK_STAT_ERR
) != 0) {
275 printf("flash program failed (stat = 0x%08lx) "
276 "at address 0x%08lx\n", (ulong
)stat
, (ulong
)addr
);
277 *addr
= C302F_BNK_CMD_CLR_STAT
;
282 /* reset to read mode */
283 *addr
= C302F_BNK_CMD_RST
;
288 #endif /* CONFIG_CMA302 */
297 /* Init: no FLASHes known */
298 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
299 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
302 fip
= &flash_info
[0];
305 #if defined(CONFIG_CMA302)
306 c302f_probe(fip
, (c302f_addr_t
)CONFIG_SYS_FLASH_BASE
);
311 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
313 cmbf_probe(fip, (cmbf_addr_t)CMA_MB_FLASH_BASE);
320 * protect monitor and environment sectors
323 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
324 flash_protect(FLAG_PROTECT_SET
,
325 CONFIG_SYS_MONITOR_BASE
,
326 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
330 #ifdef CONFIG_ENV_IS_IN_FLASH
331 /* ENV protection ON by default */
332 flash_protect(FLAG_PROTECT_SET
,
334 CONFIG_ENV_ADDR
+CONFIG_ENV_SECT_SIZE
-1,
340 /*-----------------------------------------------------------------------
343 flash_print_info(flash_info_t
*info
)
347 if (info
->flash_id
== FLASH_UNKNOWN
) {
348 printf ("missing or unknown FLASH type\n");
352 switch (info
->flash_id
& FLASH_VENDMASK
) {
353 case FLASH_MAN_INTEL
: printf ("INTEL "); break;
354 default: printf ("Unknown Vendor "); break;
357 switch (info
->flash_id
& FLASH_TYPEMASK
) {
358 case FLASH_28F008S5
: printf ("28F008S5\n");
360 default: printf ("Unknown Chip Type\n");
364 printf (" Size: %ld MB in %d Sectors\n",
365 info
->size
>> 20, info
->sector_count
);
367 printf (" Sector Start Addresses:");
368 for (i
=0; i
<info
->sector_count
; ++i
) {
371 printf (" %2d - %08lX%s", i
,
373 info
->protect
[i
] ? " (RO)" : " "
380 /*-----------------------------------------------------------------------
384 /*-----------------------------------------------------------------------
388 * The following code cannot be run from FLASH!
392 flash_erase(flash_info_t
*info
, int s_first
, int s_last
)
394 int prot
, sect
, haderr
;
395 ulong start
, now
, last
;
396 void (*erase_init
)(flash_info_t
*, int);
397 int (*erase_poll
)(flash_info_t
*, int);
398 void (*reset
)(flash_info_t
*, int);
402 printf("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
403 " Bank # %d: ", s_last
- s_first
+ 1, s_first
, s_last
,
404 (info
- flash_info
) + 1);
405 flash_print_info(info
);
408 if ((s_first
< 0) || (s_first
> s_last
)) {
409 if (info
->flash_id
== FLASH_UNKNOWN
) {
410 printf ("- missing\n");
412 printf ("- no sectors to erase\n");
417 switch (info
->flash_id
) {
419 #if defined(CONFIG_CMA302)
420 case FLASH_MAN_INTEL
|FLASH_28F008S5
:
421 erase_init
= c302f_erase_init
;
422 erase_poll
= c302f_erase_poll
;
427 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
428 case FLASH_MAN_INTEL
|FLASH_28F800_B
:
429 case FLASH_MAN_AMD
|FLASH_AM29F800B
:
431 erase_init = cmbf_erase_init;
432 erase_poll = cmbf_erase_poll;
439 printf ("Flash type %08lx not supported - aborted\n",
445 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
446 if (info
->protect
[sect
]) {
452 printf("- Warning: %d protected sector%s will not be erased!\n",
453 prot
, (prot
> 1 ? "s" : ""));
456 start
= get_timer (0);
460 for (sect
= s_first
; sect
<= s_last
; sect
++) {
461 if (info
->protect
[sect
] == 0) { /* not protected */
465 (*erase_init
)(info
, sect
);
467 /* wait at least 80us - let's wait 1 ms */
470 estart
= get_timer(start
);
473 now
= get_timer(start
);
475 if (now
- estart
> CONFIG_SYS_FLASH_ERASE_TOUT
) {
476 printf ("Timeout (sect %d)\n", sect
);
482 /* show that we're waiting */
483 if ((now
- last
) > 1000) { /* every second */
489 sectdone
= (*erase_poll
)(info
, sect
);
504 printf (" failed\n");
510 /* reset to read mode */
511 for (sect
= s_first
; sect
<= s_last
; sect
++) {
512 if (info
->protect
[sect
] == 0) { /* not protected */
513 (*reset
)(info
, sect
);
519 /*-----------------------------------------------------------------------
520 * Copy memory to flash, returns:
523 * 2 - Flash not erased
528 write_buff(flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
532 ulong start
, now
, last
;
534 wp
= (addr
& ~3); /* get lower word aligned address */
537 * handle unaligned start bytes
539 if ((l
= addr
- wp
) != 0) {
541 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
542 data
= (data
<< 8) | (*(uchar
*)cp
);
544 for (; i
<4 && cnt
>0; ++i
) {
545 data
= (data
<< 8) | *src
++;
549 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
550 data
= (data
<< 8) | (*(uchar
*)cp
);
553 if ((rc
= write_word(info
, wp
, data
)) != 0) {
560 * handle word aligned part
562 start
= get_timer (0);
566 for (i
=0; i
<4; ++i
) {
567 data
= (data
<< 8) | *src
++;
569 if ((rc
= write_word(info
, wp
, data
)) != 0) {
575 /* show that we're waiting */
576 now
= get_timer(start
);
577 if ((now
- last
) > 1000) { /* every second */
588 * handle unaligned tail bytes
591 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
592 data
= (data
<< 8) | *src
++;
595 for (; i
<4; ++i
, ++cp
) {
596 data
= (data
<< 8) | (*(uchar
*)cp
);
599 return (write_word(info
, wp
, data
));
602 /*-----------------------------------------------------------------------
603 * Write a word to Flash, returns:
606 * 2 - Flash not erased
610 write_word(flash_info_t
*info
, ulong dest
, ulong data
)
614 /* Check if Flash is (sufficiently) erased */
615 if ((*(ulong
*)dest
& data
) != data
) {
619 switch (info
->flash_id
) {
621 #if defined(CONFIG_CMA302)
622 case FLASH_MAN_INTEL
|FLASH_28F008S5
:
623 retval
= c302f_write_word((c302f_addr_t
)dest
, (c302f_word_t
)data
);
627 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
628 case FLASH_MAN_INTEL
|FLASH_28F800_B
:
629 case FLASH_MAN_AMD
|FLASH_AM29F800B
:
631 retval = cmbf_write_word((cmbf_addr_t)dest, (cmbf_word_t)data);
638 printf ("Flash type %08lx not supported - aborted\n",
647 /*-----------------------------------------------------------------------