Skip to contentSkip to conversation

Add support for STM32 DMA

PATCH Superseded M'boumba Cedric Madianga 3 participants 21 messages Oct 13, 2015 – Oct 15, 2015

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add support for STM32 DMA

This patchset adds support for the STM32 DMA controller. This controller provides 8 channels dedicated to managing memory access request from one or more peripherals.
Each stream can have up to 8 requests in total.

Changes since v1:
- remove dmamux boolean as it is not needed
- replace dma by DMA in Kconfig
- add default return value in stm32_dma_get_width()
- add defalut return value in stm32_dma_get_burst()
- use lower case for constant hexadecimal values

M'boumba Cedric Madianga (4):
dt-bindings: Document the STM32 DMA bindings
dmaengine: Add STM32 DMA driver
ARM: dts: Add STM32 DMA support for STM32F429 MCU
ARM: configs: Add STM32 DMA support in STM32 defconfig

 .../devicetree/bindings/dma/stm32-dma.txt          |   98 ++
 arch/arm/boot/dts/stm32f429.dtsi                   |   31 +
 arch/arm/configs/stm32_defconfig                   |    2 +
 drivers/dma/Kconfig                                |   12 +
 drivers/dma/Makefile                               |    1 +
 drivers/dma/stm32-dma.c                            | 1175 ++++++++++++++++++++
 6 files changed, 1319 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dma/stm32-dma.txt
 create mode 100644 drivers/dma/stm32-dma.c
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/4 dt-bindings: Document the STM32 DMA bindings

This patch adds documentation of device tree bindings for the STM32 dma controller.

---
 .../devicetree/bindings/dma/stm32-dma.txt          | 98 ++++++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dma/stm32-dma.txt
Documentation/devicetree/bindings/dma/stm32-dma.txt new file +98 -0
new file mode 100644
index 0000000..9ce0d49
@@ -0,0 +1,98 @@
+* STMicroelectronics STM32 DMA controller
+
+The STM32 DMA is a general-purpose direct memory access controller capable of
+supporting 8 independent DMA channels. Each channel can have up to 8 requests.
+
+Required properties:
+- compatible: Should be "st,stm32-dma"
+- reg: Should contain DMA registers location and length. This should include
+  all of the per-channel registers.
+- interrupts: Should contain all of the per-channel DMA interrupts.
+- clocks: Should contain the input clock of the DMA instance.
+- #dma-cells : Must be <4>. See DMA client paragraph for more details.
+
+Optional properties:
+- resets: Reference to a reset controller asserting the DMA controller
+- st,mem2mem: boolean; if defined, it indicates that the controller supports
+  memory-to-memory transfer
+
+Example:
+
+	dma2: dma-controller at 40026400 {
+		compatible = "st,stm32-dma";
+		reg = <0x40026400 0x400>;
+		interrupts = <56>,
+			     <57>,
+			     <58>,
+			     <59>,
+			     <60>,
+			     <68>,
+			     <69>,
+			     <70>;
+		clocks = <&clk_hclk>;
+		#dma-cells = <4>;
+		st,mem2mem;
+		resets = <&rcc 150>;
+	};
+
+* DMA client
+
+Required properties:
+- dmas: Comma separated list of dma channel requests
+- dma-names: Names of the aforementioned requested channels
+
+Each dmas request consists of 5 cells:
+1. A phandle pointing to the STM32 DMA controller
+2. The channel id
+3. The request line number
+4. A 32bit mask specifying the DMA channel configuration
+ -bit 1: Direct Mode Error Interrupt
+	0x0: disabled
+	0x1: enabled
+ -bit 2: Transfer Error Interrupt
+	0x0: disabled
+	0x1: enabled
+ -bit 3: Half Transfer Mode Error Interrupt
+	0x0: disabled
+	0x1: enabled
+ -bit 4: Transfer Complete Interrupt
+	0x0: disabled
+	0x1: enabled
+ -bit 9: Peripheral Increment Address
+	0x0: no address increment between transfers
+	0x1: increment address between transfers
+ -bit 10: Memory Increment Address
+	0x0: no address increment between transfers
+	0x1: increment address between transfers
+ -bit 15: Peripheral Increment Offset Size
+	0x0: offset size is linked to the peripheral bus width
+	0x1: offset size is fixed to 4 (32-bit alignment)
+ -bit 16-17: Priority level
+	0x0: low
+	0x1: medium
+	0x2: high
+	0x3: very high
+5. A 32bit mask specifying the DMA FIFO configuration
+ -bit 0-1: Fifo threshold
+	0x0: 1/4 full FIFO
+	0x1: 1/2 full FIFO
+	0x2: 3/4 full FIFO
+	0x3:full FIFO
+ -bit 2: Direct mode
+	0x0: enabled
+	0x1: disabled
+ -bit 7: FIFO Error Interrupt
+	0x0: disabled
+	0x1: enabled
+
+Example:
+
+	usart1: serial at 40011000 {
+		compatible = "st,stm32-usart", "st,stm32-uart";
+		reg = <0x40011000 0x400>;
+		interrupts = <37>;
+		clocks = <&clk_pclk2>;
+		dmas = <&dma2 2 4 0x20610 0x3>,
+		       <&dma2 7 5 0x20610 0x3>;
+		dma-names = "rx", "tx";
+	};
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v2 1/4] dt-bindings: Document the STM32 DMA bindings

Hi Mark,

Documentation/devicetree/bindings/dma/stm32-dma.txt line 87

Quoted 10 lines

+5. A 32bit mask specifying the DMA FIFO configuration
+ -bit 0-1: Fifo threshold

Documentation/devicetree/bindings/dma/stm32-dma.txt new file +8 -0
+       0x0: 1/4 full FIFO
+       0x1: 1/2 full FIFO
+       0x2: 3/4 full FIFO
+       0x3:full FIFO
+ -bit 2: Direct mode
+       0x0: enabled
+       0x1: disabled
+

Finally, I find a way to handle the above inputs outside DT. So, I will remove it from DT in the next version. Thanks.

To sum-up, the remaining information that the client will have to specify from DT are describing below.
Is it ok ?

Quoted 4 lines

+ -bit 9: Peripheral Increment Address

Documentation/devicetree/bindings/dma/stm32-dma.txt new file +3 -0
+       0x0: no address increment between transfers
+       0x1: increment address between transfers
+ -bit 10: Memory Increment Address

Documentation/devicetree/bindings/dma/stm32-dma.txt line 74

Quoted 10 lines
Documentation/devicetree/bindings/dma/stm32-dma.txt new file +10 -0
+       0x0: no address increment between transfers
+       0x1: increment address between transfers
+ -bit 15: Peripheral Increment Offset Size
+       0x0: offset size is linked to the peripheral bus width
+       0x1: offset size is fixed to 4 (32-bit alignment)
+ -bit 16-17: Priority level
+       0x0: low
+       0x1: medium
+       0x2: high
+       0x3: very high

BR,
Cedric

#3 lore raw

PATCH v2 2/4 dmaengine: Add STM32 DMA driver

In reply to M'boumba Cedric Madianga

This patch adds support for the STM32 DMA controller.

---
 drivers/dma/Kconfig     |   12 +
 drivers/dma/Makefile    |    1 +
 drivers/dma/stm32-dma.c | 1175 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1188 insertions(+)
 create mode 100644 drivers/dma/stm32-dma.c
drivers/dma/Kconfig +12 -0
index 5c931d4..a5dd649 100644
@@ -431,6 +431,18 @@ config STE_DMA40
 	help
 	  Support for ST-Ericsson DMA40 controller
 
