blob: dca10be1b5ac31a78dd569be906777a0aca80867 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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
22 */
23
24#include <common.h>
25#include <ppc4xx.h>
26#include <asm/processor.h>
27
28flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
29
30/*-----------------------------------------------------------------------
31 * Functions
32 */
33static int write_word (flash_info_t *info, ulong dest, ulong data);
34
35/*-----------------------------------------------------------------------
36 */
37static void flash_get_offsets (ulong base, flash_info_t *info)
38{
39 int i;
40 short n;
41
42 /* set up sector start address table */
wdenk8bde7f72003-06-27 21:31:46 +000043 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
wdenkaffae2b2002-08-17 09:36:01 +000044 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
45 for (i = 0; i < info->sector_count; i++)
46 info->start[i] = base + (i * 0x00010000);
wdenk8bde7f72003-06-27 21:31:46 +000047 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
wdenkaffae2b2002-08-17 09:36:01 +000048 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
49 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
50 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
51 /* set sector offsets for bottom boot block type */
52 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
53 info->start[i] = base;
54 base += 8 << 10;
55 }
56 while (i < info->sector_count) { /* 64k regular sectors */
57 info->start[i] = base;
58 base += 64 << 10;
59 ++i;
60 }
wdenk8bde7f72003-06-27 21:31:46 +000061 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
wdenkaffae2b2002-08-17 09:36:01 +000062 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
63 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
64 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
65 /* set sector offsets for top boot block type */
66 base += info->size;
67 i = info->sector_count;
68 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
69 base -= 8 << 10;
70 --i;
71 info->start[i] = base;
72 }
73 while (i > 0) { /* 64k regular sectors */
74 base -= 64 << 10;
75 --i;
76 info->start[i] = base;
77 }
wdenk8bde7f72003-06-27 21:31:46 +000078 } else {
wdenkaffae2b2002-08-17 09:36:01 +000079 if (info->flash_id & FLASH_BTYPE) {
80 /* set sector offsets for bottom boot block type */
81 info->start[0] = base + 0x00000000;
82 info->start[1] = base + 0x00004000;
83 info->start[2] = base + 0x00006000;
84 info->start[3] = base + 0x00008000;
85 for (i = 4; i < info->sector_count; i++) {
86 info->start[i] = base + (i * 0x00010000) - 0x00030000;
87 }
88 } else {
89 /* set sector offsets for top boot block type */
90 i = info->sector_count - 1;
91 info->start[i--] = base + info->size - 0x00004000;
92 info->start[i--] = base + info->size - 0x00006000;
93 info->start[i--] = base + info->size - 0x00008000;
94 for (; i >= 0; i--) {
95 info->start[i] = base + i * 0x00010000;
96 }
97 }
98 }
99}
100
101/*-----------------------------------------------------------------------
102 */
103void flash_print_info (flash_info_t *info)
104{
105 int i;
wdenk8bde7f72003-06-27 21:31:46 +0000106 int k;
107 int size;
108 int erased;
109 volatile unsigned long *flash;
wdenkaffae2b2002-08-17 09:36:01 +0000110
111 if (info->flash_id == FLASH_UNKNOWN) {
112 printf ("missing or unknown FLASH type\n");
113 return;
114 }
115
116 switch (info->flash_id & FLASH_VENDMASK) {
117 case FLASH_MAN_AMD: printf ("AMD "); break;
118 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
119 case FLASH_MAN_SST: printf ("SST "); break;
stroese0621f6f2004-12-16 18:43:13 +0000120 case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break;
wdenkaffae2b2002-08-17 09:36:01 +0000121 default: printf ("Unknown Vendor "); break;
122 }
123
124 switch (info->flash_id & FLASH_TYPEMASK) {
125 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
126 break;
127 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
128 break;
129 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
130 break;
131 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
132 break;
133 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
134 break;
135 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
136 break;
137 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
138 break;
139 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
140 break;
141 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
142 break;
143 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
144 break;
145 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
146 break;
147 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
148 break;
149 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
150 break;
151 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
152 break;
153 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
154 break;
stroese0621f6f2004-12-16 18:43:13 +0000155 case FLASH_SST320: printf ("SST39LF/VF320 (32 Mbit, uniform sector size)\n");
156 break;
157 case FLASH_SST640: printf ("SST39LF/VF640 (64 Mbit, uniform sector size)\n");
158 break;
wdenkaffae2b2002-08-17 09:36:01 +0000159 default: printf ("Unknown Chip Type\n");
160 break;
161 }
162
163 printf (" Size: %ld MB in %d Sectors\n",
164 info->size >> 20, info->sector_count);
165
166 printf (" Sector Start Addresses:");
167 for (i=0; i<info->sector_count; ++i) {
168#ifdef CFG_FLASH_EMPTY_INFO
wdenk8bde7f72003-06-27 21:31:46 +0000169 /*
170 * Check if whole sector is erased
171 */
172 if (i != (info->sector_count-1))
173 size = info->start[i+1] - info->start[i];
174 else
175 size = info->start[0] + info->size - info->start[i];
176 erased = 1;
177 flash = (volatile unsigned long *)info->start[i];
178 size = size >> 2; /* divide by 4 for longword access */
179 for (k=0; k<size; k++)
180 {
181 if (*flash++ != 0xffffffff)
182 {
183 erased = 0;
184 break;
185 }
186 }
wdenkaffae2b2002-08-17 09:36:01 +0000187
188 if ((i % 5) == 0)
189 printf ("\n ");
wdenk8bde7f72003-06-27 21:31:46 +0000190 /* print empty and read-only info */
wdenkaffae2b2002-08-17 09:36:01 +0000191 printf (" %08lX%s%s",
192 info->start[i],
193 erased ? " E" : " ",
194 info->protect[i] ? "RO " : " ");
195#else
196 if ((i % 5) == 0)
197 printf ("\n ");
198 printf (" %08lX%s",
199 info->start[i],
200 info->protect[i] ? " (RO)" : " ");
201#endif
202
203 }
204 printf ("\n");
205 return;
206}
207
208/*-----------------------------------------------------------------------
209 */
210
211
212/*-----------------------------------------------------------------------
213 */
214
215/*
216 * The following code cannot be run from FLASH!
217 */
218static ulong flash_get_size (vu_long *addr, flash_info_t *info)
219{
220 short i;
221 short n;
222 CFG_FLASH_WORD_SIZE value;
223 ulong base = (ulong)addr;
wdenk8bde7f72003-06-27 21:31:46 +0000224 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)addr;
wdenkaffae2b2002-08-17 09:36:01 +0000225
226 /* Write auto select command: read Manufacturer ID */
227 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
228 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
229 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090;
230
231 value = addr2[CFG_FLASH_READ0];
232
233 switch (value) {
234 case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT:
235 info->flash_id = FLASH_MAN_AMD;
236 break;
237 case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT:
238 info->flash_id = FLASH_MAN_FUJ;
239 break;
240 case (CFG_FLASH_WORD_SIZE)SST_MANUFACT:
241 info->flash_id = FLASH_MAN_SST;
242 break;
stroese0621f6f2004-12-16 18:43:13 +0000243 case (CFG_FLASH_WORD_SIZE)EXCEL_MANUFACT:
244 info->flash_id = FLASH_MAN_EXCEL;
245 break;
wdenkaffae2b2002-08-17 09:36:01 +0000246 default:
247 info->flash_id = FLASH_UNKNOWN;
248 info->sector_count = 0;
249 info->size = 0;
250 return (0); /* no or unknown flash */
251 }
252
253 value = addr2[CFG_FLASH_READ1]; /* device ID */
254
255 switch (value) {
256 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T:
257 info->flash_id += FLASH_AM400T;
258 info->sector_count = 11;
259 info->size = 0x00080000;
260 break; /* => 0.5 MB */
261
262 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B:
263 info->flash_id += FLASH_AM400B;
264 info->sector_count = 11;
265 info->size = 0x00080000;
266 break; /* => 0.5 MB */
267
268 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T:
269 info->flash_id += FLASH_AM800T;
270 info->sector_count = 19;
271 info->size = 0x00100000;
272 break; /* => 1 MB */
273
274 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B:
275 info->flash_id += FLASH_AM800B;
276 info->sector_count = 19;
277 info->size = 0x00100000;
278 break; /* => 1 MB */
279
280 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T:
281 info->flash_id += FLASH_AM160T;
282 info->sector_count = 35;
283 info->size = 0x00200000;
284 break; /* => 2 MB */
285
286 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B:
287 info->flash_id += FLASH_AM160B;
288 info->sector_count = 35;
289 info->size = 0x00200000;
290 break; /* => 2 MB */
291
292 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T:
wdenk8bde7f72003-06-27 21:31:46 +0000293 info->flash_id += FLASH_AM320T;
294 info->sector_count = 71;
wdenkaffae2b2002-08-17 09:36:01 +0000295 info->size = 0x00400000; break; /* => 4 MB */
296
297 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B:
wdenk8bde7f72003-06-27 21:31:46 +0000298 info->flash_id += FLASH_AM320B;
wdenkaffae2b2002-08-17 09:36:01 +0000299 info->sector_count = 71;
300 info->size = 0x00400000; break; /* => 4 MB */
301
302 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T:
wdenk8bde7f72003-06-27 21:31:46 +0000303 info->flash_id += FLASH_AMDL322T;
304 info->sector_count = 71;
wdenkaffae2b2002-08-17 09:36:01 +0000305 info->size = 0x00400000; break; /* => 4 MB */
306
307 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B:
wdenk8bde7f72003-06-27 21:31:46 +0000308 info->flash_id += FLASH_AMDL322B;
wdenkaffae2b2002-08-17 09:36:01 +0000309 info->sector_count = 71;
310 info->size = 0x00400000; break; /* => 4 MB */
311
312 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T:
wdenk8bde7f72003-06-27 21:31:46 +0000313 info->flash_id += FLASH_AMDL323T;
wdenkaffae2b2002-08-17 09:36:01 +0000314 info->sector_count = 71;
315 info->size = 0x00400000; break; /* => 4 MB */
316
317 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B:
wdenk8bde7f72003-06-27 21:31:46 +0000318 info->flash_id += FLASH_AMDL323B;
wdenkaffae2b2002-08-17 09:36:01 +0000319 info->sector_count = 71;
320 info->size = 0x00400000; break; /* => 4 MB */
321
322 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV640U:
wdenk8bde7f72003-06-27 21:31:46 +0000323 info->flash_id += FLASH_AM640U;
wdenkaffae2b2002-08-17 09:36:01 +0000324 info->sector_count = 128;
325 info->size = 0x00800000; break; /* => 8 MB */
326
stroese0621f6f2004-12-16 18:43:13 +0000327#if !(defined(CONFIG_ADCIOP) || defined(CONFIG_DASA_SIM))
wdenkaffae2b2002-08-17 09:36:01 +0000328 case (CFG_FLASH_WORD_SIZE)SST_ID_xF800A:
329 info->flash_id += FLASH_SST800A;
330 info->sector_count = 16;
331 info->size = 0x00100000;
332 break; /* => 1 MB */
333
334 case (CFG_FLASH_WORD_SIZE)SST_ID_xF160A:
stroese0621f6f2004-12-16 18:43:13 +0000335 case (CFG_FLASH_WORD_SIZE)SST_ID_xF1601:
336 case (CFG_FLASH_WORD_SIZE)SST_ID_xF1602:
wdenkaffae2b2002-08-17 09:36:01 +0000337 info->flash_id += FLASH_SST160A;
338 info->sector_count = 32;
339 info->size = 0x00200000;
340 break; /* => 2 MB */
341
stroese0621f6f2004-12-16 18:43:13 +0000342 case (CFG_FLASH_WORD_SIZE)SST_ID_xF3201:
343 case (CFG_FLASH_WORD_SIZE)SST_ID_xF3202:
344 info->flash_id += FLASH_SST320;
345 info->sector_count = 64;
346 info->size = 0x00400000;
347 break; /* => 4 MB */
348
349 case (CFG_FLASH_WORD_SIZE)SST_ID_xF6401:
350 case (CFG_FLASH_WORD_SIZE)SST_ID_xF6402:
351 info->flash_id += FLASH_SST640;
352 info->sector_count = 128;
353 info->size = 0x00800000;
354 break; /* => 8 MB */
355#endif
356
wdenkaffae2b2002-08-17 09:36:01 +0000357 default:
358 info->flash_id = FLASH_UNKNOWN;
359 return (0); /* => no or unknown flash */
360
361 }
362
363 /* set up sector start address table */
wdenk8bde7f72003-06-27 21:31:46 +0000364 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
wdenkaffae2b2002-08-17 09:36:01 +0000365 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
366 for (i = 0; i < info->sector_count; i++)
367 info->start[i] = base + (i * 0x00010000);
wdenk8bde7f72003-06-27 21:31:46 +0000368 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
wdenkaffae2b2002-08-17 09:36:01 +0000369 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
370 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
371 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
372 /* set sector offsets for bottom boot block type */
373 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
374 info->start[i] = base;
375 base += 8 << 10;
376 }
377 while (i < info->sector_count) { /* 64k regular sectors */
378 info->start[i] = base;
379 base += 64 << 10;
380 ++i;
381 }
wdenk8bde7f72003-06-27 21:31:46 +0000382 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
wdenkaffae2b2002-08-17 09:36:01 +0000383 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
384 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
385 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
386 /* set sector offsets for top boot block type */
387 base += info->size;
388 i = info->sector_count;
389 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
390 base -= 8 << 10;
391 --i;
392 info->start[i] = base;
393 }
394 while (i > 0) { /* 64k regular sectors */
395 base -= 64 << 10;
396 --i;
397 info->start[i] = base;
398 }
wdenk8bde7f72003-06-27 21:31:46 +0000399 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000400 if (info->flash_id & FLASH_BTYPE) {
401 /* set sector offsets for bottom boot block type */
402 info->start[0] = base + 0x00000000;
403 info->start[1] = base + 0x00004000;
404 info->start[2] = base + 0x00006000;
405 info->start[3] = base + 0x00008000;
406 for (i = 4; i < info->sector_count; i++) {
407 info->start[i] = base + (i * 0x00010000) - 0x00030000;
408 }
409 } else {
410 /* set sector offsets for top boot block type */
411 i = info->sector_count - 1;
412 info->start[i--] = base + info->size - 0x00004000;
413 info->start[i--] = base + info->size - 0x00006000;
414 info->start[i--] = base + info->size - 0x00008000;
415 for (; i >= 0; i--) {
416 info->start[i] = base + i * 0x00010000;
417 }
418 }
419 }
420
421 /* check for protected sectors */
422 for (i = 0; i < info->sector_count; i++) {
423 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
424 /* D0 = 1 if protected */
425 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
stroese0621f6f2004-12-16 18:43:13 +0000426 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
wdenk8bde7f72003-06-27 21:31:46 +0000427 info->protect[i] = 0;
428 else
429 info->protect[i] = addr2[CFG_FLASH_READ2] & 1;
wdenkaffae2b2002-08-17 09:36:01 +0000430 }
431
432 /*
433 * Prevent writes to uninitialized FLASH.
434 */
435 if (info->flash_id != FLASH_UNKNOWN) {
436 addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0];
437 *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
438 }
439
440 return (info->size);
441}
442
443
444/*-----------------------------------------------------------------------
445 */
446
447int flash_erase (flash_info_t *info, int s_first, int s_last)
448{
449 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
450 volatile CFG_FLASH_WORD_SIZE *addr2;
451 int flag, prot, sect, l_sect;
452 ulong start, now, last;
wdenk8bde7f72003-06-27 21:31:46 +0000453 int i;
wdenkaffae2b2002-08-17 09:36:01 +0000454
455 if ((s_first < 0) || (s_first > s_last)) {
456 if (info->flash_id == FLASH_UNKNOWN) {
457 printf ("- missing\n");
458 } else {
459 printf ("- no sectors to erase\n");
460 }
461 return 1;
462 }
463
464 if (info->flash_id == FLASH_UNKNOWN) {
465 printf ("Can't erase unknown flash type - aborted\n");
466 return 1;
467 }
468
469 prot = 0;
470 for (sect=s_first; sect<=s_last; ++sect) {
471 if (info->protect[sect]) {
472 prot++;
473 }
474 }
475
476 if (prot) {
477 printf ("- Warning: %d protected sectors will not be erased!\n",
478 prot);
479 } else {
480 printf ("\n");
481 }
482
483 l_sect = -1;
484
485 /* Disable interrupts which might cause a timeout here */
486 flag = disable_interrupts();
487
488 /* Start erase on unprotected sectors */
489 for (sect = s_first; sect<=s_last; sect++) {
490 if (info->protect[sect] == 0) { /* not protected */
491 addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
wdenk8bde7f72003-06-27 21:31:46 +0000492 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
493 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
494 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
495 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
496 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
497 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
498 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00500050; /* block erase */
499 for (i=0; i<50; i++)
500 udelay(1000); /* wait 1 ms */
501 } else {
502 if (sect == s_first) {
503 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
504 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
505 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
506 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
507 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
508 }
509 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
510 }
wdenkaffae2b2002-08-17 09:36:01 +0000511 l_sect = sect;
512 }
513 }
514
515 /* re-enable interrupts if necessary */
516 if (flag)
517 enable_interrupts();
518
519 /* wait at least 80us - let's wait 1 ms */
520 udelay (1000);
521
522 /*
523 * We wait for the last triggered sector
524 */
525 if (l_sect < 0)
526 goto DONE;
527
528 start = get_timer (0);
529 last = start;
530 addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
531 while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
532 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
533 printf ("Timeout\n");
534 return 1;
535 }
536 /* show that we're waiting */
537 if ((now - last) > 1000) { /* every second */
538 putc ('.');
539 last = now;
540 }
541 }
542
543DONE:
544 /* reset to read mode */
545 addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
546 addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
547
548 printf (" done\n");
549 return 0;
550}
551
552/*-----------------------------------------------------------------------
553 * Copy memory to flash, returns:
554 * 0 - OK
555 * 1 - write timeout
556 * 2 - Flash not erased
557 */
558
559int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
560{
561 ulong cp, wp, data;
562 int i, l, rc;
563
564 wp = (addr & ~3); /* get lower word aligned address */
565
566 /*
567 * handle unaligned start bytes
568 */
569 if ((l = addr - wp) != 0) {
570 data = 0;
571 for (i=0, cp=wp; i<l; ++i, ++cp) {
572 data = (data << 8) | (*(uchar *)cp);
573 }
574 for (; i<4 && cnt>0; ++i) {
575 data = (data << 8) | *src++;
576 --cnt;
577 ++cp;
578 }
579 for (; cnt==0 && i<4; ++i, ++cp) {
580 data = (data << 8) | (*(uchar *)cp);
581 }
582
583 if ((rc = write_word(info, wp, data)) != 0) {
584 return (rc);
585 }
586 wp += 4;
587 }
588
589 /*
590 * handle word aligned part
591 */
592 while (cnt >= 4) {
593 data = 0;
594 for (i=0; i<4; ++i) {
595 data = (data << 8) | *src++;
596 }
597 if ((rc = write_word(info, wp, data)) != 0) {
598 return (rc);
599 }
600 wp += 4;
601 cnt -= 4;
602 }
603
604 if (cnt == 0) {
605 return (0);
606 }
607
608 /*
609 * handle unaligned tail bytes
610 */
611 data = 0;
612 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
613 data = (data << 8) | *src++;
614 --cnt;
615 }
616 for (; i<4; ++i, ++cp) {
617 data = (data << 8) | (*(uchar *)cp);
618 }
619
620 return (write_word(info, wp, data));
621}
622
623/*-----------------------------------------------------------------------
624 * Write a word to Flash, returns:
625 * 0 - OK
626 * 1 - write timeout
627 * 2 - Flash not erased
628 */
629static int write_word (flash_info_t *info, ulong dest, ulong data)
630{
wdenk8bde7f72003-06-27 21:31:46 +0000631 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
632 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
633 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
wdenkaffae2b2002-08-17 09:36:01 +0000634 ulong start;
635 int flag;
wdenk8bde7f72003-06-27 21:31:46 +0000636 int i;
wdenkaffae2b2002-08-17 09:36:01 +0000637
638 /* Check if Flash is (sufficiently) erased */
stroese0621f6f2004-12-16 18:43:13 +0000639 if ((*((vu_long *)dest) & data) != data) {
wdenkaffae2b2002-08-17 09:36:01 +0000640 return (2);
641 }
stroese0621f6f2004-12-16 18:43:13 +0000642
wdenkaffae2b2002-08-17 09:36:01 +0000643 /* Disable interrupts which might cause a timeout here */
644 flag = disable_interrupts();
645
wdenk8bde7f72003-06-27 21:31:46 +0000646 for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++)
647 {
648 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
649 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
650 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
wdenkaffae2b2002-08-17 09:36:01 +0000651
wdenk8bde7f72003-06-27 21:31:46 +0000652 dest2[i] = data2[i];
wdenkaffae2b2002-08-17 09:36:01 +0000653
wdenk8bde7f72003-06-27 21:31:46 +0000654 /* re-enable interrupts if necessary */
655 if (flag)
656 enable_interrupts();
wdenkaffae2b2002-08-17 09:36:01 +0000657
wdenk8bde7f72003-06-27 21:31:46 +0000658 /* data polling for D7 */
659 start = get_timer (0);
660 while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
661 (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) {
662 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
663 return (1);
664 }
665 }
666 }
wdenkaffae2b2002-08-17 09:36:01 +0000667
668 return (0);
669}
670
671/*-----------------------------------------------------------------------
672 */