2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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"
18 #include "yaffs_mtdif2.h"
19 #include "yaffs_packedtags2.h"
20 #include "yaffs_linux.h"
21 #include "linux/mtd/mtd.h"
22 #include "linux/types.h"
23 #include "linux/time.h"
26 /* NB For use with inband tags....
27 * We assume that the data buffer is of size total_bytes_per_chunk so that
28 * we can also use it to load the tags.
30 int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
32 const struct yaffs_ext_tags *tags)
34 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
35 struct mtd_oob_ops ops;
38 struct yaffs_packed_tags2 pt;
39 int packed_tags_size =
40 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
41 void *packed_tags_ptr =
42 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
44 yaffs_trace(YAFFS_TRACE_MTD,
45 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
46 nand_chunk, data, tags);
48 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
50 /* For yaffs2 writing there must be both data and tags.
51 * If we're using inband tags, then the tags are stuffed into
52 * the end of the data buffer.
56 else if (dev->param.inband_tags) {
57 struct yaffs_packed_tags2_tags_only *pt2tp;
60 (struct yaffs_packed_tags2_tags_only *)
61 (data + dev->data_bytes_per_chunk);
62 yaffs_pack_tags2_tags_only(pt2tp, tags);
64 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
67 ops.mode = MTD_OOB_AUTO;
68 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
69 ops.len = dev->param.total_bytes_per_chunk;
71 ops.datbuf = (u8 *) data;
72 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
73 retval = mtd->write_oob(mtd, addr, &ops);
81 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
82 u8 *data, struct yaffs_ext_tags *tags)
84 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
85 struct mtd_oob_ops ops;
89 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
90 struct yaffs_packed_tags2 pt;
91 int packed_tags_size =
92 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
93 void *packed_tags_ptr =
94 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
96 yaffs_trace(YAFFS_TRACE_MTD,
97 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
98 nand_chunk, data, tags);
100 if (dev->param.inband_tags && !data) {
102 data = yaffs_get_temp_buffer(dev);
105 if (dev->param.inband_tags || (data && !tags)) {
106 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
109 ops.mode = MTD_OOB_AUTO;
110 ops.ooblen = packed_tags_size;
111 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
114 ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
115 retval = mtd->read_oob(mtd, addr, &ops);
118 if (dev->param.inband_tags && tags) {
119 struct yaffs_packed_tags2_tags_only *pt2tp;
122 (struct yaffs_packed_tags2_tags_only *)
123 &data[dev->data_bytes_per_chunk];
124 yaffs_unpack_tags2_tags_only(tags, pt2tp);
126 memcpy(packed_tags_ptr,
127 yaffs_dev_to_lc(dev)->spare_buffer,
129 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
133 yaffs_release_temp_buffer(dev, data);
135 if (tags && retval == -EBADMSG &&
136 tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
137 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
138 dev->n_ecc_unfixed++;
140 if (tags && retval == -EUCLEAN &&
141 tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
142 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
152 int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
154 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
157 yaffs_trace(YAFFS_TRACE_MTD,
158 "nandmtd2_mark_block_bad %d", block_no);
161 mtd->block_markbad(mtd,
162 block_no * dev->param.chunks_per_block *
163 dev->param.total_bytes_per_chunk);
171 int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
172 enum yaffs_block_state *state, u32 * seq_number)
174 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
177 yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no);
179 mtd->block_isbad(mtd,
180 block_no * dev->param.chunks_per_block *
181 dev->param.total_bytes_per_chunk);
184 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
186 *state = YAFFS_BLOCK_STATE_DEAD;
189 struct yaffs_ext_tags t;
191 nandmtd2_read_chunk_tags(dev, block_no *
192 dev->param.chunks_per_block, NULL, &t);
195 *seq_number = t.seq_number;
196 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
199 *state = YAFFS_BLOCK_STATE_EMPTY;
202 yaffs_trace(YAFFS_TRACE_MTD,
203 "block is bad seq %d state %d", *seq_number, *state);