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 3b7cb4dc85086a9ed45f2d212bd6f38773945b7f
Author: Xiang Xiao <[email protected]>
AuthorDate: Mon Mar 16 22:01:10 2026 +0800

    drivers/timers: add SP804 dual timer driver
    
    Add support for the ARM SP804 dual timer, which is used in
    several ARM development boards emulated by QEMU (VersatilePB,
    IntegratorCP, RealView EB).
    
    Signed-off-by: Xiang Xiao <[email protected]>
---
 drivers/timers/CMakeLists.txt |   4 +
 drivers/timers/Kconfig        |   9 ++
 drivers/timers/Make.defs      |   6 ++
 drivers/timers/sp804.c        | 187 ++++++++++++++++++++++++++++++++++++++++++
 include/nuttx/timers/sp804.h  |  68 +++++++++++++++
 5 files changed, 274 insertions(+)

diff --git a/drivers/timers/CMakeLists.txt b/drivers/timers/CMakeLists.txt
index 5aa04fd5c36..3cfe6e363fb 100644
--- a/drivers/timers/CMakeLists.txt
+++ b/drivers/timers/CMakeLists.txt
@@ -61,6 +61,10 @@ if(CONFIG_RTC_PCF8563)
   list(APPEND SRCS pcf8563.c)
 endif()
 
+if(CONFIG_TIMER_SP804)
+  list(APPEND SRCS sp804.c)
+endif()
+
 if(CONFIG_RTC_PL031)
   list(APPEND SRCS pl031.c)
 endif()
diff --git a/drivers/timers/Kconfig b/drivers/timers/Kconfig
index 83bcad5e9b8..a7014d5f261 100644
--- a/drivers/timers/Kconfig
+++ b/drivers/timers/Kconfig
@@ -470,6 +470,15 @@ config RTC_RPMSG_SYNC_BASETIME
 
 endif # RTC
 
+config TIMER_SP804
+       bool "SP804 Dual Timer Support"
+       default n
+       select ONESHOT_COUNT
+       ---help---
+               Enables support for the ARM SP804 dual timer module.
+               This timer is commonly found on ARM Versatile, Integrator
+               and RealView platforms, including QEMU emulations.
+
 menuconfig WATCHDOG
        bool "Watchdog Timer Support"
        default n
diff --git a/drivers/timers/Make.defs b/drivers/timers/Make.defs
index 3c48a32f2e1..bf3f1a239b7 100644
--- a/drivers/timers/Make.defs
+++ b/drivers/timers/Make.defs
@@ -79,6 +79,12 @@ ifeq ($(CONFIG_RTC_PCF8563),y)
   TMRVPATH = :timers
 endif
 
+ifeq ($(CONFIG_TIMER_SP804),y)
+  CSRCS += sp804.c
+  TMRDEPPATH = --dep-path timers
+  TMRVPATH = :timers
+endif
+
 ifeq ($(CONFIG_RTC_PL031),y)
   CSRCS += pl031.c
   TMRDEPPATH = --dep-path timers
