;	[]===========================================================[]
;
;	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
;----------------------------------------------------------------------------
;R01	01/28/99 BAR	Support update ESCD DMI in SMM mode.
;			define switch "Flash_IN_SMBASE"
;R00	12/18/98 BAR	Initial Revision.


;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT AMD 2M TYPE FLASH ROM		*
;*								*
;****************************************************************

ifndef	Flash_IN_SMBASE				;R01
;[]========================================================================[]
; MX29F001T_Flash_Erase :
;
;	Erase MX29F001T PARAMETER BLOCK (FC000h - FDFFFh)
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
MX29F001T_Flash_Erase	Proc	Near
ifdef ESCD_M2
		mov	byte ptr gs:[05555h],0aah	;
		mov	byte ptr gs:[02aaah],055h	;
		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

		mov     di, 05555h		;R04; es:di = 05555h	
		mov     al, 0AAh		; 1st byte [0555] = AAh
		mov	dx,offset Ct_Set_Flash 
		call	dx

		mov     di, 02AAAh		;R04 ;ds:si = 02AAAh
		mov     al, 055h		; 2nd byte [0AAA] = 55h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 05555h		;R04; es:di = 05555h	
		mov     al, 80h			; 3rd byte [0555] = 80h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 05555h		;R04; es:di = 05555h	
		mov     al, 0AAh 		; 4th byte [0555] = AAh
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 02AAAh		;R04 ;ds:si = 02AAAh
		mov     al, 055h		; 5th byte [0AAA] = 55h
		mov	dx,offset Ct_Set_Flash
		call	dx

		pop	di
endif ;ESCD_M2

		mov     al, 30h			; 6th byte [0555] = 30h
		mov	dx,offset Ct_Set_Flash
		call	dx
		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

		pusha

		mov	dx,offset Ct_Get_Flash
		call	dx
		mov	bh,al
MX29F001T_Erase_Verify:
		mov	dx,offset Ct_Get_Flash
		call	dx
		mov	bl,bh
		xor	bh,al
		test	bh,40h
		mov	bh,al
		jz	short MX29F001T_Erase_OK

		test	bl,20h
		jz	short MX29F001T_Erase_Verify

		mov	dx,offset Ct_Get_Flash
		call	dx
		xor	bl,al
		test	bl,40h
		jz	short MX29F001T_Erase_OK

MX29F001T_Erase_Fail:
		stc
		popa
		ret

MX29F001T_Erase_OK:
		cmp	al,0ffh
		jne	short MX29F001T_Erase_Fail
		clc
		popa
		ret
MX29F001T_Flash_Erase	endp

;[]====================================================================[]
; MX29F001T_Flash_Write :
;
;	Program MX29F001T PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]
MX29F001T_Flash_Write	Proc	Near

		pusha

MX29F001T_Prg_Next_Byte:
		mov	bx,25				;Program Retry Count
		mov	al,ds:[si]			;Read Data

MX29F001T_Verify_Retry:
		call	MX29F001T_Prg_Sequence
		mov	dx,offset Ct_Set_Flash
		call	dx
		call	MX29F001T_Check_Toggle_Ready

		jc	short MX29F001NT_Toggle_error	;R03

	;; Make Sure Program Correct
		mov	dx,offset Ct_Get_Flash
		call	dx
		cmp	al,ds:[si]
		je	short MX29F001T_Verify_OK
MX29F001NT_Toggle_error:				;R03
		dec	bx
		jnz	short MX29F001T_Verify_Retry

		stc					;Set Program Fail
		jmp	short MX29F001T_Prog_Exit

MX29F001T_Verify_OK:
		inc	si
		inc	di
		loop	MX29F001T_Prg_Next_Byte
		clc

MX29F001T_Prog_Exit:

		popa
		ret
MX29F001T_Flash_Write	Endp

MX29F001T_Prg_Sequence	proc	near
ifdef ESCD_M2
		mov	byte ptr gs:[05555h],0aah	;
		mov	byte ptr gs:[02aaah],055h
		mov	byte ptr gs:[05555h],0a0h	
else ;ESCD_M2
		push	di
		push	ax

		mov     di, 05555h		;es:di = 05555h	
		mov	al,0aah
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 02AAAh		;es:di = 02AAAh
		mov	al,55h
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov     di, 05555h		;es:di = 05555h	
		mov	al,0a0h
		mov	dx,offset Ct_Set_Flash
		call	dx

		pop	ax
		pop	di
endif ;ESCD_M2
		ret
MX29F001T_Prg_Sequence	endp

MX29F001T_Check_Toggle_Ready	proc	near
		clc
		push	ax
		push	bx
MX29F001T_CTR_TOG1:
		mov	dx,offset Ct_Get_Flash
		call	dx
		mov	ah,ds:[si]			;Read Source Bit once
		and	ax,8080h
		cmp	al,ah
		je	short MX29F001T_CTR_TOG3

MX29F001T_CTR_TOG2:
		mov	bl,ah
		mov	dx,offset Ct_Get_Flash
		call	dx
		and	al,0a0h
		test	al,20h
		jz	short MX29F001T_CTR_TOG1
		and	al,80h
		cmp	al,bl
		je	short MX29F001T_CTR_TOG3
		stc
MX29F001T_CTR_TOG3:
		pop	bx
		pop	ax
		ret
MX29F001T_Check_Toggle_Ready	endp
;R01 - start
else;	Flash_IN_SMBASE
;[]========================================================================[]
; MX29F001T_Flash_Erase :
;
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
MX29F001T_Flash_Erase	Proc	Near
		push	edi
		mov	edi,0FFFF0000H
		mov	byte ptr es:[edi + 05555h],0aah
		mov	byte ptr es:[edi + 02AAAh],055h
		mov	byte ptr es:[edi + 05555h],080h
		mov	byte ptr es:[edi + 05555h],0aah
		mov	byte ptr es:[edi + 02AAAh],055h
		pop	edi
		mov	byte ptr es:[edi],30h
		push	cx
ifdef	More_Flash_Wait_Time
		mov	cx,350 * 3		
else;	More_Flash_Wait_Time
		mov	cx,350	
endif;	More_Flash_Wait_Time
		call	dx		;delay time = (30ns)* cx =10.5ms
		pop	cx
	
		pusha
		mov	al,byte ptr es:[edi]
		mov	bh,al
MX29F001T_Erase_Verify:
		mov	al,byte ptr es:[edi]
		mov	bl,bh
		xor	bh,al
		test	bh,40h
		mov	bh,al
		jz	short MX29F001T_Erase_OK

		test	bl,20h
		jz	short MX29F001T_Erase_Verify

		mov	al,byte ptr es:[edi]
		xor	bl,al
		test	bl,40h
		jz	short MX29F001T_Erase_OK

MX29F001T_Erase_Fail:
		stc
		popa
		ret

MX29F001T_Erase_OK:
		cmp	al,0ffh
		jne	short MX29F001T_Erase_Fail
		clc
		popa
		ret
MX29F001T_Flash_Erase	endp

;[]====================================================================[]
; MX29F001T_Flash_Write :
;
;	Program AM29F002T PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]
MX29F001T_Flash_Write	Proc	Near

		pusha
MX29F001T_Prg_Next_Byte:
		mov	bx,25				;Program Retry Count
		mov	al,byte ptr ds:[esi]			;Read Data

MX29F001T_Verify_Retry:
	;   Program Sequence
		mov	byte ptr es:[0FFFF5555h],0aah
		mov	byte ptr es:[0FFFF2AAAh],055h
		mov	byte ptr es:[0FFFF5555h],0a0h

		mov	byte ptr es:[edi],al			;Write Data
		call	MX29F001T_Check_Toggle_Ready
		jc	short MX29F001T_Toggle_error

	;; Make Sure Program Correct
		mov	al,byte ptr es:[edi]
		cmp	al,byte ptr ds:[esi]
		je	short MX29F001T_Verify_OK
MX29F001T_Toggle_error:	
		dec	bx
		jnz	short MX29F001T_Verify_Retry

		stc					;Set Program Fail
		jmp	short MX29F001T_Prog_Exit

MX29F001T_Verify_OK:
		inc	esi
		inc	edi
		loop	MX29F001T_Prg_Next_Byte
		clc

MX29F001T_Prog_Exit:

		popa
		ret
MX29F001T_Flash_Write	Endp

MX29F001T_Check_Toggle_Ready	proc	near
		clc
		push	ax
		push	bx
MX29F001T_CTR_TOG1:
		mov	al,byte ptr es:[edi]
		mov	ah,byte ptr ds:[esi]			;Read Source Bit once
		and	ax,8080h
		cmp	al,ah
		je	short MX29F001T_CTR_TOG3

MX29F001T_CTR_TOG2:
		mov	bl,ah
		mov	al,byte ptr es:[edi]
		and	al,0a0h
		test	al,20h
		jz	short MX29F001T_CTR_TOG1
		and	al,80h
		cmp	al,bl
		je	short MX29F001T_CTR_TOG3
		stc
MX29F001T_CTR_TOG3:
		pop	bx
		pop	ax
		ret
MX29F001T_Check_Toggle_Ready	endp
endif;	Flash_IN_SMBASE		
;R01 - end

