Skip to content

Commit 18197e9

Browse files
Xianwei Zhaobroonie
authored andcommitted
spi: meson-spicc: add DMA support
Add DMA support for spicc driver. DMA works if the transfer meets the following conditions: 1. 64 bits per word; 2. The transfer length must be multiples of the dma_burst_len, and the dma_burst_len should be one of 8,7...2, otherwise, it will be split into several SPI bursts. Signed-off-by: Sunny Luo <[email protected]> Signed-off-by: Xianwei Zhao <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent b50a1e1 commit 18197e9

File tree

1 file changed

+220
-21
lines changed

1 file changed

+220
-21
lines changed

drivers/spi/spi-meson-spicc.c

Lines changed: 220 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@
2121
#include <linux/interrupt.h>
2222
#include <linux/reset.h>
2323
#include <linux/pinctrl/consumer.h>
24+
#include <linux/dma-mapping.h>
2425

2526
/*
26-
* The Meson SPICC controller could support DMA based transfers, but is not
27-
* implemented by the vendor code, and while having the registers documentation
28-
* it has never worked on the GXL Hardware.
29-
* The PIO mode is the only mode implemented, and due to badly designed HW :
30-
* - all transfers are cutted in 16 words burst because the FIFO hangs on
31-
* TX underflow, and there is no TX "Half-Empty" interrupt, so we go by
32-
* FIFO max size chunk only
33-
* - CS management is dumb, and goes UP between every burst, so is really a
34-
* "Data Valid" signal than a Chip Select, GPIO link should be used instead
35-
* to have a CS go down over the full transfer
27+
* There are two modes for data transmission: PIO and DMA.
28+
* When bits_per_word is 8, 16, 24, or 32, data is transferred using PIO mode.
29+
* When bits_per_word is 64, DMA mode is used by default.
30+
*
31+
* DMA achieves a transfer with one or more SPI bursts, each SPI burst is made
32+
* up of one or more DMA bursts. The DMA burst implementation mechanism is,
33+
* For TX, when the number of words in TXFIFO is less than the preset
34+
* reading threshold, SPICC starts a reading DMA burst, which reads the preset
35+
* number of words from TX buffer, then writes them into TXFIFO.
36+
* For RX, when the number of words in RXFIFO is greater than the preset
37+
* writing threshold, SPICC starts a writing request burst, which reads the
38+
* preset number of words from RXFIFO, then write them into RX buffer.
39+
* DMA works if the transfer meets the following conditions,
40+
* - 64 bits per word
41+
* - The transfer length in word must be multiples of the dma_burst_len, and
42+
* the dma_burst_len should be one of 8,7...2, otherwise, it will be split
43+
* into several SPI bursts by this driver
3644
*/
3745

3846
#define SPICC_MAX_BURST 128
@@ -128,6 +136,23 @@
128136

129137
#define SPICC_DWADDR 0x24 /* Write Address of DMA */
130138

139+
#define SPICC_LD_CNTL0 0x28
140+
#define VSYNC_IRQ_SRC_SELECT BIT(0)
141+
#define DMA_EN_SET_BY_VSYNC BIT(2)
142+
#define XCH_EN_SET_BY_VSYNC BIT(3)
143+
#define DMA_READ_COUNTER_EN BIT(4)
144+
#define DMA_WRITE_COUNTER_EN BIT(5)
145+
#define DMA_RADDR_LOAD_BY_VSYNC BIT(6)
146+
#define DMA_WADDR_LOAD_BY_VSYNC BIT(7)
147+
#define DMA_ADDR_LOAD_FROM_LD_ADDR BIT(8)
148+
149+
#define SPICC_LD_CNTL1 0x2c
150+
#define DMA_READ_COUNTER GENMASK(15, 0)
151+
#define DMA_WRITE_COUNTER GENMASK(31, 16)
152+
#define DMA_BURST_LEN_DEFAULT 8
153+
#define DMA_BURST_COUNT_MAX 0xffff
154+
#define SPI_BURST_LEN_MAX (DMA_BURST_LEN_DEFAULT * DMA_BURST_COUNT_MAX)
155+
131156
#define SPICC_ENH_CTL0 0x38 /* Enhanced Feature */
132157
#define SPICC_ENH_CLK_CS_DELAY_MASK GENMASK(15, 0)
133158
#define SPICC_ENH_DATARATE_MASK GENMASK(23, 16)
@@ -171,6 +196,9 @@ struct meson_spicc_device {
171196
struct pinctrl *pinctrl;
172197
struct pinctrl_state *pins_idle_high;
173198
struct pinctrl_state *pins_idle_low;
199+
dma_addr_t tx_dma;
200+
dma_addr_t rx_dma;
201+
bool using_dma;
174202
};
175203

