mtd.h 7.9 KB

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