162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// HiSilicon SPI NOR V3XX Flash Controller Driver for hi16xx chipsets
462306a36Sopenharmony_ci//
562306a36Sopenharmony_ci// Copyright (c) 2019 HiSilicon Technologies Co., Ltd.
662306a36Sopenharmony_ci// Author: John Garry <john.garry@huawei.com>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/bitops.h>
962306a36Sopenharmony_ci#include <linux/completion.h>
1062306a36Sopenharmony_ci#include <linux/dmi.h>
1162306a36Sopenharmony_ci#include <linux/interrupt.h>
1262306a36Sopenharmony_ci#include <linux/iopoll.h>
1362306a36Sopenharmony_ci#include <linux/module.h>
1462306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
1562306a36Sopenharmony_ci#include <linux/platform_device.h>
1662306a36Sopenharmony_ci#include <linux/slab.h>
1762306a36Sopenharmony_ci#include <linux/spi/spi.h>
1862306a36Sopenharmony_ci#include <linux/spi/spi-mem.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define HISI_SFC_V3XX_VERSION (0x1f8)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define HISI_SFC_V3XX_GLB_CFG (0x100)
2362306a36Sopenharmony_ci#define HISI_SFC_V3XX_GLB_CFG_CS0_ADDR_MODE BIT(2)
2462306a36Sopenharmony_ci#define HISI_SFC_V3XX_RAW_INT_STAT (0x120)
2562306a36Sopenharmony_ci#define HISI_SFC_V3XX_INT_STAT (0x124)
2662306a36Sopenharmony_ci#define HISI_SFC_V3XX_INT_MASK (0x128)
2762306a36Sopenharmony_ci#define HISI_SFC_V3XX_INT_CLR (0x12c)
2862306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_CFG (0x300)
2962306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_CFG_DATA_CNT_OFF 9
3062306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_CFG_RW_MSK BIT(8)
3162306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_CFG_DATA_EN_MSK BIT(7)
3262306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_CFG_DUMMY_CNT_OFF 4
3362306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_CFG_ADDR_EN_MSK BIT(3)
3462306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_CFG_CS_SEL_OFF 1
3562306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_CFG_START_MSK BIT(0)
3662306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_INS (0x308)
3762306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_ADDR (0x30c)
3862306a36Sopenharmony_ci#define HISI_SFC_V3XX_CMD_DATABUF0 (0x400)
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* Common definition of interrupt bit masks */
4162306a36Sopenharmony_ci#define HISI_SFC_V3XX_INT_MASK_ALL (0x1ff)	/* all the masks */
4262306a36Sopenharmony_ci#define HISI_SFC_V3XX_INT_MASK_CPLT BIT(0)	/* command execution complete */
4362306a36Sopenharmony_ci#define HISI_SFC_V3XX_INT_MASK_PP_ERR BIT(2)	/* page progrom error */
4462306a36Sopenharmony_ci#define HISI_SFC_V3XX_INT_MASK_IACCES BIT(5)	/* error visiting inaccessible/
4562306a36Sopenharmony_ci						 * protected address
4662306a36Sopenharmony_ci						 */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/* IO Mode definition in HISI_SFC_V3XX_CMD_CFG */
4962306a36Sopenharmony_ci#define HISI_SFC_V3XX_STD (0 << 17)
5062306a36Sopenharmony_ci#define HISI_SFC_V3XX_DIDO (1 << 17)
5162306a36Sopenharmony_ci#define HISI_SFC_V3XX_DIO (2 << 17)
5262306a36Sopenharmony_ci#define HISI_SFC_V3XX_FULL_DIO (3 << 17)
5362306a36Sopenharmony_ci#define HISI_SFC_V3XX_QIQO (5 << 17)
5462306a36Sopenharmony_ci#define HISI_SFC_V3XX_QIO (6 << 17)
5562306a36Sopenharmony_ci#define HISI_SFC_V3XX_FULL_QIO (7 << 17)
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/*
5862306a36Sopenharmony_ci * The IO modes lookup table. hisi_sfc_v3xx_io_modes[(z - 1) / 2][y / 2][x / 2]
5962306a36Sopenharmony_ci * stands for x-y-z mode, as described in SFDP terminology. -EIO indicates
6062306a36Sopenharmony_ci * an invalid mode.
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_cistatic const int hisi_sfc_v3xx_io_modes[2][3][3] = {
6362306a36Sopenharmony_ci	{
6462306a36Sopenharmony_ci		{ HISI_SFC_V3XX_DIDO, HISI_SFC_V3XX_DIDO, HISI_SFC_V3XX_DIDO },
6562306a36Sopenharmony_ci		{ HISI_SFC_V3XX_DIO, HISI_SFC_V3XX_FULL_DIO, -EIO },
6662306a36Sopenharmony_ci		{ -EIO, -EIO, -EIO },
6762306a36Sopenharmony_ci	},
6862306a36Sopenharmony_ci	{
6962306a36Sopenharmony_ci		{ HISI_SFC_V3XX_QIQO, HISI_SFC_V3XX_QIQO, HISI_SFC_V3XX_QIQO },
7062306a36Sopenharmony_ci		{ -EIO, -EIO, -EIO },
7162306a36Sopenharmony_ci		{ HISI_SFC_V3XX_QIO, -EIO, HISI_SFC_V3XX_FULL_QIO },
7262306a36Sopenharmony_ci	},
7362306a36Sopenharmony_ci};
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistruct hisi_sfc_v3xx_host {
7662306a36Sopenharmony_ci	struct device *dev;
7762306a36Sopenharmony_ci	void __iomem *regbase;
7862306a36Sopenharmony_ci	int max_cmd_dword;
7962306a36Sopenharmony_ci	struct completion *completion;
8062306a36Sopenharmony_ci	u8 address_mode;
8162306a36Sopenharmony_ci	int irq;
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistatic void hisi_sfc_v3xx_disable_int(struct hisi_sfc_v3xx_host *host)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	writel(0, host->regbase + HISI_SFC_V3XX_INT_MASK);
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic void hisi_sfc_v3xx_enable_int(struct hisi_sfc_v3xx_host *host)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	writel(HISI_SFC_V3XX_INT_MASK_ALL, host->regbase + HISI_SFC_V3XX_INT_MASK);
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic void hisi_sfc_v3xx_clear_int(struct hisi_sfc_v3xx_host *host)
9562306a36Sopenharmony_ci{
9662306a36Sopenharmony_ci	writel(HISI_SFC_V3XX_INT_MASK_ALL, host->regbase + HISI_SFC_V3XX_INT_CLR);
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/*
10062306a36Sopenharmony_ci * The interrupt status register indicates whether an error occurs
10162306a36Sopenharmony_ci * after per operation. Check it, and clear the interrupts for
10262306a36Sopenharmony_ci * next time judgement.
10362306a36Sopenharmony_ci */
10462306a36Sopenharmony_cistatic int hisi_sfc_v3xx_handle_completion(struct hisi_sfc_v3xx_host *host)
10562306a36Sopenharmony_ci{
10662306a36Sopenharmony_ci	u32 reg;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	reg = readl(host->regbase + HISI_SFC_V3XX_RAW_INT_STAT);
10962306a36Sopenharmony_ci	hisi_sfc_v3xx_clear_int(host);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	if (reg & HISI_SFC_V3XX_INT_MASK_IACCES) {
11262306a36Sopenharmony_ci		dev_err(host->dev, "fail to access protected address\n");
11362306a36Sopenharmony_ci		return -EIO;
11462306a36Sopenharmony_ci	}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	if (reg & HISI_SFC_V3XX_INT_MASK_PP_ERR) {
11762306a36Sopenharmony_ci		dev_err(host->dev, "page program operation failed\n");
11862306a36Sopenharmony_ci		return -EIO;
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/*
12262306a36Sopenharmony_ci	 * The other bits of the interrupt registers is not currently
12362306a36Sopenharmony_ci	 * used and probably not be triggered in this driver. When it
12462306a36Sopenharmony_ci	 * happens, we regard it as an unsupported error here.
12562306a36Sopenharmony_ci	 */
12662306a36Sopenharmony_ci	if (!(reg & HISI_SFC_V3XX_INT_MASK_CPLT)) {
12762306a36Sopenharmony_ci		dev_err(host->dev, "unsupported error occurred, status=0x%x\n", reg);
12862306a36Sopenharmony_ci		return -EIO;
12962306a36Sopenharmony_ci	}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	return 0;
13262306a36Sopenharmony_ci}
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#define HISI_SFC_V3XX_WAIT_TIMEOUT_US		1000000
13562306a36Sopenharmony_ci#define HISI_SFC_V3XX_WAIT_POLL_INTERVAL_US	10
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_cistatic int hisi_sfc_v3xx_wait_cmd_idle(struct hisi_sfc_v3xx_host *host)
13862306a36Sopenharmony_ci{
13962306a36Sopenharmony_ci	u32 reg;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	return readl_poll_timeout(host->regbase + HISI_SFC_V3XX_CMD_CFG, reg,
14262306a36Sopenharmony_ci				  !(reg & HISI_SFC_V3XX_CMD_CFG_START_MSK),
14362306a36Sopenharmony_ci				  HISI_SFC_V3XX_WAIT_POLL_INTERVAL_US,
14462306a36Sopenharmony_ci				  HISI_SFC_V3XX_WAIT_TIMEOUT_US);
14562306a36Sopenharmony_ci}
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_cistatic int hisi_sfc_v3xx_adjust_op_size(struct spi_mem *mem,
14862306a36Sopenharmony_ci					struct spi_mem_op *op)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	struct spi_device *spi = mem->spi;
15162306a36Sopenharmony_ci	struct hisi_sfc_v3xx_host *host;
15262306a36Sopenharmony_ci	uintptr_t addr = (uintptr_t)op->data.buf.in;
15362306a36Sopenharmony_ci	int max_byte_count;
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	host = spi_controller_get_devdata(spi->controller);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	max_byte_count = host->max_cmd_dword * 4;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	if (!IS_ALIGNED(addr, 4) && op->data.nbytes >= 4)
16062306a36Sopenharmony_ci		op->data.nbytes = 4 - (addr % 4);
16162306a36Sopenharmony_ci	else if (op->data.nbytes > max_byte_count)
16262306a36Sopenharmony_ci		op->data.nbytes = max_byte_count;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	return 0;
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/*
16862306a36Sopenharmony_ci * The controller only supports Standard SPI mode, Dual mode and
16962306a36Sopenharmony_ci * Quad mode. Double sanitize the ops here to avoid OOB access.
17062306a36Sopenharmony_ci */
17162306a36Sopenharmony_cistatic bool hisi_sfc_v3xx_supports_op(struct spi_mem *mem,
17262306a36Sopenharmony_ci				      const struct spi_mem_op *op)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	struct spi_device *spi = mem->spi;
17562306a36Sopenharmony_ci	struct hisi_sfc_v3xx_host *host;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	host = spi_controller_get_devdata(spi->controller);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	if (op->data.buswidth > 4 || op->dummy.buswidth > 4 ||
18062306a36Sopenharmony_ci	    op->addr.buswidth > 4 || op->cmd.buswidth > 4)
18162306a36Sopenharmony_ci		return false;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	if (op->addr.nbytes != host->address_mode && op->addr.nbytes)
18462306a36Sopenharmony_ci		return false;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	return spi_mem_default_supports_op(mem, op);
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/*
19062306a36Sopenharmony_ci * memcpy_{to,from}io doesn't gurantee 32b accesses - which we require for the
19162306a36Sopenharmony_ci * DATABUF registers -so use __io{read,write}32_copy when possible. For
19262306a36Sopenharmony_ci * trailing bytes, copy them byte-by-byte from the DATABUF register, as we
19362306a36Sopenharmony_ci * can't clobber outside the source/dest buffer.
19462306a36Sopenharmony_ci *
19562306a36Sopenharmony_ci * For efficient data read/write, we try to put any start 32b unaligned data
19662306a36Sopenharmony_ci * into a separate transaction in hisi_sfc_v3xx_adjust_op_size().
19762306a36Sopenharmony_ci */
19862306a36Sopenharmony_cistatic void hisi_sfc_v3xx_read_databuf(struct hisi_sfc_v3xx_host *host,
19962306a36Sopenharmony_ci				       u8 *to, unsigned int len)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	void __iomem *from;
20262306a36Sopenharmony_ci	int i;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	from = host->regbase + HISI_SFC_V3XX_CMD_DATABUF0;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	if (IS_ALIGNED((uintptr_t)to, 4)) {
20762306a36Sopenharmony_ci		int words = len / 4;
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci		__ioread32_copy(to, from, words);
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci		len -= words * 4;
21262306a36Sopenharmony_ci		if (len) {
21362306a36Sopenharmony_ci			u32 val;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci			to += words * 4;
21662306a36Sopenharmony_ci			from += words * 4;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci			val = __raw_readl(from);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci			for (i = 0; i < len; i++, val >>= 8, to++)
22162306a36Sopenharmony_ci				*to = (u8)val;
22262306a36Sopenharmony_ci		}
22362306a36Sopenharmony_ci	} else {
22462306a36Sopenharmony_ci		for (i = 0; i < DIV_ROUND_UP(len, 4); i++, from += 4) {
22562306a36Sopenharmony_ci			u32 val = __raw_readl(from);
22662306a36Sopenharmony_ci			int j;
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci			for (j = 0; j < 4 && (j + (i * 4) < len);
22962306a36Sopenharmony_ci			     to++, val >>= 8, j++)
23062306a36Sopenharmony_ci				*to = (u8)val;
23162306a36Sopenharmony_ci		}
23262306a36Sopenharmony_ci	}
23362306a36Sopenharmony_ci}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_cistatic void hisi_sfc_v3xx_write_databuf(struct hisi_sfc_v3xx_host *host,
23662306a36Sopenharmony_ci					const u8 *from, unsigned int len)
23762306a36Sopenharmony_ci{
23862306a36Sopenharmony_ci	void __iomem *to;
23962306a36Sopenharmony_ci	int i;
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	to = host->regbase + HISI_SFC_V3XX_CMD_DATABUF0;
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	if (IS_ALIGNED((uintptr_t)from, 4)) {
24462306a36Sopenharmony_ci		int words = len / 4;
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci		__iowrite32_copy(to, from, words);
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci		len -= words * 4;
24962306a36Sopenharmony_ci		if (len) {
25062306a36Sopenharmony_ci			u32 val = 0;
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci			to += words * 4;
25362306a36Sopenharmony_ci			from += words * 4;
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci			for (i = 0; i < len; i++, from++)
25662306a36Sopenharmony_ci				val |= *from << i * 8;
25762306a36Sopenharmony_ci			__raw_writel(val, to);
25862306a36Sopenharmony_ci		}
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	} else {
26162306a36Sopenharmony_ci		for (i = 0; i < DIV_ROUND_UP(len, 4); i++, to += 4) {
26262306a36Sopenharmony_ci			u32 val = 0;
26362306a36Sopenharmony_ci			int j;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci			for (j = 0; j < 4 && (j + (i * 4) < len);
26662306a36Sopenharmony_ci			     from++, j++)
26762306a36Sopenharmony_ci				val |= *from << j * 8;
26862306a36Sopenharmony_ci			__raw_writel(val, to);
26962306a36Sopenharmony_ci		}
27062306a36Sopenharmony_ci	}
27162306a36Sopenharmony_ci}
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_cistatic int hisi_sfc_v3xx_start_bus(struct hisi_sfc_v3xx_host *host,
27462306a36Sopenharmony_ci				   const struct spi_mem_op *op,
27562306a36Sopenharmony_ci				   u8 chip_select)
27662306a36Sopenharmony_ci{
27762306a36Sopenharmony_ci	int len = op->data.nbytes, buswidth_mode;
27862306a36Sopenharmony_ci	u32 config = 0;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	if (op->addr.nbytes)
28162306a36Sopenharmony_ci		config |= HISI_SFC_V3XX_CMD_CFG_ADDR_EN_MSK;
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	if (op->data.buswidth == 0 || op->data.buswidth == 1) {
28462306a36Sopenharmony_ci		buswidth_mode = HISI_SFC_V3XX_STD;
28562306a36Sopenharmony_ci	} else {
28662306a36Sopenharmony_ci		int data_idx, addr_idx, cmd_idx;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci		data_idx = (op->data.buswidth - 1) / 2;
28962306a36Sopenharmony_ci		addr_idx = op->addr.buswidth / 2;
29062306a36Sopenharmony_ci		cmd_idx = op->cmd.buswidth / 2;
29162306a36Sopenharmony_ci		buswidth_mode = hisi_sfc_v3xx_io_modes[data_idx][addr_idx][cmd_idx];
29262306a36Sopenharmony_ci	}
29362306a36Sopenharmony_ci	if (buswidth_mode < 0)
29462306a36Sopenharmony_ci		return buswidth_mode;
29562306a36Sopenharmony_ci	config |= buswidth_mode;
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci	if (op->data.dir != SPI_MEM_NO_DATA) {
29862306a36Sopenharmony_ci		config |= (len - 1) << HISI_SFC_V3XX_CMD_CFG_DATA_CNT_OFF;
29962306a36Sopenharmony_ci		config |= HISI_SFC_V3XX_CMD_CFG_DATA_EN_MSK;
30062306a36Sopenharmony_ci	}
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	if (op->data.dir == SPI_MEM_DATA_IN)
30362306a36Sopenharmony_ci		config |= HISI_SFC_V3XX_CMD_CFG_RW_MSK;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci	config |= op->dummy.nbytes << HISI_SFC_V3XX_CMD_CFG_DUMMY_CNT_OFF |
30662306a36Sopenharmony_ci		  chip_select << HISI_SFC_V3XX_CMD_CFG_CS_SEL_OFF |
30762306a36Sopenharmony_ci		  HISI_SFC_V3XX_CMD_CFG_START_MSK;
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci	writel(op->addr.val, host->regbase + HISI_SFC_V3XX_CMD_ADDR);
31062306a36Sopenharmony_ci	writel(op->cmd.opcode, host->regbase + HISI_SFC_V3XX_CMD_INS);
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	writel(config, host->regbase + HISI_SFC_V3XX_CMD_CFG);
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci	return 0;
31562306a36Sopenharmony_ci}
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_cistatic int hisi_sfc_v3xx_generic_exec_op(struct hisi_sfc_v3xx_host *host,
31862306a36Sopenharmony_ci					 const struct spi_mem_op *op,
31962306a36Sopenharmony_ci					 u8 chip_select)
32062306a36Sopenharmony_ci{
32162306a36Sopenharmony_ci	DECLARE_COMPLETION_ONSTACK(done);
32262306a36Sopenharmony_ci	int ret;
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	if (host->irq) {
32562306a36Sopenharmony_ci		host->completion = &done;
32662306a36Sopenharmony_ci		hisi_sfc_v3xx_enable_int(host);
32762306a36Sopenharmony_ci	}
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	if (op->data.dir == SPI_MEM_DATA_OUT)
33062306a36Sopenharmony_ci		hisi_sfc_v3xx_write_databuf(host, op->data.buf.out, op->data.nbytes);
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	ret = hisi_sfc_v3xx_start_bus(host, op, chip_select);
33362306a36Sopenharmony_ci	if (ret)
33462306a36Sopenharmony_ci		return ret;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	if (host->irq) {
33762306a36Sopenharmony_ci		ret = wait_for_completion_timeout(host->completion,
33862306a36Sopenharmony_ci						  usecs_to_jiffies(HISI_SFC_V3XX_WAIT_TIMEOUT_US));
33962306a36Sopenharmony_ci		if (!ret)
34062306a36Sopenharmony_ci			ret = -ETIMEDOUT;
34162306a36Sopenharmony_ci		else
34262306a36Sopenharmony_ci			ret = 0;
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci		hisi_sfc_v3xx_disable_int(host);
34562306a36Sopenharmony_ci		synchronize_irq(host->irq);
34662306a36Sopenharmony_ci		host->completion = NULL;
34762306a36Sopenharmony_ci	} else {
34862306a36Sopenharmony_ci		ret = hisi_sfc_v3xx_wait_cmd_idle(host);
34962306a36Sopenharmony_ci	}
35062306a36Sopenharmony_ci	if (hisi_sfc_v3xx_handle_completion(host) || ret)
35162306a36Sopenharmony_ci		return -EIO;
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	if (op->data.dir == SPI_MEM_DATA_IN)
35462306a36Sopenharmony_ci		hisi_sfc_v3xx_read_databuf(host, op->data.buf.in, op->data.nbytes);
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	return 0;
35762306a36Sopenharmony_ci}
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_cistatic int hisi_sfc_v3xx_exec_op(struct spi_mem *mem,
36062306a36Sopenharmony_ci				 const struct spi_mem_op *op)
36162306a36Sopenharmony_ci{
36262306a36Sopenharmony_ci	struct hisi_sfc_v3xx_host *host;
36362306a36Sopenharmony_ci	struct spi_device *spi = mem->spi;
36462306a36Sopenharmony_ci	u8 chip_select = spi_get_chipselect(spi, 0);
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	host = spi_controller_get_devdata(spi->controller);
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	return hisi_sfc_v3xx_generic_exec_op(host, op, chip_select);
36962306a36Sopenharmony_ci}
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_cistatic const struct spi_controller_mem_ops hisi_sfc_v3xx_mem_ops = {
37262306a36Sopenharmony_ci	.adjust_op_size = hisi_sfc_v3xx_adjust_op_size,
37362306a36Sopenharmony_ci	.supports_op = hisi_sfc_v3xx_supports_op,
37462306a36Sopenharmony_ci	.exec_op = hisi_sfc_v3xx_exec_op,
37562306a36Sopenharmony_ci};
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_cistatic irqreturn_t hisi_sfc_v3xx_isr(int irq, void *data)
37862306a36Sopenharmony_ci{
37962306a36Sopenharmony_ci	struct hisi_sfc_v3xx_host *host = data;
38062306a36Sopenharmony_ci	u32 reg;
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	reg = readl(host->regbase + HISI_SFC_V3XX_INT_STAT);
38362306a36Sopenharmony_ci	if (!reg)
38462306a36Sopenharmony_ci		return IRQ_NONE;
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	hisi_sfc_v3xx_disable_int(host);
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	complete(host->completion);
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	return IRQ_HANDLED;
39162306a36Sopenharmony_ci}
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_cistatic int hisi_sfc_v3xx_buswidth_override_bits;
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci/*
39662306a36Sopenharmony_ci * ACPI FW does not allow us to currently set the device buswidth, so quirk it
39762306a36Sopenharmony_ci * depending on the board.
39862306a36Sopenharmony_ci */
39962306a36Sopenharmony_cistatic int __init hisi_sfc_v3xx_dmi_quirk(const struct dmi_system_id *d)
40062306a36Sopenharmony_ci{
40162306a36Sopenharmony_ci	hisi_sfc_v3xx_buswidth_override_bits = SPI_RX_QUAD | SPI_TX_QUAD;
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	return 0;
40462306a36Sopenharmony_ci}
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_cistatic const struct dmi_system_id hisi_sfc_v3xx_dmi_quirk_table[]  = {
40762306a36Sopenharmony_ci	{
40862306a36Sopenharmony_ci	.callback = hisi_sfc_v3xx_dmi_quirk,
40962306a36Sopenharmony_ci	.matches = {
41062306a36Sopenharmony_ci		DMI_MATCH(DMI_SYS_VENDOR, "Huawei"),
41162306a36Sopenharmony_ci		DMI_MATCH(DMI_PRODUCT_NAME, "D06"),
41262306a36Sopenharmony_ci	},
41362306a36Sopenharmony_ci	},
41462306a36Sopenharmony_ci	{
41562306a36Sopenharmony_ci	.callback = hisi_sfc_v3xx_dmi_quirk,
41662306a36Sopenharmony_ci	.matches = {
41762306a36Sopenharmony_ci		DMI_MATCH(DMI_SYS_VENDOR, "Huawei"),
41862306a36Sopenharmony_ci		DMI_MATCH(DMI_PRODUCT_NAME, "TaiShan 2280 V2"),
41962306a36Sopenharmony_ci	},
42062306a36Sopenharmony_ci	},
42162306a36Sopenharmony_ci	{
42262306a36Sopenharmony_ci	.callback = hisi_sfc_v3xx_dmi_quirk,
42362306a36Sopenharmony_ci	.matches = {
42462306a36Sopenharmony_ci		DMI_MATCH(DMI_SYS_VENDOR, "Huawei"),
42562306a36Sopenharmony_ci		DMI_MATCH(DMI_PRODUCT_NAME, "TaiShan 200 (Model 2280)"),
42662306a36Sopenharmony_ci	},
42762306a36Sopenharmony_ci	},
42862306a36Sopenharmony_ci	{}
42962306a36Sopenharmony_ci};
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_cistatic int hisi_sfc_v3xx_probe(struct platform_device *pdev)
43262306a36Sopenharmony_ci{
43362306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
43462306a36Sopenharmony_ci	struct hisi_sfc_v3xx_host *host;
43562306a36Sopenharmony_ci	struct spi_controller *ctlr;
43662306a36Sopenharmony_ci	u32 version, glb_config;
43762306a36Sopenharmony_ci	int ret;
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	ctlr = spi_alloc_host(&pdev->dev, sizeof(*host));
44062306a36Sopenharmony_ci	if (!ctlr)
44162306a36Sopenharmony_ci		return -ENOMEM;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD |
44462306a36Sopenharmony_ci			  SPI_TX_DUAL | SPI_TX_QUAD;
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	ctlr->buswidth_override_bits = hisi_sfc_v3xx_buswidth_override_bits;
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci	host = spi_controller_get_devdata(ctlr);
44962306a36Sopenharmony_ci	host->dev = dev;
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	platform_set_drvdata(pdev, host);
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	host->regbase = devm_platform_ioremap_resource(pdev, 0);
45462306a36Sopenharmony_ci	if (IS_ERR(host->regbase)) {
45562306a36Sopenharmony_ci		ret = PTR_ERR(host->regbase);
45662306a36Sopenharmony_ci		goto err_put_host;
45762306a36Sopenharmony_ci	}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	host->irq = platform_get_irq_optional(pdev, 0);
46062306a36Sopenharmony_ci	if (host->irq == -EPROBE_DEFER) {
46162306a36Sopenharmony_ci		ret = -EPROBE_DEFER;
46262306a36Sopenharmony_ci		goto err_put_host;
46362306a36Sopenharmony_ci	}
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci	hisi_sfc_v3xx_disable_int(host);
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	if (host->irq > 0) {
46862306a36Sopenharmony_ci		ret = devm_request_irq(dev, host->irq, hisi_sfc_v3xx_isr, 0,
46962306a36Sopenharmony_ci				       "hisi-sfc-v3xx", host);
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci		if (ret) {
47262306a36Sopenharmony_ci			dev_err(dev, "failed to request irq%d, ret = %d\n", host->irq, ret);
47362306a36Sopenharmony_ci			host->irq = 0;
47462306a36Sopenharmony_ci		}
47562306a36Sopenharmony_ci	} else {
47662306a36Sopenharmony_ci		host->irq = 0;
47762306a36Sopenharmony_ci	}
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci	ctlr->bus_num = -1;
48062306a36Sopenharmony_ci	ctlr->num_chipselect = 1;
48162306a36Sopenharmony_ci	ctlr->mem_ops = &hisi_sfc_v3xx_mem_ops;
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci	/*
48462306a36Sopenharmony_ci	 * The address mode of the controller is either 3 or 4,
48562306a36Sopenharmony_ci	 * which is indicated by the address mode bit in
48662306a36Sopenharmony_ci	 * the global config register. The register is read only
48762306a36Sopenharmony_ci	 * for the OS driver.
48862306a36Sopenharmony_ci	 */
48962306a36Sopenharmony_ci	glb_config = readl(host->regbase + HISI_SFC_V3XX_GLB_CFG);
49062306a36Sopenharmony_ci	if (glb_config & HISI_SFC_V3XX_GLB_CFG_CS0_ADDR_MODE)
49162306a36Sopenharmony_ci		host->address_mode = 4;
49262306a36Sopenharmony_ci	else
49362306a36Sopenharmony_ci		host->address_mode = 3;
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	version = readl(host->regbase + HISI_SFC_V3XX_VERSION);
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	if (version >= 0x351)
49862306a36Sopenharmony_ci		host->max_cmd_dword = 64;
49962306a36Sopenharmony_ci	else
50062306a36Sopenharmony_ci		host->max_cmd_dword = 16;
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci	ret = devm_spi_register_controller(dev, ctlr);
50362306a36Sopenharmony_ci	if (ret)
50462306a36Sopenharmony_ci		goto err_put_host;
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	dev_info(&pdev->dev, "hw version 0x%x, %s mode.\n",
50762306a36Sopenharmony_ci		 version, host->irq ? "irq" : "polling");
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci	return 0;
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_cierr_put_host:
51262306a36Sopenharmony_ci	spi_controller_put(ctlr);
51362306a36Sopenharmony_ci	return ret;
51462306a36Sopenharmony_ci}
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_cistatic const struct acpi_device_id hisi_sfc_v3xx_acpi_ids[] = {
51762306a36Sopenharmony_ci	{"HISI0341", 0},
51862306a36Sopenharmony_ci	{}
51962306a36Sopenharmony_ci};
52062306a36Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, hisi_sfc_v3xx_acpi_ids);
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_cistatic struct platform_driver hisi_sfc_v3xx_spi_driver = {
52362306a36Sopenharmony_ci	.driver = {
52462306a36Sopenharmony_ci		.name	= "hisi-sfc-v3xx",
52562306a36Sopenharmony_ci		.acpi_match_table = hisi_sfc_v3xx_acpi_ids,
52662306a36Sopenharmony_ci	},
52762306a36Sopenharmony_ci	.probe	= hisi_sfc_v3xx_probe,
52862306a36Sopenharmony_ci};
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_cistatic int __init hisi_sfc_v3xx_spi_init(void)
53162306a36Sopenharmony_ci{
53262306a36Sopenharmony_ci	dmi_check_system(hisi_sfc_v3xx_dmi_quirk_table);
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci	return platform_driver_register(&hisi_sfc_v3xx_spi_driver);
53562306a36Sopenharmony_ci}
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_cistatic void __exit hisi_sfc_v3xx_spi_exit(void)
53862306a36Sopenharmony_ci{
53962306a36Sopenharmony_ci	platform_driver_unregister(&hisi_sfc_v3xx_spi_driver);
54062306a36Sopenharmony_ci}
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_cimodule_init(hisi_sfc_v3xx_spi_init);
54362306a36Sopenharmony_cimodule_exit(hisi_sfc_v3xx_spi_exit);
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ciMODULE_LICENSE("GPL");
54662306a36Sopenharmony_ciMODULE_AUTHOR("John Garry <john.garry@huawei.com>");
54762306a36Sopenharmony_ciMODULE_DESCRIPTION("HiSilicon SPI NOR V3XX Flash Controller Driver for hi16xx chipsets");
548