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