blob: abd6955ad1f790c9c5bbaf8fb0cd25f28ad92e5f [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/*
Grant Likelyca632f52011-06-06 01:16:30 -06003 * Special handling for DW core on Intel MID platform
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
Feng Tang7063c0d2010-12-24 13:59:11 +08008#include <linux/spi/spi.h>
Viresh Kumar258aea72012-02-01 16:12:19 +05309#include <linux/types.h>
Grant Likely568a60e2011-02-28 12:47:12 -070010
Grant Likelyca632f52011-06-06 01:16:30 -060011#include "spi-dw.h"
Feng Tang7063c0d2010-12-24 13:59:11 +080012
13#ifdef CONFIG_SPI_DW_MID_DMA
Serge Seminbdbdf0f2020-05-29 16:11:52 +030014#include <linux/completion.h>
Andy Shevchenkoe7940952020-05-06 18:30:22 +030015#include <linux/dma-mapping.h>
16#include <linux/dmaengine.h>
Andy Shevchenkoe62a15d2020-05-06 18:30:21 +030017#include <linux/irqreturn.h>
Serge Seminbdbdf0f2020-05-29 16:11:52 +030018#include <linux/jiffies.h>
Feng Tang7063c0d2010-12-24 13:59:11 +080019#include <linux/pci.h>
Andy Shevchenkod744f822015-03-09 16:48:50 +020020#include <linux/platform_data/dma-dw.h>
Feng Tang7063c0d2010-12-24 13:59:11 +080021
Serge Semin1ade2d82020-05-29 16:11:53 +030022#define WAIT_RETRIES 5
Andy Shevchenko30c8eb52014-10-28 18:25:02 +020023#define RX_BUSY 0
24#define TX_BUSY 1
25
Feng Tang7063c0d2010-12-24 13:59:11 +080026static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param)
27{
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
Andy Shevchenko6370aba2020-05-06 18:30:24 +030037static int mid_spi_dma_init_mfld(struct device *dev, struct dw_spi *dws)
Feng Tang7063c0d2010-12-24 13:59:11 +080038{
Serge Semin2afccbd2020-05-22 03:07:52 +030039 struct dw_dma_slave slave = {
40 .src_id = 0,
41 .dst_id = 0
42 };
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030043 struct pci_dev *dma_dev;
Feng Tang7063c0d2010-12-24 13:59:11 +080044 dma_cap_mask_t mask;
45
46 /*
47 * Get pci device for DMA controller, currently it could only
Andy Shevchenkoea092452014-09-12 15:11:59 +030048 * be the DMA controller of Medfield
Feng Tang7063c0d2010-12-24 13:59:11 +080049 */
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030050 dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
51 if (!dma_dev)
52 return -ENODEV;
53
Feng Tang7063c0d2010-12-24 13:59:11 +080054 dma_cap_zero(mask);
55 dma_cap_set(DMA_SLAVE, mask);
56
57 /* 1. Init rx channel */
Serge Semin2afccbd2020-05-22 03:07:52 +030058 slave.dma_dev = &dma_dev->dev;
59 dws->rxchan = dma_request_channel(mask, mid_spi_dma_chan_filter, &slave);
Feng Tang7063c0d2010-12-24 13:59:11 +080060 if (!dws->rxchan)
61 goto err_exit;
Feng Tang7063c0d2010-12-24 13:59:11 +080062
63 /* 2. Init tx channel */
Serge Semin2afccbd2020-05-22 03:07:52 +030064 slave.dst_id = 1;
65 dws->txchan = dma_request_channel(mask, mid_spi_dma_chan_filter, &slave);
Feng Tang7063c0d2010-12-24 13:59:11 +080066 if (!dws->txchan)
67 goto free_rxchan;
Andy Shevchenkoa041e672020-05-07 14:54:49 +030068
69 dws->master->dma_rx = dws->rxchan;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +020070 dws->master->dma_tx = dws->txchan;
Feng Tang7063c0d2010-12-24 13:59:11 +080071
Serge Seminbdbdf0f2020-05-29 16:11:52 +030072 init_completion(&dws->dma_completion);
73
Feng Tang7063c0d2010-12-24 13:59:11 +080074 return 0;
75
76free_rxchan:
77 dma_release_channel(dws->rxchan);
Andy Shevchenkoa041e672020-05-07 14:54:49 +030078 dws->rxchan = NULL;
Feng Tang7063c0d2010-12-24 13:59:11 +080079err_exit:
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030080 return -EBUSY;
Feng Tang7063c0d2010-12-24 13:59:11 +080081}
82
Jarkko Nikula22d48ad2020-05-06 18:30:25 +030083static int mid_spi_dma_init_generic(struct device *dev, struct dw_spi *dws)
84{
85 dws->rxchan = dma_request_slave_channel(dev, "rx");
86 if (!dws->rxchan)
87 return -ENODEV;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +030088
89 dws->txchan = dma_request_slave_channel(dev, "tx");
90 if (!dws->txchan) {
91 dma_release_channel(dws->rxchan);
Andy Shevchenkoa041e672020-05-07 14:54:49 +030092 dws->rxchan = NULL;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +030093 return -ENODEV;
94 }
Andy Shevchenkoa041e672020-05-07 14:54:49 +030095
96 dws->master->dma_rx = dws->rxchan;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +030097 dws->master->dma_tx = dws->txchan;
98
Serge Seminbdbdf0f2020-05-29 16:11:52 +030099 init_completion(&dws->dma_completion);
100
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300101 return 0;
102}
103
Feng Tang7063c0d2010-12-24 13:59:11 +0800104static void mid_spi_dma_exit(struct dw_spi *dws)
105{
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300106 if (dws->txchan) {
107 dmaengine_terminate_sync(dws->txchan);
108 dma_release_channel(dws->txchan);
109 }
Andy Shevchenko8e45ef62014-09-18 20:08:53 +0300110
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300111 if (dws->rxchan) {
112 dmaengine_terminate_sync(dws->rxchan);
113 dma_release_channel(dws->rxchan);
114 }
Serge Semin0327f0b2020-05-15 13:47:42 +0300115
116 dw_writel(dws, DW_SPI_DMACR, 0);
Feng Tang7063c0d2010-12-24 13:59:11 +0800117}
118
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200119static irqreturn_t dma_transfer(struct dw_spi *dws)
120{
Thor Thayerdd114442015-03-12 14:19:31 -0500121 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200122
123 if (!irq_status)
124 return IRQ_NONE;
125
Thor Thayerdd114442015-03-12 14:19:31 -0500126 dw_readl(dws, DW_SPI_ICR);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200127 spi_reset_chip(dws);
128
129 dev_err(&dws->master->dev, "%s: FIFO overrun/underrun\n", __func__);
130 dws->master->cur_msg->status = -EIO;
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300131 complete(&dws->dma_completion);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200132 return IRQ_HANDLED;
133}
134
Jarkko Nikula721483e2018-02-01 17:17:29 +0200135static bool mid_spi_can_dma(struct spi_controller *master,
136 struct spi_device *spi, struct spi_transfer *xfer)
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200137{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200138 struct dw_spi *dws = spi_controller_get_devdata(master);
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200139
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200140 return xfer->len > dws->fifo_len;
141}
142
Serge Semin4fdc03a2020-05-22 03:07:54 +0300143static enum dma_slave_buswidth convert_dma_width(u8 n_bytes) {
144 if (n_bytes == 1)
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200145 return DMA_SLAVE_BUSWIDTH_1_BYTE;
Serge Semin4fdc03a2020-05-22 03:07:54 +0300146 else if (n_bytes == 2)
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200147 return DMA_SLAVE_BUSWIDTH_2_BYTES;
148
149 return DMA_SLAVE_BUSWIDTH_UNDEFINED;
150}
151
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300152static int dw_spi_dma_wait(struct dw_spi *dws, struct spi_transfer *xfer)
153{
154 unsigned long long ms;
155
156 ms = xfer->len * MSEC_PER_SEC * BITS_PER_BYTE;
157 do_div(ms, xfer->effective_speed_hz);
158 ms += ms + 200;
159
160 if (ms > UINT_MAX)
161 ms = UINT_MAX;
162
163 ms = wait_for_completion_timeout(&dws->dma_completion,
164 msecs_to_jiffies(ms));
165
166 if (ms == 0) {
167 dev_err(&dws->master->cur_msg->spi->dev,
168 "DMA transaction timed out\n");
169 return -ETIMEDOUT;
170 }
171
172 return 0;
173}
174
Serge Semin1ade2d82020-05-29 16:11:53 +0300175static inline bool dw_spi_dma_tx_busy(struct dw_spi *dws)
176{
177 return !(dw_readl(dws, DW_SPI_SR) & SR_TF_EMPT);
178}
179
180static int dw_spi_dma_wait_tx_done(struct dw_spi *dws,
181 struct spi_transfer *xfer)
182{
183 int retry = WAIT_RETRIES;
184 struct spi_delay delay;
185 u32 nents;
186
187 nents = dw_readl(dws, DW_SPI_TXFLR);
188 delay.unit = SPI_DELAY_UNIT_SCK;
189 delay.value = nents * dws->n_bytes * BITS_PER_BYTE;
190
191 while (dw_spi_dma_tx_busy(dws) && retry--)
192 spi_delay_exec(&delay, xfer);
193
194 if (retry < 0) {
195 dev_err(&dws->master->dev, "Tx hanged up\n");
196 return -EIO;
197 }
198
199 return 0;
200}
201
Feng Tang7063c0d2010-12-24 13:59:11 +0800202/*
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200203 * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
204 * channel will clear a corresponding bit.
Feng Tang7063c0d2010-12-24 13:59:11 +0800205 */
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200206static void dw_spi_dma_tx_done(void *arg)
Feng Tang7063c0d2010-12-24 13:59:11 +0800207{
208 struct dw_spi *dws = arg;
209
Andy Shevchenko854d2f22015-03-06 14:42:01 +0200210 clear_bit(TX_BUSY, &dws->dma_chan_busy);
211 if (test_bit(RX_BUSY, &dws->dma_chan_busy))
Feng Tang7063c0d2010-12-24 13:59:11 +0800212 return;
Serge Semin0327f0b2020-05-15 13:47:42 +0300213
214 dw_writel(dws, DW_SPI_DMACR, 0);
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300215 complete(&dws->dma_completion);
Feng Tang7063c0d2010-12-24 13:59:11 +0800216}
217
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200218static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws,
219 struct spi_transfer *xfer)
Feng Tang7063c0d2010-12-24 13:59:11 +0800220{
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200221 struct dma_slave_config txconf;
222 struct dma_async_tx_descriptor *txdesc;
Feng Tang7063c0d2010-12-24 13:59:11 +0800223
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200224 if (!xfer->tx_buf)
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200225 return NULL;
226
Andy Shevchenko3cb97e22020-05-06 18:30:18 +0300227 memset(&txconf, 0, sizeof(txconf));
Vinod Koula485df42011-10-14 10:47:38 +0530228 txconf.direction = DMA_MEM_TO_DEV;
Feng Tang7063c0d2010-12-24 13:59:11 +0800229 txconf.dst_addr = dws->dma_addr;
Andy Shevchenkod744f822015-03-09 16:48:50 +0200230 txconf.dst_maxburst = 16;
Feng Tang7063c0d2010-12-24 13:59:11 +0800231 txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Serge Semin4fdc03a2020-05-22 03:07:54 +0300232 txconf.dst_addr_width = convert_dma_width(dws->n_bytes);
Viresh Kumar258aea72012-02-01 16:12:19 +0530233 txconf.device_fc = false;
Feng Tang7063c0d2010-12-24 13:59:11 +0800234
Andy Shevchenko2a285292014-10-02 16:31:08 +0300235 dmaengine_slave_config(dws->txchan, &txconf);
Feng Tang7063c0d2010-12-24 13:59:11 +0800236
Andy Shevchenko2a285292014-10-02 16:31:08 +0300237 txdesc = dmaengine_prep_slave_sg(dws->txchan,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200238 xfer->tx_sg.sgl,
239 xfer->tx_sg.nents,
Vinod Koula485df42011-10-14 10:47:38 +0530240 DMA_MEM_TO_DEV,
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300241 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Andy Shevchenkoc9dafb22015-03-02 20:15:58 +0200242 if (!txdesc)
243 return NULL;
244
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200245 txdesc->callback = dw_spi_dma_tx_done;
Feng Tang7063c0d2010-12-24 13:59:11 +0800246 txdesc->callback_param = dws;
247
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200248 return txdesc;
249}
250
Serge Semin33726ef2020-05-29 16:11:54 +0300251static inline bool dw_spi_dma_rx_busy(struct dw_spi *dws)
252{
253 return !!(dw_readl(dws, DW_SPI_SR) & SR_RF_NOT_EMPT);
254}
255
256static int dw_spi_dma_wait_rx_done(struct dw_spi *dws)
257{
258 int retry = WAIT_RETRIES;
259 struct spi_delay delay;
260 unsigned long ns, us;
261 u32 nents;
262
263 /*
264 * It's unlikely that DMA engine is still doing the data fetching, but
265 * if it's let's give it some reasonable time. The timeout calculation
266 * is based on the synchronous APB/SSI reference clock rate, on a
267 * number of data entries left in the Rx FIFO, times a number of clock
268 * periods normally needed for a single APB read/write transaction
269 * without PREADY signal utilized (which is true for the DW APB SSI
270 * controller).
271 */
272 nents = dw_readl(dws, DW_SPI_RXFLR);
273 ns = 4U * NSEC_PER_SEC / dws->max_freq * nents;
274 if (ns <= NSEC_PER_USEC) {
275 delay.unit = SPI_DELAY_UNIT_NSECS;
276 delay.value = ns;
277 } else {
278 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
279 delay.unit = SPI_DELAY_UNIT_USECS;
280 delay.value = clamp_val(us, 0, USHRT_MAX);
281 }
282
283 while (dw_spi_dma_rx_busy(dws) && retry--)
284 spi_delay_exec(&delay, NULL);
285
286 if (retry < 0) {
287 dev_err(&dws->master->dev, "Rx hanged up\n");
288 return -EIO;
289 }
290
291 return 0;
292}
293
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200294/*
295 * dws->dma_chan_busy is set before the dma transfer starts, callback for rx
296 * channel will clear a corresponding bit.
297 */
298static void dw_spi_dma_rx_done(void *arg)
299{
300 struct dw_spi *dws = arg;
301
Andy Shevchenko854d2f22015-03-06 14:42:01 +0200302 clear_bit(RX_BUSY, &dws->dma_chan_busy);
303 if (test_bit(TX_BUSY, &dws->dma_chan_busy))
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200304 return;
Serge Semin0327f0b2020-05-15 13:47:42 +0300305
306 dw_writel(dws, DW_SPI_DMACR, 0);
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300307 complete(&dws->dma_completion);
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200308}
309
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200310static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws,
311 struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200312{
313 struct dma_slave_config rxconf;
314 struct dma_async_tx_descriptor *rxdesc;
315
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200316 if (!xfer->rx_buf)
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200317 return NULL;
318
Andy Shevchenko3cb97e22020-05-06 18:30:18 +0300319 memset(&rxconf, 0, sizeof(rxconf));
Vinod Koula485df42011-10-14 10:47:38 +0530320 rxconf.direction = DMA_DEV_TO_MEM;
Feng Tang7063c0d2010-12-24 13:59:11 +0800321 rxconf.src_addr = dws->dma_addr;
Andy Shevchenkod744f822015-03-09 16:48:50 +0200322 rxconf.src_maxburst = 16;
Feng Tang7063c0d2010-12-24 13:59:11 +0800323 rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Serge Semin4fdc03a2020-05-22 03:07:54 +0300324 rxconf.src_addr_width = convert_dma_width(dws->n_bytes);
Viresh Kumar258aea72012-02-01 16:12:19 +0530325 rxconf.device_fc = false;
Feng Tang7063c0d2010-12-24 13:59:11 +0800326
Andy Shevchenko2a285292014-10-02 16:31:08 +0300327 dmaengine_slave_config(dws->rxchan, &rxconf);
Feng Tang7063c0d2010-12-24 13:59:11 +0800328
Andy Shevchenko2a285292014-10-02 16:31:08 +0300329 rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200330 xfer->rx_sg.sgl,
331 xfer->rx_sg.nents,
Vinod Koula485df42011-10-14 10:47:38 +0530332 DMA_DEV_TO_MEM,
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300333 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Andy Shevchenkoc9dafb22015-03-02 20:15:58 +0200334 if (!rxdesc)
335 return NULL;
336
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200337 rxdesc->callback = dw_spi_dma_rx_done;
Feng Tang7063c0d2010-12-24 13:59:11 +0800338 rxdesc->callback_param = dws;
339
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200340 return rxdesc;
341}
342
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200343static int mid_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200344{
Serge Semin43dba9f2020-05-22 03:07:51 +0300345 u16 imr = 0, dma_ctrl = 0;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200346
Thor Thayerdd114442015-03-12 14:19:31 -0500347 dw_writel(dws, DW_SPI_DMARDLR, 0xf);
348 dw_writel(dws, DW_SPI_DMATDLR, 0x10);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200349
Serge Semin43dba9f2020-05-22 03:07:51 +0300350 if (xfer->tx_buf) {
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200351 dma_ctrl |= SPI_DMA_TDMAE;
Serge Semin43dba9f2020-05-22 03:07:51 +0300352 imr |= SPI_INT_TXOI;
353 }
354 if (xfer->rx_buf) {
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200355 dma_ctrl |= SPI_DMA_RDMAE;
Serge Semin43dba9f2020-05-22 03:07:51 +0300356 imr |= SPI_INT_RXUI | SPI_INT_RXOI;
357 }
Thor Thayerdd114442015-03-12 14:19:31 -0500358 dw_writel(dws, DW_SPI_DMACR, dma_ctrl);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200359
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200360 /* Set the interrupt mask */
Serge Semin43dba9f2020-05-22 03:07:51 +0300361 spi_umask_intr(dws, imr);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200362
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300363 reinit_completion(&dws->dma_completion);
364
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200365 dws->transfer_handler = dma_transfer;
366
Andy Shevchenko9f145382015-03-09 16:48:46 +0200367 return 0;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200368}
369
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200370static int mid_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200371{
372 struct dma_async_tx_descriptor *txdesc, *rxdesc;
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300373 int ret;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200374
Andy Shevchenko9f145382015-03-09 16:48:46 +0200375 /* Prepare the TX dma transfer */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200376 txdesc = dw_spi_dma_prepare_tx(dws, xfer);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200377
Andy Shevchenko9f145382015-03-09 16:48:46 +0200378 /* Prepare the RX dma transfer */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200379 rxdesc = dw_spi_dma_prepare_rx(dws, xfer);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200380
Feng Tang7063c0d2010-12-24 13:59:11 +0800381 /* rx must be started before tx due to spi instinct */
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200382 if (rxdesc) {
383 set_bit(RX_BUSY, &dws->dma_chan_busy);
384 dmaengine_submit(rxdesc);
385 dma_async_issue_pending(dws->rxchan);
386 }
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300387
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200388 if (txdesc) {
389 set_bit(TX_BUSY, &dws->dma_chan_busy);
390 dmaengine_submit(txdesc);
391 dma_async_issue_pending(dws->txchan);
392 }
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300393
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300394 ret = dw_spi_dma_wait(dws, xfer);
395 if (ret)
396 return ret;
397
Serge Semin1ade2d82020-05-29 16:11:53 +0300398 if (txdesc && dws->master->cur_msg->status == -EINPROGRESS) {
399 ret = dw_spi_dma_wait_tx_done(dws, xfer);
400 if (ret)
401 return ret;
402 }
403
Serge Semin33726ef2020-05-29 16:11:54 +0300404 if (rxdesc && dws->master->cur_msg->status == -EINPROGRESS)
405 ret = dw_spi_dma_wait_rx_done(dws);
406
407 return ret;
Feng Tang7063c0d2010-12-24 13:59:11 +0800408}
409
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200410static void mid_spi_dma_stop(struct dw_spi *dws)
411{
412 if (test_bit(TX_BUSY, &dws->dma_chan_busy)) {
Andy Shevchenkocf1716e2017-01-03 15:48:20 +0200413 dmaengine_terminate_sync(dws->txchan);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200414 clear_bit(TX_BUSY, &dws->dma_chan_busy);
415 }
416 if (test_bit(RX_BUSY, &dws->dma_chan_busy)) {
Andy Shevchenkocf1716e2017-01-03 15:48:20 +0200417 dmaengine_terminate_sync(dws->rxchan);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200418 clear_bit(RX_BUSY, &dws->dma_chan_busy);
419 }
Serge Semin0327f0b2020-05-15 13:47:42 +0300420
421 dw_writel(dws, DW_SPI_DMACR, 0);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200422}
423
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300424static const struct dw_spi_dma_ops mfld_dma_ops = {
425 .dma_init = mid_spi_dma_init_mfld,
Feng Tang7063c0d2010-12-24 13:59:11 +0800426 .dma_exit = mid_spi_dma_exit,
Andy Shevchenko9f145382015-03-09 16:48:46 +0200427 .dma_setup = mid_spi_dma_setup,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200428 .can_dma = mid_spi_can_dma,
Feng Tang7063c0d2010-12-24 13:59:11 +0800429 .dma_transfer = mid_spi_dma_transfer,
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200430 .dma_stop = mid_spi_dma_stop,
Feng Tang7063c0d2010-12-24 13:59:11 +0800431};
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300432
433static void dw_spi_mid_setup_dma_mfld(struct dw_spi *dws)
434{
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300435 dws->dma_ops = &mfld_dma_ops;
436}
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300437
438static const struct dw_spi_dma_ops generic_dma_ops = {
439 .dma_init = mid_spi_dma_init_generic,
440 .dma_exit = mid_spi_dma_exit,
441 .dma_setup = mid_spi_dma_setup,
442 .can_dma = mid_spi_can_dma,
443 .dma_transfer = mid_spi_dma_transfer,
444 .dma_stop = mid_spi_dma_stop,
445};
446
447static void dw_spi_mid_setup_dma_generic(struct dw_spi *dws)
448{
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300449 dws->dma_ops = &generic_dma_ops;
450}
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300451#else /* CONFIG_SPI_DW_MID_DMA */
452static inline void dw_spi_mid_setup_dma_mfld(struct dw_spi *dws) {}
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300453static inline void dw_spi_mid_setup_dma_generic(struct dw_spi *dws) {}
Feng Tang7063c0d2010-12-24 13:59:11 +0800454#endif
455
Andy Shevchenkoea092452014-09-12 15:11:59 +0300456/* Some specific info for SPI0 controller on Intel MID */
Feng Tang7063c0d2010-12-24 13:59:11 +0800457
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200458/* HW info for MRST Clk Control Unit, 32b reg per controller */
Feng Tang7063c0d2010-12-24 13:59:11 +0800459#define MRST_SPI_CLK_BASE 100000000 /* 100m */
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200460#define MRST_CLK_SPI_REG 0xff11d86c
Feng Tang7063c0d2010-12-24 13:59:11 +0800461#define CLK_SPI_BDIV_OFFSET 0
462#define CLK_SPI_BDIV_MASK 0x00000007
463#define CLK_SPI_CDIV_OFFSET 9
464#define CLK_SPI_CDIV_MASK 0x00000e00
465#define CLK_SPI_DISABLE_OFFSET 8
466
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300467int dw_spi_mid_init_mfld(struct dw_spi *dws)
Feng Tang7063c0d2010-12-24 13:59:11 +0800468{
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700469 void __iomem *clk_reg;
470 u32 clk_cdiv;
Feng Tang7063c0d2010-12-24 13:59:11 +0800471
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100472 clk_reg = ioremap(MRST_CLK_SPI_REG, 16);
Feng Tang7063c0d2010-12-24 13:59:11 +0800473 if (!clk_reg)
474 return -ENOMEM;
475
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200476 /* Get SPI controller operating freq info */
477 clk_cdiv = readl(clk_reg + dws->bus_num * sizeof(u32));
478 clk_cdiv &= CLK_SPI_CDIV_MASK;
479 clk_cdiv >>= CLK_SPI_CDIV_OFFSET;
Feng Tang7063c0d2010-12-24 13:59:11 +0800480 dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200481
Feng Tang7063c0d2010-12-24 13:59:11 +0800482 iounmap(clk_reg);
483
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800484 /* Register hook to configure CTRLR0 */
485 dws->update_cr0 = dw_spi_update_cr0;
486
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300487 dw_spi_mid_setup_dma_mfld(dws);
Feng Tang7063c0d2010-12-24 13:59:11 +0800488 return 0;
489}
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300490
491int dw_spi_mid_init_generic(struct dw_spi *dws)
492{
493 /* Register hook to configure CTRLR0 */
494 dws->update_cr0 = dw_spi_update_cr0;
495
496 dw_spi_mid_setup_dma_generic(dws);
497 return 0;
498}