yaffsfs: Add commenting for OS glue interface
[yaffs2.git] / direct / test-framework / yaffs_nor_drv.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  * This is an interface module for handling NOR in yaffs1 mode.
16  */
17
18 /* This code is intended to be used with "regular" NOR that is bit-modifyable.
19  *
20  * Each "chunk" is a contguous area of 512 + 16 bytes.
21  * This makes 248 such chunks with some space left over where a format markerr
22  * is stored.
23  */
24
25 #include "yaffs_nor_drv.h"
26
27 #include "yportenv.h"
28 #include "yaffs_trace.h"
29
30 #include "yaffs_flashif.h"
31 #include "yaffs_guts.h"
32
33 /* Tunable data */
34 #define DATA_BYTES_PER_CHUNK    512
35 #define SPARE_BYTES_PER_CHUNK   16
36 #define BLOCK_SIZE_IN_BYTES     (128*1024)
37 #define BYTES_IN_DEVICE         (4*1024*1024)
38
39 #define BYTES_PER_CHUNK         (DATA_BYTES_PER_CHUNK + SPARE_BYTES_PER_CHUNK)
40 #define SPARE_AREA_OFFSET       DATA_BYTES_PER_CHUNK
41 #define CHUNKS_PER_BLOCK (BLOCK_SIZE_IN_BYTES/BYTES_PER_CHUNK)
42
43 #define BLOCKS_IN_DEVICE        (BYTES_IN_DEVICE/BLOCK_SIZE_IN_BYTES)
44
45 #define YNOR_PREMARKER          (0xF6)
46 #define YNOR_POSTMARKER         (0xF0)
47
48 #define FORMAT_OFFSET           (CHUNKS_PER_BLOCK * BYTES_PER_CHUNK)
49 #define FORMAT_VALUE            0x1234
50
51 #if 1
52
53 /* Compile this for a simulation */
54 #include "ynorsim.h"
55
56 static struct nor_sim *nor_sim;
57
58 #define nor_drv_FlashInit() do {nor_sim = ynorsim_initialise("emfile-nor", BLOCKS_IN_DEVICE, BLOCK_SIZE_IN_BYTES); } while(0)
59 #define nor_drv_FlashDeinit() ynorsim_shutdown(nor_sim)
60 #define nor_drv_FlashWrite32(addr,buf,nwords) ynorsim_wr32(nor_sim,addr,buf,nwords)
61 #define nor_drv_FlashRead32(addr,buf,nwords) ynorsim_rd32(nor_sim,addr,buf,nwords)
62 #define nor_drv_FlashEraseBlock(addr) ynorsim_erase(nor_sim,addr)
63 #define DEVICE_BASE     ynorsim_get_base(nor_sim)
64
65 #else
66
67 /* Compile this to hook up to read hardware */
68 #include "../blob/yflashrw.h"
69 #define nor_drv_FlashInit()  do{} while(0)
70 #define nor_drv_FlashDeinit() do {} while(0)
71 #define nor_drv_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
72 #define nor_drv_FlashRead32(addr,buf,nwords)  Y_FlashRead(addr,buf,nwords)
73 #define nor_drv_FlashEraseBlock(addr)         Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
74 #define DEVICE_BASE     (32 * 1024 * 1024)
75 #endif
76
77
78 static u32 *Block2Addr(struct yaffs_dev *dev, int blockNumber)
79 {
80         u8 *addr;
81
82         dev=dev;
83
84         addr = (u8*)DEVICE_BASE;
85         addr += blockNumber * BLOCK_SIZE_IN_BYTES;
86
87         return (u32 *) addr;
88 }
89
90 static u32 *Block2FormatAddr(struct yaffs_dev *dev, int blockNumber)
91 {
92         u8 *addr;
93
94         addr = (u8*) Block2Addr(dev,blockNumber);
95         addr += FORMAT_OFFSET;
96
97         return (u32 *)addr;
98 }
99
100 static u32 *Chunk2DataAddr(struct yaffs_dev *dev, int chunk_id)
101 {
102         unsigned block;
103         unsigned chunkInBlock;
104         u8 *addr;
105
106         block = chunk_id/dev->param.chunks_per_block;
107         chunkInBlock = chunk_id % dev->param.chunks_per_block;
108
109         addr = (u8*) Block2Addr(dev,block);
110         addr += chunkInBlock * BYTES_PER_CHUNK;
111
112         return (u32 *)addr;
113 }
114
115 static u32 *Chunk2SpareAddr(struct yaffs_dev *dev, int chunk_id)
116 {
117         u8 *addr;
118
119         addr = (u8*) Chunk2DataAddr(dev, chunk_id);
120         addr += SPARE_AREA_OFFSET;
121         return (u32 *)addr;
122 }
123
124
125 static void nor_drv_AndBytes(u8*target, const u8 *src, int nbytes)
126 {
127         while(nbytes > 0){
128                 *target &= *src;
129                 target++;
130                 src++;
131                 nbytes--;
132         }
133 }
134
135 static int nor_drv_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,
136                                     const u8 *data, int data_len,
137                                     const u8 *oob, int oob_len)
138 {
139         u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
140         u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
141
142         struct yaffs_spare *spare = (struct yaffs_spare *)oob;
143         struct yaffs_spare tmpSpare;
144
145         (void) oob_len;
146
147         /* We should only be getting called for one of 3 reasons:
148          * Writing a chunk: data and spare will not be NULL
149          * Writing a deletion marker: data will be NULL, spare not NULL
150          * Writing a bad block marker: data will be NULL, spare not NULL
151          */
152
153         if(sizeof(struct yaffs_spare) != SPARE_BYTES_PER_CHUNK)
154                 BUG();
155
156         if(data && oob) {
157                 if(spare->page_status != 0xff)
158                         BUG();
159                 /* Write a pre-marker */
160                 memset(&tmpSpare,0xff,sizeof(tmpSpare));
161                 tmpSpare.page_status = YNOR_PREMARKER;
162                 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
163
164                 /* Write the data */
165                 nor_drv_FlashWrite32(dataAddr,(u32 *)data, data_len/ sizeof(u32));
166
167                 memcpy(&tmpSpare,spare,sizeof(struct yaffs_spare));
168
169                 /* Write the real tags, but override the premarker*/
170                 tmpSpare.page_status = YNOR_PREMARKER;
171                 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
172
173                 /* Write a post-marker */
174                 tmpSpare.page_status = YNOR_POSTMARKER;
175                 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(tmpSpare)/sizeof(u32));
176
177         } else if(spare){
178                 /* This has to be a read-modify-write operation to handle NOR-ness */
179
180                 nor_drv_FlashRead32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
181
182                 nor_drv_AndBytes((u8 *)&tmpSpare,(u8 *)spare,sizeof(struct yaffs_spare));
183
184                 nor_drv_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/sizeof(u32));
185         } else {
186                 BUG();
187         }
188
189         return YAFFS_OK;
190 }
191
192 static int nor_drv_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk,
193                                         u8 *data, int data_len,
194                                         u8 *oob, int oob_len,
195                                         enum yaffs_ecc_result *ecc_result)
196 {
197         struct yaffs_spare *spare = (struct yaffs_spare *)oob;
198
199         u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
200         u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
201
202         if (data) {
203                 nor_drv_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / sizeof(u32));
204         }
205
206         if (oob) {
207                 nor_drv_FlashRead32(spareAddr,(u32 *)spare, oob_len/ sizeof(u32));
208
209                 /* If the page status is YNOR_POSTMARKER then it was written properly
210                  * so change that to 0xFF so that the rest of yaffs is happy.
211                  */
212                 if(spare->page_status == YNOR_POSTMARKER)
213                         spare->page_status = 0xff;
214                 else if(spare->page_status != 0xff &&
215                         (spare->page_status | YNOR_PREMARKER) != 0xff)
216                         spare->page_status = YNOR_PREMARKER;
217         }
218
219         if(ecc_result)
220                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
221
222         return YAFFS_OK;
223
224 }
225
226
227 static int nor_drv_FormatBlock(struct yaffs_dev *dev, int blockNumber)
228 {
229         u32 *blockAddr = Block2Addr(dev,blockNumber);
230         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
231         u32 formatValue = FORMAT_VALUE;
232
233         nor_drv_FlashEraseBlock(blockAddr);
234         nor_drv_FlashWrite32(formatAddr,&formatValue,1);
235
236         return YAFFS_OK;
237 }
238
239 static int nor_drv_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
240 {
241         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
242         u32 formatValue = 0;
243
244         nor_drv_FlashWrite32(formatAddr,&formatValue,1);
245
246         return YAFFS_OK;
247 }
248
249 static int nor_drv_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
250 {
251         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
252         u32 formatValue;
253
254
255         nor_drv_FlashRead32(formatAddr,&formatValue,1);
256
257         return (formatValue == FORMAT_VALUE);
258 }
259
260 static int nor_drv_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
261 {
262
263         if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
264         {
265                 yaffs_trace(YAFFS_TRACE_ALWAYS,
266                         "Attempt to erase non-existant block %d\n",
267                         blockNumber);
268                 return YAFFS_FAIL;
269         }
270         else
271         {
272                 nor_drv_UnformatBlock(dev,blockNumber);
273                 nor_drv_FormatBlock(dev,blockNumber);
274                 return YAFFS_OK;
275         }
276
277 }
278
279 static int nor_drv_InitialiseNAND(struct yaffs_dev *dev)
280 {
281         int i;
282
283         nor_drv_FlashInit();
284         /* Go through the blocks formatting them if they are not formatted */
285         for(i = dev->param.start_block; i <= dev->param.end_block; i++){
286                 if(!nor_drv_IsBlockFormatted(dev,i)){
287                         nor_drv_FormatBlock(dev,i);
288                 }
289         }
290         return YAFFS_OK;
291 }
292
293 static int nor_drv_Deinitialise_flash_fn(struct yaffs_dev *dev)
294 {
295         dev=dev;
296
297         nor_drv_FlashDeinit();
298
299         return YAFFS_OK;
300 }
301
302
303 struct yaffs_dev *yaffs_nor_install_drv(const char *name)
304 {
305
306         struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
307         char *name_copy = strdup(name);
308         struct yaffs_param *param;
309         struct yaffs_driver *drv;
310
311
312         if(!dev || !name_copy) {
313                 free(name_copy);
314                 free(dev);
315                 return NULL;
316         }
317
318         param = &dev->param;
319         drv = &dev->drv;
320
321         memset(dev, 0, sizeof(*dev));
322
323         param->name = name_copy;
324
325         param->total_bytes_per_chunk = DATA_BYTES_PER_CHUNK;
326         param->chunks_per_block = CHUNKS_PER_BLOCK;
327         param->n_reserved_blocks = 2;
328         param->start_block = 0; // Can use block 0
329         param->end_block = BLOCKS_IN_DEVICE - 1; // Last block
330         param->use_nand_ecc = 0; // use YAFFS's ECC
331
332         drv->drv_write_chunk_fn = nor_drv_WriteChunkToNAND;
333         drv->drv_read_chunk_fn = nor_drv_ReadChunkFromNAND;
334         drv->drv_erase_fn = nor_drv_EraseBlockInNAND;
335         drv->drv_initialise_fn = nor_drv_InitialiseNAND;
336         drv->drv_deinitialise_fn = nor_drv_Deinitialise_flash_fn;
337
338         param->n_caches = 10;
339         param->disable_soft_del = 1;
340
341         dev->driver_context = (void *) nor_sim;
342
343         yaffs_add_device(dev);
344
345         return NULL;
346 }