blob: 28f6f0a22b08c7f5f4f73b5f9075fab6bf8f9cd8 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2000, 2001
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/*
25 * Modified 4/5/2001
26 * Wait for completion of each sector erase command issued
27 * 4/5/2001
28 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
29 */
30
31/*
32 * Modified 3/7/2001
wdenk33149b82003-05-23 11:38:58 +000033 * - adapted for pip405, Denis Peter, MPL AG Switzerland
wdenkaffae2b2002-08-17 09:36:01 +000034 * TODO:
35 * clean-up
36 */
37
38#include <common.h>
39#include <ppc4xx.h>
40#include <asm/processor.h>
wdenkaffae2b2002-08-17 09:36:01 +000041#include "common_util.h"
42
43flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
44/*-----------------------------------------------------------------------
45 * Functions
46 */
47static ulong flash_get_size (vu_long *addr, flash_info_t *info);
48static int write_word (flash_info_t *info, ulong dest, ulong data);
wdenkaffae2b2002-08-17 09:36:01 +000049
50void unlock_intel_sectors(flash_info_t *info,ulong addr,ulong cnt);
51
52
wdenkaffae2b2002-08-17 09:36:01 +000053#ifdef CONFIG_PIP405
54#define ADDR0 0x5555
55#define ADDR1 0x2aaa
56#define FLASH_WORD_SIZE unsigned short
57#endif
58
59#ifdef CONFIG_MIP405
60#define ADDR0 0x5555
61#define ADDR1 0x2aaa
62#define FLASH_WORD_SIZE unsigned short
63#endif
64
65#define FALSE 0
66#define TRUE 1
67
68/*-----------------------------------------------------------------------
69 */
70
71
72unsigned long flash_init (void)
73{
74 unsigned long size_b0, size_b1;
75 int i;
wdenkaffae2b2002-08-17 09:36:01 +000076
wdenk33149b82003-05-23 11:38:58 +000077 /* Since we are relocated, we can set-up the CS finally */
78 setup_cs_reloc();
79 /* get and display boot mode */
80 i=get_boot_mode();
81 if(i & BOOT_PCI)
82 printf("(PCI Boot %s Map) ",(i & BOOT_MPS) ?
83 "MPS" : "Flash");
wdenkaffae2b2002-08-17 09:36:01 +000084 else
wdenk33149b82003-05-23 11:38:58 +000085 printf("(%s Boot) ",(i & BOOT_MPS) ?
86 "MPS" : "Flash");
wdenkaffae2b2002-08-17 09:36:01 +000087 /* Init: no FLASHes known */
88 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
89 flash_info[i].flash_id = FLASH_UNKNOWN;
90 }
91
92 /* Static FLASH Bank configuration here - FIXME XXX */
93
94 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
95
96 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
97 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
98 size_b0, size_b0<<20);
99 }
wdenk33149b82003-05-23 11:38:58 +0000100 /* protect the bootloader */
101 /* Monitor protection ON by default */
wdenkaffae2b2002-08-17 09:36:01 +0000102#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
wdenk33149b82003-05-23 11:38:58 +0000103 flash_protect(FLAG_PROTECT_SET,
104 CFG_MONITOR_BASE,
wdenk3b57fe02003-05-30 12:48:29 +0000105 CFG_MONITOR_BASE+monitor_flash_len-1,
wdenk33149b82003-05-23 11:38:58 +0000106 &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000107#endif
wdenk33149b82003-05-23 11:38:58 +0000108 size_b1 = 0 ;
109 flash_info[0].size = size_b0;
wdenkaffae2b2002-08-17 09:36:01 +0000110#if 0
wdenk33149b82003-05-23 11:38:58 +0000111 /* include this if you want to test if
112 the relocation has be done ok.
113 This will disable both Chipselects */
114 mtdcr (ebccfga, pb0cr);
115 mtdcr (ebccfgd, 0L);
116 mtdcr (ebccfga, pb1cr);
117 mtdcr (ebccfgd, 0L);
118 printf("CS0 & CS1 switched off for test\n");
119#endif
120 return (size_b0);
wdenkaffae2b2002-08-17 09:36:01 +0000121}
122
wdenk33149b82003-05-23 11:38:58 +0000123
wdenkaffae2b2002-08-17 09:36:01 +0000124/*-----------------------------------------------------------------------
125 */
126void flash_print_info (flash_info_t *info)
127{
128 int i;
wdenk33149b82003-05-23 11:38:58 +0000129 int k;
130 int size;
131 int erased;
132 volatile unsigned long *flash;
wdenkaffae2b2002-08-17 09:36:01 +0000133
134 if (info->flash_id == FLASH_UNKNOWN) {
135 printf ("missing or unknown FLASH type\n");
136 return;
137 }
138
139 switch (info->flash_id & FLASH_VENDMASK) {
140 case FLASH_MAN_AMD: printf ("AMD "); break;
141 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
142 case FLASH_MAN_SST: printf ("SST "); break;
143 case FLASH_MAN_INTEL: printf ("Intel "); break;
144 default: printf ("Unknown Vendor "); break;
145 }
146
147 switch (info->flash_id & FLASH_TYPEMASK) {
148 case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
149 break;
150 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
151 break;
152 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
153 break;
154 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
155 break;
156 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
157 break;
158 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
159 break;
160 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
161 break;
162 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
163 break;
164 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
165 break;
166 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
167 break;
168 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
169 break;
170 case FLASH_INTEL320T: printf ("TE28F320C3 (32 Mbit, top sector size)\n");
171 break;
172 default: printf ("Unknown Chip Type\n");
173 break;
174 }
175
176 printf (" Size: %ld KB in %d Sectors\n",
177 info->size >> 10, info->sector_count);
178
179 printf (" Sector Start Addresses:");
180 for (i=0; i<info->sector_count; ++i) {
wdenk33149b82003-05-23 11:38:58 +0000181 /*
182 * Check if whole sector is erased
183 */
wdenkaffae2b2002-08-17 09:36:01 +0000184 if (i != (info->sector_count-1))
wdenk33149b82003-05-23 11:38:58 +0000185 size = info->start[i+1] - info->start[i];
wdenkaffae2b2002-08-17 09:36:01 +0000186 else
wdenk33149b82003-05-23 11:38:58 +0000187 size = info->start[0] + info->size - info->start[i];
188 erased = 1;
189 flash = (volatile unsigned long *)info->start[i];
190 size = size >> 2; /* divide by 4 for longword access */
191 for (k=0; k<size; k++) {
192 if (*flash++ != 0xffffffff) {
193 erased = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000194 break;
195 }
wdenk33149b82003-05-23 11:38:58 +0000196 }
wdenkaffae2b2002-08-17 09:36:01 +0000197 if ((i % 5) == 0)
198 printf ("\n ");
wdenkaffae2b2002-08-17 09:36:01 +0000199 printf (" %08lX%s%s",
200 info->start[i],
201 erased ? " E" : " ",
wdenk33149b82003-05-23 11:38:58 +0000202 info->protect[i] ? "RO " : " ");
wdenkaffae2b2002-08-17 09:36:01 +0000203 }
204 printf ("\n");
wdenkaffae2b2002-08-17 09:36:01 +0000205}
206
207/*-----------------------------------------------------------------------
208 */
209
210
211/*-----------------------------------------------------------------------
212 */
213
214/*
215 * The following code cannot be run from FLASH!
216 */
217static ulong flash_get_size (vu_long *addr, flash_info_t *info)
218{
219 short i;
220 FLASH_WORD_SIZE value;
221 ulong base = (ulong)addr;
222 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
223
224 /* Write auto select command: read Manufacturer ID */
225 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
226 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
227 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
228
229 value = addr2[0];
230 /* printf("flash_get_size value: %x\n",value); */
231 switch (value) {
232 case (FLASH_WORD_SIZE)AMD_MANUFACT:
233 info->flash_id = FLASH_MAN_AMD;
234 break;
235 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
236 info->flash_id = FLASH_MAN_FUJ;
237 break;
238 case (FLASH_WORD_SIZE)INTEL_MANUFACT:
239 info->flash_id = FLASH_MAN_INTEL;
240 break;
241 case (FLASH_WORD_SIZE)SST_MANUFACT:
242 info->flash_id = FLASH_MAN_SST;
243 break;
244 default:
245 info->flash_id = FLASH_UNKNOWN;
246 info->sector_count = 0;
247 info->size = 0;
248 return (0); /* no or unknown flash */
249 }
250 value = addr2[1]; /* device ID */
251 /* printf("Device value %x\n",value); */
252 switch (value) {
253 case (FLASH_WORD_SIZE)AMD_ID_F040B:
wdenk8bde7f72003-06-27 21:31:46 +0000254 info->flash_id += FLASH_AM040;
wdenkaffae2b2002-08-17 09:36:01 +0000255 info->sector_count = 8;
256 info->size = 0x0080000; /* => 512 ko */
257 break;
258 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
259 info->flash_id += FLASH_AM400T;
260 info->sector_count = 11;
261 info->size = 0x00080000;
262 break; /* => 0.5 MB */
263
264 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
265 info->flash_id += FLASH_AM400B;
266 info->sector_count = 11;
267 info->size = 0x00080000;
268 break; /* => 0.5 MB */
269
270 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
271 info->flash_id += FLASH_AM800T;
272 info->sector_count = 19;
273 info->size = 0x00100000;
274 break; /* => 1 MB */
275
276 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
277 info->flash_id += FLASH_AM800B;
278 info->sector_count = 19;
279 info->size = 0x00100000;
280 break; /* => 1 MB */
281
282 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
283 info->flash_id += FLASH_AM160T;
284 info->sector_count = 35;
285 info->size = 0x00200000;
286 break; /* => 2 MB */
287
288 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
289 info->flash_id += FLASH_AM160B;
290 info->sector_count = 35;
291 info->size = 0x00200000;
292 break; /* => 2 MB */
293#if 0 /* enable when device IDs are available */
294 case (FLASH_WORD_SIZE)AMD_ID_LV320T:
295 info->flash_id += FLASH_AM320T;
296 info->sector_count = 67;
297 info->size = 0x00400000;
298 break; /* => 4 MB */
299
300 case (FLASH_WORD_SIZE)AMD_ID_LV320B:
301 info->flash_id += FLASH_AM320B;
302 info->sector_count = 67;
303 info->size = 0x00400000;
304 break; /* => 4 MB */
305#endif
306 case (FLASH_WORD_SIZE)SST_ID_xF800A:
307 info->flash_id += FLASH_SST800A;
308 info->sector_count = 16;
309 info->size = 0x00100000;
310 break; /* => 1 MB */
311 case (FLASH_WORD_SIZE)INTEL_ID_28F320C3T:
312 info->flash_id += FLASH_INTEL320T;
313 info->sector_count = 71;
314 info->size = 0x00400000;
315 break; /* => 4 MB */
316
317
318 case (FLASH_WORD_SIZE)SST_ID_xF160A:
319 info->flash_id += FLASH_SST160A;
320 info->sector_count = 32;
321 info->size = 0x00200000;
322 break; /* => 2 MB */
323
324 default:
325 info->flash_id = FLASH_UNKNOWN;
326 return (0); /* => no or unknown flash */
327
328 }
329
330 /* set up sector start address table */
wdenk33149b82003-05-23 11:38:58 +0000331 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
332 (info->flash_id == FLASH_AM040)){
wdenkaffae2b2002-08-17 09:36:01 +0000333 for (i = 0; i < info->sector_count; i++)
334 info->start[i] = base + (i * 0x00010000);
wdenk33149b82003-05-23 11:38:58 +0000335 }
336 else {
wdenkaffae2b2002-08-17 09:36:01 +0000337 if (info->flash_id & FLASH_BTYPE) {
wdenk33149b82003-05-23 11:38:58 +0000338 /* set sector offsets for bottom boot block type */
339 info->start[0] = base + 0x00000000;
340 info->start[1] = base + 0x00004000;
341 info->start[2] = base + 0x00006000;
342 info->start[3] = base + 0x00008000;
343 for (i = 4; i < info->sector_count; i++)
344 info->start[i] = base + (i * 0x00010000) - 0x00030000;
345 }
346 else {
wdenkaffae2b2002-08-17 09:36:01 +0000347 /* set sector offsets for top boot block type */
348 i = info->sector_count - 1;
349 if(info->sector_count==71) {
350
351 info->start[i--] = base + info->size - 0x00002000;
352 info->start[i--] = base + info->size - 0x00004000;
353 info->start[i--] = base + info->size - 0x00006000;
354 info->start[i--] = base + info->size - 0x00008000;
355 info->start[i--] = base + info->size - 0x0000A000;
356 info->start[i--] = base + info->size - 0x0000C000;
357 info->start[i--] = base + info->size - 0x0000E000;
358 for (; i >= 0; i--)
359 info->start[i] = base + i * 0x000010000;
360 }
361 else {
362 info->start[i--] = base + info->size - 0x00004000;
363 info->start[i--] = base + info->size - 0x00006000;
364 info->start[i--] = base + info->size - 0x00008000;
365 for (; i >= 0; i--)
366 info->start[i] = base + i * 0x00010000;
367 }
368 }
369 }
370
371 /* check for protected sectors */
372 for (i = 0; i < info->sector_count; i++) {
373 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
374 /* D0 = 1 if protected */
375 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
wdenk33149b82003-05-23 11:38:58 +0000376 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
377 info->protect[i] = 0;
378 else
379 info->protect[i] = addr2[2] & 1;
wdenkaffae2b2002-08-17 09:36:01 +0000380 }
381
382 /*
383 * Prevent writes to uninitialized FLASH.
384 */
385 if (info->flash_id != FLASH_UNKNOWN) {
wdenkaffae2b2002-08-17 09:36:01 +0000386 addr2 = (FLASH_WORD_SIZE *)info->start[0];
387 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000388 }
389 return (info->size);
390}
391
wdenk33149b82003-05-23 11:38:58 +0000392
wdenkaffae2b2002-08-17 09:36:01 +0000393int wait_for_DQ7(flash_info_t *info, int sect)
394{
395 ulong start, now, last;
396 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
397
398 start = get_timer (0);
wdenk33149b82003-05-23 11:38:58 +0000399 last = start;
400 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
401 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
402 printf ("Timeout\n");
403 return -1;
404 }
405 /* show that we're waiting */
406 if ((now - last) > 1000) { /* every second */
407 putc ('.');
408 last = now;
409 }
410 }
wdenkaffae2b2002-08-17 09:36:01 +0000411 return 0;
412}
413
414int intel_wait_for_DQ7(flash_info_t *info, int sect)
415{
416 ulong start, now, last;
417 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
418
wdenk33149b82003-05-23 11:38:58 +0000419 start = get_timer (0);
420 last = start;
421 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
422 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
423 printf ("Timeout\n");
424 return -1;
425 }
426 /* show that we're waiting */
427 if ((now - last) > 1000) { /* every second */
428 putc ('.');
429 last = now;
430 }
431 }
432 addr[0]=(FLASH_WORD_SIZE)0x00500050;
wdenkaffae2b2002-08-17 09:36:01 +0000433 return 0;
434}
435
436/*-----------------------------------------------------------------------
437 */
438
439int flash_erase (flash_info_t *info, int s_first, int s_last)
440{
441 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
442 volatile FLASH_WORD_SIZE *addr2;
443 int flag, prot, sect, l_sect;
444 int i;
445
446
447 if ((s_first < 0) || (s_first > s_last)) {
448 if (info->flash_id == FLASH_UNKNOWN) {
449 printf ("- missing\n");
450 } else {
451 printf ("- no sectors to erase\n");
452 }
453 return 1;
454 }
455
456 if (info->flash_id == FLASH_UNKNOWN) {
457 printf ("Can't erase unknown flash type - aborted\n");
458 return 1;
459 }
460
461 prot = 0;
462 for (sect=s_first; sect<=s_last; ++sect) {
463 if (info->protect[sect]) {
464 prot++;
465 }
466 }
467
468 if (prot) {
469 printf ("- Warning: %d protected sectors will not be erased!\n",
470 prot);
471 } else {
472 printf ("\n");
473 }
474
475 l_sect = -1;
476
477 /* Disable interrupts which might cause a timeout here */
478 flag = disable_interrupts();
479
480 /* Start erase on unprotected sectors */
481 for (sect = s_first; sect<=s_last; sect++) {
482 if (info->protect[sect] == 0) { /* not protected */
483 addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
484 /* printf("Erasing sector %p\n", addr2); */ /* CLH */
wdenk33149b82003-05-23 11:38:58 +0000485 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
wdenkaffae2b2002-08-17 09:36:01 +0000486 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
487 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
488 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
489 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
490 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
491 addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
492 for (i=0; i<50; i++)
493 udelay(1000); /* wait 1 ms */
wdenk33149b82003-05-23 11:38:58 +0000494 wait_for_DQ7(info, sect);
495 }
496 else {
wdenk8bde7f72003-06-27 21:31:46 +0000497 if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
wdenkaffae2b2002-08-17 09:36:01 +0000498 addr2[0] = (FLASH_WORD_SIZE)0x00600060; /* unlock sector */
499 addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* sector erase */
wdenk33149b82003-05-23 11:38:58 +0000500 intel_wait_for_DQ7(info, sect);
wdenkaffae2b2002-08-17 09:36:01 +0000501 addr2[0] = (FLASH_WORD_SIZE)0x00200020; /* sector erase */
502 addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* sector erase */
wdenk33149b82003-05-23 11:38:58 +0000503 intel_wait_for_DQ7(info, sect);
wdenkaffae2b2002-08-17 09:36:01 +0000504 }
505 else {
506 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
507 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
508 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
509 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
510 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
511 addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
wdenk33149b82003-05-23 11:38:58 +0000512 wait_for_DQ7(info, sect);
513 }
514 }
515 l_sect = sect;
516 /*
517 * Wait for each sector to complete, it's more
518 * reliable. According to AMD Spec, you must
519 * issue all erase commands within a specified
520 * timeout. This has been seen to fail, especially
521 * if printf()s are included (for debug)!!
522 */
523 /* wait_for_DQ7(info, sect); */
wdenkaffae2b2002-08-17 09:36:01 +0000524 }
525 }
526
527 /* re-enable interrupts if necessary */
528 if (flag)
529 enable_interrupts();
530
531 /* wait at least 80us - let's wait 1 ms */
532 udelay (1000);
533
534#if 0
535 /*
536 * We wait for the last triggered sector
537 */
538 if (l_sect < 0)
539 goto DONE;
540 wait_for_DQ7(info, l_sect);
541
542DONE:
543#endif
544 /* reset to read mode */
545 addr = (FLASH_WORD_SIZE *)info->start[0];
546 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
547
548 printf (" done\n");
549 return 0;
550}
551
wdenk33149b82003-05-23 11:38:58 +0000552
wdenkaffae2b2002-08-17 09:36:01 +0000553void unlock_intel_sectors(flash_info_t *info,ulong addr,ulong cnt)
554{
555 int i;
556 volatile FLASH_WORD_SIZE *addr2;
557 long c;
558 c= (long)cnt;
559 for(i=info->sector_count-1;i>0;i--)
560 {
561 if(addr>=info->start[i])
562 break;
563 }
564 do {
565 addr2 = (FLASH_WORD_SIZE *)(info->start[i]);
566 addr2[0] = (FLASH_WORD_SIZE)0x00600060; /* unlock sector setup */
567 addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* unlock sector */
568 intel_wait_for_DQ7(info, i);
569 i++;
570 c-=(info->start[i]-info->start[i-1]);
571 }while(c>0);
wdenkaffae2b2002-08-17 09:36:01 +0000572}
573
574
575/*-----------------------------------------------------------------------
576 * Copy memory to flash, returns:
577 * 0 - OK
578 * 1 - write timeout
579 * 2 - Flash not erased
580 */
581
582int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
583{
584 ulong cp, wp, data;
585 int i, l, rc;
586
587 if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
588 unlock_intel_sectors(info,addr,cnt);
589 }
590 wp = (addr & ~3); /* get lower word aligned address */
591 /*
592 * handle unaligned start bytes
593 */
594 if ((l = addr - wp) != 0) {
595 data = 0;
596 for (i=0, cp=wp; i<l; ++i, ++cp) {
597 data = (data << 8) | (*(uchar *)cp);
598 }
599 for (; i<4 && cnt>0; ++i) {
600 data = (data << 8) | *src++;
601 --cnt;
602 ++cp;
603 }
604 for (; cnt==0 && i<4; ++i, ++cp) {
605 data = (data << 8) | (*(uchar *)cp);
606 }
607
608 if ((rc = write_word(info, wp, data)) != 0) {
609 return (rc);
610 }
611 wp += 4;
612 }
613
614 /*
615 * handle word aligned part
616 */
617 while (cnt >= 4) {
618 data = 0;
619 for (i=0; i<4; ++i) {
620 data = (data << 8) | *src++;
621 }
622 if ((rc = write_word(info, wp, data)) != 0) {
623 return (rc);
624 }
625 wp += 4;
626 if((wp % 0x10000)==0)
627 printf("."); /* show Progress */
628 cnt -= 4;
629 }
630
631 if (cnt == 0) {
632 return (0);
633 }
634
635 /*
636 * handle unaligned tail bytes
637 */
638 data = 0;
639 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
640 data = (data << 8) | *src++;
641 --cnt;
642 }
643 for (; i<4; ++i, ++cp) {
644 data = (data << 8) | (*(uchar *)cp);
645 }
646 rc=write_word(info, wp, data);
647 return rc;
648}
649
650/*-----------------------------------------------------------------------
651 * Write a word to Flash, returns:
652 * 0 - OK
653 * 1 - write timeout
654 * 2 - Flash not erased
655 */
656static FLASH_WORD_SIZE *read_val = (FLASH_WORD_SIZE *)0x200000;
657
658static int write_word (flash_info_t *info, ulong dest, ulong data)
659{
660 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
661 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
wdenk8bde7f72003-06-27 21:31:46 +0000662 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
wdenkaffae2b2002-08-17 09:36:01 +0000663 ulong start;
664 int flag;
665 int i;
666
667 /* Check if Flash is (sufficiently) erased */
668 if ((*((volatile FLASH_WORD_SIZE *)dest) &
669 (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
670 return (2);
671 }
672 /* Disable interrupts which might cause a timeout here */
673 flag = disable_interrupts();
674 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
675 {
676 if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
677 /* intel style writting */
678 dest2[i] = (FLASH_WORD_SIZE)0x00500050;
679 dest2[i] = (FLASH_WORD_SIZE)0x00400040;
680 *read_val++ = data2[i];
681 dest2[i] = data2[i];
682 if (flag)
683 enable_interrupts();
684 /* data polling for D7 */
685 start = get_timer (0);
686 udelay(10);
687 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080)
688 {
689 if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
690 return (1);
691 }
692 dest2[i] = (FLASH_WORD_SIZE)0x00FF00FF; /* return to read mode */
693 udelay(10);
694 dest2[i] = (FLASH_WORD_SIZE)0x00FF00FF; /* return to read mode */
695 if(dest2[i]!=data2[i])
696 printf("Error at %p 0x%04X != 0x%04X\n",&dest2[i],dest2[i],data2[i]);
697 }
698 else {
699 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
700 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
701 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
702 dest2[i] = data2[i];
703 /* re-enable interrupts if necessary */
704 if (flag)
705 enable_interrupts();
706 /* data polling for D7 */
707 start = get_timer (0);
708 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
709 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
710 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
wdenk8bde7f72003-06-27 21:31:46 +0000711 return (1);
wdenkaffae2b2002-08-17 09:36:01 +0000712 }
713 }
714 }
715 }
716 return (0);
717}
718
719/*-----------------------------------------------------------------------
720 */