]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/sandpoint/flash.c
mpc8260: remove atc board support
[people/ms/u-boot.git] / board / sandpoint / flash.c
1 /*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
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
17 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
18
19 #if defined(CONFIG_ENV_IS_IN_FLASH)
20 # ifndef CONFIG_ENV_ADDR
21 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
22 # endif
23 # ifndef CONFIG_ENV_SIZE
24 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
25 # endif
26 # ifndef CONFIG_ENV_SECT_SIZE
27 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
28 # endif
29 #endif
30
31 /*-----------------------------------------------------------------------
32 * Functions
33 */
34 static int write_word (flash_info_t *info, ulong dest, ulong data);
35 #if 0
36 static 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
57 static int byte_parity_odd(unsigned char x) __attribute__ ((const));
58 #endif /* 0 */
59 static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
60
61 typedef struct
62 {
63 FLASH_WORD_SIZE extval;
64 unsigned short intval;
65 } map_entry;
66
67 #if 0
68 static int
69 byte_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
79 static unsigned long
80 flash_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
122 unsigned long
123 flash_init(void)
124 {
125 unsigned long i;
126 unsigned char j;
127 static const ulong flash_banks[] = CONFIG_SYS_FLASH_BANKS;
128
129 /* Init: no FLASHes known */
130 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++)
131 {
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;
141 CONFIG_READ_BYTE(CONFIG_SYS_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR, temp);
142 temp &= ~0x20; /* clear BIOSWP bit */
143 CONFIG_WRITE_BYTE(CONFIG_SYS_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR, temp);
144 }
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:
175 pflinfo->size = 0x00080000;
176 pflinfo->sector_count = 8;
177 for(j = 0; j < 8; j++)
178 {
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
195 /* 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 }
205 #endif
206 break;
207 }
208 /* Protect monitor and environment sectors
209 */
210 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
211 flash_protect(FLAG_PROTECT_SET,
212 CONFIG_SYS_MONITOR_BASE,
213 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
214 &flash_info[0]);
215 #endif
216
217 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
218 flash_protect(FLAG_PROTECT_SET,
219 CONFIG_ENV_ADDR,
220 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
221 &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
233 static void
234 flash_get_offsets (ulong base, flash_info_t *info)
235 {
236 int i;
237
238 /* set up sector start address table */
239 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
245 if (info->flash_id & FLASH_BTYPE) {
246 /* 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 }
254 } else {
255 /* 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 }
263 }
264
265 }
266 #endif /* 0 */
267
268 /*-----------------------------------------------------------------------
269 */
270 void
271 flash_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 */
355 ulong
356 flash_get_size (vu_long *addr, flash_info_t *info)
357 {
358 short i;
359 FLASH_WORD_SIZE value;
360 ulong base = (ulong)addr;
361 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
362
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:
373 info->flash_id = FLASH_MAN_AMD;
374 break;
375 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
376 info->flash_id = FLASH_MAN_FUJ;
377 break;
378 case (FLASH_WORD_SIZE)SST_MANUFACT:
379 info->flash_id = FLASH_MAN_SST;
380 break;
381 default:
382 info->flash_id = FLASH_UNKNOWN;
383 info->sector_count = 0;
384 info->size = 0;
385 return (0); /* no or unknown flash */
386 }
387 printf("recognised manufacturer");
388
389 value = addr2[ADDR3]; /* device ID */
390 debug ("\ndev_code=%x\n", value);
391
392 switch (value) {
393 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
394 info->flash_id += FLASH_AM400T;
395 info->sector_count = 11;
396 info->size = 0x00080000;
397 break; /* => 0.5 MB */
398
399 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
400 info->flash_id += FLASH_AM400B;
401 info->sector_count = 11;
402 info->size = 0x00080000;
403 break; /* => 0.5 MB */
404
405 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
406 info->flash_id += FLASH_AM800T;
407 info->sector_count = 19;
408 info->size = 0x00100000;
409 break; /* => 1 MB */
410
411 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
412 info->flash_id += FLASH_AM800B;
413 info->sector_count = 19;
414 info->size = 0x00100000;
415 break; /* => 1 MB */
416
417 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
418 info->flash_id += FLASH_AM160T;
419 info->sector_count = 35;
420 info->size = 0x00200000;
421 break; /* => 2 MB */
422
423 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
424 info->flash_id += FLASH_AM160B;
425 info->sector_count = 35;
426 info->size = 0x00200000;
427 break; /* => 2 MB */
428
429 case (FLASH_WORD_SIZE)SST_ID_xF800A:
430 info->flash_id += FLASH_SST800A;
431 info->sector_count = 16;
432 info->size = 0x00100000;
433 break; /* => 1 MB */
434
435 case (FLASH_WORD_SIZE)SST_ID_xF160A:
436 info->flash_id += FLASH_SST160A;
437 info->sector_count = 32;
438 info->size = 0x00200000;
439 break; /* => 2 MB */
440
441 case (FLASH_WORD_SIZE)AMD_ID_F040B:
442 info->flash_id += FLASH_AM040;
443 info->sector_count = 8;
444 info->size = 0x00080000;
445 break; /* => 0.5 MB */
446
447 default:
448 info->flash_id = FLASH_UNKNOWN;
449 return (0); /* => no or unknown flash */
450
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 */
455 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
461 if (info->flash_id & FLASH_BTYPE) {
462 /* 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 }
470 } else {
471 /* 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 }
479 }
480
481 /* check for protected sectors */
482 for (i = 0; i < info->sector_count; i++) {
483 /* 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;
490 }
491
492 /*
493 * Prevent writes to uninitialized FLASH.
494 */
495 if (info->flash_id != FLASH_UNKNOWN) {
496 addr2 = (FLASH_WORD_SIZE *)info->start[0];
497 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
498 }
499
500 return (info->size);
501 }
502
503 #endif
504
505
506 int
507 flash_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)) {
515 if (info->flash_id == FLASH_UNKNOWN) {
516 printf ("- missing\n");
517 } else {
518 printf ("- no sectors to erase\n");
519 }
520 return 1;
521 }
522
523 if ((info->flash_id == FLASH_UNKNOWN) ||
524 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
525 printf ("Can't erase unknown flash type - aborted\n");
526 return 1;
527 }
528
529 prot = 0;
530 for (sect=s_first; sect<=s_last; ++sect) {
531 if (info->protect[sect]) {
532 prot++;
533 }
534 }
535
536 if (prot) {
537 printf ("- Warning: %d protected sectors will not be erased!\n",
538 prot);
539 } else {
540 printf ("\n");
541 }
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++) {
562 if (info->protect[sect] == 0) { /* not protected */
563 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
564 (info->start[sect] - info->start[0]) << sh8b));
565 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 }
579 }
580
581 /* re-enable interrupts if necessary */
582 if (flag)
583 enable_interrupts();
584
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)
592 goto DONE;
593
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) {
599 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
600 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 }
608 }
609
610 DONE:
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
626 int 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) {
637 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 }
649
650 if ((rc = write_word(info, wp, data)) != 0) {
651 return (rc);
652 }
653 wp += 4;
654 }
655
656 /*
657 * handle word aligned part
658 */
659 while (cnt >= 4) {
660 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;
669 }
670
671 if (cnt == 0) {
672 return (0);
673 }
674
675 /*
676 * handle unaligned tail bytes
677 */
678 data = 0;
679 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
680 data = (data << 8) | *src++;
681 --cnt;
682 }
683 for (; i<4; ++i, ++cp) {
684 data = (data << 8) | (*(uchar *)cp);
685 }
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 */
696 static int write_word (flash_info_t *info, ulong dest, ulong data)
697 {
698 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;
701 ulong start;
702 int flag;
703 int i;
704 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) {
717 return (2);
718 }
719 /* Disable interrupts which might cause a timeout here */
720 flag = disable_interrupts();
721
722 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;
727
728 dest2[i << sh8b] = data2[i];
729
730 /* re-enable interrupts if necessary */
731 if (flag)
732 enable_interrupts();
733
734 /* 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)) {
738 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
739 return (1);
740 }
741 }
742 }
743
744 return (0);
745 }
746
747 /*-----------------------------------------------------------------------
748 */