/**************************************************************
***************************************************************
**
** Host Application, Copyright (C) 2000-2002
** Ikanos Communication, Fremont, CA 
** All rights reserved.
**
** The information and contents of this file are the
** proprietary information of Ikanos Communication and may not
** be disclosed or used without the formal written approval of 
** Ikanos Communication
**
***************************************************************

$Revision: 1.1 $
$Date: 2006/10/16 04:34:50 $
**************************************************************/
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "vxWorks.h"
#include "drv/intrCtl/ppc860Intr.h"

#include "apiosmacro.h" 
#include "dapi_define.h" 
#include "dapi_parms.h"
#include "diag_ver.h"
#include "mem.h"
#include "bmehwapi.h"
#include "diag.h"
#include "bitmask.h"
#ifndef CPE_ATM
#include "drv/multi/mdpm860Siu.h"
#endif

#include "oampdef.h"
#include "apitype.h"
#include "apilocal.h"
#include "flash.h"
#include "bsp.h"
                                                                       
/* Driver debug control */
#define DRV_DEBUG557

LOCAL int getPara 
    (
    char 	*strptr,	/* pointer to pointer to string */
    BOOL	defaultHex,	/* TRUE = arg is hex (even w/o 0x) */
    BOOL	optional	/* TRUE = ok if end of line */
    );
typedef struct FunTblDef {
    char index;
    volatile FUNCPTR func;
} tFunTblDef;

#ifndef CPE_ATM
BOOL	sysAuxClkRunning	= FALSE;
int	sysAuxClkTicksPerSecond	= 60;
FUNCPTR	sysAuxClkRoutine	= NULL;
int	sysAuxClkArg		= NULL;
#endif

void* cookieTbl[32];

unsigned space_for_delay_read, delay_read_cnt;

char *cmdPtr, capTest;
char dtArray[6500];
int num1, num2, num3;
int tbits1, tbits2, fnum;
// Global Variables
tBmeStruct Bme[5];
tdiagConfTbl confTbl;

/* external definition */
extern tBmeStruct  	BmeStruct[];	  		 /* BME objects for direct API */
extern ipos_api_bme	apiBme[IPOS_MAX_BME+1];  
extern BYTE BmeResetVecCode[];
extern BYTE BMECodeArray[];

extern int 	fioRdString (int fd, char string[], int maxbytes);
extern void etherDiag(int);
extern void bmeReset(tBmeStruct *pBme);
extern int  tune_test;
extern BYTE HostCpuEndianType;
extern char tmpBuf[];
extern int	tmpFileSize;

extern void InitBmeStruct(tBmeStruct *pBme, int BmeNum);
extern void bmeRelCpu (tBmeStruct *pBme,unsigned char enable);
extern void InitDmaChannel15 (tBmeStruct *bmeStruct);
extern HWAPI_RET_VAL bmeVerifyWriteCode (tBmeStruct *bmeStruct, UINT32 codeDnldAddr, UINT32 codeLen, BYTE *BmeCodeArrayPtr);
extern eoamp_bme_state oampBmeStateUpdate(int bmeNum,eoamp_event   event);

tFunTblDef ft[] = { 
                    {1, (volatile FUNCPTR)xfifo_fast_mem_test},
                    {2, (volatile FUNCPTR)xfifo_inter_test},
                    {3, (volatile FUNCPTR)ext_test},
                    {4, (volatile FUNCPTR)fmc_i_test},
                    {5, (volatile FUNCPTR)fmc_d_test},
                    {6, (volatile FUNCPTR)Yfifo_fast_mem_test},
                    {7, (volatile FUNCPTR)tone_table_test},
                    {8, (volatile FUNCPTR)mapper_gain_test},
                    {9, (volatile FUNCPTR)zfifo_mem_test},
                    {10, (volatile FUNCPTR)cfifo_mem_test},
                    {11, (volatile FUNCPTR)dmc_i_test},
                    {12, (volatile FUNCPTR)dmc_d_test},
                    {13, (volatile FUNCPTR)smc_snr_test},
                    {14, (volatile FUNCPTR)smc_tone_test},
                    {15, (volatile FUNCPTR)smc_thres_test},
                    {16, (volatile FUNCPTR)bfifo_inter_test},
                    {17, (volatile FUNCPTR)bfifo_fast_mem_test},
                    {18, (volatile FUNCPTR)bfifo_demapper_test},
                    {19, (volatile FUNCPTR)demapper_test},
                    {20, (volatile FUNCPTR)feq_test},
                    {21, (volatile FUNCPTR)teq_test},
                    {22, (volatile FUNCPTR)rmc_i_test},
                    {23, (volatile FUNCPTR)rmc_d_test},
                    {24, (volatile FUNCPTR)afifo_mem_test},
                    {25, (volatile FUNCPTR)bme_dma_test},
                    {26, (volatile FUNCPTR)vbus0_test},
                    {27, (volatile FUNCPTR)vbus1_test},
                    {28, (volatile FUNCPTR)TNI_PioTst},
                    {29, (volatile FUNCPTR)Xfifo_PioTst},
                    {30, (volatile FUNCPTR)Micro_cont_PioTst},
                    {31, (volatile FUNCPTR)Yfifo_PioTst},
                    {32, (volatile FUNCPTR)Mapper_PioTst},
                    {33, (volatile FUNCPTR)Zfifo_PioTst},
                    {34, (volatile FUNCPTR)VBus_PioTst},
                    {35, (volatile FUNCPTR)Afifo_PioTst},
                    {36, (volatile FUNCPTR)Rcv_Micro_cont_PioTst},
                    {37, (volatile FUNCPTR)Dmap_PioTst},
                    {38, (volatile FUNCPTR)Bfifo_PioTst},
                    {39, (volatile FUNCPTR)Dfrm_Micro_cont_PioTst},
                    {40, (volatile FUNCPTR)Cfifo_PioTst},
                    {41, (volatile FUNCPTR)RNI_PioTst},
                    {42, (volatile FUNCPTR)Pio_Interface_PioTst},
                    {43, (volatile FUNCPTR)bmeExtTest},
                    {44, (volatile FUNCPTR)bmeIDTest}
};


void printMainLogo (void)
{

 	printf("\r\n####################################################");
 	printf("\r\n#                                                  #");
 	printf("\r\n#        Hostless CPE Diagnostics Menu             #");
 	printf("\r\n#                                                  #");
 	printf("\r\n####################################################");
 	printf("\r\n");
 	printf("\r\n       1. Hardware Diagnostics  ");
 	printf("\r\n       2. Connection Diagnostics  ");
 	printf("\r\n       3. Edit Configuration Table  ");
 	printf("\r\n");
 	printf("\r\n       Enter Selection:  ");

}
/***********************************************************************/
void diag_start()
{	

#ifdef BME_CPE_20
    tdiagConfTbl *cTbl;
    int dt, n1, n2;

    cTbl = (tdiagConfTbl *)&confTbl;
    getDiagFlashInfo((char *)cTbl, CONFIG_INFO_OFFSET, sizeof(tdiagConfTbl)); 

    n2 = 0;
    while (!n2) {
        printMainLogo();
        scanf ("%i",&n1);

        if (n1 == 1) {            /*hardware diagnostics*/
            n2 = 1;
        }                           /*connection diagnostics*/
        else if (n1 == 2) {
            n2 = 2;
        }                           /*edit configuration table*/
        else if (n1 == 3) {
            n1 = n2 = 0;                   //stay the loop
            printf("\r\n       1. Ikanos Intel PHY Configuration");
            printf("\r\n       2. Ikanos Boardcom PHY Configuration");
            printf("\r\n       3. Ikanos Divicom Configration ");
            printf("\r\n       4. Ikanos Altima Configration ");
            printf("\r\n       5. Custom Configration ");
            printf("\r\n       6. Display Configration ");
            printf("\r\n");
            printf("\r\n       Enter Selection:  ");
            
            scanf ("%i",&n1);
            switch (n1) {
            case 1:
                cTbl->PhyBaseAddr = INTEL_PTMF_ENET_PHY_BASE_ADDR;
                cTbl->PhyMdioTestAddr = INTEL_PTMF_ENET_PHY_TEST_ADDR;
                cTbl->SDRAMBuswidth = 0;
                cTbl->SDRAMClkSpeed = 0;
                break;
            case 2:
                cTbl->PhyBaseAddr = BC_PTMF_ENET_PHY_BASE_ADDR;
                cTbl->PhyMdioTestAddr = BC_PTMF_ENET_PHY_TEST_ADDR;
                cTbl->SDRAMBuswidth = 1;
                cTbl->SDRAMClkSpeed = 0;
                break;
            case 3:
                cTbl->PhyBaseAddr = DM_PTMF_ENET_PHY_BASE_ADDR;
                cTbl->PhyMdioTestAddr = DM_PTMF_ENET_PHY_TEST_ADDR;
                cTbl->SDRAMBuswidth = 1;
                cTbl->SDRAMClkSpeed = 0;
                break;
            case 4:
                cTbl->PhyBaseAddr = AT_PTMF_ENET_PHY_BASE_ADDR;
                cTbl->PhyMdioTestAddr = AT_PTMF_ENET_PHY_TEST_ADDR;
                cTbl->SDRAMBuswidth = 1;
                cTbl->SDRAMClkSpeed = 0;
                break;
            case 5:
                printf("\r\n       Enter PHY base address (HEX): ");
                scanf ("%x",&dt);
                cTbl->PhyBaseAddr = (U8)dt;

                printf("       Enter PHY MDIO test register address (HEX): ");
                scanf ("%x",&dt);
                cTbl->PhyMdioTestAddr = (U8)dt;

                printf ("       Enter memory bus width(1=16bit/0=32bit): ");
                scanf ("%i",&dt);
                cTbl->SDRAMBuswidth = dt;

                printf ("       Enter memory clock speed(0=one/1=half): ");
                scanf ("%i",&dt);
                cTbl->SDRAMClkSpeed = dt;
                break;
            case 6:
                printf("\r\n       PHY base address: 0x%x\n", cTbl->PhyBaseAddr);
                printf("       PHY MDIO test address: 0x%x\n", cTbl->PhyMdioTestAddr);
                printf("       SDRAM Bus width:  %x\n", (unsigned int)cTbl->SDRAMBuswidth);
                printf("       SDRAM Clock Speed: %x\n", (unsigned int)cTbl->SDRAMClkSpeed);
                break;
            default:
                cTbl->PhyBaseAddr = INTEL_PTMF_ENET_PHY_BASE_ADDR;
                cTbl->PhyMdioTestAddr = INTEL_PTMF_ENET_PHY_TEST_ADDR;
                cTbl->SDRAMBuswidth = 0;
                cTbl->SDRAMClkSpeed = 0;
                break;
            }
            saveDiagFlashInfo((char *)&cTbl->PhyBaseAddr, CONFIG_INFO_OFFSET, sizeof(tdiagConfTbl));

        }
    }

    if (n2 == 2) {                    //connection diags
        return;
    }
#endif
#ifdef P2CO
    HostEpldInterfaceInit();
	HostAfeAddrSet(0xe4);
#endif //P2CO
#if defined(P2CPE) && defined(INI_SUPPORT)
  HostIniFpgaProgm();
#endif //P2CPE


    if(iposSysInit() == IPOS_SUCCESS) {
        printf("\nipos system initialized \n");
        // Initialize BSP related components
        sysBMEInterfaceInit();
        hostIntInit();
        sysConfigPortPinsForBMEInterface();

        /*For now, one BME in system */
        InitBmeStruct(&Bme[1],1);
    }
    
    diag_menu();

}
/***********************************************************************/
void printDiagLogo (void)
{
char diagVer[30];                                               

    strcpy (diagVer,DIAG_VERSION_STR);
 	printf("\r\n\n\nDiagnostic Program    Version %-18s", diagVer);
}
/***********************************************************************/
void printHelp (void)
{
#ifdef VTU_R
 	printf("\r\n\n\nCPE Module Diagnostic Help Menu\r\n");
#else
  printf("\r\n\n\nCO Module Diagnostic Help Menu\r\n");
#endif

 	printf("\r\ndt1.................BME HIC Interface Test");
 	printf("\r\ndt2.................BME CLKOUT Test");
 	printf("\r\ndt3.................BME RESETOUT Test");
 	printf("\r\ndt4.................BME V-bus/AFE/IFE Register Test");
 	printf("\r\ndt5.................BME External Memory Test");
 	printf("\r\ndt6.................BME to Host Data Path Test");
 	printf("\r\ndt7.................AFE_LS Digital Loopback Test");
 	printf("\r\ndt8.................TX Single Tone Test");
 	printf("\r\ndt9.................Missing Tone Test");
    printf("\r\ndta.................Capture Tone Test");
 	printf("\r\ndtb.................Utopia Loopback Test");
    printf("\r\ndtc.................PTMF -> Utopia Loopback Test");
#ifdef BME_CPE_20
#ifdef IKANOS_BOARD
    printf("\r\ndtd.................GPIO/LED Test");
 	printf("\r\ndte.................Register Test");
#endif
 	printf("\r\ndtf.................Master MDIO Test");
#ifdef IKANOS_BOARD
 	printf("\r\ndtg.................Slave MDIO Test");
#endif
 	printf("\r\ndth.................Serial Port Test");
#endif /* BME_CPE_20 */
    printf("\r\ndti.................Host Interrupt Test");

#if defined(BME_CPE_20) && defined (INI_SUPPORT)
    printf("\r\ndtj.................Ethernet External Loopback Test");
    printf("\r\ndtk.................Ethernet PHY Loopback Test");
    
#endif
 	printf("\r\ndtA.................All Automatic Tests Above");
#ifdef DIAG_INTERNAL
 	printf("\r\ndtD.................Debug Menu");
#endif
#ifdef BME_CPE_20
    printf("\r\ndtL.................Program Flash");
 	printf("\r\ndtU.................Upload Flash Image");
#endif
}


