]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/dave/common/flash.c
rename CFG_ macros to CONFIG_SYS
[people/ms/u-boot.git] / board / dave / common / flash.c
1 /*
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 <asm/processor.h>
26
27 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
28
29 /*-----------------------------------------------------------------------
30 * Functions
31 */
32 static int write_word (flash_info_t *info, ulong dest, ulong data);
33
34 /*-----------------------------------------------------------------------
35 */
36 static void flash_get_offsets (ulong base, flash_info_t *info)
37 {
38 int i;
39 short n;
40
41 /* set up sector start address table */
42 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
43 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
44 for (i = 0; i < info->sector_count; i++)
45 info->start[i] = base + (i * 0x00010000);
46 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
47 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
48 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
49 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
50 /* set sector offsets for bottom boot block type */
51 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
52 info->start[i] = base;
53 base += 8 << 10;
54 }
55 while (i < info->sector_count) { /* 64k regular sectors */
56 info->start[i] = base;
57 base += 64 << 10;
58 ++i;
59 }
60 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
61 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
62 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
63 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
64 /* set sector offsets for top boot block type */
65 base += info->size;
66 i = info->sector_count;
67 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
68 base -= 8 << 10;
69 --i;
70 info->start[i] = base;
71 }
72 while (i > 0) { /* 64k regular sectors */
73 base -= 64 << 10;
74 --i;
75 info->start[i] = base;
76 }
77 } else {
78 if (info->flash_id & FLASH_BTYPE) {
79 /* set sector offsets for bottom boot block type */
80 info->start[0] = base + 0x00000000;
81 info->start[1] = base + 0x00004000;
82 info->start[2] = base + 0x00006000;
83 info->start[3] = base + 0x00008000;
84 for (i = 4; i < info->sector_count; i++) {
85 info->start[i] = base + (i * 0x00010000) - 0x00030000;
86 }
87 } else {
88 /* set sector offsets for top boot block type */
89 i = info->sector_count - 1;
90 info->start[i--] = base + info->size - 0x00004000;
91 info->start[i--] = base + info->size - 0x00006000;
92 info->start[i--] = base + info->size - 0x00008000;
93 for (; i >= 0; i--) {
94 info->start[i] = base + i * 0x00010000;
95 }
96 }
97 }
98 }
99
100 /*-----------------------------------------------------------------------
101 */
102 void flash_print_info (flash_info_t *info)
103 {
104 int i;
105 int k;
106 int size;
107 int erased;
108 volatile unsigned long *flash;
109
110 if (info->flash_id == FLASH_UNKNOWN) {
111 printf ("missing or unknown FLASH type\n");
112 return;
113 }
114
115 switch (info->flash_id & FLASH_VENDMASK) {
116 case FLASH_MAN_AMD: printf ("AMD "); break;
117 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
118 case FLASH_MAN_SST: printf ("SST "); break;
119 case FLASH_MAN_STM: printf ("ST "); break;
120 default: printf ("Unknown Vendor "); break;
121 }
122
123 switch (info->flash_id & FLASH_TYPEMASK) {
124 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
125 break;
126 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
127 break;
128 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
129 break;
130 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
131 break;
132 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
133 break;
134 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
135 break;
136 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
137 break;
138 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
139 break;
140 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
141 break;
142 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
143 break;
144 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
145 break;
146 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
147 break;
148 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
149 break;
150 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
151 break;
152 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
153 break;
154 case FLASH_STMW320DT: printf ("M29W320DT (32 M, top sector)\n");
155 break;
156 default: printf ("Unknown Chip Type\n");
157 break;
158 }
159
160 printf (" Size: %ld MB in %d Sectors\n",
161 info->size >> 20, info->sector_count);
162
163 printf (" Sector Start Addresses:");
164 for (i=0; i<info->sector_count; ++i) {
165 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
166 /*
167 * Check if whole sector is erased
168 */
169 if (i != (info->sector_count-1))
170 size = info->start[i+1] - info->start[i];
171 else
172 size = info->start[0] + info->size - info->start[i];
173 erased = 1;
174 flash = (volatile unsigned long *)info->start[i];
175 size = size >> 2; /* divide by 4 for longword access */
176 for (k=0; k<size; k++)
177 {
178 if (*flash++ != 0xffffffff)
179 {
180 erased = 0;
181 break;
182 }
183 }
184
185 if ((i % 5) == 0)
186 printf ("\n ");
187 /* print empty and read-only info */
188 printf (" %08lX%s%s",
189 info->start[i],
190 erased ? " E" : " ",
191 info->protect[i] ? "RO " : " ");
192 #else
193 if ((i % 5) == 0)
194 printf ("\n ");
195 printf (" %08lX%s",
196 info->start[i],
197 info->protect[i] ? " (RO)" : " ");
198 #endif
199
200 }
201 printf ("\n");
202 return;
203 }
204
205 /*-----------------------------------------------------------------------
206 */
207
208
209 /*-----------------------------------------------------------------------
210 */
211
212 /*
213 * The following code cannot be run from FLASH!
214 */
215 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
216 {
217 short i;
218 short n;
219 CONFIG_SYS_FLASH_WORD_SIZE value;
220 ulong base = (ulong)addr;
221 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
222
223 debug("[%s, %d] Entering ...\n", __FUNCTION__, __LINE__);
224
225 /* Write auto select command: read Manufacturer ID */
226 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
227 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
228 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
229
230 value = addr2[CONFIG_SYS_FLASH_READ0];
231
232 switch (value) {
233 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
234 info->flash_id = FLASH_MAN_AMD;
235 break;
236 case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
237 info->flash_id = FLASH_MAN_FUJ;
238 break;
239 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
240 info->flash_id = FLASH_MAN_SST;
241 break;
242 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_MANUFACT:
243 info->flash_id = FLASH_MAN_STM;
244 break;
245 default:
246 info->flash_id = FLASH_UNKNOWN;
247 info->sector_count = 0;
248 info->size = 0;
249 return (0); /* no or unknown flash */
250 }
251
252 value = addr2[CONFIG_SYS_FLASH_READ1]; /* device ID */
253
254 switch (value) {
255 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
256 info->flash_id += FLASH_AM400T;
257 info->sector_count = 11;
258 info->size = 0x00080000;
259 break; /* => 0.5 MB */
260
261 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
262 info->flash_id += FLASH_AM400B;
263 info->sector_count = 11;
264 info->size = 0x00080000;
265 break; /* => 0.5 MB */
266
267 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
268 info->flash_id += FLASH_AM800T;
269 info->sector_count = 19;
270 info->size = 0x00100000;
271 break; /* => 1 MB */
272
273 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
274 info->flash_id += FLASH_AM800B;
275 info->sector_count = 19;
276 info->size = 0x00100000;
277 break; /* => 1 MB */
278
279 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
280 info->flash_id += FLASH_AM160T;
281 info->sector_count = 35;
282 info->size = 0x00200000;
283 break; /* => 2 MB */
284
285 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
286 info->flash_id += FLASH_AM160B;
287 info->sector_count = 35;
288 info->size = 0x00200000;
289 break; /* => 2 MB */
290
291 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
292 info->flash_id += FLASH_STMW320DT;
293 info->sector_count = 67;
294 info->size = 0x00400000; break; /* => 4 MB */
295
296 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
297 info->flash_id += FLASH_AM320T;
298 info->sector_count = 71;
299 info->size = 0x00400000; break; /* => 4 MB */
300
301 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
302 info->flash_id += FLASH_AM320B;
303 info->sector_count = 71;
304 info->size = 0x00400000; break; /* => 4 MB */
305
306 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
307 info->flash_id += FLASH_AMDL322T;
308 info->sector_count = 71;
309 info->size = 0x00400000; break; /* => 4 MB */
310
311 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
312 info->flash_id += FLASH_AMDL322B;
313 info->sector_count = 71;
314 info->size = 0x00400000; break; /* => 4 MB */
315
316 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
317 info->flash_id += FLASH_AMDL323T;
318 info->sector_count = 71;
319 info->size = 0x00400000; break; /* => 4 MB */
320
321 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
322 info->flash_id += FLASH_AMDL323B;
323 info->sector_count = 71;
324 info->size = 0x00400000; break; /* => 4 MB */
325
326 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV640U:
327 info->flash_id += FLASH_AM640U;
328 info->sector_count = 128;
329 info->size = 0x00800000; break; /* => 8 MB */
330
331 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF800A:
332 info->flash_id += FLASH_SST800A;
333 info->sector_count = 16;
334 info->size = 0x00100000;
335 break; /* => 1 MB */
336
337 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF160A:
338 info->flash_id += FLASH_SST160A;
339 info->sector_count = 32;
340 info->size = 0x00200000;
341 break; /* => 2 MB */
342
343 default:
344 info->flash_id = FLASH_UNKNOWN;
345 return (0); /* => no or unknown flash */
346
347 }
348
349 /* set up sector start address table */
350 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
351 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
352 for (i = 0; i < info->sector_count; i++)
353 info->start[i] = base + (i * 0x00010000);
354 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
355 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
356 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
357 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
358 /* set sector offsets for bottom boot block type */
359 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
360 info->start[i] = base;
361 base += 8 << 10;
362 }
363 while (i < info->sector_count) { /* 64k regular sectors */
364 info->start[i] = base;
365 base += 64 << 10;
366 ++i;
367 }
368 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
369 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
370 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
371 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
372 /* set sector offsets for top boot block type */
373 base += info->size;
374 i = info->sector_count;
375 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
376 base -= 8 << 10;
377 --i;
378 info->start[i] = base;
379 }
380 while (i > 0) { /* 64k regular sectors */
381 base -= 64 << 10;
382 --i;
383 info->start[i] = base;
384 }
385 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
386 /* set sector offsets for top boot block type */
387 base += info->size;
388 i = info->sector_count;
389 /* 1 x 16k boot sector */
390 base -= 16 << 10;
391 --i;
392 info->start[i] = base;
393 /* 2 x 8k boot sectors */
394 for (n=0; n<2; ++n) {
395 base -= 8 << 10;
396 --i;
397 info->start[i] = base;
398 }
399 /* 1 x 32k boot sector */
400 base -= 32 << 10;
401 --i;
402 info->start[i] = base;
403
404 while (i > 0) { /* 64k regular sectors */
405 base -= 64 << 10;
406 --i;
407 info->start[i] = base;
408 }
409 } else {
410 if (info->flash_id & FLASH_BTYPE) {
411 /* set sector offsets for bottom boot block type */
412 info->start[0] = base + 0x00000000;
413 info->start[1] = base + 0x00004000;
414 info->start[2] = base + 0x00006000;
415 info->start[3] = base + 0x00008000;
416 for (i = 4; i < info->sector_count; i++) {
417 info->start[i] = base + (i * 0x00010000) - 0x00030000;
418 }
419 } else {
420 /* set sector offsets for top boot block type */
421 i = info->sector_count - 1;
422 info->start[i--] = base + info->size - 0x00004000;
423 info->start[i--] = base + info->size - 0x00006000;
424 info->start[i--] = base + info->size - 0x00008000;
425 for (; i >= 0; i--) {
426 info->start[i] = base + i * 0x00010000;
427 }
428 }
429 }
430
431 /* check for protected sectors */
432 for (i = 0; i < info->sector_count; i++) {
433 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
434 /* D0 = 1 if protected */
435 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
436 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
437 info->protect[i] = 0;
438 else
439 info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
440 }
441
442 /*
443 * Prevent writes to uninitialized FLASH.
444 */
445 if (info->flash_id != FLASH_UNKNOWN) {
446 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
447 *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
448 }
449
450 return (info->size);
451 }
452
453
454 /*-----------------------------------------------------------------------
455 */
456
457 int flash_erase (flash_info_t *info, int s_first, int s_last)
458 {
459 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
460 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
461 int flag, prot, sect, l_sect;
462 ulong start, now, last;
463 int i;
464
465 if ((s_first < 0) || (s_first > s_last)) {
466 if (info->flash_id == FLASH_UNKNOWN) {
467 printf ("- missing\n");
468 } else {
469 printf ("- no sectors to erase\n");
470 }
471 return 1;
472 }
473
474 if (info->flash_id == FLASH_UNKNOWN) {
475 printf ("Can't erase unknown flash type - aborted\n");
476 return 1;
477 }
478
479 prot = 0;
480 for (sect=s_first; sect<=s_last; ++sect) {
481 if (info->protect[sect]) {
482 prot++;
483 }
484 }
485
486 if (prot) {
487 printf ("- Warning: %d protected sectors will not be erased!\n",
488 prot);
489 } else {
490 printf ("\n");
491 }
492
493 l_sect = -1;
494
495 /* Disable interrupts which might cause a timeout here */
496 flag = disable_interrupts();
497
498 /* Start erase on unprotected sectors */
499 for (sect = s_first; sect<=s_last; sect++) {
500 if (info->protect[sect] == 0) { /* not protected */
501 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
502 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
503 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
504 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
505 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
506 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
507 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
508 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00500050; /* block erase */
509 for (i=0; i<50; i++)
510 udelay(1000); /* wait 1 ms */
511 } else {
512 if (sect == s_first) {
513 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
514 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
515 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
516 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
517 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
518 }
519 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030; /* sector erase */
520 }
521 l_sect = sect;
522 }
523 }
524
525 /* re-enable interrupts if necessary */
526 if (flag)
527 enable_interrupts();
528
529 /* wait at least 80us - let's wait 1 ms */
530 udelay (1000);
531
532 /*
533 * We wait for the last triggered sector
534 */
535 if (l_sect < 0)
536 goto DONE;
537
538 start = get_timer (0);
539 last = start;
540 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
541 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
542 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
543 printf ("Timeout\n");
544 return 1;
545 }
546 /* show that we're waiting */
547 if ((now - last) > 1000) { /* every second */
548 putc ('.');
549 last = now;
550 }
551 }
552
553 DONE:
554 /* reset to read mode */
555 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
556 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
557
558 printf (" done\n");
559 return 0;
560 }
561
562 /*-----------------------------------------------------------------------
563 * Copy memory to flash, returns:
564 * 0 - OK
565 * 1 - write timeout
566 * 2 - Flash not erased
567 */
568
569 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
570 {
571 ulong cp, wp, data;
572 int i, l, rc;
573
574 wp = (addr & ~3); /* get lower word aligned address */
575
576 /*
577 * handle unaligned start bytes
578 */
579 if ((l = addr - wp) != 0) {
580 data = 0;
581 for (i=0, cp=wp; i<l; ++i, ++cp) {
582 #ifdef CONFIG_B2
583 data = data | ((*(uchar *)cp)<<(8*i));
584 #else
585 data = (data << 8) | (*(uchar *)cp);
586 #endif
587 }
588 for (; i<4 && cnt>0; ++i) {
589 #ifdef CONFIG_B2
590 data = data | ((*src++)<<(8*i));
591 #else
592 data = (data << 8) | *src++;
593 #endif
594 --cnt;
595 ++cp;
596 }
597 for (; cnt==0 && i<4; ++i, ++cp) {
598 #ifdef CONFIG_B2
599 data = data | ((*(uchar *)cp)<<(8*i));
600 #else
601 data = (data << 8) | (*(uchar *)cp);
602 #endif
603 }
604
605 if ((rc = write_word(info, wp, data)) != 0) {
606 return (rc);
607 }
608 wp += 4;
609 }
610
611 /*
612 * handle word aligned part
613 */
614 while (cnt >= 4) {
615 data = 0;
616 #ifdef CONFIG_B2
617 data = (*(ulong*)src);
618 src += 4;
619 #else
620 for (i=0; i<4; ++i) {
621 data = (data << 8) | *src++;
622 }
623 #endif
624 if ((rc = write_word(info, wp, data)) != 0) {
625 return (rc);
626 }
627 wp += 4;
628 cnt -= 4;
629 }
630
631 if (cnt == 0) {
632 return (0);
633 }
634
635 /*
636 * handle unaligned tail bytes
637 */
638 data = 0;
639 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
640 #ifdef CONFIG_B2
641 data = data | ((*src++)<<(8*i));
642 #else
643 data = (data << 8) | *src++;
644 #endif
645 --cnt;
646 }
647 for (; i<4; ++i, ++cp) {
648 #ifdef CONFIG_B2
649 data = data | ((*(uchar *)cp)<<(8*i));
650 #else
651 data = (data << 8) | (*(uchar *)cp);
652 #endif
653 }
654
655 return (write_word(info, wp, data));
656 }
657
658 /*-----------------------------------------------------------------------
659 * Write a word to Flash, returns:
660 * 0 - OK
661 * 1 - write timeout
662 * 2 - Flash not erased
663 */
664 static int write_word (flash_info_t *info, ulong dest, ulong data)
665 {
666 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
667 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
668 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)&data;
669 ulong start;
670 int flag;
671 int i;
672
673 /* Check if Flash is (sufficiently) erased */
674 if ((*((volatile ulong *)dest) & data) != data) {
675 return (2);
676 }
677 /* Disable interrupts which might cause a timeout here */
678 flag = disable_interrupts();
679
680 for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++)
681 {
682 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
683 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
684 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
685
686 dest2[i] = data2[i];
687
688 /* re-enable interrupts if necessary */
689 if (flag)
690 enable_interrupts();
691
692 /* data polling for D7 */
693 start = get_timer (0);
694 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
695 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
696 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
697 return (1);
698 }
699 }
700 }
701
702 return (0);
703 }
704
705 /*-----------------------------------------------------------------------
706 */