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