/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <linux/kernel.h>
#include <linux/irq.h>
#include <linux/hardirq.h>
#include <linux/interrupt.h>
#include <asm/mipsregs.h>
#include <asm/irq_cpu.h>

#include <irq.h>
#include <gpio.h>
#include <pnx833x.h>

#include <prjconfig.h>

#if defined (CONFIG_TIME_DOCTOR)
#include <linux/timedoctor.h>
#define TIMEDOCTOR_INTERRUPT_ENTER(irq) do { timeDoctor_interrupt_enter ((irq)); } while (0)
#define TIMEDOCTOR_INTERRUPT_EXIT(irq)	do { timeDoctor_interrupt_exit ((irq)); } while (0)
#else
#define TIMEDOCTOR_INTERRUPT_ENTER(irq) do { } while (0)
#define TIMEDOCTOR_INTERRUPT_EXIT(irq)	do { } while (0)
#endif

/*  Interrupt priority table.
    0 = no interrupt generated, 15 = highest priority
    Interrupts of priority 9 and above are not affected by calls to
    tmosalDisableInterrupts(). See phStbOsal/src/linux/misc.c for details
*/

#define HIVID_PRI   11  /* critical video interrupts                         */
#define AUDIO_PRI   10  /* critical audio interrupts                         */
#define LOVID_PRI   9   /* less critical video interrupts                    */
#define OTHER_PRI   8   /* other high priority interrupts (maskable by osal) */

#if defined (CONFIG_SMART_CARD_HIGH_PRIORITY_IRQ)
#define SMC_PRI     12  /* 12 seems excessively high, but is value used in Mediabox tree... */
#else
#define SMC_PRI     4   /* 4 is historical (but perhaps untested ?) default */
#endif

