blob: cb04f5638db16b69c9625617456fc7684fe285ab [file] [log] [blame]
Bin Meng833508c2018-12-12 06:12:26 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
4 */
5
6#include <common.h>
7#include <cpu.h>
8#include <dm.h>
9#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Bin Meng833508c2018-12-12 06:12:26 -080011#include <dm/device-internal.h>
12#include <dm/lists.h>
Simon Glasscd93d622020-05-10 11:40:13 -060013#include <linux/bitops.h>
Bin Meng833508c2018-12-12 06:12:26 -080014
Atish Patra007056f2019-02-25 08:15:14 +000015DECLARE_GLOBAL_DATA_PTR;
16
Bin Meng833508c2018-12-12 06:12:26 -080017static int riscv_cpu_get_desc(struct udevice *dev, char *buf, int size)
18{
19 const char *isa;
20
21 isa = dev_read_string(dev, "riscv,isa");
22 if (size < (strlen(isa) + 1))
23 return -ENOSPC;
24
25 strcpy(buf, isa);
26
27 return 0;
28}
29
30static int riscv_cpu_get_info(struct udevice *dev, struct cpu_info *info)
31{
32 const char *mmu;
33
34 dev_read_u32(dev, "clock-frequency", (u32 *)&info->cpu_freq);
35
36 mmu = dev_read_string(dev, "mmu-type");
37 if (!mmu)
38 info->features |= BIT(CPU_FEAT_MMU);
39
40 return 0;
41}
42
43static int riscv_cpu_get_count(struct udevice *dev)
44{
45 ofnode node;
46 int num = 0;
47
48 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
49 const char *device_type;
50
Bin Meng4dfea4b2019-08-08 00:52:08 -070051 /* skip if hart is marked as not available in the device tree */
52 if (!ofnode_is_available(node))
53 continue;
54
Bin Meng833508c2018-12-12 06:12:26 -080055 device_type = ofnode_read_string(node, "device_type");
56 if (!device_type)
57 continue;
58 if (strcmp(device_type, "cpu") == 0)
59 num++;
60 }
61
62 return num;
63}
64
65static int riscv_cpu_bind(struct udevice *dev)
66{
67 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
68 struct driver *drv;
69 int ret;
70
71 /* save the hart id */
72 plat->cpu_id = dev_read_addr(dev);
Bin Meng833508c2018-12-12 06:12:26 -080073 /* first examine the property in current cpu node */
74 ret = dev_read_u32(dev, "timebase-frequency", &plat->timebase_freq);
75 /* if not found, then look at the parent /cpus node */
76 if (ret)
77 dev_read_u32(dev->parent, "timebase-frequency",
78 &plat->timebase_freq);
79
80 /*
Atish Patra007056f2019-02-25 08:15:14 +000081 * Bind riscv-timer driver on boot hart.
Bin Meng833508c2018-12-12 06:12:26 -080082 *
83 * We only instantiate one timer device which is enough for U-Boot.
84 * Pass the "timebase-frequency" value as the driver data for the
85 * timer device.
86 *
87 * Return value is not checked since it's possible that the timer
88 * driver is not included.
89 */
Atish Patra007056f2019-02-25 08:15:14 +000090 if (plat->cpu_id == gd->arch.boot_hart && plat->timebase_freq) {
Bin Meng833508c2018-12-12 06:12:26 -080091 drv = lists_driver_lookup_name("riscv_timer");
92 if (!drv) {
93 debug("Cannot find the timer driver, not included?\n");
94 return 0;
95 }
96
97 device_bind_with_driver_data(dev, drv, "riscv_timer",
98 plat->timebase_freq, ofnode_null(),
99 NULL);
100 }
101
102 return 0;
103}
104
105static const struct cpu_ops riscv_cpu_ops = {
106 .get_desc = riscv_cpu_get_desc,
107 .get_info = riscv_cpu_get_info,
108 .get_count = riscv_cpu_get_count,
109};
110
111static const struct udevice_id riscv_cpu_ids[] = {
112 { .compatible = "riscv" },
113 { }
114};
115
116U_BOOT_DRIVER(riscv_cpu) = {
117 .name = "riscv_cpu",
118 .id = UCLASS_CPU,
119 .of_match = riscv_cpu_ids,
120 .bind = riscv_cpu_bind,
121 .ops = &riscv_cpu_ops,
122 .flags = DM_FLAG_PRE_RELOC,
123};