diff mbox series

[v4,14/17] riscv: Try to get cpu frequency from device tree

Message ID 20200211060425.1619471-15-seanga2@gmail.com
State Superseded
Delegated to: Andes
Headers show
Series riscv: Add Sipeed Maix support | expand

Commit Message

Sean Anderson Feb. 11, 2020, 6:04 a.m. UTC
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).

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
---
This patch was previously sumbitted on its own as
https://patchwork.ozlabs.org/patch/1232420/

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 in v4:
- New

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

Comments

Bin Meng Feb. 19, 2020, 4:12 p.m. UTC | #1
Hi Sean,

On Tue, Feb 11, 2020 at 2:05 PM Sean Anderson <seanga2@gmail.com> wrote:
>
> 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).
>

I just realized the commit title looked a little bit confusing.

I think it could be more specific like:

riscv: Try to get cpu frequency from a "clocks" node if it exists

?

Regards,
Bin
Sean Anderson Feb. 19, 2020, 8:02 p.m. UTC | #2
On 2/19/20 11:12 AM, Bin Meng wrote:
> Hi Sean,
> 
> On Tue, Feb 11, 2020 at 2:05 PM Sean Anderson <seanga2@gmail.com> wrote:
>>
>> 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).
>>
> 
> I just realized the commit title looked a little bit confusing.
> 
> I think it could be more specific like:
> 
> riscv: Try to get cpu frequency from a "clocks" node if it exists
> 
> ?
> 
> Regards,
> Bin
> 

That makes sense to me.

--Sean
diff mbox series

Patch

diff --git a/drivers/cpu/riscv_cpu.c b/drivers/cpu/riscv_cpu.c
index 28ad0aa30f..5309a49e60 100644
--- a/drivers/cpu/riscv_cpu.c
+++ b/drivers/cpu/riscv_cpu.c
@@ -3,6 +3,7 @@ 
  * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  */
 
+#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)