blob: f29def268d10d0dd791ecfc77526ff2715b4a573 [file] [log] [blame]
wdenkca0e7742004-06-09 15:37:23 +00001/*
2 * (C) Copyright 2002
3 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
4 *
5 * (C) Copyright 2000-2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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
25 */
26
27#include <common.h>
28#include <linux/byteorder/swab.h>
29
wdenkca0e7742004-06-09 15:37:23 +000030#define SWAP(x) __swab32(x)
31
32flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
33
34/* Functions */
35static ulong flash_get_size (vu_long *addr, flash_info_t *info);
36static int write_word (flash_info_t *info, ulong dest, ulong data);
37static void flash_get_offsets (ulong base, flash_info_t *info);
38
wdenkca0e7742004-06-09 15:37:23 +000039/*-----------------------------------------------------------------------
40 */
41unsigned long flash_init (void)
42{
43 int i;
44 ulong size = 0;
45
46 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
47 switch (i) {
48 case 0:
Jean-Christophe PLAGNIOL-VILLARDe78220f2007-10-19 06:33:45 +020049 flash_get_size ((vu_long *) PHYS_FLASH_1, &flash_info[i]);
wdenkca0e7742004-06-09 15:37:23 +000050 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
51 break;
52 case 1:
Jean-Christophe PLAGNIOL-VILLARDe78220f2007-10-19 06:33:45 +020053 flash_get_size ((vu_long *) PHYS_FLASH_2, &flash_info[i]);
wdenkca0e7742004-06-09 15:37:23 +000054 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
55 break;
56 default:
57 panic ("configured too many flash banks!\n");
58 break;
59 }
60 size += flash_info[i].size;
61 }
62
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] );
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020065 flash_protect ( FLAG_PROTECT_SET,CONFIG_ENV_ADDR,CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0] );
wdenkca0e7742004-06-09 15:37:23 +000066
67 return size;
68}
69
70/*-----------------------------------------------------------------------
71 */
72static void flash_get_offsets (ulong base, flash_info_t *info)
73{
74 int i;
75
76 if (info->flash_id == FLASH_UNKNOWN) return;
77
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;
82 }
83 }
84}
85
86/*-----------------------------------------------------------------------
87 */
88void flash_print_info (flash_info_t *info)
89{
90 int i;
91
92 if (info->flash_id == FLASH_UNKNOWN) {
93 printf ("missing or unknown FLASH type\n");
94 return;
95 }
96
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;
101 }
102
103 switch (info->flash_id & FLASH_TYPEMASK) {
wdenkca0e7742004-06-09 15:37:23 +0000104 case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
105 break;
Wolfgang Denk45237bc2005-10-05 00:03:55 +0200106 case FLASH_S29GL064M: printf ("S29GL064M (64Mbit, top boot sector size)\n");
wdenkca0e7742004-06-09 15:37:23 +0000107 break;
108 default: printf ("Unknown Chip Type\n");
109 break;
110 }
111
112 printf (" Size: %ld MB in %d Sectors\n",
113 info->size >> 20, info->sector_count);
114
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)" : " "
122 );
123 }
124 printf ("\n");
125 return;
126}
127
128/*
129 * The following code cannot be run from FLASH!
130 */
131static ulong flash_get_size (vu_long *addr, flash_info_t *info)
132{
133 short i;
134 ulong value;
135 ulong base = (ulong)addr;
136
137 /* Write auto select command: read Manufacturer ID */
138 addr[0x0555] = 0x00AA00AA;
139 addr[0x02AA] = 0x00550055;
140 addr[0x0555] = 0x00900090;
141
142 value = addr[0];
143
144 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
145
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 */
161 }
162
163 value = addr[1]; /* device ID */
164
165 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
166
167 switch (value) {
168
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]) {
wdenkca0e7742004-06-09 15:37:23 +0000173 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;
182 }
183 break; /* => 16 MB */
Wolfgang Denk45237bc2005-10-05 00:03:55 +0200184 case AMD_ID_GL064MT_2:
185 if (addr[15] != AMD_ID_GL064MT_3) {
186 debug ("Chip: S29GL064M-R3 -> unknown\n");
wdenkca0e7742004-06-09 15:37:23 +0000187 info->flash_id = FLASH_UNKNOWN;
188 } else {
Wolfgang Denk45237bc2005-10-05 00:03:55 +0200189 debug ("Chip: S29GL064M-R3\n");
190 info->flash_id += FLASH_S29GL064M;
191 info->sector_count = 128;
192 info->size = 0x01000000;
wdenkca0e7742004-06-09 15:37:23 +0000193 }
Wolfgang Denk45237bc2005-10-05 00:03:55 +0200194 break; /* => 16 MB */
wdenkca0e7742004-06-09 15:37:23 +0000195 default:
196 debug ("Chip: *** unknown ***\n");
197 info->flash_id = FLASH_UNKNOWN;
198 break;
199 }
200 break;
201
202 default:
203 info->flash_id = FLASH_UNKNOWN;
204 return (0); /* => no or unknown flash */
205 }
206
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;
218 }
219 break;
220 case FLASH_AMLV320B:
221 for (i = 0; i < info->sector_count; i++) {
222 info->start[i] = base;
223 /*
224 * The first 8 sectors are 8 kB,
225 * all the other ones are 64 kB
226 */
227 base += (i < 8)
228 ? 2 * ( 8 << 10)
229 : 2 * (64 << 10);
230 }
231 break;
232 }
233 break;
234
235 default:
236 return (0);
237 break;
238 }
239
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;
247 }
248#endif
249
250 /*
251 * Prevent writes to uninitialized FLASH.
252 */
253 if (info->flash_id != FLASH_UNKNOWN) {
254 addr = (volatile unsigned long *)info->start[0];
255
256 *addr = 0x00F000F0; /* reset bank */
257 }
258
259 return (info->size);
260}
261
262
263/*-----------------------------------------------------------------------
264 */
265
266int flash_erase (flash_info_t *info, int s_first, int s_last)
267{
268 vu_long *addr = (vu_long*)(info->start[0]);
269 int flag, prot, sect, l_sect;
270 ulong start, now, last;
271
272 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
273
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");
279 }
280 return 1;
281 }
282
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;
288 }
289
290 prot = 0;
291 for (sect=s_first; sect<=s_last; ++sect) {
292 if (info->protect[sect]) {
293 prot++;
294 }
295 }
296
297 if (prot) {
298 printf ("- Warning: %d protected sectors will not be erased!\n",
299 prot);
300 } else {
301 printf ("\n");
302 }
303
304 l_sect = -1;
305
306 /* Disable interrupts which might cause a timeout here */
307 flag = disable_interrupts();
308
309 addr[0x0555] = 0x00AA00AA;
310 addr[0x02AA] = 0x00550055;
311 addr[0x0555] = 0x00800080;
312 addr[0x0555] = 0x00AA00AA;
313 addr[0x02AA] = 0x00550055;
314
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;
321 }
322 }
323
324 /* re-enable interrupts if necessary */
325 if (flag)
326 enable_interrupts();
327
328 /* wait at least 80us - let's wait 1 ms */
329 udelay (1000);
330
331 /*
332 * We wait for the last triggered sector
333 */
334 if (l_sect < 0)
335 goto DONE;
336
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;
344 }
345 /* show that we're waiting */
346 if ((now - last) > 100000) { /* every second */
347 putc ('.');
348 last = now;
349 }
350 }
351
352DONE:
353 /* reset to read mode */
354 addr = (volatile unsigned long *)info->start[0];
355 addr[0] = 0x00F000F0; /* reset bank */
356
357 printf (" done\n");
358 return 0;
359}
360
361/*-----------------------------------------------------------------------
362 * Copy memory to flash, returns:
363 * 0 - OK
364 * 1 - write timeout
365 * 2 - Flash not erased
366 */
367
368int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
369{
370 ulong cp, wp, data;
371 int i, l, rc;
372
373 wp = (addr & ~3); /* get lower word aligned address */
374
375 /*
376 * handle unaligned start bytes
377 */
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);
382 }
383 for (; i<4 && cnt>0; ++i) {
384 data = (data << 8) | *src++;
385 --cnt;
386 ++cp;
387 }
388 for (; cnt==0 && i<4; ++i, ++cp) {
389 data = (data << 8) | (*(uchar *)cp);
390 }
391
392 if ((rc = write_word(info, wp, SWAP(data))) != 0) {
393 return (rc);
394 }
395 wp += 4;
396 }
397
398 /*
399 * handle word aligned part
400 */
401 while (cnt >= 4) {
402 data = 0;
403 for (i=0; i<4; ++i) {
404 data = (data << 8) | *src++;
405 }
406 if ((rc = write_word(info, wp, SWAP(data))) != 0) {
407 return (rc);
408 }
409 wp += 4;
410 cnt -= 4;
411 }
412
413 if (cnt == 0) {
414 return (0);
415 }
416
417 /*
418 * handle unaligned tail bytes
419 */
420 data = 0;
421 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
422 data = (data << 8) | *src++;
423 --cnt;
424 }
425 for (; i<4; ++i, ++cp) {
426 data = (data << 8) | (*(uchar *)cp);
427 }
428
429 return (write_word(info, wp, SWAP(data)));
430}
431
432/*-----------------------------------------------------------------------
433 * Write a word to Flash, returns:
434 * 0 - OK
435 * 1 - write timeout
436 * 2 - Flash not erased
437 */
438static int write_word (flash_info_t *info, ulong dest, ulong data)
439{
440 vu_long *addr = (vu_long*)(info->start[0]);
441 ulong start;
wdenkca0e7742004-06-09 15:37:23 +0000442 int flag;
wdenkca0e7742004-06-09 15:37:23 +0000443
444 /* Check if Flash is (sufficiently) erased */
445 if ((*((vu_long *)dest) & data) != data) {
446 return (2);
447 }
448
449 /* Disable interrupts which might cause a timeout here */
450 flag = disable_interrupts();
451
452 addr[0x0555] = 0x00AA00AA;
453 addr[0x02AA] = 0x00550055;
454 addr[0x0555] = 0x00A000A0;
455
456 *((vu_long *)dest) = data;
457
458 /* re-enable interrupts if necessary */
459 if (flag)
460 enable_interrupts();
461
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);
467 }
468 }
469 return (0);
470}