;	[]===========================================================[]
;
;	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
;----------------------------------------------------------------------------
;R03  	04/12/99 BAR	Fixed reboot run bootblock, after Update ESCD/DMI 
;R02 	02/26/99 BAR	PMC change erase parmeter block address .
;R01	01/28/99 BAR	Support update ESCD DMI in SMM mode.
;			define switch "Flash_IN_SMBASE"
;R00	03/26/98 BAR	Initial Revision.
;			2M Flash only support ESCD_M2

;****************************************************************
;*								*
;*	SUBROUTINES TO SUPPORT PMC 2M TYPE FLASH ROM		*
;*								*
;****************************************************************

ifdef	Flash_16K_8K_8K_Unit
ifndef	Flash_IN_SMBASE		;R01
;[]========================================================================[]
; PMC_29F002C_Flash_Erase :
;
;	Erase PMC_29F002C PARAMETER BLOCK (FA000h - FBFFFh)
;
;Saves : None
;Input : CX = Erase size
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
PMC_29F002C_Flash_Erase	Proc	Near
 		pusha
;R03		push	di
;R02		mov	ax,di
;R02
;R02		mov	di,07000H			;Parameter block1
;R02		cmp	ax,02000H
;R02		jb	short PMC_29F002C_Flash_Erase_Do
;R02
;R02		mov	di,05000h			;Parameter block2
;R02		cmp	ax,04000H
;R02		jb	short PMC_29F002C_Flash_Erase_Do
;R02		pop	di				;get real addr
;R02
;R02		jmp	short	PMC_29F002C_Erase_Fail
PMC_29F002C_Flash_Erase_Do:

		mov	byte ptr gs:[05555h],0aah	;
		mov	byte ptr gs:[02aaah],055h	;PMC
		mov	byte ptr gs:[05555h],080h	;block erase
		mov	byte ptr gs:[05555h],0aah	;command
		mov	byte ptr gs:[02aaah],055h	;
;R03			mov	byte ptr gs:[di],030h		;erase block
;R03	
;R03			pop	di				;get real addr
;R03 - start
		mov     al, 30h			; 6th byte [0555] = 30h
		mov	dx,offset Ct_Set_Flash
		call	dx
;R03 - end

		mov	dx,offset EEPROM_Wait_10ms
		call	dx

		call	PMC_Check_Toggle_Ready

		call	PMC_CHIP_RESET
		mov	dx,offset Ct_Get_Flash	
		call	dx
		cmp	al,0ffh					;erase OK
		jne	short PMC_29F002C_Erase_Fail
		clc
		popa
		ret
PMC_29F002C_Erase_Fail:
		stc
		popa
		ret
PMC_29F002C_Flash_Erase	endp


;[]====================================================================[]
; PMC_29F002C_Flash_Write :
;
;	Program PMC_29F002C PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]

PMC_29F002C_Flash_Write	Proc	Near
		pusha

		mov	bx,25				;retry times
PMC_29F002C_Prg_Next_Byte:
		mov	al,ds:[si]			;Read Data
PMC_29F002C_Verify_Retry:
		mov	byte ptr gs:[5555h],0aah	;
		mov	byte ptr gs:[2aaah],055h
		mov	byte ptr gs:[5555h],0a0h	;page erase

		mov	dx,offset Ct_Set_Flash		
		call	dx				;Witre data to es:[di]
		mov	dx,offset Write_Delay		;wait 10us
		call	dx
		call	PMC_Check_Toggle_Ready
		call	PMC_CHIP_RESET

	;; Make Sure Program Correct
		mov	dx,offset Ct_Get_Flash		
		call	dx
		mov	ah,ds:[si]
		cmp	ah,al
		je	short PMC_29F002C_Verify_OK
PMC_29F002C_Verify_Fail:

		dec	bx
		jnz	short PMC_29F002C_Prg_Next_Byte
		stc					;Set Program Fail
		jmp	short PMC_29F002C_Prog_Exit

PMC_29F002C_Verify_OK:
		inc	si
		inc	di
		loop	PMC_29F002C_Prg_Next_Byte
		clc
PMC_29F002C_Prog_Exit:
		call	PMC_CHIP_RESET

		popa
		ret
PMC_29F002C_Flash_Write	Endp


PMC_Check_Toggle_Ready	proc    near
	   	pusha
		mov	dx,offset Ct_Get_Flash	
		call	dx
		and     al, 40h
		mov	bl,al
PMC_CTR_Tog1:
		mov	dx,offset Ct_Get_Flash
		call	dx
		and     al, 40h
		cmp     al, bl
		je      short PMC_CTR_Tog2
		xchg	bl,al
		jmp	short PMC_CTR_Tog1
