xref: /linux/drivers/irqchip/irq-ls-extirq.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define pr_fmt(fmt) "irq-ls-extirq: " fmt
4 
5 #include <linux/irq.h>
6 #include <linux/irqchip.h>
7 #include <linux/irqdomain.h>
8 #include <linux/of.h>
9 #include <linux/of_address.h>
10 #include <linux/slab.h>
11 
12 #include <dt-bindings/interrupt-controller/arm-gic.h>
13 
14 #define MAXIRQ 12
15 #define LS1021A_SCFGREVCR 0x200
16 
17 struct ls_extirq_data {
18 	void __iomem		*intpcr;
19 	raw_spinlock_t		lock;
20 	bool			big_endian;
21 	bool			is_ls1021a_or_ls1043a;
22 	u32			nirq;
23 	struct irq_fwspec	map[MAXIRQ];
24 };
25 
26 static void ls_extirq_intpcr_rmw(struct ls_extirq_data *priv, u32 mask,
27 				 u32 value)
28 {
29 	u32 intpcr;
30 
31 	/*
32 	 * Serialize concurrent calls to ls_extirq_set_type() from multiple
33 	 * IRQ descriptors, making sure the read-modify-write is atomic.
34 	 */
35 	raw_spin_lock(&priv->lock);
36 
37 	if (priv->big_endian)
38 		intpcr = ioread32be(priv->intpcr);
39 	else
40 		intpcr = ioread32(priv->intpcr);
41 
42 	intpcr &= ~mask;
43 	intpcr |= value;
44 
45 	if (priv->big_endian)
46 		iowrite32be(intpcr, priv->intpcr);
47 	else
48 		iowrite32(intpcr, priv->intpcr);
49 
50 	raw_spin_unlock(&priv->lock);
51 }
52 
53 static int
54 ls_extirq_set_type(struct irq_data *data, unsigned int type)
55 {
56 	struct ls_extirq_data *priv = data->chip_data;
57 	irq_hw_number_t hwirq = data->hwirq;
58 	u32 value, mask;
59 
60 	if (priv->is_ls1021a_or_ls1043a)
61 		mask = 1U << (31 - hwirq);
62 	else
63 		mask = 1U << hwirq;
64 
65 	switch (type) {
66 	case IRQ_TYPE_LEVEL_LOW:
67 		type = IRQ_TYPE_LEVEL_HIGH;
68 		value = mask;
69 		break;
70 	case IRQ_TYPE_EDGE_FALLING:
71 		type = IRQ_TYPE_EDGE_RISING;
72 		value = mask;
73 		break;
74 	case IRQ_TYPE_LEVEL_HIGH:
75 	case IRQ_TYPE_EDGE_RISING:
76 		value = 0;
77 		break;
78 	default:
79 		return -EINVAL;
80 	}
81 
82 	ls_extirq_intpcr_rmw(priv, mask, value);
83 
84 	return irq_chip_set_type_parent(data, type);
85 }
86 
87 static struct irq_chip ls_extirq_chip = {
88 	.name			= "ls-extirq",
89 	.irq_mask		= irq_chip_mask_parent,
90 	.irq_unmask		= irq_chip_unmask_parent,
91 	.irq_eoi		= irq_chip_eoi_parent,
92 	.irq_set_type		= ls_extirq_set_type,
93 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
94 	.irq_set_affinity	= irq_chip_set_affinity_parent,
95 	.flags                  = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_SKIP_SET_WAKE,
96 };
97 
98 static int
99 ls_extirq_domain_alloc(struct irq_domain *domain, unsigned int virq,
100 		       unsigned int nr_irqs, void *arg)
101 {
102 	struct ls_extirq_data *priv = domain->host_data;
103 	struct irq_fwspec *fwspec = arg;
104 	irq_hw_number_t hwirq;
105 
106 	if (fwspec->param_count != 2)
107 		return -EINVAL;
108 
109 	hwirq = fwspec->param[0];
110 	if (hwirq >= priv->nirq)
111 		return -EINVAL;
112 
113 	irq_domain_set_hwirq_and_chip(domain, virq, hwirq, &ls_extirq_chip,
114 				      priv);
115 
116 	return irq_domain_alloc_irqs_parent(domain, virq, 1, &priv->map[hwirq]);
117 }
118 
119 static const struct irq_domain_ops extirq_domain_ops = {
120 	.xlate		= irq_domain_xlate_twocell,
121 	.alloc		= ls_extirq_domain_alloc,
122 	.free		= irq_domain_free_irqs_common,
123 };
124 
125 static int
126 ls_extirq_parse_map(struct ls_extirq_data *priv, struct device_node *node)
127 {
128 	const __be32 *map;
129 	u32 mapsize;
130 	int ret;
131 
132 	map = of_get_property(node, "interrupt-map", &mapsize);
133 	if (!map)
134 		return -ENOENT;
135 	if (mapsize % sizeof(*map))
136 		return -EINVAL;
137 	mapsize /= sizeof(*map);
138 
139 	while (mapsize) {
140 		struct device_node *ipar;
141 		u32 hwirq, intsize, j;
142 
143 		if (mapsize < 3)
144 			return -EINVAL;
145 		hwirq = be32_to_cpup(map);
146 		if (hwirq >= MAXIRQ)
147 			return -EINVAL;
148 		priv->nirq = max(priv->nirq, hwirq + 1);
149 
150 		ipar = of_find_node_by_phandle(be32_to_cpup(map + 2));
151 		map += 3;
152 		mapsize -= 3;
153 		if (!ipar)
154 			return -EINVAL;
155 		priv->map[hwirq].fwnode = &ipar->fwnode;
156 		ret = of_property_read_u32(ipar, "#interrupt-cells", &intsize);
157 		if (ret)
158 			return ret;
159 
160 		if (intsize > mapsize)
161 			return -EINVAL;
162 
163 		priv->map[hwirq].param_count = intsize;
164 		for (j = 0; j < intsize; ++j)
165 			priv->map[hwirq].param[j] = be32_to_cpup(map++);
166 		mapsize -= intsize;
167 	}
168 	return 0;
169 }
170 
171 static int ls_extirq_probe(struct platform_device *pdev)
172 {
173 	struct irq_domain *domain, *parent_domain;
174 	struct device_node *node, *parent;
175 	struct device *dev = &pdev->dev;
176 	struct ls_extirq_data *priv;
177 	int ret;
178 
179 	node = dev->of_node;
180 	parent = of_irq_find_parent(node);
181 	if (!parent)
182 		return dev_err_probe(dev, -ENODEV, "Failed to get IRQ parent node\n");
183 
184 	parent_domain = irq_find_host(parent);
185 	if (!parent_domain)
186 		return dev_err_probe(dev, -EPROBE_DEFER, "Cannot find parent domain\n");
187 
188 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
189 	if (!priv)
190 		return dev_err_probe(dev, -ENOMEM, "Failed to allocate memory\n");
191 
192 	priv->intpcr = devm_of_iomap(dev, node, 0, NULL);
193 	if (!priv->intpcr)
194 		return dev_err_probe(dev, -ENOMEM, "Cannot ioremap OF node %pOF\n", node);
195 
196 	ret = ls_extirq_parse_map(priv, node);
197 	if (ret)
198 		return dev_err_probe(dev, ret, "Failed to parse IRQ map\n");
199 
200 	priv->big_endian = of_device_is_big_endian(node->parent);
201 	priv->is_ls1021a_or_ls1043a = of_device_is_compatible(node, "fsl,ls1021a-extirq") ||
202 				      of_device_is_compatible(node, "fsl,ls1043a-extirq");
203 	raw_spin_lock_init(&priv->lock);
204 
205 	domain = irq_domain_create_hierarchy(parent_domain, 0, priv->nirq, of_fwnode_handle(node),
206 					     &extirq_domain_ops, priv);
207 	if (!domain)
208 		return dev_err_probe(dev, -ENOMEM, "Failed to add IRQ domain\n");
209 
210 	return 0;
211 }
212 
213 static const struct of_device_id ls_extirq_dt_ids[] = {
214 	{ .compatible = "fsl,ls1021a-extirq" },
215 	{ .compatible = "fsl,ls1043a-extirq" },
216 	{ .compatible = "fsl,ls1088a-extirq" },
217 	{}
218 };
219 MODULE_DEVICE_TABLE(of, ls_extirq_dt_ids);
220 
221 static struct platform_driver ls_extirq_driver = {
222 	.probe = ls_extirq_probe,
223 	.driver = {
224 		.name = "ls-extirq",
225 		.of_match_table = ls_extirq_dt_ids,
226 	}
227 };
228 
229 builtin_platform_driver(ls_extirq_driver);
230