06bea3dc7218b2aa5be273faa7185bd0400ed410
[yaffs2.git] / direct / yaffs_norif1.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  * 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.4 2009-01-09 02:54:14 charles Exp $";
39
40 #include "yaffs_norif1.h"
41
42 #include "yportenv.h"
43
44 #include "yaffs_flashif.h"
45 #include "yaffs_guts.h"
46
47 #include "devextras.h"
48
49 #define SPARE_BYTES_PER_CHUNK   16
50 #define M18_SKIP                16
51 #define PROG_REGION_SIZE        1024
52 #define BLOCK_SIZE_IN_BYTES     (256*1024)
53 #define CHUNKS_PER_BLOCK        248
54 #define SPARE_AREA_OFFSET       (CHUNKS_PER_BLOCK * PROG_REGION_SIZE)
55
56 #define FORMAT_OFFSET           (SPARE_AREA_OFFSET + CHUNKS_PER_BLOCK * (SPARE_BYTES_PER_CHUNK + M18_SKIP))
57
58 #define FORMAT_VALUE            0x1234
59
60 #define DATA_BYTES_PER_CHUNK    1024
61 #define BLOCKS_IN_DEVICE        (8*1024/256)
62
63
64 #define YNOR_PREMARKER          (0xF6)
65 #define YNOR_POSTMARKER         (0xF0)
66
67
68 #if 1
69
70 /* Compile this for a simulation */
71 #include "ynorsim.h"
72 #define ynorif1_FlashInit() ynorsim_Initialise()
73 #define ynorif1_FlashDeinit() ynorsim_Shutdown()
74 #define ynorif1_FlashWrite32(addr,buf,nwords) ynorsim_Write32(addr,buf,nwords) 
75 #define ynorif1_FlashRead32(addr,buf,nwords) ynorsim_Read32(addr,buf,nwords) 
76 #define ynorif1_FlashEraseBlock(addr) ynorsim_EraseBlock(addr)
77 #define DEVICE_BASE     ynorsim_GetBase()
78 #else
79
80 /* Compile this for running on blob, hacked for yaffs access */
81 #include "../blob/yflashrw.h"
82 #define ynorif1_FlashInit()  do{} while(0)
83 #define ynorif1_FlashDeinit() do {} while(0)
84 #define ynorif1_FlashWrite32(addr,buf,nwords) Y_FlashWrite(addr,buf,nwords) 
85 #define ynorif1_FlashRead32(addr,buf,nwords)  Y_FlashRead(addr,buf,nwords) 
86 #define ynorif1_FlashEraseBlock(addr)         Y_FlashErase(addr,BLOCK_SIZE_IN_BYTES)
87 #define DEVICE_BASE     (32 * 1024 * 1024)
88 #endif
89
90 __u32 *Block2Addr(yaffs_Device *dev, int blockNumber)
91 {
92         __u32 addr;
93         
94         addr = (__u32) DEVICE_BASE;
95         addr += blockNumber * BLOCK_SIZE_IN_BYTES;
96         
97         return (__u32 *) addr;
98 }
99
100 __u32 *Block2FormatAddr(yaffs_Device *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 __u32 *Chunk2DataAddr(yaffs_Device *dev,int chunkId)
110 {
111         unsigned block;
112         unsigned chunkInBlock;
113         __u32  addr;
114         
115         block = chunkId/dev->nChunksPerBlock;
116         chunkInBlock = chunkId % dev->nChunksPerBlock;
117         
118         addr = (__u32) Block2Addr(dev,block);
119         addr += chunkInBlock * DATA_BYTES_PER_CHUNK;
120         
121         return (__u32 *)addr;
122 }
123
124 __u32 *Chunk2SpareAddr(yaffs_Device *dev,int chunkId)
125 {
126         unsigned block;
127         unsigned chunkInBlock;
128         __u32 addr;
129         
130         block = chunkId/dev->nChunksPerBlock;
131         chunkInBlock = chunkId % dev->nChunksPerBlock;
132         
133         addr = (__u32) Block2Addr(dev,block);
134         addr += SPARE_AREA_OFFSET;
135         addr += chunkInBlock * (SPARE_BYTES_PER_CHUNK + M18_SKIP);
136         return (__u32 *)addr;
137 }
138
139
140 void ynorif1_AndBytes(__u8*target, const __u8   *src, int nbytes)
141 {
142         while(nbytes > 0){
143                 *target &= *src;
144                 target++;
145                 src++;
146                 nbytes--;
147         }
148 }
149
150 int ynorif1_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
151 {
152         __u32 *dataAddr = Chunk2DataAddr(dev,chunkInNAND);
153         __u32 *spareAddr = Chunk2SpareAddr(dev,chunkInNAND);
154         
155         yaffs_Spare tmpSpare;
156         
157         /* We should only be getting called for one of 3 reasons:
158          * Writing a chunk: data and spare will not be NULL
159          * Writing a deletion marker: data will be NULL, spare not NULL
160          * Writing a bad block marker: data will be NULL, spare not NULL
161          */
162          
163         if(sizeof(yaffs_Spare) != 16)
164                 YBUG();
165         
166         if(data && spare)
167         {
168                 if(spare->pageStatus != 0xff)
169                         YBUG();
170                 /* Write a pre-marker */
171                 memset(&tmpSpare,0xff,sizeof(tmpSpare));
172                 tmpSpare.pageStatus = YNOR_PREMARKER;
173                 ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(yaffs_Spare)/4);
174
175                 /* Write the data */            
176                 ynorif1_FlashWrite32(dataAddr,(__u32 *)data,dev->totalBytesPerChunk / 4);
177                 
178                 
179                 memcpy(&tmpSpare,spare,sizeof(yaffs_Spare));
180                 
181                 /* Write the real tags, but override the premarker*/
182                 tmpSpare.pageStatus = YNOR_PREMARKER;
183                 ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(yaffs_Spare)/4);
184                 
185                 /* Write a post-marker */
186                 tmpSpare.pageStatus = YNOR_POSTMARKER;
187                 ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,sizeof(tmpSpare)/4);  
188
189         } else if(spare){
190                 /* This has to be a read-modify-write operation to handle NOR-ness */
191
192                 ynorif1_FlashRead32(spareAddr,(__u32 *)&tmpSpare,16/ 4);
193                 
194                 ynorif1_AndBytes((__u8 *)&tmpSpare,(__u8 *)spare,sizeof(yaffs_Spare));
195                 
196                 ynorif1_FlashWrite32(spareAddr,(__u32 *)&tmpSpare,16/ 4);
197         }
198         else {
199                 YBUG();
200         }
201         
202
203         return YAFFS_OK;        
204
205 }
206
207 int ynorif1_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
208 {
209
210         __u32 *dataAddr = Chunk2DataAddr(dev,chunkInNAND);
211         __u32 *spareAddr = Chunk2SpareAddr(dev,chunkInNAND);
212         
213         if(data)
214         {
215                 ynorif1_FlashRead32(dataAddr,(__u32 *)data,dev->totalBytesPerChunk / 4);
216         }
217         
218         if(spare)
219         {
220                 ynorif1_FlashRead32(spareAddr,(__u32 *)spare,16/ 4);
221                 
222                 /* If the page status is YNOR_POSTMARKER then it was written properly
223                  * so change that to 0xFF so that the rest of yaffs is happy.
224                  */
225                 if(spare->pageStatus == YNOR_POSTMARKER)
226                         spare->pageStatus = 0xFF;
227                 else if(spare->pageStatus != 0xff &&
228                         (spare->pageStatus | YNOR_PREMARKER) != 0xff)
229                         spare->pageStatus = YNOR_PREMARKER;
230         }
231         
232
233         return YAFFS_OK;        
234
235 }
236
237 static int ynorif1_FormatBlock(yaffs_Device *dev, int blockNumber)
238 {
239         __u32 *blockAddr = Block2Addr(dev,blockNumber);
240         __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
241         __u32 formatValue = FORMAT_VALUE;
242         
243         ynorif1_FlashEraseBlock(blockAddr);
244         ynorif1_FlashWrite32(formatAddr,&formatValue,1);
245         
246         return YAFFS_OK;
247 }
248
249 static int ynorif1_UnformatBlock(yaffs_Device *dev, int blockNumber)
250 {
251         __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
252         __u32 formatValue = 0;
253         
254         ynorif1_FlashWrite32(formatAddr,&formatValue,1);
255         
256         return YAFFS_OK;
257 }
258
259 static int ynorif1_IsBlockFormatted(yaffs_Device *dev, int blockNumber)
260 {
261         __u32 *formatAddr = Block2FormatAddr(dev,blockNumber);
262         __u32 formatValue;
263         
264         
265         ynorif1_FlashRead32(formatAddr,&formatValue,1);
266         
267         return (formatValue == FORMAT_VALUE);
268 }
269
270 int ynorif1_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
271 {
272
273         if(blockNumber < 0 || blockNumber >= BLOCKS_IN_DEVICE)
274         {
275                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
276                 return YAFFS_FAIL;
277         }
278         else
279         {
280                 ynorif1_UnformatBlock(dev,blockNumber);
281                 ynorif1_FormatBlock(dev,blockNumber);
282                 return YAFFS_OK;
283         }
284         
285 }
286
287 int ynorif1_InitialiseNAND(yaffs_Device *dev)
288 {
289         int i;
290         
291         ynorif1_FlashInit();
292         /* Go through the blocks formatting them if they are not formatted */
293         for(i = dev->startBlock; i <= dev->endBlock; i++){
294                 if(!ynorif1_IsBlockFormatted(dev,i)){
295                         ynorif1_FormatBlock(dev,i);
296                 }
297         }
298         return YAFFS_OK;
299 }
300
301 int ynorif1_DeinitialiseNAND(yaffs_Device *dev)
302 {
303         
304         ynorif1_FlashDeinit();
305
306         return YAFFS_OK;
307 }
308
309