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