Skip to contentSkip to conversation

riscv: Try to get cpu frequency from device tree

PATCH New Sean Anderson 2 participants 4 messages Feb 2, 2020 – Feb 4, 2020 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 riscv: Try to get cpu frequency from device tree

Instead of always using the "clock-frequency" property to determine cpu frequency, try using a clock in "clocks" if it exists. This patch also fixes a bug where there could be spurious higher frequencies if sizeof(u32) != sizeof(ulong).

---
This patch is the combination of the patches
https://patchwork.ozlabs.org/patch/1223933/
https://patchwork.ozlabs.org/patch/1224957/
"riscv: Fix incorrect cpu frequency on RV64" "riscv: Try to get cpu frequency from device tree"

Changes for v2:
- Renamed err to ret

 drivers/cpu/riscv_cpu.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/cpu/riscv_cpu.c +17 -1
index 28ad0aa30f..5309a49e60 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2018, Bin Meng <[email protected]>
  */
 
+#include <clk.h>
 #include <common.h>
 #include <cpu.h>
 #include <dm.h>
@@ -27,9 +28,24 @@ static int riscv_cpu_get_desc(struct udevice *dev, char *buf, int size)
 
 static int riscv_cpu_get_info(struct udevice *dev, struct cpu_info *info)
 {
+	int ret;
+	struct clk clk;
 	const char *mmu;
 
-	dev_read_u32(dev, "clock-frequency", (u32 *)&info->cpu_freq);
+	/* Zero out the frequency, in case sizeof(ulong) != sizeof(u32) */
+	info->cpu_freq = 0;
+
+	/* First try getting the frequency from the assigned clock */
+	ret = clk_get_by_index(dev, 0, &clk);
+	if (!ret) {
+		ret = clk_get_rate(&clk);
+		if (!IS_ERR_VALUE(ret))
+			info->cpu_freq = ret;
+		clk_free(&clk);
+	}
+
+	if (!info->cpu_freq)
+		dev_read_u32(dev, "clock-frequency", (u32 *)&info->cpu_freq);
 
 	mmu = dev_read_string(dev, "mmu-type");
 	if (!mmu)
Signature · 1 line
2.25.0
#1 lore raw

PATCH v2 2/2 riscv: Enable cpu clock if it is present

The cpu clock is probably already enabled if we are executing code (though we could be executing from a different core). This patch prevents the cpu clock or its parents from being disabled.

---
This doesn't strictly depend on the previous patch, but it doesn't make too much sense without it.

Changes for v2:
- New

 drivers/cpu/riscv_cpu.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/cpu/riscv_cpu.c +20 -0
index 5309a49e60..52b74d9e69 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2018, Bin Meng <[email protected]>
+ * Copyright (C) 2020, Sean Anderson <[email protected]>
  */
 
 #include <clk.h>
@@ -116,6 +117,24 @@ static int riscv_cpu_bind(struct udevice *dev)
 	return 0;
 }
 
+static int riscv_cpu_probe(struct udevice *dev)
+{
+	int ret = 0;
+	struct clk clk;
+
+	/* Get a clock if it exists */
+	ret = clk_get_by_index(dev, 0, &clk);
+	if (ret)
+		return 0;
+
+	ret = clk_enable(&clk);
+	clk_free(&clk);
+	if (ret == -ENOSYS || ret == -ENOTSUPP)
+		return 0;
+	else
+		return ret;
+}
+
 static const struct cpu_ops riscv_cpu_ops = {
 	.get_desc	= riscv_cpu_get_desc,
 	.get_info	= riscv_cpu_get_info,
@@ -132,6 +151,7 @@ U_BOOT_DRIVER(riscv_cpu) = {
 	.id = UCLASS_CPU,
 	.of_match = riscv_cpu_ids,
 	.bind = riscv_cpu_bind,
+	.probe = riscv_cpu_probe,
 	.ops = &riscv_cpu_ops,
 	.flags = DM_FLAG_PRE_RELOC,
 };
Signature · 1 line
2.25.0
#2 lore raw

PATCH v2 2/2 riscv: Enable cpu clock if it is present

Quoted 6 lines · Sean Anderson

The cpu clock is probably already enabled if we are executing code (though we could be executing from a different core). This patch prevents the cpu clock or its parents from being disabled.

Quoted 9 lines

---
This doesn't strictly depend on the previous patch, but it doesn't make too much sense without it.

Changes for v2:
- New

 drivers/cpu/riscv_cpu.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

I believe both 2 patches in this series are needed by "riscv: Add Sipeed Maix support" series?
If yes, I think you can just put 2 patches into the same series, to give people a good context next time.

Regards,
Bin

#3 lore raw

PATCH v2 1/2 riscv: Try to get cpu frequency from device tree

In reply to Sean Anderson
Quoted 14 lines · Sean Anderson

Instead of always using the "clock-frequency" property to determine cpu frequency, try using a clock in "clocks" if it exists. This patch also fixes a bug where there could be spurious higher frequencies if sizeof(u32) != sizeof(ulong).

---
This patch is the combination of the patches
https://patchwork.ozlabs.org/patch/1223933/
https://patchwork.ozlabs.org/patch/1224957/
"riscv: Fix incorrect cpu frequency on RV64" "riscv: Try to get cpu frequency from device tree"

Quoted 5 lines

Changes for v2:
- Renamed err to ret

 drivers/cpu/riscv_cpu.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)