* [PATCH RFC 01/19] riscv: Prepare ESP32-P4 early CLIC trap vectors
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 02/19] riscv: Handle ESP32-P4 CLIC trap context Vítor Hugo Silva
` (17 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti
The ESP32-P4 HP cores run with mtvec.MODE fixed to CLIC. The legacy
mie and mip CSRs cannot mask CLIC interrupts, and mtvec.BASE requires
64-byte alignment.
Add an opt-in configuration for RV32, single-hart M-mode. Mask local
interrupts through mstatus.MIE at kernel entry. Align the early park,
PMP probe and normal exception vectors to 64 bytes.
AI assisted with the trap entry changes and elaborate the help
description.
Link: https://documentation.espressif.com/esp32-p4_technical_reference_manual_en.pdf
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
arch/riscv/Kconfig.socs | 9 +++++++++
arch/riscv/kernel/entry.S | 6 ++++++
arch/riscv/kernel/head.S | 13 +++++++++++++
3 files changed, 28 insertions(+)
diff --git a/arch/riscv/Kconfig.socs b/arch/riscv/Kconfig.socs
index cecbce0c4983..0272923c7d88 100644
--- a/arch/riscv/Kconfig.socs
+++ b/arch/riscv/Kconfig.socs
@@ -12,6 +12,15 @@ config ARCH_ANLOGIC
help
This enables support for Anlogic SoC platform hardware.
+config RISCV_ESP32P4_CLIC
+ bool "Espressif ESP32-P4 early CLIC setup"
+ depends on ARCH_RV32I && RISCV_M_MODE && !SMP
+ help
+ Enable early machine-mode trap setup for a single ESP32-P4 hart.
+ This aligns the trap vectors to 64 bytes as required by the CLIC.
+ During kernel entry, it clears mstatus.MIE to mask interrupts,
+ since the legacy mie and mip CSRs do not control the CLIC.
+
config ARCH_ESWIN
bool "ESWIN SoCs"
help
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index 61b7a7716697..f659bf62bb39 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -125,7 +125,13 @@
CONFIG_RISCV_USER_CFI)
.endm
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* mtvec.BASE starts at bit 6 in CLIC mode. */
+ .balign 64
+SYM_CODE_START_NOALIGN(handle_exception)
+#else
SYM_CODE_START(handle_exception)
+#endif
/*
* If coming from userspace, preserve the user thread pointer and load
* the kernel thread pointer. If we came from the kernel, the scratch
diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S
index 1631fd6bc786..f266c4238b2f 100644
--- a/arch/riscv/kernel/head.S
+++ b/arch/riscv/kernel/head.S
@@ -183,7 +183,11 @@ secondary_start_sbi:
call smp_callin
#endif /* CONFIG_SMP */
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+.balign 64
+#else
.align 2
+#endif
.Lsecondary_park:
/*
* Park this hart if we:
@@ -210,9 +214,14 @@ secondary_start_sbi:
SYM_CODE_END(_start)
SYM_CODE_START(_start_kernel)
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* The CLIC uses mstatus.MIE to mask interrupts globally. */
+ csrci CSR_STATUS, SR_IE
+#else
/* Mask all interrupts */
csrw CSR_IE, zero
csrw CSR_IP, zero
+#endif
#ifdef CONFIG_RISCV_M_MODE
/* flush the instruction cache */
@@ -233,7 +242,11 @@ SYM_CODE_START(_start_kernel)
csrw CSR_PMPADDR0, a0
li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X)
csrw CSR_PMPCFG0, a0
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+.balign 64
+#else
.align 2
+#endif
.Lpmp_done:
/*
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 02/19] riscv: Handle ESP32-P4 CLIC trap context
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 01/19] riscv: Prepare ESP32-P4 early CLIC trap vectors Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 03/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC Vítor Hugo Silva
` (16 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Oleg Nesterov
The ESP32-P4 CLIC encodes trap return state alongside the cause
code and interrupt flag in mcause. The RISC-V exception dispatcher
expects only the cause code and interrupt flag.
Save the raw mcause in pt_regs, normalize the cause for dispatch
and translate MPP and MPIE into the saved status layout. Restore
the saved interrupt level and rebuild MPP and MPIE from pt_regs
on the non-vectored trap return path.
The current CLIC level stays active until mret. irq_exit_rcu() can
run softirqs with interrupts enabled, and irqentry_exit() can
schedule before the final mret. Holding the hardirq level across
either path blocks interrupts at that level or below.
Restore the interrupted context's level with an M-mode mret
after the hardirq handler and before irq_exit_rcu(). Keep interrupts
masked across this intermediate return and retain the original
trap frame.
Tested on a Waveshare ESP32-P4-Module-DEV-KIT with revision v3.2
silicon. The kernel booted to a UART shell, completed 20 consecutive
two-process pipelines and continued to receive CLINT timer and UART0
interrupts.
AI assisted with the assembly and JTAG diagnosis.
Link: https://documentation.espressif.com/esp32-p4_technical_reference_manual_en.pdf
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
arch/riscv/include/asm/asm-prototypes.h | 4 ++
arch/riscv/include/asm/ptrace.h | 4 ++
arch/riscv/kernel/asm-offsets.c | 3 ++
arch/riscv/kernel/entry.S | 61 +++++++++++++++++++++++++
arch/riscv/kernel/traps.c | 4 ++
5 files changed, 76 insertions(+)
diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/asm/asm-prototypes.h
index a0ca9efff267..e0fec71022f9 100644
--- a/arch/riscv/include/asm/asm-prototypes.h
+++ b/arch/riscv/include/asm/asm-prototypes.h
@@ -64,4 +64,8 @@ asmlinkage void handle_bad_stack(struct pt_regs *regs);
asmlinkage void do_page_fault(struct pt_regs *regs);
asmlinkage void do_irq(struct pt_regs *regs);
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+asmlinkage void esp32p4_clic_restore_irq_level(unsigned long cause);
+#endif
+
#endif /* _ASM_RISCV_PROTOTYPES_H */
diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
index addc8188152f..5729fbac0b57 100644
--- a/arch/riscv/include/asm/ptrace.h
+++ b/arch/riscv/include/asm/ptrace.h
@@ -53,6 +53,10 @@ struct pt_regs {
unsigned long cause;
/* a0 value before the syscall */
unsigned long orig_a0;
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* Raw mcause for CLIC trap return */
+ unsigned long clic_mcause;
+#endif
};
#define PTRACE_SYSEMU 0x1f
diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c
index a75f0cfea1e9..9f8310dd0a6e 100644
--- a/arch/riscv/kernel/asm-offsets.c
+++ b/arch/riscv/kernel/asm-offsets.c
@@ -130,6 +130,9 @@ void asm_offsets(void)
OFFSET(PT_STATUS, pt_regs, status);
OFFSET(PT_BADADDR, pt_regs, badaddr);
OFFSET(PT_CAUSE, pt_regs, cause);
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ OFFSET(PT_CLIC_MCAUSE, pt_regs, clic_mcause);
+#endif
OFFSET(SUSPEND_CONTEXT_REGS, suspend_context, regs);
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index f659bf62bb39..91269ab82c60 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -196,6 +196,22 @@ SYM_CODE_START(handle_exception)
csrr s2, CSR_EPC
csrr s3, CSR_TVAL
csrr s4, CSR_CAUSE
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ REG_S s4, PT_CLIC_MCAUSE(sp)
+ /* Translate CLIC MPP and MPIE into the saved mstatus layout. */
+ li t0, ~(SR_MPP | SR_MPIE)
+ and s1, s1, t0
+ srli t0, s4, 28 - 11
+ li t1, SR_MPP
+ and t0, t0, t1
+ or s1, s1, t0
+ srli t0, s4, 27 - 7
+ andi t0, t0, SR_MPIE
+ or s1, s1, t0
+ /* Keep the interrupt flag and the six bit CLIC cause code. */
+ li t0, CAUSE_IRQ_FLAG | 0x3f
+ and s4, s4, t0
+#endif
csrr s5, CSR_SCRATCH
REG_S s0, PT_SP(sp)
REG_S s1, PT_STATUS(sp)
@@ -311,6 +327,25 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
csrw CSR_STATUS, a0
csrw CSR_EPC, a2
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /*
+ * Preserve INT, MPIL and CODE from the saved trap.
+ * Clear MINHV so mret uses mepc as an instruction address.
+ */
+ REG_L a3, PT_CLIC_MCAUSE(sp)
+ li a4, CAUSE_IRQ_FLAG | (0xff << 16) | 0x3f
+ and a3, a3, a4
+
+ /* Rebuild MPP and MPIE from the current pt_regs status. */
+ li a4, SR_MPP
+ and a4, a0, a4
+ slli a4, a4, 28 - 11
+ or a3, a3, a4
+ andi a4, a0, SR_MPIE
+ slli a4, a4, 27 - 7
+ or a3, a3, a4
+ csrw CSR_CAUSE, a3
+#endif
REG_L x1, PT_RA(sp)
REG_L x3, PT_GP(sp)
@@ -329,6 +364,32 @@ SYM_INNER_LABEL(ret_from_exception_end, SYM_L_GLOBAL)
SYM_CODE_END(ret_from_exception)
ASM_NOKPROBE(ret_from_exception)
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+/* Restore MPIL from the IRQ frame without leaving M-mode. */
+SYM_FUNC_START(esp32p4_clic_restore_irq_level)
+ csrr t0, CSR_CAUSE
+ csrr t1, CSR_EPC
+ csrrci t2, CSR_STATUS, SR_IE
+
+ li t3, CAUSE_IRQ_FLAG | (0xff << 16) | 0x3f
+ and a0, a0, t3
+ /* MPP=M and MPIE=0 keep interrupts disabled on the synthetic return. */
+ li t3, 3 << 28
+ or a0, a0, t3
+ csrw CSR_CAUSE, a0
+ lla a0, .Lclic_irq_level_restored
+ csrw CSR_EPC, a0
+ mret
+
+.Lclic_irq_level_restored:
+ csrw CSR_CAUSE, t0
+ csrw CSR_EPC, t1
+ csrw CSR_STATUS, t2
+ ret
+SYM_FUNC_END(esp32p4_clic_restore_irq_level)
+ASM_NOKPROBE(esp32p4_clic_restore_irq_level)
+#endif
+
#ifdef CONFIG_VMAP_STACK
SYM_CODE_START_LOCAL(handle_kernel_stack_overflow)
/* we reach here from kernel context, sscratch must be 0 */
diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index f8292adda099..28343a601969 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -428,6 +428,10 @@ static void noinstr handle_riscv_irq(struct pt_regs *regs)
old_regs = set_irq_regs(regs);
handle_arch_irq(regs);
set_irq_regs(old_regs);
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* Restore the interrupted level before irq_exit_rcu() can run softirqs. */
+ esp32p4_clic_restore_irq_level(regs->clic_mcause);
+#endif
irq_exit_rcu();
}
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 03/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC
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 01/19] riscv: Prepare ESP32-P4 early CLIC trap vectors Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 02/19] riscv: Handle ESP32-P4 CLIC trap context Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 04/19] irqchip: Add Espressif ESP32-P4 CLIC support Vítor Hugo Silva
` (15 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Thomas Gleixner, Radu Rendec
Describe the memory-mapped core-local interrupt controller on the ESP32-P4.
Define its two-cell interrupt specifier as the CLIC interrupt number and
trigger type.
AI assisted with drafting the schema.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
.../esp,esp32p4-clic.yaml | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
diff --git a/Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml b/Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
new file mode 100644
index 000000000000..2ef36e4e4019
--- /dev/null
+++ b/Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/interrupt-controller/esp,esp32p4-clic.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Espressif ESP32-P4 Core-Local Interrupt Controller
+
+maintainers:
+ - Vítor Hugo Silva <vitorhg20080@gmail.com>
+
+description:
+ Each high-performance RISC-V core has a CLIC that receives the local
+ software and timer interrupts and interrupts from the interrupt matrix.
+
+properties:
+ compatible:
+ enum:
+ - esp,esp32p4-clic
+ - esp,esp32p4-v3-clic
+
+ reg:
+ maxItems: 1
+
+ interrupt-controller: true
+
+ '#interrupt-cells':
+ const: 2
+ description:
+ The first cell is the CLIC interrupt number. Interrupts 3 and 7 are
+ the local machine-mode software and timer interrupts, respectively.
+ Interrupts 16 through 47 are inputs from the interrupt matrix.
+
+ The second cell is the trigger type defined in
+ dt-bindings/interrupt-controller/irq.h. Use IRQ_TYPE_LEVEL_HIGH for
+ the local CLINT interrupts and for signals from the interrupt matrix.
+
+required:
+ - compatible
+ - reg
+ - interrupt-controller
+ - '#interrupt-cells'
+
+additionalProperties: false
+
+examples:
+ - |
+ interrupt-controller@20800000 {
+ compatible = "esp,esp32p4-clic";
+ reg = <0x20800000 0x10000>;
+ interrupt-controller;
+ #interrupt-cells = <2>;
+ };
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 04/19] irqchip: Add Espressif ESP32-P4 CLIC support
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 03/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 05/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt matrix Vítor Hugo Silva
` (14 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Thomas Gleixner, Radu Rendec
Register a hierarchical IRQ domain for the ESP32-P4 CLIC. Handle
per-CPU software and timer interrupts and support active-high level
triggering on the 32 external interrupt inputs.
Tested on the Waveshare ESP32-P4-Module-DEV-KIT hardware.
Verified that the CLIC handled local timer interrupts from the
CLINT and external interrupts routed through the interrupt matrix.
AI assisted with driver development, validation and testing.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
drivers/irqchip/Makefile | 1 +
drivers/irqchip/irq-esp32p4-clic.c | 178 +++++++++++++++++++++++++++++
2 files changed, 179 insertions(+)
create mode 100644 drivers/irqchip/irq-esp32p4-clic.c
diff --git a/drivers/irqchip/Makefile b/drivers/irqchip/Makefile
index 16a5fd126c9a..408d06c20ab8 100644
--- a/drivers/irqchip/Makefile
+++ b/drivers/irqchip/Makefile
@@ -102,6 +102,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o
obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o
obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o
obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o
+obj-$(CONFIG_RISCV_ESP32P4_CLIC) += irq-esp32p4-clic.o
obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic-main.o irq-riscv-aplic-direct.o
obj-$(CONFIG_RISCV_APLIC_MSI) += irq-riscv-aplic-msi.o
obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic-state.o irq-riscv-imsic-early.o irq-riscv-imsic-platform.o
diff --git a/drivers/irqchip/irq-esp32p4-clic.c b/drivers/irqchip/irq-esp32p4-clic.c
new file mode 100644
index 000000000000..f9235f230d37
--- /dev/null
+++ b/drivers/irqchip/irq-esp32p4-clic.c
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Espressif ESP32-P4 Core-Local Interrupt Controller
+ *
+ * Author: Vítor Hugo Silva <vitorhg20080@gmail.com>
+ * ESP32-P4 Technical Reference Manual, section 2.9.2 (CLIC)
+ * https://documentation.espressif.com/esp32-p4_technical_reference_manual_en.pdf
+ */
+#include <linux/bits.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/irqchip.h>
+#include <linux/irqdomain.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/printk.h>
+
+#include <asm/csr.h>
+#include <asm/ptrace.h>
+
+#define CLIC_FIRST_EXT_IRQ 16
+#define CLIC_NR_IRQS 48
+#define CLIC_INTTHRESH 0x0008
+#define CLIC_INTIE(id) (0x1001 + 4 * (id))
+#define CLIC_INTATTR(id) (0x1002 + 4 * (id))
+#define CLIC_INTCTL(id) (0x1003 + 4 * (id))
+#define CLIC_INTATTR_MODE_M GENMASK(7, 6)
+#define CLIC_INTCTL_MAX GENMASK(7, 5)
+
+#define CSR_MINTTHRESH 0x347
+
+static struct irq_domain *esp32p4_clic_domain __ro_after_init;
+
+static void esp32p4_clic_irq_mask(struct irq_data *d)
+{
+ void __iomem *base = irq_data_get_irq_chip_data(d);
+
+ writeb(0, base + CLIC_INTIE(d->hwirq));
+}
+
+static void esp32p4_clic_irq_unmask(struct irq_data *d)
+{
+ void __iomem *base = irq_data_get_irq_chip_data(d);
+
+ writeb(1, base + CLIC_INTIE(d->hwirq));
+}
+
+static int esp32p4_clic_irq_set_type(struct irq_data *d, unsigned int type)
+{
+ if (type != IRQ_TYPE_LEVEL_HIGH)
+ return -EINVAL;
+
+ return IRQ_SET_MASK_OK;
+}
+
+static struct irq_chip esp32p4_clic_chip = {
+ .name = "ESP32-P4 CLIC",
+ .irq_mask = esp32p4_clic_irq_mask,
+ .irq_unmask = esp32p4_clic_irq_unmask,
+ .irq_set_type = esp32p4_clic_irq_set_type,
+};
+
+static int esp32p4_clic_domain_alloc(struct irq_domain *domain,
+ unsigned int virq, unsigned int nr_irqs,
+ void *arg)
+{
+ struct irq_fwspec *fwspec = arg;
+ irq_flow_handler_t handler;
+ irq_hw_number_t hwirq;
+ unsigned int type;
+ int ret;
+
+ if (nr_irqs != 1 || !fwspec)
+ return -EINVAL;
+
+ ret = irq_domain_translate_twocell(domain, fwspec, &hwirq, &type);
+ if (ret)
+ return ret;
+
+ if (type != IRQ_TYPE_LEVEL_HIGH)
+ return -EINVAL;
+
+ if (hwirq == IRQ_M_SOFT || hwirq == IRQ_M_TIMER) {
+ ret = irq_set_percpu_devid(virq);
+ if (ret)
+ return ret;
+ handler = handle_percpu_devid_irq;
+ } else {
+ if (hwirq < CLIC_FIRST_EXT_IRQ || hwirq >= CLIC_NR_IRQS)
+ return -EINVAL;
+ handler = handle_level_irq;
+ }
+
+ irq_domain_set_info(domain, virq, hwirq, &esp32p4_clic_chip,
+ domain->host_data, handler, NULL, NULL);
+
+ return 0;
+}
+
+static const struct irq_domain_ops esp32p4_clic_domain_ops = {
+ .alloc = esp32p4_clic_domain_alloc,
+ .free = irq_domain_free_irqs_top,
+ .translate = irq_domain_translate_twocell,
+};
+
+static void esp32p4_clic_handle_irq(struct pt_regs *regs)
+{
+ unsigned long hwirq = regs->cause & ~CAUSE_IRQ_FLAG;
+
+ if (generic_handle_domain_irq(esp32p4_clic_domain, hwirq))
+ pr_warn_ratelimited("ESP32-P4 CLIC: unhandled IRQ %lu\n", hwirq);
+}
+
+static void __init esp32p4_clic_init_irq(void __iomem *base, unsigned int hwirq)
+{
+ writeb(0, base + CLIC_INTIE(hwirq));
+
+ /* Use non-vectored, active-high level interrupts in M-mode. */
+ writeb(CLIC_INTATTR_MODE_M, base + CLIC_INTATTR(hwirq));
+
+ /* Use one level for all IRQs regardless of the inherited MNLBITS. */
+ writeb(CLIC_INTCTL_MAX, base + CLIC_INTCTL(hwirq));
+}
+
+static int __init esp32p4_clic_init(struct device_node *node,
+ struct device_node *parent)
+{
+ struct irq_domain *domain;
+ void __iomem *base;
+ unsigned int hwirq;
+ int ret;
+
+ base = of_iomap(node, 0);
+ if (!base)
+ return -ENOMEM;
+
+ domain = irq_domain_create_hierarchy(NULL, 0, CLIC_NR_IRQS,
+ of_fwnode_handle(node),
+ &esp32p4_clic_domain_ops, base);
+ if (!domain) {
+ ret = -ENOMEM;
+ goto err_unmap;
+ }
+
+ ret = set_handle_irq(esp32p4_clic_handle_irq);
+ if (ret)
+ goto err_remove_domain;
+
+ esp32p4_clic_init_irq(base, IRQ_M_SOFT);
+ esp32p4_clic_init_irq(base, IRQ_M_TIMER);
+ for (hwirq = CLIC_FIRST_EXT_IRQ; hwirq < CLIC_NR_IRQS; hwirq++)
+ esp32p4_clic_init_irq(base, hwirq);
+
+ /* Clear the threshold inherited from firmware. */
+ if (of_device_is_compatible(node, "esp,esp32p4-v3-clic")) {
+ csr_write(CSR_MINTTHRESH, 0);
+ } else {
+ writel(0, base + CLIC_INTTHRESH);
+ /* Ensure the new threshold is active before enabling IRQs. */
+ readl(base + CLIC_INTTHRESH);
+ }
+
+ esp32p4_clic_domain = domain;
+ pr_info("%pOF: ESP32-P4 CLIC registered\n", node);
+
+ return 0;
+
+err_remove_domain:
+ irq_domain_remove(domain);
+err_unmap:
+ iounmap(base);
+ return ret;
+}
+
+IRQCHIP_DECLARE(esp32p4_clic, "esp,esp32p4-clic", esp32p4_clic_init);
+IRQCHIP_DECLARE(esp32p4_v3_clic, "esp,esp32p4-v3-clic", esp32p4_clic_init);
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 05/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt matrix
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 04/19] irqchip: Add Espressif ESP32-P4 CLIC support Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 06/19] irqchip: Add Espressif ESP32-P4 interrupt matrix support Vítor Hugo Silva
` (13 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Thomas Gleixner, Radu Rendec
Describe the interrupt matrix that routes peripheral interrupt sources
to the CLIC. Define the interrupt parent and the two-cell specifier
for the peripheral source number and trigger type.
Tested on a Waveshare ESP32-P4-Module-DEV-KIT.
AI assisted with drafting and validating the binding.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
.../esp,esp32p4-intmtx.yaml | 67 +++++++++++++++++++
1 file changed, 67 insertions(+)
create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
diff --git a/Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml b/Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
new file mode 100644
index 000000000000..20f36c4f45da
--- /dev/null
+++ b/Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
@@ -0,0 +1,67 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/interrupt-controller/esp,esp32p4-intmtx.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Espressif ESP32-P4 High-Performance Interrupt Matrix
+
+maintainers:
+ - Vítor Hugo Silva <vitorhg20080@gmail.com>
+
+description:
+ The high-performance interrupt matrix routes peripheral interrupt sources
+ to the external inputs of a high-performance core's CLIC. The matrix can
+ independently select the destination input for each peripheral source.
+ ESP32-P4 revision 3.x adds three interrupt routing registers.
+
+properties:
+ compatible:
+ enum:
+ - esp,esp32p4-intmtx
+ - esp,esp32p4-v3-intmtx
+
+ reg:
+ maxItems: 1
+
+ interrupt-controller: true
+
+ '#interrupt-cells':
+ const: 2
+ description:
+ The first cell is the peripheral interrupt source number in the matrix.
+ The second cell is the trigger type as defined in
+ dt-bindings/interrupt-controller/irq.h.
+
+ '#address-cells':
+ const: 0
+
+ interrupt-parent:
+ description: Phandle to the CLIC that receives interrupts from this matrix.
+
+required:
+ - compatible
+ - reg
+ - interrupt-controller
+ - '#interrupt-cells'
+ - interrupt-parent
+
+additionalProperties: false
+
+examples:
+ - |
+ clic: interrupt-controller@20800000 {
+ compatible = "esp,esp32p4-clic";
+ reg = <0x20800000 0x10000>;
+ interrupt-controller;
+ #interrupt-cells = <2>;
+ };
+
+ interrupt-controller@500d6000 {
+ compatible = "esp,esp32p4-intmtx";
+ reg = <0x500d6000 0x400>;
+ interrupt-controller;
+ #interrupt-cells = <2>;
+ #address-cells = <0>;
+ interrupt-parent = <&clic>;
+ };
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 06/19] irqchip: Add Espressif ESP32-P4 interrupt matrix support
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 05/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt matrix Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 07/19] dt-bindings: timer: Add Espressif ESP32-P4 CLINT Vítor Hugo Silva
` (12 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Thomas Gleixner, Radu Rendec
Register a child IRQ domain for the ESP32-P4 interrupt matrix.
Allocate a CLIC external interrupt input for each peripheral interrupt
and program the route when the interrupt is activated. Support
active-high level interrupts for source numbers 0 through 127.
Disconnect firmware routes before registering the IRQ domain. On
ESP32-P4 revision 3.x, also clear the three additional routing registers.
Tested on a Waveshare ESP32-P4-Module-DEV-KIT. Verified that peripheral
interrupts were delivered to the CLIC through the matrix.
AI assisted with driver development and validation.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
drivers/irqchip/Makefile | 1 +
drivers/irqchip/irq-esp32p4-intmtx.c | 139 +++++++++++++++++++++++++++
2 files changed, 140 insertions(+)
create mode 100644 drivers/irqchip/irq-esp32p4-intmtx.c
diff --git a/drivers/irqchip/Makefile b/drivers/irqchip/Makefile
index 408d06c20ab8..772d9558bbc8 100644
--- a/drivers/irqchip/Makefile
+++ b/drivers/irqchip/Makefile
@@ -103,6 +103,7 @@ obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o
obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o
obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o
obj-$(CONFIG_RISCV_ESP32P4_CLIC) += irq-esp32p4-clic.o
+obj-$(CONFIG_RISCV_ESP32P4_CLIC) += irq-esp32p4-intmtx.o
obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic-main.o irq-riscv-aplic-direct.o
obj-$(CONFIG_RISCV_APLIC_MSI) += irq-riscv-aplic-msi.o
obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic-state.o irq-riscv-imsic-early.o irq-riscv-imsic-platform.o
diff --git a/drivers/irqchip/irq-esp32p4-intmtx.c b/drivers/irqchip/irq-esp32p4-intmtx.c
new file mode 100644
index 000000000000..0fc1a7b78f8f
--- /dev/null
+++ b/drivers/irqchip/irq-esp32p4-intmtx.c
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Espressif ESP32-P4 interrupt matrix */
+
+#include <linux/bits.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/irqchip.h>
+#include <linux/irqdomain.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+
+#define CLIC_FIRST_EXT_IRQ 16
+#define CLIC_LAST_EXT_IRQ 47
+#define INTMTX_NR_MAP_REGS 128
+#define INTMTX_MAP_MASK GENMASK(5, 0)
+#define INTMTX_MAP_DISABLED 0
+#define INTMTX_V3_FIRST_EXTRA_SOURCE 133
+#define INTMTX_V3_LAST_EXTRA_SOURCE 135
+
+static const struct irq_chip esp32p4_intmtx_chip = {
+ .name = "ESP32-P4 interrupt matrix",
+ .irq_mask = irq_chip_mask_parent,
+ .irq_unmask = irq_chip_unmask_parent,
+ .irq_set_type = irq_chip_set_type_parent,
+};
+
+static int esp32p4_intmtx_domain_alloc(struct irq_domain *domain,
+ unsigned int virq, unsigned int nr_irqs,
+ void *arg)
+{
+ struct irq_fwspec *fwspec = arg;
+ struct irq_fwspec parent_fwspec = {
+ .fwnode = domain->parent->fwnode,
+ .param_count = 2,
+ .param[1] = IRQ_TYPE_LEVEL_HIGH,
+ };
+ unsigned int clic_irq;
+ int ret;
+
+ if (nr_irqs != 1 || !fwspec || fwspec->param_count != 2)
+ return -EINVAL;
+ if (fwspec->param[0] >= INTMTX_NR_MAP_REGS ||
+ fwspec->param[1] != IRQ_TYPE_LEVEL_HIGH)
+ return -EINVAL;
+
+ /* The IRQ domain core serializes allocation across the hierarchy. */
+ for (clic_irq = CLIC_FIRST_EXT_IRQ; clic_irq <= CLIC_LAST_EXT_IRQ;
+ clic_irq++) {
+ if (!irq_find_mapping(domain->parent, clic_irq))
+ break;
+ }
+ if (clic_irq > CLIC_LAST_EXT_IRQ)
+ return -ENOSPC;
+
+ ret = irq_domain_set_hwirq_and_chip(domain, virq, fwspec->param[0],
+ &esp32p4_intmtx_chip, NULL);
+ if (ret)
+ return ret;
+
+ parent_fwspec.param[0] = clic_irq;
+ return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &parent_fwspec);
+}
+
+static void esp32p4_intmtx_set_route(void __iomem *base, unsigned int source,
+ unsigned int clic_irq)
+{
+ void __iomem *reg = base + 4 * source;
+ u32 val = readl(reg);
+
+ writel((val & ~INTMTX_MAP_MASK) | clic_irq, reg);
+}
+
+static int esp32p4_intmtx_activate(struct irq_domain *domain,
+ struct irq_data *data, bool reserve)
+{
+ esp32p4_intmtx_set_route(domain->host_data, irqd_to_hwirq(data),
+ irqd_to_hwirq(data->parent_data));
+ return 0;
+}
+
+static void esp32p4_intmtx_deactivate(struct irq_domain *domain,
+ struct irq_data *data)
+{
+ esp32p4_intmtx_set_route(domain->host_data, irqd_to_hwirq(data),
+ INTMTX_MAP_DISABLED);
+}
+
+static const struct irq_domain_ops esp32p4_intmtx_domain_ops = {
+ .alloc = esp32p4_intmtx_domain_alloc,
+ .free = irq_domain_free_irqs_common,
+ .translate = irq_domain_translate_twocell,
+ .activate = esp32p4_intmtx_activate,
+ .deactivate = esp32p4_intmtx_deactivate,
+};
+
+static int __init esp32p4_intmtx_init(struct device_node *node,
+ struct device_node *parent)
+{
+ struct irq_domain *parent_domain, *domain;
+ void __iomem *base;
+ unsigned int source;
+
+ if (!parent)
+ return -ENODEV;
+
+ parent_domain = irq_find_host(parent);
+ if (!parent_domain)
+ return -ENODEV;
+
+ base = of_iomap(node, 0);
+ if (!base)
+ return -ENOMEM;
+
+ /* Disconnect routes left by firmware before allocating IRQs. */
+ for (source = 0; source < INTMTX_NR_MAP_REGS; source++)
+ esp32p4_intmtx_set_route(base, source, INTMTX_MAP_DISABLED);
+
+ /* Skip the status and clock registers between routing banks. */
+ if (of_device_is_compatible(node, "esp,esp32p4-v3-intmtx")) {
+ for (source = INTMTX_V3_FIRST_EXTRA_SOURCE;
+ source <= INTMTX_V3_LAST_EXTRA_SOURCE; source++)
+ esp32p4_intmtx_set_route(base, source, INTMTX_MAP_DISABLED);
+ }
+
+ domain = irq_domain_create_hierarchy(parent_domain, 0, 0,
+ of_fwnode_handle(node),
+ &esp32p4_intmtx_domain_ops, base);
+ if (!domain) {
+ iounmap(base);
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
+IRQCHIP_DECLARE(esp32p4_intmtx, "esp,esp32p4-intmtx", esp32p4_intmtx_init);
+IRQCHIP_DECLARE(esp32p4_v3_intmtx, "esp,esp32p4-v3-intmtx", esp32p4_intmtx_init);
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 07/19] dt-bindings: timer: Add Espressif ESP32-P4 CLINT
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (5 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 06/19] irqchip: Add Espressif ESP32-P4 interrupt matrix support Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 08/19] clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support Vítor Hugo Silva
` (11 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Daniel Lezcano, Thomas Gleixner, Paul Walmsley,
Samuel Holland
Document the ESP32-P4 CLINT compatible and its machine-mode software
and timer interrupts routed to the CLIC. Add an example showing both
interrupt specifiers.
AI assisted with drafting the binding.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
.../bindings/timer/sifive,clint.yaml | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/Documentation/devicetree/bindings/timer/sifive,clint.yaml b/Documentation/devicetree/bindings/timer/sifive,clint.yaml
index d53f52e80d88..e6d08a4ff702 100644
--- a/Documentation/devicetree/bindings/timer/sifive,clint.yaml
+++ b/Documentation/devicetree/bindings/timer/sifive,clint.yaml
@@ -27,6 +27,7 @@ description:
properties:
compatible:
oneOf:
+ - const: esp,esp32p4-clint
- items:
- enum:
- canaan,k210-clint # Canaan Kendryte K210
@@ -74,6 +75,17 @@ properties:
minItems: 1
maxItems: 4095
+if:
+ properties:
+ compatible:
+ const: esp,esp32p4-clint
+then:
+ properties:
+ interrupts-extended:
+ items:
+ - description: Machine-mode software interrupt
+ - description: Machine-mode timer interrupt
+
additionalProperties: false
required:
@@ -91,4 +103,20 @@ examples:
<&cpu4intc 3>, <&cpu4intc 7>;
reg = <0x2000000 0x10000>;
};
+ - |
+ #include <dt-bindings/interrupt-controller/irq.h>
+
+ timer@20000000 {
+ compatible = "esp,esp32p4-clint";
+ reg = <0x20000000 0x10000>;
+ interrupts-extended = <&clic 3 IRQ_TYPE_LEVEL_HIGH>,
+ <&clic 7 IRQ_TYPE_LEVEL_HIGH>;
+ };
+
+ clic: interrupt-controller@20800000 {
+ compatible = "esp,esp32p4-clic";
+ reg = <0x20800000 0x10000>;
+ interrupt-controller;
+ #interrupt-cells = <2>;
+ };
...
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 08/19] clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (6 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 07/19] dt-bindings: timer: Add Espressif ESP32-P4 CLINT Vítor Hugo Silva
@ 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
` (10 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Daniel Lezcano, Thomas Gleixner
The ESP32-P4 delivers machine-mode timer interrupts through the CLIC.
Select the ESP32-P4 handling by compatible string, parse the two-cell
interrupt specifier and clear the timer interrupt through mtimecmp.
Program mtimecmp with three 32-bit writes to prevent an intermediate
compare value from triggering an early interrupt on RV32.
Enable the ESP32-P4 mtime counter before registering the clocksource
so it can be used even if firmware stopped it.
Tested on a Waveshare ESP32-P4-Module-DEV-KIT. Verified CLINT
clocksource selection and delivery of timer interrupts.
AI assisted with driver development.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
drivers/clocksource/timer-clint.c | 57 ++++++++++++++++++++++++++++---
1 file changed, 53 insertions(+), 4 deletions(-)
diff --git a/drivers/clocksource/timer-clint.c b/drivers/clocksource/timer-clint.c
index e56eee7e3781..bf5bae077d7a 100644
--- a/drivers/clocksource/timer-clint.c
+++ b/drivers/clocksource/timer-clint.c
@@ -20,6 +20,7 @@
#include <linux/irq.h>
#include <linux/irqchip/chained_irq.h>
#include <linux/irqdomain.h>
+#include <linux/limits.h>
#include <linux/of_irq.h>
#include <linux/smp.h>
#include <linux/timex.h>
@@ -31,6 +32,8 @@
#define CLINT_IPI_OFF 0
#define CLINT_TIMER_CMP_OFF 0x4000
#define CLINT_TIMER_VAL_OFF 0xbff8
+#define ESP32P4_CLINT_MTIME_CTL_OFF 0x4010
+#define ESP32P4_CLINT_MTIME_EN BIT(0)
/* CLINT manages IPI and Timer for RISC-V M-mode */
static u32 __iomem *clint_ipi_base;
@@ -39,6 +42,7 @@ static u64 __iomem *clint_timer_cmp;
static u64 __iomem *clint_timer_val;
static unsigned long clint_timer_freq;
static unsigned int clint_timer_irq;
+static bool clint_uses_clic __ro_after_init;
#ifdef CONFIG_RISCV_M_MODE
u64 __iomem *clint_time_val;
@@ -119,6 +123,29 @@ static int clint_clock_next_event(unsigned long delta,
return 0;
}
+static void esp32p4_clint_write_cmp(u64 value)
+{
+ u32 __iomem *cmp = (u32 __iomem *)clint_timer_cmp;
+
+ /* Keep the intermediate RV32 compare values from expiring early. */
+ writel_relaxed(U32_MAX, cmp);
+ writel_relaxed((u32)(value >> 32), cmp + 1);
+ writel_relaxed((u32)value, cmp);
+}
+
+static int esp32p4_clint_next_event(unsigned long delta,
+ struct clock_event_device *ce)
+{
+ esp32p4_clint_write_cmp(clint_get_cycles64() + delta);
+ return 0;
+}
+
+static int esp32p4_clint_shutdown(struct clock_event_device *ce)
+{
+ esp32p4_clint_write_cmp(U64_MAX);
+ return 0;
+}
+
static DEFINE_PER_CPU(struct clock_event_device, clint_clock_event) = {
.name = "clint_clockevent",
.features = CLOCK_EVT_FEAT_ONESHOT,
@@ -130,13 +157,22 @@ static int clint_timer_starting_cpu(unsigned int cpu)
{
struct clock_event_device *ce = per_cpu_ptr(&clint_clock_event, cpu);
+ if (clint_uses_clic) {
+ ce->set_next_event = esp32p4_clint_next_event;
+ ce->set_state_shutdown = esp32p4_clint_shutdown;
+ ce->set_state_oneshot_stopped = esp32p4_clint_shutdown;
+ esp32p4_clint_shutdown(ce);
+ }
+
ce->cpumask = cpumask_of(cpu);
clockevents_config_and_register(ce, clint_timer_freq, 100, ULONG_MAX);
enable_percpu_irq(clint_timer_irq,
irq_get_trigger_type(clint_timer_irq));
- enable_percpu_irq(clint_ipi_irq,
- irq_get_trigger_type(clint_ipi_irq));
+ if (!clint_uses_clic)
+ enable_percpu_irq(clint_ipi_irq,
+ irq_get_trigger_type(clint_ipi_irq));
+
return 0;
}
@@ -155,7 +191,10 @@ static irqreturn_t clint_timer_interrupt(int irq, void *dev_id)
{
struct clock_event_device *evdev = this_cpu_ptr(&clint_clock_event);
- csr_clear(CSR_IE, IE_TIE);
+ if (clint_uses_clic)
+ esp32p4_clint_shutdown(evdev);
+ else
+ csr_clear(CSR_IE, IE_TIE);
evdev->event_handler(evdev);
return IRQ_HANDLED;
@@ -163,6 +202,7 @@ static irqreturn_t clint_timer_interrupt(int irq, void *dev_id)
static int __init clint_timer_init_dt(struct device_node *np)
{
+ bool esp32p4 = of_device_is_compatible(np, "esp,esp32p4-clint");
int rc;
u32 i, nr_irqs;
void __iomem *base;
@@ -179,7 +219,7 @@ static int __init clint_timer_init_dt(struct device_node *np)
continue;
}
- if ((oirq.args_count != 1) ||
+ if (oirq.args_count != (esp32p4 ? 2 : 1) ||
(oirq.args[0] != RV_IRQ_TIMER &&
oirq.args[0] != RV_IRQ_SOFT)) {
pr_err("%pOFP: invalid irq %d (hwirq %d)\n",
@@ -212,10 +252,18 @@ static int __init clint_timer_init_dt(struct device_node *np)
return -ENODEV;
}
+ if (esp32p4) {
+ void __iomem *ctl = base + ESP32P4_CLINT_MTIME_CTL_OFF;
+ u32 val = readl(ctl);
+
+ writel(val | ESP32P4_CLINT_MTIME_EN, ctl);
+ }
+
clint_ipi_base = base + CLINT_IPI_OFF;
clint_timer_cmp = base + CLINT_TIMER_CMP_OFF;
clint_timer_val = base + CLINT_TIMER_VAL_OFF;
clint_timer_freq = riscv_timebase;
+ clint_uses_clic = esp32p4;
#ifdef CONFIG_RISCV_M_MODE
/*
@@ -275,3 +323,4 @@ fail_iounmap:
TIMER_OF_DECLARE(clint_timer, "riscv,clint0", clint_timer_init_dt);
TIMER_OF_DECLARE(clint_timer1, "sifive,clint0", clint_timer_init_dt);
+TIMER_OF_DECLARE(esp32p4_clint, "esp,esp32p4-clint", clint_timer_init_dt);
--
2.56.1
^ permalink raw reply [flat|nested] 20+ 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
` (7 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 08/19] clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support 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
` (9 subsequent siblings)
18 siblings, 0 replies; 20+ 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 [flat|nested] 20+ 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
` (8 preceding siblings ...)
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-25 23:30 ` [PATCH RFC 11/19] serial: esp32: Add support for the ESP32-P4 UART Vítor Hugo Silva
` (8 subsequent siblings)
18 siblings, 0 replies; 20+ 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 [flat|nested] 20+ 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
` (9 preceding siblings ...)
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-25 23:30 ` [PATCH RFC 12/19] serial: esp32: Rearm TX interrupts when the FIFO is full Vítor Hugo Silva
` (7 subsequent siblings)
18 siblings, 0 replies; 20+ 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 [flat|nested] 20+ 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
` (10 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-25 23:30 ` [PATCH RFC 13/19] serial: esp32: Add early console support for ESP32-P4 Vítor Hugo Silva
` (6 subsequent siblings)
18 siblings, 0 replies; 20+ 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 [flat|nested] 20+ 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
` (11 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-25 23:30 ` [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM Vítor Hugo Silva
` (5 subsequent siblings)
18 siblings, 0 replies; 20+ 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 [flat|nested] 20+ messages in thread* [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (12 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-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 15/19] riscv: Align init data to the ESP32-P4 PMP granularity Vítor Hugo Silva
` (4 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Alexander Viro, Christian Brauner, Jan Kara,
Kees Cook, David Hildenbrand, linux-fsdevel, linux-mm
On ESP32-P4, a load can observe new instruction bytes in the data
cache while the instruction fetch path still sees old bytes. The
runtime constant patching code can then execute stale instructions
and use an invalid address during boot.
Write back the L1 data and L2 caches for the modified PSRAM range,
invalidate the local L1 instruction cache for that range and issue
fence.i before executing the modified instructions. Apply the same
sequence to NOMMU userspace code, including FLAT binaries and the
signal return trampoline.
Split the aligned range into 32 KiB chunks and restore the caller's
IRQ state after each chunk. In instrumented measurements on the
Waveshare board, the longest observed IRQ-disabled window fell from
2.69 ms with 128 KiB chunks to 1.13 ms with 32 KiB chunks.
Bound cache synchronization by a fixed number of reads per command.
After a timeout, stop issuing commands because the controller may
still be busy. Propagate failures through executable loading and
signal setup. Treat a failure to synchronize patched kernel text as
fatal.
The ESP32-P4 board probe observed stale instructions after fence.i
alone. Combining data writeback and instruction cache invalidation
made the modified instructions visible.
Verified boot to the UART shell on a Waveshare ESP32-P4-Module-DEV-KIT
with SDMMC disabled in Linux. The shell handled SIGUSR1 and resumed
execution. With CLIC disabled, the RV32 NOMMU patch.o and signal.o
matched the reference byte for byte.
AI assisted with isolating the cache synchronization change from
the rest of the ESP32-P4 port.
Link: https://docs.espressif.com/projects/esp-idf/en/stable/esp32p4/api-reference/system/mm_sync.html
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
arch/riscv/include/asm/cacheflush.h | 6 ++
arch/riscv/kernel/patch.c | 132 +++++++++++++++++++++++++++-
arch/riscv/kernel/signal.c | 6 +-
fs/binfmt_flat.c | 6 +-
fs/exec.c | 10 ++-
include/linux/cacheflush.h | 7 ++
6 files changed, 160 insertions(+), 7 deletions(-)
diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h
index c2b0a2928f06..a5c20d7d1c9d 100644
--- a/arch/riscv/include/asm/cacheflush.h
+++ b/arch/riscv/include/asm/cacheflush.h
@@ -85,6 +85,12 @@ static inline void flush_icache_range(unsigned long start, unsigned long end)
flush_icache_all();
}
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+int esp32p4_sync_user_text(unsigned long start, unsigned long end);
+
+#define flush_icache_user_range_checked esp32p4_sync_user_text
+#endif
+
extern unsigned int riscv_cbom_block_size;
extern unsigned int riscv_cboz_block_size;
extern unsigned int riscv_cbop_block_size;
diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
index 2b5adf01c550..e0687c943358 100644
--- a/arch/riscv/kernel/patch.c
+++ b/arch/riscv/kernel/patch.c
@@ -3,6 +3,13 @@
* Copyright (C) 2020 SiFive
*/
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+#include <linux/align.h>
+#include <linux/io.h>
+#include <linux/irqflags.h>
+#include <linux/panic.h>
+#include <linux/sizes.h>
+#endif
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/memory.h>
@@ -173,9 +180,132 @@ static int __patch_insn_set(void *addr, u8 c, size_t len)
}
NOKPROBE_SYMBOL(__patch_insn_set);
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+#define ESP32P4_CACHE_BASE 0x3ff10000
+#define ESP32P4_CACHE_SYNC_CTRL 0x98
+#define ESP32P4_CACHE_SYNC_MAP 0x9c
+#define ESP32P4_CACHE_SYNC_ADDR 0xa0
+#define ESP32P4_CACHE_SYNC_SIZE 0xa4
+
+#define ESP32P4_CACHE_INVALIDATE BIT(0)
+#define ESP32P4_CACHE_WRITEBACK BIT(2)
+#define ESP32P4_CACHE_SYNC_DONE BIT(4)
+#define ESP32P4_CACHE_SYNC_MAX_POLLS 1000000
+
+#define ESP32P4_CACHE_L1_I0 BIT(0)
+#define ESP32P4_CACHE_L1_D BIT(4)
+#define ESP32P4_CACHE_L2 BIT(5)
+
+#define ESP32P4_PSRAM_START 0x48000000UL
+#define ESP32P4_PSRAM_END 0x4c000000UL
+#define ESP32P4_CACHE_MAX_LINE 128
+
+static int esp32p4_cache_sync(u32 map, u32 start, u32 size, u32 op,
+ unsigned int count)
+{
+ /* The caller holds local IRQs disabled on the single supported hart. */
+ static bool sync_failed;
+ void __iomem *base = (void __iomem *)ESP32P4_CACHE_BASE;
+ unsigned int polls;
+
+ if (sync_failed)
+ return -EIO;
+
+ writel(map, base + ESP32P4_CACHE_SYNC_MAP);
+ writel(start, base + ESP32P4_CACHE_SYNC_ADDR);
+ writel(size, base + ESP32P4_CACHE_SYNC_SIZE);
+
+ while (count--) {
+ writel(op, base + ESP32P4_CACHE_SYNC_CTRL);
+ /* Order the sync command before reading its completion status. */
+ mb();
+ for (polls = 0; polls < ESP32P4_CACHE_SYNC_MAX_POLLS; polls++) {
+ if (readl(base + ESP32P4_CACHE_SYNC_CTRL) & ESP32P4_CACHE_SYNC_DONE)
+ break;
+ cpu_relax();
+ }
+ if (polls == ESP32P4_CACHE_SYNC_MAX_POLLS) {
+ sync_failed = true;
+ return -ETIMEDOUT;
+ }
+ }
+
+ return 0;
+}
+
+static int esp32p4_cache_writeback(u32 start, u32 size)
+{
+ int ret;
+
+ /* Repeat writeback to avoid losing a sync with conflicting writes. */
+ ret = esp32p4_cache_sync(ESP32P4_CACHE_L1_D, start, size, ESP32P4_CACHE_WRITEBACK, 2);
+ if (ret)
+ return ret;
+
+ return esp32p4_cache_sync(ESP32P4_CACHE_L2, start, size, ESP32P4_CACHE_WRITEBACK, 2);
+}
+
+static int esp32p4_sync_text(void *addr, size_t len, bool fatal)
+{
+ unsigned long start = (unsigned long)addr;
+ unsigned long first, end, flags;
+
+ if (start < ESP32P4_PSRAM_START || start >= ESP32P4_PSRAM_END ||
+ !len || len > ESP32P4_PSRAM_END - start)
+ return 0;
+
+ first = ALIGN_DOWN(start, ESP32P4_CACHE_MAX_LINE);
+ end = ALIGN(start + len, ESP32P4_CACHE_MAX_LINE);
+
+ /* Allow interrupts between chunks if they were enabled on entry. */
+ while (first < end) {
+ u32 size = min_t(u32, end - first, SZ_32K);
+ int ret;
+
+ local_irq_save(flags);
+ ret = esp32p4_cache_writeback(first, size);
+ if (!ret)
+ ret = esp32p4_cache_sync(ESP32P4_CACHE_L1_I0, first, size,
+ ESP32P4_CACHE_INVALIDATE, 1);
+ if (ret) {
+ if (fatal)
+ panic("ESP32-P4 text cache sync failed: %d\n", ret);
+ local_irq_restore(flags);
+ return ret;
+ }
+
+ local_flush_icache_all();
+ local_irq_restore(flags);
+ first += size;
+ }
+
+ return 0;
+}
+
+int esp32p4_sync_user_text(unsigned long start, unsigned long end)
+{
+ int ret;
+
+ if (end > start) {
+ ret = esp32p4_sync_text((void *)start, end - start, false);
+ if (ret)
+ return ret;
+ }
+
+ flush_icache_range(start, end);
+ return 0;
+}
+#endif
+
static int __patch_insn_write(void *addr, const void *insn, size_t len)
{
- return copy_to_kernel_nofault(addr, insn, len);
+ int ret = copy_to_kernel_nofault(addr, insn, len);
+
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ if (!ret)
+ esp32p4_sync_text(addr, len, true);
+#endif
+ return ret;
}
NOKPROBE_SYMBOL(__patch_insn_write);
#endif /* CONFIG_MMU */
diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c
index 59784dc117e4..05266d7c1466 100644
--- a/arch/riscv/kernel/signal.c
+++ b/arch/riscv/kernel/signal.c
@@ -7,6 +7,7 @@
*/
#include <linux/compat.h>
+#include <linux/cacheflush.h>
#include <linux/signal.h>
#include <linux/uaccess.h>
#include <linux/syscalls.h>
@@ -21,7 +22,6 @@
#include <asm/switch_to.h>
#include <asm/vector.h>
#include <asm/csr.h>
-#include <asm/cacheflush.h>
#include <asm/usercfi.h>
unsigned long signal_minsigstksz __ro_after_init;
@@ -453,7 +453,9 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
addr = (unsigned long)&frame->sigreturn_code;
/* Make sure the two instructions are pushed to icache. */
- flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
+ err = flush_icache_user_range_checked(addr, addr + sizeof(frame->sigreturn_code));
+ if (err)
+ return err;
regs->ra = addr;
#endif /* CONFIG_MMU */
diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
index b5b5ca1a44f7..890a81d805dc 100644
--- a/fs/binfmt_flat.c
+++ b/fs/binfmt_flat.c
@@ -32,6 +32,7 @@
#include <linux/user.h>
#include <linux/slab.h>
#include <linux/binfmts.h>
+#include <linux/cacheflush.h>
#include <linux/personality.h>
#include <linux/init.h>
#include <linux/flat.h>
@@ -40,7 +41,6 @@
#include <asm/byteorder.h>
#include <linux/unaligned.h>
-#include <asm/cacheflush.h>
#include <asm/page.h>
#include <asm/flat.h>
@@ -830,7 +830,9 @@ static int load_flat_file(struct linux_binprm *bprm,
#endif /* CONFIG_BINFMT_FLAT_OLD */
}
- flush_icache_user_range(start_code, end_code);
+ ret = flush_icache_user_range_checked(start_code, end_code);
+ if (ret)
+ return ret;
/* zero the BSS, BRK and stack areas */
if (clear_user((void __user *)(datapos + data_len), bss_len +
diff --git a/fs/exec.c b/fs/exec.c
index 8d2b14e1d455..9066d2bebd1a 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -23,6 +23,7 @@
* formats.
*/
+#include <linux/cacheflush.h>
#include <linux/kernel_read_file.h>
#include <linux/slab.h>
#include <linux/file.h>
@@ -827,8 +828,13 @@ EXPORT_SYMBOL(open_exec);
ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
{
ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
- if (res > 0)
- flush_icache_user_range(addr, addr + len);
+
+ if (res > 0) {
+ int ret = flush_icache_user_range_checked(addr, addr + len);
+
+ if (ret)
+ return ret;
+ }
return res;
}
EXPORT_SYMBOL(read_code);
diff --git a/include/linux/cacheflush.h b/include/linux/cacheflush.h
index 55f297b2c23f..6a7d6f425737 100644
--- a/include/linux/cacheflush.h
+++ b/include/linux/cacheflush.h
@@ -26,4 +26,11 @@ static inline void flush_icache_pages(struct vm_area_struct *vma,
#define flush_icache_page(vma, page) flush_icache_pages(vma, page, 1)
+#ifndef flush_icache_user_range_checked
+#define flush_icache_user_range_checked(start, end) ({ \
+ flush_icache_user_range((start), (end)); \
+ 0; \
+})
+#endif
+
#endif /* _LINUX_CACHEFLUSH_H */
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 15/19] riscv: Align init data to the ESP32-P4 PMP granularity
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (13 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 16/19] dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT Vítor Hugo Silva
` (3 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti
ESP32-P4 has a PMP granularity of 128 bytes. The RISC-V linker
script can place __init_data_begin at a smaller alignment, so a boot
stage using that symbol to separate executable code from writable
data cannot always encode the boundary exactly.
Align __init_data_begin to 128 bytes on ESP32-P4 so PMP can apply
distinct permissions to the two regions.
Verified the alignment in an RV32 NOMMU kernel that booted to the
UART shell on a Waveshare ESP32-P4-Module-DEV-KIT.
AI assisted with preparing the patch and validating the
linker layout.
Link: https://github.com/espressif/esp-idf/blob/master/components/soc/esp32p4/include/soc/soc_caps.h#L191
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
arch/riscv/kernel/vmlinux.lds.S | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index 1f4f8496941a..e4498a9fdc6c 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -77,6 +77,10 @@ SECTIONS
#ifdef CONFIG_EFI
. = ALIGN(PECOFF_SECTION_ALIGNMENT);
__pecoff_text_end = .;
+#endif
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* Keep init data outside the last executable PMP granule. */
+ . = ALIGN(SZ_128);
#endif
/* Start of init data section */
__init_data_begin = .;
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 16/19] dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (14 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 15/19] riscv: Align init data to the ESP32-P4 PMP granularity Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 17/19] dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts Vítor Hugo Silva
` (2 subsequent siblings)
18 siblings, 0 replies; 20+ 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, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti
Document the root compatible for the Waveshare ESP32-P4-Module-DEV-KIT
with the Espressif ESP32-P4 SoC compatible as its fallback.
AI assisted with writing and validating the schema.
Link: https://docs.waveshare.com/ESP32-P4-Module-DEV-KIT
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
.../devicetree/bindings/riscv/espressif.yaml | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
create mode 100644 Documentation/devicetree/bindings/riscv/espressif.yaml
diff --git a/Documentation/devicetree/bindings/riscv/espressif.yaml b/Documentation/devicetree/bindings/riscv/espressif.yaml
new file mode 100644
index 000000000000..564e93cd5f30
--- /dev/null
+++ b/Documentation/devicetree/bindings/riscv/espressif.yaml
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/riscv/espressif.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Espressif ESP32-P4-based boards
+
+maintainers:
+ - Vítor Hugo Silva <vitorhg20080@gmail.com>
+
+properties:
+ $nodename:
+ const: '/'
+ compatible:
+ items:
+ - const: waveshare,esp32-p4-module-dev-kit
+ - const: esp,esp32p4
+
+additionalProperties: true
+
+...
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 17/19] dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (15 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 16/19] dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 18/19] riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 19/19] MAINTAINERS: Add the Espressif ESP32-P4 platform Vítor Hugo Silva
18 siblings, 0 replies; 20+ 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, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti
Add esp,esp32p4-hp as a compatible for the ESP32-P4 high-performance
harts, with riscv as the fallback.
These harts receive interrupts through the memory-mapped CLIC, which
has its own binding and node. Disallow the CSR-based interrupt
controller child for ESP32-P4 HP harts and keep requiring it for
other RISC-V CPUs.
AI assisted with writing and validating the schema change.
Link: https://documentation.espressif.com/esp32-p4_technical_reference_manual_en.pdf
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
.../devicetree/bindings/riscv/cpus.yaml | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/Documentation/devicetree/bindings/riscv/cpus.yaml b/Documentation/devicetree/bindings/riscv/cpus.yaml
index 0da219ae6769..17fef52aede1 100644
--- a/Documentation/devicetree/bindings/riscv/cpus.yaml
+++ b/Documentation/devicetree/bindings/riscv/cpus.yaml
@@ -38,6 +38,19 @@ allOf:
then:
properties:
thead,vlenb: false
+ - if:
+ properties:
+ compatible:
+ contains:
+ const: esp,esp32p4-hp
+ required:
+ - compatible
+ then:
+ properties:
+ interrupt-controller: false
+ else:
+ required:
+ - interrupt-controller
properties:
compatible:
@@ -48,6 +61,7 @@ properties:
- amd,mbv64
- andestech,ax45mp
- canaan,k210
+ - esp,esp32p4-hp
- nuclei,ux900
- sifive,bullet0
- sifive,e5
@@ -154,9 +168,6 @@ dependencies:
riscv,isa-base: ["riscv,isa-extensions"]
riscv,isa-extensions: ["riscv,isa-base"]
-required:
- - interrupt-controller
-
unevaluatedProperties: false
examples:
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 18/19] riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (16 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 17/19] dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 19/19] MAINTAINERS: Add the Espressif ESP32-P4 platform Vítor Hugo Silva
18 siblings, 0 replies; 20+ 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, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti
Add the initial ESP32-P4 SoC description for the CLIC, CLINT,
interrupt matrix and UART0. Describe the board's 32 MiB of PSRAM and
the 40 MHz clock used by UART0.
Build the board DTB when ESP32-P4 CLIC support is enabled. The initial
device tree supports booting to a UART shell on the Waveshare board.
Use UART0 as the default console at 115200 baud.
Describe both HP harts in the ESP32-P4 SoC device tree.
The initial Linux support runs on the boot hart only.
Select the revision 3.x interrupt matrix on the Waveshare board.
The CLINT timebase follows the CPU's clock. The firmware must populate
/cpus/timebase-frequency with the actual rate before booting Linux.
AI assisted with formatting the DTS and validating the resulting DTB.
Link: https://docs.waveshare.com/ESP32-P4-Module-DEV-KIT
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
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 +++++++++++++++++++
4 files changed, 113 insertions(+)
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
diff --git a/arch/riscv/boot/dts/Makefile b/arch/riscv/boot/dts/Makefile
index 69d8751fb17c..fa0f23de2616 100644
--- a/arch/riscv/boot/dts/Makefile
+++ b/arch/riscv/boot/dts/Makefile
@@ -3,6 +3,7 @@ subdir-y += allwinner
subdir-y += andes
subdir-y += anlogic
subdir-y += canaan
+subdir-y += espressif
subdir-y += eswin
subdir-y += microchip
subdir-y += renesas
diff --git a/arch/riscv/boot/dts/espressif/Makefile b/arch/riscv/boot/dts/espressif/Makefile
new file mode 100644
index 000000000000..fd89616862fb
--- /dev/null
+++ b/arch/riscv/boot/dts/espressif/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+dtb-$(CONFIG_RISCV_ESP32P4_CLIC) += esp32p4-waveshare-module-dev-kit.dtb
diff --git a/arch/riscv/boot/dts/espressif/esp32p4-waveshare-module-dev-kit.dts b/arch/riscv/boot/dts/espressif/esp32p4-waveshare-module-dev-kit.dts
new file mode 100644
index 000000000000..bdef328e19ef
--- /dev/null
+++ b/arch/riscv/boot/dts/espressif/esp32p4-waveshare-module-dev-kit.dts
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+/dts-v1/;
+
+#include "esp32p4.dtsi"
+
+/ {
+ model = "Waveshare ESP32-P4-Module-DEV-KIT";
+ compatible = "waveshare,esp32-p4-module-dev-kit", "esp,esp32p4";
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ aliases {
+ serial0 = &uart0;
+ };
+
+ chosen {
+ stdout-path = "serial0:115200n8";
+ };
+
+ xtal40m: clock-40000000 {
+ compatible = "fixed-clock";
+ #clock-cells = <0>;
+ clock-frequency = <40000000>;
+ };
+
+ memory@48000000 {
+ device_type = "memory";
+ reg = <0x48000000 0x02000000>;
+ };
+};
+
+&clic {
+ compatible = "esp,esp32p4-v3-clic";
+};
+
+&intmtx0 {
+ compatible = "esp,esp32p4-v3-intmtx";
+
+ status = "okay";
+};
+
+&uart0 {
+ clocks = <&xtal40m>;
+
+ status = "okay";
+};
diff --git a/arch/riscv/boot/dts/espressif/esp32p4.dtsi b/arch/riscv/boot/dts/espressif/esp32p4.dtsi
new file mode 100644
index 000000000000..211eef62b52e
--- /dev/null
+++ b/arch/riscv/boot/dts/espressif/esp32p4.dtsi
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+#include <dt-bindings/interrupt-controller/irq.h>
+
+/ {
+ cpus {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ /* Firmware supplies the timebase-frequency before boot. */
+
+ cpu@0 {
+ device_type = "cpu";
+ compatible = "esp,esp32p4-hp", "riscv";
+ reg = <0>;
+ mmu-type = "riscv,none";
+ riscv,isa-base = "rv32i";
+ riscv,isa-extensions = "i", "m", "a", "f", "c",
+ "zicsr", "zifencei";
+ };
+
+ cpu@1 {
+ device_type = "cpu";
+ compatible = "esp,esp32p4-hp", "riscv";
+ reg = <1>;
+ mmu-type = "riscv,none";
+ riscv,isa-base = "rv32i";
+ riscv,isa-extensions = "i", "m", "a", "f", "c",
+ "zicsr", "zifencei";
+ };
+ };
+
+ clic: interrupt-controller@20800000 {
+ compatible = "esp,esp32p4-clic";
+ reg = <0x20800000 0x10000>;
+ interrupt-controller;
+ #interrupt-cells = <2>;
+ };
+
+ intmtx0: interrupt-controller@500d6000 {
+ compatible = "esp,esp32p4-intmtx";
+ reg = <0x500d6000 0x400>;
+ interrupt-controller;
+ #interrupt-cells = <2>;
+ #address-cells = <0>;
+ interrupt-parent = <&clic>;
+
+ status = "disabled";
+ };
+
+ uart0: serial@500ca000 {
+ compatible = "esp,esp32p4-uart";
+ reg = <0x500ca000 0x1000>;
+ interrupts-extended = <&intmtx0 31 IRQ_TYPE_LEVEL_HIGH>;
+
+ status = "disabled";
+ };
+
+ timer@20000000 {
+ compatible = "esp,esp32p4-clint";
+ reg = <0x20000000 0x10000>;
+ interrupts-extended = <&clic 3 IRQ_TYPE_LEVEL_HIGH>,
+ <&clic 7 IRQ_TYPE_LEVEL_HIGH>;
+ };
+};
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread* [PATCH RFC 19/19] MAINTAINERS: Add the Espressif ESP32-P4 platform
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
` (17 preceding siblings ...)
2026-11-25 23:30 ` [PATCH RFC 18/19] riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT Vítor Hugo Silva
@ 2026-11-25 23:30 ` Vítor Hugo Silva
18 siblings, 0 replies; 20+ 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, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti
Add an entry for the ESP32-P4 Device Tree files and interrupt
controller drivers. Match ESP32-P4-specific changes in shared
files by keyword.
AI assisted with validating patch routing.
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
MAINTAINERS | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index ccbcdc0799b9..538f1a353526 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -24162,6 +24162,20 @@ F: arch/riscv/
N: riscv
K: riscv
+RISC-V ESPRESSIF ESP32-P4 SUPPORT
+M: Vítor Hugo Silva <vitorhg20080@gmail.com>
+L: linux-riscv@lists.infradead.org
+S: Maintained
+F: Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
+F: Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
+F: Documentation/devicetree/bindings/riscv/espressif.yaml
+F: arch/riscv/boot/dts/espressif/Makefile
+F: arch/riscv/boot/dts/espressif/esp32p4*
+F: drivers/irqchip/irq-esp32p4-clic.c
+F: drivers/irqchip/irq-esp32p4-intmtx.c
+K: \b(CONFIG_)?RISCV_ESP32P4_CLIC\b
+K: \b(esp32p4|ESP32P4)(\b|_)
+
RISC-V IOMMU
M: Tomasz Jeznach <tomasz.jeznach@linux.dev>
L: iommu@lists.linux.dev
--
2.56.1
^ permalink raw reply [flat|nested] 20+ messages in thread