[PATCH v5 2/7] serial: sh-sci: Keep TX enabled on TEI while data is pending

From: Diogo Ivo

Date: Tue Nov 17 2026 - 18:41:05 EST


From: Biju Das <biju.das.jz@xxxxxxxxxxxxxx>

sci_tx_end_interrupt() clears TE and TEIE unconditionally on SCI and
RSCI ports. If new data was queued or transmission restarted before the
TEI handler runs, disabling the transmitter stalls or truncates output.

To fix truncations, verify that poll_sent_bits are set, and address
stalls by checking for pending data (while the port is not suspended)
before clearing TE and TEIE. The existing TE/TEIE teardown path only
runs once nothing is left to send and we are sure that we are not still
sending data.

While at it, make sure to also stop TX when the port is stopped with data
queued.

Fixes: d61ae331d6f3 ("tty: serial: sh-sci: Add support for tx end interrupt handling")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Biju Das <biju.das.jz@xxxxxxxxxxxxxx>
Co-developed-by: Diogo Ivo <diogo.ivo.sx@xxxxxxxxxxx>
Signed-off-by: Diogo Ivo <diogo.ivo.sx@xxxxxxxxxxx>
---
Changes in v5:
- Make the commit message more succinct.
- Drop unused tie and teie initializers from sci_scif_port_params_bits.
- Enable SCSCR.TEIE in sci_stop_tx() on PORT_SCI and call
sci_stop_tx() in sci_transmit_chars() when empty or stopped.
- Check poll_sent_bits in sci_tx_end_interrupt() before acting on TEI.
- Prevent sci_tx_end_interrupt() from restarting TX during suspend.
---
drivers/tty/serial/rsci.c | 4 +++-
drivers/tty/serial/sh-sci-common.h | 2 ++
drivers/tty/serial/sh-sci.c | 45 ++++++++++++++++++++++++++++----------
3 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/drivers/tty/serial/rsci.c b/drivers/tty/serial/rsci.c
index 40db9daa4272..0391111db8ae 100644
--- a/drivers/tty/serial/rsci.c
+++ b/drivers/tty/serial/rsci.c
@@ -465,7 +465,7 @@ static void rsci_transmit_chars(struct uart_port *port)
if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
uart_write_wakeup(port);

- if (kfifo_is_empty(&tport->xmit_fifo)) {
+ if (kfifo_is_empty(&tport->xmit_fifo) || stopped) {
ctrl = rsci_serial_in(port, CCR0);
ctrl &= ~CCR0_TIE;
ctrl |= CCR0_TEIE;
@@ -627,6 +627,8 @@ static const struct sci_port_params_bits rsci_port_param_bits = {
.rxtx_enable = CCR0_RE | CCR0_TE,
.te_clear = CCR0_TE | CCR0_TEIE,
.poll_sent_bits = CSR_TDRE | CSR_TEND,
+ .tie = CCR0_TIE,
+ .teie = CCR0_TEIE
};

static const struct sci_port_params rsci_rzg3e_port_params = {
diff --git a/drivers/tty/serial/sh-sci-common.h b/drivers/tty/serial/sh-sci-common.h
index 01ff9fced803..273856fbc20b 100644
--- a/drivers/tty/serial/sh-sci-common.h
+++ b/drivers/tty/serial/sh-sci-common.h
@@ -56,6 +56,8 @@ struct sci_port_params_bits {
unsigned int rxtx_enable;
unsigned int te_clear;
unsigned int poll_sent_bits;
+ unsigned int tie;
+ unsigned int teie;
};

struct sci_common_regs {
diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
index 9eb297dbc901..c2de7e7faab6 100644
--- a/drivers/tty/serial/sh-sci.c
+++ b/drivers/tty/serial/sh-sci.c
@@ -261,7 +261,9 @@ static bool sci_uart_earlycon_dev_probing;
static const struct sci_port_params_bits sci_sci_port_params_bits = {
.rxtx_enable = SCSCR_RE | SCSCR_TE,
.te_clear = SCSCR_TE | SCSCR_TEIE,
- .poll_sent_bits = SCI_TDRE | SCI_TEND
+ .poll_sent_bits = SCI_TDRE | SCI_TEND,
+ .tie = SCSCR_TIE,
+ .teie = SCSCR_TEIE
};

static const struct sci_port_params_bits sci_scif_port_params_bits = {
@@ -810,6 +812,9 @@ static void sci_stop_tx(struct uart_port *port)

ctrl &= ~SCSCR_TIE;

+ if (s->type == PORT_SCI && (ctrl & SCSCR_TE))
+ ctrl |= SCSCR_TEIE;
+
sci_serial_out(port, SCSCR, ctrl);

#ifdef CONFIG_SERIAL_SH_SCI_DMA
@@ -1049,16 +1054,9 @@ static void sci_transmit_chars(struct uart_port *port)

if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
uart_write_wakeup(port);
- if (kfifo_is_empty(&tport->xmit_fifo)) {
- if (s->type == PORT_SCI) {
- ctrl = sci_serial_in(port, SCSCR);
- ctrl &= ~SCSCR_TIE;
- ctrl |= SCSCR_TEIE;
- sci_serial_out(port, SCSCR, ctrl);
- }

+ if (kfifo_is_empty(&tport->xmit_fifo) || stopped)
sci_stop_tx(port);
- }
}

static void sci_receive_chars(struct uart_port *port)
@@ -2012,17 +2010,40 @@ static irqreturn_t sci_tx_end_interrupt(int irq, void *ptr)
{
struct uart_port *port = ptr;
struct sci_port *s = to_sci_port(port);
+ struct tty_port *tport = &port->state->port;
+ const struct sci_port_params_bits *bits = s->params->param_bits;
const struct sci_common_regs *regs = s->params->common_regs;
unsigned long flags;
- u32 ctrl;
+ u32 ctrl, status;

if (s->type != PORT_SCI && !sci_is_rsci_type(s->type))
return sci_tx_interrupt(irq, ptr);

uart_port_lock_irqsave(port, &flags);
- ctrl = s->ops->read_reg(port, regs->control) &
- ~(s->params->param_bits->te_clear);
+
+ /*
+ * We may be sending data while we service this TEI. If so,
+ * bail out so we don't stop an ongoing TX.
+ */
+ status = s->ops->read_reg(port, regs->status);
+ if ((status & bits->poll_sent_bits) != bits->poll_sent_bits)
+ goto out_unlock;
+
+ ctrl = s->ops->read_reg(port, regs->control);
+
+ if (!port->suspended &&
+ (port->x_char ||
+ (!kfifo_is_empty(&tport->xmit_fifo) && !uart_tx_stopped(port)))) {
+ ctrl &= ~bits->teie;
+ ctrl |= bits->tie;
+ s->ops->write_reg(port, regs->control, ctrl);
+ s->ops->transmit_chars(port);
+ goto out_unlock;
+ }
+
+ ctrl &= ~bits->te_clear;
s->ops->write_reg(port, regs->control, ctrl);
+out_unlock:
uart_port_unlock_irqrestore(port, flags);

return IRQ_HANDLED;

--
2.56.1