2 * IBM Onboard Peripheral Bus Interrupt Controller
4 * Copyright 2010 Jack Miller, IBM Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 #include <linux/interrupt.h>
14 #include <linux/irq.h>
16 #include <linux/slab.h>
17 #include <linux/time.h>
19 #include <asm/reg_a2.h>
22 #define OPB_NR_IRQS 32
24 #define OPB_MLSASIER 0x04 /* MLS Accumulated Status IER */
25 #define OPB_MLSIR 0x50 /* MLS Interrupt Register */
26 #define OPB_MLSIER 0x54 /* MLS Interrupt Enable Register */
27 #define OPB_MLSIPR 0x58 /* MLS Interrupt Polarity Register */
28 #define OPB_MLSIIR 0x5c /* MLS Interrupt Inputs Register */
30 static int opb_index
= 0;
33 struct irq_host
*host
;
39 static u32
opb_in(struct opb_pic
*opb
, int offset
)
41 return in_be32(opb
->regs
+ offset
);
44 static void opb_out(struct opb_pic
*opb
, int offset
, u32 val
)
46 out_be32(opb
->regs
+ offset
, val
);
49 static void opb_unmask_irq(struct irq_data
*d
)
56 bitset
= (1 << (31 - irqd_to_hwirq(d
)));
58 spin_lock_irqsave(&opb
->lock
, flags
);
60 ier
= opb_in(opb
, OPB_MLSIER
);
61 opb_out(opb
, OPB_MLSIER
, ier
| bitset
);
62 ier
= opb_in(opb
, OPB_MLSIER
);
64 spin_unlock_irqrestore(&opb
->lock
, flags
);
67 static void opb_mask_irq(struct irq_data
*d
)
74 mask
= ~(1 << (31 - irqd_to_hwirq(d
)));
76 spin_lock_irqsave(&opb
->lock
, flags
);
78 ier
= opb_in(opb
, OPB_MLSIER
);
79 opb_out(opb
, OPB_MLSIER
, ier
& mask
);
80 ier
= opb_in(opb
, OPB_MLSIER
); // Flush posted writes
82 spin_unlock_irqrestore(&opb
->lock
, flags
);
85 static void opb_ack_irq(struct irq_data
*d
)
92 bitset
= (1 << (31 - irqd_to_hwirq(d
)));
94 spin_lock_irqsave(&opb
->lock
, flags
);
96 opb_out(opb
, OPB_MLSIR
, bitset
);
97 opb_in(opb
, OPB_MLSIR
); // Flush posted writes
99 spin_unlock_irqrestore(&opb
->lock
, flags
);
102 static void opb_mask_ack_irq(struct irq_data
*d
)
110 bitset
= (1 << (31 - irqd_to_hwirq(d
)));
112 spin_lock_irqsave(&opb
->lock
, flags
);
114 ier
= opb_in(opb
, OPB_MLSIER
);
115 opb_out(opb
, OPB_MLSIER
, ier
& ~bitset
);
116 ier
= opb_in(opb
, OPB_MLSIER
); // Flush posted writes
118 opb_out(opb
, OPB_MLSIR
, bitset
);
119 ir
= opb_in(opb
, OPB_MLSIR
); // Flush posted writes
121 spin_unlock_irqrestore(&opb
->lock
, flags
);
124 static int opb_set_irq_type(struct irq_data
*d
, unsigned int flow
)
128 int invert
, ipr
, mask
, bit
;
132 /* The only information we're interested in in the type is whether it's
133 * a high or low trigger. For high triggered interrupts, the polarity
134 * set for it in the MLS Interrupt Polarity Register is 0, for low
135 * interrupts it's 1 so that the proper input in the MLS Interrupt Input
136 * Register is interrupted as asserting the interrupt. */
143 case IRQ_TYPE_LEVEL_HIGH
:
147 case IRQ_TYPE_LEVEL_LOW
:
155 bit
= (1 << (31 - irqd_to_hwirq(d
)));
158 spin_lock_irqsave(&opb
->lock
, flags
);
160 ipr
= opb_in(opb
, OPB_MLSIPR
);
161 ipr
= (ipr
& mask
) | (invert
? bit
: 0);
162 opb_out(opb
, OPB_MLSIPR
, ipr
);
163 ipr
= opb_in(opb
, OPB_MLSIPR
); // Flush posted writes
165 spin_unlock_irqrestore(&opb
->lock
, flags
);
167 /* Record the type in the interrupt descriptor */
168 irqd_set_trigger_type(d
, flow
);
173 static struct irq_chip opb_irq_chip
= {
175 .irq_mask
= opb_mask_irq
,
176 .irq_unmask
= opb_unmask_irq
,
177 .irq_mask_ack
= opb_mask_ack_irq
,
178 .irq_ack
= opb_ack_irq
,
179 .irq_set_type
= opb_set_irq_type
182 static int opb_host_map(struct irq_host
*host
, unsigned int virq
,
183 irq_hw_number_t hwirq
)
187 opb
= host
->host_data
;
189 /* Most of the important stuff is handled by the generic host code, like
190 * the lookup, so just attach some info to the virtual irq */
192 irq_set_chip_data(virq
, opb
);
193 irq_set_chip_and_handler(virq
, &opb_irq_chip
, handle_level_irq
);
194 irq_set_irq_type(virq
, IRQ_TYPE_NONE
);
199 static int opb_host_xlate(struct irq_host
*host
, struct device_node
*dn
,
200 const u32
*intspec
, unsigned int intsize
,
201 irq_hw_number_t
*out_hwirq
, unsigned int *out_type
)
203 /* Interrupt size must == 2 */
204 BUG_ON(intsize
!= 2);
205 *out_hwirq
= intspec
[0];
206 *out_type
= intspec
[1];
210 static struct irq_host_ops opb_host_ops
= {
212 .xlate
= opb_host_xlate
,
215 irqreturn_t
opb_irq_handler(int irq
, void *private)
218 u32 ir
, src
, subvirq
;
220 opb
= (struct opb_pic
*) private;
222 /* Read the OPB MLS Interrupt Register for
223 * asserted interrupts */
224 ir
= opb_in(opb
, OPB_MLSIR
);
229 /* Get 1 - 32 source, *NOT* bit */
232 /* Translate from the OPB's conception of interrupt number to
233 * Linux's virtual IRQ */
235 subvirq
= irq_linear_revmap(opb
->host
, src
);
237 generic_handle_irq(subvirq
);
238 } while ((ir
= opb_in(opb
, OPB_MLSIR
)));
243 struct opb_pic
*opb_pic_init_one(struct device_node
*dn
)
248 if (of_address_to_resource(dn
, 0, &res
)) {
249 printk(KERN_ERR
"opb: Couldn't translate resource\n");
253 opb
= kzalloc(sizeof(struct opb_pic
), GFP_KERNEL
);
255 printk(KERN_ERR
"opb: Failed to allocate opb struct!\n");
259 /* Get access to the OPB MMIO registers */
260 opb
->regs
= ioremap(res
.start
+ 0x10000, 0x1000);
262 printk(KERN_ERR
"opb: Failed to allocate register space!\n");
266 /* Allocate an irq host so that Linux knows that despite only
267 * having one interrupt to issue, we're the controller for multiple
268 * hardware IRQs, so later we can lookup their virtual IRQs. */
270 opb
->host
= irq_alloc_host(dn
, IRQ_HOST_MAP_LINEAR
,
271 OPB_NR_IRQS
, &opb_host_ops
, -1);
274 printk(KERN_ERR
"opb: Failed to allocate IRQ host!\n");
278 opb
->index
= opb_index
++;
279 spin_lock_init(&opb
->lock
);
280 opb
->host
->host_data
= opb
;
282 /* Disable all interrupts by default */
283 opb_out(opb
, OPB_MLSASIER
, 0);
284 opb_out(opb
, OPB_MLSIER
, 0);
286 /* ACK any interrupts left by FW */
287 opb_out(opb
, OPB_MLSIR
, 0xFFFFFFFF);
298 void __init
opb_pic_init(void)
300 struct device_node
*dn
;
305 /* Call init_one for each OPB device */
306 for_each_compatible_node(dn
, NULL
, "ibm,opb") {
308 /* Fill in an OPB struct */
309 opb
= opb_pic_init_one(dn
);
311 printk(KERN_WARNING
"opb: Failed to init node, skipped!\n");
315 /* Map / get opb's hardware virtual irq */
316 virq
= irq_of_parse_and_map(dn
, 0);
318 printk("opb: irq_op_parse_and_map failed!\n");
322 /* Attach opb interrupt handler to new virtual IRQ */
323 rc
= request_irq(virq
, opb_irq_handler
, IRQF_NO_THREAD
,
324 "OPB LS Cascade", opb
);
326 printk("opb: request_irq failed: %d\n", rc
);
330 printk("OPB%d init with %d IRQs at %p\n", opb
->index
,
331 OPB_NR_IRQS
, opb
->regs
);