1 /*
2 * (C) Copyright 2004-2005
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
6 * Add support for Am29F016D and dynamic switch setting.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27 /*
28 * Modified 4/5/2001
29 * Wait for completion of each sector erase command issued
30 * 4/5/2001
31 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
32 */
33
34 #include <common.h>
35 #include <ppc4xx.h>
36 #include <asm/processor.h>
37 #include "flash_local.h"
38
39 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
40
41 /*-----------------------------------------------------------------------
42 * Functions
43 */
44 int write_word(flash_info_t * info, ulong dest, ulong data, ulong *pippo);
45
flash_print_info(flash_info_t * info)46 void flash_print_info(flash_info_t * info)
47 {
48 int i;
49 int k;
50 int size;
51 int erased;
52 volatile unsigned long *flash;
53
54 if (info->flash_id == FLASH_UNKNOWN) {
55 printf("missing or unknown FLASH type\n");
56 return;
57 }
58
59 switch (info->flash_id & FLASH_VENDMASK) {
60 case FLASH_MAN_AMD:
61 printf("AMD ");
62 break;
63 case FLASH_MAN_STM:
64 printf("STM ");
65 break;
66 case FLASH_MAN_FUJ:
67 printf("FUJITSU ");
68 break;
69 case FLASH_MAN_SST:
70 printf("SST ");
71 break;
72 case FLASH_MAN_MX:
73 printf ("MACRONIX ");
74 break;
75 case FLASH_MAN_ATM:
76 printf ("ATMEL ");
77 break;
78 default:
79 printf("Unknown Vendor ");
80 break;
81 }
82
83 switch (info->flash_id & FLASH_TYPEMASK) {
84 case FLASH_AT040:
85 printf("AT49F040 (4 Mbit, variable sector size)\n");
86 break;
87 case FLASH_AM040:
88 printf("AM29F040 (4 Mbit, uniform sector size)\n");
89 break;
90 case FLASH_AM400B:
91 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
92 break;
93 case FLASH_AM400T:
94 printf("AM29LV400T (4 Mbit, top boot sector)\n");
95 break;
96 case FLASH_AM800B:
97 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
98 break;
99 case FLASH_AM800T:
100 printf("AM29LV800T (8 Mbit, top boot sector)\n");
101 break;
102 case FLASH_AMD016:
103 printf("AM29F016D (16 Mbit, uniform sector size)\n");
104 break;
105 case FLASH_AM160B:
106 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
107 break;
108 case FLASH_AM160T:
109 printf("AM29LV160T (16 Mbit, top boot sector)\n");
110 break;
111 case FLASH_AM320B:
112 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
113 break;
114 case FLASH_AM320T:
115 printf("AM29LV320T (32 Mbit, top boot sector)\n");
116 break;
117 case FLASH_AM033C:
118 printf("AM29LV033C (32 Mbit, top boot sector)\n");
119 break;
120 case FLASH_SST800A:
121 printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
122 break;
123 case FLASH_SST160A:
124 printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
125 break;
126 case FLASH_STMW320DT:
127 printf ("M29W320DT (32 M, top sector)\n");
128 break;
129 case FLASH_MXLV320T:
130 printf ("MXLV320T (32 Mbit, top sector)\n");
131 break;
132 default:
133 printf("Unknown Chip Type\n");
134 break;
135 }
136
137 printf(" Size: %ld KB in %d Sectors\n",
138 info->size >> 10, info->sector_count);
139
140 printf(" Sector Start Addresses:");
141 for (i = 0; i < info->sector_count; ++i) {
142 /*
143 * Check if whole sector is erased
144 */
145 if (i != (info->sector_count - 1))
146 size = info->start[i + 1] - info->start[i];
147 else
148 size = info->start[0] + info->size - info->start[i];
149 erased = 1;
150 flash = (volatile unsigned long *)info->start[i];
151 size = size >> 2; /* divide by 4 for longword access */
152 for (k = 0; k < size; k++) {
153 if (*flash++ != 0xffffffff) {
154 erased = 0;
155 break;
156 }
157 }
158
159 if ((i % 5) == 0)
160 printf("\n ");
161 printf(" %08lX%s%s",
162 info->start[i],
163 erased ? " E" : " ", info->protect[i] ? "RO " : " ");
164 }
165 printf("\n");
166 return;
167 }
168
flash_get_size(vu_long * addr,flash_info_t * info)169 ulong flash_get_size(vu_long * addr, flash_info_t * info)
170 {
171 short i;
172 CONFIG_SYS_FLASH_WORD_SIZE value;
173 ulong base = (ulong) addr;
174 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
175
176 printf("FLASH ADDR: %08x\n", (unsigned)addr);
177
178 /* Write auto select command: read Manufacturer ID */
179 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
180 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
181 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
182 udelay(1000);
183
184 value = addr2[0];
185 printf("FLASH MANUFACT: %x\n", value);
186
187 switch (value) {
188 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
189 info->flash_id = FLASH_MAN_AMD;
190 break;
191 case (CONFIG_SYS_FLASH_WORD_SIZE) ATM_MANUFACT:
192 info->flash_id = FLASH_MAN_ATM;
193 break;
194 case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
195 info->flash_id = FLASH_MAN_FUJ;
196 break;
197 case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
198 info->flash_id = FLASH_MAN_SST;
199 break;
200 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
201 info->flash_id = FLASH_MAN_STM;
202 break;
203 default:
204 info->flash_id = FLASH_UNKNOWN;
205 info->sector_count = 0;
206 info->size = 0;
207 return (0); /* no or unknown flash */
208 }
209
210 value = addr2[1]; /* device ID */
211 printf("FLASH DEVICEID: %x\n", value);
212
213 switch (value) {
214 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV040B:
215 info->flash_id += FLASH_AM040;
216 info->sector_count = 8;
217 info->size = 0x0080000; /* => 512 KiB */
218 break;
219
220 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F040B:
221 info->flash_id += FLASH_AM040;
222 info->sector_count = 8;
223 info->size = 0x0080000; /* => 512 KiB */
224 break;
225
226 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_ID_M29W040B:
227 info->flash_id += FLASH_AM040;
228 info->sector_count = 8;
229 info->size = 0x0080000; /* => 512 KiB */
230 break;
231
232 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F016D:
233 info->flash_id += FLASH_AMD016;
234 info->sector_count = 32;
235 info->size = 0x00200000; /* => 2 MiB */
236 break;
237
238 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV033C:
239 info->flash_id += FLASH_AMDLV033C;
240 info->sector_count = 64;
241 info->size = 0x00400000; /* => 4 MiB */
242 break;
243
244 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400T:
245 info->flash_id += FLASH_AM400T;
246 info->sector_count = 11;
247 info->size = 0x00080000; /* => 512 KiB */
248 break;
249
250 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400B:
251 info->flash_id += FLASH_AM400B;
252 info->sector_count = 11;
253 info->size = 0x00080000; /* => 512 KiB */
254 break;
255
256 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800T:
257 info->flash_id += FLASH_AM800T;
258 info->sector_count = 19;
259 info->size = 0x00100000; /* => 1 MiB */
260 break;
261
262 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800B:
263 info->flash_id += FLASH_AM800B;
264 info->sector_count = 19;
265 info->size = 0x00100000; /* => 1 MiB */
266 break;
267
268 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160T:
269 info->flash_id += FLASH_AM160T;
270 info->sector_count = 35;
271 info->size = 0x00200000; /* => 2 MiB */
272 break;
273
274 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160B:
275 info->flash_id += FLASH_AM160B;
276 info->sector_count = 35;
277 info->size = 0x00200000; /* => 2 MiB */
278 break;
279
280 case (CONFIG_SYS_FLASH_WORD_SIZE) ATM_ID_BV040:
281 info->flash_id += FLASH_AT040;
282 info->sector_count = 11;
283 info->size = 0x00080000;
284 break;
285
286 default:
287 info->flash_id = FLASH_UNKNOWN;
288 return (0); /* => no or unknown flash */
289 }
290
291 /* set up sector start address table */
292 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
293 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
294 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
295 for (i = 0; i < info->sector_count; i++)
296 info->start[i] = base + (i * 0x00010000);
297 } else {
298 if (info->flash_id & FLASH_BTYPE) {
299 /* set sector offsets for bottom boot block type */
300 info->start[0] = base + 0x00000000;
301 info->start[1] = base + 0x00004000;
302 info->start[2] = base + 0x00006000;
303 info->start[3] = base + 0x00008000;
304 for (i = 4; i < info->sector_count; i++) {
305 info->start[i] =
306 base + (i * 0x00010000) - 0x00030000;
307 }
308 } else {
309 /* set sector offsets for top boot block type */
310 i = info->sector_count - 1;
311 info->start[i--] = base + info->size - 0x00004000;
312 info->start[i--] = base + info->size - 0x00006000;
313 info->start[i--] = base + info->size - 0x00008000;
314 for (; i >= 0; i--) {
315 info->start[i] = base + i * 0x00010000;
316 }
317 }
318 }
319
320 /* check for protected sectors */
321 for (i = 0; i < info->sector_count; i++) {
322 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
323 /* D0 = 1 if protected */
324 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
325
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 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
330 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
331 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
332 }
333
334 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
335 info->protect[i] = 0;
336 else
337 info->protect[i] = addr2[2] & 1;
338 }
339
340 /* issue bank reset to return to read mode */
341 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
342
343 return (info->size);
344 }
345
wait_for_DQ7_1(flash_info_t * info,int sect)346 static int wait_for_DQ7_1(flash_info_t * info, int sect)
347 {
348 ulong start, now, last;
349 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
350 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
351
352 start = get_timer(0);
353 last = start;
354 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
355 (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
356 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
357 printf("Timeout\n");
358 return -1;
359 }
360 /* show that we're waiting */
361 if ((now - last) > 1000) { /* every second */
362 putc('.');
363 last = now;
364 }
365 }
366 return 0;
367 }
368
flash_erase(flash_info_t * info,int s_first,int s_last)369 int flash_erase(flash_info_t * info, int s_first, int s_last)
370 {
371 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
372 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
373 int flag, prot, sect, l_sect;
374 int i;
375
376 if ((s_first < 0) || (s_first > s_last)) {
377 if (info->flash_id == FLASH_UNKNOWN) {
378 printf("- missing\n");
379 } else {
380 printf("- no sectors to erase\n");
381 }
382 return 1;
383 }
384
385 if (info->flash_id == FLASH_UNKNOWN) {
386 printf("Can't erase unknown flash type - aborted\n");
387 return 1;
388 }
389
390 prot = 0;
391 for (sect = s_first; sect <= s_last; ++sect) {
392 if (info->protect[sect]) {
393 prot++;
394 }
395 }
396
397 if (prot) {
398 printf("- Warning: %d protected sectors will not be erased!\n",
399 prot);
400 } else {
401 //printf("\n");
402 }
403
404 l_sect = -1;
405
406 /* Disable interrupts which might cause a timeout here */
407 flag = disable_interrupts();
408
409 /* Start erase on unprotected sectors */
410 for (sect = s_first; sect <= s_last; sect++) {
411 if (info->protect[sect] == 0) { /* not protected */
412 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
413
414 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
415 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
416 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
417 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
418 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
419 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
420 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
421 for (i = 0; i < 50; i++)
422 udelay(1500); /* wait 1.5 ms */
423 } else {
424 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
425 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
426 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
427 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
428 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
429 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
430 }
431 l_sect = sect;
432 /*
433 * Wait for each sector to complete, it's more
434 * reliable. According to AMD Spec, you must
435 * issue all erase commands within a specified
436 * timeout. This has been seen to fail, especially
437 * if printf()s are included (for debug)!!
438 */
439 wait_for_DQ7_1(info, sect);
440 }
441 }
442
443 /* re-enable interrupts if necessary */
444 if (flag)
445 enable_interrupts();
446
447 /* wait at least 80us - let's wait 1.5 ms */
448 udelay(1500);
449
450 /* reset to read mode */
451 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
452 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 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 */
write_buff(flash_info_t * info,uchar * src,ulong addr,ulong cnt)464 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
465 {
466 ulong cp, wp, data;
467 int i, l, rc;
468 static ulong pippo = 1;
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, &pippo)) != 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, &pippo)) != 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, &pippo));
527 }
528
529 /*-----------------------------------------------------------------------
530 * Copy memory to flash, returns:
531 * 0 - OK
532 * 1 - write timeout
533 * 2 - Flash not erased
534 */
write_word(flash_info_t * info,ulong dest,ulong data,ulong * pippo)535 int write_word(flash_info_t * info, ulong dest, ulong data, ulong *pippo)
536 {
537 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
538 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
539 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *) & data;
540 ulong start;
541 int i;
542
543 /* Check if Flash is (sufficiently) erased */
544 if ((*((vu_long *)dest) & data) != data) {
545 return (2);
546 }
547
548 ++(*pippo);
549 if (*pippo == 11915) { *pippo = 0; putc(219); }
550
551 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
552 int flag;
553
554 /* Disable interrupts which might cause a timeout here */
555 flag = disable_interrupts();
556
557 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
558 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
559 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
560
561 dest2[i] = data2[i];
562
563 /* re-enable interrupts if necessary */
564 if (flag)
565 enable_interrupts();
566
567 /* data polling for D7 */
568 start = get_timer(0);
569 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
570 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
571
572 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
573 return (1);
574 }
575 }
576 }
577
578 return (0);
579 }
580