xref: /rk3399_rockchip-uboot/drivers/net/mpc8xx_fec.c (revision fad51ac3af429d5b5b6f6ad4d924ee00e1525db1)
1*fad51ac3SChristophe Leroy /*
2*fad51ac3SChristophe Leroy  * (C) Copyright 2000
3*fad51ac3SChristophe Leroy  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*fad51ac3SChristophe Leroy  *
5*fad51ac3SChristophe Leroy  * SPDX-License-Identifier:	GPL-2.0+
6*fad51ac3SChristophe Leroy  */
7*fad51ac3SChristophe Leroy 
8*fad51ac3SChristophe Leroy #include <common.h>
9*fad51ac3SChristophe Leroy #include <command.h>
10*fad51ac3SChristophe Leroy #include <commproc.h>
11*fad51ac3SChristophe Leroy #include <malloc.h>
12*fad51ac3SChristophe Leroy #include <net.h>
13*fad51ac3SChristophe Leroy #include <asm/io.h>
14*fad51ac3SChristophe Leroy 
15*fad51ac3SChristophe Leroy #include <phy.h>
16*fad51ac3SChristophe Leroy 
17*fad51ac3SChristophe Leroy DECLARE_GLOBAL_DATA_PTR;
18*fad51ac3SChristophe Leroy 
19*fad51ac3SChristophe Leroy /* define WANT_MII when MII support is required */
20*fad51ac3SChristophe Leroy #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)
21*fad51ac3SChristophe Leroy #define WANT_MII
22*fad51ac3SChristophe Leroy #else
23*fad51ac3SChristophe Leroy #undef WANT_MII
24*fad51ac3SChristophe Leroy #endif
25*fad51ac3SChristophe Leroy 
26*fad51ac3SChristophe Leroy #if defined(WANT_MII)
27*fad51ac3SChristophe Leroy #include <miiphy.h>
28*fad51ac3SChristophe Leroy 
29*fad51ac3SChristophe Leroy #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
30*fad51ac3SChristophe Leroy #error "CONFIG_MII has to be defined!"
31*fad51ac3SChristophe Leroy #endif
32*fad51ac3SChristophe Leroy 
33*fad51ac3SChristophe Leroy #endif
34*fad51ac3SChristophe Leroy 
35*fad51ac3SChristophe Leroy #if defined(CONFIG_RMII) && !defined(WANT_MII)
36*fad51ac3SChristophe Leroy #error RMII support is unusable without a working PHY.
37*fad51ac3SChristophe Leroy #endif
38*fad51ac3SChristophe Leroy 
39*fad51ac3SChristophe Leroy #ifdef CONFIG_SYS_DISCOVER_PHY
40*fad51ac3SChristophe Leroy static int mii_discover_phy(struct eth_device *dev);
41*fad51ac3SChristophe Leroy #endif
42*fad51ac3SChristophe Leroy 
43*fad51ac3SChristophe Leroy int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg);
44*fad51ac3SChristophe Leroy int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
45*fad51ac3SChristophe Leroy 			u16 value);
46*fad51ac3SChristophe Leroy 
47*fad51ac3SChristophe Leroy static struct ether_fcc_info_s
48*fad51ac3SChristophe Leroy {
49*fad51ac3SChristophe Leroy 	int ether_index;
50*fad51ac3SChristophe Leroy 	int fecp_offset;
51*fad51ac3SChristophe Leroy 	int phy_addr;
52*fad51ac3SChristophe Leroy 	int actual_phy_addr;
53*fad51ac3SChristophe Leroy 	int initialized;
54*fad51ac3SChristophe Leroy }
55*fad51ac3SChristophe Leroy 	ether_fcc_info[] = {
56*fad51ac3SChristophe Leroy #if defined(CONFIG_ETHER_ON_FEC1)
57*fad51ac3SChristophe Leroy 	{
58*fad51ac3SChristophe Leroy 		0,
59*fad51ac3SChristophe Leroy 		offsetof(immap_t, im_cpm.cp_fec1),
60*fad51ac3SChristophe Leroy 		CONFIG_FEC1_PHY,
61*fad51ac3SChristophe Leroy 		-1,
62*fad51ac3SChristophe Leroy 		0,
63*fad51ac3SChristophe Leroy 
64*fad51ac3SChristophe Leroy 	},
65*fad51ac3SChristophe Leroy #endif
66*fad51ac3SChristophe Leroy #if defined(CONFIG_ETHER_ON_FEC2)
67*fad51ac3SChristophe Leroy 	{
68*fad51ac3SChristophe Leroy 		1,
69*fad51ac3SChristophe Leroy 		offsetof(immap_t, im_cpm.cp_fec2),
70*fad51ac3SChristophe Leroy 		CONFIG_FEC2_PHY,
71*fad51ac3SChristophe Leroy 		-1,
72*fad51ac3SChristophe Leroy 		0,
73*fad51ac3SChristophe Leroy 	},
74*fad51ac3SChristophe Leroy #endif
75*fad51ac3SChristophe Leroy };
76*fad51ac3SChristophe Leroy 
77*fad51ac3SChristophe Leroy /* Ethernet Transmit and Receive Buffers */
78*fad51ac3SChristophe Leroy #define DBUF_LENGTH  1520
79*fad51ac3SChristophe Leroy 
80*fad51ac3SChristophe Leroy #define TX_BUF_CNT 2
81*fad51ac3SChristophe Leroy 
82*fad51ac3SChristophe Leroy #define TOUT_LOOP 100
83*fad51ac3SChristophe Leroy 
84*fad51ac3SChristophe Leroy #define PKT_MAXBUF_SIZE		1518
85*fad51ac3SChristophe Leroy #define PKT_MINBUF_SIZE		64
86*fad51ac3SChristophe Leroy #define PKT_MAXBLR_SIZE		1520
87*fad51ac3SChristophe Leroy 
88*fad51ac3SChristophe Leroy #ifdef __GNUC__
89*fad51ac3SChristophe Leroy static char txbuf[DBUF_LENGTH] __aligned(8);
90*fad51ac3SChristophe Leroy #else
91*fad51ac3SChristophe Leroy #error txbuf must be aligned.
92*fad51ac3SChristophe Leroy #endif
93*fad51ac3SChristophe Leroy 
94*fad51ac3SChristophe Leroy static uint rxIdx;	/* index of the current RX buffer */
95*fad51ac3SChristophe Leroy static uint txIdx;	/* index of the current TX buffer */
96*fad51ac3SChristophe Leroy 
97*fad51ac3SChristophe Leroy /*
98*fad51ac3SChristophe Leroy   * FEC Ethernet Tx and Rx buffer descriptors allocated at the
99*fad51ac3SChristophe Leroy   *  immr->udata_bd address on Dual-Port RAM
100*fad51ac3SChristophe Leroy   * Provide for Double Buffering
101*fad51ac3SChristophe Leroy   */
102*fad51ac3SChristophe Leroy 
103*fad51ac3SChristophe Leroy struct common_buf_desc {
104*fad51ac3SChristophe Leroy 	cbd_t rxbd[PKTBUFSRX];		/* Rx BD */
105*fad51ac3SChristophe Leroy 	cbd_t txbd[TX_BUF_CNT];		/* Tx BD */
106*fad51ac3SChristophe Leroy };
107*fad51ac3SChristophe Leroy 
108*fad51ac3SChristophe Leroy static struct common_buf_desc __iomem *rtx;
109*fad51ac3SChristophe Leroy 
110*fad51ac3SChristophe Leroy static int fec_send(struct eth_device *dev, void *packet, int length);
111*fad51ac3SChristophe Leroy static int fec_recv(struct eth_device *dev);
112*fad51ac3SChristophe Leroy static int fec_init(struct eth_device *dev, bd_t *bd);
113*fad51ac3SChristophe Leroy static void fec_halt(struct eth_device *dev);
114*fad51ac3SChristophe Leroy #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
115*fad51ac3SChristophe Leroy static void __mii_init(void);
116*fad51ac3SChristophe Leroy #endif
117*fad51ac3SChristophe Leroy 
118*fad51ac3SChristophe Leroy int fec_initialize(bd_t *bis)
119*fad51ac3SChristophe Leroy {
120*fad51ac3SChristophe Leroy 	struct eth_device *dev;
121*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis;
122*fad51ac3SChristophe Leroy 	int             i;
123*fad51ac3SChristophe Leroy 
124*fad51ac3SChristophe Leroy 	for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) {
125*fad51ac3SChristophe Leroy 		dev = malloc(sizeof(*dev));
126*fad51ac3SChristophe Leroy 		if (dev == NULL)
127*fad51ac3SChristophe Leroy 			hang();
128*fad51ac3SChristophe Leroy 
129*fad51ac3SChristophe Leroy 		memset(dev, 0, sizeof(*dev));
130*fad51ac3SChristophe Leroy 
131*fad51ac3SChristophe Leroy 		/* for FEC1 make sure that the name of the interface is the same
132*fad51ac3SChristophe Leroy 		   as the old one for compatibility reasons */
133*fad51ac3SChristophe Leroy 		if (i == 0)
134*fad51ac3SChristophe Leroy 			strcpy(dev->name, "FEC");
135*fad51ac3SChristophe Leroy 		else
136*fad51ac3SChristophe Leroy 			sprintf(dev->name, "FEC%d",
137*fad51ac3SChristophe Leroy 				ether_fcc_info[i].ether_index + 1);
138*fad51ac3SChristophe Leroy 
139*fad51ac3SChristophe Leroy 		efis = &ether_fcc_info[i];
140*fad51ac3SChristophe Leroy 
141*fad51ac3SChristophe Leroy 		/*
142*fad51ac3SChristophe Leroy 		 * reset actual phy addr
143*fad51ac3SChristophe Leroy 		 */
144*fad51ac3SChristophe Leroy 		efis->actual_phy_addr = -1;
145*fad51ac3SChristophe Leroy 
146*fad51ac3SChristophe Leroy 		dev->priv = efis;
147*fad51ac3SChristophe Leroy 		dev->init = fec_init;
148*fad51ac3SChristophe Leroy 		dev->halt = fec_halt;
149*fad51ac3SChristophe Leroy 		dev->send = fec_send;
150*fad51ac3SChristophe Leroy 		dev->recv = fec_recv;
151*fad51ac3SChristophe Leroy 
152*fad51ac3SChristophe Leroy 		eth_register(dev);
153*fad51ac3SChristophe Leroy 
154*fad51ac3SChristophe Leroy #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
155*fad51ac3SChristophe Leroy 		int retval;
156*fad51ac3SChristophe Leroy 		struct mii_dev *mdiodev = mdio_alloc();
157*fad51ac3SChristophe Leroy 		if (!mdiodev)
158*fad51ac3SChristophe Leroy 			return -ENOMEM;
159*fad51ac3SChristophe Leroy 		strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
160*fad51ac3SChristophe Leroy 		mdiodev->read = fec8xx_miiphy_read;
161*fad51ac3SChristophe Leroy 		mdiodev->write = fec8xx_miiphy_write;
162*fad51ac3SChristophe Leroy 
163*fad51ac3SChristophe Leroy 		retval = mdio_register(mdiodev);
164*fad51ac3SChristophe Leroy 		if (retval < 0)
165*fad51ac3SChristophe Leroy 			return retval;
166*fad51ac3SChristophe Leroy #endif
167*fad51ac3SChristophe Leroy 	}
168*fad51ac3SChristophe Leroy 	return 1;
169*fad51ac3SChristophe Leroy }
170*fad51ac3SChristophe Leroy 
171*fad51ac3SChristophe Leroy static int fec_send(struct eth_device *dev, void *packet, int length)
172*fad51ac3SChristophe Leroy {
173*fad51ac3SChristophe Leroy 	int j, rc;
174*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis = dev->priv;
175*fad51ac3SChristophe Leroy 	fec_t __iomem *fecp =
176*fad51ac3SChristophe Leroy 			(fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
177*fad51ac3SChristophe Leroy 
178*fad51ac3SChristophe Leroy 	/* section 16.9.23.3
179*fad51ac3SChristophe Leroy 	 * Wait for ready
180*fad51ac3SChristophe Leroy 	 */
181*fad51ac3SChristophe Leroy 	j = 0;
182*fad51ac3SChristophe Leroy 	while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) &&
183*fad51ac3SChristophe Leroy 	       (j < TOUT_LOOP)) {
184*fad51ac3SChristophe Leroy 		udelay(1);
185*fad51ac3SChristophe Leroy 		j++;
186*fad51ac3SChristophe Leroy 	}
187*fad51ac3SChristophe Leroy 	if (j >= TOUT_LOOP)
188*fad51ac3SChristophe Leroy 		printf("TX not ready\n");
189*fad51ac3SChristophe Leroy 
190*fad51ac3SChristophe Leroy 	out_be32(&rtx->txbd[txIdx].cbd_bufaddr, (uint)packet);
191*fad51ac3SChristophe Leroy 	out_be16(&rtx->txbd[txIdx].cbd_datlen, length);
192*fad51ac3SChristophe Leroy 	setbits_be16(&rtx->txbd[txIdx].cbd_sc,
193*fad51ac3SChristophe Leroy 		     BD_ENET_TX_READY | BD_ENET_TX_LAST);
194*fad51ac3SChristophe Leroy 
195*fad51ac3SChristophe Leroy 	/* Activate transmit Buffer Descriptor polling */
196*fad51ac3SChristophe Leroy 	/* Descriptor polling active	*/
197*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_x_des_active, 0x01000000);
198*fad51ac3SChristophe Leroy 
199*fad51ac3SChristophe Leroy 	j = 0;
200*fad51ac3SChristophe Leroy 	while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) &&
201*fad51ac3SChristophe Leroy 	       (j < TOUT_LOOP)) {
202*fad51ac3SChristophe Leroy 		udelay(1);
203*fad51ac3SChristophe Leroy 		j++;
204*fad51ac3SChristophe Leroy 	}
205*fad51ac3SChristophe Leroy 	if (j >= TOUT_LOOP)
206*fad51ac3SChristophe Leroy 		printf("TX timeout\n");
207*fad51ac3SChristophe Leroy 
208*fad51ac3SChristophe Leroy 	/* return only status bits */;
209*fad51ac3SChristophe Leroy 	rc = in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_STATS;
210*fad51ac3SChristophe Leroy 
211*fad51ac3SChristophe Leroy 	txIdx = (txIdx + 1) % TX_BUF_CNT;
212*fad51ac3SChristophe Leroy 
213*fad51ac3SChristophe Leroy 	return rc;
214*fad51ac3SChristophe Leroy }
215*fad51ac3SChristophe Leroy 
216*fad51ac3SChristophe Leroy static int fec_recv(struct eth_device *dev)
217*fad51ac3SChristophe Leroy {
218*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis = dev->priv;
219*fad51ac3SChristophe Leroy 	fec_t __iomem *fecp =
220*fad51ac3SChristophe Leroy 			(fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
221*fad51ac3SChristophe Leroy 	int length;
222*fad51ac3SChristophe Leroy 
223*fad51ac3SChristophe Leroy 	for (;;) {
224*fad51ac3SChristophe Leroy 		/* section 16.9.23.2 */
225*fad51ac3SChristophe Leroy 		if (in_be16(&rtx->rxbd[rxIdx].cbd_sc) & BD_ENET_RX_EMPTY) {
226*fad51ac3SChristophe Leroy 			length = -1;
227*fad51ac3SChristophe Leroy 			break;	/* nothing received - leave for() loop */
228*fad51ac3SChristophe Leroy 		}
229*fad51ac3SChristophe Leroy 
230*fad51ac3SChristophe Leroy 		length = in_be16(&rtx->rxbd[rxIdx].cbd_datlen);
231*fad51ac3SChristophe Leroy 
232*fad51ac3SChristophe Leroy 		if (!(in_be16(&rtx->rxbd[rxIdx].cbd_sc) & 0x003f)) {
233*fad51ac3SChristophe Leroy 			uchar *rx = net_rx_packets[rxIdx];
234*fad51ac3SChristophe Leroy 
235*fad51ac3SChristophe Leroy 			length -= 4;
236*fad51ac3SChristophe Leroy 
237*fad51ac3SChristophe Leroy #if defined(CONFIG_CMD_CDP)
238*fad51ac3SChristophe Leroy 			if ((rx[0] & 1) != 0 &&
239*fad51ac3SChristophe Leroy 			    memcmp((uchar *)rx, net_bcast_ethaddr, 6) != 0 &&
240*fad51ac3SChristophe Leroy 			    !is_cdp_packet((uchar *)rx))
241*fad51ac3SChristophe Leroy 				rx = NULL;
242*fad51ac3SChristophe Leroy #endif
243*fad51ac3SChristophe Leroy 			/*
244*fad51ac3SChristophe Leroy 			 * Pass the packet up to the protocol layers.
245*fad51ac3SChristophe Leroy 			 */
246*fad51ac3SChristophe Leroy 			if (rx != NULL)
247*fad51ac3SChristophe Leroy 				net_process_received_packet(rx, length);
248*fad51ac3SChristophe Leroy 		}
249*fad51ac3SChristophe Leroy 
250*fad51ac3SChristophe Leroy 		/* Give the buffer back to the FEC. */
251*fad51ac3SChristophe Leroy 		out_be16(&rtx->rxbd[rxIdx].cbd_datlen, 0);
252*fad51ac3SChristophe Leroy 
253*fad51ac3SChristophe Leroy 		/* wrap around buffer index when necessary */
254*fad51ac3SChristophe Leroy 		if ((rxIdx + 1) >= PKTBUFSRX) {
255*fad51ac3SChristophe Leroy 			out_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc,
256*fad51ac3SChristophe Leroy 				 BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
257*fad51ac3SChristophe Leroy 			rxIdx = 0;
258*fad51ac3SChristophe Leroy 		} else {
259*fad51ac3SChristophe Leroy 			out_be16(&rtx->rxbd[rxIdx].cbd_sc, BD_ENET_RX_EMPTY);
260*fad51ac3SChristophe Leroy 			rxIdx++;
261*fad51ac3SChristophe Leroy 		}
262*fad51ac3SChristophe Leroy 
263*fad51ac3SChristophe Leroy 		/* Try to fill Buffer Descriptors */
264*fad51ac3SChristophe Leroy 		/* Descriptor polling active    */
265*fad51ac3SChristophe Leroy 		out_be32(&fecp->fec_r_des_active, 0x01000000);
266*fad51ac3SChristophe Leroy 	}
267*fad51ac3SChristophe Leroy 
268*fad51ac3SChristophe Leroy 	return length;
269*fad51ac3SChristophe Leroy }
270*fad51ac3SChristophe Leroy 
271*fad51ac3SChristophe Leroy /**************************************************************
272*fad51ac3SChristophe Leroy  *
273*fad51ac3SChristophe Leroy  * FEC Ethernet Initialization Routine
274*fad51ac3SChristophe Leroy  *
275*fad51ac3SChristophe Leroy  *************************************************************/
276*fad51ac3SChristophe Leroy 
277*fad51ac3SChristophe Leroy #define	FEC_ECNTRL_PINMUX	0x00000004
278*fad51ac3SChristophe Leroy #define FEC_ECNTRL_ETHER_EN	0x00000002
279*fad51ac3SChristophe Leroy #define FEC_ECNTRL_RESET	0x00000001
280*fad51ac3SChristophe Leroy 
281*fad51ac3SChristophe Leroy #define FEC_RCNTRL_BC_REJ	0x00000010
282*fad51ac3SChristophe Leroy #define FEC_RCNTRL_PROM		0x00000008
283*fad51ac3SChristophe Leroy #define FEC_RCNTRL_MII_MODE	0x00000004
284*fad51ac3SChristophe Leroy #define FEC_RCNTRL_DRT		0x00000002
285*fad51ac3SChristophe Leroy #define FEC_RCNTRL_LOOP		0x00000001
286*fad51ac3SChristophe Leroy 
287*fad51ac3SChristophe Leroy #define FEC_TCNTRL_FDEN		0x00000004
288*fad51ac3SChristophe Leroy #define FEC_TCNTRL_HBC		0x00000002
289*fad51ac3SChristophe Leroy #define FEC_TCNTRL_GTS		0x00000001
290*fad51ac3SChristophe Leroy 
291*fad51ac3SChristophe Leroy #define	FEC_RESET_DELAY		50
292*fad51ac3SChristophe Leroy 
293*fad51ac3SChristophe Leroy #if defined(CONFIG_RMII)
294*fad51ac3SChristophe Leroy 
295*fad51ac3SChristophe Leroy static inline void fec_10Mbps(struct eth_device *dev)
296*fad51ac3SChristophe Leroy {
297*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis = dev->priv;
298*fad51ac3SChristophe Leroy 	int fecidx = efis->ether_index;
299*fad51ac3SChristophe Leroy 	uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
300*fad51ac3SChristophe Leroy 	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
301*fad51ac3SChristophe Leroy 
302*fad51ac3SChristophe Leroy 	if ((unsigned int)fecidx >= 2)
303*fad51ac3SChristophe Leroy 		hang();
304*fad51ac3SChristophe Leroy 
305*fad51ac3SChristophe Leroy 	setbits_be32(&immr->im_cpm.cp_cptr, mask);
306*fad51ac3SChristophe Leroy }
307*fad51ac3SChristophe Leroy 
308*fad51ac3SChristophe Leroy static inline void fec_100Mbps(struct eth_device *dev)
309*fad51ac3SChristophe Leroy {
310*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis = dev->priv;
311*fad51ac3SChristophe Leroy 	int fecidx = efis->ether_index;
312*fad51ac3SChristophe Leroy 	uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
313*fad51ac3SChristophe Leroy 	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
314*fad51ac3SChristophe Leroy 
315*fad51ac3SChristophe Leroy 	if ((unsigned int)fecidx >= 2)
316*fad51ac3SChristophe Leroy 		hang();
317*fad51ac3SChristophe Leroy 
318*fad51ac3SChristophe Leroy 	clrbits_be32(&immr->im_cpm.cp_cptr, mask);
319*fad51ac3SChristophe Leroy }
320*fad51ac3SChristophe Leroy 
321*fad51ac3SChristophe Leroy #endif
322*fad51ac3SChristophe Leroy 
323*fad51ac3SChristophe Leroy static inline void fec_full_duplex(struct eth_device *dev)
324*fad51ac3SChristophe Leroy {
325*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis = dev->priv;
326*fad51ac3SChristophe Leroy 	fec_t __iomem *fecp =
327*fad51ac3SChristophe Leroy 			(fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
328*fad51ac3SChristophe Leroy 
329*fad51ac3SChristophe Leroy 	clrbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT);
330*fad51ac3SChristophe Leroy 	setbits_be32(&fecp->fec_x_cntrl,  FEC_TCNTRL_FDEN);	/* FD enable */
331*fad51ac3SChristophe Leroy }
332*fad51ac3SChristophe Leroy 
333*fad51ac3SChristophe Leroy static inline void fec_half_duplex(struct eth_device *dev)
334*fad51ac3SChristophe Leroy {
335*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis = dev->priv;
336*fad51ac3SChristophe Leroy 	fec_t __iomem *fecp =
337*fad51ac3SChristophe Leroy 			(fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
338*fad51ac3SChristophe Leroy 
339*fad51ac3SChristophe Leroy 	setbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT);
340*fad51ac3SChristophe Leroy 	clrbits_be32(&fecp->fec_x_cntrl,  FEC_TCNTRL_FDEN);	/* FD disable */
341*fad51ac3SChristophe Leroy }
342*fad51ac3SChristophe Leroy 
343*fad51ac3SChristophe Leroy static void fec_pin_init(int fecidx)
344*fad51ac3SChristophe Leroy {
345*fad51ac3SChristophe Leroy 	bd_t           *bd = gd->bd;
346*fad51ac3SChristophe Leroy 	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
347*fad51ac3SChristophe Leroy 
348*fad51ac3SChristophe Leroy 	/*
349*fad51ac3SChristophe Leroy 	 * Set MII speed to 2.5 MHz or slightly below.
350*fad51ac3SChristophe Leroy 	 *
351*fad51ac3SChristophe Leroy 	 * According to the MPC860T (Rev. D) Fast ethernet controller user
352*fad51ac3SChristophe Leroy 	 * manual (6.2.14),
353*fad51ac3SChristophe Leroy 	 * the MII management interface clock must be less than or equal
354*fad51ac3SChristophe Leroy 	 * to 2.5 MHz.
355*fad51ac3SChristophe Leroy 	 * This MDC frequency is equal to system clock / (2 * MII_SPEED).
356*fad51ac3SChristophe Leroy 	 * Then MII_SPEED = system_clock / 2 * 2,5 MHz.
357*fad51ac3SChristophe Leroy 	 *
358*fad51ac3SChristophe Leroy 	 * All MII configuration is done via FEC1 registers:
359*fad51ac3SChristophe Leroy 	 */
360*fad51ac3SChristophe Leroy 	out_be32(&immr->im_cpm.cp_fec1.fec_mii_speed,
361*fad51ac3SChristophe Leroy 		 ((bd->bi_intfreq + 4999999) / 5000000) << 1);
362*fad51ac3SChristophe Leroy 
363*fad51ac3SChristophe Leroy #if defined(CONFIG_MPC885) && defined(WANT_MII)
364*fad51ac3SChristophe Leroy 	/* use MDC for MII */
365*fad51ac3SChristophe Leroy 	setbits_be16(&immr->im_ioport.iop_pdpar, 0x0080);
366*fad51ac3SChristophe Leroy 	clrbits_be16(&immr->im_ioport.iop_pddir, 0x0080);
367*fad51ac3SChristophe Leroy #endif
368*fad51ac3SChristophe Leroy 
369*fad51ac3SChristophe Leroy 	if (fecidx == 0) {
370*fad51ac3SChristophe Leroy #if defined(CONFIG_ETHER_ON_FEC1)
371*fad51ac3SChristophe Leroy 
372*fad51ac3SChristophe Leroy #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */
373*fad51ac3SChristophe Leroy 
374*fad51ac3SChristophe Leroy #if !defined(CONFIG_RMII)
375*fad51ac3SChristophe Leroy 
376*fad51ac3SChristophe Leroy 		setbits_be16(&immr->im_ioport.iop_papar, 0xf830);
377*fad51ac3SChristophe Leroy 		setbits_be16(&immr->im_ioport.iop_padir, 0x0830);
378*fad51ac3SChristophe Leroy 		clrbits_be16(&immr->im_ioport.iop_padir, 0xf000);
379*fad51ac3SChristophe Leroy 
380*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pbpar, 0x00001001);
381*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00001001);
382*fad51ac3SChristophe Leroy 
383*fad51ac3SChristophe Leroy 		setbits_be16(&immr->im_ioport.iop_pcpar, 0x000c);
384*fad51ac3SChristophe Leroy 		clrbits_be16(&immr->im_ioport.iop_pcdir, 0x000c);
385*fad51ac3SChristophe Leroy 
386*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pepar, 0x00000003);
387*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pedir, 0x00000003);
388*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_peso, 0x00000003);
389*fad51ac3SChristophe Leroy 
390*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000100);
391*fad51ac3SChristophe Leroy 
392*fad51ac3SChristophe Leroy #else
393*fad51ac3SChristophe Leroy 
394*fad51ac3SChristophe Leroy #if !defined(CONFIG_FEC1_PHY_NORXERR)
395*fad51ac3SChristophe Leroy 		setbits_be16(&immr->im_ioport.iop_papar, 0x1000);
396*fad51ac3SChristophe Leroy 		clrbits_be16(&immr->im_ioport.iop_padir, 0x1000);
397*fad51ac3SChristophe Leroy #endif
398*fad51ac3SChristophe Leroy 		setbits_be16(&immr->im_ioport.iop_papar, 0xe810);
399*fad51ac3SChristophe Leroy 		setbits_be16(&immr->im_ioport.iop_padir, 0x0810);
400*fad51ac3SChristophe Leroy 		clrbits_be16(&immr->im_ioport.iop_padir, 0xe000);
401*fad51ac3SChristophe Leroy 
402*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pbpar, 0x00000001);
403*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00000001);
404*fad51ac3SChristophe Leroy 
405*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_cptr, 0x00000100);
406*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000050);
407*fad51ac3SChristophe Leroy 
408*fad51ac3SChristophe Leroy #endif /* !CONFIG_RMII */
409*fad51ac3SChristophe Leroy 
410*fad51ac3SChristophe Leroy #else
411*fad51ac3SChristophe Leroy 		/*
412*fad51ac3SChristophe Leroy 		 * Configure all of port D for MII.
413*fad51ac3SChristophe Leroy 		 */
414*fad51ac3SChristophe Leroy 		out_be16(&immr->im_ioport.iop_pdpar, 0x1fff);
415*fad51ac3SChristophe Leroy 		out_be16(&immr->im_ioport.iop_pddir, 0x1fff);
416*fad51ac3SChristophe Leroy #endif
417*fad51ac3SChristophe Leroy 
418*fad51ac3SChristophe Leroy #endif	/* CONFIG_ETHER_ON_FEC1 */
419*fad51ac3SChristophe Leroy 	} else if (fecidx == 1) {
420*fad51ac3SChristophe Leroy #if defined(CONFIG_ETHER_ON_FEC2)
421*fad51ac3SChristophe Leroy 
422*fad51ac3SChristophe Leroy #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */
423*fad51ac3SChristophe Leroy 
424*fad51ac3SChristophe Leroy #if !defined(CONFIG_RMII)
425*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pepar, 0x0003fffc);
426*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pedir, 0x0003fffc);
427*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_peso, 0x000087fc);
428*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_peso, 0x00037800);
429*fad51ac3SChristophe Leroy 
430*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000080);
431*fad51ac3SChristophe Leroy #else
432*fad51ac3SChristophe Leroy 
433*fad51ac3SChristophe Leroy #if !defined(CONFIG_FEC2_PHY_NORXERR)
434*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pepar, 0x00000010);
435*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pedir, 0x00000010);
436*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_peso, 0x00000010);
437*fad51ac3SChristophe Leroy #endif
438*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pepar, 0x00039620);
439*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_pedir, 0x00039620);
440*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_peso, 0x00031000);
441*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_peso, 0x00008620);
442*fad51ac3SChristophe Leroy 
443*fad51ac3SChristophe Leroy 		setbits_be32(&immr->im_cpm.cp_cptr, 0x00000080);
444*fad51ac3SChristophe Leroy 		clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000028);
445*fad51ac3SChristophe Leroy #endif /* CONFIG_RMII */
446*fad51ac3SChristophe Leroy 
447*fad51ac3SChristophe Leroy #endif /* CONFIG_MPC885 */
448*fad51ac3SChristophe Leroy 
449*fad51ac3SChristophe Leroy #endif /* CONFIG_ETHER_ON_FEC2 */
450*fad51ac3SChristophe Leroy 	}
451*fad51ac3SChristophe Leroy }
452*fad51ac3SChristophe Leroy 
453*fad51ac3SChristophe Leroy static int fec_reset(fec_t __iomem *fecp)
454*fad51ac3SChristophe Leroy {
455*fad51ac3SChristophe Leroy 	int i;
456*fad51ac3SChristophe Leroy 
457*fad51ac3SChristophe Leroy 	/* Whack a reset.
458*fad51ac3SChristophe Leroy 	 * A delay is required between a reset of the FEC block and
459*fad51ac3SChristophe Leroy 	 * initialization of other FEC registers because the reset takes
460*fad51ac3SChristophe Leroy 	 * some time to complete. If you don't delay, subsequent writes
461*fad51ac3SChristophe Leroy 	 * to FEC registers might get killed by the reset routine which is
462*fad51ac3SChristophe Leroy 	 * still in progress.
463*fad51ac3SChristophe Leroy 	 */
464*fad51ac3SChristophe Leroy 
465*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
466*fad51ac3SChristophe Leroy 	for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) &&
467*fad51ac3SChristophe Leroy 	     (i < FEC_RESET_DELAY); ++i)
468*fad51ac3SChristophe Leroy 		udelay(1);
469*fad51ac3SChristophe Leroy 
470*fad51ac3SChristophe Leroy 	if (i == FEC_RESET_DELAY)
471*fad51ac3SChristophe Leroy 		return -1;
472*fad51ac3SChristophe Leroy 
473*fad51ac3SChristophe Leroy 	return 0;
474*fad51ac3SChristophe Leroy }
475*fad51ac3SChristophe Leroy 
476*fad51ac3SChristophe Leroy static int fec_init(struct eth_device *dev, bd_t *bd)
477*fad51ac3SChristophe Leroy {
478*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis = dev->priv;
479*fad51ac3SChristophe Leroy 	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
480*fad51ac3SChristophe Leroy 	fec_t __iomem *fecp =
481*fad51ac3SChristophe Leroy 			(fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
482*fad51ac3SChristophe Leroy 	int i;
483*fad51ac3SChristophe Leroy 
484*fad51ac3SChristophe Leroy #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
485*fad51ac3SChristophe Leroy 	/* the MII interface is connected to FEC1
486*fad51ac3SChristophe Leroy 	 * so for the miiphy_xxx function to work we must
487*fad51ac3SChristophe Leroy 	 * call mii_init since fec_halt messes the thing up
488*fad51ac3SChristophe Leroy 	 */
489*fad51ac3SChristophe Leroy 	if (efis->ether_index != 0)
490*fad51ac3SChristophe Leroy 		__mii_init();
491*fad51ac3SChristophe Leroy #endif
492*fad51ac3SChristophe Leroy 
493*fad51ac3SChristophe Leroy 	if (fec_reset(fecp) < 0)
494*fad51ac3SChristophe Leroy 		printf("FEC_RESET_DELAY timeout\n");
495*fad51ac3SChristophe Leroy 
496*fad51ac3SChristophe Leroy 	/* We use strictly polling mode only
497*fad51ac3SChristophe Leroy 	 */
498*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_imask, 0);
499*fad51ac3SChristophe Leroy 
500*fad51ac3SChristophe Leroy 	/* Clear any pending interrupt
501*fad51ac3SChristophe Leroy 	 */
502*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_ievent, 0xffc0);
503*fad51ac3SChristophe Leroy 
504*fad51ac3SChristophe Leroy 	/* No need to set the IVEC register */
505*fad51ac3SChristophe Leroy 
506*fad51ac3SChristophe Leroy 	/* Set station address
507*fad51ac3SChristophe Leroy 	 */
508*fad51ac3SChristophe Leroy #define ea dev->enetaddr
509*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_addr_low, (ea[0] << 24) | (ea[1] << 16) |
510*fad51ac3SChristophe Leroy 				      (ea[2] << 8) | ea[3]);
511*fad51ac3SChristophe Leroy 	out_be16(&fecp->fec_addr_high, (ea[4] << 8) | ea[5]);
512*fad51ac3SChristophe Leroy #undef ea
513*fad51ac3SChristophe Leroy 
514*fad51ac3SChristophe Leroy #if defined(CONFIG_CMD_CDP)
515*fad51ac3SChristophe Leroy 	/*
516*fad51ac3SChristophe Leroy 	 * Turn on multicast address hash table
517*fad51ac3SChristophe Leroy 	 */
518*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_hash_table_high, 0xffffffff);
519*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_hash_table_low, 0xffffffff);
520*fad51ac3SChristophe Leroy #else
521*fad51ac3SChristophe Leroy 	/* Clear multicast address hash table
522*fad51ac3SChristophe Leroy 	 */
523*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_hash_table_high, 0);
524*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_hash_table_low, 0);
525*fad51ac3SChristophe Leroy #endif
526*fad51ac3SChristophe Leroy 
527*fad51ac3SChristophe Leroy 	/* Set maximum receive buffer size.
528*fad51ac3SChristophe Leroy 	 */
529*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_r_buff_size, PKT_MAXBLR_SIZE);
530*fad51ac3SChristophe Leroy 
531*fad51ac3SChristophe Leroy 	/* Set maximum frame length
532*fad51ac3SChristophe Leroy 	 */
533*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_r_hash, PKT_MAXBUF_SIZE);
534*fad51ac3SChristophe Leroy 
535*fad51ac3SChristophe Leroy 	/*
536*fad51ac3SChristophe Leroy 	 * Setup Buffers and Buffer Descriptors
537*fad51ac3SChristophe Leroy 	 */
538*fad51ac3SChristophe Leroy 	rxIdx = 0;
539*fad51ac3SChristophe Leroy 	txIdx = 0;
540*fad51ac3SChristophe Leroy 
541*fad51ac3SChristophe Leroy 	if (!rtx)
542*fad51ac3SChristophe Leroy 		rtx = (struct common_buf_desc __iomem *)
543*fad51ac3SChristophe Leroy 		      (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
544*fad51ac3SChristophe Leroy 	/*
545*fad51ac3SChristophe Leroy 	 * Setup Receiver Buffer Descriptors (13.14.24.18)
546*fad51ac3SChristophe Leroy 	 * Settings:
547*fad51ac3SChristophe Leroy 	 *     Empty, Wrap
548*fad51ac3SChristophe Leroy 	 */
549*fad51ac3SChristophe Leroy 	for (i = 0; i < PKTBUFSRX; i++) {
550*fad51ac3SChristophe Leroy 		out_be16(&rtx->rxbd[i].cbd_sc, BD_ENET_RX_EMPTY);
551*fad51ac3SChristophe Leroy 		out_be16(&rtx->rxbd[i].cbd_datlen, 0);	/* Reset */
552*fad51ac3SChristophe Leroy 		out_be32(&rtx->rxbd[i].cbd_bufaddr, (uint)net_rx_packets[i]);
553*fad51ac3SChristophe Leroy 	}
554*fad51ac3SChristophe Leroy 	setbits_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc, BD_ENET_RX_WRAP);
555*fad51ac3SChristophe Leroy 
556*fad51ac3SChristophe Leroy 	/*
557*fad51ac3SChristophe Leroy 	 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
558*fad51ac3SChristophe Leroy 	 * Settings:
559*fad51ac3SChristophe Leroy 	 *    Last, Tx CRC
560*fad51ac3SChristophe Leroy 	 */
561*fad51ac3SChristophe Leroy 	for (i = 0; i < TX_BUF_CNT; i++) {
562*fad51ac3SChristophe Leroy 		out_be16(&rtx->txbd[i].cbd_sc, BD_ENET_TX_LAST | BD_ENET_TX_TC);
563*fad51ac3SChristophe Leroy 		out_be16(&rtx->txbd[i].cbd_datlen, 0);	/* Reset */
564*fad51ac3SChristophe Leroy 		out_be32(&rtx->txbd[i].cbd_bufaddr, (uint)txbuf);
565*fad51ac3SChristophe Leroy 	}
566*fad51ac3SChristophe Leroy 	setbits_be16(&rtx->txbd[TX_BUF_CNT - 1].cbd_sc, BD_ENET_TX_WRAP);
567*fad51ac3SChristophe Leroy 
568*fad51ac3SChristophe Leroy 	/* Set receive and transmit descriptor base
569*fad51ac3SChristophe Leroy 	 */
570*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_r_des_start, (__force unsigned int)rtx->rxbd);
571*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_x_des_start, (__force unsigned int)rtx->txbd);
572*fad51ac3SChristophe Leroy 
573*fad51ac3SChristophe Leroy 	/* Enable MII mode
574*fad51ac3SChristophe Leroy 	 */
575*fad51ac3SChristophe Leroy 	/* Half duplex mode */
576*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT);
577*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_x_cntrl, 0);
578*fad51ac3SChristophe Leroy 
579*fad51ac3SChristophe Leroy 	/* Enable big endian and don't care about SDMA FC.
580*fad51ac3SChristophe Leroy 	 */
581*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_fun_code, 0x78000000);
582*fad51ac3SChristophe Leroy 
583*fad51ac3SChristophe Leroy 	/*
584*fad51ac3SChristophe Leroy 	 * Setup the pin configuration of the FEC
585*fad51ac3SChristophe Leroy 	 */
586*fad51ac3SChristophe Leroy 	fec_pin_init(efis->ether_index);
587*fad51ac3SChristophe Leroy 
588*fad51ac3SChristophe Leroy 	rxIdx = 0;
589*fad51ac3SChristophe Leroy 	txIdx = 0;
590*fad51ac3SChristophe Leroy 
591*fad51ac3SChristophe Leroy 	/*
592*fad51ac3SChristophe Leroy 	 * Now enable the transmit and receive processing
593*fad51ac3SChristophe Leroy 	 */
594*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
595*fad51ac3SChristophe Leroy 
596*fad51ac3SChristophe Leroy 	if (efis->phy_addr == -1) {
597*fad51ac3SChristophe Leroy #ifdef CONFIG_SYS_DISCOVER_PHY
598*fad51ac3SChristophe Leroy 		/*
599*fad51ac3SChristophe Leroy 		 * wait for the PHY to wake up after reset
600*fad51ac3SChristophe Leroy 		 */
601*fad51ac3SChristophe Leroy 		efis->actual_phy_addr = mii_discover_phy(dev);
602*fad51ac3SChristophe Leroy 
603*fad51ac3SChristophe Leroy 		if (efis->actual_phy_addr == -1) {
604*fad51ac3SChristophe Leroy 			printf("Unable to discover phy!\n");
605*fad51ac3SChristophe Leroy 			return -1;
606*fad51ac3SChristophe Leroy 		}
607*fad51ac3SChristophe Leroy #else
608*fad51ac3SChristophe Leroy 		efis->actual_phy_addr = -1;
609*fad51ac3SChristophe Leroy #endif
610*fad51ac3SChristophe Leroy 	} else {
611*fad51ac3SChristophe Leroy 		efis->actual_phy_addr = efis->phy_addr;
612*fad51ac3SChristophe Leroy 	}
613*fad51ac3SChristophe Leroy 
614*fad51ac3SChristophe Leroy #if defined(CONFIG_MII) && defined(CONFIG_RMII)
615*fad51ac3SChristophe Leroy 	/*
616*fad51ac3SChristophe Leroy 	 * adapt the RMII speed to the speed of the phy
617*fad51ac3SChristophe Leroy 	 */
618*fad51ac3SChristophe Leroy 	if (miiphy_speed(dev->name, efis->actual_phy_addr) == _100BASET)
619*fad51ac3SChristophe Leroy 		fec_100Mbps(dev);
620*fad51ac3SChristophe Leroy 	else
621*fad51ac3SChristophe Leroy 		fec_10Mbps(dev);
622*fad51ac3SChristophe Leroy #endif
623*fad51ac3SChristophe Leroy 
624*fad51ac3SChristophe Leroy #if defined(CONFIG_MII)
625*fad51ac3SChristophe Leroy 	/*
626*fad51ac3SChristophe Leroy 	 * adapt to the half/full speed settings
627*fad51ac3SChristophe Leroy 	 */
628*fad51ac3SChristophe Leroy 	if (miiphy_duplex(dev->name, efis->actual_phy_addr) == FULL)
629*fad51ac3SChristophe Leroy 		fec_full_duplex(dev);
630*fad51ac3SChristophe Leroy 	else
631*fad51ac3SChristophe Leroy 		fec_half_duplex(dev);
632*fad51ac3SChristophe Leroy #endif
633*fad51ac3SChristophe Leroy 
634*fad51ac3SChristophe Leroy 	/* And last, try to fill Rx Buffer Descriptors */
635*fad51ac3SChristophe Leroy 	/* Descriptor polling active    */
636*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_r_des_active, 0x01000000);
637*fad51ac3SChristophe Leroy 
638*fad51ac3SChristophe Leroy 	efis->initialized = 1;
639*fad51ac3SChristophe Leroy 
640*fad51ac3SChristophe Leroy 	return 0;
641*fad51ac3SChristophe Leroy }
642*fad51ac3SChristophe Leroy 
643*fad51ac3SChristophe Leroy 
644*fad51ac3SChristophe Leroy static void fec_halt(struct eth_device *dev)
645*fad51ac3SChristophe Leroy {
646*fad51ac3SChristophe Leroy 	struct ether_fcc_info_s *efis = dev->priv;
647*fad51ac3SChristophe Leroy 	fec_t __iomem *fecp =
648*fad51ac3SChristophe Leroy 			(fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
649*fad51ac3SChristophe Leroy 	int i;
650*fad51ac3SChristophe Leroy 
651*fad51ac3SChristophe Leroy 	/* avoid halt if initialized; mii gets stuck otherwise */
652*fad51ac3SChristophe Leroy 	if (!efis->initialized)
653*fad51ac3SChristophe Leroy 		return;
654*fad51ac3SChristophe Leroy 
655*fad51ac3SChristophe Leroy 	/* Whack a reset.
656*fad51ac3SChristophe Leroy 	 * A delay is required between a reset of the FEC block and
657*fad51ac3SChristophe Leroy 	 * initialization of other FEC registers because the reset takes
658*fad51ac3SChristophe Leroy 	 * some time to complete. If you don't delay, subsequent writes
659*fad51ac3SChristophe Leroy 	 * to FEC registers might get killed by the reset routine which is
660*fad51ac3SChristophe Leroy 	 * still in progress.
661*fad51ac3SChristophe Leroy 	 */
662*fad51ac3SChristophe Leroy 
663*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
664*fad51ac3SChristophe Leroy 	for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) &&
665*fad51ac3SChristophe Leroy 	     (i < FEC_RESET_DELAY); ++i)
666*fad51ac3SChristophe Leroy 		udelay(1);
667*fad51ac3SChristophe Leroy 
668*fad51ac3SChristophe Leroy 	if (i == FEC_RESET_DELAY) {
669*fad51ac3SChristophe Leroy 		printf("FEC_RESET_DELAY timeout\n");
670*fad51ac3SChristophe Leroy 		return;
671*fad51ac3SChristophe Leroy 	}
672*fad51ac3SChristophe Leroy 
673*fad51ac3SChristophe Leroy 	efis->initialized = 0;
674*fad51ac3SChristophe Leroy }
675*fad51ac3SChristophe Leroy 
676*fad51ac3SChristophe Leroy #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
677*fad51ac3SChristophe Leroy 
678*fad51ac3SChristophe Leroy /* Make MII read/write commands for the FEC.
679*fad51ac3SChristophe Leroy */
680*fad51ac3SChristophe Leroy 
681*fad51ac3SChristophe Leroy #define mk_mii_read(ADDR, REG)	(0x60020000 | ((ADDR << 23) | \
682*fad51ac3SChristophe Leroy 						(REG & 0x1f) << 18))
683*fad51ac3SChristophe Leroy 
684*fad51ac3SChristophe Leroy #define mk_mii_write(ADDR, REG, VAL)	(0x50020000 | ((ADDR << 23) | \
685*fad51ac3SChristophe Leroy 						(REG & 0x1f) << 18) | \
686*fad51ac3SChristophe Leroy 						(VAL & 0xffff))
687*fad51ac3SChristophe Leroy 
688*fad51ac3SChristophe Leroy /* Interrupt events/masks.
689*fad51ac3SChristophe Leroy */
690*fad51ac3SChristophe Leroy #define FEC_ENET_HBERR	((uint)0x80000000)	/* Heartbeat error */
691*fad51ac3SChristophe Leroy #define FEC_ENET_BABR	((uint)0x40000000)	/* Babbling receiver */
692*fad51ac3SChristophe Leroy #define FEC_ENET_BABT	((uint)0x20000000)	/* Babbling transmitter */
693*fad51ac3SChristophe Leroy #define FEC_ENET_GRA	((uint)0x10000000)	/* Graceful stop complete */
694*fad51ac3SChristophe Leroy #define FEC_ENET_TXF	((uint)0x08000000)	/* Full frame transmitted */
695*fad51ac3SChristophe Leroy #define FEC_ENET_TXB	((uint)0x04000000)	/* A buffer was transmitted */
696*fad51ac3SChristophe Leroy #define FEC_ENET_RXF	((uint)0x02000000)	/* Full frame received */
697*fad51ac3SChristophe Leroy #define FEC_ENET_RXB	((uint)0x01000000)	/* A buffer was received */
698*fad51ac3SChristophe Leroy #define FEC_ENET_MII	((uint)0x00800000)	/* MII interrupt */
699*fad51ac3SChristophe Leroy #define FEC_ENET_EBERR	((uint)0x00400000)	/* SDMA bus error */
700*fad51ac3SChristophe Leroy 
701*fad51ac3SChristophe Leroy /* send command to phy using mii, wait for result */
702*fad51ac3SChristophe Leroy static uint
703*fad51ac3SChristophe Leroy mii_send(uint mii_cmd)
704*fad51ac3SChristophe Leroy {
705*fad51ac3SChristophe Leroy 	uint mii_reply;
706*fad51ac3SChristophe Leroy 	fec_t __iomem *ep;
707*fad51ac3SChristophe Leroy 	int cnt;
708*fad51ac3SChristophe Leroy 	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
709*fad51ac3SChristophe Leroy 
710*fad51ac3SChristophe Leroy 	ep = &immr->im_cpm.cp_fec;
711*fad51ac3SChristophe Leroy 
712*fad51ac3SChristophe Leroy 	out_be32(&ep->fec_mii_data, mii_cmd);	/* command to phy */
713*fad51ac3SChristophe Leroy 
714*fad51ac3SChristophe Leroy 	/* wait for mii complete */
715*fad51ac3SChristophe Leroy 	cnt = 0;
716*fad51ac3SChristophe Leroy 	while (!(in_be32(&ep->fec_ievent) & FEC_ENET_MII)) {
717*fad51ac3SChristophe Leroy 		if (++cnt > 1000) {
718*fad51ac3SChristophe Leroy 			printf("mii_send STUCK!\n");
719*fad51ac3SChristophe Leroy 			break;
720*fad51ac3SChristophe Leroy 		}
721*fad51ac3SChristophe Leroy 	}
722*fad51ac3SChristophe Leroy 	mii_reply = in_be32(&ep->fec_mii_data);		/* result from phy */
723*fad51ac3SChristophe Leroy 	out_be32(&ep->fec_ievent, FEC_ENET_MII);	/* clear MII complete */
724*fad51ac3SChristophe Leroy 	return mii_reply & 0xffff;		/* data read from phy */
725*fad51ac3SChristophe Leroy }
726*fad51ac3SChristophe Leroy #endif
727*fad51ac3SChristophe Leroy 
728*fad51ac3SChristophe Leroy #if defined(CONFIG_SYS_DISCOVER_PHY)
729*fad51ac3SChristophe Leroy static int mii_discover_phy(struct eth_device *dev)
730*fad51ac3SChristophe Leroy {
731*fad51ac3SChristophe Leroy #define MAX_PHY_PASSES 11
732*fad51ac3SChristophe Leroy 	uint phyno;
733*fad51ac3SChristophe Leroy 	int  pass;
734*fad51ac3SChristophe Leroy 	uint phytype;
735*fad51ac3SChristophe Leroy 	int phyaddr;
736*fad51ac3SChristophe Leroy 
737*fad51ac3SChristophe Leroy 	phyaddr = -1;	/* didn't find a PHY yet */
738*fad51ac3SChristophe Leroy 	for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
739*fad51ac3SChristophe Leroy 		if (pass > 1) {
740*fad51ac3SChristophe Leroy 			/* PHY may need more time to recover from reset.
741*fad51ac3SChristophe Leroy 			 * The LXT970 needs 50ms typical, no maximum is
742*fad51ac3SChristophe Leroy 			 * specified, so wait 10ms before try again.
743*fad51ac3SChristophe Leroy 			 * With 11 passes this gives it 100ms to wake up.
744*fad51ac3SChristophe Leroy 			 */
745*fad51ac3SChristophe Leroy 			udelay(10000);	/* wait 10ms */
746*fad51ac3SChristophe Leroy 		}
747*fad51ac3SChristophe Leroy 		for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
748*fad51ac3SChristophe Leroy 			phytype = mii_send(mk_mii_read(phyno, MII_PHYSID2));
749*fad51ac3SChristophe Leroy 			if (phytype != 0xffff) {
750*fad51ac3SChristophe Leroy 				phyaddr = phyno;
751*fad51ac3SChristophe Leroy 				phytype |= mii_send(mk_mii_read(phyno,
752*fad51ac3SChristophe Leroy 								MII_PHYSID1)) << 16;
753*fad51ac3SChristophe Leroy 			}
754*fad51ac3SChristophe Leroy 		}
755*fad51ac3SChristophe Leroy 	}
756*fad51ac3SChristophe Leroy 	if (phyaddr < 0)
757*fad51ac3SChristophe Leroy 		printf("No PHY device found.\n");
758*fad51ac3SChristophe Leroy 
759*fad51ac3SChristophe Leroy 	return phyaddr;
760*fad51ac3SChristophe Leroy }
761*fad51ac3SChristophe Leroy #endif	/* CONFIG_SYS_DISCOVER_PHY */
762*fad51ac3SChristophe Leroy 
763*fad51ac3SChristophe Leroy #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
764*fad51ac3SChristophe Leroy 
765*fad51ac3SChristophe Leroy /****************************************************************************
766*fad51ac3SChristophe Leroy  * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet
767*fad51ac3SChristophe Leroy  * This function is a subset of eth_init
768*fad51ac3SChristophe Leroy  ****************************************************************************
769*fad51ac3SChristophe Leroy  */
770*fad51ac3SChristophe Leroy static void __mii_init(void)
771*fad51ac3SChristophe Leroy {
772*fad51ac3SChristophe Leroy 	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
773*fad51ac3SChristophe Leroy 	fec_t __iomem *fecp = &immr->im_cpm.cp_fec;
774*fad51ac3SChristophe Leroy 
775*fad51ac3SChristophe Leroy 	if (fec_reset(fecp) < 0)
776*fad51ac3SChristophe Leroy 		printf("FEC_RESET_DELAY timeout\n");
777*fad51ac3SChristophe Leroy 
778*fad51ac3SChristophe Leroy 	/* We use strictly polling mode only
779*fad51ac3SChristophe Leroy 	 */
780*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_imask, 0);
781*fad51ac3SChristophe Leroy 
782*fad51ac3SChristophe Leroy 	/* Clear any pending interrupt
783*fad51ac3SChristophe Leroy 	 */
784*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_ievent, 0xffc0);
785*fad51ac3SChristophe Leroy 
786*fad51ac3SChristophe Leroy 	/* Now enable the transmit and receive processing
787*fad51ac3SChristophe Leroy 	 */
788*fad51ac3SChristophe Leroy 	out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
789*fad51ac3SChristophe Leroy }
790*fad51ac3SChristophe Leroy 
791*fad51ac3SChristophe Leroy void mii_init(void)
792*fad51ac3SChristophe Leroy {
793*fad51ac3SChristophe Leroy 	int i;
794*fad51ac3SChristophe Leroy 
795*fad51ac3SChristophe Leroy 	__mii_init();
796*fad51ac3SChristophe Leroy 
797*fad51ac3SChristophe Leroy 	/* Setup the pin configuration of the FEC(s)
798*fad51ac3SChristophe Leroy 	*/
799*fad51ac3SChristophe Leroy 	for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++)
800*fad51ac3SChristophe Leroy 		fec_pin_init(ether_fcc_info[i].ether_index);
801*fad51ac3SChristophe Leroy }
802*fad51ac3SChristophe Leroy 
803*fad51ac3SChristophe Leroy /*****************************************************************************
804*fad51ac3SChristophe Leroy  * Read and write a MII PHY register, routines used by MII Utilities
805*fad51ac3SChristophe Leroy  *
806*fad51ac3SChristophe Leroy  * FIXME: These routines are expected to return 0 on success, but mii_send
807*fad51ac3SChristophe Leroy  *	  does _not_ return an error code. Maybe 0xFFFF means error, i.e.
808*fad51ac3SChristophe Leroy  *	  no PHY connected...
809*fad51ac3SChristophe Leroy  *	  For now always return 0.
810*fad51ac3SChristophe Leroy  * FIXME: These routines only work after calling eth_init() at least once!
811*fad51ac3SChristophe Leroy  *	  Otherwise they hang in mii_send() !!! Sorry!
812*fad51ac3SChristophe Leroy  *****************************************************************************/
813*fad51ac3SChristophe Leroy 
814*fad51ac3SChristophe Leroy int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
815*fad51ac3SChristophe Leroy {
816*fad51ac3SChristophe Leroy 	unsigned short value = 0;
817*fad51ac3SChristophe Leroy 	short rdreg;    /* register working value */
818*fad51ac3SChristophe Leroy 
819*fad51ac3SChristophe Leroy 	rdreg = mii_send(mk_mii_read(addr, reg));
820*fad51ac3SChristophe Leroy 
821*fad51ac3SChristophe Leroy 	value = rdreg;
822*fad51ac3SChristophe Leroy 	return value;
823*fad51ac3SChristophe Leroy }
824*fad51ac3SChristophe Leroy 
825*fad51ac3SChristophe Leroy int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
826*fad51ac3SChristophe Leroy 			u16 value)
827*fad51ac3SChristophe Leroy {
828*fad51ac3SChristophe Leroy 	(void)mii_send(mk_mii_write(addr, reg, value));
829*fad51ac3SChristophe Leroy 
830*fad51ac3SChristophe Leroy 	return 0;
831*fad51ac3SChristophe Leroy }
832*fad51ac3SChristophe Leroy #endif
833