[PATCH RFC 2/4] serial: esp32: Fix ESP32-S3 baud rate prescaler calculation
Vítor Hugo Silva
vitorhg20080 at gmail.com
Fri Nov 27 08:55:50 PST 2026
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 at 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);
More information about the linux-riscv
mailing list