#include <FW_cardinfo/FW_cardinfo_api.h>
#include <FW_flash/FW_flash_api.h>
#include <FW_lt/FW_lt_api.h>
#include <fcntl.h>

#include <umap_type.h>

#define msg(x,y)  printf(x,y);

#define EC_SHIFT(x)     (((((x)&0xfe)<<2)|((x)&1)) + 0x1f202800)
#define EC0_CMD	        EC_SHIFT(0x00)  /* The command register (for all pages) */
#define EC0_CLDALO	EC_SHIFT(0x01)	/* Low byte of current local dma addr  RD */
#define EC0_CLDAHI	EC_SHIFT(0x02)	/* High byte of current local dma addr  RD */
#define EN0_BOUNDARY	EC_SHIFT(0x03)	/* Boundary page of ring bfr RD WR */
#define EC0_TSR		EC_SHIFT(0x04)	/* Transmit status reg RD */
#define EC0_NCR		EC_SHIFT(0x05)	/* Number of collision reg RD */
#define EC0_FIFO	EC_SHIFT(0x06)	/* FIFO RD */
#define EN0_ISR		EC_SHIFT(0x07)	/* Interrupt status reg RD WR */
#define EC0_CRDALO	EC_SHIFT(0x08)	/* low byte of current remote dma address RD */
#define EC0_CRDAHI	EC_SHIFT(0x09)	/* high byte, current remote dma address RD */
#define EC0_RSR		EC_SHIFT(0x0c)	/* rx status reg RD */
#define EC0_COUNTER0	EC_SHIFT(0x0d)	/* Rcv alignment error counter RD */
#define EC0_COUNTER1	EC_SHIFT(0x0e)	/* Rcv CRC error counter RD */
#define EC0_COUNTER2	EC_SHIFT(0x0f)	/* Rcv missed frame error counter RD */
#define EC0_DATAPORT    EC_SHIFT(0x10)	/* NatSemi-defined port window offset. */
#define EC0_RESET       EC_SHIFT(0x1f)	/* Issue a read to reset, a write to clear. */

#define EC0_MEMR        EC_SHIFT(0x14) /* MII/EEPROM Management register */

typedef int (*p_func)();

typedef struct 
{
  p_func func;
} pro_f;

int decode(char *cmd, int *int_cmd, int size);
int p_SRAM();
int p_LtType();
int p_LtVersion();
int p_LtUtopia();
int p_LtSysLED();
int p_LtDslLED();
int p_NtTemperature();
int p_LtTemperature();
int p_NtInformation();
int p_LtInformation();
int p_NtVersion();
int p_BpVersion();
int p_CardEquipment();
int p_HouseKeeping();
int p_FibreStatus();
int p_AcoButton();
int p_NtAlarmLED();
int p_NtSysLED();
int p_FanErrorSignal();
int p_LtHostBus();
int p_LtSwRst();
int p_AlarmOutput();
int p_NtAllBus();
int p_EthCtrlRst();
int p_FanCtrl();
int p_GiGaPHY();
int p_ReadFlash();
int p_WriteFlash();
int p_EraseFlash();
int p_MipsReset();
int p_test();


int main()
{
	char cmd[3];
	int rc,int_cmd;
	pro_f pro[] = {
			p_SRAM,           p_LtType,         p_LtVersion,
			p_LtUtopia,       p_LtSysLED,       p_LtDslLED,
			p_NtTemperature,  p_LtTemperature,  p_NtInformation,
			p_LtInformation,  p_NtVersion,      p_BpVersion,
			p_CardEquipment,  p_HouseKeeping,   p_FibreStatus,
			p_AcoButton,      p_NtAlarmLED,     p_NtSysLED,
			p_FanErrorSignal, p_LtHostBus,      p_LtSwRst,
			p_AlarmOutput,    p_NtAllBus,       p_EthCtrlRst,
			p_FanCtrl,        p_GiGaPHY,        p_ReadFlash,
			p_WriteFlash,     p_EraseFlash,     p_MipsReset,
			p_test,
		      };
	
	printf("Welcome to alfred test program for R3.1\n");
	
	while(1){
		printf("\nAlfred>");
		scanf("%s",cmd);
		printf("\n");

		if(cmd[0] == 'q')
			break;
	
		rc = decode(cmd, &int_cmd, 2);
		
		if(int_cmd > sizeof(pro)/sizeof(pro_f))
			printf("Invalid command,enter <h> to get help\n");
		else
			if(!rc){
				msg("Function %d go\n",int_cmd);
				pro[int_cmd].func();
			}
		
	}
	
	return 0;
}
/*---------------------------------------------------
      decode for input cmd
-----------------------------------------------------*/
int decode(char *cmd, int *int_cmd, int size)
{
	int idx;
	
	*int_cmd = 0;
	
	for(idx=0;idx<size;idx++){
		switch(*(cmd + idx)){
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				*int_cmd *= 10;
				*int_cmd += *(cmd + idx) - 0x30;
				break;
			case 'h':
				printf("Alfred test command:\n");
				printf("h :help\n");
				printf("q :quit\n");
				printf("00:SRAM check\n");
				printf("01:Get Lt card Type\n");
				printf("02:Get Lt card version\n");
				printf("03:Lt card Utopia functions\n");
				printf("04:Lt card system LED functions\n");
				printf("05:Lt card xDSL LED functions\n");
				printf("06:Nt card temperature functions\n");
				printf("07:Lt card temperature functions\n");
				printf("08:Nt card information functions\n");
				printf("09:Lt card information functions\n");
				printf("10:Get Nt card version\n");
				printf("11:Get back-plane version\n");
				printf("12:Get Lt card x equipment\n");
				printf("13:Get house keeping\n");
				printf("14:Get fibre status\n");
				printf("15:Get ACO button\n");
				printf("16:Nt card alarm LED functions\n");
				printf("17:Nt card system LED functions\n");
				printf("18:Get FAN error signal\n");
				printf("19:Lt card x host bus enable or disable\n");
				printf("20:Lt card x software reset\n");
				printf("21:Set alarm\n");
				printf("22:Nt card all bus enable or disable\n");
				printf("23:Ethernet Controller reset\n");
				printf("24:FAN turn on or turn off\n");
				printf("25:GiGa PHY reset\n");
				printf("26:Flash Read\n");
				printf("27:Flash Write\n");
				printf("28:Flash Erase\n");
				printf("29:MIPS Reset\n");
				printf("30:Test\n");
				//printf("xx :RTC functions\n");
				return -1;
			default:
				printf("Invalid command,enter <h> to get help\n");
				return -1;
		}//switch
	}//for
	return 0;
}
/*-------------------------------------------------------
         SRAM check function
---------------------------------------------------------*/
int p_SRAM()
{
	unsigned char value,buf[1024];
	int rc,i,idx,times;
	
	printf("SRAM check test...\n");
	
	times = 2;
	value = 0x55;
	do{
		for(i=0;i<1024;i++)
			buf[i] = value;
		for(idx=0;idx<1024;idx++)
			if((rc = FW_WriteMainBus(HOST_SRAM_ADDR + (idx * 0x400), 1024, buf))){
				printf("Write SRAM function error\n");
				return rc;
			}
		for(idx=0;idx<1024;idx++){
			if((rc = FW_ReadMainBus(HOST_SRAM_ADDR + (idx * 0x400), 1024, buf))){
				printf("Read SRAM function errorr\n");
				return rc;
			}
			for(i=0;i<1024;i++)
				if(buf[i] != value){
					printf("Error Address:%x ,Write Value:%x\n",(idx * 0x400) + i,value);
					return -1;
				}
		}//for
		times--;
		value = 0xaa;
	}while(times != 0);
	
	printf("OK\n");
	return rc;
}
/*------------------------------------------------------------
          RTC test function
--------------------------------------------------------------/
int p_RTC()
{
	struct rtc time_value;
	int rc,sel,buf;
	
	printf("RTC test function...\n");
	rc = FW_CheckRtcSetting();
	if(rc){
		printf("Check RTC Setting error\n");
		return rc;
	}
	printf("1 : Get time from RTC\n");
	printf("2 : Set time to RTC\n");
	scanf("%d",sel);
	printf("\n");
	switch(sel){
		case 1://get
			rc = FW_GetRtc(&time_value);
			if(rc){
				printf("Get time from RTC error\n");
				return -1;	
			}
			printf("\n%d/%d/%d\n",
				(time_value.century - 1) * 100 + time_value.year,
				time_value.month,
				time_value.day);
			printf("Day of week(1=Sunday...7=Saturday) : %d\n",time_value.day_of_week);
			if(time_value.am_pm == 0)//am
				printf("AM-%d:%d:%d\n",time_value.hour,time_value.minute,time_value.second);
			else //pm
				printf("PM-%d:%d:%d\n",time_value.hour,time_value.minute,time_value.second);
			break;
		case 2://set
			printf("\nInput year>");
			scanf("%d",buf);
			time_value.century = (buf/100) + 1;
			time_value.year = buf - (time_value.century - 1) * 100;
			printf("\nInput month>");
			scanf("%d",time_value.month);
			printf("\nInput day>");
			scanf("%d",time_value.day);
			printf("\nInput day of week(1=Sunday...7=Saturday)>");
			scanf("%d",time_value.day_of_week);
			printf("\nInput AM:0 or PM:1>");
			scanf("%d",time_value.am_pm);
			printf("\nInput hour>");
			scanf("%d",time_value.hour);
			printf("\nInput minute>");
			scanf("%d",time_value.minute);
			printf("\nInput second>");
			scanf("%d",time_value.second);
			rc = FW_SetRtc(time_value);
			if(rc){
				printf("\nSet time to RTC error\n");
				return -1;	
			}
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
*/
/*------------------------------------------------------------
           Get Lt card type
--------------------------------------------------------------*/
int p_LtType()
{
	int rc,x;
	char cmd[2];
	unsigned char type;
	
	printf("Get Lt card type...\n");
	printf("Input card number(0~4)>");
	scanf("%s",cmd);
	decode(cmd, &x, 1);
	rc = FW_GetLtCardType(CARD_TYPE_ADDR(x), &type);
	if(!rc)
		printf("\nLt card %d type : %x\n",x,type);
	else
		printf("\nGet Lt card type function error\n");
	return rc;
}
/*-----------------------------------------------------------
           Get Lt card version
-------------------------------------------------------------*/
int p_LtVersion()
{
	int rc,x;
	char cmd[2];
	unsigned char version;
	
	printf("Get Lt card version...\n");
	printf("Input card number(0~4)>");
	scanf("%s",cmd);
	decode(cmd, &x, 1);
	rc = FW_GetLtCardType(CARD_VERSION_ADDR(x), &version);
	if(!rc)
		printf("\nLt card %d version : %x\n",x,version);
	else
		printf("\nGet Lt card version function error\n");
	return rc;	
}
/*------------------------------------------------------------
           Lt card Utopia bus enable or disable
--------------------------------------------------------------*/
int p_LtUtopia()
{
	int sel,rc,x,en;
	char cmd[2];

	
	printf("Lt card Utopia bus test...\n");
	printf("1 : Get Lt card Utopia bus status\n");
	printf("2 : Set Lt card Utopia bus enable or disable\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			printf("Input card number(0~4)>");
			scanf("%s",cmd);
			decode(cmd, &x, 1);
			rc = FW_GetLtCardUtopia(UTOPIA_ENABLE_ADDR(x), (unsigned char*)&en);
			if(rc){
				printf("\nGet Lt card Utopia bus status error\n");
				return -1;
			}
			if(((en >> 24) & 0x01) == 0x0)//en=0
				printf("\nLt card %d Utopia status : enable\n",x);
			else
				printf("\nLt card %d Utopia status : disable\n",x);
			break;
		case 2: //set
			printf("Input card number(0~4)>");
			scanf("%s",cmd);
			decode(cmd, &x, 1);
			printf("\nInput 0:enable or 1:disable>");
			scanf("%s",cmd);
			decode(cmd, &en, 1);
			rc = FW_SetLtCardUtopia(UTOPIA_ENABLE_ADDR(x), en & 0x01);
			if(rc){
				printf("\nSet Lt card Utopia bus error\n");
				return -1;
			}
			if((en & 0x01) == 0x0)//en=0
				printf("\nSet Lt card %d Utopia status enable successful\n",x);
			else
				printf("\nSet Lt card %d Utopia status disable successful\n",x);
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*-----------------------------------------------------------------
               Lt card system LED
-------------------------------------------------------------------*/
int p_LtSysLED()
{
	int sel,rc,x,sys;
	char cmd[2];

	
	printf("Lt card system LED test...\n");
	printf("1 : Get Lt card system LED status\n");
	printf("2 : Set Lt card system LED\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			printf("Input card number(0~4)>");
			scanf("%s",cmd);
			decode(cmd, &x, 1);
			rc = FW_GetLtSystemLed(LT_LED_CONTROL_ADDR(x), (unsigned char*)&sys);
			if(rc){
				printf("\nGet Lt card system LED status error\n");
				return -1;
			}
			switch((sys >> 24) & 0x03){
				case 0: printf("\nLt card %d system LED : TURN OFF\n",x);
					break;
				case 1: printf("\nLt card %d system LED : RED\n",x);
					break;
				case 2: printf("\nLt card %d system LED : GREEN\n",x);
					break;
				case 3: printf("\nLt card %d system LED : YELLOW\n",x);
					break;
				default:
					printf("\nFunction return invalid value\n");
					return -1;
			}
			break;
		case 2: //set
			printf("Input card number(0~4)>");
			scanf("%s",cmd);
			decode(cmd, &x, 1);
			printf("\nInput 0:TURN OFF, 1:RED, 2:GREEN, 3:YELLOW>");
			scanf("%s",cmd);
			decode(cmd, &sys, 1);
			rc = FW_SetLtSystemLed(LT_LED_CONTROL_ADDR(x), sys & 0x03);
			if(rc){
				printf("\nSet Lt card system LED error\n");
				return -1;
			}
			switch(sys & 0x03){
				case 0: printf("\nSet Lt card %d system LED TURN OFF successful\n",x);
					break;
				case 1: printf("\nSet Lt card %d system LED RED successful\n",x);
					break;
				case 2: printf("\nSet Lt card %d system LED GREEN successful\n",x);
					break;
				case 3: printf("\nSet Lt card %d system LED YELLOW successful\n",x);
					break;
				default:
					printf("\nInput invalid value\n");
					return -1;
			}
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*-----------------------------------------------------------------
              Lt card DSL LED
-------------------------------------------------------------------*/
int p_LtDslLED()
{
	int sel,rc,x,dsl;
	char cmd[2];

	
	printf("Lt card DSL LED test...\n");
	printf("1 : Get Lt card DSL LED status\n");
	printf("2 : Set Lt card DSL LED\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			printf("Input card number(0~4)>");
			scanf("%s",cmd);
			decode(cmd, &x, 1);
			rc = FW_GetLtDslLed(LT_LED_CONTROL_ADDR(x), (unsigned char*)&dsl);
			if(rc){
				printf("\nGet Lt card DSL LED status error\n");
				return -1;
			}
			switch((dsl >> 24) & 0x03){
				case 0: printf("\nLt card %d DSL LED : TURN OFF\n",x);
					break;
				case 1: printf("\nLt card %d DSL LED : RED\n",x);
					break;
				case 2: printf("\nLt card %d DSL LED : GREEN\n",x);
					break;
				case 3: printf("\nLt card %d DSL LED : YELLOW\n",x);
					break;
				default:
					printf("\nFunction return invalid value\n");
					return -1;
			}
			break;
		case 2: //set
			printf("Input card number(0~4)>");
			scanf("%s",cmd);
			decode(cmd, &x, 1);
			printf("\nInput 0:TURN OFF, 1:RED, 2:GREEN, 3:YELLOW>");
			scanf("%s",cmd);
			decode(cmd, &dsl, 1);
			rc = FW_SetLtDslLed(LT_LED_CONTROL_ADDR(x), dsl & 0x03);
			if(rc){
				printf("\nSet Lt card DSL LED error\n");
				return -1;
			}
			switch(dsl & 0x03){
				case 0: printf("\nSet Lt card %d DSL LED TURN OFF successful\n",x);
					break;
				case 1: printf("\nSet Lt card %d DSL LED RED successful\n",x);
					break;
				case 2: printf("\nSet Lt card %d DSL LED GREEN successful\n",x);
					break;
				case 3: printf("\nSet Lt card %d DSL LED YELLOW successful\n",x);
					break;
				default:
					printf("\nInput invalid value\n");
					return -1;
			}
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*-----------------------------------------------------------
         Nt card temperature
-------------------------------------------------------------*/
int p_NtTemperature()
{
	int sel,rc,temperature;
	char cmd[5];
	unsigned int reg_addr;
	
	printf("Nt card temperature test...\n");
	printf("1 : Get Nt card temperature status\n");
	printf("2 : Set Nt card temperature\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			printf("Input Register Address 0:temperature, 2:tHYST, 3:tOS");
			scanf("%s",cmd);
			decode(cmd, &reg_addr, 1);
			rc = FW_GetNtTemperature(&temperature, reg_addr);
			if(rc){
				printf("\nGet Nt card temperature status error\n");
				return -1;
			}
			printf("\nGet Register %x value : %d\n",reg_addr,temperature);
			break;
		case 2: //set
			printf("Input temperature(-125~125) 3 bytes number>");
			scanf("%s",cmd);
			if(cmd[0] == '-'){
				cmd[0]=cmd[1]; cmd[1]=cmd[2]; cmd[2]=cmd[3];
				decode(cmd, &temperature, 3);
				temperature *= -1;
			}
			else
				decode(cmd, &temperature, 3);
			printf("Input Register Address 2:tHYST, 3:tOS");
			scanf("%s",cmd);
			decode(cmd, &reg_addr, 1);
			rc = FW_SetNtTemperature(temperature, reg_addr);
			if(rc){
				printf("\nSet Nt card temperature error\n");
				return -1;
			}
			printf("\nSet Register %x value : %d successful\n",reg_addr,temperature);
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*----------------------------------------------------------------
             Lt card temperature
------------------------------------------------------------------*/
int p_LtTemperature()
{
	int sel,rc,temperature;
	char cmd[5];
	unsigned int reg_addr;
	
	printf("Lt card temperature test...\n");
	printf("1 : Get Lt card temperature status\n");
	printf("2 : Set Lt card temperature\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			printf("Input Register Address 0:temperature, 2:tHYST, 3:tOS");
			scanf("%s",cmd);
			decode(cmd, &reg_addr, 1);
			rc = FW_GetLtTemperature(&temperature, reg_addr);
			if(rc){
				printf("\nGet Lt card temperature status error\n");
				return -1;
			}
			printf("\nGet Register %x value : %d\n",reg_addr,temperature);
			break;
		case 2: //set
			printf("Input temperature(-125~125)>");
			scanf("%s",cmd);
			if(cmd[0] == '-'){
				cmd[0]=cmd[1]; cmd[1]=cmd[2]; cmd[2]=cmd[3];
				decode(cmd, &temperature, 3);
				temperature *= -1;
			}
			else
				decode(cmd, &temperature, 3);
			printf("Input Register Address 2:tHYST, 3:tOS");
			scanf("%s",cmd);
			decode(cmd, &reg_addr, 1);
			rc = FW_SetLtTemperature(temperature, reg_addr);
			if(rc){
				printf("\nSet Lt card temperature error\n");
				return -1;
			}
			printf("\nSet Register %x value : %d successful\n",reg_addr,temperature);
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*-------------------------------------------------------------
         Nt card information
---------------------------------------------------------------*/
int p_NtInformation()
{
	int sel,rc,i;
	char invInfo[256],cmd[2];
	
	printf("Nt card inventory test...\n");
	printf("1 : Get Nt card inventory\n");
	printf("2 : Set Nt card inventory\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			rc = FW_GetNtInventory(invInfo);
			if(rc){
				printf("\nGet Nt card inventory error\n");
				return -1;
			}
			printf("\n");
			for(i=0;i<64;i++)
				printf("%1x, %1x, %1x, %1x\n",
					invInfo[i*4],invInfo[i*4+1],invInfo[i*4+2],invInfo[i*4+3]);
			break;
		case 2: //set
			printf("Input 256 bytes Information>");
			scanf("%256c",invInfo);
			rc = FW_SetNtInventory(invInfo);
			if(rc){
				printf("\nSet Nt card inventory error\n");
				return -1;
			}
			printf("\nSet Nt card inventory successful\n");
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*----------------------------------------------------------------
              Lt card information
------------------------------------------------------------------*/
int p_LtInformation()
{
	int sel,rc,x,i;
	char invInfo[256],cmd[2];
	
	printf("Lt card inventory test...\n");
	printf("1 : Get Lt card inventory\n");
	printf("2 : Set Lt card inventory\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			printf("Input card number(0~4)>");
			scanf("%s",cmd);
			decode(cmd, &x, 1);
			rc = FW_GetLtInventory(x, invInfo);
			if(rc){
				printf("\nGet Lt card inventory error\n");
				return -1;
			}
			printf("\n");
			for(i=0;i<64;i++)
				printf("%1x, %1x, %1x, %1x\n",
					invInfo[i*4],invInfo[i*4+1],invInfo[i*4+2],invInfo[i*4+3]);
			break;
		case 2: //set
			printf("Input card number(0~4)>");
			scanf("%s",cmd);
			decode(cmd, &x, 1);
			printf("Input 256 bytes Information>");
			scanf("%256c",invInfo);
			rc = FW_SetLtInventory(x, invInfo);
			if(rc){
				printf("\nSet Lt card inventory error\n");
				return -1;
			}
			printf("\nSet Lt card inventory successful\n");
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*----------------------------------------------------------------
          Get Nt card version
------------------------------------------------------------------*/
int p_NtVersion()
{
	int rc;
	unsigned char nt_version;
	
	printf("Get Nt card version test...\n");
	rc = FW_GetNtVersion(&nt_version);
	if(!rc)
		printf("Nt card version : %d\n",nt_version);
	else
		printf("Get Nt card version error\n");
	return rc;
}
/*----------------------------------------------------------------
           Get back-plane version
------------------------------------------------------------------*/
int p_BpVersion()
{
	int rc;
	unsigned char bp_version;
	
	printf("Get back-plane version test...\n");
	rc = FW_GetBpVersion(&bp_version);
	if(!rc)
		printf("Back-plane version : %d\n",bp_version);
	else
		printf("Get Nt card version error\n");
	return rc;
}
/*----------------------------------------------------------------
           Get Lt card equipment
------------------------------------------------------------------*/
int p_CardEquipment()
{
	int rc,x;
	char cmd[2];
	unsigned char card_equip;
	
	printf("Get Lt card equipment test...\n");
	printf("Input card number(0~4)>");
	scanf("%s",cmd);
	decode(cmd, &x, 1);
	printf("\n");
	switch(x){
		case 0: rc = FW_GetBpAllCardEquip0(&card_equip); break;
		case 1: rc = FW_GetBpAllCardEquip1(&card_equip); break;
		case 2: rc = FW_GetBpAllCardEquip2(&card_equip); break;
		case 3: rc = FW_GetBpAllCardEquip3(&card_equip); break;
		case 4: rc = FW_GetBpAllCardEquip4(&card_equip); break;
		default: printf("Invalid value\n");  return -1;
	}
	if(!rc)
		if(card_equip)    printf("Lt card %d Equipment\n",x);//1:equipment
		else              printf("Lt card %d Un-Equipment\n",x);
	else
		printf("Get Lt card equipment error\n");
	
	return rc;
}
/*----------------------------------------------------------------
              Get house keeping
------------------------------------------------------------------*/
int p_HouseKeeping()
{
	int rc;
	unsigned char hk;
	
	printf("Get house keeping test...\n");
	
	rc = FW_GetBpHk(&hk);
	if(rc) {printf("Get house keeping error\n"); return -1;}
		
	if((hk & 0x01) == 0x01) printf("House keeping 1 no event\n");//1:no event
	else                    printf("House keeping 1 event\n");
	if((hk & 0x02) == 0x02) printf("House keeping 2 no event\n");//1:no event
	else                    printf("House keeping 2 event\n");
	if((hk & 0x04) == 0x04) printf("House keeping 3 no event\n");//1:no event
	else                    printf("House keeping 3 event\n");
	
	return rc;
}
/*-----------------------------------------------------------------
               Get fibre status
-------------------------------------------------------------------*/
int p_FibreStatus()
{
	int rc;
	unsigned char fibre1,fibre2;
	
	printf("Get Fibre status test...\n");
	
	rc = FW_GetFibreOpticsUnit1(&fibre1);
	if(!rc)
		rc = FW_GetFibreOpticsUnit2(&fibre2);
	if(rc) {printf("Get Fibre status error\n"); return -1;}
	
	if(fibre1) printf("Fibre 1 NO active\n");//1:no active
	else       printf("Fibre 1 active\n");
	if(fibre2) printf("Fibre 2 NO active\n");//1:no active
	else       printf("Fibre 2 active\n");
	
	return rc;
}
/*-----------------------------------------------------------------
            Get ACO button status
-------------------------------------------------------------------*/
int p_AcoButton()
{
	int rc;
	unsigned char aco;
	
	printf("Get ACO button status test...\n");
	
	rc = FW_GetAcoButton(&aco);
	if(rc){printf("Get ACO button status error\n"); return -1;}
	
	if(aco) printf("NO push ACO button\n");//no push
	else    printf("Push ACO button\n");
	
	return rc;
}
/*-----------------------------------------------------------------
            Nt card alarm LED
-------------------------------------------------------------------*/
int p_NtAlarmLED()
{
	int sel,rc,alarm;
	char cmd[2];

	
	printf("Nt card alarm LED test...\n");
	printf("1 : Get Nt card alarm LED status\n");
	printf("2 : Set Nt card alarm LED\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			rc = FW_GetNtAlarmLed((unsigned char*)&alarm);
			msg("%x\n",alarm >> 24);
			if(rc){
				printf("\nGet Nt card alarm LED status error\n");
				return -1;
			}
			switch((alarm >> 24) & 0x03){
				case 0: printf("\nNt card alarm LED : TURN OFF\n");
					break;
				case 1: printf("\nNt card alarm LED : RED\n");
					break;
				case 2: printf("\nNt card alarm LED : GREEN\n");
					break;
				case 3: printf("\nNt card alarm LED : YELLOW\n");
					break;
				default:
					printf("\nFunction return invalid value\n");
					return -1;
			}
			break;
		case 2: //set
			printf("\nInput 0:TURN OFF, 1:RED, 2:GREEN, 3:YELLOW>");
			scanf("%s",cmd);
			decode(cmd, &alarm, 1);
			msg("%x\n",alarm);
			rc = FW_SetNtAlarmLed(alarm);
			if(rc){
				printf("\nSet Nt card alarm LED error\n");
				return -1;
			}
			switch(alarm & 0x03){
				case 0: printf("\nSet Nt card alarm LED TURN OFF successful\n");
					break;
				case 1: printf("\nSet Nt card alarm LED RED successful\n");
					break;
				case 2: printf("\nSet Nt card alarm LED GREEN successful\n");
					break;
				case 3: printf("\nSet Nt card alarm LED YELLOW successful\n");
					break;
				default:
					printf("\nInput invalid value\n");
					return -1;
			}
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*---------------------------------------------------------------
           Nt card system LED
-----------------------------------------------------------------*/
int p_NtSysLED()
{
	int sel,rc,sys;
	char cmd[2];

	
	printf("Nt card system LED test...\n");
	printf("1 : Get Nt card system LED status\n");
	printf("2 : Set Nt card system LED\n");
	scanf("%s",cmd);
	decode(cmd, &sel, 1);
	printf("\n");
	switch(sel){
		case 1: //get
			rc = FW_GetNtSystemLed((unsigned char*)&sys);
			if(rc){
				printf("\nGet Nt card system LED status error\n");
				return -1;
			}
			switch((sys >> 24) & 0x03){
				case 0: printf("\nNt card system LED : TURN OFF\n");
					break;
				case 1: printf("\nNt card system LED : RED\n");
					break;
				case 2: printf("\nNt card system LED : GREEN\n");
					break;
				case 3: printf("\nNt card system LED : YELLOW\n");
					break;
				default:
					printf("\nFunction return invalid value\n");
					return -1;
			}
			break;
		case 2: //set
			printf("\nInput 0:TURN OFF, 1:RED, 2:GREEN, 3:YELLOW>");
			scanf("%s",cmd);
			decode(cmd, &sys, 1);
			rc = FW_SetNtSystemLed(sys);
			if(rc){
				printf("\nSet Nt card system LED error\n");
				return -1;
			}
			switch(sys & 0x03){
				case 0: printf("\nSet Nt card system LED TURN OFF successful\n");
					break;
				case 1: printf("\nSet Nt card system LED RED successful\n");
					break;
				case 2: printf("\nSet Nt card system LED GREEN successful\n");
					break;
				case 3: printf("\nSet Nt card system LED YELLOW successful\n");
					break;
				default:
					printf("\nInput invalid value\n");
					return -1;
			}
			break;
		default:
			printf("Invalid value\n");
			return -1;
	}
	return rc;
}
/*-----------------------------------------------------------
             Get FAN error signal
-------------------------------------------------------------*/
int p_FanErrorSignal()
{
	int rc;
	unsigned char err_sig;
	
	printf("Get FAN error signal test...\n");
	
	rc = FW_GetBpFanErrsig(&err_sig);
	if(rc){printf("Get FAN error signal error\n"); return -1;}
	
	if(err_sig) printf("FAN ERROR\n");//1:FAN error
	else        printf("FAN normal\n");
	
	return rc;
}
/*-----------------------------------------------------------
             Set Lt card host bus
-------------------------------------------------------------*/
int p_LtHostBus()
{
	int rc,x,en;
	char cmd[2];

	
	printf("Set Lt card host bus test...\n");
	printf("Input card number(0~4)>");
	scanf("%s",cmd);
	decode(cmd, &x, 1);
	printf("\nInput 1:enable or 0:disable>");
	scanf("%s",cmd);
	decode(cmd, &en, 1);
	printf("\n");
	switch(x){
		case 0: rc = FW_SetBpHostBusControl0(en); break;
		case 1: rc = FW_SetBpHostBusControl1(en); break;
		case 2: rc = FW_SetBpHostBusControl2(en); break;
		case 3: rc = FW_SetBpHostBusControl3(en); break;
		case 4: rc = FW_SetBpHostBusControl4(en); break;
		default: printf("Invalid value\n");  return -1;
	}
	if(!rc)
		if(en)    printf("Set Lt card %d host bus disable successful\n",x);//1:disable
		else      printf("Set Lt card %d host bus enable successful\n",x);
	else
		printf("Set Lt card host bus error\n");
	
	return rc;
}
/*---------------------------------------------------------
           Set Lt card software reset
-----------------------------------------------------------*/
int p_LtSwRst()
{
	int rc,x,rst;
	char cmd[2];

	
	printf("Set Lt card software reset test...\n");
	printf("Input card number(0~4)>");
	scanf("%s",cmd);
	decode(cmd, &x, 1);
	printf("\nInput 0:reset or 1:normal>");
	scanf("%s",cmd);
	decode(cmd, &rst, 1);
	printf("\n");
	switch(x){
		case 0: rc = FW_SetBpSwRst0(rst); break;
		case 1: rc = FW_SetBpSwRst1(rst); break;
		case 2: rc = FW_SetBpSwRst2(rst); break;
		case 3: rc = FW_SetBpSwRst3(rst); break;
		case 4: rc = FW_SetBpSwRst4(rst); break;
		default: printf("Invalid value\n");  return -1;
	}
	if(!rc)
		if(rst)    printf("Set Lt card %d normal successful\n",x);//1:normal
		else       printf("Set Lt card %d reset successful\n",x);
	else
		printf("Set Lt card software reset error\n");
	
	return rc;
}
/*--------------------------------------------------------------------
              Alarm output
----------------------------------------------------------------------*/
int p_AlarmOutput()
{
	int rc,alarm1,alarm2;
	char cmd[2];
	
	printf("Set alarm output test...\n");
	
	printf("Input alarm 1 value 1:NO alarm or 0:alarm>");
	scanf("%s",cmd);
	decode(cmd, &alarm1, 1);
	rc = FW_SetAlarm1(alarm1);
	if(rc) {printf("\nSet alarm 1 output error\n"); return -1;}
	
	printf("Input alarm 2 value 1:NO alarm or 0:alarm>");
	scanf("%s",cmd);
	decode(cmd, &alarm2, 1);
	rc = FW_SetAlarm2(alarm2);
	if(rc) {printf("\nSet alarm 2 output error\n"); return -1;}
	
	if(alarm1) printf("\nSet alarm 1 NO alarm successful\n");//1:no alarm
	else       printf("\nSet alarm 1 alarm successful\n");
	if(alarm2) printf("\nSet alarm 2 NO alarm successful\n");//1:no alarm
	else       printf("\nSet alarm 2 alarm successful\n");
	
	return rc;
}
/*------------------------------------------------------------------
            Set Nt card all bus
--------------------------------------------------------------------*/
int p_NtAllBus()
{
	int rc,en;
	char cmd[2];

	
	printf("Set Nt card all bus test...\n");
	
	printf("Input 0:enable or 1:disable>");
	scanf("%s",cmd);
	decode(cmd, &en, 1);
	rc = FW_SetAllBusEn(en);
	if(rc) {printf("\nSet Nt card all bus error\n"); return -1;}
	
	if(en) printf("\nSet Nt card all bus disable successful\n");//1:disable
	else   printf("\nSet Nt card all bus enable successful\n");

	return rc;	
}
/*-----------------------------------------------------------------
            Set ethernet controller reset
-------------------------------------------------------------------*/
int p_EthCtrlRst()
{
	int rc,rst;
	char cmd[2];

	
	printf("Set ethernet controller reset test...\n");
	
	printf("Input 0:normal or 1:reset>");
	scanf("%s",cmd);
	decode(cmd, &rst, 1);
	rc = FW_SetEthRst(rst);
	if(rc) {printf("\nSet ethernet controller reset error\n"); return -1;}
	
	if(rst) printf("\nSet ethernet controller RESET successful\n");//1:reset
	else    printf("\nSet ethernet controller Normal successful\n");

	return rc;	
}
/*----------------------------------------------------------------
           FAN turn on or turn off
------------------------------------------------------------------*/
int p_FanCtrl()
{
	int rc,en;
	char cmd[2];

	
	printf("FAN turn on or turn off test...\n");
	
	printf("Input 0:turn ON or 1:turn OFF>");
	scanf("%s",cmd);
	decode(cmd, &en, 1);
	rc = FW_SetBpFanEn(en);
	if(rc) {printf("\nFAN turn on or turn off error\n"); return -1;}
	
	if(en) printf("\nFAN TURN OFF successful\n");//1:turn off
	else   printf("\nFAN TURN ON successful\n");

	return rc;	
}
/*------------------------------------------------------------------
            Set GiGa PHY reset
--------------------------------------------------------------------*/
int p_GiGaPHY()
{
	int rc,rst;
	char cmd[2];

	
	printf("Set GiGa PHY reset test...\n");
	
	printf("Input 0:reset or 1:normal>");
	scanf("%s",cmd);
	decode(cmd, &rst, 1);
	rc = FW_SetGigaPhyRst(rst);
	if(rc) {printf("\nSet GiGa PHY reset error\n"); return -1;}
	
	if(rst) printf("\nSet GiGa PHY Normal successful\n");//1:normal
	else    printf("\nSet GiGa PHY RESET successful\n");

	return rc;	
}
/*-----------------------------------------------------------------
                  Read from flash
-------------------------------------------------------------------*/
int p_ReadFlash()
{
	int rc,i;
	unsigned short data[20];
	
	printf("Read Flash test...\n");

	rc = FW_ReadFlash(FLASH_BASE_ADDRESS + PARTITION_ONE_OFFSET, 20, data);

	if(rc) {printf("Read Flash error\n"); return -1;}
	
	for(i=0;i<20;i++)
		printf("%x\n",data[i]);
//*/

	return rc;
}
/*-----------------------------------------------------------------
                  Write from flash
-------------------------------------------------------------------*/
int p_WriteFlash()
{
	int rc,i;
	struct flash_option_info info;
	
	printf("Write Flash test...\n");
	
	info.type = SECTOR_PROGRAM;
	info.addr_offset = (SECTOR_OFFSET(4) / 2);
	info.word_size = 1;
	for(i=0;i<10;i++)
		info.data[i] = 2;


	rc = FW_WriteFlash(FLASH_BASE_ADDRESS, 1, &info);

	if(rc) {printf("Write Flash error\n"); return -1;}

	return rc;
}
/*-----------------------------------------------------------------
                  Erase flash
-------------------------------------------------------------------*/
int p_EraseFlash()
{
	int rc;
	struct flash_option_info info;
	
	printf("Erase Flash test...\n");
	
	info.type = SECTOR_ERASE;
	info.sector_offset = SECTOR_OFFSET(4);
	info.sector_size = 1;

	rc = FW_WriteFlash(FLASH_BASE_ADDRESS, 1, &info);

	if(rc) {printf("Erase Flash error\n"); return -1;}

	return rc;
}
/*-----------------------------------------------------------------
                  MIPS reset
-------------------------------------------------------------------*/
int p_MipsReset()
{
	int rc;
	
	printf("MIPS Reset test...\n");
	
	rc = FW_MipsReset();
	
	return rc;
}
/*-----------------------------------------------------------------
                  For test
-------------------------------------------------------------------*/
int p_test()
{
	unsigned char buf;
	int rc;//,i;
	//int handle;
	
	printf("Test...\n");
	
	//handle = open("/home/test/vmlinux_alfred.zip",O_RDONLY);
	//rc = read(handle, buf, 100*2);
	//for(i=0;i<(100/5);i++)
	//	printf("%04x,%04x,%04x,%04x,%04x\n",buf[i*5],buf[i*5+1],buf[i*5+2],buf[i*5+3],buf[i*5+4]);
	//close(handle);
	//rc = FW_FlashCopy("/home/test/linux.zip",TWO_PARTITION);
	
	FW_ReadMainBus(EC0_CMD, 1, &buf);
	printf("RD:0x00=%x\n",buf);
	FW_ReadMainBus(EC0_CLDALO, 1, &buf);
	printf("RD:0x01=%x\n",buf);
	FW_ReadMainBus(EC0_CLDAHI, 1, &buf);
	printf("RD:0x02=%x\n",buf);
	FW_ReadMainBus(EN0_BOUNDARY, 1, &buf);
	printf("RD:0x03=%x\n",buf);
	FW_ReadMainBus(EC0_TSR, 1, &buf);
	printf("RD:0x04=%x\n",buf);
	FW_ReadMainBus(EC0_NCR, 1, &buf);
	printf("RD:0x05=%x\n",buf);
	FW_ReadMainBus(EC0_FIFO, 1, &buf);
	printf("RD:0x06=%x\n",buf);
	FW_ReadMainBus(EN0_ISR, 1, &buf);
	printf("RD:0x07=%x\n",buf);
	FW_ReadMainBus(EC0_CRDALO, 1, &buf);
	printf("RD:0x08=%x\n",buf);
	FW_ReadMainBus(EC0_CRDAHI, 1, &buf);
	printf("RD:0x09=%x\n",buf);
	FW_ReadMainBus(EC0_RSR, 1, &buf);
	printf("RD:0x0c=%x\n",buf);
	FW_ReadMainBus(EC0_COUNTER0, 1, &buf);
	printf("RD:0x0d=%x\n",buf);
	FW_ReadMainBus(EC0_COUNTER1, 1, &buf);
	printf("RD:0x0e=%x\n",buf);
	FW_ReadMainBus(EC0_COUNTER2, 1, &buf);
	printf("RD:0x0f=%x\n",buf);
	FW_ReadMainBus(EC0_DATAPORT, 1, &buf);
	printf("RD:0x10=%x\n",buf);
	FW_ReadMainBus(EC0_RESET, 1, &buf);
	printf("RD:0x1f=%x\n",buf);
	/*
	buf[0] = 0;
	buf[1] = 0;
	rc = FW_ReadMainBus(0x1f600000, 2, buf);
	printf("0x%x\n",buf[0]);
	printf("0x%x\n",buf[2]);
	buf[0] = 0x11;
	buf[2] = 0x22;
	rc = FW_WriteMainBus(0x1f600000, 2, buf);
	
	rc = FW_ReadMainBus(0x1f300000, 2, buf);
	printf("0x%x\n",buf[0]);
	printf("0x%x\n",buf[1]);
	buf[0] = 0x11;
	buf[1] = 0x22;
	rc = FW_WriteMainBus(0x1f300000, 2, buf);
	rc = FW_ReadMainBus(0x1f300000, 2, buf);
	printf("0x%x\n",buf[0]);
	printf("0x%x\n",buf[1]);*/
	return rc;
}
