18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Atmel AT91 AIC (Advanced Interrupt Controller) driver
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *  Copyright (C) 2004 SAN People
58c2ecf20Sopenharmony_ci *  Copyright (C) 2004 ATMEL
68c2ecf20Sopenharmony_ci *  Copyright (C) Rick Bronson
78c2ecf20Sopenharmony_ci *  Copyright (C) 2014 Free Electrons
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public
128c2ecf20Sopenharmony_ci * License version 2.  This program is licensed "as is" without any
138c2ecf20Sopenharmony_ci * warranty of any kind, whether express or implied.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/mm.h>
198c2ecf20Sopenharmony_ci#include <linux/bitmap.h>
208c2ecf20Sopenharmony_ci#include <linux/types.h>
218c2ecf20Sopenharmony_ci#include <linux/irq.h>
228c2ecf20Sopenharmony_ci#include <linux/irqchip.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/irqdomain.h>
278c2ecf20Sopenharmony_ci#include <linux/err.h>
288c2ecf20Sopenharmony_ci#include <linux/slab.h>
298c2ecf20Sopenharmony_ci#include <linux/io.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <asm/exception.h>
328c2ecf20Sopenharmony_ci#include <asm/mach/irq.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include "irq-atmel-aic-common.h"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Number of irq lines managed by AIC */
378c2ecf20Sopenharmony_ci#define NR_AIC_IRQS	32
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define AT91_AIC_SMR(n)			((n) * 4)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define AT91_AIC_SVR(n)			(0x80 + ((n) * 4))
428c2ecf20Sopenharmony_ci#define AT91_AIC_IVR			0x100
438c2ecf20Sopenharmony_ci#define AT91_AIC_FVR			0x104
448c2ecf20Sopenharmony_ci#define AT91_AIC_ISR			0x108
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define AT91_AIC_IPR			0x10c
478c2ecf20Sopenharmony_ci#define AT91_AIC_IMR			0x110
488c2ecf20Sopenharmony_ci#define AT91_AIC_CISR			0x114
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define AT91_AIC_IECR			0x120
518c2ecf20Sopenharmony_ci#define AT91_AIC_IDCR			0x124
528c2ecf20Sopenharmony_ci#define AT91_AIC_ICCR			0x128
538c2ecf20Sopenharmony_ci#define AT91_AIC_ISCR			0x12c
548c2ecf20Sopenharmony_ci#define AT91_AIC_EOICR			0x130
558c2ecf20Sopenharmony_ci#define AT91_AIC_SPU			0x134
568c2ecf20Sopenharmony_ci#define AT91_AIC_DCR			0x138
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic struct irq_domain *aic_domain;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic asmlinkage void __exception_irq_entry
618c2ecf20Sopenharmony_ciaic_handle(struct pt_regs *regs)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct irq_domain_chip_generic *dgc = aic_domain->gc;
648c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = dgc->gc[0];
658c2ecf20Sopenharmony_ci	u32 irqnr;
668c2ecf20Sopenharmony_ci	u32 irqstat;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	irqnr = irq_reg_readl(gc, AT91_AIC_IVR);
698c2ecf20Sopenharmony_ci	irqstat = irq_reg_readl(gc, AT91_AIC_ISR);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (!irqstat)
728c2ecf20Sopenharmony_ci		irq_reg_writel(gc, 0, AT91_AIC_EOICR);
738c2ecf20Sopenharmony_ci	else
748c2ecf20Sopenharmony_ci		handle_domain_irq(aic_domain, irqnr, regs);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int aic_retrigger(struct irq_data *d)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* Enable interrupt on AIC5 */
828c2ecf20Sopenharmony_ci	irq_gc_lock(gc);
838c2ecf20Sopenharmony_ci	irq_reg_writel(gc, d->mask, AT91_AIC_ISCR);
848c2ecf20Sopenharmony_ci	irq_gc_unlock(gc);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return 1;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int aic_set_type(struct irq_data *d, unsigned type)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
928c2ecf20Sopenharmony_ci	unsigned int smr;
938c2ecf20Sopenharmony_ci	int ret;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	smr = irq_reg_readl(gc, AT91_AIC_SMR(d->hwirq));
968c2ecf20Sopenharmony_ci	ret = aic_common_set_type(d, type, &smr);
978c2ecf20Sopenharmony_ci	if (ret)
988c2ecf20Sopenharmony_ci		return ret;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	irq_reg_writel(gc, smr, AT91_AIC_SMR(d->hwirq));
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	return 0;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
1068c2ecf20Sopenharmony_cistatic void aic_suspend(struct irq_data *d)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	irq_gc_lock(gc);
1118c2ecf20Sopenharmony_ci	irq_reg_writel(gc, gc->mask_cache, AT91_AIC_IDCR);
1128c2ecf20Sopenharmony_ci	irq_reg_writel(gc, gc->wake_active, AT91_AIC_IECR);
1138c2ecf20Sopenharmony_ci	irq_gc_unlock(gc);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void aic_resume(struct irq_data *d)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	irq_gc_lock(gc);
1218c2ecf20Sopenharmony_ci	irq_reg_writel(gc, gc->wake_active, AT91_AIC_IDCR);
1228c2ecf20Sopenharmony_ci	irq_reg_writel(gc, gc->mask_cache, AT91_AIC_IECR);
1238c2ecf20Sopenharmony_ci	irq_gc_unlock(gc);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic void aic_pm_shutdown(struct irq_data *d)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	irq_gc_lock(gc);
1318c2ecf20Sopenharmony_ci	irq_reg_writel(gc, 0xffffffff, AT91_AIC_IDCR);
1328c2ecf20Sopenharmony_ci	irq_reg_writel(gc, 0xffffffff, AT91_AIC_ICCR);
1338c2ecf20Sopenharmony_ci	irq_gc_unlock(gc);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci#else
1368c2ecf20Sopenharmony_ci#define aic_suspend		NULL
1378c2ecf20Sopenharmony_ci#define aic_resume		NULL
1388c2ecf20Sopenharmony_ci#define aic_pm_shutdown		NULL
1398c2ecf20Sopenharmony_ci#endif /* CONFIG_PM */
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic void __init aic_hw_init(struct irq_domain *domain)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_get_domain_generic_chip(domain, 0);
1448c2ecf20Sopenharmony_ci	int i;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/*
1478c2ecf20Sopenharmony_ci	 * Perform 8 End Of Interrupt Command to make sure AIC
1488c2ecf20Sopenharmony_ci	 * will not Lock out nIRQ
1498c2ecf20Sopenharmony_ci	 */
1508c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++)
1518c2ecf20Sopenharmony_ci		irq_reg_writel(gc, 0, AT91_AIC_EOICR);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	/*
1548c2ecf20Sopenharmony_ci	 * Spurious Interrupt ID in Spurious Vector Register.
1558c2ecf20Sopenharmony_ci	 * When there is no current interrupt, the IRQ Vector Register
1568c2ecf20Sopenharmony_ci	 * reads the value stored in AIC_SPU
1578c2ecf20Sopenharmony_ci	 */
1588c2ecf20Sopenharmony_ci	irq_reg_writel(gc, 0xffffffff, AT91_AIC_SPU);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* No debugging in AIC: Debug (Protect) Control Register */
1618c2ecf20Sopenharmony_ci	irq_reg_writel(gc, 0, AT91_AIC_DCR);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	/* Disable and clear all interrupts initially */
1648c2ecf20Sopenharmony_ci	irq_reg_writel(gc, 0xffffffff, AT91_AIC_IDCR);
1658c2ecf20Sopenharmony_ci	irq_reg_writel(gc, 0xffffffff, AT91_AIC_ICCR);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	for (i = 0; i < 32; i++)
1688c2ecf20Sopenharmony_ci		irq_reg_writel(gc, i, AT91_AIC_SVR(i));
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int aic_irq_domain_xlate(struct irq_domain *d,
1728c2ecf20Sopenharmony_ci				struct device_node *ctrlr,
1738c2ecf20Sopenharmony_ci				const u32 *intspec, unsigned int intsize,
1748c2ecf20Sopenharmony_ci				irq_hw_number_t *out_hwirq,
1758c2ecf20Sopenharmony_ci				unsigned int *out_type)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct irq_domain_chip_generic *dgc = d->gc;
1788c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc;
1798c2ecf20Sopenharmony_ci	unsigned long flags;
1808c2ecf20Sopenharmony_ci	unsigned smr;
1818c2ecf20Sopenharmony_ci	int idx;
1828c2ecf20Sopenharmony_ci	int ret;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (!dgc)
1858c2ecf20Sopenharmony_ci		return -EINVAL;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	ret = aic_common_irq_domain_xlate(d, ctrlr, intspec, intsize,
1888c2ecf20Sopenharmony_ci					  out_hwirq, out_type);
1898c2ecf20Sopenharmony_ci	if (ret)
1908c2ecf20Sopenharmony_ci		return ret;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	idx = intspec[0] / dgc->irqs_per_chip;
1938c2ecf20Sopenharmony_ci	if (idx >= dgc->num_chips)
1948c2ecf20Sopenharmony_ci		return -EINVAL;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	gc = dgc->gc[idx];
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	irq_gc_lock_irqsave(gc, flags);
1998c2ecf20Sopenharmony_ci	smr = irq_reg_readl(gc, AT91_AIC_SMR(*out_hwirq));
2008c2ecf20Sopenharmony_ci	aic_common_set_priority(intspec[2], &smr);
2018c2ecf20Sopenharmony_ci	irq_reg_writel(gc, smr, AT91_AIC_SMR(*out_hwirq));
2028c2ecf20Sopenharmony_ci	irq_gc_unlock_irqrestore(gc, flags);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	return ret;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic const struct irq_domain_ops aic_irq_ops = {
2088c2ecf20Sopenharmony_ci	.map	= irq_map_generic_chip,
2098c2ecf20Sopenharmony_ci	.xlate	= aic_irq_domain_xlate,
2108c2ecf20Sopenharmony_ci};
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic void __init at91rm9200_aic_irq_fixup(void)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	aic_common_rtc_irq_fixup();
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic void __init at91sam9260_aic_irq_fixup(void)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	aic_common_rtt_irq_fixup();
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic void __init at91sam9g45_aic_irq_fixup(void)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	aic_common_rtc_irq_fixup();
2258c2ecf20Sopenharmony_ci	aic_common_rtt_irq_fixup();
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic const struct of_device_id aic_irq_fixups[] __initconst = {
2298c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91rm9200", .data = at91rm9200_aic_irq_fixup },
2308c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91sam9g45", .data = at91sam9g45_aic_irq_fixup },
2318c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91sam9n12", .data = at91rm9200_aic_irq_fixup },
2328c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91sam9rl", .data = at91sam9g45_aic_irq_fixup },
2338c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91sam9x5", .data = at91rm9200_aic_irq_fixup },
2348c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91sam9260", .data = at91sam9260_aic_irq_fixup },
2358c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91sam9261", .data = at91sam9260_aic_irq_fixup },
2368c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91sam9263", .data = at91sam9260_aic_irq_fixup },
2378c2ecf20Sopenharmony_ci	{ .compatible = "atmel,at91sam9g20", .data = at91sam9260_aic_irq_fixup },
2388c2ecf20Sopenharmony_ci	{ /* sentinel */ },
2398c2ecf20Sopenharmony_ci};
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic int __init aic_of_init(struct device_node *node,
2428c2ecf20Sopenharmony_ci			      struct device_node *parent)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc;
2458c2ecf20Sopenharmony_ci	struct irq_domain *domain;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	if (aic_domain)
2488c2ecf20Sopenharmony_ci		return -EEXIST;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	domain = aic_common_of_init(node, &aic_irq_ops, "atmel-aic",
2518c2ecf20Sopenharmony_ci				    NR_AIC_IRQS, aic_irq_fixups);
2528c2ecf20Sopenharmony_ci	if (IS_ERR(domain))
2538c2ecf20Sopenharmony_ci		return PTR_ERR(domain);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	aic_domain = domain;
2568c2ecf20Sopenharmony_ci	gc = irq_get_domain_generic_chip(domain, 0);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	gc->chip_types[0].regs.eoi = AT91_AIC_EOICR;
2598c2ecf20Sopenharmony_ci	gc->chip_types[0].regs.enable = AT91_AIC_IECR;
2608c2ecf20Sopenharmony_ci	gc->chip_types[0].regs.disable = AT91_AIC_IDCR;
2618c2ecf20Sopenharmony_ci	gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg;
2628c2ecf20Sopenharmony_ci	gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg;
2638c2ecf20Sopenharmony_ci	gc->chip_types[0].chip.irq_retrigger = aic_retrigger;
2648c2ecf20Sopenharmony_ci	gc->chip_types[0].chip.irq_set_type = aic_set_type;
2658c2ecf20Sopenharmony_ci	gc->chip_types[0].chip.irq_suspend = aic_suspend;
2668c2ecf20Sopenharmony_ci	gc->chip_types[0].chip.irq_resume = aic_resume;
2678c2ecf20Sopenharmony_ci	gc->chip_types[0].chip.irq_pm_shutdown = aic_pm_shutdown;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	aic_hw_init(domain);
2708c2ecf20Sopenharmony_ci	set_handle_irq(aic_handle);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return 0;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ciIRQCHIP_DECLARE(at91rm9200_aic, "atmel,at91rm9200-aic", aic_of_init);
275