xref: /OK3568_Linux_fs/u-boot/include/mtd/mtd-abi.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * SPDX-License-Identifier:    GPL-2.0+
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef __MTD_ABI_H__
9*4882a593Smuzhiyun #define __MTD_ABI_H__
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #ifdef __UBOOT__
12*4882a593Smuzhiyun #include <linux/compat.h>
13*4882a593Smuzhiyun #endif
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/compiler.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun struct erase_info_user {
18*4882a593Smuzhiyun 	__u32 start;
19*4882a593Smuzhiyun 	__u32 length;
20*4882a593Smuzhiyun };
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun struct erase_info_user64 {
23*4882a593Smuzhiyun 	__u64 start;
24*4882a593Smuzhiyun 	__u64 length;
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct mtd_oob_buf {
28*4882a593Smuzhiyun 	__u32 start;
29*4882a593Smuzhiyun 	__u32 length;
30*4882a593Smuzhiyun 	unsigned char __user *ptr;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun struct mtd_oob_buf64 {
34*4882a593Smuzhiyun 	__u64 start;
35*4882a593Smuzhiyun 	__u32 pad;
36*4882a593Smuzhiyun 	__u32 length;
37*4882a593Smuzhiyun 	__u64 usr_ptr;
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun  * MTD operation modes
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * @MTD_OPS_PLACE_OOB:	OOB data are placed at the given offset (default)
44*4882a593Smuzhiyun  * @MTD_OPS_AUTO_OOB:	OOB data are automatically placed at the free areas
45*4882a593Smuzhiyun  *			which are defined by the internal ecclayout
46*4882a593Smuzhiyun  * @MTD_OPS_RAW:	data are transferred as-is, with no error correction;
47*4882a593Smuzhiyun  *			this mode implies %MTD_OPS_PLACE_OOB
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * These modes can be passed to ioctl(MEMWRITE) and are also used internally.
50*4882a593Smuzhiyun  * See notes on "MTD file modes" for discussion on %MTD_OPS_RAW vs.
51*4882a593Smuzhiyun  * %MTD_FILE_MODE_RAW.
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun enum {
54*4882a593Smuzhiyun 	MTD_OPS_PLACE_OOB = 0,
55*4882a593Smuzhiyun 	MTD_OPS_AUTO_OOB = 1,
56*4882a593Smuzhiyun 	MTD_OPS_RAW = 2,
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /**
60*4882a593Smuzhiyun  * struct mtd_write_req - data structure for requesting a write operation
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * @start:	start address
63*4882a593Smuzhiyun  * @len:	length of data buffer
64*4882a593Smuzhiyun  * @ooblen:	length of OOB buffer
65*4882a593Smuzhiyun  * @usr_data:	user-provided data buffer
66*4882a593Smuzhiyun  * @usr_oob:	user-provided OOB buffer
67*4882a593Smuzhiyun  * @mode:	MTD mode (see "MTD operation modes")
68*4882a593Smuzhiyun  * @padding:	reserved, must be set to 0
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * This structure supports ioctl(MEMWRITE) operations, allowing data and/or OOB
71*4882a593Smuzhiyun  * writes in various modes. To write to OOB-only, set @usr_data == NULL, and to
72*4882a593Smuzhiyun  * write data-only, set @usr_oob == NULL. However, setting both @usr_data and
73*4882a593Smuzhiyun  * @usr_oob to NULL is not allowed.
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun struct mtd_write_req {
76*4882a593Smuzhiyun 	__u64 start;
77*4882a593Smuzhiyun 	__u64 len;
78*4882a593Smuzhiyun 	__u64 ooblen;
79*4882a593Smuzhiyun 	__u64 usr_data;
80*4882a593Smuzhiyun 	__u64 usr_oob;
81*4882a593Smuzhiyun 	__u8 mode;
82*4882a593Smuzhiyun 	__u8 padding[7];
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define MTD_ABSENT		0
86*4882a593Smuzhiyun #define MTD_RAM			1
87*4882a593Smuzhiyun #define MTD_ROM			2
88*4882a593Smuzhiyun #define MTD_NORFLASH		3
89*4882a593Smuzhiyun #define MTD_NANDFLASH		4	/* SLC NAND */
90*4882a593Smuzhiyun #define MTD_DATAFLASH		6
91*4882a593Smuzhiyun #define MTD_UBIVOLUME		7
92*4882a593Smuzhiyun #define MTD_MLCNANDFLASH	8	/* MLC NAND (including TLC) */
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #define MTD_WRITEABLE		0x400	/* Device is writeable */
95*4882a593Smuzhiyun #define MTD_BIT_WRITEABLE	0x800	/* Single bits can be flipped */
96*4882a593Smuzhiyun #define MTD_NO_ERASE		0x1000	/* No erase necessary */
97*4882a593Smuzhiyun #define MTD_POWERUP_LOCK	0x2000	/* Always locked after reset */
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /* Some common devices / combinations of capabilities */
100*4882a593Smuzhiyun #define MTD_CAP_ROM		0
101*4882a593Smuzhiyun #define MTD_CAP_RAM		(MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE)
102*4882a593Smuzhiyun #define MTD_CAP_NORFLASH	(MTD_WRITEABLE | MTD_BIT_WRITEABLE)
103*4882a593Smuzhiyun #define MTD_CAP_NANDFLASH	(MTD_WRITEABLE)
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /* Obsolete ECC byte placement modes (used with obsolete MEMGETOOBSEL) */
106*4882a593Smuzhiyun #define MTD_NANDECC_OFF		0	// Switch off ECC (Not recommended)
107*4882a593Smuzhiyun #define MTD_NANDECC_PLACE	1	// Use the given placement in the structure (YAFFS1 legacy mode)
108*4882a593Smuzhiyun #define MTD_NANDECC_AUTOPLACE	2	// Use the default placement scheme
109*4882a593Smuzhiyun #define MTD_NANDECC_PLACEONLY	3	// Use the given placement in the structure (Do not store ecc result on read)
110*4882a593Smuzhiyun #define MTD_NANDECC_AUTOPL_USR 	4	// Use the given autoplacement scheme rather than using the default
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /* OTP mode selection */
113*4882a593Smuzhiyun #define MTD_OTP_OFF		0
114*4882a593Smuzhiyun #define MTD_OTP_FACTORY		1
115*4882a593Smuzhiyun #define MTD_OTP_USER		2
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun struct mtd_info_user {
118*4882a593Smuzhiyun 	__u8 type;
119*4882a593Smuzhiyun 	__u32 flags;
120*4882a593Smuzhiyun 	__u32 size;	/* Total size of the MTD */
121*4882a593Smuzhiyun 	__u32 erasesize;
122*4882a593Smuzhiyun 	__u32 writesize;
123*4882a593Smuzhiyun 	__u32 oobsize;	/* Amount of OOB data per block (e.g. 16) */
124*4882a593Smuzhiyun 	__u64 padding;	/* Old obsolete field; do not use */
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun struct region_info_user {
128*4882a593Smuzhiyun 	__u32 offset;		/* At which this region starts,
129*4882a593Smuzhiyun 				 * from the beginning of the MTD */
130*4882a593Smuzhiyun 	__u32 erasesize;	/* For this region */
131*4882a593Smuzhiyun 	__u32 numblocks;	/* Number of blocks in this region */
132*4882a593Smuzhiyun 	__u32 regionindex;
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun struct otp_info {
136*4882a593Smuzhiyun 	__u32 start;
137*4882a593Smuzhiyun 	__u32 length;
138*4882a593Smuzhiyun 	__u32 locked;
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun  * Note, the following ioctl existed in the past and was removed:
143*4882a593Smuzhiyun  * #define MEMSETOOBSEL           _IOW('M', 9, struct nand_oobinfo)
144*4882a593Smuzhiyun  * Try to avoid adding a new ioctl with the same ioctl number.
145*4882a593Smuzhiyun  */
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /* Get basic MTD characteristics info (better to use sysfs) */
148*4882a593Smuzhiyun #define MEMGETINFO		_IOR('M', 1, struct mtd_info_user)
149*4882a593Smuzhiyun /* Erase segment of MTD */
150*4882a593Smuzhiyun #define MEMERASE		_IOW('M', 2, struct erase_info_user)
151*4882a593Smuzhiyun /* Write out-of-band data from MTD */
152*4882a593Smuzhiyun #define MEMWRITEOOB		_IOWR('M', 3, struct mtd_oob_buf)
153*4882a593Smuzhiyun /* Read out-of-band data from MTD */
154*4882a593Smuzhiyun #define MEMREADOOB		_IOWR('M', 4, struct mtd_oob_buf)
155*4882a593Smuzhiyun /* Lock a chip (for MTD that supports it) */
156*4882a593Smuzhiyun #define MEMLOCK			_IOW('M', 5, struct erase_info_user)
157*4882a593Smuzhiyun /* Unlock a chip (for MTD that supports it) */
158*4882a593Smuzhiyun #define MEMUNLOCK		_IOW('M', 6, struct erase_info_user)
159*4882a593Smuzhiyun /* Get the number of different erase regions */
160*4882a593Smuzhiyun #define MEMGETREGIONCOUNT	_IOR('M', 7, int)
161*4882a593Smuzhiyun /* Get information about the erase region for a specific index */
162*4882a593Smuzhiyun #define MEMGETREGIONINFO	_IOWR('M', 8, struct region_info_user)
163*4882a593Smuzhiyun /* Get info about OOB modes (e.g., RAW, PLACE, AUTO) - legacy interface */
164*4882a593Smuzhiyun #define MEMGETOOBSEL		_IOR('M', 10, struct nand_oobinfo)
165*4882a593Smuzhiyun /* Check if an eraseblock is bad */
166*4882a593Smuzhiyun #define MEMGETBADBLOCK		_IOW('M', 11, __kernel_loff_t)
167*4882a593Smuzhiyun /* Mark an eraseblock as bad */
168*4882a593Smuzhiyun #define MEMSETBADBLOCK		_IOW('M', 12, __kernel_loff_t)
169*4882a593Smuzhiyun /* Set OTP (One-Time Programmable) mode (factory vs. user) */
170*4882a593Smuzhiyun #define OTPSELECT		_IOR('M', 13, int)
171*4882a593Smuzhiyun /* Get number of OTP (One-Time Programmable) regions */
172*4882a593Smuzhiyun #define OTPGETREGIONCOUNT	_IOW('M', 14, int)
173*4882a593Smuzhiyun /* Get all OTP (One-Time Programmable) info about MTD */
174*4882a593Smuzhiyun #define OTPGETREGIONINFO	_IOW('M', 15, struct otp_info)
175*4882a593Smuzhiyun /* Lock a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */
176*4882a593Smuzhiyun #define OTPLOCK			_IOR('M', 16, struct otp_info)
177*4882a593Smuzhiyun /* Get ECC layout (deprecated) */
178*4882a593Smuzhiyun #define ECCGETLAYOUT		_IOR('M', 17, struct nand_ecclayout_user)
179*4882a593Smuzhiyun /* Get statistics about corrected/uncorrected errors */
180*4882a593Smuzhiyun #define ECCGETSTATS		_IOR('M', 18, struct mtd_ecc_stats)
181*4882a593Smuzhiyun /* Set MTD mode on a per-file-descriptor basis (see "MTD file modes") */
182*4882a593Smuzhiyun #define MTDFILEMODE		_IO('M', 19)
183*4882a593Smuzhiyun /* Erase segment of MTD (supports 64-bit address) */
184*4882a593Smuzhiyun #define MEMERASE64		_IOW('M', 20, struct erase_info_user64)
185*4882a593Smuzhiyun /* Write data to OOB (64-bit version) */
186*4882a593Smuzhiyun #define MEMWRITEOOB64		_IOWR('M', 21, struct mtd_oob_buf64)
187*4882a593Smuzhiyun /* Read data from OOB (64-bit version) */
188*4882a593Smuzhiyun #define MEMREADOOB64		_IOWR('M', 22, struct mtd_oob_buf64)
189*4882a593Smuzhiyun /* Check if chip is locked (for MTD that supports it) */
190*4882a593Smuzhiyun #define MEMISLOCKED		_IOR('M', 23, struct erase_info_user)
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun  * Most generic write interface; can write in-band and/or out-of-band in various
193*4882a593Smuzhiyun  * modes (see "struct mtd_write_req"). This ioctl is not supported for flashes
194*4882a593Smuzhiyun  * without OOB, e.g., NOR flash.
195*4882a593Smuzhiyun  */
196*4882a593Smuzhiyun #define MEMWRITE		_IOWR('M', 24, struct mtd_write_req)
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun /*
199*4882a593Smuzhiyun  * Obsolete legacy interface. Keep it in order not to break userspace
200*4882a593Smuzhiyun  * interfaces
201*4882a593Smuzhiyun  */
202*4882a593Smuzhiyun struct nand_oobinfo {
203*4882a593Smuzhiyun 	__u32 useecc;
204*4882a593Smuzhiyun 	__u32 eccbytes;
205*4882a593Smuzhiyun 	__u32 oobfree[8][2];
206*4882a593Smuzhiyun 	__u32 eccpos[32];
207*4882a593Smuzhiyun };
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun struct nand_oobfree {
210*4882a593Smuzhiyun 	__u32 offset;
211*4882a593Smuzhiyun 	__u32 length;
212*4882a593Smuzhiyun };
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun #define MTD_MAX_OOBFREE_ENTRIES	8
215*4882a593Smuzhiyun #define MTD_MAX_ECCPOS_ENTRIES	64
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun  * OBSOLETE: ECC layout control structure. Exported to user-space via ioctl
218*4882a593Smuzhiyun  * ECCGETLAYOUT for backwards compatbility and should not be mistaken as a
219*4882a593Smuzhiyun  * complete set of ECC information. The ioctl truncates the larger internal
220*4882a593Smuzhiyun  * structure to retain binary compatibility with the static declaration of the
221*4882a593Smuzhiyun  * ioctl. Note that the "MTD_MAX_..._ENTRIES" macros represent the max size of
222*4882a593Smuzhiyun  * the user struct, not the MAX size of the internal struct nand_ecclayout.
223*4882a593Smuzhiyun  */
224*4882a593Smuzhiyun struct nand_ecclayout_user {
225*4882a593Smuzhiyun 	__u32 eccbytes;
226*4882a593Smuzhiyun 	__u32 eccpos[MTD_MAX_ECCPOS_ENTRIES];
227*4882a593Smuzhiyun 	__u32 oobavail;
228*4882a593Smuzhiyun 	struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES];
229*4882a593Smuzhiyun };
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun  * struct mtd_ecc_stats - error correction stats
233*4882a593Smuzhiyun  *
234*4882a593Smuzhiyun  * @corrected:	number of corrected bits
235*4882a593Smuzhiyun  * @failed:	number of uncorrectable errors
236*4882a593Smuzhiyun  * @badblocks:	number of bad blocks in this partition
237*4882a593Smuzhiyun  * @bbtblocks:	number of blocks reserved for bad block tables
238*4882a593Smuzhiyun  */
239*4882a593Smuzhiyun struct mtd_ecc_stats {
240*4882a593Smuzhiyun 	__u32 corrected;
241*4882a593Smuzhiyun 	__u32 failed;
242*4882a593Smuzhiyun 	__u32 badblocks;
243*4882a593Smuzhiyun 	__u32 bbtblocks;
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun  * MTD file modes - for read/write access to MTD
248*4882a593Smuzhiyun  *
249*4882a593Smuzhiyun  * @MTD_FILE_MODE_NORMAL:	OTP disabled, ECC enabled
250*4882a593Smuzhiyun  * @MTD_FILE_MODE_OTP_FACTORY:	OTP enabled in factory mode
251*4882a593Smuzhiyun  * @MTD_FILE_MODE_OTP_USER:	OTP enabled in user mode
252*4882a593Smuzhiyun  * @MTD_FILE_MODE_RAW:		OTP disabled, ECC disabled
253*4882a593Smuzhiyun  *
254*4882a593Smuzhiyun  * These modes can be set via ioctl(MTDFILEMODE). The mode mode will be retained
255*4882a593Smuzhiyun  * separately for each open file descriptor.
256*4882a593Smuzhiyun  *
257*4882a593Smuzhiyun  * Note: %MTD_FILE_MODE_RAW provides the same functionality as %MTD_OPS_RAW -
258*4882a593Smuzhiyun  * raw access to the flash, without error correction or autoplacement schemes.
259*4882a593Smuzhiyun  * Wherever possible, the MTD_OPS_* mode will override the MTD_FILE_MODE_* mode
260*4882a593Smuzhiyun  * (e.g., when using ioctl(MEMWRITE)), but in some cases, the MTD_FILE_MODE is
261*4882a593Smuzhiyun  * used out of necessity (e.g., `write()', ioctl(MEMWRITEOOB64)).
262*4882a593Smuzhiyun  */
263*4882a593Smuzhiyun enum mtd_file_modes {
264*4882a593Smuzhiyun 	MTD_FILE_MODE_NORMAL = MTD_OTP_OFF,
265*4882a593Smuzhiyun 	MTD_FILE_MODE_OTP_FACTORY = MTD_OTP_FACTORY,
266*4882a593Smuzhiyun 	MTD_FILE_MODE_OTP_USER = MTD_OTP_USER,
267*4882a593Smuzhiyun 	MTD_FILE_MODE_RAW,
268*4882a593Smuzhiyun };
269*4882a593Smuzhiyun 
mtd_type_is_nand_user(const struct mtd_info_user * mtd)270*4882a593Smuzhiyun static inline int mtd_type_is_nand_user(const struct mtd_info_user *mtd)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun 	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun #endif /* __MTD_ABI_H__ */
276