xref: /linux/drivers/net/ethernet/freescale/fec_main.c (revision d13919301d9a34cd4d3cc5ac73b89012c48cad71)
1793fc096SFrank Li /*
2793fc096SFrank Li  * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3793fc096SFrank Li  * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4793fc096SFrank Li  *
5793fc096SFrank Li  * Right now, I am very wasteful with the buffers.  I allocate memory
6793fc096SFrank Li  * pages and then divide them into 2K frame buffers.  This way I know I
7793fc096SFrank Li  * have buffers large enough to hold one frame within one buffer descriptor.
8793fc096SFrank Li  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9793fc096SFrank Li  * will be much more memory efficient and will easily handle lots of
10793fc096SFrank Li  * small packets.
11793fc096SFrank Li  *
12793fc096SFrank Li  * Much better multiple PHY support by Magnus Damm.
13793fc096SFrank Li  * Copyright (c) 2000 Ericsson Radio Systems AB.
14793fc096SFrank Li  *
15793fc096SFrank Li  * Support for FEC controller of ColdFire processors.
16793fc096SFrank Li  * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
17793fc096SFrank Li  *
18793fc096SFrank Li  * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
19793fc096SFrank Li  * Copyright (c) 2004-2006 Macq Electronique SA.
20793fc096SFrank Li  *
21793fc096SFrank Li  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
22793fc096SFrank Li  */
23793fc096SFrank Li 
24793fc096SFrank Li #include <linux/module.h>
25793fc096SFrank Li #include <linux/kernel.h>
26793fc096SFrank Li #include <linux/string.h>
27793fc096SFrank Li #include <linux/ptrace.h>
28793fc096SFrank Li #include <linux/errno.h>
29793fc096SFrank Li #include <linux/ioport.h>
30793fc096SFrank Li #include <linux/slab.h>
31793fc096SFrank Li #include <linux/interrupt.h>
32793fc096SFrank Li #include <linux/init.h>
33793fc096SFrank Li #include <linux/delay.h>
34793fc096SFrank Li #include <linux/netdevice.h>
35793fc096SFrank Li #include <linux/etherdevice.h>
36793fc096SFrank Li #include <linux/skbuff.h>
374c09eed9SJim Baxter #include <linux/in.h>
384c09eed9SJim Baxter #include <linux/ip.h>
394c09eed9SJim Baxter #include <net/ip.h>
404c09eed9SJim Baxter #include <linux/tcp.h>
414c09eed9SJim Baxter #include <linux/udp.h>
424c09eed9SJim Baxter #include <linux/icmp.h>
43793fc096SFrank Li #include <linux/spinlock.h>
44793fc096SFrank Li #include <linux/workqueue.h>
45793fc096SFrank Li #include <linux/bitops.h>
46793fc096SFrank Li #include <linux/io.h>
47793fc096SFrank Li #include <linux/irq.h>
48793fc096SFrank Li #include <linux/clk.h>
49793fc096SFrank Li #include <linux/platform_device.h>
50793fc096SFrank Li #include <linux/phy.h>
51793fc096SFrank Li #include <linux/fec.h>
52793fc096SFrank Li #include <linux/of.h>
53793fc096SFrank Li #include <linux/of_device.h>
54793fc096SFrank Li #include <linux/of_gpio.h>
55793fc096SFrank Li #include <linux/of_net.h>
56793fc096SFrank Li #include <linux/pinctrl/consumer.h>
57793fc096SFrank Li #include <linux/regulator/consumer.h>
58793fc096SFrank Li 
59793fc096SFrank Li #include <asm/cacheflush.h>
60793fc096SFrank Li 
61793fc096SFrank Li #include "fec.h"
62793fc096SFrank Li 
63793fc096SFrank Li #if defined(CONFIG_ARM)
64793fc096SFrank Li #define FEC_ALIGNMENT	0xf
65793fc096SFrank Li #else
66793fc096SFrank Li #define FEC_ALIGNMENT	0x3
67793fc096SFrank Li #endif
68793fc096SFrank Li 
69793fc096SFrank Li #define DRIVER_NAME	"fec"
70793fc096SFrank Li #define FEC_NAPI_WEIGHT	64
71793fc096SFrank Li 
72793fc096SFrank Li /* Pause frame feild and FIFO threshold */
73793fc096SFrank Li #define FEC_ENET_FCE	(1 << 5)
74793fc096SFrank Li #define FEC_ENET_RSEM_V	0x84
75793fc096SFrank Li #define FEC_ENET_RSFL_V	16
76793fc096SFrank Li #define FEC_ENET_RAEM_V	0x8
77793fc096SFrank Li #define FEC_ENET_RAFL_V	0x8
78793fc096SFrank Li #define FEC_ENET_OPD_V	0xFFF0
79793fc096SFrank Li 
80793fc096SFrank Li /* Controller is ENET-MAC */
81793fc096SFrank Li #define FEC_QUIRK_ENET_MAC		(1 << 0)
82793fc096SFrank Li /* Controller needs driver to swap frame */
83793fc096SFrank Li #define FEC_QUIRK_SWAP_FRAME		(1 << 1)
84793fc096SFrank Li /* Controller uses gasket */
85793fc096SFrank Li #define FEC_QUIRK_USE_GASKET		(1 << 2)
86793fc096SFrank Li /* Controller has GBIT support */
87793fc096SFrank Li #define FEC_QUIRK_HAS_GBIT		(1 << 3)
88793fc096SFrank Li /* Controller has extend desc buffer */
89793fc096SFrank Li #define FEC_QUIRK_HAS_BUFDESC_EX	(1 << 4)
9048496255SShawn Guo /* Controller has hardware checksum support */
9148496255SShawn Guo #define FEC_QUIRK_HAS_CSUM		(1 << 5)
92793fc096SFrank Li 
93793fc096SFrank Li static struct platform_device_id fec_devtype[] = {
94793fc096SFrank Li 	{
95793fc096SFrank Li 		/* keep it for coldfire */
96793fc096SFrank Li 		.name = DRIVER_NAME,
97793fc096SFrank Li 		.driver_data = 0,
98793fc096SFrank Li 	}, {
99793fc096SFrank Li 		.name = "imx25-fec",
100793fc096SFrank Li 		.driver_data = FEC_QUIRK_USE_GASKET,
101793fc096SFrank Li 	}, {
102793fc096SFrank Li 		.name = "imx27-fec",
103793fc096SFrank Li 		.driver_data = 0,
104793fc096SFrank Li 	}, {
105793fc096SFrank Li 		.name = "imx28-fec",
106793fc096SFrank Li 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
107793fc096SFrank Li 	}, {
108793fc096SFrank Li 		.name = "imx6q-fec",
109793fc096SFrank Li 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
11048496255SShawn Guo 				FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM,
111793fc096SFrank Li 	}, {
11236803542SShawn Guo 		.name = "mvf600-fec",
113ca7c4a45SJingchang Lu 		.driver_data = FEC_QUIRK_ENET_MAC,
114ca7c4a45SJingchang Lu 	}, {
115793fc096SFrank Li 		/* sentinel */
116793fc096SFrank Li 	}
117793fc096SFrank Li };
118793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype);
119793fc096SFrank Li 
120793fc096SFrank Li enum imx_fec_type {
121793fc096SFrank Li 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
122793fc096SFrank Li 	IMX27_FEC,	/* runs on i.mx27/35/51 */
123793fc096SFrank Li 	IMX28_FEC,
124793fc096SFrank Li 	IMX6Q_FEC,
12536803542SShawn Guo 	MVF600_FEC,
126793fc096SFrank Li };
127793fc096SFrank Li 
128793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = {
129793fc096SFrank Li 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
130793fc096SFrank Li 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
131793fc096SFrank Li 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
132793fc096SFrank Li 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
13336803542SShawn Guo 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
134793fc096SFrank Li 	{ /* sentinel */ }
135793fc096SFrank Li };
136793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids);
137793fc096SFrank Li 
138793fc096SFrank Li static unsigned char macaddr[ETH_ALEN];
139793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0);
140793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
141793fc096SFrank Li 
142793fc096SFrank Li #if defined(CONFIG_M5272)
143793fc096SFrank Li /*
144793fc096SFrank Li  * Some hardware gets it MAC address out of local flash memory.
145793fc096SFrank Li  * if this is non-zero then assume it is the address to get MAC from.
146793fc096SFrank Li  */
147793fc096SFrank Li #if defined(CONFIG_NETtel)
148793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006006
149793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
150793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006000
151793fc096SFrank Li #elif defined(CONFIG_CANCam)
152793fc096SFrank Li #define	FEC_FLASHMAC	0xf0020000
153793fc096SFrank Li #elif defined (CONFIG_M5272C3)
154793fc096SFrank Li #define	FEC_FLASHMAC	(0xffe04000 + 4)
155793fc096SFrank Li #elif defined(CONFIG_MOD5272)
156793fc096SFrank Li #define FEC_FLASHMAC	0xffc0406b
157793fc096SFrank Li #else
158793fc096SFrank Li #define	FEC_FLASHMAC	0
159793fc096SFrank Li #endif
160793fc096SFrank Li #endif /* CONFIG_M5272 */
161793fc096SFrank Li 
162793fc096SFrank Li #if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
163793fc096SFrank Li #error "FEC: descriptor ring size constants too large"
164793fc096SFrank Li #endif
165793fc096SFrank Li 
166793fc096SFrank Li /* Interrupt events/masks. */
167793fc096SFrank Li #define FEC_ENET_HBERR	((uint)0x80000000)	/* Heartbeat error */
168793fc096SFrank Li #define FEC_ENET_BABR	((uint)0x40000000)	/* Babbling receiver */
169793fc096SFrank Li #define FEC_ENET_BABT	((uint)0x20000000)	/* Babbling transmitter */
170793fc096SFrank Li #define FEC_ENET_GRA	((uint)0x10000000)	/* Graceful stop complete */
171793fc096SFrank Li #define FEC_ENET_TXF	((uint)0x08000000)	/* Full frame transmitted */
172793fc096SFrank Li #define FEC_ENET_TXB	((uint)0x04000000)	/* A buffer was transmitted */
173793fc096SFrank Li #define FEC_ENET_RXF	((uint)0x02000000)	/* Full frame received */
174793fc096SFrank Li #define FEC_ENET_RXB	((uint)0x01000000)	/* A buffer was received */
175793fc096SFrank Li #define FEC_ENET_MII	((uint)0x00800000)	/* MII interrupt */
176793fc096SFrank Li #define FEC_ENET_EBERR	((uint)0x00400000)	/* SDMA bus error */
177793fc096SFrank Li 
178793fc096SFrank Li #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
179793fc096SFrank Li #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
180793fc096SFrank Li 
181793fc096SFrank Li /* The FEC stores dest/src/type, data, and checksum for receive packets.
182793fc096SFrank Li  */
183793fc096SFrank Li #define PKT_MAXBUF_SIZE		1518
184793fc096SFrank Li #define PKT_MINBUF_SIZE		64
185793fc096SFrank Li #define PKT_MAXBLR_SIZE		1520
186793fc096SFrank Li 
1874c09eed9SJim Baxter /* FEC receive acceleration */
1884c09eed9SJim Baxter #define FEC_RACC_IPDIS		(1 << 1)
1894c09eed9SJim Baxter #define FEC_RACC_PRODIS		(1 << 2)
1904c09eed9SJim Baxter #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
1914c09eed9SJim Baxter 
192793fc096SFrank Li /*
193793fc096SFrank Li  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
194793fc096SFrank Li  * size bits. Other FEC hardware does not, so we need to take that into
195793fc096SFrank Li  * account when setting it.
196793fc096SFrank Li  */
197793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
198793fc096SFrank Li     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
199793fc096SFrank Li #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
200793fc096SFrank Li #else
201793fc096SFrank Li #define	OPT_FRAME_SIZE	0
202793fc096SFrank Li #endif
203793fc096SFrank Li 
204793fc096SFrank Li /* FEC MII MMFR bits definition */
205793fc096SFrank Li #define FEC_MMFR_ST		(1 << 30)
206793fc096SFrank Li #define FEC_MMFR_OP_READ	(2 << 28)
207793fc096SFrank Li #define FEC_MMFR_OP_WRITE	(1 << 28)
208793fc096SFrank Li #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
209793fc096SFrank Li #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
210793fc096SFrank Li #define FEC_MMFR_TA		(2 << 16)
211793fc096SFrank Li #define FEC_MMFR_DATA(v)	(v & 0xffff)
212793fc096SFrank Li 
213793fc096SFrank Li #define FEC_MII_TIMEOUT		30000 /* us */
214793fc096SFrank Li 
215793fc096SFrank Li /* Transmitter timeout */
216793fc096SFrank Li #define TX_TIMEOUT (2 * HZ)
217793fc096SFrank Li 
218793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG	0x1
219793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE	0x2
220793fc096SFrank Li 
221793fc096SFrank Li static int mii_cnt;
222793fc096SFrank Li 
223793fc096SFrank Li static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, int is_ex)
224793fc096SFrank Li {
225793fc096SFrank Li 	struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
226793fc096SFrank Li 	if (is_ex)
227793fc096SFrank Li 		return (struct bufdesc *)(ex + 1);
228793fc096SFrank Li 	else
229793fc096SFrank Li 		return bdp + 1;
230793fc096SFrank Li }
231793fc096SFrank Li 
232793fc096SFrank Li static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, int is_ex)
233793fc096SFrank Li {
234793fc096SFrank Li 	struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
235793fc096SFrank Li 	if (is_ex)
236793fc096SFrank Li 		return (struct bufdesc *)(ex - 1);
237793fc096SFrank Li 	else
238793fc096SFrank Li 		return bdp - 1;
239793fc096SFrank Li }
240793fc096SFrank Li 
241793fc096SFrank Li static void *swap_buffer(void *bufaddr, int len)
242793fc096SFrank Li {
243793fc096SFrank Li 	int i;
244793fc096SFrank Li 	unsigned int *buf = bufaddr;
245793fc096SFrank Li 
246793fc096SFrank Li 	for (i = 0; i < (len + 3) / 4; i++, buf++)
247793fc096SFrank Li 		*buf = cpu_to_be32(*buf);
248793fc096SFrank Li 
249793fc096SFrank Li 	return bufaddr;
250793fc096SFrank Li }
251793fc096SFrank Li 
2524c09eed9SJim Baxter static int
2534c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
2544c09eed9SJim Baxter {
2554c09eed9SJim Baxter 	/* Only run for packets requiring a checksum. */
2564c09eed9SJim Baxter 	if (skb->ip_summed != CHECKSUM_PARTIAL)
2574c09eed9SJim Baxter 		return 0;
2584c09eed9SJim Baxter 
2594c09eed9SJim Baxter 	if (unlikely(skb_cow_head(skb, 0)))
2604c09eed9SJim Baxter 		return -1;
2614c09eed9SJim Baxter 
2624c09eed9SJim Baxter 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
2634c09eed9SJim Baxter 
2644c09eed9SJim Baxter 	return 0;
2654c09eed9SJim Baxter }
2664c09eed9SJim Baxter 
267793fc096SFrank Li static netdev_tx_t
268793fc096SFrank Li fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
269793fc096SFrank Li {
270793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
271793fc096SFrank Li 	const struct platform_device_id *id_entry =
272793fc096SFrank Li 				platform_get_device_id(fep->pdev);
273793fc096SFrank Li 	struct bufdesc *bdp;
274793fc096SFrank Li 	void *bufaddr;
275793fc096SFrank Li 	unsigned short	status;
276793fc096SFrank Li 	unsigned int index;
277793fc096SFrank Li 
278793fc096SFrank Li 	if (!fep->link) {
2794c09eed9SJim Baxter 		/* Link is down or auto-negotiation is in progress. */
280793fc096SFrank Li 		return NETDEV_TX_BUSY;
281793fc096SFrank Li 	}
282793fc096SFrank Li 
283793fc096SFrank Li 	/* Fill in a Tx ring entry */
284793fc096SFrank Li 	bdp = fep->cur_tx;
285793fc096SFrank Li 
286793fc096SFrank Li 	status = bdp->cbd_sc;
287793fc096SFrank Li 
288793fc096SFrank Li 	if (status & BD_ENET_TX_READY) {
289793fc096SFrank Li 		/* Ooops.  All transmit buffers are full.  Bail out.
290793fc096SFrank Li 		 * This should not happen, since ndev->tbusy should be set.
291793fc096SFrank Li 		 */
29231b7720cSJoe Perches 		netdev_err(ndev, "tx queue full!\n");
293793fc096SFrank Li 		return NETDEV_TX_BUSY;
294793fc096SFrank Li 	}
295793fc096SFrank Li 
2964c09eed9SJim Baxter 	/* Protocol checksum off-load for TCP and UDP. */
2974c09eed9SJim Baxter 	if (fec_enet_clear_csum(skb, ndev)) {
2984c09eed9SJim Baxter 		kfree_skb(skb);
2994c09eed9SJim Baxter 		return NETDEV_TX_OK;
3004c09eed9SJim Baxter 	}
3014c09eed9SJim Baxter 
302793fc096SFrank Li 	/* Clear all of the status flags */
303793fc096SFrank Li 	status &= ~BD_ENET_TX_STATS;
304793fc096SFrank Li 
305793fc096SFrank Li 	/* Set buffer length and buffer pointer */
306793fc096SFrank Li 	bufaddr = skb->data;
307793fc096SFrank Li 	bdp->cbd_datlen = skb->len;
308793fc096SFrank Li 
309793fc096SFrank Li 	/*
310793fc096SFrank Li 	 * On some FEC implementations data must be aligned on
311793fc096SFrank Li 	 * 4-byte boundaries. Use bounce buffers to copy data
312793fc096SFrank Li 	 * and get it aligned. Ugh.
313793fc096SFrank Li 	 */
314793fc096SFrank Li 	if (fep->bufdesc_ex)
315793fc096SFrank Li 		index = (struct bufdesc_ex *)bdp -
316793fc096SFrank Li 			(struct bufdesc_ex *)fep->tx_bd_base;
317793fc096SFrank Li 	else
318793fc096SFrank Li 		index = bdp - fep->tx_bd_base;
319793fc096SFrank Li 
320793fc096SFrank Li 	if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
321793fc096SFrank Li 		memcpy(fep->tx_bounce[index], skb->data, skb->len);
322793fc096SFrank Li 		bufaddr = fep->tx_bounce[index];
323793fc096SFrank Li 	}
324793fc096SFrank Li 
325793fc096SFrank Li 	/*
326793fc096SFrank Li 	 * Some design made an incorrect assumption on endian mode of
327793fc096SFrank Li 	 * the system that it's running on. As the result, driver has to
328793fc096SFrank Li 	 * swap every frame going to and coming from the controller.
329793fc096SFrank Li 	 */
330793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
331793fc096SFrank Li 		swap_buffer(bufaddr, skb->len);
332793fc096SFrank Li 
333793fc096SFrank Li 	/* Save skb pointer */
334793fc096SFrank Li 	fep->tx_skbuff[index] = skb;
335793fc096SFrank Li 
336793fc096SFrank Li 	/* Push the data cache so the CPM does not get stale memory
337793fc096SFrank Li 	 * data.
338793fc096SFrank Li 	 */
339793fc096SFrank Li 	bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
340793fc096SFrank Li 			FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
341793fc096SFrank Li 
342793fc096SFrank Li 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
343793fc096SFrank Li 	 * it's the last BD of the frame, and to put the CRC on the end.
344793fc096SFrank Li 	 */
345793fc096SFrank Li 	status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
346793fc096SFrank Li 			| BD_ENET_TX_LAST | BD_ENET_TX_TC);
347793fc096SFrank Li 	bdp->cbd_sc = status;
348793fc096SFrank Li 
349793fc096SFrank Li 	if (fep->bufdesc_ex) {
350793fc096SFrank Li 
351793fc096SFrank Li 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
352793fc096SFrank Li 		ebdp->cbd_bdu = 0;
353793fc096SFrank Li 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
354793fc096SFrank Li 			fep->hwts_tx_en)) {
355793fc096SFrank Li 			ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
356793fc096SFrank Li 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
357793fc096SFrank Li 		} else {
358793fc096SFrank Li 			ebdp->cbd_esc = BD_ENET_TX_INT;
3594c09eed9SJim Baxter 
3604c09eed9SJim Baxter 			/* Enable protocol checksum flags
3614c09eed9SJim Baxter 			 * We do not bother with the IP Checksum bits as they
3624c09eed9SJim Baxter 			 * are done by the kernel
3634c09eed9SJim Baxter 			 */
3644c09eed9SJim Baxter 			if (skb->ip_summed == CHECKSUM_PARTIAL)
3654c09eed9SJim Baxter 				ebdp->cbd_esc |= BD_ENET_TX_PINS;
366793fc096SFrank Li 		}
367793fc096SFrank Li 	}
368793fc096SFrank Li 	/* If this was the last BD in the ring, start at the beginning again. */
369793fc096SFrank Li 	if (status & BD_ENET_TX_WRAP)
370793fc096SFrank Li 		bdp = fep->tx_bd_base;
371793fc096SFrank Li 	else
372793fc096SFrank Li 		bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
373793fc096SFrank Li 
374793fc096SFrank Li 	fep->cur_tx = bdp;
375793fc096SFrank Li 
376793fc096SFrank Li 	if (fep->cur_tx == fep->dirty_tx)
377793fc096SFrank Li 		netif_stop_queue(ndev);
378793fc096SFrank Li 
379793fc096SFrank Li 	/* Trigger transmission start */
380793fc096SFrank Li 	writel(0, fep->hwp + FEC_X_DES_ACTIVE);
381793fc096SFrank Li 
382793fc096SFrank Li 	skb_tx_timestamp(skb);
383793fc096SFrank Li 
384793fc096SFrank Li 	return NETDEV_TX_OK;
385793fc096SFrank Li }
386793fc096SFrank Li 
387a210576cSDavid S. Miller /* Init RX & TX buffer descriptors
388a210576cSDavid S. Miller  */
389a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev)
390a210576cSDavid S. Miller {
391a210576cSDavid S. Miller 	struct fec_enet_private *fep = netdev_priv(dev);
392a210576cSDavid S. Miller 	struct bufdesc *bdp;
393a210576cSDavid S. Miller 	unsigned int i;
394a210576cSDavid S. Miller 
395a210576cSDavid S. Miller 	/* Initialize the receive buffer descriptors. */
396a210576cSDavid S. Miller 	bdp = fep->rx_bd_base;
397a210576cSDavid S. Miller 	for (i = 0; i < RX_RING_SIZE; i++) {
398a210576cSDavid S. Miller 
399a210576cSDavid S. Miller 		/* Initialize the BD for every fragment in the page. */
400a210576cSDavid S. Miller 		if (bdp->cbd_bufaddr)
401a210576cSDavid S. Miller 			bdp->cbd_sc = BD_ENET_RX_EMPTY;
402a210576cSDavid S. Miller 		else
403a210576cSDavid S. Miller 			bdp->cbd_sc = 0;
404a210576cSDavid S. Miller 		bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
405a210576cSDavid S. Miller 	}
406a210576cSDavid S. Miller 
407a210576cSDavid S. Miller 	/* Set the last buffer to wrap */
408a210576cSDavid S. Miller 	bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
409a210576cSDavid S. Miller 	bdp->cbd_sc |= BD_SC_WRAP;
410a210576cSDavid S. Miller 
411a210576cSDavid S. Miller 	fep->cur_rx = fep->rx_bd_base;
412a210576cSDavid S. Miller 
413a210576cSDavid S. Miller 	/* ...and the same for transmit */
414a210576cSDavid S. Miller 	bdp = fep->tx_bd_base;
415a210576cSDavid S. Miller 	fep->cur_tx = bdp;
416a210576cSDavid S. Miller 	for (i = 0; i < TX_RING_SIZE; i++) {
417a210576cSDavid S. Miller 
418a210576cSDavid S. Miller 		/* Initialize the BD for every fragment in the page. */
419a210576cSDavid S. Miller 		bdp->cbd_sc = 0;
420a210576cSDavid S. Miller 		if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
421a210576cSDavid S. Miller 			dev_kfree_skb_any(fep->tx_skbuff[i]);
422a210576cSDavid S. Miller 			fep->tx_skbuff[i] = NULL;
423a210576cSDavid S. Miller 		}
424a210576cSDavid S. Miller 		bdp->cbd_bufaddr = 0;
425a210576cSDavid S. Miller 		bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
426a210576cSDavid S. Miller 	}
427a210576cSDavid S. Miller 
428a210576cSDavid S. Miller 	/* Set the last buffer to wrap */
429a210576cSDavid S. Miller 	bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
430a210576cSDavid S. Miller 	bdp->cbd_sc |= BD_SC_WRAP;
431a210576cSDavid S. Miller 	fep->dirty_tx = bdp;
432a210576cSDavid S. Miller }
433a210576cSDavid S. Miller 
434793fc096SFrank Li /* This function is called to start or restart the FEC during a link
435793fc096SFrank Li  * change.  This only happens when switching between half and full
436793fc096SFrank Li  * duplex.
437793fc096SFrank Li  */
438793fc096SFrank Li static void
439793fc096SFrank Li fec_restart(struct net_device *ndev, int duplex)
440793fc096SFrank Li {
441793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
442793fc096SFrank Li 	const struct platform_device_id *id_entry =
443793fc096SFrank Li 				platform_get_device_id(fep->pdev);
444793fc096SFrank Li 	int i;
4454c09eed9SJim Baxter 	u32 val;
446793fc096SFrank Li 	u32 temp_mac[2];
447793fc096SFrank Li 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
448793fc096SFrank Li 	u32 ecntl = 0x2; /* ETHEREN */
449793fc096SFrank Li 
45054309fa6SFrank Li 	if (netif_running(ndev)) {
45154309fa6SFrank Li 		netif_device_detach(ndev);
45254309fa6SFrank Li 		napi_disable(&fep->napi);
45354309fa6SFrank Li 		netif_stop_queue(ndev);
45431691344SFabio Estevam 		netif_tx_lock_bh(ndev);
45554309fa6SFrank Li 	}
45654309fa6SFrank Li 
457793fc096SFrank Li 	/* Whack a reset.  We should wait for this. */
458793fc096SFrank Li 	writel(1, fep->hwp + FEC_ECNTRL);
459793fc096SFrank Li 	udelay(10);
460793fc096SFrank Li 
461793fc096SFrank Li 	/*
462793fc096SFrank Li 	 * enet-mac reset will reset mac address registers too,
463793fc096SFrank Li 	 * so need to reconfigure it.
464793fc096SFrank Li 	 */
465793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
466793fc096SFrank Li 		memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
467793fc096SFrank Li 		writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
468793fc096SFrank Li 		writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
469793fc096SFrank Li 	}
470793fc096SFrank Li 
471793fc096SFrank Li 	/* Clear any outstanding interrupt. */
472793fc096SFrank Li 	writel(0xffc00000, fep->hwp + FEC_IEVENT);
473793fc096SFrank Li 
474793fc096SFrank Li 	/* Reset all multicast.	*/
475793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
476793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
477793fc096SFrank Li #ifndef CONFIG_M5272
478793fc096SFrank Li 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
479793fc096SFrank Li 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
480793fc096SFrank Li #endif
481793fc096SFrank Li 
482793fc096SFrank Li 	/* Set maximum receive buffer size. */
483793fc096SFrank Li 	writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
484793fc096SFrank Li 
485a210576cSDavid S. Miller 	fec_enet_bd_init(ndev);
486a210576cSDavid S. Miller 
487793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
488793fc096SFrank Li 	writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
489793fc096SFrank Li 	if (fep->bufdesc_ex)
490793fc096SFrank Li 		writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
491793fc096SFrank Li 			* RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
492793fc096SFrank Li 	else
493793fc096SFrank Li 		writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
494793fc096SFrank Li 			* RX_RING_SIZE,	fep->hwp + FEC_X_DES_START);
495793fc096SFrank Li 
496793fc096SFrank Li 
497793fc096SFrank Li 	for (i = 0; i <= TX_RING_MOD_MASK; i++) {
498793fc096SFrank Li 		if (fep->tx_skbuff[i]) {
499793fc096SFrank Li 			dev_kfree_skb_any(fep->tx_skbuff[i]);
500793fc096SFrank Li 			fep->tx_skbuff[i] = NULL;
501793fc096SFrank Li 		}
502793fc096SFrank Li 	}
503793fc096SFrank Li 
504793fc096SFrank Li 	/* Enable MII mode */
505793fc096SFrank Li 	if (duplex) {
506793fc096SFrank Li 		/* FD enable */
507793fc096SFrank Li 		writel(0x04, fep->hwp + FEC_X_CNTRL);
508793fc096SFrank Li 	} else {
509793fc096SFrank Li 		/* No Rcv on Xmit */
510793fc096SFrank Li 		rcntl |= 0x02;
511793fc096SFrank Li 		writel(0x0, fep->hwp + FEC_X_CNTRL);
512793fc096SFrank Li 	}
513793fc096SFrank Li 
514793fc096SFrank Li 	fep->full_duplex = duplex;
515793fc096SFrank Li 
516793fc096SFrank Li 	/* Set MII speed */
517793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
518793fc096SFrank Li 
519*d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
5204c09eed9SJim Baxter 	/* set RX checksum */
5214c09eed9SJim Baxter 	val = readl(fep->hwp + FEC_RACC);
5224c09eed9SJim Baxter 	if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
5234c09eed9SJim Baxter 		val |= FEC_RACC_OPTIONS;
5244c09eed9SJim Baxter 	else
5254c09eed9SJim Baxter 		val &= ~FEC_RACC_OPTIONS;
5264c09eed9SJim Baxter 	writel(val, fep->hwp + FEC_RACC);
527*d1391930SGuenter Roeck #endif
5284c09eed9SJim Baxter 
529793fc096SFrank Li 	/*
530793fc096SFrank Li 	 * The phy interface and speed need to get configured
531793fc096SFrank Li 	 * differently on enet-mac.
532793fc096SFrank Li 	 */
533793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
534793fc096SFrank Li 		/* Enable flow control and length check */
535793fc096SFrank Li 		rcntl |= 0x40000000 | 0x00000020;
536793fc096SFrank Li 
537793fc096SFrank Li 		/* RGMII, RMII or MII */
538793fc096SFrank Li 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
539793fc096SFrank Li 			rcntl |= (1 << 6);
540793fc096SFrank Li 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
541793fc096SFrank Li 			rcntl |= (1 << 8);
542793fc096SFrank Li 		else
543793fc096SFrank Li 			rcntl &= ~(1 << 8);
544793fc096SFrank Li 
545793fc096SFrank Li 		/* 1G, 100M or 10M */
546793fc096SFrank Li 		if (fep->phy_dev) {
547793fc096SFrank Li 			if (fep->phy_dev->speed == SPEED_1000)
548793fc096SFrank Li 				ecntl |= (1 << 5);
549793fc096SFrank Li 			else if (fep->phy_dev->speed == SPEED_100)
550793fc096SFrank Li 				rcntl &= ~(1 << 9);
551793fc096SFrank Li 			else
552793fc096SFrank Li 				rcntl |= (1 << 9);
553793fc096SFrank Li 		}
554793fc096SFrank Li 	} else {
555793fc096SFrank Li #ifdef FEC_MIIGSK_ENR
556793fc096SFrank Li 		if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
557793fc096SFrank Li 			u32 cfgr;
558793fc096SFrank Li 			/* disable the gasket and wait */
559793fc096SFrank Li 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
560793fc096SFrank Li 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
561793fc096SFrank Li 				udelay(1);
562793fc096SFrank Li 
563793fc096SFrank Li 			/*
564793fc096SFrank Li 			 * configure the gasket:
565793fc096SFrank Li 			 *   RMII, 50 MHz, no loopback, no echo
566793fc096SFrank Li 			 *   MII, 25 MHz, no loopback, no echo
567793fc096SFrank Li 			 */
568793fc096SFrank Li 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
569793fc096SFrank Li 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
570793fc096SFrank Li 			if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
571793fc096SFrank Li 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
572793fc096SFrank Li 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
573793fc096SFrank Li 
574793fc096SFrank Li 			/* re-enable the gasket */
575793fc096SFrank Li 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
576793fc096SFrank Li 		}
577793fc096SFrank Li #endif
578793fc096SFrank Li 	}
579793fc096SFrank Li 
580*d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
581793fc096SFrank Li 	/* enable pause frame*/
582793fc096SFrank Li 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
583793fc096SFrank Li 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
584793fc096SFrank Li 	     fep->phy_dev && fep->phy_dev->pause)) {
585793fc096SFrank Li 		rcntl |= FEC_ENET_FCE;
586793fc096SFrank Li 
5874c09eed9SJim Baxter 		/* set FIFO threshold parameter to reduce overrun */
588793fc096SFrank Li 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
589793fc096SFrank Li 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
590793fc096SFrank Li 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
591793fc096SFrank Li 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
592793fc096SFrank Li 
593793fc096SFrank Li 		/* OPD */
594793fc096SFrank Li 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
595793fc096SFrank Li 	} else {
596793fc096SFrank Li 		rcntl &= ~FEC_ENET_FCE;
597793fc096SFrank Li 	}
598*d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
599793fc096SFrank Li 
600793fc096SFrank Li 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
601793fc096SFrank Li 
602793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
603793fc096SFrank Li 		/* enable ENET endian swap */
604793fc096SFrank Li 		ecntl |= (1 << 8);
605793fc096SFrank Li 		/* enable ENET store and forward mode */
606793fc096SFrank Li 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
607793fc096SFrank Li 	}
608793fc096SFrank Li 
609793fc096SFrank Li 	if (fep->bufdesc_ex)
610793fc096SFrank Li 		ecntl |= (1 << 4);
611793fc096SFrank Li 
612793fc096SFrank Li 	/* And last, enable the transmit and receive processing */
613793fc096SFrank Li 	writel(ecntl, fep->hwp + FEC_ECNTRL);
614793fc096SFrank Li 	writel(0, fep->hwp + FEC_R_DES_ACTIVE);
615793fc096SFrank Li 
616793fc096SFrank Li 	if (fep->bufdesc_ex)
617793fc096SFrank Li 		fec_ptp_start_cyclecounter(ndev);
618793fc096SFrank Li 
619793fc096SFrank Li 	/* Enable interrupts we wish to service */
620793fc096SFrank Li 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
62154309fa6SFrank Li 
62254309fa6SFrank Li 	if (netif_running(ndev)) {
62331691344SFabio Estevam 		netif_tx_unlock_bh(ndev);
6241ed0d56cSFabio Estevam 		netif_wake_queue(ndev);
6251ed0d56cSFabio Estevam 		napi_enable(&fep->napi);
6261ed0d56cSFabio Estevam 		netif_device_attach(ndev);
62754309fa6SFrank Li 	}
628793fc096SFrank Li }
629793fc096SFrank Li 
630793fc096SFrank Li static void
631793fc096SFrank Li fec_stop(struct net_device *ndev)
632793fc096SFrank Li {
633793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
634793fc096SFrank Li 	const struct platform_device_id *id_entry =
635793fc096SFrank Li 				platform_get_device_id(fep->pdev);
636793fc096SFrank Li 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
637793fc096SFrank Li 
638793fc096SFrank Li 	/* We cannot expect a graceful transmit stop without link !!! */
639793fc096SFrank Li 	if (fep->link) {
640793fc096SFrank Li 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
641793fc096SFrank Li 		udelay(10);
642793fc096SFrank Li 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
64331b7720cSJoe Perches 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
644793fc096SFrank Li 	}
645793fc096SFrank Li 
646793fc096SFrank Li 	/* Whack a reset.  We should wait for this. */
647793fc096SFrank Li 	writel(1, fep->hwp + FEC_ECNTRL);
648793fc096SFrank Li 	udelay(10);
649793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
650793fc096SFrank Li 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
651793fc096SFrank Li 
652793fc096SFrank Li 	/* We have to keep ENET enabled to have MII interrupt stay working */
653793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
654793fc096SFrank Li 		writel(2, fep->hwp + FEC_ECNTRL);
655793fc096SFrank Li 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
656793fc096SFrank Li 	}
657793fc096SFrank Li }
658793fc096SFrank Li 
659793fc096SFrank Li 
660793fc096SFrank Li static void
661793fc096SFrank Li fec_timeout(struct net_device *ndev)
662793fc096SFrank Li {
663793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
664793fc096SFrank Li 
665793fc096SFrank Li 	ndev->stats.tx_errors++;
666793fc096SFrank Li 
66754309fa6SFrank Li 	fep->delay_work.timeout = true;
66854309fa6SFrank Li 	schedule_delayed_work(&(fep->delay_work.delay_work), 0);
66954309fa6SFrank Li }
67054309fa6SFrank Li 
67154309fa6SFrank Li static void fec_enet_work(struct work_struct *work)
67254309fa6SFrank Li {
67354309fa6SFrank Li 	struct fec_enet_private *fep =
67454309fa6SFrank Li 		container_of(work,
67554309fa6SFrank Li 			     struct fec_enet_private,
67654309fa6SFrank Li 			     delay_work.delay_work.work);
67754309fa6SFrank Li 
67854309fa6SFrank Li 	if (fep->delay_work.timeout) {
67954309fa6SFrank Li 		fep->delay_work.timeout = false;
68054309fa6SFrank Li 		fec_restart(fep->netdev, fep->full_duplex);
68154309fa6SFrank Li 		netif_wake_queue(fep->netdev);
68254309fa6SFrank Li 	}
683793fc096SFrank Li }
684793fc096SFrank Li 
685793fc096SFrank Li static void
686793fc096SFrank Li fec_enet_tx(struct net_device *ndev)
687793fc096SFrank Li {
688793fc096SFrank Li 	struct	fec_enet_private *fep;
689793fc096SFrank Li 	struct bufdesc *bdp;
690793fc096SFrank Li 	unsigned short status;
691793fc096SFrank Li 	struct	sk_buff	*skb;
692793fc096SFrank Li 	int	index = 0;
693793fc096SFrank Li 
694793fc096SFrank Li 	fep = netdev_priv(ndev);
695793fc096SFrank Li 	bdp = fep->dirty_tx;
696793fc096SFrank Li 
697793fc096SFrank Li 	/* get next bdp of dirty_tx */
698793fc096SFrank Li 	if (bdp->cbd_sc & BD_ENET_TX_WRAP)
699793fc096SFrank Li 		bdp = fep->tx_bd_base;
700793fc096SFrank Li 	else
701793fc096SFrank Li 		bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
702793fc096SFrank Li 
703793fc096SFrank Li 	while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
704793fc096SFrank Li 
705793fc096SFrank Li 		/* current queue is empty */
706793fc096SFrank Li 		if (bdp == fep->cur_tx)
707793fc096SFrank Li 			break;
708793fc096SFrank Li 
709793fc096SFrank Li 		if (fep->bufdesc_ex)
710793fc096SFrank Li 			index = (struct bufdesc_ex *)bdp -
711793fc096SFrank Li 				(struct bufdesc_ex *)fep->tx_bd_base;
712793fc096SFrank Li 		else
713793fc096SFrank Li 			index = bdp - fep->tx_bd_base;
714793fc096SFrank Li 
715793fc096SFrank Li 		dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
716793fc096SFrank Li 				FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
717793fc096SFrank Li 		bdp->cbd_bufaddr = 0;
718793fc096SFrank Li 
719793fc096SFrank Li 		skb = fep->tx_skbuff[index];
720793fc096SFrank Li 
721793fc096SFrank Li 		/* Check for errors. */
722793fc096SFrank Li 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
723793fc096SFrank Li 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
724793fc096SFrank Li 				   BD_ENET_TX_CSL)) {
725793fc096SFrank Li 			ndev->stats.tx_errors++;
726793fc096SFrank Li 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
727793fc096SFrank Li 				ndev->stats.tx_heartbeat_errors++;
728793fc096SFrank Li 			if (status & BD_ENET_TX_LC)  /* Late collision */
729793fc096SFrank Li 				ndev->stats.tx_window_errors++;
730793fc096SFrank Li 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
731793fc096SFrank Li 				ndev->stats.tx_aborted_errors++;
732793fc096SFrank Li 			if (status & BD_ENET_TX_UN)  /* Underrun */
733793fc096SFrank Li 				ndev->stats.tx_fifo_errors++;
734793fc096SFrank Li 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
735793fc096SFrank Li 				ndev->stats.tx_carrier_errors++;
736793fc096SFrank Li 		} else {
737793fc096SFrank Li 			ndev->stats.tx_packets++;
738793fc096SFrank Li 		}
739793fc096SFrank Li 
740793fc096SFrank Li 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
741793fc096SFrank Li 			fep->bufdesc_ex) {
742793fc096SFrank Li 			struct skb_shared_hwtstamps shhwtstamps;
743793fc096SFrank Li 			unsigned long flags;
744793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
745793fc096SFrank Li 
746793fc096SFrank Li 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
747793fc096SFrank Li 			spin_lock_irqsave(&fep->tmreg_lock, flags);
748793fc096SFrank Li 			shhwtstamps.hwtstamp = ns_to_ktime(
749793fc096SFrank Li 				timecounter_cyc2time(&fep->tc, ebdp->ts));
750793fc096SFrank Li 			spin_unlock_irqrestore(&fep->tmreg_lock, flags);
751793fc096SFrank Li 			skb_tstamp_tx(skb, &shhwtstamps);
752793fc096SFrank Li 		}
753793fc096SFrank Li 
754793fc096SFrank Li 		if (status & BD_ENET_TX_READY)
75531b7720cSJoe Perches 			netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
756793fc096SFrank Li 
757793fc096SFrank Li 		/* Deferred means some collisions occurred during transmit,
758793fc096SFrank Li 		 * but we eventually sent the packet OK.
759793fc096SFrank Li 		 */
760793fc096SFrank Li 		if (status & BD_ENET_TX_DEF)
761793fc096SFrank Li 			ndev->stats.collisions++;
762793fc096SFrank Li 
763793fc096SFrank Li 		/* Free the sk buffer associated with this last transmit */
764793fc096SFrank Li 		dev_kfree_skb_any(skb);
765793fc096SFrank Li 		fep->tx_skbuff[index] = NULL;
766793fc096SFrank Li 
767793fc096SFrank Li 		fep->dirty_tx = bdp;
768793fc096SFrank Li 
769793fc096SFrank Li 		/* Update pointer to next buffer descriptor to be transmitted */
770793fc096SFrank Li 		if (status & BD_ENET_TX_WRAP)
771793fc096SFrank Li 			bdp = fep->tx_bd_base;
772793fc096SFrank Li 		else
773793fc096SFrank Li 			bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
774793fc096SFrank Li 
775793fc096SFrank Li 		/* Since we have freed up a buffer, the ring is no longer full
776793fc096SFrank Li 		 */
777793fc096SFrank Li 		if (fep->dirty_tx != fep->cur_tx) {
778793fc096SFrank Li 			if (netif_queue_stopped(ndev))
779793fc096SFrank Li 				netif_wake_queue(ndev);
780793fc096SFrank Li 		}
781793fc096SFrank Li 	}
782793fc096SFrank Li 	return;
783793fc096SFrank Li }
784793fc096SFrank Li 
785793fc096SFrank Li 
786793fc096SFrank Li /* During a receive, the cur_rx points to the current incoming buffer.
787793fc096SFrank Li  * When we update through the ring, if the next incoming buffer has
788793fc096SFrank Li  * not been given to the system, we just set the empty indicator,
789793fc096SFrank Li  * effectively tossing the packet.
790793fc096SFrank Li  */
791793fc096SFrank Li static int
792793fc096SFrank Li fec_enet_rx(struct net_device *ndev, int budget)
793793fc096SFrank Li {
794793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
795793fc096SFrank Li 	const struct platform_device_id *id_entry =
796793fc096SFrank Li 				platform_get_device_id(fep->pdev);
797793fc096SFrank Li 	struct bufdesc *bdp;
798793fc096SFrank Li 	unsigned short status;
799793fc096SFrank Li 	struct	sk_buff	*skb;
800793fc096SFrank Li 	ushort	pkt_len;
801793fc096SFrank Li 	__u8 *data;
802793fc096SFrank Li 	int	pkt_received = 0;
803793fc096SFrank Li 
804793fc096SFrank Li #ifdef CONFIG_M532x
805793fc096SFrank Li 	flush_cache_all();
806793fc096SFrank Li #endif
807793fc096SFrank Li 
808793fc096SFrank Li 	/* First, grab all of the stats for the incoming packet.
809793fc096SFrank Li 	 * These get messed up if we get called due to a busy condition.
810793fc096SFrank Li 	 */
811793fc096SFrank Li 	bdp = fep->cur_rx;
812793fc096SFrank Li 
813793fc096SFrank Li 	while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
814793fc096SFrank Li 
815793fc096SFrank Li 		if (pkt_received >= budget)
816793fc096SFrank Li 			break;
817793fc096SFrank Li 		pkt_received++;
818793fc096SFrank Li 
819793fc096SFrank Li 		/* Since we have allocated space to hold a complete frame,
820793fc096SFrank Li 		 * the last indicator should be set.
821793fc096SFrank Li 		 */
822793fc096SFrank Li 		if ((status & BD_ENET_RX_LAST) == 0)
82331b7720cSJoe Perches 			netdev_err(ndev, "rcv is not +last\n");
824793fc096SFrank Li 
825793fc096SFrank Li 		if (!fep->opened)
826793fc096SFrank Li 			goto rx_processing_done;
827793fc096SFrank Li 
828793fc096SFrank Li 		/* Check for errors. */
829793fc096SFrank Li 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
830793fc096SFrank Li 			   BD_ENET_RX_CR | BD_ENET_RX_OV)) {
831793fc096SFrank Li 			ndev->stats.rx_errors++;
832793fc096SFrank Li 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
833793fc096SFrank Li 				/* Frame too long or too short. */
834793fc096SFrank Li 				ndev->stats.rx_length_errors++;
835793fc096SFrank Li 			}
836793fc096SFrank Li 			if (status & BD_ENET_RX_NO)	/* Frame alignment */
837793fc096SFrank Li 				ndev->stats.rx_frame_errors++;
838793fc096SFrank Li 			if (status & BD_ENET_RX_CR)	/* CRC Error */
839793fc096SFrank Li 				ndev->stats.rx_crc_errors++;
840793fc096SFrank Li 			if (status & BD_ENET_RX_OV)	/* FIFO overrun */
841793fc096SFrank Li 				ndev->stats.rx_fifo_errors++;
842793fc096SFrank Li 		}
843793fc096SFrank Li 
844793fc096SFrank Li 		/* Report late collisions as a frame error.
845793fc096SFrank Li 		 * On this error, the BD is closed, but we don't know what we
846793fc096SFrank Li 		 * have in the buffer.  So, just drop this frame on the floor.
847793fc096SFrank Li 		 */
848793fc096SFrank Li 		if (status & BD_ENET_RX_CL) {
849793fc096SFrank Li 			ndev->stats.rx_errors++;
850793fc096SFrank Li 			ndev->stats.rx_frame_errors++;
851793fc096SFrank Li 			goto rx_processing_done;
852793fc096SFrank Li 		}
853793fc096SFrank Li 
854793fc096SFrank Li 		/* Process the incoming frame. */
855793fc096SFrank Li 		ndev->stats.rx_packets++;
856793fc096SFrank Li 		pkt_len = bdp->cbd_datlen;
857793fc096SFrank Li 		ndev->stats.rx_bytes += pkt_len;
858793fc096SFrank Li 		data = (__u8*)__va(bdp->cbd_bufaddr);
859793fc096SFrank Li 
860793fc096SFrank Li 		dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
861793fc096SFrank Li 				FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
862793fc096SFrank Li 
863793fc096SFrank Li 		if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
864793fc096SFrank Li 			swap_buffer(data, pkt_len);
865793fc096SFrank Li 
866793fc096SFrank Li 		/* This does 16 byte alignment, exactly what we need.
867793fc096SFrank Li 		 * The packet length includes FCS, but we don't want to
868793fc096SFrank Li 		 * include that when passing upstream as it messes up
869793fc096SFrank Li 		 * bridging applications.
870793fc096SFrank Li 		 */
871793fc096SFrank Li 		skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
872793fc096SFrank Li 
873793fc096SFrank Li 		if (unlikely(!skb)) {
874793fc096SFrank Li 			ndev->stats.rx_dropped++;
875793fc096SFrank Li 		} else {
876793fc096SFrank Li 			skb_reserve(skb, NET_IP_ALIGN);
877793fc096SFrank Li 			skb_put(skb, pkt_len - 4);	/* Make room */
878793fc096SFrank Li 			skb_copy_to_linear_data(skb, data, pkt_len - 4);
879793fc096SFrank Li 			skb->protocol = eth_type_trans(skb, ndev);
880793fc096SFrank Li 
881793fc096SFrank Li 			/* Get receive timestamp from the skb */
882793fc096SFrank Li 			if (fep->hwts_rx_en && fep->bufdesc_ex) {
883793fc096SFrank Li 				struct skb_shared_hwtstamps *shhwtstamps =
884793fc096SFrank Li 							    skb_hwtstamps(skb);
885793fc096SFrank Li 				unsigned long flags;
886793fc096SFrank Li 				struct bufdesc_ex *ebdp =
887793fc096SFrank Li 					(struct bufdesc_ex *)bdp;
888793fc096SFrank Li 
889793fc096SFrank Li 				memset(shhwtstamps, 0, sizeof(*shhwtstamps));
890793fc096SFrank Li 
891793fc096SFrank Li 				spin_lock_irqsave(&fep->tmreg_lock, flags);
892793fc096SFrank Li 				shhwtstamps->hwtstamp = ns_to_ktime(
893793fc096SFrank Li 				    timecounter_cyc2time(&fep->tc, ebdp->ts));
894793fc096SFrank Li 				spin_unlock_irqrestore(&fep->tmreg_lock, flags);
895793fc096SFrank Li 			}
896793fc096SFrank Li 
8974c09eed9SJim Baxter 			if (fep->bufdesc_ex &&
8984c09eed9SJim Baxter 				(fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
8994c09eed9SJim Baxter 				struct bufdesc_ex *ebdp =
9004c09eed9SJim Baxter 					(struct bufdesc_ex *)bdp;
9014c09eed9SJim Baxter 				if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
9024c09eed9SJim Baxter 					/* don't check it */
9034c09eed9SJim Baxter 					skb->ip_summed = CHECKSUM_UNNECESSARY;
9044c09eed9SJim Baxter 				} else {
9054c09eed9SJim Baxter 					skb_checksum_none_assert(skb);
9064c09eed9SJim Baxter 				}
9074c09eed9SJim Baxter 			}
9084c09eed9SJim Baxter 
909793fc096SFrank Li 			if (!skb_defer_rx_timestamp(skb))
910793fc096SFrank Li 				napi_gro_receive(&fep->napi, skb);
911793fc096SFrank Li 		}
912793fc096SFrank Li 
913793fc096SFrank Li 		bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
914793fc096SFrank Li 				FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
915793fc096SFrank Li rx_processing_done:
916793fc096SFrank Li 		/* Clear the status flags for this buffer */
917793fc096SFrank Li 		status &= ~BD_ENET_RX_STATS;
918793fc096SFrank Li 
919793fc096SFrank Li 		/* Mark the buffer empty */
920793fc096SFrank Li 		status |= BD_ENET_RX_EMPTY;
921793fc096SFrank Li 		bdp->cbd_sc = status;
922793fc096SFrank Li 
923793fc096SFrank Li 		if (fep->bufdesc_ex) {
924793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
925793fc096SFrank Li 
926793fc096SFrank Li 			ebdp->cbd_esc = BD_ENET_RX_INT;
927793fc096SFrank Li 			ebdp->cbd_prot = 0;
928793fc096SFrank Li 			ebdp->cbd_bdu = 0;
929793fc096SFrank Li 		}
930793fc096SFrank Li 
931793fc096SFrank Li 		/* Update BD pointer to next entry */
932793fc096SFrank Li 		if (status & BD_ENET_RX_WRAP)
933793fc096SFrank Li 			bdp = fep->rx_bd_base;
934793fc096SFrank Li 		else
935793fc096SFrank Li 			bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
936793fc096SFrank Li 		/* Doing this here will keep the FEC running while we process
937793fc096SFrank Li 		 * incoming frames.  On a heavily loaded network, we should be
938793fc096SFrank Li 		 * able to keep up at the expense of system resources.
939793fc096SFrank Li 		 */
940793fc096SFrank Li 		writel(0, fep->hwp + FEC_R_DES_ACTIVE);
941793fc096SFrank Li 	}
942793fc096SFrank Li 	fep->cur_rx = bdp;
943793fc096SFrank Li 
944793fc096SFrank Li 	return pkt_received;
945793fc096SFrank Li }
946793fc096SFrank Li 
947793fc096SFrank Li static irqreturn_t
948793fc096SFrank Li fec_enet_interrupt(int irq, void *dev_id)
949793fc096SFrank Li {
950793fc096SFrank Li 	struct net_device *ndev = dev_id;
951793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
952793fc096SFrank Li 	uint int_events;
953793fc096SFrank Li 	irqreturn_t ret = IRQ_NONE;
954793fc096SFrank Li 
955793fc096SFrank Li 	do {
956793fc096SFrank Li 		int_events = readl(fep->hwp + FEC_IEVENT);
957793fc096SFrank Li 		writel(int_events, fep->hwp + FEC_IEVENT);
958793fc096SFrank Li 
959793fc096SFrank Li 		if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
960793fc096SFrank Li 			ret = IRQ_HANDLED;
961793fc096SFrank Li 
962793fc096SFrank Li 			/* Disable the RX interrupt */
963793fc096SFrank Li 			if (napi_schedule_prep(&fep->napi)) {
964793fc096SFrank Li 				writel(FEC_RX_DISABLED_IMASK,
965793fc096SFrank Li 					fep->hwp + FEC_IMASK);
966793fc096SFrank Li 				__napi_schedule(&fep->napi);
967793fc096SFrank Li 			}
968793fc096SFrank Li 		}
969793fc096SFrank Li 
970793fc096SFrank Li 		if (int_events & FEC_ENET_MII) {
971793fc096SFrank Li 			ret = IRQ_HANDLED;
972793fc096SFrank Li 			complete(&fep->mdio_done);
973793fc096SFrank Li 		}
974793fc096SFrank Li 	} while (int_events);
975793fc096SFrank Li 
976793fc096SFrank Li 	return ret;
977793fc096SFrank Li }
978793fc096SFrank Li 
979793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
980793fc096SFrank Li {
981793fc096SFrank Li 	struct net_device *ndev = napi->dev;
982793fc096SFrank Li 	int pkts = fec_enet_rx(ndev, budget);
983793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
984793fc096SFrank Li 
985793fc096SFrank Li 	fec_enet_tx(ndev);
986793fc096SFrank Li 
987793fc096SFrank Li 	if (pkts < budget) {
988793fc096SFrank Li 		napi_complete(napi);
989793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
990793fc096SFrank Li 	}
991793fc096SFrank Li 	return pkts;
992793fc096SFrank Li }
993793fc096SFrank Li 
994793fc096SFrank Li /* ------------------------------------------------------------------------- */
995793fc096SFrank Li static void fec_get_mac(struct net_device *ndev)
996793fc096SFrank Li {
997793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
998793fc096SFrank Li 	struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
999793fc096SFrank Li 	unsigned char *iap, tmpaddr[ETH_ALEN];
1000793fc096SFrank Li 
1001793fc096SFrank Li 	/*
1002793fc096SFrank Li 	 * try to get mac address in following order:
1003793fc096SFrank Li 	 *
1004793fc096SFrank Li 	 * 1) module parameter via kernel command line in form
1005793fc096SFrank Li 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1006793fc096SFrank Li 	 */
1007793fc096SFrank Li 	iap = macaddr;
1008793fc096SFrank Li 
1009793fc096SFrank Li 	/*
1010793fc096SFrank Li 	 * 2) from device tree data
1011793fc096SFrank Li 	 */
1012793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1013793fc096SFrank Li 		struct device_node *np = fep->pdev->dev.of_node;
1014793fc096SFrank Li 		if (np) {
1015793fc096SFrank Li 			const char *mac = of_get_mac_address(np);
1016793fc096SFrank Li 			if (mac)
1017793fc096SFrank Li 				iap = (unsigned char *) mac;
1018793fc096SFrank Li 		}
1019793fc096SFrank Li 	}
1020793fc096SFrank Li 
1021793fc096SFrank Li 	/*
1022793fc096SFrank Li 	 * 3) from flash or fuse (via platform data)
1023793fc096SFrank Li 	 */
1024793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1025793fc096SFrank Li #ifdef CONFIG_M5272
1026793fc096SFrank Li 		if (FEC_FLASHMAC)
1027793fc096SFrank Li 			iap = (unsigned char *)FEC_FLASHMAC;
1028793fc096SFrank Li #else
1029793fc096SFrank Li 		if (pdata)
1030793fc096SFrank Li 			iap = (unsigned char *)&pdata->mac;
1031793fc096SFrank Li #endif
1032793fc096SFrank Li 	}
1033793fc096SFrank Li 
1034793fc096SFrank Li 	/*
1035793fc096SFrank Li 	 * 4) FEC mac registers set by bootloader
1036793fc096SFrank Li 	 */
1037793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1038793fc096SFrank Li 		*((unsigned long *) &tmpaddr[0]) =
1039793fc096SFrank Li 			be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
1040793fc096SFrank Li 		*((unsigned short *) &tmpaddr[4]) =
1041793fc096SFrank Li 			be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1042793fc096SFrank Li 		iap = &tmpaddr[0];
1043793fc096SFrank Li 	}
1044793fc096SFrank Li 
1045ff5b2fabSLucas Stach 	/*
1046ff5b2fabSLucas Stach 	 * 5) random mac address
1047ff5b2fabSLucas Stach 	 */
1048ff5b2fabSLucas Stach 	if (!is_valid_ether_addr(iap)) {
1049ff5b2fabSLucas Stach 		/* Report it and use a random ethernet address instead */
1050ff5b2fabSLucas Stach 		netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1051ff5b2fabSLucas Stach 		eth_hw_addr_random(ndev);
1052ff5b2fabSLucas Stach 		netdev_info(ndev, "Using random MAC address: %pM\n",
1053ff5b2fabSLucas Stach 			    ndev->dev_addr);
1054ff5b2fabSLucas Stach 		return;
1055ff5b2fabSLucas Stach 	}
1056ff5b2fabSLucas Stach 
1057793fc096SFrank Li 	memcpy(ndev->dev_addr, iap, ETH_ALEN);
1058793fc096SFrank Li 
1059793fc096SFrank Li 	/* Adjust MAC if using macaddr */
1060793fc096SFrank Li 	if (iap == macaddr)
1061793fc096SFrank Li 		 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1062793fc096SFrank Li }
1063793fc096SFrank Li 
1064793fc096SFrank Li /* ------------------------------------------------------------------------- */
1065793fc096SFrank Li 
1066793fc096SFrank Li /*
1067793fc096SFrank Li  * Phy section
1068793fc096SFrank Li  */
1069793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev)
1070793fc096SFrank Li {
1071793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1072793fc096SFrank Li 	struct phy_device *phy_dev = fep->phy_dev;
1073793fc096SFrank Li 	int status_change = 0;
1074793fc096SFrank Li 
1075793fc096SFrank Li 	/* Prevent a state halted on mii error */
1076793fc096SFrank Li 	if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1077793fc096SFrank Li 		phy_dev->state = PHY_RESUMING;
107854309fa6SFrank Li 		return;
1079793fc096SFrank Li 	}
1080793fc096SFrank Li 
1081793fc096SFrank Li 	if (phy_dev->link) {
1082793fc096SFrank Li 		if (!fep->link) {
1083793fc096SFrank Li 			fep->link = phy_dev->link;
1084793fc096SFrank Li 			status_change = 1;
1085793fc096SFrank Li 		}
1086793fc096SFrank Li 
1087793fc096SFrank Li 		if (fep->full_duplex != phy_dev->duplex)
1088793fc096SFrank Li 			status_change = 1;
1089793fc096SFrank Li 
1090793fc096SFrank Li 		if (phy_dev->speed != fep->speed) {
1091793fc096SFrank Li 			fep->speed = phy_dev->speed;
1092793fc096SFrank Li 			status_change = 1;
1093793fc096SFrank Li 		}
1094793fc096SFrank Li 
1095793fc096SFrank Li 		/* if any of the above changed restart the FEC */
1096793fc096SFrank Li 		if (status_change)
1097793fc096SFrank Li 			fec_restart(ndev, phy_dev->duplex);
1098793fc096SFrank Li 	} else {
1099793fc096SFrank Li 		if (fep->link) {
1100793fc096SFrank Li 			fec_stop(ndev);
11016e0895c2SDavid S. Miller 			fep->link = phy_dev->link;
1102793fc096SFrank Li 			status_change = 1;
1103793fc096SFrank Li 		}
1104793fc096SFrank Li 	}
1105793fc096SFrank Li 
1106793fc096SFrank Li 	if (status_change)
1107793fc096SFrank Li 		phy_print_status(phy_dev);
1108793fc096SFrank Li }
1109793fc096SFrank Li 
1110793fc096SFrank Li static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1111793fc096SFrank Li {
1112793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
1113793fc096SFrank Li 	unsigned long time_left;
1114793fc096SFrank Li 
1115793fc096SFrank Li 	fep->mii_timeout = 0;
1116793fc096SFrank Li 	init_completion(&fep->mdio_done);
1117793fc096SFrank Li 
1118793fc096SFrank Li 	/* start a read op */
1119793fc096SFrank Li 	writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1120793fc096SFrank Li 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1121793fc096SFrank Li 		FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1122793fc096SFrank Li 
1123793fc096SFrank Li 	/* wait for end of transfer */
1124793fc096SFrank Li 	time_left = wait_for_completion_timeout(&fep->mdio_done,
1125793fc096SFrank Li 			usecs_to_jiffies(FEC_MII_TIMEOUT));
1126793fc096SFrank Li 	if (time_left == 0) {
1127793fc096SFrank Li 		fep->mii_timeout = 1;
112831b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO read timeout\n");
1129793fc096SFrank Li 		return -ETIMEDOUT;
1130793fc096SFrank Li 	}
1131793fc096SFrank Li 
1132793fc096SFrank Li 	/* return value */
1133793fc096SFrank Li 	return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1134793fc096SFrank Li }
1135793fc096SFrank Li 
1136793fc096SFrank Li static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1137793fc096SFrank Li 			   u16 value)
1138793fc096SFrank Li {
1139793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
1140793fc096SFrank Li 	unsigned long time_left;
1141793fc096SFrank Li 
1142793fc096SFrank Li 	fep->mii_timeout = 0;
1143793fc096SFrank Li 	init_completion(&fep->mdio_done);
1144793fc096SFrank Li 
1145793fc096SFrank Li 	/* start a write op */
1146793fc096SFrank Li 	writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1147793fc096SFrank Li 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1148793fc096SFrank Li 		FEC_MMFR_TA | FEC_MMFR_DATA(value),
1149793fc096SFrank Li 		fep->hwp + FEC_MII_DATA);
1150793fc096SFrank Li 
1151793fc096SFrank Li 	/* wait for end of transfer */
1152793fc096SFrank Li 	time_left = wait_for_completion_timeout(&fep->mdio_done,
1153793fc096SFrank Li 			usecs_to_jiffies(FEC_MII_TIMEOUT));
1154793fc096SFrank Li 	if (time_left == 0) {
1155793fc096SFrank Li 		fep->mii_timeout = 1;
115631b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO write timeout\n");
1157793fc096SFrank Li 		return -ETIMEDOUT;
1158793fc096SFrank Li 	}
1159793fc096SFrank Li 
1160793fc096SFrank Li 	return 0;
1161793fc096SFrank Li }
1162793fc096SFrank Li 
1163793fc096SFrank Li static int fec_enet_mdio_reset(struct mii_bus *bus)
1164793fc096SFrank Li {
1165793fc096SFrank Li 	return 0;
1166793fc096SFrank Li }
1167793fc096SFrank Li 
1168793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev)
1169793fc096SFrank Li {
1170793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1171793fc096SFrank Li 	const struct platform_device_id *id_entry =
1172793fc096SFrank Li 				platform_get_device_id(fep->pdev);
1173793fc096SFrank Li 	struct phy_device *phy_dev = NULL;
1174793fc096SFrank Li 	char mdio_bus_id[MII_BUS_ID_SIZE];
1175793fc096SFrank Li 	char phy_name[MII_BUS_ID_SIZE + 3];
1176793fc096SFrank Li 	int phy_id;
1177793fc096SFrank Li 	int dev_id = fep->dev_id;
1178793fc096SFrank Li 
1179793fc096SFrank Li 	fep->phy_dev = NULL;
1180793fc096SFrank Li 
1181793fc096SFrank Li 	/* check for attached phy */
1182793fc096SFrank Li 	for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1183793fc096SFrank Li 		if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1184793fc096SFrank Li 			continue;
1185793fc096SFrank Li 		if (fep->mii_bus->phy_map[phy_id] == NULL)
1186793fc096SFrank Li 			continue;
1187793fc096SFrank Li 		if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1188793fc096SFrank Li 			continue;
1189793fc096SFrank Li 		if (dev_id--)
1190793fc096SFrank Li 			continue;
1191793fc096SFrank Li 		strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1192793fc096SFrank Li 		break;
1193793fc096SFrank Li 	}
1194793fc096SFrank Li 
1195793fc096SFrank Li 	if (phy_id >= PHY_MAX_ADDR) {
119631b7720cSJoe Perches 		netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1197793fc096SFrank Li 		strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1198793fc096SFrank Li 		phy_id = 0;
1199793fc096SFrank Li 	}
1200793fc096SFrank Li 
1201793fc096SFrank Li 	snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
1202793fc096SFrank Li 	phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1203793fc096SFrank Li 			      fep->phy_interface);
1204793fc096SFrank Li 	if (IS_ERR(phy_dev)) {
120531b7720cSJoe Perches 		netdev_err(ndev, "could not attach to PHY\n");
1206793fc096SFrank Li 		return PTR_ERR(phy_dev);
1207793fc096SFrank Li 	}
1208793fc096SFrank Li 
1209793fc096SFrank Li 	/* mask with MAC supported features */
1210793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
1211793fc096SFrank Li 		phy_dev->supported &= PHY_GBIT_FEATURES;
1212*d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1213793fc096SFrank Li 		phy_dev->supported |= SUPPORTED_Pause;
1214*d1391930SGuenter Roeck #endif
1215793fc096SFrank Li 	}
1216793fc096SFrank Li 	else
1217793fc096SFrank Li 		phy_dev->supported &= PHY_BASIC_FEATURES;
1218793fc096SFrank Li 
1219793fc096SFrank Li 	phy_dev->advertising = phy_dev->supported;
1220793fc096SFrank Li 
1221793fc096SFrank Li 	fep->phy_dev = phy_dev;
1222793fc096SFrank Li 	fep->link = 0;
1223793fc096SFrank Li 	fep->full_duplex = 0;
1224793fc096SFrank Li 
122531b7720cSJoe Perches 	netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1226793fc096SFrank Li 		    fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1227793fc096SFrank Li 		    fep->phy_dev->irq);
1228793fc096SFrank Li 
1229793fc096SFrank Li 	return 0;
1230793fc096SFrank Li }
1231793fc096SFrank Li 
1232793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev)
1233793fc096SFrank Li {
1234793fc096SFrank Li 	static struct mii_bus *fec0_mii_bus;
1235793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
1236793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1237793fc096SFrank Li 	const struct platform_device_id *id_entry =
1238793fc096SFrank Li 				platform_get_device_id(fep->pdev);
1239793fc096SFrank Li 	int err = -ENXIO, i;
1240793fc096SFrank Li 
1241793fc096SFrank Li 	/*
1242793fc096SFrank Li 	 * The dual fec interfaces are not equivalent with enet-mac.
1243793fc096SFrank Li 	 * Here are the differences:
1244793fc096SFrank Li 	 *
1245793fc096SFrank Li 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
1246793fc096SFrank Li 	 *  - fec0 acts as the 1588 time master while fec1 is slave
1247793fc096SFrank Li 	 *  - external phys can only be configured by fec0
1248793fc096SFrank Li 	 *
1249793fc096SFrank Li 	 * That is to say fec1 can not work independently. It only works
1250793fc096SFrank Li 	 * when fec0 is working. The reason behind this design is that the
1251793fc096SFrank Li 	 * second interface is added primarily for Switch mode.
1252793fc096SFrank Li 	 *
1253793fc096SFrank Li 	 * Because of the last point above, both phys are attached on fec0
1254793fc096SFrank Li 	 * mdio interface in board design, and need to be configured by
1255793fc096SFrank Li 	 * fec0 mii_bus.
1256793fc096SFrank Li 	 */
1257793fc096SFrank Li 	if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
1258793fc096SFrank Li 		/* fec1 uses fec0 mii_bus */
1259793fc096SFrank Li 		if (mii_cnt && fec0_mii_bus) {
1260793fc096SFrank Li 			fep->mii_bus = fec0_mii_bus;
1261793fc096SFrank Li 			mii_cnt++;
1262793fc096SFrank Li 			return 0;
1263793fc096SFrank Li 		}
1264793fc096SFrank Li 		return -ENOENT;
1265793fc096SFrank Li 	}
1266793fc096SFrank Li 
1267793fc096SFrank Li 	fep->mii_timeout = 0;
1268793fc096SFrank Li 
1269793fc096SFrank Li 	/*
1270793fc096SFrank Li 	 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1271793fc096SFrank Li 	 *
1272793fc096SFrank Li 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1273793fc096SFrank Li 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
1274793fc096SFrank Li 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1275793fc096SFrank Li 	 * document.
1276793fc096SFrank Li 	 */
1277793fc096SFrank Li 	fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
1278793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1279793fc096SFrank Li 		fep->phy_speed--;
1280793fc096SFrank Li 	fep->phy_speed <<= 1;
1281793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1282793fc096SFrank Li 
1283793fc096SFrank Li 	fep->mii_bus = mdiobus_alloc();
1284793fc096SFrank Li 	if (fep->mii_bus == NULL) {
1285793fc096SFrank Li 		err = -ENOMEM;
1286793fc096SFrank Li 		goto err_out;
1287793fc096SFrank Li 	}
1288793fc096SFrank Li 
1289793fc096SFrank Li 	fep->mii_bus->name = "fec_enet_mii_bus";
1290793fc096SFrank Li 	fep->mii_bus->read = fec_enet_mdio_read;
1291793fc096SFrank Li 	fep->mii_bus->write = fec_enet_mdio_write;
1292793fc096SFrank Li 	fep->mii_bus->reset = fec_enet_mdio_reset;
1293793fc096SFrank Li 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1294793fc096SFrank Li 		pdev->name, fep->dev_id + 1);
1295793fc096SFrank Li 	fep->mii_bus->priv = fep;
1296793fc096SFrank Li 	fep->mii_bus->parent = &pdev->dev;
1297793fc096SFrank Li 
1298793fc096SFrank Li 	fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1299793fc096SFrank Li 	if (!fep->mii_bus->irq) {
1300793fc096SFrank Li 		err = -ENOMEM;
1301793fc096SFrank Li 		goto err_out_free_mdiobus;
1302793fc096SFrank Li 	}
1303793fc096SFrank Li 
1304793fc096SFrank Li 	for (i = 0; i < PHY_MAX_ADDR; i++)
1305793fc096SFrank Li 		fep->mii_bus->irq[i] = PHY_POLL;
1306793fc096SFrank Li 
1307793fc096SFrank Li 	if (mdiobus_register(fep->mii_bus))
1308793fc096SFrank Li 		goto err_out_free_mdio_irq;
1309793fc096SFrank Li 
1310793fc096SFrank Li 	mii_cnt++;
1311793fc096SFrank Li 
1312793fc096SFrank Li 	/* save fec0 mii_bus */
1313793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1314793fc096SFrank Li 		fec0_mii_bus = fep->mii_bus;
1315793fc096SFrank Li 
1316793fc096SFrank Li 	return 0;
1317793fc096SFrank Li 
1318793fc096SFrank Li err_out_free_mdio_irq:
1319793fc096SFrank Li 	kfree(fep->mii_bus->irq);
1320793fc096SFrank Li err_out_free_mdiobus:
1321793fc096SFrank Li 	mdiobus_free(fep->mii_bus);
1322793fc096SFrank Li err_out:
1323793fc096SFrank Li 	return err;
1324793fc096SFrank Li }
1325793fc096SFrank Li 
1326793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep)
1327793fc096SFrank Li {
1328793fc096SFrank Li 	if (--mii_cnt == 0) {
1329793fc096SFrank Li 		mdiobus_unregister(fep->mii_bus);
1330793fc096SFrank Li 		kfree(fep->mii_bus->irq);
1331793fc096SFrank Li 		mdiobus_free(fep->mii_bus);
1332793fc096SFrank Li 	}
1333793fc096SFrank Li }
1334793fc096SFrank Li 
1335793fc096SFrank Li static int fec_enet_get_settings(struct net_device *ndev,
1336793fc096SFrank Li 				  struct ethtool_cmd *cmd)
1337793fc096SFrank Li {
1338793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1339793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
1340793fc096SFrank Li 
1341793fc096SFrank Li 	if (!phydev)
1342793fc096SFrank Li 		return -ENODEV;
1343793fc096SFrank Li 
1344793fc096SFrank Li 	return phy_ethtool_gset(phydev, cmd);
1345793fc096SFrank Li }
1346793fc096SFrank Li 
1347793fc096SFrank Li static int fec_enet_set_settings(struct net_device *ndev,
1348793fc096SFrank Li 				 struct ethtool_cmd *cmd)
1349793fc096SFrank Li {
1350793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1351793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
1352793fc096SFrank Li 
1353793fc096SFrank Li 	if (!phydev)
1354793fc096SFrank Li 		return -ENODEV;
1355793fc096SFrank Li 
1356793fc096SFrank Li 	return phy_ethtool_sset(phydev, cmd);
1357793fc096SFrank Li }
1358793fc096SFrank Li 
1359793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev,
1360793fc096SFrank Li 				 struct ethtool_drvinfo *info)
1361793fc096SFrank Li {
1362793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1363793fc096SFrank Li 
1364793fc096SFrank Li 	strlcpy(info->driver, fep->pdev->dev.driver->name,
1365793fc096SFrank Li 		sizeof(info->driver));
1366793fc096SFrank Li 	strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1367793fc096SFrank Li 	strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
1368793fc096SFrank Li }
1369793fc096SFrank Li 
1370793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev,
1371793fc096SFrank Li 				struct ethtool_ts_info *info)
1372793fc096SFrank Li {
1373793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1374793fc096SFrank Li 
1375793fc096SFrank Li 	if (fep->bufdesc_ex) {
1376793fc096SFrank Li 
1377793fc096SFrank Li 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1378793fc096SFrank Li 					SOF_TIMESTAMPING_RX_SOFTWARE |
1379793fc096SFrank Li 					SOF_TIMESTAMPING_SOFTWARE |
1380793fc096SFrank Li 					SOF_TIMESTAMPING_TX_HARDWARE |
1381793fc096SFrank Li 					SOF_TIMESTAMPING_RX_HARDWARE |
1382793fc096SFrank Li 					SOF_TIMESTAMPING_RAW_HARDWARE;
1383793fc096SFrank Li 		if (fep->ptp_clock)
1384793fc096SFrank Li 			info->phc_index = ptp_clock_index(fep->ptp_clock);
1385793fc096SFrank Li 		else
1386793fc096SFrank Li 			info->phc_index = -1;
1387793fc096SFrank Li 
1388793fc096SFrank Li 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1389793fc096SFrank Li 				 (1 << HWTSTAMP_TX_ON);
1390793fc096SFrank Li 
1391793fc096SFrank Li 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1392793fc096SFrank Li 				   (1 << HWTSTAMP_FILTER_ALL);
1393793fc096SFrank Li 		return 0;
1394793fc096SFrank Li 	} else {
1395793fc096SFrank Li 		return ethtool_op_get_ts_info(ndev, info);
1396793fc096SFrank Li 	}
1397793fc096SFrank Li }
1398793fc096SFrank Li 
1399*d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1400*d1391930SGuenter Roeck 
1401793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev,
1402793fc096SFrank Li 				    struct ethtool_pauseparam *pause)
1403793fc096SFrank Li {
1404793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1405793fc096SFrank Li 
1406793fc096SFrank Li 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1407793fc096SFrank Li 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1408793fc096SFrank Li 	pause->rx_pause = pause->tx_pause;
1409793fc096SFrank Li }
1410793fc096SFrank Li 
1411793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev,
1412793fc096SFrank Li 				   struct ethtool_pauseparam *pause)
1413793fc096SFrank Li {
1414793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1415793fc096SFrank Li 
1416793fc096SFrank Li 	if (pause->tx_pause != pause->rx_pause) {
1417793fc096SFrank Li 		netdev_info(ndev,
1418793fc096SFrank Li 			"hardware only support enable/disable both tx and rx");
1419793fc096SFrank Li 		return -EINVAL;
1420793fc096SFrank Li 	}
1421793fc096SFrank Li 
1422793fc096SFrank Li 	fep->pause_flag = 0;
1423793fc096SFrank Li 
1424793fc096SFrank Li 	/* tx pause must be same as rx pause */
1425793fc096SFrank Li 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1426793fc096SFrank Li 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1427793fc096SFrank Li 
1428793fc096SFrank Li 	if (pause->rx_pause || pause->autoneg) {
1429793fc096SFrank Li 		fep->phy_dev->supported |= ADVERTISED_Pause;
1430793fc096SFrank Li 		fep->phy_dev->advertising |= ADVERTISED_Pause;
1431793fc096SFrank Li 	} else {
1432793fc096SFrank Li 		fep->phy_dev->supported &= ~ADVERTISED_Pause;
1433793fc096SFrank Li 		fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1434793fc096SFrank Li 	}
1435793fc096SFrank Li 
1436793fc096SFrank Li 	if (pause->autoneg) {
1437793fc096SFrank Li 		if (netif_running(ndev))
1438793fc096SFrank Li 			fec_stop(ndev);
1439793fc096SFrank Li 		phy_start_aneg(fep->phy_dev);
1440793fc096SFrank Li 	}
1441793fc096SFrank Li 	if (netif_running(ndev))
1442793fc096SFrank Li 		fec_restart(ndev, 0);
1443793fc096SFrank Li 
1444793fc096SFrank Li 	return 0;
1445793fc096SFrank Li }
1446793fc096SFrank Li 
1447*d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
1448*d1391930SGuenter Roeck 
1449793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = {
1450*d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1451793fc096SFrank Li 	.get_pauseparam		= fec_enet_get_pauseparam,
1452793fc096SFrank Li 	.set_pauseparam		= fec_enet_set_pauseparam,
1453*d1391930SGuenter Roeck #endif
1454793fc096SFrank Li 	.get_settings		= fec_enet_get_settings,
1455793fc096SFrank Li 	.set_settings		= fec_enet_set_settings,
1456793fc096SFrank Li 	.get_drvinfo		= fec_enet_get_drvinfo,
1457793fc096SFrank Li 	.get_link		= ethtool_op_get_link,
1458793fc096SFrank Li 	.get_ts_info		= fec_enet_get_ts_info,
1459793fc096SFrank Li };
1460793fc096SFrank Li 
1461793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1462793fc096SFrank Li {
1463793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1464793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
1465793fc096SFrank Li 
1466793fc096SFrank Li 	if (!netif_running(ndev))
1467793fc096SFrank Li 		return -EINVAL;
1468793fc096SFrank Li 
1469793fc096SFrank Li 	if (!phydev)
1470793fc096SFrank Li 		return -ENODEV;
1471793fc096SFrank Li 
1472793fc096SFrank Li 	if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
1473793fc096SFrank Li 		return fec_ptp_ioctl(ndev, rq, cmd);
1474793fc096SFrank Li 
1475793fc096SFrank Li 	return phy_mii_ioctl(phydev, rq, cmd);
1476793fc096SFrank Li }
1477793fc096SFrank Li 
1478793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev)
1479793fc096SFrank Li {
1480793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1481793fc096SFrank Li 	unsigned int i;
1482793fc096SFrank Li 	struct sk_buff *skb;
1483793fc096SFrank Li 	struct bufdesc	*bdp;
1484793fc096SFrank Li 
1485793fc096SFrank Li 	bdp = fep->rx_bd_base;
1486793fc096SFrank Li 	for (i = 0; i < RX_RING_SIZE; i++) {
1487793fc096SFrank Li 		skb = fep->rx_skbuff[i];
1488793fc096SFrank Li 
1489793fc096SFrank Li 		if (bdp->cbd_bufaddr)
1490793fc096SFrank Li 			dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1491793fc096SFrank Li 					FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1492793fc096SFrank Li 		if (skb)
1493793fc096SFrank Li 			dev_kfree_skb(skb);
1494793fc096SFrank Li 		bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
1495793fc096SFrank Li 	}
1496793fc096SFrank Li 
1497793fc096SFrank Li 	bdp = fep->tx_bd_base;
1498793fc096SFrank Li 	for (i = 0; i < TX_RING_SIZE; i++)
1499793fc096SFrank Li 		kfree(fep->tx_bounce[i]);
1500793fc096SFrank Li }
1501793fc096SFrank Li 
1502793fc096SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev)
1503793fc096SFrank Li {
1504793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1505793fc096SFrank Li 	unsigned int i;
1506793fc096SFrank Li 	struct sk_buff *skb;
1507793fc096SFrank Li 	struct bufdesc	*bdp;
1508793fc096SFrank Li 
1509793fc096SFrank Li 	bdp = fep->rx_bd_base;
1510793fc096SFrank Li 	for (i = 0; i < RX_RING_SIZE; i++) {
1511793fc096SFrank Li 		skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1512793fc096SFrank Li 		if (!skb) {
1513793fc096SFrank Li 			fec_enet_free_buffers(ndev);
1514793fc096SFrank Li 			return -ENOMEM;
1515793fc096SFrank Li 		}
1516793fc096SFrank Li 		fep->rx_skbuff[i] = skb;
1517793fc096SFrank Li 
1518793fc096SFrank Li 		bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1519793fc096SFrank Li 				FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1520793fc096SFrank Li 		bdp->cbd_sc = BD_ENET_RX_EMPTY;
1521793fc096SFrank Li 
1522793fc096SFrank Li 		if (fep->bufdesc_ex) {
1523793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1524793fc096SFrank Li 			ebdp->cbd_esc = BD_ENET_RX_INT;
1525793fc096SFrank Li 		}
1526793fc096SFrank Li 
1527793fc096SFrank Li 		bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
1528793fc096SFrank Li 	}
1529793fc096SFrank Li 
1530793fc096SFrank Li 	/* Set the last buffer to wrap. */
1531793fc096SFrank Li 	bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
1532793fc096SFrank Li 	bdp->cbd_sc |= BD_SC_WRAP;
1533793fc096SFrank Li 
1534793fc096SFrank Li 	bdp = fep->tx_bd_base;
1535793fc096SFrank Li 	for (i = 0; i < TX_RING_SIZE; i++) {
1536793fc096SFrank Li 		fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1537793fc096SFrank Li 
1538793fc096SFrank Li 		bdp->cbd_sc = 0;
1539793fc096SFrank Li 		bdp->cbd_bufaddr = 0;
1540793fc096SFrank Li 
1541793fc096SFrank Li 		if (fep->bufdesc_ex) {
1542793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
154396d2222bSJim Baxter 			ebdp->cbd_esc = BD_ENET_TX_INT;
1544793fc096SFrank Li 		}
1545793fc096SFrank Li 
1546793fc096SFrank Li 		bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
1547793fc096SFrank Li 	}
1548793fc096SFrank Li 
1549793fc096SFrank Li 	/* Set the last buffer to wrap. */
1550793fc096SFrank Li 	bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
1551793fc096SFrank Li 	bdp->cbd_sc |= BD_SC_WRAP;
1552793fc096SFrank Li 
1553793fc096SFrank Li 	return 0;
1554793fc096SFrank Li }
1555793fc096SFrank Li 
1556793fc096SFrank Li static int
1557793fc096SFrank Li fec_enet_open(struct net_device *ndev)
1558793fc096SFrank Li {
1559793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1560793fc096SFrank Li 	int ret;
1561793fc096SFrank Li 
1562793fc096SFrank Li 	napi_enable(&fep->napi);
1563793fc096SFrank Li 
1564793fc096SFrank Li 	/* I should reset the ring buffers here, but I don't yet know
1565793fc096SFrank Li 	 * a simple way to do that.
1566793fc096SFrank Li 	 */
1567793fc096SFrank Li 
1568793fc096SFrank Li 	ret = fec_enet_alloc_buffers(ndev);
1569793fc096SFrank Li 	if (ret)
1570793fc096SFrank Li 		return ret;
1571793fc096SFrank Li 
1572793fc096SFrank Li 	/* Probe and connect to PHY when open the interface */
1573793fc096SFrank Li 	ret = fec_enet_mii_probe(ndev);
1574793fc096SFrank Li 	if (ret) {
1575793fc096SFrank Li 		fec_enet_free_buffers(ndev);
1576793fc096SFrank Li 		return ret;
1577793fc096SFrank Li 	}
1578793fc096SFrank Li 	phy_start(fep->phy_dev);
1579793fc096SFrank Li 	netif_start_queue(ndev);
1580793fc096SFrank Li 	fep->opened = 1;
1581793fc096SFrank Li 	return 0;
1582793fc096SFrank Li }
1583793fc096SFrank Li 
1584793fc096SFrank Li static int
1585793fc096SFrank Li fec_enet_close(struct net_device *ndev)
1586793fc096SFrank Li {
1587793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1588793fc096SFrank Li 
1589793fc096SFrank Li 	/* Don't know what to do yet. */
1590793fc096SFrank Li 	napi_disable(&fep->napi);
1591793fc096SFrank Li 	fep->opened = 0;
1592793fc096SFrank Li 	netif_stop_queue(ndev);
1593793fc096SFrank Li 	fec_stop(ndev);
1594793fc096SFrank Li 
1595793fc096SFrank Li 	if (fep->phy_dev) {
1596793fc096SFrank Li 		phy_stop(fep->phy_dev);
1597793fc096SFrank Li 		phy_disconnect(fep->phy_dev);
1598793fc096SFrank Li 	}
1599793fc096SFrank Li 
1600793fc096SFrank Li 	fec_enet_free_buffers(ndev);
1601793fc096SFrank Li 
1602793fc096SFrank Li 	return 0;
1603793fc096SFrank Li }
1604793fc096SFrank Li 
1605793fc096SFrank Li /* Set or clear the multicast filter for this adaptor.
1606793fc096SFrank Li  * Skeleton taken from sunlance driver.
1607793fc096SFrank Li  * The CPM Ethernet implementation allows Multicast as well as individual
1608793fc096SFrank Li  * MAC address filtering.  Some of the drivers check to make sure it is
1609793fc096SFrank Li  * a group multicast address, and discard those that are not.  I guess I
1610793fc096SFrank Li  * will do the same for now, but just remove the test if you want
1611793fc096SFrank Li  * individual filtering as well (do the upper net layers want or support
1612793fc096SFrank Li  * this kind of feature?).
1613793fc096SFrank Li  */
1614793fc096SFrank Li 
1615793fc096SFrank Li #define HASH_BITS	6		/* #bits in hash */
1616793fc096SFrank Li #define CRC32_POLY	0xEDB88320
1617793fc096SFrank Li 
1618793fc096SFrank Li static void set_multicast_list(struct net_device *ndev)
1619793fc096SFrank Li {
1620793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1621793fc096SFrank Li 	struct netdev_hw_addr *ha;
1622793fc096SFrank Li 	unsigned int i, bit, data, crc, tmp;
1623793fc096SFrank Li 	unsigned char hash;
1624793fc096SFrank Li 
1625793fc096SFrank Li 	if (ndev->flags & IFF_PROMISC) {
1626793fc096SFrank Li 		tmp = readl(fep->hwp + FEC_R_CNTRL);
1627793fc096SFrank Li 		tmp |= 0x8;
1628793fc096SFrank Li 		writel(tmp, fep->hwp + FEC_R_CNTRL);
1629793fc096SFrank Li 		return;
1630793fc096SFrank Li 	}
1631793fc096SFrank Li 
1632793fc096SFrank Li 	tmp = readl(fep->hwp + FEC_R_CNTRL);
1633793fc096SFrank Li 	tmp &= ~0x8;
1634793fc096SFrank Li 	writel(tmp, fep->hwp + FEC_R_CNTRL);
1635793fc096SFrank Li 
1636793fc096SFrank Li 	if (ndev->flags & IFF_ALLMULTI) {
1637793fc096SFrank Li 		/* Catch all multicast addresses, so set the
1638793fc096SFrank Li 		 * filter to all 1's
1639793fc096SFrank Li 		 */
1640793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1641793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1642793fc096SFrank Li 
1643793fc096SFrank Li 		return;
1644793fc096SFrank Li 	}
1645793fc096SFrank Li 
1646793fc096SFrank Li 	/* Clear filter and add the addresses in hash register
1647793fc096SFrank Li 	 */
1648793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1649793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1650793fc096SFrank Li 
1651793fc096SFrank Li 	netdev_for_each_mc_addr(ha, ndev) {
1652793fc096SFrank Li 		/* calculate crc32 value of mac address */
1653793fc096SFrank Li 		crc = 0xffffffff;
1654793fc096SFrank Li 
1655793fc096SFrank Li 		for (i = 0; i < ndev->addr_len; i++) {
1656793fc096SFrank Li 			data = ha->addr[i];
1657793fc096SFrank Li 			for (bit = 0; bit < 8; bit++, data >>= 1) {
1658793fc096SFrank Li 				crc = (crc >> 1) ^
1659793fc096SFrank Li 				(((crc ^ data) & 1) ? CRC32_POLY : 0);
1660793fc096SFrank Li 			}
1661793fc096SFrank Li 		}
1662793fc096SFrank Li 
1663793fc096SFrank Li 		/* only upper 6 bits (HASH_BITS) are used
1664793fc096SFrank Li 		 * which point to specific bit in he hash registers
1665793fc096SFrank Li 		 */
1666793fc096SFrank Li 		hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1667793fc096SFrank Li 
1668793fc096SFrank Li 		if (hash > 31) {
1669793fc096SFrank Li 			tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1670793fc096SFrank Li 			tmp |= 1 << (hash - 32);
1671793fc096SFrank Li 			writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1672793fc096SFrank Li 		} else {
1673793fc096SFrank Li 			tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1674793fc096SFrank Li 			tmp |= 1 << hash;
1675793fc096SFrank Li 			writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1676793fc096SFrank Li 		}
1677793fc096SFrank Li 	}
1678793fc096SFrank Li }
1679793fc096SFrank Li 
1680793fc096SFrank Li /* Set a MAC change in hardware. */
1681793fc096SFrank Li static int
1682793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p)
1683793fc096SFrank Li {
1684793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1685793fc096SFrank Li 	struct sockaddr *addr = p;
1686793fc096SFrank Li 
1687793fc096SFrank Li 	if (!is_valid_ether_addr(addr->sa_data))
1688793fc096SFrank Li 		return -EADDRNOTAVAIL;
1689793fc096SFrank Li 
1690793fc096SFrank Li 	memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
1691793fc096SFrank Li 
1692793fc096SFrank Li 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1693793fc096SFrank Li 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
1694793fc096SFrank Li 		fep->hwp + FEC_ADDR_LOW);
1695793fc096SFrank Li 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
1696793fc096SFrank Li 		fep->hwp + FEC_ADDR_HIGH);
1697793fc096SFrank Li 	return 0;
1698793fc096SFrank Li }
1699793fc096SFrank Li 
1700793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
1701793fc096SFrank Li /**
1702793fc096SFrank Li  * fec_poll_controller - FEC Poll controller function
1703793fc096SFrank Li  * @dev: The FEC network adapter
1704793fc096SFrank Li  *
1705793fc096SFrank Li  * Polled functionality used by netconsole and others in non interrupt mode
1706793fc096SFrank Li  *
1707793fc096SFrank Li  */
1708793fc096SFrank Li static void fec_poll_controller(struct net_device *dev)
1709793fc096SFrank Li {
1710793fc096SFrank Li 	int i;
1711793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(dev);
1712793fc096SFrank Li 
1713793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
1714793fc096SFrank Li 		if (fep->irq[i] > 0) {
1715793fc096SFrank Li 			disable_irq(fep->irq[i]);
1716793fc096SFrank Li 			fec_enet_interrupt(fep->irq[i], dev);
1717793fc096SFrank Li 			enable_irq(fep->irq[i]);
1718793fc096SFrank Li 		}
1719793fc096SFrank Li 	}
1720793fc096SFrank Li }
1721793fc096SFrank Li #endif
1722793fc096SFrank Li 
17234c09eed9SJim Baxter static int fec_set_features(struct net_device *netdev,
17244c09eed9SJim Baxter 	netdev_features_t features)
17254c09eed9SJim Baxter {
17264c09eed9SJim Baxter 	struct fec_enet_private *fep = netdev_priv(netdev);
17274c09eed9SJim Baxter 	netdev_features_t changed = features ^ netdev->features;
17284c09eed9SJim Baxter 
17294c09eed9SJim Baxter 	netdev->features = features;
17304c09eed9SJim Baxter 
17314c09eed9SJim Baxter 	/* Receive checksum has been changed */
17324c09eed9SJim Baxter 	if (changed & NETIF_F_RXCSUM) {
17334c09eed9SJim Baxter 		if (features & NETIF_F_RXCSUM)
17344c09eed9SJim Baxter 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
17354c09eed9SJim Baxter 		else
17364c09eed9SJim Baxter 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
17374c09eed9SJim Baxter 
17384c09eed9SJim Baxter 		if (netif_running(netdev)) {
17394c09eed9SJim Baxter 			fec_stop(netdev);
17404c09eed9SJim Baxter 			fec_restart(netdev, fep->phy_dev->duplex);
17414c09eed9SJim Baxter 			netif_wake_queue(netdev);
17424c09eed9SJim Baxter 		} else {
17434c09eed9SJim Baxter 			fec_restart(netdev, fep->phy_dev->duplex);
17444c09eed9SJim Baxter 		}
17454c09eed9SJim Baxter 	}
17464c09eed9SJim Baxter 
17474c09eed9SJim Baxter 	return 0;
17484c09eed9SJim Baxter }
17494c09eed9SJim Baxter 
1750793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = {
1751793fc096SFrank Li 	.ndo_open		= fec_enet_open,
1752793fc096SFrank Li 	.ndo_stop		= fec_enet_close,
1753793fc096SFrank Li 	.ndo_start_xmit		= fec_enet_start_xmit,
1754793fc096SFrank Li 	.ndo_set_rx_mode	= set_multicast_list,
1755793fc096SFrank Li 	.ndo_change_mtu		= eth_change_mtu,
1756793fc096SFrank Li 	.ndo_validate_addr	= eth_validate_addr,
1757793fc096SFrank Li 	.ndo_tx_timeout		= fec_timeout,
1758793fc096SFrank Li 	.ndo_set_mac_address	= fec_set_mac_address,
1759793fc096SFrank Li 	.ndo_do_ioctl		= fec_enet_ioctl,
1760793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
1761793fc096SFrank Li 	.ndo_poll_controller	= fec_poll_controller,
1762793fc096SFrank Li #endif
17634c09eed9SJim Baxter 	.ndo_set_features	= fec_set_features,
1764793fc096SFrank Li };
1765793fc096SFrank Li 
1766793fc096SFrank Li  /*
1767793fc096SFrank Li   * XXX:  We need to clean up on failure exits here.
1768793fc096SFrank Li   *
1769793fc096SFrank Li   */
1770793fc096SFrank Li static int fec_enet_init(struct net_device *ndev)
1771793fc096SFrank Li {
1772793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
177348496255SShawn Guo 	const struct platform_device_id *id_entry =
177448496255SShawn Guo 				platform_get_device_id(fep->pdev);
1775793fc096SFrank Li 	struct bufdesc *cbd_base;
1776793fc096SFrank Li 
1777793fc096SFrank Li 	/* Allocate memory for buffer descriptors. */
1778793fc096SFrank Li 	cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1779793fc096SFrank Li 				      GFP_KERNEL);
1780793fc096SFrank Li 	if (!cbd_base)
1781793fc096SFrank Li 		return -ENOMEM;
1782793fc096SFrank Li 
1783a210576cSDavid S. Miller 	memset(cbd_base, 0, PAGE_SIZE);
1784793fc096SFrank Li 
1785793fc096SFrank Li 	fep->netdev = ndev;
1786793fc096SFrank Li 
1787793fc096SFrank Li 	/* Get the Ethernet address */
1788793fc096SFrank Li 	fec_get_mac(ndev);
1789793fc096SFrank Li 
1790793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
1791793fc096SFrank Li 	fep->rx_bd_base = cbd_base;
1792793fc096SFrank Li 	if (fep->bufdesc_ex)
1793793fc096SFrank Li 		fep->tx_bd_base = (struct bufdesc *)
1794793fc096SFrank Li 			(((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
1795793fc096SFrank Li 	else
1796793fc096SFrank Li 		fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1797793fc096SFrank Li 
1798793fc096SFrank Li 	/* The FEC Ethernet specific entries in the device structure */
1799793fc096SFrank Li 	ndev->watchdog_timeo = TX_TIMEOUT;
1800793fc096SFrank Li 	ndev->netdev_ops = &fec_netdev_ops;
1801793fc096SFrank Li 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
1802793fc096SFrank Li 
1803793fc096SFrank Li 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
1804793fc096SFrank Li 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
1805793fc096SFrank Li 
180648496255SShawn Guo 	if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
18074c09eed9SJim Baxter 		/* enable hw accelerator */
18084c09eed9SJim Baxter 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
18094c09eed9SJim Baxter 				| NETIF_F_RXCSUM);
18104c09eed9SJim Baxter 		ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
18114c09eed9SJim Baxter 				| NETIF_F_RXCSUM);
18124c09eed9SJim Baxter 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
181348496255SShawn Guo 	}
18144c09eed9SJim Baxter 
1815793fc096SFrank Li 	fec_restart(ndev, 0);
1816793fc096SFrank Li 
1817793fc096SFrank Li 	return 0;
1818793fc096SFrank Li }
1819793fc096SFrank Li 
1820793fc096SFrank Li #ifdef CONFIG_OF
1821793fc096SFrank Li static void fec_reset_phy(struct platform_device *pdev)
1822793fc096SFrank Li {
1823793fc096SFrank Li 	int err, phy_reset;
1824793fc096SFrank Li 	int msec = 1;
1825793fc096SFrank Li 	struct device_node *np = pdev->dev.of_node;
1826793fc096SFrank Li 
1827793fc096SFrank Li 	if (!np)
1828793fc096SFrank Li 		return;
1829793fc096SFrank Li 
1830793fc096SFrank Li 	of_property_read_u32(np, "phy-reset-duration", &msec);
1831793fc096SFrank Li 	/* A sane reset duration should not be longer than 1s */
1832793fc096SFrank Li 	if (msec > 1000)
1833793fc096SFrank Li 		msec = 1;
1834793fc096SFrank Li 
1835793fc096SFrank Li 	phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
1836793fc096SFrank Li 	if (!gpio_is_valid(phy_reset))
1837793fc096SFrank Li 		return;
1838793fc096SFrank Li 
1839793fc096SFrank Li 	err = devm_gpio_request_one(&pdev->dev, phy_reset,
1840793fc096SFrank Li 				    GPIOF_OUT_INIT_LOW, "phy-reset");
1841793fc096SFrank Li 	if (err) {
1842793fc096SFrank Li 		dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
1843793fc096SFrank Li 		return;
1844793fc096SFrank Li 	}
1845793fc096SFrank Li 	msleep(msec);
1846793fc096SFrank Li 	gpio_set_value(phy_reset, 1);
1847793fc096SFrank Li }
1848793fc096SFrank Li #else /* CONFIG_OF */
1849793fc096SFrank Li static void fec_reset_phy(struct platform_device *pdev)
1850793fc096SFrank Li {
1851793fc096SFrank Li 	/*
1852793fc096SFrank Li 	 * In case of platform probe, the reset has been done
1853793fc096SFrank Li 	 * by machine code.
1854793fc096SFrank Li 	 */
1855793fc096SFrank Li }
1856793fc096SFrank Li #endif /* CONFIG_OF */
1857793fc096SFrank Li 
1858793fc096SFrank Li static int
1859793fc096SFrank Li fec_probe(struct platform_device *pdev)
1860793fc096SFrank Li {
1861793fc096SFrank Li 	struct fec_enet_private *fep;
1862793fc096SFrank Li 	struct fec_platform_data *pdata;
1863793fc096SFrank Li 	struct net_device *ndev;
1864793fc096SFrank Li 	int i, irq, ret = 0;
1865793fc096SFrank Li 	struct resource *r;
1866793fc096SFrank Li 	const struct of_device_id *of_id;
1867793fc096SFrank Li 	static int dev_id;
1868793fc096SFrank Li 	struct pinctrl *pinctrl;
1869793fc096SFrank Li 	struct regulator *reg_phy;
1870793fc096SFrank Li 
1871793fc096SFrank Li 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
1872793fc096SFrank Li 	if (of_id)
1873793fc096SFrank Li 		pdev->id_entry = of_id->data;
1874793fc096SFrank Li 
1875793fc096SFrank Li 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1876793fc096SFrank Li 	if (!r)
1877793fc096SFrank Li 		return -ENXIO;
1878793fc096SFrank Li 
1879793fc096SFrank Li 	/* Init network device */
1880793fc096SFrank Li 	ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1881793fc096SFrank Li 	if (!ndev)
1882793fc096SFrank Li 		return -ENOMEM;
1883793fc096SFrank Li 
1884793fc096SFrank Li 	SET_NETDEV_DEV(ndev, &pdev->dev);
1885793fc096SFrank Li 
1886793fc096SFrank Li 	/* setup board info structure */
1887793fc096SFrank Li 	fep = netdev_priv(ndev);
1888793fc096SFrank Li 
1889*d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1890793fc096SFrank Li 	/* default enable pause frame auto negotiation */
1891793fc096SFrank Li 	if (pdev->id_entry &&
1892793fc096SFrank Li 	    (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
1893793fc096SFrank Li 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
1894*d1391930SGuenter Roeck #endif
1895793fc096SFrank Li 
1896793fc096SFrank Li 	fep->hwp = devm_request_and_ioremap(&pdev->dev, r);
1897793fc096SFrank Li 	fep->pdev = pdev;
1898793fc096SFrank Li 	fep->dev_id = dev_id++;
1899793fc096SFrank Li 
1900793fc096SFrank Li 	fep->bufdesc_ex = 0;
1901793fc096SFrank Li 
1902793fc096SFrank Li 	if (!fep->hwp) {
1903793fc096SFrank Li 		ret = -ENOMEM;
1904793fc096SFrank Li 		goto failed_ioremap;
1905793fc096SFrank Li 	}
1906793fc096SFrank Li 
1907793fc096SFrank Li 	platform_set_drvdata(pdev, ndev);
1908793fc096SFrank Li 
19096c5f7808SGuenter Roeck 	ret = of_get_phy_mode(pdev->dev.of_node);
1910793fc096SFrank Li 	if (ret < 0) {
1911793fc096SFrank Li 		pdata = pdev->dev.platform_data;
1912793fc096SFrank Li 		if (pdata)
1913793fc096SFrank Li 			fep->phy_interface = pdata->phy;
1914793fc096SFrank Li 		else
1915793fc096SFrank Li 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
1916793fc096SFrank Li 	} else {
1917793fc096SFrank Li 		fep->phy_interface = ret;
1918793fc096SFrank Li 	}
1919793fc096SFrank Li 
1920793fc096SFrank Li 	pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1921793fc096SFrank Li 	if (IS_ERR(pinctrl)) {
1922793fc096SFrank Li 		ret = PTR_ERR(pinctrl);
1923793fc096SFrank Li 		goto failed_pin;
1924793fc096SFrank Li 	}
1925793fc096SFrank Li 
1926793fc096SFrank Li 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1927793fc096SFrank Li 	if (IS_ERR(fep->clk_ipg)) {
1928793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ipg);
1929793fc096SFrank Li 		goto failed_clk;
1930793fc096SFrank Li 	}
1931793fc096SFrank Li 
1932793fc096SFrank Li 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1933793fc096SFrank Li 	if (IS_ERR(fep->clk_ahb)) {
1934793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ahb);
1935793fc096SFrank Li 		goto failed_clk;
1936793fc096SFrank Li 	}
1937793fc096SFrank Li 
193838f56f33SLinus Torvalds 	/* enet_out is optional, depends on board */
193938f56f33SLinus Torvalds 	fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
194038f56f33SLinus Torvalds 	if (IS_ERR(fep->clk_enet_out))
194138f56f33SLinus Torvalds 		fep->clk_enet_out = NULL;
194238f56f33SLinus Torvalds 
1943793fc096SFrank Li 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
1944793fc096SFrank Li 	fep->bufdesc_ex =
1945793fc096SFrank Li 		pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
1946793fc096SFrank Li 	if (IS_ERR(fep->clk_ptp)) {
194738f56f33SLinus Torvalds 		fep->clk_ptp = NULL;
1948793fc096SFrank Li 		fep->bufdesc_ex = 0;
1949793fc096SFrank Li 	}
1950793fc096SFrank Li 
1951793fc096SFrank Li 	clk_prepare_enable(fep->clk_ahb);
1952793fc096SFrank Li 	clk_prepare_enable(fep->clk_ipg);
195338f56f33SLinus Torvalds 	clk_prepare_enable(fep->clk_enet_out);
1954793fc096SFrank Li 	clk_prepare_enable(fep->clk_ptp);
1955793fc096SFrank Li 
1956793fc096SFrank Li 	reg_phy = devm_regulator_get(&pdev->dev, "phy");
1957793fc096SFrank Li 	if (!IS_ERR(reg_phy)) {
1958793fc096SFrank Li 		ret = regulator_enable(reg_phy);
1959793fc096SFrank Li 		if (ret) {
1960793fc096SFrank Li 			dev_err(&pdev->dev,
1961793fc096SFrank Li 				"Failed to enable phy regulator: %d\n", ret);
1962793fc096SFrank Li 			goto failed_regulator;
1963793fc096SFrank Li 		}
1964793fc096SFrank Li 	}
1965793fc096SFrank Li 
1966793fc096SFrank Li 	fec_reset_phy(pdev);
1967793fc096SFrank Li 
1968793fc096SFrank Li 	if (fep->bufdesc_ex)
1969793fc096SFrank Li 		fec_ptp_init(ndev, pdev);
1970793fc096SFrank Li 
1971793fc096SFrank Li 	ret = fec_enet_init(ndev);
1972793fc096SFrank Li 	if (ret)
1973793fc096SFrank Li 		goto failed_init;
1974793fc096SFrank Li 
1975793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
1976793fc096SFrank Li 		irq = platform_get_irq(pdev, i);
1977793fc096SFrank Li 		if (irq < 0) {
1978793fc096SFrank Li 			if (i)
1979793fc096SFrank Li 				break;
1980793fc096SFrank Li 			ret = irq;
1981793fc096SFrank Li 			goto failed_irq;
1982793fc096SFrank Li 		}
1983793fc096SFrank Li 		ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1984793fc096SFrank Li 		if (ret) {
1985793fc096SFrank Li 			while (--i >= 0) {
1986793fc096SFrank Li 				irq = platform_get_irq(pdev, i);
1987793fc096SFrank Li 				free_irq(irq, ndev);
1988793fc096SFrank Li 			}
1989793fc096SFrank Li 			goto failed_irq;
1990793fc096SFrank Li 		}
1991793fc096SFrank Li 	}
1992793fc096SFrank Li 
1993793fc096SFrank Li 	ret = fec_enet_mii_init(pdev);
1994793fc096SFrank Li 	if (ret)
1995793fc096SFrank Li 		goto failed_mii_init;
1996793fc096SFrank Li 
1997793fc096SFrank Li 	/* Carrier starts down, phylib will bring it up */
1998793fc096SFrank Li 	netif_carrier_off(ndev);
1999793fc096SFrank Li 
2000793fc096SFrank Li 	ret = register_netdev(ndev);
2001793fc096SFrank Li 	if (ret)
2002793fc096SFrank Li 		goto failed_register;
2003793fc096SFrank Li 
2004eb1d0640SFabio Estevam 	if (fep->bufdesc_ex && fep->ptp_clock)
2005eb1d0640SFabio Estevam 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2006eb1d0640SFabio Estevam 
200754309fa6SFrank Li 	INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
2008793fc096SFrank Li 	return 0;
2009793fc096SFrank Li 
2010793fc096SFrank Li failed_register:
2011793fc096SFrank Li 	fec_enet_mii_remove(fep);
2012793fc096SFrank Li failed_mii_init:
2013793fc096SFrank Li failed_init:
2014793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
2015793fc096SFrank Li 		irq = platform_get_irq(pdev, i);
2016793fc096SFrank Li 		if (irq > 0)
2017793fc096SFrank Li 			free_irq(irq, ndev);
2018793fc096SFrank Li 	}
2019793fc096SFrank Li failed_irq:
2020793fc096SFrank Li failed_regulator:
2021793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ahb);
2022793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ipg);
202338f56f33SLinus Torvalds 	clk_disable_unprepare(fep->clk_enet_out);
2024793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ptp);
2025793fc096SFrank Li failed_pin:
2026793fc096SFrank Li failed_clk:
2027793fc096SFrank Li failed_ioremap:
2028793fc096SFrank Li 	free_netdev(ndev);
2029793fc096SFrank Li 
2030793fc096SFrank Li 	return ret;
2031793fc096SFrank Li }
2032793fc096SFrank Li 
2033793fc096SFrank Li static int
2034793fc096SFrank Li fec_drv_remove(struct platform_device *pdev)
2035793fc096SFrank Li {
2036793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
2037793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2038793fc096SFrank Li 	int i;
2039793fc096SFrank Li 
204054309fa6SFrank Li 	cancel_delayed_work_sync(&(fep->delay_work.delay_work));
2041793fc096SFrank Li 	unregister_netdev(ndev);
2042793fc096SFrank Li 	fec_enet_mii_remove(fep);
2043793fc096SFrank Li 	del_timer_sync(&fep->time_keep);
2044793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ptp);
2045793fc096SFrank Li 	if (fep->ptp_clock)
2046793fc096SFrank Li 		ptp_clock_unregister(fep->ptp_clock);
204738f56f33SLinus Torvalds 	clk_disable_unprepare(fep->clk_enet_out);
2048793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ahb);
2049793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ipg);
2050793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
2051793fc096SFrank Li 		int irq = platform_get_irq(pdev, i);
2052793fc096SFrank Li 		if (irq > 0)
2053793fc096SFrank Li 			free_irq(irq, ndev);
2054793fc096SFrank Li 	}
2055793fc096SFrank Li 	free_netdev(ndev);
2056793fc096SFrank Li 
2057793fc096SFrank Li 	platform_set_drvdata(pdev, NULL);
2058793fc096SFrank Li 
2059793fc096SFrank Li 	return 0;
2060793fc096SFrank Li }
2061793fc096SFrank Li 
2062bf7bfd7fSFabio Estevam #ifdef CONFIG_PM_SLEEP
2063793fc096SFrank Li static int
2064793fc096SFrank Li fec_suspend(struct device *dev)
2065793fc096SFrank Li {
2066793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
2067793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2068793fc096SFrank Li 
2069793fc096SFrank Li 	if (netif_running(ndev)) {
2070793fc096SFrank Li 		fec_stop(ndev);
2071793fc096SFrank Li 		netif_device_detach(ndev);
2072793fc096SFrank Li 	}
207338f56f33SLinus Torvalds 	clk_disable_unprepare(fep->clk_enet_out);
2074793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ahb);
2075793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ipg);
2076793fc096SFrank Li 
2077793fc096SFrank Li 	return 0;
2078793fc096SFrank Li }
2079793fc096SFrank Li 
2080793fc096SFrank Li static int
2081793fc096SFrank Li fec_resume(struct device *dev)
2082793fc096SFrank Li {
2083793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
2084793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2085793fc096SFrank Li 
208638f56f33SLinus Torvalds 	clk_prepare_enable(fep->clk_enet_out);
2087793fc096SFrank Li 	clk_prepare_enable(fep->clk_ahb);
2088793fc096SFrank Li 	clk_prepare_enable(fep->clk_ipg);
2089793fc096SFrank Li 	if (netif_running(ndev)) {
2090793fc096SFrank Li 		fec_restart(ndev, fep->full_duplex);
2091793fc096SFrank Li 		netif_device_attach(ndev);
2092793fc096SFrank Li 	}
2093793fc096SFrank Li 
2094793fc096SFrank Li 	return 0;
2095793fc096SFrank Li }
2096bf7bfd7fSFabio Estevam #endif /* CONFIG_PM_SLEEP */
2097793fc096SFrank Li 
2098bf7bfd7fSFabio Estevam static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
2099793fc096SFrank Li 
2100793fc096SFrank Li static struct platform_driver fec_driver = {
2101793fc096SFrank Li 	.driver	= {
2102793fc096SFrank Li 		.name	= DRIVER_NAME,
2103793fc096SFrank Li 		.owner	= THIS_MODULE,
2104793fc096SFrank Li 		.pm	= &fec_pm_ops,
2105793fc096SFrank Li 		.of_match_table = fec_dt_ids,
2106793fc096SFrank Li 	},
2107793fc096SFrank Li 	.id_table = fec_devtype,
2108793fc096SFrank Li 	.probe	= fec_probe,
2109793fc096SFrank Li 	.remove	= fec_drv_remove,
2110793fc096SFrank Li };
2111793fc096SFrank Li 
2112793fc096SFrank Li module_platform_driver(fec_driver);
2113793fc096SFrank Li 
2114793fc096SFrank Li MODULE_LICENSE("GPL");
2115