;	[]===========================================================[]
;
;	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
;----------------------------------------------------------------------------
;R05	01/28/99 BAR	Support update ESCD DMI in SMM mode.
;			define switch "Flash_IN_SMBASE"
;R04	05/08/98 KEN	Modified some codes to correspond to the ESCDCHIP
;			modification that using flat mode to update flash
;			ROM.(i.e. Don't destroy any segment registers at
;			flash ROM reading/writing routines.)
;R03	01/17/96 AVN	Speed Up INTEL ESCD/DMI Update Speed By Default, And
;			Define 'Speed_Down_EEPROM_Update' For Update Soly If
;			To Soon To Update Fail.
;R02	01/07/97 AVN	Added Define 'Speed_Up_EEPROM_Update' For Update ESCD
;			and DMI in Short Time.
;R01	12/05/96 AVN	Suppost Both With/out Define ESCD_M2 and Save Code.
;R00	09/17/96 AVN	Initial Revision.

ifdef	Flash_16K_8K_8K_Unit

;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT INTEL FLASH ROM			*
;*								*
;****************************************************************

;[]========================================================================[]
;
;	Intel's 28F0001BX-T commands
;
;[]========================================================================[]

RESET_CMD	EQU	0FFh
RD_ID_CMD	EQU	090h
RD_STATUS_CMD	EQU	070h
CL_STATUS_CMD	EQU	050h
PROG_CMD	EQU	040h
ERASE_CMD	EQU	020h
ERASE_CONFIRM	EQU	0D0h
ERASE_SUSPEND	EQU	0B0h
RETRY_COUNT	EQU	10
WAIT_COUNT	EQU	10
INTEL_RETRY	EQU	10
INTEL_WAIT	EQU	10

ifndef	Flash_IN_SMBASE		;R05
;[]========================================================================[]
; F002BXT_Flash_Erase :
;
;	Erase Intel 28F002BX-T PARAMETER BLOCK (FD000h - FDFFFh)
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
F002BXT_Flash_Erase	Proc	Near
ifdef	Flash_4M_support		;;;debug
		stc			;;;debug
		ret			;;;debug
endif;	Flash_4M_support		;;;debug

		push	bx
		push	di
;R04		push	es
		push	cx

		mov	bx, INTEL_RETRY
Erase_Retry:
		push	bx

		mov	al, CL_STATUS_CMD	;clear status command
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01

		mov	al, ERASE_CMD		;erase command
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01

		mov	al, ERASE_CONFIRM	;erase confirm command
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01

		mov	bx, INTEL_WAIT
Erase_Wait:
;R01 		call	INTEL_28f002_Wait_10ms
		mov	dx,offset EEPROM_Wait_10ms	; R01
		call	dx				; R01

		mov	al, RD_STATUS_CMD	;read status command
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 		call	Ct_Get_Flash
;R01 else;	ESCD_M2
;R01		mov	es:[di],al
;R01		mov	al,es:[di]
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01
		mov	dx,offset Ct_Get_Flash	; R01
		call	dx			; R01

		test	al, 80h			;busy/ready bit
		jnz	short Erase_Ok
		dec	bx
		jnz	short Erase_Wait
		pop	bx
		dec	bx
		jnz	short Erase_Retry
Erase_Error:
		mov	al, RESET_CMD		;reset to ROM read status
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01
		stc
		pop	cx
;R04		pop	es
		pop	di
		pop	bx
		ret

Erase_Ok:
		pop	bx
		mov	al, RESET_CMD		;reset to ROM read status
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01
		clc
		pop	cx
;R04		pop	es
		pop	di
		pop	bx
		ret

F002BXT_Flash_Erase	Endp

;[]====================================================================[]
; F002BXT_Flash_Write :
;
;	Program Intel 28F002BX-T PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]
F002BXT_Flash_Write	Proc	Near

		push	bx
		push	cx
		push	si
		push	di

Prg_Next_Byte:
		mov	bx, RETRY_COUNT
Prg_Retry:
		push	bx

		mov	al, CL_STATUS_CMD	;clear status command
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01

		mov	al, PROG_CMD		;program command
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01

		mov	al, ds:[si]		;get source to write
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01

		mov	bx, WAIT_COUNT
Prg_Wait:
		mov	al, RD_STATUS_CMD	;read status command
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
;R01		call	INTEL_28f002_Wait_10ms
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01
;R03 ifndef	Speed_Up_EEPROM_Update			; R02
;R03 		mov	dx,offset EEPROM_Wait_10ms	; R01
;R03 		call	dx				; R01
;R03 else;	Speed_Up_EEPROM_Update			; R02
		mov	dx,offset Write_Delay		; R02
		call	dx				; R02
;R03 endif;	Speed_Up_EEPROM_Update			; R02

;R01 ifndef	ESCD_M2
;R01 		call	Ct_Get_Flash
;R01 else;	ESCD_M2
;R01 		mov	al,es:[di]
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Get_Flash	; R01
		call	dx			; R01
		test	al, 80h			;busy/ready bit
		jnz	short Prg_Verify_Ok
		dec	bx
		jnz	short Prg_Wait
		pop	bx
		dec	bx
		jnz	short Prg_Retry
Prg_Error:
		mov	al, RESET_CMD		;reset to ROM read status
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01
		stc
		pop	di
		pop	si
		pop	cx
		pop	bx
		ret

