11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25fe3bba3SYingjoe Chen /*
35fe3bba3SYingjoe Chen  * Copyright (c) 2014 MediaTek Inc.
45fe3bba3SYingjoe Chen  * Author: Joe.C <yingjoe.chen@mediatek.com>
55fe3bba3SYingjoe Chen  */
65fe3bba3SYingjoe Chen 
75fe3bba3SYingjoe Chen #include <linux/irq.h>
841a83e06SJoel Porquet #include <linux/irqchip.h>
95fe3bba3SYingjoe Chen #include <linux/irqdomain.h>
105fe3bba3SYingjoe Chen #include <linux/of.h>
115fe3bba3SYingjoe Chen #include <linux/of_irq.h>
125fe3bba3SYingjoe Chen #include <linux/of_address.h>
135fe3bba3SYingjoe Chen #include <linux/io.h>
145fe3bba3SYingjoe Chen #include <linux/slab.h>
155fe3bba3SYingjoe Chen #include <linux/spinlock.h>
165fe3bba3SYingjoe Chen 
175fe3bba3SYingjoe Chen struct mtk_sysirq_chip_data {
186eeb997aSBartosz Golaszewski 	raw_spinlock_t lock;
1913683f9bSMars Cheng 	u32 nr_intpol_bases;
2013683f9bSMars Cheng 	void __iomem **intpol_bases;
2113683f9bSMars Cheng 	u32 *intpol_words;
2213683f9bSMars Cheng 	u8 *intpol_idx;
2313683f9bSMars Cheng 	u16 *which_word;
245fe3bba3SYingjoe Chen };
255fe3bba3SYingjoe Chen 
mtk_sysirq_set_type(struct irq_data * data,unsigned int type)265fe3bba3SYingjoe Chen static int mtk_sysirq_set_type(struct irq_data *data, unsigned int type)
275fe3bba3SYingjoe Chen {
285fe3bba3SYingjoe Chen 	irq_hw_number_t hwirq = data->hwirq;
295fe3bba3SYingjoe Chen 	struct mtk_sysirq_chip_data *chip_data = data->chip_data;
3013683f9bSMars Cheng 	u8 intpol_idx = chip_data->intpol_idx[hwirq];
3113683f9bSMars Cheng 	void __iomem *base;
325fe3bba3SYingjoe Chen 	u32 offset, reg_index, value;
335fe3bba3SYingjoe Chen 	unsigned long flags;
345fe3bba3SYingjoe Chen 	int ret;
355fe3bba3SYingjoe Chen 
3613683f9bSMars Cheng 	base = chip_data->intpol_bases[intpol_idx];
3713683f9bSMars Cheng 	reg_index = chip_data->which_word[hwirq];
385fe3bba3SYingjoe Chen 	offset = hwirq & 0x1f;
395fe3bba3SYingjoe Chen 
406eeb997aSBartosz Golaszewski 	raw_spin_lock_irqsave(&chip_data->lock, flags);
4113683f9bSMars Cheng 	value = readl_relaxed(base + reg_index * 4);
425fe3bba3SYingjoe Chen 	if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_EDGE_FALLING) {
435fe3bba3SYingjoe Chen 		if (type == IRQ_TYPE_LEVEL_LOW)
445fe3bba3SYingjoe Chen 			type = IRQ_TYPE_LEVEL_HIGH;
455fe3bba3SYingjoe Chen 		else
465fe3bba3SYingjoe Chen 			type = IRQ_TYPE_EDGE_RISING;
475fe3bba3SYingjoe Chen 		value |= (1 << offset);
485fe3bba3SYingjoe Chen 	} else {
495fe3bba3SYingjoe Chen 		value &= ~(1 << offset);
505fe3bba3SYingjoe Chen 	}
5113683f9bSMars Cheng 
525e11d16cSMars Cheng 	writel_relaxed(value, base + reg_index * 4);
535fe3bba3SYingjoe Chen 
545fe3bba3SYingjoe Chen 	data = data->parent_data;
555fe3bba3SYingjoe Chen 	ret = data->chip->irq_set_type(data, type);
566eeb997aSBartosz Golaszewski 	raw_spin_unlock_irqrestore(&chip_data->lock, flags);
575fe3bba3SYingjoe Chen 	return ret;
585fe3bba3SYingjoe Chen }
595fe3bba3SYingjoe Chen 
605fe3bba3SYingjoe Chen static struct irq_chip mtk_sysirq_chip = {
615fe3bba3SYingjoe Chen 	.name			= "MT_SYSIRQ",
625fe3bba3SYingjoe Chen 	.irq_mask		= irq_chip_mask_parent,
635fe3bba3SYingjoe Chen 	.irq_unmask		= irq_chip_unmask_parent,
645fe3bba3SYingjoe Chen 	.irq_eoi		= irq_chip_eoi_parent,
655fe3bba3SYingjoe Chen 	.irq_set_type		= mtk_sysirq_set_type,
665fe3bba3SYingjoe Chen 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
675fe3bba3SYingjoe Chen 	.irq_set_affinity	= irq_chip_set_affinity_parent,
68*c775626fSMarkus Schneider-Pargmann 	.flags			= IRQCHIP_SKIP_SET_WAKE,
695fe3bba3SYingjoe Chen };
705fe3bba3SYingjoe Chen 
mtk_sysirq_domain_translate(struct irq_domain * d,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)71f833f57fSMarc Zyngier static int mtk_sysirq_domain_translate(struct irq_domain *d,
72f833f57fSMarc Zyngier 				       struct irq_fwspec *fwspec,
73f833f57fSMarc Zyngier 				       unsigned long *hwirq,
74f833f57fSMarc Zyngier 				       unsigned int *type)
755fe3bba3SYingjoe Chen {
76f833f57fSMarc Zyngier 	if (is_of_node(fwspec->fwnode)) {
77f833f57fSMarc Zyngier 		if (fwspec->param_count != 3)
785fe3bba3SYingjoe Chen 			return -EINVAL;
795fe3bba3SYingjoe Chen 
80f833f57fSMarc Zyngier 		/* No PPI should point to this domain */
81f833f57fSMarc Zyngier 		if (fwspec->param[0] != 0)
825fe3bba3SYingjoe Chen 			return -EINVAL;
835fe3bba3SYingjoe Chen 
84f833f57fSMarc Zyngier 		*hwirq = fwspec->param[1];
85f833f57fSMarc Zyngier 		*type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
865fe3bba3SYingjoe Chen 		return 0;
875fe3bba3SYingjoe Chen 	}
885fe3bba3SYingjoe Chen 
89f833f57fSMarc Zyngier 	return -EINVAL;
90f833f57fSMarc Zyngier }
91f833f57fSMarc Zyngier 
mtk_sysirq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)925fe3bba3SYingjoe Chen static int mtk_sysirq_domain_alloc(struct irq_domain *domain, unsigned int virq,
935fe3bba3SYingjoe Chen 				   unsigned int nr_irqs, void *arg)
945fe3bba3SYingjoe Chen {
955fe3bba3SYingjoe Chen 	int i;
965fe3bba3SYingjoe Chen 	irq_hw_number_t hwirq;
97f833f57fSMarc Zyngier 	struct irq_fwspec *fwspec = arg;
98f833f57fSMarc Zyngier 	struct irq_fwspec gic_fwspec = *fwspec;
995fe3bba3SYingjoe Chen 
100f833f57fSMarc Zyngier 	if (fwspec->param_count != 3)
1015fe3bba3SYingjoe Chen 		return -EINVAL;
1025fe3bba3SYingjoe Chen 
1035fe3bba3SYingjoe Chen 	/* sysirq doesn't support PPI */
104f833f57fSMarc Zyngier 	if (fwspec->param[0])
1055fe3bba3SYingjoe Chen 		return -EINVAL;
1065fe3bba3SYingjoe Chen 
107f833f57fSMarc Zyngier 	hwirq = fwspec->param[1];
1085fe3bba3SYingjoe Chen 	for (i = 0; i < nr_irqs; i++)
1095fe3bba3SYingjoe Chen 		irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
1105fe3bba3SYingjoe Chen 					      &mtk_sysirq_chip,
1115fe3bba3SYingjoe Chen 					      domain->host_data);
1125fe3bba3SYingjoe Chen 
113f833f57fSMarc Zyngier 	gic_fwspec.fwnode = domain->parent->fwnode;
114f833f57fSMarc Zyngier 	return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &gic_fwspec);
1155fe3bba3SYingjoe Chen }
1165fe3bba3SYingjoe Chen 
11796009736SKrzysztof Kozlowski static const struct irq_domain_ops sysirq_domain_ops = {
118f833f57fSMarc Zyngier 	.translate	= mtk_sysirq_domain_translate,
1195fe3bba3SYingjoe Chen 	.alloc		= mtk_sysirq_domain_alloc,
1205fe3bba3SYingjoe Chen 	.free		= irq_domain_free_irqs_common,
1215fe3bba3SYingjoe Chen };
1225fe3bba3SYingjoe Chen 
mtk_sysirq_of_init(struct device_node * node,struct device_node * parent)1235fe3bba3SYingjoe Chen static int __init mtk_sysirq_of_init(struct device_node *node,
1245fe3bba3SYingjoe Chen 				     struct device_node *parent)
1255fe3bba3SYingjoe Chen {
1265fe3bba3SYingjoe Chen 	struct irq_domain *domain, *domain_parent;
1275fe3bba3SYingjoe Chen 	struct mtk_sysirq_chip_data *chip_data;
12813683f9bSMars Cheng 	int ret, size, intpol_num = 0, nr_intpol_bases = 0, i = 0;
1295fe3bba3SYingjoe Chen 
1305fe3bba3SYingjoe Chen 	domain_parent = irq_find_host(parent);
1315fe3bba3SYingjoe Chen 	if (!domain_parent) {
1325fe3bba3SYingjoe Chen 		pr_err("mtk_sysirq: interrupt-parent not found\n");
1335fe3bba3SYingjoe Chen 		return -EINVAL;
1345fe3bba3SYingjoe Chen 	}
1355fe3bba3SYingjoe Chen 
1365fe3bba3SYingjoe Chen 	chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL);
1375fe3bba3SYingjoe Chen 	if (!chip_data)
1385fe3bba3SYingjoe Chen 		return -ENOMEM;
1395fe3bba3SYingjoe Chen 
14013683f9bSMars Cheng 	while (of_get_address(node, i++, NULL, NULL))
14113683f9bSMars Cheng 		nr_intpol_bases++;
14213683f9bSMars Cheng 
14313683f9bSMars Cheng 	if (nr_intpol_bases == 0) {
14413683f9bSMars Cheng 		pr_err("mtk_sysirq: base address not specified\n");
14513683f9bSMars Cheng 		ret = -EINVAL;
14613683f9bSMars Cheng 		goto out_free_chip;
14713683f9bSMars Cheng 	}
14813683f9bSMars Cheng 
14913683f9bSMars Cheng 	chip_data->intpol_words = kcalloc(nr_intpol_bases,
15013683f9bSMars Cheng 					  sizeof(*chip_data->intpol_words),
15113683f9bSMars Cheng 					  GFP_KERNEL);
15213683f9bSMars Cheng 	if (!chip_data->intpol_words) {
15313683f9bSMars Cheng 		ret = -ENOMEM;
15413683f9bSMars Cheng 		goto out_free_chip;
15513683f9bSMars Cheng 	}
15613683f9bSMars Cheng 
15713683f9bSMars Cheng 	chip_data->intpol_bases = kcalloc(nr_intpol_bases,
15813683f9bSMars Cheng 					  sizeof(*chip_data->intpol_bases),
15913683f9bSMars Cheng 					  GFP_KERNEL);
16013683f9bSMars Cheng 	if (!chip_data->intpol_bases) {
16113683f9bSMars Cheng 		ret = -ENOMEM;
16213683f9bSMars Cheng 		goto out_free_intpol_words;
16313683f9bSMars Cheng 	}
16413683f9bSMars Cheng 
16513683f9bSMars Cheng 	for (i = 0; i < nr_intpol_bases; i++) {
16613683f9bSMars Cheng 		struct resource res;
16713683f9bSMars Cheng 
16813683f9bSMars Cheng 		ret = of_address_to_resource(node, i, &res);
169cdb647a7SYingjoe Chen 		size = resource_size(&res);
17013683f9bSMars Cheng 		intpol_num += size * 8;
17113683f9bSMars Cheng 		chip_data->intpol_words[i] = size / 4;
17213683f9bSMars Cheng 		chip_data->intpol_bases[i] = of_iomap(node, i);
17313683f9bSMars Cheng 		if (ret || !chip_data->intpol_bases[i]) {
174e81f54c6SRob Herring 			pr_err("%pOF: couldn't map region %d\n", node, i);
17513683f9bSMars Cheng 			ret = -ENODEV;
17613683f9bSMars Cheng 			goto out_free_intpol;
17713683f9bSMars Cheng 		}
17813683f9bSMars Cheng 	}
17913683f9bSMars Cheng 
18013683f9bSMars Cheng 	chip_data->intpol_idx = kcalloc(intpol_num,
18113683f9bSMars Cheng 					sizeof(*chip_data->intpol_idx),
18213683f9bSMars Cheng 					GFP_KERNEL);
18313683f9bSMars Cheng 	if (!chip_data->intpol_idx) {
18413683f9bSMars Cheng 		ret = -ENOMEM;
18513683f9bSMars Cheng 		goto out_free_intpol;
18613683f9bSMars Cheng 	}
18713683f9bSMars Cheng 
18813683f9bSMars Cheng 	chip_data->which_word = kcalloc(intpol_num,
18913683f9bSMars Cheng 					sizeof(*chip_data->which_word),
19013683f9bSMars Cheng 					GFP_KERNEL);
19113683f9bSMars Cheng 	if (!chip_data->which_word) {
19213683f9bSMars Cheng 		ret = -ENOMEM;
19313683f9bSMars Cheng 		goto out_free_intpol_idx;
19413683f9bSMars Cheng 	}
19513683f9bSMars Cheng 
19613683f9bSMars Cheng 	/*
19713683f9bSMars Cheng 	 * assign an index of the intpol_bases for each irq
19813683f9bSMars Cheng 	 * to set it fast later
19913683f9bSMars Cheng 	 */
20013683f9bSMars Cheng 	for (i = 0; i < intpol_num ; i++) {
20113683f9bSMars Cheng 		u32 word = i / 32, j;
20213683f9bSMars Cheng 
20313683f9bSMars Cheng 		for (j = 0; word >= chip_data->intpol_words[j] ; j++)
20413683f9bSMars Cheng 			word -= chip_data->intpol_words[j];
20513683f9bSMars Cheng 
20613683f9bSMars Cheng 		chip_data->intpol_idx[i] = j;
20713683f9bSMars Cheng 		chip_data->which_word[i] = word;
2085fe3bba3SYingjoe Chen 	}
2095fe3bba3SYingjoe Chen 
210cdb647a7SYingjoe Chen 	domain = irq_domain_add_hierarchy(domain_parent, 0, intpol_num, node,
2115fe3bba3SYingjoe Chen 					  &sysirq_domain_ops, chip_data);
2125fe3bba3SYingjoe Chen 	if (!domain) {
2135fe3bba3SYingjoe Chen 		ret = -ENOMEM;
21413683f9bSMars Cheng 		goto out_free_which_word;
2155fe3bba3SYingjoe Chen 	}
2166eeb997aSBartosz Golaszewski 	raw_spin_lock_init(&chip_data->lock);
2175fe3bba3SYingjoe Chen 
2185fe3bba3SYingjoe Chen 	return 0;
2195fe3bba3SYingjoe Chen 
22013683f9bSMars Cheng out_free_which_word:
22113683f9bSMars Cheng 	kfree(chip_data->which_word);
22213683f9bSMars Cheng out_free_intpol_idx:
22313683f9bSMars Cheng 	kfree(chip_data->intpol_idx);
22413683f9bSMars Cheng out_free_intpol:
22513683f9bSMars Cheng 	for (i = 0; i < nr_intpol_bases; i++)
22613683f9bSMars Cheng 		if (chip_data->intpol_bases[i])
22713683f9bSMars Cheng 			iounmap(chip_data->intpol_bases[i]);
22813683f9bSMars Cheng 	kfree(chip_data->intpol_bases);
22913683f9bSMars Cheng out_free_intpol_words:
23013683f9bSMars Cheng 	kfree(chip_data->intpol_words);
23113683f9bSMars Cheng out_free_chip:
2325fe3bba3SYingjoe Chen 	kfree(chip_data);
2335fe3bba3SYingjoe Chen 	return ret;
2345fe3bba3SYingjoe Chen }
235a150dac5SMarc Zyngier IRQCHIP_DECLARE(mtk_sysirq, "mediatek,mt6577-sysirq", mtk_sysirq_of_init);
236