blob: 5d1026b37da9ce6df5a1ba39526db92687f9c406 [file] [log] [blame]
Bin Mengc4fcb622018-12-12 06:12:26 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
Sean Andersond2a10ff2020-06-24 06:41:22 -04004 * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com>
Bin Mengc4fcb622018-12-12 06:12:26 -08005 */
6
Sean Anderson47244092020-06-24 06:41:21 -04007#include <clk.h>
Bin Mengc4fcb622018-12-12 06:12:26 -08008#include <common.h>
9#include <cpu.h>
10#include <dm.h>
11#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Heinrich Schuchardt6403e3d2024-01-29 12:46:05 +010014#include <asm/sbi.h>
Bin Mengc4fcb622018-12-12 06:12:26 -080015#include <dm/device-internal.h>
16#include <dm/lists.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Sean Anderson47244092020-06-24 06:41:21 -040018#include <linux/err.h>
Bin Mengc4fcb622018-12-12 06:12:26 -080019
Atish Patra04098f92019-02-25 08:15:14 +000020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass791fa452020-01-26 22:06:27 -070022static int riscv_cpu_get_desc(const struct udevice *dev, char *buf, int size)
Bin Mengc4fcb622018-12-12 06:12:26 -080023{
24 const char *isa;
25
26 isa = dev_read_string(dev, "riscv,isa");
27 if (size < (strlen(isa) + 1))
28 return -ENOSPC;
29
30 strcpy(buf, isa);
31
32 return 0;
33}
34
Simon Glass791fa452020-01-26 22:06:27 -070035static int riscv_cpu_get_info(const struct udevice *dev, struct cpu_info *info)
Bin Mengc4fcb622018-12-12 06:12:26 -080036{
Sean Anderson47244092020-06-24 06:41:21 -040037 int ret;
38 struct clk clk;
Bin Mengc4fcb622018-12-12 06:12:26 -080039 const char *mmu;
Sagar Shrikant Kadam4ae7a952020-06-28 07:45:03 -070040 u32 i_cache_size;
41 u32 d_cache_size;
Bin Mengc4fcb622018-12-12 06:12:26 -080042
Sean Anderson47244092020-06-24 06:41:21 -040043 /* First try getting the frequency from the assigned clock */
Simon Glass791fa452020-01-26 22:06:27 -070044 ret = clk_get_by_index((struct udevice *)dev, 0, &clk);
Sean Anderson47244092020-06-24 06:41:21 -040045 if (!ret) {
46 ret = clk_get_rate(&clk);
47 if (!IS_ERR_VALUE(ret))
48 info->cpu_freq = ret;
Sean Anderson47244092020-06-24 06:41:21 -040049 }
50
51 if (!info->cpu_freq)
52 dev_read_u32(dev, "clock-frequency", (u32 *)&info->cpu_freq);
Bin Mengc4fcb622018-12-12 06:12:26 -080053
54 mmu = dev_read_string(dev, "mmu-type");
Sagar Shrikant Kadamafc0c192020-06-28 07:45:02 -070055 if (mmu)
Bin Mengc4fcb622018-12-12 06:12:26 -080056 info->features |= BIT(CPU_FEAT_MMU);
57
Sagar Shrikant Kadam4ae7a952020-06-28 07:45:03 -070058 /* check if I cache is present */
59 ret = dev_read_u32(dev, "i-cache-size", &i_cache_size);
60 if (ret)
61 /* if not found check if d-cache is present */
62 ret = dev_read_u32(dev, "d-cache-size", &d_cache_size);
63
64 /* if either I or D cache is present set L1 cache feature */
65 if (!ret)
66 info->features |= BIT(CPU_FEAT_L1_CACHE);
67
Bin Mengc4fcb622018-12-12 06:12:26 -080068 return 0;
69}
70
Simon Glass791fa452020-01-26 22:06:27 -070071static int riscv_cpu_get_count(const struct udevice *dev)
Bin Mengc4fcb622018-12-12 06:12:26 -080072{
73 ofnode node;
74 int num = 0;
75
76 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
77 const char *device_type;
78
Bin Meng33d46a22019-08-08 00:52:08 -070079 /* skip if hart is marked as not available in the device tree */
Simon Glass2e4938b2022-09-06 20:27:17 -060080 if (!ofnode_is_enabled(node))
Bin Meng33d46a22019-08-08 00:52:08 -070081 continue;
82
Bin Mengc4fcb622018-12-12 06:12:26 -080083 device_type = ofnode_read_string(node, "device_type");
84 if (!device_type)
85 continue;
86 if (strcmp(device_type, "cpu") == 0)
87 num++;
88 }
89
90 return num;
91}
92
93static int riscv_cpu_bind(struct udevice *dev)
94{
Simon Glassb75b15b2020-12-03 16:55:23 -070095 struct cpu_plat *plat = dev_get_parent_plat(dev);
Bin Mengc4fcb622018-12-12 06:12:26 -080096 struct driver *drv;
97 int ret;
Heinrich Schuchardt6403e3d2024-01-29 12:46:05 +010098 long mvendorid;
Bin Mengc4fcb622018-12-12 06:12:26 -080099
100 /* save the hart id */
101 plat->cpu_id = dev_read_addr(dev);
Heinrich Schuchardt6403e3d2024-01-29 12:46:05 +0100102 /* provide data for SMBIOS */
Heinrich Schuchardtc0c63fa2023-12-28 08:30:24 +0100103 if (IS_ENABLED(CONFIG_64BIT))
104 plat->family = 0x201;
105 else
106 plat->family = 0x200;
Heinrich Schuchardt6403e3d2024-01-29 12:46:05 +0100107 if (CONFIG_IS_ENABLED(RISCV_SMODE)) {
108 /*
109 * For RISC-V CPUs the SMBIOS Processor ID field contains
110 * the Machine Vendor ID from CSR mvendorid.
111 */
112 ret = sbi_get_mvendorid(&mvendorid);
113 if (!ret)
114 plat->id[0] = mvendorid;
115 }
Bin Mengc4fcb622018-12-12 06:12:26 -0800116 /* first examine the property in current cpu node */
117 ret = dev_read_u32(dev, "timebase-frequency", &plat->timebase_freq);
118 /* if not found, then look at the parent /cpus node */
119 if (ret)
120 dev_read_u32(dev->parent, "timebase-frequency",
121 &plat->timebase_freq);
122
123 /*
Atish Patra04098f92019-02-25 08:15:14 +0000124 * Bind riscv-timer driver on boot hart.
Bin Mengc4fcb622018-12-12 06:12:26 -0800125 *
126 * We only instantiate one timer device which is enough for U-Boot.
127 * Pass the "timebase-frequency" value as the driver data for the
128 * timer device.
129 *
130 * Return value is not checked since it's possible that the timer
131 * driver is not included.
132 */
Atish Patra04098f92019-02-25 08:15:14 +0000133 if (plat->cpu_id == gd->arch.boot_hart && plat->timebase_freq) {
Bin Mengc4fcb622018-12-12 06:12:26 -0800134 drv = lists_driver_lookup_name("riscv_timer");
135 if (!drv) {
136 debug("Cannot find the timer driver, not included?\n");
137 return 0;
138 }
139
140 device_bind_with_driver_data(dev, drv, "riscv_timer",
141 plat->timebase_freq, ofnode_null(),
142 NULL);
143 }
144
145 return 0;
146}
147
Sean Andersond2a10ff2020-06-24 06:41:22 -0400148static int riscv_cpu_probe(struct udevice *dev)
149{
150 int ret = 0;
151 struct clk clk;
152
153 /* Get a clock if it exists */
154 ret = clk_get_by_index(dev, 0, &clk);
155 if (ret)
156 return 0;
157
158 ret = clk_enable(&clk);
Sean Andersond2a10ff2020-06-24 06:41:22 -0400159 if (ret == -ENOSYS || ret == -ENOTSUPP)
160 return 0;
161 else
162 return ret;
163}
164
Bin Mengc4fcb622018-12-12 06:12:26 -0800165static const struct cpu_ops riscv_cpu_ops = {
166 .get_desc = riscv_cpu_get_desc,
167 .get_info = riscv_cpu_get_info,
168 .get_count = riscv_cpu_get_count,
169};
170
171static const struct udevice_id riscv_cpu_ids[] = {
172 { .compatible = "riscv" },
173 { }
174};
175
176U_BOOT_DRIVER(riscv_cpu) = {
177 .name = "riscv_cpu",
178 .id = UCLASS_CPU,
179 .of_match = riscv_cpu_ids,
180 .bind = riscv_cpu_bind,
Sean Andersond2a10ff2020-06-24 06:41:22 -0400181 .probe = riscv_cpu_probe,
Bin Mengc4fcb622018-12-12 06:12:26 -0800182 .ops = &riscv_cpu_ops,
183 .flags = DM_FLAG_PRE_RELOC,
184};