📄 fec.c.svn-base
字号:
/* * (C) Copyright 2000 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * * See file CREDITS for list of people who contributed to this * project. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */#include <common.h>#include <malloc.h>#include <commproc.h>#include <net.h>#include <command.h>DECLARE_GLOBAL_DATA_PTR;#undef ET_DEBUG#if (CONFIG_COMMANDS & CFG_CMD_NET) && \ (defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FEC1) || defined(CONFIG_ETHER_ON_FEC2))/* compatibility test, if only FEC_ENET defined assume ETHER on FEC1 */#if defined(FEC_ENET) && !defined(CONFIG_ETHER_ON_FEC1) && !defined(CONFIG_ETHER_ON_FEC2)#define CONFIG_ETHER_ON_FEC1 1#endif/* define WANT_MII when MII support is required */#if defined(CFG_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)#define WANT_MII#else#undef WANT_MII#endif#if defined(WANT_MII)#include <miiphy.h>#if !(defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII))#error "CONFIG_MII has to be defined!"#endif#endif#if defined(CONFIG_RMII) && !defined(WANT_MII)#error RMII support is unusable without a working PHY.#endif#ifdef CFG_DISCOVER_PHYstatic int mii_discover_phy(struct eth_device *dev);#endifint fec8xx_miiphy_read(char *devname, unsigned char addr, unsigned char reg, unsigned short *value);int fec8xx_miiphy_write(char *devname, unsigned char addr, unsigned char reg, unsigned short value);static struct ether_fcc_info_s{ int ether_index; int fecp_offset; int phy_addr; int actual_phy_addr; int initialized;} ether_fcc_info[] = {#if defined(CONFIG_ETHER_ON_FEC1) { 0, offsetof(immap_t, im_cpm.cp_fec1),#if defined(CONFIG_FEC1_PHY) CONFIG_FEC1_PHY,#else -1, /* discover */#endif -1, 0, },#endif#if defined(CONFIG_ETHER_ON_FEC2) { 1, offsetof(immap_t, im_cpm.cp_fec2),#if defined(CONFIG_FEC2_PHY) CONFIG_FEC2_PHY,#else -1,#endif -1, 0, },#endif};/* Ethernet Transmit and Receive Buffers */#define DBUF_LENGTH 1520#define TX_BUF_CNT 2#define TOUT_LOOP 100#define PKT_MAXBUF_SIZE 1518#define PKT_MINBUF_SIZE 64#define PKT_MAXBLR_SIZE 1520#ifdef __GNUC__static char txbuf[DBUF_LENGTH] __attribute__ ((aligned(8)));#else#error txbuf must be aligned.#endifstatic uint rxIdx; /* index of the current RX buffer */static uint txIdx; /* index of the current TX buffer *//* * FEC Ethernet Tx and Rx buffer descriptors allocated at the * immr->udata_bd address on Dual-Port RAM * Provide for Double Buffering */typedef volatile struct CommonBufferDescriptor { cbd_t rxbd[PKTBUFSRX]; /* Rx BD */ cbd_t txbd[TX_BUF_CNT]; /* Tx BD */} RTXBD;static RTXBD *rtx = NULL;static int fec_send(struct eth_device* dev, volatile void *packet, int length);static int fec_recv(struct eth_device* dev);static int fec_init(struct eth_device* dev, bd_t * bd);static void fec_halt(struct eth_device* dev);int fec_initialize(bd_t *bis){ struct eth_device* dev; struct ether_fcc_info_s *efis; int i; for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++) { dev = malloc(sizeof(*dev)); if (dev == NULL) hang(); memset(dev, 0, sizeof(*dev)); /* for FEC1 make sure that the name of the interface is the same as the old one for compatibility reasons */ if (i == 0) { sprintf (dev->name, "FEC ETHERNET"); } else { sprintf (dev->name, "FEC%d ETHERNET", ether_fcc_info[i].ether_index + 1); } efis = ðer_fcc_info[i]; /* * reset actual phy addr */ efis->actual_phy_addr = -1; dev->priv = efis; dev->init = fec_init; dev->halt = fec_halt; dev->send = fec_send; dev->recv = fec_recv; eth_register(dev);#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII) miiphy_register(dev->name, fec8xx_miiphy_read, fec8xx_miiphy_write);#endif } return 1;}static int fec_send(struct eth_device* dev, volatile void *packet, int length){ int j, rc; struct ether_fcc_info_s *efis = dev->priv; volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset); /* section 16.9.23.3 * Wait for ready */ j = 0; while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) { udelay(1); j++; } if (j>=TOUT_LOOP) { printf("TX not ready\n"); } rtx->txbd[txIdx].cbd_bufaddr = (uint)packet; rtx->txbd[txIdx].cbd_datlen = length; rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST; __asm__ ("eieio"); /* Activate transmit Buffer Descriptor polling */ fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */ j = 0; while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {#if defined(CONFIG_ICU862) udelay(10);#else udelay(1);#endif j++; } if (j>=TOUT_LOOP) { printf("TX timeout\n"); }#ifdef ET_DEBUG printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n", __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc, (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2);#endif /* return only status bits */; rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS); txIdx = (txIdx + 1) % TX_BUF_CNT; return rc;}static int fec_recv (struct eth_device *dev){ struct ether_fcc_info_s *efis = dev->priv; volatile fec_t *fecp = (volatile fec_t *) (CFG_IMMR + efis->fecp_offset); int length; for (;;) { /* section 16.9.23.2 */ if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) { length = -1; break; /* nothing received - leave for() loop */ } length = rtx->rxbd[rxIdx].cbd_datlen; if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {#ifdef ET_DEBUG printf ("%s[%d] err: %x\n", __FUNCTION__, __LINE__, rtx->rxbd[rxIdx].cbd_sc);#endif } else { volatile uchar *rx = NetRxPackets[rxIdx]; length -= 4;#if (CONFIG_COMMANDS & CFG_CMD_CDP) if ((rx[0] & 1) != 0 && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0 && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0) rx = NULL;#endif /* * Pass the packet up to the protocol layers. */ if (rx != NULL) NetReceive (rx, length); } /* Give the buffer back to the FEC. */ rtx->rxbd[rxIdx].cbd_datlen = 0; /* wrap around buffer index when necessary */ if ((rxIdx + 1) >= PKTBUFSRX) { rtx->rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY); rxIdx = 0; } else { rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY; rxIdx++; } __asm__ ("eieio"); /* Try to fill Buffer Descriptors */ fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */ } return length;}/************************************************************** * * FEC Ethernet Initialization Routine * *************************************************************/#define FEC_ECNTRL_PINMUX 0x00000004#define FEC_ECNTRL_ETHER_EN 0x00000002#define FEC_ECNTRL_RESET 0x00000001#define FEC_RCNTRL_BC_REJ 0x00000010#define FEC_RCNTRL_PROM 0x00000008#define FEC_RCNTRL_MII_MODE 0x00000004#define FEC_RCNTRL_DRT 0x00000002#define FEC_RCNTRL_LOOP 0x00000001#define FEC_TCNTRL_FDEN 0x00000004#define FEC_TCNTRL_HBC 0x00000002#define FEC_TCNTRL_GTS 0x00000001#define FEC_RESET_DELAY 50#if defined(CONFIG_RMII)static inline void fec_10Mbps(struct eth_device *dev){ struct ether_fcc_info_s *efis = dev->priv; int fecidx = efis->ether_index; uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008; if ((unsigned int)fecidx >= 2) hang(); ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr |= mask;}static inline void fec_100Mbps(struct eth_device *dev){ struct ether_fcc_info_s *efis = dev->priv; int fecidx = efis->ether_index; uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008; if ((unsigned int)fecidx >= 2) hang(); ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr &= ~mask;}#endifstatic inline void fec_full_duplex(struct eth_device *dev){ struct ether_fcc_info_s *efis = dev->priv; volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset); fecp->fec_r_cntrl &= ~FEC_RCNTRL_DRT; fecp->fec_x_cntrl |= FEC_TCNTRL_FDEN; /* FD enable */}static inline void fec_half_duplex(struct eth_device *dev){ struct ether_fcc_info_s *efis = dev->priv; volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset); fecp->fec_r_cntrl |= FEC_RCNTRL_DRT; fecp->fec_x_cntrl &= ~FEC_TCNTRL_FDEN; /* FD disable */}static void fec_pin_init(int fecidx){ bd_t *bd = gd->bd; volatile immap_t *immr = (immap_t *) CFG_IMMR; volatile fec_t *fecp; /* * only two FECs please */ if ((unsigned int)fecidx >= 2) hang(); if (fecidx == 0) fecp = &immr->im_cpm.cp_fec1; else fecp = &immr->im_cpm.cp_fec2; /* * Set MII speed to 2.5 MHz or slightly below. * * According to the MPC860T (Rev. D) Fast ethernet controller user * * manual (6.2.14), * * the MII management interface clock must be less than or equal * * to 2.5 MHz. * * This MDC frequency is equal to system clock / (2 * MII_SPEED). * * Then MII_SPEED = system_clock / 2 * 2,5 Mhz. */ fecp->fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1;#if defined(CONFIG_NETTA) || defined(CONFIG_NETPHONE) || defined(CONFIG_NETTA2) /* our PHYs are the limit at 2.5 MHz */ fecp->fec_mii_speed <<= 1;#endif#if defined(CONFIG_MPC885_FAMILY) && defined(WANT_MII) /* use MDC for MII */ immr->im_ioport.iop_pdpar |= 0x0080; immr->im_ioport.iop_pddir &= ~0x0080;#endif if (fecidx == 0) {#if defined(CONFIG_ETHER_ON_FEC1)#if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */#if !defined(CONFIG_RMII) immr->im_ioport.iop_papar |= 0xf830; immr->im_ioport.iop_padir |= 0x0830; immr->im_ioport.iop_padir &= ~0xf000; immr->im_cpm.cp_pbpar |= 0x00001001; immr->im_cpm.cp_pbdir &= ~0x00001001; immr->im_ioport.iop_pcpar |= 0x000c; immr->im_ioport.iop_pcdir &= ~0x000c; immr->im_cpm.cp_pepar |= 0x00000003; immr->im_cpm.cp_pedir |= 0x00000003; immr->im_cpm.cp_peso &= ~0x00000003; immr->im_cpm.cp_cptr &= ~0x00000100;#else#if !defined(CONFIG_FEC1_PHY_NORXERR) immr->im_ioport.iop_papar |= 0x1000; immr->im_ioport.iop_padir &= ~0x1000;#endif immr->im_ioport.iop_papar |= 0xe810; immr->im_ioport.iop_padir |= 0x0810; immr->im_ioport.iop_padir &= ~0xe000; immr->im_cpm.cp_pbpar |= 0x00000001; immr->im_cpm.cp_pbdir &= ~0x00000001; immr->im_cpm.cp_cptr |= 0x00000100; immr->im_cpm.cp_cptr &= ~0x00000050;#endif /* !CONFIG_RMII */#elif !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210) /* * Configure all of port D for MII. */ immr->im_ioport.iop_pdpar = 0x1fff; /* * Bits moved from Rev. D onward */ if ((get_immr(0) & 0xffff) < 0x0501) immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */ else immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */#else /* * Configure port A for MII. */#if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY) /* * On the ICU862 board the MII-MDC pin is routed to PD8 pin * * of CPU, so for this board we need to configure Utopia and * * enable PD8 to MII-MDC function */ immr->im_ioport.iop_pdpar |= 0x4080;#endif /* * Has Utopia been configured? */ if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) { /* * YES - Use MUXED mode for UTOPIA bus. * This frees Port A for use by MII (see 862UM table 41-6). */ immr->im_ioport.utmode &= ~0x80; } else { /* * NO - set SPLIT mode for UTOPIA bus. * * This doesn't really effect UTOPIA (which isn't * enabled anyway) but just tells the 862 * to use port A for MII (see 862UM table 41-6). */ immr->im_ioport.utmode |= 0x80; }#endif /* !defined(CONFIG_ICU862) */#endif /* CONFIG_ETHER_ON_FEC1 */ } else if (fecidx == 1) {#if defined(CONFIG_ETHER_ON_FEC2)#if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */#if !defined(CONFIG_RMII)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -