Compilation clean up
[yaffs2.git] / direct / test-framework / yaffs_ramem2k.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14 /*
15  * yaffs_ramem2k.c: RAM emulation in-kernel for 2K pages (YAFFS2)
16  */
17
18
19 #ifndef __KERNEL__
20 #define CONFIG_YAFFS_RAM_ENABLED
21 #else
22 #include <linux/config.h>
23 #endif
24
25 #ifdef CONFIG_YAFFS_RAM_ENABLED
26
27 #include "yportenv.h"
28 #include "yaffs_trace.h"
29
30 #include "yaffs_nandemul2k.h"
31 #include "yaffs_guts.h"
32 #include "yaffs_packedtags2.h"
33
34
35
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)
40
41
42
43 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
44
45 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
46
47 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
48
49 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
50
51
52 typedef struct
53 {
54         u8 data[PAGE_TOTAL_SIZE]; // Data + spare
55         int empty;      // is this empty?
56 } nandemul_Page;
57
58
59 typedef struct
60 {
61         nandemul_Page *page[PAGES_PER_BLOCK];
62         int damaged;
63 } nandemul_Block;
64
65
66
67 typedef struct
68 {
69         nandemul_Block**block;
70         int nBlocks;
71 } nandemul_Device;
72
73 static nandemul_Device ned;
74
75 static int sizeInMB = EM_SIZE_IN_MEG;
76
77
78 static void nandemul_yield(int n)
79 {
80         (void)n;
81 #ifdef __KERNEL__
82         if(n > 0) schedule_timeout(n);
83 #endif
84
85 }
86
87
88 static void nandemul_ReallyEraseBlock(int blockNumber)
89 {
90         int i;
91
92         nandemul_Block *blk;
93
94         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
95         {
96                 return;
97         }
98
99         blk = ned.block[blockNumber];
100
101         for(i = 0; i < PAGES_PER_BLOCK; i++)
102         {
103                 memset(blk->page[i],0xff,sizeof(nandemul_Page));
104                 blk->page[i]->empty = 1;
105         }
106         nandemul_yield(2);
107 }
108
109
110 static int nandemul2k_CalcNBlocks(void)
111 {
112         return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
113 }
114
115
116
117 static int  CheckInit(void)
118 {
119         static int initialised = 0;
120
121         int i,j;
122
123         int fail = 0;
124         int nBlocks;
125
126         int nAllocated = 0;
127
128         if(initialised)
129         {
130                 return YAFFS_OK;
131         }
132
133
134         ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
135
136
137         ned.block = malloc(sizeof(nandemul_Block*) * nBlocks );
138
139         if(!ned.block) return YAFFS_FAIL;
140
141
142
143
144
145         for(i=fail=0; i <nBlocks; i++)
146         {
147
148                 nandemul_Block *blk;
149
150                 if(!(blk = ned.block[i] = malloc(sizeof(nandemul_Block))))
151                 {
152                  fail = 1;
153                 }
154                 else
155                 {
156                         for(j = 0; j < PAGES_PER_BLOCK; j++)
157                         {
158                                 if((blk->page[j] = malloc(sizeof(nandemul_Page))) == 0)
159                                 {
160                                         fail = 1;
161                                 }
162                         }
163                         nandemul_ReallyEraseBlock(i);
164                         ned.block[i]->damaged = 0;
165                         nAllocated++;
166                 }
167         }
168
169         if(fail)
170         {
171                 //Todo thump pages
172
173                 for(i = 0; i < nAllocated; i++)
174                 {
175                         kfree(ned.block[i]);
176                 }
177                 kfree(ned.block);
178
179                 yaffs_trace(YAFFS_TRACE_ALWAYS,
180                         "Allocation failed, could only allocate %dMB of %dMB requested.\n",
181                         nAllocated/64,sizeInMB);
182                 return 0;
183         }
184
185         ned.nBlocks = nBlocks;
186
187         initialised = 1;
188
189         return 1;
190 }
191
192 int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
193 {
194         int blk;
195         int pg;
196         int i;
197
198         u8 *x;
199
200
201         blk = nand_chunk/PAGES_PER_BLOCK;
202         pg = nand_chunk%PAGES_PER_BLOCK;
203
204
205         if(data)
206         {
207                 x = ned.block[blk]->page[pg]->data;
208
209                 for(i = 0; i < PAGE_DATA_SIZE; i++)
210                 {
211                         x[i] &=data[i];
212                 }
213
214                 ned.block[blk]->page[pg]->empty = 0;
215         }
216
217
218         if(tags)
219         {
220                 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
221
222                 yaffs_pack_tags2(dev, (struct yaffs_packed_tags2 *)x,tags, !dev->param.no_tags_ecc);
223
224         }
225
226         if(tags || data)
227         {
228                 nandemul_yield(1);
229         }
230
231         return YAFFS_OK;
232 }
233
234
235 int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
236 {
237         int blk;
238         int pg;
239
240         u8 *x;
241
242
243
244         blk = nand_chunk/PAGES_PER_BLOCK;
245         pg = nand_chunk%PAGES_PER_BLOCK;
246
247
248         if(data)
249         {
250                 memcpy(data,ned.block[blk]->page[pg]->data,PAGE_DATA_SIZE);
251         }
252
253
254         if(tags)
255         {
256                 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
257
258                 yaffs_unpack_tags2(dev, tags,(struct yaffs_packed_tags2 *)x, !dev->param.no_tags_ecc);
259         }
260
261         return YAFFS_OK;
262 }
263
264 int nandemul2k_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
265 {
266         (void) dev;
267
268         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
269         {
270                 yaffs_trace(YAFFS_TRACE_ALWAYS,
271                         "Attempt to erase non-existant block %d\n",
272                         blockNumber);
273         }
274         else if(ned.block[blockNumber]->damaged)
275         {
276                 yaffs_trace(YAFFS_TRACE_ALWAYS,
277                         "Attempt to erase damaged block %d\n",
278                         blockNumber);
279         }
280         else
281         {
282                 nandemul_ReallyEraseBlock(blockNumber);
283         }
284
285         return YAFFS_OK;
286 }
287
288 int nandemul2k_InitialiseNAND(struct yaffs_dev *dev)
289 {
290         (void) dev;
291
292         CheckInit();
293         return YAFFS_OK;
294 }
295
296 int nandemul2k_MarkNANDBlockBad(struct yaffs_dev *dev, int block_no)
297 {
298
299         u8 *x;
300
301         (void) dev;
302
303         x = &ned.block[block_no]->page[0]->data[PAGE_DATA_SIZE];
304
305         memset(x,0,sizeof(struct yaffs_packed_tags2));
306
307
308         return YAFFS_OK;
309
310 }
311
312 int nandemul2k_QueryNANDBlock(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32  *seq_number)
313 {
314         struct yaffs_ext_tags tags;
315         int chunkNo;
316
317         *seq_number = 0;
318
319         chunkNo = block_no * dev->param.chunks_per_block;
320
321         nandemul2k_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
322         if(tags.block_bad)
323         {
324                 *state = YAFFS_BLOCK_STATE_DEAD;
325         }
326         else if(!tags.chunk_used)
327         {
328                 *state = YAFFS_BLOCK_STATE_EMPTY;
329         }
330         else if(tags.chunk_used)
331         {
332                 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
333                 *seq_number = tags.seq_number;
334         }
335         return YAFFS_OK;
336 }
337
338 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
339
340 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
341 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
342
343
344 #endif //YAFFS_RAM_ENABLED
345