blob: 6a63661312a562e2e5575aae5b2abaca7b957f92 [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
Rick Chen6df4ed02019-04-02 15:56:39 +080011#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Rick Chen6df4ed02019-04-02 15:56:39 +080013#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 Glassd66c5f72020-02-03 07:36:15 -070021#include <linux/err.h>
Rick Chen6df4ed02019-04-02 15:56:39 +080022
23/* pending register */
Randolph1a9a7a92023-10-12 13:35:34 +080024#define PENDING_REG(base) ((ulong)(base) + 0x1000)
Rick Chen6df4ed02019-04-02 15:56:39 +080025/* 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 Linc99c3842023-07-04 19:13:20 +080029/* priority register */
30#define PRIORITY_REG(base) ((ulong)(base) + PLICSW_PRIORITY_BASE)
Rick Chen6df4ed02019-04-02 15:56:39 +080031
Randolph1a9a7a92023-10-12 13:35:34 +080032/* 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 Linc99c3842023-07-04 19:13:20 +080035#define PLICSW_PRIORITY_BASE 0x4
Randolph1a9a7a92023-10-12 13:35:34 +080036#define PLICSW_INTERRUPT_PER_HART 0x1
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
Randolph1a9a7a92023-10-12 13:35:34 +080044 en = FIRST_AVAILABLE_BIT << hart;
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080045 writel(en, (void __iomem *)ENABLE_REG(gd->arch.plicsw, hart));
Rick Chen6df4ed02019-04-02 15:56:39 +080046
47 return 0;
48}
49
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080050static 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 Anderson28bfc322020-09-28 10:52:25 -040061int riscv_init_ipi(void)
Rick Chen6df4ed02019-04-02 15:56:39 +080062{
Rick Chen6df4ed02019-04-02 15:56:39 +080063 int ret;
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080064 int hart_num = 0;
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080065 long *base = syscon_get_first_range(RISCV_SYSCON_PLICSW);
Sean Anderson28bfc322020-09-28 10:52:25 -040066 ofnode node;
67 struct udevice *dev;
Rick Cheneaae83b2019-08-21 11:26:50 +080068 u32 reg;
Rick Chen6df4ed02019-04-02 15:56:39 +080069
Sean Anderson28bfc322020-09-28 10:52:25 -040070 if (IS_ERR(base))
71 return PTR_ERR(base);
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080072 gd->arch.plicsw = base;
Sean Anderson28bfc322020-09-28 10:52:25 -040073
Rick Chen6df4ed02019-04-02 15:56:39 +080074 ret = uclass_find_first_device(UCLASS_CPU, &dev);
75 if (ret)
76 return ret;
Randolph1a9a7a92023-10-12 13:35:34 +080077 if (!dev)
Sean Anderson28bfc322020-09-28 10:52:25 -040078 return -ENODEV;
Rick Chen6df4ed02019-04-02 15:56:39 +080079
Sean Anderson28bfc322020-09-28 10:52:25 -040080 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
81 const char *device_type;
Rick Cheneaae83b2019-08-21 11:26:50 +080082
Sean Anderson28bfc322020-09-28 10:52:25 -040083 device_type = ofnode_read_string(node, "device_type");
84 if (!device_type)
85 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080086
Sean Anderson28bfc322020-09-28 10:52:25 -040087 if (strcmp(device_type, "cpu"))
88 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080089
Sean Anderson28bfc322020-09-28 10:52:25 -040090 /* skip if hart is marked as not available */
Simon Glass2e4938b2022-09-06 20:27:17 -060091 if (!ofnode_is_enabled(node))
Sean Anderson28bfc322020-09-28 10:52:25 -040092 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080093
Sean Anderson28bfc322020-09-28 10:52:25 -040094 /* read hart ID of CPU */
95 ret = ofnode_read_u32(node, "reg", &reg);
96 if (ret == 0)
97 enable_ipi(reg);
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080098 hart_num++;
Rick Chen6df4ed02019-04-02 15:56:39 +080099 }
100
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +0800101 init_priority_ipi(hart_num);
Sean Anderson28bfc322020-09-28 10:52:25 -0400102 return 0;
Sean Andersonb1d0cb32020-06-24 06:41:18 -0400103}
104
105int riscv_send_ipi(int hart)
106{
Randolph1a9a7a92023-10-12 13:35:34 +0800107 unsigned int ipi = SEND_IPI_TO_HART(hart);
Sean Andersonb1d0cb32020-06-24 06:41:18 -0400108
Randolph1a9a7a92023-10-12 13:35:34 +0800109 writel(ipi, (void __iomem *)PENDING_REG(gd->arch.plicsw));
Rick Chen6df4ed02019-04-02 15:56:39 +0800110
111 return 0;
112}
113
114int riscv_clear_ipi(int hart)
115{
116 u32 source_id;
117
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800118 source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
119 writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
Rick Chen6df4ed02019-04-02 15:56:39 +0800120
121 return 0;
122}
123
Lukas Auerc7460b82019-12-08 23:28:50 +0100124int riscv_get_ipi(int hart, int *pending)
125{
Randolph1a9a7a92023-10-12 13:35:34 +0800126 unsigned int ipi = SEND_IPI_TO_HART(hart);
Bin Mengb6ec26b2021-06-15 13:45:57 +0800127
Randolph1a9a7a92023-10-12 13:35:34 +0800128 *pending = readl((void __iomem *)PENDING_REG(gd->arch.plicsw));
Bin Mengb6ec26b2021-06-15 13:45:57 +0800129 *pending = !!(*pending & ipi);
Lukas Auerc7460b82019-12-08 23:28:50 +0100130
131 return 0;
132}
133
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800134static const struct udevice_id andes_plicsw_ids[] = {
135 { .compatible = "andestech,plicsw", .data = RISCV_SYSCON_PLICSW },
Rick Chen6df4ed02019-04-02 15:56:39 +0800136 { }
137};
138
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800139U_BOOT_DRIVER(andes_plicsw) = {
140 .name = "andes_plicsw",
Rick Chen6df4ed02019-04-02 15:56:39 +0800141 .id = UCLASS_SYSCON,
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800142 .of_match = andes_plicsw_ids,
Rick Chen6df4ed02019-04-02 15:56:39 +0800143 .flags = DM_FLAG_PRE_RELOC,
144};