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 4818db43124d501adb236d41b0a6d6daa3a9b78b
Author: qinwei1 <[email protected]>
AuthorDate: Sun Jun 7 20:27:38 2026 +0800

    drivers/devicetree: register fdt_ctucanfd and fdt_pl061
    
    search the device tree and register fdt_ctucanfd and fdt_pl061
    
    Signed-off-by: qinwei1 <[email protected]>
---
 drivers/devicetree/CMakeLists.txt                  |   8 ++
 drivers/devicetree/Make.defs                       |   8 ++
 .../devicetree/fdt_ctucanfd.c                      |  75 ++++++-----
 drivers/devicetree/fdt_pl061.c                     | 150 +++++++++++++++++++++
 include/nuttx/can/ctucanfd_pci.h                   |   2 +
 include/nuttx/fdt.h                                |  42 ++++++
 6 files changed, 252 insertions(+), 33 deletions(-)

diff --git a/drivers/devicetree/CMakeLists.txt 
b/drivers/devicetree/CMakeLists.txt
index 9779cfecf83..3ae58751f7f 100644
--- a/drivers/devicetree/CMakeLists.txt
+++ b/drivers/devicetree/CMakeLists.txt
@@ -53,6 +53,14 @@ if(CONFIG_DEVICE_TREE)
     list(APPEND SRCS fdt_sifive_pwm.c)
   endif()
 
+  if(CONFIG_IOEXPANDER_PL061)
+    list(APPEND SRCS fdt_pl061.c)
+  endif()
+
+  if(CONFIG_CAN_CTUCANFD)
+    list(APPEND SRCS fdt_ctucanfd.c)
+  endif()
+
   target_include_directories(drivers
                              PRIVATE ${NUTTX_DIR}/libs/libc/fdt/dtc/libfdt)
   target_sources(drivers PRIVATE ${SRCS})
diff --git a/drivers/devicetree/Make.defs b/drivers/devicetree/Make.defs
index 3d294f421d4..d0da776ebcc 100644
--- a/drivers/devicetree/Make.defs
+++ b/drivers/devicetree/Make.defs
@@ -57,6 +57,14 @@ ifeq ($(CONFIG_PWM_SIFIVE),y)
   CSRCS += fdt_sifive_pwm.c
 endif
 
+ifeq ($(CONFIG_IOEXPANDER_PL061),y)
+  CSRCS += fdt_pl061.c
+endif
+
+ifeq ($(CONFIG_CAN_CTUCANFD),y)
+  CSRCS += fdt_ctucanfd.c
+endif
+
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)libs$(DELIM)libc$(DELIM)fdt$(DELIM)dtc$(DELIM)libfdt
 
 DEPPATH += --dep-path devicetree
diff --git a/include/nuttx/can/ctucanfd_pci.h 
b/drivers/devicetree/fdt_ctucanfd.c
similarity index 57%
copy from include/nuttx/can/ctucanfd_pci.h
copy to drivers/devicetree/fdt_ctucanfd.c
index 2b3404ded66..ab7046e2c3f 100644
--- a/include/nuttx/can/ctucanfd_pci.h
+++ b/drivers/devicetree/fdt_ctucanfd.c
@@ -1,7 +1,5 @@
 /****************************************************************************
- * include/nuttx/can/ctucanfd_pci.h
- *
- * SPDX-License-Identifier: Apache-2.0
+ * drivers/devicetree/fdt_ctucanfd.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -20,55 +18,66 @@
  *
  ****************************************************************************/
 
-#ifndef __INCLUDE_NUTTX_CAN_CTUCANFD_H
-#define __INCLUDE_NUTTX_CAN_CTUCANFD_H
-
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
+#include <debug.h>
+#include <errno.h>
 
