blob: a84ab99f35ec5cf07c890edf809a2a18c206e324 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8#include <common.h>
9#include <mpc8xx.h>
10
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020011flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000012
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020013#if defined(CONFIG_ENV_IS_IN_FLASH)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020014# ifndef CONFIG_ENV_ADDR
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020015# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenkaffae2b2002-08-17 09:36:01 +000016# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020017# ifndef CONFIG_ENV_SIZE
18# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
wdenkaffae2b2002-08-17 09:36:01 +000019# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020020# ifndef CONFIG_ENV_SECT_SIZE
21# define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
wdenkaffae2b2002-08-17 09:36:01 +000022# endif
23#endif
24
25/*-----------------------------------------------------------------------
26 * Functions
27 */
28static ulong flash_get_size (vu_long *addr, flash_info_t *info);
29static int write_word (flash_info_t *info, ulong dest, ulong data);
30static void flash_get_offsets (ulong base, flash_info_t *info);
31
32/*-----------------------------------------------------------------------
33 */
34
35unsigned long flash_init (void)
36{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020037 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
wdenkaffae2b2002-08-17 09:36:01 +000038 volatile memctl8xx_t *memctl = &immap->im_memctl;
Wolfgang Denk13039eb2011-11-04 15:55:33 +000039 unsigned long size_b0;
wdenkaffae2b2002-08-17 09:36:01 +000040 int i;
41
42 /* Init: no FLASHes known */
Wolfgang Denk13039eb2011-11-04 15:55:33 +000043 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
wdenkaffae2b2002-08-17 09:36:01 +000044 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenkaffae2b2002-08-17 09:36:01 +000045
46 /* Static FLASH Bank configuration here - FIXME XXX */
47
48 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
49
50 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
51 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
52 size_b0,
53 size_b0 >> 20);
54 }
55
wdenkaffae2b2002-08-17 09:36:01 +000056 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020057 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
58 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
wdenkaffae2b2002-08-17 09:36:01 +000059
60 /* Re-do sizing to get full correct info */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020061 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000062
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020063 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000064
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020065#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +000066 /* monitor protection ON by default */
67 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068 CONFIG_SYS_MONITOR_BASE,
69 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +000070 &flash_info[0]);
71#endif
72
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020073#ifdef CONFIG_ENV_IS_IN_FLASH
wdenkaffae2b2002-08-17 09:36:01 +000074 /* ENV protection ON by default */
75 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020076 CONFIG_ENV_ADDR,
77 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
wdenkaffae2b2002-08-17 09:36:01 +000078 &flash_info[0]);
79#endif
80
81 /* ICU862 Board has only one Flash Bank */
wdenkaffae2b2002-08-17 09:36:01 +000082 flash_info[0].size = size_b0;
wdenkaffae2b2002-08-17 09:36:01 +000083
Wolfgang Denk13039eb2011-11-04 15:55:33 +000084 return size_b0;
wdenkaffae2b2002-08-17 09:36:01 +000085
86}
87
88/*-----------------------------------------------------------------------
89 */
90static void flash_get_offsets (ulong base, flash_info_t *info)
91{
92 int i;
93
94 /* set up sector start address table */
95 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
96 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM033C)) {
97 /* set sector offsets for uniform sector type */
98 for (i = 0; i < info->sector_count; i++) {
99 info->start[i] = base + (i * 0x00040000);
100 }
101 }
102}
103
104/*-----------------------------------------------------------------------
105 */
106void flash_print_info (flash_info_t *info)
107{
108 int i;
109
110 if (info->flash_id == FLASH_UNKNOWN) {
111 puts ("missing or unknown FLASH type\n");
112 return;
113 }
114
115 switch (info->flash_id & FLASH_VENDMASK) {
116 case FLASH_MAN_AMD: puts ("AMD "); break;
117 case FLASH_MAN_FUJ: puts ("FUJITSU "); break;
118 case FLASH_MAN_BM: puts ("BRIGHT MICRO "); break;
119 default: puts ("Unknown Vendor "); break;
120 }
121
122 switch (info->flash_id & FLASH_TYPEMASK) {
123 case FLASH_AM040: puts ("29F040/29LV040 (4 Mbit, uniform sectors)\n");
124 break;
125 case FLASH_AM400B: puts ("AM29LV400B (4 Mbit, bottom boot sect)\n");
126 break;
127 case FLASH_AM400T: puts ("AM29LV400T (4 Mbit, top boot sector)\n");
128 break;
129 case FLASH_AM800B: puts ("AM29LV800B (8 Mbit, bottom boot sect)\n");
130 break;
131 case FLASH_AM800T: puts ("AM29LV800T (8 Mbit, top boot sector)\n");
132 break;
133 case FLASH_AM160B: puts ("AM29LV160B (16 Mbit, bottom boot sect)\n");
134 break;
135 case FLASH_AM160T: puts ("AM29LV160T (16 Mbit, top boot sector)\n");
136 break;
137 case FLASH_AM320B: puts ("AM29LV320B (32 Mbit, bottom boot sect)\n");
138 break;
139 case FLASH_AM320T: puts ("AM29LV320T (32 Mbit, top boot sector)\n");
140 break;
141 case FLASH_AM033C: puts ("AM29LV033C (32 Mbit)\n");
142 break;
143 default: puts ("Unknown Chip Type\n");
144 break;
145 }
146
147 printf (" Size: %ld MB in %d Sectors\n",
148 info->size >> 20, info->sector_count);
149
150 puts (" Sector Start Addresses:");
151
152 for (i=0; i<info->sector_count; ++i) {
153 if ((i % 5) == 0) {
154 puts ("\n ");
155 }
156
157 printf (" %08lX%s",
158 info->start[i],
159 info->protect[i] ? " (RO)" : " "
160 );
161 }
162
163 puts ("\n");
164}
165
166/*-----------------------------------------------------------------------
167 */
168
169/*
170 * The following code cannot be run from FLASH!
171 */
172
173static ulong flash_get_size (vu_long *addr, flash_info_t *info)
174{
175 short i;
176#if 0
177 ulong base = (ulong)addr;
178#endif
179 uchar value;
180
181 /* Write auto select command: read Manufacturer ID */
182#if 0
183 addr[0x0555] = 0x00AA00AA;
184 addr[0x02AA] = 0x00550055;
185 addr[0x0555] = 0x00900090;
186#else
187 addr[0x0555] = 0xAAAAAAAA;
188 addr[0x02AA] = 0x55555555;
189 addr[0x0555] = 0x90909090;
190#endif
191
192 value = addr[0];
193
194 switch (value + (value << 16)) {
195 case AMD_MANUFACT:
196 info->flash_id = FLASH_MAN_AMD;
197 break;
198
199 case FUJ_MANUFACT:
200 info->flash_id = FLASH_MAN_FUJ;
201 break;
202
203 default:
204 info->flash_id = FLASH_UNKNOWN;
205 info->sector_count = 0;
206 info->size = 0;
207 break;
208 }
209
210 value = addr[1]; /* device ID */
211
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200212 switch ((unsigned long)value) {
wdenkaffae2b2002-08-17 09:36:01 +0000213 case AMD_ID_F040B:
214 info->flash_id += FLASH_AM040;
215 info->sector_count = 8;
216 info->size = 0x00200000;
217 break; /* => 2 MB */
218
219 case AMD_ID_LV400T:
220 info->flash_id += FLASH_AM400T;
221 info->sector_count = 11;
222 info->size = 0x00100000;
223 break; /* => 1 MB */
224
225 case AMD_ID_LV400B:
226 info->flash_id += FLASH_AM400B;
227 info->sector_count = 11;
228 info->size = 0x00100000;
229 break; /* => 1 MB */
230
231 case AMD_ID_LV800T:
232 info->flash_id += FLASH_AM800T;
233 info->sector_count = 19;
234 info->size = 0x00200000;
235 break; /* => 2 MB */
236
237 case AMD_ID_LV800B:
238 info->flash_id += FLASH_AM800B;
239 info->sector_count = 19;
240 info->size = 0x00200000;
241 break; /* => 2 MB */
242
243 case AMD_ID_LV160T:
244 info->flash_id += FLASH_AM160T;
245 info->sector_count = 35;
246 info->size = 0x00400000;
247 break; /* => 4 MB */
248
249 case AMD_ID_LV160B:
250 info->flash_id += FLASH_AM160B;
251 info->sector_count = 35;
252 info->size = 0x00400000;
253 break; /* => 4 MB */
254#if 0 /* enable when device IDs are available */
255 case AMD_ID_LV320T:
256 info->flash_id += FLASH_AM320T;
257 info->sector_count = 67;
258 info->size = 0x00800000;
259 break; /* => 8 MB */
260
261 case AMD_ID_LV320B:
262 info->flash_id += FLASH_AM320B;
263 info->sector_count = 67;
264 info->size = 0x00800000;
265 break; /* => 8 MB */
266#endif
267 case AMD_ID_LV033C:
268 info->flash_id += FLASH_AM033C;
269 info->sector_count = 64;
270 info->size = 0x01000000;
271 break; /* => 16Mb */
272 default:
273 info->flash_id = FLASH_UNKNOWN;
274 return (0); /* => no or unknown flash */
275
276 }
277
278#if 0
279 /* set up sector start address table */
280 if (info->flash_id & FLASH_BTYPE) {
281 /* set sector offsets for bottom boot block type */
282 info->start[0] = base + 0x00000000;
283 info->start[1] = base + 0x00008000;
284 info->start[2] = base + 0x0000C000;
285 info->start[3] = base + 0x00010000;
286 for (i = 4; i < info->sector_count; i++) {
287 info->start[i] = base + (i * 0x00020000) - 0x00060000;
288 }
289 } else {
290 /* set sector offsets for top boot block type */
291 i = info->sector_count - 1;
292 info->start[i--] = base + info->size - 0x00008000;
293 info->start[i--] = base + info->size - 0x0000C000;
294 info->start[i--] = base + info->size - 0x00010000;
295 for (; i >= 0; i--) {
296 info->start[i] = base + i * 0x00020000;
297 }
298 }
299#else
300 flash_get_offsets ((ulong)addr, &flash_info[0]);
301#endif
302
303 /* check for protected sectors */
304 for (i = 0; i < info->sector_count; i++) {
305 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
306 /* D0 = 1 if protected */
307 addr = (volatile unsigned long *)(info->start[i]);
308#if 1
309 /* We don't know why it happens, but on ICU Board *
310 * for AMD29033C flash we need to resend the command of *
311 * reading flash protection for upper 8 Mb of flash */
312 if ( i == 32 ) {
313 addr[0x0555] = 0xAAAAAAAA;
314 addr[0x02AA] = 0x55555555;
315 addr[0x0555] = 0x90909090;
316 }
317#endif
318 info->protect[i] = addr[2] & 1;
319 }
320
321 /*
322 * Prevent writes to uninitialized FLASH.
323 */
324 if (info->flash_id != FLASH_UNKNOWN) {
325 addr = (volatile unsigned long *)info->start[0];
326#if 0
327 *addr = 0x00F000F0; /* reset bank */
328#else
329 *addr = 0xF0F0F0F0; /* reset bank */
330#endif
331 }
332
333 return (info->size);
334}
335
336
337/*-----------------------------------------------------------------------
338 */
339
340int flash_erase (flash_info_t *info, int s_first, int s_last)
341{
342 vu_long *addr = (vu_long*)(info->start[0]);
343 int flag, prot, sect, l_sect;
344 ulong start, now, last;
345
346 if ((s_first < 0) || (s_first > s_last)) {
347 if (info->flash_id == FLASH_UNKNOWN) {
348 puts ("- missing\n");
349 } else {
350 puts ("- no sectors to erase\n");
351 }
352 return 1;
353 }
354
355 if ((info->flash_id == FLASH_UNKNOWN) ||
356 (info->flash_id > FLASH_AMD_COMP)) {
357 puts ("Can't erase unknown flash type - aborted\n");
358 return 1;
359 }
360
361 prot = 0;
362 for (sect=s_first; sect<=s_last; ++sect) {
363 if (info->protect[sect]) {
364 prot++;
365 }
366 }
367
368 if (prot) {
369 printf ("- Warning: %d protected sectors will not be erased!\n",
370 prot);
371 } else {
372 puts ("\n");
373 }
374
375 l_sect = -1;
376
377 /* Disable interrupts which might cause a timeout here */
378 flag = disable_interrupts();
379
380#if 0
381 addr[0x0555] = 0x00AA00AA;
382 addr[0x02AA] = 0x00550055;
383 addr[0x0555] = 0x00800080;
384 addr[0x0555] = 0x00AA00AA;
385 addr[0x02AA] = 0x00550055;
386#else
387 addr[0x0555] = 0xAAAAAAAA;
388 addr[0x02AA] = 0x55555555;
389 addr[0x0555] = 0x80808080;
390 addr[0x0555] = 0xAAAAAAAA;
391 addr[0x02AA] = 0x55555555;
392#endif
393
394 /* Start erase on unprotected sectors */
395 for (sect = s_first; sect<=s_last; sect++) {
396 if (info->protect[sect] == 0) { /* not protected */
397 addr = (vu_long*)(info->start[sect]);
398#if 0
399 addr[0] = 0x00300030;
400#else
401 addr[0] = 0x30303030;
402#endif
403 l_sect = sect;
404 }
405 }
406
407 /* re-enable interrupts if necessary */
408 if (flag)
409 enable_interrupts();
410
411 /* wait at least 80us - let's wait 1 ms */
412 udelay (1000);
413
414 /*
415 * We wait for the last triggered sector
416 */
417 if (l_sect < 0)
418 goto DONE;
419
420 start = get_timer (0);
421 last = start;
422 addr = (vu_long*)(info->start[l_sect]);
423#if 0
424 while ((addr[0] & 0x00800080) != 0x00800080)
425#else
426 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
427#endif
428 {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200429 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000430 puts ("Timeout\n");
431 return 1;
432 }
433 /* show that we're waiting */
434 if ((now - last) > 1000) { /* every second */
435 putc ('.');
436 last = now;
437 }
438 }
439
440DONE:
441 /* reset to read mode */
442 addr = (volatile unsigned long *)info->start[0];
443#if 0
444 addr[0] = 0x00F000F0; /* reset bank */
445#else
446 addr[0] = 0xF0F0F0F0; /* reset bank */
447#endif
448
449 puts (" done\n");
450 return 0;
451}
452
453/*-----------------------------------------------------------------------
454 * Copy memory to flash, returns:
455 * 0 - OK
456 * 1 - write timeout
457 * 2 - Flash not erased
458 */
459
460int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
461{
462 ulong cp, wp, data;
463 int i, l, rc;
464
465 wp = (addr & ~3); /* get lower word aligned address */
466
467 /*
468 * handle unaligned start bytes
469 */
470 if ((l = addr - wp) != 0) {
471 data = 0;
472 for (i=0, cp=wp; i<l; ++i, ++cp) {
473 data = (data << 8) | (*(uchar *)cp);
474 }
475 for (; i<4 && cnt>0; ++i) {
476 data = (data << 8) | *src++;
477 --cnt;
478 ++cp;
479 }
480 for (; cnt==0 && i<4; ++i, ++cp) {
481 data = (data << 8) | (*(uchar *)cp);
482 }
483
484 if ((rc = write_word(info, wp, data)) != 0) {
485 return (rc);
486 }
487 wp += 4;
488 }
489
490 /*
491 * handle word aligned part
492 */
493 while (cnt >= 4) {
494 data = 0;
495 for (i=0; i<4; ++i) {
496 data = (data << 8) | *src++;
497 }
498 if ((rc = write_word(info, wp, data)) != 0) {
499 return (rc);
500 }
501 wp += 4;
502 cnt -= 4;
503 }
504
505 if (cnt == 0) {
506 return (0);
507 }
508
509 /*
510 * handle unaligned tail bytes
511 */
512 data = 0;
513 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
514 data = (data << 8) | *src++;
515 --cnt;
516 }
517 for (; i<4; ++i, ++cp) {
518 data = (data << 8) | (*(uchar *)cp);
519 }
520
521 return (write_word(info, wp, data));
522}
523
524/*-----------------------------------------------------------------------
525 * Write a word to Flash, returns:
526 * 0 - OK
527 * 1 - write timeout
528 * 2 - Flash not erased
529 */
530static int write_word (flash_info_t *info, ulong dest, ulong data)
531{
532 vu_long *addr = (vu_long*)(info->start[0]);
533 ulong start;
534 int flag;
535
536 /* Check if Flash is (sufficiently) erased */
537 if ((*((vu_long *)dest) & data) != data) {
538 return (2);
539 }
540 /* Disable interrupts which might cause a timeout here */
541 flag = disable_interrupts();
542
543#if 0
544 addr[0x0555] = 0x00AA00AA;
545 addr[0x02AA] = 0x00550055;
546 addr[0x0555] = 0x00A000A0;
547#else
548 addr[0x0555] = 0xAAAAAAAA;
549 addr[0x02AA] = 0x55555555;
550 addr[0x0555] = 0xA0A0A0A0;
551#endif
552
553 *((vu_long *)dest) = data;
554
555 /* re-enable interrupts if necessary */
556 if (flag)
557 enable_interrupts();
558
559 /* data polling for D7 */
560 start = get_timer (0);
561#if 0
562 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080))
563#else
564 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080))
565#endif
566 {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200567 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000568 return (1);
569 }
570 }
571 return (0);
572}
573
574/*-----------------------------------------------------------------------
575 */