1 Index: linux-2.6.23.16/drivers/mtd/chips/Kconfig
2 ===================================================================
3 --- linux-2.6.23.16.orig/drivers/mtd/chips/Kconfig 2008-03-15 17:03:14.374622039 +0200
4 +++ linux-2.6.23.16/drivers/mtd/chips/Kconfig 2008-03-15 17:03:17.874821522 +0200
6 This option enables basic support for ROM chips accessed through
10 + tristate "Support for Serial chips in bus mapping"
13 + This option enables basic support for Serial chips accessed through
14 + a bus mapping driver.
17 tristate "Support for absent chips in bus mapping"
19 Index: linux-2.6.23.16/drivers/mtd/chips/cfi_cmdset_0002.c
20 ===================================================================
21 --- linux-2.6.23.16.orig/drivers/mtd/chips/cfi_cmdset_0002.c 2008-03-15 17:03:14.374622039 +0200
22 +++ linux-2.6.23.16/drivers/mtd/chips/cfi_cmdset_0002.c 2008-03-15 17:03:17.874821522 +0200
24 #include <linux/mtd/cfi.h>
25 #include <linux/mtd/xip.h>
27 +//****** Storlink SoC ******
28 #define AMD_BOOTLOC_BUG
29 -#define FORCE_WORD_WRITE 0
31 -#define MAX_WORD_RETRIES 3
32 +//#define FORCE_WORD_WRITE 0
33 +#define FORCE_WORD_WRITE 1
34 +#define FORCE_FAST_PROG 0
36 +//#define MAX_WORD_RETRIES 3
37 +#define MAX_WORD_RETRIES 3 // CONFIG_MTD_CFI_AMDSTD_RETRY
38 +//**************************
40 #define MANUFACTURER_AMD 0x0001
41 #define MANUFACTURER_ATMEL 0x001F
45 bootloc = extp->TopBottom;
46 +//****** Storlink SoC ******
50 + extp->TopBottom = 3;
52 +//**************************
53 if ((bootloc != 2) && (bootloc != 3)) {
54 printk(KERN_WARNING "%s: CFI does not contain boot "
55 "bank location. Assuming top.\n", map->name);
57 cfi->cfiq->EraseRegionInfo[j] = swap;
60 +#ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
61 + cfi->device_type = CFI_DEVICETYPE_X8;
63 /* Set the default CFI lock/unlock addresses */
64 cfi->addr_unlock1 = 0x555;
65 cfi->addr_unlock2 = 0x2aa;
69 d = map_read(map, addr);
70 + udelay(20); //Storlink SoC
71 t = map_read(map, addr);
73 return map_word_equal(map, d, t);
76 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
78 +//****** Storlink SoC ******
80 +//**************************
85 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
86 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
88 +//****** Storlink SoC ******
90 +//**************************
91 spin_unlock(chip->mutex);
94 @@ -1005,7 +1025,10 @@
96 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
99 +//****** Storlink SoC ******
101 + map_word oldd, tmp;
102 +//**************************
106 @@ -1037,9 +1060,15 @@
108 xip_disable(map, chip, adr);
110 +//****** Storlink SoC ******
111 +#if FORCE_FAST_PROG /* Unlock bypass */
112 + cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
114 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
115 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
116 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
118 +//**************************
119 map_write(map, datum, adr);
120 chip->state = FL_WRITING;
122 @@ -1072,7 +1101,13 @@
125 if (chip_ready(map, adr))
128 + tmp = map_read(map, adr);
129 + if(map_word_equal(map, tmp, datum))
135 /* Latency issues. Drop the lock, wait a while and retry */
136 UDELAY(map, chip, adr, 1);
137 @@ -1084,8 +1119,17 @@
138 /* FIXME - should have reset delay before continuing */
140 if (++retry_cnt <= MAX_WORD_RETRIES)
142 +//****** Storlink SoC ******
144 + cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
145 + cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
146 + cfi_send_gen_cmd(0x20, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
155 xip_enable(map, chip, adr);
156 @@ -1171,7 +1215,14 @@
161 +//****** Storlink SoC ******
162 + map_write( map, CMD(0xF0), chipstart );
164 + cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
165 + cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chipstart, map, cfi, cfi->device_type, NULL);
166 + cfi_send_gen_cmd(0x20, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
168 +//**************************
169 /* We are now aligned, write as much as possible */
170 while(len >= map_bankwidth(map)) {
172 @@ -1181,7 +1232,15 @@
173 ret = do_write_oneword(map, &cfi->chips[chipnum],
177 +//****** Storlink SoC ******
179 + /* Get out of unlock bypass mode */
180 + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
181 + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
186 ofs += map_bankwidth(map);
187 buf += map_bankwidth(map);
188 @@ -1189,19 +1248,38 @@
189 len -= map_bankwidth(map);
191 if (ofs >> cfi->chipshift) {
192 +//****** Storlink SoC ******
194 + /* Get out of unlock bypass mode */
195 + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
196 + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
200 if (chipnum == cfi->numchips)
202 chipstart = cfi->chips[chipnum].start;
204 + /* Go into unlock bypass mode for next set of chips */
205 + cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
206 + cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chipstart, map, cfi, cfi->device_type, NULL);
207 + cfi_send_gen_cmd(0x20, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
213 + /* Get out of unlock bypass mode */
214 + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
215 + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
218 /* Write the trailing bytes if any */
219 if (len & (map_bankwidth(map)-1)) {
224 spin_lock(cfi->chips[chipnum].mutex);
226 if (cfi->chips[chipnum].state != FL_READY) {
227 @@ -1221,7 +1299,11 @@
233 + cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
234 + cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chipstart, map, cfi, cfi->device_type, NULL);
235 + cfi_send_gen_cmd(0x20, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
237 tmp_buf = map_read(map, ofs + chipstart);
239 spin_unlock(cfi->chips[chipnum].mutex);
240 @@ -1231,11 +1313,23 @@
241 ret = do_write_oneword(map, &cfi->chips[chipnum],
246 + /* Get out of unlock bypass mode */
247 + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
248 + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
254 + /* Get out of unlock bypass mode */
255 + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
256 + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
261 + map_write( map, CMD(0xF0), chipstart );
265 @@ -1275,6 +1369,7 @@
267 xip_disable(map, chip, cmd_adr);
269 + map_write( map, CMD(0xF0), chip->start ); //Storlink
270 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
271 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
272 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
273 @@ -1535,6 +1630,9 @@
274 DECLARE_WAITQUEUE(wait, current);
277 +#ifdef CONFIG_SL2312_SHARE_PIN
278 + mtd_lock(); // sl2312 share pin lock
282 spin_lock(chip->mutex);
283 @@ -1613,6 +1711,9 @@
284 chip->state = FL_READY;
285 put_chip(map, chip, adr);
286 spin_unlock(chip->mutex);
287 +#ifdef CONFIG_SL2312_SHARE_PIN
288 + mtd_unlock(); // sl2312 share pin lock
293 Index: linux-2.6.23.16/drivers/mtd/chips/map_serial.c
294 ===================================================================
295 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
296 +++ linux-2.6.23.16/drivers/mtd/chips/map_serial.c 2008-03-15 17:03:17.874821522 +0200
299 + * Common code to handle map devices which are simple ROM
300 + * (C) 2000 Red Hat. GPL'd.
301 + * $Id: map_serial.c,v 1.3 2006/06/05 02:34:54 middle Exp $
304 +#include <linux/version.h>
305 +#include <linux/module.h>
306 +#include <linux/types.h>
307 +#include <linux/kernel.h>
310 +#include <asm/byteorder.h>
311 +#include <linux/errno.h>
312 +#include <linux/slab.h>
314 +#include <asm/hardware.h>
315 +#include <linux/mtd/map.h>
316 +#include <linux/mtd/mtd.h>
317 +#include <linux/init.h> //add
318 +#include <asm/arch/sl2312.h>
319 +#include <asm/arch/flash.h>
321 +static int mapserial_erase(struct mtd_info *mtd, struct erase_info *instr);
322 +static int mapserial_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
323 +static int mapserial_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
324 +static void mapserial_nop (struct mtd_info *);
325 +struct mtd_info *map_serial_probe(struct map_info *map);
327 +extern int m25p80_sector_erase(__u32 address, __u32 schip_en);
329 +static struct mtd_chip_driver mapserial_chipdrv = {
330 + probe: map_serial_probe,
331 + name: "map_serial",
332 + module: THIS_MODULE
335 +struct mtd_info *map_serial_probe(struct map_info *map)
337 + struct mtd_info *mtd;
339 + mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
343 + memset(mtd, 0, sizeof(*mtd));
345 + map->fldrv = &mapserial_chipdrv;
347 + mtd->name = map->name;
348 + mtd->type = MTD_OTHER;
349 + mtd->erase = mapserial_erase;
350 + mtd->size = map->size;
351 + mtd->read = mapserial_read;
352 + mtd->write = mapserial_write;
353 + mtd->sync = mapserial_nop;
354 + mtd->flags = (MTD_WRITEABLE|MTD_ERASEABLE);
355 +// mtd->erasesize = 512; // page size;
356 +#ifdef CONFIG_MTD_SL2312_SERIAL_ST
357 + mtd->erasesize = M25P80_SECTOR_SIZE; // block size;
359 + mtd->erasesize = 0x1000; // block size;
362 + __module_get(THIS_MODULE);
363 + //MOD_INC_USE_COUNT;
367 +#define FLASH_ACCESS_OFFSET 0x00000010
368 +#define FLASH_ADDRESS_OFFSET 0x00000014
369 +#define FLASH_WRITE_DATA_OFFSET 0x00000018
370 +#define FLASH_READ_DATA_OFFSET 0x00000018
372 +static __u32 readflash_ctrl_reg(__u32 ofs)
376 + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
377 + return __raw_readl(base);
380 +static void writeflash_ctrl_reg(__u32 data, __u32 ofs)
384 + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
385 + __raw_writel(data, base);
388 +static int mapserial_erase_block(struct map_info *map,unsigned int block)
392 +#ifdef CONFIG_MTD_SL2312_SERIAL_ST
394 + if(!m25p80_sector_erase(block, 0))
395 + return (MTD_ERASE_DONE);
401 + // printk("mapserial_erase_block : erase block %d \n",block);
402 +// opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd;
403 + opcode = 0x80000000 | 0x0200 | 0x50;
404 + address = (block << 13);
405 + writeflash_ctrl_reg(address,FLASH_ADDRESS_OFFSET);
406 + writeflash_ctrl_reg(opcode,FLASH_ACCESS_OFFSET);
407 + opcode=readflash_ctrl_reg(FLASH_ACCESS_OFFSET);
408 + while(opcode&0x80000000)
410 + opcode = readflash_ctrl_reg(FLASH_ACCESS_OFFSET);
414 + return (MTD_ERASE_FAILED);
417 + return (MTD_ERASE_DONE);
421 +static int mapserial_erase(struct mtd_info *mtd, struct erase_info *instr)
423 + struct map_info *map = (struct map_info *)mtd->priv;
426 + unsigned int block;
427 + unsigned int ret=0;
429 + addr = instr->addr;
433 + block = addr / mtd->erasesize;
434 +#ifdef CONFIG_MTD_SL2312_SERIAL_ST
435 + ret = mapserial_erase_block(map,addr);
437 + ret = mapserial_erase_block(map,block);
439 + addr = addr + mtd->erasesize;
440 + len = len - mtd->erasesize;
445 +static int mapserial_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
447 + struct map_info *map = (struct map_info *)mtd->priv;
448 +// printk("mapserial_read : \n");
449 + map->copy_from(map, buf, from, len);
454 +static void mapserial_nop(struct mtd_info *mtd)
456 + /* Nothing to see here */
459 +static int mapserial_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
461 + struct map_info *map = (struct map_info *)mtd->priv;
462 +// printk("mapserial_write : buf %x to %x len %x \n",(int)buf, (int)to, (int)len);
463 + //map->copy_to(map, buf, to, len);
464 + map->copy_to(map, to, buf, len);
469 +int __init map_serial_init(void)
471 + register_mtd_chip_driver(&mapserial_chipdrv);
475 +static void __exit map_serial_exit(void)
477 + unregister_mtd_chip_driver(&mapserial_chipdrv);
480 +module_init(map_serial_init);
481 +module_exit(map_serial_exit);
483 +MODULE_LICENSE("GPL");
484 +MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
485 +MODULE_DESCRIPTION("MTD chip driver for ROM chips");
486 Index: linux-2.6.23.16/drivers/mtd/maps/Kconfig
487 ===================================================================
488 --- linux-2.6.23.16.orig/drivers/mtd/maps/Kconfig 2008-03-15 17:03:14.374622039 +0200
489 +++ linux-2.6.23.16/drivers/mtd/maps/Kconfig 2008-03-15 17:03:17.874821522 +0200
492 This selection automatically selects the map_ram driver.
494 +#***************************************************************************************
495 +# Storlink parallel/Serial Flash configuration
496 +#***************************************************************************************
497 +config MTD_SL2312_CFI
498 + tristate "CFI Flash device mapped on SL2312"
501 + Map driver for SL2312 demo board.
503 +config MTD_SL2312_SERIAL_ATMEL
504 + tristate "ATMEL Serial Flash device mapped on SL2312"
505 + depends on MTD_PARTITIONS && ARCH_SL2312
507 + Map driver for SL2312 demo board.
509 +config MTD_SL2312_SERIAL_ST
510 + tristate "ST Serial Flash device mapped on SL2312"
511 + depends on MTD_PARTITIONS && ARCH_SL2312
513 + Map driver for SL2312 demo board.
515 +config SL2312_SHARE_PIN
516 + tristate "Parallel Flash share pin on SL2312 ASIC"
517 + depends on SL3516_ASIC
521 Index: linux-2.6.23.16/drivers/mtd/maps/sl2312-flash-atmel.c
522 ===================================================================
523 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
524 +++ linux-2.6.23.16/drivers/mtd/maps/sl2312-flash-atmel.c 2008-03-15 17:04:02.877385981 +0200
527 + * $Id: sl2312-flash-atmel.c,v 1.2 2006/06/05 02:35:57 middle Exp $
529 + * Flash and EPROM on Hitachi Solution Engine and similar boards.
531 + * (C) 2001 Red Hat, Inc.
536 +#include <linux/module.h>
537 +#include <linux/types.h>
538 +#include <linux/kernel.h>
541 +#include <linux/mtd/mtd.h>
542 +#include <linux/mtd/map.h>
543 +#include <linux/mtd/partitions.h>
544 +#include <asm/hardware.h>
546 +#include <asm/arch/sl2312.h>
547 +#include <asm/arch/flash.h>
548 +#include <linux/init.h> //add
551 +#define g_page_addr AT45DB321_PAGE_SHIFT //321 : shift 10 ; 642 : shift 11
552 +#define g_chipen SERIAL_FLASH_CHIP0_EN //atmel
554 +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts);
556 +void address_to_page(__u32 address, __u16 *page, __u16 *offset)
558 + *page = address / SPAGE_SIZE;
559 + *offset = address % SPAGE_SIZE;
562 +static __u32 read_flash_ctrl_reg(__u32 ofs)
566 + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
567 + return __raw_readl(base);
570 +static void write_flash_ctrl_reg(__u32 ofs,__u32 data)
574 + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
575 + __raw_writel(data, base);
578 +void atmel_read_status(__u8 cmd, __u8 *data)
583 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | cmd | g_chipen;
584 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
585 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
586 + while(opcode&0x80000000)
588 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
593 + value=read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
594 + *data = value & 0xff;
597 +void main_memory_page_read(__u8 cmd, __u16 page, __u16 offset, __u8 *data)
603 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_4X_DATA | cmd | g_chipen;
604 + address = (page << g_page_addr) + offset;
605 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
606 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
607 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
608 + while(opcode&0x80000000)
610 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
615 + value=read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
616 + *data = value & 0xff;
619 +void buffer_to_main_memory(__u8 cmd, __u16 page)
625 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd | g_chipen;
626 + address = (page << g_page_addr);
627 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
628 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
629 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
630 + while(opcode&0x80000000)
632 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
636 + atmel_read_status(READ_STATUS_SPI, &status);
637 + while(!(status&0x80))
639 + atmel_read_status(READ_STATUS_SPI, &status);
647 +void atmel_flash_read_page(__u32 address, __u8 *buffer, __u32 len)
650 + __u16 page, offset;
653 + address_to_page(address, &page, &offset);
655 + for(i=0; i<len; i++,offset++)
657 + main_memory_page_read(MAIN_MEMORY_PAGE_READ_SPI , page, offset, &byte);
662 +void atmel_flash_program_page(__u32 address, __u8 *buffer, __u32 len)
665 + __u16 page, offset;
668 + address_to_page(address, &page, &offset);
669 + // printk("atmel_flash_program_page: offset %x len %x page %x \n", offset, len, page);
672 + main_memory_to_buffer(MAIN_MEMORY_TO_BUFFER1,page);
674 + for(i=0; i<len; i++,offset++)
676 + pattern = buffer[i];
677 + atmel_buffer_write(BUFFER1_WRITE,offset,pattern);
680 + // printk("atmel_flash_program_page: offset %x \n", offset);
681 + buffer_to_main_memory(BUFFER1_TO_MAIN_MEMORY, page);
682 + // printk("atmel_flash_program_page: buffer_to_main_memory %x page\n", page);
687 +void main_memory_to_buffer(__u8 cmd, __u16 page)
693 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd | g_chipen;
694 + address = (page << g_page_addr);
695 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
696 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
697 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
698 + while(opcode&0x80000000)
700 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
704 + atmel_read_status(READ_STATUS_SPI, &status);
705 + while(!(status&0x80))
707 + atmel_read_status(READ_STATUS_SPI, &status);
714 +void main_memory_page_program(__u8 cmd, __u16 page, __u16 offset, __u8 data)
720 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_DATA | cmd | g_chipen;
721 + address = (page << g_page_addr) + offset;
722 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
723 + write_flash_ctrl_reg(FLASH_WRITE_DATA_OFFSET, data);
724 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
725 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
726 + while(opcode&0x80000000)
728 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
732 + atmel_read_status(READ_STATUS_SPI, &status);
733 + while(!(status&0x80))
735 + atmel_read_status(READ_STATUS_SPI, &status);
741 +void atmel_buffer_write(__u8 cmd, __u16 offset, __u8 data)
746 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_DATA | cmd | g_chipen;
748 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
749 + write_flash_ctrl_reg(FLASH_WRITE_DATA_OFFSET, data);
750 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
751 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
752 + while(opcode&0x80000000)
754 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
761 +void atmel_erase_page(__u8 cmd, __u16 page)
767 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd | g_chipen;
768 + address = (page << g_page_addr);
769 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
770 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
771 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
772 + while(opcode&0x80000000)
774 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
778 + atmel_read_status(READ_STATUS_SPI, &status);
779 + while(!(status&0x80))
781 + atmel_read_status(READ_STATUS_SPI, &status);
788 +void atmel_erase_block(__u8 cmd, __u16 block)
794 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd | g_chipen;
795 + address = (block << 13);
796 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
797 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
798 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
799 + while(opcode&0x80000000)
801 + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
805 + atmel_read_status(READ_STATUS_SPI, &status);
806 + while(!(status&0x80))
808 + atmel_read_status(READ_STATUS_SPI, &status);
815 +void flash_delay(void)
819 + for(i=0; i<50; i++)
826 +__u32 sl2312_read32(struct map_info *map, unsigned long ofs)
830 + __u16 page, offset;
835 + address_to_page(ofs, &page, &offset);
836 + for(i=0; i<4; i++, offset++)
838 + pattern = pattern << 8;
839 + main_memory_page_read(MAIN_MEMORY_PAGE_READ_SPI , page, offset, &byte);
840 +//printk("sl2312_read32:: address = %08x data = %c \n",ofs,byte);
845 + return read_flash_ctrl_reg(ofs);
850 +__u8 sl2312_read8(struct map_info *map, unsigned long ofs)
852 + __u16 page, offset;
855 + address_to_page(ofs, &page, &offset);
856 + main_memory_page_read(MAIN_MEMORY_PAGE_READ_SPI , page, offset, &byte);
857 + //printk("sl2312_read8:: address = %08x data = %c \n",ofs,byte);
862 +void sl2312_write32(struct map_info *map, __u32 d, unsigned long ofs)
865 + __u16 page, offset;
868 + address_to_page(ofs, &page, &offset);
869 + for(i=0; i<4; i++, offset++)
872 + main_memory_page_program(MAIN_MEMORY_PROGRAM_BUFFER1, page, offset, byte);
874 +//printk("sl2312_write32:: address = %08x data = %c \n",ofs,byte);
877 + write_flash_ctrl_reg(ofs, d);
881 +void sl2312_write8(struct map_info *map, __u8 d, unsigned long ofs)
883 + __u16 page, offset;
885 + address_to_page(ofs, &page, &offset);
886 + main_memory_page_program(MAIN_MEMORY_PROGRAM_BUFFER1, page, offset, d);
887 +//printk("sl2312_write8:: address = %08x data = %c \n",ofs,d);
891 +void sl2312_copy_from(struct map_info *map, void *buf, unsigned long ofs, ssize_t len)
895 + __u32 length;//i, j,
897 + //printk("sl2312_copy_from:: address = %08x datalen = %d \n",ofs,len);
900 + buffer = (__u8 *)buf;
903 + size = SPAGE_SIZE - (ofs%SPAGE_SIZE);
906 + atmel_flash_read_page(ofs, buffer, size);
913 + buffer = (__u8 *)buf;
914 + for(i=0; i<length; i+=16)
916 + for(j=0; j<16; j++,buffer++)
918 + if((i*16+j)<length)
919 + printk("%x ",(int)*buffer);
930 +void sl2312_copy_to(struct map_info *map, unsigned long ofs, void *buf, ssize_t len)
935 + buffer = (__u8 *)buf;
936 + //printk("sl2312_copy_to:offset %x len %x \n", ofs, len);
937 +// printk("sl2312_copy_to:buf is %x \n", (int)buf);
941 + size = SPAGE_SIZE - (ofs%SPAGE_SIZE);
944 + atmel_flash_program_page(ofs, buffer, size);
954 +static struct mtd_info *serial_mtd;
956 +static struct mtd_partition *parsed_parts;
958 +static struct map_info sl2312_serial_map = {
959 +// name: "SL2312 serial flash",
960 +// size: 4194304, //0x400000,
963 +// phys: SL2312_FLASH_BASE,
964 +//#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
965 +// //read32: sl2312_read32,
966 +// //read8: sl2312_read8,
967 +// copy_from: sl2312_copy_from,
968 +// //write8: sl2312_write8,
969 +// //write32: sl2312_write32,
970 +// read: sl2312_read32,
971 +// write: sl2312_write32,
972 +// copy_to: sl2312_copy_to
974 + .name = "SL2312 serial flash",
975 + .size = 4194304, //0x400000,
978 + .phys = SL2312_FLASH_BASE,
979 +#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
980 + //read32: sl2312_read32,
981 + //read8: sl2312_read8,
982 + .copy_from = sl2312_copy_from,
983 + //write8: sl2312_write8,
984 + //write32: sl2312_write32,
985 + .read = sl2312_read32,
986 + .write = sl2312_write32,
987 + .copy_to = sl2312_copy_to
993 +static struct mtd_partition sl2312_partitions[] = {
997 + //{ name: "bootloader", offset: 0x00000000, size: 0x20000, },
998 + ///* kernel image */
999 + //{ name: "kerel image", offset: 0x000020000, size: 0x2E0000 },
1000 + ///* All else is writable (e.g. JFFS) */
1001 + //{ name: "user data", offset: 0x00300000, size: 0x00100000, },
1003 + { .name = "bootloader", .offset = 0x00000000, .size = 0x20000, },
1004 + /* kernel image */
1005 + { .name = "kerel image", .offset = 0x000020000, .size = 0xE0000 },
1006 + /* All else is writable (e.g. JFFS) */
1007 + { .name = "user data", .offset = 0x00100000, .size = 0x00300000, },
1014 +static int __init init_sl2312_maps(void)
1017 + struct mtd_partition *parts;
1019 + serial_mtd = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
1023 + memset(serial_mtd, 0, sizeof(struct mtd_info));
1024 + //sl2312flash_map.virt = (unsigned long)ioremap(SL2312_FLASH_BASE, FLASH_SIZE);
1025 + //sl2312_serial_map.map_priv_1 = (unsigned long)ioremap(SL2312_FLASH_BASE, SFLASH_SIZE);//(unsigned long)FLASH_VBASE;
1026 + sl2312_serial_map.virt = (unsigned long)ioremap(SL2312_FLASH_BASE, SFLASH_SIZE);//(unsigned long)ioremap(FLASH_START, SFLASH_SIZE);
1027 + if (!sl2312_serial_map.virt) {
1028 + printk(" failed to ioremap \n");
1031 + serial_mtd = do_map_probe("map_serial", &sl2312_serial_map);
1033 + //serial_mtd->module = THIS_MODULE;
1034 + serial_mtd->owner = THIS_MODULE;
1038 +#ifdef CONFIG_MTD_REDBOOT_PARTS
1039 + nr_parts = parse_redboot_partitions(serial_mtd, &parsed_parts);
1041 + printk(KERN_NOTICE "Found RedBoot partition table.\n");
1042 + else if (nr_parts < 0)
1043 + printk(KERN_NOTICE "Error looking for RedBoot partitions.\n");
1045 + parsed_parts = sl2312_partitions;
1046 + parts = sl2312_partitions;
1047 + nr_parts = sizeof(sl2312_partitions)/sizeof(*parts);
1048 + nr_parts = sizeof(sl2312_partitions)/sizeof(*parsed_parts);
1049 +#endif /* CONFIG_MTD_REDBOOT_PARTS */
1052 + add_mtd_partitions(serial_mtd, parsed_parts, nr_parts);
1054 + add_mtd_device(serial_mtd);
1059 +static void __exit cleanup_sl2312_maps(void)
1062 + del_mtd_partitions(serial_mtd);
1064 + del_mtd_device(serial_mtd);
1066 + map_destroy(serial_mtd);
1071 +module_init(init_sl2312_maps);
1072 +module_exit(cleanup_sl2312_maps);
1076 +MODULE_LICENSE("GPL");
1077 +MODULE_AUTHOR("Plus Chen <plus@storlink.com.tw>");
1078 +MODULE_DESCRIPTION("MTD map driver for Storlink Sword boards");
1080 Index: linux-2.6.23.16/drivers/mtd/maps/sl2312-flash-cfi.c
1081 ===================================================================
1082 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1083 +++ linux-2.6.23.16/drivers/mtd/maps/sl2312-flash-cfi.c 2008-03-15 17:04:09.377756409 +0200
1085 +/*======================================================================
1087 + This program is free software; you can redistribute it and/or modify
1088 + it under the terms of the GNU General Public License as published by
1089 + the Free Software Foundation; either version 2 of the License, or
1090 + (at your option) any later version.
1092 + This program is distributed in the hope that it will be useful,
1093 + but WITHOUT ANY WARRANTY; without even the implied warranty of
1094 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1095 + GNU General Public License for more details.
1097 + You should have received a copy of the GNU General Public License
1098 + along with this program; if not, write to the Free Software
1099 + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1100 +======================================================================*/
1102 +#include <linux/module.h>
1103 +#include <linux/types.h>
1104 +#include <linux/kernel.h>
1105 +#include <linux/slab.h>
1106 +#include <linux/ioport.h>
1107 +#include <linux/init.h>
1108 +#include <linux/string.h>
1110 +#include <linux/mtd/mtd.h>
1111 +#include <linux/mtd/map.h>
1112 +#include <linux/mtd/partitions.h>
1114 +#include <asm/hardware.h>
1115 +#include <asm/io.h>
1116 +#include <asm/system.h>
1117 +#include <asm/arch/sl2312.h>
1118 +#include <linux/mtd/kvctl.h>
1119 +#include "sl2312_flashmap.h"
1122 +//extern int parse_afs_partitions(struct mtd_info *, struct mtd_partition **);
1124 +/* the base address of FLASH control register */
1125 +#define FLASH_CONTROL_BASE_ADDR (IO_ADDRESS(SL2312_FLASH_CTRL_BASE))
1126 +#define SL2312_GLOBAL_BASE_ADDR (IO_ADDRESS(SL2312_GLOBAL_BASE))
1128 +/* define read/write register utility */
1129 +#define FLASH_READ_REG(offset) (__raw_readl(offset+FLASH_CONTROL_BASE_ADDR))
1130 +#define FLASH_WRITE_REG(offset,val) (__raw_writel(val,offset+FLASH_CONTROL_BASE_ADDR))
1132 +/* the offset of FLASH control register */
1133 +enum EMAC_REGISTER {
1134 + FLASH_ID = 0x0000,
1135 + FLASH_STATUS = 0x0008,
1136 + FLASH_TYPE = 0x000c,
1137 + FLASH_ACCESS = 0x0020,
1138 + FLASH_ADDRESS = 0x0024,
1139 + FLASH_DATA = 0x0028,
1140 + FLASH_TIMING = 0x002c,
1143 +//#define FLASH_BASE FLASH_CONTROL_BASE_ADDR
1144 +//#define FLASH_SIZE 0x00800000 //INTEGRATOR_FLASH_SIZE
1146 +//#define FLASH_PART_SIZE 8388608
1148 +static unsigned int flash_indirect_access = 0;
1150 +#ifdef CONFIG_SL2312_SHARE_PIN
1151 +static unsigned int chip_en = 0x00000000;
1153 +void sl2312flash_enable_parallel_flash(void)
1155 + unsigned int reg_val;
1157 + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
1158 + reg_val = reg_val & 0xfffffffd;
1159 + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
1163 +void sl2312flash_disable_parallel_flash(void)
1165 + unsigned int reg_val;
1167 + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
1168 + reg_val = reg_val | 0x00000002;
1169 + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
1175 +static struct map_info sl2312flash_map =
1177 + name: "SL2312 CFI Flash",
1180 + //bankwidth: 1, //for 8 bits width
1181 + phys: SL2312_FLASH_BASE,
1184 +static struct mtd_info *mtd;
1186 +static struct mtd_partition sl2312_partitions[] = {
1189 + name: "bootloader",
1190 + offset: 0x00000000,
1192 +// mask_flags: MTD_WRITEABLE,
1194 + /* kernel image */
1196 + name: "kerel image",
1197 + offset: 0x00020000,
1200 + /* All else is writable (e.g. JFFS) */
1202 + name: "user data",
1203 + offset: 0x00300000,
1211 +static int __init sl2312flash_init(void)
1213 + struct mtd_partition *parts;
1216 +#ifndef CONFIG_SL2312_SHARE_PIN
1217 + unsigned int reg_val;
1220 + printk("SL2312 MTD Driver Init.......\n");
1222 +#ifndef CONFIG_SL2312_SHARE_PIN
1223 + /* enable flash */
1224 + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
1225 + reg_val = reg_val & 0xfffffffd;
1226 + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
1228 + sl2312flash_enable_parallel_flash(); /* enable Parallel FLASH */
1230 + FLASH_WRITE_REG(FLASH_ACCESS,0x00004000); /* parallel flash direct access mode */
1231 + ret = FLASH_READ_REG(FLASH_ACCESS);
1232 + if (ret == 0x00004000)
1234 + flash_indirect_access = 0; /* parallel flash direct access */
1238 + flash_indirect_access = 1; /* parallel flash indirect access */
1242 + * Also, the CFI layer automatically works out what size
1243 + * of chips we have, and does the necessary identification
1244 + * for us automatically.
1246 +#ifdef CONFIG_GEMINI_IPI
1247 + sl2312flash_map.virt = FLASH_VBASE;//(unsigned int *)ioremap(SL2312_FLASH_BASE, FLASH_SIZE);
1249 + sl2312flash_map.virt = (unsigned int *)ioremap(SL2312_FLASH_BASE, FLASH_SIZE);
1251 + //printk("sl2312flash_map.virt = %08x\n",(unsigned int)sl2312flash_map.virt);
1253 +// simple_map_init(&sl2312flash_map);
1255 + mtd = do_map_probe("cfi_probe", &sl2312flash_map);
1258 +#ifdef CONFIG_SL2312_SHARE_PIN
1259 + sl2312flash_disable_parallel_flash(); /* disable Parallel FLASH */
1263 + mtd->owner = THIS_MODULE;
1264 +// mtd->erase = flash_erase;
1265 +// mtd->read = flash_read;
1266 +// mtd->write = flash_write;
1268 + parts = sl2312_partitions;
1269 + nr_parts = sizeof(sl2312_partitions)/sizeof(*parts);
1270 + ret = add_mtd_partitions(mtd, parts, nr_parts);
1271 + /*If we got an error, free all resources.*/
1273 + del_mtd_partitions(mtd);
1276 +#ifdef CONFIG_SL2312_SHARE_PIN
1277 + sl2312flash_disable_parallel_flash(); /* disable Parallel FLASH */
1279 + printk("SL2312 MTD Driver Init Success ......\n");
1283 +static void __exit sl2312flash_exit(void)
1286 + del_mtd_partitions(mtd);
1290 + if (sl2312flash_map.virt) {
1291 + iounmap((void *)sl2312flash_map.virt);
1292 + sl2312flash_map.virt = 0;
1296 +char chrtohex(char c)
1299 + if ((c >= '0') && (c <= '9'))
1304 + else if ((c >= 'a') && (c <= 'f'))
1306 + val = 10 + (c - 'a');
1309 + else if ((c >= 'A') && (c <= 'F'))
1311 + val = 10 + (c - 'A');
1314 + printk("<1>Error number\n");
1319 +int get_vlaninfo(vlaninfo* vlan)
1321 + vctl_mheader head;
1323 + struct mtd_info *mymtd=NULL;
1324 + int i, j, loc = 0;
1325 + char *payload=0, *tmp1, *tmp2, tmp3[9];
1328 + #ifdef CONFIG_SL2312_SHARE_PIN
1329 + sl2312flash_enable_parallel_flash();
1331 + for(i=0;i<MAX_MTD_DEVICES;i++)
1333 + mymtd=get_mtd_device(NULL,i);
1334 + // printk("mymtd->name: %s\n", mymtd->name);
1335 + if(mymtd && !strcmp(mymtd->name,"VCTL"))
1337 + // printk("%s\n", mymtd->name);
1341 + if( i >= MAX_MTD_DEVICES)
1343 + printk("Can't find version control\n");
1344 + #ifdef CONFIG_SL2312_SHARE_PIN
1345 + sl2312flash_disable_parallel_flash();
1350 + if (!mymtd | !mymtd->read)
1352 + printk("<1>Can't read Version Configuration\n");
1353 + #ifdef CONFIG_SL2312_SHARE_PIN
1354 + sl2312flash_disable_parallel_flash();
1359 + mymtd->read(mymtd, 0, VCTL_HEAD_SIZE, &retlen, (u_char*)&head);
1360 + // printk("entry header: %c%c%c%c\n", head.header[0], head.header[1], head.header[2], head.header[3]);
1361 + // printk("entry number: %x\n", head.entry_num);
1362 + if ( strncmp(head.header, "FLFM", 4) )
1364 + printk("VCTL is a erase block\n");
1365 + #ifdef CONFIG_SL2312_SHARE_PIN
1366 + sl2312flash_disable_parallel_flash();
1371 + for (i = 0; i < head.entry_num; i++)
1373 + mymtd->read(mymtd, loc, VCTL_ENTRY_LEN, &retlen, (u_char*)&entry);
1374 + // printk("type: %x\n", entry.type);
1375 + // printk("size: %x\n", entry.size);
1376 + strncpy(tmp3, entry.header, 4);
1377 + if (entry.type == VCT_VLAN)
1379 + for (j = 0; j < 6 ; j++)
1381 + vlan[0].mac[j] = 0;
1382 + vlan[1].mac[j] = 0;
1384 + vlan[0].vlanid = 1;
1385 + vlan[1].vlanid = 2;
1386 + vlan[0].vlanmap = 0x7F;
1387 + vlan[1].vlanmap = 0x80;
1389 + payload = (char *)kmalloc(entry.size - VCTL_ENTRY_LEN, GFP_KERNEL);
1390 + loc += VCTL_ENTRY_LEN;
1391 + mymtd->read(mymtd, loc, entry.size - VCTL_ENTRY_LEN, &retlen, payload);
1392 + // printk("%s\n", payload);
1393 + tmp1 = strstr(payload, "MAC1:");
1394 + tmp2 = strstr(payload, "MAC2:");
1397 + #ifdef CONFIG_SL2312_SHARE_PIN
1398 + sl2312flash_disable_parallel_flash();
1400 + printk("Error VCTL format!!\n");
1407 + for (j = 0; j < 6; j++)
1409 + vlan[0].mac[j] = chrtohex(tmp1[2*j])*16 + chrtohex(tmp1[(2*j)+1]);
1410 + vlan[1].mac[j] = chrtohex(tmp2[2*j])*16 + chrtohex(tmp2[(2*j)+1]);
1412 + tmp1 = strstr(payload, "ID1:");
1413 + tmp2 = strstr(payload, "ID2:");
1416 + vlan[0].vlanid = tmp1[0] - '0';
1417 + vlan[1].vlanid = tmp2[0] - '0';
1418 + tmp1 = strstr(payload, "MAP1:");
1419 + tmp2 = strstr(payload, "MAP2:");
1422 + vlan[0].vlanmap = chrtohex(tmp1[0]) * 16 + chrtohex(tmp1[1]);
1423 + vlan[1].vlanmap = chrtohex(tmp2[0]) * 16 + chrtohex(tmp2[1]);
1424 + // printk("Vlan1 id:%x map:%02x mac:%x%x%x%x%x%x\n", vlan[0].vlanid, vlan[0].vlanmap, vlan[0].mac[0], vlan[0].mac[1], vlan[0].mac[2], vlan[0].mac[3], vlan[0].mac[4], vlan[0].mac[5]);
1425 + // printk("Vlan2 id:%x map:%02x mac:%x%x%x%x%x%x\n", vlan[1].vlanid, vlan[1].vlanmap, vlan[1].mac[0], vlan[1].mac[1], vlan[1].mac[2], vlan[1].mac[3], vlan[1].mac[4], vlan[1].mac[5]);
1428 + loc += entry.size;
1430 + if ( entry.type == VCT_VLAN )
1432 + #ifdef CONFIG_SL2312_SHARE_PIN
1433 + sl2312flash_disable_parallel_flash();
1438 + if (i >= head.entry_num)
1439 + printk("Can't find vlan information\n");
1440 + #ifdef CONFIG_SL2312_SHARE_PIN
1441 + sl2312flash_disable_parallel_flash();
1446 +EXPORT_SYMBOL(get_vlaninfo);
1449 +module_init(sl2312flash_init);
1450 +module_exit(sl2312flash_exit);
1452 +MODULE_AUTHOR("Storlink Ltd");
1453 +MODULE_DESCRIPTION("CFI map driver");
1454 +MODULE_LICENSE("GPL");
1455 Index: linux-2.6.23.16/drivers/mtd/maps/sl2312-flash-m25p80.c
1456 ===================================================================
1457 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1458 +++ linux-2.6.23.16/drivers/mtd/maps/sl2312-flash-m25p80.c 2008-03-15 17:04:15.378098557 +0200
1461 + * $Id: sl2312-flash-m25p80.c,v 1.2 2006/06/02 08:46:02 middle Exp $
1463 + * Flash and EPROM on Hitachi Solution Engine and similar boards.
1465 + * (C) 2001 Red Hat, Inc.
1470 +#include <linux/module.h>
1471 +#include <linux/types.h>
1472 +#include <linux/kernel.h>
1474 +#include <asm/io.h>
1475 +#include <linux/mtd/mtd.h>
1476 +#include <linux/mtd/map.h>
1477 +#include <linux/mtd/partitions.h>
1478 +#include <asm/hardware.h>
1480 +#include <asm/arch/sl2312.h>
1481 +#include <asm/arch/flash.h>
1482 +#include <linux/init.h> //add
1483 +#define g_chipen SERIAL_FLASH_CHIP0_EN //ST
1485 +//static int m25p80_page_program(__u32 address, __u8 data, __u32 schip_en);
1486 +static void m25p80_write_cmd(__u8 cmd, __u32 schip_en);
1487 +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts);
1490 +static __u32 read_flash_ctrl_reg(__u32 ofs)
1494 + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
1495 + return __raw_readl(base);
1498 +static void write_flash_ctrl_reg(__u32 ofs,__u32 data)
1502 + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
1503 + __raw_writel(data, base);
1506 +static void m25p80_read(__u32 address, __u8 *data, __u32 schip_en)
1508 + __u32 opcode,status;
1511 + //opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ;
1512 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_DATA | M25P80_READ;
1513 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
1517 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1518 + status=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1519 + while(status&0x80000000)
1521 + status=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1526 + value=read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1527 + *data = value & 0xff;
1530 +static int m25p80_page_program(__u32 address, __u8 *data, __u32 schip_en)
1535 + int res = FLASH_ERR_OK;
1536 + //volatile FLASH_DATA_T* data_ptr = (volatile FLASH_DATA_T*) data;
1537 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ_STATUS;
1541 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1542 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1543 + while(tmp&0x80000000)
1545 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1549 + //middle delay_ms(130);
1550 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1551 + if((status&0x02)==0x02)
1553 + //middle delay_ms(100);
1554 + m25p80_write_cmd(M25P80_WRITE_DISABLE, schip_en);
1558 + m25p80_write_cmd(M25P80_WRITE_ENABLE, schip_en);
1559 + ////middle delay_ms(10);
1560 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_DATA | M25P80_PAGE_PROGRAM;
1561 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
1562 + write_flash_ctrl_reg(FLASH_WRITE_DATA_OFFSET, *data);
1564 + //status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1565 + //while(status!=data)
1567 + // status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1568 + // //middle delay_ms(10);
1573 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1574 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1575 + while(tmp&0x80000000)
1577 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1581 + //opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1583 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ_STATUS;
1588 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1589 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1590 + while(tmp&0x80000000)
1592 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1596 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1597 + //while(status&0xfd)
1598 + while(status&0x01)
1600 + //if((status&0x9c)!=0)
1601 + // printf(" m25p80_page_program Protect Status = %x\n",status);
1602 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1603 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1604 + while(tmp&0x80000000)
1606 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1610 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1613 + //middle delay_ms(50);
1615 + //printf("status = %x, data = %x\n",status,data);
1616 + if((status&0x02)==0x02)
1618 + //middle delay_ms(100);
1619 + m25p80_write_cmd(M25P80_WRITE_DISABLE, schip_en);
1621 + //};//while (len > 0)
1625 +void m25p80_copy_from(struct map_info *map, void *buf, unsigned long ofs, ssize_t len)
1629 + __u32 length;//i, j,
1632 + buffer = (__u8 *)buf;
1635 + m25p80_read(ofs, buffer, g_chipen);
1643 +__u32 m25p80_read32(struct map_info *map, unsigned long ofs)
1646 + return read_flash_ctrl_reg(ofs);
1651 +void m25p80_write32(struct map_info *map, __u32 d, unsigned long ofs)
1654 + write_flash_ctrl_reg(ofs, d);
1658 +void m25p80_copy_to(struct map_info *map, unsigned long ofs, void *buf, ssize_t len)
1660 + __u32 size, i, ret;
1664 + if(len >= M25P80_PAGE_SIZE)
1665 + size = M25P80_PAGE_SIZE;
1669 + for(i=0;i<size;i++)
1671 + ret = m25p80_page_program( (ofs+i), (buf+i), g_chipen);
1673 + buf+=M25P80_PAGE_SIZE;
1674 + ofs+=M25P80_PAGE_SIZE;
1675 + len-=M25P80_PAGE_SIZE;
1682 +static struct mtd_info *serial_mtd;
1684 +static struct mtd_partition *parsed_parts;
1686 +static struct map_info m25p80_map = {
1688 + .name = "SL2312 serial flash m25p80",
1689 + .size = 1048576, //0x100000,
1692 + .phys = SL2312_FLASH_BASE,
1693 +#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
1694 + .copy_from = m25p80_copy_from,
1695 + .read = m25p80_read32,
1696 + .write = m25p80_write32,
1697 + .copy_to = m25p80_copy_to
1703 +static struct mtd_partition m25p80_partitions[] = {
1706 + { .name = "bootloader", .offset = 0x00000000, .size = 0x20000, },
1707 + /* kernel image */
1708 + { .name = "kerel image", .offset = 0x000020000, .size = 0xC0000 },
1709 + /* All else is writable (e.g. JFFS) */
1710 + { .name = "user data", .offset = 0x000E0000, .size = 0x00010000, },
1718 + for(i=0;i<0x100;i++)
1722 +int m25p80_sector_erase(__u32 address, __u32 schip_en)
1727 + int res = FLASH_ERR_OK;
1728 + //printf("\n-->m25p80_sector_erase");
1729 + if(address >= FLASH_START)
1730 + address-=FLASH_START;
1732 + m25p80_write_cmd(M25P80_WRITE_ENABLE, schip_en);
1733 + //printf("\n m25p80_sector_erase : after we-en");
1734 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | M25P80_SECTOR_ERASE;
1735 + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
1736 + #ifdef MIDWAY_DIAG
1739 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1740 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1741 + while(tmp&0x80000000)
1743 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1748 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ_STATUS;
1749 + #ifdef MIDWAY_DIAG
1753 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1754 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1755 + while(tmp&0x80000000)
1757 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1761 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1762 + //while(status&0xfd)
1763 + while(status&0x01)
1765 + //if((status&0x9c)!=0)
1766 + // printf(" m25p80_sector_erase Protect Status = %x\n",status);
1767 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1768 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1769 + while(tmp&0x80000000)
1771 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1775 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1778 + //middle delay_ms(50);
1780 + if((status&0x02)==0x02)
1782 + //middle delay_ms(100);
1783 + m25p80_write_cmd(M25P80_WRITE_DISABLE, schip_en);
1785 + //printf("\n<--m25p80_sector_erase");
1789 +static void m25p80_write_cmd(__u8 cmd, __u32 schip_en)
1797 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE | cmd;
1801 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1802 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1803 + while(tmp&0x80000000)
1805 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1810 + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ_STATUS;
1814 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1815 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1816 + while(tmp&0x80000000)
1818 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1822 + //middle delay_ms(130);
1823 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1824 + //printf("\ncmd =%x status = %x",cmd,status);
1825 + if(cmd==M25P80_WRITE_ENABLE)
1827 + //printf("\n**-->enable** status = %x",status);
1828 + //middle delay_ms(100);
1829 + while((status&0x03) != 2)
1831 + //if((status&0x9c)!=0)
1832 + // printf(" M25P80_WRITE_ENABLE Protect Status = %x\n",status);
1834 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1835 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1836 + while(tmp&0x80000000)
1838 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1841 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1842 + //printf("\n**enable** status = %x",status);
1845 + //middle delay_ms(100);
1848 + else if(cmd==M25P80_WRITE_DISABLE)
1850 + //while((status&0x03) == 2)
1851 + // printf("\n**disable** status = %x",status);
1852 + //middle delay_ms(100);
1853 + while((status&0x03) != 0)
1855 + //m25p80_write_status((status&0xfd),schip_en);
1856 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1857 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1858 + while(tmp&0x80000000)
1860 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1864 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1865 + //printf("\n**disable** status = %x",status);
1868 + //middle delay_ms(50);
1873 + //while((status&0x01) !=0)
1874 + while((status&0x01) !=0)
1876 + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
1877 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1878 + while(tmp&0x80000000)
1880 + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
1884 + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
1887 + //middle delay_ms(50);
1892 + //printf("\n<-- status = %x",status);
1895 +static int __init init_sl2312_m25p80(void)
1898 + struct mtd_partition *parts;
1900 + serial_mtd = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
1904 + memset(serial_mtd, 0, sizeof(struct mtd_info));
1905 + m25p80_map.virt = (unsigned long)ioremap(SL2312_FLASH_BASE, SFLASH_SIZE);//(unsigned long)ioremap(FLASH_START, SFLASH_SIZE);
1906 + if (!m25p80_map.virt) {
1907 + printk(" failed to ioremap \n");
1910 + serial_mtd = do_map_probe("map_serial", &m25p80_map);
1912 + serial_mtd->owner = THIS_MODULE;
1916 +#ifdef CONFIG_MTD_REDBOOT_PARTS
1917 + nr_parts = parse_redboot_partitions(serial_mtd, &parsed_parts);
1919 + printk(KERN_NOTICE "Found RedBoot partition table.\n");
1920 + else if (nr_parts < 0)
1921 + printk(KERN_NOTICE "Error looking for RedBoot partitions.\n");
1923 + parsed_parts = m25p80_partitions;
1924 + parts = m25p80_partitions;
1925 + nr_parts = sizeof(m25p80_partitions)/sizeof(*parts);
1926 + nr_parts = sizeof(m25p80_partitions)/sizeof(*parsed_parts);
1927 +#endif /* CONFIG_MTD_REDBOOT_PARTS */
1930 + add_mtd_partitions(serial_mtd, parsed_parts, nr_parts);
1932 + add_mtd_device(serial_mtd);
1937 +static void __exit cleanup_sl2312_m25p80(void)
1940 + del_mtd_partitions(serial_mtd);
1942 + del_mtd_device(serial_mtd);
1944 + map_destroy(serial_mtd);
1949 +module_init(init_sl2312_m25p80);
1950 +module_exit(cleanup_sl2312_m25p80);
1954 +MODULE_LICENSE("GPL");
1955 +MODULE_AUTHOR("Plus Chen <plus@storlink.com.tw>");
1956 +MODULE_DESCRIPTION("MTD map driver for Storlink Sword boards");
1958 Index: linux-2.6.23.16/drivers/mtd/maps/sl2312_flashmap.h
1959 ===================================================================
1960 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1961 +++ linux-2.6.23.16/drivers/mtd/maps/sl2312_flashmap.h 2008-03-15 17:03:17.874821522 +0200
1964 + * Please note that the name are used in mkflash script. Therefore
1965 + * don't change them. If you want to add different partitions, you
1966 + * will need to modify mkflash script as well so that the end image
1967 + * is what you include here!
1969 + * Also, the 7th item is always the size, so please don't add extra
1970 + * spaces in the name or other items.
1975 +static struct mtd_partition sl2312_partitions[] = {
1976 + { name: "RedBoot", offset: 0x00000000, size: 0x00020000, },
1977 + { name: "Kernel", offset: 0x00020000, size: 0x00100000, },
1978 + { name: "Ramdisk", offset: 0x00120000, size: 0x00500000, },
1979 + { name: "etc", offset: 0x00620000, size: 0x001A0000, },
1980 + { name: "VCTL", offset: 0x007C0000, size: 0x00010000, },
1981 + { name: "cfg", offset: 0x007D0000, size: 0x00020000, },
1982 + { name: "FIS directory", offset: 0x007F0000, size: 0x00010000, }
1984 Index: linux-2.6.23.16/drivers/mtd/maps/sl2312_flashmap.h.16MB
1985 ===================================================================
1986 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1987 +++ linux-2.6.23.16/drivers/mtd/maps/sl2312_flashmap.h.16MB 2008-03-15 17:03:17.874821522 +0200
1990 + * Please note that the name are used in mkflash script. Therefore
1991 + * don't change them. If you want to add different partitions, you
1992 + * will need to modify mkflash script as well so that the end image
1993 + * is what you include here!
1995 + * Also, the 7th item is always the size, so please don't add extra
1996 + * spaces in the name or other items.
2001 +static struct mtd_partition sl2312_partitions[] = {
2002 + { name: "RedBoot", offset: 0x00000000, size: 0x00020000, },
2003 + { name: "Kernel", offset: 0x00020000, size: 0x00300000, },
2004 + { name: "Ramdisk", offset: 0x00320000, size: 0x00600000, },
2005 + { name: "Application", offset: 0x00920000, size: 0x00600000, },
2006 + { name: "VCTL", offset: 0x00F20000, size: 0x00020000, },
2007 + { name: "CurConf", offset: 0x00F40000, size: 0x000A0000, },
2008 + { name: "FIS directory", offset: 0x00FE0000, size: 0x00020000, }
2010 Index: linux-2.6.23.16/drivers/mtd/maps/sl2312_flashmap.h.8MB
2011 ===================================================================
2012 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2013 +++ linux-2.6.23.16/drivers/mtd/maps/sl2312_flashmap.h.8MB 2008-03-15 17:03:17.874821522 +0200
2016 + * Please note that the name are used in mkflash script. Therefore
2017 + * don't change them. If you want to add different partitions, you
2018 + * will need to modify mkflash script as well so that the end image
2019 + * is what you include here!
2021 + * Also, the 7th item is always the size, so please don't add extra
2022 + * spaces in the name or other items.
2027 +static struct mtd_partition sl2312_partitions[] = {
2028 + { name: "RedBoot", offset: 0x00000000, size: 0x00020000, },
2029 + { name: "Kernel", offset: 0x00020000, size: 0x00200000, },
2030 + { name: "Ramdisk", offset: 0x00220000, size: 0x00280000, },
2031 + { name: "Application", offset: 0x004A0000, size: 0x00300000, },
2032 + { name: "VCTL", offset: 0x007A0000, size: 0x00020000, },
2033 + { name: "CurConf", offset: 0x007C0000, size: 0x00020000, },
2034 + { name: "FIS directory", offset: 0x007E0000, size: 0x00020000, }
2036 Index: linux-2.6.23.16/drivers/mtd/mtdchar.c
2037 ===================================================================
2038 --- linux-2.6.23.16.orig/drivers/mtd/mtdchar.c 2008-03-15 17:03:14.374622039 +0200
2039 +++ linux-2.6.23.16/drivers/mtd/mtdchar.c 2008-03-15 17:03:17.874821522 +0200
2041 enum mtd_file_modes mode;
2044 +/***********************************************************************
2045 +/* Storlink SoC -- flash
2046 +/***********************************************************************/
2047 +#ifdef CONFIG_SL2312_SHARE_PIN
2048 +unsigned int share_pin_flag=0; // bit0:FLASH, bit1:UART, bit2:EMAC, bit3-4:IDE
2049 +unsigned int check_sleep_flag=0; // bit0:FLASH, bit1:IDE
2050 +static spinlock_t sl2312_flash_lock = SPIN_LOCK_UNLOCKED;
2051 +EXPORT_SYMBOL(share_pin_flag);
2053 +DECLARE_WAIT_QUEUE_HEAD(wq);
2054 +extern struct wait_queue_head_t *flash_wait;
2055 +unsigned int flash_req=0;
2058 + struct task_struct *tsk = current;
2059 + unsigned int value ;
2060 + unsigned long flags;
2062 + DECLARE_WAITQUEUE(wait, tsk);
2063 + add_wait_queue(&wq, &wait);
2066 + set_task_state(tsk, TASK_INTERRUPTIBLE);
2067 + spin_lock_irqsave(&sl2312_flash_lock,flags);
2068 + if((share_pin_flag&0x1E)){//||(check_sleep_flag&0x00000002)) {
2069 + spin_unlock_irqrestore(&sl2312_flash_lock, flags);
2070 + check_sleep_flag |= 0x00000001;
2072 + printk("mtd yield %x %x\n",share_pin_flag,check_sleep_flag);
2073 + wake_up_interruptible(&flash_wait);
2077 + check_sleep_flag &= ~0x01;
2078 + share_pin_flag |= 0x00000001 ; // set share pin flag
2079 + spin_unlock_irqrestore(&sl2312_flash_lock, flags);
2080 + value = readl(IO_ADDRESS((SL2312_GLOBAL_BASE+GLOBAL_MISC_REG)));
2081 + value = value & (~PFLASH_SHARE_BIT) ;
2082 + writel(value,IO_ADDRESS((SL2312_GLOBAL_BASE+GLOBAL_MISC_REG)));
2084 + printk("mtd Go %x %x\n",share_pin_flag,check_sleep_flag);
2085 + tsk->state = TASK_RUNNING;
2086 + remove_wait_queue(&wq, &wait);
2094 + unsigned int value ;
2095 + unsigned long flags;
2097 + spin_lock_irqsave(&sl2312_flash_lock,flags); // Disable IRQ
2098 + value = readl(IO_ADDRESS((SL2312_GLOBAL_BASE+GLOBAL_MISC_REG)));
2099 + value = value | PFLASH_SHARE_BIT ; // Disable Flash PADs
2100 + writel(value,IO_ADDRESS((SL2312_GLOBAL_BASE+GLOBAL_MISC_REG)));
2101 + share_pin_flag &= ~(0x00000001); // clear share pin flag
2102 + check_sleep_flag &= ~0x00000001;
2103 + spin_unlock_irqrestore(&sl2312_flash_lock, flags); // Restore IRQ
2104 + if (check_sleep_flag & 0x00000002)
2106 + check_sleep_flag &= ~(0x00000002);
2107 + wake_up_interruptible(&flash_wait);
2109 + DEBUG(MTD_DEBUG_LEVEL0, "Flash Unlock...\n");
2113 +/***********************************************************************/
2115 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
2117 struct mtd_file_info *mfi = file->private_data;
2118 @@ -162,13 +233,21 @@
2122 +#ifdef CONFIG_SL2312_SHARE_PIN
2123 + mtd_lock(); // sl2312 share pin lock
2126 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
2128 if (*ppos + count > mtd->size)
2129 count = mtd->size - *ppos;
2133 +#ifdef CONFIG_SL2312_SHARE_PIN
2134 + mtd_unlock(); // sl2312 share pin lock
2139 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
2140 and pass them directly to the MTD functions */
2141 @@ -178,8 +257,12 @@
2143 kbuf=kmalloc(count, GFP_KERNEL);
2147 +#ifdef CONFIG_SL2312_SHARE_PIN
2148 + mtd_unlock(); // sl2312 share pin lock
2157 if (copy_to_user(buf, kbuf, retlen)) {
2159 +#ifdef CONFIG_SL2312_SHARE_PIN
2160 + mtd_unlock(); // sl2312 share pin lock
2165 @@ -235,13 +321,19 @@
2171 +#ifdef CONFIG_SL2312_SHARE_PIN
2172 + mtd_unlock(); // sl2312 share pin lock
2180 +#ifdef CONFIG_SL2312_SHARE_PIN
2181 + mtd_unlock(); // sl2312 share pin lock
2183 return total_retlen;
2186 @@ -255,24 +347,40 @@
2190 +#ifdef CONFIG_SL2312_SHARE_PIN
2191 + mtd_lock(); // sl2312 share pin lock
2194 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
2196 - if (*ppos == mtd->size)
2197 + if (*ppos == mtd->size){
2198 +#ifdef CONFIG_SL2312_SHARE_PIN
2199 + mtd_unlock(); // sl2312 share pin lock
2204 if (*ppos + count > mtd->size)
2205 count = mtd->size - *ppos;
2209 +#ifdef CONFIG_SL2312_SHARE_PIN
2210 + mtd_unlock(); // sl2312 share pin lock
2215 if (count > MAX_KMALLOC_SIZE)
2216 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
2218 kbuf=kmalloc(count, GFP_KERNEL);
2222 +#ifdef CONFIG_SL2312_SHARE_PIN
2223 + mtd_unlock(); // sl2312 share pin lock
2232 if (copy_from_user(kbuf, buf, len)) {
2234 +#ifdef CONFIG_SL2312_SHARE_PIN
2235 + mtd_unlock(); // sl2312 share pin lock
2240 @@ -323,11 +434,17 @@
2244 +#ifdef CONFIG_SL2312_SHARE_PIN
2245 + mtd_unlock(); // sl2312 share pin lock
2252 +#ifdef CONFIG_SL2312_SHARE_PIN
2253 + mtd_unlock(); // sl2312 share pin lock
2255 return total_retlen;
2258 @@ -381,36 +498,67 @@
2260 struct mtd_info_user info;
2262 +#ifdef CONFIG_SL2312_SHARE_PIN
2263 + mtd_lock(); // sl2312 share pin lock
2266 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
2268 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
2270 if (!access_ok(VERIFY_READ, argp, size))
2272 +#ifdef CONFIG_SL2312_SHARE_PIN
2273 + mtd_unlock(); // sl2312 share pin lock
2278 if (cmd & IOC_OUT) {
2279 if (!access_ok(VERIFY_WRITE, argp, size))
2281 +#ifdef CONFIG_SL2312_SHARE_PIN
2282 + mtd_unlock(); // sl2312 share pin lock
2289 case MEMGETREGIONCOUNT:
2290 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
2292 +#ifdef CONFIG_SL2312_SHARE_PIN
2293 + mtd_unlock(); // sl2312 share pin lock
2299 case MEMGETREGIONINFO:
2301 struct region_info_user ur;
2303 - if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
2304 + if (copy_from_user(&ur, argp, sizeof(struct region_info_user))) {
2305 +#ifdef CONFIG_SL2312_SHARE_PIN
2306 + mtd_unlock(); // sl2312 share pin lock
2311 - if (ur.regionindex >= mtd->numeraseregions)
2312 + if (ur.regionindex >= mtd->numeraseregions) {
2313 +#ifdef CONFIG_SL2312_SHARE_PIN
2314 + mtd_unlock(); // sl2312 share pin lock
2318 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
2319 - sizeof(struct mtd_erase_region_info)))
2320 + sizeof(struct mtd_erase_region_info))) {
2321 +#ifdef CONFIG_SL2312_SHARE_PIN
2322 + mtd_unlock(); // sl2312 share pin lock
2329 @@ -433,7 +581,12 @@
2330 struct erase_info *erase;
2332 if(!(file->f_mode & 2))
2334 +#ifdef CONFIG_SL2312_SHARE_PIN
2335 + mtd_unlock(); // sl2312 share pin lock
2340 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
2343 if (copy_from_user(&erase->addr, argp,
2344 sizeof(struct erase_info_user))) {
2346 +#ifdef CONFIG_SL2312_SHARE_PIN
2347 + mtd_unlock(); // sl2312 share pin lock
2352 @@ -484,14 +640,26 @@
2353 struct mtd_oob_buf buf;
2354 struct mtd_oob_ops ops;
2356 - if(!(file->f_mode & 2))
2357 + if(!(file->f_mode & 2)) {
2358 +#ifdef CONFIG_SL2312_SHARE_PIN
2359 + mtd_unlock(); // sl2312 share pin lock
2364 - if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
2365 + if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) {
2366 +#ifdef CONFIG_SL2312_SHARE_PIN
2367 + mtd_unlock(); // sl2312 share pin lock
2372 - if (buf.length > 4096)
2373 + if (buf.length > 4096) {
2374 +#ifdef CONFIG_SL2312_SHARE_PIN
2375 + mtd_unlock(); // sl2312 share pin lock
2380 if (!mtd->write_oob)
2382 @@ -499,8 +667,12 @@
2383 ret = access_ok(VERIFY_READ, buf.ptr,
2384 buf.length) ? 0 : EFAULT;
2388 +#ifdef CONFIG_SL2312_SHARE_PIN
2389 + mtd_unlock(); // sl2312 share pin lock
2394 ops.ooblen = buf.length;
2395 ops.ooboffs = buf.start & (mtd->oobsize - 1);
2396 @@ -536,19 +708,35 @@
2397 struct mtd_oob_buf buf;
2398 struct mtd_oob_ops ops;
2400 - if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
2401 + if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) {
2402 +#ifdef CONFIG_SL2312_SHARE_PIN
2403 + mtd_unlock(); // sl2312 share pin lock
2408 - if (buf.length > 4096)
2409 + if (buf.length > 4096) {
2410 +#ifdef CONFIG_SL2312_SHARE_PIN
2411 + mtd_unlock(); // sl2312 share pin lock
2416 - if (!mtd->read_oob)
2417 + if (!mtd->read_oob) {
2418 +#ifdef CONFIG_SL2312_SHARE_PIN
2419 + mtd_unlock(); // sl2312 share pin lock
2424 ret = access_ok(VERIFY_WRITE, buf.ptr,
2425 buf.length) ? 0 : -EFAULT;
2428 +#ifdef CONFIG_SL2312_SHARE_PIN
2429 + mtd_unlock(); // sl2312 share pin lock
2434 ops.ooblen = buf.length;
2435 ops.ooboffs = buf.start & (mtd->oobsize - 1);
2436 @@ -580,7 +768,12 @@
2437 struct erase_info_user info;
2439 if (copy_from_user(&info, argp, sizeof(info)))
2441 +#ifdef CONFIG_SL2312_SHARE_PIN
2442 + mtd_unlock(); // sl2312 share pin lock
2449 @@ -594,7 +787,12 @@
2450 struct erase_info_user info;
2452 if (copy_from_user(&info, argp, sizeof(info)))
2454 +#ifdef CONFIG_SL2312_SHARE_PIN
2455 + mtd_unlock(); // sl2312 share pin lock
2462 @@ -629,11 +827,21 @@
2465 if (copy_from_user(&offs, argp, sizeof(loff_t)))
2467 +#ifdef CONFIG_SL2312_SHARE_PIN
2468 + mtd_unlock(); // sl2312 share pin lock
2472 if (!mtd->block_isbad)
2476 +#ifdef CONFIG_SL2312_SHARE_PIN
2477 + mtd_unlock(); // sl2312 share pin lock
2479 return mtd->block_isbad(mtd, offs);
2484 @@ -642,11 +850,21 @@
2487 if (copy_from_user(&offs, argp, sizeof(loff_t)))
2489 +#ifdef CONFIG_SL2312_SHARE_PIN
2490 + mtd_unlock(); // sl2312 share pin lock
2494 if (!mtd->block_markbad)
2498 +#ifdef CONFIG_SL2312_SHARE_PIN
2499 + mtd_unlock(); // sl2312 share pin lock
2501 return mtd->block_markbad(mtd, offs);
2506 @@ -654,8 +872,12 @@
2510 - if (copy_from_user(&mode, argp, sizeof(int)))
2511 + if (copy_from_user(&mode, argp, sizeof(int))) {
2512 +#ifdef CONFIG_SL2312_SHARE_PIN
2513 + mtd_unlock(); // sl2312 share pin lock
2518 mfi->mode = MTD_MODE_NORMAL;
2520 @@ -670,7 +892,12 @@
2522 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
2525 +#ifdef CONFIG_SL2312_SHARE_PIN
2526 + mtd_unlock(); // sl2312 share pin lock
2531 switch (mfi->mode) {
2532 case MTD_MODE_OTP_FACTORY:
2533 @@ -701,12 +928,24 @@
2535 struct otp_info info;
2537 - if (mfi->mode != MTD_MODE_OTP_USER)
2538 + if (mfi->mode != MTD_MODE_OTP_USER) {
2539 +#ifdef CONFIG_SL2312_SHARE_PIN
2540 + mtd_unlock(); // sl2312 share pin lock
2543 - if (copy_from_user(&info, argp, sizeof(info)))
2545 + if (copy_from_user(&info, argp, sizeof(info))) {
2546 +#ifdef CONFIG_SL2312_SHARE_PIN
2547 + mtd_unlock(); // sl2312 share pin lock
2550 - if (!mtd->lock_user_prot_reg)
2552 + if (!mtd->lock_user_prot_reg) {
2553 +#ifdef CONFIG_SL2312_SHARE_PIN
2554 + mtd_unlock(); // sl2312 share pin lock
2558 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
2561 @@ -742,8 +981,12 @@
2565 - if (!mtd->read_oob || !mtd->write_oob)
2566 + if (!mtd->read_oob || !mtd->write_oob) {
2567 +#ifdef CONFIG_SL2312_SHARE_PIN
2568 + mtd_unlock(); // sl2312 share pin lock
2574 case MTD_MODE_NORMAL:
2575 @@ -766,6 +1009,10 @@
2579 +#ifdef CONFIG_SL2312_SHARE_PIN
2580 + mtd_unlock(); // sl2312 share pin lock
2584 } /* memory_ioctl */
2586 Index: linux-2.6.23.16/drivers/mtd/nand/Kconfig
2587 ===================================================================
2588 --- linux-2.6.23.16.orig/drivers/mtd/nand/Kconfig 2008-03-15 17:03:14.374622039 +0200
2589 +++ linux-2.6.23.16/drivers/mtd/nand/Kconfig 2008-03-15 17:03:17.874821522 +0200
2591 This enables the driver for the autronix autcpu12 board to
2592 access the SmartMediaCard.
2594 +config MTD_NAND_SL2312
2595 + tristate "NAND Flash device on Storlink board"
2596 + depends on ARM && MTD_NAND && ARCH_SL2312
2598 + This enables the driver for the Storlink board to
2599 + access the nand device.
2601 config MTD_NAND_EDB7312
2602 tristate "Support for Cirrus Logic EBD7312 evaluation board"
2603 depends on ARCH_EDB7312
2604 Index: linux-2.6.23.16/drivers/mtd/nand/sl2312-flash-nand.c
2605 ===================================================================
2606 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2607 +++ linux-2.6.23.16/drivers/mtd/nand/sl2312-flash-nand.c 2008-03-15 17:03:17.874821522 +0200
2610 + * drivers/mtd/sl2312.c
2612 + * $Id: sl2312-flash-nand.c,v 1.5 2006/06/15 07:02:29 middle Exp $
2614 + * Copyright (C) 2001 Toshiba Corporation
2616 + * 2003 (c) MontaVista Software, Inc. This file is licensed under
2617 + * the terms of the GNU General Public License version 2. This program
2618 + * is licensed "as is" without any warranty of any kind, whether express
2623 +#include <linux/slab.h>
2624 +#include <linux/init.h>
2625 +#include <linux/module.h>
2626 +#include <linux/mtd/mtd.h>
2627 +#include <linux/mtd/nand.h>
2628 +#include <linux/mtd/nand_ecc.h>
2629 +#include <linux/mtd/partitions.h>
2630 +#include <linux/delay.h>
2631 +#include <asm/io.h>
2632 +#include <asm/hardware.h>
2633 +#include <asm/arch/sl2312.h>
2634 +#include "sl2312-flash-nand.h"
2637 +#include <linux/errno.h>
2638 +#include <linux/sched.h>
2639 +#include <linux/types.h>
2640 +#include <linux/mtd/compatmac.h>
2641 +#include <linux/interrupt.h>
2642 +#include <linux/bitops.h>
2646 + * NAND low-level MTD interface functions
2648 +static void sl2312_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
2649 +static void sl2312_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
2650 +static int sl2312_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
2652 +static int sl2312_nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
2653 +static int sl2312_nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
2654 +static int sl2312_nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
2655 +static int sl2312_nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
2656 +static int sl2312_nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
2657 + size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
2658 +static int sl2312_nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
2659 +static int sl2312_nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
2660 + unsigned long count, loff_t to, size_t * retlen);
2661 +static int sl2312_nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
2662 + unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
2663 +static int sl2312_nand_erase (struct mtd_info *mtd, struct erase_info *instr, int allowbbt);
2664 +static void sl2312_nand_sync (struct mtd_info *mtd);
2665 +static int sl2312_nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf, struct nand_oobinfo *oobsel);
2666 +static int sl2312_nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt);
2667 +static int sl2312_nand_erase_block(struct mtd_info *mtd, int page);
2670 + * MTD structure for sl2312 NDFMC
2672 +static struct mtd_info *sl2312_mtd = NULL;
2673 +static int nand_page=0,nand_col=0;
2675 +/* Define default oob placement schemes for large and small page devices */
2676 +static struct nand_oobinfo nand_oob_8 = {
2677 + .useecc = MTD_NANDECC_AUTOPLACE,
2679 + .eccpos = {0, 1, 2},
2680 + .oobfree = { {3, 2}, {6, 2} }
2683 +static struct nand_oobinfo nand_oob_16 = {
2684 + .useecc = MTD_NANDECC_AUTOPLACE,
2686 + .eccpos = {0, 1, 2, 3, 6, 7},
2687 + .oobfree = { {8, 8} }
2690 +static struct nand_oobinfo nand_oob_64 = {
2691 + .useecc = MTD_NANDECC_AUTOPLACE,
2694 + 40, 41, 42, 43, 44, 45, 46, 47,
2695 + 48, 49, 50, 51, 52, 53, 54, 55,
2696 + 56, 57, 58, 59, 60, 61, 62, 63},
2697 + .oobfree = { {2, 38} }
2702 + * Define partitions for flash device
2704 +/* the base address of FLASH control register */
2705 +#define FLASH_CONTROL_BASE_ADDR (IO_ADDRESS(SL2312_FLASH_CTRL_BASE))
2706 +#define SL2312_GLOBAL_BASE_ADDR (IO_ADDRESS(SL2312_GLOBAL_BASE))
2707 +//#define SL2312_FLASH_BASE_ADDR (IO_ADDRESS(SL2312_FLASH_BASE))
2708 +#define SL2312_FLASH_BASE_ADDR FLASH_VADDR(SL2312_FLASH_BASE)
2709 +static unsigned int CHIP_EN;
2710 +/* define read/write register utility */
2711 +//#define FLASH_READ_REG(offset) (__raw_readl(offset+FLASH_CONTROL_BASE_ADDR))
2712 +//#define FLASH_WRITE_REG(offset,val) (__raw_writel(val,offset+FLASH_CONTROL_BASE_ADDR))
2713 +//#define FLASH_READ_DATA(offset) (__raw_readb(offset+SL2312_FLASH_BASE_ADDR))
2714 +//#define FLASH_WRITE_DATA(offset,val) (__raw_writeb(val,offset+SL2312_FLASH_BASE_ADDR))
2716 +unsigned int FLASH_READ_REG(unsigned int addr)
2718 + unsigned int *base;
2719 + unsigned int data;
2721 + base = (unsigned int *)(FLASH_CONTROL_BASE_ADDR + addr);
2726 +void FLASH_WRITE_REG(unsigned int addr,unsigned int data)
2728 + unsigned int *base;
2730 + base = (unsigned int *)(FLASH_CONTROL_BASE_ADDR + addr);
2735 +unsigned int FLASH_READ_DATA(unsigned int addr)
2737 + unsigned char *base;
2738 + unsigned int data;
2740 + base = (unsigned char *)(SL2312_FLASH_BASE_ADDR + addr);
2745 +void FLASH_WRITE_DATA(unsigned int addr,unsigned int data)
2747 + unsigned char *base;
2749 + base = (unsigned char *)(SL2312_FLASH_BASE_ADDR + addr);
2754 +/* the offset of FLASH control register */
2755 +enum NFLASH_REGISTER {
2756 + NFLASH_ID = 0x0000,
2757 + NFLASH_STATUS = 0x0008,
2758 + NFLASH_TYPE = 0x000c,
2759 + NFLASH_ACCESS = 0x0030,
2760 + NFLASH_COUNT = 0x0034,
2761 + NFLASH_CMD_ADDR = 0x0038,
2762 + NFLASH_ADDRESS = 0x003C,
2763 + NFLASH_DATA = 0x0040,
2764 + NFLASH_TIMING = 0x004C,
2765 + NFLASH_ECC_STATUS = 0x0050,
2766 + NFLASH_ECC_CONTROL = 0x0054,
2767 + NFLASH_ECC_OOB = 0x005c,
2768 + NFLASH_ECC_CODE_GEN0 = 0x0060,
2769 + NFLASH_ECC_CODE_GEN1 = 0x0064,
2770 + NFLASH_ECC_CODE_GEN2 = 0x0068,
2771 + NFLASH_ECC_CODE_GEN3 = 0x006C,
2772 + NFLASH_FIFO_CONTROL = 0x0070,
2773 + NFLASH_FIFO_STATUS = 0x0074,
2774 + NFLASH_FIFO_ADDRESS = 0x0078,
2775 + NFLASH_FIFO_DATA = 0x007c,
2780 +//#define FLASH_BASE FLASH_CONTROL_BASE_ADDR
2781 +//#define FLASH_SIZE 0x00800000 //INTEGRATOR_FLASH_SIZE
2783 +//#define FLASH_PART_SIZE 8388608
2785 +//static unsigned int flash_indirect_access = 0;
2788 +#ifdef CONFIG_SL2312_SHARE_PIN
2789 +void sl2312flash_enable_nand_flash(void)
2791 + unsigned int reg_val;
2793 + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
2794 + reg_val = reg_val & 0xfffffffb;
2795 + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
2799 +void sl2312flash_disable_nand_flash(void)
2801 + unsigned int reg_val;
2803 + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
2804 + reg_val = reg_val | 0x00000004;
2805 + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
2810 +extern struct nand_oobinfo jffs2_oobinfo;
2812 + * Define partitions for flash devices
2815 +static struct mtd_partition sl2312_partitions[] = {
2816 + { name: "RedBoot", offset: 0x00000000, size: 0x0020000, },
2817 + { name: "Kernel", offset: 0x00020000, size: 0x00200000, },
2818 + { name: "Ramdisk", offset: 0x00220000, size: 0x00280000, },
2819 + { name: "Application", offset: 0x004A0000, size: 0x00320000, },
2820 + { name: "VCTL", offset: 0x007C0000, size: 0x20000, },
2821 + { name: "CurConf", offset: 0x007E0000, size: 0x20000, },
2822 + { name: "FIS directory", offset: 0x007e0000, size: 0x00020000, }
2828 + * hardware specific access to control-lines
2830 +static void sl2312_hwcontrol(struct mtd_info *mtd, int cmd)
2836 +static int sl2312_nand_scan_bbt(struct mtd_info *mtd)
2842 + * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2843 + * @mtd: MTD device structure
2844 + * @ofs: offset relative to mtd start
2846 +static int sl2312_nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2848 + /* Check for invalid offset */
2849 + if (ofs > mtd->size)
2852 + return sl2312_nand_block_checkbad (mtd, ofs, 1, 0);
2856 + * nand_block_checkbad - [GENERIC] Check if a block is marked bad
2857 + * @mtd: MTD device structure
2858 + * @ofs: offset from device start
2859 + * @getchip: 0, if the chip is already selected
2860 + * @allowbbt: 1, if its allowed to access the bbt area
2862 + * Check, if the block is bad. Either by reading the bad block table or
2863 + * calling of the scan function.
2866 +static int sl2312_nand_erase_block(struct mtd_info *mtd, int page)
2869 + /* Send commands to erase a page */
2870 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
2872 + if(mtd->oobblock > 528)
2873 + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff21); // 3 address & 2 command
2875 + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff11); // 2 address & 2 command
2877 + FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x0000d060); // write read id command
2878 + FLASH_WRITE_REG(NFLASH_ADDRESS, page); //write address 0x00
2882 + /* read maker code */
2883 + opcode = 0x80003000|DWIDTH|CHIP_EN; //set start bit & 8bits write command
2884 + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
2886 + while(opcode&0x80000000) //polling flash access 31b
2888 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
2889 + //sl2312_flash_delay();
2895 +void sl2312_flash_delay(void)
2899 + for(i=0; i<50; i++)
2903 +static int sl2312_nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
2905 + struct nand_chip *this = mtd->priv;
2908 + return this->block_bad(mtd, ofs, getchip);
2910 + /* Return info from the table */
2911 + return nand_isbad_bbt (mtd, ofs, allowbbt);
2915 + * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2916 + * @mtd: MTD device structure
2917 + * @ofs: offset relative to mtd start
2919 +static int sl2312_nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2921 + struct nand_chip *this = mtd->priv;
2924 + if ((ret = sl2312_nand_block_isbad(mtd, ofs))) {
2925 + /* If it was bad already, return success and do nothing. */
2931 + return this->block_markbad(mtd, ofs);
2935 + * Get chip for selected access
2937 +static inline void sl2312_nand_get_chip (struct nand_chip *this, struct mtd_info *mtd, int new_state, int *erase_state)
2940 + DECLARE_WAITQUEUE (wait, current);
2943 + * Grab the lock and see if the device is available
2944 + * For erasing, we keep the spinlock until the
2945 + * erase command is written.
2948 + spin_lock_bh (&this->chip_lock);
2950 + if (this->state == FL_READY) {
2951 + this->state = new_state;
2952 + if (new_state != FL_ERASING)
2953 + spin_unlock_bh (&this->chip_lock);
2957 + if (this->state == FL_ERASING) {
2958 + if (new_state != FL_ERASING) {
2959 + this->state = new_state;
2960 + spin_unlock_bh (&this->chip_lock);
2961 + this->select_chip(mtd, 0); /* select in any case */
2962 + this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2967 + set_current_state (TASK_UNINTERRUPTIBLE);
2968 + add_wait_queue (&this->wq, &wait);
2969 + spin_unlock_bh (&this->chip_lock);
2971 + remove_wait_queue (&this->wq, &wait);
2976 +* read device ready pin
2978 +static int sl2312_device_ready(struct mtd_info *mtd)
2982 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
2983 + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f000070); //set only command no address and two data
2985 + FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x00000070); //write read status command
2988 + ready = 0x80002000|DWIDTH|CHIP_EN; //set start bit & 8bits read command
2989 + FLASH_WRITE_REG(NFLASH_ACCESS, ready);
2991 + while(ready&0x80000000) //polling flash access 31b
2993 + ready=FLASH_READ_REG(NFLASH_ACCESS);
2994 + //sl2312_flash_delay();
2997 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
2998 + ready=FLASH_READ_REG(NFLASH_DATA)&0xff;
3001 +void sl2312_enable_hwecc(struct mtd_info *mtd, int mode)
3004 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x80000001); //set 31b = 0
3009 +void sl2312_device_setup(void)
3013 +static u_char sl2312_nand_read_byte(struct mtd_info *mtd)
3016 + unsigned int data=0, page=0, col=0, tmp, i;
3018 + printk ("**************************sl2312_nand_read_byte !! \n");
3019 + //page = FLASH_READ_REG(NFLASH_ADDRESS)&0xffffff00;
3020 + //col = FLASH_READ_REG(NFLASH_ADDRESS)&0x000000ff;
3023 + for(i=0;i<(mtd->oobblock+mtd->oobsize);i++)
3026 + data = FLASH_READ_DATA(page*mtd->oobblock +i);
3028 + tmp = FLASH_READ_DATA(page*mtd->oobblock +i);
3033 +static void sl2312_nand_write_byte(struct mtd_info *mtd, u_char byte)
3035 + //struct nand_chip *this = mtd->priv;
3036 + unsigned int page=0, col=0, i;
3037 + u_char *databuf,oobbuf[mtd->oobsize];
3040 + printk ("********************sl2312_nand_write_byte !! \n");
3043 + databuf = kmalloc (mtd->oobsize+mtd->oobblock,GFP_KERNEL);
3046 + printk ("sl2312_nand_write_byte : Unable to allocate SL2312 NAND MTD device structure.\n");
3050 + for(i=0;i<(mtd->oobblock+mtd->oobsize);i++)
3051 + databuf[i] = FLASH_READ_DATA(page*mtd->oobblock +i);
3053 + databuf[col] = byte;
3054 + sl2312_nand_write_ecc (mtd, page, mtd->oobblock, &retlen, databuf, oobbuf, NULL);
3058 +static void sl2312_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
3060 + int i, page=0,col=0;
3061 + struct nand_chip *this = mtd->priv;
3062 + u_char *databuf, *oobbuf;
3067 + printk ("***********************sl2312_nand_write_buf !! \n");
3068 + databuf = &(this->data_buf[0]);
3069 + oobbuf = &(this->data_buf[mtd->oobblock]);
3070 + for (i = 0; i < mtd->oobsize; i++)
3073 + if(len < mtd->oobblock)
3075 + //addr = FLASH_READ_REG(NFLASH_ADDRESS);
3076 + //page = FLASH_READ_REG(NFLASH_ADDRESS)&0xffffff00;
3077 + //col = FLASH_READ_REG(NFLASH_ADDRESS)&0x000000ff;
3081 + sl2312_nand_read_ecc (mtd, page, mtd->oobblock , &retlen, databuf, oobbuf, NULL);
3083 + for(i=col;i<len;i++)
3084 + databuf[col+i] = buf[i];
3086 + sl2312_nand_write_ecc (mtd, page, mtd->oobblock, &retlen, databuf, oobbuf, NULL);
3092 +static void sl2312_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
3094 + int i, page=0,col=0,addr=0,tmp=0;
3095 + //struct nand_chip *this = mtd->priv;
3096 + printk ("********************sl2312_nand_read_buf !! \n");
3097 + if(len < mtd->oobblock)
3099 + //addr = FLASH_READ_REG(NFLASH_ADDRESS);
3100 + //page = FLASH_READ_REG(NFLASH_ADDRESS)&0xffffff00;
3101 + //col = FLASH_READ_REG(NFLASH_ADDRESS)&0x000000ff;
3104 + for (i=col; i<((mtd->oobblock+mtd->oobsize)-col); i++)
3107 + buf[i] = FLASH_READ_DATA(addr+i);
3109 + tmp = FLASH_READ_DATA(addr+i);
3114 +static int sl2312_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
3117 + //struct nand_chip *this = mtd->priv;
3118 + u_char *datatmp, *oobtmp;
3122 + datatmp = kmalloc (mtd->oobblock,GFP_KERNEL);
3123 + oobtmp = kmalloc (mtd->oobsize,GFP_KERNEL);
3125 + if ((!datatmp)||(!oobtmp)) {
3126 + printk ("sl2312_nand_verify_buf : Unable to allocate SL2312 NAND MTD device structure.\n");
3129 + //page = nand_page;
3130 + for(i=0;i<mtd->oobblock;i++)
3131 + datatmp[i] = FLASH_READ_DATA(nand_page*mtd->oobblock +i);
3132 + /* read oobdata */
3133 + for (i = 0; i < mtd->oobsize; i++)
3134 + oobtmp[i] = FLASH_READ_DATA(nand_page*mtd->oobblock + mtd->oobblock + i);
3136 + if(len==mtd->oobblock)
3138 + for (i=0; i<len; i++)
3140 + if (buf[i] != datatmp[i])
3144 + printk("Data verify error -> page: %x, byte: %x \n",nand_page,i);
3149 + else if(len == mtd->oobsize)
3151 + for (i=0; i<len; i++)
3153 + if (buf[i] != oobtmp[i])
3157 + printk("OOB verify error -> page: %x, byte: %x \n",nand_page,i);
3164 + printk (KERN_WARNING "sl2312_nand_verify_buf : verify length not match 0x%08x\n", len);
3176 + * Send command to NAND device
3178 +static void sl2312_nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
3180 + register struct nand_chip *this = mtd->priv;
3185 + * program and erase have their own busy handlers
3186 + * status and sequential in needs no delay
3188 + switch (command) {
3190 + case NAND_CMD_PAGEPROG:
3191 + case NAND_CMD_ERASE1:
3192 + case NAND_CMD_ERASE2:
3193 + case NAND_CMD_SEQIN:
3194 + case NAND_CMD_STATUS:
3195 + case NAND_CMD_READ0:
3198 + * Write out the command to the device.
3200 + if (column != -1 || page_addr != -1) {
3202 + /* Serially input address */
3204 + //FLASH_WRITE_REG(NFLASH_ADDRESS,column);
3207 + opcode = FLASH_READ_REG(NFLASH_ADDRESS);
3209 + if (page_addr != -1)
3210 + //FLASH_WRITE_REG(NFLASH_ADDRESS,opcode|(page_addr<<8));
3211 + nand_page = page_addr;
3216 + case NAND_CMD_RESET:
3217 + if (this->dev_ready)
3219 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
3220 + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff70); //set only command and no other data
3221 + FLASH_WRITE_REG(NFLASH_CMD_ADDR, NAND_CMD_RESET); //write reset command
3223 + opcode = 0x80002000|DWIDTH|CHIP_EN; //set start bit & 8bits read command
3224 + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
3226 + while(opcode&0x80000000) //polling flash access 31b
3228 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
3229 + //sl2312_flash_delay();
3232 + while ( !(sl2312_device_ready(mtd) & 0x40));
3234 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
3235 + //sl2312_flash_delay();
3239 + /* This applies to read commands */
3242 + * If we don't have access to the busy pin, we apply the given
3245 + if (!this->dev_ready) {
3246 + udelay (this->chip_delay);
3247 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
3252 + /* wait until command is processed */
3253 + while (!this->dev_ready(mtd));
3257 +static void nand_read_id(int chip_no, unsigned char *id)
3259 + unsigned int opcode, i;
3262 + CHIP_EN = NFLASH_CHIP0_EN;
3264 + CHIP_EN = NFLASH_CHIP1_EN;
3266 + opcode = FLASH_READ_REG(NFLASH_TYPE);
3268 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
3269 + if((opcode&0x00000300)<=0x00000100)
3270 + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f000100); //set only command & address and two data
3272 + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f000300); //set only command & address and 4 data
3274 + FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x00000090); //write read id command
3275 + FLASH_WRITE_REG(NFLASH_ADDRESS, 0x00000000); //write address 0x00
3277 + /* read maker code */
3278 + opcode = 0x80002000|DWIDTH|CHIP_EN;//|chip0_en; //set start bit & 8bits read command
3279 + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
3280 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
3281 + while(opcode&0x80000000) //polling flash access 31b
3283 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
3284 + //sl2312_flash_delay();
3288 + opcode = FLASH_READ_REG(NFLASH_DATA);
3289 + if(DWIDTH==NFLASH_WiDTH16)
3291 + id[0] = opcode&0xff;
3292 + id[1] = (opcode&0xff00)>>8;
3296 + id[0] = opcode&0xff;
3297 + opcode = 0x80002000|DWIDTH|CHIP_EN;//|chip0_en; //set start bit & 8bits read command
3298 + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
3299 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
3300 + while(opcode&0x80000000) //polling flash access 31b
3302 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
3303 + //sl2312_flash_delay();
3306 + opcode = FLASH_READ_REG(NFLASH_DATA);
3307 + id[1] = (opcode&0xff00)>>8;
3309 + opcode=FLASH_READ_REG(NFLASH_TYPE);
3310 + if((opcode&0x300)>0x100)
3314 + //data cycle 3 & 4 ->not use
3315 + opcode = 0x80002000|DWIDTH|CHIP_EN;//set start bit & 8bits read command
3316 + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
3317 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
3318 + while(opcode&0x80000000) //polling flash access 31b
3320 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
3321 + //sl2312_flash_delay();
3325 + opcode=FLASH_READ_REG(NFLASH_DATA);
3326 + id[2+i] = (opcode&(0xff0000<<i*8))>>(8*(2+i));
3330 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
3334 + * NAND erase a block
3336 +static int sl2312_nand_erase (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
3338 + int page, len, status, pages_per_block, ret, chipnr;
3339 + struct nand_chip *this = mtd->priv;
3341 + DEBUG (MTD_DEBUG_LEVEL3,
3342 + "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
3344 + /* Start address must align on block boundary */
3345 + if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
3346 + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
3350 + /* Length must align on block boundary */
3351 + if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
3352 + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
3356 + /* Do not allow erase past end of device */
3357 + if ((instr->len + instr->addr) > mtd->size) {
3358 + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
3362 + instr->fail_addr = 0xffffffff;
3364 + /* Grab the lock and see if the device is available */
3365 + sl2312_nand_get_chip (this, mtd, FL_ERASING, NULL);
3367 + /* Shift to get first page */
3368 + page = (int) (instr->addr >> this->page_shift);
3369 + chipnr = (int) (instr->addr >> this->chip_shift);
3371 + /* Calculate pages in each block */
3372 + pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
3374 + /* Select the NAND device */
3375 + //this->select_chip(mtd, chipnr);
3376 + this->select_chip(mtd, 0);
3378 + /* Check the WP bit */
3379 + /* Check, if it is write protected */
3380 + status = sl2312_device_ready(mtd);
3381 + if (!(status & 0x80)) {
3382 + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
3383 + instr->state = MTD_ERASE_FAILED;
3387 + /* Loop through the pages */
3390 + instr->state = MTD_ERASING;
3393 + /* Check if we have a bad block, we do not erase bad blocks ! */
3394 + if (this->block_bad(mtd, ((loff_t) page) << this->page_shift, 0)) {
3395 + printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
3396 + //instr->state = MTD_ERASE_FAILED;
3397 + //goto erase_exit;
3400 + /* Invalidate the page cache, if we erase the block which contains
3401 + the current cached page */
3402 + if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
3403 + this->pagebuf = -1;
3406 + ///* Send commands to erase a page */
3407 + //FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
3409 + //if(mtd->oobblock > 528)
3410 + // FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff21); // 3 address & 2 command
3412 + // FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff11); // 2 address & 2 command
3414 + //FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x0000d060); // write read id command
3415 + //FLASH_WRITE_REG(NFLASH_ADDRESS, page); //write address 0x00
3419 + ///* read maker code */
3420 + //opcode = 0x80003000|DWIDTH|CHIP_EN; //set start bit & 8bits write command
3421 + //FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
3423 + //while(opcode&0x80000000) //polling flash access 31b
3425 + // opcode=FLASH_READ_REG(NFLASH_ACCESS);
3426 + // //sl2312_flash_delay();
3428 + // //cond_resched();
3430 + sl2312_nand_erase_block(mtd, page);
3432 + status = this->waitfunc (mtd, this, FL_ERASING);
3433 + /* See if block erase succeeded */
3434 + if (status & 0x01) {
3435 + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
3436 + instr->state = MTD_ERASE_FAILED;
3437 + instr->fail_addr = (page << this->page_shift);
3441 + /* Increment page address and decrement length */
3442 + len -= (1 << this->phys_erase_shift);
3443 + page += pages_per_block;
3445 + /* Check, if we cross a chip boundary */
3446 + if (len && !(page & this->pagemask)) {
3448 + this->select_chip(mtd, 0);
3449 + this->select_chip(mtd, 0);
3451 + //sl2312_flash_delay();
3455 + instr->state = MTD_ERASE_DONE;
3458 + /* De-select the NAND device */
3459 + this->select_chip(mtd, 0);
3460 + spin_unlock_bh (&this->chip_lock);
3462 + ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;;
3463 + /* Do call back function */
3464 + if (!ret && instr->callback)
3465 + instr->callback (instr);
3467 + /* The device is ready */
3468 + spin_lock_bh (&this->chip_lock);
3469 + this->state = FL_READY;
3470 + spin_unlock_bh (&this->chip_lock);
3471 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
3472 + /* Return more or less happy */
3476 +static void sl2312_nand_select_chip(struct mtd_info *mtd, int chip)
3478 + //struct nand_chip *this = mtd->priv;
3482 + CHIP_EN = NFLASH_CHIP0_EN;
3485 + CHIP_EN = NFLASH_CHIP0_EN;
3488 + CHIP_EN = NFLASH_CHIP1_EN;
3491 + CHIP_EN = NFLASH_CHIP0_EN;
3497 + * nand_default_block_markbad - [DEFAULT] mark a block bad
3498 + * @mtd: MTD device structure
3499 + * @ofs: offset from device start
3501 + * This is the default implementation, which can be overridden by
3502 + * a hardware specific driver.
3504 +static int sl2312_nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
3506 + struct nand_chip *this = mtd->priv;
3507 + u_char buf[2] = {0, 0};
3511 + /* Get block number */
3512 + block = ((int) ofs) >> this->bbt_erase_shift;
3513 + this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
3515 + /* Do we have a flash based bad block table ? */
3516 + if (this->options & NAND_USE_FLASH_BBT)
3517 + return nand_update_bbt (mtd, ofs);
3519 + /* We write two bytes, so we dont have to mess with 16 bit access */
3520 + ofs += mtd->oobsize + (this->badblockpos & ~0x01);
3521 + return sl2312_nand_write_oob (mtd, ofs , 2, &retlen, buf);
3524 +/* Appropriate chip should already be selected */
3525 +static int sl2312_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)//(struct mtd_info *mtd, unsigned long page, )
3527 + u_char *buf, *oobbuf;
3529 + unsigned long page, chipnr;
3530 + struct nand_chip *this = mtd->priv;
3533 + page = (int)(ofs >> this->page_shift);
3534 + chipnr = (int)(ofs >> this->chip_shift);
3536 + /* Grab the lock and see if the device is available */
3537 + sl2312_nand_get_chip (this, mtd, FL_READING, NULL);
3538 + /* Select the NAND device */
3539 + this->select_chip(mtd, chipnr);
3543 + buf = kmalloc (mtd->oobblock,GFP_KERNEL);
3544 + oobbuf = kmalloc (mtd->oobsize,GFP_KERNEL);
3546 + if ((!buf)||(!oobbuf)) {
3547 + printk ("sl2312_nand_block_bad : Unable to allocate SL2312 NAND MTD device structure.\n");
3551 + sl2312_nand_read_ecc (mtd, page, mtd->oobblock , &retlen, buf, oobbuf, NULL);
3554 + if(((mtd->oobblock < 528)&&(oobbuf[5] != 0xff))||((mtd->oobblock > 528)&&(oobbuf[0] != 0xff)))
3567 +* Use NAND read ECC
3569 +static int sl2312_nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
3571 + return sl2312_nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
3575 + * NAND read with ECC
3577 +static int sl2312_nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
3578 + size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
3580 + int j, col, page, opcode, i;
3581 + int end=0;//, ecc=0;//, end_page=0;
3582 + int erase_state = 0;
3583 + int read = 0, oob = 0, ecc_failed = 0;//, ecc_status = 0
3584 + struct nand_chip *this = mtd->priv;
3585 + u_char *data_poi, *oob_data = oob_buf;
3586 + //u_char ecc_calc[6];
3587 + //u_char ecc_code[6];
3593 + // use chip default if zero
3594 + if (oobsel == NULL)
3595 + oobsel = &mtd->oobinfo;
3597 + eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
3598 + oob_config = oobsel->eccpos;
3600 + DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
3602 + /* Do not allow reads past end of device */
3603 + if ((from + len) > mtd->size) {
3604 + DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
3609 + /* Grab the lock and see if the device is available */
3610 + sl2312_nand_get_chip (this, mtd ,FL_READING, &erase_state);
3612 + /* Select the NAND device */
3613 + this->select_chip(mtd, 0);
3615 + /* First we calculate the starting page */
3616 + page = from >> this->page_shift;
3618 + //end_page = mtd->oobblock + mtd->oobsize;
3619 + end = mtd->oobblock;
3620 + //ecc = mtd->eccsize;
3621 + /* Get raw starting column */
3622 + col = (from & (mtd->oobblock - 1));
3625 + /* Send the read command */
3626 + //this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
3628 + /* Loop until all data read */
3629 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
3630 + while (read < len) {
3633 + /* If we have consequent page reads, apply delay or wait for ready/busy pin */
3635 + if (!this->dev_ready)
3636 + udelay (this->chip_delay);
3638 + while (!this->dev_ready(mtd));
3642 + * If the read is not page aligned, we have to read into data buffer
3643 + * due to ecc, else we read into return buffer direct
3645 + if (!col && (len - read) >= end)
3646 + data_poi = &buf[read];
3648 + data_poi = this->data_buf;
3650 + /* get oob area, if we have no oob buffer from fs-driver */
3652 + oob_data = &this->data_buf[end];
3657 + switch (eccmode) {
3658 + case NAND_ECC_NONE: { /* No ECC, Read in a page */
3659 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
3663 + case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
3666 + case NAND_ECC_HW3_256: /* Hardware ECC 3 byte /256 byte data: Read in first 256 byte, get ecc, */
3669 + case NAND_ECC_HW3_512:
3670 + case NAND_ECC_HW6_512: /* Hardware ECC 3/6 byte / 512 byte data : Read in a page */
3671 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x80000001); //set 31b = 0
3675 + printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
3676 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0);
3680 + for(i=0;i<end;i++)
3683 + data_poi[i] = FLASH_READ_DATA(page*mtd->oobblock +i);
3685 + /* read oobdata */
3686 + for (i = 0; i < mtd->oobsize; i++)
3689 + oob_data[oob + i] = FLASH_READ_DATA(page*mtd->oobblock +end+i);
3692 + /* Skip ECC, if not active */
3693 + if (eccmode == NAND_ECC_NONE)
3696 + // compare ecc and correct data
3698 + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
3699 + while(!(opcode&0x80000000)) //polling flash access 31b
3701 + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
3702 + //sl2312_flash_delay();
3705 + for(j=0;j<(end/512);j++)
3708 + opcode = 0x00000000|oob_data[mtd->oobsize-3-4*j]<<16|oob_data[mtd->oobsize-2-4*j]<<8|oob_data[mtd->oobsize-1-4*j];
3710 + //opcode=FLASH_READ_REG(NFLASH_ECC_CODE_GEN0+(j*4));
3712 + FLASH_WRITE_REG(NFLASH_ECC_OOB, opcode);
3713 + opcode = 0x00000000|(j<<8); //select ECC code generation 0
3714 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, opcode); //???
3716 + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
3717 + if((opcode&0x00000003)==0x03)
3719 + printk (KERN_WARNING "\nPageRead Uncorrectable error !!\n");
3722 + else if((opcode&0x00000003)==0x01)
3724 + printk (KERN_WARNING "\nPageRead One bit data error !!");
3726 + if((data_poi[(opcode&0xff80)>>7]>>((opcode&0x38)>>3))%1)
3727 + data_poi[(opcode&0xff80)>>7] &= ~(1<<((opcode&0x38)>>3));
3729 + data_poi[(opcode&0xff80)>>7] |= (1<<((opcode&0x38)>>3));
3732 + else if((opcode&0x00000003)==0x02)
3734 + printk (KERN_WARNING "\nPageRead One bit ECC error !!\n");
3736 + else if((opcode&0x00000003)==0x00)
3743 + if (col || (len - read) < end) {
3744 + for (j = col; j < end && read < len; j++)
3745 + buf[read++] = data_poi[j];
3747 + read += mtd->oobblock;
3748 + /* For subsequent reads align to page boundary. */
3750 + /* Increment page address */
3754 + /* De-select the NAND device */
3755 + //this->select_chip(mtd, -1);
3756 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
3757 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_INDIRECT);
3758 + /* Wake up anyone waiting on the device */
3759 + spin_lock_bh (&this->chip_lock);
3760 + this->state = FL_READY;
3761 + wake_up (&this->wq);
3762 + spin_unlock_bh (&this->chip_lock);
3765 + * Return success, if no ECC failures, else -EIO
3766 + * fs driver will take care of that, because
3767 + * retlen == desired len and result == -EIO
3770 + return ecc_failed ? -EIO : 0;
3774 + * Wait for command done. This applies to erase and program only
3775 + * Erase can take up to 400ms and program up to 20ms according to
3776 + * general NAND and SmartMedia specs
3779 +static int sl2312_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this, int state)
3781 + unsigned long timeo = jiffies;
3782 + int status, opcode;
3784 + if (state == FL_ERASING)
3785 + timeo += (HZ * 400) / 1000;
3787 + timeo += (HZ * 20) / 1000;
3789 + spin_lock_bh (&this->chip_lock);
3790 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
3791 + FLASH_WRITE_REG(NFLASH_COUNT, 0x007f000070); //set only command no address and two data
3793 + FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x00000070); //write read status command
3796 + opcode = 0x80002000|DWIDTH|CHIP_EN; //set start bit & 8bits read command
3797 + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
3799 + while(opcode&0x80000000) //polling flash access 31b
3801 + opcode=FLASH_READ_REG(NFLASH_ACCESS);
3802 + //sl2312_flash_delay();
3806 + while (time_before(jiffies, timeo)) {
3807 + /* Check, if we were interrupted */
3808 + if (this->state != state) {
3809 + spin_unlock_bh (&this->chip_lock);
3810 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
3813 + if (this->dev_ready) {
3814 + if (this->dev_ready(mtd))
3817 + if (FLASH_READ_REG(NFLASH_DATA) & 0x40)
3820 + spin_unlock_bh (&this->chip_lock);
3822 + spin_lock_bh (&this->chip_lock);
3824 + status = FLASH_READ_REG(NFLASH_DATA)&0xff;
3825 + spin_unlock_bh (&this->chip_lock);
3826 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
3830 +static int sl2312_nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
3832 + int i, col, page, j=0;
3833 + //int erase_state = 0;
3834 + struct nand_chip *this = mtd->priv;
3835 + u_char *databuf, *oobbuf;
3837 + databuf = &this->data_buf[0];
3838 + oobbuf = &this->data_buf[mtd->oobblock];
3839 + for (i = 0; i < mtd->oobsize; i++)
3842 + DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
3844 + /* Shift to get page */
3845 + page = ((int) from) >> this->page_shift;
3847 + /* Mask to get column */
3848 + col = from & (mtd->oobsize-1); //0x0f;
3850 + /* Initialize return length value */
3852 + sl2312_nand_read_ecc (mtd, page, mtd->oobblock , retlen, databuf, oobbuf, NULL);
3853 + for(i=col,j=0;i<mtd->oobsize||i<(col+len);i++,j++)
3854 + buf[j] = oobbuf[i];
3860 +#define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
3862 +* Use NAND write ECC
3864 +static int sl2312_nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
3866 + return (sl2312_nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
3870 + * NAND write with ECC
3872 +static int sl2312_nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
3873 + size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
3875 + int page, ret = 0, oob = 0, written = 0;
3876 + struct nand_chip *this = mtd->priv;
3878 + DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
3881 + /* Do not allow write past end of device */
3882 + if ((to + len) > mtd->size) {
3883 + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
3887 + /* reject writes, which are not page aligned */
3888 + if (NOTALIGNED (to) || NOTALIGNED(len)) {
3889 + printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
3893 + // if oobsel is NULL, use chip defaults
3894 + if (oobsel == NULL)
3895 + oobsel = &mtd->oobinfo;
3897 + /* Shift to get page */
3898 + page = ((int) to) >> this->page_shift;
3900 + /* Grab the lock and see if the device is available */
3901 + sl2312_nand_get_chip (this, mtd, FL_WRITING, NULL);
3903 + /* Select the NAND device */
3904 + this->select_chip(mtd, 0);
3906 + /* Check the WP bit */
3907 + if (!(sl2312_device_ready(mtd) & 0x80)) {
3908 + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Device is write protected!!!\n");
3913 + /* Loop until all data is written */
3914 + while (written < len) {
3916 + int cnt = mtd->oobblock;
3917 + this->data_poi = (u_char*) &buf[written];
3918 + /* We use the same function for write and writev */
3920 + ret = sl2312_nand_write_page (mtd, this, page, &eccbuf[oob], oobsel);
3921 + oob += mtd->oobsize;
3923 + ret = sl2312_nand_write_page (mtd, this, page, NULL, oobsel);
3928 + /* Update written bytes count */
3930 + /* Increment page address */
3935 + /* De-select the NAND device */
3936 + //this->select_chip(mtd, -1);
3938 + /* Wake up anyone waiting on the device */
3939 + spin_lock_bh (&this->chip_lock);
3940 + this->state = FL_READY;
3941 + wake_up (&this->wq);
3942 + spin_unlock_bh (&this->chip_lock);
3944 + *retlen = written;
3949 + * Nand_page_program function is used for write and writev !
3950 + * This function will always program a full page of data
3951 + * If you call it with a non page aligned buffer, you're lost :)
3953 +static int sl2312_nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf, struct nand_oobinfo *oobsel)
3955 + int i, j, status, opcode;
3956 + u_char ecc_code[16], *oob_data;
3957 + int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
3958 + //int *oob_config = oobsel->eccpos;
3960 + /* pad oob area, if we have no oob buffer from fs-driver */
3962 + oob_data = &this->data_buf[mtd->oobblock];
3963 + for (i = 0; i < mtd->oobsize; i++)
3964 + oob_data[i] = 0xff;
3966 + oob_data = oob_buf;
3968 + /* Send command to begin auto page programming */
3970 + memset(oob_data,0xff,mtd->oobsize);
3971 + /* Write out complete page of data, take care of eccmode */
3972 + switch (eccmode) {
3973 + /* No ecc and software ecc 3/256, write all */
3974 + case NAND_ECC_NONE:
3975 + printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
3976 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
3978 + case NAND_ECC_SOFT:
3981 + /* Hardware ecc 3 byte / 256 data, write first half, get ecc, then second, if 512 byte pagesize */
3982 + case NAND_ECC_HW3_256:
3985 + /* Hardware ecc 3 byte / 512 byte data, write full page */
3986 + case NAND_ECC_HW3_512:
3987 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x80000001); //set 31b = 0
3989 + /* Hardware ecc 6 byte / 512 byte data, write full page */
3990 + case NAND_ECC_HW6_512:
3994 + printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
3995 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
3999 + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
4001 + for(i=0;i<mtd->oobblock;i++)
4004 + FLASH_WRITE_DATA((page*mtd->oobblock)+i,this->data_poi[i]);
4007 + if(eccmode!=NAND_ECC_NONE)
4009 + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
4010 + while(!(opcode&0x80000000)) //polling flash access 31b
4012 + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
4013 + //sl2312_flash_delay();
4018 + for(i=0;i<(mtd->oobblock/512);i++)
4020 + opcode=FLASH_READ_REG(NFLASH_ECC_CODE_GEN0+(i*4));
4023 + oob_data[(mtd->oobsize-j-(i*4))] = (opcode<<((4-j)*8)) >>24;
4027 + ecc_code[15-i*4] = opcode;
4028 + ecc_code[15-i*4-1] = opcode>>8;
4029 + ecc_code[15-i*4-2] = opcode>>16;
4034 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000);
4036 + /* Write out OOB data */
4037 + for(i=0;i<mtd->oobsize;i++)
4040 + FLASH_WRITE_DATA((page*mtd->oobblock)+mtd->oobblock+i,oob_data[i]);
4045 + for(i=0;i<mtd->oobsize;i++)
4048 + FLASH_WRITE_DATA((page*mtd->oobblock)+mtd->oobblock+i,0xff);
4053 + /* call wait ready function */
4054 + status = this->waitfunc (mtd, this, FL_WRITING);
4055 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
4056 + /* See if device thinks it succeeded */
4057 + if (status & 0x01) {
4058 + DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
4059 + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
4063 +#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
4065 + * The NAND device assumes that it is always writing to
4066 + * a cleanly erased page. Hence, it performs its internal
4067 + * write verification only on bits that transitioned from
4068 + * 1 to 0. The device does NOT verify the whole page on a
4069 + * byte by byte basis. It is possible that the page was
4070 + * not completely erased or the page is becoming unusable
4071 + * due to wear. The read with ECC would catch the error
4072 + * later when the ECC page check fails, but we would rather
4073 + * catch it early in the page write stage. Better to write
4074 + * no data than invalid data.
4077 + /* Send command to read back the page */
4078 + this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
4079 + /* Loop through and verify the data */
4080 + if (this->verify_buf(mtd, this->data_poi, mtd->oobblock)) {
4081 + DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
4085 + /* check, if we have a fs-supplied oob-buffer */
4087 + if (this->verify_buf(mtd, oob_data, mtd->oobsize)) {
4088 + DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
4092 + if (eccmode != NAND_ECC_NONE) {
4093 + int ecc_bytes = 0;
4095 + switch (this->eccmode) {
4096 + case NAND_ECC_SOFT:
4097 + case NAND_ECC_HW3_256: ecc_bytes = (mtd->oobblock == 512) ? 6 : 3; break;
4098 + case NAND_ECC_HW3_512: ecc_bytes = 3; break;
4099 + case NAND_ECC_HW6_512: ecc_bytes = 6; break;
4104 + for(i=0;i < (mtd->oobblock+mtd->oobsize);i++)
4106 + if(i>=mtd->oobblock)
4107 + oob_data[i-mtd->oobblock] = FLASH_READ_DATA((page*mtd->oobblock) +i);
4109 + oob_data[0] = FLASH_READ_DATA((page*mtd->oobblock) +i);
4112 + if(this->eccmode == NAND_ECC_HW3_512)
4114 + for(i=0;i<(mtd->oobblock/512);i++)
4118 + if (oob_data[mtd->oobsize-1-j-4*i] != ecc_code[15-j-4*i]) {
4119 + DEBUG (MTD_DEBUG_LEVEL0,
4120 + "%s: Failed ECC write "
4121 + "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
4127 + }//eccmode != NAND_ECC_NONE
4130 + * Terminate the read command. This is faster than sending a reset command or
4131 + * applying a 20us delay before issuing the next programm sequence.
4132 + * This is not a problem for all chips, but I have found a bunch of them.
4134 + //this->select_chip(mtd, -1);
4135 + //this->select_chip(mtd, 0);
4142 + * NAND write with iovec
4144 +static int sl2312_nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
4145 + loff_t to, size_t * retlen)
4147 + return (sl2312_nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, 0));
4150 +static int sl2312_nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
4151 + loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
4153 + int i, page, len, total_len, ret = 0, written = 0;
4154 + struct nand_chip *this = mtd->priv;
4156 + /* Calculate total length of data */
4158 + for (i = 0; i < count; i++)
4159 + total_len += (int) vecs[i].iov_len;
4161 + DEBUG (MTD_DEBUG_LEVEL3,
4162 + "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
4164 + /* Do not allow write past end of page */
4165 + if ((to + total_len) > mtd->size) {
4166 + DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
4170 + /* reject writes, which are not page aligned */
4171 + if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
4172 + printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
4176 + // if oobsel is NULL, use chip defaults
4177 + if (oobsel == NULL)
4178 + oobsel = &mtd->oobinfo;
4180 + /* Shift to get page */
4181 + page = ((int) to) >> this->page_shift;
4183 + /* Grab the lock and see if the device is available */
4184 + sl2312_nand_get_chip (this, mtd, FL_WRITING, NULL);
4186 + /* Select the NAND device */
4187 + this->select_chip(mtd, 0);
4189 + /* Check the WP bit */
4190 + if (!(sl2312_device_ready(mtd) & 0x80)) {
4191 + DEBUG (MTD_DEBUG_LEVEL0, "sl2312_nand_writev_ecc: Device is write protected!!!\n");
4196 + /* Loop until all iovecs' data has been written */
4200 + * Check, if the tuple gives us not enough data for a
4201 + * full page write. Then we can use the iov direct,
4202 + * else we have to copy into data_buf.
4204 + if ((vecs->iov_len - len) >= mtd->oobblock) {
4205 + this->data_poi = (u_char *) vecs->iov_base;
4206 + this->data_poi += len;
4207 + len += mtd->oobblock;
4208 + /* Check, if we have to switch to the next tuple */
4209 + if (len >= (int) vecs->iov_len) {
4216 + * Read data out of each tuple until we have a full page
4217 + * to write or we've read all the tuples.
4220 + while ((cnt < mtd->oobblock) && count) {
4221 + if (vecs->iov_base != NULL && vecs->iov_len) {
4222 + this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
4224 + /* Check, if we have to switch to the next tuple */
4225 + if (len >= (int) vecs->iov_len) {
4231 + this->data_poi = this->data_buf;
4234 + /* We use the same function for write and writev !) */
4235 + ret = sl2312_nand_write_page (mtd, this, page, NULL, oobsel);
4239 + /* Update written bytes count */
4240 + written += mtd->oobblock;;
4242 + /* Increment page address */
4247 + /* De-select the NAND device */
4248 + //this->select_chip(mtd, -1);
4250 + /* Wake up anyone waiting on the device */
4251 + spin_lock_bh (&this->chip_lock);
4252 + this->state = FL_READY;
4253 + wake_up (&this->wq);
4254 + spin_unlock_bh (&this->chip_lock);
4256 + *retlen = written;
4261 +static u_char ffchars[] = {
4262 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4263 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
4267 + * NAND write out-of-band
4269 +static int sl2312_nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
4271 + int column, page, status, ret = 0, j=0;
4272 + struct nand_chip *this = mtd->priv;
4273 + u_char *databuf, *oobbuf;
4276 + databuf = &this->data_buf[0];
4277 + oobbuf = &this->data_buf[mtd->oobblock];
4278 + for (j = 0; j < mtd->oobsize; j++)
4280 +//#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
4284 + DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
4286 + /* Shift to get page */
4287 + page = ((int) to) >> this->page_shift;
4289 + /* Mask to get column */
4290 + column = to & 0x1f;
4292 + /* Initialize return length value */
4295 + /* Do not allow write past end of page */
4296 + if ((column + len) > mtd->oobsize) {
4297 + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
4301 + /* Grab the lock and see if the device is available */
4302 + sl2312_nand_get_chip (this, mtd, FL_WRITING, NULL);
4304 + /* Select the NAND device */
4305 + this->select_chip(mtd, 0);
4307 + /* Reset the chip. Some chips (like the Toshiba TC5832DC found
4308 + in one of my DiskOnChip 2000 test units) will clear the whole
4309 + data page too if we don't do this. I have no clue why, but
4310 + I seem to have 'fixed' it in the doc2000 driver in
4311 + August 1999. dwmw2. */
4312 + this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
4314 + /* Check the WP bit */
4315 + if (!(sl2312_device_ready(mtd) & 0x80)) {
4316 + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Device is write protected!!!\n");
4320 + /* Write out desired data */
4321 + this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page);
4323 + sl2312_nand_read_ecc (mtd, page, mtd->oobblock , retlen, databuf, oobbuf, NULL);
4325 + for(j=column;j<(column+len);j++)
4326 + oobbuf[j] = buf[j-column];
4327 + sl2312_nand_write_ecc (mtd, page, mtd->oobblock, retlen, databuf, oobbuf, NULL);
4329 + status = this->waitfunc (mtd, this, FL_WRITING);
4331 + /* See if device thinks it succeeded */
4332 + if (status & 0x01) {
4333 + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
4337 + /* Return happy */
4342 + /* De-select the NAND device */
4343 + //this->select_chip(mtd, -1);
4345 + /* Wake up anyone waiting on the device */
4346 + spin_lock_bh (&this->chip_lock);
4347 + this->state = FL_READY;
4348 + wake_up (&this->wq);
4349 + spin_unlock_bh (&this->chip_lock);
4357 +static void sl2312_nand_sync (struct mtd_info *mtd)
4359 + struct nand_chip *this = mtd->priv;
4360 + DECLARE_WAITQUEUE (wait, current);
4362 + DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
4365 + /* Grab the spinlock */
4366 + spin_lock_bh (&this->chip_lock);
4368 + /* See what's going on */
4369 + switch (this->state) {
4372 + this->state = FL_SYNCING;
4373 + spin_unlock_bh (&this->chip_lock);
4377 + /* Not an idle state */
4378 + add_wait_queue (&this->wq, &wait);
4379 + spin_unlock_bh (&this->chip_lock);
4382 + remove_wait_queue (&this->wq, &wait);
4386 + /* Lock the device */
4387 + spin_lock_bh (&this->chip_lock);
4389 + /* Set the device to be ready again */
4390 + if (this->state == FL_SYNCING) {
4391 + this->state = FL_READY;
4392 + wake_up (&this->wq);
4395 + /* Unlock the device */
4396 + spin_unlock_bh (&this->chip_lock);
4401 + * Scan for the NAND device
4403 +int sl2312_nand_scan (struct mtd_info *mtd, int maxchips)
4405 + int i, j, nand_maf_id, nand_dev_id, busw;
4406 + struct nand_chip *this = mtd->priv;
4407 + unsigned char id[4];
4409 + /* Get buswidth to select the correct functions*/
4410 + busw = this->options & NAND_BUSWIDTH_16;
4412 + /* check for proper chip_delay setup, set 20us if not */
4413 + if (!this->chip_delay)
4414 + this->chip_delay = 20;
4416 + /* check, if a user supplied command function given */
4417 + if (this->cmdfunc == NULL)
4418 + this->cmdfunc = sl2312_nand_command;
4420 + /* check, if a user supplied wait function given */
4421 + if (this->waitfunc == NULL)
4422 + this->waitfunc = sl2312_nand_waitfunc;
4424 + if (!this->select_chip)
4425 + this->select_chip = sl2312_nand_select_chip;
4426 + if (!this->write_byte)
4427 + this->write_byte = sl2312_nand_write_byte; //busw ? nand_write_byte16 : nand_write_byte;
4428 + if (!this->read_byte)
4429 + this->read_byte = sl2312_nand_read_byte; //busw ? nand_read_byte16 : nand_read_byte;
4430 +// if (!this->write_word)
4431 +// this->write_word = nand_write_word;
4432 +// if (!this->read_word)
4433 +// this->read_word = nand_read_word;
4434 +// if (!this->block_bad)
4435 + this->block_bad = sl2312_nand_block_bad; //nand_block_bad;
4436 + if (!this->block_markbad)
4437 + this->block_markbad = sl2312_nand_default_block_markbad;
4438 + if (!this->write_buf)
4439 + this->write_buf = sl2312_nand_write_buf; //busw ? nand_write_buf16 : nand_write_buf;
4440 + if (!this->read_buf)
4441 + this->read_buf = sl2312_nand_read_buf; //busw ? nand_read_buf16 : nand_read_buf;
4442 + if (!this->verify_buf)
4443 + this->verify_buf = sl2312_nand_verify_buf; //busw ? nand_verify_buf16 : nand_verify_buf;
4444 + if (!this->scan_bbt)
4445 + this->scan_bbt = sl2312_nand_scan_bbt;
4447 + /* Select the device */
4448 + this->select_chip(mtd, 0);
4450 + /* Read manufacturer and device IDs */
4451 + nand_read_id(0,id);
4453 + nand_maf_id = id[0];
4454 + nand_dev_id = id[1];
4456 + /* Print and store flash device information */
4457 + for (i = 0; nand_flash_ids[i].name != NULL; i++) {
4459 + if (nand_dev_id != nand_flash_ids[i].id)
4462 + if (!mtd->name) mtd->name = nand_flash_ids[i].name;
4463 + this->chipsize = nand_flash_ids[i].chipsize << 20;
4465 + /* New devices have all the information in additional id bytes */
4466 + if (!nand_flash_ids[i].pagesize) {
4469 + /* The 4th id byte is the important one */
4471 + /* Calc pagesize */
4472 + mtd->oobblock = 1024 << (extid & 0x3);
4474 + /* Calc oobsize */
4475 + mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
4477 + /* Calc blocksize. Blocksize is multiples of 64KiB */
4478 + mtd->erasesize = (64 * 1024) << (extid & 0x03);
4480 + /* Get buswidth information */
4481 + busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
4484 + /* Old devices have this data hardcoded in the
4485 + * device id table */
4486 + mtd->erasesize = nand_flash_ids[i].erasesize;
4487 + mtd->oobblock = nand_flash_ids[i].pagesize;
4488 + mtd->oobsize = mtd->oobblock / 32;
4489 + busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
4492 + /* Check, if buswidth is correct. Hardware drivers should set
4493 + * this correct ! */
4494 + if (busw != (this->options & NAND_BUSWIDTH_16)) {
4495 + printk (KERN_INFO "NAND device: Manufacturer ID:"
4496 + " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
4497 + nand_manuf_ids[i].name , mtd->name);
4498 + printk (KERN_WARNING
4499 + "NAND bus width %d instead %d bit\n",
4500 + (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
4502 + this->select_chip(mtd, -1);
4506 + /* Calculate the address shift from the page size */
4507 + this->page_shift = ffs(mtd->oobblock) - 1;
4508 + this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
4509 + this->chip_shift = ffs(this->chipsize) - 1;
4511 + /* Set the bad block position */
4512 + this->badblockpos = mtd->oobblock > 512 ?
4513 + NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
4515 + /* Get chip options, preserve non chip based options */
4516 + this->options &= ~NAND_CHIPOPTIONS_MSK;
4517 + this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
4518 + /* Set this as a default. Board drivers can override it, if neccecary */
4519 + this->options |= NAND_NO_AUTOINCR;
4520 + /* Check if this is a not a samsung device. Do not clear the options
4521 + * for chips which are not having an extended id.
4523 + if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
4524 + this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4526 + /* Check for AND chips with 4 page planes */
4527 + // if (this->options & NAND_4PAGE_ARRAY)
4528 + // this->erase_cmd = multi_erase_cmd;
4530 + // this->erase_cmd = single_erase_cmd;
4532 + /* Do not replace user supplied command function ! */
4533 + // if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
4534 + // this->cmdfunc = nand_command_lp;
4536 + /* Try to identify manufacturer */
4537 + for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
4538 + if (nand_manuf_ids[j].id == nand_maf_id)
4541 + printk (KERN_INFO "NAND device: Manufacturer ID:"
4542 + " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
4543 + nand_manuf_ids[j].name , nand_flash_ids[i].name);
4546 + /////////////////////////////
4548 + for (i=1; i < maxchips; i++) {
4549 + this->select_chip(mtd, i);
4551 + /* Send the command for reading device ID */
4552 + nand_read_id(1,id);
4554 + /* Read manufacturer and device IDs */
4555 + if (nand_maf_id != id[0] ||
4556 + nand_dev_id != id[1])
4560 + printk(KERN_INFO "%d NAND chips detected\n", i);
4562 + /* Allocate buffers, if neccecary */
4563 + if (!this->oob_buf) {
4565 + len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
4566 + this->oob_buf = kmalloc (len, GFP_KERNEL);
4567 + if (!this->oob_buf) {
4568 + printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
4571 + this->options |= NAND_OOBBUF_ALLOC;
4574 + if (!this->data_buf) {
4576 + len = mtd->oobblock + mtd->oobsize;
4577 + this->data_buf = kmalloc (len, GFP_KERNEL);
4578 + if (!this->data_buf) {
4579 + if (this->options & NAND_OOBBUF_ALLOC)
4580 + kfree (this->oob_buf);
4581 + printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
4584 + this->options |= NAND_DATABUF_ALLOC;
4587 + /* Store the number of chips and calc total size for mtd */
4588 + this->numchips = i;
4589 + mtd->size = i * this->chipsize;
4590 + /* Convert chipsize to number of pages per chip -1. */
4591 + this->pagemask = (this->chipsize >> this->page_shift) - 1;
4592 + /* Preset the internal oob buffer */
4593 + memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
4595 + /* If no default placement scheme is given, select an
4596 + * appropriate one */
4597 + if (!this->autooob) {
4598 + /* Select the appropriate default oob placement scheme for
4599 + * placement agnostic filesystems */
4600 + switch (mtd->oobsize) {
4602 + this->autooob = &nand_oob_8;
4605 + this->autooob = &nand_oob_16;
4608 + this->autooob = &nand_oob_64;
4611 + printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
4617 + /* The number of bytes available for the filesystem to place fs dependend
4619 + if (this->options & NAND_BUSWIDTH_16) {
4620 + mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
4621 + if (this->autooob->eccbytes & 0x01)
4624 + mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
4628 + * check ECC mode, default to software
4629 + * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
4630 + * fallback to software ECC
4632 + this->eccsize = 256; /* set default eccsize */
4633 + this->eccbytes = 3;
4635 + switch (this->eccmode) {
4636 + case NAND_ECC_HW12_2048:
4637 + if (mtd->oobblock < 2048) {
4638 + printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4640 + this->eccmode = NAND_ECC_SOFT;
4641 + this->calculate_ecc = nand_calculate_ecc;
4642 + this->correct_data = nand_correct_data;
4644 + this->eccsize = 2048;
4647 + case NAND_ECC_HW3_512:
4648 + case NAND_ECC_HW6_512:
4649 + case NAND_ECC_HW8_512:
4650 + if (mtd->oobblock == 256) {
4651 + printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
4652 + this->eccmode = NAND_ECC_SOFT;
4653 + this->calculate_ecc = nand_calculate_ecc;
4654 + this->correct_data = nand_correct_data;
4656 + this->eccsize = 512; /* set eccsize to 512 */
4659 + case NAND_ECC_HW3_256:
4662 + case NAND_ECC_NONE:
4663 + printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
4664 + this->eccmode = NAND_ECC_NONE;
4667 + case NAND_ECC_SOFT:
4668 + this->calculate_ecc = nand_calculate_ecc;
4669 + this->correct_data = nand_correct_data;
4673 + printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
4677 + /* Check hardware ecc function availability and adjust number of ecc bytes per
4678 + * calculation step
4680 + switch (this->eccmode) {
4681 + case NAND_ECC_HW12_2048:
4682 + this->eccbytes += 4;
4683 + case NAND_ECC_HW8_512:
4684 + this->eccbytes += 2;
4685 + case NAND_ECC_HW6_512:
4686 + this->eccbytes += 3;
4687 +// case NAND_ECC_HW3_512:
4688 + case NAND_ECC_HW3_256:
4689 + if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
4691 + printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
4695 + mtd->eccsize = this->eccsize;
4697 + /* Set the number of read / write steps for one page to ensure ECC generation */
4698 + switch (this->eccmode) {
4699 + case NAND_ECC_HW12_2048:
4700 + this->eccsteps = mtd->oobblock / 2048;
4702 + case NAND_ECC_HW3_512:
4703 + case NAND_ECC_HW6_512:
4704 + case NAND_ECC_HW8_512:
4705 + this->eccsteps = mtd->oobblock / 512;
4707 + case NAND_ECC_HW3_256:
4708 + case NAND_ECC_SOFT:
4709 + this->eccsteps = mtd->oobblock / 256;
4712 + case NAND_ECC_NONE:
4713 + this->eccsteps = 1;
4717 + /* Initialize state, waitqueue and spinlock */
4718 + this->state = FL_READY;
4719 + init_waitqueue_head (&this->wq);
4720 + spin_lock_init (&this->chip_lock);
4722 + /* De-select the device */
4723 + this->select_chip(mtd, 0);
4725 + /* Print warning message for no device */
4727 + printk (KERN_WARNING "No NAND device found!!!\n");
4731 + /* Fill in remaining MTD driver data */
4732 + mtd->type = MTD_NANDFLASH;
4733 + mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
4734 + mtd->ecctype = MTD_ECC_SW;
4735 + mtd->erase = sl2312_nand_erase;
4736 + mtd->point = NULL;
4737 + mtd->unpoint = NULL;
4738 + mtd->read = sl2312_nand_read;
4739 + mtd->write = sl2312_nand_write;
4740 + mtd->read_ecc = sl2312_nand_read_ecc;
4741 + mtd->write_ecc = sl2312_nand_write_ecc;
4742 + mtd->read_oob = sl2312_nand_read_oob;
4743 + mtd->write_oob = sl2312_nand_write_oob;
4744 + mtd->readv = NULL;
4745 + mtd->writev = sl2312_nand_writev;
4746 + mtd->writev_ecc = sl2312_nand_writev_ecc;
4747 + mtd->sync = sl2312_nand_sync;
4749 + mtd->unlock = NULL;
4750 + mtd->suspend = NULL;
4751 + mtd->resume = NULL;
4752 + mtd->block_isbad = sl2312_nand_block_isbad;
4753 + mtd->block_markbad = sl2312_nand_block_markbad;
4755 + /* and make the autooob the default one */
4756 + memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
4758 + mtd->owner = THIS_MODULE;
4760 + /* Build bad block table */
4761 + return this->scan_bbt (mtd);
4764 +/*End Add function*/
4767 + * Main initialization routine
4769 +extern int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc);
4771 +int __init sl2312_mtd_init (void)
4773 + struct nand_chip *this;
4775 + struct mtd_partition *parts;
4777 + int ret, data, *base;
4779 + printk("NAND MTD Driver Start Init ......\n");
4781 + base = (unsigned int *)(IO_ADDRESS(SL2312_GLOBAL_BASE) + 0x30);
4784 + data|=0x3; //disable p & s flash
4787 + /* Allocate memory for MTD device structure and private data */
4788 + sl2312_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
4789 + if (!sl2312_mtd) {
4790 + printk ("Unable to allocate SL2312 NAND MTD device structure.\n");
4795 + // sl2312_device_setup();
4797 + /* io is indirect via a register so don't need to ioremap address */
4799 + /* Get pointer to private data */
4800 + this = (struct nand_chip *) (&sl2312_mtd[1]);
4802 + /* Initialize structures */
4803 + memset((char *) sl2312_mtd, 0, sizeof(struct mtd_info));
4804 + memset((char *) this, 0, sizeof(struct nand_chip));
4806 + /* Link the private data with the MTD structure */
4807 + sl2312_mtd->priv = this;
4808 + sl2312_mtd->name = "sl2312-nand";
4810 + /* Set address of NAND IO lines */
4811 + this->IO_ADDR_R = (void __iomem *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE+NFLASH_DATA)); //(unsigned long)&(sl2312_ndfmcptr->dtr);
4812 + this->IO_ADDR_W = (void __iomem *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE+NFLASH_DATA)); //(unsigned long)&(sl2312_ndfmcptr->dtr);
4813 + this->read_byte = sl2312_nand_read_byte;
4814 + this->write_byte = sl2312_nand_write_byte;
4815 + this->write_buf = sl2312_nand_write_buf;
4816 + this->read_buf = sl2312_nand_read_buf;
4817 + this->verify_buf = sl2312_nand_verify_buf;
4818 + this->select_chip = sl2312_nand_select_chip;
4819 + this->block_bad = sl2312_nand_block_bad;
4820 + this->hwcontrol = sl2312_hwcontrol;
4821 + this->dev_ready = sl2312_device_ready;
4822 + this->cmdfunc = sl2312_nand_command;
4823 + this->waitfunc = sl2312_nand_waitfunc;
4824 + //this->calculate_ecc = sl2312_readecc;
4825 + this->enable_hwecc = sl2312_enable_hwecc;
4826 + this->eccmode = NAND_ECC_HW3_512;
4827 + /*this->eccsize = 512; */
4828 + /* 20 us command delay time */
4829 + this->chip_delay = 20;
4831 + this->correct_data = nand_correct_data;
4832 +// this->scan_bbt = sl2312_nand_scan_bbt;
4834 + /* Allocate memory for internal data buffer */
4835 + this->data_buf = kmalloc (sizeof(u_char) * (sl2312_mtd->oobblock + sl2312_mtd->oobsize), GFP_KERNEL);
4836 + if (!this->data_buf) {
4837 + printk ("Unable to allocate NAND data buffer.\n");
4842 + /* Scan to find existance of the device */
4843 + if (sl2312_nand_scan(sl2312_mtd, 1)) {
4848 + /* Register the partitions */
4849 + parts = sl2312_partitions;
4850 + nr_parts = sizeof(sl2312_partitions)/sizeof(*parts);
4852 + ret = add_mtd_partitions(sl2312_mtd, sl2312_partitions, nr_parts);
4853 + /*If we got an error, free all resources.*/
4855 + del_mtd_partitions(sl2312_mtd);
4856 + map_destroy(sl2312_mtd);
4861 +// kfree (this->data_buf);
4864 + printk("NAND MTD Driver Init Success ......\n");
4868 +module_init(sl2312_mtd_init);
4871 + * Clean up routine
4874 +static void __exit sl2312_cleanup (void)
4876 + struct nand_chip *this = (struct nand_chip *) &sl2312_mtd[1];
4878 + /* Unregister partitions */
4879 + del_mtd_partitions(sl2312_mtd);
4881 + /* Unregister the device */
4882 + del_mtd_device (sl2312_mtd);
4884 + /* Free internal data buffers */
4885 + kfree (this->data_buf);
4887 + /* Free the MTD device structure */
4888 + kfree (sl2312_mtd);
4890 +module_exit(sl2312_cleanup);
4893 +MODULE_LICENSE("GPL");
4894 +MODULE_AUTHOR("Alice Hennessy <ahennessy@mvista.com>");
4895 +MODULE_DESCRIPTION("Glue layer for SmartMediaCard on Toshiba RBsl2312");
4896 Index: linux-2.6.23.16/drivers/mtd/nand/sl2312-flash-nand.h
4897 ===================================================================
4898 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4899 +++ linux-2.6.23.16/drivers/mtd/nand/sl2312-flash-nand.h 2008-03-15 17:04:25.878696749 +0200
4901 +#ifndef SL2312_FLASH_NAND_H
4902 +#define SL2312_FLASH_NAND_H
4904 +#include <linux/wait.h>
4905 +#include <linux/spinlock.h>
4908 +static void nand_read_id(int chip_no,unsigned char *id);
4912 +#define NFLASH_WiDTH8 0x00000000
4913 +#define NFLASH_WiDTH16 0x00000400
4914 +#define NFLASH_WiDTH32 0x00000800
4915 +#define NFLASH_CHIP0_EN 0x00000000 // 16th bit = 0
4916 +#define NFLASH_CHIP1_EN 0x00010000 // 16th bit = 1
4917 +#define NFLASH_DIRECT 0x00004000
4918 +#define NFLASH_INDIRECT 0x00000000
4921 +#define DWIDTH NFLASH_WiDTH8
4924 +#endif /* SL2312_FLASH_NAND_H */
4925 Index: linux-2.6.23.16/include/linux/mtd/kvctl.h
4926 ===================================================================
4927 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4928 +++ linux-2.6.23.16/include/linux/mtd/kvctl.h 2008-03-15 17:03:17.874821522 +0200
4933 +#define VCTL_HEAD_SIZE 8
4934 +#define VCTL_ENTRY_LEN 20
4939 + unsigned int entry_num;
4945 + unsigned int size;
4946 + unsigned int type;
4949 + unsigned char *payload;
4954 + unsigned char mac[6];
4955 + unsigned char vlanid;
4956 + unsigned char vlanmap;
4959 +#define VCT_VENDORSPEC 0
4960 +#define VCT_BOOTLOADER 1
4961 +#define VCT_KERNEL 2
4962 +#define VCT_VERCTL 3
4963 +#define VCT_CURRCONF 4
4964 +#define VCT_DEFAULTCONF 5
4965 +#define VCT_ROOTFS 6
4970 Index: linux-2.6.23.16/drivers/mtd/maps/Makefile
4971 ===================================================================
4972 --- linux-2.6.23.16.orig/drivers/mtd/maps/Makefile 2008-03-15 17:03:14.374622039 +0200
4973 +++ linux-2.6.23.16/drivers/mtd/maps/Makefile 2008-03-15 17:03:17.874821522 +0200
4975 obj-$(CONFIG_MTD_OMAP_NOR) += omap_nor.o
4976 obj-$(CONFIG_MTD_MTX1) += mtx-1_flash.o
4977 obj-$(CONFIG_MTD_TQM834x) += tqm834x.o
4978 +###### for Storlink Soc #######
4979 +obj-$(CONFIG_MTD_SL2312_CFI) += sl2312-flash-cfi.o
4980 +obj-$(CONFIG_MTD_SL2312_SERIAL_ATMEL) += sl2312-flash-atmel.o
4981 +obj-$(CONFIG_MTD_SL2312_SERIAL_ST) += sl2312-flash-m25p80.o