;	[]===========================================================[]
;
;	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
;----------------------------------------------------------------------------
;R04	01/28/99 BAR	Support update ESCD DMI in SMM mode.
;			define switch "Flash_IN_SMBASE"
;R03	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.)
;R02	03/07/97 AVN	Added MXIC 28F2000TPC EEPROM support.
;R01	12/05/96 AVN	Suppost Both With/out Define ESCD_M2 and Save Code.
;R00	10/16/96 AVN	Initial Revision.

;R02 ifdef	MXIC_28F2000PPC
;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT MXIC FLASH ROM			*
;*								*
;****************************************************************

;[]========================================================================[]
;
;	MXIC 28F2000PPC commands
;
;[]========================================================================[]
; MX28F2000PPC commands
MX_READ_MEM_CMD			EQU	000H
MX_READ_ID_CMD			EQU	090H
MX_ERASE_CHIP			EQU	020H
MX_ERASE_BLOCK			EQU	060H
MX_ERASE_VERIFY			EQU	0A0H
MX_AUTO_ERASE_CHIP		EQU	030H
MX_AUTO_ERASE_BLOCK_CMD_1	EQU	020H
MX_AUTO_ERASE_BLOCK_CMD_2	EQU	0D0H
MX_AUTO_PROGRAM			EQU	040H
MX_RESET			EQU	0FFH
RETRY_COUNT			EQU	10
ERASE_POLLING			EQU	1000
ifndef	Flash_IN_SMBASE		;R04
;[]========================================================================[]
; MX_28f2000PPC_Flash_Erase:
;
;	Erase MXIC 28F2000PPC PARAMETER BLOCK (FD000h - FDFFFh)
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
MX_28f2000PPC_Flash_Erase	Proc	Near

		push	bx
		push	cx
		push	di
;R03		push	es

MXIC_Erase_Retry:

;R01		mov	al, MX_AUTO_ERASE_BLOCK_CMD_1	; Erase Command	1
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
;R01 		mov	al, MX_AUTO_ERASE_BLOCK_CMD_2	; Erase Command	2
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
		mov	al, MX_AUTO_ERASE_BLOCK_CMD_1	; R01 Erase Command	1
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01
 		mov	al, MX_AUTO_ERASE_BLOCK_CMD_2	; R01 Erase Command	2
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01

		mov	cx, ERASE_POLLING
;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
		mov	bl, al
MX_Erase_Polling:
;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
		xor	bl, al
		test	bl, 40h				; Test Toggle Bit
		mov	bl, al
		jnz	short MX_Erase_Polling

		test	bl, 10000000b			; Verify Bit
ifdef	Flash_16K_8K_8K_Unit				; R02
		jnz	short MXIC_Erase_Retry_1	; R02
elseifdef	Flash_4K_Unit				; R02
		jnz	short MXIC_Erase_Ok
endif;	Flash_16K_8K_8K_Unit				; R02
		loop	MX_Erase_Polling

;R02 - start
ifdef	Flash_16K_8K_8K_Unit
		jmp	short MXIC_Erase_Error

MXIC_Erase_Retry_1:
		add	di,1000h
		mov	al,MX_AUTO_ERASE_BLOCK_CMD_1	; Erase Command	1
		mov	dx,offset Ct_Set_Flash
		call	dx
 		mov	al,MX_AUTO_ERASE_BLOCK_CMD_2	; Erase Command	2
		mov	dx,offset Ct_Set_Flash
		call	dx

		mov	cx,ERASE_POLLING
		mov	dx,offset Ct_Get_Flash
		call	dx
		mov	bl,al
MX_Erase_Polling_1:
		mov	dx,offset Ct_Get_Flash
		call	dx
		xor	bl,al
		test	bl,40h				; Test Toggle Bit
		mov	bl,al
		jnz	short MX_Erase_Polling_1

		test	bl,10000000b			; Verify Bit
		jnz	short MXIC_Erase_Ok
		loop	MX_Erase_Polling_1
endif;	Flash_16K_8K_8K_Unit
;R02 - end

MXIC_Erase_Error:
;R01 		mov	al, MX_RESET			;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
;R01 		mov	al, MX_RESET			;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	al, MX_RESET			; R01 reset to ROM read status
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01
 		mov	al, MX_RESET			; R01 reset to ROM read status
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01
		stc
;R03		pop	es
		pop	di
		pop	cx
		pop	bx
		ret

MXIC_Erase_Ok:
;R01 		mov	al, MX_RESET		;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
;R01 		mov	al, MX_RESET		;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	al, MX_RESET			; R01 reset to ROM read status
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01
 		mov	al, MX_RESET			; R01 reset to ROM read status
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01
		clc
;R03		pop	es
		pop	di
		pop	cx
		pop	bx
		ret

MX_28f2000PPC_Flash_Erase	Endp

;[]====================================================================[]
; MX_28f2000PPC_Flash_Write:
;
;	Program MXIC 28F2000PPC PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FD000h-FDFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]
MX_28f2000PPC_Flash_Write	Proc	Near

		push	bx
		push	cx
		push	si
		push	di

MXIC_Prg_Next_Byte:
		push	cx
		mov	cx, RETRY_COUNT
MXIC_Prg_Retry:
		push	cx

	;; Write Commnad
		mov	al,byte ptr MX_AUTO_PROGRAM
