xref: /openbmc/linux/include/linux/mtd/mtd.h (revision 7bea6056)
1fd534e9bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
3a1452a37SDavid Woodhouse  * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #ifndef __MTD_MTD_H__
71da177e4SLinus Torvalds #define __MTD_MTD_H__
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/uio.h>
1146b5889cSMiquel Raynal #include <linux/list.h>
12963a6fb0SNicolas Pitre #include <linux/notifier.h>
131f24b5a8SDavid Brownell #include <linux/device.h>
1428309572SCédric Le Goater #include <linux/of.h>
15c4dfa25aSAlban Bedel #include <linux/nvmem-provider.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <mtd/mtd-abi.h>
181da177e4SLinus Torvalds 
1969423d99SAdrian Hunter #include <asm/div64.h>
2069423d99SAdrian Hunter 
2169423d99SAdrian Hunter #define MTD_FAIL_ADDR_UNKNOWN -1LL
22bb0eb217SAdrian Hunter 
238f347c42SBoris Brezillon struct mtd_info;
248f347c42SBoris Brezillon 
25e2e24e8eSBrian Norris /*
26e2e24e8eSBrian Norris  * If the erase fails, fail_addr might indicate exactly which block failed. If
27e2e24e8eSBrian Norris  * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
28e2e24e8eSBrian Norris  * or was not specific to any particular block.
29e2e24e8eSBrian Norris  */
301da177e4SLinus Torvalds struct erase_info {
3169423d99SAdrian Hunter 	uint64_t addr;
3269423d99SAdrian Hunter 	uint64_t len;
3369423d99SAdrian Hunter 	uint64_t fail_addr;
341da177e4SLinus Torvalds };
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds struct mtd_erase_region_info {
3769423d99SAdrian Hunter 	uint64_t offset;		/* At which this region starts, from the beginning of the MTD */
3826cdb67cSDavid Woodhouse 	uint32_t erasesize;		/* For this region */
3926cdb67cSDavid Woodhouse 	uint32_t numblocks;		/* Number of blocks of erasesize in this region */
400ecbc81aSRodolfo Giometti 	unsigned long *lockmap;		/* If keeping bitmap of locks */
411da177e4SLinus Torvalds };
421da177e4SLinus Torvalds 
4365394169SMichał Kępień struct mtd_req_stats {
44*7bea6056SMichał Kępień 	unsigned int uncorrectable_errors;
45*7bea6056SMichał Kępień 	unsigned int corrected_bitflips;
4665394169SMichał Kępień 	unsigned int max_bitflips;
4765394169SMichał Kępień };
4865394169SMichał Kępień 
498593fbc6SThomas Gleixner /**
508593fbc6SThomas Gleixner  * struct mtd_oob_ops - oob operation operands
518593fbc6SThomas Gleixner  * @mode:	operation mode
528593fbc6SThomas Gleixner  *
537014568bSVitaly Wool  * @len:	number of data bytes to write/read
548593fbc6SThomas Gleixner  *
557014568bSVitaly Wool  * @retlen:	number of data bytes written/read
568593fbc6SThomas Gleixner  *
577014568bSVitaly Wool  * @ooblen:	number of oob bytes to write/read
587014568bSVitaly Wool  * @oobretlen:	number of oob bytes written/read
598593fbc6SThomas Gleixner  * @ooboffs:	offset of oob data in the oob area (only relevant when
604180f24aSBrian Norris  *		mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
618593fbc6SThomas Gleixner  * @datbuf:	data buffer - if NULL only oob data are read/written
628593fbc6SThomas Gleixner  * @oobbuf:	oob data buffer
6373a4421cSArtem Bityutskiy  *
64d70420bcSMiquel Raynal  * Note, some MTD drivers do not allow you to write more than one OOB area at
65d70420bcSMiquel Raynal  * one go. If you try to do that on such an MTD device, -EINVAL will be
66d70420bcSMiquel Raynal  * returned. If you want to make your implementation portable on all kind of MTD
67d70420bcSMiquel Raynal  * devices you should split the write request into several sub-requests when the
68d70420bcSMiquel Raynal  * request crosses a page boundary.
698593fbc6SThomas Gleixner  */
708593fbc6SThomas Gleixner struct mtd_oob_ops {
71905c6bcdSBrian Norris 	unsigned int	mode;
728593fbc6SThomas Gleixner 	size_t		len;
738593fbc6SThomas Gleixner 	size_t		retlen;
748593fbc6SThomas Gleixner 	size_t		ooblen;
757014568bSVitaly Wool 	size_t		oobretlen;
768593fbc6SThomas Gleixner 	uint32_t	ooboffs;
778593fbc6SThomas Gleixner 	uint8_t		*datbuf;
788593fbc6SThomas Gleixner 	uint8_t		*oobbuf;
7965394169SMichał Kępień 	struct mtd_req_stats *stats;
808593fbc6SThomas Gleixner };
818593fbc6SThomas Gleixner 
8275eb2cecSBoris Brezillon /**
8375eb2cecSBoris Brezillon  * struct mtd_oob_region - oob region definition
8475eb2cecSBoris Brezillon  * @offset: region offset
8575eb2cecSBoris Brezillon  * @length: region length
8675eb2cecSBoris Brezillon  *
8775eb2cecSBoris Brezillon  * This structure describes a region of the OOB area, and is used
8875eb2cecSBoris Brezillon  * to retrieve ECC or free bytes sections.
8975eb2cecSBoris Brezillon  * Each section is defined by an offset within the OOB area and a
9075eb2cecSBoris Brezillon  * length.
9175eb2cecSBoris Brezillon  */
9275eb2cecSBoris Brezillon struct mtd_oob_region {
9375eb2cecSBoris Brezillon 	u32 offset;
9475eb2cecSBoris Brezillon 	u32 length;
9575eb2cecSBoris Brezillon };
9675eb2cecSBoris Brezillon 
97adbbc3bcSBoris Brezillon /*
98adbbc3bcSBoris Brezillon  * struct mtd_ooblayout_ops - NAND OOB layout operations
99adbbc3bcSBoris Brezillon  * @ecc: function returning an ECC region in the OOB area.
100adbbc3bcSBoris Brezillon  *	 Should return -ERANGE if %section exceeds the total number of
101adbbc3bcSBoris Brezillon  *	 ECC sections.
102adbbc3bcSBoris Brezillon  * @free: function returning a free region in the OOB area.
103adbbc3bcSBoris Brezillon  *	  Should return -ERANGE if %section exceeds the total number of
104adbbc3bcSBoris Brezillon  *	  free sections.
105adbbc3bcSBoris Brezillon  */
106adbbc3bcSBoris Brezillon struct mtd_ooblayout_ops {
107adbbc3bcSBoris Brezillon 	int (*ecc)(struct mtd_info *mtd, int section,
108adbbc3bcSBoris Brezillon 		   struct mtd_oob_region *oobecc);
109adbbc3bcSBoris Brezillon 	int (*free)(struct mtd_info *mtd, int section,
110adbbc3bcSBoris Brezillon 		    struct mtd_oob_region *oobfree);
111adbbc3bcSBoris Brezillon };
112adbbc3bcSBoris Brezillon 
113477b0229SBoris Brezillon /**
114477b0229SBoris Brezillon  * struct mtd_pairing_info - page pairing information
115477b0229SBoris Brezillon  *
116477b0229SBoris Brezillon  * @pair: pair id
117477b0229SBoris Brezillon  * @group: group id
118477b0229SBoris Brezillon  *
119477b0229SBoris Brezillon  * The term "pair" is used here, even though TLC NANDs might group pages by 3
120477b0229SBoris Brezillon  * (3 bits in a single cell). A pair should regroup all pages that are sharing
121477b0229SBoris Brezillon  * the same cell. Pairs are then indexed in ascending order.
122477b0229SBoris Brezillon  *
123477b0229SBoris Brezillon  * @group is defining the position of a page in a given pair. It can also be
124477b0229SBoris Brezillon  * seen as the bit position in the cell: page attached to bit 0 belongs to
125477b0229SBoris Brezillon  * group 0, page attached to bit 1 belongs to group 1, etc.
126477b0229SBoris Brezillon  *
127477b0229SBoris Brezillon  * Example:
128477b0229SBoris Brezillon  * The H27UCG8T2BTR-BC datasheet describes the following pairing scheme:
129477b0229SBoris Brezillon  *
130477b0229SBoris Brezillon  *		group-0		group-1
131477b0229SBoris Brezillon  *
132477b0229SBoris Brezillon  *  pair-0	page-0		page-4
133477b0229SBoris Brezillon  *  pair-1	page-1		page-5
134477b0229SBoris Brezillon  *  pair-2	page-2		page-8
135477b0229SBoris Brezillon  *  ...
136477b0229SBoris Brezillon  *  pair-127	page-251	page-255
137477b0229SBoris Brezillon  *
138477b0229SBoris Brezillon  *
139477b0229SBoris Brezillon  * Note that the "group" and "pair" terms were extracted from Samsung and
140477b0229SBoris Brezillon  * Hynix datasheets, and might be referenced under other names in other
141477b0229SBoris Brezillon  * datasheets (Micron is describing this concept as "shared pages").
142477b0229SBoris Brezillon  */
143477b0229SBoris Brezillon struct mtd_pairing_info {
144477b0229SBoris Brezillon 	int pair;
145477b0229SBoris Brezillon 	int group;
146477b0229SBoris Brezillon };
147477b0229SBoris Brezillon 
148477b0229SBoris Brezillon /**
149477b0229SBoris Brezillon  * struct mtd_pairing_scheme - page pairing scheme description
150477b0229SBoris Brezillon  *
151477b0229SBoris Brezillon  * @ngroups: number of groups. Should be related to the number of bits
152477b0229SBoris Brezillon  *	     per cell.
153477b0229SBoris Brezillon  * @get_info: converts a write-unit (page number within an erase block) into
154477b0229SBoris Brezillon  *	      mtd_pairing information (pair + group). This function should
155477b0229SBoris Brezillon  *	      fill the info parameter based on the wunit index or return
156477b0229SBoris Brezillon  *	      -EINVAL if the wunit parameter is invalid.
157477b0229SBoris Brezillon  * @get_wunit: converts pairing information into a write-unit (page) number.
158477b0229SBoris Brezillon  *	       This function should return the wunit index pointed by the
159477b0229SBoris Brezillon  *	       pairing information described in the info argument. It should
160477b0229SBoris Brezillon  *	       return -EINVAL, if there's no wunit corresponding to the
161477b0229SBoris Brezillon  *	       passed pairing information.
162477b0229SBoris Brezillon  *
163477b0229SBoris Brezillon  * See mtd_pairing_info documentation for a detailed explanation of the
164477b0229SBoris Brezillon  * pair and group concepts.
165477b0229SBoris Brezillon  *
166477b0229SBoris Brezillon  * The mtd_pairing_scheme structure provides a generic solution to represent
167477b0229SBoris Brezillon  * NAND page pairing scheme. Instead of exposing two big tables to do the
168477b0229SBoris Brezillon  * write-unit <-> (pair + group) conversions, we ask the MTD drivers to
169477b0229SBoris Brezillon  * implement the ->get_info() and ->get_wunit() functions.
170477b0229SBoris Brezillon  *
171477b0229SBoris Brezillon  * MTD users will then be able to query these information by using the
172477b0229SBoris Brezillon  * mtd_pairing_info_to_wunit() and mtd_wunit_to_pairing_info() helpers.
173477b0229SBoris Brezillon  *
174477b0229SBoris Brezillon  * @ngroups is here to help MTD users iterating over all the pages in a
175477b0229SBoris Brezillon  * given pair. This value can be retrieved by MTD users using the
176477b0229SBoris Brezillon  * mtd_pairing_groups() helper.
177477b0229SBoris Brezillon  *
178477b0229SBoris Brezillon  * Examples are given in the mtd_pairing_info_to_wunit() and
179477b0229SBoris Brezillon  * mtd_wunit_to_pairing_info() documentation.
180477b0229SBoris Brezillon  */
181477b0229SBoris Brezillon struct mtd_pairing_scheme {
182477b0229SBoris Brezillon 	int ngroups;
183477b0229SBoris Brezillon 	int (*get_info)(struct mtd_info *mtd, int wunit,
184477b0229SBoris Brezillon 			struct mtd_pairing_info *info);
185477b0229SBoris Brezillon 	int (*get_wunit)(struct mtd_info *mtd,
186477b0229SBoris Brezillon 			 const struct mtd_pairing_info *info);
187477b0229SBoris Brezillon };
188477b0229SBoris Brezillon 
189de477254SPaul Gortmaker struct module;	/* only needed for owner field in mtd_info */
190de477254SPaul Gortmaker 
191e8e3edb9SMario Rugiero /**
192e8e3edb9SMario Rugiero  * struct mtd_debug_info - debugging information for an MTD device.
193e8e3edb9SMario Rugiero  *
194e8e3edb9SMario Rugiero  * @dfs_dir: direntry object of the MTD device debugfs directory
195e8e3edb9SMario Rugiero  */
196e8e3edb9SMario Rugiero struct mtd_debug_info {
197e8e3edb9SMario Rugiero 	struct dentry *dfs_dir;
198e8e3edb9SMario Rugiero };
199e8e3edb9SMario Rugiero 
20046b5889cSMiquel Raynal /**
20146b5889cSMiquel Raynal  * struct mtd_part - MTD partition specific fields
20246b5889cSMiquel Raynal  *
20346b5889cSMiquel Raynal  * @node: list node used to add an MTD partition to the parent partition list
20446b5889cSMiquel Raynal  * @offset: offset of the partition relatively to the parent offset
2059e3307a1SBoris Brezillon  * @size: partition size. Should be equal to mtd->size unless
2069e3307a1SBoris Brezillon  *	  MTD_SLC_ON_MLC_EMULATION is set
20746b5889cSMiquel Raynal  * @flags: original flags (before the mtdpart logic decided to tweak them based
20846b5889cSMiquel Raynal  *	   on flash constraints, like eraseblock/pagesize alignment)
20946b5889cSMiquel Raynal  *
21046b5889cSMiquel Raynal  * This struct is embedded in mtd_info and contains partition-specific
21146b5889cSMiquel Raynal  * properties/fields.
21246b5889cSMiquel Raynal  */
21346b5889cSMiquel Raynal struct mtd_part {
21446b5889cSMiquel Raynal 	struct list_head node;
21546b5889cSMiquel Raynal 	u64 offset;
2169e3307a1SBoris Brezillon 	u64 size;
21746b5889cSMiquel Raynal 	u32 flags;
21846b5889cSMiquel Raynal };
21946b5889cSMiquel Raynal 
22046b5889cSMiquel Raynal /**
22146b5889cSMiquel Raynal  * struct mtd_master - MTD master specific fields
22246b5889cSMiquel Raynal  *
22346b5889cSMiquel Raynal  * @partitions_lock: lock protecting accesses to the partition list. Protects
22446b5889cSMiquel Raynal  *		     not only the master partition list, but also all
22546b5889cSMiquel Raynal  *		     sub-partitions.
22646b5889cSMiquel Raynal  * @suspended: et to 1 when the device is suspended, 0 otherwise
22746b5889cSMiquel Raynal  *
22846b5889cSMiquel Raynal  * This struct is embedded in mtd_info and contains master-specific
22946b5889cSMiquel Raynal  * properties/fields. The master is the root MTD device from the MTD partition
23046b5889cSMiquel Raynal  * point of view.
23146b5889cSMiquel Raynal  */
23246b5889cSMiquel Raynal struct mtd_master {
23346b5889cSMiquel Raynal 	struct mutex partitions_lock;
2341ad55288SAlexander Sverdlin 	struct mutex chrdev_lock;
23546b5889cSMiquel Raynal 	unsigned int suspended : 1;
23646b5889cSMiquel Raynal };
23746b5889cSMiquel Raynal 
2381da177e4SLinus Torvalds struct mtd_info {
2391da177e4SLinus Torvalds 	u_char type;
24026cdb67cSDavid Woodhouse 	uint32_t flags;
24169423d99SAdrian Hunter 	uint64_t size;	 // Total size of the MTD
2421da177e4SLinus Torvalds 
243151e7659SDavid Woodhouse 	/* "Major" erase size for the device. Naïve users may take this
2441da177e4SLinus Torvalds 	 * to be the only erase size available, or may use the more detailed
2451da177e4SLinus Torvalds 	 * information below if they desire
2461da177e4SLinus Torvalds 	 */
24726cdb67cSDavid Woodhouse 	uint32_t erasesize;
248783ed81fSArtem B. Bityutskiy 	/* Minimal writable flash unit size. In case of NOR flash it is 1 (even
249783ed81fSArtem B. Bityutskiy 	 * though individual bits can be cleared), in case of NAND flash it is
250783ed81fSArtem B. Bityutskiy 	 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
251783ed81fSArtem B. Bityutskiy 	 * it is of ECC block size, etc. It is illegal to have writesize = 0.
252783ed81fSArtem B. Bityutskiy 	 * Any driver registering a struct mtd_info must ensure a writesize of
253783ed81fSArtem B. Bityutskiy 	 * 1 or larger.
25428318776SJoern Engel 	 */
25526cdb67cSDavid Woodhouse 	uint32_t writesize;
2561da177e4SLinus Torvalds 
2570e4ca7e5SAnatolij Gustschin 	/*
2580e4ca7e5SAnatolij Gustschin 	 * Size of the write buffer used by the MTD. MTD devices having a write
2590e4ca7e5SAnatolij Gustschin 	 * buffer can write multiple writesize chunks at a time. E.g. while
2600e4ca7e5SAnatolij Gustschin 	 * writing 4 * writesize bytes to a device with 2 * writesize bytes
2610e4ca7e5SAnatolij Gustschin 	 * buffer the MTD driver can (but doesn't have to) do 2 writesize
2620e4ca7e5SAnatolij Gustschin 	 * operations, but not 4. Currently, all NANDs have writebufsize
2630e4ca7e5SAnatolij Gustschin 	 * equivalent to writesize (NAND page size). Some NOR flashes do have
2640e4ca7e5SAnatolij Gustschin 	 * writebufsize greater than writesize.
2650e4ca7e5SAnatolij Gustschin 	 */
2660e4ca7e5SAnatolij Gustschin 	uint32_t writebufsize;
2670e4ca7e5SAnatolij Gustschin 
26826cdb67cSDavid Woodhouse 	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
26926cdb67cSDavid Woodhouse 	uint32_t oobavail;  // Available OOB bytes per block
2701da177e4SLinus Torvalds 
27169423d99SAdrian Hunter 	/*
27269423d99SAdrian Hunter 	 * If erasesize is a power of 2 then the shift is stored in
27369423d99SAdrian Hunter 	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
27469423d99SAdrian Hunter 	 */
27569423d99SAdrian Hunter 	unsigned int erasesize_shift;
27669423d99SAdrian Hunter 	unsigned int writesize_shift;
27769423d99SAdrian Hunter 	/* Masks based on erasesize_shift and writesize_shift */
27869423d99SAdrian Hunter 	unsigned int erasesize_mask;
27969423d99SAdrian Hunter 	unsigned int writesize_mask;
2801da177e4SLinus Torvalds 
281d062d4edSMike Dunn 	/*
282d062d4edSMike Dunn 	 * read ops return -EUCLEAN if max number of bitflips corrected on any
283d062d4edSMike Dunn 	 * one region comprising an ecc step equals or exceeds this value.
284d062d4edSMike Dunn 	 * Settable by driver, else defaults to ecc_strength.  User can override
285d062d4edSMike Dunn 	 * in sysfs.  N.B. The meaning of the -EUCLEAN return code has changed;
286d062d4edSMike Dunn 	 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
287d062d4edSMike Dunn 	 */
288d062d4edSMike Dunn 	unsigned int bitflip_threshold;
289d062d4edSMike Dunn 
290971e4aeeSPavel Machek 	/* Kernel-only stuff starts here. */
291eadcf0d7SGreg Kroah-Hartman 	const char *name;
2921da177e4SLinus Torvalds 	int index;
2931da177e4SLinus Torvalds 
294adbbc3bcSBoris Brezillon 	/* OOB layout description */
295adbbc3bcSBoris Brezillon 	const struct mtd_ooblayout_ops *ooblayout;
296adbbc3bcSBoris Brezillon 
297477b0229SBoris Brezillon 	/* NAND pairing scheme, only provided for MLC/TLC NANDs */
298477b0229SBoris Brezillon 	const struct mtd_pairing_scheme *pairing;
299477b0229SBoris Brezillon 
300c41a0582SHuang Shijie 	/* the ecc step size. */
301c41a0582SHuang Shijie 	unsigned int ecc_step_size;
302c41a0582SHuang Shijie 
30386c2072bSMike Dunn 	/* max number of correctible bit errors per ecc step */
3041d0b95b0SMike Dunn 	unsigned int ecc_strength;
3051d0b95b0SMike Dunn 
3061da177e4SLinus Torvalds 	/* Data for variable erase regions. If numeraseregions is zero,
3071da177e4SLinus Torvalds 	 * it means that the whole device has erasesize as given above.
3081da177e4SLinus Torvalds 	 */
3091da177e4SLinus Torvalds 	int numeraseregions;
3101da177e4SLinus Torvalds 	struct mtd_erase_region_info *eraseregions;
3111da177e4SLinus Torvalds 
312b38178eeSJörn Engel 	/*
3137e1f0dc0SArtem Bityutskiy 	 * Do not call via these pointers, use corresponding mtd_*()
3147e1f0dc0SArtem Bityutskiy 	 * wrappers instead.
315b38178eeSJörn Engel 	 */
3163c3c10bbSArtem Bityutskiy 	int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
3173c3c10bbSArtem Bityutskiy 	int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
318a98889f3SJared Hulbert 		       size_t *retlen, void **virt, resource_size_t *phys);
3195e4e6e3fSArtem Bityutskiy 	int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
3203c3c10bbSArtem Bityutskiy 	int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
321329ad399SArtem Bityutskiy 		      size_t *retlen, u_char *buf);
3223c3c10bbSArtem Bityutskiy 	int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
323eda95cbfSArtem Bityutskiy 		       size_t *retlen, const u_char *buf);
3243c3c10bbSArtem Bityutskiy 	int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
3257ae79d7fSArtem Bityutskiy 			     size_t *retlen, const u_char *buf);
3263c3c10bbSArtem Bityutskiy 	int (*_read_oob) (struct mtd_info *mtd, loff_t from,
327fd2819bbSArtem Bityutskiy 			  struct mtd_oob_ops *ops);
3283c3c10bbSArtem Bityutskiy 	int (*_write_oob) (struct mtd_info *mtd, loff_t to,
329a2cc5ba0SArtem Bityutskiy 			   struct mtd_oob_ops *ops);
3304b78fc42SChristian Riesch 	int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
3314b78fc42SChristian Riesch 				    size_t *retlen, struct otp_info *buf);
3323c3c10bbSArtem Bityutskiy 	int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
333d264f72aSArtem Bityutskiy 				    size_t len, size_t *retlen, u_char *buf);
3344b78fc42SChristian Riesch 	int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
3354b78fc42SChristian Riesch 				    size_t *retlen, struct otp_info *buf);
3363c3c10bbSArtem Bityutskiy 	int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
3374ea1cabbSArtem Bityutskiy 				    size_t len, size_t *retlen, u_char *buf);
3383c3c10bbSArtem Bityutskiy 	int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
3391df1fc8cSTudor Ambarus 				     size_t len, size_t *retlen,
3401df1fc8cSTudor Ambarus 				     const u_char *buf);
3413c3c10bbSArtem Bityutskiy 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
3424403dbfbSArtem Bityutskiy 				    size_t len);
343e3c1f1c9SMichael Walle 	int (*_erase_user_prot_reg) (struct mtd_info *mtd, loff_t from,
344e3c1f1c9SMichael Walle 				     size_t len);
3453c3c10bbSArtem Bityutskiy 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
346b0a31f7bSArtem Bityutskiy 			unsigned long count, loff_t to, size_t *retlen);
3473c3c10bbSArtem Bityutskiy 	void (*_sync) (struct mtd_info *mtd);
3483c3c10bbSArtem Bityutskiy 	int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3493c3c10bbSArtem Bityutskiy 	int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3503c3c10bbSArtem Bityutskiy 	int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3518471bb73SEzequiel Garcia 	int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
3523c3c10bbSArtem Bityutskiy 	int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
3533c3c10bbSArtem Bityutskiy 	int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
3546080ef6eSJeff Westfahl 	int (*_max_bad_blocks) (struct mtd_info *mtd, loff_t ofs, size_t len);
3553c3c10bbSArtem Bityutskiy 	int (*_suspend) (struct mtd_info *mtd);
3563c3c10bbSArtem Bityutskiy 	void (*_resume) (struct mtd_info *mtd);
3573efe41beSBrian Norris 	void (*_reboot) (struct mtd_info *mtd);
358a88d2dc6SArtem Bityutskiy 	/*
359a88d2dc6SArtem Bityutskiy 	 * If the driver is something smart, like UBI, it may need to maintain
360a88d2dc6SArtem Bityutskiy 	 * its own reference counting. The below functions are only for driver.
361a88d2dc6SArtem Bityutskiy 	 */
3623c3c10bbSArtem Bityutskiy 	int (*_get_device) (struct mtd_info *mtd);
3633c3c10bbSArtem Bityutskiy 	void (*_put_device) (struct mtd_info *mtd);
364402d3265SDavid Howells 
3659f897bfdSKamal Dasu 	/*
3669f897bfdSKamal Dasu 	 * flag indicates a panic write, low level drivers can take appropriate
3679f897bfdSKamal Dasu 	 * action if required to ensure writes go through
3689f897bfdSKamal Dasu 	 */
3699f897bfdSKamal Dasu 	bool oops_panic_write;
3709f897bfdSKamal Dasu 
371963a6fb0SNicolas Pitre 	struct notifier_block reboot_notifier;  /* default mode before reboot */
372963a6fb0SNicolas Pitre 
3737fac4648SThomas Gleixner 	/* ECC status information */
3747fac4648SThomas Gleixner 	struct mtd_ecc_stats ecc_stats;
37529072b96SThomas Gleixner 	/* Subpage shift (NAND) */
37629072b96SThomas Gleixner 	int subpage_sft;
3777fac4648SThomas Gleixner 
3781da177e4SLinus Torvalds 	void *priv;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	struct module *owner;
3811f24b5a8SDavid Brownell 	struct device dev;
3821da177e4SLinus Torvalds 	int usecount;
383e8e3edb9SMario Rugiero 	struct mtd_debug_info dbg;
384c4dfa25aSAlban Bedel 	struct nvmem_device *nvmem;
3854b361cfaSMichael Walle 	struct nvmem_device *otp_user_nvmem;
3864b361cfaSMichael Walle 	struct nvmem_device *otp_factory_nvmem;
38746b5889cSMiquel Raynal 
38846b5889cSMiquel Raynal 	/*
38946b5889cSMiquel Raynal 	 * Parent device from the MTD partition point of view.
39046b5889cSMiquel Raynal 	 *
39146b5889cSMiquel Raynal 	 * MTD masters do not have any parent, MTD partitions do. The parent
39246b5889cSMiquel Raynal 	 * MTD device can itself be a partition.
39346b5889cSMiquel Raynal 	 */
39446b5889cSMiquel Raynal 	struct mtd_info *parent;
39546b5889cSMiquel Raynal 
39646b5889cSMiquel Raynal 	/* List of partitions attached to this MTD device */
39746b5889cSMiquel Raynal 	struct list_head partitions;
39846b5889cSMiquel Raynal 
39946b5889cSMiquel Raynal 	struct mtd_part part;
40046b5889cSMiquel Raynal 	struct mtd_master master;
4011da177e4SLinus Torvalds };
40246b5889cSMiquel Raynal 
40346b5889cSMiquel Raynal static inline struct mtd_info *mtd_get_master(struct mtd_info *mtd)
40446b5889cSMiquel Raynal {
40546b5889cSMiquel Raynal 	while (mtd->parent)
40646b5889cSMiquel Raynal 		mtd = mtd->parent;
40746b5889cSMiquel Raynal 
40846b5889cSMiquel Raynal 	return mtd;
40946b5889cSMiquel Raynal }
41046b5889cSMiquel Raynal 
41146b5889cSMiquel Raynal static inline u64 mtd_get_master_ofs(struct mtd_info *mtd, u64 ofs)
41246b5889cSMiquel Raynal {
41346b5889cSMiquel Raynal 	while (mtd->parent) {
41446b5889cSMiquel Raynal 		ofs += mtd->part.offset;
41546b5889cSMiquel Raynal 		mtd = mtd->parent;
41646b5889cSMiquel Raynal 	}
41746b5889cSMiquel Raynal 
41846b5889cSMiquel Raynal 	return ofs;
41946b5889cSMiquel Raynal }
42046b5889cSMiquel Raynal 
42146b5889cSMiquel Raynal static inline bool mtd_is_partition(const struct mtd_info *mtd)
42246b5889cSMiquel Raynal {
42346b5889cSMiquel Raynal 	return mtd->parent;
42446b5889cSMiquel Raynal }
42546b5889cSMiquel Raynal 
42646b5889cSMiquel Raynal static inline bool mtd_has_partitions(const struct mtd_info *mtd)
42746b5889cSMiquel Raynal {
42846b5889cSMiquel Raynal 	return !list_empty(&mtd->partitions);
42946b5889cSMiquel Raynal }
4301da177e4SLinus Torvalds 
43175eb2cecSBoris Brezillon int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
43275eb2cecSBoris Brezillon 		      struct mtd_oob_region *oobecc);
43375eb2cecSBoris Brezillon int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
43475eb2cecSBoris Brezillon 				 int *section,
43575eb2cecSBoris Brezillon 				 struct mtd_oob_region *oobregion);
43675eb2cecSBoris Brezillon int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
43775eb2cecSBoris Brezillon 			       const u8 *oobbuf, int start, int nbytes);
43875eb2cecSBoris Brezillon int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
43975eb2cecSBoris Brezillon 			       u8 *oobbuf, int start, int nbytes);
44075eb2cecSBoris Brezillon int mtd_ooblayout_free(struct mtd_info *mtd, int section,
44175eb2cecSBoris Brezillon 		       struct mtd_oob_region *oobfree);
44275eb2cecSBoris Brezillon int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
44375eb2cecSBoris Brezillon 				const u8 *oobbuf, int start, int nbytes);
44475eb2cecSBoris Brezillon int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
44575eb2cecSBoris Brezillon 				u8 *oobbuf, int start, int nbytes);
44675eb2cecSBoris Brezillon int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
44775eb2cecSBoris Brezillon int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
44875eb2cecSBoris Brezillon 
449adbbc3bcSBoris Brezillon static inline void mtd_set_ooblayout(struct mtd_info *mtd,
450adbbc3bcSBoris Brezillon 				     const struct mtd_ooblayout_ops *ooblayout)
451036d6543SBoris Brezillon {
452adbbc3bcSBoris Brezillon 	mtd->ooblayout = ooblayout;
453036d6543SBoris Brezillon }
454036d6543SBoris Brezillon 
455477b0229SBoris Brezillon static inline void mtd_set_pairing_scheme(struct mtd_info *mtd,
456477b0229SBoris Brezillon 				const struct mtd_pairing_scheme *pairing)
457477b0229SBoris Brezillon {
458477b0229SBoris Brezillon 	mtd->pairing = pairing;
459477b0229SBoris Brezillon }
460477b0229SBoris Brezillon 
46128b8b26bSBrian Norris static inline void mtd_set_of_node(struct mtd_info *mtd,
46228b8b26bSBrian Norris 				   struct device_node *np)
46328b8b26bSBrian Norris {
46428b8b26bSBrian Norris 	mtd->dev.of_node = np;
46528309572SCédric Le Goater 	if (!mtd->name)
46628309572SCédric Le Goater 		of_property_read_string(np, "label", &mtd->name);
46728b8b26bSBrian Norris }
46828b8b26bSBrian Norris 
46928b8b26bSBrian Norris static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
47028b8b26bSBrian Norris {
4714a67c9fdSRafał Miłecki 	return dev_of_node(&mtd->dev);
47228b8b26bSBrian Norris }
47328b8b26bSBrian Norris 
4744348433dSMiquel Raynal static inline u32 mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
47529f1058aSBoris BREZILLON {
47629f1058aSBoris BREZILLON 	return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
47729f1058aSBoris BREZILLON }
47829f1058aSBoris BREZILLON 
4796080ef6eSJeff Westfahl static inline int mtd_max_bad_blocks(struct mtd_info *mtd,
4806080ef6eSJeff Westfahl 				     loff_t ofs, size_t len)
4816080ef6eSJeff Westfahl {
48246b5889cSMiquel Raynal 	struct mtd_info *master = mtd_get_master(mtd);
48346b5889cSMiquel Raynal 
48446b5889cSMiquel Raynal 	if (!master->_max_bad_blocks)
4856080ef6eSJeff Westfahl 		return -ENOTSUPP;
4866080ef6eSJeff Westfahl 
4876080ef6eSJeff Westfahl 	if (mtd->size < (len + ofs) || ofs < 0)
4886080ef6eSJeff Westfahl 		return -EINVAL;
4896080ef6eSJeff Westfahl 
49046b5889cSMiquel Raynal 	return master->_max_bad_blocks(master, mtd_get_master_ofs(mtd, ofs),
49146b5889cSMiquel Raynal 				       len);
4926080ef6eSJeff Westfahl }
4936080ef6eSJeff Westfahl 
494477b0229SBoris Brezillon int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
495477b0229SBoris Brezillon 			      struct mtd_pairing_info *info);
496477b0229SBoris Brezillon int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
497477b0229SBoris Brezillon 			      const struct mtd_pairing_info *info);
498477b0229SBoris Brezillon int mtd_pairing_groups(struct mtd_info *mtd);
4998273a0c9SArtem Bityutskiy int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
5008273a0c9SArtem Bityutskiy int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
5018273a0c9SArtem Bityutskiy 	      void **virt, resource_size_t *phys);
5028273a0c9SArtem Bityutskiy int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
5038273a0c9SArtem Bityutskiy unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
5048273a0c9SArtem Bityutskiy 				    unsigned long offset, unsigned long flags);
5058273a0c9SArtem Bityutskiy int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
5068273a0c9SArtem Bityutskiy 	     u_char *buf);
5078273a0c9SArtem Bityutskiy int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
5088273a0c9SArtem Bityutskiy 	      const u_char *buf);
5098273a0c9SArtem Bityutskiy int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
5108273a0c9SArtem Bityutskiy 		    const u_char *buf);
5117ae79d7fSArtem Bityutskiy 
512d2d48480SBrian Norris int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
5130c034fe3SEzequiel Garcia int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
514a2cc5ba0SArtem Bityutskiy 
5154b78fc42SChristian Riesch int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
5164b78fc42SChristian Riesch 			   struct otp_info *buf);
517de3cac93SArtem Bityutskiy int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
518de3cac93SArtem Bityutskiy 			   size_t *retlen, u_char *buf);
5194b78fc42SChristian Riesch int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
5204b78fc42SChristian Riesch 			   struct otp_info *buf);
521de3cac93SArtem Bityutskiy int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
522de3cac93SArtem Bityutskiy 			   size_t *retlen, u_char *buf);
523de3cac93SArtem Bityutskiy int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
5241df1fc8cSTudor Ambarus 			    size_t *retlen, const u_char *buf);
525de3cac93SArtem Bityutskiy int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
526e3c1f1c9SMichael Walle int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
5274403dbfbSArtem Bityutskiy 
5281dbebd32SArtem Bityutskiy int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
5291dbebd32SArtem Bityutskiy 	       unsigned long count, loff_t to, size_t *retlen);
530b0a31f7bSArtem Bityutskiy 
53185f2f2a8SArtem Bityutskiy static inline void mtd_sync(struct mtd_info *mtd)
53285f2f2a8SArtem Bityutskiy {
53346b5889cSMiquel Raynal 	struct mtd_info *master = mtd_get_master(mtd);
53446b5889cSMiquel Raynal 
53546b5889cSMiquel Raynal 	if (master->_sync)
53646b5889cSMiquel Raynal 		master->_sync(master);
53785f2f2a8SArtem Bityutskiy }
53885f2f2a8SArtem Bityutskiy 
5398273a0c9SArtem Bityutskiy int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
5408273a0c9SArtem Bityutskiy int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
5418273a0c9SArtem Bityutskiy int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
5428471bb73SEzequiel Garcia int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
5438273a0c9SArtem Bityutskiy int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
5448273a0c9SArtem Bityutskiy int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
545e95e9786SArtem Bityutskiy 
5463fe4bae8SArtem Bityutskiy static inline int mtd_suspend(struct mtd_info *mtd)
5473fe4bae8SArtem Bityutskiy {
54846b5889cSMiquel Raynal 	struct mtd_info *master = mtd_get_master(mtd);
54946b5889cSMiquel Raynal 	int ret;
55046b5889cSMiquel Raynal 
55146b5889cSMiquel Raynal 	if (master->master.suspended)
55246b5889cSMiquel Raynal 		return 0;
55346b5889cSMiquel Raynal 
55446b5889cSMiquel Raynal 	ret = master->_suspend ? master->_suspend(master) : 0;
55546b5889cSMiquel Raynal 	if (ret)
55646b5889cSMiquel Raynal 		return ret;
55746b5889cSMiquel Raynal 
55846b5889cSMiquel Raynal 	master->master.suspended = 1;
55946b5889cSMiquel Raynal 	return 0;
5603fe4bae8SArtem Bityutskiy }
5613fe4bae8SArtem Bityutskiy 
562ead995f8SArtem Bityutskiy static inline void mtd_resume(struct mtd_info *mtd)
563ead995f8SArtem Bityutskiy {
56446b5889cSMiquel Raynal 	struct mtd_info *master = mtd_get_master(mtd);
56546b5889cSMiquel Raynal 
56646b5889cSMiquel Raynal 	if (!master->master.suspended)
56746b5889cSMiquel Raynal 		return;
56846b5889cSMiquel Raynal 
56946b5889cSMiquel Raynal 	if (master->_resume)
57046b5889cSMiquel Raynal 		master->_resume(master);
57146b5889cSMiquel Raynal 
57246b5889cSMiquel Raynal 	master->master.suspended = 0;
573ead995f8SArtem Bityutskiy }
574ead995f8SArtem Bityutskiy 
57526cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
57669423d99SAdrian Hunter {
57769423d99SAdrian Hunter 	if (mtd->erasesize_shift)
57869423d99SAdrian Hunter 		return sz >> mtd->erasesize_shift;
57969423d99SAdrian Hunter 	do_div(sz, mtd->erasesize);
58069423d99SAdrian Hunter 	return sz;
58169423d99SAdrian Hunter }
58269423d99SAdrian Hunter 
58326cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
58469423d99SAdrian Hunter {
58569423d99SAdrian Hunter 	if (mtd->erasesize_shift)
58669423d99SAdrian Hunter 		return sz & mtd->erasesize_mask;
58769423d99SAdrian Hunter 	return do_div(sz, mtd->erasesize);
58869423d99SAdrian Hunter }
58969423d99SAdrian Hunter 
590f72071b8SBoris Brezillon /**
591f72071b8SBoris Brezillon  * mtd_align_erase_req - Adjust an erase request to align things on eraseblock
592f72071b8SBoris Brezillon  *			 boundaries.
593f72071b8SBoris Brezillon  * @mtd: the MTD device this erase request applies on
594f72071b8SBoris Brezillon  * @req: the erase request to adjust
595f72071b8SBoris Brezillon  *
596f72071b8SBoris Brezillon  * This function will adjust @req->addr and @req->len to align them on
597f72071b8SBoris Brezillon  * @mtd->erasesize. Of course we expect @mtd->erasesize to be != 0.
598f72071b8SBoris Brezillon  */
599f72071b8SBoris Brezillon static inline void mtd_align_erase_req(struct mtd_info *mtd,
600f72071b8SBoris Brezillon 				       struct erase_info *req)
601f72071b8SBoris Brezillon {
602f72071b8SBoris Brezillon 	u32 mod;
603f72071b8SBoris Brezillon 
604f72071b8SBoris Brezillon 	if (WARN_ON(!mtd->erasesize))
605f72071b8SBoris Brezillon 		return;
606f72071b8SBoris Brezillon 
607f72071b8SBoris Brezillon 	mod = mtd_mod_by_eb(req->addr, mtd);
608f72071b8SBoris Brezillon 	if (mod) {
609f72071b8SBoris Brezillon 		req->addr -= mod;
610f72071b8SBoris Brezillon 		req->len += mod;
611f72071b8SBoris Brezillon 	}
612f72071b8SBoris Brezillon 
613f72071b8SBoris Brezillon 	mod = mtd_mod_by_eb(req->addr + req->len, mtd);
614f72071b8SBoris Brezillon 	if (mod)
615f72071b8SBoris Brezillon 		req->len += mtd->erasesize - mod;
616f72071b8SBoris Brezillon }
617f72071b8SBoris Brezillon 
61826cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
61969423d99SAdrian Hunter {
62069423d99SAdrian Hunter 	if (mtd->writesize_shift)
62169423d99SAdrian Hunter 		return sz >> mtd->writesize_shift;
62269423d99SAdrian Hunter 	do_div(sz, mtd->writesize);
62369423d99SAdrian Hunter 	return sz;
62469423d99SAdrian Hunter }
62569423d99SAdrian Hunter 
62626cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
62769423d99SAdrian Hunter {
62869423d99SAdrian Hunter 	if (mtd->writesize_shift)
62969423d99SAdrian Hunter 		return sz & mtd->writesize_mask;
63069423d99SAdrian Hunter 	return do_div(sz, mtd->writesize);
63169423d99SAdrian Hunter }
6321da177e4SLinus Torvalds 
633477b0229SBoris Brezillon static inline int mtd_wunit_per_eb(struct mtd_info *mtd)
634477b0229SBoris Brezillon {
6359e3307a1SBoris Brezillon 	struct mtd_info *master = mtd_get_master(mtd);
6369e3307a1SBoris Brezillon 
6379e3307a1SBoris Brezillon 	return master->erasesize / mtd->writesize;
638477b0229SBoris Brezillon }
639477b0229SBoris Brezillon 
640477b0229SBoris Brezillon static inline int mtd_offset_to_wunit(struct mtd_info *mtd, loff_t offs)
641477b0229SBoris Brezillon {
642477b0229SBoris Brezillon 	return mtd_div_by_ws(mtd_mod_by_eb(offs, mtd), mtd);
643477b0229SBoris Brezillon }
644477b0229SBoris Brezillon 
645477b0229SBoris Brezillon static inline loff_t mtd_wunit_to_offset(struct mtd_info *mtd, loff_t base,
646477b0229SBoris Brezillon 					 int wunit)
647477b0229SBoris Brezillon {
648477b0229SBoris Brezillon 	return base + (wunit * mtd->writesize);
649477b0229SBoris Brezillon }
650477b0229SBoris Brezillon 
651477b0229SBoris Brezillon 
652fc002e3cSArtem Bityutskiy static inline int mtd_has_oob(const struct mtd_info *mtd)
653fc002e3cSArtem Bityutskiy {
65446b5889cSMiquel Raynal 	struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
65546b5889cSMiquel Raynal 
65646b5889cSMiquel Raynal 	return master->_read_oob && master->_write_oob;
657fc002e3cSArtem Bityutskiy }
658fc002e3cSArtem Bityutskiy 
659818b9739SHuang Shijie static inline int mtd_type_is_nand(const struct mtd_info *mtd)
660818b9739SHuang Shijie {
661818b9739SHuang Shijie 	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
662818b9739SHuang Shijie }
663818b9739SHuang Shijie 
6648f461a73SArtem Bityutskiy static inline int mtd_can_have_bb(const struct mtd_info *mtd)
6658f461a73SArtem Bityutskiy {
66646b5889cSMiquel Raynal 	struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
66746b5889cSMiquel Raynal 
66846b5889cSMiquel Raynal 	return !!master->_block_isbad;
6698f461a73SArtem Bityutskiy }
6708f461a73SArtem Bityutskiy 
6711da177e4SLinus Torvalds 	/* Kernel-side ioctl definitions */
6721da177e4SLinus Torvalds 
673f5671ab3SJamie Iles struct mtd_partition;
674c7975330SDmitry Eremin-Solenikov struct mtd_part_parser_data;
675f5671ab3SJamie Iles 
6761c4c215cSDmitry Eremin-Solenikov extern int mtd_device_parse_register(struct mtd_info *mtd,
67726a47346SArtem Bityutskiy 				     const char * const *part_probe_types,
678c7975330SDmitry Eremin-Solenikov 				     struct mtd_part_parser_data *parser_data,
6791c4c215cSDmitry Eremin-Solenikov 				     const struct mtd_partition *defparts,
6801c4c215cSDmitry Eremin-Solenikov 				     int defnr_parts);
68115c60a50SDmitry Eremin-Solenikov #define mtd_device_register(master, parts, nr_parts)	\
68215c60a50SDmitry Eremin-Solenikov 	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
683f5671ab3SJamie Iles extern int mtd_device_unregister(struct mtd_info *master);
6841da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
6853bd45657SMaxim Levitsky extern int __get_mtd_device(struct mtd_info *mtd);
6863bd45657SMaxim Levitsky extern void __put_mtd_device(struct mtd_info *mtd);
6877799308fSArtem Bityutskiy extern struct mtd_info *get_mtd_device_nm(const char *name);
6881da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds struct mtd_notifier {
6921da177e4SLinus Torvalds 	void (*add)(struct mtd_info *mtd);
6931da177e4SLinus Torvalds 	void (*remove)(struct mtd_info *mtd);
6941da177e4SLinus Torvalds 	struct list_head list;
6951da177e4SLinus Torvalds };
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new);
6991da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old);
70033b53716SGrant Erickson void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
70133b53716SGrant Erickson 
7027387ce77SBrian Norris static inline int mtd_is_bitflip(int err) {
7037387ce77SBrian Norris 	return err == -EUCLEAN;
7047387ce77SBrian Norris }
7051da177e4SLinus Torvalds 
7067387ce77SBrian Norris static inline int mtd_is_eccerr(int err) {
7077387ce77SBrian Norris 	return err == -EBADMSG;
7087387ce77SBrian Norris }
7091da177e4SLinus Torvalds 
7107387ce77SBrian Norris static inline int mtd_is_bitflip_or_eccerr(int err) {
7117387ce77SBrian Norris 	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
7127387ce77SBrian Norris }
7131da177e4SLinus Torvalds 
714b4caecd4SChristoph Hellwig unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
715b4caecd4SChristoph Hellwig 
716ad5e35f5SMiquel Raynal #ifdef CONFIG_DEBUG_FS
717ad5e35f5SMiquel Raynal bool mtd_check_expert_analysis_mode(void);
718ad5e35f5SMiquel Raynal #else
719ad5e35f5SMiquel Raynal static inline bool mtd_check_expert_analysis_mode(void) { return false; }
720ad5e35f5SMiquel Raynal #endif
721ad5e35f5SMiquel Raynal 
72267b967ddSMiquel Raynal 
7231da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */
724