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.24 2010-02-19 01:00:14 charles 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 #include "yaffs_linux.h"
29 #if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 18))
30 static struct nand_oobinfo yaffs_oobinfo = {
33 .eccpos = {8, 9, 10, 13, 14, 15}
36 static struct nand_oobinfo yaffs_noeccinfo = {
41 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
42 static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
44 oob[0] = spare->tagByte0;
45 oob[1] = spare->tagByte1;
46 oob[2] = spare->tagByte2;
47 oob[3] = spare->tagByte3;
48 oob[4] = spare->tagByte4;
49 oob[5] = spare->tagByte5 & 0x3f;
50 oob[5] |= spare->blockStatus == 'Y' ? 0 : 0x80;
51 oob[5] |= spare->pageStatus == 0 ? 0 : 0x40;
52 oob[6] = spare->tagByte6;
53 oob[7] = spare->tagByte7;
56 static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
58 struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
59 spare->tagByte0 = oob[0];
60 spare->tagByte1 = oob[1];
61 spare->tagByte2 = oob[2];
62 spare->tagByte3 = oob[3];
63 spare->tagByte4 = oob[4];
64 spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
65 spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
66 spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
67 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
68 spare->tagByte6 = oob[6];
69 spare->tagByte7 = oob[7];
70 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
72 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
76 int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
77 const __u8 *data, const yaffs_Spare *spare)
79 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
80 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
81 struct mtd_oob_ops ops;
86 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
87 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
88 __u8 spareAsBytes[8]; /* OOB */
91 retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
94 if (dev->param.useNANDECC) {
95 translate_spare2oob(spare, spareAsBytes);
96 ops.mode = MTD_OOB_AUTO;
97 ops.ooblen = 8; /* temp hack */
99 ops.mode = MTD_OOB_RAW;
100 ops.ooblen = YAFFS_BYTES_PER_SPARE;
102 ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
103 ops.datbuf = (u8 *)data;
105 ops.oobbuf = spareAsBytes;
106 retval = mtd->write_oob(mtd, addr, &ops);
109 __u8 *spareAsBytes = (__u8 *) spare;
112 if (dev->param.useNANDECC)
114 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
115 &dummy, data, spareAsBytes,
119 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
120 &dummy, data, spareAsBytes,
125 mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
129 mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
130 &dummy, spareAsBytes);
140 int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
143 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
144 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
145 struct mtd_oob_ops ops;
150 loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
151 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
152 __u8 spareAsBytes[8]; /* OOB */
155 retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
158 if (dev->param.useNANDECC) {
159 ops.mode = MTD_OOB_AUTO;
160 ops.ooblen = 8; /* temp hack */
162 ops.mode = MTD_OOB_RAW;
163 ops.ooblen = YAFFS_BYTES_PER_SPARE;
165 ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
168 ops.oobbuf = spareAsBytes;
169 retval = mtd->read_oob(mtd, addr, &ops);
170 if (dev->param.useNANDECC)
171 translate_oob2spare(spare, spareAsBytes);
174 __u8 *spareAsBytes = (__u8 *) spare;
177 if (dev->param.useNANDECC) {
178 /* Careful, this call adds 2 ints */
179 /* to the end of the spare data. Calling function */
180 /* should allocate enough memory for spare, */
181 /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
183 mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
184 &dummy, data, spareAsBytes,
188 mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
189 &dummy, data, spareAsBytes,
195 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
199 mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
200 &dummy, spareAsBytes);
210 int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
212 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
214 ((loff_t) blockNumber) * dev->param.totalBytesPerChunk
215 * dev->param.nChunksPerBlock;
216 struct erase_info ei;
222 ei.len = dev->param.totalBytesPerChunk * dev->param.nChunksPerBlock;
226 ei.priv = (u_long) dev;
228 retval = mtd->erase(mtd, &ei);
236 int nandmtd_InitialiseNAND(yaffs_Device *dev)