i.MX SPI DMA cleanup
Subsystems (2)
Sascha Hauer
maintainerPATCH v4 1/1 i.MX SPI DMA cleanup
Changes since v3:
- improve commit description for "spi: imx: initialize usedma earlier"
Changes since v2:
- Drop patches already applied
- rebase on next
Changes since v1:
- Add missing Signed-off-by to Patch 3
- Drop device tree change as it's already applied by Shawn
- Add patch subject when referring to commits
----------------------------------------------------------------
Anton Bondarenko (1):
spi: imx: add support for all SPI word width for DMA
Sascha Hauer (8):
spi: imx: drop fallback to PIO
spi: imx: initialize usedma earlier
spi: imx: drop unnecessary read/modify/write
spi: imx: drop unncessary dma_is_inited variable
spi: imx: remove unnecessary bit clearing in mx51_ecspi_config
spi: imx: make some register defines simpler
spi: imx: set MX51_ECSPI_CTRL_SMC bit in setup function
spi: imx: drop bogus tests for rx/tx bufs in DMA transfer
drivers/spi/spi-imx.c | 296 ++++++++++++++++++++++++++------------------------ 1 file changed, 153 insertions(+), 143 deletions(-)
Sascha Hauer
maintainerPATCH 1/9 spi: imx: drop fallback to PIO
At the moment the driver decides to fallback to PIO mode the buffers are already mapped for DMA. It's a bug to access them with the CPU afterwards, so we cannot just fallback to PIO mode.
It should not be necessary anyway, since we only use DMA when we verified that it's possible in the fist place, so when prep_slave_sg fails it's a bug, either in the SDMA driver or in the can_dma implementation.
--- drivers/spi/spi-imx.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
drivers/spi/spi-imx.c +6 -13
@@ -945,7 +945,7 @@ static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx, tx->sgl, tx->nents,DMA_MEM_TO_DEV , DMA_PREP_INTERRUPT | DMA_CTRL_ACK);if (!desc_tx) - goto tx_nodma; +return -EINVAL ; desc_tx->callback = spi_imx_dma_tx_callback; desc_tx->callback_param = (void *)spi_imx;
@@ -956,8 +956,10 @@ static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx, desc_rx =dmaengine_prep_slave_sg (master->dma_rx, rx->sgl, rx->nents,DMA_DEV_TO_MEM ,DMA_PREP_INTERRUPT |DMA_CTRL_ACK ); -if (!desc_rx) - goto rx_nodma; +if (!desc_rx) { +dmaengine_terminate_all (master->dma_tx); +return -EINVAL ; + } desc_rx->callback = spi_imx_dma_rx_callback; desc_rx->callback_param = (void *)spi_imx;
@@ -1010,12 +1012,6 @@ static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx, ret = transfer->len;return ret; - -rx_nodma: -dmaengine_terminate_all (master->dma_tx); -tx_nodma: -dev_warn_once (spi_imx->dev,"DMA not available, falling back to PIO\n" ); -return -EAGAIN ; } static int spi_imx_pio_transfer(struct spi_device *spi,
@@ -1042,15 +1038,12 @@ static int spi_imx_pio_transfer(struct spi_device *spi,static int spi_imx_transfer (struct spi_device *spi,struct spi_transfer *transfer) { -int ret;struct spi_imx_data *spi_imx =spi_master_get_devdata (spi->master);if (spi_imx->bitbang.master->can_dma &&spi_imx_can_dma (spi_imx->bitbang.master, spi, transfer)) { spi_imx->usedma = true; - ret =spi_imx_dma_transfer (spi_imx, transfer); -if (ret != -EAGAIN ) -return ret; +return spi_imx_dma_transfer (spi_imx, transfer); } spi_imx->usedma = false;
Signature · 1 line
2.7.0
Sascha Hauer
maintainerPATCH 2/9 spi: imx: initialize usedma earlier
In reply to Sascha HauerThe SoC specific config function does not know if DMA will be used or not. This information will be useful to configure the SPI controller correctly for DMA in following patches, so initialize the usedma variable before calling into the SoC specific config function.
--- drivers/spi/spi-imx.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/spi/spi-imx.c +8 -7
@@ -815,6 +815,11 @@ static int spi_imx_setupxfer(struct spi_device *spi, spi_imx->tx = spi_imx_buf_tx_u32; } +if (spi_imx_can_dma (spi_imx->bitbang.master, spi, t)) + spi_imx->usedma =1 ; +else + spi_imx->usedma =0 ; + spi_imx->devtype_data->config (spi_imx, &config);return 0 ;
@@ -1040,14 +1045,10 @@ static int spi_imx_transfer(struct spi_device *spi, {struct spi_imx_data *spi_imx =spi_master_get_devdata (spi->master); -if (spi_imx->bitbang.master->can_dma && -spi_imx_can_dma (spi_imx->bitbang.master, spi, transfer)) { - spi_imx->usedma = true; +if (spi_imx->usedma)return spi_imx_dma_transfer (spi_imx, transfer); - } - spi_imx->usedma = false; - -return spi_imx_pio_transfer (spi, transfer); +else +return spi_imx_pio_transfer (spi, transfer); }static int spi_imx_setup (struct spi_device *spi)
Signature · 1 line
2.7.0
Sascha Hauer
maintainerPATCH 3/9 spi: imx: drop unnecessary read/modify/write
In reply to Sascha HauerWhen the MX51_ECSPI_DMA is configured we control every single bit of the register, so there's no need to read/modify/write it. Instead just write the value we want to have in the register. Also, drop unnecessary check if we are actually doing DMA. The values written to the register have no effect in PIO mode and value written there during the last DMA transfer is still in the register, so we can equally well always write a value.
--- drivers/spi/spi-imx.c | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-)
drivers/spi/spi-imx.c +10 -21
@@ -240,9 +240,9 @@ static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,#define MX51_ECSPI_DMA_RXT_WML_OFFSET 24#define MX51_ECSPI_DMA_RXT_WML_MASK (0x3F << 24) -#define MX51_ECSPI_DMA_TEDEN_OFFSET 7 -#define MX51_ECSPI_DMA_RXDEN_OFFSET 23 -#define MX51_ECSPI_DMA_RXTDEN_OFFSET 31 +#define MX51_ECSPI_DMA_TEDEN (1 << 7) +#define MX51_ECSPI_DMA_RXDEN (1 << 23) +#define MX51_ECSPI_DMA_RXTDEN (1 << 31)#define MX51_ECSPI_STAT 0x18#define MX51_ECSPI_STAT_RR (1 << 3)
@@ -318,8 +318,7 @@ static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx)static int __maybe_unused mx51_ecspi_config (struct spi_imx_data *spi_imx,struct spi_imx_config *config) { -u32 ctrl =MX51_ECSPI_CTRL_ENABLE , cfg =0 , dma =0 ; -u32 tx_wml_cfg, rx_wml_cfg, rxt_wml_cfg; +u32 ctrl =MX51_ECSPI_CTRL_ENABLE , cfg =0 ;u32 clk = config->speed_hz, delay, reg;/*
@@ -392,22 +391,12 @@ static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,* Configure the DMA register: setup the watermark * and enable DMA request. */ -if (spi_imx->dma_is_inited) { - dma =readl (spi_imx->base +MX51_ECSPI_DMA ); - - rx_wml_cfg = spi_imx->wml <<MX51_ECSPI_DMA_RX_WML_OFFSET ; - tx_wml_cfg = spi_imx->wml <<MX51_ECSPI_DMA_TX_WML_OFFSET ; - rxt_wml_cfg = spi_imx->wml <<MX51_ECSPI_DMA_RXT_WML_OFFSET ; - dma = (dma & ~MX51_ECSPI_DMA_TX_WML_MASK - & ~MX51_ECSPI_DMA_RX_WML_MASK - & ~MX51_ECSPI_DMA_RXT_WML_MASK ) - | rx_wml_cfg | tx_wml_cfg | rxt_wml_cfg - |(1 <<MX51_ECSPI_DMA_TEDEN_OFFSET ) - |(1 <<MX51_ECSPI_DMA_RXDEN_OFFSET ) - |(1 <<MX51_ECSPI_DMA_RXTDEN_OFFSET ); - -writel (dma, spi_imx->base +MX51_ECSPI_DMA ); - } + +writel (spi_imx->wml <<MX51_ECSPI_DMA_RX_WML_OFFSET | + spi_imx->wml <<MX51_ECSPI_DMA_TX_WML_OFFSET | + spi_imx->wml <<MX51_ECSPI_DMA_RXT_WML_OFFSET | +MX51_ECSPI_DMA_TEDEN |MX51_ECSPI_DMA_RXDEN | +MX51_ECSPI_DMA_RXTDEN , spi_imx->base +MX51_ECSPI_DMA );return 0 ; }
Signature · 1 line
2.7.0
Sascha Hauer
maintainerPATCH 4/9 spi: imx: drop unncessary dma_is_inited variable
In reply to Sascha HauerThere's no need for an extra dma_is_inited variable when we can equally well check for the existence of a DMA channel.
--- drivers/spi/spi-imx.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/spi/spi-imx.c +1 -5
@@ -102,7 +102,6 @@ struct spi_imx_data {unsigned int txfifo;/* number of words pushed in tx FIFO */ /* DMA */ -unsigned int dma_is_inited;unsigned int dma_finished;bool usedma;u32 wml;
@@ -205,7 +204,7 @@ static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi, {struct spi_imx_data *spi_imx =spi_master_get_devdata (master); -if (spi_imx->dma_is_inited && transfer->len >= spi_imx->wml && +if (master->dma_rx && transfer->len >= spi_imx->wml && (transfer->len % spi_imx->wml) ==0 )return true;return false;
@@ -827,8 +826,6 @@ static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)dma_release_channel (master->dma_tx); master->dma_tx =NULL ; } - - spi_imx->dma_is_inited =0 ; } static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
@@ -888,7 +885,6 @@ static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx, master->max_dma_len =MAX_SDMA_BD_BYTES ; spi_imx->bitbang.master->flags =SPI_MASTER_MUST_RX |SPI_MASTER_MUST_TX ; - spi_imx->dma_is_inited =1 ;return 0 ; err:
Signature · 1 line
2.7.0
Sascha Hauer
maintainerPATCH 5/9 spi: imx: add support for all SPI word width for DMA
In reply to Sascha HauerFrom: Anton Bondarenko <[email protected]>
DMA transfer for SPI was limited to up to 8 bits word size until now. Sync in SPI burst size and DMA bus width is necessary to correctly support 16 and 32 BPW.
--- drivers/spi/spi-imx.c | 118 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 27 deletions(-)
drivers/spi/spi-imx.c +91 -27
@@ -89,11 +89,15 @@ struct spi_imx_data {struct completion xfer_done;void __iomem *base; +unsigned long base_phys; +struct clk *clk_per;struct clk *clk_ipg;unsigned long spi_clk;unsigned int spi_bus_clk; +unsigned int bytes_per_word; +unsigned int count;void (*tx)(struct spi_imx_data *);void (*rx)(struct spi_imx_data *);
@@ -199,15 +203,35 @@ static unsigned int spi_imx_clkdiv_2(unsigned int fin,return 7 ; } +static int spi_imx_bytes_per_word (const int bpw) +{ +return DIV_ROUND_UP (bpw,BITS_PER_BYTE ); +} +static bool spi_imx_can_dma (struct spi_master *master,struct spi_device *spi,struct spi_transfer *transfer) {struct spi_imx_data *spi_imx =spi_master_get_devdata (master); +unsigned int bpw = transfer->bits_per_word; + +if (!master->dma_rx) +return false; + +if (!bpw) + bpw = spi->bits_per_word; + + bpw =spi_imx_bytes_per_word (bpw); + +if (bpw !=1 && bpw !=2 && bpw !=4 ) +return false; + +if (transfer->len < spi_imx->wml * bpw) +return false; + +if (transfer->len % (spi_imx->wml * bpw)) +return false; -if (master->dma_rx && transfer->len >= spi_imx->wml && - (transfer->len % spi_imx->wml) ==0 ) -return true; -return false; +return true; }#define MX51_ECSPI_CTRL 0x08
@@ -775,11 +799,63 @@ static irqreturn_t spi_imx_isr(int irq, void *dev_id)return IRQ_HANDLED ; } +static int spi_imx_dma_configure (struct spi_master *master, +int bytes_per_word) +{ +int ret; +enum dma_slave_buswidth buswidth; +struct dma_slave_config rx = {}, tx = {}; +struct spi_imx_data *spi_imx =spi_master_get_devdata (master); + +if (bytes_per_word == spi_imx->bytes_per_word) +/* Same as last time */ +return 0 ; + +switch (bytes_per_word) { +case 4 : + buswidth =DMA_SLAVE_BUSWIDTH_4_BYTES ; +break ; +case 2 : + buswidth =DMA_SLAVE_BUSWIDTH_2_BYTES ; +break ; +case 1 : + buswidth =DMA_SLAVE_BUSWIDTH_1_BYTE ; +break ; +default : +return -EINVAL ; + } + + tx.direction =DMA_MEM_TO_DEV ; + tx.dst_addr = spi_imx->base_phys +MXC_CSPITXDATA ; + tx.dst_addr_width = buswidth; + tx.dst_maxburst = spi_imx->wml; + ret =dmaengine_slave_config (master->dma_tx, &tx); +if (ret) { +dev_err (spi_imx->dev,"TX dma configuration failed with %d\n" , ret); +return ret; + } + + rx.direction =DMA_DEV_TO_MEM ; + rx.src_addr = spi_imx->base_phys +MXC_CSPIRXDATA ; + rx.src_addr_width = buswidth; + rx.src_maxburst = spi_imx->wml; + ret =dmaengine_slave_config (master->dma_rx, &rx); +if (ret) { +dev_err (spi_imx->dev,"RX dma configuration failed with %d\n" , ret); +return ret; + } + + spi_imx->bytes_per_word = bytes_per_word; + +return 0 ; +} +static int spi_imx_setupxfer (struct spi_device *spi,struct spi_transfer *t) {struct spi_imx_data *spi_imx =spi_master_get_devdata (spi->master);struct spi_imx_config config; +int ret; config.bpw = t ? t->bits_per_word : spi->bits_per_word; config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
@@ -808,6 +884,13 @@ static int spi_imx_setupxfer(struct spi_device *spi,else spi_imx->usedma =0 ; +if (spi_imx->usedma) { + ret =spi_imx_dma_configure (spi->master, +spi_imx_bytes_per_word (config.bpw)); +if (ret) +return ret; + } + spi_imx->devtype_data->config (spi_imx, &config);return 0 ;
@@ -829,10 +912,8 @@ static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx) }static int spi_imx_sdma_init (struct device *dev,struct spi_imx_data *spi_imx, -struct spi_master *master, -const struct resource *res) +struct spi_master *master) { -struct dma_slave_config slave_config = {};int ret;/* use pio mode for i.mx6dl chip TKT238285 */
@@ -850,16 +931,6 @@ static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx, goto err; } - slave_config.direction =DMA_MEM_TO_DEV ; - slave_config.dst_addr = res->start +MXC_CSPITXDATA ; - slave_config.dst_addr_width =DMA_SLAVE_BUSWIDTH_1_BYTE ; - slave_config.dst_maxburst = spi_imx->wml; - ret =dmaengine_slave_config (master->dma_tx, &slave_config); -if (ret) { -dev_err (dev,"error in TX dma configuration.\n" ); - goto err; - } -/* Prepare for RX : */ master->dma_rx =dma_request_slave_channel_reason (dev,"rx" );if (IS_ERR (master->dma_rx)) {
@@ -869,15 +940,7 @@ static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx, goto err; } - slave_config.direction =DMA_DEV_TO_MEM ; - slave_config.src_addr = res->start +MXC_CSPIRXDATA ; - slave_config.src_addr_width =DMA_SLAVE_BUSWIDTH_1_BYTE ; - slave_config.src_maxburst = spi_imx->wml; - ret =dmaengine_slave_config (master->dma_rx, &slave_config); -if (ret) { -dev_err (dev,"error in RX dma configuration.\n" ); - goto err; - } +spi_imx_dma_configure (master,1 );init_completion (&spi_imx->dma_rx_completion);init_completion (&spi_imx->dma_tx_completion);
@@ -1164,6 +1227,7 @@ static int spi_imx_probe(struct platform_device *pdev) ret =PTR_ERR (spi_imx->base); goto out_master_put; } + spi_imx->base_phys = res->start; irq =platform_get_irq (pdev,0 );if (irq <0 ) {
@@ -1204,7 +1268,7 @@ static int spi_imx_probe(struct platform_device *pdev)* other chips. */ if (is_imx51_ecspi (spi_imx)) { - ret =spi_imx_sdma_init (&pdev->dev, spi_imx, master, res); + ret =spi_imx_sdma_init (&pdev->dev, spi_imx, master);if (ret == -EPROBE_DEFER ) goto out_clk_put;
Signature · 1 line
2.7.0
PATCH 5/9 spi: imx: add support for all SPI word width for DMA
Hi Anton, Sascha,
Quoted 183 lines · Sascha Hauer
From: Anton Bondarenko <[email protected]>
DMA transfer for SPI was limited to up to 8 bits word size until now. Sync in SPI burst size and DMA bus width is necessary to correctly support 16 and 32 BPW.
Signed-off-by Anton Bondarenko <[email protected]>Signed-off-by Sascha Hauer <[email protected]>--- drivers/spi/spi-imx.c | 118 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 27 deletions(-)drivers/spi/spi-imx.c +88
-17@@ -89,11 +89,15 @@ struct spi_imx_data {struct completion xfer_done;void __iomem *base; +unsigned long base_phys; +struct clk *clk_per;struct clk *clk_ipg;unsigned long spi_clk;unsigned int spi_bus_clk; +unsigned int bytes_per_word; +unsigned int count;void (*tx)(struct spi_imx_data *);void (*rx)(struct spi_imx_data *);@@ -199,15 +203,35 @@ static unsigned int spi_imx_clkdiv_2(unsigned int fin,return 7 ; } +static int spi_imx_bytes_per_word (const int bpw) +{ +return DIV_ROUND_UP (bpw,BITS_PER_BYTE ); +} +static bool spi_imx_can_dma (struct spi_master *master,struct spi_device *spi,struct spi_transfer *transfer) {struct spi_imx_data *spi_imx =spi_master_get_devdata (master); +unsigned int bpw = transfer->bits_per_word; + +if (!master->dma_rx) +return false; + +if (!bpw) + bpw = spi->bits_per_word; + + bpw =spi_imx_bytes_per_word (bpw); + +if (bpw !=1 && bpw !=2 && bpw !=4 ) +return false; + +if (transfer->len < spi_imx->wml * bpw) +return false; + +if (transfer->len % (spi_imx->wml * bpw)) +return false; -if (master->dma_rx && transfer->len >= spi_imx->wml && - (transfer->len % spi_imx->wml) ==0 ) -return true; -return false; +return true; }#define MX51_ECSPI_CTRL 0x08@@ -775,11 +799,63 @@ static irqreturn_t spi_imx_isr(int irq, void *dev_id)return IRQ_HANDLED ; } +static int spi_imx_dma_configure (struct spi_master *master, +int bytes_per_word) +{ +int ret; +enum dma_slave_buswidth buswidth; +struct dma_slave_config rx = {}, tx = {}; +struct spi_imx_data *spi_imx =spi_master_get_devdata (master); + +if (bytes_per_word == spi_imx->bytes_per_word) +/* Same as last time */ +return 0 ; + +switch (bytes_per_word) { +case 4 : + buswidth =DMA_SLAVE_BUSWIDTH_4_BYTES ; +break ; +case 2 : + buswidth =DMA_SLAVE_BUSWIDTH_2_BYTES ; +break ; +case 1 : + buswidth =DMA_SLAVE_BUSWIDTH_1_BYTE ; +break ; +default : +return -EINVAL ; + } + + tx.direction =DMA_MEM_TO_DEV ; + tx.dst_addr = spi_imx->base_phys +MXC_CSPITXDATA ; + tx.dst_addr_width = buswidth; + tx.dst_maxburst = spi_imx->wml; + ret =dmaengine_slave_config (master->dma_tx, &tx); +if (ret) { +dev_err (spi_imx->dev,"TX dma configuration failed with %d\n" , ret); +return ret; + } + + rx.direction =DMA_DEV_TO_MEM ; + rx.src_addr = spi_imx->base_phys +MXC_CSPIRXDATA ; + rx.src_addr_width = buswidth; + rx.src_maxburst = spi_imx->wml; + ret =dmaengine_slave_config (master->dma_rx, &rx); +if (ret) { +dev_err (spi_imx->dev,"RX dma configuration failed with %d\n" , ret); +return ret; + } + + spi_imx->bytes_per_word = bytes_per_word; + +return 0 ; +} +static int spi_imx_setupxfer (struct spi_device *spi,struct spi_transfer *t) {struct spi_imx_data *spi_imx =spi_master_get_devdata (spi->master);struct spi_imx_config config; +int ret; config.bpw = t ? t->bits_per_word : spi->bits_per_word; config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;@@ -808,6 +884,13 @@ static int spi_imx_setupxfer(struct spi_device *spi,else spi_imx->usedma =0 ; +if (spi_imx->usedma) { + ret =spi_imx_dma_configure (spi->master, +spi_imx_bytes_per_word (config.bpw)); +if (ret) +return ret; + } + spi_imx->devtype_data->config (spi_imx, &config);return 0 ;@@ -829,10 +912,8 @@ static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx) }static int spi_imx_sdma_init (struct device *dev,struct spi_imx_data *spi_imx, -struct spi_master *master, -const struct resource *res) +struct spi_master *master) { -struct dma_slave_config slave_config = {};int ret;/* use pio mode for i.mx6dl chip TKT238285 */ @@ -850,16 +931,6 @@ static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx, goto err; } - slave_config.direction =DMA_MEM_TO_DEV ; - slave_config.dst_addr = res->start +MXC_CSPITXDATA ; - slave_config.dst_addr_width =DMA_SLAVE_BUSWIDTH_1_BYTE ; - slave_config.dst_maxburst = spi_imx->wml; - ret =dmaengine_slave_config (master->dma_tx, &slave_config); -if (ret) { -dev_err (dev,"error in TX dma configuration.\n" ); - goto err; - } -/* Prepare for RX : */ master->dma_rx =dma_request_slave_channel_reason (dev,"rx" );if (IS_ERR (master->dma_rx)) {@@ -869,15 +940,7 @@ static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx, goto err; }
drivers/spi/spi-imx.c static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx, · line 943
Quoted 10 lines
drivers/spi/spi-imx.c +1
-9- slave_config.direction =DMA_DEV_TO_MEM ; - slave_config.src_addr = res->start +MXC_CSPIRXDATA ; - slave_config.src_addr_width =DMA_SLAVE_BUSWIDTH_1_BYTE ; - slave_config.src_maxburst = spi_imx->wml; - ret =dmaengine_slave_config (master->dma_rx, &slave_config); -if (ret) { -dev_err (dev,"error in RX dma configuration.\n" ); - goto err; - } +spi_imx_dma_configure (master,1 );
here it might make sense to check for returned error code.
Quoted 13 lines
drivers/spi/spi-imx.c +1
-0init_completion (&spi_imx->dma_rx_completion);init_completion (&spi_imx->dma_tx_completion);@@ -1164,6 +1227,7 @@ static int spi_imx_probe(struct platform_device *pdev) ret =PTR_ERR (spi_imx->base); goto out_master_put; } + spi_imx->base_phys = res->start; irq =platform_get_irq (pdev,0 );if (irq <0 ) {
drivers/spi/spi-imx.c static int spi_imx_probe(struct platform_device *pdev) · line 1274
Quoted 8 lines
drivers/spi/spi-imx.c +1
-1@@ -1204,7 +1268,7 @@ static int spi_imx_probe(struct platform_device *pdev)* other chips. */ if (is_imx51_ecspi (spi_imx)) { - ret =spi_imx_sdma_init (&pdev->dev, spi_imx, master, res); + ret =spi_imx_sdma_init (&pdev->dev, spi_imx, master);if (ret == -EPROBE_DEFER ) goto out_clk_put;
--
With best wishes,
Vladimir
Sascha Hauer
maintainerPATCH 6/9 spi: imx: remove unnecessary bit clearing in mx51_ecspi_config
In reply to Sascha HauerThis reverts patch 1476253cef (spi: imx: fix ecspi mode setup) The patch tried to fix something by clearing bits in the cfg variable, but cfg is initialized to zero on function entry. There are no bits to clear.
--- drivers/spi/spi-imx.c | 7 ------- 1 file changed, 7 deletions(-)
drivers/spi/spi-imx.c +0 -7
@@ -366,20 +366,13 @@ static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,if (config->mode &SPI_CPHA ) cfg |=MX51_ECSPI_CONFIG_SCLKPHA (config->cs); -else - cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA (config->cs);if (config->mode &SPI_CPOL ) { cfg |=MX51_ECSPI_CONFIG_SCLKPOL (config->cs); cfg |=MX51_ECSPI_CONFIG_SCLKCTL (config->cs); - }else { - cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL (config->cs); - cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL (config->cs); }if (config->mode &SPI_CS_HIGH ) cfg |=MX51_ECSPI_CONFIG_SSBPOL (config->cs); -else - cfg &= ~MX51_ECSPI_CONFIG_SSBPOL (config->cs);/* CTRL register always go first to bring out controller from reset */ writel (ctrl, spi_imx->base +MX51_ECSPI_CTRL );
Signature · 1 line
2.7.0
PATCH 6/9 spi: imx: remove unnecessary bit clearing in mx51_ecspi_config
Hi Sascha,
Quoted 6 lines · Sascha Hauer
This reverts patch
1476253cef(spi: imx: fix ecspi mode setup) The patch tried to fix something by clearing bits in the cfg variable, but cfg is initialized to zero on function entry. There are no bits to clear.Signed-off-by Sascha Hauer <[email protected]>
I believe here the expected fix should be
----8<----
drivers/spi/spi-imx.c +0 -0
@@ -316,9 +316,10 @@ static void __maybe_unused mx51_ecspi_trigger(struct
spi_imx_data *spi_imx)
static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,
struct spi_imx_config *config)
{
- u32 ctrl = MX51_ECSPI_CTRL_ENABLE, cfg = 0, dma = 0;
+ u32 ctrl = MX51_ECSPI_CTRL_ENABLE, dma = 0;
u32 tx_wml_cfg, rx_wml_cfg, rxt_wml_cfg;
u32 clk = config->speed_hz, delay, reg;
+ u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
/*
* The hardware seems to have a race condition when changing modes. The
----8<----
Quoted 20 lines
--- drivers/spi/spi-imx.c | 7 ------- 1 file changed, 7 deletions(-)drivers/spi/spi-imx.c +0
-3@@ -366,20 +366,13 @@ static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,if (config->mode &SPI_CPHA ) cfg |=MX51_ECSPI_CONFIG_SCLKPHA (config->cs); -else - cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA (config->cs);if (config->mode &SPI_CPOL ) { cfg |=MX51_ECSPI_CONFIG_SCLKPOL (config->cs); cfg |=MX51_ECSPI_CONFIG_SCLKCTL (config->cs); - }else {
drivers/spi/spi-imx.c static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx, · line 378
Quoted 10 lines
drivers/spi/spi-imx.c +0
-4- cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL (config->cs); - cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL (config->cs); }if (config->mode &SPI_CS_HIGH ) cfg |=MX51_ECSPI_CONFIG_SSBPOL (config->cs); -else - cfg &= ~MX51_ECSPI_CONFIG_SSBPOL (config->cs);/* CTRL register always go first to bring out controller from reset */ writel (ctrl, spi_imx->base +MX51_ECSPI_CTRL );
--
With best wishes,
Vladimir
Sascha Hauer
maintainerPATCH 6/9 spi: imx: remove unnecessary bit clearing in mx51_ecspi_config
Quoted 19 lines · Vladimir Zapolskiy
Hi Sascha,
Quoted 6 lines · Sascha Hauer
This reverts patch
1476253cef(spi: imx: fix ecspi mode setup) The patch tried to fix something by clearing bits in the cfg variable, but cfg is initialized to zero on function entry. There are no bits to clear.Signed-off-by Sascha Hauer <[email protected]>I believe here the expected fix should be
----8<----
drivers/spi/spi-imx.c +0
-0
drivers/spi/spi-imx.c static void __maybe_unused mx51_ecspi_trigger(struct · line 324
Quoted 10 lines
drivers/spi/spi-imx.c +2
-1@@ -316,9 +316,10 @@ static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx)static int __maybe_unused mx51_ecspi_config (struct spi_imx_data *spi_imx,struct spi_imx_config *config) { -u32 ctrl =MX51_ECSPI_CTRL_ENABLE , cfg =0 , dma =0 ; +u32 ctrl =MX51_ECSPI_CTRL_ENABLE , dma =0 ;u32 tx_wml_cfg, rx_wml_cfg, rxt_wml_cfg;u32 clk = config->speed_hz, delay, reg; +u32 cfg =readl (spi_imx->base +MX51_ECSPI_CONFIG );
With this change applied we would indeed have to clear and not only set the bits, but why would we do this?
Sascha
Signature · 4 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH 6/9 spi: imx: remove unnecessary bit clearing in mx51_ecspi_config
Quoted 25 lines · Sascha Hauer
Quoted 24 lines · Vladimir Zapolskiy
Hi Sascha,
Quoted 6 lines · Sascha Hauer
This reverts patch
1476253cef(spi: imx: fix ecspi mode setup) The patch tried to fix something by clearing bits in the cfg variable, but cfg is initialized to zero on function entry. There are no bits to clear.Signed-off-by Sascha Hauer <[email protected]>I believe here the expected fix should be
----8<----
drivers/spi/spi-imx.c +0
-1@@ -316,9 +316,10 @@ static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx)static int __maybe_unused mx51_ecspi_config (struct spi_imx_data *spi_imx,struct spi_imx_config *config) { -u32 ctrl =MX51_ECSPI_CTRL_ENABLE , cfg =0 , dma =0 ;
Quoted 8 lines
drivers/spi/spi-imx.c static void __maybe_unused mx51_ecspi_trigger(struct · line 324
Quoted 5 lines
drivers/spi/spi-imx.c +2
-0+u32 ctrl =MX51_ECSPI_CTRL_ENABLE , dma =0 ;u32 tx_wml_cfg, rx_wml_cfg, rxt_wml_cfg;u32 clk = config->speed_hz, delay, reg; +u32 cfg =readl (spi_imx->base +MX51_ECSPI_CONFIG );With this change applied we would indeed have to clear and not only set the bits, but why would we do this?
Locally we have quite a similar to 1476253cef change (well, correct version of it) authored by Knut Wohlrab, here is his comment from a discussion:
Please notice that iMX6 ECSPI controller support 4 channels/chip selects at one SPI bus. The original iMX driver implementation(*) did not care about the SPI_CPHA, SPI_CPOL and SPI_CS_HIGH configuration of the not selected channels and set this to "0". Depending on the configuration of this channels this could cause unwanted edges at the clock line when using this channels the next time. To prevent this, we introduce the read of the actual configuration/register content and set only the necessary changes.
(*) here it means the version prior to 1476253cef ("spi: imx: fix ecspi mode setup)
--
With best wishes,
Vladimir
Sascha Hauer
maintainerPATCH 6/9 spi: imx: remove unnecessary bit clearing in mx51_ecspi_config
Quoted 38 lines · Vladimir Zapolskiy
Quoted 25 lines · Sascha Hauer
Quoted 24 lines · Vladimir Zapolskiy
Hi Sascha,
Quoted 6 lines · Sascha Hauer
This reverts patch
1476253cef(spi: imx: fix ecspi mode setup) The patch tried to fix something by clearing bits in the cfg variable, but cfg is initialized to zero on function entry. There are no bits to clear.Signed-off-by Sascha Hauer <[email protected]>I believe here the expected fix should be
----8<----
drivers/spi/spi-imx.c +0
-1@@ -316,9 +316,10 @@ static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx)static int __maybe_unused mx51_ecspi_config (struct spi_imx_data *spi_imx,struct spi_imx_config *config) { -u32 ctrl =MX51_ECSPI_CTRL_ENABLE , cfg =0 , dma =0 ;Quoted 8 lines
drivers/spi/spi-imx.c static void __maybe_unused mx51_ecspi_trigger(struct · line 324
Quoted 5 lines
drivers/spi/spi-imx.c +2
-0+u32 ctrl =MX51_ECSPI_CTRL_ENABLE , dma =0 ;u32 tx_wml_cfg, rx_wml_cfg, rxt_wml_cfg;u32 clk = config->speed_hz, delay, reg; +u32 cfg =readl (spi_imx->base +MX51_ECSPI_CONFIG );With this change applied we would indeed have to clear and not only set the bits, but why would we do this?
Locally we have quite a similar to
1476253cefchange (well, correct version of it) authored by Knut Wohlrab, here is his comment from a discussion:
Quoted 7 lines
Please notice that iMX6 ECSPI controller support 4 channels/chip selects at one SPI bus. The original iMX driver
implementation(*)did not care about theSPI_CPHA,SPI_CPOLandSPI_CS_HIGHconfiguration of the not selected channels and set this to "0". Depending on the configuration of this channels this could cause unwanted edges at the clock line when using this channels the next time. To prevent this, we introduce the read of the actual configuration/register content and set only the necessary changes.
If that's the reason to introduce this reason then we should commit a patch that really fixes this behaviour and has the above reasoning in the commit message. 1476253cef doesn't really change anything. Is it really "could cause unwanted edeges" or "We have seen unwanted edges"?
Sascha
Signature · 4 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Sascha Hauer
maintainerPATCH 7/9 spi: imx: make some register defines simpler
In reply to Sascha HauerThe watermark levels in the DMA register are write only, the driver should never have to read them back from the hardware. Replace the current _MASK and _OFFSET defines with defines taking the watermark level directly.
--- drivers/spi/spi-imx.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/spi/spi-imx.c +6 -9
@@ -256,12 +256,9 @@ static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,#define MX51_ECSPI_INT_RREN (1 << 3)#define MX51_ECSPI_DMA 0x14 -#define MX51_ECSPI_DMA_TX_WML_OFFSET 0 -#define MX51_ECSPI_DMA_TX_WML_MASK 0x3F -#define MX51_ECSPI_DMA_RX_WML_OFFSET 16 -#define MX51_ECSPI_DMA_RX_WML_MASK (0x3F << 16) -#define MX51_ECSPI_DMA_RXT_WML_OFFSET 24 -#define MX51_ECSPI_DMA_RXT_WML_MASK (0x3F << 24) +#define MX51_ECSPI_DMA_TX_WML (wml) ((wml) & 0x3f) +#define MX51_ECSPI_DMA_RX_WML (wml) (((wml) & 0x3f) << 16) +#define MX51_ECSPI_DMA_RXT_WML (wml) (((wml) & 0x3f) << 24)#define MX51_ECSPI_DMA_TEDEN (1 << 7)#define MX51_ECSPI_DMA_RXDEN (1 << 23)
@@ -408,9 +405,9 @@ static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,* and enable DMA request. */ -writel (spi_imx->wml <<MX51_ECSPI_DMA_RX_WML_OFFSET | - spi_imx->wml <<MX51_ECSPI_DMA_TX_WML_OFFSET | - spi_imx->wml <<MX51_ECSPI_DMA_RXT_WML_OFFSET | +writel (MX51_ECSPI_DMA_RX_WML (spi_imx->wml) | +MX51_ECSPI_DMA_TX_WML (spi_imx->wml) | +MX51_ECSPI_DMA_RXT_WML (spi_imx->wml) |MX51_ECSPI_DMA_TEDEN |MX51_ECSPI_DMA_RXDEN |MX51_ECSPI_DMA_RXTDEN , spi_imx->base +MX51_ECSPI_DMA );
Signature · 1 line
2.7.0
Sascha Hauer
maintainerPATCH 8/9 spi: imx: set MX51_ECSPI_CTRL_SMC bit in setup function
In reply to Sascha HauerNow that the config function knows whether we are doing DMA or not we can do the necessary register setup in the config function and no longer have to do this in the trigger function. With this the trigger function becomes a no-op for DMA, so instead of testing if we are doing DMA or not in the trigger function we simply no longer call it in the DMA case.
--- drivers/spi/spi-imx.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-)
drivers/spi/spi-imx.c +6 -15
@@ -106,7 +106,6 @@ struct spi_imx_data {unsigned int txfifo;/* number of words pushed in tx FIFO */ /* DMA */ -unsigned int dma_finished;bool usedma;u32 wml;struct completion dma_rx_completion;
@@ -324,14 +323,10 @@ static void __maybe_unused mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, intstatic void __maybe_unused mx51_ecspi_trigger (struct spi_imx_data *spi_imx) { -u32 reg =readl (spi_imx->base +MX51_ECSPI_CTRL ); +u32 reg; -if (!spi_imx->usedma) - reg |=MX51_ECSPI_CTRL_XCH ; -else if (!spi_imx->dma_finished) - reg |=MX51_ECSPI_CTRL_SMC ; -else - reg &= ~MX51_ECSPI_CTRL_SMC ; + reg =readl (spi_imx->base +MX51_ECSPI_CTRL ); + reg |=MX51_ECSPI_CTRL_XCH ;writel (reg, spi_imx->base +MX51_ECSPI_CTRL ); }
@@ -371,6 +366,9 @@ static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,if (config->mode &SPI_CS_HIGH ) cfg |=MX51_ECSPI_CONFIG_SSBPOL (config->cs); +if (spi_imx->usedma) + ctrl |=MX51_ECSPI_CTRL_SMC ; +/* CTRL register always go first to bring out controller from reset */ writel (ctrl, spi_imx->base +MX51_ECSPI_CTRL );
@@ -1012,9 +1010,6 @@ static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,reinit_completion (&spi_imx->dma_rx_completion);reinit_completion (&spi_imx->dma_tx_completion); -/* Trigger the cspi module. */ - spi_imx->dma_finished =0 ; -/* * Set these order to avoid potential RX overflow. The overflow may * happen if we enable SPI HW before starting RX DMA due to rescheduling
@@ -1025,7 +1020,6 @@ static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,*/ dma_async_issue_pending (master->dma_rx);dma_async_issue_pending (master->dma_tx); - spi_imx->devtype_data->trigger (spi_imx); transfer_timeout =spi_imx_calculate_timeout (spi_imx, transfer->len);
@@ -1046,9 +1040,6 @@ static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx, } } - spi_imx->dma_finished =1 ; - spi_imx->devtype_data->trigger (spi_imx); -if (!timeout) ret = -ETIMEDOUT ;else
Signature · 1 line
2.7.0
Sascha Hauer
maintainerPATCH 9/9 spi: imx: drop bogus tests for rx/tx bufs in DMA transfer
In reply to Sascha HauerThe driver tries to be clever by only setting up DMA channels when the corresponding sg tables are non NULL. The sg tables are embedded structs in struct spi_transfer, so they are guaranteed to be non NULL which makes the if(tx)/if(rx) tests completely bogus. The driver even sets the SPI_MASTER_MUST_RX / SPI_MASTER_MUST_TX flags which makes sure the sg tables are not only present but also non empty.
Drop the tests and make the DMA path easier to follow.
--- drivers/spi/spi-imx.c | 82 +++++++++++++++++++++------------------------------ 1 file changed, 34 insertions(+), 48 deletions(-)
drivers/spi/spi-imx.c +34 -48
@@ -974,51 +974,40 @@ static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)static int spi_imx_dma_transfer (struct spi_imx_data *spi_imx,struct spi_transfer *transfer) { -struct dma_async_tx_descriptor *desc_tx =NULL , *desc_rx =NULL ; -int ret; +struct dma_async_tx_descriptor *desc_tx, *desc_rx;unsigned long transfer_timeout;unsigned long timeout;struct spi_master *master = spi_imx->bitbang.master;struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg; -if (tx) { - desc_tx =dmaengine_prep_slave_sg (master->dma_tx, - tx->sgl, tx->nents,DMA_MEM_TO_DEV , -DMA_PREP_INTERRUPT |DMA_CTRL_ACK ); -if (!desc_tx) -return -EINVAL ; - - desc_tx->callback = spi_imx_dma_tx_callback; - desc_tx->callback_param = (void *)spi_imx; -dmaengine_submit (desc_tx); - } +/* +* The TX DMA setup starts the transfer, so make sure RX is configured +* before TX. +*/ + desc_rx =dmaengine_prep_slave_sg (master->dma_rx, + rx->sgl, rx->nents,DMA_DEV_TO_MEM , +DMA_PREP_INTERRUPT |DMA_CTRL_ACK ); +if (!desc_rx) +return -EINVAL ; -if (rx) { - desc_rx =dmaengine_prep_slave_sg (master->dma_rx, - rx->sgl, rx->nents,DMA_DEV_TO_MEM , -DMA_PREP_INTERRUPT |DMA_CTRL_ACK ); -if (!desc_rx) { -dmaengine_terminate_all (master->dma_tx); -return -EINVAL ; - } + desc_rx->callback = spi_imx_dma_rx_callback; + desc_rx->callback_param = (void *)spi_imx; +dmaengine_submit (desc_rx); +reinit_completion (&spi_imx->dma_rx_completion); +dma_async_issue_pending (master->dma_rx); - desc_rx->callback = spi_imx_dma_rx_callback; - desc_rx->callback_param = (void *)spi_imx; -dmaengine_submit (desc_rx); + desc_tx =dmaengine_prep_slave_sg (master->dma_tx, + tx->sgl, tx->nents,DMA_MEM_TO_DEV , +DMA_PREP_INTERRUPT |DMA_CTRL_ACK ); +if (!desc_tx) { +dmaengine_terminate_all (master->dma_tx); +return -EINVAL ; } -reinit_completion (&spi_imx->dma_rx_completion); + desc_tx->callback = spi_imx_dma_tx_callback; + desc_tx->callback_param = (void *)spi_imx; +dmaengine_submit (desc_tx);reinit_completion (&spi_imx->dma_tx_completion); - -/* -* Set these order to avoid potential RX overflow. The overflow may -* happen if we enable SPI HW before starting RX DMA due to rescheduling -* for another task and/or interrupt. -* So RX DMA enabled first to make sure data would be read out from FIFO -* ASAP. TX DMA enabled next to start filling TX FIFO with new data. -* And finaly SPI HW enabled to start actual data transfer. -*/ -dma_async_issue_pending (master->dma_rx);dma_async_issue_pending (master->dma_tx); transfer_timeout =spi_imx_calculate_timeout (spi_imx, transfer->len);
@@ -1030,22 +1019,19 @@ static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,dev_err (spi_imx->dev,"I/O Error in DMA TX\n" );dmaengine_terminate_all (master->dma_tx);dmaengine_terminate_all (master->dma_rx); - }else { - timeout =wait_for_completion_timeout ( - &spi_imx->dma_rx_completion, transfer_timeout); -if (!timeout) { -dev_err (spi_imx->dev,"I/O Error in DMA RX\n" ); - spi_imx->devtype_data->reset (spi_imx); -dmaengine_terminate_all (master->dma_rx); - } +return -ETIMEDOUT ; } -if (!timeout) - ret = -ETIMEDOUT ; -else - ret = transfer->len; + timeout =wait_for_completion_timeout (&spi_imx->dma_rx_completion, + transfer_timeout); +if (!timeout) { +dev_err (&master->dev,"I/O Error in DMA RX\n" ); + spi_imx->devtype_data->reset (spi_imx); +dmaengine_terminate_all (master->dma_rx); +return -ETIMEDOUT ; + } -return ret; +return transfer->len; } static int spi_imx_pio_transfer(struct spi_device *spi,
Signature · 1 line
2.7.0