cfi.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> et al.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #ifndef __MTD_CFI_H__
  20. #define __MTD_CFI_H__
  21. #include <linux/delay.h>
  22. #include <linux/types.h>
  23. #include <linux/bug.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/mtd/flashchip.h>
  26. #include <linux/mtd/map.h>
  27. #include <linux/mtd/cfi_endian.h>
  28. #include <linux/mtd/xip.h>
  29. #ifdef CONFIG_MTD_CFI_I1
  30. #define cfi_interleave(cfi) 1
  31. #define cfi_interleave_is_1(cfi) (cfi_interleave(cfi) == 1)
  32. #else
  33. #define cfi_interleave_is_1(cfi) (0)
  34. #endif
  35. #ifdef CONFIG_MTD_CFI_I2
  36. # ifdef cfi_interleave
  37. # undef cfi_interleave
  38. # define cfi_interleave(cfi) ((cfi)->interleave)
  39. # else
  40. # define cfi_interleave(cfi) 2
  41. # endif
  42. #define cfi_interleave_is_2(cfi) (cfi_interleave(cfi) == 2)
  43. #else
  44. #define cfi_interleave_is_2(cfi) (0)
  45. #endif
  46. #ifdef CONFIG_MTD_CFI_I4
  47. # ifdef cfi_interleave
  48. # undef cfi_interleave
  49. # define cfi_interleave(cfi) ((cfi)->interleave)
  50. # else
  51. # define cfi_interleave(cfi) 4
  52. # endif
  53. #define cfi_interleave_is_4(cfi) (cfi_interleave(cfi) == 4)
  54. #else
  55. #define cfi_interleave_is_4(cfi) (0)
  56. #endif
  57. #ifdef CONFIG_MTD_CFI_I8
  58. # ifdef cfi_interleave
  59. # undef cfi_interleave
  60. # define cfi_interleave(cfi) ((cfi)->interleave)
  61. # else
  62. # define cfi_interleave(cfi) 8
  63. # endif
  64. #define cfi_interleave_is_8(cfi) (cfi_interleave(cfi) == 8)
  65. #else
  66. #define cfi_interleave_is_8(cfi) (0)
  67. #endif
  68. #ifndef cfi_interleave
  69. #warning No CONFIG_MTD_CFI_Ix selected. No NOR chip support can work.
  70. static inline int cfi_interleave(void *cfi)
  71. {
  72. BUG();
  73. return 0;
  74. }
  75. #endif
  76. static inline int cfi_interleave_supported(int i)
  77. {
  78. switch (i) {
  79. #ifdef CONFIG_MTD_CFI_I1
  80. case 1:
  81. #endif
  82. #ifdef CONFIG_MTD_CFI_I2
  83. case 2:
  84. #endif
  85. #ifdef CONFIG_MTD_CFI_I4
  86. case 4:
  87. #endif
  88. #ifdef CONFIG_MTD_CFI_I8
  89. case 8:
  90. #endif
  91. return 1;
  92. default:
  93. return 0;
  94. }
  95. }
  96. /* NB: these values must represents the number of bytes needed to meet the
  97. * device type (x8, x16, x32). Eg. a 32 bit device is 4 x 8 bytes.
  98. * These numbers are used in calculations.
  99. */
  100. #define CFI_DEVICETYPE_X8 (8 / 8)
  101. #define CFI_DEVICETYPE_X16 (16 / 8)
  102. #define CFI_DEVICETYPE_X32 (32 / 8)
  103. #define CFI_DEVICETYPE_X64 (64 / 8)
  104. /* Device Interface Code Assignments from the "Common Flash Memory Interface
  105. * Publication 100" dated December 1, 2001.
  106. */
  107. #define CFI_INTERFACE_X8_ASYNC 0x0000
  108. #define CFI_INTERFACE_X16_ASYNC 0x0001
  109. #define CFI_INTERFACE_X8_BY_X16_ASYNC 0x0002
  110. #define CFI_INTERFACE_X32_ASYNC 0x0003
  111. #define CFI_INTERFACE_X16_BY_X32_ASYNC 0x0005
  112. #define CFI_INTERFACE_NOT_ALLOWED 0xffff
  113. /* NB: We keep these structures in memory in HOST byteorder, except
  114. * where individually noted.
  115. */
  116. /* Basic Query Structure */
  117. struct cfi_ident {
  118. uint8_t qry[3];
  119. uint16_t P_ID;
  120. uint16_t P_ADR;
  121. uint16_t A_ID;
  122. uint16_t A_ADR;
  123. uint8_t VccMin;
  124. uint8_t VccMax;
  125. uint8_t VppMin;
  126. uint8_t VppMax;
  127. uint8_t WordWriteTimeoutTyp;
  128. uint8_t BufWriteTimeoutTyp;
  129. uint8_t BlockEraseTimeoutTyp;
  130. uint8_t ChipEraseTimeoutTyp;
  131. uint8_t WordWriteTimeoutMax;
  132. uint8_t BufWriteTimeoutMax;
  133. uint8_t BlockEraseTimeoutMax;
  134. uint8_t ChipEraseTimeoutMax;
  135. uint8_t DevSize;
  136. uint16_t InterfaceDesc;
  137. uint16_t MaxBufWriteSize;
  138. uint8_t NumEraseRegions;
  139. uint32_t EraseRegionInfo[0]; /* Not host ordered */
  140. } __packed;
  141. /* Extended Query Structure for both PRI and ALT */
  142. struct cfi_extquery {
  143. uint8_t pri[3];
  144. uint8_t MajorVersion;
  145. uint8_t MinorVersion;
  146. } __packed;
  147. /* Vendor-Specific PRI for Intel/Sharp Extended Command Set (0x0001) */
  148. struct cfi_pri_intelext {
  149. uint8_t pri[3];
  150. uint8_t MajorVersion;
  151. uint8_t MinorVersion;
  152. uint32_t FeatureSupport; /* if bit 31 is set then an additional uint32_t feature
  153. block follows - FIXME - not currently supported */
  154. uint8_t SuspendCmdSupport;
  155. uint16_t BlkStatusRegMask;
  156. uint8_t VccOptimal;
  157. uint8_t VppOptimal;
  158. uint8_t NumProtectionFields;
  159. uint16_t ProtRegAddr;
  160. uint8_t FactProtRegSize;
  161. uint8_t UserProtRegSize;
  162. uint8_t extra[0];
  163. } __packed;
  164. struct cfi_intelext_otpinfo {
  165. uint32_t ProtRegAddr;
  166. uint16_t FactGroups;
  167. uint8_t FactProtRegSize;
  168. uint16_t UserGroups;
  169. uint8_t UserProtRegSize;
  170. } __packed;
  171. struct cfi_intelext_blockinfo {
  172. uint16_t NumIdentBlocks;
  173. uint16_t BlockSize;
  174. uint16_t MinBlockEraseCycles;
  175. uint8_t BitsPerCell;
  176. uint8_t BlockCap;
  177. } __packed;
  178. struct cfi_intelext_regioninfo {
  179. uint16_t NumIdentPartitions;
  180. uint8_t NumOpAllowed;
  181. uint8_t NumOpAllowedSimProgMode;
  182. uint8_t NumOpAllowedSimEraMode;
  183. uint8_t NumBlockTypes;
  184. struct cfi_intelext_blockinfo BlockTypes[1];
  185. } __packed;
  186. struct cfi_intelext_programming_regioninfo {
  187. uint8_t ProgRegShift;
  188. uint8_t Reserved1;
  189. uint8_t ControlValid;
  190. uint8_t Reserved2;
  191. uint8_t ControlInvalid;
  192. uint8_t Reserved3;
  193. } __packed;
  194. /* Vendor-Specific PRI for AMD/Fujitsu Extended Command Set (0x0002) */
  195. struct cfi_pri_amdstd {
  196. uint8_t pri[3];
  197. uint8_t MajorVersion;
  198. uint8_t MinorVersion;
  199. uint8_t SiliconRevision; /* bits 1-0: Address Sensitive Unlock */
  200. uint8_t EraseSuspend;
  201. uint8_t BlkProt;
  202. uint8_t TmpBlkUnprotect;
  203. uint8_t BlkProtUnprot;
  204. uint8_t SimultaneousOps;
  205. uint8_t BurstMode;
  206. uint8_t PageMode;
  207. uint8_t VppMin;
  208. uint8_t VppMax;
  209. uint8_t TopBottom;
  210. } __packed;
  211. /* Vendor-Specific PRI for Atmel chips (command set 0x0002) */
  212. struct cfi_pri_atmel {
  213. uint8_t pri[3];
  214. uint8_t MajorVersion;
  215. uint8_t MinorVersion;
  216. uint8_t Features;
  217. uint8_t BottomBoot;
  218. uint8_t BurstMode;
  219. uint8_t PageMode;
  220. } __packed;
  221. struct cfi_pri_query {
  222. uint8_t NumFields;
  223. uint32_t ProtField[1]; /* Not host ordered */
  224. } __packed;
  225. struct cfi_bri_query {
  226. uint8_t PageModeReadCap;
  227. uint8_t NumFields;
  228. uint32_t ConfField[1]; /* Not host ordered */
  229. } __packed;
  230. #define P_ID_NONE 0x0000
  231. #define P_ID_INTEL_EXT 0x0001
  232. #define P_ID_AMD_STD 0x0002
  233. #define P_ID_INTEL_STD 0x0003
  234. #define P_ID_AMD_EXT 0x0004
  235. #define P_ID_WINBOND 0x0006
  236. #define P_ID_ST_ADV 0x0020
  237. #define P_ID_MITSUBISHI_STD 0x0100
  238. #define P_ID_MITSUBISHI_EXT 0x0101
  239. #define P_ID_SST_PAGE 0x0102
  240. #define P_ID_SST_OLD 0x0701
  241. #define P_ID_INTEL_PERFORMANCE 0x0200
  242. #define P_ID_INTEL_DATA 0x0210
  243. #define P_ID_RESERVED 0xffff
  244. #define CFI_MODE_CFI 1
  245. #define CFI_MODE_JEDEC 0
  246. struct cfi_private {
  247. uint16_t cmdset;
  248. void *cmdset_priv;
  249. int interleave;
  250. int device_type;
  251. int cfi_mode; /* Are we a JEDEC device pretending to be CFI? */
  252. int addr_unlock1;
  253. int addr_unlock2;
  254. struct mtd_info *(*cmdset_setup)(struct map_info *);
  255. struct cfi_ident *cfiq; /* For now only one. We insist that all devs
  256. must be of the same type. */
  257. int mfr, id;
  258. int numchips;
  259. map_word sector_erase_cmd;
  260. unsigned long chipshift; /* Because they're of the same type */
  261. const char *im_name; /* inter_module name for cmdset_setup */
  262. struct flchip chips[0]; /* per-chip data structure for each chip */
  263. };
  264. uint32_t cfi_build_cmd_addr(uint32_t cmd_ofs,
  265. struct map_info *map, struct cfi_private *cfi);
  266. map_word cfi_build_cmd(u_long cmd, struct map_info *map, struct cfi_private *cfi);
  267. #define CMD(x) cfi_build_cmd((x), map, cfi)
  268. unsigned long cfi_merge_status(map_word val, struct map_info *map,
  269. struct cfi_private *cfi);
  270. #define MERGESTATUS(x) cfi_merge_status((x), map, cfi)
  271. uint32_t cfi_send_gen_cmd(u_char cmd, uint32_t cmd_addr, uint32_t base,
  272. struct map_info *map, struct cfi_private *cfi,
  273. int type, map_word *prev_val);
  274. static inline uint8_t cfi_read_query(struct map_info *map, uint32_t addr)
  275. {
  276. map_word val = map_read(map, addr);
  277. if (map_bankwidth_is_1(map)) {
  278. return val.x[0];
  279. } else if (map_bankwidth_is_2(map)) {
  280. return cfi16_to_cpu(map, val.x[0]);
  281. } else {
  282. /* No point in a 64-bit byteswap since that would just be
  283. swapping the responses from different chips, and we are
  284. only interested in one chip (a representative sample) */
  285. return cfi32_to_cpu(map, val.x[0]);
  286. }
  287. }
  288. static inline uint16_t cfi_read_query16(struct map_info *map, uint32_t addr)
  289. {
  290. map_word val = map_read(map, addr);
  291. if (map_bankwidth_is_1(map)) {
  292. return val.x[0] & 0xff;
  293. } else if (map_bankwidth_is_2(map)) {
  294. return cfi16_to_cpu(map, val.x[0]);
  295. } else {
  296. /* No point in a 64-bit byteswap since that would just be
  297. swapping the responses from different chips, and we are
  298. only interested in one chip (a representative sample) */
  299. return cfi32_to_cpu(map, val.x[0]);
  300. }
  301. }
  302. void cfi_udelay(int us);
  303. int __xipram cfi_qry_present(struct map_info *map, __u32 base,
  304. struct cfi_private *cfi);
  305. int __xipram cfi_qry_mode_on(uint32_t base, struct map_info *map,
  306. struct cfi_private *cfi);
  307. void __xipram cfi_qry_mode_off(uint32_t base, struct map_info *map,
  308. struct cfi_private *cfi);
  309. struct cfi_extquery *cfi_read_pri(struct map_info *map, uint16_t adr, uint16_t size,
  310. const char* name);
  311. struct cfi_fixup {
  312. uint16_t mfr;
  313. uint16_t id;
  314. void (*fixup)(struct mtd_info *mtd);
  315. };
  316. #define CFI_MFR_ANY 0xFFFF
  317. #define CFI_ID_ANY 0xFFFF
  318. #define CFI_MFR_CONTINUATION 0x007F
  319. #define CFI_MFR_AMD 0x0001
  320. #define CFI_MFR_AMIC 0x0037
  321. #define CFI_MFR_ATMEL 0x001F
  322. #define CFI_MFR_EON 0x001C
  323. #define CFI_MFR_FUJITSU 0x0004
  324. #define CFI_MFR_HYUNDAI 0x00AD
  325. #define CFI_MFR_INTEL 0x0089
  326. #define CFI_MFR_MACRONIX 0x00C2
  327. #define CFI_MFR_NEC 0x0010
  328. #define CFI_MFR_PMC 0x009D
  329. #define CFI_MFR_SAMSUNG 0x00EC
  330. #define CFI_MFR_SHARP 0x00B0
  331. #define CFI_MFR_SST 0x00BF
  332. #define CFI_MFR_ST 0x0020 /* STMicroelectronics */
  333. #define CFI_MFR_TOSHIBA 0x0098
  334. #define CFI_MFR_WINBOND 0x00DA
  335. void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup* fixups);
  336. typedef int (*varsize_frob_t)(struct map_info *map, struct flchip *chip,
  337. unsigned long adr, int len, void *thunk);
  338. int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
  339. loff_t ofs, size_t len, void *thunk);
  340. #endif /* __MTD_CFI_H__ */