176204
#define pow2_clk_to_spicc(_div) container_of(_div, struct meson_spicc_device, pow2_div)
@@ -202,6 +230,148 @@ static void meson_spicc_oen_enable(struct meson_spicc_device *spicc)
202230
writel_relaxed(conf, spicc->base + SPICC_ENH_CTL0);
203231
}
204232

233+
static int meson_spicc_dma_map(struct meson_spicc_device *spicc,
234+
struct spi_transfer *t)
235+
{
236+
struct device *dev = spicc->host->dev.parent;
237+
238+
if (!(t->tx_buf && t->rx_buf))
239+
return -EINVAL;
240+
241+
t->tx_dma = dma_map_single(dev, (void *)t->tx_buf, t->len, DMA_TO_DEVICE);
242+
if (dma_mapping_error(dev, t->tx_dma))
243+
return -ENOMEM;
244+
245+
t->rx_dma = dma_map_single(dev, t->rx_buf, t->len, DMA_FROM_DEVICE);
246+
if (dma_mapping_error(dev, t->rx_dma))
247+
return -ENOMEM;
248+
249+
spicc->tx_dma = t->tx_dma;
250+
spicc->rx_dma = t->rx_dma;
251+
252+
return 0;
253+
}
254+
255+
static void meson_spicc_dma_unmap(struct meson_spicc_device *spicc,
256+
struct spi_transfer *t)
257+
{
258+
struct device *dev = spicc->host->dev.parent;
259+
260+
if (t->tx_dma)
261+
dma_unmap_single(dev, t->tx_dma, t->len, DMA_TO_DEVICE);
262+
if (t->rx_dma)
263+
dma_unmap_single(dev, t->rx_dma, t->len, DMA_FROM_DEVICE);
264+
}
265+
266+
/*
267+
* According to the remain words length, calculate a suitable spi burst length
268+
* and a dma burst length for current spi burst
269+
*/
270+
static u32 meson_spicc_calc_dma_len(struct meson_spicc_device *spicc,
271+
u32 len, u32 *dma_burst_len)
272+
{
273+
u32 i;
274+
275+
if (len <= spicc->data->fifo_size) {
276+
*dma_burst_len = len;
277+
return len;
278+
}
279+
280+
*dma_burst_len = DMA_BURST_LEN_DEFAULT;
281+
282+
if (len == (SPI_BURST_LEN_MAX + 1))
283+
return SPI_BURST_LEN_MAX - DMA_BURST_LEN_DEFAULT;
284+
285+
if (len >= SPI_BURST_LEN_MAX)
286+
return SPI_BURST_LEN_MAX;
287+
288+
for (i = DMA_BURST_LEN_DEFAULT; i > 1; i--)
289+
if ((len % i) == 0) {
290+
*dma_burst_len = i;
291+
return len;
292+
}
293+
294+
i = len % DMA_BURST_LEN_DEFAULT;
295+
len -= i;
296+
297+
if (i == 1)
298+
len -= DMA_BURST_LEN_DEFAULT;
299+
300+
return len;
301+
}
302+
303+
static void meson_spicc_setup_dma(struct meson_spicc_device *spicc)
304+
{
305+
unsigned int len;
306+
unsigned int dma_burst_len, dma_burst_count;
307+
unsigned int count_en = 0;
308+
unsigned int txfifo_thres = 0;
309+
unsigned int read_req = 0;
310+
unsigned int rxfifo_thres = 31;
311+
unsigned int write_req = 0;
312+
unsigned int ld_ctr1 = 0;
313+
314+
writel_relaxed(spicc->tx_dma, spicc->base + SPICC_DRADDR);
315+
writel_relaxed(spicc->rx_dma, spicc->base + SPICC_DWADDR);
316+
317+
/* Set the max burst length to support a transmission with length of
318+
* no more than 1024 bytes(128 words), which must use the CS management
319+
* because of some strict timing requirements
320+
*/
321+
writel_bits_relaxed(SPICC_BURSTLENGTH_MASK, SPICC_BURSTLENGTH_MASK,
322+
spicc->base + SPICC_CONREG);
323+
324+
len = meson_spicc_calc_dma_len(spicc, spicc->xfer_remain,
325+
&dma_burst_len);
326+
spicc->xfer_remain -= len;
327+
dma_burst_count = DIV_ROUND_UP(len, dma_burst_len);
328+
dma_burst_len--;
329+
330+
if (spicc->tx_dma) {
331+
spicc->tx_dma += len;
332+
count_en |= DMA_READ_COUNTER_EN;
333+
txfifo_thres = spicc->data->fifo_size - dma_burst_len;
334+
read_req = dma_burst_len;
335+
ld_ctr1 |= FIELD_PREP(DMA_READ_COUNTER, dma_burst_count);
336+
}
337+
338+
if (spicc->rx_dma) {
339+
spicc->rx_dma += len;
340+
count_en |= DMA_WRITE_COUNTER_EN;
341+
rxfifo_thres = dma_burst_len;
342+
write_req = dma_burst_len;
343+
ld_ctr1 |= FIELD_PREP(DMA_WRITE_COUNTER, dma_burst_count);
344+
}
345+
346+
writel_relaxed(count_en, spicc->base + SPICC_LD_CNTL0);
347+
writel_relaxed(ld_ctr1, spicc->base + SPICC_LD_CNTL1);
348+
writel_relaxed(SPICC_DMA_ENABLE
349+
| SPICC_DMA_URGENT
350+
| FIELD_PREP(SPICC_TXFIFO_THRESHOLD_MASK, txfifo_thres)
351+
| FIELD_PREP(SPICC_READ_BURST_MASK, read_req)
352+
| FIELD_PREP(SPICC_RXFIFO_THRESHOLD_MASK, rxfifo_thres)
353+
| FIELD_PREP(SPICC_WRITE_BURST_MASK, write_req),
354+
spicc->base + SPICC_DMAREG);
355+
}
356+
357+
static irqreturn_t meson_spicc_dma_irq(struct meson_spicc_device *spicc)
358+
{
359+
if (readl_relaxed(spicc->base + SPICC_DMAREG) & SPICC_DMA_ENABLE)
360+
return IRQ_HANDLED;
361+
362+
if (spicc->xfer_remain) {
363+
meson_spicc_setup_dma(spicc);
364+
} else {
365+
writel_bits_relaxed(SPICC_SMC, 0, spicc->base + SPICC_CONREG);
366+
writel_relaxed(0, spicc->base + SPICC_INTREG);
367+
writel_relaxed(0, spicc->base + SPICC_DMAREG);
368+
meson_spicc_dma_unmap(spicc, spicc->xfer);
369+
complete(&spicc->done);
370+
}
371+
372+
return IRQ_HANDLED;
373+
}
374+
205375
static inline bool meson_spicc_txfull(struct meson_spicc_device *spicc)
206376
{
207377
return !!FIELD_GET(SPICC_TF,
@@ -293,6 +463,9 @@ static irqreturn_t meson_spicc_irq(int irq, void *data)
293463

294464
writel_bits_relaxed(SPICC_TC, SPICC_TC, spicc->base + SPICC_STATREG);
295465

466+
if (spicc->using_dma)
467+
return meson_spicc_dma_irq(spicc);
468+
296469
/* Empty RX FIFO */
297470
meson_spicc_rx(spicc);
298471

@@ -426,9 +599,6 @@ static int meson_spicc_transfer_one(struct spi_controller *host,
426599

427600
meson_spicc_reset_fifo(spicc);
428601

429-
/* Setup burst */
430-
meson_spicc_setup_burst(spicc);
431-
432602
/* Setup wait for completion */
433603
reinit_completion(&spicc->done);
434604

@@ -442,11 +612,36 @@ static int meson_spicc_transfer_one(struct spi_controller *host,
442612
/* Increase it twice and add 200 ms tolerance */
443613
timeout += timeout + 200;
444614

445-
/* Start burst */
446-
writel_bits_relaxed(SPICC_XCH, SPICC_XCH, spicc->base + SPICC_CONREG);
615+
if (xfer->bits_per_word == 64) {
616+
int ret;
617+
618+
/* dma_burst_len 1 can't trigger a dma burst */
619+
if (xfer->len < 16)
620+
return -EINVAL;
621+
622+
ret = meson_spicc_dma_map(spicc, xfer);
623+
if (ret) {
624+
meson_spicc_dma_unmap(spicc, xfer);
625+
dev_err(host->dev.parent, "dma map failed\n");
626+
return ret;
627+
}
628+
629+
spicc->using_dma = true;
630+
spicc->xfer_remain = DIV_ROUND_UP(xfer->len, spicc->bytes_per_word);
631+
meson_spicc_setup_dma(spicc);
632+
writel_relaxed(SPICC_TE_EN, spicc->base + SPICC_INTREG);
633+
writel_bits_relaxed(SPICC_SMC, SPICC_SMC, spicc->base + SPICC_CONREG);
634+
} else {
635+
spicc->using_dma = false;
636+
/* Setup burst */
637+
meson_spicc_setup_burst(spicc);
447638

448-
/* Enable interrupts */
449-
writel_relaxed(SPICC_TC_EN, spicc->base + SPICC_INTREG);
639+
/* Start burst */
640+
writel_bits_relaxed(SPICC_XCH, SPICC_XCH, spicc->base + SPICC_CONREG);
641+
642+
/* Enable interrupts */
643+
writel_relaxed(SPICC_TC_EN, spicc->base + SPICC_INTREG);
644+
}
450645

451646
if (!wait_for_completion_timeout(&spicc->done, msecs_to_jiffies(timeout)))
452647
return -ETIMEDOUT;
@@ -545,6 +740,14 @@ static int meson_spicc_setup(struct spi_device *spi)
545740
if (!spi->controller_state)
546741
spi->controller_state = spi_controller_get_devdata(spi->controller);
547742

743+
/* DMA works at 64 bits, the rest works on PIO */
744+
if (spi->bits_per_word != 8 &&
745+
spi->bits_per_word != 16 &&
746+
spi->bits_per_word != 24 &&
747+
spi->bits_per_word != 32 &&
748+
spi->bits_per_word != 64)
749+
return -EINVAL;
750+
548751
return 0;
549752
}
550753

@@ -853,10 +1056,6 @@ static int meson_spicc_probe(struct platform_device *pdev)
8531056
host->num_chipselect = 4;
8541057
host->dev.of_node = pdev->dev.of_node;
8551058
host->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LOOP;
856-
host->bits_per_word_mask = SPI_BPW_MASK(32) |
857-
SPI_BPW_MASK(24) |
858-
SPI_BPW_MASK(16) |
859-
SPI_BPW_MASK(8);
8601059
host->flags = (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX);
8611060
host->min_speed_hz = spicc->data->min_speed_hz;
8621061
host->max_speed_hz = spicc->data->max_speed_hz;

0 commit comments

Comments
 (0)