mtd.h 9.2 KB

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