blob: 7f72258f1aeba45cc725fe4e3981202fd97bd63b [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020027flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000028
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020029#if defined(CONFIG_ENV_IS_IN_FLASH)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020030# ifndef CONFIG_ENV_ADDR
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020031# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenkaffae2b2002-08-17 09:36:01 +000032# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020033# ifndef CONFIG_ENV_SIZE
34# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
wdenkaffae2b2002-08-17 09:36:01 +000035# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020036# ifndef CONFIG_ENV_SECT_SIZE
37# define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
wdenkaffae2b2002-08-17 09:36:01 +000038# 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{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020053 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
wdenkaffae2b2002-08-17 09:36:01 +000054 volatile memctl8xx_t *memctl = &immap->im_memctl;
Wolfgang Denk13039eb2011-11-04 15:55:33 +000055 unsigned long size_b0;
wdenkaffae2b2002-08-17 09:36:01 +000056 int i;
57
58 /* Init: no FLASHes known */
Wolfgang Denk13039eb2011-11-04 15:55:33 +000059 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
wdenkaffae2b2002-08-17 09:36:01 +000060 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenkaffae2b2002-08-17 09:36:01 +000061
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 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
68 size_b0,
69 size_b0 >> 20);
70 }
71
wdenkaffae2b2002-08-17 09:36:01 +000072 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
74 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
wdenkaffae2b2002-08-17 09:36:01 +000075
76 /* Re-do sizing to get full correct info */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020077 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000078
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000080
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020081#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +000082 /* monitor protection ON by default */
83 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020084 CONFIG_SYS_MONITOR_BASE,
85 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +000086 &flash_info[0]);
87#endif
88
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020089#ifdef CONFIG_ENV_IS_IN_FLASH
wdenkaffae2b2002-08-17 09:36:01 +000090 /* ENV protection ON by default */
91 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020092 CONFIG_ENV_ADDR,
93 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
wdenkaffae2b2002-08-17 09:36:01 +000094 &flash_info[0]);
95#endif
96
97 /* ICU862 Board has only one Flash Bank */
wdenkaffae2b2002-08-17 09:36:01 +000098 flash_info[0].size = size_b0;
wdenkaffae2b2002-08-17 09:36:01 +000099
Wolfgang Denk13039eb2011-11-04 15:55:33 +0000100 return size_b0;
wdenkaffae2b2002-08-17 09:36:01 +0000101
102}
103
104/*-----------------------------------------------------------------------
105 */
106static void flash_get_offsets (ulong base, flash_info_t *info)
107{
108 int i;
109
110 /* set up sector start address table */
111 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
112 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM033C)) {
113 /* set sector offsets for uniform sector type */
114 for (i = 0; i < info->sector_count; i++) {
115 info->start[i] = base + (i * 0x00040000);
116 }
117 }
118}
119
120/*-----------------------------------------------------------------------
121 */
122void flash_print_info (flash_info_t *info)
123{
124 int i;
125
126 if (info->flash_id == FLASH_UNKNOWN) {
127 puts ("missing or unknown FLASH type\n");
128 return;
129 }
130
131 switch (info->flash_id & FLASH_VENDMASK) {
132 case FLASH_MAN_AMD: puts ("AMD "); break;
133 case FLASH_MAN_FUJ: puts ("FUJITSU "); break;
134 case FLASH_MAN_BM: puts ("BRIGHT MICRO "); break;
135 default: puts ("Unknown Vendor "); break;
136 }
137
138 switch (info->flash_id & FLASH_TYPEMASK) {
139 case FLASH_AM040: puts ("29F040/29LV040 (4 Mbit, uniform sectors)\n");
140 break;
141 case FLASH_AM400B: puts ("AM29LV400B (4 Mbit, bottom boot sect)\n");
142 break;
143 case FLASH_AM400T: puts ("AM29LV400T (4 Mbit, top boot sector)\n");
144 break;
145 case FLASH_AM800B: puts ("AM29LV800B (8 Mbit, bottom boot sect)\n");
146 break;
147 case FLASH_AM800T: puts ("AM29LV800T (8 Mbit, top boot sector)\n");
148 break;
149 case FLASH_AM160B: puts ("AM29LV160B (16 Mbit, bottom boot sect)\n");
150 break;
151 case FLASH_AM160T: puts ("AM29LV160T (16 Mbit, top boot sector)\n");
152 break;
153 case FLASH_AM320B: puts ("AM29LV320B (32 Mbit, bottom boot sect)\n");
154 break;
155 case FLASH_AM320T: puts ("AM29LV320T (32 Mbit, top boot sector)\n");
156 break;
157 case FLASH_AM033C: puts ("AM29LV033C (32 Mbit)\n");
158 break;
159 default: puts ("Unknown Chip Type\n");
160 break;
161 }
162
163 printf (" Size: %ld MB in %d Sectors\n",
164 info->size >> 20, info->sector_count);
165
166 puts (" Sector Start Addresses:");
167
168 for (i=0; i<info->sector_count; ++i) {
169 if ((i % 5) == 0) {
170 puts ("\n ");
171 }
172
173 printf (" %08lX%s",
174 info->start[i],
175 info->protect[i] ? " (RO)" : " "
176 );
177 }
178
179 puts ("\n");
180}
181
182/*-----------------------------------------------------------------------
183 */
184
185/*
186 * The following code cannot be run from FLASH!
187 */
188
189static ulong flash_get_size (vu_long *addr, flash_info_t *info)
190{
191 short i;
192#if 0
193 ulong base = (ulong)addr;
194#endif
195 uchar value;
196
197 /* Write auto select command: read Manufacturer ID */
198#if 0
199 addr[0x0555] = 0x00AA00AA;
200 addr[0x02AA] = 0x00550055;
201 addr[0x0555] = 0x00900090;
202#else
203 addr[0x0555] = 0xAAAAAAAA;
204 addr[0x02AA] = 0x55555555;
205 addr[0x0555] = 0x90909090;
206#endif
207
208 value = addr[0];
209
210 switch (value + (value << 16)) {
211 case AMD_MANUFACT:
212 info->flash_id = FLASH_MAN_AMD;
213 break;
214
215 case FUJ_MANUFACT:
216 info->flash_id = FLASH_MAN_FUJ;
217 break;
218
219 default:
220 info->flash_id = FLASH_UNKNOWN;
221 info->sector_count = 0;
222 info->size = 0;
223 break;
224 }
225
226 value = addr[1]; /* device ID */
227
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200228 switch ((unsigned long)value) {
wdenkaffae2b2002-08-17 09:36:01 +0000229 case AMD_ID_F040B:
230 info->flash_id += FLASH_AM040;
231 info->sector_count = 8;
232 info->size = 0x00200000;
233 break; /* => 2 MB */
234
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#if 0 /* enable when device IDs are available */
271 case AMD_ID_LV320T:
272 info->flash_id += FLASH_AM320T;
273 info->sector_count = 67;
274 info->size = 0x00800000;
275 break; /* => 8 MB */
276
277 case AMD_ID_LV320B:
278 info->flash_id += FLASH_AM320B;
279 info->sector_count = 67;
280 info->size = 0x00800000;
281 break; /* => 8 MB */
282#endif
283 case AMD_ID_LV033C:
284 info->flash_id += FLASH_AM033C;
285 info->sector_count = 64;
286 info->size = 0x01000000;
287 break; /* => 16Mb */
288 default:
289 info->flash_id = FLASH_UNKNOWN;
290 return (0); /* => no or unknown flash */
291
292 }
293
294#if 0
295 /* set up sector start address table */
296 if (info->flash_id & FLASH_BTYPE) {
297 /* set sector offsets for bottom boot block type */
298 info->start[0] = base + 0x00000000;
299 info->start[1] = base + 0x00008000;
300 info->start[2] = base + 0x0000C000;
301 info->start[3] = base + 0x00010000;
302 for (i = 4; i < info->sector_count; i++) {
303 info->start[i] = base + (i * 0x00020000) - 0x00060000;
304 }
305 } else {
306 /* set sector offsets for top boot block type */
307 i = info->sector_count - 1;
308 info->start[i--] = base + info->size - 0x00008000;
309 info->start[i--] = base + info->size - 0x0000C000;
310 info->start[i--] = base + info->size - 0x00010000;
311 for (; i >= 0; i--) {
312 info->start[i] = base + i * 0x00020000;
313 }
314 }
315#else
316 flash_get_offsets ((ulong)addr, &flash_info[0]);
317#endif
318
319 /* check for protected sectors */
320 for (i = 0; i < info->sector_count; i++) {
321 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
322 /* D0 = 1 if protected */
323 addr = (volatile unsigned long *)(info->start[i]);
324#if 1
325 /* We don't know why it happens, but on ICU Board *
326 * for AMD29033C flash we need to resend the command of *
327 * reading flash protection for upper 8 Mb of flash */
328 if ( i == 32 ) {
329 addr[0x0555] = 0xAAAAAAAA;
330 addr[0x02AA] = 0x55555555;
331 addr[0x0555] = 0x90909090;
332 }
333#endif
334 info->protect[i] = addr[2] & 1;
335 }
336
337 /*
338 * Prevent writes to uninitialized FLASH.
339 */
340 if (info->flash_id != FLASH_UNKNOWN) {
341 addr = (volatile unsigned long *)info->start[0];
342#if 0
343 *addr = 0x00F000F0; /* reset bank */
344#else
345 *addr = 0xF0F0F0F0; /* reset bank */
346#endif
347 }
348
349 return (info->size);
350}
351
352
353/*-----------------------------------------------------------------------
354 */
355
356int flash_erase (flash_info_t *info, int s_first, int s_last)
357{
358 vu_long *addr = (vu_long*)(info->start[0]);
359 int flag, prot, sect, l_sect;
360 ulong start, now, last;
361
362 if ((s_first < 0) || (s_first > s_last)) {
363 if (info->flash_id == FLASH_UNKNOWN) {
364 puts ("- missing\n");
365 } else {
366 puts ("- no sectors to erase\n");
367 }
368 return 1;
369 }
370
371 if ((info->flash_id == FLASH_UNKNOWN) ||
372 (info->flash_id > FLASH_AMD_COMP)) {
373 puts ("Can't erase unknown flash type - aborted\n");
374 return 1;
375 }
376
377 prot = 0;
378 for (sect=s_first; sect<=s_last; ++sect) {
379 if (info->protect[sect]) {
380 prot++;
381 }
382 }
383
384 if (prot) {
385 printf ("- Warning: %d protected sectors will not be erased!\n",
386 prot);
387 } else {
388 puts ("\n");
389 }
390
391 l_sect = -1;
392
393 /* Disable interrupts which might cause a timeout here */
394 flag = disable_interrupts();
395
396#if 0
397 addr[0x0555] = 0x00AA00AA;
398 addr[0x02AA] = 0x00550055;
399 addr[0x0555] = 0x00800080;
400 addr[0x0555] = 0x00AA00AA;
401 addr[0x02AA] = 0x00550055;
402#else
403 addr[0x0555] = 0xAAAAAAAA;
404 addr[0x02AA] = 0x55555555;
405 addr[0x0555] = 0x80808080;
406 addr[0x0555] = 0xAAAAAAAA;
407 addr[0x02AA] = 0x55555555;
408#endif
409
410 /* Start erase on unprotected sectors */
411 for (sect = s_first; sect<=s_last; sect++) {
412 if (info->protect[sect] == 0) { /* not protected */
413 addr = (vu_long*)(info->start[sect]);
414#if 0
415 addr[0] = 0x00300030;
416#else
417 addr[0] = 0x30303030;
418#endif
419 l_sect = sect;
420 }
421 }
422
423 /* re-enable interrupts if necessary */
424 if (flag)
425 enable_interrupts();
426
427 /* wait at least 80us - let's wait 1 ms */
428 udelay (1000);
429
430 /*
431 * We wait for the last triggered sector
432 */
433 if (l_sect < 0)
434 goto DONE;
435
436 start = get_timer (0);
437 last = start;
438 addr = (vu_long*)(info->start[l_sect]);
439#if 0
440 while ((addr[0] & 0x00800080) != 0x00800080)
441#else
442 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
443#endif
444 {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200445 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000446 puts ("Timeout\n");
447 return 1;
448 }
449 /* show that we're waiting */
450 if ((now - last) > 1000) { /* every second */
451 putc ('.');
452 last = now;
453 }
454 }
455
456DONE:
457 /* reset to read mode */
458 addr = (volatile unsigned long *)info->start[0];
459#if 0
460 addr[0] = 0x00F000F0; /* reset bank */
461#else
462 addr[0] = 0xF0F0F0F0; /* reset bank */
463#endif
464
465 puts (" done\n");
466 return 0;
467}
468
469/*-----------------------------------------------------------------------
470 * Copy memory to flash, returns:
471 * 0 - OK
472 * 1 - write timeout
473 * 2 - Flash not erased
474 */
475
476int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
477{
478 ulong cp, wp, data;
479 int i, l, rc;
480
481 wp = (addr & ~3); /* get lower word aligned address */
482
483 /*
484 * handle unaligned start bytes
485 */
486 if ((l = addr - wp) != 0) {
487 data = 0;
488 for (i=0, cp=wp; i<l; ++i, ++cp) {
489 data = (data << 8) | (*(uchar *)cp);
490 }
491 for (; i<4 && cnt>0; ++i) {
492 data = (data << 8) | *src++;
493 --cnt;
494 ++cp;
495 }
496 for (; cnt==0 && i<4; ++i, ++cp) {
497 data = (data << 8) | (*(uchar *)cp);
498 }
499
500 if ((rc = write_word(info, wp, data)) != 0) {
501 return (rc);
502 }
503 wp += 4;
504 }
505
506 /*
507 * handle word aligned part
508 */
509 while (cnt >= 4) {
510 data = 0;
511 for (i=0; i<4; ++i) {
512 data = (data << 8) | *src++;
513 }
514 if ((rc = write_word(info, wp, data)) != 0) {
515 return (rc);
516 }
517 wp += 4;
518 cnt -= 4;
519 }
520
521 if (cnt == 0) {
522 return (0);
523 }
524
525 /*
526 * handle unaligned tail bytes
527 */
528 data = 0;
529 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
530 data = (data << 8) | *src++;
531 --cnt;
532 }
533 for (; i<4; ++i, ++cp) {
534 data = (data << 8) | (*(uchar *)cp);
535 }
536
537 return (write_word(info, wp, data));
538}
539
540/*-----------------------------------------------------------------------
541 * Write a word to Flash, returns:
542 * 0 - OK
543 * 1 - write timeout
544 * 2 - Flash not erased
545 */
546static int write_word (flash_info_t *info, ulong dest, ulong data)
547{
548 vu_long *addr = (vu_long*)(info->start[0]);
549 ulong start;
550 int flag;
551
552 /* Check if Flash is (sufficiently) erased */
553 if ((*((vu_long *)dest) & data) != data) {
554 return (2);
555 }
556 /* Disable interrupts which might cause a timeout here */
557 flag = disable_interrupts();
558
559#if 0
560 addr[0x0555] = 0x00AA00AA;
561 addr[0x02AA] = 0x00550055;
562 addr[0x0555] = 0x00A000A0;
563#else
564 addr[0x0555] = 0xAAAAAAAA;
565 addr[0x02AA] = 0x55555555;
566 addr[0x0555] = 0xA0A0A0A0;
567#endif
568
569 *((vu_long *)dest) = data;
570
571 /* re-enable interrupts if necessary */
572 if (flag)
573 enable_interrupts();
574
575 /* data polling for D7 */
576 start = get_timer (0);
577#if 0
578 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080))
579#else
580 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080))
581#endif
582 {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200583 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000584 return (1);
585 }
586 }
587 return (0);
588}
589
590/*-----------------------------------------------------------------------
591 */