;	[]===========================================================[]
;
;	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	01/23/98 BAR	Initial Revision.


;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT BM 2M TYPE FLASH ROM		*
;*								*
;****************************************************************

ifdef	Flash_16K_8K_8K_Unit
ifndef	Flash_IN_SMBASE		;R01
;[]========================================================================[]
; BM_29FS020_Flash_Erase :
;
;	Erase BM_29FS020 PARAMETER BLOCK (FA000h - FBFFFh)
;
;Saves : None
;Input : CX = Erase size
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
BM_29FS020_Flash_Erase	Proc	Near
 		pusha
		shr	cx,11			;one page is 2048 byte 
						;mininum page erase is 2K
						;cx = input erase size/2048
BM_29FS020_Erase_loop:
ifdef ESCD_M2
		mov	byte ptr gs:[05555h],0aah	;
		mov	byte ptr gs:[02aaah],055h	;BM
		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
		call	BM_Check_Toggle_Ready
		jc	short BM_29FS020_Erase_Fail
		call	BM_CHIP_RESET
		mov	dx,offset Ct_Get_Flash	
		call	dx
		cmp	al,0ffh					;erase OK
		jne	short BM_29FS020_Erase_Fail
		add	di,2048					;next page
		loop	short BM_29FS020_Erase_loop		;
		clc
		popa
		ret
BM_29FS020_Erase_Fail:
		stc
		popa
		ret
BM_29FS020_Flash_Erase	endp

;[]====================================================================[]
; BM_29FS020_Flash_Write :
;
;	Program BM_29FS020 PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]

BM_29FS020_Flash_Write	Proc	Near
		pusha

		mov	bx,25				;retry times
BM_29FS020_Prg_Next_Byte:
		mov	al,ds:[si]			;Read Data
BM_29FS020_Verify_Retry:
		call	BM_29FS020_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	BM_Check_Toggle_Ready
		jc	short BM_29FS020_Verify_Fail
		call	BM_CHIP_RESET

	;; Make Sure Program Correct
		mov	dx,offset Ct_Get_Flash		
		call	dx
		mov	ah,ds:[si]
		cmp	ah,al
		je	short BM_29FS020_Verify_OK
BM_29FS020_Verify_Fail:

		dec	bx
		jnz	short BM_29FS020_Prg_Next_Byte
		stc					;Set Program Fail
		jmp	short BM_29FS020_Prog_Exit

BM_29FS020_Verify_OK:
		inc	si
		inc	di
		loop	BM_29FS020_Prg_Next_Byte
		clc
BM_29FS020_Prog_Exit:
		call	BM_CHIP_RESET

		popa
		ret
BM_29FS020_Flash_Write	Endp

BM_29FS020_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
BM_29FS020_Prg_Sequence	endp


BM_Check_Toggle_Ready	proc    near
	   	pusha
		mov	dx,offset Ct_Get_Flash	
		call	dx
		and     al, 40h
		mov	bl,al
BM_CTR_Tog1:
		mov	dx,offset Ct_Get_Flash
		call	dx
		mov	cl,al
		and     al, 40h
		cmp     al, bl
		je      short BM_CTR_Tog2
		xchg	bl,al
		test	cl,20h
		jz	BM_CTR_Tog1

		mov	dx,offset Ct_Get_Flash
		call	dx
		and     al, 40h
		cmp     al, bl
		jne      short BM_CTR_Tog3
BM_CTR_Tog2:
		clc
		popa
		ret
BM_CTR_Tog3:
		stc
		popa
		ret

BM_Check_Toggle_Ready	endp

BM_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

BM_CHIP_RESET	endp
;R01 - start
else;	Flash_IN_SMBASE
;[]========================================================================[]
; BM_29FS020_Flash_Erase :
;
;	Erase BM_29FS020 PARAMETER BLOCK (FA000h - FBFFFh)
;
;Saves : None
;Input : CX = Erase size
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
BM_29FS020_Flash_Erase	Proc	Near
 		pusha
		add	cx,2047
		shr	cx,11			;one page is 2048 byte 
						;mininum page erase is 2K
						;cx = input erase size/2048
BM_29FS020_Erase_loop:
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h	;BM
		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		;


		call	BM_Check_Toggle_Ready
		jc	short BM_29FS020_Erase_Fail
		call	BM_CHIP_RESET
		mov	al,es:[edi]
		cmp	al,0ffh					;erase OK
		jne	short BM_29FS020_Erase_Fail
		add	di,2048					;next page
		loop	short BM_29FS020_Erase_loop		;
		clc
		popa
		ret
BM_29FS020_Erase_Fail:
		stc
		popa
		ret
BM_29FS020_Flash_Erase	endp

;[]====================================================================[]
; BM_29FS020_Flash_Write :
;
;	Program BM_29FS020 PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]

BM_29FS020_Flash_Write	Proc	Near
		pusha

		mov	bx,25				;retry times
BM_29FS020_Prg_Next_Byte:
		mov	al,ds:[esi]			;Read Data
BM_29FS020_Verify_Retry:
		call	BM_29FS020_Prg_Sequence
		mov	es:[edi],al
		push	cx
		mov	cx,1	  ;Write_Delay		;wait 10us
		call	dx
		pop	cx
		call	BM_Check_Toggle_Ready
		jc	short BM_29FS020_Verify_Fail
		call	BM_CHIP_RESET

	;; Make Sure Program Correct
		mov	al,es:[edi]
		mov	ah,ds:[esi]
		cmp	ah,al
		je	short BM_29FS020_Verify_OK
BM_29FS020_Verify_Fail:

		dec	bx
		jnz	short BM_29FS020_Prg_Next_Byte
		stc					;Set Program Fail
		jmp	short BM_29FS020_Prog_Exit

BM_29FS020_Verify_OK:
		inc	esi
		inc	edi
		loop	BM_29FS020_Prg_Next_Byte
		clc
BM_29FS020_Prog_Exit:
		call	BM_CHIP_RESET

		popa
		ret
BM_29FS020_Flash_Write	Endp

BM_29FS020_Prg_Sequence	proc	near
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h
		mov	byte ptr es:[0FFFF5555h],0a0h	;page erase
		ret
BM_29FS020_Prg_Sequence	endp


BM_Check_Toggle_Ready	proc    near
	   	pusha
		mov	al,es:[edi]
		and     al, 40h
		mov	bl,al
BM_CTR_Tog1:
		mov	al,es:[edi]
		mov	cl,al
		and     al, 40h
		cmp     al, bl
		je      short BM_CTR_Tog2
		xchg	bl,al
		test	cl,20h
		jz	BM_CTR_Tog1

		mov	al,es:[edi]
		and     al, 40h
		cmp     al, bl
		jne      short BM_CTR_Tog3
BM_CTR_Tog2:
		clc
		popa
		ret
BM_CTR_Tog3:
		stc
		popa
		ret

BM_Check_Toggle_Ready	endp

BM_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

BM_CHIP_RESET	endp
endif;	Flash_IN_SMBASE		
;R01 - end

endif;	Flash_16K_8K_8K_Unit
