blob: 75184080890f3bbc568cfa26ac4db7b350c57669 [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 */
Rick Cheneb613032019-11-14 13:52:24 +080025#define PENDING_REG(base, hart) ((ulong)(base) + 0x1000 + ((hart) / 4) * 4)
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
Yu Chien Peter Linbcb208b2022-10-14 15:00:18 +080033#define ENABLE_HART_IPI (0x01010101)
34#define SEND_IPI_TO_HART(hart) (0x1 << (hart))
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080035#define PLICSW_PRIORITY_BASE 0x4
36#define PLICSW_INTERRUPT_PER_HART 0x8
Rick Chen6df4ed02019-04-02 15:56:39 +080037
38DECLARE_GLOBAL_DATA_PTR;
Rick Chen6df4ed02019-04-02 15:56:39 +080039
Rick Cheneaae83b2019-08-21 11:26:50 +080040static int enable_ipi(int hart)
Rick Chen6df4ed02019-04-02 15:56:39 +080041{
Rick Cheneb613032019-11-14 13:52:24 +080042 unsigned int en;
Rick Chen6df4ed02019-04-02 15:56:39 +080043
Yu Chien Peter Linbcb208b2022-10-14 15:00:18 +080044 en = ENABLE_HART_IPI << hart;
Yu Chien Peter Lin739cd6f2022-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 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;
Sean Anderson28bfc322020-09-28 10:52:25 -040078 else if (!dev)
79 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{
108 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
109
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800110 writel(ipi, (void __iomem *)PENDING_REG(gd->arch.plicsw,
Rick Cheneb613032019-11-14 13:52:24 +0800111 gd->arch.boot_hart));
Rick Chen6df4ed02019-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 Lin739cd6f2022-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 Chen6df4ed02019-04-02 15:56:39 +0800122
123 return 0;
124}
125
Lukas Auerc7460b82019-12-08 23:28:50 +0100126int riscv_get_ipi(int hart, int *pending)
127{
Bin Mengb6ec26b2021-06-15 13:45:57 +0800128 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
129
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800130 *pending = readl((void __iomem *)PENDING_REG(gd->arch.plicsw,
Lukas Auerc7460b82019-12-08 23:28:50 +0100131 gd->arch.boot_hart));
Bin Mengb6ec26b2021-06-15 13:45:57 +0800132 *pending = !!(*pending & ipi);
Lukas Auerc7460b82019-12-08 23:28:50 +0100133
134 return 0;
135}
136
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800137static const struct udevice_id andes_plicsw_ids[] = {
138 { .compatible = "andestech,plicsw", .data = RISCV_SYSCON_PLICSW },
Rick Chen6df4ed02019-04-02 15:56:39 +0800139 { }
140};
141
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800142U_BOOT_DRIVER(andes_plicsw) = {
143 .name = "andes_plicsw",
Rick Chen6df4ed02019-04-02 15:56:39 +0800144 .id = UCLASS_SYSCON,
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800145 .of_match = andes_plicsw_ids,
Rick Chen6df4ed02019-04-02 15:56:39 +0800146 .flags = DM_FLAG_PRE_RELOC,
147};