mtd.h 16 KB

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