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>
15d7f74cc3SPraveen Talari #include <linux/property.h>
16491581f4SElliot Berman #include <linux/soc/qcom/geni-se.h>
17561de45fSGirish Mahadevan #include <linux/spi/spi.h>
18561de45fSGirish Mahadevan #include <linux/spinlock.h>
19561de45fSGirish Mahadevan
20561de45fSGirish Mahadevan /* SPI SE specific registers and respective register fields */
21561de45fSGirish Mahadevan #define SE_SPI_CPHA 0x224
22561de45fSGirish Mahadevan #define CPHA BIT(0)
23561de45fSGirish Mahadevan
24561de45fSGirish Mahadevan #define SE_SPI_LOOPBACK 0x22c
25561de45fSGirish Mahadevan #define LOOPBACK_ENABLE 0x1
26561de45fSGirish Mahadevan #define NORMAL_MODE 0x0
27561de45fSGirish Mahadevan #define LOOPBACK_MSK GENMASK(1, 0)
28561de45fSGirish Mahadevan
29561de45fSGirish Mahadevan #define SE_SPI_CPOL 0x230
30561de45fSGirish Mahadevan #define CPOL BIT(2)
31561de45fSGirish Mahadevan
32561de45fSGirish Mahadevan #define SE_SPI_DEMUX_OUTPUT_INV 0x24c
33561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_INV_MSK GENMASK(3, 0)
34561de45fSGirish Mahadevan
35561de45fSGirish Mahadevan #define SE_SPI_DEMUX_SEL 0x250
36561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_SEL GENMASK(3, 0)
37561de45fSGirish Mahadevan
38561de45fSGirish Mahadevan #define SE_SPI_TRANS_CFG 0x25c
395fd7c99eSVijaya Krishna Nivarthi #define CS_TOGGLE BIT(1)
40561de45fSGirish Mahadevan
41561de45fSGirish Mahadevan #define SE_SPI_WORD_LEN 0x268
42561de45fSGirish Mahadevan #define WORD_LEN_MSK GENMASK(9, 0)
43561de45fSGirish Mahadevan #define MIN_WORD_LEN 4
44561de45fSGirish Mahadevan
45561de45fSGirish Mahadevan #define SE_SPI_TX_TRANS_LEN 0x26c
46561de45fSGirish Mahadevan #define SE_SPI_RX_TRANS_LEN 0x270
47561de45fSGirish Mahadevan #define TRANS_LEN_MSK GENMASK(23, 0)
48561de45fSGirish Mahadevan
49561de45fSGirish Mahadevan #define SE_SPI_PRE_POST_CMD_DLY 0x274
50561de45fSGirish Mahadevan
51561de45fSGirish Mahadevan #define SE_SPI_DELAY_COUNTERS 0x278
52561de45fSGirish Mahadevan #define SPI_INTER_WORDS_DELAY_MSK GENMASK(9, 0)
53561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_MSK GENMASK(19, 10)
54561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_SHFT 10
55561de45fSGirish Mahadevan
56d7f74cc3SPraveen Talari #define SE_SPI_SLAVE_EN (0x2BC)
57d7f74cc3SPraveen Talari #define SPI_SLAVE_EN BIT(0)
58d7f74cc3SPraveen Talari
59561de45fSGirish Mahadevan /* M_CMD OP codes for SPI */
60561de45fSGirish Mahadevan #define SPI_TX_ONLY 1
61561de45fSGirish Mahadevan #define SPI_RX_ONLY 2
62561de45fSGirish Mahadevan #define SPI_TX_RX 7
63561de45fSGirish Mahadevan #define SPI_CS_ASSERT 8
64561de45fSGirish Mahadevan #define SPI_CS_DEASSERT 9
65561de45fSGirish Mahadevan #define SPI_SCK_ONLY 10
66561de45fSGirish Mahadevan /* M_CMD params for SPI */
67561de45fSGirish Mahadevan #define SPI_PRE_CMD_DELAY BIT(0)
68561de45fSGirish Mahadevan #define TIMESTAMP_BEFORE BIT(1)
69561de45fSGirish Mahadevan #define FRAGMENTATION BIT(2)
70561de45fSGirish Mahadevan #define TIMESTAMP_AFTER BIT(3)
71561de45fSGirish Mahadevan #define POST_CMD_DELAY BIT(4)
72561de45fSGirish Mahadevan
73b59c1224SVinod Koul #define GSI_LOOPBACK_EN BIT(0)
74b59c1224SVinod Koul #define GSI_CS_TOGGLE BIT(3)
75b59c1224SVinod Koul #define GSI_CPHA BIT(4)
76b59c1224SVinod Koul #define GSI_CPOL BIT(5)
77b59c1224SVinod Koul
78561de45fSGirish Mahadevan struct spi_geni_master {
79561de45fSGirish Mahadevan struct geni_se se;
80561de45fSGirish Mahadevan struct device *dev;
81561de45fSGirish Mahadevan u32 tx_fifo_depth;
82561de45fSGirish Mahadevan u32 fifo_width_bits;
83561de45fSGirish Mahadevan u32 tx_wm;
84da48dc8cSDouglas Anderson u32 last_mode;
85561de45fSGirish Mahadevan unsigned long cur_speed_hz;
865f219524SDouglas Anderson unsigned long cur_sclk_hz;
87561de45fSGirish Mahadevan unsigned int cur_bits_per_word;
88561de45fSGirish Mahadevan unsigned int tx_rem_bytes;
89561de45fSGirish Mahadevan unsigned int rx_rem_bytes;
90561de45fSGirish Mahadevan const struct spi_transfer *cur_xfer;
917ba9bdcbSDouglas Anderson struct completion cs_done;
927ba9bdcbSDouglas Anderson struct completion cancel_done;
937ba9bdcbSDouglas Anderson struct completion abort_done;
94e5f0dfa7SVijaya Krishna Nivarthi struct completion tx_reset_done;
95e5f0dfa7SVijaya Krishna Nivarthi struct completion rx_reset_done;
96561de45fSGirish Mahadevan unsigned int oversampling;
97561de45fSGirish Mahadevan spinlock_t lock;
98561de45fSGirish Mahadevan int irq;
99638d8488SDouglas Anderson bool cs_flag;
100690d8b91SDouglas Anderson bool abort_failed;
101b59c1224SVinod Koul struct dma_chan *tx;
102b59c1224SVinod Koul struct dma_chan *rx;
103b59c1224SVinod Koul int cur_xfer_mode;
104561de45fSGirish Mahadevan };
105561de45fSGirish Mahadevan
spi_slv_setup(struct spi_geni_master * mas)106d7f74cc3SPraveen Talari static void spi_slv_setup(struct spi_geni_master *mas)
107d7f74cc3SPraveen Talari {
108d7f74cc3SPraveen Talari struct geni_se *se = &mas->se;
109d7f74cc3SPraveen Talari
110d7f74cc3SPraveen Talari writel(SPI_SLAVE_EN, se->base + SE_SPI_SLAVE_EN);
111d7f74cc3SPraveen Talari writel(GENI_IO_MUX_0_EN, se->base + GENI_OUTPUT_CTRL);
112d7f74cc3SPraveen Talari writel(START_TRIGGER, se->base + SE_GENI_CFG_SEQ_START);
113d7f74cc3SPraveen Talari dev_dbg(mas->dev, "spi slave setup done\n");
114d7f74cc3SPraveen Talari }
115d7f74cc3SPraveen Talari
get_spi_clk_cfg(unsigned int speed_hz,struct spi_geni_master * mas,unsigned int * clk_idx,unsigned int * clk_div)116561de45fSGirish Mahadevan static int get_spi_clk_cfg(unsigned int speed_hz,
117561de45fSGirish Mahadevan struct spi_geni_master *mas,
118561de45fSGirish Mahadevan unsigned int *clk_idx,
119561de45fSGirish Mahadevan unsigned int *clk_div)
120561de45fSGirish Mahadevan {
121561de45fSGirish Mahadevan unsigned long sclk_freq;
122561de45fSGirish Mahadevan unsigned int actual_hz;
123561de45fSGirish Mahadevan int ret;
124561de45fSGirish Mahadevan
125561de45fSGirish Mahadevan ret = geni_se_clk_freq_match(&mas->se,
126561de45fSGirish Mahadevan speed_hz * mas->oversampling,
127561de45fSGirish Mahadevan clk_idx, &sclk_freq, false);
128561de45fSGirish Mahadevan if (ret) {
129561de45fSGirish Mahadevan dev_err(mas->dev, "Failed(%d) to find src clk for %dHz\n",
130561de45fSGirish Mahadevan ret, speed_hz);
131561de45fSGirish Mahadevan return ret;
132561de45fSGirish Mahadevan }
133561de45fSGirish Mahadevan
134561de45fSGirish Mahadevan *clk_div = DIV_ROUND_UP(sclk_freq, mas->oversampling * speed_hz);
135561de45fSGirish Mahadevan actual_hz = sclk_freq / (mas->oversampling * *clk_div);
136561de45fSGirish Mahadevan
137561de45fSGirish Mahadevan dev_dbg(mas->dev, "req %u=>%u sclk %lu, idx %d, div %d\n", speed_hz,
138561de45fSGirish Mahadevan actual_hz, sclk_freq, *clk_idx, *clk_div);
1391a9e489eSRajendra Nayak ret = dev_pm_opp_set_rate(mas->dev, sclk_freq);
140561de45fSGirish Mahadevan if (ret)
1411a9e489eSRajendra Nayak dev_err(mas->dev, "dev_pm_opp_set_rate failed %d\n", ret);
1425f219524SDouglas Anderson else
1435f219524SDouglas Anderson mas->cur_sclk_hz = sclk_freq;
1445f219524SDouglas Anderson
145561de45fSGirish Mahadevan return ret;
146561de45fSGirish Mahadevan }
147561de45fSGirish Mahadevan
handle_se_timeout(struct spi_controller * spi,struct spi_message * msg)1488726bdceSYang Yingliang static void handle_se_timeout(struct spi_controller *spi,
149de43affeSStephen Boyd struct spi_message *msg)
150de43affeSStephen Boyd {
1518726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
152539afdf9SDouglas Anderson unsigned long time_left;
153de43affeSStephen Boyd struct geni_se *se = &mas->se;
154e5f0dfa7SVijaya Krishna Nivarthi const struct spi_transfer *xfer;
155de43affeSStephen Boyd
156539afdf9SDouglas Anderson spin_lock_irq(&mas->lock);
157e5f0dfa7SVijaya Krishna Nivarthi if (mas->cur_xfer_mode == GENI_SE_FIFO)
158de43affeSStephen Boyd writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
159e5f0dfa7SVijaya Krishna Nivarthi
160e5f0dfa7SVijaya Krishna Nivarthi xfer = mas->cur_xfer;
1617ba9bdcbSDouglas Anderson mas->cur_xfer = NULL;
162d7f74cc3SPraveen Talari
1638726bdceSYang Yingliang if (spi->target) {
164d7f74cc3SPraveen Talari /*
1658726bdceSYang Yingliang * skip CMD Cancel sequnece since spi target
166d7f74cc3SPraveen Talari * doesn`t support CMD Cancel sequnece
167d7f74cc3SPraveen Talari */
168d7f74cc3SPraveen Talari spin_unlock_irq(&mas->lock);
1698a6b446dSVijaya Krishna Nivarthi goto reset_if_dma;
170d7f74cc3SPraveen Talari }
171d7f74cc3SPraveen Talari
172d7f74cc3SPraveen Talari reinit_completion(&mas->cancel_done);
1737ba9bdcbSDouglas Anderson geni_se_cancel_m_cmd(se);
174539afdf9SDouglas Anderson spin_unlock_irq(&mas->lock);
1757ba9bdcbSDouglas Anderson
1767ba9bdcbSDouglas Anderson time_left = wait_for_completion_timeout(&mas->cancel_done, HZ);
177de43affeSStephen Boyd if (time_left)
1788a6b446dSVijaya Krishna Nivarthi goto reset_if_dma;
179de43affeSStephen Boyd
180539afdf9SDouglas Anderson spin_lock_irq(&mas->lock);
1817ba9bdcbSDouglas Anderson reinit_completion(&mas->abort_done);
182de43affeSStephen Boyd geni_se_abort_m_cmd(se);
183539afdf9SDouglas Anderson spin_unlock_irq(&mas->lock);
1847ba9bdcbSDouglas Anderson
1857ba9bdcbSDouglas Anderson time_left = wait_for_completion_timeout(&mas->abort_done, HZ);
186690d8b91SDouglas Anderson if (!time_left) {
187de43affeSStephen Boyd dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
188690d8b91SDouglas Anderson
189690d8b91SDouglas Anderson /*
190690d8b91SDouglas Anderson * No need for a lock since SPI core has a lock and we never
191690d8b91SDouglas Anderson * access this from an interrupt.
192690d8b91SDouglas Anderson */
193690d8b91SDouglas Anderson mas->abort_failed = true;
194690d8b91SDouglas Anderson }
195e5f0dfa7SVijaya Krishna Nivarthi
1968a6b446dSVijaya Krishna Nivarthi reset_if_dma:
197e5f0dfa7SVijaya Krishna Nivarthi if (mas->cur_xfer_mode == GENI_SE_DMA) {
198e5f0dfa7SVijaya Krishna Nivarthi if (xfer) {
1993a76c7caSVijaya Krishna Nivarthi if (xfer->tx_buf) {
200e5f0dfa7SVijaya Krishna Nivarthi spin_lock_irq(&mas->lock);
201e5f0dfa7SVijaya Krishna Nivarthi reinit_completion(&mas->tx_reset_done);
202e5f0dfa7SVijaya Krishna Nivarthi writel(1, se->base + SE_DMA_TX_FSM_RST);
203e5f0dfa7SVijaya Krishna Nivarthi spin_unlock_irq(&mas->lock);
204e5f0dfa7SVijaya Krishna Nivarthi time_left = wait_for_completion_timeout(&mas->tx_reset_done, HZ);
205e5f0dfa7SVijaya Krishna Nivarthi if (!time_left)
206e5f0dfa7SVijaya Krishna Nivarthi dev_err(mas->dev, "DMA TX RESET failed\n");
207e5f0dfa7SVijaya Krishna Nivarthi }
2083a76c7caSVijaya Krishna Nivarthi if (xfer->rx_buf) {
209e5f0dfa7SVijaya Krishna Nivarthi spin_lock_irq(&mas->lock);
210e5f0dfa7SVijaya Krishna Nivarthi reinit_completion(&mas->rx_reset_done);
211e5f0dfa7SVijaya Krishna Nivarthi writel(1, se->base + SE_DMA_RX_FSM_RST);
212e5f0dfa7SVijaya Krishna Nivarthi spin_unlock_irq(&mas->lock);
213e5f0dfa7SVijaya Krishna Nivarthi time_left = wait_for_completion_timeout(&mas->rx_reset_done, HZ);
214e5f0dfa7SVijaya Krishna Nivarthi if (!time_left)
215e5f0dfa7SVijaya Krishna Nivarthi dev_err(mas->dev, "DMA RX RESET failed\n");
216e5f0dfa7SVijaya Krishna Nivarthi }
217e5f0dfa7SVijaya Krishna Nivarthi } else {
218e5f0dfa7SVijaya Krishna Nivarthi /*
219e5f0dfa7SVijaya Krishna Nivarthi * This can happen if a timeout happened and we had to wait
220e5f0dfa7SVijaya Krishna Nivarthi * for lock in this function because isr was holding the lock
221e5f0dfa7SVijaya Krishna Nivarthi * and handling transfer completion at that time.
222e5f0dfa7SVijaya Krishna Nivarthi */
223e5f0dfa7SVijaya Krishna Nivarthi dev_warn(mas->dev, "Cancel/Abort on completed SPI transfer\n");
224e5f0dfa7SVijaya Krishna Nivarthi }
225e5f0dfa7SVijaya Krishna Nivarthi }
226690d8b91SDouglas Anderson }
227690d8b91SDouglas Anderson
handle_gpi_timeout(struct spi_controller * spi,struct spi_message * msg)2288726bdceSYang Yingliang static void handle_gpi_timeout(struct spi_controller *spi, struct spi_message *msg)
229f8039ea5SVinod Koul {
2308726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
231f8039ea5SVinod Koul
232f8039ea5SVinod Koul dmaengine_terminate_sync(mas->tx);
233f8039ea5SVinod Koul dmaengine_terminate_sync(mas->rx);
234f8039ea5SVinod Koul }
235f8039ea5SVinod Koul
spi_geni_handle_err(struct spi_controller * spi,struct spi_message * msg)2368726bdceSYang Yingliang static void spi_geni_handle_err(struct spi_controller *spi, struct spi_message *msg)
237f8039ea5SVinod Koul {
2388726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
239f8039ea5SVinod Koul
240f8039ea5SVinod Koul switch (mas->cur_xfer_mode) {
241f8039ea5SVinod Koul case GENI_SE_FIFO:
242e5f0dfa7SVijaya Krishna Nivarthi case GENI_SE_DMA:
243e5f0dfa7SVijaya Krishna Nivarthi handle_se_timeout(spi, msg);
244f8039ea5SVinod Koul break;
245f8039ea5SVinod Koul case GENI_GPI_DMA:
246f8039ea5SVinod Koul handle_gpi_timeout(spi, msg);
247f8039ea5SVinod Koul break;
248f8039ea5SVinod Koul default:
249f8039ea5SVinod Koul dev_err(mas->dev, "Abort on Mode:%d not supported", mas->cur_xfer_mode);
250f8039ea5SVinod Koul }
251f8039ea5SVinod Koul }
252f8039ea5SVinod Koul
spi_geni_is_abort_still_pending(struct spi_geni_master * mas)253690d8b91SDouglas Anderson static bool spi_geni_is_abort_still_pending(struct spi_geni_master *mas)
254690d8b91SDouglas Anderson {
255690d8b91SDouglas Anderson struct geni_se *se = &mas->se;
256690d8b91SDouglas Anderson u32 m_irq, m_irq_en;
257690d8b91SDouglas Anderson
258690d8b91SDouglas Anderson if (!mas->abort_failed)
259690d8b91SDouglas Anderson return false;
260690d8b91SDouglas Anderson
261690d8b91SDouglas Anderson /*
262690d8b91SDouglas Anderson * The only known case where a transfer times out and then a cancel
263690d8b91SDouglas Anderson * times out then an abort times out is if something is blocking our
264690d8b91SDouglas Anderson * interrupt handler from running. Avoid starting any new transfers
265690d8b91SDouglas Anderson * until that sorts itself out.
266690d8b91SDouglas Anderson */
267690d8b91SDouglas Anderson spin_lock_irq(&mas->lock);
268690d8b91SDouglas Anderson m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
269690d8b91SDouglas Anderson m_irq_en = readl(se->base + SE_GENI_M_IRQ_EN);
270690d8b91SDouglas Anderson spin_unlock_irq(&mas->lock);
271690d8b91SDouglas Anderson
272690d8b91SDouglas Anderson if (m_irq & m_irq_en) {
273690d8b91SDouglas Anderson dev_err(mas->dev, "Interrupts pending after abort: %#010x\n",
274690d8b91SDouglas Anderson m_irq & m_irq_en);
275690d8b91SDouglas Anderson return true;
276690d8b91SDouglas Anderson }
277690d8b91SDouglas Anderson
278690d8b91SDouglas Anderson /*
279690d8b91SDouglas Anderson * If we're here the problem resolved itself so no need to check more
280690d8b91SDouglas Anderson * on future transfers.
281690d8b91SDouglas Anderson */
282690d8b91SDouglas Anderson mas->abort_failed = false;
283690d8b91SDouglas Anderson
284690d8b91SDouglas Anderson return false;
285de43affeSStephen Boyd }
286de43affeSStephen Boyd
spi_geni_set_cs(struct spi_device * slv,bool set_flag)287561de45fSGirish Mahadevan static void spi_geni_set_cs(struct spi_device *slv, bool set_flag)
288561de45fSGirish Mahadevan {
2898726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(slv->controller);
2908726bdceSYang Yingliang struct spi_controller *spi = dev_get_drvdata(mas->dev);
291561de45fSGirish Mahadevan struct geni_se *se = &mas->se;
2920dccff3cSAlok Chauhan unsigned long time_left;
293561de45fSGirish Mahadevan
294561de45fSGirish Mahadevan if (!(slv->mode & SPI_CS_HIGH))
295561de45fSGirish Mahadevan set_flag = !set_flag;
296561de45fSGirish Mahadevan
297638d8488SDouglas Anderson if (set_flag == mas->cs_flag)
298638d8488SDouglas Anderson return;
299638d8488SDouglas Anderson
300690d8b91SDouglas Anderson pm_runtime_get_sync(mas->dev);
301690d8b91SDouglas Anderson
302690d8b91SDouglas Anderson if (spi_geni_is_abort_still_pending(mas)) {
303690d8b91SDouglas Anderson dev_err(mas->dev, "Can't set chip select\n");
304690d8b91SDouglas Anderson goto exit;
305690d8b91SDouglas Anderson }
306690d8b91SDouglas Anderson
3072ee471a1SDouglas Anderson spin_lock_irq(&mas->lock);
3083d7d916fSDouglas Anderson if (mas->cur_xfer) {
3093d7d916fSDouglas Anderson dev_err(mas->dev, "Can't set CS when prev xfer running\n");
3103d7d916fSDouglas Anderson spin_unlock_irq(&mas->lock);
3113d7d916fSDouglas Anderson goto exit;
3123d7d916fSDouglas Anderson }
3133d7d916fSDouglas Anderson
3143d7d916fSDouglas Anderson mas->cs_flag = set_flag;
315e5f0dfa7SVijaya Krishna Nivarthi /* set xfer_mode to FIFO to complete cs_done in isr */
316e5f0dfa7SVijaya Krishna Nivarthi mas->cur_xfer_mode = GENI_SE_FIFO;
3174c329f5dSVijaya Krishna Nivarthi geni_se_select_mode(se, mas->cur_xfer_mode);
3184c329f5dSVijaya Krishna Nivarthi
3197ba9bdcbSDouglas Anderson reinit_completion(&mas->cs_done);
320561de45fSGirish Mahadevan if (set_flag)
321561de45fSGirish Mahadevan geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
322561de45fSGirish Mahadevan else
323561de45fSGirish Mahadevan geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
3242ee471a1SDouglas Anderson spin_unlock_irq(&mas->lock);
325561de45fSGirish Mahadevan
3267ba9bdcbSDouglas Anderson time_left = wait_for_completion_timeout(&mas->cs_done, HZ);
32717fa81aaSDouglas Anderson if (!time_left) {
32817fa81aaSDouglas Anderson dev_warn(mas->dev, "Timeout setting chip select\n");
329e5f0dfa7SVijaya Krishna Nivarthi handle_se_timeout(spi, NULL);
33017fa81aaSDouglas Anderson }
331561de45fSGirish Mahadevan
332690d8b91SDouglas Anderson exit:
333561de45fSGirish Mahadevan pm_runtime_put(mas->dev);
334561de45fSGirish Mahadevan }
335561de45fSGirish Mahadevan
spi_setup_word_len(struct spi_geni_master * mas,u16 mode,unsigned int bits_per_word)336561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
337561de45fSGirish Mahadevan unsigned int bits_per_word)
338561de45fSGirish Mahadevan {
339561de45fSGirish Mahadevan unsigned int pack_words;
340561de45fSGirish Mahadevan bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
341561de45fSGirish Mahadevan struct geni_se *se = &mas->se;
342561de45fSGirish Mahadevan u32 word_len;
343561de45fSGirish Mahadevan
344561de45fSGirish Mahadevan /*
345561de45fSGirish Mahadevan * If bits_per_word isn't a byte aligned value, set the packing to be
346561de45fSGirish Mahadevan * 1 SPI word per FIFO word.
347561de45fSGirish Mahadevan */
348561de45fSGirish Mahadevan if (!(mas->fifo_width_bits % bits_per_word))
349561de45fSGirish Mahadevan pack_words = mas->fifo_width_bits / bits_per_word;
350561de45fSGirish Mahadevan else
351561de45fSGirish Mahadevan pack_words = 1;
352561de45fSGirish Mahadevan geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
353561de45fSGirish Mahadevan true, true);
354da48dc8cSDouglas Anderson word_len = (bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK;
355561de45fSGirish Mahadevan writel(word_len, se->base + SE_SPI_WORD_LEN);
356561de45fSGirish Mahadevan }
357561de45fSGirish Mahadevan
geni_spi_set_clock_and_bw(struct spi_geni_master * mas,unsigned long clk_hz)3580e3b8a81SAkash Asthana static int geni_spi_set_clock_and_bw(struct spi_geni_master *mas,
3590e3b8a81SAkash Asthana unsigned long clk_hz)
360e68b6624SDouglas Anderson {
361e68b6624SDouglas Anderson u32 clk_sel, m_clk_cfg, idx, div;
362e68b6624SDouglas Anderson struct geni_se *se = &mas->se;
363e68b6624SDouglas Anderson int ret;
364e68b6624SDouglas Anderson
36568890e20SDouglas Anderson if (clk_hz == mas->cur_speed_hz)
36668890e20SDouglas Anderson return 0;
36768890e20SDouglas Anderson
368e68b6624SDouglas Anderson ret = get_spi_clk_cfg(clk_hz, mas, &idx, &div);
369e68b6624SDouglas Anderson if (ret) {
370e68b6624SDouglas Anderson dev_err(mas->dev, "Err setting clk to %lu: %d\n", clk_hz, ret);
371e68b6624SDouglas Anderson return ret;
372e68b6624SDouglas Anderson }
373e68b6624SDouglas Anderson
374e68b6624SDouglas Anderson /*
375e68b6624SDouglas Anderson * SPI core clock gets configured with the requested frequency
376e68b6624SDouglas Anderson * or the frequency closer to the requested frequency.
377e68b6624SDouglas Anderson * For that reason requested frequency is stored in the
378e68b6624SDouglas Anderson * cur_speed_hz and referred in the consecutive transfer instead
379e68b6624SDouglas Anderson * of calling clk_get_rate() API.
380e68b6624SDouglas Anderson */
381e68b6624SDouglas Anderson mas->cur_speed_hz = clk_hz;
382e68b6624SDouglas Anderson
383e68b6624SDouglas Anderson clk_sel = idx & CLK_SEL_MSK;
384e68b6624SDouglas Anderson m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
385e68b6624SDouglas Anderson writel(clk_sel, se->base + SE_GENI_CLK_SEL);
386e68b6624SDouglas Anderson writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
387e68b6624SDouglas Anderson
3880e3b8a81SAkash Asthana /* Set BW quota for CPU as driver supports FIFO mode only. */
3890e3b8a81SAkash Asthana se->icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(mas->cur_speed_hz);
3900e3b8a81SAkash Asthana ret = geni_icc_set_bw(se);
3910e3b8a81SAkash Asthana if (ret)
3920e3b8a81SAkash Asthana return ret;
3930e3b8a81SAkash Asthana
394e68b6624SDouglas Anderson return 0;
395e68b6624SDouglas Anderson }
396e68b6624SDouglas Anderson
setup_fifo_params(struct spi_device * spi_slv,struct spi_controller * spi)397561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
3988726bdceSYang Yingliang struct spi_controller *spi)
399561de45fSGirish Mahadevan {
4008726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
401561de45fSGirish Mahadevan struct geni_se *se = &mas->se;
402da48dc8cSDouglas Anderson u32 loopback_cfg = 0, cpol = 0, cpha = 0, demux_output_inv = 0;
403e68b6624SDouglas Anderson u32 demux_sel;
404561de45fSGirish Mahadevan
405da48dc8cSDouglas Anderson if (mas->last_mode != spi_slv->mode) {
406561de45fSGirish Mahadevan if (spi_slv->mode & SPI_LOOP)
407da48dc8cSDouglas Anderson loopback_cfg = LOOPBACK_ENABLE;
408561de45fSGirish Mahadevan
409561de45fSGirish Mahadevan if (spi_slv->mode & SPI_CPOL)
410da48dc8cSDouglas Anderson cpol = CPOL;
411561de45fSGirish Mahadevan
412561de45fSGirish Mahadevan if (spi_slv->mode & SPI_CPHA)
413da48dc8cSDouglas Anderson cpha = CPHA;
414561de45fSGirish Mahadevan
415561de45fSGirish Mahadevan if (spi_slv->mode & SPI_CS_HIGH)
4169e264f3fSAmit Kumar Mahapatra via Alsa-devel demux_output_inv = BIT(spi_get_chipselect(spi_slv, 0));
417561de45fSGirish Mahadevan
4189e264f3fSAmit Kumar Mahapatra via Alsa-devel demux_sel = spi_get_chipselect(spi_slv, 0);
419561de45fSGirish Mahadevan mas->cur_bits_per_word = spi_slv->bits_per_word;
420561de45fSGirish Mahadevan
421561de45fSGirish Mahadevan spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
422561de45fSGirish Mahadevan writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
423561de45fSGirish Mahadevan writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
424561de45fSGirish Mahadevan writel(cpha, se->base + SE_SPI_CPHA);
425561de45fSGirish Mahadevan writel(cpol, se->base + SE_SPI_CPOL);
426561de45fSGirish Mahadevan writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
427e68b6624SDouglas Anderson
428da48dc8cSDouglas Anderson mas->last_mode = spi_slv->mode;
429da48dc8cSDouglas Anderson }
430da48dc8cSDouglas Anderson
4310e3b8a81SAkash Asthana return geni_spi_set_clock_and_bw(mas, spi_slv->max_speed_hz);
432561de45fSGirish Mahadevan }
433561de45fSGirish Mahadevan
434b59c1224SVinod Koul static void
spi_gsi_callback_result(void * cb,const struct dmaengine_result * result)435b59c1224SVinod Koul spi_gsi_callback_result(void *cb, const struct dmaengine_result *result)
436b59c1224SVinod Koul {
4378726bdceSYang Yingliang struct spi_controller *spi = cb;
438b59c1224SVinod Koul
43974b86d6aSVinod Koul spi->cur_msg->status = -EIO;
440b59c1224SVinod Koul if (result->result != DMA_TRANS_NOERROR) {
441b59c1224SVinod Koul dev_err(&spi->dev, "DMA txn failed: %d\n", result->result);
44274b86d6aSVinod Koul spi_finalize_current_transfer(spi);
443b59c1224SVinod Koul return;
444b59c1224SVinod Koul }
445b59c1224SVinod Koul
446b59c1224SVinod Koul if (!result->residue) {
44774b86d6aSVinod Koul spi->cur_msg->status = 0;
448b59c1224SVinod Koul dev_dbg(&spi->dev, "DMA txn completed\n");
449b59c1224SVinod Koul } else {
450b59c1224SVinod Koul dev_err(&spi->dev, "DMA xfer has pending: %d\n", result->residue);
451b59c1224SVinod Koul }
45274b86d6aSVinod Koul
45374b86d6aSVinod Koul spi_finalize_current_transfer(spi);
454b59c1224SVinod Koul }
455b59c1224SVinod Koul
setup_gsi_xfer(struct spi_transfer * xfer,struct spi_geni_master * mas,struct spi_device * spi_slv,struct spi_controller * spi)456b59c1224SVinod Koul static int setup_gsi_xfer(struct spi_transfer *xfer, struct spi_geni_master *mas,
4578726bdceSYang Yingliang struct spi_device *spi_slv, struct spi_controller *spi)
458b59c1224SVinod Koul {
459b59c1224SVinod Koul unsigned long flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
460b59c1224SVinod Koul struct dma_slave_config config = {};
461b59c1224SVinod Koul struct gpi_spi_config peripheral = {};
462b59c1224SVinod Koul struct dma_async_tx_descriptor *tx_desc, *rx_desc;
463b59c1224SVinod Koul int ret;
464b59c1224SVinod Koul
465b59c1224SVinod Koul config.peripheral_config = &peripheral;
466b59c1224SVinod Koul config.peripheral_size = sizeof(peripheral);
467b59c1224SVinod Koul peripheral.set_config = true;
468b59c1224SVinod Koul
469b59c1224SVinod Koul if (xfer->bits_per_word != mas->cur_bits_per_word ||
470b59c1224SVinod Koul xfer->speed_hz != mas->cur_speed_hz) {
471b59c1224SVinod Koul mas->cur_bits_per_word = xfer->bits_per_word;
472b59c1224SVinod Koul mas->cur_speed_hz = xfer->speed_hz;
473b59c1224SVinod Koul }
474b59c1224SVinod Koul
475b59c1224SVinod Koul if (xfer->tx_buf && xfer->rx_buf) {
476b59c1224SVinod Koul peripheral.cmd = SPI_DUPLEX;
477b59c1224SVinod Koul } else if (xfer->tx_buf) {
478b59c1224SVinod Koul peripheral.cmd = SPI_TX;
479b59c1224SVinod Koul peripheral.rx_len = 0;
480b59c1224SVinod Koul } else if (xfer->rx_buf) {
481b59c1224SVinod Koul peripheral.cmd = SPI_RX;
482b59c1224SVinod Koul if (!(mas->cur_bits_per_word % MIN_WORD_LEN)) {
483b59c1224SVinod Koul peripheral.rx_len = ((xfer->len << 3) / mas->cur_bits_per_word);
484b59c1224SVinod Koul } else {
485b59c1224SVinod Koul int bytes_per_word = (mas->cur_bits_per_word / BITS_PER_BYTE) + 1;
486b59c1224SVinod Koul
487b59c1224SVinod Koul peripheral.rx_len = (xfer->len / bytes_per_word);
488b59c1224SVinod Koul }
489b59c1224SVinod Koul }
490b59c1224SVinod Koul
491b59c1224SVinod Koul peripheral.loopback_en = !!(spi_slv->mode & SPI_LOOP);
492b59c1224SVinod Koul peripheral.clock_pol_high = !!(spi_slv->mode & SPI_CPOL);
493b59c1224SVinod Koul peripheral.data_pol_high = !!(spi_slv->mode & SPI_CPHA);
4949e264f3fSAmit Kumar Mahapatra via Alsa-devel peripheral.cs = spi_get_chipselect(spi_slv, 0);
495b59c1224SVinod Koul peripheral.pack_en = true;
496b59c1224SVinod Koul peripheral.word_len = xfer->bits_per_word - MIN_WORD_LEN;
497b59c1224SVinod Koul
498b59c1224SVinod Koul ret = get_spi_clk_cfg(mas->cur_speed_hz, mas,
499b59c1224SVinod Koul &peripheral.clk_src, &peripheral.clk_div);
500b59c1224SVinod Koul if (ret) {
501b59c1224SVinod Koul dev_err(mas->dev, "Err in get_spi_clk_cfg() :%d\n", ret);
502b59c1224SVinod Koul return ret;
503b59c1224SVinod Koul }
504b59c1224SVinod Koul
505b59c1224SVinod Koul if (!xfer->cs_change) {
506b59c1224SVinod Koul if (!list_is_last(&xfer->transfer_list, &spi->cur_msg->transfers))
507b59c1224SVinod Koul peripheral.fragmentation = FRAGMENTATION;
508b59c1224SVinod Koul }
509b59c1224SVinod Koul
510b59c1224SVinod Koul if (peripheral.cmd & SPI_RX) {
511b59c1224SVinod Koul dmaengine_slave_config(mas->rx, &config);
512b59c1224SVinod Koul rx_desc = dmaengine_prep_slave_sg(mas->rx, xfer->rx_sg.sgl, xfer->rx_sg.nents,
513b59c1224SVinod Koul DMA_DEV_TO_MEM, flags);
514b59c1224SVinod Koul if (!rx_desc) {
515b59c1224SVinod Koul dev_err(mas->dev, "Err setting up rx desc\n");
516b59c1224SVinod Koul return -EIO;
517b59c1224SVinod Koul }
518b59c1224SVinod Koul }
519b59c1224SVinod Koul
520b59c1224SVinod Koul /*
521b59c1224SVinod Koul * Prepare the TX always, even for RX or tx_buf being null, we would
522b59c1224SVinod Koul * need TX to be prepared per GSI spec
523b59c1224SVinod Koul */
524b59c1224SVinod Koul dmaengine_slave_config(mas->tx, &config);
525b59c1224SVinod Koul tx_desc = dmaengine_prep_slave_sg(mas->tx, xfer->tx_sg.sgl, xfer->tx_sg.nents,
526b59c1224SVinod Koul DMA_MEM_TO_DEV, flags);
527b59c1224SVinod Koul if (!tx_desc) {
528b59c1224SVinod Koul dev_err(mas->dev, "Err setting up tx desc\n");
529b59c1224SVinod Koul return -EIO;
530b59c1224SVinod Koul }
531b59c1224SVinod Koul
532b59c1224SVinod Koul tx_desc->callback_result = spi_gsi_callback_result;
533b59c1224SVinod Koul tx_desc->callback_param = spi;
534b59c1224SVinod Koul
535b59c1224SVinod Koul if (peripheral.cmd & SPI_RX)
536b59c1224SVinod Koul dmaengine_submit(rx_desc);
537b59c1224SVinod Koul dmaengine_submit(tx_desc);
538b59c1224SVinod Koul
539b59c1224SVinod Koul if (peripheral.cmd & SPI_RX)
540b59c1224SVinod Koul dma_async_issue_pending(mas->rx);
541b59c1224SVinod Koul
542b59c1224SVinod Koul dma_async_issue_pending(mas->tx);
543b59c1224SVinod Koul return 1;
544b59c1224SVinod Koul }
545b59c1224SVinod Koul
get_xfer_len_in_words(struct spi_transfer * xfer,struct spi_geni_master * mas)5463a76c7caSVijaya Krishna Nivarthi static u32 get_xfer_len_in_words(struct spi_transfer *xfer,
5473a76c7caSVijaya Krishna Nivarthi struct spi_geni_master *mas)
5483a76c7caSVijaya Krishna Nivarthi {
5493a76c7caSVijaya Krishna Nivarthi u32 len;
5503a76c7caSVijaya Krishna Nivarthi
5513a76c7caSVijaya Krishna Nivarthi if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
5523a76c7caSVijaya Krishna Nivarthi len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
5533a76c7caSVijaya Krishna Nivarthi else
5543a76c7caSVijaya Krishna Nivarthi len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
5553a76c7caSVijaya Krishna Nivarthi len &= TRANS_LEN_MSK;
5563a76c7caSVijaya Krishna Nivarthi
5573a76c7caSVijaya Krishna Nivarthi return len;
5583a76c7caSVijaya Krishna Nivarthi }
5593a76c7caSVijaya Krishna Nivarthi
geni_can_dma(struct spi_controller * ctlr,struct spi_device * slv,struct spi_transfer * xfer)560b59c1224SVinod Koul static bool geni_can_dma(struct spi_controller *ctlr,
561b59c1224SVinod Koul struct spi_device *slv, struct spi_transfer *xfer)
562b59c1224SVinod Koul {
5638726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(slv->controller);
5643a76c7caSVijaya Krishna Nivarthi u32 len, fifo_size;
565b59c1224SVinod Koul
5663a76c7caSVijaya Krishna Nivarthi if (mas->cur_xfer_mode == GENI_GPI_DMA)
5673a76c7caSVijaya Krishna Nivarthi return true;
5683a76c7caSVijaya Krishna Nivarthi
5698726bdceSYang Yingliang /* Set SE DMA mode for SPI target. */
5708726bdceSYang Yingliang if (ctlr->target)
571d7f74cc3SPraveen Talari return true;
572d7f74cc3SPraveen Talari
5733a76c7caSVijaya Krishna Nivarthi len = get_xfer_len_in_words(xfer, mas);
5743a76c7caSVijaya Krishna Nivarthi fifo_size = mas->tx_fifo_depth * mas->fifo_width_bits / mas->cur_bits_per_word;
5753a76c7caSVijaya Krishna Nivarthi
5763a76c7caSVijaya Krishna Nivarthi if (len > fifo_size)
5773a76c7caSVijaya Krishna Nivarthi return true;
5783a76c7caSVijaya Krishna Nivarthi else
5793a76c7caSVijaya Krishna Nivarthi return false;
580b59c1224SVinod Koul }
581b59c1224SVinod Koul
spi_geni_prepare_message(struct spi_controller * spi,struct spi_message * spi_msg)5828726bdceSYang Yingliang static int spi_geni_prepare_message(struct spi_controller *spi,
583561de45fSGirish Mahadevan struct spi_message *spi_msg)
584561de45fSGirish Mahadevan {
5858726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
586b59c1224SVinod Koul int ret;
587561de45fSGirish Mahadevan
588b59c1224SVinod Koul switch (mas->cur_xfer_mode) {
589b59c1224SVinod Koul case GENI_SE_FIFO:
590e5f0dfa7SVijaya Krishna Nivarthi case GENI_SE_DMA:
591690d8b91SDouglas Anderson if (spi_geni_is_abort_still_pending(mas))
592690d8b91SDouglas Anderson return -EBUSY;
593561de45fSGirish Mahadevan ret = setup_fifo_params(spi_msg->spi, spi);
594561de45fSGirish Mahadevan if (ret)
595561de45fSGirish Mahadevan dev_err(mas->dev, "Couldn't select mode %d\n", ret);
596561de45fSGirish Mahadevan return ret;
597b59c1224SVinod Koul
598b59c1224SVinod Koul case GENI_GPI_DMA:
599b59c1224SVinod Koul /* nothing to do for GPI DMA */
600b59c1224SVinod Koul return 0;
601b59c1224SVinod Koul }
602b59c1224SVinod Koul
603b59c1224SVinod Koul dev_err(mas->dev, "Mode not supported %d", mas->cur_xfer_mode);
604b59c1224SVinod Koul return -EINVAL;
605b59c1224SVinod Koul }
606b59c1224SVinod Koul
spi_geni_release_dma_chan(void * data)60707f1eb71SJinjie Ruan static void spi_geni_release_dma_chan(void *data)
60807f1eb71SJinjie Ruan {
60907f1eb71SJinjie Ruan struct spi_geni_master *mas = data;
61007f1eb71SJinjie Ruan
61107f1eb71SJinjie Ruan if (mas->rx) {
61207f1eb71SJinjie Ruan dma_release_channel(mas->rx);
61307f1eb71SJinjie Ruan mas->rx = NULL;
61407f1eb71SJinjie Ruan }
61507f1eb71SJinjie Ruan
61607f1eb71SJinjie Ruan if (mas->tx) {
61707f1eb71SJinjie Ruan dma_release_channel(mas->tx);
61807f1eb71SJinjie Ruan mas->tx = NULL;
61907f1eb71SJinjie Ruan }
62007f1eb71SJinjie Ruan }
62107f1eb71SJinjie Ruan
spi_geni_grab_gpi_chan(struct spi_geni_master * mas)622b59c1224SVinod Koul static int spi_geni_grab_gpi_chan(struct spi_geni_master *mas)
623b59c1224SVinod Koul {
624b59c1224SVinod Koul int ret;
625b59c1224SVinod Koul
626b59c1224SVinod Koul mas->tx = dma_request_chan(mas->dev, "tx");
6276532582cSDan Carpenter if (IS_ERR(mas->tx)) {
6286532582cSDan Carpenter ret = dev_err_probe(mas->dev, PTR_ERR(mas->tx),
6296532582cSDan Carpenter "Failed to get tx DMA ch\n");
630b59c1224SVinod Koul goto err_tx;
6316532582cSDan Carpenter }
632b59c1224SVinod Koul
633b59c1224SVinod Koul mas->rx = dma_request_chan(mas->dev, "rx");
6346532582cSDan Carpenter if (IS_ERR(mas->rx)) {
6356532582cSDan Carpenter ret = dev_err_probe(mas->dev, PTR_ERR(mas->rx),
6366532582cSDan Carpenter "Failed to get rx DMA ch\n");
637b59c1224SVinod Koul goto err_rx;
6386532582cSDan Carpenter }
639b59c1224SVinod Koul
64007f1eb71SJinjie Ruan ret = devm_add_action_or_reset(mas->dev, spi_geni_release_dma_chan, mas);
64107f1eb71SJinjie Ruan if (ret) {
64207f1eb71SJinjie Ruan dev_err(mas->dev, "Unable to add action.\n");
64307f1eb71SJinjie Ruan return ret;
64407f1eb71SJinjie Ruan }
64507f1eb71SJinjie Ruan
646b59c1224SVinod Koul return 0;
647b59c1224SVinod Koul
648b59c1224SVinod Koul err_rx:
649b59c1224SVinod Koul mas->rx = NULL;
6506532582cSDan Carpenter dma_release_channel(mas->tx);
6516532582cSDan Carpenter err_tx:
6526532582cSDan Carpenter mas->tx = NULL;
653b59c1224SVinod Koul return ret;
654b59c1224SVinod Koul }
655b59c1224SVinod Koul
spi_geni_init(struct spi_geni_master * mas)656561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
657561de45fSGirish Mahadevan {
65814cea923SUwe Kleine-König struct spi_controller *spi = dev_get_drvdata(mas->dev);
659561de45fSGirish Mahadevan struct geni_se *se = &mas->se;
660561de45fSGirish Mahadevan unsigned int proto, major, minor, ver;
661b59c1224SVinod Koul u32 spi_tx_cfg, fifo_disable;
662b59c1224SVinod Koul int ret = -ENXIO;
663561de45fSGirish Mahadevan
664561de45fSGirish Mahadevan pm_runtime_get_sync(mas->dev);
665561de45fSGirish Mahadevan
666561de45fSGirish Mahadevan proto = geni_se_read_proto(se);
667d7f74cc3SPraveen Talari
6688726bdceSYang Yingliang if (spi->target) {
669d7f74cc3SPraveen Talari if (proto != GENI_SE_SPI_SLAVE) {
670d7f74cc3SPraveen Talari dev_err(mas->dev, "Invalid proto %d\n", proto);
671d7f74cc3SPraveen Talari goto out_pm;
672d7f74cc3SPraveen Talari }
673d7f74cc3SPraveen Talari spi_slv_setup(mas);
674d7f74cc3SPraveen Talari } else if (proto != GENI_SE_SPI) {
675561de45fSGirish Mahadevan dev_err(mas->dev, "Invalid proto %d\n", proto);
676b59c1224SVinod Koul goto out_pm;
677561de45fSGirish Mahadevan }
678561de45fSGirish Mahadevan mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
679561de45fSGirish Mahadevan
680561de45fSGirish Mahadevan /* Width of Tx and Rx FIFO is same */
681561de45fSGirish Mahadevan mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
682561de45fSGirish Mahadevan
683561de45fSGirish Mahadevan /*
684561de45fSGirish Mahadevan * Hardware programming guide suggests to configure
685561de45fSGirish Mahadevan * RX FIFO RFR level to fifo_depth-2.
686561de45fSGirish Mahadevan */
687fc129a43SDouglas Anderson geni_se_init(se, mas->tx_fifo_depth - 3, mas->tx_fifo_depth - 2);
688561de45fSGirish Mahadevan /* Transmit an entire FIFO worth of data per IRQ */
689561de45fSGirish Mahadevan mas->tx_wm = 1;
690561de45fSGirish Mahadevan ver = geni_se_get_qup_hw_version(se);
691561de45fSGirish Mahadevan major = GENI_SE_VERSION_MAJOR(ver);
692561de45fSGirish Mahadevan minor = GENI_SE_VERSION_MINOR(ver);
693561de45fSGirish Mahadevan
694561de45fSGirish Mahadevan if (major == 1 && minor == 0)
695561de45fSGirish Mahadevan mas->oversampling = 2;
696561de45fSGirish Mahadevan else
697561de45fSGirish Mahadevan mas->oversampling = 1;
698561de45fSGirish Mahadevan
699b59c1224SVinod Koul fifo_disable = readl(se->base + GENI_IF_DISABLE_RO) & FIFO_IF_DISABLE;
700b59c1224SVinod Koul switch (fifo_disable) {
701b59c1224SVinod Koul case 1:
702b59c1224SVinod Koul ret = spi_geni_grab_gpi_chan(mas);
703b59c1224SVinod Koul if (!ret) { /* success case */
704b59c1224SVinod Koul mas->cur_xfer_mode = GENI_GPI_DMA;
705b59c1224SVinod Koul geni_se_select_mode(se, GENI_GPI_DMA);
706b59c1224SVinod Koul dev_dbg(mas->dev, "Using GPI DMA mode for SPI\n");
707b59c1224SVinod Koul break;
7089d7054fbSNeil Armstrong } else if (ret == -EPROBE_DEFER) {
7099d7054fbSNeil Armstrong goto out_pm;
710b59c1224SVinod Koul }
711b59c1224SVinod Koul /*
712e5f0dfa7SVijaya Krishna Nivarthi * in case of failure to get gpi dma channel, we can still do the
713b59c1224SVinod Koul * FIFO mode, so fallthrough
714b59c1224SVinod Koul */
715b59c1224SVinod Koul dev_warn(mas->dev, "FIFO mode disabled, but couldn't get DMA, fall back to FIFO mode\n");
716b59c1224SVinod Koul fallthrough;
717b59c1224SVinod Koul
718b59c1224SVinod Koul case 0:
719b59c1224SVinod Koul mas->cur_xfer_mode = GENI_SE_FIFO;
720da48dc8cSDouglas Anderson geni_se_select_mode(se, GENI_SE_FIFO);
721b59c1224SVinod Koul ret = 0;
722b59c1224SVinod Koul break;
723b59c1224SVinod Koul }
724da48dc8cSDouglas Anderson
72514ac4e04SDouglas Anderson /* We always control CS manually */
7268726bdceSYang Yingliang if (!spi->target) {
72714ac4e04SDouglas Anderson spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
72814ac4e04SDouglas Anderson spi_tx_cfg &= ~CS_TOGGLE;
72914ac4e04SDouglas Anderson writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
730d7f74cc3SPraveen Talari }
73114ac4e04SDouglas Anderson
732b59c1224SVinod Koul out_pm:
733561de45fSGirish Mahadevan pm_runtime_put(mas->dev);
734b59c1224SVinod Koul return ret;
735561de45fSGirish Mahadevan }
736561de45fSGirish Mahadevan
geni_byte_per_fifo_word(struct spi_geni_master * mas)7376d66507dSDouglas Anderson static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
7386d66507dSDouglas Anderson {
7396d66507dSDouglas Anderson /*
7406d66507dSDouglas Anderson * Calculate how many bytes we'll put in each FIFO word. If the
7416d66507dSDouglas Anderson * transfer words don't pack cleanly into a FIFO word we'll just put
7426d66507dSDouglas Anderson * one transfer word in each FIFO word. If they do pack we'll pack 'em.
7436d66507dSDouglas Anderson */
7446d66507dSDouglas Anderson if (mas->fifo_width_bits % mas->cur_bits_per_word)
7456d66507dSDouglas Anderson return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
7466d66507dSDouglas Anderson BITS_PER_BYTE));
7476d66507dSDouglas Anderson
7486d66507dSDouglas Anderson return mas->fifo_width_bits / BITS_PER_BYTE;
7496d66507dSDouglas Anderson }
7506d66507dSDouglas Anderson
geni_spi_handle_tx(struct spi_geni_master * mas)7516d66507dSDouglas Anderson static bool geni_spi_handle_tx(struct spi_geni_master *mas)
7526d66507dSDouglas Anderson {
7536d66507dSDouglas Anderson struct geni_se *se = &mas->se;
7546d66507dSDouglas Anderson unsigned int max_bytes;
7556d66507dSDouglas Anderson const u8 *tx_buf;
7566d66507dSDouglas Anderson unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
7576d66507dSDouglas Anderson unsigned int i = 0;
7586d66507dSDouglas Anderson
7594aa1464aSDouglas Anderson /* Stop the watermark IRQ if nothing to send */
7604aa1464aSDouglas Anderson if (!mas->cur_xfer) {
7614aa1464aSDouglas Anderson writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
7624aa1464aSDouglas Anderson return false;
7634aa1464aSDouglas Anderson }
7644aa1464aSDouglas Anderson
7656d66507dSDouglas Anderson max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
7666d66507dSDouglas Anderson if (mas->tx_rem_bytes < max_bytes)
7676d66507dSDouglas Anderson max_bytes = mas->tx_rem_bytes;
7686d66507dSDouglas Anderson
7696d66507dSDouglas Anderson tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
7706d66507dSDouglas Anderson while (i < max_bytes) {
7716d66507dSDouglas Anderson unsigned int j;
7726d66507dSDouglas Anderson unsigned int bytes_to_write;
7736d66507dSDouglas Anderson u32 fifo_word = 0;
7746d66507dSDouglas Anderson u8 *fifo_byte = (u8 *)&fifo_word;
7756d66507dSDouglas Anderson
7766d66507dSDouglas Anderson bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
7776d66507dSDouglas Anderson for (j = 0; j < bytes_to_write; j++)
7786d66507dSDouglas Anderson fifo_byte[j] = tx_buf[i++];
7796d66507dSDouglas Anderson iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
7806d66507dSDouglas Anderson }
7816d66507dSDouglas Anderson mas->tx_rem_bytes -= max_bytes;
7826d66507dSDouglas Anderson if (!mas->tx_rem_bytes) {
7836d66507dSDouglas Anderson writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
7846d66507dSDouglas Anderson return false;
7856d66507dSDouglas Anderson }
7866d66507dSDouglas Anderson return true;
7876d66507dSDouglas Anderson }
7886d66507dSDouglas Anderson
geni_spi_handle_rx(struct spi_geni_master * mas)7896d66507dSDouglas Anderson static void geni_spi_handle_rx(struct spi_geni_master *mas)
7906d66507dSDouglas Anderson {
7916d66507dSDouglas Anderson struct geni_se *se = &mas->se;
7926d66507dSDouglas Anderson u32 rx_fifo_status;
7936d66507dSDouglas Anderson unsigned int rx_bytes;
7946d66507dSDouglas Anderson unsigned int rx_last_byte_valid;
7956d66507dSDouglas Anderson u8 *rx_buf;
7966d66507dSDouglas Anderson unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
7976d66507dSDouglas Anderson unsigned int i = 0;
7986d66507dSDouglas Anderson
7996d66507dSDouglas Anderson rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
8006d66507dSDouglas Anderson rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
8016d66507dSDouglas Anderson if (rx_fifo_status & RX_LAST) {
8026d66507dSDouglas Anderson rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
8036d66507dSDouglas Anderson rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
8046d66507dSDouglas Anderson if (rx_last_byte_valid && rx_last_byte_valid < 4)
8056d66507dSDouglas Anderson rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
8066d66507dSDouglas Anderson }
8074aa1464aSDouglas Anderson
8084aa1464aSDouglas Anderson /* Clear out the FIFO and bail if nowhere to put it */
8094aa1464aSDouglas Anderson if (!mas->cur_xfer) {
8104aa1464aSDouglas Anderson for (i = 0; i < DIV_ROUND_UP(rx_bytes, bytes_per_fifo_word); i++)
8114aa1464aSDouglas Anderson readl(se->base + SE_GENI_RX_FIFOn);
8124aa1464aSDouglas Anderson return;
8134aa1464aSDouglas Anderson }
8144aa1464aSDouglas Anderson
8156d66507dSDouglas Anderson if (mas->rx_rem_bytes < rx_bytes)
8166d66507dSDouglas Anderson rx_bytes = mas->rx_rem_bytes;
8176d66507dSDouglas Anderson
8186d66507dSDouglas Anderson rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
8196d66507dSDouglas Anderson while (i < rx_bytes) {
8206d66507dSDouglas Anderson u32 fifo_word = 0;
8216d66507dSDouglas Anderson u8 *fifo_byte = (u8 *)&fifo_word;
8226d66507dSDouglas Anderson unsigned int bytes_to_read;
8236d66507dSDouglas Anderson unsigned int j;
8246d66507dSDouglas Anderson
8256d66507dSDouglas Anderson bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
8266d66507dSDouglas Anderson ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
8276d66507dSDouglas Anderson for (j = 0; j < bytes_to_read; j++)
8286d66507dSDouglas Anderson rx_buf[i++] = fifo_byte[j];
8296d66507dSDouglas Anderson }
8306d66507dSDouglas Anderson mas->rx_rem_bytes -= rx_bytes;
8316d66507dSDouglas Anderson }
8326d66507dSDouglas Anderson
setup_se_xfer(struct spi_transfer * xfer,struct spi_geni_master * mas,u16 mode,struct spi_controller * spi)833e5f0dfa7SVijaya Krishna Nivarthi static int setup_se_xfer(struct spi_transfer *xfer,
834561de45fSGirish Mahadevan struct spi_geni_master *mas,
8358726bdceSYang Yingliang u16 mode, struct spi_controller *spi)
836561de45fSGirish Mahadevan {
837561de45fSGirish Mahadevan u32 m_cmd = 0;
8383a76c7caSVijaya Krishna Nivarthi u32 len;
839561de45fSGirish Mahadevan struct geni_se *se = &mas->se;
840e68b6624SDouglas Anderson int ret;
841561de45fSGirish Mahadevan
8422ee471a1SDouglas Anderson /*
8432ee471a1SDouglas Anderson * Ensure that our interrupt handler isn't still running from some
8442ee471a1SDouglas Anderson * prior command before we start messing with the hardware behind
8452ee471a1SDouglas Anderson * its back. We don't need to _keep_ the lock here since we're only
8462ee471a1SDouglas Anderson * worried about racing with out interrupt handler. The SPI core
8472ee471a1SDouglas Anderson * already handles making sure that we're not trying to do two
8482ee471a1SDouglas Anderson * transfers at once or setting a chip select and doing a transfer
8492ee471a1SDouglas Anderson * concurrently.
8502ee471a1SDouglas Anderson *
8512ee471a1SDouglas Anderson * NOTE: we actually _can't_ hold the lock here because possibly we
8522ee471a1SDouglas Anderson * might call clk_set_rate() which needs to be able to sleep.
8532ee471a1SDouglas Anderson */
8542ee471a1SDouglas Anderson spin_lock_irq(&mas->lock);
8552ee471a1SDouglas Anderson spin_unlock_irq(&mas->lock);
8562ee471a1SDouglas Anderson
857561de45fSGirish Mahadevan if (xfer->bits_per_word != mas->cur_bits_per_word) {
858561de45fSGirish Mahadevan spi_setup_word_len(mas, mode, xfer->bits_per_word);
859561de45fSGirish Mahadevan mas->cur_bits_per_word = xfer->bits_per_word;
860561de45fSGirish Mahadevan }
861561de45fSGirish Mahadevan
862561de45fSGirish Mahadevan /* Speed and bits per word can be overridden per transfer */
8630e3b8a81SAkash Asthana ret = geni_spi_set_clock_and_bw(mas, xfer->speed_hz);
864e68b6624SDouglas Anderson if (ret)
865e5f0dfa7SVijaya Krishna Nivarthi return ret;
866561de45fSGirish Mahadevan
867561de45fSGirish Mahadevan mas->tx_rem_bytes = 0;
868561de45fSGirish Mahadevan mas->rx_rem_bytes = 0;
869561de45fSGirish Mahadevan
8703a76c7caSVijaya Krishna Nivarthi len = get_xfer_len_in_words(xfer, mas);
871561de45fSGirish Mahadevan
872561de45fSGirish Mahadevan mas->cur_xfer = xfer;
87319ea3275SStephen Boyd if (xfer->tx_buf) {
87419ea3275SStephen Boyd m_cmd |= SPI_TX_ONLY;
875561de45fSGirish Mahadevan mas->tx_rem_bytes = xfer->len;
876561de45fSGirish Mahadevan writel(len, se->base + SE_SPI_TX_TRANS_LEN);
877561de45fSGirish Mahadevan }
878561de45fSGirish Mahadevan
87919ea3275SStephen Boyd if (xfer->rx_buf) {
88019ea3275SStephen Boyd m_cmd |= SPI_RX_ONLY;
881561de45fSGirish Mahadevan writel(len, se->base + SE_SPI_RX_TRANS_LEN);
882561de45fSGirish Mahadevan mas->rx_rem_bytes = xfer->len;
883561de45fSGirish Mahadevan }
8842ee471a1SDouglas Anderson
8853a76c7caSVijaya Krishna Nivarthi /*
8863a76c7caSVijaya Krishna Nivarthi * Select DMA mode if sgt are present; and with only 1 entry
8873a76c7caSVijaya Krishna Nivarthi * This is not a serious limitation because the xfer buffers are
8883a76c7caSVijaya Krishna Nivarthi * expected to fit into in 1 entry almost always, and if any
8893a76c7caSVijaya Krishna Nivarthi * doesn't for any reason we fall back to FIFO mode anyway
8903a76c7caSVijaya Krishna Nivarthi */
8913a76c7caSVijaya Krishna Nivarthi if (!xfer->tx_sg.nents && !xfer->rx_sg.nents)
8923a76c7caSVijaya Krishna Nivarthi mas->cur_xfer_mode = GENI_SE_FIFO;
8933a76c7caSVijaya Krishna Nivarthi else if (xfer->tx_sg.nents > 1 || xfer->rx_sg.nents > 1) {
8943a76c7caSVijaya Krishna Nivarthi dev_warn_once(mas->dev, "Doing FIFO, cannot handle tx_nents-%d, rx_nents-%d\n",
8953a76c7caSVijaya Krishna Nivarthi xfer->tx_sg.nents, xfer->rx_sg.nents);
8963a76c7caSVijaya Krishna Nivarthi mas->cur_xfer_mode = GENI_SE_FIFO;
8973a76c7caSVijaya Krishna Nivarthi } else
8983a76c7caSVijaya Krishna Nivarthi mas->cur_xfer_mode = GENI_SE_DMA;
899e5f0dfa7SVijaya Krishna Nivarthi geni_se_select_mode(se, mas->cur_xfer_mode);
900e5f0dfa7SVijaya Krishna Nivarthi
9012ee471a1SDouglas Anderson /*
9022ee471a1SDouglas Anderson * Lock around right before we start the transfer since our
9032ee471a1SDouglas Anderson * interrupt could come in at any time now.
9042ee471a1SDouglas Anderson */
9052ee471a1SDouglas Anderson spin_lock_irq(&mas->lock);
906561de45fSGirish Mahadevan geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
907e5f0dfa7SVijaya Krishna Nivarthi
908e5f0dfa7SVijaya Krishna Nivarthi if (mas->cur_xfer_mode == GENI_SE_DMA) {
9093a76c7caSVijaya Krishna Nivarthi if (m_cmd & SPI_RX_ONLY)
9103a76c7caSVijaya Krishna Nivarthi geni_se_rx_init_dma(se, sg_dma_address(xfer->rx_sg.sgl),
9113a76c7caSVijaya Krishna Nivarthi sg_dma_len(xfer->rx_sg.sgl));
9123a76c7caSVijaya Krishna Nivarthi if (m_cmd & SPI_TX_ONLY)
9133a76c7caSVijaya Krishna Nivarthi geni_se_tx_init_dma(se, sg_dma_address(xfer->tx_sg.sgl),
9143a76c7caSVijaya Krishna Nivarthi sg_dma_len(xfer->tx_sg.sgl));
915e5f0dfa7SVijaya Krishna Nivarthi } else if (m_cmd & SPI_TX_ONLY) {
9166d66507dSDouglas Anderson if (geni_spi_handle_tx(mas))
917561de45fSGirish Mahadevan writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
9186d66507dSDouglas Anderson }
919e5f0dfa7SVijaya Krishna Nivarthi
9202ee471a1SDouglas Anderson spin_unlock_irq(&mas->lock);
921e5f0dfa7SVijaya Krishna Nivarthi return ret;
922561de45fSGirish Mahadevan }
923561de45fSGirish Mahadevan
spi_geni_transfer_one(struct spi_controller * spi,struct spi_device * slv,struct spi_transfer * xfer)9248726bdceSYang Yingliang static int spi_geni_transfer_one(struct spi_controller *spi,
925561de45fSGirish Mahadevan struct spi_device *slv,
926561de45fSGirish Mahadevan struct spi_transfer *xfer)
927561de45fSGirish Mahadevan {
9288726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
929e5f0dfa7SVijaya Krishna Nivarthi int ret;
930561de45fSGirish Mahadevan
931690d8b91SDouglas Anderson if (spi_geni_is_abort_still_pending(mas))
932690d8b91SDouglas Anderson return -EBUSY;
933690d8b91SDouglas Anderson
934561de45fSGirish Mahadevan /* Terminate and return success for 0 byte length transfer */
935561de45fSGirish Mahadevan if (!xfer->len)
936561de45fSGirish Mahadevan return 0;
937561de45fSGirish Mahadevan
938e5f0dfa7SVijaya Krishna Nivarthi if (mas->cur_xfer_mode == GENI_SE_FIFO || mas->cur_xfer_mode == GENI_SE_DMA) {
939e5f0dfa7SVijaya Krishna Nivarthi ret = setup_se_xfer(xfer, mas, slv->mode, spi);
940e5f0dfa7SVijaya Krishna Nivarthi /* SPI framework expects +ve ret code to wait for transfer complete */
941e5f0dfa7SVijaya Krishna Nivarthi if (!ret)
942e5f0dfa7SVijaya Krishna Nivarthi ret = 1;
943e5f0dfa7SVijaya Krishna Nivarthi return ret;
944561de45fSGirish Mahadevan }
945b59c1224SVinod Koul return setup_gsi_xfer(xfer, mas, slv, spi);
946b59c1224SVinod Koul }
947561de45fSGirish Mahadevan
geni_spi_isr(int irq,void * data)948561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
949561de45fSGirish Mahadevan {
9508726bdceSYang Yingliang struct spi_controller *spi = data;
9518726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
952561de45fSGirish Mahadevan struct geni_se *se = &mas->se;
953561de45fSGirish Mahadevan u32 m_irq;
954561de45fSGirish Mahadevan
9552ee471a1SDouglas Anderson m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
9562ee471a1SDouglas Anderson if (!m_irq)
957561de45fSGirish Mahadevan return IRQ_NONE;
958561de45fSGirish Mahadevan
959e191a082SDouglas Anderson if (m_irq & (M_CMD_OVERRUN_EN | M_ILLEGAL_CMD_EN | M_CMD_FAILURE_EN |
960e191a082SDouglas Anderson M_RX_FIFO_RD_ERR_EN | M_RX_FIFO_WR_ERR_EN |
961e191a082SDouglas Anderson M_TX_FIFO_RD_ERR_EN | M_TX_FIFO_WR_ERR_EN))
962e191a082SDouglas Anderson dev_warn(mas->dev, "Unexpected IRQ err status %#010x\n", m_irq);
963e191a082SDouglas Anderson
964539afdf9SDouglas Anderson spin_lock(&mas->lock);
965561de45fSGirish Mahadevan
966e5f0dfa7SVijaya Krishna Nivarthi if (mas->cur_xfer_mode == GENI_SE_FIFO) {
967561de45fSGirish Mahadevan if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
968561de45fSGirish Mahadevan geni_spi_handle_rx(mas);
969561de45fSGirish Mahadevan
970561de45fSGirish Mahadevan if (m_irq & M_TX_FIFO_WATERMARK_EN)
971561de45fSGirish Mahadevan geni_spi_handle_tx(mas);
972561de45fSGirish Mahadevan
973561de45fSGirish Mahadevan if (m_irq & M_CMD_DONE_EN) {
9747ba9bdcbSDouglas Anderson if (mas->cur_xfer) {
975561de45fSGirish Mahadevan spi_finalize_current_transfer(spi);
9767ba9bdcbSDouglas Anderson mas->cur_xfer = NULL;
977561de45fSGirish Mahadevan /*
97859ab0fa0SStephen Boyd * If this happens, then a CMD_DONE came before all the
97959ab0fa0SStephen Boyd * Tx buffer bytes were sent out. This is unusual, log
98059ab0fa0SStephen Boyd * this condition and disable the WM interrupt to
98159ab0fa0SStephen Boyd * prevent the system from stalling due an interrupt
98259ab0fa0SStephen Boyd * storm.
98359ab0fa0SStephen Boyd *
98459ab0fa0SStephen Boyd * If this happens when all Rx bytes haven't been
98559ab0fa0SStephen Boyd * received, log the condition. The only known time
98659ab0fa0SStephen Boyd * this can happen is if bits_per_word != 8 and some
98759ab0fa0SStephen Boyd * registers that expect xfer lengths in num spi_words
988561de45fSGirish Mahadevan * weren't written correctly.
989561de45fSGirish Mahadevan */
990561de45fSGirish Mahadevan if (mas->tx_rem_bytes) {
991561de45fSGirish Mahadevan writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
992561de45fSGirish Mahadevan dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
993561de45fSGirish Mahadevan mas->tx_rem_bytes, mas->cur_bits_per_word);
994561de45fSGirish Mahadevan }
995561de45fSGirish Mahadevan if (mas->rx_rem_bytes)
996561de45fSGirish Mahadevan dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
997561de45fSGirish Mahadevan mas->rx_rem_bytes, mas->cur_bits_per_word);
99859ab0fa0SStephen Boyd } else {
99959ab0fa0SStephen Boyd complete(&mas->cs_done);
100059ab0fa0SStephen Boyd }
1001561de45fSGirish Mahadevan }
1002e5f0dfa7SVijaya Krishna Nivarthi } else if (mas->cur_xfer_mode == GENI_SE_DMA) {
1003e5f0dfa7SVijaya Krishna Nivarthi const struct spi_transfer *xfer = mas->cur_xfer;
1004e5f0dfa7SVijaya Krishna Nivarthi u32 dma_tx_status = readl_relaxed(se->base + SE_DMA_TX_IRQ_STAT);
1005e5f0dfa7SVijaya Krishna Nivarthi u32 dma_rx_status = readl_relaxed(se->base + SE_DMA_RX_IRQ_STAT);
1006e5f0dfa7SVijaya Krishna Nivarthi
1007e5f0dfa7SVijaya Krishna Nivarthi if (dma_tx_status)
1008e5f0dfa7SVijaya Krishna Nivarthi writel(dma_tx_status, se->base + SE_DMA_TX_IRQ_CLR);
1009e5f0dfa7SVijaya Krishna Nivarthi if (dma_rx_status)
1010e5f0dfa7SVijaya Krishna Nivarthi writel(dma_rx_status, se->base + SE_DMA_RX_IRQ_CLR);
1011e5f0dfa7SVijaya Krishna Nivarthi if (dma_tx_status & TX_DMA_DONE)
1012e5f0dfa7SVijaya Krishna Nivarthi mas->tx_rem_bytes = 0;
1013e5f0dfa7SVijaya Krishna Nivarthi if (dma_rx_status & RX_DMA_DONE)
1014e5f0dfa7SVijaya Krishna Nivarthi mas->rx_rem_bytes = 0;
1015e5f0dfa7SVijaya Krishna Nivarthi if (dma_tx_status & TX_RESET_DONE)
1016e5f0dfa7SVijaya Krishna Nivarthi complete(&mas->tx_reset_done);
1017e5f0dfa7SVijaya Krishna Nivarthi if (dma_rx_status & RX_RESET_DONE)
1018e5f0dfa7SVijaya Krishna Nivarthi complete(&mas->rx_reset_done);
1019e5f0dfa7SVijaya Krishna Nivarthi if (!mas->tx_rem_bytes && !mas->rx_rem_bytes && xfer) {
1020e5f0dfa7SVijaya Krishna Nivarthi spi_finalize_current_transfer(spi);
1021e5f0dfa7SVijaya Krishna Nivarthi mas->cur_xfer = NULL;
1022e5f0dfa7SVijaya Krishna Nivarthi }
1023e5f0dfa7SVijaya Krishna Nivarthi }
1024561de45fSGirish Mahadevan
10257ba9bdcbSDouglas Anderson if (m_irq & M_CMD_CANCEL_EN)
10267ba9bdcbSDouglas Anderson complete(&mas->cancel_done);
10277ba9bdcbSDouglas Anderson if (m_irq & M_CMD_ABORT_EN)
10287ba9bdcbSDouglas Anderson complete(&mas->abort_done);
1029561de45fSGirish Mahadevan
10302ee471a1SDouglas Anderson /*
1031db56d030SJay Fang * It's safe or a good idea to Ack all of our interrupts at the end
1032db56d030SJay Fang * of the function. Specifically:
10332ee471a1SDouglas Anderson * - M_CMD_DONE_EN / M_RX_FIFO_LAST_EN: Edge triggered interrupts and
10342ee471a1SDouglas Anderson * clearing Acks. Clearing at the end relies on nobody else having
10352ee471a1SDouglas Anderson * started a new transfer yet or else we could be clearing _their_
10362ee471a1SDouglas Anderson * done bit, but everyone grabs the spinlock before starting a new
10372ee471a1SDouglas Anderson * transfer.
10382ee471a1SDouglas Anderson * - M_RX_FIFO_WATERMARK_EN / M_TX_FIFO_WATERMARK_EN: These appear
10392ee471a1SDouglas Anderson * to be "latched level" interrupts so it's important to clear them
10402ee471a1SDouglas Anderson * _after_ you've handled the condition and always safe to do so
10412ee471a1SDouglas Anderson * since they'll re-assert if they're still happening.
10422ee471a1SDouglas Anderson */
1043561de45fSGirish Mahadevan writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
10442ee471a1SDouglas Anderson
1045539afdf9SDouglas Anderson spin_unlock(&mas->lock);
10462ee471a1SDouglas Anderson
10470dccff3cSAlok Chauhan return IRQ_HANDLED;
1048561de45fSGirish Mahadevan }
1049561de45fSGirish Mahadevan
spi_geni_probe(struct platform_device * pdev)1050561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
1051561de45fSGirish Mahadevan {
10526a34e285SAlok Chauhan int ret, irq;
10538726bdceSYang Yingliang struct spi_controller *spi;
1054561de45fSGirish Mahadevan struct spi_geni_master *mas;
10556a34e285SAlok Chauhan void __iomem *base;
10566a34e285SAlok Chauhan struct clk *clk;
1057ea1e5b33SStephen Boyd struct device *dev = &pdev->dev;
10586a34e285SAlok Chauhan
10596a34e285SAlok Chauhan irq = platform_get_irq(pdev, 0);
10606b8ac10eSStephen Boyd if (irq < 0)
10616a34e285SAlok Chauhan return irq;
10626a34e285SAlok Chauhan
1063b59c1224SVinod Koul ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
1064b59c1224SVinod Koul if (ret)
1065b59c1224SVinod Koul return dev_err_probe(dev, ret, "could not set DMA mask\n");
1066b59c1224SVinod Koul
1067d8e477abSYueHaibing base = devm_platform_ioremap_resource(pdev, 0);
10686a34e285SAlok Chauhan if (IS_ERR(base))
10696a34e285SAlok Chauhan return PTR_ERR(base);
10706a34e285SAlok Chauhan
1071ea1e5b33SStephen Boyd clk = devm_clk_get(dev, "se");
1072ea1e5b33SStephen Boyd if (IS_ERR(clk))
10736a34e285SAlok Chauhan return PTR_ERR(clk);
1074561de45fSGirish Mahadevan
10758726bdceSYang Yingliang spi = devm_spi_alloc_host(dev, sizeof(*mas));
1076561de45fSGirish Mahadevan if (!spi)
1077561de45fSGirish Mahadevan return -ENOMEM;
1078561de45fSGirish Mahadevan
1079561de45fSGirish Mahadevan platform_set_drvdata(pdev, spi);
10808726bdceSYang Yingliang mas = spi_controller_get_devdata(spi);
10816a34e285SAlok Chauhan mas->irq = irq;
1082ea1e5b33SStephen Boyd mas->dev = dev;
1083ea1e5b33SStephen Boyd mas->se.dev = dev;
1084ea1e5b33SStephen Boyd mas->se.wrapper = dev_get_drvdata(dev->parent);
10856a34e285SAlok Chauhan mas->se.base = base;
10866a34e285SAlok Chauhan mas->se.clk = clk;
1087cfb12911SYangtao Li
1088cfb12911SYangtao Li ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1089cfb12911SYangtao Li if (ret)
1090cfb12911SYangtao Li return ret;
10911a9e489eSRajendra Nayak /* OPP table is optional */
1092cfb12911SYangtao Li ret = devm_pm_opp_of_add_table(&pdev->dev);
10937d568edfSViresh Kumar if (ret && ret != -ENODEV) {
10941a9e489eSRajendra Nayak dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1095cfb12911SYangtao Li return ret;
10961a9e489eSRajendra Nayak }
1097561de45fSGirish Mahadevan
1098561de45fSGirish Mahadevan spi->bus_num = -1;
1099ea1e5b33SStephen Boyd spi->dev.of_node = dev->of_node;
1100561de45fSGirish Mahadevan spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
1101561de45fSGirish Mahadevan spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1102561de45fSGirish Mahadevan spi->num_chipselect = 4;
1103561de45fSGirish Mahadevan spi->max_speed_hz = 50000000;
11043a76c7caSVijaya Krishna Nivarthi spi->max_dma_len = 0xffff0; /* 24 bits for tx/rx dma length */
1105561de45fSGirish Mahadevan spi->prepare_message = spi_geni_prepare_message;
1106561de45fSGirish Mahadevan spi->transfer_one = spi_geni_transfer_one;
1107b59c1224SVinod Koul spi->can_dma = geni_can_dma;
1108b59c1224SVinod Koul spi->dma_map_dev = dev->parent;
1109561de45fSGirish Mahadevan spi->auto_runtime_pm = true;
1110f8039ea5SVinod Koul spi->handle_err = spi_geni_handle_err;
11113b25f337SStephen Boyd spi->use_gpio_descriptors = true;
1112561de45fSGirish Mahadevan
11137ba9bdcbSDouglas Anderson init_completion(&mas->cs_done);
11147ba9bdcbSDouglas Anderson init_completion(&mas->cancel_done);
11157ba9bdcbSDouglas Anderson init_completion(&mas->abort_done);
1116e5f0dfa7SVijaya Krishna Nivarthi init_completion(&mas->tx_reset_done);
1117e5f0dfa7SVijaya Krishna Nivarthi init_completion(&mas->rx_reset_done);
1118561de45fSGirish Mahadevan spin_lock_init(&mas->lock);
1119*d0ccf760SGeorgi Djakov
1120*d0ccf760SGeorgi Djakov ret = geni_icc_get(&mas->se, NULL);
1121*d0ccf760SGeorgi Djakov if (ret)
1122*d0ccf760SGeorgi Djakov return ret;
1123*d0ccf760SGeorgi Djakov
1124cfdab2cdSDouglas Anderson pm_runtime_use_autosuspend(&pdev->dev);
1125cfdab2cdSDouglas Anderson pm_runtime_set_autosuspend_delay(&pdev->dev, 250);
112689e362c8SJinjie Ruan ret = devm_pm_runtime_enable(dev);
112789e362c8SJinjie Ruan if (ret)
112889e362c8SJinjie Ruan return ret;
1129561de45fSGirish Mahadevan
1130d7f74cc3SPraveen Talari if (device_property_read_bool(&pdev->dev, "spi-slave"))
11318726bdceSYang Yingliang spi->target = true;
1132d7f74cc3SPraveen Talari
11330e3b8a81SAkash Asthana /* Set the bus quota to a reasonable value for register access */
11340e3b8a81SAkash Asthana mas->se.icc_paths[GENI_TO_CORE].avg_bw = Bps_to_icc(CORE_2X_50_MHZ);
11350e3b8a81SAkash Asthana mas->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
11360e3b8a81SAkash Asthana
11370e3b8a81SAkash Asthana ret = geni_icc_set_bw(&mas->se);
11380e3b8a81SAkash Asthana if (ret)
113989e362c8SJinjie Ruan return ret;
11400e3b8a81SAkash Asthana
1141561de45fSGirish Mahadevan ret = spi_geni_init(mas);
1142561de45fSGirish Mahadevan if (ret)
114389e362c8SJinjie Ruan return ret;
1144561de45fSGirish Mahadevan
1145b59c1224SVinod Koul /*
1146b59c1224SVinod Koul * check the mode supported and set_cs for fifo mode only
1147b59c1224SVinod Koul * for dma (gsi) mode, the gsi will set cs based on params passed in
1148b59c1224SVinod Koul * TRE
1149b59c1224SVinod Koul */
11508726bdceSYang Yingliang if (!spi->target && mas->cur_xfer_mode == GENI_SE_FIFO)
1151b59c1224SVinod Koul spi->set_cs = spi_geni_set_cs;
1152b59c1224SVinod Koul
1153d1000583SDmitry Baryshkov /*
1154d1000583SDmitry Baryshkov * TX is required per GSI spec, see setup_gsi_xfer().
1155d1000583SDmitry Baryshkov */
1156d1000583SDmitry Baryshkov if (mas->cur_xfer_mode == GENI_GPI_DMA)
1157d1000583SDmitry Baryshkov spi->flags = SPI_CONTROLLER_MUST_TX;
1158d1000583SDmitry Baryshkov
115907f1eb71SJinjie Ruan ret = devm_request_irq(dev, mas->irq, geni_spi_isr, 0, dev_name(dev), spi);
1160561de45fSGirish Mahadevan if (ret)
1161561de45fSGirish Mahadevan return ret;
1162561de45fSGirish Mahadevan
116307f1eb71SJinjie Ruan return devm_spi_register_controller(dev, spi);
1164561de45fSGirish Mahadevan }
1165561de45fSGirish Mahadevan
spi_geni_runtime_suspend(struct device * dev)1166561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
1167561de45fSGirish Mahadevan {
11688726bdceSYang Yingliang struct spi_controller *spi = dev_get_drvdata(dev);
11698726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
11700e3b8a81SAkash Asthana int ret;
1171561de45fSGirish Mahadevan
11721a9e489eSRajendra Nayak /* Drop the performance state vote */
11731a9e489eSRajendra Nayak dev_pm_opp_set_rate(dev, 0);
11741a9e489eSRajendra Nayak
11750e3b8a81SAkash Asthana ret = geni_se_resources_off(&mas->se);
11760e3b8a81SAkash Asthana if (ret)
11770e3b8a81SAkash Asthana return ret;
11780e3b8a81SAkash Asthana
11790e3b8a81SAkash Asthana return geni_icc_disable(&mas->se);
1180561de45fSGirish Mahadevan }
1181561de45fSGirish Mahadevan
spi_geni_runtime_resume(struct device * dev)1182561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
1183561de45fSGirish Mahadevan {
11848726bdceSYang Yingliang struct spi_controller *spi = dev_get_drvdata(dev);
11858726bdceSYang Yingliang struct spi_geni_master *mas = spi_controller_get_devdata(spi);
11860e3b8a81SAkash Asthana int ret;
11870e3b8a81SAkash Asthana
11880e3b8a81SAkash Asthana ret = geni_icc_enable(&mas->se);
11890e3b8a81SAkash Asthana if (ret)
11900e3b8a81SAkash Asthana return ret;
1191561de45fSGirish Mahadevan
11925f219524SDouglas Anderson ret = geni_se_resources_on(&mas->se);
11935f219524SDouglas Anderson if (ret)
11945f219524SDouglas Anderson return ret;
11955f219524SDouglas Anderson
11965f219524SDouglas Anderson return dev_pm_opp_set_rate(mas->dev, mas->cur_sclk_hz);
1197561de45fSGirish Mahadevan }
1198561de45fSGirish Mahadevan
spi_geni_suspend(struct device * dev)1199561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
1200561de45fSGirish Mahadevan {
12018726bdceSYang Yingliang struct spi_controller *spi = dev_get_drvdata(dev);
1202561de45fSGirish Mahadevan int ret;
1203561de45fSGirish Mahadevan
12048726bdceSYang Yingliang ret = spi_controller_suspend(spi);
1205561de45fSGirish Mahadevan if (ret)
1206561de45fSGirish Mahadevan return ret;
1207561de45fSGirish Mahadevan
1208561de45fSGirish Mahadevan ret = pm_runtime_force_suspend(dev);
1209561de45fSGirish Mahadevan if (ret)
12108726bdceSYang Yingliang spi_controller_resume(spi);
1211561de45fSGirish Mahadevan
1212561de45fSGirish Mahadevan return ret;
1213561de45fSGirish Mahadevan }
1214561de45fSGirish Mahadevan
spi_geni_resume(struct device * dev)1215561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
1216561de45fSGirish Mahadevan {
12178726bdceSYang Yingliang struct spi_controller *spi = dev_get_drvdata(dev);
1218561de45fSGirish Mahadevan int ret;
1219561de45fSGirish Mahadevan
1220561de45fSGirish Mahadevan ret = pm_runtime_force_resume(dev);
1221561de45fSGirish Mahadevan if (ret)
1222561de45fSGirish Mahadevan return ret;
1223561de45fSGirish Mahadevan
12248726bdceSYang Yingliang ret = spi_controller_resume(spi);
1225561de45fSGirish Mahadevan if (ret)
1226561de45fSGirish Mahadevan pm_runtime_force_suspend(dev);
1227561de45fSGirish Mahadevan
1228561de45fSGirish Mahadevan return ret;
1229561de45fSGirish Mahadevan }
1230561de45fSGirish Mahadevan
1231561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
1232561de45fSGirish Mahadevan SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
1233561de45fSGirish Mahadevan spi_geni_runtime_resume, NULL)
1234561de45fSGirish Mahadevan SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
1235561de45fSGirish Mahadevan };
1236561de45fSGirish Mahadevan
1237561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
1238561de45fSGirish Mahadevan { .compatible = "qcom,geni-spi" },
1239561de45fSGirish Mahadevan {}
1240561de45fSGirish Mahadevan };
1241561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
1242561de45fSGirish Mahadevan
1243561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
1244561de45fSGirish Mahadevan .probe = spi_geni_probe,
1245561de45fSGirish Mahadevan .driver = {
1246561de45fSGirish Mahadevan .name = "geni_spi",
1247561de45fSGirish Mahadevan .pm = &spi_geni_pm_ops,
1248561de45fSGirish Mahadevan .of_match_table = spi_geni_dt_match,
1249561de45fSGirish Mahadevan },
1250561de45fSGirish Mahadevan };
1251561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
1252561de45fSGirish Mahadevan
1253561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
1254561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
1255