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 defined(CONFIG_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) || defined(CONFIG_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);
146 static void __mii_init(void);
148 int fec_initialize(bd_t *bis)
150 struct eth_device* dev;
151 struct ether_fcc_info_s *efis;
154 for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++) {
156 dev = malloc(sizeof(*dev));
160 memset(dev, 0, sizeof(*dev));
162 /* for FEC1 make sure that the name of the interface is the same
163 as the old one for compatibility reasons */
165 sprintf (dev->name, "FEC ETHERNET");
167 sprintf (dev->name, "FEC%d ETHERNET",
168 ether_fcc_info[i].ether_index + 1);
171 efis = ðer_fcc_info[i];
174 * reset actual phy addr
176 efis->actual_phy_addr = -1;
179 dev->init = fec_init;
180 dev->halt = fec_halt;
181 dev->send = fec_send;
182 dev->recv = fec_recv;
186 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
187 miiphy_register(dev->name,
188 fec8xx_miiphy_read, fec8xx_miiphy_write);
194 static int fec_send(struct eth_device* dev, volatile void *packet, int length)
197 struct ether_fcc_info_s *efis = dev->priv;
198 volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
204 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
209 printf("TX not ready\n");
212 rtx->txbd[txIdx].cbd_bufaddr = (uint)packet;
213 rtx->txbd[txIdx].cbd_datlen = length;
214 rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
217 /* Activate transmit Buffer Descriptor polling */
218 fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
221 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
222 #if defined(CONFIG_ICU862)
230 printf("TX timeout\n");
233 printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
234 __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc,
235 (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2);
237 /* return only status bits */;
238 rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
240 txIdx = (txIdx + 1) % TX_BUF_CNT;
245 static int fec_recv (struct eth_device *dev)
247 struct ether_fcc_info_s *efis = dev->priv;
248 volatile fec_t *fecp =
249 (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
253 /* section 16.9.23.2 */
254 if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
256 break; /* nothing received - leave for() loop */
259 length = rtx->rxbd[rxIdx].cbd_datlen;
261 if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
263 printf ("%s[%d] err: %x\n",
264 __FUNCTION__, __LINE__,
265 rtx->rxbd[rxIdx].cbd_sc);
268 volatile uchar *rx = NetRxPackets[rxIdx];
272 #if defined(CONFIG_CMD_CDP)
274 && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0
275 && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0)
279 * Pass the packet up to the protocol layers.
282 NetReceive (rx, length);
285 /* Give the buffer back to the FEC. */
286 rtx->rxbd[rxIdx].cbd_datlen = 0;
288 /* wrap around buffer index when necessary */
289 if ((rxIdx + 1) >= PKTBUFSRX) {
290 rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
291 (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
294 rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
300 /* Try to fill Buffer Descriptors */
301 fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
307 /**************************************************************
309 * FEC Ethernet Initialization Routine
311 *************************************************************/
313 #define FEC_ECNTRL_PINMUX 0x00000004
314 #define FEC_ECNTRL_ETHER_EN 0x00000002
315 #define FEC_ECNTRL_RESET 0x00000001
317 #define FEC_RCNTRL_BC_REJ 0x00000010
318 #define FEC_RCNTRL_PROM 0x00000008
319 #define FEC_RCNTRL_MII_MODE 0x00000004
320 #define FEC_RCNTRL_DRT 0x00000002
321 #define FEC_RCNTRL_LOOP 0x00000001
323 #define FEC_TCNTRL_FDEN 0x00000004
324 #define FEC_TCNTRL_HBC 0x00000002
325 #define FEC_TCNTRL_GTS 0x00000001
327 #define FEC_RESET_DELAY 50
329 #if defined(CONFIG_RMII)
331 static inline void fec_10Mbps(struct eth_device *dev)
333 struct ether_fcc_info_s *efis = dev->priv;
334 int fecidx = efis->ether_index;
335 uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
337 if ((unsigned int)fecidx >= 2)
340 ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr |= mask;
343 static inline void fec_100Mbps(struct eth_device *dev)
345 struct ether_fcc_info_s *efis = dev->priv;
346 int fecidx = efis->ether_index;
347 uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
349 if ((unsigned int)fecidx >= 2)
352 ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr &= ~mask;
357 static inline void fec_full_duplex(struct eth_device *dev)
359 struct ether_fcc_info_s *efis = dev->priv;
360 volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
362 fecp->fec_r_cntrl &= ~FEC_RCNTRL_DRT;
363 fecp->fec_x_cntrl |= FEC_TCNTRL_FDEN; /* FD enable */
366 static inline void fec_half_duplex(struct eth_device *dev)
368 struct ether_fcc_info_s *efis = dev->priv;
369 volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
371 fecp->fec_r_cntrl |= FEC_RCNTRL_DRT;
372 fecp->fec_x_cntrl &= ~FEC_TCNTRL_FDEN; /* FD disable */
375 static void fec_pin_init(int fecidx)
378 volatile immap_t *immr = (immap_t *) CFG_IMMR;
379 volatile fec_t *fecp;
382 * only two FECs please
384 if ((unsigned int)fecidx >= 2)
388 fecp = &immr->im_cpm.cp_fec1;
390 fecp = &immr->im_cpm.cp_fec2;
393 * Set MII speed to 2.5 MHz or slightly below.
394 * * According to the MPC860T (Rev. D) Fast ethernet controller user
396 * * the MII management interface clock must be less than or equal
398 * * This MDC frequency is equal to system clock / (2 * MII_SPEED).
399 * * Then MII_SPEED = system_clock / 2 * 2,5 Mhz.
401 * All MII configuration is done via FEC1 registers:
403 immr->im_cpm.cp_fec1.fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1;
405 #if defined(CONFIG_NETTA) || defined(CONFIG_NETPHONE) || defined(CONFIG_NETTA2)
406 /* our PHYs are the limit at 2.5 MHz */
407 fecp->fec_mii_speed <<= 1;
410 #if defined(CONFIG_MPC885_FAMILY) && defined(WANT_MII)
411 /* use MDC for MII */
412 immr->im_ioport.iop_pdpar |= 0x0080;
413 immr->im_ioport.iop_pddir &= ~0x0080;
417 #if defined(CONFIG_ETHER_ON_FEC1)
419 #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
421 #if !defined(CONFIG_RMII)
423 immr->im_ioport.iop_papar |= 0xf830;
424 immr->im_ioport.iop_padir |= 0x0830;
425 immr->im_ioport.iop_padir &= ~0xf000;
427 immr->im_cpm.cp_pbpar |= 0x00001001;
428 immr->im_cpm.cp_pbdir &= ~0x00001001;
430 immr->im_ioport.iop_pcpar |= 0x000c;
431 immr->im_ioport.iop_pcdir &= ~0x000c;
433 immr->im_cpm.cp_pepar |= 0x00000003;
434 immr->im_cpm.cp_pedir |= 0x00000003;
435 immr->im_cpm.cp_peso &= ~0x00000003;
437 immr->im_cpm.cp_cptr &= ~0x00000100;
441 #if !defined(CONFIG_FEC1_PHY_NORXERR)
442 immr->im_ioport.iop_papar |= 0x1000;
443 immr->im_ioport.iop_padir &= ~0x1000;
445 immr->im_ioport.iop_papar |= 0xe810;
446 immr->im_ioport.iop_padir |= 0x0810;
447 immr->im_ioport.iop_padir &= ~0xe000;
449 immr->im_cpm.cp_pbpar |= 0x00000001;
450 immr->im_cpm.cp_pbdir &= ~0x00000001;
452 immr->im_cpm.cp_cptr |= 0x00000100;
453 immr->im_cpm.cp_cptr &= ~0x00000050;
455 #endif /* !CONFIG_RMII */
457 #elif !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
459 * Configure all of port D for MII.
461 immr->im_ioport.iop_pdpar = 0x1fff;
464 * Bits moved from Rev. D onward
466 if ((get_immr(0) & 0xffff) < 0x0501)
467 immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
469 immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
472 * Configure port A for MII.
475 #if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY)
478 * On the ICU862 board the MII-MDC pin is routed to PD8 pin
479 * * of CPU, so for this board we need to configure Utopia and
480 * * enable PD8 to MII-MDC function
482 immr->im_ioport.iop_pdpar |= 0x4080;
486 * Has Utopia been configured?
488 if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
490 * YES - Use MUXED mode for UTOPIA bus.
491 * This frees Port A for use by MII (see 862UM table 41-6).
493 immr->im_ioport.utmode &= ~0x80;
496 * NO - set SPLIT mode for UTOPIA bus.
498 * This doesn't really effect UTOPIA (which isn't
499 * enabled anyway) but just tells the 862
500 * to use port A for MII (see 862UM table 41-6).
502 immr->im_ioport.utmode |= 0x80;
504 #endif /* !defined(CONFIG_ICU862) */
506 #endif /* CONFIG_ETHER_ON_FEC1 */
507 } else if (fecidx == 1) {
509 #if defined(CONFIG_ETHER_ON_FEC2)
511 #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
513 #if !defined(CONFIG_RMII)
514 immr->im_cpm.cp_pepar |= 0x0003fffc;
515 immr->im_cpm.cp_pedir |= 0x0003fffc;
516 immr->im_cpm.cp_peso &= ~0x000087fc;
517 immr->im_cpm.cp_peso |= 0x00037800;
519 immr->im_cpm.cp_cptr &= ~0x00000080;
522 #if !defined(CONFIG_FEC2_PHY_NORXERR)
523 immr->im_cpm.cp_pepar |= 0x00000010;
524 immr->im_cpm.cp_pedir |= 0x00000010;
525 immr->im_cpm.cp_peso &= ~0x00000010;
527 immr->im_cpm.cp_pepar |= 0x00039620;
528 immr->im_cpm.cp_pedir |= 0x00039620;
529 immr->im_cpm.cp_peso |= 0x00031000;
530 immr->im_cpm.cp_peso &= ~0x00008620;
532 immr->im_cpm.cp_cptr |= 0x00000080;
533 immr->im_cpm.cp_cptr &= ~0x00000028;
534 #endif /* CONFIG_RMII */
536 #endif /* CONFIG_MPC885_FAMILY */
538 #endif /* CONFIG_ETHER_ON_FEC2 */
543 static int fec_reset(volatile fec_t *fecp)
548 * A delay is required between a reset of the FEC block and
549 * initialization of other FEC registers because the reset takes
550 * some time to complete. If you don't delay, subsequent writes
551 * to FEC registers might get killed by the reset routine which is
555 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
557 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
561 if (i == FEC_RESET_DELAY)
567 static int fec_init (struct eth_device *dev, bd_t * bd)
569 struct ether_fcc_info_s *efis = dev->priv;
570 volatile immap_t *immr = (immap_t *) CFG_IMMR;
571 volatile fec_t *fecp =
572 (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
575 if (efis->ether_index == 0) {
576 #if defined(CONFIG_FADS) /* FADS family uses FPGA (BCSR) to control PHYs */
577 #if defined(CONFIG_MPC885ADS)
578 *(vu_char *) BCSR5 &= ~(BCSR5_MII1_EN | BCSR5_MII1_RST);
580 /* configure FADS for fast (FEC) ethernet, half-duplex */
581 /* The LXT970 needs about 50ms to recover from reset, so
582 * wait for it by discovering the PHY before leaving eth_init().
585 volatile uint *bcsr4 = (volatile uint *) BCSR4;
587 *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
588 | (BCSR4_FETHCFG0 | BCSR4_FETHFDE |
591 /* reset the LXT970 PHY */
592 *bcsr4 &= ~BCSR4_FETHRST;
594 *bcsr4 |= BCSR4_FETHRST;
597 #endif /* CONFIG_MPC885ADS */
598 #endif /* CONFIG_FADS */
601 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
602 /* the MII interface is connected to FEC1
603 * so for the miiphy_xxx function to work we must
604 * call mii_init since fec_halt messes the thing up
606 if (efis->ether_index != 0)
610 if (fec_reset(fecp) < 0)
611 printf ("FEC_RESET_DELAY timeout\n");
613 /* We use strictly polling mode only
617 /* Clear any pending interrupt
619 fecp->fec_ievent = 0xffc0;
621 /* No need to set the IVEC register */
623 /* Set station address
625 #define ea dev->enetaddr
626 fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
627 fecp->fec_addr_high = (ea[4] << 8) | (ea[5]);
630 #if defined(CONFIG_CMD_CDP)
632 * Turn on multicast address hash table
634 fecp->fec_hash_table_high = 0xffffffff;
635 fecp->fec_hash_table_low = 0xffffffff;
637 /* Clear multicast address hash table
639 fecp->fec_hash_table_high = 0;
640 fecp->fec_hash_table_low = 0;
643 /* Set maximum receive buffer size.
645 fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
647 /* Set maximum frame length
649 fecp->fec_r_hash = PKT_MAXBUF_SIZE;
652 * Setup Buffers and Buffer Desriptors
658 #ifdef CFG_ALLOC_DPRAM
659 rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
660 dpram_alloc_align (sizeof (RTXBD), 8));
662 rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
666 * Setup Receiver Buffer Descriptors (13.14.24.18)
670 for (i = 0; i < PKTBUFSRX; i++) {
671 rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
672 rtx->rxbd[i].cbd_datlen = 0; /* Reset */
673 rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
675 rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
678 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
682 for (i = 0; i < TX_BUF_CNT; i++) {
683 rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
684 rtx->txbd[i].cbd_datlen = 0; /* Reset */
685 rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
687 rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
689 /* Set receive and transmit descriptor base
691 fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
692 fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
696 #if 0 /* Full duplex mode */
697 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
698 fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
699 #else /* Half duplex mode */
700 fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
701 fecp->fec_x_cntrl = 0;
704 /* Enable big endian and don't care about SDMA FC.
706 fecp->fec_fun_code = 0x78000000;
709 * Setup the pin configuration of the FEC
711 fec_pin_init (efis->ether_index);
717 * Now enable the transmit and receive processing
719 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
721 if (efis->phy_addr == -1) {
722 #ifdef CFG_DISCOVER_PHY
724 * wait for the PHY to wake up after reset
726 efis->actual_phy_addr = mii_discover_phy (dev);
728 if (efis->actual_phy_addr == -1) {
729 printf ("Unable to discover phy!\n");
733 efis->actual_phy_addr = -1;
736 efis->actual_phy_addr = efis->phy_addr;
739 #if defined(CONFIG_MII) && defined(CONFIG_RMII)
741 * adapt the RMII speed to the speed of the phy
743 if (miiphy_speed (dev->name, efis->actual_phy_addr) == _100BASET) {
750 #if defined(CONFIG_MII)
752 * adapt to the half/full speed settings
754 if (miiphy_duplex (dev->name, efis->actual_phy_addr) == FULL) {
755 fec_full_duplex (dev);
757 fec_half_duplex (dev);
761 /* And last, try to fill Rx Buffer Descriptors */
762 fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
764 efis->initialized = 1;
770 static void fec_halt(struct eth_device* dev)
772 struct ether_fcc_info_s *efis = dev->priv;
773 volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
776 /* avoid halt if initialized; mii gets stuck otherwise */
777 if (!efis->initialized)
781 * A delay is required between a reset of the FEC block and
782 * initialization of other FEC registers because the reset takes
783 * some time to complete. If you don't delay, subsequent writes
784 * to FEC registers might get killed by the reset routine which is
788 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
790 (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
794 if (i == FEC_RESET_DELAY) {
795 printf ("FEC_RESET_DELAY timeout\n");
799 efis->initialized = 0;
802 #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
804 /* Make MII read/write commands for the FEC.
807 #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
810 #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
811 (REG & 0x1f) << 18) | \
814 /* Interrupt events/masks.
816 #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
817 #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
818 #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
819 #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
820 #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
821 #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
822 #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
823 #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
824 #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
825 #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
827 /* PHY identification
829 #define PHY_ID_LXT970 0x78100000 /* LXT970 */
830 #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
831 #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
832 #define PHY_ID_QS6612 0x01814400 /* QS6612 */
833 #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
834 #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
835 #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
836 #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */
837 #define PHY_ID_KSM8995M 0x00221450 /* MICREL KS8995MA */
839 /* send command to phy using mii, wait for result */
841 mii_send(uint mii_cmd)
847 ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec);
849 ep->fec_mii_data = mii_cmd; /* command to phy */
851 /* wait for mii complete */
853 while (!(ep->fec_ievent & FEC_ENET_MII)) {
855 printf("mii_send STUCK!\n");
859 mii_reply = ep->fec_mii_data; /* result from phy */
860 ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
862 printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
863 __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
865 return (mii_reply & 0xffff); /* data read from phy */
869 #if defined(CFG_DISCOVER_PHY)
870 static int mii_discover_phy(struct eth_device *dev)
872 #define MAX_PHY_PASSES 11
878 phyaddr = -1; /* didn't find a PHY yet */
879 for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
881 /* PHY may need more time to recover from reset.
882 * The LXT970 needs 50ms typical, no maximum is
883 * specified, so wait 10ms before try again.
884 * With 11 passes this gives it 100ms to wake up.
886 udelay(10000); /* wait 10ms */
888 for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
889 phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR2));
891 printf("PHY type 0x%x pass %d type ", phytype, pass);
893 if (phytype != 0xffff) {
895 phytype |= mii_send(mk_mii_read(phyno,
899 printf("PHY @ 0x%x pass %d type ",phyno,pass);
900 switch (phytype & 0xfffffff0) {
913 case PHY_ID_AMD79C784:
914 printf("AMD79C784\n");
916 case PHY_ID_LSI80225B:
917 printf("LSI L80225/B\n");
920 printf("Davicom DM9161\n");
922 case PHY_ID_KSM8995M:
923 printf("MICREL KS8995M\n");
926 printf("0x%08x\n", phytype);
934 printf("No PHY device found.\n");
938 #endif /* CFG_DISCOVER_PHY */
940 #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
942 /****************************************************************************
943 * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet
944 * This function is a subset of eth_init
945 ****************************************************************************
947 static void __mii_init(void)
949 volatile immap_t *immr = (immap_t *) CFG_IMMR;
950 volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
952 if (fec_reset(fecp) < 0)
953 printf ("FEC_RESET_DELAY timeout\n");
955 /* We use strictly polling mode only
959 /* Clear any pending interrupt
961 fecp->fec_ievent = 0xffc0;
963 /* Now enable the transmit and receive processing
965 fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
974 /* Setup the pin configuration of the FEC(s)
976 for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
977 fec_pin_init(ether_fcc_info[i].ether_index);
980 /*****************************************************************************
981 * Read and write a MII PHY register, routines used by MII Utilities
983 * FIXME: These routines are expected to return 0 on success, but mii_send
984 * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
985 * no PHY connected...
986 * For now always return 0.
987 * FIXME: These routines only work after calling eth_init() at least once!
988 * Otherwise they hang in mii_send() !!! Sorry!
989 *****************************************************************************/
991 int fec8xx_miiphy_read(char *devname, unsigned char addr,
992 unsigned char reg, unsigned short *value)
994 short rdreg; /* register working value */
997 printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
999 rdreg = mii_send(mk_mii_read(addr, reg));
1003 printf ("0x%04x\n", *value);
1008 int fec8xx_miiphy_write(char *devname, unsigned char addr,
1009 unsigned char reg, unsigned short value)
1011 short rdreg; /* register working value */
1013 printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
1015 rdreg = mii_send(mk_mii_write(addr, reg, value));
1018 printf ("0x%04x\n", value);