mtd.h 10 KB

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