Skip to contentSkip to conversation

add uart DMA function

PATCH In review Long Cheng 5 participants 10 messages Feb 16, 2017 – Mar 7, 2017

Subsystems (4)
lore
Share & tools
lore

Long Cheng

author
#0 lore raw

cover add uart DMA function

In Mediatek SOCs, the uart can support DMA function.
Base on DMA engine formwork, we add the DMA code to support uart. And put the code under drivers/dma.

This series contains document bindings, Kconfig to control the function enable or not, device tree including interrupt and dma device node, the code of UART DMA function.

Long Cheng (4):
dt-bindings: dma: uart: add uart dma bindings
dmaengine: mtk_uart_dma: add Mediatek uart DMA support
serial: 8250-mtk: add uart DMA support
arm: dts: mt2701: add uart APDMA to device tree

 .../devicetree/bindings/dma/mtk_uart_dma.txt       |   32 +
 arch/arm/boot/dts/mt2701-evb.dts                   |   22 +
 arch/arm/boot/dts/mt2701.dtsi                      |   29 +
 drivers/dma/Kconfig                                |    9 +
 drivers/dma/Makefile                               |    1 +
 drivers/dma/mtk_uart_dma.c                         |  955 ++++++++++++++++++++
 drivers/tty/serial/8250/8250_mtk.c                 |  194 +++-
 7 files changed, 1241 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/dma/mtk_uart_dma.txt
 create mode 100644 drivers/dma/mtk_uart_dma.c
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/4 dt-bindings: dma: uart: add uart dma bindings

add uart dma bindings

---
 .../devicetree/bindings/dma/mtk_uart_dma.txt       |   32 ++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dma/mtk_uart_dma.txt
Documentation/devicetree/bindings/dma/mtk_uart_dma.txt new file +32 -0
new file mode 100644
index 0000000..b8aa7f4
@@ -0,0 +1,32 @@
+* Mediatek UART APDMA Controller
+
+Required properties:
+- compatible should contain:
+  * "mediatek,mt2701-uart-dma" for MT2701 compatible APDMA
+  * "mediatek,mt6577-uart-dma" for MT6577 and all of the above
+
+- reg: The base address of the APDMA register bank.
+
+- interrupts: A single interrupt specifier.
+
+- clocks : Must contain an entry for each entry in clock-names.
+  See ../clocks/clock-bindings.txt for details.
+- clock-names: The APDMA clock for register accesses
+
+Examples:
+
+	apdma: dma-controller at 11000380 {
+		compatible = "mediatek,mt2701-uart-dma";
+		reg = <0 0x11000380 0 0x400>;
+		interrupts = <GIC_SPI 63 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 64 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 65 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 66 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 67 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 68 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 69 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 70 IRQ_TYPE_LEVEL_LOW>;
+		clocks = <&pericfg CLK_PERI_AP_DMA>;
+		clock-names = "apdma";
+		#dma-cells = <1>;
+	};
Signature · 1 line
1.7.9.5
author
#3 lore raw

PATCH 2/4 dmaengine: mtk_uart_dma: add Mediatek uart DMA support

In reply to Long Cheng

In DMA engine framework, add 8250 mtk dma to support it.

---
 drivers/dma/Kconfig        |    9 +
 drivers/dma/Makefile       |    1 +
 drivers/dma/mtk_uart_dma.c |  955 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 965 insertions(+)
 create mode 100644 drivers/dma/mtk_uart_dma.c
drivers/dma/Kconfig +9 -0
index 263495d..d76dc96 100644
@@ -128,6 +128,15 @@ config DMA_JZ4780
 	  If you have a board based on such a SoC and wish to use DMA for
 	  devices which can use the DMA controller, say Y or M here.
 
+config DMA_MTK_UART
+	tristate "MediaTek SoCs APDMA support for UART"
+	depends on OF
+	select DMA_ENGINE
+	select DMA_VIRTUAL_CHANNELS
+	help
+	  Support for the UART DMA engine found on MediaTek MTK SoCs.
+	  This DMA engine just only if used when 8250 mtk uart is enabled.
+
 config DMA_OMAP
 	tristate "OMAP DMA support"
 	depends on ARCH_OMAP || COMPILE_TEST
drivers/dma/Makefile +1 -0
index a4fa336..5f0404a 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_COH901318) += coh901318.o coh901318_lli.o
 obj-$(CONFIG_DMA_BCM2835) += bcm2835-dma.o
 obj-$(CONFIG_DMA_JZ4740) += dma-jz4740.o
 obj-$(CONFIG_DMA_JZ4780) += dma-jz4780.o
+obj-$(CONFIG_DMA_MTK_UART) += mtk_uart_dma.o
 obj-$(CONFIG_DMA_OMAP) += omap-dma.o
 obj-$(CONFIG_DMA_SA11X0) += sa11x0-dma.o
 obj-$(CONFIG_DMA_SUN4I) += sun4i-dma.o
drivers/dma/mtk_uart_dma.c new file +955 -0

955 lines not on this page · raw message

Signature · 1 line
1.7.9.5
author
#6 lore raw

PATCH 3/4 serial: 8250-mtk: add uart DMA support

In reply to Long Cheng

Modify uart register to support DMA function.

---
 drivers/tty/serial/8250/8250_mtk.c |  194 +++++++++++++++++++++++++++++++++++-
 1 file changed, 193 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_mtk.c +193 -1
index ce0cc47..7d7a864 100644
@@ -23,6 +23,10 @@
 #include <linux/pm_runtime.h>
 #include <linux/serial_8250.h>
 #include <linux/serial_reg.h>
+#include <linux/console.h>
+#include <linux/dma-mapping.h>
+#include <linux/tty.h>
+#include <linux/tty_flip.h>
 
 #include "8250.h"
 
@@ -30,13 +34,165 @@
 #define UART_MTK_SAMPLE_COUNT	0x0a	/* Sample count register */
 #define UART_MTK_SAMPLE_POINT	0x0b	/* Sample point register */
 #define MTK_UART_RATE_FIX	0x0d	/* UART Rate Fix Register */
-
+#define UART_MTK_DMA_EN		0x13
+#define TX_TRIGGER		1
+#define RX_TRIGGER		8192
+
+#ifdef CONFIG_SERIAL_8250_DMA
+enum dma_rx_status {
+	DMA_RX_START = 0,
+	DMA_RX_RUNNING = 1,
+	DMA_RX_SHUTDOWN = 2,
+};
+#endif
 struct mtk8250_data {
 	int			line;
+	int			rxpos;
 	struct clk		*uart_clk;
 	struct clk		*bus_clk;
+	struct uart_8250_dma	*dma;
+#ifdef CONFIG_SERIAL_8250_DMA
+	enum dma_rx_status	rxstatus;
+#endif
 };
 
+#ifdef CONFIG_SERIAL_8250_DMA
+static int mtk8250_rx_dma(struct uart_8250_port *up);
+
+static void __mtkdma_rx_complete(void *param)
+{
+	struct uart_8250_port *up = param;
+	struct uart_8250_dma *dma = up->dma;
+	struct mtk8250_data *data = up->port.private_data;
+	struct tty_port *tty_port = &up->port.state->port;
+	struct dma_tx_state state;
+	unsigned char *ptr;
+	int copied;
+
+	dma_sync_single_for_cpu(dma->rxchan->device->dev, dma->rx_addr,
+				dma->rx_size, DMA_FROM_DEVICE);
+
+	dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
+	dmaengine_terminate_all(dma->rxchan);
+
+	if (data->rxstatus == DMA_RX_SHUTDOWN)
+		return;
+
+	if ((data->rxpos + state.residue) <= dma->rx_size) {
+		ptr = (unsigned char *)(data->rxpos + dma->rx_buf);
+		copied = tty_insert_flip_string(tty_port, ptr, state.residue);
+	} else {
+		ptr = (unsigned char *)(data->rxpos + dma->rx_buf);
+		copied = tty_insert_flip_string(tty_port, ptr,
+				(dma->rx_size - data->rxpos));
+		ptr = (unsigned char *)(dma->rx_buf);
+		copied += tty_insert_flip_string(tty_port, ptr,
+				(data->rxpos + state.residue - dma->rx_size));
+	}
+	up->port.icount.rx += copied;
+
+	tty_flip_buffer_push(tty_port);
+
+	mtk8250_rx_dma(up);
+}
+
+static int mtk8250_rx_dma(struct uart_8250_port *up)
+{
+	struct uart_8250_dma *dma = up->dma;
+	struct mtk8250_data *data = up->port.private_data;
+	struct dma_async_tx_descriptor	*desc;
+	struct dma_tx_state	 state;
+
+	desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
+					   dma->rx_size, DMA_DEV_TO_MEM,
+					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
+	if (!desc)
+		return -EBUSY;
+
+	desc->callback = __mtkdma_rx_complete;
+	desc->callback_param = up;
+
+	dma->rx_cookie = dmaengine_submit(desc);
+
+	dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
+	data->rxpos = state.residue;
+
+	dma_sync_single_for_device(dma->rxchan->device->dev, dma->rx_addr,
+				   dma->rx_size, DMA_FROM_DEVICE);
+
+	dma_async_issue_pending(dma->rxchan);
+
+	return 0;
+}
+
+static void mtk_dma_enable(struct uart_8250_port *up)
+{
+	struct uart_port *port = &up->port;
+	struct uart_8250_dma *dma = up->dma;
+	struct mtk8250_data *data = up->port.private_data;
+	int tmp = 0;
+
+	if (data->rxstatus != DMA_RX_START)
+		return;
+
+	dma->rxconf.direction		= DMA_DEV_TO_MEM;
+	dma->rxconf.src_addr_width	= dma->rx_size/1024;
+	dma->rxconf.src_addr		= dma->rx_addr;
+
+	dma->txconf.direction		= DMA_MEM_TO_DEV;
+	dma->txconf.dst_addr_width	= UART_XMIT_SIZE/1024;
+	dma->txconf.dst_addr		= dma->tx_addr;
+
+	serial_port_out(port, UART_FCR, ((1 << 0)|(1 << 1)|(1 << 2)));
+	serial_port_out(port, UART_MTK_DMA_EN,
+			(serial_port_in(port, UART_MTK_DMA_EN) |
+			((1 << 0) | (1 << 2))));
+	serial_port_out(port, UART_MTK_DMA_EN,
+			(serial_port_in(port, UART_MTK_DMA_EN) |
+			(1 << 1)));
+
+	tmp = serial_port_in(port, UART_LCR);
+	serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
+	serial_port_out(port, UART_EFR, UART_EFR_ECB);
+	serial_port_out(port, UART_LCR, tmp);
+
+	dmaengine_slave_config(dma->rxchan, &dma->rxconf);
+	dmaengine_slave_config(dma->txchan, &dma->txconf);
+
+	data->rxstatus = DMA_RX_RUNNING;
+	data->rxpos = 0;
+	mtk8250_rx_dma(up);
+}
+#endif
+
+static int mtk8250_startup(struct uart_port *port)
+{
+#ifdef CONFIG_SERIAL_8250_DMA
+	struct uart_8250_port *up =
+		container_of(port, struct uart_8250_port, port);
+	struct mtk8250_data *data = port->private_data;
+
+	if (up->dma != NULL)
+		data->rxstatus = DMA_RX_START;
+#endif
+
+	return serial8250_do_startup(port);
+}
+
+static void mtk8250_shutdown(struct uart_port *port)
+{
+#ifdef CONFIG_SERIAL_8250_DMA
+	struct uart_8250_port *up =
+		container_of(port, struct uart_8250_port, port);
+	struct mtk8250_data *data = port->private_data;
+
+	if (up->dma != NULL)
+		data->rxstatus = DMA_RX_SHUTDOWN;
+#endif
+
+	return serial8250_do_shutdown(port);
+}
+
 static void
 mtk8250_set_termios(struct uart_port *port, struct ktermios *termios,
 			struct ktermios *old)
