blob: 1ab668ce8a355ac24d95d80e4eb60a14af00854e [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +00006 */
7
8#include <common.h>
9#include <mpc824x.h>
10#include <asm/processor.h>
11#include <asm/pci_io.h>
12#include <w83c553f.h>
13
14#define ROM_CS0_START 0xFF800000
15#define ROM_CS1_START 0xFF000000
16
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020017flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkfe8c2802002-11-03 00:38:21 +000018
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020019#if defined(CONFIG_ENV_IS_IN_FLASH)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020020# ifndef CONFIG_ENV_ADDR
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020021# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenkfe8c2802002-11-03 00:38:21 +000022# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020023# ifndef CONFIG_ENV_SIZE
24# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
wdenkfe8c2802002-11-03 00:38:21 +000025# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020026# ifndef CONFIG_ENV_SECT_SIZE
27# define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
wdenkfe8c2802002-11-03 00:38:21 +000028# endif
29#endif
30
31/*-----------------------------------------------------------------------
32 * Functions
33 */
34static int write_word (flash_info_t *info, ulong dest, ulong data);
35#if 0
36static void flash_get_offsets (ulong base, flash_info_t *info);
37#endif /* 0 */
38
39/*flash command address offsets*/
40
41#if 0
42#define ADDR0 (0x555)
43#define ADDR1 (0x2AA)
44#define ADDR3 (0x001)
45#else
46#define ADDR0 (0xAAA)
47#define ADDR1 (0x555)
48#define ADDR3 (0x001)
49#endif
50
51#define FLASH_WORD_SIZE unsigned char
52
53/*-----------------------------------------------------------------------
54 */
55
56#if 0
57static int byte_parity_odd(unsigned char x) __attribute__ ((const));
58#endif /* 0 */
59static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
60
61typedef struct
62{
63 FLASH_WORD_SIZE extval;
64 unsigned short intval;
65} map_entry;
66
67#if 0
68static int
69byte_parity_odd(unsigned char x)
70{
71 x ^= x >> 4;
72 x ^= x >> 2;
73 x ^= x >> 1;
74 return (x & 0x1) != 0;
75}
76#endif /* 0 */
77
78
wdenkfe8c2802002-11-03 00:38:21 +000079static unsigned long
80flash_id(unsigned char mfct, unsigned char chip)
81{
82 static const map_entry mfct_map[] =
83 {
84 {(FLASH_WORD_SIZE) AMD_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
85 {(FLASH_WORD_SIZE) FUJ_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
86 {(FLASH_WORD_SIZE) STM_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
87 {(FLASH_WORD_SIZE) MT_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
88 {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
89 {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
90 };
91
92 static const map_entry chip_map[] =
93 {
94 {AMD_ID_F040B, FLASH_AM040},
95 {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
96 };
97
98 const map_entry *p;
99 unsigned long result = FLASH_UNKNOWN;
100
101 /* find chip id */
102 for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
103 if(p->extval == chip)
104 {
105 result = FLASH_VENDMASK | p->intval;
106 break;
107 }
108
109 /* find vendor id */
110 for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
111 if(p->extval == mfct)
112 {
113 result &= ~FLASH_VENDMASK;
114 result |= (unsigned long) p->intval << 16;
115 break;
116 }
117
118 return result;
119}
120
121
wdenkfe8c2802002-11-03 00:38:21 +0000122unsigned long
123flash_init(void)
124{
125 unsigned long i;
126 unsigned char j;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200127 static const ulong flash_banks[] = CONFIG_SYS_FLASH_BANKS;
wdenkfe8c2802002-11-03 00:38:21 +0000128
129 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200130 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++)
wdenkfe8c2802002-11-03 00:38:21 +0000131 {
132 flash_info_t * const pflinfo = &flash_info[i];
133 pflinfo->flash_id = FLASH_UNKNOWN;
134 pflinfo->size = 0;
135 pflinfo->sector_count = 0;
136 }
137
138 /* Enable writes to Sandpoint flash */
139 {
140 register unsigned char temp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200141 CONFIG_READ_BYTE(CONFIG_SYS_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR, temp);
wdenkfe8c2802002-11-03 00:38:21 +0000142 temp &= ~0x20; /* clear BIOSWP bit */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200143 CONFIG_WRITE_BYTE(CONFIG_SYS_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR, temp);
wdenkfe8c2802002-11-03 00:38:21 +0000144 }
145
146 for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++)
147 {
148 flash_info_t * const pflinfo = &flash_info[i];
149 const unsigned long base_address = flash_banks[i];
150 volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
151#if 0
152 volatile FLASH_WORD_SIZE * addr2;
153#endif
154#if 0
155 /* write autoselect sequence */
156 flash[0x5555] = 0xaa;
157 flash[0x2aaa] = 0x55;
158 flash[0x5555] = 0x90;
159#else
160 flash[0xAAA << (3 * i)] = 0xaa;
161 flash[0x555 << (3 * i)] = 0x55;
162 flash[0xAAA << (3 * i)] = 0x90;
163#endif
164 __asm__ __volatile__("sync");
165
166#if 0
167 pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
168#else
169 pflinfo->flash_id = flash_id(flash[0x0], flash[0x2 + 14 * i]);
170#endif
171
172 switch(pflinfo->flash_id & FLASH_TYPEMASK)
173 {
174 case FLASH_AM040:
wdenk8bde7f72003-06-27 21:31:46 +0000175 pflinfo->size = 0x00080000;
wdenkfe8c2802002-11-03 00:38:21 +0000176 pflinfo->sector_count = 8;
wdenk8bde7f72003-06-27 21:31:46 +0000177 for(j = 0; j < 8; j++)
wdenkfe8c2802002-11-03 00:38:21 +0000178 {
179 pflinfo->start[j] = base_address + 0x00010000 * j;
180 pflinfo->protect[j] = flash[(j << 16) | 0x2];
181 }
182 break;
183 case FLASH_STM800AB:
184 pflinfo->size = 0x00100000;
185 pflinfo->sector_count = 19;
186 pflinfo->start[0] = base_address;
187 pflinfo->start[1] = base_address + 0x4000;
188 pflinfo->start[2] = base_address + 0x6000;
189 pflinfo->start[3] = base_address + 0x8000;
190 for(j = 1; j < 16; j++)
191 {
192 pflinfo->start[j+3] = base_address + 0x00010000 * j;
193 }
194#if 0
wdenk8bde7f72003-06-27 21:31:46 +0000195 /* check for protected sectors */
196 for (j = 0; j < pflinfo->sector_count; j++) {
197 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
198 /* D0 = 1 if protected */
199 addr2 = (volatile FLASH_WORD_SIZE *)(pflinfo->start[j]);
200 if (pflinfo->flash_id & FLASH_MAN_SST)
201 pflinfo->protect[j] = 0;
202 else
203 pflinfo->protect[j] = addr2[2] & 1;
204 }
wdenkfe8c2802002-11-03 00:38:21 +0000205#endif
206 break;
207 }
208 /* Protect monitor and environment sectors
209 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200210#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkfe8c2802002-11-03 00:38:21 +0000211 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200212 CONFIG_SYS_MONITOR_BASE,
213 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
wdenkfe8c2802002-11-03 00:38:21 +0000214 &flash_info[0]);
215#endif
216
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200217#if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
wdenkfe8c2802002-11-03 00:38:21 +0000218 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200219 CONFIG_ENV_ADDR,
220 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
wdenkfe8c2802002-11-03 00:38:21 +0000221 &flash_info[0]);
222#endif
223
224 /* reset device to read mode */
225 flash[0x0000] = 0xf0;
226 __asm__ __volatile__("sync");
227 }
228
229 return flash_info[0].size + flash_info[1].size;
230}
231
232#if 0
233static void
234flash_get_offsets (ulong base, flash_info_t *info)
235{
236 int i;
237
238 /* set up sector start address table */
wdenk8bde7f72003-06-27 21:31:46 +0000239 if (info->flash_id & FLASH_MAN_SST)
240 {
241 for (i = 0; i < info->sector_count; i++)
242 info->start[i] = base + (i * 0x00010000);
243 }
244 else
wdenkfe8c2802002-11-03 00:38:21 +0000245 if (info->flash_id & FLASH_BTYPE) {
wdenk8bde7f72003-06-27 21:31:46 +0000246 /* set sector offsets for bottom boot block type */
247 info->start[0] = base + 0x00000000;
248 info->start[1] = base + 0x00004000;
249 info->start[2] = base + 0x00006000;
250 info->start[3] = base + 0x00008000;
251 for (i = 4; i < info->sector_count; i++) {
252 info->start[i] = base + (i * 0x00010000) - 0x00030000;
253 }
wdenkfe8c2802002-11-03 00:38:21 +0000254 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000255 /* set sector offsets for top boot block type */
256 i = info->sector_count - 1;
257 info->start[i--] = base + info->size - 0x00004000;
258 info->start[i--] = base + info->size - 0x00006000;
259 info->start[i--] = base + info->size - 0x00008000;
260 for (; i >= 0; i--) {
261 info->start[i] = base + i * 0x00010000;
262 }
wdenkfe8c2802002-11-03 00:38:21 +0000263 }
264
265}
266#endif /* 0 */
267
268/*-----------------------------------------------------------------------
269 */
270void
271flash_print_info(flash_info_t *info)
272{
273 static const char unk[] = "Unknown";
274 const char *mfct = unk, *type = unk;
275 unsigned int i;
276
277 if(info->flash_id != FLASH_UNKNOWN)
278 {
279 switch(info->flash_id & FLASH_VENDMASK)
280 {
281 case FLASH_MAN_AMD: mfct = "AMD"; break;
282 case FLASH_MAN_FUJ: mfct = "FUJITSU"; break;
283 case FLASH_MAN_STM: mfct = "STM"; break;
284 case FLASH_MAN_SST: mfct = "SST"; break;
285 case FLASH_MAN_BM: mfct = "Bright Microelectonics"; break;
286 case FLASH_MAN_INTEL: mfct = "Intel"; break;
287 }
288
289 switch(info->flash_id & FLASH_TYPEMASK)
290 {
291 case FLASH_AM040: type = "AM29F040B (512K * 8, uniform sector size)"; break;
292 case FLASH_AM400B: type = "AM29LV400B (4 Mbit, bottom boot sect)"; break;
293 case FLASH_AM400T: type = "AM29LV400T (4 Mbit, top boot sector)"; break;
294 case FLASH_AM800B: type = "AM29LV800B (8 Mbit, bottom boot sect)"; break;
295 case FLASH_AM800T: type = "AM29LV800T (8 Mbit, top boot sector)"; break;
296 case FLASH_AM160T: type = "AM29LV160T (16 Mbit, top boot sector)"; break;
297 case FLASH_AM320B: type = "AM29LV320B (32 Mbit, bottom boot sect)"; break;
298 case FLASH_AM320T: type = "AM29LV320T (32 Mbit, top boot sector)"; break;
299 case FLASH_STM800AB: type = "M29W800AB (8 Mbit, bottom boot sect)"; break;
300 case FLASH_SST800A: type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; break;
301 case FLASH_SST160A: type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; break;
302 }
303 }
304
305 printf(
306 "\n Brand: %s Type: %s\n"
307 " Size: %lu KB in %d Sectors\n",
308 mfct,
309 type,
310 info->size >> 10,
311 info->sector_count
312 );
313
314 printf (" Sector Start Addresses:");
315
316 for (i = 0; i < info->sector_count; i++)
317 {
318 unsigned long size;
319 unsigned int erased;
320 unsigned long * flash = (unsigned long *) info->start[i];
321
322 /*
323 * Check if whole sector is erased
324 */
325 size =
326 (i != (info->sector_count - 1)) ?
327 (info->start[i + 1] - info->start[i]) >> 2 :
328 (info->start[0] + info->size - info->start[i]) >> 2;
329
330 for(
331 flash = (unsigned long *) info->start[i], erased = 1;
332 (flash != (unsigned long *) info->start[i] + size) && erased;
333 flash++
334 )
335 erased = *flash == ~0x0UL;
336
337 printf(
338 "%s %08lX %s %s",
339 (i % 5) ? "" : "\n ",
340 info->start[i],
341 erased ? "E" : " ",
342 info->protect[i] ? "RO" : " "
343 );
344 }
345
346 puts("\n");
347 return;
348}
349
350#if 0
351
352/*
353 * The following code cannot be run from FLASH!
354 */
355ulong
356flash_get_size (vu_long *addr, flash_info_t *info)
357{
358 short i;
359 FLASH_WORD_SIZE value;
360 ulong base = (ulong)addr;
wdenk8bde7f72003-06-27 21:31:46 +0000361 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
wdenkfe8c2802002-11-03 00:38:21 +0000362
363 printf("flash_get_size: \n");
364 /* Write auto select command: read Manufacturer ID */
365 eieio();
366 addr2[ADDR0] = (FLASH_WORD_SIZE)0xAA;
367 addr2[ADDR1] = (FLASH_WORD_SIZE)0x55;
368 addr2[ADDR0] = (FLASH_WORD_SIZE)0x90;
369 value = addr2[0];
370
371 switch (value) {
372 case (FLASH_WORD_SIZE)AMD_MANUFACT:
wdenk8bde7f72003-06-27 21:31:46 +0000373 info->flash_id = FLASH_MAN_AMD;
374 break;
wdenkfe8c2802002-11-03 00:38:21 +0000375 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
wdenk8bde7f72003-06-27 21:31:46 +0000376 info->flash_id = FLASH_MAN_FUJ;
377 break;
wdenkfe8c2802002-11-03 00:38:21 +0000378 case (FLASH_WORD_SIZE)SST_MANUFACT:
wdenk8bde7f72003-06-27 21:31:46 +0000379 info->flash_id = FLASH_MAN_SST;
380 break;
wdenkfe8c2802002-11-03 00:38:21 +0000381 default:
wdenk8bde7f72003-06-27 21:31:46 +0000382 info->flash_id = FLASH_UNKNOWN;
383 info->sector_count = 0;
384 info->size = 0;
385 return (0); /* no or unknown flash */
wdenkfe8c2802002-11-03 00:38:21 +0000386 }
387 printf("recognised manufacturer");
388
389 value = addr2[ADDR3]; /* device ID */
wdenk8bde7f72003-06-27 21:31:46 +0000390 debug ("\ndev_code=%x\n", value);
wdenkfe8c2802002-11-03 00:38:21 +0000391
392 switch (value) {
393 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
wdenk8bde7f72003-06-27 21:31:46 +0000394 info->flash_id += FLASH_AM400T;
395 info->sector_count = 11;
396 info->size = 0x00080000;
397 break; /* => 0.5 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000398
399 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
wdenk8bde7f72003-06-27 21:31:46 +0000400 info->flash_id += FLASH_AM400B;
401 info->sector_count = 11;
402 info->size = 0x00080000;
403 break; /* => 0.5 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000404
405 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
wdenk8bde7f72003-06-27 21:31:46 +0000406 info->flash_id += FLASH_AM800T;
407 info->sector_count = 19;
408 info->size = 0x00100000;
409 break; /* => 1 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000410
411 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
wdenk8bde7f72003-06-27 21:31:46 +0000412 info->flash_id += FLASH_AM800B;
413 info->sector_count = 19;
414 info->size = 0x00100000;
415 break; /* => 1 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000416
417 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
wdenk8bde7f72003-06-27 21:31:46 +0000418 info->flash_id += FLASH_AM160T;
419 info->sector_count = 35;
420 info->size = 0x00200000;
421 break; /* => 2 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000422
423 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
wdenk8bde7f72003-06-27 21:31:46 +0000424 info->flash_id += FLASH_AM160B;
425 info->sector_count = 35;
426 info->size = 0x00200000;
427 break; /* => 2 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000428
429 case (FLASH_WORD_SIZE)SST_ID_xF800A:
wdenk8bde7f72003-06-27 21:31:46 +0000430 info->flash_id += FLASH_SST800A;
431 info->sector_count = 16;
432 info->size = 0x00100000;
433 break; /* => 1 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000434
435 case (FLASH_WORD_SIZE)SST_ID_xF160A:
wdenk8bde7f72003-06-27 21:31:46 +0000436 info->flash_id += FLASH_SST160A;
437 info->sector_count = 32;
438 info->size = 0x00200000;
439 break; /* => 2 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000440
441 case (FLASH_WORD_SIZE)AMD_ID_F040B:
wdenk8bde7f72003-06-27 21:31:46 +0000442 info->flash_id += FLASH_AM040;
443 info->sector_count = 8;
444 info->size = 0x00080000;
445 break; /* => 0.5 MB */
wdenkfe8c2802002-11-03 00:38:21 +0000446
447 default:
wdenk8bde7f72003-06-27 21:31:46 +0000448 info->flash_id = FLASH_UNKNOWN;
449 return (0); /* => no or unknown flash */
wdenkfe8c2802002-11-03 00:38:21 +0000450
451 }
452
453 printf("flash id %lx; sector count %x, size %lx\n", info->flash_id,info->sector_count,info->size);
454 /* set up sector start address table */
wdenk8bde7f72003-06-27 21:31:46 +0000455 if (info->flash_id & FLASH_MAN_SST)
456 {
457 for (i = 0; i < info->sector_count; i++)
458 info->start[i] = base + (i * 0x00010000);
459 }
460 else
wdenkfe8c2802002-11-03 00:38:21 +0000461 if (info->flash_id & FLASH_BTYPE) {
wdenk8bde7f72003-06-27 21:31:46 +0000462 /* set sector offsets for bottom boot block type */
463 info->start[0] = base + 0x00000000;
464 info->start[1] = base + 0x00004000;
465 info->start[2] = base + 0x00006000;
466 info->start[3] = base + 0x00008000;
467 for (i = 4; i < info->sector_count; i++) {
468 info->start[i] = base + (i * 0x00010000) - 0x00030000;
469 }
wdenkfe8c2802002-11-03 00:38:21 +0000470 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000471 /* set sector offsets for top boot block type */
472 i = info->sector_count - 1;
473 info->start[i--] = base + info->size - 0x00004000;
474 info->start[i--] = base + info->size - 0x00006000;
475 info->start[i--] = base + info->size - 0x00008000;
476 for (; i >= 0; i--) {
477 info->start[i] = base + i * 0x00010000;
478 }
wdenkfe8c2802002-11-03 00:38:21 +0000479 }
480
481 /* check for protected sectors */
482 for (i = 0; i < info->sector_count; i++) {
wdenk8bde7f72003-06-27 21:31:46 +0000483 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
484 /* D0 = 1 if protected */
485 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
486 if (info->flash_id & FLASH_MAN_SST)
487 info->protect[i] = 0;
488 else
489 info->protect[i] = addr2[2] & 1;
wdenkfe8c2802002-11-03 00:38:21 +0000490 }
491
492 /*
493 * Prevent writes to uninitialized FLASH.
494 */
495 if (info->flash_id != FLASH_UNKNOWN) {
496 addr2 = (FLASH_WORD_SIZE *)info->start[0];
wdenk8bde7f72003-06-27 21:31:46 +0000497 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
wdenkfe8c2802002-11-03 00:38:21 +0000498 }
499
500 return (info->size);
501}
502
503#endif
504
505
506int
507flash_erase(flash_info_t *info, int s_first, int s_last)
508{
509 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
510 int flag, prot, sect, l_sect;
511 ulong start, now, last;
512 unsigned char sh8b;
513
514 if ((s_first < 0) || (s_first > s_last)) {
wdenk8bde7f72003-06-27 21:31:46 +0000515 if (info->flash_id == FLASH_UNKNOWN) {
516 printf ("- missing\n");
517 } else {
518 printf ("- no sectors to erase\n");
519 }
520 return 1;
wdenkfe8c2802002-11-03 00:38:21 +0000521 }
522
523 if ((info->flash_id == FLASH_UNKNOWN) ||
wdenk8bde7f72003-06-27 21:31:46 +0000524 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
525 printf ("Can't erase unknown flash type - aborted\n");
526 return 1;
wdenkfe8c2802002-11-03 00:38:21 +0000527 }
528
529 prot = 0;
530 for (sect=s_first; sect<=s_last; ++sect) {
wdenk8bde7f72003-06-27 21:31:46 +0000531 if (info->protect[sect]) {
532 prot++;
533 }
wdenkfe8c2802002-11-03 00:38:21 +0000534 }
535
536 if (prot) {
wdenk8bde7f72003-06-27 21:31:46 +0000537 printf ("- Warning: %d protected sectors will not be erased!\n",
538 prot);
wdenkfe8c2802002-11-03 00:38:21 +0000539 } else {
wdenk8bde7f72003-06-27 21:31:46 +0000540 printf ("\n");
wdenkfe8c2802002-11-03 00:38:21 +0000541 }
542
543 l_sect = -1;
544
545 /* Check the ROM CS */
546 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
547 sh8b = 3;
548 else
549 sh8b = 0;
550
551 /* Disable interrupts which might cause a timeout here */
552 flag = disable_interrupts();
553
554 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
555 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
556 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
557 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
558 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
559
560 /* Start erase on unprotected sectors */
561 for (sect = s_first; sect<=s_last; sect++) {
wdenk8bde7f72003-06-27 21:31:46 +0000562 if (info->protect[sect] == 0) { /* not protected */
563 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
wdenkfe8c2802002-11-03 00:38:21 +0000564 (info->start[sect] - info->start[0]) << sh8b));
wdenk8bde7f72003-06-27 21:31:46 +0000565 if (info->flash_id & FLASH_MAN_SST)
566 {
567 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
568 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
569 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
570 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
571 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
572 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
573 udelay(30000); /* wait 30 ms */
574 }
575 else
576 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
577 l_sect = sect;
578 }
wdenkfe8c2802002-11-03 00:38:21 +0000579 }
580
581 /* re-enable interrupts if necessary */
582 if (flag)
wdenk8bde7f72003-06-27 21:31:46 +0000583 enable_interrupts();
wdenkfe8c2802002-11-03 00:38:21 +0000584
585 /* wait at least 80us - let's wait 1 ms */
586 udelay (1000);
587
588 /*
589 * We wait for the last triggered sector
590 */
591 if (l_sect < 0)
wdenk8bde7f72003-06-27 21:31:46 +0000592 goto DONE;
wdenkfe8c2802002-11-03 00:38:21 +0000593
594 start = get_timer (0);
595 last = start;
596 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
597 (info->start[l_sect] - info->start[0]) << sh8b));
598 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200599 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk8bde7f72003-06-27 21:31:46 +0000600 printf ("Timeout\n");
601 return 1;
602 }
603 /* show that we're waiting */
604 if ((now - last) > 1000) { /* every second */
605 serial_putc ('.');
606 last = now;
607 }
wdenkfe8c2802002-11-03 00:38:21 +0000608 }
609
610DONE:
611 /* reset to read mode */
612 addr = (FLASH_WORD_SIZE *)info->start[0];
613 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
614
615 printf (" done\n");
616 return 0;
617}
618
619/*-----------------------------------------------------------------------
620 * Copy memory to flash, returns:
621 * 0 - OK
622 * 1 - write timeout
623 * 2 - Flash not erased
624 */
625
626int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
627{
628 ulong cp, wp, data;
629 int i, l, rc;
630
631 wp = (addr & ~3); /* get lower word aligned address */
632
633 /*
634 * handle unaligned start bytes
635 */
636 if ((l = addr - wp) != 0) {
wdenk8bde7f72003-06-27 21:31:46 +0000637 data = 0;
638 for (i=0, cp=wp; i<l; ++i, ++cp) {
639 data = (data << 8) | (*(uchar *)cp);
640 }
641 for (; i<4 && cnt>0; ++i) {
642 data = (data << 8) | *src++;
643 --cnt;
644 ++cp;
645 }
646 for (; cnt==0 && i<4; ++i, ++cp) {
647 data = (data << 8) | (*(uchar *)cp);
648 }
wdenkfe8c2802002-11-03 00:38:21 +0000649
wdenk8bde7f72003-06-27 21:31:46 +0000650 if ((rc = write_word(info, wp, data)) != 0) {
651 return (rc);
652 }
653 wp += 4;
wdenkfe8c2802002-11-03 00:38:21 +0000654 }
655
656 /*
657 * handle word aligned part
658 */
659 while (cnt >= 4) {
wdenk8bde7f72003-06-27 21:31:46 +0000660 data = 0;
661 for (i=0; i<4; ++i) {
662 data = (data << 8) | *src++;
663 }
664 if ((rc = write_word(info, wp, data)) != 0) {
665 return (rc);
666 }
667 wp += 4;
668 cnt -= 4;
wdenkfe8c2802002-11-03 00:38:21 +0000669 }
670
671 if (cnt == 0) {
wdenk8bde7f72003-06-27 21:31:46 +0000672 return (0);
wdenkfe8c2802002-11-03 00:38:21 +0000673 }
674
675 /*
676 * handle unaligned tail bytes
677 */
678 data = 0;
679 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
wdenk8bde7f72003-06-27 21:31:46 +0000680 data = (data << 8) | *src++;
681 --cnt;
wdenkfe8c2802002-11-03 00:38:21 +0000682 }
683 for (; i<4; ++i, ++cp) {
wdenk8bde7f72003-06-27 21:31:46 +0000684 data = (data << 8) | (*(uchar *)cp);
wdenkfe8c2802002-11-03 00:38:21 +0000685 }
686
687 return (write_word(info, wp, data));
688}
689
690/*-----------------------------------------------------------------------
691 * Write a word to Flash, returns:
692 * 0 - OK
693 * 1 - write timeout
694 * 2 - Flash not erased
695 */
696static int write_word (flash_info_t *info, ulong dest, ulong data)
697{
wdenk8bde7f72003-06-27 21:31:46 +0000698 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
699 volatile FLASH_WORD_SIZE *dest2;
700 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
wdenkfe8c2802002-11-03 00:38:21 +0000701 ulong start;
702 int flag;
wdenk8bde7f72003-06-27 21:31:46 +0000703 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000704 unsigned char sh8b;
705
706 /* Check the ROM CS */
707 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
708 sh8b = 3;
709 else
710 sh8b = 0;
711
712 dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
713 info->start[0]);
714
715 /* Check if Flash is (sufficiently) erased */
716 if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
wdenk8bde7f72003-06-27 21:31:46 +0000717 return (2);
wdenkfe8c2802002-11-03 00:38:21 +0000718 }
719 /* Disable interrupts which might cause a timeout here */
720 flag = disable_interrupts();
721
wdenk8bde7f72003-06-27 21:31:46 +0000722 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
723 {
724 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
725 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
726 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
wdenkfe8c2802002-11-03 00:38:21 +0000727
wdenk8bde7f72003-06-27 21:31:46 +0000728 dest2[i << sh8b] = data2[i];
wdenkfe8c2802002-11-03 00:38:21 +0000729
wdenk8bde7f72003-06-27 21:31:46 +0000730 /* re-enable interrupts if necessary */
731 if (flag)
732 enable_interrupts();
wdenkfe8c2802002-11-03 00:38:21 +0000733
wdenk8bde7f72003-06-27 21:31:46 +0000734 /* data polling for D7 */
735 start = get_timer (0);
736 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
737 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200738 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk8bde7f72003-06-27 21:31:46 +0000739 return (1);
740 }
741 }
742 }
wdenkfe8c2802002-11-03 00:38:21 +0000743
744 return (0);
745}
746
747/*-----------------------------------------------------------------------
748 */