3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
7 * Hacked for the marvell db64360 eval board by
8 * Ingo Assmus <ingo.assmus@keymile.com>
13 #include "../include/mv_gen_reg.h"
14 #include "../include/memory.h"
15 #include "intel_flash.h"
18 /*-----------------------------------------------------------------------
21 #define FLAG_PROTECT_SET 0x01
22 #define FLAG_PROTECT_CLEAR 0x02
24 static void bank_reset (flash_info_t
* info
, int sect
)
26 bank_addr_t addrw
, eaddrw
;
28 addrw
= (bank_addr_t
) info
->start
[sect
];
29 eaddrw
= BANK_ADDR_NEXT_WORD (addrw
);
31 while (addrw
< eaddrw
) {
33 printf (" writing reset cmd to addr 0x%08lx\n",
34 (unsigned long) addrw
);
36 *addrw
= BANK_CMD_RST
;
41 static void bank_erase_init (flash_info_t
* info
, int sect
)
43 bank_addr_t addrw
, saddrw
, eaddrw
;
47 printf ("0x%08x BANK_CMD_PROG\n", BANK_CMD_PROG
);
48 printf ("0x%08x BANK_CMD_ERASE1\n", BANK_CMD_ERASE1
);
49 printf ("0x%08x BANK_CMD_ERASE2\n", BANK_CMD_ERASE2
);
50 printf ("0x%08x BANK_CMD_CLR_STAT\n", BANK_CMD_CLR_STAT
);
51 printf ("0x%08x BANK_CMD_RST\n", BANK_CMD_RST
);
52 printf ("0x%08x BANK_STAT_RDY\n", BANK_STAT_RDY
);
53 printf ("0x%08x BANK_STAT_ERR\n", BANK_STAT_ERR
);
56 saddrw
= (bank_addr_t
) info
->start
[sect
];
57 eaddrw
= BANK_ADDR_NEXT_WORD (saddrw
);
60 printf ("erasing sector %d, start addr = 0x%08lx "
61 "(bank next word addr = 0x%08lx)\n", sect
,
62 (unsigned long) saddrw
, (unsigned long) eaddrw
);
65 /* Disable intrs which might cause a timeout here */
66 flag
= disable_interrupts ();
68 for (addrw
= saddrw
; addrw
< eaddrw
; addrw
++) {
70 printf (" writing erase cmd to addr 0x%08lx\n",
71 (unsigned long) addrw
);
73 *addrw
= BANK_CMD_ERASE1
;
74 *addrw
= BANK_CMD_ERASE2
;
77 /* re-enable interrupts if necessary */
82 static int bank_erase_poll (flash_info_t
* info
, int sect
)
84 bank_addr_t addrw
, saddrw
, eaddrw
;
87 saddrw
= (bank_addr_t
) info
->start
[sect
];
88 eaddrw
= BANK_ADDR_NEXT_WORD (saddrw
);
93 for (addrw
= saddrw
; addrw
< eaddrw
; addrw
++) {
94 bank_word_t stat
= *addrw
;
97 printf (" checking status at addr "
98 "0x%08x [0x%08x]\n", (unsigned long) addrw
, stat
);
100 if ((stat
& BANK_STAT_RDY
) != BANK_STAT_RDY
)
102 else if ((stat
& BANK_STAT_ERR
) != 0) {
103 printf (" failed on sector %d "
104 "(stat = 0x%08x) at "
105 "address 0x%p\n", sect
, stat
, addrw
);
106 *addrw
= BANK_CMD_CLR_STAT
;
117 int write_word_intel (bank_addr_t addr
, bank_word_t value
)
123 /* Disable interrupts which might cause a timeout here */
124 flag
= disable_interrupts ();
126 *addr
= BANK_CMD_PROG
;
130 /* re-enable interrupts if necessary */
132 enable_interrupts ();
136 /* data polling for D7 */
137 start
= get_timer (0);
139 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
144 } while ((stat
& BANK_STAT_RDY
) != BANK_STAT_RDY
);
146 if ((stat
& BANK_STAT_ERR
) != 0) {
147 printf ("flash program failed (stat = 0x%08lx) "
148 "at address 0x%08lx\n", (ulong
) stat
, (ulong
) addr
);
149 *addr
= BANK_CMD_CLR_STAT
;
154 /* reset to read mode */
155 *addr
= BANK_CMD_RST
;
160 /*-----------------------------------------------------------------------
163 int flash_erase_intel (flash_info_t
* info
, int s_first
, int s_last
)
165 int prot
, sect
, haderr
;
166 ulong start
, now
, last
;
169 printf ("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
170 " Bank # %d: ", s_last
- s_first
+ 1, s_first
, s_last
,
171 (info
- flash_info
) + 1);
172 flash_print_info (info
);
175 if ((s_first
< 0) || (s_first
> s_last
)) {
176 if (info
->flash_id
== FLASH_UNKNOWN
) {
177 printf ("- missing\n");
179 printf ("- no sectors to erase\n");
185 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
186 if (info
->protect
[sect
]) {
192 printf ("- Warning: %d protected sector%s will not be erased!\n", prot
, (prot
> 1 ? "s" : ""));
195 start
= get_timer (0);
199 for (sect
= s_first
; sect
<= s_last
; sect
++) {
200 if (info
->protect
[sect
] == 0) { /* not protected */
204 bank_erase_init (info
, sect
);
206 /* wait at least 80us - let's wait 1 ms */
209 estart
= get_timer (start
);
212 now
= get_timer (start
);
214 if (now
- estart
> CONFIG_SYS_FLASH_ERASE_TOUT
) {
215 printf ("Timeout (sect %d)\n", sect
);
220 /* show that we're waiting */
221 if ((now
- last
) > 1000) { /* every second */
227 sectdone
= bank_erase_poll (info
, sect
);
242 printf (" failed\n");
246 /* reset to read mode */
247 for (sect
= s_first
; sect
<= s_last
; sect
++) {
248 if (info
->protect
[sect
] == 0) { /* not protected */
249 bank_reset (info
, sect
);