blob: 112690fe3a4bedc86989e467b38ae62870d63c32 [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>
Bin Mengc4fcb622018-12-12 06:12:26 -080013#include <dm/device-internal.h>
14#include <dm/lists.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Sean Anderson47244092020-06-24 06:41:21 -040016#include <linux/err.h>
Bin Mengc4fcb622018-12-12 06:12:26 -080017
Atish Patra04098f92019-02-25 08:15:14 +000018DECLARE_GLOBAL_DATA_PTR;
19
Bin Mengc4fcb622018-12-12 06:12:26 -080020static int riscv_cpu_get_desc(struct udevice *dev, char *buf, int size)
21{
22 const char *isa;
23
24 isa = dev_read_string(dev, "riscv,isa");
25 if (size < (strlen(isa) + 1))
26 return -ENOSPC;
27
28 strcpy(buf, isa);
29
30 return 0;
31}
32
33static int riscv_cpu_get_info(struct udevice *dev, struct cpu_info *info)
34{
Sean Anderson47244092020-06-24 06:41:21 -040035 int ret;
36 struct clk clk;
Bin Mengc4fcb622018-12-12 06:12:26 -080037 const char *mmu;
38
Sean Anderson47244092020-06-24 06:41:21 -040039 /* First try getting the frequency from the assigned clock */
40 ret = clk_get_by_index(dev, 0, &clk);
41 if (!ret) {
42 ret = clk_get_rate(&clk);
43 if (!IS_ERR_VALUE(ret))
44 info->cpu_freq = ret;
45 clk_free(&clk);
46 }
47
48 if (!info->cpu_freq)
49 dev_read_u32(dev, "clock-frequency", (u32 *)&info->cpu_freq);
Bin Mengc4fcb622018-12-12 06:12:26 -080050
51 mmu = dev_read_string(dev, "mmu-type");
Sagar Shrikant Kadamafc0c192020-06-28 07:45:02 -070052 if (mmu)
Bin Mengc4fcb622018-12-12 06:12:26 -080053 info->features |= BIT(CPU_FEAT_MMU);
54
55 return 0;
56}
57
58static int riscv_cpu_get_count(struct udevice *dev)
59{
60 ofnode node;
61 int num = 0;
62
63 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
64 const char *device_type;
65
Bin Meng33d46a22019-08-08 00:52:08 -070066 /* skip if hart is marked as not available in the device tree */
67 if (!ofnode_is_available(node))
68 continue;
69
Bin Mengc4fcb622018-12-12 06:12:26 -080070 device_type = ofnode_read_string(node, "device_type");
71 if (!device_type)
72 continue;
73 if (strcmp(device_type, "cpu") == 0)
74 num++;
75 }
76
77 return num;
78}
79
80static int riscv_cpu_bind(struct udevice *dev)
81{
82 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
83 struct driver *drv;
84 int ret;
85
86 /* save the hart id */
87 plat->cpu_id = dev_read_addr(dev);
Bin Mengc4fcb622018-12-12 06:12:26 -080088 /* first examine the property in current cpu node */
89 ret = dev_read_u32(dev, "timebase-frequency", &plat->timebase_freq);
90 /* if not found, then look at the parent /cpus node */
91 if (ret)
92 dev_read_u32(dev->parent, "timebase-frequency",
93 &plat->timebase_freq);
94
95 /*
Atish Patra04098f92019-02-25 08:15:14 +000096 * Bind riscv-timer driver on boot hart.
Bin Mengc4fcb622018-12-12 06:12:26 -080097 *
98 * We only instantiate one timer device which is enough for U-Boot.
99 * Pass the "timebase-frequency" value as the driver data for the
100 * timer device.
101 *
102 * Return value is not checked since it's possible that the timer
103 * driver is not included.
104 */
Atish Patra04098f92019-02-25 08:15:14 +0000105 if (plat->cpu_id == gd->arch.boot_hart && plat->timebase_freq) {
Bin Mengc4fcb622018-12-12 06:12:26 -0800106 drv = lists_driver_lookup_name("riscv_timer");
107 if (!drv) {
108 debug("Cannot find the timer driver, not included?\n");
109 return 0;
110 }
111
112 device_bind_with_driver_data(dev, drv, "riscv_timer",
113 plat->timebase_freq, ofnode_null(),
114 NULL);
115 }
116
117 return 0;
118}
119
Sean Andersond2a10ff2020-06-24 06:41:22 -0400120static int riscv_cpu_probe(struct udevice *dev)
121{
122 int ret = 0;
123 struct clk clk;
124
125 /* Get a clock if it exists */
126 ret = clk_get_by_index(dev, 0, &clk);
127 if (ret)
128 return 0;
129
130 ret = clk_enable(&clk);
131 clk_free(&clk);
132 if (ret == -ENOSYS || ret == -ENOTSUPP)
133 return 0;
134 else
135 return ret;
136}
137
Bin Mengc4fcb622018-12-12 06:12:26 -0800138static const struct cpu_ops riscv_cpu_ops = {
139 .get_desc = riscv_cpu_get_desc,
140 .get_info = riscv_cpu_get_info,
141 .get_count = riscv_cpu_get_count,
142};
143
144static const struct udevice_id riscv_cpu_ids[] = {
145 { .compatible = "riscv" },
146 { }
147};
148
149U_BOOT_DRIVER(riscv_cpu) = {
150 .name = "riscv_cpu",
151 .id = UCLASS_CPU,
152 .of_match = riscv_cpu_ids,
153 .bind = riscv_cpu_bind,
Sean Andersond2a10ff2020-06-24 06:41:22 -0400154 .probe = riscv_cpu_probe,
Bin Mengc4fcb622018-12-12 06:12:26 -0800155 .ops = &riscv_cpu_ops,
156 .flags = DM_FLAG_PRE_RELOC,
157};