yaffs: Refactor drivers WIP - stress tests passing
[yaffs2.git] / direct / basic-test / yaffs_norif1.c
index 4fcf97129d24bf3505a9a81d6f617a366a0626de..20511e45e85c973d35bc86882a917f964f159431 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  *
- * Copyright (C) 2002-2010 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>
 
 /* First set up for M18 with 1k chunks and 16-byte spares.
  *
- * NB We're using the oddball M18 modes of operation here 
+ * NB We're using the oddball M18 modes of operation here
  * The chip is 64MB based at 0x0000, but YAFFS only going to use the top half
  * ie. YAFFS will be from 32MB to 64MB.
  *
- * The M18 has two ways of writing data. Every Programming Region (1kbytes) 
+ * The M18 has two ways of writing data. Every Programming Region (1kbytes)
  * can be programmed in two modes:
  * * Object Mode 1024 bytes of write once data.
- * * Control Mode: 512bytes of bit-writeable data. 
+ * * Control Mode: 512bytes of bit-writeable data.
  *    This is arranged as 32 * (16 bytes of bit-writable followed by 16 bytes of "dont touch")
- * 
+ *
  * The block size is 256kB, making 128 blocks in the 32MB YAFFS area.
  * Each block comprises:
  *   Offset   0k: 248 x 1k data pages
  *   Offset 248k: 248 x 32-byte spare areas implemented as 16 bytes of spare followed by 16 bytes untouched)
  *   Offset 248k + (248 * 32): Format marker
- *   
+ *
  */
 
 const char *yaffs_norif1_c_version = "$Id: yaffs_norif1.c,v 1.6 2010-02-18 01:18:04 charles Exp $";
@@ -45,8 +45,6 @@ const char *yaffs_norif1_c_version = "$Id: yaffs_norif1.c,v 1.6 2010-02-18 01:18
 #include "yaffs_flashif.h"
 #include "yaffs_guts.h"
 
-#include "devextras.h"
-
 #define SPARE_BYTES_PER_CHUNK  16
 #define M18_SKIP               16
 #define PROG_REGION_SIZE       1024
@@ -72,8 +70,8 @@ const char *yaffs_norif1_c_version = "$Id: yaffs_norif1.c,v 1.6 2010-02-18 01:18
 #include "ynorsim.h"
 #define ynorif1_FlashInit() ynorsim_initialise()
 #define ynorif1_FlashDeinit() ynorsim_shutdown()
-#define ynorif1_FlashWrite32(addr,buf,nwords) ynorsim_wr32(addr,buf,nwords) 
-#define ynorif1_FlashRead32(addr,buf,nwords) ynorsim_rd32(addr,buf,nwords) 
+#define ynorif1_FlashWrite32(addr,buf,nwords) ynorsim_wr32(addr,buf,nwords)
+#define ynorif1_FlashRead32(addr,buf,nwords) ynorsim_rd32(addr,buf,nwords)
 #define ynorif1_FlashEraseBlock(addr) ynorsim_erase(addr)
 #define DEVICE_BASE     ynorsim_get_base()
 #else
@@ -82,64 +80,65 @@ const char *yaffs_norif1_c_version = "$Id: yaffs_norif1.c,v 1.6 2010-02-18 01:18
 #include "../blob/yflashrw.h"
 #define ynorif1_FlashInit()  do{} while(0)
 #define ynorif1_FlashDeinit() do {} while(0)
-#define ynorif1_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords) 
-#define ynorif1_FlashRead32(addr,buf,nwords)  Y_FlashRead(addr,buf,nwords) 
+#define ynorif1_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
+#define ynorif1_FlashRead32(addr,buf,nwords)  Y_FlashRead(addr,buf,nwords)
 #define ynorif1_FlashEraseBlock(addr)         Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
 #define DEVICE_BASE     (32 * 1024 * 1024)
 #endif
 
