xref: /openbmc/linux/include/linux/mtd/mtd.h (revision d3712b9d)
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
101cc26c3cdSBrian Norris #define MTD_MAX_ECCPOS_ENTRIES_LARGE	448
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 
1601da177e4SLinus Torvalds 	// Kernel-only stuff starts here.
161eadcf0d7SGreg Kroah-Hartman 	const char *name;
1621da177e4SLinus Torvalds 	int index;
1631da177e4SLinus Torvalds 
1647854d3f7SBrian Norris 	/* ECC layout structure pointer - read only! */
1655bd34c09SThomas Gleixner 	struct nand_ecclayout *ecclayout;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	/* Data for variable erase regions. If numeraseregions is zero,
1681da177e4SLinus Torvalds 	 * it means that the whole device has erasesize as given above.
1691da177e4SLinus Torvalds 	 */
1701da177e4SLinus Torvalds 	int numeraseregions;
1711da177e4SLinus Torvalds 	struct mtd_erase_region_info *eraseregions;
1721da177e4SLinus Torvalds 
173b38178eeSJörn Engel 	/*
1747e1f0dc0SArtem Bityutskiy 	 * Do not call via these pointers, use corresponding mtd_*()
1757e1f0dc0SArtem Bityutskiy 	 * wrappers instead.
176b38178eeSJörn Engel 	 */
1771da177e4SLinus Torvalds 	int (*erase) (struct mtd_info *mtd, struct erase_info *instr);
178a98889f3SJared Hulbert 	int (*point) (struct mtd_info *mtd, loff_t from, size_t len,
179a98889f3SJared Hulbert 		      size_t *retlen, void **virt, resource_size_t *phys);
180a98889f3SJared Hulbert 	void (*unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
181402d3265SDavid Howells 	unsigned long (*get_unmapped_area) (struct mtd_info *mtd,
182402d3265SDavid Howells 					    unsigned long len,
183402d3265SDavid Howells 					    unsigned long offset,
184402d3265SDavid Howells 					    unsigned long flags);
185329ad399SArtem Bityutskiy 	int (*read) (struct mtd_info *mtd, loff_t from, size_t len,
186329ad399SArtem Bityutskiy 		     size_t *retlen, u_char *buf);
187eda95cbfSArtem Bityutskiy 	int (*write) (struct mtd_info *mtd, loff_t to, size_t len,
188eda95cbfSArtem Bityutskiy 		      size_t *retlen, const u_char *buf);
1897ae79d7fSArtem Bityutskiy 	int (*panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
1907ae79d7fSArtem Bityutskiy 			    size_t *retlen, const u_char *buf);
191fd2819bbSArtem Bityutskiy 	int (*read_oob) (struct mtd_info *mtd, loff_t from,
192fd2819bbSArtem Bityutskiy 			 struct mtd_oob_ops *ops);
193a2cc5ba0SArtem Bityutskiy 	int (*write_oob) (struct mtd_info *mtd, loff_t to,
194a2cc5ba0SArtem Bityutskiy 			  struct mtd_oob_ops *ops);
195a750b5ceSArtem Bityutskiy 	int (*get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf,
196a750b5ceSArtem Bityutskiy 				   size_t len);
197d264f72aSArtem Bityutskiy 	int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
198d264f72aSArtem Bityutskiy 				   size_t len, size_t *retlen, u_char *buf);
199855e5d8cSArtem Bityutskiy 	int (*get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf,
200855e5d8cSArtem Bityutskiy 				   size_t len);
2014ea1cabbSArtem Bityutskiy 	int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
2024ea1cabbSArtem Bityutskiy 				   size_t len, size_t *retlen, u_char *buf);
203482b43adSArtem Bityutskiy 	int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t to, size_t len,
204482b43adSArtem Bityutskiy 				    size_t *retlen, u_char *buf);
2054403dbfbSArtem Bityutskiy 	int (*lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
2064403dbfbSArtem Bityutskiy 				   size_t len);
207b0a31f7bSArtem Bityutskiy 	int (*writev) (struct mtd_info *mtd, const struct kvec *vecs,
208b0a31f7bSArtem Bityutskiy 			unsigned long count, loff_t to, size_t *retlen);
20985f2f2a8SArtem Bityutskiy 	void (*sync) (struct mtd_info *mtd);
2107799f9acSArtem Bityutskiy 	int (*lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
211b66005cdSArtem Bityutskiy 	int (*unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
212e95e9786SArtem Bityutskiy 	int (*is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
2137086c19dSArtem Bityutskiy 	int (*block_isbad) (struct mtd_info *mtd, loff_t ofs);
2145942ddbcSArtem Bityutskiy 	int (*block_markbad) (struct mtd_info *mtd, loff_t ofs);
2153fe4bae8SArtem Bityutskiy 	int (*suspend) (struct mtd_info *mtd);
216ead995f8SArtem Bityutskiy 	void (*resume) (struct mtd_info *mtd);
217a88d2dc6SArtem Bityutskiy 	/*
218a88d2dc6SArtem Bityutskiy 	 * If the driver is something smart, like UBI, it may need to maintain
219a88d2dc6SArtem Bityutskiy 	 * its own reference counting. The below functions are only for driver.
220a88d2dc6SArtem Bityutskiy 	 */
221a88d2dc6SArtem Bityutskiy 	int (*get_device) (struct mtd_info *mtd);
222a88d2dc6SArtem Bityutskiy 	void (*put_device) (struct mtd_info *mtd);
223402d3265SDavid Howells 
224402d3265SDavid Howells 	/* Backing device capabilities for this device
225402d3265SDavid Howells 	 * - provides mmap capabilities
226402d3265SDavid Howells 	 */
227402d3265SDavid Howells 	struct backing_dev_info *backing_dev_info;
228402d3265SDavid Howells 
229963a6fb0SNicolas Pitre 	struct notifier_block reboot_notifier;  /* default mode before reboot */
230963a6fb0SNicolas Pitre 
2317fac4648SThomas Gleixner 	/* ECC status information */
2327fac4648SThomas Gleixner 	struct mtd_ecc_stats ecc_stats;
23329072b96SThomas Gleixner 	/* Subpage shift (NAND) */
23429072b96SThomas Gleixner 	int subpage_sft;
2357fac4648SThomas Gleixner 
2361da177e4SLinus Torvalds 	void *priv;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	struct module *owner;
2391f24b5a8SDavid Brownell 	struct device dev;
2401da177e4SLinus Torvalds 	int usecount;
2411da177e4SLinus Torvalds };
2421da177e4SLinus Torvalds 
2437e1f0dc0SArtem Bityutskiy /*
2447e1f0dc0SArtem Bityutskiy  * Erase is an asynchronous operation.  Device drivers are supposed
2457e1f0dc0SArtem Bityutskiy  * to call instr->callback() whenever the operation completes, even
2467e1f0dc0SArtem Bityutskiy  * if it completes with a failure.
2477e1f0dc0SArtem Bityutskiy  * Callers are supposed to pass a callback function and wait for it
2487e1f0dc0SArtem Bityutskiy  * to be called before writing to the block.
2497e1f0dc0SArtem Bityutskiy  */
2507e1f0dc0SArtem Bityutskiy static inline int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
2517e1f0dc0SArtem Bityutskiy {
2527e1f0dc0SArtem Bityutskiy 	return mtd->erase(mtd, instr);
2537e1f0dc0SArtem Bityutskiy }
2547e1f0dc0SArtem Bityutskiy 
255d35ea200SArtem Bityutskiy /*
256d35ea200SArtem Bityutskiy  * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
257d35ea200SArtem Bityutskiy  */
258d35ea200SArtem Bityutskiy static inline int mtd_point(struct mtd_info *mtd, loff_t from, size_t len,
259d35ea200SArtem Bityutskiy 			    size_t *retlen, void **virt, resource_size_t *phys)
260d35ea200SArtem Bityutskiy {
2619cf075f8SArtem Bityutskiy 	*retlen = 0;
26210934478SArtem Bityutskiy 	if (!mtd->point)
26310934478SArtem Bityutskiy 		return -EOPNOTSUPP;
264d35ea200SArtem Bityutskiy 	return mtd->point(mtd, from, len, retlen, virt, phys);
265d35ea200SArtem Bityutskiy }
266d35ea200SArtem Bityutskiy 
2677219778aSArtem Bityutskiy /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
2687219778aSArtem Bityutskiy static inline void mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
2697219778aSArtem Bityutskiy {
2707219778aSArtem Bityutskiy 	return mtd->unpoint(mtd, from, len);
2717219778aSArtem Bityutskiy }
2727219778aSArtem Bityutskiy 
27304c601bfSArtem Bityutskiy /*
27404c601bfSArtem Bityutskiy  * Allow NOMMU mmap() to directly map the device (if not NULL)
27504c601bfSArtem Bityutskiy  * - return the address to which the offset maps
27604c601bfSArtem Bityutskiy  * - return -ENOSYS to indicate refusal to do the mapping
27704c601bfSArtem Bityutskiy  */
27804c601bfSArtem Bityutskiy static inline unsigned long mtd_get_unmapped_area(struct mtd_info *mtd,
27904c601bfSArtem Bityutskiy 						  unsigned long len,
28004c601bfSArtem Bityutskiy 						  unsigned long offset,
28104c601bfSArtem Bityutskiy 						  unsigned long flags)
28204c601bfSArtem Bityutskiy {
283cd621274SArtem Bityutskiy 	if (!mtd->get_unmapped_area)
284cd621274SArtem Bityutskiy 		return -EOPNOTSUPP;
28504c601bfSArtem Bityutskiy 	return mtd->get_unmapped_area(mtd, len, offset, flags);
28604c601bfSArtem Bityutskiy }
28704c601bfSArtem Bityutskiy 
288329ad399SArtem Bityutskiy static inline int mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
289329ad399SArtem Bityutskiy 			   size_t *retlen, u_char *buf)
290329ad399SArtem Bityutskiy {
291329ad399SArtem Bityutskiy 	return mtd->read(mtd, from, len, retlen, buf);
292329ad399SArtem Bityutskiy }
293329ad399SArtem Bityutskiy 
294eda95cbfSArtem Bityutskiy static inline int mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
295eda95cbfSArtem Bityutskiy 			    size_t *retlen, const u_char *buf)
296eda95cbfSArtem Bityutskiy {
2979cf075f8SArtem Bityutskiy 	*retlen = 0;
29852b02031SArtem Bityutskiy 	if (!mtd->write)
29952b02031SArtem Bityutskiy 		return -EROFS;
300eda95cbfSArtem Bityutskiy 	return mtd->write(mtd, to, len, retlen, buf);
301eda95cbfSArtem Bityutskiy }
302eda95cbfSArtem Bityutskiy 
3037ae79d7fSArtem Bityutskiy /*
3047ae79d7fSArtem Bityutskiy  * In blackbox flight recorder like scenarios we want to make successful writes
3057ae79d7fSArtem Bityutskiy  * in interrupt context. panic_write() is only intended to be called when its
3067ae79d7fSArtem Bityutskiy  * known the kernel is about to panic and we need the write to succeed. Since
3077ae79d7fSArtem Bityutskiy  * the kernel is not going to be running for much longer, this function can
3087ae79d7fSArtem Bityutskiy  * break locks and delay to ensure the write succeeds (but not sleep).
3097ae79d7fSArtem Bityutskiy  */
3107ae79d7fSArtem Bityutskiy static inline int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
3117ae79d7fSArtem Bityutskiy 				  size_t *retlen, const u_char *buf)
3127ae79d7fSArtem Bityutskiy {
3139cf075f8SArtem Bityutskiy 	*retlen = 0;
314016c1291SArtem Bityutskiy 	if (!mtd->panic_write)
315016c1291SArtem Bityutskiy 		return -EOPNOTSUPP;
3167ae79d7fSArtem Bityutskiy 	return mtd->panic_write(mtd, to, len, retlen, buf);
3177ae79d7fSArtem Bityutskiy }
3187ae79d7fSArtem Bityutskiy 
319fd2819bbSArtem Bityutskiy static inline int mtd_read_oob(struct mtd_info *mtd, loff_t from,
320fd2819bbSArtem Bityutskiy 			       struct mtd_oob_ops *ops)
321fd2819bbSArtem Bityutskiy {
3229cf075f8SArtem Bityutskiy 	ops->retlen = ops->oobretlen = 0;
323dac2639fSArtem Bityutskiy 	if (!mtd->read_oob)
324dac2639fSArtem Bityutskiy 		return -EOPNOTSUPP;
325fd2819bbSArtem Bityutskiy 	return mtd->read_oob(mtd, from, ops);
326fd2819bbSArtem Bityutskiy }
327fd2819bbSArtem Bityutskiy 
328a2cc5ba0SArtem Bityutskiy static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
329a2cc5ba0SArtem Bityutskiy 				struct mtd_oob_ops *ops)
330a2cc5ba0SArtem Bityutskiy {
3319cf075f8SArtem Bityutskiy 	ops->retlen = ops->oobretlen = 0;
332dac2639fSArtem Bityutskiy 	if (!mtd->write_oob)
333dac2639fSArtem Bityutskiy 		return -EOPNOTSUPP;
334a2cc5ba0SArtem Bityutskiy 	return mtd->write_oob(mtd, to, ops);
335a2cc5ba0SArtem Bityutskiy }
336a2cc5ba0SArtem Bityutskiy 
337a750b5ceSArtem Bityutskiy /*
338a750b5ceSArtem Bityutskiy  * Method to access the protection register area, present in some flash
339a750b5ceSArtem Bityutskiy  * devices. The user data is one time programmable but the factory data is read
340a750b5ceSArtem Bityutskiy  * only.
341a750b5ceSArtem Bityutskiy  */
342a750b5ceSArtem Bityutskiy static inline int mtd_get_fact_prot_info(struct mtd_info *mtd,
343a750b5ceSArtem Bityutskiy 					 struct otp_info *buf, size_t len)
344a750b5ceSArtem Bityutskiy {
34587e858a9SArtem Bityutskiy 	if (!mtd->get_fact_prot_info)
34687e858a9SArtem Bityutskiy 		return -EOPNOTSUPP;
347a750b5ceSArtem Bityutskiy 	return mtd->get_fact_prot_info(mtd, buf, len);
348a750b5ceSArtem Bityutskiy }
349a750b5ceSArtem Bityutskiy 
350d264f72aSArtem Bityutskiy static inline int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
351d264f72aSArtem Bityutskiy 					 size_t len, size_t *retlen,
352d264f72aSArtem Bityutskiy 					 u_char *buf)
353d264f72aSArtem Bityutskiy {
3549cf075f8SArtem Bityutskiy 	*retlen = 0;
355b6de3d6cSArtem Bityutskiy 	if (!mtd->read_fact_prot_reg)
356b6de3d6cSArtem Bityutskiy 		return -EOPNOTSUPP;
357d264f72aSArtem Bityutskiy 	return mtd->read_fact_prot_reg(mtd, from, len, retlen, buf);
358d264f72aSArtem Bityutskiy }
359d264f72aSArtem Bityutskiy 
360855e5d8cSArtem Bityutskiy static inline int mtd_get_user_prot_info(struct mtd_info *mtd,
361855e5d8cSArtem Bityutskiy 					 struct otp_info *buf,
362855e5d8cSArtem Bityutskiy 					 size_t len)
363855e5d8cSArtem Bityutskiy {
36487e858a9SArtem Bityutskiy 	if (!mtd->get_user_prot_info)
36587e858a9SArtem Bityutskiy 		return -EOPNOTSUPP;
366855e5d8cSArtem Bityutskiy 	return mtd->get_user_prot_info(mtd, buf, len);
367855e5d8cSArtem Bityutskiy }
368855e5d8cSArtem Bityutskiy 
3694ea1cabbSArtem Bityutskiy static inline int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
3704ea1cabbSArtem Bityutskiy 					 size_t len, size_t *retlen,
3714ea1cabbSArtem Bityutskiy 					 u_char *buf)
3724ea1cabbSArtem Bityutskiy {
3739cf075f8SArtem Bityutskiy 	*retlen = 0;
374b6de3d6cSArtem Bityutskiy 	if (!mtd->read_user_prot_reg)
375b6de3d6cSArtem Bityutskiy 		return -EOPNOTSUPP;
3764ea1cabbSArtem Bityutskiy 	return mtd->read_user_prot_reg(mtd, from, len, retlen, buf);
3774ea1cabbSArtem Bityutskiy }
3784ea1cabbSArtem Bityutskiy 
379482b43adSArtem Bityutskiy static inline int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to,
380482b43adSArtem Bityutskiy 					  size_t len, size_t *retlen,
381482b43adSArtem Bityutskiy 					  u_char *buf)
382482b43adSArtem Bityutskiy {
3839cf075f8SArtem Bityutskiy 	*retlen = 0;
38427c151a5SArtem Bityutskiy 	if (!mtd->write_user_prot_reg)
38527c151a5SArtem Bityutskiy 		return -EOPNOTSUPP;
386482b43adSArtem Bityutskiy 	return mtd->write_user_prot_reg(mtd, to, len, retlen, buf);
387482b43adSArtem Bityutskiy }
388482b43adSArtem Bityutskiy 
3894403dbfbSArtem Bityutskiy static inline int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
3904403dbfbSArtem Bityutskiy 					 size_t len)
3914403dbfbSArtem Bityutskiy {
392e2936b2aSArtem Bityutskiy 	if (!mtd->lock_user_prot_reg)
393e2936b2aSArtem Bityutskiy 		return -EOPNOTSUPP;
3944403dbfbSArtem Bityutskiy 	return mtd->lock_user_prot_reg(mtd, from, len);
3954403dbfbSArtem Bityutskiy }
3964403dbfbSArtem Bityutskiy 
3971dbebd32SArtem Bityutskiy int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
3981dbebd32SArtem Bityutskiy 	       unsigned long count, loff_t to, size_t *retlen);
399b0a31f7bSArtem Bityutskiy 
40085f2f2a8SArtem Bityutskiy static inline void mtd_sync(struct mtd_info *mtd)
40185f2f2a8SArtem Bityutskiy {
402327cf292SArtem Bityutskiy 	if (mtd->sync)
40385f2f2a8SArtem Bityutskiy 		mtd->sync(mtd);
40485f2f2a8SArtem Bityutskiy }
40585f2f2a8SArtem Bityutskiy 
4067799f9acSArtem Bityutskiy /* Chip-supported device locking */
4077799f9acSArtem Bityutskiy static inline int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
4087799f9acSArtem Bityutskiy {
40938134565SArtem Bityutskiy 	if (!mtd->lock)
41038134565SArtem Bityutskiy 		return -EOPNOTSUPP;
4117799f9acSArtem Bityutskiy 	return mtd->lock(mtd, ofs, len);
4127799f9acSArtem Bityutskiy }
4137799f9acSArtem Bityutskiy 
414b66005cdSArtem Bityutskiy static inline int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
415b66005cdSArtem Bityutskiy {
41638134565SArtem Bityutskiy 	if (!mtd->unlock)
41738134565SArtem Bityutskiy 		return -EOPNOTSUPP;
418b66005cdSArtem Bityutskiy 	return mtd->unlock(mtd, ofs, len);
419b66005cdSArtem Bityutskiy }
420b66005cdSArtem Bityutskiy 
421e95e9786SArtem Bityutskiy static inline int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
422e95e9786SArtem Bityutskiy {
42338134565SArtem Bityutskiy 	if (!mtd->is_locked)
42438134565SArtem Bityutskiy 		return -EOPNOTSUPP;
425e95e9786SArtem Bityutskiy 	return mtd->is_locked(mtd, ofs, len);
426e95e9786SArtem Bityutskiy }
427e95e9786SArtem Bityutskiy 
4283fe4bae8SArtem Bityutskiy static inline int mtd_suspend(struct mtd_info *mtd)
4293fe4bae8SArtem Bityutskiy {
430079c985eSArtem Bityutskiy 	if (!mtd->suspend)
431079c985eSArtem Bityutskiy 		return -EOPNOTSUPP;
4323fe4bae8SArtem Bityutskiy 	return mtd->suspend(mtd);
4333fe4bae8SArtem Bityutskiy }
4343fe4bae8SArtem Bityutskiy 
435ead995f8SArtem Bityutskiy static inline void mtd_resume(struct mtd_info *mtd)
436ead995f8SArtem Bityutskiy {
437079c985eSArtem Bityutskiy 	if (mtd->resume)
438ead995f8SArtem Bityutskiy 		mtd->resume(mtd);
439ead995f8SArtem Bityutskiy }
440ead995f8SArtem Bityutskiy 
4417086c19dSArtem Bityutskiy static inline int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
4427086c19dSArtem Bityutskiy {
4438f461a73SArtem Bityutskiy 	if (!mtd->block_isbad)
444d3712b9dSLinus Torvalds 		return 0;
4457086c19dSArtem Bityutskiy 	return mtd->block_isbad(mtd, ofs);
4467086c19dSArtem Bityutskiy }
4477086c19dSArtem Bityutskiy 
4485942ddbcSArtem Bityutskiy static inline int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
4495942ddbcSArtem Bityutskiy {
450800ffd34SArtem Bityutskiy 	if (!mtd->block_markbad)
451800ffd34SArtem Bityutskiy 		return -EOPNOTSUPP;
4525942ddbcSArtem Bityutskiy 	return mtd->block_markbad(mtd, ofs);
4535942ddbcSArtem Bityutskiy }
4545942ddbcSArtem Bityutskiy 
45526cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
45669423d99SAdrian Hunter {
45769423d99SAdrian Hunter 	if (mtd->erasesize_shift)
45869423d99SAdrian Hunter 		return sz >> mtd->erasesize_shift;
45969423d99SAdrian Hunter 	do_div(sz, mtd->erasesize);
46069423d99SAdrian Hunter 	return sz;
46169423d99SAdrian Hunter }
46269423d99SAdrian Hunter 
46326cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
46469423d99SAdrian Hunter {
46569423d99SAdrian Hunter 	if (mtd->erasesize_shift)
46669423d99SAdrian Hunter 		return sz & mtd->erasesize_mask;
46769423d99SAdrian Hunter 	return do_div(sz, mtd->erasesize);
46869423d99SAdrian Hunter }
46969423d99SAdrian Hunter 
47026cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
47169423d99SAdrian Hunter {
47269423d99SAdrian Hunter 	if (mtd->writesize_shift)
47369423d99SAdrian Hunter 		return sz >> mtd->writesize_shift;
47469423d99SAdrian Hunter 	do_div(sz, mtd->writesize);
47569423d99SAdrian Hunter 	return sz;
47669423d99SAdrian Hunter }
47769423d99SAdrian Hunter 
47826cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
47969423d99SAdrian Hunter {
48069423d99SAdrian Hunter 	if (mtd->writesize_shift)
48169423d99SAdrian Hunter 		return sz & mtd->writesize_mask;
48269423d99SAdrian Hunter 	return do_div(sz, mtd->writesize);
48369423d99SAdrian Hunter }
4841da177e4SLinus Torvalds 
485fc002e3cSArtem Bityutskiy static inline int mtd_has_oob(const struct mtd_info *mtd)
486fc002e3cSArtem Bityutskiy {
487fc002e3cSArtem Bityutskiy 	return mtd->read_oob && mtd->write_oob;
488fc002e3cSArtem Bityutskiy }
489fc002e3cSArtem Bityutskiy 
4908f461a73SArtem Bityutskiy static inline int mtd_can_have_bb(const struct mtd_info *mtd)
4918f461a73SArtem Bityutskiy {
492d3712b9dSLinus Torvalds 	return 0;
4938f461a73SArtem Bityutskiy }
4948f461a73SArtem Bityutskiy 
4951da177e4SLinus Torvalds 	/* Kernel-side ioctl definitions */
4961da177e4SLinus Torvalds 
497f5671ab3SJamie Iles struct mtd_partition;
498c7975330SDmitry Eremin-Solenikov struct mtd_part_parser_data;
499f5671ab3SJamie Iles 
5001c4c215cSDmitry Eremin-Solenikov extern int mtd_device_parse_register(struct mtd_info *mtd,
5011c4c215cSDmitry Eremin-Solenikov 			      const char **part_probe_types,
502c7975330SDmitry Eremin-Solenikov 			      struct mtd_part_parser_data *parser_data,
5031c4c215cSDmitry Eremin-Solenikov 			      const struct mtd_partition *defparts,
5041c4c215cSDmitry Eremin-Solenikov 			      int defnr_parts);
50515c60a50SDmitry Eremin-Solenikov #define mtd_device_register(master, parts, nr_parts)	\
50615c60a50SDmitry Eremin-Solenikov 	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
507f5671ab3SJamie Iles extern int mtd_device_unregister(struct mtd_info *master);
5081da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
5093bd45657SMaxim Levitsky extern int __get_mtd_device(struct mtd_info *mtd);
5103bd45657SMaxim Levitsky extern void __put_mtd_device(struct mtd_info *mtd);
5117799308fSArtem Bityutskiy extern struct mtd_info *get_mtd_device_nm(const char *name);
5121da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds struct mtd_notifier {
5161da177e4SLinus Torvalds 	void (*add)(struct mtd_info *mtd);
5171da177e4SLinus Torvalds 	void (*remove)(struct mtd_info *mtd);
5181da177e4SLinus Torvalds 	struct list_head list;
5191da177e4SLinus Torvalds };
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new);
5231da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old);
52433b53716SGrant Erickson void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
52533b53716SGrant Erickson 
5261da177e4SLinus Torvalds void mtd_erase_callback(struct erase_info *instr);
5271da177e4SLinus Torvalds 
5287387ce77SBrian Norris static inline int mtd_is_bitflip(int err) {
5297387ce77SBrian Norris 	return err == -EUCLEAN;
5307387ce77SBrian Norris }
5311da177e4SLinus Torvalds 
5327387ce77SBrian Norris static inline int mtd_is_eccerr(int err) {
5337387ce77SBrian Norris 	return err == -EBADMSG;
5347387ce77SBrian Norris }
5351da177e4SLinus Torvalds 
5367387ce77SBrian Norris static inline int mtd_is_bitflip_or_eccerr(int err) {
5377387ce77SBrian Norris 	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
5387387ce77SBrian Norris }
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */
541