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 #ifdef YAFFS_RAM_ENABLED
22 #include "yaffs_nandemul.h"
23 #include "yaffs_guts.h"
24 #include "yaffsinterface.h"
25 #include "devextras.h"
28 #define EM_SIZE_IN_MEG 2
30 #define BLOCK_SIZE (32 * 528)
31 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
32 #define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG)
33 #define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * BLOCK_SIZE)
35 #define T(x) YPRINTF(x)
37 #define DEFAULT_SIZE_IN_MB 2
41 __u8 data[528]; // Data + spare
42 int count[3]; // The programming count for each area of
43 // the page (0..255,256..511,512..527
44 int empty; // is this empty?
49 nandemul_Page page[32]; // The pages in the block
50 __u8 damaged; // Is the block damaged?
58 nandemul_Block **block;
62 static nandemul_Device ned;
64 int sizeInMB = DEFAULT_SIZE_IN_MB;
67 static void nandemul_ReallyEraseBlock(int blockNumber)
71 nandemul_Block *theBlock = ned.block[blockNumber];
73 for(i = 0; i < 32; i++)
75 memset(theBlock->page[i].data,0xff,528);
76 theBlock->page[i].count[0] = 0;
77 theBlock->page[i].count[1] = 0;
78 theBlock->page[i].count[2] = 0;
79 theBlock->page[i].empty = 1;
85 int nandemul_CalcNBlocks(void)
98 sizeInMB = DEFAULT_SIZE_IN_MB;
100 return sizeInMB * 64;
105 static int CheckInit(void)
107 static int initialised = 0;
120 nBlocks = nandemul_CalcNBlocks();
122 ned.block = YMALLOC(sizeof(nandemul_Block *) * nBlocks);
124 if(!ned.block) return 0;
126 for(i=0; i <nBlocks; i++)
131 for(i=0; i <nBlocks && !fail; i++)
133 if((ned.block[i] = YMALLOC(sizeof(nandemul_Block))) == 0)
139 nandemul_ReallyEraseBlock(i);
140 ned.block[i]->damaged = 0;
147 for(i = 0; i < nAllocated; i++)
153 T(("Allocation failed, could only allocate %dMB of %dMB requested.\n",
154 nAllocated/64,sizeInMB));
158 ned.nBlocks = nBlocks;
165 int nandemul_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
173 __u8 *spareAsBytes = (__u8 *)spare;
178 blk = chunkInNAND/32;
184 x = ned.block[blk]->page[pg].data;
186 for(i = 0; i < 512; i++)
191 ned.block[blk]->page[pg].count[0]++;
192 ned.block[blk]->page[pg].count[1]++;
193 ned.block[blk]->page[pg].empty = 0;
199 x = &ned.block[blk]->page[pg].data[512];
201 for(i = 0; i < 16; i++)
203 x[i] &=spareAsBytes[i];
205 ned.block[blk]->page[pg].count[2]++;
212 int nandemul_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
220 blk = chunkInNAND/32;
226 memcpy(data,ned.block[blk]->page[pg].data,512);
232 memcpy(spare,&ned.block[blk]->page[pg].data[512],16);
239 int nandemul_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
248 blk = chunkInNAND/32;
252 for(i = 0; i < 528; i++)
254 if(ned.block[blk]->page[pg].data[i] != 0xFF)
264 int nandemul_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
269 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
271 T(("Attempt to erase non-existant block %d\n",blockNumber));
273 else if(ned.block[blockNumber]->damaged)
275 T(("Attempt to erase damaged block %d\n",blockNumber));
279 nandemul_ReallyEraseBlock(blockNumber);
285 int nandemul_InitialiseNAND(yaffs_Device *dev)
290 #endif //YAFFS_RAM_ENABLED