spi: davinci: Use SPI framework to handle DMA mapping
PATCH 1/2 spi: davinci: Use SPI framework to handle DMA mapping
Uppers layers like MTD can pass vmalloc'd buffers to the SPI driver, and the current implementation will fail to map these kind of buffers. The SPI framework is able to detect the best way to handle and map buffers.
This commit updates the davinci SPI driver in order to use the SPI framework to handle the DMA mapping of buffers coming from an upper layer.
--- drivers/spi/spi-davinci.c | 78 ++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 32 deletions(-)
drivers/spi/spi-davinci.c +46 -32
@@ -630,7 +630,6 @@ static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t) };struct dma_async_tx_descriptor *rxdesc;struct dma_async_tx_descriptor *txdesc; -void *buf; dummy_buf =kzalloc (t->len,GFP_KERNEL );if (!dummy_buf)
@@ -639,42 +638,40 @@ static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)dmaengine_slave_config (dspi->dma_rx, &dma_rx_conf);dmaengine_slave_config (dspi->dma_tx, &dma_tx_conf); -sg_init_table (&sg_rx,1 ); -if (!t->rx_buf) - buf = dummy_buf; -else - buf = t->rx_buf; - t->rx_dma =dma_map_single (&spi->dev, buf, - t->len,DMA_FROM_DEVICE ); -if (dma_mapping_error (&spi->dev, !t->rx_dma)) { - ret = -EFAULT ; - goto err_rx_map; +if (!t->rx_buf) { +sg_init_table (&sg_rx,1 ); + t->rx_dma =dma_map_single (&spi->dev, dummy_buf, + t->len,DMA_FROM_DEVICE ); +if (dma_mapping_error (&spi->dev, !t->rx_dma)) { + ret = -EFAULT ; + goto err_rx_map; + } +sg_dma_address (&sg_rx) = t->rx_dma; +sg_dma_len (&sg_rx) = t->len; } -sg_dma_address (&sg_rx) = t->rx_dma; -sg_dma_len (&sg_rx) = t->len;sg_init_table (&sg_tx,1 ); -if (!t->tx_buf) - buf = dummy_buf; -else - buf = (void *)t->tx_buf; - t->tx_dma =dma_map_single (&spi->dev, buf, - t->len,DMA_TO_DEVICE ); -if (dma_mapping_error (&spi->dev, t->tx_dma)) { - ret = -EFAULT ; - goto err_tx_map; +if (!t->tx_buf) { + t->tx_dma =dma_map_single (&spi->dev, (void *)t->tx_buf, + t->len,DMA_TO_DEVICE ); +if (dma_mapping_error (&spi->dev, t->tx_dma)) { + ret = -EFAULT ; + goto err_tx_map; + } +sg_dma_address (&sg_tx) = t->tx_dma; +sg_dma_len (&sg_tx) = t->len; } -sg_dma_address (&sg_tx) = t->tx_dma; -sg_dma_len (&sg_tx) = t->len; rxdesc =dmaengine_prep_slave_sg (dspi->dma_rx, - &sg_rx,1 ,DMA_DEV_TO_MEM , + t->rx_sg.sgl ?: &sg_rx, t->rx_sg.nents ?:1 , +DMA_DEV_TO_MEM ,DMA_PREP_INTERRUPT |DMA_CTRL_ACK );if (!rxdesc) goto err_desc; txdesc =dmaengine_prep_slave_sg (dspi->dma_tx, - &sg_tx,1 ,DMA_MEM_TO_DEV , + t->tx_sg.sgl ?: &sg_tx, t->tx_sg.nents ?:1 , +DMA_MEM_TO_DEV ,DMA_PREP_INTERRUPT |DMA_CTRL_ACK );if (!txdesc) goto err_desc;
@@ -713,10 +710,12 @@ static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)if (spicfg->io_type ==SPI_IO_TYPE_DMA ) {clear_io_bits (dspi->base +SPIINT ,SPIINT_DMA_REQ_EN ); -dma_unmap_single (&spi->dev, t->rx_dma, - t->len,DMA_FROM_DEVICE ); -dma_unmap_single (&spi->dev, t->tx_dma, - t->len,DMA_TO_DEVICE ); +if (!t->rx_buf) +dma_unmap_single (&spi->dev, t->rx_dma, + t->len,DMA_FROM_DEVICE ); +if (!t->tx_buf) +dma_unmap_single (&spi->dev, t->tx_dma, + t->len,DMA_TO_DEVICE );kfree (dummy_buf); }
@@ -742,9 +741,11 @@ static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)return t->len; err_desc: -dma_unmap_single (&spi->dev, t->tx_dma, t->len,DMA_TO_DEVICE ); +if (!t->tx_buf) +dma_unmap_single (&spi->dev, t->tx_dma, t->len,DMA_TO_DEVICE ); err_tx_map: -dma_unmap_single (&spi->dev, t->rx_dma, t->len,DMA_FROM_DEVICE ); +if (!t->rx_buf) +dma_unmap_single (&spi->dev, t->rx_dma, t->len,DMA_FROM_DEVICE ); err_rx_map:kfree (dummy_buf); err_alloc_dummy_buf:
@@ -898,6 +899,18 @@ static struct davinci_spi_platform_data }#endif +static bool davinci_spi_can_dma (struct spi_master *master, +struct spi_device *spi, +struct spi_transfer *xfer) +{ +struct davinci_spi_config *spicfg = spi->controller_data; + +if (!spicfg) + spicfg = &davinci_spi_default_cfg; + +return spicfg->io_type ==SPI_IO_TYPE_DMA ; +} +/** * davinci_spi_probe - probe function for SPI Master Controller * @pdev: platform_device structure which contains plateform specific data
@@ -990,6 +1003,7 @@ static int davinci_spi_probe(struct platform_device *pdev) master->bits_per_word_mask =SPI_BPW_RANGE_MASK (2 ,16 ); master->setup = davinci_spi_setup; master->cleanup = davinci_spi_cleanup; + master->can_dma = davinci_spi_can_dma; dspi->bitbang.chipselect = davinci_spi_chipselect; dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
Signature · 6 lines
2.11.0 -- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 2/2 spi: davinci: enable DMA when channels are defined in DT
When booting with DT the SPI driver is always using
the SPI_IO_TYPE_INTR mode to transfer data even if DMA channels are defined in the DT.
This commit changes the behaviour to select the SPI_IO_TYPE_DMA mode if DMA channels are defined in the DT and will keep SPI_IO_TYPE_INTR if the channels are not defined in it.
--- drivers/spi/spi-davinci.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/spi/spi-davinci.c +4 -0
@@ -389,6 +389,7 @@ static int davinci_spi_of_setup(struct spi_device *spi) {struct davinci_spi_config *spicfg = spi->controller_data;struct device_node *np = spi->dev.of_node; +struct davinci_spi *dspi =spi_master_get_devdata (spi->master);u32 prop;if (spicfg ==NULL && np) {
@@ -400,6 +401,9 @@ static int davinci_spi_of_setup(struct spi_device *spi)if (!of_property_read_u32 (np,"ti,spi-wdelay" , &prop)) spicfg->wdelay = (u8 )prop; spi->controller_data = spicfg; + +if (dspi->dma_rx && dspi->dma_tx) + spicfg->io_type =SPI_IO_TYPE_DMA ; }return 0 ;
Signature · 6 lines
2.11.0 -- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Mark Brown
maintainerApplied "spi: davinci: enable DMA when channels are defined in DT" to the spi tree
The patch
spi: davinci: enable DMA when channels are defined in DT
has been applied to the spi tree at
git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark
Quoted 1 line
From
3e2e1258443ea97e40dfb4a3cf15108d17939066Mon Sep 17 00:00:00 2001
From: Fabien Parent <[email protected]>
Date: Thu, 23 Feb 2017 19:01:57 +0100
Subject: [PATCH] spi: davinci: enable DMA when channels are defined in DT
When booting with DT the SPI driver is always using
the SPI_IO_TYPE_INTR mode to transfer data even if DMA channels are defined in the DT.
This commit changes the behaviour to select the SPI_IO_TYPE_DMA mode if DMA channels are defined in the DT and will keep SPI_IO_TYPE_INTR if the channels are not defined in it.
--- drivers/spi/spi-davinci.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/spi/spi-davinci.c +4 -0
@@ -389,6 +389,7 @@ static int davinci_spi_of_setup(struct spi_device *spi) {struct davinci_spi_config *spicfg = spi->controller_data;struct device_node *np = spi->dev.of_node; +struct davinci_spi *dspi =spi_master_get_devdata (spi->master);u32 prop;if (spicfg ==NULL && np) {
@@ -400,6 +401,9 @@ static int davinci_spi_of_setup(struct spi_device *spi)if (!of_property_read_u32 (np,"ti,spi-wdelay" , &prop)) spicfg->wdelay = (u8 )prop; spi->controller_data = spicfg; + +if (dspi->dma_rx && dspi->dma_tx) + spicfg->io_type =SPI_IO_TYPE_DMA ; }return 0 ;
Signature · 1 line
2.11.0
Re: Applied "spi: davinci: enable DMA when channels are defined in DT" to the spi tree
Hello Mark,
To avoid data corruption issues with UBIFS volume over SPI NOR, DMA should not be used for vmalloc'ed buffers. The 5'th patch in my series fixes that:
0003-spi-davinci-use-rx-buffer-as-dummy-tx-buffer.patch 0004-spi-davinci-do-not-use-DMA-if-transfer-length-is-les.patch 0005-spi-davinci-do-not-use-DMA-for-vmalloc-ed-buffers.patch
Thanks,
Frode
Quoted 60 lines · Mark Brown
The patch
spi: davinci: enable DMA when channels are defined in DT
has been applied to the spi tree at
git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.gitAll being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
MarkFrom
3e2e1258443ea97e40dfb4a3cf15108d17939066Mon Sep 17 00:00:00 2001
From: Fabien Parent <fparent-rdvid1DuHRBWk0Htik3J/[email protected]>
Date: Thu, 23 Feb 2017 19:01:57 +0100
Subject: [PATCH] spi: davinci: enable DMA when channels are defined in DTWhen booting with DT the SPI driver is always using
theSPI_IO_TYPE_INTRmode to transfer data even if DMA channels are defined in the DT.This commit changes the behaviour to select the
SPI_IO_TYPE_DMAmode if DMA channels are defined in the DT and will keepSPI_IO_TYPE_INTRif the channels are not defined in it.Signed-off-by Mark Brown <[email protected]>--- drivers/spi/spi-davinci.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/spi/spi-davinci.c +1
-0@@ -389,6 +389,7 @@ static int davinci_spi_of_setup(struct spi_device *spi) {struct davinci_spi_config *spicfg = spi->controller_data;struct device_node *np = spi->dev.of_node; +struct davinci_spi *dspi =spi_master_get_devdata (spi->master);u32 prop;if (spicfg ==NULL && np) {
drivers/spi/spi-davinci.c static int davinci_spi_of_setup(struct spi_device *spi) · line 409
Quoted 9 lines
drivers/spi/spi-davinci.c +3
-0@@ -400,6 +401,9 @@ static int davinci_spi_of_setup(struct spi_device *spi)if (!of_property_read_u32 (np,"ti,spi-wdelay" , &prop)) spicfg->wdelay = (u8 )prop; spi->controller_data = spicfg; + +if (dspi->dma_rx && dspi->dma_tx) + spicfg->io_type =SPI_IO_TYPE_DMA ; }return 0 ;
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Mark Brown
maintainerRe: Applied "spi: davinci: enable DMA when channels are defined in DT" to the spi tree
Quoted 1 line · Frode Isaksen
To avoid data corruption issues with UBIFS volume over SPI NOR, DMA should not be used for vmalloc'ed buffers. The 5'th patch in my series fixes that:
It's best to always try to ensure that your patch serieses are bisectable...
Re: Applied "spi: davinci: enable DMA when channels are defined in DT" to the spi tree
Quoted 5 lines · Mark Brown
Quoted 1 line · Frode Isaksen
To avoid data corruption issues with UBIFS volume over SPI NOR, DMA should not be used for vmalloc'ed buffers. The 5'th patch in my series fixes that:
It's best to always try to ensure that your patch serieses are bisectable...
My wrong - do you want me to re-submit the patch series with the the last patch in the series that really enabled DMA ?
Thanks,
Frode
Mark Brown
maintainerRe: Applied "spi: davinci: enable DMA when channels are defined in DT" to the spi tree
On Fri, Mar 10, 2017 at 11:24:43AM +0100, Frode Isaksen wrote:
Please fix your mail client to word wrap within paragraphs at something substantially less than 80 columns. Doing this makes your messages much easier to read and reply to.
Quoted 1 line
My wrong - do you want me to re-submit the patch series with the the last patch in the series that really enabled DMA ?
I'm still reviewing the rest of the series.
Mark Brown
maintainerQuoted 3 lines · Fabien Parent
This commit updates the davinci SPI driver in order to use the SPI framework to handle the DMA mapping of buffers coming from an upper layer.
This claims to be using the framework to do DMA mapping but...
drivers/spi/spi-davinci.c static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t) · line 650
Quoted 10 lines
drivers/spi/spi-davinci.c +10
-0+if (!t->rx_buf) { +sg_init_table (&sg_rx,1 ); + t->rx_dma =dma_map_single (&spi->dev, dummy_buf, + t->len,DMA_FROM_DEVICE ); +if (dma_mapping_error (&spi->dev, !t->rx_dma)) { + ret = -EFAULT ; + goto err_rx_map; + } +sg_dma_address (&sg_rx) = t->rx_dma; +sg_dma_len (&sg_rx) = t->len;
...adds code that does DMA mapping. That's confusing?