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_dev_t *dev, int nand_chunk,
35 const yaffs_ext_tags *tags)
37 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
38 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
39 struct mtd_oob_ops ops;
49 int packed_tags_size = dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
50 void * packed_tags_ptr = dev->param.no_tags_ecc ? (void *) &pt.t : (void *)&pt;
54 ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
55 TENDSTR), nand_chunk, data, tags));
58 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
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.inband_tags) {
67 yaffs_PackedTags2TagsPart *pt2tp;
68 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->data_bytes_per_chunk);
69 yaffs_PackTags2TagsPart(pt2tp, tags);
71 yaffs_PackTags2(&pt, tags, !dev->param.no_tags_ecc);
73 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
74 ops.mode = MTD_OOB_AUTO;
75 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
76 ops.len = dev->param.total_bytes_per_chunk;
78 ops.datbuf = (__u8 *)data;
79 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
80 retval = mtd->write_oob(mtd, addr, &ops);
83 if (!dev->param.inband_tags) {
85 mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
86 &dummy, data, (__u8 *) packed_tags_ptr, NULL);
89 mtd->write(mtd, addr, dev->param.total_bytes_per_chunk, &dummy,
100 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_dev_t *dev, int nand_chunk,
101 __u8 *data, yaffs_ext_tags *tags)
103 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
104 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
105 struct mtd_oob_ops ops;
111 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
113 yaffs_PackedTags2 pt;
115 int packed_tags_size = dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
116 void * packed_tags_ptr = dev->param.no_tags_ecc ? (void *) &pt.t: (void *)&pt;
120 ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
121 TENDSTR), nand_chunk, data, tags));
123 if (dev->param.inband_tags) {
127 data = yaffs_get_temp_buffer(dev, __LINE__);
134 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
135 if (dev->param.inband_tags || (data && !tags))
136 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
139 ops.mode = MTD_OOB_AUTO;
140 ops.ooblen = packed_tags_size;
141 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
144 ops.oobbuf = yaffs_dev_to_lc(dev)->spareBuffer;
145 retval = mtd->read_oob(mtd, addr, &ops);
148 if (!dev->param.inband_tags && data && tags) {
150 retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
151 &dummy, data, dev->spareBuffer,
156 mtd->read(mtd, addr, dev->data_bytes_per_chunk, &dummy,
158 if (!dev->param.inband_tags && tags)
160 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
166 if (dev->param.inband_tags) {
168 yaffs_PackedTags2TagsPart *pt2tp;
169 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->data_bytes_per_chunk];
170 yaffs_unpack_tags2tags_part(tags, pt2tp);
174 memcpy(packed_tags_ptr, yaffs_dev_to_lc(dev)->spareBuffer, packed_tags_size);
175 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
180 yaffs_release_temp_buffer(dev, data, __LINE__);
182 if (tags && retval == -EBADMSG && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
183 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
184 dev->n_ecc_unfixed++;
186 if(tags && retval == -EUCLEAN && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
187 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
196 int nandmtd2_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no)
198 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
201 (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), block_no));
204 mtd->block_markbad(mtd,
205 block_no * dev->param.chunks_per_block *
206 dev->param.total_bytes_per_chunk);
215 int nandmtd2_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no,
216 yaffs_block_state_t *state, __u32 *seq_number)
218 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
222 (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), block_no));
224 mtd->block_isbad(mtd,
225 block_no * dev->param.chunks_per_block *
226 dev->param.total_bytes_per_chunk);
229 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
231 *state = YAFFS_BLOCK_STATE_DEAD;
235 nandmtd2_ReadChunkWithTagsFromNAND(dev,
237 dev->param.chunks_per_block, NULL,
241 *seq_number = t.seq_number;
242 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
245 *state = YAFFS_BLOCK_STATE_EMPTY;
249 (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number,