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#include <common.h>
29
30
31#define INTEL_ERASE 0x20
32#define INTEL_PROGRAM 0x40
33#define INTEL_CLEAR 0x50
34#define INTEL_LOCKBIT 0x60
35#define INTEL_PROTECT 0x01
36#define INTEL_STATUS 0x70
37#define INTEL_READID 0x90
38#define INTEL_READID 0x90
39#define INTEL_SUSPEND 0xB0
40#define INTEL_CONFIRM 0xD0
41#define INTEL_RESET 0xFF
42
43
44#define INTEL_FINISHED 0x80
45#define INTEL_OK 0x80
46
47typedef unsigned char FLASH_PORT_WIDTH;
48typedef volatile unsigned char FLASH_PORT_WIDTHV;
49#define FPW FLASH_PORT_WIDTH
50#define FPWV FLASH_PORT_WIDTHV
51#define FLASH_ID_MASK 0xFF
52
53#define ORMASK(size) ((-size) & OR_AM_MSK)
54
55#define FLASH_CYCLE1 0x0555
56#define FLASH_CYCLE2 0x02aa
57
58flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
59
60
61
62
63static ulong flash_get_size(FPWV *addr, flash_info_t *info);
64static void flash_reset(flash_info_t *info);
65static flash_info_t *flash_get_info(ulong base);
66static int write_data (flash_info_t *info, FPWV *dest, FPW data);
67static void flash_sync_real_protect (flash_info_t * info);
68static unsigned char intel_sector_protected (flash_info_t *info, ushort sector);
69
70
71
72
73
74
75unsigned long flash_init (void)
76{
77 unsigned long size = 0;
78 int i;
79 extern void flash_preinit(void);
80 extern void flash_afterinit(ulong);
81
82 flash_preinit();
83
84
85 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
86 memset(&flash_info[i], 0, sizeof(flash_info_t));
87 flash_info[i].flash_id = FLASH_UNKNOWN;
88 }
89
90
91 flash_info[0].size =
92 flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
93 size += flash_info[0].size;
94
95
96 flash_sync_real_protect(&flash_info[0]);
97
98#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
99
100 flash_protect(FLAG_PROTECT_SET,
101 CONFIG_SYS_MONITOR_BASE,
102 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
103 flash_get_info(CONFIG_SYS_MONITOR_BASE));
104#endif
105
106#ifdef CONFIG_ENV_IS_IN_FLASH
107
108 flash_protect(FLAG_PROTECT_SET,
109 CONFIG_ENV_ADDR,
110 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
111 flash_get_info(CONFIG_ENV_ADDR));
112#endif
113
114
115 flash_afterinit(size);
116 return (size ? size : 1);
117}
118
119
120
121static void flash_reset(flash_info_t *info)
122{
123 FPWV *base = (FPWV *)(info->start[0]);
124
125
126 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
127 *base = (FPW) INTEL_RESET;
128}
129
130
131
132
133static flash_info_t *flash_get_info(ulong base)
134{
135 int i;
136 flash_info_t * info;
137
138 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i ++) {
139 info = & flash_info[i];
140 if (info->size &&
141 info->start[0] <= base &&
142 base <= info->start[0] + info->size - 1)
143 break;
144 }
145
146 return (i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info);
147}
148
149
150
151
152void flash_print_info (flash_info_t *info)
153{
154 int i;
155 uchar *boottype;
156 uchar *bootletter;
157 char *fmt;
158 uchar botbootletter[] = "B";
159 uchar topbootletter[] = "T";
160 uchar botboottype[] = "bottom boot sector";
161 uchar topboottype[] = "top boot sector";
162
163 if (info->flash_id == FLASH_UNKNOWN) {
164 printf ("missing or unknown FLASH type\n");
165 return;
166 }
167
168 switch (info->flash_id & FLASH_VENDMASK) {
169 case FLASH_MAN_INTEL:
170 printf ("INTEL ");
171 break;
172 default:
173 printf ("Unknown Vendor ");
174 break;
175 }
176
177
178 if (info->flash_id & FLASH_BTYPE) {
179 boottype = botboottype;
180 bootletter = botbootletter;
181 } else {
182 boottype = topboottype;
183 bootletter = topbootletter;
184 }
185
186 switch (info->flash_id & FLASH_TYPEMASK) {
187 case FLASH_28F128J3A:
188 fmt = "28F128J3 (128 Mbit, uniform sectors)\n";
189 break;
190 default:
191 fmt = "Unknown Chip Type\n";
192 break;
193 }
194
195 printf (fmt, bootletter, boottype);
196 printf (" Size: %ld MB in %d Sectors\n",
197 info->size >> 20,
198 info->sector_count);
199
200 printf (" Sector Start Addresses:");
201 for (i=0; i<info->sector_count; ++i) {
202 if ((i % 5) == 0)
203 printf ("\n ");
204
205 printf (" %08lX%s", info->start[i],
206 info->protect[i] ? " (RO)" : " ");
207 }
208 printf ("\n");
209}
210
211
212
213
214
215
216
217ulong flash_get_size (FPWV *addr, flash_info_t *info)
218{
219 int i;
220
221
222
223 addr[FLASH_CYCLE1] = (FPW) INTEL_READID;
224
225
226
227
228 udelay(100);
229 switch (addr[0] & 0xff) {
230 case (uchar)INTEL_MANUFACT:
231 info->flash_id = FLASH_MAN_INTEL;
232 break;
233 default:
234 info->flash_id = FLASH_UNKNOWN;
235 info->sector_count = 0;
236 info->size = 0;
237 break;
238 }
239
240
241
242 if (info->flash_id != FLASH_UNKNOWN) {
243 switch ((FPW)addr[2]) {
244 case (FPW)INTEL_ID_28F128J3:
245 info->flash_id += FLASH_28F128J3A;
246 info->sector_count = 128;
247 info->size = 0x01000000;
248 for( i = 0; i < info->sector_count; i++ )
249 info->start[i] = (ulong)addr + (i * 0x20000);
250 break;
251 default:
252 printf("Flash_id != %xd\n", (FPW)addr[2]);
253 info->flash_id = FLASH_UNKNOWN;
254 info->sector_count = 0;
255 info->size = 0;
256 return (0);
257 }
258 }
259
260
261 flash_reset(info);
262
263 return (info->size);
264}
265
266
267
268
269int flash_erase (flash_info_t *info, int s_first, int s_last)
270{
271 FPWV *addr;
272 int flag, prot, sect;
273 ulong start, now, last;
274 int rcode = 0;
275
276 if ((s_first < 0) || (s_first > s_last)) {
277 if (info->flash_id == FLASH_UNKNOWN) {
278 printf ("- missing\n");
279 } else {
280 printf ("- no sectors to erase\n");
281 }
282 return 1;
283 }
284
285 switch (info->flash_id & FLASH_TYPEMASK) {
286 case FLASH_28F128J3A:
287 break;
288 case FLASH_UNKNOWN:
289 default:
290 printf ("Can't erase unknown flash type %08lx - aborted\n",
291 info->flash_id);
292 return 1;
293 }
294
295 prot = 0;
296 for (sect = s_first; sect <= s_last; ++sect)
297 if (info->protect[sect])
298 prot++;
299
300 if (prot)
301 printf ("- Warning: %d protected sectors will not be erased!",
302 prot);
303
304 printf ("\n");
305 last = get_timer(0);
306
307
308 for (sect = s_first; sect <= s_last && rcode == 0; sect++) {
309
310 if (info->protect[sect] != 0)
311 continue;
312
313
314 flag = disable_interrupts();
315
316 addr = (FPWV *)(info->start[sect]);
317 *addr = (FPW) INTEL_CLEAR;
318 *addr = (FPW) INTEL_ERASE;
319 *addr = (FPW) INTEL_CONFIRM;
320
321
322 if (flag)
323 enable_interrupts();
324
325 start = get_timer(0);
326
327
328 udelay (1000);
329
330 while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
331 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
332 printf ("Timeout\n");
333 *addr = (FPW) INTEL_SUSPEND;
334 flash_reset(info);
335 rcode = 1;
336 break;
337 }
338
339
340 if ((get_timer(last)) > CONFIG_SYS_HZ) {
341 putc ('.');
342 last = get_timer(0);
343 }
344 }
345
346 flash_reset(info);
347 }
348
349 printf (" done\n");
350 return rcode;
351}
352
353
354
355
356
357
358
359int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
360{
361 FPW data = 0;
362 int bytes;
363 int left;
364 int i, res;
365
366 for (left = cnt, res = 0;
367 left > 0 && res == 0;
368 addr += sizeof(data), left -= sizeof(data) - bytes) {
369
370 bytes = addr & (sizeof(data) - 1);
371 addr &= ~(sizeof(data) - 1);
372
373
374
375 for (i = 0; i < sizeof(data); i++) {
376 data <<= 8;
377 if (i < bytes || i - bytes >= left )
378 data += *((uchar *)addr + i);
379 else
380 data += *src++;
381 }
382
383
384 switch (info->flash_id & FLASH_VENDMASK) {
385 case FLASH_MAN_INTEL:
386 res = write_data(info, (FPWV *)addr, data);
387 break;
388 default:
389
390 printf ("missing or unknown FLASH type\n");
391 res = 1;
392 break;
393 }
394 }
395
396 return (res);
397}
398
399
400
401
402
403
404
405static int write_data (flash_info_t *info, FPWV *dest, FPW data)
406{
407 FPWV *addr = dest;
408 ulong status;
409 ulong start;
410 int flag;
411
412
413 if ((*addr & data) != data) {
414 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
415 return (2);
416 }
417
418 flag = disable_interrupts ();
419
420 *addr = (FPW) INTEL_PROGRAM;
421 *addr = data;
422
423
424 start = get_timer(0);
425
426
427 while (((status = *addr) & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
428 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
429 *addr = (FPW) INTEL_RESET;
430 return (1);
431 }
432 }
433
434 *addr = (FPW) INTEL_RESET;
435 if (flag)
436 enable_interrupts();
437
438 return (0);
439}
440
441
442
443
444
445
446
447int flash_real_protect (flash_info_t * info, long sector, int prot)
448{
449 ulong start;
450 int i;
451 int rc = 0;
452 FPWV *addr = (FPWV *) (info->start[sector]);
453 int flag = disable_interrupts ();
454
455 *addr = INTEL_CLEAR;
456 if (prot) {
457 *addr = INTEL_LOCKBIT;
458 *addr = INTEL_PROTECT;
459 } else {
460 *addr = INTEL_LOCKBIT;
461 *addr = INTEL_CONFIRM;
462 }
463
464 start = get_timer (0);
465
466 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
467 if (get_timer (start) > CONFIG_SYS_FLASH_UNLOCK_TOUT) {
468 printf ("Flash lock bit operation timed out\n");
469 rc = 1;
470 break;
471 }
472 }
473
474 if (*addr != INTEL_OK) {
475 printf ("Flash lock bit operation failed at %08X, CSR=%08X\n",
476 (uint) addr, (uint) * addr);
477 rc = 1;
478 }
479
480 if (!rc)
481 info->protect[sector] = prot;
482
483
484
485
486
487 if (!prot) {
488 for (i = 0; i < info->sector_count; i++) {
489 if (info->protect[i]) {
490 start = get_timer (0);
491 addr = (FPWV *) (info->start[i]);
492 *addr = INTEL_LOCKBIT;
493 *addr = INTEL_PROTECT;
494 while ((*addr & INTEL_FINISHED) !=
495 INTEL_FINISHED) {
496 if (get_timer (start) >
497 CONFIG_SYS_FLASH_UNLOCK_TOUT) {
498 printf ("Flash lock bit operation timed out\n");
499 rc = 1;
500 break;
501 }
502 }
503 }
504 }
505 }
506
507 if (flag)
508 enable_interrupts ();
509
510 *addr = INTEL_RESET;
511
512 return rc;
513}
514
515
516
517
518
519
520
521static void flash_sync_real_protect (flash_info_t * info)
522{
523 int i;
524 switch (info->flash_id & FLASH_TYPEMASK) {
525 case FLASH_28F128J3A:
526 for (i = 0; i < info->sector_count; ++i) {
527 info->protect[i] = intel_sector_protected(info, i);
528 }
529 break;
530 default:
531
532 break;
533 }
534}
535
536
537
538
539
540
541
542
543static unsigned char intel_sector_protected (flash_info_t *info, ushort sector)
544{
545 FPWV *addr;
546 FPWV *lock_conf_addr;
547 ulong start;
548 unsigned char ret;
549
550
551
552
553
554
555
556
557
558
559
560 udelay(1);
561 addr = (FPWV *) info->start[sector];
562 *addr = (FPW) INTEL_STATUS;
563
564 start = get_timer (0);
565 while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
566 if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
567 *addr = (FPW) INTEL_RESET;
568 printf("WSM busy too long, can't get prot status\n");
569 return 1;
570 }
571 }
572
573
574 *addr = (FPW) INTEL_READID;
575
576
577 udelay(1);
578
579
580 lock_conf_addr = (FPWV *) info->start[sector] + 4;
581 ret = (*lock_conf_addr & (FPW) INTEL_PROTECT) ? 1 : 0;
582
583
584 *addr = (FPW) INTEL_RESET;
585
586 return ret;
587}
588