767dceae262b7c8b923cff943bae8815311917c6
[yaffs2.git] / direct / yaffs_ramem2k.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 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.5 2009-10-14 00:01:57 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
31 #include "yaffs_nandemul2k.h"
32 #include "yaffs_guts.h"
33 #include "yaffsinterface.h"
34 #include "devextras.h"
35 #include "yaffs_packedtags2.h"
36
37
38
39 #define EM_SIZE_IN_MEG (32)
40 #define PAGE_DATA_SIZE  (2048)
41 #define PAGE_SPARE_SIZE (64)
42 #define PAGES_PER_BLOCK (64)
43
44
45
46 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
47
48 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
49
50 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
51
52 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
53
54
55 typedef struct 
56 {
57         __u8 data[PAGE_TOTAL_SIZE]; // Data + spare
58         int empty;      // is this empty?
59 } nandemul_Page;
60
61
62 typedef struct
63 {
64         nandemul_Page *page[PAGES_PER_BLOCK];
65         int damaged;    
66 } nandemul_Block;
67
68
69
70 typedef struct
71 {
72         nandemul_Block**block;
73         int nBlocks;
74 } nandemul_Device;
75
76 static nandemul_Device ned;
77
78 static int sizeInMB = EM_SIZE_IN_MEG;
79
80
81 static void nandemul_yield(int n)
82 {
83 #ifdef __KERNEL__
84         if(n > 0) schedule_timeout(n);
85 #endif
86
87 }
88
89
90 static void nandemul_ReallyEraseBlock(int blockNumber)
91 {
92         int i;
93         
94         nandemul_Block *blk;
95         
96         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
97         {
98                 return;
99         }
100         
101         blk = ned.block[blockNumber];
102         
103         for(i = 0; i < PAGES_PER_BLOCK; i++)
104         {
105                 memset(blk->page[i],0xff,sizeof(nandemul_Page));
106                 blk->page[i]->empty = 1;
107         }
108         nandemul_yield(2);
109 }
110
111
112 static int nandemul2k_CalcNBlocks(void)
113 {
114         return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
115 }
116
117
118
119 static int  CheckInit(void)
120 {
121         static int initialised = 0;
122         
123         int i,j;
124         
125         int fail = 0;
126         int nBlocks; 
127
128         int nAllocated = 0;
129         
130         if(initialised) 
131         {
132                 return YAFFS_OK;
133         }
134         
135         
136         ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
137
138         
139         ned.block = YMALLOC(sizeof(nandemul_Block*) * nBlocks );
140         
141         if(!ned.block) return YAFFS_FAIL;
142         
143         
144         
145
146                 
147         for(i=fail=0; i <nBlocks; i++)
148         {
149                 
150                 nandemul_Block *blk;
151                 
152                 if(!(blk = ned.block[i] = YMALLOC(sizeof(nandemul_Block))))
153                 {
154                  fail = 1;
155                 }  
156                 else
157                 {
158                         for(j = 0; j < PAGES_PER_BLOCK; j++)
159                         {
160                                 if((blk->page[j] = YMALLOC(sizeof(nandemul_Page))) == 0)
161                                 {
162                                         fail = 1;
163                                 }
164                         }
165                         nandemul_ReallyEraseBlock(i);
166                         ned.block[i]->damaged = 0;
167                         nAllocated++;
168                 }
169         }
170         
171         if(fail)
172         {
173                 //Todo thump pages
174                 
175                 for(i = 0; i < nAllocated; i++)
176                 {
177                         YFREE(ned.block[i]);
178                 }
179                 YFREE(ned.block);
180                 
181                 T(YAFFS_TRACE_ALWAYS,("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(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
194 {
195         int blk;
196         int pg;
197         int i;
198         
199         __u8 *x;
200
201         
202         blk = chunkInNAND/PAGES_PER_BLOCK;
203         pg = chunkInNAND%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_PackTags2(dev,(yaffs_PackedTags2 *)x,tags);
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(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
237 {
238         int blk;
239         int pg;
240         
241         __u8 *x;
242
243         
244         
245         blk = chunkInNAND/PAGES_PER_BLOCK;
246         pg = chunkInNAND%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_UnpackTags2(dev,tags,(yaffs_PackedTags2 *)x);
260         }
261
262         return YAFFS_OK;
263 }
264
265
266 static int nandemul2k_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
267 {
268         int blk;
269         int pg;
270         int i;
271
272         
273         
274         blk = chunkInNAND/PAGES_PER_BLOCK;
275         pg = chunkInNAND%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(yaffs_Device *dev, int blockNumber)
291 {
292         
293         
294         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
295         {
296                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
297         }
298         else if(ned.block[blockNumber]->damaged)
299         {
300                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase damaged block %d\n",blockNumber));
301         }
302         else
303         {
304                 nandemul_ReallyEraseBlock(blockNumber);
305         }
306         
307         return YAFFS_OK;
308 }
309
310 int nandemul2k_InitialiseNAND(yaffs_Device *dev)
311 {
312         CheckInit();
313         return YAFFS_OK;
314 }
315  
316 int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
317 {
318         
319         __u8 *x;
320         
321         x = &ned.block[blockNo]->page[0]->data[PAGE_DATA_SIZE];
322         
323         memset(x,0,sizeof(yaffs_PackedTags2));
324         
325         
326         return YAFFS_OK;
327         
328 }
329
330 int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32  *sequenceNumber)
331 {
332         yaffs_ExtendedTags tags;
333         int chunkNo;
334
335         *sequenceNumber = 0;
336         
337         chunkNo = blockNo * dev->nChunksPerBlock;
338         
339         nandemul2k_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
340         if(tags.blockBad)
341         {
342                 *state = YAFFS_BLOCK_STATE_DEAD;
343         }
344         else if(!tags.chunkUsed)
345         {
346                 *state = YAFFS_BLOCK_STATE_EMPTY;
347         }
348         else if(tags.chunkUsed)
349         {
350                 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
351                 *sequenceNumber = tags.sequenceNumber;
352         }
353         return YAFFS_OK;
354 }
355
356 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
357
358 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
359 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
360
361
362 #endif //YAFFS_RAM_ENABLED
363