You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2026/10/09 18:12:34 UTC

(nuttx) branch master updated (db514c3e63a -> a661949f8a5)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


    from db514c3e63a arch/arm/src/common/stm32: enable backup domain access for LSI on U5
     new bc44441f9d8 arch/arm/src/imxrt: Add an RPTUN backend for the RT1170 CM4.
     new 5dcaf8ace8d Documentation/platforms/arm/imxrt: Document the RT1170 CM4 rptun driver.
     new a661949f8a5 boards/arm/imxrt/fmu-v6xrt: Add an rptun configuration for the CM4.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../platforms/arm/imxrt/boards/fmu-v6xrt/index.rst |  41 +-
 Documentation/platforms/arm/imxrt/index.rst        |  51 +++
 arch/arm/src/imxrt/CMakeLists.txt                  |   4 +
 arch/arm/src/imxrt/Kconfig                         |  17 +
 arch/arm/src/imxrt/Make.defs                       |   4 +
 arch/arm/src/imxrt/hardware/rt117x/imxrt117x_mu.h  |  92 +++++
 arch/arm/src/imxrt/hardware/rt117x/imxrt117x_src.h |  84 +++++
 arch/arm/src/imxrt/imxrt_periphclks.h              |   8 +
 arch/arm/src/imxrt/imxrt_rptun.c                   | 411 +++++++++++++++++++++
 .../arm/src/imxrt/imxrt_rptun.h                    |  79 ++--
 boards/arm/imxrt/fmu-v6xrt/Kconfig                 |  22 ++
 .../fmu-v6xrt/configs/{nsh => rptun}/defconfig     |  23 ++
 boards/arm/imxrt/fmu-v6xrt/src/.gitignore          |   3 +
 boards/arm/imxrt/fmu-v6xrt/src/CMakeLists.txt      |  17 +
 boards/arm/imxrt/fmu-v6xrt/src/Makefile            |   8 +
 boards/arm/imxrt/fmu-v6xrt/src/fmu-v6xrt.h         |  13 +
 boards/arm/imxrt/fmu-v6xrt/src/imxrt_bringup.c     |  10 +
 boards/arm/imxrt/fmu-v6xrt/src/imxrt_cm4.c         | 106 ++++++
 18 files changed, 941 insertions(+), 52 deletions(-)
 create mode 100644 arch/arm/src/imxrt/hardware/rt117x/imxrt117x_mu.h
 create mode 100644 arch/arm/src/imxrt/hardware/rt117x/imxrt117x_src.h
 create mode 100644 arch/arm/src/imxrt/imxrt_rptun.c
 copy include/nuttx/aie/ai_engine.h => arch/arm/src/imxrt/imxrt_rptun.h (53%)
 copy boards/arm/imxrt/fmu-v6xrt/configs/{nsh => rptun}/defconfig (74%)
 create mode 100644 boards/arm/imxrt/fmu-v6xrt/src/.gitignore
 create mode 100644 boards/arm/imxrt/fmu-v6xrt/src/imxrt_cm4.c


(nuttx) 02/03: Documentation/platforms/arm/imxrt: Document the RT1170 CM4 rptun driver.

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 5dcaf8ace8d9170013e8dde49d1fb0a75f84b72d
Author: Lourens Naude <lo...@bearmetal.eu>
AuthorDate: Thu Sep 24 23:02:49 2026 +0100

    Documentation/platforms/arm/imxrt: Document the RT1170 CM4 rptun driver.
    
    Document CONFIG_IMXRT_RPTUN: what the driver does at start and stop,
    the memory map rules the board must follow (only the first 128 KB of
    the backdoor window reaches the CM4, boot from the backdoor alias,
    shared window mapped non-cacheable), the carveout resource the CM4
    table must carry for the vrings and buffers, and the kick ordering the
    CM4 firmware must honour.
    
    Assisted-by: Claude:claude-fable-5-1
    Signed-off-by: Lourens Naude <lo...@bearmetal.eu>
---
 Documentation/platforms/arm/imxrt/index.rst | 42 +++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/Documentation/platforms/arm/imxrt/index.rst b/Documentation/platforms/arm/imxrt/index.rst
index 3e302d83aaf..285f3330c9b 100644
--- a/Documentation/platforms/arm/imxrt/index.rst
+++ b/Documentation/platforms/arm/imxrt/index.rst
@@ -295,6 +295,48 @@ USB
 Console communication over USB is supported via CDC-ACM. Only USB Device is currently supported
 for i.MX RT in NuttX
 
+Multicore: RT1170 CM4 over RPTUN
+================================
+
+The i.MX RT1170 pairs the Cortex-M7 with a Cortex-M4. NuttX runs on the CM7 and can load, release
+and hold a firmware on the CM4 through the NuttX RPTUN device on top of the OpenAMP framework
+(``CONFIG_IMXRT_RPTUN``, which selects ``CONFIG_RPTUN_LOADER``). The CM4 firmware is the virtio
+device side and provides the resource table in its ELF image; it may be bare metal. The table must
+carry a carveout resource for the shared window after the table: NuttX 13 rptun places the vrings
+and the rpmsg buffers in it and refuses the device without it, NuttX 12.12 rptun parses it and lays
+out the window on its own.
+
+The board registers the remote with :c:func:`imxrt_rptun_init`, passing the remote name, the ELF
+path, the address environment (CM4 device addresses to CM7 physical addresses), the boot vector and
+whether to autostart. The option is offered only on parts with a CM4 (``IMXRT_HAVE_CM4``) and
+selects ``DEV_SIMPLE_ADDRENV`` for libmetal.
+The driver:
+
+- Programs the boot vector into ``IOMUXC_LPSR_GPR0/1`` and reads it back.
+- Releases the core with ``SRC_SCR.BT_RELEASE_M4``. The slice stays under reset until its first
+  release and cannot be held again afterwards: a software reset of the slice is a pulse after which
+  the core restarts from the boot vector. To stop the core the driver writes a ``wfi`` loop over the
+  start of the image and pulses the reset, so the core parks there until the next start reloads the
+  image. A reset completion is awaited only when restarting a core that was already released.
+- Keeps a CM4 lockup local by setting ``SRC_SRMR`` so it does not reset the whole chip.
+- Exchanges virtqueue kicks with the CM4 on MU-A channel 0. Neither side reads the mailbox word; a
+  kick into a full mailbox is coalesced with the pending one and the receiver rescans every
+  virtqueue. The CM4 firmware must therefore read MU-B ``RR0`` before it scans, so that a kick
+  coalesced during the scan is covered by it.
+- Starts the remote from :c:func:`rptun_initialize` when the board sets ``autostart``; otherwise
+  ``RPTUNIOC_START`` on ``/dev/rptun/<name>`` or :c:func:`rptun_boot` starts it.
+
+Memory map rules the board must follow:
+
+- The CM7 reaches the CM4 TCM through the LMEM backdoor window at ``IMXRT_OCRAM_M4_BASE``. Only the
+  first 128 KB of that window, the CM4 code TCM, is reachable; writes to the second half do not
+  reach the system TCM. The resource table, vrings and rpmsg buffers must live in the code TCM.
+- The CM4 boots from the backdoor alias of its code TCM (``0x20200000``), as NXP MCMGR does; the
+  CM4-native ``0x1FFE0000`` does not boot.
+- The board maps the memory the CM4 writes as non-cacheable (for example an MPU region over the
+  shared window) before calling :c:func:`imxrt_rptun_init`. The driver cleans the D-cache over the
+  loaded image before releasing the core.
+
 Supported Boards
 ================
 


(nuttx) 01/03: arch/arm/src/imxrt: Add an RPTUN backend for the RT1170 CM4.

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit bc44441f9d8af5c60771401a2e893828550f5adc
Author: Lourens Naude <lo...@bearmetal.eu>
AuthorDate: Thu Sep 24 23:02:49 2026 +0100

    arch/arm/src/imxrt: Add an RPTUN backend for the RT1170 CM4.
    
    The i.MX RT1176 pairs the Cortex-M7 with a Cortex-M4 that NuttX left
    in ROM-held reset.  This backend lets NuttX on the CM7 run the CM4 as
    an RPTUN remote.  The loader writes the CM4 ELF through the LMEM
    backdoor window, the boot vector goes into IOMUXC_LPSR_GPR0/1 as the
    backdoor alias of the code TCM (the CM4-native address does not boot,
    which NXP's MCMGR also assumes), and SRC releases the core.
    
    A released slice cannot be held in reset again: a software reset is a
    pulse after which the core restarts from the boot vector.  Stop
    therefore parks the core in a wfi loop written over the start of its
    image, and the next start reloads the image.
    
    Virtqueue kicks travel on MU-A channel 0.  A kick into a full mailbox
    is coalesced, because the remote rescans every virtqueue on any kick.
    SRC_SRMR is set so a CM4 lockup resets only the CM4 instead of the
    whole chip.
    
    The board supplies the remote name, the ELF path, the address
    environment, the boot vector and the autostart choice through
    imxrt_rptun_init().  Gated by IMXRT_HAVE_CM4, which only the
    MIMXRT1176 selects; selects DEV_SIMPLE_ADDRENV for libmetal.
    
    Assisted-by: Claude:claude-fable-5-1
    Signed-off-by: Lourens Naude <lo...@bearmetal.eu>
---
 arch/arm/src/imxrt/CMakeLists.txt                  |   4 +
 arch/arm/src/imxrt/Kconfig                         |  17 +
 arch/arm/src/imxrt/Make.defs                       |   4 +
 arch/arm/src/imxrt/hardware/rt117x/imxrt117x_mu.h  |  92 +++++
 arch/arm/src/imxrt/hardware/rt117x/imxrt117x_src.h |  84 +++++
 arch/arm/src/imxrt/imxrt_periphclks.h              |   8 +
 arch/arm/src/imxrt/imxrt_rptun.c                   | 411 +++++++++++++++++++++
 arch/arm/src/imxrt/imxrt_rptun.h                   |  83 +++++
 8 files changed, 703 insertions(+)

diff --git a/arch/arm/src/imxrt/CMakeLists.txt b/arch/arm/src/imxrt/CMakeLists.txt
index 9a2a9aa4293..f7ecd7a1a2f 100644
--- a/arch/arm/src/imxrt/CMakeLists.txt
+++ b/arch/arm/src/imxrt/CMakeLists.txt
@@ -103,6 +103,10 @@ if(CONFIG_IMXRT_EDMA)
   endif()
 endif()
 
+if(CONFIG_IMXRT_RPTUN)
+  list(APPEND SRCS imxrt_rptun.c)
+endif()
+
 if(CONFIG_IMXRT_USDHC)
   list(APPEND SRCS imxrt_usdhc.c)
 endif()
diff --git a/arch/arm/src/imxrt/Kconfig b/arch/arm/src/imxrt/Kconfig
index b89db3dac00..ec58312062d 100644
--- a/arch/arm/src/imxrt/Kconfig
+++ b/arch/arm/src/imxrt/Kconfig
@@ -101,6 +101,7 @@ config ARCH_CHIP_MIMXRT1064CVL5A
 config ARCH_CHIP_MIMXRT1176DVMAA
 	bool "MIMXRT1176DVMAA"
 	select ARCH_FAMILY_IMXRT117x
+	select IMXRT_HAVE_CM4
 	select IMXRT_HAVE_LCD
 	select IMXRT_HAVE_USBOTG1
 	select IMXRT_HAVE_USBOTG2
@@ -390,6 +391,10 @@ config IMXRT_HAVE_LCD
 	bool
 	default n
 
+config IMXRT_HAVE_CM4
+	bool
+	default n
+
 config IMXRT_HAVE_USBOTG1
 	bool
 	default n
@@ -569,6 +574,18 @@ config IMXRT_WDOG
 	default n
 	depends on WATCHDOG
 
+config IMXRT_RPTUN
+	bool "RPTUN transport to the CM4 core"
+	default n
+	depends on IMXRT_HAVE_CM4 && RPTUN
+	select RPTUN_LOADER
+	select DEV_SIMPLE_ADDRENV
+	---help---
+		rpmsg/virtio transport to the Cortex-M4 of the dual-core i.MX RT1170.
+		The CM7 loads the CM4 ELF, releases the core and signals it over the
+		MU-A mailbox.  The board supplies the memory map and the ELF path
+		through imxrt_rptun_init().
+
 menu "FlexIO Peripherals"
 
 config IMXRT_FLEXIO1
diff --git a/arch/arm/src/imxrt/Make.defs b/arch/arm/src/imxrt/Make.defs
index e7f037dbdf8..fd74819ee10 100644
--- a/arch/arm/src/imxrt/Make.defs
+++ b/arch/arm/src/imxrt/Make.defs
@@ -99,6 +99,10 @@ CHIP_CSRCS += imxrt_edma.c
 endif
 endif
 
+ifeq ($(CONFIG_IMXRT_RPTUN),y)
+CHIP_CSRCS += imxrt_rptun.c
+endif
+
 ifdef CONFIG_IMXRT_USDHC
 CHIP_CSRCS += imxrt_usdhc.c
 endif
diff --git a/arch/arm/src/imxrt/hardware/rt117x/imxrt117x_mu.h b/arch/arm/src/imxrt/hardware/rt117x/imxrt117x_mu.h
new file mode 100644
index 00000000000..f8368d48a12
--- /dev/null
+++ b/arch/arm/src/imxrt/hardware/rt117x/imxrt117x_mu.h
@@ -0,0 +1,92 @@
+/****************************************************************************
+ * arch/arm/src/imxrt/hardware/rt117x/imxrt117x_mu.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_ARM_SRC_IMXRT_HARDWARE_RT117X_IMXRT117X_MU_H
+#define __ARCH_ARM_SRC_IMXRT_HARDWARE_RT117X_IMXRT117X_MU_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include "hardware/imxrt_memorymap.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The i.MX RT117x Messaging Unit is the "v1" MU: 4 transmit and 4 receive
+ * mailbox registers, one status and one control register per side.
+ * MU-A is the CM7 side, MU-B is the CM4 side.
+ */
+
+#define IMXRT_MU_CHANNELS            4
+
+/* Register Offsets *********************************************************/
+
+#define IMXRT_MU_TR_OFFSET(n)        (0x0000 + ((n) << 2)) /* Transmit n */
+#define IMXRT_MU_RR_OFFSET(n)        (0x0010 + ((n) << 2)) /* Receive n */
+#define IMXRT_MU_SR_OFFSET           0x0020                /* Status */
+#define IMXRT_MU_CR_OFFSET           0x0024                /* Control */
+
+/* Register Addresses (CM7 side, MU-A) **************************************/
+
+#define IMXRT_MUA_TR(n)              (IMXRT_MU_A + IMXRT_MU_TR_OFFSET(n))
+#define IMXRT_MUA_RR(n)              (IMXRT_MU_A + IMXRT_MU_RR_OFFSET(n))
+#define IMXRT_MUA_SR                 (IMXRT_MU_A + IMXRT_MU_SR_OFFSET)
+#define IMXRT_MUA_CR                 (IMXRT_MU_A + IMXRT_MU_CR_OFFSET)
+
+/* Status Register (SR) *****************************************************/
+
+#define MU_SR_F_SHIFT                (0)       /* Bits 0-2: Other side flags */
+#define MU_SR_F_MASK                 (7 << MU_SR_F_SHIFT)
+#define MU_SR_EP                     (1 << 4)  /* Bit 4:  Event pending */
+#define MU_SR_RS                     (1 << 7)  /* Bit 7:  Other side reset */
+#define MU_SR_TE_SHIFT               (20)      /* Bits 20-23: TEn, TE0 is bit 23 */
+#define MU_SR_TE(n)                  (1 << (23 - (n)))
+#define MU_SR_TE_MASK                (15 << MU_SR_TE_SHIFT)
+#define MU_SR_RF_SHIFT               (24)      /* Bits 24-27: RFn, RF0 is bit 27 */
+#define MU_SR_RF(n)                  (1 << (27 - (n)))
+#define MU_SR_RF_MASK                (15 << MU_SR_RF_SHIFT)
+#define MU_SR_GIP_SHIFT              (28)      /* Bits 28-31: GIP0 is bit 31 */
+#define MU_SR_GIP(n)                 (1 << (31 - (n)))
+#define MU_SR_GIP_MASK               (15 << MU_SR_GIP_SHIFT)
+
+/* Control Register (CR) ****************************************************/
+
+#define MU_CR_F_SHIFT                (0)       /* Bits 0-2: Flags to other side */
+#define MU_CR_F_MASK                 (7 << MU_CR_F_SHIFT)
+#define MU_CR_MUR                    (1 << 5)  /* Bit 5:  MU reset */
+#define MU_CR_GIR_SHIFT              (16)      /* Bits 16-19: GIR0 is bit 19 */
+#define MU_CR_GIR(n)                 (1 << (19 - (n)))
+#define MU_CR_GIR_MASK               (15 << MU_CR_GIR_SHIFT)
+#define MU_CR_TIE_SHIFT              (20)      /* Bits 20-23: TIE0 is bit 23 */
+#define MU_CR_TIE(n)                 (1 << (23 - (n)))
+#define MU_CR_TIE_MASK               (15 << MU_CR_TIE_SHIFT)
+#define MU_CR_RIE_SHIFT              (24)      /* Bits 24-27: RIE0 is bit 27 */
+#define MU_CR_RIE(n)                 (1 << (27 - (n)))
+#define MU_CR_RIE_MASK               (15 << MU_CR_RIE_SHIFT)
+#define MU_CR_GIE_SHIFT              (28)      /* Bits 28-31: GIE0 is bit 31 */
+#define MU_CR_GIE(n)                 (1 << (31 - (n)))
+#define MU_CR_GIE_MASK               (15 << MU_CR_GIE_SHIFT)
+
+#endif /* __ARCH_ARM_SRC_IMXRT_HARDWARE_RT117X_IMXRT117X_MU_H */
diff --git a/arch/arm/src/imxrt/hardware/rt117x/imxrt117x_src.h b/arch/arm/src/imxrt/hardware/rt117x/imxrt117x_src.h
new file mode 100644
index 00000000000..6f4483761e8
--- /dev/null
+++ b/arch/arm/src/imxrt/hardware/rt117x/imxrt117x_src.h
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * arch/arm/src/imxrt/hardware/rt117x/imxrt117x_src.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_ARM_SRC_IMXRT_HARDWARE_RT117X_IMXRT117X_SRC_H
+#define __ARCH_ARM_SRC_IMXRT_HARDWARE_RT117X_IMXRT117X_SRC_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include "hardware/imxrt_memorymap.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* System Reset Controller.  Partial: the registers that boot, hold and
+ * diagnose the CM4 (RM chapter 25); the per-slice authentication and the
+ * other reset slices are not described yet.
+ */
+
+#define IMXRT_SRC_SCR_OFFSET              0x0000  /* SRC Control */
+#define IMXRT_SRC_SRMR_OFFSET             0x0004  /* SRC Reset Mode */
+#define IMXRT_SRC_SRSR_OFFSET             0x0010  /* SRC Reset Status */
+#define IMXRT_SRC_CTRL_M4CORE_OFFSET      0x0284  /* M4 core slice control */
+#define IMXRT_SRC_STAT_M4CORE_OFFSET      0x0290  /* M4 core slice status */
+
+#define IMXRT_SRC_SCR                     (IMXRT_SRC_BASE + IMXRT_SRC_SCR_OFFSET)
+#define IMXRT_SRC_SRMR                    (IMXRT_SRC_BASE + IMXRT_SRC_SRMR_OFFSET)
+#define IMXRT_SRC_SRSR                    (IMXRT_SRC_BASE + IMXRT_SRC_SRSR_OFFSET)
+#define IMXRT_SRC_CTRL_M4CORE             (IMXRT_SRC_BASE + IMXRT_SRC_CTRL_M4CORE_OFFSET)
+#define IMXRT_SRC_STAT_M4CORE             (IMXRT_SRC_BASE + IMXRT_SRC_STAT_M4CORE_OFFSET)
+
+#define SRC_SCR_BT_RELEASE_M4             (1 << 0)  /* Release CM4 from reset */
+
+/* Reset mode per source: 0 resets the system, 3 resets nothing. */
+
+#define SRC_SRMR_M4LOCKUP_SHIFT           (6)
+#define SRC_SRMR_M4LOCKUP_MASK            (3 << SRC_SRMR_M4LOCKUP_SHIFT)
+#define SRC_SRMR_M4LOCKUP_NONE            (3 << SRC_SRMR_M4LOCKUP_SHIFT)
+
+/* Reset status: sticky causes, write 1 to clear. */
+
+#define SRC_SRSR_M7_LOCKUP                (1 << 2)  /* CM7 lockup */
+#define SRC_SRSR_WDOG                     (1 << 5)  /* WDOG1 timeout */
+#define SRC_SRSR_M4_LOCKUP                (1 << 12) /* CM4 lockup */
+
+#define SRC_CTRL_M4CORE_SW_RESET          (1 << 0)  /* Software reset of CM4 slice */
+#define SRC_STAT_M4CORE_UNDER_RST         (1 << 0)  /* CM4 slice in reset */
+
+/* IOMUXC LPSR GPR0/1 hold the CM4 initial vector table address. */
+
+#define IMXRT_IOMUXC_LPSR_GPR_GPR0_OFFSET (0x0000)
+#define IMXRT_IOMUXC_LPSR_GPR_GPR1_OFFSET (0x0004)
+
+#define IMXRT_IOMUXC_LPSR_GPR_GPR0        (IMXRT_IOMUXCLPSRGPR_BASE + IMXRT_IOMUXC_LPSR_GPR_GPR0_OFFSET)
+#define IMXRT_IOMUXC_LPSR_GPR_GPR1        (IMXRT_IOMUXCLPSRGPR_BASE + IMXRT_IOMUXC_LPSR_GPR_GPR1_OFFSET)
+
+/* GPR0[15:3] holds VTOR[15:3], GPR1[15:0] holds VTOR[31:16]. */
+
+#define GPR0_CM4_INIT_VTOR_LOW(vtor)      ((vtor) & 0x0000fff8)
+#define GPR1_CM4_INIT_VTOR_HIGH(vtor)     (((vtor) >> 16) & 0x0000ffff)
+
+#endif /* __ARCH_ARM_SRC_IMXRT_HARDWARE_RT117X_IMXRT117X_SRC_H */
diff --git a/arch/arm/src/imxrt/imxrt_periphclks.h b/arch/arm/src/imxrt/imxrt_periphclks.h
index b8d700a3e72..127432bb5ec 100644
--- a/arch/arm/src/imxrt/imxrt_periphclks.h
+++ b/arch/arm/src/imxrt/imxrt_periphclks.h
@@ -173,6 +173,10 @@
 #  define imxrt_clockoff_lpuart12()       imxrt_periphclk_configure(CCM_CCGR_LPUART12, CCM_CG_OFF)
 #endif
 #define imxrt_clockoff_mqs()              imxrt_periphclk_configure(CCM_CCGR_MQS, CCM_CG_OFF)
+#ifdef CONFIG_ARCH_FAMILY_IMXRT117x
+#  define imxrt_clockoff_mu_a()           imxrt_periphclk_configure(CCM_CCGR_MU_A, CCM_CG_OFF)
+#  define imxrt_clockoff_mu_b()           imxrt_periphclk_configure(CCM_CCGR_MU_B, CCM_CG_OFF)
+#endif
 #define imxrt_clockoff_ocotp_ctrl()       imxrt_periphclk_configure(CCM_CCGR_OCOTP_CTRL, CCM_CG_OFF)
 #define imxrt_clockoff_ocram()            imxrt_periphclk_configure(CCM_CCGR_OCRAM, CCM_CG_OFF)
 #define imxrt_clockoff_pit()              imxrt_periphclk_configure(CCM_CCGR_PIT, CCM_CG_OFF)
@@ -513,6 +517,10 @@
 #  define imxrt_clockall_lpuart12()       imxrt_periphclk_configure(CCM_CCGR_LPUART12, CCM_CG_ALL)
 #endif
 #define imxrt_clockall_mqs()              imxrt_periphclk_configure(CCM_CCGR_MQS, CCM_CG_ALL)
+#ifdef CONFIG_ARCH_FAMILY_IMXRT117x
+#  define imxrt_clockall_mu_a()           imxrt_periphclk_configure(CCM_CCGR_MU_A, CCM_CG_ALL)
+#  define imxrt_clockall_mu_b()           imxrt_periphclk_configure(CCM_CCGR_MU_B, CCM_CG_ALL)
+#endif
 #define imxrt_clockall_ocotp_ctrl()       imxrt_periphclk_configure(CCM_CCGR_OCOTP_CTRL, CCM_CG_ALL)
 #define imxrt_clockall_ocram()            imxrt_periphclk_configure(CCM_CCGR_OCRAM, CCM_CG_ALL)
 #define imxrt_clockall_pit()              imxrt_periphclk_configure(CCM_CCGR_PIT, CCM_CG_ALL)
diff --git a/arch/arm/src/imxrt/imxrt_rptun.c b/arch/arm/src/imxrt/imxrt_rptun.c
new file mode 100644
index 00000000000..b34486bdc55
--- /dev/null
+++ b/arch/arm/src/imxrt/imxrt_rptun.c
@@ -0,0 +1,411 @@
+/****************************************************************************
+ * arch/arm/src/imxrt/imxrt_rptun.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <debug.h>
+#include <inttypes.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/cache.h>
+#include <nuttx/irq.h>
+#include <nuttx/nuttx.h>
+#include <nuttx/rptun/rptun.h>
+
+#include "arm_internal.h"
+#include "imxrt_periphclks.h"
+#include "imxrt_rptun.h"
+#include "hardware/rt117x/imxrt117x_mu.h"
+#include "hardware/rt117x/imxrt117x_src.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Kicks travel on MU channel 0 in both directions.  The word carries no
+ * information: either side rescans every virtqueue on any kick.
+ */
+
+#define IMXRT_RPTUN_MU_CHAN   0
+#define IMXRT_RPTUN_KICK      1
+
+/* Wait for the M4 slice to leave reset: 1000 x 10 us. */
+
+#define IMXRT_RPTUN_RESET_RETRIES 1000
+#define IMXRT_RPTUN_RESET_DELAY   10
+
+/* Once released, the M4 slice cannot be held in reset again: a software
+ * reset is a pulse and the core restarts from CM4_INIT_VTOR.  To stop the
+ * core we point its reset vector at a two-instruction park loop written
+ * over the start of its image, then pulse the reset.
+ *
+ *   boot + 0: initial SP        (top of the CM4 system TCM)
+ *   boot + 4: reset handler     (boot + 8, Thumb)
+ *   boot + 8: wfi; b .-2        (0xBF30, 0xE7FD)
+ */
+
+#define IMXRT_RPTUN_PARK_SP       0x20020000
+#define IMXRT_RPTUN_PARK_CODE     0xE7FDBF30
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct imxrt_rptun_dev_s
+{
+  struct rptun_dev_s                 rptun;
+  const struct imxrt_rptun_config_s *cfg;
+  rptun_callback_t                   callback;
+  void                              *arg;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static const char *imxrt_rptun_get_cpuname(struct rptun_dev_s *dev);
+static const char *imxrt_rptun_get_firmware(struct rptun_dev_s *dev);
+static const struct rptun_addrenv_s *
+imxrt_rptun_get_addrenv(struct rptun_dev_s *dev);
+static bool imxrt_rptun_is_autostart(struct rptun_dev_s *dev);
+static bool imxrt_rptun_is_master(struct rptun_dev_s *dev);
+static int imxrt_rptun_start(struct rptun_dev_s *dev);
+static int imxrt_rptun_stop(struct rptun_dev_s *dev);
+static int imxrt_rptun_notify(struct rptun_dev_s *dev, uint32_t vqid);
+static int imxrt_rptun_register_callback(struct rptun_dev_s *dev,
+                                         rptun_callback_t callback,
+                                         void *arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct rptun_ops_s g_imxrt_rptun_ops =
+{
+  .get_cpuname       = imxrt_rptun_get_cpuname,
+  .get_firmware      = imxrt_rptun_get_firmware,
+  .get_addrenv       = imxrt_rptun_get_addrenv,
+  .is_autostart      = imxrt_rptun_is_autostart,
+  .is_master         = imxrt_rptun_is_master,
+  .start             = imxrt_rptun_start,
+  .stop              = imxrt_rptun_stop,
+  .notify            = imxrt_rptun_notify,
+  .register_callback = imxrt_rptun_register_callback,
+};
+
+static struct imxrt_rptun_dev_s g_imxrt_rptun;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static const char *imxrt_rptun_get_cpuname(struct rptun_dev_s *dev)
+{
+  struct imxrt_rptun_dev_s *priv =
+    container_of(dev, struct imxrt_rptun_dev_s, rptun);
+
+  return priv->cfg->cpuname;
+}
+
+static const char *imxrt_rptun_get_firmware(struct rptun_dev_s *dev)
+{
+  struct imxrt_rptun_dev_s *priv =
+    container_of(dev, struct imxrt_rptun_dev_s, rptun);
+
+  return priv->cfg->firmware;
+}
+
+static const struct rptun_addrenv_s *
+imxrt_rptun_get_addrenv(struct rptun_dev_s *dev)
+{
+  struct imxrt_rptun_dev_s *priv =
+    container_of(dev, struct imxrt_rptun_dev_s, rptun);
+
+  return priv->cfg->addrenv;
+}
+
+static bool imxrt_rptun_is_autostart(struct rptun_dev_s *dev)
+{
+  struct imxrt_rptun_dev_s *priv =
+    container_of(dev, struct imxrt_rptun_dev_s, rptun);
+
+  return priv->cfg->autostart;
+}
+
+static bool imxrt_rptun_is_master(struct rptun_dev_s *dev)
+{
+  return true;
+}
+
+/****************************************************************************
+ * Name: imxrt_rptun_start
+ *
+ * Description:
+ *   Called by rptun after the ELF is loaded and the virtio device is
+ *   DRIVER_OK.  Publish the boot vector and release the CM4.
+ *
+ ****************************************************************************/
+
+static int imxrt_rptun_start(struct rptun_dev_s *dev)
+{
+  struct imxrt_rptun_dev_s *priv =
+    container_of(dev, struct imxrt_rptun_dev_s, rptun);
+  const struct rptun_addrenv_s *env;
+  uint32_t vtor;
+  int retries = IMXRT_RPTUN_RESET_RETRIES;
+
+  /* The loader wrote the image through the (possibly cached) OCRAM_M4
+   * alias; make sure the CM4 sees it before it starts fetching.
+   */
+
+  for (env = priv->cfg->addrenv; env != NULL && env->size != 0; env++)
+    {
+      up_clean_dcache(env->pa, env->pa + env->size);
+    }
+
+  putreg32(GPR0_CM4_INIT_VTOR_LOW(priv->cfg->boot_addr),
+           IMXRT_IOMUXC_LPSR_GPR_GPR0);
+  putreg32(GPR1_CM4_INIT_VTOR_HIGH(priv->cfg->boot_addr),
+           IMXRT_IOMUXC_LPSR_GPR_GPR1);
+
+  vtor = (getreg32(IMXRT_IOMUXC_LPSR_GPR_GPR1) & 0xffff) << 16 |
+         (getreg32(IMXRT_IOMUXC_LPSR_GPR_GPR0) & 0xfff8);
+  if (vtor != priv->cfg->boot_addr)
+    {
+      ipcerr("ERROR: CM4 VTOR readback %08" PRIx32 " != %08" PRIxPTR "\n",
+             vtor, priv->cfg->boot_addr);
+      return -EIO;
+    }
+
+  /* The slice stays in reset until the first release, so only re-reset a
+   * core that is already running (restart, or released by a debugger).
+   */
+
+  if ((getreg32(IMXRT_SRC_SCR) & SRC_SCR_BT_RELEASE_M4) != 0)
+    {
+      putreg32(SRC_CTRL_M4CORE_SW_RESET, IMXRT_SRC_CTRL_M4CORE);
+      while ((getreg32(IMXRT_SRC_STAT_M4CORE) &
+              SRC_STAT_M4CORE_UNDER_RST) != 0)
+        {
+          if (--retries == 0)
+            {
+              ipcerr("ERROR: CM4 slice did not leave reset\n");
+              return -ETIMEDOUT;
+            }
+
+          up_udelay(IMXRT_RPTUN_RESET_DELAY);
+        }
+    }
+
+  modifyreg32(IMXRT_SRC_SCR, 0, SRC_SCR_BT_RELEASE_M4);
+
+  ipcinfo("CM4 released, VTOR=%08" PRIxPTR "\n", priv->cfg->boot_addr);
+  return OK;
+}
+
+static int imxrt_rptun_stop(struct rptun_dev_s *dev)
+{
+  struct imxrt_rptun_dev_s *priv =
+    container_of(dev, struct imxrt_rptun_dev_s, rptun);
+  uintptr_t boot = priv->cfg->boot_addr;
+
+  /* Park the core: write the loop over the image start, then reset the
+   * slice so it restarts into the loop.  The next start reloads the image.
+   */
+
+  putreg32(IMXRT_RPTUN_PARK_SP, boot);
+  putreg32((boot + 8) | 1, boot + 4);
+  putreg32(IMXRT_RPTUN_PARK_CODE, boot + 8);
+  up_clean_dcache(boot, boot + 12);
+
+  putreg32(SRC_CTRL_M4CORE_SW_RESET, IMXRT_SRC_CTRL_M4CORE);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: imxrt_rptun_notify
+ *
+ * Description:
+ *   Kick the CM4.  TEn clears on write and returns once the CM4 has read
+ *   RRn; a full mailbox means a kick is already pending and the CM4 rescans
+ *   everything when it takes it, so coalesce rather than spin: rptun kicks
+ *   before the CM4 is even released from reset.
+ *
+ ****************************************************************************/
+
+static int imxrt_rptun_notify(struct rptun_dev_s *dev, uint32_t vqid)
+{
+  irqstate_t flags = enter_critical_section();
+
+  if ((getreg32(IMXRT_MUA_SR) & MU_SR_TE(IMXRT_RPTUN_MU_CHAN)) != 0)
+    {
+      putreg32(IMXRT_RPTUN_KICK, IMXRT_MUA_TR(IMXRT_RPTUN_MU_CHAN));
+    }
+
+  leave_critical_section(flags);
+  return OK;
+}
+
+static int imxrt_rptun_register_callback(struct rptun_dev_s *dev,
+                                         rptun_callback_t callback,
+                                         void *arg)
+{
+  struct imxrt_rptun_dev_s *priv =
+    container_of(dev, struct imxrt_rptun_dev_s, rptun);
+
+  priv->callback = callback;
+  priv->arg      = arg;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: imxrt_rptun_mu_interrupt
+ *
+ * Description:
+ *   The CM4 kicked.  Reading RRn clears RFn; rptun scans every virtqueue.
+ *
+ ****************************************************************************/
+
+static int imxrt_rptun_mu_interrupt(int irq, void *context, void *arg)
+{
+  struct imxrt_rptun_dev_s *priv = arg;
+  uint32_t sr = getreg32(IMXRT_MUA_SR);
+  int chan;
+
+  for (chan = 0; chan < IMXRT_MU_CHANNELS; chan++)
+    {
+      if ((sr & MU_SR_RF(chan)) != 0)
+        {
+          (void)getreg32(IMXRT_MUA_RR(chan));
+        }
+    }
+
+  if ((sr & MU_SR_GIP_MASK) != 0)
+    {
+      putreg32(sr & MU_SR_GIP_MASK, IMXRT_MUA_SR);
+    }
+
+  if ((sr & MU_SR_RF(IMXRT_RPTUN_MU_CHAN)) != 0 && priv->callback != NULL)
+    {
+      priv->callback(priv->arg, RPTUN_NOTIFY_ALL);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: imxrt_rptun_mu_init
+ *
+ * Description:
+ *   Bring up MU-A without resetting the unit (MUR also resets the CM4
+ *   side): clock both sides, drain the receive mailboxes, clear the
+ *   general interrupt flags, then enable the channel 0 receive interrupt.
+ *
+ ****************************************************************************/
+
+static int imxrt_rptun_mu_init(struct imxrt_rptun_dev_s *priv)
+{
+  int ret;
+  int chan;
+
+  imxrt_clockall_mu_a();
+  imxrt_clockall_mu_b();
+
+  putreg32(0, IMXRT_MUA_CR);
+  for (chan = 0; chan < IMXRT_MU_CHANNELS; chan++)
+    {
+      (void)getreg32(IMXRT_MUA_RR(chan));
+    }
+
+  putreg32(MU_SR_GIP_MASK, IMXRT_MUA_SR);
+
+  ret = irq_attach(IMXRT_IRQ_MU, imxrt_rptun_mu_interrupt, priv);
+  if (ret < 0)
+    {
+      imxrt_clockoff_mu_b();
+      imxrt_clockoff_mu_a();
+      return ret;
+    }
+
+  putreg32(MU_CR_RIE(IMXRT_RPTUN_MU_CHAN), IMXRT_MUA_CR);
+  up_enable_irq(IMXRT_IRQ_MU);
+  return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int imxrt_rptun_init(const struct imxrt_rptun_config_s *cfg)
+{
+  struct imxrt_rptun_dev_s *priv = &g_imxrt_rptun;
+  int ret;
+
+  if (cfg == NULL || cfg->cpuname == NULL || cfg->firmware == NULL ||
+      cfg->addrenv == NULL)
+    {
+      return -EINVAL;
+    }
+
+  if (priv->cfg != NULL)
+    {
+      return -EBUSY;
+    }
+
+  /* A CM4 lockup would otherwise reset the whole chip. The CM7 owns the
+   * remote (stop/start), so keep the reset local to the CM4.
+   */
+
+  modifyreg32(IMXRT_SRC_SRMR, SRC_SRMR_M4LOCKUP_MASK,
+              SRC_SRMR_M4LOCKUP_NONE);
+
+  priv->cfg       = cfg;
+  priv->rptun.ops = &g_imxrt_rptun_ops;
+
+  ret = imxrt_rptun_mu_init(priv);
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  ret = rptun_initialize(&priv->rptun);
+  if (ret < 0)
+    {
+      ipcerr("ERROR: rptun_initialize failed: %d\n", ret);
+      up_disable_irq(IMXRT_IRQ_MU);
+      irq_detach(IMXRT_IRQ_MU);
+      imxrt_clockoff_mu_b();
+      imxrt_clockoff_mu_a();
+      goto errout;
+    }
+
+  return OK;
+
+errout:
+  priv->cfg = NULL;
+  return ret;
+}
diff --git a/arch/arm/src/imxrt/imxrt_rptun.h b/arch/arm/src/imxrt/imxrt_rptun.h
new file mode 100644
index 00000000000..61c63f8a43a
--- /dev/null
+++ b/arch/arm/src/imxrt/imxrt_rptun.h
@@ -0,0 +1,83 @@
+/****************************************************************************
+ * arch/arm/src/imxrt/imxrt_rptun.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_ARM_SRC_IMXRT_IMXRT_RPTUN_H
+#define __ARCH_ARM_SRC_IMXRT_IMXRT_RPTUN_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#include <nuttx/rptun/rptun.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* Board-supplied description of the CM4 remote.  The CM7 is always the
+ * rptun master: it loads the CM4 ELF (whose .resource_table carries the
+ * vdev/vring description), fills in the vring addresses, then releases the
+ * CM4 from reset.  Kicks are coalesced in the MU mailbox, so the remote
+ * must read MU-B RR0 before it scans its virtqueues; a kick that arrives
+ * while the mailbox is full is covered by that scan.
+ */
+
+struct imxrt_rptun_config_s
+{
+  const char *cpuname;    /* Remote name, e.g. "cm4" -> /dev/rptun/cm4 */
+  const char *firmware;   /* ELF path passed to the rptun loader */
+
+  /* CM4 device address -> CM7 physical address map, terminated by
+   * an entry with size 0.  Must cover every ELF segment and the shared
+   * memory region.
+   */
+
+  const struct rptun_addrenv_s *addrenv;
+
+  uintptr_t boot_addr;    /* Value programmed into CM4_INIT_VTOR */
+  bool autostart;         /* Start the remote from rptun_initialize() */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: imxrt_rptun_init
+ *
+ * Description:
+ *   Register the CM4 as an rptun device.  cfg must stay valid for the life
+ *   of the system.  The board maps the memory the CM4 writes (vrings,
+ *   rpmsg buffers) as non-cacheable before calling this.  The remote is
+ *   not started; use RPTUNIOC_START on the character device or
+ *   rptun_boot(cpuname).
+ *
+ ****************************************************************************/
+
+int imxrt_rptun_init(const struct imxrt_rptun_config_s *cfg);
+
+#endif /* __ARCH_ARM_SRC_IMXRT_IMXRT_RPTUN_H */


(nuttx) 03/03: boards/arm/imxrt/fmu-v6xrt: Add an rptun configuration for the CM4.

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit a661949f8a567eae94368a1a988926aedf78827f
Author: Lourens Naude <lo...@bearmetal.eu>
AuthorDate: Thu Oct 8 12:59:51 2026 +0100

    boards/arm/imxrt/fmu-v6xrt: Add an rptun configuration for the CM4.
    
    The Pixhawk 6X-RT is an RT1176.  The rptun configuration registers the
    CM4 as /dev/rptun/cm4 from the board bring-up with the TCM memory map
    the LMEM backdoor window allows, maps the shared window non-cacheable
    and loads the CM4 ELF on rptun start.  The image comes from the SD card
    (CONFIG_FMU_V6XRT_CM4_FIRMWARE) or, with CONFIG_FMU_V6XRT_CM4_ROMFS,
    from the etc ROMFS built from src/etc/cm4.elf, so no card is needed.
    The nsh rptun builtin starts and stops the core and rpmsg ping
    /dev/rpmsg/cm4 exercises the link; syslog goes to a RAM log readable
    with dmesg.  The board page documents the configuration and the
    uploader.
    
    Assisted-by: Claude:claude-fable-5-1
    Signed-off-by: Lourens Naude <lo...@bearmetal.eu>
---
 .../platforms/arm/imxrt/boards/fmu-v6xrt/index.rst |  41 +++++++-
 Documentation/platforms/arm/imxrt/index.rst        |   9 ++
 boards/arm/imxrt/fmu-v6xrt/Kconfig                 |  22 +++++
 boards/arm/imxrt/fmu-v6xrt/configs/rptun/defconfig |  79 +++++++++++++++
 boards/arm/imxrt/fmu-v6xrt/src/.gitignore          |   3 +
 boards/arm/imxrt/fmu-v6xrt/src/CMakeLists.txt      |  17 ++++
 boards/arm/imxrt/fmu-v6xrt/src/Makefile            |   8 ++
 boards/arm/imxrt/fmu-v6xrt/src/fmu-v6xrt.h         |  13 +++
 boards/arm/imxrt/fmu-v6xrt/src/imxrt_bringup.c     |  10 ++
 boards/arm/imxrt/fmu-v6xrt/src/imxrt_cm4.c         | 106 +++++++++++++++++++++
 10 files changed, 304 insertions(+), 4 deletions(-)

diff --git a/Documentation/platforms/arm/imxrt/boards/fmu-v6xrt/index.rst b/Documentation/platforms/arm/imxrt/boards/fmu-v6xrt/index.rst
index d7740f5be9b..a8e5b1184da 100644
--- a/Documentation/platforms/arm/imxrt/boards/fmu-v6xrt/index.rst
+++ b/Documentation/platforms/arm/imxrt/boards/fmu-v6xrt/index.rst
@@ -92,14 +92,16 @@ Installation
 The toolchain is the same as for every i.MX RT board; see
 :doc:`the platform page <../../index>`.
 
-Flashing needs ``px_uploader.py`` from the PX4 source tree. The bootloader
-enumerates as a USB CDC/ACM device for roughly five seconds after reset, so
-start the uploader first and then reset the board:
+Flashing uses the uploader from the PX4 source tree, ``Tools/px4_uploader.py``.
+PX4 releases before 1.18 ship it as ``px_uploader.py``, without the ``4``, with
+the same arguments. The bootloader enumerates as a USB CDC/ACM device for
+roughly five seconds after reset, so start the uploader first and then reset
+the board:
 
 .. code-block:: console
 
    $ python Tools/px_mkfw.py --prototype firmware.prototype --image nuttx.bin > nuttx.px4
-   $ python Tools/px_uploader.py --port "/dev/serial/by-id/*PX4*" nuttx.px4
+   $ python Tools/px4_uploader.py --port "/dev/serial/by-id/*PX4*" nuttx.px4
 
 The uploader expects a ``.px4`` container rather than a raw binary.
 
@@ -113,3 +115,34 @@ The basic NuttShell configuration. The console is USB CDC/ACM, so the shell
 appears on the same connector used for flashing, at ``/dev/ttyACM0`` on the
 host. ``/dev/random`` and ``/dev/urandom`` are backed by the CAAM hardware
 entropy source.
+
+rptun
+-----
+
+``nsh`` plus the SD card and the RPTUN backend for the CM4
+(``CONFIG_IMXRT_RPTUN``). The board bring-up registers ``/dev/rptun/cm4`` and
+holds the core; ``rptun start`` loads the ELF named by
+``CONFIG_FMU_V6XRT_CM4_FIRMWARE`` (``/mnt/sd/cm4.elf``) into the CM4 code TCM
+and releases it. The image must carry a ``.resource_table`` section and link
+to ``0x1ffe0000`` with its shared memory in the upper 64 KB of that TCM; see
+the RPTUN section of :doc:`the platform page <../../index>`.
+
+.. code-block:: console
+
+   nsh> mount -t vfat /dev/mmcsd0 /mnt/sd
+   nsh> rptun start /dev/rptun/cm4
+   nsh> sleep 2
+   nsh> rpmsg ping /dev/rpmsg/cm4 100 64 3 0
+   nsh> rpmsg dump all
+   nsh> rptun stop /dev/rptun/cm4
+
+Start is asynchronous; the rpmsg node ``/dev/rpmsg/cm4`` appears once the CM4
+has announced its endpoints. ``rpmsg ping`` and ``rpmsg dump`` report through
+syslog, which this configuration keeps in a RAM log: read it with ``dmesg``.
+After ``rptun stop`` the CM4 is parked in a ``wfi`` loop at its boot address;
+the next start reloads the image.
+
+To run without an SD card, enable ``CONFIG_FMU_V6XRT_CM4_ROMFS`` and copy the CM4 image to
+``boards/arm/imxrt/fmu-v6xrt/src/etc/cm4.elf`` before building. It is baked into the etc ROMFS
+that NuttX mounts at boot and the firmware path becomes ``/etc/cm4.elf``, so the ``mount`` step
+goes away. The file is ignored by git.
diff --git a/Documentation/platforms/arm/imxrt/index.rst b/Documentation/platforms/arm/imxrt/index.rst
index 285f3330c9b..c430b981f49 100644
--- a/Documentation/platforms/arm/imxrt/index.rst
+++ b/Documentation/platforms/arm/imxrt/index.rst
@@ -326,6 +326,15 @@ The driver:
 - Starts the remote from :c:func:`rptun_initialize` when the board sets ``autostart``; otherwise
   ``RPTUNIOC_START`` on ``/dev/rptun/<name>`` or :c:func:`rptun_boot` starts it.
 
+The ``fmu-v6xrt:rptun`` configuration is the reference user: ``imxrt_cm4_initialize()`` in the board
+bring-up registers ``/dev/rptun/cm4`` with the memory map below, maps the shared window non-cacheable
+and leaves the core held. Copy an ELF built for the CM4 to the SD card as
+``CONFIG_FMU_V6XRT_CM4_FIRMWARE`` (``/mnt/sd/cm4.elf``), then from nsh::
+
+    mount -t vfat /dev/mmcsd0 /mnt/sd
+    rptun start /dev/rptun/cm4
+    rptun ping /dev/rptun/cm4 100 64 3 0
+
 Memory map rules the board must follow:
 
 - The CM7 reaches the CM4 TCM through the LMEM backdoor window at ``IMXRT_OCRAM_M4_BASE``. Only the
diff --git a/boards/arm/imxrt/fmu-v6xrt/Kconfig b/boards/arm/imxrt/fmu-v6xrt/Kconfig
index 5c6bd455634..cae2444c7ce 100644
--- a/boards/arm/imxrt/fmu-v6xrt/Kconfig
+++ b/boards/arm/imxrt/fmu-v6xrt/Kconfig
@@ -35,4 +35,26 @@ config IMXRT1170_FLEXSPI_FRAM
 	depends on IMXRT_FLEXSPI
 	select MTD
 
+config FMU_V6XRT_CM4_ROMFS
+	bool "Embed the CM4 firmware in the etc ROMFS"
+	default n
+	depends on IMXRT_RPTUN
+	select FS_ROMFS
+	select ETC_ROMFS
+	---help---
+		Build boards/arm/imxrt/fmu-v6xrt/src/etc/cm4.elf into the etc
+		ROMFS that is mounted at boot, so no SD card is needed.  The file
+		is not part of the tree; copy the CM4 image there before building.
+
+config FMU_V6XRT_CM4_FIRMWARE
+	string "CM4 firmware path"
+	default "/mnt/sd/cm4.elf"
+	depends on IMXRT_RPTUN && !FMU_V6XRT_CM4_ROMFS
+	---help---
+		ELF image the RPTUN loader writes into the CM4 TCM on
+		"rptun start /dev/rptun/cm4".  It must carry a .resource_table
+		section and link against the CM4 code TCM at 0x1ffe0000, with
+		its shared memory in the upper 64 KB of that TCM.  With
+		FMU_V6XRT_CM4_ROMFS the path is /etc/cm4.elf.
+
 endif
diff --git a/boards/arm/imxrt/fmu-v6xrt/configs/rptun/defconfig b/boards/arm/imxrt/fmu-v6xrt/configs/rptun/defconfig
new file mode 100644
index 00000000000..17c9279d2e7
--- /dev/null
+++ b/boards/arm/imxrt/fmu-v6xrt/configs/rptun/defconfig
@@ -0,0 +1,79 @@
+#
+# This file is autogenerated: PLEASE DO NOT EDIT IT.
+#
+# You can use "make menuconfig" to make any modifications to the installed .config file.
+# You can then do "make savedefconfig" to generate a new defconfig file that includes your
+# modifications.
+#
+# CONFIG_ARCH_RAMFUNCS is not set
+# CONFIG_NSH_DISABLE_DATE is not set
+# CONFIG_NSH_DISABLE_MB is not set
+# CONFIG_NSH_DISABLE_MH is not set
+# CONFIG_NSH_DISABLE_MW is not set
+CONFIG_ARCH="arm"
+CONFIG_ARCH_BOARD="fmu-v6xrt"
+CONFIG_ARCH_BOARD_FMU_V6XRT=y
+CONFIG_ARCH_CHIP="imxrt"
+CONFIG_ARCH_CHIP_IMXRT=y
+CONFIG_ARCH_CHIP_MIMXRT1176DVMAA=y
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_ARMV7M_DCACHE=y
+CONFIG_ARMV7M_DTCM=y
+CONFIG_ARMV7M_ICACHE=y
+CONFIG_ARMV7M_ITCM=y
+CONFIG_ARM_MPU=y
+CONFIG_BINFMT_DISABLE=y
+CONFIG_BOARD_INITRNGSEED=y
+CONFIG_BOARD_LOOPSPERMSEC=104926
+CONFIG_BUILTIN=y
+CONFIG_CDCACM=y
+CONFIG_CDCACM_CONSOLE=y
+CONFIG_CRYPTO=y
+CONFIG_CRYPTO_RANDOM_POOL=y
+CONFIG_DEBUG_FEATURES=y
+CONFIG_DEBUG_IPC=y
+CONFIG_DEBUG_IPC_ERROR=y
+CONFIG_DEBUG_IPC_WARN=y
+CONFIG_DEV_URANDOM=y
+CONFIG_FAT_LFN=y
+CONFIG_FS_FAT=y
+CONFIG_FS_FATTIME=y
+CONFIG_FS_PROCFS=y
+CONFIG_IDLETHREAD_STACKSIZE=2048
+CONFIG_IMXRT_DTCM=0
+CONFIG_IMXRT_ITCM=0
+CONFIG_IMXRT_LPUART1=y
+CONFIG_IMXRT_RNG=y
+CONFIG_IMXRT_RPTUN=y
+CONFIG_IMXRT_USBDEV=y
+CONFIG_IMXRT_USDHC1=y
+CONFIG_IMXRT_USDHC1_INVERT_CD=y
+CONFIG_IMXRT_USDHC1_WIDTH_D1_D4=y
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_INTELHEX_BINARY=y
+CONFIG_LIBC_LOCALTIME=y
+CONFIG_LINE_MAX=64
+CONFIG_MMCSD=y
+CONFIG_MMCSD_SDIO=y
+CONFIG_NSH_BUILTIN_APPS=y
+CONFIG_NSH_DISABLE_IFUPDOWN=y
+CONFIG_NSH_FILEIOSIZE=512
+CONFIG_NSH_READLINE=y
+CONFIG_NSH_USBCONSOLE=y
+CONFIG_RAMLOG=y
+CONFIG_RAMLOG_BUFSIZE=8192
+CONFIG_RAMLOG_SYSLOG=y
+CONFIG_RAM_SIZE=524288
+CONFIG_RAM_START=0x20240000
+CONFIG_RAW_BINARY=y
+CONFIG_RPMSG_PING=y
+CONFIG_RPTUN=y
+CONFIG_SCHED_BACKTRACE=y
+CONFIG_SCHED_HPWORK=y
+CONFIG_SCHED_HPWORKPRIORITY=249
+CONFIG_SCHED_LPWORK=y
+CONFIG_SDIO_BLOCKSETUP=y
+CONFIG_START_DAY=14
+CONFIG_START_MONTH=3
+CONFIG_SYSTEM_NSH=y
+CONFIG_USBDEV=y
diff --git a/boards/arm/imxrt/fmu-v6xrt/src/.gitignore b/boards/arm/imxrt/fmu-v6xrt/src/.gitignore
new file mode 100644
index 00000000000..456fdd03d62
--- /dev/null
+++ b/boards/arm/imxrt/fmu-v6xrt/src/.gitignore
@@ -0,0 +1,3 @@
+/etc/cm4.elf
+/etctmp
+/etctmp.c
diff --git a/boards/arm/imxrt/fmu-v6xrt/src/CMakeLists.txt b/boards/arm/imxrt/fmu-v6xrt/src/CMakeLists.txt
index 22596a36b9c..3a2aeebc93d 100644
--- a/boards/arm/imxrt/fmu-v6xrt/src/CMakeLists.txt
+++ b/boards/arm/imxrt/fmu-v6xrt/src/CMakeLists.txt
@@ -23,8 +23,25 @@
 set(SRCS imxrt_boot.c imxrt_flexspi_nor_boot.c imxrt_flexspi_nor_flash.c
          imxrt_clockconfig.c imxrt_bringup.c)
 
+if(CONFIG_IMXRT_RPTUN)
+  list(APPEND SRCS imxrt_cm4.c)
+endif()
+
 target_sources(board PRIVATE ${SRCS})
 
+if(CONFIG_FMU_V6XRT_CM4_ROMFS)
+  nuttx_add_romfs(
+    NAME
+    etc
+    MOUNTPOINT
+    etc
+    RCRAWS
+    etc/cm4.elf
+    PATH
+    ${CMAKE_CURRENT_BINARY_DIR}/etc)
+  target_link_libraries(board PRIVATE romfs_etc)
+endif()
+
 if(CONFIG_BOOT_RUNFROMFLASH)
   set_property(GLOBAL PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/flash.ld")
 elseif(CONFIG_BOOT_RUNFROMISRAM)
diff --git a/boards/arm/imxrt/fmu-v6xrt/src/Makefile b/boards/arm/imxrt/fmu-v6xrt/src/Makefile
index f11b15bd63a..1ef687f9f31 100644
--- a/boards/arm/imxrt/fmu-v6xrt/src/Makefile
+++ b/boards/arm/imxrt/fmu-v6xrt/src/Makefile
@@ -31,4 +31,12 @@ else ifeq ($(CONFIG_BOARD_LATE_INITIALIZE),y)
 CSRCS += imxrt_bringup.c
 endif
 
+ifeq ($(CONFIG_IMXRT_RPTUN),y)
+CSRCS += imxrt_cm4.c
+endif
+
+ifeq ($(CONFIG_FMU_V6XRT_CM4_ROMFS),y)
+RCRAWS = etc/cm4.elf
+endif
+
 include $(TOPDIR)/boards/Board.mk
diff --git a/boards/arm/imxrt/fmu-v6xrt/src/fmu-v6xrt.h b/boards/arm/imxrt/fmu-v6xrt/src/fmu-v6xrt.h
index 09e2401339b..d96b64ce70b 100644
--- a/boards/arm/imxrt/fmu-v6xrt/src/fmu-v6xrt.h
+++ b/boards/arm/imxrt/fmu-v6xrt/src/fmu-v6xrt.h
@@ -337,5 +337,18 @@ int imxrt_flexspi_nor_initialize(void);
 int imxrt_flexspi_fram_initialize(void);
 #endif
 
+/****************************************************************************
+ * Name: imxrt_cm4_initialize
+ *
+ * Description:
+ *   Register the CM4 as an RPTUN remote (/dev/rptun/cm4) and map its shared
+ *   window non-cacheable.  The core is started with "rptun start".
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IMXRT_RPTUN
+int imxrt_cm4_initialize(void);
+#endif
+
 #endif /* __ASSEMBLY__ */
 #endif /* __BOARDS_ARM_IMXRT_FMU_V6XRT_SRC_FMU_V6XRT_H */
diff --git a/boards/arm/imxrt/fmu-v6xrt/src/imxrt_bringup.c b/boards/arm/imxrt/fmu-v6xrt/src/imxrt_bringup.c
index c8aaed2b7ea..2a4f7be8a7e 100644
--- a/boards/arm/imxrt/fmu-v6xrt/src/imxrt_bringup.c
+++ b/boards/arm/imxrt/fmu-v6xrt/src/imxrt_bringup.c
@@ -168,6 +168,16 @@ int imxrt_bringup(void)
   nsh_sdmmc_initialize();
 #endif
 
+#ifdef CONFIG_IMXRT_RPTUN
+  /* Register the CM4 as an RPTUN remote; "rptun start" loads and runs it */
+
+  ret = imxrt_cm4_initialize();
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: imxrt_cm4_initialize failed: %d\n", ret);
+    }
+#endif
+
 #ifdef CONFIG_IMXRT_LPSPI
   imxrt_spidev_initialize();
 #endif
diff --git a/boards/arm/imxrt/fmu-v6xrt/src/imxrt_cm4.c b/boards/arm/imxrt/fmu-v6xrt/src/imxrt_cm4.c
new file mode 100644
index 00000000000..c1be629cbe0
--- /dev/null
+++ b/boards/arm/imxrt/fmu-v6xrt/src/imxrt_cm4.c
@@ -0,0 +1,106 @@
+/****************************************************************************
+ * boards/arm/imxrt/fmu-v6xrt/src/imxrt_cm4.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/rptun/rptun.h>
+
+#include "arm_internal.h"
+#include "mpu.h"
+#include "imxrt_rptun.h"
+#include "hardware/imxrt_memorymap.h"
+#include "fmu-v6xrt.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The CM4 tightly coupled memories as the CM4 sees them (device addresses)
+ * and as the CM7 reaches them through the LMEM backdoor window (physical
+ * addresses).  Only the code TCM half of the window is reachable, so the
+ * CM4 image, its resource table, the vrings and the rpmsg buffers all live
+ * there: code in the lower 64 KB, shared memory in the upper 64 KB.  The
+ * CM4 boots from the backdoor alias; the CM4-native address does not boot.
+ */
+
+#define CM4_CODE_TCM_DA     0x1ffe0000
+#define CM4_SYS_TCM_DA      0x20000000
+#define CM4_TCM_SIZE        (128 * 1024)
+#define CM4_CODE_TCM_PA     IMXRT_OCRAM_M4_BASE
+#define CM4_SYS_TCM_PA      (IMXRT_OCRAM_M4_BASE + CM4_TCM_SIZE)
+#define CM4_SHM_PA          (CM4_CODE_TCM_PA + (64 * 1024))
+#define CM4_SHM_SIZE        (64 * 1024)
+
+#ifdef CONFIG_FMU_V6XRT_CM4_ROMFS
+#  define CM4_FIRMWARE      "/etc/cm4.elf"
+#else
+#  define CM4_FIRMWARE      CONFIG_FMU_V6XRT_CM4_FIRMWARE
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct rptun_addrenv_s g_cm4_addrenv[] =
+{
+  { .pa = CM4_CODE_TCM_PA, .da = CM4_CODE_TCM_DA, .size = CM4_TCM_SIZE },
+  { .pa = CM4_SYS_TCM_PA,  .da = CM4_SYS_TCM_DA,  .size = CM4_TCM_SIZE },
+  { .size = 0 },
+};
+
+static const struct imxrt_rptun_config_s g_cm4_config =
+{
+  .cpuname   = "cm4",
+  .firmware  = CM4_FIRMWARE,
+  .addrenv   = g_cm4_addrenv,
+  .boot_addr = CM4_CODE_TCM_PA,
+  .autostart = false,
+};
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: imxrt_cm4_initialize
+ *
+ * Description:
+ *   Register the CM4 as /dev/rptun/cm4.  The CM4 writes the vrings and the
+ *   rpmsg buffers, so the CM7 must not read them through its D-cache: map
+ *   the shared window Normal, non-cacheable and shareable.  The region is
+ *   added after imxrt_mpu_initialize() and therefore wins over the
+ *   cacheable OCRAM_M4 region it overlaps.
+ *
+ ****************************************************************************/
+
+int imxrt_cm4_initialize(void)
+{
+  mpu_configure_region(CM4_SHM_PA, CM4_SHM_SIZE,
+                       MPU_RASR_AP_RWRW | MPU_RASR_TEX_NOR |
+                       MPU_RASR_S | MPU_RASR_XN);
+
+  return imxrt_rptun_init(&g_cm4_config);
+}