void debugHelp (void)
{                                                        
 	printf("\r\n1.................XFIFO FAST MEM TEST");
 	printf("\r\n2.................XFIFO INTERLEAVER TEST");
 	printf("\r\n3.................EXTERNAL DMA TEST");
 	printf("\r\n4.................FMC ICACHE MEM TEST");
 	printf("\r\n5.................FMC DCACHE MEM TEST");
 	printf("\r\n6.................YFIFO FAST MEM TEST");
 	printf("\r\n7.................MAPPER TONE MEM TEST");
 	printf("\r\n8.................MAPPER GAIN MEM TEST");
 	printf("\r\n9.................ZFIFO MEM TEST");
 	printf("\r\n10................CFIFO MEM TEST");
 	printf("\r\n11................DMC ICACHE MEM TEST");
 	printf("\r\n12................DMC DCACHE MEM TEST");
 	printf("\r\n13................SMC SNR MEM TEST");
 	printf("\r\n14................SMC TONE MEM TEST");
 	printf("\r\n15................SMC THRESHOLD MEM TEST");
 	printf("\r\n16................BFIFO INTERLEAVER MEM TEST");
 	printf("\r\n17................BFIFO FAST MEM TEST");
 	printf("\r\n18................BFIFO DEMAPPER MEM TEST");
 	printf("\r\n19................DEMAPPER (ROT) MEM TEST");
 	printf("\r\n20................FEQ MEM TEST");
 	printf("\r\n21................TEQ MEM TEST");
 	printf("\r\n22................RMC ICACHE MEM TEST");
 	printf("\r\n23................RMC DCACHE MEM TEST");
 	printf("\r\n24................AFIFO FAST MEM TEST");
 	printf("\r\n25................BME DMA TEST");
 	printf("\r\n26................VBUS0/AFE TEST");
 	printf("\r\n27................VBUS1/AFE TEST");
 	printf("\r\n28................TNI PIO TEST");
 	printf("\r\n29................XFIFO PIO TEST");
 	printf("\r\n30................MICRO/CONT PIO TEST");
 	printf("\r\n31................YFIFO PIO TEST");
 	printf("\r\n32................MAPPER PIO TEST");
 	printf("\r\n33................ZFIFO PIO TEST");
 	printf("\r\n34................VBUS PIO TEST");
 	printf("\r\n35................AFIFO PIO TEST");
 	printf("\r\n36................RCV MICROCONTROLLER TEST");
 	printf("\r\n37................DEMAPPER PIO TEST");
 	printf("\r\n38................BFIFO PIO TEST");
 	printf("\r\n39................DEFRAMER MICRO PIO TEST");
 	printf("\r\n40................CFIFO PIO TEST");
 	printf("\r\n41................RNI PIO TEST");
 	printf("\r\n42................INTERFACE PIO TEST");
 	printf("\r\n43................EXTERNAL SRAM TEST");
 	printf("\r\n44................BME ID TEST");
 	printf("\r\n45................Select all above tests");
}

/*******************************************************************************
*
* skipSpace - advance pointer past white space
*
* Increments the string pointer passed as a parameter to the next
* non-white-space character in the string.
*/

LOCAL void skipSpace 
    (
    FAST char **strptr	/* pointer to pointer to string */
    )
    {
    while (isspace ((UINT) ** strptr))
	++ * strptr;
    }
/*******************************************************************************
*
* getPara - convert a string to hex
*
* convert a string to hex value in byte, half word or word.
*/

LOCAL int getPara 
    (
    char 	*strptr,	/* pointer to pointer to string */
    BOOL	defaultHex,	/* TRUE = arg is hex (even w/o 0x) */
    BOOL	optional	/* TRUE = ok if end of line */
    )
    {
    int  pValue = 0;		/* ptr where to return value */
    int  value = 0;		/* ptr where to return value */

    while (*strptr != EOS) {
		if(*strptr != ' ') {
			if (*strptr > 0x40)	{				/*from a - f*/
				value = (int)((*strptr & 0xdf) - 0x37); 
				}
			else { 
				value = (int)*strptr & 0xf; 
				}
			pValue = pValue * 16 + value;
			++strptr;
			}
		else
			break;
		}
	cmdPtr = strptr;	/*save the pointer*/
	return(pValue);
    }
/***********************************************************************/
void diag_menu(void)
{
int ch1, adr, adr1=0, word3;
char loop=1, line [MAX_LINE], *pLine; 

#ifdef BME_CPE_20
tdiagConfTbl *cTbl;
volatile int dt;
char dl=0;
char line2[16], line3[16];
int ch, *ptr, len = 0;
#endif

#ifdef DIAG_INTERNAL
volatile FUNCPTR funcEntry;
#endif
//int loopbackcount;
#ifdef INI_SUPPORT
int cellCount;
#endif


#ifdef BME_CPE_20
    cTbl = (tdiagConfTbl *)&confTbl;
    InitSdram (cTbl->SDRAMBuswidth,cTbl->SDRAMClkSpeed);
#endif
	
    if(bootloadInfo() == 0) {		/* bootloader is compatiable */
        if (indexFlagGet() == 1 && *DOWNLOAD_1_ADRS == 0) {
            imageFlagSet(1);
        }
        else if (indexFlagGet() == 2 && *DOWNLOAD_2_ADRS == 0) {
            imageFlagSet(2);
        }
    }
    while (loop) {
        iposDelay();
        if (capTest) {
            capTest = 0;
        }
        else {
            printDiagLogo();
            printHelp();  
            printf("\r\n>");
        }

	    /*reset the string*/
	    for (ch1=0; ch1 < MAX_LINE; ch1++) {			
		    line[ch1] = ' ';
		    }
        /*read the command line*/

        gets(line);
        pLine = line;
        if (*pLine == 'd' && *(pLine+1) == 't') {
            if (*(pLine+2) == '0') {				/*general test*/
                pLine = pLine + 3;
                skipSpace (&pLine);
                adr	= getPara (pLine, HEX, OPT);  	/* first parameter */

                pLine = cmdPtr;
                skipSpace (&pLine);
                adr1= getPara (pLine, HEX, OPT);  	/* second parameter */

                pLine = cmdPtr;
                skipSpace (&pLine);
                word3 = getPara (pLine, HEX, OPT);  /* third parameter */

            }
            else if (*(pLine+2) == '1') {			
	
			/*** BME HIC Interface test***/

#ifdef BME_CPE_20
                InitSdram (cTbl->SDRAMBuswidth,cTbl->SDRAMClkSpeed);
#endif
                bmeHicTest();
            }
            else if (*(pLine+2) == '2') {			
			/*** DACOUT test ***/
                dacOutTest();
            }
            else if (*(pLine+2) == '3') {			
			
			/*** RESETOUT test****/
                resetTest();
            }
            else if (*(pLine+2) == '4') {			
				/* V-bus 0 test*/

                printf ("\nEnter vbus number: ");
                scanf ("%i",&num1);

                printf ("\nEnter AFE address: ");
                scanf ("%i",&num2);
                v_busTest(num1, num2<<8);
            }
            else if (*(pLine+2) == '5') { 
                /* BME external test*/
#ifdef BME_CPE_20
                printf ("\nEnter bus width(1=16bit/0=32bit): ");
                scanf ("%i",&num1);

                printf ("\nEnter Clock Speed(0=one/1=half): ");
                scanf ("%i",&num2);
                InitSdram (num1,num2);
#endif
                bmeExtTest();
            }
            else if (*(pLine+2) == '6') {			
                /* Data Path test*/
                dataPathTest();                
            }

            else if (*(pLine+2) == '7') { 
                num1 = num2 = 0;
                afe2_lb(num2,1);
                printf("Test Done!");
            }
            else if (*(pLine+2) == '8') {		  /* TX Single Tone Test */
                tune_test = 1;
                co_tx(0,1,0);
                printf("Test Done!");
            }
            else if (*(pLine+2) == '9') {		  /* MISSSING Tone Test */
                tune_test = 2;
                co_tx(0,1,0);
                printf("Test Done!");
            }
            else if (*(pLine+2) == 'a')           /*tone capture test*/
            { 
                capTest = 1;
                // capture data
                afe2_recv(0, 1);
            }

            else if (*(pLine+2) == 'b') 
            {			
#ifdef BME_CPE_20
                printf("This test is not available!");
#else
                int loopbackcount;			          /* utopia test*/
                printf("\n X-FIFO to C-FIFO loopback!");
                printf ("\nEnter number of loopback count: ");
                scanf ("%i",&loopbackcount);
                x2cloop(loopbackcount);
                printf("Test Done!");
#endif
            }
            else if (*(pLine+2) == 'c') 
            {			
#ifdef INI_SUPPORT 
                int loopbackcount;
			          /* PTMF loopback through utopia interface on the BME */
                printf("\n PTMF loopback through X->C FIFO on BME!");
                printf ("\nEnter number of loopback count: ");
                scanf ("%i",&loopbackcount);
                printf ("\nEnter number of cell count: ");
                scanf ("%i",&cellCount);
                printf("\n %d - %d ",loopbackcount, cellCount);
                cloop(loopbackcount, cellCount);
                printf("Test Done!");
#endif
            }
            else if (*(pLine+2) == 'i') 
            {
                writeControlPort((char *)BME_BASE_ADDR,DMA_CTRL_PORT,0x33);
                testPIOWrite_diag(1,PIO_MCR(0),BIT31);
                printf("Test Done!");
            }
            else if (*(pLine+2) == 'j') 
            {
#ifdef INI_SUPPORT
                cx_loop(4);
#endif
            }
#ifdef BME_CPE_20
            else if (*(pLine+2) == 'k') 
            {
#ifdef INI_SUPPORT
                dbgEnetPhyWrite(cTbl->PhyBaseAddr,0,0x6100);
                cx_loop(4);
                dbgEnetPhyWrite(cTbl->PhyBaseAddr,0,0x3100);
#endif
            }
            else if (*(pLine+2) == 'L') 
            {
                if (!dl) {
                    printf("\r\nStarting downloading BME image....\r\n ");
                    ethDl(0,0);
                    dl = 1;
                }
                printf("\r\nPlease enable the flash in target board.\r\n");
                while ((dt=testPIORead_diag(1, FLASH_PROGRAM_ADDR)) != FLASH_PROGRAM_FLAG1);
                adr=0;
                printf("\r\nEnter Host IP address: ");
                scanf ("%s",line2);
                printf("\r\nEnter file name: ");
                scanf ("%s",line3);
                printf("\r\nStarting Downloading flash image by TFTP....\r\n ");
                remoteDownLoadImage(1, line2, line3);   
                if (tmpFileSize) {
                    printf("\r\nWhich image to download?");
                    printf("\r\n1. Boot code");
                    printf("\r\n2. App code 1");
                    printf("\r\n3. App code 2");
                    printf("\r\n4. Flash code");
                    printf("\r\n\nEnter Selection: ");
                    scanf ("%x", &num1);
                    if (num1 == 1) {
                        adr = 40;
                        adr1 = 0;
                        len = tmpFileSize;
                    }
                    else if (num1 == 2) {
                        adr = 0;
                        adr1 = 0x40000;
                        ptr = (int *)&tmpBuf[36];
                        len = *ptr + 40;
                        printf("\r\nCalculated checksum=%x", calCS((int)&tmpBuf[0]+40, len));
                    }
                    else if (num1 == 3) {
                        adr = 0;
                        adr1 = 0xe0000;
                        ptr = (int *)&tmpBuf[36];
                        len = *ptr + 40;
                        printf("\r\nCalculated checksum=%x", calCS((int)&tmpBuf[0]+40, len));
                    }
                    else if (num1 == 4) {
                        adr = 0;
                        adr1 = 0;
                        len = HL_FLASH_SECT_SIZE;
                        printf("\r\nCalculated checksum=%x", calCS((int)&tmpBuf[0], HL_FLASH_SECT_SIZE));
                    }
                    testPIOWrite_diag(1, FLASH_PROGRAM_ADDR+8, len);            /* write length */

                    if (len) {
                        word3 = testPIORead_diag(1, FLASH_PROGRAM_ADDR+4);    /* read buffer address */
                        printf("\r\nStarting Writing flash image to BME memory....\r\n ");
                        ch1 = (int)&tmpBuf[0];
                        for (ch1+=adr, ch=len; ch > 0x100; ch-=0x100,word3+=0x100, ch1+=0x100){  
                            doDmaWrite(word3, (BYTE *)ch1, 0x100, 0);
                        }
                        doDmaWrite(word3, (BYTE *)ch1, ch, 0);
                    }
                    testPIOWrite_diag(1, FLASH_PROGRAM_ADDR+0xc, adr1);             /* offset to flash */
                    testPIOWrite_diag(1, FLASH_PROGRAM_ADDR, FLASH_PROGRAM_FLAG2);    /* ready to program */
                }
                printf("\r\nDone.\r\n ");

            }

            else if (*(pLine+2) == 'U') {
                printf("\r\nEnter Host IP address: ");
                scanf ("%s",line2);
                printf("\r\nEnter file name: ");
                scanf ("%s",line3);
                printf("\r\nStarting Downloading flash image by TFTP....\r\n ");
                lftftp(HL_FLASH_SECT_SIZE, line2, line3);
            }
#ifdef IKANOS_BOARD
            else if (*(pLine+2) == 'd') { /* GPIO Test */
                testGpio ();
            }
            else if (*(pLine+2) == 'e') { /* BME_CPE_20 Register Test */
                AztecRegReadWrite ();
            }
#endif
            else if (*(pLine+2) == 'f') { /* Master MDIO Test */
                testMmdio (cTbl->PhyBaseAddr & 0x1f, cTbl->PhyMdioTestAddr);
            }
#ifdef IKANOS_BOARD
            else if (*(pLine+2) == 'g') { /* Slave MDIO Test */
                testSmdio ();
            }
#endif
            else if (*(pLine+2) == 'h') { /* UART Test */
                printf ("\nEnter UART #(1=/0): ");
                scanf ("%i",&num1);
                LoopbackUart(num1);
            }
#endif
            else if (*(pLine+2) == 'A') {			                
                bmeHicTest();
                dacOutTest() ;
                resetTest(); 
                v_busTest(0, 0<<8);
#ifdef VTU_O
                v_busTest(0, 1<<8);
#endif
#ifdef BME_CPE_20
                InitSdram (cTbl->SDRAMBuswidth,cTbl->SDRAMClkSpeed);
#endif
                bmeExtTest();
                dataPathTest();
                num1 = num2 = 0;
                afe2_lb(num2,1);
#if 0
                testGpio ();
                AztecRegReadWrite ();
                testMmdio ();
                testSmdio ();
#endif
                printf("Test Done!");
            }

#ifdef DIAG_INTERNAL
            else if (*(pLine+2) == 'D') {		  /* Debugging Test */   
                lp = 1;
#ifdef BME_CPE_20
                InitSdram (cTbl->SDRAMBuswidth,cTbl->SDRAMClkSpeed);
#endif
                while (lp) {
                    printf("\n1. Add a test into the list\n");
                    printf("2. Clear your list\n");
                    printf("3. Run tests from the list\n");
                    printf("4. Exit debug menu\n");
                    printf("Your choice: ");
                    scanf ("%s",line2);
                    if (line2[0] == '1') {
                        debugHelp();
                        printf("\nEnter test number:  ");
                        scanf ("%i",&num2);
                        if (num2 < 33) {
                            tbits1 |= 1<<(num2-1);
                        }
                        else if (num2 > MAN_DIAGS_NUM2 && num2 < MAN_DIAGS_NUM1) {
                            tbits2 |= 1<<(num2-33);
                        }
                        else if (num2 == MAN_DIAGS_NUM1) {
#ifdef VTU_O
                            tbits1 = -1;
#else
                            tbits1 = ~_bit26;
#endif
                            tbits2 = 0xfff;
                        }
                        else
                            printf("\nInvalid Number.\n");
                    }                                         
                    else if (line2[0] == '2') {
                        tbits1 = tbits2 = 0;
                        printf("The list is clear.\n");
                    }
                    else if (line2[0] == '3') {
                        fnum = 0;
                        printf("Single or Continuous Test(1=single/2=continuous)? ");
                        scanf ("%i",&num1);
                        if (num1 == 2) {
                           word3 = 0xfffff;
                        }
                        else if (num1 == 1) {
                           word3 = 1;
                        }
                        else {
                           word3 = 0;
                           printf("\nInvalid Number.\n");
                        }
                        for (ch=0; ch < word3; ch++){  
                            for (ch1=0; ch1 < MAN_DIAGS_NUM2; ch1++){  
                                if (tbits1 & 1<<ch1) {
                                    funcEntry = ft[ch1].func;
                                    if (funcEntry () != IPOS_SUCCESS) {
                                        ch = word3;
                                        ch1 = MAN_DIAGS_NUM2;
                                    }
                                }
                            }
                            if (word3) {
                                for (ch1=0; ch1 < MAN_DIAGS_NUM1-MAN_DIAGS_NUM2; ch1++){  
                                    if (tbits2 & 1<<ch1) {
                                        funcEntry = ft[ch1+MAN_DIAGS_NUM2].func;
                                        if (funcEntry () != IPOS_SUCCESS) {
                                            ch = word3;
                                            ch1 = MAN_DIAGS_NUM1-MAN_DIAGS_NUM2;
                                        }
                                    }
                                }
                            }
                        }
                        printf("\n %d test(s) failed.\n", fnum);
                        printf("Test Done.\n");
                    }
                    else if (line2[0] == '4')
                        lp = 0;
                }
            }
#endif /* DIAG_INTERNAL */
            else
                printf("\r\nUnknown Command.");	   
        }
	    else if (*pLine == 'x') 
		    loop = 0;								
	    else if (*pLine == '?') 
		    printHelp();								
	    else
		    printf(" ");	   
	
    }                 
}

