65faca1255597e4afd2eb67445a849fa5f942271
[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         (void) data_len;
203
204         if (data) {
205                 nor_drv_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / sizeof(u32));
206         }
207
208         if (oob) {
209                 nor_drv_FlashRead32(spareAddr,(u32 *)spare, oob_len/ sizeof(u32));
210
211                 /* If the page status is YNOR_POSTMARKER then it was written properly
212                  * so change that to 0xFF so that the rest of yaffs is happy.
213                  */
214                 if(spare->page_status == YNOR_POSTMARKER)
215                         spare->page_status = 0xff;
216                 else if(spare->page_status != 0xff &&
217                         (spare->page_status | YNOR_PREMARKER) != 0xff)
218                         spare->page_status = YNOR_PREMARKER;
219         }
220
221         if(ecc_result)
222                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
223
224         return YAFFS_OK;
225
226 }
227
228
229 static int nor_drv_FormatBlock(struct yaffs_dev *dev, int blockNumber)
230 {
231         u32 *blockAddr = Block2Addr(dev,blockNumber);
232         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
233         u32 formatValue = FORMAT_VALUE;
234
235         nor_drv_FlashEraseBlock(blockAddr);
236         nor_drv_FlashWrite32(formatAddr,&formatValue,1);
237
238         return YAFFS_OK;
239 }
240
241 static int nor_drv_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
242 {
243         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
244         u32 formatValue = 0;
245
246         nor_drv_FlashWrite32(formatAddr,&formatValue,1);
247
248         return YAFFS_OK;
249 }
250
251 static int nor_drv_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
252 {
253         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
254         u32 formatValue;
255
256
257         nor_drv_FlashRead32(formatAddr,&formatValue,1);
258
259         return (formatValue == FORMAT_VALUE);
260 }
261
262 static int nor_drv_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
263 {
264
265         if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
266         {
267                 yaffs_trace(YAFFS_TRACE_ALWAYS,
268                         "Attempt to erase non-existant block %d\n",
269                         blockNumber);
270                 return YAFFS_FAIL;
271         }
272         else
273         {
274                 nor_drv_UnformatBlock(dev,blockNumber);
275                 nor_drv_FormatBlock(dev,blockNumber);
276                 return YAFFS_OK;
277         }
278
279 }
280
281 static int nor_drv_InitialiseNAND(struct yaffs_dev *dev)
282 {
283         u32 i;
284
285         nor_drv_FlashInit();
286         /* Go through the blocks formatting them if they are not formatted */
287         for(i = dev->param.start_block; i <= dev->param.end_block; i++){
288                 if(!nor_drv_IsBlockFormatted(dev,i)){
289                         nor_drv_FormatBlock(dev,i);
290                 }
291         }
292         return YAFFS_OK;
293 }
294
295 static int nor_drv_Deinitialise_flash_fn(struct yaffs_dev *dev)
296 {
297         dev=dev;
298
299         nor_drv_FlashDeinit();
300
301         return YAFFS_OK;
302 }
303
304
305 struct yaffs_dev *yaffs_nor_install_drv(const char *name)
306 {
307
308         struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
309         char *name_copy = strdup(name);
310         struct yaffs_param *param;
311         struct yaffs_driver *drv;
312
313
314         if(!dev || !name_copy) {
315                 free(name_copy);
316                 free(dev);
317                 return NULL;
318         }
319
320         param = &dev->param;
321         drv = &dev->drv;
322
323         memset(dev, 0, sizeof(*dev));
324
325         param->name = name_copy;
326
327         param->total_bytes_per_chunk = DATA_BYTES_PER_CHUNK;
328         param->chunks_per_block = CHUNKS_PER_BLOCK;
329         param->n_reserved_blocks = 2;
330         param->start_block = 0; // Can use block 0
331         param->end_block = BLOCKS_IN_DEVICE - 1; // Last block
332         param->use_nand_ecc = 0; // use YAFFS's ECC
333         param->disable_soft_del = 1;
334
335         drv->drv_write_chunk_fn = nor_drv_WriteChunkToNAND;
336         drv->drv_read_chunk_fn = nor_drv_ReadChunkFromNAND;
337         drv->drv_erase_fn = nor_drv_EraseBlockInNAND;
338         drv->drv_initialise_fn = nor_drv_InitialiseNAND;
339         drv->drv_deinitialise_fn = nor_drv_Deinitialise_flash_fn;
340
341         param->n_caches = 10;
342         param->disable_soft_del = 1;
343
344         dev->driver_context = (void *) nor_sim;
345
346         yaffs_add_device(dev);
347
348         return NULL;
349 }