lindent
[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.10 2005-08-11 01:07:43 marty Exp $";
18
19 #include "yportenv.h"
20
21 #ifdef CONFIG_YAFFS_YAFFS1
22
23 #include "yaffs_mtdif.h"
24
25 #include "linux/mtd/mtd.h"
26 #include "linux/types.h"
27 #include "linux/time.h"
28 #include "linux/mtd/nand.h"
29
30 static struct nand_oobinfo yaffs_oobinfo = {
31         .useecc = 1,
32 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8))
33         .eccbytes = 6,
34 #endif
35         .eccpos = {8, 9, 10, 13, 14, 15}
36 };
37
38 static struct nand_oobinfo yaffs_noeccinfo = {
39         .useecc = 0,
40 };
41
42 int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
43                              const __u8 * data, const yaffs_Spare * spare)
44 {
45         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
46         size_t dummy;
47         int retval = 0;
48
49         loff_t addr = ((loff_t) chunkInNAND) * dev->nBytesPerChunk;
50
51         __u8 *spareAsBytes = (__u8 *) spare;
52
53         if (data && spare) {
54                 if (dev->useNANDECC)
55                         retval =
56                             mtd->write_ecc(mtd, addr, dev->nBytesPerChunk,
57                                            &dummy, data, spareAsBytes,
58                                            &yaffs_oobinfo);
59                 else
60                         retval =
61                             mtd->write_ecc(mtd, addr, dev->nBytesPerChunk,
62                                            &dummy, data, spareAsBytes,
63                                            &yaffs_noeccinfo);
64         } else {
65                 if (data)
66                         retval =
67                             mtd->write(mtd, addr, dev->nBytesPerChunk, &dummy,
68                                        data);
69                 if (spare)
70                         retval =
71                             mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
72                                            &dummy, spareAsBytes);
73         }
74
75         if (retval == 0)
76                 return YAFFS_OK;
77         else
78                 return YAFFS_FAIL;
79 }
80
81 int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
82                               yaffs_Spare * spare)
83 {
84         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
85         size_t dummy;
86         int retval = 0;
87
88         loff_t addr = ((loff_t) chunkInNAND) * dev->nBytesPerChunk;
89
90         __u8 *spareAsBytes = (__u8 *) spare;
91
92         if (data && spare) {
93                 if (dev->useNANDECC) {  /* Careful, this call adds 2 ints */
94                         /* to the end of the spare data.  Calling function */
95                         /* should allocate enough memory for spare, */
96                         /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
97                         retval =
98                             mtd->read_ecc(mtd, addr, dev->nBytesPerChunk,
99                                           &dummy, data, spareAsBytes,
100                                           &yaffs_oobinfo);
101                 } else {
102                         retval =
103                             mtd->read_ecc(mtd, addr, dev->nBytesPerChunk,
104                                           &dummy, data, spareAsBytes,
105                                           &yaffs_noeccinfo);
106                 }
107         } else {
108                 if (data)
109                         retval =
110                             mtd->read(mtd, addr, dev->nBytesPerChunk, &dummy,
111                                       data);
112                 if (spare)
113                         retval =
114                             mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
115                                           &dummy, spareAsBytes);
116         }
117
118         if (retval == 0)
119                 return YAFFS_OK;
120         else
121                 return YAFFS_FAIL;
122 }
123
124 int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
125 {
126         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
127         __u32 addr =
128             ((loff_t) blockNumber) * dev->nBytesPerChunk
129                 * dev->nChunksPerBlock;
130         struct erase_info ei;
131         int retval = 0;
132
133         ei.mtd = mtd;
134         ei.addr = addr;
135         ei.len = dev->nBytesPerChunk * dev->nChunksPerBlock;
136         ei.time = 1000;
137         ei.retries = 2;
138         ei.callback = NULL;
139         ei.priv = (u_long) dev;
140
141         /* Todo finish off the ei if required */
142
143         sema_init(&dev->sem, 0);
144
145         retval = mtd->erase(mtd, &ei);
146
147         if (retval == 0)
148                 return YAFFS_OK;
149         else
150                 return YAFFS_FAIL;
151 }
152
153 int nandmtd_InitialiseNAND(yaffs_Device * dev)
154 {
155         return YAFFS_OK;
156 }
157
158 #endif  /* CONFIG_YAFFS_YAFFS1 */