1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <common.h>
34#include <asm/processor.h>
35#include <asm/pci_io.h>
36
37flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
38
39ulong flash_get_size (vu_long *addr, flash_info_t *info);
40static int write_word (flash_info_t *info, ulong dest, ulong data);
41
42
43
44#define ADDR0 (0x555)
45#define ADDR1 (0x2AA)
46#define ADDR3 (0x001)
47
48#define FLASH_WORD_SIZE unsigned char
49
50
51
52unsigned long flash_init (void)
53{
54 unsigned long size1, size2;
55 int i;
56
57
58 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i)
59 {
60 flash_info[i].flash_id = FLASH_UNKNOWN;
61 }
62
63
64 size1 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
65
66 if (flash_info[0].flash_id == FLASH_UNKNOWN)
67 {
68 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
69 size1, size1<<20);
70 }
71
72
73 size2 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
74
75 if (flash_info[1].flash_id == FLASH_UNKNOWN)
76 {
77 printf ("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
78 size2, size2<<20);
79 }
80
81
82 if (size1 == 512*1024)
83 {
84 (void)flash_protect(FLAG_PROTECT_SET,
85 FLASH_BASE0_PRELIM,
86 FLASH_BASE0_PRELIM+monitor_flash_len-1,
87 &flash_info[0]);
88 }
89 if (size2 == 512*1024)
90 {
91 (void)flash_protect(FLAG_PROTECT_SET,
92 FLASH_BASE1_PRELIM,
93 FLASH_BASE1_PRELIM+monitor_flash_len-1,
94 &flash_info[1]);
95 }
96 if (size2 == 4*1024*1024)
97 {
98 (void)flash_protect(FLAG_PROTECT_SET,
99 CONFIG_SYS_FLASH_BASE,
100 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
101 &flash_info[1]);
102 }
103
104 return (size1 + size2);
105}
106
107
108
109void flash_print_info (flash_info_t *info)
110{
111 int i;
112 int k;
113 int size;
114 int erased;
115 volatile unsigned long *flash;
116
117 if (info->flash_id == FLASH_UNKNOWN) {
118 printf ("missing or unknown FLASH type\n");
119 flash_init();
120 }
121
122 if (info->flash_id == FLASH_UNKNOWN) {
123 printf ("missing or unknown FLASH type\n");
124 return;
125 }
126
127 switch (info->flash_id & FLASH_VENDMASK) {
128 case FLASH_MAN_AMD:
129 printf ("AMD ");
130 break;
131 default:
132 printf ("Unknown Vendor ");
133 break;
134 }
135
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case AMD_ID_F040B:
138 printf ("AM29F040B (4 Mbit)\n");
139 break;
140 case AMD_ID_F016D:
141 printf ("AM29F016D (16 Mbit)\n");
142 break;
143 case AMD_ID_F032B:
144 printf ("AM29F032B (32 Mbit)\n");
145 break;
146 default:
147 printf ("Unknown Chip Type\n");
148 break;
149 }
150
151 if (info->size >= (1 << 20)) {
152 printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
153 } else {
154 printf (" Size: %ld kB in %d Sectors\n", info->size >> 10, info->sector_count);
155 }
156
157 printf (" Sector Start Addresses:");
158 for (i=0; i<info->sector_count; ++i) {
159
160
161
162 if (i != (info->sector_count-1))
163 size = info->start[i+1] - info->start[i];
164 else
165 size = info->start[0] + info->size - info->start[i];
166
167 erased = 1;
168 flash = (volatile unsigned long *)info->start[i];
169 size = size >> 2;
170 for (k=0; k<size; k++) {
171 if (*flash++ != 0xffffffff) {
172 erased = 0;
173 break;
174 }
175 }
176
177 if ((i % 5) == 0)
178 printf ("\n ");
179
180 printf (" %08lX%s%s",
181 info->start[i],
182 erased ? " E" : " ",
183 info->protect[i] ? "RO " : " ");
184 }
185 printf ("\n");
186}
187
188
189
190
191
192ulong flash_get_size (vu_long *addr, flash_info_t *info)
193{
194 short i;
195 ulong vendor, devid;
196 ulong base = (ulong)addr;
197 volatile unsigned char *caddr = (unsigned char *)addr;
198
199#ifdef DEBUG
200 printf("flash_get_size for address 0x%lx: \n", (unsigned long)caddr);
201#endif
202
203
204 caddr[0] = 0xF0;
205 udelay(10);
206
207 eieio();
208 caddr[0x555] = 0xAA;
209 udelay(10);
210 caddr[0x2AA] = 0x55;
211 udelay(10);
212 caddr[0x555] = 0x90;
213
214 udelay(10);
215
216 vendor = caddr[0];
217 devid = caddr[1];
218
219#ifdef DEBUG
220 printf("Manufacturer: 0x%lx\n", vendor);
221#endif
222
223 vendor &= 0xff;
224 devid &= 0xff;
225
226
227 switch (vendor) {
228 case (FLASH_WORD_SIZE)AMD_MANUFACT:
229 info->flash_id = FLASH_MAN_AMD;
230 break;
231 default:
232 info->flash_id = FLASH_UNKNOWN;
233 info->sector_count = 0;
234 info->size = 0;
235 return (0);
236 }
237
238 switch (devid) {
239 case (FLASH_WORD_SIZE)AMD_ID_F040B:
240 info->flash_id |= AMD_ID_F040B;
241 info->sector_count = 8;
242 info->size = 0x00080000;
243 break;
244
245 case (FLASH_WORD_SIZE)AMD_ID_F016D:
246 info->flash_id |= AMD_ID_F016D;
247 info->sector_count = 32;
248 info->size = 0x00200000;
249 break;
250
251 case (FLASH_WORD_SIZE)AMD_ID_F032B:
252 info->flash_id |= AMD_ID_F032B;
253 info->sector_count = 64;
254 info->size = 0x00400000;
255 break;
256
257 default:
258 info->flash_id = FLASH_UNKNOWN;
259 return (0);
260
261 }
262
263#ifdef DEBUG
264 printf("flash id 0x%lx; sector count 0x%x, size 0x%lx\n", info->flash_id, info->sector_count, info->size);
265#endif
266
267
268 for (i = 0; i < info->sector_count; i++) {
269
270 info->start[i] = base + i * (info->size / info->sector_count);
271
272
273 caddr = (volatile unsigned char *)(info->start[i]);
274 info->protect[i] = caddr[2] & 1;
275 }
276
277
278
279
280 if (info->flash_id != FLASH_UNKNOWN) {
281 caddr = (volatile unsigned char *)info->start[0];
282 caddr[0] = 0xF0;
283 }
284
285 return (info->size);
286}
287
288
289
290int flash_erase (flash_info_t *info, int s_first, int s_last)
291{
292 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
293 int flag, prot, sect, l_sect;
294 ulong start, now, last;
295 int rc = 0;
296
297 if ((s_first < 0) || (s_first > s_last)) {
298 if (info->flash_id == FLASH_UNKNOWN) {
299 printf ("- missing\n");
300 } else {
301 printf ("- no sectors to erase\n");
302 }
303 return 1;
304 }
305
306 if ((info->flash_id == FLASH_UNKNOWN) ||
307 (info->flash_id > FLASH_AMD_COMP)) {
308 printf ("Can't erase unknown flash type - aborted\n");
309 return 1;
310 }
311
312 prot = 0;
313 for (sect=s_first; sect<=s_last; ++sect) {
314 if (info->protect[sect]) {
315 prot++;
316 }
317 }
318
319 if (prot) {
320 printf ("- Warning: %d protected sectors will not be erased!\n",
321 prot);
322 } else {
323 printf ("\n");
324 }
325
326 l_sect = -1;
327
328
329 flag = disable_interrupts();
330
331 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
332 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
333 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
334 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
335 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
336
337
338 for (sect = s_first; sect<=s_last; sect++) {
339 if (info->protect[sect] == 0) {
340 addr = (FLASH_WORD_SIZE *)(info->start[sect]);
341 if (info->flash_id & FLASH_MAN_SST) {
342 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
343 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
344 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
345 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
346 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
347 addr[0] = (FLASH_WORD_SIZE)0x00500050;
348 udelay(30000);
349 }
350 else
351 addr[0] = (FLASH_WORD_SIZE)0x00300030;
352 l_sect = sect;
353 }
354 }
355
356
357 if (flag)
358 enable_interrupts();
359
360
361 udelay (1000);
362
363
364
365
366 if (l_sect < 0)
367 goto DONE;
368
369 start = get_timer (0);
370 last = start;
371 addr = (FLASH_WORD_SIZE *)(info->start[l_sect]);
372 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
373 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
374 printf ("Timeout\n");
375 return 1;
376 }
377
378 if ((now - last) > 1000) {
379 serial_putc ('.');
380 last = now;
381 }
382 }
383
384DONE:
385
386 addr = (FLASH_WORD_SIZE *)info->start[0];
387 addr[0] = (FLASH_WORD_SIZE)0x00F000F0;
388
389 printf (" done\n");
390 return rc;
391}
392
393
394
395
396
397
398
399
400int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
401{
402 ulong cp, wp, data;
403 int i, l, rc;
404
405 wp = (addr & ~3);
406
407
408
409
410 if ((l = addr - wp) != 0) {
411 data = 0;
412 for (i=0, cp=wp; i<l; ++i, ++cp) {
413 data = (data << 8) | (*(uchar *)cp);
414 }
415 for (; i<4 && cnt>0; ++i) {
416 data = (data << 8) | *src++;
417 --cnt;
418 ++cp;
419 }
420 for (; cnt==0 && i<4; ++i, ++cp) {
421 data = (data << 8) | (*(uchar *)cp);
422 }
423
424 if ((rc = write_word(info, wp, data)) != 0) {
425 return (rc);
426 }
427 wp += 4;
428 }
429
430
431
432
433 while (cnt >= 4) {
434 data = 0;
435 for (i=0; i<4; ++i) {
436 data = (data << 8) | *src++;
437 }
438 if ((rc = write_word(info, wp, data)) != 0) {
439 return (rc);
440 }
441 wp += 4;
442 cnt -= 4;
443 }
444
445 if (cnt == 0) {
446 return (0);
447 }
448
449
450
451
452 data = 0;
453 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
454 data = (data << 8) | *src++;
455 --cnt;
456 }
457 for (; i<4; ++i, ++cp) {
458 data = (data << 8) | (*(uchar *)cp);
459 }
460
461 return (write_word(info, wp, data));
462}
463
464
465
466
467
468
469
470static int write_word (flash_info_t *info, ulong dest, ulong data)
471{
472 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
473 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
474 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
475 ulong start;
476 int flag;
477 int i;
478
479
480 if ((*((volatile FLASH_WORD_SIZE *)dest) &
481 (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
482 return (2);
483 }
484
485 flag = disable_interrupts();
486
487 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
488 {
489 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
490 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
491 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
492
493 dest2[i] = data2[i];
494
495
496 if (flag)
497 enable_interrupts();
498
499
500 start = get_timer (0);
501 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
502 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
503 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
504 return (1);
505 }
506 }
507 }
508
509 return (0);
510}
511
512
513