From: "Vítor Hugo Silva" <vitorhg20080@gmail.com>
To: linux-serial@vger.kernel.org
Cc: gregkh@linuxfoundation.org, jirislaby@kernel.org,
linux-riscv@lists.infradead.org, sashiko-reviews@lists.linux.dev,
"Vítor Hugo Silva" <vitorhg20080@gmail.com>
Subject: [PATCH RFC 1/2] serial: esp32: Enable hardware RTS/CTS flow control
Date: Fri, 27 Nov 2026 19:25:10 +0000 [thread overview]
Message-ID: <20261127192514.174468-1-vitorhg20080@gmail.com> (raw)
In-Reply-To: <20261125233020.1380594-11-vitorhg20080@gmail.com>
CRTSCTS currently enables receive flow control without programming an
RX FIFO threshold or enabling CTS gating on transmit.
Set a 64-byte RX FIFO threshold using the register layout of each
variant. Enable hardware CTS gating and tell serial core that the
UART handles CTS. Let the FIFO control RTS while RTS is asserted,
and use software control to keep RTS deasserted when requested.
On the Waveshare ESP32-P4-Module-DEV-KIT, UART1 internal loopback
verified that deasserting RTS with CRTSCTS enabled stopped transmission
and reasserting RTS delivered the queued bytes intact. With CRTSCTS
disabled, transmission completed with RTS deasserted.
AI assisted with driver development and the loopback test.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
Applies on top of the ESP32-P4 UART RFC through patch 13/19 with the
four UART fixes for patch 10/19 and v2 of patch 12/19 applied.
RFC: https://lore.kernel.org/linux-serial/20261125233020.1380594-1-vitorhg20080@gmail.com/
UART fixes: https://lore.kernel.org/linux-serial/20261127165552.133713-1-vitorhg20080@gmail.com/
TX IRQ v2: https://lore.kernel.org/linux-serial/20261127155623.129706-1-vitorhg20080@gmail.com/
drivers/tty/serial/esp32_uart.c | 82 +++++++++++++++++++++++++++------
1 file changed, 68 insertions(+), 14 deletions(-)
diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
index 75f243f..7d58a41 100644
--- a/drivers/tty/serial/esp32_uart.c
+++ b/drivers/tty/serial/esp32_uart.c
@@ -26,6 +26,7 @@
#define ESP32_UART_TX_FIFO_SIZE 127
#define ESP32_UART_RX_FIFO_SIZE 127
+#define ESP32_UART_RTS_THRESHOLD 64
#define ESP32_UART_TX_POLLS 1000000
#define UART_FIFO_REG 0x00
@@ -71,8 +72,11 @@
#define UART_RXFIFO_FULL_THRHD_SHIFT 0
#define ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT 8
#define ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT 10
+#define ESP32_UART_RX_FLOW_THRHD GENMASK(22, 16)
#define ESP32_UART_RX_FLOW_EN BIT(23)
#define ESP32S3_UART_RX_FLOW_EN BIT(22)
+#define ESP32S3_UART_MEM_CONF_REG 0x60
+#define ESP32S3_UART_RX_FLOW_THRHD GENMASK(16, 7)
#define ESP32S3_UART_CLK_CONF_REG 0x78
#define ESP32S3_UART_SCLK_DIV_B GENMASK(5, 0)
#define ESP32S3_UART_SCLK_DIV_A GENMASK(11, 6)
@@ -101,10 +105,12 @@
/* ESP32-P4 uses a dedicated register for RX flow control. */
#define ESP32P4_UART_HWFC_CONF_REG 0x2c
+#define ESP32P4_UART_RX_FLOW_THRHD GENMASK(7, 0)
#define ESP32P4_UART_RX_FLOW_EN BIT(8)
#define ESP32P4_UART_REG_UPDATE_REG 0x98
#define ESP32P4_UART_REG_UPDATE BIT(0)
#define ESP32P4_UART_LOOPBACK BIT(12)
+#define ESP32P4_UART_TX_FLOW_EN BIT(13)
#define ESP32P4_UART_SW_RTS BIT(21)
#define ESP32P4_UART_SW_DTR BIT(20)
#define ESP32P4_UART_RTS_INV BIT(18)
@@ -121,8 +127,11 @@ struct esp32_uart_variant {
u32 rxfifo_cnt_mask;
u32 txfifo_cnt_mask;
u32 txfifo_empty_thrhd_shift;
+ u32 tx_flow_en;
u32 rx_flow_reg;
u32 rx_flow_en;
+ u32 rx_flow_thresh_reg;
+ u32 rx_flow_thresh_mask;
u32 loopback_mask;
u32 sw_rts_mask;
u32 sw_dtr_reg;
@@ -138,8 +147,11 @@ static const struct esp32_uart_variant esp32_variant = {
.rxfifo_cnt_mask = ESP32_UART_RXFIFO_CNT,
.txfifo_cnt_mask = ESP32_UART_TXFIFO_CNT,
.txfifo_empty_thrhd_shift = ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT,
+ .tx_flow_en = UART_TX_FLOW_EN,
.rx_flow_reg = UART_CONF1_REG,
.rx_flow_en = ESP32_UART_RX_FLOW_EN,
+ .rx_flow_thresh_reg = UART_CONF1_REG,
+ .rx_flow_thresh_mask = ESP32_UART_RX_FLOW_THRHD,
.type = "ESP32 UART",
.loopback_mask = UART_LOOPBACK,
.sw_rts_mask = UART_SW_RTS,
@@ -153,8 +165,11 @@ static const struct esp32_uart_variant esp32s3_variant = {
.rxfifo_cnt_mask = ESP32S3_UART_RXFIFO_CNT,
.txfifo_cnt_mask = ESP32S3_UART_TXFIFO_CNT,
.txfifo_empty_thrhd_shift = ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT,
+ .tx_flow_en = UART_TX_FLOW_EN,
.rx_flow_reg = UART_CONF1_REG,
.rx_flow_en = ESP32S3_UART_RX_FLOW_EN,
+ .rx_flow_thresh_reg = ESP32S3_UART_MEM_CONF_REG,
+ .rx_flow_thresh_mask = ESP32S3_UART_RX_FLOW_THRHD,
.type = "ESP32S3 UART",
.has_clkconf = true,
.loopback_mask = UART_LOOPBACK,
@@ -169,8 +184,11 @@ static const struct esp32_uart_variant esp32p4_variant = {
.rxfifo_cnt_mask = ESP32P4_UART_RXFIFO_CNT,
.txfifo_cnt_mask = ESP32P4_UART_TXFIFO_CNT,
.txfifo_empty_thrhd_shift = ESP32P4_UART_TXFIFO_EMPTY_THRHD_SHIFT,
+ .tx_flow_en = ESP32P4_UART_TX_FLOW_EN,
.rx_flow_reg = ESP32P4_UART_HWFC_CONF_REG,
.rx_flow_en = ESP32P4_UART_RX_FLOW_EN,
+ .rx_flow_thresh_reg = ESP32P4_UART_HWFC_CONF_REG,
+ .rx_flow_thresh_mask = ESP32P4_UART_RX_FLOW_THRHD,
.type = "ESP32-P4 UART",
.needs_reg_update = true,
.loopback_mask = ESP32P4_UART_LOOPBACK,
@@ -249,16 +267,43 @@ static unsigned int esp32_uart_tx_empty(struct uart_port *port)
return esp32_uart_tx_fifo_cnt(port) ? 0 : TIOCSER_TEMT;
}
+static void esp32_uart_set_rx_flow(struct uart_port *port, bool enable)
+{
+ const struct esp32_uart_variant *variant = port_variant(port);
+ u32 flow = esp32_uart_read(port, variant->rx_flow_reg);
+
+ if (enable) {
+ u32 mask = variant->rx_flow_thresh_mask;
+ u32 threshold = field_prep(mask, ESP32_UART_RTS_THRESHOLD);
+ u32 reg = variant->rx_flow_thresh_reg;
+
+ if (reg == variant->rx_flow_reg) {
+ flow = (flow & ~mask) | threshold;
+ } else {
+ u32 val = esp32_uart_read(port, reg);
+
+ esp32_uart_write(port, reg, (val & ~mask) | threshold);
+ }
+ flow |= variant->rx_flow_en;
+ } else {
+ flow &= ~variant->rx_flow_en;
+ }
+
+ esp32_uart_write(port, variant->rx_flow_reg, flow);
+}
+
static void esp32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
const struct esp32_uart_variant *variant = port_variant(port);
bool dtr_in_conf0 = variant->sw_dtr_reg == UART_CONF0_REG;
+ bool hw_flow = port->status & UPSTAT_AUTOCTS;
+ bool rts = mctrl & TIOCM_RTS;
u32 conf0, dtr_conf;
conf0 = esp32_uart_read(port, UART_CONF0_REG);
conf0 &= ~(variant->loopback_mask | variant->sw_rts_mask);
- if (mctrl & TIOCM_RTS)
+ if (rts)
conf0 |= variant->sw_rts_mask;
if (mctrl & TIOCM_LOOP)
conf0 |= variant->loopback_mask;
@@ -275,7 +320,14 @@ static void esp32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
else
esp32_uart_write(port, variant->sw_dtr_reg, dtr_conf);
+ /* Keep RTS deasserted until the FIFO can control it again. */
+ if (hw_flow && rts)
+ esp32_uart_set_rx_flow(port, true);
+
esp32_uart_write(port, UART_CONF0_REG, conf0);
+
+ if (hw_flow && !rts)
+ esp32_uart_set_rx_flow(port, false);
}
static unsigned int esp32_uart_get_mctrl(struct uart_port *port)
@@ -515,16 +567,16 @@ static void esp32_uart_set_termios(struct uart_port *port,
struct ktermios *termios,
const struct ktermios *old)
{
+ const struct esp32_uart_variant *variant = port_variant(port);
+ bool hw_flow = termios->c_cflag & CRTSCTS;
unsigned long flags;
- u32 conf0, flow;
+ u32 conf0;
u32 baud, min_baud, max_baud;
- const u32 rx_flow_reg = port_variant(port)->rx_flow_reg;
- const u32 rx_flow_en = port_variant(port)->rx_flow_en;
- u32 max_div = port_variant(port)->clkdiv_mask;
+ u32 max_div = variant->clkdiv_mask;
termios->c_cflag &= ~CMSPAR;
- if (port_variant(port)->has_clkconf) {
+ if (variant->has_clkconf) {
max_div *= FIELD_MAX(ESP32S3_UART_SCLK_DIV_NUM) + 1;
min_baud = DIV_ROUND_UP(port->uartclk, max_div);
} else {
@@ -532,17 +584,15 @@ static void esp32_uart_set_termios(struct uart_port *port,
}
max_baud = port->uartclk / 16;
- if (port_variant(port) == &esp32p4_variant)
+ if (variant == &esp32p4_variant)
esp32p4_uart_limit_baud_rate(termios, old, min_baud, max_baud);
baud = uart_get_baud_rate(port, termios, old, min_baud, max_baud);
spin_lock_irqsave(&port->lock, flags);
conf0 = esp32_uart_read(port, UART_CONF0_REG);
- conf0 &= ~(UART_PARITY_EN | UART_PARITY | UART_BIT_NUM | UART_STOP_BIT_NUM);
-
- flow = esp32_uart_read(port, rx_flow_reg);
- flow &= ~rx_flow_en;
+ conf0 &= ~(UART_PARITY_EN | UART_PARITY | UART_BIT_NUM | UART_STOP_BIT_NUM |
+ variant->tx_flow_en);
if (termios->c_cflag & PARENB) {
conf0 |= UART_PARITY_EN;
@@ -570,11 +620,15 @@ static void esp32_uart_set_termios(struct uart_port *port,
else
conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_1);
- if (termios->c_cflag & CRTSCTS)
- flow |= rx_flow_en;
+ if (hw_flow) {
+ conf0 |= variant->tx_flow_en;
+ port->status |= UPSTAT_AUTOCTS;
+ } else {
+ port->status &= ~UPSTAT_AUTOCTS;
+ }
esp32_uart_write(port, UART_CONF0_REG, conf0);
- esp32_uart_write(port, rx_flow_reg, flow);
+ esp32_uart_set_rx_flow(port, hw_flow && (port->mctrl & TIOCM_RTS));
if (baud) {
esp32_uart_set_baud(port, baud);
--
2.56.1
next prev parent reply other threads:[~2026-11-27 19:25 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 09/19] dt-bindings: serial: Add Espressif ESP32-P4 UART Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 10/19] serial: esp32: Restore the ESP32 UART driver Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 16:55 ` [PATCH RFC 1/4] serial: esp32: Validate the baud rate before programming the UART Vítor Hugo Silva
2026-11-27 16:55 ` [PATCH RFC 2/4] serial: esp32: Fix ESP32-S3 baud rate prescaler calculation Vítor Hugo Silva
2026-11-27 16:55 ` [PATCH RFC 3/4] serial: esp32: Deliver and mask receive break interrupts Vítor Hugo Silva
2026-11-27 16:55 ` [PATCH RFC 4/4] serial: esp32: Enable the UART clock during port registration Vítor Hugo Silva
2026-11-27 19:25 ` Vítor Hugo Silva [this message]
2026-11-27 19:25 ` [PATCH RFC 2/2] serial: esp32: Report active-low CTS correctly on ESP32-S3 and ESP32-P4 Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 11/19] serial: esp32: Add support for the ESP32-P4 UART Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 19:39 ` [PATCH RFC v2] " Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 12/19] serial: esp32: Rearm TX interrupts when the FIFO is full Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 15:40 ` Vítor Hugo Silva
2026-11-27 15:56 ` [PATCH RFC v2] " Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 13/19] serial: esp32: Add early console support for ESP32-P4 Vítor Hugo Silva
2026-11-26 23:32 ` [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127192514.174468-1-vitorhg20080@gmail.com \
--to=vitorhg20080@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-serial@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox