to make u-boot work for fat32 filesystem
[jz_uboot.git] / board / evb64260 / intel_flash.c
blobed6a2a029221e01b4b7e4ae36bda05b206e1f2a6
1 /*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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,
21 * MA 02111-1307 USA
23 * Hacked for the Hymod board by Murray.Jensen@cmst.csiro.au, 20-Oct-00
26 #include <common.h>
27 #include <mpc8xx.h>
28 #include <galileo/gt64260R.h>
29 #include <galileo/memory.h>
30 #include "intel_flash.h"
33 /*-----------------------------------------------------------------------
34 * Protection Flags:
36 #define FLAG_PROTECT_SET 0x01
37 #define FLAG_PROTECT_CLEAR 0x02
39 static void
40 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) {
48 #ifdef FLASH_DEBUG
49 printf(" writing reset cmd to addr 0x%08lx\n",
50 (unsigned long)addrw);
51 #endif
52 *addrw = BANK_CMD_RST;
53 addrw++;
57 static void
58 bank_erase_init(flash_info_t *info, int sect)
60 bank_addr_t addrw, saddrw, eaddrw;
61 int flag;
63 #ifdef FLASH_DEBUG
64 printf("0x%08x BANK_CMD_PROG\n", BANK_CMD_PROG);
65 printf("0x%08x BANK_CMD_ERASE1\n", BANK_CMD_ERASE1);
66 printf("0x%08x BANK_CMD_ERASE2\n", BANK_CMD_ERASE2);
67 printf("0x%08x BANK_CMD_CLR_STAT\n", BANK_CMD_CLR_STAT);
68 printf("0x%08x BANK_CMD_RST\n", BANK_CMD_RST);
69 printf("0x%08x BANK_STAT_RDY\n", BANK_STAT_RDY);
70 printf("0x%08x BANK_STAT_ERR\n", BANK_STAT_ERR);
71 #endif
73 saddrw = (bank_addr_t)info->start[sect];
74 eaddrw = BANK_ADDR_NEXT_WORD(saddrw);
76 #ifdef FLASH_DEBUG
77 printf("erasing sector %d, start addr = 0x%08lx "
78 "(bank next word addr = 0x%08lx)\n", sect,
79 (unsigned long)saddrw, (unsigned long)eaddrw);
80 #endif
82 /* Disable intrs which might cause a timeout here */
83 flag = disable_interrupts();
85 for (addrw = saddrw; addrw < eaddrw; addrw++) {
86 #ifdef FLASH_DEBUG
87 printf(" writing erase cmd to addr 0x%08lx\n",
88 (unsigned long)addrw);
89 #endif
90 *addrw = BANK_CMD_ERASE1;
91 *addrw = BANK_CMD_ERASE2;
94 /* re-enable interrupts if necessary */
95 if (flag)
96 enable_interrupts();
99 static int
100 bank_erase_poll(flash_info_t *info, int sect)
102 bank_addr_t addrw, saddrw, eaddrw;
103 int sectdone, haderr;
105 saddrw = (bank_addr_t)info->start[sect];
106 eaddrw = BANK_ADDR_NEXT_WORD(saddrw);
108 sectdone = 1;
109 haderr = 0;
111 for (addrw = saddrw; addrw < eaddrw; addrw++) {
112 bank_word_t stat = *addrw;
114 #ifdef FLASH_DEBUG
115 printf(" checking status at addr "
116 "0x%08x [0x%08x]\n",
117 (unsigned long)addrw, stat);
118 #endif
119 if ((stat & BANK_STAT_RDY) != BANK_STAT_RDY)
120 sectdone = 0;
121 else if ((stat & BANK_STAT_ERR) != 0) {
122 printf(" failed on sector %d "
123 "(stat = 0x%08x) at "
124 "address 0x%p\n",
125 sect, stat, addrw);
126 *addrw = BANK_CMD_CLR_STAT;
127 haderr = 1;
131 if (haderr)
132 return (-1);
133 else
134 return (sectdone);
138 write_word_intel(bank_addr_t addr, bank_word_t value)
140 bank_word_t stat;
141 ulong start;
142 int flag, retval;
144 /* Disable interrupts which might cause a timeout here */
145 flag = disable_interrupts();
147 *addr = BANK_CMD_PROG;
149 *addr = value;
151 /* re-enable interrupts if necessary */
152 if (flag)
153 enable_interrupts();
155 retval = 0;
157 /* data polling for D7 */
158 start = get_timer (0);
159 do {
160 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
161 retval = 1;
162 goto done;
164 stat = *addr;
165 } while ((stat & BANK_STAT_RDY) != BANK_STAT_RDY);
167 if ((stat & BANK_STAT_ERR) != 0) {
168 printf("flash program failed (stat = 0x%08lx) "
169 "at address 0x%08lx\n", (ulong)stat, (ulong)addr);
170 *addr = BANK_CMD_CLR_STAT;
171 retval = 3;
174 done:
175 /* reset to read mode */
176 *addr = BANK_CMD_RST;
178 return (retval);
181 /*-----------------------------------------------------------------------
185 flash_erase_intel(flash_info_t *info, int s_first, int s_last)
187 int prot, sect, haderr;
188 ulong start, now, last;
190 #ifdef FLASH_DEBUG
191 printf("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
192 " Bank # %d: ", s_last - s_first + 1, s_first, s_last,
193 (info - flash_info) + 1);
194 flash_print_info(info);
195 #endif
197 if ((s_first < 0) || (s_first > s_last)) {
198 if (info->flash_id == FLASH_UNKNOWN) {
199 printf ("- missing\n");
200 } else {
201 printf ("- no sectors to erase\n");
203 return 1;
206 prot = 0;
207 for (sect=s_first; sect<=s_last; ++sect) {
208 if (info->protect[sect]) {
209 prot++;
213 if (prot) {
214 printf("- Warning: %d protected sector%s will not be erased!\n",
215 prot, (prot > 1 ? "s" : ""));
218 start = get_timer (0);
219 last = 0;
220 haderr = 0;
222 for (sect = s_first; sect <= s_last; sect++) {
223 if (info->protect[sect] == 0) { /* not protected */
224 ulong estart;
225 int sectdone;
227 bank_erase_init(info, sect);
229 /* wait at least 80us - let's wait 1 ms */
230 udelay (1000);
232 estart = get_timer(start);
234 do {
235 now = get_timer(start);
237 if (now - estart > CFG_FLASH_ERASE_TOUT) {
238 printf ("Timeout (sect %d)\n", sect);
239 haderr = 1;
240 break;
243 #ifndef FLASH_DEBUG
244 /* show that we're waiting */
245 if ((now - last) > 1000) { /* every second */
246 putc ('.');
247 last = now;
249 #endif
251 sectdone = bank_erase_poll(info, sect);
253 if (sectdone < 0) {
254 haderr = 1;
255 break;
258 } while (!sectdone);
260 if (haderr)
261 break;
265 if (haderr > 0)
266 printf (" failed\n");
267 else
268 printf (" done\n");
270 /* reset to read mode */
271 for (sect = s_first; sect <= s_last; sect++) {
272 if (info->protect[sect] == 0) { /* not protected */
273 bank_reset(info, sect);
276 return haderr;