blob: e04af976f2d7b7bb104413855e02bfb67897c20d [file] [log] [blame]
wdenkc542fb22002-11-03 00:47:09 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <mpc824x.h>
26#include <asm/processor.h>
27#include <asm/pci_io.h>
28
29#define ROM_CS0_START 0xFF800000
30#define ROM_CS1_START 0xFF000000
31
32flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
33
34#if defined(CFG_ENV_IS_IN_FLASH)
35# ifndef CFG_ENV_ADDR
36# define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
37# endif
38# ifndef CFG_ENV_SIZE
39# define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
40# endif
41# ifndef CFG_ENV_SECT_SIZE
42# define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
43# endif
44#endif
45
46/*-----------------------------------------------------------------------
47 * Functions
48 */
49static int write_word (flash_info_t *info, ulong dest, ulong data);
50#if 0
51static void flash_get_offsets (ulong base, flash_info_t *info);
52#endif /* 0 */
53
54/*flash command address offsets*/
55
56#if 0
57#define ADDR0 (0x555)
58#define ADDR1 (0x2AA)
59#define ADDR3 (0x001)
60#else
61#define ADDR0 (0xAAA)
62#define ADDR1 (0x555)
63#define ADDR3 (0x001)
64#endif
65
66#define FLASH_WORD_SIZE unsigned char
67
68/*-----------------------------------------------------------------------
69 */
70
71#if 0
72static int byte_parity_odd(unsigned char x) __attribute__ ((const));
73#endif /* 0 */
74static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
75
76typedef struct
77{
78 FLASH_WORD_SIZE extval;
79 unsigned short intval;
80} map_entry;
81
82#if 0
83static int
84byte_parity_odd(unsigned char x)
85{
86 x ^= x >> 4;
87 x ^= x >> 2;
88 x ^= x >> 1;
89 return (x & 0x1) != 0;
90}
91#endif /* 0 */
92
93
wdenkc542fb22002-11-03 00:47:09 +000094static unsigned long
95flash_id(unsigned char mfct, unsigned char chip)
96{
97 static const map_entry mfct_map[] =
98 {
99 {(FLASH_WORD_SIZE) AMD_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
100 {(FLASH_WORD_SIZE) FUJ_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
101 {(FLASH_WORD_SIZE) STM_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
102 {(FLASH_WORD_SIZE) MT_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
103 {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
104 {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
105 };
106
107 static const map_entry chip_map[] =
108 {
109 {AMD_ID_F040B, FLASH_AM040},
110 {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
111 };
112
113 const map_entry *p;
114 unsigned long result = FLASH_UNKNOWN;
115
116 /* find chip id */
117 for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
118 if(p->extval == chip)
119 {
120 result = FLASH_VENDMASK | p->intval;
121 break;
122 }
123
124 /* find vendor id */
125 for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
126 if(p->extval == mfct)
127 {
128 result &= ~FLASH_VENDMASK;
129 result |= (unsigned long) p->intval << 16;
130 break;
131 }
132
133 return result;
134}
135
136
wdenkc542fb22002-11-03 00:47:09 +0000137unsigned long
138flash_init(void)
139{
140 unsigned long i;
141 unsigned char j;
142 static const ulong flash_banks[] = CFG_FLASH_BANKS;
143
144 /* Init: no FLASHes known */
145 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
146 {
147 flash_info_t * const pflinfo = &flash_info[i];
148 pflinfo->flash_id = FLASH_UNKNOWN;
149 pflinfo->size = 0;
150 pflinfo->sector_count = 0;
151 }
152
153 for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++)
154 {
155 flash_info_t * const pflinfo = &flash_info[i];
156 const unsigned long base_address = flash_banks[i];
157 volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
158#if 0
159 volatile FLASH_WORD_SIZE * addr2;
160#endif
161#if 0
162 /* write autoselect sequence */
163 flash[0x5555] = 0xaa;
164 flash[0x2aaa] = 0x55;
165 flash[0x5555] = 0x90;
166#else
167 flash[0xAAA << (3 * i)] = 0xaa;
168 flash[0x555 << (3 * i)] = 0x55;
169 flash[0xAAA << (3 * i)] = 0x90;
170#endif
171 __asm__ __volatile__("sync");
172
173#if 0
174 pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
175#else
176 pflinfo->flash_id = flash_id(flash[0x0], flash[0x2 + 14 * i]);
177#endif
178
179 switch(pflinfo->flash_id & FLASH_TYPEMASK)
180 {
181 case FLASH_AM040:
wdenk57b2d802003-06-27 21:31:46 +0000182 pflinfo->size = 0x00080000;
wdenkc542fb22002-11-03 00:47:09 +0000183 pflinfo->sector_count = 8;
wdenk57b2d802003-06-27 21:31:46 +0000184 for(j = 0; j < 8; j++)
wdenkc542fb22002-11-03 00:47:09 +0000185 {
186 pflinfo->start[j] = base_address + 0x00010000 * j;
187 pflinfo->protect[j] = flash[(j << 16) | 0x2];
188 }
189 break;
190 case FLASH_STM800AB:
191 pflinfo->size = 0x00100000;
192 pflinfo->sector_count = 19;
193 pflinfo->start[0] = base_address;
194 pflinfo->start[1] = base_address + 0x4000;
195 pflinfo->start[2] = base_address + 0x6000;
196 pflinfo->start[3] = base_address + 0x8000;
197 for(j = 1; j < 16; j++)
198 {
199 pflinfo->start[j+3] = base_address + 0x00010000 * j;
200 }
201#if 0
wdenk57b2d802003-06-27 21:31:46 +0000202 /* check for protected sectors */
203 for (j = 0; j < pflinfo->sector_count; j++) {
204 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
205 /* D0 = 1 if protected */
206 addr2 = (volatile FLASH_WORD_SIZE *)(pflinfo->start[j]);
207 if (pflinfo->flash_id & FLASH_MAN_SST)
208 pflinfo->protect[j] = 0;
209 else
210 pflinfo->protect[j] = addr2[2] & 1;
211 }
wdenkc542fb22002-11-03 00:47:09 +0000212#endif
213 break;
214 }
215 /* Protect monitor and environment sectors
216 */
217#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
218 flash_protect(FLAG_PROTECT_SET,
219 CFG_MONITOR_BASE,
wdenkb9a83a92003-05-30 12:48:29 +0000220 CFG_MONITOR_BASE + monitor_flash_len - 1,
wdenkc542fb22002-11-03 00:47:09 +0000221 &flash_info[0]);
222#endif
223
224#if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
225 flash_protect(FLAG_PROTECT_SET,
226 CFG_ENV_ADDR,
227 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
228 &flash_info[0]);
229#endif
230
231 /* reset device to read mode */
232 flash[0x0000] = 0xf0;
233 __asm__ __volatile__("sync");
234 }
235
236 return flash_info[0].size + flash_info[1].size;
237}
238
239#if 0
240static void
241flash_get_offsets (ulong base, flash_info_t *info)
242{
243 int i;
244
245 /* set up sector start address table */
wdenk57b2d802003-06-27 21:31:46 +0000246 if (info->flash_id & FLASH_MAN_SST)
247 {
248 for (i = 0; i < info->sector_count; i++)
249 info->start[i] = base + (i * 0x00010000);
250 }
251 else
wdenkc542fb22002-11-03 00:47:09 +0000252 if (info->flash_id & FLASH_BTYPE) {
wdenk57b2d802003-06-27 21:31:46 +0000253 /* set sector offsets for bottom boot block type */
254 info->start[0] = base + 0x00000000;
255 info->start[1] = base + 0x00004000;
256 info->start[2] = base + 0x00006000;
257 info->start[3] = base + 0x00008000;
258 for (i = 4; i < info->sector_count; i++) {
259 info->start[i] = base + (i * 0x00010000) - 0x00030000;
260 }
wdenkc542fb22002-11-03 00:47:09 +0000261 } else {
wdenk57b2d802003-06-27 21:31:46 +0000262 /* set sector offsets for top boot block type */
263 i = info->sector_count - 1;
264 info->start[i--] = base + info->size - 0x00004000;
265 info->start[i--] = base + info->size - 0x00006000;
266 info->start[i--] = base + info->size - 0x00008000;
267 for (; i >= 0; i--) {
268 info->start[i] = base + i * 0x00010000;
269 }
wdenkc542fb22002-11-03 00:47:09 +0000270 }
271
272}
273#endif /* 0 */
274
275/*-----------------------------------------------------------------------
276 */
277void
278flash_print_info(flash_info_t *info)
279{
280 static const char unk[] = "Unknown";
281 const char *mfct = unk, *type = unk;
282 unsigned int i;
283
284 if(info->flash_id != FLASH_UNKNOWN)
285 {
286 switch(info->flash_id & FLASH_VENDMASK)
287 {
288 case FLASH_MAN_AMD: mfct = "AMD"; break;
289 case FLASH_MAN_FUJ: mfct = "FUJITSU"; break;
290 case FLASH_MAN_STM: mfct = "STM"; break;
291 case FLASH_MAN_SST: mfct = "SST"; break;
292 case FLASH_MAN_BM: mfct = "Bright Microelectonics"; break;
293 case FLASH_MAN_INTEL: mfct = "Intel"; break;
294 }
295
296 switch(info->flash_id & FLASH_TYPEMASK)
297 {
298 case FLASH_AM040: type = "AM29F040B (512K * 8, uniform sector size)"; break;
299 case FLASH_AM400B: type = "AM29LV400B (4 Mbit, bottom boot sect)"; break;
300 case FLASH_AM400T: type = "AM29LV400T (4 Mbit, top boot sector)"; break;
301 case FLASH_AM800B: type = "AM29LV800B (8 Mbit, bottom boot sect)"; break;
302 case FLASH_AM800T: type = "AM29LV800T (8 Mbit, top boot sector)"; break;
303 case FLASH_AM160T: type = "AM29LV160T (16 Mbit, top boot sector)"; break;
304 case FLASH_AM320B: type = "AM29LV320B (32 Mbit, bottom boot sect)"; break;
305 case FLASH_AM320T: type = "AM29LV320T (32 Mbit, top boot sector)"; break;
306 case FLASH_STM800AB: type = "M29W800AB (8 Mbit, bottom boot sect)"; break;
307 case FLASH_SST800A: type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; break;
308 case FLASH_SST160A: type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; break;
309 }
310 }
311
312 printf(
313 "\n Brand: %s Type: %s\n"
314 " Size: %lu KB in %d Sectors\n",
315 mfct,
316 type,
317 info->size >> 10,
318 info->sector_count
319 );
320
321 printf (" Sector Start Addresses:");
322
323 for (i = 0; i < info->sector_count; i++)
324 {
325 unsigned long size;
326 unsigned int erased;
327 unsigned long * flash = (unsigned long *) info->start[i];
328
329 /*
330 * Check if whole sector is erased
331 */
332 size =
333 (i != (info->sector_count - 1)) ?
334 (info->start[i + 1] - info->start[i]) >> 2 :
335 (info->start[0] + info->size - info->start[i]) >> 2;
336
337 for(
338 flash = (unsigned long *) info->start[i], erased = 1;
339 (flash != (unsigned long *) info->start[i] + size) && erased;
340 flash++
341 )
342 erased = *flash == ~0x0UL;
343
344 printf(
345 "%s %08lX %s %s",
346 (i % 5) ? "" : "\n ",
347 info->start[i],
348 erased ? "E" : " ",
349 info->protect[i] ? "RO" : " "
350 );
351 }
352
353 puts("\n");
354 return;
355}
356
357#if 0
358
359/*
360 * The following code cannot be run from FLASH!
361 */
362ulong
363flash_get_size (vu_long *addr, flash_info_t *info)
364{
365 short i;
366 FLASH_WORD_SIZE value;
367 ulong base = (ulong)addr;
wdenk57b2d802003-06-27 21:31:46 +0000368 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
wdenkc542fb22002-11-03 00:47:09 +0000369
370 printf("flash_get_size: \n");
371 /* Write auto select command: read Manufacturer ID */
372 eieio();
373 addr2[ADDR0] = (FLASH_WORD_SIZE)0xAA;
374 addr2[ADDR1] = (FLASH_WORD_SIZE)0x55;
375 addr2[ADDR0] = (FLASH_WORD_SIZE)0x90;
376 value = addr2[0];
377
378 switch (value) {
379 case (FLASH_WORD_SIZE)AMD_MANUFACT:
wdenk57b2d802003-06-27 21:31:46 +0000380 info->flash_id = FLASH_MAN_AMD;
381 break;
wdenkc542fb22002-11-03 00:47:09 +0000382 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
wdenk57b2d802003-06-27 21:31:46 +0000383 info->flash_id = FLASH_MAN_FUJ;
384 break;
wdenkc542fb22002-11-03 00:47:09 +0000385 case (FLASH_WORD_SIZE)SST_MANUFACT:
wdenk57b2d802003-06-27 21:31:46 +0000386 info->flash_id = FLASH_MAN_SST;
387 break;
wdenkc542fb22002-11-03 00:47:09 +0000388 default:
wdenk57b2d802003-06-27 21:31:46 +0000389 info->flash_id = FLASH_UNKNOWN;
390 info->sector_count = 0;
391 info->size = 0;
392 return (0); /* no or unknown flash */
wdenkc542fb22002-11-03 00:47:09 +0000393 }
394 printf("recognised manufacturer");
395
396 value = addr2[ADDR3]; /* device ID */
wdenk57b2d802003-06-27 21:31:46 +0000397 debug ("\ndev_code=%x\n", value);
wdenkc542fb22002-11-03 00:47:09 +0000398
399 switch (value) {
400 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
wdenk57b2d802003-06-27 21:31:46 +0000401 info->flash_id += FLASH_AM400T;
402 info->sector_count = 11;
403 info->size = 0x00080000;
404 break; /* => 0.5 MB */
wdenkc542fb22002-11-03 00:47:09 +0000405
406 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
wdenk57b2d802003-06-27 21:31:46 +0000407 info->flash_id += FLASH_AM400B;
408 info->sector_count = 11;
409 info->size = 0x00080000;
410 break; /* => 0.5 MB */
wdenkc542fb22002-11-03 00:47:09 +0000411
412 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
wdenk57b2d802003-06-27 21:31:46 +0000413 info->flash_id += FLASH_AM800T;
414 info->sector_count = 19;
415 info->size = 0x00100000;
416 break; /* => 1 MB */
wdenkc542fb22002-11-03 00:47:09 +0000417
418 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
wdenk57b2d802003-06-27 21:31:46 +0000419 info->flash_id += FLASH_AM800B;
420 info->sector_count = 19;
421 info->size = 0x00100000;
422 break; /* => 1 MB */
wdenkc542fb22002-11-03 00:47:09 +0000423
424 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
wdenk57b2d802003-06-27 21:31:46 +0000425 info->flash_id += FLASH_AM160T;
426 info->sector_count = 35;
427 info->size = 0x00200000;
428 break; /* => 2 MB */
wdenkc542fb22002-11-03 00:47:09 +0000429
430 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
wdenk57b2d802003-06-27 21:31:46 +0000431 info->flash_id += FLASH_AM160B;
432 info->sector_count = 35;
433 info->size = 0x00200000;
434 break; /* => 2 MB */
wdenkc542fb22002-11-03 00:47:09 +0000435
436 case (FLASH_WORD_SIZE)SST_ID_xF800A:
wdenk57b2d802003-06-27 21:31:46 +0000437 info->flash_id += FLASH_SST800A;
438 info->sector_count = 16;
439 info->size = 0x00100000;
440 break; /* => 1 MB */
wdenkc542fb22002-11-03 00:47:09 +0000441
442 case (FLASH_WORD_SIZE)SST_ID_xF160A:
wdenk57b2d802003-06-27 21:31:46 +0000443 info->flash_id += FLASH_SST160A;
444 info->sector_count = 32;
445 info->size = 0x00200000;
446 break; /* => 2 MB */
wdenkc542fb22002-11-03 00:47:09 +0000447
448 case (FLASH_WORD_SIZE)AMD_ID_F040B:
wdenk57b2d802003-06-27 21:31:46 +0000449 info->flash_id += FLASH_AM040;
450 info->sector_count = 8;
451 info->size = 0x00080000;
452 break; /* => 0.5 MB */
wdenkc542fb22002-11-03 00:47:09 +0000453
454 default:
wdenk57b2d802003-06-27 21:31:46 +0000455 info->flash_id = FLASH_UNKNOWN;
456 return (0); /* => no or unknown flash */
wdenkc542fb22002-11-03 00:47:09 +0000457
458 }
459
460 printf("flash id %lx; sector count %x, size %lx\n", info->flash_id,info->sector_count,info->size);
461 /* set up sector start address table */
wdenk57b2d802003-06-27 21:31:46 +0000462 if (info->flash_id & FLASH_MAN_SST)
463 {
464 for (i = 0; i < info->sector_count; i++)
465 info->start[i] = base + (i * 0x00010000);
466 }
467 else
wdenkc542fb22002-11-03 00:47:09 +0000468 if (info->flash_id & FLASH_BTYPE) {
wdenk57b2d802003-06-27 21:31:46 +0000469 /* set sector offsets for bottom boot block type */
470 info->start[0] = base + 0x00000000;
471 info->start[1] = base + 0x00004000;
472 info->start[2] = base + 0x00006000;
473 info->start[3] = base + 0x00008000;
474 for (i = 4; i < info->sector_count; i++) {
475 info->start[i] = base + (i * 0x00010000) - 0x00030000;
476 }
wdenkc542fb22002-11-03 00:47:09 +0000477 } else {
wdenk57b2d802003-06-27 21:31:46 +0000478 /* set sector offsets for top boot block type */
479 i = info->sector_count - 1;
480 info->start[i--] = base + info->size - 0x00004000;
481 info->start[i--] = base + info->size - 0x00006000;
482 info->start[i--] = base + info->size - 0x00008000;
483 for (; i >= 0; i--) {
484 info->start[i] = base + i * 0x00010000;
485 }
wdenkc542fb22002-11-03 00:47:09 +0000486 }
487
488 /* check for protected sectors */
489 for (i = 0; i < info->sector_count; i++) {
wdenk57b2d802003-06-27 21:31:46 +0000490 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
491 /* D0 = 1 if protected */
492 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
493 if (info->flash_id & FLASH_MAN_SST)
494 info->protect[i] = 0;
495 else
496 info->protect[i] = addr2[2] & 1;
wdenkc542fb22002-11-03 00:47:09 +0000497 }
498
499 /*
500 * Prevent writes to uninitialized FLASH.
501 */
502 if (info->flash_id != FLASH_UNKNOWN) {
503 addr2 = (FLASH_WORD_SIZE *)info->start[0];
wdenk57b2d802003-06-27 21:31:46 +0000504 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
wdenkc542fb22002-11-03 00:47:09 +0000505 }
506
507 return (info->size);
508}
509
510#endif
511
512
513int
514flash_erase(flash_info_t *info, int s_first, int s_last)
515{
516 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
517 int flag, prot, sect, l_sect;
518 ulong start, now, last;
519 unsigned char sh8b;
520
521 if ((s_first < 0) || (s_first > s_last)) {
wdenk57b2d802003-06-27 21:31:46 +0000522 if (info->flash_id == FLASH_UNKNOWN) {
523 printf ("- missing\n");
524 } else {
525 printf ("- no sectors to erase\n");
526 }
527 return 1;
wdenkc542fb22002-11-03 00:47:09 +0000528 }
529
530 if ((info->flash_id == FLASH_UNKNOWN) ||
wdenk57b2d802003-06-27 21:31:46 +0000531 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
532 printf ("Can't erase unknown flash type - aborted\n");
533 return 1;
wdenkc542fb22002-11-03 00:47:09 +0000534 }
535
536 prot = 0;
537 for (sect=s_first; sect<=s_last; ++sect) {
wdenk57b2d802003-06-27 21:31:46 +0000538 if (info->protect[sect]) {
539 prot++;
540 }
wdenkc542fb22002-11-03 00:47:09 +0000541 }
542
543 if (prot) {
wdenk57b2d802003-06-27 21:31:46 +0000544 printf ("- Warning: %d protected sectors will not be erased!\n",
545 prot);
wdenkc542fb22002-11-03 00:47:09 +0000546 } else {
wdenk57b2d802003-06-27 21:31:46 +0000547 printf ("\n");
wdenkc542fb22002-11-03 00:47:09 +0000548 }
549
550 l_sect = -1;
551
552 /* Check the ROM CS */
553 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
554 sh8b = 3;
555 else
556 sh8b = 0;
557
558 /* Disable interrupts which might cause a timeout here */
559 flag = disable_interrupts();
560
561 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
562 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
563 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
564 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
565 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
566
567 /* Start erase on unprotected sectors */
568 for (sect = s_first; sect<=s_last; sect++) {
wdenk57b2d802003-06-27 21:31:46 +0000569 if (info->protect[sect] == 0) { /* not protected */
570 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
wdenkc542fb22002-11-03 00:47:09 +0000571 (info->start[sect] - info->start[0]) << sh8b));
wdenk57b2d802003-06-27 21:31:46 +0000572 if (info->flash_id & FLASH_MAN_SST)
573 {
574 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
575 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
576 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
577 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
578 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
579 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
580 udelay(30000); /* wait 30 ms */
581 }
582 else
583 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
584 l_sect = sect;
585 }
wdenkc542fb22002-11-03 00:47:09 +0000586 }
587
588 /* re-enable interrupts if necessary */
589 if (flag)
wdenk57b2d802003-06-27 21:31:46 +0000590 enable_interrupts();
wdenkc542fb22002-11-03 00:47:09 +0000591
592 /* wait at least 80us - let's wait 1 ms */
593 udelay (1000);
594
595 /*
596 * We wait for the last triggered sector
597 */
598 if (l_sect < 0)
wdenk57b2d802003-06-27 21:31:46 +0000599 goto DONE;
wdenkc542fb22002-11-03 00:47:09 +0000600
601 start = get_timer (0);
602 last = start;
603 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
604 (info->start[l_sect] - info->start[0]) << sh8b));
605 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
wdenk57b2d802003-06-27 21:31:46 +0000606 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
607 printf ("Timeout\n");
608 return 1;
609 }
610 /* show that we're waiting */
611 if ((now - last) > 1000) { /* every second */
612 serial_putc ('.');
613 last = now;
614 }
wdenkc542fb22002-11-03 00:47:09 +0000615 }
616
617DONE:
618 /* reset to read mode */
619 addr = (FLASH_WORD_SIZE *)info->start[0];
620 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
621
622 printf (" done\n");
623 return 0;
624}
625
626/*-----------------------------------------------------------------------
627 * Copy memory to flash, returns:
628 * 0 - OK
629 * 1 - write timeout
630 * 2 - Flash not erased
631 */
632
633int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
634{
635 ulong cp, wp, data;
636 int i, l, rc;
637
638 wp = (addr & ~3); /* get lower word aligned address */
639
640 /*
641 * handle unaligned start bytes
642 */
643 if ((l = addr - wp) != 0) {
wdenk57b2d802003-06-27 21:31:46 +0000644 data = 0;
645 for (i=0, cp=wp; i<l; ++i, ++cp) {
646 data = (data << 8) | (*(uchar *)cp);
647 }
648 for (; i<4 && cnt>0; ++i) {
649 data = (data << 8) | *src++;
650 --cnt;
651 ++cp;
652 }
653 for (; cnt==0 && i<4; ++i, ++cp) {
654 data = (data << 8) | (*(uchar *)cp);
655 }
wdenkc542fb22002-11-03 00:47:09 +0000656
wdenk57b2d802003-06-27 21:31:46 +0000657 if ((rc = write_word(info, wp, data)) != 0) {
658 return (rc);
659 }
660 wp += 4;
wdenkc542fb22002-11-03 00:47:09 +0000661 }
662
663 /*
664 * handle word aligned part
665 */
666 while (cnt >= 4) {
wdenk57b2d802003-06-27 21:31:46 +0000667 data = 0;
668 for (i=0; i<4; ++i) {
669 data = (data << 8) | *src++;
670 }
671 if ((rc = write_word(info, wp, data)) != 0) {
672 return (rc);
673 }
674 wp += 4;
675 cnt -= 4;
wdenkc542fb22002-11-03 00:47:09 +0000676 }
677
678 if (cnt == 0) {
wdenk57b2d802003-06-27 21:31:46 +0000679 return (0);
wdenkc542fb22002-11-03 00:47:09 +0000680 }
681
682 /*
683 * handle unaligned tail bytes
684 */
685 data = 0;
686 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
wdenk57b2d802003-06-27 21:31:46 +0000687 data = (data << 8) | *src++;
688 --cnt;
wdenkc542fb22002-11-03 00:47:09 +0000689 }
690 for (; i<4; ++i, ++cp) {
wdenk57b2d802003-06-27 21:31:46 +0000691 data = (data << 8) | (*(uchar *)cp);
wdenkc542fb22002-11-03 00:47:09 +0000692 }
693
694 return (write_word(info, wp, data));
695}
696
697/*-----------------------------------------------------------------------
698 * Write a word to Flash, returns:
699 * 0 - OK
700 * 1 - write timeout
701 * 2 - Flash not erased
702 */
703static int write_word (flash_info_t *info, ulong dest, ulong data)
704{
wdenk57b2d802003-06-27 21:31:46 +0000705 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
706 volatile FLASH_WORD_SIZE *dest2;
707 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
wdenkc542fb22002-11-03 00:47:09 +0000708 ulong start;
709 int flag;
wdenk57b2d802003-06-27 21:31:46 +0000710 int i;
wdenkc542fb22002-11-03 00:47:09 +0000711 unsigned char sh8b;
712
713 /* Check the ROM CS */
714 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
715 sh8b = 3;
716 else
717 sh8b = 0;
718
719 dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
720 info->start[0]);
721
722 /* Check if Flash is (sufficiently) erased */
723 if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
wdenk57b2d802003-06-27 21:31:46 +0000724 return (2);
wdenkc542fb22002-11-03 00:47:09 +0000725 }
726 /* Disable interrupts which might cause a timeout here */
727 flag = disable_interrupts();
728
wdenk57b2d802003-06-27 21:31:46 +0000729 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
730 {
731 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
732 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
733 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
wdenkc542fb22002-11-03 00:47:09 +0000734
wdenk57b2d802003-06-27 21:31:46 +0000735 dest2[i << sh8b] = data2[i];
wdenkc542fb22002-11-03 00:47:09 +0000736
wdenk57b2d802003-06-27 21:31:46 +0000737 /* re-enable interrupts if necessary */
738 if (flag)
739 enable_interrupts();
wdenkc542fb22002-11-03 00:47:09 +0000740
wdenk57b2d802003-06-27 21:31:46 +0000741 /* data polling for D7 */
742 start = get_timer (0);
743 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
744 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
745 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
746 return (1);
747 }
748 }
749 }
wdenkc542fb22002-11-03 00:47:09 +0000750
751 return (0);
752}
753
754/*-----------------------------------------------------------------------
755 */