Add first-cut Linux kernel patch-in mechanism
[yaffs/.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 = "$Id: yaffs_mtdif.c,v 1.10 2004-09-19 08:14:50 charles Exp $";
17
18 #ifdef CONFIG_YAFFS_MTD_ENABLED
19  
20 #include "yportenv.h"
21
22 #include "yaffs_mtdif.h"
23
24 #include "linux/mtd/mtd.h"
25 #include "linux/types.h"
26 #include "linux/time.h"
27
28 #ifndef CONFIG_YAFFS_USE_OLD_MTD
29 #include "linux/mtd/nand.h"
30 #endif
31
32 struct nand_oobinfo yaffs_oobinfo = {
33         useecc: 1,
34 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8))
35 // this is for versions of mtd nand driver in kernel 2.6.8 and later
36         eccbytes: 6,
37 #endif
38         eccpos: {8, 9, 10, 13, 14, 15}
39 };
40
41 struct nand_oobinfo yaffs_noeccinfo = {
42         useecc: 0,
43 };
44
45
46 int nandmtd_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
47 {
48         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
49         size_t dummy;
50     int retval = 0;
51         
52         loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk;
53         
54         __u8 *spareAsBytes = (__u8 *)spare;
55
56 #ifndef CONFIG_YAFFS_USE_OLD_MTD
57         if(data && spare)
58         {
59                 if(dev->useNANDECC)
60                         mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo);
61                 else
62                         mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo);
63         }
64         else
65         {
66 #endif
67         if(data)
68                 retval = mtd->write(mtd,addr,dev->nBytesPerChunk,&dummy,data);
69         if(spare)
70                 retval = mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
71 #ifndef CONFIG_YAFFS_USE_OLD_MTD
72         }
73 #endif
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, 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 #ifndef CONFIG_YAFFS_USE_OLD_MTD
92         if(data && spare)
93         {
94                 if(dev->useNANDECC)
95                 {   // Careful, this call adds 2 ints to the end of the spare data.  Calling function should
96             // allocate enough memory for spare, i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)].
97                 retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo);            
98                 }
99                 else
100                 {
101                         retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo);
102                 }
103         }
104         else
105         {
106 #endif
107         if(data)
108                 retval = mtd->read(mtd,addr,dev->nBytesPerChunk,&dummy,data);
109         if(spare)
110                 retval = mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
111 #ifndef CONFIG_YAFFS_USE_OLD_MTD
112         }
113 #endif
114
115     if (retval == 0)
116         return YAFFS_OK;
117     else
118         return YAFFS_FAIL;
119 }
120
121 // Callback not needed for NAND
122 #if 0
123 static void nandmtd_EraseCallback(struct erase_info *ei)
124 {
125         yaffs_Device *dev = (yaffs_Device *)ei->priv;   
126         up(&dev->sem);
127 }
128 #endif
129
130
131 int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
132 {
133         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
134         __u32 addr = ((loff_t) blockNumber) * dev->nBytesPerChunk * dev->nChunksPerBlock;
135         struct erase_info ei;
136     int retval = 0;
137         
138         ei.mtd = mtd;
139         ei.addr = addr;
140         ei.len = dev->nBytesPerChunk * dev->nChunksPerBlock;
141         ei.time = 1000;
142         ei.retries = 2;
143         ei.callback = NULL;
144         ei.priv = (u_long)dev;
145         
146         // Todo finish off the ei if required
147         
148         sema_init(&dev->sem,0);
149
150         retval = mtd->erase(mtd,&ei);   
151         
152         //No need for callback 
153         // down(&dev->sem); // Wait for the erasure to complete
154
155     if (retval == 0)    
156         return YAFFS_OK;
157     else
158         return YAFFS_FAIL;
159 }
160
161 int nandmtd_InitialiseNAND(yaffs_Device *dev)
162 {
163         return YAFFS_OK;
164 }
165
166 #endif // CONFIG_YAFFS_MTD_ENABLED
167