blob: 70014407c6ef632134c339d2015c52452ea22438 [file] [log] [blame]
Stefan Roesea4c8d132006-06-02 16:18:04 +02001/*
2 * (C) Copyright 2006
3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
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 <asm/processor.h>
26
27#ifndef CFG_FLASH_READ0
28#define CFG_FLASH_READ0 0x0000 /* 0 is standard */
29#define CFG_FLASH_READ1 0x0001 /* 1 is standard */
30#define CFG_FLASH_READ2 0x0002 /* 2 is standard */
31#endif
32
33flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
34
35/*
36 * Functions
37 */
38static int write_word(flash_info_t *info, ulong dest, ulong data);
39static ulong flash_get_size(vu_long *addr, flash_info_t *info);
40
41unsigned long flash_init(void)
42{
43 unsigned long size_b0, size_b1;
44 int i;
45 unsigned long base_b0, base_b1;
46
47 /* Init: no FLASHes known */
48 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
49 flash_info[i].flash_id = FLASH_UNKNOWN;
50 }
51
52 /* Static FLASH Bank configuration here - FIXME XXX */
53
54 base_b0 = FLASH_BASE0_PRELIM;
55 size_b0 = flash_get_size ((vu_long *) base_b0, &flash_info[0]);
56
57 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
58 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
59 size_b0, size_b0 << 20);
60 }
61
62 base_b1 = FLASH_BASE1_PRELIM;
63 size_b1 = flash_get_size ((vu_long *) base_b1, &flash_info[1]);
64
65 return (size_b0 + size_b1);
66}
67
68void flash_print_info(flash_info_t *info)
69{
70 int i;
71 int k;
72 int size;
73 int erased;
74 volatile unsigned long *flash;
75
76 if (info->flash_id == FLASH_UNKNOWN) {
77 printf ("missing or unknown FLASH type\n");
78 return;
79 }
80
81 switch (info->flash_id & FLASH_VENDMASK) {
82 case FLASH_MAN_AMD: printf ("AMD "); break;
83 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
84 case FLASH_MAN_SST: printf ("SST "); break;
85 case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break;
Heiko Schocher566a4942007-06-22 19:11:54 +020086 case FLASH_MAN_MX: printf ("MXIC "); break;
Stefan Roesea4c8d132006-06-02 16:18:04 +020087 default: printf ("Unknown Vendor "); break;
88 }
89
90 switch (info->flash_id & FLASH_TYPEMASK) {
91 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
92 break;
93 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
94 break;
95 case FLASH_AM040: printf ("AM29LV040B (4 Mbit, uniform sector size)\n");
96 break;
97 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
98 break;
99 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
100 break;
101 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
102 break;
103 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
104 break;
105 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
106 break;
107 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
108 break;
109 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
110 break;
111 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
112 break;
113 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
114 break;
115 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
116 break;
117 case FLASH_SST020: printf ("SST39LF/VF020 (2 Mbit, uniform sector size)\n");
118 break;
119 case FLASH_SST040: printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
120 break;
121 default: printf ("Unknown Chip Type\n");
122 break;
123 }
124
125 printf (" Size: %ld MB in %d Sectors\n",
126 info->size >> 20, info->sector_count);
127
128 printf (" Sector Start Addresses:");
129 for (i=0; i<info->sector_count; ++i) {
130#ifdef CFG_FLASH_EMPTY_INFO
131 /*
132 * Check if whole sector is erased
133 */
134 if (i != (info->sector_count-1))
135 size = info->start[i+1] - info->start[i];
136 else
137 size = info->start[0] + info->size - info->start[i];
138 erased = 1;
139 flash = (volatile unsigned long *)info->start[i];
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200140 size = size >> 2; /* divide by 4 for longword access */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200141 for (k=0; k<size; k++) {
142 if (*flash++ != 0xffffffff) {
143 erased = 0;
144 break;
145 }
146 }
147
148 if ((i % 5) == 0)
149 printf ("\n ");
150 /* print empty and read-only info */
151 printf (" %08lX%s%s",
152 info->start[i],
153 erased ? " E" : " ",
154 info->protect[i] ? "RO " : " ");
155#else
156 if ((i % 5) == 0)
157 printf ("\n ");
158 printf (" %08lX%s",
159 info->start[i],
160 info->protect[i] ? " (RO)" : " ");
161#endif
162
163 }
164 printf ("\n");
165 return;
166}
167
168/*
169 * The following code cannot be run from FLASH!
170 */
171static ulong flash_get_size(vu_long *addr, flash_info_t *info)
172{
173 short i;
174 short n;
175 volatile CFG_FLASH_WORD_SIZE value;
176 ulong base = (ulong)addr;
177 volatile CFG_FLASH_WORD_SIZE *addr2 = (volatile CFG_FLASH_WORD_SIZE *)addr;
178
179 /* Write auto select command: read Manufacturer ID */
180 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
181 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
182 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090;
183
184 value = addr2[CFG_FLASH_READ0];
185
186 switch (value) {
187 case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT:
188 info->flash_id = FLASH_MAN_AMD;
189 break;
190 case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT:
191 info->flash_id = FLASH_MAN_FUJ;
192 break;
193 case (CFG_FLASH_WORD_SIZE)SST_MANUFACT:
194 info->flash_id = FLASH_MAN_SST;
195 break;
196 case (CFG_FLASH_WORD_SIZE)EXCEL_MANUFACT:
197 info->flash_id = FLASH_MAN_EXCEL;
198 break;
Heiko Schocher566a4942007-06-22 19:11:54 +0200199 case (CFG_FLASH_WORD_SIZE)MX_MANUFACT:
200 info->flash_id = FLASH_MAN_MX;
201 break;
Stefan Roesea4c8d132006-06-02 16:18:04 +0200202 default:
203 info->flash_id = FLASH_UNKNOWN;
204 info->sector_count = 0;
205 info->size = 0;
206 return (0); /* no or unknown flash */
207 }
208
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200209 value = addr2[CFG_FLASH_READ1]; /* device ID */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200210
211 switch (value) {
212 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T:
213 info->flash_id += FLASH_AM400T;
214 info->sector_count = 11;
215 info->size = 0x00080000;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200216 break; /* => 0.5 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200217
218 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B:
219 info->flash_id += FLASH_AM400B;
220 info->sector_count = 11;
221 info->size = 0x00080000;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200222 break; /* => 0.5 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200223
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200224 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV040B:
225 info->flash_id += FLASH_AM040;
226 info->sector_count = 8;
227 info->size = 0x0080000; /* => 0.5 MB */
228 break;
Stefan Roesea4c8d132006-06-02 16:18:04 +0200229
230 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T:
231 info->flash_id += FLASH_AM800T;
232 info->sector_count = 19;
233 info->size = 0x00100000;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200234 break; /* => 1 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200235
236 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B:
237 info->flash_id += FLASH_AM800B;
238 info->sector_count = 19;
239 info->size = 0x00100000;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200240 break; /* => 1 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200241
242 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T:
243 info->flash_id += FLASH_AM160T;
244 info->sector_count = 35;
245 info->size = 0x00200000;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200246 break; /* => 2 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200247
248 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B:
249 info->flash_id += FLASH_AM160B;
250 info->sector_count = 35;
251 info->size = 0x00200000;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200252 break; /* => 2 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200253
254 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T:
255 info->flash_id += FLASH_AM320T;
256 info->sector_count = 71;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200257 info->size = 0x00400000;
258 break; /* => 4 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200259
260 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B:
261 info->flash_id += FLASH_AM320B;
262 info->sector_count = 71;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200263 info->size = 0x00400000;
264 break; /* => 4 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200265
266 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T:
267 info->flash_id += FLASH_AMDL322T;
268 info->sector_count = 71;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200269 info->size = 0x00400000;
270 break; /* => 4 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200271
272 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B:
273 info->flash_id += FLASH_AMDL322B;
274 info->sector_count = 71;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200275 info->size = 0x00400000;
276 break; /* => 4 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200277
278 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T:
279 info->flash_id += FLASH_AMDL323T;
280 info->sector_count = 71;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200281 info->size = 0x00400000;
282 break; /* => 4 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200283
284 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B:
285 info->flash_id += FLASH_AMDL323B;
286 info->sector_count = 71;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200287 info->size = 0x00400000;
288 break; /* => 4 MB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200289
290 case (CFG_FLASH_WORD_SIZE)SST_ID_xF020:
291 info->flash_id += FLASH_SST020;
292 info->sector_count = 64;
293 info->size = 0x00040000;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200294 break; /* => 256 kB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200295
296 case (CFG_FLASH_WORD_SIZE)SST_ID_xF040:
297 info->flash_id += FLASH_SST040;
298 info->sector_count = 128;
299 info->size = 0x00080000;
Wolfgang Denk2fd9eb52006-06-02 17:04:21 +0200300 break; /* => 512 kB */
Stefan Roesea4c8d132006-06-02 16:18:04 +0200301
302 default:
303 info->flash_id = FLASH_UNKNOWN;
304 return (0); /* => no or unknown flash */
305
306 }
307
308 /* set up sector start address table */
309 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
310 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
311 for (i = 0; i < info->sector_count; i++)
312 info->start[i] = base + (i * 0x00001000);
313 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
314 for (i = 0; i < info->sector_count; i++)
315 info->start[i] = base + (i * 0x00010000);
316 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
317 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
318 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
319 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
320 /* set sector offsets for bottom boot block type */
321 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
322 info->start[i] = base;
323 base += 8 << 10;
324 }
325 while (i < info->sector_count) { /* 64k regular sectors */
326 info->start[i] = base;
327 base += 64 << 10;
328 ++i;
329 }
330 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
331 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
332 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
333 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
334 /* set sector offsets for top boot block type */
335 base += info->size;
336 i = info->sector_count;
337 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
338 base -= 8 << 10;
339 --i;
340 info->start[i] = base;
341 }
342 while (i > 0) { /* 64k regular sectors */
343 base -= 64 << 10;
344 --i;
345 info->start[i] = base;
346 }
347 } else {
348 if (info->flash_id & FLASH_BTYPE) {
349 /* set sector offsets for bottom boot block type */
350 info->start[0] = base + 0x00000000;
351 info->start[1] = base + 0x00004000;
352 info->start[2] = base + 0x00006000;
353 info->start[3] = base + 0x00008000;
354 for (i = 4; i < info->sector_count; i++) {
355 info->start[i] = base + (i * 0x00010000) - 0x00030000;
356 }
357 } else {
358 /* set sector offsets for top boot block type */
359 i = info->sector_count - 1;
360 info->start[i--] = base + info->size - 0x00004000;
361 info->start[i--] = base + info->size - 0x00006000;
362 info->start[i--] = base + info->size - 0x00008000;
363 for (; i >= 0; i--) {
364 info->start[i] = base + i * 0x00010000;
365 }
366 }
367 }
368
369 /* check for protected sectors */
370 for (i = 0; i < info->sector_count; i++) {
371 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
372 /* D0 = 1 if protected */
373 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
374 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
375 info->protect[i] = 0;
376 else
377 info->protect[i] = addr2[CFG_FLASH_READ2] & 1;
378 }
379
380 /*
381 * Prevent writes to uninitialized FLASH.
382 */
383 if (info->flash_id != FLASH_UNKNOWN) {
384 addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0];
385 *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
386 }
387
388 return (info->size);
389}
390
391
392int flash_erase(flash_info_t *info, int s_first, int s_last)
393{
394 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
395 volatile CFG_FLASH_WORD_SIZE *addr2;
396 int flag, prot, sect, l_sect;
397 ulong start, now, last;
398
399 if ((s_first < 0) || (s_first > s_last)) {
400 if (info->flash_id == FLASH_UNKNOWN)
401 printf ("- missing\n");
402 else
403 printf ("- no sectors to erase\n");
404 return 1;
405 }
406
407 if (info->flash_id == FLASH_UNKNOWN) {
408 printf ("Can't erase unknown flash type - aborted\n");
409 return 1;
410 }
411
412 prot = 0;
413 for (sect=s_first; sect<=s_last; ++sect)
414 if (info->protect[sect])
415 prot++;
416
417 if (prot)
418 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
419 else
420 printf ("\n");
421
422 l_sect = -1;
423
424 /* Disable interrupts which might cause a timeout here */
425 flag = disable_interrupts();
426
427 /* Start erase on unprotected sectors */
428 for (sect = s_first; sect<=s_last; sect++) {
429 if (info->protect[sect] == 0) { /* not protected */
430 addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
431 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
432 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
433 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
434 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
435 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
436 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
437 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
438
439 /* re-enable interrupts if necessary */
440 if (flag) {
441 enable_interrupts();
442 flag = 0;
443 }
444
445 /* data polling for D7 */
446 start = get_timer (0);
447 while ((addr2[0] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
448 (CFG_FLASH_WORD_SIZE)0x00800080) {
449 if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
450 return (1);
451 }
452 } else {
453 if (sect == s_first) {
454 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
455 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
456 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
457 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
458 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
459 }
460 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
461 }
462 l_sect = sect;
463 }
464 }
465
466 /* re-enable interrupts if necessary */
467 if (flag)
468 enable_interrupts();
469
470 /* wait at least 80us - let's wait 1 ms */
471 udelay (1000);
472
473 /*
474 * We wait for the last triggered sector
475 */
476 if (l_sect < 0)
477 goto DONE;
478
479 start = get_timer (0);
480 last = start;
481 addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
482 while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
483 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
484 printf ("Timeout\n");
485 return 1;
486 }
487 /* show that we're waiting */
488 if ((now - last) > 1000) { /* every second */
489 putc ('.');
490 last = now;
491 }
492 }
493
494DONE:
495 /* reset to read mode */
496 addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
497 addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
498
499 printf (" done\n");
500 return 0;
501}
502
503/*
504 * Copy memory to flash, returns:
505 * 0 - OK
506 * 1 - write timeout
507 * 2 - Flash not erased
508 */
509int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
510{
511 ulong cp, wp, data;
512 int i, l, rc;
513
514 wp = (addr & ~3); /* get lower word aligned address */
515
516 /*
517 * handle unaligned start bytes
518 */
519 if ((l = addr - wp) != 0) {
520 data = 0;
521 for (i=0, cp=wp; i<l; ++i, ++cp) {
522 data = (data << 8) | (*(uchar *)cp);
523 }
524 for (; i<4 && cnt>0; ++i) {
525 data = (data << 8) | *src++;
526 --cnt;
527 ++cp;
528 }
529 for (; cnt==0 && i<4; ++i, ++cp) {
530 data = (data << 8) | (*(uchar *)cp);
531 }
532
533 if ((rc = write_word(info, wp, data)) != 0) {
534 return (rc);
535 }
536 wp += 4;
537 }
538
539 /*
540 * handle word aligned part
541 */
542 while (cnt >= 4) {
543 data = 0;
544 for (i=0; i<4; ++i)
545 data = (data << 8) | *src++;
546 if ((rc = write_word(info, wp, data)) != 0)
547 return (rc);
548 wp += 4;
549 cnt -= 4;
550 }
551
552 if (cnt == 0)
553 return (0);
554
555 /*
556 * handle unaligned tail bytes
557 */
558 data = 0;
559 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
560 data = (data << 8) | *src++;
561 --cnt;
562 }
563 for (; i<4; ++i, ++cp)
564 data = (data << 8) | (*(uchar *)cp);
565
566 return (write_word(info, wp, data));
567}
568
569/*
570 * Write a word to Flash, returns:
571 * 0 - OK
572 * 1 - write timeout
573 * 2 - Flash not erased
574 */
575static int write_word(flash_info_t *info, ulong dest, ulong data)
576{
577 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
578 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
579 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
580 ulong start;
581 int flag;
582 int i;
583
584 /* Check if Flash is (sufficiently) erased */
585 if ((*((vu_long *)dest) & data) != data)
586 return (2);
587
588 /* Disable interrupts which might cause a timeout here */
589 flag = disable_interrupts();
590
591 for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++) {
592 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
593 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
594 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
595
596 dest2[i] = data2[i];
597
598 /* re-enable interrupts if necessary */
599 if (flag)
600 enable_interrupts();
601
602 /* data polling for D7 */
603 start = get_timer (0);
604 while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
605 (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) {
606 if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
607 return (1);
608 }
609 }
610
611 return (0);
612}