diff --git a/drivers/timers/sp804.c b/drivers/timers/sp804.c
new file mode 100644
index 00000000000..b8cb75c096a
--- /dev/null
+++ b/drivers/timers/sp804.c
@@ -0,0 +1,187 @@
+/****************************************************************************
+ * drivers/timers/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 <nuttx/irq.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/timers/sp804.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* SP804 per-timer register offsets */
+
+#define SP804_LOAD          0x00  /* Load register */
+#define SP804_VALUE         0x04  /* Current value (RO) */
+#define SP804_CONTROL       0x08  /* Control register */
+#define SP804_INTCLR        0x0c  /* Interrupt clear (WO) */
+
+/* Timer 2 sits at base + 0x20 with the same layout */
+
+#define SP804_TIMER2_OFF    0x20
+
+/* Control register bits */
+
+#define SP804_CTRL_ONESHOT  (1 << 0)  /* One-shot mode */
+#define SP804_CTRL_32BIT    (1 << 1)  /* 32-bit counter */
+#define SP804_CTRL_IE       (1 << 5)  /* Interrupt enable */
+#define SP804_CTRL_ENABLE   (1 << 7)  /* Timer enable */
+
+/* Helper macros */
+
+#define sp804_getreg(b, o) \
+  (*(FAR volatile uint32_t *)((b) + (o)))
+#define sp804_putreg(v, b, o) \
+  (*((FAR volatile uint32_t *)((b) + (o))) = (v))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sp804_lowerhalf_s
+{
+  struct oneshot_lowerhalf_s lower;
+  uintptr_t cnt;    /* Timer 1: free-running counter */
+  uintptr_t alarm;  /* Timer 2: oneshot alarm */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static clkcnt_t sp804_max_delay(FAR struct oneshot_lowerhalf_s *lower);
+static clkcnt_t sp804_current(FAR struct oneshot_lowerhalf_s *lower);
+static void sp804_start(FAR struct oneshot_lowerhalf_s *lower,
+                        clkcnt_t delta);
+static void sp804_cancel(FAR struct oneshot_lowerhalf_s *lower);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct oneshot_operations_s g_sp804_ops =
+{
+  .current   = sp804_current,
+  .start     = sp804_start,
+  .cancel    = sp804_cancel,
+  .max_delay = sp804_max_delay,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sp804_interrupt
+ ****************************************************************************/
+
+static int sp804_interrupt(int irq, FAR void *context, FAR void *arg)
+{
+  FAR struct sp804_lowerhalf_s *priv = arg;
+
+  sp804_putreg(1, priv->alarm, SP804_INTCLR);
+  sp804_putreg(0, priv->alarm, SP804_CONTROL);
+  oneshot_process_callback(&priv->lower);
+
+  return 0;
+}
+
+static clkcnt_t sp804_max_delay(FAR struct oneshot_lowerhalf_s *lower)
+{
+  return UINT32_MAX;
+}
+
+static clkcnt_t sp804_current(FAR struct oneshot_lowerhalf_s *lower)
+{
+  FAR struct sp804_lowerhalf_s *priv =
+    (FAR struct sp804_lowerhalf_s *)lower;
+
+  /* Timer 1 counts down; invert to get increasing count */
+
+  return UINT32_MAX - sp804_getreg(priv->cnt, SP804_VALUE);
+}
+
+static void sp804_start(FAR struct oneshot_lowerhalf_s *lower,
+                        clkcnt_t delta)
+{
+  FAR struct sp804_lowerhalf_s *priv =
+    (FAR struct sp804_lowerhalf_s *)lower;
+
+  sp804_putreg(0, priv->alarm, SP804_CONTROL);
+  sp804_putreg((uint32_t)delta, priv->alarm, SP804_LOAD);
+  sp804_putreg(1, priv->alarm, SP804_INTCLR);
+  sp804_putreg(SP804_CTRL_ENABLE | SP804_CTRL_32BIT |
+               SP804_CTRL_ONESHOT | SP804_CTRL_IE,
+               priv->alarm, SP804_CONTROL);
+}
+
+static void sp804_cancel(FAR struct oneshot_lowerhalf_s *lower)
+{
+  FAR struct sp804_lowerhalf_s *priv =
+    (FAR struct sp804_lowerhalf_s *)lower;
+
+  sp804_putreg(0, priv->alarm, SP804_CONTROL);
+  sp804_putreg(1, priv->alarm, SP804_INTCLR);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct oneshot_lowerhalf_s *
+sp804_initialize(uintptr_t base, int irq, uint32_t freq)
+{
+  FAR struct sp804_lowerhalf_s *priv;
+
+  priv = kmm_zalloc(sizeof(*priv));
+  if (priv == NULL)
+    {
+      return NULL;
+    }
+
+  priv->lower.ops = &g_sp804_ops;
+  priv->cnt   = base;
+  priv->alarm = base + SP804_TIMER2_OFF;
+
+  oneshot_count_init(&priv->lower, freq);
+
+  /* Timer 1: free-running 32-bit counter */
+
+  sp804_putreg(0, priv->cnt, SP804_CONTROL);
+  sp804_putreg(UINT32_MAX, priv->cnt, SP804_LOAD);
+  sp804_putreg(SP804_CTRL_ENABLE | SP804_CTRL_32BIT,
+               priv->cnt, SP804_CONTROL);
+
+  /* Timer 2: oneshot alarm (initially disabled) */
+
+  sp804_putreg(0, priv->alarm, SP804_CONTROL);
+  sp804_putreg(1, priv->alarm, SP804_INTCLR);
+
+  irq_attach(irq, sp804_interrupt, priv);
+  up_enable_irq(irq);
+
+  return &priv->lower;
+}
diff --git a/include/nuttx/timers/sp804.h b/include/nuttx/timers/sp804.h
new file mode 100644
index 00000000000..4a096d5225f
--- /dev/null
+++ b/include/nuttx/timers/sp804.h
@@ -0,0 +1,68 @@
+/****************************************************************************
+ * include/nuttx/timers/sp804.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 __INCLUDE_NUTTX_TIMERS_SP804_H
+#define __INCLUDE_NUTTX_TIMERS_SP804_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/timers/oneshot.h>
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Name: sp804_initialize
+ *
+ * Description:
+ *   Initialize the SP804 dual timer and return a oneshot lowerhalf
+ *   driver instance that can be bound to the OS alarm infrastructure
+ *   via up_alarm_set_lowerhalf().
+ *
+ * Input Parameters:
+ *   base - Physical base address of the SP804 timer block.
+ *   irq  - IRQ number associated with the timer interrupt.
+ *   freq - Input clock frequency in Hz.
+ *
+ * Returned Value:
+ *   On success, a non-NULL oneshot lowerhalf pointer is returned.
+ *   NULL is returned on any failure.
+ *
+ ****************************************************************************/
+
+FAR struct oneshot_lowerhalf_s *
+sp804_initialize(uintptr_t base, int irq, uint32_t freq);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_TIMERS_SP804_H */

Reply via email to