+config STM32_DMA
+	tristate "STMicroelectronics STM32 DMA support"
+	depends on ARCH_STM32
+	select DMA_ENGINE
+	select DMA_OF
+	select DMA_VIRTUAL_CHANNELS
+	help
+	  Enable support for the on-chip DMA controller on STMicroelectronics
+	  STM32 MCUs.
+	  If you have a board based on such a MCU and wish to use DMA say Y or M
+	  here.
+
 config S3C24XX_DMAC
 	tristate "Samsung S3C24XX DMA support"
 	depends on ARCH_S3C24XX
drivers/dma/Makefile +1 -0
index ef9c099..2dd0a067 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_QCOM_BAM_DMA) += qcom_bam_dma.o
 obj-$(CONFIG_RENESAS_DMA) += sh/
 obj-$(CONFIG_SIRF_DMA) += sirf-dma.o
 obj-$(CONFIG_STE_DMA40) += ste_dma40.o ste_dma40_ll.o
+obj-$(CONFIG_STM32_DMA) += stm32-dma.o
 obj-$(CONFIG_S3C24XX_DMAC) += s3c24xx-dma.o
 obj-$(CONFIG_TXX9_DMAC) += txx9dmac.o
 obj-$(CONFIG_TEGRA20_APB_DMA) += tegra20-apb-dma.o
drivers/dma/stm32-dma.c new file +1175 -0

1,175 lines not on this page · raw message

Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Quoted 29 lines · M'boumba Cedric Madianga

This patch adds support for the STM32 DMA controller.

---
  drivers/dma/Kconfig     |   12 +
  drivers/dma/Makefile    |    1 +
  drivers/dma/stm32-dma.c | 1175 +++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 1188 insertions(+)

create mode 100644 drivers/dma/stm32-dma.c

drivers/dma/stm32-dma.c new file +13 -0
new file mode 100644
index 0000000..031bab7
 
+enum stm32_dma_width {
+	STM32_DMA_BYTE,
+	STM32_DMA_HALF_WORD,
+	STM32_DMA_WORD,
+};
+
+enum stm32_dma_burst_size {
+	STM32_DMA_BURST_SINGLE,
+	STM32_DMA_BURST_INCR4,
+	STM32_DMA_BURST_INCR8,
+	STM32_DMA_BURST_INCR16,
+};
+

drivers/dma/stm32-dma.c line 144

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+enum stm32_dma_channel_id {
+	STM32_DMA_CHANNEL0,
+	STM32_DMA_CHANNEL1,
+	STM32_DMA_CHANNEL2,
+	STM32_DMA_CHANNEL3,
+	STM32_DMA_CHANNEL4,
+	STM32_DMA_CHANNEL5,
+	STM32_DMA_CHANNEL6,
+	STM32_DMA_CHANNEL7,
+};

Why have (unused) enumerations to map numeric symbols to their own natural values? Using normal integers would be better.

drivers/dma/stm32-dma.c line 155

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+enum stm32_dma_request_id {
+	STM32_DMA_REQUEST0,
+	STM32_DMA_REQUEST1,
+	STM32_DMA_REQUEST2,
+	STM32_DMA_REQUEST3,
+	STM32_DMA_REQUEST4,
+	STM32_DMA_REQUEST5,
+	STM32_DMA_REQUEST6,
+	STM32_DMA_REQUEST7,
+};

Ditto.

Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
+{
+	struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+
+	dev_dbg(chan2dev(chan), "SCR:   0x%08x\n",
+		stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)));
+	dev_dbg(chan2dev(chan), "NDTR:  0x%08x\n",

drivers/dma/stm32-dma.c line 444

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+		stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id)));
+	dev_dbg(chan2dev(chan), "SPAR:  0x%08x\n",
+		stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id)));
+	dev_dbg(chan2dev(chan), "SM0AR: 0x%08x\n",
+		stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id)));
+	dev_dbg(chan2dev(chan), "SM1AR: 0x%08x\n",
+		stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id)));
+	dev_dbg(chan2dev(chan), "SFCR:  0x%08x\n",
+		stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id)));
+}

Even at dev_dbg() this looks like log spam. This debug info gets issued every time we set up a transfer!

Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
+{
+	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
+	struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+	int ret;
+
+	chan->config_init = false;

drivers/dma/stm32-dma.c line 954

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+	ret = clk_prepare_enable(dmadev->clk);
+	if (ret < 0) {
+		dev_err(chan2dev(chan), "clk_prepare_enable failed: %d\n", ret);
+		return ret;
+	}
+
+	ret = stm32_dma_disable_chan(chan);
+
+	return ret;
+}

The error path here looks like it will leak clock references.

Quoted 121 lines
drivers/dma/stm32-dma.c new file +121 -0
+static int stm32_dma_probe(struct platform_device *pdev)
+{
+	struct stm32_dma_chan *chan;
+	struct stm32_dma_device *dmadev;
+	struct dma_device *dd;
+	const struct of_device_id *match;
+	unsigned int i;
+	struct resource *res;
+	int ret;
+
+	match = of_match_device(stm32_dma_of_match, &pdev->dev);
+	if (!match) {
+		dev_err(&pdev->dev, "Error: No device match found\n");
+		return -ENODEV;
+	}
+
+	dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
+	if (!dmadev)
+		return -ENOMEM;
+
+	dd = &dmadev->ddev;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	dmadev->base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(dmadev->base))
+		return PTR_ERR(dmadev->base);
+
+	dmadev->clk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(dmadev->clk)) {
+		dev_err(&pdev->dev, "Error: Missing controller clock\n");
+		return PTR_ERR(dmadev->clk);
+	}
+
+	dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node,
+						"st,mem2mem");
+
+	dmadev->rst = devm_reset_control_get(&pdev->dev, NULL);
+	if (!IS_ERR(dmadev->rst)) {
+		reset_control_assert(dmadev->rst);
+		udelay(2);
+		reset_control_deassert(dmadev->rst);
+	}
+
+	dma_cap_set(DMA_SLAVE, dd->cap_mask);
+	dma_cap_set(DMA_PRIVATE, dd->cap_mask);
+	dma_cap_set(DMA_CYCLIC, dd->cap_mask);
+	dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources;
+	dd->device_free_chan_resources = stm32_dma_free_chan_resources;
+	dd->device_tx_status = stm32_dma_tx_status;
+	dd->device_issue_pending = stm32_dma_issue_pending;
+	dd->device_prep_slave_sg = stm32_dma_prep_slave_sg;
+	dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
+	dd->device_config = stm32_dma_slave_config;
+	dd->device_terminate_all = stm32_dma_terminate_all;
+	dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
+		BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
+		BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
+	dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
+		BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
+		BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
+	dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
+	dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
+	dd->dev = &pdev->dev;
+	INIT_LIST_HEAD(&dd->channels);
+
+	if (dmadev->mem2mem) {
+		dma_cap_set(DMA_MEMCPY, dd->cap_mask);
+		dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy;
+		dd->directions |= BIT(DMA_MEM_TO_MEM);
+	}
+
+	for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
+		chan = &dmadev->chan[i];
+		chan->id = i;
+		chan->vchan.desc_free = stm32_dma_desc_free;
+		vchan_init(&chan->vchan, dd);
+	}
+
+	ret = dma_async_device_register(dd);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
+		chan = &dmadev->chan[i];
+		res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
+		if (!res) {
+			ret = -EINVAL;
+			dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
+			goto err_unregister;
+		}
+		chan->irq = res->start;
+		ret = devm_request_irq(&pdev->dev, chan->irq,
+				       stm32_dma_chan_irq, 0,
+				       dev_name(chan2dev(chan)), chan);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"request_irq failed with err %d channel %d\n",
+				ret, i);
+			goto err_unregister;
+		}
+	}
+
+	ret = of_dma_controller_register(pdev->dev.of_node,
+					 stm32_dma_of_xlate, dmadev);
+	if (ret < 0) {
+		dev_err(&pdev->dev,
+			"STM32 DMA DMA OF registration failed %d\n", ret);
+		goto err_unregister;
+	}
+
+	platform_set_drvdata(pdev, dmadev);
+
+	dev_info(&pdev->dev, "STM32 DMA driver registered\n");
+
+	return 0;
+
+err_unregister:
+	dma_async_device_unregister(dd);
+
+	return ret;
+}

drivers/dma/stm32-dma.c line 1157

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+
+static int stm32_dma_remove(struct platform_device *pdev)
+{
+	struct stm32_dma_device *dmadev = platform_get_drvdata(pdev);
+
+	of_dma_controller_free(pdev->dev.of_node);
+
+	dma_async_device_unregister(&dmadev->ddev);
+
+	clk_disable_unprepare(dmadev->clk);

What is the purpose of disabling/unpreparing the clock here? stm32_dma_alloc_chan_resources() and stm32_dma_free_chan_resources() should pair up and the clock should already be stopped.

Daniel.

#5 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Hi Daniel,

2015-10-13 16:34 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 40 lines · M'boumba Cedric Madianga
Quoted 39 lines

This patch adds support for the STM32 DMA controller.

---
  drivers/dma/Kconfig     |   12 +
  drivers/dma/Makefile    |    1 +
  drivers/dma/stm32-dma.c | 1175
+++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 1188 insertions(+)

create mode 100644 drivers/dma/stm32-dma.c

drivers/dma/stm32-dma.c new file +19 -0
new file mode 100644
index 0000000..031bab7
 
 
+enum stm32_dma_width {
+       STM32_DMA_BYTE,
+       STM32_DMA_HALF_WORD,
+       STM32_DMA_WORD,
+};
+
+enum stm32_dma_burst_size {
+       STM32_DMA_BURST_SINGLE,
+       STM32_DMA_BURST_INCR4,
+       STM32_DMA_BURST_INCR8,
+       STM32_DMA_BURST_INCR16,
+};
+
+enum stm32_dma_channel_id {
+       STM32_DMA_CHANNEL0,
+       STM32_DMA_CHANNEL1,
+       STM32_DMA_CHANNEL2,
+       STM32_DMA_CHANNEL3,
+       STM32_DMA_CHANNEL4,
Quoted 8 lines

drivers/dma/stm32-dma.c line 144

Quoted 4 lines
drivers/dma/stm32-dma.c new file +4 -0
+       STM32_DMA_CHANNEL5,
+       STM32_DMA_CHANNEL6,
+       STM32_DMA_CHANNEL7,
+};

Why have (unused) enumerations to map numeric symbols to their own natural values? Using normal integers would be better.

Good point. I will remove these in the next version. Thanks.

Quoted 15 lines

drivers/dma/stm32-dma.c line 155

Quoted 12 lines
drivers/dma/stm32-dma.c new file +10 -0
+enum stm32_dma_request_id {
+       STM32_DMA_REQUEST0,
+       STM32_DMA_REQUEST1,
+       STM32_DMA_REQUEST2,
+       STM32_DMA_REQUEST3,
+       STM32_DMA_REQUEST4,
+       STM32_DMA_REQUEST5,
+       STM32_DMA_REQUEST6,
+       STM32_DMA_REQUEST7,
+};

Ditto.

As above, I will remove it in the next version. Thanks.

Quoted 15 lines
Quoted 15 lines
drivers/dma/stm32-dma.c new file +13 -0
+static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
+{
+       struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+
+       dev_dbg(chan2dev(chan), "SCR:   0x%08x\n",
+               stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)));
+       dev_dbg(chan2dev(chan), "NDTR:  0x%08x\n",
+               stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id)));
+       dev_dbg(chan2dev(chan), "SPAR:  0x%08x\n",
+               stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id)));
+       dev_dbg(chan2dev(chan), "SM0AR: 0x%08x\n",
+               stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id)));
+       dev_dbg(chan2dev(chan), "SM1AR: 0x%08x\n",
Quoted 8 lines

drivers/dma/stm32-dma.c line 444

Quoted 4 lines
drivers/dma/stm32-dma.c new file +4 -0
+               stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id)));
+       dev_dbg(chan2dev(chan), "SFCR:  0x%08x\n",
+               stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id)));
+}

Even at dev_dbg() this looks like log spam. This debug info gets issued every time we set up a transfer!

Yes, this info will be logged after each transfer.
But it will complete other debug info print when DMADEVICES_DEBUG is set.

Quoted 15 lines
Quoted 15 lines
drivers/dma/stm32-dma.c new file +10 -0
+static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
+{
+       struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
+       struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+       int ret;
+
+       chan->config_init = false;
+       ret = clk_prepare_enable(dmadev->clk);
+       if (ret < 0) {
+               dev_err(chan2dev(chan), "clk_prepare_enable failed: %d\n",

ret);

drivers/dma/stm32-dma.c new file +2 -0
+               return ret;
+       }
Quoted 8 lines

drivers/dma/stm32-dma.c line 465

Quoted 5 lines
drivers/dma/stm32-dma.c new file +5 -0
+
+       ret = stm32_dma_disable_chan(chan);
+
+       return ret;
+}

The error path here looks like it will leak clock references.

Sorry I didn't catch it. What does it mean ?

