2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 * This version hacked for emulating 2kpage NAND for YAFFS2 testing.
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/version.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
25 #include <linux/proc_fs.h>
26 #include <linux/pagemap.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/interrupt.h>
29 #include <linux/string.h>
30 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
31 #include <linux/locks.h>
34 #include <asm/uaccess.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/partitions.h>
37 #include <linux/mtd/nand.h>
39 #define T(f,x) printk x
40 #define ALLOCATE(x) kmalloc(x,GFP_KERNEL)
41 #define FREE(x) kfree(x)
47 #define EM_SIZE_IN_MEG 4
48 #define PAGE_DATA_SIZE (2048)
49 #define PAGE_SPARE_SIZE (64)
50 #define PAGES_PER_BLOCK (64)
51 #define NAND_SHIFT (11) // Shifter for 2k
54 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
56 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
58 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
60 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
63 static struct mtd_info nandemul2k_mtd;
67 __u8 data[PAGE_TOTAL_SIZE]; // Data + spare
68 int empty; // is this empty?
74 nandemul_Page *page[PAGES_PER_BLOCK];
82 nandemul_Block**block;
86 static nandemul_Device ned;
88 static int sizeInMB = EM_SIZE_IN_MEG;
91 static void nandemul_yield(int n)
94 if(n > 0) schedule_timeout(n);
100 static void nandemul2k_Read(void *buffer, int page, int start, int nBytes)
102 int pg = page%PAGES_PER_BLOCK;
103 int blk = page/PAGES_PER_BLOCK;
104 if(buffer && nBytes > 0)
106 memcpy(buffer,&ned.block[blk]->page[pg]->data[start],nBytes);
111 static void nandemul2k_Program(const void *buffer, int page, int start, int nBytes)
113 int pg = page%PAGES_PER_BLOCK;
114 int blk = page/PAGES_PER_BLOCK;
116 __u8 *b = (__u8 *)buffer;
118 p = &ned.block[blk]->page[pg]->data[start];
120 while(buffer && nBytes>0)
129 static void nandemul2k_DoErase(int blockNumber)
135 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
140 blk = ned.block[blockNumber];
142 for(i = 0; i < PAGES_PER_BLOCK; i++)
144 memset(blk->page[i],0xff,sizeof(nandemul_Page));
145 blk->page[i]->empty = 1;
151 static int nandemul2k_CalcNBlocks(void)
153 return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
158 static int CheckInit(void)
160 static int initialised = 0;
175 ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
178 ned.block = ALLOCATE(sizeof(nandemul_Block*) * nBlocks );
180 if(!ned.block) return ENOMEM;
186 for(i=fail=0; i <nBlocks; i++)
191 if(!(blk = ned.block[i] = ALLOCATE(sizeof(nandemul_Block))))
197 for(j = 0; j < PAGES_PER_BLOCK; j++)
199 if((blk->page[j] = ALLOCATE(sizeof(nandemul_Page))) == 0)
204 nandemul2k_DoErase(i);
205 ned.block[i]->damaged = 0;
214 for(i = 0; i < nAllocated; i++)
223 ned.nBlocks = nBlocks;
232 static void nandemul2k_CleanUp(void)
236 for(i = 0; i < ned.nBlocks; i++)
238 for(j = 0; j < PAGES_PER_BLOCK; j++)
240 FREE(ned.block[i]->page[j]);
249 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
251 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
252 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
256 int nandemul2k_ReadId(__u8 *vendorId, __u8 *deviceId)
265 int nandemul2k_ReadStatus(__u8 *status)
272 #ifdef CONFIG_MTD_NAND_ECC
273 #include <linux/mtd/nand_ecc.h>
277 * NAND low-level MTD interface functions
279 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
280 size_t *retlen, u_char *buf);
281 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
282 size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *dummy);
283 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
284 size_t *retlen, u_char *buf);
285 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
286 size_t *retlen, const u_char *buf);
287 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
288 size_t *retlen, const u_char *buf,
289 const u_char *oob_buf, struct nand_oobinfo *dummy);
290 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
291 size_t *retlen, const u_char *buf);
292 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
293 unsigned long count, loff_t to, size_t *retlen);
294 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
295 static void nand_sync (struct mtd_info *mtd);
302 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
303 size_t *retlen, u_char *buf)
305 return nand_read_ecc (mtd, from, len, retlen, buf, NULL,NULL);
312 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
313 size_t *retlen, u_char *buf, u_char *oob_buf,struct nand_oobinfo *oobsel)
321 /* Do not allow reads past end of device */
322 if ((from + len) > mtd->size) {
328 /* Initialize return value */
334 /* First we calculate the starting page */
335 page = from >> NAND_SHIFT;
337 /* Get raw starting column */
339 start = from & (mtd->oobblock-1);
341 // OK now check for the curveball where the start and end are in
343 if((start + n) < mtd->oobblock)
349 nToCopy = mtd->oobblock - start;
352 nandemul2k_Read(buf, page, start, nToCopy);
353 nandemul2k_Read(oob_buf,page,PAGE_DATA_SIZE,PAGE_SPARE_SIZE);
358 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
368 * NAND read out-of-band
370 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
371 size_t *retlen, u_char *buf)
375 T(0,("nand_read_oob: from = 0x%08x, buf = 0x%08x, len = %i\n", (unsigned int) from, (unsigned int) buf,
378 /* Shift to get page */
379 page = ((int) from) >> NAND_SHIFT;
381 /* Mask to get column */
384 /* Initialize return length value */
387 /* Do not allow reads past end of device */
388 if ((from + len) > mtd->size) {
390 ("nand_read_oob: Attempt read beyond end of device\n"));
395 nandemul2k_Read(buf,page,PAGE_DATA_SIZE + col,len);
405 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
406 size_t *retlen, const u_char *buf)
408 return nand_write_ecc (mtd, to, len, retlen, buf, NULL,NULL);
412 * NAND write with ECC
414 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
415 size_t *retlen, const u_char *buf,
416 const u_char *oob_buf, struct nand_oobinfo *dummy)
425 /* Do not allow reads past end of device */
426 if ((to + len) > mtd->size) {
432 /* Initialize return value */
438 /* First we calculate the starting page */
439 page = to >> NAND_SHIFT;
441 /* Get raw starting column */
443 start = to & (mtd->oobblock - 1);
445 // OK now check for the curveball where the start and end are in
447 if((start + n) < mtd->oobblock)
453 nToCopy = mtd->oobblock - start;
456 nandemul2k_Program(buf, page, start, nToCopy);
457 nandemul2k_Program(oob_buf, page, PAGE_DATA_SIZE, PAGE_SPARE_SIZE);
462 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
472 * NAND write out-of-band
474 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
475 size_t *retlen, const u_char *buf)
481 "nand_read_oob: to = 0x%08x, len = %i\n", (unsigned int) to,
484 /* Shift to get page */
485 page = ((int) to) >> NAND_SHIFT;
487 /* Mask to get column */
490 /* Initialize return length value */
493 /* Do not allow reads past end of device */
494 if ((to + len) > mtd->size) {
496 "nand_read_oob: Attempt read beyond end of device\n"));
501 nandemul2k_Program(buf,page,512 + col,len);
510 * NAND write with iovec
512 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
513 unsigned long count, loff_t to, size_t *retlen)
521 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
523 int i, nBlocks,block;
526 "nand_erase: start = 0x%08x, len = %i\n",
527 (unsigned int) instr->addr, (unsigned int) instr->len));
529 /* Start address must align on block boundary */
530 if (instr->addr & (mtd->erasesize - 1)) {
532 "nand_erase: Unaligned address\n"));
536 /* Length must align on block boundary */
537 if (instr->len & (mtd->erasesize - 1)) {
539 "nand_erase: Length not block aligned\n"));
543 /* Do not allow erase past end of device */
544 if ((instr->len + instr->addr) > mtd->size) {
546 "nand_erase: Erase past end of device\n"));
550 nBlocks = instr->len >> (NAND_SHIFT + 5);
551 block = instr->addr >> (NAND_SHIFT + 5);
553 for(i = 0; i < nBlocks; i++)
555 nandemul2k_DoErase(block);
567 int nand_block_isbad(struct mtd_info *mtd,int blockNo)
572 int nand_block_markbad(struct mtd_info *mtd, int blockNo)
581 static void nand_sync (struct mtd_info *mtd)
583 T(0,("nand_sync: called\n"));
588 * Scan for the NAND device
590 int nand_scan (struct mtd_info *mtd,int nchips)
592 mtd->oobblock = PAGE_DATA_SIZE;
593 mtd->oobsize = PAGE_SPARE_SIZE;
594 mtd->erasesize = PAGE_DATA_SIZE * PAGES_PER_BLOCK;
595 mtd->size = sizeInMB * 1024*1024;
599 /* Fill in remaining MTD driver data */
600 mtd->type = MTD_NANDFLASH;
601 mtd->flags = MTD_CAP_NANDFLASH;
602 mtd->owner = THIS_MODULE;
603 mtd->ecctype = MTD_ECC_NONE;
604 mtd->erase = nand_erase;
607 mtd->read = nand_read;
608 mtd->write = nand_write;
609 mtd->read_ecc = nand_read_ecc;
610 mtd->write_ecc = nand_write_ecc;
611 mtd->read_oob = nand_read_oob;
612 mtd->write_oob = nand_write_oob;
613 mtd->block_isbad = nand_block_isbad;
614 mtd->block_markbad = nand_block_markbad;
616 mtd->writev = nand_writev;
617 mtd->sync = nand_sync;
629 MODULE_PARM(sizeInMB, "i");
631 __setup("sizeInMB=",sizeInMB);
636 * Define partitions for flash devices
639 static struct mtd_partition nandemul2k_partition[] =
641 { name: "NANDemul partition 1",
646 static int nPartitions = sizeof(nandemul2k_partition)/sizeof(nandemul2k_partition[0]);
649 * Main initialization routine
651 int __init nandemul2k_init (void)
658 nand_scan(&nandemul2k_mtd,1);
660 // Build the partition table
662 nandemul2k_partition[0].size = sizeInMB * 1024 * 1024;
664 // Register the partition
665 add_mtd_partitions(&nandemul2k_mtd,nandemul2k_partition,nPartitions);
671 module_init(nandemul2k_init);
677 static void __exit nandemul2k_cleanup (void)
680 nandemul2k_CleanUp();
682 /* Unregister partitions */
683 del_mtd_partitions(&nandemul2k_mtd);
685 /* Unregister the device */
686 del_mtd_device (&nandemul2k_mtd);
689 module_exit(nandemul2k_cleanup);
692 MODULE_LICENSE("GPL");
693 MODULE_AUTHOR("Charles Manning <manningc@aleph1.co.uk>");
694 MODULE_DESCRIPTION("2k Page/128k Block NAND emulated in RAM");