mtd.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  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 <linux/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 680
  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_isreserved) (struct mtd_info *mtd, loff_t ofs);
  216. int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
  217. int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
  218. #ifndef __UBOOT__
  219. int (*_suspend) (struct mtd_info *mtd);
  220. void (*_resume) (struct mtd_info *mtd);
  221. void (*_reboot) (struct mtd_info *mtd);
  222. #endif
  223. /*
  224. * If the driver is something smart, like UBI, it may need to maintain
  225. * its own reference counting. The below functions are only for driver.
  226. */
  227. int (*_get_device) (struct mtd_info *mtd);
  228. void (*_put_device) (struct mtd_info *mtd);
  229. #ifndef __UBOOT__
  230. /* Backing device capabilities for this device
  231. * - provides mmap capabilities
  232. */
  233. struct backing_dev_info *backing_dev_info;
  234. struct notifier_block reboot_notifier; /* default mode before reboot */
  235. #endif
  236. /* ECC status information */
  237. struct mtd_ecc_stats ecc_stats;
  238. /* Subpage shift (NAND) */
  239. int subpage_sft;
  240. void *priv;
  241. struct module *owner;
  242. #ifndef __UBOOT__
  243. struct device dev;
  244. #else
  245. struct udevice *dev;
  246. #endif
  247. int usecount;
  248. };
  249. static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
  250. {
  251. return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
  252. }
  253. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
  254. #ifndef __UBOOT__
  255. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  256. void **virt, resource_size_t *phys);
  257. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
  258. #endif
  259. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  260. unsigned long offset, unsigned long flags);
  261. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  262. u_char *buf);
  263. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  264. const u_char *buf);
  265. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  266. const u_char *buf);
  267. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
  268. static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
  269. struct mtd_oob_ops *ops)
  270. {
  271. ops->retlen = ops->oobretlen = 0;
  272. if (!mtd->_write_oob)
  273. return -EOPNOTSUPP;
  274. if (!(mtd->flags & MTD_WRITEABLE))
  275. return -EROFS;
  276. return mtd->_write_oob(mtd, to, ops);
  277. }
  278. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  279. struct otp_info *buf);
  280. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  281. size_t *retlen, u_char *buf);
  282. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  283. struct otp_info *buf);
  284. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  285. size_t *retlen, u_char *buf);
  286. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  287. size_t *retlen, u_char *buf);
  288. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
  289. #ifndef __UBOOT__
  290. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  291. unsigned long count, loff_t to, size_t *retlen);
  292. #endif
  293. static inline void mtd_sync(struct mtd_info *mtd)
  294. {
  295. if (mtd->_sync)
  296. mtd->_sync(mtd);
  297. }
  298. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  299. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  300. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  301. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
  302. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
  303. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
  304. #ifndef __UBOOT__
  305. static inline int mtd_suspend(struct mtd_info *mtd)
  306. {
  307. return mtd->_suspend ? mtd->_suspend(mtd) : 0;
  308. }
  309. static inline void mtd_resume(struct mtd_info *mtd)
  310. {
  311. if (mtd->_resume)
  312. mtd->_resume(mtd);
  313. }
  314. #endif
  315. static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
  316. {
  317. if (mtd->erasesize_shift)
  318. return sz >> mtd->erasesize_shift;
  319. do_div(sz, mtd->erasesize);
  320. return sz;
  321. }
  322. static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
  323. {
  324. if (mtd->erasesize_shift)
  325. return sz & mtd->erasesize_mask;
  326. return do_div(sz, mtd->erasesize);
  327. }
  328. static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
  329. {
  330. if (mtd->writesize_shift)
  331. return sz >> mtd->writesize_shift;
  332. do_div(sz, mtd->writesize);
  333. return sz;
  334. }
  335. static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
  336. {
  337. if (mtd->writesize_shift)
  338. return sz & mtd->writesize_mask;
  339. return do_div(sz, mtd->writesize);
  340. }
  341. static inline int mtd_has_oob(const struct mtd_info *mtd)
  342. {
  343. return mtd->_read_oob && mtd->_write_oob;
  344. }
  345. static inline int mtd_type_is_nand(const struct mtd_info *mtd)
  346. {
  347. return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
  348. }
  349. static inline int mtd_can_have_bb(const struct mtd_info *mtd)
  350. {
  351. return !!mtd->_block_isbad;
  352. }
  353. /* Kernel-side ioctl definitions */
  354. struct mtd_partition;
  355. struct mtd_part_parser_data;
  356. extern int mtd_device_parse_register(struct mtd_info *mtd,
  357. const char * const *part_probe_types,
  358. struct mtd_part_parser_data *parser_data,
  359. const struct mtd_partition *defparts,
  360. int defnr_parts);
  361. #define mtd_device_register(master, parts, nr_parts) \
  362. mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
  363. extern int mtd_device_unregister(struct mtd_info *master);
  364. extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
  365. extern int __get_mtd_device(struct mtd_info *mtd);
  366. extern void __put_mtd_device(struct mtd_info *mtd);
  367. extern struct mtd_info *get_mtd_device_nm(const char *name);
  368. extern void put_mtd_device(struct mtd_info *mtd);
  369. #ifndef __UBOOT__
  370. struct mtd_notifier {
  371. void (*add)(struct mtd_info *mtd);
  372. void (*remove)(struct mtd_info *mtd);
  373. struct list_head list;
  374. };
  375. extern void register_mtd_user (struct mtd_notifier *new);
  376. extern int unregister_mtd_user (struct mtd_notifier *old);
  377. #endif
  378. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
  379. #ifdef CONFIG_MTD_PARTITIONS
  380. void mtd_erase_callback(struct erase_info *instr);
  381. #else
  382. static inline void mtd_erase_callback(struct erase_info *instr)
  383. {
  384. if (instr->callback)
  385. instr->callback(instr);
  386. }
  387. #endif
  388. #ifdef __UBOOT__
  389. /*
  390. * Debugging macro and defines
  391. */
  392. #define MTD_DEBUG_LEVEL0 (0) /* Quiet */
  393. #define MTD_DEBUG_LEVEL1 (1) /* Audible */
  394. #define MTD_DEBUG_LEVEL2 (2) /* Loud */
  395. #define MTD_DEBUG_LEVEL3 (3) /* Noisy */
  396. #ifdef CONFIG_MTD_DEBUG
  397. #define pr_debug(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  398. #define MTDDEBUG(n, args...) \
  399. do { \
  400. if (n <= CONFIG_MTD_DEBUG_VERBOSE) \
  401. printk(KERN_INFO args); \
  402. } while(0)
  403. #else /* CONFIG_MTD_DEBUG */
  404. #define pr_debug(args...)
  405. #define MTDDEBUG(n, args...) \
  406. do { \
  407. if (0) \
  408. printk(KERN_INFO args); \
  409. } while(0)
  410. #endif /* CONFIG_MTD_DEBUG */
  411. #define pr_info(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  412. #define pr_warn(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  413. #define pr_err(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  414. #define pr_crit(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  415. #define pr_cont(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  416. #define pr_notice(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
  417. #endif
  418. static inline int mtd_is_bitflip(int err) {
  419. return err == -EUCLEAN;
  420. }
  421. static inline int mtd_is_eccerr(int err) {
  422. return err == -EBADMSG;
  423. }
  424. static inline int mtd_is_bitflip_or_eccerr(int err) {
  425. return mtd_is_bitflip(err) || mtd_is_eccerr(err);
  426. }
  427. unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
  428. #ifdef __UBOOT__
  429. /* drivers/mtd/mtdcore.h */
  430. int add_mtd_device(struct mtd_info *mtd);
  431. int del_mtd_device(struct mtd_info *mtd);
  432. int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
  433. int del_mtd_partitions(struct mtd_info *);
  434. int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
  435. loff_t *maxsize, int devtype, uint64_t chipsize);
  436. int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
  437. loff_t *size, loff_t *maxsize, int devtype,
  438. uint64_t chipsize);
  439. /* drivers/mtd/mtdcore.c */
  440. void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
  441. const uint64_t length, uint64_t *len_incl_bad,
  442. int *truncated);
  443. #endif
  444. #endif /* __MTD_MTD_H__ */