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
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,
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 */
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)
46 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
49 flash_get_size ((long *) PHYS_FLASH_1, &flash_info[i]);
50 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
53 flash_get_size ((long *) PHYS_FLASH_2, &flash_info[i]);
54 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
57 panic ("configured too many flash banks!\n");
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] );
70 /*-----------------------------------------------------------------------
72 static void flash_get_offsets (ulong base, flash_info_t *info)
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);
86 /*-----------------------------------------------------------------------
88 void flash_print_info (flash_info_t *info)
92 if (info->flash_id == FLASH_UNKNOWN) {
93 printf ("missing or unknown FLASH type\n");
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_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
106 case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
108 case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
110 case FLASH_AMLV320B: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
112 default: printf ("Unknown Chip Type\n");
116 printf (" Size: %ld MB in %d Sectors\n",
117 info->size >> 20, info->sector_count);
119 printf (" Sector Start Addresses:");
120 for (i=0; i<info->sector_count; ++i) {
125 info->protect[i] ? " (RO)" : " "
133 * The following code cannot be run from FLASH!
135 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
139 ulong base = (ulong)addr;
141 /* Write auto select command: read Manufacturer ID */
142 addr[0x0555] = 0x00AA00AA;
143 addr[0x02AA] = 0x00550055;
144 addr[0x0555] = 0x00900090;
148 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
152 debug ("Manufacturer: AMD\n");
153 info->flash_id = FLASH_MAN_AMD;
156 debug ("Manufacturer: FUJITSU\n");
157 info->flash_id = FLASH_MAN_FUJ;
160 debug ("Manufacturer: *** unknown ***\n");
161 info->flash_id = FLASH_UNKNOWN;
162 info->sector_count = 0;
164 return (0); /* no or unknown flash */
167 value = addr[1]; /* device ID */
169 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
174 debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
177 case AMD_ID_LV128U_2:
178 if (addr[15] != AMD_ID_LV128U_3) {
179 debug ("Chip: AMLV128U -> unknown\n");
180 info->flash_id = FLASH_UNKNOWN;
182 debug ("Chip: AMLV128U\n");
183 info->flash_id += FLASH_AMLV128U;
184 info->sector_count = 256;
185 info->size = 0x02000000;
187 break; /* => 32 MB */
188 case AMD_ID_LV640U_2:
189 if (addr[15] != AMD_ID_LV640U_3) {
190 debug ("Chip: AMLV640U -> unknown\n");
191 info->flash_id = FLASH_UNKNOWN;
193 debug ("Chip: AMLV640U\n");
194 info->flash_id += FLASH_AMLV640U;
195 info->sector_count = 128;
196 info->size = 0x01000000;
198 break; /* => 16 MB */
199 case AMD_ID_LV320B_2:
200 if (addr[15] != AMD_ID_LV320B_3) {
201 debug ("Chip: AMLV320B -> unknown\n");
202 info->flash_id = FLASH_UNKNOWN;
204 debug ("Chip: AMLV320B\n");
205 info->flash_id += FLASH_AMLV320B;
206 info->sector_count = 71;
207 info->size = 0x00800000;
211 debug ("Chip: *** unknown ***\n");
212 info->flash_id = FLASH_UNKNOWN;
218 info->flash_id = FLASH_UNKNOWN;
219 return (0); /* => no or unknown flash */
222 /* set up sector start address table */
225 switch (info->flash_id & FLASH_TYPEMASK) {
226 /* only known types here - no default */
230 for (i = 0; i < info->sector_count; i++) {
231 info->start[i] = base;
236 for (i = 0; i < info->sector_count; i++) {
237 info->start[i] = base;
239 * The first 8 sectors are 8 kB,
240 * all the other ones are 64 kB
256 /* check for protected sectors */
257 for (i = 0; i < info->sector_count; i++) {
258 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
259 /* D0 = 1 if protected */
260 addr = (volatile unsigned long *)(info->start[i]);
261 info->protect[i] = addr[2] & 1;
266 * Prevent writes to uninitialized FLASH.
268 if (info->flash_id != FLASH_UNKNOWN) {
269 addr = (volatile unsigned long *)info->start[0];
271 *addr = 0x00F000F0; /* reset bank */
278 /*-----------------------------------------------------------------------
281 int flash_erase (flash_info_t *info, int s_first, int s_last)
283 vu_long *addr = (vu_long*)(info->start[0]);
284 int flag, prot, sect, l_sect;
285 ulong start, now, last;
287 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
289 if ((s_first < 0) || (s_first > s_last)) {
290 if (info->flash_id == FLASH_UNKNOWN) {
291 printf ("- missing\n");
293 printf ("- no sectors to erase\n");
298 if ((info->flash_id == FLASH_UNKNOWN) ||
299 (info->flash_id > FLASH_AMD_COMP)) {
300 printf ("Can't erase unknown flash type %08lx - aborted\n",
306 for (sect=s_first; sect<=s_last; ++sect) {
307 if (info->protect[sect]) {
313 printf ("- Warning: %d protected sectors will not be erased!\n",
321 /* Disable interrupts which might cause a timeout here */
322 flag = disable_interrupts();
324 addr[0x0555] = 0x00AA00AA;
325 addr[0x02AA] = 0x00550055;
326 addr[0x0555] = 0x00800080;
327 addr[0x0555] = 0x00AA00AA;
328 addr[0x02AA] = 0x00550055;
330 /* Start erase on unprotected sectors */
331 for (sect = s_first; sect<=s_last; sect++) {
332 if (info->protect[sect] == 0) { /* not protected */
333 addr = (vu_long*)(info->start[sect]);
334 addr[0] = 0x00300030;
339 /* re-enable interrupts if necessary */
343 /* wait at least 80us - let's wait 1 ms */
347 * We wait for the last triggered sector
352 start = get_timer (0);
354 addr = (vu_long*)(info->start[l_sect]);
355 while ((addr[0] & 0x00800080) != 0x00800080) {
356 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
357 printf ("Timeout\n");
360 /* show that we're waiting */
361 if ((now - last) > 100000) { /* every second */
368 /* reset to read mode */
369 addr = (volatile unsigned long *)info->start[0];
370 addr[0] = 0x00F000F0; /* reset bank */
376 /*-----------------------------------------------------------------------
377 * Copy memory to flash, returns:
380 * 2 - Flash not erased
383 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
388 wp = (addr & ~3); /* get lower word aligned address */
391 * handle unaligned start bytes
393 if ((l = addr - wp) != 0) {
395 for (i=0, cp=wp; i<l; ++i, ++cp) {
396 data = (data << 8) | (*(uchar *)cp);
398 for (; i<4 && cnt>0; ++i) {
399 data = (data << 8) | *src++;
403 for (; cnt==0 && i<4; ++i, ++cp) {
404 data = (data << 8) | (*(uchar *)cp);
407 if ((rc = write_word(info, wp, SWAP(data))) != 0) {
414 * handle word aligned part
418 for (i=0; i<4; ++i) {
419 data = (data << 8) | *src++;
421 if ((rc = write_word(info, wp, SWAP(data))) != 0) {
433 * handle unaligned tail bytes
436 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
437 data = (data << 8) | *src++;
440 for (; i<4; ++i, ++cp) {
441 data = (data << 8) | (*(uchar *)cp);
444 return (write_word(info, wp, SWAP(data)));
447 /*-----------------------------------------------------------------------
448 * Write a word to Flash, returns:
451 * 2 - Flash not erased
453 static int write_word (flash_info_t *info, ulong dest, ulong data)
455 vu_long *addr = (vu_long*)(info->start[0]);
459 /* Check if Flash is (sufficiently) erased */
460 if ((*((vu_long *)dest) & data) != data) {
464 /* Disable interrupts which might cause a timeout here */
465 flag = disable_interrupts();
467 addr[0x0555] = 0x00AA00AA;
468 addr[0x02AA] = 0x00550055;
469 addr[0x0555] = 0x00A000A0;
471 *((vu_long *)dest) = data;
473 /* re-enable interrupts if necessary */
477 /* data polling for D7 */
478 start = get_timer (0);
479 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
480 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {