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.
19 #define CONFIG_YAFFS_RAM_ENABLED
22 #ifdef CONFIG_YAFFS_RAM_ENABLED
26 #include "yaffs_nandemul.h"
27 #include "yaffs_guts.h"
28 #include "yaffsinterface.h"
29 #include "devextras.h"
32 #define EM_SIZE_IN_MEG 2
34 #define BLOCK_SIZE (32 * 528)
35 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
36 #define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG)
37 #define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * BLOCK_SIZE)
39 #define T(x) YPRINTF(x)
41 #define DEFAULT_SIZE_IN_MB 2
45 __u8 data[528]; // Data + spare
46 int count[3]; // The programming count for each area of
47 // the page (0..255,256..511,512..527
48 int empty; // is this empty?
53 nandemul_Page page[32]; // The pages in the block
54 __u8 damaged; // Is the block damaged?
62 nandemul_Block **block;
66 static nandemul_Device ned;
68 int sizeInMB = DEFAULT_SIZE_IN_MB;
71 static void nandemul_yield(int n)
74 if(n > 0) schedule_timeout(n);
80 static void nandemul_ReallyEraseBlock(int blockNumber)
84 nandemul_Block *theBlock = ned.block[blockNumber];
86 for(i = 0; i < 32; i++)
88 memset(theBlock->page[i].data,0xff,528);
89 theBlock->page[i].count[0] = 0;
90 theBlock->page[i].count[1] = 0;
91 theBlock->page[i].count[2] = 0;
92 theBlock->page[i].empty = 1;
99 int nandemul_CalcNBlocks(void)
112 sizeInMB = DEFAULT_SIZE_IN_MB;
114 return sizeInMB * 64;
119 static int CheckInit(void)
121 static int initialised = 0;
134 nBlocks = nandemul_CalcNBlocks();
136 ned.block = YMALLOC(sizeof(nandemul_Block *) * nBlocks);
138 if(!ned.block) return 0;
140 for(i=0; i <nBlocks; i++)
145 for(i=0; i <nBlocks && !fail; i++)
147 if((ned.block[i] = YMALLOC(sizeof(nandemul_Block))) == 0)
153 nandemul_ReallyEraseBlock(i);
154 ned.block[i]->damaged = 0;
161 for(i = 0; i < nAllocated; i++)
167 T(("Allocation failed, could only allocate %dMB of %dMB requested.\n",
168 nAllocated/64,sizeInMB));
172 ned.nBlocks = nBlocks;
179 int nandemul_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
187 __u8 *spareAsBytes = (__u8 *)spare;
192 blk = chunkInNAND/32;
198 x = ned.block[blk]->page[pg].data;
200 for(i = 0; i < 512; i++)
205 ned.block[blk]->page[pg].count[0]++;
206 ned.block[blk]->page[pg].count[1]++;
207 ned.block[blk]->page[pg].empty = 0;
213 x = &ned.block[blk]->page[pg].data[512];
215 for(i = 0; i < 16; i++)
217 x[i] &=spareAsBytes[i];
219 ned.block[blk]->page[pg].count[2]++;
231 int nandemul_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
239 blk = chunkInNAND/32;
245 memcpy(data,ned.block[blk]->page[pg].data,512);
251 memcpy(spare,&ned.block[blk]->page[pg].data[512],16);
258 int nandemul_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
267 blk = chunkInNAND/32;
271 for(i = 0; i < 528; i++)
273 if(ned.block[blk]->page[pg].data[i] != 0xFF)
283 int nandemul_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
288 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
290 T(("Attempt to erase non-existant block %d\n",blockNumber));
292 else if(ned.block[blockNumber]->damaged)
294 T(("Attempt to erase damaged block %d\n",blockNumber));
298 nandemul_ReallyEraseBlock(blockNumber);
304 int nandemul_InitialiseNAND(yaffs_Device *dev)
309 #endif //YAFFS_RAM_ENABLED