#ifdef BUP_DIAGS
#include <stdio.h>
#ifdef USE_VXWORKS /* IXDP425 */
#include <vxWorks.h>
#include <ioLib.h>
#include <tickLib.h>
#else
#include "ipos.h"
#include "dapi.h"
#include "apilocal.h"
#include "host_ver.h"
#include "apposmacro.h"
#define UINT unsigned int
#endif


int pio_read_return (int);
void bmeHwResetApi (int BmeNum);
void bme2HWReset (void);
extern void pio_read (UINT32 srcAddr, UINT32 *data);
extern void pio_write (UINT32 destAddr, UINT32 data);
extern void doDmaWrite(UINT32 destAddr, BYTE *dataPtr, UINT32 dataLen, int destFifoFlag); 
extern void doDmaRead (UINT32 srcAddr, UINT32 codeLen, BYTE * BmeCodeArrayPtr, int destFifoFlag);
extern int testPIORead1 (int BmeNum, unsigned long addr);
extern void testPIOWrite_diag (int BmeNum, unsigned long addr, unsigned long data);
//extern void doDmaRead_diag (int, int, char *, int);
extern void iposDelay (void);
extern int fnum;
int bup_InitSdramCO ();
int bup_HICTest ();
int bup_SDRAMTest ();
int bup_EMITest ();
int bup_DMATest ();
int bup_UARTTest ();
int bup_UARTLoopTest ();
int bup_AFETest ();
int bup_IFETest ();
int bup_HICStressTest ();
int bup_BMEStressTest ();
int bup_memTestGetMemSz ();
int set_clk ();
int bup_memTestAddrData ();
int bup_SdramRandom ();
int bup_SdramDma ();
int bup_regress_dma ();
int bup_sdram_dma_1_100bytes ();
int bup_dmadebug ();
int bup_check_co_id ();
int bup_SendMsgUart0CO ();
int bup_SendMsgUart1CO ();
int bup_LoopbackUartCO ();
int bup_afe_reg_rw ();
int bup_testIfe ();
int bup_testHICT2 ();
int bup_testHICT1 ();

int bup_IFEVersion ();
int bup_IFEWrRd ();
int bup_IFEWrRdTest ();
int bup_HLDWrRdTest ();

int printDiags = 1;
// assume user mem = 8meg
int userMem = 0x800000;
int userMem1 = 0x800000;
int userMem2 = 0x800000;
int bup_bme = 1;

#define HIC_STRESS		9
#define BME_STRESS		10

//-----------------------------------------------------
UINT32 bup_tprr (int junk, UINT32 addr) {
  UINT32 temp;

  temp = testPIORead1 (bup_bme,addr);
  return temp;
}
//-----------------------------------------------------
void bup_init_dma ()
{
  // program DMA controller registers
  testPIOWrite (bup_bme, 0xe0030040, 0x80000000);  //ecr
  testPIOWrite (bup_bme, 0xe0030080, 0xc2000000);  //sar
  testPIOWrite (bup_bme, 0xe00300c0, 0xd0000000);  //dar
  testPIOWrite (bup_bme, 0xe0030000, 0x02057000);  //ccr
}
//---------------------------------------------
int bup_start_dma (int DmaSize)
{
  UINT32 temp=0;
  UINT32 ccr;

  ccr = 0x1601 | ((DmaSize&0x1fff) << 16);
  testPIOWrite (bup_bme, 0xe0030000, ccr);  // start dma
  
  while (!(temp & 0x100)) {
    temp = bup_tprr (bup_bme,0xe0030000);
  }
}
//---------------------------------------------

//--------------------------------------------------------------
void bup_set_clk () 
{
    tpw (bup_bme,0xe0070008,0x4f);
    tpw (bup_bme,0xe0070008,0x14f);
//    tpw (bup_bme,0xe0070024,0x43);
//    tpw (bup_bme,0xe0070024,0x143);
}



int  bup_JagClrRxReady(int i)
{
	testPIOWrite(1, 0x40008f78, 0);
}