@@ -44,6 +200,10 @@ struct mtk8250_data {
 	struct uart_8250_port *up = up_to_u8250p(port);
 	unsigned long flags;
 	unsigned int baud, quot;
+#ifdef CONFIG_SERIAL_8250_DMA
+	if (up->dma != NULL)
+		mtk_dma_enable(up);
+#endif
 
 	serial8250_do_set_termios(port, termios, old);
 
@@ -152,9 +312,20 @@ static int __maybe_unused mtk8250_runtime_resume(struct device *dev)
 		pm_runtime_put_sync_suspend(port->dev);
 }
 
+#ifdef CONFIG_SERIAL_8250_DMA
+static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
+{
+	return false;
+}
+#endif
+
 static int mtk8250_probe_of(struct platform_device *pdev, struct uart_port *p,
 			   struct mtk8250_data *data)
 {
+#ifdef CONFIG_SERIAL_8250_DMA
+	int dmacnt;
+#endif
+
 	data->uart_clk = devm_clk_get(&pdev->dev, "baud");
 	if (IS_ERR(data->uart_clk)) {
 		/*
@@ -174,6 +345,21 @@ static int mtk8250_probe_of(struct platform_device *pdev, struct uart_port *p,
 	if (IS_ERR(data->bus_clk))
 		return PTR_ERR(data->bus_clk);
 
+	data->dma = NULL;
+#ifdef CONFIG_SERIAL_8250_DMA
+	dmacnt = of_property_count_strings(pdev->dev.of_node, "dma-names");
+	if (dmacnt == 2) {
+		data->dma = devm_kzalloc(&pdev->dev,
+						sizeof(*(data->dma)),
+						GFP_KERNEL);
+		data->dma->fn = the_no_dma_filter_fn;
+		data->dma->rx_size = RX_TRIGGER;
+		data->dma->rxconf.src_maxburst = RX_TRIGGER;
+		data->dma->txconf.dst_maxburst = TX_TRIGGER;
+		data->dma->rx_dma = mtk8250_rx_dma;
+	}
+#endif
+
 	return 0;
 }
 
@@ -216,8 +402,14 @@ static int mtk8250_probe(struct platform_device *pdev)
 	uart.port.iotype = UPIO_MEM32;
 	uart.port.regshift = 2;
 	uart.port.private_data = data;
+	uart.port.shutdown = mtk8250_shutdown;
+	uart.port.startup = mtk8250_startup;
 	uart.port.set_termios = mtk8250_set_termios;
 	uart.port.uartclk = clk_get_rate(data->uart_clk);
+#ifdef CONFIG_SERIAL_8250_DMA
+	if (data->dma)
+		uart.dma = data->dma;
+#endif
 
 	/* Disable Rate Fix function */
 	writel(0x0, uart.port.membase +
Signature · 1 line
1.7.9.5
author
#8 lore raw

PATCH 4/4 arm: dts: mt2701: add uart APDMA to device tree

In reply to Long Cheng

1. add uart APDMA controller device node
2. add uart 0/1/2/3 DMA function
3. uart0 is console, So disable DMA
4. enable uart2 port to test DMA function.

---
 arch/arm/boot/dts/mt2701-evb.dts |   22 ++++++++++++++++++++++
 arch/arm/boot/dts/mt2701.dtsi    |   29 +++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
arch/arm/boot/dts/mt2701-evb.dts +22 -0
index 082ca88..2f92bf9 100644
@@ -24,6 +24,28 @@
 	};
 };
 
+&pio {
+	uart2_default_cfg: uart2default {
+		pins_cmd_dat {
+			pinmux = <MT2701_PIN_14_URXD2__FUNC_URXD2>,
+				 <MT2701_PIN_15_UTXD2__FUNC_UTXD2>,
+				 <MT2701_PIN_242_URTS2__FUNC_URTS2>,
+				 <MT2701_PIN_243_UCTS2__FUNC_UCTS2>;
+			bias-pull-up;
+		};
+	};
+};
+
 &uart0 {
+	dmas = "";
+	dma-names = "";
+
+	status = "okay";
+};
+
+&uart2 {
+	pinctrl-names = "default";
+	pinctrl-0 = <&uart2_default_cfg>;
+
 	status = "okay";
 };
arch/arm/boot/dts/mt2701.dtsi +29 -0
index 7eab6f4..62b3ec9 100644
@@ -175,6 +175,23 @@
 		      <0 0x10216000 0 0x2000>;
 	};
 
+	apdma: dma-controller at 11000380 {
+		compatible = "mediatek,mt2701-uart-dma",
+			     "mediatek,mt6577-uart-dma";
+		reg = <0 0x11000380 0 0x400>;
+		interrupts = <GIC_SPI 63 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 64 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 65 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 66 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 67 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 68 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 69 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 70 IRQ_TYPE_LEVEL_LOW>;
+		clocks = <&pericfg CLK_PERI_AP_DMA>;
+		clock-names = "apdma";
+		#dma-cells = <1>;
+	};
+
 	uart0: serial at 11002000 {
 		compatible = "mediatek,mt2701-uart",
 			     "mediatek,mt6577-uart";
@@ -182,6 +199,9 @@
 		interrupts = <GIC_SPI 51 IRQ_TYPE_LEVEL_LOW>;
 		clocks = <&pericfg CLK_PERI_UART0_SEL>, <&pericfg CLK_PERI_UART0>;
 		clock-names = "baud", "bus";
+		dmas = <&apdma 0
+			&apdma 1>;
+		dma-names = "tx", "rx";
 		status = "disabled";
 	};
 
@@ -192,6 +212,9 @@
 		interrupts = <GIC_SPI 52 IRQ_TYPE_LEVEL_LOW>;
 		clocks = <&pericfg CLK_PERI_UART1_SEL>, <&pericfg CLK_PERI_UART1>;
 		clock-names = "baud", "bus";
+		dmas = <&apdma 2
+			&apdma 3>;
+		dma-names = "tx", "rx";
 		status = "disabled";
 	};
 
@@ -202,6 +225,9 @@
 		interrupts = <GIC_SPI 53 IRQ_TYPE_LEVEL_LOW>;
 		clocks = <&pericfg CLK_PERI_UART2_SEL>, <&pericfg CLK_PERI_UART2>;
 		clock-names = "baud", "bus";
+		dmas = <&apdma 4
+			&apdma 5>;
+		dma-names = "tx", "rx";
 		status = "disabled";
 	};
 
@@ -212,6 +238,9 @@
 		interrupts = <GIC_SPI 54 IRQ_TYPE_LEVEL_LOW>;
 		clocks = <&pericfg CLK_PERI_UART3_SEL>, <&pericfg CLK_PERI_UART3>;
 		clock-names = "baud", "bus";
+		dmas = <&apdma 6
+			&apdma 7>;
+		dma-names = "tx", "rx";
 		status = "disabled";
 	};
 };
Signature · 1 line
1.7.9.5
#4 lore raw

Re: [PATCH 2/4] dmaengine: mtk_uart_dma: add Mediatek uart DMA support

In reply to Long Cheng
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi Long,

[auto build test WARNING on robh/for-next]
[also build test WARNING on v4.10-rc8 next-20170216]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Long-Cheng/add-uart-DMA-function/20170216-200846
base:   https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
config: ia64-allmodconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 6.2.0

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=ia64

All warnings (new ones prefixed by >>):

drivers/dma/mtk_uart_dma.c: In function 'mtk_dma_tx_write':

