mtd.h 8.8 KB

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