cfi.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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/interrupt.h>
  24. #include <linux/mtd/flashchip.h>
  25. #include <linux/mtd/map.h>
  26. #include <linux/mtd/cfi_endian.h>
  27. #include <linux/mtd/xip.h>
  28. #ifdef CONFIG_MTD_CFI_I1
  29. #define cfi_interleave(cfi) 1
  30. #define cfi_interleave_is_1(cfi) (cfi_interleave(cfi) == 1)
  31. #else
  32. #define cfi_interleave_is_1(cfi) (0)
  33. #endif
  34. #ifdef CONFIG_MTD_CFI_I2
  35. # ifdef cfi_interleave
  36. # undef cfi_interleave
  37. # define cfi_interleave(cfi) ((cfi)->interleave)
  38. # else
  39. # define cfi_interleave(cfi) 2
  40. # endif
  41. #define cfi_interleave_is_2(cfi) (cfi_interleave(cfi) == 2)
  42. #else
  43. #define cfi_interleave_is_2(cfi) (0)
  44. #endif
  45. #ifdef CONFIG_MTD_CFI_I4
  46. # ifdef cfi_interleave
  47. # undef cfi_interleave
  48. # define cfi_interleave(cfi) ((cfi)->interleave)
  49. # else
  50. # define cfi_interleave(cfi) 4
  51. # endif
  52. #define cfi_interleave_is_4(cfi) (cfi_interleave(cfi) == 4)
  53. #else
  54. #define cfi_interleave_is_4(cfi) (0)
  55. #endif
  56. #ifdef CONFIG_MTD_CFI_I8
  57. # ifdef cfi_interleave
  58. # undef cfi_interleave
  59. # define cfi_interleave(cfi) ((cfi)->interleave)
  60. # else
  61. # define cfi_interleave(cfi) 8
  62. # endif
  63. #define cfi_interleave_is_8(cfi) (cfi_interleave(cfi) == 8)
  64. #else
  65. #define cfi_interleave_is_8(cfi) (0)
  66. #endif
  67. #ifndef cfi_interleave
  68. #warning No CONFIG_MTD_CFI_Ix selected. No NOR chip support can work.
  69. static inline int cfi_interleave(void *cfi)
  70. {
  71. BUG();
  72. return 0;
  73. }
  74. #endif
  75. static inline int cfi_interleave_supported(int i)
  76. {
  77. switch (i) {
  78. #ifdef CONFIG_MTD_CFI_I1
  79. case 1:
  80. #endif
  81. #ifdef CONFIG_MTD_CFI_I2
  82. case 2:
  83. #endif
  84. #ifdef CONFIG_MTD_CFI_I4
  85. case 4:
  86. #endif
  87. #ifdef CONFIG_MTD_CFI_I8
  88. case 8:
  89. #endif
  90. return 1;
  91. default:
  92. return 0;
  93. }
  94. }
  95. /* NB: these values must represents the number of bytes needed to meet the
  96. * device type (x8, x16, x32). Eg. a 32 bit device is 4 x 8 bytes.
  97. * These numbers are used in calculations.
  98. */
  99. #define CFI_DEVICETYPE_X8 (8 / 8)
  100. #define CFI_DEVICETYPE_X16 (16 / 8)
  101. #define CFI_DEVICETYPE_X32 (32 / 8)
  102. #define CFI_DEVICETYPE_X64 (64 / 8)
  103. /* Device Interface Code Assignments from the "Common Flash Memory Interface
  104. * Publication 100" dated December 1, 2001.
  105. */
  106. #define CFI_INTERFACE_X8_ASYNC 0x0000
  107. #define CFI_INTERFACE_X16_ASYNC 0x0001
  108. #define CFI_INTERFACE_X8_BY_X16_ASYNC 0x0002
  109. #define CFI_INTERFACE_X32_ASYNC 0x0003
  110. #define CFI_INTERFACE_X16_BY_X32_ASYNC 0x0005
  111. #define CFI_INTERFACE_NOT_ALLOWED 0xffff
  112. /* NB: We keep these structures in memory in HOST byteorder, except
  113. * where individually noted.
  114. */
  115. /* Basic Query Structure */
  116. struct cfi_ident {
  117. uint8_t qry[3];
  118. uint16_t P_ID;
  119. uint16_t P_ADR;
  120. uint16_t A_ID;
  121. uint16_t A_ADR;
  122. uint8_t VccMin;
  123. uint8_t VccMax;
  124. uint8_t VppMin;
  125. uint8_t VppMax;
  126. uint8_t WordWriteTimeoutTyp;
  127. uint8_t BufWriteTimeoutTyp;
  128. uint8_t BlockEraseTimeoutTyp;
  129. uint8_t ChipEraseTimeoutTyp;
  130. uint8_t WordWriteTimeoutMax;
  131. uint8_t BufWriteTimeoutMax;
  132. uint8_t BlockEraseTimeoutMax;
  133. uint8_t ChipEraseTimeoutMax;
  134. uint8_t DevSize;
  135. uint16_t InterfaceDesc;
  136. uint16_t MaxBufWriteSize;
  137. uint8_t NumEraseRegions;
  138. uint32_t EraseRegionInfo[0]; /* Not host ordered */
  139. } __attribute__((packed));
  140. /* Extended Query Structure for both PRI and ALT */
  141. struct cfi_extquery {
  142. uint8_t pri[3];
  143. uint8_t MajorVersion;
  144. uint8_t MinorVersion;
  145. } __attribute__((packed));
  146. /* Vendor-Specific PRI for Intel/Sharp Extended Command Set (0x0001) */
  147. struct cfi_pri_intelext {
  148. uint8_t pri[3];
  149. uint8_t MajorVersion;
  150. uint8_t MinorVersion;
  151. uint32_t FeatureSupport; /* if bit 31 is set then an additional uint32_t feature
  152. block follows - FIXME - not currently supported */
  153. uint8_t SuspendCmdSupport;
  154. uint16_t BlkStatusRegMask;
  155. uint8_t VccOptimal;
  156. uint8_t VppOptimal;
  157. uint8_t NumProtectionFields;
  158. uint16_t ProtRegAddr;
  159. uint8_t FactProtRegSize;
  160. uint8_t UserProtRegSize;
  161. uint8_t extra[0];
  162. } __attribute__((packed));
  163. struct cfi_intelext_otpinfo {
  164. uint32_t ProtRegAddr;
  165. uint16_t FactGroups;
  166. uint8_t FactProtRegSize;
  167. uint16_t UserGroups;
  168. uint8_t UserProtRegSize;
  169. } __attribute__((packed));
  170. struct cfi_intelext_blockinfo {
  171. uint16_t NumIdentBlocks;
  172. uint16_t BlockSize;
  173. uint16_t MinBlockEraseCycles;
  174. uint8_t BitsPerCell;
  175. uint8_t BlockCap;
  176. } __attribute__((packed));
  177. struct cfi_intelext_regioninfo {
  178. uint16_t NumIdentPartitions;
  179. uint8_t NumOpAllowed;
  180. uint8_t NumOpAllowedSimProgMode;
  181. uint8_t NumOpAllowedSimEraMode;
  182. uint8_t NumBlockTypes;
  183. struct cfi_intelext_blockinfo BlockTypes[1];
  184. } __attribute__((packed));
  185. struct cfi_intelext_programming_regioninfo {
  186. uint8_t ProgRegShift;
  187. uint8_t Reserved1;
  188. uint8_t ControlValid;
  189. uint8_t Reserved2;
  190. uint8_t ControlInvalid;
  191. uint8_t Reserved3;
  192. } __attribute__((packed));
  193. /* Vendor-Specific PRI for AMD/Fujitsu Extended Command Set (0x0002) */
  194. struct cfi_pri_amdstd {
  195. uint8_t pri[3];
  196. uint8_t MajorVersion;
  197. uint8_t MinorVersion;
  198. uint8_t SiliconRevision; /* bits 1-0: Address Sensitive Unlock */
  199. uint8_t EraseSuspend;
  200. uint8_t BlkProt;
  201. uint8_t TmpBlkUnprotect;
  202. uint8_t BlkProtUnprot;
  203. uint8_t SimultaneousOps;
  204. uint8_t BurstMode;
  205. uint8_t PageMode;
  206. uint8_t VppMin;
  207. uint8_t VppMax;
  208. uint8_t TopBottom;
  209. } __attribute__((packed));
  210. /* Vendor-Specific PRI for Atmel chips (command set 0x0002) */
  211. struct cfi_pri_atmel {
  212. uint8_t pri[3];
  213. uint8_t MajorVersion;
  214. uint8_t MinorVersion;
  215. uint8_t Features;
  216. uint8_t BottomBoot;
  217. uint8_t BurstMode;
  218. uint8_t PageMode;
  219. } __attribute__((packed));
  220. struct cfi_pri_query {
  221. uint8_t NumFields;
  222. uint32_t ProtField[1]; /* Not host ordered */
  223. } __attribute__((packed));
  224. struct cfi_bri_query {
  225. uint8_t PageModeReadCap;
  226. uint8_t NumFields;
  227. uint32_t ConfField[1]; /* Not host ordered */
  228. } __attribute__((packed));
  229. #define P_ID_NONE 0x0000
  230. #define P_ID_INTEL_EXT 0x0001
  231. #define P_ID_AMD_STD 0x0002
  232. #define P_ID_INTEL_STD 0x0003
  233. #define P_ID_AMD_EXT 0x0004
  234. #define P_ID_WINBOND 0x0006
  235. #define P_ID_ST_ADV 0x0020
  236. #define P_ID_MITSUBISHI_STD 0x0100
  237. #define P_ID_MITSUBISHI_EXT 0x0101
  238. #define P_ID_SST_PAGE 0x0102
  239. #define P_ID_SST_OLD 0x0701
  240. #define P_ID_INTEL_PERFORMANCE 0x0200
  241. #define P_ID_INTEL_DATA 0x0210
  242. #define P_ID_RESERVED 0xffff
  243. #define CFI_MODE_CFI 1
  244. #define CFI_MODE_JEDEC 0
  245. struct cfi_private {
  246. uint16_t cmdset;
  247. void *cmdset_priv;
  248. int interleave;
  249. int device_type;
  250. int cfi_mode; /* Are we a JEDEC device pretending to be CFI? */
  251. int addr_unlock1;
  252. int addr_unlock2;
  253. struct mtd_info *(*cmdset_setup)(struct map_info *);
  254. struct cfi_ident *cfiq; /* For now only one. We insist that all devs
  255. must be of the same type. */
  256. int mfr, id;
  257. int numchips;
  258. map_word sector_erase_cmd;
  259. unsigned long chipshift; /* Because they're of the same type */
  260. const char *im_name; /* inter_module name for cmdset_setup */
  261. struct flchip chips[0]; /* per-chip data structure for each chip */
  262. };
  263. /*
  264. * Returns the command address according to the given geometry.
  265. */
  266. static inline uint32_t cfi_build_cmd_addr(uint32_t cmd_ofs,
  267. struct map_info *map, struct cfi_private *cfi)
  268. {
  269. unsigned bankwidth = map_bankwidth(map);
  270. unsigned interleave = cfi_interleave(cfi);
  271. unsigned type = cfi->device_type;
  272. uint32_t addr;
  273. addr = (cmd_ofs * type) * interleave;
  274. /* Modify the unlock address if we are in compatibility mode.
  275. * For 16bit devices on 8 bit busses
  276. * and 32bit devices on 16 bit busses
  277. * set the low bit of the alternating bit sequence of the address.
  278. */
  279. if (((type * interleave) > bankwidth) && ((cmd_ofs & 0xff) == 0xaa))
  280. addr |= (type >> 1)*interleave;
  281. return addr;
  282. }
  283. /*
  284. * Transforms the CFI command for the given geometry (bus width & interleave).
  285. * It looks too long to be inline, but in the common case it should almost all
  286. * get optimised away.
  287. */
  288. static inline map_word cfi_build_cmd(u_long cmd, struct map_info *map, struct cfi_private *cfi)
  289. {
  290. map_word val = { {0} };
  291. int wordwidth, words_per_bus, chip_mode, chips_per_word;
  292. unsigned long onecmd;
  293. int i;
  294. /* We do it this way to give the compiler a fighting chance
  295. of optimising away all the crap for 'bankwidth' larger than
  296. an unsigned long, in the common case where that support is
  297. disabled */
  298. if (map_bankwidth_is_large(map)) {
  299. wordwidth = sizeof(unsigned long);
  300. words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
  301. } else {
  302. wordwidth = map_bankwidth(map);
  303. words_per_bus = 1;
  304. }
  305. chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
  306. chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
  307. /* First, determine what the bit-pattern should be for a single
  308. device, according to chip mode and endianness... */
  309. switch (chip_mode) {
  310. default: BUG();
  311. case 1:
  312. onecmd = cmd;
  313. break;
  314. case 2:
  315. onecmd = cpu_to_cfi16(map, cmd);
  316. break;
  317. case 4:
  318. onecmd = cpu_to_cfi32(map, cmd);
  319. break;
  320. }
  321. /* Now replicate it across the size of an unsigned long, or
  322. just to the bus width as appropriate */
  323. switch (chips_per_word) {
  324. default: BUG();
  325. #if BITS_PER_LONG >= 64
  326. case 8:
  327. onecmd |= (onecmd << (chip_mode * 32));
  328. #endif
  329. case 4:
  330. onecmd |= (onecmd << (chip_mode * 16));
  331. case 2:
  332. onecmd |= (onecmd << (chip_mode * 8));
  333. case 1:
  334. ;
  335. }
  336. /* And finally, for the multi-word case, replicate it
  337. in all words in the structure */
  338. for (i=0; i < words_per_bus; i++) {
  339. val.x[i] = onecmd;
  340. }
  341. return val;
  342. }
  343. #define CMD(x) cfi_build_cmd((x), map, cfi)
  344. static inline unsigned long cfi_merge_status(map_word val, struct map_info *map,
  345. struct cfi_private *cfi)
  346. {
  347. int wordwidth, words_per_bus, chip_mode, chips_per_word;
  348. unsigned long onestat, res = 0;
  349. int i;
  350. /* We do it this way to give the compiler a fighting chance
  351. of optimising away all the crap for 'bankwidth' larger than
  352. an unsigned long, in the common case where that support is
  353. disabled */
  354. if (map_bankwidth_is_large(map)) {
  355. wordwidth = sizeof(unsigned long);
  356. words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
  357. } else {
  358. wordwidth = map_bankwidth(map);
  359. words_per_bus = 1;
  360. }
  361. chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
  362. chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
  363. onestat = val.x[0];
  364. /* Or all status words together */
  365. for (i=1; i < words_per_bus; i++) {
  366. onestat |= val.x[i];
  367. }
  368. res = onestat;
  369. switch(chips_per_word) {
  370. default: BUG();
  371. #if BITS_PER_LONG >= 64
  372. case 8:
  373. res |= (onestat >> (chip_mode * 32));
  374. #endif
  375. case 4:
  376. res |= (onestat >> (chip_mode * 16));
  377. case 2:
  378. res |= (onestat >> (chip_mode * 8));
  379. case 1:
  380. ;
  381. }
  382. /* Last, determine what the bit-pattern should be for a single
  383. device, according to chip mode and endianness... */
  384. switch (chip_mode) {
  385. case 1:
  386. break;
  387. case 2:
  388. res = cfi16_to_cpu(map, res);
  389. break;
  390. case 4:
  391. res = cfi32_to_cpu(map, res);
  392. break;
  393. default: BUG();
  394. }
  395. return res;
  396. }
  397. #define MERGESTATUS(x) cfi_merge_status((x), map, cfi)
  398. /*
  399. * Sends a CFI command to a bank of flash for the given geometry.
  400. *
  401. * Returns the offset in flash where the command was written.
  402. * If prev_val is non-null, it will be set to the value at the command address,
  403. * before the command was written.
  404. */
  405. static inline uint32_t cfi_send_gen_cmd(u_char cmd, uint32_t cmd_addr, uint32_t base,
  406. struct map_info *map, struct cfi_private *cfi,
  407. int type, map_word *prev_val)
  408. {
  409. map_word val;
  410. uint32_t addr = base + cfi_build_cmd_addr(cmd_addr, map, cfi);
  411. val = cfi_build_cmd(cmd, map, cfi);
  412. if (prev_val)
  413. *prev_val = map_read(map, addr);
  414. map_write(map, val, addr);
  415. return addr - base;
  416. }
  417. static inline uint8_t cfi_read_query(struct map_info *map, uint32_t addr)
  418. {
  419. map_word val = map_read(map, addr);
  420. if (map_bankwidth_is_1(map)) {
  421. return val.x[0];
  422. } else if (map_bankwidth_is_2(map)) {
  423. return cfi16_to_cpu(map, val.x[0]);
  424. } else {
  425. /* No point in a 64-bit byteswap since that would just be
  426. swapping the responses from different chips, and we are
  427. only interested in one chip (a representative sample) */
  428. return cfi32_to_cpu(map, val.x[0]);
  429. }
  430. }
  431. static inline uint16_t cfi_read_query16(struct map_info *map, uint32_t addr)
  432. {
  433. map_word val = map_read(map, addr);
  434. if (map_bankwidth_is_1(map)) {
  435. return val.x[0] & 0xff;
  436. } else if (map_bankwidth_is_2(map)) {
  437. return cfi16_to_cpu(map, val.x[0]);
  438. } else {
  439. /* No point in a 64-bit byteswap since that would just be
  440. swapping the responses from different chips, and we are
  441. only interested in one chip (a representative sample) */
  442. return cfi32_to_cpu(map, val.x[0]);
  443. }
  444. }
  445. static inline void cfi_udelay(int us)
  446. {
  447. if (us >= 1000) {
  448. msleep((us+999)/1000);
  449. } else {
  450. udelay(us);
  451. cond_resched();
  452. }
  453. }
  454. int __xipram cfi_qry_present(struct map_info *map, __u32 base,
  455. struct cfi_private *cfi);
  456. int __xipram cfi_qry_mode_on(uint32_t base, struct map_info *map,
  457. struct cfi_private *cfi);
  458. void __xipram cfi_qry_mode_off(uint32_t base, struct map_info *map,
  459. struct cfi_private *cfi);
  460. struct cfi_extquery *cfi_read_pri(struct map_info *map, uint16_t adr, uint16_t size,
  461. const char* name);
  462. struct cfi_fixup {
  463. uint16_t mfr;
  464. uint16_t id;
  465. void (*fixup)(struct mtd_info *mtd);
  466. };
  467. #define CFI_MFR_ANY 0xFFFF
  468. #define CFI_ID_ANY 0xFFFF
  469. #define CFI_MFR_CONTINUATION 0x007F
  470. #define CFI_MFR_AMD 0x0001
  471. #define CFI_MFR_AMIC 0x0037
  472. #define CFI_MFR_ATMEL 0x001F
  473. #define CFI_MFR_EON 0x001C
  474. #define CFI_MFR_FUJITSU 0x0004
  475. #define CFI_MFR_HYUNDAI 0x00AD
  476. #define CFI_MFR_INTEL 0x0089
  477. #define CFI_MFR_MACRONIX 0x00C2
  478. #define CFI_MFR_NEC 0x0010
  479. #define CFI_MFR_PMC 0x009D
  480. #define CFI_MFR_SAMSUNG 0x00EC
  481. #define CFI_MFR_SHARP 0x00B0
  482. #define CFI_MFR_SST 0x00BF
  483. #define CFI_MFR_ST 0x0020 /* STMicroelectronics */
  484. #define CFI_MFR_TOSHIBA 0x0098
  485. #define CFI_MFR_WINBOND 0x00DA
  486. void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup* fixups);
  487. typedef int (*varsize_frob_t)(struct map_info *map, struct flchip *chip,
  488. unsigned long adr, int len, void *thunk);
  489. int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
  490. loff_t ofs, size_t len, void *thunk);
  491. #endif /* __MTD_CFI_H__ */