//##############################################################################
//#
//#                  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
//# --------  --- -- ----  ---- ------- ---------------------
//# M7SMUSB1   04 11 2009  Fabre - M7 S2M USB draft
//#
//##############################################################################
//#               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 M7_verify_burnin();
void ESMT_test(int nLoopCnt, int nStartLba, int nEndLba);
void Trinity_Check_ALIST_Count(int nMCId,int nOffs);
void SetAtwhCondition(void);

void main()
{
	int nRet,nCnt;	
	int nGlistCnt,nFeatureFlag;
	int nV1,nV2,nV3,nV4,nV5;

	@USB_load_port();
	@USB_set_timeout(25000); // SET COMMAND TIMEOUT
	gSeries=1; //M7
	setcdb(2);
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 60000;
	@USB_power_on(BY_SPC); // POWER ON, SPINUP, & CHECK READY.
	delays(3);
@MESSAGE_Step(NUM);
	@USB_init(SMT_CODE);
	strcpy(PATTERN, "RE_SC");
	@USB_screen_processcode(0,0x50); //'P', SMT code
	@USB_screen_processcode(0,0x53); //'S', S/C code
@MESSAGE_Step(NUM); 
	@USB_init(0);
	@ACPT_start();

//#--------------------------------------------------------------------
//# CHECK MODEL
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"SAMSUNG");
	@USB_verify_model(); // CHECK TO VERIFY IDFY DATA & ATA INTF
	strcpy(PATTERN,"$MODEL");
	PATTERN[4]=0;PATTERN[5]=0;
	@USB_verify_model(); // M40-2 HEAD - VERIFY MODEL BEFORE
	strcpy(PATTERN,"2AC");	
	@USB_verify_firmware(BY_MAIN); 
@MESSAGE_Step(NUM); 
	strstr(gszCODENEGREV, ".d45", nRet );
	strstr(gszCODENEGREV, ".d46", nCnt );
	if(nRet==0 && nCnt==0)
	{
		printf("not M7 S2M");
		gErrorCode = ERR_MODEL;
		stoptest();
	}
//#--------------------------------------------------------------------
//# VERIFY BURNIN RESULTS.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM);
	M7_verify_burnin();
@MESSAGE_Step(NUM);
	strcpy(PATTERN,"HSTR");	
	nRet=verify_signature(9,0,1,0x64,BY_TEXT|BY_FID);
	if(nRet!=0)
	{
		printf("FT CHK");
		gErrorCode = ERR_FINAL_TEST; // Final test
		stoptest();	
	}
@MESSAGE_Step(NUM);
	@USB_set_serialnumber();
@MESSAGE_Step(NUM);
	//block 1K sector
	strcpy(PATTERN,"0200"); 
	@USB_verify_signature(9,0,1,0x1B0,BY_HEXA|BY_FID|BY_REVERSE);
//#--------------------------------------------------------------------
//# MC modify before Maincode D/L
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); // clear 2nd S/N
	strcpy(PATTERN, "0000");
	@USB_modify_sector(7,0,2,2,48,BY_HEXA|BY_FID); // SN CLEAR
@MESSAGE_Step(NUM); // P/N 202020202020 write
	strcpy(PATTERN, "202020202020");
	@USB_modify_sector(7,0,2,2,30,BY_HEXA|BY_FID); // P/N CLEAR
//#---------------------------------------------------------------------
//# DOWNLOAD THE MAIN CODE BEFORE STARTING FINAL TEST.
//#---------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	@USB_set_timeout(50000); // SET COMMAND TIMEOUT
	strcpy(FILENAME,"$MCODE");		
	@USB_download_microcode(64);
	@USB_set_timeout(25000); // SET COMMAND TIMEOUT
	@USB_power_off();                       
	delays(3);
//#---------------------------------------------------------------------
//# POWER UP AND INITIALIZE DRIVE PARAMETERS.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM);
	LastPowerOnLimit = 60000;
	@USB_power_on(BY_SPC); // POWER ON, SPINUP, & CHECK READY.
	delays(3);
@MESSAGE_Step(NUM); 
	@USB_init(0); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
	@ACPT_MES_Check();	