/***********************************************************************/
int testPIORead_diag (int BmeNum, unsigned long addr)
{
    ipos_api_bme	*pApiBme;
	UINT32 data = 0x5a5a;	
	
    BmeStruct[BmeNum].BMEbaseAddr = BME_BASE_ADDR;	
	BmeStruct[BmeNum].BmeNum=BmeNum;
    
    pApiBme=(ipos_api_bme *) iposGetBmeHandle(BmeNum);
  
    if(iposSemTake(pApiBme->semBme, IPOS_WAIT_FOREVER) == OS_SUCCESS)
    {
      bmePioRead (&BmeStruct[BmeNum],  addr, (BYTE *)&data);
      iposSemGive(pApiBme->semBme);			/* read BME successful, release the bme Lock */

    }
    return data;


}


/***********************************************************************/
void testPIOWrite_diag (int BmeNum, unsigned long addr, unsigned long data)
{
    ipos_api_bme	*pApiBme;

    BmeStruct[BmeNum].BMEbaseAddr = BME_BASE_ADDR;	
	BmeStruct[BmeNum].BmeNum=BmeNum;
    
    pApiBme=(ipos_api_bme *) iposGetBmeHandle(BmeNum);
    if(iposSemTake(pApiBme->semBme, IPOS_WAIT_FOREVER) == OS_SUCCESS)
    {
      bmePioWrite (&BmeStruct[BmeNum], addr, (BYTE *)&data);
      iposSemGive(pApiBme->semBme);			/* read BME successful, release the bme Lock */

    }

}

/***********************************************************************/
int bmeIDTest (void)
{
	UINT32 data = 0; 
    data = testPIORead_diag(1, PIO_BID(0));
    if (data == BME_CHIP_ID){
        printf("\nBME ID Test passed.\n");
        return IPOS_SUCCESS;
    }
    else{
        printf("\nBME ID test failed.\n");
        fnum++;
        return IPOS_ERROR;
    }

    printf("Test Done!\n");
}
 
/***********************************************************************/
void bmeHicTest(void)
{
int ch1, adr, word3, word4, good;

    fnum = 0;
    /*** Test 1 ***/
    
    good = TRUE;
	printf ("\nTest 1: Writing 0x0 to 0xe0018018 100 times");
    for (ch1=0, adr=VIF_SCCR; ch1 < 100; ch1++) {             /*BME regiser test*/
        word3 = 0;
        testPIOWrite_diag(1, adr, word3);
        word4 = testPIORead_diag(1, adr);
        if (word3 != word4) {
            printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", word3, word4, adr);
            good = FALSE;
            break;
        }
    }
    if (good == TRUE) 
        printf("\nTest 1 passed.\n");
    else {
        printf("\nTest 1 failed. Error code = %x\n",DIAGS_ERROR_BME_REG_STRESS_0);
        fnum++;
    }

    /*** Test 2 ***/
    good = TRUE;
	printf ("\nTest 2: Writing 0xffffffff to 0xe0018018 100 times");
    for (ch1=0, adr=VIF_SCCR; ch1 < 100; ch1++) {
        word3 = _32b;
        testPIOWrite_diag(1, adr, word3);
        word4 = testPIORead_diag(1, adr);
        if (word3 != word4) {
            printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", word3, word4, adr);
            good = FALSE;
            break;
        }
    }
    if (good == TRUE) 
        printf("\nTest 2 passed.\n");
    else {
        printf("\nTest 2 failed. Error code = %x\n", DIAGS_ERROR_BME_REG_STRESS_F);
        fnum++;
    }

	/*** Test 3 ***/
	printf ("\nTest 3: Writing 0x55aa55aa to 0xe0018018 100 times");
    good = TRUE;
    for (ch1=0, adr=VIF_SCCR; ch1 < 100; ch1++) {
        word3 = 0x55aa55aa;
        testPIOWrite_diag(1, adr, word3);
        word4 = testPIORead_diag(1, adr);
        if (word3 != word4) {
            printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", word3, word4, adr);
            good = FALSE;
            break;
            }
        }
    if (good == TRUE) 
        printf("\nTest 3 passed.\n");
    else {
        printf("\nTest 3 failed. Error code = %x\n", DIAGS_ERROR_BME_REG_STRESS_5A);
        fnum++;
    }

    /*** Test 4 ***/
    printf ("\nTest 4: Writing 0xaa55aa55 to 0xe0018018 100 times");
    good = TRUE;
    for (ch1=0, adr=VIF_SCCR; ch1 < 100; ch1++) {
        word3 = 0xaa55aa55;
        testPIOWrite_diag(1, adr, word3);
        word4 = testPIORead_diag(1, adr);
        if (word3 != word4) {
            printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", word3, word4, adr);
            good = FALSE;
            break;
            }
        }
                
    if (good == TRUE) 
        printf("\nTest 4 passed.\n");
    else {
        printf("\nTest 4 failed. Error code = %x\n", DIAGS_ERROR_BME_REG_STRESS_5A);
        fnum++;
    }

    /*** BME memory test ***/
	printf ("\nWriting 0->100 to 0x%x->0x%x + 0x100", CODE_MEMORY_ADR, CODE_MEMORY_ADR);
    good = TRUE;
    for (ch1=0, adr=CODE_MEMORY_ADR; ch1 < 100; ch1++, adr+=4) {         /*BME memory test*/
        testPIOWrite_diag(1, adr, ch1);
        }
    for (ch1=0, adr=CODE_MEMORY_ADR; ch1 < 100; ch1++, adr+=4) {
        word4 = testPIORead_diag(1, adr);
        if (ch1 != word4) {
            printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", ch1, word4, adr);
            good = FALSE;
            break;
            }
        }
    if (good == TRUE) 
        printf("\nBME memory test passed.\n");
    else {
        printf("\nBME memory test failed. Error code = %x\n", DIAGS_ERROR_BME_REG_STRESS_A5);
        fnum++;
    }

    /*** Basic BME DMA test<---Need to eventually port this ***/
    printf ("############################################################\n");
    printf ("\t\t\tStarting BASIC DMA TEST\n");
    printf ("############################################################\n");
    bme_dma_test();    

	/*** PIO reg read/write test ***/
	printf ("############################################################\n");
    printf ("Starting ref_host_pio_rd_wr\n");
	printf ("############################################################\n");
    ref_host_pio_rd_wr(0,0);

    printf("\n %d test(s) failed.\n", fnum);
    printf("Test Done.\n");
        				    
}

/***********************************************************************/
void dacOutTest (void)
{
    int i,j;

    /*** DACOUT test ***/
	printf("\nSetting CKM_CCR to 0x2001000\n");
    testPIOWrite_diag(1, CKM_CCR(0), 0x2001000);
	printf ("Measure CLK_NTR output signal now\n");
    printf("CLK_NTR frequency should be 35.3230 Mhz\n");
    for (i=0; i<0x7ffffff; i++) {
        j++;
    }

	printf("\nSetting CKM_CCR to 0x2002000\n");
    testPIOWrite_diag(1, CKM_CCR(0), 0x2002000);
    printf ("Measure CLK_NTR output signal now\n");
    printf("CLK_NTR frequency should be 35.3281 Mhz\n");
    for (i=0; i<0x7ffffff; i++) {
        j++;
    }

	printf("\nSetting CKM_CCR to 0x2000ffe\n");
    testPIOWrite_diag(1, CKM_CCR(0), 0x2000ffe);
	printf ("Measure CLK_NTR output signal now\n");
    printf("CLK_NTR frequency should be 35.3340 Mhz\n");
    for (i=0; i<0x7ffffff; i++) {
        j++;
    }

	printf("\nReseting CKM_CCR to 0x2000400\n");
    testPIOWrite_diag(1, CKM_CCR(0), 0x2000400);
    printf("Test Done.\n");

}
                               
/***********************************************************************/
void resetTest(void)
{
int word3, word4;

    /*For now, one BME in system */
    printf ("\nPerforming Hardware Reset on BME \n");            
    bmeHwResetApi(1);
    word3 = testPIORead_diag(1,VIF_ICCR);
    word4 = testPIORead_diag(1,VIF_SCCR);
    printf("\nVIF_ICCR=%x, VIF_SCCR=%x\n", word3, word4);
    if (word3 == 0x380038 && word4 == 0x1f1f0707)
        printf("\nBME HW reset successful!\n");
    else
        printf("\nBME HW reset not successful!\n");

    printf("Test Done!\n");
}

