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 4a6b4a4127810414469ecdbed85ddae1d85ba3ca
Author: qinwei1 <[email protected]>
AuthorDate: Wed Aug 12 09:32:41 2026 +0800

    drivers/devicetree: FDT auto-register for SP804 timer and CMSDK watchdog
    
    - Add fdt_sp804_devices_register() and fdt_cmsdk_wdt_devices_register(),
      which scan the device tree for "arm,sp804" / "arm,cmsdk-wdt" nodes and
      register each as /dev/timerN / /dev/watchdogN.
    - Wire both into qemu-armv8a bringup so no hard-coded board init is needed.
      Guarded by their respective configs (TIMER+TIMER_SP804, WATCHDOG_CMSDK).
    
    Signed-off-by: qinwei1 <[email protected]>
---
 boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c |   8 ++
 drivers/devicetree/CMakeLists.txt                |   8 ++
 drivers/devicetree/Make.defs                     |  10 ++
 drivers/devicetree/fdt_cmsdk_wdt.c               | 128 +++++++++++++++++++++
 drivers/devicetree/fdt_sp804.c                   | 136 +++++++++++++++++++++++
 include/nuttx/fdt.h                              |  42 +++++++
 6 files changed, 332 insertions(+)

diff --git a/boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c 
b/boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c
index 87a232361e6..b5f805959bb 100644
--- a/boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c
+++ b/boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c
@@ -81,6 +81,14 @@ static void register_devices_from_fdt(void)
     }
 #endif
 
+#if defined(CONFIG_TIMER) && defined(CONFIG_TIMER_SP804)
+  fdt_sp804_devices_register(fdt, QEMU_SPI_IRQ_BASE);
+#endif
+
+#ifdef CONFIG_WATCHDOG_CMSDK
+  fdt_cmsdk_wdt_devices_register(fdt, QEMU_SPI_IRQ_BASE);
+#endif
+
   UNUSED(ret);
 }
 
diff --git a/drivers/devicetree/CMakeLists.txt 
b/drivers/devicetree/CMakeLists.txt
index 2f9c9391af4..4f84b9ec842 100644
--- a/drivers/devicetree/CMakeLists.txt
+++ b/drivers/devicetree/CMakeLists.txt
@@ -34,6 +34,14 @@ if(CONFIG_DEVICE_TREE)
     list(APPEND SRCS fdt_cfi.c)
   endif()
 
+  if(CONFIG_TIMER_SP804 AND CONFIG_TIMER)
+    list(APPEND SRCS fdt_sp804.c)
+  endif()
+
+  if(CONFIG_WATCHDOG_CMSDK)
+    list(APPEND SRCS fdt_cmsdk_wdt.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 b67aadd581f..67dfa55f06e 100644
--- a/drivers/devicetree/Make.defs
+++ b/drivers/devicetree/Make.defs
@@ -36,6 +36,16 @@ ifeq ($(CONFIG_MTD_CFI),y)
   CSRCS += fdt_cfi.c
 endif
 
+ifeq ($(CONFIG_TIMER_SP804),y)
+ifeq ($(CONFIG_TIMER),y)
+  CSRCS += fdt_sp804.c
+endif
+endif
+
+ifeq ($(CONFIG_WATCHDOG_CMSDK),y)
+  CSRCS += fdt_cmsdk_wdt.c
+endif
+
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)libs$(DELIM)libc$(DELIM)fdt$(DELIM)dtc$(DELIM)libfdt
 
 DEPPATH += --dep-path devicetree
