xref: /linux/arch/mips/rb532/irq.c (revision 0337966d121ebebf73a1c346123e8112796e684e)
173b4390fSRalf Baechle /*
273b4390fSRalf Baechle  *  This program is free software; you can redistribute  it and/or modify it
373b4390fSRalf Baechle  *  under  the terms of  the GNU General  Public License as published by the
473b4390fSRalf Baechle  *  Free Software Foundation;  either version 2 of the  License, or (at your
573b4390fSRalf Baechle  *  option) any later version.
673b4390fSRalf Baechle  *
773b4390fSRalf Baechle  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
873b4390fSRalf Baechle  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
973b4390fSRalf Baechle  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
1073b4390fSRalf Baechle  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
1173b4390fSRalf Baechle  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1273b4390fSRalf Baechle  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
1373b4390fSRalf Baechle  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1473b4390fSRalf Baechle  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
1573b4390fSRalf Baechle  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1673b4390fSRalf Baechle  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1773b4390fSRalf Baechle  *
1873b4390fSRalf Baechle  *  You should have received a copy of the  GNU General Public License along
1973b4390fSRalf Baechle  *  with this program; if not, write  to the Free Software Foundation, Inc.,
2073b4390fSRalf Baechle  *  675 Mass Ave, Cambridge, MA 02139, USA.
2173b4390fSRalf Baechle  *
2273b4390fSRalf Baechle  * Copyright 2002 MontaVista Software Inc.
2373b4390fSRalf Baechle  * Author: MontaVista Software, Inc.
2473b4390fSRalf Baechle  *		stevel@mvista.com or source@mvista.com
2573b4390fSRalf Baechle  */
2673b4390fSRalf Baechle 
2773b4390fSRalf Baechle #include <linux/bitops.h>
2873b4390fSRalf Baechle #include <linux/errno.h>
2973b4390fSRalf Baechle #include <linux/init.h>
3073b4390fSRalf Baechle #include <linux/io.h>
3173b4390fSRalf Baechle #include <linux/kernel_stat.h>
3273b4390fSRalf Baechle #include <linux/signal.h>
3373b4390fSRalf Baechle #include <linux/sched.h>
3473b4390fSRalf Baechle #include <linux/types.h>
3573b4390fSRalf Baechle #include <linux/interrupt.h>
3673b4390fSRalf Baechle #include <linux/ioport.h>
3773b4390fSRalf Baechle #include <linux/timex.h>
3873b4390fSRalf Baechle #include <linux/random.h>
3973b4390fSRalf Baechle #include <linux/delay.h>
4073b4390fSRalf Baechle 
4173b4390fSRalf Baechle #include <asm/bootinfo.h>
4273b4390fSRalf Baechle #include <asm/time.h>
4373b4390fSRalf Baechle #include <asm/mipsregs.h>
4473b4390fSRalf Baechle 
45606a083bSFlorian Fainelli #include <asm/mach-rc32434/irq.h>
464aa0f4d7SPhil Sutter #include <asm/mach-rc32434/gpio.h>
4773b4390fSRalf Baechle 
4873b4390fSRalf Baechle struct intr_group {
4973b4390fSRalf Baechle 	u32 mask;	/* mask of valid bits in pending/mask registers */
5073b4390fSRalf Baechle 	volatile u32 *base_addr;
5173b4390fSRalf Baechle };
5273b4390fSRalf Baechle 
5373b4390fSRalf Baechle #define RC32434_NR_IRQS	 (GROUP4_IRQ_BASE + 32)
5473b4390fSRalf Baechle 
5573b4390fSRalf Baechle #if (NR_IRQS < RC32434_NR_IRQS)
5673b4390fSRalf Baechle #error Too little irqs defined. Did you override <asm/irq.h> ?
5773b4390fSRalf Baechle #endif
5873b4390fSRalf Baechle 
5973b4390fSRalf Baechle static const struct intr_group intr_group[NUM_INTR_GROUPS] = {
6073b4390fSRalf Baechle 	{
6173b4390fSRalf Baechle 		.mask	= 0x0000efff,
6273b4390fSRalf Baechle 		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 0 * IC_GROUP_OFFSET)},
6373b4390fSRalf Baechle 	{
6473b4390fSRalf Baechle 		.mask	= 0x00001fff,
6573b4390fSRalf Baechle 		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 1 * IC_GROUP_OFFSET)},
6673b4390fSRalf Baechle 	{
6773b4390fSRalf Baechle 		.mask	= 0x00000007,
6873b4390fSRalf Baechle 		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 2 * IC_GROUP_OFFSET)},
6973b4390fSRalf Baechle 	{
7073b4390fSRalf Baechle 		.mask	= 0x0003ffff,
7173b4390fSRalf Baechle 		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 3 * IC_GROUP_OFFSET)},
7273b4390fSRalf Baechle 	{
7373b4390fSRalf Baechle 		.mask	= 0xffffffff,
7473b4390fSRalf Baechle 		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 4 * IC_GROUP_OFFSET)}
7573b4390fSRalf Baechle };
7673b4390fSRalf Baechle 
7773b4390fSRalf Baechle #define READ_PEND(base) (*(base))
7873b4390fSRalf Baechle #define READ_MASK(base) (*(base + 2))
7973b4390fSRalf Baechle #define WRITE_MASK(base, val) (*(base + 2) = (val))
8073b4390fSRalf Baechle 
irq_to_group(unsigned int irq_nr)8173b4390fSRalf Baechle static inline int irq_to_group(unsigned int irq_nr)
8273b4390fSRalf Baechle {
8373b4390fSRalf Baechle 	return (irq_nr - GROUP0_IRQ_BASE) >> 5;
8473b4390fSRalf Baechle }
8573b4390fSRalf Baechle 
group_to_ip(unsigned int group)8673b4390fSRalf Baechle static inline int group_to_ip(unsigned int group)
8773b4390fSRalf Baechle {
8873b4390fSRalf Baechle 	return group + 2;
8973b4390fSRalf Baechle }
9073b4390fSRalf Baechle 
enable_local_irq(unsigned int ip)9173b4390fSRalf Baechle static inline void enable_local_irq(unsigned int ip)
9273b4390fSRalf Baechle {
9373b4390fSRalf Baechle 	int ipnum = 0x100 << ip;
9473b4390fSRalf Baechle 
9573b4390fSRalf Baechle 	set_c0_status(ipnum);
9673b4390fSRalf Baechle }
9773b4390fSRalf Baechle 
disable_local_irq(unsigned int ip)9873b4390fSRalf Baechle static inline void disable_local_irq(unsigned int ip)
9973b4390fSRalf Baechle {
10073b4390fSRalf Baechle 	int ipnum = 0x100 << ip;
10173b4390fSRalf Baechle 
10273b4390fSRalf Baechle 	clear_c0_status(ipnum);
10373b4390fSRalf Baechle }
10473b4390fSRalf Baechle 
ack_local_irq(unsigned int ip)10573b4390fSRalf Baechle static inline void ack_local_irq(unsigned int ip)
10673b4390fSRalf Baechle {
10773b4390fSRalf Baechle 	int ipnum = 0x100 << ip;
10873b4390fSRalf Baechle 
10973b4390fSRalf Baechle 	clear_c0_cause(ipnum);
11073b4390fSRalf Baechle }
11173b4390fSRalf Baechle 
rb532_enable_irq(struct irq_data * d)112efb02da5SThomas Gleixner static void rb532_enable_irq(struct irq_data *d)
11373b4390fSRalf Baechle {
114efb02da5SThomas Gleixner 	unsigned int group, intr_bit, irq_nr = d->irq;
11573b4390fSRalf Baechle 	int ip = irq_nr - GROUP0_IRQ_BASE;
11673b4390fSRalf Baechle 	volatile unsigned int *addr;
11773b4390fSRalf Baechle 
11873b4390fSRalf Baechle 	if (ip < 0)
11973b4390fSRalf Baechle 		enable_local_irq(irq_nr);
12073b4390fSRalf Baechle 	else {
12173b4390fSRalf Baechle 		group = ip >> 5;
12273b4390fSRalf Baechle 
12373b4390fSRalf Baechle 		ip &= (1 << 5) - 1;
12473b4390fSRalf Baechle 		intr_bit = 1 << ip;
12573b4390fSRalf Baechle 
12673b4390fSRalf Baechle 		enable_local_irq(group_to_ip(group));
12773b4390fSRalf Baechle 
12873b4390fSRalf Baechle 		addr = intr_group[group].base_addr;
12973b4390fSRalf Baechle 		WRITE_MASK(addr, READ_MASK(addr) & ~intr_bit);
13073b4390fSRalf Baechle 	}
13173b4390fSRalf Baechle }
13273b4390fSRalf Baechle 
rb532_disable_irq(struct irq_data * d)133efb02da5SThomas Gleixner static void rb532_disable_irq(struct irq_data *d)
13473b4390fSRalf Baechle {
135efb02da5SThomas Gleixner 	unsigned int group, intr_bit, mask, irq_nr = d->irq;
13673b4390fSRalf Baechle 	int ip = irq_nr - GROUP0_IRQ_BASE;
13773b4390fSRalf Baechle 	volatile unsigned int *addr;
13873b4390fSRalf Baechle 
13973b4390fSRalf Baechle 	if (ip < 0) {
14073b4390fSRalf Baechle 		disable_local_irq(irq_nr);
14173b4390fSRalf Baechle 	} else {
14273b4390fSRalf Baechle 		group = ip >> 5;
14373b4390fSRalf Baechle 
14473b4390fSRalf Baechle 		ip &= (1 << 5) - 1;
14573b4390fSRalf Baechle 		intr_bit = 1 << ip;
14673b4390fSRalf Baechle 		addr = intr_group[group].base_addr;
14773b4390fSRalf Baechle 		mask = READ_MASK(addr);
14873b4390fSRalf Baechle 		mask |= intr_bit;
14973b4390fSRalf Baechle 		WRITE_MASK(addr, mask);
15073b4390fSRalf Baechle 
151d36773e5SFlorian Fainelli 		/* There is a maximum of 14 GPIO interrupts */
152d36773e5SFlorian Fainelli 		if (group == GPIO_MAPPED_IRQ_GROUP && irq_nr <= (GROUP4_IRQ_BASE + 13))
1534aa0f4d7SPhil Sutter 			rb532_gpio_set_istat(0, irq_nr - GPIO_MAPPED_IRQ_BASE);
1544aa0f4d7SPhil Sutter 
15573b4390fSRalf Baechle 		/*
15673b4390fSRalf Baechle 		 * if there are no more interrupts enabled in this
15773b4390fSRalf Baechle 		 * group, disable corresponding IP
15873b4390fSRalf Baechle 		 */
15973b4390fSRalf Baechle 		if (mask == intr_group[group].mask)
16073b4390fSRalf Baechle 			disable_local_irq(group_to_ip(group));
16173b4390fSRalf Baechle 	}
16273b4390fSRalf Baechle }
16373b4390fSRalf Baechle 
rb532_mask_and_ack_irq(struct irq_data * d)164efb02da5SThomas Gleixner static void rb532_mask_and_ack_irq(struct irq_data *d)
16573b4390fSRalf Baechle {
166efb02da5SThomas Gleixner 	rb532_disable_irq(d);
167efb02da5SThomas Gleixner 	ack_local_irq(group_to_ip(irq_to_group(d->irq)));
16873b4390fSRalf Baechle }
16973b4390fSRalf Baechle 
rb532_set_type(struct irq_data * d,unsigned type)170efb02da5SThomas Gleixner static int rb532_set_type(struct irq_data *d,  unsigned type)
1714aa0f4d7SPhil Sutter {
172efb02da5SThomas Gleixner 	int gpio = d->irq - GPIO_MAPPED_IRQ_BASE;
173efb02da5SThomas Gleixner 	int group = irq_to_group(d->irq);
1744aa0f4d7SPhil Sutter 
175efb02da5SThomas Gleixner 	if (group != GPIO_MAPPED_IRQ_GROUP || d->irq > (GROUP4_IRQ_BASE + 13))
1764aa0f4d7SPhil Sutter 		return (type == IRQ_TYPE_LEVEL_HIGH) ? 0 : -EINVAL;
1774aa0f4d7SPhil Sutter 
1784aa0f4d7SPhil Sutter 	switch (type) {
1794aa0f4d7SPhil Sutter 	case IRQ_TYPE_LEVEL_HIGH:
1804aa0f4d7SPhil Sutter 		rb532_gpio_set_ilevel(1, gpio);
1814aa0f4d7SPhil Sutter 		break;
1824aa0f4d7SPhil Sutter 	case IRQ_TYPE_LEVEL_LOW:
1834aa0f4d7SPhil Sutter 		rb532_gpio_set_ilevel(0, gpio);
1844aa0f4d7SPhil Sutter 		break;
1854aa0f4d7SPhil Sutter 	default:
1864aa0f4d7SPhil Sutter 		return -EINVAL;
1874aa0f4d7SPhil Sutter 	}
1884aa0f4d7SPhil Sutter 
1894aa0f4d7SPhil Sutter 	return 0;
1904aa0f4d7SPhil Sutter }
1914aa0f4d7SPhil Sutter 
19273b4390fSRalf Baechle static struct irq_chip rc32434_irq_type = {
19373b4390fSRalf Baechle 	.name		= "RB532",
194efb02da5SThomas Gleixner 	.irq_ack	= rb532_disable_irq,
195efb02da5SThomas Gleixner 	.irq_mask	= rb532_disable_irq,
196efb02da5SThomas Gleixner 	.irq_mask_ack	= rb532_mask_and_ack_irq,
197efb02da5SThomas Gleixner 	.irq_unmask	= rb532_enable_irq,
198efb02da5SThomas Gleixner 	.irq_set_type	= rb532_set_type,
19973b4390fSRalf Baechle };
20073b4390fSRalf Baechle 
arch_init_irq(void)20173b4390fSRalf Baechle void __init arch_init_irq(void)
20273b4390fSRalf Baechle {
20373b4390fSRalf Baechle 	int i;
20473b4390fSRalf Baechle 
20573b4390fSRalf Baechle 	pr_info("Initializing IRQ's: %d out of %d\n", RC32434_NR_IRQS, NR_IRQS);
20673b4390fSRalf Baechle 
20773b4390fSRalf Baechle 	for (i = 0; i < RC32434_NR_IRQS; i++)
208*e4ec7989SThomas Gleixner 		irq_set_chip_and_handler(i, &rc32434_irq_type,
20973b4390fSRalf Baechle 					 handle_level_irq);
21073b4390fSRalf Baechle }
21173b4390fSRalf Baechle 
21273b4390fSRalf Baechle /* Main Interrupt dispatcher */
plat_irq_dispatch(void)21373b4390fSRalf Baechle asmlinkage void plat_irq_dispatch(void)
21473b4390fSRalf Baechle {
21573b4390fSRalf Baechle 	unsigned int ip, pend, group;
21673b4390fSRalf Baechle 	volatile unsigned int *addr;
21773b4390fSRalf Baechle 	unsigned int cp0_cause = read_c0_cause() & read_c0_status();
21873b4390fSRalf Baechle 
21973b4390fSRalf Baechle 	if (cp0_cause & CAUSEF_IP7) {
22073b4390fSRalf Baechle 		do_IRQ(7);
22173b4390fSRalf Baechle 	} else {
22273b4390fSRalf Baechle 		ip = (cp0_cause & 0x7c00);
22373b4390fSRalf Baechle 		if (ip) {
22473b4390fSRalf Baechle 			group = 21 + (fls(ip) - 32);
22573b4390fSRalf Baechle 
22673b4390fSRalf Baechle 			addr = intr_group[group].base_addr;
22773b4390fSRalf Baechle 
22873b4390fSRalf Baechle 			pend = READ_PEND(addr);
22973b4390fSRalf Baechle 			pend &= ~READ_MASK(addr);	/* only unmasked interrupts */
23073b4390fSRalf Baechle 			pend = 39 + (fls(pend) - 32);
23173b4390fSRalf Baechle 			do_IRQ((group << 5) + pend);
23273b4390fSRalf Baechle 		}
23373b4390fSRalf Baechle 	}
23473b4390fSRalf Baechle }
235