blob: c8b56fb96b24a0585e543402af50078075cf5a58 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +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
27flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
28
29/*-----------------------------------------------------------------------
30 * Functions
31 */
32static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33static int write_word (flash_info_t *info, ulong dest, ulong data);
34static void flash_get_offsets (ulong base, flash_info_t *info);
35
36/*-----------------------------------------------------------------------
37 * Protection Flags:
38 */
39#define FLAG_PROTECT_SET 0x01
40#define FLAG_PROTECT_CLEAR 0x02
41
42/*-----------------------------------------------------------------------
43 */
44
45unsigned long flash_init (void)
46{
47 volatile immap_t *immap = (immap_t *)CFG_IMMR;
48 volatile memctl8xx_t *memctl = &immap->im_memctl;
49 unsigned long size_b0, size_b1;
50 int i;
51
52 /*printf("faking");*/
53
54 return(0x1fffff);
55
56 /* Init: no FLASHes known */
57 for (i=0; i < CFG_MAX_FLASH_BANKS; ++i)
58 {
59 flash_info[i].flash_id = FLASH_UNKNOWN;
60 }
61
62 /* Static FLASH Bank configuration here - FIXME XXX */
63
64 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
65
66 if (flash_info[0].flash_id == FLASH_UNKNOWN)
67 {
68 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
69 size_b0, size_b0<<20);
70 }
71
72#if 0
73 if (FLASH_BASE1_PRELIM != 0x0) {
74 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
75
76 if (size_b1 > size_b0) {
77 printf ("## ERROR: Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
78 size_b1, size_b1<<20,size_b0, size_b0<<20);
79
80 flash_info[0].flash_id = FLASH_UNKNOWN;
81 flash_info[1].flash_id = FLASH_UNKNOWN;
82 flash_info[0].sector_count = -1;
83 flash_info[1].sector_count = -1;
84 flash_info[0].size = 0;
85 flash_info[1].size = 0;
86 return (0);
87 }
88 } else {
89#endif
90 size_b1 = 0;
91
92 /* Remap FLASH according to real size */
93 memctl->memc_or0 = CFG_OR0_PRELIM;
94 memctl->memc_br0 = CFG_BR0_PRELIM;
95
96 /* Re-do sizing to get full correct info */
97 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
98
99 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
100
101#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
102 /* monitor protection ON by default */
103 (void)flash_protect(FLAG_PROTECT_SET,
104 CFG_MONITOR_BASE,
wdenkb9a83a92003-05-30 12:48:29 +0000105 CFG_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +0000106 &flash_info[0]);
107#endif
108
109 if (size_b1)
110 {
111 /* memctl->memc_or1 = CFG_OR1_PRELIM;
112 memctl->memc_br1 = CFG_BR1_PRELIM; */
113
114 /* Re-do sizing to get full correct info */
115 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
116 &flash_info[1]);
117
118 flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
119
120#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
121 /* monitor protection ON by default */
122 (void)flash_protect(FLAG_PROTECT_SET,
123 CFG_MONITOR_BASE,
wdenkb9a83a92003-05-30 12:48:29 +0000124 CFG_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +0000125 &flash_info[1]);
126#endif
127 }
128 else
129 {
130/* memctl->memc_or1 = CFG_OR1_PRELIM;
131 FIXME memctl->memc_br1 = CFG_BR1_PRELIM; */
132
133 flash_info[1].flash_id = FLASH_UNKNOWN;
134 flash_info[1].sector_count = -1;
135 }
136
137 flash_info[0].size = size_b0;
138 flash_info[1].size = size_b1;
139
140 return (size_b0 + size_b1);
141}
142
143
144static void flash_get_offsets (ulong base, flash_info_t *info)
145{
146 int i;
147
148 /* set up sector start adress table */
149 if (info->flash_id & FLASH_BTYPE)
150 {
151 /* set sector offsets for bottom boot block type */
152 for (i = 0; i < info->sector_count; i++)
153 {
154 info->start[i] = base + (i * 0x00040000);
155 }
156 }
157 else
158 {
159 /* set sector offsets for top boot block type */
160 i = info->sector_count - 1;
161 for (; i >= 0; i--)
162 {
163 info->start[i] = base + i * 0x00040000;
164 }
165 }
166
167}
168
169/*-----------------------------------------------------------------------
170 */
171void flash_print_info (flash_info_t *info)
172{
173 int i;
174
175 if (info->flash_id == FLASH_UNKNOWN) {
176 printf ("missing or unknown FLASH type\n");
177 return;
178 }
179
180 switch (info->flash_id & FLASH_VENDMASK) {
181 case FLASH_MAN_AMD: printf ("AMD "); break;
182 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
183 default: printf ("Unknown Vendor "); break;
184 }
185
186 switch (info->flash_id & FLASH_TYPEMASK) {
187
188#if 0
189 case FLASH_AM040B:
190 printf ("AM29F040B (4 Mbit, bottom boot sect)\n");
191 break;
192 case FLASH_AM040T:
193 printf ("AM29F040T (4 Mbit, top boot sect)\n");
194 break;
195#endif
196 case FLASH_AM400B:
197 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
198 break;
199 case FLASH_AM400T:
200 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
201 break;
202 case FLASH_AM800B:
203 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
204 break;
205 case FLASH_AM800T:
206 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
207 break;
208 case FLASH_AM160B:
209 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
210 break;
211 case FLASH_AM160T:
212 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
213 break;
214 case FLASH_AM320B:
215 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
216 break;
217 case FLASH_AM320T:
218 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
219 break;
220 default:
221 printf ("Unknown Chip Type\n");
222 break;
223 }
224
225 printf (" Size: %ld MB in %d Sectors\n",
226 info->size >> 20,
227 info->sector_count);
228
229 printf (" Sector Start Addresses:");
230
231 for (i=0; i<info->sector_count; ++i)
232 {
233 if ((i % 5) == 0)
234 {
235 printf ("\n ");
236 }
237
238 printf (" %08lX%s",
239 info->start[i],
240 info->protect[i] ? " (RO)" : " ");
241 }
242
243 printf ("\n");
244 return;
245}
246
247/*-----------------------------------------------------------------------
248 */
249
250
251/*-----------------------------------------------------------------------
252 */
253
254/*
255 * The following code cannot be run from FLASH!
256 */
257
258static ulong flash_get_size (vu_long *addr, flash_info_t *info)
259{
260 short i;
261#if 0
262 ulong base = (ulong)addr;
263#endif
264 uchar value;
265
266 /* Write auto select command: read Manufacturer ID */
267#if 0
268 addr[0x0555] = 0x00AA00AA;
269 addr[0x02AA] = 0x00550055;
270 addr[0x0555] = 0x00900090;
271#else
272 addr[0x0555] = 0xAAAAAAAA;
273 addr[0x02AA] = 0x55555555;
274 addr[0x0555] = 0x90909090;
275#endif
276
277 value = addr[0];
278
279 switch (value)
280 {
281 case AMD_MANUFACT:case 0x01:
282 info->flash_id = FLASH_MAN_AMD;
283 break;
284
285 case FUJ_MANUFACT:
286 info->flash_id = FLASH_MAN_FUJ;
287 break;
288
289 default:
290 info->flash_id = FLASH_UNKNOWN;
291 info->sector_count = 0;
292 info->size = 0;
293 break;
294 }
295
296 value = addr[1]; /* device ID */
297
298 switch (value)
299 {
300#if 0
301 case AMD_ID_F040B:
302 info->flash_id += FLASH_AM040B;
303 info->sector_count = 8;
304 info->size = 0x00200000;
305 break; /* => 2 MB */
306#endif
307 case AMD_ID_LV400T:
308 info->flash_id += FLASH_AM400T;
309 info->sector_count = 11;
310 info->size = 0x00100000;
311 break; /* => 1 MB */
312
313 case AMD_ID_LV400B:
314 info->flash_id += FLASH_AM400B;
315 info->sector_count = 11;
316 info->size = 0x00100000;
317 break; /* => 1 MB */
318
319 case AMD_ID_LV800T:
320 info->flash_id += FLASH_AM800T;
321 info->sector_count = 19;
322 info->size = 0x00200000;
323 break; /* => 2 MB */
324
325 case AMD_ID_LV800B:
326 info->flash_id += FLASH_AM800B;
327 info->sector_count = 19;
328 info->size = 0x00200000;
329 break; /* => 2 MB */
330
331 case AMD_ID_LV160T:
332 info->flash_id += FLASH_AM160T;
333 info->sector_count = 35;
334 info->size = 0x00400000;
335 break; /* => 4 MB */
336
337 case AMD_ID_LV160B:
338 info->flash_id += FLASH_AM160B;
339 info->sector_count = 35;
340 info->size = 0x00400000;
341 break; /* => 4 MB */
342#if 0 /* enable when device IDs are available */
343 case AMD_ID_LV320T:
344 info->flash_id += FLASH_AM320T;
345 info->sector_count = 67;
346 info->size = 0x00800000;
347 break; /* => 8 MB */
348
349 case AMD_ID_LV320B:
350 info->flash_id += FLASH_AM320B;
351 info->sector_count = 67;
352 info->size = 0x00800000;
353 break; /* => 8 MB */
354#endif
355 default:
356 info->flash_id = FLASH_UNKNOWN;
357 return (0); /* => no or unknown flash */
358
359 }
360
361#if 0
362 /* set up sector start adress table */
363 if (info->flash_id & FLASH_BTYPE) {
364 /* set sector offsets for bottom boot block type */
365 info->start[0] = base + 0x00000000;
366 info->start[1] = base + 0x00008000;
367 info->start[2] = base + 0x0000C000;
368 info->start[3] = base + 0x00010000;
369 for (i = 4; i < info->sector_count; i++) {
370 info->start[i] = base + (i * 0x00020000) - 0x00060000;
371 }
372 } else {
373 /* set sector offsets for top boot block type */
374 i = info->sector_count - 1;
375 info->start[i--] = base + info->size - 0x00008000;
376 info->start[i--] = base + info->size - 0x0000C000;
377 info->start[i--] = base + info->size - 0x00010000;
378 for (; i >= 0; i--) {
379 info->start[i] = base + i * 0x00020000;
380 }
381 }
382#else
383 flash_get_offsets ((ulong)addr, &flash_info[0]);
384#endif
385
386 /* check for protected sectors */
387 for (i = 0; i < info->sector_count; i++)
388 {
389 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
390 /* D0 = 1 if protected */
391 addr = (volatile unsigned long *)(info->start[i]);
392 info->protect[i] = addr[2] & 1;
393 }
394
395 /*
396 * Prevent writes to uninitialized FLASH.
397 */
398 if (info->flash_id != FLASH_UNKNOWN)
399 {
400 addr = (volatile unsigned long *)info->start[0];
401#if 0
402 *addr = 0x00F000F0; /* reset bank */
403#else
404 *addr = 0xF0F0F0F0; /* reset bank */
405#endif
406 }
407
408 return (info->size);
409}
410
411
412/*-----------------------------------------------------------------------
413 */
414
415int flash_erase (flash_info_t *info, int s_first, int s_last)
416{
417 vu_long *addr = (vu_long*)(info->start[0]);
418 int flag, prot, sect, l_sect;
419 ulong start, now, last;
420
421 if ((s_first < 0) || (s_first > s_last)) {
422 if (info->flash_id == FLASH_UNKNOWN) {
423 printf ("- missing\n");
424 } else {
425 printf ("- no sectors to erase\n");
426 }
427 return 1;
428 }
429
430 if ((info->flash_id == FLASH_UNKNOWN) ||
431 (info->flash_id > FLASH_AMD_COMP)) {
432 printf ("Can't erase unknown flash type - aborted\n");
433 return 1;
434 }
435
436 prot = 0;
437 for (sect=s_first; sect<=s_last; ++sect) {
438 if (info->protect[sect]) {
439 prot++;
440 }
441 }
442
443 if (prot) {
444 printf ("- Warning: %d protected sectors will not be erased!\n",
445 prot);
446 } else {
447 printf ("\n");
448 }
449
450 l_sect = -1;
451
452 /* Disable interrupts which might cause a timeout here */
453 flag = disable_interrupts();
454
455#if 0
456 addr[0x0555] = 0x00AA00AA;
457 addr[0x02AA] = 0x00550055;
458 addr[0x0555] = 0x00800080;
459 addr[0x0555] = 0x00AA00AA;
460 addr[0x02AA] = 0x00550055;
461#else
462 addr[0x0555] = 0xAAAAAAAA;
463 addr[0x02AA] = 0x55555555;
464 addr[0x0555] = 0x80808080;
465 addr[0x0555] = 0xAAAAAAAA;
466 addr[0x02AA] = 0x55555555;
467#endif
468
469 /* Start erase on unprotected sectors */
470 for (sect = s_first; sect<=s_last; sect++) {
471 if (info->protect[sect] == 0) { /* not protected */
472 addr = (vu_long*)(info->start[sect]);
473#if 0
474 addr[0] = 0x00300030;
475#else
476 addr[0] = 0x30303030;
477#endif
478 l_sect = sect;
479 }
480 }
481
482 /* re-enable interrupts if necessary */
483 if (flag)
484 enable_interrupts();
485
486 /* wait at least 80us - let's wait 1 ms */
487 udelay (1000);
488
489 /*
490 * We wait for the last triggered sector
491 */
492 if (l_sect < 0)
493 goto DONE;
494
495 start = get_timer (0);
496 last = start;
497 addr = (vu_long*)(info->start[l_sect]);
498#if 0
499 while ((addr[0] & 0x00800080) != 0x00800080)
500#else
501 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
502#endif
503 {
504 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
505 printf ("Timeout\n");
506 return 1;
507 }
508 /* show that we're waiting */
509 if ((now - last) > 1000) { /* every second */
510 putc ('.');
511 last = now;
512 }
513 }
514
515DONE:
516 /* reset to read mode */
517 addr = (volatile unsigned long *)info->start[0];
518#if 0
519 addr[0] = 0x00F000F0; /* reset bank */
520#else
521 addr[0] = 0xF0F0F0F0; /* reset bank */
522#endif
523
524 printf (" done\n");
525 return 0;
526}
527
528/*-----------------------------------------------------------------------
529 * Copy memory to flash, returns:
530 * 0 - OK
531 * 1 - write timeout
532 * 2 - Flash not erased
533 */
534
535int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
536{
537 ulong cp, wp, data;
538 int i, l, rc;
539
540 wp = (addr & ~3); /* get lower word aligned address */
541
542 /*
543 * handle unaligned start bytes
544 */
545 if ((l = addr - wp) != 0) {
546 data = 0;
547 for (i=0, cp=wp; i<l; ++i, ++cp) {
548 data = (data << 8) | (*(uchar *)cp);
549 }
550 for (; i<4 && cnt>0; ++i) {
551 data = (data << 8) | *src++;
552 --cnt;
553 ++cp;
554 }
555 for (; cnt==0 && i<4; ++i, ++cp) {
556 data = (data << 8) | (*(uchar *)cp);
557 }
558
559 if ((rc = write_word(info, wp, data)) != 0) {
560 return (rc);
561 }
562 wp += 4;
563 }
564
565 /*
566 * handle word aligned part
567 */
568 while (cnt >= 4) {
569 data = 0;
570 for (i=0; i<4; ++i) {
571 data = (data << 8) | *src++;
572 }
573 if ((rc = write_word(info, wp, data)) != 0) {
574 return (rc);
575 }
576 wp += 4;
577 cnt -= 4;
578 }
579
580 if (cnt == 0) {
581 return (0);
582 }
583
584 /*
585 * handle unaligned tail bytes
586 */
587 data = 0;
588 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
589 data = (data << 8) | *src++;
590 --cnt;
591 }
592 for (; i<4; ++i, ++cp) {
593 data = (data << 8) | (*(uchar *)cp);
594 }
595
596 return (write_word(info, wp, data));
597}
598/*-----------------------------------------------------------------------
599 * Write a word to Flash, returns:
600 * 0 - OK
601 * 1 - write timeout
602 * 2 - Flash not erased
603 */
604static int write_word (flash_info_t *info, ulong dest, ulong data)
605{
606 vu_long *addr = (vu_long*)(info->start[0]);
607 ulong start;
608 int flag;
609
610 /* Check if Flash is (sufficiently) erased */
611 if ((*((vu_long *)dest) & data) != data) {
612 return (2);
613 }
614 /* Disable interrupts which might cause a timeout here */
615 flag = disable_interrupts();
616
617#if 0
618 addr[0x0555] = 0x00AA00AA;
619 addr[0x02AA] = 0x00550055;
620 addr[0x0555] = 0x00A000A0;
621#else
622 addr[0x0555] = 0xAAAAAAAA;
623 addr[0x02AA] = 0x55555555;
624 addr[0x0555] = 0xA0A0A0A0;
625#endif
626
627 *((vu_long *)dest) = data;
628
629 /* re-enable interrupts if necessary */
630 if (flag)
631 enable_interrupts();
632
633 /* data polling for D7 */
634 start = get_timer (0);
635#if 0
636 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080))
637#else
638 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080))
639#endif
640 {
641 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
642 return (1);
643 }
644 }
645 return (0);
646}
647
648/*-----------------------------------------------------------------------
649 */