/* $Id: irq.c,v 1.19 1999/07/22 17:23:38 bjornw Exp $
 *
 *	linux/arch/etrax100/kernel/irq.c
 *
 *	Copyright (C) 1992 Linus Torvalds
 *                    1998 Axis Communications AB
 *
 * This file contains the code used by various IRQ handling routines:
 * asking for different IRQ's should be done through these routines
 * instead of just grabbing them. Thus setups with different IRQ numbers
 * shouldn't result in any weird surprises, and installing new handlers
 * should be easier.
 */

/*
 * IRQ's are in fact implemented a bit like signal handlers for the kernel.
 * Naturally it's not a 1:1 relation, but there are similarities.
 */

#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/kernel_stat.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/timex.h>
#include <linux/malloc.h>
#include <linux/random.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/bitops.h>

#include <asm/svinto.h>

char *hw_bp_msg = "BP 0x%x\n";

static inline void
mask_irq(unsigned int irq_nr)
{
	*R_VECT_MASK_CLR = 1 << irq_nr;
}

static inline void
unmask_irq(unsigned int irq_nr)
{
	*R_VECT_MASK_SET = 1 << irq_nr;
}

void
disable_irq(unsigned int irq_nr)
{
	unsigned long flags;
	
	save_flags(flags);
	cli();
	mask_irq(irq_nr);
	restore_flags(flags);
}

void
enable_irq(unsigned int irq_nr)
{
	unsigned long flags;
	save_flags(flags);
	cli();
	unmask_irq(irq_nr);
	restore_flags(flags);
}

unsigned long
probe_irq_on()
{
	return 0;
}

int
probe_irq_off(unsigned long x)
{
	return 0;
}

irqvectptr irq_shortcuts[NR_IRQS]; /* vector of shortcut jumps after the irq prologue */

/* don't use set_int_vector, it bypasses the linux interrupt handlers. it is
 * global just so that the kernel gdb can use it.
 */

void
set_int_vector(int n, irqvectptr addr, irqvectptr saddr)
{
	/* remember the shortcut entry point, after the prologue */

	irq_shortcuts[n] = saddr;

	/* Svinto-1 has a bug that causes random IRQ vectors to be generated. To
	   circumvent that we put the multiple_interrupt handler at every position,
	   and that means we shouldn't write the actual vector into etrax_irv here
	   but just remember the shortcut.
	*/

#if !defined(CONFIG_SVINTO1)
	etrax_irv->v[n + 0x20] = (irqvectptr)addr;
#endif
}

/* the breakpoint vector is obviously not made just like the normal irq handlers
 * but needs to contain _code_ to jump to addr.
 *
 * the BREAK n instruction jumps to IBR + n * 8
 */

void
set_break_vector(int n, irqvectptr addr)
{
	unsigned short *jinstr = (unsigned short *)&etrax_irv->v[n*2];
	unsigned long *jaddr = (unsigned long *)(jinstr + 1);
	
	*jinstr = 0x0d3f;
	*jaddr = addr;

	/* 00000026 <clrlop+1a> 3f0d82000000     jump  0x82 */
}


/*
 * This builds up the IRQ handler stubs using some ugly macros in irq.h
 *
 * These macros create the low-level assembly IRQ routines that do all
 * the operations that are needed. They are also written to be fast - and to
 * disable interrupts as little as humanly possible.
 *
 * NOTE! These macros expand to three different handlers for each line: one
 * complete handler that does all the fancy stuff (including signal handling),
 * and one fast handler that is meant for simple IRQ's that want to be
 * atomic. The specific handler is chosen depending on the SA_INTERRUPT
 * flag when installing a handler. Finally, one "bad interrupt" handler, that
 * is used when no handler is present.
 *
 * The timer interrupt is handled specially to insure that the jiffies
 * variable is updated at all times.  Specifically, the timer interrupt is
 * just like the complete handlers except that it is invoked with interrupts
 * disabled and should never re-enable them.  If other interrupts were
 * allowed to be processed while the timer interrupt is active, then the
 * other interrupts would have to avoid using the jiffies variable for delay
 * and interval timing operations to avoid hanging the system.
 */

