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"
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 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
47 struct mtd_oob_ops ops;
56 struct yaffs_packed_tags2 pt;
58 int packed_tags_size =
59 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
60 void *packed_tags_ptr =
61 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
63 yaffs_trace(YAFFS_TRACE_MTD,
64 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
65 nand_chunk, data, tags);
67 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
69 /* For yaffs2 writing there must be both data and tags.
70 * If we're using inband tags, then the tags are stuffed into
71 * the end of the data buffer.
75 else if (dev->param.inband_tags) {
76 struct yaffs_packed_tags2_tags_only *pt2tp;
78 (struct yaffs_packed_tags2_tags_only *)(data +
80 data_bytes_per_chunk);
81 yaffs_pack_tags2_tags_only(pt2tp, tags);
83 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
86 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
87 ops.mode = MTD_OOB_AUTO;
88 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
89 ops.len = dev->param.total_bytes_per_chunk;
91 ops.datbuf = (u8 *) data;
92 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
93 retval = mtd->write_oob(mtd, addr, &ops);
96 if (!dev->param.inband_tags) {
98 mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
99 &dummy, data, (u8 *) packed_tags_ptr, NULL);
102 mtd->write(mtd, addr, dev->param.total_bytes_per_chunk,
113 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
114 u8 *data, struct yaffs_ext_tags *tags)
116 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
121 struct mtd_oob_ops ops;
127 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
129 struct yaffs_packed_tags2 pt;
131 int packed_tags_size =
132 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
133 void *packed_tags_ptr =
134 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
136 yaffs_trace(YAFFS_TRACE_MTD,
137 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
138 nand_chunk, data, tags);
140 if (dev->param.inband_tags) {
144 data = yaffs_get_temp_buffer(dev);
149 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
150 if (dev->param.inband_tags || (data && !tags))
151 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
154 ops.mode = MTD_OOB_AUTO;
155 ops.ooblen = packed_tags_size;
156 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
159 ops.oobbuf = local_spare;
160 retval = mtd->read_oob(mtd, addr, &ops);
163 if (!dev->param.inband_tags && data && tags) {
165 retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
166 &dummy, data, dev->spare_buffer, NULL);
170 mtd->read(mtd, addr, dev->data_bytes_per_chunk,
172 if (!dev->param.inband_tags && tags)
174 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
179 if (dev->param.inband_tags) {
181 struct yaffs_packed_tags2_tags_only *pt2tp;
183 (struct yaffs_packed_tags2_tags_only *)
184 &data[dev->data_bytes_per_chunk];
185 yaffs_unpack_tags2_tags_only(tags, pt2tp);
189 memcpy(packed_tags_ptr,
192 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
197 yaffs_release_temp_buffer(dev, data);
199 if (tags && retval == -EBADMSG
200 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
201 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
202 dev->n_ecc_unfixed++;
204 if (tags && retval == -EUCLEAN
205 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
206 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
216 int nandmtd2_MarkNANDBlockBad(struct yaffs_dev *dev, int blockNo)
218 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
221 yaffs_trace(YAFFS_TRACE_MTD,
222 "nandmtd2_MarkNANDBlockBad %d", blockNo);
225 mtd->block_markbad(mtd,
226 blockNo * dev->param.chunks_per_block *
227 dev->data_bytes_per_chunk);
236 int nandmtd2_QueryNANDBlock(struct yaffs_dev *dev, int blockNo,
237 enum yaffs_block_state *state, u32 *sequenceNumber)
239 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
242 yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_QueryNANDBlock %d", blockNo);
244 mtd->block_isbad(mtd,
245 blockNo * dev->param.chunks_per_block *
246 dev->data_bytes_per_chunk);
249 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
251 *state = YAFFS_BLOCK_STATE_DEAD;
254 struct yaffs_ext_tags t;
255 nandmtd2_read_chunk_tags(dev,
257 dev->param.chunks_per_block, NULL,
261 *sequenceNumber = t.seq_number;
262 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
265 *state = YAFFS_BLOCK_STATE_EMPTY;
268 yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d",
269 *sequenceNumber, *state);