xref: /openbmc/linux/include/linux/mtd/mtd.h (revision 9f897bfd)
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>
11963a6fb0SNicolas Pitre #include <linux/notifier.h>
121f24b5a8SDavid Brownell #include <linux/device.h>
1328309572SCédric Le Goater #include <linux/of.h>
14c4dfa25aSAlban Bedel #include <linux/nvmem-provider.h>
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <mtd/mtd-abi.h>
171da177e4SLinus Torvalds 
1869423d99SAdrian Hunter #include <asm/div64.h>
1969423d99SAdrian Hunter 
2069423d99SAdrian Hunter #define MTD_FAIL_ADDR_UNKNOWN -1LL
21bb0eb217SAdrian Hunter 
228f347c42SBoris Brezillon struct mtd_info;
238f347c42SBoris Brezillon 
24e2e24e8eSBrian Norris /*
25e2e24e8eSBrian Norris  * If the erase fails, fail_addr might indicate exactly which block failed. If
26e2e24e8eSBrian Norris  * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
27e2e24e8eSBrian Norris  * or was not specific to any particular block.
28e2e24e8eSBrian Norris  */
291da177e4SLinus Torvalds struct erase_info {
3069423d99SAdrian Hunter 	uint64_t addr;
3169423d99SAdrian Hunter 	uint64_t len;
3269423d99SAdrian Hunter 	uint64_t fail_addr;
331da177e4SLinus Torvalds };
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds struct mtd_erase_region_info {
3669423d99SAdrian Hunter 	uint64_t offset;		/* At which this region starts, from the beginning of the MTD */
3726cdb67cSDavid Woodhouse 	uint32_t erasesize;		/* For this region */
3826cdb67cSDavid Woodhouse 	uint32_t numblocks;		/* Number of blocks of erasesize in this region */
390ecbc81aSRodolfo Giometti 	unsigned long *lockmap;		/* If keeping bitmap of locks */
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
428593fbc6SThomas Gleixner /**
438593fbc6SThomas Gleixner  * struct mtd_oob_ops - oob operation operands
448593fbc6SThomas Gleixner  * @mode:	operation mode
458593fbc6SThomas Gleixner  *
467014568bSVitaly Wool  * @len:	number of data bytes to write/read
478593fbc6SThomas Gleixner  *
487014568bSVitaly Wool  * @retlen:	number of data bytes written/read
498593fbc6SThomas Gleixner  *
507014568bSVitaly Wool  * @ooblen:	number of oob bytes to write/read
517014568bSVitaly Wool  * @oobretlen:	number of oob bytes written/read
528593fbc6SThomas Gleixner  * @ooboffs:	offset of oob data in the oob area (only relevant when
534180f24aSBrian Norris  *		mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
548593fbc6SThomas Gleixner  * @datbuf:	data buffer - if NULL only oob data are read/written
558593fbc6SThomas Gleixner  * @oobbuf:	oob data buffer
5673a4421cSArtem Bityutskiy  *
57d70420bcSMiquel Raynal  * Note, some MTD drivers do not allow you to write more than one OOB area at
58d70420bcSMiquel Raynal  * one go. If you try to do that on such an MTD device, -EINVAL will be
59d70420bcSMiquel Raynal  * returned. If you want to make your implementation portable on all kind of MTD
60d70420bcSMiquel Raynal  * devices you should split the write request into several sub-requests when the
61d70420bcSMiquel Raynal  * request crosses a page boundary.
628593fbc6SThomas Gleixner  */
638593fbc6SThomas Gleixner struct mtd_oob_ops {
64905c6bcdSBrian Norris 	unsigned int	mode;
658593fbc6SThomas Gleixner 	size_t		len;
668593fbc6SThomas Gleixner 	size_t		retlen;
678593fbc6SThomas Gleixner 	size_t		ooblen;
687014568bSVitaly Wool 	size_t		oobretlen;
698593fbc6SThomas Gleixner 	uint32_t	ooboffs;
708593fbc6SThomas Gleixner 	uint8_t		*datbuf;
718593fbc6SThomas Gleixner 	uint8_t		*oobbuf;
728593fbc6SThomas Gleixner };
738593fbc6SThomas Gleixner 
74cc26c3cdSBrian Norris #define MTD_MAX_OOBFREE_ENTRIES_LARGE	32
752f25ae97SVipin Kumar #define MTD_MAX_ECCPOS_ENTRIES_LARGE	640
7675eb2cecSBoris Brezillon /**
7775eb2cecSBoris Brezillon  * struct mtd_oob_region - oob region definition
7875eb2cecSBoris Brezillon  * @offset: region offset
7975eb2cecSBoris Brezillon  * @length: region length
8075eb2cecSBoris Brezillon  *
8175eb2cecSBoris Brezillon  * This structure describes a region of the OOB area, and is used
8275eb2cecSBoris Brezillon  * to retrieve ECC or free bytes sections.
8375eb2cecSBoris Brezillon  * Each section is defined by an offset within the OOB area and a
8475eb2cecSBoris Brezillon  * length.
8575eb2cecSBoris Brezillon  */
8675eb2cecSBoris Brezillon struct mtd_oob_region {
8775eb2cecSBoris Brezillon 	u32 offset;
8875eb2cecSBoris Brezillon 	u32 length;
8975eb2cecSBoris Brezillon };
9075eb2cecSBoris Brezillon 
91adbbc3bcSBoris Brezillon /*
92adbbc3bcSBoris Brezillon  * struct mtd_ooblayout_ops - NAND OOB layout operations
93adbbc3bcSBoris Brezillon  * @ecc: function returning an ECC region in the OOB area.
94adbbc3bcSBoris Brezillon  *	 Should return -ERANGE if %section exceeds the total number of
95adbbc3bcSBoris Brezillon  *	 ECC sections.
96adbbc3bcSBoris Brezillon  * @free: function returning a free region in the OOB area.
97adbbc3bcSBoris Brezillon  *	  Should return -ERANGE if %section exceeds the total number of
98adbbc3bcSBoris Brezillon  *	  free sections.
99adbbc3bcSBoris Brezillon  */
100adbbc3bcSBoris Brezillon struct mtd_ooblayout_ops {
101adbbc3bcSBoris Brezillon 	int (*ecc)(struct mtd_info *mtd, int section,
102adbbc3bcSBoris Brezillon 		   struct mtd_oob_region *oobecc);
103adbbc3bcSBoris Brezillon 	int (*free)(struct mtd_info *mtd, int section,
104adbbc3bcSBoris Brezillon 		    struct mtd_oob_region *oobfree);
105adbbc3bcSBoris Brezillon };
106adbbc3bcSBoris Brezillon 
107477b0229SBoris Brezillon /**
108477b0229SBoris Brezillon  * struct mtd_pairing_info - page pairing information
109477b0229SBoris Brezillon  *
110477b0229SBoris Brezillon  * @pair: pair id
111477b0229SBoris Brezillon  * @group: group id
112477b0229SBoris Brezillon  *
113477b0229SBoris Brezillon  * The term "pair" is used here, even though TLC NANDs might group pages by 3
114477b0229SBoris Brezillon  * (3 bits in a single cell). A pair should regroup all pages that are sharing
115477b0229SBoris Brezillon  * the same cell. Pairs are then indexed in ascending order.
116477b0229SBoris Brezillon  *
117477b0229SBoris Brezillon  * @group is defining the position of a page in a given pair. It can also be
118477b0229SBoris Brezillon  * seen as the bit position in the cell: page attached to bit 0 belongs to
119477b0229SBoris Brezillon  * group 0, page attached to bit 1 belongs to group 1, etc.
120477b0229SBoris Brezillon  *
121477b0229SBoris Brezillon  * Example:
122477b0229SBoris Brezillon  * The H27UCG8T2BTR-BC datasheet describes the following pairing scheme:
123477b0229SBoris Brezillon  *
124477b0229SBoris Brezillon  *		group-0		group-1
125477b0229SBoris Brezillon  *
126477b0229SBoris Brezillon  *  pair-0	page-0		page-4
127477b0229SBoris Brezillon  *  pair-1	page-1		page-5
128477b0229SBoris Brezillon  *  pair-2	page-2		page-8
129477b0229SBoris Brezillon  *  ...
130477b0229SBoris Brezillon  *  pair-127	page-251	page-255
131477b0229SBoris Brezillon  *
132477b0229SBoris Brezillon  *
133477b0229SBoris Brezillon  * Note that the "group" and "pair" terms were extracted from Samsung and
134477b0229SBoris Brezillon  * Hynix datasheets, and might be referenced under other names in other
135477b0229SBoris Brezillon  * datasheets (Micron is describing this concept as "shared pages").
136477b0229SBoris Brezillon  */
137477b0229SBoris Brezillon struct mtd_pairing_info {
138477b0229SBoris Brezillon 	int pair;
139477b0229SBoris Brezillon 	int group;
140477b0229SBoris Brezillon };
141477b0229SBoris Brezillon 
142477b0229SBoris Brezillon /**
143477b0229SBoris Brezillon  * struct mtd_pairing_scheme - page pairing scheme description
144477b0229SBoris Brezillon  *
145477b0229SBoris Brezillon  * @ngroups: number of groups. Should be related to the number of bits
146477b0229SBoris Brezillon  *	     per cell.
147477b0229SBoris Brezillon  * @get_info: converts a write-unit (page number within an erase block) into
148477b0229SBoris Brezillon  *	      mtd_pairing information (pair + group). This function should
149477b0229SBoris Brezillon  *	      fill the info parameter based on the wunit index or return
150477b0229SBoris Brezillon  *	      -EINVAL if the wunit parameter is invalid.
151477b0229SBoris Brezillon  * @get_wunit: converts pairing information into a write-unit (page) number.
152477b0229SBoris Brezillon  *	       This function should return the wunit index pointed by the
153477b0229SBoris Brezillon  *	       pairing information described in the info argument. It should
154477b0229SBoris Brezillon  *	       return -EINVAL, if there's no wunit corresponding to the
155477b0229SBoris Brezillon  *	       passed pairing information.
156477b0229SBoris Brezillon  *
157477b0229SBoris Brezillon  * See mtd_pairing_info documentation for a detailed explanation of the
158477b0229SBoris Brezillon  * pair and group concepts.
159477b0229SBoris Brezillon  *
160477b0229SBoris Brezillon  * The mtd_pairing_scheme structure provides a generic solution to represent
161477b0229SBoris Brezillon  * NAND page pairing scheme. Instead of exposing two big tables to do the
162477b0229SBoris Brezillon  * write-unit <-> (pair + group) conversions, we ask the MTD drivers to
163477b0229SBoris Brezillon  * implement the ->get_info() and ->get_wunit() functions.
164477b0229SBoris Brezillon  *
165477b0229SBoris Brezillon  * MTD users will then be able to query these information by using the
166477b0229SBoris Brezillon  * mtd_pairing_info_to_wunit() and mtd_wunit_to_pairing_info() helpers.
167477b0229SBoris Brezillon  *
168477b0229SBoris Brezillon  * @ngroups is here to help MTD users iterating over all the pages in a
169477b0229SBoris Brezillon  * given pair. This value can be retrieved by MTD users using the
170477b0229SBoris Brezillon  * mtd_pairing_groups() helper.
171477b0229SBoris Brezillon  *
172477b0229SBoris Brezillon  * Examples are given in the mtd_pairing_info_to_wunit() and
173477b0229SBoris Brezillon  * mtd_wunit_to_pairing_info() documentation.
174477b0229SBoris Brezillon  */
175477b0229SBoris Brezillon struct mtd_pairing_scheme {
176477b0229SBoris Brezillon 	int ngroups;
177477b0229SBoris Brezillon 	int (*get_info)(struct mtd_info *mtd, int wunit,
178477b0229SBoris Brezillon 			struct mtd_pairing_info *info);
179477b0229SBoris Brezillon 	int (*get_wunit)(struct mtd_info *mtd,
180477b0229SBoris Brezillon 			 const struct mtd_pairing_info *info);
181477b0229SBoris Brezillon };
182477b0229SBoris Brezillon 
183de477254SPaul Gortmaker struct module;	/* only needed for owner field in mtd_info */
184de477254SPaul Gortmaker 
185e8e3edb9SMario Rugiero /**
186e8e3edb9SMario Rugiero  * struct mtd_debug_info - debugging information for an MTD device.
187e8e3edb9SMario Rugiero  *
188e8e3edb9SMario Rugiero  * @dfs_dir: direntry object of the MTD device debugfs directory
189e8e3edb9SMario Rugiero  */
190e8e3edb9SMario Rugiero struct mtd_debug_info {
191e8e3edb9SMario Rugiero 	struct dentry *dfs_dir;
192e8e3edb9SMario Rugiero };
193e8e3edb9SMario Rugiero 
1941da177e4SLinus Torvalds struct mtd_info {
1951da177e4SLinus Torvalds 	u_char type;
19626cdb67cSDavid Woodhouse 	uint32_t flags;
1971186af45SRafał Miłecki 	uint32_t orig_flags; /* Flags as before running mtd checks */
19869423d99SAdrian Hunter 	uint64_t size;	 // Total size of the MTD
1991da177e4SLinus Torvalds 
200151e7659SDavid Woodhouse 	/* "Major" erase size for the device. Naïve users may take this
2011da177e4SLinus Torvalds 	 * to be the only erase size available, or may use the more detailed
2021da177e4SLinus Torvalds 	 * information below if they desire
2031da177e4SLinus Torvalds 	 */
20426cdb67cSDavid Woodhouse 	uint32_t erasesize;
205783ed81fSArtem B. Bityutskiy 	/* Minimal writable flash unit size. In case of NOR flash it is 1 (even
206783ed81fSArtem B. Bityutskiy 	 * though individual bits can be cleared), in case of NAND flash it is
207783ed81fSArtem B. Bityutskiy 	 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
208783ed81fSArtem B. Bityutskiy 	 * it is of ECC block size, etc. It is illegal to have writesize = 0.
209783ed81fSArtem B. Bityutskiy 	 * Any driver registering a struct mtd_info must ensure a writesize of
210783ed81fSArtem B. Bityutskiy 	 * 1 or larger.
21128318776SJoern Engel 	 */
21226cdb67cSDavid Woodhouse 	uint32_t writesize;
2131da177e4SLinus Torvalds 
2140e4ca7e5SAnatolij Gustschin 	/*
2150e4ca7e5SAnatolij Gustschin 	 * Size of the write buffer used by the MTD. MTD devices having a write
2160e4ca7e5SAnatolij Gustschin 	 * buffer can write multiple writesize chunks at a time. E.g. while
2170e4ca7e5SAnatolij Gustschin 	 * writing 4 * writesize bytes to a device with 2 * writesize bytes
2180e4ca7e5SAnatolij Gustschin 	 * buffer the MTD driver can (but doesn't have to) do 2 writesize
2190e4ca7e5SAnatolij Gustschin 	 * operations, but not 4. Currently, all NANDs have writebufsize
2200e4ca7e5SAnatolij Gustschin 	 * equivalent to writesize (NAND page size). Some NOR flashes do have
2210e4ca7e5SAnatolij Gustschin 	 * writebufsize greater than writesize.
2220e4ca7e5SAnatolij Gustschin 	 */
2230e4ca7e5SAnatolij Gustschin 	uint32_t writebufsize;
2240e4ca7e5SAnatolij Gustschin 
22526cdb67cSDavid Woodhouse 	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
22626cdb67cSDavid Woodhouse 	uint32_t oobavail;  // Available OOB bytes per block
2271da177e4SLinus Torvalds 
22869423d99SAdrian Hunter 	/*
22969423d99SAdrian Hunter 	 * If erasesize is a power of 2 then the shift is stored in
23069423d99SAdrian Hunter 	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
23169423d99SAdrian Hunter 	 */
23269423d99SAdrian Hunter 	unsigned int erasesize_shift;
23369423d99SAdrian Hunter 	unsigned int writesize_shift;
23469423d99SAdrian Hunter 	/* Masks based on erasesize_shift and writesize_shift */
23569423d99SAdrian Hunter 	unsigned int erasesize_mask;
23669423d99SAdrian Hunter 	unsigned int writesize_mask;
2371da177e4SLinus Torvalds 
238d062d4edSMike Dunn 	/*
239d062d4edSMike Dunn 	 * read ops return -EUCLEAN if max number of bitflips corrected on any
240d062d4edSMike Dunn 	 * one region comprising an ecc step equals or exceeds this value.
241d062d4edSMike Dunn 	 * Settable by driver, else defaults to ecc_strength.  User can override
242d062d4edSMike Dunn 	 * in sysfs.  N.B. The meaning of the -EUCLEAN return code has changed;
243d062d4edSMike Dunn 	 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
244d062d4edSMike Dunn 	 */
245d062d4edSMike Dunn 	unsigned int bitflip_threshold;
246d062d4edSMike Dunn 
247971e4aeeSPavel Machek 	/* Kernel-only stuff starts here. */
248eadcf0d7SGreg Kroah-Hartman 	const char *name;
2491da177e4SLinus Torvalds 	int index;
2501da177e4SLinus Torvalds 
251adbbc3bcSBoris Brezillon 	/* OOB layout description */
252adbbc3bcSBoris Brezillon 	const struct mtd_ooblayout_ops *ooblayout;
253adbbc3bcSBoris Brezillon 
254477b0229SBoris Brezillon 	/* NAND pairing scheme, only provided for MLC/TLC NANDs */
255477b0229SBoris Brezillon 	const struct mtd_pairing_scheme *pairing;
256477b0229SBoris Brezillon 
257c41a0582SHuang Shijie 	/* the ecc step size. */
258c41a0582SHuang Shijie 	unsigned int ecc_step_size;
259c41a0582SHuang Shijie 
26086c2072bSMike Dunn 	/* max number of correctible bit errors per ecc step */
2611d0b95b0SMike Dunn 	unsigned int ecc_strength;
2621d0b95b0SMike Dunn 
2631da177e4SLinus Torvalds 	/* Data for variable erase regions. If numeraseregions is zero,
2641da177e4SLinus Torvalds 	 * it means that the whole device has erasesize as given above.
2651da177e4SLinus Torvalds 	 */
2661da177e4SLinus Torvalds 	int numeraseregions;
2671da177e4SLinus Torvalds 	struct mtd_erase_region_info *eraseregions;
2681da177e4SLinus Torvalds 
269b38178eeSJörn Engel 	/*
2707e1f0dc0SArtem Bityutskiy 	 * Do not call via these pointers, use corresponding mtd_*()
2717e1f0dc0SArtem Bityutskiy 	 * wrappers instead.
272b38178eeSJörn Engel 	 */
2733c3c10bbSArtem Bityutskiy 	int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
2743c3c10bbSArtem Bityutskiy 	int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
275a98889f3SJared Hulbert 		       size_t *retlen, void **virt, resource_size_t *phys);
2765e4e6e3fSArtem Bityutskiy 	int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
2773c3c10bbSArtem Bityutskiy 	int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
278329ad399SArtem Bityutskiy 		      size_t *retlen, u_char *buf);
2793c3c10bbSArtem Bityutskiy 	int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
280eda95cbfSArtem Bityutskiy 		       size_t *retlen, const u_char *buf);
2813c3c10bbSArtem Bityutskiy 	int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
2827ae79d7fSArtem Bityutskiy 			     size_t *retlen, const u_char *buf);
2833c3c10bbSArtem Bityutskiy 	int (*_read_oob) (struct mtd_info *mtd, loff_t from,
284fd2819bbSArtem Bityutskiy 			  struct mtd_oob_ops *ops);
2853c3c10bbSArtem Bityutskiy 	int (*_write_oob) (struct mtd_info *mtd, loff_t to,
286a2cc5ba0SArtem Bityutskiy 			   struct mtd_oob_ops *ops);
2874b78fc42SChristian Riesch 	int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
2884b78fc42SChristian Riesch 				    size_t *retlen, struct otp_info *buf);
2893c3c10bbSArtem Bityutskiy 	int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
290d264f72aSArtem Bityutskiy 				    size_t len, size_t *retlen, u_char *buf);
2914b78fc42SChristian Riesch 	int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
2924b78fc42SChristian Riesch 				    size_t *retlen, struct otp_info *buf);
2933c3c10bbSArtem Bityutskiy 	int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
2944ea1cabbSArtem Bityutskiy 				    size_t len, size_t *retlen, u_char *buf);
2953c3c10bbSArtem Bityutskiy 	int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
2963c3c10bbSArtem Bityutskiy 				     size_t len, size_t *retlen, u_char *buf);
2973c3c10bbSArtem Bityutskiy 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
2984403dbfbSArtem Bityutskiy 				    size_t len);
2993c3c10bbSArtem Bityutskiy 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
300b0a31f7bSArtem Bityutskiy 			unsigned long count, loff_t to, size_t *retlen);
3013c3c10bbSArtem Bityutskiy 	void (*_sync) (struct mtd_info *mtd);
3023c3c10bbSArtem Bityutskiy 	int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3033c3c10bbSArtem Bityutskiy 	int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3043c3c10bbSArtem Bityutskiy 	int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3058471bb73SEzequiel Garcia 	int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
3063c3c10bbSArtem Bityutskiy 	int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
3073c3c10bbSArtem Bityutskiy 	int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
3086080ef6eSJeff Westfahl 	int (*_max_bad_blocks) (struct mtd_info *mtd, loff_t ofs, size_t len);
3093c3c10bbSArtem Bityutskiy 	int (*_suspend) (struct mtd_info *mtd);
3103c3c10bbSArtem Bityutskiy 	void (*_resume) (struct mtd_info *mtd);
3113efe41beSBrian Norris 	void (*_reboot) (struct mtd_info *mtd);
312a88d2dc6SArtem Bityutskiy 	/*
313a88d2dc6SArtem Bityutskiy 	 * If the driver is something smart, like UBI, it may need to maintain
314a88d2dc6SArtem Bityutskiy 	 * its own reference counting. The below functions are only for driver.
315a88d2dc6SArtem Bityutskiy 	 */
3163c3c10bbSArtem Bityutskiy 	int (*_get_device) (struct mtd_info *mtd);
3173c3c10bbSArtem Bityutskiy 	void (*_put_device) (struct mtd_info *mtd);
318402d3265SDavid Howells 
3199f897bfdSKamal Dasu 	/*
3209f897bfdSKamal Dasu 	 * flag indicates a panic write, low level drivers can take appropriate
3219f897bfdSKamal Dasu 	 * action if required to ensure writes go through
3229f897bfdSKamal Dasu 	 */
3239f897bfdSKamal Dasu 	bool oops_panic_write;
3249f897bfdSKamal Dasu 
325963a6fb0SNicolas Pitre 	struct notifier_block reboot_notifier;  /* default mode before reboot */
326963a6fb0SNicolas Pitre 
3277fac4648SThomas Gleixner 	/* ECC status information */
3287fac4648SThomas Gleixner 	struct mtd_ecc_stats ecc_stats;
32929072b96SThomas Gleixner 	/* Subpage shift (NAND) */
33029072b96SThomas Gleixner 	int subpage_sft;
3317fac4648SThomas Gleixner 
3321da177e4SLinus Torvalds 	void *priv;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	struct module *owner;
3351f24b5a8SDavid Brownell 	struct device dev;
3361da177e4SLinus Torvalds 	int usecount;
337e8e3edb9SMario Rugiero 	struct mtd_debug_info dbg;
338c4dfa25aSAlban Bedel 	struct nvmem_device *nvmem;
3391da177e4SLinus Torvalds };
3401da177e4SLinus Torvalds 
34175eb2cecSBoris Brezillon int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
34275eb2cecSBoris Brezillon 		      struct mtd_oob_region *oobecc);
34375eb2cecSBoris Brezillon int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
34475eb2cecSBoris Brezillon 				 int *section,
34575eb2cecSBoris Brezillon 				 struct mtd_oob_region *oobregion);
34675eb2cecSBoris Brezillon int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
34775eb2cecSBoris Brezillon 			       const u8 *oobbuf, int start, int nbytes);
34875eb2cecSBoris Brezillon int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
34975eb2cecSBoris Brezillon 			       u8 *oobbuf, int start, int nbytes);
35075eb2cecSBoris Brezillon int mtd_ooblayout_free(struct mtd_info *mtd, int section,
35175eb2cecSBoris Brezillon 		       struct mtd_oob_region *oobfree);
35275eb2cecSBoris Brezillon int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
35375eb2cecSBoris Brezillon 				const u8 *oobbuf, int start, int nbytes);
35475eb2cecSBoris Brezillon int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
35575eb2cecSBoris Brezillon 				u8 *oobbuf, int start, int nbytes);
35675eb2cecSBoris Brezillon int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
35775eb2cecSBoris Brezillon int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
35875eb2cecSBoris Brezillon 
359adbbc3bcSBoris Brezillon static inline void mtd_set_ooblayout(struct mtd_info *mtd,
360adbbc3bcSBoris Brezillon 				     const struct mtd_ooblayout_ops *ooblayout)
361036d6543SBoris Brezillon {
362adbbc3bcSBoris Brezillon 	mtd->ooblayout = ooblayout;
363036d6543SBoris Brezillon }
364036d6543SBoris Brezillon 
365477b0229SBoris Brezillon static inline void mtd_set_pairing_scheme(struct mtd_info *mtd,
366477b0229SBoris Brezillon 				const struct mtd_pairing_scheme *pairing)
367477b0229SBoris Brezillon {
368477b0229SBoris Brezillon 	mtd->pairing = pairing;
369477b0229SBoris Brezillon }
370477b0229SBoris Brezillon 
37128b8b26bSBrian Norris static inline void mtd_set_of_node(struct mtd_info *mtd,
37228b8b26bSBrian Norris 				   struct device_node *np)
37328b8b26bSBrian Norris {
37428b8b26bSBrian Norris 	mtd->dev.of_node = np;
37528309572SCédric Le Goater 	if (!mtd->name)
37628309572SCédric Le Goater 		of_property_read_string(np, "label", &mtd->name);
37728b8b26bSBrian Norris }
37828b8b26bSBrian Norris 
37928b8b26bSBrian Norris static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
38028b8b26bSBrian Norris {
3814a67c9fdSRafał Miłecki 	return dev_of_node(&mtd->dev);
38228b8b26bSBrian Norris }
38328b8b26bSBrian Norris 
3844348433dSMiquel Raynal static inline u32 mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
38529f1058aSBoris BREZILLON {
38629f1058aSBoris BREZILLON 	return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
38729f1058aSBoris BREZILLON }
38829f1058aSBoris BREZILLON 
3896080ef6eSJeff Westfahl static inline int mtd_max_bad_blocks(struct mtd_info *mtd,
3906080ef6eSJeff Westfahl 				     loff_t ofs, size_t len)
3916080ef6eSJeff Westfahl {
3926080ef6eSJeff Westfahl 	if (!mtd->_max_bad_blocks)
3936080ef6eSJeff Westfahl 		return -ENOTSUPP;
3946080ef6eSJeff Westfahl 
3956080ef6eSJeff Westfahl 	if (mtd->size < (len + ofs) || ofs < 0)
3966080ef6eSJeff Westfahl 		return -EINVAL;
3976080ef6eSJeff Westfahl 
3986080ef6eSJeff Westfahl 	return mtd->_max_bad_blocks(mtd, ofs, len);
3996080ef6eSJeff Westfahl }
4006080ef6eSJeff Westfahl 
401477b0229SBoris Brezillon int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
402477b0229SBoris Brezillon 			      struct mtd_pairing_info *info);
403477b0229SBoris Brezillon int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
404477b0229SBoris Brezillon 			      const struct mtd_pairing_info *info);
405477b0229SBoris Brezillon int mtd_pairing_groups(struct mtd_info *mtd);
4068273a0c9SArtem Bityutskiy int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
4078273a0c9SArtem Bityutskiy int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
4088273a0c9SArtem Bityutskiy 	      void **virt, resource_size_t *phys);
4098273a0c9SArtem Bityutskiy int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
4108273a0c9SArtem Bityutskiy unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
4118273a0c9SArtem Bityutskiy 				    unsigned long offset, unsigned long flags);
4128273a0c9SArtem Bityutskiy int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
4138273a0c9SArtem Bityutskiy 	     u_char *buf);
4148273a0c9SArtem Bityutskiy int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
4158273a0c9SArtem Bityutskiy 	      const u_char *buf);
4168273a0c9SArtem Bityutskiy int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
4178273a0c9SArtem Bityutskiy 		    const u_char *buf);
4187ae79d7fSArtem Bityutskiy 
419d2d48480SBrian Norris int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
4200c034fe3SEzequiel Garcia int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
421a2cc5ba0SArtem Bityutskiy 
4224b78fc42SChristian Riesch int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
4234b78fc42SChristian Riesch 			   struct otp_info *buf);
424de3cac93SArtem Bityutskiy int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
425de3cac93SArtem Bityutskiy 			   size_t *retlen, u_char *buf);
4264b78fc42SChristian Riesch int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
4274b78fc42SChristian Riesch 			   struct otp_info *buf);
428de3cac93SArtem Bityutskiy int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
429de3cac93SArtem Bityutskiy 			   size_t *retlen, u_char *buf);
430de3cac93SArtem Bityutskiy int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
431de3cac93SArtem Bityutskiy 			    size_t *retlen, u_char *buf);
432de3cac93SArtem Bityutskiy int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
4334403dbfbSArtem Bityutskiy 
4341dbebd32SArtem Bityutskiy int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
4351dbebd32SArtem Bityutskiy 	       unsigned long count, loff_t to, size_t *retlen);
436b0a31f7bSArtem Bityutskiy 
43785f2f2a8SArtem Bityutskiy static inline void mtd_sync(struct mtd_info *mtd)
43885f2f2a8SArtem Bityutskiy {
4393c3c10bbSArtem Bityutskiy 	if (mtd->_sync)
4403c3c10bbSArtem Bityutskiy 		mtd->_sync(mtd);
44185f2f2a8SArtem Bityutskiy }
44285f2f2a8SArtem Bityutskiy 
4438273a0c9SArtem Bityutskiy int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
4448273a0c9SArtem Bityutskiy int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
4458273a0c9SArtem Bityutskiy int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
4468471bb73SEzequiel Garcia int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
4478273a0c9SArtem Bityutskiy int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
4488273a0c9SArtem Bityutskiy int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
449e95e9786SArtem Bityutskiy 
4503fe4bae8SArtem Bityutskiy static inline int mtd_suspend(struct mtd_info *mtd)
4513fe4bae8SArtem Bityutskiy {
4523c3c10bbSArtem Bityutskiy 	return mtd->_suspend ? mtd->_suspend(mtd) : 0;
4533fe4bae8SArtem Bityutskiy }
4543fe4bae8SArtem Bityutskiy 
455ead995f8SArtem Bityutskiy static inline void mtd_resume(struct mtd_info *mtd)
456ead995f8SArtem Bityutskiy {
4573c3c10bbSArtem Bityutskiy 	if (mtd->_resume)
4583c3c10bbSArtem Bityutskiy 		mtd->_resume(mtd);
459ead995f8SArtem Bityutskiy }
460ead995f8SArtem Bityutskiy 
46126cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
46269423d99SAdrian Hunter {
46369423d99SAdrian Hunter 	if (mtd->erasesize_shift)
46469423d99SAdrian Hunter 		return sz >> mtd->erasesize_shift;
46569423d99SAdrian Hunter 	do_div(sz, mtd->erasesize);
46669423d99SAdrian Hunter 	return sz;
46769423d99SAdrian Hunter }
46869423d99SAdrian Hunter 
46926cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
47069423d99SAdrian Hunter {
47169423d99SAdrian Hunter 	if (mtd->erasesize_shift)
47269423d99SAdrian Hunter 		return sz & mtd->erasesize_mask;
47369423d99SAdrian Hunter 	return do_div(sz, mtd->erasesize);
47469423d99SAdrian Hunter }
47569423d99SAdrian Hunter 
476f72071b8SBoris Brezillon /**
477f72071b8SBoris Brezillon  * mtd_align_erase_req - Adjust an erase request to align things on eraseblock
478f72071b8SBoris Brezillon  *			 boundaries.
479f72071b8SBoris Brezillon  * @mtd: the MTD device this erase request applies on
480f72071b8SBoris Brezillon  * @req: the erase request to adjust
481f72071b8SBoris Brezillon  *
482f72071b8SBoris Brezillon  * This function will adjust @req->addr and @req->len to align them on
483f72071b8SBoris Brezillon  * @mtd->erasesize. Of course we expect @mtd->erasesize to be != 0.
484f72071b8SBoris Brezillon  */
485f72071b8SBoris Brezillon static inline void mtd_align_erase_req(struct mtd_info *mtd,
486f72071b8SBoris Brezillon 				       struct erase_info *req)
487f72071b8SBoris Brezillon {
488f72071b8SBoris Brezillon 	u32 mod;
489f72071b8SBoris Brezillon 
490f72071b8SBoris Brezillon 	if (WARN_ON(!mtd->erasesize))
491f72071b8SBoris Brezillon 		return;
492f72071b8SBoris Brezillon 
493f72071b8SBoris Brezillon 	mod = mtd_mod_by_eb(req->addr, mtd);
494f72071b8SBoris Brezillon 	if (mod) {
495f72071b8SBoris Brezillon 		req->addr -= mod;
496f72071b8SBoris Brezillon 		req->len += mod;
497f72071b8SBoris Brezillon 	}
498f72071b8SBoris Brezillon 
499f72071b8SBoris Brezillon 	mod = mtd_mod_by_eb(req->addr + req->len, mtd);
500f72071b8SBoris Brezillon 	if (mod)
501f72071b8SBoris Brezillon 		req->len += mtd->erasesize - mod;
502f72071b8SBoris Brezillon }
503f72071b8SBoris Brezillon 
50426cdb67cSDavid Woodhouse static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
50569423d99SAdrian Hunter {
50669423d99SAdrian Hunter 	if (mtd->writesize_shift)
50769423d99SAdrian Hunter 		return sz >> mtd->writesize_shift;
50869423d99SAdrian Hunter 	do_div(sz, mtd->writesize);
50969423d99SAdrian Hunter 	return sz;
51069423d99SAdrian Hunter }
51169423d99SAdrian Hunter 
51226cdb67cSDavid Woodhouse static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
51369423d99SAdrian Hunter {
51469423d99SAdrian Hunter 	if (mtd->writesize_shift)
51569423d99SAdrian Hunter 		return sz & mtd->writesize_mask;
51669423d99SAdrian Hunter 	return do_div(sz, mtd->writesize);
51769423d99SAdrian Hunter }
5181da177e4SLinus Torvalds 
519477b0229SBoris Brezillon static inline int mtd_wunit_per_eb(struct mtd_info *mtd)
520477b0229SBoris Brezillon {
521477b0229SBoris Brezillon 	return mtd->erasesize / mtd->writesize;
522477b0229SBoris Brezillon }
523477b0229SBoris Brezillon 
524477b0229SBoris Brezillon static inline int mtd_offset_to_wunit(struct mtd_info *mtd, loff_t offs)
525477b0229SBoris Brezillon {
526477b0229SBoris Brezillon 	return mtd_div_by_ws(mtd_mod_by_eb(offs, mtd), mtd);
527477b0229SBoris Brezillon }
528477b0229SBoris Brezillon 
529477b0229SBoris Brezillon static inline loff_t mtd_wunit_to_offset(struct mtd_info *mtd, loff_t base,
530477b0229SBoris Brezillon 					 int wunit)
531477b0229SBoris Brezillon {
532477b0229SBoris Brezillon 	return base + (wunit * mtd->writesize);
533477b0229SBoris Brezillon }
534477b0229SBoris Brezillon 
535477b0229SBoris Brezillon 
536fc002e3cSArtem Bityutskiy static inline int mtd_has_oob(const struct mtd_info *mtd)
537fc002e3cSArtem Bityutskiy {
5383c3c10bbSArtem Bityutskiy 	return mtd->_read_oob && mtd->_write_oob;
539fc002e3cSArtem Bityutskiy }
540fc002e3cSArtem Bityutskiy 
541818b9739SHuang Shijie static inline int mtd_type_is_nand(const struct mtd_info *mtd)
542818b9739SHuang Shijie {
543818b9739SHuang Shijie 	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
544818b9739SHuang Shijie }
545818b9739SHuang Shijie 
5468f461a73SArtem Bityutskiy static inline int mtd_can_have_bb(const struct mtd_info *mtd)
5478f461a73SArtem Bityutskiy {
5483c3c10bbSArtem Bityutskiy 	return !!mtd->_block_isbad;
5498f461a73SArtem Bityutskiy }
5508f461a73SArtem Bityutskiy 
5511da177e4SLinus Torvalds 	/* Kernel-side ioctl definitions */
5521da177e4SLinus Torvalds 
553f5671ab3SJamie Iles struct mtd_partition;
554c7975330SDmitry Eremin-Solenikov struct mtd_part_parser_data;
555f5671ab3SJamie Iles 
5561c4c215cSDmitry Eremin-Solenikov extern int mtd_device_parse_register(struct mtd_info *mtd,
55726a47346SArtem Bityutskiy 				     const char * const *part_probe_types,
558c7975330SDmitry Eremin-Solenikov 				     struct mtd_part_parser_data *parser_data,
5591c4c215cSDmitry Eremin-Solenikov 				     const struct mtd_partition *defparts,
5601c4c215cSDmitry Eremin-Solenikov 				     int defnr_parts);
56115c60a50SDmitry Eremin-Solenikov #define mtd_device_register(master, parts, nr_parts)	\
56215c60a50SDmitry Eremin-Solenikov 	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
563f5671ab3SJamie Iles extern int mtd_device_unregister(struct mtd_info *master);
5641da177e4SLinus Torvalds extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
5653bd45657SMaxim Levitsky extern int __get_mtd_device(struct mtd_info *mtd);
5663bd45657SMaxim Levitsky extern void __put_mtd_device(struct mtd_info *mtd);
5677799308fSArtem Bityutskiy extern struct mtd_info *get_mtd_device_nm(const char *name);
5681da177e4SLinus Torvalds extern void put_mtd_device(struct mtd_info *mtd);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds struct mtd_notifier {
5721da177e4SLinus Torvalds 	void (*add)(struct mtd_info *mtd);
5731da177e4SLinus Torvalds 	void (*remove)(struct mtd_info *mtd);
5741da177e4SLinus Torvalds 	struct list_head list;
5751da177e4SLinus Torvalds };
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds extern void register_mtd_user (struct mtd_notifier *new);
5791da177e4SLinus Torvalds extern int unregister_mtd_user (struct mtd_notifier *old);
58033b53716SGrant Erickson void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
58133b53716SGrant Erickson 
5827387ce77SBrian Norris static inline int mtd_is_bitflip(int err) {
5837387ce77SBrian Norris 	return err == -EUCLEAN;
5847387ce77SBrian Norris }
5851da177e4SLinus Torvalds 
5867387ce77SBrian Norris static inline int mtd_is_eccerr(int err) {
5877387ce77SBrian Norris 	return err == -EBADMSG;
5887387ce77SBrian Norris }
5891da177e4SLinus Torvalds 
5907387ce77SBrian Norris static inline int mtd_is_bitflip_or_eccerr(int err) {
5917387ce77SBrian Norris 	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
5927387ce77SBrian Norris }
5931da177e4SLinus Torvalds 
594b4caecd4SChristoph Hellwig unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
595b4caecd4SChristoph Hellwig 
5961da177e4SLinus Torvalds #endif /* __MTD_MTD_H__ */
597