blob: 50b496ed97102d62989cb4814840024753f7c7f9 [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 */
150 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
151 /* 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;
182 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
183 break;
184 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
185 break;
186 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
187 break;
188 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
189 break;
190 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
191 break;
192 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
193 break;
194 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
195 break;
196 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
197 break;
198 default: printf ("Unknown Chip Type\n");
199 break;
200 }
201
202 printf (" Size: %ld MB in %d Sectors\n",info->size >> 20, info->sector_count);
203
204 printf (" Sector Start Addresses:");
205
206 for (i=0; i<info->sector_count; ++i)
207 {
208 if ((i % 5) == 0)
209 {
210 printf ("\n ");
211 }
212
213 printf (" %08lX%s",
214 info->start[i],
215 info->protect[i] ? " (RO)" : " ");
216 }
217
218 printf ("\n");
219 return;
220}
221
222/*-----------------------------------------------------------------------
223 */
224
225
226/*-----------------------------------------------------------------------
227 */
228
229/*
230 * The following code cannot be run from FLASH!
231 */
232
233static ulong flash_get_size (vu_long *addr, flash_info_t *info)
234{
235 short i;
236#if 0
237 ulong base = (ulong)addr;
238#endif
239 uchar value;
240
241 /* Write auto select command: read Manufacturer ID */
242#if 0
243 addr[0x0555] = 0x00AA00AA;
244 addr[0x02AA] = 0x00550055;
245 addr[0x0555] = 0x00900090;
246#else
247 addr[0x0555] = 0xAAAAAAAA;
248 addr[0x02AA] = 0x55555555;
249 addr[0x0555] = 0x90909090;
250#endif
251
252 value = addr[0];
253
254 switch (value + (value << 16))
255 {
256 case AMD_MANUFACT:
257 info->flash_id = FLASH_MAN_AMD;
258 break;
259
260 case FUJ_MANUFACT:
261 info->flash_id = FLASH_MAN_FUJ;
262 break;
263
264 default:
265 info->flash_id = FLASH_UNKNOWN;
266 info->sector_count = 0;
267 info->size = 0;
268 break;
269 }
270
271 value = addr[1]; /* device ID */
272
273 switch (value)
274 {
275 case AMD_ID_F040B:
276 info->flash_id += FLASH_AM040;
277 info->sector_count = 8;
278 info->size = 0x00200000;
279 break; /* => 2 MB */
280
281 case AMD_ID_LV400T:
282 info->flash_id += FLASH_AM400T;
283 info->sector_count = 11;
284 info->size = 0x00100000;
285 break; /* => 1 MB */
286
287 case AMD_ID_LV400B:
288 info->flash_id += FLASH_AM400B;
289 info->sector_count = 11;
290 info->size = 0x00100000;
291 break; /* => 1 MB */
292
293 case AMD_ID_LV800T:
294 info->flash_id += FLASH_AM800T;
295 info->sector_count = 19;
296 info->size = 0x00200000;
297 break; /* => 2 MB */
298
299 case AMD_ID_LV800B:
300 info->flash_id += FLASH_AM800B;
301 info->sector_count = 19;
302 info->size = 0x00200000;
303 break; /* => 2 MB */
304
305 case AMD_ID_LV160T:
306 info->flash_id += FLASH_AM160T;
307 info->sector_count = 35;
308 info->size = 0x00400000;
309 break; /* => 4 MB */
310
311 case AMD_ID_LV160B:
312 info->flash_id += FLASH_AM160B;
313 info->sector_count = 35;
314 info->size = 0x00400000;
315 break; /* => 4 MB */
316#if 0 /* enable when device IDs are available */
317 case AMD_ID_LV320T:
318 info->flash_id += FLASH_AM320T;
319 info->sector_count = 67;
320 info->size = 0x00800000;
321 break; /* => 8 MB */
322
323 case AMD_ID_LV320B:
324 info->flash_id += FLASH_AM320B;
325 info->sector_count = 67;
326 info->size = 0x00800000;
327 break; /* => 8 MB */
328#endif
329 default:
330 info->flash_id = FLASH_UNKNOWN;
331 return (0); /* => no or unknown flash */
332
333 }
334
335#if 0
336 /* set up sector start address table */
337 if (info->flash_id & FLASH_BTYPE) {
338 /* set sector offsets for bottom boot block type */
339 info->start[0] = base + 0x00000000;
340 info->start[1] = base + 0x00008000;
341 info->start[2] = base + 0x0000C000;
342 info->start[3] = base + 0x00010000;
343 for (i = 4; i < info->sector_count; i++) {
344 info->start[i] = base + (i * 0x00020000) - 0x00060000;
345 }
346 } else {
347 /* set sector offsets for top boot block type */
348 i = info->sector_count - 1;
349 info->start[i--] = base + info->size - 0x00008000;
350 info->start[i--] = base + info->size - 0x0000C000;
351 info->start[i--] = base + info->size - 0x00010000;
352 for (; i >= 0; i--) {
353 info->start[i] = base + i * 0x00020000;
354 }
355 }
356#else
357 flash_get_offsets ((ulong)addr, info);
358#endif
359
360 /* check for protected sectors */
361 for (i = 0; i < info->sector_count; i++)
362 {
363 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
364 /* D0 = 1 if protected */
365 addr = (volatile unsigned long *)(info->start[i]);
366 info->protect[i] = addr[2] & 1;
367 }
368
369 /*
370 * Prevent writes to uninitialized FLASH.
371 */
372 if (info->flash_id != FLASH_UNKNOWN)
373 {
374 addr = (volatile unsigned long *)info->start[0];
375#if 0
376 *addr = 0x00F000F0; /* reset bank */
377#else
378 *addr = 0xF0F0F0F0; /* reset bank */
379#endif
380 }
381
382 return (info->size);
383}
384
385
386/*-----------------------------------------------------------------------
387 */
388
389int flash_erase (flash_info_t *info, int s_first, int s_last)
390{
391 vu_long *addr = (vu_long*)(info->start[0]);
392 int flag, prot, sect, l_sect;
393 ulong start, now, last;
394
395 if ((s_first < 0) || (s_first > s_last)) {
396 if (info->flash_id == FLASH_UNKNOWN) {
397 printf ("- missing\n");
398 } else {
399 printf ("- no sectors to erase\n");
400 }
401 return 1;
402 }
403
404 if ((info->flash_id == FLASH_UNKNOWN) ||
405 (info->flash_id > FLASH_AMD_COMP)) {
406 printf ("Can't erase unknown flash type - aborted\n");
407 return 1;
408 }
409
410 prot = 0;
411 for (sect=s_first; sect<=s_last; ++sect) {
412 if (info->protect[sect]) {
413 prot++;
414 }
415 }
416
417 if (prot) {
418 printf ("- Warning: %d protected sectors will not be erased!\n",
419 prot);
420 } else {
421 printf ("\n");
422 }
423
424 l_sect = -1;
425
426 /* Disable interrupts which might cause a timeout here */
427 flag = disable_interrupts();
428
429#if 0
430 addr[0x0555] = 0x00AA00AA;
431 addr[0x02AA] = 0x00550055;
432 addr[0x0555] = 0x00800080;
433 addr[0x0555] = 0x00AA00AA;
434 addr[0x02AA] = 0x00550055;
435#else
436 addr[0x0555] = 0xAAAAAAAA;
437 addr[0x02AA] = 0x55555555;
438 addr[0x0555] = 0x80808080;
439 addr[0x0555] = 0xAAAAAAAA;
440 addr[0x02AA] = 0x55555555;
441#endif
442
443 /* Start erase on unprotected sectors */
444 for (sect = s_first; sect<=s_last; sect++) {
445 if (info->protect[sect] == 0) { /* not protected */
446 addr = (vu_long*)(info->start[sect]);
447#if 0
448 addr[0] = 0x00300030;
449#else
450 addr[0] = 0x30303030;
451#endif
452 l_sect = sect;
453 }
454 }
455
456 /* re-enable interrupts if necessary */
457 if (flag)
458 enable_interrupts();
459
460 /* wait at least 80us - let's wait 1 ms */
461 udelay (1000);
462
463 /*
464 * We wait for the last triggered sector
465 */
466 if (l_sect < 0)
467 goto DONE;
468
469 start = get_timer (0);
470 last = start;
471 addr = (vu_long*)(info->start[l_sect]);
472#if 0
473 while ((addr[0] & 0x00800080) != 0x00800080)
474#else
475 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
476#endif
477 {
478 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
479 printf ("Timeout\n");
480 return 1;
481 }
482 /* show that we're waiting */
483 if ((now - last) > 1000) { /* every second */
484 putc ('.');
485 last = now;
486 }
487 }
488
489DONE:
490 /* reset to read mode */
491 addr = (volatile unsigned long *)info->start[0];
492#if 0
493 addr[0] = 0x00F000F0; /* reset bank */
494#else
495 addr[0] = 0xF0F0F0F0; /* reset bank */
496#endif
497
498 printf (" done\n");
499 return 0;
500}
501
502/*-----------------------------------------------------------------------
503 * Copy memory to flash, returns:
504 * 0 - OK
505 * 1 - write timeout
506 * 2 - Flash not erased
507 */
508
509int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
510{
511 ulong cp, wp, data;
512 int i, l, rc;
513
514 wp = (addr & ~3); /* get lower word aligned address */
515
516 /*
517 * handle unaligned start bytes
518 */
519 if ((l = addr - wp) != 0) {
520 data = 0;
521 for (i=0, cp=wp; i<l; ++i, ++cp) {
522 data = (data << 8) | (*(uchar *)cp);
523 }
524 for (; i<4 && cnt>0; ++i) {
525 data = (data << 8) | *src++;
526 --cnt;
527 ++cp;
528 }
529 for (; cnt==0 && i<4; ++i, ++cp) {
530 data = (data << 8) | (*(uchar *)cp);
531 }
532
533 if ((rc = write_word(info, wp, data)) != 0) {
534 return (rc);
535 }
536 wp += 4;
537 }
538
539 /*
540 * handle word aligned part
541 */
542 while (cnt >= 4) {
543 data = 0;
544 for (i=0; i<4; ++i) {
545 data = (data << 8) | *src++;
546 }
547 if ((rc = write_word(info, wp, data)) != 0) {
548 return (rc);
549 }
550 wp += 4;
551 cnt -= 4;
552 }
553
554 if (cnt == 0) {
555 return (0);
556 }
557
558 /*
559 * handle unaligned tail bytes
560 */
561 data = 0;
562 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
563 data = (data << 8) | *src++;
564 --cnt;
565 }
566 for (; i<4; ++i, ++cp) {
567 data = (data << 8) | (*(uchar *)cp);
568 }
569
570 return (write_word(info, wp, data));
571}
572
573/*-----------------------------------------------------------------------
574 * Write a word to Flash, returns:
575 * 0 - OK
576 * 1 - write timeout
577 * 2 - Flash not erased
578 */
579static int write_word (flash_info_t *info, ulong dest, ulong data)
580{
581 vu_long *addr = (vu_long*)(info->start[0]);
582 ulong start;
583 int flag;
584
585 /* Check if Flash is (sufficiently) erased */
586 if ((*((vu_long *)dest) & data) != data) {
587 return (2);
588 }
589 /* Disable interrupts which might cause a timeout here */
590 flag = disable_interrupts();
591
592#if 0
593 addr[0x0555] = 0x00AA00AA;
594 addr[0x02AA] = 0x00550055;
595 addr[0x0555] = 0x00A000A0;
596#else
597 addr[0x0555] = 0xAAAAAAAA;
598 addr[0x02AA] = 0x55555555;
599 addr[0x0555] = 0xA0A0A0A0;
600#endif
601
602 *((vu_long *)dest) = data;
603
604 /* re-enable interrupts if necessary */
605 if (flag)
606 enable_interrupts();
607
608 /* data polling for D7 */
609 start = get_timer (0);
610#if 0
611 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080))
612#else
613 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080))
614#endif
615 {
616 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
617 return (1);
618 }
619 }
620 return (0);
621}
622
623/*-----------------------------------------------------------------------
624 */