Linux Serial subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vítor Hugo Silva" <vitorhg20080@gmail.com>
Cc: devicetree@vger.kernel.org, linux-serial@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH RFC 10/19] serial: esp32: Restore the ESP32 UART driver
Date: Thu, 26 Nov 2026 23:32:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-236332@kernel.org> (raw)
In-Reply-To: <20261125233020.1380594-11-vitorhg20080@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] serial: esp32: divide-by-zero in esp32_uart_set_baud() during
  earlycon setup
- [Medium] serial: esp32: SCLK prescaler miscalculation in
  esp32_uart_set_baud()
- [Medium] serial: esp32: unclocked MMIO access in
  esp32_uart_set_mctrl() during probe
- [Medium] serial: esp32: missing TTY_BREAK insertion in
  esp32_uart_rxint()
- [Medium] serial: esp32: UART_BRK_DET_INT left enabled in
  esp32_uart_stop_rx()
- [Low] serial: esp32: incomplete CRTSCTS flow control in
  esp32_uart_set_termios()
--

commit 12c65ed88656e1a1416d2544ca647a643b9237af
Author: Vítor Hugo Silva <vitorhg20080@gmail.com>

serial: esp32: Restore the ESP32 UART driver

This commit restores the Espressif ESP32 UART driver under COMPILE_TEST
with esp32_uart_console_setup() moved to regular text.

> diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
> new file mode 100644
> index 0000000000000..8f257cf0d2909
> --- /dev/null
> +++ b/drivers/tty/serial/esp32_uart.c

[ ... ]

