*** empty log message ***
[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.5 2002-12-13 00:13:06 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 int nandmtd_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
33 {
34         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
35         size_t dummy;
36     int retval = 0;
37         
38         loff_t addr = ((loff_t)chunkInNAND) * YAFFS_BYTES_PER_CHUNK;
39         
40         __u8 *spareAsBytes = (__u8 *)spare;
41
42 #ifndef CONFIG_YAFFS_USE_OLD_MTD
43         if(data && spare)
44         {
45 #ifdef CONFIG_YAFFS_USE_NANDECC
46                 retval = mtd->write_ecc(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data,spareAsBytes,NAND_YAFFS_OOB);
47 #else
48                 retval = mtd->write_ecc(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data,spareAsBytes,NAND_NONE_OOB);
49 #endif
50         }
51         else
52         {
53 #endif
54         if(data)
55                 retval = mtd->write(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data);
56         if(spare)
57                 retval = mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
58 #ifndef CONFIG_YAFFS_USE_OLD_MTD
59         }
60 #endif
61
62     if (retval == 0)
63         return YAFFS_OK;
64     else
65         return YAFFS_FAIL;
66 }
67
68 int nandmtd_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
69 {
70         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
71         size_t dummy;
72     int retval = 0;
73         
74         loff_t addr = ((loff_t)chunkInNAND) * YAFFS_BYTES_PER_CHUNK;
75         
76         __u8 *spareAsBytes = (__u8 *)spare;
77         
78 #ifndef CONFIG_YAFFS_USE_OLD_MTD
79         if(data && spare)
80         {
81 #ifdef CONFIG_YAFFS_USE_NANDECC
82         u8 tmpSpare[ YAFFS_BYTES_PER_SPARE + (2*sizeof(int)) ];
83                 retval = mtd->read_ecc(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data,tmpSpare,NAND_YAFFS_OOB);
84         memcpy(spareAsBytes, tmpSpare, YAFFS_BYTES_PER_SPARE);
85 #else
86                 retval = mtd->read_ecc(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data,spareAsBytes,NAND_NONE_OOB);
87 #endif  
88         }
89         else
90         {
91 #endif
92         if(data)
93                 retval = mtd->read(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data);
94         if(spare)
95                 retval = mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
96 #ifndef CONFIG_YAFFS_USE_OLD_MTD
97         }
98 #endif
99
100     if (retval == 0)
101         return YAFFS_OK;
102     else
103         return YAFFS_FAIL;
104 }
105
106
107 static void nandmtd_EraseCallback(struct erase_info *ei)
108 {
109         yaffs_Device *dev = (yaffs_Device *)ei->priv;   
110         up(&dev->sem);
111 }
112
113
114 int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
115 {
116         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
117         __u32 addr = ((loff_t) blockNumber) * YAFFS_BYTES_PER_BLOCK;
118         struct erase_info ei;
119     int retval = 0;
120         
121         ei.mtd = mtd;
122         ei.addr = addr;
123         ei.len = YAFFS_BYTES_PER_BLOCK;
124         ei.time = 1000;
125         ei.retries = 2;
126         ei.callback = nandmtd_EraseCallback;
127         ei.priv = (u_long)dev;
128         
129         // Todo finish off the ei if required
130         
131         sema_init(&dev->sem,0);
132
133         retval = mtd->erase(mtd,&ei);   
134         
135         down(&dev->sem); // Wait for the erasure to complete
136
137     if (retval == 0)    
138         return YAFFS_OK;
139     else
140         return YAFFS_FAIL;
141 }
142
143 int nandmtd_InitialiseNAND(yaffs_Device *dev)
144 {
145         return YAFFS_OK;
146 }
147
148 #endif // CONFIG_YAFFS_MTD_ENABLED
149