mtd.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <mtd/mtd-abi.h>
  11. #include <asm/errno.h>
  12. #define MTD_CHAR_MAJOR 90
  13. #define MTD_BLOCK_MAJOR 31
  14. #define MAX_MTD_DEVICES 32
  15. #define MTD_ERASE_PENDING 0x01
  16. #define MTD_ERASING 0x02
  17. #define MTD_ERASE_SUSPEND 0x04
  18. #define MTD_ERASE_DONE 0x08
  19. #define MTD_ERASE_FAILED 0x10
  20. #define MTD_FAIL_ADDR_UNKNOWN -1LL
  21. /*
  22. * Enumeration for NAND/OneNAND flash chip state
  23. */
  24. enum {
  25. FL_READY,
  26. FL_READING,
  27. FL_WRITING,
  28. FL_ERASING,
  29. FL_SYNCING,
  30. FL_CACHEDPRG,
  31. FL_RESETING,
  32. FL_UNLOCKING,
  33. FL_LOCKING,
  34. FL_PM_SUSPENDED,
  35. };
  36. /* If the erase fails, fail_addr might indicate exactly which block failed. If
  37. fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level or was not
  38. specific to any particular block. */
  39. struct erase_info {
  40. struct mtd_info *mtd;
  41. uint64_t addr;
  42. uint64_t len;
  43. uint64_t fail_addr;
  44. u_long time;
  45. u_long retries;
  46. u_int dev;
  47. u_int cell;
  48. void (*callback) (struct erase_info *self);
  49. u_long priv;
  50. u_char state;
  51. struct erase_info *next;
  52. int scrub;
  53. };
  54. struct mtd_erase_region_info {
  55. uint64_t offset; /* At which this region starts, from the beginning of the MTD */
  56. u_int32_t erasesize; /* For this region */
  57. u_int32_t numblocks; /* Number of blocks of erasesize in this region */
  58. unsigned long *lockmap; /* If keeping bitmap of locks */
  59. };
  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_OPS_PLACE_OOB or MTD_OPS_RAW)
  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 then 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. unsigned int 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. u_int32_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. u_int32_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. u_int32_t writesize;
  106. u_int32_t oobsize; /* Amount of OOB data per block (e.g. 16) */
  107. u_int32_t oobavail; /* Available OOB bytes per block */
  108. /*
  109. * read ops return -EUCLEAN if max number of bitflips corrected on any
  110. * one region comprising an ecc step equals or exceeds this value.
  111. * Settable by driver, else defaults to ecc_strength. User can override
  112. * in sysfs. N.B. The meaning of the -EUCLEAN return code has changed;
  113. * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
  114. */
  115. unsigned int bitflip_threshold;
  116. /* Kernel-only stuff starts here. */
  117. const char *name;
  118. int index;
  119. /* ECC layout structure pointer - read only! */
  120. struct nand_ecclayout *ecclayout;
  121. /* max number of correctible bit errors per ecc step */
  122. unsigned int ecc_strength;
  123. /* Data for variable erase regions. If numeraseregions is zero,
  124. * it means that the whole device has erasesize as given above.
  125. */
  126. int numeraseregions;
  127. struct mtd_erase_region_info *eraseregions;
  128. /*
  129. * Do not call via these pointers, use corresponding mtd_*()
  130. * wrappers instead.
  131. */
  132. int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
  133. int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
  134. size_t *retlen, void **virt, phys_addr_t *phys);
  135. void (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
  136. int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
  137. size_t *retlen, u_char *buf);
  138. int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
  139. size_t *retlen, const u_char *buf);
  140. /* In blackbox flight recorder like scenarios we want to make successful
  141. writes in interrupt context. panic_write() is only intended to be
  142. called when its known the kernel is about to panic and we need the
  143. write to succeed. Since the kernel is not going to be running for much
  144. longer, this function can break locks and delay to ensure the write
  145. succeeds (but not sleep). */
  146. int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
  147. int (*_read_oob) (struct mtd_info *mtd, loff_t from,
  148. struct mtd_oob_ops *ops);
  149. int (*_write_oob) (struct mtd_info *mtd, loff_t to,
  150. struct mtd_oob_ops *ops);
  151. int (*_get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf,
  152. size_t len);
  153. int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
  154. size_t len, size_t *retlen, u_char *buf);
  155. int (*_get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf,
  156. size_t len);
  157. int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
  158. size_t len, size_t *retlen, u_char *buf);
  159. int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to, size_t len,
  160. size_t *retlen, u_char *buf);
  161. int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
  162. size_t len);
  163. void (*_sync) (struct mtd_info *mtd);
  164. int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
  165. int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
  166. int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
  167. int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
  168. /*
  169. * If the driver is something smart, like UBI, it may need to maintain
  170. * its own reference counting. The below functions are only for driver.
  171. */
  172. int (*_get_device) (struct mtd_info *mtd);
  173. void (*_put_device) (struct mtd_info *mtd);
  174. /* XXX U-BOOT XXX */
  175. #if 0
  176. /* kvec-based read/write methods.
  177. NB: The 'count' parameter is the number of _vectors_, each of
  178. which contains an (ofs, len) tuple.
  179. */
  180. int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen);
  181. #endif
  182. /* XXX U-BOOT XXX */
  183. #if 0
  184. struct notifier_block reboot_notifier; /* default mode before reboot */
  185. #endif
  186. /* ECC status information */
  187. struct mtd_ecc_stats ecc_stats;
  188. /* Subpage shift (NAND) */
  189. int subpage_sft;
  190. void *priv;
  191. struct module *owner;
  192. int usecount;
  193. };
  194. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
  195. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  196. u_char *buf);
  197. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  198. const u_char *buf);
  199. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  200. const u_char *buf);
  201. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
  202. static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
  203. struct mtd_oob_ops *ops)
  204. {
  205. ops->retlen = ops->oobretlen = 0;
  206. if (!mtd->_write_oob)
  207. return -EOPNOTSUPP;
  208. if (!(mtd->flags & MTD_WRITEABLE))
  209. return -EROFS;
  210. return mtd->_write_oob(mtd, to, ops);
  211. }
  212. int mtd_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
  213. size_t len);
  214. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  215. size_t *retlen, u_char *buf);
  216. int mtd_get_user_prot_info(struct mtd_info *mtd, struct otp_info *buf,
  217. size_t len);
  218. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  219. size_t *retlen, u_char *buf);
  220. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  221. size_t *retlen, u_char *buf);
  222. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
  223. /* XXX U-BOOT XXX */
  224. #if 0
  225. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  226. unsigned long count, loff_t to, size_t *retlen);
  227. #endif
  228. static inline void mtd_sync(struct mtd_info *mtd)
  229. {
  230. if (mtd->_sync)
  231. mtd->_sync(mtd);
  232. }
  233. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  234. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  235. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  236. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
  237. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
  238. static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
  239. {
  240. do_div(sz, mtd->erasesize);
  241. return sz;
  242. }
  243. static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
  244. {
  245. return do_div(sz, mtd->erasesize);
  246. }
  247. static inline int mtd_has_oob(const struct mtd_info *mtd)
  248. {
  249. return mtd->_read_oob && mtd->_write_oob;
  250. }
  251. static inline int mtd_can_have_bb(const struct mtd_info *mtd)
  252. {
  253. return !!mtd->_block_isbad;
  254. }
  255. /* Kernel-side ioctl definitions */
  256. extern int add_mtd_device(struct mtd_info *mtd);
  257. extern int del_mtd_device (struct mtd_info *mtd);
  258. extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
  259. extern struct mtd_info *get_mtd_device_nm(const char *name);
  260. extern void put_mtd_device(struct mtd_info *mtd);
  261. extern void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
  262. const uint64_t length, uint64_t *len_incl_bad,
  263. int *truncated);
  264. /* XXX U-BOOT XXX */
  265. #if 0
  266. struct mtd_notifier {
  267. void (*add)(struct mtd_info *mtd);
  268. void (*remove)(struct mtd_info *mtd);
  269. struct list_head list;
  270. };
  271. extern void register_mtd_user (struct mtd_notifier *new);
  272. extern int unregister_mtd_user (struct mtd_notifier *old);
  273. #endif
  274. #ifdef CONFIG_MTD_PARTITIONS
  275. void mtd_erase_callback(struct erase_info *instr);
  276. #else
  277. static inline void mtd_erase_callback(struct erase_info *instr)
  278. {
  279. if (instr->callback)
  280. instr->callback(instr);
  281. }
  282. #endif
  283. /*
  284. * Debugging macro and defines
  285. */
  286. #define MTD_DEBUG_LEVEL0 (0) /* Quiet */
  287. #define MTD_DEBUG_LEVEL1 (1) /* Audible */
  288. #define MTD_DEBUG_LEVEL2 (2) /* Loud */
  289. #define MTD_DEBUG_LEVEL3 (3) /* Noisy */
  290. #ifdef CONFIG_MTD_DEBUG
  291. #define pr_debug(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  292. #define MTDDEBUG(n, args...) \
  293. do { \
  294. if (n <= CONFIG_MTD_DEBUG_VERBOSE) \
  295. printk(KERN_INFO args); \
  296. } while(0)
  297. #else /* CONFIG_MTD_DEBUG */
  298. #define pr_debug(args...)
  299. #define MTDDEBUG(n, args...) \
  300. do { \
  301. if (0) \
  302. printk(KERN_INFO args); \
  303. } while(0)
  304. #endif /* CONFIG_MTD_DEBUG */
  305. #define pr_info(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  306. #define pr_warn(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  307. #define pr_err(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  308. static inline int mtd_is_bitflip(int err) {
  309. return err == -EUCLEAN;
  310. }
  311. static inline int mtd_is_eccerr(int err) {
  312. return err == -EBADMSG;
  313. }
  314. static inline int mtd_is_bitflip_or_eccerr(int err) {
  315. return mtd_is_bitflip(err) || mtd_is_eccerr(err);
  316. }
  317. #endif /* __MTD_MTD_H__ */