blob: 442dd00519af80d099ccbeb1accc6235c0585c5f [file] [log] [blame]
wdenke69b4b82002-09-17 21:26:59 +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/*
25 * 07-10-2002 Frank Gottschling: added 29F032 flash (ELPPC).
26 * fixed monitor protection part
27 *
28 * 09-18-2001 Andreas Heppel: Reduced the code in here to the usage
29 * of AMD's 29F040 and 29F016 flashes, since the BAB7xx does use
30 * any other.
31 */
32
33#include <common.h>
34#include <asm/processor.h>
35#include <asm/pci_io.h>
36
37flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
38
39ulong flash_get_size (vu_long *addr, flash_info_t *info);
40static int write_word (flash_info_t *info, ulong dest, ulong data);
41
42/*flash command address offsets*/
43
44#define ADDR0 (0x555)
45#define ADDR1 (0x2AA)
46#define ADDR3 (0x001)
47
48#define FLASH_WORD_SIZE unsigned char
49
50/*----------------------------------------------------------------------------*/
51
52unsigned long flash_init (void)
53{
54 unsigned long size1, size2;
55 int i;
56
57 /* Init: no FLASHes known */
58 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i)
59 {
wdenk57b2d802003-06-27 21:31:46 +000060 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenke69b4b82002-09-17 21:26:59 +000061 }
62
63 /* initialise 1st flash */
64 size1 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
65
66 if (flash_info[0].flash_id == FLASH_UNKNOWN)
67 {
wdenk57b2d802003-06-27 21:31:46 +000068 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
69 size1, size1<<20);
wdenke69b4b82002-09-17 21:26:59 +000070 }
71
72 /* initialise 2nd flash */
73 size2 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
74
75 if (flash_info[1].flash_id == FLASH_UNKNOWN)
76 {
wdenk57b2d802003-06-27 21:31:46 +000077 printf ("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
78 size2, size2<<20);
wdenke69b4b82002-09-17 21:26:59 +000079 }
80
81 /* monitor protection ON by default */
82 if (size1 == 512*1024)
83 {
wdenk57b2d802003-06-27 21:31:46 +000084 (void)flash_protect(FLAG_PROTECT_SET,
85 FLASH_BASE0_PRELIM,
86 FLASH_BASE0_PRELIM+monitor_flash_len-1,
87 &flash_info[0]);
wdenke69b4b82002-09-17 21:26:59 +000088 }
89 if (size2 == 512*1024)
90 {
wdenk57b2d802003-06-27 21:31:46 +000091 (void)flash_protect(FLAG_PROTECT_SET,
92 FLASH_BASE1_PRELIM,
93 FLASH_BASE1_PRELIM+monitor_flash_len-1,
94 &flash_info[1]);
wdenke69b4b82002-09-17 21:26:59 +000095 }
96 if (size2 == 4*1024*1024)
97 {
wdenk57b2d802003-06-27 21:31:46 +000098 (void)flash_protect(FLAG_PROTECT_SET,
99 CFG_FLASH_BASE,
100 CFG_FLASH_BASE+monitor_flash_len-1,
101 &flash_info[1]);
wdenke69b4b82002-09-17 21:26:59 +0000102 }
103
104 return (size1 + size2);
105}
106
107/*----------------------------------------------------------------------------*/
108
109void flash_print_info (flash_info_t *info)
110{
111 int i;
112 int k;
113 int size;
114 int erased;
115 volatile unsigned long *flash;
116
117 if (info->flash_id == FLASH_UNKNOWN) {
wdenk57b2d802003-06-27 21:31:46 +0000118 printf ("missing or unknown FLASH type\n");
119 flash_init();
wdenke69b4b82002-09-17 21:26:59 +0000120 }
121
122 if (info->flash_id == FLASH_UNKNOWN) {
wdenk57b2d802003-06-27 21:31:46 +0000123 printf ("missing or unknown FLASH type\n");
124 return;
wdenke69b4b82002-09-17 21:26:59 +0000125 }
126
127 switch (info->flash_id & FLASH_VENDMASK) {
128 case FLASH_MAN_AMD:
wdenk57b2d802003-06-27 21:31:46 +0000129 printf ("AMD ");
130 break;
wdenke69b4b82002-09-17 21:26:59 +0000131 default:
wdenk57b2d802003-06-27 21:31:46 +0000132 printf ("Unknown Vendor ");
133 break;
wdenke69b4b82002-09-17 21:26:59 +0000134 }
135
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case AMD_ID_F040B:
wdenk57b2d802003-06-27 21:31:46 +0000138 printf ("AM29F040B (4 Mbit)\n");
139 break;
wdenke69b4b82002-09-17 21:26:59 +0000140 case AMD_ID_F016D:
wdenk57b2d802003-06-27 21:31:46 +0000141 printf ("AM29F016D (16 Mbit)\n");
142 break;
wdenke69b4b82002-09-17 21:26:59 +0000143 case AMD_ID_F032B:
wdenk57b2d802003-06-27 21:31:46 +0000144 printf ("AM29F032B (32 Mbit)\n");
145 break;
wdenke69b4b82002-09-17 21:26:59 +0000146 default:
wdenk57b2d802003-06-27 21:31:46 +0000147 printf ("Unknown Chip Type\n");
148 break;
wdenke69b4b82002-09-17 21:26:59 +0000149 }
150
151 if (info->size >= (1 << 20)) {
wdenk57b2d802003-06-27 21:31:46 +0000152 printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
wdenke69b4b82002-09-17 21:26:59 +0000153 } else {
wdenk57b2d802003-06-27 21:31:46 +0000154 printf (" Size: %ld kB in %d Sectors\n", info->size >> 10, info->sector_count);
wdenke69b4b82002-09-17 21:26:59 +0000155 }
156
157 printf (" Sector Start Addresses:");
158 for (i=0; i<info->sector_count; ++i) {
wdenk57b2d802003-06-27 21:31:46 +0000159 /*
160 * Check if whole sector is erased
161 */
162 if (i != (info->sector_count-1))
163 size = info->start[i+1] - info->start[i];
164 else
165 size = info->start[0] + info->size - info->start[i];
wdenke69b4b82002-09-17 21:26:59 +0000166
wdenk57b2d802003-06-27 21:31:46 +0000167 erased = 1;
168 flash = (volatile unsigned long *)info->start[i];
169 size = size >> 2; /* divide by 4 for longword access */
170 for (k=0; k<size; k++) {
171 if (*flash++ != 0xffffffff) {
172 erased = 0;
173 break;
174 }
175 }
wdenke69b4b82002-09-17 21:26:59 +0000176
wdenk57b2d802003-06-27 21:31:46 +0000177 if ((i % 5) == 0)
178 printf ("\n ");
wdenke69b4b82002-09-17 21:26:59 +0000179
wdenk57b2d802003-06-27 21:31:46 +0000180 printf (" %08lX%s%s",
181 info->start[i],
182 erased ? " E" : " ",
183 info->protect[i] ? "RO " : " ");
wdenke69b4b82002-09-17 21:26:59 +0000184 }
185 printf ("\n");
186}
187
188/*----------------------------------------------------------------------------*/
189/*
190 * The following code cannot be run from FLASH!
191 */
192ulong flash_get_size (vu_long *addr, flash_info_t *info)
193{
194 short i;
195 ulong vendor, devid;
196 ulong base = (ulong)addr;
197 volatile unsigned char *caddr = (unsigned char *)addr;
198
199#ifdef DEBUG
200 printf("flash_get_size for address 0x%lx: \n", (unsigned long)caddr);
201#endif
202
203 /* Write auto select command: read Manufacturer ID */
204 caddr[0] = 0xF0; /* reset bank */
205 udelay(10);
206
207 eieio();
208 caddr[0x555] = 0xAA;
209 udelay(10);
210 caddr[0x2AA] = 0x55;
211 udelay(10);
212 caddr[0x555] = 0x90;
213
214 udelay(10);
215
216 vendor = caddr[0];
217 devid = caddr[1];
218
219#ifdef DEBUG
220 printf("Manufacturer: 0x%lx\n", vendor);
221#endif
222
223 vendor &= 0xff;
224 devid &= 0xff;
225
226 /* We accept only two AMD types */
227 switch (vendor) {
228 case (FLASH_WORD_SIZE)AMD_MANUFACT:
wdenk57b2d802003-06-27 21:31:46 +0000229 info->flash_id = FLASH_MAN_AMD;
230 break;
wdenke69b4b82002-09-17 21:26:59 +0000231 default:
wdenk57b2d802003-06-27 21:31:46 +0000232 info->flash_id = FLASH_UNKNOWN;
233 info->sector_count = 0;
234 info->size = 0;
235 return (0); /* no or unknown flash */
wdenke69b4b82002-09-17 21:26:59 +0000236 }
237
238 switch (devid) {
239 case (FLASH_WORD_SIZE)AMD_ID_F040B:
wdenk57b2d802003-06-27 21:31:46 +0000240 info->flash_id |= AMD_ID_F040B;
241 info->sector_count = 8;
242 info->size = 0x00080000;
243 break; /* => 0.5 MB */
wdenke69b4b82002-09-17 21:26:59 +0000244
245 case (FLASH_WORD_SIZE)AMD_ID_F016D:
wdenk57b2d802003-06-27 21:31:46 +0000246 info->flash_id |= AMD_ID_F016D;
247 info->sector_count = 32;
248 info->size = 0x00200000;
249 break; /* => 2 MB */
wdenke69b4b82002-09-17 21:26:59 +0000250
251 case (FLASH_WORD_SIZE)AMD_ID_F032B:
wdenk57b2d802003-06-27 21:31:46 +0000252 info->flash_id |= AMD_ID_F032B;
253 info->sector_count = 64;
254 info->size = 0x00400000;
255 break; /* => 4 MB */
wdenke69b4b82002-09-17 21:26:59 +0000256
257 default:
wdenk57b2d802003-06-27 21:31:46 +0000258 info->flash_id = FLASH_UNKNOWN;
259 return (0); /* => no or unknown flash */
wdenke69b4b82002-09-17 21:26:59 +0000260
261 }
262
263#ifdef DEBUG
264 printf("flash id 0x%lx; sector count 0x%x, size 0x%lx\n", info->flash_id, info->sector_count, info->size);
265#endif
266
267 /* check for protected sectors */
268 for (i = 0; i < info->sector_count; i++) {
wdenk57b2d802003-06-27 21:31:46 +0000269 /* sector base address */
270 info->start[i] = base + i * (info->size / info->sector_count);
271 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
272 /* D0 = 1 if protected */
273 caddr = (volatile unsigned char *)(info->start[i]);
274 info->protect[i] = caddr[2] & 1;
wdenke69b4b82002-09-17 21:26:59 +0000275 }
276
277 /*
278 * Prevent writes to uninitialized FLASH.
279 */
280 if (info->flash_id != FLASH_UNKNOWN) {
wdenk57b2d802003-06-27 21:31:46 +0000281 caddr = (volatile unsigned char *)info->start[0];
282 caddr[0] = 0xF0; /* reset bank */
wdenke69b4b82002-09-17 21:26:59 +0000283 }
284
285 return (info->size);
286}
287
288/*----------------------------------------------------------------------------*/
289
290int flash_erase (flash_info_t *info, int s_first, int s_last)
291{
292 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
293 int flag, prot, sect, l_sect;
294 ulong start, now, last;
295 int rc = 0;
296
297 if ((s_first < 0) || (s_first > s_last)) {
wdenk57b2d802003-06-27 21:31:46 +0000298 if (info->flash_id == FLASH_UNKNOWN) {
299 printf ("- missing\n");
300 } else {
301 printf ("- no sectors to erase\n");
302 }
303 return 1;
wdenke69b4b82002-09-17 21:26:59 +0000304 }
305
306 if ((info->flash_id == FLASH_UNKNOWN) ||
wdenk57b2d802003-06-27 21:31:46 +0000307 (info->flash_id > FLASH_AMD_COMP)) {
308 printf ("Can't erase unknown flash type - aborted\n");
309 return 1;
wdenke69b4b82002-09-17 21:26:59 +0000310 }
311
312 prot = 0;
313 for (sect=s_first; sect<=s_last; ++sect) {
wdenk57b2d802003-06-27 21:31:46 +0000314 if (info->protect[sect]) {
315 prot++;
316 }
wdenke69b4b82002-09-17 21:26:59 +0000317 }
318
319 if (prot) {
wdenk57b2d802003-06-27 21:31:46 +0000320 printf ("- Warning: %d protected sectors will not be erased!\n",
321 prot);
wdenke69b4b82002-09-17 21:26:59 +0000322 } else {
wdenk57b2d802003-06-27 21:31:46 +0000323 printf ("\n");
wdenke69b4b82002-09-17 21:26:59 +0000324 }
325
326 l_sect = -1;
327
328 /* Disable interrupts which might cause a timeout here */
329 flag = disable_interrupts();
330
331 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
332 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
333 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
334 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
335 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
336
337 /* Start erase on unprotected sectors */
338 for (sect = s_first; sect<=s_last; sect++) {
wdenk57b2d802003-06-27 21:31:46 +0000339 if (info->protect[sect] == 0) { /* not protected */
340 addr = (FLASH_WORD_SIZE *)(info->start[sect]);
341 if (info->flash_id & FLASH_MAN_SST) {
342 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
343 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
344 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
345 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
346 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
347 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
348 udelay(30000); /* wait 30 ms */
349 }
350 else
351 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
352 l_sect = sect;
353 }
wdenke69b4b82002-09-17 21:26:59 +0000354 }
355
356 /* re-enable interrupts if necessary */
357 if (flag)
wdenk57b2d802003-06-27 21:31:46 +0000358 enable_interrupts();
wdenke69b4b82002-09-17 21:26:59 +0000359
360 /* wait at least 80us - let's wait 1 ms */
361 udelay (1000);
362
363 /*
364 * We wait for the last triggered sector
365 */
366 if (l_sect < 0)
wdenk57b2d802003-06-27 21:31:46 +0000367 goto DONE;
wdenke69b4b82002-09-17 21:26:59 +0000368
369 start = get_timer (0);
370 last = start;
371 addr = (FLASH_WORD_SIZE *)(info->start[l_sect]);
372 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
wdenk57b2d802003-06-27 21:31:46 +0000373 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
374 printf ("Timeout\n");
375 return 1;
376 }
377 /* show that we're waiting */
378 if ((now - last) > 1000) { /* every second */
379 serial_putc ('.');
380 last = now;
381 }
wdenke69b4b82002-09-17 21:26:59 +0000382 }
383
384DONE:
385 /* reset to read mode */
386 addr = (FLASH_WORD_SIZE *)info->start[0];
387 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
388
389 printf (" done\n");
390 return rc;
391}
392
393/*----------------------------------------------------------------------------*/
394/*
395 * Copy memory to flash, returns:
396 * 0 - OK
397 * 1 - write timeout
398 * 2 - Flash not erased
399 */
400int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
401{
402 ulong cp, wp, data;
403 int i, l, rc;
404
405 wp = (addr & ~3); /* get lower word aligned address */
406
407 /*
408 * handle unaligned start bytes
409 */
410 if ((l = addr - wp) != 0) {
wdenk57b2d802003-06-27 21:31:46 +0000411 data = 0;
412 for (i=0, cp=wp; i<l; ++i, ++cp) {
413 data = (data << 8) | (*(uchar *)cp);
414 }
415 for (; i<4 && cnt>0; ++i) {
416 data = (data << 8) | *src++;
417 --cnt;
418 ++cp;
419 }
420 for (; cnt==0 && i<4; ++i, ++cp) {
421 data = (data << 8) | (*(uchar *)cp);
422 }
wdenke69b4b82002-09-17 21:26:59 +0000423
wdenk57b2d802003-06-27 21:31:46 +0000424 if ((rc = write_word(info, wp, data)) != 0) {
425 return (rc);
426 }
427 wp += 4;
wdenke69b4b82002-09-17 21:26:59 +0000428 }
429
430 /*
431 * handle word aligned part
432 */
433 while (cnt >= 4) {
wdenk57b2d802003-06-27 21:31:46 +0000434 data = 0;
435 for (i=0; i<4; ++i) {
436 data = (data << 8) | *src++;
437 }
438 if ((rc = write_word(info, wp, data)) != 0) {
439 return (rc);
440 }
441 wp += 4;
442 cnt -= 4;
wdenke69b4b82002-09-17 21:26:59 +0000443 }
444
445 if (cnt == 0) {
wdenk57b2d802003-06-27 21:31:46 +0000446 return (0);
wdenke69b4b82002-09-17 21:26:59 +0000447 }
448
449 /*
450 * handle unaligned tail bytes
451 */
452 data = 0;
453 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
wdenk57b2d802003-06-27 21:31:46 +0000454 data = (data << 8) | *src++;
455 --cnt;
wdenke69b4b82002-09-17 21:26:59 +0000456 }
457 for (; i<4; ++i, ++cp) {
wdenk57b2d802003-06-27 21:31:46 +0000458 data = (data << 8) | (*(uchar *)cp);
wdenke69b4b82002-09-17 21:26:59 +0000459 }
460
461 return (write_word(info, wp, data));
462}
463
464/*----------------------------------------------------------------------------*/
465/* Write a word to Flash, returns:
466 * 0 - OK
467 * 1 - write timeout
468 * 2 - Flash not erased
469 */
470static int write_word (flash_info_t *info, ulong dest, ulong data)
471{
wdenk57b2d802003-06-27 21:31:46 +0000472 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
473 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
474 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
wdenke69b4b82002-09-17 21:26:59 +0000475 ulong start;
476 int flag;
wdenk57b2d802003-06-27 21:31:46 +0000477 int i;
wdenke69b4b82002-09-17 21:26:59 +0000478
479 /* Check if Flash is (sufficiently) erased */
480 if ((*((volatile FLASH_WORD_SIZE *)dest) &
wdenk57b2d802003-06-27 21:31:46 +0000481 (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
482 return (2);
wdenke69b4b82002-09-17 21:26:59 +0000483 }
484 /* Disable interrupts which might cause a timeout here */
485 flag = disable_interrupts();
486
wdenk57b2d802003-06-27 21:31:46 +0000487 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
488 {
489 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
490 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
491 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
wdenke69b4b82002-09-17 21:26:59 +0000492
wdenk57b2d802003-06-27 21:31:46 +0000493 dest2[i] = data2[i];
wdenke69b4b82002-09-17 21:26:59 +0000494
wdenk57b2d802003-06-27 21:31:46 +0000495 /* re-enable interrupts if necessary */
496 if (flag)
497 enable_interrupts();
wdenke69b4b82002-09-17 21:26:59 +0000498
wdenk57b2d802003-06-27 21:31:46 +0000499 /* data polling for D7 */
500 start = get_timer (0);
501 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
502 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
503 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
504 return (1);
505 }
506 }
507 }
wdenke69b4b82002-09-17 21:26:59 +0000508
509 return (0);
510}
511
512/*----------------------------------------------------------------------------*/