2 /* $Id: mtd.h,v 1.35 2002/08/29 21:41:42 gleixner Exp $ */
6 * 19-Sep-2002 Lineo Japan, Inc. add erase-by-force mode
12 * 19-Sep-2002 Lineo Japan, Inc. add erase-by-force mode
13 * 23-Oct-2002 SHARP add definitions for CONFIG_MTD_NAND_LOGICAL_ADDRESS_ACCESS
22 #include <linux/config.h>
23 #include <linux/version.h>
24 #include <linux/types.h>
25 #include <linux/mtd/compatmac.h>
26 #include <linux/module.h>
27 #include <linux/uio.h>
29 #endif /* __KERNEL__ */
31 struct erase_info_user {
43 #define MTD_CHAR_MAJOR 90
44 #define MTD_BLOCK_MAJOR 31
45 #define MAX_MTD_DEVICES 16
52 #define MTD_NORFLASH 3
53 #define MTD_NANDFLASH 4
56 #define MTD_UNKNOWN 15
60 #define MTD_CLEAR_BITS 1 // Bits can be cleared (flash)
61 #define MTD_SET_BITS 2 // Bits can be set
62 #define MTD_ERASEABLE 4 // Has an erase function
63 #define MTD_WRITEB_WRITEABLE 8 // Direct IO is possible
64 #define MTD_VOLATILE 16 // Set for RAMs
65 #define MTD_XIP 32 // eXecute-In-Place possible
66 #define MTD_OOB 64 // Out-of-band data (NAND flash)
67 #define MTD_ECC 128 // Device capable of automatic ECC
69 // Some common devices / combinations of capabilities
71 #define MTD_CAP_RAM (MTD_CLEAR_BITS|MTD_SET_BITS|MTD_WRITEB_WRITEABLE)
72 #define MTD_CAP_NORFLASH (MTD_CLEAR_BITS|MTD_ERASEABLE)
73 #define MTD_CAP_NANDFLASH (MTD_CLEAR_BITS|MTD_ERASEABLE|MTD_OOB)
74 #define MTD_WRITEABLE (MTD_CLEAR_BITS|MTD_SET_BITS)
77 // Types of automatic ECC/Checksum available
78 #define MTD_ECC_NONE 0 // No automatic ECC available
79 #define MTD_ECC_RS_DiskOnChip 1 // Automatic ECC on DiskOnChip
80 #define MTD_ECC_SW 2 // SW ECC for Toshiba & Samsung devices
82 struct mtd_info_user {
85 u_int32_t size; // Total size of the MTD
87 u_int32_t oobblock; // Size of OOB blocks (e.g. 512)
88 u_int32_t oobsize; // Amount of OOB data per block (e.g. 16)
93 struct region_info_user {
94 u_int32_t offset; /* At which this region starts,
95 * from the beginning of the MTD */
96 u_int32_t erasesize; /* For this region */
97 u_int32_t numblocks; /* Number of blocks in this region */
98 u_int32_t regionindex;
101 #ifdef CONFIG_MTD_NAND_LOGICAL_ADDRESS_ACCESS
102 struct read_laddr_info_user {
108 struct write_laddr_info_user {
115 #define MEMGETINFO _IOR('M', 1, struct mtd_info_user)
116 #define MEMERASE _IOW('M', 2, struct erase_info_user)
117 #define MEMWRITEOOB _IOWR('M', 3, struct mtd_oob_buf)
118 #define MEMREADOOB _IOWR('M', 4, struct mtd_oob_buf)
119 #define MEMLOCK _IOW('M', 5, struct erase_info_user)
120 #define MEMUNLOCK _IOW('M', 6, struct erase_info_user)
121 #define MEMGETREGIONCOUNT _IOR('M', 7, int)
122 #define MEMGETREGIONINFO _IOWR('M', 8, struct region_info_user)
123 #ifdef CONFIG_MTD_NAND_ERASE_BY_FORCE
124 #define MEMERASEBYFORCE _IOW('M', 9, struct erase_info_user)
126 #define MEMIOCTLRSV9 _IO('M', 9)
128 #ifdef CONFIG_MTD_NAND_LOGICAL_ADDRESS_ACCESS
129 #define MEMCLEANUPLADDR _IO('M', 10)
130 #define MEMREADLADDR _IOR('M', 11, struct read_laddr_info_user)
131 #define MEMWRITELADDR _IOW('M', 12, struct write_laddr_info_user)
133 #define MEMIOCTLRSV10 _IO('M', 10)
134 #define MEMIOCTLRSV11 _IO('M', 11)
135 #define MEMIOCTLRSV12 _IO('M', 12)
140 typedef struct mtd_info_user mtd_info_t;
141 typedef struct erase_info_user erase_info_t;
142 typedef struct region_info_user region_info_t;
144 /* User-space ioctl definitions */
147 #else /* __KERNEL__ */
150 #define MTD_ERASE_PENDING 0x01
151 #define MTD_ERASING 0x02
152 #define MTD_ERASE_SUSPEND 0x04
153 #define MTD_ERASE_DONE 0x08
154 #define MTD_ERASE_FAILED 0x10
157 struct mtd_info *mtd;
164 void (*callback) (struct erase_info *self);
167 struct erase_info *next;
168 #ifdef CONFIG_MTD_NAND_ERASE_BY_FORCE
173 struct mtd_erase_region_info {
174 u_int32_t offset; /* At which this region starts, from the beginning of the MTD */
175 u_int32_t erasesize; /* For this region */
176 u_int32_t numblocks; /* Number of blocks of erasesize in this region */
182 u_int32_t size; // Total size of the MTD
184 /* "Major" erase size for the device. Naïve users may take this
185 * to be the only erase size available, or may use the more detailed
186 * information below if they desire
190 u_int32_t oobblock; // Size of OOB blocks (e.g. 512)
191 u_int32_t oobsize; // Amount of OOB data per block (e.g. 16)
195 // Kernel-only stuff starts here.
199 /* Data for variable erase regions. If numeraseregions is zero,
200 * it means that the whole device has erasesize as given above.
203 struct mtd_erase_region_info *eraseregions;
205 /* This really shouldn't be here. It can go away in 2.5 */
208 struct module *module;
209 int (*erase) (struct mtd_info *mtd, struct erase_info *instr);
211 /* This stuff for eXecute-In-Place */
212 int (*point) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf);
214 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
215 void (*unpoint) (struct mtd_info *mtd, u_char * addr);
218 int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
219 int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
221 int (*read_ecc) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel);
222 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);
224 int (*read_oob) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
225 int (*write_oob) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
228 * Methods to access the protection register area, present in some
229 * flash devices. The user data is one time programmable but the
230 * factory data is read only.
232 int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
234 int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
236 /* This function is not yet implemented */
237 int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
239 /* iovec-based read/write methods. We need these especially for NAND flash,
240 with its limited number of write cycles per erase.
241 NB: The 'count' parameter is the number of _vectors_, each of
242 which contains an (ofs, len) tuple.
244 int (*readv) (struct mtd_info *mtd, struct iovec *vecs, unsigned long count, loff_t from, size_t *retlen);
245 int (*readv_ecc) (struct mtd_info *mtd, struct iovec *vecs, unsigned long count, loff_t from,
246 size_t *retlen, u_char *eccbuf, int oobsel);
247 int (*writev) (struct mtd_info *mtd, const struct iovec *vecs, unsigned long count, loff_t to, size_t *retlen);
248 int (*writev_ecc) (struct mtd_info *mtd, const struct iovec *vecs, unsigned long count, loff_t to,
249 size_t *retlen, u_char *eccbuf, int oobsel);
252 void (*sync) (struct mtd_info *mtd);
254 /* Chip-supported device locking */
255 int (*lock) (struct mtd_info *mtd, loff_t ofs, size_t len);
256 int (*unlock) (struct mtd_info *mtd, loff_t ofs, size_t len);
258 /* Power Management functions */
259 int (*suspend) (struct mtd_info *mtd);
260 void (*resume) (struct mtd_info *mtd);
262 #ifdef CONFIG_MTD_NAND_LOGICAL_ADDRESS_ACCESS
263 /* Logical Address Access functions */
264 int (*cleanup_laddr)(struct mtd_info *mtd);
265 int (*read_laddr)(struct mtd_info *mtd, loff_t from, size_t len, u_char *buf);
266 int (*write_laddr)(struct mtd_info *mtd, loff_t to, size_t len, u_char *buf, int (*eraseproc)(struct mtd_info *mtd, u_int32_t addr));
273 /* Kernel-side ioctl definitions */
275 extern int add_mtd_device(struct mtd_info *mtd);
276 extern int del_mtd_device (struct mtd_info *mtd);
278 extern struct mtd_info *__get_mtd_device(struct mtd_info *mtd, int num);
280 static inline struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
282 struct mtd_info *ret;
284 ret = __get_mtd_device(mtd, num);
286 if (ret && ret->module && !try_inc_mod_count(ret->module))
292 static inline void put_mtd_device(struct mtd_info *mtd)
295 __MOD_DEC_USE_COUNT(mtd->module);
299 struct mtd_notifier {
300 void (*add)(struct mtd_info *mtd);
301 void (*remove)(struct mtd_info *mtd);
302 struct mtd_notifier *next;
306 extern void register_mtd_user (struct mtd_notifier *new);
307 extern int unregister_mtd_user (struct mtd_notifier *old);
311 #define MTD_ERASE(mtd, args...) (*(mtd->erase))(mtd, args)
312 #define MTD_POINT(mtd, a,b,c,d) (*(mtd->point))(mtd, a,b,c, (u_char **)(d))
313 #define MTD_UNPOINT(mtd, arg) (*(mtd->unpoint))(mtd, (u_char *)arg)
314 #define MTD_READ(mtd, args...) (*(mtd->read))(mtd, args)
315 #define MTD_WRITE(mtd, args...) (*(mtd->write))(mtd, args)
316 #define MTD_READV(mtd, args...) (*(mtd->readv))(mtd, args)
317 #define MTD_WRITEV(mtd, args...) (*(mtd->writev))(mtd, args)
318 #define MTD_READECC(mtd, args...) (*(mtd->read_ecc))(mtd, args)
319 #define MTD_WRITEECC(mtd, args...) (*(mtd->write_ecc))(mtd, args)
320 #define MTD_READOOB(mtd, args...) (*(mtd->read_oob))(mtd, args)
321 #define MTD_WRITEOOB(mtd, args...) (*(mtd->write_oob))(mtd, args)
322 #define MTD_SYNC(mtd) do { if (mtd->sync) (*(mtd->sync))(mtd); } while (0)
326 * Debugging macro and defines
328 #define MTD_DEBUG_LEVEL0 (0) /* Quiet */
329 #define MTD_DEBUG_LEVEL1 (1) /* Audible */
330 #define MTD_DEBUG_LEVEL2 (2) /* Loud */
331 #define MTD_DEBUG_LEVEL3 (3) /* Noisy */
333 #ifdef CONFIG_MTD_DEBUG
334 #define DEBUG(n, args...) \
335 if (n <= CONFIG_MTD_DEBUG_VERBOSE) { \
336 printk(KERN_INFO args); \
338 #else /* CONFIG_MTD_DEBUG */
339 #define DEBUG(n, args...)
340 #endif /* CONFIG_MTD_DEBUG */
342 #endif /* __KERNEL__ */
344 #endif /* __MTD_MTD_H__ */