Skip to contentSkip to conversation

riscv: Clean up timer drivers

PATCH Superseded Sean Anderson 3 participants 14 messages Sep 15, 2020 – Sep 28, 2020

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover riscv: Clean up timer drivers

This series cleans up the timer drivers in RISC-V and converts them to DM.

This series needs to be tested! I have only tested it on QEMU and the K210. Notably, this means that the HiFive and anything Andes is completely untested.

This series depends on [1]. Without that patch, build will fail for sifive targets.

[1] http://patchwork.ozlabs.org/project/uboot/patch/1600157107-57175-2-git-send-email-bmeng.cn at gmail.com/

Changes in v5:
- Add test for timer_timebase_fallback
- Don't add a dependency on REGMAP for SIFIVE_FU540_DDR. Instead, depend on Bin Meng's patch.
- Remove RISCV_SYSCON_PLMT
- Undo changes to ae350 device trees. They are unnecessary with timer_timebase_fallback.

Changes in v4:
- Both the Andes PMLT and Sifive CLINT now fall back on timebase-frequency, per discussion with Anup Patel
- Introduce helper function for falling back on timebase-frequency
- Modify RISCV_TIMER KConfig
- Now depends on RISCV
- Implied by S-Mode (with or without SPL)
- Rebase
- Remove clock-frequency property from k210 clint binding because we fall back on timebase-frequency

Changes in v3:
- Don't initialize the IPI in spl_invoke_opensbi. Further testing has revealed it to be unnecessary.
- Rebase

Changes in v2:
- Fix SiFive CLINT not getting tick-rate from rtcclk
- Remove RISCV_RDTIME KConfig option
- Split Kendryte binding changes into their own commit

Sean Anderson (9):
riscv: Rework riscv timer driver to only support S-mode
timer: Add helper for drivers using timebase fallback
timer: Add a test for timer_timebase_fallback
riscv: Rework Andes PLMT as a UCLASS_TIMER driver
riscv: Clean up initialization in Andes PLIC
riscv: Rework Sifive CLINT as UCLASS_TIMER driver
riscv: clk: Add CLINT clock to kendryte clock driver
riscv: Update Kendryte device tree for new CLINT driver
riscv: Update SiFive device tree for new CLINT driver

 arch/riscv/Kconfig                            | 16 -----
 arch/riscv/cpu/ax25/Kconfig                   |  2 +-
 arch/riscv/cpu/fu540/Kconfig                  |  2 +-
 arch/riscv/cpu/generic/Kconfig                |  2 +-
 arch/riscv/dts/fu540-c000-u-boot.dtsi         |  8 ++-
 .../dts/hifive-unleashed-a00-u-boot.dtsi      |  4 ++
 arch/riscv/dts/k210.dtsi                      |  7 +-
 arch/riscv/include/asm/global_data.h          |  3 -
 arch/riscv/include/asm/syscon.h               |  4 +-
 arch/riscv/lib/Makefile                       |  1 -
 arch/riscv/lib/andes_plic.c                   | 58 +++++++---------
 arch/riscv/lib/andes_plmt.c                   | 44 ++++++-------
 arch/riscv/lib/rdtime.c                       | 38 -----------
 arch/riscv/lib/sifive_clint.c                 | 66 ++++++++++---------
 arch/sandbox/dts/test.dts                     |  9 ++-
 arch/sandbox/include/asm/cpu.h                | 11 ++++
 drivers/clk/kendryte/clk.c                    |  4 ++
 drivers/cpu/cpu_sandbox.c                     | 39 +++++++++--
 drivers/timer/Kconfig                         |  4 +-
 drivers/timer/riscv_timer.c                   | 39 +++++------
 drivers/timer/sandbox_timer.c                 |  4 +-
 drivers/timer/timer-uclass.c                  | 25 +++++++
 include/dt-bindings/clock/k210-sysctl.h       |  1 +
 include/timer.h                               | 15 +++++
 test/dm/timer.c                               | 27 +++++++-
 25 files changed, 248 insertions(+), 185 deletions(-)
 delete mode 100644 arch/riscv/lib/rdtime.c
 create mode 100644 arch/sandbox/include/asm/cpu.h
Signature · 1 line
2.28.0
#1 lore raw

PATCH v5 1/9 riscv: Rework riscv timer driver to only support S-mode

The riscv-timer driver currently serves as a shim for several riscv timer drivers. This is not too desirable because it bypasses the usual timer selection via the driver model. There is no easy way to specify an alternate timing driver, or have the tick rate depend on the cpu's configured frequency. The timer drivers also do not have device structs, and so have to rely on storing parameters in gd_t. Lastly, there is no initialization call, so driver init is done in the same function which reads the time. This can result in confusing error messages. To a user, it looks like the driver failed when trying to read the time, whereas it may have failed while initializing.

