2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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 */
17 #include "yaffs_trace.h"
19 #include "yaffs_mtdif2.h"
21 #include "linux/mtd/mtd.h"
22 #include "linux/types.h"
23 #include "linux/time.h"
25 #include "yaffs_packedtags2.h"
27 #include "yaffs_linux.h"
29 /* NB For use with inband tags....
30 * We assume that the data buffer is of size totalBytersPerChunk so that we can also
31 * use it to load the tags.
33 int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
35 const yaffs_ExtendedTags *tags)
37 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
38 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
39 struct mtd_oob_ops ops;
49 int packed_tags_size = dev->param.noTagsECC ? sizeof(pt.t) : sizeof(pt);
50 void * packed_tags_ptr = dev->param.noTagsECC ? (void *) &pt.t : (void *)&pt;
54 ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
55 TENDSTR), chunkInNAND, data, tags));
58 addr = ((loff_t) chunkInNAND) * dev->param.totalBytesPerChunk;
60 /* For yaffs2 writing there must be both data and tags.
61 * If we're using inband tags, then the tags are stuffed into
62 * the end of the data buffer.
66 else if (dev->param.inbandTags) {
67 yaffs_PackedTags2TagsPart *pt2tp;
68 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
69 yaffs_PackTags2TagsPart(pt2tp, tags);
71 yaffs_PackTags2(&pt, tags, !dev->param.noTagsECC);
73 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
74 ops.mode = MTD_OOB_AUTO;
75 ops.ooblen = (dev->param.inbandTags) ? 0 : packed_tags_size;
76 ops.len = dev->param.totalBytesPerChunk;
78 ops.datbuf = (__u8 *)data;
79 ops.oobbuf = (dev->param.inbandTags) ? NULL : packed_tags_ptr;
80 retval = mtd->write_oob(mtd, addr, &ops);
83 if (!dev->param.inbandTags) {
85 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
86 &dummy, data, (__u8 *) packed_tags_ptr, NULL);
89 mtd->write(mtd, addr, dev->param.totalBytesPerChunk, &dummy,
100 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
101 __u8 *data, yaffs_ExtendedTags *tags)
103 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
104 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
105 struct mtd_oob_ops ops;
111 loff_t addr = ((loff_t) chunkInNAND) * dev->param.totalBytesPerChunk;
113 yaffs_PackedTags2 pt;
115 int packed_tags_size = dev->param.noTagsECC ? sizeof(pt.t) : sizeof(pt);
116 void * packed_tags_ptr = dev->param.noTagsECC ? (void *) &pt.t: (void *)&pt;
120 ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
121 TENDSTR), chunkInNAND, data, tags));
123 if (dev->param.inbandTags) {
127 data = yaffs_GetTempBuffer(dev, __LINE__);
134 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
135 if (dev->param.inbandTags || (data && !tags))
136 retval = mtd->read(mtd, addr, dev->param.totalBytesPerChunk,
139 ops.mode = MTD_OOB_AUTO;
140 ops.ooblen = packed_tags_size;
141 ops.len = data ? dev->nDataBytesPerChunk : packed_tags_size;
144 ops.oobbuf = yaffs_DeviceToContext(dev)->spareBuffer;
145 retval = mtd->read_oob(mtd, addr, &ops);
148 if (!dev->param.inbandTags && data && tags) {
150 retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
151 &dummy, data, dev->spareBuffer,
156 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
158 if (!dev->param.inbandTags && tags)
160 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
166 if (dev->param.inbandTags) {
168 yaffs_PackedTags2TagsPart *pt2tp;
169 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
170 yaffs_UnpackTags2TagsPart(tags, pt2tp);
174 memcpy(packed_tags_ptr, yaffs_DeviceToContext(dev)->spareBuffer, packed_tags_size);
175 yaffs_UnpackTags2(tags, &pt, !dev->param.noTagsECC);
180 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
182 if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) {
183 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
186 if(tags && retval == -EUCLEAN && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) {
187 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
196 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
198 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
201 (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
204 mtd->block_markbad(mtd,
205 blockNo * dev->param.nChunksPerBlock *
206 dev->param.totalBytesPerChunk);
215 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
216 yaffs_BlockState *state, __u32 *sequenceNumber)
218 struct mtd_info *mtd = yaffs_DeviceToContext(dev)->mtd;
222 (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
224 mtd->block_isbad(mtd,
225 blockNo * dev->param.nChunksPerBlock *
226 dev->param.totalBytesPerChunk);
229 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
231 *state = YAFFS_BLOCK_STATE_DEAD;
234 yaffs_ExtendedTags t;
235 nandmtd2_ReadChunkWithTagsFromNAND(dev,
237 dev->param.nChunksPerBlock, NULL,
241 *sequenceNumber = t.sequenceNumber;
242 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
245 *state = YAFFS_BLOCK_STATE_EMPTY;
249 (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,