-#ifdef __cplusplus
-extern "C"
-{
-#endif
+#include <nuttx/can/ctucanfd_pci.h>
+#include <nuttx/fdt.h>
 
-/****************************************************************************
- * Public Function Prototypes
- ****************************************************************************/
+#include <libfdt.h>
 
 /****************************************************************************
- * Name: pci_ctucanfd_init
- *
- * Description:
- *   Register a pci driver
- *
+ * Public Functions
  ****************************************************************************/
 
-int pci_ctucanfd_init(void);
-
 /****************************************************************************
- * Name: ctucanfd_register
+ * Name: fdt_ctucanfd_devices_register
  *
  * Description:
- *   Register a CTU CAN FD device via SysBus MMIO (without PCI).
- *   Used on platforms where the CAN controller is memory-mapped directly,
- *   such as the QEMU ARM virt platform with ctucan-sysbus.
+ *   Scan the device tree for nodes with compatible "ctu,ctucanfd" and
+ *   register each via ctucanfd_register() (SysBus / MMIO path, no PCI).
  *
  * Input Parameters:
- *   base - MMIO base address of the CTU CAN FD controller
- *   irq  - IRQ number
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number (added to FDT-supplied SPI index)
  *
  * Returned Value:
- *   Zero on success; a negated errno on failure
+ *   Return OK on success; a negated errno on failure.
  *
  ****************************************************************************/
 
-int ctucanfd_register(uintptr_t base, int irq);
+int fdt_ctucanfd_devices_register(FAR const void *fdt, int irqbase)
+{
+  uintptr_t addr;
+  int offset = -1;
+  int irqnum;
+  int ret;
 
-#ifdef __cplusplus
-}
-#endif
+  for (; ; )
+    {
+      offset = fdt_node_offset_by_compatible(fdt, offset, "ctu,ctucanfd");
+      if (offset == -FDT_ERR_NOTFOUND)
+        {
+          break;
+        }
 
-#endif /* __INCLUDE_NUTTX_CAN_CTUCANFD_H */
+      addr = fdt_get_reg_base(fdt, offset, 0);
+      irqnum = fdt_get_irq(fdt, offset, 1, irqbase);
+      if (addr == 0 || irqnum < 0)
+        {
+          return -EINVAL;
+        }
+
+      ret = ctucanfd_register(addr, irqnum);
+      if (ret < 0 && ret != -ENODEV)
+        {
+          return ret;
+        }
+    }
+
+  return OK;
+}
diff --git a/drivers/devicetree/fdt_pl061.c b/drivers/devicetree/fdt_pl061.c
new file mode 100644
index 00000000000..34a9c5cbfed
--- /dev/null
+++ b/drivers/devicetree/fdt_pl061.c
@@ -0,0 +1,150 @@
+/****************************************************************************
+ * drivers/devicetree/fdt_pl061.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 <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <string.h>
+
+#include <nuttx/fdt.h>
+#include <nuttx/ioexpander/gpio.h>
+#include <nuttx/ioexpander/ioexpander.h>
+#include <nuttx/ioexpander/pl061.h>
+
+#include <libfdt.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* PL061 has 8 GPIO lines per instance */
+
+#define PL061_NPINS 8
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_pl061_devices_register
+ *
+ * Description:
+ *   Scan the device tree for nodes with compatible "arm,pl061" and
+ *   register each as an ioexpander via pl061_ioe_initialize(), then
+ *   expose all 8 pins as standard /dev/gpioN character devices through
+ *   gpio_lower_half().
+ *
+ *   Pins are initially registered as input (GPIO_INPUT_PIN); applications
+ *   may switch them to output via ioctl(GPIOC_SETPINTYPE).
+ *
+ * Input Parameters:
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number (added to FDT-supplied SPI index)
+ *
+ * Returned Value:
+ *   Return OK on success; a negated errno on failure.
+ *
+ ****************************************************************************/
+
+int fdt_pl061_devices_register(FAR const void *fdt, int irqbase)
+{
+  FAR struct ioexpander_dev_s *dev;
+  FAR const char *status;
+  uintptr_t addr;
+  int offset = -1;
+  int irqnum;
+#ifdef CONFIG_GPIO_LOWER_HALF
+  int minor = 0;
+  int i;
+#endif
+
+  for (; ; )
+    {
+      offset = fdt_node_offset_by_compatible(fdt, offset, "arm,pl061");
+      if (offset == -FDT_ERR_NOTFOUND)
+        {
+          break;
+        }
+
+      /* Honor the standard device-tree 'status' property: skip nodes
+       * marked "disabled" (i.e., not usable in this execution context).
+       *
+       * On the QEMU 'virt' machine there are two PL061 instances:
+       *
+       *   - Normal-world PL061 (@9030000):
+       *       status = "okay"
+       *       Used as general-purpose GPIO. pin 3 is the power-button
+       *       input (gpio-keys), other pins are free.
+       *
+       *   - Secure-world PL061 (@90b0000):
+       *       status         = "disabled"   <-- visible here, NS world
+       *       secure-status  = "okay"       <-- only S world should use
+       *       Wired internally to gpio-poweroff (pin 0) and gpio-restart
+       *       (pin 1). Driving these pins HIGH from the normal world
+       *       would shut down or reset the whole machine.
+       *
+       * Without this check we would register /dev/gpio0../dev/gpio7
+       * against the secure controller, and the first ioctl(GPIOC_WRITE)
+       * with value=1 on /dev/gpio0 would immediately power the VM off.
+       */
+
+      status = fdt_getprop(fdt, offset, "status", NULL);
+      if (status != NULL && strcmp(status, "disabled") == 0)
+        {
+          continue;
+        }
+
+      addr = fdt_get_reg_base(fdt, offset, 0);
+      irqnum = fdt_get_irq(fdt, offset, 1, irqbase);
+      if (addr == 0 || irqnum < 0)
+        {
+          return -EINVAL;
+        }
+
+      dev = pl061_ioe_initialize(addr, irqnum);
+      if (dev == NULL)
+        {
+          return -ENODEV;
+        }
+
+      /* Expose each pin as /dev/gpioN. Initial type is INPUT;
+       * applications switch via ioctl(GPIOC_SETPINTYPE).
+       *
+       * Only available when the GPIO lower-half (CONFIG_GPIO_LOWER_HALF)
+       * is enabled. Otherwise the PL061 is still registered as an
+       * ioexpander above, just not exposed as /dev/gpioN char devices.
+       */
+
+#ifdef CONFIG_GPIO_LOWER_HALF
+      for (i = 0; i < PL061_NPINS; i++)
+        {
+          gpio_lower_half(dev, i, GPIO_INPUT_PIN, minor++);
+        }
+#endif
+    }
+
+  return OK;
+}
diff --git a/include/nuttx/can/ctucanfd_pci.h b/include/nuttx/can/ctucanfd_pci.h
index 2b3404ded66..04d4f5aa3d1 100644
--- a/include/nuttx/can/ctucanfd_pci.h
+++ b/include/nuttx/can/ctucanfd_pci.h
@@ -29,6 +29,8 @@
 
 #include <nuttx/config.h>
 
