Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2008 Freescale Semiconductor, Inc. All rights reserved.
0004  *
0005  * Author: John Rigby, <jrigby@freescale.com>
0006  *
0007  * Description:
0008  * MPC5121ADS CPLD irq handling
0009  */
0010 
0011 #undef DEBUG
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/irq.h>
0016 #include <linux/io.h>
0017 #include <linux/of_address.h>
0018 #include <linux/of_irq.h>
0019 
0020 static struct device_node *cpld_pic_node;
0021 static struct irq_domain *cpld_pic_host;
0022 
0023 /*
0024  * Bits to ignore in the misc_status register
0025  * 0x10 touch screen pendown is hard routed to irq1
0026  * 0x02 pci status is read from pci status register
0027  */
0028 #define MISC_IGNORE 0x12
0029 
0030 /*
0031  * Nothing to ignore in pci status register
0032  */
0033 #define PCI_IGNORE 0x00
0034 
0035 struct cpld_pic {
0036     u8 pci_mask;
0037     u8 pci_status;
0038     u8 route;
0039     u8 misc_mask;
0040     u8 misc_status;
0041     u8 misc_control;
0042 };
0043 
0044 static struct cpld_pic __iomem *cpld_regs;
0045 
0046 static void __iomem *
0047 irq_to_pic_mask(unsigned int irq)
0048 {
0049     return irq <= 7 ? &cpld_regs->pci_mask : &cpld_regs->misc_mask;
0050 }
0051 
0052 static unsigned int
0053 irq_to_pic_bit(unsigned int irq)
0054 {
0055     return 1 << (irq & 0x7);
0056 }
0057 
0058 static void
0059 cpld_mask_irq(struct irq_data *d)
0060 {
0061     unsigned int cpld_irq = (unsigned int)irqd_to_hwirq(d);
0062     void __iomem *pic_mask = irq_to_pic_mask(cpld_irq);
0063 
0064     out_8(pic_mask,
0065           in_8(pic_mask) | irq_to_pic_bit(cpld_irq));
0066 }
0067 
0068 static void
0069 cpld_unmask_irq(struct irq_data *d)
0070 {
0071     unsigned int cpld_irq = (unsigned int)irqd_to_hwirq(d);
0072     void __iomem *pic_mask = irq_to_pic_mask(cpld_irq);
0073 
0074     out_8(pic_mask,
0075           in_8(pic_mask) & ~irq_to_pic_bit(cpld_irq));
0076 }
0077 
0078 static struct irq_chip cpld_pic = {
0079     .name = "CPLD PIC",
0080     .irq_mask = cpld_mask_irq,
0081     .irq_ack = cpld_mask_irq,
0082     .irq_unmask = cpld_unmask_irq,
0083 };
0084 
0085 static unsigned int
0086 cpld_pic_get_irq(int offset, u8 ignore, u8 __iomem *statusp,
0087                 u8 __iomem *maskp)
0088 {
0089     u8 status = in_8(statusp);
0090     u8 mask = in_8(maskp);
0091 
0092     /* ignore don't cares and masked irqs */
0093     status |= (ignore | mask);
0094 
0095     if (status == 0xff)
0096         return ~0;
0097 
0098     return ffz(status) + offset;
0099 }
0100 
0101 static void cpld_pic_cascade(struct irq_desc *desc)
0102 {
0103     unsigned int hwirq;
0104 
0105     hwirq = cpld_pic_get_irq(0, PCI_IGNORE, &cpld_regs->pci_status,
0106         &cpld_regs->pci_mask);
0107     if (hwirq != ~0) {
0108         generic_handle_domain_irq(cpld_pic_host, hwirq);
0109         return;
0110     }
0111 
0112     hwirq = cpld_pic_get_irq(8, MISC_IGNORE, &cpld_regs->misc_status,
0113         &cpld_regs->misc_mask);
0114     if (hwirq != ~0) {
0115         generic_handle_domain_irq(cpld_pic_host, hwirq);
0116         return;
0117     }
0118 }
0119 
0120 static int
0121 cpld_pic_host_match(struct irq_domain *h, struct device_node *node,
0122             enum irq_domain_bus_token bus_token)
0123 {
0124     return cpld_pic_node == node;
0125 }
0126 
0127 static int
0128 cpld_pic_host_map(struct irq_domain *h, unsigned int virq,
0129                  irq_hw_number_t hw)
0130 {
0131     irq_set_status_flags(virq, IRQ_LEVEL);
0132     irq_set_chip_and_handler(virq, &cpld_pic, handle_level_irq);
0133     return 0;
0134 }
0135 
0136 static const struct irq_domain_ops cpld_pic_host_ops = {
0137     .match = cpld_pic_host_match,
0138     .map = cpld_pic_host_map,
0139 };
0140 
0141 void __init
0142 mpc5121_ads_cpld_map(void)
0143 {
0144     struct device_node *np = NULL;
0145 
0146     np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121ads-cpld-pic");
0147     if (!np) {
0148         printk(KERN_ERR "CPLD PIC init: can not find cpld-pic node\n");
0149         return;
0150     }
0151 
0152     cpld_regs = of_iomap(np, 0);
0153     of_node_put(np);
0154 }
0155 
0156 void __init
0157 mpc5121_ads_cpld_pic_init(void)
0158 {
0159     unsigned int cascade_irq;
0160     struct device_node *np = NULL;
0161 
0162     pr_debug("cpld_ic_init\n");
0163 
0164     np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121ads-cpld-pic");
0165     if (!np) {
0166         printk(KERN_ERR "CPLD PIC init: can not find cpld-pic node\n");
0167         return;
0168     }
0169 
0170     if (!cpld_regs)
0171         goto end;
0172 
0173     cascade_irq = irq_of_parse_and_map(np, 0);
0174     if (!cascade_irq)
0175         goto end;
0176 
0177     /*
0178      * statically route touch screen pendown through 1
0179      * and ignore it here
0180      * route all others through our cascade irq
0181      */
0182     out_8(&cpld_regs->route, 0xfd);
0183     out_8(&cpld_regs->pci_mask, 0xff);
0184     /* unmask pci ints in misc mask */
0185     out_8(&cpld_regs->misc_mask, ~(MISC_IGNORE));
0186 
0187     cpld_pic_node = of_node_get(np);
0188 
0189     cpld_pic_host = irq_domain_add_linear(np, 16, &cpld_pic_host_ops, NULL);
0190     if (!cpld_pic_host) {
0191         printk(KERN_ERR "CPLD PIC: failed to allocate irq host!\n");
0192         goto end;
0193     }
0194 
0195     irq_set_chained_handler(cascade_irq, cpld_pic_cascade);
0196 end:
0197     of_node_put(np);
0198 }