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