#ifdef CPE_40
#ifdef BUP_DIAGS
#include <stdio.h>
#ifdef USE_VXWORKS /* IXDP425 */
#include <vxWorks.h>
#include <bme.h>
#else
#include <sys/types.h>
#include <linux/param.h>
#include <v2pthread.h>
#include <sys/times.h>
#endif
#include "bup_jaguar_mem.h"
#define VERSION_NUMBER 0xA10
#define VERSION_ADDR 0x0B
#ifdef CPE_50

int chkAfeBusy ()
{

    volatile unsigned long busyStatus;
    int itrCount = 0;
        busyStatus = testPIORead1 (1,CPE5_AFE_RDWR_STAT );

    while (busyStatus & AFE_CMD_BUSY)
    {
        if (itrCount++>AFE_MAX_RW_REPEAT)
        {
            printf(" AFE is Busy - timed out");
            busyStatus = 0xFF;
            break;
        }
        taskDelay(5);
            busyStatus = testPIORead1 (1,CPE5_AFE_RDWR_STAT );
    }

    if (busyStatus == 0xFF)
            return -1;
        else
        return 0;
}

#endif  /* CPE_50 */


#ifdef CPE_50
unsigned int rdIfeReg(unsigned int addr, int * data)
{
	unsigned int reg_data;
	unsigned int status, stat= 0;
	int i = 0;
	// Check the status of the IFE serial STAT reg
        status = testPIORead1 (1,CPE5_AFE_RDWR_STAT );

	i = 0;
        if ((status & AFE_CMD_FAIL) == AFE_CMD_FAIL)
	{
		printf(" ERROR : Fail bit is set in the AFE_RDWR_STAT Register: status = 0x%x\n",status);
		stat = -1;
		goto error;
	}

    // Check for Busy condition
        while ((status & AFE_CMD_BUSY) == AFE_CMD_BUSY )
	{
                status = testPIORead1(1,CPE5_AFE_RDWR_STAT);
		i++;
		taskDelay(20);
		if (i > 200 )
		{
			printf(" ERROR : TimedOUT for ReadDone bit: status = 0x%x\n",status);
			stat = -1;
			goto error;
		}
	}
        // Issue the read command
        testPIOWrite(1,CPE5_AFE_RDWR_CMD, (AFE_REG_RD | (addr & AFE_REG_ADDR_MASK)));

        // Check the status of the IFE serial STAT reg
        status = testPIORead1 (1,CPE5_AFE_RDWR_STAT );
    // Check for Command Failure condition
    i = 0;
        if ((status & AFE_CMD_FAIL) == AFE_CMD_FAIL)
	{
		printf(" ERROR : Fail bit is set in the AFE_RDWR_STAT Register: status = 0x%x\n",status);
		stat = -1;
		goto error;
	}

	// 0x80000000 is for read bit, send read command & wait for data in data reg
	testPIOWrite(1,AFE_RDWR_CMD, (0x80000000 | addr));

	// Check the status of the IFE serial STAT reg
	status = testPIORead1 (1,AFE_RDWR_STAT );

	i = 0;
    //Check for the Busy and Fail condition
        while ((status & AFE_CMD_BUSY) == AFE_CMD_BUSY )
	{
                status = testPIORead1(1,CPE5_AFE_RDWR_STAT);
		i++;
		taskDelay(20);
		if (i > 200 )
		{
                        printf(" ERROR : AFE BUSY after issuing read command : 0 x%x\n",status);
			stat = -1;
			goto error;
		}
	}
        // Check for the Done status
    i = 0;
    status = testPIORead1(1, CPE5_AFE_RDWR_STAT);
    while ((status & AFE_CMD_PASS) == 0)
        {
                status = testPIORead1(1,CPE5_AFE_RDWR_STAT);
                i++;
                taskDelay(20);
                if (i > 200 )
	{
                        printf(" ERROR : AFE READ DONE timeout Detected after : 0x%x\n",status);
		stat = -1;
		goto error;
	}

        }

    // Done status detected indicating the command has passed, fetch data from t he data register
        reg_data = testPIORead1(1,CPE5_AFE_READ_DATA);

        /* Set AFE_CMD_PASS to clear status */
        testPIOWrite(1,CPE5_AFE_RDWR_STAT, AFE_CMD_PASS);
        *data = reg_data;

error:

if (stat) printf ("rdife %x\n", stat);
        return stat;

}

