18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * Programmable Interrupt Controller functions for the Freescale MPC52xx.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Secret Lab Technologies Ltd.
68c2ecf20Sopenharmony_ci * Copyright (C) 2006 bplan GmbH
78c2ecf20Sopenharmony_ci * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
88c2ecf20Sopenharmony_ci * Copyright (C) 2003 Montavista Software, Inc
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Based on the code from the 2.4 kernel by
118c2ecf20Sopenharmony_ci * Dale Farnsworth <dfarnsworth@mvista.com> and Kent Borg.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public License
148c2ecf20Sopenharmony_ci * version 2. This program is licensed "as is" without any warranty of any
158c2ecf20Sopenharmony_ci * kind, whether express or implied.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * This is the device driver for the MPC5200 interrupt controller.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * hardware overview
238c2ecf20Sopenharmony_ci * -----------------
248c2ecf20Sopenharmony_ci * The MPC5200 interrupt controller groups the all interrupt sources into
258c2ecf20Sopenharmony_ci * three groups called 'critical', 'main', and 'peripheral'.  The critical
268c2ecf20Sopenharmony_ci * group has 3 irqs, External IRQ0, slice timer 0 irq, and wake from deep
278c2ecf20Sopenharmony_ci * sleep.  Main group include the other 3 external IRQs, slice timer 1, RTC,
288c2ecf20Sopenharmony_ci * gpios, and the general purpose timers.  Peripheral group contains the
298c2ecf20Sopenharmony_ci * remaining irq sources from all of the on-chip peripherals (PSCs, Ethernet,
308c2ecf20Sopenharmony_ci * USB, DMA, etc).
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * virqs
338c2ecf20Sopenharmony_ci * -----
348c2ecf20Sopenharmony_ci * The Linux IRQ subsystem requires that each irq source be assigned a
358c2ecf20Sopenharmony_ci * system wide unique IRQ number starting at 1 (0 means no irq).  Since
368c2ecf20Sopenharmony_ci * systems can have multiple interrupt controllers, the virtual IRQ (virq)
378c2ecf20Sopenharmony_ci * infrastructure lets each interrupt controller to define a local set
388c2ecf20Sopenharmony_ci * of IRQ numbers and the virq infrastructure maps those numbers into
398c2ecf20Sopenharmony_ci * a unique range of the global IRQ# space.
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * To define a range of virq numbers for this controller, this driver first
428c2ecf20Sopenharmony_ci * assigns a number to each of the irq groups (called the level 1 or L1
438c2ecf20Sopenharmony_ci * value).  Within each group individual irq sources are also assigned a
448c2ecf20Sopenharmony_ci * number, as defined by the MPC5200 user guide, and refers to it as the
458c2ecf20Sopenharmony_ci * level 2 or L2 value.  The virq number is determined by shifting up the
468c2ecf20Sopenharmony_ci * L1 value by MPC52xx_IRQ_L1_OFFSET and ORing it with the L2 value.
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * For example, the TMR0 interrupt is irq 9 in the main group.  The
498c2ecf20Sopenharmony_ci * virq for TMR0 is calculated by ((1 << MPC52xx_IRQ_L1_OFFSET) | 9).
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * The observant reader will also notice that this driver defines a 4th
528c2ecf20Sopenharmony_ci * interrupt group called 'bestcomm'.  The bestcomm group isn't physically
538c2ecf20Sopenharmony_ci * part of the MPC5200 interrupt controller, but it is used here to assign
548c2ecf20Sopenharmony_ci * a separate virq number for each bestcomm task (since any of the 16
558c2ecf20Sopenharmony_ci * bestcomm tasks can cause the bestcomm interrupt to be raised).  When a
568c2ecf20Sopenharmony_ci * bestcomm interrupt occurs (peripheral group, irq 0) this driver determines
578c2ecf20Sopenharmony_ci * which task needs servicing and returns the irq number for that task.  This
588c2ecf20Sopenharmony_ci * allows drivers which use bestcomm to define their own interrupt handlers.
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * irq_chip structures
618c2ecf20Sopenharmony_ci * -------------------
628c2ecf20Sopenharmony_ci * For actually manipulating IRQs (masking, enabling, clearing, etc) this
638c2ecf20Sopenharmony_ci * driver defines four separate 'irq_chip' structures, one for the main
648c2ecf20Sopenharmony_ci * group, one for the peripherals group, one for the bestcomm group and one
658c2ecf20Sopenharmony_ci * for external interrupts.  The irq_chip structures provide the hooks needed
668c2ecf20Sopenharmony_ci * to manipulate each IRQ source, and since each group is has a separate set
678c2ecf20Sopenharmony_ci * of registers for controlling the irq, it makes sense to divide up the
688c2ecf20Sopenharmony_ci * hooks along those lines.
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci * You'll notice that there is not an irq_chip for the critical group and
718c2ecf20Sopenharmony_ci * you'll also notice that there is an irq_chip defined for external
728c2ecf20Sopenharmony_ci * interrupts even though there is no external interrupt group.  The reason
738c2ecf20Sopenharmony_ci * for this is that the four external interrupts are all managed with the same
748c2ecf20Sopenharmony_ci * register even though one of the external IRQs is in the critical group and
758c2ecf20Sopenharmony_ci * the other three are in the main group.  For this reason it makes sense for
768c2ecf20Sopenharmony_ci * the 4 external irqs to be managed using a separate set of hooks.  The
778c2ecf20Sopenharmony_ci * reason there is no crit irq_chip is that of the 3 irqs in the critical
788c2ecf20Sopenharmony_ci * group, only external interrupt is actually support at this time by this
798c2ecf20Sopenharmony_ci * driver and since external interrupt is the only one used, it can just
808c2ecf20Sopenharmony_ci * be directed to make use of the external irq irq_chip.
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * device tree bindings
838c2ecf20Sopenharmony_ci * --------------------
848c2ecf20Sopenharmony_ci * The device tree bindings for this controller reflect the two level
858c2ecf20Sopenharmony_ci * organization of irqs in the device.  #interrupt-cells = <3> where the
868c2ecf20Sopenharmony_ci * first cell is the group number [0..3], the second cell is the irq
878c2ecf20Sopenharmony_ci * number in the group, and the third cell is the sense type (level/edge).
888c2ecf20Sopenharmony_ci * For reference, the following is a list of the interrupt property values
898c2ecf20Sopenharmony_ci * associated with external interrupt sources on the MPC5200 (just because
908c2ecf20Sopenharmony_ci * it is non-obvious to determine what the interrupts property should be
918c2ecf20Sopenharmony_ci * when reading the mpc5200 manual and it is a frequently asked question).
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * External interrupts:
948c2ecf20Sopenharmony_ci * <0 0 n>	external irq0, n is sense	(n=0: level high,
958c2ecf20Sopenharmony_ci * <1 1 n>	external irq1, n is sense	 n=1: edge rising,
968c2ecf20Sopenharmony_ci * <1 2 n>	external irq2, n is sense	 n=2: edge falling,
978c2ecf20Sopenharmony_ci * <1 3 n>	external irq3, n is sense	 n=3: level low)
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_ci#undef DEBUG
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
1028c2ecf20Sopenharmony_ci#include <linux/irq.h>
1038c2ecf20Sopenharmony_ci#include <linux/of.h>
1048c2ecf20Sopenharmony_ci#include <asm/io.h>
1058c2ecf20Sopenharmony_ci#include <asm/prom.h>
1068c2ecf20Sopenharmony_ci#include <asm/mpc52xx.h>
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* HW IRQ mapping */
1098c2ecf20Sopenharmony_ci#define MPC52xx_IRQ_L1_CRIT	(0)
1108c2ecf20Sopenharmony_ci#define MPC52xx_IRQ_L1_MAIN	(1)
1118c2ecf20Sopenharmony_ci#define MPC52xx_IRQ_L1_PERP	(2)
1128c2ecf20Sopenharmony_ci#define MPC52xx_IRQ_L1_SDMA	(3)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define MPC52xx_IRQ_L1_OFFSET	(6)
1158c2ecf20Sopenharmony_ci#define MPC52xx_IRQ_L1_MASK	(0x00c0)
1168c2ecf20Sopenharmony_ci#define MPC52xx_IRQ_L2_MASK	(0x003f)
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define MPC52xx_IRQ_HIGHTESTHWIRQ (0xd0)
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/* MPC5200 device tree match tables */
1228c2ecf20Sopenharmony_cistatic const struct of_device_id mpc52xx_pic_ids[] __initconst = {
1238c2ecf20Sopenharmony_ci	{ .compatible = "fsl,mpc5200-pic", },
1248c2ecf20Sopenharmony_ci	{ .compatible = "mpc5200-pic", },
1258c2ecf20Sopenharmony_ci	{}
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_cistatic const struct of_device_id mpc52xx_sdma_ids[] __initconst = {
1288c2ecf20Sopenharmony_ci	{ .compatible = "fsl,mpc5200-bestcomm", },
1298c2ecf20Sopenharmony_ci	{ .compatible = "mpc5200-bestcomm", },
1308c2ecf20Sopenharmony_ci	{}
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic struct mpc52xx_intr __iomem *intr;
1348c2ecf20Sopenharmony_cistatic struct mpc52xx_sdma __iomem *sdma;
1358c2ecf20Sopenharmony_cistatic struct irq_domain *mpc52xx_irqhost = NULL;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic unsigned char mpc52xx_map_senses[4] = {
1388c2ecf20Sopenharmony_ci	IRQ_TYPE_LEVEL_HIGH,
1398c2ecf20Sopenharmony_ci	IRQ_TYPE_EDGE_RISING,
1408c2ecf20Sopenharmony_ci	IRQ_TYPE_EDGE_FALLING,
1418c2ecf20Sopenharmony_ci	IRQ_TYPE_LEVEL_LOW,
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/* Utility functions */
1458c2ecf20Sopenharmony_cistatic inline void io_be_setbit(u32 __iomem *addr, int bitno)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	out_be32(addr, in_be32(addr) | (1 << bitno));
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic inline void io_be_clrbit(u32 __iomem *addr, int bitno)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	out_be32(addr, in_be32(addr) & ~(1 << bitno));
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/*
1568c2ecf20Sopenharmony_ci * IRQ[0-3] interrupt irq_chip
1578c2ecf20Sopenharmony_ci */
1588c2ecf20Sopenharmony_cistatic void mpc52xx_extirq_mask(struct irq_data *d)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
1618c2ecf20Sopenharmony_ci	io_be_clrbit(&intr->ctrl, 11 - l2irq);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void mpc52xx_extirq_unmask(struct irq_data *d)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
1678c2ecf20Sopenharmony_ci	io_be_setbit(&intr->ctrl, 11 - l2irq);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic void mpc52xx_extirq_ack(struct irq_data *d)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
1738c2ecf20Sopenharmony_ci	io_be_setbit(&intr->ctrl, 27-l2irq);
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic int mpc52xx_extirq_set_type(struct irq_data *d, unsigned int flow_type)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	u32 ctrl_reg, type;
1798c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
1808c2ecf20Sopenharmony_ci	void *handler = handle_level_irq;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__,
1838c2ecf20Sopenharmony_ci		(int) irqd_to_hwirq(d), l2irq, flow_type);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	switch (flow_type) {
1868c2ecf20Sopenharmony_ci	case IRQF_TRIGGER_HIGH: type = 0; break;
1878c2ecf20Sopenharmony_ci	case IRQF_TRIGGER_RISING: type = 1; handler = handle_edge_irq; break;
1888c2ecf20Sopenharmony_ci	case IRQF_TRIGGER_FALLING: type = 2; handler = handle_edge_irq; break;
1898c2ecf20Sopenharmony_ci	case IRQF_TRIGGER_LOW: type = 3; break;
1908c2ecf20Sopenharmony_ci	default:
1918c2ecf20Sopenharmony_ci		type = 0;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	ctrl_reg = in_be32(&intr->ctrl);
1958c2ecf20Sopenharmony_ci	ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));
1968c2ecf20Sopenharmony_ci	ctrl_reg |= (type << (22 - (l2irq * 2)));
1978c2ecf20Sopenharmony_ci	out_be32(&intr->ctrl, ctrl_reg);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	irq_set_handler_locked(d, handler);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	return 0;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic struct irq_chip mpc52xx_extirq_irqchip = {
2058c2ecf20Sopenharmony_ci	.name = "MPC52xx External",
2068c2ecf20Sopenharmony_ci	.irq_mask = mpc52xx_extirq_mask,
2078c2ecf20Sopenharmony_ci	.irq_unmask = mpc52xx_extirq_unmask,
2088c2ecf20Sopenharmony_ci	.irq_ack = mpc52xx_extirq_ack,
2098c2ecf20Sopenharmony_ci	.irq_set_type = mpc52xx_extirq_set_type,
2108c2ecf20Sopenharmony_ci};
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/*
2138c2ecf20Sopenharmony_ci * Main interrupt irq_chip
2148c2ecf20Sopenharmony_ci */
2158c2ecf20Sopenharmony_cistatic int mpc52xx_null_set_type(struct irq_data *d, unsigned int flow_type)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	return 0; /* Do nothing so that the sense mask will get updated */
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic void mpc52xx_main_mask(struct irq_data *d)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
2238c2ecf20Sopenharmony_ci	io_be_setbit(&intr->main_mask, 16 - l2irq);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic void mpc52xx_main_unmask(struct irq_data *d)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
2298c2ecf20Sopenharmony_ci	io_be_clrbit(&intr->main_mask, 16 - l2irq);
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic struct irq_chip mpc52xx_main_irqchip = {
2338c2ecf20Sopenharmony_ci	.name = "MPC52xx Main",
2348c2ecf20Sopenharmony_ci	.irq_mask = mpc52xx_main_mask,
2358c2ecf20Sopenharmony_ci	.irq_mask_ack = mpc52xx_main_mask,
2368c2ecf20Sopenharmony_ci	.irq_unmask = mpc52xx_main_unmask,
2378c2ecf20Sopenharmony_ci	.irq_set_type = mpc52xx_null_set_type,
2388c2ecf20Sopenharmony_ci};
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/*
2418c2ecf20Sopenharmony_ci * Peripherals interrupt irq_chip
2428c2ecf20Sopenharmony_ci */
2438c2ecf20Sopenharmony_cistatic void mpc52xx_periph_mask(struct irq_data *d)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
2468c2ecf20Sopenharmony_ci	io_be_setbit(&intr->per_mask, 31 - l2irq);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic void mpc52xx_periph_unmask(struct irq_data *d)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
2528c2ecf20Sopenharmony_ci	io_be_clrbit(&intr->per_mask, 31 - l2irq);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic struct irq_chip mpc52xx_periph_irqchip = {
2568c2ecf20Sopenharmony_ci	.name = "MPC52xx Peripherals",
2578c2ecf20Sopenharmony_ci	.irq_mask = mpc52xx_periph_mask,
2588c2ecf20Sopenharmony_ci	.irq_mask_ack = mpc52xx_periph_mask,
2598c2ecf20Sopenharmony_ci	.irq_unmask = mpc52xx_periph_unmask,
2608c2ecf20Sopenharmony_ci	.irq_set_type = mpc52xx_null_set_type,
2618c2ecf20Sopenharmony_ci};
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/*
2648c2ecf20Sopenharmony_ci * SDMA interrupt irq_chip
2658c2ecf20Sopenharmony_ci */
2668c2ecf20Sopenharmony_cistatic void mpc52xx_sdma_mask(struct irq_data *d)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
2698c2ecf20Sopenharmony_ci	io_be_setbit(&sdma->IntMask, l2irq);
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic void mpc52xx_sdma_unmask(struct irq_data *d)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
2758c2ecf20Sopenharmony_ci	io_be_clrbit(&sdma->IntMask, l2irq);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic void mpc52xx_sdma_ack(struct irq_data *d)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
2818c2ecf20Sopenharmony_ci	out_be32(&sdma->IntPend, 1 << l2irq);
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic struct irq_chip mpc52xx_sdma_irqchip = {
2858c2ecf20Sopenharmony_ci	.name = "MPC52xx SDMA",
2868c2ecf20Sopenharmony_ci	.irq_mask = mpc52xx_sdma_mask,
2878c2ecf20Sopenharmony_ci	.irq_unmask = mpc52xx_sdma_unmask,
2888c2ecf20Sopenharmony_ci	.irq_ack = mpc52xx_sdma_ack,
2898c2ecf20Sopenharmony_ci	.irq_set_type = mpc52xx_null_set_type,
2908c2ecf20Sopenharmony_ci};
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/**
2938c2ecf20Sopenharmony_ci * mpc52xx_is_extirq - Returns true if hwirq number is for an external IRQ
2948c2ecf20Sopenharmony_ci */
2958c2ecf20Sopenharmony_cistatic int mpc52xx_is_extirq(int l1, int l2)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	return ((l1 == 0) && (l2 == 0)) ||
2988c2ecf20Sopenharmony_ci	       ((l1 == 1) && (l2 >= 1) && (l2 <= 3));
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci/**
3028c2ecf20Sopenharmony_ci * mpc52xx_irqhost_xlate - translate virq# from device tree interrupts property
3038c2ecf20Sopenharmony_ci */
3048c2ecf20Sopenharmony_cistatic int mpc52xx_irqhost_xlate(struct irq_domain *h, struct device_node *ct,
3058c2ecf20Sopenharmony_ci				 const u32 *intspec, unsigned int intsize,
3068c2ecf20Sopenharmony_ci				 irq_hw_number_t *out_hwirq,
3078c2ecf20Sopenharmony_ci				 unsigned int *out_flags)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	int intrvect_l1;
3108c2ecf20Sopenharmony_ci	int intrvect_l2;
3118c2ecf20Sopenharmony_ci	int intrvect_type;
3128c2ecf20Sopenharmony_ci	int intrvect_linux;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (intsize != 3)
3158c2ecf20Sopenharmony_ci		return -1;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	intrvect_l1 = (int)intspec[0];
3188c2ecf20Sopenharmony_ci	intrvect_l2 = (int)intspec[1];
3198c2ecf20Sopenharmony_ci	intrvect_type = (int)intspec[2] & 0x3;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	intrvect_linux = (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) &
3228c2ecf20Sopenharmony_ci			 MPC52xx_IRQ_L1_MASK;
3238c2ecf20Sopenharmony_ci	intrvect_linux |= intrvect_l2 & MPC52xx_IRQ_L2_MASK;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	*out_hwirq = intrvect_linux;
3268c2ecf20Sopenharmony_ci	*out_flags = IRQ_TYPE_LEVEL_LOW;
3278c2ecf20Sopenharmony_ci	if (mpc52xx_is_extirq(intrvect_l1, intrvect_l2))
3288c2ecf20Sopenharmony_ci		*out_flags = mpc52xx_map_senses[intrvect_type];
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,
3318c2ecf20Sopenharmony_ci		 intrvect_l2);
3328c2ecf20Sopenharmony_ci	return 0;
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci/**
3368c2ecf20Sopenharmony_ci * mpc52xx_irqhost_map - Hook to map from virq to an irq_chip structure
3378c2ecf20Sopenharmony_ci */
3388c2ecf20Sopenharmony_cistatic int mpc52xx_irqhost_map(struct irq_domain *h, unsigned int virq,
3398c2ecf20Sopenharmony_ci			       irq_hw_number_t irq)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	int l1irq;
3428c2ecf20Sopenharmony_ci	int l2irq;
3438c2ecf20Sopenharmony_ci	struct irq_chip *irqchip;
3448c2ecf20Sopenharmony_ci	void *hndlr;
3458c2ecf20Sopenharmony_ci	int type;
3468c2ecf20Sopenharmony_ci	u32 reg;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;
3498c2ecf20Sopenharmony_ci	l2irq = irq & MPC52xx_IRQ_L2_MASK;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	/*
3528c2ecf20Sopenharmony_ci	 * External IRQs are handled differently by the hardware so they are
3538c2ecf20Sopenharmony_ci	 * handled by a dedicated irq_chip structure.
3548c2ecf20Sopenharmony_ci	 */
3558c2ecf20Sopenharmony_ci	if (mpc52xx_is_extirq(l1irq, l2irq)) {
3568c2ecf20Sopenharmony_ci		reg = in_be32(&intr->ctrl);
3578c2ecf20Sopenharmony_ci		type = mpc52xx_map_senses[(reg >> (22 - l2irq * 2)) & 0x3];
3588c2ecf20Sopenharmony_ci		if ((type == IRQ_TYPE_EDGE_FALLING) ||
3598c2ecf20Sopenharmony_ci		    (type == IRQ_TYPE_EDGE_RISING))
3608c2ecf20Sopenharmony_ci			hndlr = handle_edge_irq;
3618c2ecf20Sopenharmony_ci		else
3628c2ecf20Sopenharmony_ci			hndlr = handle_level_irq;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci		irq_set_chip_and_handler(virq, &mpc52xx_extirq_irqchip, hndlr);
3658c2ecf20Sopenharmony_ci		pr_debug("%s: External IRQ%i virq=%x, hw=%x. type=%x\n",
3668c2ecf20Sopenharmony_ci			 __func__, l2irq, virq, (int)irq, type);
3678c2ecf20Sopenharmony_ci		return 0;
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* It is an internal SOC irq.  Choose the correct irq_chip */
3718c2ecf20Sopenharmony_ci	switch (l1irq) {
3728c2ecf20Sopenharmony_ci	case MPC52xx_IRQ_L1_MAIN: irqchip = &mpc52xx_main_irqchip; break;
3738c2ecf20Sopenharmony_ci	case MPC52xx_IRQ_L1_PERP: irqchip = &mpc52xx_periph_irqchip; break;
3748c2ecf20Sopenharmony_ci	case MPC52xx_IRQ_L1_SDMA: irqchip = &mpc52xx_sdma_irqchip; break;
3758c2ecf20Sopenharmony_ci	case MPC52xx_IRQ_L1_CRIT:
3768c2ecf20Sopenharmony_ci		pr_warn("%s: Critical IRQ #%d is unsupported! Nopping it.\n",
3778c2ecf20Sopenharmony_ci			__func__, l2irq);
3788c2ecf20Sopenharmony_ci		irq_set_chip(virq, &no_irq_chip);
3798c2ecf20Sopenharmony_ci		return 0;
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	irq_set_chip_and_handler(virq, irqchip, handle_level_irq);
3838c2ecf20Sopenharmony_ci	pr_debug("%s: virq=%x, l1=%i, l2=%i\n", __func__, virq, l1irq, l2irq);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	return 0;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic const struct irq_domain_ops mpc52xx_irqhost_ops = {
3898c2ecf20Sopenharmony_ci	.xlate = mpc52xx_irqhost_xlate,
3908c2ecf20Sopenharmony_ci	.map = mpc52xx_irqhost_map,
3918c2ecf20Sopenharmony_ci};
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci/**
3948c2ecf20Sopenharmony_ci * mpc52xx_init_irq - Initialize and register with the virq subsystem
3958c2ecf20Sopenharmony_ci *
3968c2ecf20Sopenharmony_ci * Hook for setting up IRQs on an mpc5200 system.  A pointer to this function
3978c2ecf20Sopenharmony_ci * is to be put into the machine definition structure.
3988c2ecf20Sopenharmony_ci *
3998c2ecf20Sopenharmony_ci * This function searches the device tree for an MPC5200 interrupt controller,
4008c2ecf20Sopenharmony_ci * initializes it, and registers it with the virq subsystem.
4018c2ecf20Sopenharmony_ci */
4028c2ecf20Sopenharmony_civoid __init mpc52xx_init_irq(void)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	u32 intr_ctrl;
4058c2ecf20Sopenharmony_ci	struct device_node *picnode;
4068c2ecf20Sopenharmony_ci	struct device_node *np;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/* Remap the necessary zones */
4098c2ecf20Sopenharmony_ci	picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);
4108c2ecf20Sopenharmony_ci	intr = of_iomap(picnode, 0);
4118c2ecf20Sopenharmony_ci	if (!intr)
4128c2ecf20Sopenharmony_ci		panic(__FILE__	": find_and_map failed on 'mpc5200-pic'. "
4138c2ecf20Sopenharmony_ci				"Check node !");
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	np = of_find_matching_node(NULL, mpc52xx_sdma_ids);
4168c2ecf20Sopenharmony_ci	sdma = of_iomap(np, 0);
4178c2ecf20Sopenharmony_ci	of_node_put(np);
4188c2ecf20Sopenharmony_ci	if (!sdma)
4198c2ecf20Sopenharmony_ci		panic(__FILE__	": find_and_map failed on 'mpc5200-bestcomm'. "
4208c2ecf20Sopenharmony_ci				"Check node !");
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	pr_debug("MPC5200 IRQ controller mapped to 0x%p\n", intr);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	/* Disable all interrupt sources. */
4258c2ecf20Sopenharmony_ci	out_be32(&sdma->IntPend, 0xffffffff);	/* 1 means clear pending */
4268c2ecf20Sopenharmony_ci	out_be32(&sdma->IntMask, 0xffffffff);	/* 1 means disabled */
4278c2ecf20Sopenharmony_ci	out_be32(&intr->per_mask, 0x7ffffc00);	/* 1 means disabled */
4288c2ecf20Sopenharmony_ci	out_be32(&intr->main_mask, 0x00010fff);	/* 1 means disabled */
4298c2ecf20Sopenharmony_ci	intr_ctrl = in_be32(&intr->ctrl);
4308c2ecf20Sopenharmony_ci	intr_ctrl &= 0x00ff0000;	/* Keeps IRQ[0-3] config */
4318c2ecf20Sopenharmony_ci	intr_ctrl |=	0x0f000000 |	/* clear IRQ 0-3 */
4328c2ecf20Sopenharmony_ci			0x00001000 |	/* MEE master external enable */
4338c2ecf20Sopenharmony_ci			0x00000000 |	/* 0 means disable IRQ 0-3 */
4348c2ecf20Sopenharmony_ci			0x00000001;	/* CEb route critical normally */
4358c2ecf20Sopenharmony_ci	out_be32(&intr->ctrl, intr_ctrl);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/* Zero a bunch of the priority settings. */
4388c2ecf20Sopenharmony_ci	out_be32(&intr->per_pri1, 0);
4398c2ecf20Sopenharmony_ci	out_be32(&intr->per_pri2, 0);
4408c2ecf20Sopenharmony_ci	out_be32(&intr->per_pri3, 0);
4418c2ecf20Sopenharmony_ci	out_be32(&intr->main_pri1, 0);
4428c2ecf20Sopenharmony_ci	out_be32(&intr->main_pri2, 0);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/*
4458c2ecf20Sopenharmony_ci	 * As last step, add an irq host to translate the real
4468c2ecf20Sopenharmony_ci	 * hw irq information provided by the ofw to linux virq
4478c2ecf20Sopenharmony_ci	 */
4488c2ecf20Sopenharmony_ci	mpc52xx_irqhost = irq_domain_add_linear(picnode,
4498c2ecf20Sopenharmony_ci	                                 MPC52xx_IRQ_HIGHTESTHWIRQ,
4508c2ecf20Sopenharmony_ci	                                 &mpc52xx_irqhost_ops, NULL);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	if (!mpc52xx_irqhost)
4538c2ecf20Sopenharmony_ci		panic(__FILE__ ": Cannot allocate the IRQ host\n");
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	irq_set_default_host(mpc52xx_irqhost);
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	pr_info("MPC52xx PIC is up and running!\n");
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci/**
4618c2ecf20Sopenharmony_ci * mpc52xx_get_irq - Get pending interrupt number hook function
4628c2ecf20Sopenharmony_ci *
4638c2ecf20Sopenharmony_ci * Called by the interrupt handler to determine what IRQ handler needs to be
4648c2ecf20Sopenharmony_ci * executed.
4658c2ecf20Sopenharmony_ci *
4668c2ecf20Sopenharmony_ci * Status of pending interrupts is determined by reading the encoded status
4678c2ecf20Sopenharmony_ci * register.  The encoded status register has three fields; one for each of the
4688c2ecf20Sopenharmony_ci * types of interrupts defined by the controller - 'critical', 'main' and
4698c2ecf20Sopenharmony_ci * 'peripheral'.  This function reads the status register and returns the IRQ
4708c2ecf20Sopenharmony_ci * number associated with the highest priority pending interrupt.  'Critical'
4718c2ecf20Sopenharmony_ci * interrupts have the highest priority, followed by 'main' interrupts, and
4728c2ecf20Sopenharmony_ci * then 'peripheral'.
4738c2ecf20Sopenharmony_ci *
4748c2ecf20Sopenharmony_ci * The mpc5200 interrupt controller can be configured to boost the priority
4758c2ecf20Sopenharmony_ci * of individual 'peripheral' interrupts.  If this is the case then a special
4768c2ecf20Sopenharmony_ci * value will appear in either the crit or main fields indicating a high
4778c2ecf20Sopenharmony_ci * or medium priority peripheral irq has occurred.
4788c2ecf20Sopenharmony_ci *
4798c2ecf20Sopenharmony_ci * This function checks each of the 3 irq request fields and returns the
4808c2ecf20Sopenharmony_ci * first pending interrupt that it finds.
4818c2ecf20Sopenharmony_ci *
4828c2ecf20Sopenharmony_ci * This function also identifies a 4th type of interrupt; 'bestcomm'.  Each
4838c2ecf20Sopenharmony_ci * bestcomm DMA task can raise the bestcomm peripheral interrupt.  When this
4848c2ecf20Sopenharmony_ci * occurs at task-specific IRQ# is decoded so that each task can have its
4858c2ecf20Sopenharmony_ci * own IRQ handler.
4868c2ecf20Sopenharmony_ci */
4878c2ecf20Sopenharmony_ciunsigned int mpc52xx_get_irq(void)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	u32 status;
4908c2ecf20Sopenharmony_ci	int irq;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	status = in_be32(&intr->enc_status);
4938c2ecf20Sopenharmony_ci	if (status & 0x00000400) {	/* critical */
4948c2ecf20Sopenharmony_ci		irq = (status >> 8) & 0x3;
4958c2ecf20Sopenharmony_ci		if (irq == 2)	/* high priority peripheral */
4968c2ecf20Sopenharmony_ci			goto peripheral;
4978c2ecf20Sopenharmony_ci		irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET);
4988c2ecf20Sopenharmony_ci	} else if (status & 0x00200000) {	/* main */
4998c2ecf20Sopenharmony_ci		irq = (status >> 16) & 0x1f;
5008c2ecf20Sopenharmony_ci		if (irq == 4)	/* low priority peripheral */
5018c2ecf20Sopenharmony_ci			goto peripheral;
5028c2ecf20Sopenharmony_ci		irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET);
5038c2ecf20Sopenharmony_ci	} else if (status & 0x20000000) {	/* peripheral */
5048c2ecf20Sopenharmony_ci	      peripheral:
5058c2ecf20Sopenharmony_ci		irq = (status >> 24) & 0x1f;
5068c2ecf20Sopenharmony_ci		if (irq == 0) {	/* bestcomm */
5078c2ecf20Sopenharmony_ci			status = in_be32(&sdma->IntPend);
5088c2ecf20Sopenharmony_ci			irq = ffs(status) - 1;
5098c2ecf20Sopenharmony_ci			irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET);
5108c2ecf20Sopenharmony_ci		} else {
5118c2ecf20Sopenharmony_ci			irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET);
5128c2ecf20Sopenharmony_ci		}
5138c2ecf20Sopenharmony_ci	} else {
5148c2ecf20Sopenharmony_ci		return 0;
5158c2ecf20Sopenharmony_ci	}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	return irq_linear_revmap(mpc52xx_irqhost, irq);
5188c2ecf20Sopenharmony_ci}
519