static const unsigned char irq_prio[PNX833X_PIC_NUM_IRQ] =
{
    0,          //unused
    4,          //PNX833X_PIC_I2C0_INT                 1
    4,          //PNX833X_PIC_I2C1_INT                 2
    1,          //PNX833X_PIC_UART0_INT                3
    1,          //PNX833X_PIC_UART1_INT                4
    6,          //PNX833X_PIC_TS_IN0_DV_INT            5
    6,          //PNX833X_PIC_TS_IN0_DMA_INT           6
    7,          //PNX833X_PIC_GPIO_INT                 7
    4,          //PNX833X_PIC_AUDIO_DEC_INT            8 (MIPS 1-2 Mailbox)
    5,          //PNX833X_PIC_VIDEO_DEC_INT            9
    4,          //PNX833X_PIC_CONFIG_INT              10
    AUDIO_PRI,  //PNX833X_PIC_AOI_INT                 11 (Audio IO. Very hard deadline. ~70Hz)
    LOVID_PRI,  //PNX833X_PIC_SYNC_INT                12 (SD Video sync. 50-60Hz)
    OTHER_PRI,  //PNX8335_PIC_SATA_INT                13
    4,          //PNX833X_PIC_OSD_INT                 14
    LOVID_PRI,  //PNX833X_PIC_DISP1_INT               15
    LOVID_PRI,  //PNX833X_PIC_DEINTERLACER_INT        16
    LOVID_PRI,  //PNX833X_PIC_DISPLAY2_INT            17
    4,          //PNX833X_PIC_VC_INT                  18
    SMC_PRI,    //PNX833X_PIC_SC_INT                  19
    OTHER_PRI,  //PNX833X_PIC_IDE_INT                 20
    OTHER_PRI,  //PNX833X_PIC_IDE_DMA_INT             21
    6,          //PNX833X_PIC_TS_IN1_DV_INT           22
    6,          //PNX833X_PIC_TS_IN1_DMA_INT          23
    4,          //PNX833X_PIC_SGDX_DMA_INT            24
    4,          //PNX833X_PIC_TS_OUT_INT              25
    4,          //PNX833X_PIC_IR_INT                  26
    3,          //PNX833X_PIC_VMSP1_INT               27
    3,          //PNX833X_PIC_VMSP2_INT               28
    4,          //PNX833X_PIC_PIBC_INT                29
    4,          //PNX833X_PIC_TS_IN0_TRD_INT          30
    4,          //PNX833X_PIC_SGDX_TPD_INT            31
    5,          //PNX833X_PIC_USB_INT                 32
    4,          //PNX833X_PIC_TS_IN1_TRD_INT          33
    4,          //PNX833X_PIC_CLOCK_INT               34
    4,          //PNX833X_PIC_SGDX_PARSER_INT         35
    4,          //PNX833X_PIC_VMSP_DMA_INT            36
#if defined(CONFIG_SOC_PNX8335)
    4,          //PNX8335_PIC_MIU_INT                 37
    4,          //PNX8335_PIC_AVCHIP_IRQ_INT          38
    HIVID_PRI,  //PNX8335_PIC_SYNC_HD_INT             39 (HD Video sync. Very hard deadline. 50-60Hz)
    LOVID_PRI,  //PNX8335_PIC_DISP_HD_INT             40
    LOVID_PRI,  //PNX8335_PIC_DISP_SCALER_INT         41
    4,          //PNX8335_PIC_OSD_HD1_INT             42
    4,          //PNX8335_PIC_DTL_WRITER_Y_INT        43
    4,          //PNX8335_PIC_DTL_WRITER_C_INT        44
    4,          //PNX8335_PIC_DTL_EMULATOR_Y_IR_INT   45
    4,          //PNX8335_PIC_DTL_EMULATOR_C_IR_INT   46
    4,          //PNX8335_PIC_DENC_TTX_INT            47
    4,          //PNX8335_PIC_MMI_SIF0_INT            48
    4,          //PNX8335_PIC_MMI_SIF1_INT            49
    4,          //PNX8335_PIC_MMI_CDMMU_INT           50
    4,          //PNX8335_PIC_PIBCS_INT               51
    2,          //PNX8335_PIC_ETHERNET_INT            52
    3,          //PNX8335_PIC_VMSP1_0_INT             53
    3,          //PNX8335_PIC_VMSP1_1_INT             54
    4,          //PNX8335_PIC_VMSP1_DMA_INT           55
    4,          //PNX8335_PIC_TDGR_DE_INT             56
    4,          //PNX8335_PIC_IR1_IRQ_INT             57
#endif
};

#define IRQFLAG_STARTED 	(1 << 0)
#define IRQFLAG_DISABLED	(1 << 1)

static unsigned char pic_irqflags[PNX833X_PIC_NUM_IRQ];

static DEFINE_SPINLOCK(irq_lock);


static inline void pnx833x_hard_enable_pic_irq (unsigned int pic_irq)
{
	/*
	   Enable PIC interrupt by setting it's priority in the PIC to the
	   priority assigned to it in our static priority look up table...
	*/
	PNX833X_PIC_INT_REG(pic_irq) = irq_prio[pic_irq];
}

static inline void pnx833x_hard_disable_pic_irq (unsigned int pic_irq)
{
	/*
	   Disable PIC interrupt by setting it's priority in the PIC to 0
	*/
	PNX833X_PIC_INT_REG(pic_irq) = 0;
}

static unsigned int pnx833x_startup_pic_irq (unsigned int irq)
{
	unsigned long flags;
	unsigned int pic_irq = irq - PNX833X_PIC_IRQ_BASE;

	spin_lock_irqsave (&irq_lock, flags);

	pic_irqflags[pic_irq] = IRQFLAG_STARTED;	/* started, not disabled */
	pnx833x_hard_enable_pic_irq (pic_irq);

	spin_unlock_irqrestore (&irq_lock, flags);
	return 0;
}