-__u32 *Block2Addr(yaffs_Device *dev, int blockNumber)
+static u32 *Block2Addr(struct yaffs_dev *dev, int blockNumber)
 {
-       __u32 addr;
+       u32 addr;
        dev=dev;
-       
-       addr = (__u32) DEVICE_BASE;
+
+       addr = (u32) DEVICE_BASE;
        addr += blockNumber * BLOCK_SIZE_IN_BYTES;
-       
-       return (__u32 *) addr;
+
+       return (u32 *) addr;
 }
 
-__u32 *Block2FormatAddr(yaffs_Device *dev,int blockNumber)
+static u32 *Block2FormatAddr(struct yaffs_dev *dev,int blockNumber)
 {
-       __u32 addr;
+       u32 addr;
 
-       addr = (__u32) Block2Addr(dev,blockNumber);
+       addr = (u32) Block2Addr(dev,blockNumber);
        addr += FORMAT_OFFSET;
-       
-       return (__u32 *)addr;
+
+       return (u32 *)addr;
 }
-__u32 *Chunk2DataAddr(yaffs_Device *dev,int chunkId)
+
+static u32 *Chunk2DataAddr(struct yaffs_dev *dev,int chunk_id)
 {
        unsigned block;
        unsigned chunkInBlock;
-       __u32  addr;
-       
-       block = chunkId/dev->param.nChunksPerBlock;
-       chunkInBlock = chunkId % dev->param.nChunksPerBlock;
-       
-       addr = (__u32) Block2Addr(dev,block);
+       u32  addr;
+
+       block = chunk_id/dev->param.chunks_per_block;
+       chunkInBlock = chunk_id % dev->param.chunks_per_block;
+
+       addr = (u32) Block2Addr(dev,block);
        addr += chunkInBlock * DATA_BYTES_PER_CHUNK;
-       
-       return (__u32 *)addr;
+
+       return (u32 *)addr;
 }
 
-__u32 *Chunk2SpareAddr(yaffs_Device *dev,int chunkId)
+static u32 *Chunk2SpareAddr(struct yaffs_dev *dev,int chunk_id)
 {
        unsigned block;
        unsigned chunkInBlock;
-       __u32 addr;
-       
-       block = chunkId/dev->param.nChunksPerBlock;
-       chunkInBlock = chunkId % dev->param.nChunksPerBlock;
-       
-       addr = (__u32) Block2Addr(dev,block);
+       u32 addr;
+
+       block = chunk_id/dev->param.chunks_per_block;
+       chunkInBlock = chunk_id % dev->param.chunks_per_block;
+
+       addr = (u32) Block2Addr(dev,block);
        addr += SPARE_AREA_OFFSET;
        addr += chunkInBlock * (SPARE_BYTES_PER_CHUNK + M18_SKIP);
-       return (__u32 *)addr;
+       return (u32 *)addr;
 }
 
 
-void ynorif1_AndBytes(__u8*target, const __u8   *src, int nbytes)
+static void ynorif1_AndBytes(u8*target, const u8   *src, int nbytes)
 {
         while(nbytes > 0){
                 *target &= *src;
@@ -148,133 +147,149 @@ void ynorif1_AndBytes(__u8*target, const __u8   *src, int nbytes)
                 nbytes--;
         }
 }
-
-int ynorif1_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
+       int (*drv_write_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
+                                  const u8 *data, int data_len,
+                                  const u8 *oob, int oob_len);
+static int ynorif1_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,
+                                   const u8 *data, int data_len,
+                                   const u8 *oob, int oob_len)
 {
-        __u32 *dataAddr = Chunk2DataAddr(dev,chunkInNAND);
-        __u32 *spareAddr = Chunk2SpareAddr(dev,chunkInNAND);
-        
-        yaffs_Spare tmpSpare;
-        
+        u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
+        u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
+
+       struct yaffs_spare *spare = (struct yaffs_spare *)oob;
+        struct yaffs_spare tmpSpare;
+
+       (void) oob_len;
+
         /* We should only be getting called for one of 3 reasons:
          * Writing a chunk: data and spare will not be NULL
          * Writing a deletion marker: data will be NULL, spare not NULL
          * Writing a bad block marker: data will be NULL, spare not NULL
          */
-         
-        if(sizeof(yaffs_Spare) != 16)
-                YBUG();
-        
-        if(data && spare)
+
+        if(sizeof(struct yaffs_spare) != 16)
+                BUG();
+
+        if(data && oob)
         {
-                if(spare->pageStatus != 0xff)
-                        YBUG();
+                if(spare->page_status != 0xff)
+                        BUG();
                 /* Write a pre-marker */
                 memset(&tmpSpare,0xff,sizeof(tmpSpare));
-                tmpSpare.pageStatus = YNOR_PREMARKER;
-                ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(yaffs_Spare)/4);
-
-                /* Write the data */            
-                ynorif1_FlashWrite32(dataAddr,(__u32 *)data,dev->param.totalBytesPerChunk / 4);
-                
-                
-                memcpy(&tmpSpare,spare,sizeof(yaffs_Spare));
-                
+                tmpSpare.page_status = YNOR_PREMARKER;
+                ynorif1_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/4);
+
+                /* Write the data */
+                ynorif1_FlashWrite32(dataAddr,(u32 *)data, data_len/ 4);
+
+
+                memcpy(&tmpSpare,spare,sizeof(struct yaffs_spare));
+
                 /* Write the real tags, but override the premarker*/
-                tmpSpare.pageStatus = YNOR_PREMARKER;
-                ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(yaffs_Spare)/4);
-                
+                tmpSpare.page_status = YNOR_PREMARKER;
+                ynorif1_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/4);
+
                 /* Write a post-marker */
-                tmpSpare.pageStatus = YNOR_POSTMARKER;
-                ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(tmpSpare)/4);  
+                tmpSpare.page_status = YNOR_POSTMARKER;
+                ynorif1_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(tmpSpare)/4);
 
         } else if(spare){
                 /* This has to be a read-modify-write operation to handle NOR-ness */
 
-                ynorif1_FlashRead32(spareAddr,(__u32 *)&tmpSpare,16/ 4);
-                
-                ynorif1_AndBytes((__u8 *)&tmpSpare,(__u8 *)spare,sizeof(yaffs_Spare));
-                
-                ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,16/ 4);
+                ynorif1_FlashRead32(spareAddr,(u32 *)&tmpSpare,16/ 4);
+
+                ynorif1_AndBytes((u8 *)&tmpSpare,(u8 *)spare,sizeof(struct yaffs_spare));
+
+                ynorif1_FlashWrite32(spareAddr,(u32 *)&tmpSpare,16/ 4);
         }
         else {
-                YBUG();
+                BUG();
         }
-        
 
-       return YAFFS_OK;        
+
+       return YAFFS_OK;
 
 }
 
-int ynorif1_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
+static int ynorif1_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk,
+                                       u8 *data, int data_len,
+                                       u8 *oob, int oob_len,
+                                       enum yaffs_ecc_result *ecc_result)
 {
+       struct yaffs_spare *spare = (struct yaffs_spare *)oob;
+
+       u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
+       u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
 
-       __u32 *dataAddr = Chunk2DataAddr(dev,chunkInNAND);
-       __u32 *spareAddr = Chunk2SpareAddr(dev,chunkInNAND);
-       
        if(data)
        {
-               ynorif1_FlashRead32(dataAddr,(__u32 *)data,dev->param.totalBytesPerChunk / 4);
+               ynorif1_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / 4);
        }
-       
-        if(spare)
+
+        if(oob)
         {
-                ynorif1_FlashRead32(spareAddr,(__u32 *)spare,16/ 4);
-                
+                ynorif1_FlashRead32(spareAddr,(u32 *)spare, oob_len/ 4);
+
                 /* If the page status is YNOR_POSTMARKER then it was written properly
                  * so change that to 0xFF so that the rest of yaffs is happy.
                  */
-                if(spare->pageStatus == YNOR_POSTMARKER)
-                        spare->pageStatus = 0xFF;
-               else if(spare->pageStatus != 0xff &&
-                       (spare->pageStatus | YNOR_PREMARKER) != 0xff)
-                       spare->pageStatus = YNOR_PREMARKER;
+                if(spare->page_status == YNOR_POSTMARKER)
+                        spare->page_status = 0xFF;
+               else if(spare->page_status != 0xff &&
+                       (spare->page_status | YNOR_PREMARKER) != 0xff)
+                       spare->page_status = YNOR_PREMARKER;
         }
