blob: c09e5c69bc31357fea563c6e0b6d6dca002194ec [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 */
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +080024#define PENDING_REG(base, hart) ((ulong)(base) + 0x1000 + 4 * (((hart) + 1) / 32))
Rick Chen6df4ed02019-04-02 15:56:39 +080025/* enable register */
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +080026#define ENABLE_REG(base, hart) ((ulong)(base) + 0x2000 + (hart) * 0x80 + 4 * (((hart) + 1) / 32))
Rick Chen6df4ed02019-04-02 15:56:39 +080027/* 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 */
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080033#define PLICSW_PRIORITY_BASE 0x4
Rick Chen6df4ed02019-04-02 15:56:39 +080034
35DECLARE_GLOBAL_DATA_PTR;
Rick Chen6df4ed02019-04-02 15:56:39 +080036
Rick Cheneaae83b2019-08-21 11:26:50 +080037static int enable_ipi(int hart)
Rick Chen6df4ed02019-04-02 15:56:39 +080038{
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +080039 u32 enable_bit = (hart + 1) % 32;
Rick Chen6df4ed02019-04-02 15:56:39 +080040
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +080041 writel(BIT(enable_bit), (void __iomem *)ENABLE_REG(gd->arch.plicsw, hart));
Rick Chen6df4ed02019-04-02 15:56:39 +080042
43 return 0;
44}
45
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080046static void init_priority_ipi(int hart_num)
47{
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +080048 u32 *priority = (void *)PRIORITY_REG(gd->arch.plicsw);
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080049
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +080050 for (int i = 0; i < hart_num; i++)
51 writel(1, &priority[i]);
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080052
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +080053 return;
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080054}
55
Sean Anderson28bfc322020-09-28 10:52:25 -040056int riscv_init_ipi(void)
Rick Chen6df4ed02019-04-02 15:56:39 +080057{
Rick Chen6df4ed02019-04-02 15:56:39 +080058 int ret;
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080059 int hart_num = 0;
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080060 long *base = syscon_get_first_range(RISCV_SYSCON_PLICSW);
Sean Anderson28bfc322020-09-28 10:52:25 -040061 ofnode node;
62 struct udevice *dev;
Rick Cheneaae83b2019-08-21 11:26:50 +080063 u32 reg;
Rick Chen6df4ed02019-04-02 15:56:39 +080064
Sean Anderson28bfc322020-09-28 10:52:25 -040065 if (IS_ERR(base))
66 return PTR_ERR(base);
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080067 gd->arch.plicsw = base;
Sean Anderson28bfc322020-09-28 10:52:25 -040068
Rick Chen6df4ed02019-04-02 15:56:39 +080069 ret = uclass_find_first_device(UCLASS_CPU, &dev);
70 if (ret)
71 return ret;
Randolph1a9a7a92023-10-12 13:35:34 +080072 if (!dev)
Sean Anderson28bfc322020-09-28 10:52:25 -040073 return -ENODEV;
Rick Chen6df4ed02019-04-02 15:56:39 +080074
Sean Anderson28bfc322020-09-28 10:52:25 -040075 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
76 const char *device_type;
Rick Cheneaae83b2019-08-21 11:26:50 +080077
Sean Anderson28bfc322020-09-28 10:52:25 -040078 device_type = ofnode_read_string(node, "device_type");
79 if (!device_type)
80 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080081
Sean Anderson28bfc322020-09-28 10:52:25 -040082 if (strcmp(device_type, "cpu"))
83 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080084
Sean Anderson28bfc322020-09-28 10:52:25 -040085 /* skip if hart is marked as not available */
Simon Glass2e4938b2022-09-06 20:27:17 -060086 if (!ofnode_is_enabled(node))
Sean Anderson28bfc322020-09-28 10:52:25 -040087 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080088
Sean Anderson28bfc322020-09-28 10:52:25 -040089 /* read hart ID of CPU */
90 ret = ofnode_read_u32(node, "reg", &reg);
91 if (ret == 0)
92 enable_ipi(reg);
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080093 hart_num++;
Rick Chen6df4ed02019-04-02 15:56:39 +080094 }
95
Yu Chien Peter Linc99c3842023-07-04 19:13:20 +080096 init_priority_ipi(hart_num);
Sean Anderson28bfc322020-09-28 10:52:25 -040097 return 0;
Sean Andersonb1d0cb32020-06-24 06:41:18 -040098}
99
100int riscv_send_ipi(int hart)
101{
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +0800102 u32 interrupt_id = hart + 1;
103 u32 pending_bit = interrupt_id % 32;
Sean Andersonb1d0cb32020-06-24 06:41:18 -0400104
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +0800105 writel(BIT(pending_bit), (void __iomem *)PENDING_REG(gd->arch.plicsw, hart));
Rick Chen6df4ed02019-04-02 15:56:39 +0800106
107 return 0;
108}
109
110int riscv_clear_ipi(int hart)
111{
112 u32 source_id;
113
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800114 source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
115 writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
Rick Chen6df4ed02019-04-02 15:56:39 +0800116
117 return 0;
118}
119
Lukas Auerc7460b82019-12-08 23:28:50 +0100120int riscv_get_ipi(int hart, int *pending)
121{
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +0800122 u32 interrupt_id = hart + 1;
123 u32 pending_bit = interrupt_id % 32;
Bin Mengb6ec26b2021-06-15 13:45:57 +0800124
Yu Chien Peter Lin922eec02023-11-16 20:46:12 +0800125 *pending = readl((void __iomem *)PENDING_REG(gd->arch.plicsw, hart));
126 *pending = !!(*pending & BIT(pending_bit));
Lukas Auerc7460b82019-12-08 23:28:50 +0100127
128 return 0;
129}
130
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800131static const struct udevice_id andes_plicsw_ids[] = {
132 { .compatible = "andestech,plicsw", .data = RISCV_SYSCON_PLICSW },
Rick Chen6df4ed02019-04-02 15:56:39 +0800133 { }
134};
135
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800136U_BOOT_DRIVER(andes_plicsw) = {
137 .name = "andes_plicsw",
Rick Chen6df4ed02019-04-02 15:56:39 +0800138 .id = UCLASS_SYSCON,
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +0800139 .of_match = andes_plicsw_ids,
Rick Chen6df4ed02019-04-02 15:56:39 +0800140 .flags = DM_FLAG_PRE_RELOC,
141};