1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019, Rick Chen <rick@andestech.com>
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.
12 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
18 #include <asm/syscon.h>
20 #include <linux/err.h>
22 /* pending register */
23 #define PENDING_REG(base, hart) ((ulong)(base) + 0x1000 + ((hart) / 4) * 4)
25 #define ENABLE_REG(base, hart) ((ulong)(base) + 0x2000 + (hart) * 0x80)
27 #define CLAIM_REG(base, hart) ((ulong)(base) + 0x200004 + (hart) * 0x1000)
29 #define ENABLE_HART_IPI (0x80808080)
30 #define SEND_IPI_TO_HART(hart) (0x80 >> (hart))
32 DECLARE_GLOBAL_DATA_PTR;
34 static int enable_ipi(int hart)
38 en = ENABLE_HART_IPI >> hart;
39 writel(en, (void __iomem *)ENABLE_REG(gd->arch.plic, hart));
44 static int init_plic(void)
51 ret = uclass_find_first_device(UCLASS_CPU, &dev);
55 if (ret == 0 && dev) {
56 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
57 const char *device_type;
59 device_type = ofnode_read_string(node, "device_type");
63 if (strcmp(device_type, "cpu"))
66 /* skip if hart is marked as not available */
67 if (!ofnode_is_available(node))
70 /* read hart ID of CPU */
71 ret = ofnode_read_u32(node, "reg", ®);
82 int riscv_init_ipi(void)
84 long *ret = syscon_get_first_range(RISCV_SYSCON_PLIC);
93 int riscv_send_ipi(int hart)
95 unsigned int ipi = (SEND_IPI_TO_HART(hart) << (8 * gd->arch.boot_hart));
97 writel(ipi, (void __iomem *)PENDING_REG(gd->arch.plic,
103 int riscv_clear_ipi(int hart)
107 source_id = readl((void __iomem *)CLAIM_REG(gd->arch.plic, hart));
108 writel(source_id, (void __iomem *)CLAIM_REG(gd->arch.plic, hart));
113 int riscv_get_ipi(int hart, int *pending)
115 *pending = readl((void __iomem *)PENDING_REG(gd->arch.plic,
116 gd->arch.boot_hart));
117 *pending = !!(*pending & SEND_IPI_TO_HART(hart));
122 static const struct udevice_id andes_plic_ids[] = {
123 { .compatible = "riscv,plic1", .data = RISCV_SYSCON_PLIC },
127 U_BOOT_DRIVER(andes_plic) = {
128 .name = "andes_plic",
130 .of_match = andes_plic_ids,
131 .flags = DM_FLAG_PRE_RELOC,