1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Western Digital Corporation or its affiliates.
4 * Copyright (C) 2022 Ventana Micro Systems Inc.
7 #include <linux/bitfield.h>
8 #include <linux/irqchip/riscv-aplic.h>
9 #include <linux/module.h>
11 #include <linux/of_irq.h>
12 #include <linux/platform_device.h>
13 #include <linux/printk.h>
15 #include "irq-riscv-aplic-main.h"
17 void aplic_irq_unmask(struct irq_data *d)
19 struct aplic_priv *priv = irq_data_get_irq_chip_data(d);
21 writel(d->hwirq, priv->regs + APLIC_SETIENUM);
24 void aplic_irq_mask(struct irq_data *d)
26 struct aplic_priv *priv = irq_data_get_irq_chip_data(d);
28 writel(d->hwirq, priv->regs + APLIC_CLRIENUM);
31 int aplic_irq_set_type(struct irq_data *d, unsigned int type)
33 struct aplic_priv *priv = irq_data_get_irq_chip_data(d);
34 void __iomem *sourcecfg;
39 val = APLIC_SOURCECFG_SM_INACTIVE;
41 case IRQ_TYPE_LEVEL_LOW:
42 val = APLIC_SOURCECFG_SM_LEVEL_LOW;
44 case IRQ_TYPE_LEVEL_HIGH:
45 val = APLIC_SOURCECFG_SM_LEVEL_HIGH;
47 case IRQ_TYPE_EDGE_FALLING:
48 val = APLIC_SOURCECFG_SM_EDGE_FALL;
50 case IRQ_TYPE_EDGE_RISING:
51 val = APLIC_SOURCECFG_SM_EDGE_RISE;
57 sourcecfg = priv->regs + APLIC_SOURCECFG_BASE;
58 sourcecfg += (d->hwirq - 1) * sizeof(u32);
59 writel(val, sourcecfg);
64 int aplic_irqdomain_translate(struct irq_fwspec *fwspec, u32 gsi_base,
65 unsigned long *hwirq, unsigned int *type)
67 if (WARN_ON(fwspec->param_count < 2))
69 if (WARN_ON(!fwspec->param[0]))
72 /* For DT, gsi_base is always zero. */
73 *hwirq = fwspec->param[0] - gsi_base;
74 *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
76 WARN_ON(*type == IRQ_TYPE_NONE);
81 void aplic_init_hw_global(struct aplic_priv *priv, bool msi_mode)
84 #ifdef CONFIG_RISCV_M_MODE
88 val = lower_32_bits(priv->msicfg.base_ppn);
89 valh = FIELD_PREP(APLIC_xMSICFGADDRH_BAPPN, upper_32_bits(priv->msicfg.base_ppn));
90 valh |= FIELD_PREP(APLIC_xMSICFGADDRH_LHXW, priv->msicfg.lhxw);
91 valh |= FIELD_PREP(APLIC_xMSICFGADDRH_HHXW, priv->msicfg.hhxw);
92 valh |= FIELD_PREP(APLIC_xMSICFGADDRH_LHXS, priv->msicfg.lhxs);
93 valh |= FIELD_PREP(APLIC_xMSICFGADDRH_HHXS, priv->msicfg.hhxs);
94 writel(val, priv->regs + APLIC_xMSICFGADDR);
95 writel(valh, priv->regs + APLIC_xMSICFGADDRH);
99 /* Setup APLIC domaincfg register */
100 val = readl(priv->regs + APLIC_DOMAINCFG);
101 val |= APLIC_DOMAINCFG_IE;
103 val |= APLIC_DOMAINCFG_DM;
104 writel(val, priv->regs + APLIC_DOMAINCFG);
105 if (readl(priv->regs + APLIC_DOMAINCFG) != val)
106 dev_warn(priv->dev, "unable to write 0x%x in domaincfg\n", val);
109 static void aplic_init_hw_irqs(struct aplic_priv *priv)
113 /* Disable all interrupts */
114 for (i = 0; i <= priv->nr_irqs; i += 32)
115 writel(-1U, priv->regs + APLIC_CLRIE_BASE + (i / 32) * sizeof(u32));
117 /* Set interrupt type and default priority for all interrupts */
118 for (i = 1; i <= priv->nr_irqs; i++) {
119 writel(0, priv->regs + APLIC_SOURCECFG_BASE + (i - 1) * sizeof(u32));
120 writel(APLIC_DEFAULT_PRIORITY,
121 priv->regs + APLIC_TARGET_BASE + (i - 1) * sizeof(u32));
124 /* Clear APLIC domaincfg */
125 writel(0, priv->regs + APLIC_DOMAINCFG);
128 int aplic_setup_priv(struct aplic_priv *priv, struct device *dev, void __iomem *regs)
130 struct of_phandle_args parent;
134 * Currently, only OF fwnode is supported so extend this
135 * function for ACPI support.
137 if (!is_of_node(dev->fwnode))
140 /* Save device pointer and register base */
144 /* Find out number of interrupt sources */
145 rc = of_property_read_u32(to_of_node(dev->fwnode), "riscv,num-sources",
148 dev_err(dev, "failed to get number of interrupt sources\n");
153 * Find out number of IDCs based on parent interrupts
155 * If "msi-parent" property is present then we ignore the
156 * APLIC IDCs which forces the APLIC driver to use MSI mode.
158 if (!of_property_present(to_of_node(dev->fwnode), "msi-parent")) {
159 while (!of_irq_parse_one(to_of_node(dev->fwnode), priv->nr_idcs, &parent))
163 /* Setup initial state APLIC interrupts */
164 aplic_init_hw_irqs(priv);
169 static int aplic_probe(struct platform_device *pdev)
171 struct device *dev = &pdev->dev;
172 bool msi_mode = false;
176 /* Map the MMIO registers */
177 regs = devm_platform_ioremap_resource(pdev, 0);
179 dev_err(dev, "failed map MMIO registers\n");
184 * If msi-parent property is present then setup APLIC MSI
185 * mode otherwise setup APLIC direct mode.
187 if (is_of_node(dev->fwnode))
188 msi_mode = of_property_present(to_of_node(dev->fwnode), "msi-parent");
190 rc = aplic_msi_setup(dev, regs);
192 rc = aplic_direct_setup(dev, regs);
194 dev_err(dev, "failed to setup APLIC in %s mode\n", msi_mode ? "MSI" : "direct");
199 static const struct of_device_id aplic_match[] = {
200 { .compatible = "riscv,aplic" },
204 static struct platform_driver aplic_driver = {
206 .name = "riscv-aplic",
207 .of_match_table = aplic_match,
209 .probe = aplic_probe,
211 builtin_platform_driver(aplic_driver);