mtd.h 7.8 KB

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