This patch removes the shim functionality from the riscv-timer driver, and has it instead implement the former rdtime.c timer driver. This is because existing u-boot users who pass in a device tree (e.g. qemu) do not create a timer device for S-mode u-boot. The existing behavior of creating the riscv-timer device in the riscv cpu driver must be kept. The actual reading of the CSRs has been redone in the style of Linux's get_cycles64.

---

(no changes since v4)

Changes in v4:
- Modify RISCV_TIMER KConfig
- Now depends on RISCV
- Implied by S-Mode (with or without SPL)

Changes in v2:
- Remove RISCV_RDTIME KConfig option

 arch/riscv/Kconfig             |  8 -------
 arch/riscv/cpu/ax25/Kconfig    |  2 +-
 arch/riscv/cpu/fu540/Kconfig   |  2 +-
 arch/riscv/cpu/generic/Kconfig |  2 +-
 arch/riscv/lib/Makefile        |  1 -
 arch/riscv/lib/rdtime.c        | 38 ---------------------------------
 drivers/timer/Kconfig          |  4 ++--
 drivers/timer/riscv_timer.c    | 39 +++++++++++++++++-----------------
 8 files changed, 25 insertions(+), 71 deletions(-)
 delete mode 100644 arch/riscv/lib/rdtime.c
arch/riscv/Kconfig +0 -8
index 009a545fcf..21e6690f4d 100644
@@ -185,14 +185,6 @@ config ANDES_PLMT
 	  The Andes PLMT block holds memory-mapped mtime register
 	  associated with timer tick.
 
-config RISCV_RDTIME
-	bool
-	default y if RISCV_SMODE || SPL_RISCV_SMODE
-	help
-	  The provides the riscv_get_time() API that is implemented using the
-	  standard rdtime instruction. This is the case for S-mode U-Boot, and
-	  is useful for processors that support rdtime in M-mode too.
-
 config SYS_MALLOC_F_LEN
 	default 0x1000
 
arch/riscv/cpu/ax25/Kconfig +1 -1
index 8d8d71dcbf..5cb5bb51eb 100644
@@ -3,7 +3,7 @@ config RISCV_NDS
 	select ARCH_EARLY_INIT_R
 	imply CPU
 	imply CPU_RISCV
-	imply RISCV_TIMER
+	imply RISCV_TIMER if (RISCV_SMODE || SPL_RISCV_SMODE)
 	imply ANDES_PLIC if (RISCV_MMODE || SPL_RISCV_MMODE)
 	imply ANDES_PLMT if (RISCV_MMODE || SPL_RISCV_MMODE)
 	imply SPL_CPU_SUPPORT
arch/riscv/cpu/fu540/Kconfig +1 -1
index 53e19635c8..ac3f183342 100644
@@ -10,7 +10,7 @@ config SIFIVE_FU540
 	select SPL_RAM if SPL
 	imply CPU
 	imply CPU_RISCV
-	imply RISCV_TIMER
+	imply RISCV_TIMER if (RISCV_SMODE || SPL_RISCV_SMODE)
 	imply SIFIVE_CLINT if (RISCV_MMODE || SPL_RISCV_MMODE)
 	imply CMD_CPU
 	imply SPL_CPU_SUPPORT
arch/riscv/cpu/generic/Kconfig +1 -1
index b2cb155d6d..f4c2e2643c 100644
@@ -7,7 +7,7 @@ config GENERIC_RISCV
 	select ARCH_EARLY_INIT_R
 	imply CPU
 	imply CPU_RISCV
-	imply RISCV_TIMER
+	imply RISCV_TIMER if (RISCV_SMODE || SPL_RISCV_SMODE)
 	imply SIFIVE_CLINT if (RISCV_MMODE || SPL_RISCV_MMODE)
 	imply CMD_CPU
 	imply SPL_CPU_SUPPORT
arch/riscv/lib/Makefile +0 -1
index 6c503ff2b2..10ac5b06d3 100644
@@ -15,7 +15,6 @@ obj-$(CONFIG_SIFIVE_CLINT) += sifive_clint.o
 obj-$(CONFIG_ANDES_PLIC) += andes_plic.o
 obj-$(CONFIG_ANDES_PLMT) += andes_plmt.o
 else
-obj-$(CONFIG_RISCV_RDTIME) += rdtime.o
 obj-$(CONFIG_SBI) += sbi.o
 obj-$(CONFIG_SBI_IPI) += sbi_ipi.o
 endif
