xref: /kernel/linux/linux-5.10/include/linux/mtd/mtd.h (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef __MTD_MTD_H__
78c2ecf20Sopenharmony_ci#define __MTD_MTD_H__
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/types.h>
108c2ecf20Sopenharmony_ci#include <linux/uio.h>
118c2ecf20Sopenharmony_ci#include <linux/list.h>
128c2ecf20Sopenharmony_ci#include <linux/notifier.h>
138c2ecf20Sopenharmony_ci#include <linux/device.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include <linux/nvmem-provider.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <mtd/mtd-abi.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <asm/div64.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define MTD_FAIL_ADDR_UNKNOWN -1LL
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct mtd_info;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * If the erase fails, fail_addr might indicate exactly which block failed. If
278c2ecf20Sopenharmony_ci * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
288c2ecf20Sopenharmony_ci * or was not specific to any particular block.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_cistruct erase_info {
318c2ecf20Sopenharmony_ci	uint64_t addr;
328c2ecf20Sopenharmony_ci	uint64_t len;
338c2ecf20Sopenharmony_ci	uint64_t fail_addr;
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct mtd_erase_region_info {
378c2ecf20Sopenharmony_ci	uint64_t offset;		/* At which this region starts, from the beginning of the MTD */
388c2ecf20Sopenharmony_ci	uint32_t erasesize;		/* For this region */
398c2ecf20Sopenharmony_ci	uint32_t numblocks;		/* Number of blocks of erasesize in this region */
408c2ecf20Sopenharmony_ci	unsigned long *lockmap;		/* If keeping bitmap of locks */
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/**
448c2ecf20Sopenharmony_ci * struct mtd_oob_ops - oob operation operands
458c2ecf20Sopenharmony_ci * @mode:	operation mode
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * @len:	number of data bytes to write/read
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * @retlen:	number of data bytes written/read
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * @ooblen:	number of oob bytes to write/read
528c2ecf20Sopenharmony_ci * @oobretlen:	number of oob bytes written/read
538c2ecf20Sopenharmony_ci * @ooboffs:	offset of oob data in the oob area (only relevant when
548c2ecf20Sopenharmony_ci *		mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
558c2ecf20Sopenharmony_ci * @datbuf:	data buffer - if NULL only oob data are read/written
568c2ecf20Sopenharmony_ci * @oobbuf:	oob data buffer
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * Note, some MTD drivers do not allow you to write more than one OOB area at
598c2ecf20Sopenharmony_ci * one go. If you try to do that on such an MTD device, -EINVAL will be
608c2ecf20Sopenharmony_ci * returned. If you want to make your implementation portable on all kind of MTD
618c2ecf20Sopenharmony_ci * devices you should split the write request into several sub-requests when the
628c2ecf20Sopenharmony_ci * request crosses a page boundary.
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_cistruct mtd_oob_ops {
658c2ecf20Sopenharmony_ci	unsigned int	mode;
668c2ecf20Sopenharmony_ci	size_t		len;
678c2ecf20Sopenharmony_ci	size_t		retlen;
688c2ecf20Sopenharmony_ci	size_t		ooblen;
698c2ecf20Sopenharmony_ci	size_t		oobretlen;
708c2ecf20Sopenharmony_ci	uint32_t	ooboffs;
718c2ecf20Sopenharmony_ci	uint8_t		*datbuf;
728c2ecf20Sopenharmony_ci	uint8_t		*oobbuf;
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define MTD_MAX_OOBFREE_ENTRIES_LARGE	32
768c2ecf20Sopenharmony_ci#define MTD_MAX_ECCPOS_ENTRIES_LARGE	640
778c2ecf20Sopenharmony_ci/**
788c2ecf20Sopenharmony_ci * struct mtd_oob_region - oob region definition
798c2ecf20Sopenharmony_ci * @offset: region offset
808c2ecf20Sopenharmony_ci * @length: region length
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * This structure describes a region of the OOB area, and is used
838c2ecf20Sopenharmony_ci * to retrieve ECC or free bytes sections.
848c2ecf20Sopenharmony_ci * Each section is defined by an offset within the OOB area and a
858c2ecf20Sopenharmony_ci * length.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_cistruct mtd_oob_region {
888c2ecf20Sopenharmony_ci	u32 offset;
898c2ecf20Sopenharmony_ci	u32 length;
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * struct mtd_ooblayout_ops - NAND OOB layout operations
948c2ecf20Sopenharmony_ci * @ecc: function returning an ECC region in the OOB area.
958c2ecf20Sopenharmony_ci *	 Should return -ERANGE if %section exceeds the total number of
968c2ecf20Sopenharmony_ci *	 ECC sections.
978c2ecf20Sopenharmony_ci * @free: function returning a free region in the OOB area.
988c2ecf20Sopenharmony_ci *	  Should return -ERANGE if %section exceeds the total number of
998c2ecf20Sopenharmony_ci *	  free sections.
1008c2ecf20Sopenharmony_ci */
1018c2ecf20Sopenharmony_cistruct mtd_ooblayout_ops {
1028c2ecf20Sopenharmony_ci	int (*ecc)(struct mtd_info *mtd, int section,
1038c2ecf20Sopenharmony_ci		   struct mtd_oob_region *oobecc);
1048c2ecf20Sopenharmony_ci	int (*free)(struct mtd_info *mtd, int section,
1058c2ecf20Sopenharmony_ci		    struct mtd_oob_region *oobfree);
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/**
1098c2ecf20Sopenharmony_ci * struct mtd_pairing_info - page pairing information
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * @pair: pair id
1128c2ecf20Sopenharmony_ci * @group: group id
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * The term "pair" is used here, even though TLC NANDs might group pages by 3
1158c2ecf20Sopenharmony_ci * (3 bits in a single cell). A pair should regroup all pages that are sharing
1168c2ecf20Sopenharmony_ci * the same cell. Pairs are then indexed in ascending order.
1178c2ecf20Sopenharmony_ci *
1188c2ecf20Sopenharmony_ci * @group is defining the position of a page in a given pair. It can also be
1198c2ecf20Sopenharmony_ci * seen as the bit position in the cell: page attached to bit 0 belongs to
1208c2ecf20Sopenharmony_ci * group 0, page attached to bit 1 belongs to group 1, etc.
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * Example:
1238c2ecf20Sopenharmony_ci * The H27UCG8T2BTR-BC datasheet describes the following pairing scheme:
1248c2ecf20Sopenharmony_ci *
1258c2ecf20Sopenharmony_ci *		group-0		group-1
1268c2ecf20Sopenharmony_ci *
1278c2ecf20Sopenharmony_ci *  pair-0	page-0		page-4
1288c2ecf20Sopenharmony_ci *  pair-1	page-1		page-5
1298c2ecf20Sopenharmony_ci *  pair-2	page-2		page-8
1308c2ecf20Sopenharmony_ci *  ...
1318c2ecf20Sopenharmony_ci *  pair-127	page-251	page-255
1328c2ecf20Sopenharmony_ci *
1338c2ecf20Sopenharmony_ci *
1348c2ecf20Sopenharmony_ci * Note that the "group" and "pair" terms were extracted from Samsung and
1358c2ecf20Sopenharmony_ci * Hynix datasheets, and might be referenced under other names in other
1368c2ecf20Sopenharmony_ci * datasheets (Micron is describing this concept as "shared pages").
1378c2ecf20Sopenharmony_ci */
1388c2ecf20Sopenharmony_cistruct mtd_pairing_info {
1398c2ecf20Sopenharmony_ci	int pair;
1408c2ecf20Sopenharmony_ci	int group;
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/**
1448c2ecf20Sopenharmony_ci * struct mtd_pairing_scheme - page pairing scheme description
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * @ngroups: number of groups. Should be related to the number of bits
1478c2ecf20Sopenharmony_ci *	     per cell.
1488c2ecf20Sopenharmony_ci * @get_info: converts a write-unit (page number within an erase block) into
1498c2ecf20Sopenharmony_ci *	      mtd_pairing information (pair + group). This function should
1508c2ecf20Sopenharmony_ci *	      fill the info parameter based on the wunit index or return
1518c2ecf20Sopenharmony_ci *	      -EINVAL if the wunit parameter is invalid.
1528c2ecf20Sopenharmony_ci * @get_wunit: converts pairing information into a write-unit (page) number.
1538c2ecf20Sopenharmony_ci *	       This function should return the wunit index pointed by the
1548c2ecf20Sopenharmony_ci *	       pairing information described in the info argument. It should
1558c2ecf20Sopenharmony_ci *	       return -EINVAL, if there's no wunit corresponding to the
1568c2ecf20Sopenharmony_ci *	       passed pairing information.
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * See mtd_pairing_info documentation for a detailed explanation of the
1598c2ecf20Sopenharmony_ci * pair and group concepts.
1608c2ecf20Sopenharmony_ci *
1618c2ecf20Sopenharmony_ci * The mtd_pairing_scheme structure provides a generic solution to represent
1628c2ecf20Sopenharmony_ci * NAND page pairing scheme. Instead of exposing two big tables to do the
1638c2ecf20Sopenharmony_ci * write-unit <-> (pair + group) conversions, we ask the MTD drivers to
1648c2ecf20Sopenharmony_ci * implement the ->get_info() and ->get_wunit() functions.
1658c2ecf20Sopenharmony_ci *
1668c2ecf20Sopenharmony_ci * MTD users will then be able to query these information by using the
1678c2ecf20Sopenharmony_ci * mtd_pairing_info_to_wunit() and mtd_wunit_to_pairing_info() helpers.
1688c2ecf20Sopenharmony_ci *
1698c2ecf20Sopenharmony_ci * @ngroups is here to help MTD users iterating over all the pages in a
1708c2ecf20Sopenharmony_ci * given pair. This value can be retrieved by MTD users using the
1718c2ecf20Sopenharmony_ci * mtd_pairing_groups() helper.
1728c2ecf20Sopenharmony_ci *
1738c2ecf20Sopenharmony_ci * Examples are given in the mtd_pairing_info_to_wunit() and
1748c2ecf20Sopenharmony_ci * mtd_wunit_to_pairing_info() documentation.
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_cistruct mtd_pairing_scheme {
1778c2ecf20Sopenharmony_ci	int ngroups;
1788c2ecf20Sopenharmony_ci	int (*get_info)(struct mtd_info *mtd, int wunit,
1798c2ecf20Sopenharmony_ci			struct mtd_pairing_info *info);
1808c2ecf20Sopenharmony_ci	int (*get_wunit)(struct mtd_info *mtd,
1818c2ecf20Sopenharmony_ci			 const struct mtd_pairing_info *info);
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistruct module;	/* only needed for owner field in mtd_info */
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/**
1878c2ecf20Sopenharmony_ci * struct mtd_debug_info - debugging information for an MTD device.
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci * @dfs_dir: direntry object of the MTD device debugfs directory
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_cistruct mtd_debug_info {
1928c2ecf20Sopenharmony_ci	struct dentry *dfs_dir;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	const char *partname;
1958c2ecf20Sopenharmony_ci	const char *partid;
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/**
1998c2ecf20Sopenharmony_ci * struct mtd_part - MTD partition specific fields
2008c2ecf20Sopenharmony_ci *
2018c2ecf20Sopenharmony_ci * @node: list node used to add an MTD partition to the parent partition list
2028c2ecf20Sopenharmony_ci * @offset: offset of the partition relatively to the parent offset
2038c2ecf20Sopenharmony_ci * @size: partition size. Should be equal to mtd->size unless
2048c2ecf20Sopenharmony_ci *	  MTD_SLC_ON_MLC_EMULATION is set
2058c2ecf20Sopenharmony_ci * @flags: original flags (before the mtdpart logic decided to tweak them based
2068c2ecf20Sopenharmony_ci *	   on flash constraints, like eraseblock/pagesize alignment)
2078c2ecf20Sopenharmony_ci *
2088c2ecf20Sopenharmony_ci * This struct is embedded in mtd_info and contains partition-specific
2098c2ecf20Sopenharmony_ci * properties/fields.
2108c2ecf20Sopenharmony_ci */
2118c2ecf20Sopenharmony_cistruct mtd_part {
2128c2ecf20Sopenharmony_ci	struct list_head node;
2138c2ecf20Sopenharmony_ci	u64 offset;
2148c2ecf20Sopenharmony_ci	u64 size;
2158c2ecf20Sopenharmony_ci	u32 flags;
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/**
2198c2ecf20Sopenharmony_ci * struct mtd_master - MTD master specific fields
2208c2ecf20Sopenharmony_ci *
2218c2ecf20Sopenharmony_ci * @partitions_lock: lock protecting accesses to the partition list. Protects
2228c2ecf20Sopenharmony_ci *		     not only the master partition list, but also all
2238c2ecf20Sopenharmony_ci *		     sub-partitions.
2248c2ecf20Sopenharmony_ci * @suspended: et to 1 when the device is suspended, 0 otherwise
2258c2ecf20Sopenharmony_ci *
2268c2ecf20Sopenharmony_ci * This struct is embedded in mtd_info and contains master-specific
2278c2ecf20Sopenharmony_ci * properties/fields. The master is the root MTD device from the MTD partition
2288c2ecf20Sopenharmony_ci * point of view.
2298c2ecf20Sopenharmony_ci */
2308c2ecf20Sopenharmony_cistruct mtd_master {
2318c2ecf20Sopenharmony_ci	struct mutex partitions_lock;
2328c2ecf20Sopenharmony_ci	unsigned int suspended : 1;
2338c2ecf20Sopenharmony_ci};
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistruct mtd_info {
2368c2ecf20Sopenharmony_ci	u_char type;
2378c2ecf20Sopenharmony_ci	uint32_t flags;
2388c2ecf20Sopenharmony_ci	uint64_t size;	 // Total size of the MTD
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	/* "Major" erase size for the device. Naïve users may take this
2418c2ecf20Sopenharmony_ci	 * to be the only erase size available, or may use the more detailed
2428c2ecf20Sopenharmony_ci	 * information below if they desire
2438c2ecf20Sopenharmony_ci	 */
2448c2ecf20Sopenharmony_ci	uint32_t erasesize;
2458c2ecf20Sopenharmony_ci	/* Minimal writable flash unit size. In case of NOR flash it is 1 (even
2468c2ecf20Sopenharmony_ci	 * though individual bits can be cleared), in case of NAND flash it is
2478c2ecf20Sopenharmony_ci	 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
2488c2ecf20Sopenharmony_ci	 * it is of ECC block size, etc. It is illegal to have writesize = 0.
2498c2ecf20Sopenharmony_ci	 * Any driver registering a struct mtd_info must ensure a writesize of
2508c2ecf20Sopenharmony_ci	 * 1 or larger.
2518c2ecf20Sopenharmony_ci	 */
2528c2ecf20Sopenharmony_ci	uint32_t writesize;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/*
2558c2ecf20Sopenharmony_ci	 * Size of the write buffer used by the MTD. MTD devices having a write
2568c2ecf20Sopenharmony_ci	 * buffer can write multiple writesize chunks at a time. E.g. while
2578c2ecf20Sopenharmony_ci	 * writing 4 * writesize bytes to a device with 2 * writesize bytes
2588c2ecf20Sopenharmony_ci	 * buffer the MTD driver can (but doesn't have to) do 2 writesize
2598c2ecf20Sopenharmony_ci	 * operations, but not 4. Currently, all NANDs have writebufsize
2608c2ecf20Sopenharmony_ci	 * equivalent to writesize (NAND page size). Some NOR flashes do have
2618c2ecf20Sopenharmony_ci	 * writebufsize greater than writesize.
2628c2ecf20Sopenharmony_ci	 */
2638c2ecf20Sopenharmony_ci	uint32_t writebufsize;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
2668c2ecf20Sopenharmony_ci	uint32_t oobavail;  // Available OOB bytes per block
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/*
2698c2ecf20Sopenharmony_ci	 * If erasesize is a power of 2 then the shift is stored in
2708c2ecf20Sopenharmony_ci	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
2718c2ecf20Sopenharmony_ci	 */
2728c2ecf20Sopenharmony_ci	unsigned int erasesize_shift;
2738c2ecf20Sopenharmony_ci	unsigned int writesize_shift;
2748c2ecf20Sopenharmony_ci	/* Masks based on erasesize_shift and writesize_shift */
2758c2ecf20Sopenharmony_ci	unsigned int erasesize_mask;
2768c2ecf20Sopenharmony_ci	unsigned int writesize_mask;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	/*
2798c2ecf20Sopenharmony_ci	 * read ops return -EUCLEAN if max number of bitflips corrected on any
2808c2ecf20Sopenharmony_ci	 * one region comprising an ecc step equals or exceeds this value.
2818c2ecf20Sopenharmony_ci	 * Settable by driver, else defaults to ecc_strength.  User can override
2828c2ecf20Sopenharmony_ci	 * in sysfs.  N.B. The meaning of the -EUCLEAN return code has changed;
2838c2ecf20Sopenharmony_ci	 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
2848c2ecf20Sopenharmony_ci	 */
2858c2ecf20Sopenharmony_ci	unsigned int bitflip_threshold;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* Kernel-only stuff starts here. */
2888c2ecf20Sopenharmony_ci	const char *name;
2898c2ecf20Sopenharmony_ci	int index;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* OOB layout description */
2928c2ecf20Sopenharmony_ci	const struct mtd_ooblayout_ops *ooblayout;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	/* NAND pairing scheme, only provided for MLC/TLC NANDs */
2958c2ecf20Sopenharmony_ci	const struct mtd_pairing_scheme *pairing;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/* the ecc step size. */
2988c2ecf20Sopenharmony_ci	unsigned int ecc_step_size;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	/* max number of correctible bit errors per ecc step */
3018c2ecf20Sopenharmony_ci	unsigned int ecc_strength;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	/* Data for variable erase regions. If numeraseregions is zero,
3048c2ecf20Sopenharmony_ci	 * it means that the whole device has erasesize as given above.
3058c2ecf20Sopenharmony_ci	 */
3068c2ecf20Sopenharmony_ci	int numeraseregions;
3078c2ecf20Sopenharmony_ci	struct mtd_erase_region_info *eraseregions;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	/*
3108c2ecf20Sopenharmony_ci	 * Do not call via these pointers, use corresponding mtd_*()
3118c2ecf20Sopenharmony_ci	 * wrappers instead.
3128c2ecf20Sopenharmony_ci	 */
3138c2ecf20Sopenharmony_ci	int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
3148c2ecf20Sopenharmony_ci	int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
3158c2ecf20Sopenharmony_ci		       size_t *retlen, void **virt, resource_size_t *phys);
3168c2ecf20Sopenharmony_ci	int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
3178c2ecf20Sopenharmony_ci	int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
3188c2ecf20Sopenharmony_ci		      size_t *retlen, u_char *buf);
3198c2ecf20Sopenharmony_ci	int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
3208c2ecf20Sopenharmony_ci		       size_t *retlen, const u_char *buf);
3218c2ecf20Sopenharmony_ci	int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
3228c2ecf20Sopenharmony_ci			     size_t *retlen, const u_char *buf);
3238c2ecf20Sopenharmony_ci	int (*_read_oob) (struct mtd_info *mtd, loff_t from,
3248c2ecf20Sopenharmony_ci			  struct mtd_oob_ops *ops);
3258c2ecf20Sopenharmony_ci	int (*_write_oob) (struct mtd_info *mtd, loff_t to,
3268c2ecf20Sopenharmony_ci			   struct mtd_oob_ops *ops);
3278c2ecf20Sopenharmony_ci	int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
3288c2ecf20Sopenharmony_ci				    size_t *retlen, struct otp_info *buf);
3298c2ecf20Sopenharmony_ci	int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
3308c2ecf20Sopenharmony_ci				    size_t len, size_t *retlen, u_char *buf);
3318c2ecf20Sopenharmony_ci	int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
3328c2ecf20Sopenharmony_ci				    size_t *retlen, struct otp_info *buf);
3338c2ecf20Sopenharmony_ci	int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
3348c2ecf20Sopenharmony_ci				    size_t len, size_t *retlen, u_char *buf);
3358c2ecf20Sopenharmony_ci	int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
3368c2ecf20Sopenharmony_ci				     size_t len, size_t *retlen, u_char *buf);
3378c2ecf20Sopenharmony_ci	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
3388c2ecf20Sopenharmony_ci				    size_t len);
3398c2ecf20Sopenharmony_ci	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
3408c2ecf20Sopenharmony_ci			unsigned long count, loff_t to, size_t *retlen);
3418c2ecf20Sopenharmony_ci	void (*_sync) (struct mtd_info *mtd);
3428c2ecf20Sopenharmony_ci	int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3438c2ecf20Sopenharmony_ci	int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3448c2ecf20Sopenharmony_ci	int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
3458c2ecf20Sopenharmony_ci	int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
3468c2ecf20Sopenharmony_ci	int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
3478c2ecf20Sopenharmony_ci	int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
3488c2ecf20Sopenharmony_ci	int (*_max_bad_blocks) (struct mtd_info *mtd, loff_t ofs, size_t len);
3498c2ecf20Sopenharmony_ci	int (*_suspend) (struct mtd_info *mtd);
3508c2ecf20Sopenharmony_ci	void (*_resume) (struct mtd_info *mtd);
3518c2ecf20Sopenharmony_ci	void (*_reboot) (struct mtd_info *mtd);
3528c2ecf20Sopenharmony_ci	/*
3538c2ecf20Sopenharmony_ci	 * If the driver is something smart, like UBI, it may need to maintain
3548c2ecf20Sopenharmony_ci	 * its own reference counting. The below functions are only for driver.
3558c2ecf20Sopenharmony_ci	 */
3568c2ecf20Sopenharmony_ci	int (*_get_device) (struct mtd_info *mtd);
3578c2ecf20Sopenharmony_ci	void (*_put_device) (struct mtd_info *mtd);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	/*
3608c2ecf20Sopenharmony_ci	 * flag indicates a panic write, low level drivers can take appropriate
3618c2ecf20Sopenharmony_ci	 * action if required to ensure writes go through
3628c2ecf20Sopenharmony_ci	 */
3638c2ecf20Sopenharmony_ci	bool oops_panic_write;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	struct notifier_block reboot_notifier;  /* default mode before reboot */
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	/* ECC status information */
3688c2ecf20Sopenharmony_ci	struct mtd_ecc_stats ecc_stats;
3698c2ecf20Sopenharmony_ci	/* Subpage shift (NAND) */
3708c2ecf20Sopenharmony_ci	int subpage_sft;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	void *priv;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	struct module *owner;
3758c2ecf20Sopenharmony_ci	struct device dev;
3768c2ecf20Sopenharmony_ci	int usecount;
3778c2ecf20Sopenharmony_ci	struct mtd_debug_info dbg;
3788c2ecf20Sopenharmony_ci	struct nvmem_device *nvmem;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	/*
3818c2ecf20Sopenharmony_ci	 * Parent device from the MTD partition point of view.
3828c2ecf20Sopenharmony_ci	 *
3838c2ecf20Sopenharmony_ci	 * MTD masters do not have any parent, MTD partitions do. The parent
3848c2ecf20Sopenharmony_ci	 * MTD device can itself be a partition.
3858c2ecf20Sopenharmony_ci	 */
3868c2ecf20Sopenharmony_ci	struct mtd_info *parent;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* List of partitions attached to this MTD device */
3898c2ecf20Sopenharmony_ci	struct list_head partitions;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	struct mtd_part part;
3928c2ecf20Sopenharmony_ci	struct mtd_master master;
3938c2ecf20Sopenharmony_ci};
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic inline struct mtd_info *mtd_get_master(struct mtd_info *mtd)
3968c2ecf20Sopenharmony_ci{
3978c2ecf20Sopenharmony_ci	while (mtd->parent)
3988c2ecf20Sopenharmony_ci		mtd = mtd->parent;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return mtd;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic inline u64 mtd_get_master_ofs(struct mtd_info *mtd, u64 ofs)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	while (mtd->parent) {
4068c2ecf20Sopenharmony_ci		ofs += mtd->part.offset;
4078c2ecf20Sopenharmony_ci		mtd = mtd->parent;
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	return ofs;
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic inline bool mtd_is_partition(const struct mtd_info *mtd)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	return mtd->parent;
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic inline bool mtd_has_partitions(const struct mtd_info *mtd)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	return !list_empty(&mtd->partitions);
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ciint mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
4248c2ecf20Sopenharmony_ci		      struct mtd_oob_region *oobecc);
4258c2ecf20Sopenharmony_ciint mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
4268c2ecf20Sopenharmony_ci				 int *section,
4278c2ecf20Sopenharmony_ci				 struct mtd_oob_region *oobregion);
4288c2ecf20Sopenharmony_ciint mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
4298c2ecf20Sopenharmony_ci			       const u8 *oobbuf, int start, int nbytes);
4308c2ecf20Sopenharmony_ciint mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
4318c2ecf20Sopenharmony_ci			       u8 *oobbuf, int start, int nbytes);
4328c2ecf20Sopenharmony_ciint mtd_ooblayout_free(struct mtd_info *mtd, int section,
4338c2ecf20Sopenharmony_ci		       struct mtd_oob_region *oobfree);
4348c2ecf20Sopenharmony_ciint mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
4358c2ecf20Sopenharmony_ci				const u8 *oobbuf, int start, int nbytes);
4368c2ecf20Sopenharmony_ciint mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
4378c2ecf20Sopenharmony_ci				u8 *oobbuf, int start, int nbytes);
4388c2ecf20Sopenharmony_ciint mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
4398c2ecf20Sopenharmony_ciint mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistatic inline void mtd_set_ooblayout(struct mtd_info *mtd,
4428c2ecf20Sopenharmony_ci				     const struct mtd_ooblayout_ops *ooblayout)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	mtd->ooblayout = ooblayout;
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic inline void mtd_set_pairing_scheme(struct mtd_info *mtd,
4488c2ecf20Sopenharmony_ci				const struct mtd_pairing_scheme *pairing)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	mtd->pairing = pairing;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic inline void mtd_set_of_node(struct mtd_info *mtd,
4548c2ecf20Sopenharmony_ci				   struct device_node *np)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	mtd->dev.of_node = np;
4578c2ecf20Sopenharmony_ci	if (!mtd->name)
4588c2ecf20Sopenharmony_ci		of_property_read_string(np, "label", &mtd->name);
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	return dev_of_node(&mtd->dev);
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic inline u32 mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_cistatic inline int mtd_max_bad_blocks(struct mtd_info *mtd,
4728c2ecf20Sopenharmony_ci				     loff_t ofs, size_t len)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	struct mtd_info *master = mtd_get_master(mtd);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	if (!master->_max_bad_blocks)
4778c2ecf20Sopenharmony_ci		return -ENOTSUPP;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	if (mtd->size < (len + ofs) || ofs < 0)
4808c2ecf20Sopenharmony_ci		return -EINVAL;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	return master->_max_bad_blocks(master, mtd_get_master_ofs(mtd, ofs),
4838c2ecf20Sopenharmony_ci				       len);
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ciint mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
4878c2ecf20Sopenharmony_ci			      struct mtd_pairing_info *info);
4888c2ecf20Sopenharmony_ciint mtd_pairing_info_to_wunit(struct mtd_info *mtd,
4898c2ecf20Sopenharmony_ci			      const struct mtd_pairing_info *info);
4908c2ecf20Sopenharmony_ciint mtd_pairing_groups(struct mtd_info *mtd);
4918c2ecf20Sopenharmony_ciint mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
4928c2ecf20Sopenharmony_ciint mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
4938c2ecf20Sopenharmony_ci	      void **virt, resource_size_t *phys);
4948c2ecf20Sopenharmony_ciint mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
4958c2ecf20Sopenharmony_ciunsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
4968c2ecf20Sopenharmony_ci				    unsigned long offset, unsigned long flags);
4978c2ecf20Sopenharmony_ciint mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
4988c2ecf20Sopenharmony_ci	     u_char *buf);
4998c2ecf20Sopenharmony_ciint mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
5008c2ecf20Sopenharmony_ci	      const u_char *buf);
5018c2ecf20Sopenharmony_ciint mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
5028c2ecf20Sopenharmony_ci		    const u_char *buf);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ciint mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
5058c2ecf20Sopenharmony_ciint mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ciint mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
5088c2ecf20Sopenharmony_ci			   struct otp_info *buf);
5098c2ecf20Sopenharmony_ciint mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
5108c2ecf20Sopenharmony_ci			   size_t *retlen, u_char *buf);
5118c2ecf20Sopenharmony_ciint mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
5128c2ecf20Sopenharmony_ci			   struct otp_info *buf);
5138c2ecf20Sopenharmony_ciint mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
5148c2ecf20Sopenharmony_ci			   size_t *retlen, u_char *buf);
5158c2ecf20Sopenharmony_ciint mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
5168c2ecf20Sopenharmony_ci			    size_t *retlen, u_char *buf);
5178c2ecf20Sopenharmony_ciint mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ciint mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
5208c2ecf20Sopenharmony_ci	       unsigned long count, loff_t to, size_t *retlen);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic inline void mtd_sync(struct mtd_info *mtd)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	struct mtd_info *master = mtd_get_master(mtd);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	if (master->_sync)
5278c2ecf20Sopenharmony_ci		master->_sync(master);
5288c2ecf20Sopenharmony_ci}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ciint mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
5318c2ecf20Sopenharmony_ciint mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
5328c2ecf20Sopenharmony_ciint mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
5338c2ecf20Sopenharmony_ciint mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
5348c2ecf20Sopenharmony_ciint mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
5358c2ecf20Sopenharmony_ciint mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_cistatic inline int mtd_suspend(struct mtd_info *mtd)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	struct mtd_info *master = mtd_get_master(mtd);
5408c2ecf20Sopenharmony_ci	int ret;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	if (master->master.suspended)
5438c2ecf20Sopenharmony_ci		return 0;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	ret = master->_suspend ? master->_suspend(master) : 0;
5468c2ecf20Sopenharmony_ci	if (ret)
5478c2ecf20Sopenharmony_ci		return ret;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	master->master.suspended = 1;
5508c2ecf20Sopenharmony_ci	return 0;
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_cistatic inline void mtd_resume(struct mtd_info *mtd)
5548c2ecf20Sopenharmony_ci{
5558c2ecf20Sopenharmony_ci	struct mtd_info *master = mtd_get_master(mtd);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	if (!master->master.suspended)
5588c2ecf20Sopenharmony_ci		return;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	if (master->_resume)
5618c2ecf20Sopenharmony_ci		master->_resume(master);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	master->master.suspended = 0;
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_cistatic inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
5678c2ecf20Sopenharmony_ci{
5688c2ecf20Sopenharmony_ci	if (mtd->erasesize_shift)
5698c2ecf20Sopenharmony_ci		return sz >> mtd->erasesize_shift;
5708c2ecf20Sopenharmony_ci	do_div(sz, mtd->erasesize);
5718c2ecf20Sopenharmony_ci	return sz;
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	if (mtd->erasesize_shift)
5778c2ecf20Sopenharmony_ci		return sz & mtd->erasesize_mask;
5788c2ecf20Sopenharmony_ci	return do_div(sz, mtd->erasesize);
5798c2ecf20Sopenharmony_ci}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci/**
5828c2ecf20Sopenharmony_ci * mtd_align_erase_req - Adjust an erase request to align things on eraseblock
5838c2ecf20Sopenharmony_ci *			 boundaries.
5848c2ecf20Sopenharmony_ci * @mtd: the MTD device this erase request applies on
5858c2ecf20Sopenharmony_ci * @req: the erase request to adjust
5868c2ecf20Sopenharmony_ci *
5878c2ecf20Sopenharmony_ci * This function will adjust @req->addr and @req->len to align them on
5888c2ecf20Sopenharmony_ci * @mtd->erasesize. Of course we expect @mtd->erasesize to be != 0.
5898c2ecf20Sopenharmony_ci */
5908c2ecf20Sopenharmony_cistatic inline void mtd_align_erase_req(struct mtd_info *mtd,
5918c2ecf20Sopenharmony_ci				       struct erase_info *req)
5928c2ecf20Sopenharmony_ci{
5938c2ecf20Sopenharmony_ci	u32 mod;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	if (WARN_ON(!mtd->erasesize))
5968c2ecf20Sopenharmony_ci		return;
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	mod = mtd_mod_by_eb(req->addr, mtd);
5998c2ecf20Sopenharmony_ci	if (mod) {
6008c2ecf20Sopenharmony_ci		req->addr -= mod;
6018c2ecf20Sopenharmony_ci		req->len += mod;
6028c2ecf20Sopenharmony_ci	}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	mod = mtd_mod_by_eb(req->addr + req->len, mtd);
6058c2ecf20Sopenharmony_ci	if (mod)
6068c2ecf20Sopenharmony_ci		req->len += mtd->erasesize - mod;
6078c2ecf20Sopenharmony_ci}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_cistatic inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	if (mtd->writesize_shift)
6128c2ecf20Sopenharmony_ci		return sz >> mtd->writesize_shift;
6138c2ecf20Sopenharmony_ci	do_div(sz, mtd->writesize);
6148c2ecf20Sopenharmony_ci	return sz;
6158c2ecf20Sopenharmony_ci}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_cistatic inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
6188c2ecf20Sopenharmony_ci{
6198c2ecf20Sopenharmony_ci	if (mtd->writesize_shift)
6208c2ecf20Sopenharmony_ci		return sz & mtd->writesize_mask;
6218c2ecf20Sopenharmony_ci	return do_div(sz, mtd->writesize);
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_cistatic inline int mtd_wunit_per_eb(struct mtd_info *mtd)
6258c2ecf20Sopenharmony_ci{
6268c2ecf20Sopenharmony_ci	struct mtd_info *master = mtd_get_master(mtd);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	return master->erasesize / mtd->writesize;
6298c2ecf20Sopenharmony_ci}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_cistatic inline int mtd_offset_to_wunit(struct mtd_info *mtd, loff_t offs)
6328c2ecf20Sopenharmony_ci{
6338c2ecf20Sopenharmony_ci	return mtd_div_by_ws(mtd_mod_by_eb(offs, mtd), mtd);
6348c2ecf20Sopenharmony_ci}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_cistatic inline loff_t mtd_wunit_to_offset(struct mtd_info *mtd, loff_t base,
6378c2ecf20Sopenharmony_ci					 int wunit)
6388c2ecf20Sopenharmony_ci{
6398c2ecf20Sopenharmony_ci	return base + (wunit * mtd->writesize);
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_cistatic inline int mtd_has_oob(const struct mtd_info *mtd)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	return master->_read_oob && master->_write_oob;
6488c2ecf20Sopenharmony_ci}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_cistatic inline int mtd_type_is_nand(const struct mtd_info *mtd)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_cistatic inline int mtd_can_have_bb(const struct mtd_info *mtd)
6568c2ecf20Sopenharmony_ci{
6578c2ecf20Sopenharmony_ci	struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	return !!master->_block_isbad;
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	/* Kernel-side ioctl definitions */
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistruct mtd_partition;
6658c2ecf20Sopenharmony_cistruct mtd_part_parser_data;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ciextern int mtd_device_parse_register(struct mtd_info *mtd,
6688c2ecf20Sopenharmony_ci				     const char * const *part_probe_types,
6698c2ecf20Sopenharmony_ci				     struct mtd_part_parser_data *parser_data,
6708c2ecf20Sopenharmony_ci				     const struct mtd_partition *defparts,
6718c2ecf20Sopenharmony_ci				     int defnr_parts);
6728c2ecf20Sopenharmony_ci#define mtd_device_register(master, parts, nr_parts)	\
6738c2ecf20Sopenharmony_ci	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
6748c2ecf20Sopenharmony_ciextern int mtd_device_unregister(struct mtd_info *master);
6758c2ecf20Sopenharmony_ciextern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
6768c2ecf20Sopenharmony_ciextern int __get_mtd_device(struct mtd_info *mtd);
6778c2ecf20Sopenharmony_ciextern void __put_mtd_device(struct mtd_info *mtd);
6788c2ecf20Sopenharmony_ciextern struct mtd_info *get_mtd_device_nm(const char *name);
6798c2ecf20Sopenharmony_ciextern void put_mtd_device(struct mtd_info *mtd);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_cistruct mtd_notifier {
6838c2ecf20Sopenharmony_ci	void (*add)(struct mtd_info *mtd);
6848c2ecf20Sopenharmony_ci	void (*remove)(struct mtd_info *mtd);
6858c2ecf20Sopenharmony_ci	struct list_head list;
6868c2ecf20Sopenharmony_ci};
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ciextern void register_mtd_user (struct mtd_notifier *new);
6908c2ecf20Sopenharmony_ciextern int unregister_mtd_user (struct mtd_notifier *old);
6918c2ecf20Sopenharmony_civoid *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_cistatic inline int mtd_is_bitflip(int err) {
6948c2ecf20Sopenharmony_ci	return err == -EUCLEAN;
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic inline int mtd_is_eccerr(int err) {
6988c2ecf20Sopenharmony_ci	return err == -EBADMSG;
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_cistatic inline int mtd_is_bitflip_or_eccerr(int err) {
7028c2ecf20Sopenharmony_ci	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
7038c2ecf20Sopenharmony_ci}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ciunsigned mtd_mmap_capabilities(struct mtd_info *mtd);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci#endif /* __MTD_MTD_H__ */
708