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.3 2005-07-29 19:57:38 luc Exp $";
21 #define CONFIG_YAFFS_RAM_ENABLED
23 #include <linux/config.h>
26 #ifdef CONFIG_YAFFS_RAM_ENABLED
30 #include "yaffs_nandemul.h"
31 #include "yaffs_guts.h"
32 #include "yaffsinterface.h"
33 #include "devextras.h"
36 #define EM_SIZE_IN_MEG 2
38 #define YAFFS_BLOCK_SIZE (32 * 528)
39 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
40 #define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG)
41 #define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * YAFFS_BLOCK_SIZE)
45 #define DEFAULT_SIZE_IN_MB 2
49 __u8 data[528]; // Data + spare
50 int count[3]; // The programming count for each area of
51 // the page (0..255,256..511,512..527
52 int empty; // is this empty?
57 nandemul_Page page[32]; // The pages in the block
58 __u8 damaged; // Is the block damaged?
66 nandemul_Block **block;
70 static nandemul_Device ned;
72 static int sizeInMB = DEFAULT_SIZE_IN_MB;
75 static void nandemul_yield(int n)
78 if(n > 0) schedule_timeout(n);
84 static void nandemul_ReallyEraseBlock(int blockNumber)
88 nandemul_Block *theBlock = ned.block[blockNumber];
90 for(i = 0; i < 32; i++)
92 memset(theBlock->page[i].data,0xff,528);
93 theBlock->page[i].count[0] = 0;
94 theBlock->page[i].count[1] = 0;
95 theBlock->page[i].count[2] = 0;
96 theBlock->page[i].empty = 1;
103 int nandemul_CalcNBlocks(void)
116 sizeInMB = DEFAULT_SIZE_IN_MB;
118 return sizeInMB * 64;
123 static int CheckInit(void)
125 static int initialised = 0;
138 nBlocks = nandemul_CalcNBlocks();
140 ned.block = YMALLOC(sizeof(nandemul_Block *) * nBlocks);
142 if(!ned.block) return 0;
144 for(i=0; i <nBlocks; i++)
149 for(i=0; i <nBlocks && !fail; i++)
151 if((ned.block[i] = YMALLOC(sizeof(nandemul_Block))) == 0)
157 nandemul_ReallyEraseBlock(i);
158 ned.block[i]->damaged = 0;
165 for(i = 0; i < nAllocated; i++)
171 T(YAFFS_TRACE_ALWAYS,("Allocation failed, could only allocate %dMB of %dMB requested.\n",
172 nAllocated/64,sizeInMB));
176 ned.nBlocks = nBlocks;
183 int nandemul_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
191 __u8 *spareAsBytes = (__u8 *)spare;
196 blk = chunkInNAND/32;
202 x = ned.block[blk]->page[pg].data;
204 for(i = 0; i < 512; i++)
209 ned.block[blk]->page[pg].count[0]++;
210 ned.block[blk]->page[pg].count[1]++;
211 ned.block[blk]->page[pg].empty = 0;
217 x = &ned.block[blk]->page[pg].data[512];
219 for(i = 0; i < 16; i++)
221 x[i] &=spareAsBytes[i];
223 ned.block[blk]->page[pg].count[2]++;
235 int nandemul_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
243 blk = chunkInNAND/32;
249 memcpy(data,ned.block[blk]->page[pg].data,512);
255 memcpy(spare,&ned.block[blk]->page[pg].data[512],16);
262 int nandemul_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
271 blk = chunkInNAND/32;
275 for(i = 0; i < 528; i++)
277 if(ned.block[blk]->page[pg].data[i] != 0xFF)
287 int nandemul_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
292 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
294 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
296 else if(ned.block[blockNumber]->damaged)
298 T(YAFFS_TRACE_ALWAYS,("Attempt to erase damaged block %d\n",blockNumber));
302 nandemul_ReallyEraseBlock(blockNumber);
308 int nandemul_InitialiseNAND(yaffs_Device *dev)
313 #endif //YAFFS_RAM_ENABLED