blob: 98bea0e00505eb09daa9e2213bdb3f141c626402 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2000-2002
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
wdenk73a8b272003-06-05 19:27:42 +000024/* #define DEBUG */
25
wdenkc6097192002-11-03 00:24:07 +000026#include <common.h>
27#include <mpc8xx.h>
28
29#ifndef CFG_ENV_ADDR
30#define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
31#endif
32
33flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
34
35/*-----------------------------------------------------------------------
36 * Functions
37 */
38static ulong flash_get_size (vu_long *addr, flash_info_t *info);
39static int write_word (flash_info_t *info, ulong dest, ulong data);
40
41/*-----------------------------------------------------------------------
42 */
43
44unsigned long flash_init (void)
45{
46 volatile immap_t *immap = (immap_t *)CFG_IMMR;
47 volatile memctl8xx_t *memctl = &immap->im_memctl;
48 unsigned long size_b0, size_b1;
49 int i;
50
51 /* Init: no FLASHes known */
52 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
53 flash_info[i].flash_id = FLASH_UNKNOWN;
54 }
55
56 /* Static FLASH Bank configuration here - FIXME XXX */
57
wdenk73a8b272003-06-05 19:27:42 +000058 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
59
wdenkc6097192002-11-03 00:24:07 +000060 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
61
wdenk73a8b272003-06-05 19:27:42 +000062 debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
63
wdenkc6097192002-11-03 00:24:07 +000064 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
65 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
66 size_b0, size_b0<<20);
67 }
68
69 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
70
wdenk73a8b272003-06-05 19:27:42 +000071 debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
72
wdenkc6097192002-11-03 00:24:07 +000073 if (size_b1 > size_b0) {
74 printf ("## ERROR: "
75 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
76 size_b1, size_b1<<20,
77 size_b0, size_b0<<20
78 );
79 flash_info[0].flash_id = FLASH_UNKNOWN;
80 flash_info[1].flash_id = FLASH_UNKNOWN;
81 flash_info[0].sector_count = -1;
82 flash_info[1].sector_count = -1;
83 flash_info[0].size = 0;
84 flash_info[1].size = 0;
85 return (0);
86 }
87
wdenk73a8b272003-06-05 19:27:42 +000088 debug ("## Before remap: "
89 "BR0: 0x%08x OR0: 0x%08x "
90 "BR1: 0x%08x OR1: 0x%08x\n",
91 memctl->memc_br0, memctl->memc_or0,
92 memctl->memc_br1, memctl->memc_or1);
93
wdenkc6097192002-11-03 00:24:07 +000094 /* Remap FLASH according to real size */
95 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
96 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
97
wdenk73a8b272003-06-05 19:27:42 +000098 debug ("## BR0: 0x%08x OR0: 0x%08x\n",
99 memctl->memc_br0, memctl->memc_or0);
100
wdenkc6097192002-11-03 00:24:07 +0000101 /* Re-do sizing to get full correct info */
102 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
103
104#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
105 /* monitor protection ON by default */
106 flash_protect(FLAG_PROTECT_SET,
107 CFG_MONITOR_BASE,
wdenk3b57fe02003-05-30 12:48:29 +0000108 CFG_MONITOR_BASE+monitor_flash_len-1,
wdenkc6097192002-11-03 00:24:07 +0000109 &flash_info[0]);
110#endif
111
112#ifdef CFG_ENV_IS_IN_FLASH
113 /* ENV protection ON by default */
114 flash_protect(FLAG_PROTECT_SET,
115 CFG_ENV_ADDR,
116 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
117 &flash_info[0]);
118#endif
119
120 if (size_b1) {
121 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
122 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
123 BR_MS_GPCM | BR_V;
124
wdenk73a8b272003-06-05 19:27:42 +0000125 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
126 memctl->memc_br1, memctl->memc_or1);
127
wdenkc6097192002-11-03 00:24:07 +0000128 /* Re-do sizing to get full correct info */
129 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
130 &flash_info[1]);
131
132#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
133 /* monitor protection ON by default */
134 flash_protect(FLAG_PROTECT_SET,
135 CFG_MONITOR_BASE,
wdenk3b57fe02003-05-30 12:48:29 +0000136 CFG_MONITOR_BASE+monitor_flash_len-1,
wdenkc6097192002-11-03 00:24:07 +0000137 &flash_info[1]);
138#endif
139
140#ifdef CFG_ENV_IS_IN_FLASH
141 /* ENV protection ON by default */
142 flash_protect(FLAG_PROTECT_SET,
143 CFG_ENV_ADDR,
144 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
145 &flash_info[1]);
146#endif
147 } else {
148 memctl->memc_br1 = 0; /* invalidate bank */
149
150 flash_info[1].flash_id = FLASH_UNKNOWN;
151 flash_info[1].sector_count = -1;
wdenk73a8b272003-06-05 19:27:42 +0000152 flash_info[1].size = 0;
153
154 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
155 memctl->memc_br1, memctl->memc_or1);
wdenkc6097192002-11-03 00:24:07 +0000156 }
157
wdenk73a8b272003-06-05 19:27:42 +0000158 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
159
wdenkc6097192002-11-03 00:24:07 +0000160 flash_info[0].size = size_b0;
161 flash_info[1].size = size_b1;
162
163 return (size_b0 + size_b1);
164}
165
166/*-----------------------------------------------------------------------
167 */
168void flash_print_info (flash_info_t *info)
169{
170 int i;
171
172 if (info->flash_id == FLASH_UNKNOWN) {
173 printf ("missing or unknown FLASH type\n");
174 return;
175 }
176
177 switch (info->flash_id & FLASH_VENDMASK) {
178 case FLASH_MAN_AMD: printf ("AMD "); break;
179 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
180 default: printf ("Unknown Vendor "); break;
181 }
182
183 switch (info->flash_id & FLASH_TYPEMASK) {
184 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",
205 info->size >> 20, info->sector_count);
206
207 printf (" Sector Start Addresses:");
208 for (i=0; i<info->sector_count; ++i) {
209 if ((i % 5) == 0)
210 printf ("\n ");
211 printf (" %08lX%s",
212 info->start[i],
213 info->protect[i] ? " (RO)" : " "
214 );
215 }
216 printf ("\n");
217 return;
218}
219
220/*-----------------------------------------------------------------------
221 */
222
223
224/*-----------------------------------------------------------------------
225 */
226
227/*
228 * The following code cannot be run from FLASH!
229 */
230
231static ulong flash_get_size (vu_long *addr, flash_info_t *info)
232{
233 short i;
234 ulong value;
235 ulong base = (ulong)addr;
236
237 /* Write auto select command: read Manufacturer ID */
238 addr[0x0555] = 0x00AA00AA;
239 addr[0x02AA] = 0x00550055;
240 addr[0x0555] = 0x00900090;
241
242 value = addr[0];
243
wdenk73a8b272003-06-05 19:27:42 +0000244 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
245
wdenkc6097192002-11-03 00:24:07 +0000246 switch (value) {
247 case AMD_MANUFACT:
248 info->flash_id = FLASH_MAN_AMD;
249 break;
250 case FUJ_MANUFACT:
251 info->flash_id = FLASH_MAN_FUJ;
252 break;
253 default:
254 info->flash_id = FLASH_UNKNOWN;
255 info->sector_count = 0;
256 info->size = 0;
257 return (0); /* no or unknown flash */
258 }
259
260 value = addr[1]; /* device ID */
261
wdenk73a8b272003-06-05 19:27:42 +0000262 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
263
wdenkc6097192002-11-03 00:24:07 +0000264 switch (value) {
265 case AMD_ID_LV400T:
266 info->flash_id += FLASH_AM400T;
267 info->sector_count = 11;
268 info->size = 0x00100000;
269 break; /* => 1 MB */
270
271 case AMD_ID_LV400B:
272 info->flash_id += FLASH_AM400B;
273 info->sector_count = 11;
274 info->size = 0x00100000;
275 break; /* => 1 MB */
276
277 case AMD_ID_LV800T:
278 info->flash_id += FLASH_AM800T;
279 info->sector_count = 19;
280 info->size = 0x00200000;
281 break; /* => 2 MB */
282
283 case AMD_ID_LV800B:
284 info->flash_id += FLASH_AM800B;
285 info->sector_count = 19;
286 info->size = 0x00200000;
287 break; /* => 2 MB */
288
289 case AMD_ID_LV160T:
290 info->flash_id += FLASH_AM160T;
291 info->sector_count = 35;
292 info->size = 0x00400000;
293 break; /* => 4 MB */
294
295 case AMD_ID_LV160B:
296 info->flash_id += FLASH_AM160B;
297 info->sector_count = 35;
298 info->size = 0x00400000;
299 break; /* => 4 MB */
300 case AMD_ID_LV320T:
301 info->flash_id += FLASH_AM320T;
302 info->sector_count = 71;
303 info->size = 0x00800000;
304 break; /* => 8 MB */
305
306 case AMD_ID_LV320B:
307 info->flash_id += FLASH_AM320B;
308 info->sector_count = 71;
309 info->size = 0x00800000;
310 break; /* => 8 MB */
wdenk73a8b272003-06-05 19:27:42 +0000311 case AMD_ID_DL640:
312debug ("## oops - same ID used for AM29LV128ML/H mirror bit flash ???\n");
313 info->flash_id += FLASH_AMDL640;
314 info->sector_count = 142;
315 info->size = 0x00800000;
316 break;
wdenkc6097192002-11-03 00:24:07 +0000317 default:
318 info->flash_id = FLASH_UNKNOWN;
319 return (0); /* => no or unknown flash */
320 }
321
322 /* set up sector start address table */
323 switch (value) {
324 case AMD_ID_LV400B:
325 case AMD_ID_LV800B:
326 case AMD_ID_LV160B:
327 /* set sector offsets for bottom boot block type */
328 info->start[0] = base + 0x00000000;
329 info->start[1] = base + 0x00008000;
330 info->start[2] = base + 0x0000C000;
331 info->start[3] = base + 0x00010000;
332 for (i = 4; i < info->sector_count; i++) {
333 info->start[i] = base + (i * 0x00020000) - 0x00060000;
334 }
335 break;
336 case AMD_ID_LV400T:
337 case AMD_ID_LV800T:
338 case AMD_ID_LV160T:
339 /* set sector offsets for top boot block type */
340 i = info->sector_count - 1;
341 info->start[i--] = base + info->size - 0x00008000;
342 info->start[i--] = base + info->size - 0x0000C000;
343 info->start[i--] = base + info->size - 0x00010000;
344 for (; i >= 0; i--) {
345 info->start[i] = base + i * 0x00020000;
346 }
347 break;
348 case AMD_ID_LV320B:
349 for (i = 0; i < info->sector_count; i++) {
350 info->start[i] = base;
351 /*
352 * The first 8 sectors are 8 kB,
353 * all the other ones are 64 kB
354 */
355 base += (i < 8)
356 ? 2 * ( 8 << 10)
357 : 2 * (64 << 10);
358 }
359 break;
360 case AMD_ID_LV320T:
361 for (i = 0; i < info->sector_count; i++) {
362 info->start[i] = base;
363 /*
364 * The last 8 sectors are 8 kB,
365 * all the other ones are 64 kB
366 */
367 base += (i < (info->sector_count - 8))
368 ? 2 * (64 << 10)
369 : 2 * ( 8 << 10);
370 }
371 break;
372 default:
373 return (0);
374 break;
375 }
376
377 /* check for protected sectors */
378 for (i = 0; i < info->sector_count; i++) {
379 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
380 /* D0 = 1 if protected */
381 addr = (volatile unsigned long *)(info->start[i]);
382 info->protect[i] = addr[2] & 1;
383 }
384
385 /*
386 * Prevent writes to uninitialized FLASH.
387 */
388 if (info->flash_id != FLASH_UNKNOWN) {
389 addr = (volatile unsigned long *)info->start[0];
390
391 *addr = 0x00F000F0; /* reset bank */
392 }
393
394 return (info->size);
395}
396
397
398/*-----------------------------------------------------------------------
399 */
400
401int flash_erase (flash_info_t *info, int s_first, int s_last)
402{
403 vu_long *addr = (vu_long*)(info->start[0]);
404 int flag, prot, sect, l_sect;
405 ulong start, now, last;
406
wdenk73a8b272003-06-05 19:27:42 +0000407 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
408
wdenkc6097192002-11-03 00:24:07 +0000409 if ((s_first < 0) || (s_first > s_last)) {
410 if (info->flash_id == FLASH_UNKNOWN) {
411 printf ("- missing\n");
412 } else {
413 printf ("- no sectors to erase\n");
414 }
415 return 1;
416 }
417
418 if ((info->flash_id == FLASH_UNKNOWN) ||
419 (info->flash_id > FLASH_AMD_COMP)) {
420 printf ("Can't erase unknown flash type %08lx - aborted\n",
421 info->flash_id);
422 return 1;
423 }
424
425 prot = 0;
426 for (sect=s_first; sect<=s_last; ++sect) {
427 if (info->protect[sect]) {
428 prot++;
429 }
430 }
431
432 if (prot) {
433 printf ("- Warning: %d protected sectors will not be erased!\n",
434 prot);
435 } else {
436 printf ("\n");
437 }
438
439 l_sect = -1;
440
441 /* Disable interrupts which might cause a timeout here */
442 flag = disable_interrupts();
443
444 addr[0x0555] = 0x00AA00AA;
445 addr[0x02AA] = 0x00550055;
446 addr[0x0555] = 0x00800080;
447 addr[0x0555] = 0x00AA00AA;
448 addr[0x02AA] = 0x00550055;
449
450 /* Start erase on unprotected sectors */
451 for (sect = s_first; sect<=s_last; sect++) {
452 if (info->protect[sect] == 0) { /* not protected */
453 addr = (vu_long*)(info->start[sect]);
454 addr[0] = 0x00300030;
455 l_sect = sect;
456 }
457 }
458
459 /* re-enable interrupts if necessary */
460 if (flag)
461 enable_interrupts();
462
463 /* wait at least 80us - let's wait 1 ms */
464 udelay (1000);
465
466 /*
467 * We wait for the last triggered sector
468 */
469 if (l_sect < 0)
470 goto DONE;
471
472 start = get_timer (0);
473 last = start;
474 addr = (vu_long*)(info->start[l_sect]);
475 while ((addr[0] & 0x00800080) != 0x00800080) {
476 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
477 printf ("Timeout\n");
478 return 1;
479 }
480 /* show that we're waiting */
481 if ((now - last) > 1000) { /* every second */
482 putc ('.');
483 last = now;
484 }
485 }
486
487DONE:
488 /* reset to read mode */
489 addr = (volatile unsigned long *)info->start[0];
490 addr[0] = 0x00F000F0; /* reset bank */
491
492 printf (" done\n");
493 return 0;
494}
495
496/*-----------------------------------------------------------------------
497 * Copy memory to flash, returns:
498 * 0 - OK
499 * 1 - write timeout
500 * 2 - Flash not erased
501 */
502
503int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
504{
505 ulong cp, wp, data;
506 int i, l, rc;
507
508 wp = (addr & ~3); /* get lower word aligned address */
509
510 /*
511 * handle unaligned start bytes
512 */
513 if ((l = addr - wp) != 0) {
514 data = 0;
515 for (i=0, cp=wp; i<l; ++i, ++cp) {
516 data = (data << 8) | (*(uchar *)cp);
517 }
518 for (; i<4 && cnt>0; ++i) {
519 data = (data << 8) | *src++;
520 --cnt;
521 ++cp;
522 }
523 for (; cnt==0 && i<4; ++i, ++cp) {
524 data = (data << 8) | (*(uchar *)cp);
525 }
526
527 if ((rc = write_word(info, wp, data)) != 0) {
528 return (rc);
529 }
530 wp += 4;
531 }
532
533 /*
534 * handle word aligned part
535 */
536 while (cnt >= 4) {
537 data = 0;
538 for (i=0; i<4; ++i) {
539 data = (data << 8) | *src++;
540 }
541 if ((rc = write_word(info, wp, data)) != 0) {
542 return (rc);
543 }
544 wp += 4;
545 cnt -= 4;
546 }
547
548 if (cnt == 0) {
549 return (0);
550 }
551
552 /*
553 * handle unaligned tail bytes
554 */
555 data = 0;
556 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
557 data = (data << 8) | *src++;
558 --cnt;
559 }
560 for (; i<4; ++i, ++cp) {
561 data = (data << 8) | (*(uchar *)cp);
562 }
563
564 return (write_word(info, wp, data));
565}
566
567/*-----------------------------------------------------------------------
568 * Write a word to Flash, returns:
569 * 0 - OK
570 * 1 - write timeout
571 * 2 - Flash not erased
572 */
573static int write_word (flash_info_t *info, ulong dest, ulong data)
574{
575 vu_long *addr = (vu_long*)(info->start[0]);
576 ulong start;
577 int flag;
578
579 /* Check if Flash is (sufficiently) erased */
580 if ((*((vu_long *)dest) & data) != data) {
581 return (2);
582 }
583 /* Disable interrupts which might cause a timeout here */
584 flag = disable_interrupts();
585
586 addr[0x0555] = 0x00AA00AA;
587 addr[0x02AA] = 0x00550055;
588 addr[0x0555] = 0x00A000A0;
589
590 *((vu_long *)dest) = data;
591
592 /* re-enable interrupts if necessary */
593 if (flag)
594 enable_interrupts();
595
596 /* data polling for D7 */
597 start = get_timer (0);
598 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
599 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
600 return (1);
601 }
602 }
603 return (0);
604}
605
606/*-----------------------------------------------------------------------
607 */