blob: fa51c90378bd00f4b25db525d1bd8a4f458d22ed [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +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 <mpc8xx.h>
26
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020027flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000028
29/*-----------------------------------------------------------------------
30 * Functions
31 */
32static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33static int write_word (flash_info_t *info, ulong dest, ulong data);
34static void flash_get_offsets (ulong base, flash_info_t *info);
35
36/*-----------------------------------------------------------------------
37 */
38
39unsigned long flash_init (void)
40{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020041 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
wdenkaffae2b2002-08-17 09:36:01 +000042 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size_b0, size_b1;
44 int i;
45
46 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +000048 flash_info[i].flash_id = FLASH_UNKNOWN;
49 }
50
51 /* Static FLASH Bank configuration here - FIXME XXX */
52
53 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
54
55 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
56 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
57 size_b0, size_b0<<20);
58 }
59
60 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
61
62 if (size_b1 > size_b0) {
63 printf ("## ERROR: "
64 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
65 size_b1, size_b1<<20,
66 size_b0, size_b0<<20
67 );
68 flash_info[0].flash_id = FLASH_UNKNOWN;
69 flash_info[1].flash_id = FLASH_UNKNOWN;
70 flash_info[0].sector_count = -1;
71 flash_info[1].sector_count = -1;
72 flash_info[0].size = 0;
73 flash_info[1].size = 0;
74 return (0);
75 }
76
77 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020078 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
wdenkaffae2b2002-08-17 09:36:01 +000079#ifdef CONFIG_FLASH_16BIT
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020080 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */
wdenkaffae2b2002-08-17 09:36:01 +000081#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020082 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
wdenkaffae2b2002-08-17 09:36:01 +000083#endif
84
85 /* Re-do sizing to get full correct info */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000087
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020088 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +000089
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020090#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +000091 /* monitor protection ON by default */
92 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020093 CONFIG_SYS_MONITOR_BASE,
94 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +000095 &flash_info[0]);
96#endif
97
98 if (size_b1) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
wdenkaffae2b2002-08-17 09:36:01 +0000100#ifdef CONFIG_FLASH_16BIT
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200101 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
wdenkaffae2b2002-08-17 09:36:01 +0000102 BR_MS_GPCM | BR_V | BR_PS_16;
103#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200104 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
wdenkaffae2b2002-08-17 09:36:01 +0000105 BR_MS_GPCM | BR_V;
106#endif
107
108 /* Re-do sizing to get full correct info */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200109 size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE + size_b0),
wdenkaffae2b2002-08-17 09:36:01 +0000110 &flash_info[1]);
111
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200112 flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
wdenkaffae2b2002-08-17 09:36:01 +0000113
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200114#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenkaffae2b2002-08-17 09:36:01 +0000115 /* monitor protection ON by default */
116 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200117 CONFIG_SYS_MONITOR_BASE,
118 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +0000119 &flash_info[1]);
120#endif
121 } else {
122 memctl->memc_br1 = 0; /* invalidate bank */
123
124 flash_info[1].flash_id = FLASH_UNKNOWN;
125 flash_info[1].sector_count = -1;
126 }
127
128 flash_info[0].size = size_b0;
129 flash_info[1].size = size_b1;
130
131 return (size_b0 + size_b1);
132}
133
134/*-----------------------------------------------------------------------
135 */
136static void flash_get_offsets (ulong base, flash_info_t *info)
137{
138 int i;
139
140 if (info->flash_id == FLASH_UNKNOWN) {
141 return;
142 }
143
144 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
145 for (i = 0; i < info->sector_count; i++) {
146 info->start[i] = base + (i * 0x00002000);
147 }
148 return;
149 }
150
151 /* set up sector start address table */
152 if (info->flash_id & FLASH_BTYPE) {
153 /* set sector offsets for bottom boot block type */
154#ifdef CONFIG_FLASH_16BIT
155 info->start[0] = base + 0x00000000;
156 info->start[1] = base + 0x00004000;
157 info->start[2] = base + 0x00006000;
158 info->start[3] = base + 0x00008000;
159 for (i = 4; i < info->sector_count; i++) {
160 info->start[i] = base + (i * 0x00010000) - 0x00030000;
161#else
162 info->start[0] = base + 0x00000000;
163 info->start[1] = base + 0x00008000;
164 info->start[2] = base + 0x0000C000;
165 info->start[3] = base + 0x00010000;
166 for (i = 4; i < info->sector_count; i++) {
167 info->start[i] = base + (i * 0x00020000) - 0x00060000;
168#endif
169 }
170 } else {
171 /* set sector offsets for top boot block type */
172 i = info->sector_count - 1;
173 info->start[i--] = base + info->size - 0x00008000;
174 info->start[i--] = base + info->size - 0x0000C000;
175 info->start[i--] = base + info->size - 0x00010000;
176 for (; i >= 0; i--) {
177 info->start[i] = base + i * 0x00020000;
178 }
179 }
180
181}
182
183/*-----------------------------------------------------------------------
184 */
185void flash_print_info (flash_info_t *info)
186{
187 int i;
188
189 if (info->flash_id == FLASH_UNKNOWN) {
190 printf ("missing or unknown FLASH type\n");
191 return;
192 }
193
194 switch (info->flash_id & FLASH_VENDMASK) {
195 case FLASH_MAN_AMD: printf ("AMD "); break;
196 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
197 case FLASH_MAN_SST: printf ("SST "); break;
198 case FLASH_MAN_STM: printf ("STM "); break;
199 default: printf ("Unknown Vendor "); break;
200 }
201
202 switch (info->flash_id & FLASH_TYPEMASK) {
203 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
204 break;
205 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
206 break;
207 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
208 break;
209 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
210 break;
211 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
212 break;
213 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
214 break;
215 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
216 break;
217 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
218 break;
219 case FLASH_SST200A: printf ("39xF200A (2M = 128K x 16)\n");
220 break;
221 case FLASH_SST400A: printf ("39xF400A (4M = 256K x 16)\n");
222 break;
223 case FLASH_SST800A: printf ("39xF800A (8M = 512K x 16)\n");
224 break;
225 case FLASH_STM800AB: printf ("M29W800AB (8M = 512K x 16)\n");
226 break;
227 default: printf ("Unknown Chip Type\n");
228 break;
229 }
230
231 printf (" Size: %ld MB in %d Sectors\n",
232 info->size >> 20, info->sector_count);
233
234 printf (" Sector Start Addresses:");
235 for (i=0; i<info->sector_count; ++i) {
236 if ((i % 5) == 0)
237 printf ("\n ");
238 printf (" %08lX%s",
239 info->start[i],
240 info->protect[i] ? " (RO)" : " "
241 );
242 }
243 printf ("\n");
244 return;
245}
246
247/*-----------------------------------------------------------------------
248 */
249
250
251/*-----------------------------------------------------------------------
252 */
253
254/*
255 * The following code cannot be run from FLASH!
256 */
257
258static ulong flash_get_size (vu_long *addr, flash_info_t *info)
259{
260 short i;
261 ulong value;
262 ulong base = (ulong)addr;
263
264 /* Write auto select command: read Manufacturer ID */
265#ifdef CONFIG_FLASH_16BIT
266 vu_short *s_addr = (vu_short*)addr;
267 s_addr[0x5555] = 0x00AA;
268 s_addr[0x2AAA] = 0x0055;
269 s_addr[0x5555] = 0x0090;
270 value = s_addr[0];
271 value = value|(value<<16);
272#else
273 addr[0x5555] = 0x00AA00AA;
274 addr[0x2AAA] = 0x00550055;
275 addr[0x5555] = 0x00900090;
276 value = addr[0];
277#endif
278
279 switch (value) {
280 case AMD_MANUFACT:
281 info->flash_id = FLASH_MAN_AMD;
282 break;
283 case FUJ_MANUFACT:
284 info->flash_id = FLASH_MAN_FUJ;
285 break;
286 case SST_MANUFACT:
287 info->flash_id = FLASH_MAN_SST;
288 break;
289 case STM_MANUFACT:
290 info->flash_id = FLASH_MAN_STM;
291 break;
292 default:
293 info->flash_id = FLASH_UNKNOWN;
294 info->sector_count = 0;
295 info->size = 0;
296 return (0); /* no or unknown flash */
297 }
298#ifdef CONFIG_FLASH_16BIT
299 value = s_addr[1];
300 value = value|(value<<16);
301#else
302 value = addr[1]; /* device ID */
303#endif
304
305 switch (value) {
306 case AMD_ID_LV400T:
307 info->flash_id += FLASH_AM400T;
308 info->sector_count = 11;
309 info->size = 0x00100000;
310 break; /* => 1 MB */
311
312 case AMD_ID_LV400B:
313 info->flash_id += FLASH_AM400B;
314 info->sector_count = 11;
315 info->size = 0x00100000;
316 break; /* => 1 MB */
317
318 case AMD_ID_LV800T:
319 info->flash_id += FLASH_AM800T;
320 info->sector_count = 19;
321 info->size = 0x00200000;
322 break; /* => 2 MB */
323
324 case AMD_ID_LV800B:
325 info->flash_id += FLASH_AM800B;
326#ifdef CONFIG_FLASH_16BIT
327 info->sector_count = 19;
328 info->size = 0x00100000; /* => 1 MB */
329#else
330 info->sector_count = 19;
331 info->size = 0x00200000; /* => 2 MB */
332#endif
333 break;
334
335 case AMD_ID_LV160T:
336 info->flash_id += FLASH_AM160T;
337 info->sector_count = 35;
338 info->size = 0x00400000;
339 break; /* => 4 MB */
340
341 case AMD_ID_LV160B:
342 info->flash_id += FLASH_AM160B;
343#ifdef CONFIG_FLASH_16BIT
344 info->sector_count = 35;
345 info->size = 0x00200000; /* => 2 MB */
346#else
347 info->sector_count = 35;
348 info->size = 0x00400000; /* => 4 MB */
349#endif
350
351 break;
352#if 0 /* enable when device IDs are available */
353 case AMD_ID_LV320T:
354 info->flash_id += FLASH_AM320T;
355 info->sector_count = 67;
356 info->size = 0x00800000;
357 break; /* => 8 MB */
358
359 case AMD_ID_LV320B:
360 info->flash_id += FLASH_AM320B;
361 info->sector_count = 67;
362 info->size = 0x00800000;
363 break; /* => 8 MB */
364#endif
365 case SST_ID_xF200A:
366 info->flash_id += FLASH_SST200A;
367 info->sector_count = 64; /* 39xF200A ID ( 2M = 128K x 16 ) */
368 info->size = 0x00080000;
369 break;
370 case SST_ID_xF400A:
371 info->flash_id += FLASH_SST400A;
372 info->sector_count = 128; /* 39xF400A ID ( 4M = 256K x 16 ) */
373 info->size = 0x00100000;
374 break;
375 case SST_ID_xF800A:
376 info->flash_id += FLASH_SST800A;
377 info->sector_count = 256; /* 39xF800A ID ( 8M = 512K x 16 ) */
378 info->size = 0x00200000;
379 break; /* => 2 MB */
380 case STM_ID_x800AB:
381 info->flash_id += FLASH_STM800AB;
382 info->sector_count = 19;
383 info->size = 0x00200000;
384 break; /* => 2 MB */
385 default:
386 info->flash_id = FLASH_UNKNOWN;
387 return (0); /* => no or unknown flash */
388
389 }
390
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200391 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
wdenkaffae2b2002-08-17 09:36:01 +0000392 printf ("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200393 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
394 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenkaffae2b2002-08-17 09:36:01 +0000395 }
396
397 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
398 for (i = 0; i < info->sector_count; i++) {
399 info->start[i] = base + (i * 0x00002000);
400 }
401 } else { /* AMD and Fujitsu types */
402 /* set up sector start address table */
403 if (info->flash_id & FLASH_BTYPE) {
404 /* set sector offsets for bottom boot block type */
405#ifdef CONFIG_FLASH_16BIT
406
407 info->start[0] = base + 0x00000000;
408 info->start[1] = base + 0x00004000;
409 info->start[2] = base + 0x00006000;
410 info->start[3] = base + 0x00008000;
411 for (i = 4; i < info->sector_count; i++) {
412 info->start[i] = base + (i * 0x00010000) - 0x00030000;
413#else
414 info->start[0] = base + 0x00000000;
415 info->start[1] = base + 0x00008000;
416 info->start[2] = base + 0x0000C000;
417 info->start[3] = base + 0x00010000;
418 for (i = 4; i < info->sector_count; i++) {
419 info->start[i] = base + (i * 0x00020000) - 0x00060000;
420#endif
421 }
422 } else {
423 /* set sector offsets for top boot block type */
424 i = info->sector_count - 1;
425 info->start[i--] = base + info->size - 0x00008000;
426 info->start[i--] = base + info->size - 0x0000C000;
427 info->start[i--] = base + info->size - 0x00010000;
428 for (; i >= 0; i--) {
429 info->start[i] = base + i * 0x00020000;
430 }
431 }
432
433 /* check for protected sectors */
434 for (i = 0; i < info->sector_count; i++) {
435 /* read sector protection at sector address:
436 * (A7 .. A0) = 0x02
437 * D0 = 1 if protected
438 */
439#ifdef CONFIG_FLASH_16BIT
440 s_addr = (volatile unsigned short *)(info->start[i]);
441 info->protect[i] = s_addr[2] & 1;
442#else
443 addr = (volatile unsigned long *)(info->start[i]);
444 info->protect[i] = addr[2] & 1;
445#endif
446 }
447 }
448
449 /*
450 * Prevent writes to uninitialized FLASH.
451 */
452 if (info->flash_id != FLASH_UNKNOWN) {
453#ifdef CONFIG_FLASH_16BIT
454 s_addr = (volatile unsigned short *)(info->start[0]);
455 *s_addr = 0x00F0; /* reset bank */
456#else
457 addr = (volatile unsigned long *)info->start[0];
458 *addr = 0x00F000F0; /* reset bank */
459#endif
460
461 }
462 return (info->size);
463}
464
465
466/*-----------------------------------------------------------------------
467 */
468
469int flash_erase (flash_info_t *info, int s_first, int s_last)
470{
471 vu_long *addr = (vu_long*)(info->start[0]);
472 int flag, prot, sect;
473 ulong start, now, last;
474#ifdef CONFIG_FLASH_16BIT
475 vu_short *s_addr = (vu_short*)addr;
476#endif
477
478 if ((s_first < 0) || (s_first > s_last)) {
479 if (info->flash_id == FLASH_UNKNOWN) {
480 printf ("- missing\n");
481 } else {
482 printf ("- no sectors to erase\n");
483 }
484 return 1;
485 }
486/*#ifndef CONFIG_FLASH_16BIT
487 ulong type;
488 type = (info->flash_id & FLASH_VENDMASK);
489 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
490 printf ("Can't erase unknown flash type %08lx - aborted\n",
491 info->flash_id);
492 return;
493 }
494#endif*/
495 prot = 0;
496 for (sect=s_first; sect<=s_last; ++sect) {
497 if (info->protect[sect]) {
498 prot++;
499 }
500 }
501
502 if (prot) {
503 printf ("- Warning: %d protected sectors will not be erased!\n",
504 prot);
505 } else {
506 printf ("\n");
507 }
508
509 start = get_timer (0);
510 last = start;
511 /* Start erase on unprotected sectors */
512 for (sect = s_first; sect<=s_last; sect++) {
513 if (info->protect[sect] == 0) { /* not protected */
514#ifdef CONFIG_FLASH_16BIT
515 vu_short *s_sect_addr = (vu_short*)(info->start[sect]);
516#else
517 vu_long *sect_addr = (vu_long*)(info->start[sect]);
518#endif
519 /* Disable interrupts which might cause a timeout here */
520 flag = disable_interrupts();
521
522#ifdef CONFIG_FLASH_16BIT
523
524 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
525 s_addr[0x5555] = 0x00AA;
526 s_addr[0x2AAA] = 0x0055;
527 s_addr[0x5555] = 0x0080;
528 s_addr[0x5555] = 0x00AA;
529 s_addr[0x2AAA] = 0x0055;
530 s_sect_addr[0] = 0x0030;
531#else
532 addr[0x5555] = 0x00AA00AA;
533 addr[0x2AAA] = 0x00550055;
534 addr[0x5555] = 0x00800080;
535 addr[0x5555] = 0x00AA00AA;
536 addr[0x2AAA] = 0x00550055;
537 sect_addr[0] = 0x00300030;
538#endif
539 /* re-enable interrupts if necessary */
540 if (flag)
541 enable_interrupts();
542
543 /* wait at least 80us - let's wait 1 ms */
544 udelay (1000);
545
546#ifdef CONFIG_FLASH_16BIT
547 while ((s_sect_addr[0] & 0x0080) != 0x0080) {
548#else
549 while ((sect_addr[0] & 0x00800080) != 0x00800080) {
550#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200551 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000552 printf ("Timeout\n");
553 return 1;
554 }
555 /* show that we're waiting */
556 if ((now - last) > 1000) { /* every second */
557 putc ('.');
558 last = now;
559 }
560 }
561 }
562 }
563
564 /* reset to read mode */
565 addr = (volatile unsigned long *)info->start[0];
566#ifdef CONFIG_FLASH_16BIT
567 s_addr[0] = 0x00F0; /* reset bank */
568#else
569 addr[0] = 0x00F000F0; /* reset bank */
570#endif
571
572 printf (" done\n");
573 return 0;
574}
575
576/*-----------------------------------------------------------------------
577 * Copy memory to flash, returns:
578 * 0 - OK
579 * 1 - write timeout
580 * 2 - Flash not erased
581 * 4 - Flash not identified
582 */
583
584int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
585{
586 ulong cp, wp, data;
587 int i, l, rc;
588
589 if (info->flash_id == FLASH_UNKNOWN) {
590 return 4;
591 }
592
593 wp = (addr & ~3); /* get lower word aligned address */
594
595 /*
596 * handle unaligned start bytes
597 */
598 if ((l = addr - wp) != 0) {
599 data = 0;
600 for (i=0, cp=wp; i<l; ++i, ++cp) {
601 data = (data << 8) | (*(uchar *)cp);
602 }
603 for (; i<4 && cnt>0; ++i) {
604 data = (data << 8) | *src++;
605 --cnt;
606 ++cp;
607 }
608 for (; cnt==0 && i<4; ++i, ++cp) {
609 data = (data << 8) | (*(uchar *)cp);
610 }
611
612 if ((rc = write_word(info, wp, data)) != 0) {
613 return (rc);
614 }
615 wp += 4;
616 }
617
618 /*
619 * handle word aligned part
620 */
621 while (cnt >= 4) {
622 data = 0;
623 for (i=0; i<4; ++i) {
624 data = (data << 8) | *src++;
625 }
626 if ((rc = write_word(info, wp, data)) != 0) {
627 return (rc);
628 }
629 wp += 4;
630 cnt -= 4;
631 }
632
633 if (cnt == 0) {
634 return (0);
635 }
636
637 /*
638 * handle unaligned tail bytes
639 */
640 data = 0;
641 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
642 data = (data << 8) | *src++;
643 --cnt;
644 }
645 for (; i<4; ++i, ++cp) {
646 data = (data << 8) | (*(uchar *)cp);
647 }
648
649 return (write_word(info, wp, data));
650}
651
652/*-----------------------------------------------------------------------
653 * Write a word to Flash, returns:
654 * 0 - OK
655 * 1 - write timeout
656 * 2 - Flash not erased
657 */
658static int write_word (flash_info_t *info, ulong dest, ulong data)
659{
660 vu_long *addr = (vu_long*)(info->start[0]);
661
662#ifdef CONFIG_FLASH_16BIT
663 vu_short high_data;
664 vu_short low_data;
665 vu_short *s_addr = (vu_short*)addr;
666#endif
667 ulong start;
668 int flag;
669
670 /* Check if Flash is (sufficiently) erased */
671 if ((*((vu_long *)dest) & data) != data) {
672 return (2);
673 }
674
675#ifdef CONFIG_FLASH_16BIT
676 /* Write the 16 higher-bits */
677 /* Disable interrupts which might cause a timeout here */
678 flag = disable_interrupts();
679
680 high_data = ((data>>16) & 0x0000ffff);
681
682 s_addr[0x5555] = 0x00AA;
683 s_addr[0x2AAA] = 0x0055;
684 s_addr[0x5555] = 0x00A0;
685
686 *((vu_short *)dest) = high_data;
687
688
689 /* re-enable interrupts if necessary */
690 if (flag)
691 enable_interrupts();
692
693 /* data polling for D7 */
694 start = get_timer (0);
695 while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200696 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000697 return (1);
698 }
699 }
700
701
702 /* Write the 16 lower-bits */
703#endif
704
705 /* Disable interrupts which might cause a timeout here */
706 flag = disable_interrupts();
707#ifdef CONFIG_FLASH_16BIT
708 dest += 0x2;
709 low_data = (data & 0x0000ffff);
710
711 s_addr[0x5555] = 0x00AA;
712 s_addr[0x2AAA] = 0x0055;
713 s_addr[0x5555] = 0x00A0;
714 *((vu_short *)dest) = low_data;
715
716#else
717 addr[0x5555] = 0x00AA00AA;
718 addr[0x2AAA] = 0x00550055;
719 addr[0x5555] = 0x00A000A0;
720 *((vu_long *)dest) = data;
721#endif
722
723 /* re-enable interrupts if necessary */
724 if (flag)
725 enable_interrupts();
726
727 /* data polling for D7 */
728 start = get_timer (0);
729
730#ifdef CONFIG_FLASH_16BIT
731 while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
732#else
733 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
734#endif
735
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200736 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000737 return (1);
738 }
739 }
740 return (0);
741}
742
743/*-----------------------------------------------------------------------
744 */