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 #if defined CFG_JFFS_CUSTOM_PART
31 #include <jffs2/jffs2.h>
34 #define SWAP(x) __swab32(x)
36 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
39 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
40 static int write_word (flash_info_t *info, ulong dest, ulong data);
41 static void flash_get_offsets (ulong base, flash_info_t *info);
43 #if defined CFG_JFFS_CUSTOM_PART
46 * jffs2_part_info - get information about a JFFS2 partition
48 * @part_num: number of the partition you want to get info about
49 * @return: struct part_info* in case of success, 0 if failure
52 static struct part_info part;
53 static int current_part = -1;
55 struct part_info* jffs2_part_info(int part_num) {
56 void *jffs2_priv_saved = part.jffs2_priv;
58 printf("jffs2_part_info: part_num=%i\n",part_num);
60 if (current_part == part_num)
63 /* u-boot partition */
65 memset(&part, 0, sizeof(part));
67 part.offset=(char*)0x00000000;
70 /* Mark the struct as ready */
71 current_part = part_num;
73 printf("part.offset = 0x%08x\n",(unsigned int)part.offset);
74 printf("part.size = 0x%08x\n",(unsigned int)part.size);
77 /* primary OS+firmware partition */
79 memset(&part, 0, sizeof(part));
81 part.offset=(char*)0x00040000;
84 /* Mark the struct as ready */
85 current_part = part_num;
87 printf("part.offset = 0x%08x\n",(unsigned int)part.offset);
88 printf("part.size = 0x%08x\n",(unsigned int)part.size);
91 /* secondary OS+firmware partition */
93 memset(&part, 0, sizeof(part));
95 part.offset=(char*)0x00140000;
96 part.size=8*1024*1024;
98 /* Mark the struct as ready */
99 current_part = part_num;
101 printf("part.offset = 0x%08x\n",(unsigned int)part.offset);
102 printf("part.size = 0x%08x\n",(unsigned int)part.size);
105 if (current_part == part_num) {
106 part.usr_priv = ¤t_part;
107 part.jffs2_priv = jffs2_priv_saved;
111 printf("jffs2_part_info: end of partition table\n");
117 /*-----------------------------------------------------------------------
119 unsigned long flash_init (void)
124 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
127 flash_get_size ((long *) PHYS_FLASH_1, &flash_info[i]);
128 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
131 flash_get_size ((long *) PHYS_FLASH_2, &flash_info[i]);
132 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
135 panic ("configured too many flash banks!\n");
138 size += flash_info[i].size;
141 /* Protect monitor and environment sectors */
142 flash_protect ( FLAG_PROTECT_SET,CFG_FLASH_BASE,CFG_FLASH_BASE + monitor_flash_len - 1,&flash_info[0] );
143 flash_protect ( FLAG_PROTECT_SET,CFG_ENV_ADDR,CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] );
148 /*-----------------------------------------------------------------------
150 static void flash_get_offsets (ulong base, flash_info_t *info)
154 if (info->flash_id == FLASH_UNKNOWN) return;
156 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
157 for (i = 0; i < info->sector_count; i++) {
158 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
159 info->protect[i] = 0;
164 /*-----------------------------------------------------------------------
166 void flash_print_info (flash_info_t *info)
170 if (info->flash_id == FLASH_UNKNOWN) {
171 printf ("missing or unknown FLASH type\n");
175 switch (info->flash_id & FLASH_VENDMASK) {
176 case FLASH_MAN_AMD: printf ("AMD "); break;
177 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
178 default: printf ("Unknown Vendor "); break;
181 switch (info->flash_id & FLASH_TYPEMASK) {
182 case FLASH_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
184 case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
186 case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
188 case FLASH_AMLV320B: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
190 default: printf ("Unknown Chip Type\n");
194 printf (" Size: %ld MB in %d Sectors\n",
195 info->size >> 20, info->sector_count);
197 printf (" Sector Start Addresses:");
198 for (i=0; i<info->sector_count; ++i) {
203 info->protect[i] ? " (RO)" : " "
211 * The following code cannot be run from FLASH!
213 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
217 ulong base = (ulong)addr;
219 /* Write auto select command: read Manufacturer ID */
220 addr[0x0555] = 0x00AA00AA;
221 addr[0x02AA] = 0x00550055;
222 addr[0x0555] = 0x00900090;
226 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
230 debug ("Manufacturer: AMD\n");
231 info->flash_id = FLASH_MAN_AMD;
234 debug ("Manufacturer: FUJITSU\n");
235 info->flash_id = FLASH_MAN_FUJ;
238 debug ("Manufacturer: *** unknown ***\n");
239 info->flash_id = FLASH_UNKNOWN;
240 info->sector_count = 0;
242 return (0); /* no or unknown flash */
245 value = addr[1]; /* device ID */
247 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
252 debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
255 case AMD_ID_LV128U_2:
256 if (addr[15] != AMD_ID_LV128U_3) {
257 debug ("Chip: AMLV128U -> unknown\n");
258 info->flash_id = FLASH_UNKNOWN;
260 debug ("Chip: AMLV128U\n");
261 info->flash_id += FLASH_AMLV128U;
262 info->sector_count = 256;
263 info->size = 0x02000000;
265 break; /* => 32 MB */
266 case AMD_ID_LV640U_2:
267 if (addr[15] != AMD_ID_LV640U_3) {
268 debug ("Chip: AMLV640U -> unknown\n");
269 info->flash_id = FLASH_UNKNOWN;
271 debug ("Chip: AMLV640U\n");
272 info->flash_id += FLASH_AMLV640U;
273 info->sector_count = 128;
274 info->size = 0x01000000;
276 break; /* => 16 MB */
277 case AMD_ID_LV320B_2:
278 if (addr[15] != AMD_ID_LV320B_3) {
279 debug ("Chip: AMLV320B -> unknown\n");
280 info->flash_id = FLASH_UNKNOWN;
282 debug ("Chip: AMLV320B\n");
283 info->flash_id += FLASH_AMLV320B;
284 info->sector_count = 71;
285 info->size = 0x00800000;
289 debug ("Chip: *** unknown ***\n");
290 info->flash_id = FLASH_UNKNOWN;
296 info->flash_id = FLASH_UNKNOWN;
297 return (0); /* => no or unknown flash */
300 /* set up sector start address table */
303 switch (info->flash_id & FLASH_TYPEMASK) {
304 /* only known types here - no default */
308 for (i = 0; i < info->sector_count; i++) {
309 info->start[i] = base;
314 for (i = 0; i < info->sector_count; i++) {
315 info->start[i] = base;
317 * The first 8 sectors are 8 kB,
318 * all the other ones are 64 kB
334 /* check for protected sectors */
335 for (i = 0; i < info->sector_count; i++) {
336 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
337 /* D0 = 1 if protected */
338 addr = (volatile unsigned long *)(info->start[i]);
339 info->protect[i] = addr[2] & 1;
344 * Prevent writes to uninitialized FLASH.
346 if (info->flash_id != FLASH_UNKNOWN) {
347 addr = (volatile unsigned long *)info->start[0];
349 *addr = 0x00F000F0; /* reset bank */
356 /*-----------------------------------------------------------------------
359 int flash_erase (flash_info_t *info, int s_first, int s_last)
361 vu_long *addr = (vu_long*)(info->start[0]);
362 int flag, prot, sect, l_sect;
363 ulong start, now, last;
365 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
367 if ((s_first < 0) || (s_first > s_last)) {
368 if (info->flash_id == FLASH_UNKNOWN) {
369 printf ("- missing\n");
371 printf ("- no sectors to erase\n");
376 if ((info->flash_id == FLASH_UNKNOWN) ||
377 (info->flash_id > FLASH_AMD_COMP)) {
378 printf ("Can't erase unknown flash type %08lx - aborted\n",
384 for (sect=s_first; sect<=s_last; ++sect) {
385 if (info->protect[sect]) {
391 printf ("- Warning: %d protected sectors will not be erased!\n",
399 /* Disable interrupts which might cause a timeout here */
400 flag = disable_interrupts();
402 addr[0x0555] = 0x00AA00AA;
403 addr[0x02AA] = 0x00550055;
404 addr[0x0555] = 0x00800080;
405 addr[0x0555] = 0x00AA00AA;
406 addr[0x02AA] = 0x00550055;
408 /* Start erase on unprotected sectors */
409 for (sect = s_first; sect<=s_last; sect++) {
410 if (info->protect[sect] == 0) { /* not protected */
411 addr = (vu_long*)(info->start[sect]);
412 addr[0] = 0x00300030;
417 /* re-enable interrupts if necessary */
421 /* wait at least 80us - let's wait 1 ms */
425 * We wait for the last triggered sector
430 start = get_timer (0);
432 addr = (vu_long*)(info->start[l_sect]);
433 while ((addr[0] & 0x00800080) != 0x00800080) {
434 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
435 printf ("Timeout\n");
438 /* show that we're waiting */
439 if ((now - last) > 100000) { /* every second */
446 /* reset to read mode */
447 addr = (volatile unsigned long *)info->start[0];
448 addr[0] = 0x00F000F0; /* reset bank */
454 /*-----------------------------------------------------------------------
455 * Copy memory to flash, returns:
458 * 2 - Flash not erased
461 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
466 wp = (addr & ~3); /* get lower word aligned address */
469 * handle unaligned start bytes
471 if ((l = addr - wp) != 0) {
473 for (i=0, cp=wp; i<l; ++i, ++cp) {
474 data = (data << 8) | (*(uchar *)cp);
476 for (; i<4 && cnt>0; ++i) {
477 data = (data << 8) | *src++;
481 for (; cnt==0 && i<4; ++i, ++cp) {
482 data = (data << 8) | (*(uchar *)cp);
485 if ((rc = write_word(info, wp, SWAP(data))) != 0) {
492 * handle word aligned part
496 for (i=0; i<4; ++i) {
497 data = (data << 8) | *src++;
499 if ((rc = write_word(info, wp, SWAP(data))) != 0) {
511 * handle unaligned tail bytes
514 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
515 data = (data << 8) | *src++;
518 for (; i<4; ++i, ++cp) {
519 data = (data << 8) | (*(uchar *)cp);
522 return (write_word(info, wp, SWAP(data)));
525 /*-----------------------------------------------------------------------
526 * Write a word to Flash, returns:
529 * 2 - Flash not erased
531 static int write_word (flash_info_t *info, ulong dest, ulong data)
533 vu_long *addr = (vu_long*)(info->start[0]);
539 /* Check if Flash is (sufficiently) erased */
540 if ((*((vu_long *)dest) & data) != data) {
544 /* Disable interrupts which might cause a timeout here */
545 flag = disable_interrupts();
547 addr[0x0555] = 0x00AA00AA;
548 addr[0x02AA] = 0x00550055;
549 addr[0x0555] = 0x00A000A0;
551 *((vu_long *)dest) = data;
553 /* re-enable interrupts if necessary */
557 /* data polling for D7 */
558 start = get_timer (0);
559 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
560 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {