* [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4
@ 2026-11-25 23:30 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
` (5 more replies)
0 siblings, 6 replies; 12+ messages in thread
From: Vítor Hugo Silva @ 2026-11-25 23:30 UTC (permalink / raw)
To: linux-riscv
Cc: devicetree, linux-kernel, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Thomas Gleixner, Daniel Lezcano, Greg Kroah-Hartman,
Jiri Slaby, linux-serial, Christian Brauner, Kees Cook,
David Hildenbrand, linux-fsdevel, linux-mm
Hello,
This series of patches adds the initial support for the Espressif's
ESP32-P4 on the Waveshare ESP32-P4-Module-DEV-KIT. It boots in M-mode on
a RV32 NOMMU kernel with one HP hart.
The series adds CLIC trap handling, drivers for clic and for interrupt
matrix, and extends the CLINT timer driver. It restores a previously
removed ESP32 UART driver with ESP32-P4 UART support and early console
support. It also synchronizes the instructions written to the PSRAM,
aligns the init data to the ESP32-P4 PMP granularity, and it adds DT
bindings and the board devicetree.
The boot firmware initializes PSRAM, loads the kernel, DTB and the
initramfs from the microSD, and it supplies the timebase-frequency
before jumping into the kernel.
The bootloader source and microSD boot instructions are available at:
https://github.com/pushdword/waveshare-esp32-p4-bootloader/blob/390880e881208ab16aa16eb520b3e5ea0c63c0a2/README.md
I tested this series on the hardware, the Waveshare
ESP32-P4-Module-DEV-KIT with the ESP32-P4 revision v3.2 with 32MB of
PSRAM. The kernel booted to the UART shell from the initramfs. The
/proc/interrupts showed the CLINT IRQ's increased, UART0 interrupts were
routed through the interrupts matrix.
It would be nice to you all send feedback on restoring the interrupted
CLIC level before irq_exit_rcu() and the interface for propagating
instruction-cache sync failures to NOMMU executable loading, and signal
setup.
AI disclaimer: I have used 6.2-Sol to assist development and to assist
debug/troubleshoot some ongoing issues. I have used Codex's 6-Sol
with Daybreak Blue enabled to generate peer-review-like report to
pinpoint potential security issues regarding the patches. I manually
reviewed and validated each change, including the AI-assisted changes.
I also manually tested each change on the hardware as described above.
Vítor Hugo Silva (19):
riscv: Prepare ESP32-P4 early CLIC trap vectors
riscv: Handle ESP32-P4 CLIC trap context
dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC
irqchip: Add Espressif ESP32-P4 CLIC support
dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt
matrix
irqchip: Add Espressif ESP32-P4 interrupt matrix support
dt-bindings: timer: Add Espressif ESP32-P4 CLINT
clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support
dt-bindings: serial: Add Espressif ESP32-P4 UART
serial: esp32: Restore the ESP32 UART driver
serial: esp32: Add support for the ESP32-P4 UART
serial: esp32: Rearm TX interrupts when the FIFO is full
serial: esp32: Add early console support for ESP32-P4
riscv: Synchronize instructions written to ESP32-P4 PSRAM
riscv: Align init data to the ESP32-P4 PMP granularity
dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT
dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts
riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT
MAINTAINERS: Add the Espressif ESP32-P4 platform
.../esp,esp32p4-clic.yaml | 53 +
.../esp,esp32p4-intmtx.yaml | 67 ++
.../devicetree/bindings/riscv/cpus.yaml | 17 +-
.../devicetree/bindings/riscv/espressif.yaml | 22 +
.../bindings/serial/esp,esp32-uart.yaml | 4 +-
.../bindings/timer/sifive,clint.yaml | 28 +
MAINTAINERS | 14 +
arch/riscv/Kconfig.socs | 9 +
arch/riscv/boot/dts/Makefile | 1 +
arch/riscv/boot/dts/espressif/Makefile | 2 +
.../esp32p4-waveshare-module-dev-kit.dts | 46 +
arch/riscv/boot/dts/espressif/esp32p4.dtsi | 64 ++
arch/riscv/include/asm/asm-prototypes.h | 4 +
arch/riscv/include/asm/cacheflush.h | 6 +
arch/riscv/include/asm/ptrace.h | 4 +
arch/riscv/kernel/asm-offsets.c | 3 +
arch/riscv/kernel/entry.S | 67 ++
arch/riscv/kernel/head.S | 13 +
arch/riscv/kernel/patch.c | 132 ++-
arch/riscv/kernel/signal.c | 6 +-
arch/riscv/kernel/traps.c | 4 +
arch/riscv/kernel/vmlinux.lds.S | 4 +
drivers/clocksource/timer-clint.c | 57 +-
drivers/irqchip/Makefile | 2 +
drivers/irqchip/irq-esp32p4-clic.c | 178 ++++
drivers/irqchip/irq-esp32p4-intmtx.c | 139 +++
drivers/tty/serial/Kconfig | 15 +
drivers/tty/serial/Makefile | 1 +
drivers/tty/serial/esp32_uart.c | 940 ++++++++++++++++++
fs/binfmt_flat.c | 6 +-
fs/exec.c | 10 +-
include/linux/cacheflush.h | 7 +
32 files changed, 1910 insertions(+), 15 deletions(-)
create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
create mode 100644 Documentation/devicetree/bindings/riscv/espressif.yaml
create mode 100644 arch/riscv/boot/dts/espressif/Makefile
create mode 100644 arch/riscv/boot/dts/espressif/esp32p4-waveshare-module-dev-kit.dts
create mode 100644 arch/riscv/boot/dts/espressif/esp32p4.dtsi
create mode 100644 drivers/irqchip/irq-esp32p4-clic.c
create mode 100644 drivers/irqchip/irq-esp32p4-intmtx.c
create mode 100644 drivers/tty/serial/esp32_uart.c
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.56.1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH RFC 09/19] dt-bindings: serial: Add Espressif ESP32-P4 UART
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 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 10/19] serial: esp32: Restore the ESP32 UART driver Vítor Hugo Silva
` (4 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: Vítor Hugo Silva @ 2026-11-25 23:30 UTC (permalink / raw)
To: linux-riscv
Cc: devicetree, linux-kernel, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Greg Kroah-Hartman, Jiri Slaby, linux-serial
Document the ESP32-P4 UART compatible using the existing binding
properties for the register range, interrupt and input clock.
AI assisted with drafting the binding.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
Documentation/devicetree/bindings/serial/esp,esp32-uart.yaml | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/Documentation/devicetree/bindings/serial/esp,esp32-uart.yaml b/Documentation/devicetree/bindings/serial/esp,esp32-uart.yaml
index 2a80ca997a0c..bf141db5e2e5 100644
--- a/Documentation/devicetree/bindings/serial/esp,esp32-uart.yaml
+++ b/Documentation/devicetree/bindings/serial/esp,esp32-uart.yaml
@@ -13,7 +13,8 @@ maintainers:
description:
ESP32 UART controller is a part of the ESP32 SoC.
ESP32S3 UART controller is a part of the ESP32S3 SoC.
- Both SoCs are produced by Espressif Systems Co. Ltd.
+ ESP32-P4 UART controller is a part of the ESP32-P4 SoC.
+ These SoCs are produced by Espressif Systems Co. Ltd.
allOf:
- $ref: serial.yaml#
@@ -22,6 +23,7 @@ properties:
compatible:
enum:
- esp,esp32-uart
+ - esp,esp32p4-uart
- esp,esp32s3-uart
reg:
--
2.56.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH RFC 10/19] serial: esp32: Restore the ESP32 UART driver
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 ` Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-25 23:30 ` [PATCH RFC 11/19] serial: esp32: Add support for the ESP32-P4 UART Vítor Hugo Silva
` (3 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Vítor Hugo Silva @ 2026-11-25 23:30 UTC (permalink / raw)
To: linux-riscv
Cc: devicetree, linux-kernel, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Greg Kroah-Hartman, Jiri Slaby, linux-serial
Restore the UART driver originally added by Max Filippov in commit
8cc89a229aac ("drivers/tty/serial: add driver for the ESP32 UART").
Use the driver source from the parent of commit 2dccde6f5f2f
("serial: remove drivers for espressif esp32").
Keep esp32_uart_console_setup() in regular text because a late probe
can register the console after init memory has been freed.
Make the restored driver available under COMPILE_TEST in preparation
for ESP32-P4 support.
AI assisted with preparing and validating the restoration.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
drivers/tty/serial/Kconfig | 13 +
drivers/tty/serial/Makefile | 1 +
drivers/tty/serial/esp32_uart.c | 779 ++++++++++++++++++++++++++++++++
3 files changed, 793 insertions(+)
create mode 100644 drivers/tty/serial/esp32_uart.c
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index 3864cef711a6..83a37922fb07 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -97,6 +97,19 @@ config SERIAL_EARLYCON_RISCV_SBI
with "earlycon=sbi" on the kernel command line. The console is
enabled when early_param is processed.
+config SERIAL_ESP32
+ tristate "Espressif ESP32 UART support"
+ depends on COMPILE_TEST && OF && HAS_IOMEM
+ select SERIAL_CORE
+ select SERIAL_CORE_CONSOLE
+ select SERIAL_EARLYCON
+ help
+ Driver for the UART controllers of the Espressif ESP32xx SoCs.
+ When earlycon option is enabled the following kernel command line
+ snippets may be used:
+ earlycon=esp32s3uart,mmio32,0x60000000,115200n8,40000000
+ earlycon=esp32uart,mmio32,0x3ff40000,115200n8
+
config SERIAL_SB1250_DUART
tristate "BCM1xxx on-chip DUART serial support"
depends on SIBYTE_SB1xxx_SOC=y
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
index d63b126f40ca..11cb7e3a0f37 100644
--- a/drivers/tty/serial/Makefile
+++ b/drivers/tty/serial/Makefile
@@ -9,6 +9,7 @@ serial_base-y := serial_core.o serial_base_bus.o serial_ctrl.o serial_port.o
obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o
obj-$(CONFIG_SERIAL_EARLYCON_SEMIHOST) += earlycon-semihost.o
obj-$(CONFIG_SERIAL_EARLYCON_RISCV_SBI) += earlycon-riscv-sbi.o
+obj-$(CONFIG_SERIAL_ESP32) += esp32_uart.o
# These Sparc drivers have to appear before others such as 8250
# which share ttySx minor node space. Otherwise console device
diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
new file mode 100644
index 000000000000..8f257cf0d290
--- /dev/null
+++ b/drivers/tty/serial/esp32_uart.c
@@ -0,0 +1,779 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/clk.h>
+#include <linux/console.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/serial_core.h>
+#include <linux/slab.h>
+#include <linux/tty_flip.h>
+#include <asm/serial.h>
+
+#define DRIVER_NAME "esp32-uart"
+#define DEV_NAME "ttyS"
+#define UART_NR 3
+
+#define ESP32_UART_TX_FIFO_SIZE 127
+#define ESP32_UART_RX_FIFO_SIZE 127
+
+#define UART_FIFO_REG 0x00
+#define UART_INT_RAW_REG 0x04
+#define UART_INT_ST_REG 0x08
+#define UART_INT_ENA_REG 0x0c
+#define UART_INT_CLR_REG 0x10
+#define UART_RXFIFO_FULL_INT BIT(0)
+#define UART_TXFIFO_EMPTY_INT BIT(1)
+#define UART_BRK_DET_INT BIT(7)
+#define UART_CLKDIV_REG 0x14
+#define ESP32_UART_CLKDIV GENMASK(19, 0)
+#define ESP32S3_UART_CLKDIV GENMASK(11, 0)
+#define UART_CLKDIV_SHIFT 0
+#define UART_CLKDIV_FRAG GENMASK(23, 20)
+#define UART_STATUS_REG 0x1c
+#define ESP32_UART_RXFIFO_CNT GENMASK(7, 0)
+#define ESP32S3_UART_RXFIFO_CNT GENMASK(9, 0)
+#define UART_RXFIFO_CNT_SHIFT 0
+#define UART_DSRN BIT(13)
+#define UART_CTSN BIT(14)
+#define ESP32_UART_TXFIFO_CNT GENMASK(23, 16)
+#define ESP32S3_UART_TXFIFO_CNT GENMASK(25, 16)
+#define UART_TXFIFO_CNT_SHIFT 16
+#define UART_CONF0_REG 0x20
+#define UART_PARITY BIT(0)
+#define UART_PARITY_EN BIT(1)
+#define UART_BIT_NUM GENMASK(3, 2)
+#define UART_BIT_NUM_5 0
+#define UART_BIT_NUM_6 1
+#define UART_BIT_NUM_7 2
+#define UART_BIT_NUM_8 3
+#define UART_STOP_BIT_NUM GENMASK(5, 4)
+#define UART_STOP_BIT_NUM_1 1
+#define UART_STOP_BIT_NUM_2 3
+#define UART_SW_RTS BIT(6)
+#define UART_SW_DTR BIT(7)
+#define UART_LOOPBACK BIT(14)
+#define UART_TX_FLOW_EN BIT(15)
+#define UART_RTS_INV BIT(23)
+#define UART_DTR_INV BIT(24)
+#define UART_CONF1_REG 0x24
+#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_EN BIT(23)
+#define ESP32S3_UART_RX_FLOW_EN BIT(22)
+#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)
+#define ESP32S3_UART_SCLK_DIV_NUM GENMASK(19, 12)
+#define ESP32S3_UART_SCLK_SEL GENMASK(21, 20)
+#define APB_CLK 1
+#define RC_FAST_CLK 2
+#define XTAL_CLK 3
+#define ESP32S3_UART_SCLK_EN BIT(22)
+#define ESP32S3_UART_RST_CORE BIT(23)
+#define ESP32S3_UART_TX_SCLK_EN BIT(24)
+#define ESP32S3_UART_RX_SCLK_EN BIT(25)
+#define ESP32S3_UART_TX_RST_CORE BIT(26)
+#define ESP32S3_UART_RX_RST_CORE BIT(27)
+
+#define ESP32S3_UART_CLK_CONF_DEFAULT \
+ (ESP32S3_UART_RX_SCLK_EN | \
+ ESP32S3_UART_TX_SCLK_EN | \
+ ESP32S3_UART_SCLK_EN | \
+ FIELD_PREP(ESP32S3_UART_SCLK_SEL, XTAL_CLK))
+
+struct esp32_port {
+ struct uart_port port;
+ struct clk *clk;
+};
+
+struct esp32_uart_variant {
+ u32 clkdiv_mask;
+ u32 rxfifo_cnt_mask;
+ u32 txfifo_cnt_mask;
+ u32 txfifo_empty_thrhd_shift;
+ u32 rx_flow_en;
+ const char *type;
+ bool has_clkconf;
+};
+
+static const struct esp32_uart_variant esp32_variant = {
+ .clkdiv_mask = ESP32_UART_CLKDIV,
+ .rxfifo_cnt_mask = ESP32_UART_RXFIFO_CNT,
+ .txfifo_cnt_mask = ESP32_UART_TXFIFO_CNT,
+ .txfifo_empty_thrhd_shift = ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT,
+ .rx_flow_en = ESP32_UART_RX_FLOW_EN,
+ .type = "ESP32 UART",
+};
+
+static const struct esp32_uart_variant esp32s3_variant = {
+ .clkdiv_mask = ESP32S3_UART_CLKDIV,
+ .rxfifo_cnt_mask = ESP32S3_UART_RXFIFO_CNT,
+ .txfifo_cnt_mask = ESP32S3_UART_TXFIFO_CNT,
+ .txfifo_empty_thrhd_shift = ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT,
+ .rx_flow_en = ESP32S3_UART_RX_FLOW_EN,
+ .type = "ESP32S3 UART",
+ .has_clkconf = true,
+};
+
+static const struct of_device_id esp32_uart_dt_ids[] = {
+ {
+ .compatible = "esp,esp32-uart",
+ .data = &esp32_variant,
+ }, {
+ .compatible = "esp,esp32s3-uart",
+ .data = &esp32s3_variant,
+ }, { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, esp32_uart_dt_ids);
+
+static struct esp32_port *esp32_uart_ports[UART_NR];
+
+static const struct esp32_uart_variant *port_variant(struct uart_port *port)
+{
+ return port->private_data;
+}
+
+static void esp32_uart_write(struct uart_port *port, unsigned long reg, u32 v)
+{
+ writel(v, port->membase + reg);
+}
+
+static u32 esp32_uart_read(struct uart_port *port, unsigned long reg)
+{
+ return readl(port->membase + reg);
+}
+
+static u32 esp32_uart_tx_fifo_cnt(struct uart_port *port)
+{
+ u32 status = esp32_uart_read(port, UART_STATUS_REG);
+
+ return (status & port_variant(port)->txfifo_cnt_mask) >> UART_TXFIFO_CNT_SHIFT;
+}
+
+static u32 esp32_uart_rx_fifo_cnt(struct uart_port *port)
+{
+ u32 status = esp32_uart_read(port, UART_STATUS_REG);
+
+ return (status & port_variant(port)->rxfifo_cnt_mask) >> UART_RXFIFO_CNT_SHIFT;
+}
+
+/* return TIOCSER_TEMT when transmitter is not busy */
+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_mctrl(struct uart_port *port, unsigned int mctrl)
+{
+ u32 conf0 = esp32_uart_read(port, UART_CONF0_REG);
+
+ conf0 &= ~(UART_LOOPBACK |
+ UART_SW_RTS | UART_RTS_INV |
+ UART_SW_DTR | UART_DTR_INV);
+
+ if (mctrl & TIOCM_RTS)
+ conf0 |= UART_SW_RTS;
+ if (mctrl & TIOCM_DTR)
+ conf0 |= UART_SW_DTR;
+ if (mctrl & TIOCM_LOOP)
+ conf0 |= UART_LOOPBACK;
+
+ esp32_uart_write(port, UART_CONF0_REG, conf0);
+}
+
+static unsigned int esp32_uart_get_mctrl(struct uart_port *port)
+{
+ u32 status = esp32_uart_read(port, UART_STATUS_REG);
+ unsigned int ret = TIOCM_CAR;
+
+ if (status & UART_DSRN)
+ ret |= TIOCM_DSR;
+ if (status & UART_CTSN)
+ ret |= TIOCM_CTS;
+
+ return ret;
+}
+
+static void esp32_uart_stop_tx(struct uart_port *port)
+{
+ u32 int_ena;
+
+ int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
+ int_ena &= ~UART_TXFIFO_EMPTY_INT;
+ esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
+}
+
+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 {
+ if (uart_handle_sysrq_char(port, (unsigned char)rx))
+ continue;
+ tty_insert_flip_char(tty_port, rx, TTY_NORMAL);
+ ++port->icount.rx;
+ }
+ }
+ spin_unlock_irqrestore(&port->lock, flags);
+
+ tty_flip_buffer_push(tty_port);
+}
+
+static void esp32_uart_put_char(struct uart_port *port, u8 c)
+{
+ esp32_uart_write(port, UART_FIFO_REG, c);
+}
+
+static void esp32_uart_put_char_sync(struct uart_port *port, u8 c)
+{
+ unsigned long timeout = jiffies + HZ;
+
+ while (esp32_uart_tx_fifo_cnt(port) >= ESP32_UART_TX_FIFO_SIZE) {
+ if (time_after(jiffies, timeout)) {
+ dev_warn(port->dev, "timeout waiting for TX FIFO\n");
+ return;
+ }
+ cpu_relax();
+ }
+ esp32_uart_put_char(port, c);
+}
+
+static void esp32_uart_transmit_buffer(struct uart_port *port)
+{
+ u32 tx_fifo_used = esp32_uart_tx_fifo_cnt(port);
+ unsigned int pending;
+ u8 ch;
+
+ if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
+ return;
+
+ pending = uart_port_tx_limited(port, ch,
+ ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
+ true, esp32_uart_put_char(port, ch),
+ ({}));
+ if (pending) {
+ u32 int_ena;
+
+ int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
+ int_ena |= UART_TXFIFO_EMPTY_INT;
+ esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
+ }
+}
+
+static void esp32_uart_txint(struct uart_port *port)
+{
+ esp32_uart_transmit_buffer(port);
+}
+
+static irqreturn_t esp32_uart_int(int irq, void *dev_id)
+{
+ struct uart_port *port = dev_id;
+ u32 status;
+
+ status = esp32_uart_read(port, UART_INT_ST_REG);
+
+ if (status & (UART_RXFIFO_FULL_INT | UART_BRK_DET_INT))
+ esp32_uart_rxint(port);
+ if (status & UART_TXFIFO_EMPTY_INT)
+ esp32_uart_txint(port);
+
+ esp32_uart_write(port, UART_INT_CLR_REG, status);
+
+ return IRQ_RETVAL(status);
+}
+
+static void esp32_uart_start_tx(struct uart_port *port)
+{
+ esp32_uart_transmit_buffer(port);
+}
+
+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);
+}
+
+static int esp32_uart_startup(struct uart_port *port)
+{
+ int ret = 0;
+ unsigned long flags;
+ struct esp32_port *sport = container_of(port, struct esp32_port, port);
+
+ ret = clk_prepare_enable(sport->clk);
+ if (ret)
+ return ret;
+
+ ret = request_irq(port->irq, esp32_uart_int, 0, DRIVER_NAME, port);
+ if (ret) {
+ clk_disable_unprepare(sport->clk);
+ return ret;
+ }
+
+ spin_lock_irqsave(&port->lock, flags);
+ if (port_variant(port)->has_clkconf)
+ esp32_uart_write(port, ESP32S3_UART_CLK_CONF_REG,
+ ESP32S3_UART_CLK_CONF_DEFAULT);
+ esp32_uart_write(port, UART_CONF1_REG,
+ (1 << UART_RXFIFO_FULL_THRHD_SHIFT) |
+ (1 << port_variant(port)->txfifo_empty_thrhd_shift));
+ esp32_uart_write(port, UART_INT_CLR_REG, UART_RXFIFO_FULL_INT | UART_BRK_DET_INT);
+ esp32_uart_write(port, UART_INT_ENA_REG, UART_RXFIFO_FULL_INT | UART_BRK_DET_INT);
+ spin_unlock_irqrestore(&port->lock, flags);
+
+ return ret;
+}
+
+static void esp32_uart_shutdown(struct uart_port *port)
+{
+ struct esp32_port *sport = container_of(port, struct esp32_port, port);
+
+ esp32_uart_write(port, UART_INT_ENA_REG, 0);
+ free_irq(port->irq, port);
+ clk_disable_unprepare(sport->clk);
+}
+
+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);
+
+ 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);
+
+ conf1 = esp32_uart_read(port, UART_CONF1_REG);
+ conf1 &= ~rx_flow_en;
+
+ if (termios->c_cflag & PARENB) {
+ conf0 |= UART_PARITY_EN;
+ if (termios->c_cflag & PARODD)
+ conf0 |= UART_PARITY;
+ }
+
+ switch (termios->c_cflag & CSIZE) {
+ case CS5:
+ conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_5);
+ break;
+ case CS6:
+ conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_6);
+ break;
+ case CS7:
+ conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_7);
+ break;
+ case CS8:
+ conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_8);
+ break;
+ }
+
+ if (termios->c_cflag & CSTOPB)
+ conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_2);
+ else
+ conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_1);
+
+ 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);
+
+ if (baud) {
+ esp32_uart_set_baud(port, baud);
+ uart_update_timeout(port, termios->c_cflag, baud);
+ } else {
+ if (esp32_uart_set_baud(port, 115200)) {
+ baud = 115200;
+ tty_termios_encode_baud_rate(termios, baud, baud);
+ uart_update_timeout(port, termios->c_cflag, baud);
+ } else {
+ dev_warn(port->dev,
+ "unable to set speed to %d baud or the default 115200\n",
+ baud);
+ }
+ }
+ spin_unlock_irqrestore(&port->lock, flags);
+}
+
+static const char *esp32_uart_type(struct uart_port *port)
+{
+ return port_variant(port)->type;
+}
+
+/* configure/auto-configure the port */
+static void esp32_uart_config_port(struct uart_port *port, int flags)
+{
+ if (flags & UART_CONFIG_TYPE)
+ port->type = PORT_GENERIC;
+}
+
+#ifdef CONFIG_CONSOLE_POLL
+static int esp32_uart_poll_init(struct uart_port *port)
+{
+ struct esp32_port *sport = container_of(port, struct esp32_port, port);
+
+ return clk_prepare_enable(sport->clk);
+}
+
+static void esp32_uart_poll_put_char(struct uart_port *port, unsigned char c)
+{
+ esp32_uart_put_char_sync(port, c);
+}
+
+static int esp32_uart_poll_get_char(struct uart_port *port)
+{
+ if (esp32_uart_rx_fifo_cnt(port))
+ return esp32_uart_read(port, UART_FIFO_REG);
+ else
+ return NO_POLL_CHAR;
+
+}
+#endif
+
+static const struct uart_ops esp32_uart_pops = {
+ .tx_empty = esp32_uart_tx_empty,
+ .set_mctrl = esp32_uart_set_mctrl,
+ .get_mctrl = esp32_uart_get_mctrl,
+ .stop_tx = esp32_uart_stop_tx,
+ .start_tx = esp32_uart_start_tx,
+ .stop_rx = esp32_uart_stop_rx,
+ .startup = esp32_uart_startup,
+ .shutdown = esp32_uart_shutdown,
+ .set_termios = esp32_uart_set_termios,
+ .type = esp32_uart_type,
+ .config_port = esp32_uart_config_port,
+#ifdef CONFIG_CONSOLE_POLL
+ .poll_init = esp32_uart_poll_init,
+ .poll_put_char = esp32_uart_poll_put_char,
+ .poll_get_char = esp32_uart_poll_get_char,
+#endif
+};
+
+static void esp32_uart_console_putchar(struct uart_port *port, u8 c)
+{
+ esp32_uart_put_char_sync(port, c);
+}
+
+static void esp32_uart_string_write(struct uart_port *port, const char *s,
+ unsigned int count)
+{
+ uart_console_write(port, s, count, esp32_uart_console_putchar);
+}
+
+static void
+esp32_uart_console_write(struct console *co, const char *s, unsigned int count)
+{
+ struct esp32_port *sport = esp32_uart_ports[co->index];
+ struct uart_port *port = &sport->port;
+ unsigned long flags;
+ bool locked = true;
+
+ if (port->sysrq)
+ locked = false;
+ else if (oops_in_progress)
+ locked = spin_trylock_irqsave(&port->lock, flags);
+ else
+ spin_lock_irqsave(&port->lock, flags);
+
+ esp32_uart_string_write(port, s, count);
+
+ if (locked)
+ spin_unlock_irqrestore(&port->lock, flags);
+}
+
+static int esp32_uart_console_setup(struct console *co, char *options)
+{
+ struct esp32_port *sport;
+ int baud = 115200;
+ int bits = 8;
+ int parity = 'n';
+ int flow = 'n';
+ int ret;
+
+ /*
+ * check whether an invalid uart number has been specified, and
+ * if so, search for the first available port that does have
+ * console support.
+ */
+ if (co->index == -1 || co->index >= ARRAY_SIZE(esp32_uart_ports))
+ co->index = 0;
+
+ sport = esp32_uart_ports[co->index];
+ if (!sport)
+ return -ENODEV;
+
+ ret = clk_prepare_enable(sport->clk);
+ if (ret)
+ return ret;
+
+ if (options)
+ uart_parse_options(options, &baud, &parity, &bits, &flow);
+
+ return uart_set_options(&sport->port, co, baud, parity, bits, flow);
+}
+
+static int esp32_uart_console_exit(struct console *co)
+{
+ struct esp32_port *sport = esp32_uart_ports[co->index];
+
+ clk_disable_unprepare(sport->clk);
+ return 0;
+}
+
+static struct uart_driver esp32_uart_reg;
+static struct console esp32_uart_console = {
+ .name = DEV_NAME,
+ .write = esp32_uart_console_write,
+ .device = uart_console_device,
+ .setup = esp32_uart_console_setup,
+ .exit = esp32_uart_console_exit,
+ .flags = CON_PRINTBUFFER,
+ .index = -1,
+ .data = &esp32_uart_reg,
+};
+
+static void esp32_uart_earlycon_putchar(struct uart_port *port, u8 c)
+{
+ esp32_uart_put_char_sync(port, c);
+}
+
+static void esp32_uart_earlycon_write(struct console *con, const char *s,
+ unsigned int n)
+{
+ struct earlycon_device *dev = con->data;
+
+ uart_console_write(&dev->port, s, n, esp32_uart_earlycon_putchar);
+}
+
+#ifdef CONFIG_CONSOLE_POLL
+static int esp32_uart_earlycon_read(struct console *con, char *s, unsigned int n)
+{
+ struct earlycon_device *dev = con->data;
+ unsigned int num_read = 0;
+
+ while (num_read < n) {
+ int c = esp32_uart_poll_get_char(&dev->port);
+
+ if (c == NO_POLL_CHAR)
+ break;
+ s[num_read++] = c;
+ }
+ return num_read;
+}
+#endif
+
+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;
+}
+
+static int __init esp32_uart_early_console_setup(struct earlycon_device *device,
+ const char *options)
+{
+ device->port.private_data = (void *)&esp32_variant;
+
+ return esp32xx_uart_early_console_setup(device, options);
+}
+
+OF_EARLYCON_DECLARE(esp32uart, "esp,esp32-uart",
+ esp32_uart_early_console_setup);
+
+static int __init esp32s3_uart_early_console_setup(struct earlycon_device *device,
+ const char *options)
+{
+ device->port.private_data = (void *)&esp32s3_variant;
+
+ return esp32xx_uart_early_console_setup(device, options);
+}
+
+OF_EARLYCON_DECLARE(esp32s3uart, "esp,esp32s3-uart",
+ esp32s3_uart_early_console_setup);
+
+static struct uart_driver esp32_uart_reg = {
+ .owner = THIS_MODULE,
+ .driver_name = DRIVER_NAME,
+ .dev_name = DEV_NAME,
+ .nr = ARRAY_SIZE(esp32_uart_ports),
+ .cons = &esp32_uart_console,
+};
+
+static int esp32_uart_probe(struct platform_device *pdev)
+{
+ struct device_node *np = pdev->dev.of_node;
+ struct uart_port *port;
+ struct esp32_port *sport;
+ struct resource *res;
+ int ret;
+
+ sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
+ if (!sport)
+ return -ENOMEM;
+
+ port = &sport->port;
+
+ ret = of_alias_get_id(np, "serial");
+ if (ret < 0) {
+ dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
+ return ret;
+ }
+ if (ret >= UART_NR) {
+ dev_err(&pdev->dev, "driver limited to %d serial ports\n", UART_NR);
+ return -ENOMEM;
+ }
+
+ port->line = ret;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res)
+ return -ENODEV;
+
+ port->mapbase = res->start;
+ port->membase = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(port->membase))
+ return PTR_ERR(port->membase);
+
+ 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);
+}
+
+static void esp32_uart_remove(struct platform_device *pdev)
+{
+ struct uart_port *port = platform_get_drvdata(pdev);
+
+ uart_remove_one_port(&esp32_uart_reg, port);
+}
+
+
+static struct platform_driver esp32_uart_driver = {
+ .probe = esp32_uart_probe,
+ .remove = esp32_uart_remove,
+ .driver = {
+ .name = DRIVER_NAME,
+ .of_match_table = esp32_uart_dt_ids,
+ },
+};
+
+static int __init esp32_uart_init(void)
+{
+ int ret;
+
+ ret = uart_register_driver(&esp32_uart_reg);
+ if (ret)
+ return ret;
+
+ ret = platform_driver_register(&esp32_uart_driver);
+ if (ret)
+ uart_unregister_driver(&esp32_uart_reg);
+
+ return ret;
+}
+
+static void __exit esp32_uart_exit(void)
+{
+ platform_driver_unregister(&esp32_uart_driver);
+ uart_unregister_driver(&esp32_uart_reg);
+}
+
+module_init(esp32_uart_init);
+module_exit(esp32_uart_exit);
+
+MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
+MODULE_DESCRIPTION("Espressif ESP32 UART support");
+MODULE_LICENSE("GPL");
--
2.56.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH RFC 11/19] serial: esp32: Add support for the ESP32-P4 UART
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-25 23:30 ` 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
` (2 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Vítor Hugo Silva @ 2026-11-25 23:30 UTC (permalink / raw)
To: linux-riscv
Cc: devicetree, linux-kernel, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Greg Kroah-Hartman, Jiri Slaby, linux-serial
Enable the ESP32 UART driver on ESP32-P4 and describe the SoC's
register layout in the variant data. Synchronize configuration
writes to the UART clock domain and keep baud rate requests within
the limits of the ESP32-P4 divider.
Propagate IRQ lookup errors, including deferred probe. Serialize UART
port registration and removal, and clear the port table entry when
registration fails or the port is removed. Keep the console setup
callback available for probes after init memory is freed.
Tested on a Waveshare ESP32-P4-Module-DEV-KIT with the transmit
interrupt fix applied. Verified UART receive interrupts and serial
console operation.
AI assisted with driver development.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
drivers/tty/serial/Kconfig | 6 +-
drivers/tty/serial/esp32_uart.c | 167 ++++++++++++++++++++++++++++----
2 files changed, 151 insertions(+), 22 deletions(-)
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index 83a37922fb07..6d3d3a342101 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -99,12 +99,14 @@ config SERIAL_EARLYCON_RISCV_SBI
config SERIAL_ESP32
tristate "Espressif ESP32 UART support"
- depends on COMPILE_TEST && OF && HAS_IOMEM
+ depends on OF && HAS_IOMEM
+ depends on RISCV_ESP32P4_CLIC || COMPILE_TEST
select SERIAL_CORE
select SERIAL_CORE_CONSOLE
select SERIAL_EARLYCON
help
- Driver for the UART controllers of the Espressif ESP32xx SoCs.
+ Driver for the UART controllers of the Espressif ESP32xx SoCs,
+ including the ESP32-P4.
When earlycon option is enabled the following kernel command line
snippets may be used:
earlycon=esp32s3uart,mmio32,0x60000000,115200n8,40000000
diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
index 8f257cf0d290..304e1345518b 100644
--- a/drivers/tty/serial/esp32_uart.c
+++ b/drivers/tty/serial/esp32_uart.c
@@ -6,10 +6,13 @@
#include <linux/console.h>
#include <linux/delay.h>
#include <linux/io.h>
+#include <linux/iopoll.h>
#include <linux/irq.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/platform_device.h>
+#include <linux/printk.h>
#include <linux/property.h>
#include <linux/serial_core.h>
#include <linux/slab.h>
@@ -89,6 +92,22 @@
ESP32S3_UART_SCLK_EN | \
FIELD_PREP(ESP32S3_UART_SCLK_SEL, XTAL_CLK))
+#define ESP32P4_UART_CLKDIV GENMASK(11, 0)
+#define ESP32P4_UART_RXFIFO_CNT GENMASK(7, 0)
+#define ESP32P4_UART_TXFIFO_CNT GENMASK(23, 16)
+#define ESP32P4_UART_TXFIFO_EMPTY_THRHD_SHIFT 8
+
+/* ESP32-P4 uses a dedicated register for RX flow control. */
+#define ESP32P4_UART_HWFC_CONF_REG 0x2c
+#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_SW_RTS BIT(21)
+#define ESP32P4_UART_SW_DTR BIT(20)
+#define ESP32P4_UART_RTS_INV BIT(18)
+#define ESP32P4_UART_DTR_INV BIT(19)
+
struct esp32_port {
struct uart_port port;
struct clk *clk;
@@ -99,9 +118,16 @@ struct esp32_uart_variant {
u32 rxfifo_cnt_mask;
u32 txfifo_cnt_mask;
u32 txfifo_empty_thrhd_shift;
+ u32 rx_flow_reg;
u32 rx_flow_en;
+ u32 loopback_mask;
+ u32 sw_rts_mask;
+ u32 sw_dtr_reg;
+ u32 sw_dtr_mask;
+ u32 mctrl_inv_mask;
const char *type;
bool has_clkconf;
+ bool needs_reg_update;
};
static const struct esp32_uart_variant esp32_variant = {
@@ -109,8 +135,14 @@ 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,
+ .rx_flow_reg = UART_CONF1_REG,
.rx_flow_en = ESP32_UART_RX_FLOW_EN,
.type = "ESP32 UART",
+ .loopback_mask = UART_LOOPBACK,
+ .sw_rts_mask = UART_SW_RTS,
+ .sw_dtr_reg = UART_CONF0_REG,
+ .sw_dtr_mask = UART_SW_DTR,
+ .mctrl_inv_mask = UART_RTS_INV | UART_DTR_INV,
};
static const struct esp32_uart_variant esp32s3_variant = {
@@ -118,9 +150,31 @@ 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,
+ .rx_flow_reg = UART_CONF1_REG,
.rx_flow_en = ESP32S3_UART_RX_FLOW_EN,
.type = "ESP32S3 UART",
.has_clkconf = true,
+ .loopback_mask = UART_LOOPBACK,
+ .sw_rts_mask = UART_SW_RTS,
+ .sw_dtr_reg = UART_CONF0_REG,
+ .sw_dtr_mask = UART_SW_DTR,
+ .mctrl_inv_mask = UART_RTS_INV | UART_DTR_INV,
+};
+
+static const struct esp32_uart_variant esp32p4_variant = {
+ .clkdiv_mask = ESP32P4_UART_CLKDIV,
+ .rxfifo_cnt_mask = ESP32P4_UART_RXFIFO_CNT,
+ .txfifo_cnt_mask = ESP32P4_UART_TXFIFO_CNT,
+ .txfifo_empty_thrhd_shift = ESP32P4_UART_TXFIFO_EMPTY_THRHD_SHIFT,
+ .rx_flow_reg = ESP32P4_UART_HWFC_CONF_REG,
+ .rx_flow_en = ESP32P4_UART_RX_FLOW_EN,
+ .type = "ESP32-P4 UART",
+ .needs_reg_update = true,
+ .loopback_mask = ESP32P4_UART_LOOPBACK,
+ .sw_rts_mask = ESP32P4_UART_SW_RTS,
+ .sw_dtr_reg = UART_CONF1_REG,
+ .sw_dtr_mask = ESP32P4_UART_SW_DTR,
+ .mctrl_inv_mask = ESP32P4_UART_RTS_INV | ESP32P4_UART_DTR_INV,
};
static const struct of_device_id esp32_uart_dt_ids[] = {
@@ -130,10 +184,14 @@ static const struct of_device_id esp32_uart_dt_ids[] = {
}, {
.compatible = "esp,esp32s3-uart",
.data = &esp32s3_variant,
+ }, {
+ .compatible = "esp,esp32p4-uart",
+ .data = &esp32p4_variant,
}, { /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, esp32_uart_dt_ids);
+static DEFINE_MUTEX(esp32_uart_ports_lock);
static struct esp32_port *esp32_uart_ports[UART_NR];
static const struct esp32_uart_variant *port_variant(struct uart_port *port)
@@ -143,7 +201,24 @@ static const struct esp32_uart_variant *port_variant(struct uart_port *port)
static void esp32_uart_write(struct uart_port *port, unsigned long reg, u32 v)
{
+ void __iomem *update = port->membase + ESP32P4_UART_REG_UPDATE_REG;
+ u32 status;
+
writel(v, port->membase + reg);
+
+ if (reg != UART_CLKDIV_REG && reg != UART_CONF0_REG &&
+ reg != ESP32P4_UART_HWFC_CONF_REG)
+ return;
+
+ if (!port_variant(port)->needs_reg_update)
+ return;
+
+ /* Commit configuration to UART core clock domain. */
+ writel(ESP32P4_UART_REG_UPDATE, update);
+ if (readl_poll_timeout_atomic(update, status,
+ !(status & ESP32P4_UART_REG_UPDATE),
+ 1, 100))
+ printk_deferred_once(DRIVER_NAME ": UART register update timed out\n");
}
static u32 esp32_uart_read(struct uart_port *port, unsigned long reg)
@@ -173,18 +248,29 @@ static unsigned int esp32_uart_tx_empty(struct uart_port *port)
static void esp32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
- u32 conf0 = esp32_uart_read(port, UART_CONF0_REG);
+ const struct esp32_uart_variant *variant = port_variant(port);
+ bool dtr_in_conf0 = variant->sw_dtr_reg == UART_CONF0_REG;
+ u32 conf0, dtr_conf;
- conf0 &= ~(UART_LOOPBACK |
- UART_SW_RTS | UART_RTS_INV |
- UART_SW_DTR | UART_DTR_INV);
+ conf0 = esp32_uart_read(port, UART_CONF0_REG);
+ conf0 &= ~(variant->loopback_mask | variant->sw_rts_mask);
if (mctrl & TIOCM_RTS)
- conf0 |= UART_SW_RTS;
- if (mctrl & TIOCM_DTR)
- conf0 |= UART_SW_DTR;
+ conf0 |= variant->sw_rts_mask;
if (mctrl & TIOCM_LOOP)
- conf0 |= UART_LOOPBACK;
+ conf0 |= variant->loopback_mask;
+
+ /* Software DTR and RTS/DTR inversion share a register. */
+ dtr_conf = dtr_in_conf0 ? conf0 : esp32_uart_read(port, variant->sw_dtr_reg);
+ dtr_conf &= ~(variant->sw_dtr_mask | variant->mctrl_inv_mask);
+
+ if (mctrl & TIOCM_DTR)
+ dtr_conf |= variant->sw_dtr_mask;
+
+ if (dtr_in_conf0)
+ conf0 = dtr_conf;
+ else
+ esp32_uart_write(port, variant->sw_dtr_reg, dtr_conf);
esp32_uart_write(port, UART_CONF0_REG, conf0);
}
@@ -387,13 +473,37 @@ static bool esp32_uart_set_baud(struct uart_port *port, u32 baud)
return false;
}
+static void esp32p4_uart_limit_baud_rate(struct ktermios *termios,
+ const struct ktermios *old,
+ u32 min_baud, u32 max_baud)
+{
+ u32 baud = tty_termios_baud_rate(termios);
+
+ if (!baud || (baud >= min_baud && baud <= max_baud))
+ return;
+
+ if (old) {
+ u32 old_baud = tty_termios_baud_rate(old);
+
+ if (old_baud && old_baud >= min_baud && old_baud <= max_baud)
+ baud = old_baud;
+ }
+
+ baud = clamp(baud, min_baud, max_baud);
+ /* Keep the fallback exact near a standard rate outside the range. */
+ termios->c_cflag &= ~(CBAUD | (CBAUD << IBSHIFT));
+ termios->c_cflag |= BOTHER;
+ tty_termios_encode_baud_rate(termios, baud, baud);
+}
+
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;
+ u32 conf0, flow;
+ 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;
@@ -402,17 +512,19 @@ static void esp32_uart_set_termios(struct uart_port *port,
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);
+ 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);
+ 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);
- conf1 = esp32_uart_read(port, UART_CONF1_REG);
- conf1 &= ~rx_flow_en;
+ flow = esp32_uart_read(port, rx_flow_reg);
+ flow &= ~rx_flow_en;
if (termios->c_cflag & PARENB) {
conf0 |= UART_PARITY_EN;
@@ -441,10 +553,10 @@ static void esp32_uart_set_termios(struct uart_port *port,
conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_1);
if (termios->c_cflag & CRTSCTS)
- conf1 |= rx_flow_en;
+ flow |= rx_flow_en;
esp32_uart_write(port, UART_CONF0_REG, conf0);
- esp32_uart_write(port, UART_CONF1_REG, conf1);
+ esp32_uart_write(port, rx_flow_reg, flow);
if (baud) {
esp32_uart_set_baud(port, baud);
@@ -715,29 +827,44 @@ static int esp32_uart_probe(struct platform_device *pdev)
if (IS_ERR(sport->clk))
return PTR_ERR(sport->clk);
+ ret = platform_get_irq(pdev, 0);
+ if (ret < 0)
+ return ret;
+
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->irq = ret;
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);
+ guard(mutex)(&esp32_uart_ports_lock);
+ if (esp32_uart_ports[port->line])
+ return -EBUSY;
+
+ /* Console setup can look up the port during registration. */
esp32_uart_ports[port->line] = sport;
+ ret = uart_add_one_port(&esp32_uart_reg, port);
+ if (ret) {
+ esp32_uart_ports[port->line] = NULL;
+ return ret;
+ }
platform_set_drvdata(pdev, port);
-
- return uart_add_one_port(&esp32_uart_reg, port);
+ return 0;
}
static void esp32_uart_remove(struct platform_device *pdev)
{
struct uart_port *port = platform_get_drvdata(pdev);
+ guard(mutex)(&esp32_uart_ports_lock);
uart_remove_one_port(&esp32_uart_reg, port);
+ esp32_uart_ports[port->line] = NULL;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH RFC 12/19] serial: esp32: Rearm TX interrupts when the FIFO is full
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (2 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 11/19] serial: esp32: Add support for the ESP32-P4 UART Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
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
5 siblings, 2 replies; 12+ messages in thread
From: Vítor Hugo Silva @ 2026-11-25 23:30 UTC (permalink / raw)
To: linux-riscv
Cc: devicetree, linux-kernel, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Greg Kroah-Hartman, Jiri Slaby, linux-serial
When transmission starts with a full FIFO, the driver can leave
queued data waiting with the TX interrupt disabled. Rearm the
interrupt whenever there is data ready to send, including a pending
high-priority character.
Hold the port lock in the TX interrupt handler as required by
uart_port_tx_limited().
Tested on a Waveshare ESP32-P4-Module-DEV-KIT. Verified that serial
output completed during SDMMC reads.
AI assisted with driver development.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
drivers/tty/serial/esp32_uart.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
index 304e1345518b..002d6fdbd875 100644
--- a/drivers/tty/serial/esp32_uart.c
+++ b/drivers/tty/serial/esp32_uart.c
@@ -354,14 +354,14 @@ static void esp32_uart_transmit_buffer(struct uart_port *port)
unsigned int pending;
u8 ch;
- if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
- return;
-
+ tx_fifo_used = min_t(u32, tx_fifo_used, ESP32_UART_TX_FIFO_SIZE);
pending = uart_port_tx_limited(port, ch,
ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
true, esp32_uart_put_char(port, ch),
({}));
- if (pending) {
+
+ /* Rearm TX even if the FIFO had no room for queued data. */
+ if (port->x_char || (pending && !uart_tx_stopped(port))) {
u32 int_ena;
int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
@@ -372,7 +372,11 @@ static void esp32_uart_transmit_buffer(struct uart_port *port)
static void esp32_uart_txint(struct uart_port *port)
{
+ unsigned long flags;
+
+ uart_port_lock_irqsave(port, &flags);
esp32_uart_transmit_buffer(port);
+ uart_port_unlock_irqrestore(port, flags);
}
static irqreturn_t esp32_uart_int(int irq, void *dev_id)
--
2.56.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH RFC 13/19] serial: esp32: Add early console support for ESP32-P4
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (3 preceding siblings ...)
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-25 23:30 ` Vítor Hugo Silva
2026-11-26 23:32 ` [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 sashiko-bot
5 siblings, 0 replies; 12+ messages in thread
From: Vítor Hugo Silva @ 2026-11-25 23:30 UTC (permalink / raw)
To: linux-riscv
Cc: devicetree, linux-kernel, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Greg Kroah-Hartman, Jiri Slaby, linux-serial
Register an early console for the ESP32-P4 UART using the UART
configuration left by firmware.
Early console writes can run before timer initialization, and
normal console writes can run with local interrupts disabled. Use
a shared polling routine that bounds the wait for TX FIFO space by
a fixed number of reads.
Allow 10,000 reads per character for the ESP32-P4 early console and
1,000,000 for the other callers. Drop the character if the FIFO
remains full for the entire polling budget.
Verified early boot output and a working UART shell on the Waveshare
ESP32-P4-Module-DEV-KIT with the driver built into an RV32 NOMMU kernel.
With a simulated FIFO, both limits dropped the character when the
FIFO stayed full and wrote it when space became available on the
last read.
AI assisted with integrating the early console and checking
its registration in the linked kernel.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
drivers/tty/serial/esp32_uart.c | 50 ++++++++++++++++++++++++++-------
1 file changed, 40 insertions(+), 10 deletions(-)
diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
index 002d6fdbd875..451caeb406fe 100644
--- a/drivers/tty/serial/esp32_uart.c
+++ b/drivers/tty/serial/esp32_uart.c
@@ -25,6 +25,7 @@
#define ESP32_UART_TX_FIFO_SIZE 127
#define ESP32_UART_RX_FIFO_SIZE 127
+#define ESP32_UART_TX_POLLS 1000000
#define UART_FIFO_REG 0x00
#define UART_INT_RAW_REG 0x04
@@ -107,6 +108,7 @@
#define ESP32P4_UART_SW_DTR BIT(20)
#define ESP32P4_UART_RTS_INV BIT(18)
#define ESP32P4_UART_DTR_INV BIT(19)
+#define ESP32P4_EARLYCON_TX_POLLS 10000
struct esp32_port {
struct uart_port port;
@@ -334,18 +336,18 @@ static void esp32_uart_put_char(struct uart_port *port, u8 c)
esp32_uart_write(port, UART_FIFO_REG, c);
}
-static void esp32_uart_put_char_sync(struct uart_port *port, u8 c)
+static void esp32_uart_put_char_sync(struct uart_port *port, u8 c,
+ unsigned int polls)
{
- unsigned long timeout = jiffies + HZ;
-
- while (esp32_uart_tx_fifo_cnt(port) >= ESP32_UART_TX_FIFO_SIZE) {
- if (time_after(jiffies, timeout)) {
- dev_warn(port->dev, "timeout waiting for TX FIFO\n");
+ /* Console writes can run before timer init or with interrupts disabled. */
+ while (polls--) {
+ if (esp32_uart_tx_fifo_cnt(port) < ESP32_UART_TX_FIFO_SIZE) {
+ esp32_uart_put_char(port, c);
return;
}
+
cpu_relax();
}
- esp32_uart_put_char(port, c);
}
static void esp32_uart_transmit_buffer(struct uart_port *port)
@@ -601,7 +603,7 @@ static int esp32_uart_poll_init(struct uart_port *port)
static void esp32_uart_poll_put_char(struct uart_port *port, unsigned char c)
{
- esp32_uart_put_char_sync(port, c);
+ esp32_uart_put_char_sync(port, c, ESP32_UART_TX_POLLS);
}
static int esp32_uart_poll_get_char(struct uart_port *port)
@@ -635,7 +637,7 @@ static const struct uart_ops esp32_uart_pops = {
static void esp32_uart_console_putchar(struct uart_port *port, u8 c)
{
- esp32_uart_put_char_sync(port, c);
+ esp32_uart_put_char_sync(port, c, ESP32_UART_TX_POLLS);
}
static void esp32_uart_string_write(struct uart_port *port, const char *s,
@@ -718,7 +720,7 @@ static struct console esp32_uart_console = {
static void esp32_uart_earlycon_putchar(struct uart_port *port, u8 c)
{
- esp32_uart_put_char_sync(port, c);
+ esp32_uart_put_char_sync(port, c, ESP32_UART_TX_POLLS);
}
static void esp32_uart_earlycon_write(struct console *con, const char *s,
@@ -784,6 +786,34 @@ static int __init esp32s3_uart_early_console_setup(struct earlycon_device *devic
OF_EARLYCON_DECLARE(esp32s3uart, "esp,esp32s3-uart",
esp32s3_uart_early_console_setup);
+static void esp32p4_uart_earlycon_putchar(struct uart_port *port, u8 c)
+{
+ esp32_uart_put_char_sync(port, c, ESP32P4_EARLYCON_TX_POLLS);
+}
+
+static void esp32p4_uart_earlycon_write(struct console *con, const char *s,
+ unsigned int n)
+{
+ struct earlycon_device *dev = con->data;
+
+ uart_console_write(&dev->port, s, n, esp32p4_uart_earlycon_putchar);
+}
+
+static int __init esp32p4_uart_early_console_setup(struct earlycon_device *device,
+ const char *options)
+{
+ if (!device->port.membase)
+ return -ENODEV;
+
+ /* Use the UART configuration left by firmware. */
+ device->port.private_data = (void *)&esp32p4_variant;
+ device->con->write = esp32p4_uart_earlycon_write;
+
+ return 0;
+}
+OF_EARLYCON_DECLARE(esp32p4, "esp,esp32p4-uart",
+ esp32p4_uart_early_console_setup);
+
static struct uart_driver esp32_uart_reg = {
.owner = THIS_MODULE,
.driver_name = DRIVER_NAME,
--
2.56.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH RFC 10/19] serial: esp32: Restore the ESP32 UART driver
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
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-26 23:32 UTC (permalink / raw)
To: Vítor Hugo Silva; +Cc: devicetree, linux-serial, robh, conor+dt
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH RFC 11/19] serial: esp32: Add support for the ESP32-P4 UART
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
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-26 23:32 UTC (permalink / raw)
To: Vítor Hugo Silva; +Cc: devicetree, linux-serial, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] serial: esp32: stale console setup claim in commit message
--
commit c3883508f7c188e23ecf9d3f613c855b10843a75
Author: Vítor Hugo Silva <vitorhg20080@gmail.com>
serial: esp32: Add support for the ESP32-P4 UART
This commit adds ESP32-P4 variant data, clock domain register update
synchronization, baud rate limiting, and probe/remove error handling to
the ESP32 UART driver.
> Propagate IRQ lookup errors, including deferred probe. Serialize UART
> port registration and removal, and clear the port table entry when
> registration fails or the port is removed. Keep the console setup
> callback available for probes after init memory is freed.
[Severity: Low]
This isn't a bug, but the last sentence in the commit message appears to
be stale. esp32_uart_console_setup() is not modified by this patch; its
__init annotation was already dropped in the preceding patch in the
series, commit "serial: esp32: Restore the ESP32 UART driver".
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125233020.1380594-1-vitorhg20080@gmail.com?part=11
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH RFC 12/19] serial: esp32: Rearm TX interrupts when the FIFO is full
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
1 sibling, 1 reply; 12+ messages in thread
From: sashiko-bot @ 2026-11-26 23:32 UTC (permalink / raw)
To: Vítor Hugo Silva; +Cc: devicetree, linux-serial, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] serial: esp32: TX IRQ storm in esp32_uart_transmit_buffer()
after x_char
--
commit dad4b1d831af0fbc408461f76bacc91b367f0e67
Author: Vítor Hugo Silva <vitorhg20080@gmail.com>
serial: esp32: Rearm TX interrupts when the FIFO is full
This commit rearms the TX FIFO empty interrupt in
esp32_uart_transmit_buffer() whenever data or x_char is waiting to be sent
even if the TX FIFO is full, and acquires the port lock in
esp32_uart_txint().
> diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
> index 304e1345518bd..002d6fdbd875f 100644
> --- a/drivers/tty/serial/esp32_uart.c
> +++ b/drivers/tty/serial/esp32_uart.c
> @@ -354,14 +354,14 @@ static void esp32_uart_transmit_buffer(struct uart_port *port)
> unsigned int pending;
> u8 ch;
>
> - if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
> - return;
> -
> + tx_fifo_used = min_t(u32, tx_fifo_used, ESP32_UART_TX_FIFO_SIZE);
> pending = uart_port_tx_limited(port, ch,
> ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
> true, esp32_uart_put_char(port, ch),
> ({}));
> - if (pending) {
> +
> + /* Rearm TX even if the FIFO had no room for queued data. */
> + if (port->x_char || (pending && !uart_tx_stopped(port))) {
> u32 int_ena;
>
> int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
[Severity: Medium]
Can this cause a UART_TXFIFO_EMPTY_INT interrupt storm after a deferred
port->x_char is sent while TX flow is stopped?
If uart_send_xchar() runs when the hardware TX FIFO is full,
uart_tx_stopped(port) is true, and xmit_fifo is non-empty (pending > 0),
uart_port_tx_limited() runs with count == 0 and leaves port->x_char set, so
this check enables UART_TXFIFO_EMPTY_INT.
When the FIFO drains below the empty threshold, esp32_uart_txint() calls
uart_port_tx_limited(), which transmits port->x_char, clears port->x_char
to 0, and breaks out of its loop because uart_tx_stopped(port) is true.
Because pending > 0, __uart_port_tx() does not call port->ops->stop_tx():
include/linux/serial_core.h:__uart_port_tx() {
...
pending = kfifo_len(&__tport->xmit_fifo);
if (pending < WAKEUP_CHARS) {
uart_write_wakeup(__port);
if (!((flags) & UART_TX_NOSTOP) && pending == 0)
__port->ops->stop_tx(__port);
}
...
}
Back in esp32_uart_transmit_buffer(), port->x_char is now 0 and
uart_tx_stopped(port) is true, so nothing disables UART_TXFIFO_EMPTY_INT.
Once that x_char shifts out, UART_TXFIFO_EMPTY_INT fires continuously in a
hardirq storm until TX flow resumes or the port is shut down. Adding an
else branch that calls esp32_uart_stop_tx(port) would ensure the interrupt
is disabled.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125233020.1380594-1-vitorhg20080@gmail.com?part=12
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (4 preceding siblings ...)
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 ` sashiko-bot
5 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-26 23:32 UTC (permalink / raw)
To: Vítor Hugo Silva; +Cc: linux-serial
Sashiko has finished reviewing this 19-patch series:
- Patches 1-7, 9, 13, and 15-18: no issues found.
- Patch 8 ("clocksource/drivers/timer-clint: Add Espressif ESP32..."): 1
potential issue (1 Medium)
- Patch 10 ("serial: esp32: Restore the ESP32 UART driver"): 6 potential
issues (5 Medium, 1 Low)
- Patch 11 ("serial: esp32: Add support for the ESP32-P4 UART"): 1
potential issue (1 Low)
- Patch 12 ("serial: esp32: Rearm TX interrupts when the FIFO is..."): 1
potential issue (1 Medium)
- Patch 14 ("riscv: Synchronize instructions written to ESP32-P4..."): 3
potential issues (3 Medium)
- Patch 19: skipped (not reviewed).
Detailed reviews have been sent as separate replies to patches 8, 10-12,
and 14.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125233020.1380594-1-vitorhg20080@gmail.com
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH RFC 12/19] serial: esp32: Rearm TX interrupts when the FIFO is full
2026-11-26 23:32 ` sashiko-bot
@ 2026-11-27 15:40 ` Vítor Hugo Silva
0 siblings, 0 replies; 12+ messages in thread
From: Vítor Hugo Silva @ 2026-11-27 15:40 UTC (permalink / raw)
To: sashiko-reviews; +Cc: devicetree, linux-serial, robh, conor+dt
On Thu, Nov 26, 2026 at 11:32:18PM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] serial: esp32: TX IRQ storm in esp32_uart_transmit_buffer()
> after x_char
> --
>
> commit dad4b1d831af0fbc408461f76bacc91b367f0e67
> Author: Vítor Hugo Silva <vitorhg20080@gmail.com>
>
> serial: esp32: Rearm TX interrupts when the FIFO is full
>
> This commit rearms the TX FIFO empty interrupt in
> esp32_uart_transmit_buffer() whenever data or x_char is waiting to be sent
> even if the TX FIFO is full, and acquires the port lock in
> esp32_uart_txint().
>
> > diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
> > index 304e1345518bd..002d6fdbd875f 100644
> > --- a/drivers/tty/serial/esp32_uart.c
> > +++ b/drivers/tty/serial/esp32_uart.c
> > @@ -354,14 +354,14 @@ static void esp32_uart_transmit_buffer(struct uart_port *port)
> > unsigned int pending;
> > u8 ch;
> >
> > - if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
> > - return;
> > -
> > + tx_fifo_used = min_t(u32, tx_fifo_used, ESP32_UART_TX_FIFO_SIZE);
> > pending = uart_port_tx_limited(port, ch,
> > ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
> > true, esp32_uart_put_char(port, ch),
> > ({}));
> > - if (pending) {
> > +
> > + /* Rearm TX even if the FIFO had no room for queued data. */
> > + if (port->x_char || (pending && !uart_tx_stopped(port))) {
> > u32 int_ena;
> >
> > int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
>
> [Severity: Medium]
> Can this cause a UART_TXFIFO_EMPTY_INT interrupt storm after a deferred
> port->x_char is sent while TX flow is stopped?
Yes. For v2, I'll use UART_TX_NOSTOP and let
esp32_uart_transmit_buffer() control when the TX IRQ is masked.
After sending x_char while TX is stoppped, driver masks the TX IRQ and
leaves the queued data in place until the TX resumes.
I've tested this and it works with a mocked FIFO and in the hardware
itself, where the serial console stayed normal and responsive during
normal use.
>
> If uart_send_xchar() runs when the hardware TX FIFO is full,
> uart_tx_stopped(port) is true, and xmit_fifo is non-empty (pending > 0),
> uart_port_tx_limited() runs with count == 0 and leaves port->x_char set, so
> this check enables UART_TXFIFO_EMPTY_INT.
>
> When the FIFO drains below the empty threshold, esp32_uart_txint() calls
> uart_port_tx_limited(), which transmits port->x_char, clears port->x_char
> to 0, and breaks out of its loop because uart_tx_stopped(port) is true.
> Because pending > 0, __uart_port_tx() does not call port->ops->stop_tx():
>
> include/linux/serial_core.h:__uart_port_tx() {
> ...
> pending = kfifo_len(&__tport->xmit_fifo);
> if (pending < WAKEUP_CHARS) {
> uart_write_wakeup(__port);
>
> if (!((flags) & UART_TX_NOSTOP) && pending == 0)
> __port->ops->stop_tx(__port);
> }
> ...
> }
>
> Back in esp32_uart_transmit_buffer(), port->x_char is now 0 and
> uart_tx_stopped(port) is true, so nothing disables UART_TXFIFO_EMPTY_INT.
> Once that x_char shifts out, UART_TXFIFO_EMPTY_INT fires continuously in a
> hardirq storm until TX flow resumes or the port is shut down. Adding an
> else branch that calls esp32_uart_stop_tx(port) would ensure the interrupt
> is disabled.
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261125233020.1380594-1-vitorhg20080@gmail.com?part=12
Thanks,
Vítor
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH RFC v2] serial: esp32: Rearm TX interrupts when the FIFO is full
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:56 ` Vítor Hugo Silva
1 sibling, 0 replies; 12+ messages in thread
From: Vítor Hugo Silva @ 2026-11-27 15:56 UTC (permalink / raw)
To: linux-serial
Cc: gregkh, jirislaby, linux-riscv, sashiko-reviews,
Vítor Hugo Silva
When transmission starts with a full FIFO, queued data can be left
waiting with the TX interrupt disabled. Keep the TX FIFO empty
interrupt enabled while a high-priority character is pending or while
transmission can progress on queued data.
Use UART_TX_NOSTOP so the driver controls TX interrupt masking. If a
deferred x_char is sent while normal transmission is stopped, mask the
TX interrupt and leave the queued data for the next start_tx() call.
This prevents repeated TX interrupts while the queue is stopped.
Hold the port lock in the TX interrupt handler as required by the
serial core TX helpers.
A mocked FIFO test verified the deferred x_char case, IRQ masking and
TX restart. Normal serial console operation was tested on the
Waveshare ESP32-P4-Module-DEV-KIT.
AI assisted with driver development.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
Changes in v2:
- Keep TX IRQ masking in the driver and mask the IRQ after sending a
deferred x_char while TX is stopped.
- Verify the stopped-TX sequence with a mocked FIFO and run a serial
console regression test on the ESP32-P4.
This replaces patch 12/19 and applies after patches 10 and 11 from v1.
v1: https://lore.kernel.org/linux-serial/20261125233020.1380594-13-vitorhg20080@gmail.com/
drivers/tty/serial/esp32_uart.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/drivers/tty/serial/esp32_uart.c b/drivers/tty/serial/esp32_uart.c
index 304e134..2058bd6 100644
--- a/drivers/tty/serial/esp32_uart.c
+++ b/drivers/tty/serial/esp32_uart.c
@@ -354,25 +354,31 @@ static void esp32_uart_transmit_buffer(struct uart_port *port)
unsigned int pending;
u8 ch;
- if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
- return;
-
- pending = uart_port_tx_limited(port, ch,
- ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
- true, esp32_uart_put_char(port, ch),
- ({}));
- if (pending) {
+ tx_fifo_used = min_t(u32, tx_fifo_used, ESP32_UART_TX_FIFO_SIZE);
+ pending = uart_port_tx_limited_flags(port, ch, UART_TX_NOSTOP,
+ ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
+ true, esp32_uart_put_char(port, ch),
+ ({}));
+
+ /* Keep TX IRQs enabled while there is sendable data. */
+ if (port->x_char || (pending && !uart_tx_stopped(port))) {
u32 int_ena;
int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
int_ena |= UART_TXFIFO_EMPTY_INT;
esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
+ } else {
+ esp32_uart_stop_tx(port);
}
}
static void esp32_uart_txint(struct uart_port *port)
{
+ unsigned long flags;
+
+ uart_port_lock_irqsave(port, &flags);
esp32_uart_transmit_buffer(port);
+ uart_port_unlock_irqrestore(port, flags);
}
static irqreturn_t esp32_uart_int(int irq, void *dev_id)
^ permalink raw reply related [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-11-27 15:56 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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-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-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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox