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 <mpc8xx.h>
26
27flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
28
29
30
31
32static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33static int write_word (flash_info_t *info, ulong dest, ulong data);
34static void flash_get_offsets (ulong base, flash_info_t *info);
35
36
37
38
39unsigned long flash_init (void)
40{
41 unsigned long size_b0, size_b1;
42 int i;
43
44
45 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
46 flash_info[i].flash_id = FLASH_UNKNOWN;
47
48
49 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
50
51
52 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
53
54#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
55
56 flash_protect(FLAG_PROTECT_SET,
57 CONFIG_SYS_MONITOR_BASE,
58 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
59 &flash_info[0]);
60#endif
61
62 size_b1 = 0 ;
63
64 flash_info[1].flash_id = FLASH_UNKNOWN;
65 flash_info[1].sector_count = -1;
66
67 flash_info[0].size = size_b0;
68 flash_info[1].size = size_b1;
69
70 return (size_b0 + size_b1);
71}
72
73
74
75
76static void flash_get_offsets (ulong base, flash_info_t *info)
77{
78 int i;
79
80
81 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
82
83 for (i = 0; i < info->sector_count; i++)
84 info->start[i] = base + (i * 0x00010000);
85 }
86}
87
88
89
90void flash_print_info (flash_info_t *info)
91{
92 int i;
93
94 if (info->flash_id == FLASH_UNKNOWN)
95 {
96 puts ("missing or unknown FLASH type\n");
97 return;
98 }
99
100 switch (info->flash_id & FLASH_VENDMASK)
101 {
102 case FLASH_MAN_AMD: printf ("AMD "); break;
103 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
104 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
105 default: printf ("Unknown Vendor "); break;
106 }
107
108 switch (info->flash_id & FLASH_TYPEMASK)
109 {
110 case FLASH_AM040: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
111 break;
112 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
113 break;
114 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
115 break;
116 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
117 break;
118 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
119 break;
120 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
121 break;
122 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
123 break;
124 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
125 break;
126 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
127 break;
128 default: printf ("Unknown Chip Type\n");
129 break;
130 }
131
132 if (info->size >> 20) {
133 printf (" Size: %ld MB in %d Sectors\n",
134 info->size >> 20,
135 info->sector_count);
136 } else {
137 printf (" Size: %ld KB in %d Sectors\n",
138 info->size >> 10,
139 info->sector_count);
140 }
141
142 puts (" Sector Start Addresses:");
143
144 for (i=0; i<info->sector_count; ++i)
145 {
146 if ((i % 5) == 0)
147 {
148 puts ("\n ");
149 }
150
151 printf (" %08lX%s",
152 info->start[i],
153 info->protect[i] ? " (RO)" : " ");
154 }
155
156 putc ('\n');
157 return;
158}
159
160
161
162
163
164
165
166static ulong flash_get_size (vu_long *addr, flash_info_t *info)
167{
168 short i;
169 volatile unsigned char *caddr;
170 char value;
171
172 caddr = (volatile unsigned char *)addr ;
173
174
175
176#if 0
177 printf("Base address is: %08x\n", caddr);
178#endif
179
180 caddr[0x0555] = 0xAA;
181 caddr[0x02AA] = 0x55;
182 caddr[0x0555] = 0x90;
183
184 value = caddr[0];
185
186#if 0
187 printf("Manufact ID: %02x\n", value);
188#endif
189 switch (value)
190 {
191 case 0x1:
192 info->flash_id = FLASH_MAN_AMD;
193 break;
194
195 case 0x4:
196 info->flash_id = FLASH_MAN_FUJ;
197 break;
198
199 default:
200 info->flash_id = FLASH_UNKNOWN;
201 info->sector_count = 0;
202 info->size = 0;
203 break;
204 }
205
206 value = caddr[1];
207#if 0
208 printf("Device ID: %02x\n", value);
209#endif
210 switch (value)
211 {
212 case AMD_ID_LV040B:
213 info->flash_id += FLASH_AM040;
214 info->sector_count = 8;
215 info->size = 0x00080000;
216 break;
217
218 default:
219 info->flash_id = FLASH_UNKNOWN;
220 return (0);
221
222 }
223
224 flash_get_offsets ((ulong)addr, &flash_info[0]);
225
226
227 for (i = 0; i < info->sector_count; i++)
228 {
229
230
231 caddr = (volatile unsigned char *)(info->start[i]);
232 info->protect[i] = caddr[2] & 1;
233 }
234
235
236
237
238 if (info->flash_id != FLASH_UNKNOWN)
239 {
240 caddr = (volatile unsigned char *)info->start[0];
241 *caddr = 0xF0;
242 }
243
244 return (info->size);
245}
246
247
248
249
250
251int flash_erase (flash_info_t *info, int s_first, int s_last)
252{
253 volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
254 int flag, prot, sect, l_sect;
255 ulong start, now, last;
256
257 if ((s_first < 0) || (s_first > s_last)) {
258 if (info->flash_id == FLASH_UNKNOWN) {
259 printf ("- missing\n");
260 } else {
261 printf ("- no sectors to erase\n");
262 }
263 return 1;
264 }
265
266 if ((info->flash_id == FLASH_UNKNOWN) ||
267 (info->flash_id > FLASH_AMD_COMP)) {
268 printf ("Can't erase unknown flash type - aborted\n");
269 return 1;
270 }
271
272 prot = 0;
273 for (sect=s_first; sect<=s_last; ++sect) {
274 if (info->protect[sect]) {
275 prot++;
276 }
277 }
278
279 if (prot) {
280 printf ("- Warning: %d protected sectors will not be erased!\n",
281 prot);
282 } else {
283 printf ("\n");
284 }
285
286 l_sect = -1;
287
288
289 flag = disable_interrupts();
290
291 addr[0x0555] = 0xAA;
292 addr[0x02AA] = 0x55;
293 addr[0x0555] = 0x80;
294 addr[0x0555] = 0xAA;
295 addr[0x02AA] = 0x55;
296
297
298 for (sect = s_first; sect<=s_last; sect++) {
299 if (info->protect[sect] == 0) {
300 addr = (volatile unsigned char *)(info->start[sect]);
301 addr[0] = 0x30;
302 l_sect = sect;
303 }
304 }
305
306
307 if (flag)
308 enable_interrupts();
309
310
311 udelay (1000);
312
313
314
315
316 if (l_sect < 0)
317 goto DONE;
318
319 start = get_timer (0);
320 last = start;
321 addr = (volatile unsigned char *)(info->start[l_sect]);
322
323 while ((addr[0] & 0xFF) != 0xFF)
324 {
325 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
326 printf ("Timeout\n");
327 return 1;
328 }
329
330 if ((now - last) > 1000) {
331 putc ('.');
332 last = now;
333 }
334 }
335
336DONE:
337
338 addr = (volatile unsigned char *)info->start[0];
339
340 addr[0] = 0xF0;
341
342 printf (" done\n");
343 return 0;
344}
345
346
347
348
349
350
351
352
353int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
354{
355 ulong cp, wp, data;
356 int i, l, rc;
357
358 wp = (addr & ~3);
359
360
361
362
363 if ((l = addr - wp) != 0) {
364 data = 0;
365 for (i=0, cp=wp; i<l; ++i, ++cp) {
366 data = (data << 8) | (*(uchar *)cp);
367 }
368 for (; i<4 && cnt>0; ++i) {
369 data = (data << 8) | *src++;
370 --cnt;
371 ++cp;
372 }
373 for (; cnt==0 && i<4; ++i, ++cp) {
374 data = (data << 8) | (*(uchar *)cp);
375 }
376
377 if ((rc = write_word(info, wp, data)) != 0) {
378 return (rc);
379 }
380 wp += 4;
381 }
382
383
384
385
386 while (cnt >= 4) {
387 data = 0;
388 for (i=0; i<4; ++i) {
389 data = (data << 8) | *src++;
390 }
391 if ((rc = write_word(info, wp, data)) != 0) {
392 return (rc);
393 }
394 wp += 4;
395 cnt -= 4;
396 }
397
398 if (cnt == 0) {
399 return (0);
400 }
401
402
403
404
405 data = 0;
406 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
407 data = (data << 8) | *src++;
408 --cnt;
409 }
410 for (; i<4; ++i, ++cp) {
411 data = (data << 8) | (*(uchar *)cp);
412 }
413
414 return (write_word(info, wp, data));
415}
416
417
418
419
420
421
422
423static int write_word (flash_info_t *info, ulong dest, ulong data)
424{
425 volatile unsigned char *addr = (volatile unsigned char*)(info->start[0]),
426 *cdest,*cdata;
427 ulong start;
428 int flag, count = 4 ;
429
430 cdest = (volatile unsigned char *)dest ;
431 cdata = (volatile unsigned char *)&data ;
432
433
434 if ((*((vu_long *)dest) & data) != data) {
435 return (2);
436 }
437
438 while(count--)
439 {
440
441 flag = disable_interrupts();
442
443 addr[0x0555] = 0xAA;
444 addr[0x02AA] = 0x55;
445 addr[0x0555] = 0xA0;
446
447 *cdest = *cdata;
448
449
450 if (flag)
451 enable_interrupts();
452
453
454 start = get_timer (0);
455 while ((*cdest ^ *cdata) & 0x80)
456 {
457 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
458 return (1);
459 }
460 }
461
462 cdata++ ;
463 cdest++ ;
464 }
465 return (0);
466}
467
468
469
470