3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
10 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
12 * See file CREDITS for list of people who contributed to this
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <asm/arch/pxa-regs.h>
34 #if defined CFG_JFFS_CUSTOM_PART
35 #include <jffs2/jffs2.h>
38 /* Debugging macros ------------------------------------------------------ */
41 //#define FLASH_DEBUG 1
43 /* Some debug macros */
44 #if (FLASH_DEBUG > 2 )
45 #define PRINTK3(args...) printf(args)
47 #define PRINTK3(args...)
51 #define PRINTK2(args...) printf(args)
53 #define PRINTK2(args...)
57 #define PRINTK(args...) printf(args)
59 #define PRINTK(args...)
62 /* ------------------------------------------------------------------------ */
64 /* Development system: we have only 16 MB Flash */
65 #ifdef CONFIG_MTD_INNOKOM_16MB
66 #define FLASH_BANK_SIZE 0x01000000 /* 16 MB (during development) */
67 #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
70 /* Production system: we have 64 MB Flash */
71 #ifdef CONFIG_MTD_INNOKOM_64MB
72 #define FLASH_BANK_SIZE 0x04000000 /* 64 MB */
73 #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
76 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
79 #if defined CFG_JFFS_CUSTOM_PART
82 * jffs2_part_info - get information about a JFFS2 partition
84 * @part_num: number of the partition you want to get info about
85 * @return: struct part_info* in case of success, 0 if failure
88 static struct part_info part;
90 #ifdef CONFIG_MTD_INNOKOM_16MB
91 #ifdef CONFIG_MTD_INNOKOM_64MB
92 #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
94 struct part_info* jffs2_part_info(int part_num) {
96 PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
98 /* u-boot partition */
100 if(part.usr_priv==(void*)1) return ∂
102 memset(&part, 0, sizeof(part));
104 part.offset=(char*)0x00000000;
107 /* Mark the struct as ready */
108 part.usr_priv=(void*)1;
110 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
111 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
115 /* primary OS+firmware partition */
117 if(part.usr_priv==(void*)1) return ∂
119 memset(&part, 0, sizeof(part));
121 part.offset=(char*)0x00040000;
124 /* Mark the struct as ready */
125 part.usr_priv=(void*)1;
127 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
128 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
132 /* secondary OS+firmware partition */
134 if(part.usr_priv==(void*)1) return ∂
136 memset(&part, 0, sizeof(part));
138 part.offset=(char*)0x00100000;
139 part.size=8*1024*1024;
141 /* Mark the struct as ready */
142 part.usr_priv=(void*)1;
144 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
145 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
151 if(part.usr_priv==(void*)1) return ∂
153 memset(&part, 0, sizeof(part));
155 part.offset=(char*)0x00900000;
156 part.size=7*1024*1024;
158 /* Mark the struct as ready */
159 part.usr_priv=(void*)1;
161 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
162 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
167 PRINTK("jffs2_part_info: end of partition table\n");
170 #endif /* CONFIG_MTD_INNOKOM_16MB */
172 #ifdef CONFIG_MTD_INNOKOM_64MB
173 #ifdef CONFIG_MTD_INNOKOM_16MB
174 #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
176 struct part_info* jffs2_part_info(int part_num) {
178 PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
180 /* u-boot partition */
182 if(part.usr_priv==(void*)1) return ∂
184 memset(&part, 0, sizeof(part));
186 part.offset=(char*)0x00000000;
189 /* Mark the struct as ready */
190 part.usr_priv=(void*)1;
192 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
193 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
197 /* primary OS+firmware partition */
199 if(part.usr_priv==(void*)1) return ∂
201 memset(&part, 0, sizeof(part));
203 part.offset=(char*)0x00040000;
204 part.size=16*1024*1024-128*1024;
206 /* Mark the struct as ready */
207 part.usr_priv=(void*)1;
209 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
210 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
214 /* secondary OS+firmware partition */
216 if(part.usr_priv==(void*)1) return ∂
218 memset(&part, 0, sizeof(part));
220 part.offset=(char*)0x01020000;
221 part.size=16*1024*1024-128*1024;
223 /* Mark the struct as ready */
224 part.usr_priv=(void*)1;
226 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
227 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
233 if(part.usr_priv==(void*)1) return ∂
235 memset(&part, 0, sizeof(part));
237 part.offset=(char*)0x02000000;
238 part.size=32*1024*1024;
240 /* Mark the struct as ready */
241 part.usr_priv=(void*)1;
243 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
244 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
249 PRINTK("jffs2_part_info: end of partition table\n");
252 #endif /* CONFIG_MTD_INNOKOM_64MB */
253 #endif /* defined CFG_JFFS_CUSTOM_PART */
257 * flash_init: - initialize data structures for flash chips
259 * @return: size of the flash
262 ulong flash_init(void)
267 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
269 flash_info[i].flash_id =
270 (INTEL_MANUFACT & FLASH_VENDMASK) |
271 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
272 flash_info[i].size = FLASH_BANK_SIZE;
273 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
274 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
278 flashbase = PHYS_FLASH_1;
281 panic("configured to many flash banks!\n");
284 for (j = 0; j < flash_info[i].sector_count; j++) {
285 flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
287 size += flash_info[i].size;
290 /* Protect u-boot sectors */
291 flash_protect(FLAG_PROTECT_SET,
293 CFG_FLASH_BASE + (256*1024) - 1,
296 #ifdef CFG_ENV_IS_IN_FLASH
297 flash_protect(FLAG_PROTECT_SET,
299 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
308 * flash_print_info: - print information about the flash situation
313 void flash_print_info (flash_info_t *info)
317 for (j=0; j<CFG_MAX_FLASH_BANKS; j++) {
319 switch (info->flash_id & FLASH_VENDMASK) {
321 case (INTEL_MANUFACT & FLASH_VENDMASK):
325 printf("Unknown Vendor ");
329 switch (info->flash_id & FLASH_TYPEMASK) {
331 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
332 printf("28F128J3 (128Mbit)\n");
335 printf("Unknown Chip Type\n");
339 printf(" Size: %ld MB in %d Sectors\n",
340 info->size >> 20, info->sector_count);
342 printf(" Sector Start Addresses:");
343 for (i = 0; i < info->sector_count; i++) {
344 if ((i % 5) == 0) printf ("\n ");
346 printf (" %08lX%s", info->start[i],
347 info->protect[i] ? " (RO)" : " ");
356 * flash_erase: - erase flash sectors
360 int flash_erase(flash_info_t *info, int s_first, int s_last)
362 int flag, prot, sect;
365 if (info->flash_id == FLASH_UNKNOWN)
366 return ERR_UNKNOWN_FLASH_TYPE;
368 if ((s_first < 0) || (s_first > s_last)) {
372 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
373 return ERR_UNKNOWN_FLASH_VENDOR;
376 for (sect=s_first; sect<=s_last; ++sect) {
377 if (info->protect[sect]) prot++;
380 if (prot) return ERR_PROTECTED;
383 * Disable interrupts which might cause a timeout
384 * here. Remember that our exception vectors are
385 * at address 0 in the flash, and we don't want a
386 * (ticker) exception to happen while the flash
387 * chip is in programming mode.
390 flag = disable_interrupts();
392 /* Start erase on unprotected sectors */
393 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
395 printf("Erasing sector %2d ... ", sect);
399 /* arm simple, non interrupt dependent timer */
400 reset_timer_masked();
402 if (info->protect[sect] == 0) { /* not protected */
403 u16 * volatile addr = (u16 * volatile)(info->start[sect]);
405 PRINTK("unlocking sector\n");
410 PRINTK("erasing sector\n");
412 PRINTK("confirming erase\n");
415 while ((*addr & 0x0080) != 0x0080) {
417 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
418 *addr = 0x00B0; /* suspend erase*/
419 *addr = 0x00FF; /* read mode */
425 PRINTK("clearing status register\n");
427 PRINTK("resetting to read mode");
434 if (ctrlc()) printf("User Interrupt!\n");
438 /* allow flash to settle - wait 10 ms */
439 udelay_masked(10000);
441 if (flag) enable_interrupts();
448 * write_word: - copy memory to flash
456 static int write_word (flash_info_t *info, ulong dest, ushort data)
458 volatile u16 *addr = (u16 *)dest, val;
462 /* Check if Flash is (sufficiently) erased */
463 if ((*addr & data) != data) return ERR_NOT_ERASED;
466 * Disable interrupts which might cause a timeout
467 * here. Remember that our exception vectors are
468 * at address 0 in the flash, and we don't want a
469 * (ticker) exception to happen while the flash
470 * chip is in programming mode.
472 flag = disable_interrupts();
474 /* clear status register command */
477 /* program set-up command */
480 /* latch address/data */
483 /* arm simple, non interrupt dependent timer */
484 reset_timer_masked();
486 /* wait while polling the status register */
487 while(((val = *addr) & 0x80) != 0x80) {
488 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
490 *addr = 0xB0; /* suspend program command */
495 if(val & 0x1A) { /* check for error */
496 printf("\nFlash write error %02x at address %08lx\n",
497 (int)val, (unsigned long)dest);
499 printf("Voltage range error.\n");
504 printf("Device protect error.\n");
509 printf("Programming error.\n");
519 *addr = 0xFF; /* read array command */
520 if (flag) enable_interrupts();
527 * write_buf: - Copy memory to flash.
530 * @param src: source of copy transaction
531 * @param addr: where to copy to
532 * @param cnt: number of bytes to copy
537 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
544 wp = (addr & ~1); /* get lower word aligned address */
547 * handle unaligned start bytes
549 if ((l = addr - wp) != 0) {
551 for (i=0, cp=wp; i<l; ++i, ++cp) {
552 data = (data >> 8) | (*(uchar *)cp << 8);
554 for (; i<2 && cnt>0; ++i) {
555 data = (data >> 8) | (*src++ << 8);
559 for (; cnt==0 && i<2; ++i, ++cp) {
560 data = (data >> 8) | (*(uchar *)cp << 8);
563 if ((rc = write_word(info, wp, data)) != 0) {
570 * handle word aligned part
573 /* data = *((vushort*)src); */
574 data = *((ushort*)src);
575 if ((rc = write_word(info, wp, data)) != 0) {
583 if (cnt == 0) return ERR_OK;
586 * handle unaligned tail bytes
589 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
590 data = (data >> 8) | (*src++ << 8);
593 for (; i<2; ++i, ++cp) {
594 data = (data >> 8) | (*(uchar *)cp << 8);
597 return write_word(info, wp, data);