@MESSAGE_Step(NUM); 
//	strcpy(PATTERN,"$MODEL");
//	@USB_verify_model(); // M40-2 HEAD - VERIFY MODEL BEFORE
@MESSAGE_Step(NUM);
	@USB_DCO_restore();
	delays(1);
@MESSAGE_Step(NUM); // set WV 15
//	strcpy(PATTERN,"0F"); 
//	@USB_modify_sector(20,0,5,5,0x1A,BY_HEXA|BY_FID);
@MESSAGE_Step(NUM); // clear profile page
	@USB_reserved(0x07,0,0,1); //UID clear
    	@USB_set_standby(0);
	@USB_power_off();                       
	delays(3);
@MESSAGE_Step(NUM);
	LastPowerOnLimit = 60000;
	@USB_power_on(BY_SPC); // POWER ON, SPINUP, & CHECK READY.
	delays(3);
	@USB_init(0); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
@MESSAGE_Step(NUM);                                       
	@USB_set_features(0x0085); // Init APM
@MESSAGE_Step(NUM);
	@USB_reserved(0x97,0,0,1); // init & Enable  ATWH
	@USB_reserved(0x95,0,0,1); 
	@USB_reserved(0x98,0,0,1);
@MESSAGE_Step(NUM);
//	@USB_reserved(0x9C,0,1,1); // Set shock LPF mode to 14k 2th order
@MESSAGE_Step(NUM); 
	setUID(0x1232,0,0x0005); //UID_PM_CONTROL_FLAG
	setUID(0x3305,0,30); //UID_WV_COUNT_LUL
	nGlistCnt=0x0A;
	setUID(0x3209,0,nGlistCnt); // UID_HIDDEN_GLIST_COUNT
	setUID(0x320A,0,16); // UID_GLIST_PROVISION_RETRY_INDEX
	setUID(0x320B,0,16); // UID_GLIST_VIRGIN_RETRY_INDEX
	setUID(0x4015,0,12); //UID_CACHE_MAX_READ_PEND_COUNT
	setUID(0x4021,0,6000); //UID_CACHE_MAX_POSTFETCH_AG, MAX_POSTFETCH_AG_4_EXT);
	setUID(0x4010,0,6000); //UID_CACHE_MAX_POST_FETCH, MAX_POSTFETCH_4_EXT);
	SetAtwhCondition();
	nFeatureFlag=0;
//OEM_WRITE_VERIFY|OEM_ATWHEALING_MODE|OEM_WRITE_REAS_VERIFY|OEM_ENABLE_REDUCED_LOW_VDD
//OEM_NEC
	nFeatureFlag= nFeatureFlag | 0x21044008; 
	setUID(0x1221,0,nFeatureFlag);//UID_FEATURE_OPTION_FLAG;
@MESSAGE_Step(NUM); 
//	@USB_reserved(0x9A,0,0,1); // EXT mode
	
	@USB_power_off();                       
	delays(3);
@MESSAGE_Step(NUM);
	LastPowerOnLimit = 60000;
	@USB_power_on(BY_SPC); // POWER ON, SPINUP, & CHECK READY.
	delays(3);
@MESSAGE_Step(NUM); 
	@USB_init(0); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
//#--------------------------------------------------------------------
//# CHECK WR/RD MULTIPLE (C5/C4) WITH DIFFERENT MULTIPLES/BLOCK COUNTS.
//#--------------------------------------------------------------------
//#--------------------------------------------------------------------
//# DATA COMPARE TEST - ESMT TEST
//#--------------------------------------------------------------------
	@USB_set_timeout(10000); 
	@USB_set_retry(30);
@MESSAGE_Step(NUM);
	@USB_set_datacompare(1); // DATACOMPARE ON
	ESMT_test( 1000, 0, 200000 );
	@USB_set_datacompare(0); // DATACOMPARE OFF

//##############################################################################
//##############################################################################
//## NON 48 BIT MODE
//##############################################################################
//##############################################################################
@MESSAGE_Step(NUM);
	wfill(0x0000);                                              
	@USB_set_datacompare(1); // ENABLE DATACOMPARE
@MESSAGE_Step(NUM); 
	@USB_write(BY_USB, 0, 97, 128, 0);
