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 =
17 "$Id: yaffs_mtdif2.c,v 1.27 2010-02-18 01:18:04 charles Exp $";
20 #include "yaffs_trace.h"
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 #include "yaffs_linux.h"
32 /* NB For use with inband tags....
33 * We assume that the data buffer is of size totalBytersPerChunk so that we can also
34 * use it to load the tags.
36 int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
38 const yaffs_ExtendedTags *tags)
40 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
41 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
42 struct mtd_oob_ops ops;
52 int packed_tags_size = dev->param.noTagsECC ? sizeof(pt.t) : sizeof(pt);
53 void * packed_tags_ptr = dev->param.noTagsECC ? (void *) &pt.t : (void *)&pt;
57 ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
58 TENDSTR), chunkInNAND, data, tags));
61 addr = ((loff_t) chunkInNAND) * dev->param.totalBytesPerChunk;
63 /* For yaffs2 writing there must be both data and tags.
64 * If we're using inband tags, then the tags are stuffed into
65 * the end of the data buffer.
69 else if (dev->param.inbandTags) {
70 yaffs_PackedTags2TagsPart *pt2tp;
71 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
72 yaffs_PackTags2TagsPart(pt2tp, tags);
74 yaffs_PackTags2(&pt, tags, !dev->param.noTagsECC);
76 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
77 ops.mode = MTD_OOB_AUTO;
78 ops.ooblen = (dev->param.inbandTags) ? 0 : packed_tags_size;
79 ops.len = dev->param.totalBytesPerChunk;
81 ops.datbuf = (__u8 *)data;
82 ops.oobbuf = (dev->param.inbandTags) ? NULL : packed_tags_ptr;
83 retval = mtd->write_oob(mtd, addr, &ops);
86 if (!dev->param.inbandTags) {
88 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
89 &dummy, data, (__u8 *) packed_tags_ptr, NULL);
92 mtd->write(mtd, addr, dev->param.totalBytesPerChunk, &dummy,
103 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
104 __u8 *data, yaffs_ExtendedTags *tags)
106 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
107 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
108 struct mtd_oob_ops ops;
114 loff_t addr = ((loff_t) chunkInNAND) * dev->param.totalBytesPerChunk;
116 yaffs_PackedTags2 pt;
118 int packed_tags_size = dev->param.noTagsECC ? sizeof(pt.t) : sizeof(pt);
119 void * packed_tags_ptr = dev->param.noTagsECC ? (void *) &pt.t: (void *)&pt;
123 ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
124 TENDSTR), chunkInNAND, data, tags));
126 if (dev->param.inbandTags) {
130 data = yaffs_GetTempBuffer(dev, __LINE__);
137 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
138 if (dev->param.inbandTags || (data && !tags))
139 retval = mtd->read(mtd, addr, dev->param.totalBytesPerChunk,
142 ops.mode = MTD_OOB_AUTO;
143 ops.ooblen = packed_tags_size;
144 ops.len = data ? dev->nDataBytesPerChunk : packed_tags_size;
147 ops.oobbuf = yaffs_DeviceToContext(dev)->spareBuffer;
148 retval = mtd->read_oob(mtd, addr, &ops);
151 if (!dev->param.inbandTags && data && tags) {
153 retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
154 &dummy, data, dev->spareBuffer,
159 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
161 if (!dev->param.inbandTags && tags)
163 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
169 if (dev->param.inbandTags) {
171 yaffs_PackedTags2TagsPart *pt2tp;
172 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
173 yaffs_UnpackTags2TagsPart(tags, pt2tp);
177 memcpy(packed_tags_ptr, yaffs_DeviceToContext(dev)->spareBuffer, packed_tags_size);
178 yaffs_UnpackTags2(tags, &pt, !dev->param.noTagsECC);
183 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
185 if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
186 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
193 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
195 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
198 (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
201 mtd->block_markbad(mtd,
202 blockNo * dev->param.nChunksPerBlock *
203 dev->param.totalBytesPerChunk);
212 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
213 yaffs_BlockState *state, __u32 *sequenceNumber)
215 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
219 (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
221 mtd->block_isbad(mtd,
222 blockNo * dev->param.nChunksPerBlock *
223 dev->param.totalBytesPerChunk);
226 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
228 *state = YAFFS_BLOCK_STATE_DEAD;
231 yaffs_ExtendedTags t;
232 nandmtd2_ReadChunkWithTagsFromNAND(dev,
234 dev->param.nChunksPerBlock, NULL,
238 *sequenceNumber = t.sequenceNumber;
239 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
242 *state = YAFFS_BLOCK_STATE_EMPTY;
246 (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,