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 /* mtd interface for YAFFS2 */
16 const char *yaffs_mtdif2_c_version =
22 #include "yaffs_mtdif2.h"
24 #include "linux/mtd/mtd.h"
25 #include "linux/types.h"
26 #include "linux/time.h"
28 #include "yaffs_packedtags2.h"
30 /* NB For use with inband tags....
31 * We assume that the data buffer is of size totalBytersPerChunk so that we can also
32 * use it to load the tags.
34 int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
36 const yaffs_ExtendedTags *tags)
38 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
39 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
40 struct mtd_oob_ops ops;
52 ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
53 TENDSTR), chunkInNAND, data, tags));
55 addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
57 /* For yaffs2 writing there must be both data and tags.
58 * If we're using inband tags, then the tags are stuffed into
59 * the end of the data buffer.
63 else if (dev->inbandTags) {
64 yaffs_PackedTags2TagsPart *pt2tp;
65 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
66 yaffs_PackTags2TagsPart(pt2tp, tags);
68 yaffs_PackTags2(&pt, tags);
70 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
71 ops.mode = MTD_OOB_AUTO;
72 ops.ooblen = (dev->inbandTags) ? 0 : sizeof(pt);
73 ops.len = dev->totalBytesPerChunk;
75 ops.datbuf = (__u8 *)data;
76 ops.oobbuf = (dev->inbandTags) ? NULL : (void *)&pt;
77 retval = mtd->write_oob(mtd, addr, &ops);
80 if (!dev->inbandTags) {
82 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
83 &dummy, data, (__u8 *) &pt, NULL);
86 mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
97 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
98 __u8 *data, yaffs_ExtendedTags *tags)
100 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
101 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
102 struct mtd_oob_ops ops;
108 loff_t addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
110 yaffs_PackedTags2 pt;
114 ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
115 TENDSTR), chunkInNAND, data, tags));
117 if (dev->inbandTags) {
121 data = yaffs_GetTempBuffer(dev, __LINE__);
128 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
129 if (dev->inbandTags || (data && !tags))
130 retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
133 ops.mode = MTD_OOB_AUTO;
134 ops.ooblen = sizeof(pt);
135 ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
138 ops.oobbuf = dev->spareBuffer;
139 retval = mtd->read_oob(mtd, addr, &ops);
142 if (!dev->inbandTags && data && tags) {
144 retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
145 &dummy, data, dev->spareBuffer,
150 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
152 if (!dev->inbandTags && tags)
154 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
160 if (dev->inbandTags) {
162 yaffs_PackedTags2TagsPart *pt2tp;
163 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
164 yaffs_UnpackTags2TagsPart(tags, pt2tp);
168 memcpy(&pt, dev->spareBuffer, sizeof(pt));
169 yaffs_UnpackTags2(tags, &pt);
174 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
176 #ifdef CONFIG_MTD_NAND_RK29
177 //dxj 20101221@ if return -EBADMSG then i think the page is badchunk so just set the eccResult=YAFFS_ECC_RESULT_NO_ERROR
178 if (tags && retval == -EBADMSG /*&& tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR*/) {
180 if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) {
182 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
185 if (tags && retval == -EUCLEAN && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) {
186 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
195 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
197 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
200 (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
203 mtd->block_markbad(mtd,
204 blockNo * dev->nChunksPerBlock *
205 dev->totalBytesPerChunk);
214 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
215 yaffs_BlockState *state, __u32 *sequenceNumber)
217 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
221 (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
223 mtd->block_isbad(mtd,
224 blockNo * dev->nChunksPerBlock *
225 dev->totalBytesPerChunk);
228 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
230 *state = YAFFS_BLOCK_STATE_DEAD;
233 yaffs_ExtendedTags t;
234 nandmtd2_ReadChunkWithTagsFromNAND(dev,
236 dev->nChunksPerBlock, NULL,
240 *sequenceNumber = t.sequenceNumber;
241 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
244 *state = YAFFS_BLOCK_STATE_EMPTY;
248 (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,