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 const struct flash_info sst_parts[] = {
128c2ecf20Sopenharmony_ci	/* SST -- large erase sizes are "overlays", "sectors" are 4K */
138c2ecf20Sopenharmony_ci	{ "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024,  8,
148c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
158c2ecf20Sopenharmony_ci	{ "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16,
168c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
178c2ecf20Sopenharmony_ci	{ "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32,
188c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
198c2ecf20Sopenharmony_ci	{ "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64,
208c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
218c2ecf20Sopenharmony_ci	{ "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128,
228c2ecf20Sopenharmony_ci			      SECT_4K | SPI_NOR_4BIT_BP) },
238c2ecf20Sopenharmony_ci	{ "sst25wf512",  INFO(0xbf2501, 0, 64 * 1024,  1,
248c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
258c2ecf20Sopenharmony_ci	{ "sst25wf010",  INFO(0xbf2502, 0, 64 * 1024,  2,
268c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
278c2ecf20Sopenharmony_ci	{ "sst25wf020",  INFO(0xbf2503, 0, 64 * 1024,  4,
288c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
298c2ecf20Sopenharmony_ci	{ "sst25wf020a", INFO(0x621612, 0, 64 * 1024,  4, SECT_4K) },
308c2ecf20Sopenharmony_ci	{ "sst25wf040b", INFO(0x621613, 0, 64 * 1024,  8, SECT_4K) },
318c2ecf20Sopenharmony_ci	{ "sst25wf040",  INFO(0xbf2504, 0, 64 * 1024,  8,
328c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
338c2ecf20Sopenharmony_ci	{ "sst25wf080",  INFO(0xbf2505, 0, 64 * 1024, 16,
348c2ecf20Sopenharmony_ci			      SECT_4K | SST_WRITE) },
358c2ecf20Sopenharmony_ci	{ "sst26wf016b", INFO(0xbf2651, 0, 64 * 1024, 32,
368c2ecf20Sopenharmony_ci			      SECT_4K | SPI_NOR_DUAL_READ |
378c2ecf20Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
388c2ecf20Sopenharmony_ci	{ "sst26vf016b", INFO(0xbf2641, 0, 64 * 1024, 32,
398c2ecf20Sopenharmony_ci			      SECT_4K | SPI_NOR_DUAL_READ) },
408c2ecf20Sopenharmony_ci	{ "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128,
418c2ecf20Sopenharmony_ci			      SECT_4K | SPI_NOR_DUAL_READ |
428c2ecf20Sopenharmony_ci			      SPI_NOR_QUAD_READ) },
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
468c2ecf20Sopenharmony_ci		     size_t *retlen, const u_char *buf)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct spi_nor *nor = mtd_to_spi_nor(mtd);
498c2ecf20Sopenharmony_ci	size_t actual = 0;
508c2ecf20Sopenharmony_ci	int ret;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	ret = spi_nor_lock_and_prep(nor);
558c2ecf20Sopenharmony_ci	if (ret)
568c2ecf20Sopenharmony_ci		return ret;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	ret = spi_nor_write_enable(nor);
598c2ecf20Sopenharmony_ci	if (ret)
608c2ecf20Sopenharmony_ci		goto out;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	nor->sst_write_second = false;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* Start write from odd address. */
658c2ecf20Sopenharmony_ci	if (to % 2) {
668c2ecf20Sopenharmony_ci		nor->program_opcode = SPINOR_OP_BP;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci		/* write one byte. */
698c2ecf20Sopenharmony_ci		ret = spi_nor_write_data(nor, to, 1, buf);
708c2ecf20Sopenharmony_ci		if (ret < 0)
718c2ecf20Sopenharmony_ci			goto out;
728c2ecf20Sopenharmony_ci		WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
738c2ecf20Sopenharmony_ci		ret = spi_nor_wait_till_ready(nor);
748c2ecf20Sopenharmony_ci		if (ret)
758c2ecf20Sopenharmony_ci			goto out;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci		to++;
788c2ecf20Sopenharmony_ci		actual++;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* Write out most of the data here. */
828c2ecf20Sopenharmony_ci	for (; actual < len - 1; actual += 2) {
838c2ecf20Sopenharmony_ci		nor->program_opcode = SPINOR_OP_AAI_WP;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci		/* write two bytes. */
868c2ecf20Sopenharmony_ci		ret = spi_nor_write_data(nor, to, 2, buf + actual);
878c2ecf20Sopenharmony_ci		if (ret < 0)
888c2ecf20Sopenharmony_ci			goto out;
898c2ecf20Sopenharmony_ci		WARN(ret != 2, "While writing 2 bytes written %i bytes\n", ret);
908c2ecf20Sopenharmony_ci		ret = spi_nor_wait_till_ready(nor);
918c2ecf20Sopenharmony_ci		if (ret)
928c2ecf20Sopenharmony_ci			goto out;
938c2ecf20Sopenharmony_ci		to += 2;
948c2ecf20Sopenharmony_ci		nor->sst_write_second = true;
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci	nor->sst_write_second = false;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	ret = spi_nor_write_disable(nor);
998c2ecf20Sopenharmony_ci	if (ret)
1008c2ecf20Sopenharmony_ci		goto out;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	ret = spi_nor_wait_till_ready(nor);
1038c2ecf20Sopenharmony_ci	if (ret)
1048c2ecf20Sopenharmony_ci		goto out;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/* Write out trailing byte if it exists. */
1078c2ecf20Sopenharmony_ci	if (actual != len) {
1088c2ecf20Sopenharmony_ci		ret = spi_nor_write_enable(nor);
1098c2ecf20Sopenharmony_ci		if (ret)
1108c2ecf20Sopenharmony_ci			goto out;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		nor->program_opcode = SPINOR_OP_BP;
1138c2ecf20Sopenharmony_ci		ret = spi_nor_write_data(nor, to, 1, buf + actual);
1148c2ecf20Sopenharmony_ci		if (ret < 0)
1158c2ecf20Sopenharmony_ci			goto out;
1168c2ecf20Sopenharmony_ci		WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
1178c2ecf20Sopenharmony_ci		ret = spi_nor_wait_till_ready(nor);
1188c2ecf20Sopenharmony_ci		if (ret)
1198c2ecf20Sopenharmony_ci			goto out;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci		actual += 1;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci		ret = spi_nor_write_disable(nor);
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ciout:
1268c2ecf20Sopenharmony_ci	*retlen += actual;
1278c2ecf20Sopenharmony_ci	spi_nor_unlock_and_unprep(nor);
1288c2ecf20Sopenharmony_ci	return ret;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic void sst_default_init(struct spi_nor *nor)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	nor->flags |= SNOR_F_HAS_LOCK;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic void sst_post_sfdp_fixups(struct spi_nor *nor)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	if (nor->info->flags & SST_WRITE)
1398c2ecf20Sopenharmony_ci		nor->mtd._write = sst_write;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic const struct spi_nor_fixups sst_fixups = {
1438c2ecf20Sopenharmony_ci	.default_init = sst_default_init,
1448c2ecf20Sopenharmony_ci	.post_sfdp = sst_post_sfdp_fixups,
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ciconst struct spi_nor_manufacturer spi_nor_sst = {
1488c2ecf20Sopenharmony_ci	.name = "sst",
1498c2ecf20Sopenharmony_ci	.parts = sst_parts,
1508c2ecf20Sopenharmony_ci	.nparts = ARRAY_SIZE(sst_parts),
1518c2ecf20Sopenharmony_ci	.fixups = &sst_fixups,
1528c2ecf20Sopenharmony_ci};
153