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>
13 * Auerswald GmbH & Co KG, Germany
14 * Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>
16 * See file CREDITS for list of people who contributed to this
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation; either version 2 of
22 * the License, or (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 #include <asm/arch/pxa-regs.h>
38 #if defined CFG_JFFS_CUSTOM_PART
39 #include <jffs2/jffs2.h>
42 /* Debugging macros ------------------------------------------------------ */
46 /* Some debug macros */
47 #if (FLASH_DEBUG > 2 )
48 #define PRINTK3(args...) printf(args)
50 #define PRINTK3(args...)
54 #define PRINTK2(args...) printf(args)
56 #define PRINTK2(args...)
60 #define PRINTK(args...) printf(args)
62 #define PRINTK(args...)
65 /* ------------------------------------------------------------------------ */
67 /* Development system: we have only 16 MB Flash */
68 #ifdef CONFIG_MTD_INNOKOM_16MB
69 #define FLASH_BANK_SIZE 0x01000000 /* 16 MB (during development) */
70 #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
73 /* Production system: we have 64 MB Flash */
74 #ifdef CONFIG_MTD_INNOKOM_64MB
75 #define FLASH_BANK_SIZE 0x04000000 /* 64 MB */
76 #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
79 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
82 #if defined CFG_JFFS_CUSTOM_PART
85 * jffs2_part_info - get information about a JFFS2 partition
87 * @part_num: number of the partition you want to get info about
88 * @return: struct part_info* in case of success, 0 if failure
91 static struct part_info part;
92 static int current_part = -1;
94 #ifdef CONFIG_MTD_INNOKOM_16MB
95 #ifdef CONFIG_MTD_INNOKOM_64MB
96 #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
98 struct part_info* jffs2_part_info(int part_num) {
99 void *jffs2_priv_saved = part.jffs2_priv;
101 PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
103 if (current_part == part_num)
106 /* u-boot partition */
108 memset(&part, 0, sizeof(part));
110 part.offset=(char*)0x00000000;
113 /* Mark the struct as ready */
114 current_part = part_num;
116 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
117 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
120 /* primary OS+firmware partition */
122 memset(&part, 0, sizeof(part));
124 part.offset=(char*)0x00040000;
127 /* Mark the struct as ready */
128 current_part = part_num;
130 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
131 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
134 /* secondary OS+firmware partition */
136 memset(&part, 0, sizeof(part));
138 part.offset=(char*)0x00100000;
139 part.size=8*1024*1024;
141 /* Mark the struct as ready */
142 current_part = part_num;
144 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
145 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
150 memset(&part, 0, sizeof(part));
152 part.offset=(char*)0x00900000;
153 part.size=7*1024*1024;
155 /* Mark the struct as ready */
156 current_part = part_num;
158 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
159 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
162 if (current_part == part_num) {
163 part.usr_priv = ¤t_part;
164 part.jffs2_priv = jffs2_priv_saved;
168 PRINTK("jffs2_part_info: end of partition table\n");
171 #endif /* CONFIG_MTD_INNOKOM_16MB */
173 #ifdef CONFIG_MTD_INNOKOM_64MB
174 #ifdef CONFIG_MTD_INNOKOM_16MB
175 #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
177 struct part_info* jffs2_part_info(int part_num) {
178 void *jffs2_priv_saved = part.jffs2_priv;
180 PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
182 if (current_part == part_num)
185 /* u-boot partition */
187 memset(&part, 0, sizeof(part));
189 part.offset=(char*)0x00000000;
192 /* Mark the struct as ready */
193 current_part = part_num;
195 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
196 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
199 /* primary OS+firmware partition */
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 current_part = part_num;
209 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
210 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
213 /* secondary OS+firmware partition */
215 memset(&part, 0, sizeof(part));
217 part.offset=(char*)0x01020000;
218 part.size=16*1024*1024-128*1024;
220 /* Mark the struct as ready */
221 current_part = part_num;
223 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
224 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
229 memset(&part, 0, sizeof(part));
231 part.offset=(char*)0x02000000;
232 part.size=32*1024*1024;
234 /* Mark the struct as ready */
235 current_part = part_num;
237 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
238 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
241 if (current_part == part_num) {
242 part.usr_priv = ¤t_part;
243 part.jffs2_priv = jffs2_priv_saved;
247 PRINTK("jffs2_part_info: end of partition table\n");
250 #endif /* CONFIG_MTD_INNOKOM_64MB */
251 #endif /* defined CFG_JFFS_CUSTOM_PART */
255 * flash_init: - initialize data structures for flash chips
257 * @return: size of the flash
260 ulong flash_init(void)
265 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
267 flash_info[i].flash_id =
268 (INTEL_MANUFACT & FLASH_VENDMASK) |
269 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
270 flash_info[i].size = FLASH_BANK_SIZE;
271 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
272 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
276 flashbase = PHYS_FLASH_1;
279 panic("configured too many flash banks!\n");
282 for (j = 0; j < flash_info[i].sector_count; j++) {
283 flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
285 size += flash_info[i].size;
288 /* Protect u-boot sectors */
289 flash_protect(FLAG_PROTECT_SET,
291 CFG_FLASH_BASE + (256*1024) - 1,
294 #ifdef CFG_ENV_IS_IN_FLASH
295 flash_protect(FLAG_PROTECT_SET,
297 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
306 * flash_print_info: - print information about the flash situation
311 void flash_print_info (flash_info_t *info)
315 for (j=0; j<CFG_MAX_FLASH_BANKS; j++) {
317 switch (info->flash_id & FLASH_VENDMASK) {
319 case (INTEL_MANUFACT & FLASH_VENDMASK):
323 printf("Unknown Vendor ");
327 switch (info->flash_id & FLASH_TYPEMASK) {
329 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
330 printf("28F128J3 (128Mbit)\n");
333 printf("Unknown Chip Type\n");
337 printf(" Size: %ld MB in %d Sectors\n",
338 info->size >> 20, info->sector_count);
340 printf(" Sector Start Addresses:");
341 for (i = 0; i < info->sector_count; i++) {
342 if ((i % 5) == 0) printf ("\n ");
344 printf (" %08lX%s", info->start[i],
345 info->protect[i] ? " (RO)" : " ");
354 * flash_erase: - erase flash sectors
358 int flash_erase(flash_info_t *info, int s_first, int s_last)
360 int flag, prot, sect;
363 if (info->flash_id == FLASH_UNKNOWN)
364 return ERR_UNKNOWN_FLASH_TYPE;
366 if ((s_first < 0) || (s_first > s_last)) {
370 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
371 return ERR_UNKNOWN_FLASH_VENDOR;
374 for (sect=s_first; sect<=s_last; ++sect) {
375 if (info->protect[sect]) prot++;
378 if (prot) return ERR_PROTECTED;
381 * Disable interrupts which might cause a timeout
382 * here. Remember that our exception vectors are
383 * at address 0 in the flash, and we don't want a
384 * (ticker) exception to happen while the flash
385 * chip is in programming mode.
388 flag = disable_interrupts();
390 /* Start erase on unprotected sectors */
391 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
393 printf("Erasing sector %2d ... ", sect);
397 /* arm simple, non interrupt dependent timer */
398 reset_timer_masked();
400 if (info->protect[sect] == 0) { /* not protected */
401 u16 * volatile addr = (u16 * volatile)(info->start[sect]);
403 PRINTK("unlocking sector\n");
408 PRINTK("erasing sector\n");
410 PRINTK("confirming erase\n");
413 while ((*addr & 0x0080) != 0x0080) {
415 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
416 *addr = 0x00B0; /* suspend erase*/
417 *addr = 0x00FF; /* read mode */
423 PRINTK("clearing status register\n");
425 PRINTK("resetting to read mode");
432 if (ctrlc()) printf("User Interrupt!\n");
436 /* allow flash to settle - wait 10 ms */
437 udelay_masked(10000);
439 if (flag) enable_interrupts();
446 * write_word: - copy memory to flash
454 static int write_word (flash_info_t *info, ulong dest, ushort data)
456 volatile u16 *addr = (u16 *)dest, val;
460 /* Check if Flash is (sufficiently) erased */
461 if ((*addr & data) != data) return ERR_NOT_ERASED;
464 * Disable interrupts which might cause a timeout
465 * here. Remember that our exception vectors are
466 * at address 0 in the flash, and we don't want a
467 * (ticker) exception to happen while the flash
468 * chip is in programming mode.
470 flag = disable_interrupts();
472 /* clear status register command */
475 /* program set-up command */
478 /* latch address/data */
481 /* arm simple, non interrupt dependent timer */
482 reset_timer_masked();
484 /* wait while polling the status register */
485 while(((val = *addr) & 0x80) != 0x80) {
486 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
488 *addr = 0xB0; /* suspend program command */
493 if(val & 0x1A) { /* check for error */
494 printf("\nFlash write error %02x at address %08lx\n",
495 (int)val, (unsigned long)dest);
497 printf("Voltage range error.\n");
502 printf("Device protect error.\n");
507 printf("Programming error.\n");
517 *addr = 0xFF; /* read array command */
518 if (flag) enable_interrupts();
525 * write_buf: - Copy memory to flash.
528 * @param src: source of copy transaction
529 * @param addr: where to copy to
530 * @param cnt: number of bytes to copy
535 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
542 wp = (addr & ~1); /* get lower word aligned address */
545 * handle unaligned start bytes
547 if ((l = addr - wp) != 0) {
549 for (i=0, cp=wp; i<l; ++i, ++cp) {
550 data = (data >> 8) | (*(uchar *)cp << 8);
552 for (; i<2 && cnt>0; ++i) {
553 data = (data >> 8) | (*src++ << 8);
557 for (; cnt==0 && i<2; ++i, ++cp) {
558 data = (data >> 8) | (*(uchar *)cp << 8);
561 if ((rc = write_word(info, wp, data)) != 0) {
568 * handle word aligned part
571 /* data = *((vushort*)src); */
572 data = *((ushort*)src);
573 if ((rc = write_word(info, wp, data)) != 0) {
581 if (cnt == 0) return ERR_OK;
584 * handle unaligned tail bytes
587 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
588 data = (data >> 8) | (*src++ << 8);
591 for (; i<2; ++i, ++cp) {
592 data = (data >> 8) | (*(uchar *)cp << 8);
595 return write_word(info, wp, data);