mtd.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #ifndef __MTD_MTD_H__
  20. #define __MTD_MTD_H__
  21. #include <linux/types.h>
  22. #include <linux/uio.h>
  23. #include <linux/notifier.h>
  24. #include <linux/device.h>
  25. #include <linux/of.h>
  26. #include <mtd/mtd-abi.h>
  27. #include <asm/div64.h>
  28. #define MTD_ERASE_PENDING 0x01
  29. #define MTD_ERASING 0x02
  30. #define MTD_ERASE_SUSPEND 0x04
  31. #define MTD_ERASE_DONE 0x08
  32. #define MTD_ERASE_FAILED 0x10
  33. #define MTD_FAIL_ADDR_UNKNOWN -1LL
  34. /*
  35. * If the erase fails, fail_addr might indicate exactly which block failed. If
  36. * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
  37. * or was not specific to any particular block.
  38. */
  39. struct erase_info {
  40. struct mtd_info *mtd;
  41. uint64_t addr;
  42. uint64_t len;
  43. uint64_t fail_addr;
  44. u_char state;
  45. };
  46. struct mtd_erase_region_info {
  47. uint64_t offset; /* At which this region starts, from the beginning of the MTD */
  48. uint32_t erasesize; /* For this region */
  49. uint32_t numblocks; /* Number of blocks of erasesize in this region */
  50. unsigned long *lockmap; /* If keeping bitmap of locks */
  51. };
  52. /**
  53. * struct mtd_oob_ops - oob operation operands
  54. * @mode: operation mode
  55. *
  56. * @len: number of data bytes to write/read
  57. *
  58. * @retlen: number of data bytes written/read
  59. *
  60. * @ooblen: number of oob bytes to write/read
  61. * @oobretlen: number of oob bytes written/read
  62. * @ooboffs: offset of oob data in the oob area (only relevant when
  63. * mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
  64. * @datbuf: data buffer - if NULL only oob data are read/written
  65. * @oobbuf: oob data buffer
  66. *
  67. * Note, it is allowed to read more than one OOB area at one go, but not write.
  68. * The interface assumes that the OOB write requests program only one page's
  69. * OOB area.
  70. */
  71. struct mtd_oob_ops {
  72. unsigned int mode;
  73. size_t len;
  74. size_t retlen;
  75. size_t ooblen;
  76. size_t oobretlen;
  77. uint32_t ooboffs;
  78. uint8_t *datbuf;
  79. uint8_t *oobbuf;
  80. };
  81. #define MTD_MAX_OOBFREE_ENTRIES_LARGE 32
  82. #define MTD_MAX_ECCPOS_ENTRIES_LARGE 640
  83. /**
  84. * struct mtd_oob_region - oob region definition
  85. * @offset: region offset
  86. * @length: region length
  87. *
  88. * This structure describes a region of the OOB area, and is used
  89. * to retrieve ECC or free bytes sections.
  90. * Each section is defined by an offset within the OOB area and a
  91. * length.
  92. */
  93. struct mtd_oob_region {
  94. u32 offset;
  95. u32 length;
  96. };
  97. /*
  98. * struct mtd_ooblayout_ops - NAND OOB layout operations
  99. * @ecc: function returning an ECC region in the OOB area.
  100. * Should return -ERANGE if %section exceeds the total number of
  101. * ECC sections.
  102. * @free: function returning a free region in the OOB area.
  103. * Should return -ERANGE if %section exceeds the total number of
  104. * free sections.
  105. */
  106. struct mtd_ooblayout_ops {
  107. int (*ecc)(struct mtd_info *mtd, int section,
  108. struct mtd_oob_region *oobecc);
  109. int (*free)(struct mtd_info *mtd, int section,
  110. struct mtd_oob_region *oobfree);
  111. };
  112. /**
  113. * struct mtd_pairing_info - page pairing information
  114. *
  115. * @pair: pair id
  116. * @group: group id
  117. *
  118. * The term "pair" is used here, even though TLC NANDs might group pages by 3
  119. * (3 bits in a single cell). A pair should regroup all pages that are sharing
  120. * the same cell. Pairs are then indexed in ascending order.
  121. *
  122. * @group is defining the position of a page in a given pair. It can also be
  123. * seen as the bit position in the cell: page attached to bit 0 belongs to
  124. * group 0, page attached to bit 1 belongs to group 1, etc.
  125. *
  126. * Example:
  127. * The H27UCG8T2BTR-BC datasheet describes the following pairing scheme:
  128. *
  129. * group-0 group-1
  130. *
  131. * pair-0 page-0 page-4
  132. * pair-1 page-1 page-5
  133. * pair-2 page-2 page-8
  134. * ...
  135. * pair-127 page-251 page-255
  136. *
  137. *
  138. * Note that the "group" and "pair" terms were extracted from Samsung and
  139. * Hynix datasheets, and might be referenced under other names in other
  140. * datasheets (Micron is describing this concept as "shared pages").
  141. */
  142. struct mtd_pairing_info {
  143. int pair;
  144. int group;
  145. };
  146. /**
  147. * struct mtd_pairing_scheme - page pairing scheme description
  148. *
  149. * @ngroups: number of groups. Should be related to the number of bits
  150. * per cell.
  151. * @get_info: converts a write-unit (page number within an erase block) into
  152. * mtd_pairing information (pair + group). This function should
  153. * fill the info parameter based on the wunit index or return
  154. * -EINVAL if the wunit parameter is invalid.
  155. * @get_wunit: converts pairing information into a write-unit (page) number.
  156. * This function should return the wunit index pointed by the
  157. * pairing information described in the info argument. It should
  158. * return -EINVAL, if there's no wunit corresponding to the
  159. * passed pairing information.
  160. *
  161. * See mtd_pairing_info documentation for a detailed explanation of the
  162. * pair and group concepts.
  163. *
  164. * The mtd_pairing_scheme structure provides a generic solution to represent
  165. * NAND page pairing scheme. Instead of exposing two big tables to do the
  166. * write-unit <-> (pair + group) conversions, we ask the MTD drivers to
  167. * implement the ->get_info() and ->get_wunit() functions.
  168. *
  169. * MTD users will then be able to query these information by using the
  170. * mtd_pairing_info_to_wunit() and mtd_wunit_to_pairing_info() helpers.
  171. *
  172. * @ngroups is here to help MTD users iterating over all the pages in a
  173. * given pair. This value can be retrieved by MTD users using the
  174. * mtd_pairing_groups() helper.
  175. *
  176. * Examples are given in the mtd_pairing_info_to_wunit() and
  177. * mtd_wunit_to_pairing_info() documentation.
  178. */
  179. struct mtd_pairing_scheme {
  180. int ngroups;
  181. int (*get_info)(struct mtd_info *mtd, int wunit,
  182. struct mtd_pairing_info *info);
  183. int (*get_wunit)(struct mtd_info *mtd,
  184. const struct mtd_pairing_info *info);
  185. };
  186. struct module; /* only needed for owner field in mtd_info */
  187. /**
  188. * struct mtd_debug_info - debugging information for an MTD device.
  189. *
  190. * @dfs_dir: direntry object of the MTD device debugfs directory
  191. */
  192. struct mtd_debug_info {
  193. struct dentry *dfs_dir;
  194. };
  195. struct mtd_info {
  196. u_char type;
  197. uint32_t flags;
  198. uint64_t size; // Total size of the MTD
  199. /* "Major" erase size for the device. Naïve users may take this
  200. * to be the only erase size available, or may use the more detailed
  201. * information below if they desire
  202. */
  203. uint32_t erasesize;
  204. /* Minimal writable flash unit size. In case of NOR flash it is 1 (even
  205. * though individual bits can be cleared), in case of NAND flash it is
  206. * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
  207. * it is of ECC block size, etc. It is illegal to have writesize = 0.
  208. * Any driver registering a struct mtd_info must ensure a writesize of
  209. * 1 or larger.
  210. */
  211. uint32_t writesize;
  212. /*
  213. * Size of the write buffer used by the MTD. MTD devices having a write
  214. * buffer can write multiple writesize chunks at a time. E.g. while
  215. * writing 4 * writesize bytes to a device with 2 * writesize bytes
  216. * buffer the MTD driver can (but doesn't have to) do 2 writesize
  217. * operations, but not 4. Currently, all NANDs have writebufsize
  218. * equivalent to writesize (NAND page size). Some NOR flashes do have
  219. * writebufsize greater than writesize.
  220. */
  221. uint32_t writebufsize;
  222. uint32_t oobsize; // Amount of OOB data per block (e.g. 16)
  223. uint32_t oobavail; // Available OOB bytes per block
  224. /*
  225. * If erasesize is a power of 2 then the shift is stored in
  226. * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
  227. */
  228. unsigned int erasesize_shift;
  229. unsigned int writesize_shift;
  230. /* Masks based on erasesize_shift and writesize_shift */
  231. unsigned int erasesize_mask;
  232. unsigned int writesize_mask;
  233. /*
  234. * read ops return -EUCLEAN if max number of bitflips corrected on any
  235. * one region comprising an ecc step equals or exceeds this value.
  236. * Settable by driver, else defaults to ecc_strength. User can override
  237. * in sysfs. N.B. The meaning of the -EUCLEAN return code has changed;
  238. * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
  239. */
  240. unsigned int bitflip_threshold;
  241. /* Kernel-only stuff starts here. */
  242. const char *name;
  243. int index;
  244. /* OOB layout description */
  245. const struct mtd_ooblayout_ops *ooblayout;
  246. /* NAND pairing scheme, only provided for MLC/TLC NANDs */
  247. const struct mtd_pairing_scheme *pairing;
  248. /* the ecc step size. */
  249. unsigned int ecc_step_size;
  250. /* max number of correctible bit errors per ecc step */
  251. unsigned int ecc_strength;
  252. /* Data for variable erase regions. If numeraseregions is zero,
  253. * it means that the whole device has erasesize as given above.
  254. */
  255. int numeraseregions;
  256. struct mtd_erase_region_info *eraseregions;
  257. /*
  258. * Do not call via these pointers, use corresponding mtd_*()
  259. * wrappers instead.
  260. */
  261. int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
  262. int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
  263. size_t *retlen, void **virt, resource_size_t *phys);
  264. int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
  265. int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
  266. size_t *retlen, u_char *buf);
  267. int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
  268. size_t *retlen, const u_char *buf);
  269. int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
  270. size_t *retlen, const u_char *buf);
  271. int (*_read_oob) (struct mtd_info *mtd, loff_t from,
  272. struct mtd_oob_ops *ops);
  273. int (*_write_oob) (struct mtd_info *mtd, loff_t to,
  274. struct mtd_oob_ops *ops);
  275. int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
  276. size_t *retlen, struct otp_info *buf);
  277. int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
  278. size_t len, size_t *retlen, u_char *buf);
  279. int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
  280. size_t *retlen, struct otp_info *buf);
  281. int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
  282. size_t len, size_t *retlen, u_char *buf);
  283. int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
  284. size_t len, size_t *retlen, u_char *buf);
  285. int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
  286. size_t len);
  287. int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
  288. unsigned long count, loff_t to, size_t *retlen);
  289. void (*_sync) (struct mtd_info *mtd);
  290. int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
  291. int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
  292. int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
  293. int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
  294. int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
  295. int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
  296. int (*_max_bad_blocks) (struct mtd_info *mtd, loff_t ofs, size_t len);
  297. int (*_suspend) (struct mtd_info *mtd);
  298. void (*_resume) (struct mtd_info *mtd);
  299. void (*_reboot) (struct mtd_info *mtd);
  300. /*
  301. * If the driver is something smart, like UBI, it may need to maintain
  302. * its own reference counting. The below functions are only for driver.
  303. */
  304. int (*_get_device) (struct mtd_info *mtd);
  305. void (*_put_device) (struct mtd_info *mtd);
  306. struct notifier_block reboot_notifier; /* default mode before reboot */
  307. /* ECC status information */
  308. struct mtd_ecc_stats ecc_stats;
  309. /* Subpage shift (NAND) */
  310. int subpage_sft;
  311. void *priv;
  312. struct module *owner;
  313. struct device dev;
  314. int usecount;
  315. struct mtd_debug_info dbg;
  316. };
  317. int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  318. struct mtd_oob_region *oobecc);
  319. int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  320. int *section,
  321. struct mtd_oob_region *oobregion);
  322. int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  323. const u8 *oobbuf, int start, int nbytes);
  324. int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  325. u8 *oobbuf, int start, int nbytes);
  326. int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  327. struct mtd_oob_region *oobfree);
  328. int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  329. const u8 *oobbuf, int start, int nbytes);
  330. int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  331. u8 *oobbuf, int start, int nbytes);
  332. int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
  333. int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
  334. static inline void mtd_set_ooblayout(struct mtd_info *mtd,
  335. const struct mtd_ooblayout_ops *ooblayout)
  336. {
  337. mtd->ooblayout = ooblayout;
  338. }
  339. static inline void mtd_set_pairing_scheme(struct mtd_info *mtd,
  340. const struct mtd_pairing_scheme *pairing)
  341. {
  342. mtd->pairing = pairing;
  343. }
  344. static inline void mtd_set_of_node(struct mtd_info *mtd,
  345. struct device_node *np)
  346. {
  347. mtd->dev.of_node = np;
  348. if (!mtd->name)
  349. of_property_read_string(np, "label", &mtd->name);
  350. }
  351. static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
  352. {
  353. return dev_of_node(&mtd->dev);
  354. }
  355. static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
  356. {
  357. return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
  358. }
  359. static inline int mtd_max_bad_blocks(struct mtd_info *mtd,
  360. loff_t ofs, size_t len)
  361. {
  362. if (!mtd->_max_bad_blocks)
  363. return -ENOTSUPP;
  364. if (mtd->size < (len + ofs) || ofs < 0)
  365. return -EINVAL;
  366. return mtd->_max_bad_blocks(mtd, ofs, len);
  367. }
  368. int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
  369. struct mtd_pairing_info *info);
  370. int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
  371. const struct mtd_pairing_info *info);
  372. int mtd_pairing_groups(struct mtd_info *mtd);
  373. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
  374. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  375. void **virt, resource_size_t *phys);
  376. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
  377. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  378. unsigned long offset, unsigned long flags);
  379. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  380. u_char *buf);
  381. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  382. const u_char *buf);
  383. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  384. const u_char *buf);
  385. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
  386. int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
  387. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  388. struct otp_info *buf);
  389. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  390. size_t *retlen, u_char *buf);
  391. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  392. struct otp_info *buf);
  393. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  394. size_t *retlen, u_char *buf);
  395. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  396. size_t *retlen, u_char *buf);
  397. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
  398. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  399. unsigned long count, loff_t to, size_t *retlen);
  400. static inline void mtd_sync(struct mtd_info *mtd)
  401. {
  402. if (mtd->_sync)
  403. mtd->_sync(mtd);
  404. }
  405. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  406. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  407. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  408. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
  409. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
  410. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
  411. static inline int mtd_suspend(struct mtd_info *mtd)
  412. {
  413. return mtd->_suspend ? mtd->_suspend(mtd) : 0;
  414. }
  415. static inline void mtd_resume(struct mtd_info *mtd)
  416. {
  417. if (mtd->_resume)
  418. mtd->_resume(mtd);
  419. }
  420. static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
  421. {
  422. if (mtd->erasesize_shift)
  423. return sz >> mtd->erasesize_shift;
  424. do_div(sz, mtd->erasesize);
  425. return sz;
  426. }
  427. static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
  428. {
  429. if (mtd->erasesize_shift)
  430. return sz & mtd->erasesize_mask;
  431. return do_div(sz, mtd->erasesize);
  432. }
  433. /**
  434. * mtd_align_erase_req - Adjust an erase request to align things on eraseblock
  435. * boundaries.
  436. * @mtd: the MTD device this erase request applies on
  437. * @req: the erase request to adjust
  438. *
  439. * This function will adjust @req->addr and @req->len to align them on
  440. * @mtd->erasesize. Of course we expect @mtd->erasesize to be != 0.
  441. */
  442. static inline void mtd_align_erase_req(struct mtd_info *mtd,
  443. struct erase_info *req)
  444. {
  445. u32 mod;
  446. if (WARN_ON(!mtd->erasesize))
  447. return;
  448. mod = mtd_mod_by_eb(req->addr, mtd);
  449. if (mod) {
  450. req->addr -= mod;
  451. req->len += mod;
  452. }
  453. mod = mtd_mod_by_eb(req->addr + req->len, mtd);
  454. if (mod)
  455. req->len += mtd->erasesize - mod;
  456. }
  457. static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
  458. {
  459. if (mtd->writesize_shift)
  460. return sz >> mtd->writesize_shift;
  461. do_div(sz, mtd->writesize);
  462. return sz;
  463. }
  464. static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
  465. {
  466. if (mtd->writesize_shift)
  467. return sz & mtd->writesize_mask;
  468. return do_div(sz, mtd->writesize);
  469. }
  470. static inline int mtd_wunit_per_eb(struct mtd_info *mtd)
  471. {
  472. return mtd->erasesize / mtd->writesize;
  473. }
  474. static inline int mtd_offset_to_wunit(struct mtd_info *mtd, loff_t offs)
  475. {
  476. return mtd_div_by_ws(mtd_mod_by_eb(offs, mtd), mtd);
  477. }
  478. static inline loff_t mtd_wunit_to_offset(struct mtd_info *mtd, loff_t base,
  479. int wunit)
  480. {
  481. return base + (wunit * mtd->writesize);
  482. }
  483. static inline int mtd_has_oob(const struct mtd_info *mtd)
  484. {
  485. return mtd->_read_oob && mtd->_write_oob;
  486. }
  487. static inline int mtd_type_is_nand(const struct mtd_info *mtd)
  488. {
  489. return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
  490. }
  491. static inline int mtd_can_have_bb(const struct mtd_info *mtd)
  492. {
  493. return !!mtd->_block_isbad;
  494. }
  495. /* Kernel-side ioctl definitions */
  496. struct mtd_partition;
  497. struct mtd_part_parser_data;
  498. extern int mtd_device_parse_register(struct mtd_info *mtd,
  499. const char * const *part_probe_types,
  500. struct mtd_part_parser_data *parser_data,
  501. const struct mtd_partition *defparts,
  502. int defnr_parts);
  503. #define mtd_device_register(master, parts, nr_parts) \
  504. mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
  505. extern int mtd_device_unregister(struct mtd_info *master);
  506. extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
  507. extern int __get_mtd_device(struct mtd_info *mtd);
  508. extern void __put_mtd_device(struct mtd_info *mtd);
  509. extern struct mtd_info *get_mtd_device_nm(const char *name);
  510. extern void put_mtd_device(struct mtd_info *mtd);
  511. struct mtd_notifier {
  512. void (*add)(struct mtd_info *mtd);
  513. void (*remove)(struct mtd_info *mtd);
  514. struct list_head list;
  515. };
  516. extern void register_mtd_user (struct mtd_notifier *new);
  517. extern int unregister_mtd_user (struct mtd_notifier *old);
  518. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
  519. void mtd_erase_callback(struct erase_info *instr);
  520. static inline int mtd_is_bitflip(int err) {
  521. return err == -EUCLEAN;
  522. }
  523. static inline int mtd_is_eccerr(int err) {
  524. return err == -EBADMSG;
  525. }
  526. static inline int mtd_is_bitflip_or_eccerr(int err) {
  527. return mtd_is_bitflip(err) || mtd_is_eccerr(err);
  528. }
  529. unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
  530. #endif /* __MTD_MTD_H__ */