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 YAFFS_RAM_ENABLED
22 #ifdef 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_ReallyEraseBlock(int blockNumber)
75 nandemul_Block *theBlock = ned.block[blockNumber];
77 for(i = 0; i < 32; i++)
79 memset(theBlock->page[i].data,0xff,528);
80 theBlock->page[i].count[0] = 0;
81 theBlock->page[i].count[1] = 0;
82 theBlock->page[i].count[2] = 0;
83 theBlock->page[i].empty = 1;
89 int nandemul_CalcNBlocks(void)
102 sizeInMB = DEFAULT_SIZE_IN_MB;
104 return sizeInMB * 64;
109 static int CheckInit(void)
111 static int initialised = 0;
124 nBlocks = nandemul_CalcNBlocks();
126 ned.block = YMALLOC(sizeof(nandemul_Block *) * nBlocks);
128 if(!ned.block) return 0;
130 for(i=0; i <nBlocks; i++)
135 for(i=0; i <nBlocks && !fail; i++)
137 if((ned.block[i] = YMALLOC(sizeof(nandemul_Block))) == 0)
143 nandemul_ReallyEraseBlock(i);
144 ned.block[i]->damaged = 0;
151 for(i = 0; i < nAllocated; i++)
157 T(("Allocation failed, could only allocate %dMB of %dMB requested.\n",
158 nAllocated/64,sizeInMB));
162 ned.nBlocks = nBlocks;
169 int nandemul_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
177 __u8 *spareAsBytes = (__u8 *)spare;
182 blk = chunkInNAND/32;
188 x = ned.block[blk]->page[pg].data;
190 for(i = 0; i < 512; i++)
195 ned.block[blk]->page[pg].count[0]++;
196 ned.block[blk]->page[pg].count[1]++;
197 ned.block[blk]->page[pg].empty = 0;
203 x = &ned.block[blk]->page[pg].data[512];
205 for(i = 0; i < 16; i++)
207 x[i] &=spareAsBytes[i];
209 ned.block[blk]->page[pg].count[2]++;
216 int nandemul_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
224 blk = chunkInNAND/32;
230 memcpy(data,ned.block[blk]->page[pg].data,512);
236 memcpy(spare,&ned.block[blk]->page[pg].data[512],16);
243 int nandemul_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
252 blk = chunkInNAND/32;
256 for(i = 0; i < 528; i++)
258 if(ned.block[blk]->page[pg].data[i] != 0xFF)
268 int nandemul_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
273 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
275 T(("Attempt to erase non-existant block %d\n",blockNumber));
277 else if(ned.block[blockNumber]->damaged)
279 T(("Attempt to erase damaged block %d\n",blockNumber));
283 nandemul_ReallyEraseBlock(blockNumber);
289 int nandemul_InitialiseNAND(yaffs_Device *dev)
294 #endif //YAFFS_RAM_ENABLED