1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2008 Freescale Semiconductor, Inc. All rights reserved.
5 * Author: John Rigby, <jrigby@freescale.com>
8 * MPC5121ADS CPLD irq handling
13 #include <linux/kernel.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
20 static struct device_node *cpld_pic_node;
21 static struct irq_domain *cpld_pic_host;
24 * Bits to ignore in the misc_status register
25 * 0x10 touch screen pendown is hard routed to irq1
26 * 0x02 pci status is read from pci status register
28 #define MISC_IGNORE 0x12
31 * Nothing to ignore in pci status register
33 #define PCI_IGNORE 0x00
44 static struct cpld_pic __iomem *cpld_regs;
47 irq_to_pic_mask(unsigned int irq)
49 return irq <= 7 ? &cpld_regs->pci_mask : &cpld_regs->misc_mask;
53 irq_to_pic_bit(unsigned int irq)
55 return 1 << (irq & 0x7);
59 cpld_mask_irq(struct irq_data *d)
61 unsigned int cpld_irq = (unsigned int)irqd_to_hwirq(d);
62 void __iomem *pic_mask = irq_to_pic_mask(cpld_irq);
65 in_8(pic_mask) | irq_to_pic_bit(cpld_irq));
69 cpld_unmask_irq(struct irq_data *d)
71 unsigned int cpld_irq = (unsigned int)irqd_to_hwirq(d);
72 void __iomem *pic_mask = irq_to_pic_mask(cpld_irq);
75 in_8(pic_mask) & ~irq_to_pic_bit(cpld_irq));
78 static struct irq_chip cpld_pic = {
80 .irq_mask = cpld_mask_irq,
81 .irq_ack = cpld_mask_irq,
82 .irq_unmask = cpld_unmask_irq,
86 cpld_pic_get_irq(int offset, u8 ignore, u8 __iomem *statusp,
89 u8 status = in_8(statusp);
90 u8 mask = in_8(maskp);
92 /* ignore don't cares and masked irqs */
93 status |= (ignore | mask);
98 return ffz(status) + offset;
101 static void cpld_pic_cascade(struct irq_desc *desc)
105 hwirq = cpld_pic_get_irq(0, PCI_IGNORE, &cpld_regs->pci_status,
106 &cpld_regs->pci_mask);
108 generic_handle_domain_irq(cpld_pic_host, hwirq);
112 hwirq = cpld_pic_get_irq(8, MISC_IGNORE, &cpld_regs->misc_status,
113 &cpld_regs->misc_mask);
115 generic_handle_domain_irq(cpld_pic_host, hwirq);
121 cpld_pic_host_match(struct irq_domain *h, struct device_node *node,
122 enum irq_domain_bus_token bus_token)
124 return cpld_pic_node == node;
128 cpld_pic_host_map(struct irq_domain *h, unsigned int virq,
131 irq_set_status_flags(virq, IRQ_LEVEL);
132 irq_set_chip_and_handler(virq, &cpld_pic, handle_level_irq);
136 static const struct irq_domain_ops cpld_pic_host_ops = {
137 .match = cpld_pic_host_match,
138 .map = cpld_pic_host_map,
142 mpc5121_ads_cpld_map(void)
144 struct device_node *np = NULL;
146 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121ads-cpld-pic");
148 printk(KERN_ERR "CPLD PIC init: can not find cpld-pic node\n");
152 cpld_regs = of_iomap(np, 0);
157 mpc5121_ads_cpld_pic_init(void)
159 unsigned int cascade_irq;
160 struct device_node *np = NULL;
162 pr_debug("cpld_ic_init\n");
164 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121ads-cpld-pic");
166 printk(KERN_ERR "CPLD PIC init: can not find cpld-pic node\n");
173 cascade_irq = irq_of_parse_and_map(np, 0);
178 * statically route touch screen pendown through 1
180 * route all others through our cascade irq
182 out_8(&cpld_regs->route, 0xfd);
183 out_8(&cpld_regs->pci_mask, 0xff);
184 /* unmask pci ints in misc mask */
185 out_8(&cpld_regs->misc_mask, ~(MISC_IGNORE));
187 cpld_pic_node = of_node_get(np);
189 cpld_pic_host = irq_domain_add_linear(np, 16, &cpld_pic_host_ops, NULL);
190 if (!cpld_pic_host) {
191 printk(KERN_ERR "CPLD PIC: failed to allocate irq host!\n");
195 irq_set_chained_handler(cascade_irq, cpld_pic_cascade);