/***********************************************************************/
void v_busTest(int vbusnum, int afenum)
{
    /* V-bus test*/

    if (vbusnum==0)
    {
    printf ("\nReading VBUS 0 Registers test");
    ref_afe_vbus0_pio_rw (afenum);
    }
    else if (vbusnum==1)
    {
    printf ("\nReading VBUS 1 Registers test");
    ref_afe_vbus1_pio_rw (afenum);
    }

    printf("Test Done!\n");
}

/***********************************************************************/
int bmeExtTest(void)
{
int ch1, adr, word4, good;

    /* BME external test*/
    good = TRUE;
    printf("\r\nAddress Ripple Test...\n");
    for (ch1=0, adr=CODE_MEMORY_ADR; ch1 < 100; ch1++, adr+=4) {         /*address ripple test*/
        testPIOWrite_diag(1, adr, ch1);
    }
    for (ch1=0, adr=CODE_MEMORY_ADR; ch1 < 100; ch1++, adr+=4) {
        word4 = testPIORead_diag(1, adr);
        if (ch1 != word4) {
            printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", ch1, word4, adr);
            good = FALSE;
            break;
        }
    }

    printf("\r\nWalk Zero Test...\n");
    for (ch1=1, adr=CODE_MEMORY_ADR+0x100; adr<CODE_MEMORY_ADR+0x200; adr+=4) {         /*Walk Zero test*/
        testPIOWrite_diag(1, adr, (_32b - ch1));
        if (ch1 >= BIT31)
            ch1 = 1;
        else 
            ch1 *=2;
    }

    for (ch1=1, adr=CODE_MEMORY_ADR+0x100; adr<CODE_MEMORY_ADR+0x200; adr+=4) {
        word4 = testPIORead_diag(1, adr);
        if (word4 != (_32b - ch1)) {
            printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", (_32b - ch1), word4, adr);
            good = FALSE;
            break;
        }
        else if (ch1 >= BIT31)
            ch1 = 1;
        else 
            ch1 *= 2;
    }

    printf("\r\nWalk One Test...\n");
    for (ch1=1, adr=CODE_MEMORY_ADR+0x300; adr<CODE_MEMORY_ADR+0x400; adr+=4) {         /*Walk One test*/
        testPIOWrite_diag(1, adr, ch1);
        if (ch1 >= BIT31)
            ch1 = 1;
        else 
            ch1 *=2;
    }
    for (ch1=1, adr=CODE_MEMORY_ADR+0x300; adr<CODE_MEMORY_ADR+0x400; adr+=4) {
        word4 = testPIORead_diag(1, adr);
        if (word4 != ch1) {
            printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", ch1, word4, adr);
            good = FALSE;
            break;
        }
        else if (ch1 >= BIT31)
            ch1 = 1;
        else 
            ch1 *= 2;
    }

    if (good == TRUE) 
        printf("\nBME External Test passed.\n");
    else
        printf("\nBME register test failed.\n");

    printf("Test Done!\n");
    
    if (good == TRUE) 
        return(IPOS_SUCCESS);
    else {
        fnum++;
        return(IPOS_ERROR);
    }
}

/***********************************************************************/
void dataPathTest(void)
{
int ch, ch1, adr, good, *buf;

    /* Data Path test*/
    good = TRUE;
    for (ch=0; ch <100; ch++) {          /*looping*/
        for (ch1=0, adr=CODE_MEMORY_ADR, buf=(int *)&dtArray[0]; adr<CODE_MEMORY_ADR+0x100; ch1++, adr+=4, buf++) { /*address ripple test*/
            testPIOWrite_diag(1, adr, ch1);
            *buf =  ch1;                            /*initial buffer*/
        }

        for (ch1=1, adr=CODE_MEMORY_ADR+0x100, buf=(int *)&dtArray[0x100]; adr<CODE_MEMORY_ADR+0x200; adr+=4, buf++) {      /*Walk Zero test*/
            testPIOWrite_diag(1, adr, (_32b - ch1));
            *buf =  (_32b - ch1);                                  /*initial buffer*/
            if (ch1 >= BIT31)
                ch1 = 1;
            else 
                ch1 *=2;
        }
        for (ch1=1, adr=CODE_MEMORY_ADR+0x200, buf=(int *)&dtArray[0x200]; adr<CODE_MEMORY_ADR+0x300; adr+=4, buf++) {      /*Walk One test*/
            testPIOWrite_diag(1, adr, ch1);
            *buf =  ch1;                                  /*initial buffer*/
            if (ch1 >= BIT31)
                ch1 = 1;
            else 
                ch1 *=2;
        }

        for (adr=CODE_MEMORY_ADR, buf=(int *)&dtArray[0x300]; adr<CODE_MEMORY_ADR; adr+=4, buf++) {             /*read the data back*/
            *buf = testPIORead_diag(1, adr);
        }

        for (adr=CODE_MEMORY_ADR, buf=(int *)&dtArray[0x300]; adr<CODE_MEMORY_ADR+0x300; adr+=4, buf++) {      /*verify the data*/
            if (*buf != *(buf + 0x300)) {
                printf("ERROR! Write data=%x, Read data=%x, Address=%x\n", 
                       *buf, *(buf + 0x300), (int)buf);
                good = FALSE;
            }
        }
    }
    if (good == TRUE) 
        printf("Data path test passed.\n");
    else
        printf("Data path test failed.\n");

    printf("Test Done!\n");

}

/***********************************************************************/
void iposHicTest(void)
{

    bmeHicTest();
}

/***********************************************************************/
void iposDacOutTest(void)
{
    dacOutTest();
}

/***********************************************************************/
void iposResetOutTest(void)
{
    resetTest();
}

/***********************************************************************/
void iposVBudTest(int vbusNum, int afeNum)
{
    v_busTest(vbusNum, afeNum<<8);
}

/***********************************************************************/
void iposExtMemTest(void)
{
    bmeExtTest();
}

/***********************************************************************/
void iposDataPathTest(void)
{
    dataPathTest();                
}

/***********************************************************************/
void iposUtopiaLbTest(int count)
{
    x2cloop(count);
}

/***********************************************************************/
void iposAfe2Lb(int lineNum, int trial)
{
    afe2_lb(lineNum, trial);
}

/***********************************************************************/
void iposSingleToneTest(int lineNum)
{
    tune_test = 1;
    co_tx(lineNum,1,0);
}

/***********************************************************************/
void iposMissingToneTest(int lineNum)
{
    tune_test = 2;
    co_tx(lineNum,1,0);
}

/***********************************************************************/
#ifdef INI_SUPPORT 
void iposPtmfLbTest(int count)
{
    cloop(count, 25);
}
#endif // INI_SUPPORT 
/***********************************************************************/
void iposCaptureToneTest(int lineNum)
{
    capTest = 1;
    afe2_recv(lineNum, 1);
}

#ifdef BME_CPE_20 
/***********************************************************************/
void iposGpioTest(void)
{
    testGpio();
}
/***********************************************************************/
void iposBMERegTest(void)
{
    AztecRegReadWrite();
}
/***********************************************************************/
void iposMMDIOTest(UINT32 adr, UINT32 adr1)
{
    testMmdio(adr, adr1);
}
/***********************************************************************/
void iposSMDIOTest(UINT32 adr)
{
    testSmdio();
}
/***********************************************************************/
void iposUartTest(int portNum)
{
    LoopbackUart(portNum);
}
/***********************************************************************/
#endif
void iposBmeRegDump(void)
{

    printf("\nBME register display begin\n"); 
    
    printf("\nTransmit Network Interface register\n"); 
    printf("Address = %x, data = %x\n", TNICH_FAST, testPIORead_diag(1, TNICH_FAST));
    printf("Address = %x, data = %x\n", TNICH_INLV, testPIORead_diag(1, TNICH_INLV));
    printf("Address = %x, data = %x\n", TNIPHY_LOW, testPIORead_diag(1, TNIPHY_LOW));
    printf("Address = %x, data = %x\n", TNIPHY_HIGH, testPIORead_diag(1, TNIPHY_HIGH));
    printf("Address = %x, data = %x\n", TNIPHY_LOW_ONOFF, testPIORead_diag(1, TNIPHY_LOW_ONOFF));
    printf("Address = %x, data = %x\n", TNIPHY_HIGH_ONOFF, testPIORead_diag(1, TNIPHY_HIGH_ONOFF));
    printf("Address = %x, data = %x\n", TNICELL_SIZE, testPIORead_diag(1, TNICELL_SIZE));
    printf("Address = %x, data = %x\n", TNILEVEL_ONFO, testPIORead_diag(1, TNILEVEL_ONFO));

    printf("\nXFIFO Network Interface register\n"); 
    printf("Address = %x, data = %x\n", XFIFOF_CXFT_A, testPIORead_diag(1, XFIFOF_CXFT_A));
    printf("Address = %x, data = %x\n", XFIFOF_CXFT_B, testPIORead_diag(1, XFIFOF_CXFT_B));
    printf("Address = %x, data = %x\n", XFIFOI_CXFT_A, testPIORead_diag(1, XFIFOI_CXFT_A));
    printf("Address = %x, data = %x\n", XFIFOI_CXFT_B, testPIORead_diag(1, XFIFOI_CXFT_B));

    printf("\nFramer Micro-Controller register\n"); 
    printf("Address = %x, data = %x\n", FCPU_FMC_FCR, testPIORead_diag(1, FCPU_FMC_FCR));
    printf("Address = %x, data = %x\n", FCPU_FMC_FPC, testPIORead_diag(1, FCPU_FMC_FPC));
    printf("Address = %x, data = %x\n", FCPU_FMC_FCR, testPIORead_diag(1, FCPU_FMC_FCR));

    printf("\nYFIFO Interface register\n"); 
    printf("Address = %x, data = %x\n", YFIFO_CYFT_A, testPIORead_diag(1, YFIFO_CYFT_A));
    printf("Address = %x, data = %x\n", YFIFO_CYFT_B, testPIORead_diag(1, YFIFO_CYFT_B));
    printf("Address = %x, data = %x\n", YFIFO_CYFT_C, testPIORead_diag(1, YFIFO_CYFT_C));
    printf("Address = %x, data = %x\n", YFIFO_CYFT_D, testPIORead_diag(1, YFIFO_CYFT_D));
    printf("Address = %x, data = %x\n", YFIFO_TOFT, testPIORead_diag(1, YFIFO_TOFT));

    printf("\nMapper register\n"); 
    printf("Address = %x, data = %x\n", PTONE_32T, testPIORead_diag(1, PTONE_32T));
    printf("Address = %x, data = %x\n", PTONE_64T, testPIORead_diag(1, PTONE_64T));
    printf("Address = %x, data = %x\n", PTONE_128T, testPIORead_diag(1, PTONE_128T));
    printf("Address = %x, data = %x\n", PTONE_256T, testPIORead_diag(1, PTONE_256T));
    printf("Address = %x, data = %x\n", PTONE_512T, testPIORead_diag(1, PTONE_512T));
    printf("Address = %x, data = %x\n", PTONE_1024T, testPIORead_diag(1, PTONE_1024T));
    printf("Address = %x, data = %x\n", PTONE_2048T, testPIORead_diag(1, PTONE_2048T));
    
    printf("Address = %x, data = %x\n", MAP_MCT, testPIORead_diag(1, MAP_MCT));

    printf("\nZFIFO register\n"); 
    printf("Address = %x, data = %x\n", ZFIFO_CZFT_A, testPIORead_diag(1, ZFIFO_CZFT_A));
    printf("Address = %x, data = %x\n", ZFIFO_CZFT_B, testPIORead_diag(1, ZFIFO_CZFT_B));
    printf("Address = %x, data = %x\n", ZFIFO_CZFT_C, testPIORead_diag(1, ZFIFO_CZFT_C));
    printf("Address = %x, data = %x\n", ZFIFO_CZFT_D, testPIORead_diag(1, ZFIFO_CZFT_D));
    printf("Address = %x, data = %x\n", PFXT_A, testPIORead_diag(1, PFXT_A));
    printf("Address = %x, data = %x\n", PFXT_B, testPIORead_diag(1, PFXT_B));
    printf("Address = %x, data = %x\n", PFXT_C, testPIORead_diag(1, PFXT_C));
    printf("Address = %x, data = %x\n", WCATBLE, testPIORead_diag(1, WCATBLE));

    printf("\nV_BUS Interface register\n"); 
    printf("Address = %x, data = %x\n", VIF_BCR, testPIORead_diag(1, VIF_BCR));
    printf("Address = %x, data = %x\n", VIF_ICCR, testPIORead_diag(1, VIF_ICCR));
    printf("Address = %x, data = %x\n", VIF_RWCR, testPIORead_diag(1, VIF_RWCR));
    printf("Address = %x, data = %x\n", VIF_RRCR, testPIORead_diag(1, VIF_RRCR));
    printf("Address = %x, data = %x\n", VIF_BCR, testPIORead_diag(1, VIF_BCR));
    printf("Address = %x, data = %x\n", VIF_RWDR, testPIORead_diag(1, VIF_RWDR));
    printf("Address = %x, data = %x\n", VIF_RASR, testPIORead_diag(1, VIF_RASR));

    printf("Address = %x, data = %x\n", VIF_SCCR, testPIORead_diag(1, VIF_SCCR));
    printf("Address = %x, data = %x\n", VIF_CLMR, testPIORead_diag(1, VIF_CLMR));

    printf("Address = %x, data = %x\n", VIF_TSCR, testPIORead_diag(1, VIF_TSCR));
    printf("Address = %x, data = %x\n", VIF_RSCR, testPIORead_diag(1, VIF_RSCR));

    printf("Address = %x, data = %x\n", VIF_CST, testPIORead_diag(1, VIF_CST));
    printf("Address = %x, data = %x\n", VIF_CST0L, testPIORead_diag(1, VIF_CST0L));
    printf("Address = %x, data = %x\n", VIF_CST0H, testPIORead_diag(1, VIF_CST0H));
    printf("Address = %x, data = %x\n", VIF_CST1L, testPIORead_diag(1, VIF_CST1L));
    printf("Address = %x, data = %x\n", VIF_CST1H, testPIORead_diag(1, VIF_CST1H));
    printf("Address = %x, data = %x\n", VIF_CST2L, testPIORead_diag(1, VIF_CST2L));
    printf("Address = %x, data = %x\n", VIF_CST3L, testPIORead_diag(1, VIF_CST2H));
    printf("Address = %x, data = %x\n", VIF_CST3L, testPIORead_diag(1, VIF_CST3L));
    printf("Address = %x, data = %x\n", VIF_CST3H, testPIORead_diag(1, VIF_CST3H));
    printf("Address = %x, data = %x\n", VIF_CST4H, testPIORead_diag(1, VIF_CST4L));
    printf("Address = %x, data = %x\n", VIF_CST4H, testPIORead_diag(1, VIF_CST4H));
    printf("Address = %x, data = %x\n", VIF_CST5L, testPIORead_diag(1, VIF_CST5L));
    printf("Address = %x, data = %x\n", VIF_CST5H, testPIORead_diag(1, VIF_CST5H));
    printf("Address = %x, data = %x\n", VIF_CST6L, testPIORead_diag(1, VIF_CST6L));
    printf("Address = %x, data = %x\n", VIF_CST6H, testPIORead_diag(1, VIF_CST6H));
    printf("Address = %x, data = %x\n", VIF_CST7L, testPIORead_diag(1, VIF_CST7L));
    printf("Address = %x, data = %x\n", VIF_CST7H, testPIORead_diag(1, VIF_CST7H));
    printf("Address = %x, data = %x\n", VIF_CST8L, testPIORead_diag(1, VIF_CST8L));
    printf("Address = %x, data = %x\n", VIF_CST8H, testPIORead_diag(1, VIF_CST8H));
    printf("Address = %x, data = %x\n", VIF_CST9L, testPIORead_diag(1, VIF_CST9L));
    printf("Address = %x, data = %x\n", VIF_CST9H, testPIORead_diag(1, VIF_CST9H));
    printf("Address = %x, data = %x\n", VIF_CST10L, testPIORead_diag(1, VIF_CST10L));
    printf("Address = %x, data = %x\n", VIF_CST10H, testPIORead_diag(1, VIF_CST10H));
    printf("Address = %x, data = %x\n", VIF_CST11L, testPIORead_diag(1, VIF_CST11L));
    printf("Address = %x, data = %x\n", VIF_CST11H, testPIORead_diag(1, VIF_CST11H));
    printf("Address = %x, data = %x\n", VIF_CST12L, testPIORead_diag(1, VIF_CST12L));
    printf("Address = %x, data = %x\n", VIF_CST12H, testPIORead_diag(1, VIF_CST12H));
    printf("Address = %x, data = %x\n", VIF_CST13L, testPIORead_diag(1, VIF_CST13L));
    printf("Address = %x, data = %x\n", VIF_CST13H, testPIORead_diag(1, VIF_CST13H));
    printf("Address = %x, data = %x\n", VIF_CST14L, testPIORead_diag(1, VIF_CST14L));
    printf("Address = %x, data = %x\n", VIF_CST14H, testPIORead_diag(1, VIF_CST14H));
    printf("Address = %x, data = %x\n", VIF_CST15L, testPIORead_diag(1, VIF_CST15L));
    printf("Address = %x, data = %x\n", VIF_CST15H, testPIORead_diag(1, VIF_CST15H));
    printf("Address = %x, data = %x\n", VIF_CST15L, testPIORead_diag(1, VIF_CST15L));
    printf("Address = %x, data = %x\n", VIF_CST16L, testPIORead_diag(1, VIF_CST16L));
    printf("Address = %x, data = %x\n", VIF_CST16H, testPIORead_diag(1, VIF_CST16H));
    printf("Address = %x, data = %x\n", VIF_CST17L, testPIORead_diag(1, VIF_CST17L));
    printf("Address = %x, data = %x\n", VIF_CST17H, testPIORead_diag(1, VIF_CST17H));
    printf("Address = %x, data = %x\n", VIF_CST18L, testPIORead_diag(1, VIF_CST18L));
    printf("Address = %x, data = %x\n", VIF_CST18H, testPIORead_diag(1, VIF_CST18H));
    printf("Address = %x, data = %x\n", VIF_CST19L, testPIORead_diag(1, VIF_CST19L));
    printf("Address = %x, data = %x\n", VIF_CST19H, testPIORead_diag(1, VIF_CST19H));
    printf("Address = %x, data = %x\n", VIF_CST20L, testPIORead_diag(1, VIF_CST20L));
    printf("Address = %x, data = %x\n", VIF_CST20H, testPIORead_diag(1, VIF_CST20H));
    printf("Address = %x, data = %x\n", VIF_CST21L, testPIORead_diag(1, VIF_CST21L));
    printf("Address = %x, data = %x\n", VIF_CST21H, testPIORead_diag(1, VIF_CST21H));
    printf("Address = %x, data = %x\n", VIF_CST22L, testPIORead_diag(1, VIF_CST22L));
    printf("Address = %x, data = %x\n", VIF_CST22H, testPIORead_diag(1, VIF_CST22H));
    printf("Address = %x, data = %x\n", VIF_CST23L, testPIORead_diag(1, VIF_CST23L));
    printf("Address = %x, data = %x\n", VIF_CST23H, testPIORead_diag(1, VIF_CST23H));
    printf("Address = %x, data = %x\n", VIF_CST24L, testPIORead_diag(1, VIF_CST24L));
    printf("Address = %x, data = %x\n", VIF_CST24H, testPIORead_diag(1, VIF_CST24H));
    printf("Address = %x, data = %x\n", VIF_CST25L, testPIORead_diag(1, VIF_CST25L));
    printf("Address = %x, data = %x\n", VIF_CST25H, testPIORead_diag(1, VIF_CST25H));
    printf("Address = %x, data = %x\n", VIF_CST26L, testPIORead_diag(1, VIF_CST26L));
    printf("Address = %x, data = %x\n", VIF_CST26H, testPIORead_diag(1, VIF_CST26H));
    printf("Address = %x, data = %x\n", VIF_CST27L, testPIORead_diag(1, VIF_CST27L));
    printf("Address = %x, data = %x\n", VIF_CST27H, testPIORead_diag(1, VIF_CST27H));
    printf("Address = %x, data = %x\n", VIF_CST28L, testPIORead_diag(1, VIF_CST28L));
    printf("Address = %x, data = %x\n", VIF_CST28H, testPIORead_diag(1, VIF_CST28H));
    printf("Address = %x, data = %x\n", VIF_CST29L, testPIORead_diag(1, VIF_CST29L));
    printf("Address = %x, data = %x\n", VIF_CST29H, testPIORead_diag(1, VIF_CST29H));
    printf("Address = %x, data = %x\n", VIF_CST30L, testPIORead_diag(1, VIF_CST30L));
    printf("Address = %x, data = %x\n", VIF_CST30H, testPIORead_diag(1, VIF_CST30H));
    printf("Address = %x, data = %x\n", VIF_CST31L, testPIORead_diag(1, VIF_CST31L));
    printf("Address = %x, data = %x\n", VIF_CST31H, testPIORead_diag(1, VIF_CST31H));

    printf("\nAFIFO register\n"); 
    printf("Address = %x, data = %x\n", CAFT_A, testPIORead_diag(1, CAFT_A));
    printf("Address = %x, data = %x\n", CAFT_C, testPIORead_diag(1, CAFT_C));
    printf("Address = %x, data = %x\n", CAFT_D, testPIORead_diag(1, CAFT_D));

    
    printf("\nReceive Micro-contrller register\n"); 
    printf("Address = %x, data = %x\n", RMC_RCR, testPIORead_diag(1, RMC_RCR));
    printf("Address = %x, data = %x\n", RMC_RPC, testPIORead_diag(1, RMC_RPC));
    printf("Address = %x, data = %x\n", RMC_ICR, testPIORead_diag(1, RMC_ICR));

    printf("\nFrequency domain Eq table register\n"); 
    printf("Address = %x, data = %x\n", FETHI, testPIORead_diag(1, FETHI));

    printf("\nDemapper register\n"); 
    printf("Address = %x, data = %x\n", DMPCTX, testPIORead_diag(1, DMPCTX));
    printf("Address = %x, data = %x\n", DMPPLT, testPIORead_diag(1, DMPPLT));
    printf("Address = %x, data = %x\n", MDPMUT, testPIORead_diag(1, MDPMUT));
    printf("Address = %x, data = %x\n", DMPSIT, testPIORead_diag(1, DMPSIT));
    printf("Address = %x, data = %x\n", SNRCTX, testPIORead_diag(1, SNRCTX));
    printf("Address = %x, data = %x\n", SNRTON, testPIORead_diag(1, SNRTON));
    printf("Address = %x, data = %x\n", DMPSBT, testPIORead_diag(1, DMPSBT));
//    testPIORead_diag(1, FETLOW);

    printf("\nBFIFO register\n"); 
    printf("Address = %x, data = %x\n", CDMT, testPIORead_diag(1, CDMT));
    printf("Address = %x, data = %x\n", CBFT_A, testPIORead_diag(1, CBFT_A));
    printf("Address = %x, data = %x\n", CBFT_B, testPIORead_diag(1, CBFT_B));
    printf("Address = %x, data = %x\n", CBIT_A, testPIORead_diag(1, CBIT_A));
    printf("Address = %x, data = %x\n", CBIT_B, testPIORead_diag(1, CBIT_B));
    printf("Address = %x, data = %x\n", CBIT_C, testPIORead_diag(1, CBIT_C));
    printf("Address = %x, data = %x\n", CBIT_D, testPIORead_diag(1, CBIT_D));

    printf("\nDeframer Micro-Controller register\n"); 
    printf("Address = %x, data = %x\n", DMC_DCR, testPIORead_diag(1, DMC_DCR));
    printf("Address = %x, data = %x\n", DMC_DPC, testPIORead_diag(1, DMC_DPC));
    printf("Address = %x, data = %x\n", DMC_ICR, testPIORead_diag(1, DMC_ICR));

    printf("\nCFIFO register\n"); 
    printf("Address = %x, data = %x\n", CCFT0_A, testPIORead_diag(1, CCFT0_A));
    printf("Address = %x, data = %x\n", CCFT0_B, testPIORead_diag(1, CCFT0_B));
    printf("Address = %x, data = %x\n", CCFT1_A, testPIORead_diag(1, CCFT1_A));
    printf("Address = %x, data = %x\n", CCFT1_B, testPIORead_diag(1, CCFT1_B));

    printf("\nReceive Network Interface register\n"); 
    printf("Address = %x, data = %x\n", RNI_MCR, testPIORead_diag(1, RNI_MCR));
    printf("Address = %x, data = %x\n", PHY_CTRL_A, testPIORead_diag(1, PHY_CTRL_A));
    printf("Address = %x, data = %x\n", PHY_CTRL_B, testPIORead_diag(1, PHY_CTRL_B));

    printf("\nPIO Interface register\n"); 
    printf("Address = %x, data = %x\n", PIO_MCR(0), testPIORead_diag(1, PIO_MCR(0)));
    printf("Address = %x, data = %x\n", CKM_CCR(0), testPIORead_diag(1, CKM_CCR(0)));
    printf("Address = %x, data = %x\n", CKM_PCR(0), testPIORead_diag(1, CKM_PCR(0)));
    printf("Address = %x, data = %x\n", PIO_ISR(0), testPIORead_diag(1, PIO_ISR(0)));
    printf("Address = %x, data = %x\n", PIO_RSR(0), testPIORead_diag(1, PIO_RSR(0)));
    printf("Address = %x, data = %x\n", PIO_BID(0), testPIORead_diag(1, PIO_BID(0)));
    printf("Address = %x, data = %x\n", DAC_MCR(0), testPIORead_diag(1, DAC_MCR(0)));

    printf("\nDMA Controller register\n"); 
    printf("Address = %x, data = %x\n", DMA_CCR0, testPIORead_diag(1, DMA_CCR0));
    printf("Address = %x, data = %x\n", DMA_CCR1, testPIORead_diag(1, DMA_CCR1));
    printf("Address = %x, data = %x\n", DMA_CCR2, testPIORead_diag(1, DMA_CCR2));
    printf("Address = %x, data = %x\n", DMA_CCR3, testPIORead_diag(1, DMA_CCR3));
    printf("Address = %x, data = %x\n", DMA_CCR4, testPIORead_diag(1, DMA_CCR4));
    printf("Address = %x, data = %x\n", DMA_CCR5, testPIORead_diag(1, DMA_CCR5));
    printf("Address = %x, data = %x\n", DMA_CCR6, testPIORead_diag(1, DMA_CCR6));
    printf("Address = %x, data = %x\n", DMA_CCR7, testPIORead_diag(1, DMA_CCR7));
    printf("Address = %x, data = %x\n", DMA_CCR8, testPIORead_diag(1, DMA_CCR8));
    printf("Address = %x, data = %x\n", DMA_CCR9, testPIORead_diag(1, DMA_CCR9));
    printf("Address = %x, data = %x\n", DMA_CCR10, testPIORead_diag(1, DMA_CCR10));
    printf("Address = %x, data = %x\n", DMA_CCR11, testPIORead_diag(1, DMA_CCR11));
    printf("Address = %x, data = %x\n", DMA_CCR12, testPIORead_diag(1, DMA_CCR12));
    printf("Address = %x, data = %x\n", DMA_CCR13, testPIORead_diag(1, DMA_CCR13));
    printf("Address = %x, data = %x\n", DMA_CCR14, testPIORead_diag(1, DMA_CCR14));
    printf("Address = %x, data = %x\n", DMA_CCR15, testPIORead_diag(1, DMA_CCR15));

    printf("Address = %x, data = %x\n", DMA_ECR0, testPIORead_diag(1, DMA_ECR0));
    printf("Address = %x, data = %x\n", DMA_ECR1, testPIORead_diag(1, DMA_ECR1));
    printf("Address = %x, data = %x\n", DMA_ECR2, testPIORead_diag(1, DMA_ECR2));
    printf("Address = %x, data = %x\n", DMA_ECR3, testPIORead_diag(1, DMA_ECR3));
    printf("Address = %x, data = %x\n", DMA_ECR4, testPIORead_diag(1, DMA_ECR4));
    printf("Address = %x, data = %x\n", DMA_ECR5, testPIORead_diag(1, DMA_ECR5));
    printf("Address = %x, data = %x\n", DMA_ECR6, testPIORead_diag(1, DMA_ECR6));
    printf("Address = %x, data = %x\n", DMA_ECR7, testPIORead_diag(1, DMA_ECR7));
    printf("Address = %x, data = %x\n", DMA_ECR8, testPIORead_diag(1, DMA_ECR8));
    printf("Address = %x, data = %x\n", DMA_ECR9, testPIORead_diag(1, DMA_ECR9));
    printf("Address = %x, data = %x\n", DMA_ECR10,  testPIORead_diag(1, DMA_ECR10));
    printf("Address = %x, data = %x\n", DMA_ECR11, testPIORead_diag(1, DMA_ECR11));
    printf("Address = %x, data = %x\n", DMA_ECR12, testPIORead_diag(1, DMA_ECR12));
    printf("Address = %x, data = %x\n", DMA_ECR13, testPIORead_diag(1, DMA_ECR13));
    printf("Address = %x, data = %x\n", DMA_ECR14, testPIORead_diag(1, DMA_ECR14));
    printf("Address = %x, data = %x\n", DMA_ECR15, testPIORead_diag(1, DMA_ECR15));

    printf("Address = %x, data = %x\n", DMA_SAR0, testPIORead_diag(1, DMA_SAR0));
    printf("Address = %x, data = %x\n", DMA_SAR1, testPIORead_diag(1, DMA_SAR1));
    printf("Address = %x, data = %x\n", DMA_SAR2, testPIORead_diag(1, DMA_SAR2));
    printf("Address = %x, data = %x\n", DMA_SAR3, testPIORead_diag(1, DMA_SAR3));
    printf("Address = %x, data = %x\n", DMA_SAR4, testPIORead_diag(1, DMA_SAR4));
    printf("Address = %x, data = %x\n", DMA_SAR5, testPIORead_diag(1, DMA_SAR5));
    printf("Address = %x, data = %x\n", DMA_SAR6, testPIORead_diag(1, DMA_SAR6));
    printf("Address = %x, data = %x\n", DMA_SAR7, testPIORead_diag(1, DMA_SAR7));
    printf("Address = %x, data = %x\n", DMA_SAR8, testPIORead_diag(1, DMA_SAR8));
    printf("Address = %x, data = %x\n", DMA_SAR9, testPIORead_diag(1, DMA_SAR9));
    printf("Address = %x, data = %x\n", DMA_SAR10, testPIORead_diag(1, DMA_SAR10));
    printf("Address = %x, data = %x\n", DMA_SAR11, testPIORead_diag(1, DMA_SAR11));
    printf("Address = %x, data = %x\n", DMA_SAR12, testPIORead_diag(1, DMA_SAR12));
    printf("Address = %x, data = %x\n", DMA_SAR13, testPIORead_diag(1, DMA_SAR13));
    printf("Address = %x, data = %x\n", DMA_SAR14, testPIORead_diag(1, DMA_SAR14));
    printf("Address = %x, data = %x\n", DMA_SAR15, testPIORead_diag(1, DMA_SAR15));

    printf("Address = %x, data = %x\n", DMA_DAR0, testPIORead_diag(1, DMA_DAR0));
    printf("Address = %x, data = %x\n", DMA_DAR1, testPIORead_diag(1, DMA_DAR1));
    printf("Address = %x, data = %x\n", DMA_DAR2, testPIORead_diag(1, DMA_DAR2));
    printf("Address = %x, data = %x\n", DMA_DAR3, testPIORead_diag(1, DMA_DAR3));
    printf("Address = %x, data = %x\n", DMA_DAR4, testPIORead_diag(1, DMA_DAR4));
    printf("Address = %x, data = %x\n", DMA_DAR5, testPIORead_diag(1, DMA_DAR5));
    printf("Address = %x, data = %x\n", DMA_DAR6, testPIORead_diag(1, DMA_DAR6));
    printf("Address = %x, data = %x\n", DMA_DAR7, testPIORead_diag(1, DMA_DAR7));
    printf("Address = %x, data = %x\n", DMA_DAR8, testPIORead_diag(1, DMA_DAR8));
    printf("Address = %x, data = %x\n", DMA_DAR9, testPIORead_diag(1, DMA_DAR9));
    printf("Address = %x, data = %x\n", DMA_DAR10, testPIORead_diag(1, DMA_DAR10));
    printf("Address = %x, data = %x\n", DMA_DAR11, testPIORead_diag(1, DMA_DAR11));
    printf("Address = %x, data = %x\n", DMA_DAR12, testPIORead_diag(1, DMA_DAR12));
    printf("Address = %x, data = %x\n", DMA_DAR13, testPIORead_diag(1, DMA_DAR13));
    printf("Address = %x, data = %x\n", DMA_DAR14, testPIORead_diag(1, DMA_DAR14));
    printf("Address = %x, data = %x\n", DMA_DAR15, testPIORead_diag(1, DMA_DAR15));

    printf("\nEMI Interface register\n"); 
    printf("Address = %x, data = %x\n", EMI_MCR(0), testPIORead_diag(1, EMI_MCR(0)));
    printf("Address = %x, data = %x\n", EMI_RPR(0), testPIORead_diag(1, EMI_RPR(0)));
    printf("Address = %x, data = %x\n", EMI_RCR(0), testPIORead_diag(1, EMI_RCR(0)));
    printf("Address = %x, data = %x\n", EMI_PCR(0), testPIORead_diag(1, EMI_PCR(0)));
    printf("Address = %x, data = %x\n", EMI_SCR(0), testPIORead_diag(1, EMI_SCR(0)));
    
    printf("\nBME register display end\n"); 
}