/* IRQ0 and 1 are special traps , and IRQ2 is the timer interrupt*/
void hwbreakpoint(void);
void IRQ1_interrupt(void);
/* the timer interrupt is special as well */
void timer_interrupt(void);
void timer_shortcut(void);
BUILD_IRQ(3, 0x08)
BUILD_IRQ(4, 0x10)
BUILD_IRQ(5, 0x20)
BUILD_IRQ(6, 0x40)
BUILD_IRQ(7, 0x80)
BUILD_IRQ(8, 0x100)
BUILD_IRQ(9, 0x200)
BUILD_IRQ(10, 0x400)
BUILD_IRQ(11, 0x800)
BUILD_IRQ(12, 0x1000)
BUILD_IRQ(13, 0x2000)
BUILD_IRQ(14, 0x4000)
/* IRQ 15 can't be hooked - it's the Etrax100 multiple IRQ interrupt */
void multiple_interrupt(void);
BUILD_IRQ(16, 0x10000)
BUILD_IRQ(17, 0x20000)
BUILD_IRQ(18, 0x40000)
BUILD_IRQ(19, 0x80000)
BUILD_IRQ(20, 0x100000)
BUILD_IRQ(21, 0x200000)
BUILD_IRQ(22, 0x400000)
BUILD_IRQ(23, 0x800000)
BUILD_IRQ(24, 0x1000000)
BUILD_IRQ(25, 0x2000000)
 
/*
 * Pointers to the low-level handlers: first the general ones, then the
 * fast ones, then the bad ones. 
 */

static void (*interrupt[NR_IRQS])(void) = {
	NULL, NULL, timer_interrupt, IRQ3_interrupt,
	IRQ4_interrupt, IRQ5_interrupt, IRQ6_interrupt, IRQ7_interrupt,
	IRQ8_interrupt, IRQ9_interrupt, IRQ10_interrupt, IRQ11_interrupt,
	IRQ12_interrupt, IRQ13_interrupt, IRQ14_interrupt, NULL,	
	IRQ16_interrupt, IRQ17_interrupt, IRQ18_interrupt, IRQ19_interrupt,	
	IRQ20_interrupt, IRQ21_interrupt, IRQ22_interrupt, IRQ23_interrupt,	
	IRQ24_interrupt, IRQ25_interrupt
};

static void (*sinterrupt[NR_IRQS])(void) = {
	NULL, NULL, timer_shortcut, sIRQ3_interrupt,
	sIRQ4_interrupt, sIRQ5_interrupt, sIRQ6_interrupt, sIRQ7_interrupt,
	sIRQ8_interrupt, sIRQ9_interrupt, sIRQ10_interrupt, sIRQ11_interrupt,
	sIRQ12_interrupt, sIRQ13_interrupt, sIRQ14_interrupt, NULL,	
	sIRQ16_interrupt, sIRQ17_interrupt, sIRQ18_interrupt, sIRQ19_interrupt,	
	sIRQ20_interrupt, sIRQ21_interrupt, sIRQ22_interrupt, sIRQ23_interrupt,	
	sIRQ24_interrupt, sIRQ25_interrupt
};

static void (*fast_interrupt[NR_IRQS])(void) = {
	NULL, NULL,
	NULL, fast_IRQ3_interrupt,
	fast_IRQ4_interrupt, fast_IRQ5_interrupt,
	fast_IRQ6_interrupt, fast_IRQ7_interrupt,
	fast_IRQ8_interrupt, fast_IRQ9_interrupt,
	fast_IRQ10_interrupt, fast_IRQ11_interrupt,
	fast_IRQ12_interrupt, fast_IRQ13_interrupt,
	fast_IRQ14_interrupt, NULL,
	fast_IRQ16_interrupt, fast_IRQ17_interrupt,
	fast_IRQ18_interrupt, fast_IRQ19_interrupt,
	fast_IRQ20_interrupt, fast_IRQ21_interrupt,
	fast_IRQ22_interrupt, fast_IRQ23_interrupt,
	fast_IRQ24_interrupt, fast_IRQ25_interrupt
};

static void (*sfast_interrupt[NR_IRQS])(void) = {
	NULL, NULL,
	NULL, sfast_IRQ3_interrupt,
	sfast_IRQ4_interrupt,  sfast_IRQ5_interrupt,
	sfast_IRQ6_interrupt,  sfast_IRQ7_interrupt,
	sfast_IRQ8_interrupt,  sfast_IRQ9_interrupt,
	sfast_IRQ10_interrupt, sfast_IRQ11_interrupt,
	sfast_IRQ12_interrupt, sfast_IRQ13_interrupt,
	sfast_IRQ14_interrupt, NULL,
	sfast_IRQ16_interrupt, sfast_IRQ17_interrupt,
	sfast_IRQ18_interrupt, sfast_IRQ19_interrupt,
	sfast_IRQ20_interrupt, sfast_IRQ21_interrupt,
	sfast_IRQ22_interrupt, sfast_IRQ23_interrupt,
	sfast_IRQ24_interrupt, sfast_IRQ25_interrupt
};

static void (*bad_interrupt[NR_IRQS])(void) = {
        NULL, NULL,
	NULL, bad_IRQ3_interrupt,
	bad_IRQ4_interrupt, bad_IRQ5_interrupt,
	bad_IRQ6_interrupt, bad_IRQ7_interrupt,
	bad_IRQ8_interrupt, bad_IRQ9_interrupt,
	bad_IRQ10_interrupt, bad_IRQ11_interrupt,
	bad_IRQ12_interrupt, bad_IRQ13_interrupt,
	bad_IRQ14_interrupt, NULL,
	bad_IRQ16_interrupt, bad_IRQ17_interrupt,
	bad_IRQ18_interrupt, bad_IRQ19_interrupt,
	bad_IRQ20_interrupt, bad_IRQ21_interrupt,
	bad_IRQ22_interrupt, bad_IRQ23_interrupt,
	bad_IRQ24_interrupt, bad_IRQ25_interrupt
};

/*
 * Initial irq handlers.
 */

static void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }

static struct irqaction *irq_action[NR_IRQS] = {
	NULL, NULL, NULL, NULL,
	NULL, NULL, NULL, NULL,
	NULL, NULL, NULL, NULL,
	NULL, NULL, NULL, NULL,
	NULL, NULL, NULL, NULL,
	NULL, NULL, NULL, NULL,
	NULL, NULL
};

int get_irq_list(char *buf)
{
	int i, len = 0;
	struct irqaction * action;

	for (i = 0; i < NR_IRQS; i++) {
		action = irq_action[i];
		if (!action) 
			continue;
		len += sprintf(buf+len, "%2d: %10u %c %s",
			i, kstat.interrupts[i],
			(action->flags & SA_INTERRUPT) ? '+' : ' ',
			action->name);
		for (action=action->next; action; action = action->next) {
			len += sprintf(buf+len, ",%s %s",
				(action->flags & SA_INTERRUPT) ? " +" : "",
				action->name);
		}
		len += sprintf(buf+len, "\n");
	}
	return len;
}


/*
 * do_IRQ handles IRQ's that have been installed without the
 * SA_INTERRUPT flag: it uses the full signal-handling return
 * and runs with other interrupts enabled. All relatively slow
 * IRQ's should use this format: notably the keyboard/timer
 * routines.
 */
asmlinkage void do_IRQ(int irq, struct pt_regs * regs)
{
	struct irqaction * action = *(irq + irq_action);
	int do_random = 0;

	kstat.interrupts[irq]++;

	while (action) {
		do_random |= action->flags;
		action->handler(irq, action->dev_id, regs);
		action = action->next;
	}
	if (do_random & SA_SAMPLE_RANDOM)
		add_interrupt_randomness(irq);
}

/*
 * do_fast_IRQ handles IRQ's that don't need the fancy interrupt return
 * stuff - the handler is also running with interrupts disabled unless
 * it explicitly enables them later.
 */

/* note that we don't really need two different functions here */

asmlinkage void do_fast_IRQ(int irq)
{
	struct irqaction * action = *(irq + irq_action);
	int do_random = 0;
	
	kstat.interrupts[irq]++;

	while (action) {
		do_random |= action->flags;
		action->handler(irq, action->dev_id, NULL);
		action = action->next;
	}
	if (do_random & SA_SAMPLE_RANDOM)
		add_interrupt_randomness(irq);
}

/* this function links in a handler into the chain of handlers for the
   given irq, and if the irq has never been registred, the appropriate
   handler is entered into the interrupt vector
*/

int setup_etrax_irq(int irq, struct irqaction * new)
{
	int shared = 0;
	struct irqaction *old, **p;
	unsigned long flags;

	p = irq_action + irq;
	if ((old = *p) != NULL) {
		/* Can't share interrupts unless both agree to */
		if (!(old->flags & new->flags & SA_SHIRQ))
			return -EBUSY;

		/* Can't share interrupts unless both are same type */
		if ((old->flags ^ new->flags) & SA_INTERRUPT)
			return -EBUSY;

		/* add new interrupt at end of irq queue */
		do {
			p = &old->next;
			old = *p;
		} while (old);
		shared = 1;
	}

	if (new->flags & SA_SAMPLE_RANDOM)
		rand_initialize_irq(irq);

	save_flags(flags);
	cli();
	*p = new;

	if (!shared) {
		/* if the irq wasn't registred before, enter it into the vector table
		   and unmask it physically 
		*/
		if (new->flags & SA_INTERRUPT)
			set_int_vector(irq, fast_interrupt[irq], sfast_interrupt[irq]);
		else
			set_int_vector(irq, interrupt[irq], sinterrupt[irq]);
		unmask_irq(irq);
	}
	
	restore_flags(flags);
	return 0;
}