-        
 
-       return YAFFS_OK;        
+       if(ecc_result)
+               *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
+
+       return YAFFS_OK;
 
 }
 
-static int ynorif1_FormatBlock(yaffs_Device *dev, int blockNumber)
+
+static int ynorif1_FormatBlock(struct yaffs_dev *dev, int blockNumber)
 {
-       __u32 *blockAddr = Block2Addr(dev,blockNumber);
-       __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
-       __u32 formatValue = FORMAT_VALUE;
-       
+       u32 *blockAddr = Block2Addr(dev,blockNumber);
+       u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
+       u32 formatValue = FORMAT_VALUE;
+
        ynorif1_FlashEraseBlock(blockAddr);
        ynorif1_FlashWrite32(formatAddr,&formatValue,1);
-       
+
        return YAFFS_OK;
 }
 
-static int ynorif1_UnformatBlock(yaffs_Device *dev, int blockNumber)
+static int ynorif1_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
 {
-       __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
-       __u32 formatValue = 0;
-       
+       u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
+       u32 formatValue = 0;
+
        ynorif1_FlashWrite32(formatAddr,&formatValue,1);
-       
+
        return YAFFS_OK;
 }
 
-static int ynorif1_IsBlockFormatted(yaffs_Device *dev, int blockNumber)
+static int ynorif1_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
 {
-       __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
-       __u32 formatValue;
-       
-       
+       u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
+       u32 formatValue;
+
+
        ynorif1_FlashRead32(formatAddr,&formatValue,1);
-       
+
        return (formatValue == FORMAT_VALUE);
 }
 
-int ynorif1_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
+static int ynorif1_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
 {
 
        if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
        {
-               T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
+               yaffs_trace(YAFFS_TRACE_ALWAYS,
+                       "Attempt to erase non-existant block %d\n",
+                       blockNumber);
                return YAFFS_FAIL;
        }
        else
@@ -283,16 +298,16 @@ int ynorif1_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
                ynorif1_FormatBlock(dev,blockNumber);
                return YAFFS_OK;
        }
-       
+
 }
 
-int ynorif1_InitialiseNAND(yaffs_Device *dev)
+static int ynorif1_InitialiseNAND(struct yaffs_dev *dev)
 {
        int i;
-       
+
        ynorif1_FlashInit();
        /* Go through the blocks formatting them if they are not formatted */
-       for(i = dev->param.startBlock; i <= dev->param.endBlock; i++){
+       for(i = dev->param.start_block; i <= dev->param.end_block; i++){
                if(!ynorif1_IsBlockFormatted(dev,i)){
                        ynorif1_FormatBlock(dev,i);
                }
@@ -300,12 +315,28 @@ int ynorif1_InitialiseNAND(yaffs_Device *dev)
        return YAFFS_OK;
 }
 
-int ynorif1_DeinitialiseNAND(yaffs_Device *dev)
+static int ynorif1_Deinitialise_flash_fn(struct yaffs_dev *dev)
 {
-       dev=dev;        
+       dev=dev;
        ynorif1_FlashDeinit();
 
        return YAFFS_OK;
 }
 
+void ynorif1_install_drv(struct yaffs_dev *dev)
+{
+       struct yaffs_param *param = &dev->param;
+
+       param->total_bytes_per_chunk = 1024;
+       param->chunks_per_block =248;
+       param->n_reserved_blocks = 2;
+       param->start_block = 0; // Can use block 0
+       param->end_block = 31; // Last block
+       param->use_nand_ecc = 0; // use YAFFS's ECC
+       param->drv_write_chunk_fn = ynorif1_WriteChunkToNAND;
+       param->drv_read_chunk_fn = ynorif1_ReadChunkFromNAND;
+       param->drv_erase_fn = ynorif1_EraseBlockInNAND;
+       param->drv_initialise_fn = ynorif1_InitialiseNAND;
+       param->drv_deinitialise_fn = ynorif1_Deinitialise_flash_fn;
+}