xref: /linux/drivers/spi/spi-geni-qcom.c (revision e5f0dfa78ac77374a3f0817bf427a22f61494246)
1561de45fSGirish Mahadevan // SPDX-License-Identifier: GPL-2.0
2561de45fSGirish Mahadevan // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3561de45fSGirish Mahadevan 
4561de45fSGirish Mahadevan #include <linux/clk.h>
5b59c1224SVinod Koul #include <linux/dmaengine.h>
6b59c1224SVinod Koul #include <linux/dma-mapping.h>
7b59c1224SVinod Koul #include <linux/dma/qcom-gpi-dma.h>
8561de45fSGirish Mahadevan #include <linux/interrupt.h>
9561de45fSGirish Mahadevan #include <linux/io.h>
10561de45fSGirish Mahadevan #include <linux/log2.h>
11561de45fSGirish Mahadevan #include <linux/module.h>
12561de45fSGirish Mahadevan #include <linux/platform_device.h>
131a9e489eSRajendra Nayak #include <linux/pm_opp.h>
14561de45fSGirish Mahadevan #include <linux/pm_runtime.h>
15561de45fSGirish Mahadevan #include <linux/qcom-geni-se.h>
16561de45fSGirish Mahadevan #include <linux/spi/spi.h>
17561de45fSGirish Mahadevan #include <linux/spinlock.h>
18561de45fSGirish Mahadevan 
19561de45fSGirish Mahadevan /* SPI SE specific registers and respective register fields */
20561de45fSGirish Mahadevan #define SE_SPI_CPHA		0x224
21561de45fSGirish Mahadevan #define CPHA			BIT(0)
22561de45fSGirish Mahadevan 
23561de45fSGirish Mahadevan #define SE_SPI_LOOPBACK		0x22c
24561de45fSGirish Mahadevan #define LOOPBACK_ENABLE		0x1
25561de45fSGirish Mahadevan #define NORMAL_MODE		0x0
26561de45fSGirish Mahadevan #define LOOPBACK_MSK		GENMASK(1, 0)
27561de45fSGirish Mahadevan 
28561de45fSGirish Mahadevan #define SE_SPI_CPOL		0x230
29561de45fSGirish Mahadevan #define CPOL			BIT(2)
30561de45fSGirish Mahadevan 
31561de45fSGirish Mahadevan #define SE_SPI_DEMUX_OUTPUT_INV	0x24c
32561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_INV_MSK	GENMASK(3, 0)
33561de45fSGirish Mahadevan 
34561de45fSGirish Mahadevan #define SE_SPI_DEMUX_SEL	0x250
35561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_SEL	GENMASK(3, 0)
36561de45fSGirish Mahadevan 
37561de45fSGirish Mahadevan #define SE_SPI_TRANS_CFG	0x25c
38561de45fSGirish Mahadevan #define CS_TOGGLE		BIT(0)
39561de45fSGirish Mahadevan 
40561de45fSGirish Mahadevan #define SE_SPI_WORD_LEN		0x268
41561de45fSGirish Mahadevan #define WORD_LEN_MSK		GENMASK(9, 0)
42561de45fSGirish Mahadevan #define MIN_WORD_LEN		4
43561de45fSGirish Mahadevan 
44561de45fSGirish Mahadevan #define SE_SPI_TX_TRANS_LEN	0x26c
45561de45fSGirish Mahadevan #define SE_SPI_RX_TRANS_LEN	0x270
46561de45fSGirish Mahadevan #define TRANS_LEN_MSK		GENMASK(23, 0)
47561de45fSGirish Mahadevan 
48561de45fSGirish Mahadevan #define SE_SPI_PRE_POST_CMD_DLY	0x274
49561de45fSGirish Mahadevan 
50561de45fSGirish Mahadevan #define SE_SPI_DELAY_COUNTERS	0x278
51561de45fSGirish Mahadevan #define SPI_INTER_WORDS_DELAY_MSK	GENMASK(9, 0)
52561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_MSK		GENMASK(19, 10)
53561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_SHFT		10
54561de45fSGirish Mahadevan 
55561de45fSGirish Mahadevan /* M_CMD OP codes for SPI */
56561de45fSGirish Mahadevan #define SPI_TX_ONLY		1
57561de45fSGirish Mahadevan #define SPI_RX_ONLY		2
58561de45fSGirish Mahadevan #define SPI_TX_RX		7
59561de45fSGirish Mahadevan #define SPI_CS_ASSERT		8
60561de45fSGirish Mahadevan #define SPI_CS_DEASSERT		9
61561de45fSGirish Mahadevan #define SPI_SCK_ONLY		10
62561de45fSGirish Mahadevan /* M_CMD params for SPI */
63561de45fSGirish Mahadevan #define SPI_PRE_CMD_DELAY	BIT(0)
64561de45fSGirish Mahadevan #define TIMESTAMP_BEFORE	BIT(1)
65561de45fSGirish Mahadevan #define FRAGMENTATION		BIT(2)
66561de45fSGirish Mahadevan #define TIMESTAMP_AFTER		BIT(3)
67561de45fSGirish Mahadevan #define POST_CMD_DELAY		BIT(4)
68561de45fSGirish Mahadevan 
69b59c1224SVinod Koul #define GSI_LOOPBACK_EN		BIT(0)
70b59c1224SVinod Koul #define GSI_CS_TOGGLE		BIT(3)
71b59c1224SVinod Koul #define GSI_CPHA		BIT(4)
72b59c1224SVinod Koul #define GSI_CPOL		BIT(5)
73b59c1224SVinod Koul 
74561de45fSGirish Mahadevan struct spi_geni_master {
75561de45fSGirish Mahadevan 	struct geni_se se;
76561de45fSGirish Mahadevan 	struct device *dev;
77561de45fSGirish Mahadevan 	u32 tx_fifo_depth;
78561de45fSGirish Mahadevan 	u32 fifo_width_bits;
79561de45fSGirish Mahadevan 	u32 tx_wm;
80da48dc8cSDouglas Anderson 	u32 last_mode;
81561de45fSGirish Mahadevan 	unsigned long cur_speed_hz;
825f219524SDouglas Anderson 	unsigned long cur_sclk_hz;
83561de45fSGirish Mahadevan 	unsigned int cur_bits_per_word;
84561de45fSGirish Mahadevan 	unsigned int tx_rem_bytes;
85561de45fSGirish Mahadevan 	unsigned int rx_rem_bytes;
86561de45fSGirish Mahadevan 	const struct spi_transfer *cur_xfer;
877ba9bdcbSDouglas Anderson 	struct completion cs_done;
887ba9bdcbSDouglas Anderson 	struct completion cancel_done;
897ba9bdcbSDouglas Anderson 	struct completion abort_done;
90*e5f0dfa7SVijaya Krishna Nivarthi 	struct completion tx_reset_done;
91*e5f0dfa7SVijaya Krishna Nivarthi 	struct completion rx_reset_done;
92561de45fSGirish Mahadevan 	unsigned int oversampling;
93561de45fSGirish Mahadevan 	spinlock_t lock;
94561de45fSGirish Mahadevan 	int irq;
95638d8488SDouglas Anderson 	bool cs_flag;
96690d8b91SDouglas Anderson 	bool abort_failed;
97b59c1224SVinod Koul 	struct dma_chan *tx;
98b59c1224SVinod Koul 	struct dma_chan *rx;
99b59c1224SVinod Koul 	int cur_xfer_mode;
100*e5f0dfa7SVijaya Krishna Nivarthi 	dma_addr_t tx_se_dma;
101*e5f0dfa7SVijaya Krishna Nivarthi 	dma_addr_t rx_se_dma;
102561de45fSGirish Mahadevan };
103561de45fSGirish Mahadevan 
104561de45fSGirish Mahadevan static int get_spi_clk_cfg(unsigned int speed_hz,
105561de45fSGirish Mahadevan 			struct spi_geni_master *mas,
106561de45fSGirish Mahadevan 			unsigned int *clk_idx,
107561de45fSGirish Mahadevan 			unsigned int *clk_div)
108561de45fSGirish Mahadevan {
109561de45fSGirish Mahadevan 	unsigned long sclk_freq;
110561de45fSGirish Mahadevan 	unsigned int actual_hz;
111561de45fSGirish Mahadevan 	int ret;
112561de45fSGirish Mahadevan 
113561de45fSGirish Mahadevan 	ret = geni_se_clk_freq_match(&mas->se,
114561de45fSGirish Mahadevan 				speed_hz * mas->oversampling,
115561de45fSGirish Mahadevan 				clk_idx, &sclk_freq, false);
116561de45fSGirish Mahadevan 	if (ret) {
117561de45fSGirish Mahadevan 		dev_err(mas->dev, "Failed(%d) to find src clk for %dHz\n",
118561de45fSGirish Mahadevan 							ret, speed_hz);
119561de45fSGirish Mahadevan 		return ret;
120561de45fSGirish Mahadevan 	}
121561de45fSGirish Mahadevan 
122561de45fSGirish Mahadevan 	*clk_div = DIV_ROUND_UP(sclk_freq, mas->oversampling * speed_hz);
123561de45fSGirish Mahadevan 	actual_hz = sclk_freq / (mas->oversampling * *clk_div);
124561de45fSGirish Mahadevan 
125561de45fSGirish Mahadevan 	dev_dbg(mas->dev, "req %u=>%u sclk %lu, idx %d, div %d\n", speed_hz,
126561de45fSGirish Mahadevan 				actual_hz, sclk_freq, *clk_idx, *clk_div);
1271a9e489eSRajendra Nayak 	ret = dev_pm_opp_set_rate(mas->dev, sclk_freq);
128561de45fSGirish Mahadevan 	if (ret)
1291a9e489eSRajendra Nayak 		dev_err(mas->dev, "dev_pm_opp_set_rate failed %d\n", ret);
1305f219524SDouglas Anderson 	else
1315f219524SDouglas Anderson 		mas->cur_sclk_hz = sclk_freq;
1325f219524SDouglas Anderson 
133561de45fSGirish Mahadevan 	return ret;
134561de45fSGirish Mahadevan }
135561de45fSGirish Mahadevan 
136*e5f0dfa7SVijaya Krishna Nivarthi static void handle_se_timeout(struct spi_master *spi,
137de43affeSStephen Boyd 				struct spi_message *msg)
138de43affeSStephen Boyd {
139de43affeSStephen Boyd 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
140539afdf9SDouglas Anderson 	unsigned long time_left;
141de43affeSStephen Boyd 	struct geni_se *se = &mas->se;
142*e5f0dfa7SVijaya Krishna Nivarthi 	const struct spi_transfer *xfer;
143de43affeSStephen Boyd 
144539afdf9SDouglas Anderson 	spin_lock_irq(&mas->lock);
1457ba9bdcbSDouglas Anderson 	reinit_completion(&mas->cancel_done);
146*e5f0dfa7SVijaya Krishna Nivarthi 	if (mas->cur_xfer_mode == GENI_SE_FIFO)
147de43affeSStephen Boyd 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
148*e5f0dfa7SVijaya Krishna Nivarthi 
149*e5f0dfa7SVijaya Krishna Nivarthi 	xfer = mas->cur_xfer;
1507ba9bdcbSDouglas Anderson 	mas->cur_xfer = NULL;
1517ba9bdcbSDouglas Anderson 	geni_se_cancel_m_cmd(se);
152539afdf9SDouglas Anderson 	spin_unlock_irq(&mas->lock);
1537ba9bdcbSDouglas Anderson 
1547ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->cancel_done, HZ);
155de43affeSStephen Boyd 	if (time_left)
156*e5f0dfa7SVijaya Krishna Nivarthi 		goto unmap_if_dma;
157de43affeSStephen Boyd 
158539afdf9SDouglas Anderson 	spin_lock_irq(&mas->lock);
1597ba9bdcbSDouglas Anderson 	reinit_completion(&mas->abort_done);
160de43affeSStephen Boyd 	geni_se_abort_m_cmd(se);
161539afdf9SDouglas Anderson 	spin_unlock_irq(&mas->lock);
1627ba9bdcbSDouglas Anderson 
1637ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->abort_done, HZ);
164690d8b91SDouglas Anderson 	if (!time_left) {
165de43affeSStephen Boyd 		dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
166690d8b91SDouglas Anderson 
167690d8b91SDouglas Anderson 		/*
168690d8b91SDouglas Anderson 		 * No need for a lock since SPI core has a lock and we never
169690d8b91SDouglas Anderson 		 * access this from an interrupt.
170690d8b91SDouglas Anderson 		 */
171690d8b91SDouglas Anderson 		mas->abort_failed = true;
172690d8b91SDouglas Anderson 	}
173*e5f0dfa7SVijaya Krishna Nivarthi 
174*e5f0dfa7SVijaya Krishna Nivarthi unmap_if_dma:
175*e5f0dfa7SVijaya Krishna Nivarthi 	if (mas->cur_xfer_mode == GENI_SE_DMA) {
176*e5f0dfa7SVijaya Krishna Nivarthi 		if (xfer) {
177*e5f0dfa7SVijaya Krishna Nivarthi 			if (xfer->tx_buf && mas->tx_se_dma) {
178*e5f0dfa7SVijaya Krishna Nivarthi 				spin_lock_irq(&mas->lock);
179*e5f0dfa7SVijaya Krishna Nivarthi 				reinit_completion(&mas->tx_reset_done);
180*e5f0dfa7SVijaya Krishna Nivarthi 				writel(1, se->base + SE_DMA_TX_FSM_RST);
181*e5f0dfa7SVijaya Krishna Nivarthi 				spin_unlock_irq(&mas->lock);
182*e5f0dfa7SVijaya Krishna Nivarthi 				time_left = wait_for_completion_timeout(&mas->tx_reset_done, HZ);
183*e5f0dfa7SVijaya Krishna Nivarthi 				if (!time_left)
184*e5f0dfa7SVijaya Krishna Nivarthi 					dev_err(mas->dev, "DMA TX RESET failed\n");
185*e5f0dfa7SVijaya Krishna Nivarthi 				geni_se_tx_dma_unprep(se, mas->tx_se_dma, xfer->len);
186*e5f0dfa7SVijaya Krishna Nivarthi 			}
187*e5f0dfa7SVijaya Krishna Nivarthi 			if (xfer->rx_buf && mas->rx_se_dma) {
188*e5f0dfa7SVijaya Krishna Nivarthi 				spin_lock_irq(&mas->lock);
189*e5f0dfa7SVijaya Krishna Nivarthi 				reinit_completion(&mas->rx_reset_done);
190*e5f0dfa7SVijaya Krishna Nivarthi 				writel(1, se->base + SE_DMA_RX_FSM_RST);
191*e5f0dfa7SVijaya Krishna Nivarthi 				spin_unlock_irq(&mas->lock);
192*e5f0dfa7SVijaya Krishna Nivarthi 				time_left = wait_for_completion_timeout(&mas->rx_reset_done, HZ);
193*e5f0dfa7SVijaya Krishna Nivarthi 				if (!time_left)
194*e5f0dfa7SVijaya Krishna Nivarthi 					dev_err(mas->dev, "DMA RX RESET failed\n");
195*e5f0dfa7SVijaya Krishna Nivarthi 				geni_se_rx_dma_unprep(se, mas->rx_se_dma, xfer->len);
196*e5f0dfa7SVijaya Krishna Nivarthi 			}
197*e5f0dfa7SVijaya Krishna Nivarthi 		} else {
198*e5f0dfa7SVijaya Krishna Nivarthi 			/*
199*e5f0dfa7SVijaya Krishna Nivarthi 			 * This can happen if a timeout happened and we had to wait
200*e5f0dfa7SVijaya Krishna Nivarthi 			 * for lock in this function because isr was holding the lock
201*e5f0dfa7SVijaya Krishna Nivarthi 			 * and handling transfer completion at that time.
202*e5f0dfa7SVijaya Krishna Nivarthi 			 */
203*e5f0dfa7SVijaya Krishna Nivarthi 			dev_warn(mas->dev, "Cancel/Abort on completed SPI transfer\n");
204*e5f0dfa7SVijaya Krishna Nivarthi 		}
205*e5f0dfa7SVijaya Krishna Nivarthi 	}
206690d8b91SDouglas Anderson }
207690d8b91SDouglas Anderson 
208f8039ea5SVinod Koul static void handle_gpi_timeout(struct spi_master *spi, struct spi_message *msg)
209f8039ea5SVinod Koul {
210f8039ea5SVinod Koul 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
211f8039ea5SVinod Koul 
212f8039ea5SVinod Koul 	dmaengine_terminate_sync(mas->tx);
213f8039ea5SVinod Koul 	dmaengine_terminate_sync(mas->rx);
214f8039ea5SVinod Koul }
215f8039ea5SVinod Koul 
216f8039ea5SVinod Koul static void spi_geni_handle_err(struct spi_master *spi, struct spi_message *msg)
217f8039ea5SVinod Koul {
218f8039ea5SVinod Koul 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
219f8039ea5SVinod Koul 
220f8039ea5SVinod Koul 	switch (mas->cur_xfer_mode) {
221f8039ea5SVinod Koul 	case GENI_SE_FIFO:
222*e5f0dfa7SVijaya Krishna Nivarthi 	case GENI_SE_DMA:
223*e5f0dfa7SVijaya Krishna Nivarthi 		handle_se_timeout(spi, msg);
224f8039ea5SVinod Koul 		break;
225f8039ea5SVinod Koul 	case GENI_GPI_DMA:
226f8039ea5SVinod Koul 		handle_gpi_timeout(spi, msg);
227f8039ea5SVinod Koul 		break;
228f8039ea5SVinod Koul 	default:
229f8039ea5SVinod Koul 		dev_err(mas->dev, "Abort on Mode:%d not supported", mas->cur_xfer_mode);
230f8039ea5SVinod Koul 	}
231f8039ea5SVinod Koul }
232f8039ea5SVinod Koul 
233690d8b91SDouglas Anderson static bool spi_geni_is_abort_still_pending(struct spi_geni_master *mas)
234690d8b91SDouglas Anderson {
235690d8b91SDouglas Anderson 	struct geni_se *se = &mas->se;
236690d8b91SDouglas Anderson 	u32 m_irq, m_irq_en;
237690d8b91SDouglas Anderson 
238690d8b91SDouglas Anderson 	if (!mas->abort_failed)
239690d8b91SDouglas Anderson 		return false;
240690d8b91SDouglas Anderson 
241690d8b91SDouglas Anderson 	/*
242690d8b91SDouglas Anderson 	 * The only known case where a transfer times out and then a cancel
243690d8b91SDouglas Anderson 	 * times out then an abort times out is if something is blocking our
244690d8b91SDouglas Anderson 	 * interrupt handler from running.  Avoid starting any new transfers
245690d8b91SDouglas Anderson 	 * until that sorts itself out.
246690d8b91SDouglas Anderson 	 */
247690d8b91SDouglas Anderson 	spin_lock_irq(&mas->lock);
248690d8b91SDouglas Anderson 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
249690d8b91SDouglas Anderson 	m_irq_en = readl(se->base + SE_GENI_M_IRQ_EN);
250690d8b91SDouglas Anderson 	spin_unlock_irq(&mas->lock);
251690d8b91SDouglas Anderson 
252690d8b91SDouglas Anderson 	if (m_irq & m_irq_en) {
253690d8b91SDouglas Anderson 		dev_err(mas->dev, "Interrupts pending after abort: %#010x\n",
254690d8b91SDouglas Anderson 			m_irq & m_irq_en);
255690d8b91SDouglas Anderson 		return true;
256690d8b91SDouglas Anderson 	}
257690d8b91SDouglas Anderson 
258690d8b91SDouglas Anderson 	/*
259690d8b91SDouglas Anderson 	 * If we're here the problem resolved itself so no need to check more
260690d8b91SDouglas Anderson 	 * on future transfers.
261690d8b91SDouglas Anderson 	 */
262690d8b91SDouglas Anderson 	mas->abort_failed = false;
263690d8b91SDouglas Anderson 
264690d8b91SDouglas Anderson 	return false;
265de43affeSStephen Boyd }
266de43affeSStephen Boyd 
267561de45fSGirish Mahadevan static void spi_geni_set_cs(struct spi_device *slv, bool set_flag)
268561de45fSGirish Mahadevan {
269561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(slv->master);
270561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(mas->dev);
271561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
2720dccff3cSAlok Chauhan 	unsigned long time_left;
273561de45fSGirish Mahadevan 
274561de45fSGirish Mahadevan 	if (!(slv->mode & SPI_CS_HIGH))
275561de45fSGirish Mahadevan 		set_flag = !set_flag;
276561de45fSGirish Mahadevan 
277638d8488SDouglas Anderson 	if (set_flag == mas->cs_flag)
278638d8488SDouglas Anderson 		return;
279638d8488SDouglas Anderson 
280690d8b91SDouglas Anderson 	pm_runtime_get_sync(mas->dev);
281690d8b91SDouglas Anderson 
282690d8b91SDouglas Anderson 	if (spi_geni_is_abort_still_pending(mas)) {
283690d8b91SDouglas Anderson 		dev_err(mas->dev, "Can't set chip select\n");
284690d8b91SDouglas Anderson 		goto exit;
285690d8b91SDouglas Anderson 	}
286690d8b91SDouglas Anderson 
2872ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
2883d7d916fSDouglas Anderson 	if (mas->cur_xfer) {
2893d7d916fSDouglas Anderson 		dev_err(mas->dev, "Can't set CS when prev xfer running\n");
2903d7d916fSDouglas Anderson 		spin_unlock_irq(&mas->lock);
2913d7d916fSDouglas Anderson 		goto exit;
2923d7d916fSDouglas Anderson 	}
2933d7d916fSDouglas Anderson 
2943d7d916fSDouglas Anderson 	mas->cs_flag = set_flag;
295*e5f0dfa7SVijaya Krishna Nivarthi 	/* set xfer_mode to FIFO to complete cs_done in isr */
296*e5f0dfa7SVijaya Krishna Nivarthi 	mas->cur_xfer_mode = GENI_SE_FIFO;
2977ba9bdcbSDouglas Anderson 	reinit_completion(&mas->cs_done);
298561de45fSGirish Mahadevan 	if (set_flag)
299561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
300561de45fSGirish Mahadevan 	else
301561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
3022ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
303561de45fSGirish Mahadevan 
3047ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->cs_done, HZ);
30517fa81aaSDouglas Anderson 	if (!time_left) {
30617fa81aaSDouglas Anderson 		dev_warn(mas->dev, "Timeout setting chip select\n");
307*e5f0dfa7SVijaya Krishna Nivarthi 		handle_se_timeout(spi, NULL);
30817fa81aaSDouglas Anderson 	}
309561de45fSGirish Mahadevan 
310690d8b91SDouglas Anderson exit:
311561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
312561de45fSGirish Mahadevan }
313561de45fSGirish Mahadevan 
314561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
315561de45fSGirish Mahadevan 					unsigned int bits_per_word)
316561de45fSGirish Mahadevan {
317561de45fSGirish Mahadevan 	unsigned int pack_words;
318561de45fSGirish Mahadevan 	bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
319561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
320561de45fSGirish Mahadevan 	u32 word_len;
321561de45fSGirish Mahadevan 
322561de45fSGirish Mahadevan 	/*
323561de45fSGirish Mahadevan 	 * If bits_per_word isn't a byte aligned value, set the packing to be
324561de45fSGirish Mahadevan 	 * 1 SPI word per FIFO word.
325561de45fSGirish Mahadevan 	 */
326561de45fSGirish Mahadevan 	if (!(mas->fifo_width_bits % bits_per_word))
327561de45fSGirish Mahadevan 		pack_words = mas->fifo_width_bits / bits_per_word;
328561de45fSGirish Mahadevan 	else
329561de45fSGirish Mahadevan 		pack_words = 1;
330561de45fSGirish Mahadevan 	geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
331561de45fSGirish Mahadevan 								true, true);
332da48dc8cSDouglas Anderson 	word_len = (bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK;
333561de45fSGirish Mahadevan 	writel(word_len, se->base + SE_SPI_WORD_LEN);
334561de45fSGirish Mahadevan }
335561de45fSGirish Mahadevan 
3360e3b8a81SAkash Asthana static int geni_spi_set_clock_and_bw(struct spi_geni_master *mas,
3370e3b8a81SAkash Asthana 					unsigned long clk_hz)
338e68b6624SDouglas Anderson {
339e68b6624SDouglas Anderson 	u32 clk_sel, m_clk_cfg, idx, div;
340e68b6624SDouglas Anderson 	struct geni_se *se = &mas->se;
341e68b6624SDouglas Anderson 	int ret;
342e68b6624SDouglas Anderson 
34368890e20SDouglas Anderson 	if (clk_hz == mas->cur_speed_hz)
34468890e20SDouglas Anderson 		return 0;
34568890e20SDouglas Anderson 
346e68b6624SDouglas Anderson 	ret = get_spi_clk_cfg(clk_hz, mas, &idx, &div);
347e68b6624SDouglas Anderson 	if (ret) {
348e68b6624SDouglas Anderson 		dev_err(mas->dev, "Err setting clk to %lu: %d\n", clk_hz, ret);
349e68b6624SDouglas Anderson 		return ret;
350e68b6624SDouglas Anderson 	}
351e68b6624SDouglas Anderson 
352e68b6624SDouglas Anderson 	/*
353e68b6624SDouglas Anderson 	 * SPI core clock gets configured with the requested frequency
354e68b6624SDouglas Anderson 	 * or the frequency closer to the requested frequency.
355e68b6624SDouglas Anderson 	 * For that reason requested frequency is stored in the
356e68b6624SDouglas Anderson 	 * cur_speed_hz and referred in the consecutive transfer instead
357e68b6624SDouglas Anderson 	 * of calling clk_get_rate() API.
358e68b6624SDouglas Anderson 	 */
359e68b6624SDouglas Anderson 	mas->cur_speed_hz = clk_hz;
360e68b6624SDouglas Anderson 
361e68b6624SDouglas Anderson 	clk_sel = idx & CLK_SEL_MSK;
362e68b6624SDouglas Anderson 	m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
363e68b6624SDouglas Anderson 	writel(clk_sel, se->base + SE_GENI_CLK_SEL);
364e68b6624SDouglas Anderson 	writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
365e68b6624SDouglas Anderson 
3660e3b8a81SAkash Asthana 	/* Set BW quota for CPU as driver supports FIFO mode only. */
3670e3b8a81SAkash Asthana 	se->icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(mas->cur_speed_hz);
3680e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(se);
3690e3b8a81SAkash Asthana 	if (ret)
3700e3b8a81SAkash Asthana 		return ret;
3710e3b8a81SAkash Asthana 
372e68b6624SDouglas Anderson 	return 0;
373e68b6624SDouglas Anderson }
374e68b6624SDouglas Anderson 
375561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
376561de45fSGirish Mahadevan 					struct spi_master *spi)
377561de45fSGirish Mahadevan {
378561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
379561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
380da48dc8cSDouglas Anderson 	u32 loopback_cfg = 0, cpol = 0, cpha = 0, demux_output_inv = 0;
381e68b6624SDouglas Anderson 	u32 demux_sel;
382561de45fSGirish Mahadevan 
383da48dc8cSDouglas Anderson 	if (mas->last_mode != spi_slv->mode) {
384561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_LOOP)
385da48dc8cSDouglas Anderson 			loopback_cfg = LOOPBACK_ENABLE;
386561de45fSGirish Mahadevan 
387561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CPOL)
388da48dc8cSDouglas Anderson 			cpol = CPOL;
389561de45fSGirish Mahadevan 
390561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CPHA)
391da48dc8cSDouglas Anderson 			cpha = CPHA;
392561de45fSGirish Mahadevan 
393561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CS_HIGH)
394561de45fSGirish Mahadevan 			demux_output_inv = BIT(spi_slv->chip_select);
395561de45fSGirish Mahadevan 
396561de45fSGirish Mahadevan 		demux_sel = spi_slv->chip_select;
397561de45fSGirish Mahadevan 		mas->cur_bits_per_word = spi_slv->bits_per_word;
398561de45fSGirish Mahadevan 
399561de45fSGirish Mahadevan 		spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
400561de45fSGirish Mahadevan 		writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
401561de45fSGirish Mahadevan 		writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
402561de45fSGirish Mahadevan 		writel(cpha, se->base + SE_SPI_CPHA);
403561de45fSGirish Mahadevan 		writel(cpol, se->base + SE_SPI_CPOL);
404561de45fSGirish Mahadevan 		writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
405e68b6624SDouglas Anderson 
406da48dc8cSDouglas Anderson 		mas->last_mode = spi_slv->mode;
407da48dc8cSDouglas Anderson 	}
408da48dc8cSDouglas Anderson 
4090e3b8a81SAkash Asthana 	return geni_spi_set_clock_and_bw(mas, spi_slv->max_speed_hz);
410561de45fSGirish Mahadevan }
411561de45fSGirish Mahadevan 
412b59c1224SVinod Koul static void
413b59c1224SVinod Koul spi_gsi_callback_result(void *cb, const struct dmaengine_result *result)
414b59c1224SVinod Koul {
415b59c1224SVinod Koul 	struct spi_master *spi = cb;
416b59c1224SVinod Koul 
41774b86d6aSVinod Koul 	spi->cur_msg->status = -EIO;
418b59c1224SVinod Koul 	if (result->result != DMA_TRANS_NOERROR) {
419b59c1224SVinod Koul 		dev_err(&spi->dev, "DMA txn failed: %d\n", result->result);
42074b86d6aSVinod Koul 		spi_finalize_current_transfer(spi);
421b59c1224SVinod Koul 		return;
422b59c1224SVinod Koul 	}
423b59c1224SVinod Koul 
424b59c1224SVinod Koul 	if (!result->residue) {
42574b86d6aSVinod Koul 		spi->cur_msg->status = 0;
426b59c1224SVinod Koul 		dev_dbg(&spi->dev, "DMA txn completed\n");
427b59c1224SVinod Koul 	} else {
428b59c1224SVinod Koul 		dev_err(&spi->dev, "DMA xfer has pending: %d\n", result->residue);
429b59c1224SVinod Koul 	}
43074b86d6aSVinod Koul 
43174b86d6aSVinod Koul 	spi_finalize_current_transfer(spi);
432b59c1224SVinod Koul }
433b59c1224SVinod Koul 
434b59c1224SVinod Koul static int setup_gsi_xfer(struct spi_transfer *xfer, struct spi_geni_master *mas,
435b59c1224SVinod Koul 			  struct spi_device *spi_slv, struct spi_master *spi)
436b59c1224SVinod Koul {
437b59c1224SVinod Koul 	unsigned long flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
438b59c1224SVinod Koul 	struct dma_slave_config config = {};
439b59c1224SVinod Koul 	struct gpi_spi_config peripheral = {};
440b59c1224SVinod Koul 	struct dma_async_tx_descriptor *tx_desc, *rx_desc;
441b59c1224SVinod Koul 	int ret;
442b59c1224SVinod Koul 
443b59c1224SVinod Koul 	config.peripheral_config = &peripheral;
444b59c1224SVinod Koul 	config.peripheral_size = sizeof(peripheral);
445b59c1224SVinod Koul 	peripheral.set_config = true;
446b59c1224SVinod Koul 
447b59c1224SVinod Koul 	if (xfer->bits_per_word != mas->cur_bits_per_word ||
448b59c1224SVinod Koul 	    xfer->speed_hz != mas->cur_speed_hz) {
449b59c1224SVinod Koul 		mas->cur_bits_per_word = xfer->bits_per_word;
450b59c1224SVinod Koul 		mas->cur_speed_hz = xfer->speed_hz;
451b59c1224SVinod Koul 	}
452b59c1224SVinod Koul 
453b59c1224SVinod Koul 	if (xfer->tx_buf && xfer->rx_buf) {
454b59c1224SVinod Koul 		peripheral.cmd = SPI_DUPLEX;
455b59c1224SVinod Koul 	} else if (xfer->tx_buf) {
456b59c1224SVinod Koul 		peripheral.cmd = SPI_TX;
457b59c1224SVinod Koul 		peripheral.rx_len = 0;
458b59c1224SVinod Koul 	} else if (xfer->rx_buf) {
459b59c1224SVinod Koul 		peripheral.cmd = SPI_RX;
460b59c1224SVinod Koul 		if (!(mas->cur_bits_per_word % MIN_WORD_LEN)) {
461b59c1224SVinod Koul 			peripheral.rx_len = ((xfer->len << 3) / mas->cur_bits_per_word);
462b59c1224SVinod Koul 		} else {
463b59c1224SVinod Koul 			int bytes_per_word = (mas->cur_bits_per_word / BITS_PER_BYTE) + 1;
464b59c1224SVinod Koul 
465b59c1224SVinod Koul 			peripheral.rx_len = (xfer->len / bytes_per_word);
466b59c1224SVinod Koul 		}
467b59c1224SVinod Koul 	}
468b59c1224SVinod Koul 
469b59c1224SVinod Koul 	peripheral.loopback_en = !!(spi_slv->mode & SPI_LOOP);
470b59c1224SVinod Koul 	peripheral.clock_pol_high = !!(spi_slv->mode & SPI_CPOL);
471b59c1224SVinod Koul 	peripheral.data_pol_high = !!(spi_slv->mode & SPI_CPHA);
472b59c1224SVinod Koul 	peripheral.cs = spi_slv->chip_select;
473b59c1224SVinod Koul 	peripheral.pack_en = true;
474b59c1224SVinod Koul 	peripheral.word_len = xfer->bits_per_word - MIN_WORD_LEN;
475b59c1224SVinod Koul 
476b59c1224SVinod Koul 	ret = get_spi_clk_cfg(mas->cur_speed_hz, mas,
477b59c1224SVinod Koul 			      &peripheral.clk_src, &peripheral.clk_div);
478b59c1224SVinod Koul 	if (ret) {
479b59c1224SVinod Koul 		dev_err(mas->dev, "Err in get_spi_clk_cfg() :%d\n", ret);
480b59c1224SVinod Koul 		return ret;
481b59c1224SVinod Koul 	}
482b59c1224SVinod Koul 
483b59c1224SVinod Koul 	if (!xfer->cs_change) {
484b59c1224SVinod Koul 		if (!list_is_last(&xfer->transfer_list, &spi->cur_msg->transfers))
485b59c1224SVinod Koul 			peripheral.fragmentation = FRAGMENTATION;
486b59c1224SVinod Koul 	}
487b59c1224SVinod Koul 
488b59c1224SVinod Koul 	if (peripheral.cmd & SPI_RX) {
489b59c1224SVinod Koul 		dmaengine_slave_config(mas->rx, &config);
490b59c1224SVinod Koul 		rx_desc = dmaengine_prep_slave_sg(mas->rx, xfer->rx_sg.sgl, xfer->rx_sg.nents,
491b59c1224SVinod Koul 						  DMA_DEV_TO_MEM, flags);
492b59c1224SVinod Koul 		if (!rx_desc) {
493b59c1224SVinod Koul 			dev_err(mas->dev, "Err setting up rx desc\n");
494b59c1224SVinod Koul 			return -EIO;
495b59c1224SVinod Koul 		}
496b59c1224SVinod Koul 	}
497b59c1224SVinod Koul 
498b59c1224SVinod Koul 	/*
499b59c1224SVinod Koul 	 * Prepare the TX always, even for RX or tx_buf being null, we would
500b59c1224SVinod Koul 	 * need TX to be prepared per GSI spec
501b59c1224SVinod Koul 	 */
502b59c1224SVinod Koul 	dmaengine_slave_config(mas->tx, &config);
503b59c1224SVinod Koul 	tx_desc = dmaengine_prep_slave_sg(mas->tx, xfer->tx_sg.sgl, xfer->tx_sg.nents,
504b59c1224SVinod Koul 					  DMA_MEM_TO_DEV, flags);
505b59c1224SVinod Koul 	if (!tx_desc) {
506b59c1224SVinod Koul 		dev_err(mas->dev, "Err setting up tx desc\n");
507b59c1224SVinod Koul 		return -EIO;
508b59c1224SVinod Koul 	}
509b59c1224SVinod Koul 
510b59c1224SVinod Koul 	tx_desc->callback_result = spi_gsi_callback_result;
511b59c1224SVinod Koul 	tx_desc->callback_param = spi;
512b59c1224SVinod Koul 
513b59c1224SVinod Koul 	if (peripheral.cmd & SPI_RX)
514b59c1224SVinod Koul 		dmaengine_submit(rx_desc);
515b59c1224SVinod Koul 	dmaengine_submit(tx_desc);
516b59c1224SVinod Koul 
517b59c1224SVinod Koul 	if (peripheral.cmd & SPI_RX)
518b59c1224SVinod Koul 		dma_async_issue_pending(mas->rx);
519b59c1224SVinod Koul 
520b59c1224SVinod Koul 	dma_async_issue_pending(mas->tx);
521b59c1224SVinod Koul 	return 1;
522b59c1224SVinod Koul }
523b59c1224SVinod Koul 
524b59c1224SVinod Koul static bool geni_can_dma(struct spi_controller *ctlr,
525b59c1224SVinod Koul 			 struct spi_device *slv, struct spi_transfer *xfer)
526b59c1224SVinod Koul {
527b59c1224SVinod Koul 	struct spi_geni_master *mas = spi_master_get_devdata(slv->master);
528b59c1224SVinod Koul 
529*e5f0dfa7SVijaya Krishna Nivarthi 	/*
530*e5f0dfa7SVijaya Krishna Nivarthi 	 * Return true if transfer needs to be mapped prior to
531*e5f0dfa7SVijaya Krishna Nivarthi 	 * calling transfer_one which is the case only for GPI_DMA.
532*e5f0dfa7SVijaya Krishna Nivarthi 	 * For SE_DMA mode, map/unmap is done in geni_se_*x_dma_prep.
533*e5f0dfa7SVijaya Krishna Nivarthi 	 */
534*e5f0dfa7SVijaya Krishna Nivarthi 	return mas->cur_xfer_mode == GENI_GPI_DMA;
535b59c1224SVinod Koul }
536b59c1224SVinod Koul 
537561de45fSGirish Mahadevan static int spi_geni_prepare_message(struct spi_master *spi,
538561de45fSGirish Mahadevan 					struct spi_message *spi_msg)
539561de45fSGirish Mahadevan {
540561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
541b59c1224SVinod Koul 	int ret;
542561de45fSGirish Mahadevan 
543b59c1224SVinod Koul 	switch (mas->cur_xfer_mode) {
544b59c1224SVinod Koul 	case GENI_SE_FIFO:
545*e5f0dfa7SVijaya Krishna Nivarthi 	case GENI_SE_DMA:
546690d8b91SDouglas Anderson 		if (spi_geni_is_abort_still_pending(mas))
547690d8b91SDouglas Anderson 			return -EBUSY;
548561de45fSGirish Mahadevan 		ret = setup_fifo_params(spi_msg->spi, spi);
549561de45fSGirish Mahadevan 		if (ret)
550561de45fSGirish Mahadevan 			dev_err(mas->dev, "Couldn't select mode %d\n", ret);
551561de45fSGirish Mahadevan 		return ret;
552b59c1224SVinod Koul 
553b59c1224SVinod Koul 	case GENI_GPI_DMA:
554b59c1224SVinod Koul 		/* nothing to do for GPI DMA */
555b59c1224SVinod Koul 		return 0;
556b59c1224SVinod Koul 	}
557b59c1224SVinod Koul 
558b59c1224SVinod Koul 	dev_err(mas->dev, "Mode not supported %d", mas->cur_xfer_mode);
559b59c1224SVinod Koul 	return -EINVAL;
560b59c1224SVinod Koul }
561b59c1224SVinod Koul 
562b59c1224SVinod Koul static int spi_geni_grab_gpi_chan(struct spi_geni_master *mas)
563b59c1224SVinod Koul {
564b59c1224SVinod Koul 	int ret;
565b59c1224SVinod Koul 
566b59c1224SVinod Koul 	mas->tx = dma_request_chan(mas->dev, "tx");
5676532582cSDan Carpenter 	if (IS_ERR(mas->tx)) {
5686532582cSDan Carpenter 		ret = dev_err_probe(mas->dev, PTR_ERR(mas->tx),
5696532582cSDan Carpenter 				    "Failed to get tx DMA ch\n");
570b59c1224SVinod Koul 		goto err_tx;
5716532582cSDan Carpenter 	}
572b59c1224SVinod Koul 
573b59c1224SVinod Koul 	mas->rx = dma_request_chan(mas->dev, "rx");
5746532582cSDan Carpenter 	if (IS_ERR(mas->rx)) {
5756532582cSDan Carpenter 		ret = dev_err_probe(mas->dev, PTR_ERR(mas->rx),
5766532582cSDan Carpenter 				    "Failed to get rx DMA ch\n");
577b59c1224SVinod Koul 		goto err_rx;
5786532582cSDan Carpenter 	}
579b59c1224SVinod Koul 
580b59c1224SVinod Koul 	return 0;
581b59c1224SVinod Koul 
582b59c1224SVinod Koul err_rx:
583b59c1224SVinod Koul 	mas->rx = NULL;
5846532582cSDan Carpenter 	dma_release_channel(mas->tx);
5856532582cSDan Carpenter err_tx:
5866532582cSDan Carpenter 	mas->tx = NULL;
587b59c1224SVinod Koul 	return ret;
588b59c1224SVinod Koul }
589b59c1224SVinod Koul 
590b59c1224SVinod Koul static void spi_geni_release_dma_chan(struct spi_geni_master *mas)
591b59c1224SVinod Koul {
592b59c1224SVinod Koul 	if (mas->rx) {
593b59c1224SVinod Koul 		dma_release_channel(mas->rx);
594b59c1224SVinod Koul 		mas->rx = NULL;
595b59c1224SVinod Koul 	}
596b59c1224SVinod Koul 
597b59c1224SVinod Koul 	if (mas->tx) {
598b59c1224SVinod Koul 		dma_release_channel(mas->tx);
599b59c1224SVinod Koul 		mas->tx = NULL;
600b59c1224SVinod Koul 	}
601561de45fSGirish Mahadevan }
602561de45fSGirish Mahadevan 
603561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
604561de45fSGirish Mahadevan {
605561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
606561de45fSGirish Mahadevan 	unsigned int proto, major, minor, ver;
607b59c1224SVinod Koul 	u32 spi_tx_cfg, fifo_disable;
608b59c1224SVinod Koul 	int ret = -ENXIO;
609561de45fSGirish Mahadevan 
610561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
611561de45fSGirish Mahadevan 
612561de45fSGirish Mahadevan 	proto = geni_se_read_proto(se);
613561de45fSGirish Mahadevan 	if (proto != GENI_SE_SPI) {
614561de45fSGirish Mahadevan 		dev_err(mas->dev, "Invalid proto %d\n", proto);
615b59c1224SVinod Koul 		goto out_pm;
616561de45fSGirish Mahadevan 	}
617561de45fSGirish Mahadevan 	mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
618561de45fSGirish Mahadevan 
619561de45fSGirish Mahadevan 	/* Width of Tx and Rx FIFO is same */
620561de45fSGirish Mahadevan 	mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
621561de45fSGirish Mahadevan 
622561de45fSGirish Mahadevan 	/*
623561de45fSGirish Mahadevan 	 * Hardware programming guide suggests to configure
624561de45fSGirish Mahadevan 	 * RX FIFO RFR level to fifo_depth-2.
625561de45fSGirish Mahadevan 	 */
626fc129a43SDouglas Anderson 	geni_se_init(se, mas->tx_fifo_depth - 3, mas->tx_fifo_depth - 2);
627561de45fSGirish Mahadevan 	/* Transmit an entire FIFO worth of data per IRQ */
628561de45fSGirish Mahadevan 	mas->tx_wm = 1;
629561de45fSGirish Mahadevan 	ver = geni_se_get_qup_hw_version(se);
630561de45fSGirish Mahadevan 	major = GENI_SE_VERSION_MAJOR(ver);
631561de45fSGirish Mahadevan 	minor = GENI_SE_VERSION_MINOR(ver);
632561de45fSGirish Mahadevan 
633561de45fSGirish Mahadevan 	if (major == 1 && minor == 0)
634561de45fSGirish Mahadevan 		mas->oversampling = 2;
635561de45fSGirish Mahadevan 	else
636561de45fSGirish Mahadevan 		mas->oversampling = 1;
637561de45fSGirish Mahadevan 
638b59c1224SVinod Koul 	fifo_disable = readl(se->base + GENI_IF_DISABLE_RO) & FIFO_IF_DISABLE;
639b59c1224SVinod Koul 	switch (fifo_disable) {
640b59c1224SVinod Koul 	case 1:
641b59c1224SVinod Koul 		ret = spi_geni_grab_gpi_chan(mas);
642b59c1224SVinod Koul 		if (!ret) { /* success case */
643b59c1224SVinod Koul 			mas->cur_xfer_mode = GENI_GPI_DMA;
644b59c1224SVinod Koul 			geni_se_select_mode(se, GENI_GPI_DMA);
645b59c1224SVinod Koul 			dev_dbg(mas->dev, "Using GPI DMA mode for SPI\n");
646b59c1224SVinod Koul 			break;
647b59c1224SVinod Koul 		}
648b59c1224SVinod Koul 		/*
649*e5f0dfa7SVijaya Krishna Nivarthi 		 * in case of failure to get gpi dma channel, we can still do the
650b59c1224SVinod Koul 		 * FIFO mode, so fallthrough
651b59c1224SVinod Koul 		 */
652b59c1224SVinod Koul 		dev_warn(mas->dev, "FIFO mode disabled, but couldn't get DMA, fall back to FIFO mode\n");
653b59c1224SVinod Koul 		fallthrough;
654b59c1224SVinod Koul 
655b59c1224SVinod Koul 	case 0:
656b59c1224SVinod Koul 		mas->cur_xfer_mode = GENI_SE_FIFO;
657da48dc8cSDouglas Anderson 		geni_se_select_mode(se, GENI_SE_FIFO);
658b59c1224SVinod Koul 		ret = 0;
659b59c1224SVinod Koul 		break;
660b59c1224SVinod Koul 	}
661da48dc8cSDouglas Anderson 
66214ac4e04SDouglas Anderson 	/* We always control CS manually */
66314ac4e04SDouglas Anderson 	spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
66414ac4e04SDouglas Anderson 	spi_tx_cfg &= ~CS_TOGGLE;
66514ac4e04SDouglas Anderson 	writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
66614ac4e04SDouglas Anderson 
667b59c1224SVinod Koul out_pm:
668561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
669b59c1224SVinod Koul 	return ret;
670561de45fSGirish Mahadevan }
671561de45fSGirish Mahadevan 
6726d66507dSDouglas Anderson static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
6736d66507dSDouglas Anderson {
6746d66507dSDouglas Anderson 	/*
6756d66507dSDouglas Anderson 	 * Calculate how many bytes we'll put in each FIFO word.  If the
6766d66507dSDouglas Anderson 	 * transfer words don't pack cleanly into a FIFO word we'll just put
6776d66507dSDouglas Anderson 	 * one transfer word in each FIFO word.  If they do pack we'll pack 'em.
6786d66507dSDouglas Anderson 	 */
6796d66507dSDouglas Anderson 	if (mas->fifo_width_bits % mas->cur_bits_per_word)
6806d66507dSDouglas Anderson 		return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
6816d66507dSDouglas Anderson 						       BITS_PER_BYTE));
6826d66507dSDouglas Anderson 
6836d66507dSDouglas Anderson 	return mas->fifo_width_bits / BITS_PER_BYTE;
6846d66507dSDouglas Anderson }
6856d66507dSDouglas Anderson 
6866d66507dSDouglas Anderson static bool geni_spi_handle_tx(struct spi_geni_master *mas)
6876d66507dSDouglas Anderson {
6886d66507dSDouglas Anderson 	struct geni_se *se = &mas->se;
6896d66507dSDouglas Anderson 	unsigned int max_bytes;
6906d66507dSDouglas Anderson 	const u8 *tx_buf;
6916d66507dSDouglas Anderson 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
6926d66507dSDouglas Anderson 	unsigned int i = 0;
6936d66507dSDouglas Anderson 
6944aa1464aSDouglas Anderson 	/* Stop the watermark IRQ if nothing to send */
6954aa1464aSDouglas Anderson 	if (!mas->cur_xfer) {
6964aa1464aSDouglas Anderson 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
6974aa1464aSDouglas Anderson 		return false;
6984aa1464aSDouglas Anderson 	}
6994aa1464aSDouglas Anderson 
7006d66507dSDouglas Anderson 	max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
7016d66507dSDouglas Anderson 	if (mas->tx_rem_bytes < max_bytes)
7026d66507dSDouglas Anderson 		max_bytes = mas->tx_rem_bytes;
7036d66507dSDouglas Anderson 
7046d66507dSDouglas Anderson 	tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
7056d66507dSDouglas Anderson 	while (i < max_bytes) {
7066d66507dSDouglas Anderson 		unsigned int j;
7076d66507dSDouglas Anderson 		unsigned int bytes_to_write;
7086d66507dSDouglas Anderson 		u32 fifo_word = 0;
7096d66507dSDouglas Anderson 		u8 *fifo_byte = (u8 *)&fifo_word;
7106d66507dSDouglas Anderson 
7116d66507dSDouglas Anderson 		bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
7126d66507dSDouglas Anderson 		for (j = 0; j < bytes_to_write; j++)
7136d66507dSDouglas Anderson 			fifo_byte[j] = tx_buf[i++];
7146d66507dSDouglas Anderson 		iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
7156d66507dSDouglas Anderson 	}
7166d66507dSDouglas Anderson 	mas->tx_rem_bytes -= max_bytes;
7176d66507dSDouglas Anderson 	if (!mas->tx_rem_bytes) {
7186d66507dSDouglas Anderson 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
7196d66507dSDouglas Anderson 		return false;
7206d66507dSDouglas Anderson 	}
7216d66507dSDouglas Anderson 	return true;
7226d66507dSDouglas Anderson }
7236d66507dSDouglas Anderson 
7246d66507dSDouglas Anderson static void geni_spi_handle_rx(struct spi_geni_master *mas)
7256d66507dSDouglas Anderson {
7266d66507dSDouglas Anderson 	struct geni_se *se = &mas->se;
7276d66507dSDouglas Anderson 	u32 rx_fifo_status;
7286d66507dSDouglas Anderson 	unsigned int rx_bytes;
7296d66507dSDouglas Anderson 	unsigned int rx_last_byte_valid;
7306d66507dSDouglas Anderson 	u8 *rx_buf;
7316d66507dSDouglas Anderson 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
7326d66507dSDouglas Anderson 	unsigned int i = 0;
7336d66507dSDouglas Anderson 
7346d66507dSDouglas Anderson 	rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
7356d66507dSDouglas Anderson 	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
7366d66507dSDouglas Anderson 	if (rx_fifo_status & RX_LAST) {
7376d66507dSDouglas Anderson 		rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
7386d66507dSDouglas Anderson 		rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
7396d66507dSDouglas Anderson 		if (rx_last_byte_valid && rx_last_byte_valid < 4)
7406d66507dSDouglas Anderson 			rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
7416d66507dSDouglas Anderson 	}
7424aa1464aSDouglas Anderson 
7434aa1464aSDouglas Anderson 	/* Clear out the FIFO and bail if nowhere to put it */
7444aa1464aSDouglas Anderson 	if (!mas->cur_xfer) {
7454aa1464aSDouglas Anderson 		for (i = 0; i < DIV_ROUND_UP(rx_bytes, bytes_per_fifo_word); i++)
7464aa1464aSDouglas Anderson 			readl(se->base + SE_GENI_RX_FIFOn);
7474aa1464aSDouglas Anderson 		return;
7484aa1464aSDouglas Anderson 	}
7494aa1464aSDouglas Anderson 
7506d66507dSDouglas Anderson 	if (mas->rx_rem_bytes < rx_bytes)
7516d66507dSDouglas Anderson 		rx_bytes = mas->rx_rem_bytes;
7526d66507dSDouglas Anderson 
7536d66507dSDouglas Anderson 	rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
7546d66507dSDouglas Anderson 	while (i < rx_bytes) {
7556d66507dSDouglas Anderson 		u32 fifo_word = 0;
7566d66507dSDouglas Anderson 		u8 *fifo_byte = (u8 *)&fifo_word;
7576d66507dSDouglas Anderson 		unsigned int bytes_to_read;
7586d66507dSDouglas Anderson 		unsigned int j;
7596d66507dSDouglas Anderson 
7606d66507dSDouglas Anderson 		bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
7616d66507dSDouglas Anderson 		ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
7626d66507dSDouglas Anderson 		for (j = 0; j < bytes_to_read; j++)
7636d66507dSDouglas Anderson 			rx_buf[i++] = fifo_byte[j];
7646d66507dSDouglas Anderson 	}
7656d66507dSDouglas Anderson 	mas->rx_rem_bytes -= rx_bytes;
7666d66507dSDouglas Anderson }
7676d66507dSDouglas Anderson 
768*e5f0dfa7SVijaya Krishna Nivarthi static int setup_se_xfer(struct spi_transfer *xfer,
769561de45fSGirish Mahadevan 				struct spi_geni_master *mas,
770561de45fSGirish Mahadevan 				u16 mode, struct spi_master *spi)
771561de45fSGirish Mahadevan {
772561de45fSGirish Mahadevan 	u32 m_cmd = 0;
773*e5f0dfa7SVijaya Krishna Nivarthi 	u32 len, fifo_size;
774561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
775e68b6624SDouglas Anderson 	int ret;
776561de45fSGirish Mahadevan 
7772ee471a1SDouglas Anderson 	/*
7782ee471a1SDouglas Anderson 	 * Ensure that our interrupt handler isn't still running from some
7792ee471a1SDouglas Anderson 	 * prior command before we start messing with the hardware behind
7802ee471a1SDouglas Anderson 	 * its back.  We don't need to _keep_ the lock here since we're only
7812ee471a1SDouglas Anderson 	 * worried about racing with out interrupt handler.  The SPI core
7822ee471a1SDouglas Anderson 	 * already handles making sure that we're not trying to do two
7832ee471a1SDouglas Anderson 	 * transfers at once or setting a chip select and doing a transfer
7842ee471a1SDouglas Anderson 	 * concurrently.
7852ee471a1SDouglas Anderson 	 *
7862ee471a1SDouglas Anderson 	 * NOTE: we actually _can't_ hold the lock here because possibly we
7872ee471a1SDouglas Anderson 	 * might call clk_set_rate() which needs to be able to sleep.
7882ee471a1SDouglas Anderson 	 */
7892ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
7902ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
7912ee471a1SDouglas Anderson 
792561de45fSGirish Mahadevan 	if (xfer->bits_per_word != mas->cur_bits_per_word) {
793561de45fSGirish Mahadevan 		spi_setup_word_len(mas, mode, xfer->bits_per_word);
794561de45fSGirish Mahadevan 		mas->cur_bits_per_word = xfer->bits_per_word;
795561de45fSGirish Mahadevan 	}
796561de45fSGirish Mahadevan 
797561de45fSGirish Mahadevan 	/* Speed and bits per word can be overridden per transfer */
7980e3b8a81SAkash Asthana 	ret = geni_spi_set_clock_and_bw(mas, xfer->speed_hz);
799e68b6624SDouglas Anderson 	if (ret)
800*e5f0dfa7SVijaya Krishna Nivarthi 		return ret;
801561de45fSGirish Mahadevan 
802561de45fSGirish Mahadevan 	mas->tx_rem_bytes = 0;
803561de45fSGirish Mahadevan 	mas->rx_rem_bytes = 0;
804561de45fSGirish Mahadevan 
805561de45fSGirish Mahadevan 	if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
806561de45fSGirish Mahadevan 		len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
807561de45fSGirish Mahadevan 	else
808561de45fSGirish Mahadevan 		len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
809561de45fSGirish Mahadevan 	len &= TRANS_LEN_MSK;
810561de45fSGirish Mahadevan 
811561de45fSGirish Mahadevan 	mas->cur_xfer = xfer;
81219ea3275SStephen Boyd 	if (xfer->tx_buf) {
81319ea3275SStephen Boyd 		m_cmd |= SPI_TX_ONLY;
814561de45fSGirish Mahadevan 		mas->tx_rem_bytes = xfer->len;
815561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_TX_TRANS_LEN);
816561de45fSGirish Mahadevan 	}
817561de45fSGirish Mahadevan 
81819ea3275SStephen Boyd 	if (xfer->rx_buf) {
81919ea3275SStephen Boyd 		m_cmd |= SPI_RX_ONLY;
820561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_RX_TRANS_LEN);
821561de45fSGirish Mahadevan 		mas->rx_rem_bytes = xfer->len;
822561de45fSGirish Mahadevan 	}
8232ee471a1SDouglas Anderson 
824*e5f0dfa7SVijaya Krishna Nivarthi 	/* Select transfer mode based on transfer length */
825*e5f0dfa7SVijaya Krishna Nivarthi 	fifo_size = mas->tx_fifo_depth * mas->fifo_width_bits / mas->cur_bits_per_word;
826*e5f0dfa7SVijaya Krishna Nivarthi 	mas->cur_xfer_mode = (len <= fifo_size) ? GENI_SE_FIFO : GENI_SE_DMA;
827*e5f0dfa7SVijaya Krishna Nivarthi 	geni_se_select_mode(se, mas->cur_xfer_mode);
828*e5f0dfa7SVijaya Krishna Nivarthi 
8292ee471a1SDouglas Anderson 	/*
8302ee471a1SDouglas Anderson 	 * Lock around right before we start the transfer since our
8312ee471a1SDouglas Anderson 	 * interrupt could come in at any time now.
8322ee471a1SDouglas Anderson 	 */
8332ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
834561de45fSGirish Mahadevan 	geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
835*e5f0dfa7SVijaya Krishna Nivarthi 
836*e5f0dfa7SVijaya Krishna Nivarthi 	if (mas->cur_xfer_mode == GENI_SE_DMA) {
837*e5f0dfa7SVijaya Krishna Nivarthi 		if (m_cmd & SPI_RX_ONLY) {
838*e5f0dfa7SVijaya Krishna Nivarthi 			ret =  geni_se_rx_dma_prep(se, xfer->rx_buf,
839*e5f0dfa7SVijaya Krishna Nivarthi 				xfer->len, &mas->rx_se_dma);
840*e5f0dfa7SVijaya Krishna Nivarthi 			if (ret) {
841*e5f0dfa7SVijaya Krishna Nivarthi 				dev_err(mas->dev, "Failed to setup Rx dma %d\n", ret);
842*e5f0dfa7SVijaya Krishna Nivarthi 				mas->rx_se_dma = 0;
843*e5f0dfa7SVijaya Krishna Nivarthi 				goto unlock_and_return;
844*e5f0dfa7SVijaya Krishna Nivarthi 			}
845*e5f0dfa7SVijaya Krishna Nivarthi 		}
8466d66507dSDouglas Anderson 		if (m_cmd & SPI_TX_ONLY) {
847*e5f0dfa7SVijaya Krishna Nivarthi 			ret =  geni_se_tx_dma_prep(se, (void *)xfer->tx_buf,
848*e5f0dfa7SVijaya Krishna Nivarthi 				xfer->len, &mas->tx_se_dma);
849*e5f0dfa7SVijaya Krishna Nivarthi 			if (ret) {
850*e5f0dfa7SVijaya Krishna Nivarthi 				dev_err(mas->dev, "Failed to setup Tx dma %d\n", ret);
851*e5f0dfa7SVijaya Krishna Nivarthi 				mas->tx_se_dma = 0;
852*e5f0dfa7SVijaya Krishna Nivarthi 				if (m_cmd & SPI_RX_ONLY) {
853*e5f0dfa7SVijaya Krishna Nivarthi 					/* Unmap rx buffer if duplex transfer */
854*e5f0dfa7SVijaya Krishna Nivarthi 					geni_se_rx_dma_unprep(se, mas->rx_se_dma, xfer->len);
855*e5f0dfa7SVijaya Krishna Nivarthi 					mas->rx_se_dma = 0;
856*e5f0dfa7SVijaya Krishna Nivarthi 				}
857*e5f0dfa7SVijaya Krishna Nivarthi 				goto unlock_and_return;
858*e5f0dfa7SVijaya Krishna Nivarthi 			}
859*e5f0dfa7SVijaya Krishna Nivarthi 		}
860*e5f0dfa7SVijaya Krishna Nivarthi 	} else if (m_cmd & SPI_TX_ONLY) {
8616d66507dSDouglas Anderson 		if (geni_spi_handle_tx(mas))
862561de45fSGirish Mahadevan 			writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
8636d66507dSDouglas Anderson 	}
864*e5f0dfa7SVijaya Krishna Nivarthi 
865*e5f0dfa7SVijaya Krishna Nivarthi unlock_and_return:
8662ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
867*e5f0dfa7SVijaya Krishna Nivarthi 	return ret;
868561de45fSGirish Mahadevan }
869561de45fSGirish Mahadevan 
870561de45fSGirish Mahadevan static int spi_geni_transfer_one(struct spi_master *spi,
871561de45fSGirish Mahadevan 				struct spi_device *slv,
872561de45fSGirish Mahadevan 				struct spi_transfer *xfer)
873561de45fSGirish Mahadevan {
874561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
875*e5f0dfa7SVijaya Krishna Nivarthi 	int ret;
876561de45fSGirish Mahadevan 
877690d8b91SDouglas Anderson 	if (spi_geni_is_abort_still_pending(mas))
878690d8b91SDouglas Anderson 		return -EBUSY;
879690d8b91SDouglas Anderson 
880561de45fSGirish Mahadevan 	/* Terminate and return success for 0 byte length transfer */
881561de45fSGirish Mahadevan 	if (!xfer->len)
882561de45fSGirish Mahadevan 		return 0;
883561de45fSGirish Mahadevan 
884*e5f0dfa7SVijaya Krishna Nivarthi 	if (mas->cur_xfer_mode == GENI_SE_FIFO || mas->cur_xfer_mode == GENI_SE_DMA) {
885*e5f0dfa7SVijaya Krishna Nivarthi 		ret = setup_se_xfer(xfer, mas, slv->mode, spi);
886*e5f0dfa7SVijaya Krishna Nivarthi 		/* SPI framework expects +ve ret code to wait for transfer complete */
887*e5f0dfa7SVijaya Krishna Nivarthi 		if (!ret)
888*e5f0dfa7SVijaya Krishna Nivarthi 			ret = 1;
889*e5f0dfa7SVijaya Krishna Nivarthi 		return ret;
890561de45fSGirish Mahadevan 	}
891b59c1224SVinod Koul 	return setup_gsi_xfer(xfer, mas, slv, spi);
892b59c1224SVinod Koul }
893561de45fSGirish Mahadevan 
894561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
895561de45fSGirish Mahadevan {
896561de45fSGirish Mahadevan 	struct spi_master *spi = data;
897561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
898561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
899561de45fSGirish Mahadevan 	u32 m_irq;
900561de45fSGirish Mahadevan 
9012ee471a1SDouglas Anderson 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
9022ee471a1SDouglas Anderson 	if (!m_irq)
903561de45fSGirish Mahadevan 		return IRQ_NONE;
904561de45fSGirish Mahadevan 
905e191a082SDouglas Anderson 	if (m_irq & (M_CMD_OVERRUN_EN | M_ILLEGAL_CMD_EN | M_CMD_FAILURE_EN |
906e191a082SDouglas Anderson 		     M_RX_FIFO_RD_ERR_EN | M_RX_FIFO_WR_ERR_EN |
907e191a082SDouglas Anderson 		     M_TX_FIFO_RD_ERR_EN | M_TX_FIFO_WR_ERR_EN))
908e191a082SDouglas Anderson 		dev_warn(mas->dev, "Unexpected IRQ err status %#010x\n", m_irq);
909e191a082SDouglas Anderson 
910539afdf9SDouglas Anderson 	spin_lock(&mas->lock);
911561de45fSGirish Mahadevan 
912*e5f0dfa7SVijaya Krishna Nivarthi 	if (mas->cur_xfer_mode == GENI_SE_FIFO) {
913561de45fSGirish Mahadevan 		if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
914561de45fSGirish Mahadevan 			geni_spi_handle_rx(mas);
915561de45fSGirish Mahadevan 
916561de45fSGirish Mahadevan 		if (m_irq & M_TX_FIFO_WATERMARK_EN)
917561de45fSGirish Mahadevan 			geni_spi_handle_tx(mas);
918561de45fSGirish Mahadevan 
919561de45fSGirish Mahadevan 		if (m_irq & M_CMD_DONE_EN) {
9207ba9bdcbSDouglas Anderson 			if (mas->cur_xfer) {
921561de45fSGirish Mahadevan 				spi_finalize_current_transfer(spi);
9227ba9bdcbSDouglas Anderson 				mas->cur_xfer = NULL;
923561de45fSGirish Mahadevan 				/*
92459ab0fa0SStephen Boyd 				 * If this happens, then a CMD_DONE came before all the
92559ab0fa0SStephen Boyd 				 * Tx buffer bytes were sent out. This is unusual, log
92659ab0fa0SStephen Boyd 				 * this condition and disable the WM interrupt to
92759ab0fa0SStephen Boyd 				 * prevent the system from stalling due an interrupt
92859ab0fa0SStephen Boyd 				 * storm.
92959ab0fa0SStephen Boyd 				 *
93059ab0fa0SStephen Boyd 				 * If this happens when all Rx bytes haven't been
93159ab0fa0SStephen Boyd 				 * received, log the condition. The only known time
93259ab0fa0SStephen Boyd 				 * this can happen is if bits_per_word != 8 and some
93359ab0fa0SStephen Boyd 				 * registers that expect xfer lengths in num spi_words
934561de45fSGirish Mahadevan 				 * weren't written correctly.
935561de45fSGirish Mahadevan 				 */
936561de45fSGirish Mahadevan 				if (mas->tx_rem_bytes) {
937561de45fSGirish Mahadevan 					writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
938561de45fSGirish Mahadevan 					dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
939561de45fSGirish Mahadevan 						mas->tx_rem_bytes, mas->cur_bits_per_word);
940561de45fSGirish Mahadevan 				}
941561de45fSGirish Mahadevan 				if (mas->rx_rem_bytes)
942561de45fSGirish Mahadevan 					dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
943561de45fSGirish Mahadevan 						mas->rx_rem_bytes, mas->cur_bits_per_word);
94459ab0fa0SStephen Boyd 			} else {
94559ab0fa0SStephen Boyd 				complete(&mas->cs_done);
94659ab0fa0SStephen Boyd 			}
947561de45fSGirish Mahadevan 		}
948*e5f0dfa7SVijaya Krishna Nivarthi 	} else if (mas->cur_xfer_mode == GENI_SE_DMA) {
949*e5f0dfa7SVijaya Krishna Nivarthi 		const struct spi_transfer *xfer = mas->cur_xfer;
950*e5f0dfa7SVijaya Krishna Nivarthi 		u32 dma_tx_status = readl_relaxed(se->base + SE_DMA_TX_IRQ_STAT);
951*e5f0dfa7SVijaya Krishna Nivarthi 		u32 dma_rx_status = readl_relaxed(se->base + SE_DMA_RX_IRQ_STAT);
952*e5f0dfa7SVijaya Krishna Nivarthi 
953*e5f0dfa7SVijaya Krishna Nivarthi 		if (dma_tx_status)
954*e5f0dfa7SVijaya Krishna Nivarthi 			writel(dma_tx_status, se->base + SE_DMA_TX_IRQ_CLR);
955*e5f0dfa7SVijaya Krishna Nivarthi 		if (dma_rx_status)
956*e5f0dfa7SVijaya Krishna Nivarthi 			writel(dma_rx_status, se->base + SE_DMA_RX_IRQ_CLR);
957*e5f0dfa7SVijaya Krishna Nivarthi 		if (dma_tx_status & TX_DMA_DONE)
958*e5f0dfa7SVijaya Krishna Nivarthi 			mas->tx_rem_bytes = 0;
959*e5f0dfa7SVijaya Krishna Nivarthi 		if (dma_rx_status & RX_DMA_DONE)
960*e5f0dfa7SVijaya Krishna Nivarthi 			mas->rx_rem_bytes = 0;
961*e5f0dfa7SVijaya Krishna Nivarthi 		if (dma_tx_status & TX_RESET_DONE)
962*e5f0dfa7SVijaya Krishna Nivarthi 			complete(&mas->tx_reset_done);
963*e5f0dfa7SVijaya Krishna Nivarthi 		if (dma_rx_status & RX_RESET_DONE)
964*e5f0dfa7SVijaya Krishna Nivarthi 			complete(&mas->rx_reset_done);
965*e5f0dfa7SVijaya Krishna Nivarthi 		if (!mas->tx_rem_bytes && !mas->rx_rem_bytes && xfer) {
966*e5f0dfa7SVijaya Krishna Nivarthi 			if (xfer->tx_buf && mas->tx_se_dma) {
967*e5f0dfa7SVijaya Krishna Nivarthi 				geni_se_tx_dma_unprep(se, mas->tx_se_dma, xfer->len);
968*e5f0dfa7SVijaya Krishna Nivarthi 				mas->tx_se_dma = 0;
969*e5f0dfa7SVijaya Krishna Nivarthi 			}
970*e5f0dfa7SVijaya Krishna Nivarthi 			if (xfer->rx_buf && mas->rx_se_dma) {
971*e5f0dfa7SVijaya Krishna Nivarthi 				geni_se_rx_dma_unprep(se, mas->rx_se_dma, xfer->len);
972*e5f0dfa7SVijaya Krishna Nivarthi 				mas->rx_se_dma = 0;
973*e5f0dfa7SVijaya Krishna Nivarthi 			}
974*e5f0dfa7SVijaya Krishna Nivarthi 			spi_finalize_current_transfer(spi);
975*e5f0dfa7SVijaya Krishna Nivarthi 			mas->cur_xfer = NULL;
976*e5f0dfa7SVijaya Krishna Nivarthi 		}
977*e5f0dfa7SVijaya Krishna Nivarthi 	}
978561de45fSGirish Mahadevan 
9797ba9bdcbSDouglas Anderson 	if (m_irq & M_CMD_CANCEL_EN)
9807ba9bdcbSDouglas Anderson 		complete(&mas->cancel_done);
9817ba9bdcbSDouglas Anderson 	if (m_irq & M_CMD_ABORT_EN)
9827ba9bdcbSDouglas Anderson 		complete(&mas->abort_done);
983561de45fSGirish Mahadevan 
9842ee471a1SDouglas Anderson 	/*
985db56d030SJay Fang 	 * It's safe or a good idea to Ack all of our interrupts at the end
986db56d030SJay Fang 	 * of the function. Specifically:
9872ee471a1SDouglas Anderson 	 * - M_CMD_DONE_EN / M_RX_FIFO_LAST_EN: Edge triggered interrupts and
9882ee471a1SDouglas Anderson 	 *   clearing Acks. Clearing at the end relies on nobody else having
9892ee471a1SDouglas Anderson 	 *   started a new transfer yet or else we could be clearing _their_
9902ee471a1SDouglas Anderson 	 *   done bit, but everyone grabs the spinlock before starting a new
9912ee471a1SDouglas Anderson 	 *   transfer.
9922ee471a1SDouglas Anderson 	 * - M_RX_FIFO_WATERMARK_EN / M_TX_FIFO_WATERMARK_EN: These appear
9932ee471a1SDouglas Anderson 	 *   to be "latched level" interrupts so it's important to clear them
9942ee471a1SDouglas Anderson 	 *   _after_ you've handled the condition and always safe to do so
9952ee471a1SDouglas Anderson 	 *   since they'll re-assert if they're still happening.
9962ee471a1SDouglas Anderson 	 */
997561de45fSGirish Mahadevan 	writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
9982ee471a1SDouglas Anderson 
999539afdf9SDouglas Anderson 	spin_unlock(&mas->lock);
10002ee471a1SDouglas Anderson 
10010dccff3cSAlok Chauhan 	return IRQ_HANDLED;
1002561de45fSGirish Mahadevan }
1003561de45fSGirish Mahadevan 
1004561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
1005561de45fSGirish Mahadevan {
10066a34e285SAlok Chauhan 	int ret, irq;
1007561de45fSGirish Mahadevan 	struct spi_master *spi;
1008561de45fSGirish Mahadevan 	struct spi_geni_master *mas;
10096a34e285SAlok Chauhan 	void __iomem *base;
10106a34e285SAlok Chauhan 	struct clk *clk;
1011ea1e5b33SStephen Boyd 	struct device *dev = &pdev->dev;
10126a34e285SAlok Chauhan 
10136a34e285SAlok Chauhan 	irq = platform_get_irq(pdev, 0);
10146b8ac10eSStephen Boyd 	if (irq < 0)
10156a34e285SAlok Chauhan 		return irq;
10166a34e285SAlok Chauhan 
1017b59c1224SVinod Koul 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
1018b59c1224SVinod Koul 	if (ret)
1019b59c1224SVinod Koul 		return dev_err_probe(dev, ret, "could not set DMA mask\n");
1020b59c1224SVinod Koul 
1021d8e477abSYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
10226a34e285SAlok Chauhan 	if (IS_ERR(base))
10236a34e285SAlok Chauhan 		return PTR_ERR(base);
10246a34e285SAlok Chauhan 
1025ea1e5b33SStephen Boyd 	clk = devm_clk_get(dev, "se");
1026ea1e5b33SStephen Boyd 	if (IS_ERR(clk))
10276a34e285SAlok Chauhan 		return PTR_ERR(clk);
1028561de45fSGirish Mahadevan 
10298f96c434SLukas Wunner 	spi = devm_spi_alloc_master(dev, sizeof(*mas));
1030561de45fSGirish Mahadevan 	if (!spi)
1031561de45fSGirish Mahadevan 		return -ENOMEM;
1032561de45fSGirish Mahadevan 
1033561de45fSGirish Mahadevan 	platform_set_drvdata(pdev, spi);
1034561de45fSGirish Mahadevan 	mas = spi_master_get_devdata(spi);
10356a34e285SAlok Chauhan 	mas->irq = irq;
1036ea1e5b33SStephen Boyd 	mas->dev = dev;
1037ea1e5b33SStephen Boyd 	mas->se.dev = dev;
1038ea1e5b33SStephen Boyd 	mas->se.wrapper = dev_get_drvdata(dev->parent);
10396a34e285SAlok Chauhan 	mas->se.base = base;
10406a34e285SAlok Chauhan 	mas->se.clk = clk;
1041cfb12911SYangtao Li 
1042cfb12911SYangtao Li 	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1043cfb12911SYangtao Li 	if (ret)
1044cfb12911SYangtao Li 		return ret;
10451a9e489eSRajendra Nayak 	/* OPP table is optional */
1046cfb12911SYangtao Li 	ret = devm_pm_opp_of_add_table(&pdev->dev);
10477d568edfSViresh Kumar 	if (ret && ret != -ENODEV) {
10481a9e489eSRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1049cfb12911SYangtao Li 		return ret;
10501a9e489eSRajendra Nayak 	}
1051561de45fSGirish Mahadevan 
1052561de45fSGirish Mahadevan 	spi->bus_num = -1;
1053ea1e5b33SStephen Boyd 	spi->dev.of_node = dev->of_node;
1054561de45fSGirish Mahadevan 	spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
1055561de45fSGirish Mahadevan 	spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1056561de45fSGirish Mahadevan 	spi->num_chipselect = 4;
1057561de45fSGirish Mahadevan 	spi->max_speed_hz = 50000000;
1058561de45fSGirish Mahadevan 	spi->prepare_message = spi_geni_prepare_message;
1059561de45fSGirish Mahadevan 	spi->transfer_one = spi_geni_transfer_one;
1060b59c1224SVinod Koul 	spi->can_dma = geni_can_dma;
1061b59c1224SVinod Koul 	spi->dma_map_dev = dev->parent;
1062561de45fSGirish Mahadevan 	spi->auto_runtime_pm = true;
1063f8039ea5SVinod Koul 	spi->handle_err = spi_geni_handle_err;
10643b25f337SStephen Boyd 	spi->use_gpio_descriptors = true;
1065561de45fSGirish Mahadevan 
10667ba9bdcbSDouglas Anderson 	init_completion(&mas->cs_done);
10677ba9bdcbSDouglas Anderson 	init_completion(&mas->cancel_done);
10687ba9bdcbSDouglas Anderson 	init_completion(&mas->abort_done);
1069*e5f0dfa7SVijaya Krishna Nivarthi 	init_completion(&mas->tx_reset_done);
1070*e5f0dfa7SVijaya Krishna Nivarthi 	init_completion(&mas->rx_reset_done);
1071561de45fSGirish Mahadevan 	spin_lock_init(&mas->lock);
1072cfdab2cdSDouglas Anderson 	pm_runtime_use_autosuspend(&pdev->dev);
1073cfdab2cdSDouglas Anderson 	pm_runtime_set_autosuspend_delay(&pdev->dev, 250);
1074ea1e5b33SStephen Boyd 	pm_runtime_enable(dev);
1075561de45fSGirish Mahadevan 
10760e3b8a81SAkash Asthana 	ret = geni_icc_get(&mas->se, NULL);
10770e3b8a81SAkash Asthana 	if (ret)
10780e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
10790e3b8a81SAkash Asthana 	/* Set the bus quota to a reasonable value for register access */
10800e3b8a81SAkash Asthana 	mas->se.icc_paths[GENI_TO_CORE].avg_bw = Bps_to_icc(CORE_2X_50_MHZ);
10810e3b8a81SAkash Asthana 	mas->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
10820e3b8a81SAkash Asthana 
10830e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(&mas->se);
10840e3b8a81SAkash Asthana 	if (ret)
10850e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
10860e3b8a81SAkash Asthana 
1087561de45fSGirish Mahadevan 	ret = spi_geni_init(mas);
1088561de45fSGirish Mahadevan 	if (ret)
1089561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
1090561de45fSGirish Mahadevan 
1091b59c1224SVinod Koul 	/*
1092b59c1224SVinod Koul 	 * check the mode supported and set_cs for fifo mode only
1093b59c1224SVinod Koul 	 * for dma (gsi) mode, the gsi will set cs based on params passed in
1094b59c1224SVinod Koul 	 * TRE
1095b59c1224SVinod Koul 	 */
1096b59c1224SVinod Koul 	if (mas->cur_xfer_mode == GENI_SE_FIFO)
1097b59c1224SVinod Koul 		spi->set_cs = spi_geni_set_cs;
1098b59c1224SVinod Koul 
1099ea1e5b33SStephen Boyd 	ret = request_irq(mas->irq, geni_spi_isr, 0, dev_name(dev), spi);
1100561de45fSGirish Mahadevan 	if (ret)
1101b59c1224SVinod Koul 		goto spi_geni_release_dma;
1102561de45fSGirish Mahadevan 
1103561de45fSGirish Mahadevan 	ret = spi_register_master(spi);
1104561de45fSGirish Mahadevan 	if (ret)
1105561de45fSGirish Mahadevan 		goto spi_geni_probe_free_irq;
1106561de45fSGirish Mahadevan 
1107561de45fSGirish Mahadevan 	return 0;
1108561de45fSGirish Mahadevan spi_geni_probe_free_irq:
1109561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
1110b59c1224SVinod Koul spi_geni_release_dma:
1111b59c1224SVinod Koul 	spi_geni_release_dma_chan(mas);
1112561de45fSGirish Mahadevan spi_geni_probe_runtime_disable:
1113ea1e5b33SStephen Boyd 	pm_runtime_disable(dev);
1114561de45fSGirish Mahadevan 	return ret;
1115561de45fSGirish Mahadevan }
1116561de45fSGirish Mahadevan 
1117561de45fSGirish Mahadevan static int spi_geni_remove(struct platform_device *pdev)
1118561de45fSGirish Mahadevan {
1119561de45fSGirish Mahadevan 	struct spi_master *spi = platform_get_drvdata(pdev);
1120561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
1121561de45fSGirish Mahadevan 
1122561de45fSGirish Mahadevan 	/* Unregister _before_ disabling pm_runtime() so we stop transfers */
1123561de45fSGirish Mahadevan 	spi_unregister_master(spi);
1124561de45fSGirish Mahadevan 
1125b59c1224SVinod Koul 	spi_geni_release_dma_chan(mas);
1126b59c1224SVinod Koul 
1127561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
1128561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
1129561de45fSGirish Mahadevan 	return 0;
1130561de45fSGirish Mahadevan }
1131561de45fSGirish Mahadevan 
1132561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
1133561de45fSGirish Mahadevan {
1134561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
1135561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
11360e3b8a81SAkash Asthana 	int ret;
1137561de45fSGirish Mahadevan 
11381a9e489eSRajendra Nayak 	/* Drop the performance state vote */
11391a9e489eSRajendra Nayak 	dev_pm_opp_set_rate(dev, 0);
11401a9e489eSRajendra Nayak 
11410e3b8a81SAkash Asthana 	ret = geni_se_resources_off(&mas->se);
11420e3b8a81SAkash Asthana 	if (ret)
11430e3b8a81SAkash Asthana 		return ret;
11440e3b8a81SAkash Asthana 
11450e3b8a81SAkash Asthana 	return geni_icc_disable(&mas->se);
1146561de45fSGirish Mahadevan }
1147561de45fSGirish Mahadevan 
1148561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
1149561de45fSGirish Mahadevan {
1150561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
1151561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
11520e3b8a81SAkash Asthana 	int ret;
11530e3b8a81SAkash Asthana 
11540e3b8a81SAkash Asthana 	ret = geni_icc_enable(&mas->se);
11550e3b8a81SAkash Asthana 	if (ret)
11560e3b8a81SAkash Asthana 		return ret;
1157561de45fSGirish Mahadevan 
11585f219524SDouglas Anderson 	ret = geni_se_resources_on(&mas->se);
11595f219524SDouglas Anderson 	if (ret)
11605f219524SDouglas Anderson 		return ret;
11615f219524SDouglas Anderson 
11625f219524SDouglas Anderson 	return dev_pm_opp_set_rate(mas->dev, mas->cur_sclk_hz);
1163561de45fSGirish Mahadevan }
1164561de45fSGirish Mahadevan 
1165561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
1166561de45fSGirish Mahadevan {
1167561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
1168561de45fSGirish Mahadevan 	int ret;
1169561de45fSGirish Mahadevan 
1170561de45fSGirish Mahadevan 	ret = spi_master_suspend(spi);
1171561de45fSGirish Mahadevan 	if (ret)
1172561de45fSGirish Mahadevan 		return ret;
1173561de45fSGirish Mahadevan 
1174561de45fSGirish Mahadevan 	ret = pm_runtime_force_suspend(dev);
1175561de45fSGirish Mahadevan 	if (ret)
1176561de45fSGirish Mahadevan 		spi_master_resume(spi);
1177561de45fSGirish Mahadevan 
1178561de45fSGirish Mahadevan 	return ret;
1179561de45fSGirish Mahadevan }
1180561de45fSGirish Mahadevan 
1181561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
1182561de45fSGirish Mahadevan {
1183561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
1184561de45fSGirish Mahadevan 	int ret;
1185561de45fSGirish Mahadevan 
1186561de45fSGirish Mahadevan 	ret = pm_runtime_force_resume(dev);
1187561de45fSGirish Mahadevan 	if (ret)
1188561de45fSGirish Mahadevan 		return ret;
1189561de45fSGirish Mahadevan 
1190561de45fSGirish Mahadevan 	ret = spi_master_resume(spi);
1191561de45fSGirish Mahadevan 	if (ret)
1192561de45fSGirish Mahadevan 		pm_runtime_force_suspend(dev);
1193561de45fSGirish Mahadevan 
1194561de45fSGirish Mahadevan 	return ret;
1195561de45fSGirish Mahadevan }
1196561de45fSGirish Mahadevan 
1197561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
1198561de45fSGirish Mahadevan 	SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
1199561de45fSGirish Mahadevan 					spi_geni_runtime_resume, NULL)
1200561de45fSGirish Mahadevan 	SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
1201561de45fSGirish Mahadevan };
1202561de45fSGirish Mahadevan 
1203561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
1204561de45fSGirish Mahadevan 	{ .compatible = "qcom,geni-spi" },
1205561de45fSGirish Mahadevan 	{}
1206561de45fSGirish Mahadevan };
1207561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
1208561de45fSGirish Mahadevan 
1209561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
1210561de45fSGirish Mahadevan 	.probe  = spi_geni_probe,
1211561de45fSGirish Mahadevan 	.remove = spi_geni_remove,
1212561de45fSGirish Mahadevan 	.driver = {
1213561de45fSGirish Mahadevan 		.name = "geni_spi",
1214561de45fSGirish Mahadevan 		.pm = &spi_geni_pm_ops,
1215561de45fSGirish Mahadevan 		.of_match_table = spi_geni_dt_match,
1216561de45fSGirish Mahadevan 	},
1217561de45fSGirish Mahadevan };
1218561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
1219561de45fSGirish Mahadevan 
1220561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
1221561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
1222