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