gpb-shadow.patch
[u-boot-openmoko/mini2440.git] / board / xsengine / flash.c
bloba188e244089e0e1a71e5e5c35ed40498fe324610
1 /*
2 * (C) Copyright 2002
3 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
5 * (C) Copyright 2000-2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
9 * project.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
27 #include <common.h>
28 #include <linux/byteorder/swab.h>
30 #define SWAP(x) __swab32(x)
32 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
34 /* Functions */
35 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
36 static int write_word (flash_info_t *info, ulong dest, ulong data);
37 static void flash_get_offsets (ulong base, flash_info_t *info);
39 /*-----------------------------------------------------------------------
41 unsigned long flash_init (void)
43 int i;
44 ulong size = 0;
46 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
47 switch (i) {
48 case 0:
49 flash_get_size ((vu_long *) PHYS_FLASH_1, &flash_info[i]);
50 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
51 break;
52 case 1:
53 flash_get_size ((vu_long *) PHYS_FLASH_2, &flash_info[i]);
54 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
55 break;
56 default:
57 panic ("configured too many flash banks!\n");
58 break;
60 size += flash_info[i].size;
63 /* Protect monitor and environment sectors */
64 flash_protect ( FLAG_PROTECT_SET,CFG_FLASH_BASE,CFG_FLASH_BASE + monitor_flash_len - 1,&flash_info[0] );
65 flash_protect ( FLAG_PROTECT_SET,CFG_ENV_ADDR,CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] );
67 return size;
70 /*-----------------------------------------------------------------------
72 static void flash_get_offsets (ulong base, flash_info_t *info)
74 int i;
76 if (info->flash_id == FLASH_UNKNOWN) return;
78 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
79 for (i = 0; i < info->sector_count; i++) {
80 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
81 info->protect[i] = 0;
86 /*-----------------------------------------------------------------------
88 void flash_print_info (flash_info_t *info)
90 int i;
92 if (info->flash_id == FLASH_UNKNOWN) {
93 printf ("missing or unknown FLASH type\n");
94 return;
97 switch (info->flash_id & FLASH_VENDMASK) {
98 case FLASH_MAN_AMD: printf ("AMD "); break;
99 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
100 default: printf ("Unknown Vendor "); break;
103 switch (info->flash_id & FLASH_TYPEMASK) {
104 case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
105 break;
106 case FLASH_S29GL064M: printf ("S29GL064M (64Mbit, top boot sector size)\n");
107 break;
108 default: printf ("Unknown Chip Type\n");
109 break;
112 printf (" Size: %ld MB in %d Sectors\n",
113 info->size >> 20, info->sector_count);
115 printf (" Sector Start Addresses:");
116 for (i=0; i<info->sector_count; ++i) {
117 if ((i % 5) == 0)
118 printf ("\n ");
119 printf (" %08lX%s",
120 info->start[i],
121 info->protect[i] ? " (RO)" : " "
124 printf ("\n");
125 return;
129 * The following code cannot be run from FLASH!
131 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
133 short i;
134 ulong value;
135 ulong base = (ulong)addr;
137 /* Write auto select command: read Manufacturer ID */
138 addr[0x0555] = 0x00AA00AA;
139 addr[0x02AA] = 0x00550055;
140 addr[0x0555] = 0x00900090;
142 value = addr[0];
144 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
146 switch (value) {
147 case AMD_MANUFACT:
148 debug ("Manufacturer: AMD\n");
149 info->flash_id = FLASH_MAN_AMD;
150 break;
151 case FUJ_MANUFACT:
152 debug ("Manufacturer: FUJITSU\n");
153 info->flash_id = FLASH_MAN_FUJ;
154 break;
155 default:
156 debug ("Manufacturer: *** unknown ***\n");
157 info->flash_id = FLASH_UNKNOWN;
158 info->sector_count = 0;
159 info->size = 0;
160 return (0); /* no or unknown flash */
163 value = addr[1]; /* device ID */
165 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
167 switch (value) {
169 case AMD_ID_MIRROR:
170 debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
171 addr[14], addr[15]);
172 switch(addr[14]) {
173 case AMD_ID_LV640U_2:
174 if (addr[15] != AMD_ID_LV640U_3) {
175 debug ("Chip: AMLV640U -> unknown\n");
176 info->flash_id = FLASH_UNKNOWN;
177 } else {
178 debug ("Chip: AMLV640U\n");
179 info->flash_id += FLASH_AMLV640U;
180 info->sector_count = 128;
181 info->size = 0x01000000;
183 break; /* => 16 MB */
184 case AMD_ID_GL064MT_2:
185 if (addr[15] != AMD_ID_GL064MT_3) {
186 debug ("Chip: S29GL064M-R3 -> unknown\n");
187 info->flash_id = FLASH_UNKNOWN;
188 } else {
189 debug ("Chip: S29GL064M-R3\n");
190 info->flash_id += FLASH_S29GL064M;
191 info->sector_count = 128;
192 info->size = 0x01000000;
194 break; /* => 16 MB */
195 default:
196 debug ("Chip: *** unknown ***\n");
197 info->flash_id = FLASH_UNKNOWN;
198 break;
200 break;
202 default:
203 info->flash_id = FLASH_UNKNOWN;
204 return (0); /* => no or unknown flash */
207 /* set up sector start address table */
208 switch (value) {
209 case AMD_ID_MIRROR:
210 switch (info->flash_id & FLASH_TYPEMASK) {
211 /* only known types here - no default */
212 case FLASH_AMLV128U:
213 case FLASH_AMLV640U:
214 case FLASH_AMLV320U:
215 for (i = 0; i < info->sector_count; i++) {
216 info->start[i] = base;
217 base += 0x20000;
219 break;
220 case FLASH_AMLV320B:
221 for (i = 0; i < info->sector_count; i++) {
222 info->start[i] = base;
224 * The first 8 sectors are 8 kB,
225 * all the other ones are 64 kB
227 base += (i < 8)
228 ? 2 * ( 8 << 10)
229 : 2 * (64 << 10);
231 break;
233 break;
235 default:
236 return (0);
237 break;
240 #if 0
241 /* check for protected sectors */
242 for (i = 0; i < info->sector_count; i++) {
243 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
244 /* D0 = 1 if protected */
245 addr = (volatile unsigned long *)(info->start[i]);
246 info->protect[i] = addr[2] & 1;
248 #endif
251 * Prevent writes to uninitialized FLASH.
253 if (info->flash_id != FLASH_UNKNOWN) {
254 addr = (volatile unsigned long *)info->start[0];
256 *addr = 0x00F000F0; /* reset bank */
259 return (info->size);
263 /*-----------------------------------------------------------------------
266 int flash_erase (flash_info_t *info, int s_first, int s_last)
268 vu_long *addr = (vu_long*)(info->start[0]);
269 int flag, prot, sect, l_sect;
270 ulong start, now, last;
272 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
274 if ((s_first < 0) || (s_first > s_last)) {
275 if (info->flash_id == FLASH_UNKNOWN) {
276 printf ("- missing\n");
277 } else {
278 printf ("- no sectors to erase\n");
280 return 1;
283 if ((info->flash_id == FLASH_UNKNOWN) ||
284 (info->flash_id > FLASH_AMD_COMP)) {
285 printf ("Can't erase unknown flash type %08lx - aborted\n",
286 info->flash_id);
287 return 1;
290 prot = 0;
291 for (sect=s_first; sect<=s_last; ++sect) {
292 if (info->protect[sect]) {
293 prot++;
297 if (prot) {
298 printf ("- Warning: %d protected sectors will not be erased!\n",
299 prot);
300 } else {
301 printf ("\n");
304 l_sect = -1;
306 /* Disable interrupts which might cause a timeout here */
307 flag = disable_interrupts();
309 addr[0x0555] = 0x00AA00AA;
310 addr[0x02AA] = 0x00550055;
311 addr[0x0555] = 0x00800080;
312 addr[0x0555] = 0x00AA00AA;
313 addr[0x02AA] = 0x00550055;
315 /* Start erase on unprotected sectors */
316 for (sect = s_first; sect<=s_last; sect++) {
317 if (info->protect[sect] == 0) { /* not protected */
318 addr = (vu_long*)(info->start[sect]);
319 addr[0] = 0x00300030;
320 l_sect = sect;
324 /* re-enable interrupts if necessary */
325 if (flag)
326 enable_interrupts();
328 /* wait at least 80us - let's wait 1 ms */
329 udelay (1000);
332 * We wait for the last triggered sector
334 if (l_sect < 0)
335 goto DONE;
337 start = get_timer (0);
338 last = start;
339 addr = (vu_long*)(info->start[l_sect]);
340 while ((addr[0] & 0x00800080) != 0x00800080) {
341 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
342 printf ("Timeout\n");
343 return 1;
345 /* show that we're waiting */
346 if ((now - last) > 100000) { /* every second */
347 putc ('.');
348 last = now;
352 DONE:
353 /* reset to read mode */
354 addr = (volatile unsigned long *)info->start[0];
355 addr[0] = 0x00F000F0; /* reset bank */
357 printf (" done\n");
358 return 0;
361 /*-----------------------------------------------------------------------
362 * Copy memory to flash, returns:
363 * 0 - OK
364 * 1 - write timeout
365 * 2 - Flash not erased
368 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
370 ulong cp, wp, data;
371 int i, l, rc;
373 wp = (addr & ~3); /* get lower word aligned address */
376 * handle unaligned start bytes
378 if ((l = addr - wp) != 0) {
379 data = 0;
380 for (i=0, cp=wp; i<l; ++i, ++cp) {
381 data = (data << 8) | (*(uchar *)cp);
383 for (; i<4 && cnt>0; ++i) {
384 data = (data << 8) | *src++;
385 --cnt;
386 ++cp;
388 for (; cnt==0 && i<4; ++i, ++cp) {
389 data = (data << 8) | (*(uchar *)cp);
392 if ((rc = write_word(info, wp, SWAP(data))) != 0) {
393 return (rc);
395 wp += 4;
399 * handle word aligned part
401 while (cnt >= 4) {
402 data = 0;
403 for (i=0; i<4; ++i) {
404 data = (data << 8) | *src++;
406 if ((rc = write_word(info, wp, SWAP(data))) != 0) {
407 return (rc);
409 wp += 4;
410 cnt -= 4;
413 if (cnt == 0) {
414 return (0);
418 * handle unaligned tail bytes
420 data = 0;
421 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
422 data = (data << 8) | *src++;
423 --cnt;
425 for (; i<4; ++i, ++cp) {
426 data = (data << 8) | (*(uchar *)cp);
429 return (write_word(info, wp, SWAP(data)));
432 /*-----------------------------------------------------------------------
433 * Write a word to Flash, returns:
434 * 0 - OK
435 * 1 - write timeout
436 * 2 - Flash not erased
438 static int write_word (flash_info_t *info, ulong dest, ulong data)
440 vu_long *addr = (vu_long*)(info->start[0]);
441 ulong start;
442 int flag;
444 /* Check if Flash is (sufficiently) erased */
445 if ((*((vu_long *)dest) & data) != data) {
446 return (2);
449 /* Disable interrupts which might cause a timeout here */
450 flag = disable_interrupts();
452 addr[0x0555] = 0x00AA00AA;
453 addr[0x02AA] = 0x00550055;
454 addr[0x0555] = 0x00A000A0;
456 *((vu_long *)dest) = data;
458 /* re-enable interrupts if necessary */
459 if (flag)
460 enable_interrupts();
462 /* data polling for D7 */
463 start = get_timer (0);
464 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
465 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
466 return (1);
469 return (0);