yaffs: Fix incorrect incrementing in summary writing.
[yaffs2.git] / mtdemul / nandemul2k.c
index e5217b054ff4c758ec26a334c5f6f86c5e1c8214..d1028ffc7e122bd9f00e7f468dfd43f9a96417f8 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * YAFFS: Yet another FFS. A NAND-flash specific file system. 
+ * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  *
- * Copyright (C) 2002 Aleph One Ltd.
+ * Copyright (C) 2002-2011 Aleph One Ltd.
  *   for Toby Churchill Ltd and Brightstar Engineering
  *
  * Created by Charles Manning <charles@aleph1.co.uk>
@@ -9,12 +9,13 @@
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
- *
- *
+ */
+
+/*
  *  This version hacked for emulating 2kpage NAND for YAFFS2 testing.
  */
 
-#include <linux/config.h>
+//#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/version.h>
 
 
 
-#define EM_SIZE_IN_MEG 4
-#define PAGE_DATA_SIZE  (2048)
-#define PAGE_SPARE_SIZE (64)
-#define PAGES_PER_BLOCK (64)
 #define NAND_SHIFT      (11)   // Shifter for 2k
+#define PAGE_DATA_SIZE  (1 << NAND_SHIFT)
+#define PAGE_SPARE_SIZE (64)
+#define BLK_SHIFT      6
+#define PAGES_PER_BLOCK (1 << BLK_SHIFT)       // = 64
 
 
+#define EM_SIZE_IN_MEG 4
 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
 
 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
@@ -64,7 +66,7 @@ static struct mtd_info nandemul2k_mtd;
 
 typedef struct 
 {
-       __u8 data[PAGE_TOTAL_SIZE]; // Data + spare
+       u8 data[PAGE_TOTAL_SIZE]; // Data + spare
        int empty;      // is this empty?
 } nandemul_Page;
 
@@ -97,32 +99,32 @@ static void nandemul_yield(int n)
 }
 
 
-static void nandemul2k_Read(void *buffer, int page, int start, int nBytes)
+static void nandemul2k_Read(void *buffer, int page, int start, int n_bytes)
 {
        int pg = page%PAGES_PER_BLOCK;
        int blk = page/PAGES_PER_BLOCK;
-       if(buffer && nBytes > 0)
+       if(buffer && n_bytes > 0)
        {
-               memcpy(buffer,&ned.block[blk]->page[pg]->data[start],nBytes);
+               memcpy(buffer,&ned.block[blk]->page[pg]->data[start],n_bytes);
        }
        
 }
 
-static void nandemul2k_Program(const void *buffer, int page, int start, int nBytes)
+static void nandemul2k_Program(const void *buffer, int page, int start, int n_bytes)
 {
        int pg = page%PAGES_PER_BLOCK;
        int blk = page/PAGES_PER_BLOCK;
-       __u8 *p;
-       __u8 *b = (__u8 *)buffer;
+       u8 *p;
+       u8 *b = (u8 *)buffer;
 
        p = &ned.block[blk]->page[pg]->data[start];
        
-       while(buffer && nBytes>0)
+       while(buffer && n_bytes>0)
        {
                *p = *p & *b;
                p++;
                b++;
-               nBytes--;
+               n_bytes--;
        }
 }
 
@@ -253,7 +255,7 @@ int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
 
 
 
-int nandemul2k_ReadId(__u8 *vendorId, __u8 *deviceId)
+static int nandemul2k_ReadId(u8 *vendorId, u8 *deviceId)
 {
        *vendorId = 'Y'; 
        *deviceId = '2';
@@ -262,7 +264,7 @@ int nandemul2k_ReadId(__u8 *vendorId, __u8 *deviceId)
 }
 
 
-int nandemul2k_ReadStatus(__u8 *status)
+static int nandemul2k_ReadStatus(u8 *status)
 {
                *status = 0;
                return 1;
@@ -278,15 +280,10 @@ int nandemul2k_ReadStatus(__u8 *status)
  */
 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len,
                        size_t *retlen, u_char *buf);
-static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
-                               size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *dummy);
 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
                                size_t *retlen, u_char *buf);
 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len,
                        size_t *retlen, const u_char *buf);
