18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Freescale Semiconductor, Inc. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/of_address.h> 78c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 88c2ecf20Sopenharmony_ci#include <linux/slab.h> 98c2ecf20Sopenharmony_ci#include <linux/irqchip.h> 108c2ecf20Sopenharmony_ci#include <linux/syscore_ops.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#define IMR_NUM 4 138c2ecf20Sopenharmony_ci#define GPC_MAX_IRQS (IMR_NUM * 32) 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define GPC_IMR1_CORE0 0x30 168c2ecf20Sopenharmony_ci#define GPC_IMR1_CORE1 0x40 178c2ecf20Sopenharmony_ci#define GPC_IMR1_CORE2 0x1c0 188c2ecf20Sopenharmony_ci#define GPC_IMR1_CORE3 0x1d0 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistruct gpcv2_irqchip_data { 228c2ecf20Sopenharmony_ci struct raw_spinlock rlock; 238c2ecf20Sopenharmony_ci void __iomem *gpc_base; 248c2ecf20Sopenharmony_ci u32 wakeup_sources[IMR_NUM]; 258c2ecf20Sopenharmony_ci u32 saved_irq_mask[IMR_NUM]; 268c2ecf20Sopenharmony_ci u32 cpu2wakeup; 278c2ecf20Sopenharmony_ci}; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic struct gpcv2_irqchip_data *imx_gpcv2_instance; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic void __iomem *gpcv2_idx_to_reg(struct gpcv2_irqchip_data *cd, int i) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci return cd->gpc_base + cd->cpu2wakeup + i * 4; 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic int gpcv2_wakeup_source_save(void) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci struct gpcv2_irqchip_data *cd; 398c2ecf20Sopenharmony_ci void __iomem *reg; 408c2ecf20Sopenharmony_ci int i; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci cd = imx_gpcv2_instance; 438c2ecf20Sopenharmony_ci if (!cd) 448c2ecf20Sopenharmony_ci return 0; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci for (i = 0; i < IMR_NUM; i++) { 478c2ecf20Sopenharmony_ci reg = gpcv2_idx_to_reg(cd, i); 488c2ecf20Sopenharmony_ci cd->saved_irq_mask[i] = readl_relaxed(reg); 498c2ecf20Sopenharmony_ci writel_relaxed(cd->wakeup_sources[i], reg); 508c2ecf20Sopenharmony_ci } 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci return 0; 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic void gpcv2_wakeup_source_restore(void) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci struct gpcv2_irqchip_data *cd; 588c2ecf20Sopenharmony_ci int i; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci cd = imx_gpcv2_instance; 618c2ecf20Sopenharmony_ci if (!cd) 628c2ecf20Sopenharmony_ci return; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci for (i = 0; i < IMR_NUM; i++) 658c2ecf20Sopenharmony_ci writel_relaxed(cd->saved_irq_mask[i], gpcv2_idx_to_reg(cd, i)); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic struct syscore_ops imx_gpcv2_syscore_ops = { 698c2ecf20Sopenharmony_ci .suspend = gpcv2_wakeup_source_save, 708c2ecf20Sopenharmony_ci .resume = gpcv2_wakeup_source_restore, 718c2ecf20Sopenharmony_ci}; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic int imx_gpcv2_irq_set_wake(struct irq_data *d, unsigned int on) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci struct gpcv2_irqchip_data *cd = d->chip_data; 768c2ecf20Sopenharmony_ci unsigned int idx = d->hwirq / 32; 778c2ecf20Sopenharmony_ci unsigned long flags; 788c2ecf20Sopenharmony_ci u32 mask, val; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&cd->rlock, flags); 818c2ecf20Sopenharmony_ci mask = BIT(d->hwirq % 32); 828c2ecf20Sopenharmony_ci val = cd->wakeup_sources[idx]; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci cd->wakeup_sources[idx] = on ? (val & ~mask) : (val | mask); 858c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&cd->rlock, flags); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* 888c2ecf20Sopenharmony_ci * Do *not* call into the parent, as the GIC doesn't have any 898c2ecf20Sopenharmony_ci * wake-up facility... 908c2ecf20Sopenharmony_ci */ 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci return 0; 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic void imx_gpcv2_irq_unmask(struct irq_data *d) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci struct gpcv2_irqchip_data *cd = d->chip_data; 988c2ecf20Sopenharmony_ci void __iomem *reg; 998c2ecf20Sopenharmony_ci u32 val; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci raw_spin_lock(&cd->rlock); 1028c2ecf20Sopenharmony_ci reg = gpcv2_idx_to_reg(cd, d->hwirq / 32); 1038c2ecf20Sopenharmony_ci val = readl_relaxed(reg); 1048c2ecf20Sopenharmony_ci val &= ~BIT(d->hwirq % 32); 1058c2ecf20Sopenharmony_ci writel_relaxed(val, reg); 1068c2ecf20Sopenharmony_ci raw_spin_unlock(&cd->rlock); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci irq_chip_unmask_parent(d); 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic void imx_gpcv2_irq_mask(struct irq_data *d) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci struct gpcv2_irqchip_data *cd = d->chip_data; 1148c2ecf20Sopenharmony_ci void __iomem *reg; 1158c2ecf20Sopenharmony_ci u32 val; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci raw_spin_lock(&cd->rlock); 1188c2ecf20Sopenharmony_ci reg = gpcv2_idx_to_reg(cd, d->hwirq / 32); 1198c2ecf20Sopenharmony_ci val = readl_relaxed(reg); 1208c2ecf20Sopenharmony_ci val |= BIT(d->hwirq % 32); 1218c2ecf20Sopenharmony_ci writel_relaxed(val, reg); 1228c2ecf20Sopenharmony_ci raw_spin_unlock(&cd->rlock); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci irq_chip_mask_parent(d); 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic struct irq_chip gpcv2_irqchip_data_chip = { 1288c2ecf20Sopenharmony_ci .name = "GPCv2", 1298c2ecf20Sopenharmony_ci .irq_eoi = irq_chip_eoi_parent, 1308c2ecf20Sopenharmony_ci .irq_mask = imx_gpcv2_irq_mask, 1318c2ecf20Sopenharmony_ci .irq_unmask = imx_gpcv2_irq_unmask, 1328c2ecf20Sopenharmony_ci .irq_set_wake = imx_gpcv2_irq_set_wake, 1338c2ecf20Sopenharmony_ci .irq_retrigger = irq_chip_retrigger_hierarchy, 1348c2ecf20Sopenharmony_ci .irq_set_type = irq_chip_set_type_parent, 1358c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP 1368c2ecf20Sopenharmony_ci .irq_set_affinity = irq_chip_set_affinity_parent, 1378c2ecf20Sopenharmony_ci#endif 1388c2ecf20Sopenharmony_ci}; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic int imx_gpcv2_domain_translate(struct irq_domain *d, 1418c2ecf20Sopenharmony_ci struct irq_fwspec *fwspec, 1428c2ecf20Sopenharmony_ci unsigned long *hwirq, 1438c2ecf20Sopenharmony_ci unsigned int *type) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci if (is_of_node(fwspec->fwnode)) { 1468c2ecf20Sopenharmony_ci if (fwspec->param_count != 3) 1478c2ecf20Sopenharmony_ci return -EINVAL; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci /* No PPI should point to this domain */ 1508c2ecf20Sopenharmony_ci if (fwspec->param[0] != 0) 1518c2ecf20Sopenharmony_ci return -EINVAL; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci *hwirq = fwspec->param[1]; 1548c2ecf20Sopenharmony_ci *type = fwspec->param[2]; 1558c2ecf20Sopenharmony_ci return 0; 1568c2ecf20Sopenharmony_ci } 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci return -EINVAL; 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic int imx_gpcv2_domain_alloc(struct irq_domain *domain, 1628c2ecf20Sopenharmony_ci unsigned int irq, unsigned int nr_irqs, 1638c2ecf20Sopenharmony_ci void *data) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci struct irq_fwspec *fwspec = data; 1668c2ecf20Sopenharmony_ci struct irq_fwspec parent_fwspec; 1678c2ecf20Sopenharmony_ci irq_hw_number_t hwirq; 1688c2ecf20Sopenharmony_ci unsigned int type; 1698c2ecf20Sopenharmony_ci int err; 1708c2ecf20Sopenharmony_ci int i; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci err = imx_gpcv2_domain_translate(domain, fwspec, &hwirq, &type); 1738c2ecf20Sopenharmony_ci if (err) 1748c2ecf20Sopenharmony_ci return err; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (hwirq >= GPC_MAX_IRQS) 1778c2ecf20Sopenharmony_ci return -EINVAL; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci for (i = 0; i < nr_irqs; i++) { 1808c2ecf20Sopenharmony_ci irq_domain_set_hwirq_and_chip(domain, irq + i, hwirq + i, 1818c2ecf20Sopenharmony_ci &gpcv2_irqchip_data_chip, domain->host_data); 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci parent_fwspec = *fwspec; 1858c2ecf20Sopenharmony_ci parent_fwspec.fwnode = domain->parent->fwnode; 1868c2ecf20Sopenharmony_ci return irq_domain_alloc_irqs_parent(domain, irq, nr_irqs, 1878c2ecf20Sopenharmony_ci &parent_fwspec); 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic const struct irq_domain_ops gpcv2_irqchip_data_domain_ops = { 1918c2ecf20Sopenharmony_ci .translate = imx_gpcv2_domain_translate, 1928c2ecf20Sopenharmony_ci .alloc = imx_gpcv2_domain_alloc, 1938c2ecf20Sopenharmony_ci .free = irq_domain_free_irqs_common, 1948c2ecf20Sopenharmony_ci}; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_cistatic const struct of_device_id gpcv2_of_match[] = { 1978c2ecf20Sopenharmony_ci { .compatible = "fsl,imx7d-gpc", .data = (const void *) 2 }, 1988c2ecf20Sopenharmony_ci { .compatible = "fsl,imx8mq-gpc", .data = (const void *) 4 }, 1998c2ecf20Sopenharmony_ci { /* END */ } 2008c2ecf20Sopenharmony_ci}; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_cistatic int __init imx_gpcv2_irqchip_init(struct device_node *node, 2038c2ecf20Sopenharmony_ci struct device_node *parent) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci struct irq_domain *parent_domain, *domain; 2068c2ecf20Sopenharmony_ci struct gpcv2_irqchip_data *cd; 2078c2ecf20Sopenharmony_ci const struct of_device_id *id; 2088c2ecf20Sopenharmony_ci unsigned long core_num; 2098c2ecf20Sopenharmony_ci int i; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci if (!parent) { 2128c2ecf20Sopenharmony_ci pr_err("%pOF: no parent, giving up\n", node); 2138c2ecf20Sopenharmony_ci return -ENODEV; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci id = of_match_node(gpcv2_of_match, node); 2178c2ecf20Sopenharmony_ci if (!id) { 2188c2ecf20Sopenharmony_ci pr_err("%pOF: unknown compatibility string\n", node); 2198c2ecf20Sopenharmony_ci return -ENODEV; 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci core_num = (unsigned long)id->data; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci parent_domain = irq_find_host(parent); 2258c2ecf20Sopenharmony_ci if (!parent_domain) { 2268c2ecf20Sopenharmony_ci pr_err("%pOF: unable to get parent domain\n", node); 2278c2ecf20Sopenharmony_ci return -ENXIO; 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci cd = kzalloc(sizeof(struct gpcv2_irqchip_data), GFP_KERNEL); 2318c2ecf20Sopenharmony_ci if (!cd) { 2328c2ecf20Sopenharmony_ci pr_err("%pOF: kzalloc failed!\n", node); 2338c2ecf20Sopenharmony_ci return -ENOMEM; 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci raw_spin_lock_init(&cd->rlock); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci cd->gpc_base = of_iomap(node, 0); 2398c2ecf20Sopenharmony_ci if (!cd->gpc_base) { 2408c2ecf20Sopenharmony_ci pr_err("%pOF: unable to map gpc registers\n", node); 2418c2ecf20Sopenharmony_ci kfree(cd); 2428c2ecf20Sopenharmony_ci return -ENOMEM; 2438c2ecf20Sopenharmony_ci } 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci domain = irq_domain_add_hierarchy(parent_domain, 0, GPC_MAX_IRQS, 2468c2ecf20Sopenharmony_ci node, &gpcv2_irqchip_data_domain_ops, cd); 2478c2ecf20Sopenharmony_ci if (!domain) { 2488c2ecf20Sopenharmony_ci iounmap(cd->gpc_base); 2498c2ecf20Sopenharmony_ci kfree(cd); 2508c2ecf20Sopenharmony_ci return -ENOMEM; 2518c2ecf20Sopenharmony_ci } 2528c2ecf20Sopenharmony_ci irq_set_default_host(domain); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci /* Initially mask all interrupts */ 2558c2ecf20Sopenharmony_ci for (i = 0; i < IMR_NUM; i++) { 2568c2ecf20Sopenharmony_ci void __iomem *reg = cd->gpc_base + i * 4; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci switch (core_num) { 2598c2ecf20Sopenharmony_ci case 4: 2608c2ecf20Sopenharmony_ci writel_relaxed(~0, reg + GPC_IMR1_CORE2); 2618c2ecf20Sopenharmony_ci writel_relaxed(~0, reg + GPC_IMR1_CORE3); 2628c2ecf20Sopenharmony_ci fallthrough; 2638c2ecf20Sopenharmony_ci case 2: 2648c2ecf20Sopenharmony_ci writel_relaxed(~0, reg + GPC_IMR1_CORE0); 2658c2ecf20Sopenharmony_ci writel_relaxed(~0, reg + GPC_IMR1_CORE1); 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci cd->wakeup_sources[i] = ~0; 2688c2ecf20Sopenharmony_ci } 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci /* Let CORE0 as the default CPU to wake up by GPC */ 2718c2ecf20Sopenharmony_ci cd->cpu2wakeup = GPC_IMR1_CORE0; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci /* 2748c2ecf20Sopenharmony_ci * Due to hardware design failure, need to make sure GPR 2758c2ecf20Sopenharmony_ci * interrupt(#32) is unmasked during RUN mode to avoid entering 2768c2ecf20Sopenharmony_ci * DSM by mistake. 2778c2ecf20Sopenharmony_ci */ 2788c2ecf20Sopenharmony_ci writel_relaxed(~0x1, cd->gpc_base + cd->cpu2wakeup); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci imx_gpcv2_instance = cd; 2818c2ecf20Sopenharmony_ci register_syscore_ops(&imx_gpcv2_syscore_ops); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci /* 2848c2ecf20Sopenharmony_ci * Clear the OF_POPULATED flag set in of_irq_init so that 2858c2ecf20Sopenharmony_ci * later the GPC power domain driver will not be skipped. 2868c2ecf20Sopenharmony_ci */ 2878c2ecf20Sopenharmony_ci of_node_clear_flag(node, OF_POPULATED); 2888c2ecf20Sopenharmony_ci return 0; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(imx_gpcv2_imx7d, "fsl,imx7d-gpc", imx_gpcv2_irqchip_init); 2928c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(imx_gpcv2_imx8mq, "fsl,imx8mq-gpc", imx_gpcv2_irqchip_init); 293