diff mbox series

[v5,2/9] timer: Add helper for drivers using timebase fallback

Message ID 20200915141724.503929-3-seanga2@gmail.com
State Superseded
Delegated to: Andes
Headers show
Series riscv: Clean up timer drivers | expand

Commit Message

Sean Anderson Sept. 15, 2020, 2:17 p.m. UTC
This function is designed to be used when a timer used to be initialized by
the cpu (e.g. RISC-V timers), but now is initialized by dm_timer_init. In
such a case, the timer may prefer to use the clocks and clock-frequency
properties, but should be able to fall back on using the cpu's
timebase-frequency.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bin.meng@windriver.com>
---

(no changes since v4)

Changes in v4:
- New

 drivers/timer/timer-uclass.c | 25 +++++++++++++++++++++++++
 include/timer.h              | 15 +++++++++++++++
 2 files changed, 40 insertions(+)

Comments

Rick Chen Sept. 22, 2020, 3:24 a.m. UTC | #1
> This function is designed to be used when a timer used to be initialized by
> the cpu (e.g. RISC-V timers), but now is initialized by dm_timer_init. In
> such a case, the timer may prefer to use the clocks and clock-frequency
> properties, but should be able to fall back on using the cpu's
> timebase-frequency.
>
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> Reviewed-by: Simon Glass <sjg@chromium.org>
> Reviewed-by: Bin Meng <bin.meng@windriver.com>
> ---
>
> (no changes since v4)
>
> Changes in v4:
> - New
>
>  drivers/timer/timer-uclass.c | 25 +++++++++++++++++++++++++
>  include/timer.h              | 15 +++++++++++++++
>  2 files changed, 40 insertions(+)
>

Reviewed-by: Rick Chen <rick@andestech.com>
Rick Chen Sept. 28, 2020, 3:28 a.m. UTC | #2
Hi Sean

> This function is designed to be used when a timer used to be initialized by
> the cpu (e.g. RISC-V timers), but now is initialized by dm_timer_init. In
> such a case, the timer may prefer to use the clocks and clock-frequency
> properties, but should be able to fall back on using the cpu's
> timebase-frequency.
>
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> Reviewed-by: Simon Glass <sjg@chromium.org>
> Reviewed-by: Bin Meng <bin.meng@windriver.com>
> ---
>
> (no changes since v4)
>
> Changes in v4:
> - New
>
>  drivers/timer/timer-uclass.c | 25 +++++++++++++++++++++++++
>  include/timer.h              | 15 +++++++++++++++
>  2 files changed, 40 insertions(+)
>
> diff --git a/drivers/timer/timer-uclass.c b/drivers/timer/timer-uclass.c
> index 14dde950a1..fb2f4c351a 100644
> --- a/drivers/timer/timer-uclass.c
> +++ b/drivers/timer/timer-uclass.c
> @@ -4,6 +4,7 @@
>   */
>
>  #include <common.h>
> +#include <cpu.h>
>  #include <dm.h>
>  #include <init.h>
>  #include <dm/lists.h>
> @@ -79,6 +80,30 @@ static int timer_post_probe(struct udevice *dev)
>         return 0;
>  }
>
> +int timer_timebase_fallback(struct udevice *dev)
> +{
> +       struct udevice *cpu;
> +       struct cpu_platdata *cpu_plat;
> +       struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
> +
> +       /* Did we get our clock rate from the device tree? */
> +       if (uc_priv->clock_rate)
> +               return 0;
> +
> +       /* Fall back to timebase-frequency */
> +       dev_dbg(dev, "missing clocks or clock-frequency property; falling back on timebase-frequency\n");
> +       cpu = cpu_get_current_dev();

It shall depend on CONFIG_CPU, please check about the fail item of CI:
https://travis-ci.org/github/rickchen36/u-boot-riscv/jobs/730170730

Thanks,
Rick

> +       if (!cpu)
> +               return -ENODEV;
> +
> +       cpu_plat = dev_get_parent_platdata(cpu);
> +       if (!cpu_plat)
> +               return -ENODEV;
> +
> +       uc_priv->clock_rate = cpu_plat->timebase_freq;
> +       return 0;
> +}
> +
>  u64 timer_conv_64(u32 count)
>  {
>         /* increment tbh if tbl has rolled over */
> diff --git a/include/timer.h b/include/timer.h
> index a49b500ce3..8b9fa51c53 100644
> --- a/include/timer.h
> +++ b/include/timer.h
> @@ -15,6 +15,21 @@
>   */
>  int dm_timer_init(void);
>
> +/**
> + * timer_timebase_fallback() - Helper for timers using timebase fallback
> + * @dev: A timer partially-probed timer device
> + *
> + * This is a helper function designed for timers which need to fall back on the
> + * cpu's timebase. This function is designed to be called during the driver's
> + * probe(). If there is a clocks or clock-frequency property in the timer's
> + * binding, then it will be used. Otherwise, the timebase of the current cpu
> + * will be used. This is initialized by the cpu driver, and usually gotten from
> + * ``/cpus/timebase-frequency`` or ``/cpus/cpu@X/timebase-frequency``.
> + *
> + * Return: 0 if OK, or negative error code on failure
> + */
> +int timer_timebase_fallback(struct udevice *dev);
> +
>  /*
>   * timer_conv_64 - convert 32-bit counter value to 64-bit
>   *
> --
> 2.28.0
>
diff mbox series

Patch

diff --git a/drivers/timer/timer-uclass.c b/drivers/timer/timer-uclass.c
index 14dde950a1..fb2f4c351a 100644
--- a/drivers/timer/timer-uclass.c
+++ b/drivers/timer/timer-uclass.c
@@ -4,6 +4,7 @@ 
  */
 
 #include <common.h>
+#include <cpu.h>
 #include <dm.h>
 #include <init.h>
 #include <dm/lists.h>
@@ -79,6 +80,30 @@  static int timer_post_probe(struct udevice *dev)
 	return 0;
 }
 
