18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2014 MediaTek Inc. 48c2ecf20Sopenharmony_ci * Author: Joe.C <yingjoe.chen@mediatek.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/irq.h> 88c2ecf20Sopenharmony_ci#include <linux/irqchip.h> 98c2ecf20Sopenharmony_ci#include <linux/irqdomain.h> 108c2ecf20Sopenharmony_ci#include <linux/of.h> 118c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 128c2ecf20Sopenharmony_ci#include <linux/of_address.h> 138c2ecf20Sopenharmony_ci#include <linux/io.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistruct mtk_sysirq_chip_data { 188c2ecf20Sopenharmony_ci raw_spinlock_t lock; 198c2ecf20Sopenharmony_ci u32 nr_intpol_bases; 208c2ecf20Sopenharmony_ci void __iomem **intpol_bases; 218c2ecf20Sopenharmony_ci u32 *intpol_words; 228c2ecf20Sopenharmony_ci u8 *intpol_idx; 238c2ecf20Sopenharmony_ci u16 *which_word; 248c2ecf20Sopenharmony_ci}; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic int mtk_sysirq_set_type(struct irq_data *data, unsigned int type) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci irq_hw_number_t hwirq = data->hwirq; 298c2ecf20Sopenharmony_ci struct mtk_sysirq_chip_data *chip_data = data->chip_data; 308c2ecf20Sopenharmony_ci u8 intpol_idx = chip_data->intpol_idx[hwirq]; 318c2ecf20Sopenharmony_ci void __iomem *base; 328c2ecf20Sopenharmony_ci u32 offset, reg_index, value; 338c2ecf20Sopenharmony_ci unsigned long flags; 348c2ecf20Sopenharmony_ci int ret; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci base = chip_data->intpol_bases[intpol_idx]; 378c2ecf20Sopenharmony_ci reg_index = chip_data->which_word[hwirq]; 388c2ecf20Sopenharmony_ci offset = hwirq & 0x1f; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&chip_data->lock, flags); 418c2ecf20Sopenharmony_ci value = readl_relaxed(base + reg_index * 4); 428c2ecf20Sopenharmony_ci if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_EDGE_FALLING) { 438c2ecf20Sopenharmony_ci if (type == IRQ_TYPE_LEVEL_LOW) 448c2ecf20Sopenharmony_ci type = IRQ_TYPE_LEVEL_HIGH; 458c2ecf20Sopenharmony_ci else 468c2ecf20Sopenharmony_ci type = IRQ_TYPE_EDGE_RISING; 478c2ecf20Sopenharmony_ci value |= (1 << offset); 488c2ecf20Sopenharmony_ci } else { 498c2ecf20Sopenharmony_ci value &= ~(1 << offset); 508c2ecf20Sopenharmony_ci } 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci writel_relaxed(value, base + reg_index * 4); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci data = data->parent_data; 558c2ecf20Sopenharmony_ci ret = data->chip->irq_set_type(data, type); 568c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&chip_data->lock, flags); 578c2ecf20Sopenharmony_ci return ret; 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic struct irq_chip mtk_sysirq_chip = { 618c2ecf20Sopenharmony_ci .name = "MT_SYSIRQ", 628c2ecf20Sopenharmony_ci .irq_mask = irq_chip_mask_parent, 638c2ecf20Sopenharmony_ci .irq_unmask = irq_chip_unmask_parent, 648c2ecf20Sopenharmony_ci .irq_eoi = irq_chip_eoi_parent, 658c2ecf20Sopenharmony_ci .irq_set_type = mtk_sysirq_set_type, 668c2ecf20Sopenharmony_ci .irq_retrigger = irq_chip_retrigger_hierarchy, 678c2ecf20Sopenharmony_ci .irq_set_affinity = irq_chip_set_affinity_parent, 688c2ecf20Sopenharmony_ci}; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic int mtk_sysirq_domain_translate(struct irq_domain *d, 718c2ecf20Sopenharmony_ci struct irq_fwspec *fwspec, 728c2ecf20Sopenharmony_ci unsigned long *hwirq, 738c2ecf20Sopenharmony_ci unsigned int *type) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci if (is_of_node(fwspec->fwnode)) { 768c2ecf20Sopenharmony_ci if (fwspec->param_count != 3) 778c2ecf20Sopenharmony_ci return -EINVAL; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci /* No PPI should point to this domain */ 808c2ecf20Sopenharmony_ci if (fwspec->param[0] != 0) 818c2ecf20Sopenharmony_ci return -EINVAL; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci *hwirq = fwspec->param[1]; 848c2ecf20Sopenharmony_ci *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK; 858c2ecf20Sopenharmony_ci return 0; 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return -EINVAL; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic int mtk_sysirq_domain_alloc(struct irq_domain *domain, unsigned int virq, 928c2ecf20Sopenharmony_ci unsigned int nr_irqs, void *arg) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci int i; 958c2ecf20Sopenharmony_ci irq_hw_number_t hwirq; 968c2ecf20Sopenharmony_ci struct irq_fwspec *fwspec = arg; 978c2ecf20Sopenharmony_ci struct irq_fwspec gic_fwspec = *fwspec; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci if (fwspec->param_count != 3) 1008c2ecf20Sopenharmony_ci return -EINVAL; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* sysirq doesn't support PPI */ 1038c2ecf20Sopenharmony_ci if (fwspec->param[0]) 1048c2ecf20Sopenharmony_ci return -EINVAL; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci hwirq = fwspec->param[1]; 1078c2ecf20Sopenharmony_ci for (i = 0; i < nr_irqs; i++) 1088c2ecf20Sopenharmony_ci irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i, 1098c2ecf20Sopenharmony_ci &mtk_sysirq_chip, 1108c2ecf20Sopenharmony_ci domain->host_data); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci gic_fwspec.fwnode = domain->parent->fwnode; 1138c2ecf20Sopenharmony_ci return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &gic_fwspec); 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic const struct irq_domain_ops sysirq_domain_ops = { 1178c2ecf20Sopenharmony_ci .translate = mtk_sysirq_domain_translate, 1188c2ecf20Sopenharmony_ci .alloc = mtk_sysirq_domain_alloc, 1198c2ecf20Sopenharmony_ci .free = irq_domain_free_irqs_common, 1208c2ecf20Sopenharmony_ci}; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic int __init mtk_sysirq_of_init(struct device_node *node, 1238c2ecf20Sopenharmony_ci struct device_node *parent) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct irq_domain *domain, *domain_parent; 1268c2ecf20Sopenharmony_ci struct mtk_sysirq_chip_data *chip_data; 1278c2ecf20Sopenharmony_ci int ret, size, intpol_num = 0, nr_intpol_bases = 0, i = 0; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci domain_parent = irq_find_host(parent); 1308c2ecf20Sopenharmony_ci if (!domain_parent) { 1318c2ecf20Sopenharmony_ci pr_err("mtk_sysirq: interrupt-parent not found\n"); 1328c2ecf20Sopenharmony_ci return -EINVAL; 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL); 1368c2ecf20Sopenharmony_ci if (!chip_data) 1378c2ecf20Sopenharmony_ci return -ENOMEM; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci while (of_get_address(node, i++, NULL, NULL)) 1408c2ecf20Sopenharmony_ci nr_intpol_bases++; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci if (nr_intpol_bases == 0) { 1438c2ecf20Sopenharmony_ci pr_err("mtk_sysirq: base address not specified\n"); 1448c2ecf20Sopenharmony_ci ret = -EINVAL; 1458c2ecf20Sopenharmony_ci goto out_free_chip; 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci chip_data->intpol_words = kcalloc(nr_intpol_bases, 1498c2ecf20Sopenharmony_ci sizeof(*chip_data->intpol_words), 1508c2ecf20Sopenharmony_ci GFP_KERNEL); 1518c2ecf20Sopenharmony_ci if (!chip_data->intpol_words) { 1528c2ecf20Sopenharmony_ci ret = -ENOMEM; 1538c2ecf20Sopenharmony_ci goto out_free_chip; 1548c2ecf20Sopenharmony_ci } 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci chip_data->intpol_bases = kcalloc(nr_intpol_bases, 1578c2ecf20Sopenharmony_ci sizeof(*chip_data->intpol_bases), 1588c2ecf20Sopenharmony_ci GFP_KERNEL); 1598c2ecf20Sopenharmony_ci if (!chip_data->intpol_bases) { 1608c2ecf20Sopenharmony_ci ret = -ENOMEM; 1618c2ecf20Sopenharmony_ci goto out_free_intpol_words; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci for (i = 0; i < nr_intpol_bases; i++) { 1658c2ecf20Sopenharmony_ci struct resource res; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci ret = of_address_to_resource(node, i, &res); 1688c2ecf20Sopenharmony_ci size = resource_size(&res); 1698c2ecf20Sopenharmony_ci intpol_num += size * 8; 1708c2ecf20Sopenharmony_ci chip_data->intpol_words[i] = size / 4; 1718c2ecf20Sopenharmony_ci chip_data->intpol_bases[i] = of_iomap(node, i); 1728c2ecf20Sopenharmony_ci if (ret || !chip_data->intpol_bases[i]) { 1738c2ecf20Sopenharmony_ci pr_err("%pOF: couldn't map region %d\n", node, i); 1748c2ecf20Sopenharmony_ci ret = -ENODEV; 1758c2ecf20Sopenharmony_ci goto out_free_intpol; 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci chip_data->intpol_idx = kcalloc(intpol_num, 1808c2ecf20Sopenharmony_ci sizeof(*chip_data->intpol_idx), 1818c2ecf20Sopenharmony_ci GFP_KERNEL); 1828c2ecf20Sopenharmony_ci if (!chip_data->intpol_idx) { 1838c2ecf20Sopenharmony_ci ret = -ENOMEM; 1848c2ecf20Sopenharmony_ci goto out_free_intpol; 1858c2ecf20Sopenharmony_ci } 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci chip_data->which_word = kcalloc(intpol_num, 1888c2ecf20Sopenharmony_ci sizeof(*chip_data->which_word), 1898c2ecf20Sopenharmony_ci GFP_KERNEL); 1908c2ecf20Sopenharmony_ci if (!chip_data->which_word) { 1918c2ecf20Sopenharmony_ci ret = -ENOMEM; 1928c2ecf20Sopenharmony_ci goto out_free_intpol_idx; 1938c2ecf20Sopenharmony_ci } 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci /* 1968c2ecf20Sopenharmony_ci * assign an index of the intpol_bases for each irq 1978c2ecf20Sopenharmony_ci * to set it fast later 1988c2ecf20Sopenharmony_ci */ 1998c2ecf20Sopenharmony_ci for (i = 0; i < intpol_num ; i++) { 2008c2ecf20Sopenharmony_ci u32 word = i / 32, j; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci for (j = 0; word >= chip_data->intpol_words[j] ; j++) 2038c2ecf20Sopenharmony_ci word -= chip_data->intpol_words[j]; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci chip_data->intpol_idx[i] = j; 2068c2ecf20Sopenharmony_ci chip_data->which_word[i] = word; 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci domain = irq_domain_add_hierarchy(domain_parent, 0, intpol_num, node, 2108c2ecf20Sopenharmony_ci &sysirq_domain_ops, chip_data); 2118c2ecf20Sopenharmony_ci if (!domain) { 2128c2ecf20Sopenharmony_ci ret = -ENOMEM; 2138c2ecf20Sopenharmony_ci goto out_free_which_word; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci raw_spin_lock_init(&chip_data->lock); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci return 0; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ciout_free_which_word: 2208c2ecf20Sopenharmony_ci kfree(chip_data->which_word); 2218c2ecf20Sopenharmony_ciout_free_intpol_idx: 2228c2ecf20Sopenharmony_ci kfree(chip_data->intpol_idx); 2238c2ecf20Sopenharmony_ciout_free_intpol: 2248c2ecf20Sopenharmony_ci for (i = 0; i < nr_intpol_bases; i++) 2258c2ecf20Sopenharmony_ci if (chip_data->intpol_bases[i]) 2268c2ecf20Sopenharmony_ci iounmap(chip_data->intpol_bases[i]); 2278c2ecf20Sopenharmony_ci kfree(chip_data->intpol_bases); 2288c2ecf20Sopenharmony_ciout_free_intpol_words: 2298c2ecf20Sopenharmony_ci kfree(chip_data->intpol_words); 2308c2ecf20Sopenharmony_ciout_free_chip: 2318c2ecf20Sopenharmony_ci kfree(chip_data); 2328c2ecf20Sopenharmony_ci return ret; 2338c2ecf20Sopenharmony_ci} 2348c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(mtk_sysirq, "mediatek,mt6577-sysirq", mtk_sysirq_of_init); 235