Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019, Rick Chen <rick@andestech.com> |
| 4 | * |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 5 | * U-Boot syscon driver for Andes' PLICSW |
| 6 | * The PLICSW block is an Andes-specific design for software interrupts, |
| 7 | * contains memory-mapped priority, enable, claim and pending registers |
| 8 | * similar to RISC-V PLIC. |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/lists.h> |
| 15 | #include <dm/uclass-internal.h> |
| 16 | #include <regmap.h> |
| 17 | #include <syscon.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <asm/syscon.h> |
| 20 | #include <cpu.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 21 | #include <linux/err.h> |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 22 | |
| 23 | /* pending register */ |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 24 | #define PENDING_REG(base, hart) ((ulong)(base) + 0x1000 + 4 * (((hart) + 1) / 32)) |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 25 | /* enable register */ |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 26 | #define ENABLE_REG(base, hart) ((ulong)(base) + 0x2000 + (hart) * 0x80 + 4 * (((hart) + 1) / 32)) |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 27 | /* claim register */ |
| 28 | #define CLAIM_REG(base, hart) ((ulong)(base) + 0x200004 + (hart) * 0x1000) |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 29 | /* priority register */ |
| 30 | #define PRIORITY_REG(base) ((ulong)(base) + PLICSW_PRIORITY_BASE) |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 31 | |
Randolph | 1a9a7a9 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 32 | /* Bit 0 of PLIC-SW pending array is hardwired to zero, so we start from bit 1 */ |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 33 | #define PLICSW_PRIORITY_BASE 0x4 |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 34 | |
| 35 | DECLARE_GLOBAL_DATA_PTR; |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 36 | |
Rick Chen | eaae83b | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 37 | static int enable_ipi(int hart) |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 38 | { |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 39 | u32 enable_bit = (hart + 1) % 32; |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 40 | |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 41 | writel(BIT(enable_bit), (void __iomem *)ENABLE_REG(gd->arch.plicsw, hart)); |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 46 | static void init_priority_ipi(int hart_num) |
| 47 | { |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 48 | u32 *priority = (void *)PRIORITY_REG(gd->arch.plicsw); |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 49 | |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 50 | for (int i = 0; i < hart_num; i++) |
| 51 | writel(1, &priority[i]); |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 52 | |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 53 | return; |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 56 | int riscv_init_ipi(void) |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 57 | { |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 58 | int ret; |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 59 | int hart_num = 0; |
Yu Chien Peter Lin | 739cd6f | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 60 | long *base = syscon_get_first_range(RISCV_SYSCON_PLICSW); |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 61 | ofnode node; |
| 62 | struct udevice *dev; |
Rick Chen | eaae83b | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 63 | u32 reg; |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 64 | |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 65 | if (IS_ERR(base)) |
| 66 | return PTR_ERR(base); |
Yu Chien Peter Lin | 739cd6f | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 67 | gd->arch.plicsw = base; |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 68 | |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 69 | ret = uclass_find_first_device(UCLASS_CPU, &dev); |
| 70 | if (ret) |
| 71 | return ret; |
Randolph | 1a9a7a9 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 72 | if (!dev) |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 73 | return -ENODEV; |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 74 | |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 75 | ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) { |
| 76 | const char *device_type; |
Rick Chen | eaae83b | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 77 | |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 78 | device_type = ofnode_read_string(node, "device_type"); |
| 79 | if (!device_type) |
| 80 | continue; |
Rick Chen | eaae83b | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 81 | |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 82 | if (strcmp(device_type, "cpu")) |
| 83 | continue; |
Rick Chen | eaae83b | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 84 | |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 85 | /* skip if hart is marked as not available */ |
Simon Glass | 2e4938b | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 86 | if (!ofnode_is_enabled(node)) |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 87 | continue; |
Rick Chen | eaae83b | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 88 | |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 89 | /* read hart ID of CPU */ |
| 90 | ret = ofnode_read_u32(node, "reg", ®); |
| 91 | if (ret == 0) |
| 92 | enable_ipi(reg); |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 93 | hart_num++; |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 94 | } |
| 95 | |
Yu Chien Peter Lin | c99c384 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 96 | init_priority_ipi(hart_num); |
Sean Anderson | 28bfc32 | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 97 | return 0; |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | int riscv_send_ipi(int hart) |
| 101 | { |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 102 | u32 interrupt_id = hart + 1; |
| 103 | u32 pending_bit = interrupt_id % 32; |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 104 | |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 105 | writel(BIT(pending_bit), (void __iomem *)PENDING_REG(gd->arch.plicsw, hart)); |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int riscv_clear_ipi(int hart) |
| 111 | { |
| 112 | u32 source_id; |
| 113 | |
Yu Chien Peter Lin | 739cd6f | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 114 | source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plicsw, hart)); |
| 115 | writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plicsw, hart)); |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Lukas Auer | c7460b8 | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 120 | int riscv_get_ipi(int hart, int *pending) |
| 121 | { |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 122 | u32 interrupt_id = hart + 1; |
| 123 | u32 pending_bit = interrupt_id % 32; |
Bin Meng | b6ec26b | 2021-06-15 13:45:57 +0800 | [diff] [blame] | 124 | |
Yu Chien Peter Lin | 922eec0 | 2023-11-16 20:46:12 +0800 | [diff] [blame] | 125 | *pending = readl((void __iomem *)PENDING_REG(gd->arch.plicsw, hart)); |
| 126 | *pending = !!(*pending & BIT(pending_bit)); |
Lukas Auer | c7460b8 | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Yu Chien Peter Lin | 739cd6f | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 131 | static const struct udevice_id andes_plicsw_ids[] = { |
| 132 | { .compatible = "andestech,plicsw", .data = RISCV_SYSCON_PLICSW }, |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 133 | { } |
| 134 | }; |
| 135 | |
Yu Chien Peter Lin | 739cd6f | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 136 | U_BOOT_DRIVER(andes_plicsw) = { |
| 137 | .name = "andes_plicsw", |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 138 | .id = UCLASS_SYSCON, |
Yu Chien Peter Lin | 739cd6f | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 139 | .of_match = andes_plicsw_ids, |
Rick Chen | 6df4ed0 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 140 | .flags = DM_FLAG_PRE_RELOC, |
| 141 | }; |