2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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)
20 #define CONFIG_YAFFS_RAM_ENABLED
22 #include <linux/config.h>
25 #ifdef CONFIG_YAFFS_RAM_ENABLED
28 #include "yaffs_trace.h"
30 #include "yaffs_nandemul2k.h"
31 #include "yaffs_guts.h"
32 #include "yaffs_packedtags2.h"
36 #define EM_SIZE_IN_MEG (32)
37 #define PAGE_DATA_SIZE (2048)
38 #define PAGE_SPARE_SIZE (64)
39 #define PAGES_PER_BLOCK (64)
43 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
45 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
47 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
49 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
54 u8 data[PAGE_TOTAL_SIZE]; // Data + spare
55 int empty; // is this empty?
61 nandemul_Page *page[PAGES_PER_BLOCK];
69 nandemul_Block**block;
73 static nandemul_Device ned;
75 static int sizeInMB = EM_SIZE_IN_MEG;
78 static void nandemul_yield(int n)
82 if(n > 0) schedule_timeout(n);
88 static void nandemul_ReallyEraseBlock(int blockNumber)
94 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
99 blk = ned.block[blockNumber];
101 for(i = 0; i < PAGES_PER_BLOCK; i++)
103 memset(blk->page[i],0xff,sizeof(nandemul_Page));
104 blk->page[i]->empty = 1;
110 static int nandemul2k_CalcNBlocks(void)
112 return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
117 static int CheckInit(void)
119 static int initialised = 0;
134 ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
137 ned.block = malloc(sizeof(nandemul_Block*) * nBlocks );
139 if(!ned.block) return YAFFS_FAIL;
145 for(i=fail=0; i <nBlocks; i++)
150 if(!(blk = ned.block[i] = malloc(sizeof(nandemul_Block))))
156 for(j = 0; j < PAGES_PER_BLOCK; j++)
158 if((blk->page[j] = malloc(sizeof(nandemul_Page))) == 0)
163 nandemul_ReallyEraseBlock(i);
164 ned.block[i]->damaged = 0;
173 for(i = 0; i < nAllocated; i++)
179 yaffs_trace(YAFFS_TRACE_ALWAYS,
180 "Allocation failed, could only allocate %dMB of %dMB requested.\n",
181 nAllocated/64,sizeInMB);
185 ned.nBlocks = nBlocks;
192 int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
201 blk = nand_chunk/PAGES_PER_BLOCK;
202 pg = nand_chunk%PAGES_PER_BLOCK;
207 x = ned.block[blk]->page[pg]->data;
209 for(i = 0; i < PAGE_DATA_SIZE; i++)
214 ned.block[blk]->page[pg]->empty = 0;
220 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
222 yaffs_pack_tags2(dev, (struct yaffs_packed_tags2 *)x,tags, !dev->param.no_tags_ecc);
235 int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
244 blk = nand_chunk/PAGES_PER_BLOCK;
245 pg = nand_chunk%PAGES_PER_BLOCK;
250 memcpy(data,ned.block[blk]->page[pg]->data,PAGE_DATA_SIZE);
256 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
258 yaffs_unpack_tags2(dev, tags,(struct yaffs_packed_tags2 *)x, !dev->param.no_tags_ecc);
264 int nandemul2k_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
268 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
270 yaffs_trace(YAFFS_TRACE_ALWAYS,
271 "Attempt to erase non-existant block %d\n",
274 else if(ned.block[blockNumber]->damaged)
276 yaffs_trace(YAFFS_TRACE_ALWAYS,
277 "Attempt to erase damaged block %d\n",
282 nandemul_ReallyEraseBlock(blockNumber);
288 int nandemul2k_InitialiseNAND(struct yaffs_dev *dev)
296 int nandemul2k_MarkNANDBlockBad(struct yaffs_dev *dev, int block_no)
303 x = &ned.block[block_no]->page[0]->data[PAGE_DATA_SIZE];
305 memset(x,0,sizeof(struct yaffs_packed_tags2));
312 int nandemul2k_QueryNANDBlock(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32 *seq_number)
314 struct yaffs_ext_tags tags;
319 chunkNo = block_no * dev->param.chunks_per_block;
321 nandemul2k_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
324 *state = YAFFS_BLOCK_STATE_DEAD;
326 else if(!tags.chunk_used)
328 *state = YAFFS_BLOCK_STATE_EMPTY;
330 else if(tags.chunk_used)
332 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
333 *seq_number = tags.seq_number;
338 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
340 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
341 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
344 #endif //YAFFS_RAM_ENABLED