;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

	;; Write Data
		mov	al,byte ptr ds:[si]
;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

	;; Wait ROM Cycle
;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
		mov	bl,al
MX_Program_Polling:
;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
		xor	bl,al
		test	bl,40h				; Test Toggle Bit
		mov	bl,al
		jnz	short MX_Program_Polling
		
	;; Send Reset Command To ROM
;R01 		mov	al,byte ptr MX_RESET
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
;R01 		mov	al,byte ptr MX_RESET
;R01 ifndef	ESCD_M2
;R01 		call	Ct_Set_Flash
;R01 else;	ESCD_M2
;R01 		mov	es:[di],al
;R01 endif;	ESCD_M2
 		mov	al, MX_RESET			; R01 reset to ROM read status
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01
 		mov	al, MX_RESET			; R01 reset to ROM read status
		mov	dx,offset Ct_Set_Flash		; R01
		call	dx				; R01

	;; Verify Data
;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
		cmp	al, ds:[si]
		pop	cx
		je	short MXIC_Prg_Verify_Ok
		loop	MXIC_Prg_Retry

MXIC_Prg_Error:
		pop	cx
		stc
		pop	di
		pop	si
		pop	cx
		pop	bx
		ret

MXIC_Prg_Verify_Ok:
		pop	cx
		inc	si
		inc	di
		loop	short MXIC_Prg_Next_Byte

		clc
		pop	di
		pop	si
		pop	cx
		pop	bx
		ret
MX_28f2000PPC_Flash_Write	Endp
;R02 endif;	MXIC_28F2000PPC
;R04 - start
else;	Flash_IN_SMBASE
;[]========================================================================[]
; MX_28f2000PPC_Flash_Erase:
;
;	Erase MXIC 28F2000PPC PARAMETER BLOCK (FD000h - FDFFFh)
;
;Saves : None
;Input : None
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
MX_28f2000PPC_Flash_Erase	Proc	Near

		pusha
ifdef	Flash_16K_8K_8K_Unit			;one page is 4096 byte 
		mov	cx,2			;mininum page erase is 4K
						;MX28F2000TPC
elseifdef	Flash_4K_Unit			;cx = input erase size/4096
		mov	cx,1			;MX28F2000PPC
endif;	Flash_16K_8K_8K_Unit
						
MXIC_Erase_Block_Loop:
		push	cx			;push	block loop
MXIC_Erase_Retry:
		mov	al, MX_AUTO_ERASE_BLOCK_CMD_1	;Erase Command	1
		mov	es:[edi] , al
 		mov	al, MX_AUTO_ERASE_BLOCK_CMD_2	;Erase Command	2
		mov	es:[edi] , al

		mov	cx, ERASE_POLLING
		mov	al , es:[edi]
		mov	bl, al
MX_Erase_Polling:
		mov	al , es:[edi]
		xor	bl, al
		test	bl, 40h				; Test Toggle Bit
		mov	bl, al
		jnz	short MX_Erase_Polling

		test	bl, 10000000b			; Verify Bit
		jnz	short MXIC_Erase_ok
		loop	MX_Erase_Polling
		stc	      				;Erase error
		pop	cx				;pop block loop
		jmp	short MXIC_Erase_Exit

MXIC_Erase_ok:
		pop	cx				;pop block loop
		add	edi,1000h
		loop	MXIC_Erase_Block_Loop
		clc
MXIC_Erase_Exit:
 		mov	al, MX_RESET			; R01 reset to ROM read status
		mov	es:[edi] , al
 		mov	al, MX_RESET			; R01 reset to ROM read status
		mov	es:[edi] , al
		popa
		ret
MX_28f2000PPC_Flash_Erase	Endp

;[]====================================================================[]
; MX_28f2000PPC_Flash_Write:
;
;	Program MXIC 28F2000PPC PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FD000h-FDFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]
MX_28f2000PPC_Flash_Write	Proc	Near

		pusha
MXIC_Prg_Next_Byte:
		push	cx
		mov	cx, RETRY_COUNT
MXIC_Prg_Retry:
		push	cx

	;; Write Commnad
		mov	al,byte ptr MX_AUTO_PROGRAM
 		mov	es:[edi],al
	;; Write Data
		mov	al,byte ptr ds:[esi]
 		mov	es:[edi],al

	;; Wait ROM Cycle
		mov	al , es:[edi]
		mov	bl,al
MX_Program_Polling:
		mov	al , es:[edi]
		xor	bl,al
		test	bl,40h				; Test Toggle Bit
		mov	bl,al
		jnz	short MX_Program_Polling
		
	;; Send Reset Command To ROM
 		mov	al, MX_RESET			; R01 reset to ROM read status
		mov	es:[edi] , al
 		mov	al, MX_RESET			; R01 reset to ROM read status
		mov	es:[edi] , al
	;; Verify Data
		mov	al , es:[edi]
		cmp	al, ds:[esi]
		pop	cx
		je	short MXIC_Prg_Verify_Ok
		loop	MXIC_Prg_Retry

MXIC_Prg_Error:
		pop	cx
		stc
		popa
		ret

MXIC_Prg_Verify_Ok:
		pop	cx
		inc	esi
		inc	edi
		loop	short MXIC_Prg_Next_Byte

		clc
		popa
		ret
MX_28f2000PPC_Flash_Write	Endp
endif;	Flash_IN_SMBASE		
;R04 - end

