Path: blob/master/arch/powerpc/platforms/wsp/opb_pic.c
10818 views
/*1* IBM Onboard Peripheral Bus Interrupt Controller2*3* Copyright 2010 Jack Miller, IBM Corporation.4*5* This program is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License as published by the7* Free Software Foundation; either version 2 of the License, or (at your8* option) any later version.9*/1011#include <linux/interrupt.h>12#include <linux/io.h>13#include <linux/irq.h>14#include <linux/of.h>15#include <linux/slab.h>16#include <linux/time.h>1718#include <asm/reg_a2.h>19#include <asm/irq.h>2021#define OPB_NR_IRQS 322223#define OPB_MLSASIER 0x04 /* MLS Accumulated Status IER */24#define OPB_MLSIR 0x50 /* MLS Interrupt Register */25#define OPB_MLSIER 0x54 /* MLS Interrupt Enable Register */26#define OPB_MLSIPR 0x58 /* MLS Interrupt Polarity Register */27#define OPB_MLSIIR 0x5c /* MLS Interrupt Inputs Register */2829static int opb_index = 0;3031struct opb_pic {32struct irq_host *host;33void *regs;34int index;35spinlock_t lock;36};3738static u32 opb_in(struct opb_pic *opb, int offset)39{40return in_be32(opb->regs + offset);41}4243static void opb_out(struct opb_pic *opb, int offset, u32 val)44{45out_be32(opb->regs + offset, val);46}4748static void opb_unmask_irq(struct irq_data *d)49{50struct opb_pic *opb;51unsigned long flags;52u32 ier, bitset;5354opb = d->chip_data;55bitset = (1 << (31 - irqd_to_hwirq(d)));5657spin_lock_irqsave(&opb->lock, flags);5859ier = opb_in(opb, OPB_MLSIER);60opb_out(opb, OPB_MLSIER, ier | bitset);61ier = opb_in(opb, OPB_MLSIER);6263spin_unlock_irqrestore(&opb->lock, flags);64}6566static void opb_mask_irq(struct irq_data *d)67{68struct opb_pic *opb;69unsigned long flags;70u32 ier, mask;7172opb = d->chip_data;73mask = ~(1 << (31 - irqd_to_hwirq(d)));7475spin_lock_irqsave(&opb->lock, flags);7677ier = opb_in(opb, OPB_MLSIER);78opb_out(opb, OPB_MLSIER, ier & mask);79ier = opb_in(opb, OPB_MLSIER); // Flush posted writes8081spin_unlock_irqrestore(&opb->lock, flags);82}8384static void opb_ack_irq(struct irq_data *d)85{86struct opb_pic *opb;87unsigned long flags;88u32 bitset;8990opb = d->chip_data;91bitset = (1 << (31 - irqd_to_hwirq(d)));9293spin_lock_irqsave(&opb->lock, flags);9495opb_out(opb, OPB_MLSIR, bitset);96opb_in(opb, OPB_MLSIR); // Flush posted writes9798spin_unlock_irqrestore(&opb->lock, flags);99}100101static void opb_mask_ack_irq(struct irq_data *d)102{103struct opb_pic *opb;104unsigned long flags;105u32 bitset;106u32 ier, ir;107108opb = d->chip_data;109bitset = (1 << (31 - irqd_to_hwirq(d)));110111spin_lock_irqsave(&opb->lock, flags);112113ier = opb_in(opb, OPB_MLSIER);114opb_out(opb, OPB_MLSIER, ier & ~bitset);115ier = opb_in(opb, OPB_MLSIER); // Flush posted writes116117opb_out(opb, OPB_MLSIR, bitset);118ir = opb_in(opb, OPB_MLSIR); // Flush posted writes119120spin_unlock_irqrestore(&opb->lock, flags);121}122123static int opb_set_irq_type(struct irq_data *d, unsigned int flow)124{125struct opb_pic *opb;126unsigned long flags;127int invert, ipr, mask, bit;128129opb = d->chip_data;130131/* The only information we're interested in in the type is whether it's132* a high or low trigger. For high triggered interrupts, the polarity133* set for it in the MLS Interrupt Polarity Register is 0, for low134* interrupts it's 1 so that the proper input in the MLS Interrupt Input135* Register is interrupted as asserting the interrupt. */136137switch (flow) {138case IRQ_TYPE_NONE:139opb_mask_irq(d);140return 0;141142case IRQ_TYPE_LEVEL_HIGH:143invert = 0;144break;145146case IRQ_TYPE_LEVEL_LOW:147invert = 1;148break;149150default:151return -EINVAL;152}153154bit = (1 << (31 - irqd_to_hwirq(d)));155mask = ~bit;156157spin_lock_irqsave(&opb->lock, flags);158159ipr = opb_in(opb, OPB_MLSIPR);160ipr = (ipr & mask) | (invert ? bit : 0);161opb_out(opb, OPB_MLSIPR, ipr);162ipr = opb_in(opb, OPB_MLSIPR); // Flush posted writes163164spin_unlock_irqrestore(&opb->lock, flags);165166/* Record the type in the interrupt descriptor */167irqd_set_trigger_type(d, flow);168169return 0;170}171172static struct irq_chip opb_irq_chip = {173.name = "OPB",174.irq_mask = opb_mask_irq,175.irq_unmask = opb_unmask_irq,176.irq_mask_ack = opb_mask_ack_irq,177.irq_ack = opb_ack_irq,178.irq_set_type = opb_set_irq_type179};180181static int opb_host_map(struct irq_host *host, unsigned int virq,182irq_hw_number_t hwirq)183{184struct opb_pic *opb;185186opb = host->host_data;187188/* Most of the important stuff is handled by the generic host code, like189* the lookup, so just attach some info to the virtual irq */190191irq_set_chip_data(virq, opb);192irq_set_chip_and_handler(virq, &opb_irq_chip, handle_level_irq);193irq_set_irq_type(virq, IRQ_TYPE_NONE);194195return 0;196}197198static int opb_host_xlate(struct irq_host *host, struct device_node *dn,199const u32 *intspec, unsigned int intsize,200irq_hw_number_t *out_hwirq, unsigned int *out_type)201{202/* Interrupt size must == 2 */203BUG_ON(intsize != 2);204*out_hwirq = intspec[0];205*out_type = intspec[1];206return 0;207}208209static struct irq_host_ops opb_host_ops = {210.map = opb_host_map,211.xlate = opb_host_xlate,212};213214irqreturn_t opb_irq_handler(int irq, void *private)215{216struct opb_pic *opb;217u32 ir, src, subvirq;218219opb = (struct opb_pic *) private;220221/* Read the OPB MLS Interrupt Register for222* asserted interrupts */223ir = opb_in(opb, OPB_MLSIR);224if (!ir)225return IRQ_NONE;226227do {228/* Get 1 - 32 source, *NOT* bit */229src = 32 - ffs(ir);230231/* Translate from the OPB's conception of interrupt number to232* Linux's virtual IRQ */233234subvirq = irq_linear_revmap(opb->host, src);235236generic_handle_irq(subvirq);237} while ((ir = opb_in(opb, OPB_MLSIR)));238239return IRQ_HANDLED;240}241242struct opb_pic *opb_pic_init_one(struct device_node *dn)243{244struct opb_pic *opb;245struct resource res;246247if (of_address_to_resource(dn, 0, &res)) {248printk(KERN_ERR "opb: Couldn't translate resource\n");249return NULL;250}251252opb = kzalloc(sizeof(struct opb_pic), GFP_KERNEL);253if (!opb) {254printk(KERN_ERR "opb: Failed to allocate opb struct!\n");255return NULL;256}257258/* Get access to the OPB MMIO registers */259opb->regs = ioremap(res.start + 0x10000, 0x1000);260if (!opb->regs) {261printk(KERN_ERR "opb: Failed to allocate register space!\n");262goto free_opb;263}264265/* Allocate an irq host so that Linux knows that despite only266* having one interrupt to issue, we're the controller for multiple267* hardware IRQs, so later we can lookup their virtual IRQs. */268269opb->host = irq_alloc_host(dn, IRQ_HOST_MAP_LINEAR,270OPB_NR_IRQS, &opb_host_ops, -1);271272if (!opb->host) {273printk(KERN_ERR "opb: Failed to allocate IRQ host!\n");274goto free_regs;275}276277opb->index = opb_index++;278spin_lock_init(&opb->lock);279opb->host->host_data = opb;280281/* Disable all interrupts by default */282opb_out(opb, OPB_MLSASIER, 0);283opb_out(opb, OPB_MLSIER, 0);284285/* ACK any interrupts left by FW */286opb_out(opb, OPB_MLSIR, 0xFFFFFFFF);287288return opb;289290free_regs:291iounmap(opb->regs);292free_opb:293kfree(opb);294return NULL;295}296297void __init opb_pic_init(void)298{299struct device_node *dn;300struct opb_pic *opb;301int virq;302int rc;303304/* Call init_one for each OPB device */305for_each_compatible_node(dn, NULL, "ibm,opb") {306307/* Fill in an OPB struct */308opb = opb_pic_init_one(dn);309if (!opb) {310printk(KERN_WARNING "opb: Failed to init node, skipped!\n");311continue;312}313314/* Map / get opb's hardware virtual irq */315virq = irq_of_parse_and_map(dn, 0);316if (virq <= 0) {317printk("opb: irq_op_parse_and_map failed!\n");318continue;319}320321/* Attach opb interrupt handler to new virtual IRQ */322rc = request_irq(virq, opb_irq_handler, 0, "OPB LS Cascade", opb);323if (rc) {324printk("opb: request_irq failed: %d\n", rc);325continue;326}327328printk("OPB%d init with %d IRQs at %p\n", opb->index,329OPB_NR_IRQS, opb->regs);330}331}332333334