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 2/4] serial: esp32: Fix ESP32-S3 baud rate prescaler calculation
Date: Fri, 27 Nov 2026 16:55:50 +0000 [thread overview]
Message-ID: <20261127165552.133713-2-vitorhg20080@gmail.com> (raw)
In-Reply-To: <20261127165552.133713-1-vitorhg20080@gmail.com>
The ESP32-S3 encodes the SCLK prescaler as divisor minus one. At the
maximum unscaled integer UART divider, the driver can program a
prescaler it did not use in the baud calculation. At lower rates the
prescaler can also exceed its register field.
Select the smallest prescaler that fits the UART divider, including
its fractional part, and reject an out-of-range prescaler before
programming the registers. Round up the minimum baud rate to avoid
advertising a rate the hardware cannot generate.
A mocked register test covered the divider boundary and rejected
38 baud while accepting 39 baud with a 40 MHz reference clock.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
drivers/tty/serial/esp32_uart.c | 40 ++++++++++++++++++---------------
1 file changed, 22 insertions(+), 18 deletions(-)
diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
index 895d0ec..0e7128a 100644
--- a/drivers/tty/serial/esp32_uart.c
+++ b/drivers/tty/serial/esp32_uart.c
@@ -8,6 +8,7 @@
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/irq.h>
+#include <linux/math.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
@@ -447,35 +448,35 @@ static void esp32_uart_shutdown(struct uart_port *port)
static bool esp32_uart_set_baud(struct uart_port *port, u32 baud)
{
+ u32 div_mask = port_variant(port)->clkdiv_mask;
u32 sclk = port->uartclk;
- u32 div;
+ u32 div, frag;
if (!baud || baud > sclk)
return false;
div = sclk / baud;
-
if (port_variant(port)->has_clkconf) {
- u32 sclk_div = div / port_variant(port)->clkdiv_mask;
+ /* The SCLK field encodes the divisor minus one. */
+ u32 sclk_div = div / (div_mask + 1);
- if (div > port_variant(port)->clkdiv_mask) {
- sclk /= (sclk_div + 1);
- div = sclk / baud;
- }
+ if (sclk_div > FIELD_MAX(ESP32S3_UART_SCLK_DIV_NUM))
+ return false;
+
+ 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;
- }
+ if (div > div_mask)
+ return false;
- return false;
+ frag = (sclk * 16) / baud - div * 16;
+ esp32_uart_write(port, UART_CLKDIV_REG,
+ div | FIELD_PREP(UART_CLKDIV_FRAG, frag));
+ return true;
}
static void esp32p4_uart_limit_baud_rate(struct ktermios *termios,
@@ -514,10 +515,13 @@ static void esp32_uart_set_termios(struct uart_port *port,
termios->c_cflag &= ~CMSPAR;
- if (port_variant(port)->has_clkconf)
- max_div *= FIELD_MAX(ESP32S3_UART_SCLK_DIV_NUM);
+ if (port_variant(port)->has_clkconf) {
+ max_div *= FIELD_MAX(ESP32S3_UART_SCLK_DIV_NUM) + 1;
+ min_baud = DIV_ROUND_UP(port->uartclk, max_div);
+ } else {
+ min_baud = port->uartclk / max_div;
+ }
- min_baud = port->uartclk / max_div;
max_baud = port->uartclk / 16;
if (port_variant(port) == &esp32p4_variant)
esp32p4_uart_limit_baud_rate(termios, old, min_baud, max_baud);
next prev parent reply other threads:[~2026-11-27 16:56 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 ` Vítor Hugo Silva [this message]
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 ` [PATCH RFC 1/2] serial: esp32: Enable hardware RTS/CTS flow control Vítor Hugo Silva
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=20261127165552.133713-2-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