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