mtd.h 17 KB

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