blob: 7cda3a412ff60601a46cf5766085a204f82d61c6 [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
wdenk99edcfb2004-06-09 21:54:22 +000027flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000028
29#if defined(CFG_ENV_IS_IN_FLASH)
30# ifndef CFG_ENV_ADDR
31# define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
32# endif
33# ifndef CFG_ENV_SIZE
34# define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
35# endif
36# ifndef CFG_ENV_SECT_SIZE
37# define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
38# endif
39#endif
40
wdenk99edcfb2004-06-09 21:54:22 +000041#define QUAD_ID(id) ((((ulong)(id) & 0xFF) << 24) | \
42 (((ulong)(id) & 0xFF) << 16) | \
43 (((ulong)(id) & 0xFF) << 8) | \
44 (((ulong)(id) & 0xFF) << 0) \
45 )
46
wdenkaffae2b2002-08-17 09:36:01 +000047/*-----------------------------------------------------------------------
48 * Functions
49 */
wdenkbf9e3b32004-02-12 00:47:09 +000050static ulong flash_get_size (vu_long * addr, flash_info_t * info);
51static int write_word (flash_info_t * info, ulong dest, ulong data);
wdenkaffae2b2002-08-17 09:36:01 +000052
53/*-----------------------------------------------------------------------
54 */
wdenkaffae2b2002-08-17 09:36:01 +000055unsigned long flash_init (void)
56{
wdenkbf9e3b32004-02-12 00:47:09 +000057 volatile immap_t *immap = (immap_t *) CFG_IMMR;
wdenkaffae2b2002-08-17 09:36:01 +000058 volatile memctl8xx_t *memctl = &immap->im_memctl;
wdenk99edcfb2004-06-09 21:54:22 +000059 vu_long *bcsr = (vu_long *)BCSR_ADDR;
60 unsigned long pd_size, total_size, bsize, or_am;
wdenkaffae2b2002-08-17 09:36:01 +000061 int i;
62
63 /* Init: no FLASHes known */
wdenkbf9e3b32004-02-12 00:47:09 +000064 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +000065 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk99edcfb2004-06-09 21:54:22 +000066 flash_info[i].size = 0;
67 flash_info[i].sector_count = 0;
68 flash_info[i].start[0] = 0xFFFFFFFF; /* For TFTP */
69 }
70
71 switch ((bcsr[2] & BCSR2_FLASH_PD_MASK) >> BCSR2_FLASH_PD_SHIFT) {
72 case 2:
73 case 4:
74 case 6:
75 pd_size = 0x800000;
76 or_am = 0xFF800000;
77 break;
78
79 case 5:
80 case 7:
81 pd_size = 0x400000;
82 or_am = 0xFFC00000;
83 break;
84
85 case 8:
86 pd_size = 0x200000;
87 or_am = 0xFFE00000;
88 break;
89
90 default:
91 pd_size = 0;
92 or_am = 0xFFE00000;
Wolfgang Denk9b55a252008-07-11 01:16:00 +020093 printf("## Unsupported flash detected by BCSR: 0x%08lX\n", bcsr[2]);
wdenkaffae2b2002-08-17 09:36:01 +000094 }
95
96 total_size = 0;
wdenk99edcfb2004-06-09 21:54:22 +000097 for (i = 0; i < CFG_MAX_FLASH_BANKS && total_size < pd_size; ++i) {
98 bsize = flash_get_size((vu_long *)(CFG_FLASH_BASE + total_size),
99 &flash_info[i]);
wdenkaffae2b2002-08-17 09:36:01 +0000100
wdenkbf9e3b32004-02-12 00:47:09 +0000101 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
wdenk99edcfb2004-06-09 21:54:22 +0000102 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
103 i, bsize, bsize >> 20);
wdenkaffae2b2002-08-17 09:36:01 +0000104 }
105
wdenk99edcfb2004-06-09 21:54:22 +0000106 total_size += bsize;
wdenkaffae2b2002-08-17 09:36:01 +0000107 }
108
wdenk99edcfb2004-06-09 21:54:22 +0000109 if (total_size != pd_size) {
110 printf("## Detected flash size %lu conflicts with PD data %lu\n",
111 total_size, pd_size);
wdenkaffae2b2002-08-17 09:36:01 +0000112 }
113
114 /* Remap FLASH according to real size */
wdenk99edcfb2004-06-09 21:54:22 +0000115 memctl->memc_or0 = or_am | CFG_OR_TIMING_FLASH;
wdenkaffae2b2002-08-17 09:36:01 +0000116
wdenkbf9e3b32004-02-12 00:47:09 +0000117 for (i = 0; i < CFG_MAX_FLASH_BANKS && flash_info[i].size != 0; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000118#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
119 /* monitor protection ON by default */
wdenk99edcfb2004-06-09 21:54:22 +0000120 if (CFG_MONITOR_BASE >= flash_info[i].start[0])
121 flash_protect (FLAG_PROTECT_SET,
122 CFG_MONITOR_BASE,
123 CFG_MONITOR_BASE + monitor_flash_len - 1,
124 &flash_info[i]);
wdenkaffae2b2002-08-17 09:36:01 +0000125#endif
126
127#ifdef CFG_ENV_IS_IN_FLASH
128 /* ENV protection ON by default */
wdenk99edcfb2004-06-09 21:54:22 +0000129 if (CFG_ENV_ADDR >= flash_info[i].start[0])
130 flash_protect (FLAG_PROTECT_SET,
131 CFG_ENV_ADDR,
132 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
133 &flash_info[i]);
wdenkaffae2b2002-08-17 09:36:01 +0000134#endif
wdenkaffae2b2002-08-17 09:36:01 +0000135 }
136
wdenk99edcfb2004-06-09 21:54:22 +0000137 return total_size;
wdenkaffae2b2002-08-17 09:36:01 +0000138}
139
140/*-----------------------------------------------------------------------
141 */
wdenkbf9e3b32004-02-12 00:47:09 +0000142void flash_print_info (flash_info_t * info)
wdenkaffae2b2002-08-17 09:36:01 +0000143{
144 int i;
145
wdenkbf9e3b32004-02-12 00:47:09 +0000146 if (info->flash_id == FLASH_UNKNOWN) {
wdenkaffae2b2002-08-17 09:36:01 +0000147 printf ("missing or unknown FLASH type\n");
148 return;
149 }
150
wdenkbf9e3b32004-02-12 00:47:09 +0000151 switch (info->flash_id & FLASH_VENDMASK) {
152 case FLASH_MAN_AMD:
153 printf ("AMD ");
154 break;
155 case FLASH_MAN_FUJ:
156 printf ("FUJITSU ");
157 break;
158 case FLASH_MAN_BM:
159 printf ("BRIGHT MICRO ");
160 break;
161 default:
162 printf ("Unknown Vendor ");
163 break;
wdenkaffae2b2002-08-17 09:36:01 +0000164 }
165
wdenkbf9e3b32004-02-12 00:47:09 +0000166 switch (info->flash_id & FLASH_TYPEMASK) {
167 case FLASH_AM040:
168 printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
169 break;
170 case FLASH_AM080:
171 printf ("29F080 or 29LV080 (8 Mbit, uniform sectors)\n");
172 break;
173 case FLASH_AM400B:
174 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
175 break;
176 case FLASH_AM400T:
177 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
178 break;
179 case FLASH_AM800B:
180 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
181 break;
182 case FLASH_AM800T:
183 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
184 break;
185 case FLASH_AM160B:
186 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
187 break;
188 case FLASH_AM160T:
189 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
190 break;
191 case FLASH_AM320B:
192 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
193 break;
194 case FLASH_AM320T:
195 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
196 break;
197 default:
198 printf ("Unknown Chip Type\n");
199 break;
wdenkaffae2b2002-08-17 09:36:01 +0000200 }
201
wdenkbf9e3b32004-02-12 00:47:09 +0000202 printf (" Size: %ld MB in %d Sectors\n", info->size >> 20,
203 info->sector_count);
wdenkaffae2b2002-08-17 09:36:01 +0000204
205 printf (" Sector Start Addresses:");
206
wdenkbf9e3b32004-02-12 00:47:09 +0000207 for (i = 0; i < info->sector_count; ++i) {
208 if ((i % 5) == 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000209 printf ("\n ");
210 }
211
212 printf (" %08lX%s",
wdenkbf9e3b32004-02-12 00:47:09 +0000213 info->start[i], info->protect[i] ? " (RO)" : " ");
wdenkaffae2b2002-08-17 09:36:01 +0000214 }
215
216 printf ("\n");
wdenkaffae2b2002-08-17 09:36:01 +0000217}
218
219/*-----------------------------------------------------------------------
wdenk99edcfb2004-06-09 21:54:22 +0000220 * The following code can not run from flash!
wdenkaffae2b2002-08-17 09:36:01 +0000221 */
wdenkbf9e3b32004-02-12 00:47:09 +0000222static ulong flash_get_size (vu_long * addr, flash_info_t * info)
wdenkaffae2b2002-08-17 09:36:01 +0000223{
224 short i;
wdenkbf9e3b32004-02-12 00:47:09 +0000225
wdenkaffae2b2002-08-17 09:36:01 +0000226 /* Write auto select command: read Manufacturer ID */
wdenkaffae2b2002-08-17 09:36:01 +0000227 addr[0x0555] = 0xAAAAAAAA;
228 addr[0x02AA] = 0x55555555;
229 addr[0x0555] = 0x90909090;
wdenkaffae2b2002-08-17 09:36:01 +0000230
wdenk99edcfb2004-06-09 21:54:22 +0000231 switch (addr[0]) {
232 case QUAD_ID(AMD_MANUFACT):
wdenkbf9e3b32004-02-12 00:47:09 +0000233 info->flash_id = FLASH_MAN_AMD;
234 break;
wdenkaffae2b2002-08-17 09:36:01 +0000235
wdenk99edcfb2004-06-09 21:54:22 +0000236 case QUAD_ID(FUJ_MANUFACT):
wdenkbf9e3b32004-02-12 00:47:09 +0000237 info->flash_id = FLASH_MAN_FUJ;
238 break;
wdenkaffae2b2002-08-17 09:36:01 +0000239
wdenkbf9e3b32004-02-12 00:47:09 +0000240 default:
241 info->flash_id = FLASH_UNKNOWN;
242 info->sector_count = 0;
243 info->size = 0;
244 break;
wdenkaffae2b2002-08-17 09:36:01 +0000245 }
246
wdenk99edcfb2004-06-09 21:54:22 +0000247 switch (addr[1]) { /* device ID */
248 case QUAD_ID(AMD_ID_F040B):
249 case QUAD_ID(AMD_ID_LV040B):
wdenkbf9e3b32004-02-12 00:47:09 +0000250 info->flash_id += FLASH_AM040;
251 info->sector_count = 8;
252 info->size = 0x00200000;
253 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000254
wdenk99edcfb2004-06-09 21:54:22 +0000255 case QUAD_ID(AMD_ID_F080B):
wdenkbf9e3b32004-02-12 00:47:09 +0000256 info->flash_id += FLASH_AM080;
257 info->sector_count = 16;
258 info->size = 0x00400000;
259 break; /* => 4 MB */
wdenk99edcfb2004-06-09 21:54:22 +0000260#if 0
wdenkbf9e3b32004-02-12 00:47:09 +0000261 case AMD_ID_LV400T:
262 info->flash_id += FLASH_AM400T;
263 info->sector_count = 11;
264 info->size = 0x00100000;
265 break; /* => 1 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000266
wdenkbf9e3b32004-02-12 00:47:09 +0000267 case AMD_ID_LV400B:
268 info->flash_id += FLASH_AM400B;
269 info->sector_count = 11;
270 info->size = 0x00100000;
271 break; /* => 1 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000272
wdenkbf9e3b32004-02-12 00:47:09 +0000273 case AMD_ID_LV800T:
274 info->flash_id += FLASH_AM800T;
275 info->sector_count = 19;
276 info->size = 0x00200000;
277 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000278
wdenkbf9e3b32004-02-12 00:47:09 +0000279 case AMD_ID_LV800B:
280 info->flash_id += FLASH_AM800B;
281 info->sector_count = 19;
282 info->size = 0x00200000;
283 break; /* => 2 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000284
wdenkbf9e3b32004-02-12 00:47:09 +0000285 case AMD_ID_LV160T:
286 info->flash_id += FLASH_AM160T;
287 info->sector_count = 35;
288 info->size = 0x00400000;
289 break; /* => 4 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000290
wdenkbf9e3b32004-02-12 00:47:09 +0000291 case AMD_ID_LV160B:
292 info->flash_id += FLASH_AM160B;
293 info->sector_count = 35;
294 info->size = 0x00400000;
295 break; /* => 4 MB */
wdenk99edcfb2004-06-09 21:54:22 +0000296
wdenkbf9e3b32004-02-12 00:47:09 +0000297 case AMD_ID_LV320T:
298 info->flash_id += FLASH_AM320T;
299 info->sector_count = 67;
300 info->size = 0x00800000;
301 break; /* => 8 MB */
wdenkaffae2b2002-08-17 09:36:01 +0000302
wdenkbf9e3b32004-02-12 00:47:09 +0000303 case AMD_ID_LV320B:
304 info->flash_id += FLASH_AM320B;
305 info->sector_count = 67;
306 info->size = 0x00800000;
307 break; /* => 8 MB */
wdenk99edcfb2004-06-09 21:54:22 +0000308#endif /* 0 */
wdenkbf9e3b32004-02-12 00:47:09 +0000309 default:
310 info->flash_id = FLASH_UNKNOWN;
311 return (0); /* => no or unknown flash */
wdenkaffae2b2002-08-17 09:36:01 +0000312 }
313
314#if 0
315 /* set up sector start address table */
316 if (info->flash_id & FLASH_BTYPE) {
wdenkbf9e3b32004-02-12 00:47:09 +0000317 /* set sector offsets for bottom boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000318 info->start[0] = base + 0x00000000;
319 info->start[1] = base + 0x00008000;
320 info->start[2] = base + 0x0000C000;
321 info->start[3] = base + 0x00010000;
322 for (i = 4; i < info->sector_count; i++) {
323 info->start[i] = base + (i * 0x00020000) - 0x00060000;
324 }
325 } else {
wdenkbf9e3b32004-02-12 00:47:09 +0000326 /* set sector offsets for top boot block type */
wdenkaffae2b2002-08-17 09:36:01 +0000327 i = info->sector_count - 1;
328 info->start[i--] = base + info->size - 0x00008000;
329 info->start[i--] = base + info->size - 0x0000C000;
330 info->start[i--] = base + info->size - 0x00010000;
331 for (; i >= 0; i--) {
332 info->start[i] = base + i * 0x00020000;
333 }
334 }
335#else
wdenk99edcfb2004-06-09 21:54:22 +0000336 /* set sector offsets for uniform sector type */
337 for (i = 0; i < info->sector_count; i++)
338 info->start[i] = (ulong)addr + (i * 0x00040000);
wdenkaffae2b2002-08-17 09:36:01 +0000339#endif
340
341 /* check for protected sectors */
wdenkbf9e3b32004-02-12 00:47:09 +0000342 for (i = 0; i < info->sector_count; i++) {
wdenkaffae2b2002-08-17 09:36:01 +0000343 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
344 /* D0 = 1 if protected */
wdenkbf9e3b32004-02-12 00:47:09 +0000345 addr = (volatile unsigned long *) (info->start[i]);
wdenkaffae2b2002-08-17 09:36:01 +0000346 info->protect[i] = addr[2] & 1;
347 }
348
wdenkbf9e3b32004-02-12 00:47:09 +0000349 if (info->flash_id != FLASH_UNKNOWN) {
350 addr = (volatile unsigned long *) info->start[0];
wdenkaffae2b2002-08-17 09:36:01 +0000351 *addr = 0xF0F0F0F0; /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000352 }
353
354 return (info->size);
355}
356
wdenkaffae2b2002-08-17 09:36:01 +0000357/*-----------------------------------------------------------------------
358 */
wdenkbf9e3b32004-02-12 00:47:09 +0000359int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenkaffae2b2002-08-17 09:36:01 +0000360{
wdenkbf9e3b32004-02-12 00:47:09 +0000361 vu_long *addr = (vu_long *) (info->start[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000362 int flag, prot, sect, l_sect;
363 ulong start, now, last;
364
365 if ((s_first < 0) || (s_first > s_last)) {
366 if (info->flash_id == FLASH_UNKNOWN) {
367 printf ("- missing\n");
368 } else {
369 printf ("- no sectors to erase\n");
370 }
wdenk99edcfb2004-06-09 21:54:22 +0000371 return ERR_INVAL;
wdenkaffae2b2002-08-17 09:36:01 +0000372 }
373
374 if ((info->flash_id == FLASH_UNKNOWN) ||
375 (info->flash_id > FLASH_AMD_COMP)) {
376 printf ("Can't erase unknown flash type - aborted\n");
wdenk99edcfb2004-06-09 21:54:22 +0000377 return ERR_UNKNOWN_FLASH_TYPE;
wdenkaffae2b2002-08-17 09:36:01 +0000378 }
379
380 prot = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000381 for (sect = s_first; sect <= s_last; ++sect) {
wdenkaffae2b2002-08-17 09:36:01 +0000382 if (info->protect[sect]) {
383 prot++;
384 }
385 }
386
387 if (prot) {
wdenkbf9e3b32004-02-12 00:47:09 +0000388 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenkaffae2b2002-08-17 09:36:01 +0000389 } else {
390 printf ("\n");
391 }
392
393 l_sect = -1;
394
395 /* Disable interrupts which might cause a timeout here */
wdenkbf9e3b32004-02-12 00:47:09 +0000396 flag = disable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +0000397
wdenkaffae2b2002-08-17 09:36:01 +0000398 addr[0x0555] = 0xAAAAAAAA;
399 addr[0x02AA] = 0x55555555;
400 addr[0x0555] = 0x80808080;
401 addr[0x0555] = 0xAAAAAAAA;
402 addr[0x02AA] = 0x55555555;
wdenkaffae2b2002-08-17 09:36:01 +0000403
404 /* Start erase on unprotected sectors */
wdenkbf9e3b32004-02-12 00:47:09 +0000405 for (sect = s_first; sect <= s_last; sect++) {
wdenkaffae2b2002-08-17 09:36:01 +0000406 if (info->protect[sect] == 0) { /* not protected */
wdenkbf9e3b32004-02-12 00:47:09 +0000407 addr = (vu_long *) (info->start[sect]);
wdenkaffae2b2002-08-17 09:36:01 +0000408 addr[0] = 0x30303030;
wdenkaffae2b2002-08-17 09:36:01 +0000409 l_sect = sect;
410 }
411 }
412
413 /* re-enable interrupts if necessary */
414 if (flag)
wdenkbf9e3b32004-02-12 00:47:09 +0000415 enable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +0000416
417 /* wait at least 80us - let's wait 1 ms */
418 udelay (1000);
419
420 /*
421 * We wait for the last triggered sector
422 */
423 if (l_sect < 0)
424 goto DONE;
425
426 start = get_timer (0);
wdenkbf9e3b32004-02-12 00:47:09 +0000427 last = start;
428 addr = (vu_long *) (info->start[l_sect]);
wdenkaffae2b2002-08-17 09:36:01 +0000429 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
wdenkaffae2b2002-08-17 09:36:01 +0000430 {
wdenkbf9e3b32004-02-12 00:47:09 +0000431 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000432 printf ("Timeout\n");
wdenk99edcfb2004-06-09 21:54:22 +0000433 return ERR_TIMOUT;
wdenkaffae2b2002-08-17 09:36:01 +0000434 }
435 /* show that we're waiting */
436 if ((now - last) > 1000) { /* every second */
437 putc ('.');
438 last = now;
439 }
440 }
441
wdenkbf9e3b32004-02-12 00:47:09 +0000442 DONE:
wdenkaffae2b2002-08-17 09:36:01 +0000443 /* reset to read mode */
wdenkbf9e3b32004-02-12 00:47:09 +0000444 addr = (volatile unsigned long *) info->start[0];
wdenkaffae2b2002-08-17 09:36:01 +0000445 addr[0] = 0xF0F0F0F0; /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000446
447 printf (" done\n");
wdenk99edcfb2004-06-09 21:54:22 +0000448
wdenkaffae2b2002-08-17 09:36:01 +0000449 return 0;
450}
451
452/*-----------------------------------------------------------------------
453 * Copy memory to flash, returns:
454 * 0 - OK
455 * 1 - write timeout
456 * 2 - Flash not erased
457 */
wdenkbf9e3b32004-02-12 00:47:09 +0000458int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenkaffae2b2002-08-17 09:36:01 +0000459{
460 ulong cp, wp, data;
461 int i, l, rc;
462
463 wp = (addr & ~3); /* get lower word aligned address */
464
465 /*
466 * handle unaligned start bytes
467 */
468 if ((l = addr - wp) != 0) {
469 data = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000470 for (i = 0, cp = wp; i < l; ++i, ++cp) {
471 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000472 }
wdenkbf9e3b32004-02-12 00:47:09 +0000473 for (; i < 4 && cnt > 0; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000474 data = (data << 8) | *src++;
475 --cnt;
476 ++cp;
477 }
wdenkbf9e3b32004-02-12 00:47:09 +0000478 for (; cnt == 0 && i < 4; ++i, ++cp) {
479 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000480 }
481
wdenkbf9e3b32004-02-12 00:47:09 +0000482 if ((rc = write_word (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000483 return (rc);
484 }
485 wp += 4;
486 }
487
488 /*
489 * handle word aligned part
490 */
491 while (cnt >= 4) {
492 data = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000493 for (i = 0; i < 4; ++i) {
wdenkaffae2b2002-08-17 09:36:01 +0000494 data = (data << 8) | *src++;
495 }
wdenkbf9e3b32004-02-12 00:47:09 +0000496 if ((rc = write_word (info, wp, data)) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +0000497 return (rc);
498 }
wdenkbf9e3b32004-02-12 00:47:09 +0000499 wp += 4;
wdenkaffae2b2002-08-17 09:36:01 +0000500 cnt -= 4;
501 }
502
503 if (cnt == 0) {
504 return (0);
505 }
506
507 /*
508 * handle unaligned tail bytes
509 */
510 data = 0;
wdenkbf9e3b32004-02-12 00:47:09 +0000511 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
wdenkaffae2b2002-08-17 09:36:01 +0000512 data = (data << 8) | *src++;
513 --cnt;
514 }
wdenkbf9e3b32004-02-12 00:47:09 +0000515 for (; i < 4; ++i, ++cp) {
516 data = (data << 8) | (*(uchar *) cp);
wdenkaffae2b2002-08-17 09:36:01 +0000517 }
518
wdenkbf9e3b32004-02-12 00:47:09 +0000519 return (write_word (info, wp, data));
wdenkaffae2b2002-08-17 09:36:01 +0000520}
521
522/*-----------------------------------------------------------------------
523 * Write a word to Flash, returns:
524 * 0 - OK
525 * 1 - write timeout
526 * 2 - Flash not erased
527 */
wdenkbf9e3b32004-02-12 00:47:09 +0000528static int write_word (flash_info_t * info, ulong dest, ulong data)
wdenkaffae2b2002-08-17 09:36:01 +0000529{
wdenkbf9e3b32004-02-12 00:47:09 +0000530 vu_long *addr = (vu_long *) (info->start[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000531 ulong start;
532 int flag;
533
534 /* Check if Flash is (sufficiently) erased */
wdenkbf9e3b32004-02-12 00:47:09 +0000535 if ((*((vu_long *) dest) & data) != data) {
wdenk99edcfb2004-06-09 21:54:22 +0000536 return ERR_NOT_ERASED;
wdenkaffae2b2002-08-17 09:36:01 +0000537 }
538 /* Disable interrupts which might cause a timeout here */
wdenkbf9e3b32004-02-12 00:47:09 +0000539 flag = disable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +0000540
wdenkaffae2b2002-08-17 09:36:01 +0000541 addr[0x0555] = 0xAAAAAAAA;
542 addr[0x02AA] = 0x55555555;
543 addr[0x0555] = 0xA0A0A0A0;
wdenkaffae2b2002-08-17 09:36:01 +0000544
wdenkbf9e3b32004-02-12 00:47:09 +0000545 *((vu_long *) dest) = data;
wdenkaffae2b2002-08-17 09:36:01 +0000546
547 /* re-enable interrupts if necessary */
548 if (flag)
wdenkbf9e3b32004-02-12 00:47:09 +0000549 enable_interrupts ();
wdenkaffae2b2002-08-17 09:36:01 +0000550
551 /* data polling for D7 */
552 start = get_timer (0);
wdenkbf9e3b32004-02-12 00:47:09 +0000553 while ((*((vu_long *) dest) & 0x80808080) != (data & 0x80808080))
wdenkaffae2b2002-08-17 09:36:01 +0000554 {
wdenkbf9e3b32004-02-12 00:47:09 +0000555 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
wdenk99edcfb2004-06-09 21:54:22 +0000556 return ERR_TIMOUT;
wdenkaffae2b2002-08-17 09:36:01 +0000557 }
558 }
559 return (0);
560}