20511e45e85c973d35bc86882a917f964f159431
[yaffs2.git] / direct / basic-test / yaffs_norif1.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 /* First set up for M18 with 1k chunks and 16-byte spares.
19  *
20  * NB We're using the oddball M18 modes of operation here
21  * The chip is 64MB based at 0x0000, but YAFFS only going to use the top half
22  * ie. YAFFS will be from 32MB to 64MB.
23  *
24  * The M18 has two ways of writing data. Every Programming Region (1kbytes)
25  * can be programmed in two modes:
26  * * Object Mode 1024 bytes of write once data.
27  * * Control Mode: 512bytes of bit-writeable data.
28  *    This is arranged as 32 * (16 bytes of bit-writable followed by 16 bytes of "dont touch")
29  *
30  * The block size is 256kB, making 128 blocks in the 32MB YAFFS area.
31  * Each block comprises:
32  *   Offset   0k: 248 x 1k data pages
33  *   Offset 248k: 248 x 32-byte spare areas implemented as 16 bytes of spare followed by 16 bytes untouched)
34  *   Offset 248k + (248 * 32): Format marker
35  *
36  */
37
38 const char *yaffs_norif1_c_version = "$Id: yaffs_norif1.c,v 1.6 2010-02-18 01:18:04 charles Exp $";
39
40 #include "yaffs_norif1.h"
41
42 #include "yportenv.h"
43 #include "yaffs_trace.h"
44
45 #include "yaffs_flashif.h"
46 #include "yaffs_guts.h"
47
48 #define SPARE_BYTES_PER_CHUNK   16
49 #define M18_SKIP                16
50 #define PROG_REGION_SIZE        1024
51 #define BLOCK_SIZE_IN_BYTES     (256*1024)
52 #define CHUNKS_PER_BLOCK        248
53 #define SPARE_AREA_OFFSET       (CHUNKS_PER_BLOCK * PROG_REGION_SIZE)
54
55 #define FORMAT_OFFSET           (SPARE_AREA_OFFSET + CHUNKS_PER_BLOCK * (SPARE_BYTES_PER_CHUNK + M18_SKIP))
56
57 #define FORMAT_VALUE            0x1234
58
59 #define DATA_BYTES_PER_CHUNK    1024
60 #define BLOCKS_IN_DEVICE        (8*1024/256)
61
62
63 #define YNOR_PREMARKER          (0xF6)
64 #define YNOR_POSTMARKER         (0xF0)
65
66
67 #if 1
68
69 /* Compile this for a simulation */
70 #include "ynorsim.h"
71 #define ynorif1_FlashInit() ynorsim_initialise()
72 #define ynorif1_FlashDeinit() ynorsim_shutdown()
73 #define ynorif1_FlashWrite32(addr,buf,nwords) ynorsim_wr32(addr,buf,nwords)
74 #define ynorif1_FlashRead32(addr,buf,nwords) ynorsim_rd32(addr,buf,nwords)
75 #define ynorif1_FlashEraseBlock(addr) ynorsim_erase(addr)
76 #define DEVICE_BASE     ynorsim_get_base()
77 #else
78
79 /* Compile this for running on blob, hacked for yaffs access */
80 #include "../blob/yflashrw.h"
81 #define ynorif1_FlashInit()  do{} while(0)
82 #define ynorif1_FlashDeinit() do {} while(0)
83 #define ynorif1_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords)
84 #define ynorif1_FlashRead32(addr,buf,nwords)  Y_FlashRead(addr,buf,nwords)
85 #define ynorif1_FlashEraseBlock(addr)         Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
86 #define DEVICE_BASE     (32 * 1024 * 1024)
87 #endif
88
89 static u32 *Block2Addr(struct yaffs_dev *dev, int blockNumber)
90 {
91         u32 addr;
92         dev=dev;
93
94         addr = (u32) DEVICE_BASE;
95         addr += blockNumber * BLOCK_SIZE_IN_BYTES;
96
97         return (u32 *) addr;
98 }
99
100 static u32 *Block2FormatAddr(struct yaffs_dev *dev,int blockNumber)
101 {
102         u32 addr;
103
104         addr = (u32) Block2Addr(dev,blockNumber);
105         addr += FORMAT_OFFSET;
106
107         return (u32 *)addr;
108 }
109
110 static u32 *Chunk2DataAddr(struct yaffs_dev *dev,int chunk_id)
111 {
112         unsigned block;
113         unsigned chunkInBlock;
114         u32  addr;
115
116         block = chunk_id/dev->param.chunks_per_block;
117         chunkInBlock = chunk_id % dev->param.chunks_per_block;
118
119         addr = (u32) Block2Addr(dev,block);
120         addr += chunkInBlock * DATA_BYTES_PER_CHUNK;
121
122         return (u32 *)addr;
123 }
124
125 static u32 *Chunk2SpareAddr(struct yaffs_dev *dev,int chunk_id)
126 {
127         unsigned block;
128         unsigned chunkInBlock;
129         u32 addr;
130
131         block = chunk_id/dev->param.chunks_per_block;
132         chunkInBlock = chunk_id % dev->param.chunks_per_block;
133
134         addr = (u32) Block2Addr(dev,block);
135         addr += SPARE_AREA_OFFSET;
136         addr += chunkInBlock * (SPARE_BYTES_PER_CHUNK + M18_SKIP);
137         return (u32 *)addr;
138 }
139
140
141 static void ynorif1_AndBytes(u8*target, const u8   *src, int nbytes)
142 {
143         while(nbytes > 0){
144                 *target &= *src;
145                 target++;
146                 src++;
147                 nbytes--;
148         }
149 }
150         int (*drv_write_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
151                                    const u8 *data, int data_len,
152                                    const u8 *oob, int oob_len);
153 static int ynorif1_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,
154                                     const u8 *data, int data_len,
155                                     const u8 *oob, int oob_len)
156 {
157         u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
158         u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
159
160         struct yaffs_spare *spare = (struct yaffs_spare *)oob;
161         struct yaffs_spare tmpSpare;
162
163         (void) oob_len;
164
165         /* We should only be getting called for one of 3 reasons:
166          * Writing a chunk: data and spare will not be NULL
167          * Writing a deletion marker: data will be NULL, spare not NULL
168          * Writing a bad block marker: data will be NULL, spare not NULL
169          */
170
171         if(sizeof(struct yaffs_spare) != 16)
172                 BUG();
173
174         if(data && oob)
175         {
176                 if(spare->page_status != 0xff)
177                         BUG();
178                 /* Write a pre-marker */
179                 memset(&tmpSpare,0xff,sizeof(tmpSpare));
180                 tmpSpare.page_status = YNOR_PREMARKER;
181                 ynorif1_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/4);
182
183                 /* Write the data */
184                 ynorif1_FlashWrite32(dataAddr,(u32 *)data, data_len/ 4);
185
186
187                 memcpy(&tmpSpare,spare,sizeof(struct yaffs_spare));
188
189                 /* Write the real tags, but override the premarker*/
190                 tmpSpare.page_status = YNOR_PREMARKER;
191                 ynorif1_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(struct yaffs_spare)/4);
192
193                 /* Write a post-marker */
194                 tmpSpare.page_status = YNOR_POSTMARKER;
195                 ynorif1_FlashWrite32(spareAddr,(u32 *)&tmpSpare,sizeof(tmpSpare)/4);
196
197         } else if(spare){
198                 /* This has to be a read-modify-write operation to handle NOR-ness */
199
200                 ynorif1_FlashRead32(spareAddr,(u32 *)&tmpSpare,16/ 4);
201
202                 ynorif1_AndBytes((u8 *)&tmpSpare,(u8 *)spare,sizeof(struct yaffs_spare));
203
204                 ynorif1_FlashWrite32(spareAddr,(u32 *)&tmpSpare,16/ 4);
205         }
206         else {
207                 BUG();
208         }
209
210
211         return YAFFS_OK;
212
213 }
214
215 static int ynorif1_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk,
216                                         u8 *data, int data_len,
217                                         u8 *oob, int oob_len,
218                                         enum yaffs_ecc_result *ecc_result)
219 {
220         struct yaffs_spare *spare = (struct yaffs_spare *)oob;
221
222         u32 *dataAddr = Chunk2DataAddr(dev,nand_chunk);
223         u32 *spareAddr = Chunk2SpareAddr(dev,nand_chunk);
224
225         if(data)
226         {
227                 ynorif1_FlashRead32(dataAddr,(u32 *)data,dev->param.total_bytes_per_chunk / 4);
228         }
229
230         if(oob)
231         {
232                 ynorif1_FlashRead32(spareAddr,(u32 *)spare, oob_len/ 4);
233
234                 /* If the page status is YNOR_POSTMARKER then it was written properly
235                  * so change that to 0xFF so that the rest of yaffs is happy.
236                  */
237                 if(spare->page_status == YNOR_POSTMARKER)
238                         spare->page_status = 0xFF;
239                 else if(spare->page_status != 0xff &&
240                         (spare->page_status | YNOR_PREMARKER) != 0xff)
241                         spare->page_status = YNOR_PREMARKER;
242         }
243
244         if(ecc_result)
245                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
246
247         return YAFFS_OK;
248
249 }
250
251
252 static int ynorif1_FormatBlock(struct yaffs_dev *dev, int blockNumber)
253 {
254         u32 *blockAddr = Block2Addr(dev,blockNumber);
255         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
256         u32 formatValue = FORMAT_VALUE;
257
258         ynorif1_FlashEraseBlock(blockAddr);
259         ynorif1_FlashWrite32(formatAddr,&formatValue,1);
260
261         return YAFFS_OK;
262 }
263
264 static int ynorif1_UnformatBlock(struct yaffs_dev *dev, int blockNumber)
265 {
266         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
267         u32 formatValue = 0;
268
269         ynorif1_FlashWrite32(formatAddr,&formatValue,1);
270
271         return YAFFS_OK;
272 }
273
274 static int ynorif1_IsBlockFormatted(struct yaffs_dev *dev, int blockNumber)
275 {
276         u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
277         u32 formatValue;
278
279
280         ynorif1_FlashRead32(formatAddr,&formatValue,1);
281
282         return (formatValue == FORMAT_VALUE);
283 }
284
285 static int ynorif1_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
286 {
287
288         if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
289         {
290                 yaffs_trace(YAFFS_TRACE_ALWAYS,
291                         "Attempt to erase non-existant block %d\n",
292                         blockNumber);
293                 return YAFFS_FAIL;
294         }
295         else
296         {
297                 ynorif1_UnformatBlock(dev,blockNumber);
298                 ynorif1_FormatBlock(dev,blockNumber);
299                 return YAFFS_OK;
300         }
301
302 }
303
304 static int ynorif1_InitialiseNAND(struct yaffs_dev *dev)
305 {
306         int i;
307
308         ynorif1_FlashInit();
309         /* Go through the blocks formatting them if they are not formatted */
310         for(i = dev->param.start_block; i <= dev->param.end_block; i++){
311                 if(!ynorif1_IsBlockFormatted(dev,i)){
312                         ynorif1_FormatBlock(dev,i);
313                 }
314         }
315         return YAFFS_OK;
316 }
317
318 static int ynorif1_Deinitialise_flash_fn(struct yaffs_dev *dev)
319 {
320         dev=dev;
321         ynorif1_FlashDeinit();
322
323         return YAFFS_OK;
324 }
325
326 void ynorif1_install_drv(struct yaffs_dev *dev)
327 {
328         struct yaffs_param *param = &dev->param;
329
330         param->total_bytes_per_chunk = 1024;
331         param->chunks_per_block =248;
332         param->n_reserved_blocks = 2;
333         param->start_block = 0; // Can use block 0
334         param->end_block = 31; // Last block
335         param->use_nand_ecc = 0; // use YAFFS's ECC
336         param->drv_write_chunk_fn = ynorif1_WriteChunkToNAND;
337         param->drv_read_chunk_fn = ynorif1_ReadChunkFromNAND;
338         param->drv_erase_fn = ynorif1_EraseBlockInNAND;
339         param->drv_initialise_fn = ynorif1_InitialiseNAND;
340         param->drv_deinitialise_fn = ynorif1_Deinitialise_flash_fn;
341 }
342