mtd.h 16 KB

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