[PATCH 13/13] serial: linflexuart: Avoid stopping DMA during receive operations
From: Larisa Grigore
Date: Mon Feb 16 2026 - 10:12:33 EST
From: Radu Pirea <radu-nicolae.pirea@xxxxxxx>
Replace DMA single transactions with DMA cyclic transactions. Characters
may be lost between two single DMA transactions if the CPU is running at
lower frequencies.
Signed-off-by: Radu Pirea <radu-nicolae.pirea@xxxxxxx>
Co-developed-by: Larisa Grigore <larisa.grigore@xxxxxxxxxxx>
Signed-off-by: Larisa Grigore <larisa.grigore@xxxxxxxxxxx>
---
drivers/tty/serial/fsl_linflexuart.c | 119 +++++++++++++++------------
1 file changed, 68 insertions(+), 51 deletions(-)
diff --git a/drivers/tty/serial/fsl_linflexuart.c b/drivers/tty/serial/fsl_linflexuart.c
index dff37c68cff0..4598c7ff669e 100644
--- a/drivers/tty/serial/fsl_linflexuart.c
+++ b/drivers/tty/serial/fsl_linflexuart.c
@@ -6,6 +6,7 @@
* Copyright 2017-2019, 2021-2022, 2025 NXP
*/
+#include <linux/circ_buf.h>
#include <linux/clk.h>
#include <linux/console.h>
#include <linux/dma-mapping.h>
@@ -180,7 +181,7 @@ struct linflex_port {
dma_addr_t dma_rx_buf_bus;
dma_cookie_t dma_tx_cookie;
dma_cookie_t dma_rx_cookie;
- unsigned char *dma_rx_buf_virt;
+ struct circ_buf dma_rx_ring_buf;
unsigned int dma_tx_bytes;
int dma_tx_in_progress;
int dma_rx_in_progress;
@@ -210,28 +211,63 @@ to_linflex_port(struct uart_port *uart)
return container_of(uart, struct linflex_port, port);
}
-static void linflex_copy_rx_to_tty(struct linflex_port *lfport,
- struct tty_port *tty, int count)
+static void linflex_copy_rx_to_tty(struct linflex_port *lfport)
{
- size_t copied;
-
- lfport->port.icount.rx += count;
+ struct circ_buf *ring_buf = &lfport->dma_rx_ring_buf;
+ struct tty_port *port = &lfport->port.state->port;
+ size_t count, received = 0, copied = 0;
+ struct dma_tx_state state;
+ enum dma_status dmastat;
+ int new_head;
- if (!tty) {
+ if (!port) {
dev_err(lfport->port.dev, "No tty port\n");
return;
}
+ dmastat = dmaengine_tx_status(lfport->dma_rx_chan, lfport->dma_rx_cookie, &state);
+ if (dmastat == DMA_ERROR) {
+ dev_err(lfport->port.dev, "Rx DMA transfer failed!\n");
+ return;
+ }
+
+ new_head = FSL_UART_RX_DMA_BUFFER_SIZE - state.residue;
+ if (ring_buf->head == new_head)
+ return;
+
+ ring_buf->head = new_head;
dma_sync_single_for_cpu(lfport->port.dev, lfport->dma_rx_buf_bus,
FSL_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
- copied = tty_insert_flip_string(tty,
- ((unsigned char *)(lfport->dma_rx_buf_virt)),
- count);
- if (copied != count) {
- WARN_ON(1);
- dev_err(lfport->port.dev, "RxData copy to tty layer failed\n");
+ if (ring_buf->head > FSL_UART_RX_DMA_BUFFER_SIZE)
+ dev_err_once(lfport->port.dev,
+ "Circular buffer head bigger than the buffer size\n");
+
+ if (ring_buf->head < ring_buf->tail) {
+ count = FSL_UART_RX_DMA_BUFFER_SIZE - ring_buf->tail;
+ received += count;
+ copied += tty_insert_flip_string(port, ring_buf->buf + ring_buf->tail, count);
+ ring_buf->tail = 0;
+ lfport->port.icount.rx += count;
}
+
+ if (ring_buf->head > ring_buf->tail) {
+ count = ring_buf->head - ring_buf->tail;
+ received += count;
+ copied += tty_insert_flip_string(port, ring_buf->buf + ring_buf->tail, count);
+ if (ring_buf->head >= FSL_UART_RX_DMA_BUFFER_SIZE)
+ ring_buf->head = 0;
+ ring_buf->tail = ring_buf->head;
+ lfport->port.icount.rx += count;
+ }
+
+ if (copied != received)
+ dev_err_once(lfport->port.dev, "RxData copy to tty layer failed\n");
+
+ dma_sync_single_for_device(lfport->port.dev, lfport->dma_rx_buf_bus,
+ FSL_UART_RX_DMA_BUFFER_SIZE,
+ DMA_FROM_DEVICE);
+ tty_flip_buffer_push(port);
}
static void linflex_enable_dma_rx(struct uart_port *port)
@@ -348,8 +384,6 @@ static void _linflex_stop_rx(struct uart_port *port)
static void linflex_stop_rx(struct uart_port *port)
{
struct linflex_port *lfport = to_linflex_port(port);
- struct dma_tx_state state;
- unsigned int count;
_linflex_stop_rx(port);
@@ -357,14 +391,12 @@ static void linflex_stop_rx(struct uart_port *port)
return;
dmaengine_pause(lfport->dma_rx_chan);
- dmaengine_tx_status(lfport->dma_rx_chan,
- lfport->dma_rx_cookie, &state);
+ linflex_copy_rx_to_tty(lfport);
+ lfport->dma_rx_ring_buf.head = 0;
+ lfport->dma_rx_ring_buf.tail = 0;
dmaengine_terminate_all(lfport->dma_rx_chan);
- count = FSL_UART_RX_DMA_BUFFER_SIZE - state.residue;
lfport->dma_rx_in_progress = 0;
- linflex_copy_rx_to_tty(lfport, &port->state->port, count);
- tty_flip_buffer_push(&port->state->port);
}
static void linflex_put_char(struct uart_port *sport, unsigned char c)
@@ -501,11 +533,12 @@ static int linflex_dma_rx(struct linflex_port *lfport)
dma_sync_single_for_device(lfport->port.dev, lfport->dma_rx_buf_bus,
FSL_UART_RX_DMA_BUFFER_SIZE,
DMA_FROM_DEVICE);
- lfport->dma_rx_desc = dmaengine_prep_slave_single(lfport->dma_rx_chan,
- lfport->dma_rx_buf_bus,
- FSL_UART_RX_DMA_BUFFER_SIZE,
- DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
- DMA_CTRL_ACK);
+ lfport->dma_rx_desc =
+ dmaengine_prep_dma_cyclic(lfport->dma_rx_chan,
+ lfport->dma_rx_buf_bus,
+ FSL_UART_RX_DMA_BUFFER_SIZE,
+ FSL_UART_RX_DMA_BUFFER_SIZE / 2,
+ DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
if (!lfport->dma_rx_desc) {
dev_err(lfport->port.dev, "Not able to get desc for rx\n");
@@ -525,11 +558,8 @@ static int linflex_dma_rx(struct linflex_port *lfport)
static void linflex_dma_rx_complete(void *arg)
{
struct linflex_port *lfport = arg;
- struct tty_port *port = &lfport->port.state->port;
unsigned long flags;
- timer_delete_sync(&lfport->timer);
-
uart_port_lock_irqsave(&lfport->port, &flags);
/* stopped before? */
@@ -538,34 +568,17 @@ static void linflex_dma_rx_complete(void *arg)
return;
}
- lfport->dma_rx_in_progress = 0;
- linflex_copy_rx_to_tty(lfport, port, FSL_UART_RX_DMA_BUFFER_SIZE);
- tty_flip_buffer_push(port);
- linflex_dma_rx(lfport);
+ linflex_copy_rx_to_tty(lfport);
uart_port_unlock_irqrestore(&lfport->port, flags);
-
mod_timer(&lfport->timer, jiffies + lfport->dma_rx_timeout);
}
static void linflex_timer_func(struct timer_list *t)
{
struct linflex_port *lfport = timer_container_of(lfport, t, timer);
- unsigned long flags;
-
- uart_port_lock_irqsave(&lfport->port, &flags);
- /* stopped before? */
- if (!lfport->dma_rx_in_progress) {
- uart_port_unlock_irqrestore(&lfport->port, flags);
- return;
- }
-
- linflex_stop_rx(&lfport->port);
- linflex_dma_rx(lfport);
-
- uart_port_unlock_irqrestore(&lfport->port, flags);
- mod_timer(&lfport->timer, jiffies + lfport->dma_rx_timeout);
+ linflex_dma_rx_complete(lfport);
}
static void _linflex_start_tx(struct uart_port *port)
@@ -827,8 +840,8 @@ static int linflex_dma_rx_request(struct uart_port *port)
{
struct linflex_port *lfport = to_linflex_port(port);
struct dma_slave_config dma_rx_sconfig;
- unsigned char *dma_buf;
dma_addr_t dma_bus;
+ char *dma_buf;
int ret;
dma_buf = devm_kmalloc(port->dev, FSL_UART_RX_DMA_BUFFER_SIZE,
@@ -860,7 +873,9 @@ static int linflex_dma_rx_request(struct uart_port *port)
return ret;
}
- lfport->dma_rx_buf_virt = dma_buf;
+ lfport->dma_rx_ring_buf.buf = dma_buf;
+ lfport->dma_rx_ring_buf.head = 0;
+ lfport->dma_rx_ring_buf.tail = 0;
lfport->dma_rx_buf_bus = dma_bus;
lfport->dma_rx_in_progress = 0;
@@ -883,10 +898,12 @@ static void linflex_dma_rx_free(struct uart_port *port)
dma_unmap_single(lfport->port.dev, lfport->dma_rx_buf_bus,
FSL_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
- devm_kfree(lfport->port.dev, lfport->dma_rx_buf_virt);
+ devm_kfree(lfport->port.dev, lfport->dma_rx_ring_buf.buf);
lfport->dma_rx_buf_bus = 0;
- lfport->dma_rx_buf_virt = NULL;
+ lfport->dma_rx_ring_buf.buf = NULL;
+ lfport->dma_rx_ring_buf.head = 0;
+ lfport->dma_rx_ring_buf.tail = 0;
}
static int linflex_startup(struct uart_port *port)
--
2.47.0