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


The following commit(s) were added to refs/heads/master by this push:
     new df2eb6754a9 drivers/timers: add SiFive PWM lower-half driver
df2eb6754a9 is described below

commit df2eb6754a9a526c067e9fcb2e313aa4e21b3e5b
Author: qinwei1 <[email protected]>
AuthorDate: Fri Sep 4 20:00:29 2026 +0800

    drivers/timers: add SiFive PWM lower-half driver
    
    sifive_pwm.c implements pwm_ops_s on top of the generic drivers/timers/pwm.c
    upper half.
    - start() picks the PWMCFG.SCALE prescaler from the requested
      frequency and writes PWMCMP(ch-1) = 0x10000 - duty for each channel,
      the exact inverse of the duty = 1 - PWMCMPx/period the device publishes;
    - stop() parks the outputs at 0%.  The counter runs in its natural
      16-bit wrap mode, so all four compare registers are usable as
      duty outputs and the frequency is approximated by SCALE, matching
      the mainline Linux pwm-sifive driver.
    - Register access is local volatile MMIO so the driver builds on
      armv8-a, armv8-r and RISC-V.
    
    Signed-off-by: qinwei1 <[email protected]>
---
 drivers/timers/CMakeLists.txt     |   4 +
 drivers/timers/Kconfig            |  11 +
 drivers/timers/Make.defs          |   6 +
 drivers/timers/sifive_pwm.c       | 419 ++++++++++++++++++++++++++++++++++++++
 include/nuttx/timers/sifive_pwm.h | 113 ++++++++++
 5 files changed, 553 insertions(+)

diff --git a/drivers/timers/CMakeLists.txt b/drivers/timers/CMakeLists.txt
index 6a49e65337d..96a66cd6563 100644
--- a/drivers/timers/CMakeLists.txt
+++ b/drivers/timers/CMakeLists.txt
@@ -104,6 +104,10 @@ if(CONFIG_PWM)
   list(APPEND SRCS pwm.c)
 endif()
 
+if(CONFIG_PWM_SIFIVE)
+  list(APPEND SRCS sifive_pwm.c)
+endif()
+
 if(CONFIG_PULSECOUNT)
   list(APPEND SRCS pulsecount.c)
 endif()
diff --git a/drivers/timers/Kconfig b/drivers/timers/Kconfig
index 776fa9f9f77..b547b192c9c 100644
--- a/drivers/timers/Kconfig
+++ b/drivers/timers/Kconfig
@@ -95,6 +95,17 @@ config PWM_NCHANNELS
                Specifies the number of output channels per timer.  Each timer
                may support fewer output channels than this value.
 
+config PWM_SIFIVE
+       bool "SiFive PWM lower-half driver"
+       default n
+       ---help---
+               Enables the SiFive PWM lower-half driver (four compare channels,
+               PWMCFG/PWMCMPx registers).  The controller is described by the
+               "sifive,pwm0" device tree binding.  The counter runs in its
+               natural 16-bit wrap mode, so the requested frequency is
+               approximated by the SCALE prescaler and all four compare
+               registers act as duty outputs.
+
 endif # PWM
 
 config CAPTURE
diff --git a/drivers/timers/Make.defs b/drivers/timers/Make.defs
index 776799039e5..c787b08d53e 100644
--- a/drivers/timers/Make.defs
+++ b/drivers/timers/Make.defs
@@ -142,6 +142,12 @@ ifeq ($(CONFIG_PWM),y)
   TMRVPATH = :timers
 endif
 
+ifeq ($(CONFIG_PWM_SIFIVE),y)
+  CSRCS += sifive_pwm.c
+  TMRDEPPATH = --dep-path timers
+  TMRVPATH = :timers
+endif
+
 ifeq ($(CONFIG_PULSECOUNT),y)
   CSRCS += pulsecount.c
   TMRDEPPATH = --dep-path timers
