/***
 *
 *  Trade secret of ATI Technologies Inc.
 *  Unpublished work, Copyright 2005 ATI Technologies Inc.
 *
 *  All rights reserved.  This notice is intended as a precaution against
 *  inadvertent publication and does not imply publication or any waiver
 *  of confidentiality.  The year included in the foregoing notice is the
 *  year of creation of the work.
 *
 ***/

#include "diag.h"
#include <iop.h>
#include <rvldv.h>
#include "initvar.h"

/* 
 * This is a basic test of SHA operation
 */
int shaOpTest(u32 srcAddr)
{
    int i, len, err = 0;
    u32 d = 0;
    u32 expected = 0;

    expected = 0x12345678;
    len = 16;		/* 16 words = 64 bytes */

    /* Initialize srcAddr with 64-byte blocks of data */
    for (i=0; i < len; i++) {
        busPrintf("%s: Writing srcAddr: 0x%x : 0x%x", __FILE__, 
            srcAddr+(i*4), expected);
        busWrt32((u32)(srcAddr+(i*4)), expected);
        busDelay(50);
    }

    /* Read and verify data */
    for (i=0; i < len; i++) {
        d = busRd32((u32)(srcAddr+(i*4)));
        busPrintf("%s: Reading srcAddr: 0x%x : 0x%x", __FILE__, 
            srcAddr+(i*4), d);
        busDelay(50);
    }

    /* Clear all 5 result buffers SHABUF[0-4] with 0 */
    busPrintf("%s: Clearing all 5 result buffers SHABUF[0-4] (0x%x)\n", 
        __FILE__, (SHA_BASE+SHABUF0));
    for (i=0; i < 5; i++) {
        busWrt32((u32)(SHA_BASE+SHABUF0+(i*4)), 0);
        d = busRd32((u32)(SHA_BASE+SHABUF0+(i*4)));
        if (d != 0) {
            err = 1;
            busPrintf(">>> %s: Clearing SHABUF%d (0x%x) failed, got=0x%x, exp=0x0\n", 
                  __FILE__, i, (SHA_BASE+SHABUF0+(i*4)), d);
            break;
        }
    } 

    /* Program SHA read pointer with srcAddr
       Bits: 5:0=0 */
    busPrintf("%s: Writing SHA.SHARPTR(0x%x) <= 0x%x\n", __FILE__, 
        (SHA_BASE+SHARPTR), srcAddr);
    busWrt32((u32)(SHA_BASE+SHARPTR), srcAddr);
    d = busRd32((u32)(SHA_BASE+SHARPTR));
    if (d != (srcAddr & ~0xF)) {
        err = 1;
        busPrintf(">>> %s: SHA.SHARPTR failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, (srcAddr & ~0xF));
    }

    /* Program SHA controller reg with length and kick-off operation 
       Bits 31=1 (start/stop, busy), 9:0=0 (64-byte block)-1 */
    d = 0x80000000;
    busPrintf("%s: Setting SHA.SHACTRL(0x%x) <= 0x%x\n", __FILE__, 
        (SHA_BASE+SHACTRL), d);
    busWrt32((u32)(SHA_BASE+SHACTRL), d);

    /* Poll busy bit 31 */ 
    busPrintf("%s: Polling SHA.SHACTRL(0x%x) busy bit 31...\n", __FILE__, 
        (SHA_BASE+SHACTRL));
    err = 1;
    for (i= 0; i < 1000; i++) {
        d = busRd32((u32)(SHA_BASE+SHACTRL));
        if ((d & 0x80000000) == 0) {
             err = 0;
             break;
        }
    }
    busPrintf("%s: Breakout from SHA.SHACTRL with 0x%x at count=%d\n", 
              __FILE__, d, i);

    /* Read SHA result buffer */
    busPrintf("%s: Reading result buffer SHABUF[0-4]\n", __FILE__);
    for (i=0; i < 5; i++) {
        d = busRd32((u32)(SHA_BASE+SHABUF0+(i*4)));
        busPrintf("%s: SHABUF%d (0x%x) = 0x%x", __FILE__, 
            i,SHA_BASE+SHABUF0+(i*4), d);
    }

    return(err);
}


int main()
{
    int err = 0;

    busInit(NULL, NULL, NULL);

    u32 srcAddr = SPLASH_BASE+0x400;

    /* Enable SHA access for SRON */
    busPrintf("%s: Enabling SHA access for SRON: Reg 0x%x <= 0x%x", __FILE__, 
        (ACR_REGS_BASE+ACRSRNPROT_OFFSET+AHB_TRUSTED_OFFSET), 
        ACRSRNPROT_SHAEN_MASK);
    busWrt32((u32)(ACR_REGS_BASE+ACRSRNPROT_OFFSET+AHB_TRUSTED_OFFSET), 
        ACRSRNPROT_SHAEN_MASK);

    err = shaOpTest(srcAddr);

    if (err)
        simFail(-1);
    else
        simPass(0);

    busClose();

    return 0;

}
