blob: 6fd49e873b10b0a8a1af61b306fbeeced1e3fef6 [file] [log] [blame]
Rick Chen6df4ed02019-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 Linc99c3842023-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 Chen6df4ed02019-04-02 15:56:39 +08009 */
10
11#include <common.h>
12#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Rick Chen6df4ed02019-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 Glassd66c5f72020-02-03 07:36:15 -070022#include <linux/err.h>
Rick Chen6df4ed02019-04-02 15:56:39 +080023
24/* pending register */
Randolph1a9a7a92023-10-12 13:35:34 +080025#define PENDING_REG(base) ((ulong)(base) + 0x1000)
Rick Chen6df4ed02019-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 Linc99c3842023-07-04 19:13:20 +080030/* priority register */
31#define PRIORITY_REG(base) ((ulong)(base) + PLICSW_PRIORITY_BASE)
Rick Chen6df4ed02019-04-02 15:56:39 +080032
Randolph1a9a7a92023-10-12 13:35:34 +080033/* Bit 0 of PLIC-SW pending array is hardwired to zero, so we start from bit 1 */
34#define FIRST_AVAILABLE_BIT 0x2
35#define SEND_IPI_TO_HART(hart) (FIRST_AVAILABLE_BIT << (hart))
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080036#define PLICSW_PRIORITY_BASE 0x4
Randolph1a9a7a92023-10-12 13:35:34 +080037#define PLICSW_INTERRUPT_PER_HART 0x1
Rick Chen6df4ed02019-04-02 15:56:39 +080038
39DECLARE_GLOBAL_DATA_PTR;
Rick Chen6df4ed02019-04-02 15:56:39 +080040
Rick Cheneaae83b2019-08-21 11:26:50 +080041static int enable_ipi(int hart)
Rick Chen6df4ed02019-04-02 15:56:39 +080042{
Rick Cheneb613032019-11-14 13:52:24 +080043 unsigned int en;
Rick Chen6df4ed02019-04-02 15:56:39 +080044
Randolph1a9a7a92023-10-12 13:35:34 +080045 en = FIRST_AVAILABLE_BIT << hart;
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080046 writel(en, (void __iomem *)ENABLE_REG(gd->arch.plicsw, hart));
Rick Chen6df4ed02019-04-02 15:56:39 +080047
48 return 0;
49}
50
Yu Chien Peter Linc99c3842023-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 Anderson28bfc322020-09-28 10:52:25 -040062int riscv_init_ipi(void)
Rick Chen6df4ed02019-04-02 15:56:39 +080063{
Rick Chen6df4ed02019-04-02 15:56:39 +080064 int ret;
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080065 int hart_num = 0;
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080066 long *base = syscon_get_first_range(RISCV_SYSCON_PLICSW);
Sean Anderson28bfc322020-09-28 10:52:25 -040067 ofnode node;
68 struct udevice *dev;
Rick Cheneaae83b2019-08-21 11:26:50 +080069 u32 reg;
Rick Chen6df4ed02019-04-02 15:56:39 +080070
Sean Anderson28bfc322020-09-28 10:52:25 -040071 if (IS_ERR(base))
72 return PTR_ERR(base);
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080073 gd->arch.plicsw = base;
Sean Anderson28bfc322020-09-28 10:52:25 -040074
Rick Chen6df4ed02019-04-02 15:56:39 +080075 ret = uclass_find_first_device(UCLASS_CPU, &dev);
76 if (ret)
77 return ret;
Randolph1a9a7a92023-10-12 13:35:34 +080078 if (!dev)
Sean Anderson28bfc322020-09-28 10:52:25 -040079 return -ENODEV;
Rick Chen6df4ed02019-04-02 15:56:39 +080080
Sean Anderson28bfc322020-09-28 10:52:25 -040081 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
82 const char *device_type;
Rick Cheneaae83b2019-08-21 11:26:50 +080083
Sean Anderson28bfc322020-09-28 10:52:25 -040084 device_type = ofnode_read_string(node, "device_type");
85 if (!device_type)
86 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080087
Sean Anderson28bfc322020-09-28 10:52:25 -040088 if (strcmp(device_type, "cpu"))
89 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080090
Sean Anderson28bfc322020-09-28 10:52:25 -040091 /* skip if hart is marked as not available */
Simon Glass2e4938b2022-09-06 20:27:17 -060092 if (!ofnode_is_enabled(node))
Sean Anderson28bfc322020-09-28 10:52:25 -040093 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080094
Sean Anderson28bfc322020-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 Linc99c3842023-07-04 19:13:20 +080099 hart_num++;
Rick Chen6df4ed02019-04-02 15:56:39 +0800100 }
101
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +0800102 init_priority_ipi(hart_num);
Sean Anderson28bfc322020-09-28 10:52:25 -0400103 return 0;
Sean Andersonb1d0cb32020-06-24 06:41:18 -0400104}
105
106int riscv_send_ipi(int hart)
107{
Randolph1a9a7a92023-10-12 13:35:34 +0800108 unsigned int ipi = SEND_IPI_TO_HART(hart);
Sean Andersonb1d0cb32020-06-24 06:41:18 -0400109
Randolph1a9a7a92023-10-12 13:35:34 +0800110 writel(ipi, (void __iomem *)PENDING_REG(gd->arch.plicsw));
Rick Chen6df4ed02019-04-02 15:56:39 +0800111
112 return 0;
113}
114
115int riscv_clear_ipi(int hart)
116{
117 u32 source_id;
118
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800119 source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
120 writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
Rick Chen6df4ed02019-04-02 15:56:39 +0800121
122 return 0;
123}
124
Lukas Auerc7460b82019-12-08 23:28:50 +0100125int riscv_get_ipi(int hart, int *pending)
126{
Randolph1a9a7a92023-10-12 13:35:34 +0800127 unsigned int ipi = SEND_IPI_TO_HART(hart);
Bin Mengb6ec26b2021-06-15 13:45:57 +0800128
Randolph1a9a7a92023-10-12 13:35:34 +0800129 *pending = readl((void __iomem *)PENDING_REG(gd->arch.plicsw));
Bin Mengb6ec26b2021-06-15 13:45:57 +0800130 *pending = !!(*pending & ipi);
Lukas Auerc7460b82019-12-08 23:28:50 +0100131
132 return 0;
133}
134
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800135static const struct udevice_id andes_plicsw_ids[] = {
136 { .compatible = "andestech,plicsw", .data = RISCV_SYSCON_PLICSW },
Rick Chen6df4ed02019-04-02 15:56:39 +0800137 { }
138};
139
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800140U_BOOT_DRIVER(andes_plicsw) = {
141 .name = "andes_plicsw",
Rick Chen6df4ed02019-04-02 15:56:39 +0800142 .id = UCLASS_SYSCON,
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800143 .of_match = andes_plicsw_ids,
Rick Chen6df4ed02019-04-02 15:56:39 +0800144 .flags = DM_FLAG_PRE_RELOC,
145};