mtd.h 8.1 KB

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