arch/riscv/lib/rdtime.c deleted +0 -38
deleted file mode 100644
index e128d7fce6..0000000000
@@ -1,38 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * Copyright (C) 2018, Anup Patel <[email protected]>
- * Copyright (C) 2018, Bin Meng <[email protected]>
- *
- * The riscv_get_time() API implementation that is using the
- * standard rdtime instruction.
- */
-
-#include <common.h>
-
-/* Implement the API required by RISC-V timer driver */
-int riscv_get_time(u64 *time)
-{
-#ifdef CONFIG_64BIT
-	u64 n;
-
-	__asm__ __volatile__ (
-		"rdtime %0"
-		: "=r" (n));
-
-	*time = n;
-#else
-	u32 lo, hi, tmp;
-
-	__asm__ __volatile__ (
-		"1:\n"
-		"rdtimeh %0\n"
-		"rdtime %1\n"
-		"rdtimeh %2\n"
-		"bne %0, %2, 1b"
-		: "=&r" (hi), "=&r" (lo), "=&r" (tmp));
-
-	*time = ((u64)hi << 32) | lo;
-#endif
-
-	return 0;
-}
drivers/timer/Kconfig +2 -2
index 637024445c..d40d313011 100644
@@ -146,8 +146,8 @@ config RISCV_TIMER
 	bool "RISC-V timer support"
 	depends on TIMER && RISCV
 	help
-	  Select this to enable support for the timer as defined
-	  by the RISC-V privileged architecture spec.
+	  Select this to enable support for a generic RISC-V S-Mode timer
+	  driver.
 
 config ROCKCHIP_TIMER
 	bool "Rockchip timer support"
drivers/timer/riscv_timer.c +20 -19
index 9f9f070e0b..449fcfcfd5 100644
@@ -1,36 +1,37 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
+ * Copyright (C) 2020, Sean Anderson <[email protected]>
  * Copyright (C) 2018, Bin Meng <[email protected]>
+ * Copyright (C) 2018, Anup Patel <[email protected]>
+ * Copyright (C) 2012 Regents of the University of California
  *
- * RISC-V privileged architecture defined generic timer driver
+ * RISC-V architecturally-defined generic timer driver
  *
- * This driver relies on RISC-V platform codes to provide the essential API
- * riscv_get_time() which is supposed to return the timer counter as defined
- * by the RISC-V privileged architecture spec.
- *
- * This driver can be used in both M-mode and S-mode U-Boot.
+ * This driver provides generic timer support for S-mode U-Boot.
  */
 
 #include <common.h>
 #include <dm.h>
 #include <errno.h>
 #include <timer.h>
-#include <asm/io.h>
-
-/**
- * riscv_get_time() - get the timer counter
- *
- * Platform codes should provide this API in order to make this driver function.
- *
- * @time:	the 64-bit timer count  as defined by the RISC-V privileged
- *		architecture spec.
- * @return:	0 on success, -ve on error.
- */
-extern int riscv_get_time(u64 *time);
+#include <asm/csr.h>
 
 static int riscv_timer_get_count(struct udevice *dev, u64 *count)
 {
-	return riscv_get_time(count);
+	if (IS_ENABLED(CONFIG_64BIT)) {
+		*count = csr_read(CSR_TIME);
+	} else {
+		u32 hi, lo;
+
+		do {
+			hi = csr_read(CSR_TIMEH);
+			lo = csr_read(CSR_TIME);
+		} while (hi != csr_read(CSR_TIMEH));
+
+		*count = ((u64)hi << 32) | lo;
+	}
+
+	return 0;
 }
 
 static int riscv_timer_probe(struct udevice *dev)
Signature · 1 line
2.28.0
#2 lore raw

PATCH v5 1/9 riscv: Rework riscv timer driver to only support S-mode

Quoted 32 lines

The riscv-timer driver currently serves as a shim for several riscv timer drivers. This is not too desirable because it bypasses the usual timer selection via the driver model. There is no easy way to specify an alternate timing driver, or have the tick rate depend on the cpu's configured frequency. The timer drivers also do not have device structs, and so have to rely on storing parameters in gd_t. Lastly, there is no initialization call, so driver init is done in the same function which reads the time. This can result in confusing error messages. To a user, it looks like the driver failed when trying to read the time, whereas it may have failed while initializing.

This patch removes the shim functionality from the riscv-timer driver, and has it instead implement the former rdtime.c timer driver. This is because existing u-boot users who pass in a device tree (e.g. qemu) do not create a timer device for S-mode u-boot. The existing behavior of creating the riscv-timer device in the riscv cpu driver must be kept. The actual reading of the CSRs has been redone in the style of Linux's get_cycles64.

---

(no changes since v4)

Changes in v4:
- Modify RISCV_TIMER KConfig
- Now depends on RISCV
- Implied by S-Mode (with or without SPL)

Changes in v2:
- Remove RISCV_RDTIME KConfig option

