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