Rick Chen | 0d38946 | 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 | bc35b49 | 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 | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Rick Chen | 0d38946 | 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 | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 21 | #include <linux/err.h> |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 22 | |
| 23 | /* pending register */ |
Randolph | 04b2123 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 24 | #define PENDING_REG(base) ((ulong)(base) + 0x1000) |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 25 | /* enable register */ |
| 26 | #define ENABLE_REG(base, hart) ((ulong)(base) + 0x2000 + (hart) * 0x80) |
| 27 | /* claim register */ |
| 28 | #define CLAIM_REG(base, hart) ((ulong)(base) + 0x200004 + (hart) * 0x1000) |
Yu Chien Peter Lin | bc35b49 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 29 | /* priority register */ |
| 30 | #define PRIORITY_REG(base) ((ulong)(base) + PLICSW_PRIORITY_BASE) |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 31 | |
Randolph | 04b2123 | 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 */ |
| 33 | #define FIRST_AVAILABLE_BIT 0x2 |
| 34 | #define SEND_IPI_TO_HART(hart) (FIRST_AVAILABLE_BIT << (hart)) |
Yu Chien Peter Lin | bc35b49 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 35 | #define PLICSW_PRIORITY_BASE 0x4 |
Randolph | 04b2123 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 36 | #define PLICSW_INTERRUPT_PER_HART 0x1 |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 37 | |
| 38 | DECLARE_GLOBAL_DATA_PTR; |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 39 | |
Rick Chen | d58b0a6 | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 40 | static int enable_ipi(int hart) |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 41 | { |
Rick Chen | 43a0832 | 2019-11-14 13:52:24 +0800 | [diff] [blame] | 42 | unsigned int en; |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 43 | |
Randolph | 04b2123 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 44 | en = FIRST_AVAILABLE_BIT << hart; |
Yu Chien Peter Lin | a5dfa3b | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 45 | writel(en, (void __iomem *)ENABLE_REG(gd->arch.plicsw, hart)); |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Yu Chien Peter Lin | bc35b49 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 50 | static void init_priority_ipi(int hart_num) |
| 51 | { |
| 52 | uint32_t *priority = (void *)PRIORITY_REG(gd->arch.plicsw); |
| 53 | |
| 54 | for (int i = 0; i < hart_num * PLICSW_INTERRUPT_PER_HART; i++) { |
| 55 | writel(1, &priority[i]); |
| 56 | } |
| 57 | |
| 58 | return; |
| 59 | } |
| 60 | |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 61 | int riscv_init_ipi(void) |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 62 | { |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 63 | int ret; |
Yu Chien Peter Lin | bc35b49 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 64 | int hart_num = 0; |
Yu Chien Peter Lin | a5dfa3b | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 65 | long *base = syscon_get_first_range(RISCV_SYSCON_PLICSW); |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 66 | ofnode node; |
| 67 | struct udevice *dev; |
Rick Chen | d58b0a6 | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 68 | u32 reg; |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 69 | |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 70 | if (IS_ERR(base)) |
| 71 | return PTR_ERR(base); |
Yu Chien Peter Lin | a5dfa3b | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 72 | gd->arch.plicsw = base; |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 73 | |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 74 | ret = uclass_find_first_device(UCLASS_CPU, &dev); |
| 75 | if (ret) |
| 76 | return ret; |
Randolph | 04b2123 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 77 | if (!dev) |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 78 | return -ENODEV; |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 79 | |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 80 | ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) { |
| 81 | const char *device_type; |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 82 | |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 83 | device_type = ofnode_read_string(node, "device_type"); |
| 84 | if (!device_type) |
| 85 | continue; |
Rick Chen | d58b0a6 | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 86 | |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 87 | if (strcmp(device_type, "cpu")) |
| 88 | continue; |
Rick Chen | d58b0a6 | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 89 | |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 90 | /* skip if hart is marked as not available */ |
Simon Glass | 8909066 | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 91 | if (!ofnode_is_enabled(node)) |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 92 | continue; |
Rick Chen | d58b0a6 | 2019-08-21 11:26:50 +0800 | [diff] [blame] | 93 | |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 94 | /* read hart ID of CPU */ |
| 95 | ret = ofnode_read_u32(node, "reg", ®); |
| 96 | if (ret == 0) |
| 97 | enable_ipi(reg); |
Yu Chien Peter Lin | bc35b49 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 98 | hart_num++; |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 99 | } |
| 100 | |
Yu Chien Peter Lin | bc35b49 | 2023-07-04 19:13:20 +0800 | [diff] [blame] | 101 | init_priority_ipi(hart_num); |
Sean Anderson | 15943bb | 2020-09-28 10:52:25 -0400 | [diff] [blame] | 102 | return 0; |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 105 | int riscv_send_ipi(int hart) |
| 106 | { |
Randolph | 04b2123 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 107 | unsigned int ipi = SEND_IPI_TO_HART(hart); |
Rick Chen | 43a0832 | 2019-11-14 13:52:24 +0800 | [diff] [blame] | 108 | |
Randolph | 04b2123 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 109 | writel(ipi, (void __iomem *)PENDING_REG(gd->arch.plicsw)); |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | int riscv_clear_ipi(int hart) |
| 115 | { |
| 116 | u32 source_id; |
| 117 | |
Yu Chien Peter Lin | a5dfa3b | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 118 | source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plicsw, hart)); |
| 119 | writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plicsw, hart)); |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Lukas Auer | 8b3e97b | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 124 | int riscv_get_ipi(int hart, int *pending) |
| 125 | { |
Randolph | 04b2123 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 126 | unsigned int ipi = SEND_IPI_TO_HART(hart); |
Bin Meng | 62ce0a0 | 2021-06-15 13:45:57 +0800 | [diff] [blame] | 127 | |
Randolph | 04b2123 | 2023-10-12 13:35:34 +0800 | [diff] [blame] | 128 | *pending = readl((void __iomem *)PENDING_REG(gd->arch.plicsw)); |
Bin Meng | 62ce0a0 | 2021-06-15 13:45:57 +0800 | [diff] [blame] | 129 | *pending = !!(*pending & ipi); |
Lukas Auer | 8b3e97b | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Yu Chien Peter Lin | a5dfa3b | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 134 | static const struct udevice_id andes_plicsw_ids[] = { |
| 135 | { .compatible = "andestech,plicsw", .data = RISCV_SYSCON_PLICSW }, |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 136 | { } |
| 137 | }; |
| 138 | |
Yu Chien Peter Lin | a5dfa3b | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 139 | U_BOOT_DRIVER(andes_plicsw) = { |
| 140 | .name = "andes_plicsw", |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 141 | .id = UCLASS_SYSCON, |
Yu Chien Peter Lin | a5dfa3b | 2022-10-25 23:03:50 +0800 | [diff] [blame] | 142 | .of_match = andes_plicsw_ids, |
Rick Chen | 0d38946 | 2019-04-02 15:56:39 +0800 | [diff] [blame] | 143 | .flags = DM_FLAG_PRE_RELOC, |
| 144 | }; |