/*
 *              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:
 *      Test program to create a scope loop for hardware bug.
 *      Jump randomly through SDRAM.
 *      This program is executed from minimon, not on the ARM simulator,
 *      so it doesn't need to initialize SDRAM.
 */


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

/*
 * Use the random register to create a seed value for srand
 */
u32
getseed(void)
{
    u32 seed = 0;
    u32 val;
    int i;

    /* turn on the RNG and warm it up */
    IO_WRITE(SYS_RNG_REG, SYS_RNG_SEL_USB << SYS_RNG_SEL_SHIFT);
    for (i = 0; i < 256; i++) {
        val = IO_READ(SYS_RNG_REG);
    }

    /* read out 32 bits for the seed */
    for (i = 0; ; i++) {
        seed |= IO_READ(SYS_RNG_REG) & SYS_RNG_BIT;
        if (i == 31)
            break;
        seed <<= 1;
    }

    return seed;
}

#define HEADER_SIZE 0x100
#define BASE_ADDRESS (MEM0_START + HEADER_SIZE)
#define TEST_SIZE 0x800000	/* 8 MB */

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

    /*
     * Running from minimon, so SDRAM and protections are already set up
     */
#if DEBUG
    for (off = 0; off < 0x10000000; off++) ;	/* to allow break */
#endif

    /* Initialize the RNG */
    srand(getseed());
    srand(rand());

    /*
     * Fill the first 8 MB of SDRAM with "jump self" ARM instruction
     */
    for (wp = (u32 *)MEM0_START; (u32)wp < MEM0_START + TEST_SIZE; wp++) {
        *wp = 0xeafffffe;
    }

    /*
     * Now create a chain of jumps that randomly go through SDRAM
     * in an infinite loop.
     */
    off = 0;
    done = 0;
    while (!done) {
        /* location to be stuffed with branch */
        wp = (u32 *)(BASE_ADDRESS + off);
        /* calculate a random offset forward by 4 to 128 bytes */
        diff = 4 + ((rand() & 0x7c0) >> 4);
        off += diff;
        /* if off the end, then jump back to beginning */
        if (off >= TEST_SIZE - HEADER_SIZE) {
            diff = (u32)wp - BASE_ADDRESS;
            diff = -diff;
            done++;
        }
        /* write relative branch instruction to the next location */
        *wp = 0xea000000 | (((diff-8)>>2) & 0xffffff);
    }

    /*
     * Set execute permission on SDRAM
     */
    u32 v = (((PT_ACCESS_EXEC << PT_CC_PROT_SHIFT) | (PT_ACCESS_EXEC << PT_SC_PROT_SHIFT)) << PT_ENTRY_SHIFT);
    u32 step = 16384;
    for (off = PT_START; off < PT_START+TEST_SIZE; off += step) {
        IO_WRITE(off, v);
    }

    /*
     * Now jump to the start of the chain ...
     */
    fp = (void (*)(void))BASE_ADDRESS;
    fp();

    /* Not reached */

    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;

    return (rc);
}
