]> git.ipfire.org Git - people/ms/u-boot.git/blame - board/eltec/elppc/flash.c
rename CFG_ macros to CONFIG_SYS
[people/ms/u-boot.git] / board / eltec / elppc / flash.c
CommitLineData
e69b4b8f
WD
1/*
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
6d0f6bcf 37flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
e69b4b8f
WD
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 */
6d0f6bcf 58 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i)
e69b4b8f 59 {
8bde7f77 60 flash_info[i].flash_id = FLASH_UNKNOWN;
e69b4b8f
WD
61 }
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 {
8bde7f77
WD
68 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
69 size1, size1<<20);
e69b4b8f
WD
70 }
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 {
8bde7f77
WD
77 printf ("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
78 size2, size2<<20);
e69b4b8f
WD
79 }
80
81 /* monitor protection ON by default */
82 if (size1 == 512*1024)
83 {
8bde7f77
WD
84 (void)flash_protect(FLAG_PROTECT_SET,
85 FLASH_BASE0_PRELIM,
86 FLASH_BASE0_PRELIM+monitor_flash_len-1,
87 &flash_info[0]);
e69b4b8f
WD
88 }
89 if (size2 == 512*1024)
90 {
8bde7f77
WD
91 (void)flash_protect(FLAG_PROTECT_SET,
92 FLASH_BASE1_PRELIM,
93 FLASH_BASE1_PRELIM+monitor_flash_len-1,
94 &flash_info[1]);
e69b4b8f
WD
95 }
96 if (size2 == 4*1024*1024)
97 {
8bde7f77 98 (void)flash_protect(FLAG_PROTECT_SET,
6d0f6bcf
JCPV
99 CONFIG_SYS_FLASH_BASE,
100 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
8bde7f77 101 &flash_info[1]);
e69b4b8f
WD
102 }
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) {
8bde7f77
WD
118 printf ("missing or unknown FLASH type\n");
119 flash_init();
e69b4b8f
WD
120 }
121
122 if (info->flash_id == FLASH_UNKNOWN) {
8bde7f77
WD
123 printf ("missing or unknown FLASH type\n");
124 return;
e69b4b8f
WD
125 }
126
127 switch (info->flash_id & FLASH_VENDMASK) {
128 case FLASH_MAN_AMD:
8bde7f77
WD
129 printf ("AMD ");
130 break;
e69b4b8f 131 default:
8bde7f77
WD
132 printf ("Unknown Vendor ");
133 break;
e69b4b8f
WD
134 }
135
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case AMD_ID_F040B:
8bde7f77
WD
138 printf ("AM29F040B (4 Mbit)\n");
139 break;
e69b4b8f 140 case AMD_ID_F016D:
8bde7f77
WD
141 printf ("AM29F016D (16 Mbit)\n");
142 break;
e69b4b8f 143 case AMD_ID_F032B:
8bde7f77
WD
144 printf ("AM29F032B (32 Mbit)\n");
145 break;
e69b4b8f 146 default:
8bde7f77
WD
147 printf ("Unknown Chip Type\n");
148 break;
e69b4b8f
WD
149 }
150
151 if (info->size >= (1 << 20)) {
8bde7f77 152 printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
e69b4b8f 153 } else {
8bde7f77 154 printf (" Size: %ld kB in %d Sectors\n", info->size >> 10, info->sector_count);
e69b4b8f
WD
155 }
156
157 printf (" Sector Start Addresses:");
158 for (i=0; i<info->sector_count; ++i) {
8bde7f77
WD
159 /*
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];
166
167 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 }
176
177 if ((i % 5) == 0)
178 printf ("\n ");
179
180 printf (" %08lX%s%s",
181 info->start[i],
182 erased ? " E" : " ",
183 info->protect[i] ? "RO " : " ");
e69b4b8f
WD
184 }
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:
8bde7f77
WD
229 info->flash_id = FLASH_MAN_AMD;
230 break;
e69b4b8f 231 default:
8bde7f77
WD
232 info->flash_id = FLASH_UNKNOWN;
233 info->sector_count = 0;
234 info->size = 0;
235 return (0); /* no or unknown flash */
e69b4b8f
WD
236 }
237
238 switch (devid) {
239 case (FLASH_WORD_SIZE)AMD_ID_F040B:
8bde7f77
WD
240 info->flash_id |= AMD_ID_F040B;
241 info->sector_count = 8;
242 info->size = 0x00080000;
243 break; /* => 0.5 MB */
e69b4b8f
WD
244
245 case (FLASH_WORD_SIZE)AMD_ID_F016D:
8bde7f77
WD
246 info->flash_id |= AMD_ID_F016D;
247 info->sector_count = 32;
248 info->size = 0x00200000;
249 break; /* => 2 MB */
e69b4b8f
WD
250
251 case (FLASH_WORD_SIZE)AMD_ID_F032B:
8bde7f77
WD
252 info->flash_id |= AMD_ID_F032B;
253 info->sector_count = 64;
254 info->size = 0x00400000;
255 break; /* => 4 MB */
e69b4b8f
WD
256
257 default:
8bde7f77
WD
258 info->flash_id = FLASH_UNKNOWN;
259 return (0); /* => no or unknown flash */
e69b4b8f
WD
260
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++) {
8bde7f77
WD
269 /* 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;
e69b4b8f
WD
275 }
276
277 /*
278 * Prevent writes to uninitialized FLASH.
279 */
280 if (info->flash_id != FLASH_UNKNOWN) {
8bde7f77
WD
281 caddr = (volatile unsigned char *)info->start[0];
282 caddr[0] = 0xF0; /* reset bank */
e69b4b8f
WD
283 }
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)) {
8bde7f77
WD
298 if (info->flash_id == FLASH_UNKNOWN) {
299 printf ("- missing\n");
300 } else {
301 printf ("- no sectors to erase\n");
302 }
303 return 1;
e69b4b8f
WD
304 }
305
306 if ((info->flash_id == FLASH_UNKNOWN) ||
8bde7f77
WD
307 (info->flash_id > FLASH_AMD_COMP)) {
308 printf ("Can't erase unknown flash type - aborted\n");
309 return 1;
e69b4b8f
WD
310 }
311
312 prot = 0;
313 for (sect=s_first; sect<=s_last; ++sect) {
8bde7f77
WD
314 if (info->protect[sect]) {
315 prot++;
316 }
e69b4b8f
WD
317 }
318
319 if (prot) {
8bde7f77
WD
320 printf ("- Warning: %d protected sectors will not be erased!\n",
321 prot);
e69b4b8f 322 } else {
8bde7f77 323 printf ("\n");
e69b4b8f
WD
324 }
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++) {
8bde7f77
WD
339 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 }
e69b4b8f
WD
354 }
355
356 /* re-enable interrupts if necessary */
357 if (flag)
8bde7f77 358 enable_interrupts();
e69b4b8f
WD
359
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)
8bde7f77 367 goto DONE;
e69b4b8f
WD
368
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) {
6d0f6bcf 373 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
8bde7f77
WD
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 }
e69b4b8f
WD
382 }
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) {
8bde7f77
WD
411 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 }
423
424 if ((rc = write_word(info, wp, data)) != 0) {
425 return (rc);
426 }
427 wp += 4;
e69b4b8f
WD
428 }
429
430 /*
431 * handle word aligned part
432 */
433 while (cnt >= 4) {
8bde7f77
WD
434 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;
e69b4b8f
WD
443 }
444
445 if (cnt == 0) {
8bde7f77 446 return (0);
e69b4b8f
WD
447 }
448
449 /*
450 * handle unaligned tail bytes
451 */
452 data = 0;
453 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
8bde7f77
WD
454 data = (data << 8) | *src++;
455 --cnt;
e69b4b8f
WD
456 }
457 for (; i<4; ++i, ++cp) {
8bde7f77 458 data = (data << 8) | (*(uchar *)cp);
e69b4b8f
WD
459 }
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{
8bde7f77
WD
472 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;
e69b4b8f
WD
475 ulong start;
476 int flag;
8bde7f77 477 int i;
e69b4b8f
WD
478
479 /* Check if Flash is (sufficiently) erased */
480 if ((*((volatile FLASH_WORD_SIZE *)dest) &
8bde7f77
WD
481 (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
482 return (2);
e69b4b8f
WD
483 }
484 /* Disable interrupts which might cause a timeout here */
485 flag = disable_interrupts();
486
8bde7f77
WD
487 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;
e69b4b8f 492
8bde7f77 493 dest2[i] = data2[i];
e69b4b8f 494
8bde7f77
WD
495 /* re-enable interrupts if necessary */
496 if (flag)
497 enable_interrupts();
e69b4b8f 498
8bde7f77
WD
499 /* data polling for D7 */
500 start = get_timer (0);
501 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
502 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
6d0f6bcf 503 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
8bde7f77
WD
504 return (1);
505 }
506 }
507 }
e69b4b8f
WD
508
509 return (0);
510}
511
512/*----------------------------------------------------------------------------*/