Path: blob/master/arch/powerpc/platforms/52xx/mpc52xx_pic.c
10818 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 <asm/io.h>104#include <asm/prom.h>105#include <asm/mpc52xx.h>106107/* HW IRQ mapping */108#define MPC52xx_IRQ_L1_CRIT (0)109#define MPC52xx_IRQ_L1_MAIN (1)110#define MPC52xx_IRQ_L1_PERP (2)111#define MPC52xx_IRQ_L1_SDMA (3)112113#define MPC52xx_IRQ_L1_OFFSET (6)114#define MPC52xx_IRQ_L1_MASK (0x00c0)115#define MPC52xx_IRQ_L2_MASK (0x003f)116117#define MPC52xx_IRQ_HIGHTESTHWIRQ (0xd0)118119120/* MPC5200 device tree match tables */121static struct of_device_id mpc52xx_pic_ids[] __initdata = {122{ .compatible = "fsl,mpc5200-pic", },123{ .compatible = "mpc5200-pic", },124{}125};126static struct of_device_id mpc52xx_sdma_ids[] __initdata = {127{ .compatible = "fsl,mpc5200-bestcomm", },128{ .compatible = "mpc5200-bestcomm", },129{}130};131132static struct mpc52xx_intr __iomem *intr;133static struct mpc52xx_sdma __iomem *sdma;134static struct irq_host *mpc52xx_irqhost = NULL;135136static unsigned char mpc52xx_map_senses[4] = {137IRQ_TYPE_LEVEL_HIGH,138IRQ_TYPE_EDGE_RISING,139IRQ_TYPE_EDGE_FALLING,140IRQ_TYPE_LEVEL_LOW,141};142143/* Utility functions */144static inline void io_be_setbit(u32 __iomem *addr, int bitno)145{146out_be32(addr, in_be32(addr) | (1 << bitno));147}148149static inline void io_be_clrbit(u32 __iomem *addr, int bitno)150{151out_be32(addr, in_be32(addr) & ~(1 << bitno));152}153154/*155* IRQ[0-3] interrupt irq_chip156*/157static void mpc52xx_extirq_mask(struct irq_data *d)158{159int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;160io_be_clrbit(&intr->ctrl, 11 - l2irq);161}162163static void mpc52xx_extirq_unmask(struct irq_data *d)164{165int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;166io_be_setbit(&intr->ctrl, 11 - l2irq);167}168169static void mpc52xx_extirq_ack(struct irq_data *d)170{171int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;172io_be_setbit(&intr->ctrl, 27-l2irq);173}174175static int mpc52xx_extirq_set_type(struct irq_data *d, unsigned int flow_type)176{177u32 ctrl_reg, type;178int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;179void *handler = handle_level_irq;180181pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__,182(int) irqd_to_hwirq(d), l2irq, flow_type);183184switch (flow_type) {185case IRQF_TRIGGER_HIGH: type = 0; break;186case IRQF_TRIGGER_RISING: type = 1; handler = handle_edge_irq; break;187case IRQF_TRIGGER_FALLING: type = 2; handler = handle_edge_irq; break;188case IRQF_TRIGGER_LOW: type = 3; break;189default:190type = 0;191}192193ctrl_reg = in_be32(&intr->ctrl);194ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));195ctrl_reg |= (type << (22 - (l2irq * 2)));196out_be32(&intr->ctrl, ctrl_reg);197198__irq_set_handler_locked(d->irq, handler);199200return 0;201}202203static struct irq_chip mpc52xx_extirq_irqchip = {204.name = "MPC52xx External",205.irq_mask = mpc52xx_extirq_mask,206.irq_unmask = mpc52xx_extirq_unmask,207.irq_ack = mpc52xx_extirq_ack,208.irq_set_type = mpc52xx_extirq_set_type,209};210211/*212* Main interrupt irq_chip213*/214static int mpc52xx_null_set_type(struct irq_data *d, unsigned int flow_type)215{216return 0; /* Do nothing so that the sense mask will get updated */217}218219static void mpc52xx_main_mask(struct irq_data *d)220{221int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;222io_be_setbit(&intr->main_mask, 16 - l2irq);223}224225static void mpc52xx_main_unmask(struct irq_data *d)226{227int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;228io_be_clrbit(&intr->main_mask, 16 - l2irq);229}230231static struct irq_chip mpc52xx_main_irqchip = {232.name = "MPC52xx Main",233.irq_mask = mpc52xx_main_mask,234.irq_mask_ack = mpc52xx_main_mask,235.irq_unmask = mpc52xx_main_unmask,236.irq_set_type = mpc52xx_null_set_type,237};238239/*240* Peripherals interrupt irq_chip241*/242static void mpc52xx_periph_mask(struct irq_data *d)243{244int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;245io_be_setbit(&intr->per_mask, 31 - l2irq);246}247248static void mpc52xx_periph_unmask(struct irq_data *d)249{250int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;251io_be_clrbit(&intr->per_mask, 31 - l2irq);252}253254static struct irq_chip mpc52xx_periph_irqchip = {255.name = "MPC52xx Peripherals",256.irq_mask = mpc52xx_periph_mask,257.irq_mask_ack = mpc52xx_periph_mask,258.irq_unmask = mpc52xx_periph_unmask,259.irq_set_type = mpc52xx_null_set_type,260};261262/*263* SDMA interrupt irq_chip264*/265static void mpc52xx_sdma_mask(struct irq_data *d)266{267int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;268io_be_setbit(&sdma->IntMask, l2irq);269}270271static void mpc52xx_sdma_unmask(struct irq_data *d)272{273int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;274io_be_clrbit(&sdma->IntMask, l2irq);275}276277static void mpc52xx_sdma_ack(struct irq_data *d)278{279int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;280out_be32(&sdma->IntPend, 1 << l2irq);281}282283static struct irq_chip mpc52xx_sdma_irqchip = {284.name = "MPC52xx SDMA",285.irq_mask = mpc52xx_sdma_mask,286.irq_unmask = mpc52xx_sdma_unmask,287.irq_ack = mpc52xx_sdma_ack,288.irq_set_type = mpc52xx_null_set_type,289};290291/**292* mpc52xx_is_extirq - Returns true if hwirq number is for an external IRQ293*/294static int mpc52xx_is_extirq(int l1, int l2)295{296return ((l1 == 0) && (l2 == 0)) ||297((l1 == 1) && (l2 >= 1) && (l2 <= 3));298}299300/**301* mpc52xx_irqhost_xlate - translate virq# from device tree interrupts property302*/303static int mpc52xx_irqhost_xlate(struct irq_host *h, struct device_node *ct,304const u32 *intspec, unsigned int intsize,305irq_hw_number_t *out_hwirq,306unsigned int *out_flags)307{308int intrvect_l1;309int intrvect_l2;310int intrvect_type;311int intrvect_linux;312313if (intsize != 3)314return -1;315316intrvect_l1 = (int)intspec[0];317intrvect_l2 = (int)intspec[1];318intrvect_type = (int)intspec[2] & 0x3;319320intrvect_linux = (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) &321MPC52xx_IRQ_L1_MASK;322intrvect_linux |= intrvect_l2 & MPC52xx_IRQ_L2_MASK;323324*out_hwirq = intrvect_linux;325*out_flags = IRQ_TYPE_LEVEL_LOW;326if (mpc52xx_is_extirq(intrvect_l1, intrvect_l2))327*out_flags = mpc52xx_map_senses[intrvect_type];328329pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,330intrvect_l2);331return 0;332}333334/**335* mpc52xx_irqhost_map - Hook to map from virq to an irq_chip structure336*/337static int mpc52xx_irqhost_map(struct irq_host *h, unsigned int virq,338irq_hw_number_t irq)339{340int l1irq;341int l2irq;342struct irq_chip *irqchip;343void *hndlr;344int type;345u32 reg;346347l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;348l2irq = irq & MPC52xx_IRQ_L2_MASK;349350/*351* External IRQs are handled differently by the hardware so they are352* handled by a dedicated irq_chip structure.353*/354if (mpc52xx_is_extirq(l1irq, l2irq)) {355reg = in_be32(&intr->ctrl);356type = mpc52xx_map_senses[(reg >> (22 - l2irq * 2)) & 0x3];357if ((type == IRQ_TYPE_EDGE_FALLING) ||358(type == IRQ_TYPE_EDGE_RISING))359hndlr = handle_edge_irq;360else361hndlr = handle_level_irq;362363irq_set_chip_and_handler(virq, &mpc52xx_extirq_irqchip, hndlr);364pr_debug("%s: External IRQ%i virq=%x, hw=%x. type=%x\n",365__func__, l2irq, virq, (int)irq, type);366return 0;367}368369/* It is an internal SOC irq. Choose the correct irq_chip */370switch (l1irq) {371case MPC52xx_IRQ_L1_MAIN: irqchip = &mpc52xx_main_irqchip; break;372case MPC52xx_IRQ_L1_PERP: irqchip = &mpc52xx_periph_irqchip; break;373case MPC52xx_IRQ_L1_SDMA: irqchip = &mpc52xx_sdma_irqchip; break;374default:375pr_err("%s: invalid irq: virq=%i, l1=%i, l2=%i\n",376__func__, virq, l1irq, l2irq);377return -EINVAL;378}379380irq_set_chip_and_handler(virq, irqchip, handle_level_irq);381pr_debug("%s: virq=%x, l1=%i, l2=%i\n", __func__, virq, l1irq, l2irq);382383return 0;384}385386static struct irq_host_ops mpc52xx_irqhost_ops = {387.xlate = mpc52xx_irqhost_xlate,388.map = mpc52xx_irqhost_map,389};390391/**392* mpc52xx_init_irq - Initialize and register with the virq subsystem393*394* Hook for setting up IRQs on an mpc5200 system. A pointer to this function395* is to be put into the machine definition structure.396*397* This function searches the device tree for an MPC5200 interrupt controller,398* initializes it, and registers it with the virq subsystem.399*/400void __init mpc52xx_init_irq(void)401{402u32 intr_ctrl;403struct device_node *picnode;404struct device_node *np;405406/* Remap the necessary zones */407picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);408intr = of_iomap(picnode, 0);409if (!intr)410panic(__FILE__ ": find_and_map failed on 'mpc5200-pic'. "411"Check node !");412413np = of_find_matching_node(NULL, mpc52xx_sdma_ids);414sdma = of_iomap(np, 0);415of_node_put(np);416if (!sdma)417panic(__FILE__ ": find_and_map failed on 'mpc5200-bestcomm'. "418"Check node !");419420pr_debug("MPC5200 IRQ controller mapped to 0x%p\n", intr);421422/* Disable all interrupt sources. */423out_be32(&sdma->IntPend, 0xffffffff); /* 1 means clear pending */424out_be32(&sdma->IntMask, 0xffffffff); /* 1 means disabled */425out_be32(&intr->per_mask, 0x7ffffc00); /* 1 means disabled */426out_be32(&intr->main_mask, 0x00010fff); /* 1 means disabled */427intr_ctrl = in_be32(&intr->ctrl);428intr_ctrl &= 0x00ff0000; /* Keeps IRQ[0-3] config */429intr_ctrl |= 0x0f000000 | /* clear IRQ 0-3 */4300x00001000 | /* MEE master external enable */4310x00000000 | /* 0 means disable IRQ 0-3 */4320x00000001; /* CEb route critical normally */433out_be32(&intr->ctrl, intr_ctrl);434435/* Zero a bunch of the priority settings. */436out_be32(&intr->per_pri1, 0);437out_be32(&intr->per_pri2, 0);438out_be32(&intr->per_pri3, 0);439out_be32(&intr->main_pri1, 0);440out_be32(&intr->main_pri2, 0);441442/*443* As last step, add an irq host to translate the real444* hw irq information provided by the ofw to linux virq445*/446mpc52xx_irqhost = irq_alloc_host(picnode, IRQ_HOST_MAP_LINEAR,447MPC52xx_IRQ_HIGHTESTHWIRQ,448&mpc52xx_irqhost_ops, -1);449450if (!mpc52xx_irqhost)451panic(__FILE__ ": Cannot allocate the IRQ host\n");452453irq_set_default_host(mpc52xx_irqhost);454455pr_info("MPC52xx PIC is up and running!\n");456}457458/**459* mpc52xx_get_irq - Get pending interrupt number hook function460*461* Called by the interrupt handler to determine what IRQ handler needs to be462* executed.463*464* Status of pending interrupts is determined by reading the encoded status465* register. The encoded status register has three fields; one for each of the466* types of interrupts defined by the controller - 'critical', 'main' and467* 'peripheral'. This function reads the status register and returns the IRQ468* number associated with the highest priority pending interrupt. 'Critical'469* interrupts have the highest priority, followed by 'main' interrupts, and470* then 'peripheral'.471*472* The mpc5200 interrupt controller can be configured to boost the priority473* of individual 'peripheral' interrupts. If this is the case then a special474* value will appear in either the crit or main fields indicating a high475* or medium priority peripheral irq has occurred.476*477* This function checks each of the 3 irq request fields and returns the478* first pending interrupt that it finds.479*480* This function also identifies a 4th type of interrupt; 'bestcomm'. Each481* bestcomm DMA task can raise the bestcomm peripheral interrupt. When this482* occurs at task-specific IRQ# is decoded so that each task can have its483* own IRQ handler.484*/485unsigned int mpc52xx_get_irq(void)486{487u32 status;488int irq;489490status = in_be32(&intr->enc_status);491if (status & 0x00000400) { /* critical */492irq = (status >> 8) & 0x3;493if (irq == 2) /* high priority peripheral */494goto peripheral;495irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET);496} else if (status & 0x00200000) { /* main */497irq = (status >> 16) & 0x1f;498if (irq == 4) /* low priority peripheral */499goto peripheral;500irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET);501} else if (status & 0x20000000) { /* peripheral */502peripheral:503irq = (status >> 24) & 0x1f;504if (irq == 0) { /* bestcomm */505status = in_be32(&sdma->IntPend);506irq = ffs(status) - 1;507irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET);508} else {509irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET);510}511} else {512return NO_IRQ;513}514515return irq_linear_revmap(mpc52xx_irqhost, irq);516}517518519