#ifdef BME_CPE_20 
/************************************************************/
/* CPE 2.0 bring up test code */
/************************************************************/

/****************************************************************************/
/*                                                                          */
/* Function.. : iniwrite	                        	   	   */
/*                                                   	                    */
/* Purpose... : Writes one byte data to general INI control register 	    */
/*									    */
/* Parameters : None								*/
/*                                                                          */
/* Returns... : N/A                                                         */
/*                                                                          */
/****************************************************************************/
void iniwrite_diag(BYTE addr,BYTE data)
{
	BYTE *baseAddr;

	baseAddr = (BYTE *) 0x09000000;
	writeControlPort(baseAddr,addr, data);
	iposDelay();
//printf ("\niniwrite: addr 0x%x data 0x%x,",addr,data);
}   

/****************************************************************************/
/*                                                                          */
/* Function.. : iniread	                        			     */
/*                                                   	                    */
/* Purpose... : Read one byte data from general INI control register 		*/
/*										*/
/* Parameters : None								*/
/*                                                                          */
/* Returns... : N/A                                                         */
/*                                                                          */
/****************************************************************************/
void iniread_diag(BYTE addr, BYTE *data)
{
	BYTE *baseAddr;

	baseAddr = (BYTE *)0x09000000;

	readControlPort (baseAddr,addr,*data);
	iposDelay();
//printf ("\n iniread addr 0x%x data: 0x%x\n",addr,*data);
}

