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 <mpc824x.h>
26
27#if defined(CONFIG_ENV_IS_IN_FLASH)
28# ifndef CONFIG_ENV_ADDR
29# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
30# endif
31# ifndef CONFIG_ENV_SIZE
32# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
33# endif
34# ifndef CONFIG_ENV_SECT_SIZE
35# define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
36# endif
37#endif
38
39
40#undef DEBUG_FLASH
41
42#ifdef DEBUG_FLASH
43#define DEBUGF(fmt,args...) printf(fmt ,##args)
44#else
45#define DEBUGF(fmt,args...)
46#endif
47
48
49flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
50
51
52
53
54static ulong flash_get_size (vu_char *addr, flash_info_t *info);
55static int write_data (flash_info_t *info, uchar *dest, uchar data);
56static void flash_get_offsets (ulong base, flash_info_t *info);
57
58
59
60
61
62
63
64#define MUSENKI_BROKEN_FLASH 1
65
66#ifdef MUSENKI_BROKEN_FLASH
67unsigned char swaptab[256] = {
68 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
69 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
70 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
71 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
72 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
73 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
74 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
75 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
76 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62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, 0xef,
99 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
100};
101
102#define BS(b) (swaptab[b])
103
104#else
105
106#define BS(b) (b)
107
108#endif
109
110#define BYTEME(x) ((x) & 0xFF)
111
112
113
114
115unsigned long flash_init (void)
116{
117 unsigned long size_b0, size_b1;
118 int i;
119
120
121 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
122 flash_info[i].flash_id = FLASH_UNKNOWN;
123 }
124
125
126
127 DEBUGF("\n## Get flash bank 1 size @ 0x%08x\n",CONFIG_SYS_FLASH_BASE0_PRELIM);
128
129 size_b0 = flash_get_size((vu_char *)CONFIG_SYS_FLASH_BASE0_PRELIM, &flash_info[0]);
130
131 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
132 printf ("## Unknown FLASH on Bank 0: "
133 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
134 flash_info[0].flash_id,
135 size_b0, size_b0<<20);
136 }
137
138 DEBUGF("## Get flash bank 2 size @ 0x%08x\n",CONFIG_SYS_FLASH_BASE1_PRELIM);
139 size_b1 = flash_get_size((vu_char *)CONFIG_SYS_FLASH_BASE1_PRELIM, &flash_info[1]);
140
141 DEBUGF("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
142
143 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
144
145 flash_info[0].size = size_b0;
146
147#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
148 DEBUGF("protect monitor %x @ %x\n", CONFIG_SYS_MONITOR_BASE, monitor_flash_len);
149
150 flash_protect(FLAG_PROTECT_SET,
151 CONFIG_SYS_MONITOR_BASE,
152 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
153 &flash_info[0]);
154#endif
155
156#ifdef CONFIG_ENV_IS_IN_FLASH
157
158 DEBUGF("protect environtment %x @ %x\n", CONFIG_ENV_ADDR, CONFIG_ENV_SECT_SIZE);
159 flash_protect(FLAG_PROTECT_SET,
160 CONFIG_ENV_ADDR,
161 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
162 &flash_info[0]);
163#endif
164
165 if (size_b1) {
166 flash_info[1].size = size_b1;
167 flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
168
169#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
170
171 flash_protect(FLAG_PROTECT_SET,
172 CONFIG_SYS_MONITOR_BASE,
173 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
174 &flash_info[1]);
175#endif
176
177#ifdef CONFIG_ENV_IS_IN_FLASH
178
179 flash_protect(FLAG_PROTECT_SET,
180 CONFIG_ENV_ADDR,
181 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
182 &flash_info[1]);
183#endif
184 } else {
185 flash_info[1].flash_id = FLASH_UNKNOWN;
186 flash_info[1].sector_count = -1;
187 flash_info[1].size = 0;
188 }
189
190 DEBUGF("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
191
192 return (size_b0 + size_b1);
193}
194
195
196
197static void flash_get_offsets (ulong base, flash_info_t *info)
198{
199 int i;
200
201 if (info->flash_id == FLASH_UNKNOWN) {
202 return;
203 }
204
205 switch (info->flash_id & FLASH_VENDMASK) {
206 case FLASH_MAN_INTEL:
207 for (i = 0; i < info->sector_count; i++) {
208 info->start[i] = base;
209 base += 0x00020000;
210 }
211 return;
212
213 default:
214 printf ("Don't know sector ofsets for flash type 0x%lx\n", info->flash_id);
215 return;
216 }
217}
218
219
220
221void flash_print_info (flash_info_t *info)
222{
223 int i;
224
225 if (info->flash_id == FLASH_UNKNOWN) {
226 printf ("missing or unknown FLASH type\n");
227 return;
228 }
229
230 switch (info->flash_id & FLASH_VENDMASK) {
231 case FLASH_MAN_AMD: printf ("AMD "); break;
232 case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
233 case FLASH_MAN_SST: printf ("SST "); break;
234 case FLASH_MAN_STM: printf ("STM "); break;
235 case FLASH_MAN_INTEL: printf ("Intel "); break;
236 case FLASH_MAN_MT: printf ("MT "); break;
237 default: printf ("Unknown Vendor "); break;
238 }
239
240 switch (info->flash_id & FLASH_TYPEMASK) {
241 case FLASH_28F320J3A: printf ("28F320J3A (32Mbit = 128K x 32)\n");
242 break;
243 case FLASH_28F640J3A: printf ("28F640J3A (64Mbit = 128K x 64)\n");
244 break;
245 case FLASH_28F128J3A: printf ("28F128J3A (128Mbit = 128K x 128)\n");
246 break;
247 default: printf ("Unknown Chip Type\n");
248 break;
249 }
250
251 if (info->size >= (1 << 20)) {
252 i = 20;
253 } else {
254 i = 10;
255 }
256 printf (" Size: %ld %cB in %d Sectors\n",
257 info->size >> i,
258 (i == 20) ? 'M' : 'k',
259 info->sector_count);
260
261 printf (" Sector Start Addresses:");
262 for (i=0; i<info->sector_count; ++i) {
263 if ((i % 5) == 0)
264 printf ("\n ");
265 printf (" %08lX%s",
266 info->start[i],
267 info->protect[i] ? " (RO)" : " "
268 );
269 }
270 printf ("\n");
271 return;
272}
273
274
275
276
277
278
279
280
281
282
283
284static ulong flash_get_size (vu_char *addr, flash_info_t *info)
285{
286 vu_char manuf, device;
287
288 addr[0] = BS(0x90);
289 manuf = BS(addr[0]);
290 DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (vu_char *)addr, manuf);
291
292 switch (manuf) {
293 case BYTEME(AMD_MANUFACT):
294 info->flash_id = FLASH_MAN_AMD;
295 break;
296 case BYTEME(FUJ_MANUFACT):
297 info->flash_id = FLASH_MAN_FUJ;
298 break;
299 case BYTEME(SST_MANUFACT):
300 info->flash_id = FLASH_MAN_SST;
301 break;
302 case BYTEME(STM_MANUFACT):
303 info->flash_id = FLASH_MAN_STM;
304 break;
305 case BYTEME(INTEL_MANUFACT):
306 info->flash_id = FLASH_MAN_INTEL;
307 break;
308 default:
309 info->flash_id = FLASH_UNKNOWN;
310 info->sector_count = 0;
311 info->size = 0;
312 addr[0] = BS(0xFF);
313 return 0;
314 }
315
316 device = BS(addr[2]);
317
318 DEBUGF("Device ID @ 0x%08x: 0x%08x\n", (&addr[1]), device);
319
320 switch (device) {
321 case BYTEME(INTEL_ID_28F320J3A):
322 info->flash_id += FLASH_28F320J3A;
323 info->sector_count = 32;
324 info->size = 0x00400000;
325 break;
326
327 case BYTEME(INTEL_ID_28F640J3A):
328 info->flash_id += FLASH_28F640J3A;
329 info->sector_count = 64;
330 info->size = 0x00800000;
331 break;
332
333 case BYTEME(INTEL_ID_28F128J3A):
334 info->flash_id += FLASH_28F128J3A;
335 info->sector_count = 128;
336 info->size = 0x01000000;
337 break;
338
339 default:
340 info->flash_id = FLASH_UNKNOWN;
341 addr[0] = BS(0xFF);
342 return 0;
343
344 }
345
346 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
347 printf ("** ERROR: sector count %d > max (%d) **\n",
348 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
349 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
350 }
351
352 addr[0] = BS(0xFF);
353
354 return (info->size);
355}
356
357
358
359
360
361int flash_erase (flash_info_t *info, int s_first, int s_last)
362{
363 int flag, prot, sect;
364 ulong start, now, last;
365
366 if ((s_first < 0) || (s_first > s_last)) {
367 if (info->flash_id == FLASH_UNKNOWN) {
368 printf ("- missing\n");
369 } else {
370 printf ("- no sectors to erase\n");
371 }
372 return 1;
373 }
374
375 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
376 printf ("Can erase only Intel flash types - aborted\n");
377 return 1;
378 }
379
380 prot = 0;
381 for (sect=s_first; sect<=s_last; ++sect) {
382 if (info->protect[sect]) {
383 prot++;
384 }
385 }
386
387 if (prot) {
388 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
389 } else {
390 printf ("\n");
391 }
392
393 start = get_timer (0);
394 last = start;
395
396 for (sect = s_first; sect<=s_last; sect++) {
397 if (info->protect[sect] == 0) {
398 vu_char *addr = (vu_char *)(info->start[sect]);
399 unsigned long status;
400
401
402 flag = disable_interrupts();
403
404 *addr = BS(0x50);
405 *addr = BS(0x20);
406 *addr = BS(0xD0);
407
408
409 if (flag) {
410 enable_interrupts();
411 }
412
413
414 udelay (1000);
415
416 while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
417 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
418 printf ("Timeout\n");
419 *addr = BS(0xB0);
420 *addr = BS(0xFF);
421 return 1;
422 }
423
424
425 if ((now - last) > 1000) {
426 putc ('.');
427 last = now;
428 }
429 }
430
431 *addr = BS(0xFF);
432 }
433 }
434 printf (" done\n");
435 return 0;
436}
437
438
439
440
441
442
443
444
445
446#define FLASH_WIDTH 1
447
448int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
449{
450 uchar *wp = (uchar *)addr;
451 int rc;
452
453 if (info->flash_id == FLASH_UNKNOWN) {
454 return 4;
455 }
456
457 while (cnt > 0) {
458 if ((rc = write_data(info, wp, *src)) != 0) {
459 return rc;
460 }
461 wp++;
462 src++;
463 cnt--;
464 }
465
466 return cnt;
467}
468
469
470
471
472
473
474
475static int write_data (flash_info_t *info, uchar *dest, uchar data)
476{
477 vu_char *addr = (vu_char *)dest;
478 ulong status;
479 ulong start;
480 int flag;
481
482
483 if ((BS(*addr) & data) != data) {
484 return 2;
485 }
486
487 flag = disable_interrupts();
488
489 *addr = BS(0x40);
490 *addr = data;
491
492
493 if (flag) {
494 enable_interrupts();
495 }
496
497 start = get_timer (0);
498
499 while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
500 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
501 *addr = BS(0xFF);
502 return 1;
503 }
504 }
505
506 *addr = BS(0xFF);
507
508 return 0;
509}
510
511
512
513