1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
11 #include <dm/device-internal.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 static int riscv_cpu_get_desc(struct udevice *dev, char *buf, int size)
20 isa = dev_read_string(dev, "riscv,isa");
21 if (size < (strlen(isa) + 1))
29 static int riscv_cpu_get_info(struct udevice *dev, struct cpu_info *info)
33 dev_read_u32(dev, "clock-frequency", (u32 *)&info->cpu_freq);
35 mmu = dev_read_string(dev, "mmu-type");
37 info->features |= BIT(CPU_FEAT_MMU);
42 static int riscv_cpu_get_count(struct udevice *dev)
47 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
48 const char *device_type;
50 /* skip if hart is marked as not available in the device tree */
51 if (!ofnode_is_available(node))
54 device_type = ofnode_read_string(node, "device_type");
57 if (strcmp(device_type, "cpu") == 0)
64 static int riscv_cpu_bind(struct udevice *dev)
66 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
70 /* save the hart id */
71 plat->cpu_id = dev_read_addr(dev);
72 /* first examine the property in current cpu node */
73 ret = dev_read_u32(dev, "timebase-frequency", &plat->timebase_freq);
74 /* if not found, then look at the parent /cpus node */
76 dev_read_u32(dev->parent, "timebase-frequency",
77 &plat->timebase_freq);
80 * Bind riscv-timer driver on boot hart.
82 * We only instantiate one timer device which is enough for U-Boot.
83 * Pass the "timebase-frequency" value as the driver data for the
86 * Return value is not checked since it's possible that the timer
87 * driver is not included.
89 if (plat->cpu_id == gd->arch.boot_hart && plat->timebase_freq) {
90 drv = lists_driver_lookup_name("riscv_timer");
92 debug("Cannot find the timer driver, not included?\n");
96 device_bind_with_driver_data(dev, drv, "riscv_timer",
97 plat->timebase_freq, ofnode_null(),
104 static const struct cpu_ops riscv_cpu_ops = {
105 .get_desc = riscv_cpu_get_desc,
106 .get_info = riscv_cpu_get_info,
107 .get_count = riscv_cpu_get_count,
110 static const struct udevice_id riscv_cpu_ids[] = {
111 { .compatible = "riscv" },
115 U_BOOT_DRIVER(riscv_cpu) = {
118 .of_match = riscv_cpu_ids,
119 .bind = riscv_cpu_bind,
120 .ops = &riscv_cpu_ops,
121 .flags = DM_FLAG_PRE_RELOC,