2 * arch/powerpc/sysdev/qe_lib/qe_ic.c
4 * Copyright (C) 2006 Freescale Semiconductor, Inc. All rights reserved.
6 * Author: Li Yang <leoli@freescale.com>
7 * Based on code from Shlomi Gridish <gridish@freescale.com>
9 * QUICC ENGINE Interrupt Controller
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/reboot.h>
21 #include <linux/slab.h>
22 #include <linux/stddef.h>
23 #include <linux/sched.h>
24 #include <linux/signal.h>
25 #include <linux/device.h>
26 #include <linux/spinlock.h>
30 #include <asm/qe_ic.h>
34 static DEFINE_RAW_SPINLOCK(qe_ic_lock
);
36 static struct qe_ic_info qe_ic_info
[] = {
39 .mask_reg
= QEIC_CIMR
,
41 .pri_reg
= QEIC_CIPWCC
,
45 .mask_reg
= QEIC_CIMR
,
47 .pri_reg
= QEIC_CIPWCC
,
51 .mask_reg
= QEIC_CIMR
,
53 .pri_reg
= QEIC_CIPWCC
,
57 .mask_reg
= QEIC_CIMR
,
59 .pri_reg
= QEIC_CIPZCC
,
63 .mask_reg
= QEIC_CIMR
,
65 .pri_reg
= QEIC_CIPZCC
,
69 .mask_reg
= QEIC_CIMR
,
71 .pri_reg
= QEIC_CIPZCC
,
75 .mask_reg
= QEIC_CIMR
,
77 .pri_reg
= QEIC_CIPZCC
,
81 .mask_reg
= QEIC_CIMR
,
83 .pri_reg
= QEIC_CIPZCC
,
87 .mask_reg
= QEIC_CIMR
,
89 .pri_reg
= QEIC_CIPZCC
,
93 .mask_reg
= QEIC_CRIMR
,
95 .pri_reg
= QEIC_CIPRTA
,
99 .mask_reg
= QEIC_CRIMR
,
101 .pri_reg
= QEIC_CIPRTB
,
105 .mask_reg
= QEIC_CRIMR
,
107 .pri_reg
= QEIC_CIPRTB
,
111 .mask_reg
= QEIC_CRIMR
,
113 .pri_reg
= QEIC_CIPRTB
,
117 .mask_reg
= QEIC_CRIMR
,
119 .pri_reg
= QEIC_CIPRTB
,
123 .mask_reg
= QEIC_CIMR
,
125 .pri_reg
= QEIC_CIPXCC
,
129 .mask_reg
= QEIC_CIMR
,
131 .pri_reg
= QEIC_CIPXCC
,
135 .mask_reg
= QEIC_CIMR
,
137 .pri_reg
= QEIC_CIPXCC
,
141 .mask_reg
= QEIC_CIMR
,
143 .pri_reg
= QEIC_CIPXCC
,
147 .mask_reg
= QEIC_CIMR
,
149 .pri_reg
= QEIC_CIPXCC
,
153 .mask_reg
= QEIC_CIMR
,
155 .pri_reg
= QEIC_CIPYCC
,
159 .mask_reg
= QEIC_CIMR
,
161 .pri_reg
= QEIC_CIPYCC
,
165 .mask_reg
= QEIC_CIMR
,
167 .pri_reg
= QEIC_CIPYCC
,
171 .mask_reg
= QEIC_CIMR
,
173 .pri_reg
= QEIC_CIPYCC
,
177 static inline u32
qe_ic_read(volatile __be32 __iomem
* base
, unsigned int reg
)
179 return in_be32(base
+ (reg
>> 2));
182 static inline void qe_ic_write(volatile __be32 __iomem
* base
, unsigned int reg
,
185 out_be32(base
+ (reg
>> 2), value
);
188 static inline struct qe_ic
*qe_ic_from_irq(unsigned int virq
)
190 return irq_get_chip_data(virq
);
193 static inline struct qe_ic
*qe_ic_from_irq_data(struct irq_data
*d
)
195 return irq_data_get_irq_chip_data(d
);
198 static void qe_ic_unmask_irq(struct irq_data
*d
)
200 struct qe_ic
*qe_ic
= qe_ic_from_irq_data(d
);
201 unsigned int src
= irqd_to_hwirq(d
);
205 raw_spin_lock_irqsave(&qe_ic_lock
, flags
);
207 temp
= qe_ic_read(qe_ic
->regs
, qe_ic_info
[src
].mask_reg
);
208 qe_ic_write(qe_ic
->regs
, qe_ic_info
[src
].mask_reg
,
209 temp
| qe_ic_info
[src
].mask
);
211 raw_spin_unlock_irqrestore(&qe_ic_lock
, flags
);
214 static void qe_ic_mask_irq(struct irq_data
*d
)
216 struct qe_ic
*qe_ic
= qe_ic_from_irq_data(d
);
217 unsigned int src
= irqd_to_hwirq(d
);
221 raw_spin_lock_irqsave(&qe_ic_lock
, flags
);
223 temp
= qe_ic_read(qe_ic
->regs
, qe_ic_info
[src
].mask_reg
);
224 qe_ic_write(qe_ic
->regs
, qe_ic_info
[src
].mask_reg
,
225 temp
& ~qe_ic_info
[src
].mask
);
227 /* Flush the above write before enabling interrupts; otherwise,
228 * spurious interrupts will sometimes happen. To be 100% sure
229 * that the write has reached the device before interrupts are
230 * enabled, the mask register would have to be read back; however,
231 * this is not required for correctness, only to avoid wasting
232 * time on a large number of spurious interrupts. In testing,
233 * a sync reduced the observed spurious interrupts to zero.
237 raw_spin_unlock_irqrestore(&qe_ic_lock
, flags
);
240 static struct irq_chip qe_ic_irq_chip
= {
242 .irq_unmask
= qe_ic_unmask_irq
,
243 .irq_mask
= qe_ic_mask_irq
,
244 .irq_mask_ack
= qe_ic_mask_irq
,
247 static int qe_ic_host_match(struct irq_domain
*h
, struct device_node
*node
,
248 enum irq_domain_bus_token bus_token
)
250 /* Exact match, unless qe_ic node is NULL */
251 struct device_node
*of_node
= irq_domain_get_of_node(h
);
252 return of_node
== NULL
|| of_node
== node
;
255 static int qe_ic_host_map(struct irq_domain
*h
, unsigned int virq
,
258 struct qe_ic
*qe_ic
= h
->host_data
;
259 struct irq_chip
*chip
;
261 if (qe_ic_info
[hw
].mask
== 0) {
262 printk(KERN_ERR
"Can't map reserved IRQ\n");
266 chip
= &qe_ic
->hc_irq
;
268 irq_set_chip_data(virq
, qe_ic
);
269 irq_set_status_flags(virq
, IRQ_LEVEL
);
271 irq_set_chip_and_handler(virq
, chip
, handle_level_irq
);
276 static const struct irq_domain_ops qe_ic_host_ops
= {
277 .match
= qe_ic_host_match
,
278 .map
= qe_ic_host_map
,
279 .xlate
= irq_domain_xlate_onetwocell
,
282 /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
283 unsigned int qe_ic_get_low_irq(struct qe_ic
*qe_ic
)
287 BUG_ON(qe_ic
== NULL
);
289 /* get the interrupt source vector. */
290 irq
= qe_ic_read(qe_ic
->regs
, QEIC_CIVEC
) >> 26;
295 return irq_linear_revmap(qe_ic
->irqhost
, irq
);
298 /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
299 unsigned int qe_ic_get_high_irq(struct qe_ic
*qe_ic
)
303 BUG_ON(qe_ic
== NULL
);
305 /* get the interrupt source vector. */
306 irq
= qe_ic_read(qe_ic
->regs
, QEIC_CHIVEC
) >> 26;
311 return irq_linear_revmap(qe_ic
->irqhost
, irq
);
314 void __init
qe_ic_init(struct device_node
*node
, unsigned int flags
,
315 void (*low_handler
)(struct irq_desc
*desc
),
316 void (*high_handler
)(struct irq_desc
*desc
))
320 u32 temp
= 0, ret
, high_active
= 0;
322 ret
= of_address_to_resource(node
, 0, &res
);
326 qe_ic
= kzalloc(sizeof(*qe_ic
), GFP_KERNEL
);
330 qe_ic
->irqhost
= irq_domain_add_linear(node
, NR_QE_IC_INTS
,
331 &qe_ic_host_ops
, qe_ic
);
332 if (qe_ic
->irqhost
== NULL
) {
337 qe_ic
->regs
= ioremap(res
.start
, resource_size(&res
));
339 qe_ic
->hc_irq
= qe_ic_irq_chip
;
341 qe_ic
->virq_high
= irq_of_parse_and_map(node
, 0);
342 qe_ic
->virq_low
= irq_of_parse_and_map(node
, 1);
344 if (qe_ic
->virq_low
== NO_IRQ
) {
345 printk(KERN_ERR
"Failed to map QE_IC low IRQ\n");
350 /* default priority scheme is grouped. If spread mode is */
351 /* required, configure cicr accordingly. */
352 if (flags
& QE_IC_SPREADMODE_GRP_W
)
354 if (flags
& QE_IC_SPREADMODE_GRP_X
)
356 if (flags
& QE_IC_SPREADMODE_GRP_Y
)
358 if (flags
& QE_IC_SPREADMODE_GRP_Z
)
360 if (flags
& QE_IC_SPREADMODE_GRP_RISCA
)
362 if (flags
& QE_IC_SPREADMODE_GRP_RISCB
)
365 /* choose destination signal for highest priority interrupt */
366 if (flags
& QE_IC_HIGH_SIGNAL
) {
367 temp
|= (SIGNAL_HIGH
<< CICR_HPIT_SHIFT
);
371 qe_ic_write(qe_ic
->regs
, QEIC_CICR
, temp
);
373 irq_set_handler_data(qe_ic
->virq_low
, qe_ic
);
374 irq_set_chained_handler(qe_ic
->virq_low
, low_handler
);
376 if (qe_ic
->virq_high
!= NO_IRQ
&&
377 qe_ic
->virq_high
!= qe_ic
->virq_low
) {
378 irq_set_handler_data(qe_ic
->virq_high
, qe_ic
);
379 irq_set_chained_handler(qe_ic
->virq_high
, high_handler
);
383 void qe_ic_set_highest_priority(unsigned int virq
, int high
)
385 struct qe_ic
*qe_ic
= qe_ic_from_irq(virq
);
386 unsigned int src
= virq_to_hw(virq
);
389 temp
= qe_ic_read(qe_ic
->regs
, QEIC_CICR
);
391 temp
&= ~CICR_HP_MASK
;
392 temp
|= src
<< CICR_HP_SHIFT
;
394 temp
&= ~CICR_HPIT_MASK
;
395 temp
|= (high
? SIGNAL_HIGH
: SIGNAL_LOW
) << CICR_HPIT_SHIFT
;
397 qe_ic_write(qe_ic
->regs
, QEIC_CICR
, temp
);
400 /* Set Priority level within its group, from 1 to 8 */
401 int qe_ic_set_priority(unsigned int virq
, unsigned int priority
)
403 struct qe_ic
*qe_ic
= qe_ic_from_irq(virq
);
404 unsigned int src
= virq_to_hw(virq
);
407 if (priority
> 8 || priority
== 0)
411 if (qe_ic_info
[src
].pri_reg
== 0)
414 temp
= qe_ic_read(qe_ic
->regs
, qe_ic_info
[src
].pri_reg
);
417 temp
&= ~(0x7 << (32 - priority
* 3));
418 temp
|= qe_ic_info
[src
].pri_code
<< (32 - priority
* 3);
420 temp
&= ~(0x7 << (24 - priority
* 3));
421 temp
|= qe_ic_info
[src
].pri_code
<< (24 - priority
* 3);
424 qe_ic_write(qe_ic
->regs
, qe_ic_info
[src
].pri_reg
, temp
);
429 /* Set a QE priority to use high irq, only priority 1~2 can use high irq */
430 int qe_ic_set_high_priority(unsigned int virq
, unsigned int priority
, int high
)
432 struct qe_ic
*qe_ic
= qe_ic_from_irq(virq
);
433 unsigned int src
= virq_to_hw(virq
);
434 u32 temp
, control_reg
= QEIC_CICNR
, shift
= 0;
436 if (priority
> 2 || priority
== 0)
439 switch (qe_ic_info
[src
].pri_reg
) {
441 shift
= CICNR_ZCC1T_SHIFT
;
444 shift
= CICNR_WCC1T_SHIFT
;
447 shift
= CICNR_YCC1T_SHIFT
;
450 shift
= CICNR_XCC1T_SHIFT
;
453 shift
= CRICR_RTA1T_SHIFT
;
454 control_reg
= QEIC_CRICR
;
457 shift
= CRICR_RTB1T_SHIFT
;
458 control_reg
= QEIC_CRICR
;
464 shift
+= (2 - priority
) * 2;
465 temp
= qe_ic_read(qe_ic
->regs
, control_reg
);
466 temp
&= ~(SIGNAL_MASK
<< shift
);
467 temp
|= (high
? SIGNAL_HIGH
: SIGNAL_LOW
) << shift
;
468 qe_ic_write(qe_ic
->regs
, control_reg
, temp
);
473 static struct bus_type qe_ic_subsys
= {
478 static struct device device_qe_ic
= {
480 .bus
= &qe_ic_subsys
,
483 static int __init
init_qe_ic_sysfs(void)
487 printk(KERN_DEBUG
"Registering qe_ic with sysfs...\n");
489 rc
= subsys_system_register(&qe_ic_subsys
, NULL
);
491 printk(KERN_ERR
"Failed registering qe_ic sys class\n");
494 rc
= device_register(&device_qe_ic
);
496 printk(KERN_ERR
"Failed registering qe_ic sys device\n");
502 subsys_initcall(init_qe_ic_sysfs
);