fs: btrfs: Crossport rbtree-utils from btrfs-progs
[platform/kernel/u-boot.git] / fs / yaffs2 / yaffs_nandif.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 #include "yportenv.h"
15 #include "yaffs_guts.h"
16 #include <malloc.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
30  * we can also use it to load the tags.
31  */
32 int ynandif_WriteChunkWithTagsToNAND(struct yaffs_dev *dev, int nand_chunk,
33                                       const u8 *data,
34                                       const struct yaffs_ext_tags *tags)
35 {
36
37         int retval = 0;
38         struct yaffs_packed_tags2 pt;
39         void *spare;
40         unsigned spareSize = 0;
41         struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
42
43         yaffs_trace(YAFFS_TRACE_MTD,
44                 "nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p",
45                 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
56                 pt2tp = (struct yaffs_packed_tags2_tags_only *)
57                         (data + dev->data_bytes_per_chunk);
58                 yaffs_pack_tags2_tags_only(pt2tp, tags);
59                 spare = NULL;
60                 spareSize = 0;
61         } else {
62                 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
63                 spare = &pt;
64                 spareSize = sizeof(struct yaffs_packed_tags2);
65         }
66
67         retval = geometry->writeChunk(dev, nand_chunk,
68                                 data, dev->param.total_bytes_per_chunk,
69                                 spare, spareSize);
70
71         return retval;
72 }
73
74 int ynandif_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev, int nand_chunk,
75                                        u8 *data, struct yaffs_ext_tags *tags)
76 {
77         struct yaffs_packed_tags2 pt;
78         int localData = 0;
79         void *spare = NULL;
80         unsigned spareSize;
81         int retval = 0;
82         int eccStatus; /* 0 = ok, 1 = fixed, -1 = unfixed */
83         struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
84
85         yaffs_trace(YAFFS_TRACE_MTD,
86                 "nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p",
87                 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);
97                 }
98                 spare = NULL;
99                 spareSize = 0;
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 *)
115                                         &data[dev->data_bytes_per_chunk];
116                         yaffs_unpack_tags2_tags_only(tags, pt2tp);
117                 }
118         } else {
119                 if (tags)
120                         yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
121         }
122
123         if (tags && tags->chunk_used) {
124                 if (eccStatus < 0 ||
125                    tags->ecc_result == YAFFS_ECC_RESULT_UNFIXED)
126                         tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
127                 else if (eccStatus > 0 ||
128                              tags->ecc_result == YAFFS_ECC_RESULT_FIXED)
129                         tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
130                 else
131                         tags->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
132         }
133
134         if (localData)
135                 yaffs_release_temp_buffer(dev, data);
136
137         return retval;
138 }
139
140 int ynandif_MarkNANDBlockBad(struct yaffs_dev *dev, int blockId)
141 {
142         struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
143
144         return geometry->markBlockBad(dev, blockId);
145 }
146
147 int ynandif_EraseBlockInNAND(struct yaffs_dev *dev, int blockId)
148 {
149         struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
150
151         return geometry->eraseBlock(dev, blockId);
152
153 }
154
155
156 static int ynandif_IsBlockOk(struct yaffs_dev *dev, int blockId)
157 {
158         struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
159
160         return geometry->checkBlockOk(dev, blockId);
161 }
162
163 int ynandif_QueryNANDBlock(struct yaffs_dev *dev, int blockId,
164                 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         } else {
176                 ynandif_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &tags);
177
178                 if (!tags.chunk_used) {
179                         *state = YAFFS_BLOCK_STATE_EMPTY;
180                 } else {
181                         *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
182                         *seq_number = tags.seq_number;
183                 }
184         }
185
186         return YAFFS_OK;
187 }
188
189
190 int ynandif_InitialiseNAND(struct yaffs_dev *dev)
191 {
192         struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
193
194         geometry->initialise(dev);
195
196         return YAFFS_OK;
197 }
198
199 int ynandif_Deinitialise_flash_fn(struct yaffs_dev *dev)
200 {
201         struct ynandif_Geometry *geometry = (struct ynandif_Geometry *)(dev->driver_context);
202
203         geometry->deinitialise(dev);
204
205         return YAFFS_OK;
206 }
207
208
209 struct yaffs_dev *
210         yaffs_add_dev_from_geometry(const YCHAR *name,
211                                         const struct ynandif_Geometry *geometry)
212 {
213         YCHAR *clonedName = malloc(sizeof(YCHAR) *
214                                 (strnlen(name, YAFFS_MAX_NAME_LENGTH)+1));
215         struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
216         struct yaffs_param *param;
217
218         if (dev && clonedName) {
219                 memset(dev, 0, sizeof(struct yaffs_dev));
220                 strcpy(clonedName, name);
221
222                 param = &dev->param;
223
224                 param->name = clonedName;
225                 param->write_chunk_tags_fn = ynandif_WriteChunkWithTagsToNAND;
226                 param->read_chunk_tags_fn = ynandif_ReadChunkWithTagsFromNAND;
227                 param->erase_fn = ynandif_EraseBlockInNAND;
228                 param->initialise_flash_fn = ynandif_InitialiseNAND;
229                 param->query_block_fn = ynandif_QueryNANDBlock;
230                 param->bad_block_fn = ynandif_MarkNANDBlockBad;
231                 param->n_caches = 20;
232                 param->start_block = geometry->start_block;
233                 param->end_block   = geometry->end_block;
234                 param->total_bytes_per_chunk  = geometry->dataSize;
235                 param->spare_bytes_per_chunk  = geometry->spareSize;
236                 param->inband_tags                = geometry->inband_tags;
237                 param->chunks_per_block   = geometry->pagesPerBlock;
238                 param->use_nand_ecc               = geometry->hasECC;
239                 param->is_yaffs2                  = geometry->useYaffs2;
240                 param->n_reserved_blocks          = 5;
241                 dev->driver_context               = (void *)geometry;
242
243                 yaffs_add_device(dev);
244
245                 return dev;
246         }
247
248         free(dev);
249         free(clonedName);
250
251         return NULL;
252 }