generic-2.6: sync yaffs code with the official CVS tree
[openwrt/staging/dedeckeh.git] / target / linux / generic-2.6 / files-2.6.26 / fs / yaffs2 / yaffs_mtdif2.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 /* mtd interface for YAFFS2 */
15
16 const char *yaffs_mtdif2_c_version =
17 "$Id: yaffs_mtdif2.c,v 1.20 2008-05-05 07:58:58 charles Exp $";
18
19 #include "yportenv.h"
20
21
22 #include "yaffs_mtdif2.h"
23
24 #include "linux/mtd/mtd.h"
25 #include "linux/types.h"
26 #include "linux/time.h"
27
28 #include "yaffs_packedtags2.h"
29
30 /* NB For use with inband tags....
31 * We assume that the data buffer is of size totalBytersPerChunk so that we can also
32 * use it to load the tags.
33 */
34 int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
35 const __u8 * data,
36 const yaffs_ExtendedTags * tags)
37 {
38 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
39 #if (MTD_VERSION_CODE > MTD_VERSION(2,6,17))
40 struct mtd_oob_ops ops;
41 #else
42 size_t dummy;
43 #endif
44 int retval = 0;
45
46 loff_t addr;
47
48 yaffs_PackedTags2 pt;
49
50 T(YAFFS_TRACE_MTD,
51 (TSTR
52 ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
53 TENDSTR), chunkInNAND, data, tags));
54
55
56 addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
57
58 /* For yaffs2 writing there must be both data and tags.
59 * If we're using inband tags, then the tags are stuffed into
60 * the end of the data buffer.
61 */
62 if(!data || !tags)
63 BUG();
64 else if(dev->inbandTags){
65 yaffs_PackedTags2TagsPart *pt2tp;
66 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
67 yaffs_PackTags2TagsPart(pt2tp,tags);
68 }
69 else
70 yaffs_PackTags2(&pt, tags);
71
72 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
73 ops.mode = MTD_OOB_AUTO;
74 ops.ooblen = (dev->inbandTags) ? 0 : sizeof(pt);
75 ops.len = dev->totalBytesPerChunk;
76 ops.ooboffs = 0;
77 ops.datbuf = (__u8 *)data;
78 ops.oobbuf = (dev->inbandTags) ? NULL : (void *)&pt;
79 retval = mtd->write_oob(mtd, addr, &ops);
80
81 #else
82 if (!dev->inbandTags) {
83 retval =
84 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
85 &dummy, data, (__u8 *) & pt, NULL);
86 } else {
87 retval =
88 mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
89 data);
90 }
91 #endif
92
93 if (retval == 0)
94 return YAFFS_OK;
95 else
96 return YAFFS_FAIL;
97 }
98
99 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
100 __u8 * data, yaffs_ExtendedTags * tags)
101 {
102 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
103 #if (MTD_VERSION_CODE > MTD_VERSION(2,6,17))
104 struct mtd_oob_ops ops;
105 #endif
106 size_t dummy;
107 int retval = 0;
108 int localData = 0;
109
110 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
111
112 yaffs_PackedTags2 pt;
113
114 T(YAFFS_TRACE_MTD,
115 (TSTR
116 ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
117 TENDSTR), chunkInNAND, data, tags));
118
119 if(dev->inbandTags){
120
121 if(!data) {
122 localData = 1;
123 data = yaffs_GetTempBuffer(dev,__LINE__);
124 }
125
126
127 }
128
129
130 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
131 if (dev->inbandTags || (data && !tags))
132 retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
133 &dummy, data);
134 else if (tags) {
135 ops.mode = MTD_OOB_AUTO;
136 ops.ooblen = sizeof(pt);
137 ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
138 ops.ooboffs = 0;
139 ops.datbuf = data;
140 ops.oobbuf = dev->spareBuffer;
141 retval = mtd->read_oob(mtd, addr, &ops);
142 }
143 #else
144 if (!dev->inbandTags && data && tags) {
145
146 retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
147 &dummy, data, dev->spareBuffer,
148 NULL);
149 } else {
150 if (data)
151 retval =
152 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
153 data);
154 if (!dev->inbandTags && tags)
155 retval =
156 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
157 dev->spareBuffer);
158 }
159 #endif
160
161
162 if(dev->inbandTags){
163 if(tags){
164 yaffs_PackedTags2TagsPart * pt2tp;
165 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
166 yaffs_UnpackTags2TagsPart(tags,pt2tp);
167 }
168 }
169 else {
170 if (tags){
171 memcpy(&pt, dev->spareBuffer, sizeof(pt));
172 yaffs_UnpackTags2(tags, &pt);
173 }
174 }
175
176 if(localData)
177 yaffs_ReleaseTempBuffer(dev,data,__LINE__);
178
179 if(tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
180 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
181 if (retval == 0)
182 return YAFFS_OK;
183 else
184 return YAFFS_FAIL;
185 }
186
187 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
188 {
189 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
190 int retval;
191 T(YAFFS_TRACE_MTD,
192 (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
193
194 retval =
195 mtd->block_markbad(mtd,
196 blockNo * dev->nChunksPerBlock *
197 dev->nDataBytesPerChunk);
198
199 if (retval == 0)
200 return YAFFS_OK;
201 else
202 return YAFFS_FAIL;
203
204 }
205
206 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
207 yaffs_BlockState * state, int *sequenceNumber)
208 {
209 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
210 int retval;
211
212 T(YAFFS_TRACE_MTD,
213 (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
214 retval =
215 mtd->block_isbad(mtd,
216 blockNo * dev->nChunksPerBlock *
217 dev->nDataBytesPerChunk);
218
219 if (retval) {
220 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
221
222 *state = YAFFS_BLOCK_STATE_DEAD;
223 *sequenceNumber = 0;
224 } else {
225 yaffs_ExtendedTags t;
226 nandmtd2_ReadChunkWithTagsFromNAND(dev,
227 blockNo *
228 dev->nChunksPerBlock, NULL,
229 &t);
230
231 if (t.chunkUsed) {
232 *sequenceNumber = t.sequenceNumber;
233 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
234 } else {
235 *sequenceNumber = 0;
236 *state = YAFFS_BLOCK_STATE_EMPTY;
237 }
238 }
239 T(YAFFS_TRACE_MTD,
240 (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
241 *state));
242
243 if (retval == 0)
244 return YAFFS_OK;
245 else
246 return YAFFS_FAIL;
247 }
248