2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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.
15 * This version hacked for emulating 2kpage NAND for YAFFS2 testing.
18 //#include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/version.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/list.h>
26 #include <linux/proc_fs.h>
27 #include <linux/pagemap.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/interrupt.h>
30 #include <linux/string.h>
31 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
32 #include <linux/locks.h>
35 #include <asm/uaccess.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/partitions.h>
38 #include <linux/mtd/nand.h>
39 #include "../yaffs_nandemul2k.h"
41 #define ALLOCATE(x) kmalloc(x,GFP_KERNEL)
42 #define FREE(x) kfree(x)
48 #define NAND_SHIFT (11) // Shifter for 2k
49 #define PAGE_DATA_SIZE (1 << NAND_SHIFT)
50 #define PAGE_SPARE_SIZE (64)
52 #define PAGES_PER_BLOCK (1 << BLK_SHIFT) // = 64
55 #define EM_SIZE_IN_MEG 4
56 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
58 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
60 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
62 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
65 static struct mtd_info nandemul2k_mtd;
69 u8 data[PAGE_TOTAL_SIZE]; // Data + spare
70 int empty; // is this empty?
76 nandemul_Page *page[PAGES_PER_BLOCK];
84 nandemul_Block**block;
88 static nandemul_Device ned;
90 static int sizeInMB = EM_SIZE_IN_MEG;
93 static void nandemul_yield(int n)
96 if(n > 0) schedule_timeout(n);
102 static void nandemul2k_Read(void *buffer, int page, int start, int n_bytes)
104 int pg = page%PAGES_PER_BLOCK;
105 int blk = page/PAGES_PER_BLOCK;
106 if(buffer && n_bytes > 0)
108 memcpy(buffer,&ned.block[blk]->page[pg]->data[start],n_bytes);
113 static void nandemul2k_Program(const void *buffer, int page, int start, int n_bytes)
115 int pg = page%PAGES_PER_BLOCK;
116 int blk = page/PAGES_PER_BLOCK;
118 u8 *b = (u8 *)buffer;
120 p = &ned.block[blk]->page[pg]->data[start];
122 while(buffer && n_bytes>0)
131 static void nandemul2k_DoErase(int blockNumber)
137 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
142 blk = ned.block[blockNumber];
144 for(i = 0; i < PAGES_PER_BLOCK; i++)
146 memset(blk->page[i],0xff,sizeof(nandemul_Page));
147 blk->page[i]->empty = 1;
153 static int nandemul2k_CalcNBlocks(void)
155 return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
160 static int CheckInit(void)
162 static int initialised = 0;
177 ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
180 ned.block = ALLOCATE(sizeof(nandemul_Block*) * nBlocks );
182 if(!ned.block) return ENOMEM;
188 for(i=fail=0; i <nBlocks; i++)
193 if(!(blk = ned.block[i] = ALLOCATE(sizeof(nandemul_Block))))
199 for(j = 0; j < PAGES_PER_BLOCK; j++)
201 if((blk->page[j] = ALLOCATE(sizeof(nandemul_Page))) == 0)
206 nandemul2k_DoErase(i);
207 ned.block[i]->damaged = 0;
216 for(i = 0; i < nAllocated; i++)
225 ned.nBlocks = nBlocks;
234 static void nandemul2k_CleanUp(void)
238 for(i = 0; i < ned.nBlocks; i++)
240 for(j = 0; j < PAGES_PER_BLOCK; j++)
242 FREE(ned.block[i]->page[j]);
251 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
253 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
254 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
258 static int nandemul2k_ReadId(u8 *vendorId, u8 *deviceId)
267 static int nandemul2k_ReadStatus(u8 *status)
274 #ifdef CONFIG_MTD_NAND_ECC
275 #include <linux/mtd/nand_ecc.h>
279 * NAND low-level MTD interface functions
281 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
282 size_t *retlen, u_char *buf);
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_oob (struct mtd_info *mtd, loff_t to, size_t len,
288 size_t *retlen, const u_char *buf);
289 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,7))
290 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
291 unsigned long count, loff_t to, size_t *retlen);
293 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
294 unsigned long count, loff_t to, size_t *retlen);
296 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
297 static void nand_sync (struct mtd_info *mtd);
304 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
305 size_t *retlen, u_char *buf)
307 return nand_read_ecc (mtd, from, len, retlen, buf, NULL,NULL);
314 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
315 size_t *retlen, u_char *buf, u_char *oob_buf,struct nand_oobinfo *oobsel)
323 /* Do not allow reads past end of device */
324 if ((from + len) > mtd->size) {
330 /* Initialize return value */
336 /* First we calculate the starting page */
337 page = from >> NAND_SHIFT;
339 /* Get raw starting column */
341 start = from & (PAGE_DATA_SIZE - 1);
343 // OK now check for the curveball where the start and end are in
345 if((start + n) < PAGE_DATA_SIZE)
351 nToCopy = PAGE_DATA_SIZE - start;
354 nandemul2k_Read(buf, page, start, nToCopy);
355 nandemul2k_Read(oob_buf,page,PAGE_DATA_SIZE,PAGE_SPARE_SIZE);
360 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
370 * NAND read out-of-band
372 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
373 size_t *retlen, u_char *buf)
377 T(0,("nand_read_oob: from = 0x%08x, buf = 0x%08x, len = %i\n", (unsigned int) from, (unsigned int) buf,
380 /* Shift to get page */
381 page = ((int) from) >> NAND_SHIFT;
383 /* Mask to get column */
384 col = from & (PAGE_SPARE_SIZE-1)
386 /* Initialize return length value */
389 /* Do not allow reads past end of device */
390 if ((from + len) > mtd->size) {
392 ("nand_read_oob: Attempt read beyond end of device\n"));
397 nandemul2k_Read(buf,page,PAGE_DATA_SIZE + col,len);
407 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
408 size_t *retlen, const u_char *buf)
410 return nand_write_ecc (mtd, to, len, retlen, buf, NULL,NULL);
414 * NAND write with ECC
416 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
417 size_t *retlen, const u_char *buf,
418 u_char *oob_buf, struct nand_oobinfo *dummy)
427 /* Do not allow reads past end of device */
428 if ((to + len) > mtd->size) {
434 /* Initialize return value */
440 /* First we calculate the starting page */
441 page = to >> NAND_SHIFT;
443 /* Get raw starting column */
445 start = to & (PAGE_DATA_SIZE - 1);
447 // OK now check for the curveball where the start and end are in
449 if((start + n) < PAGE_DATA_SIZE)
455 nToCopy = PAGE_DATA_SIZE - start;
458 nandemul2k_Program(buf, page, start, nToCopy);
459 nandemul2k_Program(oob_buf, page, PAGE_DATA_SIZE, PAGE_SPARE_SIZE);
464 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
474 * NAND write out-of-band
476 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
477 size_t *retlen, const u_char *buf)
483 "nand_read_oob: to = 0x%08x, len = %i\n", (unsigned int) to,
486 /* Shift to get page */
487 page = ((int) to) >> NAND_SHIFT;
489 /* Mask to get column */
490 col = to & PAGE_SPARE_SIZE;
492 /* Initialize return length value */
495 /* Do not allow reads past end of device */
496 if ((to + len) > mtd->size) {
498 "nand_read_oob: Attempt read beyond end of device\n"));
503 nandemul2k_Program(buf,page,512 + col,len);
512 * NAND write with iovec
514 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,7))
515 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
516 unsigned long count, loff_t to, size_t *retlen)
518 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
519 unsigned long count, loff_t to, size_t *retlen)
528 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
530 int i, nBlocks,block;
533 "nand_erase: start = 0x%08x, len = %i\n",
534 (unsigned int) instr->addr, (unsigned int) instr->len));
536 /* Start address must align on block boundary */
537 if (instr->addr & (mtd->erasesize - 1)) {
539 "nand_erase: Unaligned address\n"));
543 /* Length must align on block boundary */
544 if (instr->len & (mtd->erasesize - 1)) {
546 "nand_erase: Length not block aligned\n"));
550 /* Do not allow erase past end of device */
551 if ((instr->len + instr->addr) > mtd->size) {
553 "nand_erase: Erase past end of device\n"));
557 nBlocks = instr->len >> (NAND_SHIFT + BLK_SHIFT);
558 block = instr->addr >> (NAND_SHIFT + BLK_SHIFT);
560 for(i = 0; i < nBlocks; i++)
562 nandemul2k_DoErase(block);
566 instr->state = MTD_ERASE_DONE; /* Changed state to done */
567 instr->callback(instr); /* ... and wake up */
575 static int nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
580 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
589 static void nand_sync (struct mtd_info *mtd)
591 T(0,("nand_sync: called\n"));
596 * Scan for the NAND device
598 static int nandemul2k_scan (struct mtd_info *mtd,int nchips)
600 mtd->writesize = PAGE_DATA_SIZE;
601 mtd->oobsize = PAGE_SPARE_SIZE;
602 mtd->oobavail = PAGE_SPARE_SIZE/2; /* Simulate using up some for other uses */
603 mtd->erasesize = PAGE_DATA_SIZE * PAGES_PER_BLOCK;
604 mtd->size = sizeInMB * 1024*1024;
608 /* Fill in remaining MTD driver data */
609 mtd->type = MTD_NANDFLASH;
610 mtd->flags = MTD_CAP_NANDFLASH;
611 mtd->owner = THIS_MODULE;
612 mtd->erase = nand_erase;
615 mtd->read = nand_read;
616 mtd->write = nand_write;
617 mtd->read_oob = nand_read_oob;
618 mtd->write_oob = nand_write_oob;
619 mtd->read_oob = nand_read_oob;
620 mtd->write_oob = nand_write_oob;
621 mtd->block_isbad = nand_block_isbad;
622 mtd->block_markbad = nand_block_markbad;
624 mtd->writev = nand_writev;
625 mtd->sync = nand_sync;
631 mtd->name = "NANDemul2k";
639 MODULE_PARM(sizeInMB, "i");
641 __setup("sizeInMB=",sizeInMB);
646 * Define partitions for flash devices
649 static struct mtd_partition nandemul2k_partition[] =
651 { .name = "NANDemul partition 1",
656 static int nPartitions = sizeof(nandemul2k_partition)/sizeof(nandemul2k_partition[0]);
659 * Main initialization routine
661 int __init nandemul2k_init (void)
668 nandemul2k_scan(&nandemul2k_mtd,1);
670 // Build the partition table
672 nandemul2k_partition[0].size = sizeInMB * 1024 * 1024;
674 // Register the partition
675 add_mtd_partitions(&nandemul2k_mtd,nandemul2k_partition,nPartitions);
681 module_init(nandemul2k_init);
687 static void __exit nandemul2k_cleanup (void)
690 nandemul2k_CleanUp();
692 /* Unregister partitions */
693 del_mtd_partitions(&nandemul2k_mtd);
695 /* Unregister the device */
696 del_mtd_device (&nandemul2k_mtd);
699 module_exit(nandemul2k_cleanup);
702 MODULE_LICENSE("GPL");
703 MODULE_AUTHOR("Charles Manning <manningc@aleph1.co.uk>");
704 MODULE_DESCRIPTION("2k Page/128k Block NAND emulated in RAM");