Applied patch 03-read-super-MTD-if-CONFIG_YAFFS-or-YAFFS2_MTD_ENABLED.diff
[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 = "$Id: yaffs_mtdif.c,v 1.2 2005-07-19 20:41:59 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         eccpos: {8, 9, 10, 13, 14, 15}
35 };
36
37 struct nand_oobinfo yaffs_noeccinfo = {
38         useecc: 0,
39 };
40
41
42 int nandmtd_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,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 #ifndef CONFIG_YAFFS_USE_OLD_MTD
53         if(data && spare)
54         {
55                 if(dev->useNANDECC)
56                         mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo);
57                 else
58                         mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo);
59         }
60         else
61         {
62 #endif
63         if(data)
64                 retval = mtd->write(mtd,addr,dev->nBytesPerChunk,&dummy,data);
65         if(spare)
66                 retval = mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
67 #ifndef CONFIG_YAFFS_USE_OLD_MTD
68         }
69 #endif
70
71     if (retval == 0)
72         return YAFFS_OK;
73     else
74         return YAFFS_FAIL;
75 }
76
77 int nandmtd_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
78 {
79         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
80         size_t dummy;
81     int retval = 0;
82         
83         loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk;
84         
85         __u8 *spareAsBytes = (__u8 *)spare;
86         
87 #ifndef CONFIG_YAFFS_USE_OLD_MTD
88         if(data && spare)
89         {
90                 if(dev->useNANDECC)
91                 {   // Careful, this call adds 2 ints to the end of the spare data.  Calling function should
92             // allocate enough memory for spare, i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)].
93                 retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo);            
94                 }
95                 else
96                 {
97                         retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo);
98                 }
99         }
100         else
101         {
102 #endif
103         if(data)
104                 retval = mtd->read(mtd,addr,dev->nBytesPerChunk,&dummy,data);
105         if(spare)
106                 retval = mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
107 #ifndef CONFIG_YAFFS_USE_OLD_MTD
108         }
109 #endif
110
111     if (retval == 0)
112         return YAFFS_OK;
113     else
114         return YAFFS_FAIL;
115 }
116
117 // Callback not needed for NAND
118 #if 0
119 static void nandmtd_EraseCallback(struct erase_info *ei)
120 {
121         yaffs_Device *dev = (yaffs_Device *)ei->priv;   
122         up(&dev->sem);
123 }
124 #endif
125
126
127 int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
128 {
129         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
130         __u32 addr = ((loff_t) blockNumber) * dev->nBytesPerChunk * dev->nChunksPerBlock;
131         struct erase_info ei;
132     int retval = 0;
133         
134         ei.mtd = mtd;
135         ei.addr = addr;
136         ei.len = dev->nBytesPerChunk * dev->nChunksPerBlock;
137         ei.time = 1000;
138         ei.retries = 2;
139         ei.callback = NULL;
140         ei.priv = (u_long)dev;
141         
142         // Todo finish off the ei if required
143         
144         sema_init(&dev->sem,0);
145
146         retval = mtd->erase(mtd,&ei);   
147         
148         //No need for callback 
149         // down(&dev->sem); // Wait for the erasure to complete
150
151     if (retval == 0)    
152         return YAFFS_OK;
153     else
154         return YAFFS_FAIL;
155 }
156
157 int nandmtd_InitialiseNAND(yaffs_Device *dev)
158 {
159         return YAFFS_OK;
160 }
161
162 #endif // CONFIG_YAFFS_MTD_ENABLED
163