Quoted 11 lines
 arch/riscv/Kconfig             |  8 -------
 arch/riscv/cpu/ax25/Kconfig    |  2 +-
 arch/riscv/cpu/fu540/Kconfig   |  2 +-
 arch/riscv/cpu/generic/Kconfig |  2 +-
 arch/riscv/lib/Makefile        |  1 -
 arch/riscv/lib/rdtime.c        | 38 ---------------------------------
 drivers/timer/Kconfig          |  4 ++--
 drivers/timer/riscv_timer.c    | 39 +++++++++++++++++-----------------
 8 files changed, 25 insertions(+), 71 deletions(-)
 delete mode 100644 arch/riscv/lib/rdtime.c
#3 lore raw

PATCH v5 2/9 timer: Add helper for drivers using timebase fallback

In reply to Sean Anderson

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.

---

(no changes since v4)

Changes in v4:
- New

 drivers/timer/timer-uclass.c | 25 +++++++++++++++++++++++++
 include/timer.h              | 15 +++++++++++++++
 2 files changed, 40 insertions(+)
drivers/timer/timer-uclass.c +25 -0
index 14dde950a1..fb2f4c351a 100644
@@ -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 */
include/timer.h +15 -0
index a49b500ce3..8b9fa51c53 100644
@@ -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 at 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
  *
Signature · 1 line
2.28.0
#4 lore raw

PATCH v5 2/9 timer: Add helper for drivers using timebase fallback

Quoted 11 lines

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.

---

Quoted 9 lines

(no changes since v4)

Changes in v4:
- New

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

PATCH v5 2/9 timer: Add helper for drivers using timebase fallback

In reply to Sean Anderson

Hi Sean

Quoted 39 lines

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.

---

(no changes since v4)

Changes in v4:
- New

 drivers/timer/timer-uclass.c | 25 +++++++++++++++++++++++++
 include/timer.h              | 15 +++++++++++++++
 2 files changed, 40 insertions(+)
drivers/timer/timer-uclass.c +4 -0
index 14dde950a1..fb2f4c351a 100644
@@ -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;

drivers/timer/timer-uclass.c static int timer_post_probe(struct udevice *dev) · line 95

Quoted 10 lines
drivers/timer/timer-uclass.c +10 -0
+       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

Quoted 43 lines
drivers/timer/timer-uclass.c +11 -0
+       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 */
include/timer.h +15 -1
index a49b500ce3..8b9fa51c53 100644
@@ -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 at 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

#6 lore raw

PATCH v5 3/9 timer: Add a test for timer_timebase_fallback

In reply to Sean Anderson

To test this function, sandbox CPU must set cpu_platdata.timebase_freq on bind. It also needs to expose a method to set the current cpu. I also make some most members of cpu_sandbox_ops static.

On the timer side, the device tree property sandbox,timebase-frequency-fallback controls whether sandbox_timer_probe falls back to time_timebase_fallback or to SANDBOX_TIMER_RATE.

---

Changes in v5:
- New

 arch/sandbox/dts/test.dts      |  9 +++++++-
 arch/sandbox/include/asm/cpu.h | 11 ++++++++++
 drivers/cpu/cpu_sandbox.c      | 39 ++++++++++++++++++++++++++++------
 drivers/timer/sandbox_timer.c  |  4 +++-
 test/dm/timer.c                | 27 ++++++++++++++++++++++-
 5 files changed, 80 insertions(+), 10 deletions(-)
 create mode 100644 arch/sandbox/include/asm/cpu.h
arch/sandbox/dts/test.dts +8 -1
index 9f45c48e4e..2f559265a5 100644
@@ -533,7 +533,9 @@
 	};
 
 	cpus {
+		timebase-frequency = <2000000>;
 		cpu-test1 {
+			timebase-frequency = <3000000>;
 			compatible = "sandbox,cpu_sandbox";
 			u-boot,dm-pre-reloc;
 		};
@@ -839,11 +841,16 @@
 			0x58 8>;
 	};
 
