cleanup mtd, implement jffs2write - one step closer to config preserving system upgrades
[openwrt-10.03/.git] / package / mtd / src / mtd-api.h
1
2 /* $Id: mtd.h,v 1.38 2003/01/12 16:30:19 spse Exp $ */
3
4 #ifndef __MTD_MTD_H__
5 #define __MTD_MTD_H__
6
7 #ifdef __KERNEL__
8
9 #include <linux/config.h>
10 #include <linux/version.h>
11 #include <linux/types.h>
12 #include <linux/mtd/compatmac.h>
13 #include <linux/notifier.h>
14 #include <linux/module.h>
15 #include <linux/uio.h>
16
17 #endif /* __KERNEL__ */
18
19 struct erase_info_user {
20         u_int32_t start;
21         u_int32_t length;
22 };
23
24 struct mtd_oob_buf {
25         u_int32_t start;
26         u_int32_t length;
27         unsigned char *ptr;
28 };
29
30
31 #define MTD_CHAR_MAJOR 90
32 #define MTD_BLOCK_MAJOR 31
33 #define MAX_MTD_DEVICES 16
34
35
36
37 #define MTD_ABSENT              0
38 #define MTD_RAM                 1
39 #define MTD_ROM                 2
40 #define MTD_NORFLASH            3
41 #define MTD_NANDFLASH           4
42 #define MTD_PEROM               5
43 #define MTD_OTHER               14
44 #define MTD_UNKNOWN             15
45
46
47
48 #define MTD_CLEAR_BITS          1       // Bits can be cleared (flash)
49 #define MTD_SET_BITS            2       // Bits can be set
50 #define MTD_ERASEABLE           4       // Has an erase function
51 #define MTD_WRITEB_WRITEABLE    8       // Direct IO is possible
52 #define MTD_VOLATILE            16      // Set for RAMs
53 #define MTD_XIP                 32      // eXecute-In-Place possible
54 #define MTD_OOB                 64      // Out-of-band data (NAND flash)
55 #define MTD_ECC                 128     // Device capable of automatic ECC
56
57 // Some common devices / combinations of capabilities
58 #define MTD_CAP_ROM             0
59 #define MTD_CAP_RAM             (MTD_CLEAR_BITS|MTD_SET_BITS|MTD_WRITEB_WRITEABLE)
60 #define MTD_CAP_NORFLASH        (MTD_CLEAR_BITS|MTD_ERASEABLE)
61 #define MTD_CAP_NANDFLASH       (MTD_CLEAR_BITS|MTD_ERASEABLE|MTD_OOB)
62 #define MTD_WRITEABLE           (MTD_CLEAR_BITS|MTD_SET_BITS)
63
64
65 // Types of automatic ECC/Checksum available
66 #define MTD_ECC_NONE            0       // No automatic ECC available
67 #define MTD_ECC_RS_DiskOnChip   1       // Automatic ECC on DiskOnChip
68 #define MTD_ECC_SW              2       // SW ECC for Toshiba & Samsung devices
69
70 struct mtd_info_user {
71         u_char type;
72         u_int32_t flags;
73         u_int32_t size;  // Total size of the MTD
74         u_int32_t erasesize;
75         u_int32_t oobblock;  // Size of OOB blocks (e.g. 512)
76         u_int32_t oobsize;   // Amount of OOB data per block (e.g. 16)
77         u_int32_t ecctype;
78         u_int32_t eccsize;
79 };
80
81 struct region_info_user {
82         u_int32_t offset;               /* At which this region starts, 
83                                          * from the beginning of the MTD */
84         u_int32_t erasesize;            /* For this region */
85         u_int32_t numblocks;            /* Number of blocks in this region */
86         u_int32_t regionindex;
87 };
88
89 #define MEMGETINFO              _IOR('M', 1, struct mtd_info_user)
90 #define MEMERASE                _IOW('M', 2, struct erase_info_user)
91 #define MEMWRITEOOB             _IOWR('M', 3, struct mtd_oob_buf)
92 #define MEMREADOOB              _IOWR('M', 4, struct mtd_oob_buf)
93 #define MEMLOCK                 _IOW('M', 5, struct erase_info_user)
94 #define MEMUNLOCK               _IOW('M', 6, struct erase_info_user)
95 #define MEMGETREGIONCOUNT       _IOR('M', 7, int)
96 #define MEMGETREGIONINFO        _IOWR('M', 8, struct region_info_user)
97 #define MEMREADDATA             _IOWR('M', 9, struct mtd_oob_buf)
98 #define MEMWRITEDATA            _IOWR('M', 10, struct mtd_oob_buf)
99 #define MTDREFRESH                              _IO('M', 23)
100
101 #ifndef __KERNEL__
102
103 typedef struct mtd_info_user mtd_info_t;
104 typedef struct erase_info_user erase_info_t;
105 typedef struct region_info_user region_info_t;
106
107         /* User-space ioctl definitions */
108
109
110 #else /* __KERNEL__ */
111
112
113 #define MTD_ERASE_PENDING       0x01
114 #define MTD_ERASING             0x02
115 #define MTD_ERASE_SUSPEND       0x04
116 #define MTD_ERASE_DONE          0x08
117 #define MTD_ERASE_FAILED        0x10
118
119 struct erase_info {
120         struct mtd_info *mtd;
121         u_int32_t addr;
122         u_int32_t len;
123         u_long time;
124         u_long retries;
125         u_int dev;
126         u_int cell;
127         void (*callback) (struct erase_info *self);
128         u_long priv;
129         u_char state;
130         struct erase_info *next;
131 };
132
133 struct mtd_erase_region_info {
134         u_int32_t offset;                       /* At which this region starts, from the beginning of the MTD */
135         u_int32_t erasesize;            /* For this region */
136         u_int32_t numblocks;            /* Number of blocks of erasesize in this region */
137 };
138
139 struct mtd_info {
140         u_char type;
141         u_int32_t flags;
142         u_int32_t size;  // Total size of the MTD
143
144         /* "Major" erase size for the device. Naïve users may take this
145          * to be the only erase size available, or may use the more detailed
146          * information below if they desire
147          */
148         u_int32_t erasesize;
149
150         u_int32_t oobblock;  // Size of OOB blocks (e.g. 512)
151         u_int32_t oobsize;   // Amount of OOB data per block (e.g. 16)
152         u_int32_t ecctype;
153         u_int32_t eccsize;
154
155         // Kernel-only stuff starts here.
156         char *name;
157         int index;
158
159         /* Data for variable erase regions. If numeraseregions is zero,
160          * it means that the whole device has erasesize as given above. 
161          */
162         int numeraseregions;
163         struct mtd_erase_region_info *eraseregions; 
164
165         /* This really shouldn't be here. It can go away in 2.5 */
166         u_int32_t bank_size;
167
168         struct module *module;
169         int (*erase) (struct mtd_info *mtd, struct erase_info *instr);
170
171         /* This stuff for eXecute-In-Place */
172         int (*point) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf);
173
174         /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
175         void (*unpoint) (struct mtd_info *mtd, u_char * addr, loff_t from, size_t len);
176
177
178         int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
179         int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
180
181         int (*read_ecc) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel);
182         int (*write_ecc) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel);
183
184         int (*read_oob) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
185         int (*write_oob) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
186
187         /* 
188          * Methods to access the protection register area, present in some 
189          * flash devices. The user data is one time programmable but the
190          * factory data is read only. 
191          */
192         int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
193
194         int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
195
196         /* This function is not yet implemented */
197         int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
198
199         /* iovec-based read/write methods. We need these especially for NAND flash,
200            with its limited number of write cycles per erase.
201            NB: The 'count' parameter is the number of _vectors_, each of 
202            which contains an (ofs, len) tuple.
203         */
204         int (*readv) (struct mtd_info *mtd, struct iovec *vecs, unsigned long count, loff_t from, size_t *retlen);
205         int (*readv_ecc) (struct mtd_info *mtd, struct iovec *vecs, unsigned long count, loff_t from, 
206                 size_t *retlen, u_char *eccbuf, int oobsel);
207         int (*writev) (struct mtd_info *mtd, const struct iovec *vecs, unsigned long count, loff_t to, size_t *retlen);
208         int (*writev_ecc) (struct mtd_info *mtd, const struct iovec *vecs, unsigned long count, loff_t to, 
209                 size_t *retlen, u_char *eccbuf, int oobsel);
210
211         /* Sync */
212         void (*sync) (struct mtd_info *mtd);
213
214         /* Chip-supported device locking */
215         int (*lock) (struct mtd_info *mtd, loff_t ofs, size_t len);
216         int (*unlock) (struct mtd_info *mtd, loff_t ofs, size_t len);
217
218         /* Power Management functions */
219         int (*suspend) (struct mtd_info *mtd);
220         void (*resume) (struct mtd_info *mtd);
221
222         struct notifier_block reboot_notifier;
223
224         void *priv;
225 };
226
227
228         /* Kernel-side ioctl definitions */
229
230 extern int add_mtd_device(struct mtd_info *mtd);
231 extern int del_mtd_device (struct mtd_info *mtd);
232
233 extern struct mtd_info *__get_mtd_device(struct mtd_info *mtd, int num);
234
235 static inline struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
236 {
237         struct mtd_info *ret;
238         
239         ret = __get_mtd_device(mtd, num);
240
241         if (ret && ret->module && !try_inc_mod_count(ret->module))
242                 return NULL;
243
244         return ret;
245 }
246
247 static inline void put_mtd_device(struct mtd_info *mtd)
248 {
249        if (mtd->module)
250                __MOD_DEC_USE_COUNT(mtd->module);
251 }
252
253
254 struct mtd_notifier {
255         void (*add)(struct mtd_info *mtd);
256         void (*remove)(struct mtd_info *mtd);
257         struct mtd_notifier *next;
258 };
259
260
261 extern void register_mtd_user (struct mtd_notifier *new);
262 extern int unregister_mtd_user (struct mtd_notifier *old);
263
264 int default_mtd_writev(struct mtd_info *mtd, const struct iovec *vecs,
265                        unsigned long count, loff_t to, size_t *retlen);
266
267 int default_mtd_readv(struct mtd_info *mtd, struct iovec *vecs,
268                       unsigned long count, loff_t from, size_t *retlen);
269
270 #ifndef MTDC
271 #define MTD_ERASE(mtd, args...) (*(mtd->erase))(mtd, args)
272 #define MTD_POINT(mtd, a,b,c,d) (*(mtd->point))(mtd, a,b,c, (u_char **)(d))
273 #define MTD_UNPOINT(mtd, arg) (*(mtd->unpoint))(mtd, (u_char *)arg)
274 #define MTD_READ(mtd, args...) (*(mtd->read))(mtd, args)
275 #define MTD_WRITE(mtd, args...) (*(mtd->write))(mtd, args)
276 #define MTD_READV(mtd, args...) (*(mtd->readv))(mtd, args)
277 #define MTD_WRITEV(mtd, args...) (*(mtd->writev))(mtd, args)
278 #define MTD_READECC(mtd, args...) (*(mtd->read_ecc))(mtd, args)
279 #define MTD_WRITEECC(mtd, args...) (*(mtd->write_ecc))(mtd, args)
280 #define MTD_READOOB(mtd, args...) (*(mtd->read_oob))(mtd, args)
281 #define MTD_WRITEOOB(mtd, args...) (*(mtd->write_oob))(mtd, args)
282 #define MTD_SYNC(mtd) do { if (mtd->sync) (*(mtd->sync))(mtd);  } while (0) 
283 #endif /* MTDC */
284
285 /*
286  * Debugging macro and defines
287  */
288 #define MTD_DEBUG_LEVEL0        (0)     /* Quiet   */
289 #define MTD_DEBUG_LEVEL1        (1)     /* Audible */
290 #define MTD_DEBUG_LEVEL2        (2)     /* Loud    */
291 #define MTD_DEBUG_LEVEL3        (3)     /* Noisy   */
292
293 #ifdef CONFIG_MTD_DEBUG
294 #define DEBUG(n, args...)                               \
295         do {                                            \
296                 if (n <= CONFIG_MTD_DEBUG_VERBOSE)      \
297                         printk(KERN_INFO args);         \
298         } while(0)
299 #else /* CONFIG_MTD_DEBUG */
300 #define DEBUG(n, args...)
301 #endif /* CONFIG_MTD_DEBUG */
302
303 #endif /* __KERNEL__ */
304
305 #endif /* __MTD_MTD_H__ */