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.
17 const char *yaffs_mtdif_c_version =
18 "$Id: yaffs_mtdif.c,v 1.19 2007/02/14 01:09:06 wookey Exp $";
23 #include "yaffs_mtdif.h"
25 #include "linux/mtd/mtd.h"
26 #include "linux/types.h"
27 #include "linux/time.h"
28 #include "linux/mtd/nand.h"
30 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
31 static struct nand_oobinfo yaffs_oobinfo = {
34 .eccpos = {8, 9, 10, 13, 14, 15}
37 static struct nand_oobinfo yaffs_noeccinfo = {
42 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
43 static inline void translate_spare2oob(const struct yaffs_spare *spare, u8 *oob)
50 oob[5] = spare->tb5 & 0x3f;
51 oob[5] |= spare->block_status == 'Y' ? 0: 0x80;
52 oob[5] |= spare->page_status == 0 ? 0: 0x40;
57 static inline void translate_oob2spare(struct yaffs_spare *spare, u8 *oob)
59 struct yaffs_nand_spare *nspare = (struct yaffs_nand_spare *)spare;
65 spare->tb5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
66 spare->block_status = oob[5] & 0x80 ? 0xff : 'Y';
67 spare->page_status = oob[5] & 0x40 ? 0xff : 0;
68 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
71 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
73 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
77 int nandmtd_WriteChunkToNAND(struct yaffs_dev * dev, int chunkInNAND,
78 const u8 * data, const struct yaffs_spare * spare)
80 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
81 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
82 struct mtd_oob_ops ops;
87 loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
88 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
89 u8 spareAsBytes[8]; /* OOB */
92 retval = mtd->write(mtd, addr, dev->data_bytes_per_chunk,
95 if (dev->param.use_nand_ecc) {
96 translate_spare2oob(spare, spareAsBytes);
97 ops.mode = MTD_OOB_AUTO;
98 ops.ooblen = 8; /* temp hack */
100 ops.mode = MTD_OOB_RAW;
101 ops.ooblen = YAFFS_BYTES_PER_SPARE;
103 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
104 ops.datbuf = (u8 *)data;
106 ops.oobbuf = spareAsBytes;
107 retval = mtd->write_oob(mtd, addr, &ops);
110 u8 *spareAsBytes = (u8 *) spare;
113 if (dev->param.use_nand_ecc)
115 mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
116 &dummy, data, spareAsBytes,
120 mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
121 &dummy, data, spareAsBytes,
126 mtd->write(mtd, addr, dev->data_bytes_per_chunk, &dummy,
130 mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
131 &dummy, spareAsBytes);
141 int nandmtd_ReadChunkFromNAND(struct yaffs_dev * dev, int chunkInNAND, u8 * data,
142 struct yaffs_spare * spare)
144 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
145 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
146 struct mtd_oob_ops ops;
151 loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
152 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
153 u8 spareAsBytes[8]; /* OOB */
156 retval = mtd->read(mtd, addr, dev->data_bytes_per_chunk,
159 if (dev->param.use_nand_ecc) {
160 ops.mode = MTD_OOB_AUTO;
161 ops.ooblen = 8; /* temp hack */
163 ops.mode = MTD_OOB_RAW;
164 ops.ooblen = YAFFS_BYTES_PER_SPARE;
166 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
169 ops.oobbuf = spareAsBytes;
170 retval = mtd->read_oob(mtd, addr, &ops);
171 if (dev->param.use_nand_ecc)
172 translate_oob2spare(spare, spareAsBytes);
175 u8 *spareAsBytes = (u8 *) spare;
178 if (dev->param.use_nand_ecc) {
179 /* Careful, this call adds 2 ints */
180 /* to the end of the spare data. Calling function */
181 /* should allocate enough memory for spare, */
182 /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
184 mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
185 &dummy, data, spareAsBytes,
189 mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
190 &dummy, data, spareAsBytes,
196 mtd->read(mtd, addr, dev->data_bytes_per_chunk, &dummy,
200 mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
201 &dummy, spareAsBytes);
211 int nandmtd_EraseBlockInNAND(struct yaffs_dev * dev, int blockNumber)
213 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
215 ((loff_t) blockNumber) * dev->data_bytes_per_chunk
216 * dev->param.chunks_per_block;
217 struct erase_info ei;
222 ei.len = dev->data_bytes_per_chunk * dev->param.chunks_per_block;
226 ei.priv = (u_long) dev;
228 /* Todo finish off the ei if required */
232 sema_init(&dev->sem, 0);
235 retval = mtd->erase(mtd, &ei);
243 int nandmtd_InitialiseNAND(struct yaffs_dev * dev)