xref: /linux/drivers/irqchip/irq-mbigen.c (revision 9650c60ebfec05fcf74d9b3eb97837501f2bb541)
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 
19*9650c60eSMa Jun #include <linux/interrupt.h>
20*9650c60eSMa Jun #include <linux/irqchip.h>
21717c3dbcSMa Jun #include <linux/module.h>
22*9650c60eSMa 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 
29*9650c60eSMa Jun /* Interrupt numbers per mbigen node supported */
30*9650c60eSMa Jun #define IRQS_PER_MBIGEN_NODE		128
31*9650c60eSMa Jun 
32*9650c60eSMa Jun /* 64 irqs (Pin0-pin63) are reserved for each mbigen chip */
33*9650c60eSMa Jun #define RESERVED_IRQ_PER_MBIGEN_CHIP	64
34*9650c60eSMa Jun 
35*9650c60eSMa Jun /* The maximum IRQ pin number of mbigen chip(start from 0) */
36*9650c60eSMa Jun #define MAXIMUM_IRQ_PIN_NUM		1407
37*9650c60eSMa Jun 
38*9650c60eSMa Jun /**
39*9650c60eSMa Jun  * In mbigen vector register
40*9650c60eSMa Jun  * bit[21:12]:	event id value
41*9650c60eSMa Jun  * bit[11:0]:	device id
42*9650c60eSMa Jun  */
43*9650c60eSMa Jun #define IRQ_EVENT_ID_SHIFT		12
44*9650c60eSMa Jun #define IRQ_EVENT_ID_MASK		0x3ff
45*9650c60eSMa Jun 
46*9650c60eSMa Jun /* register range of each mbigen node */
47*9650c60eSMa Jun #define MBIGEN_NODE_OFFSET		0x1000
48*9650c60eSMa Jun 
49*9650c60eSMa Jun /* offset of vector register in mbigen node */
50*9650c60eSMa Jun #define REG_MBIGEN_VEC_OFFSET		0x200
51*9650c60eSMa Jun 
52717c3dbcSMa Jun /**
53717c3dbcSMa Jun  * struct mbigen_device - holds the information of mbigen device.
54717c3dbcSMa Jun  *
55717c3dbcSMa Jun  * @pdev:		pointer to the platform device structure of mbigen chip.
56717c3dbcSMa Jun  * @base:		mapped address of this mbigen chip.
57717c3dbcSMa Jun  */
58717c3dbcSMa Jun struct mbigen_device {
59717c3dbcSMa Jun 	struct platform_device	*pdev;
60717c3dbcSMa Jun 	void __iomem		*base;
61717c3dbcSMa Jun };
62717c3dbcSMa Jun 
63*9650c60eSMa Jun static inline unsigned int get_mbigen_vec_reg(irq_hw_number_t hwirq)
64*9650c60eSMa Jun {
65*9650c60eSMa Jun 	unsigned int nid, pin;
66*9650c60eSMa Jun 
67*9650c60eSMa Jun 	hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
68*9650c60eSMa Jun 	nid = hwirq / IRQS_PER_MBIGEN_NODE + 1;
69*9650c60eSMa Jun 	pin = hwirq % IRQS_PER_MBIGEN_NODE;
70*9650c60eSMa Jun 
71*9650c60eSMa Jun 	return pin * 4 + nid * MBIGEN_NODE_OFFSET
72*9650c60eSMa Jun 			+ REG_MBIGEN_VEC_OFFSET;
73*9650c60eSMa Jun }
74*9650c60eSMa Jun 
75*9650c60eSMa Jun static struct irq_chip mbigen_irq_chip = {
76*9650c60eSMa Jun 	.name =			"mbigen-v2",
77*9650c60eSMa Jun };
78*9650c60eSMa Jun 
79*9650c60eSMa Jun static void mbigen_write_msg(struct msi_desc *desc, struct msi_msg *msg)
80*9650c60eSMa Jun {
81*9650c60eSMa Jun 	struct irq_data *d = irq_get_irq_data(desc->irq);
82*9650c60eSMa Jun 	void __iomem *base = d->chip_data;
83*9650c60eSMa Jun 	u32 val;
84*9650c60eSMa Jun 
85*9650c60eSMa Jun 	base += get_mbigen_vec_reg(d->hwirq);
86*9650c60eSMa Jun 	val = readl_relaxed(base);
87*9650c60eSMa Jun 
88*9650c60eSMa Jun 	val &= ~(IRQ_EVENT_ID_MASK << IRQ_EVENT_ID_SHIFT);
89*9650c60eSMa Jun 	val |= (msg->data << IRQ_EVENT_ID_SHIFT);
90*9650c60eSMa Jun 
91*9650c60eSMa Jun 	/* The address of doorbell is encoded in mbigen register by default
92*9650c60eSMa Jun 	 * So,we don't need to program the doorbell address at here
93*9650c60eSMa Jun 	 */
94*9650c60eSMa Jun 	writel_relaxed(val, base);
95*9650c60eSMa Jun }
96*9650c60eSMa Jun 
97*9650c60eSMa Jun static int mbigen_domain_translate(struct irq_domain *d,
98*9650c60eSMa Jun 				    struct irq_fwspec *fwspec,
99*9650c60eSMa Jun 				    unsigned long *hwirq,
100*9650c60eSMa Jun 				    unsigned int *type)
101*9650c60eSMa Jun {
102*9650c60eSMa Jun 	if (is_of_node(fwspec->fwnode)) {
103*9650c60eSMa Jun 		if (fwspec->param_count != 2)
104*9650c60eSMa Jun 			return -EINVAL;
105*9650c60eSMa Jun 
106*9650c60eSMa Jun 		if ((fwspec->param[0] > MAXIMUM_IRQ_PIN_NUM) ||
107*9650c60eSMa Jun 			(fwspec->param[0] < RESERVED_IRQ_PER_MBIGEN_CHIP))
108*9650c60eSMa Jun 			return -EINVAL;
109*9650c60eSMa Jun 		else
110*9650c60eSMa Jun 			*hwirq = fwspec->param[0];
111*9650c60eSMa Jun 
112*9650c60eSMa Jun 		/* If there is no valid irq type, just use the default type */
113*9650c60eSMa Jun 		if ((fwspec->param[1] == IRQ_TYPE_EDGE_RISING) ||
114*9650c60eSMa Jun 			(fwspec->param[1] == IRQ_TYPE_LEVEL_HIGH))
115*9650c60eSMa Jun 			*type = fwspec->param[1];
116*9650c60eSMa Jun 		else
117*9650c60eSMa Jun 			return -EINVAL;
118*9650c60eSMa Jun 
119*9650c60eSMa Jun 		return 0;
120*9650c60eSMa Jun 	}
121*9650c60eSMa Jun 	return -EINVAL;
122*9650c60eSMa Jun }
123*9650c60eSMa Jun 
124*9650c60eSMa Jun static int mbigen_irq_domain_alloc(struct irq_domain *domain,
125*9650c60eSMa Jun 					unsigned int virq,
126*9650c60eSMa Jun 					unsigned int nr_irqs,
127*9650c60eSMa Jun 					void *args)
128*9650c60eSMa Jun {
129*9650c60eSMa Jun 	struct irq_fwspec *fwspec = args;
130*9650c60eSMa Jun 	irq_hw_number_t hwirq;
131*9650c60eSMa Jun 	unsigned int type;
132*9650c60eSMa Jun 	struct mbigen_device *mgn_chip;
133*9650c60eSMa Jun 	int i, err;
134*9650c60eSMa Jun 
135*9650c60eSMa Jun 	err = mbigen_domain_translate(domain, fwspec, &hwirq, &type);
136*9650c60eSMa Jun 	if (err)
137*9650c60eSMa Jun 		return err;
138*9650c60eSMa Jun 
139*9650c60eSMa Jun 	err = platform_msi_domain_alloc(domain, virq, nr_irqs);
140*9650c60eSMa Jun 	if (err)
141*9650c60eSMa Jun 		return err;
142*9650c60eSMa Jun 
143*9650c60eSMa Jun 	mgn_chip = platform_msi_get_host_data(domain);
144*9650c60eSMa Jun 
145*9650c60eSMa Jun 	for (i = 0; i < nr_irqs; i++)
146*9650c60eSMa Jun 		irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
147*9650c60eSMa Jun 				      &mbigen_irq_chip, mgn_chip->base);
148*9650c60eSMa Jun 
149*9650c60eSMa Jun 	return 0;
150*9650c60eSMa Jun }
151*9650c60eSMa Jun 
152*9650c60eSMa Jun static struct irq_domain_ops mbigen_domain_ops = {
153*9650c60eSMa Jun 	.translate	= mbigen_domain_translate,
154*9650c60eSMa Jun 	.alloc		= mbigen_irq_domain_alloc,
155*9650c60eSMa Jun 	.free		= irq_domain_free_irqs_common,
156*9650c60eSMa Jun };
157*9650c60eSMa Jun 
158717c3dbcSMa Jun static int mbigen_device_probe(struct platform_device *pdev)
159717c3dbcSMa Jun {
160717c3dbcSMa Jun 	struct mbigen_device *mgn_chip;
161717c3dbcSMa Jun 	struct resource *res;
162*9650c60eSMa Jun 	struct irq_domain *domain;
163*9650c60eSMa Jun 	u32 num_pins;
164717c3dbcSMa Jun 
165717c3dbcSMa Jun 	mgn_chip = devm_kzalloc(&pdev->dev, sizeof(*mgn_chip), GFP_KERNEL);
166717c3dbcSMa Jun 	if (!mgn_chip)
167717c3dbcSMa Jun 		return -ENOMEM;
168717c3dbcSMa Jun 
169717c3dbcSMa Jun 	mgn_chip->pdev = pdev;
170717c3dbcSMa Jun 
171717c3dbcSMa Jun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
172717c3dbcSMa Jun 	mgn_chip->base = devm_ioremap_resource(&pdev->dev, res);
173717c3dbcSMa Jun 	if (IS_ERR(mgn_chip->base))
174717c3dbcSMa Jun 		return PTR_ERR(mgn_chip->base);
175717c3dbcSMa Jun 
176*9650c60eSMa Jun 	if (of_property_read_u32(pdev->dev.of_node, "num-pins", &num_pins) < 0) {
177*9650c60eSMa Jun 		dev_err(&pdev->dev, "No num-pins property\n");
178*9650c60eSMa Jun 		return -EINVAL;
179*9650c60eSMa Jun 	}
180*9650c60eSMa Jun 
181*9650c60eSMa Jun 	domain = platform_msi_create_device_domain(&pdev->dev, num_pins,
182*9650c60eSMa Jun 							mbigen_write_msg,
183*9650c60eSMa Jun 							&mbigen_domain_ops,
184*9650c60eSMa Jun 							mgn_chip);
185*9650c60eSMa Jun 
186*9650c60eSMa Jun 	if (!domain)
187*9650c60eSMa Jun 		return -ENOMEM;
188*9650c60eSMa Jun 
189717c3dbcSMa Jun 	platform_set_drvdata(pdev, mgn_chip);
190717c3dbcSMa Jun 
191*9650c60eSMa Jun 	dev_info(&pdev->dev, "Allocated %d MSIs\n", num_pins);
192*9650c60eSMa Jun 
193717c3dbcSMa Jun 	return 0;
194717c3dbcSMa Jun }
195717c3dbcSMa Jun 
196717c3dbcSMa Jun static const struct of_device_id mbigen_of_match[] = {
197717c3dbcSMa Jun 	{ .compatible = "hisilicon,mbigen-v2" },
198717c3dbcSMa Jun 	{ /* END */ }
199717c3dbcSMa Jun };
200717c3dbcSMa Jun MODULE_DEVICE_TABLE(of, mbigen_of_match);
201717c3dbcSMa Jun 
202717c3dbcSMa Jun static struct platform_driver mbigen_platform_driver = {
203717c3dbcSMa Jun 	.driver = {
204717c3dbcSMa Jun 		.name		= "Hisilicon MBIGEN-V2",
205717c3dbcSMa Jun 		.owner		= THIS_MODULE,
206717c3dbcSMa Jun 		.of_match_table	= mbigen_of_match,
207717c3dbcSMa Jun 	},
208717c3dbcSMa Jun 	.probe			= mbigen_device_probe,
209717c3dbcSMa Jun };
210717c3dbcSMa Jun 
211717c3dbcSMa Jun module_platform_driver(mbigen_platform_driver);
212717c3dbcSMa Jun 
213717c3dbcSMa Jun MODULE_AUTHOR("Jun Ma <majun258@huawei.com>");
214717c3dbcSMa Jun MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>");
215717c3dbcSMa Jun MODULE_LICENSE("GPL");
216717c3dbcSMa Jun MODULE_DESCRIPTION("Hisilicon MBI Generator driver");
217