1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005, Intec Automation Inc.
4 * Copyright (C) 2014, Freescale Semiconductor, Inc.
7 #include <linux/mtd/spi-nor.h>
11 #define WINBOND_NOR_OP_RDEAR 0xc8 /* Read Extended Address Register */
12 #define WINBOND_NOR_OP_WREAR 0xc5 /* Write Extended Address Register */
14 #define WINBOND_NOR_WREAR_OP(buf) \
15 SPI_MEM_OP(SPI_MEM_OP_CMD(WINBOND_NOR_OP_WREAR, 0), \
17 SPI_MEM_OP_NO_DUMMY, \
18 SPI_MEM_OP_DATA_OUT(1, buf, 0))
21 w25q128_post_bfpt_fixups(struct spi_nor *nor,
22 const struct sfdp_parameter_header *bfpt_header,
23 const struct sfdp_bfpt *bfpt)
26 * Zetta ZD25Q128C is a clone of the Winbond device. But the encoded
27 * size is really wrong. It seems that they confused Mbit with MiB.
28 * Thus the flash is discovered as a 2MiB device.
30 if (bfpt_header->major == SFDP_JESD216_MAJOR &&
31 bfpt_header->minor == SFDP_JESD216_MINOR &&
32 nor->params->size == SZ_2M &&
33 nor->params->erase_map.regions[0].size == SZ_2M) {
34 nor->params->size = SZ_16M;
35 nor->params->erase_map.regions[0].size = SZ_16M;
41 static const struct spi_nor_fixups w25q128_fixups = {
42 .post_bfpt = w25q128_post_bfpt_fixups,
46 w25q256_post_bfpt_fixups(struct spi_nor *nor,
47 const struct sfdp_parameter_header *bfpt_header,
48 const struct sfdp_bfpt *bfpt)
51 * W25Q256JV supports 4B opcodes but W25Q256FV does not.
52 * Unfortunately, Winbond has re-used the same JEDEC ID for both
53 * variants which prevents us from defining a new entry in the parts
55 * To differentiate between W25Q256JV and W25Q256FV check SFDP header
56 * version: only JV has JESD216A compliant structure (version 5).
58 if (bfpt_header->major == SFDP_JESD216_MAJOR &&
59 bfpt_header->minor == SFDP_JESD216A_MINOR)
60 nor->flags |= SNOR_F_4B_OPCODES;
65 static const struct spi_nor_fixups w25q256_fixups = {
66 .post_bfpt = w25q256_post_bfpt_fixups,
69 static const struct flash_info winbond_nor_parts[] = {
71 .id = SNOR_ID(0xef, 0x30, 0x10),
74 .no_sfdp_flags = SECT_4K,
76 .id = SNOR_ID(0xef, 0x30, 0x11),
79 .no_sfdp_flags = SECT_4K,
81 .id = SNOR_ID(0xef, 0x30, 0x12),
84 .no_sfdp_flags = SECT_4K,
86 .id = SNOR_ID(0xef, 0x30, 0x13),
89 .no_sfdp_flags = SECT_4K,
91 .id = SNOR_ID(0xef, 0x30, 0x14),
94 .no_sfdp_flags = SECT_4K,
96 .id = SNOR_ID(0xef, 0x30, 0x15),
99 .no_sfdp_flags = SECT_4K,
101 .id = SNOR_ID(0xef, 0x30, 0x16),
104 .no_sfdp_flags = SECT_4K,
106 .id = SNOR_ID(0xef, 0x30, 0x17),
109 .no_sfdp_flags = SECT_4K,
111 .id = SNOR_ID(0xef, 0x40, 0x12),
114 .no_sfdp_flags = SECT_4K,
116 .id = SNOR_ID(0xef, 0x40, 0x14),
119 .no_sfdp_flags = SECT_4K,
121 .id = SNOR_ID(0xef, 0x40, 0x16),
124 .no_sfdp_flags = SECT_4K,
126 .id = SNOR_ID(0xef, 0x40, 0x17),
129 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
131 .id = SNOR_ID(0xef, 0x40, 0x18),
132 /* Flavors w/ and w/o SFDP. */
135 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
136 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
137 .fixups = &w25q128_fixups,
139 .id = SNOR_ID(0xef, 0x40, 0x19),
142 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
143 .fixups = &w25q256_fixups,
145 .id = SNOR_ID(0xef, 0x40, 0x20),
146 .name = "w25q512jvq",
148 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
150 .id = SNOR_ID(0xef, 0x50, 0x12),
153 .no_sfdp_flags = SECT_4K,
155 .id = SNOR_ID(0xef, 0x50, 0x14),
158 .no_sfdp_flags = SECT_4K,
160 .id = SNOR_ID(0xef, 0x60, 0x12),
163 .no_sfdp_flags = SECT_4K,
165 .id = SNOR_ID(0xef, 0x60, 0x15),
168 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
169 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
171 .id = SNOR_ID(0xef, 0x60, 0x16),
174 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
175 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
176 .otp = SNOR_OTP(256, 3, 0x1000, 0x1000),
178 .id = SNOR_ID(0xef, 0x60, 0x17),
181 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
182 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
184 .id = SNOR_ID(0xef, 0x60, 0x18),
187 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
188 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
190 .id = SNOR_ID(0xef, 0x60, 0x19),
193 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
195 .id = SNOR_ID(0xef, 0x60, 0x20),
196 .name = "w25q512nwq",
197 .otp = SNOR_OTP(256, 3, 0x1000, 0x1000),
199 .id = SNOR_ID(0xef, 0x70, 0x15),
200 .name = "w25q16jv-im/jm",
202 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
203 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
205 .id = SNOR_ID(0xef, 0x70, 0x16),
208 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
209 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
211 .id = SNOR_ID(0xef, 0x70, 0x17),
214 .no_sfdp_flags = SECT_4K,
216 .id = SNOR_ID(0xef, 0x70, 0x18),
219 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
220 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
222 .id = SNOR_ID(0xef, 0x70, 0x19),
223 .name = "w25q256jvm",
225 .id = SNOR_ID(0xef, 0x71, 0x19),
228 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
230 .id = SNOR_ID(0xef, 0x80, 0x16),
233 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
234 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
235 .otp = SNOR_OTP(256, 3, 0x1000, 0x1000),
237 .id = SNOR_ID(0xef, 0x80, 0x17),
240 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
241 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
243 .id = SNOR_ID(0xef, 0x80, 0x18),
244 .name = "w25q128jwm",
246 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
247 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
249 .id = SNOR_ID(0xef, 0x80, 0x19),
250 .name = "w25q256jwm",
252 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
253 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
255 .id = SNOR_ID(0xef, 0x80, 0x20),
256 .name = "w25q512nwm",
257 .otp = SNOR_OTP(256, 3, 0x1000, 0x1000),
262 * winbond_nor_write_ear() - Write Extended Address Register.
263 * @nor: pointer to 'struct spi_nor'.
264 * @ear: value to write to the Extended Address Register.
266 * Return: 0 on success, -errno otherwise.
268 static int winbond_nor_write_ear(struct spi_nor *nor, u8 ear)
272 nor->bouncebuf[0] = ear;
275 struct spi_mem_op op = WINBOND_NOR_WREAR_OP(nor->bouncebuf);
277 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
279 ret = spi_mem_exec_op(nor->spimem, &op);
281 ret = spi_nor_controller_ops_write_reg(nor,
282 WINBOND_NOR_OP_WREAR,
287 dev_dbg(nor->dev, "error %d writing EAR\n", ret);
293 * winbond_nor_set_4byte_addr_mode() - Set 4-byte address mode for Winbond
295 * @nor: pointer to 'struct spi_nor'.
296 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
299 * Return: 0 on success, -errno otherwise.
301 static int winbond_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
305 ret = spi_nor_set_4byte_addr_mode_en4b_ex4b(nor, enable);
310 * On Winbond W25Q256FV, leaving 4byte mode causes the Extended Address
311 * Register to be set to 1, so all 3-byte-address reads come from the
312 * second 16M. We must clear the register to enable normal behavior.
314 ret = spi_nor_write_enable(nor);
318 ret = winbond_nor_write_ear(nor, 0);
322 return spi_nor_write_disable(nor);
325 static const struct spi_nor_otp_ops winbond_nor_otp_ops = {
326 .read = spi_nor_otp_read_secr,
327 .write = spi_nor_otp_write_secr,
328 .erase = spi_nor_otp_erase_secr,
329 .lock = spi_nor_otp_lock_sr2,
330 .is_locked = spi_nor_otp_is_locked_sr2,
333 static int winbond_nor_late_init(struct spi_nor *nor)
335 struct spi_nor_flash_parameter *params = nor->params;
338 params->otp.ops = &winbond_nor_otp_ops;
341 * Winbond seems to require that the Extended Address Register to be set
342 * to zero when exiting the 4-Byte Address Mode, at least for W25Q256FV.
343 * This requirement is not described in the JESD216 SFDP standard, thus
344 * it is Winbond specific. Since we do not know if other Winbond flashes
345 * have the same requirement, play safe and overwrite the method parsed
348 params->set_4byte_addr_mode = winbond_nor_set_4byte_addr_mode;
353 static const struct spi_nor_fixups winbond_nor_fixups = {
354 .late_init = winbond_nor_late_init,
357 const struct spi_nor_manufacturer spi_nor_winbond = {
359 .parts = winbond_nor_parts,
360 .nparts = ARRAY_SIZE(winbond_nor_parts),
361 .fixups = &winbond_nor_fixups,