]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/amcc/taihu/flash.c
Merge branch 'master' of git://git.denx.de/u-boot-mpc83xx
[karo-tx-uboot.git] / board / amcc / taihu / flash.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@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 /*
25  * Modified 4/5/2001
26  * Wait for completion of each sector erase command issued
27  * 4/5/2001
28  * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
29  */
30
31 #include <common.h>
32 #include <asm/ppc4xx.h>
33 #include <asm/processor.h>
34
35 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];    /* info for FLASH chips        */
36
37 #undef DEBUG
38 #ifdef DEBUG
39 #define DEBUGF(x...) printf(x)
40 #else
41 #define DEBUGF(x...)
42 #endif                          /* DEBUG */
43
44 #define CONFIG_SYS_FLASH_CHAR_SIZE unsigned char
45 #define CONFIG_SYS_FLASH_CHAR_ADDR0 (0x0aaa)
46 #define CONFIG_SYS_FLASH_CHAR_ADDR1 (0x0555)
47 /*-----------------------------------------------------------------------
48  * Functions
49  */
50 static ulong flash_get_size(vu_long * addr, flash_info_t * info);
51 static void flash_get_offsets(ulong base, flash_info_t * info);
52 static int write_word(flash_info_t * info, ulong dest, ulong data);
53 #ifdef FLASH_BASE1_PRELIM
54 static int write_word_1(flash_info_t * info, ulong dest, ulong data);
55 static int write_word_2(flash_info_t * info, ulong dest, ulong data);
56 static int flash_erase_1(flash_info_t * info, int s_first, int s_last);
57 static int flash_erase_2(flash_info_t * info, int s_first, int s_last);
58 static ulong flash_get_size_1(vu_long * addr, flash_info_t * info);
59 static ulong flash_get_size_2(vu_long * addr, flash_info_t * info);
60 #endif
61
62 unsigned long flash_init(void)
63 {
64         unsigned long size_b0, size_b1=0;
65         int i;
66
67         /* Init: no FLASHes known */
68         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
69                 flash_info[i].flash_id = FLASH_UNKNOWN;
70         }
71
72         /* Static FLASH Bank configuration here - FIXME XXX */
73
74         size_b0 =
75             flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
76
77         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
78                 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
79                        size_b0, size_b0 << 20);
80         }
81
82         if (size_b0) {
83                 /* Setup offsets */
84                 flash_get_offsets(FLASH_BASE0_PRELIM, &flash_info[0]);
85                 /* Monitor protection ON by default */
86                 (void)flash_protect(FLAG_PROTECT_SET,
87                                     CONFIG_SYS_MONITOR_BASE,
88                                     CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
89                                     &flash_info[0]);
90 #ifdef CONFIG_ENV_IS_IN_FLASH
91                 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
92                                     CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
93                                     &flash_info[0]);
94                 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR_REDUND,
95                                     CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
96                                     &flash_info[0]);
97 #endif
98                 /* Also protect sector containing initial power-up instruction */
99                 /* (flash_protect() checks address range - other call ignored) */
100                 (void)flash_protect(FLAG_PROTECT_SET,
101                                     0xFFFFFFFC, 0xFFFFFFFF, &flash_info[0]);
102
103                 flash_info[0].size = size_b0;
104         }
105 #ifdef FLASH_BASE1_PRELIM
106         size_b1 =
107             flash_get_size((vu_long *) FLASH_BASE1_PRELIM, &flash_info[1])*2;
108
109         if (flash_info[1].flash_id == FLASH_UNKNOWN) {
110                 printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
111                        size_b1, size_b1 << 20);
112         }
113
114         if (size_b1) {
115                 /* Setup offsets */
116                 flash_get_offsets(FLASH_BASE1_PRELIM, &flash_info[1]);
117                 flash_info[1].size = size_b1;
118         }
119 #endif
120         return (size_b0 + size_b1);
121 }
122
123 static void flash_get_offsets(ulong base, flash_info_t * info)
124 {
125         int i;
126
127         /* set up sector start address table */
128         if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
129             (info->flash_id == FLASH_AM040)) {
130                 for (i = 0; i < info->sector_count; i++)
131                         info->start[i] = base + (i * 0x00010000);
132         } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
133                 for (i = 0; i < info->sector_count; i++) {
134                         info->start[i] = base + (i * 0x00010000*2);
135                 }
136         } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N ) {
137                 for (i = 0; i < info->sector_count; i++) {
138                         info->start[i] = base + (i * 0x00020000*2);
139                 }
140         } else {
141                 if (info->flash_id & FLASH_BTYPE) {
142                         /* set sector offsets for bottom boot block type        */
143                         info->start[0] = base + 0x00000000;
144                         info->start[1] = base + 0x00004000;
145                         info->start[2] = base + 0x00006000;
146                         info->start[3] = base + 0x00008000;
147                         for (i = 4; i < info->sector_count; i++) {
148                                 info->start[i] =
149                                     base + (i * 0x00010000) - 0x00030000;
150                         }
151                 } else {
152                         /* set sector offsets for top boot block type           */
153                         i = info->sector_count - 1;
154                         info->start[i--] = base + info->size - 0x00004000;
155                         info->start[i--] = base + info->size - 0x00006000;
156                         info->start[i--] = base + info->size - 0x00008000;
157                         for (; i >= 0; i--) {
158                                 info->start[i] = base + i * 0x00010000;
159                         }
160                 }
161         }
162 }
163
164
165 void flash_print_info(flash_info_t * info)
166 {
167         int i;
168         int k;
169         int size;
170         int erased;
171         volatile unsigned long *flash;
172
173         if (info->flash_id == FLASH_UNKNOWN) {
174                 printf("missing or unknown FLASH type\n");
175                 return;
176         }
177
178         switch (info->flash_id & FLASH_VENDMASK) {
179         case FLASH_MAN_AMD:
180                 printf("AMD ");
181                 break;
182         case FLASH_MAN_STM:
183                 printf("STM ");
184                 break;
185         case FLASH_MAN_FUJ:
186                 printf("FUJITSU ");
187                 break;
188         case FLASH_MAN_SST:
189                 printf("SST ");
190                 break;
191         default:
192                 printf("Unknown Vendor ");
193                 break;
194         }
195
196         switch (info->flash_id & FLASH_TYPEMASK) {
197         case FLASH_AM040:
198                 printf("AM29F040 (512 Kbit, uniform sector size)\n");
199                 break;
200         case FLASH_AM400B:
201                 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
202                 break;
203         case FLASH_AM400T:
204                 printf("AM29LV400T (4 Mbit, top boot sector)\n");
205                 break;
206         case FLASH_AM800B:
207                 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
208                 break;
209         case FLASH_AM800T:
210                 printf("AM29LV800T (8 Mbit, top boot sector)\n");
211                 break;
212         case FLASH_AMD016:
213                 printf("AM29F016D (16 Mbit, uniform sector size)\n");
214                 break;
215         case FLASH_AM160B:
216                 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
217                 break;
218         case FLASH_AM160T:
219                 printf("AM29LV160T (16 Mbit, top boot sector)\n");
220                 break;
221         case FLASH_AM320B:
222                 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
223                 break;
224         case FLASH_AM320T:
225                 printf("AM29LV320T (32 Mbit, top boot sector)\n");
226                 break;
227         case FLASH_AM033C:
228                 printf("AM29LV033C (32 Mbit, top boot sector)\n");
229                 break;
230         case FLASH_AMLV128U:
231                 printf("AM29LV128U (128 Mbit * 2, top boot sector)\n");
232                 break;
233         case FLASH_SST800A:
234                 printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
235                 break;
236         case FLASH_SST160A:
237                 printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
238                 break;
239         case FLASH_STMW320DT:
240                 printf ("M29W320DT (32 M, top sector)\n");
241                 break;
242         case FLASH_S29GL128N:
243                 printf ("S29GL128N (256 Mbit, uniform sector size)\n");
244                 break;
245         default:
246                 printf("Unknown Chip Type\n");
247                 break;
248         }
249
250         printf("  Size: %ld KB in %d Sectors\n",
251                info->size >> 10, info->sector_count);
252
253         printf("  Sector Start Addresses:");
254         for (i = 0; i < info->sector_count; ++i) {
255                 /*
256                  * Check if whole sector is erased
257                  */
258                 if (i != (info->sector_count - 1))
259                         size = info->start[i + 1] - info->start[i];
260                 else
261                         size = info->start[0] + info->size - info->start[i];
262                 erased = 1;
263                 flash = (volatile unsigned long *)info->start[i];
264                 size = size >> 2;       /* divide by 4 for longword access */
265                 for (k = 0; k < size; k++) {
266                         if (*flash++ != 0xffffffff) {
267                                 erased = 0;
268                                 break;
269                         }
270                 }
271
272                 if ((i % 5) == 0)
273                         printf("\n   ");
274                 printf(" %08lX%s%s",
275                        info->start[i],
276                        erased ? " E" : "  ", info->protect[i] ? "RO " : "   ");
277         }
278         printf("\n");
279         return;
280 }
281
282
283 /*
284  * The following code cannot be run from FLASH!
285  */
286 #ifdef FLASH_BASE1_PRELIM
287 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
288 {
289         if ((ulong)addr == FLASH_BASE1_PRELIM) {
290                 return flash_get_size_2(addr, info);
291         } else {
292                 return flash_get_size_1(addr, info);
293         }
294 }
295
296 static ulong flash_get_size_1(vu_long * addr, flash_info_t * info)
297 #else
298 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
299 #endif
300 {
301         short i;
302         CONFIG_SYS_FLASH_WORD_SIZE value;
303         ulong base = (ulong) addr;
304         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
305
306         DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
307
308         /* Write auto select command: read Manufacturer ID */
309         addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
310         addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
311         addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
312         udelay(1000);
313
314         value = addr2[0];
315         DEBUGF("FLASH MANUFACT: %x\n", value);
316
317         switch (value) {
318         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
319                 info->flash_id = FLASH_MAN_AMD;
320                 break;
321         case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
322                 info->flash_id = FLASH_MAN_FUJ;
323                 break;
324         case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
325                 info->flash_id = FLASH_MAN_SST;
326                 break;
327         case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
328                 info->flash_id = FLASH_MAN_STM;
329                 break;
330         default:
331                 info->flash_id = FLASH_UNKNOWN;
332                 info->sector_count = 0;
333                 info->size = 0;
334                 return 0;       /* no or unknown flash  */
335         }
336
337         value = addr2[1];       /* device ID            */
338         DEBUGF("\nFLASH DEVICEID: %x\n", value);
339
340         switch (value) {
341         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV040B:
342                 info->flash_id += FLASH_AM040;
343                 info->sector_count = 8;
344                 info->size = 0x0080000; /* => 512 ko */
345                 break;
346
347         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F040B:
348                 info->flash_id += FLASH_AM040;
349                 info->sector_count = 8;
350                 info->size = 0x0080000; /* => 512 ko */
351                 break;
352
353         case (CONFIG_SYS_FLASH_WORD_SIZE) STM_ID_M29W040B:
354                 info->flash_id += FLASH_AM040;
355                 info->sector_count = 8;
356                 info->size = 0x0080000; /* => 512 ko */
357                 break;
358
359         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F016D:
360                 info->flash_id += FLASH_AMD016;
361                 info->sector_count = 32;
362                 info->size = 0x00200000;
363                 break;          /* => 2 MB              */
364
365         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV033C:
366                 info->flash_id += FLASH_AMDLV033C;
367                 info->sector_count = 64;
368                 info->size = 0x00400000;
369                 break;          /* => 4 MB              */
370
371         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400T:
372                 info->flash_id += FLASH_AM400T;
373                 info->sector_count = 11;
374                 info->size = 0x00080000;
375                 break;          /* => 0.5 MB            */
376
377         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400B:
378                 info->flash_id += FLASH_AM400B;
379                 info->sector_count = 11;
380                 info->size = 0x00080000;
381                 break;          /* => 0.5 MB            */
382
383         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800T:
384                 info->flash_id += FLASH_AM800T;
385                 info->sector_count = 19;
386                 info->size = 0x00100000;
387                 break;          /* => 1 MB              */
388
389         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800B:
390                 info->flash_id += FLASH_AM800B;
391                 info->sector_count = 19;
392                 info->size = 0x00100000;
393                 break;          /* => 1 MB              */
394
395         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160T:
396                 info->flash_id += FLASH_AM160T;
397                 info->sector_count = 35;
398                 info->size = 0x00200000;
399                 break;          /* => 2 MB              */
400
401         case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160B:
402                 info->flash_id += FLASH_AM160B;
403                 info->sector_count = 35;
404                 info->size = 0x00200000;
405                 break;          /* => 2 MB              */
406         default:
407                 info->flash_id = FLASH_UNKNOWN;
408                 return 0;       /* => no or unknown flash */
409         }
410
411         /* set up sector start address table */
412         if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
413             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
414             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
415                 for (i = 0; i < info->sector_count; i++)
416                         info->start[i] = base + (i * 0x00010000);
417         }
418         else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
419                 for (i = 0; i < info->sector_count; i++)
420                         info->start[i] = base + (i * 0x00010000 * 2);
421         } else {
422                 if (info->flash_id & FLASH_BTYPE) {
423                         /* set sector offsets for bottom boot block type        */
424                         info->start[0] = base + 0x00000000;
425                         info->start[1] = base + 0x00004000;
426                         info->start[2] = base + 0x00006000;
427                         info->start[3] = base + 0x00008000;
428                         for (i = 4; i < info->sector_count; i++) {
429                                 info->start[i] =
430                                     base + (i * 0x00010000) - 0x00030000;
431                         }
432                 } else {
433                         /* set sector offsets for top boot block type           */
434                         i = info->sector_count - 1;
435                         info->start[i--] = base + info->size - 0x00004000;
436                         info->start[i--] = base + info->size - 0x00006000;
437                         info->start[i--] = base + info->size - 0x00008000;
438                         for (; i >= 0; i--) {
439                                 info->start[i] = base + i * 0x00010000;
440                         }
441                 }
442         }
443
444         /* check for protected sectors */
445         for (i = 0; i < info->sector_count; i++) {
446                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
447                 /* D0 = 1 if protected */
448                 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
449
450                 /* For AMD29033C flash we need to resend the command of *
451                  * reading flash protection for upper 8 Mb of flash     */
452                 if (i == 32) {
453                         addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
454                         addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
455                         addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
456                 }
457
458                 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
459                         info->protect[i] = 0;
460                 else
461                         info->protect[i] = addr2[2] & 1;
462         }
463
464         /* issue bank reset to return to read mode */
465         addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
466
467         return info->size;
468 }
469
470 static int wait_for_DQ7_1(flash_info_t * info, int sect)
471 {
472         ulong start, now, last;
473         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
474             (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
475
476         start = get_timer(0);
477         last = start;
478         while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
479                (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
480                 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
481                         printf("Timeout\n");
482                         return -1;
483                 }
484                 /* show that we're waiting */
485                 if ((now - last) > 1000) {      /* every second */
486                         putc('.');
487                         last = now;
488                 }
489         }
490         return 0;
491 }
492
493 #ifdef FLASH_BASE1_PRELIM
494 int flash_erase(flash_info_t * info, int s_first, int s_last)
495 {
496         if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
497             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
498             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) ||
499             ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N) ||
500             ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
501                 return flash_erase_2(info, s_first, s_last);
502         } else {
503                 return flash_erase_1(info, s_first, s_last);
504         }
505 }
506
507 static int flash_erase_1(flash_info_t * info, int s_first, int s_last)
508 #else
509 int flash_erase(flash_info_t * info, int s_first, int s_last)
510 #endif
511 {
512         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
513         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
514         int flag, prot, sect;
515         int i;
516
517         if ((s_first < 0) || (s_first > s_last)) {
518                 if (info->flash_id == FLASH_UNKNOWN) {
519                         printf("- missing\n");
520                 } else {
521                         printf("- no sectors to erase\n");
522                 }
523                 return 1;
524         }
525
526         if (info->flash_id == FLASH_UNKNOWN) {
527                 printf("Can't erase unknown flash type - aborted\n");
528                 return 1;
529         }
530
531         prot = 0;
532         for (sect = s_first; sect <= s_last; ++sect) {
533                 if (info->protect[sect]) {
534                         prot++;
535                 }
536         }
537
538         if (prot) {
539                 printf("- Warning: %d protected sectors will not be erased!\n",
540                        prot);
541         } else {
542                 printf("\n");
543         }
544
545         /* Disable interrupts which might cause a timeout here */
546         flag = disable_interrupts();
547
548         /* Start erase on unprotected sectors */
549         for (sect = s_first; sect <= s_last; sect++) {
550                 if (info->protect[sect] == 0) { /* not protected */
551                         addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
552
553                         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
554                                 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
555                                 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
556                                 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
557                                 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
558                                 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
559                                 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050;     /* block erase */
560                                 for (i = 0; i < 50; i++)
561                                         udelay(1000);   /* wait 1 ms */
562                         } else {
563                                 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
564                                 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
565                                 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
566                                 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
567                                 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
568                                 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030;     /* sector erase */
569                         }
570                         /*
571                          * Wait for each sector to complete, it's more
572                          * reliable.  According to AMD Spec, you must
573                          * issue all erase commands within a specified
574                          * timeout.  This has been seen to fail, especially
575                          * if printf()s are included (for debug)!!
576                          */
577                         wait_for_DQ7_1(info, sect);
578                 }
579         }
580
581         /* re-enable interrupts if necessary */
582         if (flag)
583                 enable_interrupts();
584
585         /* wait at least 80us - let's wait 1 ms */
586         udelay(1000);
587
588         /* reset to read mode */
589         addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
590         addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;      /* reset bank */
591
592         printf(" done\n");
593         return 0;
594 }
595
596 /*-----------------------------------------------------------------------
597  * Copy memory to flash, returns:
598  * 0 - OK
599  * 1 - write timeout
600  * 2 - Flash not erased
601  */
602 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
603 {
604         ulong cp, wp, data;
605         int i, l, rc;
606
607         wp = (addr & ~3);       /* get lower word aligned address */
608
609         /*
610          * handle unaligned start bytes
611          */
612         if ((l = addr - wp) != 0) {
613                 data = 0;
614                 for (i = 0, cp = wp; i < l; ++i, ++cp) {
615                         data = (data << 8) | (*(uchar *) cp);
616                 }
617                 for (; i < 4 && cnt > 0; ++i) {
618                         data = (data << 8) | *src++;
619                         --cnt;
620                         ++cp;
621                 }
622                 for (; cnt == 0 && i < 4; ++i, ++cp) {
623                         data = (data << 8) | (*(uchar *) cp);
624                 }
625
626                 if ((rc = write_word(info, wp, data)) != 0) {
627                         return rc;
628                 }
629                 wp += 4;
630         }
631
632         /*
633          * handle word aligned part
634          */
635         while (cnt >= 4) {
636                 data = 0;
637                 for (i = 0; i < 4; ++i) {
638                         data = (data << 8) | *src++;
639                 }
640                 if ((rc = write_word(info, wp, data)) != 0) {
641                         return rc;
642                 }
643                 wp += 4;
644                 cnt -= 4;
645         }
646
647         if (cnt == 0) {
648                 return 0;
649         }
650
651         /*
652          * handle unaligned tail bytes
653          */
654         data = 0;
655         for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
656                 data = (data << 8) | *src++;
657                 --cnt;
658         }
659         for (; i < 4; ++i, ++cp) {
660                 data = (data << 8) | (*(uchar *) cp);
661         }
662
663         return (write_word(info, wp, data));
664 }
665
666 /*-----------------------------------------------------------------------
667  * Copy memory to flash, returns:
668  * 0 - OK
669  * 1 - write timeout
670  * 2 - Flash not erased
671  */
672 #ifdef FLASH_BASE1_PRELIM
673 static int write_word(flash_info_t * info, ulong dest, ulong data)
674 {
675         if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
676             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
677             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) ||
678             ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N) ||
679             ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
680                 return write_word_2(info, dest, data);
681         } else {
682                 return write_word_1(info, dest, data);
683         }
684 }
685
686 static int write_word_1(flash_info_t * info, ulong dest, ulong data)
687 #else
688 static int write_word(flash_info_t * info, ulong dest, ulong data)
689 #endif
690 {
691         ulong *data_ptr = &data;
692         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
693         volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
694         volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
695         ulong start;
696         int i;
697
698         /* Check if Flash is (sufficiently) erased */
699         if ((*((vu_long *)dest) & data) != data) {
700                 return 2;
701         }
702
703         for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
704                 int flag;
705
706                 /* Disable interrupts which might cause a timeout here */
707                 flag = disable_interrupts();
708
709                 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
710                 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
711                 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
712
713                 dest2[i] = data2[i];
714
715                 /* re-enable interrupts if necessary */
716                 if (flag)
717                         enable_interrupts();
718
719                 /* data polling for D7 */
720                 start = get_timer(0);
721                 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
722                        (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
723
724                         if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
725                                 return 1;
726                         }
727                 }
728         }
729
730         return 0;
731 }
732
733 #ifdef FLASH_BASE1_PRELIM
734
735 /*
736  * The following code cannot be run from FLASH!
737  */
738 static ulong flash_get_size_2(vu_long * addr, flash_info_t * info)
739 {
740         short i;
741         CONFIG_SYS_FLASH_CHAR_SIZE value;
742         ulong base = (ulong) addr;
743         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
744
745         DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
746
747         /* Write auto select command: read Manufacturer ID */
748         addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
749         addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
750         addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
751         udelay(1000);
752
753         value = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0];
754         DEBUGF("FLASH MANUFACT: %x\n", value);
755
756         switch (value) {
757         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_MANUFACT:
758                 info->flash_id = FLASH_MAN_AMD;
759                 break;
760         case (CONFIG_SYS_FLASH_CHAR_SIZE) FUJ_MANUFACT:
761                 info->flash_id = FLASH_MAN_FUJ;
762                 break;
763         case (CONFIG_SYS_FLASH_CHAR_SIZE) SST_MANUFACT:
764                 info->flash_id = FLASH_MAN_SST;
765                 break;
766         case (CONFIG_SYS_FLASH_CHAR_SIZE) STM_MANUFACT:
767                 info->flash_id = FLASH_MAN_STM;
768                 break;
769         default:
770                 info->flash_id = FLASH_UNKNOWN;
771                 info->sector_count = 0;
772                 info->size = 0;
773                 return 0;               /* no or unknown flash */
774         }
775
776         value = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[2];   /* device ID */
777         DEBUGF("\nFLASH DEVICEID: %x\n", value);
778
779         switch (value) {
780         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV040B:
781                 info->flash_id += FLASH_AM040;
782                 info->sector_count = 8;
783                 info->size = 0x0080000; /* => 512 ko */
784                 break;
785
786         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_F040B:
787                 info->flash_id += FLASH_AM040;
788                 info->sector_count = 8;
789                 info->size = 0x0080000; /* => 512 ko */
790                 break;
791
792         case (CONFIG_SYS_FLASH_CHAR_SIZE) STM_ID_M29W040B:
793                 info->flash_id += FLASH_AM040;
794                 info->sector_count = 8;
795                 info->size = 0x0080000; /* => 512 ko */
796                 break;
797
798         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_F016D:
799                 info->flash_id += FLASH_AMD016;
800                 info->sector_count = 32;
801                 info->size = 0x00200000;
802                 break;                  /* => 2 MB */
803
804         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV033C:
805                 info->flash_id += FLASH_AMDLV033C;
806                 info->sector_count = 64;
807                 info->size = 0x00400000;
808                 break;                  /* => 4 MB */
809
810         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV400T:
811                 info->flash_id += FLASH_AM400T;
812                 info->sector_count = 11;
813                 info->size = 0x00080000;
814                 break;                  /* => 0.5 MB */
815
816         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV400B:
817                 info->flash_id += FLASH_AM400B;
818                 info->sector_count = 11;
819                 info->size = 0x00080000;
820                 break;                  /* => 0.5 MB */
821
822         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV800T:
823                 info->flash_id += FLASH_AM800T;
824                 info->sector_count = 19;
825                 info->size = 0x00100000;
826                 break;                  /* => 1 MB */
827
828         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV800B:
829                 info->flash_id += FLASH_AM800B;
830                 info->sector_count = 19;
831                 info->size = 0x00100000;
832                 break;                  /* => 1 MB */
833
834         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV160T:
835                 info->flash_id += FLASH_AM160T;
836                 info->sector_count = 35;
837                 info->size = 0x00200000;
838                 break;                  /* => 2 MB */
839
840         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV160B:
841                 info->flash_id += FLASH_AM160B;
842                 info->sector_count = 35;
843                 info->size = 0x00200000;
844                 break;                  /* => 2 MB */
845         case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_MIRROR:
846                 if ((CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1c] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_LV128U_2
847                                 && (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1e] ==  (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_LV128U_3) {
848                         info->flash_id += FLASH_AMLV128U;
849                         info->sector_count = 256;
850                         info->size = 0x01000000;
851                 } else if ((CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1c] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_GL128N_2
852                                 && (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1e] ==  (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_GL128N_3 ) {
853                         info->flash_id += FLASH_S29GL128N;
854                         info->sector_count = 128;
855                         info->size = 0x01000000;
856                 }
857                 else
858                         info->flash_id = FLASH_UNKNOWN;
859                 break;                  /* => 2 MB */
860
861         default:
862                 info->flash_id = FLASH_UNKNOWN;
863                 return 0;               /* => no or unknown flash */
864         }
865
866         /* set up sector start address table */
867         if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
868             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
869             ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
870                 for (i = 0; i < info->sector_count; i++)
871                         info->start[i] = base + (i * 0x00010000);
872         } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
873                 for (i = 0; i < info->sector_count; i++)
874                         info->start[i] = base + (i * 0x00010000);
875         } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N ) {
876                 for (i = 0; i < info->sector_count; i++)
877                         info->start[i] = base + (i * 0x00020000);
878         } else {
879                 if (info->flash_id & FLASH_BTYPE) {
880                         /* set sector offsets for bottom boot block type */
881                         info->start[0] = base + 0x00000000;
882                         info->start[1] = base + 0x00004000;
883                         info->start[2] = base + 0x00006000;
884                         info->start[3] = base + 0x00008000;
885                         for (i = 4; i < info->sector_count; i++) {
886                                 info->start[i] =
887                                     base + (i * 0x00010000) - 0x00030000;
888                         }
889                 } else {
890                         /* set sector offsets for top boot block type */
891                         i = info->sector_count - 1;
892                         info->start[i--] = base + info->size - 0x00004000;
893                         info->start[i--] = base + info->size - 0x00006000;
894                         info->start[i--] = base + info->size - 0x00008000;
895                         for (; i >= 0; i--) {
896                                 info->start[i] = base + i * 0x00010000;
897                         }
898                 }
899         }
900
901         /* check for protected sectors */
902         for (i = 0; i < info->sector_count; i++) {
903                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
904                 /* D0 = 1 if protected */
905                 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
906
907                 /* For AMD29033C flash we need to resend the command of *
908                  * reading flash protection for upper 8 Mb of flash     */
909                 if (i == 32) {
910                         addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
911                         addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
912                         addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
913                 }
914
915                 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
916                         info->protect[i] = 0;
917                 else
918                         info->protect[i] = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[4] & 1;
919         }
920
921         /* issue bank reset to return to read mode */
922         addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF0F0F0F0;
923         return info->size;
924 }
925
926 static int wait_for_DQ7_2(flash_info_t * info, int sect)
927 {
928         ulong start, now, last;
929         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
930             (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
931
932         start = get_timer(0);
933         last = start;
934         while (((CONFIG_SYS_FLASH_WORD_SIZE)addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) !=
935                (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) {
936                 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
937                         printf("Timeout\n");
938                         return -1;
939                 }
940                 /* show that we're waiting */
941                 if ((now - last) > 1000) { /* every second */
942                         putc('.');
943                         last = now;
944                 }
945         }
946         return 0;
947 }
948
949 static int flash_erase_2(flash_info_t * info, int s_first, int s_last)
950 {
951         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
952         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
953         int flag, prot, sect;
954         int i;
955
956         if ((s_first < 0) || (s_first > s_last)) {
957                 if (info->flash_id == FLASH_UNKNOWN) {
958                         printf("- missing\n");
959                 } else {
960                         printf("- no sectors to erase\n");
961                 }
962                 return 1;
963         }
964
965         if (info->flash_id == FLASH_UNKNOWN) {
966                 printf("Can't erase unknown flash type - aborted\n");
967                 return 1;
968         }
969
970         prot = 0;
971         for (sect = s_first; sect <= s_last; ++sect) {
972                 if (info->protect[sect]) {
973                         prot++;
974                 }
975         }
976
977         if (prot) {
978                 printf("- Warning: %d protected sectors will not be erased!\n",
979                        prot);
980         } else {
981                 printf("\n");
982         }
983
984         /* Disable interrupts which might cause a timeout here */
985         flag = disable_interrupts();
986
987         /* Start erase on unprotected sectors */
988         for (sect = s_first; sect <= s_last; sect++) {
989                 if (info->protect[sect] == 0) { /* not protected */
990                         addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
991
992                         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
993                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
994                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
995                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080;
996                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
997                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
998                                 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x50505050;     /* block erase */
999                                 for (i = 0; i < 50; i++)
1000                                         udelay(1000);   /* wait 1 ms */
1001                         } else {
1002                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
1003                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
1004                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080;
1005                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
1006                                 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
1007                                 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x30303030;     /* sector erase */
1008                         }
1009                         /*
1010                          * Wait for each sector to complete, it's more
1011                          * reliable.  According to AMD Spec, you must
1012                          * issue all erase commands within a specified
1013                          * timeout.  This has been seen to fail, especially
1014                          * if printf()s are included (for debug)!!
1015                          */
1016                         wait_for_DQ7_2(info, sect);
1017                 }
1018         }
1019
1020         /* re-enable interrupts if necessary */
1021         if (flag)
1022                 enable_interrupts();
1023
1024         /* wait at least 80us - let's wait 1 ms */
1025         udelay(1000);
1026
1027         /* reset to read mode */
1028         addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
1029         addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF0F0F0F0; /* reset bank */
1030
1031         printf(" done\n");
1032         return 0;
1033 }
1034
1035 static int write_word_2(flash_info_t * info, ulong dest, ulong data)
1036 {
1037         ulong *data_ptr = &data;
1038         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
1039         volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
1040         volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
1041         ulong start;
1042         int i;
1043
1044         /* Check if Flash is (sufficiently) erased */
1045         if ((*((vu_long *)dest) & data) != data) {
1046                 return 2;
1047         }
1048
1049         for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
1050                 int flag;
1051
1052                 /* Disable interrupts which might cause a timeout here */
1053                 flag = disable_interrupts();
1054
1055                 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
1056                 addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
1057                 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xA0A0A0A0;
1058
1059                 dest2[i] = data2[i];
1060
1061                 /* re-enable interrupts if necessary */
1062                 if (flag)
1063                         enable_interrupts();
1064
1065                 /* data polling for D7 */
1066                 start = get_timer(0);
1067                 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) !=
1068                        (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080)) {
1069
1070                         if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1071                                 return 1;
1072                         }
1073                 }
1074         }
1075
1076         return 0;
1077 }
1078
1079 #endif /* FLASH_BASE1_PRELIM */