> +static void esp32_uart_rxint(struct uart_port *port)
> +{
> +	struct tty_port *tty_port = &port->state->port;
> +	u32 rx_fifo_cnt = esp32_uart_rx_fifo_cnt(port);
> +	unsigned long flags;
> +	u32 i;
> +
> +	if (!rx_fifo_cnt)
> +		return;
> +
> +	spin_lock_irqsave(&port->lock, flags);
> +
> +	for (i = 0; i < rx_fifo_cnt; ++i) {
> +		u32 rx = esp32_uart_read(port, UART_FIFO_REG);
> +
> +		if (!rx &&
> +		    (esp32_uart_read(port, UART_INT_ST_REG) & UART_BRK_DET_INT)) {
> +			esp32_uart_write(port, UART_INT_CLR_REG, UART_BRK_DET_INT);
> +			++port->icount.brk;
> +			uart_handle_break(port);
> +		} else {

[Severity: Medium]
Should esp32_uart_rxint() insert a TTY_BREAK character into tty_port when
uart_handle_break() returns 0?  Without tty_insert_flip_char(tty_port, rx,
TTY_BREAK), breaks not consumed by SysRq are silently dropped and never
reach the TTY line discipline.

[ ... ]

> +static void esp32_uart_stop_rx(struct uart_port *port)
> +{
> +	u32 int_ena;
> +
> +	int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
> +	int_ena &= ~UART_RXFIFO_FULL_INT;
> +	esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
> +}

[Severity: Medium]
Should esp32_uart_stop_rx() also clear UART_BRK_DET_INT?

esp32_uart_startup() enables both UART_RXFIFO_FULL_INT and
UART_BRK_DET_INT, and esp32_uart_int() calls esp32_uart_rxint() when
either bit is set.  If a break arrives after stop_rx(), esp32_uart_rxint()
will still run and push received bytes to the flip buffer.

[ ... ]

> +static bool esp32_uart_set_baud(struct uart_port *port, u32 baud)
> +{
> +	u32 sclk = port->uartclk;
> +	u32 div = sclk / baud;
> +
> +	if (port_variant(port)->has_clkconf) {
> +		u32 sclk_div = div / port_variant(port)->clkdiv_mask;
> +
> +		if (div > port_variant(port)->clkdiv_mask) {
> +			sclk /= (sclk_div + 1);
> +			div = sclk / baud;
> +		}
> +		esp32_uart_write(port, ESP32S3_UART_CLK_CONF_REG,
> +				 FIELD_PREP(ESP32S3_UART_SCLK_DIV_NUM, sclk_div) |
> +				 ESP32S3_UART_CLK_CONF_DEFAULT);
> +	}
> +
> +	if (div <= port_variant(port)->clkdiv_mask) {
> +		u32 frag = (sclk * 16) / baud - div * 16;
> +
> +		esp32_uart_write(port, UART_CLKDIV_REG,
> +				 div | FIELD_PREP(UART_CLKDIV_FRAG, frag));
> +		return true;
> +	}
> +
> +	return false;
> +}
> +
> +static void esp32_uart_set_termios(struct uart_port *port,
> +				   struct ktermios *termios,
> +				   const struct ktermios *old)
> +{
> +	unsigned long flags;
> +	u32 conf0, conf1;
> +	u32 baud;
> +	const u32 rx_flow_en = port_variant(port)->rx_flow_en;
> +	u32 max_div = port_variant(port)->clkdiv_mask;
> +
> +	termios->c_cflag &= ~CMSPAR;
> +
> +	if (port_variant(port)->has_clkconf)
> +		max_div *= FIELD_MAX(ESP32S3_UART_SCLK_DIV_NUM);
> +
> +	baud = uart_get_baud_rate(port, termios, old,
> +				  port->uartclk / max_div,
> +				  port->uartclk / 16);

[Severity: Medium]
Can esp32_uart_set_baud() miscalculate the SCLK prescaler on variants with
has_clkconf?

When div equals clkdiv_mask (4095 on ESP32-S3, e.g. 9768 baud at 40 MHz),
sclk_div is 1, but the div > clkdiv_mask branch is skipped.  That writes
sclk_div = 1 into ESP32S3_UART_CLK_CONF_REG (dividing the source clock by
2) while leaving div = 4095 unscaled in UART_CLKDIV_REG, halving the baud
rate.

In addition, port->uartclk / max_div in esp32_uart_set_termios() truncates
downward (38 baud at 40 MHz with max_div = 4095 * 255), which yields div =
1052631 and sclk_div = 257 in esp32_uart_set_baud(), overflowing the 8-bit
ESP32S3_UART_SCLK_DIV_NUM field in FIELD_PREP().

[ ... ]

> +	if (termios->c_cflag & CRTSCTS)
> +		conf1 |= rx_flow_en;
> +
> +	esp32_uart_write(port, UART_CONF0_REG, conf0);
> +	esp32_uart_write(port, UART_CONF1_REG, conf1);

[Severity: Low]
Does CRTSCTS handling in esp32_uart_set_termios() leave hardware flow
control incomplete?

It toggles rx_flow_en in UART_CONF1_REG, but UART_TX_FLOW_EN in
UART_CONF0_REG is never set or cleared for CTS transmit flow control, and
esp32_uart_startup() overwrites UART_CONF1_REG without programming an RX
flow control threshold.

[ ... ]

> +static int __init esp32xx_uart_early_console_setup(struct earlycon_device *device,
> +						   const char *options)
> +{
> +	if (!device->port.membase)
> +		return -ENODEV;
> +
> +	device->con->write = esp32_uart_earlycon_write;
> +#ifdef CONFIG_CONSOLE_POLL
> +	device->con->read = esp32_uart_earlycon_read;
> +#endif
> +	if (device->port.uartclk != BASE_BAUD * 16)
> +		esp32_uart_set_baud(&device->port, device->baud);
> +
> +	return 0;
> +}

[Severity: Medium]
Can esp32xx_uart_early_console_setup() divide by zero or zero out
UART_CLKDIV_REG in esp32_uart_set_baud()?

When earlycon is set up via OF_EARLYCON_DECLARE, of_setup_earlycon() leaves
port.uartclk at 0 if clock-frequency is absent in Devicetree (as required
by esp,esp32-uart.yaml), which is not equal to BASE_BAUD * 16 on
architectures with a non-zero BASE_BAUD.  If device->baud is also 0 (or a
command-line earlycon specifies a clock rate without a baud rate),
sclk / baud in esp32_uart_set_baud() divides by zero; if device->baud is
set while port.uartclk is 0, esp32_uart_set_baud() writes a zero divisor to
UART_CLKDIV_REG.  Checking device->port.uartclk && device->baud before
calling esp32_uart_set_baud() would avoid both.

[ ... ]

> +static int esp32_uart_probe(struct platform_device *pdev)
> +{

[ ... ]

> +	sport->clk = devm_clk_get(&pdev->dev, NULL);
> +	if (IS_ERR(sport->clk))
> +		return PTR_ERR(sport->clk);
> +
> +	port->uartclk = clk_get_rate(sport->clk);
> +	port->dev = &pdev->dev;
> +	port->type = PORT_GENERIC;
> +	port->iotype = UPIO_MEM;
> +	port->irq = platform_get_irq(pdev, 0);
> +	port->ops = &esp32_uart_pops;
> +	port->flags = UPF_BOOT_AUTOCONF;
> +	port->has_sysrq = 1;
> +	port->fifosize = ESP32_UART_TX_FIFO_SIZE;
> +	port->private_data = (void *)device_get_match_data(&pdev->dev);
> +
> +	esp32_uart_ports[port->line] = sport;
> +
> +	platform_set_drvdata(pdev, port);
> +
> +	return uart_add_one_port(&esp32_uart_reg, port);
> +}

[Severity: Medium]
Does uart_add_one_port() access UART_CONF0_REG before sport->clk is
enabled?

During uart_add_one_port(), uart_configure_port() calls
esp32_uart_set_mctrl(), which reads and writes UART_CONF0_REG before
esp32_uart_console_setup() or esp32_uart_startup() enables sport->clk
(and esp32_uart_pops has no .pm callback).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125233020.1380594-1-vitorhg20080@gmail.com?part=10

  reply	other threads:[~2026-11-26 23:32 UTC|newest]

Thread overview: 10+ 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 [this message]
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-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-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=sashiko-outbox-236332@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vitorhg20080@gmail.com \
    /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