xref: /linux/drivers/spi/spi-mt65xx.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a568231fSLeilk Liu /*
3a568231fSLeilk Liu  * Copyright (c) 2015 MediaTek Inc.
4a568231fSLeilk Liu  * Author: Leilk Liu <leilk.liu@mediatek.com>
5a568231fSLeilk Liu  */
6a568231fSLeilk Liu 
7a568231fSLeilk Liu #include <linux/clk.h>
8a568231fSLeilk Liu #include <linux/device.h>
9a568231fSLeilk Liu #include <linux/err.h>
10a568231fSLeilk Liu #include <linux/interrupt.h>
11dd69a0a6SLeilk Liu #include <linux/io.h>
12a568231fSLeilk Liu #include <linux/ioport.h>
13a568231fSLeilk Liu #include <linux/module.h>
14a568231fSLeilk Liu #include <linux/of.h>
151a5a87d5SLinus Walleij #include <linux/gpio/consumer.h>
164247d7f2SRuihai Zhou #include <linux/pinctrl/consumer.h>
17a568231fSLeilk Liu #include <linux/platform_device.h>
18a568231fSLeilk Liu #include <linux/platform_data/spi-mt65xx.h>
19a568231fSLeilk Liu #include <linux/pm_runtime.h>
20a568231fSLeilk Liu #include <linux/spi/spi.h>
219f763fd2SLeilk Liu #include <linux/spi/spi-mem.h>
22fdeae8f5Sluhua.xu #include <linux/dma-mapping.h>
23a568231fSLeilk Liu 
24a568231fSLeilk Liu #define SPI_CFG0_REG			0x0000
25a568231fSLeilk Liu #define SPI_CFG1_REG			0x0004
26a568231fSLeilk Liu #define SPI_TX_SRC_REG			0x0008
27a568231fSLeilk Liu #define SPI_RX_DST_REG			0x000c
28a568231fSLeilk Liu #define SPI_TX_DATA_REG			0x0010
29a568231fSLeilk Liu #define SPI_RX_DATA_REG			0x0014
30a568231fSLeilk Liu #define SPI_CMD_REG			0x0018
31a568231fSLeilk Liu #define SPI_STATUS0_REG			0x001c
32a568231fSLeilk Liu #define SPI_PAD_SEL_REG			0x0024
33058fe49dSLeilk Liu #define SPI_CFG2_REG			0x0028
34fdeae8f5Sluhua.xu #define SPI_TX_SRC_REG_64		0x002c
35fdeae8f5Sluhua.xu #define SPI_RX_DST_REG_64		0x0030
367e963fb2SLeilk Liu #define SPI_CFG3_IPM_REG		0x0040
37a568231fSLeilk Liu 
38a568231fSLeilk Liu #define SPI_CFG0_SCK_HIGH_OFFSET	0
39a568231fSLeilk Liu #define SPI_CFG0_SCK_LOW_OFFSET		8
40a568231fSLeilk Liu #define SPI_CFG0_CS_HOLD_OFFSET		16
41a568231fSLeilk Liu #define SPI_CFG0_CS_SETUP_OFFSET	24
42058fe49dSLeilk Liu #define SPI_ADJUST_CFG0_CS_HOLD_OFFSET	0
43058fe49dSLeilk Liu #define SPI_ADJUST_CFG0_CS_SETUP_OFFSET	16
44a568231fSLeilk Liu 
45a568231fSLeilk Liu #define SPI_CFG1_CS_IDLE_OFFSET		0
46a568231fSLeilk Liu #define SPI_CFG1_PACKET_LOOP_OFFSET	8
47a568231fSLeilk Liu #define SPI_CFG1_PACKET_LENGTH_OFFSET	16
48f84d866aSMason Zhang #define SPI_CFG1_GET_TICK_DLY_OFFSET	29
4903b1be37SLeilk Liu #define SPI_CFG1_GET_TICK_DLY_OFFSET_V1	30
50a568231fSLeilk Liu 
51f84d866aSMason Zhang #define SPI_CFG1_GET_TICK_DLY_MASK	0xe0000000
5203b1be37SLeilk Liu #define SPI_CFG1_GET_TICK_DLY_MASK_V1	0xc0000000
5303b1be37SLeilk Liu 
54a568231fSLeilk Liu #define SPI_CFG1_CS_IDLE_MASK		0xff
55a568231fSLeilk Liu #define SPI_CFG1_PACKET_LOOP_MASK	0xff00
56a568231fSLeilk Liu #define SPI_CFG1_PACKET_LENGTH_MASK	0x3ff0000
577e963fb2SLeilk Liu #define SPI_CFG1_IPM_PACKET_LENGTH_MASK	GENMASK(31, 16)
5844b37eb7Sleilk.liu #define SPI_CFG2_SCK_HIGH_OFFSET	0
5944b37eb7Sleilk.liu #define SPI_CFG2_SCK_LOW_OFFSET		16
60a568231fSLeilk Liu 
61a71d6ea6SLeilk Liu #define SPI_CMD_ACT			BIT(0)
62a71d6ea6SLeilk Liu #define SPI_CMD_RESUME			BIT(1)
63a568231fSLeilk Liu #define SPI_CMD_RST			BIT(2)
64a568231fSLeilk Liu #define SPI_CMD_PAUSE_EN		BIT(4)
65a568231fSLeilk Liu #define SPI_CMD_DEASSERT		BIT(5)
66058fe49dSLeilk Liu #define SPI_CMD_SAMPLE_SEL		BIT(6)
67058fe49dSLeilk Liu #define SPI_CMD_CS_POL			BIT(7)
68a568231fSLeilk Liu #define SPI_CMD_CPHA			BIT(8)
69a568231fSLeilk Liu #define SPI_CMD_CPOL			BIT(9)
70a568231fSLeilk Liu #define SPI_CMD_RX_DMA			BIT(10)
71a568231fSLeilk Liu #define SPI_CMD_TX_DMA			BIT(11)
72a568231fSLeilk Liu #define SPI_CMD_TXMSBF			BIT(12)
73a568231fSLeilk Liu #define SPI_CMD_RXMSBF			BIT(13)
74a568231fSLeilk Liu #define SPI_CMD_RX_ENDIAN		BIT(14)
75a568231fSLeilk Liu #define SPI_CMD_TX_ENDIAN		BIT(15)
76a568231fSLeilk Liu #define SPI_CMD_FINISH_IE		BIT(16)
77a568231fSLeilk Liu #define SPI_CMD_PAUSE_IE		BIT(17)
787e963fb2SLeilk Liu #define SPI_CMD_IPM_NONIDLE_MODE	BIT(19)
797e963fb2SLeilk Liu #define SPI_CMD_IPM_SPIM_LOOP		BIT(21)
807e963fb2SLeilk Liu #define SPI_CMD_IPM_GET_TICKDLY_OFFSET	22
81a568231fSLeilk Liu 
827e963fb2SLeilk Liu #define SPI_CMD_IPM_GET_TICKDLY_MASK	GENMASK(24, 22)
839f763fd2SLeilk Liu 
849f763fd2SLeilk Liu #define PIN_MODE_CFG(x)	((x) / 2)
859f763fd2SLeilk Liu 
867e963fb2SLeilk Liu #define SPI_CFG3_IPM_HALF_DUPLEX_DIR	BIT(2)
877e963fb2SLeilk Liu #define SPI_CFG3_IPM_HALF_DUPLEX_EN	BIT(3)
889f763fd2SLeilk Liu #define SPI_CFG3_IPM_XMODE_EN		BIT(4)
899f763fd2SLeilk Liu #define SPI_CFG3_IPM_NODATA_FLAG	BIT(5)
909f763fd2SLeilk Liu #define SPI_CFG3_IPM_CMD_BYTELEN_OFFSET	8
919f763fd2SLeilk Liu #define SPI_CFG3_IPM_ADDR_BYTELEN_OFFSET 12
929f763fd2SLeilk Liu 
939f763fd2SLeilk Liu #define SPI_CFG3_IPM_CMD_PIN_MODE_MASK	GENMASK(1, 0)
949f763fd2SLeilk Liu #define SPI_CFG3_IPM_CMD_BYTELEN_MASK	GENMASK(11, 8)
959f763fd2SLeilk Liu #define SPI_CFG3_IPM_ADDR_BYTELEN_MASK	GENMASK(15, 12)
969f763fd2SLeilk Liu 
97a568231fSLeilk Liu #define MT8173_SPI_MAX_PAD_SEL		3
98a568231fSLeilk Liu 
9950f8fec2SLeilk Liu #define MTK_SPI_PAUSE_INT_STATUS	0x2
10050f8fec2SLeilk Liu 
1011ce24864SDaniel Kurtz #define MTK_SPI_MAX_FIFO_SIZE		32U
102a568231fSLeilk Liu #define MTK_SPI_PACKET_SIZE		1024
1037e963fb2SLeilk Liu #define MTK_SPI_IPM_PACKET_SIZE		SZ_64K
1049f763fd2SLeilk Liu #define MTK_SPI_IPM_PACKET_LOOP		SZ_256
1059f763fd2SLeilk Liu 
1068e8a9e36SAngeloGioacchino Del Regno #define MTK_SPI_IDLE			0
1078e8a9e36SAngeloGioacchino Del Regno #define MTK_SPI_PAUSED			1
1088e8a9e36SAngeloGioacchino Del Regno 
109fdeae8f5Sluhua.xu #define MTK_SPI_32BITS_MASK		(0xffffffff)
110fdeae8f5Sluhua.xu 
111fdeae8f5Sluhua.xu #define DMA_ADDR_EXT_BITS		(36)
112fdeae8f5Sluhua.xu #define DMA_ADDR_DEF_BITS		(32)
113a568231fSLeilk Liu 
1143c5cd2e2SAngeloGioacchino Del Regno /**
1153c5cd2e2SAngeloGioacchino Del Regno  * struct mtk_spi_compatible - device data structure
1163c5cd2e2SAngeloGioacchino Del Regno  * @need_pad_sel:	Enable pad (pins) selection in SPI controller
1173c5cd2e2SAngeloGioacchino Del Regno  * @must_tx:		Must explicitly send dummy TX bytes to do RX only transfer
1183c5cd2e2SAngeloGioacchino Del Regno  * @enhance_timing:	Enable adjusting cfg register to enhance time accuracy
1193c5cd2e2SAngeloGioacchino Del Regno  * @dma_ext:		DMA address extension supported
1203c5cd2e2SAngeloGioacchino Del Regno  * @no_need_unprepare:	Don't unprepare the SPI clk during runtime
1213c5cd2e2SAngeloGioacchino Del Regno  * @ipm_design:		Adjust/extend registers to support IPM design IP features
1223c5cd2e2SAngeloGioacchino Del Regno  */
123a568231fSLeilk Liu struct mtk_spi_compatible {
124af57937eSLeilk Liu 	bool need_pad_sel;
125af57937eSLeilk Liu 	bool must_tx;
126058fe49dSLeilk Liu 	bool enhance_timing;
127fdeae8f5Sluhua.xu 	bool dma_ext;
128162a31efSMason Zhang 	bool no_need_unprepare;
1297e963fb2SLeilk Liu 	bool ipm_design;
130a568231fSLeilk Liu };
131a568231fSLeilk Liu 
1323c5cd2e2SAngeloGioacchino Del Regno /**
1333c5cd2e2SAngeloGioacchino Del Regno  * struct mtk_spi - SPI driver instance
1343c5cd2e2SAngeloGioacchino Del Regno  * @base:		Start address of the SPI controller registers
1353c5cd2e2SAngeloGioacchino Del Regno  * @state:		SPI controller state
1363c5cd2e2SAngeloGioacchino Del Regno  * @pad_num:		Number of pad_sel entries
1373c5cd2e2SAngeloGioacchino Del Regno  * @pad_sel:		Groups of pins to select
1383c5cd2e2SAngeloGioacchino Del Regno  * @parent_clk:		Parent of sel_clk
139cae15788SYang Yingliang  * @sel_clk:		SPI host mux clock
1403c5cd2e2SAngeloGioacchino Del Regno  * @spi_clk:		Peripheral clock
1413c5cd2e2SAngeloGioacchino Del Regno  * @spi_hclk:		AHB bus clock
1423c5cd2e2SAngeloGioacchino Del Regno  * @cur_transfer:	Currently processed SPI transfer
1433c5cd2e2SAngeloGioacchino Del Regno  * @xfer_len:		Number of bytes to transfer
1443c5cd2e2SAngeloGioacchino Del Regno  * @num_xfered:		Number of transferred bytes
1453c5cd2e2SAngeloGioacchino Del Regno  * @tx_sgl:		TX transfer scatterlist
1463c5cd2e2SAngeloGioacchino Del Regno  * @rx_sgl:		RX transfer scatterlist
1473c5cd2e2SAngeloGioacchino Del Regno  * @tx_sgl_len:		Size of TX DMA transfer
1483c5cd2e2SAngeloGioacchino Del Regno  * @rx_sgl_len:		Size of RX DMA transfer
1493c5cd2e2SAngeloGioacchino Del Regno  * @dev_comp:		Device data structure
1503c5cd2e2SAngeloGioacchino Del Regno  * @spi_clk_hz:		Current SPI clock in Hz
1513c5cd2e2SAngeloGioacchino Del Regno  * @spimem_done:	SPI-MEM operation completion
1523c5cd2e2SAngeloGioacchino Del Regno  * @use_spimem:		Enables SPI-MEM
1533c5cd2e2SAngeloGioacchino Del Regno  * @dev:		Device pointer
1543c5cd2e2SAngeloGioacchino Del Regno  * @tx_dma:		DMA start for SPI-MEM TX
1553c5cd2e2SAngeloGioacchino Del Regno  * @rx_dma:		DMA start for SPI-MEM RX
1563c5cd2e2SAngeloGioacchino Del Regno  */
157a568231fSLeilk Liu struct mtk_spi {
158a568231fSLeilk Liu 	void __iomem *base;
159a568231fSLeilk Liu 	u32 state;
16037457607SLeilk Liu 	int pad_num;
16137457607SLeilk Liu 	u32 *pad_sel;
162a740f4e6SLeilk Liu 	struct clk *parent_clk, *sel_clk, *spi_clk, *spi_hclk;
163a568231fSLeilk Liu 	struct spi_transfer *cur_transfer;
164a568231fSLeilk Liu 	u32 xfer_len;
16500bca73bSPeter Shih 	u32 num_xfered;
166a568231fSLeilk Liu 	struct scatterlist *tx_sgl, *rx_sgl;
167a568231fSLeilk Liu 	u32 tx_sgl_len, rx_sgl_len;
168a568231fSLeilk Liu 	const struct mtk_spi_compatible *dev_comp;
169162a31efSMason Zhang 	u32 spi_clk_hz;
1709f763fd2SLeilk Liu 	struct completion spimem_done;
1719f763fd2SLeilk Liu 	bool use_spimem;
1729f763fd2SLeilk Liu 	struct device *dev;
1739f763fd2SLeilk Liu 	dma_addr_t tx_dma;
1749f763fd2SLeilk Liu 	dma_addr_t rx_dma;
175a568231fSLeilk Liu };
176a568231fSLeilk Liu 
1774eaf6f73SLeilk Liu static const struct mtk_spi_compatible mtk_common_compat;
178fc4f226fSLeilk Liu 
179b6b1f2d9Sleilk.liu@mediatek.com static const struct mtk_spi_compatible mt2712_compat = {
180b6b1f2d9Sleilk.liu@mediatek.com 	.must_tx = true,
181b6b1f2d9Sleilk.liu@mediatek.com };
182b6b1f2d9Sleilk.liu@mediatek.com 
1837e963fb2SLeilk Liu static const struct mtk_spi_compatible mtk_ipm_compat = {
1847e963fb2SLeilk Liu 	.enhance_timing = true,
1857e963fb2SLeilk Liu 	.dma_ext = true,
1867e963fb2SLeilk Liu 	.ipm_design = true,
1877e963fb2SLeilk Liu };
1887e963fb2SLeilk Liu 
1892c231e0aSluhua.xu static const struct mtk_spi_compatible mt6765_compat = {
1902c231e0aSluhua.xu 	.need_pad_sel = true,
1912c231e0aSluhua.xu 	.must_tx = true,
1922c231e0aSluhua.xu 	.enhance_timing = true,
193fdeae8f5Sluhua.xu 	.dma_ext = true,
1942c231e0aSluhua.xu };
1952c231e0aSluhua.xu 
196fc4f226fSLeilk Liu static const struct mtk_spi_compatible mt7622_compat = {
197fc4f226fSLeilk Liu 	.must_tx = true,
198fc4f226fSLeilk Liu 	.enhance_timing = true,
199fc4f226fSLeilk Liu };
200fc4f226fSLeilk Liu 
201a568231fSLeilk Liu static const struct mtk_spi_compatible mt8173_compat = {
202af57937eSLeilk Liu 	.need_pad_sel = true,
203af57937eSLeilk Liu 	.must_tx = true,
204a568231fSLeilk Liu };
205a568231fSLeilk Liu 
206b654aa6fSLeilk Liu static const struct mtk_spi_compatible mt8183_compat = {
207b654aa6fSLeilk Liu 	.need_pad_sel = true,
208b654aa6fSLeilk Liu 	.must_tx = true,
209b654aa6fSLeilk Liu 	.enhance_timing = true,
210b654aa6fSLeilk Liu };
211b654aa6fSLeilk Liu 
212162a31efSMason Zhang static const struct mtk_spi_compatible mt6893_compat = {
213162a31efSMason Zhang 	.need_pad_sel = true,
214162a31efSMason Zhang 	.must_tx = true,
215162a31efSMason Zhang 	.enhance_timing = true,
216162a31efSMason Zhang 	.dma_ext = true,
217162a31efSMason Zhang 	.no_need_unprepare = true,
218162a31efSMason Zhang };
219162a31efSMason Zhang 
220a568231fSLeilk Liu /*
221a568231fSLeilk Liu  * A piece of default chip info unless the platform
222a568231fSLeilk Liu  * supplies it.
223a568231fSLeilk Liu  */
224a568231fSLeilk Liu static const struct mtk_chip_config mtk_default_chip_info = {
225058fe49dSLeilk Liu 	.sample_sel = 0,
226f84d866aSMason Zhang 	.tick_delay = 0,
227a568231fSLeilk Liu };
228a568231fSLeilk Liu 
229a568231fSLeilk Liu static const struct of_device_id mtk_spi_of_match[] = {
2307e963fb2SLeilk Liu 	{ .compatible = "mediatek,spi-ipm",
2317e963fb2SLeilk Liu 		.data = (void *)&mtk_ipm_compat,
2327e963fb2SLeilk Liu 	},
23315bcdefdSLeilk Liu 	{ .compatible = "mediatek,mt2701-spi",
23415bcdefdSLeilk Liu 		.data = (void *)&mtk_common_compat,
23515bcdefdSLeilk Liu 	},
236b6b1f2d9Sleilk.liu@mediatek.com 	{ .compatible = "mediatek,mt2712-spi",
237b6b1f2d9Sleilk.liu@mediatek.com 		.data = (void *)&mt2712_compat,
238b6b1f2d9Sleilk.liu@mediatek.com 	},
2394eaf6f73SLeilk Liu 	{ .compatible = "mediatek,mt6589-spi",
2404eaf6f73SLeilk Liu 		.data = (void *)&mtk_common_compat,
2414eaf6f73SLeilk Liu 	},
2422c231e0aSluhua.xu 	{ .compatible = "mediatek,mt6765-spi",
2432c231e0aSluhua.xu 		.data = (void *)&mt6765_compat,
2442c231e0aSluhua.xu 	},
245fc4f226fSLeilk Liu 	{ .compatible = "mediatek,mt7622-spi",
246fc4f226fSLeilk Liu 		.data = (void *)&mt7622_compat,
247fc4f226fSLeilk Liu 	},
248942779c6SLeilk Liu 	{ .compatible = "mediatek,mt7629-spi",
249942779c6SLeilk Liu 		.data = (void *)&mt7622_compat,
250942779c6SLeilk Liu 	},
2514eaf6f73SLeilk Liu 	{ .compatible = "mediatek,mt8135-spi",
2524eaf6f73SLeilk Liu 		.data = (void *)&mtk_common_compat,
2534eaf6f73SLeilk Liu 	},
2544eaf6f73SLeilk Liu 	{ .compatible = "mediatek,mt8173-spi",
2554eaf6f73SLeilk Liu 		.data = (void *)&mt8173_compat,
2564eaf6f73SLeilk Liu 	},
257b654aa6fSLeilk Liu 	{ .compatible = "mediatek,mt8183-spi",
258b654aa6fSLeilk Liu 		.data = (void *)&mt8183_compat,
259b654aa6fSLeilk Liu 	},
2608cf125c4Sleilk.liu 	{ .compatible = "mediatek,mt8192-spi",
2618cf125c4Sleilk.liu 		.data = (void *)&mt6765_compat,
2628cf125c4Sleilk.liu 	},
263162a31efSMason Zhang 	{ .compatible = "mediatek,mt6893-spi",
264162a31efSMason Zhang 		.data = (void *)&mt6893_compat,
265162a31efSMason Zhang 	},
266a568231fSLeilk Liu 	{}
267a568231fSLeilk Liu };
268a568231fSLeilk Liu MODULE_DEVICE_TABLE(of, mtk_spi_of_match);
269a568231fSLeilk Liu 
mtk_spi_reset(struct mtk_spi * mdata)270a568231fSLeilk Liu static void mtk_spi_reset(struct mtk_spi *mdata)
271a568231fSLeilk Liu {
272a568231fSLeilk Liu 	u32 reg_val;
273a568231fSLeilk Liu 
274a568231fSLeilk Liu 	/* set the software reset bit in SPI_CMD_REG. */
275a568231fSLeilk Liu 	reg_val = readl(mdata->base + SPI_CMD_REG);
276a568231fSLeilk Liu 	reg_val |= SPI_CMD_RST;
277a568231fSLeilk Liu 	writel(reg_val, mdata->base + SPI_CMD_REG);
278a568231fSLeilk Liu 
279a568231fSLeilk Liu 	reg_val = readl(mdata->base + SPI_CMD_REG);
280a568231fSLeilk Liu 	reg_val &= ~SPI_CMD_RST;
281a568231fSLeilk Liu 	writel(reg_val, mdata->base + SPI_CMD_REG);
282a568231fSLeilk Liu }
283a568231fSLeilk Liu 
mtk_spi_set_hw_cs_timing(struct spi_device * spi)28404e6bb0dSMason Zhang static int mtk_spi_set_hw_cs_timing(struct spi_device *spi)
28504e6bb0dSMason Zhang {
286cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(spi->controller);
28704e6bb0dSMason Zhang 	struct spi_delay *cs_setup = &spi->cs_setup;
28804e6bb0dSMason Zhang 	struct spi_delay *cs_hold = &spi->cs_hold;
28904e6bb0dSMason Zhang 	struct spi_delay *cs_inactive = &spi->cs_inactive;
2905c842e51SMason Zhang 	u32 setup, hold, inactive;
29104e6bb0dSMason Zhang 	u32 reg_val;
29204e6bb0dSMason Zhang 	int delay;
29304e6bb0dSMason Zhang 
29404e6bb0dSMason Zhang 	delay = spi_delay_to_ns(cs_setup, NULL);
29504e6bb0dSMason Zhang 	if (delay < 0)
29604e6bb0dSMason Zhang 		return delay;
29704e6bb0dSMason Zhang 	setup = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000;
29804e6bb0dSMason Zhang 
29904e6bb0dSMason Zhang 	delay = spi_delay_to_ns(cs_hold, NULL);
30004e6bb0dSMason Zhang 	if (delay < 0)
30104e6bb0dSMason Zhang 		return delay;
30204e6bb0dSMason Zhang 	hold = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000;
30304e6bb0dSMason Zhang 
30404e6bb0dSMason Zhang 	delay = spi_delay_to_ns(cs_inactive, NULL);
30504e6bb0dSMason Zhang 	if (delay < 0)
30604e6bb0dSMason Zhang 		return delay;
30704e6bb0dSMason Zhang 	inactive = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000;
30804e6bb0dSMason Zhang 
3093672bb82SDafna Hirschfeld 	if (hold || setup) {
31004e6bb0dSMason Zhang 		reg_val = readl(mdata->base + SPI_CFG0_REG);
31104e6bb0dSMason Zhang 		if (mdata->dev_comp->enhance_timing) {
3123672bb82SDafna Hirschfeld 			if (hold) {
3135c842e51SMason Zhang 				hold = min_t(u32, hold, 0x10000);
31404e6bb0dSMason Zhang 				reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
31504e6bb0dSMason Zhang 				reg_val |= (((hold - 1) & 0xffff)
31604e6bb0dSMason Zhang 					<< SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
3173672bb82SDafna Hirschfeld 			}
3183672bb82SDafna Hirschfeld 			if (setup) {
3193672bb82SDafna Hirschfeld 				setup = min_t(u32, setup, 0x10000);
32004e6bb0dSMason Zhang 				reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
32104e6bb0dSMason Zhang 				reg_val |= (((setup - 1) & 0xffff)
32204e6bb0dSMason Zhang 					<< SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
3233672bb82SDafna Hirschfeld 			}
32404e6bb0dSMason Zhang 		} else {
3253672bb82SDafna Hirschfeld 			if (hold) {
3265c842e51SMason Zhang 				hold = min_t(u32, hold, 0x100);
32704e6bb0dSMason Zhang 				reg_val &= ~(0xff << SPI_CFG0_CS_HOLD_OFFSET);
32804e6bb0dSMason Zhang 				reg_val |= (((hold - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET);
3293672bb82SDafna Hirschfeld 			}
3303672bb82SDafna Hirschfeld 			if (setup) {
3313672bb82SDafna Hirschfeld 				setup = min_t(u32, setup, 0x100);
33204e6bb0dSMason Zhang 				reg_val &= ~(0xff << SPI_CFG0_CS_SETUP_OFFSET);
33304e6bb0dSMason Zhang 				reg_val |= (((setup - 1) & 0xff)
33404e6bb0dSMason Zhang 					<< SPI_CFG0_CS_SETUP_OFFSET);
33504e6bb0dSMason Zhang 			}
3363672bb82SDafna Hirschfeld 		}
33704e6bb0dSMason Zhang 		writel(reg_val, mdata->base + SPI_CFG0_REG);
3383672bb82SDafna Hirschfeld 	}
33904e6bb0dSMason Zhang 
3403672bb82SDafna Hirschfeld 	if (inactive) {
3415c842e51SMason Zhang 		inactive = min_t(u32, inactive, 0x100);
34204e6bb0dSMason Zhang 		reg_val = readl(mdata->base + SPI_CFG1_REG);
34304e6bb0dSMason Zhang 		reg_val &= ~SPI_CFG1_CS_IDLE_MASK;
34404e6bb0dSMason Zhang 		reg_val |= (((inactive - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET);
34504e6bb0dSMason Zhang 		writel(reg_val, mdata->base + SPI_CFG1_REG);
3463672bb82SDafna Hirschfeld 	}
34704e6bb0dSMason Zhang 
34804e6bb0dSMason Zhang 	return 0;
34904e6bb0dSMason Zhang }
35004e6bb0dSMason Zhang 
mtk_spi_hw_init(struct spi_controller * host,struct spi_device * spi)351cae15788SYang Yingliang static int mtk_spi_hw_init(struct spi_controller *host,
3527e963fb2SLeilk Liu 			   struct spi_device *spi)
353a568231fSLeilk Liu {
35479b5d3f2SLeilk Liu 	u16 cpha, cpol;
355a568231fSLeilk Liu 	u32 reg_val;
35658a984c7SLeilk Liu 	struct mtk_chip_config *chip_config = spi->controller_data;
357cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
35879b5d3f2SLeilk Liu 
35979b5d3f2SLeilk Liu 	cpha = spi->mode & SPI_CPHA ? 1 : 0;
36079b5d3f2SLeilk Liu 	cpol = spi->mode & SPI_CPOL ? 1 : 0;
36179b5d3f2SLeilk Liu 
36279b5d3f2SLeilk Liu 	reg_val = readl(mdata->base + SPI_CMD_REG);
3637e963fb2SLeilk Liu 	if (mdata->dev_comp->ipm_design) {
3647e963fb2SLeilk Liu 		/* SPI transfer without idle time until packet length done */
3657e963fb2SLeilk Liu 		reg_val |= SPI_CMD_IPM_NONIDLE_MODE;
3667e963fb2SLeilk Liu 		if (spi->mode & SPI_LOOP)
3677e963fb2SLeilk Liu 			reg_val |= SPI_CMD_IPM_SPIM_LOOP;
3687e963fb2SLeilk Liu 		else
3697e963fb2SLeilk Liu 			reg_val &= ~SPI_CMD_IPM_SPIM_LOOP;
3707e963fb2SLeilk Liu 	}
3717e963fb2SLeilk Liu 
37279b5d3f2SLeilk Liu 	if (cpha)
37379b5d3f2SLeilk Liu 		reg_val |= SPI_CMD_CPHA;
37479b5d3f2SLeilk Liu 	else
37579b5d3f2SLeilk Liu 		reg_val &= ~SPI_CMD_CPHA;
37679b5d3f2SLeilk Liu 	if (cpol)
37779b5d3f2SLeilk Liu 		reg_val |= SPI_CMD_CPOL;
37879b5d3f2SLeilk Liu 	else
37979b5d3f2SLeilk Liu 		reg_val &= ~SPI_CMD_CPOL;
380a568231fSLeilk Liu 
381a568231fSLeilk Liu 	/* set the mlsbx and mlsbtx */
3823e582c6eSLeilk Liu 	if (spi->mode & SPI_LSB_FIRST) {
383a71d6ea6SLeilk Liu 		reg_val &= ~SPI_CMD_TXMSBF;
384a71d6ea6SLeilk Liu 		reg_val &= ~SPI_CMD_RXMSBF;
3853e582c6eSLeilk Liu 	} else {
3863e582c6eSLeilk Liu 		reg_val |= SPI_CMD_TXMSBF;
3873e582c6eSLeilk Liu 		reg_val |= SPI_CMD_RXMSBF;
3883e582c6eSLeilk Liu 	}
389a568231fSLeilk Liu 
390a568231fSLeilk Liu 	/* set the tx/rx endian */
39144f636daSLeilk Liu #ifdef __LITTLE_ENDIAN
39244f636daSLeilk Liu 	reg_val &= ~SPI_CMD_TX_ENDIAN;
39344f636daSLeilk Liu 	reg_val &= ~SPI_CMD_RX_ENDIAN;
39444f636daSLeilk Liu #else
39544f636daSLeilk Liu 	reg_val |= SPI_CMD_TX_ENDIAN;
39644f636daSLeilk Liu 	reg_val |= SPI_CMD_RX_ENDIAN;
39744f636daSLeilk Liu #endif
398a568231fSLeilk Liu 
399058fe49dSLeilk Liu 	if (mdata->dev_comp->enhance_timing) {
400ae7c2d34SLuhua Xu 		/* set CS polarity */
401ae7c2d34SLuhua Xu 		if (spi->mode & SPI_CS_HIGH)
402058fe49dSLeilk Liu 			reg_val |= SPI_CMD_CS_POL;
403058fe49dSLeilk Liu 		else
404058fe49dSLeilk Liu 			reg_val &= ~SPI_CMD_CS_POL;
405ae7c2d34SLuhua Xu 
406058fe49dSLeilk Liu 		if (chip_config->sample_sel)
407058fe49dSLeilk Liu 			reg_val |= SPI_CMD_SAMPLE_SEL;
408058fe49dSLeilk Liu 		else
409058fe49dSLeilk Liu 			reg_val &= ~SPI_CMD_SAMPLE_SEL;
410058fe49dSLeilk Liu 	}
411058fe49dSLeilk Liu 
412a568231fSLeilk Liu 	/* set finish and pause interrupt always enable */
41315293324SLeilk Liu 	reg_val |= SPI_CMD_FINISH_IE | SPI_CMD_PAUSE_IE;
414a568231fSLeilk Liu 
415a568231fSLeilk Liu 	/* disable dma mode */
416a568231fSLeilk Liu 	reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA);
417a568231fSLeilk Liu 
418a568231fSLeilk Liu 	/* disable deassert mode */
419a568231fSLeilk Liu 	reg_val &= ~SPI_CMD_DEASSERT;
420a568231fSLeilk Liu 
421a568231fSLeilk Liu 	writel(reg_val, mdata->base + SPI_CMD_REG);
422a568231fSLeilk Liu 
423a568231fSLeilk Liu 	/* pad select */
424a568231fSLeilk Liu 	if (mdata->dev_comp->need_pad_sel)
4259e264f3fSAmit Kumar Mahapatra via Alsa-devel 		writel(mdata->pad_sel[spi_get_chipselect(spi, 0)],
42637457607SLeilk Liu 		       mdata->base + SPI_PAD_SEL_REG);
427a568231fSLeilk Liu 
428f84d866aSMason Zhang 	/* tick delay */
42903b1be37SLeilk Liu 	if (mdata->dev_comp->enhance_timing) {
4307e963fb2SLeilk Liu 		if (mdata->dev_comp->ipm_design) {
4317e963fb2SLeilk Liu 			reg_val = readl(mdata->base + SPI_CMD_REG);
4327e963fb2SLeilk Liu 			reg_val &= ~SPI_CMD_IPM_GET_TICKDLY_MASK;
4337e963fb2SLeilk Liu 			reg_val |= ((chip_config->tick_delay & 0x7)
4347e963fb2SLeilk Liu 				    << SPI_CMD_IPM_GET_TICKDLY_OFFSET);
4357e963fb2SLeilk Liu 			writel(reg_val, mdata->base + SPI_CMD_REG);
4367e963fb2SLeilk Liu 		} else {
4377e963fb2SLeilk Liu 			reg_val = readl(mdata->base + SPI_CFG1_REG);
438f84d866aSMason Zhang 			reg_val &= ~SPI_CFG1_GET_TICK_DLY_MASK;
439f84d866aSMason Zhang 			reg_val |= ((chip_config->tick_delay & 0x7)
440f84d866aSMason Zhang 				    << SPI_CFG1_GET_TICK_DLY_OFFSET);
4417e963fb2SLeilk Liu 			writel(reg_val, mdata->base + SPI_CFG1_REG);
4427e963fb2SLeilk Liu 		}
44303b1be37SLeilk Liu 	} else {
4447e963fb2SLeilk Liu 		reg_val = readl(mdata->base + SPI_CFG1_REG);
44503b1be37SLeilk Liu 		reg_val &= ~SPI_CFG1_GET_TICK_DLY_MASK_V1;
44603b1be37SLeilk Liu 		reg_val |= ((chip_config->tick_delay & 0x3)
44703b1be37SLeilk Liu 			    << SPI_CFG1_GET_TICK_DLY_OFFSET_V1);
448f84d866aSMason Zhang 		writel(reg_val, mdata->base + SPI_CFG1_REG);
4497e963fb2SLeilk Liu 	}
450f84d866aSMason Zhang 
45104e6bb0dSMason Zhang 	/* set hw cs timing */
45204e6bb0dSMason Zhang 	mtk_spi_set_hw_cs_timing(spi);
453a568231fSLeilk Liu 	return 0;
454a568231fSLeilk Liu }
455a568231fSLeilk Liu 
mtk_spi_prepare_message(struct spi_controller * host,struct spi_message * msg)456cae15788SYang Yingliang static int mtk_spi_prepare_message(struct spi_controller *host,
4577e963fb2SLeilk Liu 				   struct spi_message *msg)
4587e963fb2SLeilk Liu {
459cae15788SYang Yingliang 	return mtk_spi_hw_init(host, msg->spi);
4607e963fb2SLeilk Liu }
4617e963fb2SLeilk Liu 
mtk_spi_set_cs(struct spi_device * spi,bool enable)462a568231fSLeilk Liu static void mtk_spi_set_cs(struct spi_device *spi, bool enable)
463a568231fSLeilk Liu {
464a568231fSLeilk Liu 	u32 reg_val;
465cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(spi->controller);
466a568231fSLeilk Liu 
467ae7c2d34SLuhua Xu 	if (spi->mode & SPI_CS_HIGH)
468ae7c2d34SLuhua Xu 		enable = !enable;
469ae7c2d34SLuhua Xu 
470a568231fSLeilk Liu 	reg_val = readl(mdata->base + SPI_CMD_REG);
4716583d203SLeilk Liu 	if (!enable) {
472a568231fSLeilk Liu 		reg_val |= SPI_CMD_PAUSE_EN;
4736583d203SLeilk Liu 		writel(reg_val, mdata->base + SPI_CMD_REG);
4746583d203SLeilk Liu 	} else {
475a568231fSLeilk Liu 		reg_val &= ~SPI_CMD_PAUSE_EN;
476a568231fSLeilk Liu 		writel(reg_val, mdata->base + SPI_CMD_REG);
4776583d203SLeilk Liu 		mdata->state = MTK_SPI_IDLE;
4786583d203SLeilk Liu 		mtk_spi_reset(mdata);
4796583d203SLeilk Liu 	}
480a568231fSLeilk Liu }
481a568231fSLeilk Liu 
mtk_spi_prepare_transfer(struct spi_controller * host,u32 speed_hz)482cae15788SYang Yingliang static void mtk_spi_prepare_transfer(struct spi_controller *host,
4837e963fb2SLeilk Liu 				     u32 speed_hz)
484a568231fSLeilk Liu {
485162a31efSMason Zhang 	u32 div, sck_time, reg_val;
486cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
487a568231fSLeilk Liu 
4887e963fb2SLeilk Liu 	if (speed_hz < mdata->spi_clk_hz / 2)
4897e963fb2SLeilk Liu 		div = DIV_ROUND_UP(mdata->spi_clk_hz, speed_hz);
490a568231fSLeilk Liu 	else
491a568231fSLeilk Liu 		div = 1;
492a568231fSLeilk Liu 
4932ce0acf5SLeilk Liu 	sck_time = (div + 1) / 2;
494a568231fSLeilk Liu 
495058fe49dSLeilk Liu 	if (mdata->dev_comp->enhance_timing) {
4969f6e7e8dSleilk.liu 		reg_val = readl(mdata->base + SPI_CFG2_REG);
4979f6e7e8dSleilk.liu 		reg_val &= ~(0xffff << SPI_CFG2_SCK_HIGH_OFFSET);
4989f6e7e8dSleilk.liu 		reg_val |= (((sck_time - 1) & 0xffff)
49944b37eb7Sleilk.liu 			   << SPI_CFG2_SCK_HIGH_OFFSET);
5009f6e7e8dSleilk.liu 		reg_val &= ~(0xffff << SPI_CFG2_SCK_LOW_OFFSET);
501058fe49dSLeilk Liu 		reg_val |= (((sck_time - 1) & 0xffff)
50244b37eb7Sleilk.liu 			   << SPI_CFG2_SCK_LOW_OFFSET);
503058fe49dSLeilk Liu 		writel(reg_val, mdata->base + SPI_CFG2_REG);
504058fe49dSLeilk Liu 	} else {
5059f6e7e8dSleilk.liu 		reg_val = readl(mdata->base + SPI_CFG0_REG);
5069f6e7e8dSleilk.liu 		reg_val &= ~(0xff << SPI_CFG0_SCK_HIGH_OFFSET);
5079f6e7e8dSleilk.liu 		reg_val |= (((sck_time - 1) & 0xff)
508058fe49dSLeilk Liu 			   << SPI_CFG0_SCK_HIGH_OFFSET);
5099f6e7e8dSleilk.liu 		reg_val &= ~(0xff << SPI_CFG0_SCK_LOW_OFFSET);
5102ce0acf5SLeilk Liu 		reg_val |= (((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET);
511a568231fSLeilk Liu 		writel(reg_val, mdata->base + SPI_CFG0_REG);
512058fe49dSLeilk Liu 	}
513a568231fSLeilk Liu }
514a568231fSLeilk Liu 
mtk_spi_setup_packet(struct spi_controller * host)515cae15788SYang Yingliang static void mtk_spi_setup_packet(struct spi_controller *host)
516a568231fSLeilk Liu {
517a568231fSLeilk Liu 	u32 packet_size, packet_loop, reg_val;
518cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
519a568231fSLeilk Liu 
5207e963fb2SLeilk Liu 	if (mdata->dev_comp->ipm_design)
5217e963fb2SLeilk Liu 		packet_size = min_t(u32,
5227e963fb2SLeilk Liu 				    mdata->xfer_len,
5237e963fb2SLeilk Liu 				    MTK_SPI_IPM_PACKET_SIZE);
5247e963fb2SLeilk Liu 	else
5257e963fb2SLeilk Liu 		packet_size = min_t(u32,
5267e963fb2SLeilk Liu 				    mdata->xfer_len,
5277e963fb2SLeilk Liu 				    MTK_SPI_PACKET_SIZE);
5287e963fb2SLeilk Liu 
529a568231fSLeilk Liu 	packet_loop = mdata->xfer_len / packet_size;
530a568231fSLeilk Liu 
531a568231fSLeilk Liu 	reg_val = readl(mdata->base + SPI_CFG1_REG);
5327e963fb2SLeilk Liu 	if (mdata->dev_comp->ipm_design)
5337e963fb2SLeilk Liu 		reg_val &= ~SPI_CFG1_IPM_PACKET_LENGTH_MASK;
5347e963fb2SLeilk Liu 	else
5357e963fb2SLeilk Liu 		reg_val &= ~SPI_CFG1_PACKET_LENGTH_MASK;
536a568231fSLeilk Liu 	reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET;
5377e963fb2SLeilk Liu 	reg_val &= ~SPI_CFG1_PACKET_LOOP_MASK;
538a568231fSLeilk Liu 	reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET;
539a568231fSLeilk Liu 	writel(reg_val, mdata->base + SPI_CFG1_REG);
540a568231fSLeilk Liu }
541a568231fSLeilk Liu 
mtk_spi_enable_transfer(struct spi_controller * host)542cae15788SYang Yingliang static void mtk_spi_enable_transfer(struct spi_controller *host)
543a568231fSLeilk Liu {
54450f8fec2SLeilk Liu 	u32 cmd;
545cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
546a568231fSLeilk Liu 
547a568231fSLeilk Liu 	cmd = readl(mdata->base + SPI_CMD_REG);
548a568231fSLeilk Liu 	if (mdata->state == MTK_SPI_IDLE)
549a71d6ea6SLeilk Liu 		cmd |= SPI_CMD_ACT;
550a568231fSLeilk Liu 	else
551a71d6ea6SLeilk Liu 		cmd |= SPI_CMD_RESUME;
552a568231fSLeilk Liu 	writel(cmd, mdata->base + SPI_CMD_REG);
553a568231fSLeilk Liu }
554a568231fSLeilk Liu 
mtk_spi_get_mult_delta(struct mtk_spi * mdata,u32 xfer_len)555cf82d0ecSzhichao.liu static int mtk_spi_get_mult_delta(struct mtk_spi *mdata, u32 xfer_len)
556a568231fSLeilk Liu {
557cf82d0ecSzhichao.liu 	u32 mult_delta = 0;
558a568231fSLeilk Liu 
559cf82d0ecSzhichao.liu 	if (mdata->dev_comp->ipm_design) {
560cf82d0ecSzhichao.liu 		if (xfer_len > MTK_SPI_IPM_PACKET_SIZE)
561cf82d0ecSzhichao.liu 			mult_delta = xfer_len % MTK_SPI_IPM_PACKET_SIZE;
562cf82d0ecSzhichao.liu 	} else {
563a568231fSLeilk Liu 		if (xfer_len > MTK_SPI_PACKET_SIZE)
564a568231fSLeilk Liu 			mult_delta = xfer_len % MTK_SPI_PACKET_SIZE;
565cf82d0ecSzhichao.liu 	}
566a568231fSLeilk Liu 
567a568231fSLeilk Liu 	return mult_delta;
568a568231fSLeilk Liu }
569a568231fSLeilk Liu 
mtk_spi_update_mdata_len(struct spi_controller * host)570cae15788SYang Yingliang static void mtk_spi_update_mdata_len(struct spi_controller *host)
571a568231fSLeilk Liu {
572a568231fSLeilk Liu 	int mult_delta;
573cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
574a568231fSLeilk Liu 
575a568231fSLeilk Liu 	if (mdata->tx_sgl_len && mdata->rx_sgl_len) {
576a568231fSLeilk Liu 		if (mdata->tx_sgl_len > mdata->rx_sgl_len) {
577cf82d0ecSzhichao.liu 			mult_delta = mtk_spi_get_mult_delta(mdata, mdata->rx_sgl_len);
578a568231fSLeilk Liu 			mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
579a568231fSLeilk Liu 			mdata->rx_sgl_len = mult_delta;
580a568231fSLeilk Liu 			mdata->tx_sgl_len -= mdata->xfer_len;
581a568231fSLeilk Liu 		} else {
582cf82d0ecSzhichao.liu 			mult_delta = mtk_spi_get_mult_delta(mdata, mdata->tx_sgl_len);
583a568231fSLeilk Liu 			mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
584a568231fSLeilk Liu 			mdata->tx_sgl_len = mult_delta;
585a568231fSLeilk Liu 			mdata->rx_sgl_len -= mdata->xfer_len;
586a568231fSLeilk Liu 		}
587a568231fSLeilk Liu 	} else if (mdata->tx_sgl_len) {
588cf82d0ecSzhichao.liu 		mult_delta = mtk_spi_get_mult_delta(mdata, mdata->tx_sgl_len);
589a568231fSLeilk Liu 		mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
590a568231fSLeilk Liu 		mdata->tx_sgl_len = mult_delta;
591a568231fSLeilk Liu 	} else if (mdata->rx_sgl_len) {
592cf82d0ecSzhichao.liu 		mult_delta = mtk_spi_get_mult_delta(mdata, mdata->rx_sgl_len);
593a568231fSLeilk Liu 		mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
594a568231fSLeilk Liu 		mdata->rx_sgl_len = mult_delta;
595a568231fSLeilk Liu 	}
596a568231fSLeilk Liu }
597a568231fSLeilk Liu 
mtk_spi_setup_dma_addr(struct spi_controller * host,struct spi_transfer * xfer)598cae15788SYang Yingliang static void mtk_spi_setup_dma_addr(struct spi_controller *host,
599a568231fSLeilk Liu 				   struct spi_transfer *xfer)
600a568231fSLeilk Liu {
601cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
602a568231fSLeilk Liu 
603fdeae8f5Sluhua.xu 	if (mdata->tx_sgl) {
604fdeae8f5Sluhua.xu 		writel((u32)(xfer->tx_dma & MTK_SPI_32BITS_MASK),
605fdeae8f5Sluhua.xu 		       mdata->base + SPI_TX_SRC_REG);
606fdeae8f5Sluhua.xu #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
607fdeae8f5Sluhua.xu 		if (mdata->dev_comp->dma_ext)
608fdeae8f5Sluhua.xu 			writel((u32)(xfer->tx_dma >> 32),
609fdeae8f5Sluhua.xu 			       mdata->base + SPI_TX_SRC_REG_64);
610fdeae8f5Sluhua.xu #endif
611fdeae8f5Sluhua.xu 	}
612fdeae8f5Sluhua.xu 
613fdeae8f5Sluhua.xu 	if (mdata->rx_sgl) {
614fdeae8f5Sluhua.xu 		writel((u32)(xfer->rx_dma & MTK_SPI_32BITS_MASK),
615fdeae8f5Sluhua.xu 		       mdata->base + SPI_RX_DST_REG);
616fdeae8f5Sluhua.xu #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
617fdeae8f5Sluhua.xu 		if (mdata->dev_comp->dma_ext)
618fdeae8f5Sluhua.xu 			writel((u32)(xfer->rx_dma >> 32),
619fdeae8f5Sluhua.xu 			       mdata->base + SPI_RX_DST_REG_64);
620fdeae8f5Sluhua.xu #endif
621fdeae8f5Sluhua.xu 	}
622a568231fSLeilk Liu }
623a568231fSLeilk Liu 
mtk_spi_fifo_transfer(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)624cae15788SYang Yingliang static int mtk_spi_fifo_transfer(struct spi_controller *host,
625a568231fSLeilk Liu 				 struct spi_device *spi,
626a568231fSLeilk Liu 				 struct spi_transfer *xfer)
627a568231fSLeilk Liu {
628de327e49SNicolas Boichat 	int cnt, remainder;
629de327e49SNicolas Boichat 	u32 reg_val;
630cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
631a568231fSLeilk Liu 
632a568231fSLeilk Liu 	mdata->cur_transfer = xfer;
6331ce24864SDaniel Kurtz 	mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, xfer->len);
63400bca73bSPeter Shih 	mdata->num_xfered = 0;
635cae15788SYang Yingliang 	mtk_spi_prepare_transfer(host, xfer->speed_hz);
636cae15788SYang Yingliang 	mtk_spi_setup_packet(host);
637a568231fSLeilk Liu 
6380d5c3954SGuenter Roeck 	if (xfer->tx_buf) {
639a568231fSLeilk Liu 		cnt = xfer->len / 4;
64044f636daSLeilk Liu 		iowrite32_rep(mdata->base + SPI_TX_DATA_REG, xfer->tx_buf, cnt);
641de327e49SNicolas Boichat 		remainder = xfer->len % 4;
642de327e49SNicolas Boichat 		if (remainder > 0) {
643de327e49SNicolas Boichat 			reg_val = 0;
644de327e49SNicolas Boichat 			memcpy(&reg_val, xfer->tx_buf + (cnt * 4), remainder);
645de327e49SNicolas Boichat 			writel(reg_val, mdata->base + SPI_TX_DATA_REG);
646de327e49SNicolas Boichat 		}
6473a70dd2dSPeter Hess 	}
648de327e49SNicolas Boichat 
649cae15788SYang Yingliang 	mtk_spi_enable_transfer(host);
650a568231fSLeilk Liu 
651a568231fSLeilk Liu 	return 1;
652a568231fSLeilk Liu }
653a568231fSLeilk Liu 
mtk_spi_dma_transfer(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)654cae15788SYang Yingliang static int mtk_spi_dma_transfer(struct spi_controller *host,
655a568231fSLeilk Liu 				struct spi_device *spi,
656a568231fSLeilk Liu 				struct spi_transfer *xfer)
657a568231fSLeilk Liu {
658a568231fSLeilk Liu 	int cmd;
659cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
660a568231fSLeilk Liu 
661a568231fSLeilk Liu 	mdata->tx_sgl = NULL;
662a568231fSLeilk Liu 	mdata->rx_sgl = NULL;
663a568231fSLeilk Liu 	mdata->tx_sgl_len = 0;
664a568231fSLeilk Liu 	mdata->rx_sgl_len = 0;
665a568231fSLeilk Liu 	mdata->cur_transfer = xfer;
66600bca73bSPeter Shih 	mdata->num_xfered = 0;
667a568231fSLeilk Liu 
668cae15788SYang Yingliang 	mtk_spi_prepare_transfer(host, xfer->speed_hz);
669a568231fSLeilk Liu 
670a568231fSLeilk Liu 	cmd = readl(mdata->base + SPI_CMD_REG);
671a568231fSLeilk Liu 	if (xfer->tx_buf)
672a568231fSLeilk Liu 		cmd |= SPI_CMD_TX_DMA;
673a568231fSLeilk Liu 	if (xfer->rx_buf)
674a568231fSLeilk Liu 		cmd |= SPI_CMD_RX_DMA;
675a568231fSLeilk Liu 	writel(cmd, mdata->base + SPI_CMD_REG);
676a568231fSLeilk Liu 
677a568231fSLeilk Liu 	if (xfer->tx_buf)
678a568231fSLeilk Liu 		mdata->tx_sgl = xfer->tx_sg.sgl;
679a568231fSLeilk Liu 	if (xfer->rx_buf)
680a568231fSLeilk Liu 		mdata->rx_sgl = xfer->rx_sg.sgl;
681a568231fSLeilk Liu 
682a568231fSLeilk Liu 	if (mdata->tx_sgl) {
683a568231fSLeilk Liu 		xfer->tx_dma = sg_dma_address(mdata->tx_sgl);
684a568231fSLeilk Liu 		mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
685a568231fSLeilk Liu 	}
686a568231fSLeilk Liu 	if (mdata->rx_sgl) {
687a568231fSLeilk Liu 		xfer->rx_dma = sg_dma_address(mdata->rx_sgl);
688a568231fSLeilk Liu 		mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
689a568231fSLeilk Liu 	}
690a568231fSLeilk Liu 
691cae15788SYang Yingliang 	mtk_spi_update_mdata_len(host);
692cae15788SYang Yingliang 	mtk_spi_setup_packet(host);
693cae15788SYang Yingliang 	mtk_spi_setup_dma_addr(host, xfer);
694cae15788SYang Yingliang 	mtk_spi_enable_transfer(host);
695a568231fSLeilk Liu 
696a568231fSLeilk Liu 	return 1;
697a568231fSLeilk Liu }
698a568231fSLeilk Liu 
mtk_spi_transfer_one(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)699cae15788SYang Yingliang static int mtk_spi_transfer_one(struct spi_controller *host,
700a568231fSLeilk Liu 				struct spi_device *spi,
701a568231fSLeilk Liu 				struct spi_transfer *xfer)
702a568231fSLeilk Liu {
703cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(spi->controller);
7047e963fb2SLeilk Liu 	u32 reg_val = 0;
7057e963fb2SLeilk Liu 
7067e963fb2SLeilk Liu 	/* prepare xfer direction and duplex mode */
7077e963fb2SLeilk Liu 	if (mdata->dev_comp->ipm_design) {
7087e963fb2SLeilk Liu 		if (!xfer->tx_buf || !xfer->rx_buf) {
7097e963fb2SLeilk Liu 			reg_val |= SPI_CFG3_IPM_HALF_DUPLEX_EN;
7107e963fb2SLeilk Liu 			if (xfer->rx_buf)
7117e963fb2SLeilk Liu 				reg_val |= SPI_CFG3_IPM_HALF_DUPLEX_DIR;
7127e963fb2SLeilk Liu 		}
7137e963fb2SLeilk Liu 		writel(reg_val, mdata->base + SPI_CFG3_IPM_REG);
7147e963fb2SLeilk Liu 	}
7157e963fb2SLeilk Liu 
716cae15788SYang Yingliang 	if (host->can_dma(host, spi, xfer))
717cae15788SYang Yingliang 		return mtk_spi_dma_transfer(host, spi, xfer);
718a568231fSLeilk Liu 	else
719cae15788SYang Yingliang 		return mtk_spi_fifo_transfer(host, spi, xfer);
720a568231fSLeilk Liu }
721a568231fSLeilk Liu 
mtk_spi_can_dma(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)722cae15788SYang Yingliang static bool mtk_spi_can_dma(struct spi_controller *host,
723a568231fSLeilk Liu 			    struct spi_device *spi,
724a568231fSLeilk Liu 			    struct spi_transfer *xfer)
725a568231fSLeilk Liu {
7261ce24864SDaniel Kurtz 	/* Buffers for DMA transactions must be 4-byte aligned */
7271ce24864SDaniel Kurtz 	return (xfer->len > MTK_SPI_MAX_FIFO_SIZE &&
7281ce24864SDaniel Kurtz 		(unsigned long)xfer->tx_buf % 4 == 0 &&
7291ce24864SDaniel Kurtz 		(unsigned long)xfer->rx_buf % 4 == 0);
730a568231fSLeilk Liu }
731a568231fSLeilk Liu 
mtk_spi_setup(struct spi_device * spi)73258a984c7SLeilk Liu static int mtk_spi_setup(struct spi_device *spi)
73358a984c7SLeilk Liu {
734cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(spi->controller);
73558a984c7SLeilk Liu 
73658a984c7SLeilk Liu 	if (!spi->controller_data)
73758a984c7SLeilk Liu 		spi->controller_data = (void *)&mtk_default_chip_info;
73858a984c7SLeilk Liu 
7399e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (mdata->dev_comp->need_pad_sel && spi_get_csgpiod(spi, 0))
7401a5a87d5SLinus Walleij 		/* CS de-asserted, gpiolib will handle inversion */
7419e264f3fSAmit Kumar Mahapatra via Alsa-devel 		gpiod_direction_output(spi_get_csgpiod(spi, 0), 0);
74237457607SLeilk Liu 
74358a984c7SLeilk Liu 	return 0;
74458a984c7SLeilk Liu }
74558a984c7SLeilk Liu 
mtk_spi_interrupt_thread(int irq,void * dev_id)746*5972eb05SAngeloGioacchino Del Regno static irqreturn_t mtk_spi_interrupt_thread(int irq, void *dev_id)
747a568231fSLeilk Liu {
74800bca73bSPeter Shih 	u32 cmd, reg_val, cnt, remainder, len;
749cae15788SYang Yingliang 	struct spi_controller *host = dev_id;
750cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
75110402419SFei Shao 	struct spi_transfer *xfer = mdata->cur_transfer;
752a568231fSLeilk Liu 
75310402419SFei Shao 	if (!host->can_dma(host, NULL, xfer)) {
75410402419SFei Shao 		if (xfer->rx_buf) {
75544f636daSLeilk Liu 			cnt = mdata->xfer_len / 4;
75644f636daSLeilk Liu 			ioread32_rep(mdata->base + SPI_RX_DATA_REG,
75710402419SFei Shao 				     xfer->rx_buf + mdata->num_xfered, cnt);
758de327e49SNicolas Boichat 			remainder = mdata->xfer_len % 4;
759de327e49SNicolas Boichat 			if (remainder > 0) {
760de327e49SNicolas Boichat 				reg_val = readl(mdata->base + SPI_RX_DATA_REG);
76110402419SFei Shao 				memcpy(xfer->rx_buf + (cnt * 4) + mdata->num_xfered,
76200bca73bSPeter Shih 					&reg_val,
76300bca73bSPeter Shih 					remainder);
764de327e49SNicolas Boichat 			}
765a568231fSLeilk Liu 		}
7661ce24864SDaniel Kurtz 
76700bca73bSPeter Shih 		mdata->num_xfered += mdata->xfer_len;
76810402419SFei Shao 		if (mdata->num_xfered == xfer->len) {
769cae15788SYang Yingliang 			spi_finalize_current_transfer(host);
770a568231fSLeilk Liu 			return IRQ_HANDLED;
771a568231fSLeilk Liu 		}
772a568231fSLeilk Liu 
77310402419SFei Shao 		len = xfer->len - mdata->num_xfered;
77400bca73bSPeter Shih 		mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, len);
775cae15788SYang Yingliang 		mtk_spi_setup_packet(host);
7761ce24864SDaniel Kurtz 
77710402419SFei Shao 		if (xfer->tx_buf) {
778a4d8f64fSLeilk Liu 			cnt = mdata->xfer_len / 4;
77900bca73bSPeter Shih 			iowrite32_rep(mdata->base + SPI_TX_DATA_REG,
78010402419SFei Shao 					xfer->tx_buf + mdata->num_xfered, cnt);
7811ce24864SDaniel Kurtz 
782a4d8f64fSLeilk Liu 			remainder = mdata->xfer_len % 4;
7831ce24864SDaniel Kurtz 			if (remainder > 0) {
7841ce24864SDaniel Kurtz 				reg_val = 0;
78500bca73bSPeter Shih 				memcpy(&reg_val,
78610402419SFei Shao 					xfer->tx_buf + (cnt * 4) + mdata->num_xfered,
78700bca73bSPeter Shih 					remainder);
7881ce24864SDaniel Kurtz 				writel(reg_val, mdata->base + SPI_TX_DATA_REG);
7891ce24864SDaniel Kurtz 			}
790a20ad450SFei Shao 		}
7911ce24864SDaniel Kurtz 
792cae15788SYang Yingliang 		mtk_spi_enable_transfer(host);
7931ce24864SDaniel Kurtz 
7941ce24864SDaniel Kurtz 		return IRQ_HANDLED;
7951ce24864SDaniel Kurtz 	}
7961ce24864SDaniel Kurtz 
797a568231fSLeilk Liu 	if (mdata->tx_sgl)
79810402419SFei Shao 		xfer->tx_dma += mdata->xfer_len;
799a568231fSLeilk Liu 	if (mdata->rx_sgl)
80010402419SFei Shao 		xfer->rx_dma += mdata->xfer_len;
801a568231fSLeilk Liu 
802a568231fSLeilk Liu 	if (mdata->tx_sgl && (mdata->tx_sgl_len == 0)) {
803a568231fSLeilk Liu 		mdata->tx_sgl = sg_next(mdata->tx_sgl);
804a568231fSLeilk Liu 		if (mdata->tx_sgl) {
80510402419SFei Shao 			xfer->tx_dma = sg_dma_address(mdata->tx_sgl);
806a568231fSLeilk Liu 			mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
807a568231fSLeilk Liu 		}
808a568231fSLeilk Liu 	}
809a568231fSLeilk Liu 	if (mdata->rx_sgl && (mdata->rx_sgl_len == 0)) {
810a568231fSLeilk Liu 		mdata->rx_sgl = sg_next(mdata->rx_sgl);
811a568231fSLeilk Liu 		if (mdata->rx_sgl) {
81210402419SFei Shao 			xfer->rx_dma = sg_dma_address(mdata->rx_sgl);
813a568231fSLeilk Liu 			mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
814a568231fSLeilk Liu 		}
815a568231fSLeilk Liu 	}
816a568231fSLeilk Liu 
817a568231fSLeilk Liu 	if (!mdata->tx_sgl && !mdata->rx_sgl) {
818a568231fSLeilk Liu 		/* spi disable dma */
819a568231fSLeilk Liu 		cmd = readl(mdata->base + SPI_CMD_REG);
820a568231fSLeilk Liu 		cmd &= ~SPI_CMD_TX_DMA;
821a568231fSLeilk Liu 		cmd &= ~SPI_CMD_RX_DMA;
822a568231fSLeilk Liu 		writel(cmd, mdata->base + SPI_CMD_REG);
823a568231fSLeilk Liu 
824cae15788SYang Yingliang 		spi_finalize_current_transfer(host);
825a568231fSLeilk Liu 		return IRQ_HANDLED;
826a568231fSLeilk Liu 	}
827a568231fSLeilk Liu 
828cae15788SYang Yingliang 	mtk_spi_update_mdata_len(host);
829cae15788SYang Yingliang 	mtk_spi_setup_packet(host);
83010402419SFei Shao 	mtk_spi_setup_dma_addr(host, xfer);
831cae15788SYang Yingliang 	mtk_spi_enable_transfer(host);
832a568231fSLeilk Liu 
833a568231fSLeilk Liu 	return IRQ_HANDLED;
834a568231fSLeilk Liu }
835a568231fSLeilk Liu 
mtk_spi_interrupt(int irq,void * dev_id)836*5972eb05SAngeloGioacchino Del Regno static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id)
837*5972eb05SAngeloGioacchino Del Regno {
838*5972eb05SAngeloGioacchino Del Regno 	struct spi_controller *host = dev_id;
839*5972eb05SAngeloGioacchino Del Regno 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
840*5972eb05SAngeloGioacchino Del Regno 	u32 reg_val;
841*5972eb05SAngeloGioacchino Del Regno 
842*5972eb05SAngeloGioacchino Del Regno 	reg_val = readl(mdata->base + SPI_STATUS0_REG);
843*5972eb05SAngeloGioacchino Del Regno 	if (reg_val & MTK_SPI_PAUSE_INT_STATUS)
844*5972eb05SAngeloGioacchino Del Regno 		mdata->state = MTK_SPI_PAUSED;
845*5972eb05SAngeloGioacchino Del Regno 	else
846*5972eb05SAngeloGioacchino Del Regno 		mdata->state = MTK_SPI_IDLE;
847*5972eb05SAngeloGioacchino Del Regno 
848*5972eb05SAngeloGioacchino Del Regno 	/* SPI-MEM ops */
849*5972eb05SAngeloGioacchino Del Regno 	if (mdata->use_spimem) {
850*5972eb05SAngeloGioacchino Del Regno 		complete(&mdata->spimem_done);
851*5972eb05SAngeloGioacchino Del Regno 		return IRQ_HANDLED;
852*5972eb05SAngeloGioacchino Del Regno 	}
853*5972eb05SAngeloGioacchino Del Regno 
854*5972eb05SAngeloGioacchino Del Regno 	return IRQ_WAKE_THREAD;
855*5972eb05SAngeloGioacchino Del Regno }
856*5972eb05SAngeloGioacchino Del Regno 
mtk_spi_mem_adjust_op_size(struct spi_mem * mem,struct spi_mem_op * op)8579f763fd2SLeilk Liu static int mtk_spi_mem_adjust_op_size(struct spi_mem *mem,
8589f763fd2SLeilk Liu 				      struct spi_mem_op *op)
8599f763fd2SLeilk Liu {
8609f763fd2SLeilk Liu 	int opcode_len;
8619f763fd2SLeilk Liu 
8629f763fd2SLeilk Liu 	if (op->data.dir != SPI_MEM_NO_DATA) {
8639f763fd2SLeilk Liu 		opcode_len = 1 + op->addr.nbytes + op->dummy.nbytes;
8649f763fd2SLeilk Liu 		if (opcode_len + op->data.nbytes > MTK_SPI_IPM_PACKET_SIZE) {
8659f763fd2SLeilk Liu 			op->data.nbytes = MTK_SPI_IPM_PACKET_SIZE - opcode_len;
8669f763fd2SLeilk Liu 			/* force data buffer dma-aligned. */
8679f763fd2SLeilk Liu 			op->data.nbytes -= op->data.nbytes % 4;
8689f763fd2SLeilk Liu 		}
8699f763fd2SLeilk Liu 	}
8709f763fd2SLeilk Liu 
8719f763fd2SLeilk Liu 	return 0;
8729f763fd2SLeilk Liu }
8739f763fd2SLeilk Liu 
mtk_spi_mem_supports_op(struct spi_mem * mem,const struct spi_mem_op * op)8749f763fd2SLeilk Liu static bool mtk_spi_mem_supports_op(struct spi_mem *mem,
8759f763fd2SLeilk Liu 				    const struct spi_mem_op *op)
8769f763fd2SLeilk Liu {
8779f763fd2SLeilk Liu 	if (!spi_mem_default_supports_op(mem, op))
8789f763fd2SLeilk Liu 		return false;
8799f763fd2SLeilk Liu 
8809f763fd2SLeilk Liu 	if (op->addr.nbytes && op->dummy.nbytes &&
8819f763fd2SLeilk Liu 	    op->addr.buswidth != op->dummy.buswidth)
8829f763fd2SLeilk Liu 		return false;
8839f763fd2SLeilk Liu 
8849f763fd2SLeilk Liu 	if (op->addr.nbytes + op->dummy.nbytes > 16)
8859f763fd2SLeilk Liu 		return false;
8869f763fd2SLeilk Liu 
8879f763fd2SLeilk Liu 	if (op->data.nbytes > MTK_SPI_IPM_PACKET_SIZE) {
8889f763fd2SLeilk Liu 		if (op->data.nbytes / MTK_SPI_IPM_PACKET_SIZE >
8899f763fd2SLeilk Liu 		    MTK_SPI_IPM_PACKET_LOOP ||
8909f763fd2SLeilk Liu 		    op->data.nbytes % MTK_SPI_IPM_PACKET_SIZE != 0)
8919f763fd2SLeilk Liu 			return false;
8929f763fd2SLeilk Liu 	}
8939f763fd2SLeilk Liu 
8949f763fd2SLeilk Liu 	return true;
8959f763fd2SLeilk Liu }
8969f763fd2SLeilk Liu 
mtk_spi_mem_setup_dma_xfer(struct spi_controller * host,const struct spi_mem_op * op)897cae15788SYang Yingliang static void mtk_spi_mem_setup_dma_xfer(struct spi_controller *host,
8989f763fd2SLeilk Liu 				       const struct spi_mem_op *op)
8999f763fd2SLeilk Liu {
900cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
9019f763fd2SLeilk Liu 
9029f763fd2SLeilk Liu 	writel((u32)(mdata->tx_dma & MTK_SPI_32BITS_MASK),
9039f763fd2SLeilk Liu 	       mdata->base + SPI_TX_SRC_REG);
9049f763fd2SLeilk Liu #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
9059f763fd2SLeilk Liu 	if (mdata->dev_comp->dma_ext)
9069f763fd2SLeilk Liu 		writel((u32)(mdata->tx_dma >> 32),
9079f763fd2SLeilk Liu 		       mdata->base + SPI_TX_SRC_REG_64);
9089f763fd2SLeilk Liu #endif
9099f763fd2SLeilk Liu 
9109f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_DATA_IN) {
9119f763fd2SLeilk Liu 		writel((u32)(mdata->rx_dma & MTK_SPI_32BITS_MASK),
9129f763fd2SLeilk Liu 		       mdata->base + SPI_RX_DST_REG);
9139f763fd2SLeilk Liu #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
9149f763fd2SLeilk Liu 		if (mdata->dev_comp->dma_ext)
9159f763fd2SLeilk Liu 			writel((u32)(mdata->rx_dma >> 32),
9169f763fd2SLeilk Liu 			       mdata->base + SPI_RX_DST_REG_64);
9179f763fd2SLeilk Liu #endif
9189f763fd2SLeilk Liu 	}
9199f763fd2SLeilk Liu }
9209f763fd2SLeilk Liu 
mtk_spi_transfer_wait(struct spi_mem * mem,const struct spi_mem_op * op)9219f763fd2SLeilk Liu static int mtk_spi_transfer_wait(struct spi_mem *mem,
9229f763fd2SLeilk Liu 				 const struct spi_mem_op *op)
9239f763fd2SLeilk Liu {
924cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(mem->spi->controller);
9259f763fd2SLeilk Liu 	/*
9269f763fd2SLeilk Liu 	 * For each byte we wait for 8 cycles of the SPI clock.
9279f763fd2SLeilk Liu 	 * Since speed is defined in Hz and we want milliseconds,
9289f763fd2SLeilk Liu 	 * so it should be 8 * 1000.
9299f763fd2SLeilk Liu 	 */
9309f763fd2SLeilk Liu 	u64 ms = 8000LL;
9319f763fd2SLeilk Liu 
9329f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_NO_DATA)
9339f763fd2SLeilk Liu 		ms *= 32; /* prevent we may get 0 for short transfers. */
9349f763fd2SLeilk Liu 	else
9359f763fd2SLeilk Liu 		ms *= op->data.nbytes;
9369f763fd2SLeilk Liu 	ms = div_u64(ms, mem->spi->max_speed_hz);
9379f763fd2SLeilk Liu 	ms += ms + 1000; /* 1s tolerance */
9389f763fd2SLeilk Liu 
9399f763fd2SLeilk Liu 	if (ms > UINT_MAX)
9409f763fd2SLeilk Liu 		ms = UINT_MAX;
9419f763fd2SLeilk Liu 
9429f763fd2SLeilk Liu 	if (!wait_for_completion_timeout(&mdata->spimem_done,
9439f763fd2SLeilk Liu 					 msecs_to_jiffies(ms))) {
9449f763fd2SLeilk Liu 		dev_err(mdata->dev, "spi-mem transfer timeout\n");
9459f763fd2SLeilk Liu 		return -ETIMEDOUT;
9469f763fd2SLeilk Liu 	}
9479f763fd2SLeilk Liu 
9489f763fd2SLeilk Liu 	return 0;
9499f763fd2SLeilk Liu }
9509f763fd2SLeilk Liu 
mtk_spi_mem_exec_op(struct spi_mem * mem,const struct spi_mem_op * op)9519f763fd2SLeilk Liu static int mtk_spi_mem_exec_op(struct spi_mem *mem,
9529f763fd2SLeilk Liu 			       const struct spi_mem_op *op)
9539f763fd2SLeilk Liu {
954cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(mem->spi->controller);
9559f763fd2SLeilk Liu 	u32 reg_val, nio, tx_size;
9569f763fd2SLeilk Liu 	char *tx_tmp_buf, *rx_tmp_buf;
9579f763fd2SLeilk Liu 	int ret = 0;
9589f763fd2SLeilk Liu 
9599f763fd2SLeilk Liu 	mdata->use_spimem = true;
9609f763fd2SLeilk Liu 	reinit_completion(&mdata->spimem_done);
9619f763fd2SLeilk Liu 
9629f763fd2SLeilk Liu 	mtk_spi_reset(mdata);
963cae15788SYang Yingliang 	mtk_spi_hw_init(mem->spi->controller, mem->spi);
964cae15788SYang Yingliang 	mtk_spi_prepare_transfer(mem->spi->controller, mem->spi->max_speed_hz);
9659f763fd2SLeilk Liu 
9669f763fd2SLeilk Liu 	reg_val = readl(mdata->base + SPI_CFG3_IPM_REG);
9679f763fd2SLeilk Liu 	/* opcode byte len */
9689f763fd2SLeilk Liu 	reg_val &= ~SPI_CFG3_IPM_CMD_BYTELEN_MASK;
9699f763fd2SLeilk Liu 	reg_val |= 1 << SPI_CFG3_IPM_CMD_BYTELEN_OFFSET;
9709f763fd2SLeilk Liu 
9719f763fd2SLeilk Liu 	/* addr & dummy byte len */
9729f763fd2SLeilk Liu 	reg_val &= ~SPI_CFG3_IPM_ADDR_BYTELEN_MASK;
9739f763fd2SLeilk Liu 	if (op->addr.nbytes || op->dummy.nbytes)
9749f763fd2SLeilk Liu 		reg_val |= (op->addr.nbytes + op->dummy.nbytes) <<
9759f763fd2SLeilk Liu 			    SPI_CFG3_IPM_ADDR_BYTELEN_OFFSET;
9769f763fd2SLeilk Liu 
9779f763fd2SLeilk Liu 	/* data byte len */
9789f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_NO_DATA) {
9799f763fd2SLeilk Liu 		reg_val |= SPI_CFG3_IPM_NODATA_FLAG;
9809f763fd2SLeilk Liu 		writel(0, mdata->base + SPI_CFG1_REG);
9819f763fd2SLeilk Liu 	} else {
9829f763fd2SLeilk Liu 		reg_val &= ~SPI_CFG3_IPM_NODATA_FLAG;
9839f763fd2SLeilk Liu 		mdata->xfer_len = op->data.nbytes;
984cae15788SYang Yingliang 		mtk_spi_setup_packet(mem->spi->controller);
9859f763fd2SLeilk Liu 	}
9869f763fd2SLeilk Liu 
9879f763fd2SLeilk Liu 	if (op->addr.nbytes || op->dummy.nbytes) {
9889f763fd2SLeilk Liu 		if (op->addr.buswidth == 1 || op->dummy.buswidth == 1)
9899f763fd2SLeilk Liu 			reg_val |= SPI_CFG3_IPM_XMODE_EN;
9909f763fd2SLeilk Liu 		else
9919f763fd2SLeilk Liu 			reg_val &= ~SPI_CFG3_IPM_XMODE_EN;
9929f763fd2SLeilk Liu 	}
9939f763fd2SLeilk Liu 
9949f763fd2SLeilk Liu 	if (op->addr.buswidth == 2 ||
9959f763fd2SLeilk Liu 	    op->dummy.buswidth == 2 ||
9969f763fd2SLeilk Liu 	    op->data.buswidth == 2)
9979f763fd2SLeilk Liu 		nio = 2;
9989f763fd2SLeilk Liu 	else if (op->addr.buswidth == 4 ||
9999f763fd2SLeilk Liu 		 op->dummy.buswidth == 4 ||
10009f763fd2SLeilk Liu 		 op->data.buswidth == 4)
10019f763fd2SLeilk Liu 		nio = 4;
10029f763fd2SLeilk Liu 	else
10039f763fd2SLeilk Liu 		nio = 1;
10049f763fd2SLeilk Liu 
10059f763fd2SLeilk Liu 	reg_val &= ~SPI_CFG3_IPM_CMD_PIN_MODE_MASK;
10069f763fd2SLeilk Liu 	reg_val |= PIN_MODE_CFG(nio);
10079f763fd2SLeilk Liu 
10089f763fd2SLeilk Liu 	reg_val |= SPI_CFG3_IPM_HALF_DUPLEX_EN;
10099f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_DATA_IN)
10109f763fd2SLeilk Liu 		reg_val |= SPI_CFG3_IPM_HALF_DUPLEX_DIR;
10119f763fd2SLeilk Liu 	else
10129f763fd2SLeilk Liu 		reg_val &= ~SPI_CFG3_IPM_HALF_DUPLEX_DIR;
10139f763fd2SLeilk Liu 	writel(reg_val, mdata->base + SPI_CFG3_IPM_REG);
10149f763fd2SLeilk Liu 
10159f763fd2SLeilk Liu 	tx_size = 1 + op->addr.nbytes + op->dummy.nbytes;
10169f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_DATA_OUT)
10179f763fd2SLeilk Liu 		tx_size += op->data.nbytes;
10189f763fd2SLeilk Liu 
10199f763fd2SLeilk Liu 	tx_size = max_t(u32, tx_size, 32);
10209f763fd2SLeilk Liu 
10219f763fd2SLeilk Liu 	tx_tmp_buf = kzalloc(tx_size, GFP_KERNEL | GFP_DMA);
10229f763fd2SLeilk Liu 	if (!tx_tmp_buf) {
10239f763fd2SLeilk Liu 		mdata->use_spimem = false;
10249f763fd2SLeilk Liu 		return -ENOMEM;
10259f763fd2SLeilk Liu 	}
10269f763fd2SLeilk Liu 
10279f763fd2SLeilk Liu 	tx_tmp_buf[0] = op->cmd.opcode;
10289f763fd2SLeilk Liu 
10299f763fd2SLeilk Liu 	if (op->addr.nbytes) {
10309f763fd2SLeilk Liu 		int i;
10319f763fd2SLeilk Liu 
10329f763fd2SLeilk Liu 		for (i = 0; i < op->addr.nbytes; i++)
10339f763fd2SLeilk Liu 			tx_tmp_buf[i + 1] = op->addr.val >>
10349f763fd2SLeilk Liu 					(8 * (op->addr.nbytes - i - 1));
10359f763fd2SLeilk Liu 	}
10369f763fd2SLeilk Liu 
10379f763fd2SLeilk Liu 	if (op->dummy.nbytes)
10389f763fd2SLeilk Liu 		memset(tx_tmp_buf + op->addr.nbytes + 1,
10399f763fd2SLeilk Liu 		       0xff,
10409f763fd2SLeilk Liu 		       op->dummy.nbytes);
10419f763fd2SLeilk Liu 
10429f763fd2SLeilk Liu 	if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
10439f763fd2SLeilk Liu 		memcpy(tx_tmp_buf + op->dummy.nbytes + op->addr.nbytes + 1,
10449f763fd2SLeilk Liu 		       op->data.buf.out,
10459f763fd2SLeilk Liu 		       op->data.nbytes);
10469f763fd2SLeilk Liu 
10479f763fd2SLeilk Liu 	mdata->tx_dma = dma_map_single(mdata->dev, tx_tmp_buf,
10489f763fd2SLeilk Liu 				       tx_size, DMA_TO_DEVICE);
10499f763fd2SLeilk Liu 	if (dma_mapping_error(mdata->dev, mdata->tx_dma)) {
10509f763fd2SLeilk Liu 		ret = -ENOMEM;
10519f763fd2SLeilk Liu 		goto err_exit;
10529f763fd2SLeilk Liu 	}
10539f763fd2SLeilk Liu 
10549f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_DATA_IN) {
10559f763fd2SLeilk Liu 		if (!IS_ALIGNED((size_t)op->data.buf.in, 4)) {
10569f763fd2SLeilk Liu 			rx_tmp_buf = kzalloc(op->data.nbytes,
10579f763fd2SLeilk Liu 					     GFP_KERNEL | GFP_DMA);
10589f763fd2SLeilk Liu 			if (!rx_tmp_buf) {
10599f763fd2SLeilk Liu 				ret = -ENOMEM;
10609f763fd2SLeilk Liu 				goto unmap_tx_dma;
10619f763fd2SLeilk Liu 			}
10629f763fd2SLeilk Liu 		} else {
10639f763fd2SLeilk Liu 			rx_tmp_buf = op->data.buf.in;
10649f763fd2SLeilk Liu 		}
10659f763fd2SLeilk Liu 
10669f763fd2SLeilk Liu 		mdata->rx_dma = dma_map_single(mdata->dev,
10679f763fd2SLeilk Liu 					       rx_tmp_buf,
10689f763fd2SLeilk Liu 					       op->data.nbytes,
10699f763fd2SLeilk Liu 					       DMA_FROM_DEVICE);
10709f763fd2SLeilk Liu 		if (dma_mapping_error(mdata->dev, mdata->rx_dma)) {
10719f763fd2SLeilk Liu 			ret = -ENOMEM;
10729f763fd2SLeilk Liu 			goto kfree_rx_tmp_buf;
10739f763fd2SLeilk Liu 		}
10749f763fd2SLeilk Liu 	}
10759f763fd2SLeilk Liu 
10769f763fd2SLeilk Liu 	reg_val = readl(mdata->base + SPI_CMD_REG);
10779f763fd2SLeilk Liu 	reg_val |= SPI_CMD_TX_DMA;
10789f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_DATA_IN)
10799f763fd2SLeilk Liu 		reg_val |= SPI_CMD_RX_DMA;
10809f763fd2SLeilk Liu 	writel(reg_val, mdata->base + SPI_CMD_REG);
10819f763fd2SLeilk Liu 
1082cae15788SYang Yingliang 	mtk_spi_mem_setup_dma_xfer(mem->spi->controller, op);
10839f763fd2SLeilk Liu 
1084cae15788SYang Yingliang 	mtk_spi_enable_transfer(mem->spi->controller);
10859f763fd2SLeilk Liu 
10869f763fd2SLeilk Liu 	/* Wait for the interrupt. */
10879f763fd2SLeilk Liu 	ret = mtk_spi_transfer_wait(mem, op);
10889f763fd2SLeilk Liu 	if (ret)
10899f763fd2SLeilk Liu 		goto unmap_rx_dma;
10909f763fd2SLeilk Liu 
10919f763fd2SLeilk Liu 	/* spi disable dma */
10929f763fd2SLeilk Liu 	reg_val = readl(mdata->base + SPI_CMD_REG);
10939f763fd2SLeilk Liu 	reg_val &= ~SPI_CMD_TX_DMA;
10949f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_DATA_IN)
10959f763fd2SLeilk Liu 		reg_val &= ~SPI_CMD_RX_DMA;
10969f763fd2SLeilk Liu 	writel(reg_val, mdata->base + SPI_CMD_REG);
10979f763fd2SLeilk Liu 
10989f763fd2SLeilk Liu unmap_rx_dma:
10999f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_DATA_IN) {
11009f763fd2SLeilk Liu 		dma_unmap_single(mdata->dev, mdata->rx_dma,
11019f763fd2SLeilk Liu 				 op->data.nbytes, DMA_FROM_DEVICE);
11029f763fd2SLeilk Liu 		if (!IS_ALIGNED((size_t)op->data.buf.in, 4))
11039f763fd2SLeilk Liu 			memcpy(op->data.buf.in, rx_tmp_buf, op->data.nbytes);
11049f763fd2SLeilk Liu 	}
11059f763fd2SLeilk Liu kfree_rx_tmp_buf:
11069f763fd2SLeilk Liu 	if (op->data.dir == SPI_MEM_DATA_IN &&
11079f763fd2SLeilk Liu 	    !IS_ALIGNED((size_t)op->data.buf.in, 4))
11089f763fd2SLeilk Liu 		kfree(rx_tmp_buf);
11099f763fd2SLeilk Liu unmap_tx_dma:
11109f763fd2SLeilk Liu 	dma_unmap_single(mdata->dev, mdata->tx_dma,
11119f763fd2SLeilk Liu 			 tx_size, DMA_TO_DEVICE);
11129f763fd2SLeilk Liu err_exit:
11139f763fd2SLeilk Liu 	kfree(tx_tmp_buf);
11149f763fd2SLeilk Liu 	mdata->use_spimem = false;
11159f763fd2SLeilk Liu 
11169f763fd2SLeilk Liu 	return ret;
11179f763fd2SLeilk Liu }
11189f763fd2SLeilk Liu 
11199f763fd2SLeilk Liu static const struct spi_controller_mem_ops mtk_spi_mem_ops = {
11209f763fd2SLeilk Liu 	.adjust_op_size = mtk_spi_mem_adjust_op_size,
11219f763fd2SLeilk Liu 	.supports_op = mtk_spi_mem_supports_op,
11229f763fd2SLeilk Liu 	.exec_op = mtk_spi_mem_exec_op,
11239f763fd2SLeilk Liu };
11249f763fd2SLeilk Liu 
mtk_spi_probe(struct platform_device * pdev)1125a568231fSLeilk Liu static int mtk_spi_probe(struct platform_device *pdev)
1126a568231fSLeilk Liu {
11276b444058SAngeloGioacchino Del Regno 	struct device *dev = &pdev->dev;
1128cae15788SYang Yingliang 	struct spi_controller *host;
1129a568231fSLeilk Liu 	struct mtk_spi *mdata;
1130fdeae8f5Sluhua.xu 	int i, irq, ret, addr_bits;
1131a568231fSLeilk Liu 
1132cae15788SYang Yingliang 	host = devm_spi_alloc_host(dev, sizeof(*mdata));
1133cae15788SYang Yingliang 	if (!host)
1134cae15788SYang Yingliang 		return dev_err_probe(dev, -ENOMEM, "failed to alloc spi host\n");
1135a568231fSLeilk Liu 
1136cae15788SYang Yingliang 	host->auto_runtime_pm = true;
1137cae15788SYang Yingliang 	host->dev.of_node = dev->of_node;
1138cae15788SYang Yingliang 	host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
1139a568231fSLeilk Liu 
1140cae15788SYang Yingliang 	host->set_cs = mtk_spi_set_cs;
1141cae15788SYang Yingliang 	host->prepare_message = mtk_spi_prepare_message;
1142cae15788SYang Yingliang 	host->transfer_one = mtk_spi_transfer_one;
1143cae15788SYang Yingliang 	host->can_dma = mtk_spi_can_dma;
1144cae15788SYang Yingliang 	host->setup = mtk_spi_setup;
1145cae15788SYang Yingliang 	host->set_cs_timing = mtk_spi_set_hw_cs_timing;
1146cae15788SYang Yingliang 	host->use_gpio_descriptors = true;
1147a568231fSLeilk Liu 
1148cae15788SYang Yingliang 	mdata = spi_controller_get_devdata(host);
11496b444058SAngeloGioacchino Del Regno 	mdata->dev_comp = device_get_match_data(dev);
1150ae7c2d34SLuhua Xu 
1151ae7c2d34SLuhua Xu 	if (mdata->dev_comp->enhance_timing)
1152cae15788SYang Yingliang 		host->mode_bits |= SPI_CS_HIGH;
1153ae7c2d34SLuhua Xu 
1154a568231fSLeilk Liu 	if (mdata->dev_comp->must_tx)
1155cae15788SYang Yingliang 		host->flags = SPI_CONTROLLER_MUST_TX;
11567e963fb2SLeilk Liu 	if (mdata->dev_comp->ipm_design)
1157cae15788SYang Yingliang 		host->mode_bits |= SPI_LOOP | SPI_RX_DUAL | SPI_TX_DUAL |
1158dcb2d274SQii Wang 				   SPI_RX_QUAD | SPI_TX_QUAD;
1159a568231fSLeilk Liu 
11609f763fd2SLeilk Liu 	if (mdata->dev_comp->ipm_design) {
11616b444058SAngeloGioacchino Del Regno 		mdata->dev = dev;
1162cae15788SYang Yingliang 		host->mem_ops = &mtk_spi_mem_ops;
11639f763fd2SLeilk Liu 		init_completion(&mdata->spimem_done);
11649f763fd2SLeilk Liu 	}
11659f763fd2SLeilk Liu 
1166a568231fSLeilk Liu 	if (mdata->dev_comp->need_pad_sel) {
11676b444058SAngeloGioacchino Del Regno 		mdata->pad_num = of_property_count_u32_elems(dev->of_node,
116837457607SLeilk Liu 			"mediatek,pad-select");
116920cdbb80SAngeloGioacchino Del Regno 		if (mdata->pad_num < 0)
117020cdbb80SAngeloGioacchino Del Regno 			return dev_err_probe(dev, -EINVAL,
117137457607SLeilk Liu 				"No 'mediatek,pad-select' property\n");
1172a568231fSLeilk Liu 
11736b444058SAngeloGioacchino Del Regno 		mdata->pad_sel = devm_kmalloc_array(dev, mdata->pad_num,
117437457607SLeilk Liu 						    sizeof(u32), GFP_KERNEL);
1175ace14580SAngeloGioacchino Del Regno 		if (!mdata->pad_sel)
1176ace14580SAngeloGioacchino Del Regno 			return -ENOMEM;
117737457607SLeilk Liu 
117837457607SLeilk Liu 		for (i = 0; i < mdata->pad_num; i++) {
11796b444058SAngeloGioacchino Del Regno 			of_property_read_u32_index(dev->of_node,
118037457607SLeilk Liu 						   "mediatek,pad-select",
118137457607SLeilk Liu 						   i, &mdata->pad_sel[i]);
118220cdbb80SAngeloGioacchino Del Regno 			if (mdata->pad_sel[i] > MT8173_SPI_MAX_PAD_SEL)
118320cdbb80SAngeloGioacchino Del Regno 				return dev_err_probe(dev, -EINVAL,
118420cdbb80SAngeloGioacchino Del Regno 						     "wrong pad-sel[%d]: %u\n",
118537457607SLeilk Liu 						     i, mdata->pad_sel[i]);
1186a568231fSLeilk Liu 		}
118737457607SLeilk Liu 	}
1188a568231fSLeilk Liu 
1189cae15788SYang Yingliang 	platform_set_drvdata(pdev, host);
11905dd381e7SMarkus Elfring 	mdata->base = devm_platform_ioremap_resource(pdev, 0);
1191ace14580SAngeloGioacchino Del Regno 	if (IS_ERR(mdata->base))
1192ace14580SAngeloGioacchino Del Regno 		return PTR_ERR(mdata->base);
1193a568231fSLeilk Liu 
1194a568231fSLeilk Liu 	irq = platform_get_irq(pdev, 0);
1195ace14580SAngeloGioacchino Del Regno 	if (irq < 0)
1196ace14580SAngeloGioacchino Del Regno 		return irq;
1197a568231fSLeilk Liu 
11986b444058SAngeloGioacchino Del Regno 	if (!dev->dma_mask)
11996b444058SAngeloGioacchino Del Regno 		dev->dma_mask = &dev->coherent_dma_mask;
1200a568231fSLeilk Liu 
1201309e9854Szhichao.liu 	if (mdata->dev_comp->ipm_design)
1202309e9854Szhichao.liu 		dma_set_max_seg_size(dev, SZ_16M);
1203309e9854Szhichao.liu 	else
1204309e9854Szhichao.liu 		dma_set_max_seg_size(dev, SZ_256K);
1205309e9854Szhichao.liu 
12066b444058SAngeloGioacchino Del Regno 	mdata->parent_clk = devm_clk_get(dev, "parent-clk");
120720cdbb80SAngeloGioacchino Del Regno 	if (IS_ERR(mdata->parent_clk))
120820cdbb80SAngeloGioacchino Del Regno 		return dev_err_probe(dev, PTR_ERR(mdata->parent_clk),
120920cdbb80SAngeloGioacchino Del Regno 				     "failed to get parent-clk\n");
1210a568231fSLeilk Liu 
12116b444058SAngeloGioacchino Del Regno 	mdata->sel_clk = devm_clk_get(dev, "sel-clk");
121220cdbb80SAngeloGioacchino Del Regno 	if (IS_ERR(mdata->sel_clk))
121320cdbb80SAngeloGioacchino Del Regno 		return dev_err_probe(dev, PTR_ERR(mdata->sel_clk), "failed to get sel-clk\n");
1214adcbcfeaSLeilk Liu 
12156b444058SAngeloGioacchino Del Regno 	mdata->spi_clk = devm_clk_get(dev, "spi-clk");
121620cdbb80SAngeloGioacchino Del Regno 	if (IS_ERR(mdata->spi_clk))
121720cdbb80SAngeloGioacchino Del Regno 		return dev_err_probe(dev, PTR_ERR(mdata->spi_clk), "failed to get spi-clk\n");
1218adcbcfeaSLeilk Liu 
12196b444058SAngeloGioacchino Del Regno 	mdata->spi_hclk = devm_clk_get_optional(dev, "hclk");
122020cdbb80SAngeloGioacchino Del Regno 	if (IS_ERR(mdata->spi_hclk))
122120cdbb80SAngeloGioacchino Del Regno 		return dev_err_probe(dev, PTR_ERR(mdata->spi_hclk), "failed to get hclk\n");
1222a740f4e6SLeilk Liu 
12235dee8bb8SAngeloGioacchino Del Regno 	ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk);
122420cdbb80SAngeloGioacchino Del Regno 	if (ret < 0)
122520cdbb80SAngeloGioacchino Del Regno 		return dev_err_probe(dev, ret, "failed to clk_set_parent\n");
12265dee8bb8SAngeloGioacchino Del Regno 
1227a740f4e6SLeilk Liu 	ret = clk_prepare_enable(mdata->spi_hclk);
122820cdbb80SAngeloGioacchino Del Regno 	if (ret < 0)
122920cdbb80SAngeloGioacchino Del Regno 		return dev_err_probe(dev, ret, "failed to enable hclk\n");
1230a740f4e6SLeilk Liu 
1231a568231fSLeilk Liu 	ret = clk_prepare_enable(mdata->spi_clk);
1232a568231fSLeilk Liu 	if (ret < 0) {
12335dee8bb8SAngeloGioacchino Del Regno 		clk_disable_unprepare(mdata->spi_hclk);
123420cdbb80SAngeloGioacchino Del Regno 		return dev_err_probe(dev, ret, "failed to enable spi_clk\n");
1235a568231fSLeilk Liu 	}
1236a568231fSLeilk Liu 
1237162a31efSMason Zhang 	mdata->spi_clk_hz = clk_get_rate(mdata->spi_clk);
1238162a31efSMason Zhang 
1239a740f4e6SLeilk Liu 	if (mdata->dev_comp->no_need_unprepare) {
1240162a31efSMason Zhang 		clk_disable(mdata->spi_clk);
1241a740f4e6SLeilk Liu 		clk_disable(mdata->spi_hclk);
1242a740f4e6SLeilk Liu 	} else {
1243a568231fSLeilk Liu 		clk_disable_unprepare(mdata->spi_clk);
1244a740f4e6SLeilk Liu 		clk_disable_unprepare(mdata->spi_hclk);
1245a740f4e6SLeilk Liu 	}
1246a568231fSLeilk Liu 
124737457607SLeilk Liu 	if (mdata->dev_comp->need_pad_sel) {
1248cae15788SYang Yingliang 		if (mdata->pad_num != host->num_chipselect)
124920cdbb80SAngeloGioacchino Del Regno 			return dev_err_probe(dev, -EINVAL,
125037457607SLeilk Liu 				"pad_num does not match num_chipselect(%d != %d)\n",
1251cae15788SYang Yingliang 				mdata->pad_num, host->num_chipselect);
125237457607SLeilk Liu 
1253cae15788SYang Yingliang 		if (!host->cs_gpiods && host->num_chipselect > 1)
125420cdbb80SAngeloGioacchino Del Regno 			return dev_err_probe(dev, -EINVAL,
125598c8dccfSNicolas Boichat 				"cs_gpios not specified and num_chipselect > 1\n");
125698c8dccfSNicolas Boichat 	}
125737457607SLeilk Liu 
1258fdeae8f5Sluhua.xu 	if (mdata->dev_comp->dma_ext)
1259fdeae8f5Sluhua.xu 		addr_bits = DMA_ADDR_EXT_BITS;
1260fdeae8f5Sluhua.xu 	else
1261fdeae8f5Sluhua.xu 		addr_bits = DMA_ADDR_DEF_BITS;
12626b444058SAngeloGioacchino Del Regno 	ret = dma_set_mask(dev, DMA_BIT_MASK(addr_bits));
1263fdeae8f5Sluhua.xu 	if (ret)
12646b444058SAngeloGioacchino Del Regno 		dev_notice(dev, "SPI dma_set_mask(%d) failed, ret:%d\n",
1265fdeae8f5Sluhua.xu 			   addr_bits, ret);
1266fdeae8f5Sluhua.xu 
1267*5972eb05SAngeloGioacchino Del Regno 	ret = devm_request_threaded_irq(dev, irq, mtk_spi_interrupt,
1268*5972eb05SAngeloGioacchino Del Regno 					mtk_spi_interrupt_thread,
1269cae15788SYang Yingliang 					IRQF_TRIGGER_NONE, dev_name(dev), host);
1270b24cded8SRicardo Ribalda 	if (ret)
1271b24cded8SRicardo Ribalda 		return dev_err_probe(dev, ret, "failed to register irq\n");
1272b24cded8SRicardo Ribalda 
12735088b313SAngeloGioacchino Del Regno 	pm_runtime_enable(dev);
12745088b313SAngeloGioacchino Del Regno 
1275cae15788SYang Yingliang 	ret = devm_spi_register_controller(dev, host);
1276c934fec1SMason Zhang 	if (ret) {
12775088b313SAngeloGioacchino Del Regno 		pm_runtime_disable(dev);
1278cae15788SYang Yingliang 		return dev_err_probe(dev, ret, "failed to register host\n");
1279c934fec1SMason Zhang 	}
1280c934fec1SMason Zhang 
1281a568231fSLeilk Liu 	return 0;
1282a568231fSLeilk Liu }
1283a568231fSLeilk Liu 
mtk_spi_remove(struct platform_device * pdev)1284df7e4719SUwe Kleine-König static void mtk_spi_remove(struct platform_device *pdev)
1285a568231fSLeilk Liu {
1286cae15788SYang Yingliang 	struct spi_controller *host = platform_get_drvdata(pdev);
1287cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
12880d10e90cSZhichao Liu 	int ret;
1289a568231fSLeilk Liu 
12904be47a5dSDaniel Golle 	if (mdata->use_spimem && !completion_done(&mdata->spimem_done))
12914be47a5dSDaniel Golle 		complete(&mdata->spimem_done);
12924be47a5dSDaniel Golle 
129322f40727SUwe Kleine-König 	ret = pm_runtime_get_sync(&pdev->dev);
129422f40727SUwe Kleine-König 	if (ret < 0) {
129522f40727SUwe Kleine-König 		dev_warn(&pdev->dev, "Failed to resume hardware (%pe)\n", ERR_PTR(ret));
129622f40727SUwe Kleine-König 	} else {
129722f40727SUwe Kleine-König 		/*
129822f40727SUwe Kleine-König 		 * If pm runtime resume failed, clks are disabled and
129922f40727SUwe Kleine-König 		 * unprepared. So don't access the hardware and skip clk
130022f40727SUwe Kleine-König 		 * unpreparing.
130122f40727SUwe Kleine-König 		 */
1302a568231fSLeilk Liu 		mtk_spi_reset(mdata);
1303a568231fSLeilk Liu 
1304a740f4e6SLeilk Liu 		if (mdata->dev_comp->no_need_unprepare) {
1305162a31efSMason Zhang 			clk_unprepare(mdata->spi_clk);
1306a740f4e6SLeilk Liu 			clk_unprepare(mdata->spi_hclk);
1307a740f4e6SLeilk Liu 		}
130822f40727SUwe Kleine-König 	}
1309162a31efSMason Zhang 
13100d10e90cSZhichao Liu 	pm_runtime_put_noidle(&pdev->dev);
13110d10e90cSZhichao Liu 	pm_runtime_disable(&pdev->dev);
1312a568231fSLeilk Liu }
1313a568231fSLeilk Liu 
1314a568231fSLeilk Liu #ifdef CONFIG_PM_SLEEP
mtk_spi_suspend(struct device * dev)1315a568231fSLeilk Liu static int mtk_spi_suspend(struct device *dev)
1316a568231fSLeilk Liu {
1317a568231fSLeilk Liu 	int ret;
1318cae15788SYang Yingliang 	struct spi_controller *host = dev_get_drvdata(dev);
1319cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
1320a568231fSLeilk Liu 
1321cae15788SYang Yingliang 	ret = spi_controller_suspend(host);
1322a568231fSLeilk Liu 	if (ret)
1323a568231fSLeilk Liu 		return ret;
1324a568231fSLeilk Liu 
1325a740f4e6SLeilk Liu 	if (!pm_runtime_suspended(dev)) {
1326a568231fSLeilk Liu 		clk_disable_unprepare(mdata->spi_clk);
1327a740f4e6SLeilk Liu 		clk_disable_unprepare(mdata->spi_hclk);
1328a740f4e6SLeilk Liu 	}
1329a568231fSLeilk Liu 
13304247d7f2SRuihai Zhou 	pinctrl_pm_select_sleep_state(dev);
13314247d7f2SRuihai Zhou 
13326f089e98SUwe Kleine-König 	return 0;
1333a568231fSLeilk Liu }
1334a568231fSLeilk Liu 
mtk_spi_resume(struct device * dev)1335a568231fSLeilk Liu static int mtk_spi_resume(struct device *dev)
1336a568231fSLeilk Liu {
1337a568231fSLeilk Liu 	int ret;
1338cae15788SYang Yingliang 	struct spi_controller *host = dev_get_drvdata(dev);
1339cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
1340a568231fSLeilk Liu 
13414247d7f2SRuihai Zhou 	pinctrl_pm_select_default_state(dev);
13424247d7f2SRuihai Zhou 
1343a568231fSLeilk Liu 	if (!pm_runtime_suspended(dev)) {
1344a568231fSLeilk Liu 		ret = clk_prepare_enable(mdata->spi_clk);
134513da5a0bSLeilk Liu 		if (ret < 0) {
134613da5a0bSLeilk Liu 			dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
1347a568231fSLeilk Liu 			return ret;
1348a568231fSLeilk Liu 		}
1349a740f4e6SLeilk Liu 
1350a740f4e6SLeilk Liu 		ret = clk_prepare_enable(mdata->spi_hclk);
1351a740f4e6SLeilk Liu 		if (ret < 0) {
1352a740f4e6SLeilk Liu 			dev_err(dev, "failed to enable spi_hclk (%d)\n", ret);
1353a740f4e6SLeilk Liu 			clk_disable_unprepare(mdata->spi_clk);
1354a740f4e6SLeilk Liu 			return ret;
1355a740f4e6SLeilk Liu 		}
135613da5a0bSLeilk Liu 	}
1357a568231fSLeilk Liu 
1358cae15788SYang Yingliang 	ret = spi_controller_resume(host);
1359a740f4e6SLeilk Liu 	if (ret < 0) {
1360a568231fSLeilk Liu 		clk_disable_unprepare(mdata->spi_clk);
1361a740f4e6SLeilk Liu 		clk_disable_unprepare(mdata->spi_hclk);
1362a740f4e6SLeilk Liu 	}
1363a568231fSLeilk Liu 
1364a568231fSLeilk Liu 	return ret;
1365a568231fSLeilk Liu }
1366a568231fSLeilk Liu #endif /* CONFIG_PM_SLEEP */
1367a568231fSLeilk Liu 
1368a568231fSLeilk Liu #ifdef CONFIG_PM
mtk_spi_runtime_suspend(struct device * dev)1369a568231fSLeilk Liu static int mtk_spi_runtime_suspend(struct device *dev)
1370a568231fSLeilk Liu {
1371cae15788SYang Yingliang 	struct spi_controller *host = dev_get_drvdata(dev);
1372cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
1373a568231fSLeilk Liu 
1374a740f4e6SLeilk Liu 	if (mdata->dev_comp->no_need_unprepare) {
1375162a31efSMason Zhang 		clk_disable(mdata->spi_clk);
1376a740f4e6SLeilk Liu 		clk_disable(mdata->spi_hclk);
1377a740f4e6SLeilk Liu 	} else {
1378a568231fSLeilk Liu 		clk_disable_unprepare(mdata->spi_clk);
1379a740f4e6SLeilk Liu 		clk_disable_unprepare(mdata->spi_hclk);
1380a740f4e6SLeilk Liu 	}
1381a568231fSLeilk Liu 
1382a568231fSLeilk Liu 	return 0;
1383a568231fSLeilk Liu }
1384a568231fSLeilk Liu 
mtk_spi_runtime_resume(struct device * dev)1385a568231fSLeilk Liu static int mtk_spi_runtime_resume(struct device *dev)
1386a568231fSLeilk Liu {
1387cae15788SYang Yingliang 	struct spi_controller *host = dev_get_drvdata(dev);
1388cae15788SYang Yingliang 	struct mtk_spi *mdata = spi_controller_get_devdata(host);
138913da5a0bSLeilk Liu 	int ret;
1390a568231fSLeilk Liu 
1391a740f4e6SLeilk Liu 	if (mdata->dev_comp->no_need_unprepare) {
1392162a31efSMason Zhang 		ret = clk_enable(mdata->spi_clk);
139313da5a0bSLeilk Liu 		if (ret < 0) {
139413da5a0bSLeilk Liu 			dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
139513da5a0bSLeilk Liu 			return ret;
139613da5a0bSLeilk Liu 		}
1397a740f4e6SLeilk Liu 		ret = clk_enable(mdata->spi_hclk);
1398a740f4e6SLeilk Liu 		if (ret < 0) {
1399a740f4e6SLeilk Liu 			dev_err(dev, "failed to enable spi_hclk (%d)\n", ret);
1400a740f4e6SLeilk Liu 			clk_disable(mdata->spi_clk);
1401a740f4e6SLeilk Liu 			return ret;
1402a740f4e6SLeilk Liu 		}
1403a740f4e6SLeilk Liu 	} else {
1404a740f4e6SLeilk Liu 		ret = clk_prepare_enable(mdata->spi_clk);
1405a740f4e6SLeilk Liu 		if (ret < 0) {
1406a740f4e6SLeilk Liu 			dev_err(dev, "failed to prepare_enable spi_clk (%d)\n", ret);
1407a740f4e6SLeilk Liu 			return ret;
1408a740f4e6SLeilk Liu 		}
1409a740f4e6SLeilk Liu 
1410a740f4e6SLeilk Liu 		ret = clk_prepare_enable(mdata->spi_hclk);
1411a740f4e6SLeilk Liu 		if (ret < 0) {
1412a740f4e6SLeilk Liu 			dev_err(dev, "failed to prepare_enable spi_hclk (%d)\n", ret);
1413a740f4e6SLeilk Liu 			clk_disable_unprepare(mdata->spi_clk);
1414a740f4e6SLeilk Liu 			return ret;
1415a740f4e6SLeilk Liu 		}
1416a740f4e6SLeilk Liu 	}
141713da5a0bSLeilk Liu 
141813da5a0bSLeilk Liu 	return 0;
1419a568231fSLeilk Liu }
1420a568231fSLeilk Liu #endif /* CONFIG_PM */
1421a568231fSLeilk Liu 
1422a568231fSLeilk Liu static const struct dev_pm_ops mtk_spi_pm = {
1423a568231fSLeilk Liu 	SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend, mtk_spi_resume)
1424a568231fSLeilk Liu 	SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend,
1425a568231fSLeilk Liu 			   mtk_spi_runtime_resume, NULL)
1426a568231fSLeilk Liu };
1427a568231fSLeilk Liu 
14284299aaaaSkbuild test robot static struct platform_driver mtk_spi_driver = {
1429a568231fSLeilk Liu 	.driver = {
1430a568231fSLeilk Liu 		.name = "mtk-spi",
1431a568231fSLeilk Liu 		.pm	= &mtk_spi_pm,
1432a568231fSLeilk Liu 		.of_match_table = mtk_spi_of_match,
1433a568231fSLeilk Liu 	},
1434a568231fSLeilk Liu 	.probe = mtk_spi_probe,
1435df7e4719SUwe Kleine-König 	.remove_new = mtk_spi_remove,
1436a568231fSLeilk Liu };
1437a568231fSLeilk Liu 
1438a568231fSLeilk Liu module_platform_driver(mtk_spi_driver);
1439a568231fSLeilk Liu 
1440a568231fSLeilk Liu MODULE_DESCRIPTION("MTK SPI Controller driver");
1441a568231fSLeilk Liu MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
1442a568231fSLeilk Liu MODULE_LICENSE("GPL v2");
1443e4001885SAxel Lin MODULE_ALIAS("platform:mtk-spi");
1444