Prg_Verify_Ok:
		pop	bx
		mov	al, RESET_CMD		;reset to ROM read status
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 		call	Ct_Get_Flash
;R01 else;	ESCD_M2
;R01		mov	es:[di],al
;R01		mov	al,es:[di]
;R01 endif;	ESCD_M2
		mov	dx,offset Ct_Set_Flash	; R01
		call	dx			; R01
		mov	dx,offset Ct_Get_Flash	; R01
		call	dx			; R01
		cmp	al,ds:[si]
		jne	short Prg_Error
		inc	si
		inc	di
		loop	short Prg_Next_Byte

		clc
		pop	di
		pop	si
		pop	cx
		pop	bx
		ret

F002BXT_Flash_Write	Endp

;R01 INTEL_28f002_Wait_10ms	proc	near
;R01 		push	bx
;R01 		push	cx
;R01 		xor	bx, bx
;R01 		mov	cx, 350
;R01 
;R01 		PUSH	AX			; save ax.
;R01 
;R01 		ALIGN	4
;R01 INTEL_WR_OUTER_LP:
;R01 INTEL_WR_INNER_LP:
;R01 INTEL_WR_STATE_0:	IN	AL,SYS1
;R01 		TEST	AL,010H
;R01 		JZ	SHORT INTEL_WR_STATE_0
;R01 
;R01 		ALIGN	4
;R01 INTEL_WR_STATE_1:	IN	AL,SYS1
;R01 		TEST	AL,010H
;R01 		JNZ	SHORT INTEL_WR_STATE_1
;R01 		LOOP	SHORT INTEL_WR_INNER_LP
;R01 
;R01 		OR	BX,BX
;R01 		JZ	SHORT INTEL_WR_EXIT
;R01 		DEC	BX
;R01 		JMP	SHORT INTEL_WR_OUTER_LP
;R01 
;R01 INTEL_WR_EXIT:	POP	AX			; restore ax
;R01 
;R01 		pop	cx
;R01 		pop	bx
;R01 		ret
;R01 INTEL_28f002_Wait_10ms	endp
;R05 - start
else;	Flash_IN_SMBASE	

;[]========================================================================[]
; F002BXT_Flash_Erase :
;
;	Erase Intel 28F002BX-T PARAMETER BLOCK (FD000h - FDFFFh)
;
;Saves : None
;Input : dx = delay function offset
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
F002BXT_Flash_Erase	Proc	Near

		pusha
		mov	bx, INTEL_RETRY
Erase_Retry:
		push	bx

		;clear status command
		mov	byte ptr es:[edi],CL_STATUS_CMD	
		;erase command
		mov	byte ptr es:[edi],ERASE_CMD
		;erase confirm command
		mov	byte ptr es:[edi],ERASE_CONFIRM

		mov	bx, INTEL_WAIT
Erase_Wait:
		push	cx
		mov	cx,350			;delay 10ms
		call	dx
		pop	cx

		;read status command
		mov	byte ptr es:[edi],RD_STATUS_CMD

		mov	al,byte ptr es:[edi]

		test	al, 80h			;busy/ready bit
		jnz	short Erase_Ok
		dec	bx
		jnz	short Erase_Wait
		pop	bx
		dec	bx
		jnz	short Erase_Retry
Erase_Error:
			;reset to ROM read status
		mov	byte ptr es:[edi],RESET_CMD
		stc
		popa
		ret

Erase_Ok:
		pop	bx
			;reset to ROM read status
		mov	byte ptr es:[edi],RESET_CMD
		clc
		popa
		ret

F002BXT_Flash_Erase	Endp

;[]====================================================================[]
; F002BXT_Flash_Write :
;
;	Program Intel 28F002BX-T PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]
F002BXT_Flash_Write	Proc	Near

		pusha
Prg_Next_Byte:
		mov	bx, RETRY_COUNT
Prg_Retry:
		push	bx

		;clear status command
		mov	byte ptr es:[edi],CL_STATUS_CMD
		;program command
		mov	byte ptr es:[edi],PROG_CMD

		mov	al, ds:[esi]		;get source to write
		mov	byte ptr es:[edi],al

		mov	bx, WAIT_COUNT
Prg_Wait:
		;read status command
		mov	byte ptr es:[edi],RD_STATUS_CMD
		push	cx
		mov	cx,2			;delay 10us
		call	dx
		pop	cx
		mov	al,byte ptr es:[edi]
		test	al, 80h			;busy/ready bit
		jnz	short Prg_Verify_Ok
		dec	bx
		jnz	short Prg_Wait
		pop	bx
		dec	bx
		jnz	short Prg_Retry
Prg_Error:
		;reset to ROM read status
		mov	byte ptr es:[edi], RESET_CMD
		stc
		popa
		ret

Prg_Verify_Ok:
		pop	bx
		;reset to ROM read status
		mov	byte ptr es:[edi], RESET_CMD

		mov	al,byte ptr es:[edi]	;read flash data
		cmp	al,ds:[esi]
		jne	short Prg_Error
		inc	esi
		inc	edi
		loop	short Prg_Next_Byte

		clc
		popa
		ret

F002BXT_Flash_Write	Endp
endif;	Flash_IN_SMBASE	
;R05 - end

endif	;Flash_16K_8K_8K_Unit
