1 /* 2 * Copyright (C) 1999-2003 David Woodhouse <dwmw2@infradead.org> et al. 3 * 4 * Released under GPL 5 */ 6 7 #ifndef __MTD_MTD_H__ 8 #define __MTD_MTD_H__ 9 10 #include <linux/types.h> 11 #include <linux/module.h> 12 #include <linux/uio.h> 13 #include <linux/notifier.h> 14 15 #include <linux/mtd/compatmac.h> 16 #include <mtd/mtd-abi.h> 17 18 #define MTD_CHAR_MAJOR 90 19 #define MTD_BLOCK_MAJOR 31 20 #define MAX_MTD_DEVICES 32 21 22 #define MTD_ERASE_PENDING 0x01 23 #define MTD_ERASING 0x02 24 #define MTD_ERASE_SUSPEND 0x04 25 #define MTD_ERASE_DONE 0x08 26 #define MTD_ERASE_FAILED 0x10 27 28 /* If the erase fails, fail_addr might indicate exactly which block failed. If 29 fail_addr = 0xffffffff, the failure was not at the device level or was not 30 specific to any particular block. */ 31 struct erase_info { 32 struct mtd_info *mtd; 33 u_int32_t addr; 34 u_int32_t len; 35 u_int32_t fail_addr; 36 u_long time; 37 u_long retries; 38 u_int dev; 39 u_int cell; 40 void (*callback) (struct erase_info *self); 41 u_long priv; 42 u_char state; 43 struct erase_info *next; 44 }; 45 46 struct mtd_erase_region_info { 47 u_int32_t offset; /* At which this region starts, from the beginning of the MTD */ 48 u_int32_t erasesize; /* For this region */ 49 u_int32_t numblocks; /* Number of blocks of erasesize in this region */ 50 unsigned long *lockmap; /* If keeping bitmap of locks */ 51 }; 52 53 /* 54 * oob operation modes 55 * 56 * MTD_OOB_PLACE: oob data are placed at the given offset 57 * MTD_OOB_AUTO: oob data are automatically placed at the free areas 58 * which are defined by the ecclayout 59 * MTD_OOB_RAW: mode to read raw data+oob in one chunk. The oob data 60 * is inserted into the data. Thats a raw image of the 61 * flash contents. 62 */ 63 typedef enum { 64 MTD_OOB_PLACE, 65 MTD_OOB_AUTO, 66 MTD_OOB_RAW, 67 } mtd_oob_mode_t; 68 69 /** 70 * struct mtd_oob_ops - oob operation operands 71 * @mode: operation mode 72 * 73 * @len: number of data bytes to write/read 74 * 75 * @retlen: number of data bytes written/read 76 * 77 * @ooblen: number of oob bytes to write/read 78 * @oobretlen: number of oob bytes written/read 79 * @ooboffs: offset of oob data in the oob area (only relevant when 80 * mode = MTD_OOB_PLACE) 81 * @datbuf: data buffer - if NULL only oob data are read/written 82 * @oobbuf: oob data buffer 83 * 84 * Note, it is allowed to read more then one OOB area at one go, but not write. 85 * The interface assumes that the OOB write requests program only one page's 86 * OOB area. 87 */ 88 struct mtd_oob_ops { 89 mtd_oob_mode_t mode; 90 size_t len; 91 size_t retlen; 92 size_t ooblen; 93 size_t oobretlen; 94 uint32_t ooboffs; 95 uint8_t *datbuf; 96 uint8_t *oobbuf; 97 }; 98 99 struct mtd_info { 100 u_char type; 101 u_int32_t flags; 102 u_int32_t size; // Total size of the MTD 103 104 /* "Major" erase size for the device. Naïve users may take this 105 * to be the only erase size available, or may use the more detailed 106 * information below if they desire 107 */ 108 u_int32_t erasesize; 109 /* Minimal writable flash unit size. In case of NOR flash it is 1 (even 110 * though individual bits can be cleared), in case of NAND flash it is 111 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR 112 * it is of ECC block size, etc. It is illegal to have writesize = 0. 113 * Any driver registering a struct mtd_info must ensure a writesize of 114 * 1 or larger. 115 */ 116 u_int32_t writesize; 117 118 u_int32_t oobsize; // Amount of OOB data per block (e.g. 16) 119 u_int32_t oobavail; // Available OOB bytes per block 120 121 // Kernel-only stuff starts here. 122 const char *name; 123 int index; 124 125 /* ecc layout structure pointer - read only ! */ 126 struct nand_ecclayout *ecclayout; 127 128 /* Data for variable erase regions. If numeraseregions is zero, 129 * it means that the whole device has erasesize as given above. 130 */ 131 int numeraseregions; 132 struct mtd_erase_region_info *eraseregions; 133 134 /* 135 * Erase is an asynchronous operation. Device drivers are supposed 136 * to call instr->callback() whenever the operation completes, even 137 * if it completes with a failure. 138 * Callers are supposed to pass a callback function and wait for it 139 * to be called before writing to the block. 140 */ 141 int (*erase) (struct mtd_info *mtd, struct erase_info *instr); 142 143 /* This stuff for eXecute-In-Place */ 144 /* phys is optional and may be set to NULL */ 145 int (*point) (struct mtd_info *mtd, loff_t from, size_t len, 146 size_t *retlen, void **virt, resource_size_t *phys); 147 148 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ 149 void (*unpoint) (struct mtd_info *mtd, loff_t from, size_t len); 150 151 152 int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 153 int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); 154 155 /* In blackbox flight recorder like scenarios we want to make successful 156 writes in interrupt context. panic_write() is only intended to be 157 called when its known the kernel is about to panic and we need the 158 write to succeed. Since the kernel is not going to be running for much 159 longer, this function can break locks and delay to ensure the write 160 succeeds (but not sleep). */ 161 162 int (*panic_write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); 163 164 int (*read_oob) (struct mtd_info *mtd, loff_t from, 165 struct mtd_oob_ops *ops); 166 int (*write_oob) (struct mtd_info *mtd, loff_t to, 167 struct mtd_oob_ops *ops); 168 169 /* 170 * Methods to access the protection register area, present in some 171 * flash devices. The user data is one time programmable but the 172 * factory data is read only. 173 */ 174 int (*get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len); 175 int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 176 int (*get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len); 177 int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 178 int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 179 int (*lock_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len); 180 181 /* kvec-based read/write methods. 182 NB: The 'count' parameter is the number of _vectors_, each of 183 which contains an (ofs, len) tuple. 184 */ 185 int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen); 186 187 /* Sync */ 188 void (*sync) (struct mtd_info *mtd); 189 190 /* Chip-supported device locking */ 191 int (*lock) (struct mtd_info *mtd, loff_t ofs, size_t len); 192 int (*unlock) (struct mtd_info *mtd, loff_t ofs, size_t len); 193 194 /* Power Management functions */ 195 int (*suspend) (struct mtd_info *mtd); 196 void (*resume) (struct mtd_info *mtd); 197 198 /* Bad block management functions */ 199 int (*block_isbad) (struct mtd_info *mtd, loff_t ofs); 200 int (*block_markbad) (struct mtd_info *mtd, loff_t ofs); 201 202 struct notifier_block reboot_notifier; /* default mode before reboot */ 203 204 /* ECC status information */ 205 struct mtd_ecc_stats ecc_stats; 206 /* Subpage shift (NAND) */ 207 int subpage_sft; 208 209 void *priv; 210 211 struct module *owner; 212 int usecount; 213 214 /* If the driver is something smart, like UBI, it may need to maintain 215 * its own reference counting. The below functions are only for driver. 216 * The driver may register its callbacks. These callbacks are not 217 * supposed to be called by MTD users */ 218 int (*get_device) (struct mtd_info *mtd); 219 void (*put_device) (struct mtd_info *mtd); 220 }; 221 222 223 /* Kernel-side ioctl definitions */ 224 225 extern int add_mtd_device(struct mtd_info *mtd); 226 extern int del_mtd_device (struct mtd_info *mtd); 227 228 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num); 229 extern struct mtd_info *get_mtd_device_nm(const char *name); 230 231 extern void put_mtd_device(struct mtd_info *mtd); 232 233 234 struct mtd_notifier { 235 void (*add)(struct mtd_info *mtd); 236 void (*remove)(struct mtd_info *mtd); 237 struct list_head list; 238 }; 239 240 241 extern void register_mtd_user (struct mtd_notifier *new); 242 extern int unregister_mtd_user (struct mtd_notifier *old); 243 244 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 245 unsigned long count, loff_t to, size_t *retlen); 246 247 int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs, 248 unsigned long count, loff_t from, size_t *retlen); 249 250 #ifdef CONFIG_MTD_PARTITIONS 251 void mtd_erase_callback(struct erase_info *instr); 252 #else 253 static inline void mtd_erase_callback(struct erase_info *instr) 254 { 255 if (instr->callback) 256 instr->callback(instr); 257 } 258 #endif 259 260 /* 261 * Debugging macro and defines 262 */ 263 #define MTD_DEBUG_LEVEL0 (0) /* Quiet */ 264 #define MTD_DEBUG_LEVEL1 (1) /* Audible */ 265 #define MTD_DEBUG_LEVEL2 (2) /* Loud */ 266 #define MTD_DEBUG_LEVEL3 (3) /* Noisy */ 267 268 #ifdef CONFIG_MTD_DEBUG 269 #define DEBUG(n, args...) \ 270 do { \ 271 if (n <= CONFIG_MTD_DEBUG_VERBOSE) \ 272 printk(KERN_INFO args); \ 273 } while(0) 274 #else /* CONFIG_MTD_DEBUG */ 275 #define DEBUG(n, args...) do { } while(0) 276 277 #endif /* CONFIG_MTD_DEBUG */ 278 279 #endif /* __MTD_MTD_H__ */ 280