18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2005, Intec Automation Inc.
48c2ecf20Sopenharmony_ci * Copyright (C) 2014, Freescale Semiconductor, Inc.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/mtd/spi-nor.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "core.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic int
128c2ecf20Sopenharmony_ciw25q256_post_bfpt_fixups(struct spi_nor *nor,
138c2ecf20Sopenharmony_ci			 const struct sfdp_parameter_header *bfpt_header,
148c2ecf20Sopenharmony_ci			 const struct sfdp_bfpt *bfpt,
158c2ecf20Sopenharmony_ci			 struct spi_nor_flash_parameter *params)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	/*
188c2ecf20Sopenharmony_ci	 * W25Q256JV supports 4B opcodes but W25Q256FV does not.
198c2ecf20Sopenharmony_ci	 * Unfortunately, Winbond has re-used the same JEDEC ID for both
208c2ecf20Sopenharmony_ci	 * variants which prevents us from defining a new entry in the parts
218c2ecf20Sopenharmony_ci	 * table.
228c2ecf20Sopenharmony_ci	 * To differentiate between W25Q256JV and W25Q256FV check SFDP header
238c2ecf20Sopenharmony_ci	 * version: only JV has JESD216A compliant structure (version 5).
248c2ecf20Sopenharmony_ci	 */
258c2ecf20Sopenharmony_ci	if (bfpt_header->major == SFDP_JESD216_MAJOR &&
268c2ecf20Sopenharmony_ci	    bfpt_header->minor == SFDP_JESD216A_MINOR)
278c2ecf20Sopenharmony_ci		nor->flags |= SNOR_F_4B_OPCODES;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	return 0;
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic struct spi_nor_fixups w25q256_fixups = {
338c2ecf20Sopenharmony_ci	.post_bfpt = w25q256_post_bfpt_fixups,
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic const struct flash_info winbond_parts[] = {
378c2ecf20Sopenharmony_ci	/* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
388c2ecf20Sopenharmony_ci	{ "w25x05", INFO(0xef3010, 0, 64 * 1024,  1,  SECT_4K) },
398c2ecf20Sopenharmony_ci	{ "w25x10", INFO(0xef3011, 0, 64 * 1024,  2,  SECT_4K) },
408c2ecf20Sopenharmony_ci	{ "w25x20", INFO(0xef3012, 0, 64 * 1024,  4,  SECT_4K) },
418c2ecf20Sopenharmony_ci	{ "w25x40", INFO(0xef3013, 0, 64 * 1024,  8,  SECT_4K) },
428c2ecf20Sopenharmony_ci	{ "w25x80", INFO(0xef3014, 0, 64 * 1024,  16, SECT_4K) },
438c2ecf20Sopenharmony_ci	{ "w25x16", INFO(0xef3015, 0, 64 * 1024,  32, SECT_4K) },
448c2ecf20Sopenharmony_ci	{ "w25q16dw", INFO(0xef6015, 0, 64 * 1024,  32,
458c2ecf20Sopenharmony_ci			   SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
468c2ecf20Sopenharmony_ci			   SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
478c2ecf20Sopenharmony_ci	{ "w25x32", INFO(0xef3016, 0, 64 * 1024,  64, SECT_4K) },
488c2ecf20Sopenharmony_ci	{ "w25q16jv-im/jm", INFO(0xef7015, 0, 64 * 1024,  32,
498c2ecf20Sopenharmony_ci				 SECT_4K | SPI_NOR_DUAL_READ |
508c2ecf20Sopenharmony_ci				 SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK |
518c2ecf20Sopenharmony_ci				 SPI_NOR_HAS_TB) },
528c2ecf20Sopenharmony_ci	{ "w25q20cl", INFO(0xef4012, 0, 64 * 1024,  4, SECT_4K) },
538c2ecf20Sopenharmony_ci	{ "w25q20bw", INFO(0xef5012, 0, 64 * 1024,  4, SECT_4K) },
548c2ecf20Sopenharmony_ci	{ "w25q20ew", INFO(0xef6012, 0, 64 * 1024,  4, SECT_4K) },
558c2ecf20Sopenharmony_ci	{ "w25q32", INFO(0xef4016, 0, 64 * 1024,  64, SECT_4K) },
568c2ecf20Sopenharmony_ci	{ "w25q32dw", INFO(0xef6016, 0, 64 * 1024,  64,
578c2ecf20Sopenharmony_ci			   SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
588c2ecf20Sopenharmony_ci			   SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
598c2ecf20Sopenharmony_ci	{ "w25q32jv", INFO(0xef7016, 0, 64 * 1024,  64,
608c2ecf20Sopenharmony_ci			   SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
618c2ecf20Sopenharmony_ci			   SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
628c2ecf20Sopenharmony_ci	},
638c2ecf20Sopenharmony_ci	{ "w25q32jwm", INFO(0xef8016, 0, 64 * 1024,  64,
648c2ecf20Sopenharmony_ci			    SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
658c2ecf20Sopenharmony_ci			    SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
668c2ecf20Sopenharmony_ci	{ "w25q64jwm", INFO(0xef8017, 0, 64 * 1024, 128,
678c2ecf20Sopenharmony_ci			    SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
688c2ecf20Sopenharmony_ci			    SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
698c2ecf20Sopenharmony_ci	{ "w25q128jwm", INFO(0xef8018, 0, 64 * 1024, 256,
708c2ecf20Sopenharmony_ci			    SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
718c2ecf20Sopenharmony_ci			    SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
728c2ecf20Sopenharmony_ci	{ "w25q256jwm", INFO(0xef8019, 0, 64 * 1024, 512,
738c2ecf20Sopenharmony_ci			    SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
748c2ecf20Sopenharmony_ci			    SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
758c2ecf20Sopenharmony_ci	{ "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
768c2ecf20Sopenharmony_ci	{ "w25q64", INFO(0xef4017, 0, 64 * 1024, 128,
778c2ecf20Sopenharmony_ci			 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
788c2ecf20Sopenharmony_ci	{ "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
798c2ecf20Sopenharmony_ci			   SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
808c2ecf20Sopenharmony_ci			   SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
818c2ecf20Sopenharmony_ci	{ "w25q64jvm", INFO(0xef7017, 0, 64 * 1024, 128, SECT_4K) },
828c2ecf20Sopenharmony_ci	{ "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
838c2ecf20Sopenharmony_ci			    SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
848c2ecf20Sopenharmony_ci			    SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
858c2ecf20Sopenharmony_ci	{ "w25q128jv", INFO(0xef7018, 0, 64 * 1024, 256,
868c2ecf20Sopenharmony_ci			    SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
878c2ecf20Sopenharmony_ci			    SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB) },
888c2ecf20Sopenharmony_ci	{ "w25q80", INFO(0xef5014, 0, 64 * 1024,  16, SECT_4K) },
898c2ecf20Sopenharmony_ci	{ "w25q80bl", INFO(0xef4014, 0, 64 * 1024,  16, SECT_4K) },
908c2ecf20Sopenharmony_ci	{ "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
918c2ecf20Sopenharmony_ci	{ "w25q256", INFO(0xef4019, 0, 64 * 1024, 512,
928c2ecf20Sopenharmony_ci			  SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)
938c2ecf20Sopenharmony_ci	  .fixups = &w25q256_fixups },
948c2ecf20Sopenharmony_ci	{ "w25q256jvm", INFO(0xef7019, 0, 64 * 1024, 512,
958c2ecf20Sopenharmony_ci			     SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
968c2ecf20Sopenharmony_ci	{ "w25q256jw", INFO(0xef6019, 0, 64 * 1024, 512,
978c2ecf20Sopenharmony_ci			     SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
988c2ecf20Sopenharmony_ci	{ "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
998c2ecf20Sopenharmony_ci			    SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/**
1038c2ecf20Sopenharmony_ci * winbond_set_4byte_addr_mode() - Set 4-byte address mode for Winbond flashes.
1048c2ecf20Sopenharmony_ci * @nor:	pointer to 'struct spi_nor'.
1058c2ecf20Sopenharmony_ci * @enable:	true to enter the 4-byte address mode, false to exit the 4-byte
1068c2ecf20Sopenharmony_ci *		address mode.
1078c2ecf20Sopenharmony_ci *
1088c2ecf20Sopenharmony_ci * Return: 0 on success, -errno otherwise.
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_cistatic int winbond_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	int ret;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	ret = spi_nor_set_4byte_addr_mode(nor, enable);
1158c2ecf20Sopenharmony_ci	if (ret || enable)
1168c2ecf20Sopenharmony_ci		return ret;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/*
1198c2ecf20Sopenharmony_ci	 * On Winbond W25Q256FV, leaving 4byte mode causes the Extended Address
1208c2ecf20Sopenharmony_ci	 * Register to be set to 1, so all 3-byte-address reads come from the
1218c2ecf20Sopenharmony_ci	 * second 16M. We must clear the register to enable normal behavior.
1228c2ecf20Sopenharmony_ci	 */
1238c2ecf20Sopenharmony_ci	ret = spi_nor_write_enable(nor);
1248c2ecf20Sopenharmony_ci	if (ret)
1258c2ecf20Sopenharmony_ci		return ret;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	ret = spi_nor_write_ear(nor, 0);
1288c2ecf20Sopenharmony_ci	if (ret)
1298c2ecf20Sopenharmony_ci		return ret;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return spi_nor_write_disable(nor);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic void winbond_default_init(struct spi_nor *nor)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	nor->params->set_4byte_addr_mode = winbond_set_4byte_addr_mode;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic const struct spi_nor_fixups winbond_fixups = {
1408c2ecf20Sopenharmony_ci	.default_init = winbond_default_init,
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciconst struct spi_nor_manufacturer spi_nor_winbond = {
1448c2ecf20Sopenharmony_ci	.name = "winbond",
1458c2ecf20Sopenharmony_ci	.parts = winbond_parts,
1468c2ecf20Sopenharmony_ci	.nparts = ARRAY_SIZE(winbond_parts),
1478c2ecf20Sopenharmony_ci	.fixups = &winbond_fixups,
1488c2ecf20Sopenharmony_ci};
149