/* this function is called by a driver to register an irq handler
   Valid flags:
   SA_INTERRUPT -> it's a fast interrupt, handler called with irq disabled and
                   no signal checking etc is performed upon exit
   SA_SHIRQ -> the interrupt can be shared between different handlers, the handler
                is required to check if the irq was "aimed" at it explicitely
   SA_RANDOM -> the interrupt will add to the random generators entropy
*/

int request_irq(unsigned int irq, 
		void (*handler)(int, void *, struct pt_regs *),
		unsigned long irqflags, 
		const char * devname,
		void *dev_id)
{
	int retval;
	struct irqaction * action;

	/* interrupts 0 and 1 are hardware breakpoint and NMI and we can't support
	   these yet. interrupt 15 is the multiple irq, it's special. */

	if(irq < 2 || irq == 15 || irq >= NR_IRQS)
		return -EINVAL;

	if(!handler)
		return -EINVAL;

	/* allocate and fill in a handler structure and setup the irq */

	action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
	if (!action)
		return -ENOMEM;

	action->handler = handler;
	action->flags = irqflags;
	action->mask = 0;
	action->name = devname;
	action->next = NULL;
	action->dev_id = dev_id;

	retval = setup_etrax_irq(irq, action);

	if (retval)
		kfree(action);
	return retval;
}
		
void free_irq(unsigned int irq, void *dev_id)
{
	struct irqaction * action, **p;
	unsigned long flags;

	if (irq >= NR_IRQS) {
		printk("Trying to free IRQ%d\n",irq);
		return;
	}
	for (p = irq + irq_action; (action = *p) != NULL; p = &action->next) {
		if (action->dev_id != dev_id)
			continue;

		/* Found it - now free it */
		save_flags(flags);
		cli();
		*p = action->next;
		if (!irq[irq_action]) {
			mask_irq(irq);
			set_int_vector(irq, bad_interrupt[irq], 0);
		}
		restore_flags(flags);
		kfree(action);
		return;
	}
	printk("Trying to free free IRQ%d\n",irq);
}

void weird_irq()
{
	__asm__("di");
	printk("weird irq\n");
	while(1);
}

/* init_IRQ() is called by start_kernel and is responsible for fixing IRQ masks and
   setting the irq vector table to point to bad_interrupt ptrs.
*/

void init_IRQ(void)
{
	int i;

	/* clear all interrupt masks */

#ifndef CONFIG_SVINTO_SIM
	*R_IRQ_MASK0_CLR = 0xffffffff;
	*R_IRQ_MASK1_CLR = 0xffffffff;
	*R_IRQ_MASK2_CLR = 0xffffffff;
#endif

	*R_VECT_MASK_CLR = 0xffffffff;

	/* clear the shortcut entry points */

	for(i = 0; i < NR_IRQS; i++)
		irq_shortcuts[i] = NULL;
	
#ifdef CONFIG_SVINTO1
	/* due to the Svinto-1 hardware bug, put the multiple IRQ handler
	   at all vectors */

	for (i = 0; i < 256; i++)
		etrax_irv->v[i] = multiple_interrupt;
#else
	for (i = 0; i < 256; i++)
		etrax_irv->v[i] = weird_irq;
      
	/* set all etrax irq's to the bad handlers */
	for (i = 2; i < NR_IRQS; i++)
		set_int_vector(i, bad_interrupt[i], 0);

	/* except IRQ 15 which is the multiple-IRQ handler on Etrax100 */

	set_int_vector(15, multiple_interrupt, 0);
	
	/* and 0 and 1 which are special breakpoint/NMI traps */

	set_int_vector(0, hwbreakpoint, 0);
	set_int_vector(1, IRQ1_interrupt, 0);
#endif

#ifdef CONFIG_KGDB
	/* setup kgdb if its enabled, and break into the debugger */

	kgdb_init();

	breakpoint();
#endif

} 
