mtd.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Copyright © 1999-2010 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. #ifndef __UBOOT__
  10. #include <linux/types.h>
  11. #include <linux/uio.h>
  12. #include <linux/notifier.h>
  13. #include <linux/device.h>
  14. #include <mtd/mtd-abi.h>
  15. #include <asm/div64.h>
  16. #else
  17. #include <linux/compat.h>
  18. #include <mtd/mtd-abi.h>
  19. #include <asm/errno.h>
  20. #include <div64.h>
  21. #define MAX_MTD_DEVICES 32
  22. #endif
  23. #define MTD_ERASE_PENDING 0x01
  24. #define MTD_ERASING 0x02
  25. #define MTD_ERASE_SUSPEND 0x04
  26. #define MTD_ERASE_DONE 0x08
  27. #define MTD_ERASE_FAILED 0x10
  28. #define MTD_FAIL_ADDR_UNKNOWN -1LL
  29. /*
  30. * If the erase fails, fail_addr might indicate exactly which block failed. If
  31. * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
  32. * or was not specific to any particular block.
  33. */
  34. struct erase_info {
  35. struct mtd_info *mtd;
  36. uint64_t addr;
  37. uint64_t len;
  38. uint64_t fail_addr;
  39. u_long time;
  40. u_long retries;
  41. unsigned dev;
  42. unsigned cell;
  43. void (*callback) (struct erase_info *self);
  44. u_long priv;
  45. u_char state;
  46. struct erase_info *next;
  47. int scrub;
  48. };
  49. struct mtd_erase_region_info {
  50. uint64_t offset; /* At which this region starts, from the beginning of the MTD */
  51. uint32_t erasesize; /* For this region */
  52. uint32_t numblocks; /* Number of blocks of erasesize in this region */
  53. unsigned long *lockmap; /* If keeping bitmap of locks */
  54. };
  55. /**
  56. * struct mtd_oob_ops - oob operation operands
  57. * @mode: operation mode
  58. *
  59. * @len: number of data bytes to write/read
  60. *
  61. * @retlen: number of data bytes written/read
  62. *
  63. * @ooblen: number of oob bytes to write/read
  64. * @oobretlen: number of oob bytes written/read
  65. * @ooboffs: offset of oob data in the oob area (only relevant when
  66. * mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
  67. * @datbuf: data buffer - if NULL only oob data are read/written
  68. * @oobbuf: oob data buffer
  69. *
  70. * Note, it is allowed to read more than one OOB area at one go, but not write.
  71. * The interface assumes that the OOB write requests program only one page's
  72. * OOB area.
  73. */
  74. struct mtd_oob_ops {
  75. unsigned int mode;
  76. size_t len;
  77. size_t retlen;
  78. size_t ooblen;
  79. size_t oobretlen;
  80. uint32_t ooboffs;
  81. uint8_t *datbuf;
  82. uint8_t *oobbuf;
  83. };
  84. #ifdef CONFIG_SYS_NAND_MAX_OOBFREE
  85. #define MTD_MAX_OOBFREE_ENTRIES_LARGE CONFIG_SYS_NAND_MAX_OOBFREE
  86. #else
  87. #define MTD_MAX_OOBFREE_ENTRIES_LARGE 32
  88. #endif
  89. #ifdef CONFIG_SYS_NAND_MAX_ECCPOS
  90. #define MTD_MAX_ECCPOS_ENTRIES_LARGE CONFIG_SYS_NAND_MAX_ECCPOS
  91. #else
  92. #define MTD_MAX_ECCPOS_ENTRIES_LARGE 640
  93. #endif
  94. /*
  95. * Internal ECC layout control structure. For historical reasons, there is a
  96. * similar, smaller struct nand_ecclayout_user (in mtd-abi.h) that is retained
  97. * for export to user-space via the ECCGETLAYOUT ioctl.
  98. * nand_ecclayout should be expandable in the future simply by the above macros.
  99. */
  100. struct nand_ecclayout {
  101. __u32 eccbytes;
  102. __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
  103. __u32 oobavail;
  104. struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
  105. };
  106. struct module; /* only needed for owner field in mtd_info */
  107. struct mtd_info {
  108. u_char type;
  109. uint32_t flags;
  110. uint64_t size; // Total size of the MTD
  111. /* "Major" erase size for the device. Naïve users may take this
  112. * to be the only erase size available, or may use the more detailed
  113. * information below if they desire
  114. */
  115. uint32_t erasesize;
  116. /* Minimal writable flash unit size. In case of NOR flash it is 1 (even
  117. * though individual bits can be cleared), in case of NAND flash it is
  118. * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
  119. * it is of ECC block size, etc. It is illegal to have writesize = 0.
  120. * Any driver registering a struct mtd_info must ensure a writesize of
  121. * 1 or larger.
  122. */
  123. uint32_t writesize;
  124. /*
  125. * Size of the write buffer used by the MTD. MTD devices having a write
  126. * buffer can write multiple writesize chunks at a time. E.g. while
  127. * writing 4 * writesize bytes to a device with 2 * writesize bytes
  128. * buffer the MTD driver can (but doesn't have to) do 2 writesize
  129. * operations, but not 4. Currently, all NANDs have writebufsize
  130. * equivalent to writesize (NAND page size). Some NOR flashes do have
  131. * writebufsize greater than writesize.
  132. */
  133. uint32_t writebufsize;
  134. uint32_t oobsize; // Amount of OOB data per block (e.g. 16)
  135. uint32_t oobavail; // Available OOB bytes per block
  136. /*
  137. * If erasesize is a power of 2 then the shift is stored in
  138. * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
  139. */
  140. unsigned int erasesize_shift;
  141. unsigned int writesize_shift;
  142. /* Masks based on erasesize_shift and writesize_shift */
  143. unsigned int erasesize_mask;
  144. unsigned int writesize_mask;
  145. /*
  146. * read ops return -EUCLEAN if max number of bitflips corrected on any
  147. * one region comprising an ecc step equals or exceeds this value.
  148. * Settable by driver, else defaults to ecc_strength. User can override
  149. * in sysfs. N.B. The meaning of the -EUCLEAN return code has changed;
  150. * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
  151. */
  152. unsigned int bitflip_threshold;
  153. // Kernel-only stuff starts here.
  154. #ifndef __UBOOT__
  155. const char *name;
  156. #else
  157. char *name;
  158. #endif
  159. int index;
  160. /* ECC layout structure pointer - read only! */
  161. struct nand_ecclayout *ecclayout;
  162. /* the ecc step size. */
  163. unsigned int ecc_step_size;
  164. /* max number of correctible bit errors per ecc step */
  165. unsigned int ecc_strength;
  166. /* Data for variable erase regions. If numeraseregions is zero,
  167. * it means that the whole device has erasesize as given above.
  168. */
  169. int numeraseregions;
  170. struct mtd_erase_region_info *eraseregions;
  171. /*
  172. * Do not call via these pointers, use corresponding mtd_*()
  173. * wrappers instead.
  174. */
  175. int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
  176. #ifndef __UBOOT__
  177. int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
  178. size_t *retlen, void **virt, resource_size_t *phys);
  179. int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
  180. #endif
  181. unsigned long (*_get_unmapped_area) (struct mtd_info *mtd,
  182. unsigned long len,
  183. unsigned long offset,
  184. unsigned long flags);
  185. int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
  186. size_t *retlen, u_char *buf);
  187. int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
  188. size_t *retlen, const u_char *buf);
  189. int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
  190. size_t *retlen, const u_char *buf);
  191. int (*_read_oob) (struct mtd_info *mtd, loff_t from,
  192. struct mtd_oob_ops *ops);
  193. int (*_write_oob) (struct mtd_info *mtd, loff_t to,
  194. struct mtd_oob_ops *ops);
  195. int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
  196. size_t *retlen, struct otp_info *buf);
  197. int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
  198. size_t len, size_t *retlen, u_char *buf);
  199. int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
  200. size_t *retlen, struct otp_info *buf);
  201. int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
  202. size_t len, size_t *retlen, u_char *buf);
  203. int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
  204. size_t len, size_t *retlen, u_char *buf);
  205. int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
  206. size_t len);
  207. #ifndef __UBOOT__
  208. int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
  209. unsigned long count, loff_t to, size_t *retlen);
  210. #endif
  211. void (*_sync) (struct mtd_info *mtd);
  212. int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
  213. int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
  214. int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
  215. int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
  216. int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
  217. #ifndef __UBOOT__
  218. int (*_suspend) (struct mtd_info *mtd);
  219. void (*_resume) (struct mtd_info *mtd);
  220. #endif
  221. /*
  222. * If the driver is something smart, like UBI, it may need to maintain
  223. * its own reference counting. The below functions are only for driver.
  224. */
  225. int (*_get_device) (struct mtd_info *mtd);
  226. void (*_put_device) (struct mtd_info *mtd);
  227. #ifndef __UBOOT__
  228. /* Backing device capabilities for this device
  229. * - provides mmap capabilities
  230. */
  231. struct backing_dev_info *backing_dev_info;
  232. struct notifier_block reboot_notifier; /* default mode before reboot */
  233. #endif
  234. /* ECC status information */
  235. struct mtd_ecc_stats ecc_stats;
  236. /* Subpage shift (NAND) */
  237. int subpage_sft;
  238. void *priv;
  239. struct module *owner;
  240. #ifndef __UBOOT__
  241. struct device dev;
  242. #endif
  243. int usecount;
  244. };
  245. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
  246. #ifndef __UBOOT__
  247. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  248. void **virt, resource_size_t *phys);
  249. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
  250. #endif
  251. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  252. unsigned long offset, unsigned long flags);
  253. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  254. u_char *buf);
  255. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  256. const u_char *buf);
  257. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  258. const u_char *buf);
  259. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
  260. static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
  261. struct mtd_oob_ops *ops)
  262. {
  263. ops->retlen = ops->oobretlen = 0;
  264. if (!mtd->_write_oob)
  265. return -EOPNOTSUPP;
  266. if (!(mtd->flags & MTD_WRITEABLE))
  267. return -EROFS;
  268. return mtd->_write_oob(mtd, to, ops);
  269. }
  270. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  271. struct otp_info *buf);
  272. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  273. size_t *retlen, u_char *buf);
  274. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  275. struct otp_info *buf);
  276. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  277. size_t *retlen, u_char *buf);
  278. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  279. size_t *retlen, u_char *buf);
  280. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
  281. #ifndef __UBOOT__
  282. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  283. unsigned long count, loff_t to, size_t *retlen);
  284. #endif
  285. static inline void mtd_sync(struct mtd_info *mtd)
  286. {
  287. if (mtd->_sync)
  288. mtd->_sync(mtd);
  289. }
  290. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  291. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  292. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  293. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
  294. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
  295. #ifndef __UBOOT__
  296. static inline int mtd_suspend(struct mtd_info *mtd)
  297. {
  298. return mtd->_suspend ? mtd->_suspend(mtd) : 0;
  299. }
  300. static inline void mtd_resume(struct mtd_info *mtd)
  301. {
  302. if (mtd->_resume)
  303. mtd->_resume(mtd);
  304. }
  305. #endif
  306. static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
  307. {
  308. if (mtd->erasesize_shift)
  309. return sz >> mtd->erasesize_shift;
  310. do_div(sz, mtd->erasesize);
  311. return sz;
  312. }
  313. static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
  314. {
  315. if (mtd->erasesize_shift)
  316. return sz & mtd->erasesize_mask;
  317. return do_div(sz, mtd->erasesize);
  318. }
  319. static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
  320. {
  321. if (mtd->writesize_shift)
  322. return sz >> mtd->writesize_shift;
  323. do_div(sz, mtd->writesize);
  324. return sz;
  325. }
  326. static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
  327. {
  328. if (mtd->writesize_shift)
  329. return sz & mtd->writesize_mask;
  330. return do_div(sz, mtd->writesize);
  331. }
  332. static inline int mtd_has_oob(const struct mtd_info *mtd)
  333. {
  334. return mtd->_read_oob && mtd->_write_oob;
  335. }
  336. static inline int mtd_type_is_nand(const struct mtd_info *mtd)
  337. {
  338. return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
  339. }
  340. static inline int mtd_can_have_bb(const struct mtd_info *mtd)
  341. {
  342. return !!mtd->_block_isbad;
  343. }
  344. /* Kernel-side ioctl definitions */
  345. struct mtd_partition;
  346. struct mtd_part_parser_data;
  347. extern int mtd_device_parse_register(struct mtd_info *mtd,
  348. const char * const *part_probe_types,
  349. struct mtd_part_parser_data *parser_data,
  350. const struct mtd_partition *defparts,
  351. int defnr_parts);
  352. #define mtd_device_register(master, parts, nr_parts) \
  353. mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
  354. extern int mtd_device_unregister(struct mtd_info *master);
  355. extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
  356. extern int __get_mtd_device(struct mtd_info *mtd);
  357. extern void __put_mtd_device(struct mtd_info *mtd);
  358. extern struct mtd_info *get_mtd_device_nm(const char *name);
  359. extern void put_mtd_device(struct mtd_info *mtd);
  360. #ifndef __UBOOT__
  361. struct mtd_notifier {
  362. void (*add)(struct mtd_info *mtd);
  363. void (*remove)(struct mtd_info *mtd);
  364. struct list_head list;
  365. };
  366. extern void register_mtd_user (struct mtd_notifier *new);
  367. extern int unregister_mtd_user (struct mtd_notifier *old);
  368. #endif
  369. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
  370. #ifdef CONFIG_MTD_PARTITIONS
  371. void mtd_erase_callback(struct erase_info *instr);
  372. #else
  373. static inline void mtd_erase_callback(struct erase_info *instr)
  374. {
  375. if (instr->callback)
  376. instr->callback(instr);
  377. }
  378. #endif
  379. #ifdef __UBOOT__
  380. /*
  381. * Debugging macro and defines
  382. */
  383. #define MTD_DEBUG_LEVEL0 (0) /* Quiet */
  384. #define MTD_DEBUG_LEVEL1 (1) /* Audible */
  385. #define MTD_DEBUG_LEVEL2 (2) /* Loud */
  386. #define MTD_DEBUG_LEVEL3 (3) /* Noisy */
  387. #ifdef CONFIG_MTD_DEBUG
  388. #define pr_debug(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  389. #define MTDDEBUG(n, args...) \
  390. do { \
  391. if (n <= CONFIG_MTD_DEBUG_VERBOSE) \
  392. printk(KERN_INFO args); \
  393. } while(0)
  394. #else /* CONFIG_MTD_DEBUG */
  395. #define pr_debug(args...)
  396. #define MTDDEBUG(n, args...) \
  397. do { \
  398. if (0) \
  399. printk(KERN_INFO args); \
  400. } while(0)
  401. #endif /* CONFIG_MTD_DEBUG */
  402. #define pr_info(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  403. #define pr_warn(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  404. #define pr_err(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  405. #define pr_crit(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  406. #define pr_cont(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  407. #define pr_notice(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  408. #endif
  409. static inline int mtd_is_bitflip(int err) {
  410. return err == -EUCLEAN;
  411. }
  412. static inline int mtd_is_eccerr(int err) {
  413. return err == -EBADMSG;
  414. }
  415. static inline int mtd_is_bitflip_or_eccerr(int err) {
  416. return mtd_is_bitflip(err) || mtd_is_eccerr(err);
  417. }
  418. #ifdef __UBOOT__
  419. /* drivers/mtd/mtdcore.h */
  420. int add_mtd_device(struct mtd_info *mtd);
  421. int del_mtd_device(struct mtd_info *mtd);
  422. int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
  423. int del_mtd_partitions(struct mtd_info *);
  424. #endif
  425. #endif /* __MTD_MTD_H__ */