#endif  /* CPE_50*/

#ifdef CPE_50
unsigned int wtIfeReg(unsigned int addr, unsigned int dt)
{
        unsigned int status, stat= 0;
        int i = 0;

        // Check the status of the IFE serial STAT reg
        status = testPIORead1 (1,CPE5_AFE_RDWR_STAT );

        i = 0;
        if ((status & AFE_CMD_FAIL) == AFE_CMD_FAIL)
        {
                printf(" ERROR : Fail bit is set in the AFE_RDWR_STAT Register: status = 0x%x\n",status);
                stat = -1;
                goto error;
                //break;
        }

    // Check for the busy status
        while ((status & AFE_CMD_BUSY) == AFE_CMD_BUSY )
        {
                status = testPIORead1(1,CPE5_AFE_RDWR_STAT);
                i++;
                taskDelay(20);
                if (i > 200 )
                {
                        printf(" ERROR : TimedOUT for ReadDone bit: status = 0x% x\n",status);
                        stat = -1;
                        goto error;
                }
        }

        // 0x40000000 is for write bit, send write command & wait for complition of command
        testPIOWrite(1,CPE5_AFE_WRITE_DATA,dt);
        testPIOWrite(1,CPE5_AFE_RDWR_CMD, (AFE_REG_WR | (addr & AFE_REG_ADDR_MASK)));

    // Check if write was successful
        status = testPIORead1 (1,CPE5_AFE_RDWR_STAT );

        if ((status & AFE_CMD_FAIL) == AFE_CMD_FAIL)
        {
                printf(" ERROR : Fail bit is set in the AFE_RDWR_STAT Register: status = 0x%x\n",status);
                stat = -1;
                goto error;
        }

        i = 0;
        while ((status & AFE_CMD_BUSY) ==AFE_CMD_BUSY )
        {
                status = testPIORead1(1,CPE5_AFE_RDWR_STAT);
                i++;
                taskDelay(20);
                if (i > 200 )
                {
                        printf(" ERROR : TimedOUT for ReadDone bit: status = 0x% x\n",status);
                        stat = -1;
                        goto error;
                }
        }

        // Check for the Done status
    i = 0;
    status = testPIORead1(1, CPE5_AFE_RDWR_STAT);
    while ((status & AFE_CMD_PASS) == 0)
        {
                status = testPIORead1(1,CPE5_AFE_RDWR_STAT);
                i++;
                taskDelay(20);
                if (i > 200 )
                {
                        printf(" ERROR : AFE READ DONE timeout Detected after : 0x%x\n",status);
                        stat = -1;
                        goto error;
                }

        }

    // Clear the done status
        testPIOWrite(1,CPE5_AFE_RDWR_STAT, AFE_CMD_PASS);

error:
if (stat) printf ("wrife %x\n", stat);

        return stat;
}


#endif  /* CPE_50 */

