Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> |
Sean Anderson | d2a10ff | 2020-06-24 06:41:22 -0400 | [diff] [blame] | 4 | * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com> |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
Sean Anderson | 4724409 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 7 | #include <clk.h> |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 8 | #include <cpu.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Heinrich Schuchardt | 6403e3d | 2024-01-29 12:46:05 +0100 | [diff] [blame] | 13 | #include <asm/sbi.h> |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/lists.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Sean Anderson | 4724409 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 17 | #include <linux/err.h> |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 18 | |
Atish Patra | 04098f9 | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 21 | static int riscv_cpu_get_desc(const struct udevice *dev, char *buf, int size) |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 22 | { |
Conor Dooley | 4198155 | 2024-03-18 15:16:02 +0000 | [diff] [blame] | 23 | const char *cpu; |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 24 | |
Conor Dooley | 4198155 | 2024-03-18 15:16:02 +0000 | [diff] [blame] | 25 | cpu = dev_read_string(dev, "compatible"); |
Hanyuan Zhao | bc735c3 | 2024-05-06 17:10:06 +0800 | [diff] [blame] | 26 | if (!cpu || size < (strlen(cpu) + 1)) |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 27 | return -ENOSPC; |
| 28 | |
Conor Dooley | 4198155 | 2024-03-18 15:16:02 +0000 | [diff] [blame] | 29 | strcpy(buf, cpu); |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 34 | static int riscv_cpu_get_info(const struct udevice *dev, struct cpu_info *info) |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 35 | { |
Sean Anderson | 4724409 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 36 | int ret; |
| 37 | struct clk clk; |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 38 | const char *mmu; |
Sagar Shrikant Kadam | 4ae7a95 | 2020-06-28 07:45:03 -0700 | [diff] [blame] | 39 | u32 i_cache_size; |
| 40 | u32 d_cache_size; |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 41 | |
Sean Anderson | 4724409 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 42 | /* First try getting the frequency from the assigned clock */ |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 43 | ret = clk_get_by_index((struct udevice *)dev, 0, &clk); |
Sean Anderson | 4724409 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 44 | if (!ret) { |
| 45 | ret = clk_get_rate(&clk); |
| 46 | if (!IS_ERR_VALUE(ret)) |
| 47 | info->cpu_freq = ret; |
Sean Anderson | 4724409 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | if (!info->cpu_freq) |
| 51 | dev_read_u32(dev, "clock-frequency", (u32 *)&info->cpu_freq); |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 52 | |
| 53 | mmu = dev_read_string(dev, "mmu-type"); |
Sagar Shrikant Kadam | afc0c19 | 2020-06-28 07:45:02 -0700 | [diff] [blame] | 54 | if (mmu) |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 55 | info->features |= BIT(CPU_FEAT_MMU); |
| 56 | |
Sagar Shrikant Kadam | 4ae7a95 | 2020-06-28 07:45:03 -0700 | [diff] [blame] | 57 | /* check if I cache is present */ |
| 58 | ret = dev_read_u32(dev, "i-cache-size", &i_cache_size); |
| 59 | if (ret) |
| 60 | /* if not found check if d-cache is present */ |
| 61 | ret = dev_read_u32(dev, "d-cache-size", &d_cache_size); |
| 62 | |
| 63 | /* if either I or D cache is present set L1 cache feature */ |
| 64 | if (!ret) |
| 65 | info->features |= BIT(CPU_FEAT_L1_CACHE); |
| 66 | |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 70 | static int riscv_cpu_get_count(const struct udevice *dev) |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 71 | { |
| 72 | ofnode node; |
| 73 | int num = 0; |
| 74 | |
| 75 | ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) { |
| 76 | const char *device_type; |
| 77 | |
Bin Meng | 33d46a2 | 2019-08-08 00:52:08 -0700 | [diff] [blame] | 78 | /* skip if hart is marked as not available in the device tree */ |
Simon Glass | 2e4938b | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 79 | if (!ofnode_is_enabled(node)) |
Bin Meng | 33d46a2 | 2019-08-08 00:52:08 -0700 | [diff] [blame] | 80 | continue; |
| 81 | |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 82 | device_type = ofnode_read_string(node, "device_type"); |
| 83 | if (!device_type) |
| 84 | continue; |
| 85 | if (strcmp(device_type, "cpu") == 0) |
| 86 | num++; |
| 87 | } |
| 88 | |
| 89 | return num; |
| 90 | } |
| 91 | |
| 92 | static int riscv_cpu_bind(struct udevice *dev) |
| 93 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 94 | struct cpu_plat *plat = dev_get_parent_plat(dev); |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 95 | struct driver *drv; |
| 96 | int ret; |
Heinrich Schuchardt | 6403e3d | 2024-01-29 12:46:05 +0100 | [diff] [blame] | 97 | long mvendorid; |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 98 | |
| 99 | /* save the hart id */ |
| 100 | plat->cpu_id = dev_read_addr(dev); |
Heinrich Schuchardt | 6403e3d | 2024-01-29 12:46:05 +0100 | [diff] [blame] | 101 | /* provide data for SMBIOS */ |
Heinrich Schuchardt | c0c63fa | 2023-12-28 08:30:24 +0100 | [diff] [blame] | 102 | if (IS_ENABLED(CONFIG_64BIT)) |
| 103 | plat->family = 0x201; |
| 104 | else |
| 105 | plat->family = 0x200; |
Heinrich Schuchardt | 6403e3d | 2024-01-29 12:46:05 +0100 | [diff] [blame] | 106 | if (CONFIG_IS_ENABLED(RISCV_SMODE)) { |
| 107 | /* |
| 108 | * For RISC-V CPUs the SMBIOS Processor ID field contains |
| 109 | * the Machine Vendor ID from CSR mvendorid. |
| 110 | */ |
| 111 | ret = sbi_get_mvendorid(&mvendorid); |
| 112 | if (!ret) |
| 113 | plat->id[0] = mvendorid; |
| 114 | } |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 115 | /* first examine the property in current cpu node */ |
| 116 | ret = dev_read_u32(dev, "timebase-frequency", &plat->timebase_freq); |
| 117 | /* if not found, then look at the parent /cpus node */ |
| 118 | if (ret) |
| 119 | dev_read_u32(dev->parent, "timebase-frequency", |
| 120 | &plat->timebase_freq); |
| 121 | |
| 122 | /* |
Atish Patra | 04098f9 | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 123 | * Bind riscv-timer driver on boot hart. |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 124 | * |
| 125 | * We only instantiate one timer device which is enough for U-Boot. |
| 126 | * Pass the "timebase-frequency" value as the driver data for the |
| 127 | * timer device. |
| 128 | * |
| 129 | * Return value is not checked since it's possible that the timer |
| 130 | * driver is not included. |
| 131 | */ |
Atish Patra | 04098f9 | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 132 | if (plat->cpu_id == gd->arch.boot_hart && plat->timebase_freq) { |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 133 | drv = lists_driver_lookup_name("riscv_timer"); |
| 134 | if (!drv) { |
| 135 | debug("Cannot find the timer driver, not included?\n"); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | device_bind_with_driver_data(dev, drv, "riscv_timer", |
| 140 | plat->timebase_freq, ofnode_null(), |
| 141 | NULL); |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Sean Anderson | d2a10ff | 2020-06-24 06:41:22 -0400 | [diff] [blame] | 147 | static int riscv_cpu_probe(struct udevice *dev) |
| 148 | { |
| 149 | int ret = 0; |
| 150 | struct clk clk; |
| 151 | |
| 152 | /* Get a clock if it exists */ |
| 153 | ret = clk_get_by_index(dev, 0, &clk); |
| 154 | if (ret) |
| 155 | return 0; |
| 156 | |
| 157 | ret = clk_enable(&clk); |
Sean Anderson | d2a10ff | 2020-06-24 06:41:22 -0400 | [diff] [blame] | 158 | if (ret == -ENOSYS || ret == -ENOTSUPP) |
| 159 | return 0; |
| 160 | else |
| 161 | return ret; |
| 162 | } |
| 163 | |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 164 | static const struct cpu_ops riscv_cpu_ops = { |
| 165 | .get_desc = riscv_cpu_get_desc, |
| 166 | .get_info = riscv_cpu_get_info, |
| 167 | .get_count = riscv_cpu_get_count, |
| 168 | }; |
| 169 | |
| 170 | static const struct udevice_id riscv_cpu_ids[] = { |
| 171 | { .compatible = "riscv" }, |
| 172 | { } |
| 173 | }; |
| 174 | |
| 175 | U_BOOT_DRIVER(riscv_cpu) = { |
| 176 | .name = "riscv_cpu", |
| 177 | .id = UCLASS_CPU, |
| 178 | .of_match = riscv_cpu_ids, |
| 179 | .bind = riscv_cpu_bind, |
Sean Anderson | d2a10ff | 2020-06-24 06:41:22 -0400 | [diff] [blame] | 180 | .probe = riscv_cpu_probe, |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 181 | .ops = &riscv_cpu_ops, |
| 182 | .flags = DM_FLAG_PRE_RELOC, |
| 183 | }; |