18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * SPEAr platform shared irq layer source file
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2009-2012 ST Microelectronics
58c2ecf20Sopenharmony_ci * Viresh Kumar <vireshk@kernel.org>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2012 ST Microelectronics
88c2ecf20Sopenharmony_ci * Shiraz Hashim <shiraz.linux.kernel@gmail.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public
118c2ecf20Sopenharmony_ci * License version 2. This program is licensed "as is" without any
128c2ecf20Sopenharmony_ci * warranty of any kind, whether express or implied.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/export.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/io.h>
208c2ecf20Sopenharmony_ci#include <linux/irq.h>
218c2ecf20Sopenharmony_ci#include <linux/irqchip.h>
228c2ecf20Sopenharmony_ci#include <linux/irqdomain.h>
238c2ecf20Sopenharmony_ci#include <linux/of.h>
248c2ecf20Sopenharmony_ci#include <linux/of_address.h>
258c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
268c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * struct spear_shirq: shared irq structure
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * base:	Base register address
328c2ecf20Sopenharmony_ci * status_reg:	Status register offset for chained interrupt handler
338c2ecf20Sopenharmony_ci * mask_reg:	Mask register offset for irq chip
348c2ecf20Sopenharmony_ci * mask:	Mask to apply to the status register
358c2ecf20Sopenharmony_ci * virq_base:	Base virtual interrupt number
368c2ecf20Sopenharmony_ci * nr_irqs:	Number of interrupts handled by this block
378c2ecf20Sopenharmony_ci * offset:	Bit offset of the first interrupt
388c2ecf20Sopenharmony_ci * irq_chip:	Interrupt controller chip used for this instance,
398c2ecf20Sopenharmony_ci *		if NULL group is disabled, but accounted
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_cistruct spear_shirq {
428c2ecf20Sopenharmony_ci	void __iomem		*base;
438c2ecf20Sopenharmony_ci	u32			status_reg;
448c2ecf20Sopenharmony_ci	u32			mask_reg;
458c2ecf20Sopenharmony_ci	u32			mask;
468c2ecf20Sopenharmony_ci	u32			virq_base;
478c2ecf20Sopenharmony_ci	u32			nr_irqs;
488c2ecf20Sopenharmony_ci	u32			offset;
498c2ecf20Sopenharmony_ci	struct irq_chip		*irq_chip;
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* spear300 shared irq registers offsets and masks */
538c2ecf20Sopenharmony_ci#define SPEAR300_INT_ENB_MASK_REG	0x54
548c2ecf20Sopenharmony_ci#define SPEAR300_INT_STS_MASK_REG	0x58
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic DEFINE_RAW_SPINLOCK(shirq_lock);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic void shirq_irq_mask(struct irq_data *d)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	struct spear_shirq *shirq = irq_data_get_irq_chip_data(d);
618c2ecf20Sopenharmony_ci	u32 val, shift = d->irq - shirq->virq_base + shirq->offset;
628c2ecf20Sopenharmony_ci	u32 __iomem *reg = shirq->base + shirq->mask_reg;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	raw_spin_lock(&shirq_lock);
658c2ecf20Sopenharmony_ci	val = readl(reg) & ~(0x1 << shift);
668c2ecf20Sopenharmony_ci	writel(val, reg);
678c2ecf20Sopenharmony_ci	raw_spin_unlock(&shirq_lock);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic void shirq_irq_unmask(struct irq_data *d)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct spear_shirq *shirq = irq_data_get_irq_chip_data(d);
738c2ecf20Sopenharmony_ci	u32 val, shift = d->irq - shirq->virq_base + shirq->offset;
748c2ecf20Sopenharmony_ci	u32 __iomem *reg = shirq->base + shirq->mask_reg;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	raw_spin_lock(&shirq_lock);
778c2ecf20Sopenharmony_ci	val = readl(reg) | (0x1 << shift);
788c2ecf20Sopenharmony_ci	writel(val, reg);
798c2ecf20Sopenharmony_ci	raw_spin_unlock(&shirq_lock);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic struct irq_chip shirq_chip = {
838c2ecf20Sopenharmony_ci	.name		= "spear-shirq",
848c2ecf20Sopenharmony_ci	.irq_mask	= shirq_irq_mask,
858c2ecf20Sopenharmony_ci	.irq_unmask	= shirq_irq_unmask,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic struct spear_shirq spear300_shirq_ras1 = {
898c2ecf20Sopenharmony_ci	.offset		= 0,
908c2ecf20Sopenharmony_ci	.nr_irqs	= 9,
918c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 9) - 1) << 0,
928c2ecf20Sopenharmony_ci	.irq_chip	= &shirq_chip,
938c2ecf20Sopenharmony_ci	.status_reg	= SPEAR300_INT_STS_MASK_REG,
948c2ecf20Sopenharmony_ci	.mask_reg	= SPEAR300_INT_ENB_MASK_REG,
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic struct spear_shirq *spear300_shirq_blocks[] = {
988c2ecf20Sopenharmony_ci	&spear300_shirq_ras1,
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* spear310 shared irq registers offsets and masks */
1028c2ecf20Sopenharmony_ci#define SPEAR310_INT_STS_MASK_REG	0x04
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic struct spear_shirq spear310_shirq_ras1 = {
1058c2ecf20Sopenharmony_ci	.offset		= 0,
1068c2ecf20Sopenharmony_ci	.nr_irqs	= 8,
1078c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 8) - 1) << 0,
1088c2ecf20Sopenharmony_ci	.irq_chip	= &dummy_irq_chip,
1098c2ecf20Sopenharmony_ci	.status_reg	= SPEAR310_INT_STS_MASK_REG,
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic struct spear_shirq spear310_shirq_ras2 = {
1138c2ecf20Sopenharmony_ci	.offset		= 8,
1148c2ecf20Sopenharmony_ci	.nr_irqs	= 5,
1158c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 5) - 1) << 8,
1168c2ecf20Sopenharmony_ci	.irq_chip	= &dummy_irq_chip,
1178c2ecf20Sopenharmony_ci	.status_reg	= SPEAR310_INT_STS_MASK_REG,
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic struct spear_shirq spear310_shirq_ras3 = {
1218c2ecf20Sopenharmony_ci	.offset		= 13,
1228c2ecf20Sopenharmony_ci	.nr_irqs	= 1,
1238c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 1) - 1) << 13,
1248c2ecf20Sopenharmony_ci	.irq_chip	= &dummy_irq_chip,
1258c2ecf20Sopenharmony_ci	.status_reg	= SPEAR310_INT_STS_MASK_REG,
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic struct spear_shirq spear310_shirq_intrcomm_ras = {
1298c2ecf20Sopenharmony_ci	.offset		= 14,
1308c2ecf20Sopenharmony_ci	.nr_irqs	= 3,
1318c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 3) - 1) << 14,
1328c2ecf20Sopenharmony_ci	.irq_chip	= &dummy_irq_chip,
1338c2ecf20Sopenharmony_ci	.status_reg	= SPEAR310_INT_STS_MASK_REG,
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic struct spear_shirq *spear310_shirq_blocks[] = {
1378c2ecf20Sopenharmony_ci	&spear310_shirq_ras1,
1388c2ecf20Sopenharmony_ci	&spear310_shirq_ras2,
1398c2ecf20Sopenharmony_ci	&spear310_shirq_ras3,
1408c2ecf20Sopenharmony_ci	&spear310_shirq_intrcomm_ras,
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* spear320 shared irq registers offsets and masks */
1448c2ecf20Sopenharmony_ci#define SPEAR320_INT_STS_MASK_REG		0x04
1458c2ecf20Sopenharmony_ci#define SPEAR320_INT_CLR_MASK_REG		0x04
1468c2ecf20Sopenharmony_ci#define SPEAR320_INT_ENB_MASK_REG		0x08
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic struct spear_shirq spear320_shirq_ras3 = {
1498c2ecf20Sopenharmony_ci	.offset		= 0,
1508c2ecf20Sopenharmony_ci	.nr_irqs	= 7,
1518c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 7) - 1) << 0,
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic struct spear_shirq spear320_shirq_ras1 = {
1558c2ecf20Sopenharmony_ci	.offset		= 7,
1568c2ecf20Sopenharmony_ci	.nr_irqs	= 3,
1578c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 3) - 1) << 7,
1588c2ecf20Sopenharmony_ci	.irq_chip	= &dummy_irq_chip,
1598c2ecf20Sopenharmony_ci	.status_reg	= SPEAR320_INT_STS_MASK_REG,
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic struct spear_shirq spear320_shirq_ras2 = {
1638c2ecf20Sopenharmony_ci	.offset		= 10,
1648c2ecf20Sopenharmony_ci	.nr_irqs	= 1,
1658c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 1) - 1) << 10,
1668c2ecf20Sopenharmony_ci	.irq_chip	= &dummy_irq_chip,
1678c2ecf20Sopenharmony_ci	.status_reg	= SPEAR320_INT_STS_MASK_REG,
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic struct spear_shirq spear320_shirq_intrcomm_ras = {
1718c2ecf20Sopenharmony_ci	.offset		= 11,
1728c2ecf20Sopenharmony_ci	.nr_irqs	= 11,
1738c2ecf20Sopenharmony_ci	.mask		= ((0x1 << 11) - 1) << 11,
1748c2ecf20Sopenharmony_ci	.irq_chip	= &dummy_irq_chip,
1758c2ecf20Sopenharmony_ci	.status_reg	= SPEAR320_INT_STS_MASK_REG,
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic struct spear_shirq *spear320_shirq_blocks[] = {
1798c2ecf20Sopenharmony_ci	&spear320_shirq_ras3,
1808c2ecf20Sopenharmony_ci	&spear320_shirq_ras1,
1818c2ecf20Sopenharmony_ci	&spear320_shirq_ras2,
1828c2ecf20Sopenharmony_ci	&spear320_shirq_intrcomm_ras,
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic void shirq_handler(struct irq_desc *desc)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct spear_shirq *shirq = irq_desc_get_handler_data(desc);
1888c2ecf20Sopenharmony_ci	u32 pend;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	pend = readl(shirq->base + shirq->status_reg) & shirq->mask;
1918c2ecf20Sopenharmony_ci	pend >>= shirq->offset;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	while (pend) {
1948c2ecf20Sopenharmony_ci		int irq = __ffs(pend);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		pend &= ~(0x1 << irq);
1978c2ecf20Sopenharmony_ci		generic_handle_irq(shirq->virq_base + irq);
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic void __init spear_shirq_register(struct spear_shirq *shirq,
2028c2ecf20Sopenharmony_ci					int parent_irq)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	int i;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	if (!shirq->irq_chip)
2078c2ecf20Sopenharmony_ci		return;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	irq_set_chained_handler_and_data(parent_irq, shirq_handler, shirq);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	for (i = 0; i < shirq->nr_irqs; i++) {
2128c2ecf20Sopenharmony_ci		irq_set_chip_and_handler(shirq->virq_base + i,
2138c2ecf20Sopenharmony_ci					 shirq->irq_chip, handle_simple_irq);
2148c2ecf20Sopenharmony_ci		irq_set_chip_data(shirq->virq_base + i, shirq);
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic int __init shirq_init(struct spear_shirq **shirq_blocks, int block_nr,
2198c2ecf20Sopenharmony_ci		struct device_node *np)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	int i, parent_irq, virq_base, hwirq = 0, nr_irqs = 0;
2228c2ecf20Sopenharmony_ci	struct irq_domain *shirq_domain;
2238c2ecf20Sopenharmony_ci	void __iomem *base;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	base = of_iomap(np, 0);
2268c2ecf20Sopenharmony_ci	if (!base) {
2278c2ecf20Sopenharmony_ci		pr_err("%s: failed to map shirq registers\n", __func__);
2288c2ecf20Sopenharmony_ci		return -ENXIO;
2298c2ecf20Sopenharmony_ci	}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	for (i = 0; i < block_nr; i++)
2328c2ecf20Sopenharmony_ci		nr_irqs += shirq_blocks[i]->nr_irqs;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	virq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
2358c2ecf20Sopenharmony_ci	if (virq_base < 0) {
2368c2ecf20Sopenharmony_ci		pr_err("%s: irq desc alloc failed\n", __func__);
2378c2ecf20Sopenharmony_ci		goto err_unmap;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	shirq_domain = irq_domain_add_legacy(np, nr_irqs, virq_base, 0,
2418c2ecf20Sopenharmony_ci			&irq_domain_simple_ops, NULL);
2428c2ecf20Sopenharmony_ci	if (WARN_ON(!shirq_domain)) {
2438c2ecf20Sopenharmony_ci		pr_warn("%s: irq domain init failed\n", __func__);
2448c2ecf20Sopenharmony_ci		goto err_free_desc;
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	for (i = 0; i < block_nr; i++) {
2488c2ecf20Sopenharmony_ci		shirq_blocks[i]->base = base;
2498c2ecf20Sopenharmony_ci		shirq_blocks[i]->virq_base = irq_find_mapping(shirq_domain,
2508c2ecf20Sopenharmony_ci				hwirq);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci		parent_irq = irq_of_parse_and_map(np, i);
2538c2ecf20Sopenharmony_ci		spear_shirq_register(shirq_blocks[i], parent_irq);
2548c2ecf20Sopenharmony_ci		hwirq += shirq_blocks[i]->nr_irqs;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return 0;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cierr_free_desc:
2608c2ecf20Sopenharmony_ci	irq_free_descs(virq_base, nr_irqs);
2618c2ecf20Sopenharmony_cierr_unmap:
2628c2ecf20Sopenharmony_ci	iounmap(base);
2638c2ecf20Sopenharmony_ci	return -ENXIO;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int __init spear300_shirq_of_init(struct device_node *np,
2678c2ecf20Sopenharmony_ci					 struct device_node *parent)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	return shirq_init(spear300_shirq_blocks,
2708c2ecf20Sopenharmony_ci			ARRAY_SIZE(spear300_shirq_blocks), np);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(spear300_shirq, "st,spear300-shirq", spear300_shirq_of_init);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic int __init spear310_shirq_of_init(struct device_node *np,
2758c2ecf20Sopenharmony_ci					 struct device_node *parent)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	return shirq_init(spear310_shirq_blocks,
2788c2ecf20Sopenharmony_ci			ARRAY_SIZE(spear310_shirq_blocks), np);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(spear310_shirq, "st,spear310-shirq", spear310_shirq_of_init);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int __init spear320_shirq_of_init(struct device_node *np,
2838c2ecf20Sopenharmony_ci					 struct device_node *parent)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	return shirq_init(spear320_shirq_blocks,
2868c2ecf20Sopenharmony_ci			ARRAY_SIZE(spear320_shirq_blocks), np);
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(spear320_shirq, "st,spear320-shirq", spear320_shirq_of_init);
289