void bup_init ()
{

//#ifdef CPE_30
#if defined(CPE_30) || defined(CPE_40)
	  bup_JagClrRxReady(1);
#else  
	  iposTaskDelay (10);
	  bmeHwResetApi (1);
      iposTaskDelay (10);
	  bup_InitSdramCO(1,1);
      iposTaskDelay (10);
#endif
	  testPIOWrite (1, 0x40000100,0);
	  testPIOWrite (1, 0x40008000,0);
	  testPIOWrite (1, 0x40008004,0);
	  testPIOWrite (1, 0x40008008,0);
	  testPIOWrite (1, 0x4000800c,0);
	  testPIOWrite (1, 0x40008010,0);
      iposTaskDelay (10);


	  bup_set_clk ();
      iposTaskDelay (10);


}

//--------------------------------------------------------------
void bup_diags ()
{
int testnum = 1;
extern int consoleFd;
int		bytesRead;
unsigned int x1, x2, x3;
char buffer [32];
static int	firstTime2 = TRUE;


//while (1) taskDelay (1);
		
	  bup_init ();

	  while (1)
	  {
top1:

      printf("\n\t  0. HIC Test\n");
      printf("\t  1. SDRAM Test\n");
#ifndef CPE_40
      printf("\t  2. EMI Test\n");
#endif
      printf("\t  3. DMA Test\n");
      printf("\t  4. UART 0 Write Test\n");

#ifndef CPE_40
      printf("\t  5. UART 1 Write Test\n");
      printf("\t  6. UART Loop Test\n");
      printf("\t  7. AFE test\n");
#endif
      // printf("\t  8. IFE test\n");
      printf("\t  8. IFE Write Read test\n");
      printf("\t  9. HIC Stress Test\n");
      printf("\t 10. BME Code Stress test\n");
      printf("\t 20. Display BME Memory '20 address' \n");
      printf("\t 21. Fill BME Memory  '21 address num 32bit_data'\n");
      printf("\t 30. Enter Memory on Board (selected: %x)  '30 memory_size'\n",userMem);
      printf("\t 31. Detect Memory size\n");
      printf("\t 88. IFE Version\n");
      printf("\t 89. Init CPE5 IFE PLL\n");
      printf("\t 90. HLD Write Read Test\n");
      printf("\t 91. Exit from Diag Menu\n");
//#ifndef CPE_30
#if !defined(CPE_30) && !defined(CPE_40)

      printf("\t 40. Enter Board number to test 1,2 (selected %d) '40 number'\n",bup_bme);
#endif

      printf("\n*****************************************************\n");  
#ifndef ALASKA
	  ioctl (consoleFd, FIORFLUSH, (int) &bytesRead);
	  ioctl (consoleFd, FIONREAD, (int) &bytesRead);
#endif
	  printf("Please enter the diag number: ");
#ifndef ALASKA
	  gets (buffer);
#else
          scanf("%s",buffer);
          fflush(stdout);
#endif
//printf("%s\n",buffer);
	  sscanf (buffer, "%d %x %x %x", &testnum, &x1, &x2, &x3);

	  if ((testnum <HIC_STRESS) || (testnum == 31))
	  {
		int data;
	    data = testPIORead1 (bup_bme,0x40000100);
		iposTaskDelay (100);
        if (data == 0x12345678)
		{
			bup_init ();
			printf("BME Running Code. Please power cycle now...\n");
			while (1);
		}

		bmeHwResetApi (1);
		iposTaskDelay (100);
//#ifndef CPE_30
#if !defined(CPE_30) && !defined(CPE_40)
	    bup_InitSdramCO(1,1);
#else
	    bup_InitSdramCPE();
	
#endif
	  }
	


	iposTaskDelay (100);

	switch (testnum) {
		case 0: bup_HICTest (); break;
		case 1: bup_SDRAMTest (); break;
#ifndef CPE_40
		case 2: bup_EMITest (); break;
#endif
		case 3: bup_DMATest (); break;
		case 4: bup_UARTTest (0); break;
#ifndef CPE_40
		case 5: bup_UARTTest (1); break;
		case 6: bup_UARTLoopTest ();break;
		case 7: bup_AFETest ();break;
#endif
                case 8: bup_IFEWrRdTest ();break;

                case 88: bup_IFEVersion (); break;
                case 89: bup_IFEPrgPLL ();break;
                case 90: bup_HLDWrRdTest ();break;
                case 91: exit(0);
		case HIC_STRESS: bup_HICStressTest ();break;
		case BME_STRESS: bup_BMEStressTest ();break;
		case 20: 
			if (x1 >= 0x40000000)
				D (bup_bme, x1,0x40,4);
			else
				printf("address %08x out of range\n",x1);
		break;
#ifndef ALASKA
		case 21: 
			if (x1 >= 0x40000000)
				F (bup_bme, x1,x2,x3);
			else
				printf("address %08x out of range\n",x1);

		break;
#endif
		case 30: 
			userMem = x1;
			if (bup_bme == 1)
				userMem1 = userMem;
			else if (bup_bme == 2)
				userMem2 = userMem;
		break;
		case 31: bup_memTestGetMemSz (); break;
//#ifndef CPE_30
#if !defined(CPE_30) && !defined(CPE_40)
		case 40: 
			bup_bme = x1;
			if (bup_bme == 1)
				userMem = userMem1;
			else if (bup_bme == 2)
			{
				userMem = userMem2;
				if (firstTime2)
				{
					bup_set_clk ();
					firstTime2 = FALSE;
					testPIOWrite (2, 0x40000100,0);
				}
			}
			bup_InitSdramCO(1,1);

		break;
#endif

		default: 
#ifndef ALASKA
			ioctl (consoleFd, FIORFLUSH, (int) &bytesRead);
#endif
			goto top1;
		break;
	}
	printf("Press ENTER to continue\n");
        fflush( stdout ); // Fusiv
	getchar ();
    }

}





int bup_SDRAMTest ()
{
int error;

   	error = bup_memTestAddrData ();
//	error |= SdramBasic(); 
	return error;
}



int bup_EMITest ()
{
	return bup_SdramRandom ();
}



int bup_DMATest ()
{
int error, oldP;

	oldP = printDiags;
	printDiags = 0;
	error = bup_SdramDma(1000,0x1a2b3c4d,1,0); 
	if (error)
		printf ("\t**** DMA Test 1 FAILED\n");
	else
		printf ("\tDMA Test 1 Passed\n");
	error |= bup_regress_dma(); 
	if (error)
		printf ("\t**** DMA Test 2 FAILED\n");
	else
		printf ("\tDMA Test 2 Passed\n");
	error |= bup_sdram_dma_1_100bytes(); 
	if (error)
		printf ("\t**** DMA Test 3 FAILED\n");
	else
		printf ("\tDMA Test 3 Passed\n");
	error |= bup_dmadebug();
	if (error)
		printf ("\t**** DMA Test 4 FAILED\n");
	else
		printf ("\tDMA Test 4 Passed\n");
	printDiags = oldP;

	if (error)
	{
		printf ("******************************************************\n"); 
		printf ("\tDMA Test FAILED\n");
		printf ("******************************************************\n"); 
	}
	else if (printDiags)
	{
		printf ("******************************************************\n"); 
		printf ("\tDMA Test PASSED\n");
		printf ("******************************************************\n"); 
	}

	return error;

}

int bup_HICTest ()
{
//#ifdef CPE_30
#if defined(CPE_30) || defined(CPE_40)
	return bup_check_cpe_id ();
#else
	return bup_check_co_id ();
#endif

}

int bup_UARTTest (int which)
{
	if (which == 0)
		bup_SendMsgUart0CO(); 
	else
		bup_SendMsgUart1CO(); 
	return 0;

}

int bup_UARTLoopTest ()
{
int error;
	error = bup_LoopbackUartCO (0);
	error |= bup_LoopbackUartCO (1);
	return error;
}


