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 const char *yaffs_mtdif_c_version
=
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"
26 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
27 static struct nand_oobinfo yaffs_oobinfo
= {
30 .eccpos
= {8, 9, 10, 13, 14, 15}
33 static struct nand_oobinfo yaffs_noeccinfo
= {
38 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
39 static inline void translate_spare2oob(const yaffs_Spare
*spare
, __u8
*oob
)
41 oob
[0] = spare
->tagByte0
;
42 oob
[1] = spare
->tagByte1
;
43 oob
[2] = spare
->tagByte2
;
44 oob
[3] = spare
->tagByte3
;
45 oob
[4] = spare
->tagByte4
;
46 oob
[5] = spare
->tagByte5
& 0x3f;
47 oob
[5] |= spare
->blockStatus
== 'Y' ? 0: 0x80;
48 oob
[5] |= spare
->pageStatus
== 0 ? 0: 0x40;
49 oob
[6] = spare
->tagByte6
;
50 oob
[7] = spare
->tagByte7
;
53 static inline void translate_oob2spare(yaffs_Spare
*spare
, __u8
*oob
)
55 struct yaffs_NANDSpare
*nspare
= (struct yaffs_NANDSpare
*)spare
;
56 spare
->tagByte0
= oob
[0];
57 spare
->tagByte1
= oob
[1];
58 spare
->tagByte2
= oob
[2];
59 spare
->tagByte3
= oob
[3];
60 spare
->tagByte4
= oob
[4];
61 spare
->tagByte5
= oob
[5] == 0xff ? 0xff : oob
[5] & 0x3f;
62 spare
->blockStatus
= oob
[5] & 0x80 ? 0xff : 'Y';
63 spare
->pageStatus
= oob
[5] & 0x40 ? 0xff : 0;
64 spare
->ecc1
[0] = spare
->ecc1
[1] = spare
->ecc1
[2] = 0xff;
65 spare
->tagByte6
= oob
[6];
66 spare
->tagByte7
= oob
[7];
67 spare
->ecc2
[0] = spare
->ecc2
[1] = spare
->ecc2
[2] = 0xff;
69 nspare
->eccres1
= nspare
->eccres2
= 0; /* FIXME */
73 int nandmtd_WriteChunkToNAND(yaffs_Device
* dev
, int chunkInNAND
,
74 const __u8
* data
, const yaffs_Spare
* spare
)
76 struct mtd_info
*mtd
= (struct mtd_info
*)(dev
->genericDevice
);
77 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
78 struct mtd_oob_ops ops
;
83 loff_t addr
= ((loff_t
) chunkInNAND
) * dev
->nDataBytesPerChunk
;
84 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
85 __u8 spareAsBytes
[8]; /* OOB */
88 retval
= mtd
->write(mtd
, addr
, dev
->nDataBytesPerChunk
,
91 if (dev
->useNANDECC
) {
92 translate_spare2oob(spare
, spareAsBytes
);
93 ops
.mode
= MTD_OOB_AUTO
;
94 ops
.ooblen
= 8; /* temp hack */
96 ops
.mode
= MTD_OOB_RAW
;
97 ops
.ooblen
= YAFFS_BYTES_PER_SPARE
;
99 ops
.len
= data
? dev
->nDataBytesPerChunk
: ops
.ooblen
;
100 ops
.datbuf
= (u8
*)data
;
102 ops
.oobbuf
= spareAsBytes
;
103 retval
= mtd
->write_oob(mtd
, addr
, &ops
);
106 __u8
*spareAsBytes
= (__u8
*) spare
;
111 mtd
->write_ecc(mtd
, addr
, dev
->nDataBytesPerChunk
,
112 &dummy
, data
, spareAsBytes
,
116 mtd
->write_ecc(mtd
, addr
, dev
->nDataBytesPerChunk
,
117 &dummy
, data
, spareAsBytes
,
122 mtd
->write(mtd
, addr
, dev
->nDataBytesPerChunk
, &dummy
,
126 mtd
->write_oob(mtd
, addr
, YAFFS_BYTES_PER_SPARE
,
127 &dummy
, spareAsBytes
);
137 int nandmtd_ReadChunkFromNAND(yaffs_Device
* dev
, int chunkInNAND
, __u8
* data
,
140 struct mtd_info
*mtd
= (struct mtd_info
*)(dev
->genericDevice
);
141 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
142 struct mtd_oob_ops ops
;
147 loff_t addr
= ((loff_t
) chunkInNAND
) * dev
->nDataBytesPerChunk
;
148 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
149 __u8 spareAsBytes
[8]; /* OOB */
152 retval
= mtd
->read(mtd
, addr
, dev
->nDataBytesPerChunk
,
155 if (dev
->useNANDECC
) {
156 ops
.mode
= MTD_OOB_AUTO
;
157 ops
.ooblen
= 8; /* temp hack */
159 ops
.mode
= MTD_OOB_RAW
;
160 ops
.ooblen
= YAFFS_BYTES_PER_SPARE
;
162 ops
.len
= data
? dev
->nDataBytesPerChunk
: ops
.ooblen
;
165 ops
.oobbuf
= spareAsBytes
;
166 retval
= mtd
->read_oob(mtd
, addr
, &ops
);
168 translate_oob2spare(spare
, spareAsBytes
);
171 __u8
*spareAsBytes
= (__u8
*) spare
;
174 if (dev
->useNANDECC
) {
175 /* Careful, this call adds 2 ints */
176 /* to the end of the spare data. Calling function */
177 /* should allocate enough memory for spare, */
178 /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
180 mtd
->read_ecc(mtd
, addr
, dev
->nDataBytesPerChunk
,
181 &dummy
, data
, spareAsBytes
,
185 mtd
->read_ecc(mtd
, addr
, dev
->nDataBytesPerChunk
,
186 &dummy
, data
, spareAsBytes
,
192 mtd
->read(mtd
, addr
, dev
->nDataBytesPerChunk
, &dummy
,
196 mtd
->read_oob(mtd
, addr
, YAFFS_BYTES_PER_SPARE
,
197 &dummy
, spareAsBytes
);
207 int nandmtd_EraseBlockInNAND(yaffs_Device
* dev
, int blockNumber
)
209 struct mtd_info
*mtd
= (struct mtd_info
*)(dev
->genericDevice
);
211 ((loff_t
) blockNumber
) * dev
->nDataBytesPerChunk
212 * dev
->nChunksPerBlock
;
213 struct erase_info ei
;
218 ei
.len
= dev
->nDataBytesPerChunk
* dev
->nChunksPerBlock
;
222 ei
.priv
= (u_long
) dev
;
224 /* Todo finish off the ei if required */
226 sema_init(&dev
->sem
, 0);
228 retval
= mtd
->erase(mtd
, &ei
);
236 int nandmtd_InitialiseNAND(yaffs_Device
* dev
)