/*修改说明:加高电压后,去掉了SetRegPage中判断net_start标志*/

#include "DataType.h"
#include "eth.h"
#include "utils.h"
#include "console.h"
#include "mac.h"
#include "Rtl8019.h"

//#define	RTL8019_OP_16
#define	DEBUG_NET	
#define	RPSTART	0x4c
#define	RPSTOP	0x80
#define	SPSTART	0x40

static U8 rBNRY;

static U8 SrcMacID[ETH_ALEN] = {0x00,0x80,0x48,0x12,0x34,0x56};
//static U8 DstMacID[ETH_ALEN];

static U8 net_start;

void SetRegPage(U8 PageIdx)
{
	U8 temp;
	
	temp = inportb(BaseAddr);	
//	temp = (temp&(0x3a|net_start))|(PageIdx<<6);
	temp = (temp&0x3b)|(PageIdx<<6);						
	outportb(BaseAddr, temp);
}
/*
static void ClearRam()
{
	int i;
	SetRegPage(0);
	outport(RSAR0, 0x4000);
	outport(RBCR0, 256*32);
	outportb(BaseAddr, 0x12);
	for(i=0; i<256*26; i++)
		outportb(RWPORT, 0x0);
}

static void ReadMacID()
{
	int i;
	
	SetRegPage(1);
	for(i=0; i<6; i++)	
		SrcMacID[i]=inportb(PAR0+i);	
}
*/
static void SetMacID()
{
	int i;
	
	SetRegPage(1);
	for(i=0; i<6; i++)
		outportb(PAR0+i, SrcMacID[i]);

}

static U8 Rst8019()
{
	int i;
	
	outportb(RstAddr, 0x5a);
	i = 20000;
	while(i--);
	SetRegPage(0);
	return (inportb(ISR)&0x80);
}

static void WakeRtl8019as()
{
	SetRegPage(3);	
	outportb(CR9346, 0xcf);		//set eem1-0, 11 ,enable write config register
	outportb(CONFIG3, 0x60);	//clear pwrdn, sleep mode, set led0 as led_col, led1 as led_crs	
	outportb(CR9346, 0x3f); 	//disable write config register
}

static void InitRS8019()
{
	net_start = 1;
	outportb(BaseAddr, 0x21);	/* set page 0 and stop */
	outportb(Pstart, RPSTART);		/* set Pstart 0x4c */
	outportb(Pstop, RPSTOP);		/* set Pstop 0x80 */
	outportb(BNRY, RPSTART);	/* BNRY-> the last page has been read */	
	outportb(TPSR, SPSTART);		/* transmit page start register, 0x40 */
	outportb(RCR, 0xcc);		/* set RCR 0xcc */	
	outportb(TCR, 0xe0);		/* set TCR 0xe0 */
#ifdef	RTL8019_OP_16
	outportb(DCR, 0xc9);		/* set DCR 0xc9, 16bit DMA */	
#else
	outportb(DCR, 0xc8);		/* 8bit DMA */
#endif
	outportb(IMR, 0x03);		/* set IMR 0x03 */
	outportb(ISR, 0xff);	
	
	SetRegPage(1);	
	outportb(CURR, RPSTART+1);	
	outportb(MAR0, 0x00);
	outportb(MAR1, 0x41);
	outportb(MAR2, 0x00);
	outportb(MAR3, 0x80);
	outportb(MAR4, 0x00);
	outportb(MAR5, 0x00);
	outportb(MAR6, 0x00);
	outportb(MAR7, 0x00);
	outportb(BaseAddr, 0x22);	/* set page 0 and start */
	
	net_start = 0;	
	rBNRY = RPSTART;
}

int s3c44b0_eth_init(void)
{	
	int i;
	
	WakeRtl8019as();
	
	if(!Rst8019())
	{
//		puts("Rtl8019 Reset Failed!\n");
//		printf("%x ", inportb(ISR));		
//		return -1;
	}
//	puts("Rtl8019 Reset Successfully\n");		
		
	InitRS8019();
	
	i  = inportb(ID8019L);	
	i |= inportb(ID8019H)<<8;
	
	SetMacID();	
	
/*		//a test for arp request			
		while(1)	
		{
			int i;
			U8 data[80];
			
			for(i = 0; i<6; i++)
				data[i] = 0xff;				
			for(; i<12; i++)
				data[i] = SrcMacID[i-6];
				
			data[i++] = 8;
			data[i++] = 6;
			data[i++] = 0;
			data[i++] = 1;
			data[i++] = 8;
			data[i++] = 0;
			data[i++] = 6;
			data[i++] = 4;
			data[i++] = 0;
			data[i++] = 1;
			
			for(; i<28; i++)
				data[i] = SrcMacID[i-22];
				
			data[i++] = 0xc0;
			data[i++] = 0xa8;
			data[i++] = 0xa8;
			data[i++] = 0x65;
			
			for(; i<38; i++)
				data[i] = 0;
				
			data[i++] = 0xc0;
			data[i++] = 0xa8;
			data[i++] = 0xa8;
			data[i++] = 0x64;				
			
			s3c44b0_eth_send(data, 60);
			getch();			
		}				
*/			
/*	while(1)
	{
		int i;
		static int q = 0;
		U8 data[80];
		
		q++;
		for(i=0; i<60; i++)
			if(q&1)
				data[i] = i;
			else
				data[i] = 60-i;
				
		s3c44b0_eth_send(data, 60);
		getch();		
		//outportb(BaseAddr, 0x22);				
	}
*/
	return 0;			
}


int s3c44b0_eth_send(unsigned char *data, unsigned int len)
{
	static sFlag = 0;
	int i;
	U8 send_page;
	
	send_page  = SPSTART;
	send_page += (sFlag&1)?6:0;
	sFlag++;			
		
	if(len<60)	
		for(; len<60; len++)
			data[len] = 0x20;		//just for pad, any data										
	
	SetRegPage(0);		
	outportb(BaseAddr, 0x22);			
	outportb(RSAR0, 0);
	outportb(RSAR1, send_page);
	outportb(RBCR0, len&0xff);	
	outportb(RBCR1, len>>8);			
	outportb(BaseAddr, 0x12);							
	for(i=0; i<len; i++)
	{					
		outportb(RWPORT, data[i]);		// tarns to ram
	}	
	/*	outportb(RSAR0, 0);
		outportb(RSAR1, SPSTART);
		outportb(RBCR0, len&0xff);	
		outportb(RBCR1, len>>8);	
		outportb(BaseAddr, 0x0a);		
		for(i=0; i<len; i++)
			data[i] = inportb(RWPORT);
			printf("%x ", data[i]);					
	*/
	//outport(RSAR0, SPSTART<<8);		
	//outportb(RSAR0, 0);
	//outportb(RSAR1, SPSTART);			
		while(inportb(BaseAddr)&4);
	outportb(TPSR, send_page);
	outportb(TBCR0, len&0xff);	
	outportb(TBCR1, len>>8);				
	outportb(BaseAddr, 0x1e);			// begin to send					

	return 0;
}