#ifndef CPE_40


int bup_AFETest ()
{
int i;
int error = 0;
int old;

	old = printDiags;
	printDiags = 0;
	printf("\n");
	error = bup_afe_reg_rw();  
	printf("\n");
	if (!error)
		printf("AFE Read/Write passed\n");
	else
		printf("AFE Read/Write Failed %d\n",i);
	printDiags = old;
	return error;
}
#endif


int bup_IFETest ()
{
int i = 0;
int error = 0;
int old;

	old = printDiags;
	printDiags = 0;
	printf("\n");

#if defined (CO_20)
	for (i = 0;i < 8;i++)
		error |= bup_testIfe (i);
#else 
	error |= bup_testIfe (0);
#endif
	printf("\n");
	if (!error)
		printf("IFE Read/Write passed\n");
	else
		printf("IFE Read/Write Failed %d\n",i);
	printDiags = old;
	return error;

}


int bup_BMETest ()
{
int data;

	bup_testHICT2 (); 
	do
	{
		data = testPIORead1 (bup_bme,0x40000104);
		iposTaskDelay (1);
	} while (data == 0x5555);

	if (data == 0x4321)
	{
		if (printDiags) printf("DMA Test passed %x\n", data);
	}
	else
		printf("DMA Test FAILED %x\n", data);

}



int bup_HICStressTest ()
{
//#ifdef CPE_30
#if defined(CPE_30) || defined(CPE_40)
	bup_JagClrRxReady(1);
#endif
	bup_testHICT1 (1000); 
	if (printDiags)
		printf("HIC Test passed\n");
	return 0;
}



int bup_BMEStressTest ()
{
	int data;
	bup_JagClrRxReady(1);
	bup_testHICT2 (); 

	do
	{
		data = testPIORead1 (bup_bme,0x40000104);
		iposTaskDelay (1);
	} while (data == 0x5555);

	if (data == 0x4321)
	{
		printf("BME Stress Test passed\n");
		return 0;
	}
	else if (data == 0x1235)
	{
		printf("AFE Stress Test FAILED %x\n", data);
		return 1;
	}
	else 
	{
		printf("BME Stress Test FAILED %x\n", data);
		return 1;
	}


}

#if 0

void wpg(int BmeNum,BYTE addr,BYTE data)
{
BYTE *baseAddr;

Bme[BmeNum].BMEbaseAddr=BME_BASE_ADDR;
baseAddr = (BYTE *)Bme[BmeNum].BMEbaseAddr;
writeControlPort(baseAddr,(addr|((BmeNum-1)<<4)), data);

}

void pio_write (UINT32 destAddr, UINT32 data)
{

	HostCpuEndianType = 2;
	Bme[1].BMEbaseAddr = BME_BASE_ADDR;	

	/* sw DMA request */
	bmePioWrite (&Bme[1],(unsigned long) destAddr, (BYTE *)&data);
	
	/*printf ("\nWrite data 0x%08lx dest: 0x%08lx", data,destAddr);			*/

}
#endif
#define IFE_VERSION_ADDR 0x00
#define IFE_PGA1_ADDR    0x01
#define IFE_PGA2_ADDR    0x08

int bup_IFEVersion ()
{
    int error = 0;
    unsigned int IfeVer;

    error = rdIfeReg (IFE_VERSION_ADDR, &IfeVer);

    if (!error)
    {
        IfeVer = IfeVer & 0x1FFF;
        printf("Read ifeVer %x \n",IfeVer);
        if ( (IfeVer == 0x0E10) || (IfeVer == 0x0E11))
                printf ("\t\t\t Read IFE Verion Passed, IFE Version is 0x%x\n",IfeVer);

    }
    else
    {
        printf ("\t\t\t******** Read IFE Version Failed ********\n");
    }

    return error;
}


