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