blob: 2c7d2a08c402dcf2975fcb18ede5fc6da53c0577 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +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 <ppc4xx.h>
26#include <asm/processor.h>
27
28flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
29
30
31#ifdef CFG_FLASH_16BIT
32#define FLASH_WORD_SIZE unsigned short
33#define FLASH_ID_MASK 0xFFFF
34#else
35#define FLASH_WORD_SIZE unsigned long
36#define FLASH_ID_MASK 0xFFFFFFFF
37#endif
38
39/*-----------------------------------------------------------------------
40 * Functions
41 */
42/* stolen from esteem192e/flash.c */
43ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info);
44
45#ifndef CFG_FLASH_16BIT
46static int write_word (flash_info_t *info, ulong dest, ulong data);
47#else
48static int write_short (flash_info_t *info, ulong dest, ushort data);
49#endif
50static void flash_get_offsets (ulong base, flash_info_t *info);
51
52
53/*-----------------------------------------------------------------------
54 */
55
56unsigned long flash_init (void)
57{
58 unsigned long size_b0, size_b1;
59 int i;
wdenk57b2d802003-06-27 21:31:46 +000060 uint pbcr;
61 unsigned long base_b0, base_b1;
wdenkfe8c2802002-11-03 00:38:21 +000062
63 /* Init: no FLASHes known */
64 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
65 flash_info[i].flash_id = FLASH_UNKNOWN;
66 }
67
68 /* Static FLASH Bank configuration here - FIXME XXX */
69
70 size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE0_PRELIM, &flash_info[0]);
71
72 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
73 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
74 size_b0, size_b0<<20);
75 }
76
77 /* Only one bank */
78 if (CFG_MAX_FLASH_BANKS == 1)
79 {
80 /* Setup offsets */
81 flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
82
83 /* Monitor protection ON by default */
84#if 0 /* sand: */
85 (void)flash_protect(FLAG_PROTECT_SET,
wdenkb9a83a92003-05-30 12:48:29 +000086 FLASH_BASE0_PRELIM-monitor_flash_len+size_b0,
wdenkfe8c2802002-11-03 00:38:21 +000087 FLASH_BASE0_PRELIM-1+size_b0,
88 &flash_info[0]);
89#else
90 (void)flash_protect(FLAG_PROTECT_SET,
wdenk57b2d802003-06-27 21:31:46 +000091 CFG_MONITOR_BASE,
92 CFG_MONITOR_BASE+monitor_flash_len-1,
93 &flash_info[0]);
wdenkfe8c2802002-11-03 00:38:21 +000094#endif
95 size_b1 = 0 ;
96 flash_info[0].size = size_b0;
97 }
98
99 /* 2 banks */
100 else
101 {
102 size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE1_PRELIM, &flash_info[1]);
103
104 /* Re-do sizing to get full correct info */
105
106 if (size_b1)
107 {
108 mtdcr(ebccfga, pb0cr);
109 pbcr = mfdcr(ebccfgd);
110 mtdcr(ebccfga, pb0cr);
111 base_b1 = -size_b1;
112 pbcr = (pbcr & 0x0001ffff) | base_b1 | (((size_b1/1024/1024)-1)<<17);
113 mtdcr(ebccfgd, pbcr);
114 /* printf("pb1cr = %x\n", pbcr); */
115 }
116
117 if (size_b0)
118 {
119 mtdcr(ebccfga, pb1cr);
120 pbcr = mfdcr(ebccfgd);
121 mtdcr(ebccfga, pb1cr);
122 base_b0 = base_b1 - size_b0;
123 pbcr = (pbcr & 0x0001ffff) | base_b0 | (((size_b0/1024/1024)-1)<<17);
124 mtdcr(ebccfgd, pbcr);
125 /* printf("pb0cr = %x\n", pbcr); */
126 }
127
128 size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)base_b0, &flash_info[0]);
129
130 flash_get_offsets (base_b0, &flash_info[0]);
131
132 /* monitor protection ON by default */
133#if 0 /* sand: */
134 (void)flash_protect(FLAG_PROTECT_SET,
wdenkb9a83a92003-05-30 12:48:29 +0000135 FLASH_BASE0_PRELIM-monitor_flash_len+size_b0,
wdenkfe8c2802002-11-03 00:38:21 +0000136 FLASH_BASE0_PRELIM-1+size_b0,
137 &flash_info[0]);
138#else
139 (void)flash_protect(FLAG_PROTECT_SET,
wdenk57b2d802003-06-27 21:31:46 +0000140 CFG_MONITOR_BASE,
141 CFG_MONITOR_BASE+monitor_flash_len-1,
142 &flash_info[0]);
wdenkfe8c2802002-11-03 00:38:21 +0000143#endif
144
145 if (size_b1) {
146 /* Re-do sizing to get full correct info */
147 size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)base_b1, &flash_info[1]);
148
149 flash_get_offsets (base_b1, &flash_info[1]);
150
151 /* monitor protection ON by default */
152 (void)flash_protect(FLAG_PROTECT_SET,
wdenkb9a83a92003-05-30 12:48:29 +0000153 base_b1+size_b1-monitor_flash_len,
wdenkfe8c2802002-11-03 00:38:21 +0000154 base_b1+size_b1-1,
155 &flash_info[1]);
156 /* monitor protection OFF by default (one is enough) */
157 (void)flash_protect(FLAG_PROTECT_CLEAR,
wdenkb9a83a92003-05-30 12:48:29 +0000158 base_b0+size_b0-monitor_flash_len,
wdenkfe8c2802002-11-03 00:38:21 +0000159 base_b0+size_b0-1,
160 &flash_info[0]);
161 } else {
162 flash_info[1].flash_id = FLASH_UNKNOWN;
163 flash_info[1].sector_count = -1;
164 }
165
166 flash_info[0].size = size_b0;
167 flash_info[1].size = size_b1;
168 }/* else 2 banks */
169 return (size_b0 + size_b1);
170}
171
172
wdenkfe8c2802002-11-03 00:38:21 +0000173/*-----------------------------------------------------------------------
174 */
175
176static void flash_get_offsets (ulong base, flash_info_t *info)
177{
178 int i;
179
180 /* set up sector start adress table */
181 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F320J3A ||
182 (info->flash_id & FLASH_TYPEMASK) == FLASH_28F640J3A ||
183 (info->flash_id & FLASH_TYPEMASK) == FLASH_28F128J3A) {
184 for (i = 0; i < info->sector_count; i++) {
185 info->start[i] = base + (i * info->size/info->sector_count);
186 }
187 } else if (info->flash_id & FLASH_BTYPE) {
wdenk57b2d802003-06-27 21:31:46 +0000188 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkfe8c2802002-11-03 00:38:21 +0000189
190#ifndef CFG_FLASH_16BIT
191 /* set sector offsets for bottom boot block type */
192 info->start[0] = base + 0x00000000;
193 info->start[1] = base + 0x00004000;
194 info->start[2] = base + 0x00008000;
195 info->start[3] = base + 0x0000C000;
196 info->start[4] = base + 0x00010000;
197 info->start[5] = base + 0x00014000;
198 info->start[6] = base + 0x00018000;
199 info->start[7] = base + 0x0001C000;
200 for (i = 8; i < info->sector_count; i++) {
201 info->start[i] = base + (i * 0x00020000) - 0x000E0000;
wdenk57b2d802003-06-27 21:31:46 +0000202 }
203 }
204 else {
wdenkfe8c2802002-11-03 00:38:21 +0000205 /* set sector offsets for bottom boot block type */
206 info->start[0] = base + 0x00000000;
207 info->start[1] = base + 0x00008000;
208 info->start[2] = base + 0x0000C000;
209 info->start[3] = base + 0x00010000;
210 for (i = 4; i < info->sector_count; i++) {
211 info->start[i] = base + (i * 0x00020000) - 0x00060000;
212 }
213 }
214#else
215 /* set sector offsets for bottom boot block type */
216 info->start[0] = base + 0x00000000;
217 info->start[1] = base + 0x00002000;
218 info->start[2] = base + 0x00004000;
219 info->start[3] = base + 0x00006000;
220 info->start[4] = base + 0x00008000;
221 info->start[5] = base + 0x0000A000;
222 info->start[6] = base + 0x0000C000;
223 info->start[7] = base + 0x0000E000;
224 for (i = 8; i < info->sector_count; i++) {
225 info->start[i] = base + (i * 0x00010000) - 0x00070000;
wdenk57b2d802003-06-27 21:31:46 +0000226 }
wdenkfe8c2802002-11-03 00:38:21 +0000227 }
wdenk57b2d802003-06-27 21:31:46 +0000228 else {
wdenkfe8c2802002-11-03 00:38:21 +0000229 /* set sector offsets for bottom boot block type */
230 info->start[0] = base + 0x00000000;
231 info->start[1] = base + 0x00004000;
232 info->start[2] = base + 0x00006000;
233 info->start[3] = base + 0x00008000;
234 for (i = 4; i < info->sector_count; i++) {
235 info->start[i] = base + (i * 0x00010000) - 0x00030000;
236 }
237 }
238#endif
239 } else {
240 /* set sector offsets for top boot block type */
241 i = info->sector_count - 1;
wdenk57b2d802003-06-27 21:31:46 +0000242 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
wdenkfe8c2802002-11-03 00:38:21 +0000243
244#ifndef CFG_FLASH_16BIT
245 info->start[i--] = base + info->size - 0x00004000;
246 info->start[i--] = base + info->size - 0x00008000;
247 info->start[i--] = base + info->size - 0x0000C000;
248 info->start[i--] = base + info->size - 0x00010000;
249 info->start[i--] = base + info->size - 0x00014000;
250 info->start[i--] = base + info->size - 0x00018000;
251 info->start[i--] = base + info->size - 0x0001C000;
252 for (; i >= 0; i--) {
253 info->start[i] = base + i * 0x00020000;
254 }
255
wdenk57b2d802003-06-27 21:31:46 +0000256 } else {
wdenkfe8c2802002-11-03 00:38:21 +0000257
258 info->start[i--] = base + info->size - 0x00008000;
259 info->start[i--] = base + info->size - 0x0000C000;
260 info->start[i--] = base + info->size - 0x00010000;
261 for (; i >= 0; i--) {
262 info->start[i] = base + i * 0x00020000;
263 }
264 }
265#else
266 info->start[i--] = base + info->size - 0x00002000;
267 info->start[i--] = base + info->size - 0x00004000;
268 info->start[i--] = base + info->size - 0x00006000;
269 info->start[i--] = base + info->size - 0x00008000;
270 info->start[i--] = base + info->size - 0x0000A000;
271 info->start[i--] = base + info->size - 0x0000C000;
272 info->start[i--] = base + info->size - 0x0000E000;
273 for (; i >= 0; i--) {
274 info->start[i] = base + i * 0x00010000;
275 }
276
wdenk57b2d802003-06-27 21:31:46 +0000277 } else {
wdenkfe8c2802002-11-03 00:38:21 +0000278
279 info->start[i--] = base + info->size - 0x00004000;
280 info->start[i--] = base + info->size - 0x00006000;
281 info->start[i--] = base + info->size - 0x00008000;
282 for (; i >= 0; i--) {
283 info->start[i] = base + i * 0x00010000;
284 }
285 }
286#endif
287 }
288
289
290}
291
292/*-----------------------------------------------------------------------
293 */
294
295void flash_print_info (flash_info_t *info)
296{
297 int i;
298 uchar *boottype;
299 uchar botboot[]=", bottom boot sect)\n";
300 uchar topboot[]=", top boot sector)\n";
301
302 if (info->flash_id == FLASH_UNKNOWN) {
303 printf ("missing or unknown FLASH type\n");
304 return;
305 }
306
307 switch (info->flash_id & FLASH_VENDMASK) {
308 case FLASH_MAN_AMD: printf ("AMD "); break;
309 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
310 case FLASH_MAN_SST: printf ("SST "); break;
311 case FLASH_MAN_STM: printf ("STM "); break;
312 case FLASH_MAN_INTEL: printf ("INTEL "); break;
313 default: printf ("Unknown Vendor "); break;
314 }
315
316 if (info->flash_id & 0x0001 ) {
317 boottype = botboot;
318 } else {
319 boottype = topboot;
320 }
321
322 switch (info->flash_id & FLASH_TYPEMASK) {
323 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit%s",boottype);
324 break;
325 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit%s",boottype);
326 break;
327 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit%s",boottype);
328 break;
329 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit%s",boottype);
330 break;
331 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit%s",boottype);
332 break;
333 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit%s",boottype);
334 break;
335 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit%s",boottype);
336 break;
337 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit%s",boottype);
338 break;
339 case FLASH_INTEL800B: printf ("INTEL28F800B (8 Mbit%s",boottype);
340 break;
341 case FLASH_INTEL800T: printf ("INTEL28F800T (8 Mbit%s",boottype);
342 break;
343 case FLASH_INTEL160B: printf ("INTEL28F160B (16 Mbit%s",boottype);
344 break;
345 case FLASH_INTEL160T: printf ("INTEL28F160T (16 Mbit%s",boottype);
346 break;
347 case FLASH_INTEL320B: printf ("INTEL28F320B (32 Mbit%s",boottype);
348 break;
349 case FLASH_INTEL320T: printf ("INTEL28F320T (32 Mbit%s",boottype);
350 break;
351
352#if 0 /* enable when devices are available */
353
354 case FLASH_INTEL640B: printf ("INTEL28F640B (64 Mbit%s",boottype);
355 break;
356 case FLASH_INTEL640T: printf ("INTEL28F640T (64 Mbit%s",boottype);
357 break;
358#endif
359 case FLASH_28F320J3A: printf ("INTEL28F320J3A (32 Mbit%s",boottype);
360 break;
361 case FLASH_28F640J3A: printf ("INTEL28F640J3A (64 Mbit%s",boottype);
362 break;
363 case FLASH_28F128J3A: printf ("INTEL28F128J3A (128 Mbit%s",boottype);
364 break;
365
366 default: printf ("Unknown Chip Type\n");
367 break;
368 }
369
370 printf (" Size: %ld MB in %d Sectors\n",
371 info->size >> 20, info->sector_count);
372
373 printf (" Sector Start Addresses:");
374 for (i=0; i<info->sector_count; ++i) {
375 if ((i % 5) == 0)
376 printf ("\n ");
377 printf (" %08lX%s",
378 info->start[i],
379 info->protect[i] ? " (RO)" : " "
380 );
381 }
382 printf ("\n");
383 return;
384}
385
386
387/*-----------------------------------------------------------------------
388 */
389
390
391/*-----------------------------------------------------------------------
392 */
393
394/*
395 * The following code cannot be run from FLASH!
396 */
397ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info)
398{
399 short i;
400 ulong base = (ulong)addr;
401 FLASH_WORD_SIZE value;
402
403 /* Write auto select command: read Manufacturer ID */
404
405
406#ifndef CFG_FLASH_16BIT
407
408 /*
409 * Note: if it is an AMD flash and the word at addr[0000]
wdenk57b2d802003-06-27 21:31:46 +0000410 * is 0x00890089 this routine will think it is an Intel
411 * flash device and may(most likely) cause trouble.
wdenkfe8c2802002-11-03 00:38:21 +0000412 */
413
414 addr[0x0000] = 0x00900090;
415 if(addr[0x0000] != 0x00890089){
416 addr[0x0555] = 0x00AA00AA;
417 addr[0x02AA] = 0x00550055;
418 addr[0x0555] = 0x00900090;
419#else
420
421 /*
422 * Note: if it is an AMD flash and the word at addr[0000]
wdenk57b2d802003-06-27 21:31:46 +0000423 * is 0x0089 this routine will think it is an Intel
424 * flash device and may(most likely) cause trouble.
wdenkfe8c2802002-11-03 00:38:21 +0000425 */
426
427 addr[0x0000] = 0x0090;
428
429 if(addr[0x0000] != 0x0089){
430 addr[0x0555] = 0x00AA;
431 addr[0x02AA] = 0x0055;
432 addr[0x0555] = 0x0090;
433#endif
434 }
435 value = addr[0];
436
437 switch (value) {
438 case (AMD_MANUFACT & FLASH_ID_MASK):
439 info->flash_id = FLASH_MAN_AMD;
440 break;
441 case (FUJ_MANUFACT & FLASH_ID_MASK):
442 info->flash_id = FLASH_MAN_FUJ;
443 break;
444 case (STM_MANUFACT & FLASH_ID_MASK):
445 info->flash_id = FLASH_MAN_STM;
446 break;
447 case (SST_MANUFACT & FLASH_ID_MASK):
448 info->flash_id = FLASH_MAN_SST;
449 break;
450 case (INTEL_MANUFACT & FLASH_ID_MASK):
451 info->flash_id = FLASH_MAN_INTEL;
452 break;
453 default:
454 info->flash_id = FLASH_UNKNOWN;
455 info->sector_count = 0;
456 info->size = 0;
457 return (0); /* no or unknown flash */
458
459 }
460
461 value = addr[1]; /* device ID */
462
463 switch (value) {
464
465 case (AMD_ID_LV400T & FLASH_ID_MASK):
466 info->flash_id += FLASH_AM400T;
467 info->sector_count = 11;
468 info->size = 0x00100000;
469 break; /* => 1 MB */
470
471 case (AMD_ID_LV400B & FLASH_ID_MASK):
472 info->flash_id += FLASH_AM400B;
473 info->sector_count = 11;
474 info->size = 0x00100000;
475 break; /* => 1 MB */
476
477 case (AMD_ID_LV800T & FLASH_ID_MASK):
478 info->flash_id += FLASH_AM800T;
479 info->sector_count = 19;
480 info->size = 0x00200000;
481 break; /* => 2 MB */
482
483 case (AMD_ID_LV800B & FLASH_ID_MASK):
484 info->flash_id += FLASH_AM800B;
485 info->sector_count = 19;
486 info->size = 0x00200000;
487 break; /* => 2 MB */
488
489 case (AMD_ID_LV160T & FLASH_ID_MASK):
490 info->flash_id += FLASH_AM160T;
491 info->sector_count = 35;
492 info->size = 0x00400000;
493 break; /* => 4 MB */
494
495 case (AMD_ID_LV160B & FLASH_ID_MASK):
496 info->flash_id += FLASH_AM160B;
497 info->sector_count = 35;
498 info->size = 0x00400000;
499 break; /* => 4 MB */
500#if 0 /* enable when device IDs are available */
501 case (AMD_ID_LV320T & FLASH_ID_MASK):
502 info->flash_id += FLASH_AM320T;
503 info->sector_count = 67;
504 info->size = 0x00800000;
505 break; /* => 8 MB */
506
507 case (AMD_ID_LV320B & FLASH_ID_MASK):
508 info->flash_id += FLASH_AM320B;
509 info->sector_count = 67;
510 info->size = 0x00800000;
511 break; /* => 8 MB */
512#endif
513
514 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
515 info->flash_id += FLASH_INTEL800T;
516 info->sector_count = 23;
517 info->size = 0x00200000;
518 break; /* => 2 MB */
519
520 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
521 info->flash_id += FLASH_INTEL800B;
522 info->sector_count = 23;
523 info->size = 0x00200000;
524 break; /* => 2 MB */
525
526 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
527 info->flash_id += FLASH_INTEL160T;
528 info->sector_count = 39;
529 info->size = 0x00400000;
530 break; /* => 4 MB */
531
532 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
533 info->flash_id += FLASH_INTEL160B;
534 info->sector_count = 39;
535 info->size = 0x00400000;
536 break; /* => 4 MB */
537
538 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
539 info->flash_id += FLASH_INTEL320T;
540 info->sector_count = 71;
541 info->size = 0x00800000;
542 break; /* => 8 MB */
543
544 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
545 info->flash_id += FLASH_AM320B;
546 info->sector_count = 71;
547 info->size = 0x00800000;
548 break; /* => 8 MB */
549
550#if 0 /* enable when devices are available */
551 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
552 info->flash_id += FLASH_INTEL320T;
553 info->sector_count = 135;
554 info->size = 0x01000000;
555 break; /* => 16 MB */
556
557 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
558 info->flash_id += FLASH_AM320B;
559 info->sector_count = 135;
560 info->size = 0x01000000;
561 break; /* => 16 MB */
562#endif
563 case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
564 info->flash_id += FLASH_28F320J3A;
565 info->sector_count = 32;
566 info->size = 0x00400000;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200567 break; /* => 32 MBit */
wdenkfe8c2802002-11-03 00:38:21 +0000568 case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
569 info->flash_id += FLASH_28F640J3A;
570 info->sector_count = 64;
571 info->size = 0x00800000;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200572 break; /* => 64 MBit */
wdenkfe8c2802002-11-03 00:38:21 +0000573 case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
574 info->flash_id += FLASH_28F128J3A;
575 info->sector_count = 128;
576 info->size = 0x01000000;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200577 break; /* => 128 MBit */
wdenkfe8c2802002-11-03 00:38:21 +0000578
579 default:
580 /* FIXME*/
581 info->flash_id = FLASH_UNKNOWN;
582 return (0); /* => no or unknown flash */
583 }
584
585 flash_get_offsets(base, info);
586
587 /* check for protected sectors */
588 for (i = 0; i < info->sector_count; i++) {
589 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
590 /* D0 = 1 if protected */
591 addr = (volatile FLASH_WORD_SIZE *)(info->start[i]);
592 info->protect[i] = addr[2] & 1;
593 }
594
595 /*
596 * Prevent writes to uninitialized FLASH.
597 */
598 if (info->flash_id != FLASH_UNKNOWN) {
599 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
600 if( (info->flash_id & 0xFF00) == FLASH_MAN_INTEL){
601 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
602 } else {
603 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
604 }
605 }
606
607 return (info->size);
608}
609
610
611/*-----------------------------------------------------------------------
612 */
613
614int flash_erase (flash_info_t *info, int s_first, int s_last)
615{
616
617 volatile FLASH_WORD_SIZE *addr=(volatile FLASH_WORD_SIZE*)(info->start[0]);
618 int flag, prot, sect, l_sect, barf;
619 ulong start, now, last;
620 int rcode = 0;
621
622 if ((s_first < 0) || (s_first > s_last)) {
623 if (info->flash_id == FLASH_UNKNOWN) {
624 printf ("- missing\n");
625 } else {
626 printf ("- no sectors to erase\n");
627 }
628 return 1;
629 }
630
631 if ((info->flash_id == FLASH_UNKNOWN) ||
632 ((info->flash_id > FLASH_AMD_COMP) &&
wdenk57b2d802003-06-27 21:31:46 +0000633 ( (info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL ) ) ){
wdenkfe8c2802002-11-03 00:38:21 +0000634 printf ("Can't erase unknown flash type - aborted\n");
635 return 1;
636 }
637
638 prot = 0;
639 for (sect=s_first; sect<=s_last; ++sect) {
640 if (info->protect[sect]) {
641 prot++;
642 }
643 }
644
645 if (prot) {
646 printf ("- Warning: %d protected sectors will not be erased!\n",
647 prot);
648 } else {
649 printf ("\n");
650 }
651
652 l_sect = -1;
653
654 /* Disable interrupts which might cause a timeout here */
655 flag = disable_interrupts();
656 if(info->flash_id < FLASH_AMD_COMP) {
657#ifndef CFG_FLASH_16BIT
658 addr[0x0555] = 0x00AA00AA;
659 addr[0x02AA] = 0x00550055;
660 addr[0x0555] = 0x00800080;
661 addr[0x0555] = 0x00AA00AA;
662 addr[0x02AA] = 0x00550055;
663#else
664 addr[0x0555] = 0x00AA;
665 addr[0x02AA] = 0x0055;
666 addr[0x0555] = 0x0080;
667 addr[0x0555] = 0x00AA;
668 addr[0x02AA] = 0x0055;
669#endif
670 /* Start erase on unprotected sectors */
671 for (sect = s_first; sect<=s_last; sect++) {
672 if (info->protect[sect] == 0) { /* not protected */
673 addr = (volatile FLASH_WORD_SIZE *)(info->start[sect]);
674 addr[0] = (0x00300030 & FLASH_ID_MASK);
675 l_sect = sect;
676 }
677 }
678
679 /* re-enable interrupts if necessary */
680 if (flag)
681 enable_interrupts();
682
683 /* wait at least 80us - let's wait 1 ms */
684 udelay (1000);
685
686 /*
687 * We wait for the last triggered sector
688 */
689 if (l_sect < 0)
690 goto DONE;
691
692 start = get_timer (0);
693 last = start;
694 addr = (volatile FLASH_WORD_SIZE*)(info->start[l_sect]);
695 while ((addr[0] & (0x00800080&FLASH_ID_MASK)) !=
696 (0x00800080&FLASH_ID_MASK) )
697 {
698 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
699 printf ("Timeout\n");
700 return 1;
701 }
702 /* show that we're waiting */
703 if ((now - last) > 1000) { /* every second */
704 serial_putc ('.');
705 last = now;
706 }
707 }
708
709DONE:
710 /* reset to read mode */
711 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
712 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
713 } else {
714
715
716 for (sect = s_first; sect<=s_last; sect++) {
717 if (info->protect[sect] == 0) { /* not protected */
718 barf = 0;
719#ifndef CFG_FLASH_16BIT
720 addr = (vu_long*)(info->start[sect]);
721 addr[0] = 0x00200020;
722 addr[0] = 0x00D000D0;
723 while(!(addr[0] & 0x00800080)); /* wait for error or finish */
724 if( addr[0] & 0x003A003A) { /* check for error */
725 barf = addr[0] & 0x003A0000;
726 if( barf ) {
727 barf >>=16;
728 } else {
729 barf = addr[0] & 0x0000003A;
730 }
731 }
732#else
733 addr = (vu_short*)(info->start[sect]);
734 addr[0] = 0x0020;
735 addr[0] = 0x00D0;
736 while(!(addr[0] & 0x0080)); /* wait for error or finish */
737 if( addr[0] & 0x003A) /* check for error */
738 barf = addr[0] & 0x003A;
739#endif
740 if(barf) {
741 printf("\nFlash error in sector at %lx\n",(unsigned long)addr);
742 if(barf & 0x0002) printf("Block locked, not erased.\n");
743 if((barf & 0x0030) == 0x0030)
744 printf("Command Sequence error.\n");
745 if((barf & 0x0030) == 0x0020)
746 printf("Block Erase error.\n");
747 if(barf & 0x0008) printf("Vpp Low error.\n");
748 rcode = 1;
749 } else printf(".");
750 l_sect = sect;
751 }
752 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
753 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
754
755 }
756
757 }
758 printf (" done\n");
759 return rcode;
760}
761
762/*-----------------------------------------------------------------------
763 */
764
765/*flash_info_t *addr2info (ulong addr)
766{
767 flash_info_t *info;
768 int i;
769
770 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
771 if ((addr >= info->start[0]) &&
772 (addr < (info->start[0] + info->size)) ) {
773 return (info);
774 }
775 }
776
777 return (NULL);
778}
779*/
780/*-----------------------------------------------------------------------
781 * Copy memory to flash.
782 * Make sure all target addresses are within Flash bounds,
783 * and no protected sectors are hit.
784 * Returns:
785 * 0 - OK
786 * 1 - write timeout
787 * 2 - Flash not erased
788 * 4 - target range includes protected sectors
789 * 8 - target address not in Flash memory
790 */
791
792/*int flash_write (uchar *src, ulong addr, ulong cnt)
793{
794 int i;
795 ulong end = addr + cnt - 1;
796 flash_info_t *info_first = addr2info (addr);
797 flash_info_t *info_last = addr2info (end );
798 flash_info_t *info;
799
800 if (cnt == 0) {
801 return (0);
802 }
803
804 if (!info_first || !info_last) {
805 return (8);
806 }
807
808 for (info = info_first; info <= info_last; ++info) {
809 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
810/* short s_end = info->sector_count - 1;
811 for (i=0; i<info->sector_count; ++i) {
812 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
813
814 if ((end >= info->start[i]) && (addr < e_addr) &&
815 (info->protect[i] != 0) ) {
816 return (4);
817 }
818 }
819 }
820
821*/ /* finally write data to flash */
822/* for (info = info_first; info <= info_last && cnt>0; ++info) {
823 ulong len;
824
825 len = info->start[0] + info->size - addr;
826 if (len > cnt)
827 len = cnt;
828 if ((i = write_buff(info, src, addr, len)) != 0) {
829 return (i);
830 }
831 cnt -= len;
832 addr += len;
833 src += len;
834 }
835 return (0);
836}
837*/
838/*-----------------------------------------------------------------------
839 * Copy memory to flash, returns:
840 * 0 - OK
841 * 1 - write timeout
842 * 2 - Flash not erased
843 */
844
845int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
846{
847#ifndef CFG_FLASH_16BIT
848 ulong cp, wp, data;
849 int l;
850#else
851 ulong cp, wp;
852 ushort data;
853#endif
854 int i, rc;
855
856#ifndef CFG_FLASH_16BIT
857
858
859 wp = (addr & ~3); /* get lower word aligned address */
860
861 /*
862 * handle unaligned start bytes
863 */
864 if ((l = addr - wp) != 0) {
865 data = 0;
866 for (i=0, cp=wp; i<l; ++i, ++cp) {
867 data = (data << 8) | (*(uchar *)cp);
868 }
869 for (; i<4 && cnt>0; ++i) {
870 data = (data << 8) | *src++;
871 --cnt;
872 ++cp;
873 }
874 for (; cnt==0 && i<4; ++i, ++cp) {
875 data = (data << 8) | (*(uchar *)cp);
876 }
877
878 if ((rc = write_word(info, wp, data)) != 0) {
879 return (rc);
880 }
881 wp += 4;
882 }
883
884 /*
885 * handle word aligned part
886 */
887 while (cnt >= 4) {
888 data = 0;
889 for (i=0; i<4; ++i) {
890 data = (data << 8) | *src++;
891 }
892 if ((rc = write_word(info, wp, data)) != 0) {
893 return (rc);
894 }
895 wp += 4;
896 cnt -= 4;
897 }
898
899 if (cnt == 0) {
900 return (0);
901 }
902
903 /*
904 * handle unaligned tail bytes
905 */
906 data = 0;
907 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
908 data = (data << 8) | *src++;
909 --cnt;
910 }
911 for (; i<4; ++i, ++cp) {
912 data = (data << 8) | (*(uchar *)cp);
913 }
914
915 return (write_word(info, wp, data));
916
917#else
918 wp = (addr & ~1); /* get lower word aligned address */
919
920 /*
921 * handle unaligned start byte
922 */
923 if (addr - wp) {
924 data = 0;
925 data = (data << 8) | *src++;
926 --cnt;
927 if ((rc = write_short(info, wp, data)) != 0) {
928 return (rc);
929 }
930 wp += 2;
931 }
932
933 /*
934 * handle word aligned part
935 */
936/* l = 0; used for debuging */
937 while (cnt >= 2) {
938 data = 0;
939 for (i=0; i<2; ++i) {
940 data = (data << 8) | *src++;
941 }
942
943/* if(!l){
944 printf("%x",data);
945 l = 1;
946 } used for debuging */
947
948 if ((rc = write_short(info, wp, data)) != 0) {
949 return (rc);
950 }
951 wp += 2;
952 cnt -= 2;
953 }
954
955 if (cnt == 0) {
956 return (0);
957 }
958
959 /*
960 * handle unaligned tail bytes
961 */
962 data = 0;
963 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
964 data = (data << 8) | *src++;
965 --cnt;
966 }
967 for (; i<2; ++i, ++cp) {
968 data = (data << 8) | (*(uchar *)cp);
969 }
970
971 return (write_short(info, wp, data));
972
973
974#endif
975}
976
977/*-----------------------------------------------------------------------
978 * Write a word to Flash, returns:
979 * 0 - OK
980 * 1 - write timeout
981 * 2 - Flash not erased
982 */
983#ifndef CFG_FLASH_16BIT
Wolfgang Denka1be4762008-05-20 16:00:29 +0200984static int write_word (flash_info_t * info, ulong dest, ulong data)
wdenkfe8c2802002-11-03 00:38:21 +0000985{
Wolfgang Denka1be4762008-05-20 16:00:29 +0200986 vu_long *addr = (vu_long *) (info->start[0]);
987 ulong start, barf;
wdenkfe8c2802002-11-03 00:38:21 +0000988 int flag;
989
990
991 /* Check if Flash is (sufficiently) erased */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200992 if ((*((vu_long *) dest) & data) != data) {
wdenkfe8c2802002-11-03 00:38:21 +0000993 return (2);
994 }
995
996 /* Disable interrupts which might cause a timeout here */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200997 flag = disable_interrupts ();
wdenkfe8c2802002-11-03 00:38:21 +0000998
Wolfgang Denka1be4762008-05-20 16:00:29 +0200999 if (info->flash_id > FLASH_AMD_COMP) {
1000 /* AMD stuff */
1001 addr[0x0555] = 0x00AA00AA;
1002 addr[0x02AA] = 0x00550055;
1003 addr[0x0555] = 0x00A000A0;
1004 } else {
1005 /* intel stuff */
1006 *addr = 0x00400040;
1007 }
1008 *((vu_long *) dest) = data;
wdenkfe8c2802002-11-03 00:38:21 +00001009
1010 /* re-enable interrupts if necessary */
1011 if (flag)
Wolfgang Denka1be4762008-05-20 16:00:29 +02001012 enable_interrupts ();
wdenkfe8c2802002-11-03 00:38:21 +00001013
1014 /* data polling for D7 */
1015 start = get_timer (0);
1016
Wolfgang Denka1be4762008-05-20 16:00:29 +02001017 if (info->flash_id > FLASH_AMD_COMP) {
wdenkfe8c2802002-11-03 00:38:21 +00001018
Wolfgang Denka1be4762008-05-20 16:00:29 +02001019 while ((*((vu_long *) dest) & 0x00800080) !=
1020 (data & 0x00800080)) {
1021 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
1022 return (1);
1023 }
wdenkfe8c2802002-11-03 00:38:21 +00001024 }
wdenkfe8c2802002-11-03 00:38:21 +00001025
Wolfgang Denka1be4762008-05-20 16:00:29 +02001026 } else {
wdenkfe8c2802002-11-03 00:38:21 +00001027
Wolfgang Denka1be4762008-05-20 16:00:29 +02001028 while (!(addr[0] & 0x00800080)) { /* wait for error or finish */
1029 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
1030 return (1);
1031 }
wdenkfe8c2802002-11-03 00:38:21 +00001032
Wolfgang Denka1be4762008-05-20 16:00:29 +02001033 if (addr[0] & 0x003A003A) { /* check for error */
1034 barf = addr[0] & 0x003A0000;
1035 if (barf) {
1036 barf >>= 16;
1037 } else {
1038 barf = addr[0] & 0x0000003A;
1039 }
1040 printf ("\nFlash write error at address %lx\n",
1041 (unsigned long) dest);
1042 if (barf & 0x0002)
1043 printf ("Block locked, not erased.\n");
1044 if (barf & 0x0010)
1045 printf ("Programming error.\n");
1046 if (barf & 0x0008)
1047 printf ("Vpp Low error.\n");
1048 return (2);
1049 }
wdenkfe8c2802002-11-03 00:38:21 +00001050
1051
Wolfgang Denka1be4762008-05-20 16:00:29 +02001052 }
wdenkfe8c2802002-11-03 00:38:21 +00001053
Wolfgang Denka1be4762008-05-20 16:00:29 +02001054 return (0);
wdenkfe8c2802002-11-03 00:38:21 +00001055
Wolfgang Denka1be4762008-05-20 16:00:29 +02001056 }
wdenkfe8c2802002-11-03 00:38:21 +00001057
1058#else
1059
Wolfgang Denka1be4762008-05-20 16:00:29 +02001060static int write_short (flash_info_t * info, ulong dest, ushort data)
wdenkfe8c2802002-11-03 00:38:21 +00001061{
Wolfgang Denka1be4762008-05-20 16:00:29 +02001062 vu_short *addr = (vu_short *) (info->start[0]);
1063 ulong start, barf;
wdenkfe8c2802002-11-03 00:38:21 +00001064 int flag;
1065
1066 /* Check if Flash is (sufficiently) erased */
Wolfgang Denka1be4762008-05-20 16:00:29 +02001067 if ((*((vu_short *) dest) & data) != data) {
wdenkfe8c2802002-11-03 00:38:21 +00001068 return (2);
1069 }
1070
1071 /* Disable interrupts which might cause a timeout here */
Wolfgang Denka1be4762008-05-20 16:00:29 +02001072 flag = disable_interrupts ();
wdenkfe8c2802002-11-03 00:38:21 +00001073
Wolfgang Denka1be4762008-05-20 16:00:29 +02001074 if (info->flash_id < FLASH_AMD_COMP) {
1075 /* AMD stuff */
1076 addr[0x0555] = 0x00AA;
1077 addr[0x02AA] = 0x0055;
1078 addr[0x0555] = 0x00A0;
1079 } else {
1080 /* intel stuff */
1081 *addr = 0x00D0;
1082 *addr = 0x0040;
1083 }
1084 *((vu_short *) dest) = data;
wdenkfe8c2802002-11-03 00:38:21 +00001085
1086 /* re-enable interrupts if necessary */
1087 if (flag)
Wolfgang Denka1be4762008-05-20 16:00:29 +02001088 enable_interrupts ();
wdenkfe8c2802002-11-03 00:38:21 +00001089
1090 /* data polling for D7 */
1091 start = get_timer (0);
1092
Wolfgang Denka1be4762008-05-20 16:00:29 +02001093 if (info->flash_id < FLASH_AMD_COMP) {
1094 /* AMD stuff */
1095 while ((*((vu_short *) dest) & 0x0080) != (data & 0x0080)) {
1096 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
1097 return (1);
1098 }
wdenkfe8c2802002-11-03 00:38:21 +00001099 }
wdenkfe8c2802002-11-03 00:38:21 +00001100
Wolfgang Denka1be4762008-05-20 16:00:29 +02001101 } else {
1102 /* intel stuff */
1103 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
1104 if (get_timer (start) > CFG_FLASH_WRITE_TOUT)
1105 return (1);
1106 }
wdenkfe8c2802002-11-03 00:38:21 +00001107
Wolfgang Denka1be4762008-05-20 16:00:29 +02001108 if (addr[0] & 0x003A) { /* check for error */
1109 barf = addr[0] & 0x003A;
1110 printf ("\nFlash write error at address %lx\n",
1111 (unsigned long) dest);
1112 if (barf & 0x0002)
1113 printf ("Block locked, not erased.\n");
1114 if (barf & 0x0010)
1115 printf ("Programming error.\n");
1116 if (barf & 0x0008)
1117 printf ("Vpp Low error.\n");
1118 return (2);
1119 }
1120 *addr = 0x00B0;
1121 *addr = 0x0070;
1122 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
1123 if (get_timer (start) > CFG_FLASH_WRITE_TOUT)
1124 return (1);
1125 }
1126 *addr = 0x00FF;
wdenkfe8c2802002-11-03 00:38:21 +00001127 }
wdenkfe8c2802002-11-03 00:38:21 +00001128 return (0);
wdenkfe8c2802002-11-03 00:38:21 +00001129}
wdenkfe8c2802002-11-03 00:38:21 +00001130#endif
Wolfgang Denka1be4762008-05-20 16:00:29 +02001131/*-----------------------------------------------------------------------*/