blob: 324eb445aaa1f7ab7f919cdcac33e9a6c37f5547 [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 *
5 * U-Boot syscon driver for Andes's Platform Level Interrupt Controller (PLIC).
6 * The PLIC block holds memory-mapped claim and pending registers
7 * associated with software interrupt.
8 */
9
10#include <common.h>
11#include <dm.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Rick Chen0d389462019-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 Glass61b29b82020-02-03 07:36:15 -070021#include <linux/err.h>
Rick Chen0d389462019-04-02 15:56:39 +080022
23/* pending register */
Rick Chen43a08322019-11-14 13:52:24 +080024#define PENDING_REG(base, hart) ((ulong)(base) + 0x1000 + ((hart) / 4) * 4)
Rick Chen0d389462019-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)
29
Yu Chien Peter Linbdb23832022-10-14 15:00:18 +080030#define ENABLE_HART_IPI (0x01010101)
31#define SEND_IPI_TO_HART(hart) (0x1 << (hart))
Rick Chen0d389462019-04-02 15:56:39 +080032
33DECLARE_GLOBAL_DATA_PTR;
Rick Chen0d389462019-04-02 15:56:39 +080034
Rick Chend58b0a62019-08-21 11:26:50 +080035static int enable_ipi(int hart)
Rick Chen0d389462019-04-02 15:56:39 +080036{
Rick Chen43a08322019-11-14 13:52:24 +080037 unsigned int en;
Rick Chen0d389462019-04-02 15:56:39 +080038
Yu Chien Peter Linbdb23832022-10-14 15:00:18 +080039 en = ENABLE_HART_IPI << hart;
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +080040 writel(en, (void __iomem *)ENABLE_REG(gd->arch.plicsw, hart));
41 writel(en, (void __iomem *)ENABLE_REG(gd->arch.plicsw + 0x4, hart));
Rick Chen0d389462019-04-02 15:56:39 +080042
43 return 0;
44}
45
Sean Anderson15943bb2020-09-28 10:52:25 -040046int riscv_init_ipi(void)
Rick Chen0d389462019-04-02 15:56:39 +080047{
Rick Chen0d389462019-04-02 15:56:39 +080048 int ret;
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +080049 long *base = syscon_get_first_range(RISCV_SYSCON_PLICSW);
Sean Anderson15943bb2020-09-28 10:52:25 -040050 ofnode node;
51 struct udevice *dev;
Rick Chend58b0a62019-08-21 11:26:50 +080052 u32 reg;
Rick Chen0d389462019-04-02 15:56:39 +080053
Sean Anderson15943bb2020-09-28 10:52:25 -040054 if (IS_ERR(base))
55 return PTR_ERR(base);
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +080056 gd->arch.plicsw = base;
Sean Anderson15943bb2020-09-28 10:52:25 -040057
Rick Chen0d389462019-04-02 15:56:39 +080058 ret = uclass_find_first_device(UCLASS_CPU, &dev);
59 if (ret)
60 return ret;
Sean Anderson15943bb2020-09-28 10:52:25 -040061 else if (!dev)
62 return -ENODEV;
Rick Chen0d389462019-04-02 15:56:39 +080063
Sean Anderson15943bb2020-09-28 10:52:25 -040064 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
65 const char *device_type;
Rick Chen0d389462019-04-02 15:56:39 +080066
Sean Anderson15943bb2020-09-28 10:52:25 -040067 device_type = ofnode_read_string(node, "device_type");
68 if (!device_type)
69 continue;
Rick Chend58b0a62019-08-21 11:26:50 +080070
Sean Anderson15943bb2020-09-28 10:52:25 -040071 if (strcmp(device_type, "cpu"))
72 continue;
Rick Chend58b0a62019-08-21 11:26:50 +080073
Sean Anderson15943bb2020-09-28 10:52:25 -040074 /* skip if hart is marked as not available */
Simon Glass89090662022-09-06 20:27:17 -060075 if (!ofnode_is_enabled(node))
Sean Anderson15943bb2020-09-28 10:52:25 -040076 continue;
Rick Chend58b0a62019-08-21 11:26:50 +080077
Sean Anderson15943bb2020-09-28 10:52:25 -040078 /* read hart ID of CPU */
79 ret = ofnode_read_u32(node, "reg", &reg);
80 if (ret == 0)
81 enable_ipi(reg);
Rick Chen0d389462019-04-02 15:56:39 +080082 }
83
Sean Anderson15943bb2020-09-28 10:52:25 -040084 return 0;
Sean Anderson40686c32020-06-24 06:41:18 -040085}
86
Rick Chen0d389462019-04-02 15:56:39 +080087int riscv_send_ipi(int hart)
88{
Sean Anderson40686c32020-06-24 06:41:18 -040089 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
Rick Chen43a08322019-11-14 13:52:24 +080090
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +080091 writel(ipi, (void __iomem *)PENDING_REG(gd->arch.plicsw,
Rick Chen43a08322019-11-14 13:52:24 +080092 gd->arch.boot_hart));
Rick Chen0d389462019-04-02 15:56:39 +080093
94 return 0;
95}
96
97int riscv_clear_ipi(int hart)
98{
99 u32 source_id;
100
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800101 source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
102 writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plicsw, hart));
Rick Chen0d389462019-04-02 15:56:39 +0800103
104 return 0;
105}
106
Lukas Auer8b3e97b2019-12-08 23:28:50 +0100107int riscv_get_ipi(int hart, int *pending)
108{
Bin Meng62ce0a02021-06-15 13:45:57 +0800109 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
110
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800111 *pending = readl((void __iomem *)PENDING_REG(gd->arch.plicsw,
Lukas Auer8b3e97b2019-12-08 23:28:50 +0100112 gd->arch.boot_hart));
Bin Meng62ce0a02021-06-15 13:45:57 +0800113 *pending = !!(*pending & ipi);
Lukas Auer8b3e97b2019-12-08 23:28:50 +0100114
115 return 0;
116}
117
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800118static const struct udevice_id andes_plicsw_ids[] = {
119 { .compatible = "andestech,plicsw", .data = RISCV_SYSCON_PLICSW },
Rick Chen0d389462019-04-02 15:56:39 +0800120 { }
121};
122
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800123U_BOOT_DRIVER(andes_plicsw) = {
124 .name = "andes_plicsw",
Rick Chen0d389462019-04-02 15:56:39 +0800125 .id = UCLASS_SYSCON,
Yu Chien Peter Lina5dfa3b2022-10-25 23:03:50 +0800126 .of_match = andes_plicsw_ids,
Rick Chen0d389462019-04-02 15:56:39 +0800127 .flags = DM_FLAG_PRE_RELOC,
128};