blob: 8c0e229130ab4ec688c363baac08cbfc622b01d0 [file] [log] [blame]
wdenkdc7c9a12003-03-26 06:55:25 +00001/*
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#include <common.h>
25#include <mpc8xx.h>
26
27#ifndef CFG_ENV_ADDR
28#define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
29#endif
30
31flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
32
33/*-----------------------------------------------------------------------
34 * Functions
35 */
36static int write_word (flash_info_t *info, ulong dest, ulong data);
37#if 0
38static ulong flash_get_size (vu_long *addr, flash_info_t *info);
39static void flash_get_offsets (ulong base, flash_info_t *info);
40#endif
41#ifdef CONFIG_BOOT_8B
42static int my_in_8( unsigned char *addr);
43static void my_out_8( unsigned char *addr, int val);
44#endif
45#ifdef CONFIG_BOOT_16B
46static int my_in_be16( unsigned short *addr);
47static void my_out_be16( unsigned short *addr, int val);
48#endif
49#ifdef CONFIG_BOOT_32B
50static unsigned my_in_be32( unsigned *addr);
51static void my_out_be32( unsigned *addr, int val);
52#endif
53/*-----------------------------------------------------------------------
54 */
55
56unsigned long flash_init (void)
57{
58 volatile immap_t *immap = (immap_t *)CFG_IMMR;
59 volatile memctl8xx_t *memctl = &immap->im_memctl;
60 unsigned long size_b0, size_b1;
61 int i;
62
63 size_b0=0;
64 size_b1=0;
65 /* Init: no FLASHes known */
66 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
67 flash_info[i].flash_id = FLASH_UNKNOWN;
68 }
69#ifdef CFG_DOC_BASE
70#ifndef CONFIG_FEL8xx_AT
71 memctl->memc_or5 = (0xffff8000 | CFG_OR_TIMING_DOC ); /* 32k bytes */
72 memctl->memc_br5 = CFG_DOC_BASE | 0x401;
73#else
74 memctl->memc_or3 = (0xffff8000 | CFG_OR_TIMING_DOC ); /* 32k bytes */
75 memctl->memc_br3 = CFG_DOC_BASE | 0x401;
76#endif
77#endif
78#if defined( CONFIG_BOOT_8B)
79// memctl->memc_or0 = 0xfff80ff4; /* 4MB bytes */
80// memctl->memc_br0 = 0x40000401;
81 size_b0 = 0x80000; /* 512 K */
82 flash_info[0].flash_id = FLASH_MAN_AMD | FLASH_AM040;
83 flash_info[0].sector_count = 8;
84 flash_info[0].size = 0x00080000;
85 /* set up sector start address table */
86 for (i = 0; i < flash_info[0].sector_count; i++)
87 flash_info[0].start[i] = 0x40000000 + (i * 0x10000);
88 /* protect all sectors */
89 for (i = 0; i < flash_info[0].sector_count; i++)
90 flash_info[0].protect[i] = 0x1;
91#elif defined (CONFIG_BOOT_16B)
92// memctl->memc_or0 = 0xfff80ff4; /* 4MB bytes */
93// memctl->memc_br0 = 0x40000401;
94 size_b0 = 0x400000; /* 4MB , assume AMD29LV320B */
95 flash_info[0].flash_id = FLASH_MAN_AMD | FLASH_AM320B;
96 flash_info[0].sector_count = 67;
97 flash_info[0].size = 0x00400000;
98 /* set up sector start address table */
99 flash_info[0].start[0] = 0x40000000 ;
100 flash_info[0].start[1] = 0x40000000 + 0x4000;
101 flash_info[0].start[2] = 0x40000000 + 0x6000;
102 flash_info[0].start[3] = 0x40000000 + 0x8000;
103 for (i = 4; i < flash_info[0].sector_count; i++)
104 flash_info[0].start[i] = 0x40000000 + 0x10000 + ((i-4) * 0x10000);
105 /* protect all sectors */
106 for (i = 0; i < flash_info[0].sector_count; i++)
107 flash_info[0].protect[i] = 0x1;
108#endif
109
110
111#ifdef CONFIG_BOOT_32B
112
113 /* Static FLASH Bank configuration here - FIXME XXX */
114
115 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
116
117 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
118 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
119 size_b0, size_b0<<20);
120 }
121
122 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
123
124 if (size_b1 > size_b0) {
125 printf ("## ERROR: "
126 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
127 size_b1, size_b1<<20,
128 size_b0, size_b0<<20
129 );
130 flash_info[0].flash_id = FLASH_UNKNOWN;
131 flash_info[1].flash_id = FLASH_UNKNOWN;
132 flash_info[0].sector_count = -1;
133 flash_info[1].sector_count = -1;
134 flash_info[0].size = 0;
135 flash_info[1].size = 0;
136 return (0);
137 }
138
139 /* Remap FLASH according to real size */
140 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
141 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
142
143 /* Re-do sizing to get full correct info */
144 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
145
146 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
147
148#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
149 /* monitor protection ON by default */
150 flash_protect(FLAG_PROTECT_SET,
151 CFG_MONITOR_BASE,
152 CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
153 &flash_info[0]);
154#endif
155
156#ifdef CFG_ENV_IS_IN_FLASH
157 /* ENV protection ON by default */
158 flash_protect(FLAG_PROTECT_SET,
159 CFG_ENV_ADDR,
160 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
161 &flash_info[0]);
162#endif
163
164 if (size_b1) {
165 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
166 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
167 BR_MS_GPCM | BR_V;
168
169 /* Re-do sizing to get full correct info */
170 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
171 &flash_info[1]);
172
173 flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
174
175#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
176 /* monitor protection ON by default */
177 flash_protect(FLAG_PROTECT_SET,
178 CFG_MONITOR_BASE,
179 CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
180 &flash_info[1]);
181#endif
182
183#ifdef CFG_ENV_IS_IN_FLASH
184 /* ENV protection ON by default */
185 flash_protect(FLAG_PROTECT_SET,
186 CFG_ENV_ADDR,
187 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
188 &flash_info[1]);
189#endif
190 } else {
191 memctl->memc_br1 = 0; /* invalidate bank */
192
193 flash_info[1].flash_id = FLASH_UNKNOWN;
194 flash_info[1].sector_count = -1;
195 }
196
197 flash_info[0].size = size_b0;
198 flash_info[1].size = size_b1;
199
200
201#endif /* CONFIG_BOOT_32B */
202
203 return (size_b0 + size_b1);
204}
205#if 0
206/*-----------------------------------------------------------------------
207 */
208static void flash_get_offsets (ulong base, flash_info_t *info)
209{
210 int i;
211
212 /* set up sector start address table */
213 if (info->flash_id & FLASH_BTYPE) {
214 /* set sector offsets for bottom boot block type */
215 info->start[0] = base + 0x00000000;
216 info->start[1] = base + 0x00008000;
217 info->start[2] = base + 0x0000C000;
218 info->start[3] = base + 0x00010000;
219 for (i = 4; i < info->sector_count; i++) {
220 info->start[i] = base + (i * 0x00020000) - 0x00060000;
221 }
222 } else {
223 /* set sector offsets for top boot block type */
224 i = info->sector_count - 1;
225 info->start[i--] = base + info->size - 0x00008000;
226 info->start[i--] = base + info->size - 0x0000C000;
227 info->start[i--] = base + info->size - 0x00010000;
228 for (; i >= 0; i--) {
229 info->start[i] = base + i * 0x00020000;
230 }
231 }
232}
233#endif
234/*-----------------------------------------------------------------------
235 */
236void flash_print_info (flash_info_t *info)
237{
238 int i;
239
240 if (info->flash_id == FLASH_UNKNOWN) {
241 printf ("missing or unknown FLASH type\n");
242 return;
243 }
244
245 switch (info->flash_id & FLASH_VENDMASK) {
246 case FLASH_MAN_AMD: printf ("AMD "); break;
247 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
248 default: printf ("Unknown Vendor "); break;
249 }
250
251 switch (info->flash_id & FLASH_TYPEMASK) {
252 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
253 break;
254 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
255 break;
256 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
257 break;
258 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
259 break;
260 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
261 break;
262 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
263 break;
264 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
265 break;
266 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
267 break;
268 default: printf ("Unknown Chip Type\n");
269 break;
270 }
271
272 printf (" Size: %ld MB in %d Sectors\n",
273 info->size >> 20, info->sector_count);
274
275 printf (" Sector Start Addresses:");
276 for (i=0; i<info->sector_count; ++i) {
277 if ((i % 5) == 0)
278 printf ("\n ");
279 printf (" %08lX%s",
280 info->start[i],
281 info->protect[i] ? " (RO)" : " "
282 );
283 }
284 printf ("\n");
285 return;
286}
287
288/*-----------------------------------------------------------------------
289 */
290
291
292/*-----------------------------------------------------------------------
293 */
294
295/*
296 * The following code cannot be run from FLASH!
297 */
298#if 0
299static ulong flash_get_size (vu_long *addr, flash_info_t *info)
300{
301 short i;
302 ulong value;
303 ulong base = (ulong)addr;
304
305 /* Write auto select command: read Manufacturer ID */
306 addr[0x0555] = 0x00AA00AA;
307 addr[0x02AA] = 0x00550055;
308 addr[0x0555] = 0x00900090;
309
310 value = addr[0];
311
312 switch (value) {
313 case AMD_MANUFACT:
314 info->flash_id = FLASH_MAN_AMD;
315 break;
316 case FUJ_MANUFACT:
317 info->flash_id = FLASH_MAN_FUJ;
318 break;
319 default:
320 info->flash_id = FLASH_UNKNOWN;
321 info->sector_count = 0;
322 info->size = 0;
323 return (0); /* no or unknown flash */
324 }
325
326 value = addr[1]; /* device ID */
327
328 switch (value) {
329 case AMD_ID_LV400T:
330 info->flash_id += FLASH_AM400T;
331 info->sector_count = 11;
332 info->size = 0x00100000;
333 break; /* => 1 MB */
334
335 case AMD_ID_LV400B:
336 info->flash_id += FLASH_AM400B;
337 info->sector_count = 11;
338 info->size = 0x00100000;
339 break; /* => 1 MB */
340
341 case AMD_ID_LV800T:
342 info->flash_id += FLASH_AM800T;
343 info->sector_count = 19;
344 info->size = 0x00200000;
345 break; /* => 2 MB */
346
347 case AMD_ID_LV800B:
348 info->flash_id += FLASH_AM800B;
349 info->sector_count = 19;
350 info->size = 0x00200000;
351 break; /* => 2 MB */
352
353 case AMD_ID_LV160T:
354 info->flash_id += FLASH_AM160T;
355 info->sector_count = 35;
356 info->size = 0x00400000;
357 break; /* => 4 MB */
358
359 case AMD_ID_LV160B:
360 info->flash_id += FLASH_AM160B;
361 info->sector_count = 35;
362 info->size = 0x00400000;
363 break; /* => 4 MB */
364#if 0 /* enable when device IDs are available */
365 case AMD_ID_LV320T:
366 info->flash_id += FLASH_AM320T;
367 info->sector_count = 67;
368 info->size = 0x00800000;
369 break; /* => 8 MB */
370
371 case AMD_ID_LV320B:
372 info->flash_id += FLASH_AM320B;
373 info->sector_count = 67;
374 info->size = 0x00800000;
375 break; /* => 8 MB */
376#endif
377 default:
378 info->flash_id = FLASH_UNKNOWN;
379 return (0); /* => no or unknown flash */
380 }
381
382 /* set up sector start address table */
383 if (info->flash_id & FLASH_BTYPE) {
384 /* set sector offsets for bottom boot block type */
385 info->start[0] = base + 0x00000000;
386 info->start[1] = base + 0x00008000;
387 info->start[2] = base + 0x0000C000;
388 info->start[3] = base + 0x00010000;
389 for (i = 4; i < info->sector_count; i++) {
390 info->start[i] = base + (i * 0x00020000) - 0x00060000;
391 }
392 } else {
393 /* set sector offsets for top boot block type */
394 i = info->sector_count - 1;
395 info->start[i--] = base + info->size - 0x00008000;
396 info->start[i--] = base + info->size - 0x0000C000;
397 info->start[i--] = base + info->size - 0x00010000;
398 for (; i >= 0; i--) {
399 info->start[i] = base + i * 0x00020000;
400 }
401 }
402
403 /* check for protected sectors */
404 for (i = 0; i < info->sector_count; i++) {
405 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
406 /* D0 = 1 if protected */
407 addr = (volatile unsigned long *)(info->start[i]);
408 info->protect[i] = addr[2] & 1;
409 }
410
411 /*
412 * Prevent writes to uninitialized FLASH.
413 */
414 if (info->flash_id != FLASH_UNKNOWN) {
415 addr = (volatile unsigned long *)info->start[0];
416
417 *addr = 0x00F000F0; /* reset bank */
418 }
419
420 return (info->size);
421}
422#endif
423
424/*-----------------------------------------------------------------------
425 */
426
427int flash_erase (flash_info_t *info, int s_first, int s_last)
428{
429 vu_long *addr = (vu_long*)(info->start[0]);
430 int flag, prot, sect, l_sect,in_mid,in_did;
431 ulong start, now, last;
432
433 if ((s_first < 0) || (s_first > s_last)) {
434 if (info->flash_id == FLASH_UNKNOWN) {
435 printf ("- missing\n");
436 } else {
437 printf ("- no sectors to erase\n");
438 }
439 return 1;
440 }
441
442 if ((info->flash_id == FLASH_UNKNOWN) ||
443 (info->flash_id > FLASH_AMD_COMP)) {
444 printf ("Can't erase unknown flash type %08lx - aborted\n",
445 info->flash_id);
446 return 1;
447 }
448
449 prot = 0;
450 for (sect=s_first; sect<=s_last; ++sect) {
451 if (info->protect[sect]) {
452 prot++;
453 }
454 }
455
456 if (prot) {
457 printf ("- Warning: %d protected sectors will not be erased!\n",
458 prot);
459 } else {
460 printf ("\n");
461 }
462
463 l_sect = -1;
464
465 /* Disable interrupts which might cause a timeout here */
466 flag = disable_interrupts();
467#if defined (CONFIG_BOOT_8B )
468 my_out_8( (unsigned char * ) ((ulong)addr+0x555) , 0xaa );
469 my_out_8( (unsigned char * ) ((ulong)addr+0x2aa) , 0x55 );
470 my_out_8( (unsigned char * ) ((ulong)addr+0x555) , 0x90 );
471 in_mid=my_in_8( (unsigned char * ) addr );
472 in_did=my_in_8( (unsigned char * ) ((ulong)addr+1) );
473 printf(" man ID=0x%x, dev ID=0x%x.\n",in_mid,in_did );
474 my_out_8( (unsigned char *)addr, 0xf0);
475 udelay(1);
476 my_out_8( (unsigned char *) ((ulong)addr+0x555),0xaa );
477 my_out_8( (unsigned char *) ((ulong)addr+0x2aa),0x55 );
478 my_out_8( (unsigned char *) ((ulong)addr+0x555),0x80 );
479 my_out_8( (unsigned char *) ((ulong)addr+0x555),0xaa );
480 my_out_8( (unsigned char *) ((ulong)addr+0x2aa),0x55 );
481 /* Start erase on unprotected sectors */
482 for (sect = s_first; sect<=s_last; sect++) {
483 if (info->protect[sect] == 0) { /* not protected */
484 addr = (vu_long*)(info->start[sect]);
485 //addr[0] = 0x00300030;
486 my_out_8( (unsigned char *) ((ulong)addr),0x30 );
487 l_sect = sect;
488 }
489 }
490#elif defined(CONFIG_BOOT_16B )
491 my_out_be16( (unsigned short * ) ((ulong)addr+ (0xaaa)) , 0xaa );
492 my_out_be16( (unsigned short * ) ((ulong)addr+ (0x554)) , 0x55 );
493 my_out_be16( (unsigned short * ) ((ulong)addr+ (0xaaa)) , 0x90 );
494 in_mid=my_in_be16( (unsigned short * ) addr );
495 in_did=my_in_be16 ( (unsigned short * ) ((ulong)addr+2) );
496 printf(" man ID=0x%x, dev ID=0x%x.\n",in_mid,in_did );
497 my_out_be16( (unsigned short *)addr, 0xf0);
498 udelay(1);
499 my_out_be16( (unsigned short *) ((ulong)addr+ 0xaaa),0xaa );
500 my_out_be16( (unsigned short *) ((ulong)addr+0x554),0x55 );
501 my_out_be16( (unsigned short *) ((ulong)addr+0xaaa),0x80 );
502 my_out_be16( (unsigned short *) ((ulong)addr+0xaaa),0xaa );
503 my_out_be16( (unsigned short *) ((ulong)addr+0x554),0x55 );
504 /* Start erase on unprotected sectors */
505 for (sect = s_first; sect<=s_last; sect++) {
506 if (info->protect[sect] == 0) { /* not protected */
507 addr = (vu_long*)(info->start[sect]);
508 my_out_be16( (unsigned short *) ((ulong)addr),0x30 );
509 l_sect = sect;
510 }
511 }
512
513#elif defined(CONFIG_BOOT_32B)
514 my_out_be32( (unsigned * ) ((ulong)addr+0x1554) , 0xaa );
515 my_out_be32( (unsigned * ) ((ulong)addr+0xaa8) , 0x55 );
516 my_out_be32( (unsigned *) ((ulong)addr+0x1554) , 0x90 );
517 in_mid=my_in_be32( (unsigned * ) addr );
518 in_did=my_in_be32( (unsigned * ) ((ulong)addr+4) );
519 printf(" man ID=0x%x, dev ID=0x%x.\n",in_mid,in_did );
520 my_out_be32( (unsigned *)addr, 0xf0);
521 udelay(1);
522 my_out_be32( (unsigned *) ((ulong)addr+0x1554),0xaa );
523 my_out_be32( (unsigned *) ((ulong)addr+0xaa8),0x55 );
524 my_out_be32( (unsigned *) ((ulong)addr+0x1554),0x80 );
525 my_out_be32( (unsigned *) ((ulong)addr+0x1554),0xaa );
526 my_out_be32( (unsigned *) ((ulong)addr+0xaa8),0x55 );
527 /* Start erase on unprotected sectors */
528 for (sect = s_first; sect<=s_last; sect++) {
529 if (info->protect[sect] == 0) { /* not protected */
530 addr = (vu_long*)(info->start[sect]);
531 my_out_be32( (unsigned *) ((ulong)addr),0x00300030 );
532 l_sect = sect;
533 }
534 }
535
536#else
537# error CONFIG_BOOT_(size)B missing.
538#endif
539 /* re-enable interrupts if necessary */
540 if (flag)
541 enable_interrupts();
542
543 /* wait at least 80us - let's wait 1 ms */
544 udelay (1000);
545
546 /*
547 * We wait for the last triggered sector
548 */
549 if (l_sect < 0)
550 goto DONE;
551
552 start = get_timer (0);
553 last = start;
554 addr = (vu_long*)(info->start[l_sect]);
555#if defined (CONFIG_BOOT_8B)
556 while ( (my_in_8((unsigned char *)addr) & 0x80) != 0x80 )
557#elif defined(CONFIG_BOOT_16B )
558 while ( (my_in_be16((unsigned short *)addr) & 0x0080) != 0x0080 )
559#elif defined(CONFIG_BOOT_32B)
560 while ( (my_in_be32((unsigned *)addr) & 0x00800080) != 0x00800080 )
561#else
562# error CONFIG_BOOT_(size)B missing.
563#endif
564 {
565 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
566 printf ("Timeout\n");
567 return 1;
568 }
569 /* show that we're waiting */
570 if ((now - last) > 1000) { /* every second */
571 putc ('.');
572 last = now;
573 }
574 }
575DONE:
576 /* reset to read mode */
577 addr = (volatile unsigned long *)info->start[0];
578#if defined (CONFIG_BOOT_8B)
579 my_out_8( (unsigned char *)addr, 0xf0);
580#elif defined(CONFIG_BOOT_16B )
581 my_out_be16( (unsigned short * ) addr , 0x00f0 );
582#elif defined(CONFIG_BOOT_32B)
583 my_out_be32 ( (unsigned *)addr, 0x00F000F0 ); /* reset bank */
584#else
585# error CONFIG_BOOT_(size)B missing.
586#endif
587 printf (" done\n");
588 return 0;
589}
590
591/*-----------------------------------------------------------------------
592 * Copy memory to flash, returns:
593 * 0 - OK
594 * 1 - write timeout
595 * 2 - Flash not erased
596 */
597
598int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
599{
600 ulong cp, wp, data;
601 int i, l, rc;
602
603 wp = (addr & ~3); /* get lower word aligned address */
604
605 /*
606 * handle unaligned start bytes
607 */
608 if ((l = addr - wp) != 0) {
609 data = 0;
610 for (i=0, cp=wp; i<l; ++i, ++cp) {
611 data = (data << 8) | (*(uchar *)cp);
612 }
613 for (; i<4 && cnt>0; ++i) {
614 data = (data << 8) | *src++;
615 --cnt;
616 ++cp;
617 }
618 for (; cnt==0 && i<4; ++i, ++cp) {
619 data = (data << 8) | (*(uchar *)cp);
620 }
621
622 if ((rc = write_word(info, wp, data)) != 0) {
623 return (rc);
624 }
625 wp += 4;
626 }
627
628 /*
629 * handle word aligned part
630 */
631 while (cnt >= 4) {
632 data = 0;
633 for (i=0; i<4; ++i) {
634 data = (data << 8) | *src++;
635 }
636 if ((rc = write_word(info, wp, data)) != 0) {
637 return (rc);
638 }
639 wp += 4;
640 cnt -= 4;
641 }
642
643 if (cnt == 0) {
644 return (0);
645 }
646
647 /*
648 * handle unaligned tail bytes
649 */
650 data = 0;
651 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
652 data = (data << 8) | *src++;
653 --cnt;
654 }
655 for (; i<4; ++i, ++cp) {
656 data = (data << 8) | (*(uchar *)cp);
657 }
658
659 return (write_word(info, wp, data));
660}
661
662/*-----------------------------------------------------------------------
663 * Write a word to Flash, returns:
664 * 0 - OK
665 * 1 - write timeout
666 * 2 - Flash not erased
667 */
668static int write_word (flash_info_t *info, ulong dest, ulong data)
669{
670 ulong addr = (ulong)(info->start[0]);
671 ulong start,last;
672 int flag;
673 ulong i;
674 int data_short[2];
675
676 /* Check if Flash is (sufficiently) erased */
677 if ( ((ulong) *(ulong *)dest & data) != data ) {
678 return (2);
679 }
680 /* Disable interrupts which might cause a timeout here */
681 flag = disable_interrupts();
682#if defined(CONFIG_BOOT_8B)
683#ifdef DEBUG
684 {
685 int in_mid,in_did;
686 my_out_8( (unsigned char * ) (addr+0x555) , 0xaa );
687 my_out_8( (unsigned char * ) (addr+0x2aa) , 0x55 );
688 my_out_8( (unsigned char * ) (addr+0x555) , 0x90 );
689 in_mid=my_in_8( (unsigned char * ) addr );
690 in_did=my_in_8( (unsigned char * ) (addr+1) );
691 printf(" man ID=0x%x, dev ID=0x%x.\n",in_mid,in_did );
692 my_out_8( (unsigned char *)addr, 0xf0);
693 udelay(1);
694 }
695#endif
696 {
697 int data_ch[4];
698 data_ch[0]=(int ) ((data>>24) & 0xff);
699 data_ch[1]=(int ) ((data>>16) &0xff );
700 data_ch[2]=(int ) ((data >>8) & 0xff);
701 data_ch[3]=(int ) (data & 0xff);
702 for (i=0;i<4;i++ ){
703 my_out_8( (unsigned char *) (addr+0x555),0xaa);
704 my_out_8((unsigned char *) (addr+0x2aa),0x55);
705 my_out_8( (unsigned char *) (addr+0x555),0xa0);
706 my_out_8((unsigned char *) (dest+i) ,data_ch[i]);
707 /* re-enable interrupts if necessary */
708 if (flag)
709 enable_interrupts();
710
711 start = get_timer (0);
712 last = start;
713 while( ( my_in_8((unsigned char *) (dest+i)) ) != ( data_ch[i] ) ) {
714 if (get_timer(start) > CFG_FLASH_WRITE_TOUT ) {
715 return 1;
716 }
717 }
718 }/* for */
719 }
720#elif defined( CONFIG_BOOT_16B)
721 data_short[0]=(int) (data>>16) & 0xffff;
722 data_short[1]=(int ) data & 0xffff ;
723 for (i=0;i<2;i++ ){
724 my_out_be16( (unsigned short *) ((ulong)addr+ 0xaaa),0xaa );
725 my_out_be16( (unsigned short *) ((ulong)addr+ 0x554),0x55 );
726 my_out_be16( (unsigned short *) ((ulong)addr+ 0xaaa),0xa0 );
727 my_out_be16( (unsigned short *) (dest+(i*2)) ,data_short[i]);
728 /* re-enable interrupts if necessary */
729 if (flag)
730 enable_interrupts();
731 start = get_timer (0);
732 last = start;
733 while( ( my_in_be16((unsigned short *) (dest+(i*2))) ) != ( data_short[i] ) ) {
734 if (get_timer(start) > CFG_FLASH_WRITE_TOUT ) {
735 return 1;
736 }
737 }
738 }
739#elif defined( CONFIG_BOOT_32B)
740 addr[0x0555] = 0x00AA00AA;
741 addr[0x02AA] = 0x00550055;
742 addr[0x0555] = 0x00A000A0;
743
744 *((vu_long *)dest) = data;
745
746 /* re-enable interrupts if necessary */
747 if (flag)
748 enable_interrupts();
749
750 /* data polling for D7 */
751 start = get_timer (0);
752 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
753 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
754 return (1);
755 }
756 }
757#endif
758
759
760 return (0);
761}
762#ifdef CONFIG_BOOT_8B
763static int my_in_8 ( unsigned char *addr)
764{
765 int ret;
766 __asm__ __volatile__("lbz%U1%X1 %0,%1; eieio" : "=r" (ret) : "m" (*addr));
767 return ret;
768}
769
770static void my_out_8 ( unsigned char *addr, int val)
771{
772 __asm__ __volatile__("stb%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
773}
774#endif
775#ifdef CONFIG_BOOT_16B
776static int my_in_be16( unsigned short *addr)
777{
778 int ret;
779 __asm__ __volatile__("lhz%U1%X1 %0,%1; eieio" : "=r" (ret) : "m" (*addr));
780 return ret;
781}
782static void my_out_be16( unsigned short *addr, int val)
783{
784 __asm__ __volatile__("sth%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
785}
786#endif
787#ifdef CONFIG_BOOT_32B
788static unsigned my_in_be32( unsigned *addr)
789{
790 unsigned ret;
791 __asm__ __volatile__("lwz%U1%X1 %0,%1; eieio" : "=r" (ret) : "m" (*addr));
792 return ret;
793}
794static void my_out_be32( unsigned *addr, int val)
795{
796 __asm__ __volatile__("stw%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
797}
798#endif
799
800
801