93a4f20ede4ec2d89b55ab9ab7fbff57876bbfc6
[openwrt.git] / target / linux / generic-2.6 / files / fs / yaffs2 / yaffs_mtdif.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 const char *yaffs_mtdif_c_version =
15
16 #include "yportenv.h"
17
18
19 #include "yaffs_mtdif.h"
20
21 #include "linux/mtd/mtd.h"
22 #include "linux/types.h"
23 #include "linux/time.h"
24 #include "linux/mtd/nand.h"
25
26 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
27 static struct nand_oobinfo yaffs_oobinfo = {
28 .useecc = 1,
29 .eccbytes = 6,
30 .eccpos = {8, 9, 10, 13, 14, 15}
31 };
32
33 static struct nand_oobinfo yaffs_noeccinfo = {
34 .useecc = 0,
35 };
36 #endif
37
38 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
39 static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
40 {
41 oob[0] = spare->tagByte0;
42 oob[1] = spare->tagByte1;
43 oob[2] = spare->tagByte2;
44 oob[3] = spare->tagByte3;
45 oob[4] = spare->tagByte4;
46 oob[5] = spare->tagByte5 & 0x3f;
47 oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
48 oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
49 oob[6] = spare->tagByte6;
50 oob[7] = spare->tagByte7;
51 }
52
53 static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
54 {
55 struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
56 spare->tagByte0 = oob[0];
57 spare->tagByte1 = oob[1];
58 spare->tagByte2 = oob[2];
59 spare->tagByte3 = oob[3];
60 spare->tagByte4 = oob[4];
61 spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
62 spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
63 spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
64 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
65 spare->tagByte6 = oob[6];
66 spare->tagByte7 = oob[7];
67 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
68
69 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
70 }
71 #endif
72
73 int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
74 const __u8 * data, const yaffs_Spare * spare)
75 {
76 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
77 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
78 struct mtd_oob_ops ops;
79 #endif
80 size_t dummy;
81 int retval = 0;
82
83 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
84 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
85 __u8 spareAsBytes[8]; /* OOB */
86
87 if (data && !spare)
88 retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
89 &dummy, data);
90 else if (spare) {
91 if (dev->useNANDECC) {
92 translate_spare2oob(spare, spareAsBytes);
93 ops.mode = MTD_OOB_AUTO;
94 ops.ooblen = 8; /* temp hack */
95 } else {
96 ops.mode = MTD_OOB_RAW;
97 ops.ooblen = YAFFS_BYTES_PER_SPARE;
98 }
99 ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
100 ops.datbuf = (u8 *)data;
101 ops.ooboffs = 0;
102 ops.oobbuf = spareAsBytes;
103 retval = mtd->write_oob(mtd, addr, &ops);
104 }
105 #else
106 __u8 *spareAsBytes = (__u8 *) spare;
107
108 if (data && spare) {
109 if (dev->useNANDECC)
110 retval =
111 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
112 &dummy, data, spareAsBytes,
113 &yaffs_oobinfo);
114 else
115 retval =
116 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
117 &dummy, data, spareAsBytes,
118 &yaffs_noeccinfo);
119 } else {
120 if (data)
121 retval =
122 mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
123 data);
124 if (spare)
125 retval =
126 mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
127 &dummy, spareAsBytes);
128 }
129 #endif
130
131 if (retval == 0)
132 return YAFFS_OK;
133 else
134 return YAFFS_FAIL;
135 }
136
137 int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
138 yaffs_Spare * spare)
139 {
140 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
141 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
142 struct mtd_oob_ops ops;
143 #endif
144 size_t dummy;
145 int retval = 0;
146
147 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
148 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
149 __u8 spareAsBytes[8]; /* OOB */
150
151 if (data && !spare)
152 retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
153 &dummy, data);
154 else if (spare) {
155 if (dev->useNANDECC) {
156 ops.mode = MTD_OOB_AUTO;
157 ops.ooblen = 8; /* temp hack */
158 } else {
159 ops.mode = MTD_OOB_RAW;
160 ops.ooblen = YAFFS_BYTES_PER_SPARE;
161 }
162 ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
163 ops.datbuf = data;
164 ops.ooboffs = 0;
165 ops.oobbuf = spareAsBytes;
166 retval = mtd->read_oob(mtd, addr, &ops);
167 if (dev->useNANDECC)
168 translate_oob2spare(spare, spareAsBytes);
169 }
170 #else
171 __u8 *spareAsBytes = (__u8 *) spare;
172
173 if (data && spare) {
174 if (dev->useNANDECC) {
175 /* Careful, this call adds 2 ints */
176 /* to the end of the spare data. Calling function */
177 /* should allocate enough memory for spare, */
178 /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
179 retval =
180 mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
181 &dummy, data, spareAsBytes,
182 &yaffs_oobinfo);
183 } else {
184 retval =
185 mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
186 &dummy, data, spareAsBytes,
187 &yaffs_noeccinfo);
188 }
189 } else {
190 if (data)
191 retval =
192 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
193 data);
194 if (spare)
195 retval =
196 mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
197 &dummy, spareAsBytes);
198 }
199 #endif
200
201 if (retval == 0)
202 return YAFFS_OK;
203 else
204 return YAFFS_FAIL;
205 }
206
207 int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
208 {
209 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
210 __u32 addr =
211 ((loff_t) blockNumber) * dev->nDataBytesPerChunk
212 * dev->nChunksPerBlock;
213 struct erase_info ei;
214 int retval = 0;
215
216 ei.mtd = mtd;
217 ei.addr = addr;
218 ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
219 ei.time = 1000;
220 ei.retries = 2;
221 ei.callback = NULL;
222 ei.priv = (u_long) dev;
223
224 /* Todo finish off the ei if required */
225
226 sema_init(&dev->sem, 0);
227
228 retval = mtd->erase(mtd, &ei);
229
230 if (retval == 0)
231 return YAFFS_OK;
232 else
233 return YAFFS_FAIL;
234 }
235
236 int nandmtd_InitialiseNAND(yaffs_Device * dev)
237 {
238 return YAFFS_OK;
239 }
240
This page took 0.058357 seconds and 3 git commands to generate.