diff --git a/drivers/devicetree/fdt_cmsdk_wdt.c 
b/drivers/devicetree/fdt_cmsdk_wdt.c
new file mode 100644
index 00000000000..40a1453a596
--- /dev/null
+++ b/drivers/devicetree/fdt_cmsdk_wdt.c
@@ -0,0 +1,128 @@
+/****************************************************************************
+ * drivers/devicetree/fdt_cmsdk_wdt.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 <debug.h>
+#include <errno.h>
+#include <stdio.h>
+
+#include <nuttx/fdt.h>
+#include <nuttx/timers/cmsdk_wdt.h>
+
+#include <libfdt.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Fallback WDOGCLK if the device tree does not carry a resolvable rate. */
+
+#define CMSDK_WDT_DEFAULT_FREQ 24000000
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Read a node's single-cell "clock-frequency" (Hz).  Falls back to
+ * CMSDK_WDT_DEFAULT_FREQ when the property is absent, unreadable or zero.
+ */
+
+static uint32_t fdt_cmsdk_wdt_clock_hz(FAR const void *fdt, int offset)
+{
+  int len;
+  FAR const fdt32_t *pv = fdt_getprop(fdt, offset, "clock-frequency", &len);
+  uint32_t freq;
+
+  if (pv == NULL || len < (int)sizeof(fdt32_t))
+    {
+      return CMSDK_WDT_DEFAULT_FREQ;
+    }
+
+  freq = fdt32_ld(pv);
+  return freq != 0 ? freq : CMSDK_WDT_DEFAULT_FREQ;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_cmsdk_wdt_devices_register
+ *
+ * Description:
+ *   Scan the device tree for "arm,cmsdk-wdt" nodes and register each as a
+ *   watchdog character device (/dev/watchdog0, /dev/watchdog1, ...).
+ *
+ * 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_cmsdk_wdt_devices_register(FAR const void *fdt, int irqbase)
+{
+  uintptr_t addr;
+  uintptr_t freq;
+  char devpath[32];
+  int offset = -1;
+  int irqnum;
+  int minor = 0;
+  int ret;
+
+  for (; ; )
+    {
+      offset = fdt_node_offset_by_compatible(fdt, offset, "arm,cmsdk-wdt");
+      if (offset == -FDT_ERR_NOTFOUND)
+        {
+          break;
+        }
+
+      addr = fdt_get_reg_base(fdt, offset, 0);
+      irqnum = fdt_get_irq(fdt, offset, 1, irqbase);
+      if (addr == 0 || irqnum < 0)
+        {
+          return -EINVAL;
+        }
+
+      /* Read the single-cell "clock-frequency" directly (see fdt_sp804.c
+       * for why the generic helper is unsuitable on this board); the helper
+       * falls back to CMSDK_WDT_DEFAULT_FREQ when it is absent or zero.
+       */
+
+      freq = fdt_cmsdk_wdt_clock_hz(fdt, offset);
+
+      snprintf(devpath, sizeof(devpath), "/dev/watchdog%d", minor++);
+      ret = cmsdk_wdt_initialize(devpath, addr, irqnum, (uint32_t)freq);
+      if (ret < 0)
+        {
+          return ret;
+        }
+    }
+
+  return OK;
+}
diff --git a/drivers/devicetree/fdt_sp804.c b/drivers/devicetree/fdt_sp804.c
new file mode 100644
index 00000000000..c35d9f8275f
--- /dev/null
+++ b/drivers/devicetree/fdt_sp804.c
@@ -0,0 +1,136 @@
+/****************************************************************************
+ * drivers/devicetree/fdt_sp804.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 <debug.h>
+#include <errno.h>
+#include <stdio.h>
+
+#include <nuttx/fdt.h>
+#include <nuttx/timers/timer.h>
+#include <nuttx/timers/sp804.h>
+
+#include <libfdt.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Fallback clock if the device tree does not carry a resolvable rate. */
+
+#define SP804_DEFAULT_FREQ 24000000
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Read a node's single-cell "clock-frequency" (Hz).  Falls back to
+ * SP804_DEFAULT_FREQ when the property is absent, unreadable or zero.
+ */
+
+static uint32_t fdt_sp804_clock_hz(FAR const void *fdt, int offset)
+{
+  int len;
+  FAR const fdt32_t *pv = fdt_getprop(fdt, offset, "clock-frequency", &len);
+  uint32_t freq;
+
+  if (pv == NULL || len < (int)sizeof(fdt32_t))
+    {
+      return SP804_DEFAULT_FREQ;
+    }
+
+  freq = fdt32_ld(pv);
+  return freq != 0 ? freq : SP804_DEFAULT_FREQ;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_sp804_devices_register
+ *
+ * Description:
+ *   Scan the device tree for "arm,sp804" nodes and register each as a
+ *   periodic timer character device (/dev/timer0, /dev/timer1, ...).
+ *
+ * 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_sp804_devices_register(FAR const void *fdt, int irqbase)
+{
+  FAR struct timer_lowerhalf_s *lower;
+  uintptr_t addr;
+  uintptr_t freq;
+  char devpath[32];
+  int offset = -1;
+  int irqnum;
+  int minor = 0;
+
+  for (; ; )
+    {
+      offset = fdt_node_offset_by_compatible(fdt, offset, "arm,sp804");
+      if (offset == -FDT_ERR_NOTFOUND)
+        {
+          break;
+        }
+
+      addr = fdt_get_reg_base(fdt, offset, 0);
+      irqnum = fdt_get_irq(fdt, offset, 1, irqbase);
+      if (addr == 0 || irqnum < 0)
+        {
+          return -EINVAL;
+        }
+
+      /* Read the single-cell "clock-frequency" directly.  The generic
+       * fdt_get_clock_frequency() helper loads it using the parent's
+       * #address-cells (2 on this board), which corrupts a 1-cell value,
+       * so parse it explicitly here.  The helper falls back to
+       * SP804_DEFAULT_FREQ when the property is absent or zero.
+       */
+
+      freq = fdt_sp804_clock_hz(fdt, offset);
+
+      lower = sp804_timer_initialize(addr, irqnum, freq);
+      if (lower == NULL)
+        {
+          return -ENODEV;
+        }
+
+      snprintf(devpath, sizeof(devpath), "/dev/timer%d", minor++);
+      if (timer_register(devpath, lower) == NULL)
+        {
+          return -EEXIST;
+        }
+    }
+
+  return OK;
+}
diff --git a/include/nuttx/fdt.h b/include/nuttx/fdt.h
index af2fda728db..b0aa9b418cf 100644
--- a/include/nuttx/fdt.h
+++ b/include/nuttx/fdt.h
@@ -508,4 +508,46 @@ int fdt_virtio_mmio_devices_register(FAR const void *fdt, 
int irqbase);
 #ifdef CONFIG_MTD_CFI
 int fdt_cfi_register(FAR const void *fdt);
 #endif
+
+/****************************************************************************
+ * Name: fdt_sp804_devices_register
+ *
+ * Description:
+ *   This function scans the device tree for nodes with compatible
+ *   "arm,sp804" and registers each as a periodic timer character device
+ *   (/dev/timerN).
+ *
+ * Input Parameters:
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_TIMER) && defined(CONFIG_TIMER_SP804)
+int fdt_sp804_devices_register(FAR const void *fdt, int irqbase);
+#endif
+
+/****************************************************************************
+ * Name: fdt_cmsdk_wdt_devices_register
+ *
+ * Description:
+ *   This function scans the device tree for nodes with compatible
+ *   "arm,cmsdk-wdt" and registers each as a watchdog character device
+ *   (/dev/watchdogN).
+ *
+ * Input Parameters:
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_WATCHDOG_CMSDK
+int fdt_cmsdk_wdt_devices_register(FAR const void *fdt, int irqbase);
+#endif
 #endif /* __INCLUDE_NUTTX_FDT_H */

Reply via email to