fd7b15d077b311586ab83c19588bcf5abe5406db
[linux-2.6.git] / include / linux / mtd / mtd.h
1 /* 
2  * $Id: mtd.h,v 1.54 2004/07/15 01:13:12 dwmw2 Exp $
3  *
4  * Copyright (C) 1999-2003 David Woodhouse <dwmw2@infradead.org> et al.
5  *
6  * Released under GPL
7  */
8
9 #ifndef __MTD_MTD_H__
10 #define __MTD_MTD_H__
11
12 #ifndef __KERNEL__
13 #error This is a kernel header. Perhaps include mtd-user.h instead?
14 #endif
15
16 #include <linux/config.h>
17 #include <linux/version.h>
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/uio.h>
21
22 #include <mtd/mtd-abi.h>
23
24 #define MTD_CHAR_MAJOR 90
25 #define MTD_BLOCK_MAJOR 31
26 #define MAX_MTD_DEVICES 16
27
28 #define MTD_ERASE_PENDING       0x01
29 #define MTD_ERASING             0x02
30 #define MTD_ERASE_SUSPEND       0x04
31 #define MTD_ERASE_DONE          0x08
32 #define MTD_ERASE_FAILED        0x10
33
34 /* If the erase fails, fail_addr might indicate exactly which block failed.  If
35    fail_addr = 0xffffffff, the failure was not at the device level or was not
36    specific to any particular block. */
37 struct erase_info {
38         struct mtd_info *mtd;
39         u_int32_t addr;
40         u_int32_t len;
41         u_int32_t fail_addr;
42         u_long time;
43         u_long retries;
44         u_int dev;
45         u_int cell;
46         void (*callback) (struct erase_info *self);
47         u_long priv;
48         u_char state;
49         struct erase_info *next;
50 };
51
52 struct mtd_erase_region_info {
53         u_int32_t offset;                       /* At which this region starts, from the beginning of the MTD */
54         u_int32_t erasesize;            /* For this region */
55         u_int32_t numblocks;            /* Number of blocks of erasesize in this region */
56 };
57
58 struct mtd_info {
59         u_char type;
60         u_int32_t flags;
61         u_int32_t size;  // Total size of the MTD
62
63         /* "Major" erase size for the device. Naïve users may take this
64          * to be the only erase size available, or may use the more detailed
65          * information below if they desire
66          */
67         u_int32_t erasesize;
68
69         u_int32_t oobblock;  // Size of OOB blocks (e.g. 512)
70         u_int32_t oobsize;   // Amount of OOB data per block (e.g. 16)
71         u_int32_t oobavail;  // Number of bytes in OOB area available for fs 
72         u_int32_t ecctype;
73         u_int32_t eccsize;
74         
75
76         // Kernel-only stuff starts here.
77         char *name;
78         int index;
79
80         // oobinfo is a nand_oobinfo structure, which can be set by iotcl (MEMSETOOBINFO)
81         struct nand_oobinfo oobinfo;
82
83         /* Data for variable erase regions. If numeraseregions is zero,
84          * it means that the whole device has erasesize as given above. 
85          */
86         int numeraseregions;
87         struct mtd_erase_region_info *eraseregions; 
88
89         /* This really shouldn't be here. It can go away in 2.5 */
90         u_int32_t bank_size;
91
92         int (*erase) (struct mtd_info *mtd, struct erase_info *instr);
93
94         /* This stuff for eXecute-In-Place */
95         int (*point) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf);
96
97         /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
98         void (*unpoint) (struct mtd_info *mtd, u_char * addr, loff_t from, size_t len);
99
100
101         int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
102         int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
103
104         int (*read_ecc) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
105         int (*write_ecc) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
106
107         int (*read_oob) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
108         int (*write_oob) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
109
110         /* 
111          * Methods to access the protection register area, present in some 
112          * flash devices. The user data is one time programmable but the
113          * factory data is read only. 
114          */
115         int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
116
117         int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
118
119         /* This function is not yet implemented */
120         int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
121
122         /* kvec-based read/write methods. We need these especially for NAND flash,
123            with its limited number of write cycles per erase.
124            NB: The 'count' parameter is the number of _vectors_, each of 
125            which contains an (ofs, len) tuple.
126         */
127         int (*readv) (struct mtd_info *mtd, struct kvec *vecs, unsigned long count, loff_t from, size_t *retlen);
128         int (*readv_ecc) (struct mtd_info *mtd, struct kvec *vecs, unsigned long count, loff_t from, 
129                 size_t *retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
130         int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen);
131         int (*writev_ecc) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, 
132                 size_t *retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
133
134         /* Sync */
135         void (*sync) (struct mtd_info *mtd);
136
137         /* Chip-supported device locking */
138         int (*lock) (struct mtd_info *mtd, loff_t ofs, size_t len);
139         int (*unlock) (struct mtd_info *mtd, loff_t ofs, size_t len);
140
141         /* Power Management functions */
142         int (*suspend) (struct mtd_info *mtd);
143         void (*resume) (struct mtd_info *mtd);
144
145         /* Bad block management functions */
146         int (*block_isbad) (struct mtd_info *mtd, loff_t ofs);
147         int (*block_markbad) (struct mtd_info *mtd, loff_t ofs);
148
149         void *priv;
150
151         struct module *owner;
152         int usecount;
153 };
154
155
156         /* Kernel-side ioctl definitions */
157
158 extern int add_mtd_device(struct mtd_info *mtd);
159 extern int del_mtd_device (struct mtd_info *mtd);
160
161 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
162
163 extern void put_mtd_device(struct mtd_info *mtd);
164
165
166 struct mtd_notifier {
167         void (*add)(struct mtd_info *mtd);
168         void (*remove)(struct mtd_info *mtd);
169         struct list_head list;
170 };
171
172
173 extern void register_mtd_user (struct mtd_notifier *new);
174 extern int unregister_mtd_user (struct mtd_notifier *old);
175
176 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
177                        unsigned long count, loff_t to, size_t *retlen);
178
179 int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs,
180                       unsigned long count, loff_t from, size_t *retlen);
181
182 #define MTD_ERASE(mtd, args...) (*(mtd->erase))(mtd, args)
183 #define MTD_POINT(mtd, a,b,c,d) (*(mtd->point))(mtd, a,b,c, (u_char **)(d))
184 #define MTD_UNPOINT(mtd, arg) (*(mtd->unpoint))(mtd, (u_char *)arg)
185 #define MTD_READ(mtd, args...) (*(mtd->read))(mtd, args)
186 #define MTD_WRITE(mtd, args...) (*(mtd->write))(mtd, args)
187 #define MTD_READV(mtd, args...) (*(mtd->readv))(mtd, args)
188 #define MTD_WRITEV(mtd, args...) (*(mtd->writev))(mtd, args)
189 #define MTD_READECC(mtd, args...) (*(mtd->read_ecc))(mtd, args)
190 #define MTD_WRITEECC(mtd, args...) (*(mtd->write_ecc))(mtd, args)
191 #define MTD_READOOB(mtd, args...) (*(mtd->read_oob))(mtd, args)
192 #define MTD_WRITEOOB(mtd, args...) (*(mtd->write_oob))(mtd, args)
193 #define MTD_SYNC(mtd) do { if (mtd->sync) (*(mtd->sync))(mtd);  } while (0) 
194
195 /*
196  * Debugging macro and defines
197  */
198 #define MTD_DEBUG_LEVEL0        (0)     /* Quiet   */
199 #define MTD_DEBUG_LEVEL1        (1)     /* Audible */
200 #define MTD_DEBUG_LEVEL2        (2)     /* Loud    */
201 #define MTD_DEBUG_LEVEL3        (3)     /* Noisy   */
202
203 #ifdef CONFIG_MTD_DEBUG
204 #define DEBUG(n, args...)                               \
205         do {                                            \
206                 if (n <= CONFIG_MTD_DEBUG_VERBOSE)      \
207                         printk(KERN_INFO args);         \
208         } while(0)
209 #else /* CONFIG_MTD_DEBUG */
210 #define DEBUG(n, args...) do { } while(0)
211
212 #endif /* CONFIG_MTD_DEBUG */
213
214 #endif /* __MTD_MTD_H__ */