Path: blob/master/arch/powerpc/platforms/52xx/mpc52xx_pic.c
26481 views
/*1*2* Programmable Interrupt Controller functions for the Freescale MPC52xx.3*4* Copyright (C) 2008 Secret Lab Technologies Ltd.5* Copyright (C) 2006 bplan GmbH6* Copyright (C) 2004 Sylvain Munaut <[email protected]>7* Copyright (C) 2003 Montavista Software, Inc8*9* Based on the code from the 2.4 kernel by10* Dale Farnsworth <[email protected]> and Kent Borg.11*12* This file is licensed under the terms of the GNU General Public License13* version 2. This program is licensed "as is" without any warranty of any14* kind, whether express or implied.15*16*/1718/*19* This is the device driver for the MPC5200 interrupt controller.20*21* hardware overview22* -----------------23* The MPC5200 interrupt controller groups the all interrupt sources into24* three groups called 'critical', 'main', and 'peripheral'. The critical25* group has 3 irqs, External IRQ0, slice timer 0 irq, and wake from deep26* sleep. Main group include the other 3 external IRQs, slice timer 1, RTC,27* gpios, and the general purpose timers. Peripheral group contains the28* remaining irq sources from all of the on-chip peripherals (PSCs, Ethernet,29* USB, DMA, etc).30*31* virqs32* -----33* The Linux IRQ subsystem requires that each irq source be assigned a34* system wide unique IRQ number starting at 1 (0 means no irq). Since35* systems can have multiple interrupt controllers, the virtual IRQ (virq)36* infrastructure lets each interrupt controller to define a local set37* of IRQ numbers and the virq infrastructure maps those numbers into38* a unique range of the global IRQ# space.39*40* To define a range of virq numbers for this controller, this driver first41* assigns a number to each of the irq groups (called the level 1 or L142* value). Within each group individual irq sources are also assigned a43* number, as defined by the MPC5200 user guide, and refers to it as the44* level 2 or L2 value. The virq number is determined by shifting up the45* L1 value by MPC52xx_IRQ_L1_OFFSET and ORing it with the L2 value.46*47* For example, the TMR0 interrupt is irq 9 in the main group. The48* virq for TMR0 is calculated by ((1 << MPC52xx_IRQ_L1_OFFSET) | 9).49*50* The observant reader will also notice that this driver defines a 4th51* interrupt group called 'bestcomm'. The bestcomm group isn't physically52* part of the MPC5200 interrupt controller, but it is used here to assign53* a separate virq number for each bestcomm task (since any of the 1654* bestcomm tasks can cause the bestcomm interrupt to be raised). When a55* bestcomm interrupt occurs (peripheral group, irq 0) this driver determines56* which task needs servicing and returns the irq number for that task. This57* allows drivers which use bestcomm to define their own interrupt handlers.58*59* irq_chip structures60* -------------------61* For actually manipulating IRQs (masking, enabling, clearing, etc) this62* driver defines four separate 'irq_chip' structures, one for the main63* group, one for the peripherals group, one for the bestcomm group and one64* for external interrupts. The irq_chip structures provide the hooks needed65* to manipulate each IRQ source, and since each group is has a separate set66* of registers for controlling the irq, it makes sense to divide up the67* hooks along those lines.68*69* You'll notice that there is not an irq_chip for the critical group and70* you'll also notice that there is an irq_chip defined for external71* interrupts even though there is no external interrupt group. The reason72* for this is that the four external interrupts are all managed with the same73* register even though one of the external IRQs is in the critical group and74* the other three are in the main group. For this reason it makes sense for75* the 4 external irqs to be managed using a separate set of hooks. The76* reason there is no crit irq_chip is that of the 3 irqs in the critical77* group, only external interrupt is actually support at this time by this78* driver and since external interrupt is the only one used, it can just79* be directed to make use of the external irq irq_chip.80*81* device tree bindings82* --------------------83* The device tree bindings for this controller reflect the two level84* organization of irqs in the device. #interrupt-cells = <3> where the85* first cell is the group number [0..3], the second cell is the irq86* number in the group, and the third cell is the sense type (level/edge).87* For reference, the following is a list of the interrupt property values88* associated with external interrupt sources on the MPC5200 (just because89* it is non-obvious to determine what the interrupts property should be90* when reading the mpc5200 manual and it is a frequently asked question).91*92* External interrupts:93* <0 0 n> external irq0, n is sense (n=0: level high,94* <1 1 n> external irq1, n is sense n=1: edge rising,95* <1 2 n> external irq2, n is sense n=2: edge falling,96* <1 3 n> external irq3, n is sense n=3: level low)97*/98#undef DEBUG99100#include <linux/interrupt.h>101#include <linux/irq.h>102#include <linux/of.h>103#include <linux/of_address.h>104#include <linux/of_irq.h>105#include <asm/io.h>106#include <asm/mpc52xx.h>107108/* HW IRQ mapping */109#define MPC52xx_IRQ_L1_CRIT (0)110#define MPC52xx_IRQ_L1_MAIN (1)111#define MPC52xx_IRQ_L1_PERP (2)112#define MPC52xx_IRQ_L1_SDMA (3)113114#define MPC52xx_IRQ_L1_OFFSET (6)115#define MPC52xx_IRQ_L1_MASK (0x00c0)116#define MPC52xx_IRQ_L2_MASK (0x003f)117118#define MPC52xx_IRQ_HIGHTESTHWIRQ (0xd0)119120121/* MPC5200 device tree match tables */122static const struct of_device_id mpc52xx_pic_ids[] __initconst = {123{ .compatible = "fsl,mpc5200-pic", },124{ .compatible = "mpc5200-pic", },125{}126};127static const struct of_device_id mpc52xx_sdma_ids[] __initconst = {128{ .compatible = "fsl,mpc5200-bestcomm", },129{ .compatible = "mpc5200-bestcomm", },130{}131};132133static struct mpc52xx_intr __iomem *intr;134static struct mpc52xx_sdma __iomem *sdma;135static struct irq_domain *mpc52xx_irqhost = NULL;136137static unsigned char mpc52xx_map_senses[4] = {138IRQ_TYPE_LEVEL_HIGH,139IRQ_TYPE_EDGE_RISING,140IRQ_TYPE_EDGE_FALLING,141IRQ_TYPE_LEVEL_LOW,142};143144/* Utility functions */145static inline void io_be_setbit(u32 __iomem *addr, int bitno)146{147out_be32(addr, in_be32(addr) | (1 << bitno));148}149150static inline void io_be_clrbit(u32 __iomem *addr, int bitno)151{152out_be32(addr, in_be32(addr) & ~(1 << bitno));153}154155/*156* IRQ[0-3] interrupt irq_chip157*/158static void mpc52xx_extirq_mask(struct irq_data *d)159{160int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;161io_be_clrbit(&intr->ctrl, 11 - l2irq);162}163164static void mpc52xx_extirq_unmask(struct irq_data *d)165{166int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;167io_be_setbit(&intr->ctrl, 11 - l2irq);168}169170static void mpc52xx_extirq_ack(struct irq_data *d)171{172int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;173io_be_setbit(&intr->ctrl, 27-l2irq);174}175176static int mpc52xx_extirq_set_type(struct irq_data *d, unsigned int flow_type)177{178u32 ctrl_reg, type;179int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;180void *handler = handle_level_irq;181182pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__,183(int) irqd_to_hwirq(d), l2irq, flow_type);184185switch (flow_type) {186case IRQF_TRIGGER_HIGH: type = 0; break;187case IRQF_TRIGGER_RISING: type = 1; handler = handle_edge_irq; break;188case IRQF_TRIGGER_FALLING: type = 2; handler = handle_edge_irq; break;189case IRQF_TRIGGER_LOW: type = 3; break;190default:191type = 0;192}193194ctrl_reg = in_be32(&intr->ctrl);195ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));196ctrl_reg |= (type << (22 - (l2irq * 2)));197out_be32(&intr->ctrl, ctrl_reg);198199irq_set_handler_locked(d, handler);200201return 0;202}203204static struct irq_chip mpc52xx_extirq_irqchip = {205.name = "MPC52xx External",206.irq_mask = mpc52xx_extirq_mask,207.irq_unmask = mpc52xx_extirq_unmask,208.irq_ack = mpc52xx_extirq_ack,209.irq_set_type = mpc52xx_extirq_set_type,210};211212/*213* Main interrupt irq_chip214*/215static int mpc52xx_null_set_type(struct irq_data *d, unsigned int flow_type)216{217return 0; /* Do nothing so that the sense mask will get updated */218}219220static void mpc52xx_main_mask(struct irq_data *d)221{222int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;223io_be_setbit(&intr->main_mask, 16 - l2irq);224}225226static void mpc52xx_main_unmask(struct irq_data *d)227{228int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;229io_be_clrbit(&intr->main_mask, 16 - l2irq);230}231232static struct irq_chip mpc52xx_main_irqchip = {233.name = "MPC52xx Main",234.irq_mask = mpc52xx_main_mask,235.irq_mask_ack = mpc52xx_main_mask,236.irq_unmask = mpc52xx_main_unmask,237.irq_set_type = mpc52xx_null_set_type,238};239240/*241* Peripherals interrupt irq_chip242*/243static void mpc52xx_periph_mask(struct irq_data *d)244{245int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;246io_be_setbit(&intr->per_mask, 31 - l2irq);247}248249static void mpc52xx_periph_unmask(struct irq_data *d)250{251int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;252io_be_clrbit(&intr->per_mask, 31 - l2irq);253}254255static struct irq_chip mpc52xx_periph_irqchip = {256.name = "MPC52xx Peripherals",257.irq_mask = mpc52xx_periph_mask,258.irq_mask_ack = mpc52xx_periph_mask,259.irq_unmask = mpc52xx_periph_unmask,260.irq_set_type = mpc52xx_null_set_type,261};262263/*264* SDMA interrupt irq_chip265*/266static void mpc52xx_sdma_mask(struct irq_data *d)267{268int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;269io_be_setbit(&sdma->IntMask, l2irq);270}271272static void mpc52xx_sdma_unmask(struct irq_data *d)273{274int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;275io_be_clrbit(&sdma->IntMask, l2irq);276}277278static void mpc52xx_sdma_ack(struct irq_data *d)279{280int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;281out_be32(&sdma->IntPend, 1 << l2irq);282}283284static struct irq_chip mpc52xx_sdma_irqchip = {285.name = "MPC52xx SDMA",286.irq_mask = mpc52xx_sdma_mask,287.irq_unmask = mpc52xx_sdma_unmask,288.irq_ack = mpc52xx_sdma_ack,289.irq_set_type = mpc52xx_null_set_type,290};291292/**293* mpc52xx_is_extirq - Returns true if hwirq number is for an external IRQ294*/295static int mpc52xx_is_extirq(int l1, int l2)296{297return ((l1 == 0) && (l2 == 0)) ||298((l1 == 1) && (l2 >= 1) && (l2 <= 3));299}300301/**302* mpc52xx_irqhost_xlate - translate virq# from device tree interrupts property303*/304static int mpc52xx_irqhost_xlate(struct irq_domain *h, struct device_node *ct,305const u32 *intspec, unsigned int intsize,306irq_hw_number_t *out_hwirq,307unsigned int *out_flags)308{309int intrvect_l1;310int intrvect_l2;311int intrvect_type;312int intrvect_linux;313314if (intsize != 3)315return -1;316317intrvect_l1 = (int)intspec[0];318intrvect_l2 = (int)intspec[1];319intrvect_type = (int)intspec[2] & 0x3;320321intrvect_linux = (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) &322MPC52xx_IRQ_L1_MASK;323intrvect_linux |= intrvect_l2 & MPC52xx_IRQ_L2_MASK;324325*out_hwirq = intrvect_linux;326*out_flags = IRQ_TYPE_LEVEL_LOW;327if (mpc52xx_is_extirq(intrvect_l1, intrvect_l2))328*out_flags = mpc52xx_map_senses[intrvect_type];329330pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,331intrvect_l2);332return 0;333}334335/**336* mpc52xx_irqhost_map - Hook to map from virq to an irq_chip structure337*/338static int mpc52xx_irqhost_map(struct irq_domain *h, unsigned int virq,339irq_hw_number_t irq)340{341int l1irq;342int l2irq;343struct irq_chip *irqchip;344void *hndlr;345int type;346u32 reg;347348l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;349l2irq = irq & MPC52xx_IRQ_L2_MASK;350351/*352* External IRQs are handled differently by the hardware so they are353* handled by a dedicated irq_chip structure.354*/355if (mpc52xx_is_extirq(l1irq, l2irq)) {356reg = in_be32(&intr->ctrl);357type = mpc52xx_map_senses[(reg >> (22 - l2irq * 2)) & 0x3];358if ((type == IRQ_TYPE_EDGE_FALLING) ||359(type == IRQ_TYPE_EDGE_RISING))360hndlr = handle_edge_irq;361else362hndlr = handle_level_irq;363364irq_set_chip_and_handler(virq, &mpc52xx_extirq_irqchip, hndlr);365pr_debug("%s: External IRQ%i virq=%x, hw=%x. type=%x\n",366__func__, l2irq, virq, (int)irq, type);367return 0;368}369370/* It is an internal SOC irq. Choose the correct irq_chip */371switch (l1irq) {372case MPC52xx_IRQ_L1_MAIN: irqchip = &mpc52xx_main_irqchip; break;373case MPC52xx_IRQ_L1_PERP: irqchip = &mpc52xx_periph_irqchip; break;374case MPC52xx_IRQ_L1_SDMA: irqchip = &mpc52xx_sdma_irqchip; break;375case MPC52xx_IRQ_L1_CRIT:376pr_warn("%s: Critical IRQ #%d is unsupported! Nopping it.\n",377__func__, l2irq);378irq_set_chip(virq, &no_irq_chip);379return 0;380}381382irq_set_chip_and_handler(virq, irqchip, handle_level_irq);383pr_debug("%s: virq=%x, l1=%i, l2=%i\n", __func__, virq, l1irq, l2irq);384385return 0;386}387388static const struct irq_domain_ops mpc52xx_irqhost_ops = {389.xlate = mpc52xx_irqhost_xlate,390.map = mpc52xx_irqhost_map,391};392393/**394* mpc52xx_init_irq - Initialize and register with the virq subsystem395*396* Hook for setting up IRQs on an mpc5200 system. A pointer to this function397* is to be put into the machine definition structure.398*399* This function searches the device tree for an MPC5200 interrupt controller,400* initializes it, and registers it with the virq subsystem.401*/402void __init mpc52xx_init_irq(void)403{404u32 intr_ctrl;405struct device_node *picnode;406struct device_node *np;407408/* Remap the necessary zones */409picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);410intr = of_iomap(picnode, 0);411if (!intr)412panic(__FILE__ ": find_and_map failed on 'mpc5200-pic'. "413"Check node !");414415np = of_find_matching_node(NULL, mpc52xx_sdma_ids);416sdma = of_iomap(np, 0);417of_node_put(np);418if (!sdma)419panic(__FILE__ ": find_and_map failed on 'mpc5200-bestcomm'. "420"Check node !");421422pr_debug("MPC5200 IRQ controller mapped to 0x%p\n", intr);423424/* Disable all interrupt sources. */425out_be32(&sdma->IntPend, 0xffffffff); /* 1 means clear pending */426out_be32(&sdma->IntMask, 0xffffffff); /* 1 means disabled */427out_be32(&intr->per_mask, 0x7ffffc00); /* 1 means disabled */428out_be32(&intr->main_mask, 0x00010fff); /* 1 means disabled */429intr_ctrl = in_be32(&intr->ctrl);430intr_ctrl &= 0x00ff0000; /* Keeps IRQ[0-3] config */431intr_ctrl |= 0x0f000000 | /* clear IRQ 0-3 */4320x00001000 | /* MEE master external enable */4330x00000000 | /* 0 means disable IRQ 0-3 */4340x00000001; /* CEb route critical normally */435out_be32(&intr->ctrl, intr_ctrl);436437/* Zero a bunch of the priority settings. */438out_be32(&intr->per_pri1, 0);439out_be32(&intr->per_pri2, 0);440out_be32(&intr->per_pri3, 0);441out_be32(&intr->main_pri1, 0);442out_be32(&intr->main_pri2, 0);443444/*445* As last step, add an irq host to translate the real446* hw irq information provided by the ofw to linux virq447*/448mpc52xx_irqhost = irq_domain_create_linear(of_fwnode_handle(picnode),449MPC52xx_IRQ_HIGHTESTHWIRQ,450&mpc52xx_irqhost_ops, NULL);451452if (!mpc52xx_irqhost)453panic(__FILE__ ": Cannot allocate the IRQ host\n");454455irq_set_default_domain(mpc52xx_irqhost);456457pr_info("MPC52xx PIC is up and running!\n");458}459460/**461* mpc52xx_get_irq - Get pending interrupt number hook function462*463* Called by the interrupt handler to determine what IRQ handler needs to be464* executed.465*466* Status of pending interrupts is determined by reading the encoded status467* register. The encoded status register has three fields; one for each of the468* types of interrupts defined by the controller - 'critical', 'main' and469* 'peripheral'. This function reads the status register and returns the IRQ470* number associated with the highest priority pending interrupt. 'Critical'471* interrupts have the highest priority, followed by 'main' interrupts, and472* then 'peripheral'.473*474* The mpc5200 interrupt controller can be configured to boost the priority475* of individual 'peripheral' interrupts. If this is the case then a special476* value will appear in either the crit or main fields indicating a high477* or medium priority peripheral irq has occurred.478*479* This function checks each of the 3 irq request fields and returns the480* first pending interrupt that it finds.481*482* This function also identifies a 4th type of interrupt; 'bestcomm'. Each483* bestcomm DMA task can raise the bestcomm peripheral interrupt. When this484* occurs at task-specific IRQ# is decoded so that each task can have its485* own IRQ handler.486*/487unsigned int mpc52xx_get_irq(void)488{489u32 status;490int irq;491492status = in_be32(&intr->enc_status);493if (status & 0x00000400) { /* critical */494irq = (status >> 8) & 0x3;495if (irq == 2) /* high priority peripheral */496goto peripheral;497irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET);498} else if (status & 0x00200000) { /* main */499irq = (status >> 16) & 0x1f;500if (irq == 4) /* low priority peripheral */501goto peripheral;502irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET);503} else if (status & 0x20000000) { /* peripheral */504peripheral:505irq = (status >> 24) & 0x1f;506if (irq == 0) { /* bestcomm */507status = in_be32(&sdma->IntPend);508irq = ffs(status) - 1;509irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET);510} else {511irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET);512}513} else {514return 0;515}516517return irq_find_mapping(mpc52xx_irqhost, irq);518}519520521