xref: /linux/drivers/irqchip/qcom-pdc.c (revision 8e4d5a5bde8896c7fa36b173c613dbbbf9d5dc32)
1f55c73aeSArchana Sathyakumar // SPDX-License-Identifier: GPL-2.0
2f55c73aeSArchana Sathyakumar /*
3f55c73aeSArchana Sathyakumar  * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
4f55c73aeSArchana Sathyakumar  */
5f55c73aeSArchana Sathyakumar 
6f55c73aeSArchana Sathyakumar #include <linux/err.h>
7f55c73aeSArchana Sathyakumar #include <linux/init.h>
8f55c73aeSArchana Sathyakumar #include <linux/irq.h>
9f55c73aeSArchana Sathyakumar #include <linux/irqchip.h>
10f55c73aeSArchana Sathyakumar #include <linux/irqdomain.h>
11f55c73aeSArchana Sathyakumar #include <linux/io.h>
12f55c73aeSArchana Sathyakumar #include <linux/kernel.h>
13f55c73aeSArchana Sathyakumar #include <linux/of.h>
14f55c73aeSArchana Sathyakumar #include <linux/of_address.h>
15f55c73aeSArchana Sathyakumar #include <linux/of_device.h>
16f55c73aeSArchana Sathyakumar #include <linux/spinlock.h>
17f55c73aeSArchana Sathyakumar #include <linux/platform_device.h>
18f55c73aeSArchana Sathyakumar #include <linux/slab.h>
19f55c73aeSArchana Sathyakumar #include <linux/types.h>
20f55c73aeSArchana Sathyakumar 
21f55c73aeSArchana Sathyakumar #define PDC_MAX_IRQS		126
22f55c73aeSArchana Sathyakumar 
23f55c73aeSArchana Sathyakumar #define CLEAR_INTR(reg, intr)	(reg & ~(1 << intr))
24f55c73aeSArchana Sathyakumar #define ENABLE_INTR(reg, intr)	(reg | (1 << intr))
25f55c73aeSArchana Sathyakumar 
26f55c73aeSArchana Sathyakumar #define IRQ_ENABLE_BANK		0x10
27f55c73aeSArchana Sathyakumar #define IRQ_i_CFG		0x110
28f55c73aeSArchana Sathyakumar 
29f55c73aeSArchana Sathyakumar struct pdc_pin_region {
30f55c73aeSArchana Sathyakumar 	u32 pin_base;
31f55c73aeSArchana Sathyakumar 	u32 parent_base;
32f55c73aeSArchana Sathyakumar 	u32 cnt;
33f55c73aeSArchana Sathyakumar };
34f55c73aeSArchana Sathyakumar 
35f55c73aeSArchana Sathyakumar static DEFINE_RAW_SPINLOCK(pdc_lock);
36f55c73aeSArchana Sathyakumar static void __iomem *pdc_base;
37f55c73aeSArchana Sathyakumar static struct pdc_pin_region *pdc_region;
38f55c73aeSArchana Sathyakumar static int pdc_region_cnt;
39f55c73aeSArchana Sathyakumar 
40f55c73aeSArchana Sathyakumar static void pdc_reg_write(int reg, u32 i, u32 val)
41f55c73aeSArchana Sathyakumar {
42f55c73aeSArchana Sathyakumar 	writel_relaxed(val, pdc_base + reg + i * sizeof(u32));
43f55c73aeSArchana Sathyakumar }
44f55c73aeSArchana Sathyakumar 
45f55c73aeSArchana Sathyakumar static u32 pdc_reg_read(int reg, u32 i)
46f55c73aeSArchana Sathyakumar {
47f55c73aeSArchana Sathyakumar 	return readl_relaxed(pdc_base + reg + i * sizeof(u32));
48f55c73aeSArchana Sathyakumar }
49f55c73aeSArchana Sathyakumar 
50f55c73aeSArchana Sathyakumar static void pdc_enable_intr(struct irq_data *d, bool on)
51f55c73aeSArchana Sathyakumar {
52f55c73aeSArchana Sathyakumar 	int pin_out = d->hwirq;
53f55c73aeSArchana Sathyakumar 	u32 index, mask;
54f55c73aeSArchana Sathyakumar 	u32 enable;
55f55c73aeSArchana Sathyakumar 
56f55c73aeSArchana Sathyakumar 	index = pin_out / 32;
57f55c73aeSArchana Sathyakumar 	mask = pin_out % 32;
58f55c73aeSArchana Sathyakumar 
59f55c73aeSArchana Sathyakumar 	raw_spin_lock(&pdc_lock);
60f55c73aeSArchana Sathyakumar 	enable = pdc_reg_read(IRQ_ENABLE_BANK, index);
61f55c73aeSArchana Sathyakumar 	enable = on ? ENABLE_INTR(enable, mask) : CLEAR_INTR(enable, mask);
62f55c73aeSArchana Sathyakumar 	pdc_reg_write(IRQ_ENABLE_BANK, index, enable);
63f55c73aeSArchana Sathyakumar 	raw_spin_unlock(&pdc_lock);
64f55c73aeSArchana Sathyakumar }
65f55c73aeSArchana Sathyakumar 
66f55c73aeSArchana Sathyakumar static void qcom_pdc_gic_mask(struct irq_data *d)
67f55c73aeSArchana Sathyakumar {
68f55c73aeSArchana Sathyakumar 	pdc_enable_intr(d, false);
69f55c73aeSArchana Sathyakumar 	irq_chip_mask_parent(d);
70f55c73aeSArchana Sathyakumar }
71f55c73aeSArchana Sathyakumar 
72f55c73aeSArchana Sathyakumar static void qcom_pdc_gic_unmask(struct irq_data *d)
73f55c73aeSArchana Sathyakumar {
74f55c73aeSArchana Sathyakumar 	pdc_enable_intr(d, true);
75f55c73aeSArchana Sathyakumar 	irq_chip_unmask_parent(d);
76f55c73aeSArchana Sathyakumar }
77f55c73aeSArchana Sathyakumar 
78f55c73aeSArchana Sathyakumar /*
79f55c73aeSArchana Sathyakumar  * GIC does not handle falling edge or active low. To allow falling edge and
80f55c73aeSArchana Sathyakumar  * active low interrupts to be handled at GIC, PDC has an inverter that inverts
81f55c73aeSArchana Sathyakumar  * falling edge into a rising edge and active low into an active high.
82f55c73aeSArchana Sathyakumar  * For the inverter to work, the polarity bit in the IRQ_CONFIG register has to
83f55c73aeSArchana Sathyakumar  * set as per the table below.
84f55c73aeSArchana Sathyakumar  * Level sensitive active low    LOW
85f55c73aeSArchana Sathyakumar  * Rising edge sensitive         NOT USED
86f55c73aeSArchana Sathyakumar  * Falling edge sensitive        LOW
87f55c73aeSArchana Sathyakumar  * Dual Edge sensitive           NOT USED
88f55c73aeSArchana Sathyakumar  * Level sensitive active High   HIGH
89f55c73aeSArchana Sathyakumar  * Falling Edge sensitive        NOT USED
90f55c73aeSArchana Sathyakumar  * Rising edge sensitive         HIGH
91f55c73aeSArchana Sathyakumar  * Dual Edge sensitive           HIGH
92f55c73aeSArchana Sathyakumar  */
93f55c73aeSArchana Sathyakumar enum pdc_irq_config_bits {
94f55c73aeSArchana Sathyakumar 	PDC_LEVEL_LOW		= 0b000,
95f55c73aeSArchana Sathyakumar 	PDC_EDGE_FALLING	= 0b010,
96f55c73aeSArchana Sathyakumar 	PDC_LEVEL_HIGH		= 0b100,
97f55c73aeSArchana Sathyakumar 	PDC_EDGE_RISING		= 0b110,
98f55c73aeSArchana Sathyakumar 	PDC_EDGE_DUAL		= 0b111,
99f55c73aeSArchana Sathyakumar };
100f55c73aeSArchana Sathyakumar 
101f55c73aeSArchana Sathyakumar /**
102f55c73aeSArchana Sathyakumar  * qcom_pdc_gic_set_type: Configure PDC for the interrupt
103f55c73aeSArchana Sathyakumar  *
104f55c73aeSArchana Sathyakumar  * @d: the interrupt data
105f55c73aeSArchana Sathyakumar  * @type: the interrupt type
106f55c73aeSArchana Sathyakumar  *
107f55c73aeSArchana Sathyakumar  * If @type is edge triggered, forward that as Rising edge as PDC
108f55c73aeSArchana Sathyakumar  * takes care of converting falling edge to rising edge signal
109f55c73aeSArchana Sathyakumar  * If @type is level, then forward that as level high as PDC
110f55c73aeSArchana Sathyakumar  * takes care of converting falling edge to rising edge signal
111f55c73aeSArchana Sathyakumar  */
112f55c73aeSArchana Sathyakumar static int qcom_pdc_gic_set_type(struct irq_data *d, unsigned int type)
113f55c73aeSArchana Sathyakumar {
114f55c73aeSArchana Sathyakumar 	int pin_out = d->hwirq;
115f55c73aeSArchana Sathyakumar 	enum pdc_irq_config_bits pdc_type;
116f55c73aeSArchana Sathyakumar 
117f55c73aeSArchana Sathyakumar 	switch (type) {
118f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_EDGE_RISING:
119f55c73aeSArchana Sathyakumar 		pdc_type = PDC_EDGE_RISING;
120f55c73aeSArchana Sathyakumar 		break;
121f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_EDGE_FALLING:
122f55c73aeSArchana Sathyakumar 		pdc_type = PDC_EDGE_FALLING;
123f55c73aeSArchana Sathyakumar 		type = IRQ_TYPE_EDGE_RISING;
124f55c73aeSArchana Sathyakumar 		break;
125f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_EDGE_BOTH:
126f55c73aeSArchana Sathyakumar 		pdc_type = PDC_EDGE_DUAL;
1277bae48b2SLina Iyer 		type = IRQ_TYPE_EDGE_RISING;
128f55c73aeSArchana Sathyakumar 		break;
129f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_LEVEL_HIGH:
130f55c73aeSArchana Sathyakumar 		pdc_type = PDC_LEVEL_HIGH;
131f55c73aeSArchana Sathyakumar 		break;
132f55c73aeSArchana Sathyakumar 	case IRQ_TYPE_LEVEL_LOW:
133f55c73aeSArchana Sathyakumar 		pdc_type = PDC_LEVEL_LOW;
134f55c73aeSArchana Sathyakumar 		type = IRQ_TYPE_LEVEL_HIGH;
135f55c73aeSArchana Sathyakumar 		break;
136f55c73aeSArchana Sathyakumar 	default:
137f55c73aeSArchana Sathyakumar 		WARN_ON(1);
138f55c73aeSArchana Sathyakumar 		return -EINVAL;
139f55c73aeSArchana Sathyakumar 	}
140f55c73aeSArchana Sathyakumar 
141f55c73aeSArchana Sathyakumar 	pdc_reg_write(IRQ_i_CFG, pin_out, pdc_type);
142f55c73aeSArchana Sathyakumar 
143f55c73aeSArchana Sathyakumar 	return irq_chip_set_type_parent(d, type);
144f55c73aeSArchana Sathyakumar }
145f55c73aeSArchana Sathyakumar 
146f55c73aeSArchana Sathyakumar static struct irq_chip qcom_pdc_gic_chip = {
147f55c73aeSArchana Sathyakumar 	.name			= "PDC",
148f55c73aeSArchana Sathyakumar 	.irq_eoi		= irq_chip_eoi_parent,
149f55c73aeSArchana Sathyakumar 	.irq_mask		= qcom_pdc_gic_mask,
150f55c73aeSArchana Sathyakumar 	.irq_unmask		= qcom_pdc_gic_unmask,
151f55c73aeSArchana Sathyakumar 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
152f55c73aeSArchana Sathyakumar 	.irq_set_type		= qcom_pdc_gic_set_type,
153f55c73aeSArchana Sathyakumar 	.flags			= IRQCHIP_MASK_ON_SUSPEND |
154f55c73aeSArchana Sathyakumar 				  IRQCHIP_SET_TYPE_MASKED |
155f55c73aeSArchana Sathyakumar 				  IRQCHIP_SKIP_SET_WAKE,
156f55c73aeSArchana Sathyakumar 	.irq_set_vcpu_affinity	= irq_chip_set_vcpu_affinity_parent,
157f55c73aeSArchana Sathyakumar 	.irq_set_affinity	= irq_chip_set_affinity_parent,
158f55c73aeSArchana Sathyakumar };
159f55c73aeSArchana Sathyakumar 
160f55c73aeSArchana Sathyakumar static irq_hw_number_t get_parent_hwirq(int pin)
161f55c73aeSArchana Sathyakumar {
162f55c73aeSArchana Sathyakumar 	int i;
163f55c73aeSArchana Sathyakumar 	struct pdc_pin_region *region;
164f55c73aeSArchana Sathyakumar 
165f55c73aeSArchana Sathyakumar 	for (i = 0; i < pdc_region_cnt; i++) {
166f55c73aeSArchana Sathyakumar 		region = &pdc_region[i];
167f55c73aeSArchana Sathyakumar 		if (pin >= region->pin_base &&
168f55c73aeSArchana Sathyakumar 		    pin < region->pin_base + region->cnt)
169f55c73aeSArchana Sathyakumar 			return (region->parent_base + pin - region->pin_base);
170f55c73aeSArchana Sathyakumar 	}
171f55c73aeSArchana Sathyakumar 
172f55c73aeSArchana Sathyakumar 	WARN_ON(1);
173f55c73aeSArchana Sathyakumar 	return ~0UL;
174f55c73aeSArchana Sathyakumar }
175f55c73aeSArchana Sathyakumar 
176f55c73aeSArchana Sathyakumar static int qcom_pdc_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
177f55c73aeSArchana Sathyakumar 			      unsigned long *hwirq, unsigned int *type)
178f55c73aeSArchana Sathyakumar {
179f55c73aeSArchana Sathyakumar 	if (is_of_node(fwspec->fwnode)) {
180f55c73aeSArchana Sathyakumar 		if (fwspec->param_count != 2)
181f55c73aeSArchana Sathyakumar 			return -EINVAL;
182f55c73aeSArchana Sathyakumar 
183f55c73aeSArchana Sathyakumar 		*hwirq = fwspec->param[0];
184f55c73aeSArchana Sathyakumar 		*type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
185f55c73aeSArchana Sathyakumar 		return 0;
186f55c73aeSArchana Sathyakumar 	}
187f55c73aeSArchana Sathyakumar 
188f55c73aeSArchana Sathyakumar 	return -EINVAL;
189f55c73aeSArchana Sathyakumar }
190f55c73aeSArchana Sathyakumar 
191f55c73aeSArchana Sathyakumar static int qcom_pdc_alloc(struct irq_domain *domain, unsigned int virq,
192f55c73aeSArchana Sathyakumar 			  unsigned int nr_irqs, void *data)
193f55c73aeSArchana Sathyakumar {
194f55c73aeSArchana Sathyakumar 	struct irq_fwspec *fwspec = data;
195f55c73aeSArchana Sathyakumar 	struct irq_fwspec parent_fwspec;
196f55c73aeSArchana Sathyakumar 	irq_hw_number_t hwirq, parent_hwirq;
197f55c73aeSArchana Sathyakumar 	unsigned int type;
198f55c73aeSArchana Sathyakumar 	int ret;
199f55c73aeSArchana Sathyakumar 
200f55c73aeSArchana Sathyakumar 	ret = qcom_pdc_translate(domain, fwspec, &hwirq, &type);
201f55c73aeSArchana Sathyakumar 	if (ret)
202f55c73aeSArchana Sathyakumar 		return -EINVAL;
203f55c73aeSArchana Sathyakumar 
204f55c73aeSArchana Sathyakumar 	parent_hwirq = get_parent_hwirq(hwirq);
205f55c73aeSArchana Sathyakumar 	if (parent_hwirq == ~0UL)
206f55c73aeSArchana Sathyakumar 		return -EINVAL;
207f55c73aeSArchana Sathyakumar 
208f55c73aeSArchana Sathyakumar 	ret  = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
209f55c73aeSArchana Sathyakumar 					     &qcom_pdc_gic_chip, NULL);
210f55c73aeSArchana Sathyakumar 	if (ret)
211f55c73aeSArchana Sathyakumar 		return ret;
212f55c73aeSArchana Sathyakumar 
213f55c73aeSArchana Sathyakumar 	if (type & IRQ_TYPE_EDGE_BOTH)
214f55c73aeSArchana Sathyakumar 		type = IRQ_TYPE_EDGE_RISING;
215f55c73aeSArchana Sathyakumar 
216f55c73aeSArchana Sathyakumar 	if (type & IRQ_TYPE_LEVEL_MASK)
217f55c73aeSArchana Sathyakumar 		type = IRQ_TYPE_LEVEL_HIGH;
218f55c73aeSArchana Sathyakumar 
219f55c73aeSArchana Sathyakumar 	parent_fwspec.fwnode      = domain->parent->fwnode;
220f55c73aeSArchana Sathyakumar 	parent_fwspec.param_count = 3;
221f55c73aeSArchana Sathyakumar 	parent_fwspec.param[0]    = 0;
222f55c73aeSArchana Sathyakumar 	parent_fwspec.param[1]    = parent_hwirq;
223f55c73aeSArchana Sathyakumar 	parent_fwspec.param[2]    = type;
224f55c73aeSArchana Sathyakumar 
225f55c73aeSArchana Sathyakumar 	return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
226f55c73aeSArchana Sathyakumar 					    &parent_fwspec);
227f55c73aeSArchana Sathyakumar }
228f55c73aeSArchana Sathyakumar 
229f55c73aeSArchana Sathyakumar static const struct irq_domain_ops qcom_pdc_ops = {
230f55c73aeSArchana Sathyakumar 	.translate	= qcom_pdc_translate,
231f55c73aeSArchana Sathyakumar 	.alloc		= qcom_pdc_alloc,
232f55c73aeSArchana Sathyakumar 	.free		= irq_domain_free_irqs_common,
233f55c73aeSArchana Sathyakumar };
234f55c73aeSArchana Sathyakumar 
235f55c73aeSArchana Sathyakumar static int pdc_setup_pin_mapping(struct device_node *np)
236f55c73aeSArchana Sathyakumar {
237f55c73aeSArchana Sathyakumar 	int ret, n;
238f55c73aeSArchana Sathyakumar 
239f55c73aeSArchana Sathyakumar 	n = of_property_count_elems_of_size(np, "qcom,pdc-ranges", sizeof(u32));
240f55c73aeSArchana Sathyakumar 	if (n <= 0 || n % 3)
241f55c73aeSArchana Sathyakumar 		return -EINVAL;
242f55c73aeSArchana Sathyakumar 
243f55c73aeSArchana Sathyakumar 	pdc_region_cnt = n / 3;
244f55c73aeSArchana Sathyakumar 	pdc_region = kcalloc(pdc_region_cnt, sizeof(*pdc_region), GFP_KERNEL);
245f55c73aeSArchana Sathyakumar 	if (!pdc_region) {
246f55c73aeSArchana Sathyakumar 		pdc_region_cnt = 0;
247f55c73aeSArchana Sathyakumar 		return -ENOMEM;
248f55c73aeSArchana Sathyakumar 	}
249f55c73aeSArchana Sathyakumar 
250f55c73aeSArchana Sathyakumar 	for (n = 0; n < pdc_region_cnt; n++) {
251f55c73aeSArchana Sathyakumar 		ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
252f55c73aeSArchana Sathyakumar 						 n * 3 + 0,
253f55c73aeSArchana Sathyakumar 						 &pdc_region[n].pin_base);
254f55c73aeSArchana Sathyakumar 		if (ret)
255f55c73aeSArchana Sathyakumar 			return ret;
256f55c73aeSArchana Sathyakumar 		ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
257f55c73aeSArchana Sathyakumar 						 n * 3 + 1,
258f55c73aeSArchana Sathyakumar 						 &pdc_region[n].parent_base);
259f55c73aeSArchana Sathyakumar 		if (ret)
260f55c73aeSArchana Sathyakumar 			return ret;
261f55c73aeSArchana Sathyakumar 		ret = of_property_read_u32_index(np, "qcom,pdc-ranges",
262f55c73aeSArchana Sathyakumar 						 n * 3 + 2,
263f55c73aeSArchana Sathyakumar 						 &pdc_region[n].cnt);
264f55c73aeSArchana Sathyakumar 		if (ret)
265f55c73aeSArchana Sathyakumar 			return ret;
266f55c73aeSArchana Sathyakumar 	}
267f55c73aeSArchana Sathyakumar 
268f55c73aeSArchana Sathyakumar 	return 0;
269f55c73aeSArchana Sathyakumar }
270f55c73aeSArchana Sathyakumar 
271f55c73aeSArchana Sathyakumar static int qcom_pdc_init(struct device_node *node, struct device_node *parent)
272f55c73aeSArchana Sathyakumar {
273f55c73aeSArchana Sathyakumar 	struct irq_domain *parent_domain, *pdc_domain;
274f55c73aeSArchana Sathyakumar 	int ret;
275f55c73aeSArchana Sathyakumar 
276f55c73aeSArchana Sathyakumar 	pdc_base = of_iomap(node, 0);
277f55c73aeSArchana Sathyakumar 	if (!pdc_base) {
278f55c73aeSArchana Sathyakumar 		pr_err("%pOF: unable to map PDC registers\n", node);
279f55c73aeSArchana Sathyakumar 		return -ENXIO;
280f55c73aeSArchana Sathyakumar 	}
281f55c73aeSArchana Sathyakumar 
282f55c73aeSArchana Sathyakumar 	parent_domain = irq_find_host(parent);
283f55c73aeSArchana Sathyakumar 	if (!parent_domain) {
284f55c73aeSArchana Sathyakumar 		pr_err("%pOF: unable to find PDC's parent domain\n", node);
285f55c73aeSArchana Sathyakumar 		ret = -ENXIO;
286f55c73aeSArchana Sathyakumar 		goto fail;
287f55c73aeSArchana Sathyakumar 	}
288f55c73aeSArchana Sathyakumar 
289f55c73aeSArchana Sathyakumar 	ret = pdc_setup_pin_mapping(node);
290f55c73aeSArchana Sathyakumar 	if (ret) {
291f55c73aeSArchana Sathyakumar 		pr_err("%pOF: failed to init PDC pin-hwirq mapping\n", node);
292f55c73aeSArchana Sathyakumar 		goto fail;
293f55c73aeSArchana Sathyakumar 	}
294f55c73aeSArchana Sathyakumar 
295f55c73aeSArchana Sathyakumar 	pdc_domain = irq_domain_create_hierarchy(parent_domain, 0, PDC_MAX_IRQS,
296f55c73aeSArchana Sathyakumar 						 of_fwnode_handle(node),
297f55c73aeSArchana Sathyakumar 						 &qcom_pdc_ops, NULL);
298f55c73aeSArchana Sathyakumar 	if (!pdc_domain) {
299f55c73aeSArchana Sathyakumar 		pr_err("%pOF: GIC domain add failed\n", node);
300f55c73aeSArchana Sathyakumar 		ret = -ENOMEM;
301f55c73aeSArchana Sathyakumar 		goto fail;
302f55c73aeSArchana Sathyakumar 	}
303f55c73aeSArchana Sathyakumar 
304f55c73aeSArchana Sathyakumar 	return 0;
305f55c73aeSArchana Sathyakumar 
306f55c73aeSArchana Sathyakumar fail:
307f55c73aeSArchana Sathyakumar 	kfree(pdc_region);
308f55c73aeSArchana Sathyakumar 	iounmap(pdc_base);
309f55c73aeSArchana Sathyakumar 	return ret;
310f55c73aeSArchana Sathyakumar }
311f55c73aeSArchana Sathyakumar 
312*8e4d5a5bSRajendra Nayak IRQCHIP_DECLARE(qcom_pdc, "qcom,pdc", qcom_pdc_init);
313