int s3c44b0_eth_rcv(unsigned char *data, unsigned int *len)
{
	U8 RxPageBeg, RxPageEnd;
	U8 RxNextPage;
	U8 RxStatus;
	int i, RxLength;	 
	
	SetRegPage(0);
		outportb(BNRY, rBNRY);	//???			
	if(inportb(ISR)&1)			//接收成功
		outportb(ISR, 0x1);		//清除中断标志
	else	
		return -1;	
		
	SetRegPage(1);
	RxPageEnd = inportb(CURR);
	SetRegPage(0);	
	/*
	RxPageBeg = inportb(BNRY)+1;		
	if(RxPageBeg>=RPSTOP)
		RxPageBeg = RPSTART;		
		
	if(RxPageBeg==RxPageEnd)
		return -1;			//no data received
	*/
	RxPageBeg = rBNRY+1;
	if(RxPageBeg>=RPSTOP)
		RxPageBeg = RPSTART;		
	outportb(BaseAddr, 0x22);
		
	//outport(RSAR0, RxPageBeg<<8);
	//outport(RBCR0, 256);		
	outportb(RSAR0, 0);
	outportb(RSAR1, RxPageBeg);
	outportb(RBCR0, 4);
	outportb(RBCR1, 0);	
	outportb(BaseAddr, 0xa);
	RxStatus   = inportb(RWPORT);
	RxNextPage = inportb(RWPORT);	
	RxLength   = inportb(RWPORT);
	RxLength  |= inportb(RWPORT)<<8;
	
//		printf("\nRxBeg = %x, RxEnd = %x, staus = %x, nextpage = %x,  size = %x", RxPageBeg, RxPageEnd, RxStatus, RxNextPage, RxLength);		
	if(RxLength>ETH_FRAME_LEN)
	{
		if(RxPageEnd==RPSTART)
			rBNRY = RPSTOP-1;
		else
			rBNRY = RxPageEnd-1;						
		outportb(BNRY, rBNRY);		 	
		return -1;		
	}
	outportb(RSAR0, 4);
	outportb(RSAR1, RxPageBeg);
	outportb(RBCR0, RxLength);
	outportb(RBCR1, RxLength>>8);	
	outportb(BaseAddr, 0xa);

	i     = 4;
	data -= 4;
	for(; RxLength--;)
	{
		if(!(i&0xff))
			{
				outportb(BNRY, RxPageBeg);				
				RxPageBeg++;
				if(RxPageBeg>=RPSTOP)
					RxPageBeg = RPSTART;					
			}
		data[i++] = inportb(RWPORT);		
	}
	outportb(BNRY, RxPageBeg);	
	rBNRY = RxPageBeg;
	
	return 0;		
/*			
	//outport(RSAR0, RxPageBeg<<8);
	//outport(RBCR0, 256);
	outportb(RSAR0, 0);
	outportb(RSAR1, RxPageBeg);
	outportb(RBCR0, 0);
	outportb(RBCR1, 1);	
	outportb(BaseAddr, 0xa);
	RxStatus   = inportb(RWPORT);
	RxNextPage = inportb(RWPORT);	
	RxLength   = inportb(RWPORT);
	RxLength  |= inportb(RWPORT)<<8;
	
		printf("\nRxBeg = %x, RxEnd = %x, staus = %x, nextpage = %x,  size = %x", RxPageBeg, RxPageEnd, RxStatus, RxNextPage, RxLength);		
	
	i     = 4;
	data -= 4;
	do{
		data[i++] = inportb(RWPORT);									
	}while(i&0xff);			
	
	if(RxLength>ETH_FRAME_LEN)
	{
		if(RxPageEnd==RPSTART)
			rBNRY = RPSTOP-1;
		else
			rBNRY = RxPageEnd-1;						
		outportb(BNRY, rBNRY);		 	
		return -1;		
	}		
	
	RxLength -= 252;	
	if(RxLength>0)	
	{
		outportb(RSAR0, 0);
		outportb(RSAR1, RxPageBeg);
		outportb(RBCR0, RxLength&0xff);
		outportb(RBCR1, RxLength>>8);		
		outportb(BaseAddr, 0xa);
		for(; RxLength--;)
		{										
			if(!(i&0xff))
			{
//				outportb(BNRY, RxPageBeg);				
				RxPageBeg++;
				if(RxPageBeg>=RPSTOP)
					RxPageBeg = RPSTART;					
			}
			data[i++] = inportb(RWPORT);
		}				
	}	
	outportb(BNRY, RxPageBeg);	
	rBNRY = RxPageBeg;		
			
//	printf("-%x,%x\n", RxNextPage-1, inportb(BNRY));		
		
	return 0;*/
}

int s3c44b0_eth_get_addr(unsigned char *addr)
{
	memcpy(addr, SrcMacID, ETH_ALEN);
	return 0;
}