/************************************************************/
void AztecRegReadWrite (void) {
  int i,j = 0;
  int MaxRegnumber = 368;
  int flag = 0;
  UINT32 ReadData;
  UINT32 RegAddr [368] = {
	0xe0000000, 0xe0000100, 0xe0000200, 0xe0000400, 0xe0040000, 0xe0040100, 0xe0040200, 0xe0040300,
        0xe0008000, 0xe0008004, 0xe0008008, 0xe0048000, 0xe0048100, 0xe0048200, 0xe0048300, 0xe0048400,
        0xe0048500, 0xe0048600, 0xe0014000, 0xe0014100, 0xe0014200, 0xe0014300, 0xe0014400, 0xe0014500,
        0xe0014600, 0xe0010000, 0xe0050000, 0xe0050200, 0xe0050300, 0xe0050400, 0xe0050500, 0xe0050600,
        0xe0050700, 0xe0050704, 0xe0050708, 0xe005070c, 0xe0050710, 0xe0050714, 0xe0050718, 0xe005071c,
        0xe0050720, 0xe0050724, 0xe0050728, 0xe005072c, 0xe0050730, 0xe0050734, 0xe0050738, 0xe005073c,
        0xe0050740, 0xe0050744, 0xe0050748, 0xe005074c, 0xe0050750, 0xe0050754, 0xe0050758, 0xe005075c,
        0xe0050760, 0xe0050764, 0xe0050768, 0xe005076c, 0xe0050770, 0xe0050774, 0xe0050778, 0xe005077c,
        0xe0050780, 0xe0050784, 0xe0050788, 0xe005078c, 0xe0050790, 0xe0050794, 0xe0050798, 0xe005079c,
        0xe00507a0, 0xe00507a4, 0xe00507a8, 0xe00507ac, 0xe00507b0, 0xe00507b4, 0xe00507b8, 0xe00507bc,
        0xe00507c0, 0xe00507c4, 0xe00507c8, 0xe00507cc, 0xe00507d0, 0xe00507d4, 0xe00507d8, 0xe00507dc,
        0xe00507e0, 0xe00507e4, 0xe00507e8, 0xe00507ec, 0xe00507f0, 0xe00507f4, 0xe00507f8, 0xe00507fc,
        0xe0018000, 0xe0018004, 0xe0018008, 0xe001800c, 0xe0018010, 0xe0018014, 0xe0018018, 0xe0018054,
        0xe0018058, 0xe0018400, 0xe0018408, 0xe0018410, 0xe0018418, 0xe0018420, 0xe0018428, 0xe0018430,
        0xe0018438, 0xe0018440, 0xe0018448, 0xe0018450, 0xe0018458, 0xe0018460, 0xe0018468, 0xe0018470,
        0xe0018478, 0xe0018480, 0xe0018488, 0xe0018490, 0xe0018498, 0xe00184a0, 0xe00184a8, 0xe00184b0,
        0xe00184b8, 0xe00184c0, 0xe00184c8, 0xe00184d0, 0xe00184d8, 0xe00184e0, 0xe00184e8, 0xe00184f0,
        0xe00184f8, 0xe0018404, 0xe001840c, 0xe0018414, 0xe001841c, 0xe0018424, 0xe001842c, 0xe0018434,
        0xe001843c, 0xe0018444, 0xe001844c, 0xe0018454, 0xe001845c, 0xe0018464, 0xe001846c, 0xe0018474,
        0xe001847c, 0xe0018484, 0xe001848c, 0xe0018494, 0xe001849c, 0xe00184a4, 0xe00184ac, 0xe00184b4,
        0xe00184bc, 0xe00184c4, 0xe00184cc, 0xe00184d4, 0xe00184dc, 0xe00184e4, 0xe00184ec, 0xe00184f4,
        0xe00184fc, 0xe0058000, 0xe0058200, 0xe0058300, 0xe0078000, 0xe0078004, 0xe0078008, 0xe0088000,
        0xe0080000, 0xe0080100, 0xe0081000, 0xe0081100, 0xe0082000, 0xe0082100, 0xe0083000, 0xe0090000,
        0xe0060000, 0xe0060100, 0xe0060200, 0xe0060300, 0xe0060400, 0xe0060500, 0xe0060600, 0xe0020000,
        0xe0020004, 0xe0020008, 0xe0068000, 0xe0068100, 0xe0068004, 0xe0068104, 0xe0028100, 0xe0028104,
        0xe0070000, 0xe0070004, 0xe0070008, 0xe007000c, 0xe0070010, 0xe0070014, 0xe0070018, 0xe007001c,
        0xe0070020, 0xe0070024, 0xe0070028, 0xe0070030, 0xe0070034, 0xe0070038, 0xe0030000, 0xe0030004,
        0xe0030008, 0xe003000c, 0xe0030010, 0xe0030014, 0xe0030018, 0xe0030040, 0xe0030044, 0xe0030048,
        0xe003004c, 0xe0030050, 0xe0030054, 0xe0030058, 0xe0030080, 0xe0030084, 0xe0030088, 0xe003008c,
        0xe0030090, 0xe0030094, 0xe0030098, 0xe00300c0, 0xe00300c4, 0xe00300c8, 0xe00300cc, 0xe00300d0,
        0xe00300d4, 0xe00300d8, 0xe0038000, 0xe0038004, 0xe0038008, 0xe003800c, 0xe0038010, 0xe0038014,
        0xe0038018, 0xe0098880, 0xe0098884, 0xe0098888, 0xe009888c, 0xe0098890, 0xe0098894, 0xe0098898,
        0xe009889c, 0xe00988a0, 0xe00988a4, 0xe00988c0, 0xe00988c4, 0xe00988c8, 0xe00988cc, 0xe00988d0,
        0xe00988d4, 0xe00988d8, 0xe00988dc, 0xe00988e0, 0xe00988e4, 0xe00988e8, 0xe0098900, 0xe0098904,
        0xe0098908, 0xe009890c, 0xe0098910, 0xe0098914, 0xe0098940, 0xe0098944, 0xe0098948, 0xe009894c,
        0xe0098950, 0xe0098954, 0xe0098958, 0xe009895c, 0xe0098960, 0xe0098964, 0xe0098968, 0xe009896c,
        0xe0098970, 0xe0098974, 0xe0098980, 0xe0098984, 0xe0098988, 0xe009898c, 0xe0098990, 0xe0098994,
        0xe0098998, 0xe009899c, 0xe00989a0, 0xe00989a4, 0xe00989a8, 0xe00989ac, 0xe00989b0, 0xe00989b4,
        0xe00989c0, 0xe00989c4, 0xe00989c8, 0xe00989cc, 0xe00989d0, 0xe00989d4, 0xe00989d8, 0xe00989dc,
        0xe00989e0, 0xe00989e4, 0xe00989e8, 0xe00989ec, 0xe00989f0, 0xe0098a00, 0xe0098a04, 0xe0098a08,
        0xe0098a0c, 0xe0098a10, 0xe0098a14, 0xe0098a18, 0xe0098a1c, 0xe0098a20, 0xe0098a24, 0xe0098a28,
        0xe0098a2c, 0xe0098a30, 0xe0098a34, 0xe0098a38, 0xe0098a40, 0xe0098a44, 0xe0098a48, 0xe0098a4c,
        0xe0098a50, 0xe0098a54, 0xe0098a58, 0xe0098a5c, 0xe0098a60, 0xe0098c60, 0xe0098c64, 0xe0098c68,
        0xe0098c6c, 0xe0098300, 0xe0098340, 0xe0098344, 0xe0098348, 0xe009834c, 0xe0098380, 0xe009808c,
        0xe0098080, 0xe0098084, 0xe0098088, 0xe0098100, 0xe0098104, 0xe0098108, 0xe00a000c, 0xe00a0000,
	0xe00a0004, 0xe00a0010, 0xe00a001c, 0xe00a200c, 0xe00a2000, 0xe00a2004, 0xe00a2010, 0xe00a201c };

  UINT32 RegMask [368] = {
	0xfe000000, 0xfe000000, 0xfe000000, 0x80000000, 0x0000e007, 0x3fffffff, 0x0000e007, 0x3fffffff,
        0x00000006, 0x00000000, 0x00000000, 0x00000007, 0xffffffff, 0xffffff1f, 0xffffffff, 0xffffffff,
        0x00007007, 0x0fffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0x0000ffff, 0x000a0000, 0x0003ffff, 0x0003ffff, 0x0ffffffc, 0x01ffffff, 0x0000001f,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xc0000000, 0x03ff03ff, 0xc000ffff, 0x00000000, 0xffffffff, 0x00000000, 0xf8000000, 0x00000000,
        0x00000001, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f,
        0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f,
        0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f, 0x0000ff1f,
        0x0000ff1f, 0x0000ffff, 0x0000ff1f, 0x0000ffff, 0x0000ff1f, 0x0000ffff, 0x0000ff1f, 0x0000ffff,
        0x0000ff1f, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0x000e004f, 0x0003ffff, 0x0003ffff, 0x00000006, 0x00000000, 0x00000000, 0x00000000,
        0xffff7f7f, 0xffffffff, 0x0000000f, 0x000000ff, 0x00000fff, 0x00000fff, 0x0000000f, 0xffff0000,
        0xffffffff, 0x00007007, 0x0fffffff, 0x00000007, 0xffffffff, 0xffffff1f, 0xffffffff, 0x7ffefffe,
        0x00000000, 0x00000000, 0x0000f82f, 0x3fffffff, 0x0000f82f, 0x3fffffff, 0x80000000, 0x80000000,
        0x80000001, 0x07ff3fff, 0x0f010100, 0x01010001, 0x00000000, 0x00000000, 0x8fffffff, 0xffffffff,
        0xf7ffffff, 0x80008000, 0xf0000000, 0x00000000, 0x80000000, 0x00000000, 0x07ff1f3f, 0x07ff1f3f,
        0x07ff1f3f, 0x07ff1f3f, 0x07ff1f3f, 0x07ff1f3f, 0x07ff1f3f, 0x71ff0000, 0x71ff0000, 0x71ff0000,
        0x71ff0000, 0x71ff0000, 0x71ff0000, 0x71ff0000, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
        0xffffffff, 0xffffffff, 0x000003ff, 0x000000ff, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00007fff, 0x0000003f, 0x0000ffff, 0x000001ff, 0x000003ff, 0x000001ff, 0x000001ff,
        0x000000c3, 0x00002003, 0x00000001, 0x0000ffff, 0x00000000, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x000000ff, 0x0000ffff, 0x0000ffff, 0x000000ff, 0x000000ff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x00000000, 0x00000000, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x00000000, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
        0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000001, 0x0000000b, 0x0000ffff, 0x000000ff, 0x0000ffff, 0x00000000,
        0x0000ffff, 0x000003ff, 0x000003ff, 0x0000ffff, 0x00000000, 0x0000ffff, 0x000000ff, 0x000000ff,
        0x000000ff, 0x0000001f, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x0000001f, 0x000000ff };

	//writing registers
	printf ("******** Writing all BME 2.0 registers ********\n");
	for (i=0; i<MaxRegnumber; i++) {
//		printf ("writing BME 2.0 register 0x%x\n", RegAddr[i]);
		testPIOWrite_diag (1, RegAddr[i], _32b);
	}

	//reading registers
	printf ("******** Reading and comparing all BME 2.0 registers ********\n");
	for (i=0; i<MaxRegnumber; i++) {
		ReadData = testPIORead_diag (1,RegAddr[i]);
		ReadData = ReadData & RegMask[i];
		if (ReadData == RegMask[i]) {
		    printf ("BME 2.0 register 0x%x write/read passed\n", (unsigned int)RegAddr[i]);
		}
		else {
		    printf ("BME 2.0 register 0x%x write/read FAILED, ReadData:0x%x ExpData:0x%x\n",
			 (unsigned int)RegAddr[i], (unsigned int)ReadData, (unsigned int)RegMask[i]);
		    flag = 1;
		    j++;
		}
	}
	if (flag) {
		printf ("***********************************************************\n");
		printf ("All BME registers write/read test FAILED for %d registers\n",j);
		printf ("***********************************************************\n");
	}
	else {
		printf ("**********************************************************\n");
		printf ("        All BME registers write/read test PASSED \n");
		printf ("**********************************************************\n");
	}
}

/************************************************************/
void mmdiowr (UINT32 MdioAddr, UINT32 Addr, UINT32 Data)
{
  UINT32 Data1;
  int i=0;

  Data1 = testPIORead_diag (1,MDIO_POLLING);
  while ((Data1 & 0x01) || (Data1 & 0x02))  //polling for status
    {
	Data1 = testPIORead_diag (1,MDIO_POLLING);
	i++;
	if (i == 300)
	  {
	    i = 0;
	    printf ("********* Time out mmdio status polling 1 ********\n");
	    break;
	  }
    }

  testPIOWrite_diag (1, MDIO_REG_2, (((MdioAddr & 0x1f) << 5) |(Addr & 0x1f)));
  testPIOWrite_diag (1, MDIO_REG_1, (Data & 0xffff));

  Data1 = testPIORead_diag (1,MDIO_POLLING);
  while ((Data1 & 0x01) || (Data1 & 0x02))  //polling for status
    {
	Data1 = testPIORead_diag (1,MDIO_POLLING);
	i++;
	if (i == 300)
	  {
	    i = 0;
	    printf ("********* Time out mmdio status polling 2 ********\n");
	    break;
	  }
    }
  printf ("Master MDIO write to PHY is successful\n");
}

//--------------------------------------------------------------
UINT32 mmdiord (UINT32 MdioAddr, UINT32 Addr)
{
  UINT32 Data1;
  int i = 0;

  Data1 = testPIORead_diag (1,MDIO_POLLING);
  while ((Data1 & 0x01) || (Data1 & 0x02))  //polling for status
    {
	Data1 = testPIORead_diag (1,MDIO_POLLING);
	i++;
	if (i == 300)
	  {
	    i = 0;
	    printf ("********* Time out mmdio status polling 1 ********\n");
	    break;
	  }
    }

  testPIOWrite_diag (1, MDIO_REG_2, (((MdioAddr & 0x1f) << 5) |(Addr & 0x1f)));
  testPIORead_diag (1,MDIO_REG_3);
  
  i = 0;
  Data1 = testPIORead_diag (1,MDIO_POLLING);
  while ((Data1 & 0x01) || (Data1 & 0x02))  //polling for status
    {
	Data1 = testPIORead_diag (1,MDIO_POLLING);
	i++;
	if (i == 300)
	  {
	    i = 0;
	    printf ("********* Time out mmdio status polling 2 ********\n");
	    break;
	  }
    }

  Data1 = (UINT32)testPIORead_diag (1,MDIO_REG_4);
  printf ("Master MDIO read from PHY is successful : RegAddr 0x%x   Data 0x%x\n",
	(unsigned int)(Addr&0x1f),(unsigned int)(Data1&0xffff));
  return Data1;
}

void testMmdio (UINT32 adr, UINT32 adr1)
  {
    UINT32 Data;
    mmdiowr (adr, adr1, 0xaaaa);  //Intel Phy mdio addr = 0x18, reg 0x18 = LED config reg
    Data = mmdiord (adr,adr1);
    if ((Data & 0xffff)== 0xaaaa)
      {
	  printf ("***************************************\n");
	  printf ("        Master MDIO test PASSED\n");
	  printf ("***************************************\n");
	}
    else
      {
	  printf ("***************************************\n");
	  printf ("        Master MDIO test FAILED\n");
	  printf ("***************************************\n");
	}
  }

/************************************************************/
void SmdioWrite (UINT32 MdioAddr, UINT32 Addr, UINT32 Data)
{
  iniwrite_diag (0x0,(Data&0xff));
  iniwrite_diag (0x1,((Data >> 8)&0xff));
  iniwrite_diag (0x2, MdioAddr&0x1f);
  iniwrite_diag (0x3,((Addr&0x1f) | 0x20));

  printf ("Slave MDIO write to BME 2.0 is successful\n");
}

UINT32 SmdioRead (UINT32 MdioAddr, UINT32 Addr)
{
  UINT8 Data1, Data2;
  UINT32 mData1, mData2;
  int i=0;

  mData1 = 0x0;
  mData2 = 0x0;
  Data1 = 0x0;
  iniwrite_diag (0x2, MdioAddr&0x1f);
  iniwrite_diag (0x3,(Addr&0x1f));

  Data1 = 0x0;
  while (!(Data1 & 0x80))  //polling for status
    {
	iniread_diag (0x3, &Data1);
	i++;
	if (i == 300)
	  {
	    i = 0;
	    printf ("********* Time out smdiord status polling ********\n");
	    break;
	  }
    }

  iniread_diag (0x0, &Data1);
  iniread_diag (0x1, &Data2);
  mData2 = (Data2 << 8);
  mData1 = Data1 | mData2;
  printf ("Slave MDIO read from BME 2.0 is successful, Addr = 0x%x  Data = 0x%x\n",
 	(unsigned int)Addr,(unsigned int)mData1);
  return mData1;
}

void testSmdio (void)
  {
    UINT32 Data;

    SmdioWrite (0x0, 0x0, 0xaa55);
    Data = SmdioRead (0x0,0x0);
    if (Data == 0xaa55)
      {
	printf ("***************************************\n");
	printf ("        Slave MDIO test PASSED\n");
	printf ("***************************************\n");
      }
    else
      {
	printf ("***************************************\n");
	printf ("        Slave MDIO test FAILED\n");
	printf ("***************************************\n");
      }
  }