+#include <stdint.h>
+
 #ifdef __cplusplus
 extern "C"
 {
diff --git a/include/nuttx/fdt.h b/include/nuttx/fdt.h
index f4b4fe98254..fe004c4796a 100644
--- a/include/nuttx/fdt.h
+++ b/include/nuttx/fdt.h
@@ -610,4 +610,46 @@ int fdt_qemu_fwcfg_register(FAR const void *fdt);
 #ifdef CONFIG_PWM_SIFIVE
 int fdt_sifive_pwm_devices_register(FAR const void *fdt);
 #endif
+
+/****************************************************************************
+ * Name: fdt_pl061_devices_register
+ *
+ * Description:
+ *   This function scans the device tree for nodes with compatible
+ *   "arm,pl061" and registers each as an ioexpander via
+ *   pl061_ioe_initialize().
+ *
+ * Input Parameters:
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IOEXPANDER_PL061
+int fdt_pl061_devices_register(FAR const void *fdt, int irqbase);
+#endif
+
+/****************************************************************************
+ * Name: fdt_ctucanfd_devices_register
+ *
+ * Description:
+ *   This function scans the device tree for nodes with compatible
+ *   "ctu,ctucanfd" and registers each via ctucanfd_register()
+ *   (SysBus/MMIO path, no PCI).
+ *
+ * Input Parameters:
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_CAN_CTUCANFD
+int fdt_ctucanfd_devices_register(FAR const void *fdt, int irqbase);
+#endif
 #endif /* __INCLUDE_NUTTX_FDT_H */

Reply via email to