/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */1/*2* Copyright © 1999-2010 David Woodhouse <[email protected]> et al.3*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation; either version 2 of the License, or7* (at your option) any later version.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*14* You should have received a copy of the GNU General Public License15* along with this program; if not, write to the Free Software16* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA17*18*/1920#ifndef __MTD_ABI_H__21#define __MTD_ABI_H__2223#include <linux/types.h>2425struct erase_info_user {26__u32 start;27__u32 length;28};2930struct erase_info_user64 {31__u64 start;32__u64 length;33};3435struct mtd_oob_buf {36__u32 start;37__u32 length;38unsigned char __user *ptr;39};4041struct mtd_oob_buf64 {42__u64 start;43__u32 pad;44__u32 length;45__u64 usr_ptr;46};4748/**49* MTD operation modes50*51* @MTD_OPS_PLACE_OOB: OOB data are placed at the given offset (default)52* @MTD_OPS_AUTO_OOB: OOB data are automatically placed at the free areas53* which are defined by the internal ecclayout54* @MTD_OPS_RAW: data are transferred as-is, with no error correction;55* this mode implies %MTD_OPS_PLACE_OOB56*57* These modes can be passed to ioctl(MEMWRITE) and ioctl(MEMREAD); they are58* also used internally. See notes on "MTD file modes" for discussion on59* %MTD_OPS_RAW vs. %MTD_FILE_MODE_RAW.60*/61enum {62MTD_OPS_PLACE_OOB = 0,63MTD_OPS_AUTO_OOB = 1,64MTD_OPS_RAW = 2,65};6667/**68* struct mtd_write_req - data structure for requesting a write operation69*70* @start: start address71* @len: length of data buffer (only lower 32 bits are used)72* @ooblen: length of OOB buffer (only lower 32 bits are used)73* @usr_data: user-provided data buffer74* @usr_oob: user-provided OOB buffer75* @mode: MTD mode (see "MTD operation modes")76* @padding: reserved, must be set to 077*78* This structure supports ioctl(MEMWRITE) operations, allowing data and/or OOB79* writes in various modes. To write to OOB-only, set @usr_data == NULL, and to80* write data-only, set @usr_oob == NULL. However, setting both @usr_data and81* @usr_oob to NULL is not allowed.82*/83struct mtd_write_req {84__u64 start;85__u64 len;86__u64 ooblen;87__u64 usr_data;88__u64 usr_oob;89__u8 mode;90__u8 padding[7];91};9293/**94* struct mtd_read_req_ecc_stats - ECC statistics for a read operation95*96* @uncorrectable_errors: the number of uncorrectable errors that happened97* during the read operation98* @corrected_bitflips: the number of bitflips corrected during the read99* operation100* @max_bitflips: the maximum number of bitflips detected in any single ECC101* step for the data read during the operation; this information102* can be used to decide whether the data stored in a specific103* region of the MTD device should be moved somewhere else to104* avoid data loss.105*/106struct mtd_read_req_ecc_stats {107__u32 uncorrectable_errors;108__u32 corrected_bitflips;109__u32 max_bitflips;110};111112/**113* struct mtd_read_req - data structure for requesting a read operation114*115* @start: start address116* @len: length of data buffer (only lower 32 bits are used)117* @ooblen: length of OOB buffer (only lower 32 bits are used)118* @usr_data: user-provided data buffer119* @usr_oob: user-provided OOB buffer120* @mode: MTD mode (see "MTD operation modes")121* @padding: reserved, must be set to 0122* @ecc_stats: ECC statistics for the read operation123*124* This structure supports ioctl(MEMREAD) operations, allowing data and/or OOB125* reads in various modes. To read from OOB-only, set @usr_data == NULL, and to126* read data-only, set @usr_oob == NULL. However, setting both @usr_data and127* @usr_oob to NULL is not allowed.128*/129struct mtd_read_req {130__u64 start;131__u64 len;132__u64 ooblen;133__u64 usr_data;134__u64 usr_oob;135__u8 mode;136__u8 padding[7];137struct mtd_read_req_ecc_stats ecc_stats;138};139140#define MTD_ABSENT 0141#define MTD_RAM 1142#define MTD_ROM 2143#define MTD_NORFLASH 3144#define MTD_NANDFLASH 4 /* SLC NAND */145#define MTD_DATAFLASH 6146#define MTD_UBIVOLUME 7147#define MTD_MLCNANDFLASH 8 /* MLC NAND (including TLC) */148149#define MTD_WRITEABLE 0x400 /* Device is writeable */150#define MTD_BIT_WRITEABLE 0x800 /* Single bits can be flipped */151#define MTD_NO_ERASE 0x1000 /* No erase necessary */152#define MTD_POWERUP_LOCK 0x2000 /* Always locked after reset */153#define MTD_SLC_ON_MLC_EMULATION 0x4000 /* Emulate SLC behavior on MLC NANDs */154155/* Some common devices / combinations of capabilities */156#define MTD_CAP_ROM 0157#define MTD_CAP_RAM (MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE)158#define MTD_CAP_NORFLASH (MTD_WRITEABLE | MTD_BIT_WRITEABLE)159#define MTD_CAP_NANDFLASH (MTD_WRITEABLE)160#define MTD_CAP_NVRAM (MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE)161162/* Obsolete ECC byte placement modes (used with obsolete MEMGETOOBSEL) */163#define MTD_NANDECC_OFF 0 /* Switch off ECC (Not recommended) */164#define MTD_NANDECC_PLACE 1 /* Use the given placement in the structure (YAFFS1 legacy mode) */165#define MTD_NANDECC_AUTOPLACE 2 /* Use the default placement scheme */166#define MTD_NANDECC_PLACEONLY 3 /* Use the given placement in the structure (Do not store ecc result on read) */167#define MTD_NANDECC_AUTOPL_USR 4 /* Use the given autoplacement scheme rather than using the default */168169/* OTP mode selection */170#define MTD_OTP_OFF 0171#define MTD_OTP_FACTORY 1172#define MTD_OTP_USER 2173174struct mtd_info_user {175__u8 type;176__u32 flags;177__u32 size; /* Total size of the MTD */178__u32 erasesize;179__u32 writesize;180__u32 oobsize; /* Amount of OOB data per block (e.g. 16) */181__u64 padding; /* Old obsolete field; do not use */182};183184struct region_info_user {185__u32 offset; /* At which this region starts,186* from the beginning of the MTD */187__u32 erasesize; /* For this region */188__u32 numblocks; /* Number of blocks in this region */189__u32 regionindex;190};191192struct otp_info {193__u32 start;194__u32 length;195__u32 locked;196};197198/*199* Note, the following ioctl existed in the past and was removed:200* #define MEMSETOOBSEL _IOW('M', 9, struct nand_oobinfo)201* Try to avoid adding a new ioctl with the same ioctl number.202*/203204/* Get basic MTD characteristics info (better to use sysfs) */205#define MEMGETINFO _IOR('M', 1, struct mtd_info_user)206/* Erase segment of MTD */207#define MEMERASE _IOW('M', 2, struct erase_info_user)208/* Write out-of-band data from MTD */209#define MEMWRITEOOB _IOWR('M', 3, struct mtd_oob_buf)210/* Read out-of-band data from MTD */211#define MEMREADOOB _IOWR('M', 4, struct mtd_oob_buf)212/* Lock a chip (for MTD that supports it) */213#define MEMLOCK _IOW('M', 5, struct erase_info_user)214/* Unlock a chip (for MTD that supports it) */215#define MEMUNLOCK _IOW('M', 6, struct erase_info_user)216/* Get the number of different erase regions */217#define MEMGETREGIONCOUNT _IOR('M', 7, int)218/* Get information about the erase region for a specific index */219#define MEMGETREGIONINFO _IOWR('M', 8, struct region_info_user)220/* Get info about OOB modes (e.g., RAW, PLACE, AUTO) - legacy interface */221#define MEMGETOOBSEL _IOR('M', 10, struct nand_oobinfo)222/* Check if an eraseblock is bad */223#define MEMGETBADBLOCK _IOW('M', 11, __kernel_loff_t)224/* Mark an eraseblock as bad */225#define MEMSETBADBLOCK _IOW('M', 12, __kernel_loff_t)226/* Set OTP (One-Time Programmable) mode (factory vs. user) */227#define OTPSELECT _IOR('M', 13, int)228/* Get number of OTP (One-Time Programmable) regions */229#define OTPGETREGIONCOUNT _IOW('M', 14, int)230/* Get all OTP (One-Time Programmable) info about MTD */231#define OTPGETREGIONINFO _IOW('M', 15, struct otp_info)232/* Lock a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */233#define OTPLOCK _IOR('M', 16, struct otp_info)234/* Get ECC layout (deprecated) */235#define ECCGETLAYOUT _IOR('M', 17, struct nand_ecclayout_user)236/* Get statistics about corrected/uncorrected errors */237#define ECCGETSTATS _IOR('M', 18, struct mtd_ecc_stats)238/* Set MTD mode on a per-file-descriptor basis (see "MTD file modes") */239#define MTDFILEMODE _IO('M', 19)240/* Erase segment of MTD (supports 64-bit address) */241#define MEMERASE64 _IOW('M', 20, struct erase_info_user64)242/* Write data to OOB (64-bit version) */243#define MEMWRITEOOB64 _IOWR('M', 21, struct mtd_oob_buf64)244/* Read data from OOB (64-bit version) */245#define MEMREADOOB64 _IOWR('M', 22, struct mtd_oob_buf64)246/* Check if chip is locked (for MTD that supports it) */247#define MEMISLOCKED _IOR('M', 23, struct erase_info_user)248/*249* Most generic write interface; can write in-band and/or out-of-band in various250* modes (see "struct mtd_write_req"). This ioctl is not supported for flashes251* without OOB, e.g., NOR flash.252*/253#define MEMWRITE _IOWR('M', 24, struct mtd_write_req)254/* Erase a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */255#define OTPERASE _IOW('M', 25, struct otp_info)256/*257* Most generic read interface; can read in-band and/or out-of-band in various258* modes (see "struct mtd_read_req"). This ioctl is not supported for flashes259* without OOB, e.g., NOR flash.260*/261#define MEMREAD _IOWR('M', 26, struct mtd_read_req)262263/*264* Obsolete legacy interface. Keep it in order not to break userspace265* interfaces266*/267struct nand_oobinfo {268__u32 useecc;269__u32 eccbytes;270__u32 oobfree[8][2];271__u32 eccpos[32];272};273274struct nand_oobfree {275__u32 offset;276__u32 length;277};278279#define MTD_MAX_OOBFREE_ENTRIES 8280#define MTD_MAX_ECCPOS_ENTRIES 64281/*282* OBSOLETE: ECC layout control structure. Exported to user-space via ioctl283* ECCGETLAYOUT for backwards compatbility and should not be mistaken as a284* complete set of ECC information. The ioctl truncates the larger internal285* structure to retain binary compatibility with the static declaration of the286* ioctl. Note that the "MTD_MAX_..._ENTRIES" macros represent the max size of287* the user struct, not the MAX size of the internal OOB layout representation.288*/289struct nand_ecclayout_user {290__u32 eccbytes;291__u32 eccpos[MTD_MAX_ECCPOS_ENTRIES];292__u32 oobavail;293struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES];294};295296/**297* struct mtd_ecc_stats - error correction stats298*299* @corrected: number of corrected bits300* @failed: number of uncorrectable errors301* @badblocks: number of bad blocks in this partition302* @bbtblocks: number of blocks reserved for bad block tables303*/304struct mtd_ecc_stats {305__u32 corrected;306__u32 failed;307__u32 badblocks;308__u32 bbtblocks;309};310311/*312* MTD file modes - for read/write access to MTD313*314* @MTD_FILE_MODE_NORMAL: OTP disabled, ECC enabled315* @MTD_FILE_MODE_OTP_FACTORY: OTP enabled in factory mode316* @MTD_FILE_MODE_OTP_USER: OTP enabled in user mode317* @MTD_FILE_MODE_RAW: OTP disabled, ECC disabled318*319* These modes can be set via ioctl(MTDFILEMODE). The mode will be retained320* separately for each open file descriptor.321*322* Note: %MTD_FILE_MODE_RAW provides the same functionality as %MTD_OPS_RAW -323* raw access to the flash, without error correction or autoplacement schemes.324* Wherever possible, the MTD_OPS_* mode will override the MTD_FILE_MODE_* mode325* (e.g., when using ioctl(MEMWRITE) or ioctl(MEMREAD)), but in some cases, the326* MTD_FILE_MODE is used out of necessity (e.g., `write()',327* ioctl(MEMWRITEOOB64)).328*/329enum mtd_file_modes {330MTD_FILE_MODE_NORMAL = MTD_OTP_OFF,331MTD_FILE_MODE_OTP_FACTORY = MTD_OTP_FACTORY,332MTD_FILE_MODE_OTP_USER = MTD_OTP_USER,333MTD_FILE_MODE_RAW,334};335336static inline int mtd_type_is_nand_user(const struct mtd_info_user *mtd)337{338return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;339}340341#endif /* __MTD_ABI_H__ */342343344