/************************************************************/
void testGpio (void)
  {
    UINT32 WriteData = 0x5a;
    UINT32 GpioDir = 0xff;
    UINT32 ReadDataAztec;
    char ReadDataFpga;

    testPIOWrite_diag (1,FPGA_REG_2,0x00);
    iniwrite_diag (0x4,0x00);
    iposDelay ();
    iniwrite_diag (0x5,0xa5);
    ReadDataAztec = testPIORead_diag (1,FPGA_REG_3);   //GPIO read data in Aztec
    iniwrite_diag (0x4,GpioDir);   //Master FPGA GPIO direction
    testPIOWrite_diag (1,FPGA_REG_2,GpioDir);   //GPIO direction in Aztec
    testPIOWrite_diag (1,FPGA_REG_1,WriteData);   //GPIO direction in Aztec

    iniread_diag (0xf, &ReadDataFpga);
    if ((ReadDataFpga == (WriteData&0xff)) && (ReadDataAztec == 0xa5a5a5a5))
      {
	printf ("***************************************\n");
	printf ("        GPIO test PASSED\n");
	printf ("***************************************\n");
      }
    else
      {
	printf ("***************************************\n");
	printf ("        GPIO test FAILED\n");
	printf ("***************************************\n");
      }

  }

/************************************************************/
void InitSdram (int width, int speed) 
//width 1-16/0-32, speed 1-core clock/0-half core clock
{
  UINT32 SdramWidth;
  UINT32 Sdramspeed;
  UINT32 EmiControlReg;

  SdramWidth = (width & 0x1) << 9;
  Sdramspeed = (speed & 0x1) << 8;
  EmiControlReg = (0x2d | SdramWidth | Sdramspeed);
  testPIOWrite_diag (1,EMI_MCR(0),EmiControlReg);
  testPIOWrite_diag (1,EMI_RPR(0),0x000000eb); //for 157.5 MHz
  testPIOWrite_diag (1,EMI_PCR(0),0x00000000);
  testPIOWrite_diag (1,EMI_RCR(0),0x00000000);
  testPIOWrite_diag (1,EMI_RCR(0),0x00000000);
  testPIOWrite_diag (1,EMI_SCR(0),0x00000000);
}
//-------------------------------------------------------
void WriteSdram (int StartAddr, int NumLocations, UINT32 StartData)
{
  UINT32 SdramAddr;
  UINT32 data;
  int SdramMaxAddr;
  int i;

  data = StartData;
  SdramMaxAddr = StartAddr + (NumLocations * 4); //max 16777216 for 16 Mbyte memory
  for (i=StartAddr; i<SdramMaxAddr; i=i+4)
    {
	SdramAddr = i & _32b;
	testPIOWrite_diag (1,SdramAddr,data);
	data++;
    }
}
/************************************************************/
//------- Top level uart diag function -------
UINT32 ReceiveCharacter (int UartNo)
{
  UINT32 UartStatus;
  UINT32 RcvdChar;
  UINT32 Data=0;
  UINT32 StatusRegAddr;
  UINT32 DataRegAddr;
  int FailStatus = 0;

  int i = 0;

	StatusRegAddr = ((UartNo&0x1) << 13) | 0xe00a0014;
	DataRegAddr =((UartNo&0x1) << 13) | UART_REG_1;
  	UartStatus = testPIORead_diag (1,StatusRegAddr);
  	while (!(UartStatus & 0x01))
        {
	  if (i == 300) {
	    FailStatus = 1;
	    break;
	  }
          UartStatus = testPIORead_diag (1,StatusRegAddr);
	  i++;
        }
	if (!FailStatus) {
  	  RcvdChar = testPIORead_diag (1,DataRegAddr);
	  Data = RcvdChar & 0xff;
	  printf ("%c", (unsigned int)Data);
	}
	else
	  printf ("***** Uart loopback test Timed out/Failed *****\n");
	
	return Data;
}

//---------------------------------------------
void LoopbackUart (int TxUart) //input Tx uart number 0/1
{
  char *pString = " Welcome to Ikanos Communications*";
  UINT32 UartData;
  UINT32 Lsr = 0x0;
  UINT32 DataAddr;
  UINT32 StatusAddr;
  UINT32 RxData;
  int RxUart;
  int TestFailStatus = 0;

  ConfigUart (0);
  ConfigUart (1);
  DataAddr = ((TxUart&0x1) << 13) | UART_REG_1;
  StatusAddr = ((TxUart&0x1) << 13) | 0xe00a0014;
  RxUart = TxUart ? 0 : 1;
  printf ("UART %d Transmitting, UART %d receiving\n",TxUart,RxUart);
  while(*pString != '*')
  {
    UartData = *pString;
    testPIOWrite_diag (1, DataAddr,UartData);
    Lsr = 0x0;
    while (!(Lsr & 0x40))
    {
        Lsr = testPIORead_diag (1,StatusAddr);
    }
    RxData = ReceiveCharacter (RxUart);
    if (RxData != UartData)
	{
	  TestFailStatus = 1;
	}
    pString++;
  }
  printf ("\n");
  if (TestFailStatus)
    {
	printf ("***************************************\n");
	printf ("      UART loopback test FAILED\n");
	printf ("***************************************\n");
    }
  else
    {
	printf ("***************************************\n");
	printf ("      UART loopback test PASSED\n");
	printf ("***************************************\n");
    }
}
//-------- lower lovel functions used by LoopbackUart ------
void ConfigUart (int UartNo)
{
  UINT32 Addr;

  Addr = ((UartNo&0x1) << 13) | UART_REG_4;
  testPIOWrite_diag (1, Addr,0x00000083); 	// set DLAB bit

  Addr = ((UartNo&0x1) << 13) | UART_REG_1;
  testPIOWrite_diag (1, Addr,0x000000a2); 	// set divisor low bit

  Addr = ((UartNo&0x1) << 13) | UART_REG_2;
  testPIOWrite_diag (1, Addr,0x00000000); 	// set divisor hi bit

  Addr = ((UartNo&0x1) << 13) | UART_REG_4;
  testPIOWrite_diag (1, Addr,0x00000003); 	// set clear DLAB bit

  Addr = ((UartNo&0x1) << 13) | UART_REG_3;
  testPIOWrite_diag (1, Addr,0x00000001); 	// set fifo enable bit

  Addr = ((UartNo&0x1) << 13) | UART_REG_2;
  testPIOWrite_diag (1, Addr,0x00000001); 	// set interrupt enable bit
}


/****************************************************************************/
/*                                                                          */
/* Function.. : ethDnld			                          			        */
/*                                                   	                    */
/* Purpose... : function to write/download code to BME memory thru DMA FIFO */
/*				interface in a loop											*/
/*																			*/
/* Parameters : 															*/
/* No		Name		Type				Description						*/
/* --       ----        ----        		-----------						*/
/*	1.		cnt			int		number of times to download code		 	*/
/*                                                                          */
/* Returns... : N/A                                                         */
/*                                                                          */
/* Notes..... : 												            */
/*                                                                          */
/****************************************************************************/
void ethDl (int clkRate,int iMode)
{
	unsigned int numread;
	unsigned int i = 0, codeDnldAddr, iCounter1 = 0, iCounter2 = 0;
	static unsigned char buf[MAX_DATA_LENGTH+1];
	tBmeStruct *bme = &BmeStruct[1];
    int num= 0xea0f8;
    int num1=0;
                   
#ifdef	BME_CPE_20
	sysInfo	sInfo;
#endif
	HostCpuEndianType = 2;
	bme->BMEbaseAddr = 0x08000000;
	bme->BmeState = INACTIVE;
	bme->BmeNum = 1;

#ifdef	BME_CPE_20
 /* 1. release cpu - ReleaseBmeCpu - Inactive state */
	if (clkRate > 0)
	{
		bmeRelCpu (bme, FALSE); /* hold BME cpu */
		iposDelay();
	}
	getSysInfo( (char *) &sInfo);

	switch(sInfo.memInfo)
	{
	case IPOS_BME_MEM_SDRAM32:
		
		init_sdram_32clk ();
		break;
	case IPOS_BME_MEM_SDRAM16:
	
		init_sdram_16clk ();
		break;
	default:
    init_sdram_16clk ();

//	init_sdram_16clkby2 ();
    }
	printf ("\nEMI initialized");

/* 2. Initialize & program DMA channel 15 */
  InitDmaChannel15 (bme);
  printf ("\nDma Channel 15 initialized");
#else	
	/* 1. release cpu - ReleaseBmeCpu - Inactive state */
	if (clkRate > 0)
	{
		bmeRelCpu (bme, FALSE); /* hold BME cpu */
 		bmeHWReset(); /* resetBmeHardware */
  		iposDelay();
  		setclk(bme/*BmeNum*/,clkRate); /* set Bme clock rate */
  		fpgaHWReset(); // reset FPGA
	}

  /* 2. Initialize & program DMA channel 15 */
	InitDmaChannel15 (bme);
	printf ("\nDma Channel 15 initialized");

  /* 3. Initialize & program the EMI interface */
	InitEMIConfig (bme);	
	printf ("\nEMI initialized");
#endif			
  
    /* 4. transfer BME code from file to BME memory */
    bme->BmeState = SW_DNLD_IN_PROGRESS;

  /* Download BME reset vector */
  /* Set jump address in reset vector */
  BmeResetVecCode[4]=BMECodeArray[8];
  BmeResetVecCode[5]=BMECodeArray[9];
  BmeResetVecCode[6]=BMECodeArray[10];
  BmeResetVecCode[7]=BMECodeArray[11];
	
  printf ("\n BmeResetVecCode %x %x %x %x", BmeResetVecCode[4],
          BmeResetVecCode[5], BmeResetVecCode[6], BmeResetVecCode[7]);


  /* set the BME code size & download address - for BME reset vector code */
  bme->BMECodeSize = BME_RESET_VEC_CODE_SIZE; /* 16 */
  bme->BMECodeDnldAddr = BME_RESET_VEC_CODE_DNLD_ADDR;
  bme->dataPtr = &BmeResetVecCode[0];

  if (bmeSoftwareDnld(bme) != HWAPI_DNLD_SUCCESS)
  {
    printf("\nError downloading BME vector!");
    return;
  }
  else
    printf ("\nReset vector code downloaded successfully");

  /* Download BME code */
  printf("\nDownloading BME software");
  codeDnldAddr = BME_CODE_DNLD_ADDR;

  while (num > 0)   {
    if (num >= MAX_DATA_LENGTH+1) {
        numread = MAX_DATA_LENGTH+1;
    }
    else
        numread = num;
    memcpy(&buf[0], &BMECodeArray[num1], numread); 
    num -= numread;
    num1 += numread;

    if ((i++ & 0x3F) == 0)
      printf(".");
    /* transmit data to BME memory */
		bme->destAddr = codeDnldAddr;
		bme->dataLen = numread;
    bme->dataPtr = buf;
    if (DoTxCmd(bme) != HWAPI_WRITE_SUCCESS)		
    {
      iCounter1++;
      if (iCounter1 < MAX_WAIT_COUNT) /* wait for success for 16 times*/
        continue;
      else
      {
        EventLog ("b%dp0 ...bmeSoftwareDnld: Error writing code into memory 0x%lx!", bme->BmeNum,codeDnldAddr);
        bme->dataPtr = &bme->BmeMsg[0];	
        return;
      }
    }
    else
    {
      iCounter1 = 0;
      while (1)
      {
        /* wait for DW_H status bit to be set to indicate DMA Write is done */
        if (bmePollDmaStatus(bme) == DMA_DONE)
        {
          iCounter2 = 0;
          break;
        }
        else
        {
          /* keep a counter or timer. If timer value times out the max
             wait time, break from the loop & report a failure to the api.*/
          iCounter2++;
          if (iCounter2 < MAX_WAIT_COUNT) /* wait for 16/256 times */	
            iposDelay();
          else
          {
            EventLog ("b%dp0 ...bmeSoftwareDnld: Error dma not done while downloading BME code!",bme->BmeNum);
            bme->dataPtr = &bme->BmeMsg[0];	
            return;
          }
        }
      }
			
      /* if DW_T bit is set by DMA controller, change the BmeDnldVerifyState
         from DW_T_BIT_RESET to DW_T_BIT_SET */
      if (bme->BmeDnldVerifyState == DW_T_BIT_RESET)
      {
        /* check the status[1] (DW_T) bit of DMA Status Port to be one. 
           This indicates data is transferred from the fifo to BME memory. */
        if (bmeRdStatus(bme) & DW_T_DMA_WRITE_DONE)
          bme->BmeDnldVerifyState = DW_T_BIT_SET;
      }	

      /* This code reads back each block of written data & checks for written data validity. 
         It compares each read byte with each write byte & gives an error, if both are not
         matching */			

      if (bmeVerifyWriteCode (bme, codeDnldAddr, numread, buf) !=
				  HWAPI_CODE_CHKSUM_SUCCESS)
      {
        EventLog ("b%d,p0 ...bmeSoftwareDnld: Error while reading data.\n",
								  bme->BmeNum);
        return;
      }
    }

    codeDnldAddr += numread; /* increment the code download addr */
  } 

  EventLog("Bme software code downloaded successfully");
	printf ("\nBme software code downloaded successfully");

	/* change the BmeState from SW_DNLD_IN_PROGRESS to SW_DNLD_DONE */
	bme->BmeState = SW_DNLD_DONE;
	bme->dataPtr = &bme->BmeMsg[0];	

  /* 5. set production mode to development */
  setProdMode(iMode);

	/* 6. release cpu - ReleaseBmeCpu - active state */
	bmeRelCpu (bme,TRUE);  /* release BME cpu */

	printf ("\n************************************");
	printf ("\nDownload completed!");
	printf ("\n************************************\n");

	oampBmeStateUpdate(1, eBME_START);		

	iposStateUpdate(&apiBme[1], IPOS_BME_STARTED);

	return;
}
#endif
