11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 1999-2003 David Woodhouse <dwmw2@infradead.org> et al. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Released under GPL 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds #ifndef __MTD_MTD_H__ 81da177e4SLinus Torvalds #define __MTD_MTD_H__ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <linux/types.h> 111da177e4SLinus Torvalds #include <linux/module.h> 121da177e4SLinus Torvalds #include <linux/uio.h> 13963a6fb0SNicolas Pitre #include <linux/notifier.h> 141f24b5a8SDavid Brownell #include <linux/device.h> 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/mtd/compatmac.h> 171da177e4SLinus Torvalds #include <mtd/mtd-abi.h> 181da177e4SLinus Torvalds 1969423d99SAdrian Hunter #include <asm/div64.h> 2069423d99SAdrian Hunter 211da177e4SLinus Torvalds #define MTD_CHAR_MAJOR 90 221da177e4SLinus Torvalds #define MTD_BLOCK_MAJOR 31 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #define MTD_ERASE_PENDING 0x01 251da177e4SLinus Torvalds #define MTD_ERASING 0x02 261da177e4SLinus Torvalds #define MTD_ERASE_SUSPEND 0x04 271da177e4SLinus Torvalds #define MTD_ERASE_DONE 0x08 281da177e4SLinus Torvalds #define MTD_ERASE_FAILED 0x10 291da177e4SLinus Torvalds 3069423d99SAdrian Hunter #define MTD_FAIL_ADDR_UNKNOWN -1LL 31bb0eb217SAdrian Hunter 321da177e4SLinus Torvalds /* If the erase fails, fail_addr might indicate exactly which block failed. If 33bb0eb217SAdrian Hunter fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level or was not 341da177e4SLinus Torvalds specific to any particular block. */ 351da177e4SLinus Torvalds struct erase_info { 361da177e4SLinus Torvalds struct mtd_info *mtd; 3769423d99SAdrian Hunter uint64_t addr; 3869423d99SAdrian Hunter uint64_t len; 3969423d99SAdrian Hunter uint64_t fail_addr; 401da177e4SLinus Torvalds u_long time; 411da177e4SLinus Torvalds u_long retries; 4226cdb67cSDavid Woodhouse unsigned dev; 4326cdb67cSDavid Woodhouse unsigned cell; 441da177e4SLinus Torvalds void (*callback) (struct erase_info *self); 451da177e4SLinus Torvalds u_long priv; 461da177e4SLinus Torvalds u_char state; 471da177e4SLinus Torvalds struct erase_info *next; 481da177e4SLinus Torvalds }; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds struct mtd_erase_region_info { 5169423d99SAdrian Hunter uint64_t offset; /* At which this region starts, from the beginning of the MTD */ 5226cdb67cSDavid Woodhouse uint32_t erasesize; /* For this region */ 5326cdb67cSDavid Woodhouse uint32_t numblocks; /* Number of blocks of erasesize in this region */ 540ecbc81aSRodolfo Giometti unsigned long *lockmap; /* If keeping bitmap of locks */ 551da177e4SLinus Torvalds }; 561da177e4SLinus Torvalds 578593fbc6SThomas Gleixner /* 588593fbc6SThomas Gleixner * oob operation modes 598593fbc6SThomas Gleixner * 608593fbc6SThomas Gleixner * MTD_OOB_PLACE: oob data are placed at the given offset 618593fbc6SThomas Gleixner * MTD_OOB_AUTO: oob data are automatically placed at the free areas 628593fbc6SThomas Gleixner * which are defined by the ecclayout 63b64d39d8SMaxim Levitsky * MTD_OOB_RAW: mode to read oob and data without doing ECC checking 648593fbc6SThomas Gleixner */ 658593fbc6SThomas Gleixner typedef enum { 668593fbc6SThomas Gleixner MTD_OOB_PLACE, 678593fbc6SThomas Gleixner MTD_OOB_AUTO, 688593fbc6SThomas Gleixner MTD_OOB_RAW, 698593fbc6SThomas Gleixner } mtd_oob_mode_t; 708593fbc6SThomas Gleixner 718593fbc6SThomas Gleixner /** 728593fbc6SThomas Gleixner * struct mtd_oob_ops - oob operation operands 738593fbc6SThomas Gleixner * @mode: operation mode 748593fbc6SThomas Gleixner * 757014568bSVitaly Wool * @len: number of data bytes to write/read 768593fbc6SThomas Gleixner * 777014568bSVitaly Wool * @retlen: number of data bytes written/read 788593fbc6SThomas Gleixner * 797014568bSVitaly Wool * @ooblen: number of oob bytes to write/read 807014568bSVitaly Wool * @oobretlen: number of oob bytes written/read 818593fbc6SThomas Gleixner * @ooboffs: offset of oob data in the oob area (only relevant when 828593fbc6SThomas Gleixner * mode = MTD_OOB_PLACE) 838593fbc6SThomas Gleixner * @datbuf: data buffer - if NULL only oob data are read/written 848593fbc6SThomas Gleixner * @oobbuf: oob data buffer 8573a4421cSArtem Bityutskiy * 86025dfdafSFrederik Schwarzer * Note, it is allowed to read more than one OOB area at one go, but not write. 8773a4421cSArtem Bityutskiy * The interface assumes that the OOB write requests program only one page's 8873a4421cSArtem Bityutskiy * OOB area. 898593fbc6SThomas Gleixner */ 908593fbc6SThomas Gleixner struct mtd_oob_ops { 918593fbc6SThomas Gleixner mtd_oob_mode_t mode; 928593fbc6SThomas Gleixner size_t len; 938593fbc6SThomas Gleixner size_t retlen; 948593fbc6SThomas Gleixner size_t ooblen; 957014568bSVitaly Wool size_t oobretlen; 968593fbc6SThomas Gleixner uint32_t ooboffs; 978593fbc6SThomas Gleixner uint8_t *datbuf; 988593fbc6SThomas Gleixner uint8_t *oobbuf; 998593fbc6SThomas Gleixner }; 1008593fbc6SThomas Gleixner 1011da177e4SLinus Torvalds struct mtd_info { 1021da177e4SLinus Torvalds u_char type; 10326cdb67cSDavid Woodhouse uint32_t flags; 10469423d99SAdrian Hunter uint64_t size; // Total size of the MTD 1051da177e4SLinus Torvalds 106151e7659SDavid Woodhouse /* "Major" erase size for the device. Naïve users may take this 1071da177e4SLinus Torvalds * to be the only erase size available, or may use the more detailed 1081da177e4SLinus Torvalds * information below if they desire 1091da177e4SLinus Torvalds */ 11026cdb67cSDavid Woodhouse uint32_t erasesize; 111783ed81fSArtem B. Bityutskiy /* Minimal writable flash unit size. In case of NOR flash it is 1 (even 112783ed81fSArtem B. Bityutskiy * though individual bits can be cleared), in case of NAND flash it is 113783ed81fSArtem B. Bityutskiy * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR 114783ed81fSArtem B. Bityutskiy * it is of ECC block size, etc. It is illegal to have writesize = 0. 115783ed81fSArtem B. Bityutskiy * Any driver registering a struct mtd_info must ensure a writesize of 116783ed81fSArtem B. Bityutskiy * 1 or larger. 11728318776SJoern Engel */ 11826cdb67cSDavid Woodhouse uint32_t writesize; 1191da177e4SLinus Torvalds 12026cdb67cSDavid Woodhouse uint32_t oobsize; // Amount of OOB data per block (e.g. 16) 12126cdb67cSDavid Woodhouse uint32_t oobavail; // Available OOB bytes per block 1221da177e4SLinus Torvalds 12369423d99SAdrian Hunter /* 12469423d99SAdrian Hunter * If erasesize is a power of 2 then the shift is stored in 12569423d99SAdrian Hunter * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize. 12669423d99SAdrian Hunter */ 12769423d99SAdrian Hunter unsigned int erasesize_shift; 12869423d99SAdrian Hunter unsigned int writesize_shift; 12969423d99SAdrian Hunter /* Masks based on erasesize_shift and writesize_shift */ 13069423d99SAdrian Hunter unsigned int erasesize_mask; 13169423d99SAdrian Hunter unsigned int writesize_mask; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds // Kernel-only stuff starts here. 134eadcf0d7SGreg Kroah-Hartman const char *name; 1351da177e4SLinus Torvalds int index; 1361da177e4SLinus Torvalds 1375bd34c09SThomas Gleixner /* ecc layout structure pointer - read only ! */ 1385bd34c09SThomas Gleixner struct nand_ecclayout *ecclayout; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* Data for variable erase regions. If numeraseregions is zero, 1411da177e4SLinus Torvalds * it means that the whole device has erasesize as given above. 1421da177e4SLinus Torvalds */ 1431da177e4SLinus Torvalds int numeraseregions; 1441da177e4SLinus Torvalds struct mtd_erase_region_info *eraseregions; 1451da177e4SLinus Torvalds 146b38178eeSJörn Engel /* 147b38178eeSJörn Engel * Erase is an asynchronous operation. Device drivers are supposed 148b38178eeSJörn Engel * to call instr->callback() whenever the operation completes, even 149b38178eeSJörn Engel * if it completes with a failure. 150b38178eeSJörn Engel * Callers are supposed to pass a callback function and wait for it 151b38178eeSJörn Engel * to be called before writing to the block. 152b38178eeSJörn Engel */ 1531da177e4SLinus Torvalds int (*erase) (struct mtd_info *mtd, struct erase_info *instr); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* This stuff for eXecute-In-Place */ 156a98889f3SJared Hulbert /* phys is optional and may be set to NULL */ 157a98889f3SJared Hulbert int (*point) (struct mtd_info *mtd, loff_t from, size_t len, 158a98889f3SJared Hulbert size_t *retlen, void **virt, resource_size_t *phys); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ 161a98889f3SJared Hulbert void (*unpoint) (struct mtd_info *mtd, loff_t from, size_t len); 1621da177e4SLinus Torvalds 163402d3265SDavid Howells /* Allow NOMMU mmap() to directly map the device (if not NULL) 164402d3265SDavid Howells * - return the address to which the offset maps 165402d3265SDavid Howells * - return -ENOSYS to indicate refusal to do the mapping 166402d3265SDavid Howells */ 167402d3265SDavid Howells unsigned long (*get_unmapped_area) (struct mtd_info *mtd, 168402d3265SDavid Howells unsigned long len, 169402d3265SDavid Howells unsigned long offset, 170402d3265SDavid Howells unsigned long flags); 171402d3265SDavid Howells 172402d3265SDavid Howells /* Backing device capabilities for this device 173402d3265SDavid Howells * - provides mmap capabilities 174402d3265SDavid Howells */ 175402d3265SDavid Howells struct backing_dev_info *backing_dev_info; 176402d3265SDavid Howells 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 1791da177e4SLinus Torvalds int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); 1801da177e4SLinus Torvalds 181388bbb09SRichard Purdie /* In blackbox flight recorder like scenarios we want to make successful 182388bbb09SRichard Purdie writes in interrupt context. panic_write() is only intended to be 183388bbb09SRichard Purdie called when its known the kernel is about to panic and we need the 184388bbb09SRichard Purdie write to succeed. Since the kernel is not going to be running for much 185388bbb09SRichard Purdie longer, this function can break locks and delay to ensure the write 186388bbb09SRichard Purdie succeeds (but not sleep). */ 187388bbb09SRichard Purdie 188388bbb09SRichard Purdie int (*panic_write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); 189388bbb09SRichard Purdie 1908593fbc6SThomas Gleixner int (*read_oob) (struct mtd_info *mtd, loff_t from, 1918593fbc6SThomas Gleixner struct mtd_oob_ops *ops); 1928593fbc6SThomas Gleixner int (*write_oob) (struct mtd_info *mtd, loff_t to, 1938593fbc6SThomas Gleixner struct mtd_oob_ops *ops); 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* 1961da177e4SLinus Torvalds * Methods to access the protection register area, present in some 1971da177e4SLinus Torvalds * flash devices. The user data is one time programmable but the 1981da177e4SLinus Torvalds * factory data is read only. 1991da177e4SLinus Torvalds */ 200f77814ddSNicolas Pitre int (*get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len); 2011da177e4SLinus Torvalds int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 202f77814ddSNicolas Pitre int (*get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len); 203f77814ddSNicolas Pitre int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 2041da177e4SLinus Torvalds int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 205f77814ddSNicolas Pitre int (*lock_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len); 2061da177e4SLinus Torvalds 2072528e8cdSThomas Gleixner /* kvec-based read/write methods. 2081da177e4SLinus Torvalds NB: The 'count' parameter is the number of _vectors_, each of 2091da177e4SLinus Torvalds which contains an (ofs, len) tuple. 2101da177e4SLinus Torvalds */ 2111da177e4SLinus Torvalds int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen); 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds /* Sync */ 2141da177e4SLinus Torvalds void (*sync) (struct mtd_info *mtd); 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* Chip-supported device locking */ 21769423d99SAdrian Hunter int (*lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len); 21869423d99SAdrian Hunter int (*unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len); 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* Power Management functions */ 2211da177e4SLinus Torvalds int (*suspend) (struct mtd_info *mtd); 2221da177e4SLinus Torvalds void (*resume) (struct mtd_info *mtd); 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds /* Bad block management functions */ 2251da177e4SLinus Torvalds int (*block_isbad) (struct mtd_info *mtd, loff_t ofs); 2261da177e4SLinus Torvalds int (*block_markbad) (struct mtd_info *mtd, loff_t ofs); 2271da177e4SLinus Torvalds 228963a6fb0SNicolas Pitre struct notifier_block reboot_notifier; /* default mode before reboot */ 229963a6fb0SNicolas Pitre 2307fac4648SThomas Gleixner /* ECC status information */ 2317fac4648SThomas Gleixner struct mtd_ecc_stats ecc_stats; 23229072b96SThomas Gleixner /* Subpage shift (NAND) */ 23329072b96SThomas Gleixner int subpage_sft; 2347fac4648SThomas Gleixner 2351da177e4SLinus Torvalds void *priv; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds struct module *owner; 2381f24b5a8SDavid Brownell struct device dev; 2391da177e4SLinus Torvalds int usecount; 2409fe912ceSArtem Bityutskiy 2419fe912ceSArtem Bityutskiy /* If the driver is something smart, like UBI, it may need to maintain 2429fe912ceSArtem Bityutskiy * its own reference counting. The below functions are only for driver. 2439fe912ceSArtem Bityutskiy * The driver may register its callbacks. These callbacks are not 2449fe912ceSArtem Bityutskiy * supposed to be called by MTD users */ 2459fe912ceSArtem Bityutskiy int (*get_device) (struct mtd_info *mtd); 2469fe912ceSArtem Bityutskiy void (*put_device) (struct mtd_info *mtd); 2471da177e4SLinus Torvalds }; 2481da177e4SLinus Torvalds 2491f24b5a8SDavid Brownell static inline struct mtd_info *dev_to_mtd(struct device *dev) 2501f24b5a8SDavid Brownell { 2516afc4fdbSSaeed Bishara return dev ? dev_get_drvdata(dev) : NULL; 2521f24b5a8SDavid Brownell } 2531f24b5a8SDavid Brownell 25426cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd) 25569423d99SAdrian Hunter { 25669423d99SAdrian Hunter if (mtd->erasesize_shift) 25769423d99SAdrian Hunter return sz >> mtd->erasesize_shift; 25869423d99SAdrian Hunter do_div(sz, mtd->erasesize); 25969423d99SAdrian Hunter return sz; 26069423d99SAdrian Hunter } 26169423d99SAdrian Hunter 26226cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd) 26369423d99SAdrian Hunter { 26469423d99SAdrian Hunter if (mtd->erasesize_shift) 26569423d99SAdrian Hunter return sz & mtd->erasesize_mask; 26669423d99SAdrian Hunter return do_div(sz, mtd->erasesize); 26769423d99SAdrian Hunter } 26869423d99SAdrian Hunter 26926cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd) 27069423d99SAdrian Hunter { 27169423d99SAdrian Hunter if (mtd->writesize_shift) 27269423d99SAdrian Hunter return sz >> mtd->writesize_shift; 27369423d99SAdrian Hunter do_div(sz, mtd->writesize); 27469423d99SAdrian Hunter return sz; 27569423d99SAdrian Hunter } 27669423d99SAdrian Hunter 27726cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd) 27869423d99SAdrian Hunter { 27969423d99SAdrian Hunter if (mtd->writesize_shift) 28069423d99SAdrian Hunter return sz & mtd->writesize_mask; 28169423d99SAdrian Hunter return do_div(sz, mtd->writesize); 28269423d99SAdrian Hunter } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* Kernel-side ioctl definitions */ 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds extern int add_mtd_device(struct mtd_info *mtd); 2871da177e4SLinus Torvalds extern int del_mtd_device (struct mtd_info *mtd); 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num); 2903bd45657SMaxim Levitsky extern int __get_mtd_device(struct mtd_info *mtd); 2913bd45657SMaxim Levitsky extern void __put_mtd_device(struct mtd_info *mtd); 2927799308fSArtem Bityutskiy extern struct mtd_info *get_mtd_device_nm(const char *name); 2931da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds struct mtd_notifier { 2971da177e4SLinus Torvalds void (*add)(struct mtd_info *mtd); 2981da177e4SLinus Torvalds void (*remove)(struct mtd_info *mtd); 2991da177e4SLinus Torvalds struct list_head list; 3001da177e4SLinus Torvalds }; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new); 3041da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 3071da177e4SLinus Torvalds unsigned long count, loff_t to, size_t *retlen); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs, 3101da177e4SLinus Torvalds unsigned long count, loff_t from, size_t *retlen); 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds #ifdef CONFIG_MTD_PARTITIONS 3131da177e4SLinus Torvalds void mtd_erase_callback(struct erase_info *instr); 3141da177e4SLinus Torvalds #else 3151da177e4SLinus Torvalds static inline void mtd_erase_callback(struct erase_info *instr) 3161da177e4SLinus Torvalds { 3171da177e4SLinus Torvalds if (instr->callback) 3181da177e4SLinus Torvalds instr->callback(instr); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds #endif 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds /* 3231da177e4SLinus Torvalds * Debugging macro and defines 3241da177e4SLinus Torvalds */ 3251da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL0 (0) /* Quiet */ 3261da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL1 (1) /* Audible */ 3271da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL2 (2) /* Loud */ 3281da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL3 (3) /* Noisy */ 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds #ifdef CONFIG_MTD_DEBUG 3311da177e4SLinus Torvalds #define DEBUG(n, args...) \ 3321da177e4SLinus Torvalds do { \ 3331da177e4SLinus Torvalds if (n <= CONFIG_MTD_DEBUG_VERBOSE) \ 3341da177e4SLinus Torvalds printk(KERN_INFO args); \ 3351da177e4SLinus Torvalds } while(0) 3361da177e4SLinus Torvalds #else /* CONFIG_MTD_DEBUG */ 33795b1bc20SDavid Brownell #define DEBUG(n, args...) \ 33895b1bc20SDavid Brownell do { \ 33995b1bc20SDavid Brownell if (0) \ 34095b1bc20SDavid Brownell printk(KERN_INFO args); \ 34195b1bc20SDavid Brownell } while(0) 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds #endif /* CONFIG_MTD_DEBUG */ 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */ 346