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> |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <cpu.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 11 | #include <dm/device-internal.h> |
| 12 | #include <dm/lists.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 14 | |
Atish Patra | 04098f9 | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 17 | static 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 | |
| 30 | static 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 | |
| 43 | static 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 Meng | 33d46a2 | 2019-08-08 00:52:08 -0700 | [diff] [blame] | 51 | /* skip if hart is marked as not available in the device tree */ |
| 52 | if (!ofnode_is_available(node)) |
| 53 | continue; |
| 54 | |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 55 | 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 | |
| 65 | static 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 Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 73 | /* 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 Patra | 04098f9 | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 81 | * Bind riscv-timer driver on boot hart. |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 82 | * |
| 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 Patra | 04098f9 | 2019-02-25 08:15:14 +0000 | [diff] [blame] | 90 | if (plat->cpu_id == gd->arch.boot_hart && plat->timebase_freq) { |
Bin Meng | c4fcb62 | 2018-12-12 06:12:26 -0800 | [diff] [blame] | 91 | 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 | |
| 105 | static 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 | |
| 111 | static const struct udevice_id riscv_cpu_ids[] = { |
| 112 | { .compatible = "riscv" }, |
| 113 | { } |
| 114 | }; |
| 115 | |
| 116 | U_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 | }; |