xref: /rk3399_rockchip-uboot/drivers/net/mcffec.c (revision f605479de2deb11e834f31dfdb0af107c86aced6)
18e585f02STsiChung Liew /*
28e585f02STsiChung Liew  * (C) Copyright 2000-2004
38e585f02STsiChung Liew  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
48e585f02STsiChung Liew  *
5f2208fbcSTsiChungLiew  * (C) Copyright 2007 Freescale Semiconductor, Inc.
68e585f02STsiChung Liew  * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
78e585f02STsiChung Liew  *
88e585f02STsiChung Liew  * See file CREDITS for list of people who contributed to this
98e585f02STsiChung Liew  * project.
108e585f02STsiChung Liew  *
118e585f02STsiChung Liew  * This program is free software; you can redistribute it and/or
128e585f02STsiChung Liew  * modify it under the terms of the GNU General Public License as
138e585f02STsiChung Liew  * published by the Free Software Foundation; either version 2 of
148e585f02STsiChung Liew  * the License, or (at your option) any later version.
158e585f02STsiChung Liew  *
168e585f02STsiChung Liew  * This program is distributed in the hope that it will be useful,
178e585f02STsiChung Liew  * but WITHOUT ANY WARRANTY; without even the implied warranty of
188e585f02STsiChung Liew  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
198e585f02STsiChung Liew  * GNU General Public License for more details.
208e585f02STsiChung Liew  *
218e585f02STsiChung Liew  * You should have received a copy of the GNU General Public License
228e585f02STsiChung Liew  * along with this program; if not, write to the Free Software
238e585f02STsiChung Liew  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
248e585f02STsiChung Liew  * MA 02111-1307 USA
258e585f02STsiChung Liew  */
268e585f02STsiChung Liew 
278e585f02STsiChung Liew #include <common.h>
288e585f02STsiChung Liew #include <malloc.h>
298e585f02STsiChung Liew 
303e66c078SWolfgang Denk #ifdef CONFIG_MCFFEC
313e66c078SWolfgang Denk 
328e585f02STsiChung Liew #include <asm/fec.h>
33f2208fbcSTsiChungLiew #include <asm/immap.h>
348e585f02STsiChung Liew 
358e585f02STsiChung Liew #include <command.h>
368e585f02STsiChung Liew #include <net.h>
378e585f02STsiChung Liew #include <miiphy.h>
388e585f02STsiChung Liew 
398e585f02STsiChung Liew #undef	ET_DEBUG
408e585f02STsiChung Liew #undef	MII_DEBUG
418e585f02STsiChung Liew 
428e585f02STsiChung Liew /* Ethernet Transmit and Receive Buffers */
438e585f02STsiChung Liew #define DBUF_LENGTH		1520
448e585f02STsiChung Liew #define TX_BUF_CNT		2
458e585f02STsiChung Liew #define PKT_MAXBUF_SIZE		1518
468e585f02STsiChung Liew #define PKT_MINBUF_SIZE		64
478e585f02STsiChung Liew #define PKT_MAXBLR_SIZE		1520
488e585f02STsiChung Liew #define LAST_PKTBUFSRX		PKTBUFSRX - 1
498e585f02STsiChung Liew #define BD_ENET_RX_W_E		(BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
508e585f02STsiChung Liew #define BD_ENET_TX_RDY_LST	(BD_ENET_TX_READY | BD_ENET_TX_LAST)
518e585f02STsiChung Liew 
528e585f02STsiChung Liew DECLARE_GLOBAL_DATA_PTR;
538e585f02STsiChung Liew 
54ab77bc54STsiChungLiew #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
558e585f02STsiChung Liew 
568e585f02STsiChung Liew struct fec_info_s fec_info[] = {
578e585f02STsiChung Liew #ifdef CFG_FEC0_IOBASE
588e585f02STsiChung Liew 	{
598e585f02STsiChung Liew 	 0,			/* index */
608e585f02STsiChung Liew 	 CFG_FEC0_IOBASE,	/* io base */
618e585f02STsiChung Liew 	 CFG_FEC0_PINMUX,	/* gpio pin muxing */
628e585f02STsiChung Liew 	 CFG_FEC0_MIIBASE,	/* mii base */
638e585f02STsiChung Liew 	 -1,			/* phy_addr */
648e585f02STsiChung Liew 	 0,			/* duplex and speed */
658e585f02STsiChung Liew 	 0,			/* phy name */
668e585f02STsiChung Liew 	 0,			/* phyname init */
678e585f02STsiChung Liew 	 0,			/* RX BD */
688e585f02STsiChung Liew 	 0,			/* TX BD */
698e585f02STsiChung Liew 	 0,			/* rx Index */
708e585f02STsiChung Liew 	 0,			/* tx Index */
718e585f02STsiChung Liew 	 0,			/* tx buffer */
728e585f02STsiChung Liew 	 0,			/* initialized flag */
738e585f02STsiChung Liew 	 },
748e585f02STsiChung Liew #endif
758e585f02STsiChung Liew #ifdef CFG_FEC1_IOBASE
768e585f02STsiChung Liew 	{
778e585f02STsiChung Liew 	 1,			/* index */
788e585f02STsiChung Liew 	 CFG_FEC1_IOBASE,	/* io base */
798e585f02STsiChung Liew 	 CFG_FEC1_PINMUX,	/* gpio pin muxing */
808e585f02STsiChung Liew 	 CFG_FEC1_MIIBASE,	/* mii base */
818e585f02STsiChung Liew 	 -1,			/* phy_addr */
828e585f02STsiChung Liew 	 0,			/* duplex and speed */
838e585f02STsiChung Liew 	 0,			/* phy name */
848e585f02STsiChung Liew 	 0,			/* phy name init */
858e585f02STsiChung Liew 	 0,			/* RX BD */
868e585f02STsiChung Liew 	 0,			/* TX BD */
878e585f02STsiChung Liew 	 0,			/* rx Index */
888e585f02STsiChung Liew 	 0,			/* tx Index */
898e585f02STsiChung Liew 	 0,			/* tx buffer */
908e585f02STsiChung Liew 	 0,			/* initialized flag */
918e585f02STsiChung Liew 	 }
928e585f02STsiChung Liew #endif
938e585f02STsiChung Liew };
948e585f02STsiChung Liew 
958e585f02STsiChung Liew int fec_send(struct eth_device *dev, volatile void *packet, int length);
968e585f02STsiChung Liew int fec_recv(struct eth_device *dev);
978e585f02STsiChung Liew int fec_init(struct eth_device *dev, bd_t * bd);
988e585f02STsiChung Liew void fec_halt(struct eth_device *dev);
998e585f02STsiChung Liew void fec_reset(struct eth_device *dev);
1008e585f02STsiChung Liew 
1018e585f02STsiChung Liew extern int fecpin_setclear(struct eth_device *dev, int setclear);
1028e585f02STsiChung Liew 
1038e585f02STsiChung Liew #ifdef CFG_DISCOVER_PHY
1049e737d84STsiChungLiew extern void __mii_init(void);
1058e585f02STsiChung Liew extern uint mii_send(uint mii_cmd);
1068e585f02STsiChung Liew extern int mii_discover_phy(struct eth_device *dev);
1078e585f02STsiChung Liew extern int mcffec_miiphy_read(char *devname, unsigned char addr,
1088e585f02STsiChung Liew 			      unsigned char reg, unsigned short *value);
1098e585f02STsiChung Liew extern int mcffec_miiphy_write(char *devname, unsigned char addr,
1108e585f02STsiChung Liew 			       unsigned char reg, unsigned short value);
1118e585f02STsiChung Liew #endif
1128e585f02STsiChung Liew 
1138e585f02STsiChung Liew void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
1148e585f02STsiChung Liew {
1158e585f02STsiChung Liew 	if ((dup_spd >> 16) == FULL) {
1168e585f02STsiChung Liew 		/* Set maximum frame length */
1178e585f02STsiChung Liew 		fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
1188e585f02STsiChung Liew 		    FEC_RCR_PROM | 0x100;
1198e585f02STsiChung Liew 		fecp->tcr = FEC_TCR_FDEN;
1208e585f02STsiChung Liew 	} else {
1218e585f02STsiChung Liew 		/* Half duplex mode */
1228e585f02STsiChung Liew 		fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
1238e585f02STsiChung Liew 		    FEC_RCR_MII_MODE | FEC_RCR_DRT;
1248e585f02STsiChung Liew 		fecp->tcr &= ~FEC_TCR_FDEN;
1258e585f02STsiChung Liew 	}
1268e585f02STsiChung Liew 
1278e585f02STsiChung Liew 	if ((dup_spd & 0xFFFF) == _100BASET) {
1288e585f02STsiChung Liew #ifdef MII_DEBUG
1298e585f02STsiChung Liew 		printf("100Mbps\n");
1308e585f02STsiChung Liew #endif
1318e585f02STsiChung Liew 		bd->bi_ethspeed = 100;
1328e585f02STsiChung Liew 	} else {
1338e585f02STsiChung Liew #ifdef MII_DEBUG
1348e585f02STsiChung Liew 		printf("10Mbps\n");
1358e585f02STsiChung Liew #endif
1368e585f02STsiChung Liew 		bd->bi_ethspeed = 10;
1378e585f02STsiChung Liew 	}
1388e585f02STsiChung Liew }
1398e585f02STsiChung Liew 
1408e585f02STsiChung Liew int fec_send(struct eth_device *dev, volatile void *packet, int length)
1418e585f02STsiChung Liew {
1428e585f02STsiChung Liew 	struct fec_info_s *info = dev->priv;
1438e585f02STsiChung Liew 	volatile fec_t *fecp = (fec_t *) (info->iobase);
1448e585f02STsiChung Liew 	int j, rc;
1458e585f02STsiChung Liew 	u16 phyStatus;
1468e585f02STsiChung Liew 
1478e585f02STsiChung Liew 	miiphy_read(dev->name, info->phy_addr, PHY_BMSR, &phyStatus);
1488e585f02STsiChung Liew 
1498e585f02STsiChung Liew 	/* section 16.9.23.3
1508e585f02STsiChung Liew 	 * Wait for ready
1518e585f02STsiChung Liew 	 */
1528e585f02STsiChung Liew 	j = 0;
1538e585f02STsiChung Liew 	while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
1548e585f02STsiChung Liew 	       (j < MCFFEC_TOUT_LOOP)) {
1558e585f02STsiChung Liew 		udelay(1);
1568e585f02STsiChung Liew 		j++;
1578e585f02STsiChung Liew 	}
1588e585f02STsiChung Liew 	if (j >= MCFFEC_TOUT_LOOP) {
1598e585f02STsiChung Liew 		printf("TX not ready\n");
1608e585f02STsiChung Liew 	}
1610dca874dSTsiChung 
1628e585f02STsiChung Liew 	info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
1638e585f02STsiChung Liew 	info->txbd[info->txIdx].cbd_datlen = length;
1648e585f02STsiChung Liew 	info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
1658e585f02STsiChung Liew 
1668e585f02STsiChung Liew 	/* Activate transmit Buffer Descriptor polling */
1678e585f02STsiChung Liew 	fecp->tdar = 0x01000000;	/* Descriptor polling active    */
1688e585f02STsiChung Liew 
169*f605479dSTsi-Chung Liew 	/* FEC fix for MCF5275, FEC unable to initial transmit data packet.
170*f605479dSTsi-Chung Liew 	 * A nop will ensure the descriptor polling active completed.
171*f605479dSTsi-Chung Liew 	 */
172*f605479dSTsi-Chung Liew #ifdef CONFIG_M5275
173*f605479dSTsi-Chung Liew 	__asm__ ("nop");
174*f605479dSTsi-Chung Liew #endif
175*f605479dSTsi-Chung Liew 
1760dca874dSTsiChung #ifdef CFG_UNIFY_CACHE
177f2208fbcSTsiChungLiew 	icache_invalid();
178f2208fbcSTsiChungLiew #endif
1790dca874dSTsiChung 	j = 0;
1808e585f02STsiChung Liew 	while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
1818e585f02STsiChung Liew 	       (j < MCFFEC_TOUT_LOOP)) {
1828e585f02STsiChung Liew 		udelay(1);
1838e585f02STsiChung Liew 		j++;
1848e585f02STsiChung Liew 	}
1858e585f02STsiChung Liew 	if (j >= MCFFEC_TOUT_LOOP) {
1868e585f02STsiChung Liew 		printf("TX timeout\n");
1878e585f02STsiChung Liew 	}
1880dca874dSTsiChung 
1898e585f02STsiChung Liew #ifdef ET_DEBUG
1908e585f02STsiChung Liew 	printf("%s[%d] %s: cycles: %d    status: %x  retry cnt: %d\n",
1918e585f02STsiChung Liew 	       __FILE__, __LINE__, __FUNCTION__, j,
1928e585f02STsiChung Liew 	       info->txbd[info->txIdx].cbd_sc,
1938e585f02STsiChung Liew 	       (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
1948e585f02STsiChung Liew #endif
1958e585f02STsiChung Liew 
1960dca874dSTsiChung 	/* return only status bits */
1978e585f02STsiChung Liew 	rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
1988e585f02STsiChung Liew 	info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
1998e585f02STsiChung Liew 
2008e585f02STsiChung Liew 	return rc;
2018e585f02STsiChung Liew }
2028e585f02STsiChung Liew 
2038e585f02STsiChung Liew int fec_recv(struct eth_device *dev)
2048e585f02STsiChung Liew {
2058e585f02STsiChung Liew 	struct fec_info_s *info = dev->priv;
2068e585f02STsiChung Liew 	volatile fec_t *fecp = (fec_t *) (info->iobase);
2078e585f02STsiChung Liew 	int length;
2088e585f02STsiChung Liew 
2098e585f02STsiChung Liew 	for (;;) {
2100dca874dSTsiChung #ifdef CFG_UNIFY_CACHE
2110dca874dSTsiChung        		icache_invalid();
2120dca874dSTsiChung #endif
2138e585f02STsiChung Liew 		/* section 16.9.23.2 */
2148e585f02STsiChung Liew 		if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
2158e585f02STsiChung Liew 			length = -1;
2168e585f02STsiChung Liew 			break;	/* nothing received - leave for() loop */
2178e585f02STsiChung Liew 		}
2188e585f02STsiChung Liew 
2198e585f02STsiChung Liew 		length = info->rxbd[info->rxIdx].cbd_datlen;
2208e585f02STsiChung Liew 
2218e585f02STsiChung Liew 		if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
2228e585f02STsiChung Liew 			printf("%s[%d] err: %x\n",
2238e585f02STsiChung Liew 			       __FUNCTION__, __LINE__,
2248e585f02STsiChung Liew 			       info->rxbd[info->rxIdx].cbd_sc);
2258e585f02STsiChung Liew #ifdef ET_DEBUG
2268e585f02STsiChung Liew 			printf("%s[%d] err: %x\n",
2278e585f02STsiChung Liew 			       __FUNCTION__, __LINE__,
2288e585f02STsiChung Liew 			       info->rxbd[info->rxIdx].cbd_sc);
2298e585f02STsiChung Liew #endif
2308e585f02STsiChung Liew 		} else {
2318e585f02STsiChung Liew 
2328e585f02STsiChung Liew 			length -= 4;
2338e585f02STsiChung Liew 			/* Pass the packet up to the protocol layers. */
2348e585f02STsiChung Liew 			NetReceive(NetRxPackets[info->rxIdx], length);
2358e585f02STsiChung Liew 
2368e585f02STsiChung Liew 			fecp->eir |= FEC_EIR_RXF;
2378e585f02STsiChung Liew 		}
2388e585f02STsiChung Liew 
2398e585f02STsiChung Liew 		/* Give the buffer back to the FEC. */
2408e585f02STsiChung Liew 		info->rxbd[info->rxIdx].cbd_datlen = 0;
2418e585f02STsiChung Liew 
2428e585f02STsiChung Liew 		/* wrap around buffer index when necessary */
2438e585f02STsiChung Liew 		if (info->rxIdx == LAST_PKTBUFSRX) {
2448e585f02STsiChung Liew 			info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
2458e585f02STsiChung Liew 			info->rxIdx = 0;
2468e585f02STsiChung Liew 		} else {
2478e585f02STsiChung Liew 			info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
2488e585f02STsiChung Liew 			info->rxIdx++;
2498e585f02STsiChung Liew 		}
2508e585f02STsiChung Liew 
2518e585f02STsiChung Liew 		/* Try to fill Buffer Descriptors */
2528e585f02STsiChung Liew 		fecp->rdar = 0x01000000;	/* Descriptor polling active    */
2538e585f02STsiChung Liew 	}
2548e585f02STsiChung Liew 
2558e585f02STsiChung Liew 	return length;
2568e585f02STsiChung Liew }
2578e585f02STsiChung Liew 
2588e585f02STsiChung Liew #ifdef ET_DEBUG
2598e585f02STsiChung Liew void dbgFecRegs(struct eth_device *dev)
2608e585f02STsiChung Liew {
2618e585f02STsiChung Liew 	struct fec_info_s *info = dev->priv;
2628e585f02STsiChung Liew 	volatile fec_t *fecp = (fec_t *) (info->iobase);
2638e585f02STsiChung Liew 
2648e585f02STsiChung Liew 	printf("=====\n");
2658e585f02STsiChung Liew 	printf("ievent       %x - %x\n", (int)&fecp->eir, fecp->eir);
2668e585f02STsiChung Liew 	printf("imask        %x - %x\n", (int)&fecp->eimr, fecp->eimr);
2678e585f02STsiChung Liew 	printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
2688e585f02STsiChung Liew 	printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
2698e585f02STsiChung Liew 	printf("ecntrl       %x - %x\n", (int)&fecp->ecr, fecp->ecr);
2708e585f02STsiChung Liew 	printf("mii_mframe   %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
2718e585f02STsiChung Liew 	printf("mii_speed    %x - %x\n", (int)&fecp->mscr, fecp->mscr);
2728e585f02STsiChung Liew 	printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
2738e585f02STsiChung Liew 	printf("r_cntrl      %x - %x\n", (int)&fecp->rcr, fecp->rcr);
2748e585f02STsiChung Liew 	printf("x_cntrl      %x - %x\n", (int)&fecp->tcr, fecp->tcr);
2758e585f02STsiChung Liew 	printf("padr_l       %x - %x\n", (int)&fecp->palr, fecp->palr);
2768e585f02STsiChung Liew 	printf("padr_u       %x - %x\n", (int)&fecp->paur, fecp->paur);
2778e585f02STsiChung Liew 	printf("op_pause     %x - %x\n", (int)&fecp->opd, fecp->opd);
2788e585f02STsiChung Liew 	printf("iadr_u       %x - %x\n", (int)&fecp->iaur, fecp->iaur);
2798e585f02STsiChung Liew 	printf("iadr_l       %x - %x\n", (int)&fecp->ialr, fecp->ialr);
2808e585f02STsiChung Liew 	printf("gadr_u       %x - %x\n", (int)&fecp->gaur, fecp->gaur);
2818e585f02STsiChung Liew 	printf("gadr_l       %x - %x\n", (int)&fecp->galr, fecp->galr);
2828e585f02STsiChung Liew 	printf("x_wmrk       %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
2838e585f02STsiChung Liew 	printf("r_bound      %x - %x\n", (int)&fecp->frbr, fecp->frbr);
2848e585f02STsiChung Liew 	printf("r_fstart     %x - %x\n", (int)&fecp->frsr, fecp->frsr);
2858e585f02STsiChung Liew 	printf("r_drng       %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
2868e585f02STsiChung Liew 	printf("x_drng       %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
2878e585f02STsiChung Liew 	printf("r_bufsz      %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
2888e585f02STsiChung Liew 
2898e585f02STsiChung Liew 	printf("\n");
2908e585f02STsiChung Liew 	printf("rmon_t_drop        %x - %x\n", (int)&fecp->rmon_t_drop,
2918e585f02STsiChung Liew 	       fecp->rmon_t_drop);
2928e585f02STsiChung Liew 	printf("rmon_t_packets     %x - %x\n", (int)&fecp->rmon_t_packets,
2938e585f02STsiChung Liew 	       fecp->rmon_t_packets);
2948e585f02STsiChung Liew 	printf("rmon_t_bc_pkt      %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
2958e585f02STsiChung Liew 	       fecp->rmon_t_bc_pkt);
2968e585f02STsiChung Liew 	printf("rmon_t_mc_pkt      %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
2978e585f02STsiChung Liew 	       fecp->rmon_t_mc_pkt);
2988e585f02STsiChung Liew 	printf("rmon_t_crc_align   %x - %x\n", (int)&fecp->rmon_t_crc_align,
2998e585f02STsiChung Liew 	       fecp->rmon_t_crc_align);
3008e585f02STsiChung Liew 	printf("rmon_t_undersize   %x - %x\n", (int)&fecp->rmon_t_undersize,
3018e585f02STsiChung Liew 	       fecp->rmon_t_undersize);
3028e585f02STsiChung Liew 	printf("rmon_t_oversize    %x - %x\n", (int)&fecp->rmon_t_oversize,
3038e585f02STsiChung Liew 	       fecp->rmon_t_oversize);
3048e585f02STsiChung Liew 	printf("rmon_t_frag        %x - %x\n", (int)&fecp->rmon_t_frag,
3058e585f02STsiChung Liew 	       fecp->rmon_t_frag);
3068e585f02STsiChung Liew 	printf("rmon_t_jab         %x - %x\n", (int)&fecp->rmon_t_jab,
3078e585f02STsiChung Liew 	       fecp->rmon_t_jab);
3088e585f02STsiChung Liew 	printf("rmon_t_col         %x - %x\n", (int)&fecp->rmon_t_col,
3098e585f02STsiChung Liew 	       fecp->rmon_t_col);
3108e585f02STsiChung Liew 	printf("rmon_t_p64         %x - %x\n", (int)&fecp->rmon_t_p64,
3118e585f02STsiChung Liew 	       fecp->rmon_t_p64);
3128e585f02STsiChung Liew 	printf("rmon_t_p65to127    %x - %x\n", (int)&fecp->rmon_t_p65to127,
3138e585f02STsiChung Liew 	       fecp->rmon_t_p65to127);
3148e585f02STsiChung Liew 	printf("rmon_t_p128to255   %x - %x\n", (int)&fecp->rmon_t_p128to255,
3158e585f02STsiChung Liew 	       fecp->rmon_t_p128to255);
3168e585f02STsiChung Liew 	printf("rmon_t_p256to511   %x - %x\n", (int)&fecp->rmon_t_p256to511,
3178e585f02STsiChung Liew 	       fecp->rmon_t_p256to511);
3188e585f02STsiChung Liew 	printf("rmon_t_p512to1023  %x - %x\n", (int)&fecp->rmon_t_p512to1023,
3198e585f02STsiChung Liew 	       fecp->rmon_t_p512to1023);
3208e585f02STsiChung Liew 	printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
3218e585f02STsiChung Liew 	       fecp->rmon_t_p1024to2047);
3228e585f02STsiChung Liew 	printf("rmon_t_p_gte2048   %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
3238e585f02STsiChung Liew 	       fecp->rmon_t_p_gte2048);
3248e585f02STsiChung Liew 	printf("rmon_t_octets      %x - %x\n", (int)&fecp->rmon_t_octets,
3258e585f02STsiChung Liew 	       fecp->rmon_t_octets);
3268e585f02STsiChung Liew 
3278e585f02STsiChung Liew 	printf("\n");
3288e585f02STsiChung Liew 	printf("ieee_t_drop      %x - %x\n", (int)&fecp->ieee_t_drop,
3298e585f02STsiChung Liew 	       fecp->ieee_t_drop);
3308e585f02STsiChung Liew 	printf("ieee_t_frame_ok  %x - %x\n", (int)&fecp->ieee_t_frame_ok,
3318e585f02STsiChung Liew 	       fecp->ieee_t_frame_ok);
3328e585f02STsiChung Liew 	printf("ieee_t_1col      %x - %x\n", (int)&fecp->ieee_t_1col,
3338e585f02STsiChung Liew 	       fecp->ieee_t_1col);
3348e585f02STsiChung Liew 	printf("ieee_t_mcol      %x - %x\n", (int)&fecp->ieee_t_mcol,
3358e585f02STsiChung Liew 	       fecp->ieee_t_mcol);
3368e585f02STsiChung Liew 	printf("ieee_t_def       %x - %x\n", (int)&fecp->ieee_t_def,
3378e585f02STsiChung Liew 	       fecp->ieee_t_def);
3388e585f02STsiChung Liew 	printf("ieee_t_lcol      %x - %x\n", (int)&fecp->ieee_t_lcol,
3398e585f02STsiChung Liew 	       fecp->ieee_t_lcol);
3408e585f02STsiChung Liew 	printf("ieee_t_excol     %x - %x\n", (int)&fecp->ieee_t_excol,
3418e585f02STsiChung Liew 	       fecp->ieee_t_excol);
3428e585f02STsiChung Liew 	printf("ieee_t_macerr    %x - %x\n", (int)&fecp->ieee_t_macerr,
3438e585f02STsiChung Liew 	       fecp->ieee_t_macerr);
3448e585f02STsiChung Liew 	printf("ieee_t_cserr     %x - %x\n", (int)&fecp->ieee_t_cserr,
3458e585f02STsiChung Liew 	       fecp->ieee_t_cserr);
3468e585f02STsiChung Liew 	printf("ieee_t_sqe       %x - %x\n", (int)&fecp->ieee_t_sqe,
3478e585f02STsiChung Liew 	       fecp->ieee_t_sqe);
3488e585f02STsiChung Liew 	printf("ieee_t_fdxfc     %x - %x\n", (int)&fecp->ieee_t_fdxfc,
3498e585f02STsiChung Liew 	       fecp->ieee_t_fdxfc);
3508e585f02STsiChung Liew 	printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
3518e585f02STsiChung Liew 	       fecp->ieee_t_octets_ok);
3528e585f02STsiChung Liew 
3538e585f02STsiChung Liew 	printf("\n");
3548e585f02STsiChung Liew 	printf("rmon_r_drop        %x - %x\n", (int)&fecp->rmon_r_drop,
3558e585f02STsiChung Liew 	       fecp->rmon_r_drop);
3568e585f02STsiChung Liew 	printf("rmon_r_packets     %x - %x\n", (int)&fecp->rmon_r_packets,
3578e585f02STsiChung Liew 	       fecp->rmon_r_packets);
3588e585f02STsiChung Liew 	printf("rmon_r_bc_pkt      %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
3598e585f02STsiChung Liew 	       fecp->rmon_r_bc_pkt);
3608e585f02STsiChung Liew 	printf("rmon_r_mc_pkt      %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
3618e585f02STsiChung Liew 	       fecp->rmon_r_mc_pkt);
3628e585f02STsiChung Liew 	printf("rmon_r_crc_align   %x - %x\n", (int)&fecp->rmon_r_crc_align,
3638e585f02STsiChung Liew 	       fecp->rmon_r_crc_align);
3648e585f02STsiChung Liew 	printf("rmon_r_undersize   %x - %x\n", (int)&fecp->rmon_r_undersize,
3658e585f02STsiChung Liew 	       fecp->rmon_r_undersize);
3668e585f02STsiChung Liew 	printf("rmon_r_oversize    %x - %x\n", (int)&fecp->rmon_r_oversize,
3678e585f02STsiChung Liew 	       fecp->rmon_r_oversize);
3688e585f02STsiChung Liew 	printf("rmon_r_frag        %x - %x\n", (int)&fecp->rmon_r_frag,
3698e585f02STsiChung Liew 	       fecp->rmon_r_frag);
3708e585f02STsiChung Liew 	printf("rmon_r_jab         %x - %x\n", (int)&fecp->rmon_r_jab,
3718e585f02STsiChung Liew 	       fecp->rmon_r_jab);
3728e585f02STsiChung Liew 	printf("rmon_r_p64         %x - %x\n", (int)&fecp->rmon_r_p64,
3738e585f02STsiChung Liew 	       fecp->rmon_r_p64);
3748e585f02STsiChung Liew 	printf("rmon_r_p65to127    %x - %x\n", (int)&fecp->rmon_r_p65to127,
3758e585f02STsiChung Liew 	       fecp->rmon_r_p65to127);
3768e585f02STsiChung Liew 	printf("rmon_r_p128to255   %x - %x\n", (int)&fecp->rmon_r_p128to255,
3778e585f02STsiChung Liew 	       fecp->rmon_r_p128to255);
3788e585f02STsiChung Liew 	printf("rmon_r_p256to511   %x - %x\n", (int)&fecp->rmon_r_p256to511,
3798e585f02STsiChung Liew 	       fecp->rmon_r_p256to511);
3808e585f02STsiChung Liew 	printf("rmon_r_p512to1023  %x - %x\n", (int)&fecp->rmon_r_p512to1023,
3818e585f02STsiChung Liew 	       fecp->rmon_r_p512to1023);
3828e585f02STsiChung Liew 	printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
3838e585f02STsiChung Liew 	       fecp->rmon_r_p1024to2047);
3848e585f02STsiChung Liew 	printf("rmon_r_p_gte2048   %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
3858e585f02STsiChung Liew 	       fecp->rmon_r_p_gte2048);
3868e585f02STsiChung Liew 	printf("rmon_r_octets      %x - %x\n", (int)&fecp->rmon_r_octets,
3878e585f02STsiChung Liew 	       fecp->rmon_r_octets);
3888e585f02STsiChung Liew 
3898e585f02STsiChung Liew 	printf("\n");
3908e585f02STsiChung Liew 	printf("ieee_r_drop      %x - %x\n", (int)&fecp->ieee_r_drop,
3918e585f02STsiChung Liew 	       fecp->ieee_r_drop);
3928e585f02STsiChung Liew 	printf("ieee_r_frame_ok  %x - %x\n", (int)&fecp->ieee_r_frame_ok,
3938e585f02STsiChung Liew 	       fecp->ieee_r_frame_ok);
3948e585f02STsiChung Liew 	printf("ieee_r_crc       %x - %x\n", (int)&fecp->ieee_r_crc,
3958e585f02STsiChung Liew 	       fecp->ieee_r_crc);
3968e585f02STsiChung Liew 	printf("ieee_r_align     %x - %x\n", (int)&fecp->ieee_r_align,
3978e585f02STsiChung Liew 	       fecp->ieee_r_align);
3988e585f02STsiChung Liew 	printf("ieee_r_macerr    %x - %x\n", (int)&fecp->ieee_r_macerr,
3998e585f02STsiChung Liew 	       fecp->ieee_r_macerr);
4008e585f02STsiChung Liew 	printf("ieee_r_fdxfc     %x - %x\n", (int)&fecp->ieee_r_fdxfc,
4018e585f02STsiChung Liew 	       fecp->ieee_r_fdxfc);
4028e585f02STsiChung Liew 	printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
4038e585f02STsiChung Liew 	       fecp->ieee_r_octets_ok);
4048e585f02STsiChung Liew 
4058e585f02STsiChung Liew 	printf("\n\n\n");
4068e585f02STsiChung Liew }
4078e585f02STsiChung Liew #endif
4088e585f02STsiChung Liew 
4098e585f02STsiChung Liew int fec_init(struct eth_device *dev, bd_t * bd)
4108e585f02STsiChung Liew {
4118e585f02STsiChung Liew 	struct fec_info_s *info = dev->priv;
4128e585f02STsiChung Liew 	volatile fec_t *fecp = (fec_t *) (info->iobase);
4138e585f02STsiChung Liew 	int i;
414f2208fbcSTsiChungLiew 	u8 *ea = NULL;
4158e585f02STsiChung Liew 
4168e585f02STsiChung Liew 	fecpin_setclear(dev, 1);
4178e585f02STsiChung Liew 
4188e585f02STsiChung Liew 	fec_reset(dev);
4198e585f02STsiChung Liew 
420ab77bc54STsiChungLiew #if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
4218e585f02STsiChung Liew 	defined (CFG_DISCOVER_PHY)
4228e585f02STsiChung Liew 
4238e585f02STsiChung Liew 	mii_init();
4248e585f02STsiChung Liew 
4258e585f02STsiChung Liew 	setFecDuplexSpeed(fecp, bd, info->dup_spd);
4268e585f02STsiChung Liew #else
4278e585f02STsiChung Liew #ifndef CFG_DISCOVER_PHY
4288e585f02STsiChung Liew 	setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
4298e585f02STsiChung Liew #endif				/* ifndef CFG_DISCOVER_PHY */
430ab77bc54STsiChungLiew #endif				/* CONFIG_CMD_MII || CONFIG_MII */
4318e585f02STsiChung Liew 
4328e585f02STsiChung Liew 	/* We use strictly polling mode only */
4338e585f02STsiChung Liew 	fecp->eimr = 0;
4348e585f02STsiChung Liew 
4358e585f02STsiChung Liew 	/* Clear any pending interrupt */
4368e585f02STsiChung Liew 	fecp->eir = 0xffffffff;
4378e585f02STsiChung Liew 
4388e585f02STsiChung Liew 	/* Set station address   */
4398e585f02STsiChung Liew 	if ((u32) fecp == CFG_FEC0_IOBASE) {
4408ae158cdSTsiChungLiew #ifdef CFG_FEC1_IOBASE
4418ae158cdSTsiChungLiew 		volatile fec_t *fecp1 = (fec_t *) (CFG_FEC1_IOBASE);
4428ae158cdSTsiChungLiew 		ea = &bd->bi_enet1addr[0];
4438ae158cdSTsiChungLiew 		fecp1->palr =
4448ae158cdSTsiChungLiew 		    (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
4458ae158cdSTsiChungLiew 		fecp1->paur = (ea[4] << 24) | (ea[5] << 16);
4468ae158cdSTsiChungLiew #endif
4478e585f02STsiChung Liew 		ea = &bd->bi_enetaddr[0];
4488ae158cdSTsiChungLiew 		fecp->palr =
4498ae158cdSTsiChungLiew 		    (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
4508ae158cdSTsiChungLiew 		fecp->paur = (ea[4] << 24) | (ea[5] << 16);
4518e585f02STsiChung Liew 	} else {
4528ae158cdSTsiChungLiew #ifdef CFG_FEC0_IOBASE
4538ae158cdSTsiChungLiew 		volatile fec_t *fecp0 = (fec_t *) (CFG_FEC0_IOBASE);
4548ae158cdSTsiChungLiew 		ea = &bd->bi_enetaddr[0];
4558ae158cdSTsiChungLiew 		fecp0->palr =
4568ae158cdSTsiChungLiew 		    (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
4578ae158cdSTsiChungLiew 		fecp0->paur = (ea[4] << 24) | (ea[5] << 16);
4588ae158cdSTsiChungLiew #endif
4598e585f02STsiChung Liew #ifdef CFG_FEC1_IOBASE
4608e585f02STsiChung Liew 		ea = &bd->bi_enet1addr[0];
4618ae158cdSTsiChungLiew 		fecp->palr =
4628ae158cdSTsiChungLiew 		    (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
4638ae158cdSTsiChungLiew 		fecp->paur = (ea[4] << 24) | (ea[5] << 16);
4648e585f02STsiChung Liew #endif
4658e585f02STsiChung Liew 	}
4668e585f02STsiChung Liew 
4678e585f02STsiChung Liew 	/* Clear unicast address hash table */
4688e585f02STsiChung Liew 	fecp->iaur = 0;
4698e585f02STsiChung Liew 	fecp->ialr = 0;
4708e585f02STsiChung Liew 
4718e585f02STsiChung Liew 	/* Clear multicast address hash table */
4728e585f02STsiChung Liew 	fecp->gaur = 0;
4738e585f02STsiChung Liew 	fecp->galr = 0;
4748e585f02STsiChung Liew 
4758e585f02STsiChung Liew 	/* Set maximum receive buffer size. */
4768e585f02STsiChung Liew 	fecp->emrbr = PKT_MAXBLR_SIZE;
4778e585f02STsiChung Liew 
4788e585f02STsiChung Liew 	/*
4798e585f02STsiChung Liew 	 * Setup Buffers and Buffer Desriptors
4808e585f02STsiChung Liew 	 */
4818e585f02STsiChung Liew 	info->rxIdx = 0;
4828e585f02STsiChung Liew 	info->txIdx = 0;
4838e585f02STsiChung Liew 
4848e585f02STsiChung Liew 	/*
4858e585f02STsiChung Liew 	 * Setup Receiver Buffer Descriptors (13.14.24.18)
4868e585f02STsiChung Liew 	 * Settings:
4878e585f02STsiChung Liew 	 *     Empty, Wrap
4888e585f02STsiChung Liew 	 */
4898e585f02STsiChung Liew 	for (i = 0; i < PKTBUFSRX; i++) {
4908e585f02STsiChung Liew 		info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
4918e585f02STsiChung Liew 		info->rxbd[i].cbd_datlen = 0;	/* Reset */
4928e585f02STsiChung Liew 		info->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
4938e585f02STsiChung Liew 	}
4948e585f02STsiChung Liew 	info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
4958e585f02STsiChung Liew 
4968e585f02STsiChung Liew 	/*
4978e585f02STsiChung Liew 	 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
4988e585f02STsiChung Liew 	 * Settings:
4998e585f02STsiChung Liew 	 *    Last, Tx CRC
5008e585f02STsiChung Liew 	 */
5018e585f02STsiChung Liew 	for (i = 0; i < TX_BUF_CNT; i++) {
5028e585f02STsiChung Liew 		info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
5038e585f02STsiChung Liew 		info->txbd[i].cbd_datlen = 0;	/* Reset */
5048e585f02STsiChung Liew 		info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
5058e585f02STsiChung Liew 	}
5068e585f02STsiChung Liew 	info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
5078e585f02STsiChung Liew 
5088e585f02STsiChung Liew 	/* Set receive and transmit descriptor base */
5098e585f02STsiChung Liew 	fecp->erdsr = (unsigned int)(&info->rxbd[0]);
5108e585f02STsiChung Liew 	fecp->etdsr = (unsigned int)(&info->txbd[0]);
5118e585f02STsiChung Liew 
5128e585f02STsiChung Liew 	/* Now enable the transmit and receive processing */
5138e585f02STsiChung Liew 	fecp->ecr |= FEC_ECR_ETHER_EN;
5148e585f02STsiChung Liew 
5158e585f02STsiChung Liew 	/* And last, try to fill Rx Buffer Descriptors */
5168e585f02STsiChung Liew 	fecp->rdar = 0x01000000;	/* Descriptor polling active    */
5178e585f02STsiChung Liew 
5188e585f02STsiChung Liew 	return 1;
5198e585f02STsiChung Liew }
5208e585f02STsiChung Liew 
5218e585f02STsiChung Liew void fec_reset(struct eth_device *dev)
5228e585f02STsiChung Liew {
5238e585f02STsiChung Liew 	struct fec_info_s *info = dev->priv;
5248e585f02STsiChung Liew 	volatile fec_t *fecp = (fec_t *) (info->iobase);
5258e585f02STsiChung Liew 	int i;
5268e585f02STsiChung Liew 
5278e585f02STsiChung Liew 	fecp->ecr = FEC_ECR_RESET;
5288e585f02STsiChung Liew 	for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
5298e585f02STsiChung Liew 		udelay(1);
5308e585f02STsiChung Liew 	}
5318e585f02STsiChung Liew 	if (i == FEC_RESET_DELAY) {
5328e585f02STsiChung Liew 		printf("FEC_RESET_DELAY timeout\n");
5338e585f02STsiChung Liew 	}
5348e585f02STsiChung Liew }
5358e585f02STsiChung Liew 
5368e585f02STsiChung Liew void fec_halt(struct eth_device *dev)
5378e585f02STsiChung Liew {
5388e585f02STsiChung Liew 	struct fec_info_s *info = dev->priv;
5398e585f02STsiChung Liew 
5408e585f02STsiChung Liew 	fec_reset(dev);
5418e585f02STsiChung Liew 
5428e585f02STsiChung Liew 	fecpin_setclear(dev, 0);
5438e585f02STsiChung Liew 
5448e585f02STsiChung Liew 	info->rxIdx = info->txIdx = 0;
5458e585f02STsiChung Liew 	memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
5468e585f02STsiChung Liew 	memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
5478e585f02STsiChung Liew 	memset(info->txbuf, 0, DBUF_LENGTH);
5488e585f02STsiChung Liew }
5498e585f02STsiChung Liew 
5508e585f02STsiChung Liew int mcffec_initialize(bd_t * bis)
5518e585f02STsiChung Liew {
5528e585f02STsiChung Liew 	struct eth_device *dev;
5538e585f02STsiChung Liew 	int i;
5548e585f02STsiChung Liew 
5558e585f02STsiChung Liew 	for (i = 0; i < sizeof(fec_info) / sizeof(fec_info[0]); i++) {
5568e585f02STsiChung Liew 
557f2208fbcSTsiChungLiew 		dev =
558f2208fbcSTsiChungLiew 		    (struct eth_device *)memalign(CFG_CACHELINE_SIZE,
559f2208fbcSTsiChungLiew 						  sizeof *dev);
5608e585f02STsiChung Liew 		if (dev == NULL)
5618e585f02STsiChung Liew 			hang();
5628e585f02STsiChung Liew 
5638e585f02STsiChung Liew 		memset(dev, 0, sizeof(*dev));
5648e585f02STsiChung Liew 
5658e585f02STsiChung Liew 		sprintf(dev->name, "FEC%d", fec_info[i].index);
5668e585f02STsiChung Liew 
5678e585f02STsiChung Liew 		dev->priv = &fec_info[i];
5688e585f02STsiChung Liew 		dev->init = fec_init;
5698e585f02STsiChung Liew 		dev->halt = fec_halt;
5708e585f02STsiChung Liew 		dev->send = fec_send;
5718e585f02STsiChung Liew 		dev->recv = fec_recv;
5728e585f02STsiChung Liew 
5738e585f02STsiChung Liew 		/* setup Receive and Transmit buffer descriptor */
5748e585f02STsiChung Liew 		fec_info[i].rxbd =
575f2208fbcSTsiChungLiew 		    (cbd_t *) memalign(CFG_CACHELINE_SIZE,
576f2208fbcSTsiChungLiew 				       (PKTBUFSRX * sizeof(cbd_t)));
5778e585f02STsiChung Liew 		fec_info[i].txbd =
578f2208fbcSTsiChungLiew 		    (cbd_t *) memalign(CFG_CACHELINE_SIZE,
579f2208fbcSTsiChungLiew 				       (TX_BUF_CNT * sizeof(cbd_t)));
580f2208fbcSTsiChungLiew 		fec_info[i].txbuf =
581f2208fbcSTsiChungLiew 		    (char *)memalign(CFG_CACHELINE_SIZE, DBUF_LENGTH);
5828e585f02STsiChung Liew #ifdef ET_DEBUG
5838e585f02STsiChung Liew 		printf("rxbd %x txbd %x\n",
5848e585f02STsiChung Liew 		       (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
5858e585f02STsiChung Liew #endif
5868e585f02STsiChung Liew 
587f2208fbcSTsiChungLiew 		fec_info[i].phy_name = (char *)memalign(CFG_CACHELINE_SIZE, 32);
5888e585f02STsiChung Liew 
5898e585f02STsiChung Liew 		eth_register(dev);
5908e585f02STsiChung Liew 
591ab77bc54STsiChungLiew #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
5928e585f02STsiChung Liew 		miiphy_register(dev->name,
5938e585f02STsiChung Liew 				mcffec_miiphy_read, mcffec_miiphy_write);
5948e585f02STsiChung Liew #endif
5958e585f02STsiChung Liew 	}
5968e585f02STsiChung Liew 
5978e585f02STsiChung Liew 	/* default speed */
5988e585f02STsiChung Liew 	bis->bi_ethspeed = 10;
5998e585f02STsiChung Liew 
6008e585f02STsiChung Liew 	return 1;
6018e585f02STsiChung Liew }
6028e585f02STsiChung Liew 
603ab77bc54STsiChungLiew #endif				/* CONFIG_CMD_NET, FEC_ENET & NET_MULTI */
6048e585f02STsiChung Liew #endif				/* CONFIG_MCFFEC */
605