blob: 1cf9e3ffe07b1aa694d1facab5f1280cc7c94667 [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
Serge Seminc534df92020-05-29 16:11:55 +030024#define RX_BURST_LEVEL 16
Andy Shevchenko30c8eb52014-10-28 18:25:02 +020025#define TX_BUSY 1
Serge Seminc534df92020-05-29 16:11:55 +030026#define TX_BURST_LEVEL 16
Andy Shevchenko30c8eb52014-10-28 18:25:02 +020027
Feng Tang7063c0d2010-12-24 13:59:11 +080028static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param)
29{
Andy Shevchenkod744f822015-03-09 16:48:50 +020030 struct dw_dma_slave *s = param;
Feng Tang7063c0d2010-12-24 13:59:11 +080031
Andy Shevchenkod744f822015-03-09 16:48:50 +020032 if (s->dma_dev != chan->device->dev)
33 return false;
34
35 chan->private = s;
36 return true;
Feng Tang7063c0d2010-12-24 13:59:11 +080037}
38
Serge Semin0b2b6652020-05-29 16:11:56 +030039static void mid_spi_maxburst_init(struct dw_spi *dws)
40{
41 struct dma_slave_caps caps;
42 u32 max_burst, def_burst;
43 int ret;
44
45 def_burst = dws->fifo_len / 2;
46
47 ret = dma_get_slave_caps(dws->rxchan, &caps);
48 if (!ret && caps.max_burst)
49 max_burst = caps.max_burst;
50 else
51 max_burst = RX_BURST_LEVEL;
52
53 dws->rxburst = min(max_burst, def_burst);
54
55 ret = dma_get_slave_caps(dws->txchan, &caps);
56 if (!ret && caps.max_burst)
57 max_burst = caps.max_burst;
58 else
59 max_burst = TX_BURST_LEVEL;
60
61 dws->txburst = min(max_burst, def_burst);
62}
63
Andy Shevchenko6370aba2020-05-06 18:30:24 +030064static int mid_spi_dma_init_mfld(struct device *dev, struct dw_spi *dws)
Feng Tang7063c0d2010-12-24 13:59:11 +080065{
Serge Semin2afccbd2020-05-22 03:07:52 +030066 struct dw_dma_slave slave = {
67 .src_id = 0,
68 .dst_id = 0
69 };
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030070 struct pci_dev *dma_dev;
Feng Tang7063c0d2010-12-24 13:59:11 +080071 dma_cap_mask_t mask;
72
73 /*
74 * Get pci device for DMA controller, currently it could only
Andy Shevchenkoea092452014-09-12 15:11:59 +030075 * be the DMA controller of Medfield
Feng Tang7063c0d2010-12-24 13:59:11 +080076 */
Andy Shevchenkob89e9c82014-09-12 15:12:00 +030077 dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
78 if (!dma_dev)
79 return -ENODEV;
80
Feng Tang7063c0d2010-12-24 13:59:11 +080081 dma_cap_zero(mask);
82 dma_cap_set(DMA_SLAVE, mask);
83
84 /* 1. Init rx channel */
Serge Semin2afccbd2020-05-22 03:07:52 +030085 slave.dma_dev = &dma_dev->dev;
86 dws->rxchan = dma_request_channel(mask, mid_spi_dma_chan_filter, &slave);
Feng Tang7063c0d2010-12-24 13:59:11 +080087 if (!dws->rxchan)
88 goto err_exit;
Feng Tang7063c0d2010-12-24 13:59:11 +080089
90 /* 2. Init tx channel */
Serge Semin2afccbd2020-05-22 03:07:52 +030091 slave.dst_id = 1;
92 dws->txchan = dma_request_channel(mask, mid_spi_dma_chan_filter, &slave);
Feng Tang7063c0d2010-12-24 13:59:11 +080093 if (!dws->txchan)
94 goto free_rxchan;
Andy Shevchenkoa041e672020-05-07 14:54:49 +030095
96 dws->master->dma_rx = dws->rxchan;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +020097 dws->master->dma_tx = dws->txchan;
Feng Tang7063c0d2010-12-24 13:59:11 +080098
Serge Seminbdbdf0f2020-05-29 16:11:52 +030099 init_completion(&dws->dma_completion);
100
Serge Semin0b2b6652020-05-29 16:11:56 +0300101 mid_spi_maxburst_init(dws);
102
Feng Tang7063c0d2010-12-24 13:59:11 +0800103 return 0;
104
105free_rxchan:
106 dma_release_channel(dws->rxchan);
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300107 dws->rxchan = NULL;
Feng Tang7063c0d2010-12-24 13:59:11 +0800108err_exit:
Andy Shevchenkob89e9c82014-09-12 15:12:00 +0300109 return -EBUSY;
Feng Tang7063c0d2010-12-24 13:59:11 +0800110}
111
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300112static int mid_spi_dma_init_generic(struct device *dev, struct dw_spi *dws)
113{
114 dws->rxchan = dma_request_slave_channel(dev, "rx");
115 if (!dws->rxchan)
116 return -ENODEV;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300117
118 dws->txchan = dma_request_slave_channel(dev, "tx");
119 if (!dws->txchan) {
120 dma_release_channel(dws->rxchan);
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300121 dws->rxchan = NULL;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300122 return -ENODEV;
123 }
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300124
125 dws->master->dma_rx = dws->rxchan;
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300126 dws->master->dma_tx = dws->txchan;
127
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300128 init_completion(&dws->dma_completion);
129
Serge Semin0b2b6652020-05-29 16:11:56 +0300130 mid_spi_maxburst_init(dws);
131
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300132 return 0;
133}
134
Feng Tang7063c0d2010-12-24 13:59:11 +0800135static void mid_spi_dma_exit(struct dw_spi *dws)
136{
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300137 if (dws->txchan) {
138 dmaengine_terminate_sync(dws->txchan);
139 dma_release_channel(dws->txchan);
140 }
Andy Shevchenko8e45ef62014-09-18 20:08:53 +0300141
Andy Shevchenkoa041e672020-05-07 14:54:49 +0300142 if (dws->rxchan) {
143 dmaengine_terminate_sync(dws->rxchan);
144 dma_release_channel(dws->rxchan);
145 }
Serge Semin0327f0b2020-05-15 13:47:42 +0300146
147 dw_writel(dws, DW_SPI_DMACR, 0);
Feng Tang7063c0d2010-12-24 13:59:11 +0800148}
149
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200150static irqreturn_t dma_transfer(struct dw_spi *dws)
151{
Thor Thayerdd114442015-03-12 14:19:31 -0500152 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200153
154 if (!irq_status)
155 return IRQ_NONE;
156
Thor Thayerdd114442015-03-12 14:19:31 -0500157 dw_readl(dws, DW_SPI_ICR);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200158 spi_reset_chip(dws);
159
160 dev_err(&dws->master->dev, "%s: FIFO overrun/underrun\n", __func__);
161 dws->master->cur_msg->status = -EIO;
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300162 complete(&dws->dma_completion);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200163 return IRQ_HANDLED;
164}
165
Jarkko Nikula721483e2018-02-01 17:17:29 +0200166static bool mid_spi_can_dma(struct spi_controller *master,
167 struct spi_device *spi, struct spi_transfer *xfer)
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200168{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200169 struct dw_spi *dws = spi_controller_get_devdata(master);
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200170
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200171 return xfer->len > dws->fifo_len;
172}
173
Serge Semin4fdc03a2020-05-22 03:07:54 +0300174static enum dma_slave_buswidth convert_dma_width(u8 n_bytes) {
175 if (n_bytes == 1)
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200176 return DMA_SLAVE_BUSWIDTH_1_BYTE;
Serge Semin4fdc03a2020-05-22 03:07:54 +0300177 else if (n_bytes == 2)
Andy Shevchenkoe31abce2015-03-09 16:48:45 +0200178 return DMA_SLAVE_BUSWIDTH_2_BYTES;
179
180 return DMA_SLAVE_BUSWIDTH_UNDEFINED;
181}
182
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300183static int dw_spi_dma_wait(struct dw_spi *dws, struct spi_transfer *xfer)
184{
185 unsigned long long ms;
186
187 ms = xfer->len * MSEC_PER_SEC * BITS_PER_BYTE;
188 do_div(ms, xfer->effective_speed_hz);
189 ms += ms + 200;
190
191 if (ms > UINT_MAX)
192 ms = UINT_MAX;
193
194 ms = wait_for_completion_timeout(&dws->dma_completion,
195 msecs_to_jiffies(ms));
196
197 if (ms == 0) {
198 dev_err(&dws->master->cur_msg->spi->dev,
199 "DMA transaction timed out\n");
200 return -ETIMEDOUT;
201 }
202
203 return 0;
204}
205
Serge Semin1ade2d82020-05-29 16:11:53 +0300206static inline bool dw_spi_dma_tx_busy(struct dw_spi *dws)
207{
208 return !(dw_readl(dws, DW_SPI_SR) & SR_TF_EMPT);
209}
210
211static int dw_spi_dma_wait_tx_done(struct dw_spi *dws,
212 struct spi_transfer *xfer)
213{
214 int retry = WAIT_RETRIES;
215 struct spi_delay delay;
216 u32 nents;
217
218 nents = dw_readl(dws, DW_SPI_TXFLR);
219 delay.unit = SPI_DELAY_UNIT_SCK;
220 delay.value = nents * dws->n_bytes * BITS_PER_BYTE;
221
222 while (dw_spi_dma_tx_busy(dws) && retry--)
223 spi_delay_exec(&delay, xfer);
224
225 if (retry < 0) {
226 dev_err(&dws->master->dev, "Tx hanged up\n");
227 return -EIO;
228 }
229
230 return 0;
231}
232
Feng Tang7063c0d2010-12-24 13:59:11 +0800233/*
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200234 * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
235 * channel will clear a corresponding bit.
Feng Tang7063c0d2010-12-24 13:59:11 +0800236 */
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200237static void dw_spi_dma_tx_done(void *arg)
Feng Tang7063c0d2010-12-24 13:59:11 +0800238{
239 struct dw_spi *dws = arg;
240
Andy Shevchenko854d2f22015-03-06 14:42:01 +0200241 clear_bit(TX_BUSY, &dws->dma_chan_busy);
242 if (test_bit(RX_BUSY, &dws->dma_chan_busy))
Feng Tang7063c0d2010-12-24 13:59:11 +0800243 return;
Serge Semin0327f0b2020-05-15 13:47:42 +0300244
245 dw_writel(dws, DW_SPI_DMACR, 0);
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300246 complete(&dws->dma_completion);
Feng Tang7063c0d2010-12-24 13:59:11 +0800247}
248
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200249static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws,
250 struct spi_transfer *xfer)
Feng Tang7063c0d2010-12-24 13:59:11 +0800251{
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200252 struct dma_slave_config txconf;
253 struct dma_async_tx_descriptor *txdesc;
Feng Tang7063c0d2010-12-24 13:59:11 +0800254
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200255 if (!xfer->tx_buf)
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200256 return NULL;
257
Andy Shevchenko3cb97e22020-05-06 18:30:18 +0300258 memset(&txconf, 0, sizeof(txconf));
Vinod Koula485df42011-10-14 10:47:38 +0530259 txconf.direction = DMA_MEM_TO_DEV;
Feng Tang7063c0d2010-12-24 13:59:11 +0800260 txconf.dst_addr = dws->dma_addr;
Serge Semin0b2b6652020-05-29 16:11:56 +0300261 txconf.dst_maxburst = dws->txburst;
Feng Tang7063c0d2010-12-24 13:59:11 +0800262 txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Serge Semin4fdc03a2020-05-22 03:07:54 +0300263 txconf.dst_addr_width = convert_dma_width(dws->n_bytes);
Viresh Kumar258aea72012-02-01 16:12:19 +0530264 txconf.device_fc = false;
Feng Tang7063c0d2010-12-24 13:59:11 +0800265
Andy Shevchenko2a285292014-10-02 16:31:08 +0300266 dmaengine_slave_config(dws->txchan, &txconf);
Feng Tang7063c0d2010-12-24 13:59:11 +0800267
Andy Shevchenko2a285292014-10-02 16:31:08 +0300268 txdesc = dmaengine_prep_slave_sg(dws->txchan,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200269 xfer->tx_sg.sgl,
270 xfer->tx_sg.nents,
Vinod Koula485df42011-10-14 10:47:38 +0530271 DMA_MEM_TO_DEV,
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300272 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Andy Shevchenkoc9dafb22015-03-02 20:15:58 +0200273 if (!txdesc)
274 return NULL;
275
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200276 txdesc->callback = dw_spi_dma_tx_done;
Feng Tang7063c0d2010-12-24 13:59:11 +0800277 txdesc->callback_param = dws;
278
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200279 return txdesc;
280}
281
Serge Semin33726ef2020-05-29 16:11:54 +0300282static inline bool dw_spi_dma_rx_busy(struct dw_spi *dws)
283{
284 return !!(dw_readl(dws, DW_SPI_SR) & SR_RF_NOT_EMPT);
285}
286
287static int dw_spi_dma_wait_rx_done(struct dw_spi *dws)
288{
289 int retry = WAIT_RETRIES;
290 struct spi_delay delay;
291 unsigned long ns, us;
292 u32 nents;
293
294 /*
295 * It's unlikely that DMA engine is still doing the data fetching, but
296 * if it's let's give it some reasonable time. The timeout calculation
297 * is based on the synchronous APB/SSI reference clock rate, on a
298 * number of data entries left in the Rx FIFO, times a number of clock
299 * periods normally needed for a single APB read/write transaction
300 * without PREADY signal utilized (which is true for the DW APB SSI
301 * controller).
302 */
303 nents = dw_readl(dws, DW_SPI_RXFLR);
304 ns = 4U * NSEC_PER_SEC / dws->max_freq * nents;
305 if (ns <= NSEC_PER_USEC) {
306 delay.unit = SPI_DELAY_UNIT_NSECS;
307 delay.value = ns;
308 } else {
309 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
310 delay.unit = SPI_DELAY_UNIT_USECS;
311 delay.value = clamp_val(us, 0, USHRT_MAX);
312 }
313
314 while (dw_spi_dma_rx_busy(dws) && retry--)
315 spi_delay_exec(&delay, NULL);
316
317 if (retry < 0) {
318 dev_err(&dws->master->dev, "Rx hanged up\n");
319 return -EIO;
320 }
321
322 return 0;
323}
324
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200325/*
326 * dws->dma_chan_busy is set before the dma transfer starts, callback for rx
327 * channel will clear a corresponding bit.
328 */
329static void dw_spi_dma_rx_done(void *arg)
330{
331 struct dw_spi *dws = arg;
332
Andy Shevchenko854d2f22015-03-06 14:42:01 +0200333 clear_bit(RX_BUSY, &dws->dma_chan_busy);
334 if (test_bit(TX_BUSY, &dws->dma_chan_busy))
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200335 return;
Serge Semin0327f0b2020-05-15 13:47:42 +0300336
337 dw_writel(dws, DW_SPI_DMACR, 0);
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300338 complete(&dws->dma_completion);
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200339}
340
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200341static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws,
342 struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200343{
344 struct dma_slave_config rxconf;
345 struct dma_async_tx_descriptor *rxdesc;
346
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200347 if (!xfer->rx_buf)
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200348 return NULL;
349
Andy Shevchenko3cb97e22020-05-06 18:30:18 +0300350 memset(&rxconf, 0, sizeof(rxconf));
Vinod Koula485df42011-10-14 10:47:38 +0530351 rxconf.direction = DMA_DEV_TO_MEM;
Feng Tang7063c0d2010-12-24 13:59:11 +0800352 rxconf.src_addr = dws->dma_addr;
Serge Semin0b2b6652020-05-29 16:11:56 +0300353 rxconf.src_maxburst = dws->rxburst;
Feng Tang7063c0d2010-12-24 13:59:11 +0800354 rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Serge Semin4fdc03a2020-05-22 03:07:54 +0300355 rxconf.src_addr_width = convert_dma_width(dws->n_bytes);
Viresh Kumar258aea72012-02-01 16:12:19 +0530356 rxconf.device_fc = false;
Feng Tang7063c0d2010-12-24 13:59:11 +0800357
Andy Shevchenko2a285292014-10-02 16:31:08 +0300358 dmaengine_slave_config(dws->rxchan, &rxconf);
Feng Tang7063c0d2010-12-24 13:59:11 +0800359
Andy Shevchenko2a285292014-10-02 16:31:08 +0300360 rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200361 xfer->rx_sg.sgl,
362 xfer->rx_sg.nents,
Vinod Koula485df42011-10-14 10:47:38 +0530363 DMA_DEV_TO_MEM,
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300364 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Andy Shevchenkoc9dafb22015-03-02 20:15:58 +0200365 if (!rxdesc)
366 return NULL;
367
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200368 rxdesc->callback = dw_spi_dma_rx_done;
Feng Tang7063c0d2010-12-24 13:59:11 +0800369 rxdesc->callback_param = dws;
370
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200371 return rxdesc;
372}
373
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200374static int mid_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200375{
Serge Semin43dba9f2020-05-22 03:07:51 +0300376 u16 imr = 0, dma_ctrl = 0;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200377
Serge Semin0b2b6652020-05-29 16:11:56 +0300378 dw_writel(dws, DW_SPI_DMARDLR, dws->rxburst - 1);
379 dw_writel(dws, DW_SPI_DMATDLR, dws->fifo_len - dws->txburst);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200380
Serge Semin43dba9f2020-05-22 03:07:51 +0300381 if (xfer->tx_buf) {
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200382 dma_ctrl |= SPI_DMA_TDMAE;
Serge Semin43dba9f2020-05-22 03:07:51 +0300383 imr |= SPI_INT_TXOI;
384 }
385 if (xfer->rx_buf) {
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200386 dma_ctrl |= SPI_DMA_RDMAE;
Serge Semin43dba9f2020-05-22 03:07:51 +0300387 imr |= SPI_INT_RXUI | SPI_INT_RXOI;
388 }
Thor Thayerdd114442015-03-12 14:19:31 -0500389 dw_writel(dws, DW_SPI_DMACR, dma_ctrl);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200390
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200391 /* Set the interrupt mask */
Serge Semin43dba9f2020-05-22 03:07:51 +0300392 spi_umask_intr(dws, imr);
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200393
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300394 reinit_completion(&dws->dma_completion);
395
Andy Shevchenkof051fc82015-03-09 16:48:47 +0200396 dws->transfer_handler = dma_transfer;
397
Andy Shevchenko9f145382015-03-09 16:48:46 +0200398 return 0;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200399}
400
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200401static int mid_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer)
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200402{
403 struct dma_async_tx_descriptor *txdesc, *rxdesc;
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300404 int ret;
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200405
Andy Shevchenko9f145382015-03-09 16:48:46 +0200406 /* Prepare the TX dma transfer */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200407 txdesc = dw_spi_dma_prepare_tx(dws, xfer);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200408
Andy Shevchenko9f145382015-03-09 16:48:46 +0200409 /* Prepare the RX dma transfer */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200410 rxdesc = dw_spi_dma_prepare_rx(dws, xfer);
Andy Shevchenkoa5c2db92014-10-28 18:25:01 +0200411
Feng Tang7063c0d2010-12-24 13:59:11 +0800412 /* rx must be started before tx due to spi instinct */
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200413 if (rxdesc) {
414 set_bit(RX_BUSY, &dws->dma_chan_busy);
415 dmaengine_submit(rxdesc);
416 dma_async_issue_pending(dws->rxchan);
417 }
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300418
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200419 if (txdesc) {
420 set_bit(TX_BUSY, &dws->dma_chan_busy);
421 dmaengine_submit(txdesc);
422 dma_async_issue_pending(dws->txchan);
423 }
Andy Shevchenkof7477c22014-10-02 16:31:09 +0300424
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300425 ret = dw_spi_dma_wait(dws, xfer);
426 if (ret)
427 return ret;
428
Serge Semin1ade2d82020-05-29 16:11:53 +0300429 if (txdesc && dws->master->cur_msg->status == -EINPROGRESS) {
430 ret = dw_spi_dma_wait_tx_done(dws, xfer);
431 if (ret)
432 return ret;
433 }
434
Serge Semin33726ef2020-05-29 16:11:54 +0300435 if (rxdesc && dws->master->cur_msg->status == -EINPROGRESS)
436 ret = dw_spi_dma_wait_rx_done(dws);
437
438 return ret;
Feng Tang7063c0d2010-12-24 13:59:11 +0800439}
440
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200441static void mid_spi_dma_stop(struct dw_spi *dws)
442{
443 if (test_bit(TX_BUSY, &dws->dma_chan_busy)) {
Andy Shevchenkocf1716e2017-01-03 15:48:20 +0200444 dmaengine_terminate_sync(dws->txchan);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200445 clear_bit(TX_BUSY, &dws->dma_chan_busy);
446 }
447 if (test_bit(RX_BUSY, &dws->dma_chan_busy)) {
Andy Shevchenkocf1716e2017-01-03 15:48:20 +0200448 dmaengine_terminate_sync(dws->rxchan);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200449 clear_bit(RX_BUSY, &dws->dma_chan_busy);
450 }
Serge Semin0327f0b2020-05-15 13:47:42 +0300451
452 dw_writel(dws, DW_SPI_DMACR, 0);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200453}
454
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300455static const struct dw_spi_dma_ops mfld_dma_ops = {
456 .dma_init = mid_spi_dma_init_mfld,
Feng Tang7063c0d2010-12-24 13:59:11 +0800457 .dma_exit = mid_spi_dma_exit,
Andy Shevchenko9f145382015-03-09 16:48:46 +0200458 .dma_setup = mid_spi_dma_setup,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200459 .can_dma = mid_spi_can_dma,
Feng Tang7063c0d2010-12-24 13:59:11 +0800460 .dma_transfer = mid_spi_dma_transfer,
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200461 .dma_stop = mid_spi_dma_stop,
Feng Tang7063c0d2010-12-24 13:59:11 +0800462};
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300463
464static void dw_spi_mid_setup_dma_mfld(struct dw_spi *dws)
465{
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300466 dws->dma_ops = &mfld_dma_ops;
467}
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300468
469static const struct dw_spi_dma_ops generic_dma_ops = {
470 .dma_init = mid_spi_dma_init_generic,
471 .dma_exit = mid_spi_dma_exit,
472 .dma_setup = mid_spi_dma_setup,
473 .can_dma = mid_spi_can_dma,
474 .dma_transfer = mid_spi_dma_transfer,
475 .dma_stop = mid_spi_dma_stop,
476};
477
478static void dw_spi_mid_setup_dma_generic(struct dw_spi *dws)
479{
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300480 dws->dma_ops = &generic_dma_ops;
481}
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300482#else /* CONFIG_SPI_DW_MID_DMA */
483static inline void dw_spi_mid_setup_dma_mfld(struct dw_spi *dws) {}
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300484static inline void dw_spi_mid_setup_dma_generic(struct dw_spi *dws) {}
Feng Tang7063c0d2010-12-24 13:59:11 +0800485#endif
486
Andy Shevchenkoea092452014-09-12 15:11:59 +0300487/* Some specific info for SPI0 controller on Intel MID */
Feng Tang7063c0d2010-12-24 13:59:11 +0800488
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200489/* HW info for MRST Clk Control Unit, 32b reg per controller */
Feng Tang7063c0d2010-12-24 13:59:11 +0800490#define MRST_SPI_CLK_BASE 100000000 /* 100m */
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200491#define MRST_CLK_SPI_REG 0xff11d86c
Feng Tang7063c0d2010-12-24 13:59:11 +0800492#define CLK_SPI_BDIV_OFFSET 0
493#define CLK_SPI_BDIV_MASK 0x00000007
494#define CLK_SPI_CDIV_OFFSET 9
495#define CLK_SPI_CDIV_MASK 0x00000e00
496#define CLK_SPI_DISABLE_OFFSET 8
497
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300498int dw_spi_mid_init_mfld(struct dw_spi *dws)
Feng Tang7063c0d2010-12-24 13:59:11 +0800499{
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700500 void __iomem *clk_reg;
501 u32 clk_cdiv;
Feng Tang7063c0d2010-12-24 13:59:11 +0800502
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100503 clk_reg = ioremap(MRST_CLK_SPI_REG, 16);
Feng Tang7063c0d2010-12-24 13:59:11 +0800504 if (!clk_reg)
505 return -ENOMEM;
506
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200507 /* Get SPI controller operating freq info */
508 clk_cdiv = readl(clk_reg + dws->bus_num * sizeof(u32));
509 clk_cdiv &= CLK_SPI_CDIV_MASK;
510 clk_cdiv >>= CLK_SPI_CDIV_OFFSET;
Feng Tang7063c0d2010-12-24 13:59:11 +0800511 dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200512
Feng Tang7063c0d2010-12-24 13:59:11 +0800513 iounmap(clk_reg);
514
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800515 /* Register hook to configure CTRLR0 */
516 dws->update_cr0 = dw_spi_update_cr0;
517
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300518 dw_spi_mid_setup_dma_mfld(dws);
Feng Tang7063c0d2010-12-24 13:59:11 +0800519 return 0;
520}
Jarkko Nikula22d48ad2020-05-06 18:30:25 +0300521
522int dw_spi_mid_init_generic(struct dw_spi *dws)
523{
524 /* Register hook to configure CTRLR0 */
525 dws->update_cr0 = dw_spi_update_cr0;
526
527 dw_spi_mid_setup_dma_generic(dws);
528 return 0;
529}