/*
 *              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:
 *      Cache tests for ARM design verification
 *
 */


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


#define SCInvalidate(addr)  IO_WRITE(CPS_START |                            \
                                (CPS_CACHE_SC << CPS_CACHE_SHIFT) |         \
                                (CPS_OP_WBINV << CPS_OP_SHIFT) | (addr), 1)


/*
 *  Function: hitL1Check
 *
 *  Description:
 *      Do byte/short/word reads and writes that hit in
 *      the L1 cache
 *
 *  Inputs:
 *      cpuId - ID of the CPU, 0 or 1
 *
 *  Outputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error
 *
 */
int
hitL1Check(int cpuId)
{
    int             rc = 0, cpuOffset = (cpuId * 16), iterations = 25;
    volatile u8     *byte_p = (u8 *) (DA_SIZE + cpuOffset);
    volatile u16    *halfword_p = (u16 *) (DA_SIZE + cpuOffset);
    volatile u32    *word_p = (u32 *) (DA_SIZE + cpuOffset);
    u32             daxAddr = 0x0, word = 0x0;
    u16             halfword = 0x0;
    u8              byte = 0x0;

    /* Set RW permission */
    daxAddr = (DAX_START + DA_SIZE + cpuOffset);
    IO_WRITE(daxAddr, PT_ACCESS_RW << PT_SC_PROT_SHIFT);

    while (iterations-- > 0) {
        /* Put the cacheline into the L1 cache */
        if (cpuId == 0) {
            *word_p = 0x12345678;
        } else {
            *word_p = 0xffeeddcc;
        }
        word = *word_p;

        /* The following reads should all hit in the L1 cache */
        word = *word_p;
        halfword = *halfword_p;
        byte = *byte_p;
        if (cpuId == 0) {
            if ((word != 0x12345678) || (halfword != 0x5678) ||
                    (byte != 0x78)) {
                rc = -1;
                goto hitL1Check_out;
            }
        } else {
            if ((word != 0xffeeddcc) || (halfword != 0xddcc) ||
                    (byte != 0xcc)) {
                rc = -2;
                goto hitL1Check_out;
            }
        }

        /* The following writes should also hit in the L1 cache */
        *word_p = 0xfedcba98;
        *halfword_p = 0x1122;
        *byte_p = 0x34;

        /* Check the result of write */
        word = *word_p;
        halfword = *halfword_p;
        byte = *byte_p;
        if ((word != 0xfedc1134) || (halfword != 0x1134) || (byte != 0x34)) {
            rc = -3;
            goto hitL1Check_out;
        }
    }

hitL1Check_out:

    return (rc);
}


/*
 *  Function: hitL2Check
 *
 *  Description:
 *      Do byte/short/word reads and writes that hit in
 *      the L2 cache
 *
 *  Inputs:
 *      cpuId - ID of the CPU, 0 or 1
 *
 *  Outputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error
 *
 */
int
hitL2Check(int cpuId)
{
    int             rc = 0, cpuOffset = (cpuId * 16), iterations = 25;
    volatile u8     *byte_p = (u8 *) (DA_SIZE + 128 + cpuOffset);
    volatile u16    *halfword_p = (u16 *) (DA_SIZE + 128 + cpuOffset);
    volatile u32    *word_p = (u32 *) (DA_SIZE + 128 + cpuOffset);
    u32             daxAddr = 0x0, word = 0x0;
    u16             halfword = 0x0;
    u8              byte = 0x0;

    /* Set RW permission */
    daxAddr = DAX_START + DA_SIZE + 128 + cpuOffset;
    IO_WRITE(daxAddr, PT_ACCESS_RW << PT_SC_PROT_SHIFT);

    while (iterations-- > 0) {
        if (cpuId == 0) {
            *word_p = 0x10203040;
        } else {
            *word_p = 0xf5e6d7c8;
        }

        /* The following reads should all hit in the L2 cache */
        DL1Invalidate((void *) word_p, sizeof (*word_p));
        word = *word_p;

        DL1Invalidate((void *) word_p, sizeof (*word_p));
        halfword = *halfword_p;

        DL1Invalidate((void *) word_p, sizeof (*word_p));
        byte = *byte_p;

        if (cpuId == 0) {
            if ((word != 0x10203040) || (halfword != 0x3040) ||
                    (byte != 0x40)) {
                rc = -21;
                goto hitL2Check_out;
            }
        } else {
            if ((word != 0xf5e6d7c8) || (halfword != 0xd7c8) ||
                    (byte != 0xc8)) {
                rc = -22;
                goto hitL2Check_out;
            }
        }

        /* The following writes should also hit in the L2 cache */
        DL1Invalidate((void *) word_p, sizeof (*word_p));
        *word_p = 0xfedcba98;
        *halfword_p = 0x1122;
        *byte_p = 0x34;

        /* Check the result of write */
        DL1Invalidate((void *) word_p, sizeof (*word_p));
        word = *word_p;

        DL1Invalidate((void *) word_p, sizeof (*word_p));
        halfword = *halfword_p;

        DL1Invalidate((void *) word_p, sizeof (*word_p));
        byte = *byte_p;

        if ((word != 0xfedc1134) || (halfword != 0x1134) || (byte != 0x34)) {
            rc = -23;
            goto hitL2Check_out;
        }
    }

hitL2Check_out:

    return (rc);
}


