blob: 7311c4edd51a4ba2c80ca395c4b41fb33aad009a [file] [log] [blame]
Stefan Roese8a316c92005-08-01 16:49:12 +02001/*
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
38flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
39
40/*-----------------------------------------------------------------------
41 * Functions
42 */
43static int write_word(flash_info_t * info, ulong dest, ulong data);
44
45void flash_print_info(flash_info_t * info)
46{
47 int i;
48 int k;
49 int size;
50 int erased;
51 volatile unsigned long *flash;
52
53 if (info->flash_id == FLASH_UNKNOWN) {
54 printf("missing or unknown FLASH type\n");
55 return;
56 }
57
58 switch (info->flash_id & FLASH_VENDMASK) {
59 case FLASH_MAN_AMD:
60 printf("AMD ");
61 break;
62 case FLASH_MAN_STM:
63 printf("STM ");
64 break;
65 case FLASH_MAN_FUJ:
66 printf("FUJITSU ");
67 break;
68 case FLASH_MAN_SST:
69 printf("SST ");
70 break;
71 default:
72 printf("Unknown Vendor ");
73 break;
74 }
75
76 switch (info->flash_id & FLASH_TYPEMASK) {
77 case FLASH_AM040:
78 printf("AM29F040 (512 Kbit, uniform sector size)\n");
79 break;
80 case FLASH_AM400B:
81 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
82 break;
83 case FLASH_AM400T:
84 printf("AM29LV400T (4 Mbit, top boot sector)\n");
85 break;
86 case FLASH_AM800B:
87 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
88 break;
89 case FLASH_AM800T:
90 printf("AM29LV800T (8 Mbit, top boot sector)\n");
91 break;
92 case FLASH_AMD016:
93 printf("AM29F016D (16 Mbit, uniform sector size)\n");
94 break;
95 case FLASH_AM160B:
96 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
97 break;
98 case FLASH_AM160T:
99 printf("AM29LV160T (16 Mbit, top boot sector)\n");
100 break;
101 case FLASH_AM320B:
102 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
103 break;
104 case FLASH_AM320T:
105 printf("AM29LV320T (32 Mbit, top boot sector)\n");
106 break;
107 case FLASH_AM033C:
108 printf("AM29LV033C (32 Mbit, top boot sector)\n");
109 break;
110 case FLASH_SST800A:
111 printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
112 break;
113 case FLASH_SST160A:
114 printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
115 break;
116 default:
117 printf("Unknown Chip Type\n");
118 break;
119 }
120
121 printf(" Size: %ld KB in %d Sectors\n",
122 info->size >> 10, info->sector_count);
123
124 printf(" Sector Start Addresses:");
125 for (i = 0; i < info->sector_count; ++i) {
126 /*
127 * Check if whole sector is erased
128 */
129 if (i != (info->sector_count - 1))
130 size = info->start[i + 1] - info->start[i];
131 else
132 size = info->start[0] + info->size - info->start[i];
133 erased = 1;
134 flash = (volatile unsigned long *)info->start[i];
135 size = size >> 2; /* divide by 4 for longword access */
136 for (k = 0; k < size; k++) {
137 if (*flash++ != 0xffffffff) {
138 erased = 0;
139 break;
140 }
141 }
142
143 if ((i % 5) == 0)
144 printf("\n ");
145 printf(" %08lX%s%s",
146 info->start[i],
147 erased ? " E" : " ", info->protect[i] ? "RO " : " ");
148 }
149 printf("\n");
150 return;
151}
152
153
154/*
155 * The following code cannot be run from FLASH!
156 */
157static ulong flash_get_size(vu_long * addr, flash_info_t * info)
158{
159 short i;
160 CFG_FLASH_WORD_SIZE value;
161 ulong base = (ulong) addr;
162 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) addr;
163
164 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
165
166 /* Write auto select command: read Manufacturer ID */
167 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
168 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
169 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00900090;
170 udelay(1000);
171
172 value = addr2[0];
173 DEBUGF("FLASH MANUFACT: %x\n", value);
174
175 switch (value) {
176 case (CFG_FLASH_WORD_SIZE) AMD_MANUFACT:
177 info->flash_id = FLASH_MAN_AMD;
178 break;
179 case (CFG_FLASH_WORD_SIZE) FUJ_MANUFACT:
180 info->flash_id = FLASH_MAN_FUJ;
181 break;
182 case (CFG_FLASH_WORD_SIZE) SST_MANUFACT:
183 info->flash_id = FLASH_MAN_SST;
184 break;
185 case (CFG_FLASH_WORD_SIZE) STM_MANUFACT:
186 info->flash_id = FLASH_MAN_STM;
187 break;
188 default:
189 info->flash_id = FLASH_UNKNOWN;
190 info->sector_count = 0;
191 info->size = 0;
192 return (0); /* no or unknown flash */
193 }
194
195 value = addr2[1]; /* device ID */
196
197 DEBUGF("\nFLASH DEVICEID: %x\n", value);
198
199 switch (value) {
200 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV040B:
201 info->flash_id += FLASH_AM040;
202 info->sector_count = 8;
203 info->size = 0x0080000; /* => 512 ko */
204 break;
205
206 case (CFG_FLASH_WORD_SIZE) AMD_ID_F040B:
207 info->flash_id += FLASH_AM040;
208 info->sector_count = 8;
209 info->size = 0x0080000; /* => 512 ko */
210 break;
211
212 case (CFG_FLASH_WORD_SIZE) STM_ID_M29W040B:
213 info->flash_id += FLASH_AM040;
214 info->sector_count = 8;
215 info->size = 0x0080000; /* => 512 ko */
216 break;
217
218 case (CFG_FLASH_WORD_SIZE) AMD_ID_F016D:
219 info->flash_id += FLASH_AMD016;
220 info->sector_count = 32;
221 info->size = 0x00200000;
222 break; /* => 2 MB */
223
224 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV033C:
225 info->flash_id += FLASH_AMDLV033C;
226 info->sector_count = 64;
227 info->size = 0x00400000;
228 break; /* => 4 MB */
229
230 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV400T:
231 info->flash_id += FLASH_AM400T;
232 info->sector_count = 11;
233 info->size = 0x00080000;
234 break; /* => 0.5 MB */
235
236 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV400B:
237 info->flash_id += FLASH_AM400B;
238 info->sector_count = 11;
239 info->size = 0x00080000;
240 break; /* => 0.5 MB */
241
242 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV800T:
243 info->flash_id += FLASH_AM800T;
244 info->sector_count = 19;
245 info->size = 0x00100000;
246 break; /* => 1 MB */
247
248 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV800B:
249 info->flash_id += FLASH_AM800B;
250 info->sector_count = 19;
251 info->size = 0x00100000;
252 break; /* => 1 MB */
253
254 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV160T:
255 info->flash_id += FLASH_AM160T;
256 info->sector_count = 35;
257 info->size = 0x00200000;
258 break; /* => 2 MB */
259
260 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV160B:
261 info->flash_id += FLASH_AM160B;
262 info->sector_count = 35;
263 info->size = 0x00200000;
264 break; /* => 2 MB */
265
266 default:
267 info->flash_id = FLASH_UNKNOWN;
268 return (0); /* => no or unknown flash */
269 }
270
271 /* set up sector start address table */
272 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
273 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
274 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
275 for (i = 0; i < info->sector_count; i++)
276 info->start[i] = base + (i * 0x00010000);
277 } else {
278 if (info->flash_id & FLASH_BTYPE) {
279 /* set sector offsets for bottom boot block type */
280 info->start[0] = base + 0x00000000;
281 info->start[1] = base + 0x00004000;
282 info->start[2] = base + 0x00006000;
283 info->start[3] = base + 0x00008000;
284 for (i = 4; i < info->sector_count; i++) {
285 info->start[i] =
286 base + (i * 0x00010000) - 0x00030000;
287 }
288 } else {
289 /* set sector offsets for top boot block type */
290 i = info->sector_count - 1;
291 info->start[i--] = base + info->size - 0x00004000;
292 info->start[i--] = base + info->size - 0x00006000;
293 info->start[i--] = base + info->size - 0x00008000;
294 for (; i >= 0; i--) {
295 info->start[i] = base + i * 0x00010000;
296 }
297 }
298 }
299
300 /* check for protected sectors */
301 for (i = 0; i < info->sector_count; i++) {
302 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
303 /* D0 = 1 if protected */
304 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
305
306 /* For AMD29033C flash we need to resend the command of *
307 * reading flash protection for upper 8 Mb of flash */
308 if (i == 32) {
309 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
310 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
311 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x90909090;
312 }
313
314 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
315 info->protect[i] = 0;
316 else
317 info->protect[i] = addr2[2] & 1;
318 }
319
320 /* issue bank reset to return to read mode */
321 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0;
322
323 return (info->size);
324}
325
326int wait_for_DQ7(flash_info_t * info, int sect)
327{
328 ulong start, now, last;
329 volatile CFG_FLASH_WORD_SIZE *addr =
330 (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
331
332 start = get_timer(0);
333 last = start;
334 while ((addr[0] & (CFG_FLASH_WORD_SIZE) 0x00800080) !=
335 (CFG_FLASH_WORD_SIZE) 0x00800080) {
336 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
337 printf("Timeout\n");
338 return -1;
339 }
340 /* show that we're waiting */
341 if ((now - last) > 1000) { /* every second */
342 putc('.');
343 last = now;
344 }
345 }
346 return 0;
347}
348
349int flash_erase(flash_info_t * info, int s_first, int s_last)
350{
351 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
352 volatile CFG_FLASH_WORD_SIZE *addr2;
353 int flag, prot, sect, l_sect;
354 int i;
355
356 if ((s_first < 0) || (s_first > s_last)) {
357 if (info->flash_id == FLASH_UNKNOWN) {
358 printf("- missing\n");
359 } else {
360 printf("- no sectors to erase\n");
361 }
362 return 1;
363 }
364
365 if (info->flash_id == FLASH_UNKNOWN) {
366 printf("Can't erase unknown flash type - aborted\n");
367 return 1;
368 }
369
370 prot = 0;
371 for (sect = s_first; sect <= s_last; ++sect) {
372 if (info->protect[sect]) {
373 prot++;
374 }
375 }
376
377 if (prot) {
378 printf("- Warning: %d protected sectors will not be erased!\n",
379 prot);
380 } else {
381 printf("\n");
382 }
383
384 l_sect = -1;
385
386 /* Disable interrupts which might cause a timeout here */
387 flag = disable_interrupts();
388
389 /* Start erase on unprotected sectors */
390 for (sect = s_first; sect <= s_last; sect++) {
391 if (info->protect[sect] == 0) { /* not protected */
392 addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
393
394 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
395 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
396 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
397 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00800080;
398 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
399 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
400 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00500050; /* block erase */
401 for (i = 0; i < 50; i++)
402 udelay(1000); /* wait 1 ms */
403 } else {
404 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
405 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
406 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00800080;
407 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
408 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
409 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
410 }
411 l_sect = sect;
412 /*
413 * Wait for each sector to complete, it's more
414 * reliable. According to AMD Spec, you must
415 * issue all erase commands within a specified
416 * timeout. This has been seen to fail, especially
417 * if printf()s are included (for debug)!!
418 */
419 wait_for_DQ7(info, 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 /* reset to read mode */
431 addr = (CFG_FLASH_WORD_SIZE *) info->start[0];
432 addr[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
433
434 printf(" done\n");
435 return 0;
436}
437
438/*-----------------------------------------------------------------------
439 * Copy memory to flash, returns:
440 * 0 - OK
441 * 1 - write timeout
442 * 2 - Flash not erased
443 */
444int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
445{
446 ulong cp, wp, data;
447 int i, l, rc;
448
449 wp = (addr & ~3); /* get lower word aligned address */
450
451 /*
452 * handle unaligned start bytes
453 */
454 if ((l = addr - wp) != 0) {
455 data = 0;
456 for (i = 0, cp = wp; i < l; ++i, ++cp) {
457 data = (data << 8) | (*(uchar *) cp);
458 }
459 for (; i < 4 && cnt > 0; ++i) {
460 data = (data << 8) | *src++;
461 --cnt;
462 ++cp;
463 }
464 for (; cnt == 0 && i < 4; ++i, ++cp) {
465 data = (data << 8) | (*(uchar *) cp);
466 }
467
468 if ((rc = write_word(info, wp, data)) != 0) {
469 return (rc);
470 }
471 wp += 4;
472 }
473
474 /*
475 * handle word aligned part
476 */
477 while (cnt >= 4) {
478 data = 0;
479 for (i = 0; i < 4; ++i) {
480 data = (data << 8) | *src++;
481 }
482 if ((rc = write_word(info, wp, data)) != 0) {
483 return (rc);
484 }
485 wp += 4;
486 cnt -= 4;
487 }
488
489 if (cnt == 0) {
490 return (0);
491 }
492
493 /*
494 * handle unaligned tail bytes
495 */
496 data = 0;
497 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
498 data = (data << 8) | *src++;
499 --cnt;
500 }
501 for (; i < 4; ++i, ++cp) {
502 data = (data << 8) | (*(uchar *) cp);
503 }
504
505 return (write_word(info, wp, data));
506}
507
508/*-----------------------------------------------------------------------
509 * Write a word to Flash, returns:
510 * 0 - OK
511 * 1 - write timeout
512 * 2 - Flash not erased
513 */
514static int write_word(flash_info_t * info, ulong dest, ulong data)
515{
516 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
517 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *) dest;
518 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *) & data;
519 ulong start;
520 int i;
521
522 /* Check if Flash is (sufficiently) erased */
523 if ((*((vu_long *)dest) & data) != data) {
524 return (2);
525 }
526
527 for (i = 0; i < 4 / sizeof(CFG_FLASH_WORD_SIZE); i++) {
528 int flag;
529
530 /* Disable interrupts which might cause a timeout here */
531 flag = disable_interrupts();
532
533 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
534 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
535 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00A000A0;
536
537 dest2[i] = data2[i];
538
539 /* re-enable interrupts if necessary */
540 if (flag)
541 enable_interrupts();
542
543 /* data polling for D7 */
544 start = get_timer(0);
545 while ((dest2[i] & (CFG_FLASH_WORD_SIZE) 0x00800080) !=
546 (data2[i] & (CFG_FLASH_WORD_SIZE) 0x00800080)) {
547
548 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
549 return (1);
550 }
551 }
552 }
553
554 return (0);
555}