@MESSAGE_Step(NUM); 
	@USB_write(BY_USB, 1001, 1256, 128, 0);                                    
@MESSAGE_Step(NUM); 
	@USB_write( BY_USB, 9645, 9645, 128, 0);   //$40G
	@USB_write( BY_USB, 14411, 14411, 128, 0); //$60G
	@USB_write( BY_USB, 19180, 19180, 128, 0); //$80G
@MESSAGE_Step(NUM); 
	@USB_write( BY_USB, 19195, 19195, 128, 0);                                    
@MESSAGE_Step(NUM); 
	@USB_read( BY_USB, 0,  201600, 128, 0);
@MESSAGE_Step(NUM); 
	@USB_read( BY_USB, 1512000,  1562400, 128, 0);
@MESSAGE_Step(NUM); 
	@USB_read( BY_USB, 3024000,  3074400, 128, 0);              
@MESSAGE_Step(NUM); 
	@USB_read( BY_USB, 5040000,  5090400, 128, 0);
@MESSAGE_Step(NUM);     
	@USB_read( BY_USB, 10080000,  10130400, 128, 0);               
@MESSAGE_Step(NUM);     
	@USB_read( BY_USB, 16128000,  16178400, 128, 0);   
            
@MESSAGE_Step(NUM); 
	@USB_write( BY_USB, 0, 151200, 128, 0); 
@MESSAGE_Step(NUM); 
	@USB_read( BY_USB, 0, 151200, 128, 0);
	@USB_set_datacompare(0); // DATA COMPARE OFF

//#--------------------------------------------------------------------
//# UDMA MODE TRANSFER CHECK.
//#--------------------------------------------------------------------
	wfill(0x0000); 
@MESSAGE_Step(NUM); 
	@USB_write (BY_USB, 0, 10080, 128, 0);
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB, 0, 10080, 128, 0);
@MESSAGE_Step(NUM); 
	@USB_write (BY_USB, 504000, 514080, 64, 0);
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB, 504000, 514080, 64, 0);
@MESSAGE_Step(NUM); 
	@USB_write (BY_USB, 1008000, 1018080, 128, 0); 
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB, 1008000, 1018080, 128, 0);
@MESSAGE_Step(NUM); 
	@USB_write (BY_USB, 1512000, 1522080, 64, 0);
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB, 1512000, 1522080, 64, 0);
@MESSAGE_Step(NUM); 
	@USB_write (BY_USB, 2016000, 2026080, 128, 0); 
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB, 2016000, 2026080, 128, 0);
@MESSAGE_Step(NUM); 
	@USB_write (BY_USB, 2520000, 2530080, 64, 0);
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB, 2520000, 2530080, 64, 0);
@MESSAGE_Step(NUM); 
	@USB_write (BY_USB, 3024000, 3074400, 64, 0); 
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB, 3024000, 3074400, 64, 0);
@MESSAGE_Step(NUM); 
	@USB_write (BY_USB, 3528000, 3578400, 128, 0);
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB, 3528000, 3578400, 128, 0);
//#--------------------------------------------------------------------
//# CHECK TO ENSURE SYS CYL READ OK.
//#--------------------------------------------------------------------
	@USB_set_datacompare(0); // DATA COMPARE OFF                                                         
@MESSAGE_Step(NUM); 
    	@USB_read (BY_CHS | BY_VENDOR, 256, 500, 64, 0);
@MESSAGE_Step(NUM);
	if(MHD>2) MHD=2;
	wfill(0x0000);
	@USB_read(BY_CHS|BY_VENDOR, 1, 63,0,0);
    	@USB_set_standby(0);
	@USB_power_off(); 
	delays(3);

//############################################################################
//# NEW AT TEST                                                               
//############################################################################
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 60000;
	@USB_power_on(BY_SPC); // POWER ON, SPINUP, & CHECK READY.
	delays(3);
	@USB_init(0);
@MESSAGE_Step(NUM); 
	@USB_reserved(0x9B,0,0,1); // USB Model set
@MESSAGE_Step(NUM); 
//	@USB_DCO_restore();
@MESSAGE_Step(NUM);
	@USB_set_features(0xFE42); //AAM Fast
@MESSAGE_Step(NUM);
	@USB_set_serialnumber();

@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"$UROM");
	@USB_verify_firmware(BY_MAIN);
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"$EROM");
	@USB_verify_firmware(BY_ENGINEERING); //$EROM
@MESSAGE_Step(NUM); 	                                          
	@USB_set_features(0x$AAMV); // AAM
@MESSAGE_Step(NUM); 
	@USB_set_retry(30);
@MESSAGE_Step(NUM); 
	wfill(0x0000);                                              
	@USB_write (BY_USB , 0, 2000, 128, 0);                                       
@MESSAGE_Step(NUM); 
	@USB_read (BY_USB , 0, 2000, 128, 0);
@MESSAGE_Step(NUM);
	Trinity_Check_ALIST_Count(19,20);    
@MESSAGE_Step(NUM); 
	@USB_reserved(0x87,0,1,1); // Reserved(135)
@MESSAGE_Step(NUM); 
	@USB_set_smart(0xDE); // SMART Initialize
	@USB_set_smart(0xD8); //$SMART_E
	@USB_set_smart(0xD8); // $SMART_S
	@USB_set_smart(0xF1D2); // $SMART_S
@MESSAGE_Step(NUM); 
    	@USB_set_standby(0);
	@USB_power_off();                       
	delays(7);                                      
@MESSAGE_Step(NUM);
	LastPowerOnLimit = 60000;
	@USB_power_on(BY_SPC); // POWER ON, SPINUP, & CHECK READY.
	delays(3);
	@USB_init(0);
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"SAMSUNG");
	@USB_verify_model(); // CHECK TO VERIFY IDFY DATA & ATA INTF        
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"$MODEL");
	@USB_verify_model();	    
@MESSAGE_Step(NUM);
	nV1=getUID(0x1232,0);
	nV2=getUID(0x3305,0);
	nV3=getUID(0x3209,0);
//	nV4=getUID(0x1221,0);
	if(nV1!=5 || nV2!=30 || nV3!=nGlistCnt )
	{
		printf("Sign");
		gErrorCode = ERR_VERIFY_SIGN;							   // JJW 1.3g
		stoptest();
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x320A,0); // UID_GLIST_PROVISION_RETRY_INDEX
	nV2=getUID(0x320B,0); // UID_GLIST_VIRGIN_RETRY_INDEX
	nV3=getUID(0x4015,0); //UID_CACHE_MAX_READ_PEND_COUNT
	nV4=getUID(0x3316,0); //UID_ATWH_MAX_ADJUST_COUNT_FOR_FIT
	if(nV1!=16 || nV2!=16 || nV3!=12 || nV4!=4)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x3311,0); //UID_ATWH_WRITE_THRESHOLD
	if(nV1!=0x3E80 && nV1!=0x7D00 && nV1!=0xFDE8)
	{
		gErrorCode = 137;	//sign error	
		stoptest();
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x3310,0); //UID_ATWH_CONTROL_FLAG
	nV2=getUID(0x4021,0); //UID_CACHE_MAX_POSTFETCH_AG, MAX_POSTFETCH_AG_4_EXT);
	nV3=getUID(0x4010,0); //UID_CACHE_MAX_POST_FETCH, MAX_POSTFETCH_4_EXT);
	if(nV1!=0x00|| nV2!=6000 || nV3!=6000)
	{
		printf("Sign");
		gErrorCode = ERR_VERIFY_SIGN;							   // JJW 1.3g
		stoptest();
	}
@MESSAGE_Step(NUM);
	strcpy(PATTERN, "B0F85009");             //$80G
	strcpy(PATTERN, "B04BF90D"); 		 //$120G
	strcpy(PATTERN, "B09EA112"); 		 //$160G
	strcpy(PATTERN, "B0F14917"); 		 //$200G
	strcpy(PATTERN, "70591C1D");                   //$250G
	strcpy(PATTERN, "B0EA4225");                   //$320G
	strcpy(PATTERN, "B090932E");                   //$400G
	strcpy(PATTERN, "3060383A");                   //$500G
	nRet=verify_signature(0,0,0,200,BY_HEXA|BY_IDFY);
	if(nRet!=0)
	{
		printf("BadCapa");
		gErrorCode = ERR_BAD_CAPA; // Bad Capa Fail!!
		stoptest();
	}
@MESSAGE_Step(NUM);
//	strcpy(PATTERN,"0F");
//	@USB_verify_signature(20,0,5,0x1A,BY_HEXA|BY_FID);

@MESSAGE_Step(NUM); // P/N 202020202020 write
	strcpy(PATTERN, "202020202020");
	nRet=verify_signature(7,0,2,30,BY_HEXA|BY_FID); // P/N CLEAR
	if(nRet!=0)
	{
		printf("PN no clear");
		gErrorCode = ERR_PARAM_CHECK; // Bad Capa Fail!!
		stoptest();
	}
@MESSAGE_Step(NUM);
	@USB_reserved(0x21,0,0,1);
	TF_CL = TF_CL & 0xFF;
	if(TF_CL<=15)
	{
		printf("Temp Sensing");
		gErrorCode = 51; //"ERR_SENSOR"
		stoptest();		
	}

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

	//$INSERT_CHKSPT

*/
@MESSAGE_Step(NUM); 
	@USB_set_standby(0);
	delays(5);
	@MESSAGE_DrivePass(); // make report file
}
void ESMT_test(int nLoopCnt, int nStartLba, int nEndLba)
{
	int nCnt;
	int nTargetLBA;
	int pttrn;
	int i,j;

	nCnt=0;
	srand(0x5555);
	while(nCnt<nLoopCnt)
	{
		rand(nTargetLBA,nStartLba,nEndLba);
		rand(pttrn,0, 0xffff);
		pttrn = pttrn << 16 | (0xffff - pttrn);

		wfill(pttrn);
		wrusb(nTargetLBA,128);
		rdusb(nTargetLBA,128);

		wfill(0);
		wrusb(nTargetLBA,128);
		rdusb(nTargetLBA,128);
		nCnt++;
	}
}
void Trinity_Check_ALIST_Count(int nMCId,int nOffs)
{
	int nReassignCnt;
	char c0,c1;

	if(nMCId==0)
	{
		@USB_phrd(1,0,372,1);
	}
	else
	{
		@USB_mcrd(nMCId,0,1);
	}
	rpeekb(c0,nOffs);rpeekb(c1,nOffs+1);
	nReassignCnt=(c1<<8)|(c0);
	if(nReassignCnt>1)
	{
		printf("ALst");
		gErrorCode = ERR_ALIST;		
		stoptest();
	}
}
void M7_verify_burnin()
{
	int result,nID;

	strcpy(FID_HEADER,"BI_RESLT");
	nID=@USB_Get_MCID();
	strcpy(PATTERN, "0000");
	result=verify_signature(nID,0,1,16,BY_HEXA|BY_FID);
	if(result!=0)
	{
		printf("BIErr");
		gErrorCode = ERR_BI_LED;
		stoptest();
	}
	strcpy(PATTERN, "02");
	result=verify_signature(nID,0,1,62,BY_HEXA|BY_FID);
	if(result!=0)
	{
		printf("NoBI");
		gErrorCode = ERR_BI_NO;
		stoptest();
	}	

}
void SetAtwhCondition(void)
{
	char c;

	setUID(0x3311,0,16000); //UID_ATWH_WRITE_THRESHOLD
	setUID(0x3316,0,4); //UID_ATWH_MAX_ADJUST_COUNT_FOR_FIT
	setUID(0x3310,0,0x00); //UID_ATWH_CONTROL_FLAG
	setUID(0x3312,0,3); //UID_ATWH_TRACK_MARGIN

    	@USB_mcrd(9,0,1);
	rpeekb(c,0x12A);

	if(c <= 18)
	{
		setUID(0x3311,0, 0xFDE8);   // atwh 65k
	}
	if(c == 21)
	{
        	setUID(0x3311,0, 0x7D00);   // atwh 32k
	}   
	if(c == 24)
	{
		setUID(0x3311,0, 0x3E80);   // atwh 16k
	}      
}
//################################# End of file ################################
