blob: 080ec7fc3d2e39dfc98b5c6ae9bd2936743c9dfc [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8#include <common.h>
9#include <mpc824x.h>
10
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020011#if defined(CONFIG_ENV_IS_IN_FLASH)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020012# ifndef CONFIG_ENV_ADDR
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020013# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenkaffae2b2002-08-17 09:36:01 +000014# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020015# ifndef CONFIG_ENV_SIZE
16# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
wdenkaffae2b2002-08-17 09:36:01 +000017# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020018# ifndef CONFIG_ENV_SECT_SIZE
19# define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
wdenkaffae2b2002-08-17 09:36:01 +000020# endif
21#endif
22
23/*---------------------------------------------------------------------*/
24#undef DEBUG_FLASH
25
26#ifdef DEBUG_FLASH
27#define DEBUGF(fmt,args...) printf(fmt ,##args)
28#else
29#define DEBUGF(fmt,args...)
30#endif
31/*---------------------------------------------------------------------*/
32
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020033flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000034
35/*-----------------------------------------------------------------------
36 * Functions
37 */
38static ulong flash_get_size (vu_char *addr, flash_info_t *info);
39static int write_data (flash_info_t *info, uchar *dest, uchar data);
40static void flash_get_offsets (ulong base, flash_info_t *info);
41
42
wdenkaffae2b2002-08-17 09:36:01 +000043/*
44 * don't ask. its stupid, but more than one soul has had to live with this mistake
45 * "swaptab[i]" is the value of "i" with the bits reversed.
46 */
47
48#define MUSENKI_BROKEN_FLASH 1
49
50#ifdef MUSENKI_BROKEN_FLASH
51unsigned char swaptab[256] = {
52 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
53 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
54 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
55 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
56 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
57 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
58 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
59 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
60 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
61 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
62 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
63 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
64 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
65 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
66 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
67 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
68 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
69 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
70 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
71 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
72 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
73 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
74 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
75 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
76 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
77 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
78 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
79 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
80 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
81 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
82 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
83 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
84};
85
86#define BS(b) (swaptab[b])
87
88#else
89
90#define BS(b) (b)
91
92#endif
93
94#define BYTEME(x) ((x) & 0xFF)
95
96/*-----------------------------------------------------------------------
97 */
98
99unsigned long flash_init (void)
100{
101 unsigned long size_b0, size_b1;
102 int i;
103
104 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200105 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000106 flash_info[i].flash_id = FLASH_UNKNOWN;
107 }
108
109 /* Static FLASH Bank configuration here - FIXME XXX */
110
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200111 DEBUGF("\n## Get flash bank 1 size @ 0x%08x\n",CONFIG_SYS_FLASH_BASE0_PRELIM);
wdenkaffae2b2002-08-17 09:36:01 +0000112
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200113 size_b0 = flash_get_size((vu_char *)CONFIG_SYS_FLASH_BASE0_PRELIM, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000114
115 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
116 printf ("## Unknown FLASH on Bank 0: "
117 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
118 flash_info[0].flash_id,
119 size_b0, size_b0<<20);
120 }
121
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122 DEBUGF("## Get flash bank 2 size @ 0x%08x\n",CONFIG_SYS_FLASH_BASE1_PRELIM);
123 size_b1 = flash_get_size((vu_char *)CONFIG_SYS_FLASH_BASE1_PRELIM, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000124
125 DEBUGF("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
126
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200127 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000128
129 flash_info[0].size = size_b0;
130
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200131#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
132 DEBUGF("protect monitor %x @ %x\n", CONFIG_SYS_MONITOR_BASE, monitor_flash_len);
wdenkaffae2b2002-08-17 09:36:01 +0000133 /* monitor protection ON by default */
134 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200135 CONFIG_SYS_MONITOR_BASE,
136 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +0000137 &flash_info[0]);
138#endif
139
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +0200140#ifdef CONFIG_ENV_IS_IN_FLASH
wdenkaffae2b2002-08-17 09:36:01 +0000141 /* ENV protection ON by default */
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200142 DEBUGF("protect environtment %x @ %x\n", CONFIG_ENV_ADDR, CONFIG_ENV_SECT_SIZE);
wdenkaffae2b2002-08-17 09:36:01 +0000143 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200144 CONFIG_ENV_ADDR,
145 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
wdenkaffae2b2002-08-17 09:36:01 +0000146 &flash_info[0]);
147#endif
148
149 if (size_b1) {
150 flash_info[1].size = size_b1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200151 flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000152
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200153#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +0000154 /* monitor protection ON by default */
155 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200156 CONFIG_SYS_MONITOR_BASE,
157 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +0000158 &flash_info[1]);
159#endif
160
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +0200161#ifdef CONFIG_ENV_IS_IN_FLASH
wdenkaffae2b2002-08-17 09:36:01 +0000162 /* ENV protection ON by default */
163 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200164 CONFIG_ENV_ADDR,
165 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
wdenkaffae2b2002-08-17 09:36:01 +0000166 &flash_info[1]);
167#endif
168 } else {
169 flash_info[1].flash_id = FLASH_UNKNOWN;
170 flash_info[1].sector_count = -1;
171 flash_info[1].size = 0;
172 }
173
174 DEBUGF("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
175
176 return (size_b0 + size_b1);
177}
178
179/*-----------------------------------------------------------------------
180 */
181static void flash_get_offsets (ulong base, flash_info_t *info)
182{
183 int i;
184
185 if (info->flash_id == FLASH_UNKNOWN) {
186 return;
187 }
188
189 switch (info->flash_id & FLASH_VENDMASK) {
190 case FLASH_MAN_INTEL:
191 for (i = 0; i < info->sector_count; i++) {
192 info->start[i] = base;
193 base += 0x00020000; /* 128k per bank */
194 }
195 return;
196
197 default:
198 printf ("Don't know sector ofsets for flash type 0x%lx\n", info->flash_id);
199 return;
200 }
201}
202
203/*-----------------------------------------------------------------------
204 */
205void flash_print_info (flash_info_t *info)
206{
207 int i;
208
209 if (info->flash_id == FLASH_UNKNOWN) {
210 printf ("missing or unknown FLASH type\n");
211 return;
212 }
213
214 switch (info->flash_id & FLASH_VENDMASK) {
215 case FLASH_MAN_AMD: printf ("AMD "); break;
216 case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
217 case FLASH_MAN_SST: printf ("SST "); break;
218 case FLASH_MAN_STM: printf ("STM "); break;
219 case FLASH_MAN_INTEL: printf ("Intel "); break;
220 case FLASH_MAN_MT: printf ("MT "); break;
221 default: printf ("Unknown Vendor "); break;
222 }
223
224 switch (info->flash_id & FLASH_TYPEMASK) {
225 case FLASH_28F320J3A: printf ("28F320J3A (32Mbit = 128K x 32)\n");
226 break;
227 case FLASH_28F640J3A: printf ("28F640J3A (64Mbit = 128K x 64)\n");
228 break;
229 case FLASH_28F128J3A: printf ("28F128J3A (128Mbit = 128K x 128)\n");
230 break;
231 default: printf ("Unknown Chip Type\n");
232 break;
233 }
234
235 if (info->size >= (1 << 20)) {
236 i = 20;
237 } else {
238 i = 10;
239 }
240 printf (" Size: %ld %cB in %d Sectors\n",
241 info->size >> i,
242 (i == 20) ? 'M' : 'k',
243 info->sector_count);
244
245 printf (" Sector Start Addresses:");
246 for (i=0; i<info->sector_count; ++i) {
247 if ((i % 5) == 0)
248 printf ("\n ");
249 printf (" %08lX%s",
250 info->start[i],
251 info->protect[i] ? " (RO)" : " "
252 );
253 }
254 printf ("\n");
255 return;
256}
257
258/*-----------------------------------------------------------------------
259 */
260
261
262/*-----------------------------------------------------------------------
263 */
264
265/*
266 * The following code cannot be run from FLASH!
267 */
268static ulong flash_get_size (vu_char *addr, flash_info_t *info)
269{
270 vu_char manuf, device;
271
272 addr[0] = BS(0x90);
273 manuf = BS(addr[0]);
274 DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (vu_char *)addr, manuf);
275
276 switch (manuf) {
277 case BYTEME(AMD_MANUFACT):
278 info->flash_id = FLASH_MAN_AMD;
279 break;
280 case BYTEME(FUJ_MANUFACT):
281 info->flash_id = FLASH_MAN_FUJ;
282 break;
283 case BYTEME(SST_MANUFACT):
284 info->flash_id = FLASH_MAN_SST;
285 break;
286 case BYTEME(STM_MANUFACT):
287 info->flash_id = FLASH_MAN_STM;
288 break;
289 case BYTEME(INTEL_MANUFACT):
290 info->flash_id = FLASH_MAN_INTEL;
291 break;
292 default:
293 info->flash_id = FLASH_UNKNOWN;
294 info->sector_count = 0;
295 info->size = 0;
296 addr[0] = BS(0xFF); /* restore read mode, (yes, BS is a NOP) */
297 return 0; /* no or unknown flash */
298 }
299
300 device = BS(addr[2]); /* device ID */
301
302 DEBUGF("Device ID @ 0x%08x: 0x%08x\n", (&addr[1]), device);
303
304 switch (device) {
305 case BYTEME(INTEL_ID_28F320J3A):
306 info->flash_id += FLASH_28F320J3A;
307 info->sector_count = 32;
308 info->size = 0x00400000;
309 break; /* => 4 MB */
310
311 case BYTEME(INTEL_ID_28F640J3A):
312 info->flash_id += FLASH_28F640J3A;
313 info->sector_count = 64;
314 info->size = 0x00800000;
315 break; /* => 8 MB */
316
317 case BYTEME(INTEL_ID_28F128J3A):
318 info->flash_id += FLASH_28F128J3A;
319 info->sector_count = 128;
320 info->size = 0x01000000;
321 break; /* => 16 MB */
322
323 default:
324 info->flash_id = FLASH_UNKNOWN;
325 addr[0] = BS(0xFF); /* restore read mode (yes, a NOP) */
326 return 0; /* => no or unknown flash */
327
328 }
329
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200330 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
wdenkaffae2b2002-08-17 09:36:01 +0000331 printf ("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200332 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
333 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenkaffae2b2002-08-17 09:36:01 +0000334 }
335
336 addr[0] = BS(0xFF); /* restore read mode */
337
338 return (info->size);
339}
340
341
342/*-----------------------------------------------------------------------
343 */
344
345int flash_erase (flash_info_t *info, int s_first, int s_last)
346{
347 int flag, prot, sect;
348 ulong start, now, last;
349
350 if ((s_first < 0) || (s_first > s_last)) {
351 if (info->flash_id == FLASH_UNKNOWN) {
352 printf ("- missing\n");
353 } else {
354 printf ("- no sectors to erase\n");
355 }
356 return 1;
357 }
358
359 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
360 printf ("Can erase only Intel flash types - aborted\n");
361 return 1;
362 }
363
364 prot = 0;
365 for (sect=s_first; sect<=s_last; ++sect) {
366 if (info->protect[sect]) {
367 prot++;
368 }
369 }
370
371 if (prot) {
372 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
373 } else {
374 printf ("\n");
375 }
376
377 start = get_timer (0);
378 last = start;
379 /* Start erase on unprotected sectors */
380 for (sect = s_first; sect<=s_last; sect++) {
381 if (info->protect[sect] == 0) { /* not protected */
382 vu_char *addr = (vu_char *)(info->start[sect]);
383 unsigned long status;
384
385 /* Disable interrupts which might cause a timeout here */
386 flag = disable_interrupts();
387
388 *addr = BS(0x50); /* clear status register */
389 *addr = BS(0x20); /* erase setup */
390 *addr = BS(0xD0); /* erase confirm */
391
392 /* re-enable interrupts if necessary */
393 if (flag) {
394 enable_interrupts();
395 }
396
397 /* wait at least 80us - let's wait 1 ms */
398 udelay (1000);
399
400 while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200401 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000402 printf ("Timeout\n");
403 *addr = BS(0xB0); /* suspend erase */
404 *addr = BS(0xFF); /* reset to read mode */
405 return 1;
406 }
407
408 /* show that we're waiting */
409 if ((now - last) > 1000) { /* every second */
410 putc ('.');
411 last = now;
412 }
413 }
414
415 *addr = BS(0xFF); /* reset to read mode */
416 }
417 }
418 printf (" done\n");
419 return 0;
420}
421
422/*-----------------------------------------------------------------------
423 * Copy memory to flash, returns:
424 * 0 - OK
425 * 1 - write timeout
426 * 2 - Flash not erased
427 * 4 - Flash not identified
428 */
429
430#define FLASH_WIDTH 1 /* flash bus width in bytes */
431
432int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
433{
434 uchar *wp = (uchar *)addr;
435 int rc;
436
437 if (info->flash_id == FLASH_UNKNOWN) {
438 return 4;
439 }
440
441 while (cnt > 0) {
442 if ((rc = write_data(info, wp, *src)) != 0) {
443 return rc;
444 }
445 wp++;
446 src++;
447 cnt--;
448 }
449
450 return cnt;
451}
452
453/*-----------------------------------------------------------------------
454 * Write a word to Flash, returns:
455 * 0 - OK
456 * 1 - write timeout
457 * 2 - Flash not erased
458 */
459static int write_data (flash_info_t *info, uchar *dest, uchar data)
460{
461 vu_char *addr = (vu_char *)dest;
462 ulong status;
463 ulong start;
464 int flag;
465
466 /* Check if Flash is (sufficiently) erased */
467 if ((BS(*addr) & data) != data) {
468 return 2;
469 }
470 /* Disable interrupts which might cause a timeout here */
471 flag = disable_interrupts();
472
473 *addr = BS(0x40); /* write setup */
474 *addr = data;
475
476 /* re-enable interrupts if necessary */
477 if (flag) {
478 enable_interrupts();
479 }
480
481 start = get_timer (0);
482
483 while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200484 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000485 *addr = BS(0xFF); /* restore read mode */
486 return 1;
487 }
488 }
489
490 *addr = BS(0xFF); /* restore read mode */
491
492 return 0;
493}
494
495/*-----------------------------------------------------------------------
496 */