Change yboot.c to LGPL as was intended
[yaffs/.git] / yaffs_mtdif.c
index 923de914c83afd471b2eb4abc810ba4da3614cc3..6dede1a55259b29d30ac8bfff80a773da0475373 100644 (file)
@@ -13,7 +13,7 @@
  *
  */
 
-const char *yaffs_mtdif_c_version = "$Id: yaffs_mtdif.c,v 1.3 2002-09-27 20:50:50 charles Exp $";
+const char *yaffs_mtdif_c_version = "$Id: yaffs_mtdif.c,v 1.10 2004-09-19 08:14:50 charles Exp $";
 
 #ifdef CONFIG_YAFFS_MTD_ENABLED
  
@@ -23,79 +23,139 @@ const char *yaffs_mtdif_c_version = "$Id: yaffs_mtdif.c,v 1.3 2002-09-27 20:50:5
 
 #include "linux/mtd/mtd.h"
 #include "linux/types.h"
+#include "linux/time.h"
+
+#ifndef        CONFIG_YAFFS_USE_OLD_MTD
+#include "linux/mtd/nand.h"
+#endif
+
+struct nand_oobinfo yaffs_oobinfo = {
+       useecc: 1,
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8))
+// this is for versions of mtd nand driver in kernel 2.6.8 and later
+       eccbytes: 6,
+#endif
+       eccpos: {8, 9, 10, 13, 14, 15}
+};
+
+struct nand_oobinfo yaffs_noeccinfo = {
+       useecc: 0,
+};
 
 
 int nandmtd_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
 {
        struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
        size_t dummy;
+    int retval = 0;
        
-       loff_t addr = ((loff_t)chunkInNAND) * YAFFS_BYTES_PER_CHUNK;
-       
+       loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk;
        
        __u8 *spareAsBytes = (__u8 *)spare;
-       
+
+#ifndef        CONFIG_YAFFS_USE_OLD_MTD
+       if(data && spare)
+       {
+               if(dev->useNANDECC)
+                       mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo);
+               else
+                       mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo);
+       }
+       else
+       {
+#endif
        if(data)
-               mtd->write(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data);
+               retval = mtd->write(mtd,addr,dev->nBytesPerChunk,&dummy,data);
        if(spare)
-               mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
-
-       return YAFFS_OK;
+               retval = mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
+#ifndef        CONFIG_YAFFS_USE_OLD_MTD
+       }
+#endif
+
+    if (retval == 0)
+       return YAFFS_OK;
+    else
+        return YAFFS_FAIL;
 }
 
-
 int nandmtd_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
 {
        struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
        size_t dummy;
+    int retval = 0;
        
-       loff_t addr = ((loff_t)chunkInNAND) * YAFFS_BYTES_PER_CHUNK;
+       loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk;
        
        __u8 *spareAsBytes = (__u8 *)spare;
        
+#ifndef        CONFIG_YAFFS_USE_OLD_MTD
+       if(data && spare)
+       {
+               if(dev->useNANDECC)
+               {   // Careful, this call adds 2 ints to the end of the spare data.  Calling function should
+            // allocate enough memory for spare, i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)].
+               retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo);            
+               }
+               else
+               {
+                       retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo);
+               }
+       }
+       else
+       {
+#endif
        if(data)
-               mtd->read(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data);
+               retval = mtd->read(mtd,addr,dev->nBytesPerChunk,&dummy,data);
        if(spare)
-               mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
-
-       return YAFFS_OK;
+               retval = mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
+#ifndef        CONFIG_YAFFS_USE_OLD_MTD
+       }
+#endif
+
+    if (retval == 0)
+       return YAFFS_OK;
+    else
+        return YAFFS_FAIL;
 }
 
-
+// Callback not needed for NAND
+#if 0
 static void nandmtd_EraseCallback(struct erase_info *ei)
 {
        yaffs_Device *dev = (yaffs_Device *)ei->priv;   
        up(&dev->sem);
 }
+#endif
 
 
 int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
 {
-       
        struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
-       __u32 addr = ((loff_t) blockNumber) * YAFFS_BYTES_PER_BLOCK;
+       __u32 addr = ((loff_t) blockNumber) * dev->nBytesPerChunk * dev->nChunksPerBlock;
        struct erase_info ei;
+    int retval = 0;
        
        ei.mtd = mtd;
        ei.addr = addr;
-       ei.len = YAFFS_BYTES_PER_BLOCK;
+       ei.len = dev->nBytesPerChunk * dev->nChunksPerBlock;
        ei.time = 1000;
        ei.retries = 2;
-       ei.callback = nandmtd_EraseCallback;
+       ei.callback = NULL;
        ei.priv = (u_long)dev;
        
        // Todo finish off the ei if required
        
        sema_init(&dev->sem,0);
 
-       mtd->erase(mtd,&ei);
+       retval = mtd->erase(mtd,&ei);   
        
-       
-       down(&dev->sem); // Wait for the erasure to complete
-                       
-       //Todo, check result of erasure
-       
-       return YAFFS_OK;
+       //No need for callback 
+       // down(&dev->sem); // Wait for the erasure to complete
+
+    if (retval == 0)   
+       return YAFFS_OK;
+    else
+        return YAFFS_FAIL;
 }
 
 int nandmtd_InitialiseNAND(yaffs_Device *dev)