mtd.h 22 KB

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