blob: 5e113ee8c94ced7929f2877d03bac24ebd1cd11d [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 *
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 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 */
Rick Cheneb613032019-11-14 13:52:24 +080024#define PENDING_REG(base, hart) ((ulong)(base) + 0x1000 + ((hart) / 4) * 4)
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)
29
30#define ENABLE_HART_IPI (0x80808080)
31#define SEND_IPI_TO_HART(hart) (0x80 >> (hart))
32
33DECLARE_GLOBAL_DATA_PTR;
Rick Chen6df4ed02019-04-02 15:56:39 +080034
Rick Cheneaae83b2019-08-21 11:26:50 +080035static int enable_ipi(int hart)
Rick Chen6df4ed02019-04-02 15:56:39 +080036{
Rick Cheneb613032019-11-14 13:52:24 +080037 unsigned int en;
Rick Chen6df4ed02019-04-02 15:56:39 +080038
Rick Cheneaae83b2019-08-21 11:26:50 +080039 en = ENABLE_HART_IPI >> hart;
40 writel(en, (void __iomem *)ENABLE_REG(gd->arch.plic, hart));
Rick Chen6df4ed02019-04-02 15:56:39 +080041
42 return 0;
43}
44
Sean Anderson28bfc322020-09-28 10:52:25 -040045int riscv_init_ipi(void)
Rick Chen6df4ed02019-04-02 15:56:39 +080046{
Rick Chen6df4ed02019-04-02 15:56:39 +080047 int ret;
Sean Anderson28bfc322020-09-28 10:52:25 -040048 long *base = syscon_get_first_range(RISCV_SYSCON_PLIC);
49 ofnode node;
50 struct udevice *dev;
Rick Cheneaae83b2019-08-21 11:26:50 +080051 u32 reg;
Rick Chen6df4ed02019-04-02 15:56:39 +080052
Sean Anderson28bfc322020-09-28 10:52:25 -040053 if (IS_ERR(base))
54 return PTR_ERR(base);
55 gd->arch.plic = base;
56
Rick Chen6df4ed02019-04-02 15:56:39 +080057 ret = uclass_find_first_device(UCLASS_CPU, &dev);
58 if (ret)
59 return ret;
Sean Anderson28bfc322020-09-28 10:52:25 -040060 else if (!dev)
61 return -ENODEV;
Rick Chen6df4ed02019-04-02 15:56:39 +080062
Sean Anderson28bfc322020-09-28 10:52:25 -040063 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
64 const char *device_type;
Rick Cheneaae83b2019-08-21 11:26:50 +080065
Sean Anderson28bfc322020-09-28 10:52:25 -040066 device_type = ofnode_read_string(node, "device_type");
67 if (!device_type)
68 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080069
Sean Anderson28bfc322020-09-28 10:52:25 -040070 if (strcmp(device_type, "cpu"))
71 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080072
Sean Anderson28bfc322020-09-28 10:52:25 -040073 /* skip if hart is marked as not available */
74 if (!ofnode_is_available(node))
75 continue;
Rick Cheneaae83b2019-08-21 11:26:50 +080076
Sean Anderson28bfc322020-09-28 10:52:25 -040077 /* read hart ID of CPU */
78 ret = ofnode_read_u32(node, "reg", &reg);
79 if (ret == 0)
80 enable_ipi(reg);
Rick Chen6df4ed02019-04-02 15:56:39 +080081 }
82
Sean Anderson28bfc322020-09-28 10:52:25 -040083 return 0;
Sean Andersonb1d0cb32020-06-24 06:41:18 -040084}
85
86int riscv_send_ipi(int hart)
87{
88 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
89
Rick Cheneb613032019-11-14 13:52:24 +080090 writel(ipi, (void __iomem *)PENDING_REG(gd->arch.plic,
91 gd->arch.boot_hart));
Rick Chen6df4ed02019-04-02 15:56:39 +080092
93 return 0;
94}
95
96int riscv_clear_ipi(int hart)
97{
98 u32 source_id;
99
Rick Chen6df4ed02019-04-02 15:56:39 +0800100 source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plic, hart));
101 writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plic, hart));
102
103 return 0;
104}
105
Lukas Auerc7460b82019-12-08 23:28:50 +0100106int riscv_get_ipi(int hart, int *pending)
107{
Bin Mengb6ec26b2021-06-15 13:45:57 +0800108 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
109
Lukas Auerc7460b82019-12-08 23:28:50 +0100110 *pending = readl((void __iomem *)PENDING_REG(gd->arch.plic,
111 gd->arch.boot_hart));
Bin Mengb6ec26b2021-06-15 13:45:57 +0800112 *pending = !!(*pending & ipi);
Lukas Auerc7460b82019-12-08 23:28:50 +0100113
114 return 0;
115}
116
Rick Chen6df4ed02019-04-02 15:56:39 +0800117static const struct udevice_id andes_plic_ids[] = {
118 { .compatible = "riscv,plic1", .data = RISCV_SYSCON_PLIC },
119 { }
120};
121
122U_BOOT_DRIVER(andes_plic) = {
123 .name = "andes_plic",
124 .id = UCLASS_SYSCON,
125 .of_match = andes_plic_ids,
126 .flags = DM_FLAG_PRE_RELOC,
127};