1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <asm/inca-ip.h>
26
27flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
28
29
30
31
32#ifdef CONFIG_FLASH_16BIT
33typedef unsigned short FLASH_PORT_WIDTH;
34typedef volatile unsigned short FLASH_PORT_WIDTHV;
35#define FLASH_ID_MASK 0xFFFF
36#else
37typedef unsigned long FLASH_PORT_WIDTH;
38typedef volatile unsigned long FLASH_PORT_WIDTHV;
39#define FLASH_ID_MASK 0xFFFFFFFF
40#endif
41
42#define FPW FLASH_PORT_WIDTH
43#define FPWV FLASH_PORT_WIDTHV
44
45#define ORMASK(size) ((-size) & OR_AM_MSK)
46
47#if 0
48#define FLASH_CYCLE1 0x0555
49#define FLASH_CYCLE2 0x02aa
50#else
51#define FLASH_CYCLE1 0x0554
52#define FLASH_CYCLE2 0x02ab
53#endif
54
55
56
57
58static ulong flash_get_size(FPWV *addr, flash_info_t *info);
59static void flash_reset(flash_info_t *info);
60static int write_word_intel(flash_info_t *info, FPWV *dest, FPW data);
61static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
62static void flash_get_offsets(ulong base, flash_info_t *info);
63static flash_info_t *flash_get_info(ulong base);
64
65
66
67
68
69
70unsigned long flash_init (void)
71{
72 unsigned long size = 0;
73 int i;
74
75
76 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
77 ulong flashbase = (i == 0) ? PHYS_FLASH_1 : PHYS_FLASH_2;
78 ulong * buscon = (ulong *)
79 ((i == 0) ? INCA_IP_EBU_EBU_BUSCON0 : INCA_IP_EBU_EBU_BUSCON2);
80
81
82 *buscon &= ~INCA_IP_EBU_EBU_BUSCON1_WRDIS;
83
84#if 1
85 memset(&flash_info[i], 0, sizeof(flash_info_t));
86#endif
87
88 flash_info[i].size =
89 flash_get_size((FPW *)flashbase, &flash_info[i]);
90
91 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
92 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx\n",
93 i, flash_info[i].size);
94 }
95
96 size += flash_info[i].size;
97 }
98
99#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
100
101 flash_protect(FLAG_PROTECT_SET,
102 CONFIG_SYS_MONITOR_BASE,
103 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
104 flash_get_info(CONFIG_SYS_MONITOR_BASE));
105#endif
106
107#ifdef CONFIG_ENV_IS_IN_FLASH
108
109 flash_protect(FLAG_PROTECT_SET,
110 CONFIG_ENV_ADDR,
111 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
112 flash_get_info(CONFIG_ENV_ADDR));
113#endif
114
115
116 return size;
117}
118
119
120
121static void flash_reset(flash_info_t *info)
122{
123 FPWV *base = (FPWV *)(info->start[0]);
124
125
126 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
127 *base = (FPW)0x00FF00FF;
128 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
129 *base = (FPW)0x00F000F0;
130}
131
132
133
134static void flash_get_offsets (ulong base, flash_info_t *info)
135{
136 int i;
137
138
139 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL
140 && (info->flash_id & FLASH_BTYPE)) {
141 int bootsect_size;
142 int sect_size;
143
144 bootsect_size = 0x00002000 * (sizeof(FPW)/2);
145 sect_size = 0x00010000 * (sizeof(FPW)/2);
146
147
148 for (i = 0; i < 8; ++i) {
149 info->start[i] = base + (i * bootsect_size);
150 }
151 for (i = 8; i < info->sector_count; i++) {
152 info->start[i] = base + ((i - 7) * sect_size);
153 }
154 }
155 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD
156 && (info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U) {
157
158 int sect_size;
159
160 sect_size = 0x00010000 * (sizeof(FPW)/2);
161
162
163 for( i = 0; i < info->sector_count; i++ )
164 info->start[i] = base + (i * sect_size);
165 }
166}
167
168
169
170
171static flash_info_t *flash_get_info(ulong base)
172{
173 int i;
174 flash_info_t * info;
175
176 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i ++) {
177 info = & flash_info[i];
178 if (info->start[0] <= base && base < info->start[0] + info->size)
179 break;
180 }
181
182 return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
183}
184
185
186
187
188void flash_print_info (flash_info_t *info)
189{
190 int i;
191 uchar *boottype;
192 uchar *bootletter;
193 char *fmt;
194 uchar botbootletter[] = "B";
195 uchar topbootletter[] = "T";
196 uchar botboottype[] = "bottom boot sector";
197 uchar topboottype[] = "top boot sector";
198
199 if (info->flash_id == FLASH_UNKNOWN) {
200 printf ("missing or unknown FLASH type\n");
201 return;
202 }
203
204 switch (info->flash_id & FLASH_VENDMASK) {
205 case FLASH_MAN_AMD: printf ("AMD "); break;
206 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
207 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
208 case FLASH_MAN_SST: printf ("SST "); break;
209 case FLASH_MAN_STM: printf ("STM "); break;
210 case FLASH_MAN_INTEL: printf ("INTEL "); break;
211 default: printf ("Unknown Vendor "); break;
212 }
213
214
215 if (info->flash_id & FLASH_BTYPE) {
216 boottype = botboottype;
217 bootletter = botbootletter;
218 }
219 else {
220 boottype = topboottype;
221 bootletter = topbootletter;
222 }
223
224 switch (info->flash_id & FLASH_TYPEMASK) {
225 case FLASH_AM640U:
226 fmt = "29LV641D (64 Mbit, uniform sectors)\n";
227 break;
228 case FLASH_28F800C3B:
229 case FLASH_28F800C3T:
230 fmt = "28F800C3%s (8 Mbit, %s)\n";
231 break;
232 case FLASH_INTEL800B:
233 case FLASH_INTEL800T:
234 fmt = "28F800B3%s (8 Mbit, %s)\n";
235 break;
236 case FLASH_28F160C3B:
237 case FLASH_28F160C3T:
238 fmt = "28F160C3%s (16 Mbit, %s)\n";
239 break;
240 case FLASH_INTEL160B:
241 case FLASH_INTEL160T:
242 fmt = "28F160B3%s (16 Mbit, %s)\n";
243 break;
244 case FLASH_28F320C3B:
245 case FLASH_28F320C3T:
246 fmt = "28F320C3%s (32 Mbit, %s)\n";
247 break;
248 case FLASH_INTEL320B:
249 case FLASH_INTEL320T:
250 fmt = "28F320B3%s (32 Mbit, %s)\n";
251 break;
252 case FLASH_28F640C3B:
253 case FLASH_28F640C3T:
254 fmt = "28F640C3%s (64 Mbit, %s)\n";
255 break;
256 case FLASH_INTEL640B:
257 case FLASH_INTEL640T:
258 fmt = "28F640B3%s (64 Mbit, %s)\n";
259 break;
260 default:
261 fmt = "Unknown Chip Type\n";
262 break;
263 }
264
265 printf (fmt, bootletter, boottype);
266
267 printf (" Size: %ld MB in %d Sectors\n",
268 info->size >> 20,
269 info->sector_count);
270
271 printf (" Sector Start Addresses:");
272
273 for (i=0; i<info->sector_count; ++i) {
274 if ((i % 5) == 0) {
275 printf ("\n ");
276 }
277
278 printf (" %08lX%s", info->start[i],
279 info->protect[i] ? " (RO)" : " ");
280 }
281
282 printf ("\n");
283}
284
285
286
287
288
289
290
291
292ulong flash_get_size (FPWV *addr, flash_info_t *info)
293{
294
295
296
297 addr[FLASH_CYCLE1] = (FPW)0x00AA00AA;
298 addr[FLASH_CYCLE2] = (FPW)0x00550055;
299 addr[FLASH_CYCLE1] = (FPW)0x00900090;
300
301
302
303
304 switch (addr[1] & 0xff) {
305
306 case (uchar)AMD_MANUFACT:
307 info->flash_id = FLASH_MAN_AMD;
308 break;
309
310 case (uchar)INTEL_MANUFACT:
311 info->flash_id = FLASH_MAN_INTEL;
312 break;
313
314 default:
315 info->flash_id = FLASH_UNKNOWN;
316 info->sector_count = 0;
317 info->size = 0;
318 break;
319 }
320
321
322 if (info->flash_id != FLASH_UNKNOWN) switch (addr[0]) {
323
324 case (FPW)AMD_ID_LV640U:
325 info->flash_id += FLASH_AM640U;
326 info->sector_count = 128;
327 info->size = 0x00800000 * (sizeof(FPW)/2);
328 break;
329
330 case (FPW)INTEL_ID_28F800C3B:
331 info->flash_id += FLASH_28F800C3B;
332 info->sector_count = 23;
333 info->size = 0x00100000 * (sizeof(FPW)/2);
334 break;
335
336 case (FPW)INTEL_ID_28F800B3B:
337 info->flash_id += FLASH_INTEL800B;
338 info->sector_count = 23;
339 info->size = 0x00100000 * (sizeof(FPW)/2);
340 break;
341
342 case (FPW)INTEL_ID_28F160C3B:
343 info->flash_id += FLASH_28F160C3B;
344 info->sector_count = 39;
345 info->size = 0x00200000 * (sizeof(FPW)/2);
346 break;
347
348 case (FPW)INTEL_ID_28F160B3B:
349 info->flash_id += FLASH_INTEL160B;
350 info->sector_count = 39;
351 info->size = 0x00200000 * (sizeof(FPW)/2);
352 break;
353
354 case (FPW)INTEL_ID_28F320C3B:
355 info->flash_id += FLASH_28F320C3B;
356 info->sector_count = 71;
357 info->size = 0x00400000 * (sizeof(FPW)/2);
358 break;
359
360 case (FPW)INTEL_ID_28F320B3B:
361 info->flash_id += FLASH_INTEL320B;
362 info->sector_count = 71;
363 info->size = 0x00400000 * (sizeof(FPW)/2);
364 break;
365
366 case (FPW)INTEL_ID_28F640C3B:
367 info->flash_id += FLASH_28F640C3B;
368 info->sector_count = 135;
369 info->size = 0x00800000 * (sizeof(FPW)/2);
370 break;
371
372 case (FPW)INTEL_ID_28F640B3B:
373 info->flash_id += FLASH_INTEL640B;
374 info->sector_count = 135;
375 info->size = 0x00800000 * (sizeof(FPW)/2);
376 break;
377
378 default:
379 info->flash_id = FLASH_UNKNOWN;
380 info->sector_count = 0;
381 info->size = 0;
382 return (0);
383 }
384
385 flash_get_offsets((ulong)addr, info);
386
387
388 flash_reset(info);
389
390 return (info->size);
391}
392
393
394
395
396int flash_erase (flash_info_t *info, int s_first, int s_last)
397{
398 FPWV *addr;
399 int flag, prot, sect;
400 int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
401 ulong start, now, last;
402 int rcode = 0;
403
404 if ((s_first < 0) || (s_first > s_last)) {
405 if (info->flash_id == FLASH_UNKNOWN) {
406 printf ("- missing\n");
407 } else {
408 printf ("- no sectors to erase\n");
409 }
410 return 1;
411 }
412
413 switch (info->flash_id & FLASH_TYPEMASK) {
414 case FLASH_INTEL800B:
415 case FLASH_INTEL160B:
416 case FLASH_INTEL320B:
417 case FLASH_INTEL640B:
418 case FLASH_28F800C3B:
419 case FLASH_28F160C3B:
420 case FLASH_28F320C3B:
421 case FLASH_28F640C3B:
422 case FLASH_AM640U:
423 break;
424 case FLASH_UNKNOWN:
425 default:
426 printf ("Can't erase unknown flash type %08lx - aborted\n",
427 info->flash_id);
428 return 1;
429 }
430
431 prot = 0;
432 for (sect=s_first; sect<=s_last; ++sect) {
433 if (info->protect[sect]) {
434 prot++;
435 }
436 }
437
438 if (prot) {
439 printf ("- Warning: %d protected sectors will not be erased!\n",
440 prot);
441 } else {
442 printf ("\n");
443 }
444
445 last = get_timer(0);
446
447
448 for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
449
450 if (info->protect[sect] != 0)
451 continue;
452
453
454 flag = disable_interrupts();
455
456 addr = (FPWV *)(info->start[sect]);
457 if (intel) {
458 *addr = (FPW)0x00500050;
459 *addr = (FPW)0x00200020;
460 *addr = (FPW)0x00D000D0;
461 }
462 else {
463
464 FPWV *base;
465
466 base = (FPWV *)(info->start[0]);
467 base[FLASH_CYCLE1] = (FPW)0x00AA00AA;
468 base[FLASH_CYCLE2] = (FPW)0x00550055;
469 base[FLASH_CYCLE1] = (FPW)0x00800080;
470 base[FLASH_CYCLE1] = (FPW)0x00AA00AA;
471 base[FLASH_CYCLE2] = (FPW)0x00550055;
472 *addr = (FPW)0x00300030;
473 }
474
475
476 if (flag)
477 enable_interrupts();
478
479 start = get_timer(0);
480
481
482
483
484 udelay (1000);
485
486 while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
487 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
488 printf ("Timeout\n");
489
490 if (intel) {
491
492 *addr = (FPW)0x00B000B0;
493 }
494
495 flash_reset(info);
496 rcode = 1;
497 break;
498 }
499
500
501 if ((get_timer(last)) > CONFIG_SYS_HZ) {
502 putc ('.');
503 last = get_timer(0);
504 }
505 }
506
507
508 if ((get_timer(last)) > CONFIG_SYS_HZ) {
509 putc ('.');
510 last = get_timer(0);
511 }
512
513 flash_reset(info);
514 }
515
516 printf (" done\n");
517 return rcode;
518}
519
520
521
522
523
524
525
526int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
527{
528 FPW data = 0;
529 int bytes;
530 int left;
531 int i, res;
532
533 for (left = cnt, res = 0;
534 left > 0 && res == 0;
535 addr += sizeof(data), left -= sizeof(data) - bytes) {
536
537 bytes = addr & (sizeof(data) - 1);
538 addr &= ~(sizeof(data) - 1);
539
540
541
542
543 for (i = 0; i < sizeof(data); i++) {
544 data <<= 8;
545 if (i < bytes || i - bytes >= left )
546 data += *((uchar *)addr + i);
547 else
548 data += *src++;
549 }
550
551
552 switch (info->flash_id & FLASH_VENDMASK) {
553 case FLASH_MAN_AMD:
554 res = write_word_amd(info, (FPWV *)addr, data);
555 break;
556 case FLASH_MAN_INTEL:
557 res = write_word_intel(info, (FPWV *)addr, data);
558 break;
559 default:
560
561 printf ("missing or unknown FLASH type\n");
562 res = 1;
563 break;
564 }
565 }
566
567 return (res);
568}
569
570
571
572
573
574
575
576
577
578
579
580static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
581{
582 ulong start;
583 int flag;
584 int res = 0;
585 FPWV *base;
586
587
588 if ((*dest & data) != data) {
589 return (2);
590 }
591
592
593 base = (FPWV *)(info->start[0]);
594
595
596 flag = disable_interrupts();
597
598 base[FLASH_CYCLE1] = (FPW)0x00AA00AA;
599 base[FLASH_CYCLE2] = (FPW)0x00550055;
600 base[FLASH_CYCLE1] = (FPW)0x00A000A0;
601
602 *dest = data;
603
604
605 if (flag)
606 enable_interrupts();
607
608 start = get_timer (0);
609
610
611 while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
612 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
613 *dest = (FPW)0x00F000F0;
614 res = 1;
615 }
616 }
617
618 return (res);
619}
620
621
622
623
624
625
626
627
628
629
630
631static int write_word_intel (flash_info_t *info, FPWV *dest, FPW data)
632{
633 ulong start;
634 int flag;
635 int res = 0;
636
637
638 if ((*dest & data) != data) {
639 return (2);
640 }
641
642
643 flag = disable_interrupts();
644
645 *dest = (FPW)0x00500050;
646 *dest = (FPW)0x00FF00FF;
647 *dest = (FPW)0x00400040;
648
649 *dest = data;
650
651
652 if (flag)
653 enable_interrupts();
654
655 start = get_timer (0);
656
657 while (res == 0 && (*dest & (FPW)0x00800080) != (FPW)0x00800080) {
658 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
659 *dest = (FPW)0x00B000B0;
660 res = 1;
661 }
662 }
663
664 if (res == 0 && (*dest & (FPW)0x00100010))
665 res = 1;
666
667 *dest = (FPW)0x00500050;
668 *dest = (FPW)0x00FF00FF;
669
670 return (res);
671}
672