PMC_CTR_Tog2:
		popa
		ret

PMC_Check_Toggle_Ready	endp


PMC_CHIP_RESET	proc	near
		mov	byte ptr gs:[5555h],0aah	;
		mov	byte ptr gs:[2aaah],055h
		mov	byte ptr gs:[5555h],0f0h	;reset command
		ret
PMC_CHIP_RESET	endp

;R01 - start
else;	Flash_IN_SMBASE
;[]========================================================================[]
; PMC_29F002C_Flash_Erase :
;
;	Erase PMC_29F002C PARAMETER BLOCK (FA000h - FBFFFh)
;
;Saves : None
;Input : CX = Erase size
;
;Output: CF = 0 , Successful
;	 CF = 1 , Error Erase
;
;[]========================================================================[]
PMC_29F002C_Flash_Erase	Proc	Near
 		pushad
;R02		push	di
;R02		mov	ax,di
;R02
;R02		mov	di,05000H			;Parameter block1
;R02		cmp	ax,0A000H
;R02		je	short PMC_29F002C_Flash_Erase_Do
;R02
;R02		mov	di,07000h			;Parameter block2
;R02		cmp	ax,08000H
;R02		je	short PMC_29F002C_Flash_Erase_Do
;R02		pop	di				;get real addr
;R02
;R02		jmp	short	PMC_29F002C_Erase_Fail
;R02PMC_29F002C_Flash_Erase_Do:

		mov	byte ptr es:[0FFFF5555h], 0aah	;
		mov	byte ptr es:[0FFFF2aaah], 055h	;PMC
		mov	byte ptr es:[0FFFF5555h], 080h	;block erase
		mov	byte ptr es:[0FFFF5555h], 0aah	;command
		mov	byte ptr es:[0FFFF2aaah], 055h	;
		mov	byte ptr es:[edi], 030h		;erase block
;R03		pop	di				;get real addr
		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	PMC_Check_Toggle_Ready

		call	PMC_CHIP_RESET
		mov	al,es:[edi]
		cmp	al,0ffh					;erase OK
		jne	short PMC_29F002C_Erase_Fail
		clc
		popad
		ret
PMC_29F002C_Erase_Fail:
		stc
		popad
		ret
PMC_29F002C_Flash_Erase	endp


;[]====================================================================[]
; PMC_29F002C_Flash_Write :
;
;	Program PMC_29F002C PARAMETER BLOCK
;
;Saves :
;Input : Source	= DS : SI
;	 Target	= ES : DI (Range = FA000h-FBFFFh)
;	 Length	= CX
;
;Output: CF = 0 Successful
;	 CF = 1	Error Program
;
;[]====================================================================[]

PMC_29F002C_Flash_Write	Proc	Near
		pushad

		mov	bx,25				;retry times
PMC_29F002C_Prg_Next_Byte:
		mov	al,ds:[esi]			;Read Data
PMC_29F002C_Verify_Retry:
		mov	byte ptr es:[0FFFF5555h],0aah	;
		mov	byte ptr es:[0FFFF2aaah],055h
		mov	byte ptr es:[0FFFF5555h],0a0h	;page erase
		mov	es:[edi],al

		push	cx
		mov	cx,1		
		call	dx		;delay
		pop	cx
		call	PMC_Check_Toggle_Ready
		call	PMC_CHIP_RESET

	;; Make Sure Program Correct
		mov	al,es:[edi]
		mov	ah,ds:[esi]
		cmp	ah,al
		je	short PMC_29F002C_Verify_OK
PMC_29F002C_Verify_Fail:

		dec	bx
		jnz	short PMC_29F002C_Prg_Next_Byte
		stc					;Set Program Fail
		jmp	short PMC_29F002C_Prog_Exit

PMC_29F002C_Verify_OK:
		inc	esi
		inc	edi
		loop	PMC_29F002C_Prg_Next_Byte
		clc
PMC_29F002C_Prog_Exit:
		call	PMC_CHIP_RESET

		popad
		ret
PMC_29F002C_Flash_Write	Endp


PMC_Check_Toggle_Ready	proc    near
	   	push	ax
		push	bx
		mov	al,es:[edi]
		and     al, 40h
		mov	bl,al
PMC_CTR_Tog1:
		mov	al,es:[edi]
		and     al, 40h
		cmp     al, bl
		je      short PMC_CTR_Tog2
		xchg	bl,al
		jmp	short PMC_CTR_Tog1
PMC_CTR_Tog2:
		pop	bx
		pop	ax
		ret

PMC_Check_Toggle_Ready	endp


PMC_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
PMC_CHIP_RESET	endp

endif;	Flash_IN_SMBASE		
;R01 - end

endif;	Flash_16K_8K_8K_Unit



