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 total_bytes_per_chunk so that we can also
31 * use it to load the tags.
33 int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
35 const struct yaffs_ext_tags *tags)
37 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
38 struct mtd_oob_ops ops;
43 struct yaffs_packed_tags2 pt;
45 int packed_tags_size =
46 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
47 void *packed_tags_ptr =
48 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
50 yaffs_trace(YAFFS_TRACE_MTD,
51 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
52 nand_chunk, data, tags);
54 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
56 /* For yaffs2 writing there must be both data and tags.
57 * If we're using inband tags, then the tags are stuffed into
58 * the end of the data buffer.
62 else if (dev->param.inband_tags) {
63 struct yaffs_packed_tags2_tags_only *pt2tp;
65 (struct yaffs_packed_tags2_tags_only *)(data +
67 data_bytes_per_chunk);
68 yaffs_pack_tags2_tags_only(pt2tp, tags);
70 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
73 ops.mode = MTD_OOB_AUTO;
74 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
75 ops.len = dev->param.total_bytes_per_chunk;
77 ops.datbuf = (u8 *) data;
78 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
79 retval = mtd->write_oob(mtd, addr, &ops);
87 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
88 u8 * data, struct yaffs_ext_tags *tags)
90 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
91 struct mtd_oob_ops ops;
97 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
99 struct yaffs_packed_tags2 pt;
101 int packed_tags_size =
102 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
103 void *packed_tags_ptr =
104 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
106 yaffs_trace(YAFFS_TRACE_MTD,
107 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
108 nand_chunk, data, tags);
110 if (dev->param.inband_tags) {
114 data = yaffs_get_temp_buffer(dev, __LINE__);
119 if (dev->param.inband_tags || (data && !tags))
120 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
123 ops.mode = MTD_OOB_AUTO;
124 ops.ooblen = packed_tags_size;
125 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
128 ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
129 retval = mtd->read_oob(mtd, addr, &ops);
132 if (dev->param.inband_tags) {
134 struct yaffs_packed_tags2_tags_only *pt2tp;
136 (struct yaffs_packed_tags2_tags_only *)&data[dev->
137 data_bytes_per_chunk];
138 yaffs_unpack_tags2_tags_only(tags, pt2tp);
142 memcpy(packed_tags_ptr,
143 yaffs_dev_to_lc(dev)->spare_buffer,
145 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
150 yaffs_release_temp_buffer(dev, data, __LINE__);
152 #ifdef CONFIG_MTD_NAND_RK29
153 //dxj 20101221@ if return -EBADMSG then i think the page is badchunk so just set the eccResult=YAFFS_ECC_RESULT_NO_ERROR
154 if (tags && retval == -EBADMSG) {
156 if (tags && retval == -EBADMSG
157 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
159 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
160 dev->n_ecc_unfixed++;
162 if (tags && retval == -EUCLEAN
163 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
164 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
173 int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
175 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
177 yaffs_trace(YAFFS_TRACE_MTD,
178 "nandmtd2_mark_block_bad %d", block_no);
181 mtd->block_markbad(mtd,
182 block_no * dev->param.chunks_per_block *
183 dev->param.total_bytes_per_chunk);
192 int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
193 enum yaffs_block_state *state, u32 * seq_number)
195 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
198 yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no);
200 mtd->block_isbad(mtd,
201 block_no * dev->param.chunks_per_block *
202 dev->param.total_bytes_per_chunk);
205 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
207 *state = YAFFS_BLOCK_STATE_DEAD;
210 struct yaffs_ext_tags t;
211 nandmtd2_read_chunk_tags(dev, block_no *
212 dev->param.chunks_per_block, NULL, &t);
215 *seq_number = t.seq_number;
216 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
219 *state = YAFFS_BLOCK_STATE_EMPTY;
222 yaffs_trace(YAFFS_TRACE_MTD,
223 "block is bad seq %d state %d", *seq_number, *state);