18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright 2015-2016 Vladimir Zapolskiy <vz@mleia.com> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "%s: " fmt, __func__ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/io.h> 98c2ecf20Sopenharmony_ci#include <linux/irqchip.h> 108c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h> 118c2ecf20Sopenharmony_ci#include <linux/of_address.h> 128c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 138c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#include <asm/exception.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define LPC32XX_INTC_MASK 0x00 188c2ecf20Sopenharmony_ci#define LPC32XX_INTC_RAW 0x04 198c2ecf20Sopenharmony_ci#define LPC32XX_INTC_STAT 0x08 208c2ecf20Sopenharmony_ci#define LPC32XX_INTC_POL 0x0C 218c2ecf20Sopenharmony_ci#define LPC32XX_INTC_TYPE 0x10 228c2ecf20Sopenharmony_ci#define LPC32XX_INTC_FIQ 0x14 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define NR_LPC32XX_IC_IRQS 32 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistruct lpc32xx_irq_chip { 278c2ecf20Sopenharmony_ci void __iomem *base; 288c2ecf20Sopenharmony_ci struct irq_domain *domain; 298c2ecf20Sopenharmony_ci struct irq_chip chip; 308c2ecf20Sopenharmony_ci}; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistatic struct lpc32xx_irq_chip *lpc32xx_mic_irqc; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic inline u32 lpc32xx_ic_read(struct lpc32xx_irq_chip *ic, u32 reg) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci return readl_relaxed(ic->base + reg); 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic inline void lpc32xx_ic_write(struct lpc32xx_irq_chip *ic, 408c2ecf20Sopenharmony_ci u32 reg, u32 val) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci writel_relaxed(val, ic->base + reg); 438c2ecf20Sopenharmony_ci} 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistatic void lpc32xx_irq_mask(struct irq_data *d) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci struct lpc32xx_irq_chip *ic = irq_data_get_irq_chip_data(d); 488c2ecf20Sopenharmony_ci u32 val, mask = BIT(d->hwirq); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci val = lpc32xx_ic_read(ic, LPC32XX_INTC_MASK) & ~mask; 518c2ecf20Sopenharmony_ci lpc32xx_ic_write(ic, LPC32XX_INTC_MASK, val); 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistatic void lpc32xx_irq_unmask(struct irq_data *d) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct lpc32xx_irq_chip *ic = irq_data_get_irq_chip_data(d); 578c2ecf20Sopenharmony_ci u32 val, mask = BIT(d->hwirq); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci val = lpc32xx_ic_read(ic, LPC32XX_INTC_MASK) | mask; 608c2ecf20Sopenharmony_ci lpc32xx_ic_write(ic, LPC32XX_INTC_MASK, val); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic void lpc32xx_irq_ack(struct irq_data *d) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci struct lpc32xx_irq_chip *ic = irq_data_get_irq_chip_data(d); 668c2ecf20Sopenharmony_ci u32 mask = BIT(d->hwirq); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci lpc32xx_ic_write(ic, LPC32XX_INTC_RAW, mask); 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic int lpc32xx_irq_set_type(struct irq_data *d, unsigned int type) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci struct lpc32xx_irq_chip *ic = irq_data_get_irq_chip_data(d); 748c2ecf20Sopenharmony_ci u32 val, mask = BIT(d->hwirq); 758c2ecf20Sopenharmony_ci bool high, edge; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci switch (type) { 788c2ecf20Sopenharmony_ci case IRQ_TYPE_EDGE_RISING: 798c2ecf20Sopenharmony_ci edge = true; 808c2ecf20Sopenharmony_ci high = true; 818c2ecf20Sopenharmony_ci break; 828c2ecf20Sopenharmony_ci case IRQ_TYPE_EDGE_FALLING: 838c2ecf20Sopenharmony_ci edge = true; 848c2ecf20Sopenharmony_ci high = false; 858c2ecf20Sopenharmony_ci break; 868c2ecf20Sopenharmony_ci case IRQ_TYPE_LEVEL_HIGH: 878c2ecf20Sopenharmony_ci edge = false; 888c2ecf20Sopenharmony_ci high = true; 898c2ecf20Sopenharmony_ci break; 908c2ecf20Sopenharmony_ci case IRQ_TYPE_LEVEL_LOW: 918c2ecf20Sopenharmony_ci edge = false; 928c2ecf20Sopenharmony_ci high = false; 938c2ecf20Sopenharmony_ci break; 948c2ecf20Sopenharmony_ci default: 958c2ecf20Sopenharmony_ci pr_info("unsupported irq type %d\n", type); 968c2ecf20Sopenharmony_ci return -EINVAL; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci irqd_set_trigger_type(d, type); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci val = lpc32xx_ic_read(ic, LPC32XX_INTC_POL); 1028c2ecf20Sopenharmony_ci if (high) 1038c2ecf20Sopenharmony_ci val |= mask; 1048c2ecf20Sopenharmony_ci else 1058c2ecf20Sopenharmony_ci val &= ~mask; 1068c2ecf20Sopenharmony_ci lpc32xx_ic_write(ic, LPC32XX_INTC_POL, val); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci val = lpc32xx_ic_read(ic, LPC32XX_INTC_TYPE); 1098c2ecf20Sopenharmony_ci if (edge) { 1108c2ecf20Sopenharmony_ci val |= mask; 1118c2ecf20Sopenharmony_ci irq_set_handler_locked(d, handle_edge_irq); 1128c2ecf20Sopenharmony_ci } else { 1138c2ecf20Sopenharmony_ci val &= ~mask; 1148c2ecf20Sopenharmony_ci irq_set_handler_locked(d, handle_level_irq); 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci lpc32xx_ic_write(ic, LPC32XX_INTC_TYPE, val); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci return 0; 1198c2ecf20Sopenharmony_ci} 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistatic void __exception_irq_entry lpc32xx_handle_irq(struct pt_regs *regs) 1228c2ecf20Sopenharmony_ci{ 1238c2ecf20Sopenharmony_ci struct lpc32xx_irq_chip *ic = lpc32xx_mic_irqc; 1248c2ecf20Sopenharmony_ci u32 hwirq = lpc32xx_ic_read(ic, LPC32XX_INTC_STAT), irq; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci while (hwirq) { 1278c2ecf20Sopenharmony_ci irq = __ffs(hwirq); 1288c2ecf20Sopenharmony_ci hwirq &= ~BIT(irq); 1298c2ecf20Sopenharmony_ci handle_domain_irq(lpc32xx_mic_irqc->domain, irq, regs); 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic void lpc32xx_sic_handler(struct irq_desc *desc) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci struct lpc32xx_irq_chip *ic = irq_desc_get_handler_data(desc); 1368c2ecf20Sopenharmony_ci struct irq_chip *chip = irq_desc_get_chip(desc); 1378c2ecf20Sopenharmony_ci u32 hwirq = lpc32xx_ic_read(ic, LPC32XX_INTC_STAT), irq; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci chained_irq_enter(chip, desc); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci while (hwirq) { 1428c2ecf20Sopenharmony_ci irq = __ffs(hwirq); 1438c2ecf20Sopenharmony_ci hwirq &= ~BIT(irq); 1448c2ecf20Sopenharmony_ci generic_handle_irq(irq_find_mapping(ic->domain, irq)); 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci chained_irq_exit(chip, desc); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic int lpc32xx_irq_domain_map(struct irq_domain *id, unsigned int virq, 1518c2ecf20Sopenharmony_ci irq_hw_number_t hw) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci struct lpc32xx_irq_chip *ic = id->host_data; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci irq_set_chip_data(virq, ic); 1568c2ecf20Sopenharmony_ci irq_set_chip_and_handler(virq, &ic->chip, handle_level_irq); 1578c2ecf20Sopenharmony_ci irq_set_status_flags(virq, IRQ_LEVEL); 1588c2ecf20Sopenharmony_ci irq_set_noprobe(virq); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci return 0; 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic void lpc32xx_irq_domain_unmap(struct irq_domain *id, unsigned int virq) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci irq_set_chip_and_handler(virq, NULL, NULL); 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic const struct irq_domain_ops lpc32xx_irq_domain_ops = { 1698c2ecf20Sopenharmony_ci .map = lpc32xx_irq_domain_map, 1708c2ecf20Sopenharmony_ci .unmap = lpc32xx_irq_domain_unmap, 1718c2ecf20Sopenharmony_ci .xlate = irq_domain_xlate_twocell, 1728c2ecf20Sopenharmony_ci}; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_cistatic int __init lpc32xx_of_ic_init(struct device_node *node, 1758c2ecf20Sopenharmony_ci struct device_node *parent) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct lpc32xx_irq_chip *irqc; 1788c2ecf20Sopenharmony_ci bool is_mic = of_device_is_compatible(node, "nxp,lpc3220-mic"); 1798c2ecf20Sopenharmony_ci const __be32 *reg = of_get_property(node, "reg", NULL); 1808c2ecf20Sopenharmony_ci u32 parent_irq, i, addr = reg ? be32_to_cpu(*reg) : 0; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci irqc = kzalloc(sizeof(*irqc), GFP_KERNEL); 1838c2ecf20Sopenharmony_ci if (!irqc) 1848c2ecf20Sopenharmony_ci return -ENOMEM; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci irqc->base = of_iomap(node, 0); 1878c2ecf20Sopenharmony_ci if (!irqc->base) { 1888c2ecf20Sopenharmony_ci pr_err("%pOF: unable to map registers\n", node); 1898c2ecf20Sopenharmony_ci kfree(irqc); 1908c2ecf20Sopenharmony_ci return -EINVAL; 1918c2ecf20Sopenharmony_ci } 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci irqc->chip.irq_ack = lpc32xx_irq_ack; 1948c2ecf20Sopenharmony_ci irqc->chip.irq_mask = lpc32xx_irq_mask; 1958c2ecf20Sopenharmony_ci irqc->chip.irq_unmask = lpc32xx_irq_unmask; 1968c2ecf20Sopenharmony_ci irqc->chip.irq_set_type = lpc32xx_irq_set_type; 1978c2ecf20Sopenharmony_ci if (is_mic) 1988c2ecf20Sopenharmony_ci irqc->chip.name = kasprintf(GFP_KERNEL, "%08x.mic", addr); 1998c2ecf20Sopenharmony_ci else 2008c2ecf20Sopenharmony_ci irqc->chip.name = kasprintf(GFP_KERNEL, "%08x.sic", addr); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci irqc->domain = irq_domain_add_linear(node, NR_LPC32XX_IC_IRQS, 2038c2ecf20Sopenharmony_ci &lpc32xx_irq_domain_ops, irqc); 2048c2ecf20Sopenharmony_ci if (!irqc->domain) { 2058c2ecf20Sopenharmony_ci pr_err("unable to add irq domain\n"); 2068c2ecf20Sopenharmony_ci iounmap(irqc->base); 2078c2ecf20Sopenharmony_ci kfree(irqc->chip.name); 2088c2ecf20Sopenharmony_ci kfree(irqc); 2098c2ecf20Sopenharmony_ci return -ENODEV; 2108c2ecf20Sopenharmony_ci } 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci if (is_mic) { 2138c2ecf20Sopenharmony_ci lpc32xx_mic_irqc = irqc; 2148c2ecf20Sopenharmony_ci set_handle_irq(lpc32xx_handle_irq); 2158c2ecf20Sopenharmony_ci } else { 2168c2ecf20Sopenharmony_ci for (i = 0; i < of_irq_count(node); i++) { 2178c2ecf20Sopenharmony_ci parent_irq = irq_of_parse_and_map(node, i); 2188c2ecf20Sopenharmony_ci if (parent_irq) 2198c2ecf20Sopenharmony_ci irq_set_chained_handler_and_data(parent_irq, 2208c2ecf20Sopenharmony_ci lpc32xx_sic_handler, irqc); 2218c2ecf20Sopenharmony_ci } 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci lpc32xx_ic_write(irqc, LPC32XX_INTC_MASK, 0x00); 2258c2ecf20Sopenharmony_ci lpc32xx_ic_write(irqc, LPC32XX_INTC_POL, 0x00); 2268c2ecf20Sopenharmony_ci lpc32xx_ic_write(irqc, LPC32XX_INTC_TYPE, 0x00); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci return 0; 2298c2ecf20Sopenharmony_ci} 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(nxp_lpc32xx_mic, "nxp,lpc3220-mic", lpc32xx_of_ic_init); 2328c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(nxp_lpc32xx_sic, "nxp,lpc3220-sic", lpc32xx_of_ic_init); 233