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 "asm/errno.h"
21 #include "yaffs_trace.h"
23 #include "yaffs_mtdif2.h"
25 #include "linux/mtd/mtd.h"
26 #include "linux/types.h"
27 #include "linux/time.h"
29 #include "yaffs_trace.h"
30 #include "yaffs_packedtags2.h"
32 #define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
33 #define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
36 /* NB For use with inband tags....
37 * We assume that the data buffer is of size total_bytes_per_chunk so
38 * that we can also use it to load the tags.
40 int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
42 const struct yaffs_ext_tags *tags)
44 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
45 struct mtd_oob_ops ops;
50 struct yaffs_packed_tags2 pt;
52 int packed_tags_size =
53 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
54 void *packed_tags_ptr =
55 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
57 yaffs_trace(YAFFS_TRACE_MTD,
58 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
59 nand_chunk, data, tags);
61 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
63 /* For yaffs2 writing there must be both data and tags.
64 * If we're using inband tags, then the tags are stuffed into
65 * the end of the data buffer.
69 else if (dev->param.inband_tags) {
70 struct yaffs_packed_tags2_tags_only *pt2tp;
72 (struct yaffs_packed_tags2_tags_only *)(data +
74 data_bytes_per_chunk);
75 yaffs_pack_tags2_tags_only(pt2tp, tags);
77 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
80 ops.mode = MTD_OOB_AUTO;
81 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
82 ops.len = dev->param.total_bytes_per_chunk;
84 ops.datbuf = (u8 *) data;
85 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
86 retval = mtd->write_oob(mtd, addr, &ops);
94 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
95 u8 *data, struct yaffs_ext_tags *tags)
97 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
99 struct mtd_oob_ops ops;
103 struct yaffs_packed_tags2 pt;
104 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
105 int packed_tags_size =
106 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
107 void *packed_tags_ptr =
108 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
110 yaffs_trace(YAFFS_TRACE_MTD,
111 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
112 nand_chunk, data, tags);
114 if (dev->param.inband_tags) {
118 data = yaffs_get_temp_buffer(dev);
123 if (dev->param.inband_tags || (data && !tags))
124 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
127 ops.mode = MTD_OOB_AUTO;
128 ops.ooblen = packed_tags_size;
129 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
132 ops.oobbuf = local_spare;
133 retval = mtd->read_oob(mtd, addr, &ops);
136 if (dev->param.inband_tags) {
138 struct yaffs_packed_tags2_tags_only *pt2tp;
140 (struct yaffs_packed_tags2_tags_only *)
141 &data[dev->data_bytes_per_chunk];
142 yaffs_unpack_tags2_tags_only(tags, pt2tp);
146 memcpy(packed_tags_ptr,
149 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
154 yaffs_release_temp_buffer(dev, data);
156 if (tags && retval == -EBADMSG
157 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
158 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
159 dev->n_ecc_unfixed++;
161 if (tags && retval == -EUCLEAN
162 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
163 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
173 int nandmtd2_MarkNANDBlockBad(struct yaffs_dev *dev, int blockNo)
175 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
178 yaffs_trace(YAFFS_TRACE_MTD,
179 "nandmtd2_MarkNANDBlockBad %d", blockNo);
182 mtd->block_markbad(mtd,
183 blockNo * dev->param.chunks_per_block *
184 dev->data_bytes_per_chunk);
193 int nandmtd2_QueryNANDBlock(struct yaffs_dev *dev, int blockNo,
194 enum yaffs_block_state *state, u32 *sequenceNumber)
196 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
199 yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_QueryNANDBlock %d", blockNo);
201 mtd->block_isbad(mtd,
202 blockNo * dev->param.chunks_per_block *
203 dev->data_bytes_per_chunk);
206 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
208 *state = YAFFS_BLOCK_STATE_DEAD;
211 struct yaffs_ext_tags t;
212 nandmtd2_read_chunk_tags(dev,
214 dev->param.chunks_per_block, NULL,
218 *sequenceNumber = t.seq_number;
219 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
222 *state = YAFFS_BLOCK_STATE_EMPTY;
225 yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d",
226 *sequenceNumber, *state);