cfi.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* Common Flash Interface structures
  2. * See http://support.intel.com/design/flash/technote/index.htm
  3. * $Id: cfi.h,v 1.50 2004/11/20 12:46:51 dwmw2 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_blockinfo {
  134. uint16_t NumIdentBlocks;
  135. uint16_t BlockSize;
  136. uint16_t MinBlockEraseCycles;
  137. uint8_t BitsPerCell;
  138. uint8_t BlockCap;
  139. } __attribute__((packed));
  140. struct cfi_intelext_regioninfo {
  141. uint16_t NumIdentPartitions;
  142. uint8_t NumOpAllowed;
  143. uint8_t NumOpAllowedSimProgMode;
  144. uint8_t NumOpAllowedSimEraMode;
  145. uint8_t NumBlockTypes;
  146. struct cfi_intelext_blockinfo BlockTypes[1];
  147. } __attribute__((packed));
  148. /* Vendor-Specific PRI for AMD/Fujitsu Extended Command Set (0x0002) */
  149. struct cfi_pri_amdstd {
  150. uint8_t pri[3];
  151. uint8_t MajorVersion;
  152. uint8_t MinorVersion;
  153. uint8_t SiliconRevision; /* bits 1-0: Address Sensitive Unlock */
  154. uint8_t EraseSuspend;
  155. uint8_t BlkProt;
  156. uint8_t TmpBlkUnprotect;
  157. uint8_t BlkProtUnprot;
  158. uint8_t SimultaneousOps;
  159. uint8_t BurstMode;
  160. uint8_t PageMode;
  161. uint8_t VppMin;
  162. uint8_t VppMax;
  163. uint8_t TopBottom;
  164. } __attribute__((packed));
  165. struct cfi_pri_query {
  166. uint8_t NumFields;
  167. uint32_t ProtField[1]; /* Not host ordered */
  168. } __attribute__((packed));
  169. struct cfi_bri_query {
  170. uint8_t PageModeReadCap;
  171. uint8_t NumFields;
  172. uint32_t ConfField[1]; /* Not host ordered */
  173. } __attribute__((packed));
  174. #define P_ID_NONE 0x0000
  175. #define P_ID_INTEL_EXT 0x0001
  176. #define P_ID_AMD_STD 0x0002
  177. #define P_ID_INTEL_STD 0x0003
  178. #define P_ID_AMD_EXT 0x0004
  179. #define P_ID_WINBOND 0x0006
  180. #define P_ID_ST_ADV 0x0020
  181. #define P_ID_MITSUBISHI_STD 0x0100
  182. #define P_ID_MITSUBISHI_EXT 0x0101
  183. #define P_ID_SST_PAGE 0x0102
  184. #define P_ID_INTEL_PERFORMANCE 0x0200
  185. #define P_ID_INTEL_DATA 0x0210
  186. #define P_ID_RESERVED 0xffff
  187. #define CFI_MODE_CFI 1
  188. #define CFI_MODE_JEDEC 0
  189. struct cfi_private {
  190. uint16_t cmdset;
  191. void *cmdset_priv;
  192. int interleave;
  193. int device_type;
  194. int cfi_mode; /* Are we a JEDEC device pretending to be CFI? */
  195. int addr_unlock1;
  196. int addr_unlock2;
  197. struct mtd_info *(*cmdset_setup)(struct map_info *);
  198. struct cfi_ident *cfiq; /* For now only one. We insist that all devs
  199. must be of the same type. */
  200. int mfr, id;
  201. int numchips;
  202. unsigned long chipshift; /* Because they're of the same type */
  203. const char *im_name; /* inter_module name for cmdset_setup */
  204. struct flchip chips[0]; /* per-chip data structure for each chip */
  205. };
  206. /*
  207. * Returns the command address according to the given geometry.
  208. */
  209. static inline uint32_t cfi_build_cmd_addr(uint32_t cmd_ofs, int interleave, int type)
  210. {
  211. return (cmd_ofs * type) * interleave;
  212. }
  213. /*
  214. * Transforms the CFI command for the given geometry (bus width & interleave).
  215. * It looks too long to be inline, but in the common case it should almost all
  216. * get optimised away.
  217. */
  218. static inline map_word cfi_build_cmd(u_char cmd, struct map_info *map, struct cfi_private *cfi)
  219. {
  220. map_word val = { {0} };
  221. int wordwidth, words_per_bus, chip_mode, chips_per_word;
  222. unsigned long onecmd;
  223. int i;
  224. /* We do it this way to give the compiler a fighting chance
  225. of optimising away all the crap for 'bankwidth' larger than
  226. an unsigned long, in the common case where that support is
  227. disabled */
  228. if (map_bankwidth_is_large(map)) {
  229. wordwidth = sizeof(unsigned long);
  230. words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
  231. } else {
  232. wordwidth = map_bankwidth(map);
  233. words_per_bus = 1;
  234. }
  235. chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
  236. chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
  237. /* First, determine what the bit-pattern should be for a single
  238. device, according to chip mode and endianness... */
  239. switch (chip_mode) {
  240. default: BUG();
  241. case 1:
  242. onecmd = cmd;
  243. break;
  244. case 2:
  245. onecmd = cpu_to_cfi16(cmd);
  246. break;
  247. case 4:
  248. onecmd = cpu_to_cfi32(cmd);
  249. break;
  250. }
  251. /* Now replicate it across the size of an unsigned long, or
  252. just to the bus width as appropriate */
  253. switch (chips_per_word) {
  254. default: BUG();
  255. #if BITS_PER_LONG >= 64
  256. case 8:
  257. onecmd |= (onecmd << (chip_mode * 32));
  258. #endif
  259. case 4:
  260. onecmd |= (onecmd << (chip_mode * 16));
  261. case 2:
  262. onecmd |= (onecmd << (chip_mode * 8));
  263. case 1:
  264. ;
  265. }
  266. /* And finally, for the multi-word case, replicate it
  267. in all words in the structure */
  268. for (i=0; i < words_per_bus; i++) {
  269. val.x[i] = onecmd;
  270. }
  271. return val;
  272. }
  273. #define CMD(x) cfi_build_cmd((x), map, cfi)
  274. /*
  275. * Sends a CFI command to a bank of flash for the given geometry.
  276. *
  277. * Returns the offset in flash where the command was written.
  278. * If prev_val is non-null, it will be set to the value at the command address,
  279. * before the command was written.
  280. */
  281. static inline uint32_t cfi_send_gen_cmd(u_char cmd, uint32_t cmd_addr, uint32_t base,
  282. struct map_info *map, struct cfi_private *cfi,
  283. int type, map_word *prev_val)
  284. {
  285. map_word val;
  286. uint32_t addr = base + cfi_build_cmd_addr(cmd_addr, cfi_interleave(cfi), type);
  287. val = cfi_build_cmd(cmd, map, cfi);
  288. if (prev_val)
  289. *prev_val = map_read(map, addr);
  290. map_write(map, val, addr);
  291. return addr - base;
  292. }
  293. static inline uint8_t cfi_read_query(struct map_info *map, uint32_t addr)
  294. {
  295. map_word val = map_read(map, addr);
  296. if (map_bankwidth_is_1(map)) {
  297. return val.x[0];
  298. } else if (map_bankwidth_is_2(map)) {
  299. return cfi16_to_cpu(val.x[0]);
  300. } else {
  301. /* No point in a 64-bit byteswap since that would just be
  302. swapping the responses from different chips, and we are
  303. only interested in one chip (a representative sample) */
  304. return cfi32_to_cpu(val.x[0]);
  305. }
  306. }
  307. static inline void cfi_udelay(int us)
  308. {
  309. if (us >= 1000) {
  310. msleep((us+999)/1000);
  311. } else {
  312. udelay(us);
  313. cond_resched();
  314. }
  315. }
  316. static inline void cfi_spin_lock(spinlock_t *mutex)
  317. {
  318. spin_lock_bh(mutex);
  319. }
  320. static inline void cfi_spin_unlock(spinlock_t *mutex)
  321. {
  322. spin_unlock_bh(mutex);
  323. }
  324. struct cfi_extquery *cfi_read_pri(struct map_info *map, uint16_t adr, uint16_t size,
  325. const char* name);
  326. struct cfi_fixup {
  327. uint16_t mfr;
  328. uint16_t id;
  329. void (*fixup)(struct mtd_info *mtd, void* param);
  330. void* param;
  331. };
  332. #define CFI_MFR_ANY 0xffff
  333. #define CFI_ID_ANY 0xffff
  334. #define CFI_MFR_AMD 0x0001
  335. #define CFI_MFR_ST 0x0020 /* STMicroelectronics */
  336. void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup* fixups);
  337. typedef int (*varsize_frob_t)(struct map_info *map, struct flchip *chip,
  338. unsigned long adr, int len, void *thunk);
  339. int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
  340. loff_t ofs, size_t len, void *thunk);
  341. #endif /* __MTD_CFI_H__ */