blob: 5986c520b19655e3c0f40c3ccf843a15ecff8b51 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Feng Tang7063c0d2010-12-24 13:59:11 +08002/*
Serge Semin6c710c02020-05-29 16:11:59 +03003 * Special handling for DW DMA core
Feng Tang7063c0d2010-12-24 13:59:11 +08004 *
Andy Shevchenko197e96b2014-09-12 15:12:01 +03005 * Copyright (c) 2009, 2014 Intel Corporation.
Feng Tang7063c0d2010-12-24 13:59:11 +08006 */
7
Serge Seminbdbdf0f2020-05-29 16:11:52 +03008#include <linux/completion.h>
Andy Shevchenkoe7940952020-05-06 18:30:22 +03009#include <linux/dma-mapping.h>
10#include <linux/dmaengine.h>
Andy Shevchenkoe62a15d2020-05-06 18:30:21 +030011#include <linux/irqreturn.h>
Serge Seminbdbdf0f2020-05-29 16:11:52 +030012#include <linux/jiffies.h>
Feng Tang7063c0d2010-12-24 13:59:11 +080013#include <linux/pci.h>
Andy Shevchenkod744f822015-03-09 16:48:50 +020014#include <linux/platform_data/dma-dw.h>
Serge Semin6c710c02020-05-29 16:11:59 +030015#include <linux/spi/spi.h>
16#include <linux/types.h>
17
18#include "spi-dw.h"
Feng Tang7063c0d2010-12-24 13:59:11 +080019
Serge Semin1ade2d82020-05-29 16:11:53 +030020#define WAIT_RETRIES 5
Andy Shevchenko30c8eb52014-10-28 18:25:02 +020021#define RX_BUSY 0
Serge Seminc534df92020-05-29 16:11:55 +030022#define RX_BURST_LEVEL 16
Andy Shevchenko30c8eb52014-10-28 18:25:02 +020023#define TX_BUSY 1
Serge Seminc534df92020-05-29 16:11:55 +030024#define TX_BURST_LEVEL 16
Andy Shevchenko30c8eb52014-10-28 18:25:02 +020025
Serge Semin57784412020-05-29 16:12:02 +030026static bool dw_spi_dma_chan_filter(struct dma_chan *chan, void *param)
Feng Tang7063c0d2010-12-24 13:59:11 +080027{
Andy Shevchenkod744f822015-03-09 16:48:50 +020028 struct dw_dma_slave *s = param;
Feng Tang7063c0d2010-12-24 13:59:11 +080029
Andy Shevchenkod744f822015-03-09 16:48:50 +020030 if (s->dma_dev != chan->device->dev)
31 return false;
32
33 chan->private = s;
34 return true;
Feng Tang7063c0d2010-12-24 13:59:11 +080035}
36
Serge Semin57784412020-05-29 16:12:02 +030037static void dw_spi_dma_maxburst_init(struct dw_spi *dws)
Serge Semin0b2b6652020-05-29 16:11:56 +030038{
39 struct dma_slave_caps caps;
40 u32 max_burst, def_burst;
41 int ret;
42
43 def_burst = dws->fifo_len / 2;
44
45 ret = dma_get_slave_caps(dws->rxchan, &caps);
46 if (!ret && caps.max_burst)
47 max_burst = caps.max_burst;
48 else
49 max_burst = RX_BURST_LEVEL;
50
51 dws->rxburst = min(max_burst, def_burst);
52
53 ret = dma_get_slave_caps(dws->txchan, &caps);
54 if (!ret && caps.max_burst)
55 max_burst = caps.max_burst;
56 else
57 max_burst = TX_BURST_LEVEL;
58
59 dws->txburst = min(max_burst, def_burst);
60}
61
Serge Semin57784412020-05-29 16:12:02 +030062static int dw_spi_dma_init_mfld(struct device *dev, struct dw_spi *dws)
Feng Tang7063c0d2010-12-24 13:59:11 +080063{
Andy Shevchenkob3f82dc2020-05-29 21:31:49 +030064 struct dw_dma_slave dma_tx = { .dst_id = 1 }, *tx = &dma_tx;
65 struct dw_dma_slave dma_rx = { .src_id = 0 }, *rx = &dma_rx;
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030066 struct pci_dev *dma_dev;
Feng Tang7063c0d2010-12-24 13:59:11 +080067 dma_cap_mask_t mask;
68
69 /*
70 * Get pci device for DMA controller, currently it could only
Andy Shevchenkoea092452014-09-12 15:11:59 +030071 * be the DMA controller of Medfield
Feng Tang7063c0d2010-12-24 13:59:11 +080072 */
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030073 dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
74 if (!dma_dev)
75 return -ENODEV;
76
Feng Tang7063c0d2010-12-24 13:59:11 +080077 dma_cap_zero(mask);
78 dma_cap_set(DMA_SLAVE, mask);
79
80 /* 1. Init rx channel */
Andy Shevchenkob3f82dc2020-05-29 21:31:49 +030081 rx->dma_dev = &dma_dev->dev;
82 dws->rxchan = dma_request_channel(mask, dw_spi_dma_chan_filter, rx);
Feng Tang7063c0d2010-12-24 13:59:11 +080083 if (!dws->rxchan)
84 goto err_exit;
Feng Tang7063c0d2010-12-24 13:59:11 +080085
86 /* 2. Init tx channel */
Andy Shevchenkob3f82dc2020-05-29 21:31:49 +030087 tx->dma_dev = &dma_dev->dev;
88 dws->txchan = dma_request_channel(mask, dw_spi_dma_chan_filter, tx);
Feng Tang7063c0d2010-12-24 13:59:11 +080089 if (!dws->txchan)
90 goto free_rxchan;
Andy Shevchenkoa041e672020-05-07 14:54:49 +030091
92 dws->master->dma_rx = dws->rxchan;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +020093 dws->master->dma_tx = dws->txchan;
Feng Tang7063c0d2010-12-24 13:59:11 +080094
Serge Seminbdbdf0f2020-05-29 16:11:52 +030095 init_completion(&dws->dma_completion);
96
Serge Semin57784412020-05-29 16:12:02 +030097 dw_spi_dma_maxburst_init(dws);
Serge Semin0b2b6652020-05-29 16:11:56 +030098
Feng Tang7063c0d2010-12-24 13:59:11 +080099 return 0;
100
101free_rxchan:
102 dma_release_channel(dws->rxchan);
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300103 dws->rxchan = NULL;
Feng Tang7063c0d2010-12-24 13:59:11 +0800104err_exit:
Andy Shevchenkob89e9c82014-09-12 15:12:00 +0300105 return -EBUSY;
Feng Tang7063c0d2010-12-24 13:59:11 +0800106}
107
Serge Semin57784412020-05-29 16:12:02 +0300108static int dw_spi_dma_init_generic(struct device *dev, struct dw_spi *dws)
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300109{
110 dws->rxchan = dma_request_slave_channel(dev, "rx");
111 if (!dws->rxchan)
112 return -ENODEV;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300113
114 dws->txchan = dma_request_slave_channel(dev, "tx");
115 if (!dws->txchan) {
116 dma_release_channel(dws->rxchan);
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300117 dws->rxchan = NULL;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300118 return -ENODEV;
119 }
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300120
121 dws->master->dma_rx = dws->rxchan;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300122 dws->master->dma_tx = dws->txchan;
123
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300124 init_completion(&dws->dma_completion);
125
Serge Semin57784412020-05-29 16:12:02 +0300126 dw_spi_dma_maxburst_init(dws);
Serge Semin0b2b6652020-05-29 16:11:56 +0300127
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300128 return 0;
129}
130
Serge Semin57784412020-05-29 16:12:02 +0300131static void dw_spi_dma_exit(struct dw_spi *dws)
Feng Tang7063c0d2010-12-24 13:59:11 +0800132{
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300133 if (dws->txchan) {
134 dmaengine_terminate_sync(dws->txchan);
135 dma_release_channel(dws->txchan);
136 }
Andy Shevchenko8e45ef62014-09-18 20:08:53 +0300137
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300138 if (dws->rxchan) {
139 dmaengine_terminate_sync(dws->rxchan);
140 dma_release_channel(dws->rxchan);
141 }
Serge Semin0327f0b2020-05-15 13:47:42 +0300142
143 dw_writel(dws, DW_SPI_DMACR, 0);
Feng Tang7063c0d2010-12-24 13:59:11 +0800144}
145
Serge Semin57784412020-05-29 16:12:02 +0300146static irqreturn_t dw_spi_dma_transfer_handler(struct dw_spi *dws)
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200147{
Thor Thayerdd114442015-03-12 14:19:31 -0500148 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200149
150 if (!irq_status)
151 return IRQ_NONE;
152
Thor Thayerdd114442015-03-12 14:19:31 -0500153 dw_readl(dws, DW_SPI_ICR);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200154 spi_reset_chip(dws);
155
156 dev_err(&dws->master->dev, "%s: FIFO overrun/underrun\n", __func__);
157 dws->master->cur_msg->status = -EIO;
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300158 complete(&dws->dma_completion);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200159 return IRQ_HANDLED;
160}
161
Serge Semin57784412020-05-29 16:12:02 +0300162static bool dw_spi_can_dma(struct spi_controller *master,
163 struct spi_device *spi, struct spi_transfer *xfer)
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200164{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200165 struct dw_spi *dws = spi_controller_get_devdata(master);
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200166
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200167 return xfer->len > dws->fifo_len;
168}
169
Serge Semin57784412020-05-29 16:12:02 +0300170static enum dma_slave_buswidth dw_spi_dma_convert_width(u8 n_bytes)
171{
Serge Semin4fdc03a2020-05-22 03:07:54 +0300172 if (n_bytes == 1)
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200173 return DMA_SLAVE_BUSWIDTH_1_BYTE;
Serge Semin4fdc03a2020-05-22 03:07:54 +0300174 else if (n_bytes == 2)
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200175 return DMA_SLAVE_BUSWIDTH_2_BYTES;
176
177 return DMA_SLAVE_BUSWIDTH_UNDEFINED;
178}
179
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300180static int dw_spi_dma_wait(struct dw_spi *dws, struct spi_transfer *xfer)
181{
182 unsigned long long ms;
183
184 ms = xfer->len * MSEC_PER_SEC * BITS_PER_BYTE;
185 do_div(ms, xfer->effective_speed_hz);
186 ms += ms + 200;
187
188 if (ms > UINT_MAX)
189 ms = UINT_MAX;
190
191 ms = wait_for_completion_timeout(&dws->dma_completion,
192 msecs_to_jiffies(ms));
193
194 if (ms == 0) {
195 dev_err(&dws->master->cur_msg->spi->dev,
196 "DMA transaction timed out\n");
197 return -ETIMEDOUT;
198 }
199
200 return 0;
201}
202
Serge Semin1ade2d82020-05-29 16:11:53 +0300203static inline bool dw_spi_dma_tx_busy(struct dw_spi *dws)
204{
205 return !(dw_readl(dws, DW_SPI_SR) & SR_TF_EMPT);
206}
207
208static int dw_spi_dma_wait_tx_done(struct dw_spi *dws,
209 struct spi_transfer *xfer)
210{
211 int retry = WAIT_RETRIES;
212 struct spi_delay delay;
213 u32 nents;
214
215 nents = dw_readl(dws, DW_SPI_TXFLR);
216 delay.unit = SPI_DELAY_UNIT_SCK;
217 delay.value = nents * dws->n_bytes * BITS_PER_BYTE;
218
219 while (dw_spi_dma_tx_busy(dws) && retry--)
220 spi_delay_exec(&delay, xfer);
221
222 if (retry < 0) {
223 dev_err(&dws->master->dev, "Tx hanged up\n");
224 return -EIO;
225 }
226
227 return 0;
228}
229
Feng Tang7063c0d2010-12-24 13:59:11 +0800230/*
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200231 * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
232 * channel will clear a corresponding bit.
Feng Tang7063c0d2010-12-24 13:59:11 +0800233 */
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200234static void dw_spi_dma_tx_done(void *arg)
Feng Tang7063c0d2010-12-24 13:59:11 +0800235{
236 struct dw_spi *dws = arg;
237
Andy Shevchenko854d2f22015-03-06 14:42:01 +0200238 clear_bit(TX_BUSY, &dws->dma_chan_busy);
239 if (test_bit(RX_BUSY, &dws->dma_chan_busy))
Feng Tang7063c0d2010-12-24 13:59:11 +0800240 return;
Serge Semin0327f0b2020-05-15 13:47:42 +0300241
242 dw_writel(dws, DW_SPI_DMACR, 0);
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300243 complete(&dws->dma_completion);
Feng Tang7063c0d2010-12-24 13:59:11 +0800244}
245
Serge Semin57784412020-05-29 16:12:02 +0300246static struct dma_async_tx_descriptor *
247dw_spi_dma_prepare_tx(struct dw_spi *dws, struct spi_transfer *xfer)
Feng Tang7063c0d2010-12-24 13:59:11 +0800248{
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200249 struct dma_slave_config txconf;
250 struct dma_async_tx_descriptor *txdesc;
Feng Tang7063c0d2010-12-24 13:59:11 +0800251
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200252 if (!xfer->tx_buf)
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200253 return NULL;
254
Andy Shevchenko3cb97e22020-05-06 18:30:18 +0300255 memset(&txconf, 0, sizeof(txconf));
Vinod Koula485df42011-10-14 10:47:38 +0530256 txconf.direction = DMA_MEM_TO_DEV;
Feng Tang7063c0d2010-12-24 13:59:11 +0800257 txconf.dst_addr = dws->dma_addr;
Serge Semin0b2b6652020-05-29 16:11:56 +0300258 txconf.dst_maxburst = dws->txburst;
Feng Tang7063c0d2010-12-24 13:59:11 +0800259 txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Serge Semin57784412020-05-29 16:12:02 +0300260 txconf.dst_addr_width = dw_spi_dma_convert_width(dws->n_bytes);
Viresh Kumar258aea72012-02-01 16:12:19 +0530261 txconf.device_fc = false;
Feng Tang7063c0d2010-12-24 13:59:11 +0800262
Andy Shevchenko2a285292014-10-02 16:31:08 +0300263 dmaengine_slave_config(dws->txchan, &txconf);
Feng Tang7063c0d2010-12-24 13:59:11 +0800264
Andy Shevchenko2a285292014-10-02 16:31:08 +0300265 txdesc = dmaengine_prep_slave_sg(dws->txchan,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200266 xfer->tx_sg.sgl,
267 xfer->tx_sg.nents,
Vinod Koula485df42011-10-14 10:47:38 +0530268 DMA_MEM_TO_DEV,
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300269 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Andy Shevchenkoc9dafb22015-03-02 20:15:58 +0200270 if (!txdesc)
271 return NULL;
272
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200273 txdesc->callback = dw_spi_dma_tx_done;
Feng Tang7063c0d2010-12-24 13:59:11 +0800274 txdesc->callback_param = dws;
275
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200276 return txdesc;
277}
278
Serge Semin33726ef2020-05-29 16:11:54 +0300279static inline bool dw_spi_dma_rx_busy(struct dw_spi *dws)
280{
281 return !!(dw_readl(dws, DW_SPI_SR) & SR_RF_NOT_EMPT);
282}
283
284static int dw_spi_dma_wait_rx_done(struct dw_spi *dws)
285{
286 int retry = WAIT_RETRIES;
287 struct spi_delay delay;
288 unsigned long ns, us;
289 u32 nents;
290
291 /*
292 * It's unlikely that DMA engine is still doing the data fetching, but
293 * if it's let's give it some reasonable time. The timeout calculation
294 * is based on the synchronous APB/SSI reference clock rate, on a
295 * number of data entries left in the Rx FIFO, times a number of clock
296 * periods normally needed for a single APB read/write transaction
297 * without PREADY signal utilized (which is true for the DW APB SSI
298 * controller).
299 */
300 nents = dw_readl(dws, DW_SPI_RXFLR);
301 ns = 4U * NSEC_PER_SEC / dws->max_freq * nents;
302 if (ns <= NSEC_PER_USEC) {
303 delay.unit = SPI_DELAY_UNIT_NSECS;
304 delay.value = ns;
305 } else {
306 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
307 delay.unit = SPI_DELAY_UNIT_USECS;
308 delay.value = clamp_val(us, 0, USHRT_MAX);
309 }
310
311 while (dw_spi_dma_rx_busy(dws) && retry--)
312 spi_delay_exec(&delay, NULL);
313
314 if (retry < 0) {
315 dev_err(&dws->master->dev, "Rx hanged up\n");
316 return -EIO;
317 }
318
319 return 0;
320}
321
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200322/*
323 * dws->dma_chan_busy is set before the dma transfer starts, callback for rx
324 * channel will clear a corresponding bit.
325 */
326static void dw_spi_dma_rx_done(void *arg)
327{
328 struct dw_spi *dws = arg;
329
Andy Shevchenko854d2f22015-03-06 14:42:01 +0200330 clear_bit(RX_BUSY, &dws->dma_chan_busy);
331 if (test_bit(TX_BUSY, &dws->dma_chan_busy))
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200332 return;
Serge Semin0327f0b2020-05-15 13:47:42 +0300333
334 dw_writel(dws, DW_SPI_DMACR, 0);
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300335 complete(&dws->dma_completion);
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200336}
337
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200338static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws,
339 struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200340{
341 struct dma_slave_config rxconf;
342 struct dma_async_tx_descriptor *rxdesc;
343
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200344 if (!xfer->rx_buf)
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200345 return NULL;
346
Andy Shevchenko3cb97e22020-05-06 18:30:18 +0300347 memset(&rxconf, 0, sizeof(rxconf));
Vinod Koula485df42011-10-14 10:47:38 +0530348 rxconf.direction = DMA_DEV_TO_MEM;
Feng Tang7063c0d2010-12-24 13:59:11 +0800349 rxconf.src_addr = dws->dma_addr;
Serge Semin0b2b6652020-05-29 16:11:56 +0300350 rxconf.src_maxburst = dws->rxburst;
Feng Tang7063c0d2010-12-24 13:59:11 +0800351 rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Serge Semin57784412020-05-29 16:12:02 +0300352 rxconf.src_addr_width = dw_spi_dma_convert_width(dws->n_bytes);
Viresh Kumar258aea72012-02-01 16:12:19 +0530353 rxconf.device_fc = false;
Feng Tang7063c0d2010-12-24 13:59:11 +0800354
Andy Shevchenko2a285292014-10-02 16:31:08 +0300355 dmaengine_slave_config(dws->rxchan, &rxconf);
Feng Tang7063c0d2010-12-24 13:59:11 +0800356
Andy Shevchenko2a285292014-10-02 16:31:08 +0300357 rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200358 xfer->rx_sg.sgl,
359 xfer->rx_sg.nents,
Vinod Koula485df42011-10-14 10:47:38 +0530360 DMA_DEV_TO_MEM,
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300361 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Andy Shevchenkoc9dafb22015-03-02 20:15:58 +0200362 if (!rxdesc)
363 return NULL;
364
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200365 rxdesc->callback = dw_spi_dma_rx_done;
Feng Tang7063c0d2010-12-24 13:59:11 +0800366 rxdesc->callback_param = dws;
367
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200368 return rxdesc;
369}
370
Serge Semin57784412020-05-29 16:12:02 +0300371static int dw_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200372{
Serge Semin43dba9f2020-05-22 03:07:51 +0300373 u16 imr = 0, dma_ctrl = 0;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200374
Serge Semin0b2b6652020-05-29 16:11:56 +0300375 dw_writel(dws, DW_SPI_DMARDLR, dws->rxburst - 1);
376 dw_writel(dws, DW_SPI_DMATDLR, dws->fifo_len - dws->txburst);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200377
Andy Shevchenko3d7db0f2020-05-29 21:31:50 +0300378 if (xfer->tx_buf)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200379 dma_ctrl |= SPI_DMA_TDMAE;
Andy Shevchenko3d7db0f2020-05-29 21:31:50 +0300380 if (xfer->rx_buf)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200381 dma_ctrl |= SPI_DMA_RDMAE;
Thor Thayerdd114442015-03-12 14:19:31 -0500382 dw_writel(dws, DW_SPI_DMACR, dma_ctrl);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200383
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200384 /* Set the interrupt mask */
Andy Shevchenko3d7db0f2020-05-29 21:31:50 +0300385 if (xfer->tx_buf)
386 imr |= SPI_INT_TXOI;
387 if (xfer->rx_buf)
388 imr |= SPI_INT_RXUI | SPI_INT_RXOI;
Serge Semin43dba9f2020-05-22 03:07:51 +0300389 spi_umask_intr(dws, imr);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200390
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300391 reinit_completion(&dws->dma_completion);
392
Serge Semin57784412020-05-29 16:12:02 +0300393 dws->transfer_handler = dw_spi_dma_transfer_handler;
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200394
Andy Shevchenko9f145382015-03-09 16:48:46 +0200395 return 0;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200396}
397
Serge Semin57784412020-05-29 16:12:02 +0300398static int dw_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200399{
400 struct dma_async_tx_descriptor *txdesc, *rxdesc;
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300401 int ret;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200402
Andy Shevchenko9f145382015-03-09 16:48:46 +0200403 /* Prepare the TX dma transfer */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200404 txdesc = dw_spi_dma_prepare_tx(dws, xfer);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200405
Andy Shevchenko9f145382015-03-09 16:48:46 +0200406 /* Prepare the RX dma transfer */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200407 rxdesc = dw_spi_dma_prepare_rx(dws, xfer);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200408
Feng Tang7063c0d2010-12-24 13:59:11 +0800409 /* rx must be started before tx due to spi instinct */
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200410 if (rxdesc) {
411 set_bit(RX_BUSY, &dws->dma_chan_busy);
412 dmaengine_submit(rxdesc);
413 dma_async_issue_pending(dws->rxchan);
414 }
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300415
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200416 if (txdesc) {
417 set_bit(TX_BUSY, &dws->dma_chan_busy);
418 dmaengine_submit(txdesc);
419 dma_async_issue_pending(dws->txchan);
420 }
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300421
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300422 ret = dw_spi_dma_wait(dws, xfer);
423 if (ret)
424 return ret;
425
Serge Semin1ade2d82020-05-29 16:11:53 +0300426 if (txdesc && dws->master->cur_msg->status == -EINPROGRESS) {
427 ret = dw_spi_dma_wait_tx_done(dws, xfer);
428 if (ret)
429 return ret;
430 }
431
Serge Semin33726ef2020-05-29 16:11:54 +0300432 if (rxdesc && dws->master->cur_msg->status == -EINPROGRESS)
433 ret = dw_spi_dma_wait_rx_done(dws);
434
435 return ret;
Feng Tang7063c0d2010-12-24 13:59:11 +0800436}
437
Serge Semin57784412020-05-29 16:12:02 +0300438static void dw_spi_dma_stop(struct dw_spi *dws)
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200439{
440 if (test_bit(TX_BUSY, &dws->dma_chan_busy)) {
Andy Shevchenkocf1716e2017-01-03 15:48:20 +0200441 dmaengine_terminate_sync(dws->txchan);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200442 clear_bit(TX_BUSY, &dws->dma_chan_busy);
443 }
444 if (test_bit(RX_BUSY, &dws->dma_chan_busy)) {
Andy Shevchenkocf1716e2017-01-03 15:48:20 +0200445 dmaengine_terminate_sync(dws->rxchan);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200446 clear_bit(RX_BUSY, &dws->dma_chan_busy);
447 }
Serge Semin0327f0b2020-05-15 13:47:42 +0300448
449 dw_writel(dws, DW_SPI_DMACR, 0);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200450}
451
Serge Semin57784412020-05-29 16:12:02 +0300452static const struct dw_spi_dma_ops dw_spi_dma_mfld_ops = {
453 .dma_init = dw_spi_dma_init_mfld,
454 .dma_exit = dw_spi_dma_exit,
455 .dma_setup = dw_spi_dma_setup,
456 .can_dma = dw_spi_can_dma,
457 .dma_transfer = dw_spi_dma_transfer,
458 .dma_stop = dw_spi_dma_stop,
Feng Tang7063c0d2010-12-24 13:59:11 +0800459};
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300460
Serge Semin57784412020-05-29 16:12:02 +0300461void dw_spi_dma_setup_mfld(struct dw_spi *dws)
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300462{
Serge Semin57784412020-05-29 16:12:02 +0300463 dws->dma_ops = &dw_spi_dma_mfld_ops;
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300464}
Serge Semin57784412020-05-29 16:12:02 +0300465EXPORT_SYMBOL_GPL(dw_spi_dma_setup_mfld);
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300466
Serge Semin57784412020-05-29 16:12:02 +0300467static const struct dw_spi_dma_ops dw_spi_dma_generic_ops = {
468 .dma_init = dw_spi_dma_init_generic,
469 .dma_exit = dw_spi_dma_exit,
470 .dma_setup = dw_spi_dma_setup,
471 .can_dma = dw_spi_can_dma,
472 .dma_transfer = dw_spi_dma_transfer,
473 .dma_stop = dw_spi_dma_stop,
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300474};
475
Serge Semin57784412020-05-29 16:12:02 +0300476void dw_spi_dma_setup_generic(struct dw_spi *dws)
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300477{
Serge Semin57784412020-05-29 16:12:02 +0300478 dws->dma_ops = &dw_spi_dma_generic_ops;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300479}
Serge Semin57784412020-05-29 16:12:02 +0300480EXPORT_SYMBOL_GPL(dw_spi_dma_setup_generic);