2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * yaffs_ramem2k.c: RAM emulation in-kernel for 2K pages (YAFFS2)
19 const char *yaffs_ramem2k_c_version = "$Id: yaffs_ramem2k.c,v 1.8 2010-02-18 01:18:04 charles Exp $";
22 #define CONFIG_YAFFS_RAM_ENABLED
24 #include <linux/config.h>
27 #ifdef CONFIG_YAFFS_RAM_ENABLED
30 #include "yaffs_trace.h"
32 #include "yaffs_nandemul2k.h"
33 #include "yaffs_guts.h"
34 #include "yaffs_packedtags2.h"
38 #define EM_SIZE_IN_MEG (32)
39 #define PAGE_DATA_SIZE (2048)
40 #define PAGE_SPARE_SIZE (64)
41 #define PAGES_PER_BLOCK (64)
45 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
47 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
49 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
51 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
56 u8 data[PAGE_TOTAL_SIZE]; // Data + spare
57 int empty; // is this empty?
63 nandemul_Page *page[PAGES_PER_BLOCK];
71 nandemul_Block**block;
75 static nandemul_Device ned;
77 static int sizeInMB = EM_SIZE_IN_MEG;
80 static void nandemul_yield(int n)
83 if(n > 0) schedule_timeout(n);
89 static void nandemul_ReallyEraseBlock(int blockNumber)
95 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
100 blk = ned.block[blockNumber];
102 for(i = 0; i < PAGES_PER_BLOCK; i++)
104 memset(blk->page[i],0xff,sizeof(nandemul_Page));
105 blk->page[i]->empty = 1;
111 static int nandemul2k_CalcNBlocks(void)
113 return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
118 static int CheckInit(void)
120 static int initialised = 0;
135 ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
138 ned.block = malloc(sizeof(nandemul_Block*) * nBlocks );
140 if(!ned.block) return YAFFS_FAIL;
146 for(i=fail=0; i <nBlocks; i++)
151 if(!(blk = ned.block[i] = malloc(sizeof(nandemul_Block))))
157 for(j = 0; j < PAGES_PER_BLOCK; j++)
159 if((blk->page[j] = malloc(sizeof(nandemul_Page))) == 0)
164 nandemul_ReallyEraseBlock(i);
165 ned.block[i]->damaged = 0;
174 for(i = 0; i < nAllocated; i++)
180 yaffs_trace(YAFFS_TRACE_ALWAYS,
181 "Allocation failed, could only allocate %dMB of %dMB requested.\n",
182 nAllocated/64,sizeInMB);
186 ned.nBlocks = nBlocks;
193 int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
202 blk = nand_chunk/PAGES_PER_BLOCK;
203 pg = nand_chunk%PAGES_PER_BLOCK;
208 x = ned.block[blk]->page[pg]->data;
210 for(i = 0; i < PAGE_DATA_SIZE; i++)
215 ned.block[blk]->page[pg]->empty = 0;
221 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
223 yaffs_pack_tags2((struct yaffs_packed_tags2 *)x,tags, !dev->param.no_tags_ecc);
236 int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
245 blk = nand_chunk/PAGES_PER_BLOCK;
246 pg = nand_chunk%PAGES_PER_BLOCK;
251 memcpy(data,ned.block[blk]->page[pg]->data,PAGE_DATA_SIZE);
257 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
259 yaffs_unpack_tags2(tags,(struct yaffs_packed_tags2 *)x, !dev->param.no_tags_ecc);
266 static int nandemul2k_CheckChunkErased(struct yaffs_dev *dev,int nand_chunk)
274 blk = nand_chunk/PAGES_PER_BLOCK;
275 pg = nand_chunk%PAGES_PER_BLOCK;
278 for(i = 0; i < PAGE_TOTAL_SIZE; i++)
280 if(ned.block[blk]->page[pg]->data[i] != 0xFF)
290 int nandemul2k_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
294 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
296 yaffs_trace(YAFFS_TRACE_ALWAYS,
297 "Attempt to erase non-existant block %d\n",
300 else if(ned.block[blockNumber]->damaged)
302 yaffs_trace(YAFFS_TRACE_ALWAYS,
303 "Attempt to erase damaged block %d\n",
308 nandemul_ReallyEraseBlock(blockNumber);
314 int nandemul2k_InitialiseNAND(struct yaffs_dev *dev)
320 int nandemul2k_MarkNANDBlockBad(struct yaffs_dev *dev, int block_no)
325 x = &ned.block[block_no]->page[0]->data[PAGE_DATA_SIZE];
327 memset(x,0,sizeof(struct yaffs_packed_tags2));
334 int nandemul2k_QueryNANDBlock(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32 *seq_number)
336 struct yaffs_ext_tags tags;
341 chunkNo = block_no * dev->param.chunks_per_block;
343 nandemul2k_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
346 *state = YAFFS_BLOCK_STATE_DEAD;
348 else if(!tags.chunk_used)
350 *state = YAFFS_BLOCK_STATE_EMPTY;
352 else if(tags.chunk_used)
354 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
355 *seq_number = tags.seq_number;
360 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
362 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
363 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
366 #endif //YAFFS_RAM_ENABLED