//##############################################################################
//#
//#                  WUTT DOALL.UTS   -  Samsung DoAll Process
//#             ( Run ServoCopy, Functional/BurnIn, Final and AT Test )
//#
//#               For All Promise PATA/SATA , SATA II Host Adapters
//#       (PATA UDMA 33/66/100/100TX2/133TX2 and SATA 150TX4 and SATA II 300)
//#
//# CHANGE HISTORY:
//# FILE+VER  	MMM-DD-YYYY NAME H/T VER DESCRIPTION OF CHANGE
//# --------  	--- -- ---- ---- ------- ---------------------
//# M8ESTD00   	02 26 2011  Fabre - M7E-S3M AT draft
//# M8ESTDZ0	03 08 2011 Fabre - 4K script
//#		03 31 2011 Fabre - Provision/virgin retry index 34,WV Temp 10
//# M8ESTDZ1	04 05 2011 Fabre - restore Provision/virgin retry index to 16 
//#				 - ATW CONTROL FLAG to 0x4A
//#		04 06 2011 Fabre - delete LSN seed check
//# M8EUCKD1	04 07 2011 Fabre - USB CKD script
//#
//#
//##############################################################################
//#               Copyright (C) Samsung Information Systems, 2005
//##############################################################################
/*--------------------------------------------------------------------
  SET THE SCRIPT FILENAME (FOR RESULT DISPLAY/VERSION CONTROL).
  MUST UPDATE WITH EACH CHANGE/VERSION.
  CHECK CORRECT MODEL BEFORE BI DUE TO UNIVERSAL HEAD.
--------------------------------------------------------------------*/
void Trinity_Check_ALIST_Count(int nMCId,int nOffs);
void M5S_BitCorruptionTest();
void Soft_Reset();
void NCQ_Error();
void NCQ_Soft_Reset();
void USB_verify_model();

void main()
{
	int nRet,nCnt,nRetry;
	int nWVTemp,nAtwhThres,nAtwhCnt;
	int nFeatureFlag,nFeatureFlag2,nFeatureFlag3;
	int nV1,nV2,nV3,nV4,nV5;

	@WINSTAR_set_timeout(25000); // SET COMMAND TIMEOUT                 
	strcpy(StringParam1,"TC_10");	
@MESSAGE_TESTPHASE();
	@WINSTAR_load_port();                                                        
	@WINSTAR_set_testoption(LOG_ACPT); // A/T Tester
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 80000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
	@WINSTAR_set_PIO (4);              
@MESSAGE_Step(NUM);
	@WINSTAR_init_smtcode();
	strcpy(PATTERN, "RE_SC");
	@WINSTAR_screen_processcode(0,0x50);
	@WINSTAR_screen_processcode(0,0x53);
@MESSAGE_Step(NUM); 
	@WINSTAR_init();
	@WINSTAR_set_PIO (4);              
	@ACPT_start();

//#--------------------------------------------------------------------
//# CHECK MODEL
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"SAMSUNG");
	@WINSTAR_verify_model(); // CHECK TO VERIFY IDFY DATA & ATA INTF      
	strcpy(PATTERN,"$MODEL");
	USB_verify_model();

//#--------------------------------------------------------------------
//# VERIFY BURNIN RESULTS.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM);
	@WINSTAR_verify_burnin();

@MESSAGE_Step(NUM);
	strcpy(PATTERN,"HSTR");	
	nRet=verify_signature(9,0,1,0x78,BY_TEXT|BY_FID);
	if(nRet!=0)
	{
		gErrorCode = 53; // final not run
		stoptest();
	}

//#--------------------------------------------------------------------
//# MC modify before Maincode D/L
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); // clear 2nd S/N
	mcrd(rdbuf,7,0,2);		
	rdbuf[48]=0;rdbuf[49]=0;
	mcwr(rdbuf,7,0,2);

//#---------------------------------------------------------------------
//# DOWNLOAD THE MAIN CODE BEFORE STARTING FINAL TEST.
//#---------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	strcpy(FILENAME,"$MCODE");		
	@WINSTAR_download_microcode(7);	
	@WINSTAR_power_off();                       
	delays(1);                                      

//#---------------------------------------------------------------------
//# POWER UP AND INITIALIZE DRIVE PARAMETERS.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 17000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
@MESSAGE_Step(NUM); 
	@WINSTAR_set_PIO (4);              
	@WINSTAR_init(); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
	strcpy(gszCODEREV,"$UROM"); // cheat for MES ROM version check
	@ACPT_MES_Check(); @WINSTAR_set_testoption(LOG_RESULT); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"$MODEL");
	USB_verify_model();
@MESSAGE_Step(NUM);	
	nRet = rdbuf[303] & 0x0F;
	if(nRet==0)
	{
		for(nCnt=0;nCnt<20;)
		{
			delays(1);
			nCnt++;
		}
		gErrorCode = 137;	//sign error	
		stoptest();		
	}

//#--------------------------------------------------------------------
//# CHECK TO ENSURE SYS CYL READ OK.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	@WINSTAR_set_datacompare(0); // DATA COMPARE OFF
	Lba48Bit = 0;
    	@WINSTAR_read (BY_CHS | BY_VENDOR, 128, 1000, 64, 0);
@MESSAGE_Step(NUM);
	if(MHD>2) MHD=2;
	Lba48Bit = 1;
	@WINSTAR_set_direction(0); 
	@WINSTAR_set_pattern(0x0000);
	@WINSTAR_read(BY_CHS | BY_VENDOR | BY_MCYL | BY_TRACK, 1, 63, MAX_BLKCNT,0);
	@WINSTAR_set_standby(0);
	@WINSTAR_power_off(); 
	delays(1);

//############################################################################
//# NEW AT TEST                                                               
//############################################################################
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 17000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
	@WINSTAR_set_PIO (4);
	@WINSTAR_init();             
@MESSAGE_Step(NUM);
	Trinity_Check_ALIST_Count(19,20);    
@MESSAGE_Step(NUM); 
	@WINSTAR_reserved(135,0,0,1); // SMART Initialize
	@WINSTAR_set_smart(0xDE); // SMART Initialize
	@WINSTAR_set_smart(0xD8); //$SMART_E
	@WINSTAR_set_smart(0xD9); //$SMART_D
	@WINSTAR_set_smart(0xD8); // $SMART_S
	@WINSTAR_set_smart(0xF1D2); // $SMART_S
@MESSAGE_Step(NUM);
	strcpy(PATTERN, "B09EA112"); 		 //$160G
	strcpy(PATTERN, "70591C1D");                   //$250G
	strcpy(PATTERN, "B0EA4225");                   //$320G
	strcpy(PATTERN, "B090932E");                   //$400G
	strcpy(PATTERN, "3060383A");                   //$500G
	strcpy(PATTERN, "B082854A");                   //$640G
	strcpy(PATTERN, "F0665457");                   //$750G
	strcpy(PATTERN, "B06D7074");                   //$1000G
	nRet=verify_signature(0,0,0,200,BY_HEXA|BY_IDFY);
	if(nRet!=0)
	{
		gErrorCode = 57; // Bad Capa Fail!!
		stoptest();
	}
@MESSAGE_Step(NUM);
	@WINSTAR_reserved(0x21,0,0,1); //screen temp sensor
	rtsk();
	TF_CL = TF_CL & 0xFF;
	if(TF_CL<=15)
	{	
		gErrorCode = 71; //Sens Error
		stoptest();		
	}

//############################################################################
//# CHECK TEST CONFIG - CHKUTS.TXT                                                               
//############################################################################

	//$INSERT_CHKSPT


@MESSAGE_Step(NUM); 
	strcpy(FILENAME,"$CODE1");		
	@WINSTAR_download_microcode(7);	
	@WINSTAR_power_off();
	delays(1);                                         
@MESSAGE_Step(NUM);
	LastPowerOnLimit = 20000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
	@WINSTAR_set_PIO (4); 
	strcpy(PATTERN,gszPN);
	@WINSTAR_init_smtcode();
	strcpy(gszPN,PATTERN);
	strcpy(gszCODEREV,"$UROM");
@MESSAGE_Step(NUM);
	// fmtu 0xE4;
	@WINSTAR_reserved(0xEA,0,0,1); 

	@MESSAGE_DrivePass(); // make report file



}
void Trinity_Check_ALIST_Count(int nMCId,int nOffs)
{
	int nReassignCnt;

	if(nMCId==0)
	{
		phrd(rdbuf,1,0,372,1);
	}
	else
	{
		mcrd(rdbuf,nMCId,0,1);
	}
	nReassignCnt=((rdbuf[nOffs+1]&0xFF)<<8)|(rdbuf[nOffs]&0xFF);
	if(nReassignCnt>1)
	{
		strcpy(StringParam1,"ALst");
		gErrorCode = 124;
		@MESSAGE_TEXT4();
		stoptest();
	}	
}
void M5S_BitCorruptionTest()
{
    int testLBA,testCount,testZone;
    int blockCount, distance;
    int pendCount;

    @WINSTAR_set_UDMA(5); // UDMA MODE 5

    for (testZone=0;testZone<3;)
    {
	Soft_Reset();

        testLBA = MAX_LBA/3*testZone;

        printf("Test Zone %02Xh \n", testZone);
        printf("Test SLBA %08Xh \n",testLBA);

        for (LBA=testLBA;LBA<testLBA+10000;)
        {
		fillsp(wdbuf,0,LBA,256);		
	        writedmaext(wdbuf,LBA,256);
        	LBA=LBA+256;
        }
        for (LBA=testLBA;LBA<testLBA+10000;)
        {
		fillsp(wdbuf,0,LBA,256);
		readdmaext(rdbuf,LBA,256);
		LBA=LBA+256;
        }
	srand(0xFFFF);
        for (testCount=0;testCount<5;)
        {
		Soft_Reset();
		rand(LBA,testLBA,testLBA+1000); 
		rand(blockCount,1,256);
		pendCount = 0;
		while(pendCount<10)
		{
			fillsp(wdbuf,0,LBA,blockCount);
	       	        writedmaext(wdbuf,LBA,blockCount);
                	rand(blockCount,1,10);
	                rand(distance,10,256);
	                LBA=LBA+distance;
        	        pendCount=pendCount+1;
            	}   
	        LBA=testLBA;
		while(LBA+256<=testLBA+1000)
		{
			fillsp(wdbuf,0,LBA,256);
			readdmaext(rdbuf,LBA,256);
			LBA=LBA+256;
		}
		testCount++;
        }
	testZone++;
    }
	@WINSTAR_set_pattern(0x0000);
    for (testZone=0;testZone<3;)
    {
        testLBA = MAX_LBA/3*testZone;
        for (LBA=testLBA;LBA<testLBA+10000;)
        {	
	        writedmaext(wdbuf,LBA,256);
		readdmaext(rdbuf,LBA,256);    
        	LBA=LBA+256;
        }
	srand(0xFFFF);
        for (testCount=0;testCount<5;)
        {
		rand(LBA,testLBA,testLBA+1000); 
		rand(blockCount,1,256);
		pendCount = 0;
		while(pendCount<10)
		{
	       	        writedmaext(wdbuf,LBA,blockCount);
	       	        readdmaext(wdbuf,LBA,blockCount);
                	rand(blockCount,1,10);
	                rand(distance,10,256);
	                LBA=LBA+distance;
        	        pendCount=pendCount+1;
            	}   
		testCount++;
        }
	testZone++;
    }

}
void Soft_Reset()
{
	int nCnt;

	softreset();
	for(nCnt=0; nCnt<150;)
	{
		rtsk();
		TF_ST = TF_ST & 0xFD;
		if ( TF_ST == 0x50 )
		{
			break;
		}
		printf("retry TF_ST=%02x\n", TF_ST );
        	delaym(100);
		nCnt++;
	}
	rtsk();
	TF_ST = TF_ST & 0xFD;
	if ( TF_ST != 0x50 )
	{
		printf("\nSoft Reset Fail!!! (TF_ST=%02x)\n", TF_ST );
		strcpy(StringParam1,"Rst ");
		gErrorCode = 12;								// JJW 1.3g
		@MESSAGE_TEXT4();
		stoptest();
		
	}
	return;
}
void NCQ_Error()
{
	gErrorCode = 210; // 77 screen(TempErr2)
	stoptest();	
}
void NCQ_Soft_Reset()
{
	int nCnt;

	for(nCnt=0; nCnt<3;)
	{
		softreset();
		rtsk();
		if(TF_ST != 0x40) break;
        	delaym(100);
		nCnt++;
	}

	for(nCnt=0; nCnt<150;)
	{
		TF_ST = TF_ST & 0xFD;
		if ( TF_ST == 0x50 )
		{
			break;
		}
		printf("retry TF_ST=%02x\n", TF_ST );
        	delaym(100);
		nCnt++;
	}
	rtsk();
	TF_ST = TF_ST & 0xFD;
	if ( TF_ST != 0x50 )
	{
		printf("\nSoft Reset Fail!!! (TF_ST=%02x)\n", TF_ST );
		NCQ_Error();	
	}
	return;
}
void USB_verify_model()
{
// 0123456789012345
// SAMSUNG HN-M101XBB
//         01234567 

	PATTERN[7]=gszMODEL[15];
	@WINSTAR_verify_model();
}


//################################# End of file ################################