static void pnx833x_shutdown_pic_irq (unsigned int irq)
{
	unsigned long flags;
	unsigned int pic_irq = irq - PNX833X_PIC_IRQ_BASE;

	spin_lock_irqsave (&irq_lock, flags);

	pic_irqflags[pic_irq] = 0;			/* not started */
	pnx833x_hard_disable_pic_irq (pic_irq);

	spin_unlock_irqrestore (&irq_lock, flags);
}

static void pnx833x_enable_pic_irq (unsigned int irq)
{
	unsigned long flags;
	unsigned int pic_irq = irq - PNX833X_PIC_IRQ_BASE;

	spin_lock_irqsave (&irq_lock, flags);

	pic_irqflags[pic_irq] &= ~IRQFLAG_DISABLED;
	if (pic_irqflags[pic_irq] == IRQFLAG_STARTED)
	{
		pnx833x_hard_enable_pic_irq (pic_irq);
	}

	spin_unlock_irqrestore (&irq_lock, flags);
}

static void pnx833x_disable_pic_irq (unsigned int irq)
{
	unsigned long flags;
	unsigned int pic_irq = irq - PNX833X_PIC_IRQ_BASE;

	spin_lock_irqsave (&irq_lock, flags);

	pic_irqflags[pic_irq] |= IRQFLAG_DISABLED;
	pnx833x_hard_disable_pic_irq (pic_irq);

	spin_unlock_irqrestore (&irq_lock, flags);
}

static struct irq_chip pnx833x_pic_irq_type = {
	.typename = "PNX-PIC",
	.startup = pnx833x_startup_pic_irq,
	.shutdown = pnx833x_shutdown_pic_irq,
	.enable = pnx833x_enable_pic_irq,
	.disable = pnx833x_disable_pic_irq,
};

#if defined (CONFIG_PNX833X_GPIO_INTERRUPTS)

static unsigned int pnx833x_startup_gpio_irq (unsigned int irq)
{
	int pin = irq - PNX833X_GPIO_IRQ_BASE;
	gpio_enable_irq (pin);
	return 0;
}

static void pnx833x_enable_gpio_irq (unsigned int irq)
{
	int pin = irq - PNX833X_GPIO_IRQ_BASE;
	gpio_enable_irq (pin);
}

static void pnx833x_disable_gpio_irq (unsigned int irq)
{
	int pin = irq - PNX833X_GPIO_IRQ_BASE;
	gpio_disable_irq (pin);
}

static int pnx833x_set_type_gpio_irq (unsigned int irq, unsigned int flow_type)
{
	int pin = irq - PNX833X_GPIO_IRQ_BASE;
	int gpio_mode;

	switch (flow_type)
	{
		case IRQ_TYPE_EDGE_RISING:
			gpio_mode = GPIO_INT_EDGE_RISING;
			break;
		case IRQ_TYPE_EDGE_FALLING:
			gpio_mode = GPIO_INT_EDGE_FALLING;
			break;
		case IRQ_TYPE_EDGE_BOTH:
			gpio_mode = GPIO_INT_EDGE_BOTH;
			break;
		case IRQ_TYPE_LEVEL_HIGH:
			gpio_mode = GPIO_INT_LEVEL_HIGH;
			break;
		case IRQ_TYPE_LEVEL_LOW:
			gpio_mode = GPIO_INT_LEVEL_LOW;
			break;
		default:
			gpio_mode = GPIO_INT_NONE;
			break;
	}

	gpio_setup_irq (gpio_mode, pin);

	return 0;
}

static struct irq_chip pnx833x_gpio_irq_type = {
	.typename = "PNX-GPIO",
	.startup = pnx833x_startup_gpio_irq,
	.shutdown = pnx833x_disable_gpio_irq,
	.enable = pnx833x_enable_gpio_irq,
	.disable = pnx833x_disable_gpio_irq,
	.set_type = pnx833x_set_type_gpio_irq
};

#endif

