11da177e4SLinus Torvalds /* 261ecfa87SThomas Gleixner * $Id: mtd.h,v 1.61 2005/11/07 11:14:54 gleixner Exp $ 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1999-2003 David Woodhouse <dwmw2@infradead.org> et al. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Released under GPL 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #ifndef __MTD_MTD_H__ 101da177e4SLinus Torvalds #define __MTD_MTD_H__ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #ifndef __KERNEL__ 131da177e4SLinus Torvalds #error This is a kernel header. Perhaps include mtd-user.h instead? 141da177e4SLinus Torvalds #endif 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/config.h> 171da177e4SLinus Torvalds #include <linux/types.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/uio.h> 20963a6fb0SNicolas Pitre #include <linux/notifier.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #include <linux/mtd/compatmac.h> 231da177e4SLinus Torvalds #include <mtd/mtd-abi.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #define MTD_CHAR_MAJOR 90 261da177e4SLinus Torvalds #define MTD_BLOCK_MAJOR 31 271da177e4SLinus Torvalds #define MAX_MTD_DEVICES 16 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #define MTD_ERASE_PENDING 0x01 301da177e4SLinus Torvalds #define MTD_ERASING 0x02 311da177e4SLinus Torvalds #define MTD_ERASE_SUSPEND 0x04 321da177e4SLinus Torvalds #define MTD_ERASE_DONE 0x08 331da177e4SLinus Torvalds #define MTD_ERASE_FAILED 0x10 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* If the erase fails, fail_addr might indicate exactly which block failed. If 361da177e4SLinus Torvalds fail_addr = 0xffffffff, the failure was not at the device level or was not 371da177e4SLinus Torvalds specific to any particular block. */ 381da177e4SLinus Torvalds struct erase_info { 391da177e4SLinus Torvalds struct mtd_info *mtd; 401da177e4SLinus Torvalds u_int32_t addr; 411da177e4SLinus Torvalds u_int32_t len; 421da177e4SLinus Torvalds u_int32_t fail_addr; 431da177e4SLinus Torvalds u_long time; 441da177e4SLinus Torvalds u_long retries; 451da177e4SLinus Torvalds u_int dev; 461da177e4SLinus Torvalds u_int cell; 471da177e4SLinus Torvalds void (*callback) (struct erase_info *self); 481da177e4SLinus Torvalds u_long priv; 491da177e4SLinus Torvalds u_char state; 501da177e4SLinus Torvalds struct erase_info *next; 511da177e4SLinus Torvalds }; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds struct mtd_erase_region_info { 541da177e4SLinus Torvalds u_int32_t offset; /* At which this region starts, from the beginning of the MTD */ 551da177e4SLinus Torvalds u_int32_t erasesize; /* For this region */ 561da177e4SLinus Torvalds u_int32_t numblocks; /* Number of blocks of erasesize in this region */ 571da177e4SLinus Torvalds }; 581da177e4SLinus Torvalds 597fac4648SThomas Gleixner /** 607fac4648SThomas Gleixner * struct mtd_ecc_stats - error correction status 617fac4648SThomas Gleixner * 627fac4648SThomas Gleixner * @corrected: number of corrected bits 637fac4648SThomas Gleixner * @failed: number of uncorrectable errors 647fac4648SThomas Gleixner */ 657fac4648SThomas Gleixner struct mtd_ecc_stats { 667fac4648SThomas Gleixner unsigned long corrected; 677fac4648SThomas Gleixner unsigned long failed; 687fac4648SThomas Gleixner }; 697fac4648SThomas Gleixner 708593fbc6SThomas Gleixner /* 718593fbc6SThomas Gleixner * oob operation modes 728593fbc6SThomas Gleixner * 738593fbc6SThomas Gleixner * MTD_OOB_PLACE: oob data are placed at the given offset 748593fbc6SThomas Gleixner * MTD_OOB_AUTO: oob data are automatically placed at the free areas 758593fbc6SThomas Gleixner * which are defined by the ecclayout 768593fbc6SThomas Gleixner * MTD_OOB_RAW: mode to read raw data+oob in one chunk. The oob data 778593fbc6SThomas Gleixner * is inserted into the data. Thats a raw image of the 788593fbc6SThomas Gleixner * flash contents. 798593fbc6SThomas Gleixner */ 808593fbc6SThomas Gleixner typedef enum { 818593fbc6SThomas Gleixner MTD_OOB_PLACE, 828593fbc6SThomas Gleixner MTD_OOB_AUTO, 838593fbc6SThomas Gleixner MTD_OOB_RAW, 848593fbc6SThomas Gleixner } mtd_oob_mode_t; 858593fbc6SThomas Gleixner 868593fbc6SThomas Gleixner /** 878593fbc6SThomas Gleixner * struct mtd_oob_ops - oob operation operands 888593fbc6SThomas Gleixner * @mode: operation mode 898593fbc6SThomas Gleixner * 908593fbc6SThomas Gleixner * @len: number of bytes to write/read. When a data buffer is given 918593fbc6SThomas Gleixner * (datbuf != NULL) this is the number of data bytes. When 928593fbc6SThomas Gleixner + no data buffer is available this is the number of oob bytes. 938593fbc6SThomas Gleixner * 948593fbc6SThomas Gleixner * @retlen: number of bytes written/read. When a data buffer is given 958593fbc6SThomas Gleixner * (datbuf != NULL) this is the number of data bytes. When 968593fbc6SThomas Gleixner + no data buffer is available this is the number of oob bytes. 978593fbc6SThomas Gleixner * 988593fbc6SThomas Gleixner * @ooblen: number of oob bytes per page 998593fbc6SThomas Gleixner * @ooboffs: offset of oob data in the oob area (only relevant when 1008593fbc6SThomas Gleixner * mode = MTD_OOB_PLACE) 1018593fbc6SThomas Gleixner * @datbuf: data buffer - if NULL only oob data are read/written 1028593fbc6SThomas Gleixner * @oobbuf: oob data buffer 1038593fbc6SThomas Gleixner */ 1048593fbc6SThomas Gleixner struct mtd_oob_ops { 1058593fbc6SThomas Gleixner mtd_oob_mode_t mode; 1068593fbc6SThomas Gleixner size_t len; 1078593fbc6SThomas Gleixner size_t retlen; 1088593fbc6SThomas Gleixner size_t ooblen; 1098593fbc6SThomas Gleixner uint32_t ooboffs; 1108593fbc6SThomas Gleixner uint8_t *datbuf; 1118593fbc6SThomas Gleixner uint8_t *oobbuf; 1128593fbc6SThomas Gleixner }; 1138593fbc6SThomas Gleixner 1141da177e4SLinus Torvalds struct mtd_info { 1151da177e4SLinus Torvalds u_char type; 1161da177e4SLinus Torvalds u_int32_t flags; 1171da177e4SLinus Torvalds u_int32_t size; // Total size of the MTD 1181da177e4SLinus Torvalds 119151e7659SDavid Woodhouse /* "Major" erase size for the device. Naïve users may take this 1201da177e4SLinus Torvalds * to be the only erase size available, or may use the more detailed 1211da177e4SLinus Torvalds * information below if they desire 1221da177e4SLinus Torvalds */ 1231da177e4SLinus Torvalds u_int32_t erasesize; 12428318776SJoern Engel /* Smallest availlable size for writing to the device. For NAND, 12528318776SJoern Engel * this is the page size, for some NOR chips, the size of ECC 12628318776SJoern Engel * covered blocks. 12728318776SJoern Engel */ 12828318776SJoern Engel u_int32_t writesize; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds u_int32_t oobsize; // Amount of OOB data per block (e.g. 16) 1311da177e4SLinus Torvalds u_int32_t ecctype; 1321da177e4SLinus Torvalds u_int32_t eccsize; 1331da177e4SLinus Torvalds 134638d9838SNicolas Pitre /* 135638d9838SNicolas Pitre * Reuse some of the above unused fields in the case of NOR flash 136638d9838SNicolas Pitre * with configurable programming regions to avoid modifying the 137638d9838SNicolas Pitre * user visible structure layout/size. Only valid when the 138638d9838SNicolas Pitre * MTD_PROGRAM_REGIONS flag is set. 139638d9838SNicolas Pitre * (Maybe we should have an union for those?) 140638d9838SNicolas Pitre */ 141638d9838SNicolas Pitre #define MTD_PROGREGION_CTRLMODE_VALID(mtd) (mtd)->oobsize 142638d9838SNicolas Pitre #define MTD_PROGREGION_CTRLMODE_INVALID(mtd) (mtd)->ecctype 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds // Kernel-only stuff starts here. 1451da177e4SLinus Torvalds char *name; 1461da177e4SLinus Torvalds int index; 1471da177e4SLinus Torvalds 1485bd34c09SThomas Gleixner /* ecc layout structure pointer - read only ! */ 1495bd34c09SThomas Gleixner struct nand_ecclayout *ecclayout; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds /* Data for variable erase regions. If numeraseregions is zero, 1521da177e4SLinus Torvalds * it means that the whole device has erasesize as given above. 1531da177e4SLinus Torvalds */ 1541da177e4SLinus Torvalds int numeraseregions; 1551da177e4SLinus Torvalds struct mtd_erase_region_info *eraseregions; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* This really shouldn't be here. It can go away in 2.5 */ 1581da177e4SLinus Torvalds u_int32_t bank_size; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds int (*erase) (struct mtd_info *mtd, struct erase_info *instr); 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* This stuff for eXecute-In-Place */ 1631da177e4SLinus Torvalds int (*point) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf); 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ 1661da177e4SLinus Torvalds void (*unpoint) (struct mtd_info *mtd, u_char * addr, loff_t from, size_t len); 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 1701da177e4SLinus Torvalds int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); 1711da177e4SLinus Torvalds 1728593fbc6SThomas Gleixner int (*read_oob) (struct mtd_info *mtd, loff_t from, 1738593fbc6SThomas Gleixner struct mtd_oob_ops *ops); 1748593fbc6SThomas Gleixner int (*write_oob) (struct mtd_info *mtd, loff_t to, 1758593fbc6SThomas Gleixner struct mtd_oob_ops *ops); 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds /* 1781da177e4SLinus Torvalds * Methods to access the protection register area, present in some 1791da177e4SLinus Torvalds * flash devices. The user data is one time programmable but the 1801da177e4SLinus Torvalds * factory data is read only. 1811da177e4SLinus Torvalds */ 182f77814ddSNicolas Pitre int (*get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len); 1831da177e4SLinus Torvalds int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 184f77814ddSNicolas Pitre int (*get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len); 185f77814ddSNicolas Pitre int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 1861da177e4SLinus Torvalds int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); 187f77814ddSNicolas Pitre int (*lock_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len); 1881da177e4SLinus Torvalds 1892528e8cdSThomas Gleixner /* kvec-based read/write methods. 1901da177e4SLinus Torvalds NB: The 'count' parameter is the number of _vectors_, each of 1911da177e4SLinus Torvalds which contains an (ofs, len) tuple. 1921da177e4SLinus Torvalds */ 1931da177e4SLinus Torvalds int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen); 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* Sync */ 1961da177e4SLinus Torvalds void (*sync) (struct mtd_info *mtd); 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds /* Chip-supported device locking */ 1991da177e4SLinus Torvalds int (*lock) (struct mtd_info *mtd, loff_t ofs, size_t len); 2001da177e4SLinus Torvalds int (*unlock) (struct mtd_info *mtd, loff_t ofs, size_t len); 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* Power Management functions */ 2031da177e4SLinus Torvalds int (*suspend) (struct mtd_info *mtd); 2041da177e4SLinus Torvalds void (*resume) (struct mtd_info *mtd); 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* Bad block management functions */ 2071da177e4SLinus Torvalds int (*block_isbad) (struct mtd_info *mtd, loff_t ofs); 2081da177e4SLinus Torvalds int (*block_markbad) (struct mtd_info *mtd, loff_t ofs); 2091da177e4SLinus Torvalds 210963a6fb0SNicolas Pitre struct notifier_block reboot_notifier; /* default mode before reboot */ 211963a6fb0SNicolas Pitre 2127fac4648SThomas Gleixner /* ECC status information */ 2137fac4648SThomas Gleixner struct mtd_ecc_stats ecc_stats; 2147fac4648SThomas Gleixner 2151da177e4SLinus Torvalds void *priv; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds struct module *owner; 2181da177e4SLinus Torvalds int usecount; 2191da177e4SLinus Torvalds }; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* Kernel-side ioctl definitions */ 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds extern int add_mtd_device(struct mtd_info *mtd); 2251da177e4SLinus Torvalds extern int del_mtd_device (struct mtd_info *mtd); 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num); 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd); 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds struct mtd_notifier { 2331da177e4SLinus Torvalds void (*add)(struct mtd_info *mtd); 2341da177e4SLinus Torvalds void (*remove)(struct mtd_info *mtd); 2351da177e4SLinus Torvalds struct list_head list; 2361da177e4SLinus Torvalds }; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new); 2401da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 2431da177e4SLinus Torvalds unsigned long count, loff_t to, size_t *retlen); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs, 2461da177e4SLinus Torvalds unsigned long count, loff_t from, size_t *retlen); 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds #ifdef CONFIG_MTD_PARTITIONS 2491da177e4SLinus Torvalds void mtd_erase_callback(struct erase_info *instr); 2501da177e4SLinus Torvalds #else 2511da177e4SLinus Torvalds static inline void mtd_erase_callback(struct erase_info *instr) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds if (instr->callback) 2541da177e4SLinus Torvalds instr->callback(instr); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds #endif 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds /* 2591da177e4SLinus Torvalds * Debugging macro and defines 2601da177e4SLinus Torvalds */ 2611da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL0 (0) /* Quiet */ 2621da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL1 (1) /* Audible */ 2631da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL2 (2) /* Loud */ 2641da177e4SLinus Torvalds #define MTD_DEBUG_LEVEL3 (3) /* Noisy */ 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds #ifdef CONFIG_MTD_DEBUG 2671da177e4SLinus Torvalds #define DEBUG(n, args...) \ 2681da177e4SLinus Torvalds do { \ 2691da177e4SLinus Torvalds if (n <= CONFIG_MTD_DEBUG_VERBOSE) \ 2701da177e4SLinus Torvalds printk(KERN_INFO args); \ 2711da177e4SLinus Torvalds } while(0) 2721da177e4SLinus Torvalds #else /* CONFIG_MTD_DEBUG */ 2731da177e4SLinus Torvalds #define DEBUG(n, args...) do { } while(0) 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds #endif /* CONFIG_MTD_DEBUG */ 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */ 278