blob: 75184080890f3bbc568cfa26ac4db7b350c57669 [file] [log] [blame]
Rick Chen0d389462019-04-02 15:56:39 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019, Rick Chen <rick@andestech.com>
4 *
Yu Chien Peter Linbc35b492023-07-04 19:13:20 +08005 * 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 Chen0d389462019-04-02 15:56:39 +08009 */
10
11#include <common.h>
12#include <dm.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Rick Chen0d389462019-04-02 15:56:39 +080014#include <dm/device-internal.h>
15#include <dm/lists.h>
16#include <dm/uclass-internal.h>
17#include <regmap.h>
18#include <syscon.h>
19#include <asm/io.h>
20#include <asm/syscon.h>
21#include <cpu.h>
Simon Glass61b29b82020-02-03 07:36:15 -070022#include <linux/err.h>
Rick Chen0d389462019-04-02 15:56:39 +080023
24/* pending register */
Rick Chen43a08322019-11-14 13:52:24 +080025#define PENDING_REG(base, hart) ((ulong)(base) + 0x1000 + ((hart) / 4) * 4)
Rick Chen0d389462019-04-02 15:56:39 +080026/* enable register */
27#define ENABLE_REG(base, hart) ((ulong)(base) + 0x2000 + (hart) * 0x80)
28/* claim register */
29#define CLAIM_REG(base, hart) ((ulong)(base) + 0x200004 + (hart) * 0x1000)
Yu Chien Peter Linbc35b492023-07-04 19:13:20 +080030/* priority register */
31#define PRIORITY_REG(base) ((ulong)(base) + PLICSW_PRIORITY_BASE)
Rick Chen0d389462019-04-02 15:56:39 +080032
Yu Chien Peter Linbdb23832022-10-14 15:00:18 +080033#define ENABLE_HART_IPI (0x01010101)
34#define SEND_IPI_TO_HART(hart) (0x1 << (hart))
Yu Chien Peter Linbc35b492023-07-04 19:13:20 +080035#define PLICSW_PRIORITY_BASE 0x4
36#define PLICSW_INTERRUPT_PER_HART 0x8
Rick Chen0d389462019-04-02 15:56:39 +080037
38DECLARE_GLOBAL_DATA_PTR;
Rick Chen0d389462019-04-02 15:56:39 +080039
Rick Chend58b0a62019-08-21 11:26:50 +080040static int enable_ipi(int hart)
Rick Chen0d389462019-04-02 15:56:39 +080041{
Rick Chen43a08322019-11-14 13:52:24 +080042 unsigned int en;
Rick Chen0d389462019-04-02 15:56:39 +080043
Yu Chien Peter Linbdb23832022-10-14 15:00:18 +080044 en = ENABLE_HART_IPI << hart;
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +080045 writel(en, (void __iomem *)ENABLE_REG(gd->arch.plicsw, hart));
46 writel(en, (void __iomem *)ENABLE_REG(gd->arch.plicsw + 0x4, hart));
Rick Chen0d389462019-04-02 15:56:39 +080047
48 return 0;
49}
50
Yu Chien Peter Linbc35b492023-07-04 19:13:20 +080051static void init_priority_ipi(int hart_num)
52{
53 uint32_t *priority = (void *)PRIORITY_REG(gd->arch.plicsw);
54
55 for (int i = 0; i < hart_num * PLICSW_INTERRUPT_PER_HART; i++) {
56 writel(1, &priority[i]);
57 }
58
59 return;
60}
61
Sean Anderson15943bb2020-09-28 10:52:25 -040062int riscv_init_ipi(void)
Rick Chen0d389462019-04-02 15:56:39 +080063{
Rick Chen0d389462019-04-02 15:56:39 +080064 int ret;
Yu Chien Peter Linbc35b492023-07-04 19:13:20 +080065 int hart_num = 0;
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +080066 long *base = syscon_get_first_range(RISCV_SYSCON_PLICSW);
Sean Anderson15943bb2020-09-28 10:52:25 -040067 ofnode node;
68 struct udevice *dev;
Rick Chend58b0a62019-08-21 11:26:50 +080069 u32 reg;
Rick Chen0d389462019-04-02 15:56:39 +080070
Sean Anderson15943bb2020-09-28 10:52:25 -040071 if (IS_ERR(base))
72 return PTR_ERR(base);
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +080073 gd->arch.plicsw = base;
Sean Anderson15943bb2020-09-28 10:52:25 -040074
Rick Chen0d389462019-04-02 15:56:39 +080075 ret = uclass_find_first_device(UCLASS_CPU, &dev);
76 if (ret)
77 return ret;
Sean Anderson15943bb2020-09-28 10:52:25 -040078 else if (!dev)
79 return -ENODEV;
Rick Chen0d389462019-04-02 15:56:39 +080080
Sean Anderson15943bb2020-09-28 10:52:25 -040081 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
82 const char *device_type;
Rick Chen0d389462019-04-02 15:56:39 +080083
Sean Anderson15943bb2020-09-28 10:52:25 -040084 device_type = ofnode_read_string(node, "device_type");
85 if (!device_type)
86 continue;
Rick Chend58b0a62019-08-21 11:26:50 +080087
Sean Anderson15943bb2020-09-28 10:52:25 -040088 if (strcmp(device_type, "cpu"))
89 continue;
Rick Chend58b0a62019-08-21 11:26:50 +080090
Sean Anderson15943bb2020-09-28 10:52:25 -040091 /* skip if hart is marked as not available */
Simon Glass89090662022-09-06 20:27:17 -060092 if (!ofnode_is_enabled(node))
Sean Anderson15943bb2020-09-28 10:52:25 -040093 continue;
Rick Chend58b0a62019-08-21 11:26:50 +080094
Sean Anderson15943bb2020-09-28 10:52:25 -040095 /* read hart ID of CPU */
96 ret = ofnode_read_u32(node, "reg", &reg);
97 if (ret == 0)
98 enable_ipi(reg);
Yu Chien Peter Linbc35b492023-07-04 19:13:20 +080099 hart_num++;
Rick Chen0d389462019-04-02 15:56:39 +0800100 }
101
Yu Chien Peter Linbc35b492023-07-04 19:13:20 +0800102 init_priority_ipi(hart_num);
Sean Anderson15943bb2020-09-28 10:52:25 -0400103 return 0;
Sean Anderson40686c32020-06-24 06:41:18 -0400104}
105
Rick Chen0d389462019-04-02 15:56:39 +0800106int riscv_send_ipi(int hart)
107{
Sean Anderson40686c32020-06-24 06:41:18 -0400108 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
Rick Chen43a08322019-11-14 13:52:24 +0800109
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800110 writel(ipi, (void __iomem *)PENDING_REG(gd->arch.plicsw,
Rick Chen43a08322019-11-14 13:52:24 +0800111 gd->arch.boot_hart));
Rick Chen0d389462019-04-02 15:56:39 +0800112
113 return 0;
114}
115
116int riscv_clear_ipi(int hart)
117{
118 u32 source_id;
119
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800120 source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
121 writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
Rick Chen0d389462019-04-02 15:56:39 +0800122
123 return 0;
124}
125
Lukas Auer8b3e97b2019-12-08 23:28:50 +0100126int riscv_get_ipi(int hart, int *pending)
127{
Bin Meng62ce0a02021-06-15 13:45:57 +0800128 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
129
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800130 *pending = readl((void __iomem *)PENDING_REG(gd->arch.plicsw,
Lukas Auer8b3e97b2019-12-08 23:28:50 +0100131 gd->arch.boot_hart));
Bin Meng62ce0a02021-06-15 13:45:57 +0800132 *pending = !!(*pending & ipi);
Lukas Auer8b3e97b2019-12-08 23:28:50 +0100133
134 return 0;
135}
136
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800137static const struct udevice_id andes_plicsw_ids[] = {
138 { .compatible = "andestech,plicsw", .data = RISCV_SYSCON_PLICSW },
Rick Chen0d389462019-04-02 15:56:39 +0800139 { }
140};
141
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800142U_BOOT_DRIVER(andes_plicsw) = {
143 .name = "andes_plicsw",
Rick Chen0d389462019-04-02 15:56:39 +0800144 .id = UCLASS_SYSCON,
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800145 .of_match = andes_plicsw_ids,
Rick Chen0d389462019-04-02 15:56:39 +0800146 .flags = DM_FLAG_PRE_RELOC,
147};