1acf9e575SChristophe Leroy // SPDX-License-Identifier: GPL-2.0
2acf9e575SChristophe Leroy /*
3acf9e575SChristophe Leroy  * Interrupt controller for the
4acf9e575SChristophe Leroy  * Communication Processor Module.
5acf9e575SChristophe Leroy  * Copyright (c) 1997 Dan error_act (dmalek@jlc.net)
6acf9e575SChristophe Leroy  */
7acf9e575SChristophe Leroy #include <linux/kernel.h>
8acf9e575SChristophe Leroy #include <linux/interrupt.h>
9acf9e575SChristophe Leroy #include <linux/irqdomain.h>
1022add2a2SChristophe Leroy #include <linux/platform_device.h>
11acf9e575SChristophe Leroy #include <asm/cpm1.h>
12acf9e575SChristophe Leroy 
13*5ad1aa00SChristophe Leroy struct cpm_pic_data {
14*5ad1aa00SChristophe Leroy 	cpic8xx_t __iomem *reg;
15*5ad1aa00SChristophe Leroy 	struct irq_domain *host;
16*5ad1aa00SChristophe Leroy };
17acf9e575SChristophe Leroy 
cpm_mask_irq(struct irq_data * d)18acf9e575SChristophe Leroy static void cpm_mask_irq(struct irq_data *d)
19acf9e575SChristophe Leroy {
20*5ad1aa00SChristophe Leroy 	struct cpm_pic_data *data = irq_data_get_irq_chip_data(d);
21acf9e575SChristophe Leroy 	unsigned int cpm_vec = (unsigned int)irqd_to_hwirq(d);
22acf9e575SChristophe Leroy 
23*5ad1aa00SChristophe Leroy 	clrbits32(&data->reg->cpic_cimr, (1 << cpm_vec));
24acf9e575SChristophe Leroy }
25acf9e575SChristophe Leroy 
cpm_unmask_irq(struct irq_data * d)26acf9e575SChristophe Leroy static void cpm_unmask_irq(struct irq_data *d)
27acf9e575SChristophe Leroy {
28*5ad1aa00SChristophe Leroy 	struct cpm_pic_data *data = irq_data_get_irq_chip_data(d);
29acf9e575SChristophe Leroy 	unsigned int cpm_vec = (unsigned int)irqd_to_hwirq(d);
30acf9e575SChristophe Leroy 
31*5ad1aa00SChristophe Leroy 	setbits32(&data->reg->cpic_cimr, (1 << cpm_vec));
32acf9e575SChristophe Leroy }
33acf9e575SChristophe Leroy 
cpm_end_irq(struct irq_data * d)34acf9e575SChristophe Leroy static void cpm_end_irq(struct irq_data *d)
35acf9e575SChristophe Leroy {
36*5ad1aa00SChristophe Leroy 	struct cpm_pic_data *data = irq_data_get_irq_chip_data(d);
37acf9e575SChristophe Leroy 	unsigned int cpm_vec = (unsigned int)irqd_to_hwirq(d);
38acf9e575SChristophe Leroy 
39*5ad1aa00SChristophe Leroy 	out_be32(&data->reg->cpic_cisr, (1 << cpm_vec));
40acf9e575SChristophe Leroy }
41acf9e575SChristophe Leroy 
42acf9e575SChristophe Leroy static struct irq_chip cpm_pic = {
43acf9e575SChristophe Leroy 	.name = "CPM PIC",
44acf9e575SChristophe Leroy 	.irq_mask = cpm_mask_irq,
45acf9e575SChristophe Leroy 	.irq_unmask = cpm_unmask_irq,
46acf9e575SChristophe Leroy 	.irq_eoi = cpm_end_irq,
47acf9e575SChristophe Leroy };
48acf9e575SChristophe Leroy 
cpm_get_irq(struct irq_desc * desc)49*5ad1aa00SChristophe Leroy static int cpm_get_irq(struct irq_desc *desc)
50acf9e575SChristophe Leroy {
51*5ad1aa00SChristophe Leroy 	struct cpm_pic_data *data = irq_desc_get_handler_data(desc);
52acf9e575SChristophe Leroy 	int cpm_vec;
53acf9e575SChristophe Leroy 
54acf9e575SChristophe Leroy 	/*
55acf9e575SChristophe Leroy 	 * Get the vector by setting the ACK bit and then reading
56acf9e575SChristophe Leroy 	 * the register.
57acf9e575SChristophe Leroy 	 */
58*5ad1aa00SChristophe Leroy 	out_be16(&data->reg->cpic_civr, 1);
59*5ad1aa00SChristophe Leroy 	cpm_vec = in_be16(&data->reg->cpic_civr);
60acf9e575SChristophe Leroy 	cpm_vec >>= 11;
61acf9e575SChristophe Leroy 
62*5ad1aa00SChristophe Leroy 	return irq_linear_revmap(data->host, cpm_vec);
63acf9e575SChristophe Leroy }
64acf9e575SChristophe Leroy 
cpm_cascade(struct irq_desc * desc)6514d893fcSChristophe Leroy static void cpm_cascade(struct irq_desc *desc)
6614d893fcSChristophe Leroy {
67*5ad1aa00SChristophe Leroy 	generic_handle_irq(cpm_get_irq(desc));
6814d893fcSChristophe Leroy }
6914d893fcSChristophe Leroy 
cpm_pic_host_map(struct irq_domain * h,unsigned int virq,irq_hw_number_t hw)70acf9e575SChristophe Leroy static int cpm_pic_host_map(struct irq_domain *h, unsigned int virq,
71acf9e575SChristophe Leroy 			    irq_hw_number_t hw)
72acf9e575SChristophe Leroy {
73*5ad1aa00SChristophe Leroy 	irq_set_chip_data(virq, h->host_data);
74acf9e575SChristophe Leroy 	irq_set_status_flags(virq, IRQ_LEVEL);
75acf9e575SChristophe Leroy 	irq_set_chip_and_handler(virq, &cpm_pic, handle_fasteoi_irq);
76acf9e575SChristophe Leroy 	return 0;
77acf9e575SChristophe Leroy }
78acf9e575SChristophe Leroy 
79acf9e575SChristophe Leroy static const struct irq_domain_ops cpm_pic_host_ops = {
80acf9e575SChristophe Leroy 	.map = cpm_pic_host_map,
81acf9e575SChristophe Leroy };
82acf9e575SChristophe Leroy 
cpm_pic_probe(struct platform_device * pdev)8314d893fcSChristophe Leroy static int cpm_pic_probe(struct platform_device *pdev)
84acf9e575SChristophe Leroy {
8514d893fcSChristophe Leroy 	struct device *dev = &pdev->dev;
8614d893fcSChristophe Leroy 	struct resource *res;
8714d893fcSChristophe Leroy 	int irq;
88*5ad1aa00SChristophe Leroy 	struct cpm_pic_data *data;
89acf9e575SChristophe Leroy 
9014d893fcSChristophe Leroy 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9114d893fcSChristophe Leroy 	if (!res)
9214d893fcSChristophe Leroy 		return -ENODEV;
93acf9e575SChristophe Leroy 
94*5ad1aa00SChristophe Leroy 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
95*5ad1aa00SChristophe Leroy 	if (!data)
96*5ad1aa00SChristophe Leroy 		return -ENOMEM;
97*5ad1aa00SChristophe Leroy 
98*5ad1aa00SChristophe Leroy 	data->reg = devm_ioremap(dev, res->start, resource_size(res));
99*5ad1aa00SChristophe Leroy 	if (!data->reg)
10014d893fcSChristophe Leroy 		return -ENODEV;
101acf9e575SChristophe Leroy 
10214d893fcSChristophe Leroy 	irq = platform_get_irq(pdev, 0);
10314d893fcSChristophe Leroy 	if (irq < 0)
10414d893fcSChristophe Leroy 		return irq;
105acf9e575SChristophe Leroy 
106acf9e575SChristophe Leroy 	/* Initialize the CPM interrupt controller. */
107*5ad1aa00SChristophe Leroy 	out_be32(&data->reg->cpic_cicr,
108acf9e575SChristophe Leroy 		 (CICR_SCD_SCC4 | CICR_SCC_SCC3 | CICR_SCB_SCC2 | CICR_SCA_SCC1) |
10914d893fcSChristophe Leroy 		 ((virq_to_hw(irq) / 2) << 13) | CICR_HP_MASK);
110acf9e575SChristophe Leroy 
111*5ad1aa00SChristophe Leroy 	out_be32(&data->reg->cpic_cimr, 0);
112acf9e575SChristophe Leroy 
113*5ad1aa00SChristophe Leroy 	data->host = irq_domain_add_linear(dev->of_node, 64, &cpm_pic_host_ops, data);
114*5ad1aa00SChristophe Leroy 	if (!data->host)
11514d893fcSChristophe Leroy 		return -ENODEV;
11614d893fcSChristophe Leroy 
117*5ad1aa00SChristophe Leroy 	irq_set_handler_data(irq, data);
11814d893fcSChristophe Leroy 	irq_set_chained_handler(irq, cpm_cascade);
119acf9e575SChristophe Leroy 
120*5ad1aa00SChristophe Leroy 	setbits32(&data->reg->cpic_cicr, CICR_IEN);
121acf9e575SChristophe Leroy 
12214d893fcSChristophe Leroy 	return 0;
123acf9e575SChristophe Leroy }
12422add2a2SChristophe Leroy 
12514d893fcSChristophe Leroy static const struct of_device_id cpm_pic_match[] = {
12614d893fcSChristophe Leroy 	{
12714d893fcSChristophe Leroy 		.compatible = "fsl,cpm1-pic",
12814d893fcSChristophe Leroy 	}, {
12914d893fcSChristophe Leroy 		.type = "cpm-pic",
13014d893fcSChristophe Leroy 		.compatible = "CPM",
13114d893fcSChristophe Leroy 	}, {},
13214d893fcSChristophe Leroy };
13314d893fcSChristophe Leroy 
13414d893fcSChristophe Leroy static struct platform_driver cpm_pic_driver = {
13514d893fcSChristophe Leroy 	.driver	= {
13614d893fcSChristophe Leroy 		.name		= "cpm-pic",
13714d893fcSChristophe Leroy 		.of_match_table	= cpm_pic_match,
13814d893fcSChristophe Leroy 	},
13914d893fcSChristophe Leroy 	.probe	= cpm_pic_probe,
14014d893fcSChristophe Leroy };
14114d893fcSChristophe Leroy 
cpm_pic_init(void)14214d893fcSChristophe Leroy static int __init cpm_pic_init(void)
14314d893fcSChristophe Leroy {
14414d893fcSChristophe Leroy 	return platform_driver_register(&cpm_pic_driver);
14514d893fcSChristophe Leroy }
14614d893fcSChristophe Leroy arch_initcall(cpm_pic_init);
14714d893fcSChristophe Leroy 
14822add2a2SChristophe Leroy /*
14922add2a2SChristophe Leroy  * The CPM can generate the error interrupt when there is a race condition
15022add2a2SChristophe Leroy  * between generating and masking interrupts.  All we have to do is ACK it
15122add2a2SChristophe Leroy  * and return.  This is a no-op function so we don't need any special
15222add2a2SChristophe Leroy  * tests in the interrupt handler.
15322add2a2SChristophe Leroy  */
cpm_error_interrupt(int irq,void * dev)15422add2a2SChristophe Leroy static irqreturn_t cpm_error_interrupt(int irq, void *dev)
15522add2a2SChristophe Leroy {
15622add2a2SChristophe Leroy 	return IRQ_HANDLED;
15722add2a2SChristophe Leroy }
15822add2a2SChristophe Leroy 
cpm_error_probe(struct platform_device * pdev)15922add2a2SChristophe Leroy static int cpm_error_probe(struct platform_device *pdev)
16022add2a2SChristophe Leroy {
16122add2a2SChristophe Leroy 	int irq;
16222add2a2SChristophe Leroy 
16322add2a2SChristophe Leroy 	irq = platform_get_irq(pdev, 0);
16422add2a2SChristophe Leroy 	if (irq < 0)
16522add2a2SChristophe Leroy 		return irq;
16622add2a2SChristophe Leroy 
16722add2a2SChristophe Leroy 	return request_irq(irq, cpm_error_interrupt, IRQF_NO_THREAD, "error", NULL);
16822add2a2SChristophe Leroy }
16922add2a2SChristophe Leroy 
17022add2a2SChristophe Leroy static const struct of_device_id cpm_error_ids[] = {
17122add2a2SChristophe Leroy 	{ .compatible = "fsl,cpm1" },
17222add2a2SChristophe Leroy 	{ .type = "cpm" },
17322add2a2SChristophe Leroy 	{},
17422add2a2SChristophe Leroy };
17522add2a2SChristophe Leroy 
17622add2a2SChristophe Leroy static struct platform_driver cpm_error_driver = {
17722add2a2SChristophe Leroy 	.driver	= {
17822add2a2SChristophe Leroy 		.name		= "cpm-error",
17922add2a2SChristophe Leroy 		.of_match_table	= cpm_error_ids,
18022add2a2SChristophe Leroy 	},
18122add2a2SChristophe Leroy 	.probe	= cpm_error_probe,
18222add2a2SChristophe Leroy };
18322add2a2SChristophe Leroy 
cpm_error_init(void)18422add2a2SChristophe Leroy static int __init cpm_error_init(void)
18522add2a2SChristophe Leroy {
18622add2a2SChristophe Leroy 	return platform_driver_register(&cpm_error_driver);
18722add2a2SChristophe Leroy }
18822add2a2SChristophe Leroy subsys_initcall(cpm_error_init);
189