1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <asm/processor.h>
26
27flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
28
29
30
31
32static int write_word(flash_info_t *info, ulong dest, ulong data);
33
34void flash_print_info(flash_info_t *info)
35{
36 int i;
37 int k;
38 int size;
39 int erased;
40 volatile unsigned long *flash;
41
42 if (info->flash_id == FLASH_UNKNOWN) {
43 printf ("missing or unknown FLASH type\n");
44 return;
45 }
46
47 switch (info->flash_id & FLASH_VENDMASK) {
48 case FLASH_MAN_AMD: printf ("AMD "); break;
49 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
50 case FLASH_MAN_SST: printf ("SST "); break;
51 case FLASH_MAN_STM: printf ("ST "); break;
52 case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break;
53 default: printf ("Unknown Vendor "); break;
54 }
55
56 switch (info->flash_id & FLASH_TYPEMASK) {
57 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
58 break;
59 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
60 break;
61 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
62 break;
63 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
64 break;
65 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
66 break;
67 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
68 break;
69 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
70 break;
71 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
72 break;
73 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
74 break;
75 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
76 break;
77 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
78 break;
79 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
80 break;
81 case FLASH_SST020: printf ("SST39LF/VF020 (2 Mbit, uniform sector size)\n");
82 break;
83 case FLASH_SST040: printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
84 break;
85 default: printf ("Unknown Chip Type\n");
86 break;
87 }
88
89 printf (" Size: %ld MB in %d Sectors\n",
90 info->size >> 20, info->sector_count);
91
92 printf (" Sector Start Addresses:");
93 for (i=0; i<info->sector_count; ++i) {
94#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
95
96
97
98 if (i != (info->sector_count-1))
99 size = info->start[i+1] - info->start[i];
100 else
101 size = info->start[0] + info->size - info->start[i];
102 erased = 1;
103 flash = (volatile unsigned long *)info->start[i];
104 size = size >> 2;
105 for (k=0; k<size; k++) {
106 if (*flash++ != 0xffffffff) {
107 erased = 0;
108 break;
109 }
110 }
111
112 if ((i % 5) == 0)
113 printf ("\n ");
114
115 printf (" %08lX%s%s",
116 info->start[i],
117 erased ? " E" : " ",
118 info->protect[i] ? "RO " : " ");
119#else
120 if ((i % 5) == 0)
121 printf ("\n ");
122 printf (" %08lX%s",
123 info->start[i],
124 info->protect[i] ? " (RO)" : " ");
125#endif
126
127 }
128 printf ("\n");
129 return;
130}
131
132
133
134
135static ulong flash_get_size(vu_long *addr, flash_info_t *info)
136{
137 short i;
138 short n;
139 CONFIG_SYS_FLASH_WORD_SIZE value;
140 ulong base = (ulong)addr;
141 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
142
143
144 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
145 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
146 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
147
148 value = addr2[CONFIG_SYS_FLASH_READ0];
149
150 switch (value) {
151 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
152 info->flash_id = FLASH_MAN_AMD;
153 break;
154 case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
155 info->flash_id = FLASH_MAN_FUJ;
156 break;
157 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
158 info->flash_id = FLASH_MAN_SST;
159 break;
160 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_MANUFACT:
161 info->flash_id = FLASH_MAN_STM;
162 break;
163 case (CONFIG_SYS_FLASH_WORD_SIZE)EXCEL_MANUFACT:
164 info->flash_id = FLASH_MAN_EXCEL;
165 break;
166 default:
167 info->flash_id = FLASH_UNKNOWN;
168 info->sector_count = 0;
169 info->size = 0;
170 return (0);
171 }
172
173 value = addr2[CONFIG_SYS_FLASH_READ1];
174
175 switch (value) {
176 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
177 info->flash_id += FLASH_AM400T;
178 info->sector_count = 11;
179 info->size = 0x00080000;
180 break;
181
182 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
183 info->flash_id += FLASH_AM400B;
184 info->sector_count = 11;
185 info->size = 0x00080000;
186 break;
187
188 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
189 info->flash_id += FLASH_AM800T;
190 info->sector_count = 19;
191 info->size = 0x00100000;
192 break;
193
194 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
195 info->flash_id += FLASH_AM800B;
196 info->sector_count = 19;
197 info->size = 0x00100000;
198 break;
199
200 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
201 info->flash_id += FLASH_AM160T;
202 info->sector_count = 35;
203 info->size = 0x00200000;
204 break;
205
206 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
207 info->flash_id += FLASH_AM160B;
208 info->sector_count = 35;
209 info->size = 0x00200000;
210 break;
211
212 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
213 info->flash_id += FLASH_AM320T;
214 info->sector_count = 71;
215 info->size = 0x00400000; break;
216
217 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
218 info->flash_id += FLASH_AM320B;
219 info->sector_count = 71;
220 info->size = 0x00400000; break;
221
222 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
223 info->flash_id += FLASH_AMDL322T;
224 info->sector_count = 71;
225 info->size = 0x00400000; break;
226
227 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
228 info->flash_id += FLASH_AMDL322B;
229 info->sector_count = 71;
230 info->size = 0x00400000; break;
231
232 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
233 info->flash_id += FLASH_AMDL323T;
234 info->sector_count = 71;
235 info->size = 0x00400000; break;
236
237 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
238 info->flash_id += FLASH_AMDL323B;
239 info->sector_count = 71;
240 info->size = 0x00400000; break;
241
242 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF020:
243 info->flash_id += FLASH_SST020;
244 info->sector_count = 64;
245 info->size = 0x00040000;
246 break;
247
248 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF040:
249 info->flash_id += FLASH_SST040;
250 info->sector_count = 128;
251 info->size = 0x00080000;
252 break;
253
254 default:
255 info->flash_id = FLASH_UNKNOWN;
256 return (0);
257
258 }
259
260
261 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
262 for (i = 0; i < info->sector_count; i++)
263 info->start[i] = base + (i * 0x00001000);
264 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
265 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
266 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
267 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
268
269 for (i=0; i<8; ++i) {
270 info->start[i] = base;
271 base += 8 << 10;
272 }
273 while (i < info->sector_count) {
274 info->start[i] = base;
275 base += 64 << 10;
276 ++i;
277 }
278 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
279 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
280 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
281 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
282
283 base += info->size;
284 i = info->sector_count;
285 for (n=0; n<8; ++n) {
286 base -= 8 << 10;
287 --i;
288 info->start[i] = base;
289 }
290 while (i > 0) {
291 base -= 64 << 10;
292 --i;
293 info->start[i] = base;
294 }
295 } else {
296 if (info->flash_id & FLASH_BTYPE) {
297
298 info->start[0] = base + 0x00000000;
299 info->start[1] = base + 0x00004000;
300 info->start[2] = base + 0x00006000;
301 info->start[3] = base + 0x00008000;
302 for (i = 4; i < info->sector_count; i++) {
303 info->start[i] = base + (i * 0x00010000) - 0x00030000;
304 }
305 } else {
306
307 i = info->sector_count - 1;
308 info->start[i--] = base + info->size - 0x00004000;
309 info->start[i--] = base + info->size - 0x00006000;
310 info->start[i--] = base + info->size - 0x00008000;
311 for (; i >= 0; i--) {
312 info->start[i] = base + i * 0x00010000;
313 }
314 }
315 }
316
317
318 for (i = 0; i < info->sector_count; i++) {
319
320
321 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
322 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
323 info->protect[i] = 0;
324 else
325 info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
326 }
327
328
329
330
331 if (info->flash_id != FLASH_UNKNOWN) {
332 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
333 *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0;
334 }
335
336 return (info->size);
337}
338
339
340int flash_erase(flash_info_t *info, int s_first, int s_last)
341{
342 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
343 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
344 int flag, prot, sect, l_sect;
345 ulong start, now, last;
346
347 if ((s_first < 0) || (s_first > s_last)) {
348 if (info->flash_id == FLASH_UNKNOWN)
349 printf ("- missing\n");
350 else
351 printf ("- no sectors to erase\n");
352 return 1;
353 }
354
355 if (info->flash_id == FLASH_UNKNOWN) {
356 printf ("Can't erase unknown flash type - aborted\n");
357 return 1;
358 }
359
360 prot = 0;
361 for (sect=s_first; sect<=s_last; ++sect)
362 if (info->protect[sect])
363 prot++;
364
365 if (prot)
366 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
367 else
368 printf ("\n");
369
370 l_sect = -1;
371
372
373 flag = disable_interrupts();
374
375
376 for (sect = s_first; sect<=s_last; sect++) {
377 if (info->protect[sect] == 0) {
378 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
379 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
380 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
381 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
382 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
383 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
384 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
385 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030;
386
387
388 if (flag) {
389 enable_interrupts();
390 flag = 0;
391 }
392
393
394 start = get_timer (0);
395 while ((addr2[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
396 (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
397 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
398 return (1);
399 }
400 } else {
401 if (sect == s_first) {
402 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
403 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
404 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
405 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
406 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
407 }
408 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030;
409 }
410 l_sect = sect;
411 }
412 }
413
414
415 if (flag)
416 enable_interrupts();
417
418
419 udelay (1000);
420
421
422
423
424 if (l_sect < 0)
425 goto DONE;
426
427 start = get_timer (0);
428 last = start;
429 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
430 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
431 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
432 printf ("Timeout\n");
433 return 1;
434 }
435
436 if ((now - last) > 1000) {
437 putc ('.');
438 last = now;
439 }
440 }
441
442DONE:
443
444 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
445 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0;
446
447 printf (" done\n");
448 return 0;
449}
450
451
452
453
454
455
456
457int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
458{
459 ulong cp, wp, data;
460 int i, l, rc;
461
462 wp = (addr & ~3);
463
464
465
466
467 if ((l = addr - wp) != 0) {
468 data = 0;
469 for (i=0, cp=wp; i<l; ++i, ++cp) {
470 data = (data << 8) | (*(uchar *)cp);
471 }
472 for (; i<4 && cnt>0; ++i) {
473 data = (data << 8) | *src++;
474 --cnt;
475 ++cp;
476 }
477 for (; cnt==0 && i<4; ++i, ++cp) {
478 data = (data << 8) | (*(uchar *)cp);
479 }
480
481 if ((rc = write_word(info, wp, data)) != 0) {
482 return (rc);
483 }
484 wp += 4;
485 }
486
487
488
489
490 while (cnt >= 4) {
491 data = 0;
492 for (i=0; i<4; ++i)
493 data = (data << 8) | *src++;
494 if ((rc = write_word(info, wp, data)) != 0)
495 return (rc);
496 wp += 4;
497 cnt -= 4;
498 }
499
500 if (cnt == 0)
501 return (0);
502
503
504
505
506 data = 0;
507 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
508 data = (data << 8) | *src++;
509 --cnt;
510 }
511 for (; i<4; ++i, ++cp)
512 data = (data << 8) | (*(uchar *)cp);
513
514 return (write_word(info, wp, data));
515}
516
517
518
519
520
521
522
523static int write_word(flash_info_t *info, ulong dest, ulong data)
524{
525 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
526 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
527 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)&data;
528 ulong start;
529 int flag;
530 int i;
531
532
533 if ((*((vu_long *)dest) & data) != data)
534 return (2);
535
536
537 flag = disable_interrupts();
538
539 for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
540 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
541 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
542 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
543
544 dest2[i] = data2[i];
545
546
547 if (flag)
548 enable_interrupts();
549
550
551 start = get_timer (0);
552 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
553 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
554 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
555 return (1);
556 }
557 }
558
559 return (0);
560}
561