1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Socionext External Interrupt Unit (EXIU)
5 * Copyright (c) 2017-2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
7 * Based on irq-tegra.c:
8 * Copyright (C) 2011 Google, Inc.
9 * Copyright (C) 2010,2013, NVIDIA Corporation
12 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/irqchip.h>
16 #include <linux/irqdomain.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
20 #include <linux/platform_device.h>
22 #include <dt-bindings/interrupt-controller/arm-gic.h>
29 #define EIRAWREQSTA 0x0C
35 struct exiu_irq_data {
40 static void exiu_irq_ack(struct irq_data *d)
42 struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
44 writel(BIT(d->hwirq), data->base + EIREQCLR);
47 static void exiu_irq_eoi(struct irq_data *d)
49 struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
52 * Level triggered interrupts are latched and must be cleared during
53 * EOI or the interrupt will be jammed on. Of course if a level
54 * triggered interrupt is still asserted then the write will not clear
57 if (irqd_is_level_type(d))
58 writel(BIT(d->hwirq), data->base + EIREQCLR);
60 irq_chip_eoi_parent(d);
63 static void exiu_irq_mask(struct irq_data *d)
65 struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
68 val = readl_relaxed(data->base + EIMASK) | BIT(d->hwirq);
69 writel_relaxed(val, data->base + EIMASK);
70 irq_chip_mask_parent(d);
73 static void exiu_irq_unmask(struct irq_data *d)
75 struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
78 val = readl_relaxed(data->base + EIMASK) & ~BIT(d->hwirq);
79 writel_relaxed(val, data->base + EIMASK);
80 irq_chip_unmask_parent(d);
83 static void exiu_irq_enable(struct irq_data *d)
85 struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
88 /* clear interrupts that were latched while disabled */
89 writel_relaxed(BIT(d->hwirq), data->base + EIREQCLR);
91 val = readl_relaxed(data->base + EIMASK) & ~BIT(d->hwirq);
92 writel_relaxed(val, data->base + EIMASK);
93 irq_chip_enable_parent(d);
96 static int exiu_irq_set_type(struct irq_data *d, unsigned int type)
98 struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
101 val = readl_relaxed(data->base + EILVL);
102 if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH)
103 val |= BIT(d->hwirq);
105 val &= ~BIT(d->hwirq);
106 writel_relaxed(val, data->base + EILVL);
108 val = readl_relaxed(data->base + EIEDG);
109 if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH) {
110 val &= ~BIT(d->hwirq);
111 irq_set_handler_locked(d, handle_fasteoi_irq);
113 val |= BIT(d->hwirq);
114 irq_set_handler_locked(d, handle_fasteoi_ack_irq);
116 writel_relaxed(val, data->base + EIEDG);
118 writel_relaxed(BIT(d->hwirq), data->base + EIREQCLR);
120 return irq_chip_set_type_parent(d, IRQ_TYPE_LEVEL_HIGH);
123 static struct irq_chip exiu_irq_chip = {
125 .irq_ack = exiu_irq_ack,
126 .irq_eoi = exiu_irq_eoi,
127 .irq_enable = exiu_irq_enable,
128 .irq_mask = exiu_irq_mask,
129 .irq_unmask = exiu_irq_unmask,
130 .irq_set_type = exiu_irq_set_type,
131 .irq_set_affinity = irq_chip_set_affinity_parent,
132 .flags = IRQCHIP_SET_TYPE_MASKED |
133 IRQCHIP_SKIP_SET_WAKE |
134 IRQCHIP_EOI_THREADED |
135 IRQCHIP_MASK_ON_SUSPEND,
138 static int exiu_domain_translate(struct irq_domain *domain,
139 struct irq_fwspec *fwspec,
140 unsigned long *hwirq,
143 struct exiu_irq_data *info = domain->host_data;
145 if (is_of_node(fwspec->fwnode)) {
146 if (fwspec->param_count != 3)
149 if (fwspec->param[0] != GIC_SPI)
150 return -EINVAL; /* No PPI should point to this domain */
152 *hwirq = fwspec->param[1] - info->spi_base;
153 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
155 if (fwspec->param_count != 2)
157 *hwirq = fwspec->param[0];
158 *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
163 static int exiu_domain_alloc(struct irq_domain *dom, unsigned int virq,
164 unsigned int nr_irqs, void *data)
166 struct irq_fwspec *fwspec = data;
167 struct irq_fwspec parent_fwspec;
168 struct exiu_irq_data *info = dom->host_data;
169 irq_hw_number_t hwirq;
171 parent_fwspec = *fwspec;
172 if (is_of_node(dom->parent->fwnode)) {
173 if (fwspec->param_count != 3)
174 return -EINVAL; /* Not GIC compliant */
175 if (fwspec->param[0] != GIC_SPI)
176 return -EINVAL; /* No PPI should point to this domain */
178 hwirq = fwspec->param[1] - info->spi_base;
180 hwirq = fwspec->param[0];
181 parent_fwspec.param[0] = hwirq + info->spi_base + 32;
183 WARN_ON(nr_irqs != 1);
184 irq_domain_set_hwirq_and_chip(dom, virq, hwirq, &exiu_irq_chip, info);
186 parent_fwspec.fwnode = dom->parent->fwnode;
187 return irq_domain_alloc_irqs_parent(dom, virq, nr_irqs, &parent_fwspec);
190 static const struct irq_domain_ops exiu_domain_ops = {
191 .translate = exiu_domain_translate,
192 .alloc = exiu_domain_alloc,
193 .free = irq_domain_free_irqs_common,
196 static struct exiu_irq_data *exiu_init(const struct fwnode_handle *fwnode,
197 struct resource *res)
199 struct exiu_irq_data *data;
202 data = kzalloc(sizeof(*data), GFP_KERNEL);
204 return ERR_PTR(-ENOMEM);
206 if (fwnode_property_read_u32_array(fwnode, "socionext,spi-base",
207 &data->spi_base, 1)) {
212 data->base = ioremap(res->start, resource_size(res));
218 /* clear and mask all interrupts */
219 writel_relaxed(0xFFFFFFFF, data->base + EIREQCLR);
220 writel_relaxed(0xFFFFFFFF, data->base + EIMASK);
229 static int __init exiu_dt_init(struct device_node *node,
230 struct device_node *parent)
232 struct irq_domain *parent_domain, *domain;
233 struct exiu_irq_data *data;
237 pr_err("%pOF: no parent, giving up\n", node);
241 parent_domain = irq_find_host(parent);
242 if (!parent_domain) {
243 pr_err("%pOF: unable to obtain parent domain\n", node);
247 if (of_address_to_resource(node, 0, &res)) {
248 pr_err("%pOF: failed to parse memory resource\n", node);
252 data = exiu_init(of_node_to_fwnode(node), &res);
254 return PTR_ERR(data);
256 domain = irq_domain_add_hierarchy(parent_domain, 0, NUM_IRQS, node,
257 &exiu_domain_ops, data);
259 pr_err("%pOF: failed to allocate domain\n", node);
263 pr_info("%pOF: %d interrupts forwarded to %pOF\n", node, NUM_IRQS,
273 IRQCHIP_DECLARE(exiu, "socionext,synquacer-exiu", exiu_dt_init);
276 static int exiu_acpi_probe(struct platform_device *pdev)
278 struct irq_domain *domain;
279 struct exiu_irq_data *data;
280 struct resource *res;
282 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
284 dev_err(&pdev->dev, "failed to parse memory resource\n");
288 data = exiu_init(dev_fwnode(&pdev->dev), res);
290 return PTR_ERR(data);
292 domain = acpi_irq_create_hierarchy(0, NUM_IRQS, dev_fwnode(&pdev->dev),
293 &exiu_domain_ops, data);
295 dev_err(&pdev->dev, "failed to create IRQ domain\n");
299 dev_info(&pdev->dev, "%d interrupts forwarded\n", NUM_IRQS);
309 static const struct acpi_device_id exiu_acpi_ids[] = {
313 MODULE_DEVICE_TABLE(acpi, exiu_acpi_ids);
315 static struct platform_driver exiu_driver = {
318 .acpi_match_table = exiu_acpi_ids,
320 .probe = exiu_acpi_probe,
322 builtin_platform_driver(exiu_driver);