2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
15 #include "yaffs_mtdif.h"
17 #include "linux/mtd/mtd.h"
18 #include "linux/types.h"
19 #include "linux/time.h"
20 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0))
21 #include "linux/mtd/nand.h"
23 #include "linux/mtd/rawnand.h"
25 #include "linux/kernel.h"
26 #include "linux/version.h"
27 #include "linux/types.h"
28 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0))
29 #include "uapi/linux/major.h"
32 #include "yaffs_trace.h"
33 #include "yaffs_guts.h"
34 #include "yaffs_linux.h"
35 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
36 #define MTD_OPS_AUTO_OOB MTD_OOB_AUTO
40 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
41 #define mtd_erase(m, ei) (m)->erase(m, ei)
42 #define mtd_write_oob(m, addr, pops) (m)->write_oob(m, addr, pops)
43 #define mtd_read_oob(m, addr, pops) (m)->read_oob(m, addr, pops)
44 #define mtd_block_isbad(m, offs) (m)->block_isbad(m, offs)
45 #define mtd_block_markbad(m, offs) (m)->block_markbad(m, offs)
48 int nandmtd_erase_block(struct yaffs_dev *dev, int block_no)
50 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
51 u32 addr = ((loff_t) block_no) * dev->param.total_bytes_per_chunk *
52 dev->param.chunks_per_block;
56 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0))
60 ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block;
61 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0))
65 ei.priv = (u_long) dev;
68 retval = mtd_erase(mtd, &ei);
76 static int yaffs_mtd_write(struct yaffs_dev *dev, int nand_chunk,
77 const u8 *data, int data_len,
78 const u8 *oob, int oob_len)
80 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
82 struct mtd_oob_ops ops;
85 yaffs_trace(YAFFS_TRACE_MTD,
86 "yaffs_mtd_write(%p, %d, %p, %d, %p, %d)\n",
87 dev, nand_chunk, data, data_len, oob, oob_len);
89 if (!data || !data_len) {
94 if (!oob || !oob_len) {
99 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
100 memset(&ops, 0, sizeof(ops));
101 ops.mode = MTD_OPS_AUTO_OOB;
102 ops.len = (data) ? data_len : 0;
103 ops.ooblen = oob_len;
104 ops.datbuf = (u8 *)data;
105 ops.oobbuf = (u8 *)oob;
107 retval = mtd_write_oob(mtd, addr, &ops);
109 yaffs_trace(YAFFS_TRACE_MTD,
110 "write_oob failed, chunk %d, mtd error %d",
113 return retval ? YAFFS_FAIL : YAFFS_OK;
116 static int yaffs_mtd_read(struct yaffs_dev *dev, int nand_chunk,
117 u8 *data, int data_len,
118 u8 *oob, int oob_len,
119 enum yaffs_ecc_result *ecc_result)
121 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
123 struct mtd_oob_ops ops;
126 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
127 memset(&ops, 0, sizeof(ops));
128 ops.mode = MTD_OPS_AUTO_OOB;
129 ops.len = (data) ? data_len : 0;
130 ops.ooblen = oob_len;
134 #if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
135 /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
136 * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
138 ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
140 /* Read page and oob using MTD.
141 * Check status and determine ECC result.
143 retval = mtd_read_oob(mtd, addr, &ops);
145 yaffs_trace(YAFFS_TRACE_MTD,
146 "read_oob failed, chunk %d, mtd error %d",
153 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
157 /* MTD's ECC fixed the data */
159 *ecc_result = YAFFS_ECC_RESULT_FIXED;
165 /* MTD's ECC could not fix the data */
166 dev->n_ecc_unfixed++;
168 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
175 static int yaffs_mtd_erase(struct yaffs_dev *dev, int block_no)
177 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
180 struct erase_info ei;
184 block_size = dev->param.total_bytes_per_chunk *
185 dev->param.chunks_per_block;
186 addr = ((loff_t) block_no) * block_size;
188 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0))
193 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0))
197 ei.priv = (u_long) dev;
200 retval = mtd_erase(mtd, &ei);
208 static int yaffs_mtd_mark_bad(struct yaffs_dev *dev, int block_no)
210 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
211 int blocksize = dev->param.chunks_per_block * dev->param.total_bytes_per_chunk;
214 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", block_no);
216 retval = mtd_block_markbad(mtd, (loff_t) blocksize * block_no);
217 return (retval) ? YAFFS_FAIL : YAFFS_OK;
220 static int yaffs_mtd_check_bad(struct yaffs_dev *dev, int block_no)
222 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
223 int blocksize = dev->param.chunks_per_block * dev->param.total_bytes_per_chunk;
226 yaffs_trace(YAFFS_TRACE_MTD, "checking block %d bad", block_no);
228 retval = mtd_block_isbad(mtd, (loff_t) blocksize * block_no);
229 return (retval) ? YAFFS_FAIL : YAFFS_OK;
232 static int yaffs_mtd_initialise(struct yaffs_dev *dev)
237 static int yaffs_mtd_deinitialise(struct yaffs_dev *dev)
242 void yaffs_mtd_drv_install(struct yaffs_dev *dev)
244 struct yaffs_driver *drv = &dev->drv;
246 drv->drv_write_chunk_fn = yaffs_mtd_write;
247 drv->drv_read_chunk_fn = yaffs_mtd_read;
248 drv->drv_erase_fn = yaffs_mtd_erase;
249 drv->drv_mark_bad_fn = yaffs_mtd_mark_bad;
250 drv->drv_check_bad_fn = yaffs_mtd_check_bad;
251 drv->drv_initialise_fn = yaffs_mtd_initialise;
252 drv->drv_deinitialise_fn = yaffs_mtd_deinitialise;
255 struct mtd_info * yaffs_get_mtd_device(dev_t sdev)
257 struct mtd_info *mtd;
259 mtd = yaffs_get_mtd_device(sdev);
261 /* Check it's an mtd device..... */
262 if (MAJOR(sdev) != MTD_BLOCK_MAJOR)
263 return NULL; /* This isn't an mtd device */
265 /* Check it's NAND */
266 if (mtd->type != MTD_NANDFLASH) {
267 yaffs_trace(YAFFS_TRACE_ALWAYS,
268 "yaffs: MTD device is not NAND it's type %d",
273 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
274 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
275 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
276 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
277 yaffs_trace(YAFFS_TRACE_OS, " size %u", mtd->size);
279 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
285 int yaffs_verify_mtd(struct mtd_info *mtd, int yaffs_version, int inband_tags)
287 if (yaffs_version == 2) {
288 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
289 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
291 yaffs_trace(YAFFS_TRACE_ALWAYS,
292 "MTD device does not have the right page sizes"
297 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
298 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
299 yaffs_trace(YAFFS_TRACE_ALWAYS,
300 "MTD device does not support have the right page sizes"
309 void yaffs_put_mtd_device(struct mtd_info *mtd)