mtd.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * $Id: mtd.h,v 1.61 2005/11/07 11:14:54 gleixner Exp $
  3. *
  4. * Copyright (C) 1999-2003 David Woodhouse <dwmw2@infradead.org> et al.
  5. *
  6. * Released under GPL
  7. */
  8. #ifndef __MTD_MTD_H__
  9. #define __MTD_MTD_H__
  10. #ifndef __KERNEL__
  11. #error This is a kernel header. Perhaps include mtd-user.h instead?
  12. #endif
  13. #include <linux/config.h>
  14. #include <linux/types.h>
  15. #include <linux/module.h>
  16. #include <linux/uio.h>
  17. #include <linux/notifier.h>
  18. #include <linux/mtd/compatmac.h>
  19. #include <mtd/mtd-abi.h>
  20. #define MTD_CHAR_MAJOR 90
  21. #define MTD_BLOCK_MAJOR 31
  22. #define MAX_MTD_DEVICES 16
  23. #define MTD_ERASE_PENDING 0x01
  24. #define MTD_ERASING 0x02
  25. #define MTD_ERASE_SUSPEND 0x04
  26. #define MTD_ERASE_DONE 0x08
  27. #define MTD_ERASE_FAILED 0x10
  28. /* If the erase fails, fail_addr might indicate exactly which block failed. If
  29. fail_addr = 0xffffffff, the failure was not at the device level or was not
  30. specific to any particular block. */
  31. struct erase_info {
  32. struct mtd_info *mtd;
  33. u_int32_t addr;
  34. u_int32_t len;
  35. u_int32_t fail_addr;
  36. u_long time;
  37. u_long retries;
  38. u_int dev;
  39. u_int cell;
  40. void (*callback) (struct erase_info *self);
  41. u_long priv;
  42. u_char state;
  43. struct erase_info *next;
  44. };
  45. struct mtd_erase_region_info {
  46. u_int32_t offset; /* At which this region starts, from the beginning of the MTD */
  47. u_int32_t erasesize; /* For this region */
  48. u_int32_t numblocks; /* Number of blocks of erasesize in this region */
  49. };
  50. /**
  51. * struct mtd_ecc_stats - error correction status
  52. *
  53. * @corrected: number of corrected bits
  54. * @failed: number of uncorrectable errors
  55. */
  56. struct mtd_ecc_stats {
  57. unsigned long corrected;
  58. unsigned long failed;
  59. };
  60. /*
  61. * oob operation modes
  62. *
  63. * MTD_OOB_PLACE: oob data are placed at the given offset
  64. * MTD_OOB_AUTO: oob data are automatically placed at the free areas
  65. * which are defined by the ecclayout
  66. * MTD_OOB_RAW: mode to read raw data+oob in one chunk. The oob data
  67. * is inserted into the data. Thats a raw image of the
  68. * flash contents.
  69. */
  70. typedef enum {
  71. MTD_OOB_PLACE,
  72. MTD_OOB_AUTO,
  73. MTD_OOB_RAW,
  74. } mtd_oob_mode_t;
  75. /**
  76. * struct mtd_oob_ops - oob operation operands
  77. * @mode: operation mode
  78. *
  79. * @len: number of bytes to write/read. When a data buffer is given
  80. * (datbuf != NULL) this is the number of data bytes. When
  81. + no data buffer is available this is the number of oob bytes.
  82. *
  83. * @retlen: number of bytes written/read. When a data buffer is given
  84. * (datbuf != NULL) this is the number of data bytes. When
  85. + no data buffer is available this is the number of oob bytes.
  86. *
  87. * @ooblen: number of oob bytes per page
  88. * @ooboffs: offset of oob data in the oob area (only relevant when
  89. * mode = MTD_OOB_PLACE)
  90. * @datbuf: data buffer - if NULL only oob data are read/written
  91. * @oobbuf: oob data buffer
  92. */
  93. struct mtd_oob_ops {
  94. mtd_oob_mode_t mode;
  95. size_t len;
  96. size_t retlen;
  97. size_t ooblen;
  98. uint32_t ooboffs;
  99. uint8_t *datbuf;
  100. uint8_t *oobbuf;
  101. };
  102. struct mtd_info {
  103. u_char type;
  104. u_int32_t flags;
  105. u_int32_t size; // Total size of the MTD
  106. /* "Major" erase size for the device. Naïve users may take this
  107. * to be the only erase size available, or may use the more detailed
  108. * information below if they desire
  109. */
  110. u_int32_t erasesize;
  111. /* Smallest availlable size for writing to the device. For NAND,
  112. * this is the page size, for some NOR chips, the size of ECC
  113. * covered blocks.
  114. */
  115. u_int32_t writesize;
  116. u_int32_t oobsize; // Amount of OOB data per block (e.g. 16)
  117. u_int32_t ecctype;
  118. u_int32_t eccsize;
  119. /*
  120. * Reuse some of the above unused fields in the case of NOR flash
  121. * with configurable programming regions to avoid modifying the
  122. * user visible structure layout/size. Only valid when the
  123. * MTD_PROGRAM_REGIONS flag is set.
  124. * (Maybe we should have an union for those?)
  125. */
  126. #define MTD_PROGREGION_CTRLMODE_VALID(mtd) (mtd)->oobsize
  127. #define MTD_PROGREGION_CTRLMODE_INVALID(mtd) (mtd)->ecctype
  128. // Kernel-only stuff starts here.
  129. char *name;
  130. int index;
  131. /* ecc layout structure pointer - read only ! */
  132. struct nand_ecclayout *ecclayout;
  133. /* Data for variable erase regions. If numeraseregions is zero,
  134. * it means that the whole device has erasesize as given above.
  135. */
  136. int numeraseregions;
  137. struct mtd_erase_region_info *eraseregions;
  138. /* This really shouldn't be here. It can go away in 2.5 */
  139. u_int32_t bank_size;
  140. int (*erase) (struct mtd_info *mtd, struct erase_info *instr);
  141. /* This stuff for eXecute-In-Place */
  142. int (*point) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf);
  143. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  144. void (*unpoint) (struct mtd_info *mtd, u_char * addr, loff_t from, size_t len);
  145. int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
  146. int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
  147. int (*read_oob) (struct mtd_info *mtd, loff_t from,
  148. struct mtd_oob_ops *ops);
  149. int (*write_oob) (struct mtd_info *mtd, loff_t to,
  150. struct mtd_oob_ops *ops);
  151. /*
  152. * Methods to access the protection register area, present in some
  153. * flash devices. The user data is one time programmable but the
  154. * factory data is read only.
  155. */
  156. int (*get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len);
  157. int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
  158. int (*get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len);
  159. int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
  160. int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
  161. int (*lock_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len);
  162. /* kvec-based read/write methods.
  163. NB: The 'count' parameter is the number of _vectors_, each of
  164. which contains an (ofs, len) tuple.
  165. */
  166. int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen);
  167. /* Sync */
  168. void (*sync) (struct mtd_info *mtd);
  169. /* Chip-supported device locking */
  170. int (*lock) (struct mtd_info *mtd, loff_t ofs, size_t len);
  171. int (*unlock) (struct mtd_info *mtd, loff_t ofs, size_t len);
  172. /* Power Management functions */
  173. int (*suspend) (struct mtd_info *mtd);
  174. void (*resume) (struct mtd_info *mtd);
  175. /* Bad block management functions */
  176. int (*block_isbad) (struct mtd_info *mtd, loff_t ofs);
  177. int (*block_markbad) (struct mtd_info *mtd, loff_t ofs);
  178. struct notifier_block reboot_notifier; /* default mode before reboot */
  179. /* ECC status information */
  180. struct mtd_ecc_stats ecc_stats;
  181. void *priv;
  182. struct module *owner;
  183. int usecount;
  184. };
  185. /* Kernel-side ioctl definitions */
  186. extern int add_mtd_device(struct mtd_info *mtd);
  187. extern int del_mtd_device (struct mtd_info *mtd);
  188. extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
  189. extern void put_mtd_device(struct mtd_info *mtd);
  190. struct mtd_notifier {
  191. void (*add)(struct mtd_info *mtd);
  192. void (*remove)(struct mtd_info *mtd);
  193. struct list_head list;
  194. };
  195. extern void register_mtd_user (struct mtd_notifier *new);
  196. extern int unregister_mtd_user (struct mtd_notifier *old);
  197. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  198. unsigned long count, loff_t to, size_t *retlen);
  199. int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs,
  200. unsigned long count, loff_t from, size_t *retlen);
  201. #ifdef CONFIG_MTD_PARTITIONS
  202. void mtd_erase_callback(struct erase_info *instr);
  203. #else
  204. static inline void mtd_erase_callback(struct erase_info *instr)
  205. {
  206. if (instr->callback)
  207. instr->callback(instr);
  208. }
  209. #endif
  210. /*
  211. * Debugging macro and defines
  212. */
  213. #define MTD_DEBUG_LEVEL0 (0) /* Quiet */
  214. #define MTD_DEBUG_LEVEL1 (1) /* Audible */
  215. #define MTD_DEBUG_LEVEL2 (2) /* Loud */
  216. #define MTD_DEBUG_LEVEL3 (3) /* Noisy */
  217. #ifdef CONFIG_MTD_DEBUG
  218. #define DEBUG(n, args...) \
  219. do { \
  220. if (n <= CONFIG_MTD_DEBUG_VERBOSE) \
  221. printk(KERN_INFO args); \
  222. } while(0)
  223. #else /* CONFIG_MTD_DEBUG */
  224. #define DEBUG(n, args...) do { } while(0)
  225. #endif /* CONFIG_MTD_DEBUG */
  226. #endif /* __MTD_MTD_H__ */