macronix.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2005, Intec Automation Inc.
  4. * Copyright (C) 2014, Freescale Semiconductor, Inc.
  5. */
  6. #include <linux/mtd/spi-nor.h>
  7. #include "core.h"
  8. #define MXIC_NOR_OP_RD_CR2 0x71 /* Read configuration register 2 opcode */
  9. #define MXIC_NOR_OP_WR_CR2 0x72 /* Write configuration register 2 opcode */
  10. #define MXIC_NOR_ADDR_CR2_MODE 0x00000000 /* CR2 address for setting spi/sopi/dopi mode */
  11. #define MXIC_NOR_ADDR_CR2_DC 0x00000300 /* CR2 address for setting dummy cycles */
  12. #define MXIC_NOR_REG_DOPI_EN 0x2 /* Enable Octal DTR */
  13. #define MXIC_NOR_REG_SPI_EN 0x0 /* Enable SPI */
  14. /* Convert dummy cycles to bit pattern */
  15. #define MXIC_NOR_REG_DC(p) \
  16. ((20 - (p)) >> 1)
  17. #define MXIC_NOR_WR_CR2(addr, ndata, buf) \
  18. SPI_MEM_OP(SPI_MEM_OP_CMD(MXIC_NOR_OP_WR_CR2, 0), \
  19. SPI_MEM_OP_ADDR(4, addr, 0), \
  20. SPI_MEM_OP_NO_DUMMY, \
  21. SPI_MEM_OP_DATA_OUT(ndata, buf, 0))
  22. static int
  23. mx25l25635_post_bfpt_fixups(struct spi_nor *nor,
  24. const struct sfdp_parameter_header *bfpt_header,
  25. const struct sfdp_bfpt *bfpt)
  26. {
  27. /*
  28. * MX25L25635F supports 4B opcodes but MX25L25635E does not.
  29. * Unfortunately, Macronix has re-used the same JEDEC ID for both
  30. * variants which prevents us from defining a new entry in the parts
  31. * table.
  32. * We need a way to differentiate MX25L25635E and MX25L25635F, and it
  33. * seems that the F version advertises support for Fast Read 4-4-4 in
  34. * its BFPT table.
  35. */
  36. if (bfpt->dwords[SFDP_DWORD(5)] & BFPT_DWORD5_FAST_READ_4_4_4)
  37. nor->flags |= SNOR_F_4B_OPCODES;
  38. return 0;
  39. }
  40. static int
  41. macronix_qpp4b_post_sfdp_fixups(struct spi_nor *nor)
  42. {
  43. /* PP_1_1_4_4B is supported but missing in 4BAIT. */
  44. struct spi_nor_flash_parameter *params = nor->params;
  45. params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
  46. spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
  47. SPINOR_OP_PP_1_1_4_4B, SNOR_PROTO_1_1_4);
  48. return 0;
  49. }
  50. static int
  51. mx25l3255e_late_init_fixups(struct spi_nor *nor)
  52. {
  53. struct spi_nor_flash_parameter *params = nor->params;
  54. /*
  55. * SFDP of MX25L3255E is JESD216, which does not include the Quad
  56. * Enable bit Requirement in BFPT. As a result, during BFPT parsing,
  57. * the quad_enable method is not set to spi_nor_sr1_bit6_quad_enable.
  58. * Therefore, it is necessary to correct this setting by late_init.
  59. */
  60. params->quad_enable = spi_nor_sr1_bit6_quad_enable;
  61. /*
  62. * In addition, MX25L3255E also supports 1-4-4 page program in 3-byte
  63. * address mode. However, since the 3-byte address 1-4-4 page program
  64. * is not defined in SFDP, it needs to be configured in late_init.
  65. */
  66. params->hwcaps.mask |= SNOR_HWCAPS_PP_1_4_4;
  67. spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_4_4],
  68. SPINOR_OP_PP_1_4_4, SNOR_PROTO_1_4_4);
  69. return 0;
  70. }
  71. static const struct spi_nor_fixups mx25l25635_fixups = {
  72. .post_bfpt = mx25l25635_post_bfpt_fixups,
  73. .post_sfdp = macronix_qpp4b_post_sfdp_fixups,
  74. };
  75. static const struct spi_nor_fixups macronix_qpp4b_fixups = {
  76. .post_sfdp = macronix_qpp4b_post_sfdp_fixups,
  77. };
  78. static const struct spi_nor_fixups mx25l3255e_fixups = {
  79. .late_init = mx25l3255e_late_init_fixups,
  80. };
  81. static const struct flash_info macronix_nor_parts[] = {
  82. {
  83. .id = SNOR_ID(0xc2, 0x20, 0x10),
  84. .name = "mx25l512e",
  85. .size = SZ_64K,
  86. .no_sfdp_flags = SECT_4K,
  87. }, {
  88. .id = SNOR_ID(0xc2, 0x20, 0x12),
  89. .name = "mx25l2005a",
  90. .size = SZ_256K,
  91. .no_sfdp_flags = SECT_4K,
  92. }, {
  93. .id = SNOR_ID(0xc2, 0x20, 0x13),
  94. .name = "mx25l4005a",
  95. .size = SZ_512K,
  96. .no_sfdp_flags = SECT_4K,
  97. }, {
  98. .id = SNOR_ID(0xc2, 0x20, 0x14),
  99. .name = "mx25l8005",
  100. .size = SZ_1M,
  101. }, {
  102. /* MX25L1606E */
  103. .id = SNOR_ID(0xc2, 0x20, 0x15),
  104. }, {
  105. .id = SNOR_ID(0xc2, 0x20, 0x16),
  106. .name = "mx25l3205d",
  107. .size = SZ_4M,
  108. .no_sfdp_flags = SECT_4K,
  109. }, {
  110. .id = SNOR_ID(0xc2, 0x20, 0x17),
  111. .name = "mx25l6405d",
  112. .size = SZ_8M,
  113. .no_sfdp_flags = SECT_4K,
  114. }, {
  115. /* MX25L12805D */
  116. .id = SNOR_ID(0xc2, 0x20, 0x18),
  117. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_4BIT_BP,
  118. }, {
  119. /* MX25L25635E, MX25L25645G */
  120. .id = SNOR_ID(0xc2, 0x20, 0x19),
  121. .fixups = &mx25l25635_fixups
  122. }, {
  123. /* MX66L51235F */
  124. .id = SNOR_ID(0xc2, 0x20, 0x1a),
  125. .fixup_flags = SPI_NOR_4B_OPCODES,
  126. .fixups = &macronix_qpp4b_fixups,
  127. }, {
  128. /* MX66L1G45G */
  129. .id = SNOR_ID(0xc2, 0x20, 0x1b),
  130. .fixups = &macronix_qpp4b_fixups,
  131. }, {
  132. /* MX66L2G45G */
  133. .id = SNOR_ID(0xc2, 0x20, 0x1c),
  134. .fixups = &macronix_qpp4b_fixups,
  135. }, {
  136. .id = SNOR_ID(0xc2, 0x23, 0x14),
  137. .name = "mx25v8035f",
  138. .size = SZ_1M,
  139. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  140. }, {
  141. .id = SNOR_ID(0xc2, 0x25, 0x32),
  142. .name = "mx25u2033e",
  143. .size = SZ_256K,
  144. .no_sfdp_flags = SECT_4K,
  145. }, {
  146. .id = SNOR_ID(0xc2, 0x25, 0x33),
  147. .name = "mx25u4035",
  148. .size = SZ_512K,
  149. .no_sfdp_flags = SECT_4K,
  150. }, {
  151. .id = SNOR_ID(0xc2, 0x25, 0x34),
  152. .name = "mx25u8035",
  153. .size = SZ_1M,
  154. .no_sfdp_flags = SECT_4K,
  155. }, {
  156. .id = SNOR_ID(0xc2, 0x25, 0x36),
  157. .name = "mx25u3235f",
  158. .size = SZ_4M,
  159. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  160. }, {
  161. .id = SNOR_ID(0xc2, 0x25, 0x37),
  162. .name = "mx25u6435f",
  163. .size = SZ_8M,
  164. .no_sfdp_flags = SECT_4K,
  165. }, {
  166. .id = SNOR_ID(0xc2, 0x25, 0x38),
  167. .name = "mx25u12835f",
  168. .size = SZ_16M,
  169. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  170. }, {
  171. /* MX25U51245G */
  172. .id = SNOR_ID(0xc2, 0x25, 0x3a),
  173. .fixups = &macronix_qpp4b_fixups,
  174. }, {
  175. /* MX66U1G45G */
  176. .id = SNOR_ID(0xc2, 0x25, 0x3b),
  177. .fixups = &macronix_qpp4b_fixups,
  178. }, {
  179. /* MX66U2G45G */
  180. .id = SNOR_ID(0xc2, 0x25, 0x3c),
  181. .fixups = &macronix_qpp4b_fixups,
  182. }, {
  183. .id = SNOR_ID(0xc2, 0x26, 0x18),
  184. .name = "mx25l12855e",
  185. .size = SZ_16M,
  186. }, {
  187. .id = SNOR_ID(0xc2, 0x26, 0x19),
  188. .name = "mx25l25655e",
  189. .size = SZ_32M,
  190. }, {
  191. .id = SNOR_ID(0xc2, 0x26, 0x1b),
  192. .name = "mx66l1g55g",
  193. .size = SZ_128M,
  194. .no_sfdp_flags = SPI_NOR_QUAD_READ,
  195. }, {
  196. .id = SNOR_ID(0xc2, 0x28, 0x15),
  197. .name = "mx25r1635f",
  198. .size = SZ_2M,
  199. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  200. }, {
  201. .id = SNOR_ID(0xc2, 0x28, 0x16),
  202. .name = "mx25r3235f",
  203. .size = SZ_4M,
  204. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  205. }, {
  206. /* MX25UW51245G */
  207. .id = SNOR_ID(0xc2, 0x81, 0x3a),
  208. .n_banks = 4,
  209. .flags = SPI_NOR_RWW,
  210. }, {
  211. /* MX25L3255E */
  212. .id = SNOR_ID(0xc2, 0x9e, 0x16),
  213. .fixups = &mx25l3255e_fixups,
  214. },
  215. /*
  216. * This spares us of adding new flash entries for flashes that can be
  217. * initialized solely based on the SFDP data, but still need the
  218. * manufacturer hooks to set parameters that can't be discovered at SFDP
  219. * parsing time.
  220. */
  221. { .id = SNOR_ID(0xc2) }
  222. };
  223. static int macronix_nor_octal_dtr_en(struct spi_nor *nor)
  224. {
  225. struct spi_mem_op op;
  226. u8 *buf = nor->bouncebuf, i;
  227. int ret;
  228. /* Use dummy cycles which is parse by SFDP and convert to bit pattern. */
  229. buf[0] = MXIC_NOR_REG_DC(nor->params->reads[SNOR_CMD_READ_8_8_8_DTR].num_wait_states);
  230. op = (struct spi_mem_op)MXIC_NOR_WR_CR2(MXIC_NOR_ADDR_CR2_DC, 1, buf);
  231. ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto);
  232. if (ret)
  233. return ret;
  234. /* Set the octal and DTR enable bits. */
  235. buf[0] = MXIC_NOR_REG_DOPI_EN;
  236. op = (struct spi_mem_op)MXIC_NOR_WR_CR2(MXIC_NOR_ADDR_CR2_MODE, 1, buf);
  237. ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto);
  238. if (ret)
  239. return ret;
  240. /* Read flash ID to make sure the switch was successful. */
  241. ret = spi_nor_read_id(nor, nor->addr_nbytes, 4, buf,
  242. SNOR_PROTO_8_8_8_DTR);
  243. if (ret) {
  244. dev_dbg(nor->dev, "error %d reading JEDEC ID after enabling 8D-8D-8D mode\n", ret);
  245. return ret;
  246. }
  247. /* Macronix SPI-NOR flash 8D-8D-8D read ID would get 6 bytes data A-A-B-B-C-C */
  248. for (i = 0; i < nor->info->id->len; i++)
  249. if (buf[i * 2] != buf[(i * 2) + 1] || buf[i * 2] != nor->info->id->bytes[i])
  250. return -EINVAL;
  251. return 0;
  252. }
  253. static int macronix_nor_octal_dtr_dis(struct spi_nor *nor)
  254. {
  255. struct spi_mem_op op;
  256. u8 *buf = nor->bouncebuf;
  257. int ret;
  258. /*
  259. * The register is 1-byte wide, but 1-byte transactions are not
  260. * allowed in 8D-8D-8D mode. Since there is no register at the
  261. * next location, just initialize the value to 0 and let the
  262. * transaction go on.
  263. */
  264. buf[0] = MXIC_NOR_REG_SPI_EN;
  265. buf[1] = 0x0;
  266. op = (struct spi_mem_op)MXIC_NOR_WR_CR2(MXIC_NOR_ADDR_CR2_MODE, 2, buf);
  267. ret = spi_nor_write_any_volatile_reg(nor, &op, SNOR_PROTO_8_8_8_DTR);
  268. if (ret)
  269. return ret;
  270. /* Read flash ID to make sure the switch was successful. */
  271. ret = spi_nor_read_id(nor, 0, 0, buf, SNOR_PROTO_1_1_1);
  272. if (ret) {
  273. dev_dbg(nor->dev, "error %d reading JEDEC ID after disabling 8D-8D-8D mode\n", ret);
  274. return ret;
  275. }
  276. if (memcmp(buf, nor->info->id->bytes, nor->info->id->len))
  277. return -EINVAL;
  278. return 0;
  279. }
  280. static int macronix_nor_set_octal_dtr(struct spi_nor *nor, bool enable)
  281. {
  282. return enable ? macronix_nor_octal_dtr_en(nor) : macronix_nor_octal_dtr_dis(nor);
  283. }
  284. static void macronix_nor_default_init(struct spi_nor *nor)
  285. {
  286. nor->params->quad_enable = spi_nor_sr1_bit6_quad_enable;
  287. }
  288. static int macronix_nor_late_init(struct spi_nor *nor)
  289. {
  290. if (!nor->params->set_4byte_addr_mode)
  291. nor->params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_en4b_ex4b;
  292. nor->params->set_octal_dtr = macronix_nor_set_octal_dtr;
  293. return 0;
  294. }
  295. static const struct spi_nor_fixups macronix_nor_fixups = {
  296. .default_init = macronix_nor_default_init,
  297. .late_init = macronix_nor_late_init,
  298. };
  299. const struct spi_nor_manufacturer spi_nor_macronix = {
  300. .name = "macronix",
  301. .parts = macronix_nor_parts,
  302. .nparts = ARRAY_SIZE(macronix_nor_parts),
  303. .fixups = &macronix_nor_fixups,
  304. };