/*
 *              Copyright (C) 2005, BroadOn Communications Corp.
 *
 * These coded instructions, statements, and computer programs contain
 * unpublished  proprietary information of BroadOn Communications Corp.,
 * and  are protected by Federal copyright law. They may not be disclosed
 * to  third  parties or copied or duplicated in any form, in whole or in
 * part, without the prior written consent of BroadOn Communications Corp.
 *
 * Description:
 *      Interrupt tests for ARM design verification
 *
 */


#include <sc/arm.h>
#include <types.h>
#include <sc/sc.h>
#include <flash.h>
#include <sc/dv_boot.h>
#include <ioslibc.h>
#include "dv_cpu_local.h"


/* Service an AES or flash interrupt */
static volatile u32     __aesIrqReceived = 0, __flIrqReceived = 0, __error = 0;
extern u32              irqHandler;

#define IRQ_STACK_SIZE 64
u32 cpu0IRQStack[IRQ_STACK_SIZE>>2] CACHE_ALIGNED = {0xff};
u32 cpu1IRQStack[IRQ_STACK_SIZE>>2] CACHE_ALIGNED = {0xff};

/*
 *  Function: aesIrqCheck
 *
 *  Description:
 *      Issue an operation to the AES unit and wait for
 *      the interrupt to come back
 *
 *  Inputs:
 *      None
 *
 *  Outputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error
 *
 */
int
aesIrqCheck(void)
{
    int                 rc = 0;
    volatile u32        *writeAddr = (u32 *) (DA_SIZE);
    u32                 *readAddr = (u32 *) (DA_SIZE + 16);
    u32                 *ivAddr = (u32 *) (DA_SIZE + 32);
    u32                 *kAddr = (u32 *) (DA_SIZE + 48);
    u32                 daxAddr = NULL, count = 0, iterations = 15;

    /* Power on AES and enable AES interrupt */
    IO_WRITE(PWR_CTRL_REG, PWR_CTRL_CLOCK_ON << PWR_CTRL_AES_SHIFT);
    IO_WRITE(PWR_CTRL_REG, PWR_CTRL_DEASSERT_RST << PWR_CTRL_AES_SHIFT);
    IO_WRITE(SYS_SMP_REG, SYS_SMP_SET_IMASK |
                        (SYS_AES_INTR << SYS_SMP_IMASK_SHIFT));

    /* Give RW access to the first 4 cachelines */
    while (count < 4) {
        daxAddr = DAX_START + DA_SIZE + (count * 16);
        IO_WRITE(daxAddr, PT_ACCESS_RW << PT_SC_PROT_SHIFT);

        count++;
    }

    while (iterations-- > 0) {
        /* Assign some random values */
        count = 0;
        while (count < 4) {
            readAddr[count] = 0x12345678;
            writeAddr[count] = 0xfedcba98;
            ivAddr[count] = (0x13e957 * count) + 0x125f;
            kAddr[count] = (0xe8aa25 * count) + 0x7c92;

            count++;
        }

        /* Program the pointers */
        IO_WRITE(AES_IVPTR_REG, CPU_TO_BUS_ADDR((u32) ivAddr));
        IO_WRITE(AES_RPTR_REG, CPU_TO_BUS_ADDR((u32) readAddr));
        IO_WRITE(AES_WPTR_REG, CPU_TO_BUS_ADDR((u32) writeAddr));

        /* Expand key */
        IO_WRITE(AES_KEY_REG, kAddr[0]);
        IO_WRITE(AES_KEY_REG, kAddr[1]);
        IO_WRITE(AES_KEY_REG, kAddr[2]);
        IO_WRITE(AES_KEY_REG, kAddr[3]);
        IO_WRITE(AES_CTRL_REG, AES_CTRL_EXEC | AES_CTRL_MASK |
                            (AES_KEYEXPAND_CMD << AES_CTRL_CMD_SHIFT));
        while (__aesIrqReceived == 0);
        __aesIrqReceived = 0;
        if (__error) {
            rc = -1;
            goto aesIrqCheck_out;
        }

        /* Encrypt 16 bytes */
        IO_WRITE(AES_CTRL_REG, AES_CTRL_EXEC | AES_CTRL_MASK |
                            (AES_ENCRYPT_CMD << AES_CTRL_CMD_SHIFT));
        while (__aesIrqReceived == 0);
        __aesIrqReceived = 0;
        if (__error) {
            rc = -2;
            goto aesIrqCheck_out;
        }

        /* Check for violation */
        if (IO_READ(AES_CTRL_REG) & AES_CTRL_VIOL_MASK) {
            rc = -3;
            goto aesIrqCheck_out;
        }

        /* Make sure the write address has been overwritten */
        if ((writeAddr[0] == 0xfedcba98) || (writeAddr[1] == 0xfedcba98) ||
                (writeAddr[2] == 0xfedcba98) || (writeAddr[3] == 0xfedcba98)) {
            rc = -4;
            goto aesIrqCheck_out;
        }
    }

aesIrqCheck_out:
    /* Disable all IRQ interrupts */
    IO_WRITE(SYS_SMP_REG, SYS_SMP_SET_IMASK);

    /* Power off AES */
    IO_WRITE(PWR_CTRL_REG, PWR_CTRL_ASSERT_RST << PWR_CTRL_AES_SHIFT);
    IO_WRITE(PWR_CTRL_REG, PWR_CTRL_CLOCK_OFF << PWR_CTRL_AES_SHIFT);

    return (rc);
}


