Bin Meng | 833508c | 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 | 6277186 | 2020-06-24 06:41:22 -0400 | [diff] [blame] | 4 | * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com> |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
Sean Anderson | ab24017 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 7 | #include <clk.h> |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <cpu.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/lists.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Sean Anderson | ab24017 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 16 | #include <linux/err.h> |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 17 | |
Atish Patra | 007056f | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 20 | static 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 | |
| 33 | static int riscv_cpu_get_info(struct udevice *dev, struct cpu_info *info) |
| 34 | { |
Sean Anderson | ab24017 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 35 | int ret; |
| 36 | struct clk clk; |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 37 | const char *mmu; |
Sagar Shrikant Kadam | add0dc1 | 2020-06-28 07:45:03 -0700 | [diff] [blame] | 38 | u32 i_cache_size; |
| 39 | u32 d_cache_size; |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 40 | |
Sean Anderson | ab24017 | 2020-06-24 06:41:21 -0400 | [diff] [blame] | 41 | /* First try getting the frequency from the assigned clock */ |
| 42 | ret = clk_get_by_index(dev, 0, &clk); |
| 43 | if (!ret) { |
| 44 | ret = clk_get_rate(&clk); |
| 45 | if (!IS_ERR_VALUE(ret)) |
| 46 | info->cpu_freq = ret; |
| 47 | clk_free(&clk); |
| 48 | } |
| 49 | |
| 50 | if (!info->cpu_freq) |
| 51 | dev_read_u32(dev, "clock-frequency", (u32 *)&info->cpu_freq); |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 52 | |
| 53 | mmu = dev_read_string(dev, "mmu-type"); |
Sagar Shrikant Kadam | b6b233d | 2020-06-28 07:45:02 -0700 | [diff] [blame] | 54 | if (mmu) |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 55 | info->features |= BIT(CPU_FEAT_MMU); |
| 56 | |
Sagar Shrikant Kadam | add0dc1 | 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 | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int riscv_cpu_get_count(struct udevice *dev) |
| 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 | 4dfea4b | 2019-08-08 00:52:08 -0700 | [diff] [blame] | 78 | /* skip if hart is marked as not available in the device tree */ |
| 79 | if (!ofnode_is_available(node)) |
| 80 | continue; |
| 81 | |
Bin Meng | 833508c | 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 | { |
| 94 | struct cpu_platdata *plat = dev_get_parent_platdata(dev); |
| 95 | struct driver *drv; |
| 96 | int ret; |
| 97 | |
| 98 | /* save the hart id */ |
| 99 | plat->cpu_id = dev_read_addr(dev); |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 100 | /* first examine the property in current cpu node */ |
| 101 | ret = dev_read_u32(dev, "timebase-frequency", &plat->timebase_freq); |
| 102 | /* if not found, then look at the parent /cpus node */ |
| 103 | if (ret) |
| 104 | dev_read_u32(dev->parent, "timebase-frequency", |
| 105 | &plat->timebase_freq); |
| 106 | |
| 107 | /* |
Atish Patra | 007056f | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 108 | * Bind riscv-timer driver on boot hart. |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 109 | * |
| 110 | * We only instantiate one timer device which is enough for U-Boot. |
| 111 | * Pass the "timebase-frequency" value as the driver data for the |
| 112 | * timer device. |
| 113 | * |
| 114 | * Return value is not checked since it's possible that the timer |
| 115 | * driver is not included. |
| 116 | */ |
Atish Patra | 007056f | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 117 | if (plat->cpu_id == gd->arch.boot_hart && plat->timebase_freq) { |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 118 | drv = lists_driver_lookup_name("riscv_timer"); |
| 119 | if (!drv) { |
| 120 | debug("Cannot find the timer driver, not included?\n"); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | device_bind_with_driver_data(dev, drv, "riscv_timer", |
| 125 | plat->timebase_freq, ofnode_null(), |
| 126 | NULL); |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Sean Anderson | 6277186 | 2020-06-24 06:41:22 -0400 | [diff] [blame] | 132 | static int riscv_cpu_probe(struct udevice *dev) |
| 133 | { |
| 134 | int ret = 0; |
| 135 | struct clk clk; |
| 136 | |
| 137 | /* Get a clock if it exists */ |
| 138 | ret = clk_get_by_index(dev, 0, &clk); |
| 139 | if (ret) |
| 140 | return 0; |
| 141 | |
| 142 | ret = clk_enable(&clk); |
| 143 | clk_free(&clk); |
| 144 | if (ret == -ENOSYS || ret == -ENOTSUPP) |
| 145 | return 0; |
| 146 | else |
| 147 | return ret; |
| 148 | } |
| 149 | |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 150 | static const struct cpu_ops riscv_cpu_ops = { |
| 151 | .get_desc = riscv_cpu_get_desc, |
| 152 | .get_info = riscv_cpu_get_info, |
| 153 | .get_count = riscv_cpu_get_count, |
| 154 | }; |
| 155 | |
| 156 | static const struct udevice_id riscv_cpu_ids[] = { |
| 157 | { .compatible = "riscv" }, |
| 158 | { } |
| 159 | }; |
| 160 | |
| 161 | U_BOOT_DRIVER(riscv_cpu) = { |
| 162 | .name = "riscv_cpu", |
| 163 | .id = UCLASS_CPU, |
| 164 | .of_match = riscv_cpu_ids, |
| 165 | .bind = riscv_cpu_bind, |
Sean Anderson | 6277186 | 2020-06-24 06:41:22 -0400 | [diff] [blame] | 166 | .probe = riscv_cpu_probe, |
Bin Meng | 833508c | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 167 | .ops = &riscv_cpu_ops, |
| 168 | .flags = DM_FLAG_PRE_RELOC, |
| 169 | }; |