cfi.h
上传用户:szlgq88
上传日期:2009-04-28
资源大小:48287k
文件大小:11k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* Common Flash Interface structures 
  2.  * See http://support.intel.com/design/flash/technote/index.htm
  3.  * $Id: cfi.h,v 1.54 2005/06/06 23:04:36 tpoynor Exp $
  4.  */
  5. #ifndef __MTD_CFI_H__
  6. #define __MTD_CFI_H__
  7. #include <linux/config.h>
  8. #include <linux/version.h>
  9. #include <linux/delay.h>
  10. #include <linux/types.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mtd/flashchip.h>
  13. #include <linux/mtd/map.h>
  14. #include <linux/mtd/cfi_endian.h>
  15. #ifdef CONFIG_MTD_CFI_I1
  16. #define cfi_interleave(cfi) 1
  17. #define cfi_interleave_is_1(cfi) (cfi_interleave(cfi) == 1)
  18. #else
  19. #define cfi_interleave_is_1(cfi) (0)
  20. #endif
  21. #ifdef CONFIG_MTD_CFI_I2
  22. # ifdef cfi_interleave
  23. #  undef cfi_interleave
  24. #  define cfi_interleave(cfi) ((cfi)->interleave)
  25. # else
  26. #  define cfi_interleave(cfi) 2
  27. # endif
  28. #define cfi_interleave_is_2(cfi) (cfi_interleave(cfi) == 2)
  29. #else
  30. #define cfi_interleave_is_2(cfi) (0)
  31. #endif
  32. #ifdef CONFIG_MTD_CFI_I4
  33. # ifdef cfi_interleave
  34. #  undef cfi_interleave
  35. #  define cfi_interleave(cfi) ((cfi)->interleave)
  36. # else
  37. #  define cfi_interleave(cfi) 4
  38. # endif
  39. #define cfi_interleave_is_4(cfi) (cfi_interleave(cfi) == 4)
  40. #else
  41. #define cfi_interleave_is_4(cfi) (0)
  42. #endif
  43. #ifdef CONFIG_MTD_CFI_I8
  44. # ifdef cfi_interleave
  45. #  undef cfi_interleave
  46. #  define cfi_interleave(cfi) ((cfi)->interleave)
  47. # else
  48. #  define cfi_interleave(cfi) 8
  49. # endif
  50. #define cfi_interleave_is_8(cfi) (cfi_interleave(cfi) == 8)
  51. #else
  52. #define cfi_interleave_is_8(cfi) (0)
  53. #endif
  54. static inline int cfi_interleave_supported(int i)
  55. {
  56. switch (i) {
  57. #ifdef CONFIG_MTD_CFI_I1
  58. case 1:
  59. #endif
  60. #ifdef CONFIG_MTD_CFI_I2
  61. case 2:
  62. #endif
  63. #ifdef CONFIG_MTD_CFI_I4
  64. case 4:
  65. #endif
  66. #ifdef CONFIG_MTD_CFI_I8
  67. case 8:
  68. #endif
  69. return 1;
  70. default:
  71. return 0;
  72. }
  73. }
  74. /* NB: these values must represents the number of bytes needed to meet the 
  75.  *     device type (x8, x16, x32).  Eg. a 32 bit device is 4 x 8 bytes. 
  76.  *     These numbers are used in calculations.
  77.  */
  78. #define CFI_DEVICETYPE_X8  (8 / 8)
  79. #define CFI_DEVICETYPE_X16 (16 / 8)
  80. #define CFI_DEVICETYPE_X32 (32 / 8)
  81. #define CFI_DEVICETYPE_X64 (64 / 8)
  82. /* NB: We keep these structures in memory in HOST byteorder, except
  83.  * where individually noted.
  84.  */
  85. /* Basic Query Structure */
  86. struct cfi_ident {
  87. uint8_t  qry[3];
  88. uint16_t P_ID;
  89. uint16_t P_ADR;
  90. uint16_t A_ID;
  91. uint16_t A_ADR;
  92. uint8_t  VccMin;
  93. uint8_t  VccMax;
  94. uint8_t  VppMin;
  95. uint8_t  VppMax;
  96. uint8_t  WordWriteTimeoutTyp;
  97. uint8_t  BufWriteTimeoutTyp;
  98. uint8_t  BlockEraseTimeoutTyp;
  99. uint8_t  ChipEraseTimeoutTyp;
  100. uint8_t  WordWriteTimeoutMax;
  101. uint8_t  BufWriteTimeoutMax;
  102. uint8_t  BlockEraseTimeoutMax;
  103. uint8_t  ChipEraseTimeoutMax;
  104. uint8_t  DevSize;
  105. uint16_t InterfaceDesc;
  106. uint16_t MaxBufWriteSize;
  107. uint8_t  NumEraseRegions;
  108. uint32_t EraseRegionInfo[0]; /* Not host ordered */
  109. } __attribute__((packed));
  110. /* Extended Query Structure for both PRI and ALT */
  111. struct cfi_extquery {
  112. uint8_t  pri[3];
  113. uint8_t  MajorVersion;
  114. uint8_t  MinorVersion;
  115. } __attribute__((packed));
  116. /* Vendor-Specific PRI for Intel/Sharp Extended Command Set (0x0001) */
  117. struct cfi_pri_intelext {
  118. uint8_t  pri[3];
  119. uint8_t  MajorVersion;
  120. uint8_t  MinorVersion;
  121. uint32_t FeatureSupport; /* if bit 31 is set then an additional uint32_t feature
  122.     block follows - FIXME - not currently supported */
  123. uint8_t  SuspendCmdSupport;
  124. uint16_t BlkStatusRegMask;
  125. uint8_t  VccOptimal;
  126. uint8_t  VppOptimal;
  127. uint8_t  NumProtectionFields;
  128. uint16_t ProtRegAddr;
  129. uint8_t  FactProtRegSize;
  130. uint8_t  UserProtRegSize;
  131. uint8_t  extra[0];
  132. } __attribute__((packed));
  133. struct cfi_intelext_otpinfo {
  134. uint32_t ProtRegAddr;
  135. uint16_t FactGroups;
  136. uint8_t  FactProtRegSize;
  137. uint16_t UserGroups;
  138. uint8_t  UserProtRegSize;
  139. } __attribute__((packed));
  140. struct cfi_intelext_blockinfo {
  141. uint16_t NumIdentBlocks;
  142. uint16_t BlockSize;
  143. uint16_t MinBlockEraseCycles;
  144. uint8_t  BitsPerCell;
  145. uint8_t  BlockCap;
  146. } __attribute__((packed));
  147. struct cfi_intelext_regioninfo {
  148. uint16_t NumIdentPartitions;
  149. uint8_t  NumOpAllowed;
  150. uint8_t  NumOpAllowedSimProgMode;
  151. uint8_t  NumOpAllowedSimEraMode;
  152. uint8_t  NumBlockTypes;
  153. struct cfi_intelext_blockinfo BlockTypes[1];
  154. } __attribute__((packed));
  155. /* Vendor-Specific PRI for AMD/Fujitsu Extended Command Set (0x0002) */
  156. struct cfi_pri_amdstd {
  157. uint8_t  pri[3];
  158. uint8_t  MajorVersion;
  159. uint8_t  MinorVersion;
  160. uint8_t  SiliconRevision; /* bits 1-0: Address Sensitive Unlock */
  161. uint8_t  EraseSuspend;
  162. uint8_t  BlkProt;
  163. uint8_t  TmpBlkUnprotect;
  164. uint8_t  BlkProtUnprot;
  165. uint8_t  SimultaneousOps;
  166. uint8_t  BurstMode;
  167. uint8_t  PageMode;
  168. uint8_t  VppMin;
  169. uint8_t  VppMax;
  170. uint8_t  TopBottom;
  171. } __attribute__((packed));
  172. struct cfi_pri_query {
  173. uint8_t  NumFields;
  174. uint32_t ProtField[1]; /* Not host ordered */
  175. } __attribute__((packed));
  176. struct cfi_bri_query {
  177. uint8_t  PageModeReadCap;
  178. uint8_t  NumFields;
  179. uint32_t ConfField[1]; /* Not host ordered */
  180. } __attribute__((packed));
  181. #define P_ID_NONE               0x0000
  182. #define P_ID_INTEL_EXT          0x0001
  183. #define P_ID_AMD_STD            0x0002
  184. #define P_ID_INTEL_STD          0x0003
  185. #define P_ID_AMD_EXT            0x0004
  186. #define P_ID_WINBOND            0x0006
  187. #define P_ID_ST_ADV             0x0020
  188. #define P_ID_MITSUBISHI_STD     0x0100
  189. #define P_ID_MITSUBISHI_EXT     0x0101
  190. #define P_ID_SST_PAGE           0x0102
  191. #define P_ID_INTEL_PERFORMANCE  0x0200
  192. #define P_ID_INTEL_DATA         0x0210
  193. #define P_ID_RESERVED           0xffff
  194. #define CFI_MODE_CFI 1
  195. #define CFI_MODE_JEDEC 0
  196. struct cfi_private {
  197. uint16_t cmdset;
  198. void *cmdset_priv;
  199. int interleave;
  200. int device_type;
  201. int cfi_mode; /* Are we a JEDEC device pretending to be CFI? */
  202. int addr_unlock1;
  203. int addr_unlock2;
  204. struct mtd_info *(*cmdset_setup)(struct map_info *);
  205. struct cfi_ident *cfiq; /* For now only one. We insist that all devs
  206.   must be of the same type. */
  207. int mfr, id;
  208. int numchips;
  209. unsigned long chipshift; /* Because they're of the same type */
  210. const char *im_name;  /* inter_module name for cmdset_setup */
  211. struct flchip chips[0];  /* per-chip data structure for each chip */
  212. };
  213. /*
  214.  * Returns the command address according to the given geometry.
  215.  */
  216. static inline uint32_t cfi_build_cmd_addr(uint32_t cmd_ofs, int interleave, int type)
  217. {
  218. return (cmd_ofs * type) * interleave;
  219. }
  220. /*
  221.  * Transforms the CFI command for the given geometry (bus width & interleave).
  222.  * It looks too long to be inline, but in the common case it should almost all
  223.  * get optimised away. 
  224.  */
  225. static inline map_word cfi_build_cmd(u_long cmd, struct map_info *map, struct cfi_private *cfi)
  226. {
  227. map_word val = { {0} };
  228. int wordwidth, words_per_bus, chip_mode, chips_per_word;
  229. unsigned long onecmd;
  230. int i;
  231. /* We do it this way to give the compiler a fighting chance 
  232.    of optimising away all the crap for 'bankwidth' larger than
  233.    an unsigned long, in the common case where that support is
  234.    disabled */
  235. if (map_bankwidth_is_large(map)) {
  236. wordwidth = sizeof(unsigned long);
  237. words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
  238. } else {
  239. wordwidth = map_bankwidth(map);
  240. words_per_bus = 1;
  241. }
  242. chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
  243. chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
  244. /* First, determine what the bit-pattern should be for a single
  245.    device, according to chip mode and endianness... */
  246. switch (chip_mode) {
  247. default: BUG();
  248. case 1:
  249. onecmd = cmd;
  250. break;
  251. case 2:
  252. onecmd = cpu_to_cfi16(cmd);
  253. break;
  254. case 4:
  255. onecmd = cpu_to_cfi32(cmd);
  256. break;
  257. }
  258. /* Now replicate it across the size of an unsigned long, or 
  259.    just to the bus width as appropriate */
  260. switch (chips_per_word) {
  261. default: BUG();
  262. #if BITS_PER_LONG >= 64
  263. case 8:
  264. onecmd |= (onecmd << (chip_mode * 32));
  265. #endif
  266. case 4:
  267. onecmd |= (onecmd << (chip_mode * 16));
  268. case 2:
  269. onecmd |= (onecmd << (chip_mode * 8));
  270. case 1:
  271. ;
  272. }
  273. /* And finally, for the multi-word case, replicate it 
  274.    in all words in the structure */
  275. for (i=0; i < words_per_bus; i++) {
  276. val.x[i] = onecmd;
  277. }
  278. return val;
  279. }
  280. #define CMD(x)  cfi_build_cmd((x), map, cfi)
  281. static inline unsigned char cfi_merge_status(map_word val, struct map_info *map, 
  282.    struct cfi_private *cfi)
  283. {
  284. int wordwidth, words_per_bus, chip_mode, chips_per_word;
  285. unsigned long onestat, res = 0;
  286. int i;
  287. /* We do it this way to give the compiler a fighting chance 
  288.    of optimising away all the crap for 'bankwidth' larger than
  289.    an unsigned long, in the common case where that support is
  290.    disabled */
  291. if (map_bankwidth_is_large(map)) {
  292. wordwidth = sizeof(unsigned long);
  293. words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
  294. } else {
  295. wordwidth = map_bankwidth(map);
  296. words_per_bus = 1;
  297. }
  298. chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
  299. chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
  300. onestat = val.x[0];
  301. /* Or all status words together */
  302. for (i=1; i < words_per_bus; i++) {
  303. onestat |= val.x[i];
  304. }
  305. res = onestat;
  306. switch(chips_per_word) {
  307. default: BUG();
  308. #if BITS_PER_LONG >= 64
  309. case 8:
  310. res |= (onestat >> (chip_mode * 32));
  311. #endif
  312. case 4:
  313. res |= (onestat >> (chip_mode * 16));
  314. case 2:
  315. res |= (onestat >> (chip_mode * 8));
  316. case 1:
  317. ;
  318. }
  319. /* Last, determine what the bit-pattern should be for a single
  320.    device, according to chip mode and endianness... */
  321. switch (chip_mode) {
  322. case 1:
  323. break;
  324. case 2:
  325. res = cfi16_to_cpu(res);
  326. break;
  327. case 4:
  328. res = cfi32_to_cpu(res);
  329. break;
  330. default: BUG();
  331. }
  332. return res;
  333. }
  334. #define MERGESTATUS(x) cfi_merge_status((x), map, cfi)
  335. /*
  336.  * Sends a CFI command to a bank of flash for the given geometry.
  337.  *
  338.  * Returns the offset in flash where the command was written.
  339.  * If prev_val is non-null, it will be set to the value at the command address,
  340.  * before the command was written.
  341.  */
  342. static inline uint32_t cfi_send_gen_cmd(u_char cmd, uint32_t cmd_addr, uint32_t base,
  343. struct map_info *map, struct cfi_private *cfi,
  344. int type, map_word *prev_val)
  345. {
  346. map_word val;
  347. uint32_t addr = base + cfi_build_cmd_addr(cmd_addr, cfi_interleave(cfi), type);
  348. val = cfi_build_cmd(cmd, map, cfi);
  349. if (prev_val)
  350. *prev_val = map_read(map, addr);
  351. map_write(map, val, addr);
  352. return addr - base;
  353. }
  354. static inline uint8_t cfi_read_query(struct map_info *map, uint32_t addr)
  355. {
  356. map_word val = map_read(map, addr);
  357. if (map_bankwidth_is_1(map)) {
  358. return val.x[0];
  359. } else if (map_bankwidth_is_2(map)) {
  360. return cfi16_to_cpu(val.x[0]);
  361. } else {
  362. /* No point in a 64-bit byteswap since that would just be
  363.    swapping the responses from different chips, and we are
  364.    only interested in one chip (a representative sample) */
  365. return cfi32_to_cpu(val.x[0]);
  366. }
  367. }
  368. static inline void cfi_udelay(int us)
  369. {
  370. if (us >= 1000) {
  371. msleep((us+999)/1000);
  372. } else {
  373. udelay(us);
  374. cond_resched();
  375. }
  376. }
  377. struct cfi_extquery *cfi_read_pri(struct map_info *map, uint16_t adr, uint16_t size,
  378.      const char* name);
  379. struct cfi_fixup {
  380. uint16_t mfr;
  381. uint16_t id;
  382. void (*fixup)(struct mtd_info *mtd, void* param);
  383. void* param;
  384. };
  385. #define CFI_MFR_ANY 0xffff
  386. #define CFI_ID_ANY  0xffff
  387. #define CFI_MFR_AMD 0x0001
  388. #define CFI_MFR_ST  0x0020  /* STMicroelectronics */
  389. void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup* fixups);
  390. typedef int (*varsize_frob_t)(struct map_info *map, struct flchip *chip,
  391.       unsigned long adr, int len, void *thunk);
  392. int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
  393. loff_t ofs, size_t len, void *thunk);
  394. #endif /* __MTD_CFI_H__ */