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