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.
20 #include "yaffs_mtdif.h"
22 #include "linux/mtd/mtd.h"
23 #include "linux/types.h"
24 #include "linux/time.h"
25 #include "linux/mtd/nand.h"
27 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
28 static struct nand_oobinfo yaffs_oobinfo = {
31 .eccpos = {8, 9, 10, 13, 14, 15}
34 static struct nand_oobinfo yaffs_noeccinfo = {
39 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
40 static inline void translate_spare2oob(const struct yaffs_spare *spare, u8 *oob)
47 oob[5] = spare->tb5 & 0x3f;
48 oob[5] |= spare->block_status == 'Y' ? 0 : 0x80;
49 oob[5] |= spare->page_status == 0 ? 0 : 0x40;
54 static inline void translate_oob2spare(struct yaffs_spare *spare, u8 *oob)
56 struct yaffs_nand_spare *nspare = (struct yaffs_nand_spare *)spare;
62 spare->tb5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
63 spare->block_status = oob[5] & 0x80 ? 0xff : 'Y';
64 spare->page_status = oob[5] & 0x40 ? 0xff : 0;
65 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
68 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
70 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
74 int nandmtd_WriteChunkToNAND(struct yaffs_dev *dev, int chunkInNAND,
75 const u8 *data, const struct yaffs_spare *spare)
77 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
78 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
79 struct mtd_oob_ops ops;
84 loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
85 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
86 u8 spareAsBytes[8]; /* OOB */
89 retval = mtd->write(mtd, addr, dev->data_bytes_per_chunk,
92 if (dev->param.use_nand_ecc) {
93 translate_spare2oob(spare, spareAsBytes);
94 ops.mode = MTD_OOB_AUTO;
95 ops.ooblen = 8; /* temp hack */
97 ops.mode = MTD_OOB_RAW;
98 ops.ooblen = YAFFS_BYTES_PER_SPARE;
100 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
101 ops.datbuf = (u8 *)data;
103 ops.oobbuf = spareAsBytes;
104 retval = mtd->write_oob(mtd, addr, &ops);
107 u8 *spareAsBytes = (u8 *) spare;
110 if (dev->param.use_nand_ecc)
112 mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
113 &dummy, data, spareAsBytes,
117 mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
118 &dummy, data, spareAsBytes,
123 mtd->write(mtd, addr, dev->data_bytes_per_chunk,
127 mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
128 &dummy, spareAsBytes);
138 int nandmtd_ReadChunkFromNAND(struct yaffs_dev *dev, int chunkInNAND, u8 *data,
139 struct yaffs_spare *spare)
141 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
142 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
143 struct mtd_oob_ops ops;
148 loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
149 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
150 u8 spareAsBytes[8]; /* OOB */
153 retval = mtd->read(mtd, addr, dev->data_bytes_per_chunk,
156 if (dev->param.use_nand_ecc) {
157 ops.mode = MTD_OOB_AUTO;
158 ops.ooblen = 8; /* temp hack */
160 ops.mode = MTD_OOB_RAW;
161 ops.ooblen = YAFFS_BYTES_PER_SPARE;
163 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
166 ops.oobbuf = spareAsBytes;
167 retval = mtd->read_oob(mtd, addr, &ops);
168 if (dev->param.use_nand_ecc)
169 translate_oob2spare(spare, spareAsBytes);
172 u8 *spareAsBytes = (u8 *) spare;
175 if (dev->param.use_nand_ecc) {
176 /* Careful, this call adds 2 ints */
177 /* to the end of the spare data. Calling function */
178 /* should allocate enough memory for spare, */
179 /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
181 mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
182 &dummy, data, spareAsBytes,
186 mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
187 &dummy, data, spareAsBytes,
193 mtd->read(mtd, addr, dev->data_bytes_per_chunk,
197 mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
198 &dummy, spareAsBytes);
208 int nandmtd_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
210 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
212 ((loff_t) blockNumber) * dev->data_bytes_per_chunk
213 * dev->param.chunks_per_block;
214 struct erase_info ei;
219 ei.len = dev->data_bytes_per_chunk * dev->param.chunks_per_block;
223 ei.priv = (u_long) dev;
225 /* Todo finish off the ei if required */
228 retval = mtd->erase(mtd, &ei);
236 int nandmtd_InitialiseNAND(struct yaffs_dev *dev)