2 * arch/powerpc/sysdev/ipic.c
4 * IPIC routines implementations.
6 * Copyright 2005 Freescale Semiconductor, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/reboot.h>
17 #include <linux/slab.h>
18 #include <linux/stddef.h>
19 #include <linux/sched.h>
20 #include <linux/signal.h>
21 #include <linux/sysdev.h>
22 #include <linux/device.h>
23 #include <linux/bootmem.h>
24 #include <linux/spinlock.h>
32 static struct ipic
* primary_ipic
;
33 static struct irq_chip ipic_level_irq_chip
, ipic_edge_irq_chip
;
34 static DEFINE_SPINLOCK(ipic_lock
);
36 static struct ipic_info ipic_info
[] = {
40 .force
= IPIC_SIFCR_H
,
47 .force
= IPIC_SIFCR_H
,
54 .force
= IPIC_SIFCR_H
,
61 .force
= IPIC_SIFCR_H
,
68 .force
= IPIC_SIFCR_H
,
75 .force
= IPIC_SIFCR_H
,
82 .force
= IPIC_SIFCR_H
,
89 .force
= IPIC_SIFCR_H
,
96 .force
= IPIC_SIFCR_H
,
101 .mask
= IPIC_SIMSR_H
,
102 .prio
= IPIC_SIPRR_D
,
103 .force
= IPIC_SIFCR_H
,
108 .mask
= IPIC_SIMSR_H
,
109 .prio
= IPIC_SIPRR_D
,
110 .force
= IPIC_SIFCR_H
,
115 .mask
= IPIC_SIMSR_H
,
116 .prio
= IPIC_SIPRR_D
,
117 .force
= IPIC_SIFCR_H
,
122 .mask
= IPIC_SIMSR_H
,
123 .prio
= IPIC_SIPRR_D
,
124 .force
= IPIC_SIFCR_H
,
129 .mask
= IPIC_SIMSR_H
,
130 .prio
= IPIC_SIPRR_D
,
131 .force
= IPIC_SIFCR_H
,
136 .mask
= IPIC_SIMSR_H
,
137 .prio
= IPIC_SIPRR_D
,
138 .force
= IPIC_SIFCR_H
,
143 .mask
= IPIC_SIMSR_H
,
144 .prio
= IPIC_SIPRR_D
,
145 .force
= IPIC_SIFCR_H
,
152 .prio
= IPIC_SMPRR_A
,
160 .prio
= IPIC_SMPRR_A
,
168 .prio
= IPIC_SMPRR_A
,
176 .prio
= IPIC_SMPRR_B
,
184 .prio
= IPIC_SMPRR_B
,
192 .prio
= IPIC_SMPRR_B
,
200 .prio
= IPIC_SMPRR_B
,
206 .mask
= IPIC_SIMSR_H
,
207 .prio
= IPIC_SIPRR_A
,
208 .force
= IPIC_SIFCR_H
,
213 .mask
= IPIC_SIMSR_H
,
214 .prio
= IPIC_SIPRR_A
,
215 .force
= IPIC_SIFCR_H
,
220 .mask
= IPIC_SIMSR_H
,
221 .prio
= IPIC_SIPRR_A
,
222 .force
= IPIC_SIFCR_H
,
227 .mask
= IPIC_SIMSR_H
,
228 .prio
= IPIC_SIPRR_A
,
229 .force
= IPIC_SIFCR_H
,
234 .mask
= IPIC_SIMSR_H
,
235 .prio
= IPIC_SIPRR_A
,
236 .force
= IPIC_SIFCR_H
,
241 .mask
= IPIC_SIMSR_H
,
242 .prio
= IPIC_SIPRR_A
,
243 .force
= IPIC_SIFCR_H
,
248 .mask
= IPIC_SIMSR_H
,
249 .prio
= IPIC_SIPRR_A
,
250 .force
= IPIC_SIFCR_H
,
255 .mask
= IPIC_SIMSR_H
,
256 .prio
= IPIC_SIPRR_A
,
257 .force
= IPIC_SIFCR_H
,
262 .mask
= IPIC_SIMSR_H
,
263 .prio
= IPIC_SIPRR_B
,
264 .force
= IPIC_SIFCR_H
,
269 .mask
= IPIC_SIMSR_H
,
270 .prio
= IPIC_SIPRR_B
,
271 .force
= IPIC_SIFCR_H
,
276 .mask
= IPIC_SIMSR_H
,
277 .prio
= IPIC_SIPRR_B
,
278 .force
= IPIC_SIFCR_H
,
283 .mask
= IPIC_SIMSR_H
,
284 .prio
= IPIC_SIPRR_B
,
285 .force
= IPIC_SIFCR_H
,
290 .mask
= IPIC_SIMSR_H
,
291 .prio
= IPIC_SIPRR_B
,
292 .force
= IPIC_SIFCR_H
,
297 .mask
= IPIC_SIMSR_H
,
298 .prio
= IPIC_SIPRR_B
,
299 .force
= IPIC_SIFCR_H
,
304 .mask
= IPIC_SIMSR_H
,
305 .prio
= IPIC_SIPRR_B
,
306 .force
= IPIC_SIFCR_H
,
311 .mask
= IPIC_SIMSR_H
,
312 .prio
= IPIC_SIPRR_B
,
313 .force
= IPIC_SIFCR_H
,
319 .prio
= IPIC_SMPRR_A
,
325 .mask
= IPIC_SIMSR_L
,
326 .prio
= IPIC_SMPRR_A
,
327 .force
= IPIC_SIFCR_L
,
332 .mask
= IPIC_SIMSR_L
,
333 .prio
= IPIC_SMPRR_A
,
334 .force
= IPIC_SIFCR_L
,
339 .mask
= IPIC_SIMSR_L
,
340 .prio
= IPIC_SMPRR_A
,
341 .force
= IPIC_SIFCR_L
,
346 .mask
= IPIC_SIMSR_L
,
347 .prio
= IPIC_SMPRR_A
,
348 .force
= IPIC_SIFCR_L
,
353 .mask
= IPIC_SIMSR_L
,
354 .prio
= IPIC_SMPRR_B
,
355 .force
= IPIC_SIFCR_L
,
360 .mask
= IPIC_SIMSR_L
,
361 .prio
= IPIC_SMPRR_B
,
362 .force
= IPIC_SIFCR_L
,
367 .mask
= IPIC_SIMSR_L
,
368 .prio
= IPIC_SMPRR_B
,
369 .force
= IPIC_SIFCR_L
,
374 .mask
= IPIC_SIMSR_L
,
375 .prio
= IPIC_SMPRR_B
,
376 .force
= IPIC_SIFCR_L
,
381 .mask
= IPIC_SIMSR_L
,
383 .force
= IPIC_SIFCR_L
,
387 .mask
= IPIC_SIMSR_L
,
389 .force
= IPIC_SIFCR_L
,
393 .mask
= IPIC_SIMSR_L
,
395 .force
= IPIC_SIFCR_L
,
399 .mask
= IPIC_SIMSR_L
,
401 .force
= IPIC_SIFCR_L
,
405 .mask
= IPIC_SIMSR_L
,
407 .force
= IPIC_SIFCR_L
,
411 .mask
= IPIC_SIMSR_L
,
413 .force
= IPIC_SIFCR_L
,
417 .mask
= IPIC_SIMSR_L
,
419 .force
= IPIC_SIFCR_L
,
423 .mask
= IPIC_SIMSR_L
,
425 .force
= IPIC_SIFCR_L
,
429 .mask
= IPIC_SIMSR_L
,
431 .force
= IPIC_SIFCR_L
,
435 .mask
= IPIC_SIMSR_L
,
437 .force
= IPIC_SIFCR_L
,
441 .mask
= IPIC_SIMSR_L
,
443 .force
= IPIC_SIFCR_L
,
447 .mask
= IPIC_SIMSR_L
,
449 .force
= IPIC_SIFCR_L
,
453 .mask
= IPIC_SIMSR_L
,
455 .force
= IPIC_SIFCR_L
,
459 .mask
= IPIC_SIMSR_L
,
461 .force
= IPIC_SIFCR_L
,
465 .mask
= IPIC_SIMSR_L
,
467 .force
= IPIC_SIFCR_L
,
471 .mask
= IPIC_SIMSR_L
,
473 .force
= IPIC_SIFCR_L
,
477 .mask
= IPIC_SIMSR_L
,
479 .force
= IPIC_SIFCR_L
,
483 .mask
= IPIC_SIMSR_L
,
485 .force
= IPIC_SIFCR_L
,
489 .mask
= IPIC_SIMSR_L
,
491 .force
= IPIC_SIFCR_L
,
495 .mask
= IPIC_SIMSR_L
,
497 .force
= IPIC_SIFCR_L
,
501 .mask
= IPIC_SIMSR_L
,
503 .force
= IPIC_SIFCR_L
,
508 static inline u32
ipic_read(volatile u32 __iomem
*base
, unsigned int reg
)
510 return in_be32(base
+ (reg
>> 2));
513 static inline void ipic_write(volatile u32 __iomem
*base
, unsigned int reg
, u32 value
)
515 out_be32(base
+ (reg
>> 2), value
);
518 static inline struct ipic
* ipic_from_irq(unsigned int virq
)
523 #define ipic_irq_to_hw(virq) ((unsigned int)irq_map[virq].hwirq)
525 static void ipic_unmask_irq(unsigned int virq
)
527 struct ipic
*ipic
= ipic_from_irq(virq
);
528 unsigned int src
= ipic_irq_to_hw(virq
);
532 spin_lock_irqsave(&ipic_lock
, flags
);
534 temp
= ipic_read(ipic
->regs
, ipic_info
[src
].mask
);
535 temp
|= (1 << (31 - ipic_info
[src
].bit
));
536 ipic_write(ipic
->regs
, ipic_info
[src
].mask
, temp
);
538 spin_unlock_irqrestore(&ipic_lock
, flags
);
541 static void ipic_mask_irq(unsigned int virq
)
543 struct ipic
*ipic
= ipic_from_irq(virq
);
544 unsigned int src
= ipic_irq_to_hw(virq
);
548 spin_lock_irqsave(&ipic_lock
, flags
);
550 temp
= ipic_read(ipic
->regs
, ipic_info
[src
].mask
);
551 temp
&= ~(1 << (31 - ipic_info
[src
].bit
));
552 ipic_write(ipic
->regs
, ipic_info
[src
].mask
, temp
);
554 /* mb() can't guarantee that masking is finished. But it does finish
555 * for nearly all cases. */
558 spin_unlock_irqrestore(&ipic_lock
, flags
);
561 static void ipic_ack_irq(unsigned int virq
)
563 struct ipic
*ipic
= ipic_from_irq(virq
);
564 unsigned int src
= ipic_irq_to_hw(virq
);
568 spin_lock_irqsave(&ipic_lock
, flags
);
570 temp
= ipic_read(ipic
->regs
, ipic_info
[src
].ack
);
571 temp
|= (1 << (31 - ipic_info
[src
].bit
));
572 ipic_write(ipic
->regs
, ipic_info
[src
].ack
, temp
);
574 /* mb() can't guarantee that ack is finished. But it does finish
575 * for nearly all cases. */
578 spin_unlock_irqrestore(&ipic_lock
, flags
);
581 static void ipic_mask_irq_and_ack(unsigned int virq
)
583 struct ipic
*ipic
= ipic_from_irq(virq
);
584 unsigned int src
= ipic_irq_to_hw(virq
);
588 spin_lock_irqsave(&ipic_lock
, flags
);
590 temp
= ipic_read(ipic
->regs
, ipic_info
[src
].mask
);
591 temp
&= ~(1 << (31 - ipic_info
[src
].bit
));
592 ipic_write(ipic
->regs
, ipic_info
[src
].mask
, temp
);
594 temp
= ipic_read(ipic
->regs
, ipic_info
[src
].ack
);
595 temp
|= (1 << (31 - ipic_info
[src
].bit
));
596 ipic_write(ipic
->regs
, ipic_info
[src
].ack
, temp
);
598 /* mb() can't guarantee that ack is finished. But it does finish
599 * for nearly all cases. */
602 spin_unlock_irqrestore(&ipic_lock
, flags
);
605 static int ipic_set_irq_type(unsigned int virq
, unsigned int flow_type
)
607 struct ipic
*ipic
= ipic_from_irq(virq
);
608 unsigned int src
= ipic_irq_to_hw(virq
);
609 struct irq_desc
*desc
= get_irq_desc(virq
);
610 unsigned int vold
, vnew
, edibit
;
612 if (flow_type
== IRQ_TYPE_NONE
)
613 flow_type
= IRQ_TYPE_LEVEL_LOW
;
615 /* ipic supports only low assertion and high-to-low change senses
617 if (!(flow_type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_EDGE_FALLING
))) {
618 printk(KERN_ERR
"ipic: sense type 0x%x not supported\n",
622 /* ipic supports only edge mode on external interrupts */
623 if ((flow_type
& IRQ_TYPE_EDGE_FALLING
) && !ipic_info
[src
].ack
) {
624 printk(KERN_ERR
"ipic: edge sense not supported on internal "
629 desc
->status
&= ~(IRQ_TYPE_SENSE_MASK
| IRQ_LEVEL
);
630 desc
->status
|= flow_type
& IRQ_TYPE_SENSE_MASK
;
631 if (flow_type
& IRQ_TYPE_LEVEL_LOW
) {
632 desc
->status
|= IRQ_LEVEL
;
633 desc
->handle_irq
= handle_level_irq
;
634 desc
->chip
= &ipic_level_irq_chip
;
636 desc
->handle_irq
= handle_edge_irq
;
637 desc
->chip
= &ipic_edge_irq_chip
;
640 /* only EXT IRQ senses are programmable on ipic
641 * internal IRQ senses are LEVEL_LOW
643 if (src
== IPIC_IRQ_EXT0
)
646 if (src
>= IPIC_IRQ_EXT1
&& src
<= IPIC_IRQ_EXT7
)
647 edibit
= (14 - (src
- IPIC_IRQ_EXT1
));
649 return (flow_type
& IRQ_TYPE_LEVEL_LOW
) ? 0 : -EINVAL
;
651 vold
= ipic_read(ipic
->regs
, IPIC_SECNR
);
652 if ((flow_type
& IRQ_TYPE_SENSE_MASK
) == IRQ_TYPE_EDGE_FALLING
) {
653 vnew
= vold
| (1 << edibit
);
655 vnew
= vold
& ~(1 << edibit
);
658 ipic_write(ipic
->regs
, IPIC_SECNR
, vnew
);
662 /* level interrupts and edge interrupts have different ack operations */
663 static struct irq_chip ipic_level_irq_chip
= {
664 .typename
= " IPIC ",
665 .unmask
= ipic_unmask_irq
,
666 .mask
= ipic_mask_irq
,
667 .mask_ack
= ipic_mask_irq
,
668 .set_type
= ipic_set_irq_type
,
671 static struct irq_chip ipic_edge_irq_chip
= {
672 .typename
= " IPIC ",
673 .unmask
= ipic_unmask_irq
,
674 .mask
= ipic_mask_irq
,
675 .mask_ack
= ipic_mask_irq_and_ack
,
677 .set_type
= ipic_set_irq_type
,
680 static int ipic_host_match(struct irq_host
*h
, struct device_node
*node
)
682 /* Exact match, unless ipic node is NULL */
683 return h
->of_node
== NULL
|| h
->of_node
== node
;
686 static int ipic_host_map(struct irq_host
*h
, unsigned int virq
,
689 struct ipic
*ipic
= h
->host_data
;
691 set_irq_chip_data(virq
, ipic
);
692 set_irq_chip_and_handler(virq
, &ipic_level_irq_chip
, handle_level_irq
);
694 /* Set default irq type */
695 set_irq_type(virq
, IRQ_TYPE_NONE
);
700 static int ipic_host_xlate(struct irq_host
*h
, struct device_node
*ct
,
701 u32
*intspec
, unsigned int intsize
,
702 irq_hw_number_t
*out_hwirq
, unsigned int *out_flags
)
705 /* interrupt sense values coming from the device tree equal either
706 * LEVEL_LOW (low assertion) or EDGE_FALLING (high-to-low change)
708 *out_hwirq
= intspec
[0];
710 *out_flags
= intspec
[1];
712 *out_flags
= IRQ_TYPE_NONE
;
716 static struct irq_host_ops ipic_host_ops
= {
717 .match
= ipic_host_match
,
718 .map
= ipic_host_map
,
719 .xlate
= ipic_host_xlate
,
722 struct ipic
* __init
ipic_init(struct device_node
*node
, unsigned int flags
)
728 ipic
= alloc_bootmem(sizeof(struct ipic
));
732 memset(ipic
, 0, sizeof(struct ipic
));
734 ipic
->irqhost
= irq_alloc_host(of_node_get(node
), IRQ_HOST_MAP_LINEAR
,
737 if (ipic
->irqhost
== NULL
) {
742 ret
= of_address_to_resource(node
, 0, &res
);
748 ipic
->regs
= ioremap(res
.start
, res
.end
- res
.start
+ 1);
750 ipic
->irqhost
->host_data
= ipic
;
753 ipic_write(ipic
->regs
, IPIC_SICNR
, 0x0);
755 /* default priority scheme is grouped. If spread mode is required
756 * configure SICFR accordingly */
757 if (flags
& IPIC_SPREADMODE_GRP_A
)
759 if (flags
& IPIC_SPREADMODE_GRP_B
)
761 if (flags
& IPIC_SPREADMODE_GRP_C
)
763 if (flags
& IPIC_SPREADMODE_GRP_D
)
765 if (flags
& IPIC_SPREADMODE_MIX_A
)
767 if (flags
& IPIC_SPREADMODE_MIX_B
)
770 ipic_write(ipic
->regs
, IPIC_SICFR
, temp
);
772 /* handle MCP route */
774 if (flags
& IPIC_DISABLE_MCP_OUT
)
776 ipic_write(ipic
->regs
, IPIC_SERCR
, temp
);
778 /* handle routing of IRQ0 to MCP */
779 temp
= ipic_read(ipic
->regs
, IPIC_SEMSR
);
781 if (flags
& IPIC_IRQ0_MCP
)
784 temp
&= ~SEMSR_SIRQ0
;
786 ipic_write(ipic
->regs
, IPIC_SEMSR
, temp
);
789 irq_set_default_host(primary_ipic
->irqhost
);
791 printk ("IPIC (%d IRQ sources) at %p\n", NR_IPIC_INTS
,
797 int ipic_set_priority(unsigned int virq
, unsigned int priority
)
799 struct ipic
*ipic
= ipic_from_irq(virq
);
800 unsigned int src
= ipic_irq_to_hw(virq
);
807 if (ipic_info
[src
].prio
== 0)
810 temp
= ipic_read(ipic
->regs
, ipic_info
[src
].prio
);
813 temp
&= ~(0x7 << (20 + (3 - priority
) * 3));
814 temp
|= ipic_info
[src
].prio_mask
<< (20 + (3 - priority
) * 3);
816 temp
&= ~(0x7 << (4 + (7 - priority
) * 3));
817 temp
|= ipic_info
[src
].prio_mask
<< (4 + (7 - priority
) * 3);
820 ipic_write(ipic
->regs
, ipic_info
[src
].prio
, temp
);
825 void ipic_set_highest_priority(unsigned int virq
)
827 struct ipic
*ipic
= ipic_from_irq(virq
);
828 unsigned int src
= ipic_irq_to_hw(virq
);
831 temp
= ipic_read(ipic
->regs
, IPIC_SICFR
);
833 /* clear and set HPI */
835 temp
|= (src
& 0x7f) << 24;
837 ipic_write(ipic
->regs
, IPIC_SICFR
, temp
);
840 void ipic_set_default_priority(void)
842 ipic_write(primary_ipic
->regs
, IPIC_SIPRR_A
, IPIC_PRIORITY_DEFAULT
);
843 ipic_write(primary_ipic
->regs
, IPIC_SIPRR_B
, IPIC_PRIORITY_DEFAULT
);
844 ipic_write(primary_ipic
->regs
, IPIC_SIPRR_C
, IPIC_PRIORITY_DEFAULT
);
845 ipic_write(primary_ipic
->regs
, IPIC_SIPRR_D
, IPIC_PRIORITY_DEFAULT
);
846 ipic_write(primary_ipic
->regs
, IPIC_SMPRR_A
, IPIC_PRIORITY_DEFAULT
);
847 ipic_write(primary_ipic
->regs
, IPIC_SMPRR_B
, IPIC_PRIORITY_DEFAULT
);
850 void ipic_enable_mcp(enum ipic_mcp_irq mcp_irq
)
852 struct ipic
*ipic
= primary_ipic
;
855 temp
= ipic_read(ipic
->regs
, IPIC_SERMR
);
856 temp
|= (1 << (31 - mcp_irq
));
857 ipic_write(ipic
->regs
, IPIC_SERMR
, temp
);
860 void ipic_disable_mcp(enum ipic_mcp_irq mcp_irq
)
862 struct ipic
*ipic
= primary_ipic
;
865 temp
= ipic_read(ipic
->regs
, IPIC_SERMR
);
866 temp
&= (1 << (31 - mcp_irq
));
867 ipic_write(ipic
->regs
, IPIC_SERMR
, temp
);
870 u32
ipic_get_mcp_status(void)
872 return ipic_read(primary_ipic
->regs
, IPIC_SERMR
);
875 void ipic_clear_mcp_status(u32 mask
)
877 ipic_write(primary_ipic
->regs
, IPIC_SERMR
, mask
);
880 /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
881 unsigned int ipic_get_irq(void)
885 BUG_ON(primary_ipic
== NULL
);
887 #define IPIC_SIVCR_VECTOR_MASK 0x7f
888 irq
= ipic_read(primary_ipic
->regs
, IPIC_SIVCR
) & IPIC_SIVCR_VECTOR_MASK
;
890 if (irq
== 0) /* 0 --> no irq is pending */
893 return irq_linear_revmap(primary_ipic
->irqhost
, irq
);
896 static struct sysdev_class ipic_sysclass
= {
900 static struct sys_device device_ipic
= {
902 .cls
= &ipic_sysclass
,
905 static int __init
init_ipic_sysfs(void)
909 if (!primary_ipic
->regs
)
911 printk(KERN_DEBUG
"Registering ipic with sysfs...\n");
913 rc
= sysdev_class_register(&ipic_sysclass
);
915 printk(KERN_ERR
"Failed registering ipic sys class\n");
918 rc
= sysdev_register(&device_ipic
);
920 printk(KERN_ERR
"Failed registering ipic sys device\n");
926 subsys_initcall(init_ipic_sysfs
);