>> drivers/dma/mtk_uart_dma.c:230:37: warning: comparison of constant '1' with boolean expression is always false [-Wbool-compare]
     if (atomic_inc_and_test(&c->entry) > 1) {
                                        ^

vim +/1 +230 drivers/dma/mtk_uart_dma.c

214 if (atomic_dec_and_test(&c->loopcnt))
215 complete(&c->done);
216 }
217 }
218
219 static int mtk_dma_tx_write(struct dma_chan *chan)
220 {
221 struct mtk_chan *c = to_mtk_dma_chan(chan);
222 struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
223 struct timespec a, b;
224 int txcount = c->remain_size;
225 unsigned int tx_size = c->cfg.dst_addr_width*1024;
226 unsigned int len, left;
227 unsigned int wpt;
228 ktime_t begin, end;
229

 > 230		if (atomic_inc_and_test(&c->entry) > 1) {

231 if (vchan_issue_pending(&c->vc) && !c->desc) {
232 spin_lock(&mtkd->lock);
233 list_add_tail(&c->node, &mtkd->pending);
234 spin_unlock(&mtkd->lock);
235 tasklet_schedule(&mtkd->task);
236 }
237 } else {
238 while (mtk_dma_chan_read(c, VFF_LEFT_SIZE) >= c->trig) {

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: .config.gz
Type: application/gzip
Size: 45853 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20170216/e910819e/attachment-0001.gz>

#7 lore raw

Re: [PATCH 3/4] serial: 8250-mtk: add uart DMA support

In reply to Long Cheng
Quoted 7 lines · CET Long Cheng

Modify uart register to support DMA function.

---
 drivers/tty/serial/8250/8250_mtk.c |  194 +++++++++++++++++++++++++++++++++++-
 1 file changed, 193 insertions(+), 1 deletion(-)

This code looks a lot like the one in 8250_omap.c and 8250_dma.c. Could it be made more generic so we can share more of the implementation?

Arnd

maintainer
#2 lore raw

Re: [PATCH 1/4] dt-bindings: dma: uart: add uart dma bindings

In reply to Long Cheng
Quoted 1 line · Long Cheng

add uart dma bindings

This and the subject need to be more specific in that this is for Mediatek.

Quoted 4 lines
---
 .../devicetree/bindings/dma/mtk_uart_dma.txt       |   32 ++++++++++++++++++++

mediatek,uart-dma.txt for filename.

Documentation/devicetree/bindings/dma/mtk_uart_dma.txt line 1

Quoted 10 lines
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dma/mtk_uart_dma.txt
Documentation/devicetree/bindings/dma/mtk_uart_dma.txt new file +1 -0
new file mode 100644
index 0000000..b8aa7f4
@@ -0,0 +1,32 @@
+* Mediatek UART APDMA Controller

Is the block really for UARTs only or just happens to be connected to UARTs. For the latter, you should drop UART here and in the compatible.

Documentation/devicetree/bindings/dma/mtk_uart_dma.txt line 14

Quoted 13 lines
Documentation/devicetree/bindings/dma/mtk_uart_dma.txt new file +13 -0
+
+Required properties:
+- compatible should contain:
+  * "mediatek,mt2701-uart-dma" for MT2701 compatible APDMA
+  * "mediatek,mt6577-uart-dma" for MT6577 and all of the above
+
+- reg: The base address of the APDMA register bank.
+
+- interrupts: A single interrupt specifier.
+
+- clocks : Must contain an entry for each entry in clock-names.
+  See ../clocks/clock-bindings.txt for details.
+- clock-names: The APDMA clock for register accesses

Needs to list "apdma" here. Is apdma really the right name here? It should be the name of the input to this block.

Quoted 21 lines
Documentation/devicetree/bindings/dma/mtk_uart_dma.txt new file +18 -0
+
+Examples:
+
+	apdma: dma-controller at 11000380 {
+		compatible = "mediatek,mt2701-uart-dma";
+		reg = <0 0x11000380 0 0x400>;
+		interrupts = <GIC_SPI 63 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 64 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 65 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 66 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 67 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 68 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 69 IRQ_TYPE_LEVEL_LOW>,
+			     <GIC_SPI 70 IRQ_TYPE_LEVEL_LOW>;
+		clocks = <&pericfg CLK_PERI_AP_DMA>;
+		clock-names = "apdma";
+		#dma-cells = <1>;
+	};
Signature · 1 line
1.7.9.5
#5 lore raw

Re: [PATCH 2/4] dmaengine: mtk_uart_dma: add Mediatek uart DMA support

In reply to Long Cheng
Quoted 1 line · Long Cheng

In DMA engine framework, add 8250 mtk dma to support it.

Can you please describe the controller here

Quoted 4 lines

+/*
+ * MTK DMAengine support
+ *
+ * Copyright (c) 2016 MediaTek Inc.

we are in 2017 now

drivers/dma/mtk_uart_dma.c line 41

Quoted 6 lines
drivers/dma/mtk_uart_dma.c new file +6 -0
+#define VFF_INT_FLAG_CLR_B	0
+/*VFF_INT_EN*/
+#define VFF_RX_INT_EN0_B	BIT(0)
+#define VFF_RX_INT_EN1_B	BIT(1)
+#define VFF_TX_INT_EN_B		BIT(0)
+#define VFF_INT_EN_CLR_B	0

empty line after each logical bloock helps in readablity

drivers/dma/mtk_uart_dma.c line 51

Quoted 10 lines
drivers/dma/mtk_uart_dma.c new file +10 -0
+/*VFF_RST*/
+#define VFF_WARM_RST_B		BIT(0)
+/*VFF_EN*/
+#define VFF_EN_B		BIT(0)
+/*VFF_STOP*/
+#define VFF_STOP_B		BIT(0)
+#define VFF_STOP_CLR_B		0
+/*VFF_FLUSH*/
+#define VFF_FLUSH_B		BIT(0)
+#define VFF_FLUSH_CLR_B		0

please align all these

drivers/dma/mtk_uart_dma.c line 54

Quoted 3 lines
drivers/dma/mtk_uart_dma.c new file +3 -0
+
+#define VFF_TX_THRE(n)		((n)*7/8)
+#define VFF_RX_THRE(n)		((n)*3/4)

can you describe this

Quoted 1 line

+static bool mtk_dma_filter_fn(struct dma_chan *chan, void *param);

is there a reason for this, we can move the filer fn here!

drivers/dma/mtk_uart_dma.c line 230

Quoted 12 lines
drivers/dma/mtk_uart_dma.c new file +12 -0
+static int mtk_dma_tx_write(struct dma_chan *chan)
+{
+	struct mtk_chan *c = to_mtk_dma_chan(chan);
+	struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
+	struct timespec a, b;
+	int txcount = c->remain_size;
+	unsigned int tx_size = c->cfg.dst_addr_width*1024;
+	unsigned int len, left;
+	unsigned int wpt;
+	ktime_t begin, end;
+
+	if (atomic_inc_and_test(&c->entry) > 1) {

why are we using atomic variables

drivers/dma/mtk_uart_dma.c line 241

Quoted 11 lines
drivers/dma/mtk_uart_dma.c new file +11 -0
+		if (vchan_issue_pending(&c->vc) && !c->desc) {
+			spin_lock(&mtkd->lock);
+			list_add_tail(&c->node, &mtkd->pending);
+			spin_unlock(&mtkd->lock);
+			tasklet_schedule(&mtkd->task);
+		}
+	} else {
+		while (mtk_dma_chan_read(c, VFF_LEFT_SIZE) >= c->trig) {
+			left = tx_size - mtk_dma_chan_read(c, VFF_VALID_SIZE);
+			left = (left > 16) ? (left - 16) : (0);
+			len = left < c->remain_size ? left : c->remain_size;

?? can you explain this

drivers/dma/mtk_uart_dma.c line 244

Quoted 3 lines
drivers/dma/mtk_uart_dma.c new file +3 -0
+
+			begin = ktime_get();
+			a = ktime_to_timespec(begin);

more alarm bells now!

drivers/dma/mtk_uart_dma.c line 250

Quoted 6 lines
drivers/dma/mtk_uart_dma.c new file +6 -0
+			while (len--) {
+				/*
+				 * DMA limitation.
+				 * Workaround: Polling flush bit to zero,
+				 * set 1s timeout
+				 */

1sec is ***VERY*** large, does the device recommend that?

drivers/dma/mtk_uart_dma.c line 261

Quoted 11 lines
drivers/dma/mtk_uart_dma.c new file +11 -0
+				while (mtk_dma_chan_read(c, VFF_FLUSH)) {
+					end = ktime_get();
+					b = ktime_to_timespec(end);
+					if ((b.tv_sec - a.tv_sec) > 1 ||
+						((b.tv_sec - a.tv_sec) == 1 &&
+						b.tv_nsec > a.tv_nsec)) {
+						dev_info(chan->device->dev,
+							"[UART] Polling flush timeout\n");
+						return 0;
+					}
+				}

No this is not how you implement a time out. Hint use jiffes and count them.

drivers/dma/mtk_uart_dma.c line 265

Quoted 4 lines
drivers/dma/mtk_uart_dma.c new file +4 -0
+
+				wpt = mtk_dma_chan_read(c, VFF_WPT);
+
+				if ((wpt & 0x0000ffffl) ==

magic number?

drivers/dma/mtk_uart_dma.c line 297

Quoted 11 lines
drivers/dma/mtk_uart_dma.c new file +11 -0
+static void mtk_dma_start_tx(struct mtk_chan *c)
+{
+	if (mtk_dma_chan_read(c, VFF_LEFT_SIZE) == 0) {
+		dev_info(c->vc.chan.device->dev, "%s maybe need fix? %d @L %d\n",
+				__func__, mtk_dma_chan_read(c, VFF_LEFT_SIZE),
+				__LINE__);
+		mtk_dma_chan_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
+	} else {
+		reinit_completion(&c->done);
+		atomic_inc(&c->loopcnt);
+		atomic_inc(&c->loopcnt);

why would you increment twice

Quoted 12 lines
drivers/dma/mtk_uart_dma.c new file +12 -0
+static void mtk_dma_reset(struct mtk_chan *c)
+{
+	struct timespec a, b;
+	ktime_t begin, end;
+
+	mtk_dma_chan_write(c, VFF_ADDR, 0);
+	mtk_dma_chan_write(c, VFF_THRE, 0);
+	mtk_dma_chan_write(c, VFF_LEN, 0);
+	mtk_dma_chan_write(c, VFF_RST, VFF_WARM_RST_B);
+
+	begin = ktime_get();
+	a = ktime_to_timespec(begin);

drivers/dma/mtk_uart_dma.c line 356

Quoted 10 lines
drivers/dma/mtk_uart_dma.c new file +10 -0
+	while (mtk_dma_chan_read(c, VFF_EN)) {
+		end = ktime_get();
+		b = ktime_to_timespec(end);
+		if ((b.tv_sec - a.tv_sec) > 1 ||
+			((b.tv_sec - a.tv_sec) == 1 &&
+			b.tv_nsec > a.tv_nsec)) {
+			dev_info(c->vc.chan.device->dev,
+				"[UART] Polling VFF EN timeout\n");
+			return;
+		}

and here we go again

drivers/dma/mtk_uart_dma.c line 375

Quoted 11 lines
drivers/dma/mtk_uart_dma.c new file +11 -0
+static void mtk_dma_stop(struct mtk_chan *c)
+{
+	int polling_cnt;
+
+	mtk_dma_chan_write(c, VFF_FLUSH, VFF_FLUSH_CLR_B);
+
+	polling_cnt = 0;
+	while (mtk_dma_chan_read(c, VFF_FLUSH))	{
+		polling_cnt++;
+		if (polling_cnt > MTK_POLLING_CNT) {
+			dev_info(c->vc.chan.device->dev, "mtk_uart_stop_dma: polling VFF_FLUSH fail VFF_DEBUG_STATUS=0x%x\n",

126 chars, surely that must be a record!

drivers/dma/mtk_uart_dma.c line 387

Quoted 12 lines
drivers/dma/mtk_uart_dma.c new file +12 -0
+					mtk_dma_chan_read(c, VFF_DEBUG_STATUS));
+			break;
+		}
+	}
+
+	polling_cnt = 0;
+	/*set stop as 1 -> wait until en is 0 -> set stop as 0*/
+	mtk_dma_chan_write(c, VFF_STOP, VFF_STOP_B);
+	while (mtk_dma_chan_read(c, VFF_EN)) {
+		polling_cnt++;
+		if (polling_cnt > MTK_POLLING_CNT) {
+			dev_info(c->vc.chan.device->dev, "mtk_uart_stop_dma: polling VFF_EN fail VFF_DEBUG_STATUS=0x%x\n",

and here

Quoted 5 lines
drivers/dma/mtk_uart_dma.c new file +5 -0
+					mtk_dma_chan_read(c, VFF_DEBUG_STATUS));
+			break;
+		}
+	}
+	mtk_dma_chan_write(c, VFF_STOP, VFF_STOP_CLR_B);

drivers/dma/mtk_uart_dma.c line 402

Quoted 10 lines
drivers/dma/mtk_uart_dma.c new file +10 -0
+	mtk_dma_chan_write(c, VFF_INT_EN, VFF_INT_EN_CLR_B);
+	mtk_dma_chan_write(c, VFF_INT_FLAG, VFF_INT_FLAG_CLR_B);
+
+	c->paused = true;
+}
+
+/*
+ * This callback schedules all pending channels. We could be more
+ * clever here by postponing allocation of the real DMA channels to
+ * this point, and freeing them when our virtual channel becomes idle.

yeah sounds good to me :)

drivers/dma/mtk_uart_dma.c line 450

Quoted 10 lines
drivers/dma/mtk_uart_dma.c new file +10 -0
+static int mtk_dma_alloc_chan_resources(struct dma_chan *chan)
+{
+	struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
+	struct mtk_chan *c = to_mtk_dma_chan(chan);
+	int ret;
+
+	ret = -EBUSY;
+
+	if (mtkd->lch_map[c->dma_ch] == NULL) {
+		c->channel_base = mtkd->base + 0x80 * c->dma_ch;

this should be probe thingy, why are we doing this here?

drivers/dma/mtk_uart_dma.c line 490

Quoted 13 lines
drivers/dma/mtk_uart_dma.c new file +13 -0
+static enum dma_status mtk_dma_tx_status(struct dma_chan *chan,
+	dma_cookie_t cookie, struct dma_tx_state *txstate)
+{
+	struct mtk_chan *c = to_mtk_dma_chan(chan);
+	enum dma_status ret;
+	unsigned long flags;
+
+	ret = dma_cookie_status(chan, cookie, txstate);
+
+	spin_lock_irqsave(&c->vc.lock, flags);
+
+	if (ret == DMA_IN_PROGRESS) {
+		c->rx_ptr = mtk_dma_chan_read(c, VFF_RPT) & 0x0000ffffl;

magic!!

drivers/dma/mtk_uart_dma.c line 493

Quoted 3 lines
drivers/dma/mtk_uart_dma.c new file +3 -0
+		txstate->residue = c->rx_ptr;
+	} else if (ret == DMA_COMPLETE && c->cfg.direction == DMA_DEV_TO_MEM) {
+		txstate->residue = c->remain_size;

this seems incorrect, if it is complete then why residue?

Review

Quoted 2 lines
(quoted diff) +2 -0
+	} else {
+		txstate->residue = 0;

which is a no-op

Quoted 17 lines
drivers/dma/mtk_uart_dma.c new file +17 -0
+static struct dma_async_tx_descriptor *mtk_dma_prep_slave_sg(
+	struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
+	enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
+{
+	struct mtk_chan *c = to_mtk_dma_chan(chan);
+	enum dma_slave_buswidth dev_width;
+	struct scatterlist *sgent;
+	struct mtk_desc *d;
+	dma_addr_t dev_addr;
+	unsigned int i, j, en, frame_bytes;
+
+	en = frame_bytes = 1;
+
+	if (dir == DMA_DEV_TO_MEM) {
+		dev_addr = c->cfg.src_addr;
+		dev_width = c->cfg.src_addr_width;
+	} else if (dir == DMA_MEM_TO_DEV) {

drivers/dma/mtk_uart_dma.c line 542

Quoted 10 lines
drivers/dma/mtk_uart_dma.c new file +10 -0
+		dev_addr = c->cfg.dst_addr;
+		dev_width = c->cfg.dst_addr_width;
+	} else {
+		dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
+		return NULL;
+	}
+
+	/* Now allocate and setup the descriptor. */
+	d = kmalloc(sizeof(*d) + sglen * sizeof(d->sg[0]),
+				GFP_KERNEL | ___GFP_ZERO);

why ___GFP_ZERO? why not GFP_NOATOMIC?

Quoted 8 lines
drivers/dma/mtk_uart_dma.c new file +8 -0
+static int
+mtk_dma_slave_config(struct dma_chan *chan, struct dma_slave_config *cfg)
+{
+	struct mtk_chan *c = to_mtk_dma_chan(chan);
+	struct mtk_dmadev *mtkd = to_mtk_dma_dev(c->vc.chan.device);
+	int ret;
+
+	memcpy(&c->cfg, cfg, sizeof(c->cfg));

drivers/dma/mtk_uart_dma.c line 665

Quoted 10 lines
drivers/dma/mtk_uart_dma.c new file +10 -0
+
+	if (cfg->direction == DMA_DEV_TO_MEM) {
+		mtk_dma_chan_write(c, VFF_ADDR, cfg->src_addr);
+		mtk_dma_chan_write(c, VFF_LEN, cfg->src_addr_width*1024);
+		mtk_dma_chan_write(c, VFF_THRE,
+					VFF_RX_THRE(cfg->src_addr_width*1024));
+		mtk_dma_chan_write(c, VFF_INT_EN,
+					VFF_RX_INT_EN0_B | VFF_RX_INT_EN1_B);
+		mtk_dma_chan_write(c, VFF_INT_FLAG, VFF_INT_FLAG_CLR_B);
+		mtk_dma_chan_write(c, VFF_EN, VFF_EN_B);

this is wrong, you dont program channel here, but upon the descriptor issue. The values should be stored locally and used to prepare.

drivers/dma/mtk_uart_dma.c line 743

Quoted 5 lines
drivers/dma/mtk_uart_dma.c new file +5 -0
+static int mtk_dma_pause(struct dma_chan *chan)
+{
+	/* Pause/Resume only allowed with cyclic mode */
+	return -EINVAL;
+}

then remove it

drivers/dma/mtk_uart_dma.c line 834

Quoted 12 lines
drivers/dma/mtk_uart_dma.c new file +12 -0
+	dma_cap_set(DMA_SLAVE, mtkd->ddev.cap_mask);
+	mtkd->ddev.device_alloc_chan_resources = mtk_dma_alloc_chan_resources;
+	mtkd->ddev.device_free_chan_resources = mtk_dma_free_chan_resources;
+	mtkd->ddev.device_tx_status = mtk_dma_tx_status;
+	mtkd->ddev.device_issue_pending = mtk_dma_issue_pending;
+	mtkd->ddev.device_prep_slave_sg = mtk_dma_prep_slave_sg;
+	mtkd->ddev.device_config = mtk_dma_slave_config;
+	mtkd->ddev.device_pause = mtk_dma_pause;
+	mtkd->ddev.device_resume = mtk_dma_resume;
+	mtkd->ddev.device_terminate_all = mtk_dma_terminate_all;
+	mtkd->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);
+	mtkd->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);

1bytes width, are you sure about that?

drivers/dma/mtk_uart_dma.c line 950

Quoted 11 lines
drivers/dma/mtk_uart_dma.c new file +11 -0
+static int mtk_dma_init(void)
+{
+	return platform_driver_register(&mtk_dma_driver);
+}
+subsys_initcall(mtk_dma_init);
+
+static void __exit mtk_dma_exit(void)
+{
+	platform_driver_unregister(&mtk_dma_driver);
+}
+module_exit(mtk_dma_exit);

platform_driver_register() pls

Signature · 1 line
~Vinod
#9 lore raw
In reply to Long Cheng
Quoted 2 lines · Long Cheng

In Mediatek SOCs, the uart can support DMA function.
Base on DMA engine formwork, we add the DMA code to support uart. And put the code under drivers/dma.

Are these interdependent, if not can you send them separately please

Quoted 24 lines

This series contains document bindings, Kconfig to control the function enable or not, device tree including interrupt and dma device node, the code of UART DMA function.

Long Cheng (4):
dt-bindings: dma: uart: add uart dma bindings
dmaengine: mtk_uart_dma: add Mediatek uart DMA support
serial: 8250-mtk: add uart DMA support
arm: dts: mt2701: add uart APDMA to device tree

 .../devicetree/bindings/dma/mtk_uart_dma.txt       |   32 +
 arch/arm/boot/dts/mt2701-evb.dts                   |   22 +
 arch/arm/boot/dts/mt2701.dtsi                      |   29 +
 drivers/dma/Kconfig                                |    9 +
 drivers/dma/Makefile                               |    1 +
 drivers/dma/mtk_uart_dma.c                         |  955 ++++++++++++++++++++
 drivers/tty/serial/8250/8250_mtk.c                 |  194 +++-
 7 files changed, 1241 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/dma/mtk_uart_dma.txt
 create mode 100644 drivers/dma/mtk_uart_dma.c
Signature · 1 line
1.7.9.5
Signature · 1 line
~Vinod