162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2005, Intec Automation Inc.
462306a36Sopenharmony_ci * Copyright (C) 2014, Freescale Semiconductor, Inc.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/mtd/spi-nor.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include "core.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#define WINBOND_NOR_OP_RDEAR	0xc8	/* Read Extended Address Register */
1262306a36Sopenharmony_ci#define WINBOND_NOR_OP_WREAR	0xc5	/* Write Extended Address Register */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#define WINBOND_NOR_WREAR_OP(buf)					\
1562306a36Sopenharmony_ci	SPI_MEM_OP(SPI_MEM_OP_CMD(WINBOND_NOR_OP_WREAR, 0),		\
1662306a36Sopenharmony_ci		   SPI_MEM_OP_NO_ADDR,					\
1762306a36Sopenharmony_ci		   SPI_MEM_OP_NO_DUMMY,					\
1862306a36Sopenharmony_ci		   SPI_MEM_OP_DATA_OUT(1, buf, 0))
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cistatic int
2162306a36Sopenharmony_ciw25q256_post_bfpt_fixups(struct spi_nor *nor,
2262306a36Sopenharmony_ci			 const struct sfdp_parameter_header *bfpt_header,
2362306a36Sopenharmony_ci			 const struct sfdp_bfpt *bfpt)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	/*
2662306a36Sopenharmony_ci	 * W25Q256JV supports 4B opcodes but W25Q256FV does not.
2762306a36Sopenharmony_ci	 * Unfortunately, Winbond has re-used the same JEDEC ID for both
2862306a36Sopenharmony_ci	 * variants which prevents us from defining a new entry in the parts
2962306a36Sopenharmony_ci	 * table.
3062306a36Sopenharmony_ci	 * To differentiate between W25Q256JV and W25Q256FV check SFDP header
3162306a36Sopenharmony_ci	 * version: only JV has JESD216A compliant structure (version 5).
3262306a36Sopenharmony_ci	 */
3362306a36Sopenharmony_ci	if (bfpt_header->major == SFDP_JESD216_MAJOR &&
3462306a36Sopenharmony_ci	    bfpt_header->minor == SFDP_JESD216A_MINOR)
3562306a36Sopenharmony_ci		nor->flags |= SNOR_F_4B_OPCODES;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	return 0;
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic const struct spi_nor_fixups w25q256_fixups = {
4162306a36Sopenharmony_ci	.post_bfpt = w25q256_post_bfpt_fixups,
4262306a36Sopenharmony_ci};
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cistatic const struct flash_info winbond_nor_parts[] = {
4562306a36Sopenharmony_ci	/* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
4662306a36Sopenharmony_ci	{ "w25x05", INFO(0xef3010, 0, 64 * 1024,  1)
4762306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
4862306a36Sopenharmony_ci	{ "w25x10", INFO(0xef3011, 0, 64 * 1024,  2)
4962306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
5062306a36Sopenharmony_ci	{ "w25x20", INFO(0xef3012, 0, 64 * 1024,  4)
5162306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
5262306a36Sopenharmony_ci	{ "w25x40", INFO(0xef3013, 0, 64 * 1024,  8)
5362306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
5462306a36Sopenharmony_ci	{ "w25x80", INFO(0xef3014, 0, 64 * 1024,  16)
5562306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
5662306a36Sopenharmony_ci	{ "w25x16", INFO(0xef3015, 0, 64 * 1024,  32)
5762306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
5862306a36Sopenharmony_ci	{ "w25q16dw", INFO(0xef6015, 0, 64 * 1024,  32)
5962306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
6062306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
6162306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
6262306a36Sopenharmony_ci	{ "w25x32", INFO(0xef3016, 0, 64 * 1024,  64)
6362306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
6462306a36Sopenharmony_ci	{ "w25q16jv-im/jm", INFO(0xef7015, 0, 64 * 1024,  32)
6562306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
6662306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
6762306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
6862306a36Sopenharmony_ci	{ "w25q20cl", INFO(0xef4012, 0, 64 * 1024,  4)
6962306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
7062306a36Sopenharmony_ci	{ "w25q20bw", INFO(0xef5012, 0, 64 * 1024,  4)
7162306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
7262306a36Sopenharmony_ci	{ "w25q20ew", INFO(0xef6012, 0, 64 * 1024,  4)
7362306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
7462306a36Sopenharmony_ci	{ "w25q32", INFO(0xef4016, 0, 64 * 1024,  64)
7562306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
7662306a36Sopenharmony_ci	{ "w25q32dw", INFO(0xef6016, 0, 64 * 1024,  64)
7762306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
7862306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)
7962306a36Sopenharmony_ci		OTP_INFO(256, 3, 0x1000, 0x1000) },
8062306a36Sopenharmony_ci	{ "w25q32jv", INFO(0xef7016, 0, 64 * 1024,  64)
8162306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
8262306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
8362306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
8462306a36Sopenharmony_ci	{ "w25q32jwm", INFO(0xef8016, 0, 64 * 1024,  64)
8562306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
8662306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)
8762306a36Sopenharmony_ci		OTP_INFO(256, 3, 0x1000, 0x1000) },
8862306a36Sopenharmony_ci	{ "w25q64jwm", INFO(0xef8017, 0, 64 * 1024, 128)
8962306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
9062306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
9162306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
9262306a36Sopenharmony_ci	{ "w25q128jwm", INFO(0xef8018, 0, 64 * 1024, 256)
9362306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
9462306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
9562306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
9662306a36Sopenharmony_ci	{ "w25q256jwm", INFO(0xef8019, 0, 64 * 1024, 512)
9762306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
9862306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
9962306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
10062306a36Sopenharmony_ci	{ "w25x64", INFO(0xef3017, 0, 64 * 1024, 128)
10162306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
10262306a36Sopenharmony_ci	{ "w25q64", INFO(0xef4017, 0, 64 * 1024, 128)
10362306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
10462306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
10562306a36Sopenharmony_ci	{ "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128)
10662306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
10762306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
10862306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
10962306a36Sopenharmony_ci	{ "w25q64jvm", INFO(0xef7017, 0, 64 * 1024, 128)
11062306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
11162306a36Sopenharmony_ci	{ "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256)
11262306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
11362306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
11462306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
11562306a36Sopenharmony_ci	{ "w25q128jv", INFO(0xef7018, 0, 64 * 1024, 256)
11662306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
11762306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
11862306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
11962306a36Sopenharmony_ci	{ "w25q80", INFO(0xef5014, 0, 64 * 1024,  16)
12062306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
12162306a36Sopenharmony_ci	{ "w25q80bl", INFO(0xef4014, 0, 64 * 1024,  16)
12262306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K) },
12362306a36Sopenharmony_ci	{ "w25q128", INFO(0xef4018, 0, 0, 0)
12462306a36Sopenharmony_ci		PARSE_SFDP
12562306a36Sopenharmony_ci		FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
12662306a36Sopenharmony_ci	{ "w25q256", INFO(0xef4019, 0, 64 * 1024, 512)
12762306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)
12862306a36Sopenharmony_ci		.fixups = &w25q256_fixups },
12962306a36Sopenharmony_ci	{ "w25q256jvm", INFO(0xef7019, 0, 64 * 1024, 512)
13062306a36Sopenharmony_ci		PARSE_SFDP },
13162306a36Sopenharmony_ci	{ "w25q256jw", INFO(0xef6019, 0, 64 * 1024, 512)
13262306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
13362306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
13462306a36Sopenharmony_ci	{ "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024)
13562306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ |
13662306a36Sopenharmony_ci			      SPI_NOR_DUAL_READ) },
13762306a36Sopenharmony_ci	{ "w25q512nwq", INFO(0xef6020, 0, 0, 0)
13862306a36Sopenharmony_ci		PARSE_SFDP
13962306a36Sopenharmony_ci		OTP_INFO(256, 3, 0x1000, 0x1000) },
14062306a36Sopenharmony_ci	{ "w25q512nwm", INFO(0xef8020, 0, 64 * 1024, 1024)
14162306a36Sopenharmony_ci		PARSE_SFDP
14262306a36Sopenharmony_ci		OTP_INFO(256, 3, 0x1000, 0x1000) },
14362306a36Sopenharmony_ci	{ "w25q512jvq", INFO(0xef4020, 0, 64 * 1024, 1024)
14462306a36Sopenharmony_ci		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
14562306a36Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
14662306a36Sopenharmony_ci};
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci/**
14962306a36Sopenharmony_ci * winbond_nor_write_ear() - Write Extended Address Register.
15062306a36Sopenharmony_ci * @nor:	pointer to 'struct spi_nor'.
15162306a36Sopenharmony_ci * @ear:	value to write to the Extended Address Register.
15262306a36Sopenharmony_ci *
15362306a36Sopenharmony_ci * Return: 0 on success, -errno otherwise.
15462306a36Sopenharmony_ci */
15562306a36Sopenharmony_cistatic int winbond_nor_write_ear(struct spi_nor *nor, u8 ear)
15662306a36Sopenharmony_ci{
15762306a36Sopenharmony_ci	int ret;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	nor->bouncebuf[0] = ear;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	if (nor->spimem) {
16262306a36Sopenharmony_ci		struct spi_mem_op op = WINBOND_NOR_WREAR_OP(nor->bouncebuf);
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci		ret = spi_mem_exec_op(nor->spimem, &op);
16762306a36Sopenharmony_ci	} else {
16862306a36Sopenharmony_ci		ret = spi_nor_controller_ops_write_reg(nor,
16962306a36Sopenharmony_ci						       WINBOND_NOR_OP_WREAR,
17062306a36Sopenharmony_ci						       nor->bouncebuf, 1);
17162306a36Sopenharmony_ci	}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	if (ret)
17462306a36Sopenharmony_ci		dev_dbg(nor->dev, "error %d writing EAR\n", ret);
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	return ret;
17762306a36Sopenharmony_ci}
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci/**
18062306a36Sopenharmony_ci * winbond_nor_set_4byte_addr_mode() - Set 4-byte address mode for Winbond
18162306a36Sopenharmony_ci * flashes.
18262306a36Sopenharmony_ci * @nor:	pointer to 'struct spi_nor'.
18362306a36Sopenharmony_ci * @enable:	true to enter the 4-byte address mode, false to exit the 4-byte
18462306a36Sopenharmony_ci *		address mode.
18562306a36Sopenharmony_ci *
18662306a36Sopenharmony_ci * Return: 0 on success, -errno otherwise.
18762306a36Sopenharmony_ci */
18862306a36Sopenharmony_cistatic int winbond_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci	int ret;
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	ret = spi_nor_set_4byte_addr_mode_en4b_ex4b(nor, enable);
19362306a36Sopenharmony_ci	if (ret || enable)
19462306a36Sopenharmony_ci		return ret;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	/*
19762306a36Sopenharmony_ci	 * On Winbond W25Q256FV, leaving 4byte mode causes the Extended Address
19862306a36Sopenharmony_ci	 * Register to be set to 1, so all 3-byte-address reads come from the
19962306a36Sopenharmony_ci	 * second 16M. We must clear the register to enable normal behavior.
20062306a36Sopenharmony_ci	 */
20162306a36Sopenharmony_ci	ret = spi_nor_write_enable(nor);
20262306a36Sopenharmony_ci	if (ret)
20362306a36Sopenharmony_ci		return ret;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	ret = winbond_nor_write_ear(nor, 0);
20662306a36Sopenharmony_ci	if (ret)
20762306a36Sopenharmony_ci		return ret;
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	return spi_nor_write_disable(nor);
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic const struct spi_nor_otp_ops winbond_nor_otp_ops = {
21362306a36Sopenharmony_ci	.read = spi_nor_otp_read_secr,
21462306a36Sopenharmony_ci	.write = spi_nor_otp_write_secr,
21562306a36Sopenharmony_ci	.erase = spi_nor_otp_erase_secr,
21662306a36Sopenharmony_ci	.lock = spi_nor_otp_lock_sr2,
21762306a36Sopenharmony_ci	.is_locked = spi_nor_otp_is_locked_sr2,
21862306a36Sopenharmony_ci};
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_cistatic int winbond_nor_late_init(struct spi_nor *nor)
22162306a36Sopenharmony_ci{
22262306a36Sopenharmony_ci	struct spi_nor_flash_parameter *params = nor->params;
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	if (params->otp.org->n_regions)
22562306a36Sopenharmony_ci		params->otp.ops = &winbond_nor_otp_ops;
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	/*
22862306a36Sopenharmony_ci	 * Winbond seems to require that the Extended Address Register to be set
22962306a36Sopenharmony_ci	 * to zero when exiting the 4-Byte Address Mode, at least for W25Q256FV.
23062306a36Sopenharmony_ci	 * This requirement is not described in the JESD216 SFDP standard, thus
23162306a36Sopenharmony_ci	 * it is Winbond specific. Since we do not know if other Winbond flashes
23262306a36Sopenharmony_ci	 * have the same requirement, play safe and overwrite the method parsed
23362306a36Sopenharmony_ci	 * from BFPT, if any.
23462306a36Sopenharmony_ci	 */
23562306a36Sopenharmony_ci	params->set_4byte_addr_mode = winbond_nor_set_4byte_addr_mode;
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	return 0;
23862306a36Sopenharmony_ci}
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cistatic const struct spi_nor_fixups winbond_nor_fixups = {
24162306a36Sopenharmony_ci	.late_init = winbond_nor_late_init,
24262306a36Sopenharmony_ci};
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ciconst struct spi_nor_manufacturer spi_nor_winbond = {
24562306a36Sopenharmony_ci	.name = "winbond",
24662306a36Sopenharmony_ci	.parts = winbond_nor_parts,
24762306a36Sopenharmony_ci	.nparts = ARRAY_SIZE(winbond_nor_parts),
24862306a36Sopenharmony_ci	.fixups = &winbond_nor_fixups,
24962306a36Sopenharmony_ci};
250