;	[]===========================================================[]
;
;	NOTICE: THIS PROGRAM BELONGS TO AWARD SOFTWARE INTERNATIONAL(R)
;	        INC. IT IS CONSIDERED A TRADE SECRET AND IS NOT TO BE 	
;	        DIVULGED OR USED BY PARTIES WHO HAVE NOT RECEIVED	
;	        WRITTEN AUTHORIZATION FROM THE OWNER.
;
; 	[]===========================================================[]
;

;----------------------------------------------------------------------------
;Rev	Date	 Name	Description
;----------------------------------------------------------------------------
;R02	01/28/99 BAR	Support update ESCD DMI in SMM mode.
;			define switch "Flash_IN_SMBASE"
;R01	08/28/98 BAR	Mininum block is 8K unit.
;R00	11/14/97 BAR	Initial Revision.


;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT IMT 2M TYPE FLASH ROM		*
;*								*
;****************************************************************

ifdef	Flash_16K_8K_8K_Unit
ifndef	Flash_IN_SMBASE		;R02
;[]========================================================================[]
; IMT_29F002T_Flash_Erase :
;
;	Erase IMT_29F002T PARAMETER BLOCK (FA000h - FBFFFh)
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
IMT_29F002T_Flash_Erase	Proc	Near
 		pusha
;R01		mov	cx,1000h/512		;one page is 512 byte 
		mov	cx,2000h/512		;R01 ;one page is 512 byte 
						;mininum page erase is 8K
IMT_29F002T_Erase_loop:
ifdef ESCD_M2
		mov	byte ptr gs:[05555h],0aah	;
		mov	byte ptr gs:[02aaah],055h	;IMT
		mov	byte ptr gs:[05555h],080h	;page erase
		mov	byte ptr gs:[05555h],0aah	;command
		mov	byte ptr gs:[02aaah],055h	;
else ;ESCD_M2
		push	di			;save erase 
		mov     di, 05555h		; es:di = 05555h
		mov     al, 0AAh		; 1st byte [05555] = AAh
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 02AAAh		; es:di = 02AAAh
		mov     al, 055h		; 2nd byte [02AAA] = 55h
		mov	dx,offset Ct_Set_Flash
		call	dx
		mov     di, 05555h		; es:di = 05555h
		mov     al, 080h		; 3rd byte [05555] = 80h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 05555h		; es:di = 05555h
		mov     al, 0AAh 		; 4th byte [05555] = AAh
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 02AAAh		; es:si = 02AAAh
		mov     al, 055h		; 5th byte [02AAA] = 55h
		mov	dx,offset Ct_Set_Flash
		call	dx
		pop	di			;pop erase page

endif ;ESCD_M2
		mov     al, 30h			; 6th byte es:[di] = 30h
		mov	dx,offset Ct_Set_Flash	
		call	dx			;erase one page
		mov	dx,offset EEPROM_Wait_10ms	
		call	dx
ifdef	More_Flash_Wait_Time				
		mov	dx,offset EEPROM_Wait_10ms	
		call	dx				
		mov	dx,offset EEPROM_Wait_10ms	
		call	dx
endif;	More_Flash_Wait_Time 
		call	IMT_Check_Toggle_Ready

		call	IMT_CHIP_RESET
		mov	dx,offset Ct_Get_Flash		
		call	dx
		cmp	al,0ffh					;erase OK
		jne	short IMT_29F002T_Erase_Fail	
		add	di,512					;next page
		loop	short IMT_29F002T_Erase_loop		;
		clc
		popa
		ret
IMT_29F002T_Erase_Fail:
		stc
		popa
		ret
IMT_29F002T_Flash_Erase	endp

;[]====================================================================[]
; IMT_29F002T_Flash_Write :
;
;	Program IMT_29F002T PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]

IMT_29F002T_Flash_Write	Proc	Near

		pusha
IMT_29F002T_Prg_Next_Byte:
		mov	al,ds:[si]			;Read Data
IMT_29F002T_Verify_Retry:
		call	IMT_29F002T_Prg_Sequence
		mov	dx,offset Ct_Set_Flash		
		call	dx				;Witre data to es:[di]
		mov	dx,offset Write_Delay		;wait 10us
		call	dx
		call	IMT_Check_Toggle_Ready
		call	IMT_CHIP_RESET

	;; Make Sure Program Correct
		mov	dx,offset Ct_Get_Flash		
		call	dx
		mov	ah,ds:[si]
		cmp	ah,al
		je	short IMT_29F002T_Verify_OK
		stc					;Set Program Fail
		jmp	short IMT_29F002T_Prog_Exit

IMT_29F002T_Verify_OK:
		inc	si
		inc	di
		loop	IMT_29F002T_Prg_Next_Byte
		clc
IMT_29F002T_Prog_Exit:
		popa
		ret
IMT_29F002T_Flash_Write	Endp

IMT_29F002T_Prg_Sequence	proc	near
ifdef ESCD_M2
		mov	byte ptr gs:[5555h],0aah	;
		mov	byte ptr gs:[2aaah],055h
		mov	byte ptr gs:[5555h],0a0h	;page erase
else ;ESCD_M2
		push	di
		push	ax

		mov	di,05555h
		mov	al,0aah
		mov	dx,offset Ct_Set_Flash
		call	dx
			    
		mov	di,02aaah
		mov	al,055h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov	di,05555h
		mov	al,0a0h
		mov	dx,offset Ct_Set_Flash
		call	dx

		pop	ax
		pop	di
endif ;ESCD_M2
		ret
IMT_29F002T_Prg_Sequence	endp

IMT_Check_Toggle_Ready	proc near

		push    ax
		push	bx
		mov	dx,offset Ct_Get_Flash	
		call	dx
		and     al, 40h
		mov	ah,al
IMT_CTR_Tog2:
		mov	bl,ah				;ifndef ESCD_M2 break ah 
		mov	dx,offset Ct_Get_Flash
		call	dx		      
		and     al, 40h
		cmp     al, bl
		je      short IMT_CTR_Tog3
		xchg	ah,al
		jmp	short IMT_CTR_Tog2
IMT_CTR_Tog3:
		pop	bx
		pop     ax
		ret

IMT_Check_Toggle_Ready	endp

IMT_CHIP_RESET	proc	near
ifdef ESCD_M2
		mov	byte ptr gs:[5555h],0aah	;
		mov	byte ptr gs:[2aaah],055h
		mov	byte ptr gs:[5555h],0f0h	;reset command
else ;ESCD_M2

		push	di
		push	ax

		mov	di,05555h
		mov	al,0aah
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov	di,02aaah
		mov	al,055h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov	di,05555h
		mov	al,0f0h
		mov	dx,offset Ct_Set_Flash
		call	dx
		pop	ax
		pop	di
endif ;ESCD_M2
		ret

IMT_CHIP_RESET	endp
;R02 - start
else;	Flash_IN_SMBASE
;[]========================================================================[]
; IMT_29F002T_Flash_Erase :
;
;	Erase IMT_29F002T PARAMETER BLOCK (FA000h - FBFFFh)
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
IMT_29F002T_Flash_Erase	Proc	Near
 		pushad
		mov	cx,2000h/512		;R01 ;one page is 512 byte 
						;mininum page erase is 8K
IMT_29F002T_Erase_loop:
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h	;IMT
		mov	byte ptr es:[0FFFF5555h],080h	;page erase
		mov	byte ptr es:[0FFFF5555h],0aah	;command
		mov	byte ptr es:[0FFFF2aaah],055h	;
		mov	byte ptr es:[edi],030h		;
		push	cx
ifdef	More_Flash_Wait_Time				
		mov	cx,350*3
else;	More_Flash_Wait_Time 
		mov	cx,350		;350*30
endif;	More_Flash_Wait_Time 
		call	dx		;delay
		pop	cx
		call	IMT_Check_Toggle_Ready

		call	IMT_CHIP_RESET
		mov	al,byte ptr es:[edi]
		cmp	al,0ffh					;erase OK
		jne	short IMT_29F002T_Erase_Fail
		add	edi,512					;next page
		loop	short IMT_29F002T_Erase_loop		;
		clc
		popad
		ret
IMT_29F002T_Erase_Fail:
		stc
		popad
		ret
IMT_29F002T_Flash_Erase	endp

;[]====================================================================[]
; IMT_29F002T_Flash_Write :
;
;	Program IMT_29F002T PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]

IMT_29F002T_Flash_Write	Proc	Near

		pushad
IMT_29F002T_Prg_Next_Byte:
		mov	al,ds:[esi]			;Read Data
IMT_29F002T_Verify_Retry:
		call	IMT_29F002T_Prg_Sequence
		mov	es:[edi],al			;Read Data
		push	cx
		mov	cx,1	  ;Write_Delay		;wait 10us
		call	dx
		pop	cx
		call	IMT_Check_Toggle_Ready
		call	IMT_CHIP_RESET

	;; Make Sure Program Correct
		mov	al,es:[edi]
		mov	ah,ds:[esi]
		cmp	ah,al
		je	short IMT_29F002T_Verify_OK
		stc					;Set Program Fail
		jmp	short IMT_29F002T_Prog_Exit

IMT_29F002T_Verify_OK:
		inc	esi
		inc	edi
		loop	IMT_29F002T_Prg_Next_Byte
		clc
IMT_29F002T_Prog_Exit:
		popad
		ret
IMT_29F002T_Flash_Write	Endp

IMT_29F002T_Prg_Sequence	proc	near
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h
		mov	byte ptr es:[0FFFF5555h],0a0h	;byte program
		ret
IMT_29F002T_Prg_Sequence	endp

IMT_Check_Toggle_Ready	proc near

		push    ax
		push	bx
		mov	al,es:[edi]
		and     al, 40h
		mov	ah,al
IMT_CTR_Tog2:
		mov	bl,ah				;ifndef ESCD_M2 break ah 
		mov	al,es:[edi]
		and     al, 40h
		cmp     al, bl
		je      short IMT_CTR_Tog3
		xchg	ah,al
		jmp	short IMT_CTR_Tog2
IMT_CTR_Tog3:
		pop	bx
		pop     ax
		ret

IMT_Check_Toggle_Ready	endp

IMT_CHIP_RESET	proc	near
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h
		mov	byte ptr es:[0FFFF5555h],0f0h	;reset command
		ret
IMT_CHIP_RESET	endp

endif;	Flash_IN_SMBASE		
;R02 - end
endif;	Flash_16K_8K_8K_Unit
