📄 mcf5xxx_fecx.c
字号:
/*
* File: fecx.c
* Purpose: Driver for the Fast Ethernet Controller (FEC)
*
* Notes: For use with processors featuring multiple
* ethernet controllers
*/
#include "src/include/dbug.h"
#include "src/uif/net/net.h"
#include "src/cpu/coldfire/mcf5xxx/mcf5xxx_fecx.h"
#include "src/cpu/coldfire/mcf5xxx/mcf5xxx_fecbd.h"
#ifdef DBUG_NETWORK
/*
* Number of FEC channels supported
*/
#ifndef FEC_NUM_CH
#define FEC_NUM_CH 1
#endif
/********************************************************************/
FEC_EVENT_LOG fec_log[FEC_NUM_CH];
/********************************************************************/
/*
* Write a value to a PHY's MII register.
*
* Parameters:
* ch FEC channel
* phy_addr Address of the PHY.
* reg_addr Address of the register in the PHY.
* data Data to be written to the PHY register.
*
* Return Values:
* 1 on failure
* 0 on success.
*
* Please refer to your PHY manual for registers and their meanings.
* mii_write() polls for the FEC's MII interrupt event (which should
* be masked from the interrupt handler) and clears it. If after a
* suitable amount of time the event isn't triggered, a value of 0
* is returned.
*/
int
fec_mii_write(uint8 ch, uint8 phy_addr, uint8 reg_addr, uint16 data)
{
int timeout;
uint32 eimr;
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
/*
* Clear the MII interrupt bit
*/
MCF_FEC_EIR(ch) = MCF_FEC_EIR_MII;
/*
* Write to the MII Management Frame Register to kick-off
* the MII write
*/
MCF_FEC_MMFR(ch) = 0
| MCF_FEC_MMFR_ST_01
| MCF_FEC_MMFR_OP_WRITE
| MCF_FEC_MMFR_PA(phy_addr)
| MCF_FEC_MMFR_RA(reg_addr)
| MCF_FEC_MMFR_TA_10
| MCF_FEC_MMFR_DATA(data);
/*
* Mask the MII interrupt
*/
eimr = MCF_FEC_EIMR(ch);
MCF_FEC_EIMR(ch) &= ~MCF_FEC_EIMR_MII;
/*
* Poll for the MII interrupt (interrupt should be masked)
*/
for (timeout = 0; timeout < FEC_MII_TIMEOUT; timeout++)
{
if (MCF_FEC_EIR(ch) & MCF_FEC_EIR_MII)
break;
}
if(timeout == FEC_MII_TIMEOUT)
return 1;
/*
* Clear the MII interrupt bit
*/
MCF_FEC_EIR(ch) = MCF_FEC_EIR_MII;
/*
* Restore the EIMR
*/
MCF_FEC_EIMR(ch) = eimr;
return 0;
}
/********************************************************************/
/*
* Read a value from a PHY's MII register.
*
* Parameters:
* ch FEC channel
* phy_addr Address of the PHY.
* reg_addr Address of the register in the PHY.
* data Pointer to storage for the Data to be read
* from the PHY register (passed by reference)
*
* Return Values:
* 1 on failure
* 0 on success.
*
* Please refer to your PHY manual for registers and their meanings.
* mii_read() polls for the FEC's MII interrupt event (which should
* be masked from the interrupt handler) and clears it. If after a
* suitable amount of time the event isn't triggered, a value of 0
* is returned.
*/
int
fec_mii_read(uint8 ch, uint8 phy_addr, uint8 reg_addr, uint16 *data)
{
int timeout;
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
/*
* Clear the MII interrupt bit
*/
MCF_FEC_EIR(ch) = MCF_FEC_EIR_MII;
/*
* Write to the MII Management Frame Register to kick-off
* the MII read
*/
MCF_FEC_MMFR(ch) = 0
| MCF_FEC_MMFR_ST_01
| MCF_FEC_MMFR_OP_READ
| MCF_FEC_MMFR_PA(phy_addr)
| MCF_FEC_MMFR_RA(reg_addr)
| MCF_FEC_MMFR_TA_10;
/*
* Poll for the MII interrupt (interrupt should be masked)
*/
for (timeout = 0; timeout < FEC_MII_TIMEOUT; timeout++)
{
if (MCF_FEC_EIR(ch) & MCF_FEC_EIR_MII)
break;
}
if(timeout == FEC_MII_TIMEOUT)
return 1;
/*
* Clear the MII interrupt bit
*/
MCF_FEC_EIR(ch) = MCF_FEC_EIR_MII;
*data = (uint16)(MCF_FEC_MMFR(ch) & 0x0000FFFF);
return 0;
}
/********************************************************************/
/*
* Initialize the MII interface controller
*
* Parameters:
* ch FEC channel
* sys_clk System Clock Frequency (in MHz)
*/
void
fec_mii_init(uint8 ch, uint32 sys_clk)
{
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
/*
* Initialize the MII clock (EMDC) frequency
*
* Desired MII clock is 2.5MHz
* MII Speed Setting = System_Clock / (2.5MHz * 2)
* (plus 1 to make sure we round up)
*/
MCF_FEC_MSCR(ch) = MCF_FEC_MSCR_MII_SPEED((sys_clk/5)+1);
}
/********************************************************************/
/* Initialize the MIB counters
*
* Parameters:
* ch FEC channel
*/
void
fec_mib_init(uint8 ch)
{
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
//To do
}
/********************************************************************/
/* Display the MIB counters
*
* Parameters:
* ch FEC channel
*/
void
fec_mib_dump(uint8 ch)
{
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
//To do
}
/********************************************************************/
/* Initialize the FEC log
*
* Parameters:
* ch FEC channel
*/
void
fec_log_init(uint8 ch)
{
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
memset(&fec_log[ch],0,sizeof(FEC_EVENT_LOG));
}
/********************************************************************/
/* Display the FEC log
*
* Parameters:
* ch FEC channel
*/
void
fec_log_dump(uint8 ch)
{
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
ASSERT(ch < FEC_NUM_CH);
printf("\n FEC%d Log\n---------------\n",ch);
printf("Total: %4d\n",fec_log[ch].errors);
printf("hberr: %4d\n",fec_log[ch].hberr);
printf("babr: %4d\n",fec_log[ch].babr);
printf("babt: %4d\n",fec_log[ch].babt);
printf("gra: %4d\n",fec_log[ch].gra);
printf("txf: %4d\n",fec_log[ch].txf);
printf("txb: %4d\n",fec_log[ch].txb);
printf("rxf: %4d\n",fec_log[ch].rxf);
printf("rxb: %4d\n",fec_log[ch].rxb);
printf("mii: %4d\n",fec_log[ch].mii);
printf("eberr: %4d\n",fec_log[ch].eberr);
printf("lc: %4d\n",fec_log[ch].lc);
printf("rl: %4d\n",fec_log[ch].rl);
printf("un: %4d\n",fec_log[ch].un);
printf("\nRFSW:\n");
printf("inv: %4d\n",fec_log[ch].rfsw_inv);
printf("l: %4d\n",fec_log[ch].rfsw_l);
printf("m: %4d\n",fec_log[ch].rfsw_m);
printf("bc: %4d\n",fec_log[ch].rfsw_bc);
printf("mc: %4d\n",fec_log[ch].rfsw_mc);
printf("lg: %4d\n",fec_log[ch].rfsw_lg);
printf("no: %4d\n",fec_log[ch].rfsw_no);
printf("cr: %4d\n",fec_log[ch].rfsw_cr);
printf("ov: %4d\n",fec_log[ch].rfsw_ov);
printf("tr: %4d\n",fec_log[ch].rfsw_tr);
printf("---------------\n\n");
}
/********************************************************************/
/*
* Display some of the registers for debugging
*
* Parameters:
* ch FEC channel
*/
void
fec_debug_dump(uint8 ch)
{
#ifdef DEBUG
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
printf("\n------------- FEC%d -------------\n");
printf("EIR %08x \n",MCF_FEC_EIR(ch));
printf("EIMR %08x \n",MCF_FEC_EIMR(ch));
printf("ECR %08x \n",MCF_FEC_ECR(ch));
printf("RCR %08x \n",MCF_FEC_RCR(ch));
printf("TCR %08x \n",MCF_FEC_TCR(ch));
printf("--------------------------------\n\n");
#endif
}
/********************************************************************/
/*
* Set the duplex on the selected FEC controller
*
* Parameters:
* ch FEC channel
* duplex FEC_MII_FULL_DUPLEX or FEC_MII_HALF_DUPLEX
*/
void
fec_duplex (uint8 ch, uint8 duplex)
{
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
switch (duplex)
{
case FEC_MII_HALF_DUPLEX:
MCF_FEC_RCR(ch) |= MCF_FEC_RCR_DRT;
MCF_FEC_TCR(ch) &= (uint32)~MCF_FEC_TCR_FDEN;
break;
case FEC_MII_FULL_DUPLEX:
default:
MCF_FEC_RCR(ch) &= (uint32)~MCF_FEC_RCR_DRT;
MCF_FEC_TCR(ch) |= MCF_FEC_TCR_FDEN;
break;
}
}
/********************************************************************/
/*
* Generate the hash table settings for the given address
*
* Parameters:
* addr 48-bit (6 byte) Address to generate the hash for
*
* Return Value:
* The 6 most significant bits of the 32-bit CRC result
*/
uint8
fec_hash_address(const uint8 *addr)
{
uint32 crc;
uint8 byte;
int i, j;
crc = 0xFFFFFFFF;
for(i=0; i<6; ++i)
{
byte = addr[i];
for(j=0; j<8; ++j)
{
if((byte & 0x01)^(crc & 0x01))
{
crc >>= 1;
crc = crc ^ 0xEDB88320;
}
else
crc >>= 1;
byte >>= 1;
}
}
return (uint8)(crc >> 26);
}
/********************************************************************/
/*
* Set the Physical (Hardware) Address and the Individual Address
* Hash in the selected FEC
*
* Parameters:
* ch FEC channel
* pa Physical (Hardware) Address for the selected FEC
*/
void
fec_set_address (uint8 ch, const uint8 *pa)
{
uint8 crc;
#if (FEC_NUM_CH == 1)
ch = 0;
#else
ASSERT(ch < FEC_NUM_CH);
#endif
/*
* Set the Physical Address
*/
MCF_FEC_PALR(ch) = (uint32)((pa[0]<<24)|(pa[1]<<16)|(pa[2]<<8)|pa[3]);
MCF_FEC_PAUR(ch) = (uint32)((pa[4]<<24)|(pa[5]<<16));
/*
* Calculate and set the hash for given Physical Address
* in the Individual Address Hash registers
*/
crc = fec_hash_address(pa);
if(crc >= 32)
MCF_FEC_IAUR(ch) |= (uint32)(1 << (crc - 32));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -