11da177e4SLinus Torvalds /* 2a1452a37SDavid Woodhouse * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al. 31da177e4SLinus Torvalds * 4a1452a37SDavid Woodhouse * This program is free software; you can redistribute it and/or modify 5a1452a37SDavid Woodhouse * it under the terms of the GNU General Public License as published by 6a1452a37SDavid Woodhouse * the Free Software Foundation; either version 2 of the License, or 7a1452a37SDavid Woodhouse * (at your option) any later version. 8a1452a37SDavid Woodhouse * 9a1452a37SDavid Woodhouse * This program is distributed in the hope that it will be useful, 10a1452a37SDavid Woodhouse * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a1452a37SDavid Woodhouse * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a1452a37SDavid Woodhouse * GNU General Public License for more details. 13a1452a37SDavid Woodhouse * 14a1452a37SDavid Woodhouse * You should have received a copy of the GNU General Public License 15a1452a37SDavid Woodhouse * along with this program; if not, write to the Free Software 16a1452a37SDavid Woodhouse * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17a1452a37SDavid Woodhouse * 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #ifndef __MTD_MTD_H__ 211da177e4SLinus Torvalds #define __MTD_MTD_H__ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/types.h> 241da177e4SLinus Torvalds #include <linux/uio.h> 25963a6fb0SNicolas Pitre #include <linux/notifier.h> 261f24b5a8SDavid Brownell #include <linux/device.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include <mtd/mtd-abi.h> 291da177e4SLinus Torvalds 3069423d99SAdrian Hunter #include <asm/div64.h> 3169423d99SAdrian Hunter 321da177e4SLinus Torvalds #define MTD_CHAR_MAJOR 90 331da177e4SLinus Torvalds #define MTD_BLOCK_MAJOR 31 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #define MTD_ERASE_PENDING 0x01 361da177e4SLinus Torvalds #define MTD_ERASING 0x02 371da177e4SLinus Torvalds #define MTD_ERASE_SUSPEND 0x04 381da177e4SLinus Torvalds #define MTD_ERASE_DONE 0x08 391da177e4SLinus Torvalds #define MTD_ERASE_FAILED 0x10 401da177e4SLinus Torvalds 4169423d99SAdrian Hunter #define MTD_FAIL_ADDR_UNKNOWN -1LL 42bb0eb217SAdrian Hunter 43e2e24e8eSBrian Norris /* 44e2e24e8eSBrian Norris * If the erase fails, fail_addr might indicate exactly which block failed. If 45e2e24e8eSBrian Norris * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level 46e2e24e8eSBrian Norris * or was not specific to any particular block. 47e2e24e8eSBrian Norris */ 481da177e4SLinus Torvalds struct erase_info { 491da177e4SLinus Torvalds struct mtd_info *mtd; 5069423d99SAdrian Hunter uint64_t addr; 5169423d99SAdrian Hunter uint64_t len; 5269423d99SAdrian Hunter uint64_t fail_addr; 531da177e4SLinus Torvalds u_long time; 541da177e4SLinus Torvalds u_long retries; 5526cdb67cSDavid Woodhouse unsigned dev; 5626cdb67cSDavid Woodhouse unsigned cell; 571da177e4SLinus Torvalds void (*callback) (struct erase_info *self); 581da177e4SLinus Torvalds u_long priv; 591da177e4SLinus Torvalds u_char state; 601da177e4SLinus Torvalds struct erase_info *next; 611da177e4SLinus Torvalds }; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds struct mtd_erase_region_info { 6469423d99SAdrian Hunter uint64_t offset; /* At which this region starts, from the beginning of the MTD */ 6526cdb67cSDavid Woodhouse uint32_t erasesize; /* For this region */ 6626cdb67cSDavid Woodhouse uint32_t numblocks; /* Number of blocks of erasesize in this region */ 670ecbc81aSRodolfo Giometti unsigned long *lockmap; /* If keeping bitmap of locks */ 681da177e4SLinus Torvalds }; 691da177e4SLinus Torvalds 708593fbc6SThomas Gleixner /** 718593fbc6SThomas Gleixner * struct mtd_oob_ops - oob operation operands 728593fbc6SThomas Gleixner * @mode: operation mode 738593fbc6SThomas Gleixner * 747014568bSVitaly Wool * @len: number of data bytes to write/read 758593fbc6SThomas Gleixner * 767014568bSVitaly Wool * @retlen: number of data bytes written/read 778593fbc6SThomas Gleixner * 787014568bSVitaly Wool * @ooblen: number of oob bytes to write/read 797014568bSVitaly Wool * @oobretlen: number of oob bytes written/read 808593fbc6SThomas Gleixner * @ooboffs: offset of oob data in the oob area (only relevant when 814180f24aSBrian Norris * mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW) 828593fbc6SThomas Gleixner * @datbuf: data buffer - if NULL only oob data are read/written 838593fbc6SThomas Gleixner * @oobbuf: oob data buffer 8473a4421cSArtem Bityutskiy * 85025dfdafSFrederik Schwarzer * Note, it is allowed to read more than one OOB area at one go, but not write. 8673a4421cSArtem Bityutskiy * The interface assumes that the OOB write requests program only one page's 8773a4421cSArtem Bityutskiy * OOB area. 888593fbc6SThomas Gleixner */ 898593fbc6SThomas Gleixner struct mtd_oob_ops { 90905c6bcdSBrian Norris unsigned int mode; 918593fbc6SThomas Gleixner size_t len; 928593fbc6SThomas Gleixner size_t retlen; 938593fbc6SThomas Gleixner size_t ooblen; 947014568bSVitaly Wool size_t oobretlen; 958593fbc6SThomas Gleixner uint32_t ooboffs; 968593fbc6SThomas Gleixner uint8_t *datbuf; 978593fbc6SThomas Gleixner uint8_t *oobbuf; 988593fbc6SThomas Gleixner }; 998593fbc6SThomas Gleixner 100cc26c3cdSBrian Norris #define MTD_MAX_OOBFREE_ENTRIES_LARGE 32 1012f25ae97SVipin Kumar #define MTD_MAX_ECCPOS_ENTRIES_LARGE 640 102cc26c3cdSBrian Norris /* 1030ceacf36SBrian Norris * Internal ECC layout control structure. For historical reasons, there is a 1040ceacf36SBrian Norris * similar, smaller struct nand_ecclayout_user (in mtd-abi.h) that is retained 1050ceacf36SBrian Norris * for export to user-space via the ECCGETLAYOUT ioctl. 1060ceacf36SBrian Norris * nand_ecclayout should be expandable in the future simply by the above macros. 107cc26c3cdSBrian Norris */ 108cc26c3cdSBrian Norris struct nand_ecclayout { 109cc26c3cdSBrian Norris __u32 eccbytes; 110cc26c3cdSBrian Norris __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE]; 111cc26c3cdSBrian Norris __u32 oobavail; 112cc26c3cdSBrian Norris struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE]; 113cc26c3cdSBrian Norris }; 114cc26c3cdSBrian Norris 115de477254SPaul Gortmaker struct module; /* only needed for owner field in mtd_info */ 116de477254SPaul Gortmaker 1171da177e4SLinus Torvalds struct mtd_info { 1181da177e4SLinus Torvalds u_char type; 11926cdb67cSDavid Woodhouse uint32_t flags; 12069423d99SAdrian Hunter uint64_t size; // Total size of the MTD 1211da177e4SLinus Torvalds 122151e7659SDavid Woodhouse /* "Major" erase size for the device. Naïve users may take this 1231da177e4SLinus Torvalds * to be the only erase size available, or may use the more detailed 1241da177e4SLinus Torvalds * information below if they desire 1251da177e4SLinus Torvalds */ 12626cdb67cSDavid Woodhouse uint32_t erasesize; 127783ed81fSArtem B. Bityutskiy /* Minimal writable flash unit size. In case of NOR flash it is 1 (even 128783ed81fSArtem B. Bityutskiy * though individual bits can be cleared), in case of NAND flash it is 129783ed81fSArtem B. Bityutskiy * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR 130783ed81fSArtem B. Bityutskiy * it is of ECC block size, etc. It is illegal to have writesize = 0. 131783ed81fSArtem B. Bityutskiy * Any driver registering a struct mtd_info must ensure a writesize of 132783ed81fSArtem B. Bityutskiy * 1 or larger. 13328318776SJoern Engel */ 13426cdb67cSDavid Woodhouse uint32_t writesize; 1351da177e4SLinus Torvalds 1360e4ca7e5SAnatolij Gustschin /* 1370e4ca7e5SAnatolij Gustschin * Size of the write buffer used by the MTD. MTD devices having a write 1380e4ca7e5SAnatolij Gustschin * buffer can write multiple writesize chunks at a time. E.g. while 1390e4ca7e5SAnatolij Gustschin * writing 4 * writesize bytes to a device with 2 * writesize bytes 1400e4ca7e5SAnatolij Gustschin * buffer the MTD driver can (but doesn't have to) do 2 writesize 1410e4ca7e5SAnatolij Gustschin * operations, but not 4. Currently, all NANDs have writebufsize 1420e4ca7e5SAnatolij Gustschin * equivalent to writesize (NAND page size). Some NOR flashes do have 1430e4ca7e5SAnatolij Gustschin * writebufsize greater than writesize. 1440e4ca7e5SAnatolij Gustschin */ 1450e4ca7e5SAnatolij Gustschin uint32_t writebufsize; 1460e4ca7e5SAnatolij Gustschin 14726cdb67cSDavid Woodhouse uint32_t oobsize; // Amount of OOB data per block (e.g. 16) 14826cdb67cSDavid Woodhouse uint32_t oobavail; // Available OOB bytes per block 1491da177e4SLinus Torvalds 15069423d99SAdrian Hunter /* 15169423d99SAdrian Hunter * If erasesize is a power of 2 then the shift is stored in 15269423d99SAdrian Hunter * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize. 15369423d99SAdrian Hunter */ 15469423d99SAdrian Hunter unsigned int erasesize_shift; 15569423d99SAdrian Hunter unsigned int writesize_shift; 15669423d99SAdrian Hunter /* Masks based on erasesize_shift and writesize_shift */ 15769423d99SAdrian Hunter unsigned int erasesize_mask; 15869423d99SAdrian Hunter unsigned int writesize_mask; 1591da177e4SLinus Torvalds 160d062d4edSMike Dunn /* 161d062d4edSMike Dunn * read ops return -EUCLEAN if max number of bitflips corrected on any 162d062d4edSMike Dunn * one region comprising an ecc step equals or exceeds this value. 163d062d4edSMike Dunn * Settable by driver, else defaults to ecc_strength. User can override 164d062d4edSMike Dunn * in sysfs. N.B. The meaning of the -EUCLEAN return code has changed; 165d062d4edSMike Dunn * see Documentation/ABI/testing/sysfs-class-mtd for more detail. 166d062d4edSMike Dunn */ 167d062d4edSMike Dunn unsigned int bitflip_threshold; 168d062d4edSMike Dunn 1691da177e4SLinus Torvalds // Kernel-only stuff starts here. 170eadcf0d7SGreg Kroah-Hartman const char *name; 1711da177e4SLinus Torvalds int index; 1721da177e4SLinus Torvalds 1737854d3f7SBrian Norris /* ECC layout structure pointer - read only! */ 1745bd34c09SThomas Gleixner struct nand_ecclayout *ecclayout; 1751da177e4SLinus Torvalds 176c41a0582SHuang Shijie /* the ecc step size. */ 177c41a0582SHuang Shijie unsigned int ecc_step_size; 178c41a0582SHuang Shijie 17986c2072bSMike Dunn /* max number of correctible bit errors per ecc step */ 1801d0b95b0SMike Dunn unsigned int ecc_strength; 1811d0b95b0SMike Dunn 1821da177e4SLinus Torvalds /* Data for variable erase regions. If numeraseregions is zero, 1831da177e4SLinus Torvalds * it means that the whole device has erasesize as given above. 1841da177e4SLinus Torvalds */ 1851da177e4SLinus Torvalds int numeraseregions; 1861da177e4SLinus Torvalds struct mtd_erase_region_info *eraseregions; 1871da177e4SLinus Torvalds 188b38178eeSJörn Engel /* 1897e1f0dc0SArtem Bityutskiy * Do not call via these pointers, use corresponding mtd_*() 1907e1f0dc0SArtem Bityutskiy * wrappers instead. 191b38178eeSJörn Engel */ 1923c3c10bbSArtem Bityutskiy int (*_erase) (struct mtd_info *mtd, struct erase_info *instr); 1933c3c10bbSArtem Bityutskiy int (*_point) (struct mtd_info *mtd, loff_t from, size_t len, 194a98889f3SJared Hulbert size_t *retlen, void **virt, resource_size_t *phys); 1955e4e6e3fSArtem Bityutskiy int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len); 1963c3c10bbSArtem Bityutskiy unsigned long (*_get_unmapped_area) (struct mtd_info *mtd, 197402d3265SDavid Howells unsigned long len, 198402d3265SDavid Howells unsigned long offset, 199402d3265SDavid Howells unsigned long flags); 2003c3c10bbSArtem Bityutskiy int (*_read) (struct mtd_info *mtd, loff_t from, size_t len, 201329ad399SArtem Bityutskiy size_t *retlen, u_char *buf); 2023c3c10bbSArtem Bityutskiy int (*_write) (struct mtd_info *mtd, loff_t to, size_t len, 203eda95cbfSArtem Bityutskiy size_t *retlen, const u_char *buf); 2043c3c10bbSArtem Bityutskiy int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len, 2057ae79d7fSArtem Bityutskiy size_t *retlen, const u_char *buf); 2063c3c10bbSArtem Bityutskiy int (*_read_oob) (struct mtd_info *mtd, loff_t from, 207fd2819bbSArtem Bityutskiy struct mtd_oob_ops *ops); 2083c3c10bbSArtem Bityutskiy int (*_write_oob) (struct mtd_info *mtd, loff_t to, 209a2cc5ba0SArtem Bityutskiy struct mtd_oob_ops *ops); 2103c3c10bbSArtem Bityutskiy int (*_get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf, 211a750b5ceSArtem Bityutskiy size_t len); 2123c3c10bbSArtem Bityutskiy int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, 213d264f72aSArtem Bityutskiy size_t len, size_t *retlen, u_char *buf); 2143c3c10bbSArtem Bityutskiy int (*_get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf, 215855e5d8cSArtem Bityutskiy size_t len); 2163c3c10bbSArtem Bityutskiy int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from, 2174ea1cabbSArtem Bityutskiy size_t len, size_t *retlen, u_char *buf); 2183c3c10bbSArtem Bityutskiy int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to, 2193c3c10bbSArtem Bityutskiy size_t len, size_t *retlen, u_char *buf); 2203c3c10bbSArtem Bityutskiy int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from, 2214403dbfbSArtem Bityutskiy size_t len); 2223c3c10bbSArtem Bityutskiy int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs, 223b0a31f7bSArtem Bityutskiy unsigned long count, loff_t to, size_t *retlen); 2243c3c10bbSArtem Bityutskiy void (*_sync) (struct mtd_info *mtd); 2253c3c10bbSArtem Bityutskiy int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len); 2263c3c10bbSArtem Bityutskiy int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len); 2273c3c10bbSArtem Bityutskiy int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len); 2283c3c10bbSArtem Bityutskiy int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs); 2293c3c10bbSArtem Bityutskiy int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs); 2303c3c10bbSArtem Bityutskiy int (*_suspend) (struct mtd_info *mtd); 2313c3c10bbSArtem Bityutskiy void (*_resume) (struct mtd_info *mtd); 232a88d2dc6SArtem Bityutskiy /* 233a88d2dc6SArtem Bityutskiy * If the driver is something smart, like UBI, it may need to maintain 234a88d2dc6SArtem Bityutskiy * its own reference counting. The below functions are only for driver. 235a88d2dc6SArtem Bityutskiy */ 2363c3c10bbSArtem Bityutskiy int (*_get_device) (struct mtd_info *mtd); 2373c3c10bbSArtem Bityutskiy void (*_put_device) (struct mtd_info *mtd); 238402d3265SDavid Howells 239402d3265SDavid Howells /* Backing device capabilities for this device 240402d3265SDavid Howells * - provides mmap capabilities 241402d3265SDavid Howells */ 242402d3265SDavid Howells struct backing_dev_info *backing_dev_info; 243402d3265SDavid Howells 244963a6fb0SNicolas Pitre struct notifier_block reboot_notifier; /* default mode before reboot */ 245963a6fb0SNicolas Pitre 2467fac4648SThomas Gleixner /* ECC status information */ 2477fac4648SThomas Gleixner struct mtd_ecc_stats ecc_stats; 24829072b96SThomas Gleixner /* Subpage shift (NAND) */ 24929072b96SThomas Gleixner int subpage_sft; 2507fac4648SThomas Gleixner 2511da177e4SLinus Torvalds void *priv; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds struct module *owner; 2541f24b5a8SDavid Brownell struct device dev; 2551da177e4SLinus Torvalds int usecount; 2561da177e4SLinus Torvalds }; 2571da177e4SLinus Torvalds 2588273a0c9SArtem Bityutskiy int mtd_erase(struct mtd_info *mtd, struct erase_info *instr); 2598273a0c9SArtem Bityutskiy int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 2608273a0c9SArtem Bityutskiy void **virt, resource_size_t *phys); 2618273a0c9SArtem Bityutskiy int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len); 2628273a0c9SArtem Bityutskiy unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, 2638273a0c9SArtem Bityutskiy unsigned long offset, unsigned long flags); 2648273a0c9SArtem Bityutskiy int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 2658273a0c9SArtem Bityutskiy u_char *buf); 2668273a0c9SArtem Bityutskiy int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 2678273a0c9SArtem Bityutskiy const u_char *buf); 2688273a0c9SArtem Bityutskiy int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 2698273a0c9SArtem Bityutskiy const u_char *buf); 2707ae79d7fSArtem Bityutskiy 271d2d48480SBrian Norris int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops); 272fd2819bbSArtem Bityutskiy 273a2cc5ba0SArtem Bityutskiy static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to, 274a2cc5ba0SArtem Bityutskiy struct mtd_oob_ops *ops) 275a2cc5ba0SArtem Bityutskiy { 2769cf075f8SArtem Bityutskiy ops->retlen = ops->oobretlen = 0; 2773c3c10bbSArtem Bityutskiy if (!mtd->_write_oob) 278dac2639fSArtem Bityutskiy return -EOPNOTSUPP; 279664addc2SArtem Bityutskiy if (!(mtd->flags & MTD_WRITEABLE)) 280664addc2SArtem Bityutskiy return -EROFS; 2813c3c10bbSArtem Bityutskiy return mtd->_write_oob(mtd, to, ops); 282a2cc5ba0SArtem Bityutskiy } 283a2cc5ba0SArtem Bityutskiy 284de3cac93SArtem Bityutskiy int mtd_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf, 285de3cac93SArtem Bityutskiy size_t len); 286de3cac93SArtem Bityutskiy int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 287de3cac93SArtem Bityutskiy size_t *retlen, u_char *buf); 288de3cac93SArtem Bityutskiy int mtd_get_user_prot_info(struct mtd_info *mtd, struct otp_info *buf, 289de3cac93SArtem Bityutskiy size_t len); 290de3cac93SArtem Bityutskiy int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 291de3cac93SArtem Bityutskiy size_t *retlen, u_char *buf); 292de3cac93SArtem Bityutskiy int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, 293de3cac93SArtem Bityutskiy size_t *retlen, u_char *buf); 294de3cac93SArtem Bityutskiy int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len); 2954403dbfbSArtem Bityutskiy 2961dbebd32SArtem Bityutskiy int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 2971dbebd32SArtem Bityutskiy unsigned long count, loff_t to, size_t *retlen); 298b0a31f7bSArtem Bityutskiy 29985f2f2a8SArtem Bityutskiy static inline void mtd_sync(struct mtd_info *mtd) 30085f2f2a8SArtem Bityutskiy { 3013c3c10bbSArtem Bityutskiy if (mtd->_sync) 3023c3c10bbSArtem Bityutskiy mtd->_sync(mtd); 30385f2f2a8SArtem Bityutskiy } 30485f2f2a8SArtem Bityutskiy 3058273a0c9SArtem Bityutskiy int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len); 3068273a0c9SArtem Bityutskiy int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len); 3078273a0c9SArtem Bityutskiy int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len); 3088273a0c9SArtem Bityutskiy int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs); 3098273a0c9SArtem Bityutskiy int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs); 310e95e9786SArtem Bityutskiy 3113fe4bae8SArtem Bityutskiy static inline int mtd_suspend(struct mtd_info *mtd) 3123fe4bae8SArtem Bityutskiy { 3133c3c10bbSArtem Bityutskiy return mtd->_suspend ? mtd->_suspend(mtd) : 0; 3143fe4bae8SArtem Bityutskiy } 3153fe4bae8SArtem Bityutskiy 316ead995f8SArtem Bityutskiy static inline void mtd_resume(struct mtd_info *mtd) 317ead995f8SArtem Bityutskiy { 3183c3c10bbSArtem Bityutskiy if (mtd->_resume) 3193c3c10bbSArtem Bityutskiy mtd->_resume(mtd); 320ead995f8SArtem Bityutskiy } 321ead995f8SArtem Bityutskiy 32226cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd) 32369423d99SAdrian Hunter { 32469423d99SAdrian Hunter if (mtd->erasesize_shift) 32569423d99SAdrian Hunter return sz >> mtd->erasesize_shift; 32669423d99SAdrian Hunter do_div(sz, mtd->erasesize); 32769423d99SAdrian Hunter return sz; 32869423d99SAdrian Hunter } 32969423d99SAdrian Hunter 33026cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd) 33169423d99SAdrian Hunter { 33269423d99SAdrian Hunter if (mtd->erasesize_shift) 33369423d99SAdrian Hunter return sz & mtd->erasesize_mask; 33469423d99SAdrian Hunter return do_div(sz, mtd->erasesize); 33569423d99SAdrian Hunter } 33669423d99SAdrian Hunter 33726cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd) 33869423d99SAdrian Hunter { 33969423d99SAdrian Hunter if (mtd->writesize_shift) 34069423d99SAdrian Hunter return sz >> mtd->writesize_shift; 34169423d99SAdrian Hunter do_div(sz, mtd->writesize); 34269423d99SAdrian Hunter return sz; 34369423d99SAdrian Hunter } 34469423d99SAdrian Hunter 34526cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd) 34669423d99SAdrian Hunter { 34769423d99SAdrian Hunter if (mtd->writesize_shift) 34869423d99SAdrian Hunter return sz & mtd->writesize_mask; 34969423d99SAdrian Hunter return do_div(sz, mtd->writesize); 35069423d99SAdrian Hunter } 3511da177e4SLinus Torvalds 352fc002e3cSArtem Bityutskiy static inline int mtd_has_oob(const struct mtd_info *mtd) 353fc002e3cSArtem Bityutskiy { 3543c3c10bbSArtem Bityutskiy return mtd->_read_oob && mtd->_write_oob; 355fc002e3cSArtem Bityutskiy } 356fc002e3cSArtem Bityutskiy 3578f461a73SArtem Bityutskiy static inline int mtd_can_have_bb(const struct mtd_info *mtd) 3588f461a73SArtem Bityutskiy { 3593c3c10bbSArtem Bityutskiy return !!mtd->_block_isbad; 3608f461a73SArtem Bityutskiy } 3618f461a73SArtem Bityutskiy 3621da177e4SLinus Torvalds /* Kernel-side ioctl definitions */ 3631da177e4SLinus Torvalds 364f5671ab3SJamie Iles struct mtd_partition; 365c7975330SDmitry Eremin-Solenikov struct mtd_part_parser_data; 366f5671ab3SJamie Iles 3671c4c215cSDmitry Eremin-Solenikov extern int mtd_device_parse_register(struct mtd_info *mtd, 36826a47346SArtem Bityutskiy const char * const *part_probe_types, 369c7975330SDmitry Eremin-Solenikov struct mtd_part_parser_data *parser_data, 3701c4c215cSDmitry Eremin-Solenikov const struct mtd_partition *defparts, 3711c4c215cSDmitry Eremin-Solenikov int defnr_parts); 37215c60a50SDmitry Eremin-Solenikov #define mtd_device_register(master, parts, nr_parts) \ 37315c60a50SDmitry Eremin-Solenikov mtd_device_parse_register(master, NULL, NULL, parts, nr_parts) 374f5671ab3SJamie Iles extern int mtd_device_unregister(struct mtd_info *master); 3751da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num); 3763bd45657SMaxim Levitsky extern int __get_mtd_device(struct mtd_info *mtd); 3773bd45657SMaxim Levitsky extern void __put_mtd_device(struct mtd_info *mtd); 3787799308fSArtem Bityutskiy extern struct mtd_info *get_mtd_device_nm(const char *name); 3791da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd); 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds struct mtd_notifier { 3831da177e4SLinus Torvalds void (*add)(struct mtd_info *mtd); 3841da177e4SLinus Torvalds void (*remove)(struct mtd_info *mtd); 3851da177e4SLinus Torvalds struct list_head list; 3861da177e4SLinus Torvalds }; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new); 3901da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old); 39133b53716SGrant Erickson void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size); 39233b53716SGrant Erickson 3931da177e4SLinus Torvalds void mtd_erase_callback(struct erase_info *instr); 3941da177e4SLinus Torvalds 3957387ce77SBrian Norris static inline int mtd_is_bitflip(int err) { 3967387ce77SBrian Norris return err == -EUCLEAN; 3977387ce77SBrian Norris } 3981da177e4SLinus Torvalds 3997387ce77SBrian Norris static inline int mtd_is_eccerr(int err) { 4007387ce77SBrian Norris return err == -EBADMSG; 4017387ce77SBrian Norris } 4021da177e4SLinus Torvalds 4037387ce77SBrian Norris static inline int mtd_is_bitflip_or_eccerr(int err) { 4047387ce77SBrian Norris return mtd_is_bitflip(err) || mtd_is_eccerr(err); 4057387ce77SBrian Norris } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */ 408