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.24 2009-10-14 00:01:57 charles Exp $";
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;
50 int packed_tags_size = dev->noTagsECC ? sizeof(pt.t) : sizeof(pt);
51 void * packed_tags_ptr = dev->noTagsECC ? (void *) &pt.t : (void *)&pt;
55 ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
56 TENDSTR), chunkInNAND, data, tags));
59 addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
61 /* For yaffs2 writing there must be both data and tags.
62 * If we're using inband tags, then the tags are stuffed into
63 * the end of the data buffer.
67 else if (dev->inbandTags) {
68 yaffs_PackedTags2TagsPart *pt2tp;
69 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
70 yaffs_PackTags2TagsPart(pt2tp, tags);
72 yaffs_PackTags2(dev, &pt, tags);
74 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
75 ops.mode = MTD_OOB_AUTO;
76 ops.ooblen = (dev->inbandTags) ? 0 : packed_tags_size;
77 ops.len = dev->totalBytesPerChunk;
79 ops.datbuf = (__u8 *)data;
80 ops.oobbuf = (dev->inbandTags) ? NULL : packed_tags_ptr;
81 retval = mtd->write_oob(mtd, addr, &ops);
84 if (!dev->inbandTags) {
86 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
87 &dummy, data, (__u8 *) packed_tags_ptr, NULL);
90 mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
101 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
102 __u8 *data, yaffs_ExtendedTags *tags)
104 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
105 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
106 struct mtd_oob_ops ops;
112 loff_t addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
114 yaffs_PackedTags2 pt;
116 int packed_tags_size = dev->noTagsECC ? sizeof(pt.t) : sizeof(pt);
117 void * packed_tags_ptr = dev->noTagsECC ? (void *) &pt.t: (void *)&pt;
121 ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
122 TENDSTR), chunkInNAND, data, tags));
124 if (dev->inbandTags) {
128 data = yaffs_GetTempBuffer(dev, __LINE__);
135 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
136 if (dev->inbandTags || (data && !tags))
137 retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
140 ops.mode = MTD_OOB_AUTO;
141 ops.ooblen = packed_tags_size;
142 ops.len = data ? dev->nDataBytesPerChunk : packed_tags_size;
145 ops.oobbuf = dev->spareBuffer;
146 retval = mtd->read_oob(mtd, addr, &ops);
149 if (!dev->inbandTags && data && tags) {
151 retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
152 &dummy, data, dev->spareBuffer,
157 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
159 if (!dev->inbandTags && tags)
161 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
167 if (dev->inbandTags) {
169 yaffs_PackedTags2TagsPart *pt2tp;
170 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
171 yaffs_UnpackTags2TagsPart(tags, pt2tp);
175 memcpy(packed_tags_ptr, dev->spareBuffer, packed_tags_size);
176 yaffs_UnpackTags2(dev, tags, &pt);
181 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
183 if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
184 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
191 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
193 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
196 (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
199 mtd->block_markbad(mtd,
200 blockNo * dev->nChunksPerBlock *
201 dev->totalBytesPerChunk);
210 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
211 yaffs_BlockState *state, __u32 *sequenceNumber)
213 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
217 (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
219 mtd->block_isbad(mtd,
220 blockNo * dev->nChunksPerBlock *
221 dev->totalBytesPerChunk);
224 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
226 *state = YAFFS_BLOCK_STATE_DEAD;
229 yaffs_ExtendedTags t;
230 nandmtd2_ReadChunkWithTagsFromNAND(dev,
232 dev->nChunksPerBlock, NULL,
236 *sequenceNumber = t.sequenceNumber;
237 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
240 *state = YAFFS_BLOCK_STATE_EMPTY;
244 (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,