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