-	timer {
+	timer at 0 {
 		compatible = "sandbox,timer";
 		clock-frequency = <1000000>;
 	};
 
+	timer at 1 {
+		compatible = "sandbox,timer";
+		sandbox,timebase-frequency-fallback;
+	};
+
 	tpm2 {
 		compatible = "sandbox,tpm2";
 	};
arch/sandbox/include/asm/cpu.h new file +11 -0
new file mode 100644
index 0000000000..c97ac7ba95
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2020 Sean Anderson <[email protected]>
+ */
+
+#ifndef __SANDBOX_CPU_H
+#define __SANDBOX_CPU_H
+
+void cpu_sandbox_set_current(const char *name);
+
+#endif /* __SANDBOX_CPU_H */
drivers/cpu/cpu_sandbox.c +32 -7
index caa26e50f2..4ba0d1b99e 100644
@@ -8,14 +8,15 @@
 #include <dm.h>
 #include <cpu.h>
 
-int cpu_sandbox_get_desc(const struct udevice *dev, char *buf, int size)
+static int cpu_sandbox_get_desc(const struct udevice *dev, char *buf, int size)
 {
 	snprintf(buf, size, "LEG Inc. SuperMegaUltraTurbo CPU No. 1");
 
 	return 0;
 }
 
-int cpu_sandbox_get_info(const struct udevice *dev, struct cpu_info *info)
+static int cpu_sandbox_get_info(const struct udevice *dev,
+				struct cpu_info *info)
 {
 	info->cpu_freq = 42 * 42 * 42 * 42 * 42;
 	info->features = 0x42424242;
@@ -24,21 +25,29 @@ int cpu_sandbox_get_info(const struct udevice *dev, struct cpu_info *info)
 	return 0;
 }
 
-int cpu_sandbox_get_count(const struct udevice *dev)
+static int cpu_sandbox_get_count(const struct udevice *dev)
 {
 	return 42;
 }
 
-int cpu_sandbox_get_vendor(const struct udevice *dev, char *buf, int size)
+static int cpu_sandbox_get_vendor(const struct udevice *dev, char *buf,
+				  int size)
 {
 	snprintf(buf, size, "Languid Example Garbage Inc.");
 
 	return 0;
 }
 
-int cpu_sandbox_is_current(struct udevice *dev)
+static const char *cpu_current = "cpu-test1";
+
+void cpu_sandbox_set_current(const char *name)
 {
-	if (!strcmp(dev->name, "cpu-test1"))
+	cpu_current = name;
+}
+
+static int cpu_sandbox_is_current(struct udevice *dev)
+{
+	if (!strcmp(dev->name, cpu_current))
 		return 1;
 
 	return 0;
@@ -52,7 +61,22 @@ static const struct cpu_ops cpu_sandbox_ops = {
 	.is_current = cpu_sandbox_is_current,
 };
 
-int cpu_sandbox_probe(struct udevice *dev)
+static int cpu_sandbox_bind(struct udevice *dev)
+{
+	int ret;
+	struct cpu_platdata *plat = dev_get_parent_platdata(dev);
+
+	/* first examine the property in current cpu node */
+	ret = dev_read_u32(dev, "timebase-frequency", &plat->timebase_freq);
+	/* if not found, then look at the parent /cpus node */
+	if (ret)
+		ret = dev_read_u32(dev->parent, "timebase-frequency",
+				   &plat->timebase_freq);
+
+	return ret;
+}
+
+static int cpu_sandbox_probe(struct udevice *dev)
 {
 	return 0;
 }
@@ -67,5 +91,6 @@ U_BOOT_DRIVER(cpu_sandbox) = {
 	.id             = UCLASS_CPU,
 	.ops		= &cpu_sandbox_ops,
 	.of_match       = cpu_sandbox_ids,
+	.bind		= cpu_sandbox_bind,
 	.probe          = cpu_sandbox_probe,
 };
drivers/timer/sandbox_timer.c +3 -1
index 5228486082..6a503c2f15 100644
@@ -40,7 +40,9 @@ static int sandbox_timer_probe(struct udevice *dev)
 {
 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
 
-	if (!uc_priv->clock_rate)
+	if (dev_read_bool(dev, "sandbox,timebase-frequency-fallback"))
+		return timer_timebase_fallback(dev);
+	else if (!uc_priv->clock_rate)
 		uc_priv->clock_rate = SANDBOX_TIMER_RATE;
 
 	return 0;
test/dm/timer.c +26 -1
index 95dab97665..70043b9eee 100644
@@ -7,8 +7,10 @@
 #include <dm.h>
 #include <timer.h>
 #include <dm/test.h>
+#include <dm/device-internal.h>
 #include <test/test.h>
 #include <test/ut.h>
+#include <asm/cpu.h>
 
 /*
  * Basic test of the timer uclass.
@@ -17,9 +19,32 @@ static int dm_test_timer_base(struct unit_test_state *uts)
 {
 	struct udevice *dev;
 
-	ut_assertok(uclass_get_device(UCLASS_TIMER, 0, &dev));
+	ut_assertok(uclass_get_device_by_name(UCLASS_TIMER, "timer at 0", &dev));
 	ut_asserteq(1000000, timer_get_rate(dev));
 
 	return 0;
 }
 DM_TEST(dm_test_timer_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+/*
+ * Test of timebase fallback
+ */
+static int dm_test_timer_timebase_fallback(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+
+	cpu_sandbox_set_current("cpu-test1");
+	ut_assertok(uclass_get_device_by_name(UCLASS_TIMER, "timer at 1", &dev));
+	ut_asserteq(3000000, timer_get_rate(dev));
+	ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
+
+	cpu_sandbox_set_current("cpu-test2");
+	ut_assertok(uclass_get_device_by_name(UCLASS_TIMER, "timer at 1", &dev));
+	ut_asserteq(2000000, timer_get_rate(dev));
+
+	cpu_sandbox_set_current("cpu-test1");
+
+	return 0;
+}
+DM_TEST(dm_test_timer_timebase_fallback,
+	UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Signature · 1 line
2.28.0
#7 lore raw

PATCH v5 3/9 timer: Add a test for timer_timebase_fallback

Quoted 15 lines · Sean Anderson

To test this function, sandbox CPU must set cpu_platdata.timebase_freq on bind. It also needs to expose a method to set the current cpu. I also make some most members of cpu_sandbox_ops static.

On the timer side, the device tree property sandbox,timebase-frequency-fallback controls whether sandbox_timer_probe falls back to time_timebase_fallback or to SANDBOX_TIMER_RATE.

---

Changes in v5:
- New

Quoted 7 lines
 arch/sandbox/dts/test.dts      |  9 +++++++-
 arch/sandbox/include/asm/cpu.h | 11 ++++++++++
 drivers/cpu/cpu_sandbox.c      | 39 ++++++++++++++++++++++++++++------
 drivers/timer/sandbox_timer.c  |  4 +++-
 test/dm/timer.c                | 27 ++++++++++++++++++++++-
 5 files changed, 80 insertions(+), 10 deletions(-)
 create mode 100644 arch/sandbox/include/asm/cpu.h
#8 lore raw

PATCH v5 4/9 riscv: Rework Andes PLMT as a UCLASS_TIMER driver

In reply to Sean Anderson

This converts the PLMT driver from the riscv-specific timer interface to be a DM-based UCLASS_TIMER driver.

The clock-frequency/clocks properties are preferred over timebase-frequency for two reasons. First, properties which affect a device should be located near its binding in the device tree. Using timebase-frequency only really makes sense when the cpu itself is the timer device. This is the case when we read the time from a CSR, but not when there is a separate device. Second, it lets the device use the clock subsystem which adds flexibility. If the device is configured for a different clock speed, the timer can adjust itself.

---
This patch builds but has NOT been tested.

Changes in v5:
- Remove RISCV_SYSCON_PLMT
- Undo changes to ae350 device trees. They are unnecessary with timer_timebase_fallback.

Changes in v4:
- Use timer_timebase_fallback

 arch/riscv/Kconfig                   |  4 ---
 arch/riscv/include/asm/global_data.h |  3 --
 arch/riscv/include/asm/syscon.h      |  4 +--
 arch/riscv/lib/andes_plmt.c          | 44 +++++++++++++---------------
 4 files changed, 23 insertions(+), 32 deletions(-)
arch/riscv/Kconfig +0 -4
index 21e6690f4d..d9155b9bab 100644
@@ -177,10 +177,6 @@ config ANDES_PLIC
 config ANDES_PLMT
 	bool
 	depends on RISCV_MMODE || SPL_RISCV_MMODE
-	select REGMAP
-	select SYSCON
-	select SPL_REGMAP if SPL
-	select SPL_SYSCON if SPL
 	help
 	  The Andes PLMT block holds memory-mapped mtime register
 	  associated with timer tick.
arch/riscv/include/asm/global_data.h +0 -3
index 2eb14815bc..0dec5e669e 100644
@@ -24,9 +24,6 @@ struct arch_global_data {
 #ifdef CONFIG_ANDES_PLIC
 	void __iomem *plic;	/* plic base address */
 #endif
-#ifdef CONFIG_ANDES_PLMT
-	void __iomem *plmt;	/* plmt base address */
-#endif
 #if CONFIG_IS_ENABLED(SMP)
 	struct ipi_data ipi[CONFIG_NR_CPUS];
 #endif
arch/riscv/include/asm/syscon.h +2 -2
index 26a008ca59..c3629e4b53 100644
@@ -7,13 +7,13 @@
 #define _ASM_SYSCON_H
 
 /*
- * System controllers in a RISC-V system
+ * System controllers in a RISC-V system. These should only be used for
+ * identifying IPI controllers. Other devices should use DM to probe.
  */
 enum {
 	RISCV_NONE,
 	RISCV_SYSCON_CLINT,	/* Core Local Interruptor (CLINT) */
 	RISCV_SYSCON_PLIC,	/* Platform Level Interrupt Controller (PLIC) */
-	RISCV_SYSCON_PLMT,	/* Platform Level Machine Timer (PLMT) */
 };
 
 #endif /* _ASM_SYSCON_H */
arch/riscv/lib/andes_plmt.c +21 -23

73 lines not on this page · raw message

Signature · 1 line
2.28.0
#9 lore raw

PATCH v5 5/9 riscv: Clean up initialization in Andes PLIC

In reply to Sean Anderson

This merges the PLIC initialization code from two functions into one.

---
This patch builds but has NOT been tested.

(no changes since v1)

 arch/riscv/lib/andes_plic.c | 58 ++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 33 deletions(-)
arch/riscv/lib/andes_plic.c +25 -33
index c2a8fe4d9e..267d6a191b 100644
@@ -41,53 +41,45 @@ static int enable_ipi(int hart)
 	return 0;
 }
 
-static int init_plic(void)
+int riscv_init_ipi(void)
 {
-	struct udevice *dev;
-	ofnode node;
 	int ret;
+	long *base = syscon_get_first_range(RISCV_SYSCON_PLIC);
+	ofnode node;
+	struct udevice *dev;
 	u32 reg;
 
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+	gd->arch.plic = base;
+
 	ret = uclass_find_first_device(UCLASS_CPU, &dev);
 	if (ret)
 		return ret;
+	else if (!dev)
+		return -ENODEV;
 
-	if (dev) {
-		ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
-			const char *device_type;
+	ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
+		const char *device_type;
 
-			device_type = ofnode_read_string(node, "device_type");
-			if (!device_type)
-				continue;
+		device_type = ofnode_read_string(node, "device_type");
+		if (!device_type)
+			continue;
 
-			if (strcmp(device_type, "cpu"))
-				continue;
+		if (strcmp(device_type, "cpu"))
+			continue;
 
-			/* skip if hart is marked as not available */
-			if (!ofnode_is_available(node))
-				continue;
+		/* skip if hart is marked as not available */
+		if (!ofnode_is_available(node))
+			continue;
 
-			/* read hart ID of CPU */
-			ret = ofnode_read_u32(node, "reg", &reg);
-			if (ret == 0)
-				enable_ipi(reg);
-		}
-
-		return 0;
+		/* read hart ID of CPU */
+		ret = ofnode_read_u32(node, "reg", &reg);
+		if (ret == 0)
+			enable_ipi(reg);
 	}
 
-	return -ENODEV;
-}
-
-int riscv_init_ipi(void)
-{
-	long *ret = syscon_get_first_range(RISCV_SYSCON_PLIC);
-
-	if (IS_ERR(ret))
-		return PTR_ERR(ret);
-	gd->arch.plic = ret;
-
-	return init_plic();
+	return 0;
 }
 
 int riscv_send_ipi(int hart)
Signature · 1 line
2.28.0
#10 lore raw

PATCH v5 6/9 riscv: Rework Sifive CLINT as UCLASS_TIMER driver

In reply to Sean Anderson

This converts the clint driver from the riscv-specific interface to be a DM-based UCLASS_TIMER driver. In addition, the SiFive DDR driver previously implicitly depended on the CLINT to select REGMAP.

Unlike Andes's PLMT/PLIC (which AFAIK never have anything pass it a dtb), the SiFive CLINT is part of the device tree passed in by qemu. This device tree doesn't have a clocks or clock-frequency property on clint, so we need to fall back on the timebase-frequency property. Perhaps in the future we can get a clock-frequency property added to the qemu dtb.

Unlike with the Andes PLMT, the Sifive CLINT is also an IPI controller. RISCV_SYSCON_CLINT is retained for this purpose.

---
This patch builds but has only been tested on the K210 and QEMU. It has NOT been tested on a HiFive.

Changes in v5:
- Don't add a dependency on REGMAP for SIFIVE_FU540_DDR. Instead, depend on Bin Meng's patch.

Changes in v4:
- Use timer_timebase_fallback

Changes in v3:
- Don't initialize the IPI in spl_invoke_opensbi. Further testing has revealed it to be unnecessary.

 arch/riscv/Kconfig            |  4 ---
 arch/riscv/lib/sifive_clint.c | 66 +++++++++++++++++++----------------
 2 files changed, 36 insertions(+), 34 deletions(-)
arch/riscv/Kconfig +0 -4
index d9155b9bab..aaa3b833a5 100644
@@ -155,10 +155,6 @@ config 64BIT
 config SIFIVE_CLINT
 	bool
 	depends on RISCV_MMODE || SPL_RISCV_MMODE
-	select REGMAP
-	select SYSCON
-	select SPL_REGMAP if SPL
-	select SPL_SYSCON if SPL
 	help
 	  The SiFive CLINT block holds memory-mapped control and status registers
 	  associated with software and timer interrupts.
arch/riscv/lib/sifive_clint.c +36 -30

94 lines not on this page · raw message

Signature · 1 line
2.28.0
#11 lore raw

PATCH v5 7/9 riscv: clk: Add CLINT clock to kendryte clock driver

In reply to Sean Anderson

Another "virtual" clock (in the sense that it isn't configurable). This could possibly be done as a clock in the device tree, but I think this is a bit cleaner.

---
checkpatch still complains about this one, but I don't see any reason to break it up even further. It doesn't make sense to me to split the header file change from everything else.

(no changes since v2)

Changes in v2:
- Split Kendryte binding changes into their own commit

 drivers/clk/kendryte/clk.c              | 4 ++++
 include/dt-bindings/clock/k210-sysctl.h | 1 +
 2 files changed, 5 insertions(+)
drivers/clk/kendryte/clk.c +4 -0
index 981b3b7699..bb196961af 100644
@@ -646,6 +646,10 @@ static int k210_clk_probe(struct udevice *dev)
 	REGISTER_GATE(K210_CLK_RTC,   "rtc",   in0);
 #undef REGISTER_GATE
 
+	/* The MTIME register in CLINT runs at one 50th the CPU clock speed */
+	clk_dm(K210_CLK_CLINT,
+	       clk_register_fixed_factor(NULL, "clint", "cpu", 0, 1, 50));
+
 	return 0;
 }
 
include/dt-bindings/clock/k210-sysctl.h +1 -0
index 0e3ed3fb9f..fe852bbd92 100644
@@ -55,5 +55,6 @@
 #define K210_CLK_OTP    43
 #define K210_CLK_RTC    44
 #define K210_CLK_ACLK   45
+#define K210_CLK_CLINT  46
 
 #endif /* CLOCK_K210_SYSCTL_H */
Signature · 1 line
2.28.0
#12 lore raw

PATCH v5 8/9 riscv: Update Kendryte device tree for new CLINT driver

In reply to Sean Anderson

The interrupt controller property is removed from the clint binding because the clint is not an interrupt-controller. That is, no other devices have an interrupt which is controlled by the clint.

---

(no changes since v4)

Changes in v4:
- Remove clock-frequency property from k210 clint binding because we fall back on timebase-frequency

Changes in v2:
- New

 arch/riscv/dts/k210.dtsi | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/riscv/dts/k210.dtsi +4 -3
index 2546c7d4e0..84cff51c36 100644
@@ -17,6 +17,8 @@
 	compatible = "kendryte,k210";
 
 	aliases {
+		cpu0 = &cpu0;
+		cpu1 = &cpu1;
 		dma0 = &dmac0;
 		gpio0 = &gpio0;
 		gpio1 = &gpio1_0;
@@ -126,14 +128,13 @@
 			read-only;
 		};
 
-		clint0: interrupt-controller at 2000000 {
+		clint0: clint at 2000000 {
 			#interrupt-cells = <1>;
 			compatible = "kendryte,k210-clint", "riscv,clint0";
 			reg = <0x2000000 0xC000>;
-			interrupt-controller;
 			interrupts-extended = <&cpu0_intc 3>, <&cpu0_intc 7>,
 					      <&cpu1_intc 3>, <&cpu1_intc 7>;
-			clocks = <&sysclk K210_CLK_CPU>;
+			clocks = <&sysclk K210_CLK_CLINT>;
 		};
 
 		plic0: interrupt-controller at C000000 {
Signature · 1 line
2.28.0
#13 lore raw

PATCH v5 9/9 riscv: Update SiFive device tree for new CLINT driver

In reply to Sean Anderson

We currently do this in a u-boot specific dts, but hopefully we can get these bindings added in Linux in the future.

---
This patch builds but has NOT been tested.

(no changes since v2)

Changes in v2:
- Fix SiFive CLINT not getting tick-rate from rtcclk

 arch/riscv/dts/fu540-c000-u-boot.dtsi           | 8 ++++++--
 arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi | 4 ++++
 2 files changed, 10 insertions(+), 2 deletions(-)
arch/riscv/dts/fu540-c000-u-boot.dtsi +6 -2
index 5302677ee4..a06e1b11c6 100644
@@ -55,9 +55,13 @@
 			reg = <0x0 0x10070000 0x0 0x1000>;
 			fuse-count = <0x1000>;
 		};
-		clint at 2000000 {
+		clint: clint at 2000000 {
 			compatible = "riscv,clint0";
-			interrupts-extended = <&cpu0_intc 3 &cpu0_intc 7 &cpu1_intc 3 &cpu1_intc 7 &cpu2_intc 3 &cpu2_intc 7 &cpu3_intc 3 &cpu3_intc 7 &cpu4_intc 3 &cpu4_intc 7>;
+			interrupts-extended = <&cpu0_intc 3 &cpu0_intc 7
+					       &cpu1_intc 3 &cpu1_intc 7
+					       &cpu2_intc 3 &cpu2_intc 7
+					       &cpu3_intc 3 &cpu3_intc 7
+					       &cpu4_intc 3 &cpu4_intc 7>;
 			reg = <0x0 0x2000000 0x0 0xc0000>;
 			u-boot,dm-spl;
 		};
arch/riscv/dts/hifive-unleashed-a00-u-boot.dtsi +4 -0
index 5d0c928b29..1996149c95 100644
@@ -34,6 +34,10 @@
 
 };
 
+&clint {
+	clocks = <&rtcclk>;
+};
+
 &qspi0 {
 	u-boot,dm-spl;
 
Signature · 1 line
2.28.0