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_eoi(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
);
45 irq_chip_eoi_parent(d
);
48 static void exiu_irq_mask(struct irq_data
*d
)
50 struct exiu_irq_data
*data
= irq_data_get_irq_chip_data(d
);
53 val
= readl_relaxed(data
->base
+ EIMASK
) | BIT(d
->hwirq
);
54 writel_relaxed(val
, data
->base
+ EIMASK
);
55 irq_chip_mask_parent(d
);
58 static void exiu_irq_unmask(struct irq_data
*d
)
60 struct exiu_irq_data
*data
= irq_data_get_irq_chip_data(d
);
63 val
= readl_relaxed(data
->base
+ EIMASK
) & ~BIT(d
->hwirq
);
64 writel_relaxed(val
, data
->base
+ EIMASK
);
65 irq_chip_unmask_parent(d
);
68 static void exiu_irq_enable(struct irq_data
*d
)
70 struct exiu_irq_data
*data
= irq_data_get_irq_chip_data(d
);
73 /* clear interrupts that were latched while disabled */
74 writel_relaxed(BIT(d
->hwirq
), data
->base
+ EIREQCLR
);
76 val
= readl_relaxed(data
->base
+ EIMASK
) & ~BIT(d
->hwirq
);
77 writel_relaxed(val
, data
->base
+ EIMASK
);
78 irq_chip_enable_parent(d
);
81 static int exiu_irq_set_type(struct irq_data
*d
, unsigned int type
)
83 struct exiu_irq_data
*data
= irq_data_get_irq_chip_data(d
);
86 val
= readl_relaxed(data
->base
+ EILVL
);
87 if (type
== IRQ_TYPE_EDGE_RISING
|| type
== IRQ_TYPE_LEVEL_HIGH
)
90 val
&= ~BIT(d
->hwirq
);
91 writel_relaxed(val
, data
->base
+ EILVL
);
93 val
= readl_relaxed(data
->base
+ EIEDG
);
94 if (type
== IRQ_TYPE_LEVEL_LOW
|| type
== IRQ_TYPE_LEVEL_HIGH
)
95 val
&= ~BIT(d
->hwirq
);
98 writel_relaxed(val
, data
->base
+ EIEDG
);
100 writel_relaxed(BIT(d
->hwirq
), data
->base
+ EIREQCLR
);
102 return irq_chip_set_type_parent(d
, IRQ_TYPE_LEVEL_HIGH
);
105 static struct irq_chip exiu_irq_chip
= {
107 .irq_eoi
= exiu_irq_eoi
,
108 .irq_enable
= exiu_irq_enable
,
109 .irq_mask
= exiu_irq_mask
,
110 .irq_unmask
= exiu_irq_unmask
,
111 .irq_set_type
= exiu_irq_set_type
,
112 .irq_set_affinity
= irq_chip_set_affinity_parent
,
113 .flags
= IRQCHIP_SET_TYPE_MASKED
|
114 IRQCHIP_SKIP_SET_WAKE
|
115 IRQCHIP_EOI_THREADED
|
116 IRQCHIP_MASK_ON_SUSPEND
,
119 static int exiu_domain_translate(struct irq_domain
*domain
,
120 struct irq_fwspec
*fwspec
,
121 unsigned long *hwirq
,
124 struct exiu_irq_data
*info
= domain
->host_data
;
126 if (is_of_node(fwspec
->fwnode
)) {
127 if (fwspec
->param_count
!= 3)
130 if (fwspec
->param
[0] != GIC_SPI
)
131 return -EINVAL
; /* No PPI should point to this domain */
133 *hwirq
= fwspec
->param
[1] - info
->spi_base
;
134 *type
= fwspec
->param
[2] & IRQ_TYPE_SENSE_MASK
;
136 if (fwspec
->param_count
!= 2)
138 *hwirq
= fwspec
->param
[0];
139 *type
= fwspec
->param
[1] & IRQ_TYPE_SENSE_MASK
;
144 static int exiu_domain_alloc(struct irq_domain
*dom
, unsigned int virq
,
145 unsigned int nr_irqs
, void *data
)
147 struct irq_fwspec
*fwspec
= data
;
148 struct irq_fwspec parent_fwspec
;
149 struct exiu_irq_data
*info
= dom
->host_data
;
150 irq_hw_number_t hwirq
;
152 parent_fwspec
= *fwspec
;
153 if (is_of_node(dom
->parent
->fwnode
)) {
154 if (fwspec
->param_count
!= 3)
155 return -EINVAL
; /* Not GIC compliant */
156 if (fwspec
->param
[0] != GIC_SPI
)
157 return -EINVAL
; /* No PPI should point to this domain */
159 hwirq
= fwspec
->param
[1] - info
->spi_base
;
161 hwirq
= fwspec
->param
[0];
162 parent_fwspec
.param
[0] = hwirq
+ info
->spi_base
+ 32;
164 WARN_ON(nr_irqs
!= 1);
165 irq_domain_set_hwirq_and_chip(dom
, virq
, hwirq
, &exiu_irq_chip
, info
);
167 parent_fwspec
.fwnode
= dom
->parent
->fwnode
;
168 return irq_domain_alloc_irqs_parent(dom
, virq
, nr_irqs
, &parent_fwspec
);
171 static const struct irq_domain_ops exiu_domain_ops
= {
172 .translate
= exiu_domain_translate
,
173 .alloc
= exiu_domain_alloc
,
174 .free
= irq_domain_free_irqs_common
,
177 static struct exiu_irq_data
*exiu_init(const struct fwnode_handle
*fwnode
,
178 struct resource
*res
)
180 struct exiu_irq_data
*data
;
183 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
185 return ERR_PTR(-ENOMEM
);
187 if (fwnode_property_read_u32_array(fwnode
, "socionext,spi-base",
188 &data
->spi_base
, 1)) {
193 data
->base
= ioremap(res
->start
, resource_size(res
));
199 /* clear and mask all interrupts */
200 writel_relaxed(0xFFFFFFFF, data
->base
+ EIREQCLR
);
201 writel_relaxed(0xFFFFFFFF, data
->base
+ EIMASK
);
210 static int __init
exiu_dt_init(struct device_node
*node
,
211 struct device_node
*parent
)
213 struct irq_domain
*parent_domain
, *domain
;
214 struct exiu_irq_data
*data
;
218 pr_err("%pOF: no parent, giving up\n", node
);
222 parent_domain
= irq_find_host(parent
);
223 if (!parent_domain
) {
224 pr_err("%pOF: unable to obtain parent domain\n", node
);
228 if (of_address_to_resource(node
, 0, &res
)) {
229 pr_err("%pOF: failed to parse memory resource\n", node
);
233 data
= exiu_init(of_node_to_fwnode(node
), &res
);
235 return PTR_ERR(data
);
237 domain
= irq_domain_add_hierarchy(parent_domain
, 0, NUM_IRQS
, node
,
238 &exiu_domain_ops
, data
);
240 pr_err("%pOF: failed to allocate domain\n", node
);
244 pr_info("%pOF: %d interrupts forwarded to %pOF\n", node
, NUM_IRQS
,
254 IRQCHIP_DECLARE(exiu
, "socionext,synquacer-exiu", exiu_dt_init
);
257 static int exiu_acpi_probe(struct platform_device
*pdev
)
259 struct irq_domain
*domain
;
260 struct exiu_irq_data
*data
;
261 struct resource
*res
;
263 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
265 dev_err(&pdev
->dev
, "failed to parse memory resource\n");
269 data
= exiu_init(dev_fwnode(&pdev
->dev
), res
);
271 return PTR_ERR(data
);
273 domain
= acpi_irq_create_hierarchy(0, NUM_IRQS
, dev_fwnode(&pdev
->dev
),
274 &exiu_domain_ops
, data
);
276 dev_err(&pdev
->dev
, "failed to create IRQ domain\n");
280 dev_info(&pdev
->dev
, "%d interrupts forwarded\n", NUM_IRQS
);
290 static const struct acpi_device_id exiu_acpi_ids
[] = {
294 MODULE_DEVICE_TABLE(acpi
, exiu_acpi_ids
);
296 static struct platform_driver exiu_driver
= {
299 .acpi_match_table
= exiu_acpi_ids
,
301 .probe
= exiu_acpi_probe
,
303 builtin_platform_driver(exiu_driver
);