xref: /openbmc/linux/include/linux/mtd/mtd.h (revision f72071b8)
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>
2728309572SCédric Le Goater #include <linux/of.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <mtd/mtd-abi.h>
301da177e4SLinus Torvalds 
3169423d99SAdrian Hunter #include <asm/div64.h>
3269423d99SAdrian Hunter 
331da177e4SLinus Torvalds #define MTD_ERASE_PENDING	0x01
341da177e4SLinus Torvalds #define MTD_ERASING		0x02
351da177e4SLinus Torvalds #define MTD_ERASE_SUSPEND	0x04
361da177e4SLinus Torvalds #define MTD_ERASE_DONE		0x08
371da177e4SLinus Torvalds #define MTD_ERASE_FAILED	0x10
381da177e4SLinus Torvalds 
3969423d99SAdrian Hunter #define MTD_FAIL_ADDR_UNKNOWN -1LL
40bb0eb217SAdrian Hunter 
41e2e24e8eSBrian Norris /*
42e2e24e8eSBrian Norris  * If the erase fails, fail_addr might indicate exactly which block failed. If
43e2e24e8eSBrian Norris  * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
44e2e24e8eSBrian Norris  * or was not specific to any particular block.
45e2e24e8eSBrian Norris  */
461da177e4SLinus Torvalds struct erase_info {
471da177e4SLinus Torvalds 	struct mtd_info *mtd;
4869423d99SAdrian Hunter 	uint64_t addr;
4969423d99SAdrian Hunter 	uint64_t len;
5069423d99SAdrian Hunter 	uint64_t fail_addr;
511da177e4SLinus Torvalds 	u_long time;
521da177e4SLinus Torvalds 	u_long retries;
5326cdb67cSDavid Woodhouse 	unsigned dev;
5426cdb67cSDavid Woodhouse 	unsigned cell;
551da177e4SLinus Torvalds 	void (*callback) (struct erase_info *self);
561da177e4SLinus Torvalds 	u_long priv;
571da177e4SLinus Torvalds 	u_char state;
581da177e4SLinus Torvalds 	struct erase_info *next;
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds struct mtd_erase_region_info {
6269423d99SAdrian Hunter 	uint64_t offset;		/* At which this region starts, from the beginning of the MTD */
6326cdb67cSDavid Woodhouse 	uint32_t erasesize;		/* For this region */
6426cdb67cSDavid Woodhouse 	uint32_t numblocks;		/* Number of blocks of erasesize in this region */
650ecbc81aSRodolfo Giometti 	unsigned long *lockmap;		/* If keeping bitmap of locks */
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
688593fbc6SThomas Gleixner /**
698593fbc6SThomas Gleixner  * struct mtd_oob_ops - oob operation operands
708593fbc6SThomas Gleixner  * @mode:	operation mode
718593fbc6SThomas Gleixner  *
727014568bSVitaly Wool  * @len:	number of data bytes to write/read
738593fbc6SThomas Gleixner  *
747014568bSVitaly Wool  * @retlen:	number of data bytes written/read
758593fbc6SThomas Gleixner  *
767014568bSVitaly Wool  * @ooblen:	number of oob bytes to write/read
777014568bSVitaly Wool  * @oobretlen:	number of oob bytes written/read
788593fbc6SThomas Gleixner  * @ooboffs:	offset of oob data in the oob area (only relevant when
794180f24aSBrian Norris  *		mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
808593fbc6SThomas Gleixner  * @datbuf:	data buffer - if NULL only oob data are read/written
818593fbc6SThomas Gleixner  * @oobbuf:	oob data buffer
8273a4421cSArtem Bityutskiy  *
83025dfdafSFrederik Schwarzer  * Note, it is allowed to read more than one OOB area at one go, but not write.
8473a4421cSArtem Bityutskiy  * The interface assumes that the OOB write requests program only one page's
8573a4421cSArtem Bityutskiy  * OOB area.
868593fbc6SThomas Gleixner  */
878593fbc6SThomas Gleixner struct mtd_oob_ops {
88905c6bcdSBrian Norris 	unsigned int	mode;
898593fbc6SThomas Gleixner 	size_t		len;
908593fbc6SThomas Gleixner 	size_t		retlen;
918593fbc6SThomas Gleixner 	size_t		ooblen;
927014568bSVitaly Wool 	size_t		oobretlen;
938593fbc6SThomas Gleixner 	uint32_t	ooboffs;
948593fbc6SThomas Gleixner 	uint8_t		*datbuf;
958593fbc6SThomas Gleixner 	uint8_t		*oobbuf;
968593fbc6SThomas Gleixner };
978593fbc6SThomas Gleixner 
98cc26c3cdSBrian Norris #define MTD_MAX_OOBFREE_ENTRIES_LARGE	32
992f25ae97SVipin Kumar #define MTD_MAX_ECCPOS_ENTRIES_LARGE	640
10075eb2cecSBoris Brezillon /**
10175eb2cecSBoris Brezillon  * struct mtd_oob_region - oob region definition
10275eb2cecSBoris Brezillon  * @offset: region offset
10375eb2cecSBoris Brezillon  * @length: region length
10475eb2cecSBoris Brezillon  *
10575eb2cecSBoris Brezillon  * This structure describes a region of the OOB area, and is used
10675eb2cecSBoris Brezillon  * to retrieve ECC or free bytes sections.
10775eb2cecSBoris Brezillon  * Each section is defined by an offset within the OOB area and a
10875eb2cecSBoris Brezillon  * length.
10975eb2cecSBoris Brezillon  */
11075eb2cecSBoris Brezillon struct mtd_oob_region {
11175eb2cecSBoris Brezillon 	u32 offset;
11275eb2cecSBoris Brezillon 	u32 length;
11375eb2cecSBoris Brezillon };
11475eb2cecSBoris Brezillon 
115adbbc3bcSBoris Brezillon /*
116adbbc3bcSBoris Brezillon  * struct mtd_ooblayout_ops - NAND OOB layout operations
117adbbc3bcSBoris Brezillon  * @ecc: function returning an ECC region in the OOB area.
118adbbc3bcSBoris Brezillon  *	 Should return -ERANGE if %section exceeds the total number of
119adbbc3bcSBoris Brezillon  *	 ECC sections.
120adbbc3bcSBoris Brezillon  * @free: function returning a free region in the OOB area.
121adbbc3bcSBoris Brezillon  *	  Should return -ERANGE if %section exceeds the total number of
122adbbc3bcSBoris Brezillon  *	  free sections.
123adbbc3bcSBoris Brezillon  */
124adbbc3bcSBoris Brezillon struct mtd_ooblayout_ops {
125adbbc3bcSBoris Brezillon 	int (*ecc)(struct mtd_info *mtd, int section,
126adbbc3bcSBoris Brezillon 		   struct mtd_oob_region *oobecc);
127adbbc3bcSBoris Brezillon 	int (*free)(struct mtd_info *mtd, int section,
128adbbc3bcSBoris Brezillon 		    struct mtd_oob_region *oobfree);
129adbbc3bcSBoris Brezillon };
130adbbc3bcSBoris Brezillon 
131477b0229SBoris Brezillon /**
132477b0229SBoris Brezillon  * struct mtd_pairing_info - page pairing information
133477b0229SBoris Brezillon  *
134477b0229SBoris Brezillon  * @pair: pair id
135477b0229SBoris Brezillon  * @group: group id
136477b0229SBoris Brezillon  *
137477b0229SBoris Brezillon  * The term "pair" is used here, even though TLC NANDs might group pages by 3
138477b0229SBoris Brezillon  * (3 bits in a single cell). A pair should regroup all pages that are sharing
139477b0229SBoris Brezillon  * the same cell. Pairs are then indexed in ascending order.
140477b0229SBoris Brezillon  *
141477b0229SBoris Brezillon  * @group is defining the position of a page in a given pair. It can also be
142477b0229SBoris Brezillon  * seen as the bit position in the cell: page attached to bit 0 belongs to
143477b0229SBoris Brezillon  * group 0, page attached to bit 1 belongs to group 1, etc.
144477b0229SBoris Brezillon  *
145477b0229SBoris Brezillon  * Example:
146477b0229SBoris Brezillon  * The H27UCG8T2BTR-BC datasheet describes the following pairing scheme:
147477b0229SBoris Brezillon  *
148477b0229SBoris Brezillon  *		group-0		group-1
149477b0229SBoris Brezillon  *
150477b0229SBoris Brezillon  *  pair-0	page-0		page-4
151477b0229SBoris Brezillon  *  pair-1	page-1		page-5
152477b0229SBoris Brezillon  *  pair-2	page-2		page-8
153477b0229SBoris Brezillon  *  ...
154477b0229SBoris Brezillon  *  pair-127	page-251	page-255
155477b0229SBoris Brezillon  *
156477b0229SBoris Brezillon  *
157477b0229SBoris Brezillon  * Note that the "group" and "pair" terms were extracted from Samsung and
158477b0229SBoris Brezillon  * Hynix datasheets, and might be referenced under other names in other
159477b0229SBoris Brezillon  * datasheets (Micron is describing this concept as "shared pages").
160477b0229SBoris Brezillon  */
161477b0229SBoris Brezillon struct mtd_pairing_info {
162477b0229SBoris Brezillon 	int pair;
163477b0229SBoris Brezillon 	int group;
164477b0229SBoris Brezillon };
165477b0229SBoris Brezillon 
166477b0229SBoris Brezillon /**
167477b0229SBoris Brezillon  * struct mtd_pairing_scheme - page pairing scheme description
168477b0229SBoris Brezillon  *
169477b0229SBoris Brezillon  * @ngroups: number of groups. Should be related to the number of bits
170477b0229SBoris Brezillon  *	     per cell.
171477b0229SBoris Brezillon  * @get_info: converts a write-unit (page number within an erase block) into
172477b0229SBoris Brezillon  *	      mtd_pairing information (pair + group). This function should
173477b0229SBoris Brezillon  *	      fill the info parameter based on the wunit index or return
174477b0229SBoris Brezillon  *	      -EINVAL if the wunit parameter is invalid.
175477b0229SBoris Brezillon  * @get_wunit: converts pairing information into a write-unit (page) number.
176477b0229SBoris Brezillon  *	       This function should return the wunit index pointed by the
177477b0229SBoris Brezillon  *	       pairing information described in the info argument. It should
178477b0229SBoris Brezillon  *	       return -EINVAL, if there's no wunit corresponding to the
179477b0229SBoris Brezillon  *	       passed pairing information.
180477b0229SBoris Brezillon  *
181477b0229SBoris Brezillon  * See mtd_pairing_info documentation for a detailed explanation of the
182477b0229SBoris Brezillon  * pair and group concepts.
183477b0229SBoris Brezillon  *
184477b0229SBoris Brezillon  * The mtd_pairing_scheme structure provides a generic solution to represent
185477b0229SBoris Brezillon  * NAND page pairing scheme. Instead of exposing two big tables to do the
186477b0229SBoris Brezillon  * write-unit <-> (pair + group) conversions, we ask the MTD drivers to
187477b0229SBoris Brezillon  * implement the ->get_info() and ->get_wunit() functions.
188477b0229SBoris Brezillon  *
189477b0229SBoris Brezillon  * MTD users will then be able to query these information by using the
190477b0229SBoris Brezillon  * mtd_pairing_info_to_wunit() and mtd_wunit_to_pairing_info() helpers.
191477b0229SBoris Brezillon  *
192477b0229SBoris Brezillon  * @ngroups is here to help MTD users iterating over all the pages in a
193477b0229SBoris Brezillon  * given pair. This value can be retrieved by MTD users using the
194477b0229SBoris Brezillon  * mtd_pairing_groups() helper.
195477b0229SBoris Brezillon  *
196477b0229SBoris Brezillon  * Examples are given in the mtd_pairing_info_to_wunit() and
197477b0229SBoris Brezillon  * mtd_wunit_to_pairing_info() documentation.
198477b0229SBoris Brezillon  */
199477b0229SBoris Brezillon struct mtd_pairing_scheme {
200477b0229SBoris Brezillon 	int ngroups;
201477b0229SBoris Brezillon 	int (*get_info)(struct mtd_info *mtd, int wunit,
202477b0229SBoris Brezillon 			struct mtd_pairing_info *info);
203477b0229SBoris Brezillon 	int (*get_wunit)(struct mtd_info *mtd,
204477b0229SBoris Brezillon 			 const struct mtd_pairing_info *info);
205477b0229SBoris Brezillon };
206477b0229SBoris Brezillon 
207de477254SPaul Gortmaker struct module;	/* only needed for owner field in mtd_info */
208de477254SPaul Gortmaker 
209e8e3edb9SMario Rugiero /**
210e8e3edb9SMario Rugiero  * struct mtd_debug_info - debugging information for an MTD device.
211e8e3edb9SMario Rugiero  *
212e8e3edb9SMario Rugiero  * @dfs_dir: direntry object of the MTD device debugfs directory
213e8e3edb9SMario Rugiero  */
214e8e3edb9SMario Rugiero struct mtd_debug_info {
215e8e3edb9SMario Rugiero 	struct dentry *dfs_dir;
216e8e3edb9SMario Rugiero };
217e8e3edb9SMario Rugiero 
2181da177e4SLinus Torvalds struct mtd_info {
2191da177e4SLinus Torvalds 	u_char type;
22026cdb67cSDavid Woodhouse 	uint32_t flags;
22169423d99SAdrian Hunter 	uint64_t size;	 // Total size of the MTD
2221da177e4SLinus Torvalds 
223151e7659SDavid Woodhouse 	/* "Major" erase size for the device. Naïve users may take this
2241da177e4SLinus Torvalds 	 * to be the only erase size available, or may use the more detailed
2251da177e4SLinus Torvalds 	 * information below if they desire
2261da177e4SLinus Torvalds 	 */
22726cdb67cSDavid Woodhouse 	uint32_t erasesize;
228783ed81fSArtem B. Bityutskiy 	/* Minimal writable flash unit size. In case of NOR flash it is 1 (even
229783ed81fSArtem B. Bityutskiy 	 * though individual bits can be cleared), in case of NAND flash it is
230783ed81fSArtem B. Bityutskiy 	 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
231783ed81fSArtem B. Bityutskiy 	 * it is of ECC block size, etc. It is illegal to have writesize = 0.
232783ed81fSArtem B. Bityutskiy 	 * Any driver registering a struct mtd_info must ensure a writesize of
233783ed81fSArtem B. Bityutskiy 	 * 1 or larger.
23428318776SJoern Engel 	 */
23526cdb67cSDavid Woodhouse 	uint32_t writesize;
2361da177e4SLinus Torvalds 
2370e4ca7e5SAnatolij Gustschin 	/*
2380e4ca7e5SAnatolij Gustschin 	 * Size of the write buffer used by the MTD. MTD devices having a write
2390e4ca7e5SAnatolij Gustschin 	 * buffer can write multiple writesize chunks at a time. E.g. while
2400e4ca7e5SAnatolij Gustschin 	 * writing 4 * writesize bytes to a device with 2 * writesize bytes
2410e4ca7e5SAnatolij Gustschin 	 * buffer the MTD driver can (but doesn't have to) do 2 writesize
2420e4ca7e5SAnatolij Gustschin 	 * operations, but not 4. Currently, all NANDs have writebufsize
2430e4ca7e5SAnatolij Gustschin 	 * equivalent to writesize (NAND page size). Some NOR flashes do have
2440e4ca7e5SAnatolij Gustschin 	 * writebufsize greater than writesize.
2450e4ca7e5SAnatolij Gustschin 	 */
2460e4ca7e5SAnatolij Gustschin 	uint32_t writebufsize;
2470e4ca7e5SAnatolij Gustschin 
24826cdb67cSDavid Woodhouse 	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
24926cdb67cSDavid Woodhouse 	uint32_t oobavail;  // Available OOB bytes per block
2501da177e4SLinus Torvalds 
25169423d99SAdrian Hunter 	/*
25269423d99SAdrian Hunter 	 * If erasesize is a power of 2 then the shift is stored in
25369423d99SAdrian Hunter 	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
25469423d99SAdrian Hunter 	 */
25569423d99SAdrian Hunter 	unsigned int erasesize_shift;
25669423d99SAdrian Hunter 	unsigned int writesize_shift;
25769423d99SAdrian Hunter 	/* Masks based on erasesize_shift and writesize_shift */
25869423d99SAdrian Hunter 	unsigned int erasesize_mask;
25969423d99SAdrian Hunter 	unsigned int writesize_mask;
2601da177e4SLinus Torvalds 
261d062d4edSMike Dunn 	/*
262d062d4edSMike Dunn 	 * read ops return -EUCLEAN if max number of bitflips corrected on any
263d062d4edSMike Dunn 	 * one region comprising an ecc step equals or exceeds this value.
264d062d4edSMike Dunn 	 * Settable by driver, else defaults to ecc_strength.  User can override
265d062d4edSMike Dunn 	 * in sysfs.  N.B. The meaning of the -EUCLEAN return code has changed;
266d062d4edSMike Dunn 	 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
267d062d4edSMike Dunn 	 */
268d062d4edSMike Dunn 	unsigned int bitflip_threshold;
269d062d4edSMike Dunn 
270971e4aeeSPavel Machek 	/* Kernel-only stuff starts here. */
271eadcf0d7SGreg Kroah-Hartman 	const char *name;
2721da177e4SLinus Torvalds 	int index;
2731da177e4SLinus Torvalds 
274adbbc3bcSBoris Brezillon 	/* OOB layout description */
275adbbc3bcSBoris Brezillon 	const struct mtd_ooblayout_ops *ooblayout;
276adbbc3bcSBoris Brezillon 
277477b0229SBoris Brezillon 	/* NAND pairing scheme, only provided for MLC/TLC NANDs */
278477b0229SBoris Brezillon 	const struct mtd_pairing_scheme *pairing;
279477b0229SBoris Brezillon 
280c41a0582SHuang Shijie 	/* the ecc step size. */
281c41a0582SHuang Shijie 	unsigned int ecc_step_size;
282c41a0582SHuang Shijie 
28386c2072bSMike Dunn 	/* max number of correctible bit errors per ecc step */
2841d0b95b0SMike Dunn 	unsigned int ecc_strength;
2851d0b95b0SMike Dunn 
2861da177e4SLinus Torvalds 	/* Data for variable erase regions. If numeraseregions is zero,
2871da177e4SLinus Torvalds 	 * it means that the whole device has erasesize as given above.
2881da177e4SLinus Torvalds 	 */
2891da177e4SLinus Torvalds 	int numeraseregions;
2901da177e4SLinus Torvalds 	struct mtd_erase_region_info *eraseregions;
2911da177e4SLinus Torvalds 
292b38178eeSJörn Engel 	/*
2937e1f0dc0SArtem Bityutskiy 	 * Do not call via these pointers, use corresponding mtd_*()
2947e1f0dc0SArtem Bityutskiy 	 * wrappers instead.
295b38178eeSJörn Engel 	 */
2963c3c10bbSArtem Bityutskiy 	int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
2973c3c10bbSArtem Bityutskiy 	int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
298a98889f3SJared Hulbert 		       size_t *retlen, void **virt, resource_size_t *phys);
2995e4e6e3fSArtem Bityutskiy 	int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
3003c3c10bbSArtem Bityutskiy 	int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
301329ad399SArtem Bityutskiy 		      size_t *retlen, u_char *buf);
3023c3c10bbSArtem Bityutskiy 	int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
303eda95cbfSArtem Bityutskiy 		       size_t *retlen, const u_char *buf);
3043c3c10bbSArtem Bityutskiy 	int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
3057ae79d7fSArtem Bityutskiy 			     size_t *retlen, const u_char *buf);
3063c3c10bbSArtem Bityutskiy 	int (*_read_oob) (struct mtd_info *mtd, loff_t from,
307fd2819bbSArtem Bityutskiy 			  struct mtd_oob_ops *ops);
3083c3c10bbSArtem Bityutskiy 	int (*_write_oob) (struct mtd_info *mtd, loff_t to,
309a2cc5ba0SArtem Bityutskiy 			   struct mtd_oob_ops *ops);
3104b78fc42SChristian Riesch 	int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
3114b78fc42SChristian Riesch 				    size_t *retlen, struct otp_info *buf);
3123c3c10bbSArtem Bityutskiy 	int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
313d264f72aSArtem Bityutskiy 				    size_t len, size_t *retlen, u_char *buf);
3144b78fc42SChristian Riesch 	int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
3154b78fc42SChristian Riesch 				    size_t *retlen, struct otp_info *buf);
3163c3c10bbSArtem Bityutskiy 	int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
3174ea1cabbSArtem Bityutskiy 				    size_t len, size_t *retlen, u_char *buf);
3183c3c10bbSArtem Bityutskiy 	int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
3193c3c10bbSArtem Bityutskiy 				     size_t len, size_t *retlen, u_char *buf);
3203c3c10bbSArtem Bityutskiy 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
3214403dbfbSArtem Bityutskiy 				    size_t len);
3223c3c10bbSArtem Bityutskiy 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
323b0a31f7bSArtem Bityutskiy 			unsigned long count, loff_t to, size_t *retlen);
3243c3c10bbSArtem Bityutskiy 	void (*_sync) (struct mtd_info *mtd);
3253c3c10bbSArtem Bityutskiy 	int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3263c3c10bbSArtem Bityutskiy 	int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3273c3c10bbSArtem Bityutskiy 	int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3288471bb73SEzequiel Garcia 	int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
3293c3c10bbSArtem Bityutskiy 	int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
3303c3c10bbSArtem Bityutskiy 	int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
3316080ef6eSJeff Westfahl 	int (*_max_bad_blocks) (struct mtd_info *mtd, loff_t ofs, size_t len);
3323c3c10bbSArtem Bityutskiy 	int (*_suspend) (struct mtd_info *mtd);
3333c3c10bbSArtem Bityutskiy 	void (*_resume) (struct mtd_info *mtd);
3343efe41beSBrian Norris 	void (*_reboot) (struct mtd_info *mtd);
335a88d2dc6SArtem Bityutskiy 	/*
336a88d2dc6SArtem Bityutskiy 	 * If the driver is something smart, like UBI, it may need to maintain
337a88d2dc6SArtem Bityutskiy 	 * its own reference counting. The below functions are only for driver.
338a88d2dc6SArtem Bityutskiy 	 */
3393c3c10bbSArtem Bityutskiy 	int (*_get_device) (struct mtd_info *mtd);
3403c3c10bbSArtem Bityutskiy 	void (*_put_device) (struct mtd_info *mtd);
341402d3265SDavid Howells 
342963a6fb0SNicolas Pitre 	struct notifier_block reboot_notifier;  /* default mode before reboot */
343963a6fb0SNicolas Pitre 
3447fac4648SThomas Gleixner 	/* ECC status information */
3457fac4648SThomas Gleixner 	struct mtd_ecc_stats ecc_stats;
34629072b96SThomas Gleixner 	/* Subpage shift (NAND) */
34729072b96SThomas Gleixner 	int subpage_sft;
3487fac4648SThomas Gleixner 
3491da177e4SLinus Torvalds 	void *priv;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	struct module *owner;
3521f24b5a8SDavid Brownell 	struct device dev;
3531da177e4SLinus Torvalds 	int usecount;
354e8e3edb9SMario Rugiero 	struct mtd_debug_info dbg;
3551da177e4SLinus Torvalds };
3561da177e4SLinus Torvalds 
35775eb2cecSBoris Brezillon int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
35875eb2cecSBoris Brezillon 		      struct mtd_oob_region *oobecc);
35975eb2cecSBoris Brezillon int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
36075eb2cecSBoris Brezillon 				 int *section,
36175eb2cecSBoris Brezillon 				 struct mtd_oob_region *oobregion);
36275eb2cecSBoris Brezillon int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
36375eb2cecSBoris Brezillon 			       const u8 *oobbuf, int start, int nbytes);
36475eb2cecSBoris Brezillon int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
36575eb2cecSBoris Brezillon 			       u8 *oobbuf, int start, int nbytes);
36675eb2cecSBoris Brezillon int mtd_ooblayout_free(struct mtd_info *mtd, int section,
36775eb2cecSBoris Brezillon 		       struct mtd_oob_region *oobfree);
36875eb2cecSBoris Brezillon int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
36975eb2cecSBoris Brezillon 				const u8 *oobbuf, int start, int nbytes);
37075eb2cecSBoris Brezillon int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
37175eb2cecSBoris Brezillon 				u8 *oobbuf, int start, int nbytes);
37275eb2cecSBoris Brezillon int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
37375eb2cecSBoris Brezillon int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
37475eb2cecSBoris Brezillon 
375adbbc3bcSBoris Brezillon static inline void mtd_set_ooblayout(struct mtd_info *mtd,
376adbbc3bcSBoris Brezillon 				     const struct mtd_ooblayout_ops *ooblayout)
377036d6543SBoris Brezillon {
378adbbc3bcSBoris Brezillon 	mtd->ooblayout = ooblayout;
379036d6543SBoris Brezillon }
380036d6543SBoris Brezillon 
381477b0229SBoris Brezillon static inline void mtd_set_pairing_scheme(struct mtd_info *mtd,
382477b0229SBoris Brezillon 				const struct mtd_pairing_scheme *pairing)
383477b0229SBoris Brezillon {
384477b0229SBoris Brezillon 	mtd->pairing = pairing;
385477b0229SBoris Brezillon }
386477b0229SBoris Brezillon 
38728b8b26bSBrian Norris static inline void mtd_set_of_node(struct mtd_info *mtd,
38828b8b26bSBrian Norris 				   struct device_node *np)
38928b8b26bSBrian Norris {
39028b8b26bSBrian Norris 	mtd->dev.of_node = np;
39128309572SCédric Le Goater 	if (!mtd->name)
39228309572SCédric Le Goater 		of_property_read_string(np, "label", &mtd->name);
39328b8b26bSBrian Norris }
39428b8b26bSBrian Norris 
39528b8b26bSBrian Norris static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
39628b8b26bSBrian Norris {
3974a67c9fdSRafał Miłecki 	return dev_of_node(&mtd->dev);
39828b8b26bSBrian Norris }
39928b8b26bSBrian Norris 
40029f1058aSBoris BREZILLON static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
40129f1058aSBoris BREZILLON {
40229f1058aSBoris BREZILLON 	return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
40329f1058aSBoris BREZILLON }
40429f1058aSBoris BREZILLON 
4056080ef6eSJeff Westfahl static inline int mtd_max_bad_blocks(struct mtd_info *mtd,
4066080ef6eSJeff Westfahl 				     loff_t ofs, size_t len)
4076080ef6eSJeff Westfahl {
4086080ef6eSJeff Westfahl 	if (!mtd->_max_bad_blocks)
4096080ef6eSJeff Westfahl 		return -ENOTSUPP;
4106080ef6eSJeff Westfahl 
4116080ef6eSJeff Westfahl 	if (mtd->size < (len + ofs) || ofs < 0)
4126080ef6eSJeff Westfahl 		return -EINVAL;
4136080ef6eSJeff Westfahl 
4146080ef6eSJeff Westfahl 	return mtd->_max_bad_blocks(mtd, ofs, len);
4156080ef6eSJeff Westfahl }
4166080ef6eSJeff Westfahl 
417477b0229SBoris Brezillon int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
418477b0229SBoris Brezillon 			      struct mtd_pairing_info *info);
419477b0229SBoris Brezillon int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
420477b0229SBoris Brezillon 			      const struct mtd_pairing_info *info);
421477b0229SBoris Brezillon int mtd_pairing_groups(struct mtd_info *mtd);
4228273a0c9SArtem Bityutskiy int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
4238273a0c9SArtem Bityutskiy int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
4248273a0c9SArtem Bityutskiy 	      void **virt, resource_size_t *phys);
4258273a0c9SArtem Bityutskiy int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
4268273a0c9SArtem Bityutskiy unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
4278273a0c9SArtem Bityutskiy 				    unsigned long offset, unsigned long flags);
4288273a0c9SArtem Bityutskiy int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
4298273a0c9SArtem Bityutskiy 	     u_char *buf);
4308273a0c9SArtem Bityutskiy int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
4318273a0c9SArtem Bityutskiy 	      const u_char *buf);
4328273a0c9SArtem Bityutskiy int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
4338273a0c9SArtem Bityutskiy 		    const u_char *buf);
4347ae79d7fSArtem Bityutskiy 
435d2d48480SBrian Norris int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
4360c034fe3SEzequiel Garcia int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
437a2cc5ba0SArtem Bityutskiy 
4384b78fc42SChristian Riesch int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
4394b78fc42SChristian Riesch 			   struct otp_info *buf);
440de3cac93SArtem Bityutskiy int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
441de3cac93SArtem Bityutskiy 			   size_t *retlen, u_char *buf);
4424b78fc42SChristian Riesch int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
4434b78fc42SChristian Riesch 			   struct otp_info *buf);
444de3cac93SArtem Bityutskiy int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
445de3cac93SArtem Bityutskiy 			   size_t *retlen, u_char *buf);
446de3cac93SArtem Bityutskiy int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
447de3cac93SArtem Bityutskiy 			    size_t *retlen, u_char *buf);
448de3cac93SArtem Bityutskiy int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
4494403dbfbSArtem Bityutskiy 
4501dbebd32SArtem Bityutskiy int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
4511dbebd32SArtem Bityutskiy 	       unsigned long count, loff_t to, size_t *retlen);
452b0a31f7bSArtem Bityutskiy 
45385f2f2a8SArtem Bityutskiy static inline void mtd_sync(struct mtd_info *mtd)
45485f2f2a8SArtem Bityutskiy {
4553c3c10bbSArtem Bityutskiy 	if (mtd->_sync)
4563c3c10bbSArtem Bityutskiy 		mtd->_sync(mtd);
45785f2f2a8SArtem Bityutskiy }
45885f2f2a8SArtem Bityutskiy 
4598273a0c9SArtem Bityutskiy int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
4608273a0c9SArtem Bityutskiy int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
4618273a0c9SArtem Bityutskiy int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
4628471bb73SEzequiel Garcia int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
4638273a0c9SArtem Bityutskiy int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
4648273a0c9SArtem Bityutskiy int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
465e95e9786SArtem Bityutskiy 
4663fe4bae8SArtem Bityutskiy static inline int mtd_suspend(struct mtd_info *mtd)
4673fe4bae8SArtem Bityutskiy {
4683c3c10bbSArtem Bityutskiy 	return mtd->_suspend ? mtd->_suspend(mtd) : 0;
4693fe4bae8SArtem Bityutskiy }
4703fe4bae8SArtem Bityutskiy 
471ead995f8SArtem Bityutskiy static inline void mtd_resume(struct mtd_info *mtd)
472ead995f8SArtem Bityutskiy {
4733c3c10bbSArtem Bityutskiy 	if (mtd->_resume)
4743c3c10bbSArtem Bityutskiy 		mtd->_resume(mtd);
475ead995f8SArtem Bityutskiy }
476ead995f8SArtem Bityutskiy 
47726cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
47869423d99SAdrian Hunter {
47969423d99SAdrian Hunter 	if (mtd->erasesize_shift)
48069423d99SAdrian Hunter 		return sz >> mtd->erasesize_shift;
48169423d99SAdrian Hunter 	do_div(sz, mtd->erasesize);
48269423d99SAdrian Hunter 	return sz;
48369423d99SAdrian Hunter }
48469423d99SAdrian Hunter 
48526cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
48669423d99SAdrian Hunter {
48769423d99SAdrian Hunter 	if (mtd->erasesize_shift)
48869423d99SAdrian Hunter 		return sz & mtd->erasesize_mask;
48969423d99SAdrian Hunter 	return do_div(sz, mtd->erasesize);
49069423d99SAdrian Hunter }
49169423d99SAdrian Hunter 
492f72071b8SBoris Brezillon /**
493f72071b8SBoris Brezillon  * mtd_align_erase_req - Adjust an erase request to align things on eraseblock
494f72071b8SBoris Brezillon  *			 boundaries.
495f72071b8SBoris Brezillon  * @mtd: the MTD device this erase request applies on
496f72071b8SBoris Brezillon  * @req: the erase request to adjust
497f72071b8SBoris Brezillon  *
498f72071b8SBoris Brezillon  * This function will adjust @req->addr and @req->len to align them on
499f72071b8SBoris Brezillon  * @mtd->erasesize. Of course we expect @mtd->erasesize to be != 0.
500f72071b8SBoris Brezillon  */
501f72071b8SBoris Brezillon static inline void mtd_align_erase_req(struct mtd_info *mtd,
502f72071b8SBoris Brezillon 				       struct erase_info *req)
503f72071b8SBoris Brezillon {
504f72071b8SBoris Brezillon 	u32 mod;
505f72071b8SBoris Brezillon 
506f72071b8SBoris Brezillon 	if (WARN_ON(!mtd->erasesize))
507f72071b8SBoris Brezillon 		return;
508f72071b8SBoris Brezillon 
509f72071b8SBoris Brezillon 	mod = mtd_mod_by_eb(req->addr, mtd);
510f72071b8SBoris Brezillon 	if (mod) {
511f72071b8SBoris Brezillon 		req->addr -= mod;
512f72071b8SBoris Brezillon 		req->len += mod;
513f72071b8SBoris Brezillon 	}
514f72071b8SBoris Brezillon 
515f72071b8SBoris Brezillon 	mod = mtd_mod_by_eb(req->addr + req->len, mtd);
516f72071b8SBoris Brezillon 	if (mod)
517f72071b8SBoris Brezillon 		req->len += mtd->erasesize - mod;
518f72071b8SBoris Brezillon }
519f72071b8SBoris Brezillon 
52026cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
52169423d99SAdrian Hunter {
52269423d99SAdrian Hunter 	if (mtd->writesize_shift)
52369423d99SAdrian Hunter 		return sz >> mtd->writesize_shift;
52469423d99SAdrian Hunter 	do_div(sz, mtd->writesize);
52569423d99SAdrian Hunter 	return sz;
52669423d99SAdrian Hunter }
52769423d99SAdrian Hunter 
52826cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
52969423d99SAdrian Hunter {
53069423d99SAdrian Hunter 	if (mtd->writesize_shift)
53169423d99SAdrian Hunter 		return sz & mtd->writesize_mask;
53269423d99SAdrian Hunter 	return do_div(sz, mtd->writesize);
53369423d99SAdrian Hunter }
5341da177e4SLinus Torvalds 
535477b0229SBoris Brezillon static inline int mtd_wunit_per_eb(struct mtd_info *mtd)
536477b0229SBoris Brezillon {
537477b0229SBoris Brezillon 	return mtd->erasesize / mtd->writesize;
538477b0229SBoris Brezillon }
539477b0229SBoris Brezillon 
540477b0229SBoris Brezillon static inline int mtd_offset_to_wunit(struct mtd_info *mtd, loff_t offs)
541477b0229SBoris Brezillon {
542477b0229SBoris Brezillon 	return mtd_div_by_ws(mtd_mod_by_eb(offs, mtd), mtd);
543477b0229SBoris Brezillon }
544477b0229SBoris Brezillon 
545477b0229SBoris Brezillon static inline loff_t mtd_wunit_to_offset(struct mtd_info *mtd, loff_t base,
546477b0229SBoris Brezillon 					 int wunit)
547477b0229SBoris Brezillon {
548477b0229SBoris Brezillon 	return base + (wunit * mtd->writesize);
549477b0229SBoris Brezillon }
550477b0229SBoris Brezillon 
551477b0229SBoris Brezillon 
552fc002e3cSArtem Bityutskiy static inline int mtd_has_oob(const struct mtd_info *mtd)
553fc002e3cSArtem Bityutskiy {
5543c3c10bbSArtem Bityutskiy 	return mtd->_read_oob && mtd->_write_oob;
555fc002e3cSArtem Bityutskiy }
556fc002e3cSArtem Bityutskiy 
557818b9739SHuang Shijie static inline int mtd_type_is_nand(const struct mtd_info *mtd)
558818b9739SHuang Shijie {
559818b9739SHuang Shijie 	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
560818b9739SHuang Shijie }
561818b9739SHuang Shijie 
5628f461a73SArtem Bityutskiy static inline int mtd_can_have_bb(const struct mtd_info *mtd)
5638f461a73SArtem Bityutskiy {
5643c3c10bbSArtem Bityutskiy 	return !!mtd->_block_isbad;
5658f461a73SArtem Bityutskiy }
5668f461a73SArtem Bityutskiy 
5671da177e4SLinus Torvalds 	/* Kernel-side ioctl definitions */
5681da177e4SLinus Torvalds 
569f5671ab3SJamie Iles struct mtd_partition;
570c7975330SDmitry Eremin-Solenikov struct mtd_part_parser_data;
571f5671ab3SJamie Iles 
5721c4c215cSDmitry Eremin-Solenikov extern int mtd_device_parse_register(struct mtd_info *mtd,
57326a47346SArtem Bityutskiy 				     const char * const *part_probe_types,
574c7975330SDmitry Eremin-Solenikov 				     struct mtd_part_parser_data *parser_data,
5751c4c215cSDmitry Eremin-Solenikov 				     const struct mtd_partition *defparts,
5761c4c215cSDmitry Eremin-Solenikov 				     int defnr_parts);
57715c60a50SDmitry Eremin-Solenikov #define mtd_device_register(master, parts, nr_parts)	\
57815c60a50SDmitry Eremin-Solenikov 	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
579f5671ab3SJamie Iles extern int mtd_device_unregister(struct mtd_info *master);
5801da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
5813bd45657SMaxim Levitsky extern int __get_mtd_device(struct mtd_info *mtd);
5823bd45657SMaxim Levitsky extern void __put_mtd_device(struct mtd_info *mtd);
5837799308fSArtem Bityutskiy extern struct mtd_info *get_mtd_device_nm(const char *name);
5841da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds struct mtd_notifier {
5881da177e4SLinus Torvalds 	void (*add)(struct mtd_info *mtd);
5891da177e4SLinus Torvalds 	void (*remove)(struct mtd_info *mtd);
5901da177e4SLinus Torvalds 	struct list_head list;
5911da177e4SLinus Torvalds };
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new);
5951da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old);
59633b53716SGrant Erickson void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
59733b53716SGrant Erickson 
5981da177e4SLinus Torvalds void mtd_erase_callback(struct erase_info *instr);
5991da177e4SLinus Torvalds 
6007387ce77SBrian Norris static inline int mtd_is_bitflip(int err) {
6017387ce77SBrian Norris 	return err == -EUCLEAN;
6027387ce77SBrian Norris }
6031da177e4SLinus Torvalds 
6047387ce77SBrian Norris static inline int mtd_is_eccerr(int err) {
6057387ce77SBrian Norris 	return err == -EBADMSG;
6067387ce77SBrian Norris }
6071da177e4SLinus Torvalds 
6087387ce77SBrian Norris static inline int mtd_is_bitflip_or_eccerr(int err) {
6097387ce77SBrian Norris 	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
6107387ce77SBrian Norris }
6111da177e4SLinus Torvalds 
612b4caecd4SChristoph Hellwig unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
613b4caecd4SChristoph Hellwig 
6141da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */
615