xref: /openbmc/linux/include/linux/mtd/mtd.h (revision adbbc3bc)
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_ERASE_PENDING	0x01
331da177e4SLinus Torvalds #define MTD_ERASING		0x02
341da177e4SLinus Torvalds #define MTD_ERASE_SUSPEND	0x04
351da177e4SLinus Torvalds #define MTD_ERASE_DONE		0x08
361da177e4SLinus Torvalds #define MTD_ERASE_FAILED	0x10
371da177e4SLinus Torvalds 
3869423d99SAdrian Hunter #define MTD_FAIL_ADDR_UNKNOWN -1LL
39bb0eb217SAdrian Hunter 
40e2e24e8eSBrian Norris /*
41e2e24e8eSBrian Norris  * If the erase fails, fail_addr might indicate exactly which block failed. If
42e2e24e8eSBrian Norris  * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
43e2e24e8eSBrian Norris  * or was not specific to any particular block.
44e2e24e8eSBrian Norris  */
451da177e4SLinus Torvalds struct erase_info {
461da177e4SLinus Torvalds 	struct mtd_info *mtd;
4769423d99SAdrian Hunter 	uint64_t addr;
4869423d99SAdrian Hunter 	uint64_t len;
4969423d99SAdrian Hunter 	uint64_t fail_addr;
501da177e4SLinus Torvalds 	u_long time;
511da177e4SLinus Torvalds 	u_long retries;
5226cdb67cSDavid Woodhouse 	unsigned dev;
5326cdb67cSDavid Woodhouse 	unsigned cell;
541da177e4SLinus Torvalds 	void (*callback) (struct erase_info *self);
551da177e4SLinus Torvalds 	u_long priv;
561da177e4SLinus Torvalds 	u_char state;
571da177e4SLinus Torvalds 	struct erase_info *next;
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds struct mtd_erase_region_info {
6169423d99SAdrian Hunter 	uint64_t offset;		/* At which this region starts, from the beginning of the MTD */
6226cdb67cSDavid Woodhouse 	uint32_t erasesize;		/* For this region */
6326cdb67cSDavid Woodhouse 	uint32_t numblocks;		/* Number of blocks of erasesize in this region */
640ecbc81aSRodolfo Giometti 	unsigned long *lockmap;		/* If keeping bitmap of locks */
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
678593fbc6SThomas Gleixner /**
688593fbc6SThomas Gleixner  * struct mtd_oob_ops - oob operation operands
698593fbc6SThomas Gleixner  * @mode:	operation mode
708593fbc6SThomas Gleixner  *
717014568bSVitaly Wool  * @len:	number of data bytes to write/read
728593fbc6SThomas Gleixner  *
737014568bSVitaly Wool  * @retlen:	number of data bytes written/read
748593fbc6SThomas Gleixner  *
757014568bSVitaly Wool  * @ooblen:	number of oob bytes to write/read
767014568bSVitaly Wool  * @oobretlen:	number of oob bytes written/read
778593fbc6SThomas Gleixner  * @ooboffs:	offset of oob data in the oob area (only relevant when
784180f24aSBrian Norris  *		mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
798593fbc6SThomas Gleixner  * @datbuf:	data buffer - if NULL only oob data are read/written
808593fbc6SThomas Gleixner  * @oobbuf:	oob data buffer
8173a4421cSArtem Bityutskiy  *
82025dfdafSFrederik Schwarzer  * Note, it is allowed to read more than one OOB area at one go, but not write.
8373a4421cSArtem Bityutskiy  * The interface assumes that the OOB write requests program only one page's
8473a4421cSArtem Bityutskiy  * OOB area.
858593fbc6SThomas Gleixner  */
868593fbc6SThomas Gleixner struct mtd_oob_ops {
87905c6bcdSBrian Norris 	unsigned int	mode;
888593fbc6SThomas Gleixner 	size_t		len;
898593fbc6SThomas Gleixner 	size_t		retlen;
908593fbc6SThomas Gleixner 	size_t		ooblen;
917014568bSVitaly Wool 	size_t		oobretlen;
928593fbc6SThomas Gleixner 	uint32_t	ooboffs;
938593fbc6SThomas Gleixner 	uint8_t		*datbuf;
948593fbc6SThomas Gleixner 	uint8_t		*oobbuf;
958593fbc6SThomas Gleixner };
968593fbc6SThomas Gleixner 
97cc26c3cdSBrian Norris #define MTD_MAX_OOBFREE_ENTRIES_LARGE	32
982f25ae97SVipin Kumar #define MTD_MAX_ECCPOS_ENTRIES_LARGE	640
99cc26c3cdSBrian Norris /*
1000ceacf36SBrian Norris  * Internal ECC layout control structure. For historical reasons, there is a
1010ceacf36SBrian Norris  * similar, smaller struct nand_ecclayout_user (in mtd-abi.h) that is retained
1020ceacf36SBrian Norris  * for export to user-space via the ECCGETLAYOUT ioctl.
1030ceacf36SBrian Norris  * nand_ecclayout should be expandable in the future simply by the above macros.
104adbbc3bcSBoris Brezillon  *
105adbbc3bcSBoris Brezillon  * This structure is now deprecated, you should use struct nand_ecclayout_ops
106adbbc3bcSBoris Brezillon  * to describe your OOB layout.
107cc26c3cdSBrian Norris  */
108cc26c3cdSBrian Norris struct nand_ecclayout {
109cc26c3cdSBrian Norris 	__u32 eccbytes;
110cc26c3cdSBrian Norris 	__u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
111cc26c3cdSBrian Norris 	struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
112cc26c3cdSBrian Norris };
113cc26c3cdSBrian Norris 
11475eb2cecSBoris Brezillon /**
11575eb2cecSBoris Brezillon  * struct mtd_oob_region - oob region definition
11675eb2cecSBoris Brezillon  * @offset: region offset
11775eb2cecSBoris Brezillon  * @length: region length
11875eb2cecSBoris Brezillon  *
11975eb2cecSBoris Brezillon  * This structure describes a region of the OOB area, and is used
12075eb2cecSBoris Brezillon  * to retrieve ECC or free bytes sections.
12175eb2cecSBoris Brezillon  * Each section is defined by an offset within the OOB area and a
12275eb2cecSBoris Brezillon  * length.
12375eb2cecSBoris Brezillon  */
12475eb2cecSBoris Brezillon struct mtd_oob_region {
12575eb2cecSBoris Brezillon 	u32 offset;
12675eb2cecSBoris Brezillon 	u32 length;
12775eb2cecSBoris Brezillon };
12875eb2cecSBoris Brezillon 
129adbbc3bcSBoris Brezillon /*
130adbbc3bcSBoris Brezillon  * struct mtd_ooblayout_ops - NAND OOB layout operations
131adbbc3bcSBoris Brezillon  * @ecc: function returning an ECC region in the OOB area.
132adbbc3bcSBoris Brezillon  *	 Should return -ERANGE if %section exceeds the total number of
133adbbc3bcSBoris Brezillon  *	 ECC sections.
134adbbc3bcSBoris Brezillon  * @free: function returning a free region in the OOB area.
135adbbc3bcSBoris Brezillon  *	  Should return -ERANGE if %section exceeds the total number of
136adbbc3bcSBoris Brezillon  *	  free sections.
137adbbc3bcSBoris Brezillon  */
138adbbc3bcSBoris Brezillon struct mtd_ooblayout_ops {
139adbbc3bcSBoris Brezillon 	int (*ecc)(struct mtd_info *mtd, int section,
140adbbc3bcSBoris Brezillon 		   struct mtd_oob_region *oobecc);
141adbbc3bcSBoris Brezillon 	int (*free)(struct mtd_info *mtd, int section,
142adbbc3bcSBoris Brezillon 		    struct mtd_oob_region *oobfree);
143adbbc3bcSBoris Brezillon };
144adbbc3bcSBoris Brezillon 
145de477254SPaul Gortmaker struct module;	/* only needed for owner field in mtd_info */
146de477254SPaul Gortmaker 
1471da177e4SLinus Torvalds struct mtd_info {
1481da177e4SLinus Torvalds 	u_char type;
14926cdb67cSDavid Woodhouse 	uint32_t flags;
15069423d99SAdrian Hunter 	uint64_t size;	 // Total size of the MTD
1511da177e4SLinus Torvalds 
152151e7659SDavid Woodhouse 	/* "Major" erase size for the device. Naïve users may take this
1531da177e4SLinus Torvalds 	 * to be the only erase size available, or may use the more detailed
1541da177e4SLinus Torvalds 	 * information below if they desire
1551da177e4SLinus Torvalds 	 */
15626cdb67cSDavid Woodhouse 	uint32_t erasesize;
157783ed81fSArtem B. Bityutskiy 	/* Minimal writable flash unit size. In case of NOR flash it is 1 (even
158783ed81fSArtem B. Bityutskiy 	 * though individual bits can be cleared), in case of NAND flash it is
159783ed81fSArtem B. Bityutskiy 	 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
160783ed81fSArtem B. Bityutskiy 	 * it is of ECC block size, etc. It is illegal to have writesize = 0.
161783ed81fSArtem B. Bityutskiy 	 * Any driver registering a struct mtd_info must ensure a writesize of
162783ed81fSArtem B. Bityutskiy 	 * 1 or larger.
16328318776SJoern Engel 	 */
16426cdb67cSDavid Woodhouse 	uint32_t writesize;
1651da177e4SLinus Torvalds 
1660e4ca7e5SAnatolij Gustschin 	/*
1670e4ca7e5SAnatolij Gustschin 	 * Size of the write buffer used by the MTD. MTD devices having a write
1680e4ca7e5SAnatolij Gustschin 	 * buffer can write multiple writesize chunks at a time. E.g. while
1690e4ca7e5SAnatolij Gustschin 	 * writing 4 * writesize bytes to a device with 2 * writesize bytes
1700e4ca7e5SAnatolij Gustschin 	 * buffer the MTD driver can (but doesn't have to) do 2 writesize
1710e4ca7e5SAnatolij Gustschin 	 * operations, but not 4. Currently, all NANDs have writebufsize
1720e4ca7e5SAnatolij Gustschin 	 * equivalent to writesize (NAND page size). Some NOR flashes do have
1730e4ca7e5SAnatolij Gustschin 	 * writebufsize greater than writesize.
1740e4ca7e5SAnatolij Gustschin 	 */
1750e4ca7e5SAnatolij Gustschin 	uint32_t writebufsize;
1760e4ca7e5SAnatolij Gustschin 
17726cdb67cSDavid Woodhouse 	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
17826cdb67cSDavid Woodhouse 	uint32_t oobavail;  // Available OOB bytes per block
1791da177e4SLinus Torvalds 
18069423d99SAdrian Hunter 	/*
18169423d99SAdrian Hunter 	 * If erasesize is a power of 2 then the shift is stored in
18269423d99SAdrian Hunter 	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
18369423d99SAdrian Hunter 	 */
18469423d99SAdrian Hunter 	unsigned int erasesize_shift;
18569423d99SAdrian Hunter 	unsigned int writesize_shift;
18669423d99SAdrian Hunter 	/* Masks based on erasesize_shift and writesize_shift */
18769423d99SAdrian Hunter 	unsigned int erasesize_mask;
18869423d99SAdrian Hunter 	unsigned int writesize_mask;
1891da177e4SLinus Torvalds 
190d062d4edSMike Dunn 	/*
191d062d4edSMike Dunn 	 * read ops return -EUCLEAN if max number of bitflips corrected on any
192d062d4edSMike Dunn 	 * one region comprising an ecc step equals or exceeds this value.
193d062d4edSMike Dunn 	 * Settable by driver, else defaults to ecc_strength.  User can override
194d062d4edSMike Dunn 	 * in sysfs.  N.B. The meaning of the -EUCLEAN return code has changed;
195d062d4edSMike Dunn 	 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
196d062d4edSMike Dunn 	 */
197d062d4edSMike Dunn 	unsigned int bitflip_threshold;
198d062d4edSMike Dunn 
1991da177e4SLinus Torvalds 	// Kernel-only stuff starts here.
200eadcf0d7SGreg Kroah-Hartman 	const char *name;
2011da177e4SLinus Torvalds 	int index;
2021da177e4SLinus Torvalds 
203adbbc3bcSBoris Brezillon 	/* [Deprecated] ECC layout structure pointer - read only! */
2045bd34c09SThomas Gleixner 	struct nand_ecclayout *ecclayout;
2051da177e4SLinus Torvalds 
206adbbc3bcSBoris Brezillon 	/* OOB layout description */
207adbbc3bcSBoris Brezillon 	const struct mtd_ooblayout_ops *ooblayout;
208adbbc3bcSBoris Brezillon 
209c41a0582SHuang Shijie 	/* the ecc step size. */
210c41a0582SHuang Shijie 	unsigned int ecc_step_size;
211c41a0582SHuang Shijie 
21286c2072bSMike Dunn 	/* max number of correctible bit errors per ecc step */
2131d0b95b0SMike Dunn 	unsigned int ecc_strength;
2141d0b95b0SMike Dunn 
2151da177e4SLinus Torvalds 	/* Data for variable erase regions. If numeraseregions is zero,
2161da177e4SLinus Torvalds 	 * it means that the whole device has erasesize as given above.
2171da177e4SLinus Torvalds 	 */
2181da177e4SLinus Torvalds 	int numeraseregions;
2191da177e4SLinus Torvalds 	struct mtd_erase_region_info *eraseregions;
2201da177e4SLinus Torvalds 
221b38178eeSJörn Engel 	/*
2227e1f0dc0SArtem Bityutskiy 	 * Do not call via these pointers, use corresponding mtd_*()
2237e1f0dc0SArtem Bityutskiy 	 * wrappers instead.
224b38178eeSJörn Engel 	 */
2253c3c10bbSArtem Bityutskiy 	int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
2263c3c10bbSArtem Bityutskiy 	int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
227a98889f3SJared Hulbert 		       size_t *retlen, void **virt, resource_size_t *phys);
2285e4e6e3fSArtem Bityutskiy 	int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
2293c3c10bbSArtem Bityutskiy 	unsigned long (*_get_unmapped_area) (struct mtd_info *mtd,
230402d3265SDavid Howells 					     unsigned long len,
231402d3265SDavid Howells 					     unsigned long offset,
232402d3265SDavid Howells 					     unsigned long flags);
2333c3c10bbSArtem Bityutskiy 	int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
234329ad399SArtem Bityutskiy 		      size_t *retlen, u_char *buf);
2353c3c10bbSArtem Bityutskiy 	int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
236eda95cbfSArtem Bityutskiy 		       size_t *retlen, const u_char *buf);
2373c3c10bbSArtem Bityutskiy 	int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
2387ae79d7fSArtem Bityutskiy 			     size_t *retlen, const u_char *buf);
2393c3c10bbSArtem Bityutskiy 	int (*_read_oob) (struct mtd_info *mtd, loff_t from,
240fd2819bbSArtem Bityutskiy 			  struct mtd_oob_ops *ops);
2413c3c10bbSArtem Bityutskiy 	int (*_write_oob) (struct mtd_info *mtd, loff_t to,
242a2cc5ba0SArtem Bityutskiy 			   struct mtd_oob_ops *ops);
2434b78fc42SChristian Riesch 	int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
2444b78fc42SChristian Riesch 				    size_t *retlen, struct otp_info *buf);
2453c3c10bbSArtem Bityutskiy 	int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
246d264f72aSArtem Bityutskiy 				    size_t len, size_t *retlen, u_char *buf);
2474b78fc42SChristian Riesch 	int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
2484b78fc42SChristian Riesch 				    size_t *retlen, struct otp_info *buf);
2493c3c10bbSArtem Bityutskiy 	int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
2504ea1cabbSArtem Bityutskiy 				    size_t len, size_t *retlen, u_char *buf);
2513c3c10bbSArtem Bityutskiy 	int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
2523c3c10bbSArtem Bityutskiy 				     size_t len, size_t *retlen, u_char *buf);
2533c3c10bbSArtem Bityutskiy 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
2544403dbfbSArtem Bityutskiy 				    size_t len);
2553c3c10bbSArtem Bityutskiy 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
256b0a31f7bSArtem Bityutskiy 			unsigned long count, loff_t to, size_t *retlen);
2573c3c10bbSArtem Bityutskiy 	void (*_sync) (struct mtd_info *mtd);
2583c3c10bbSArtem Bityutskiy 	int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
2593c3c10bbSArtem Bityutskiy 	int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
2603c3c10bbSArtem Bityutskiy 	int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
2618471bb73SEzequiel Garcia 	int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
2623c3c10bbSArtem Bityutskiy 	int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
2633c3c10bbSArtem Bityutskiy 	int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
2643c3c10bbSArtem Bityutskiy 	int (*_suspend) (struct mtd_info *mtd);
2653c3c10bbSArtem Bityutskiy 	void (*_resume) (struct mtd_info *mtd);
2663efe41beSBrian Norris 	void (*_reboot) (struct mtd_info *mtd);
267a88d2dc6SArtem Bityutskiy 	/*
268a88d2dc6SArtem Bityutskiy 	 * If the driver is something smart, like UBI, it may need to maintain
269a88d2dc6SArtem Bityutskiy 	 * its own reference counting. The below functions are only for driver.
270a88d2dc6SArtem Bityutskiy 	 */
2713c3c10bbSArtem Bityutskiy 	int (*_get_device) (struct mtd_info *mtd);
2723c3c10bbSArtem Bityutskiy 	void (*_put_device) (struct mtd_info *mtd);
273402d3265SDavid Howells 
274402d3265SDavid Howells 	/* Backing device capabilities for this device
275402d3265SDavid Howells 	 * - provides mmap capabilities
276402d3265SDavid Howells 	 */
277402d3265SDavid Howells 	struct backing_dev_info *backing_dev_info;
278402d3265SDavid Howells 
279963a6fb0SNicolas Pitre 	struct notifier_block reboot_notifier;  /* default mode before reboot */
280963a6fb0SNicolas Pitre 
2817fac4648SThomas Gleixner 	/* ECC status information */
2827fac4648SThomas Gleixner 	struct mtd_ecc_stats ecc_stats;
28329072b96SThomas Gleixner 	/* Subpage shift (NAND) */
28429072b96SThomas Gleixner 	int subpage_sft;
2857fac4648SThomas Gleixner 
2861da177e4SLinus Torvalds 	void *priv;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	struct module *owner;
2891f24b5a8SDavid Brownell 	struct device dev;
2901da177e4SLinus Torvalds 	int usecount;
2911da177e4SLinus Torvalds };
2921da177e4SLinus Torvalds 
29375eb2cecSBoris Brezillon int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
29475eb2cecSBoris Brezillon 		      struct mtd_oob_region *oobecc);
29575eb2cecSBoris Brezillon int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
29675eb2cecSBoris Brezillon 				 int *section,
29775eb2cecSBoris Brezillon 				 struct mtd_oob_region *oobregion);
29875eb2cecSBoris Brezillon int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
29975eb2cecSBoris Brezillon 			       const u8 *oobbuf, int start, int nbytes);
30075eb2cecSBoris Brezillon int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
30175eb2cecSBoris Brezillon 			       u8 *oobbuf, int start, int nbytes);
30275eb2cecSBoris Brezillon int mtd_ooblayout_free(struct mtd_info *mtd, int section,
30375eb2cecSBoris Brezillon 		       struct mtd_oob_region *oobfree);
30475eb2cecSBoris Brezillon int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
30575eb2cecSBoris Brezillon 				const u8 *oobbuf, int start, int nbytes);
30675eb2cecSBoris Brezillon int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
30775eb2cecSBoris Brezillon 				u8 *oobbuf, int start, int nbytes);
30875eb2cecSBoris Brezillon int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
30975eb2cecSBoris Brezillon int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
31075eb2cecSBoris Brezillon 
311adbbc3bcSBoris Brezillon void mtd_set_ecclayout(struct mtd_info *mtd, struct nand_ecclayout *ecclayout);
312adbbc3bcSBoris Brezillon 
313adbbc3bcSBoris Brezillon static inline void mtd_set_ooblayout(struct mtd_info *mtd,
314adbbc3bcSBoris Brezillon 				     const struct mtd_ooblayout_ops *ooblayout)
315036d6543SBoris Brezillon {
316adbbc3bcSBoris Brezillon 	mtd->ooblayout = ooblayout;
317036d6543SBoris Brezillon }
318036d6543SBoris Brezillon 
31928b8b26bSBrian Norris static inline void mtd_set_of_node(struct mtd_info *mtd,
32028b8b26bSBrian Norris 				   struct device_node *np)
32128b8b26bSBrian Norris {
32228b8b26bSBrian Norris 	mtd->dev.of_node = np;
32328b8b26bSBrian Norris }
32428b8b26bSBrian Norris 
32528b8b26bSBrian Norris static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
32628b8b26bSBrian Norris {
32728b8b26bSBrian Norris 	return mtd->dev.of_node;
32828b8b26bSBrian Norris }
32928b8b26bSBrian Norris 
33029f1058aSBoris BREZILLON static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
33129f1058aSBoris BREZILLON {
33229f1058aSBoris BREZILLON 	return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
33329f1058aSBoris BREZILLON }
33429f1058aSBoris BREZILLON 
3358273a0c9SArtem Bityutskiy int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
3368273a0c9SArtem Bityutskiy int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
3378273a0c9SArtem Bityutskiy 	      void **virt, resource_size_t *phys);
3388273a0c9SArtem Bityutskiy int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
3398273a0c9SArtem Bityutskiy unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
3408273a0c9SArtem Bityutskiy 				    unsigned long offset, unsigned long flags);
3418273a0c9SArtem Bityutskiy int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
3428273a0c9SArtem Bityutskiy 	     u_char *buf);
3438273a0c9SArtem Bityutskiy int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
3448273a0c9SArtem Bityutskiy 	      const u_char *buf);
3458273a0c9SArtem Bityutskiy int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
3468273a0c9SArtem Bityutskiy 		    const u_char *buf);
3477ae79d7fSArtem Bityutskiy 
348d2d48480SBrian Norris int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
3490c034fe3SEzequiel Garcia int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
350a2cc5ba0SArtem Bityutskiy 
3514b78fc42SChristian Riesch int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
3524b78fc42SChristian Riesch 			   struct otp_info *buf);
353de3cac93SArtem Bityutskiy int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
354de3cac93SArtem Bityutskiy 			   size_t *retlen, u_char *buf);
3554b78fc42SChristian Riesch int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
3564b78fc42SChristian Riesch 			   struct otp_info *buf);
357de3cac93SArtem Bityutskiy int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
358de3cac93SArtem Bityutskiy 			   size_t *retlen, u_char *buf);
359de3cac93SArtem Bityutskiy int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
360de3cac93SArtem Bityutskiy 			    size_t *retlen, u_char *buf);
361de3cac93SArtem Bityutskiy int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
3624403dbfbSArtem Bityutskiy 
3631dbebd32SArtem Bityutskiy int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
3641dbebd32SArtem Bityutskiy 	       unsigned long count, loff_t to, size_t *retlen);
365b0a31f7bSArtem Bityutskiy 
36685f2f2a8SArtem Bityutskiy static inline void mtd_sync(struct mtd_info *mtd)
36785f2f2a8SArtem Bityutskiy {
3683c3c10bbSArtem Bityutskiy 	if (mtd->_sync)
3693c3c10bbSArtem Bityutskiy 		mtd->_sync(mtd);
37085f2f2a8SArtem Bityutskiy }
37185f2f2a8SArtem Bityutskiy 
3728273a0c9SArtem Bityutskiy int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
3738273a0c9SArtem Bityutskiy int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
3748273a0c9SArtem Bityutskiy int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
3758471bb73SEzequiel Garcia int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
3768273a0c9SArtem Bityutskiy int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
3778273a0c9SArtem Bityutskiy int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
378e95e9786SArtem Bityutskiy 
3793fe4bae8SArtem Bityutskiy static inline int mtd_suspend(struct mtd_info *mtd)
3803fe4bae8SArtem Bityutskiy {
3813c3c10bbSArtem Bityutskiy 	return mtd->_suspend ? mtd->_suspend(mtd) : 0;
3823fe4bae8SArtem Bityutskiy }
3833fe4bae8SArtem Bityutskiy 
384ead995f8SArtem Bityutskiy static inline void mtd_resume(struct mtd_info *mtd)
385ead995f8SArtem Bityutskiy {
3863c3c10bbSArtem Bityutskiy 	if (mtd->_resume)
3873c3c10bbSArtem Bityutskiy 		mtd->_resume(mtd);
388ead995f8SArtem Bityutskiy }
389ead995f8SArtem Bityutskiy 
39026cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
39169423d99SAdrian Hunter {
39269423d99SAdrian Hunter 	if (mtd->erasesize_shift)
39369423d99SAdrian Hunter 		return sz >> mtd->erasesize_shift;
39469423d99SAdrian Hunter 	do_div(sz, mtd->erasesize);
39569423d99SAdrian Hunter 	return sz;
39669423d99SAdrian Hunter }
39769423d99SAdrian Hunter 
39826cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
39969423d99SAdrian Hunter {
40069423d99SAdrian Hunter 	if (mtd->erasesize_shift)
40169423d99SAdrian Hunter 		return sz & mtd->erasesize_mask;
40269423d99SAdrian Hunter 	return do_div(sz, mtd->erasesize);
40369423d99SAdrian Hunter }
40469423d99SAdrian Hunter 
40526cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
40669423d99SAdrian Hunter {
40769423d99SAdrian Hunter 	if (mtd->writesize_shift)
40869423d99SAdrian Hunter 		return sz >> mtd->writesize_shift;
40969423d99SAdrian Hunter 	do_div(sz, mtd->writesize);
41069423d99SAdrian Hunter 	return sz;
41169423d99SAdrian Hunter }
41269423d99SAdrian Hunter 
41326cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
41469423d99SAdrian Hunter {
41569423d99SAdrian Hunter 	if (mtd->writesize_shift)
41669423d99SAdrian Hunter 		return sz & mtd->writesize_mask;
41769423d99SAdrian Hunter 	return do_div(sz, mtd->writesize);
41869423d99SAdrian Hunter }
4191da177e4SLinus Torvalds 
420fc002e3cSArtem Bityutskiy static inline int mtd_has_oob(const struct mtd_info *mtd)
421fc002e3cSArtem Bityutskiy {
4223c3c10bbSArtem Bityutskiy 	return mtd->_read_oob && mtd->_write_oob;
423fc002e3cSArtem Bityutskiy }
424fc002e3cSArtem Bityutskiy 
425818b9739SHuang Shijie static inline int mtd_type_is_nand(const struct mtd_info *mtd)
426818b9739SHuang Shijie {
427818b9739SHuang Shijie 	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
428818b9739SHuang Shijie }
429818b9739SHuang Shijie 
4308f461a73SArtem Bityutskiy static inline int mtd_can_have_bb(const struct mtd_info *mtd)
4318f461a73SArtem Bityutskiy {
4323c3c10bbSArtem Bityutskiy 	return !!mtd->_block_isbad;
4338f461a73SArtem Bityutskiy }
4348f461a73SArtem Bityutskiy 
4351da177e4SLinus Torvalds 	/* Kernel-side ioctl definitions */
4361da177e4SLinus Torvalds 
437f5671ab3SJamie Iles struct mtd_partition;
438c7975330SDmitry Eremin-Solenikov struct mtd_part_parser_data;
439f5671ab3SJamie Iles 
4401c4c215cSDmitry Eremin-Solenikov extern int mtd_device_parse_register(struct mtd_info *mtd,
44126a47346SArtem Bityutskiy 				     const char * const *part_probe_types,
442c7975330SDmitry Eremin-Solenikov 				     struct mtd_part_parser_data *parser_data,
4431c4c215cSDmitry Eremin-Solenikov 				     const struct mtd_partition *defparts,
4441c4c215cSDmitry Eremin-Solenikov 				     int defnr_parts);
44515c60a50SDmitry Eremin-Solenikov #define mtd_device_register(master, parts, nr_parts)	\
44615c60a50SDmitry Eremin-Solenikov 	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
447f5671ab3SJamie Iles extern int mtd_device_unregister(struct mtd_info *master);
4481da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
4493bd45657SMaxim Levitsky extern int __get_mtd_device(struct mtd_info *mtd);
4503bd45657SMaxim Levitsky extern void __put_mtd_device(struct mtd_info *mtd);
4517799308fSArtem Bityutskiy extern struct mtd_info *get_mtd_device_nm(const char *name);
4521da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds struct mtd_notifier {
4561da177e4SLinus Torvalds 	void (*add)(struct mtd_info *mtd);
4571da177e4SLinus Torvalds 	void (*remove)(struct mtd_info *mtd);
4581da177e4SLinus Torvalds 	struct list_head list;
4591da177e4SLinus Torvalds };
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new);
4631da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old);
46433b53716SGrant Erickson void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
46533b53716SGrant Erickson 
4661da177e4SLinus Torvalds void mtd_erase_callback(struct erase_info *instr);
4671da177e4SLinus Torvalds 
4687387ce77SBrian Norris static inline int mtd_is_bitflip(int err) {
4697387ce77SBrian Norris 	return err == -EUCLEAN;
4707387ce77SBrian Norris }
4711da177e4SLinus Torvalds 
4727387ce77SBrian Norris static inline int mtd_is_eccerr(int err) {
4737387ce77SBrian Norris 	return err == -EBADMSG;
4747387ce77SBrian Norris }
4751da177e4SLinus Torvalds 
4767387ce77SBrian Norris static inline int mtd_is_bitflip_or_eccerr(int err) {
4777387ce77SBrian Norris 	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
4787387ce77SBrian Norris }
4791da177e4SLinus Torvalds 
480b4caecd4SChristoph Hellwig unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
481b4caecd4SChristoph Hellwig 
4821da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */
483