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 #include <linux/locks.h>
32 #include <asm/uaccess.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/mtd/partitions.h>
35 #include <linux/mtd/nand.h>
37 #define T(f,x) printk x
38 #define ALLOCATE(x) kmalloc(x,GFP_KERNEL)
39 #define FREE(x) kfree(x)
45 #define EM_SIZE_IN_MEG 4
46 #define PAGE_DATA_SIZE (2048)
47 #define PAGE_SPARE_SIZE (64)
48 #define PAGES_PER_BLOCK (64)
49 #define NAND_SHIFT (11) // Shifter for 2k
52 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
54 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
56 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
58 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
61 static struct mtd_info nandemul2k_mtd;
65 __u8 data[PAGE_TOTAL_SIZE]; // Data + spare
66 int empty; // is this empty?
72 nandemul_Page *page[PAGES_PER_BLOCK];
80 nandemul_Block**block;
84 static nandemul_Device ned;
86 static int sizeInMB = EM_SIZE_IN_MEG;
89 static void nandemul_yield(int n)
92 if(n > 0) schedule_timeout(n);
98 static void nandemul2k_Read(void *buffer, int page, int start, int nBytes)
100 int pg = page%PAGES_PER_BLOCK;
101 int blk = page/PAGES_PER_BLOCK;
102 if(buffer && nBytes > 0)
104 memcpy(buffer,&ned.block[blk]->page[pg]->data[start],nBytes);
109 static void nandemul2k_Program(const void *buffer, int page, int start, int nBytes)
111 int pg = page%PAGES_PER_BLOCK;
112 int blk = page/PAGES_PER_BLOCK;
114 __u8 *b = (__u8 *)buffer;
116 p = &ned.block[blk]->page[pg]->data[start];
118 while(buffer && nBytes>0)
127 static void nandemul2k_DoErase(int blockNumber)
133 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
138 blk = ned.block[blockNumber];
140 for(i = 0; i < PAGES_PER_BLOCK; i++)
142 memset(blk->page[i],0xff,sizeof(nandemul_Page));
143 blk->page[i]->empty = 1;
149 static int nandemul2k_CalcNBlocks(void)
151 return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
156 static int CheckInit(void)
158 static int initialised = 0;
173 ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
176 ned.block = ALLOCATE(sizeof(nandemul_Block*) * nBlocks );
178 if(!ned.block) return ENOMEM;
184 for(i=fail=0; i <nBlocks; i++)
189 if(!(blk = ned.block[i] = ALLOCATE(sizeof(nandemul_Block))))
195 for(j = 0; j < PAGES_PER_BLOCK; j++)
197 if((blk->page[j] = ALLOCATE(sizeof(nandemul_Page))) == 0)
202 nandemul2k_DoErase(i);
203 ned.block[i]->damaged = 0;
212 for(i = 0; i < nAllocated; i++)
221 ned.nBlocks = nBlocks;
230 static void nandemul2k_CleanUp(void)
234 for(i = 0; i < ned.nBlocks; i++)
236 for(j = 0; j < PAGES_PER_BLOCK; j++)
238 FREE(ned.block[i]->page[j]);
247 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
249 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
250 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
254 int nandemul2k_ReadId(__u8 *vendorId, __u8 *deviceId)
263 int nandemul2k_ReadStatus(__u8 *status)
270 #ifdef CONFIG_MTD_NAND_ECC
271 #include <linux/mtd/nand_ecc.h>
275 * NAND low-level MTD interface functions
277 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
278 size_t *retlen, u_char *buf);
279 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
280 size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *dummy);
281 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
282 size_t *retlen, u_char *buf);
283 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
284 size_t *retlen, const u_char *buf);
285 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
286 size_t *retlen, const u_char *buf,
287 const u_char *oob_buf, struct nand_oobinfo *dummy);
288 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
289 size_t *retlen, const u_char *buf);
290 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
291 unsigned long count, loff_t to, size_t *retlen);
292 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
293 static void nand_sync (struct mtd_info *mtd);
300 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
301 size_t *retlen, u_char *buf)
303 return nand_read_ecc (mtd, from, len, retlen, buf, NULL,NULL);
310 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
311 size_t *retlen, u_char *buf, u_char *oob_buf,struct nand_oobinfo *oobsel)
319 /* Do not allow reads past end of device */
320 if ((from + len) > mtd->size) {
326 /* Initialize return value */
332 /* First we calculate the starting page */
333 page = from >> NAND_SHIFT;
335 /* Get raw starting column */
337 start = from & (mtd->oobblock-1);
339 // OK now check for the curveball where the start and end are in
341 if((start + n) < mtd->oobblock)
347 nToCopy = mtd->oobblock - start;
350 nandemul2k_Read(buf, page, start, nToCopy);
351 nandemul2k_Read(oob_buf,page,PAGE_DATA_SIZE,PAGE_SPARE_SIZE);
356 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
366 * NAND read out-of-band
368 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
369 size_t *retlen, u_char *buf)
373 T(0,("nand_read_oob: from = 0x%08x, buf = 0x%08x, len = %i\n", (unsigned int) from, (unsigned int) buf,
376 /* Shift to get page */
377 page = ((int) from) >> NAND_SHIFT;
379 /* Mask to get column */
382 /* Initialize return length value */
385 /* Do not allow reads past end of device */
386 if ((from + len) > mtd->size) {
388 ("nand_read_oob: Attempt read beyond end of device\n"));
393 nandemul2k_Read(buf,page,PAGE_DATA_SIZE + col,len);
403 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
404 size_t *retlen, const u_char *buf)
406 return nand_write_ecc (mtd, to, len, retlen, buf, NULL,NULL);
410 * NAND write with ECC
412 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
413 size_t *retlen, const u_char *buf,
414 const u_char *oob_buf, struct nand_oobinfo *dummy)
423 /* Do not allow reads past end of device */
424 if ((to + len) > mtd->size) {
430 /* Initialize return value */
436 /* First we calculate the starting page */
437 page = to >> NAND_SHIFT;
439 /* Get raw starting column */
441 start = to & (mtd->oobblock - 1);
443 // OK now check for the curveball where the start and end are in
445 if((start + n) < mtd->oobblock)
451 nToCopy = mtd->oobblock - start;
454 nandemul2k_Program(buf, page, start, nToCopy);
455 nandemul2k_Program(oob_buf, page, PAGE_DATA_SIZE, PAGE_SPARE_SIZE);
460 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
470 * NAND write out-of-band
472 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
473 size_t *retlen, const u_char *buf)
479 "nand_read_oob: to = 0x%08x, len = %i\n", (unsigned int) to,
482 /* Shift to get page */
483 page = ((int) to) >> NAND_SHIFT;
485 /* Mask to get column */
488 /* Initialize return length value */
491 /* Do not allow reads past end of device */
492 if ((to + len) > mtd->size) {
494 "nand_read_oob: Attempt read beyond end of device\n"));
499 nandemul2k_Program(buf,page,512 + col,len);
508 * NAND write with iovec
510 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
511 unsigned long count, loff_t to, size_t *retlen)
519 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
521 int i, nBlocks,block;
524 "nand_erase: start = 0x%08x, len = %i\n",
525 (unsigned int) instr->addr, (unsigned int) instr->len));
527 /* Start address must align on block boundary */
528 if (instr->addr & (mtd->erasesize - 1)) {
530 "nand_erase: Unaligned address\n"));
534 /* Length must align on block boundary */
535 if (instr->len & (mtd->erasesize - 1)) {
537 "nand_erase: Length not block aligned\n"));
541 /* Do not allow erase past end of device */
542 if ((instr->len + instr->addr) > mtd->size) {
544 "nand_erase: Erase past end of device\n"));
548 nBlocks = instr->len >> (NAND_SHIFT + 5);
549 block = instr->addr >> (NAND_SHIFT + 5);
551 for(i = 0; i < nBlocks; i++)
553 nandemul2k_DoErase(block);
565 int nand_block_isbad(struct mtd_info *mtd,int blockNo)
570 int nand_block_markbad(struct mtd_info *mtd, int blockNo)
579 static void nand_sync (struct mtd_info *mtd)
581 T(0,("nand_sync: called\n"));
586 * Scan for the NAND device
588 int nand_scan (struct mtd_info *mtd,int nchips)
590 mtd->oobblock = PAGE_DATA_SIZE;
591 mtd->oobsize = PAGE_SPARE_SIZE;
592 mtd->erasesize = PAGE_DATA_SIZE * PAGES_PER_BLOCK;
593 mtd->size = sizeInMB * 1024*1024;
597 /* Fill in remaining MTD driver data */
598 mtd->type = MTD_NANDFLASH;
599 mtd->flags = MTD_CAP_NANDFLASH;
600 mtd->owner = THIS_MODULE;
601 mtd->ecctype = MTD_ECC_NONE;
602 mtd->erase = nand_erase;
605 mtd->read = nand_read;
606 mtd->write = nand_write;
607 mtd->read_ecc = nand_read_ecc;
608 mtd->write_ecc = nand_write_ecc;
609 mtd->read_oob = nand_read_oob;
610 mtd->write_oob = nand_write_oob;
611 mtd->block_isbad = nand_block_isbad;
612 mtd->block_markbad = nand_block_markbad;
614 mtd->writev = nand_writev;
615 mtd->sync = nand_sync;
627 MODULE_PARM(sizeInMB, "i");
629 __setup("sizeInMB=",sizeInMB);
634 * Define partitions for flash devices
637 static struct mtd_partition nandemul2k_partition[] =
639 { name: "NANDemul partition 1",
644 static int nPartitions = sizeof(nandemul2k_partition)/sizeof(nandemul2k_partition[0]);
647 * Main initialization routine
649 int __init nandemul2k_init (void)
656 nand_scan(&nandemul2k_mtd,1);
658 // Build the partition table
660 nandemul2k_partition[0].size = sizeInMB * 1024 * 1024;
662 // Register the partition
663 add_mtd_partitions(&nandemul2k_mtd,nandemul2k_partition,nPartitions);
669 module_init(nandemul2k_init);
675 static void __exit nandemul2k_cleanup (void)
678 nandemul2k_CleanUp();
680 /* Unregister partitions */
681 del_mtd_partitions(&nandemul2k_mtd);
683 /* Unregister the device */
684 del_mtd_device (&nandemul2k_mtd);
687 module_exit(nandemul2k_cleanup);
690 MODULE_LICENSE("GPL");
691 MODULE_AUTHOR("Charles Manning <manningc@aleph1.co.uk>");
692 MODULE_DESCRIPTION("2k Page/128k Block NAND emulated in RAM");