diff --git a/drivers/timers/sifive_pwm.c b/drivers/timers/sifive_pwm.c
new file mode 100644
index 00000000000..9a43ec773f8
--- /dev/null
+++ b/drivers/timers/sifive_pwm.c
@@ -0,0 +1,419 @@
+/****************************************************************************
+ * drivers/timers/sifive_pwm.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 <sys/types.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/timers/pwm.h>
+#include <nuttx/timers/sifive_pwm.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Register offsets of the SiFive PWM block ("sifive,pwm0"). */
+
+#define SIFIVE_PWM_CFG        0x00    /* Configuration                     */
+#define SIFIVE_PWM_COUNT      0x08    /* Counter (31-bit, self-increments) */
+#define SIFIVE_PWM_S          0x10    /* Scaled counter, compared to CMPx  */
+#define SIFIVE_PWM_CMP(n)     (0x20 + ((n) << 2))
+
+/* PWMCFG bit fields (only the ones this driver programs). */
+
+#define PWM_CFG_SCALE_MASK    0x0000000fu  /* [3:0]  counter prescaler     */
+#define PWM_CFG_ZEROCMP       (1u << 9)    /* PWMCMP0 acts as the period   */
+#define PWM_CFG_ENALWAYS      (1u << 12)   /* Counter runs continuously    */
+#define PWM_CFG_ENONESHOT     (1u << 13)   /* Counter runs once            */
+
+/* PWMCMPx is 16 bits wide, so the counter wraps after PWM_CMP_FULL scaled
+ * ticks when ZEROCMP is left clear (the mode used here).
+ */
+
+#define PWM_CMP_WIDTH         16
+#define PWM_CMP_FULL          (1u << PWM_CMP_WIDTH)   /* 65536             */
+#define PWM_CMP_MASK          (PWM_CMP_FULL - 1)      /* 0xffff            */
+
+#define PWM_CFG_SCALE_MAX     PWM_CFG_SCALE_MASK
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sifive_pwm_lowerhalf_s
+{
+  /* The PWM upper half expects this to be the first member. */
+
+  FAR const struct pwm_ops_s *ops;
+
+  uintptr_t base;        /* MMIO register base                            */
+  uint32_t  freq_hz;     /* Reference clock feeding the counter           */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sifive_pwm_setup(FAR struct pwm_lowerhalf_s *dev);
+static int sifive_pwm_shutdown(FAR struct pwm_lowerhalf_s *dev);
+static int sifive_pwm_start(FAR struct pwm_lowerhalf_s *dev,
+                            FAR const struct pwm_info_s *info);
+static int sifive_pwm_stop(FAR struct pwm_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct pwm_ops_s g_pwmops =
+{
+  sifive_pwm_setup,     /* setup    */
+  sifive_pwm_shutdown,  /* shutdown */
+  sifive_pwm_start,     /* start    */
+  sifive_pwm_stop       /* stop     */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Direct MMIO accessors.  Kept local so the driver is architecture neutral
+ * (armv8-a / armv8-r / RISC-V) and does not depend on any chip-specific
+ * getreg macro, matching drivers/timers/sp804.h and drivers/ioexpander.
+ */
+
+static inline uint32_t sifive_pwm_getreg(uintptr_t base, unsigned int off)
+{
+  return *(FAR volatile uint32_t *)(base + off);
+}
+
+static inline void sifive_pwm_putreg(uintptr_t base, unsigned int off,
+                                     uint32_t val)
+{
+  *(FAR volatile uint32_t *)(base + off) = val;
+}
+
+/****************************************************************************
+ * Name: sifive_pwm_scale
+ *
+ * Description:
+ *   Pick the PWMCFG.SCALE prescaler whose 65536-tick period is closest to
+ *   (but not shorter than) the requested pulse period.  With SCALE = s the
+ *   output frequency is freq_hz / (1 << (16 + s)), so the achievable set is
+ *   quantised to powers of two - the mainline Linux pwm-sifive driver
+ *   approximates the period the same way.
+ *
+ * Input Parameters:
+ *   freq_hz   - Reference clock in Hz
+ *   frequency - Requested output frequency in Hz
+ *
+ * Returned Value:
+ *   SCALE field value in [0, 15].
+ *
+ ****************************************************************************/
+
+static uint32_t sifive_pwm_scale(uint32_t freq_hz, uint32_t frequency)
+{
+  uint32_t ticks;
+  uint32_t scale;
+
+  /* Counter ticks needed for one output period. */
+
+  ticks = freq_hz / frequency;
+
+  /* SCALE shifts the counter right before the compare, so one period is
+   * (1 << (16 + SCALE)) reference ticks.  Find the smallest SCALE whose
+   * period covers the request.
+   */
+
+  for (scale = 0; scale < PWM_CFG_SCALE_MAX; scale++)
+    {
+      if ((PWM_CMP_FULL << scale) >= ticks)
+        {
+          break;
+        }
+    }
+
+  return scale;
+}
+
+/****************************************************************************
+ * Name: sifive_pwm_duty2cmp
+ *
+ * Description:
+ *   Convert an upper-half duty (b16 fraction, 0..0xffff for 0..100%) into a
+ *   PWMCMPx compare value.  The output is high while PWMS < PWMCMPx counts
+ *   down from the wrap point, so the device reports
+ *   duty = 1 - PWMCMPx / 65536: the compare value is the complement of the
+ *   requested duty.  This is the same inverted-polarity mapping the QEMU
+ *   model publishes on "pwm/<instance>/out/<ch>" and that mainline Linux
+ *   describes as PWM_POLARITY_INVERSED.
+ *
+ ****************************************************************************/
+
+static uint32_t sifive_pwm_duty2cmp(ub16_t duty)
+{
+  uint32_t cmp;
+
+  if (duty >= PWM_CMP_FULL)
+    {
+      /* 100% duty: the compare never matches within a period. */
+
+      return 0;
+    }
+
+  cmp = PWM_CMP_FULL - duty;
+
+  return cmp > PWM_CMP_MASK ? PWM_CMP_MASK : cmp;
+}
+
+/****************************************************************************
+ * Name: sifive_pwm_idle
+ *
+ * Description:
+ *   Drive every channel to its idle (0% duty) compare value.  Writing the
+ *   compare registers is what makes the QEMU model re-publish the duty, so
+ *   this also updates any external subscriber.
+ *
+ ****************************************************************************/
+
+static void sifive_pwm_idle(FAR struct sifive_pwm_lowerhalf_s *priv)
+{
+  int i;
+
+  for (i = 0; i < SIFIVE_PWM_NCHANNELS; i++)
+    {
+      sifive_pwm_putreg(priv->base, SIFIVE_PWM_CMP(i), PWM_CMP_MASK);
+    }
+}
+
+/****************************************************************************
+ * Name: sifive_pwm_setup
+ *
+ * Description:
+ *   Called when /dev/pwmN is first opened.  Put the counter in a known,
+ *   stopped state with all outputs idle; pulses only start from start().
+ *
+ ****************************************************************************/
+
+static int sifive_pwm_setup(FAR struct pwm_lowerhalf_s *dev)
+{
+  FAR struct sifive_pwm_lowerhalf_s *priv =
+    (FAR struct sifive_pwm_lowerhalf_s *)dev;
+
+  sifive_pwm_putreg(priv->base, SIFIVE_PWM_CFG, 0);
+  sifive_pwm_putreg(priv->base, SIFIVE_PWM_COUNT, 0);
+  sifive_pwm_idle(priv);
+
+  pwminfo("base=0x%" PRIxPTR " clk=%" PRIu32 " Hz\n",
+          priv->base, priv->freq_hz);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: sifive_pwm_shutdown
+ *
+ * Description:
+ *   Called when the last reference to /dev/pwmN is closed.  Stop the
+ *   counter and leave the outputs idle.
+ *
+ ****************************************************************************/
+
+static int sifive_pwm_shutdown(FAR struct pwm_lowerhalf_s *dev)
+{
+  return sifive_pwm_stop(dev);
+}
+
+/****************************************************************************
+ * Name: sifive_pwm_start
+ *
+ * Description:
+ *   Program the prescaler from info->frequency and one compare register per
+ *   requested channel, then let the counter run.  Channel numbers are
+ *   one-based (channel 1 drives PWMCMP0); a zero entry is skipped and a
+ *   negative entry ends the list.
+ *
+ ****************************************************************************/
+
+static int sifive_pwm_start(FAR struct pwm_lowerhalf_s *dev,
+                            FAR const struct pwm_info_s *info)
+{
+  FAR struct sifive_pwm_lowerhalf_s *priv =
+    (FAR struct sifive_pwm_lowerhalf_s *)dev;
+  uint32_t frequency;
+  uint32_t scale;
+  int i;
+
+#ifdef CONFIG_PWM_FREQUENCY_FIXED
+  frequency = b32toi(info->frequency);
+#else
+  frequency = info->frequency;
+#endif
+
+  if (frequency == 0 || frequency > priv->freq_hz)
+    {
+      pwmerr("ERROR: frequency %" PRIu32 " Hz out of range (clk %" PRIu32
+             " Hz)\n", frequency, priv->freq_hz);
+      return -EINVAL;
+    }
+
+  scale = sifive_pwm_scale(priv->freq_hz, frequency);
+
+  /* Restart the period from a known point, then enable free running. */
+
+  sifive_pwm_putreg(priv->base, SIFIVE_PWM_CFG, 0);
+  sifive_pwm_putreg(priv->base, SIFIVE_PWM_COUNT, 0);
+
+  for (i = 0; i < CONFIG_PWM_NCHANNELS; i++)
+    {
+      int8_t channel = info->channels[i].channel;
+      uint32_t cmp;
+
+      if (channel < 0)
+        {
+          /* End of the channel list. */
+
+          break;
+        }
+
+      if (channel == 0)
+        {
+          /* Unused entry. */
+
+          continue;
+        }
+
+      if (channel > SIFIVE_PWM_NCHANNELS)
+        {
+          pwmerr("ERROR: channel %d exceeds %d\n",
+                 channel, SIFIVE_PWM_NCHANNELS);
+          return -EINVAL;
+        }
+
+      cmp = sifive_pwm_duty2cmp(info->channels[i].duty);
+      sifive_pwm_putreg(priv->base, SIFIVE_PWM_CMP(channel - 1), cmp);
+
+      pwminfo("channel %d: duty=0x%08" PRIx32 " -> PWMCMP%d=0x%04" PRIx32
+              "\n", channel, (uint32_t)info->channels[i].duty,
+              channel - 1, cmp);
+    }
+
+  sifive_pwm_putreg(priv->base, SIFIVE_PWM_CFG,
+                    (scale & PWM_CFG_SCALE_MASK) | PWM_CFG_ENALWAYS);
+
+  pwminfo("frequency=%" PRIu32 " Hz scale=%" PRIu32 " (actual %" PRIu32
+          " Hz)\n", frequency, scale,
+          priv->freq_hz >> (PWM_CMP_WIDTH + scale));
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: sifive_pwm_stop
+ *
+ * Description:
+ *   Stop the counter and park every output at 0% duty.
+ *
+ ****************************************************************************/
+
+static int sifive_pwm_stop(FAR struct pwm_lowerhalf_s *dev)
+{
+  FAR struct sifive_pwm_lowerhalf_s *priv =
+    (FAR struct sifive_pwm_lowerhalf_s *)dev;
+
+  /* Park the outputs first so the published duty reflects the stop, then
+   * take the enable bits away.
+   */
+
+  sifive_pwm_idle(priv);
+  sifive_pwm_putreg(priv->base, SIFIVE_PWM_CFG, 0);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sifive_pwm_initialize
+ *
+ * Description:
+ *   See include/nuttx/timers/sifive_pwm.h.
+ *
+ ****************************************************************************/
+
+FAR struct pwm_lowerhalf_s *sifive_pwm_initialize(uintptr_t base,
+                                                  uint32_t freq_hz)
+{
+  FAR struct sifive_pwm_lowerhalf_s *priv;
+
+  if (base == 0)
+    {
+      return NULL;
+    }
+
+  /* One state block per controller, allocated so that any number of
+   * controllers can be registered.
+   */
+
+  priv = kmm_zalloc(sizeof(*priv));
+  if (priv == NULL)
+    {
+      return NULL;
+    }
+
+  priv->ops     = &g_pwmops;
+  priv->base    = base;
+  priv->freq_hz = freq_hz != 0 ? freq_hz : SIFIVE_PWM_DEFAULT_FREQ;
+
+  return (FAR struct pwm_lowerhalf_s *)priv;
+}
+
+/****************************************************************************
+ * Name: sifive_pwm_uninitialize
+ *
+ * Description:
+ *   See include/nuttx/timers/sifive_pwm.h.
+ *
+ ****************************************************************************/
+
+void sifive_pwm_uninitialize(FAR struct pwm_lowerhalf_s *dev)
+{
+  FAR struct sifive_pwm_lowerhalf_s *priv =
+    (FAR struct sifive_pwm_lowerhalf_s *)dev;
+
+  if (priv != NULL)
+    {
+      kmm_free(priv);
+    }
+}
diff --git a/include/nuttx/timers/sifive_pwm.h 
b/include/nuttx/timers/sifive_pwm.h
new file mode 100644
index 00000000000..b9e23cf9fdc
--- /dev/null
+++ b/include/nuttx/timers/sifive_pwm.h
@@ -0,0 +1,113 @@
+/****************************************************************************
+ * include/nuttx/timers/sifive_pwm.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_SIFIVE_PWM_H
+#define __INCLUDE_NUTTX_TIMERS_SIFIVE_PWM_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+
+#include <nuttx/timers/pwm.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Compare channels implemented by the IP.  PWMCMP0..PWMCMP3 each drive one
+ * output, so the lower-half accepts channel numbers 1..4 (the PWM upper half
+ * numbers channels from one).
+ */
+
+#define SIFIVE_PWM_NCHANNELS  4
+
+/* Reference clock used when the device tree carries no "clock-frequency".
+ * Matches the default of QEMU's sifive-pwm model (hw/timer/sifive_pwm.c).
+ */
+
+#define SIFIVE_PWM_DEFAULT_FREQ  500000000
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Name: sifive_pwm_initialize
+ *
+ * Description:
+ *   Initialise a SiFive PWM lower-half bound to a memory-mapped register
+ *   window (as modelled by QEMU's "sifive,pwm0" device on the virt
+ *   platform).  The returned device is passed to pwm_register().
+ *
+ *   The counter runs in its natural 16-bit wrap mode, so the period is
+ *   fixed at 65536 scaled ticks and all four compare registers are
+ *   available as duty outputs.  A requested frequency is approximated by
+ *   the PWMCFG.SCALE prescaler, which quantises it to powers of two - the
+ *   same behaviour as the mainline Linux pwm-sifive driver.
+ *
+ * Input Parameters:
+ *   Any number of controllers may be initialised: each call returns its own
+ *   state block, so callers must not assume a single instance.
+ *
+ * Input Parameters:
+ *   base    - Physical (kernel-visible) base address of the PWM register
+ *             window, taken from the device tree "reg" property.
+ *   freq_hz - Reference clock feeding the counter, in Hz.  Pass
+ *             SIFIVE_PWM_DEFAULT_FREQ when the device tree does not say.
+ *
+ * Returned Value:
+ *   Valid PWM lower-half structure reference on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct pwm_lowerhalf_s *sifive_pwm_initialize(uintptr_t base,
+                                                  uint32_t freq_hz);
+
+/****************************************************************************
+ * Name: sifive_pwm_uninitialize
+ *
+ * Description:
+ *   Release a lower-half returned by sifive_pwm_initialize().  Only needed
+ *   on an error path before the device reaches pwm_register(): once
+ *   registered, the lower-half must persist for the lifetime of the driver.
+ *
+ * Input Parameters:
+ *   dev - Lower-half returned by sifive_pwm_initialize(), or NULL.
+ *
+ ****************************************************************************/
+
+void sifive_pwm_uninitialize(FAR struct pwm_lowerhalf_s *dev);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_TIMERS_SIFIVE_PWM_H */

Reply via email to