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 */
18 #include <linux/bug.h>
19 #include <linux/errno.h>
22 #include "yaffs_trace.h"
24 #include "yaffs_mtdif2.h"
26 #include <linux/mtd/mtd.h>
27 #include <linux/types.h>
28 #include <linux/time.h>
30 #include "yaffs_trace.h"
31 #include "yaffs_packedtags2.h"
33 #define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
34 #define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
37 /* NB For use with inband tags....
38 * We assume that the data buffer is of size total_bytes_per_chunk so
39 * that we can also use it to load the tags.
41 int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
43 const struct yaffs_ext_tags *tags)
45 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
46 struct mtd_oob_ops ops;
51 struct yaffs_packed_tags2 pt;
53 int packed_tags_size =
54 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
55 void *packed_tags_ptr =
56 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
58 yaffs_trace(YAFFS_TRACE_MTD,
59 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
60 nand_chunk, data, tags);
62 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
64 /* For yaffs2 writing there must be both data and tags.
65 * If we're using inband tags, then the tags are stuffed into
66 * the end of the data buffer.
70 else if (dev->param.inband_tags) {
71 struct yaffs_packed_tags2_tags_only *pt2tp;
73 (struct yaffs_packed_tags2_tags_only *)(data +
75 data_bytes_per_chunk);
76 yaffs_pack_tags2_tags_only(pt2tp, tags);
78 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
81 ops.mode = MTD_OPS_AUTO_OOB;
82 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
83 ops.len = dev->param.total_bytes_per_chunk;
85 ops.datbuf = (u8 *) data;
86 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
87 retval = mtd_write_oob(mtd, addr, &ops);
95 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
96 u8 *data, struct yaffs_ext_tags *tags)
98 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
100 struct mtd_oob_ops ops;
104 struct yaffs_packed_tags2 pt;
105 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
106 int packed_tags_size =
107 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
108 void *packed_tags_ptr =
109 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
111 yaffs_trace(YAFFS_TRACE_MTD,
112 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
113 nand_chunk, data, tags);
115 if (dev->param.inband_tags) {
119 data = yaffs_get_temp_buffer(dev);
124 if (dev->param.inband_tags || (data && !tags))
125 retval = mtd_read(mtd, addr, dev->param.total_bytes_per_chunk,
128 ops.mode = MTD_OPS_AUTO_OOB;
129 ops.ooblen = packed_tags_size;
130 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
133 ops.oobbuf = local_spare;
134 retval = mtd_read_oob(mtd, addr, &ops);
137 if (dev->param.inband_tags) {
139 struct yaffs_packed_tags2_tags_only *pt2tp;
141 (struct yaffs_packed_tags2_tags_only *)
142 &data[dev->data_bytes_per_chunk];
143 yaffs_unpack_tags2_tags_only(tags, pt2tp);
147 memcpy(packed_tags_ptr,
150 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
155 yaffs_release_temp_buffer(dev, data);
157 if (tags && retval == -EBADMSG
158 && 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;
174 int nandmtd2_MarkNANDBlockBad(struct yaffs_dev *dev, int blockNo)
176 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
179 yaffs_trace(YAFFS_TRACE_MTD,
180 "nandmtd2_MarkNANDBlockBad %d", blockNo);
183 mtd_block_markbad(mtd,
184 blockNo * dev->param.chunks_per_block *
185 dev->data_bytes_per_chunk);
194 int nandmtd2_QueryNANDBlock(struct yaffs_dev *dev, int blockNo,
195 enum yaffs_block_state *state, u32 *sequenceNumber)
197 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
200 yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_QueryNANDBlock %d", blockNo);
203 blockNo * dev->param.chunks_per_block *
204 dev->data_bytes_per_chunk);
207 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
209 *state = YAFFS_BLOCK_STATE_DEAD;
212 struct yaffs_ext_tags t;
213 nandmtd2_read_chunk_tags(dev,
215 dev->param.chunks_per_block, NULL,
219 *sequenceNumber = t.seq_number;
220 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
223 *state = YAFFS_BLOCK_STATE_EMPTY;
226 yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d",
227 *sequenceNumber, *state);