/*
 *  Function: flIrqCheck
 *
 *  Description:
 *      Issue an operation to the flash unit and wait for
 *      the interrupt to come back
 *
 *  Inputs:
 *      None
 *
 *  Outputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error
 *
 */
int
flIrqCheck(void)
{
    int             rc = 0;
    volatile u8     *buf = (u8 *) (DA_SIZE + 128);
    u32             daxAddr = NULL, iterations = 50;

    /* Power on flash and enable flash interrupt */
    IO_WRITE(PWR_CTRL_REG, PWR_CTRL_CLOCK_ON << PWR_CTRL_FLASH_SHIFT);
    IO_WRITE(PWR_CTRL_REG, PWR_CTRL_DEASSERT_RST << PWR_CTRL_FLASH_SHIFT);
    IO_WRITE(SYS_SMP_REG, SYS_SMP_SET_IMASK |
                        (SYS_FLASH_INTR << SYS_SMP_IMASK_SHIFT));

    /* Give RW access to the the cacheline */
    daxAddr = DAX_START + DA_SIZE + 128;
    IO_WRITE(daxAddr, PT_ACCESS_RW << PT_SC_PROT_SHIFT);

    /* Enable flash */
    IO_WRITE(FL_CONF_REG, IO_READ(FL_CONF_REG) | FL_CONF_DEV);
    IO_WRITE(FL_ID_REG, 0x0);

    while (iterations-- > 0) {
        buf[0] = 0x0;

        /* Program the pointers */
        IO_WRITE(FL_ID_REG, 0x0);
        IO_WRITE(FL_AD0_REG, 0x0);
        IO_WRITE(FL_MA0_REG, CPU_TO_BUS_ADDR((u32) buf));
        IO_WRITE(FL_CTRL_REG, FL_CTRL_EXEC | FL_CTRL_MASK |
                        (0x1 << FL_CTRL_ADP_SHIFT) | FL_CTRL_RDP | 
                        (0x90 << FL_CTRL_CMD_SHIFT) |
                        (16 << FL_CTRL_SIZE_SHIFT));
        while (__flIrqReceived == 0);
        __flIrqReceived = 0;
        if (__error) {
            rc = -11;
            goto flIrqCheck_out;
        }

        /* Check for violation */
        if (IO_READ(FL_CTRL_REG) & FL_CTRL_MVIOL) {
            rc = -12;
            goto flIrqCheck_out;
        }

        if ((buf[0] != FL_SAMSUNG_ID) 
         && (buf[0] != FL_TOSHIBA_ID)
         && (buf[0] != FL_HYNIX_ID)) {
            rc = -13;
            printf("Unrecognized Flash part ID = 0x%x\n", buf[0]);
            goto flIrqCheck_out;
        }
    }

flIrqCheck_out:
    /* Disable all IRQ interrupts */
    IO_WRITE(SYS_SMP_REG, SYS_SMP_SET_IMASK);

    /* Power off flash */
    IO_WRITE(PWR_CTRL_REG, PWR_CTRL_ASSERT_RST << PWR_CTRL_FLASH_SHIFT);
    IO_WRITE(PWR_CTRL_REG, PWR_CTRL_CLOCK_OFF << PWR_CTRL_FLASH_SHIFT);

    return (rc);
}

