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