#ifdef CPE_50
int HldWrite (int data)
{

    int i = 0;
    int error = 0;
    int doneStatus;
    int itrCount = AFE_MAX_RW_REPEAT;

    // Check for done status before issuing the write HLD data or command
    while (itrCount--)
    {
        // wait for done bit
        error = rdIfeReg (AFE_HLD_REG_RD_WR_STAT, &doneStatus);

        if (!error)
        {
			{
                if ((doneStatus & HLD_STAT) == HLD_STAT)
				    break;
			}

        }
        taskDelay (5);

    }

    if (!itrCount)
    {
        printf("Done bit not cleared, exit without write data to hld: doneStatus - 0x%x\n", doneStatus);
        goto exitPoint;

    }

    //Write the data to HLD
    //error = wtIfeReg (AFE_HLD_REG_RD_WR_STAT, (data << 17));
    //Format is 31     30:28    27:17   16       15:12    11:1      0
    //          Status Reserved WR_DATA Port Sel Reserved Read Data Port Selected

    error = wtIfeReg (AFE_HLD_REG_RD_WR_STAT, (data << 17));
    if (!error)
    {
        error = wtIfeReg (AFE_HLD_RDWR_CMD, HLD_REG_WR);
        if (!error)
        {
            printf ("Write Command and Data to HLD complete, check for dones status\n");
        }
        else
        {
            printf ("Write Command to HLD Failed: error 0x%x\n", error);
            goto exitPoint;
        }
    }
    else
    {
        printf ("Write Data to HLD Failed: error 0x%x\n", error);
        goto exitPoint;
    }

    // wait for done bit after issuing the command and data
    itrCount = AFE_MAX_RW_REPEAT;
    while (itrCount--)
    {
        // wait for done bit
        error = rdIfeReg (AFE_HLD_REG_RD_WR_STAT, &doneStatus);

        if (!error)
        {
			{
                if ((doneStatus & HLD_STAT) == HLD_STAT)
				    break;
			}

        }
        taskDelay (5);

    }

    if (!itrCount)
    {
        printf("Done bit not cleared, after issuing write command and data to HLD : doneStatus - 0x%x\n", doneStatus);
        goto exitPoint;

    }

exitPoint:
    return error;
}


int HldRead (int *data)
{

    int error = 0;
    int doneStatus;
    int itrCount = AFE_MAX_RW_REPEAT;

    // write the read command to HLD
    error = wtIfeReg (AFE_HLD_RDWR_CMD, HLD_REG_RD);
    if (!error)
	{
		// Check for done status before reading HLD data 
		while (itrCount--)
		{
			// wait for done bit
			error = rdIfeReg (AFE_HLD_REG_RD_WR_STAT, &doneStatus);

			if (!error)
			{
                if ((doneStatus & HLD_STAT) == HLD_STAT)
				    break;
			}
			taskDelay (5);

		}

		if (!itrCount)
		{
			printf("Done bit not cleared, exit without reading data from hld: doneStatus - 0x%x\n", doneStatus);
			goto exitPoint;

		}

        // Fetch data from HLD
        error = rdIfeReg (AFE_HLD_REG_RD_WR_STAT, data); 
//newly added -- veerasena
	*data = (*data >> 1);
        if (!error)
        {
            printf ("Data Read from HLD 0x%x\n", *data);
        }
        else
        {
            printf ("Read data from HLD failed: error 0x%x\n", error);
            goto exitPoint;
        }
	}
    else
    {
        printf ("Read Command to HLD Failed \n");
        goto exitPoint;
    }


exitPoint:
    return error;

}

#endif	/* CPE_50 */
//---------------------------------------------------------------
int ife_loop ()
{
	int i=0;
	int ife_present=0;
	unsigned int data;
	unsigned int IfeId;
	int Flag = 0;
	int stat = 0;


	stat = wtIfeReg (0x0, 0xaaaaaaaa);
	if (stat)
		goto end;
	stat = rdIfeReg (0x0, &IfeId);
	if (stat)
		goto end;

	if (IfeId != 0xaaaaaaaa)
	{
		stat = ERROR;
		goto end;
	}



	stat = wtIfeReg (0x0, 0x55555555);
	if (stat)
		goto end;
	stat = rdIfeReg (0x0, &IfeId);
	if (stat)
		goto end;


	if (IfeId != 0x55555555)
		stat = ERROR;

end:
	if (stat)
		printf ("\t\t\t**************** IFE Serial Test Failed\n");
	else
		printf ("\t\t\tIFE Serial Test Passed\n");

	return stat;


}
//------------------------------------------------------------


int bup_testIfe ()
{
int status = 0;

	//w do a serial test, then an 
	status = ife_loop ();
	if (!status)
		status = lbistDdr ();
	return status;
}




#endif
#endif