volatile int cpu1done = 0;

/*
 *  Function: cpu0main
 *
 *  Description:
 *      Entry function for CPU0
 *
 *  Inputs:
 *      None
 *
 *  Outputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error
 *
 */
int 
cpu0main(void)
{
    int     rc = 0;

    __skDisableInt();
    /* Clear WERR if set and make sure IRQs are masked */
    IO_WRITE(SYS_SMP_REG, SYS_SMP_WERR|SYS_SMP_SET_IMASK);

    DVSetIRQStack((u32) cpu0IRQStack + sizeof(cpu0IRQStack));
    (void) DVSetExcHandler(ARM_EXC_IRQ, (u32) &irqHandler);

    /* unmask interrupts */
    __skEnableInt();

    printf("cpu0: test AES interrupt ...\n");
    /* Test #1: Verify AES interrupt */
    rc = aesIrqCheck();
    if (rc < 0) {
        goto cpu0main_out;
    }

    printf("cpu0: test Flash interrupt ...\n");
    /* Test #2: Verify flash interrupt */
    rc = flIrqCheck();
    if (rc < 0) {
        goto cpu0main_out;
    }

    printf("cpu0: starting cpu1 ...\n");
    DVStartCpu1();

    printf("cpu0: wait for cpu1 to finish ...\n");
    while (cpu1done == 0);      /* CPU 1 will exit after it's done */
    printf("cpu0: cpu1 returns %d\n", cpu1done);

cpu0main_out:
    printf("cpu0: exit with rc %d\n", rc);
    return (rc);
}


/*
 *  Function: cpu1main
 *
 *  Description:
 *      Entry function for CPU1
 *
 *  Inputs:
 *      None
 *
 *  Outputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error
 *
 */
int 
cpu1main(void)
{
    int     rc = 0;

    __skDisableInt();
    /* Clear WERR if set and make sure IRQs are masked */
    IO_WRITE(SYS_SMP_REG, SYS_SMP_WERR|SYS_SMP_SET_IMASK);

    DVSetIRQStack((u32) cpu1IRQStack + sizeof(cpu1IRQStack));

    /* unmask interrupts */
    __skEnableInt();

    /* Test #2: Verify flash interrupt */
    rc = flIrqCheck();
    if (rc < 0) {
        goto cpu1main_out;
    }

cpu1main_out:
    cpu1done = (rc == 0) ? 1 : rc;
    return (rc);
}


/*
 *  Function: irq
 *
 *  Description:
 *      Interrupt handler
 *
 *  Inputs:
 *      None
 *
 *  Outputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error
 *
 */
int
irq(void)
{
    u32     cpuId = 0x0, ipend = 0x0;

    cpuId = (IO_READ(SYS_SMP_REG) & SYS_SMP_CPU_ID) >> 31;
    ipend = SYS_SMP_IPEND(IO_READ(SYS_SMP_REG));

#if 0
    if ((cpuId == 0) && (ipend & SYS_AES_INTR)) {
        /* Clear the interrupt */
        IO_WRITE(AES_CTRL_REG, 0);
        __aesIrqReceived = 1;
    } else if ((cpuId == 1) && (ipend & SYS_FLASH_INTR)) {
        /* Clear the interrupt */
        IO_WRITE(FL_CTRL_REG, 0);
        __flIrqReceived = 1;
    } else {
        __aesIrqReceived = 1;
        __flIrqReceived = 1;
        __error = 1;
    }
#else
    if ((ipend & SYS_AES_INTR)) {
        /* Clear the interrupt */
        IO_WRITE(AES_CTRL_REG, 0);
        __aesIrqReceived = 1;
    } else if ((ipend & SYS_FLASH_INTR)) {
        /* Clear the interrupt */
        IO_WRITE(FL_CTRL_REG, 0);
        __flIrqReceived = 1;
    } else {
        __aesIrqReceived = 1;
        __flIrqReceived = 1;
        __error = 1;
    }
#endif

    return (0);
}
