yaffs More quick tests and have also fixed a bugin yaffs_mount function.
[yaffs2.git] / direct / yaffs_nandif.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 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 #include "yportenv.h"
15 #include "yaffs_guts.h"
16
17
18 #include "yaffs_nandif.h"
19 #include "yaffs_packedtags2.h"
20
21 #include "yramsim.h"
22
23 #include "yaffs_trace.h"
24 #include "yaffsfs.h"
25
26
27 /* NB For use with inband tags....
28  * We assume that the data buffer is of size totalBytersPerChunk so that we can also
29  * use it to load the tags.
30  */
31 int ynandif_WriteChunkWithTagsToNAND(struct yaffs_dev * dev, int nand_chunk,
32                                       const u8 * data,
33                                       const struct yaffs_ext_tags * tags)
34 {
35
36         int retval = 0;
37         struct yaffs_packed_tags2 pt;
38         void *spare;
39         unsigned spareSize = 0;
40         ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
41
42         T(YAFFS_TRACE_MTD,
43           (TSTR
44            ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
45             TENDSTR), nand_chunk, data, tags));
46             
47
48         /* For yaffs2 writing there must be both data and tags.
49          * If we're using inband tags, then the tags are stuffed into
50          * the end of the data buffer.
51          */
52
53         if(dev->param.inband_tags){
54                 struct yaffs_packed_tags2_tags_only *pt2tp;
55                 pt2tp = (struct yaffs_packed_tags2_tags_only *)(data + dev->data_bytes_per_chunk);
56                 yaffs_pack_tags2_tags_only(pt2tp,tags);
57                 spare = NULL;
58                 spareSize = 0;
59         }
60         else{
61                 yaffs_pack_tags2(&pt, tags,!dev->param.no_tags_ecc);
62                 spare = &pt;
63                 spareSize = sizeof(struct yaffs_packed_tags2);
64         }
65         
66         retval = geometry->writeChunk(dev,nand_chunk,
67                                           data, dev->param.total_bytes_per_chunk, spare, spareSize);
68
69         return retval;
70 }
71
72 int ynandif_ReadChunkWithTagsFromNAND(struct yaffs_dev * dev, int nand_chunk,
73                                        u8 * data, struct yaffs_ext_tags * tags)
74 {
75         struct yaffs_packed_tags2 pt;
76         int localData = 0;
77         void *spare = NULL;
78         unsigned spareSize;
79         int retval = 0;
80         int eccStatus; //0 = ok, 1 = fixed, -1 = unfixed
81         ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
82
83         T(YAFFS_TRACE_MTD,
84           (TSTR
85            ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
86             TENDSTR), nand_chunk, data, tags));
87             
88         if(!tags){
89                 spare = NULL;
90                 spareSize = 0;
91         }else if(dev->param.inband_tags){
92                 
93                 if(!data) {
94                         localData = 1;
95                         data = yaffs_get_temp_buffer(dev,__LINE__);
96                 }
97                 spare = NULL;
98                 spareSize = 0;
99         }
100         else {
101                 spare = &pt;
102                 spareSize = sizeof(struct yaffs_packed_tags2);
103         }
104
105         retval = geometry->readChunk(dev,nand_chunk,
106                                          data,
107                                          data ? dev->param.total_bytes_per_chunk : 0,
108                                          spare,spareSize,
109                                          &eccStatus);
110
111         if(dev->param.inband_tags){
112                 if(tags){
113                         struct yaffs_packed_tags2_tags_only * pt2tp;
114                         pt2tp = (struct yaffs_packed_tags2_tags_only *)&data[dev->data_bytes_per_chunk];        
115                         yaffs_unpack_tags2_tags_only(tags,pt2tp);
116                 }
117         }
118         else {
119                 if (tags){
120                         yaffs_unpack_tags2(tags, &pt,!dev->param.no_tags_ecc);
121                 }
122         }
123
124         if(tags && tags->chunk_used){
125                 if(eccStatus < 0 || 
126                    tags->ecc_result == YAFFS_ECC_RESULT_UNFIXED)
127                         tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
128                 else if(eccStatus > 0 ||
129                              tags->ecc_result == YAFFS_ECC_RESULT_FIXED)
130                         tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
131                 else
132                         tags->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
133         }
134
135         if(localData)
136                 yaffs_release_temp_buffer(dev,data,__LINE__);
137         
138         return retval;
139 }
140
141 int ynandif_MarkNANDBlockBad(struct yaffs_dev *dev, int blockId)
142 {
143         ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
144
145         return geometry->markBlockBad(dev,blockId);
146 }
147
148 int ynandif_EraseBlockInNAND(struct yaffs_dev *dev, int blockId)
149 {
150         ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
151
152         return geometry->eraseBlock(dev,blockId);
153
154 }
155
156
157 static int ynandif_IsBlockOk(struct yaffs_dev *dev, int blockId)
158 {
159         ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
160
161         return geometry->checkBlockOk(dev,blockId);
162 }
163
164 int ynandif_QueryNANDBlock(struct yaffs_dev *dev, int blockId, enum yaffs_block_state *state, u32 *seq_number)
165 {
166         unsigned chunkNo;
167         struct yaffs_ext_tags tags;
168
169         *seq_number = 0;
170         
171         chunkNo = blockId * dev->param.chunks_per_block;
172         
173         if(!ynandif_IsBlockOk(dev,blockId)){
174                 *state = YAFFS_BLOCK_STATE_DEAD;
175         } 
176         else 
177         {
178                 ynandif_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
179
180                 if(!tags.chunk_used)
181                 {
182                         *state = YAFFS_BLOCK_STATE_EMPTY;
183                 }
184                 else 
185                 {
186                         *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
187                         *seq_number = tags.seq_number;
188                 }
189         }
190
191         return YAFFS_OK;
192 }
193
194
195 int ynandif_InitialiseNAND(struct yaffs_dev *dev)
196 {
197         ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
198
199         geometry->initialise(dev);
200
201         return YAFFS_OK;
202 }
203
204 int ynandif_Deinitialise_flash_fn(struct yaffs_dev *dev)
205 {
206         ynandif_Geometry *geometry = (ynandif_Geometry *)(dev->driver_context);
207
208         geometry->deinitialise(dev);
209
210         return YAFFS_OK;
211 }
212
213
214 struct yaffs_dev * 
215         yaffs_add_dev_from_geometry(const YCHAR *name,
216                                         const ynandif_Geometry *geometry)
217 {
218         YCHAR *clonedName = YMALLOC(sizeof(YCHAR) * (yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH)+1));
219         struct yaffs_dev *dev = YMALLOC(sizeof(struct yaffs_dev));
220
221         if(dev && clonedName){
222                 memset(dev,0,sizeof(struct yaffs_dev));
223                 yaffs_strcpy(clonedName,name);
224
225                 dev->param.name = clonedName;
226                 dev->param.write_chunk_tags_fn  = ynandif_WriteChunkWithTagsToNAND;
227                 dev->param.read_chunk_tags_fn = ynandif_ReadChunkWithTagsFromNAND;
228                 dev->param.erase_fn          = ynandif_EraseBlockInNAND;
229                 dev->param.initialise_flash_fn            = ynandif_InitialiseNAND;
230                 dev->param.query_block_fn            = ynandif_QueryNANDBlock;
231                 dev->param.bad_block_fn          = ynandif_MarkNANDBlockBad;
232                 dev->param.n_caches                        = 20;
233                 dev->param.start_block                = geometry->start_block;
234                 dev->param.end_block                  = geometry->end_block;
235                 dev->param.total_bytes_per_chunk                   = geometry->dataSize;
236                 dev->param.spare_bytes_per_chunk                   = geometry->spareSize;
237                 dev->param.inband_tags                             = geometry->inband_tags;
238                 dev->param.chunks_per_block                = geometry->pagesPerBlock;
239                 dev->param.use_nand_ecc                            = geometry->hasECC;
240                 dev->param.is_yaffs2                               = geometry->useYaffs2;
241                 dev->param.n_reserved_blocks               = 5;
242                 dev->driver_context                        = (void *)geometry;
243
244                 yaffs_add_device(dev);
245
246                 return dev;
247         }
248
249         if(dev)
250                 YFREE(dev);
251         if(clonedName)
252                 YFREE(clonedName);
253
254         return NULL;
255 }