3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 DECLARE_GLOBAL_DATA_PTR;
34 #if (CONFIG_COMMANDS & CFG_CMD_NET) && \
35 (defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FEC1) || defined(CONFIG_ETHER_ON_FEC2))
37 /* compatibility test, if only FEC_ENET defined assume ETHER on FEC1 */
38 #if defined(FEC_ENET) && !defined(CONFIG_ETHER_ON_FEC1) && !defined(CONFIG_ETHER_ON_FEC2)
39 #define CONFIG_ETHER_ON_FEC1 1
42 /* define WANT_MII when MII support is required */
43 #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)
52 #if !(defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII))
53 #error "CONFIG_MII has to be defined!"
58 #if defined(CONFIG_RMII) && !defined(WANT_MII)
59 #error RMII support is unusable without a working PHY.
62 #ifdef CFG_DISCOVER_PHY
63 static int mii_discover_phy(struct eth_device *dev);
66 int fec8xx_miiphy_read(char *devname, unsigned char addr,
67 unsigned char reg, unsigned short *value);
68 int fec8xx_miiphy_write(char *devname, unsigned char addr,
69 unsigned char reg, unsigned short value);
71 static struct ether_fcc_info_s
80 #if defined(CONFIG_ETHER_ON_FEC1)
83 offsetof(immap_t, im_cpm.cp_fec1),
84 #if defined(CONFIG_FEC1_PHY)
94 #if defined(CONFIG_ETHER_ON_FEC2)
97 offsetof(immap_t, im_cpm.cp_fec2),
98 #if defined(CONFIG_FEC2_PHY)
109 /* Ethernet Transmit and Receive Buffers */
110 #define DBUF_LENGTH 1520
114 #define TOUT_LOOP 100
116 #define PKT_MAXBUF_SIZE 1518
117 #define PKT_MINBUF_SIZE 64
118 #define PKT_MAXBLR_SIZE 1520
121 static char txbuf[DBUF_LENGTH] __attribute__ ((aligned(8)));
123 #error txbuf must be aligned.
126 static uint rxIdx; /* index of the current RX buffer */
127 static uint txIdx; /* index of the current TX buffer */
130 * FEC Ethernet Tx and Rx buffer descriptors allocated at the
131 * immr->udata_bd address on Dual-Port RAM
132 * Provide for Double Buffering
135 typedef volatile struct CommonBufferDescriptor {
136 cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
137 cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
140 static RTXBD *rtx = NULL;
142 static int fec_send(struct eth_device* dev, volatile void *packet, int length);
143 static int fec_recv(struct eth_device* dev);
144 static int fec_init(struct eth_device* dev, bd_t * bd);
145 static void fec_halt(struct eth_device* dev);
147 int fec_initialize(bd_t *bis)
149 struct eth_device* dev;
150 struct ether_fcc_info_s *efis;
153 for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++) {
155 dev = malloc(sizeof(*dev));
159 memset(dev, 0, sizeof(*dev));
161 /* for FEC1 make sure that the name of the interface is the same
162 as the old one for compatibility reasons */
164 sprintf (dev->name, "FEC ETHERNET");
166 sprintf (dev->name, "FEC%d ETHERNET",
167 ether_fcc_info[i].ether_index + 1);
170 efis = ðer_fcc_info[i];
173 * reset actual phy addr
175 efis->actual_phy_addr = -1;
178 dev->init = fec_init;
179 dev->halt = fec_halt;
180 dev->send = fec_send;
181 dev->recv = fec_recv;
185 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
186 miiphy_register(dev->name,
187 fec8xx_miiphy_read, fec8xx_miiphy_write);
193 static int fec_send(struct eth_device* dev, volatile void *packet, int length)
196 struct ether_fcc_info_s *efis = dev->priv;
197 volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
203 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
208 printf("TX not ready\n");
211 rtx->txbd[txIdx].cbd_bufaddr = (uint)packet;
212 rtx->txbd[txIdx].cbd_datlen = length;
213 rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
216 /* Activate transmit Buffer Descriptor polling */
217 fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
220 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
221 #if defined(CONFIG_ICU862)
229 printf("TX timeout\n");
232 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
233 __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc,
234 (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2);
236 /* return only status bits */;
237 rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
239 txIdx = (txIdx + 1) % TX_BUF_CNT;
244 static int fec_recv (struct eth_device *dev)
246 struct ether_fcc_info_s *efis = dev->priv;
247 volatile fec_t *fecp =
248 (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
252 /* section 16.9.23.2 */
253 if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
255 break; /* nothing received - leave for() loop */
258 length = rtx->rxbd[rxIdx].cbd_datlen;
260 if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
262 printf ("%s[%d] err: %x\n",
263 __FUNCTION__, __LINE__,
264 rtx->rxbd[rxIdx].cbd_sc);
267 volatile uchar *rx = NetRxPackets[rxIdx];
271 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
273 && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0
274 && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0)
278 * Pass the packet up to the protocol layers.
281 NetReceive (rx, length);
284 /* Give the buffer back to the FEC. */
285 rtx->rxbd[rxIdx].cbd_datlen = 0;
287 /* wrap around buffer index when necessary */
288 if ((rxIdx + 1) >= PKTBUFSRX) {
289 rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
290 (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
293 rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
299 /* Try to fill Buffer Descriptors */
300 fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
306 /**************************************************************
308 * FEC Ethernet Initialization Routine
310 *************************************************************/
312 #define FEC_ECNTRL_PINMUX 0x00000004
313 #define FEC_ECNTRL_ETHER_EN 0x00000002
314 #define FEC_ECNTRL_RESET 0x00000001
316 #define FEC_RCNTRL_BC_REJ 0x00000010
317 #define FEC_RCNTRL_PROM 0x00000008
318 #define FEC_RCNTRL_MII_MODE 0x00000004
319 #define FEC_RCNTRL_DRT 0x00000002
320 #define FEC_RCNTRL_LOOP 0x00000001
322 #define FEC_TCNTRL_FDEN 0x00000004
323 #define FEC_TCNTRL_HBC 0x00000002
324 #define FEC_TCNTRL_GTS 0x00000001
326 #define FEC_RESET_DELAY 50
328 #if defined(CONFIG_RMII)
330 static inline void fec_10Mbps(struct eth_device *dev)
332 struct ether_fcc_info_s *efis = dev->priv;
333 int fecidx = efis->ether_index;
334 uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
336 if ((unsigned int)fecidx >= 2)
339 ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr |= mask;
342 static inline void fec_100Mbps(struct eth_device *dev)
344 struct ether_fcc_info_s *efis = dev->priv;
345 int fecidx = efis->ether_index;
346 uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
348 if ((unsigned int)fecidx >= 2)
351 ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr &= ~mask;
356 static inline void fec_full_duplex(struct eth_device *dev)
358 struct ether_fcc_info_s *efis = dev->priv;
359 volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
361 fecp->fec_r_cntrl &= ~FEC_RCNTRL_DRT;
362 fecp->fec_x_cntrl |= FEC_TCNTRL_FDEN; /* FD enable */
365 static inline void fec_half_duplex(struct eth_device *dev)
367 struct ether_fcc_info_s *efis = dev->priv;
368 volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
370 fecp->fec_r_cntrl |= FEC_RCNTRL_DRT;
371 fecp->fec_x_cntrl &= ~FEC_TCNTRL_FDEN; /* FD disable */
374 static void fec_pin_init(int fecidx)
377 volatile immap_t *immr = (immap_t *) CFG_IMMR;
378 volatile fec_t *fecp;
381 * only two FECs please
383 if ((unsigned int)fecidx >= 2)
387 fecp = &immr->im_cpm.cp_fec1;
389 fecp = &immr->im_cpm.cp_fec2;
392 * Set MII speed to 2.5 MHz or slightly below.
393 * * According to the MPC860T (Rev. D) Fast ethernet controller user
395 * * the MII management interface clock must be less than or equal
397 * * This MDC frequency is equal to system clock / (2 * MII_SPEED).
398 * * Then MII_SPEED = system_clock / 2 * 2,5 Mhz.
400 fecp->fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1;
402 #if defined(CONFIG_NETTA) || defined(CONFIG_NETPHONE) || defined(CONFIG_NETTA2)
403 /* our PHYs are the limit at 2.5 MHz */
404 fecp->fec_mii_speed <<= 1;
407 #if defined(CONFIG_MPC885_FAMILY) && defined(WANT_MII)
408 /* use MDC for MII */
409 immr->im_ioport.iop_pdpar |= 0x0080;
410 immr->im_ioport.iop_pddir &= ~0x0080;
414 #if defined(CONFIG_ETHER_ON_FEC1)
416 #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
418 #if !defined(CONFIG_RMII)
420 immr->im_ioport.iop_papar |= 0xf830;
421 immr->im_ioport.iop_padir |= 0x0830;
422 immr->im_ioport.iop_padir &= ~0xf000;
424 immr->im_cpm.cp_pbpar |= 0x00001001;
425 immr->im_cpm.cp_pbdir &= ~0x00001001;
427 immr->im_ioport.iop_pcpar |= 0x000c;
428 immr->im_ioport.iop_pcdir &= ~0x000c;
430 immr->im_cpm.cp_pepar |= 0x00000003;
431 immr->im_cpm.cp_pedir |= 0x00000003;
432 immr->im_cpm.cp_peso &= ~0x00000003;
434 immr->im_cpm.cp_cptr &= ~0x00000100;
438 #if !defined(CONFIG_FEC1_PHY_NORXERR)
439 immr->im_ioport.iop_papar |= 0x1000;
440 immr->im_ioport.iop_padir &= ~0x1000;
442 immr->im_ioport.iop_papar |= 0xe810;
443 immr->im_ioport.iop_padir |= 0x0810;
444 immr->im_ioport.iop_padir &= ~0xe000;
446 immr->im_cpm.cp_pbpar |= 0x00000001;
447 immr->im_cpm.cp_pbdir &= ~0x00000001;
449 immr->im_cpm.cp_cptr |= 0x00000100;
450 immr->im_cpm.cp_cptr &= ~0x00000050;
452 #endif /* !CONFIG_RMII */
454 #elif !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
456 * Configure all of port D for MII.
458 immr->im_ioport.iop_pdpar = 0x1fff;
461 * Bits moved from Rev. D onward
463 if ((get_immr(0) & 0xffff) < 0x0501)
464 immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
466 immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
469 * Configure port A for MII.
472 #if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY)
475 * On the ICU862 board the MII-MDC pin is routed to PD8 pin
476 * * of CPU, so for this board we need to configure Utopia and
477 * * enable PD8 to MII-MDC function
479 immr->im_ioport.iop_pdpar |= 0x4080;
483 * Has Utopia been configured?
485 if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
487 * YES - Use MUXED mode for UTOPIA bus.
488 * This frees Port A for use by MII (see 862UM table 41-6).
490 immr->im_ioport.utmode &= ~0x80;
493 * NO - set SPLIT mode for UTOPIA bus.
495 * This doesn't really effect UTOPIA (which isn't
496 * enabled anyway) but just tells the 862
497 * to use port A for MII (see 862UM table 41-6).
499 immr->im_ioport.utmode |= 0x80;
501 #endif /* !defined(CONFIG_ICU862) */
503 #endif /* CONFIG_ETHER_ON_FEC1 */
504 } else if (fecidx == 1) {
506 #if defined(CONFIG_ETHER_ON_FEC2)
508 #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
510 #if !defined(CONFIG_RMII)
512 #warning this configuration is not tested; please report if it works
513 immr->im_cpm.cp_pepar |= 0x0003fffc;
514 immr->im_cpm.cp_pedir |= 0x0003fffc;
515 immr->im_cpm.cp_peso &= ~0x000087fc;
516 immr->im_cpm.cp_peso |= 0x00037800;
518 immr->im_cpm.cp_cptr &= ~0x00000080;
521 #if !defined(CONFIG_FEC2_PHY_NORXERR)
522 immr->im_cpm.cp_pepar |= 0x00000010;
523 immr->im_cpm.cp_pedir |= 0x00000010;
524 immr->im_cpm.cp_peso &= ~0x00000010;
526 immr->im_cpm.cp_pepar |= 0x00039620;
527 immr->im_cpm.cp_pedir |= 0x00039620;
528 immr->im_cpm.cp_peso |= 0x00031000;
529 immr->im_cpm.cp_peso &= ~0x00008620;
531 immr->im_cpm.cp_cptr |= 0x00000080;
532 immr->im_cpm.cp_cptr &= ~0x00000028;
533 #endif /* CONFIG_RMII */
535 #endif /* CONFIG_MPC885_FAMILY */
537 #endif /* CONFIG_ETHER_ON_FEC2 */
542 static int fec_init (struct eth_device *dev, bd_t * bd)
544 struct ether_fcc_info_s *efis = dev->priv;
545 volatile immap_t *immr = (immap_t *) CFG_IMMR;
546 volatile fec_t *fecp =
547 (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
550 if (efis->ether_index == 0) {
551 #if defined(CONFIG_FADS) /* FADS family uses FPGA (BCSR) to control PHYs */
552 #if defined(CONFIG_MPC885ADS)
553 *(vu_char *) BCSR5 &= ~(BCSR5_MII1_EN | BCSR5_MII1_RST);
555 /* configure FADS for fast (FEC) ethernet, half-duplex */
556 /* The LXT970 needs about 50ms to recover from reset, so
557 * wait for it by discovering the PHY before leaving eth_init().
560 volatile uint *bcsr4 = (volatile uint *) BCSR4;
562 *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
563 | (BCSR4_FETHCFG0 | BCSR4_FETHFDE |
566 /* reset the LXT970 PHY */
567 *bcsr4 &= ~BCSR4_FETHRST;
569 *bcsr4 |= BCSR4_FETHRST;
572 #endif /* CONFIG_MPC885ADS */
573 #endif /* CONFIG_FADS */
577 * A delay is required between a reset of the FEC block and
578 * initialization of other FEC registers because the reset takes
579 * some time to complete. If you don't delay, subsequent writes
580 * to FEC registers might get killed by the reset routine which is
583 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
585 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
589 if (i == FEC_RESET_DELAY) {
590 printf ("FEC_RESET_DELAY timeout\n");
594 /* We use strictly polling mode only
598 /* Clear any pending interrupt
600 fecp->fec_ievent = 0xffc0;
602 /* No need to set the IVEC register */
604 /* Set station address
606 #define ea eth_get_dev()->enetaddr
607 fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
608 fecp->fec_addr_high = (ea[4] << 8) | (ea[5]);
611 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
613 * Turn on multicast address hash table
615 fecp->fec_hash_table_high = 0xffffffff;
616 fecp->fec_hash_table_low = 0xffffffff;
618 /* Clear multicast address hash table
620 fecp->fec_hash_table_high = 0;
621 fecp->fec_hash_table_low = 0;
624 /* Set maximum receive buffer size.
626 fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
628 /* Set maximum frame length
630 fecp->fec_r_hash = PKT_MAXBUF_SIZE;
633 * Setup Buffers and Buffer Desriptors
639 #ifdef CFG_ALLOC_DPRAM
640 rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
641 dpram_alloc_align (sizeof (RTXBD), 8));
643 rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
647 * Setup Receiver Buffer Descriptors (13.14.24.18)
651 for (i = 0; i < PKTBUFSRX; i++) {
652 rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
653 rtx->rxbd[i].cbd_datlen = 0; /* Reset */
654 rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
656 rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
659 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
663 for (i = 0; i < TX_BUF_CNT; i++) {
664 rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
665 rtx->txbd[i].cbd_datlen = 0; /* Reset */
666 rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
668 rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
670 /* Set receive and transmit descriptor base
672 fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
673 fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
677 #if 0 /* Full duplex mode */
678 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
679 fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
680 #else /* Half duplex mode */
681 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
682 fecp->fec_x_cntrl = 0;
685 /* Enable big endian and don't care about SDMA FC.
687 fecp->fec_fun_code = 0x78000000;
690 * Setup the pin configuration of the FEC
692 fec_pin_init (efis->ether_index);
698 * Now enable the transmit and receive processing
700 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
702 if (efis->phy_addr == -1) {
703 #ifdef CFG_DISCOVER_PHY
705 * wait for the PHY to wake up after reset
707 efis->actual_phy_addr = mii_discover_phy (dev);
709 if (efis->actual_phy_addr == -1) {
710 printf ("Unable to discover phy!\n");
714 efis->actual_phy_addr = -1;
717 efis->actual_phy_addr = efis->phy_addr;
719 #if defined(CONFIG_MII) && defined(CONFIG_RMII)
721 /* the MII interface is connected to FEC1
722 * so for the miiphy_xxx function to work we must
723 * call mii_init since fec_halt messes the thing up
725 if (efis->ether_index != 0)
729 * adapt the RMII speed to the speed of the phy
731 if (miiphy_speed (dev->name, efis->actual_phy_addr) == _100BASET) {
738 #if defined(CONFIG_MII)
740 * adapt to the half/full speed settings
742 if (miiphy_duplex (dev->name, efis->actual_phy_addr) == FULL) {
743 fec_full_duplex (dev);
745 fec_half_duplex (dev);
749 /* And last, try to fill Rx Buffer Descriptors */
750 fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
752 efis->initialized = 1;
758 static void fec_halt(struct eth_device* dev)
760 struct ether_fcc_info_s *efis = dev->priv;
761 volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
764 /* avoid halt if initialized; mii gets stuck otherwise */
765 if (!efis->initialized)
769 * A delay is required between a reset of the FEC block and
770 * initialization of other FEC registers because the reset takes
771 * some time to complete. If you don't delay, subsequent writes
772 * to FEC registers might get killed by the reset routine which is
776 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
778 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
782 if (i == FEC_RESET_DELAY) {
783 printf ("FEC_RESET_DELAY timeout\n");
787 efis->initialized = 0;
790 #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
792 /* Make MII read/write commands for the FEC.
795 #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
798 #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
799 (REG & 0x1f) << 18) | \
802 /* Interrupt events/masks.
804 #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
805 #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
806 #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
807 #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
808 #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
809 #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
810 #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
811 #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
812 #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
813 #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
815 /* PHY identification
817 #define PHY_ID_LXT970 0x78100000 /* LXT970 */
818 #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
819 #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
820 #define PHY_ID_QS6612 0x01814400 /* QS6612 */
821 #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
822 #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
823 #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
824 #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */
825 #define PHY_ID_KSM8995M 0x00221450 /* MICREL KS8995MA */
827 /* send command to phy using mii, wait for result */
829 mii_send(uint mii_cmd)
835 ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec);
837 ep->fec_mii_data = mii_cmd; /* command to phy */
839 /* wait for mii complete */
841 while (!(ep->fec_ievent & FEC_ENET_MII)) {
843 printf("mii_send STUCK!\n");
847 mii_reply = ep->fec_mii_data; /* result from phy */
848 ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
850 printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
851 __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
853 return (mii_reply & 0xffff); /* data read from phy */
855 #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */
857 #if defined(CFG_DISCOVER_PHY)
858 static int mii_discover_phy(struct eth_device *dev)
860 #define MAX_PHY_PASSES 11
866 phyaddr = -1; /* didn't find a PHY yet */
867 for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
869 /* PHY may need more time to recover from reset.
870 * The LXT970 needs 50ms typical, no maximum is
871 * specified, so wait 10ms before try again.
872 * With 11 passes this gives it 100ms to wake up.
874 udelay(10000); /* wait 10ms */
876 for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
877 phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1));
879 printf("PHY type 0x%x pass %d type ", phytype, pass);
881 if (phytype != 0xffff) {
884 phytype |= mii_send(mk_mii_read(phyno,
888 printf("PHY @ 0x%x pass %d type ",phyno,pass);
889 switch (phytype & 0xfffffff0) {
902 case PHY_ID_AMD79C784:
903 printf("AMD79C784\n");
905 case PHY_ID_LSI80225B:
906 printf("LSI L80225/B\n");
909 printf("Davicom DM9161\n");
911 case PHY_ID_KSM8995M:
912 printf("MICREL KS8995M\n");
915 printf("0x%08x\n", phytype);
923 printf("No PHY device found.\n");
927 #endif /* CFG_DISCOVER_PHY */
929 #if (defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
931 /****************************************************************************
932 * mii_init -- Initialize the MII for MII command without ethernet
933 * This function is a subset of eth_init
934 ****************************************************************************
938 volatile immap_t *immr = (immap_t *) CFG_IMMR;
939 volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
942 for (j = 0; j < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); j++) {
945 * A delay is required between a reset of the FEC block and
946 * initialization of other FEC registers because the reset takes
947 * some time to complete. If you don't delay, subsequent writes
948 * to FEC registers might get killed by the reset routine which is
952 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
954 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
958 if (i == FEC_RESET_DELAY) {
959 printf ("FEC_RESET_DELAY timeout\n");
963 /* We use strictly polling mode only
967 /* Clear any pending interrupt
969 fecp->fec_ievent = 0xffc0;
971 /* Setup the pin configuration of the FEC(s)
973 fec_pin_init(ether_fcc_info[i].ether_index);
975 /* Now enable the transmit and receive processing
977 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
981 /*****************************************************************************
982 * Read and write a MII PHY register, routines used by MII Utilities
984 * FIXME: These routines are expected to return 0 on success, but mii_send
985 * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
986 * no PHY connected...
987 * For now always return 0.
988 * FIXME: These routines only work after calling eth_init() at least once!
989 * Otherwise they hang in mii_send() !!! Sorry!
990 *****************************************************************************/
992 int fec8xx_miiphy_read(char *devname, unsigned char addr,
993 unsigned char reg, unsigned short *value)
995 short rdreg; /* register working value */
998 printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
1000 rdreg = mii_send(mk_mii_read(addr, reg));
1004 printf ("0x%04x\n", *value);
1009 int fec8xx_miiphy_write(char *devname, unsigned char addr,
1010 unsigned char reg, unsigned short value)
1012 short rdreg; /* register working value */
1014 printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
1016 rdreg = mii_send(mk_mii_write(addr, reg, value));
1019 printf ("0x%04x\n", value);
1023 #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)*/
1025 #endif /* CFG_CMD_NET, FEC_ENET */