Initial revision
[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  #ifdef YAFFS_MTD_ENABLED
16  
17 #include "yportenv.h"
18
19 #include "yaffs_mtdif.h"
20
21 #include "linux/mtd/mtd.h"
22 #include "linux/types.h"
23
24
25 int nandmtd_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
26 {
27         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
28         size_t dummy;
29         
30         loff_t addr = ((loff_t)chunkInNAND) * YAFFS_BYTES_PER_CHUNK;
31         
32         
33         __u8 *spareAsBytes = (__u8 *)spare;
34         
35         if(data)
36                 mtd->write(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data);
37         if(spare)
38                 mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
39
40         return YAFFS_OK;
41 }
42
43
44 int nandmtd_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
45 {
46         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
47         size_t dummy;
48         
49         loff_t addr = ((loff_t)chunkInNAND) * YAFFS_BYTES_PER_CHUNK;
50         
51         __u8 *spareAsBytes = (__u8 *)spare;
52         
53         if(data)
54                 mtd->read(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data);
55         if(spare)
56                 mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes);
57
58         return YAFFS_OK;
59 }
60
61
62 static void nandmtd_EraseCallback(struct erase_info *ei)
63 {
64         yaffs_Device *dev = (yaffs_Device *)ei->priv;   
65         up(&dev->sem);
66 }
67
68
69 int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
70 {
71         
72         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
73         __u32 addr = ((loff_t) blockNumber) * YAFFS_BYTES_PER_BLOCK;
74         struct erase_info ei;
75         
76         ei.mtd = mtd;
77         ei.addr = addr;
78         ei.len = YAFFS_BYTES_PER_BLOCK;
79         ei.time = 1000;
80         ei.retries = 2;
81         ei.callback = nandmtd_EraseCallback;
82         ei.priv = (u_long)dev;
83         
84         // Todo finish off the ei if required
85         
86         sema_init(&dev->sem,0);
87
88         mtd->erase(mtd,&ei);
89         
90         
91         down(&dev->sem); // Wait for the erasure to complete
92                         
93         //Todo, check result of erasure
94         
95         return YAFFS_OK;
96 }
97
98 int nandmtd_InitialiseNAND(yaffs_Device *dev)
99 {
100         return YAFFS_OK;
101 }
102
103 #endif // YAFFS_MTD_ENABLED
104