2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
15 const char *yaffs_mtdif_c_version =
16 "$Id: yaffs_mtdif.c,v 1.18 2007-02-12 16:55:26 wookey Exp $";
21 #include "yaffs_mtdif.h"
23 #include "linux/mtd/mtd.h"
24 #include "linux/types.h"
25 #include "linux/time.h"
26 #include "linux/mtd/nand.h"
28 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
29 static struct nand_oobinfo yaffs_oobinfo = {
32 .eccpos = {8, 9, 10, 13, 14, 15}
35 static struct nand_oobinfo yaffs_noeccinfo = {
40 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
41 static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
43 oob[0] = spare->tagByte0;
44 oob[1] = spare->tagByte1;
45 oob[2] = spare->tagByte2;
46 oob[3] = spare->tagByte3;
47 oob[4] = spare->tagByte4;
48 oob[5] = spare->tagByte5 & 0x3f;
49 oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
50 oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
51 oob[6] = spare->tagByte6;
52 oob[7] = spare->tagByte7;
55 static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
57 struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
58 spare->tagByte0 = oob[0];
59 spare->tagByte1 = oob[1];
60 spare->tagByte2 = oob[2];
61 spare->tagByte3 = oob[3];
62 spare->tagByte4 = oob[4];
63 spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
64 spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
65 spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
66 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
67 spare->tagByte6 = oob[6];
68 spare->tagByte7 = oob[7];
69 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
71 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
75 int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
76 const __u8 * data, const yaffs_Spare * spare)
78 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
79 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
80 struct mtd_oob_ops ops;
85 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
86 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
87 __u8 spareAsBytes[8]; /* OOB */
90 retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
93 if (dev->useNANDECC) {
94 translate_spare2oob(spare, spareAsBytes);
95 ops.mode = MTD_OOB_AUTO;
96 ops.ooblen = 8; /* temp hack */
98 ops.mode = MTD_OOB_RAW;
99 ops.ooblen = YAFFS_BYTES_PER_SPARE;
101 ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
102 ops.datbuf = (u8 *)data;
104 ops.oobbuf = spareAsBytes;
105 retval = mtd->write_oob(mtd, addr, &ops);
108 __u8 *spareAsBytes = (__u8 *) spare;
113 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
114 &dummy, data, spareAsBytes,
118 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
119 &dummy, data, spareAsBytes,
124 mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
128 mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
129 &dummy, spareAsBytes);
139 int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
142 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
144 struct mtd_oob_ops ops;
149 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
150 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
151 __u8 spareAsBytes[8]; /* OOB */
154 retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
157 if (dev->useNANDECC) {
158 ops.mode = MTD_OOB_AUTO;
159 ops.ooblen = 8; /* temp hack */
161 ops.mode = MTD_OOB_RAW;
162 ops.ooblen = YAFFS_BYTES_PER_SPARE;
164 ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
167 ops.oobbuf = spareAsBytes;
168 retval = mtd->read_oob(mtd, addr, &ops);
170 translate_oob2spare(spare, spareAsBytes);
173 __u8 *spareAsBytes = (__u8 *) spare;
176 if (dev->useNANDECC) {
177 /* Careful, this call adds 2 ints */
178 /* to the end of the spare data. Calling function */
179 /* should allocate enough memory for spare, */
180 /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
182 mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
183 &dummy, data, spareAsBytes,
187 mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
188 &dummy, data, spareAsBytes,
194 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
198 mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
199 &dummy, spareAsBytes);
209 int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
211 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
213 ((loff_t) blockNumber) * dev->nDataBytesPerChunk
214 * dev->nChunksPerBlock;
215 struct erase_info ei;
220 ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
224 ei.priv = (u_long) dev;
226 /* Todo finish off the ei if required */
228 sema_init(&dev->sem, 0);
230 retval = mtd->erase(mtd, &ei);
238 int nandmtd_InitialiseNAND(yaffs_Device * dev)