18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
48c2ecf20Sopenharmony_ci * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/irq.h>
98c2ecf20Sopenharmony_ci#include <linux/irqdomain.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/of.h>
128c2ecf20Sopenharmony_ci#include <linux/of_address.h>
138c2ecf20Sopenharmony_ci#include <asm/mach/irq.h>
148c2ecf20Sopenharmony_ci#include <asm/exception.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "common.h"
178c2ecf20Sopenharmony_ci#include "hardware.h"
188c2ecf20Sopenharmony_ci#include "irq-common.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define AVIC_INTCNTL		0x00	/* int control reg */
218c2ecf20Sopenharmony_ci#define AVIC_NIMASK		0x04	/* int mask reg */
228c2ecf20Sopenharmony_ci#define AVIC_INTENNUM		0x08	/* int enable number reg */
238c2ecf20Sopenharmony_ci#define AVIC_INTDISNUM		0x0C	/* int disable number reg */
248c2ecf20Sopenharmony_ci#define AVIC_INTENABLEH		0x10	/* int enable reg high */
258c2ecf20Sopenharmony_ci#define AVIC_INTENABLEL		0x14	/* int enable reg low */
268c2ecf20Sopenharmony_ci#define AVIC_INTTYPEH		0x18	/* int type reg high */
278c2ecf20Sopenharmony_ci#define AVIC_INTTYPEL		0x1C	/* int type reg low */
288c2ecf20Sopenharmony_ci#define AVIC_NIPRIORITY(x)	(0x20 + 4 * (7 - (x))) /* int priority */
298c2ecf20Sopenharmony_ci#define AVIC_NIVECSR		0x40	/* norm int vector/status */
308c2ecf20Sopenharmony_ci#define AVIC_FIVECSR		0x44	/* fast int vector/status */
318c2ecf20Sopenharmony_ci#define AVIC_INTSRCH		0x48	/* int source reg high */
328c2ecf20Sopenharmony_ci#define AVIC_INTSRCL		0x4C	/* int source reg low */
338c2ecf20Sopenharmony_ci#define AVIC_INTFRCH		0x50	/* int force reg high */
348c2ecf20Sopenharmony_ci#define AVIC_INTFRCL		0x54	/* int force reg low */
358c2ecf20Sopenharmony_ci#define AVIC_NIPNDH		0x58	/* norm int pending high */
368c2ecf20Sopenharmony_ci#define AVIC_NIPNDL		0x5C	/* norm int pending low */
378c2ecf20Sopenharmony_ci#define AVIC_FIPNDH		0x60	/* fast int pending high */
388c2ecf20Sopenharmony_ci#define AVIC_FIPNDL		0x64	/* fast int pending low */
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define AVIC_NUM_IRQS 64
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* low power interrupt mask registers */
438c2ecf20Sopenharmony_ci#define MX25_CCM_LPIMR0	0x68
448c2ecf20Sopenharmony_ci#define MX25_CCM_LPIMR1	0x6C
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic void __iomem *avic_base;
478c2ecf20Sopenharmony_cistatic void __iomem *mx25_ccm_base;
488c2ecf20Sopenharmony_cistatic struct irq_domain *domain;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#ifdef CONFIG_FIQ
518c2ecf20Sopenharmony_cistatic int avic_set_irq_fiq(unsigned int hwirq, unsigned int type)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	unsigned int irqt;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	if (hwirq >= AVIC_NUM_IRQS)
568c2ecf20Sopenharmony_ci		return -EINVAL;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (hwirq < AVIC_NUM_IRQS / 2) {
598c2ecf20Sopenharmony_ci		irqt = imx_readl(avic_base + AVIC_INTTYPEL) & ~(1 << hwirq);
608c2ecf20Sopenharmony_ci		imx_writel(irqt | (!!type << hwirq), avic_base + AVIC_INTTYPEL);
618c2ecf20Sopenharmony_ci	} else {
628c2ecf20Sopenharmony_ci		hwirq -= AVIC_NUM_IRQS / 2;
638c2ecf20Sopenharmony_ci		irqt = imx_readl(avic_base + AVIC_INTTYPEH) & ~(1 << hwirq);
648c2ecf20Sopenharmony_ci		imx_writel(irqt | (!!type << hwirq), avic_base + AVIC_INTTYPEH);
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return 0;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci#endif /* CONFIG_FIQ */
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic struct mxc_extra_irq avic_extra_irq = {
738c2ecf20Sopenharmony_ci#ifdef CONFIG_FIQ
748c2ecf20Sopenharmony_ci	.set_irq_fiq = avic_set_irq_fiq,
758c2ecf20Sopenharmony_ci#endif
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
798c2ecf20Sopenharmony_cistatic u32 avic_saved_mask_reg[2];
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic void avic_irq_suspend(struct irq_data *d)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
848c2ecf20Sopenharmony_ci	struct irq_chip_type *ct = gc->chip_types;
858c2ecf20Sopenharmony_ci	int idx = d->hwirq >> 5;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	avic_saved_mask_reg[idx] = imx_readl(avic_base + ct->regs.mask);
888c2ecf20Sopenharmony_ci	imx_writel(gc->wake_active, avic_base + ct->regs.mask);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if (mx25_ccm_base) {
918c2ecf20Sopenharmony_ci		u8 offs = d->hwirq < AVIC_NUM_IRQS / 2 ?
928c2ecf20Sopenharmony_ci			MX25_CCM_LPIMR0 : MX25_CCM_LPIMR1;
938c2ecf20Sopenharmony_ci		/*
948c2ecf20Sopenharmony_ci		 * The interrupts which are still enabled will be used as wakeup
958c2ecf20Sopenharmony_ci		 * sources. Allow those interrupts in low-power mode.
968c2ecf20Sopenharmony_ci		 * The LPIMR registers use 0 to allow an interrupt, the AVIC
978c2ecf20Sopenharmony_ci		 * registers use 1.
988c2ecf20Sopenharmony_ci		 */
998c2ecf20Sopenharmony_ci		imx_writel(~gc->wake_active, mx25_ccm_base + offs);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic void avic_irq_resume(struct irq_data *d)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1068c2ecf20Sopenharmony_ci	struct irq_chip_type *ct = gc->chip_types;
1078c2ecf20Sopenharmony_ci	int idx = d->hwirq >> 5;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	imx_writel(avic_saved_mask_reg[idx], avic_base + ct->regs.mask);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (mx25_ccm_base) {
1128c2ecf20Sopenharmony_ci		u8 offs = d->hwirq < AVIC_NUM_IRQS / 2 ?
1138c2ecf20Sopenharmony_ci			MX25_CCM_LPIMR0 : MX25_CCM_LPIMR1;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci		imx_writel(0xffffffff, mx25_ccm_base + offs);
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#else
1208c2ecf20Sopenharmony_ci#define avic_irq_suspend NULL
1218c2ecf20Sopenharmony_ci#define avic_irq_resume NULL
1228c2ecf20Sopenharmony_ci#endif
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic __init void avic_init_gc(int idx, unsigned int irq_start)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct irq_chip_generic *gc;
1278c2ecf20Sopenharmony_ci	struct irq_chip_type *ct;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	gc = irq_alloc_generic_chip("mxc-avic", 1, irq_start, avic_base,
1308c2ecf20Sopenharmony_ci				    handle_level_irq);
1318c2ecf20Sopenharmony_ci	gc->private = &avic_extra_irq;
1328c2ecf20Sopenharmony_ci	gc->wake_enabled = IRQ_MSK(32);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	ct = gc->chip_types;
1358c2ecf20Sopenharmony_ci	ct->chip.irq_mask = irq_gc_mask_clr_bit;
1368c2ecf20Sopenharmony_ci	ct->chip.irq_unmask = irq_gc_mask_set_bit;
1378c2ecf20Sopenharmony_ci	ct->chip.irq_ack = irq_gc_mask_clr_bit;
1388c2ecf20Sopenharmony_ci	ct->chip.irq_set_wake = irq_gc_set_wake;
1398c2ecf20Sopenharmony_ci	ct->chip.irq_suspend = avic_irq_suspend;
1408c2ecf20Sopenharmony_ci	ct->chip.irq_resume = avic_irq_resume;
1418c2ecf20Sopenharmony_ci	ct->regs.mask = !idx ? AVIC_INTENABLEL : AVIC_INTENABLEH;
1428c2ecf20Sopenharmony_ci	ct->regs.ack = ct->regs.mask;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	irq_setup_generic_chip(gc, IRQ_MSK(32), 0, IRQ_NOREQUEST, 0);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic void __exception_irq_entry avic_handle_irq(struct pt_regs *regs)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	u32 nivector;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	do {
1528c2ecf20Sopenharmony_ci		nivector = imx_readl(avic_base + AVIC_NIVECSR) >> 16;
1538c2ecf20Sopenharmony_ci		if (nivector == 0xffff)
1548c2ecf20Sopenharmony_ci			break;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		handle_domain_irq(domain, nivector, regs);
1578c2ecf20Sopenharmony_ci	} while (1);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/*
1618c2ecf20Sopenharmony_ci * This function initializes the AVIC hardware and disables all the
1628c2ecf20Sopenharmony_ci * interrupts. It registers the interrupt enable and disable functions
1638c2ecf20Sopenharmony_ci * to the kernel for each interrupt source.
1648c2ecf20Sopenharmony_ci */
1658c2ecf20Sopenharmony_civoid __init mxc_init_irq(void __iomem *irqbase)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct device_node *np;
1688c2ecf20Sopenharmony_ci	int irq_base;
1698c2ecf20Sopenharmony_ci	int i;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	avic_base = irqbase;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "fsl,imx25-ccm");
1748c2ecf20Sopenharmony_ci	mx25_ccm_base = of_iomap(np, 0);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (mx25_ccm_base) {
1778c2ecf20Sopenharmony_ci		/*
1788c2ecf20Sopenharmony_ci		 * By default, we mask all interrupts. We set the actual mask
1798c2ecf20Sopenharmony_ci		 * before we go into low-power mode.
1808c2ecf20Sopenharmony_ci		 */
1818c2ecf20Sopenharmony_ci		imx_writel(0xffffffff, mx25_ccm_base + MX25_CCM_LPIMR0);
1828c2ecf20Sopenharmony_ci		imx_writel(0xffffffff, mx25_ccm_base + MX25_CCM_LPIMR1);
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* put the AVIC into the reset value with
1868c2ecf20Sopenharmony_ci	 * all interrupts disabled
1878c2ecf20Sopenharmony_ci	 */
1888c2ecf20Sopenharmony_ci	imx_writel(0, avic_base + AVIC_INTCNTL);
1898c2ecf20Sopenharmony_ci	imx_writel(0x1f, avic_base + AVIC_NIMASK);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/* disable all interrupts */
1928c2ecf20Sopenharmony_ci	imx_writel(0, avic_base + AVIC_INTENABLEH);
1938c2ecf20Sopenharmony_ci	imx_writel(0, avic_base + AVIC_INTENABLEL);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	/* all IRQ no FIQ */
1968c2ecf20Sopenharmony_ci	imx_writel(0, avic_base + AVIC_INTTYPEH);
1978c2ecf20Sopenharmony_ci	imx_writel(0, avic_base + AVIC_INTTYPEL);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	irq_base = irq_alloc_descs(-1, 0, AVIC_NUM_IRQS, numa_node_id());
2008c2ecf20Sopenharmony_ci	WARN_ON(irq_base < 0);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "fsl,avic");
2038c2ecf20Sopenharmony_ci	domain = irq_domain_add_legacy(np, AVIC_NUM_IRQS, irq_base, 0,
2048c2ecf20Sopenharmony_ci				       &irq_domain_simple_ops, NULL);
2058c2ecf20Sopenharmony_ci	WARN_ON(!domain);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	for (i = 0; i < AVIC_NUM_IRQS / 32; i++, irq_base += 32)
2088c2ecf20Sopenharmony_ci		avic_init_gc(i, irq_base);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Set default priority value (0) for all IRQ's */
2118c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++)
2128c2ecf20Sopenharmony_ci		imx_writel(0, avic_base + AVIC_NIPRIORITY(i));
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	set_handle_irq(avic_handle_irq);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci#ifdef CONFIG_FIQ
2178c2ecf20Sopenharmony_ci	/* Initialize FIQ */
2188c2ecf20Sopenharmony_ci	init_FIQ(FIQ_START);
2198c2ecf20Sopenharmony_ci#endif
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	printk(KERN_INFO "MXC IRQ initialized\n");
2228c2ecf20Sopenharmony_ci}
223