-static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
-                               size_t *retlen, const u_char *buf,
-                               u_char *oob_buf, struct nand_oobinfo *dummy);
 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
                                size_t *retlen, const u_char *buf);
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,7))
@@ -341,17 +338,17 @@ static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
 
                /* Get raw starting column */
 
-               start = from & (mtd->oobblock-1);
+               start = from & (PAGE_DATA_SIZE - 1);
 
                // OK now check for the curveball where the start and end are in
                // the same page
-               if((start + n) < mtd->oobblock)
+               if((start + n) < PAGE_DATA_SIZE)
                {
                        nToCopy = n;
                }
                else
                {
-                       nToCopy =  mtd->oobblock - start;
+                       nToCopy =  PAGE_DATA_SIZE - start;
                }
 
                nandemul2k_Read(buf, page, start, nToCopy);
@@ -384,7 +381,7 @@ static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
        page = ((int) from) >> NAND_SHIFT;
 
        /* Mask to get column */
-       col = from & 0x0f;
+       col = from & (PAGE_SPARE_SIZE-1)
 
        /* Initialize return length value */
        *retlen = 0;
@@ -445,17 +442,17 @@ static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
 
                /* Get raw starting column */
 
-               start = to & (mtd->oobblock - 1);
+               start = to & (PAGE_DATA_SIZE - 1);
 
                // OK now check for the curveball where the start and end are in
                // the same page
-               if((start + n) < mtd->oobblock)
+               if((start + n) < PAGE_DATA_SIZE)
                {
                        nToCopy = n;
                }
                else
                {
-                       nToCopy =  mtd->oobblock - start;
+                       nToCopy =  PAGE_DATA_SIZE - start;
                }
 
                nandemul2k_Program(buf, page, start, nToCopy);
@@ -490,7 +487,7 @@ static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
        page = ((int) to) >> NAND_SHIFT;
 
        /* Mask to get column */
-       col = to & 0x0f;
+       col = to & PAGE_SPARE_SIZE;
 
        /* Initialize return length value */
        *retlen = 0;
@@ -557,16 +554,17 @@ static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
                return -EINVAL;
        }
 
-       nBlocks = instr->len >> (NAND_SHIFT + 5);
-       block = instr->addr >> (NAND_SHIFT + 5);
+       nBlocks = instr->len >> (NAND_SHIFT + BLK_SHIFT);
+       block = instr->addr >> (NAND_SHIFT + BLK_SHIFT);
 
        for(i = 0; i < nBlocks; i++)
        {
                nandemul2k_DoErase(block);
                block++;
        }
-
-
+       
+       instr->state = MTD_ERASE_DONE; /* Changed state to done */
+       instr->callback(instr);        /* ... and wake up */
 
        return 0;
 
@@ -599,8 +597,9 @@ static void nand_sync (struct mtd_info *mtd)
  */
 static int nandemul2k_scan (struct mtd_info *mtd,int nchips)
 {
-       mtd->oobblock = PAGE_DATA_SIZE;
-       mtd->oobsize =  PAGE_SPARE_SIZE;
+       mtd->writesize = PAGE_DATA_SIZE;
+       mtd->oobsize   = PAGE_SPARE_SIZE;
+       mtd->oobavail  = PAGE_SPARE_SIZE/2; /* Simulate using up some for other uses */
        mtd->erasesize = PAGE_DATA_SIZE * PAGES_PER_BLOCK;
        mtd->size = sizeInMB * 1024*1024;
 
@@ -610,14 +609,13 @@ static int nandemul2k_scan (struct mtd_info *mtd,int nchips)
        mtd->type = MTD_NANDFLASH;
        mtd->flags = MTD_CAP_NANDFLASH;
        mtd->owner = THIS_MODULE;
-       mtd->ecctype = MTD_ECC_NONE;
        mtd->erase = nand_erase;
        mtd->point = NULL;
        mtd->unpoint = NULL;
        mtd->read = nand_read;
        mtd->write = nand_write;
-       mtd->read_ecc = nand_read_ecc;
-       mtd->write_ecc = nand_write_ecc;
+       mtd->read_oob = nand_read_oob;
+       mtd->write_oob = nand_write_oob;
        mtd->read_oob = nand_read_oob;
        mtd->write_oob = nand_write_oob;
        mtd->block_isbad = nand_block_isbad;