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