Fixed tools/env utilities
[u-boot-openmoko/mini2440.git] / board / eric / flash.c
blobc08a760266129a6f4d23ce570150aec41acd83df
1 /*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
24 #include <common.h>
25 #include <ppc4xx.h>
26 #include <asm/processor.h>
28 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
31 #ifdef CFG_FLASH_16BIT
32 #define FLASH_WORD_SIZE unsigned short
33 #define FLASH_ID_MASK 0xFFFF
34 #else
35 #define FLASH_WORD_SIZE unsigned long
36 #define FLASH_ID_MASK 0xFFFFFFFF
37 #endif
39 /*-----------------------------------------------------------------------
40 * Functions
42 /* stolen from esteem192e/flash.c */
43 ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info);
45 #ifndef CFG_FLASH_16BIT
46 static int write_word (flash_info_t *info, ulong dest, ulong data);
47 #else
48 static int write_short (flash_info_t *info, ulong dest, ushort data);
49 #endif
50 static void flash_get_offsets (ulong base, flash_info_t *info);
53 /*-----------------------------------------------------------------------
56 unsigned long flash_init (void)
58 unsigned long size_b0, size_b1;
59 int i;
60 uint pbcr;
61 unsigned long base_b0, base_b1;
63 /* Init: no FLASHes known */
64 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
65 flash_info[i].flash_id = FLASH_UNKNOWN;
68 /* Static FLASH Bank configuration here - FIXME XXX */
70 size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE0_PRELIM, &flash_info[0]);
72 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
73 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
74 size_b0, size_b0<<20);
77 /* Only one bank */
78 if (CFG_MAX_FLASH_BANKS == 1)
80 /* Setup offsets */
81 flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
83 /* Monitor protection ON by default */
84 #if 0 /* sand: */
85 (void)flash_protect(FLAG_PROTECT_SET,
86 FLASH_BASE0_PRELIM-monitor_flash_len+size_b0,
87 FLASH_BASE0_PRELIM-1+size_b0,
88 &flash_info[0]);
89 #else
90 (void)flash_protect(FLAG_PROTECT_SET,
91 CFG_MONITOR_BASE,
92 CFG_MONITOR_BASE+monitor_flash_len-1,
93 &flash_info[0]);
94 #endif
95 size_b1 = 0 ;
96 flash_info[0].size = size_b0;
99 /* 2 banks */
100 else
102 size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE1_PRELIM, &flash_info[1]);
104 /* Re-do sizing to get full correct info */
106 if (size_b1)
108 mtdcr(ebccfga, pb0cr);
109 pbcr = mfdcr(ebccfgd);
110 mtdcr(ebccfga, pb0cr);
111 base_b1 = -size_b1;
112 pbcr = (pbcr & 0x0001ffff) | base_b1 | (((size_b1/1024/1024)-1)<<17);
113 mtdcr(ebccfgd, pbcr);
114 /* printf("pb1cr = %x\n", pbcr); */
117 if (size_b0)
119 mtdcr(ebccfga, pb1cr);
120 pbcr = mfdcr(ebccfgd);
121 mtdcr(ebccfga, pb1cr);
122 base_b0 = base_b1 - size_b0;
123 pbcr = (pbcr & 0x0001ffff) | base_b0 | (((size_b0/1024/1024)-1)<<17);
124 mtdcr(ebccfgd, pbcr);
125 /* printf("pb0cr = %x\n", pbcr); */
128 size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)base_b0, &flash_info[0]);
130 flash_get_offsets (base_b0, &flash_info[0]);
132 /* monitor protection ON by default */
133 #if 0 /* sand: */
134 (void)flash_protect(FLAG_PROTECT_SET,
135 FLASH_BASE0_PRELIM-monitor_flash_len+size_b0,
136 FLASH_BASE0_PRELIM-1+size_b0,
137 &flash_info[0]);
138 #else
139 (void)flash_protect(FLAG_PROTECT_SET,
140 CFG_MONITOR_BASE,
141 CFG_MONITOR_BASE+monitor_flash_len-1,
142 &flash_info[0]);
143 #endif
145 if (size_b1) {
146 /* Re-do sizing to get full correct info */
147 size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)base_b1, &flash_info[1]);
149 flash_get_offsets (base_b1, &flash_info[1]);
151 /* monitor protection ON by default */
152 (void)flash_protect(FLAG_PROTECT_SET,
153 base_b1+size_b1-monitor_flash_len,
154 base_b1+size_b1-1,
155 &flash_info[1]);
156 /* monitor protection OFF by default (one is enough) */
157 (void)flash_protect(FLAG_PROTECT_CLEAR,
158 base_b0+size_b0-monitor_flash_len,
159 base_b0+size_b0-1,
160 &flash_info[0]);
161 } else {
162 flash_info[1].flash_id = FLASH_UNKNOWN;
163 flash_info[1].sector_count = -1;
166 flash_info[0].size = size_b0;
167 flash_info[1].size = size_b1;
168 }/* else 2 banks */
169 return (size_b0 + size_b1);
173 /*-----------------------------------------------------------------------
176 static void flash_get_offsets (ulong base, flash_info_t *info)
178 int i;
180 /* set up sector start adress table */
181 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F320J3A ||
182 (info->flash_id & FLASH_TYPEMASK) == FLASH_28F640J3A ||
183 (info->flash_id & FLASH_TYPEMASK) == FLASH_28F128J3A) {
184 for (i = 0; i < info->sector_count; i++) {
185 info->start[i] = base + (i * info->size/info->sector_count);
187 } else if (info->flash_id & FLASH_BTYPE) {
188 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
190 #ifndef CFG_FLASH_16BIT
191 /* set sector offsets for bottom boot block type */
192 info->start[0] = base + 0x00000000;
193 info->start[1] = base + 0x00004000;
194 info->start[2] = base + 0x00008000;
195 info->start[3] = base + 0x0000C000;
196 info->start[4] = base + 0x00010000;
197 info->start[5] = base + 0x00014000;
198 info->start[6] = base + 0x00018000;
199 info->start[7] = base + 0x0001C000;
200 for (i = 8; i < info->sector_count; i++) {
201 info->start[i] = base + (i * 0x00020000) - 0x000E0000;
204 else {
205 /* set sector offsets for bottom boot block type */
206 info->start[0] = base + 0x00000000;
207 info->start[1] = base + 0x00008000;
208 info->start[2] = base + 0x0000C000;
209 info->start[3] = base + 0x00010000;
210 for (i = 4; i < info->sector_count; i++) {
211 info->start[i] = base + (i * 0x00020000) - 0x00060000;
214 #else
215 /* set sector offsets for bottom boot block type */
216 info->start[0] = base + 0x00000000;
217 info->start[1] = base + 0x00002000;
218 info->start[2] = base + 0x00004000;
219 info->start[3] = base + 0x00006000;
220 info->start[4] = base + 0x00008000;
221 info->start[5] = base + 0x0000A000;
222 info->start[6] = base + 0x0000C000;
223 info->start[7] = base + 0x0000E000;
224 for (i = 8; i < info->sector_count; i++) {
225 info->start[i] = base + (i * 0x00010000) - 0x00070000;
228 else {
229 /* set sector offsets for bottom boot block type */
230 info->start[0] = base + 0x00000000;
231 info->start[1] = base + 0x00004000;
232 info->start[2] = base + 0x00006000;
233 info->start[3] = base + 0x00008000;
234 for (i = 4; i < info->sector_count; i++) {
235 info->start[i] = base + (i * 0x00010000) - 0x00030000;
238 #endif
239 } else {
240 /* set sector offsets for top boot block type */
241 i = info->sector_count - 1;
242 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
244 #ifndef CFG_FLASH_16BIT
245 info->start[i--] = base + info->size - 0x00004000;
246 info->start[i--] = base + info->size - 0x00008000;
247 info->start[i--] = base + info->size - 0x0000C000;
248 info->start[i--] = base + info->size - 0x00010000;
249 info->start[i--] = base + info->size - 0x00014000;
250 info->start[i--] = base + info->size - 0x00018000;
251 info->start[i--] = base + info->size - 0x0001C000;
252 for (; i >= 0; i--) {
253 info->start[i] = base + i * 0x00020000;
256 } else {
258 info->start[i--] = base + info->size - 0x00008000;
259 info->start[i--] = base + info->size - 0x0000C000;
260 info->start[i--] = base + info->size - 0x00010000;
261 for (; i >= 0; i--) {
262 info->start[i] = base + i * 0x00020000;
265 #else
266 info->start[i--] = base + info->size - 0x00002000;
267 info->start[i--] = base + info->size - 0x00004000;
268 info->start[i--] = base + info->size - 0x00006000;
269 info->start[i--] = base + info->size - 0x00008000;
270 info->start[i--] = base + info->size - 0x0000A000;
271 info->start[i--] = base + info->size - 0x0000C000;
272 info->start[i--] = base + info->size - 0x0000E000;
273 for (; i >= 0; i--) {
274 info->start[i] = base + i * 0x00010000;
277 } else {
279 info->start[i--] = base + info->size - 0x00004000;
280 info->start[i--] = base + info->size - 0x00006000;
281 info->start[i--] = base + info->size - 0x00008000;
282 for (; i >= 0; i--) {
283 info->start[i] = base + i * 0x00010000;
286 #endif
292 /*-----------------------------------------------------------------------
295 void flash_print_info (flash_info_t *info)
297 int i;
298 uchar *boottype;
299 uchar botboot[]=", bottom boot sect)\n";
300 uchar topboot[]=", top boot sector)\n";
302 if (info->flash_id == FLASH_UNKNOWN) {
303 printf ("missing or unknown FLASH type\n");
304 return;
307 switch (info->flash_id & FLASH_VENDMASK) {
308 case FLASH_MAN_AMD: printf ("AMD "); break;
309 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
310 case FLASH_MAN_SST: printf ("SST "); break;
311 case FLASH_MAN_STM: printf ("STM "); break;
312 case FLASH_MAN_INTEL: printf ("INTEL "); break;
313 default: printf ("Unknown Vendor "); break;
316 if (info->flash_id & 0x0001 ) {
317 boottype = botboot;
318 } else {
319 boottype = topboot;
322 switch (info->flash_id & FLASH_TYPEMASK) {
323 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit%s",boottype);
324 break;
325 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit%s",boottype);
326 break;
327 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit%s",boottype);
328 break;
329 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit%s",boottype);
330 break;
331 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit%s",boottype);
332 break;
333 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit%s",boottype);
334 break;
335 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit%s",boottype);
336 break;
337 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit%s",boottype);
338 break;
339 case FLASH_INTEL800B: printf ("INTEL28F800B (8 Mbit%s",boottype);
340 break;
341 case FLASH_INTEL800T: printf ("INTEL28F800T (8 Mbit%s",boottype);
342 break;
343 case FLASH_INTEL160B: printf ("INTEL28F160B (16 Mbit%s",boottype);
344 break;
345 case FLASH_INTEL160T: printf ("INTEL28F160T (16 Mbit%s",boottype);
346 break;
347 case FLASH_INTEL320B: printf ("INTEL28F320B (32 Mbit%s",boottype);
348 break;
349 case FLASH_INTEL320T: printf ("INTEL28F320T (32 Mbit%s",boottype);
350 break;
352 #if 0 /* enable when devices are available */
354 case FLASH_INTEL640B: printf ("INTEL28F640B (64 Mbit%s",boottype);
355 break;
356 case FLASH_INTEL640T: printf ("INTEL28F640T (64 Mbit%s",boottype);
357 break;
358 #endif
359 case FLASH_28F320J3A: printf ("INTEL28F320J3A (32 Mbit%s",boottype);
360 break;
361 case FLASH_28F640J3A: printf ("INTEL28F640J3A (64 Mbit%s",boottype);
362 break;
363 case FLASH_28F128J3A: printf ("INTEL28F128J3A (128 Mbit%s",boottype);
364 break;
366 default: printf ("Unknown Chip Type\n");
367 break;
370 printf (" Size: %ld MB in %d Sectors\n",
371 info->size >> 20, info->sector_count);
373 printf (" Sector Start Addresses:");
374 for (i=0; i<info->sector_count; ++i) {
375 if ((i % 5) == 0)
376 printf ("\n ");
377 printf (" %08lX%s",
378 info->start[i],
379 info->protect[i] ? " (RO)" : " "
382 printf ("\n");
383 return;
387 /*-----------------------------------------------------------------------
391 /*-----------------------------------------------------------------------
395 * The following code cannot be run from FLASH!
397 ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info)
399 short i;
400 ulong base = (ulong)addr;
401 FLASH_WORD_SIZE value;
403 /* Write auto select command: read Manufacturer ID */
406 #ifndef CFG_FLASH_16BIT
409 * Note: if it is an AMD flash and the word at addr[0000]
410 * is 0x00890089 this routine will think it is an Intel
411 * flash device and may(most likely) cause trouble.
414 addr[0x0000] = 0x00900090;
415 if(addr[0x0000] != 0x00890089){
416 addr[0x0555] = 0x00AA00AA;
417 addr[0x02AA] = 0x00550055;
418 addr[0x0555] = 0x00900090;
419 #else
422 * Note: if it is an AMD flash and the word at addr[0000]
423 * is 0x0089 this routine will think it is an Intel
424 * flash device and may(most likely) cause trouble.
427 addr[0x0000] = 0x0090;
429 if(addr[0x0000] != 0x0089){
430 addr[0x0555] = 0x00AA;
431 addr[0x02AA] = 0x0055;
432 addr[0x0555] = 0x0090;
433 #endif
435 value = addr[0];
437 switch (value) {
438 case (AMD_MANUFACT & FLASH_ID_MASK):
439 info->flash_id = FLASH_MAN_AMD;
440 break;
441 case (FUJ_MANUFACT & FLASH_ID_MASK):
442 info->flash_id = FLASH_MAN_FUJ;
443 break;
444 case (STM_MANUFACT & FLASH_ID_MASK):
445 info->flash_id = FLASH_MAN_STM;
446 break;
447 case (SST_MANUFACT & FLASH_ID_MASK):
448 info->flash_id = FLASH_MAN_SST;
449 break;
450 case (INTEL_MANUFACT & FLASH_ID_MASK):
451 info->flash_id = FLASH_MAN_INTEL;
452 break;
453 default:
454 info->flash_id = FLASH_UNKNOWN;
455 info->sector_count = 0;
456 info->size = 0;
457 return (0); /* no or unknown flash */
461 value = addr[1]; /* device ID */
463 switch (value) {
465 case (AMD_ID_LV400T & FLASH_ID_MASK):
466 info->flash_id += FLASH_AM400T;
467 info->sector_count = 11;
468 info->size = 0x00100000;
469 break; /* => 1 MB */
471 case (AMD_ID_LV400B & FLASH_ID_MASK):
472 info->flash_id += FLASH_AM400B;
473 info->sector_count = 11;
474 info->size = 0x00100000;
475 break; /* => 1 MB */
477 case (AMD_ID_LV800T & FLASH_ID_MASK):
478 info->flash_id += FLASH_AM800T;
479 info->sector_count = 19;
480 info->size = 0x00200000;
481 break; /* => 2 MB */
483 case (AMD_ID_LV800B & FLASH_ID_MASK):
484 info->flash_id += FLASH_AM800B;
485 info->sector_count = 19;
486 info->size = 0x00200000;
487 break; /* => 2 MB */
489 case (AMD_ID_LV160T & FLASH_ID_MASK):
490 info->flash_id += FLASH_AM160T;
491 info->sector_count = 35;
492 info->size = 0x00400000;
493 break; /* => 4 MB */
495 case (AMD_ID_LV160B & FLASH_ID_MASK):
496 info->flash_id += FLASH_AM160B;
497 info->sector_count = 35;
498 info->size = 0x00400000;
499 break; /* => 4 MB */
500 #if 0 /* enable when device IDs are available */
501 case (AMD_ID_LV320T & FLASH_ID_MASK):
502 info->flash_id += FLASH_AM320T;
503 info->sector_count = 67;
504 info->size = 0x00800000;
505 break; /* => 8 MB */
507 case (AMD_ID_LV320B & FLASH_ID_MASK):
508 info->flash_id += FLASH_AM320B;
509 info->sector_count = 67;
510 info->size = 0x00800000;
511 break; /* => 8 MB */
512 #endif
514 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
515 info->flash_id += FLASH_INTEL800T;
516 info->sector_count = 23;
517 info->size = 0x00200000;
518 break; /* => 2 MB */
520 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
521 info->flash_id += FLASH_INTEL800B;
522 info->sector_count = 23;
523 info->size = 0x00200000;
524 break; /* => 2 MB */
526 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
527 info->flash_id += FLASH_INTEL160T;
528 info->sector_count = 39;
529 info->size = 0x00400000;
530 break; /* => 4 MB */
532 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
533 info->flash_id += FLASH_INTEL160B;
534 info->sector_count = 39;
535 info->size = 0x00400000;
536 break; /* => 4 MB */
538 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
539 info->flash_id += FLASH_INTEL320T;
540 info->sector_count = 71;
541 info->size = 0x00800000;
542 break; /* => 8 MB */
544 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
545 info->flash_id += FLASH_AM320B;
546 info->sector_count = 71;
547 info->size = 0x00800000;
548 break; /* => 8 MB */
550 #if 0 /* enable when devices are available */
551 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
552 info->flash_id += FLASH_INTEL320T;
553 info->sector_count = 135;
554 info->size = 0x01000000;
555 break; /* => 16 MB */
557 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
558 info->flash_id += FLASH_AM320B;
559 info->sector_count = 135;
560 info->size = 0x01000000;
561 break; /* => 16 MB */
562 #endif
563 case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
564 info->flash_id += FLASH_28F320J3A;
565 info->sector_count = 32;
566 info->size = 0x00400000;
567 break; /* => 32 MBit */
568 case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
569 info->flash_id += FLASH_28F640J3A;
570 info->sector_count = 64;
571 info->size = 0x00800000;
572 break; /* => 64 MBit */
573 case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
574 info->flash_id += FLASH_28F128J3A;
575 info->sector_count = 128;
576 info->size = 0x01000000;
577 break; /* => 128 MBit */
579 default:
580 /* FIXME*/
581 info->flash_id = FLASH_UNKNOWN;
582 return (0); /* => no or unknown flash */
585 flash_get_offsets(base, info);
587 /* check for protected sectors */
588 for (i = 0; i < info->sector_count; i++) {
589 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
590 /* D0 = 1 if protected */
591 addr = (volatile FLASH_WORD_SIZE *)(info->start[i]);
592 info->protect[i] = addr[2] & 1;
596 * Prevent writes to uninitialized FLASH.
598 if (info->flash_id != FLASH_UNKNOWN) {
599 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
600 if( (info->flash_id & 0xFF00) == FLASH_MAN_INTEL){
601 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
602 } else {
603 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
607 return (info->size);
611 /*-----------------------------------------------------------------------
614 int flash_erase (flash_info_t *info, int s_first, int s_last)
617 volatile FLASH_WORD_SIZE *addr=(volatile FLASH_WORD_SIZE*)(info->start[0]);
618 int flag, prot, sect, l_sect, barf;
619 ulong start, now, last;
620 int rcode = 0;
622 if ((s_first < 0) || (s_first > s_last)) {
623 if (info->flash_id == FLASH_UNKNOWN) {
624 printf ("- missing\n");
625 } else {
626 printf ("- no sectors to erase\n");
628 return 1;
631 if ((info->flash_id == FLASH_UNKNOWN) ||
632 ((info->flash_id > FLASH_AMD_COMP) &&
633 ( (info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL ) ) ){
634 printf ("Can't erase unknown flash type - aborted\n");
635 return 1;
638 prot = 0;
639 for (sect=s_first; sect<=s_last; ++sect) {
640 if (info->protect[sect]) {
641 prot++;
645 if (prot) {
646 printf ("- Warning: %d protected sectors will not be erased!\n",
647 prot);
648 } else {
649 printf ("\n");
652 l_sect = -1;
654 /* Disable interrupts which might cause a timeout here */
655 flag = disable_interrupts();
656 if(info->flash_id < FLASH_AMD_COMP) {
657 #ifndef CFG_FLASH_16BIT
658 addr[0x0555] = 0x00AA00AA;
659 addr[0x02AA] = 0x00550055;
660 addr[0x0555] = 0x00800080;
661 addr[0x0555] = 0x00AA00AA;
662 addr[0x02AA] = 0x00550055;
663 #else
664 addr[0x0555] = 0x00AA;
665 addr[0x02AA] = 0x0055;
666 addr[0x0555] = 0x0080;
667 addr[0x0555] = 0x00AA;
668 addr[0x02AA] = 0x0055;
669 #endif
670 /* Start erase on unprotected sectors */
671 for (sect = s_first; sect<=s_last; sect++) {
672 if (info->protect[sect] == 0) { /* not protected */
673 addr = (volatile FLASH_WORD_SIZE *)(info->start[sect]);
674 addr[0] = (0x00300030 & FLASH_ID_MASK);
675 l_sect = sect;
679 /* re-enable interrupts if necessary */
680 if (flag)
681 enable_interrupts();
683 /* wait at least 80us - let's wait 1 ms */
684 udelay (1000);
687 * We wait for the last triggered sector
689 if (l_sect < 0)
690 goto DONE;
692 start = get_timer (0);
693 last = start;
694 addr = (volatile FLASH_WORD_SIZE*)(info->start[l_sect]);
695 while ((addr[0] & (0x00800080&FLASH_ID_MASK)) !=
696 (0x00800080&FLASH_ID_MASK) )
698 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
699 printf ("Timeout\n");
700 return 1;
702 /* show that we're waiting */
703 if ((now - last) > 1000) { /* every second */
704 serial_putc ('.');
705 last = now;
709 DONE:
710 /* reset to read mode */
711 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
712 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
713 } else {
716 for (sect = s_first; sect<=s_last; sect++) {
717 if (info->protect[sect] == 0) { /* not protected */
718 barf = 0;
719 #ifndef CFG_FLASH_16BIT
720 addr = (vu_long*)(info->start[sect]);
721 addr[0] = 0x00200020;
722 addr[0] = 0x00D000D0;
723 while(!(addr[0] & 0x00800080)); /* wait for error or finish */
724 if( addr[0] & 0x003A003A) { /* check for error */
725 barf = addr[0] & 0x003A0000;
726 if( barf ) {
727 barf >>=16;
728 } else {
729 barf = addr[0] & 0x0000003A;
732 #else
733 addr = (vu_short*)(info->start[sect]);
734 addr[0] = 0x0020;
735 addr[0] = 0x00D0;
736 while(!(addr[0] & 0x0080)); /* wait for error or finish */
737 if( addr[0] & 0x003A) /* check for error */
738 barf = addr[0] & 0x003A;
739 #endif
740 if(barf) {
741 printf("\nFlash error in sector at %lx\n",(unsigned long)addr);
742 if(barf & 0x0002) printf("Block locked, not erased.\n");
743 if((barf & 0x0030) == 0x0030)
744 printf("Command Sequence error.\n");
745 if((barf & 0x0030) == 0x0020)
746 printf("Block Erase error.\n");
747 if(barf & 0x0008) printf("Vpp Low error.\n");
748 rcode = 1;
749 } else printf(".");
750 l_sect = sect;
752 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
753 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
758 printf (" done\n");
759 return rcode;
762 /*-----------------------------------------------------------------------
765 /*flash_info_t *addr2info (ulong addr)
767 flash_info_t *info;
768 int i;
770 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
771 if ((addr >= info->start[0]) &&
772 (addr < (info->start[0] + info->size)) ) {
773 return (info);
777 return (NULL);
780 /*-----------------------------------------------------------------------
781 * Copy memory to flash.
782 * Make sure all target addresses are within Flash bounds,
783 * and no protected sectors are hit.
784 * Returns:
785 * 0 - OK
786 * 1 - write timeout
787 * 2 - Flash not erased
788 * 4 - target range includes protected sectors
789 * 8 - target address not in Flash memory
792 /*int flash_write (uchar *src, ulong addr, ulong cnt)
794 int i;
795 ulong end = addr + cnt - 1;
796 flash_info_t *info_first = addr2info (addr);
797 flash_info_t *info_last = addr2info (end );
798 flash_info_t *info;
800 if (cnt == 0) {
801 return (0);
804 if (!info_first || !info_last) {
805 return (8);
808 for (info = info_first; info <= info_last; ++info) {
809 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
810 /* short s_end = info->sector_count - 1;
811 for (i=0; i<info->sector_count; ++i) {
812 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
814 if ((end >= info->start[i]) && (addr < e_addr) &&
815 (info->protect[i] != 0) ) {
816 return (4);
821 */ /* finally write data to flash */
822 /* for (info = info_first; info <= info_last && cnt>0; ++info) {
823 ulong len;
825 len = info->start[0] + info->size - addr;
826 if (len > cnt)
827 len = cnt;
828 if ((i = write_buff(info, src, addr, len)) != 0) {
829 return (i);
831 cnt -= len;
832 addr += len;
833 src += len;
835 return (0);
838 /*-----------------------------------------------------------------------
839 * Copy memory to flash, returns:
840 * 0 - OK
841 * 1 - write timeout
842 * 2 - Flash not erased
845 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
847 #ifndef CFG_FLASH_16BIT
848 ulong cp, wp, data;
849 int l;
850 #else
851 ulong cp, wp;
852 ushort data;
853 #endif
854 int i, rc;
856 #ifndef CFG_FLASH_16BIT
859 wp = (addr & ~3); /* get lower word aligned address */
862 * handle unaligned start bytes
864 if ((l = addr - wp) != 0) {
865 data = 0;
866 for (i=0, cp=wp; i<l; ++i, ++cp) {
867 data = (data << 8) | (*(uchar *)cp);
869 for (; i<4 && cnt>0; ++i) {
870 data = (data << 8) | *src++;
871 --cnt;
872 ++cp;
874 for (; cnt==0 && i<4; ++i, ++cp) {
875 data = (data << 8) | (*(uchar *)cp);
878 if ((rc = write_word(info, wp, data)) != 0) {
879 return (rc);
881 wp += 4;
885 * handle word aligned part
887 while (cnt >= 4) {
888 data = 0;
889 for (i=0; i<4; ++i) {
890 data = (data << 8) | *src++;
892 if ((rc = write_word(info, wp, data)) != 0) {
893 return (rc);
895 wp += 4;
896 cnt -= 4;
899 if (cnt == 0) {
900 return (0);
904 * handle unaligned tail bytes
906 data = 0;
907 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
908 data = (data << 8) | *src++;
909 --cnt;
911 for (; i<4; ++i, ++cp) {
912 data = (data << 8) | (*(uchar *)cp);
915 return (write_word(info, wp, data));
917 #else
918 wp = (addr & ~1); /* get lower word aligned address */
921 * handle unaligned start byte
923 if (addr - wp) {
924 data = 0;
925 data = (data << 8) | *src++;
926 --cnt;
927 if ((rc = write_short(info, wp, data)) != 0) {
928 return (rc);
930 wp += 2;
934 * handle word aligned part
936 /* l = 0; used for debuging */
937 while (cnt >= 2) {
938 data = 0;
939 for (i=0; i<2; ++i) {
940 data = (data << 8) | *src++;
943 /* if(!l){
944 printf("%x",data);
945 l = 1;
946 } used for debuging */
948 if ((rc = write_short(info, wp, data)) != 0) {
949 return (rc);
951 wp += 2;
952 cnt -= 2;
955 if (cnt == 0) {
956 return (0);
960 * handle unaligned tail bytes
962 data = 0;
963 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
964 data = (data << 8) | *src++;
965 --cnt;
967 for (; i<2; ++i, ++cp) {
968 data = (data << 8) | (*(uchar *)cp);
971 return (write_short(info, wp, data));
974 #endif
977 /*-----------------------------------------------------------------------
978 * Write a word to Flash, returns:
979 * 0 - OK
980 * 1 - write timeout
981 * 2 - Flash not erased
983 #ifndef CFG_FLASH_16BIT
984 static int write_word (flash_info_t *info, ulong dest, ulong data)
986 vu_long *addr = (vu_long*)(info->start[0]);
987 ulong start,barf;
988 int flag;
991 /* Check if Flash is (sufficiently) erased */
992 if ((*((vu_long *)dest) & data) != data) {
993 return (2);
996 /* Disable interrupts which might cause a timeout here */
997 flag = disable_interrupts();
999 if(info->flash_id > FLASH_AMD_COMP) {
1000 /* AMD stuff */
1001 addr[0x0555] = 0x00AA00AA;
1002 addr[0x02AA] = 0x00550055;
1003 addr[0x0555] = 0x00A000A0;
1004 } else {
1005 /* intel stuff */
1006 *addr = 0x00400040;
1008 *((vu_long *)dest) = data;
1010 /* re-enable interrupts if necessary */
1011 if (flag)
1012 enable_interrupts();
1014 /* data polling for D7 */
1015 start = get_timer (0);
1017 if(info->flash_id > FLASH_AMD_COMP) {
1019 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
1020 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
1021 return (1);
1025 } else {
1027 while(!(addr[0] & 0x00800080)){ /* wait for error or finish */
1028 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
1029 return (1);
1032 if( addr[0] & 0x003A003A) { /* check for error */
1033 barf = addr[0] & 0x003A0000;
1034 if( barf ) {
1035 barf >>=16;
1036 } else {
1037 barf = addr[0] & 0x0000003A;
1039 printf("\nFlash write error at address %lx\n",(unsigned long)dest);
1040 if(barf & 0x0002) printf("Block locked, not erased.\n");
1041 if(barf & 0x0010) printf("Programming error.\n");
1042 if(barf & 0x0008) printf("Vpp Low error.\n");
1043 return(2);
1049 return (0);
1053 #else
1055 static int write_short (flash_info_t *info, ulong dest, ushort data)
1057 vu_short *addr = (vu_short*)(info->start[0]);
1058 ulong start,barf;
1059 int flag;
1061 /* Check if Flash is (sufficiently) erased */
1062 if ((*((vu_short *)dest) & data) != data) {
1063 return (2);
1066 /* Disable interrupts which might cause a timeout here */
1067 flag = disable_interrupts();
1069 if(info->flash_id < FLASH_AMD_COMP) {
1070 /* AMD stuff */
1071 addr[0x0555] = 0x00AA;
1072 addr[0x02AA] = 0x0055;
1073 addr[0x0555] = 0x00A0;
1074 } else {
1075 /* intel stuff */
1076 *addr = 0x00D0;
1077 *addr = 0x0040;
1079 *((vu_short *)dest) = data;
1081 /* re-enable interrupts if necessary */
1082 if (flag)
1083 enable_interrupts();
1085 /* data polling for D7 */
1086 start = get_timer (0);
1088 if(info->flash_id < FLASH_AMD_COMP) {
1089 /* AMD stuff */
1090 while ((*((vu_short *)dest) & 0x0080) != (data & 0x0080)) {
1091 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
1092 return (1);
1096 } else {
1097 /* intel stuff */
1098 while(!(addr[0] & 0x0080)){ /* wait for error or finish */
1099 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) return (1);
1102 if( addr[0] & 0x003A) { /* check for error */
1103 barf = addr[0] & 0x003A;
1104 printf("\nFlash write error at address %lx\n",(unsigned long)dest);
1105 if(barf & 0x0002) printf("Block locked, not erased.\n");
1106 if(barf & 0x0010) printf("Programming error.\n");
1107 if(barf & 0x0008) printf("Vpp Low error.\n");
1108 return(2);
1110 *addr = 0x00B0;
1111 *addr = 0x0070;
1112 while(!(addr[0] & 0x0080)){ /* wait for error or finish */
1113 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) return (1);
1116 *addr = 0x00FF;
1120 return (0);
1125 #endif
1127 /*-----------------------------------------------------------------------