18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  This program is free software; you can redistribute  it and/or modify it
38c2ecf20Sopenharmony_ci *  under  the terms of  the GNU General  Public License as published by the
48c2ecf20Sopenharmony_ci *  Free Software Foundation;  either version 2 of the  License, or (at your
58c2ecf20Sopenharmony_ci *  option) any later version.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
88c2ecf20Sopenharmony_ci *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
98c2ecf20Sopenharmony_ci *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
108c2ecf20Sopenharmony_ci *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
118c2ecf20Sopenharmony_ci *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
128c2ecf20Sopenharmony_ci *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
138c2ecf20Sopenharmony_ci *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
148c2ecf20Sopenharmony_ci *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
158c2ecf20Sopenharmony_ci *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
168c2ecf20Sopenharmony_ci *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *  You should have received a copy of the  GNU General Public License along
198c2ecf20Sopenharmony_ci *  with this program; if not, write  to the Free Software Foundation, Inc.,
208c2ecf20Sopenharmony_ci *  675 Mass Ave, Cambridge, MA 02139, USA.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Copyright 2002 MontaVista Software Inc.
238c2ecf20Sopenharmony_ci * Author: MontaVista Software, Inc.
248c2ecf20Sopenharmony_ci *		stevel@mvista.com or source@mvista.com
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/bitops.h>
288c2ecf20Sopenharmony_ci#include <linux/errno.h>
298c2ecf20Sopenharmony_ci#include <linux/init.h>
308c2ecf20Sopenharmony_ci#include <linux/io.h>
318c2ecf20Sopenharmony_ci#include <linux/kernel_stat.h>
328c2ecf20Sopenharmony_ci#include <linux/signal.h>
338c2ecf20Sopenharmony_ci#include <linux/sched.h>
348c2ecf20Sopenharmony_ci#include <linux/types.h>
358c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
368c2ecf20Sopenharmony_ci#include <linux/ioport.h>
378c2ecf20Sopenharmony_ci#include <linux/timex.h>
388c2ecf20Sopenharmony_ci#include <linux/random.h>
398c2ecf20Sopenharmony_ci#include <linux/delay.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include <asm/bootinfo.h>
428c2ecf20Sopenharmony_ci#include <asm/time.h>
438c2ecf20Sopenharmony_ci#include <asm/mipsregs.h>
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#include <asm/mach-rc32434/irq.h>
468c2ecf20Sopenharmony_ci#include <asm/mach-rc32434/gpio.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct intr_group {
498c2ecf20Sopenharmony_ci	u32 mask;	/* mask of valid bits in pending/mask registers */
508c2ecf20Sopenharmony_ci	volatile u32 *base_addr;
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define RC32434_NR_IRQS	 (GROUP4_IRQ_BASE + 32)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#if (NR_IRQS < RC32434_NR_IRQS)
568c2ecf20Sopenharmony_ci#error Too little irqs defined. Did you override <asm/irq.h> ?
578c2ecf20Sopenharmony_ci#endif
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic const struct intr_group intr_group[NUM_INTR_GROUPS] = {
608c2ecf20Sopenharmony_ci	{
618c2ecf20Sopenharmony_ci		.mask	= 0x0000efff,
628c2ecf20Sopenharmony_ci		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 0 * IC_GROUP_OFFSET)},
638c2ecf20Sopenharmony_ci	{
648c2ecf20Sopenharmony_ci		.mask	= 0x00001fff,
658c2ecf20Sopenharmony_ci		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 1 * IC_GROUP_OFFSET)},
668c2ecf20Sopenharmony_ci	{
678c2ecf20Sopenharmony_ci		.mask	= 0x00000007,
688c2ecf20Sopenharmony_ci		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 2 * IC_GROUP_OFFSET)},
698c2ecf20Sopenharmony_ci	{
708c2ecf20Sopenharmony_ci		.mask	= 0x0003ffff,
718c2ecf20Sopenharmony_ci		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 3 * IC_GROUP_OFFSET)},
728c2ecf20Sopenharmony_ci	{
738c2ecf20Sopenharmony_ci		.mask	= 0xffffffff,
748c2ecf20Sopenharmony_ci		.base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 4 * IC_GROUP_OFFSET)}
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define READ_PEND(base) (*(base))
788c2ecf20Sopenharmony_ci#define READ_MASK(base) (*(base + 2))
798c2ecf20Sopenharmony_ci#define WRITE_MASK(base, val) (*(base + 2) = (val))
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic inline int irq_to_group(unsigned int irq_nr)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	return (irq_nr - GROUP0_IRQ_BASE) >> 5;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic inline int group_to_ip(unsigned int group)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return group + 2;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic inline void enable_local_irq(unsigned int ip)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	int ipnum = 0x100 << ip;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	set_c0_status(ipnum);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic inline void disable_local_irq(unsigned int ip)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	int ipnum = 0x100 << ip;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	clear_c0_status(ipnum);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic inline void ack_local_irq(unsigned int ip)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	int ipnum = 0x100 << ip;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	clear_c0_cause(ipnum);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic void rb532_enable_irq(struct irq_data *d)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	unsigned int group, intr_bit, irq_nr = d->irq;
1158c2ecf20Sopenharmony_ci	int ip = irq_nr - GROUP0_IRQ_BASE;
1168c2ecf20Sopenharmony_ci	volatile unsigned int *addr;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (ip < 0)
1198c2ecf20Sopenharmony_ci		enable_local_irq(irq_nr);
1208c2ecf20Sopenharmony_ci	else {
1218c2ecf20Sopenharmony_ci		group = ip >> 5;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci		ip &= (1 << 5) - 1;
1248c2ecf20Sopenharmony_ci		intr_bit = 1 << ip;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci		enable_local_irq(group_to_ip(group));
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci		addr = intr_group[group].base_addr;
1298c2ecf20Sopenharmony_ci		WRITE_MASK(addr, READ_MASK(addr) & ~intr_bit);
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic void rb532_disable_irq(struct irq_data *d)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	unsigned int group, intr_bit, mask, irq_nr = d->irq;
1368c2ecf20Sopenharmony_ci	int ip = irq_nr - GROUP0_IRQ_BASE;
1378c2ecf20Sopenharmony_ci	volatile unsigned int *addr;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (ip < 0) {
1408c2ecf20Sopenharmony_ci		disable_local_irq(irq_nr);
1418c2ecf20Sopenharmony_ci	} else {
1428c2ecf20Sopenharmony_ci		group = ip >> 5;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci		ip &= (1 << 5) - 1;
1458c2ecf20Sopenharmony_ci		intr_bit = 1 << ip;
1468c2ecf20Sopenharmony_ci		addr = intr_group[group].base_addr;
1478c2ecf20Sopenharmony_ci		mask = READ_MASK(addr);
1488c2ecf20Sopenharmony_ci		mask |= intr_bit;
1498c2ecf20Sopenharmony_ci		WRITE_MASK(addr, mask);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		/* There is a maximum of 14 GPIO interrupts */
1528c2ecf20Sopenharmony_ci		if (group == GPIO_MAPPED_IRQ_GROUP && irq_nr <= (GROUP4_IRQ_BASE + 13))
1538c2ecf20Sopenharmony_ci			rb532_gpio_set_istat(0, irq_nr - GPIO_MAPPED_IRQ_BASE);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		/*
1568c2ecf20Sopenharmony_ci		 * if there are no more interrupts enabled in this
1578c2ecf20Sopenharmony_ci		 * group, disable corresponding IP
1588c2ecf20Sopenharmony_ci		 */
1598c2ecf20Sopenharmony_ci		if (mask == intr_group[group].mask)
1608c2ecf20Sopenharmony_ci			disable_local_irq(group_to_ip(group));
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void rb532_mask_and_ack_irq(struct irq_data *d)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	rb532_disable_irq(d);
1678c2ecf20Sopenharmony_ci	ack_local_irq(group_to_ip(irq_to_group(d->irq)));
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int rb532_set_type(struct irq_data *d,  unsigned type)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	int gpio = d->irq - GPIO_MAPPED_IRQ_BASE;
1738c2ecf20Sopenharmony_ci	int group = irq_to_group(d->irq);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (group != GPIO_MAPPED_IRQ_GROUP || d->irq > (GROUP4_IRQ_BASE + 13))
1768c2ecf20Sopenharmony_ci		return (type == IRQ_TYPE_LEVEL_HIGH) ? 0 : -EINVAL;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	switch (type) {
1798c2ecf20Sopenharmony_ci	case IRQ_TYPE_LEVEL_HIGH:
1808c2ecf20Sopenharmony_ci		rb532_gpio_set_ilevel(1, gpio);
1818c2ecf20Sopenharmony_ci		break;
1828c2ecf20Sopenharmony_ci	case IRQ_TYPE_LEVEL_LOW:
1838c2ecf20Sopenharmony_ci		rb532_gpio_set_ilevel(0, gpio);
1848c2ecf20Sopenharmony_ci		break;
1858c2ecf20Sopenharmony_ci	default:
1868c2ecf20Sopenharmony_ci		return -EINVAL;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return 0;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic struct irq_chip rc32434_irq_type = {
1938c2ecf20Sopenharmony_ci	.name		= "RB532",
1948c2ecf20Sopenharmony_ci	.irq_ack	= rb532_disable_irq,
1958c2ecf20Sopenharmony_ci	.irq_mask	= rb532_disable_irq,
1968c2ecf20Sopenharmony_ci	.irq_mask_ack	= rb532_mask_and_ack_irq,
1978c2ecf20Sopenharmony_ci	.irq_unmask	= rb532_enable_irq,
1988c2ecf20Sopenharmony_ci	.irq_set_type	= rb532_set_type,
1998c2ecf20Sopenharmony_ci};
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_civoid __init arch_init_irq(void)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	int i;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	pr_info("Initializing IRQ's: %d out of %d\n", RC32434_NR_IRQS, NR_IRQS);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	for (i = 0; i < RC32434_NR_IRQS; i++)
2088c2ecf20Sopenharmony_ci		irq_set_chip_and_handler(i, &rc32434_irq_type,
2098c2ecf20Sopenharmony_ci					 handle_level_irq);
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/* Main Interrupt dispatcher */
2138c2ecf20Sopenharmony_ciasmlinkage void plat_irq_dispatch(void)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	unsigned int ip, pend, group;
2168c2ecf20Sopenharmony_ci	volatile unsigned int *addr;
2178c2ecf20Sopenharmony_ci	unsigned int cp0_cause = read_c0_cause() & read_c0_status();
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	if (cp0_cause & CAUSEF_IP7) {
2208c2ecf20Sopenharmony_ci		do_IRQ(7);
2218c2ecf20Sopenharmony_ci	} else {
2228c2ecf20Sopenharmony_ci		ip = (cp0_cause & 0x7c00);
2238c2ecf20Sopenharmony_ci		if (ip) {
2248c2ecf20Sopenharmony_ci			group = 21 + (fls(ip) - 32);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci			addr = intr_group[group].base_addr;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci			pend = READ_PEND(addr);
2298c2ecf20Sopenharmony_ci			pend &= ~READ_MASK(addr);	/* only unmasked interrupts */
2308c2ecf20Sopenharmony_ci			pend = 39 + (fls(pend) - 32);
2318c2ecf20Sopenharmony_ci			do_IRQ((group << 5) + pend);
2328c2ecf20Sopenharmony_ci		}
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci}
235