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)
81 if(n > 0) schedule_timeout(n);
87 static void nandemul_ReallyEraseBlock(int blockNumber)
93 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
98 blk = ned.block[blockNumber];
100 for(i = 0; i < PAGES_PER_BLOCK; i++)
102 memset(blk->page[i],0xff,sizeof(nandemul_Page));
103 blk->page[i]->empty = 1;
109 static int nandemul2k_CalcNBlocks(void)
111 return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
116 static int CheckInit(void)
118 static int initialised = 0;
133 ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
136 ned.block = malloc(sizeof(nandemul_Block*) * nBlocks );
138 if(!ned.block) return YAFFS_FAIL;
144 for(i=fail=0; i <nBlocks; i++)
149 if(!(blk = ned.block[i] = malloc(sizeof(nandemul_Block))))
155 for(j = 0; j < PAGES_PER_BLOCK; j++)
157 if((blk->page[j] = malloc(sizeof(nandemul_Page))) == 0)
162 nandemul_ReallyEraseBlock(i);
163 ned.block[i]->damaged = 0;
172 for(i = 0; i < nAllocated; i++)
178 yaffs_trace(YAFFS_TRACE_ALWAYS,
179 "Allocation failed, could only allocate %dMB of %dMB requested.\n",
180 nAllocated/64,sizeInMB);
184 ned.nBlocks = nBlocks;
191 int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
200 blk = nand_chunk/PAGES_PER_BLOCK;
201 pg = nand_chunk%PAGES_PER_BLOCK;
206 x = ned.block[blk]->page[pg]->data;
208 for(i = 0; i < PAGE_DATA_SIZE; i++)
213 ned.block[blk]->page[pg]->empty = 0;
219 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
221 yaffs_pack_tags2((struct yaffs_packed_tags2 *)x,tags, !dev->param.no_tags_ecc);
234 int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
243 blk = nand_chunk/PAGES_PER_BLOCK;
244 pg = nand_chunk%PAGES_PER_BLOCK;
249 memcpy(data,ned.block[blk]->page[pg]->data,PAGE_DATA_SIZE);
255 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
257 yaffs_unpack_tags2(tags,(struct yaffs_packed_tags2 *)x, !dev->param.no_tags_ecc);
264 static int nandemul2k_CheckChunkErased(struct yaffs_dev *dev,int nand_chunk)
272 blk = nand_chunk/PAGES_PER_BLOCK;
273 pg = nand_chunk%PAGES_PER_BLOCK;
276 for(i = 0; i < PAGE_TOTAL_SIZE; i++)
278 if(ned.block[blk]->page[pg]->data[i] != 0xFF)
288 int nandemul2k_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
292 if(blockNumber < 0 || blockNumber >= ned.nBlocks)
294 yaffs_trace(YAFFS_TRACE_ALWAYS,
295 "Attempt to erase non-existant block %d\n",
298 else if(ned.block[blockNumber]->damaged)
300 yaffs_trace(YAFFS_TRACE_ALWAYS,
301 "Attempt to erase damaged block %d\n",
306 nandemul_ReallyEraseBlock(blockNumber);
312 int nandemul2k_InitialiseNAND(struct yaffs_dev *dev)
318 int nandemul2k_MarkNANDBlockBad(struct yaffs_dev *dev, int block_no)
323 x = &ned.block[block_no]->page[0]->data[PAGE_DATA_SIZE];
325 memset(x,0,sizeof(struct yaffs_packed_tags2));
332 int nandemul2k_QueryNANDBlock(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32 *seq_number)
334 struct yaffs_ext_tags tags;
339 chunkNo = block_no * dev->param.chunks_per_block;
341 nandemul2k_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
344 *state = YAFFS_BLOCK_STATE_DEAD;
346 else if(!tags.chunk_used)
348 *state = YAFFS_BLOCK_STATE_EMPTY;
350 else if(tags.chunk_used)
352 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
353 *seq_number = tags.seq_number;
358 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
360 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
361 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
364 #endif //YAFFS_RAM_ENABLED