xref: /linux/drivers/irqchip/irq-mbigen.c (revision ed2a1002d25ccdb6606c8ccb608524118bd30614)
1717c3dbcSMa Jun /*
2717c3dbcSMa Jun  * Copyright (C) 2015 Hisilicon Limited, All Rights Reserved.
3717c3dbcSMa Jun  * Author: Jun Ma <majun258@huawei.com>
4717c3dbcSMa Jun  * Author: Yun Wu <wuyun.wu@huawei.com>
5717c3dbcSMa Jun  *
6717c3dbcSMa Jun  * This program is free software; you can redistribute it and/or modify
7717c3dbcSMa Jun  * it under the terms of the GNU General Public License version 2 as
8717c3dbcSMa Jun  * published by the Free Software Foundation.
9717c3dbcSMa Jun  *
10717c3dbcSMa Jun  * This program is distributed in the hope that it will be useful,
11717c3dbcSMa Jun  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12717c3dbcSMa Jun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13717c3dbcSMa Jun  * GNU General Public License for more details.
14717c3dbcSMa Jun  *
15717c3dbcSMa Jun  * You should have received a copy of the GNU General Public License
16717c3dbcSMa Jun  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17717c3dbcSMa Jun  */
18717c3dbcSMa Jun 
199650c60eSMa Jun #include <linux/interrupt.h>
209650c60eSMa Jun #include <linux/irqchip.h>
21717c3dbcSMa Jun #include <linux/module.h>
229650c60eSMa Jun #include <linux/msi.h>
23717c3dbcSMa Jun #include <linux/of_address.h>
24717c3dbcSMa Jun #include <linux/of_irq.h>
25717c3dbcSMa Jun #include <linux/of_platform.h>
26717c3dbcSMa Jun #include <linux/platform_device.h>
27717c3dbcSMa Jun #include <linux/slab.h>
28717c3dbcSMa Jun 
299650c60eSMa Jun /* Interrupt numbers per mbigen node supported */
309650c60eSMa Jun #define IRQS_PER_MBIGEN_NODE		128
319650c60eSMa Jun 
329650c60eSMa Jun /* 64 irqs (Pin0-pin63) are reserved for each mbigen chip */
339650c60eSMa Jun #define RESERVED_IRQ_PER_MBIGEN_CHIP	64
349650c60eSMa Jun 
359650c60eSMa Jun /* The maximum IRQ pin number of mbigen chip(start from 0) */
369650c60eSMa Jun #define MAXIMUM_IRQ_PIN_NUM		1407
379650c60eSMa Jun 
389650c60eSMa Jun /**
399650c60eSMa Jun  * In mbigen vector register
409650c60eSMa Jun  * bit[21:12]:	event id value
419650c60eSMa Jun  * bit[11:0]:	device id
429650c60eSMa Jun  */
439650c60eSMa Jun #define IRQ_EVENT_ID_SHIFT		12
449650c60eSMa Jun #define IRQ_EVENT_ID_MASK		0x3ff
459650c60eSMa Jun 
469650c60eSMa Jun /* register range of each mbigen node */
479650c60eSMa Jun #define MBIGEN_NODE_OFFSET		0x1000
489650c60eSMa Jun 
499650c60eSMa Jun /* offset of vector register in mbigen node */
509650c60eSMa Jun #define REG_MBIGEN_VEC_OFFSET		0x200
519650c60eSMa Jun 
52717c3dbcSMa Jun /**
53a6c2f87bSMa Jun  * offset of clear register in mbigen node
54a6c2f87bSMa Jun  * This register is used to clear the status
55a6c2f87bSMa Jun  * of interrupt
56a6c2f87bSMa Jun  */
57a6c2f87bSMa Jun #define REG_MBIGEN_CLEAR_OFFSET		0xa000
58a6c2f87bSMa Jun 
59a6c2f87bSMa Jun /**
60a6c2f87bSMa Jun  * offset of interrupt type register
61a6c2f87bSMa Jun  * This register is used to configure interrupt
62a6c2f87bSMa Jun  * trigger type
63a6c2f87bSMa Jun  */
64a6c2f87bSMa Jun #define REG_MBIGEN_TYPE_OFFSET		0x0
65a6c2f87bSMa Jun 
66a6c2f87bSMa Jun /**
67717c3dbcSMa Jun  * struct mbigen_device - holds the information of mbigen device.
68717c3dbcSMa Jun  *
69717c3dbcSMa Jun  * @pdev:		pointer to the platform device structure of mbigen chip.
70717c3dbcSMa Jun  * @base:		mapped address of this mbigen chip.
71717c3dbcSMa Jun  */
72717c3dbcSMa Jun struct mbigen_device {
73717c3dbcSMa Jun 	struct platform_device	*pdev;
74717c3dbcSMa Jun 	void __iomem		*base;
75717c3dbcSMa Jun };
76717c3dbcSMa Jun 
779650c60eSMa Jun static inline unsigned int get_mbigen_vec_reg(irq_hw_number_t hwirq)
789650c60eSMa Jun {
799650c60eSMa Jun 	unsigned int nid, pin;
809650c60eSMa Jun 
819650c60eSMa Jun 	hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
829650c60eSMa Jun 	nid = hwirq / IRQS_PER_MBIGEN_NODE + 1;
839650c60eSMa Jun 	pin = hwirq % IRQS_PER_MBIGEN_NODE;
849650c60eSMa Jun 
859650c60eSMa Jun 	return pin * 4 + nid * MBIGEN_NODE_OFFSET
869650c60eSMa Jun 			+ REG_MBIGEN_VEC_OFFSET;
879650c60eSMa Jun }
889650c60eSMa Jun 
89a6c2f87bSMa Jun static inline void get_mbigen_type_reg(irq_hw_number_t hwirq,
90a6c2f87bSMa Jun 					u32 *mask, u32 *addr)
91a6c2f87bSMa Jun {
92a6c2f87bSMa Jun 	unsigned int nid, irq_ofst, ofst;
93a6c2f87bSMa Jun 
94a6c2f87bSMa Jun 	hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
95a6c2f87bSMa Jun 	nid = hwirq / IRQS_PER_MBIGEN_NODE + 1;
96a6c2f87bSMa Jun 	irq_ofst = hwirq % IRQS_PER_MBIGEN_NODE;
97a6c2f87bSMa Jun 
98a6c2f87bSMa Jun 	*mask = 1 << (irq_ofst % 32);
99a6c2f87bSMa Jun 	ofst = irq_ofst / 32 * 4;
100a6c2f87bSMa Jun 
101a6c2f87bSMa Jun 	*addr = ofst + nid * MBIGEN_NODE_OFFSET
102a6c2f87bSMa Jun 		+ REG_MBIGEN_TYPE_OFFSET;
103a6c2f87bSMa Jun }
104a6c2f87bSMa Jun 
105a6c2f87bSMa Jun static inline void get_mbigen_clear_reg(irq_hw_number_t hwirq,
106a6c2f87bSMa Jun 					u32 *mask, u32 *addr)
107a6c2f87bSMa Jun {
108a6c2f87bSMa Jun 	unsigned int ofst;
109a6c2f87bSMa Jun 
110a6c2f87bSMa Jun 	hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
111a6c2f87bSMa Jun 	ofst = hwirq / 32 * 4;
112a6c2f87bSMa Jun 
113a6c2f87bSMa Jun 	*mask = 1 << (hwirq % 32);
114a6c2f87bSMa Jun 	*addr = ofst + REG_MBIGEN_CLEAR_OFFSET;
115a6c2f87bSMa Jun }
116a6c2f87bSMa Jun 
117a6c2f87bSMa Jun static void mbigen_eoi_irq(struct irq_data *data)
118a6c2f87bSMa Jun {
119a6c2f87bSMa Jun 	void __iomem *base = data->chip_data;
120a6c2f87bSMa Jun 	u32 mask, addr;
121a6c2f87bSMa Jun 
122a6c2f87bSMa Jun 	get_mbigen_clear_reg(data->hwirq, &mask, &addr);
123a6c2f87bSMa Jun 
124a6c2f87bSMa Jun 	writel_relaxed(mask, base + addr);
125a6c2f87bSMa Jun 
126a6c2f87bSMa Jun 	irq_chip_eoi_parent(data);
127a6c2f87bSMa Jun }
128a6c2f87bSMa Jun 
129a6c2f87bSMa Jun static int mbigen_set_type(struct irq_data *data, unsigned int type)
130a6c2f87bSMa Jun {
131a6c2f87bSMa Jun 	void __iomem *base = data->chip_data;
132a6c2f87bSMa Jun 	u32 mask, addr, val;
133a6c2f87bSMa Jun 
134a6c2f87bSMa Jun 	if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
135a6c2f87bSMa Jun 		return -EINVAL;
136a6c2f87bSMa Jun 
137a6c2f87bSMa Jun 	get_mbigen_type_reg(data->hwirq, &mask, &addr);
138a6c2f87bSMa Jun 
139a6c2f87bSMa Jun 	val = readl_relaxed(base + addr);
140a6c2f87bSMa Jun 
141a6c2f87bSMa Jun 	if (type == IRQ_TYPE_LEVEL_HIGH)
142a6c2f87bSMa Jun 		val |= mask;
143a6c2f87bSMa Jun 	else
144a6c2f87bSMa Jun 		val &= ~mask;
145a6c2f87bSMa Jun 
146a6c2f87bSMa Jun 	writel_relaxed(val, base + addr);
147a6c2f87bSMa Jun 
148a6c2f87bSMa Jun 	return 0;
149a6c2f87bSMa Jun }
150a6c2f87bSMa Jun 
1519650c60eSMa Jun static struct irq_chip mbigen_irq_chip = {
1529650c60eSMa Jun 	.name =			"mbigen-v2",
153a6c2f87bSMa Jun 	.irq_mask =		irq_chip_mask_parent,
154a6c2f87bSMa Jun 	.irq_unmask =		irq_chip_unmask_parent,
155a6c2f87bSMa Jun 	.irq_eoi =		mbigen_eoi_irq,
156a6c2f87bSMa Jun 	.irq_set_type =		mbigen_set_type,
157a6c2f87bSMa Jun 	.irq_set_affinity =	irq_chip_set_affinity_parent,
1589650c60eSMa Jun };
1599650c60eSMa Jun 
1609650c60eSMa Jun static void mbigen_write_msg(struct msi_desc *desc, struct msi_msg *msg)
1619650c60eSMa Jun {
1629650c60eSMa Jun 	struct irq_data *d = irq_get_irq_data(desc->irq);
1639650c60eSMa Jun 	void __iomem *base = d->chip_data;
1649650c60eSMa Jun 	u32 val;
1659650c60eSMa Jun 
1669650c60eSMa Jun 	base += get_mbigen_vec_reg(d->hwirq);
1679650c60eSMa Jun 	val = readl_relaxed(base);
1689650c60eSMa Jun 
1699650c60eSMa Jun 	val &= ~(IRQ_EVENT_ID_MASK << IRQ_EVENT_ID_SHIFT);
1709650c60eSMa Jun 	val |= (msg->data << IRQ_EVENT_ID_SHIFT);
1719650c60eSMa Jun 
1729650c60eSMa Jun 	/* The address of doorbell is encoded in mbigen register by default
1739650c60eSMa Jun 	 * So,we don't need to program the doorbell address at here
1749650c60eSMa Jun 	 */
1759650c60eSMa Jun 	writel_relaxed(val, base);
1769650c60eSMa Jun }
1779650c60eSMa Jun 
1789650c60eSMa Jun static int mbigen_domain_translate(struct irq_domain *d,
1799650c60eSMa Jun 				    struct irq_fwspec *fwspec,
1809650c60eSMa Jun 				    unsigned long *hwirq,
1819650c60eSMa Jun 				    unsigned int *type)
1829650c60eSMa Jun {
1839650c60eSMa Jun 	if (is_of_node(fwspec->fwnode)) {
1849650c60eSMa Jun 		if (fwspec->param_count != 2)
1859650c60eSMa Jun 			return -EINVAL;
1869650c60eSMa Jun 
1879650c60eSMa Jun 		if ((fwspec->param[0] > MAXIMUM_IRQ_PIN_NUM) ||
1889650c60eSMa Jun 			(fwspec->param[0] < RESERVED_IRQ_PER_MBIGEN_CHIP))
1899650c60eSMa Jun 			return -EINVAL;
1909650c60eSMa Jun 		else
1919650c60eSMa Jun 			*hwirq = fwspec->param[0];
1929650c60eSMa Jun 
1939650c60eSMa Jun 		/* If there is no valid irq type, just use the default type */
1949650c60eSMa Jun 		if ((fwspec->param[1] == IRQ_TYPE_EDGE_RISING) ||
1959650c60eSMa Jun 			(fwspec->param[1] == IRQ_TYPE_LEVEL_HIGH))
1969650c60eSMa Jun 			*type = fwspec->param[1];
1979650c60eSMa Jun 		else
1989650c60eSMa Jun 			return -EINVAL;
1999650c60eSMa Jun 
2009650c60eSMa Jun 		return 0;
2019650c60eSMa Jun 	}
2029650c60eSMa Jun 	return -EINVAL;
2039650c60eSMa Jun }
2049650c60eSMa Jun 
2059650c60eSMa Jun static int mbigen_irq_domain_alloc(struct irq_domain *domain,
2069650c60eSMa Jun 					unsigned int virq,
2079650c60eSMa Jun 					unsigned int nr_irqs,
2089650c60eSMa Jun 					void *args)
2099650c60eSMa Jun {
2109650c60eSMa Jun 	struct irq_fwspec *fwspec = args;
2119650c60eSMa Jun 	irq_hw_number_t hwirq;
2129650c60eSMa Jun 	unsigned int type;
2139650c60eSMa Jun 	struct mbigen_device *mgn_chip;
2149650c60eSMa Jun 	int i, err;
2159650c60eSMa Jun 
2169650c60eSMa Jun 	err = mbigen_domain_translate(domain, fwspec, &hwirq, &type);
2179650c60eSMa Jun 	if (err)
2189650c60eSMa Jun 		return err;
2199650c60eSMa Jun 
2209650c60eSMa Jun 	err = platform_msi_domain_alloc(domain, virq, nr_irqs);
2219650c60eSMa Jun 	if (err)
2229650c60eSMa Jun 		return err;
2239650c60eSMa Jun 
2249650c60eSMa Jun 	mgn_chip = platform_msi_get_host_data(domain);
2259650c60eSMa Jun 
2269650c60eSMa Jun 	for (i = 0; i < nr_irqs; i++)
2279650c60eSMa Jun 		irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
2289650c60eSMa Jun 				      &mbigen_irq_chip, mgn_chip->base);
2299650c60eSMa Jun 
2309650c60eSMa Jun 	return 0;
2319650c60eSMa Jun }
2329650c60eSMa Jun 
2339650c60eSMa Jun static struct irq_domain_ops mbigen_domain_ops = {
2349650c60eSMa Jun 	.translate	= mbigen_domain_translate,
2359650c60eSMa Jun 	.alloc		= mbigen_irq_domain_alloc,
2369650c60eSMa Jun 	.free		= irq_domain_free_irqs_common,
2379650c60eSMa Jun };
2389650c60eSMa Jun 
239717c3dbcSMa Jun static int mbigen_device_probe(struct platform_device *pdev)
240717c3dbcSMa Jun {
241717c3dbcSMa Jun 	struct mbigen_device *mgn_chip;
242*ed2a1002SMaJun 	struct platform_device *child;
2439650c60eSMa Jun 	struct irq_domain *domain;
244*ed2a1002SMaJun 	struct device_node *np;
245*ed2a1002SMaJun 	struct device *parent;
246*ed2a1002SMaJun 	struct resource *res;
2479650c60eSMa Jun 	u32 num_pins;
248717c3dbcSMa Jun 
249717c3dbcSMa Jun 	mgn_chip = devm_kzalloc(&pdev->dev, sizeof(*mgn_chip), GFP_KERNEL);
250717c3dbcSMa Jun 	if (!mgn_chip)
251717c3dbcSMa Jun 		return -ENOMEM;
252717c3dbcSMa Jun 
253717c3dbcSMa Jun 	mgn_chip->pdev = pdev;
254717c3dbcSMa Jun 
255717c3dbcSMa Jun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
256717c3dbcSMa Jun 	mgn_chip->base = devm_ioremap_resource(&pdev->dev, res);
257717c3dbcSMa Jun 	if (IS_ERR(mgn_chip->base))
258717c3dbcSMa Jun 		return PTR_ERR(mgn_chip->base);
259717c3dbcSMa Jun 
260*ed2a1002SMaJun 	for_each_child_of_node(pdev->dev.of_node, np) {
261*ed2a1002SMaJun 		if (!of_property_read_bool(np, "interrupt-controller"))
262*ed2a1002SMaJun 			continue;
263*ed2a1002SMaJun 
264*ed2a1002SMaJun 		parent = platform_bus_type.dev_root;
265*ed2a1002SMaJun 		child = of_platform_device_create(np, NULL, parent);
266*ed2a1002SMaJun 		if (IS_ERR(child))
267*ed2a1002SMaJun 			return PTR_ERR(child);
268*ed2a1002SMaJun 
269*ed2a1002SMaJun 		if (of_property_read_u32(child->dev.of_node, "num-pins",
270*ed2a1002SMaJun 					 &num_pins) < 0) {
2719650c60eSMa Jun 			dev_err(&pdev->dev, "No num-pins property\n");
2729650c60eSMa Jun 			return -EINVAL;
2739650c60eSMa Jun 		}
2749650c60eSMa Jun 
275*ed2a1002SMaJun 		domain = platform_msi_create_device_domain(&child->dev, num_pins,
2769650c60eSMa Jun 							   mbigen_write_msg,
2779650c60eSMa Jun 							   &mbigen_domain_ops,
2789650c60eSMa Jun 							   mgn_chip);
2799650c60eSMa Jun 		if (!domain)
2809650c60eSMa Jun 			return -ENOMEM;
281*ed2a1002SMaJun 	}
2829650c60eSMa Jun 
283717c3dbcSMa Jun 	platform_set_drvdata(pdev, mgn_chip);
284717c3dbcSMa Jun 	return 0;
285717c3dbcSMa Jun }
286717c3dbcSMa Jun 
287717c3dbcSMa Jun static const struct of_device_id mbigen_of_match[] = {
288717c3dbcSMa Jun 	{ .compatible = "hisilicon,mbigen-v2" },
289717c3dbcSMa Jun 	{ /* END */ }
290717c3dbcSMa Jun };
291717c3dbcSMa Jun MODULE_DEVICE_TABLE(of, mbigen_of_match);
292717c3dbcSMa Jun 
293717c3dbcSMa Jun static struct platform_driver mbigen_platform_driver = {
294717c3dbcSMa Jun 	.driver = {
295717c3dbcSMa Jun 		.name		= "Hisilicon MBIGEN-V2",
296717c3dbcSMa Jun 		.owner		= THIS_MODULE,
297717c3dbcSMa Jun 		.of_match_table	= mbigen_of_match,
298717c3dbcSMa Jun 	},
299717c3dbcSMa Jun 	.probe			= mbigen_device_probe,
300717c3dbcSMa Jun };
301717c3dbcSMa Jun 
302717c3dbcSMa Jun module_platform_driver(mbigen_platform_driver);
303717c3dbcSMa Jun 
304717c3dbcSMa Jun MODULE_AUTHOR("Jun Ma <majun258@huawei.com>");
305717c3dbcSMa Jun MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>");
306717c3dbcSMa Jun MODULE_LICENSE("GPL");
307717c3dbcSMa Jun MODULE_DESCRIPTION("Hisilicon MBI Generator driver");
308