📄 mac.c
字号:
/*修改说明:加高电压后,去掉了SetRegPage中判断net_start标志*/
#include "DataType.h"
#include "eth.h"
#include "utils.h"
#include "mac.h"
#include "Rtl8019.h"
#include "skbuff.h"
#define DEBUG_NET
#define RPSTART 0x4c
#define RPSTOP 0x80
#define SPSTART 0x40
extern int rd_p,wr_p,total_packet;
extern struct sk_buff skbuff[50];
extern struct sk_buff *skb_g;
static U8 rBNRY;
static U8 SrcMacID[ETH_ALEN] = {0x00,0x80,0x48,0x12,0x34,0x56};
//static U8 DstMacID[ETH_ALEN];
static U8 net_start;
U8 bnry;
U8 curr;
extern int SemPost;
void SetRegPage(U8 PageIdx)
{
U8 temp;
temp = inportb(BaseAddr);
// temp = (temp&(0x3a|net_start))|(PageIdx<<6); //ensure bit0 clear!!!
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()
{
SetRegPage(1);
SrcMacID[0]=inportb(PAR0);
SrcMacID[1]=inportb(PAR1);
SrcMacID[2]=inportb(PAR2);
SrcMacID[3]=inportb(PAR3);
SrcMacID[4]=inportb(PAR4);
SrcMacID[5]=inportb(PAR5);
}
*/
static void SetMacID()
{
SetRegPage(1);
outportb(PAR0, SrcMacID[0]);
outportb(PAR1, SrcMacID[1]);
outportb(PAR2, SrcMacID[2]);
outportb(PAR3, SrcMacID[3]);
outportb(PAR4, SrcMacID[4]);
outportb(PAR5, SrcMacID[5]);
}
static U8 Rst8019()
{
int i;
Uart_Printf("Rst8019中ISR0--->%x\n", *((volatile unsigned char *)(ISR)));
outportb(RstAddr, 0x5a);
Uart_Printf("Rst8019中outportb(RstAddr, 0x5a);\n");
i = 20000;
Uart_Printf("Rst8019中i = 20000;\n");
while(i--);
Uart_Printf("Rst8019中while(i--);\n");
SetRegPage(0);
Uart_Printf("Rst8019中SetRegPage(0);\n");
Uart_Printf("Rst8019中ISR1--->%x\n", *((volatile unsigned char *)(ISR)));
Uart_Printf("Rst8019中ISR2--->%x\n", *((volatile unsigned char *)(ISR)));
Uart_Printf("Rst8019中ISR3--->%x\n", *((volatile unsigned char *)(ISR)));
//Uart_Printf("Rst8019中ISR4--->%x\n", *((volatile unsigned char *)(ISR)));
return (inportb(ISR)&0x80);
}
static void WakeRtl8019as()
{
SetRegPage(3);
outportb(CR9346, 0xcf); //set eem1-0, 11 ,enable write config register
outportb(CONFIG3, 0x70); //clear pwrdn, sleep mode, set led0 as led_link, led1 as led_rx
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, 0x01); /* set IMR 0x01 */
outportb(ISR, 0xff);
//Uart_Printf("InitRS8019中ISR----%x\n", *((volatile unsigned char *)(ISR)));
SetRegPage(1);
outportb(CURR, RPSTART+1);
outportb(MAR0, 0x00);
//Uart_Printf("InitRS8019中MAR0----%x\n", *((volatile unsigned char *)(MAR0)));
outportb(MAR1, 0x41);
//Uart_Printf("InitRS8019中MAR1----%x\n", *((volatile unsigned char *)(MAR1)));
outportb(MAR2, 0x00);
outportb(MAR3, 0x80);
//Uart_Printf("InitRS8019中MAR3----%x\n", *((volatile unsigned char *)(MAR3)));
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())
{
Uart_Printf("Rtl8019 Reset Failed!\n");
Uart_Printf("ISR--->%x\n", *((volatile unsigned char *)(ISR)));
return -1;
}
Uart_Printf("Rtl8019 Reset Successfully\n");
InitRS8019();
//Uart_Printf("InitRS8019\n");
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);
#ifdef RTL8019_OP_16
len += len&1;
for(i=0; i<(len>>1); i++)
outportw(RWPORT, ((U16 *)data)[i]);
#else
for(i=0; i<len; i++)
outportb(RWPORT, data[i]); // tarns to ram
#endif
/* 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_buff(void)
{
if((rd_p==wr_p)&&total_packet==0)
{
//Uart_Printf("缓冲区为空!\n");
//Uart_Printf("缓冲区为空total_packet-->%x!\n",total_packet);
return -1;
}
//Uart_Printf("total_packet1-->%x!\n",total_packet);
//Uart_Printf("rd_p-->%x!\n",rd_p);
//Uart_Printf("wr_p-->%x!\n",wr_p);
//OS_ENTER_CRITICAL();
skb_g = (struct sk_buff *)&skbuff[rd_p];
if((rd_p==wr_p)&&(total_packet==1))
{
total_packet=0;
}
else
{
rd_p = (rd_p + 1)%BUFF_LEN;
total_packet = total_packet - 1;
}
//OS_EXIT_CRITICAL();
//Uart_Printf("total_packet2-->%x!\n",total_packet);
return 0;
}
int s3c44b0_eth_rcv(unsigned char *data, unsigned int *len)
{
U8 RxPageBeg, RxPageEnd;
U8 RxNextPage;
U8 RxStatus;
int i, RxLength;
U16 *data_16;
data_16 = (U16 *)data;
SetRegPage(0);
outportb(BNRY, rBNRY); //???
if(inportb(ISR)&1){ //接收成功
outportb(ISR, 0xff); //清除中断标志
//Uart_Printf("Rtl8019 接收成功!\n");
}
else{
//Uart_Printf("Rtl8019 接收失败!\n");
//return -1;
}
//outportb(ISR, 0xff); //清除中断标志
//Uart_Printf("isr----%x\n", *((volatile unsigned char *)(ISR)));
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);
#ifdef RTL8019_OP_16
RxLength = inportw(RWPORT);
RxStatus = RxLength&0xff;
RxNextPage = RxLength>>8;
RxLength = inportw(RWPORT);
RxLength += RxLength&1;
#else
RxStatus = inportb(RWPORT);
RxNextPage = inportb(RWPORT);
RxLength = inportb(RWPORT);
RxLength |= inportb(RWPORT)<<8;
#endif
// printf("\nRxBeg = %x, RxEnd = %x, staus = %x, nextpage = %x, size = %x", RxPageBeg, RxPageEnd, RxStatus, RxNextPage, RxLength);
// printf("\nbeg=%x,len=%x", RxPageBeg, 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);
#ifdef RTL8019_OP_16
i = 2;
data_16 -= i;
RxLength >>= 1;
for(; RxLength--;) {
if(!(i&0x7f)) {
outportb(BNRY, RxPageBeg);
RxPageBeg++;
if(RxPageBeg>=RPSTOP)
RxPageBeg = RPSTART;
}
data_16[i++] = inportw(RWPORT);
// printf("%x,", data_16[i-1]);
}
#else
i = 4;
data -= i;
for(; RxLength--;)
{
if(!(i&0xff))
{
outportb(BNRY, RxPageBeg);
RxPageBeg++;
if(RxPageBeg>=RPSTOP)
RxPageBeg = RPSTART;
}
data[i++] = inportb(RWPORT);
}
#endif
outportb(BNRY, RxPageBeg);
rBNRY = RxPageBeg;
//Uart_Printf("BNRY-->%x\n",RxPageBeg);
//Uart_Printf("CURR-->%x\n",RxPageEnd);
bnry = RxPageBeg;
curr = RxPageEnd;
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -