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;
89 static int current_part = -1;
91 #ifdef CONFIG_MTD_INNOKOM_16MB
92 #ifdef CONFIG_MTD_INNOKOM_64MB
93 #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
95 struct part_info* jffs2_part_info(int part_num) {
96 void *jffs2_priv_saved = part.jffs2_priv;
98 PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
100 if (current_part == part_num)
103 /* u-boot partition */
105 memset(&part, 0, sizeof(part));
107 part.offset=(char*)0x00000000;
110 /* Mark the struct as ready */
111 current_part = part_num;
113 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
114 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
117 /* primary OS+firmware partition */
119 memset(&part, 0, sizeof(part));
121 part.offset=(char*)0x00040000;
124 /* Mark the struct as ready */
125 current_part = part_num;
127 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
128 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
131 /* secondary OS+firmware partition */
133 memset(&part, 0, sizeof(part));
135 part.offset=(char*)0x00100000;
136 part.size=8*1024*1024;
138 /* Mark the struct as ready */
139 current_part = part_num;
141 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
142 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
147 memset(&part, 0, sizeof(part));
149 part.offset=(char*)0x00900000;
150 part.size=7*1024*1024;
152 /* Mark the struct as ready */
153 current_part = part_num;
155 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
156 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
159 if (current_part == part_num) {
160 part.usr_priv = ¤t_part;
161 part.jffs2_priv = jffs2_priv_saved;
165 PRINTK("jffs2_part_info: end of partition table\n");
168 #endif /* CONFIG_MTD_INNOKOM_16MB */
170 #ifdef CONFIG_MTD_INNOKOM_64MB
171 #ifdef CONFIG_MTD_INNOKOM_16MB
172 #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
174 struct part_info* jffs2_part_info(int part_num) {
175 void *jffs2_priv_saved = part.jffs2_priv;
177 PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
179 if (current_part == part_num)
182 /* u-boot partition */
184 memset(&part, 0, sizeof(part));
186 part.offset=(char*)0x00000000;
189 /* Mark the struct as ready */
190 current_part = part_num;
192 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
193 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
196 /* primary OS+firmware partition */
198 memset(&part, 0, sizeof(part));
200 part.offset=(char*)0x00040000;
201 part.size=16*1024*1024-128*1024;
203 /* Mark the struct as ready */
204 current_part = part_num;
206 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
207 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
210 /* secondary OS+firmware partition */
212 memset(&part, 0, sizeof(part));
214 part.offset=(char*)0x01020000;
215 part.size=16*1024*1024-128*1024;
217 /* Mark the struct as ready */
218 current_part = part_num;
220 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
221 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
226 memset(&part, 0, sizeof(part));
228 part.offset=(char*)0x02000000;
229 part.size=32*1024*1024;
231 /* Mark the struct as ready */
232 current_part = part_num;
234 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
235 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
238 if (current_part == part_num) {
239 part.usr_priv = ¤t_part;
240 part.jffs2_priv = jffs2_priv_saved;
244 PRINTK("jffs2_part_info: end of partition table\n");
247 #endif /* CONFIG_MTD_INNOKOM_64MB */
248 #endif /* defined CFG_JFFS_CUSTOM_PART */
252 * flash_init: - initialize data structures for flash chips
254 * @return: size of the flash
257 ulong flash_init(void)
262 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
264 flash_info[i].flash_id =
265 (INTEL_MANUFACT & FLASH_VENDMASK) |
266 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
267 flash_info[i].size = FLASH_BANK_SIZE;
268 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
269 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
273 flashbase = PHYS_FLASH_1;
276 panic("configured to many flash banks!\n");
279 for (j = 0; j < flash_info[i].sector_count; j++) {
280 flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
282 size += flash_info[i].size;
285 /* Protect u-boot sectors */
286 flash_protect(FLAG_PROTECT_SET,
288 CFG_FLASH_BASE + (256*1024) - 1,
291 #ifdef CFG_ENV_IS_IN_FLASH
292 flash_protect(FLAG_PROTECT_SET,
294 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
303 * flash_print_info: - print information about the flash situation
308 void flash_print_info (flash_info_t *info)
312 for (j=0; j<CFG_MAX_FLASH_BANKS; j++) {
314 switch (info->flash_id & FLASH_VENDMASK) {
316 case (INTEL_MANUFACT & FLASH_VENDMASK):
320 printf("Unknown Vendor ");
324 switch (info->flash_id & FLASH_TYPEMASK) {
326 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
327 printf("28F128J3 (128Mbit)\n");
330 printf("Unknown Chip Type\n");
334 printf(" Size: %ld MB in %d Sectors\n",
335 info->size >> 20, info->sector_count);
337 printf(" Sector Start Addresses:");
338 for (i = 0; i < info->sector_count; i++) {
339 if ((i % 5) == 0) printf ("\n ");
341 printf (" %08lX%s", info->start[i],
342 info->protect[i] ? " (RO)" : " ");
351 * flash_erase: - erase flash sectors
355 int flash_erase(flash_info_t *info, int s_first, int s_last)
357 int flag, prot, sect;
360 if (info->flash_id == FLASH_UNKNOWN)
361 return ERR_UNKNOWN_FLASH_TYPE;
363 if ((s_first < 0) || (s_first > s_last)) {
367 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
368 return ERR_UNKNOWN_FLASH_VENDOR;
371 for (sect=s_first; sect<=s_last; ++sect) {
372 if (info->protect[sect]) prot++;
375 if (prot) return ERR_PROTECTED;
378 * Disable interrupts which might cause a timeout
379 * here. Remember that our exception vectors are
380 * at address 0 in the flash, and we don't want a
381 * (ticker) exception to happen while the flash
382 * chip is in programming mode.
385 flag = disable_interrupts();
387 /* Start erase on unprotected sectors */
388 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
390 printf("Erasing sector %2d ... ", sect);
394 /* arm simple, non interrupt dependent timer */
395 reset_timer_masked();
397 if (info->protect[sect] == 0) { /* not protected */
398 u16 * volatile addr = (u16 * volatile)(info->start[sect]);
400 PRINTK("unlocking sector\n");
405 PRINTK("erasing sector\n");
407 PRINTK("confirming erase\n");
410 while ((*addr & 0x0080) != 0x0080) {
412 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
413 *addr = 0x00B0; /* suspend erase*/
414 *addr = 0x00FF; /* read mode */
420 PRINTK("clearing status register\n");
422 PRINTK("resetting to read mode");
429 if (ctrlc()) printf("User Interrupt!\n");
433 /* allow flash to settle - wait 10 ms */
434 udelay_masked(10000);
436 if (flag) enable_interrupts();
443 * write_word: - copy memory to flash
451 static int write_word (flash_info_t *info, ulong dest, ushort data)
453 volatile u16 *addr = (u16 *)dest, val;
457 /* Check if Flash is (sufficiently) erased */
458 if ((*addr & data) != data) return ERR_NOT_ERASED;
461 * Disable interrupts which might cause a timeout
462 * here. Remember that our exception vectors are
463 * at address 0 in the flash, and we don't want a
464 * (ticker) exception to happen while the flash
465 * chip is in programming mode.
467 flag = disable_interrupts();
469 /* clear status register command */
472 /* program set-up command */
475 /* latch address/data */
478 /* arm simple, non interrupt dependent timer */
479 reset_timer_masked();
481 /* wait while polling the status register */
482 while(((val = *addr) & 0x80) != 0x80) {
483 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
485 *addr = 0xB0; /* suspend program command */
490 if(val & 0x1A) { /* check for error */
491 printf("\nFlash write error %02x at address %08lx\n",
492 (int)val, (unsigned long)dest);
494 printf("Voltage range error.\n");
499 printf("Device protect error.\n");
504 printf("Programming error.\n");
514 *addr = 0xFF; /* read array command */
515 if (flag) enable_interrupts();
522 * write_buf: - Copy memory to flash.
525 * @param src: source of copy transaction
526 * @param addr: where to copy to
527 * @param cnt: number of bytes to copy
532 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
539 wp = (addr & ~1); /* get lower word aligned address */
542 * handle unaligned start bytes
544 if ((l = addr - wp) != 0) {
546 for (i=0, cp=wp; i<l; ++i, ++cp) {
547 data = (data >> 8) | (*(uchar *)cp << 8);
549 for (; i<2 && cnt>0; ++i) {
550 data = (data >> 8) | (*src++ << 8);
554 for (; cnt==0 && i<2; ++i, ++cp) {
555 data = (data >> 8) | (*(uchar *)cp << 8);
558 if ((rc = write_word(info, wp, data)) != 0) {
565 * handle word aligned part
568 /* data = *((vushort*)src); */
569 data = *((ushort*)src);
570 if ((rc = write_word(info, wp, data)) != 0) {
578 if (cnt == 0) return ERR_OK;
581 * handle unaligned tail bytes
584 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
585 data = (data >> 8) | (*src++ << 8);
588 for (; i<2; ++i, ++cp) {
589 data = (data >> 8) | (*(uchar *)cp << 8);
592 return write_word(info, wp, data);