+int timer_timebase_fallback(struct udevice *dev)
+{
+	struct udevice *cpu;
+	struct cpu_platdata *cpu_plat;
+	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+
+	/* Did we get our clock rate from the device tree? */
+	if (uc_priv->clock_rate)
+		return 0;
+
+	/* Fall back to timebase-frequency */
+	dev_dbg(dev, "missing clocks or clock-frequency property; falling back on timebase-frequency\n");
+	cpu = cpu_get_current_dev();
+	if (!cpu)
+		return -ENODEV;
+
+	cpu_plat = dev_get_parent_platdata(cpu);
+	if (!cpu_plat)
+		return -ENODEV;
+
+	uc_priv->clock_rate = cpu_plat->timebase_freq;
+	return 0;
+}
+
 u64 timer_conv_64(u32 count)
 {
 	/* increment tbh if tbl has rolled over */
diff --git a/include/timer.h b/include/timer.h
index a49b500ce3..8b9fa51c53 100644
--- a/include/timer.h
+++ b/include/timer.h
@@ -15,6 +15,21 @@ 
  */
 int dm_timer_init(void);
 
+/**
+ * timer_timebase_fallback() - Helper for timers using timebase fallback
+ * @dev: A timer partially-probed timer device
+ *
+ * This is a helper function designed for timers which need to fall back on the
+ * cpu's timebase. This function is designed to be called during the driver's
+ * probe(). If there is a clocks or clock-frequency property in the timer's
+ * binding, then it will be used. Otherwise, the timebase of the current cpu
+ * will be used. This is initialized by the cpu driver, and usually gotten from
+ * ``/cpus/timebase-frequency`` or ``/cpus/cpu@X/timebase-frequency``.
+ *
+ * Return: 0 if OK, or negative error code on failure
+ */
+int timer_timebase_fallback(struct udevice *dev);
+
 /*
  * timer_conv_64 - convert 32-bit counter value to 64-bit
  *