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 ------------------------------------------------------ */
45 //#define FLASH_DEBUG 1
47 /* Some debug macros */
48 #if (FLASH_DEBUG > 2 )
49 #define PRINTK3(args...) printf(args)
51 #define PRINTK3(args...)
55 #define PRINTK2(args...) printf(args)
57 #define PRINTK2(args...)
61 #define PRINTK(args...) printf(args)
63 #define PRINTK(args...)
66 /* ------------------------------------------------------------------------ */
68 /* Development system: we have only 16 MB Flash */
69 #ifdef CONFIG_MTD_INNOKOM_16MB
70 #define FLASH_BANK_SIZE 0x01000000 /* 16 MB (during development) */
71 #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
74 /* Production system: we have 64 MB Flash */
75 #ifdef CONFIG_MTD_INNOKOM_64MB
76 #define FLASH_BANK_SIZE 0x04000000 /* 64 MB */
77 #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
80 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
83 #if defined CFG_JFFS_CUSTOM_PART
86 * jffs2_part_info - get information about a JFFS2 partition
88 * @part_num: number of the partition you want to get info about
89 * @return: struct part_info* in case of success, 0 if failure
92 static struct part_info part;
93 static int current_part = -1;
95 #ifdef CONFIG_MTD_INNOKOM_16MB
96 #ifdef CONFIG_MTD_INNOKOM_64MB
97 #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
99 struct part_info* jffs2_part_info(int part_num) {
100 void *jffs2_priv_saved = part.jffs2_priv;
102 PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
104 if (current_part == part_num)
107 /* u-boot partition */
109 memset(&part, 0, sizeof(part));
111 part.offset=(char*)0x00000000;
114 /* Mark the struct as ready */
115 current_part = part_num;
117 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
118 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
121 /* primary OS+firmware partition */
123 memset(&part, 0, sizeof(part));
125 part.offset=(char*)0x00040000;
128 /* Mark the struct as ready */
129 current_part = part_num;
131 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
132 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
135 /* secondary OS+firmware partition */
137 memset(&part, 0, sizeof(part));
139 part.offset=(char*)0x00100000;
140 part.size=8*1024*1024;
142 /* Mark the struct as ready */
143 current_part = part_num;
145 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
146 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
151 memset(&part, 0, sizeof(part));
153 part.offset=(char*)0x00900000;
154 part.size=7*1024*1024;
156 /* Mark the struct as ready */
157 current_part = part_num;
159 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
160 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
163 if (current_part == part_num) {
164 part.usr_priv = ¤t_part;
165 part.jffs2_priv = jffs2_priv_saved;
169 PRINTK("jffs2_part_info: end of partition table\n");
172 #endif /* CONFIG_MTD_INNOKOM_16MB */
174 #ifdef CONFIG_MTD_INNOKOM_64MB
175 #ifdef CONFIG_MTD_INNOKOM_16MB
176 #error Please define only one CONFIG_MTD_INNOKOM_XXMB option.
178 struct part_info* jffs2_part_info(int part_num) {
179 void *jffs2_priv_saved = part.jffs2_priv;
181 PRINTK2("jffs2_part_info: part_num=%i\n",part_num);
183 if (current_part == part_num)
186 /* u-boot partition */
188 memset(&part, 0, sizeof(part));
190 part.offset=(char*)0x00000000;
193 /* Mark the struct as ready */
194 current_part = part_num;
196 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
197 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
200 /* primary OS+firmware partition */
202 memset(&part, 0, sizeof(part));
204 part.offset=(char*)0x00040000;
205 part.size=16*1024*1024-128*1024;
207 /* Mark the struct as ready */
208 current_part = part_num;
210 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
211 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
214 /* secondary OS+firmware partition */
216 memset(&part, 0, sizeof(part));
218 part.offset=(char*)0x01020000;
219 part.size=16*1024*1024-128*1024;
221 /* Mark the struct as ready */
222 current_part = part_num;
224 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
225 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
230 memset(&part, 0, sizeof(part));
232 part.offset=(char*)0x02000000;
233 part.size=32*1024*1024;
235 /* Mark the struct as ready */
236 current_part = part_num;
238 PRINTK("part.offset = 0x%08x\n",(unsigned int)part.offset);
239 PRINTK("part.size = 0x%08x\n",(unsigned int)part.size);
242 if (current_part == part_num) {
243 part.usr_priv = ¤t_part;
244 part.jffs2_priv = jffs2_priv_saved;
248 PRINTK("jffs2_part_info: end of partition table\n");
251 #endif /* CONFIG_MTD_INNOKOM_64MB */
252 #endif /* defined CFG_JFFS_CUSTOM_PART */
256 * flash_init: - initialize data structures for flash chips
258 * @return: size of the flash
261 ulong flash_init(void)
266 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
268 flash_info[i].flash_id =
269 (INTEL_MANUFACT & FLASH_VENDMASK) |
270 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
271 flash_info[i].size = FLASH_BANK_SIZE;
272 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
273 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
277 flashbase = PHYS_FLASH_1;
280 panic("configured to many flash banks!\n");
283 for (j = 0; j < flash_info[i].sector_count; j++) {
284 flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
286 size += flash_info[i].size;
289 /* Protect u-boot sectors */
290 flash_protect(FLAG_PROTECT_SET,
292 CFG_FLASH_BASE + (256*1024) - 1,
295 #ifdef CFG_ENV_IS_IN_FLASH
296 flash_protect(FLAG_PROTECT_SET,
298 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
307 * flash_print_info: - print information about the flash situation
312 void flash_print_info (flash_info_t *info)
316 for (j=0; j<CFG_MAX_FLASH_BANKS; j++) {
318 switch (info->flash_id & FLASH_VENDMASK) {
320 case (INTEL_MANUFACT & FLASH_VENDMASK):
324 printf("Unknown Vendor ");
328 switch (info->flash_id & FLASH_TYPEMASK) {
330 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
331 printf("28F128J3 (128Mbit)\n");
334 printf("Unknown Chip Type\n");
338 printf(" Size: %ld MB in %d Sectors\n",
339 info->size >> 20, info->sector_count);
341 printf(" Sector Start Addresses:");
342 for (i = 0; i < info->sector_count; i++) {
343 if ((i % 5) == 0) printf ("\n ");
345 printf (" %08lX%s", info->start[i],
346 info->protect[i] ? " (RO)" : " ");
355 * flash_erase: - erase flash sectors
359 int flash_erase(flash_info_t *info, int s_first, int s_last)
361 int flag, prot, sect;
364 if (info->flash_id == FLASH_UNKNOWN)
365 return ERR_UNKNOWN_FLASH_TYPE;
367 if ((s_first < 0) || (s_first > s_last)) {
371 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
372 return ERR_UNKNOWN_FLASH_VENDOR;
375 for (sect=s_first; sect<=s_last; ++sect) {
376 if (info->protect[sect]) prot++;
379 if (prot) return ERR_PROTECTED;
382 * Disable interrupts which might cause a timeout
383 * here. Remember that our exception vectors are
384 * at address 0 in the flash, and we don't want a
385 * (ticker) exception to happen while the flash
386 * chip is in programming mode.
389 flag = disable_interrupts();
391 /* Start erase on unprotected sectors */
392 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
394 printf("Erasing sector %2d ... ", sect);
398 /* arm simple, non interrupt dependent timer */
399 reset_timer_masked();
401 if (info->protect[sect] == 0) { /* not protected */
402 u16 * volatile addr = (u16 * volatile)(info->start[sect]);
404 PRINTK("unlocking sector\n");
409 PRINTK("erasing sector\n");
411 PRINTK("confirming erase\n");
414 while ((*addr & 0x0080) != 0x0080) {
416 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
417 *addr = 0x00B0; /* suspend erase*/
418 *addr = 0x00FF; /* read mode */
424 PRINTK("clearing status register\n");
426 PRINTK("resetting to read mode");
433 if (ctrlc()) printf("User Interrupt!\n");
437 /* allow flash to settle - wait 10 ms */
438 udelay_masked(10000);
440 if (flag) enable_interrupts();
447 * write_word: - copy memory to flash
455 static int write_word (flash_info_t *info, ulong dest, ushort data)
457 volatile u16 *addr = (u16 *)dest, val;
461 /* Check if Flash is (sufficiently) erased */
462 if ((*addr & data) != data) return ERR_NOT_ERASED;
465 * Disable interrupts which might cause a timeout
466 * here. Remember that our exception vectors are
467 * at address 0 in the flash, and we don't want a
468 * (ticker) exception to happen while the flash
469 * chip is in programming mode.
471 flag = disable_interrupts();
473 /* clear status register command */
476 /* program set-up command */
479 /* latch address/data */
482 /* arm simple, non interrupt dependent timer */
483 reset_timer_masked();
485 /* wait while polling the status register */
486 while(((val = *addr) & 0x80) != 0x80) {
487 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
489 *addr = 0xB0; /* suspend program command */
494 if(val & 0x1A) { /* check for error */
495 printf("\nFlash write error %02x at address %08lx\n",
496 (int)val, (unsigned long)dest);
498 printf("Voltage range error.\n");
503 printf("Device protect error.\n");
508 printf("Programming error.\n");
518 *addr = 0xFF; /* read array command */
519 if (flag) enable_interrupts();
526 * write_buf: - Copy memory to flash.
529 * @param src: source of copy transaction
530 * @param addr: where to copy to
531 * @param cnt: number of bytes to copy
536 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
543 wp = (addr & ~1); /* get lower word aligned address */
546 * handle unaligned start bytes
548 if ((l = addr - wp) != 0) {
550 for (i=0, cp=wp; i<l; ++i, ++cp) {
551 data = (data >> 8) | (*(uchar *)cp << 8);
553 for (; i<2 && cnt>0; ++i) {
554 data = (data >> 8) | (*src++ << 8);
558 for (; cnt==0 && i<2; ++i, ++cp) {
559 data = (data >> 8) | (*(uchar *)cp << 8);
562 if ((rc = write_word(info, wp, data)) != 0) {
569 * handle word aligned part
572 /* data = *((vushort*)src); */
573 data = *((ushort*)src);
574 if ((rc = write_word(info, wp, data)) != 0) {
582 if (cnt == 0) return ERR_OK;
585 * handle unaligned tail bytes
588 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
589 data = (data >> 8) | (*src++ << 8);
592 for (; i<2; ++i, ++cp) {
593 data = (data >> 8) | (*(uchar *)cp << 8);
596 return write_word(info, wp, data);