mtd.h 7.9 KB

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