2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_ramem.c NAND emulation on top of a chunk of RAM
5 * Copyright (C) 2002 Aleph One Ltd.
6 * for Toby Churchill Ltd and Brightstar Engineering
8 * Created by Charles Manning <charles@aleph1.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 // Since this creates the RAM block at start up it is pretty useless for testing the scanner.
18 const char *yaffs_ramem_c_version = "$Id: yaffs_ramem.c,v 1.2 2005-07-19 20:41:59 charles Exp $";
21 #define CONFIG_YAFFS_RAM_ENABLED
24 #ifdef CONFIG_YAFFS_RAM_ENABLED
28 #include "yaffs_nandemul.h"
29 #include "yaffs_guts.h"
30 #include "yaffsinterface.h"
31 #include "devextras.h"
34 #define EM_SIZE_IN_MEG 2
36 #define YAFFS_BLOCK_SIZE (32 * 528)
37 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
38 #define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG)
39 #define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * YAFFS_BLOCK_SIZE)
43 #define DEFAULT_SIZE_IN_MB 2
47 __u8 data[528]; // Data + spare
48 int count[3]; // The programming count for each area of
49 // the page (0..255,256..511,512..527
50 int empty; // is this empty?
55 nandemul_Page page[32]; // The pages in the block
56 __u8 damaged; // Is the block damaged?
64 nandemul_Block **block;
68 static nandemul_Device ned;
70 static int sizeInMB = DEFAULT_SIZE_IN_MB;
73 static void nandemul_yield(int n)
76 if(n > 0) schedule_timeout(n);
82 static void nandemul_ReallyEraseBlock(int blockNumber)
86 nandemul_Block *theBlock = ned.block[blockNumber];
88 for(i = 0; i < 32; i++)
90 memset(theBlock->page[i].data,0xff,528);
91 theBlock->page[i].count[0] = 0;
92 theBlock->page[i].count[1] = 0;
93 theBlock->page[i].count[2] = 0;
94 theBlock->page[i].empty = 1;
101 int nandemul_CalcNBlocks(void)
114 sizeInMB = DEFAULT_SIZE_IN_MB;
116 return sizeInMB * 64;
121 static int CheckInit(void)
123 static int initialised = 0;
136 nBlocks = nandemul_CalcNBlocks();
138 ned.block = YMALLOC(sizeof(nandemul_Block *) * nBlocks);
140 if(!ned.block) return 0;
142 for(i=0; i <nBlocks; i++)
147 for(i=0; i <nBlocks && !fail; i++)
149 if((ned.block[i] = YMALLOC(sizeof(nandemul_Block))) == 0)
155 nandemul_ReallyEraseBlock(i);
156 ned.block[i]->damaged = 0;
163 for(i = 0; i < nAllocated; i++)
169 T(YAFFS_TRACE_ALWAYS,("Allocation failed, could only allocate %dMB of %dMB requested.\n",
170 nAllocated/64,sizeInMB));
174 ned.nBlocks = nBlocks;
181 int nandemul_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
189 __u8 *spareAsBytes = (__u8 *)spare;
194 blk = chunkInNAND/32;
200 x = ned.block[blk]->page[pg].data;
202 for(i = 0; i < 512; i++)
207 ned.block[blk]->page[pg].count[0]++;
208 ned.block[blk]->page[pg].count[1]++;
209 ned.block[blk]->page[pg].empty = 0;
215 x = &ned.block[blk]->page[pg].data[512];
217 for(i = 0; i < 16; i++)
219 x[i] &=spareAsBytes[i];
221 ned.block[blk]->page[pg].count[2]++;
233 int nandemul_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
241 blk = chunkInNAND/32;
247 memcpy(data,ned.block[blk]->page[pg].data,512);
253 memcpy(spare,&ned.block[blk]->page[pg].data[512],16);
260 int nandemul_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
269 blk = chunkInNAND/32;
273 for(i = 0; i < 528; i++)
275 if(ned.block[blk]->page[pg].data[i] != 0xFF)
285 int nandemul_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
290 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
292 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
294 else if(ned.block[blockNumber]->damaged)
296 T(YAFFS_TRACE_ALWAYS,("Attempt to erase damaged block %d\n",blockNumber));
300 nandemul_ReallyEraseBlock(blockNumber);
306 int nandemul_InitialiseNAND(yaffs_Device *dev)
311 #endif //YAFFS_RAM_ENABLED