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.
19 #include "yaffs_mtdif.h"
21 #include "linux/mtd/mtd.h"
22 #include "linux/types.h"
23 #include "linux/time.h"
24 #include "linux/mtd/nand.h"
27 static inline void translate_spare2oob(const struct yaffs_spare *spare, u8 *oob)
34 oob[5] = spare->tb5 & 0x3f;
35 oob[5] |= spare->block_status == 'Y' ? 0 : 0x80;
36 oob[5] |= spare->page_status == 0 ? 0 : 0x40;
41 static inline void translate_oob2spare(struct yaffs_spare *spare, u8 *oob)
43 struct yaffs_nand_spare *nspare = (struct yaffs_nand_spare *)spare;
49 spare->tb5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
50 spare->block_status = oob[5] & 0x80 ? 0xff : 'Y';
51 spare->page_status = oob[5] & 0x40 ? 0xff : 0;
52 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
55 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
57 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
61 int nandmtd_WriteChunkToNAND(struct yaffs_dev *dev, int chunkInNAND,
62 const u8 *data, const struct yaffs_spare *spare)
64 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
65 struct mtd_oob_ops ops;
68 loff_t addr = ((loff_t) chunkInNAND) * dev->param.total_bytes_per_chunk;
69 u8 spareAsBytes[8]; /* OOB */
72 retval = mtd->write(mtd, addr, dev->data_bytes_per_chunk,
75 if (dev->param.use_nand_ecc) {
76 translate_spare2oob(spare, spareAsBytes);
77 ops.mode = MTD_OOB_AUTO;
78 ops.ooblen = 8; /* temp hack */
80 ops.mode = MTD_OOB_RAW;
81 ops.ooblen = YAFFS_BYTES_PER_SPARE;
83 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
84 ops.datbuf = (u8 *)data;
86 ops.oobbuf = spareAsBytes;
87 retval = mtd->write_oob(mtd, addr, &ops);
96 int nandmtd_ReadChunkFromNAND(struct yaffs_dev *dev, int chunkInNAND, u8 *data,
97 struct yaffs_spare *spare)
99 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
100 struct mtd_oob_ops ops;
104 loff_t addr = ((loff_t) chunkInNAND) * dev->param.total_bytes_per_chunk;
105 u8 spareAsBytes[8]; /* OOB */
108 retval = mtd->read(mtd, addr, dev->data_bytes_per_chunk,
111 if (dev->param.use_nand_ecc) {
112 ops.mode = MTD_OOB_AUTO;
113 ops.ooblen = 8; /* temp hack */
115 ops.mode = MTD_OOB_RAW;
116 ops.ooblen = YAFFS_BYTES_PER_SPARE;
118 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
121 ops.oobbuf = spareAsBytes;
122 retval = mtd->read_oob(mtd, addr, &ops);
123 if (dev->param.use_nand_ecc)
124 translate_oob2spare(spare, spareAsBytes);
133 int nandmtd_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
135 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
137 ((loff_t) blockNumber) * dev->param.total_bytes_per_chunk
138 * dev->param.chunks_per_block;
139 struct erase_info ei;
144 ei.len = dev->data_bytes_per_chunk * dev->param.chunks_per_block;
148 ei.priv = (u_long) dev;
150 /* Todo finish off the ei if required */
153 retval = mtd->erase(mtd, &ei);
161 int nandmtd_InitialiseNAND(struct yaffs_dev *dev)