Adapt the code structure after removing of the yaffsram support.
[yaffs2.git] / yaffs_mtdif2.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 // mtd interface for YAFFS2
17
18 const char *yaffs_mtdif2_c_version = "$Id: yaffs_mtdif2.c,v 1.6 2005-08-01 20:52:35 luc Exp $";
19  
20 #include "yportenv.h"
21
22 #ifdef CONFIG_YAFFS_YAFFS1
23
24 #include "yaffs_mtdif2.h"
25
26 #include "linux/mtd/mtd.h"
27 #include "linux/types.h"
28 #include "linux/time.h"
29
30 #include "yaffs_packedtags2.h"
31
32
33
34
35 int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
36 {
37         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
38         size_t dummy;
39         int retval = 0;
40
41
42         loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk;
43
44         yaffs_PackedTags2 pt;
45         
46         T(YAFFS_TRACE_MTD,(TSTR("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p" TENDSTR),chunkInNAND,data,tags));  
47
48         if(tags)
49         {
50                 yaffs_PackTags2(&pt,tags);
51         }
52
53         if(data && tags)
54         {
55                 if(dev->useNANDECC)
56                   retval = mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,(__u8 *)&pt,NULL);
57                 else
58                   retval = mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,(__u8 *)&pt,NULL);
59         }
60         else
61         {
62         if(data)
63                 retval = mtd->write(mtd,addr,dev->nBytesPerChunk,&dummy,data);
64         if(tags)
65                 retval = mtd->write_oob(mtd,addr,mtd->oobsize,&dummy,(__u8 *)&pt);
66                 
67         }
68
69     if (retval == 0)
70         return YAFFS_OK;
71     else
72         return YAFFS_FAIL;
73 }
74
75 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
76 {
77         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
78         size_t dummy;
79         int retval = 0;
80         
81
82         loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk;
83         
84         yaffs_PackedTags2 pt;
85
86         T(YAFFS_TRACE_MTD,(TSTR("nandmtd2_ReadChunkWithTagsToNAND chunk %d data %p tags %p" TENDSTR),chunkInNAND,data,tags));   
87
88         if(data && tags)
89         {
90                 if(dev->useNANDECC)
91                 {
92                         retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,dev->spareBuffer,NULL);            
93                 }
94                 else
95                 {
96                         retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,dev->spareBuffer,NULL);
97                 }
98         }
99         else
100         {
101         if(data)
102                 retval = mtd->read(mtd,addr,dev->nBytesPerChunk,&dummy,data);
103         if(tags)
104                 retval = mtd->read_oob(mtd,addr,mtd->oobsize,&dummy,dev->spareBuffer);
105         }
106
107     memcpy(&pt,dev->spareBuffer,sizeof(pt));
108     
109     if(tags)
110         yaffs_UnpackTags2(tags,&pt);
111     
112     if (retval == 0)
113         return YAFFS_OK;
114     else
115         return YAFFS_FAIL;
116 }
117
118 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
119 {
120         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
121         int retval;
122         T(YAFFS_TRACE_MTD,(TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR),blockNo));      
123         
124         
125         retval = mtd->block_markbad(mtd,blockNo * dev->nChunksPerBlock * dev->nBytesPerChunk);
126
127     if (retval == 0)
128         return YAFFS_OK;
129     else
130         return YAFFS_FAIL;
131
132 }
133
134 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
135 {
136         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
137         int retval;
138         
139         T(YAFFS_TRACE_MTD,(TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR),blockNo));        
140         retval = mtd->block_isbad(mtd,blockNo* dev->nChunksPerBlock * dev->nBytesPerChunk);
141         
142         if(retval)
143         {
144                 T(YAFFS_TRACE_MTD,(TSTR("block is bad" TENDSTR)));
145         
146                 *state = YAFFS_BLOCK_STATE_DEAD;
147                 *sequenceNumber = 0;
148         }
149         else
150         {
151                 yaffs_ExtendedTags t;
152                 nandmtd2_ReadChunkWithTagsFromNAND(dev,blockNo * dev->nChunksPerBlock,NULL, &t);
153                 
154                 if(t.chunkUsed)
155                 {
156                   *sequenceNumber = t.sequenceNumber;
157                   *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
158                 }
159                 else
160                 {
161                   *sequenceNumber = 0;
162                   *state = YAFFS_BLOCK_STATE_EMPTY;
163                 }
164         }
165                 T(YAFFS_TRACE_MTD,(TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,*state));
166
167     if (retval == 0)
168         return YAFFS_OK;
169     else
170         return YAFFS_FAIL;
171 }
172
173 #endif
174
175
176