]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - board/dave/common/flash.c
b0e411617f2efe3b8cb57969be4d390268cd3172
[karo-tx-uboot.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[CFG_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 CFG_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         CFG_FLASH_WORD_SIZE value;
220         ulong base = (ulong)addr;
221         volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)addr;
222
223         /* Write auto select command: read Manufacturer ID */
224         addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
225         addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
226         addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090;
227
228         value = addr2[CFG_FLASH_READ0];
229
230         switch (value) {
231         case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT:
232                 info->flash_id = FLASH_MAN_AMD;
233                 break;
234         case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT:
235                 info->flash_id = FLASH_MAN_FUJ;
236                 break;
237         case (CFG_FLASH_WORD_SIZE)SST_MANUFACT:
238                 info->flash_id = FLASH_MAN_SST;
239                 break;
240         case (CFG_FLASH_WORD_SIZE)STM_MANUFACT:
241                 info->flash_id = FLASH_MAN_STM;
242                 break;
243         default:
244                 info->flash_id = FLASH_UNKNOWN;
245                 info->sector_count = 0;
246                 info->size = 0;
247                 return (0);                     /* no or unknown flash  */
248         }
249
250         value = addr2[CFG_FLASH_READ1];         /* device ID            */
251
252         switch (value) {
253         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T:
254                 info->flash_id += FLASH_AM400T;
255                 info->sector_count = 11;
256                 info->size = 0x00080000;
257                 break;                          /* => 0.5 MB            */
258
259         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B:
260                 info->flash_id += FLASH_AM400B;
261                 info->sector_count = 11;
262                 info->size = 0x00080000;
263                 break;                          /* => 0.5 MB            */
264
265         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T:
266                 info->flash_id += FLASH_AM800T;
267                 info->sector_count = 19;
268                 info->size = 0x00100000;
269                 break;                          /* => 1 MB              */
270
271         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B:
272                 info->flash_id += FLASH_AM800B;
273                 info->sector_count = 19;
274                 info->size = 0x00100000;
275                 break;                          /* => 1 MB              */
276
277         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T:
278                 info->flash_id += FLASH_AM160T;
279                 info->sector_count = 35;
280                 info->size = 0x00200000;
281                 break;                          /* => 2 MB              */
282
283         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B:
284                 info->flash_id += FLASH_AM160B;
285                 info->sector_count = 35;
286                 info->size = 0x00200000;
287                 break;                          /* => 2 MB              */
288
289         case (CFG_FLASH_WORD_SIZE)STM_ID_29W320DT:
290                 info->flash_id += FLASH_STMW320DT;
291                 info->sector_count = 67;
292                 info->size = 0x00400000;  break;        /* => 4 MB      */
293
294         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T:
295                 info->flash_id += FLASH_AM320T;
296                 info->sector_count = 71;
297                 info->size = 0x00400000;  break;        /* => 4 MB      */
298
299         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B:
300                 info->flash_id += FLASH_AM320B;
301                 info->sector_count = 71;
302                 info->size = 0x00400000;  break;        /* => 4 MB      */
303
304         case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T:
305                 info->flash_id += FLASH_AMDL322T;
306                 info->sector_count = 71;
307                 info->size = 0x00400000;  break;        /* => 4 MB      */
308
309         case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B:
310                 info->flash_id += FLASH_AMDL322B;
311                 info->sector_count = 71;
312                 info->size = 0x00400000;  break;        /* => 4 MB      */
313
314         case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T:
315                 info->flash_id += FLASH_AMDL323T;
316                 info->sector_count = 71;
317                 info->size = 0x00400000;  break;        /* => 4 MB      */
318
319         case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B:
320                 info->flash_id += FLASH_AMDL323B;
321                 info->sector_count = 71;
322                 info->size = 0x00400000;  break;        /* => 4 MB      */
323
324         case (CFG_FLASH_WORD_SIZE)AMD_ID_LV640U:
325                 info->flash_id += FLASH_AM640U;
326                 info->sector_count = 128;
327                 info->size = 0x00800000;  break;        /* => 8 MB      */
328
329         case (CFG_FLASH_WORD_SIZE)SST_ID_xF800A:
330                 info->flash_id += FLASH_SST800A;
331                 info->sector_count = 16;
332                 info->size = 0x00100000;
333                 break;                          /* => 1 MB              */
334
335         case (CFG_FLASH_WORD_SIZE)SST_ID_xF160A:
336                 info->flash_id += FLASH_SST160A;
337                 info->sector_count = 32;
338                 info->size = 0x00200000;
339                 break;                          /* => 2 MB              */
340
341         default:
342                 info->flash_id = FLASH_UNKNOWN;
343                 return (0);                     /* => no or unknown flash */
344
345         }
346
347         /* set up sector start address table */
348         if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
349             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
350             for (i = 0; i < info->sector_count; i++)
351                 info->start[i] = base + (i * 0x00010000);
352         } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
353                    ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
354                    ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
355                    ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
356                 /* set sector offsets for bottom boot block type        */
357                 for (i=0; i<8; ++i) {           /*  8 x 8k boot sectors */
358                         info->start[i] = base;
359                         base += 8 << 10;
360                 }
361                 while (i < info->sector_count) {        /* 64k regular sectors  */
362                         info->start[i] = base;
363                         base += 64 << 10;
364                         ++i;
365                 }
366         } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
367                    ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
368                    ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
369                    ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
370                 /* set sector offsets for top boot block type           */
371                 base += info->size;
372                 i = info->sector_count;
373                 for (n=0; n<8; ++n) {           /*  8 x 8k boot sectors */
374                         base -= 8 << 10;
375                         --i;
376                         info->start[i] = base;
377                 }
378                 while (i > 0) {                 /* 64k regular sectors  */
379                         base -= 64 << 10;
380                         --i;
381                         info->start[i] = base;
382                 }
383         } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
384                 /* set sector offsets for top boot block type           */
385                 base += info->size;
386                 i = info->sector_count;
387                 /*  1 x 16k boot sector */
388                 base -= 16 << 10;
389                 --i;
390                 info->start[i] = base;
391                 /*  2 x 8k  boot sectors */
392                 for (n=0; n<2; ++n) {
393                         base -= 8 << 10;
394                         --i;
395                         info->start[i] = base;
396                 }
397                 /*  1 x 32k boot sector */
398                 base -= 32 << 10;
399                 --i;
400                 info->start[i] = base;
401
402                 while (i > 0) {                 /* 64k regular sectors  */
403                         base -= 64 << 10;
404                         --i;
405                         info->start[i] = base;
406                 }
407         } else {
408             if (info->flash_id & FLASH_BTYPE) {
409                 /* set sector offsets for bottom boot block type        */
410                 info->start[0] = base + 0x00000000;
411                 info->start[1] = base + 0x00004000;
412                 info->start[2] = base + 0x00006000;
413                 info->start[3] = base + 0x00008000;
414                 for (i = 4; i < info->sector_count; i++) {
415                         info->start[i] = base + (i * 0x00010000) - 0x00030000;
416                 }
417             } else {
418                 /* set sector offsets for top boot block type           */
419                 i = info->sector_count - 1;
420                 info->start[i--] = base + info->size - 0x00004000;
421                 info->start[i--] = base + info->size - 0x00006000;
422                 info->start[i--] = base + info->size - 0x00008000;
423                 for (; i >= 0; i--) {
424                         info->start[i] = base + i * 0x00010000;
425                 }
426             }
427         }
428
429         /* check for protected sectors */
430         for (i = 0; i < info->sector_count; i++) {
431                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
432                 /* D0 = 1 if protected */
433                 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
434                 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
435                   info->protect[i] = 0;
436                 else
437                   info->protect[i] = addr2[CFG_FLASH_READ2] & 1;
438         }
439
440         /*
441          * Prevent writes to uninitialized FLASH.
442          */
443         if (info->flash_id != FLASH_UNKNOWN) {
444                 addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0];
445                 *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0;       /* reset bank */
446         }
447
448         return (info->size);
449 }
450
451
452 /*-----------------------------------------------------------------------
453  */
454
455 int     flash_erase (flash_info_t *info, int s_first, int s_last)
456 {
457         volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
458         volatile CFG_FLASH_WORD_SIZE *addr2;
459         int flag, prot, sect, l_sect;
460         ulong start, now, last;
461         int i;
462
463         if ((s_first < 0) || (s_first > s_last)) {
464                 if (info->flash_id == FLASH_UNKNOWN) {
465                         printf ("- missing\n");
466                 } else {
467                         printf ("- no sectors to erase\n");
468                 }
469                 return 1;
470         }
471
472         if (info->flash_id == FLASH_UNKNOWN) {
473                 printf ("Can't erase unknown flash type - aborted\n");
474                 return 1;
475         }
476
477         prot = 0;
478         for (sect=s_first; sect<=s_last; ++sect) {
479                 if (info->protect[sect]) {
480                         prot++;
481                 }
482         }
483
484         if (prot) {
485                 printf ("- Warning: %d protected sectors will not be erased!\n",
486                         prot);
487         } else {
488                 printf ("\n");
489         }
490
491         l_sect = -1;
492
493         /* Disable interrupts which might cause a timeout here */
494         flag = disable_interrupts();
495
496         /* Start erase on unprotected sectors */
497         for (sect = s_first; sect<=s_last; sect++) {
498                 if (info->protect[sect] == 0) { /* not protected */
499                     addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
500                     if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
501                         addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
502                         addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
503                         addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
504                         addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
505                         addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
506                         addr2[0] = (CFG_FLASH_WORD_SIZE)0x00500050;  /* block erase */
507                         for (i=0; i<50; i++)
508                           udelay(1000);  /* wait 1 ms */
509                     } else {
510                         if (sect == s_first) {
511                             addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
512                             addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
513                             addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
514                             addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
515                             addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
516                         }
517                         addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030;  /* sector erase */
518                     }
519                     l_sect = sect;
520                 }
521         }
522
523         /* re-enable interrupts if necessary */
524         if (flag)
525                 enable_interrupts();
526
527         /* wait at least 80us - let's wait 1 ms */
528         udelay (1000);
529
530         /*
531          * We wait for the last triggered sector
532          */
533         if (l_sect < 0)
534                 goto DONE;
535
536         start = get_timer (0);
537         last  = start;
538         addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
539         while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
540                 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
541                         printf ("Timeout\n");
542                         return 1;
543                 }
544                 /* show that we're waiting */
545                 if ((now - last) > 1000) {      /* every second */
546                         putc ('.');
547                         last = now;
548                 }
549         }
550
551 DONE:
552         /* reset to read mode */
553         addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
554         addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0;      /* reset bank */
555
556         printf (" done\n");
557         return 0;
558 }
559
560 /*-----------------------------------------------------------------------
561  * Copy memory to flash, returns:
562  * 0 - OK
563  * 1 - write timeout
564  * 2 - Flash not erased
565  */
566
567 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
568 {
569         ulong cp, wp, data;
570         int i, l, rc;
571
572         wp = (addr & ~3);       /* get lower word aligned address */
573
574         /*
575          * handle unaligned start bytes
576          */
577         if ((l = addr - wp) != 0) {
578                 data = 0;
579                 for (i=0, cp=wp; i<l; ++i, ++cp) {
580 #ifdef CONFIG_B2
581                         data = data | ((*(uchar *)cp)<<(8*i));
582 #else
583                         data = (data << 8) | (*(uchar *)cp);
584 #endif
585                 }
586                 for (; i<4 && cnt>0; ++i) {
587 #ifdef CONFIG_B2
588                         data = data  | ((*src++)<<(8*i));
589 #else
590                         data = (data << 8) | *src++;
591 #endif
592                         --cnt;
593                         ++cp;
594                 }
595                 for (; cnt==0 && i<4; ++i, ++cp) {
596 #ifdef CONFIG_B2
597                         data = data | ((*(uchar *)cp)<<(8*i));
598 #else
599                         data = (data << 8) | (*(uchar *)cp);
600 #endif
601                 }
602
603                 if ((rc = write_word(info, wp, data)) != 0) {
604                         return (rc);
605                 }
606                 wp += 4;
607         }
608
609         /*
610          * handle word aligned part
611          */
612         while (cnt >= 4) {
613                 data = 0;
614 #ifdef CONFIG_B2
615                 data = (*(ulong*)src);
616                 src += 4;
617 #else
618                 for (i=0; i<4; ++i) {
619                         data = (data << 8) | *src++;
620                 }
621 #endif
622                 if ((rc = write_word(info, wp, data)) != 0) {
623                         return (rc);
624                 }
625                 wp  += 4;
626                 cnt -= 4;
627         }
628
629         if (cnt == 0) {
630                 return (0);
631         }
632
633         /*
634          * handle unaligned tail bytes
635          */
636         data = 0;
637         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
638 #ifdef CONFIG_B2
639                 data = data  | ((*src++)<<(8*i));
640 #else
641                 data = (data << 8) | *src++;
642 #endif
643                 --cnt;
644         }
645         for (; i<4; ++i, ++cp) {
646 #ifdef CONFIG_B2
647                 data = data | ((*(uchar *)cp)<<(8*i));
648 #else
649                 data = (data << 8) | (*(uchar *)cp);
650 #endif
651         }
652
653         return (write_word(info, wp, data));
654 }
655
656 /*-----------------------------------------------------------------------
657  * Write a word to Flash, returns:
658  * 0 - OK
659  * 1 - write timeout
660  * 2 - Flash not erased
661  */
662 static int write_word (flash_info_t *info, ulong dest, ulong data)
663 {
664         volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
665         volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
666         volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
667         ulong start;
668         int flag;
669         int i;
670
671         /* Check if Flash is (sufficiently) erased */
672         if ((*((volatile CFG_FLASH_WORD_SIZE *)dest) &
673              (CFG_FLASH_WORD_SIZE)data) != (CFG_FLASH_WORD_SIZE)data) {
674                 return (2);
675         }
676         /* Disable interrupts which might cause a timeout here */
677         flag = disable_interrupts();
678
679         for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++)
680           {
681             addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
682             addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
683             addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
684
685             dest2[i] = data2[i];
686
687             /* re-enable interrupts if necessary */
688             if (flag)
689               enable_interrupts();
690
691             /* data polling for D7 */
692             start = get_timer (0);
693             while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
694                    (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) {
695               if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
696                 return (1);
697               }
698             }
699           }
700
701         return (0);
702 }
703
704 /*-----------------------------------------------------------------------
705  */