Silly Marty, picked wrong guard for moduleconfig.h
[yaffs2.git] / yaffs_mtdif.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffs_mtdif.c  NAND mtd wrapper functions.
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15
16 const char *yaffs_mtdif_c_version =
17     "$Id: yaffs_mtdif.c,v 1.11 2005-09-20 05:23:41 charles Exp $";
18
19 #include "yportenv.h"
20
21
22 #include "yaffs_mtdif.h"
23
24 #include "linux/mtd/mtd.h"
25 #include "linux/types.h"
26 #include "linux/time.h"
27 #include "linux/mtd/nand.h"
28
29 static struct nand_oobinfo yaffs_oobinfo = {
30         .useecc = 1,
31 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8))
32         .eccbytes = 6,
33 #endif
34         .eccpos = {8, 9, 10, 13, 14, 15}
35 };
36
37 static struct nand_oobinfo yaffs_noeccinfo = {
38         .useecc = 0,
39 };
40
41 int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
42                              const __u8 * data, const yaffs_Spare * spare)
43 {
44         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
45         size_t dummy;
46         int retval = 0;
47
48         loff_t addr = ((loff_t) chunkInNAND) * dev->nBytesPerChunk;
49
50         __u8 *spareAsBytes = (__u8 *) spare;
51
52         if (data && spare) {
53                 if (dev->useNANDECC)
54                         retval =
55                             mtd->write_ecc(mtd, addr, dev->nBytesPerChunk,
56                                            &dummy, data, spareAsBytes,
57                                            &yaffs_oobinfo);
58                 else
59                         retval =
60                             mtd->write_ecc(mtd, addr, dev->nBytesPerChunk,
61                                            &dummy, data, spareAsBytes,
62                                            &yaffs_noeccinfo);
63         } else {
64                 if (data)
65                         retval =
66                             mtd->write(mtd, addr, dev->nBytesPerChunk, &dummy,
67                                        data);
68                 if (spare)
69                         retval =
70                             mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
71                                            &dummy, spareAsBytes);
72         }
73
74         if (retval == 0)
75                 return YAFFS_OK;
76         else
77                 return YAFFS_FAIL;
78 }
79
80 int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
81                               yaffs_Spare * spare)
82 {
83         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
84         size_t dummy;
85         int retval = 0;
86
87         loff_t addr = ((loff_t) chunkInNAND) * dev->nBytesPerChunk;
88
89         __u8 *spareAsBytes = (__u8 *) spare;
90
91         if (data && spare) {
92                 if (dev->useNANDECC) {  /* Careful, this call adds 2 ints */
93                         /* to the end of the spare data.  Calling function */
94                         /* should allocate enough memory for spare, */
95                         /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
96                         retval =
97                             mtd->read_ecc(mtd, addr, dev->nBytesPerChunk,
98                                           &dummy, data, spareAsBytes,
99                                           &yaffs_oobinfo);
100                 } else {
101                         retval =
102                             mtd->read_ecc(mtd, addr, dev->nBytesPerChunk,
103                                           &dummy, data, spareAsBytes,
104                                           &yaffs_noeccinfo);
105                 }
106         } else {
107                 if (data)
108                         retval =
109                             mtd->read(mtd, addr, dev->nBytesPerChunk, &dummy,
110                                       data);
111                 if (spare)
112                         retval =
113                             mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
114                                           &dummy, spareAsBytes);
115         }
116
117         if (retval == 0)
118                 return YAFFS_OK;
119         else
120                 return YAFFS_FAIL;
121 }
122
123 int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
124 {
125         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
126         __u32 addr =
127             ((loff_t) blockNumber) * dev->nBytesPerChunk
128                 * dev->nChunksPerBlock;
129         struct erase_info ei;
130         int retval = 0;
131
132         ei.mtd = mtd;
133         ei.addr = addr;
134         ei.len = dev->nBytesPerChunk * dev->nChunksPerBlock;
135         ei.time = 1000;
136         ei.retries = 2;
137         ei.callback = NULL;
138         ei.priv = (u_long) dev;
139
140         /* Todo finish off the ei if required */
141
142         sema_init(&dev->sem, 0);
143
144         retval = mtd->erase(mtd, &ei);
145
146         if (retval == 0)
147                 return YAFFS_OK;
148         else
149                 return YAFFS_FAIL;
150 }
151
152 int nandmtd_InitialiseNAND(yaffs_Device * dev)
153 {
154         return YAFFS_OK;
155 }
156