Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 2 | /* |
Serge Semin | 6c710c0 | 2020-05-29 16:11:59 +0300 | [diff] [blame] | 3 | * Special handling for DW DMA core |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 4 | * |
Andy Shevchenko | 197e96b | 2014-09-12 15:12:01 +0300 | [diff] [blame] | 5 | * Copyright (c) 2009, 2014 Intel Corporation. |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 8 | #include <linux/completion.h> |
Andy Shevchenko | e794095 | 2020-05-06 18:30:22 +0300 | [diff] [blame] | 9 | #include <linux/dma-mapping.h> |
| 10 | #include <linux/dmaengine.h> |
Andy Shevchenko | e62a15d | 2020-05-06 18:30:21 +0300 | [diff] [blame] | 11 | #include <linux/irqreturn.h> |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 12 | #include <linux/jiffies.h> |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 13 | #include <linux/pci.h> |
Andy Shevchenko | d744f82 | 2015-03-09 16:48:50 +0200 | [diff] [blame] | 14 | #include <linux/platform_data/dma-dw.h> |
Serge Semin | 6c710c0 | 2020-05-29 16:11:59 +0300 | [diff] [blame] | 15 | #include <linux/spi/spi.h> |
| 16 | #include <linux/types.h> |
| 17 | |
| 18 | #include "spi-dw.h" |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 19 | |
Serge Semin | 1ade2d8 | 2020-05-29 16:11:53 +0300 | [diff] [blame] | 20 | #define WAIT_RETRIES 5 |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 21 | #define RX_BUSY 0 |
Serge Semin | c534df9 | 2020-05-29 16:11:55 +0300 | [diff] [blame] | 22 | #define RX_BURST_LEVEL 16 |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 23 | #define TX_BUSY 1 |
Serge Semin | c534df9 | 2020-05-29 16:11:55 +0300 | [diff] [blame] | 24 | #define TX_BURST_LEVEL 16 |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 25 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 26 | static bool dw_spi_dma_chan_filter(struct dma_chan *chan, void *param) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 27 | { |
Andy Shevchenko | d744f82 | 2015-03-09 16:48:50 +0200 | [diff] [blame] | 28 | struct dw_dma_slave *s = param; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 29 | |
Andy Shevchenko | d744f82 | 2015-03-09 16:48:50 +0200 | [diff] [blame] | 30 | if (s->dma_dev != chan->device->dev) |
| 31 | return false; |
| 32 | |
| 33 | chan->private = s; |
| 34 | return true; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 35 | } |
| 36 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 37 | static void dw_spi_dma_maxburst_init(struct dw_spi *dws) |
Serge Semin | 0b2b665 | 2020-05-29 16:11:56 +0300 | [diff] [blame] | 38 | { |
| 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); |
Serge Semin | 01ddbbb | 2020-09-20 14:23:12 +0300 | [diff] [blame] | 52 | dw_writel(dws, DW_SPI_DMARDLR, dws->rxburst - 1); |
Serge Semin | 0b2b665 | 2020-05-29 16:11:56 +0300 | [diff] [blame] | 53 | |
| 54 | ret = dma_get_slave_caps(dws->txchan, &caps); |
| 55 | if (!ret && caps.max_burst) |
| 56 | max_burst = caps.max_burst; |
| 57 | else |
| 58 | max_burst = TX_BURST_LEVEL; |
| 59 | |
Serge Semin | 01ddbbb | 2020-09-20 14:23:12 +0300 | [diff] [blame] | 60 | /* |
| 61 | * Having a Rx DMA channel serviced with higher priority than a Tx DMA |
| 62 | * channel might not be enough to provide a well balanced DMA-based |
| 63 | * SPI transfer interface. There might still be moments when the Tx DMA |
| 64 | * channel is occasionally handled faster than the Rx DMA channel. |
| 65 | * That in its turn will eventually cause the SPI Rx FIFO overflow if |
| 66 | * SPI bus speed is high enough to fill the SPI Rx FIFO in before it's |
| 67 | * cleared by the Rx DMA channel. In order to fix the problem the Tx |
| 68 | * DMA activity is intentionally slowed down by limiting the SPI Tx |
| 69 | * FIFO depth with a value twice bigger than the Tx burst length. |
| 70 | */ |
Serge Semin | 0b2b665 | 2020-05-29 16:11:56 +0300 | [diff] [blame] | 71 | dws->txburst = min(max_burst, def_burst); |
Serge Semin | 01ddbbb | 2020-09-20 14:23:12 +0300 | [diff] [blame] | 72 | dw_writel(dws, DW_SPI_DMATDLR, dws->txburst); |
Serge Semin | 0b2b665 | 2020-05-29 16:11:56 +0300 | [diff] [blame] | 73 | } |
| 74 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 75 | static int dw_spi_dma_init_mfld(struct device *dev, struct dw_spi *dws) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 76 | { |
Andy Shevchenko | b3f82dc | 2020-05-29 21:31:49 +0300 | [diff] [blame] | 77 | struct dw_dma_slave dma_tx = { .dst_id = 1 }, *tx = &dma_tx; |
| 78 | struct dw_dma_slave dma_rx = { .src_id = 0 }, *rx = &dma_rx; |
Andy Shevchenko | b89e9c8 | 2014-09-12 15:12:00 +0300 | [diff] [blame] | 79 | struct pci_dev *dma_dev; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 80 | dma_cap_mask_t mask; |
| 81 | |
| 82 | /* |
| 83 | * Get pci device for DMA controller, currently it could only |
Andy Shevchenko | ea09245 | 2014-09-12 15:11:59 +0300 | [diff] [blame] | 84 | * be the DMA controller of Medfield |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 85 | */ |
Andy Shevchenko | b89e9c8 | 2014-09-12 15:12:00 +0300 | [diff] [blame] | 86 | dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL); |
| 87 | if (!dma_dev) |
| 88 | return -ENODEV; |
| 89 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 90 | dma_cap_zero(mask); |
| 91 | dma_cap_set(DMA_SLAVE, mask); |
| 92 | |
| 93 | /* 1. Init rx channel */ |
Andy Shevchenko | b3f82dc | 2020-05-29 21:31:49 +0300 | [diff] [blame] | 94 | rx->dma_dev = &dma_dev->dev; |
| 95 | dws->rxchan = dma_request_channel(mask, dw_spi_dma_chan_filter, rx); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 96 | if (!dws->rxchan) |
| 97 | goto err_exit; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 98 | |
| 99 | /* 2. Init tx channel */ |
Andy Shevchenko | b3f82dc | 2020-05-29 21:31:49 +0300 | [diff] [blame] | 100 | tx->dma_dev = &dma_dev->dev; |
| 101 | dws->txchan = dma_request_channel(mask, dw_spi_dma_chan_filter, tx); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 102 | if (!dws->txchan) |
| 103 | goto free_rxchan; |
Andy Shevchenko | a041e67 | 2020-05-07 14:54:49 +0300 | [diff] [blame] | 104 | |
| 105 | dws->master->dma_rx = dws->rxchan; |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 106 | dws->master->dma_tx = dws->txchan; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 107 | |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 108 | init_completion(&dws->dma_completion); |
| 109 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 110 | dw_spi_dma_maxburst_init(dws); |
Serge Semin | 0b2b665 | 2020-05-29 16:11:56 +0300 | [diff] [blame] | 111 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 112 | return 0; |
| 113 | |
| 114 | free_rxchan: |
| 115 | dma_release_channel(dws->rxchan); |
Andy Shevchenko | a041e67 | 2020-05-07 14:54:49 +0300 | [diff] [blame] | 116 | dws->rxchan = NULL; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 117 | err_exit: |
Andy Shevchenko | b89e9c8 | 2014-09-12 15:12:00 +0300 | [diff] [blame] | 118 | return -EBUSY; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 119 | } |
| 120 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 121 | static int dw_spi_dma_init_generic(struct device *dev, struct dw_spi *dws) |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 122 | { |
| 123 | dws->rxchan = dma_request_slave_channel(dev, "rx"); |
| 124 | if (!dws->rxchan) |
| 125 | return -ENODEV; |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 126 | |
| 127 | dws->txchan = dma_request_slave_channel(dev, "tx"); |
| 128 | if (!dws->txchan) { |
| 129 | dma_release_channel(dws->rxchan); |
Andy Shevchenko | a041e67 | 2020-05-07 14:54:49 +0300 | [diff] [blame] | 130 | dws->rxchan = NULL; |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 131 | return -ENODEV; |
| 132 | } |
Andy Shevchenko | a041e67 | 2020-05-07 14:54:49 +0300 | [diff] [blame] | 133 | |
| 134 | dws->master->dma_rx = dws->rxchan; |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 135 | dws->master->dma_tx = dws->txchan; |
| 136 | |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 137 | init_completion(&dws->dma_completion); |
| 138 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 139 | dw_spi_dma_maxburst_init(dws); |
Serge Semin | 0b2b665 | 2020-05-29 16:11:56 +0300 | [diff] [blame] | 140 | |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 144 | static void dw_spi_dma_exit(struct dw_spi *dws) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 145 | { |
Andy Shevchenko | a041e67 | 2020-05-07 14:54:49 +0300 | [diff] [blame] | 146 | if (dws->txchan) { |
| 147 | dmaengine_terminate_sync(dws->txchan); |
| 148 | dma_release_channel(dws->txchan); |
| 149 | } |
Andy Shevchenko | 8e45ef6 | 2014-09-18 20:08:53 +0300 | [diff] [blame] | 150 | |
Andy Shevchenko | a041e67 | 2020-05-07 14:54:49 +0300 | [diff] [blame] | 151 | if (dws->rxchan) { |
| 152 | dmaengine_terminate_sync(dws->rxchan); |
| 153 | dma_release_channel(dws->rxchan); |
| 154 | } |
Serge Semin | 0327f0b | 2020-05-15 13:47:42 +0300 | [diff] [blame] | 155 | |
| 156 | dw_writel(dws, DW_SPI_DMACR, 0); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 159 | static irqreturn_t dw_spi_dma_transfer_handler(struct dw_spi *dws) |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 160 | { |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 161 | u16 irq_status = dw_readl(dws, DW_SPI_ISR); |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 162 | |
| 163 | if (!irq_status) |
| 164 | return IRQ_NONE; |
| 165 | |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 166 | dw_readl(dws, DW_SPI_ICR); |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 167 | spi_reset_chip(dws); |
| 168 | |
| 169 | dev_err(&dws->master->dev, "%s: FIFO overrun/underrun\n", __func__); |
| 170 | dws->master->cur_msg->status = -EIO; |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 171 | complete(&dws->dma_completion); |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 172 | return IRQ_HANDLED; |
| 173 | } |
| 174 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 175 | static bool dw_spi_can_dma(struct spi_controller *master, |
| 176 | struct spi_device *spi, struct spi_transfer *xfer) |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 177 | { |
Jarkko Nikula | 721483e | 2018-02-01 17:17:29 +0200 | [diff] [blame] | 178 | struct dw_spi *dws = spi_controller_get_devdata(master); |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 179 | |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 180 | return xfer->len > dws->fifo_len; |
| 181 | } |
| 182 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 183 | static enum dma_slave_buswidth dw_spi_dma_convert_width(u8 n_bytes) |
| 184 | { |
Serge Semin | 4fdc03a | 2020-05-22 03:07:54 +0300 | [diff] [blame] | 185 | if (n_bytes == 1) |
Andy Shevchenko | e31abce | 2015-03-09 16:48:45 +0200 | [diff] [blame] | 186 | return DMA_SLAVE_BUSWIDTH_1_BYTE; |
Serge Semin | 4fdc03a | 2020-05-22 03:07:54 +0300 | [diff] [blame] | 187 | else if (n_bytes == 2) |
Andy Shevchenko | e31abce | 2015-03-09 16:48:45 +0200 | [diff] [blame] | 188 | return DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 189 | |
| 190 | return DMA_SLAVE_BUSWIDTH_UNDEFINED; |
| 191 | } |
| 192 | |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 193 | static int dw_spi_dma_wait(struct dw_spi *dws, struct spi_transfer *xfer) |
| 194 | { |
| 195 | unsigned long long ms; |
| 196 | |
| 197 | ms = xfer->len * MSEC_PER_SEC * BITS_PER_BYTE; |
| 198 | do_div(ms, xfer->effective_speed_hz); |
| 199 | ms += ms + 200; |
| 200 | |
| 201 | if (ms > UINT_MAX) |
| 202 | ms = UINT_MAX; |
| 203 | |
| 204 | ms = wait_for_completion_timeout(&dws->dma_completion, |
| 205 | msecs_to_jiffies(ms)); |
| 206 | |
| 207 | if (ms == 0) { |
| 208 | dev_err(&dws->master->cur_msg->spi->dev, |
| 209 | "DMA transaction timed out\n"); |
| 210 | return -ETIMEDOUT; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Serge Semin | 1ade2d8 | 2020-05-29 16:11:53 +0300 | [diff] [blame] | 216 | static inline bool dw_spi_dma_tx_busy(struct dw_spi *dws) |
| 217 | { |
| 218 | return !(dw_readl(dws, DW_SPI_SR) & SR_TF_EMPT); |
| 219 | } |
| 220 | |
| 221 | static int dw_spi_dma_wait_tx_done(struct dw_spi *dws, |
| 222 | struct spi_transfer *xfer) |
| 223 | { |
| 224 | int retry = WAIT_RETRIES; |
| 225 | struct spi_delay delay; |
| 226 | u32 nents; |
| 227 | |
| 228 | nents = dw_readl(dws, DW_SPI_TXFLR); |
| 229 | delay.unit = SPI_DELAY_UNIT_SCK; |
| 230 | delay.value = nents * dws->n_bytes * BITS_PER_BYTE; |
| 231 | |
| 232 | while (dw_spi_dma_tx_busy(dws) && retry--) |
| 233 | spi_delay_exec(&delay, xfer); |
| 234 | |
| 235 | if (retry < 0) { |
| 236 | dev_err(&dws->master->dev, "Tx hanged up\n"); |
| 237 | return -EIO; |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 243 | /* |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 244 | * dws->dma_chan_busy is set before the dma transfer starts, callback for tx |
| 245 | * channel will clear a corresponding bit. |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 246 | */ |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 247 | static void dw_spi_dma_tx_done(void *arg) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 248 | { |
| 249 | struct dw_spi *dws = arg; |
| 250 | |
Andy Shevchenko | 854d2f2 | 2015-03-06 14:42:01 +0200 | [diff] [blame] | 251 | clear_bit(TX_BUSY, &dws->dma_chan_busy); |
| 252 | if (test_bit(RX_BUSY, &dws->dma_chan_busy)) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 253 | return; |
Serge Semin | 0327f0b | 2020-05-15 13:47:42 +0300 | [diff] [blame] | 254 | |
| 255 | dw_writel(dws, DW_SPI_DMACR, 0); |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 256 | complete(&dws->dma_completion); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 257 | } |
| 258 | |
Serge Semin | a874d81 | 2020-09-20 14:23:14 +0300 | [diff] [blame^] | 259 | static int dw_spi_dma_config_tx(struct dw_spi *dws) |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 260 | { |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 261 | struct dma_slave_config txconf; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 262 | |
Andy Shevchenko | 3cb97e2 | 2020-05-06 18:30:18 +0300 | [diff] [blame] | 263 | memset(&txconf, 0, sizeof(txconf)); |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 264 | txconf.direction = DMA_MEM_TO_DEV; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 265 | txconf.dst_addr = dws->dma_addr; |
Serge Semin | 0b2b665 | 2020-05-29 16:11:56 +0300 | [diff] [blame] | 266 | txconf.dst_maxburst = dws->txburst; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 267 | txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 268 | txconf.dst_addr_width = dw_spi_dma_convert_width(dws->n_bytes); |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 269 | txconf.device_fc = false; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 270 | |
Serge Semin | a874d81 | 2020-09-20 14:23:14 +0300 | [diff] [blame^] | 271 | return dmaengine_slave_config(dws->txchan, &txconf); |
| 272 | } |
| 273 | |
| 274 | static struct dma_async_tx_descriptor * |
| 275 | dw_spi_dma_prepare_tx(struct dw_spi *dws, struct spi_transfer *xfer) |
| 276 | { |
| 277 | struct dma_async_tx_descriptor *txdesc; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 278 | |
Andy Shevchenko | 2a28529 | 2014-10-02 16:31:08 +0300 | [diff] [blame] | 279 | txdesc = dmaengine_prep_slave_sg(dws->txchan, |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 280 | xfer->tx_sg.sgl, |
| 281 | xfer->tx_sg.nents, |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 282 | DMA_MEM_TO_DEV, |
Andy Shevchenko | f7477c2 | 2014-10-02 16:31:09 +0300 | [diff] [blame] | 283 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Andy Shevchenko | c9dafb2 | 2015-03-02 20:15:58 +0200 | [diff] [blame] | 284 | if (!txdesc) |
| 285 | return NULL; |
| 286 | |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 287 | txdesc->callback = dw_spi_dma_tx_done; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 288 | txdesc->callback_param = dws; |
| 289 | |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 290 | return txdesc; |
| 291 | } |
| 292 | |
Serge Semin | 33726ef | 2020-05-29 16:11:54 +0300 | [diff] [blame] | 293 | static inline bool dw_spi_dma_rx_busy(struct dw_spi *dws) |
| 294 | { |
| 295 | return !!(dw_readl(dws, DW_SPI_SR) & SR_RF_NOT_EMPT); |
| 296 | } |
| 297 | |
| 298 | static int dw_spi_dma_wait_rx_done(struct dw_spi *dws) |
| 299 | { |
| 300 | int retry = WAIT_RETRIES; |
| 301 | struct spi_delay delay; |
| 302 | unsigned long ns, us; |
| 303 | u32 nents; |
| 304 | |
| 305 | /* |
| 306 | * It's unlikely that DMA engine is still doing the data fetching, but |
| 307 | * if it's let's give it some reasonable time. The timeout calculation |
| 308 | * is based on the synchronous APB/SSI reference clock rate, on a |
| 309 | * number of data entries left in the Rx FIFO, times a number of clock |
| 310 | * periods normally needed for a single APB read/write transaction |
| 311 | * without PREADY signal utilized (which is true for the DW APB SSI |
| 312 | * controller). |
| 313 | */ |
| 314 | nents = dw_readl(dws, DW_SPI_RXFLR); |
| 315 | ns = 4U * NSEC_PER_SEC / dws->max_freq * nents; |
| 316 | if (ns <= NSEC_PER_USEC) { |
| 317 | delay.unit = SPI_DELAY_UNIT_NSECS; |
| 318 | delay.value = ns; |
| 319 | } else { |
| 320 | us = DIV_ROUND_UP(ns, NSEC_PER_USEC); |
| 321 | delay.unit = SPI_DELAY_UNIT_USECS; |
| 322 | delay.value = clamp_val(us, 0, USHRT_MAX); |
| 323 | } |
| 324 | |
| 325 | while (dw_spi_dma_rx_busy(dws) && retry--) |
| 326 | spi_delay_exec(&delay, NULL); |
| 327 | |
| 328 | if (retry < 0) { |
| 329 | dev_err(&dws->master->dev, "Rx hanged up\n"); |
| 330 | return -EIO; |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 336 | /* |
| 337 | * dws->dma_chan_busy is set before the dma transfer starts, callback for rx |
| 338 | * channel will clear a corresponding bit. |
| 339 | */ |
| 340 | static void dw_spi_dma_rx_done(void *arg) |
| 341 | { |
| 342 | struct dw_spi *dws = arg; |
| 343 | |
Andy Shevchenko | 854d2f2 | 2015-03-06 14:42:01 +0200 | [diff] [blame] | 344 | clear_bit(RX_BUSY, &dws->dma_chan_busy); |
| 345 | if (test_bit(TX_BUSY, &dws->dma_chan_busy)) |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 346 | return; |
Serge Semin | 0327f0b | 2020-05-15 13:47:42 +0300 | [diff] [blame] | 347 | |
| 348 | dw_writel(dws, DW_SPI_DMACR, 0); |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 349 | complete(&dws->dma_completion); |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Serge Semin | a874d81 | 2020-09-20 14:23:14 +0300 | [diff] [blame^] | 352 | static int dw_spi_dma_config_rx(struct dw_spi *dws) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 353 | { |
| 354 | struct dma_slave_config rxconf; |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 355 | |
Andy Shevchenko | 3cb97e2 | 2020-05-06 18:30:18 +0300 | [diff] [blame] | 356 | memset(&rxconf, 0, sizeof(rxconf)); |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 357 | rxconf.direction = DMA_DEV_TO_MEM; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 358 | rxconf.src_addr = dws->dma_addr; |
Serge Semin | 0b2b665 | 2020-05-29 16:11:56 +0300 | [diff] [blame] | 359 | rxconf.src_maxburst = dws->rxburst; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 360 | rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 361 | rxconf.src_addr_width = dw_spi_dma_convert_width(dws->n_bytes); |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 362 | rxconf.device_fc = false; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 363 | |
Serge Semin | a874d81 | 2020-09-20 14:23:14 +0300 | [diff] [blame^] | 364 | return dmaengine_slave_config(dws->rxchan, &rxconf); |
| 365 | } |
| 366 | |
| 367 | static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws, |
| 368 | struct spi_transfer *xfer) |
| 369 | { |
| 370 | struct dma_async_tx_descriptor *rxdesc; |
| 371 | |
| 372 | if (!xfer->rx_buf) |
| 373 | return NULL; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 374 | |
Andy Shevchenko | 2a28529 | 2014-10-02 16:31:08 +0300 | [diff] [blame] | 375 | rxdesc = dmaengine_prep_slave_sg(dws->rxchan, |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 376 | xfer->rx_sg.sgl, |
| 377 | xfer->rx_sg.nents, |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 378 | DMA_DEV_TO_MEM, |
Andy Shevchenko | f7477c2 | 2014-10-02 16:31:09 +0300 | [diff] [blame] | 379 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Andy Shevchenko | c9dafb2 | 2015-03-02 20:15:58 +0200 | [diff] [blame] | 380 | if (!rxdesc) |
| 381 | return NULL; |
| 382 | |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 383 | rxdesc->callback = dw_spi_dma_rx_done; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 384 | rxdesc->callback_param = dws; |
| 385 | |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 386 | return rxdesc; |
| 387 | } |
| 388 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 389 | static int dw_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 390 | { |
Serge Semin | 7ef3038 | 2020-09-20 14:23:13 +0300 | [diff] [blame] | 391 | u16 imr, dma_ctrl; |
Serge Semin | a874d81 | 2020-09-20 14:23:14 +0300 | [diff] [blame^] | 392 | int ret; |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 393 | |
Serge Semin | 7ef3038 | 2020-09-20 14:23:13 +0300 | [diff] [blame] | 394 | if (!xfer->tx_buf) |
| 395 | return -EINVAL; |
| 396 | |
Serge Semin | a874d81 | 2020-09-20 14:23:14 +0300 | [diff] [blame^] | 397 | /* Setup DMA channels */ |
| 398 | ret = dw_spi_dma_config_tx(dws); |
| 399 | if (ret) |
| 400 | return ret; |
| 401 | |
| 402 | if (xfer->rx_buf) { |
| 403 | ret = dw_spi_dma_config_rx(dws); |
| 404 | if (ret) |
| 405 | return ret; |
| 406 | } |
| 407 | |
Serge Semin | 7ef3038 | 2020-09-20 14:23:13 +0300 | [diff] [blame] | 408 | /* Set the DMA handshaking interface */ |
| 409 | dma_ctrl = SPI_DMA_TDMAE; |
Andy Shevchenko | 3d7db0f | 2020-05-29 21:31:50 +0300 | [diff] [blame] | 410 | if (xfer->rx_buf) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 411 | dma_ctrl |= SPI_DMA_RDMAE; |
Thor Thayer | dd11444 | 2015-03-12 14:19:31 -0500 | [diff] [blame] | 412 | dw_writel(dws, DW_SPI_DMACR, dma_ctrl); |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 413 | |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 414 | /* Set the interrupt mask */ |
Serge Semin | 7ef3038 | 2020-09-20 14:23:13 +0300 | [diff] [blame] | 415 | imr = SPI_INT_TXOI; |
Andy Shevchenko | 3d7db0f | 2020-05-29 21:31:50 +0300 | [diff] [blame] | 416 | if (xfer->rx_buf) |
| 417 | imr |= SPI_INT_RXUI | SPI_INT_RXOI; |
Serge Semin | 43dba9f | 2020-05-22 03:07:51 +0300 | [diff] [blame] | 418 | spi_umask_intr(dws, imr); |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 419 | |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 420 | reinit_completion(&dws->dma_completion); |
| 421 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 422 | dws->transfer_handler = dw_spi_dma_transfer_handler; |
Andy Shevchenko | f051fc8 | 2015-03-09 16:48:47 +0200 | [diff] [blame] | 423 | |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 424 | return 0; |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 427 | static int dw_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer) |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 428 | { |
| 429 | struct dma_async_tx_descriptor *txdesc, *rxdesc; |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 430 | int ret; |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 431 | |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 432 | /* Prepare the TX dma transfer */ |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 433 | txdesc = dw_spi_dma_prepare_tx(dws, xfer); |
Serge Semin | 7ef3038 | 2020-09-20 14:23:13 +0300 | [diff] [blame] | 434 | if (!txdesc) |
| 435 | return -EINVAL; |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 436 | |
Andy Shevchenko | 9f14538 | 2015-03-09 16:48:46 +0200 | [diff] [blame] | 437 | /* Prepare the RX dma transfer */ |
Andy Shevchenko | f89a6d8 | 2015-03-09 16:48:49 +0200 | [diff] [blame] | 438 | rxdesc = dw_spi_dma_prepare_rx(dws, xfer); |
Andy Shevchenko | a5c2db9 | 2014-10-28 18:25:01 +0200 | [diff] [blame] | 439 | |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 440 | /* rx must be started before tx due to spi instinct */ |
Andy Shevchenko | 30c8eb5 | 2014-10-28 18:25:02 +0200 | [diff] [blame] | 441 | if (rxdesc) { |
| 442 | set_bit(RX_BUSY, &dws->dma_chan_busy); |
| 443 | dmaengine_submit(rxdesc); |
| 444 | dma_async_issue_pending(dws->rxchan); |
| 445 | } |
Andy Shevchenko | f7477c2 | 2014-10-02 16:31:09 +0300 | [diff] [blame] | 446 | |
Serge Semin | 7ef3038 | 2020-09-20 14:23:13 +0300 | [diff] [blame] | 447 | set_bit(TX_BUSY, &dws->dma_chan_busy); |
| 448 | dmaengine_submit(txdesc); |
| 449 | dma_async_issue_pending(dws->txchan); |
Andy Shevchenko | f7477c2 | 2014-10-02 16:31:09 +0300 | [diff] [blame] | 450 | |
Serge Semin | bdbdf0f | 2020-05-29 16:11:52 +0300 | [diff] [blame] | 451 | ret = dw_spi_dma_wait(dws, xfer); |
| 452 | if (ret) |
| 453 | return ret; |
| 454 | |
Serge Semin | 7ef3038 | 2020-09-20 14:23:13 +0300 | [diff] [blame] | 455 | if (dws->master->cur_msg->status == -EINPROGRESS) { |
Serge Semin | 1ade2d8 | 2020-05-29 16:11:53 +0300 | [diff] [blame] | 456 | ret = dw_spi_dma_wait_tx_done(dws, xfer); |
| 457 | if (ret) |
| 458 | return ret; |
| 459 | } |
| 460 | |
Serge Semin | 33726ef | 2020-05-29 16:11:54 +0300 | [diff] [blame] | 461 | if (rxdesc && dws->master->cur_msg->status == -EINPROGRESS) |
| 462 | ret = dw_spi_dma_wait_rx_done(dws); |
| 463 | |
| 464 | return ret; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 465 | } |
| 466 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 467 | static void dw_spi_dma_stop(struct dw_spi *dws) |
Andy Shevchenko | 4d5ac1e | 2015-03-09 16:48:48 +0200 | [diff] [blame] | 468 | { |
| 469 | if (test_bit(TX_BUSY, &dws->dma_chan_busy)) { |
Andy Shevchenko | cf1716e | 2017-01-03 15:48:20 +0200 | [diff] [blame] | 470 | dmaengine_terminate_sync(dws->txchan); |
Andy Shevchenko | 4d5ac1e | 2015-03-09 16:48:48 +0200 | [diff] [blame] | 471 | clear_bit(TX_BUSY, &dws->dma_chan_busy); |
| 472 | } |
| 473 | if (test_bit(RX_BUSY, &dws->dma_chan_busy)) { |
Andy Shevchenko | cf1716e | 2017-01-03 15:48:20 +0200 | [diff] [blame] | 474 | dmaengine_terminate_sync(dws->rxchan); |
Andy Shevchenko | 4d5ac1e | 2015-03-09 16:48:48 +0200 | [diff] [blame] | 475 | clear_bit(RX_BUSY, &dws->dma_chan_busy); |
| 476 | } |
Serge Semin | 0327f0b | 2020-05-15 13:47:42 +0300 | [diff] [blame] | 477 | |
| 478 | dw_writel(dws, DW_SPI_DMACR, 0); |
Andy Shevchenko | 4d5ac1e | 2015-03-09 16:48:48 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 481 | static const struct dw_spi_dma_ops dw_spi_dma_mfld_ops = { |
| 482 | .dma_init = dw_spi_dma_init_mfld, |
| 483 | .dma_exit = dw_spi_dma_exit, |
| 484 | .dma_setup = dw_spi_dma_setup, |
| 485 | .can_dma = dw_spi_can_dma, |
| 486 | .dma_transfer = dw_spi_dma_transfer, |
| 487 | .dma_stop = dw_spi_dma_stop, |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 488 | }; |
Andy Shevchenko | 37aa8aa | 2020-05-06 18:30:23 +0300 | [diff] [blame] | 489 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 490 | void dw_spi_dma_setup_mfld(struct dw_spi *dws) |
Andy Shevchenko | 37aa8aa | 2020-05-06 18:30:23 +0300 | [diff] [blame] | 491 | { |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 492 | dws->dma_ops = &dw_spi_dma_mfld_ops; |
Andy Shevchenko | 37aa8aa | 2020-05-06 18:30:23 +0300 | [diff] [blame] | 493 | } |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 494 | EXPORT_SYMBOL_GPL(dw_spi_dma_setup_mfld); |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 495 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 496 | static const struct dw_spi_dma_ops dw_spi_dma_generic_ops = { |
| 497 | .dma_init = dw_spi_dma_init_generic, |
| 498 | .dma_exit = dw_spi_dma_exit, |
| 499 | .dma_setup = dw_spi_dma_setup, |
| 500 | .can_dma = dw_spi_can_dma, |
| 501 | .dma_transfer = dw_spi_dma_transfer, |
| 502 | .dma_stop = dw_spi_dma_stop, |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 503 | }; |
| 504 | |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 505 | void dw_spi_dma_setup_generic(struct dw_spi *dws) |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 506 | { |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 507 | dws->dma_ops = &dw_spi_dma_generic_ops; |
Jarkko Nikula | 22d48ad | 2020-05-06 18:30:25 +0300 | [diff] [blame] | 508 | } |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 509 | EXPORT_SYMBOL_GPL(dw_spi_dma_setup_generic); |