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.
14 const char *yaffs_mtdif_c_version =
15 "$Id: yaffs_mtdif.c,v 1.19 2007/02/14 01:09:06 wookey Exp $";
20 #include "yaffs_mtdif.h"
22 #include "linux/mtd/mtd.h"
23 #include "linux/types.h"
24 #include "linux/time.h"
25 #include "linux/mtd/nand.h"
27 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
28 static struct nand_oobinfo yaffs_oobinfo = {
31 .eccpos = {8, 9, 10, 13, 14, 15}
34 static struct nand_oobinfo yaffs_noeccinfo = {
39 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
40 static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
42 oob[0] = spare->tagByte0;
43 oob[1] = spare->tagByte1;
44 oob[2] = spare->tagByte2;
45 oob[3] = spare->tagByte3;
46 oob[4] = spare->tagByte4;
47 oob[5] = spare->tagByte5 & 0x3f;
48 oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
49 oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
50 oob[6] = spare->tagByte6;
51 oob[7] = spare->tagByte7;
54 static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
56 struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
57 spare->tagByte0 = oob[0];
58 spare->tagByte1 = oob[1];
59 spare->tagByte2 = oob[2];
60 spare->tagByte3 = oob[3];
61 spare->tagByte4 = oob[4];
62 spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
63 spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
64 spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
65 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
66 spare->tagByte6 = oob[6];
67 spare->tagByte7 = oob[7];
68 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
70 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
74 int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
75 const __u8 * data, const yaffs_Spare * spare)
77 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
78 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
79 struct mtd_oob_ops ops;
84 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
85 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
86 __u8 spareAsBytes[8]; /* OOB */
89 retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
92 if (dev->useNANDECC) {
93 translate_spare2oob(spare, spareAsBytes);
94 ops.mode = MTD_OOB_AUTO;
95 ops.ooblen = 8; /* temp hack */
97 ops.mode = MTD_OOB_RAW;
98 ops.ooblen = YAFFS_BYTES_PER_SPARE;
100 ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
101 ops.datbuf = (u8 *)data;
103 ops.oobbuf = spareAsBytes;
104 retval = mtd->write_oob(mtd, addr, &ops);
107 __u8 *spareAsBytes = (__u8 *) spare;
112 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
113 &dummy, data, spareAsBytes,
117 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
118 &dummy, data, spareAsBytes,
123 mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
127 mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
128 &dummy, spareAsBytes);
138 int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
141 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
142 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
143 struct mtd_oob_ops ops;
148 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
149 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
150 __u8 spareAsBytes[8]; /* OOB */
153 retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
156 if (dev->useNANDECC) {
157 ops.mode = MTD_OOB_AUTO;
158 ops.ooblen = 8; /* temp hack */
160 ops.mode = MTD_OOB_RAW;
161 ops.ooblen = YAFFS_BYTES_PER_SPARE;
163 ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
166 ops.oobbuf = spareAsBytes;
167 retval = mtd->read_oob(mtd, addr, &ops);
169 translate_oob2spare(spare, spareAsBytes);
172 __u8 *spareAsBytes = (__u8 *) spare;
175 if (dev->useNANDECC) {
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)]. */
181 mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
182 &dummy, data, spareAsBytes,
186 mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
187 &dummy, data, spareAsBytes,
193 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
197 mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
198 &dummy, spareAsBytes);
208 int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
210 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
212 ((loff_t) blockNumber) * dev->nDataBytesPerChunk
213 * dev->nChunksPerBlock;
214 struct erase_info ei;
219 ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
223 ei.priv = (u_long) dev;
225 /* Todo finish off the ei if required */
227 sema_init(&dev->sem, 0);
229 retval = mtd->erase(mtd, &ei);
237 int nandmtd_InitialiseNAND(yaffs_Device * dev)