18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
68c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License as published by
78c2ecf20Sopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
88c2ecf20Sopenharmony_ci * (at your option) any later version.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful,
118c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
138c2ecf20Sopenharmony_ci * GNU General Public License for more details.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
168c2ecf20Sopenharmony_ci * along with this program; if not, write to the Free Software
178c2ecf20Sopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#ifndef __MTD_ABI_H__
228c2ecf20Sopenharmony_ci#define __MTD_ABI_H__
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <linux/types.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct erase_info_user {
278c2ecf20Sopenharmony_ci	__u32 start;
288c2ecf20Sopenharmony_ci	__u32 length;
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistruct erase_info_user64 {
328c2ecf20Sopenharmony_ci	__u64 start;
338c2ecf20Sopenharmony_ci	__u64 length;
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct mtd_oob_buf {
378c2ecf20Sopenharmony_ci	__u32 start;
388c2ecf20Sopenharmony_ci	__u32 length;
398c2ecf20Sopenharmony_ci	unsigned char __user *ptr;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistruct mtd_oob_buf64 {
438c2ecf20Sopenharmony_ci	__u64 start;
448c2ecf20Sopenharmony_ci	__u32 pad;
458c2ecf20Sopenharmony_ci	__u32 length;
468c2ecf20Sopenharmony_ci	__u64 usr_ptr;
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/**
508c2ecf20Sopenharmony_ci * MTD operation modes
518c2ecf20Sopenharmony_ci *
528c2ecf20Sopenharmony_ci * @MTD_OPS_PLACE_OOB:	OOB data are placed at the given offset (default)
538c2ecf20Sopenharmony_ci * @MTD_OPS_AUTO_OOB:	OOB data are automatically placed at the free areas
548c2ecf20Sopenharmony_ci *			which are defined by the internal ecclayout
558c2ecf20Sopenharmony_ci * @MTD_OPS_RAW:	data are transferred as-is, with no error correction;
568c2ecf20Sopenharmony_ci *			this mode implies %MTD_OPS_PLACE_OOB
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * These modes can be passed to ioctl(MEMWRITE) and are also used internally.
598c2ecf20Sopenharmony_ci * See notes on "MTD file modes" for discussion on %MTD_OPS_RAW vs.
608c2ecf20Sopenharmony_ci * %MTD_FILE_MODE_RAW.
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_cienum {
638c2ecf20Sopenharmony_ci	MTD_OPS_PLACE_OOB = 0,
648c2ecf20Sopenharmony_ci	MTD_OPS_AUTO_OOB = 1,
658c2ecf20Sopenharmony_ci	MTD_OPS_RAW = 2,
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/**
698c2ecf20Sopenharmony_ci * struct mtd_write_req - data structure for requesting a write operation
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * @start:	start address
728c2ecf20Sopenharmony_ci * @len:	length of data buffer
738c2ecf20Sopenharmony_ci * @ooblen:	length of OOB buffer
748c2ecf20Sopenharmony_ci * @usr_data:	user-provided data buffer
758c2ecf20Sopenharmony_ci * @usr_oob:	user-provided OOB buffer
768c2ecf20Sopenharmony_ci * @mode:	MTD mode (see "MTD operation modes")
778c2ecf20Sopenharmony_ci * @padding:	reserved, must be set to 0
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * This structure supports ioctl(MEMWRITE) operations, allowing data and/or OOB
808c2ecf20Sopenharmony_ci * writes in various modes. To write to OOB-only, set @usr_data == NULL, and to
818c2ecf20Sopenharmony_ci * write data-only, set @usr_oob == NULL. However, setting both @usr_data and
828c2ecf20Sopenharmony_ci * @usr_oob to NULL is not allowed.
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_cistruct mtd_write_req {
858c2ecf20Sopenharmony_ci	__u64 start;
868c2ecf20Sopenharmony_ci	__u64 len;
878c2ecf20Sopenharmony_ci	__u64 ooblen;
888c2ecf20Sopenharmony_ci	__u64 usr_data;
898c2ecf20Sopenharmony_ci	__u64 usr_oob;
908c2ecf20Sopenharmony_ci	__u8 mode;
918c2ecf20Sopenharmony_ci	__u8 padding[7];
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define MTD_ABSENT		0
958c2ecf20Sopenharmony_ci#define MTD_RAM			1
968c2ecf20Sopenharmony_ci#define MTD_ROM			2
978c2ecf20Sopenharmony_ci#define MTD_NORFLASH		3
988c2ecf20Sopenharmony_ci#define MTD_NANDFLASH		4	/* SLC NAND */
998c2ecf20Sopenharmony_ci#define MTD_DATAFLASH		6
1008c2ecf20Sopenharmony_ci#define MTD_UBIVOLUME		7
1018c2ecf20Sopenharmony_ci#define MTD_MLCNANDFLASH	8	/* MLC NAND (including TLC) */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define MTD_WRITEABLE		0x400	/* Device is writeable */
1048c2ecf20Sopenharmony_ci#define MTD_BIT_WRITEABLE	0x800	/* Single bits can be flipped */
1058c2ecf20Sopenharmony_ci#define MTD_NO_ERASE		0x1000	/* No erase necessary */
1068c2ecf20Sopenharmony_ci#define MTD_POWERUP_LOCK	0x2000	/* Always locked after reset */
1078c2ecf20Sopenharmony_ci#define MTD_SLC_ON_MLC_EMULATION 0x4000	/* Emulate SLC behavior on MLC NANDs */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* Some common devices / combinations of capabilities */
1108c2ecf20Sopenharmony_ci#define MTD_CAP_ROM		0
1118c2ecf20Sopenharmony_ci#define MTD_CAP_RAM		(MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE)
1128c2ecf20Sopenharmony_ci#define MTD_CAP_NORFLASH	(MTD_WRITEABLE | MTD_BIT_WRITEABLE)
1138c2ecf20Sopenharmony_ci#define MTD_CAP_NANDFLASH	(MTD_WRITEABLE)
1148c2ecf20Sopenharmony_ci#define MTD_CAP_NVRAM		(MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/* Obsolete ECC byte placement modes (used with obsolete MEMGETOOBSEL) */
1178c2ecf20Sopenharmony_ci#define MTD_NANDECC_OFF		0	/* Switch off ECC (Not recommended) */
1188c2ecf20Sopenharmony_ci#define MTD_NANDECC_PLACE	1	/* Use the given placement in the structure (YAFFS1 legacy mode) */
1198c2ecf20Sopenharmony_ci#define MTD_NANDECC_AUTOPLACE	2	/* Use the default placement scheme */
1208c2ecf20Sopenharmony_ci#define MTD_NANDECC_PLACEONLY	3	/* Use the given placement in the structure (Do not store ecc result on read) */
1218c2ecf20Sopenharmony_ci#define MTD_NANDECC_AUTOPL_USR 	4	/* Use the given autoplacement scheme rather than using the default */
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* OTP mode selection */
1248c2ecf20Sopenharmony_ci#define MTD_OTP_OFF		0
1258c2ecf20Sopenharmony_ci#define MTD_OTP_FACTORY		1
1268c2ecf20Sopenharmony_ci#define MTD_OTP_USER		2
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistruct mtd_info_user {
1298c2ecf20Sopenharmony_ci	__u8 type;
1308c2ecf20Sopenharmony_ci	__u32 flags;
1318c2ecf20Sopenharmony_ci	__u32 size;	/* Total size of the MTD */
1328c2ecf20Sopenharmony_ci	__u32 erasesize;
1338c2ecf20Sopenharmony_ci	__u32 writesize;
1348c2ecf20Sopenharmony_ci	__u32 oobsize;	/* Amount of OOB data per block (e.g. 16) */
1358c2ecf20Sopenharmony_ci	__u64 padding;	/* Old obsolete field; do not use */
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistruct region_info_user {
1398c2ecf20Sopenharmony_ci	__u32 offset;		/* At which this region starts,
1408c2ecf20Sopenharmony_ci				 * from the beginning of the MTD */
1418c2ecf20Sopenharmony_ci	__u32 erasesize;	/* For this region */
1428c2ecf20Sopenharmony_ci	__u32 numblocks;	/* Number of blocks in this region */
1438c2ecf20Sopenharmony_ci	__u32 regionindex;
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistruct otp_info {
1478c2ecf20Sopenharmony_ci	__u32 start;
1488c2ecf20Sopenharmony_ci	__u32 length;
1498c2ecf20Sopenharmony_ci	__u32 locked;
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci * Note, the following ioctl existed in the past and was removed:
1548c2ecf20Sopenharmony_ci * #define MEMSETOOBSEL           _IOW('M', 9, struct nand_oobinfo)
1558c2ecf20Sopenharmony_ci * Try to avoid adding a new ioctl with the same ioctl number.
1568c2ecf20Sopenharmony_ci */
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/* Get basic MTD characteristics info (better to use sysfs) */
1598c2ecf20Sopenharmony_ci#define MEMGETINFO		_IOR('M', 1, struct mtd_info_user)
1608c2ecf20Sopenharmony_ci/* Erase segment of MTD */
1618c2ecf20Sopenharmony_ci#define MEMERASE		_IOW('M', 2, struct erase_info_user)
1628c2ecf20Sopenharmony_ci/* Write out-of-band data from MTD */
1638c2ecf20Sopenharmony_ci#define MEMWRITEOOB		_IOWR('M', 3, struct mtd_oob_buf)
1648c2ecf20Sopenharmony_ci/* Read out-of-band data from MTD */
1658c2ecf20Sopenharmony_ci#define MEMREADOOB		_IOWR('M', 4, struct mtd_oob_buf)
1668c2ecf20Sopenharmony_ci/* Lock a chip (for MTD that supports it) */
1678c2ecf20Sopenharmony_ci#define MEMLOCK			_IOW('M', 5, struct erase_info_user)
1688c2ecf20Sopenharmony_ci/* Unlock a chip (for MTD that supports it) */
1698c2ecf20Sopenharmony_ci#define MEMUNLOCK		_IOW('M', 6, struct erase_info_user)
1708c2ecf20Sopenharmony_ci/* Get the number of different erase regions */
1718c2ecf20Sopenharmony_ci#define MEMGETREGIONCOUNT	_IOR('M', 7, int)
1728c2ecf20Sopenharmony_ci/* Get information about the erase region for a specific index */
1738c2ecf20Sopenharmony_ci#define MEMGETREGIONINFO	_IOWR('M', 8, struct region_info_user)
1748c2ecf20Sopenharmony_ci/* Get info about OOB modes (e.g., RAW, PLACE, AUTO) - legacy interface */
1758c2ecf20Sopenharmony_ci#define MEMGETOOBSEL		_IOR('M', 10, struct nand_oobinfo)
1768c2ecf20Sopenharmony_ci/* Check if an eraseblock is bad */
1778c2ecf20Sopenharmony_ci#define MEMGETBADBLOCK		_IOW('M', 11, __kernel_loff_t)
1788c2ecf20Sopenharmony_ci/* Mark an eraseblock as bad */
1798c2ecf20Sopenharmony_ci#define MEMSETBADBLOCK		_IOW('M', 12, __kernel_loff_t)
1808c2ecf20Sopenharmony_ci/* Set OTP (One-Time Programmable) mode (factory vs. user) */
1818c2ecf20Sopenharmony_ci#define OTPSELECT		_IOR('M', 13, int)
1828c2ecf20Sopenharmony_ci/* Get number of OTP (One-Time Programmable) regions */
1838c2ecf20Sopenharmony_ci#define OTPGETREGIONCOUNT	_IOW('M', 14, int)
1848c2ecf20Sopenharmony_ci/* Get all OTP (One-Time Programmable) info about MTD */
1858c2ecf20Sopenharmony_ci#define OTPGETREGIONINFO	_IOW('M', 15, struct otp_info)
1868c2ecf20Sopenharmony_ci/* Lock a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */
1878c2ecf20Sopenharmony_ci#define OTPLOCK			_IOR('M', 16, struct otp_info)
1888c2ecf20Sopenharmony_ci/* Get ECC layout (deprecated) */
1898c2ecf20Sopenharmony_ci#define ECCGETLAYOUT		_IOR('M', 17, struct nand_ecclayout_user)
1908c2ecf20Sopenharmony_ci/* Get statistics about corrected/uncorrected errors */
1918c2ecf20Sopenharmony_ci#define ECCGETSTATS		_IOR('M', 18, struct mtd_ecc_stats)
1928c2ecf20Sopenharmony_ci/* Set MTD mode on a per-file-descriptor basis (see "MTD file modes") */
1938c2ecf20Sopenharmony_ci#define MTDFILEMODE		_IO('M', 19)
1948c2ecf20Sopenharmony_ci/* Erase segment of MTD (supports 64-bit address) */
1958c2ecf20Sopenharmony_ci#define MEMERASE64		_IOW('M', 20, struct erase_info_user64)
1968c2ecf20Sopenharmony_ci/* Write data to OOB (64-bit version) */
1978c2ecf20Sopenharmony_ci#define MEMWRITEOOB64		_IOWR('M', 21, struct mtd_oob_buf64)
1988c2ecf20Sopenharmony_ci/* Read data from OOB (64-bit version) */
1998c2ecf20Sopenharmony_ci#define MEMREADOOB64		_IOWR('M', 22, struct mtd_oob_buf64)
2008c2ecf20Sopenharmony_ci/* Check if chip is locked (for MTD that supports it) */
2018c2ecf20Sopenharmony_ci#define MEMISLOCKED		_IOR('M', 23, struct erase_info_user)
2028c2ecf20Sopenharmony_ci/*
2038c2ecf20Sopenharmony_ci * Most generic write interface; can write in-band and/or out-of-band in various
2048c2ecf20Sopenharmony_ci * modes (see "struct mtd_write_req"). This ioctl is not supported for flashes
2058c2ecf20Sopenharmony_ci * without OOB, e.g., NOR flash.
2068c2ecf20Sopenharmony_ci */
2078c2ecf20Sopenharmony_ci#define MEMWRITE		_IOWR('M', 24, struct mtd_write_req)
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci/*
2108c2ecf20Sopenharmony_ci * Obsolete legacy interface. Keep it in order not to break userspace
2118c2ecf20Sopenharmony_ci * interfaces
2128c2ecf20Sopenharmony_ci */
2138c2ecf20Sopenharmony_cistruct nand_oobinfo {
2148c2ecf20Sopenharmony_ci	__u32 useecc;
2158c2ecf20Sopenharmony_ci	__u32 eccbytes;
2168c2ecf20Sopenharmony_ci	__u32 oobfree[8][2];
2178c2ecf20Sopenharmony_ci	__u32 eccpos[32];
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistruct nand_oobfree {
2218c2ecf20Sopenharmony_ci	__u32 offset;
2228c2ecf20Sopenharmony_ci	__u32 length;
2238c2ecf20Sopenharmony_ci};
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci#define MTD_MAX_OOBFREE_ENTRIES	8
2268c2ecf20Sopenharmony_ci#define MTD_MAX_ECCPOS_ENTRIES	64
2278c2ecf20Sopenharmony_ci/*
2288c2ecf20Sopenharmony_ci * OBSOLETE: ECC layout control structure. Exported to user-space via ioctl
2298c2ecf20Sopenharmony_ci * ECCGETLAYOUT for backwards compatbility and should not be mistaken as a
2308c2ecf20Sopenharmony_ci * complete set of ECC information. The ioctl truncates the larger internal
2318c2ecf20Sopenharmony_ci * structure to retain binary compatibility with the static declaration of the
2328c2ecf20Sopenharmony_ci * ioctl. Note that the "MTD_MAX_..._ENTRIES" macros represent the max size of
2338c2ecf20Sopenharmony_ci * the user struct, not the MAX size of the internal OOB layout representation.
2348c2ecf20Sopenharmony_ci */
2358c2ecf20Sopenharmony_cistruct nand_ecclayout_user {
2368c2ecf20Sopenharmony_ci	__u32 eccbytes;
2378c2ecf20Sopenharmony_ci	__u32 eccpos[MTD_MAX_ECCPOS_ENTRIES];
2388c2ecf20Sopenharmony_ci	__u32 oobavail;
2398c2ecf20Sopenharmony_ci	struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES];
2408c2ecf20Sopenharmony_ci};
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci/**
2438c2ecf20Sopenharmony_ci * struct mtd_ecc_stats - error correction stats
2448c2ecf20Sopenharmony_ci *
2458c2ecf20Sopenharmony_ci * @corrected:	number of corrected bits
2468c2ecf20Sopenharmony_ci * @failed:	number of uncorrectable errors
2478c2ecf20Sopenharmony_ci * @badblocks:	number of bad blocks in this partition
2488c2ecf20Sopenharmony_ci * @bbtblocks:	number of blocks reserved for bad block tables
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_cistruct mtd_ecc_stats {
2518c2ecf20Sopenharmony_ci	__u32 corrected;
2528c2ecf20Sopenharmony_ci	__u32 failed;
2538c2ecf20Sopenharmony_ci	__u32 badblocks;
2548c2ecf20Sopenharmony_ci	__u32 bbtblocks;
2558c2ecf20Sopenharmony_ci};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci/*
2588c2ecf20Sopenharmony_ci * MTD file modes - for read/write access to MTD
2598c2ecf20Sopenharmony_ci *
2608c2ecf20Sopenharmony_ci * @MTD_FILE_MODE_NORMAL:	OTP disabled, ECC enabled
2618c2ecf20Sopenharmony_ci * @MTD_FILE_MODE_OTP_FACTORY:	OTP enabled in factory mode
2628c2ecf20Sopenharmony_ci * @MTD_FILE_MODE_OTP_USER:	OTP enabled in user mode
2638c2ecf20Sopenharmony_ci * @MTD_FILE_MODE_RAW:		OTP disabled, ECC disabled
2648c2ecf20Sopenharmony_ci *
2658c2ecf20Sopenharmony_ci * These modes can be set via ioctl(MTDFILEMODE). The mode will be retained
2668c2ecf20Sopenharmony_ci * separately for each open file descriptor.
2678c2ecf20Sopenharmony_ci *
2688c2ecf20Sopenharmony_ci * Note: %MTD_FILE_MODE_RAW provides the same functionality as %MTD_OPS_RAW -
2698c2ecf20Sopenharmony_ci * raw access to the flash, without error correction or autoplacement schemes.
2708c2ecf20Sopenharmony_ci * Wherever possible, the MTD_OPS_* mode will override the MTD_FILE_MODE_* mode
2718c2ecf20Sopenharmony_ci * (e.g., when using ioctl(MEMWRITE)), but in some cases, the MTD_FILE_MODE is
2728c2ecf20Sopenharmony_ci * used out of necessity (e.g., `write()', ioctl(MEMWRITEOOB64)).
2738c2ecf20Sopenharmony_ci */
2748c2ecf20Sopenharmony_cienum mtd_file_modes {
2758c2ecf20Sopenharmony_ci	MTD_FILE_MODE_NORMAL = MTD_OTP_OFF,
2768c2ecf20Sopenharmony_ci	MTD_FILE_MODE_OTP_FACTORY = MTD_OTP_FACTORY,
2778c2ecf20Sopenharmony_ci	MTD_FILE_MODE_OTP_USER = MTD_OTP_USER,
2788c2ecf20Sopenharmony_ci	MTD_FILE_MODE_RAW,
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic inline int mtd_type_is_nand_user(const struct mtd_info_user *mtd)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci#endif /* __MTD_ABI_H__ */
287