static void pic_dispatch (void)
{
	unsigned int pic_irq = PNX833X_REGFIELD(PIC_INT_SRC, INT_SRC);
	unsigned int priority_previous;
	unsigned int irq;

	if ((pic_irq >= 1) && (pic_irq < (PNX833X_PIC_NUM_IRQ)))
	{
		/*
		   Mask all interrupts of lower priority than the one currently being serviced...
		*/
		priority_previous = PNX833X_PIC_INT_PRIORITY;
		PNX833X_PIC_INT_PRIORITY = irq_prio[pic_irq];

		if (unlikely (pic_irq == (PNX833X_PIC_GPIO_INT - PNX833X_PIC_IRQ_BASE)))
		{
#if defined (CONFIG_PNX833X_GPIO_INTERRUPTS)
			int pin;
			unsigned int active;

			while (1)
			{
				active = gpio_irq_status_active_and_unmasked();
				if (active == 0)
					break;

				pin = ffs (active) - 1;
				irq = PNX833X_GPIO_IRQ_BASE + pin;

				TIMEDOCTOR_INTERRUPT_ENTER(irq);

				do_IRQ (irq);

				/*
				   Clearing should only be required for edge
				   triggered interrupts (which are latched within
				   the GPIO interrupt controller). Hopefully does
				   no harm for level triggered ?
				*/
				gpio_clear_irq (pin);

				TIMEDOCTOR_INTERRUPT_EXIT(irq);
			}
#else
			/*
			   In this case, the GPIO IRQ should never get enabled in the PIC, so
			   it should never be active... but if somehow it is, just disable it
			   and continue...
			*/
			pnx833x_hard_disable_pic_irq (pic_irq);
#endif
		}
		else
		{
			irq = PNX833X_PIC_IRQ_BASE + pic_irq;

			TIMEDOCTOR_INTERRUPT_ENTER(irq);
			do_IRQ (irq);
			TIMEDOCTOR_INTERRUPT_EXIT(irq);
		}

		PNX833X_PIC_INT_PRIORITY = priority_previous;
	}
	else
	{
		/* The PIC specification states that it is possible for the  */
		/* PIC interrupt to be asserted, but the interrupt source is */
		/* de-asseted 'for various reasons' and that the ISR should  */
		/* 'cope with the situation' ... consider it coped with!     */
	}
}

asmlinkage void plat_irq_dispatch (void)
{
	unsigned int pending = read_c0_status() & read_c0_cause();

	if (pending & STATUSF_IP4)
	{
		pic_dispatch();
	}
	else if (pending & STATUSF_IP7)
	{
		do_IRQ (PNX833X_TIMER_IRQ);
	}
	else
	{
		spurious_interrupt();
	}
}

void __init arch_init_irq (void)
{
	unsigned int irq;

	/* setup standard internal cpu irqs */
	mips_cpu_irq_init();

	for (irq = PNX833X_PIC_IRQ_BASE; irq < (PNX833X_PIC_IRQ_BASE + PNX833X_PIC_NUM_IRQ); irq++)
	{
		unsigned int pic_irq = irq - PNX833X_PIC_IRQ_BASE;

		if (pic_irq == 0)
			continue;
		pnx833x_hard_disable_pic_irq (pic_irq);
		set_irq_chip_and_handler (irq, &pnx833x_pic_irq_type, handle_simple_irq);
	}

#if defined (CONFIG_PNX833X_GPIO_INTERRUPTS)

	for (irq = PNX833X_GPIO_IRQ_BASE; irq < (PNX833X_GPIO_IRQ_BASE + PNX833X_GPIO_NUM_IRQ); irq++)
	{
		set_irq_chip_and_handler (irq, &pnx833x_gpio_irq_type, handle_simple_irq);
	}

	pnx833x_startup_pic_irq (PNX833X_PIC_GPIO_INT);

#endif

	/* Set PIC priority limiter register to 0 */
	PNX833X_PIC_INT_PRIORITY = 0;

	/* Enable PIC IRQs (HWIRQ2) */
	if (cpu_has_vint)
	{
		set_vi_handler(4, pic_dispatch);
	}

	write_c0_status (read_c0_status() | IE_IRQ2);
}