/*
 *  Function: hitSDRAMCheck
 *
 *  Description:
 *      Do byte/short/word reads and writes that hit in
 *      the SDRAM
 *
 *  Inputs:
 *      cpuId - ID of the CPU, 0 or 1
 *
 *  Outputs:
 *      None
 *
 *  Return values:
 *      0 - Successful completion
 *      < 0 - Error
 *
 */
int
hitSDRAMCheck(int cpuId)
{
    int             rc = 0, cpuOffset = (cpuId * 16), iterations = 25;
    volatile u8     *byte_p = (u8 *) (MEM0_START + cpuOffset);
    volatile u16    *halfword_p = (u16 *) (MEM0_START + cpuOffset);
    volatile u32    *word_p = (u32 *) (MEM0_START + cpuOffset);
    u32             word = 0x0;
    u16             halfword = 0x0;
    u8              byte = 0x0;

    /* Give RW access */
    IO_WRITE(PT_START, (PT_ACCESS_RW << PT_SC_PROT_SHIFT) << PT_ENTRY_SHIFT);

    while (iterations-- > 0) {
        if (cpuId == 0) {
            *word_p = 0xab12cd34;
        } else {
            *word_p = 0xee55ff66;
        }

        /* The following reads should all hit in the SDRAM */
        SCInvalidate(cpuOffset);
        word = *word_p;

        SCInvalidate(cpuOffset);
        halfword = *halfword_p;

        SCInvalidate(cpuOffset);
        byte = *byte_p;

        if (cpuId == 0) {
            if ((word != 0xab12cd34) || (halfword != 0xcd34) ||
                    (byte != 0x34)) {
                rc = -11;
                goto hitSDRAMCheck_out;
            }
        } else {
            if ((word != 0xee55ff66) || (halfword != 0xff66) ||
                    (byte != 0x66)) {
                rc = -12;
                goto hitSDRAMCheck_out;
            }
        }

        /* The following writes should also hit in the SDRAM */
        SCInvalidate(cpuOffset);
        *word_p = 0x13572468;

        SCInvalidate(cpuOffset);
        *halfword_p = 0xacbd;

        SCInvalidate(cpuOffset);
        *byte_p = 0xfe;

        /* Check the result of write */
        SCInvalidate(cpuOffset);
        word = *word_p;

        SCInvalidate(cpuOffset);
        halfword = *halfword_p;

        SCInvalidate(cpuOffset);
        byte = *byte_p;

        if ((word != 0x1357acfe) || (halfword != 0xacfe) || (byte != 0xfe)) {
            rc = -13;
            goto hitSDRAMCheck_out;
        }
    }

hitSDRAMCheck_out:

    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;
    u32     delay = 150;

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

    /*
     * Change the cache config to 192KB DA, 64KB SC, and
     * 64KB CC
     */
    IO_WRITE(SYS_CTRL_REG, (IO_READ(SYS_CTRL_REG) &
                        ~(SYS_CTRL_CACHE_CONF_MASK | SYS_CTRL_SOFT_RST)) |
                        SYS_CACHE_CONF_DA_192_CC_64_SC_64 | SYS_CTRL_CFG_EN);

    delay=10;
    IO_WRITE(MC_CTRL_REG, 0);
    while (delay--);
    delay=150;

    /* Enable the SDRAM */
    IO_WRITE(MC_CTRL_REG, MC_CTRL_ENA | MC_CTRL_PALL | 0x400);
    while (delay--);
    delay = 150;
    IO_WRITE(MC_CTRL_REG, MC_CTRL_ENA | MC_CTRL_MRS | 0x2a);
    while (delay--);
    delay = 150;
    IO_WRITE(MC_CTRL_REG, MC_CTRL_ENA);
    while (delay--);

    delay=10;
    IO_WRITE(MC_CTRL_REG, MC_CTRL_ENA | MC_CTRL_DQM_ENA);
    while (delay--);

    DVStartCpu1();

    /* Test #1: Reads/writes that hit in the L1 cache */
    rc = hitL1Check(0);
    if (rc < 0) {
        goto cpu0main_out;
    }

    /* Test #2: Reads/writes that hit in the L2 cache */
    rc = hitL2Check(0);
    if (rc < 0) {
        goto cpu0main_out;
    }

    /* Test #3: Read/writes that hit in the SDRAM */
    rc = hitSDRAMCheck(0);
    if (rc < 0) {
        goto cpu0main_out;
    }

    printf("cpu0: wait for cpu1 ...\n");
    while (cpu1done == 0);
    printf("cpu0: cpu1done %d\n", cpu1done);

cpu0main_out:

    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);

    /* Test #4: Read/writes that hit in the SDRAM */
    rc = hitSDRAMCheck(1);
    if (rc < 0) {
        goto cpu1main_out;
    }

    /* Test #5: Reads/writes that hit in the L2 cache */
    rc = hitL2Check(1);
    if (rc < 0) {
        goto cpu1main_out;
    }

    /* Test #6: Reads/writes that hit in the L1 cache */
    rc = hitL1Check(1);
    if (rc < 0) {
        goto cpu1main_out;
    }

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