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>
38 #include "../yaffs_nandemul2k.h"
40 #define ALLOCATE(x) kmalloc(x,GFP_KERNEL)
41 #define FREE(x) kfree(x)
47 #define NAND_SHIFT (11) // Shifter for 2k
48 #define PAGE_DATA_SIZE (1 << NAND_SHIFT)
49 #define PAGE_SPARE_SIZE (64)
51 #define PAGES_PER_BLOCK (1 << BLK_SHIFT) // = 64
54 #define EM_SIZE_IN_MEG 4
55 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
57 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
59 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
61 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
64 static struct mtd_info nandemul2k_mtd;
68 __u8 data[PAGE_TOTAL_SIZE]; // Data + spare
69 int empty; // is this empty?
75 nandemul_Page *page[PAGES_PER_BLOCK];
83 nandemul_Block**block;
87 static nandemul_Device ned;
89 static int sizeInMB = EM_SIZE_IN_MEG;
92 static void nandemul_yield(int n)
95 if(n > 0) schedule_timeout(n);
101 static void nandemul2k_Read(void *buffer, int page, int start, int nBytes)
103 int pg = page%PAGES_PER_BLOCK;
104 int blk = page/PAGES_PER_BLOCK;
105 if(buffer && nBytes > 0)
107 memcpy(buffer,&ned.block[blk]->page[pg]->data[start],nBytes);
112 static void nandemul2k_Program(const void *buffer, int page, int start, int nBytes)
114 int pg = page%PAGES_PER_BLOCK;
115 int blk = page/PAGES_PER_BLOCK;
117 __u8 *b = (__u8 *)buffer;
119 p = &ned.block[blk]->page[pg]->data[start];
121 while(buffer && nBytes>0)
130 static void nandemul2k_DoErase(int blockNumber)
136 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
141 blk = ned.block[blockNumber];
143 for(i = 0; i < PAGES_PER_BLOCK; i++)
145 memset(blk->page[i],0xff,sizeof(nandemul_Page));
146 blk->page[i]->empty = 1;
152 static int nandemul2k_CalcNBlocks(void)
154 return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
159 static int CheckInit(void)
161 static int initialised = 0;
176 ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
179 ned.block = ALLOCATE(sizeof(nandemul_Block*) * nBlocks );
181 if(!ned.block) return ENOMEM;
187 for(i=fail=0; i <nBlocks; i++)
192 if(!(blk = ned.block[i] = ALLOCATE(sizeof(nandemul_Block))))
198 for(j = 0; j < PAGES_PER_BLOCK; j++)
200 if((blk->page[j] = ALLOCATE(sizeof(nandemul_Page))) == 0)
205 nandemul2k_DoErase(i);
206 ned.block[i]->damaged = 0;
215 for(i = 0; i < nAllocated; i++)
224 ned.nBlocks = nBlocks;
233 static void nandemul2k_CleanUp(void)
237 for(i = 0; i < ned.nBlocks; i++)
239 for(j = 0; j < PAGES_PER_BLOCK; j++)
241 FREE(ned.block[i]->page[j]);
250 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
252 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
253 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
257 static int nandemul2k_ReadId(__u8 *vendorId, __u8 *deviceId)
266 static int nandemul2k_ReadStatus(__u8 *status)
273 #ifdef CONFIG_MTD_NAND_ECC
274 #include <linux/mtd/nand_ecc.h>
278 * NAND low-level MTD interface functions
280 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
281 size_t *retlen, u_char *buf);
282 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
283 size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *dummy);
284 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
285 size_t *retlen, u_char *buf);
286 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
287 size_t *retlen, const u_char *buf);
288 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
289 size_t *retlen, const u_char *buf,
290 u_char *oob_buf, struct nand_oobinfo *dummy);
291 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
292 size_t *retlen, const u_char *buf);
293 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,7))
294 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
295 unsigned long count, loff_t to, size_t *retlen);
297 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
298 unsigned long count, loff_t to, size_t *retlen);
300 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
301 static void nand_sync (struct mtd_info *mtd);
308 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
309 size_t *retlen, u_char *buf)
311 return nand_read_ecc (mtd, from, len, retlen, buf, NULL,NULL);
318 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
319 size_t *retlen, u_char *buf, u_char *oob_buf,struct nand_oobinfo *oobsel)
327 /* Do not allow reads past end of device */
328 if ((from + len) > mtd->size) {
334 /* Initialize return value */
340 /* First we calculate the starting page */
341 page = from >> NAND_SHIFT;
343 /* Get raw starting column */
345 start = from & (mtd->oobblock-1);
347 // OK now check for the curveball where the start and end are in
349 if((start + n) < mtd->oobblock)
355 nToCopy = mtd->oobblock - start;
358 nandemul2k_Read(buf, page, start, nToCopy);
359 nandemul2k_Read(oob_buf,page,PAGE_DATA_SIZE,PAGE_SPARE_SIZE);
364 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
374 * NAND read out-of-band
376 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
377 size_t *retlen, u_char *buf)
381 T(0,("nand_read_oob: from = 0x%08x, buf = 0x%08x, len = %i\n", (unsigned int) from, (unsigned int) buf,
384 /* Shift to get page */
385 page = ((int) from) >> NAND_SHIFT;
387 /* Mask to get column */
390 /* Initialize return length value */
393 /* Do not allow reads past end of device */
394 if ((from + len) > mtd->size) {
396 ("nand_read_oob: Attempt read beyond end of device\n"));
401 nandemul2k_Read(buf,page,PAGE_DATA_SIZE + col,len);
411 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
412 size_t *retlen, const u_char *buf)
414 return nand_write_ecc (mtd, to, len, retlen, buf, NULL,NULL);
418 * NAND write with ECC
420 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
421 size_t *retlen, const u_char *buf,
422 u_char *oob_buf, struct nand_oobinfo *dummy)
431 /* Do not allow reads past end of device */
432 if ((to + len) > mtd->size) {
438 /* Initialize return value */
444 /* First we calculate the starting page */
445 page = to >> NAND_SHIFT;
447 /* Get raw starting column */
449 start = to & (mtd->oobblock - 1);
451 // OK now check for the curveball where the start and end are in
453 if((start + n) < mtd->oobblock)
459 nToCopy = mtd->oobblock - start;
462 nandemul2k_Program(buf, page, start, nToCopy);
463 nandemul2k_Program(oob_buf, page, PAGE_DATA_SIZE, PAGE_SPARE_SIZE);
468 if(oob_buf) oob_buf += PAGE_SPARE_SIZE;
478 * NAND write out-of-band
480 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
481 size_t *retlen, const u_char *buf)
487 "nand_read_oob: to = 0x%08x, len = %i\n", (unsigned int) to,
490 /* Shift to get page */
491 page = ((int) to) >> NAND_SHIFT;
493 /* Mask to get column */
496 /* Initialize return length value */
499 /* Do not allow reads past end of device */
500 if ((to + len) > mtd->size) {
502 "nand_read_oob: Attempt read beyond end of device\n"));
507 nandemul2k_Program(buf,page,512 + col,len);
516 * NAND write with iovec
518 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,7))
519 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
520 unsigned long count, loff_t to, size_t *retlen)
522 static int nand_writev (struct mtd_info *mtd, const struct iovec *vecs,
523 unsigned long count, loff_t to, size_t *retlen)
532 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
534 int i, nBlocks,block;
537 "nand_erase: start = 0x%08x, len = %i\n",
538 (unsigned int) instr->addr, (unsigned int) instr->len));
540 /* Start address must align on block boundary */
541 if (instr->addr & (mtd->erasesize - 1)) {
543 "nand_erase: Unaligned address\n"));
547 /* Length must align on block boundary */
548 if (instr->len & (mtd->erasesize - 1)) {
550 "nand_erase: Length not block aligned\n"));
554 /* Do not allow erase past end of device */
555 if ((instr->len + instr->addr) > mtd->size) {
557 "nand_erase: Erase past end of device\n"));
561 nBlocks = instr->len >> (NAND_SHIFT + BLK_SHIFT);
562 block = instr->addr >> (NAND_SHIFT + BLK_SHIFT);
564 for(i = 0; i < nBlocks; i++)
566 nandemul2k_DoErase(block);
578 static int nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
583 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
592 static void nand_sync (struct mtd_info *mtd)
594 T(0,("nand_sync: called\n"));
599 * Scan for the NAND device
601 static int nandemul2k_scan (struct mtd_info *mtd,int nchips)
603 mtd->oobblock = PAGE_DATA_SIZE;
604 mtd->oobsize = PAGE_SPARE_SIZE;
605 mtd->erasesize = PAGE_DATA_SIZE * PAGES_PER_BLOCK;
606 mtd->size = sizeInMB * 1024*1024;
610 /* Fill in remaining MTD driver data */
611 mtd->type = MTD_NANDFLASH;
612 mtd->flags = MTD_CAP_NANDFLASH;
613 mtd->owner = THIS_MODULE;
614 mtd->ecctype = MTD_ECC_NONE;
615 mtd->erase = nand_erase;
618 mtd->read = nand_read;
619 mtd->write = nand_write;
620 mtd->read_ecc = nand_read_ecc;
621 mtd->write_ecc = nand_write_ecc;
622 mtd->read_oob = nand_read_oob;
623 mtd->write_oob = nand_write_oob;
624 mtd->block_isbad = nand_block_isbad;
625 mtd->block_markbad = nand_block_markbad;
627 mtd->writev = nand_writev;
628 mtd->sync = nand_sync;
634 mtd->name = "NANDemul2k";
642 MODULE_PARM(sizeInMB, "i");
644 __setup("sizeInMB=",sizeInMB);
649 * Define partitions for flash devices
652 static struct mtd_partition nandemul2k_partition[] =
654 { .name = "NANDemul partition 1",
659 static int nPartitions = sizeof(nandemul2k_partition)/sizeof(nandemul2k_partition[0]);
662 * Main initialization routine
664 int __init nandemul2k_init (void)
671 nandemul2k_scan(&nandemul2k_mtd,1);
673 // Build the partition table
675 nandemul2k_partition[0].size = sizeInMB * 1024 * 1024;
677 // Register the partition
678 add_mtd_partitions(&nandemul2k_mtd,nandemul2k_partition,nPartitions);
684 module_init(nandemul2k_init);
690 static void __exit nandemul2k_cleanup (void)
693 nandemul2k_CleanUp();
695 /* Unregister partitions */
696 del_mtd_partitions(&nandemul2k_mtd);
698 /* Unregister the device */
699 del_mtd_device (&nandemul2k_mtd);
702 module_exit(nandemul2k_cleanup);
705 MODULE_LICENSE("GPL");
706 MODULE_AUTHOR("Charles Manning <manningc@aleph1.co.uk>");
707 MODULE_DESCRIPTION("2k Page/128k Block NAND emulated in RAM");