winbond.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 WINBOND_NOR_OP_RDEAR 0xc8 /* Read Extended Address Register */
  9. #define WINBOND_NOR_OP_WREAR 0xc5 /* Write Extended Address Register */
  10. #define WINBOND_NOR_WREAR_OP(buf) \
  11. SPI_MEM_OP(SPI_MEM_OP_CMD(WINBOND_NOR_OP_WREAR, 0), \
  12. SPI_MEM_OP_NO_ADDR, \
  13. SPI_MEM_OP_NO_DUMMY, \
  14. SPI_MEM_OP_DATA_OUT(1, buf, 0))
  15. static int
  16. w25q128_post_bfpt_fixups(struct spi_nor *nor,
  17. const struct sfdp_parameter_header *bfpt_header,
  18. const struct sfdp_bfpt *bfpt)
  19. {
  20. /*
  21. * Zetta ZD25Q128C is a clone of the Winbond device. But the encoded
  22. * size is really wrong. It seems that they confused Mbit with MiB.
  23. * Thus the flash is discovered as a 2MiB device.
  24. */
  25. if (bfpt_header->major == SFDP_JESD216_MAJOR &&
  26. bfpt_header->minor == SFDP_JESD216_MINOR &&
  27. nor->params->size == SZ_2M &&
  28. nor->params->erase_map.regions[0].size == SZ_2M) {
  29. nor->params->size = SZ_16M;
  30. nor->params->erase_map.regions[0].size = SZ_16M;
  31. }
  32. return 0;
  33. }
  34. static const struct spi_nor_fixups w25q128_fixups = {
  35. .post_bfpt = w25q128_post_bfpt_fixups,
  36. };
  37. static int
  38. w25q256_post_bfpt_fixups(struct spi_nor *nor,
  39. const struct sfdp_parameter_header *bfpt_header,
  40. const struct sfdp_bfpt *bfpt)
  41. {
  42. /*
  43. * W25Q256JV supports 4B opcodes but W25Q256FV does not.
  44. * Unfortunately, Winbond has re-used the same JEDEC ID for both
  45. * variants which prevents us from defining a new entry in the parts
  46. * table.
  47. * To differentiate between W25Q256JV and W25Q256FV check SFDP header
  48. * version: only JV has JESD216A compliant structure (version 5).
  49. */
  50. if (bfpt_header->major == SFDP_JESD216_MAJOR &&
  51. bfpt_header->minor == SFDP_JESD216A_MINOR)
  52. nor->flags |= SNOR_F_4B_OPCODES;
  53. return 0;
  54. }
  55. static const struct spi_nor_fixups w25q256_fixups = {
  56. .post_bfpt = w25q256_post_bfpt_fixups,
  57. };
  58. static const struct flash_info winbond_nor_parts[] = {
  59. {
  60. .id = SNOR_ID(0xef, 0x30, 0x10),
  61. .name = "w25x05",
  62. .size = SZ_64K,
  63. .no_sfdp_flags = SECT_4K,
  64. }, {
  65. .id = SNOR_ID(0xef, 0x30, 0x11),
  66. .name = "w25x10",
  67. .size = SZ_128K,
  68. .no_sfdp_flags = SECT_4K,
  69. }, {
  70. .id = SNOR_ID(0xef, 0x30, 0x12),
  71. .name = "w25x20",
  72. .size = SZ_256K,
  73. .no_sfdp_flags = SECT_4K,
  74. }, {
  75. .id = SNOR_ID(0xef, 0x30, 0x13),
  76. .name = "w25x40",
  77. .size = SZ_512K,
  78. .no_sfdp_flags = SECT_4K,
  79. }, {
  80. .id = SNOR_ID(0xef, 0x30, 0x14),
  81. .name = "w25x80",
  82. .size = SZ_1M,
  83. .no_sfdp_flags = SECT_4K,
  84. }, {
  85. .id = SNOR_ID(0xef, 0x30, 0x15),
  86. .name = "w25x16",
  87. .size = SZ_2M,
  88. .no_sfdp_flags = SECT_4K,
  89. }, {
  90. .id = SNOR_ID(0xef, 0x30, 0x16),
  91. .name = "w25x32",
  92. .size = SZ_4M,
  93. .no_sfdp_flags = SECT_4K,
  94. }, {
  95. .id = SNOR_ID(0xef, 0x30, 0x17),
  96. .name = "w25x64",
  97. .size = SZ_8M,
  98. .no_sfdp_flags = SECT_4K,
  99. }, {
  100. .id = SNOR_ID(0xef, 0x40, 0x12),
  101. .name = "w25q20cl",
  102. .size = SZ_256K,
  103. .no_sfdp_flags = SECT_4K,
  104. }, {
  105. .id = SNOR_ID(0xef, 0x40, 0x14),
  106. .name = "w25q80bl",
  107. .size = SZ_1M,
  108. .no_sfdp_flags = SECT_4K,
  109. }, {
  110. .id = SNOR_ID(0xef, 0x40, 0x16),
  111. .name = "w25q32",
  112. .size = SZ_4M,
  113. .no_sfdp_flags = SECT_4K,
  114. }, {
  115. .id = SNOR_ID(0xef, 0x40, 0x17),
  116. .name = "w25q64",
  117. .size = SZ_8M,
  118. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  119. }, {
  120. .id = SNOR_ID(0xef, 0x40, 0x18),
  121. .name = "w25q128",
  122. .size = SZ_16M,
  123. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  124. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  125. .fixups = &w25q128_fixups,
  126. }, {
  127. .id = SNOR_ID(0xef, 0x40, 0x19),
  128. .name = "w25q256",
  129. .size = SZ_32M,
  130. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  131. .fixups = &w25q256_fixups,
  132. }, {
  133. .id = SNOR_ID(0xef, 0x40, 0x20),
  134. .name = "w25q512jvq",
  135. .size = SZ_64M,
  136. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  137. }, {
  138. .id = SNOR_ID(0xef, 0x50, 0x12),
  139. .name = "w25q20bw",
  140. .size = SZ_256K,
  141. .no_sfdp_flags = SECT_4K,
  142. }, {
  143. .id = SNOR_ID(0xef, 0x50, 0x14),
  144. .name = "w25q80",
  145. .size = SZ_1M,
  146. .no_sfdp_flags = SECT_4K,
  147. }, {
  148. .id = SNOR_ID(0xef, 0x60, 0x12),
  149. .name = "w25q20ew",
  150. .size = SZ_256K,
  151. .no_sfdp_flags = SECT_4K,
  152. }, {
  153. .id = SNOR_ID(0xef, 0x60, 0x15),
  154. .name = "w25q16dw",
  155. .size = SZ_2M,
  156. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  157. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  158. }, {
  159. .id = SNOR_ID(0xef, 0x60, 0x16),
  160. .name = "w25q32dw",
  161. .size = SZ_4M,
  162. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  163. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  164. .otp = SNOR_OTP(256, 3, 0x1000, 0x1000),
  165. }, {
  166. .id = SNOR_ID(0xef, 0x60, 0x17),
  167. .name = "w25q64dw",
  168. .size = SZ_8M,
  169. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  170. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  171. }, {
  172. .id = SNOR_ID(0xef, 0x60, 0x18),
  173. .name = "w25q128fw",
  174. .size = SZ_16M,
  175. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  176. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  177. }, {
  178. .id = SNOR_ID(0xef, 0x60, 0x19),
  179. .name = "w25q256jw",
  180. .size = SZ_32M,
  181. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  182. }, {
  183. .id = SNOR_ID(0xef, 0x60, 0x20),
  184. .name = "w25q512nwq",
  185. .otp = SNOR_OTP(256, 3, 0x1000, 0x1000),
  186. }, {
  187. .id = SNOR_ID(0xef, 0x70, 0x15),
  188. .name = "w25q16jv-im/jm",
  189. .size = SZ_2M,
  190. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  191. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  192. }, {
  193. .id = SNOR_ID(0xef, 0x70, 0x16),
  194. .name = "w25q32jv",
  195. .size = SZ_4M,
  196. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  197. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  198. }, {
  199. .id = SNOR_ID(0xef, 0x70, 0x17),
  200. .name = "w25q64jvm",
  201. .size = SZ_8M,
  202. .no_sfdp_flags = SECT_4K,
  203. }, {
  204. .id = SNOR_ID(0xef, 0x70, 0x18),
  205. .name = "w25q128jv",
  206. .size = SZ_16M,
  207. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  208. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  209. }, {
  210. .id = SNOR_ID(0xef, 0x70, 0x19),
  211. .name = "w25q256jvm",
  212. }, {
  213. .id = SNOR_ID(0xef, 0x71, 0x19),
  214. .name = "w25m512jv",
  215. .size = SZ_64M,
  216. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  217. }, {
  218. .id = SNOR_ID(0xef, 0x80, 0x16),
  219. .name = "w25q32jwm",
  220. .size = SZ_4M,
  221. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  222. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  223. .otp = SNOR_OTP(256, 3, 0x1000, 0x1000),
  224. }, {
  225. .id = SNOR_ID(0xef, 0x80, 0x17),
  226. .name = "w25q64jwm",
  227. .size = SZ_8M,
  228. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  229. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  230. }, {
  231. .id = SNOR_ID(0xef, 0x80, 0x18),
  232. .name = "w25q128jwm",
  233. .size = SZ_16M,
  234. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  235. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  236. }, {
  237. .id = SNOR_ID(0xef, 0x80, 0x19),
  238. .name = "w25q256jwm",
  239. .size = SZ_32M,
  240. .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
  241. .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
  242. }, {
  243. .id = SNOR_ID(0xef, 0x80, 0x20),
  244. .name = "w25q512nwm",
  245. .otp = SNOR_OTP(256, 3, 0x1000, 0x1000),
  246. },
  247. };
  248. /**
  249. * winbond_nor_write_ear() - Write Extended Address Register.
  250. * @nor: pointer to 'struct spi_nor'.
  251. * @ear: value to write to the Extended Address Register.
  252. *
  253. * Return: 0 on success, -errno otherwise.
  254. */
  255. static int winbond_nor_write_ear(struct spi_nor *nor, u8 ear)
  256. {
  257. int ret;
  258. nor->bouncebuf[0] = ear;
  259. if (nor->spimem) {
  260. struct spi_mem_op op = WINBOND_NOR_WREAR_OP(nor->bouncebuf);
  261. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  262. ret = spi_mem_exec_op(nor->spimem, &op);
  263. } else {
  264. ret = spi_nor_controller_ops_write_reg(nor,
  265. WINBOND_NOR_OP_WREAR,
  266. nor->bouncebuf, 1);
  267. }
  268. if (ret)
  269. dev_dbg(nor->dev, "error %d writing EAR\n", ret);
  270. return ret;
  271. }
  272. /**
  273. * winbond_nor_set_4byte_addr_mode() - Set 4-byte address mode for Winbond
  274. * flashes.
  275. * @nor: pointer to 'struct spi_nor'.
  276. * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
  277. * address mode.
  278. *
  279. * Return: 0 on success, -errno otherwise.
  280. */
  281. static int winbond_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
  282. {
  283. int ret;
  284. ret = spi_nor_set_4byte_addr_mode_en4b_ex4b(nor, enable);
  285. if (ret || enable)
  286. return ret;
  287. /*
  288. * On Winbond W25Q256FV, leaving 4byte mode causes the Extended Address
  289. * Register to be set to 1, so all 3-byte-address reads come from the
  290. * second 16M. We must clear the register to enable normal behavior.
  291. */
  292. ret = spi_nor_write_enable(nor);
  293. if (ret)
  294. return ret;
  295. ret = winbond_nor_write_ear(nor, 0);
  296. if (ret)
  297. return ret;
  298. return spi_nor_write_disable(nor);
  299. }
  300. static const struct spi_nor_otp_ops winbond_nor_otp_ops = {
  301. .read = spi_nor_otp_read_secr,
  302. .write = spi_nor_otp_write_secr,
  303. .erase = spi_nor_otp_erase_secr,
  304. .lock = spi_nor_otp_lock_sr2,
  305. .is_locked = spi_nor_otp_is_locked_sr2,
  306. };
  307. static int winbond_nor_late_init(struct spi_nor *nor)
  308. {
  309. struct spi_nor_flash_parameter *params = nor->params;
  310. if (params->otp.org)
  311. params->otp.ops = &winbond_nor_otp_ops;
  312. /*
  313. * Winbond seems to require that the Extended Address Register to be set
  314. * to zero when exiting the 4-Byte Address Mode, at least for W25Q256FV.
  315. * This requirement is not described in the JESD216 SFDP standard, thus
  316. * it is Winbond specific. Since we do not know if other Winbond flashes
  317. * have the same requirement, play safe and overwrite the method parsed
  318. * from BFPT, if any.
  319. */
  320. params->set_4byte_addr_mode = winbond_nor_set_4byte_addr_mode;
  321. return 0;
  322. }
  323. static const struct spi_nor_fixups winbond_nor_fixups = {
  324. .late_init = winbond_nor_late_init,
  325. };
  326. const struct spi_nor_manufacturer spi_nor_winbond = {
  327. .name = "winbond",
  328. .parts = winbond_nor_parts,
  329. .nparts = ARRAY_SIZE(winbond_nor_parts),
  330. .fixups = &winbond_nor_fixups,
  331. };