mtd-abi.h 8.7 KB

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