Quoted 133 lines
Quoted 133 lines
drivers/dma/stm32-dma.c new file +88 -0
+static int stm32_dma_probe(struct platform_device *pdev)
+{
+       struct stm32_dma_chan *chan;
+       struct stm32_dma_device *dmadev;
+       struct dma_device *dd;
+       const struct of_device_id *match;
+       unsigned int i;
+       struct resource *res;
+       int ret;
+
+       match = of_match_device(stm32_dma_of_match, &pdev->dev);
+       if (!match) {
+               dev_err(&pdev->dev, "Error: No device match found\n");
+               return -ENODEV;
+       }
+
+       dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
+       if (!dmadev)
+               return -ENOMEM;
+
+       dd = &dmadev->ddev;
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       dmadev->base = devm_ioremap_resource(&pdev->dev, res);
+       if (IS_ERR(dmadev->base))
+               return PTR_ERR(dmadev->base);
+
+       dmadev->clk = devm_clk_get(&pdev->dev, NULL);
+       if (IS_ERR(dmadev->clk)) {
+               dev_err(&pdev->dev, "Error: Missing controller clock\n");
+               return PTR_ERR(dmadev->clk);
+       }
+
+       dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node,
+                                               "st,mem2mem");
+
+       dmadev->rst = devm_reset_control_get(&pdev->dev, NULL);
+       if (!IS_ERR(dmadev->rst)) {
+               reset_control_assert(dmadev->rst);
+               udelay(2);
+               reset_control_deassert(dmadev->rst);
+       }
+
+       dma_cap_set(DMA_SLAVE, dd->cap_mask);
+       dma_cap_set(DMA_PRIVATE, dd->cap_mask);
+       dma_cap_set(DMA_CYCLIC, dd->cap_mask);
+       dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources;
+       dd->device_free_chan_resources = stm32_dma_free_chan_resources;
+       dd->device_tx_status = stm32_dma_tx_status;
+       dd->device_issue_pending = stm32_dma_issue_pending;
+       dd->device_prep_slave_sg = stm32_dma_prep_slave_sg;
+       dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
+       dd->device_config = stm32_dma_slave_config;
+       dd->device_terminate_all = stm32_dma_terminate_all;
+       dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
+               BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
+               BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
+       dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
+               BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
+               BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
+       dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
+       dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
+       dd->dev = &pdev->dev;
+       INIT_LIST_HEAD(&dd->channels);
+
+       if (dmadev->mem2mem) {
+               dma_cap_set(DMA_MEMCPY, dd->cap_mask);
+               dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy;
+               dd->directions |= BIT(DMA_MEM_TO_MEM);
+       }
+
+       for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
+               chan = &dmadev->chan[i];
+               chan->id = i;
+               chan->vchan.desc_free = stm32_dma_desc_free;
+               vchan_init(&chan->vchan, dd);
+       }
+
+       ret = dma_async_device_register(dd);
+       if (ret)
+               return ret;
+
+       for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
+               chan = &dmadev->chan[i];
+               res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
+               if (!res) {
+                       ret = -EINVAL;
+                       dev_err(&pdev->dev, "No irq resource for chan

%d\n", i);

drivers/dma/stm32-dma.c new file +9 -0
+                       goto err_unregister;
+               }
+               chan->irq = res->start;
+               ret = devm_request_irq(&pdev->dev, chan->irq,
+                                      stm32_dma_chan_irq, 0,
+                                      dev_name(chan2dev(chan)), chan);
+               if (ret) {
+                       dev_err(&pdev->dev,
+                               "request_irq failed with err %d channel

%d\n",

drivers/dma/stm32-dma.c new file +31 -0
+                               ret, i);
+                       goto err_unregister;
+               }
+       }
+
+       ret = of_dma_controller_register(pdev->dev.of_node,
+                                        stm32_dma_of_xlate, dmadev);
+       if (ret < 0) {
+               dev_err(&pdev->dev,
+                       "STM32 DMA DMA OF registration failed %d\n", ret);
+               goto err_unregister;
+       }
+
+       platform_set_drvdata(pdev, dmadev);
+
+       dev_info(&pdev->dev, "STM32 DMA driver registered\n");
+
+       return 0;
+
+err_unregister:
+       dma_async_device_unregister(dd);
+
+       return ret;
+}
+
+static int stm32_dma_remove(struct platform_device *pdev)
+{
+       struct stm32_dma_device *dmadev = platform_get_drvdata(pdev);
+
+       of_dma_controller_free(pdev->dev.of_node);
+
Quoted 8 lines

drivers/dma/stm32-dma.c line 1157

Quoted 3 lines
drivers/dma/stm32-dma.c new file +3 -0
+       dma_async_device_unregister(&dmadev->ddev);
+
+       clk_disable_unprepare(dmadev->clk);

What is the purpose of disabling/unpreparing the clock here? stm32_dma_alloc_chan_resources() and stm32_dma_free_chan_resources() should pair up and the clock should already be stopped.

I agree with you. I will remove it in the next version. Thanks

Quoted 3 lines

Daniel.

Thanks for your review.

BR,
Cedric

#6 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Quoted 22 lines · M'boumba Cedric Madianga
Quoted 13 lines
Quoted 13 lines
drivers/dma/stm32-dma.c new file +10 -0
+static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
+{
+       struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
+       struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+       int ret;
+
+       chan->config_init = false;
+       ret = clk_prepare_enable(dmadev->clk);
+       if (ret < 0) {
+               dev_err(chan2dev(chan), "clk_prepare_enable failed: %d\n",

ret);

(quoted diff) +2 -0
+               return ret;
+       }
Quoted 8 lines

drivers/dma/stm32-dma.c line 465

Quoted 5 lines
drivers/dma/stm32-dma.c new file +5 -0
+
+       ret = stm32_dma_disable_chan(chan);
+
+       return ret;
+}

The error path here looks like it will leak clock references.

Quoted 1 line

Sorry I didn't catch it. What does it mean ?

If stm32_dma_disable_chan() returns an error then we will not restore the original the clock counts causing them to "leak".

Daniel.

#7 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Hi Daniel,

2015-10-14 10:52 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 24 lines · M'boumba Cedric Madianga
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/dma/stm32-dma.c new file +10 -0
+static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
+{
+       struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
+       struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+       int ret;
+
+       chan->config_init = false;
+       ret = clk_prepare_enable(dmadev->clk);
+       if (ret < 0) {
+               dev_err(chan2dev(chan), "clk_prepare_enable failed:

%d\n",
ret);

Quoted 8 lines
Quoted 8 lines

drivers/dma/stm32-dma.c line 954

Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+               return ret;
+       }
+
+       ret = stm32_dma_disable_chan(chan);
+
+       return ret;
+}
Quoted 2 lines

If stm32_dma_disable_chan() returns an error then we will not restore the original the clock counts causing them to "leak".

Ok got it. Thanks for the clarification.
I will fix it in the next version.

BR,
Cedric

#8 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

In reply to M'boumba Cedric Madianga

Hi Daniel,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+
+static int stm32_dma_remove(struct platform_device *pdev)
+{
+       struct stm32_dma_device *dmadev = platform_get_drvdata(pdev);
+
+       of_dma_controller_free(pdev->dev.of_node);
+
Quoted 8 lines
Quoted 8 lines

drivers/dma/stm32-dma.c line 1157

Quoted 3 lines
drivers/dma/stm32-dma.c new file +3 -0
+       dma_async_device_unregister(&dmadev->ddev);
+
+       clk_disable_unprepare(dmadev->clk);

What is the purpose of disabling/unpreparing the clock here? stm32_dma_alloc_chan_resources() and stm32_dma_free_chan_resources() should pair up and the clock should already be stopped.

stm32_dma_remove() could be called during an on-going transfer during module unload.
So in that case, it seems that disabling/unpreparing the clock is needed.

BR,
Cedric

#9 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Quoted 18 lines · M'boumba Cedric Madianga

Hi Daniel,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+
+static int stm32_dma_remove(struct platform_device *pdev)
+{
+       struct stm32_dma_device *dmadev = platform_get_drvdata(pdev);
+
+       of_dma_controller_free(pdev->dev.of_node);
+
Quoted 8 lines
Quoted 8 lines

drivers/dma/stm32-dma.c line 1157

Quoted 3 lines
drivers/dma/stm32-dma.c new file +3 -0
+       dma_async_device_unregister(&dmadev->ddev);
+
+       clk_disable_unprepare(dmadev->clk);

What is the purpose of disabling/unpreparing the clock here? stm32_dma_alloc_chan_resources() and stm32_dma_free_chan_resources() should pair up and the clock should already be stopped.

Quoted 3 lines

stm32_dma_remove() could be called during an on-going transfer during module unload.
So in that case, it seems that disabling/unpreparing the clock is needed.

Really?

I think we need to be sure any on-going transfers are stopped.

There are multiple reasons for this, not least the risk of executing a callback that has been freed, but the one related to my point is that a single clk_disable_unprepare() will remain broken because if you don't know that the transfers have stopped then you don't know how many on-going transfers there are.

Daniel.

#10 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

2015-10-14 15:29 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 30 lines · M'boumba Cedric Madianga
Quoted 22 lines

Hi Daniel,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/dma/stm32-dma.c new file +9 -0
+
+static int stm32_dma_remove(struct platform_device *pdev)
+{
+       struct stm32_dma_device *dmadev = platform_get_drvdata(pdev);
+
+       of_dma_controller_free(pdev->dev.of_node);
+
+       dma_async_device_unregister(&dmadev->ddev);
+
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

+ clk_disable_unprepare(dmadev->clk);

What is the purpose of disabling/unpreparing the clock here? stm32_dma_alloc_chan_resources() and stm32_dma_free_chan_resources() should
pair up and the clock should already be stopped.

Quoted 3 lines

stm32_dma_remove() could be called during an on-going transfer during module unload.
So in that case, it seems that disabling/unpreparing the clock is needed.

Really?

Quoted 7 lines

I think we need to be sure any on-going transfers are stopped.

There are multiple reasons for this, not least the risk of executing a callback that has been freed, but the one related to my point is that a single clk_disable_unprepare() will remain broken because if you don't know that the transfers have stopped then you don't know how many on-going transfers there are.

In the next version, I am going to stop the DMA, free all interrupts and kill tasklet in stm32_dma_remove.
In that way, all on-going transfers will be lost as we don't have to wait the end of remaining transfer in order to execute this function as quickly as possible.
But even with this improvement, I think I have to disable the clock here.

#11 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Quoted 39 lines · M'boumba Cedric Madianga

2015-10-14 15:29 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 30 lines · M'boumba Cedric Madianga
Quoted 22 lines

Hi Daniel,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/dma/stm32-dma.c new file +9 -0
+
+static int stm32_dma_remove(struct platform_device *pdev)
+{
+       struct stm32_dma_device *dmadev = platform_get_drvdata(pdev);
+
+       of_dma_controller_free(pdev->dev.of_node);
+
+       dma_async_device_unregister(&dmadev->ddev);
+
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

+ clk_disable_unprepare(dmadev->clk);

What is the purpose of disabling/unpreparing the clock here? stm32_dma_alloc_chan_resources() and stm32_dma_free_chan_resources() should
pair up and the clock should already be stopped.

Quoted 3 lines

stm32_dma_remove() could be called during an on-going transfer during module unload.
So in that case, it seems that disabling/unpreparing the clock is needed.

Really?

Quoted 7 lines

I think we need to be sure any on-going transfers are stopped.

There are multiple reasons for this, not least the risk of executing a callback that has been freed, but the one related to my point is that a single clk_disable_unprepare() will remain broken because if you don't know that the transfers have stopped then you don't know how many on-going transfers there are.

Quoted 5 lines

In the next version, I am going to stop the DMA, free all interrupts and kill tasklet in stm32_dma_remove.
In that way, all on-going transfers will be lost as we don't have to wait the end of remaining transfer in order to execute this function as quickly as possible.

Hmnnn...

The dmaengine framework will WARN_ONCE() if an dmaengine is removed whilst it is active and also works hard to ensure dmaengine modules are not removed whilst there are active drivers using the framework.

How do we get into this function whilst there is still an active DMA channels?

Quoted 1 line

But even with this improvement, I think I have to disable the clock here.

As above, I think the dmaengine framework work to protect you from this sort problem. However even if I am wrong about that then unconditionally calling clk_disable_unprepare() can not be used to reliably manage the clocks. You don't know what the counts are!

Daniel.

#12 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

2015-10-14 16:24 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 9 lines

Hmnnn...

The dmaengine framework will WARN_ONCE() if an dmaengine is removed whilst it is active and also works hard to ensure dmaengine modules are not removed whilst there are active drivers using the framework.

How do we get into this function whilst there is still an active DMA channels?

For example, when a user try "rmmod stm32-dma" in uart console. It will enter in stm32_dma_remove while there is potentially still active DMA.

Quoted 9 lines
Quoted 3 lines

But even with this improvement, I think I have to disable the clock here.

As above, I think the dmaengine framework work to protect you from this sort problem. However even if I am wrong about that then unconditionally calling clk_disable_unprepare() can not be used to reliably manage the clocks. You don't know what the counts are!

Ok got it. Thanks for the clarification.

Cedric

#13 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Quoted 8 lines · M'boumba Cedric Madianga

2015-10-14 16:24 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 7 lines

Hmnnn...

The dmaengine framework will WARN_ONCE() if an dmaengine is removed whilst it is active and also works hard to ensure dmaengine modules are not removed whilst there are active drivers using the framework.

Quoted 5 lines
Quoted 2 lines

How do we get into this function whilst there is still an active DMA channels?

For example, when a user try "rmmod stm32-dma" in uart console. It will enter in stm32_dma_remove while there is potentially still active DMA.

Check dmaengine.c for yourself but I think in this case the dmaengine framework will hold references to the module and prevent the remove from taking place.

Daniel.

#14 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

2015-10-14 17:28 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 17 lines · M'boumba Cedric Madianga
Quoted 16 lines

2015-10-14 16:24 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 12 lines

Hmnnn...

The dmaengine framework will WARN_ONCE() if an dmaengine is removed whilst
it is active and also works hard to ensure dmaengine modules are not removed
whilst there are active drivers using the framework.

How do we get into this function whilst there is still an active DMA channels?

Quoted 8 lines
Quoted 3 lines

For example, when a user try "rmmod stm32-dma" in uart console. It will enter in stm32_dma_remove while there is potentially still active DMA.

Check dmaengine.c for yourself but I think in this case the dmaengine framework will hold references to the module and prevent the remove from taking place.

Yes I did it.
As far I understand, the dmaengine framework will print a warning message but doesn't stop removing operation if there are some active clients.

Cedric.

#15 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Quoted 23 lines · M'boumba Cedric Madianga

2015-10-14 17:28 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 22 lines · M'boumba Cedric Madianga
Quoted 19 lines

2015-10-14 16:24 GMT+02:00 Daniel Thompson <[email protected]>:

Quoted 4 lines

Hmnnn...

Quoted 8 lines

The dmaengine framework will WARN_ONCE() if an dmaengine is removed whilst
it is active and also works hard to ensure dmaengine modules are not removed
whilst there are active drivers using the framework.

How do we get into this function whilst there is still an active DMA channels?

For example, when a user try "rmmod stm32-dma" in uart console. It will enter in stm32_dma_remove while there is potentially still active DMA.

Quoted 8 lines
Quoted 3 lines

Check dmaengine.c for yourself but I think in this case the dmaengine framework will hold references to the module and prevent the remove from taking place.

Yes I did it.
As far I understand, the dmaengine framework will print a warning message but doesn't stop removing operation if there are some active clients.

we hold a ref, so removal wont work, see dma_chan_get()

Signature · 1 line
~Vinod
#16 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

In reply to M'boumba Cedric Madianga

drivers/dma/stm32-dma.c line 39

Quoted 9 lines · M'boumba Cedric Madianga
drivers/dma/stm32-dma.c new file +9 -0
+#define STM32_DMA_LISR			0x0000 /* DMA Low Int Status Reg */
+#define STM32_DMA_HISR			0x0004 /* DMA High Int Status Reg */
+#define STM32_DMA_LIFCR			0x0008 /* DMA Low Int Flag Clear Reg */
+#define STM32_DMA_HIFCR			0x000c /* DMA High Int Flag Clear Reg */
+#define STM32_DMA_TCI			BIT(5) /* Transfer Complete Interrupt */
+#define STM32_DMA_HTI			BIT(4) /* Half Transfer Interrupt */
+#define STM32_DMA_TEI			BIT(3) /* Transfer Error Interrupt */
+#define STM32_DMA_DMEI			BIT(2) /* Direct Mode Error Interrupt */
+#define STM32_DMA_FEI			BIT(0) /* FIFO Error Interrupt */

Why not use BIT() for everything here and make it consistent

Also where ever possible stick to 80 char limit like above you can

drivers/dma/stm32-dma.c line 43

Quoted 4 lines
drivers/dma/stm32-dma.c new file +4 -0
+
+/* DMA Stream x Configuration Register */
+#define STM32_DMA_SCR(x)		(0x0010 + 0x18 * (x)) /* x = 0..7 */
+#define STM32_DMA_SCR_REQ(n)		((n & 0x7) << 25)

this and below looks ugly and hard to maintain, are you sure spec doesn't have a formulae for these?

Quoted 1 line

+static inline uint32_t stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg)

this and few other could be made more readable

drivers/dma/stm32-dma.c line 247

Quoted 4 lines
drivers/dma/stm32-dma.c new file +4 -0
+static struct stm32_dma_desc *stm32_dma_alloc_desc(unsigned int num_sgs)
+{
+	return kzalloc(sizeof(struct stm32_dma_desc) +
+		       sizeof(struct stm32_dma_sg_req) * num_sgs, GFP_ATOMIC);

Not GFP_NOWAIT ?

Quoted 4 lines
drivers/dma/stm32-dma.c new file +4 -0
+static enum stm32_dma_width stm32_get_dma_width(struct stm32_dma_chan *chan,
+						enum dma_slave_buswidth width)
+{
+	switch (width) {

drivers/dma/stm32-dma.c line 263

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+	case DMA_SLAVE_BUSWIDTH_1_BYTE:
+		return STM32_DMA_BYTE;
+	case DMA_SLAVE_BUSWIDTH_2_BYTES:
+		return STM32_DMA_HALF_WORD;
+	case DMA_SLAVE_BUSWIDTH_4_BYTES:
+		return STM32_DMA_WORD;
+	default:
+		dev_warn(chan2dev(chan),
+			 "Dma bus width not supported, using 32bits\n");
+		return STM32_DMA_WORD;

pls return error here
Assuming wrong parameter can cause havoc of transfer, so is not advisable

Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+static enum stm32_dma_burst_size stm32_get_dma_burst(
+		struct stm32_dma_chan *chan, u32 maxburst)
+{
+	switch (maxburst) {
+	case 0:
+	case 1:
+		return STM32_DMA_BURST_SINGLE;

drivers/dma/stm32-dma.c line 283

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+	case 4:
+		return STM32_DMA_BURST_INCR4;
+	case 8:
+		return STM32_DMA_BURST_INCR8;
+	case 16:
+		return STM32_DMA_BURST_INCR16;
+	default:
+		dev_warn(chan2dev(chan),
+			 "Dma burst size not supported, using single\n");
+		return STM32_DMA_BURST_SINGLE;

here too

drivers/dma/stm32-dma.c line 295

Quoted 12 lines
drivers/dma/stm32-dma.c new file +12 -0
+	}
+}
+
+static int stm32_dma_slave_config(struct dma_chan *c,
+				  struct dma_slave_config *config)
+{
+	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
+
+	if (chan->busy) {
+		dev_err(chan2dev(chan), "Configuration not allowed\n");
+		return -EBUSY;
+	}

That is false condition. This configuration should be used for next descriptor prepare

Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+static int stm32_dma_disable_chan(struct stm32_dma_chan *chan)
+{
+	struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+	unsigned long timeout = jiffies + msecs_to_jiffies(5000);
+	u32 dma_scr;
+
+	dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));

drivers/dma/stm32-dma.c line 363

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+	if (dma_scr & STM32_DMA_SCR_EN) {
+		dma_scr &= ~STM32_DMA_SCR_EN;
+		stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
+
+		do {
+			dma_scr = stm32_dma_read(dmadev,
+						 STM32_DMA_SCR(chan->id));
+			dma_scr &= STM32_DMA_SCR_EN;
+			if (!dma_scr)
+				break;

empty line here would improve readability

Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+static irqreturn_t stm32_dma_chan_irq(int irq, void *devid)
+{
+	struct stm32_dma_chan *chan = devid;
+	struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+	u32 status, scr, sfcr;
+
+	spin_lock(&chan->vchan.lock);

drivers/dma/stm32-dma.c line 566

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+
+	status = stm32_dma_irq_status(chan);
+	scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
+	sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
+
+	if ((status & STM32_DMA_HTI) && (scr & STM32_DMA_SCR_HTIE)) {
+		stm32_dma_irq_clear(chan, STM32_DMA_HTI);
+		vchan_cyclic_callback(&chan->desc->vdesc);
+		spin_unlock(&chan->vchan.lock);
+		return IRQ_HANDLED;

line here please and below

Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+	} else if ((status & STM32_DMA_TCI) && (scr & STM32_DMA_SCR_TCIE)) {
+		stm32_dma_irq_clear(chan, STM32_DMA_TCI);
+		stm32_dma_handle_chan_done(chan);
+		spin_unlock(&chan->vchan.lock);
+		return IRQ_HANDLED;
+	} else if ((status & STM32_DMA_TEI) && (scr & STM32_DMA_SCR_TEIE)) {
+		dev_err(chan2dev(chan), "DMA error: received TEI event\n");

drivers/dma/stm32-dma.c line 583

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+		stm32_dma_irq_clear(chan, STM32_DMA_TEI);
+		chan->status = DMA_ERROR;
+		spin_unlock(&chan->vchan.lock);
+		return IRQ_HANDLED;
+	} else if ((status & STM32_DMA_FEI) && (sfcr & STM32_DMA_SFCR_FEIE)) {
+		dev_err(chan2dev(chan), "DMA error: received FEI event\n");
+		stm32_dma_irq_clear(chan, STM32_DMA_FEI);
+		chan->status = DMA_ERROR;
+		spin_unlock(&chan->vchan.lock);
+		return IRQ_HANDLED;

this is repeat of above apart from err print!!

drivers/dma/stm32-dma.c line 589

Quoted 6 lines
drivers/dma/stm32-dma.c new file +6 -0
+	} else if ((status & STM32_DMA_DMEI) && (scr & STM32_DMA_SCR_DMEIE)) {
+		dev_err(chan2dev(chan), "DMA error: received DMEI event\n");
+		stm32_dma_irq_clear(chan, STM32_DMA_DMEI);
+		chan->status = DMA_ERROR;
+		spin_unlock(&chan->vchan.lock);
+		return IRQ_HANDLED;

same here :(

Quoted 6 lines
drivers/dma/stm32-dma.c new file +6 -0
+static enum dma_status stm32_dma_tx_status(struct dma_chan *c,
+					   dma_cookie_t cookie,
+					   struct dma_tx_state *state)
+{
+	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
+	struct virt_dma_desc *vdesc;

drivers/dma/stm32-dma.c line 917

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+	enum dma_status status;
+	unsigned long flags;
+	unsigned int residue;
+
+	status = dma_cookie_status(c, cookie, state);
+	if (status == DMA_COMPLETE)
+		return status;
+
+	if (!state)
+		return chan->status;

why channel status and not status from dma_cookie_status()?

drivers/dma/stm32-dma.c line 1157

Quoted 9 lines
drivers/dma/stm32-dma.c new file +9 -0
+static int stm32_dma_remove(struct platform_device *pdev)
+{
+	struct stm32_dma_device *dmadev = platform_get_drvdata(pdev);
+
+	of_dma_controller_free(pdev->dev.of_node);
+
+	dma_async_device_unregister(&dmadev->ddev);
+
+	clk_disable_unprepare(dmadev->clk);

and your irq is enabled and you can still receive interrupts and schedule tasklets :(

Signature · 1 line
~Vinod
#17 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Hi Vinod,

2015-10-14 13:16 GMT+02:00 Vinod Koul <[email protected]>:

Quoted 13 lines · M'boumba Cedric Madianga

drivers/dma/stm32-dma.c line 39

Quoted 9 lines
drivers/dma/stm32-dma.c new file +9 -0
+#define STM32_DMA_LISR                       0x0000 /* DMA Low Int Status Reg */
+#define STM32_DMA_HISR                       0x0004 /* DMA High Int Status Reg */
+#define STM32_DMA_LIFCR                      0x0008 /* DMA Low Int Flag Clear Reg */
+#define STM32_DMA_HIFCR                      0x000c /* DMA High Int Flag Clear Reg */
+#define STM32_DMA_TCI                        BIT(5) /* Transfer Complete Interrupt */
+#define STM32_DMA_HTI                        BIT(4) /* Half Transfer Interrupt */
+#define STM32_DMA_TEI                        BIT(3) /* Transfer Error Interrupt */
+#define STM32_DMA_DMEI                       BIT(2) /* Direct Mode Error Interrupt */
+#define STM32_DMA_FEI                        BIT(0) /* FIFO Error Interrupt */

Why not use BIT() for everything here and make it consistent

Quoted 1 line

Also where ever possible stick to 80 char limit like above you can

In fact STM32_DMA_LISR, HISR, LIFCR and HIFCR are offset to access register. So BIT() could not be used for this purpose.

Quoted 8 lines

drivers/dma/stm32-dma.c line 43

Quoted 5 lines
drivers/dma/stm32-dma.c new file +4 -0
+
+/* DMA Stream x Configuration Register */
+#define STM32_DMA_SCR(x)             (0x0010 + 0x18 * (x)) /* x = 0..7 */
+#define STM32_DMA_SCR_REQ(n)         ((n & 0x7) << 25)

this and below looks ugly and hard to maintain, are you sure spec doesn't have a formulae for these?

We don't have any formulae in spec to handle this.
It is clearly described that we have to access registers in that way to address the good channel.
Please, see below an extract of the spec for more details:
DMA stream x configuration register (DMA_SxCR) (x = 0..7) This register is used to configure the concerned stream. Address offset: 0x10 + 0x18 ? stream number

Quoted 4 lines
Quoted 2 lines

+static inline uint32_t stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg)

this and few other could be made more readable

Ok, I could replace uint32_t by u32. Is it what you expect ? For others, I don't see how I could make it more readable. Could you please give me more details to help me ? Thanks in advance

Quoted 7 lines

drivers/dma/stm32-dma.c line 247

Quoted 5 lines
drivers/dma/stm32-dma.c new file +4 -0
+static struct stm32_dma_desc *stm32_dma_alloc_desc(unsigned int num_sgs)
+{
+     return kzalloc(sizeof(struct stm32_dma_desc) +
+                    sizeof(struct stm32_dma_sg_req) * num_sgs, GFP_ATOMIC);

Not GFP_NOWAIT ?

You're right. We could use GFP_NOWAIT instead. Thanks.

Quoted 10 lines
Quoted 10 lines
drivers/dma/stm32-dma.c new file +9 -0
+static enum stm32_dma_width stm32_get_dma_width(struct stm32_dma_chan *chan,
+                                             enum dma_slave_buswidth width)
+{
+     switch (width) {
+     case DMA_SLAVE_BUSWIDTH_1_BYTE:
+             return STM32_DMA_BYTE;
+     case DMA_SLAVE_BUSWIDTH_2_BYTES:
+             return STM32_DMA_HALF_WORD;
+     case DMA_SLAVE_BUSWIDTH_4_BYTES:
Quoted 8 lines

drivers/dma/stm32-dma.c line 263

Quoted 5 lines
drivers/dma/stm32-dma.c new file +5 -0
+             return STM32_DMA_WORD;
+     default:
+             dev_warn(chan2dev(chan),
+                      "Dma bus width not supported, using 32bits\n");
+             return STM32_DMA_WORD;

pls return error here
Assuming wrong parameter can cause havoc of transfer, so is not advisable

OK, I will fix it in the next version. Thanks for the explanation.

Quoted 12 lines
Quoted 12 lines
drivers/dma/stm32-dma.c new file +11 -0
+static enum stm32_dma_burst_size stm32_get_dma_burst(
+             struct stm32_dma_chan *chan, u32 maxburst)
+{
+     switch (maxburst) {
+     case 0:
+     case 1:
+             return STM32_DMA_BURST_SINGLE;
+     case 4:
+             return STM32_DMA_BURST_INCR4;
+     case 8:
+             return STM32_DMA_BURST_INCR8;
Quoted 8 lines

drivers/dma/stm32-dma.c line 283

Quoted 6 lines
drivers/dma/stm32-dma.c new file +6 -0
+     case 16:
+             return STM32_DMA_BURST_INCR16;
+     default:
+             dev_warn(chan2dev(chan),
+                      "Dma burst size not supported, using single\n");
+             return STM32_DMA_BURST_SINGLE;

here too

Ditto

Quoted 16 lines

drivers/dma/stm32-dma.c line 295

Quoted 13 lines
drivers/dma/stm32-dma.c new file +12 -0
+     }
+}
+
+static int stm32_dma_slave_config(struct dma_chan *c,
+                               struct dma_slave_config *config)
+{
+     struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
+
+     if (chan->busy) {
+             dev_err(chan2dev(chan), "Configuration not allowed\n");
+             return -EBUSY;
+     }

That is false condition. This configuration should be used for next descriptor prepare

OK. Will be fixed in the next patch version.

Quoted 12 lines
Quoted 12 lines
drivers/dma/stm32-dma.c new file +11 -0
+static int stm32_dma_disable_chan(struct stm32_dma_chan *chan)
+{
+     struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+     unsigned long timeout = jiffies + msecs_to_jiffies(5000);
+     u32 dma_scr;
+
+     dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
+     if (dma_scr & STM32_DMA_SCR_EN) {
+             dma_scr &= ~STM32_DMA_SCR_EN;
+             stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
+
Quoted 8 lines

drivers/dma/stm32-dma.c line 363

Quoted 6 lines
drivers/dma/stm32-dma.c new file +6 -0
+             do {
+                     dma_scr = stm32_dma_read(dmadev,
+                                              STM32_DMA_SCR(chan->id));
+                     dma_scr &= STM32_DMA_SCR_EN;
+                     if (!dma_scr)
+                             break;

empty line here would improve readability

OK

Quoted 12 lines
Quoted 12 lines
drivers/dma/stm32-dma.c new file +11 -0
+static irqreturn_t stm32_dma_chan_irq(int irq, void *devid)
+{
+     struct stm32_dma_chan *chan = devid;
+     struct stm32_dma_device *dmadev = stm32_dma_chan_get_dev(chan);
+     u32 status, scr, sfcr;
+
+     spin_lock(&chan->vchan.lock);
+
+     status = stm32_dma_irq_status(chan);
+     scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
+     sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
Quoted 8 lines

drivers/dma/stm32-dma.c line 566

Quoted 6 lines
drivers/dma/stm32-dma.c new file +6 -0
+
+     if ((status & STM32_DMA_HTI) && (scr & STM32_DMA_SCR_HTIE)) {
+             stm32_dma_irq_clear(chan, STM32_DMA_HTI);
+             vchan_cyclic_callback(&chan->desc->vdesc);
+             spin_unlock(&chan->vchan.lock);
+             return IRQ_HANDLED;

line here please and below

OK

Quoted 19 lines
Quoted 8 lines
drivers/dma/stm32-dma.c new file +7 -0
+     } else if ((status & STM32_DMA_TCI) && (scr & STM32_DMA_SCR_TCIE)) {
+             stm32_dma_irq_clear(chan, STM32_DMA_TCI);
+             stm32_dma_handle_chan_done(chan);
+             spin_unlock(&chan->vchan.lock);
+             return IRQ_HANDLED;
+     } else if ((status & STM32_DMA_TEI) && (scr & STM32_DMA_SCR_TEIE)) {
+             dev_err(chan2dev(chan), "DMA error: received TEI event\n");

drivers/dma/stm32-dma.c line 583

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+             stm32_dma_irq_clear(chan, STM32_DMA_TEI);
+             chan->status = DMA_ERROR;
+             spin_unlock(&chan->vchan.lock);
+             return IRQ_HANDLED;
+     } else if ((status & STM32_DMA_FEI) && (sfcr & STM32_DMA_SFCR_FEIE)) {
+             dev_err(chan2dev(chan), "DMA error: received FEI event\n");
+             stm32_dma_irq_clear(chan, STM32_DMA_FEI);
+             chan->status = DMA_ERROR;
+             spin_unlock(&chan->vchan.lock);
+             return IRQ_HANDLED;
Quoted 1 line

this is repeat of above apart from err print!!

Not only for print as we also need that to define which bit to clear in the IRQ status.
In that way we will be sure to handle each interrupt event.

Quoted 9 lines

drivers/dma/stm32-dma.c line 589

Quoted 7 lines
drivers/dma/stm32-dma.c new file +6 -0
+     } else if ((status & STM32_DMA_DMEI) && (scr & STM32_DMA_SCR_DMEIE)) {
+             dev_err(chan2dev(chan), "DMA error: received DMEI event\n");
+             stm32_dma_irq_clear(chan, STM32_DMA_DMEI);
+             chan->status = DMA_ERROR;
+             spin_unlock(&chan->vchan.lock);
+             return IRQ_HANDLED;

same here :(

Ditto

Quoted 10 lines
Quoted 10 lines
drivers/dma/stm32-dma.c new file +9 -0
+static enum dma_status stm32_dma_tx_status(struct dma_chan *c,
+                                        dma_cookie_t cookie,
+                                        struct dma_tx_state *state)
+{
+     struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
+     struct virt_dma_desc *vdesc;
+     enum dma_status status;
+     unsigned long flags;
+     unsigned int residue;
Quoted 8 lines

drivers/dma/stm32-dma.c line 917

Quoted 7 lines
drivers/dma/stm32-dma.c new file +7 -0
+
+     status = dma_cookie_status(c, cookie, state);
+     if (status == DMA_COMPLETE)
+             return status;
+
+     if (!state)
+             return chan->status;

why channel status and not status from dma_cookie_status()?

If status is different than DMA_COMPLETE it seems better to use channel status. Indeed, in that way, we have the possibility to notify that there is a potential error in the bus via DMA_ERROR.
But if I return status from dma_cookie_status(), I will always notify DMA_IN_PROGRESS.

Quoted 13 lines

drivers/dma/stm32-dma.c line 1157

Quoted 10 lines
drivers/dma/stm32-dma.c new file +9 -0
+static int stm32_dma_remove(struct platform_device *pdev)
+{
+     struct stm32_dma_device *dmadev = platform_get_drvdata(pdev);
+
+     of_dma_controller_free(pdev->dev.of_node);
+
+     dma_async_device_unregister(&dmadev->ddev);
+
+     clk_disable_unprepare(dmadev->clk);

and your irq is enabled and you can still receive interrupts and schedule tasklets :(

So, I have to free interrupts and also kill tasklet from interrupt here. I will also disable the channel in case of ongoing transfer in order to let the DMA controller in a good state.

Thanks for your review :)

BR,
Cedric

#18 lore raw

Re: [PATCH v2 2/4] dmaengine: Add STM32 DMA driver

Quoted 7 lines · M'boumba Cedric Madianga
Quoted 3 lines
Quoted 1 line

+static inline uint32_t stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg)

this and few other could be made more readable

Ok, I could replace uint32_t by u32. Is it what you expect ? For others, I don't see how I could make it more readable. Could you please give me more details to help me ? Thanks in advance

Yes that is one and also aplitting this up so that it fits within 80chars while not sacrficing readablity...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/dma/stm32-dma.c new file +6 -0
+     } else if ((status & STM32_DMA_FEI) && (sfcr & STM32_DMA_SFCR_FEIE)) {
+             dev_err(chan2dev(chan), "DMA error: received FEI event\n");
+             stm32_dma_irq_clear(chan, STM32_DMA_FEI);
+             chan->status = DMA_ERROR;
+             spin_unlock(&chan->vchan.lock);
+             return IRQ_HANDLED;
Quoted 5 lines
Quoted 1 line

this is repeat of above apart from err print!!

Not only for print as we also need that to define which bit to clear in the IRQ status.
In that way we will be sure to handle each interrupt event.

You can print error type based on status, or print the whole status but handle it same for all three cases

Quoted 12 lines
Quoted 11 lines

drivers/dma/stm32-dma.c line 917

Quoted 10 lines
drivers/dma/stm32-dma.c new file +10 -0
+     enum dma_status status;
+     unsigned long flags;
+     unsigned int residue;
+
+     status = dma_cookie_status(c, cookie, state);
+     if (status == DMA_COMPLETE)
+             return status;
+
+     if (!state)
+             return chan->status;

why channel status and not status from dma_cookie_status()?

Quoted 5 lines

If status is different than DMA_COMPLETE it seems better to use channel status. Indeed, in that way, we have the possibility to notify that there is a potential error in the bus via DMA_ERROR.
But if I return status from dma_cookie_status(), I will always notify DMA_IN_PROGRESS.

No, you are supposed to return the descriptor status and not channel status!

Signature · 1 line
~Vinod
#19 lore raw

PATCH v2 3/4 ARM: dts: Add STM32 DMA support for STM32F429 MCU

In reply to M'boumba Cedric Madianga

This patch adds STM32 DMA bindings for STM32F429.

---
 arch/arm/boot/dts/stm32f429.dtsi | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
arch/arm/boot/dts/stm32f429.dtsi +31 -0
index d78a481..037eb29 100644
@@ -174,6 +174,37 @@
 			reg = <0x40023800 0x400>;
 			clocks = <&clk_hse>;
 		};
+
+		dma1: dma-controller at 40026000 {
+			compatible = "st,stm32-dma";
+			reg = <0x40026000 0x400>;
+			interrupts = <11>,
+				     <12>,
+				     <13>,
+				     <14>,
+				     <15>,
+				     <16>,
+				     <17>,
+				     <47>;
+			clocks = <&rcc 0 21>;
+			#dma-cells = <4>;
+		};
+
+		dma2: dma-controller at 40026400 {
+			compatible = "st,stm32-dma";
+			reg = <0x40026400 0x400>;
+			interrupts = <56>,
+				     <57>,
+				     <58>,
+				     <59>,
+				     <60>,
+				     <68>,
+				     <69>,
+				     <70>;
+			clocks = <&rcc 0 22>;
+			#dma-cells = <4>;
+			st,mem2mem;
+		};
 	};
 };
 
Signature · 1 line
1.9.1
#20 lore raw

PATCH v2 4/4 ARM: configs: Add STM32 DMA support in STM32 defconfig

In reply to M'boumba Cedric Madianga

This patch adds STM32 DMA support in stm32_defconfig file

---
 arch/arm/configs/stm32_defconfig | 2 ++
 1 file changed, 2 insertions(+)
arch/arm/configs/stm32_defconfig +2 -0
index 4725fab..ec52505 100644
@@ -54,6 +54,8 @@ CONFIG_NEW_LEDS=y
 CONFIG_LEDS_CLASS=y
 CONFIG_LEDS_TRIGGERS=y
 CONFIG_LEDS_TRIGGER_HEARTBEAT=y
+CONFIG_DMADEVICES=y
+CONFIG_STM32_DMA=y
 # CONFIG_FILE_LOCKING is not set
 # CONFIG_DNOTIFY is not set
 # CONFIG_INOTIFY_USER is not set
Signature · 1 line
1.9.1