int bup_IFEWrRd (unsigned int addr, unsigned int pattern)
{
    unsigned int data;
    int error = 0;

    error = wtIfeReg (addr, pattern);

    if (!error)
    {
            printf ("\t\t\t******** IFE Write of pattern 0x%x to register %d successful\n", pattern, addr);
    }
    else
    {
            printf ("\t\t\t******** IFE Write of pattern 0x%x to register %d Failed\n", pattern, addr);
        return error;
    }

    error = rdIfeReg (addr, &data);
    if (!error)
    {
            printf ("\t\t\t******** IFE Read of register %d successful with data0x%x\n", addr, data);
    }
    else
    {
           printf ("\t\t\t******** IFE Read of register %d Failed\n", addr);
        return error;
    }

    if (pattern != data)
    {
         printf ("Mismatch in written data : 0x%x and read values : 0x%x\n", pattern, data);
         error = 1;
    }

    return error;
}

int bup_IFEWrRdTest ( )
{
    int error = 0;

    error  = bup_IFEWrRd (IFE_PGA1_ADDR, 0xaaaaaaaa);
    error |= bup_IFEWrRd (IFE_PGA1_ADDR, 0x55555555);
#if 1
    error |= bup_IFEWrRd (IFE_PGA2_ADDR, 0xaaaaaaaa);
    error |= bup_IFEWrRd (IFE_PGA2_ADDR, 0x55555555);
#endif


    if (!error)
        printf("IFE Read/Write Test passed\n");
    else
        printf("IFE Read/Write Test failed\n");

    return error;
}

int bup_IFEPrgPLL ()
{
    #define CPE5_AFE_BANDGAP_CTRL 0x03

    unsigned int IfeVer;
    int readData;
    int error = 0;
    int itrCount = 10;

    // Read IFE Version
    error = rdIfeReg (IFE_VERSION_ADDR, &IfeVer);

    // If read not successful, cannot program the PLL in Band Gap, else, goaheadto program PLL
    if (!error)
    {
        IfeVer = IfeVer & 0x1FFF;

        // If IfeVersion is 0x0E10 then program the Band gap register (PLL bug)
        if (IfeVer == 0x0E10)
        {
            printf ("\t\t\t Read IFE Version is 0x%x\n", IfeVer);
            while (itrCount --)
           {
                error = wtIfeReg (CPE5_AFE_BANDGAP_CTRL, 0);
                if (!error)
                {
                     error = rdIfeReg (CPE5_AFE_BANDGAP_CTRL, &readData);
                     if (!error)
                     {
                         if (readData == 0)
                             break;
                     }
                }
            }
            if (itrCount)
            {
                printf ("Band Gap Register set to 0x%x, ItrCount = %d\n", readData, (10 - itrCount));
            }
            else
            {
                printf ("ERROR!!!: Band Gap Register Not set to 0x%x, ItrCount =%d\n", readData, (10 - itrCount));

            }
        }

    }
    else
    {
        printf ("\t\t\t******** Read IFE Version Failed Cannot program Band GapRegister ********\n");
    }

    return error;
}

int bup_HLDWrRdTest ()
{

#define HLD_LNA_GAIN  ((1 << 10) | (1 << 9))
    #define MAX_HLD_BITS_TO_TEST 8

    int retVal;
    int i;

    unsigned short int curData;

    for(i=0; i<MAX_HLD_BITS_TO_TEST; i++)
    {
        printf ("\n");
        retVal = HldWrite (1<<i);
        if (retVal != 0)
            printf ("Write HLD Failed\n", retVal);
        else
        {
            retVal = HldRead (&curData);
            if (retVal != 0)
                printf ("Read HLD Failed\n", retVal);
            else
            {
                if ((curData & 0xFF) != (1<<i))
                {
                    printf ("HLD Verify Failed: Data Written 0x%x, Data Read 0x%x\n", (1<<i), (curData & 0xFF));
                }
                else
                {
                    printf ("HLD Verify Passed: Data Written 0x%x, Data Read 0x%x\n", (1<<i), (curData & 0xFF));
                }
            }
        }
    }


    return retVal;
}


#endif
