Check the return value of mtd->write_ecc() calls.
[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.9 2005-08-02 19:18:44 luc Exp $";
17  
18 #include "yportenv.h"
19
20 #ifdef CONFIG_YAFFS_YAFFS1
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
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         if(data && spare)
53         {
54                 if(dev->useNANDECC)
55                         retval = mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo);
56                 else
57                         retval = mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo);
58         }
59         else
60         {
61         if(data)
62                 retval = mtd->write(mtd,addr,dev->nBytesPerChunk,&dummy,data);
63         if(spare)
64                 retval = mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
65         }
66
67     if (retval == 0)
68         return YAFFS_OK;
69     else
70         return YAFFS_FAIL;
71 }
72
73 int nandmtd_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
74 {
75         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
76         size_t dummy;
77     int retval = 0;
78         
79         loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk;
80         
81         __u8 *spareAsBytes = (__u8 *)spare;
82         
83         if(data && spare)
84         {
85                 if(dev->useNANDECC)
86                 {   // Careful, this call adds 2 ints to the end of the spare data.  Calling function should
87             // allocate enough memory for spare, i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)].
88                 retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo);            
89                 }
90                 else
91                 {
92                         retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo);
93                 }
94         }
95         else
96         {
97         if(data)
98                 retval = mtd->read(mtd,addr,dev->nBytesPerChunk,&dummy,data);
99         if(spare)
100                 retval = mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
101         }
102
103     if (retval == 0)
104         return YAFFS_OK;
105     else
106         return YAFFS_FAIL;
107 }
108
109 // Callback not needed for NAND
110 #if 0
111 static void nandmtd_EraseCallback(struct erase_info *ei)
112 {
113         yaffs_Device *dev = (yaffs_Device *)ei->priv;   
114         up(&dev->sem);
115 }
116 #endif
117
118
119 int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
120 {
121         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
122         __u32 addr = ((loff_t) blockNumber) * dev->nBytesPerChunk * dev->nChunksPerBlock;
123         struct erase_info ei;
124     int retval = 0;
125         
126         ei.mtd = mtd;
127         ei.addr = addr;
128         ei.len = dev->nBytesPerChunk * dev->nChunksPerBlock;
129         ei.time = 1000;
130         ei.retries = 2;
131         ei.callback = NULL;
132         ei.priv = (u_long)dev;
133         
134         // Todo finish off the ei if required
135         
136         sema_init(&dev->sem,0);
137
138         retval = mtd->erase(mtd,&ei);   
139         
140         //No need for callback 
141         // down(&dev->sem); // Wait for the erasure to complete
142
143     if (retval == 0)    
144         return YAFFS_OK;
145     else
146         return YAFFS_FAIL;
147 }
148
149 int nandmtd_InitialiseNAND(yaffs_Device *dev)
150 {
151         return YAFFS_OK;
152 }
153
154 #endif // CONFIG_YAFFS_YAFFS1
155