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 marvell db64360 eval board by
24 * Ingo Assmus <ingo.assmus@keymile.com>
29 #include "../include/mv_gen_reg.h"
30 #include "../include/memory.h"
31 #include "intel_flash.h"
34 /*-----------------------------------------------------------------------
37 #define FLAG_PROTECT_SET 0x01
38 #define FLAG_PROTECT_CLEAR 0x02
40 static void bank_reset (flash_info_t * info, int sect)
42 bank_addr_t addrw, eaddrw;
44 addrw = (bank_addr_t) info->start[sect];
45 eaddrw = BANK_ADDR_NEXT_WORD (addrw);
47 while (addrw < eaddrw) {
49 printf (" writing reset cmd to addr 0x%08lx\n",
50 (unsigned long) addrw);
52 *addrw = BANK_CMD_RST;
57 static void bank_erase_init (flash_info_t * info, int sect)
59 bank_addr_t addrw, saddrw, eaddrw;
63 printf ("0x%08x BANK_CMD_PROG\n", BANK_CMD_PROG);
64 printf ("0x%08x BANK_CMD_ERASE1\n", BANK_CMD_ERASE1);
65 printf ("0x%08x BANK_CMD_ERASE2\n", BANK_CMD_ERASE2);
66 printf ("0x%08x BANK_CMD_CLR_STAT\n", BANK_CMD_CLR_STAT);
67 printf ("0x%08x BANK_CMD_RST\n", BANK_CMD_RST);
68 printf ("0x%08x BANK_STAT_RDY\n", BANK_STAT_RDY);
69 printf ("0x%08x BANK_STAT_ERR\n", BANK_STAT_ERR);
72 saddrw = (bank_addr_t) info->start[sect];
73 eaddrw = BANK_ADDR_NEXT_WORD (saddrw);
76 printf ("erasing sector %d, start addr = 0x%08lx "
77 "(bank next word addr = 0x%08lx)\n", sect,
78 (unsigned long) saddrw, (unsigned long) eaddrw);
81 /* Disable intrs which might cause a timeout here */
82 flag = disable_interrupts ();
84 for (addrw = saddrw; addrw < eaddrw; addrw++) {
86 printf (" writing erase cmd to addr 0x%08lx\n",
87 (unsigned long) addrw);
89 *addrw = BANK_CMD_ERASE1;
90 *addrw = BANK_CMD_ERASE2;
93 /* re-enable interrupts if necessary */
98 static int bank_erase_poll (flash_info_t * info, int sect)
100 bank_addr_t addrw, saddrw, eaddrw;
101 int sectdone, haderr;
103 saddrw = (bank_addr_t) info->start[sect];
104 eaddrw = BANK_ADDR_NEXT_WORD (saddrw);
109 for (addrw = saddrw; addrw < eaddrw; addrw++) {
110 bank_word_t stat = *addrw;
113 printf (" checking status at addr "
114 "0x%08x [0x%08x]\n", (unsigned long) addrw, stat);
116 if ((stat & BANK_STAT_RDY) != BANK_STAT_RDY)
118 else if ((stat & BANK_STAT_ERR) != 0) {
119 printf (" failed on sector %d "
120 "(stat = 0x%08x) at "
121 "address 0x%p\n", sect, stat, addrw);
122 *addrw = BANK_CMD_CLR_STAT;
133 int write_word_intel (bank_addr_t addr, bank_word_t value)
139 /* Disable interrupts which might cause a timeout here */
140 flag = disable_interrupts ();
142 *addr = BANK_CMD_PROG;
146 /* re-enable interrupts if necessary */
148 enable_interrupts ();
152 /* data polling for D7 */
153 start = get_timer (0);
155 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
160 } while ((stat & BANK_STAT_RDY) != BANK_STAT_RDY);
162 if ((stat & BANK_STAT_ERR) != 0) {
163 printf ("flash program failed (stat = 0x%08lx) "
164 "at address 0x%08lx\n", (ulong) stat, (ulong) addr);
165 *addr = BANK_CMD_CLR_STAT;
170 /* reset to read mode */
171 *addr = BANK_CMD_RST;
176 /*-----------------------------------------------------------------------
179 int flash_erase_intel (flash_info_t * info, int s_first, int s_last)
181 int prot, sect, haderr;
182 ulong start, now, last;
185 printf ("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
186 " Bank # %d: ", s_last - s_first + 1, s_first, s_last,
187 (info - flash_info) + 1);
188 flash_print_info (info);
191 if ((s_first < 0) || (s_first > s_last)) {
192 if (info->flash_id == FLASH_UNKNOWN) {
193 printf ("- missing\n");
195 printf ("- no sectors to erase\n");
201 for (sect = s_first; sect <= s_last; ++sect) {
202 if (info->protect[sect]) {
208 printf ("- Warning: %d protected sector%s will not be erased!\n", prot, (prot > 1 ? "s" : ""));
211 start = get_timer (0);
215 for (sect = s_first; sect <= s_last; sect++) {
216 if (info->protect[sect] == 0) { /* not protected */
220 bank_erase_init (info, sect);
222 /* wait at least 80us - let's wait 1 ms */
225 estart = get_timer (start);
228 now = get_timer (start);
230 if (now - estart > CFG_FLASH_ERASE_TOUT) {
231 printf ("Timeout (sect %d)\n", sect);
236 /* show that we're waiting */
237 if ((now - last) > 1000) { /* every second */
243 sectdone = bank_erase_poll (info, sect);
258 printf (" failed\n");
262 /* reset to read mode */
263 for (sect = s_first; sect <= s_last; sect++) {
264 if (info->protect[sect] == 0) { /* not protected */
265 bank_reset (info, sect);