xref: /linux/drivers/net/ethernet/freescale/fec_main.c (revision bfd4ecdd87d350e19457fe0d02fa1e046774c44e)
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/delay.h>
33793fc096SFrank Li #include <linux/netdevice.h>
34793fc096SFrank Li #include <linux/etherdevice.h>
35793fc096SFrank Li #include <linux/skbuff.h>
364c09eed9SJim Baxter #include <linux/in.h>
374c09eed9SJim Baxter #include <linux/ip.h>
384c09eed9SJim Baxter #include <net/ip.h>
3979f33912SNimrod Andy #include <net/tso.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/regulator/consumer.h>
57cdffcf1bSJim Baxter #include <linux/if_vlan.h>
58a68ab98eSFabio Estevam #include <linux/pinctrl/consumer.h>
59793fc096SFrank Li 
60793fc096SFrank Li #include <asm/cacheflush.h>
61793fc096SFrank Li 
62793fc096SFrank Li #include "fec.h"
63793fc096SFrank Li 
64772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev);
65772e42b0SChristoph Müllner 
66793fc096SFrank Li #if defined(CONFIG_ARM)
67793fc096SFrank Li #define FEC_ALIGNMENT	0xf
68793fc096SFrank Li #else
69793fc096SFrank Li #define FEC_ALIGNMENT	0x3
70793fc096SFrank Li #endif
71793fc096SFrank Li 
72793fc096SFrank Li #define DRIVER_NAME	"fec"
73793fc096SFrank Li 
74793fc096SFrank Li /* Pause frame feild and FIFO threshold */
75793fc096SFrank Li #define FEC_ENET_FCE	(1 << 5)
76793fc096SFrank Li #define FEC_ENET_RSEM_V	0x84
77793fc096SFrank Li #define FEC_ENET_RSFL_V	16
78793fc096SFrank Li #define FEC_ENET_RAEM_V	0x8
79793fc096SFrank Li #define FEC_ENET_RAFL_V	0x8
80793fc096SFrank Li #define FEC_ENET_OPD_V	0xFFF0
81793fc096SFrank Li 
82793fc096SFrank Li /* Controller is ENET-MAC */
83793fc096SFrank Li #define FEC_QUIRK_ENET_MAC		(1 << 0)
84793fc096SFrank Li /* Controller needs driver to swap frame */
85793fc096SFrank Li #define FEC_QUIRK_SWAP_FRAME		(1 << 1)
86793fc096SFrank Li /* Controller uses gasket */
87793fc096SFrank Li #define FEC_QUIRK_USE_GASKET		(1 << 2)
88793fc096SFrank Li /* Controller has GBIT support */
89793fc096SFrank Li #define FEC_QUIRK_HAS_GBIT		(1 << 3)
90793fc096SFrank Li /* Controller has extend desc buffer */
91793fc096SFrank Li #define FEC_QUIRK_HAS_BUFDESC_EX	(1 << 4)
9248496255SShawn Guo /* Controller has hardware checksum support */
9348496255SShawn Guo #define FEC_QUIRK_HAS_CSUM		(1 << 5)
94cdffcf1bSJim Baxter /* Controller has hardware vlan support */
95cdffcf1bSJim Baxter #define FEC_QUIRK_HAS_VLAN		(1 << 6)
9603191656SFrank Li /* ENET IP errata ERR006358
9703191656SFrank Li  *
9803191656SFrank Li  * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
9903191656SFrank Li  * detected as not set during a prior frame transmission, then the
10003191656SFrank Li  * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
10103191656SFrank Li  * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
10203191656SFrank Li  * frames not being transmitted until there is a 0-to-1 transition on
10303191656SFrank Li  * ENET_TDAR[TDAR].
10403191656SFrank Li  */
10503191656SFrank Li #define FEC_QUIRK_ERR006358            (1 << 7)
106793fc096SFrank Li 
107793fc096SFrank Li static struct platform_device_id fec_devtype[] = {
108793fc096SFrank Li 	{
109793fc096SFrank Li 		/* keep it for coldfire */
110793fc096SFrank Li 		.name = DRIVER_NAME,
111793fc096SFrank Li 		.driver_data = 0,
112793fc096SFrank Li 	}, {
113793fc096SFrank Li 		.name = "imx25-fec",
114793fc096SFrank Li 		.driver_data = FEC_QUIRK_USE_GASKET,
115793fc096SFrank Li 	}, {
116793fc096SFrank Li 		.name = "imx27-fec",
117793fc096SFrank Li 		.driver_data = 0,
118793fc096SFrank Li 	}, {
119793fc096SFrank Li 		.name = "imx28-fec",
120793fc096SFrank Li 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
121793fc096SFrank Li 	}, {
122793fc096SFrank Li 		.name = "imx6q-fec",
123793fc096SFrank Li 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
124cdffcf1bSJim Baxter 				FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
12503191656SFrank Li 				FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
126793fc096SFrank Li 	}, {
12736803542SShawn Guo 		.name = "mvf600-fec",
128ca7c4a45SJingchang Lu 		.driver_data = FEC_QUIRK_ENET_MAC,
129ca7c4a45SJingchang Lu 	}, {
130793fc096SFrank Li 		/* sentinel */
131793fc096SFrank Li 	}
132793fc096SFrank Li };
133793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype);
134793fc096SFrank Li 
135793fc096SFrank Li enum imx_fec_type {
136793fc096SFrank Li 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
137793fc096SFrank Li 	IMX27_FEC,	/* runs on i.mx27/35/51 */
138793fc096SFrank Li 	IMX28_FEC,
139793fc096SFrank Li 	IMX6Q_FEC,
14036803542SShawn Guo 	MVF600_FEC,
141793fc096SFrank Li };
142793fc096SFrank Li 
143793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = {
144793fc096SFrank Li 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
145793fc096SFrank Li 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
146793fc096SFrank Li 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
147793fc096SFrank Li 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
14836803542SShawn Guo 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
149793fc096SFrank Li 	{ /* sentinel */ }
150793fc096SFrank Li };
151793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids);
152793fc096SFrank Li 
153793fc096SFrank Li static unsigned char macaddr[ETH_ALEN];
154793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0);
155793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
156793fc096SFrank Li 
157793fc096SFrank Li #if defined(CONFIG_M5272)
158793fc096SFrank Li /*
159793fc096SFrank Li  * Some hardware gets it MAC address out of local flash memory.
160793fc096SFrank Li  * if this is non-zero then assume it is the address to get MAC from.
161793fc096SFrank Li  */
162793fc096SFrank Li #if defined(CONFIG_NETtel)
163793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006006
164793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
165793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006000
166793fc096SFrank Li #elif defined(CONFIG_CANCam)
167793fc096SFrank Li #define	FEC_FLASHMAC	0xf0020000
168793fc096SFrank Li #elif defined (CONFIG_M5272C3)
169793fc096SFrank Li #define	FEC_FLASHMAC	(0xffe04000 + 4)
170793fc096SFrank Li #elif defined(CONFIG_MOD5272)
171793fc096SFrank Li #define FEC_FLASHMAC	0xffc0406b
172793fc096SFrank Li #else
173793fc096SFrank Li #define	FEC_FLASHMAC	0
174793fc096SFrank Li #endif
175793fc096SFrank Li #endif /* CONFIG_M5272 */
176793fc096SFrank Li 
177793fc096SFrank Li /* Interrupt events/masks. */
178793fc096SFrank Li #define FEC_ENET_HBERR	((uint)0x80000000)	/* Heartbeat error */
179793fc096SFrank Li #define FEC_ENET_BABR	((uint)0x40000000)	/* Babbling receiver */
180793fc096SFrank Li #define FEC_ENET_BABT	((uint)0x20000000)	/* Babbling transmitter */
181793fc096SFrank Li #define FEC_ENET_GRA	((uint)0x10000000)	/* Graceful stop complete */
182793fc096SFrank Li #define FEC_ENET_TXF	((uint)0x08000000)	/* Full frame transmitted */
183793fc096SFrank Li #define FEC_ENET_TXB	((uint)0x04000000)	/* A buffer was transmitted */
184793fc096SFrank Li #define FEC_ENET_RXF	((uint)0x02000000)	/* Full frame received */
185793fc096SFrank Li #define FEC_ENET_RXB	((uint)0x01000000)	/* A buffer was received */
186793fc096SFrank Li #define FEC_ENET_MII	((uint)0x00800000)	/* MII interrupt */
187793fc096SFrank Li #define FEC_ENET_EBERR	((uint)0x00400000)	/* SDMA bus error */
188793fc096SFrank Li 
189793fc096SFrank Li #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
190793fc096SFrank Li #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
191793fc096SFrank Li 
192cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
193793fc096SFrank Li  */
194cdffcf1bSJim Baxter #define PKT_MAXBUF_SIZE		1522
195793fc096SFrank Li #define PKT_MINBUF_SIZE		64
196cdffcf1bSJim Baxter #define PKT_MAXBLR_SIZE		1536
197793fc096SFrank Li 
1984c09eed9SJim Baxter /* FEC receive acceleration */
1994c09eed9SJim Baxter #define FEC_RACC_IPDIS		(1 << 1)
2004c09eed9SJim Baxter #define FEC_RACC_PRODIS		(1 << 2)
2014c09eed9SJim Baxter #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
2024c09eed9SJim Baxter 
203793fc096SFrank Li /*
204793fc096SFrank Li  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
205793fc096SFrank Li  * size bits. Other FEC hardware does not, so we need to take that into
206793fc096SFrank Li  * account when setting it.
207793fc096SFrank Li  */
208793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
209793fc096SFrank Li     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
210793fc096SFrank Li #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
211793fc096SFrank Li #else
212793fc096SFrank Li #define	OPT_FRAME_SIZE	0
213793fc096SFrank Li #endif
214793fc096SFrank Li 
215793fc096SFrank Li /* FEC MII MMFR bits definition */
216793fc096SFrank Li #define FEC_MMFR_ST		(1 << 30)
217793fc096SFrank Li #define FEC_MMFR_OP_READ	(2 << 28)
218793fc096SFrank Li #define FEC_MMFR_OP_WRITE	(1 << 28)
219793fc096SFrank Li #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
220793fc096SFrank Li #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
221793fc096SFrank Li #define FEC_MMFR_TA		(2 << 16)
222793fc096SFrank Li #define FEC_MMFR_DATA(v)	(v & 0xffff)
223793fc096SFrank Li 
224793fc096SFrank Li #define FEC_MII_TIMEOUT		30000 /* us */
225793fc096SFrank Li 
226793fc096SFrank Li /* Transmitter timeout */
227793fc096SFrank Li #define TX_TIMEOUT (2 * HZ)
228793fc096SFrank Li 
229793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG	0x1
230793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE	0x2
231793fc096SFrank Li 
23279f33912SNimrod Andy #define TSO_HEADER_SIZE		128
23379f33912SNimrod Andy /* Max number of allowed TCP segments for software TSO */
23479f33912SNimrod Andy #define FEC_MAX_TSO_SEGS	100
23579f33912SNimrod Andy #define FEC_MAX_SKB_DESCS	(FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
23679f33912SNimrod Andy 
23779f33912SNimrod Andy #define IS_TSO_HEADER(txq, addr) \
23879f33912SNimrod Andy 	((addr >= txq->tso_hdrs_dma) && \
23979f33912SNimrod Andy 	(addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
24079f33912SNimrod Andy 
241793fc096SFrank Li static int mii_cnt;
242793fc096SFrank Li 
24336e24e2eSDuan Fugang-B38611 static inline
24436e24e2eSDuan Fugang-B38611 struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
245793fc096SFrank Li {
24636e24e2eSDuan Fugang-B38611 	struct bufdesc *new_bd = bdp + 1;
24736e24e2eSDuan Fugang-B38611 	struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
24836e24e2eSDuan Fugang-B38611 	struct bufdesc_ex *ex_base;
24936e24e2eSDuan Fugang-B38611 	struct bufdesc *base;
25036e24e2eSDuan Fugang-B38611 	int ring_size;
25136e24e2eSDuan Fugang-B38611 
25236e24e2eSDuan Fugang-B38611 	if (bdp >= fep->tx_bd_base) {
25336e24e2eSDuan Fugang-B38611 		base = fep->tx_bd_base;
25436e24e2eSDuan Fugang-B38611 		ring_size = fep->tx_ring_size;
25536e24e2eSDuan Fugang-B38611 		ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
25636e24e2eSDuan Fugang-B38611 	} else {
25736e24e2eSDuan Fugang-B38611 		base = fep->rx_bd_base;
25836e24e2eSDuan Fugang-B38611 		ring_size = fep->rx_ring_size;
25936e24e2eSDuan Fugang-B38611 		ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
260793fc096SFrank Li 	}
261793fc096SFrank Li 
26236e24e2eSDuan Fugang-B38611 	if (fep->bufdesc_ex)
26336e24e2eSDuan Fugang-B38611 		return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
26436e24e2eSDuan Fugang-B38611 			ex_base : ex_new_bd);
265793fc096SFrank Li 	else
26636e24e2eSDuan Fugang-B38611 		return (new_bd >= (base + ring_size)) ?
26736e24e2eSDuan Fugang-B38611 			base : new_bd;
26836e24e2eSDuan Fugang-B38611 }
26936e24e2eSDuan Fugang-B38611 
27036e24e2eSDuan Fugang-B38611 static inline
27136e24e2eSDuan Fugang-B38611 struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
27236e24e2eSDuan Fugang-B38611 {
27336e24e2eSDuan Fugang-B38611 	struct bufdesc *new_bd = bdp - 1;
27436e24e2eSDuan Fugang-B38611 	struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
27536e24e2eSDuan Fugang-B38611 	struct bufdesc_ex *ex_base;
27636e24e2eSDuan Fugang-B38611 	struct bufdesc *base;
27736e24e2eSDuan Fugang-B38611 	int ring_size;
27836e24e2eSDuan Fugang-B38611 
27936e24e2eSDuan Fugang-B38611 	if (bdp >= fep->tx_bd_base) {
28036e24e2eSDuan Fugang-B38611 		base = fep->tx_bd_base;
28136e24e2eSDuan Fugang-B38611 		ring_size = fep->tx_ring_size;
28236e24e2eSDuan Fugang-B38611 		ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
28336e24e2eSDuan Fugang-B38611 	} else {
28436e24e2eSDuan Fugang-B38611 		base = fep->rx_bd_base;
28536e24e2eSDuan Fugang-B38611 		ring_size = fep->rx_ring_size;
28636e24e2eSDuan Fugang-B38611 		ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
28736e24e2eSDuan Fugang-B38611 	}
28836e24e2eSDuan Fugang-B38611 
28936e24e2eSDuan Fugang-B38611 	if (fep->bufdesc_ex)
29036e24e2eSDuan Fugang-B38611 		return (struct bufdesc *)((ex_new_bd < ex_base) ?
29136e24e2eSDuan Fugang-B38611 			(ex_new_bd + ring_size) : ex_new_bd);
29236e24e2eSDuan Fugang-B38611 	else
29336e24e2eSDuan Fugang-B38611 		return (new_bd < base) ? (new_bd + ring_size) : new_bd;
294793fc096SFrank Li }
295793fc096SFrank Li 
29661a4427bSNimrod Andy static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
29761a4427bSNimrod Andy 				struct fec_enet_private *fep)
29861a4427bSNimrod Andy {
29961a4427bSNimrod Andy 	return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
30061a4427bSNimrod Andy }
30161a4427bSNimrod Andy 
3026e909283SNimrod Andy static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep)
3036e909283SNimrod Andy {
3046e909283SNimrod Andy 	int entries;
3056e909283SNimrod Andy 
3066e909283SNimrod Andy 	entries = ((const char *)fep->dirty_tx -
3076e909283SNimrod Andy 			(const char *)fep->cur_tx) / fep->bufdesc_size - 1;
3086e909283SNimrod Andy 
3096e909283SNimrod Andy 	return entries > 0 ? entries : entries + fep->tx_ring_size;
3106e909283SNimrod Andy }
3116e909283SNimrod Andy 
312793fc096SFrank Li static void *swap_buffer(void *bufaddr, int len)
313793fc096SFrank Li {
314793fc096SFrank Li 	int i;
315793fc096SFrank Li 	unsigned int *buf = bufaddr;
316793fc096SFrank Li 
317ffed61e6SFabio Estevam 	for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
318793fc096SFrank Li 		*buf = cpu_to_be32(*buf);
319793fc096SFrank Li 
320793fc096SFrank Li 	return bufaddr;
321793fc096SFrank Li }
322793fc096SFrank Li 
323344756f6SRussell King static void fec_dump(struct net_device *ndev)
324344756f6SRussell King {
325344756f6SRussell King 	struct fec_enet_private *fep = netdev_priv(ndev);
326344756f6SRussell King 	struct bufdesc *bdp = fep->tx_bd_base;
327344756f6SRussell King 	unsigned int index = 0;
328344756f6SRussell King 
329344756f6SRussell King 	netdev_info(ndev, "TX ring dump\n");
330344756f6SRussell King 	pr_info("Nr     SC     addr       len  SKB\n");
331344756f6SRussell King 
332344756f6SRussell King 	do {
333344756f6SRussell King 		pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
334344756f6SRussell King 			index,
335344756f6SRussell King 			bdp == fep->cur_tx ? 'S' : ' ',
336344756f6SRussell King 			bdp == fep->dirty_tx ? 'H' : ' ',
337344756f6SRussell King 			bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
338344756f6SRussell King 			fep->tx_skbuff[index]);
339344756f6SRussell King 		bdp = fec_enet_get_nextdesc(bdp, fep);
340344756f6SRussell King 		index++;
341344756f6SRussell King 	} while (bdp != fep->tx_bd_base);
342344756f6SRussell King }
343344756f6SRussell King 
34462a02c98SFugang Duan static inline bool is_ipv4_pkt(struct sk_buff *skb)
34562a02c98SFugang Duan {
34662a02c98SFugang Duan 	return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
34762a02c98SFugang Duan }
34862a02c98SFugang Duan 
3494c09eed9SJim Baxter static int
3504c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
3514c09eed9SJim Baxter {
3524c09eed9SJim Baxter 	/* Only run for packets requiring a checksum. */
3534c09eed9SJim Baxter 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3544c09eed9SJim Baxter 		return 0;
3554c09eed9SJim Baxter 
3564c09eed9SJim Baxter 	if (unlikely(skb_cow_head(skb, 0)))
3574c09eed9SJim Baxter 		return -1;
3584c09eed9SJim Baxter 
35962a02c98SFugang Duan 	if (is_ipv4_pkt(skb))
36096c50caaSNimrod Andy 		ip_hdr(skb)->check = 0;
3614c09eed9SJim Baxter 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
3624c09eed9SJim Baxter 
3634c09eed9SJim Baxter 	return 0;
3644c09eed9SJim Baxter }
3654c09eed9SJim Baxter 
3666e909283SNimrod Andy static int
3676e909283SNimrod Andy fec_enet_txq_submit_frag_skb(struct sk_buff *skb, struct net_device *ndev)
3686e909283SNimrod Andy {
3696e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
3706e909283SNimrod Andy 	const struct platform_device_id *id_entry =
3716e909283SNimrod Andy 				platform_get_device_id(fep->pdev);
3726e909283SNimrod Andy 	struct bufdesc *bdp = fep->cur_tx;
3736e909283SNimrod Andy 	struct bufdesc_ex *ebdp;
3746e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
3756e909283SNimrod Andy 	int frag, frag_len;
3766e909283SNimrod Andy 	unsigned short status;
3776e909283SNimrod Andy 	unsigned int estatus = 0;
3786e909283SNimrod Andy 	skb_frag_t *this_frag;
3796e909283SNimrod Andy 	unsigned int index;
3806e909283SNimrod Andy 	void *bufaddr;
381d6bf3143SRussell King 	dma_addr_t addr;
3826e909283SNimrod Andy 	int i;
3836e909283SNimrod Andy 
3846e909283SNimrod Andy 	for (frag = 0; frag < nr_frags; frag++) {
3856e909283SNimrod Andy 		this_frag = &skb_shinfo(skb)->frags[frag];
3866e909283SNimrod Andy 		bdp = fec_enet_get_nextdesc(bdp, fep);
3876e909283SNimrod Andy 		ebdp = (struct bufdesc_ex *)bdp;
3886e909283SNimrod Andy 
3896e909283SNimrod Andy 		status = bdp->cbd_sc;
3906e909283SNimrod Andy 		status &= ~BD_ENET_TX_STATS;
3916e909283SNimrod Andy 		status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
3926e909283SNimrod Andy 		frag_len = skb_shinfo(skb)->frags[frag].size;
3936e909283SNimrod Andy 
3946e909283SNimrod Andy 		/* Handle the last BD specially */
3956e909283SNimrod Andy 		if (frag == nr_frags - 1) {
3966e909283SNimrod Andy 			status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
3976e909283SNimrod Andy 			if (fep->bufdesc_ex) {
3986e909283SNimrod Andy 				estatus |= BD_ENET_TX_INT;
3996e909283SNimrod Andy 				if (unlikely(skb_shinfo(skb)->tx_flags &
4006e909283SNimrod Andy 					SKBTX_HW_TSTAMP && fep->hwts_tx_en))
4016e909283SNimrod Andy 					estatus |= BD_ENET_TX_TS;
4026e909283SNimrod Andy 			}
4036e909283SNimrod Andy 		}
4046e909283SNimrod Andy 
4056e909283SNimrod Andy 		if (fep->bufdesc_ex) {
4066e909283SNimrod Andy 			if (skb->ip_summed == CHECKSUM_PARTIAL)
4076e909283SNimrod Andy 				estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
4086e909283SNimrod Andy 			ebdp->cbd_bdu = 0;
4096e909283SNimrod Andy 			ebdp->cbd_esc = estatus;
4106e909283SNimrod Andy 		}
4116e909283SNimrod Andy 
4126e909283SNimrod Andy 		bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
4136e909283SNimrod Andy 
4146e909283SNimrod Andy 		index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
4156e909283SNimrod Andy 		if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
4166e909283SNimrod Andy 			id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
4176e909283SNimrod Andy 			memcpy(fep->tx_bounce[index], bufaddr, frag_len);
4186e909283SNimrod Andy 			bufaddr = fep->tx_bounce[index];
4196e909283SNimrod Andy 
4206e909283SNimrod Andy 			if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
4216e909283SNimrod Andy 				swap_buffer(bufaddr, frag_len);
4226e909283SNimrod Andy 		}
4236e909283SNimrod Andy 
424d6bf3143SRussell King 		addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
425d6bf3143SRussell King 				      DMA_TO_DEVICE);
426d6bf3143SRussell King 		if (dma_mapping_error(&fep->pdev->dev, addr)) {
4276e909283SNimrod Andy 			dev_kfree_skb_any(skb);
4286e909283SNimrod Andy 			if (net_ratelimit())
4296e909283SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
4306e909283SNimrod Andy 			goto dma_mapping_error;
4316e909283SNimrod Andy 		}
4326e909283SNimrod Andy 
433d6bf3143SRussell King 		bdp->cbd_bufaddr = addr;
4346e909283SNimrod Andy 		bdp->cbd_datlen = frag_len;
4356e909283SNimrod Andy 		bdp->cbd_sc = status;
4366e909283SNimrod Andy 	}
4376e909283SNimrod Andy 
4386e909283SNimrod Andy 	fep->cur_tx = bdp;
4396e909283SNimrod Andy 
4406e909283SNimrod Andy 	return 0;
4416e909283SNimrod Andy 
4426e909283SNimrod Andy dma_mapping_error:
4436e909283SNimrod Andy 	bdp = fep->cur_tx;
4446e909283SNimrod Andy 	for (i = 0; i < frag; i++) {
4456e909283SNimrod Andy 		bdp = fec_enet_get_nextdesc(bdp, fep);
4466e909283SNimrod Andy 		dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
4476e909283SNimrod Andy 				bdp->cbd_datlen, DMA_TO_DEVICE);
4486e909283SNimrod Andy 	}
4496e909283SNimrod Andy 	return NETDEV_TX_OK;
4506e909283SNimrod Andy }
4516e909283SNimrod Andy 
4526e909283SNimrod Andy static int fec_enet_txq_submit_skb(struct sk_buff *skb, struct net_device *ndev)
4536e909283SNimrod Andy {
4546e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
4556e909283SNimrod Andy 	const struct platform_device_id *id_entry =
4566e909283SNimrod Andy 				platform_get_device_id(fep->pdev);
4576e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
4586e909283SNimrod Andy 	struct bufdesc *bdp, *last_bdp;
4596e909283SNimrod Andy 	void *bufaddr;
460d6bf3143SRussell King 	dma_addr_t addr;
4616e909283SNimrod Andy 	unsigned short status;
4626e909283SNimrod Andy 	unsigned short buflen;
4636e909283SNimrod Andy 	unsigned int estatus = 0;
4646e909283SNimrod Andy 	unsigned int index;
46579f33912SNimrod Andy 	int entries_free;
4666e909283SNimrod Andy 	int ret;
4676e909283SNimrod Andy 
46879f33912SNimrod Andy 	entries_free = fec_enet_get_free_txdesc_num(fep);
46979f33912SNimrod Andy 	if (entries_free < MAX_SKB_FRAGS + 1) {
47079f33912SNimrod Andy 		dev_kfree_skb_any(skb);
47179f33912SNimrod Andy 		if (net_ratelimit())
47279f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for SG!\n");
47379f33912SNimrod Andy 		return NETDEV_TX_OK;
47479f33912SNimrod Andy 	}
47579f33912SNimrod Andy 
4766e909283SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
4776e909283SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
4786e909283SNimrod Andy 		dev_kfree_skb_any(skb);
4796e909283SNimrod Andy 		return NETDEV_TX_OK;
4806e909283SNimrod Andy 	}
4816e909283SNimrod Andy 
4826e909283SNimrod Andy 	/* Fill in a Tx ring entry */
4836e909283SNimrod Andy 	bdp = fep->cur_tx;
4846e909283SNimrod Andy 	status = bdp->cbd_sc;
4856e909283SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
4866e909283SNimrod Andy 
4876e909283SNimrod Andy 	/* Set buffer length and buffer pointer */
4886e909283SNimrod Andy 	bufaddr = skb->data;
4896e909283SNimrod Andy 	buflen = skb_headlen(skb);
4906e909283SNimrod Andy 
4916e909283SNimrod Andy 	index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
4926e909283SNimrod Andy 	if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
4936e909283SNimrod Andy 		id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
4946e909283SNimrod Andy 		memcpy(fep->tx_bounce[index], skb->data, buflen);
4956e909283SNimrod Andy 		bufaddr = fep->tx_bounce[index];
4966e909283SNimrod Andy 
4976e909283SNimrod Andy 		if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
4986e909283SNimrod Andy 			swap_buffer(bufaddr, buflen);
4996e909283SNimrod Andy 	}
5006e909283SNimrod Andy 
501d6bf3143SRussell King 	/* Push the data cache so the CPM does not get stale memory data. */
502d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
503d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
5046e909283SNimrod Andy 		dev_kfree_skb_any(skb);
5056e909283SNimrod Andy 		if (net_ratelimit())
5066e909283SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
5076e909283SNimrod Andy 		return NETDEV_TX_OK;
5086e909283SNimrod Andy 	}
5096e909283SNimrod Andy 
5106e909283SNimrod Andy 	if (nr_frags) {
5116e909283SNimrod Andy 		ret = fec_enet_txq_submit_frag_skb(skb, ndev);
5126e909283SNimrod Andy 		if (ret)
5136e909283SNimrod Andy 			return ret;
5146e909283SNimrod Andy 	} else {
5156e909283SNimrod Andy 		status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
5166e909283SNimrod Andy 		if (fep->bufdesc_ex) {
5176e909283SNimrod Andy 			estatus = BD_ENET_TX_INT;
5186e909283SNimrod Andy 			if (unlikely(skb_shinfo(skb)->tx_flags &
5196e909283SNimrod Andy 				SKBTX_HW_TSTAMP && fep->hwts_tx_en))
5206e909283SNimrod Andy 				estatus |= BD_ENET_TX_TS;
5216e909283SNimrod Andy 		}
5226e909283SNimrod Andy 	}
5236e909283SNimrod Andy 
5246e909283SNimrod Andy 	if (fep->bufdesc_ex) {
5256e909283SNimrod Andy 
5266e909283SNimrod Andy 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
5276e909283SNimrod Andy 
5286e909283SNimrod Andy 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
5296e909283SNimrod Andy 			fep->hwts_tx_en))
5306e909283SNimrod Andy 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
5316e909283SNimrod Andy 
5326e909283SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
5336e909283SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
5346e909283SNimrod Andy 
5356e909283SNimrod Andy 		ebdp->cbd_bdu = 0;
5366e909283SNimrod Andy 		ebdp->cbd_esc = estatus;
5376e909283SNimrod Andy 	}
5386e909283SNimrod Andy 
5396e909283SNimrod Andy 	last_bdp = fep->cur_tx;
5406e909283SNimrod Andy 	index = fec_enet_get_bd_index(fep->tx_bd_base, last_bdp, fep);
5416e909283SNimrod Andy 	/* Save skb pointer */
5426e909283SNimrod Andy 	fep->tx_skbuff[index] = skb;
5436e909283SNimrod Andy 
5446e909283SNimrod Andy 	bdp->cbd_datlen = buflen;
545d6bf3143SRussell King 	bdp->cbd_bufaddr = addr;
5466e909283SNimrod Andy 
5476e909283SNimrod Andy 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
5486e909283SNimrod Andy 	 * it's the last BD of the frame, and to put the CRC on the end.
5496e909283SNimrod Andy 	 */
5506e909283SNimrod Andy 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
5516e909283SNimrod Andy 	bdp->cbd_sc = status;
5526e909283SNimrod Andy 
553793fc096SFrank Li 	/* If this was the last BD in the ring, start at the beginning again. */
5546e909283SNimrod Andy 	bdp = fec_enet_get_nextdesc(last_bdp, fep);
555793fc096SFrank Li 
5567a2a8451SEric Dumazet 	skb_tx_timestamp(skb);
5577a2a8451SEric Dumazet 
558793fc096SFrank Li 	fep->cur_tx = bdp;
559793fc096SFrank Li 
560793fc096SFrank Li 	/* Trigger transmission start */
561793fc096SFrank Li 	writel(0, fep->hwp + FEC_X_DES_ACTIVE);
562793fc096SFrank Li 
5636e909283SNimrod Andy 	return 0;
564793fc096SFrank Li }
565793fc096SFrank Li 
56679f33912SNimrod Andy static int
56779f33912SNimrod Andy fec_enet_txq_put_data_tso(struct sk_buff *skb, struct net_device *ndev,
56879f33912SNimrod Andy 			struct bufdesc *bdp, int index, char *data,
56979f33912SNimrod Andy 			int size, bool last_tcp, bool is_last)
57079f33912SNimrod Andy {
57179f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
57279f33912SNimrod Andy 	const struct platform_device_id *id_entry =
57379f33912SNimrod Andy 				platform_get_device_id(fep->pdev);
57479f33912SNimrod Andy 	struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
57579f33912SNimrod Andy 	unsigned short status;
57679f33912SNimrod Andy 	unsigned int estatus = 0;
577d6bf3143SRussell King 	dma_addr_t addr;
57879f33912SNimrod Andy 
57979f33912SNimrod Andy 	status = bdp->cbd_sc;
58079f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
58179f33912SNimrod Andy 
58279f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
58379f33912SNimrod Andy 
58479f33912SNimrod Andy 	if (((unsigned long) data) & FEC_ALIGNMENT ||
58579f33912SNimrod Andy 		id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
58679f33912SNimrod Andy 		memcpy(fep->tx_bounce[index], data, size);
58779f33912SNimrod Andy 		data = fep->tx_bounce[index];
58879f33912SNimrod Andy 
58979f33912SNimrod Andy 		if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
59079f33912SNimrod Andy 			swap_buffer(data, size);
59179f33912SNimrod Andy 	}
59279f33912SNimrod Andy 
593d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
594d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
59579f33912SNimrod Andy 		dev_kfree_skb_any(skb);
59679f33912SNimrod Andy 		if (net_ratelimit())
59779f33912SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
59879f33912SNimrod Andy 		return NETDEV_TX_BUSY;
59979f33912SNimrod Andy 	}
60079f33912SNimrod Andy 
601d6bf3143SRussell King 	bdp->cbd_datlen = size;
602d6bf3143SRussell King 	bdp->cbd_bufaddr = addr;
603d6bf3143SRussell King 
60479f33912SNimrod Andy 	if (fep->bufdesc_ex) {
60579f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
60679f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
60779f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
60879f33912SNimrod Andy 		ebdp->cbd_esc = estatus;
60979f33912SNimrod Andy 	}
61079f33912SNimrod Andy 
61179f33912SNimrod Andy 	/* Handle the last BD specially */
61279f33912SNimrod Andy 	if (last_tcp)
61379f33912SNimrod Andy 		status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
61479f33912SNimrod Andy 	if (is_last) {
61579f33912SNimrod Andy 		status |= BD_ENET_TX_INTR;
61679f33912SNimrod Andy 		if (fep->bufdesc_ex)
61779f33912SNimrod Andy 			ebdp->cbd_esc |= BD_ENET_TX_INT;
61879f33912SNimrod Andy 	}
61979f33912SNimrod Andy 
62079f33912SNimrod Andy 	bdp->cbd_sc = status;
62179f33912SNimrod Andy 
62279f33912SNimrod Andy 	return 0;
62379f33912SNimrod Andy }
62479f33912SNimrod Andy 
62579f33912SNimrod Andy static int
62679f33912SNimrod Andy fec_enet_txq_put_hdr_tso(struct sk_buff *skb, struct net_device *ndev,
62779f33912SNimrod Andy 			struct bufdesc *bdp, int index)
62879f33912SNimrod Andy {
62979f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
63079f33912SNimrod Andy 	const struct platform_device_id *id_entry =
63179f33912SNimrod Andy 				platform_get_device_id(fep->pdev);
63279f33912SNimrod Andy 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
63379f33912SNimrod Andy 	struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
63479f33912SNimrod Andy 	void *bufaddr;
63579f33912SNimrod Andy 	unsigned long dmabuf;
63679f33912SNimrod Andy 	unsigned short status;
63779f33912SNimrod Andy 	unsigned int estatus = 0;
63879f33912SNimrod Andy 
63979f33912SNimrod Andy 	status = bdp->cbd_sc;
64079f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
64179f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
64279f33912SNimrod Andy 
64379f33912SNimrod Andy 	bufaddr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
64479f33912SNimrod Andy 	dmabuf = fep->tso_hdrs_dma + index * TSO_HEADER_SIZE;
64579f33912SNimrod Andy 	if (((unsigned long) bufaddr) & FEC_ALIGNMENT ||
64679f33912SNimrod Andy 		id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
64779f33912SNimrod Andy 		memcpy(fep->tx_bounce[index], skb->data, hdr_len);
64879f33912SNimrod Andy 		bufaddr = fep->tx_bounce[index];
64979f33912SNimrod Andy 
65079f33912SNimrod Andy 		if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
65179f33912SNimrod Andy 			swap_buffer(bufaddr, hdr_len);
65279f33912SNimrod Andy 
65379f33912SNimrod Andy 		dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
65479f33912SNimrod Andy 					hdr_len, DMA_TO_DEVICE);
65579f33912SNimrod Andy 		if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
65679f33912SNimrod Andy 			dev_kfree_skb_any(skb);
65779f33912SNimrod Andy 			if (net_ratelimit())
65879f33912SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
65979f33912SNimrod Andy 			return NETDEV_TX_BUSY;
66079f33912SNimrod Andy 		}
66179f33912SNimrod Andy 	}
66279f33912SNimrod Andy 
66379f33912SNimrod Andy 	bdp->cbd_bufaddr = dmabuf;
66479f33912SNimrod Andy 	bdp->cbd_datlen = hdr_len;
66579f33912SNimrod Andy 
66679f33912SNimrod Andy 	if (fep->bufdesc_ex) {
66779f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
66879f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
66979f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
67079f33912SNimrod Andy 		ebdp->cbd_esc = estatus;
67179f33912SNimrod Andy 	}
67279f33912SNimrod Andy 
67379f33912SNimrod Andy 	bdp->cbd_sc = status;
67479f33912SNimrod Andy 
67579f33912SNimrod Andy 	return 0;
67679f33912SNimrod Andy }
67779f33912SNimrod Andy 
67879f33912SNimrod Andy static int fec_enet_txq_submit_tso(struct sk_buff *skb, struct net_device *ndev)
67979f33912SNimrod Andy {
68079f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
68179f33912SNimrod Andy 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
68279f33912SNimrod Andy 	int total_len, data_left;
68379f33912SNimrod Andy 	struct bufdesc *bdp = fep->cur_tx;
68479f33912SNimrod Andy 	struct tso_t tso;
68579f33912SNimrod Andy 	unsigned int index = 0;
68679f33912SNimrod Andy 	int ret;
68779f33912SNimrod Andy 
68879f33912SNimrod Andy 	if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep)) {
68979f33912SNimrod Andy 		dev_kfree_skb_any(skb);
69079f33912SNimrod Andy 		if (net_ratelimit())
69179f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for TSO!\n");
69279f33912SNimrod Andy 		return NETDEV_TX_OK;
69379f33912SNimrod Andy 	}
69479f33912SNimrod Andy 
69579f33912SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
69679f33912SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
69779f33912SNimrod Andy 		dev_kfree_skb_any(skb);
69879f33912SNimrod Andy 		return NETDEV_TX_OK;
69979f33912SNimrod Andy 	}
70079f33912SNimrod Andy 
70179f33912SNimrod Andy 	/* Initialize the TSO handler, and prepare the first payload */
70279f33912SNimrod Andy 	tso_start(skb, &tso);
70379f33912SNimrod Andy 
70479f33912SNimrod Andy 	total_len = skb->len - hdr_len;
70579f33912SNimrod Andy 	while (total_len > 0) {
70679f33912SNimrod Andy 		char *hdr;
70779f33912SNimrod Andy 
70879f33912SNimrod Andy 		index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
70979f33912SNimrod Andy 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
71079f33912SNimrod Andy 		total_len -= data_left;
71179f33912SNimrod Andy 
71279f33912SNimrod Andy 		/* prepare packet headers: MAC + IP + TCP */
71379f33912SNimrod Andy 		hdr = fep->tso_hdrs + index * TSO_HEADER_SIZE;
71479f33912SNimrod Andy 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
71579f33912SNimrod Andy 		ret = fec_enet_txq_put_hdr_tso(skb, ndev, bdp, index);
71679f33912SNimrod Andy 		if (ret)
71779f33912SNimrod Andy 			goto err_release;
71879f33912SNimrod Andy 
71979f33912SNimrod Andy 		while (data_left > 0) {
72079f33912SNimrod Andy 			int size;
72179f33912SNimrod Andy 
72279f33912SNimrod Andy 			size = min_t(int, tso.size, data_left);
72379f33912SNimrod Andy 			bdp = fec_enet_get_nextdesc(bdp, fep);
72479f33912SNimrod Andy 			index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
72579f33912SNimrod Andy 			ret = fec_enet_txq_put_data_tso(skb, ndev, bdp, index, tso.data,
72679f33912SNimrod Andy 							size, size == data_left,
72779f33912SNimrod Andy 							total_len == 0);
72879f33912SNimrod Andy 			if (ret)
72979f33912SNimrod Andy 				goto err_release;
73079f33912SNimrod Andy 
73179f33912SNimrod Andy 			data_left -= size;
73279f33912SNimrod Andy 			tso_build_data(skb, &tso, size);
73379f33912SNimrod Andy 		}
73479f33912SNimrod Andy 
73579f33912SNimrod Andy 		bdp = fec_enet_get_nextdesc(bdp, fep);
73679f33912SNimrod Andy 	}
73779f33912SNimrod Andy 
73879f33912SNimrod Andy 	/* Save skb pointer */
73979f33912SNimrod Andy 	fep->tx_skbuff[index] = skb;
74079f33912SNimrod Andy 
74179f33912SNimrod Andy 	skb_tx_timestamp(skb);
74279f33912SNimrod Andy 	fep->cur_tx = bdp;
74379f33912SNimrod Andy 
74479f33912SNimrod Andy 	/* Trigger transmission start */
74579f33912SNimrod Andy 	writel(0, fep->hwp + FEC_X_DES_ACTIVE);
74679f33912SNimrod Andy 
74779f33912SNimrod Andy 	return 0;
74879f33912SNimrod Andy 
74979f33912SNimrod Andy err_release:
75079f33912SNimrod Andy 	/* TODO: Release all used data descriptors for TSO */
75179f33912SNimrod Andy 	return ret;
75279f33912SNimrod Andy }
75379f33912SNimrod Andy 
75461a4427bSNimrod Andy static netdev_tx_t
75561a4427bSNimrod Andy fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
75661a4427bSNimrod Andy {
75761a4427bSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
7586e909283SNimrod Andy 	int entries_free;
75961a4427bSNimrod Andy 	int ret;
76061a4427bSNimrod Andy 
76179f33912SNimrod Andy 	if (skb_is_gso(skb))
76279f33912SNimrod Andy 		ret = fec_enet_txq_submit_tso(skb, ndev);
76379f33912SNimrod Andy 	else
7646e909283SNimrod Andy 		ret = fec_enet_txq_submit_skb(skb, ndev);
7656e909283SNimrod Andy 	if (ret)
7666e909283SNimrod Andy 		return ret;
76761a4427bSNimrod Andy 
7686e909283SNimrod Andy 	entries_free = fec_enet_get_free_txdesc_num(fep);
76979f33912SNimrod Andy 	if (entries_free <= fep->tx_stop_threshold)
77061a4427bSNimrod Andy 		netif_stop_queue(ndev);
77161a4427bSNimrod Andy 
77261a4427bSNimrod Andy 	return NETDEV_TX_OK;
77361a4427bSNimrod Andy }
77461a4427bSNimrod Andy 
775a210576cSDavid S. Miller /* Init RX & TX buffer descriptors
776a210576cSDavid S. Miller  */
777a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev)
778a210576cSDavid S. Miller {
779a210576cSDavid S. Miller 	struct fec_enet_private *fep = netdev_priv(dev);
780a210576cSDavid S. Miller 	struct bufdesc *bdp;
781a210576cSDavid S. Miller 	unsigned int i;
782a210576cSDavid S. Miller 
783a210576cSDavid S. Miller 	/* Initialize the receive buffer descriptors. */
784a210576cSDavid S. Miller 	bdp = fep->rx_bd_base;
78536e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->rx_ring_size; i++) {
786a210576cSDavid S. Miller 
787a210576cSDavid S. Miller 		/* Initialize the BD for every fragment in the page. */
788a210576cSDavid S. Miller 		if (bdp->cbd_bufaddr)
789a210576cSDavid S. Miller 			bdp->cbd_sc = BD_ENET_RX_EMPTY;
790a210576cSDavid S. Miller 		else
791a210576cSDavid S. Miller 			bdp->cbd_sc = 0;
79236e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
793a210576cSDavid S. Miller 	}
794a210576cSDavid S. Miller 
795a210576cSDavid S. Miller 	/* Set the last buffer to wrap */
79636e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_prevdesc(bdp, fep);
797a210576cSDavid S. Miller 	bdp->cbd_sc |= BD_SC_WRAP;
798a210576cSDavid S. Miller 
799a210576cSDavid S. Miller 	fep->cur_rx = fep->rx_bd_base;
800a210576cSDavid S. Miller 
801a210576cSDavid S. Miller 	/* ...and the same for transmit */
802a210576cSDavid S. Miller 	bdp = fep->tx_bd_base;
803a210576cSDavid S. Miller 	fep->cur_tx = bdp;
80436e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->tx_ring_size; i++) {
805a210576cSDavid S. Miller 
806a210576cSDavid S. Miller 		/* Initialize the BD for every fragment in the page. */
807a210576cSDavid S. Miller 		bdp->cbd_sc = 0;
808d6bf3143SRussell King 		if (fep->tx_skbuff[i]) {
809a210576cSDavid S. Miller 			dev_kfree_skb_any(fep->tx_skbuff[i]);
810a210576cSDavid S. Miller 			fep->tx_skbuff[i] = NULL;
811a210576cSDavid S. Miller 		}
812a210576cSDavid S. Miller 		bdp->cbd_bufaddr = 0;
81336e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
814a210576cSDavid S. Miller 	}
815a210576cSDavid S. Miller 
816a210576cSDavid S. Miller 	/* Set the last buffer to wrap */
81736e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_prevdesc(bdp, fep);
818a210576cSDavid S. Miller 	bdp->cbd_sc |= BD_SC_WRAP;
819a210576cSDavid S. Miller 	fep->dirty_tx = bdp;
820a210576cSDavid S. Miller }
821a210576cSDavid S. Miller 
822dbc64a8eSRussell King /*
823dbc64a8eSRussell King  * This function is called to start or restart the FEC during a link
824dbc64a8eSRussell King  * change, transmit timeout, or to reconfigure the FEC.  The network
825dbc64a8eSRussell King  * packet processing for this device must be stopped before this call.
826793fc096SFrank Li  */
827793fc096SFrank Li static void
828ef83337dSRussell King fec_restart(struct net_device *ndev)
829793fc096SFrank Li {
830793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
831793fc096SFrank Li 	const struct platform_device_id *id_entry =
832793fc096SFrank Li 				platform_get_device_id(fep->pdev);
833793fc096SFrank Li 	int i;
8344c09eed9SJim Baxter 	u32 val;
835793fc096SFrank Li 	u32 temp_mac[2];
836793fc096SFrank Li 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
837793fc096SFrank Li 	u32 ecntl = 0x2; /* ETHEREN */
838793fc096SFrank Li 
839793fc096SFrank Li 	/* Whack a reset.  We should wait for this. */
840793fc096SFrank Li 	writel(1, fep->hwp + FEC_ECNTRL);
841793fc096SFrank Li 	udelay(10);
842793fc096SFrank Li 
843793fc096SFrank Li 	/*
844793fc096SFrank Li 	 * enet-mac reset will reset mac address registers too,
845793fc096SFrank Li 	 * so need to reconfigure it.
846793fc096SFrank Li 	 */
847793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
848793fc096SFrank Li 		memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
849793fc096SFrank Li 		writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
850793fc096SFrank Li 		writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
851793fc096SFrank Li 	}
852793fc096SFrank Li 
853793fc096SFrank Li 	/* Clear any outstanding interrupt. */
854793fc096SFrank Li 	writel(0xffc00000, fep->hwp + FEC_IEVENT);
855793fc096SFrank Li 
856793fc096SFrank Li 	/* Set maximum receive buffer size. */
857793fc096SFrank Li 	writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
858793fc096SFrank Li 
859a210576cSDavid S. Miller 	fec_enet_bd_init(ndev);
860a210576cSDavid S. Miller 
861793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
862793fc096SFrank Li 	writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
863793fc096SFrank Li 	if (fep->bufdesc_ex)
864793fc096SFrank Li 		writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
86536e24e2eSDuan Fugang-B38611 			* fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
866793fc096SFrank Li 	else
867793fc096SFrank Li 		writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
86836e24e2eSDuan Fugang-B38611 			* fep->rx_ring_size,	fep->hwp + FEC_X_DES_START);
869793fc096SFrank Li 
870793fc096SFrank Li 
871793fc096SFrank Li 	for (i = 0; i <= TX_RING_MOD_MASK; i++) {
872793fc096SFrank Li 		if (fep->tx_skbuff[i]) {
873793fc096SFrank Li 			dev_kfree_skb_any(fep->tx_skbuff[i]);
874793fc096SFrank Li 			fep->tx_skbuff[i] = NULL;
875793fc096SFrank Li 		}
876793fc096SFrank Li 	}
877793fc096SFrank Li 
878793fc096SFrank Li 	/* Enable MII mode */
879ef83337dSRussell King 	if (fep->full_duplex == DUPLEX_FULL) {
880793fc096SFrank Li 		/* FD enable */
881793fc096SFrank Li 		writel(0x04, fep->hwp + FEC_X_CNTRL);
882793fc096SFrank Li 	} else {
883793fc096SFrank Li 		/* No Rcv on Xmit */
884793fc096SFrank Li 		rcntl |= 0x02;
885793fc096SFrank Li 		writel(0x0, fep->hwp + FEC_X_CNTRL);
886793fc096SFrank Li 	}
887793fc096SFrank Li 
888793fc096SFrank Li 	/* Set MII speed */
889793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
890793fc096SFrank Li 
891d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
8924c09eed9SJim Baxter 	/* set RX checksum */
8934c09eed9SJim Baxter 	val = readl(fep->hwp + FEC_RACC);
8944c09eed9SJim Baxter 	if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
8954c09eed9SJim Baxter 		val |= FEC_RACC_OPTIONS;
8964c09eed9SJim Baxter 	else
8974c09eed9SJim Baxter 		val &= ~FEC_RACC_OPTIONS;
8984c09eed9SJim Baxter 	writel(val, fep->hwp + FEC_RACC);
899d1391930SGuenter Roeck #endif
9004c09eed9SJim Baxter 
901793fc096SFrank Li 	/*
902793fc096SFrank Li 	 * The phy interface and speed need to get configured
903793fc096SFrank Li 	 * differently on enet-mac.
904793fc096SFrank Li 	 */
905793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
906793fc096SFrank Li 		/* Enable flow control and length check */
907793fc096SFrank Li 		rcntl |= 0x40000000 | 0x00000020;
908793fc096SFrank Li 
909793fc096SFrank Li 		/* RGMII, RMII or MII */
910793fc096SFrank Li 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
911793fc096SFrank Li 			rcntl |= (1 << 6);
912793fc096SFrank Li 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
913793fc096SFrank Li 			rcntl |= (1 << 8);
914793fc096SFrank Li 		else
915793fc096SFrank Li 			rcntl &= ~(1 << 8);
916793fc096SFrank Li 
917793fc096SFrank Li 		/* 1G, 100M or 10M */
918793fc096SFrank Li 		if (fep->phy_dev) {
919793fc096SFrank Li 			if (fep->phy_dev->speed == SPEED_1000)
920793fc096SFrank Li 				ecntl |= (1 << 5);
921793fc096SFrank Li 			else if (fep->phy_dev->speed == SPEED_100)
922793fc096SFrank Li 				rcntl &= ~(1 << 9);
923793fc096SFrank Li 			else
924793fc096SFrank Li 				rcntl |= (1 << 9);
925793fc096SFrank Li 		}
926793fc096SFrank Li 	} else {
927793fc096SFrank Li #ifdef FEC_MIIGSK_ENR
928793fc096SFrank Li 		if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
929793fc096SFrank Li 			u32 cfgr;
930793fc096SFrank Li 			/* disable the gasket and wait */
931793fc096SFrank Li 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
932793fc096SFrank Li 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
933793fc096SFrank Li 				udelay(1);
934793fc096SFrank Li 
935793fc096SFrank Li 			/*
936793fc096SFrank Li 			 * configure the gasket:
937793fc096SFrank Li 			 *   RMII, 50 MHz, no loopback, no echo
938793fc096SFrank Li 			 *   MII, 25 MHz, no loopback, no echo
939793fc096SFrank Li 			 */
940793fc096SFrank Li 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
941793fc096SFrank Li 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
942793fc096SFrank Li 			if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
943793fc096SFrank Li 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
944793fc096SFrank Li 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
945793fc096SFrank Li 
946793fc096SFrank Li 			/* re-enable the gasket */
947793fc096SFrank Li 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
948793fc096SFrank Li 		}
949793fc096SFrank Li #endif
950793fc096SFrank Li 	}
951793fc096SFrank Li 
952d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
953793fc096SFrank Li 	/* enable pause frame*/
954793fc096SFrank Li 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
955793fc096SFrank Li 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
956793fc096SFrank Li 	     fep->phy_dev && fep->phy_dev->pause)) {
957793fc096SFrank Li 		rcntl |= FEC_ENET_FCE;
958793fc096SFrank Li 
9594c09eed9SJim Baxter 		/* set FIFO threshold parameter to reduce overrun */
960793fc096SFrank Li 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
961793fc096SFrank Li 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
962793fc096SFrank Li 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
963793fc096SFrank Li 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
964793fc096SFrank Li 
965793fc096SFrank Li 		/* OPD */
966793fc096SFrank Li 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
967793fc096SFrank Li 	} else {
968793fc096SFrank Li 		rcntl &= ~FEC_ENET_FCE;
969793fc096SFrank Li 	}
970d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
971793fc096SFrank Li 
972793fc096SFrank Li 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
973793fc096SFrank Li 
97484fe6182SStefan Wahren 	/* Setup multicast filter. */
97584fe6182SStefan Wahren 	set_multicast_list(ndev);
97684fe6182SStefan Wahren #ifndef CONFIG_M5272
97784fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
97884fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
97984fe6182SStefan Wahren #endif
98084fe6182SStefan Wahren 
981793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
982793fc096SFrank Li 		/* enable ENET endian swap */
983793fc096SFrank Li 		ecntl |= (1 << 8);
984793fc096SFrank Li 		/* enable ENET store and forward mode */
985793fc096SFrank Li 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
986793fc096SFrank Li 	}
987793fc096SFrank Li 
988793fc096SFrank Li 	if (fep->bufdesc_ex)
989793fc096SFrank Li 		ecntl |= (1 << 4);
990793fc096SFrank Li 
99138ae92dcSChris Healy #ifndef CONFIG_M5272
992b9eef55cSJim Baxter 	/* Enable the MIB statistic event counters */
993b9eef55cSJim Baxter 	writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
99438ae92dcSChris Healy #endif
99538ae92dcSChris Healy 
996793fc096SFrank Li 	/* And last, enable the transmit and receive processing */
997793fc096SFrank Li 	writel(ecntl, fep->hwp + FEC_ECNTRL);
998793fc096SFrank Li 	writel(0, fep->hwp + FEC_R_DES_ACTIVE);
999793fc096SFrank Li 
1000793fc096SFrank Li 	if (fep->bufdesc_ex)
1001793fc096SFrank Li 		fec_ptp_start_cyclecounter(ndev);
1002793fc096SFrank Li 
1003793fc096SFrank Li 	/* Enable interrupts we wish to service */
1004793fc096SFrank Li 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1005793fc096SFrank Li }
1006793fc096SFrank Li 
1007793fc096SFrank Li static void
1008793fc096SFrank Li fec_stop(struct net_device *ndev)
1009793fc096SFrank Li {
1010793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1011793fc096SFrank Li 	const struct platform_device_id *id_entry =
1012793fc096SFrank Li 				platform_get_device_id(fep->pdev);
1013793fc096SFrank Li 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1014793fc096SFrank Li 
1015793fc096SFrank Li 	/* We cannot expect a graceful transmit stop without link !!! */
1016793fc096SFrank Li 	if (fep->link) {
1017793fc096SFrank Li 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1018793fc096SFrank Li 		udelay(10);
1019793fc096SFrank Li 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
102031b7720cSJoe Perches 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1021793fc096SFrank Li 	}
1022793fc096SFrank Li 
1023793fc096SFrank Li 	/* Whack a reset.  We should wait for this. */
1024793fc096SFrank Li 	writel(1, fep->hwp + FEC_ECNTRL);
1025793fc096SFrank Li 	udelay(10);
1026793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1027793fc096SFrank Li 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1028793fc096SFrank Li 
1029793fc096SFrank Li 	/* We have to keep ENET enabled to have MII interrupt stay working */
1030793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1031793fc096SFrank Li 		writel(2, fep->hwp + FEC_ECNTRL);
1032793fc096SFrank Li 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1033793fc096SFrank Li 	}
1034793fc096SFrank Li }
1035793fc096SFrank Li 
1036793fc096SFrank Li 
1037793fc096SFrank Li static void
1038793fc096SFrank Li fec_timeout(struct net_device *ndev)
1039793fc096SFrank Li {
1040793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1041793fc096SFrank Li 
1042344756f6SRussell King 	fec_dump(ndev);
1043344756f6SRussell King 
1044793fc096SFrank Li 	ndev->stats.tx_errors++;
1045793fc096SFrank Li 
104636cdc743SRussell King 	schedule_work(&fep->tx_timeout_work);
104754309fa6SFrank Li }
104854309fa6SFrank Li 
104936cdc743SRussell King static void fec_enet_timeout_work(struct work_struct *work)
105054309fa6SFrank Li {
105154309fa6SFrank Li 	struct fec_enet_private *fep =
105236cdc743SRussell King 		container_of(work, struct fec_enet_private, tx_timeout_work);
10538ce5624fSRussell King 	struct net_device *ndev = fep->netdev;
105454309fa6SFrank Li 
1055da1774e5SRussell King 	rtnl_lock();
10568ce5624fSRussell King 	if (netif_device_present(ndev) || netif_running(ndev)) {
1057dbc64a8eSRussell King 		napi_disable(&fep->napi);
1058dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
1059ef83337dSRussell King 		fec_restart(ndev);
10608ce5624fSRussell King 		netif_wake_queue(ndev);
10616af42d42SRussell King 		netif_tx_unlock_bh(ndev);
1062dbc64a8eSRussell King 		napi_enable(&fep->napi);
10638ce5624fSRussell King 	}
1064da1774e5SRussell King 	rtnl_unlock();
106554309fa6SFrank Li }
1066793fc096SFrank Li 
1067793fc096SFrank Li static void
1068*bfd4ecddSRussell King fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1069*bfd4ecddSRussell King 	struct skb_shared_hwtstamps *hwtstamps)
1070*bfd4ecddSRussell King {
1071*bfd4ecddSRussell King 	unsigned long flags;
1072*bfd4ecddSRussell King 	u64 ns;
1073*bfd4ecddSRussell King 
1074*bfd4ecddSRussell King 	spin_lock_irqsave(&fep->tmreg_lock, flags);
1075*bfd4ecddSRussell King 	ns = timecounter_cyc2time(&fep->tc, ts);
1076*bfd4ecddSRussell King 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1077*bfd4ecddSRussell King 
1078*bfd4ecddSRussell King 	memset(hwtstamps, 0, sizeof(*hwtstamps));
1079*bfd4ecddSRussell King 	hwtstamps->hwtstamp = ns_to_ktime(ns);
1080*bfd4ecddSRussell King }
1081*bfd4ecddSRussell King 
1082*bfd4ecddSRussell King static void
1083793fc096SFrank Li fec_enet_tx(struct net_device *ndev)
1084793fc096SFrank Li {
1085793fc096SFrank Li 	struct	fec_enet_private *fep;
1086793fc096SFrank Li 	struct bufdesc *bdp;
1087793fc096SFrank Li 	unsigned short status;
1088793fc096SFrank Li 	struct	sk_buff	*skb;
1089793fc096SFrank Li 	int	index = 0;
109079f33912SNimrod Andy 	int	entries_free;
1091793fc096SFrank Li 
1092793fc096SFrank Li 	fep = netdev_priv(ndev);
1093793fc096SFrank Li 	bdp = fep->dirty_tx;
1094793fc096SFrank Li 
1095793fc096SFrank Li 	/* get next bdp of dirty_tx */
109636e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_nextdesc(bdp, fep);
1097793fc096SFrank Li 
1098793fc096SFrank Li 	while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
1099793fc096SFrank Li 
1100793fc096SFrank Li 		/* current queue is empty */
1101793fc096SFrank Li 		if (bdp == fep->cur_tx)
1102793fc096SFrank Li 			break;
1103793fc096SFrank Li 
110461a4427bSNimrod Andy 		index = fec_enet_get_bd_index(fep->tx_bd_base, bdp, fep);
1105793fc096SFrank Li 
1106793fc096SFrank Li 		skb = fep->tx_skbuff[index];
1107d6bf3143SRussell King 		fep->tx_skbuff[index] = NULL;
110879f33912SNimrod Andy 		if (!IS_TSO_HEADER(fep, bdp->cbd_bufaddr))
110979f33912SNimrod Andy 			dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
111079f33912SNimrod Andy 					bdp->cbd_datlen, DMA_TO_DEVICE);
11112488a54eSSebastian Siewior 		bdp->cbd_bufaddr = 0;
11126e909283SNimrod Andy 		if (!skb) {
11136e909283SNimrod Andy 			bdp = fec_enet_get_nextdesc(bdp, fep);
11146e909283SNimrod Andy 			continue;
11156e909283SNimrod Andy 		}
1116793fc096SFrank Li 
1117793fc096SFrank Li 		/* Check for errors. */
1118793fc096SFrank Li 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1119793fc096SFrank Li 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
1120793fc096SFrank Li 				   BD_ENET_TX_CSL)) {
1121793fc096SFrank Li 			ndev->stats.tx_errors++;
1122793fc096SFrank Li 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
1123793fc096SFrank Li 				ndev->stats.tx_heartbeat_errors++;
1124793fc096SFrank Li 			if (status & BD_ENET_TX_LC)  /* Late collision */
1125793fc096SFrank Li 				ndev->stats.tx_window_errors++;
1126793fc096SFrank Li 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
1127793fc096SFrank Li 				ndev->stats.tx_aborted_errors++;
1128793fc096SFrank Li 			if (status & BD_ENET_TX_UN)  /* Underrun */
1129793fc096SFrank Li 				ndev->stats.tx_fifo_errors++;
1130793fc096SFrank Li 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
1131793fc096SFrank Li 				ndev->stats.tx_carrier_errors++;
1132793fc096SFrank Li 		} else {
1133793fc096SFrank Li 			ndev->stats.tx_packets++;
11346e909283SNimrod Andy 			ndev->stats.tx_bytes += skb->len;
1135793fc096SFrank Li 		}
1136793fc096SFrank Li 
1137793fc096SFrank Li 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1138793fc096SFrank Li 			fep->bufdesc_ex) {
1139793fc096SFrank Li 			struct skb_shared_hwtstamps shhwtstamps;
1140793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1141793fc096SFrank Li 
1142*bfd4ecddSRussell King 			fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
1143793fc096SFrank Li 			skb_tstamp_tx(skb, &shhwtstamps);
1144793fc096SFrank Li 		}
1145793fc096SFrank Li 
1146793fc096SFrank Li 		/* Deferred means some collisions occurred during transmit,
1147793fc096SFrank Li 		 * but we eventually sent the packet OK.
1148793fc096SFrank Li 		 */
1149793fc096SFrank Li 		if (status & BD_ENET_TX_DEF)
1150793fc096SFrank Li 			ndev->stats.collisions++;
1151793fc096SFrank Li 
1152793fc096SFrank Li 		/* Free the sk buffer associated with this last transmit */
1153793fc096SFrank Li 		dev_kfree_skb_any(skb);
1154793fc096SFrank Li 
1155793fc096SFrank Li 		fep->dirty_tx = bdp;
1156793fc096SFrank Li 
1157793fc096SFrank Li 		/* Update pointer to next buffer descriptor to be transmitted */
115836e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
1159793fc096SFrank Li 
1160793fc096SFrank Li 		/* Since we have freed up a buffer, the ring is no longer full
1161793fc096SFrank Li 		 */
116279f33912SNimrod Andy 		if (netif_queue_stopped(ndev)) {
116379f33912SNimrod Andy 			entries_free = fec_enet_get_free_txdesc_num(fep);
116479f33912SNimrod Andy 			if (entries_free >= fep->tx_wake_threshold)
1165793fc096SFrank Li 				netif_wake_queue(ndev);
1166793fc096SFrank Li 		}
116779f33912SNimrod Andy 	}
1168ccea2968SRussell King 
1169ccea2968SRussell King 	/* ERR006538: Keep the transmitter going */
1170ccea2968SRussell King 	if (bdp != fep->cur_tx && readl(fep->hwp + FEC_X_DES_ACTIVE) == 0)
1171ccea2968SRussell King 		writel(0, fep->hwp + FEC_X_DES_ACTIVE);
1172793fc096SFrank Li }
1173793fc096SFrank Li 
1174793fc096SFrank Li /* During a receive, the cur_rx points to the current incoming buffer.
1175793fc096SFrank Li  * When we update through the ring, if the next incoming buffer has
1176793fc096SFrank Li  * not been given to the system, we just set the empty indicator,
1177793fc096SFrank Li  * effectively tossing the packet.
1178793fc096SFrank Li  */
1179793fc096SFrank Li static int
1180793fc096SFrank Li fec_enet_rx(struct net_device *ndev, int budget)
1181793fc096SFrank Li {
1182793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1183793fc096SFrank Li 	const struct platform_device_id *id_entry =
1184793fc096SFrank Li 				platform_get_device_id(fep->pdev);
1185793fc096SFrank Li 	struct bufdesc *bdp;
1186793fc096SFrank Li 	unsigned short status;
1187793fc096SFrank Li 	struct	sk_buff	*skb;
1188793fc096SFrank Li 	ushort	pkt_len;
1189793fc096SFrank Li 	__u8 *data;
1190793fc096SFrank Li 	int	pkt_received = 0;
1191cdffcf1bSJim Baxter 	struct	bufdesc_ex *ebdp = NULL;
1192cdffcf1bSJim Baxter 	bool	vlan_packet_rcvd = false;
1193cdffcf1bSJim Baxter 	u16	vlan_tag;
1194d842a31fSDuan Fugang-B38611 	int	index = 0;
1195793fc096SFrank Li 
1196793fc096SFrank Li #ifdef CONFIG_M532x
1197793fc096SFrank Li 	flush_cache_all();
1198793fc096SFrank Li #endif
1199793fc096SFrank Li 
1200793fc096SFrank Li 	/* First, grab all of the stats for the incoming packet.
1201793fc096SFrank Li 	 * These get messed up if we get called due to a busy condition.
1202793fc096SFrank Li 	 */
1203793fc096SFrank Li 	bdp = fep->cur_rx;
1204793fc096SFrank Li 
1205793fc096SFrank Li 	while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
1206793fc096SFrank Li 
1207793fc096SFrank Li 		if (pkt_received >= budget)
1208793fc096SFrank Li 			break;
1209793fc096SFrank Li 		pkt_received++;
1210793fc096SFrank Li 
1211793fc096SFrank Li 		/* Since we have allocated space to hold a complete frame,
1212793fc096SFrank Li 		 * the last indicator should be set.
1213793fc096SFrank Li 		 */
1214793fc096SFrank Li 		if ((status & BD_ENET_RX_LAST) == 0)
121531b7720cSJoe Perches 			netdev_err(ndev, "rcv is not +last\n");
1216793fc096SFrank Li 
1217db3421c1SRussell King 		writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
1218db3421c1SRussell King 
1219793fc096SFrank Li 		/* Check for errors. */
1220793fc096SFrank Li 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1221793fc096SFrank Li 			   BD_ENET_RX_CR | BD_ENET_RX_OV)) {
1222793fc096SFrank Li 			ndev->stats.rx_errors++;
1223793fc096SFrank Li 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1224793fc096SFrank Li 				/* Frame too long or too short. */
1225793fc096SFrank Li 				ndev->stats.rx_length_errors++;
1226793fc096SFrank Li 			}
1227793fc096SFrank Li 			if (status & BD_ENET_RX_NO)	/* Frame alignment */
1228793fc096SFrank Li 				ndev->stats.rx_frame_errors++;
1229793fc096SFrank Li 			if (status & BD_ENET_RX_CR)	/* CRC Error */
1230793fc096SFrank Li 				ndev->stats.rx_crc_errors++;
1231793fc096SFrank Li 			if (status & BD_ENET_RX_OV)	/* FIFO overrun */
1232793fc096SFrank Li 				ndev->stats.rx_fifo_errors++;
1233793fc096SFrank Li 		}
1234793fc096SFrank Li 
1235793fc096SFrank Li 		/* Report late collisions as a frame error.
1236793fc096SFrank Li 		 * On this error, the BD is closed, but we don't know what we
1237793fc096SFrank Li 		 * have in the buffer.  So, just drop this frame on the floor.
1238793fc096SFrank Li 		 */
1239793fc096SFrank Li 		if (status & BD_ENET_RX_CL) {
1240793fc096SFrank Li 			ndev->stats.rx_errors++;
1241793fc096SFrank Li 			ndev->stats.rx_frame_errors++;
1242793fc096SFrank Li 			goto rx_processing_done;
1243793fc096SFrank Li 		}
1244793fc096SFrank Li 
1245793fc096SFrank Li 		/* Process the incoming frame. */
1246793fc096SFrank Li 		ndev->stats.rx_packets++;
1247793fc096SFrank Li 		pkt_len = bdp->cbd_datlen;
1248793fc096SFrank Li 		ndev->stats.rx_bytes += pkt_len;
1249793fc096SFrank Li 
125061a4427bSNimrod Andy 		index = fec_enet_get_bd_index(fep->rx_bd_base, bdp, fep);
1251d842a31fSDuan Fugang-B38611 		data = fep->rx_skbuff[index]->data;
1252d842a31fSDuan Fugang-B38611 		dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1253d842a31fSDuan Fugang-B38611 					FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1254793fc096SFrank Li 
1255793fc096SFrank Li 		if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1256793fc096SFrank Li 			swap_buffer(data, pkt_len);
1257793fc096SFrank Li 
1258cdffcf1bSJim Baxter 		/* Extract the enhanced buffer descriptor */
1259cdffcf1bSJim Baxter 		ebdp = NULL;
1260cdffcf1bSJim Baxter 		if (fep->bufdesc_ex)
1261cdffcf1bSJim Baxter 			ebdp = (struct bufdesc_ex *)bdp;
1262cdffcf1bSJim Baxter 
1263cdffcf1bSJim Baxter 		/* If this is a VLAN packet remove the VLAN Tag */
1264cdffcf1bSJim Baxter 		vlan_packet_rcvd = false;
1265cdffcf1bSJim Baxter 		if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1266cdffcf1bSJim Baxter 		    fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
1267cdffcf1bSJim Baxter 			/* Push and remove the vlan tag */
1268cdffcf1bSJim Baxter 			struct vlan_hdr *vlan_header =
1269cdffcf1bSJim Baxter 					(struct vlan_hdr *) (data + ETH_HLEN);
1270cdffcf1bSJim Baxter 			vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1271cdffcf1bSJim Baxter 			pkt_len -= VLAN_HLEN;
1272cdffcf1bSJim Baxter 
1273cdffcf1bSJim Baxter 			vlan_packet_rcvd = true;
1274cdffcf1bSJim Baxter 		}
1275cdffcf1bSJim Baxter 
1276793fc096SFrank Li 		/* This does 16 byte alignment, exactly what we need.
1277793fc096SFrank Li 		 * The packet length includes FCS, but we don't want to
1278793fc096SFrank Li 		 * include that when passing upstream as it messes up
1279793fc096SFrank Li 		 * bridging applications.
1280793fc096SFrank Li 		 */
1281793fc096SFrank Li 		skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
1282793fc096SFrank Li 
1283793fc096SFrank Li 		if (unlikely(!skb)) {
1284793fc096SFrank Li 			ndev->stats.rx_dropped++;
1285793fc096SFrank Li 		} else {
1286cdffcf1bSJim Baxter 			int payload_offset = (2 * ETH_ALEN);
1287793fc096SFrank Li 			skb_reserve(skb, NET_IP_ALIGN);
1288793fc096SFrank Li 			skb_put(skb, pkt_len - 4);	/* Make room */
1289cdffcf1bSJim Baxter 
1290cdffcf1bSJim Baxter 			/* Extract the frame data without the VLAN header. */
1291cdffcf1bSJim Baxter 			skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
1292cdffcf1bSJim Baxter 			if (vlan_packet_rcvd)
1293cdffcf1bSJim Baxter 				payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
1294cdffcf1bSJim Baxter 			skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
1295cdffcf1bSJim Baxter 						       data + payload_offset,
1296cdffcf1bSJim Baxter 						       pkt_len - 4 - (2 * ETH_ALEN));
1297cdffcf1bSJim Baxter 
1298793fc096SFrank Li 			skb->protocol = eth_type_trans(skb, ndev);
1299793fc096SFrank Li 
1300793fc096SFrank Li 			/* Get receive timestamp from the skb */
1301*bfd4ecddSRussell King 			if (fep->hwts_rx_en && fep->bufdesc_ex)
1302*bfd4ecddSRussell King 				fec_enet_hwtstamp(fep, ebdp->ts,
1303*bfd4ecddSRussell King 						  skb_hwtstamps(skb));
1304793fc096SFrank Li 
13054c09eed9SJim Baxter 			if (fep->bufdesc_ex &&
13064c09eed9SJim Baxter 			    (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
13074c09eed9SJim Baxter 				if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
13084c09eed9SJim Baxter 					/* don't check it */
13094c09eed9SJim Baxter 					skb->ip_summed = CHECKSUM_UNNECESSARY;
13104c09eed9SJim Baxter 				} else {
13114c09eed9SJim Baxter 					skb_checksum_none_assert(skb);
13124c09eed9SJim Baxter 				}
13134c09eed9SJim Baxter 			}
13144c09eed9SJim Baxter 
1315cdffcf1bSJim Baxter 			/* Handle received VLAN packets */
1316cdffcf1bSJim Baxter 			if (vlan_packet_rcvd)
1317cdffcf1bSJim Baxter 				__vlan_hwaccel_put_tag(skb,
1318cdffcf1bSJim Baxter 						       htons(ETH_P_8021Q),
1319cdffcf1bSJim Baxter 						       vlan_tag);
1320cdffcf1bSJim Baxter 
1321793fc096SFrank Li 			napi_gro_receive(&fep->napi, skb);
1322793fc096SFrank Li 		}
1323793fc096SFrank Li 
1324d842a31fSDuan Fugang-B38611 		dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1325d842a31fSDuan Fugang-B38611 					FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1326793fc096SFrank Li rx_processing_done:
1327793fc096SFrank Li 		/* Clear the status flags for this buffer */
1328793fc096SFrank Li 		status &= ~BD_ENET_RX_STATS;
1329793fc096SFrank Li 
1330793fc096SFrank Li 		/* Mark the buffer empty */
1331793fc096SFrank Li 		status |= BD_ENET_RX_EMPTY;
1332793fc096SFrank Li 		bdp->cbd_sc = status;
1333793fc096SFrank Li 
1334793fc096SFrank Li 		if (fep->bufdesc_ex) {
1335793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1336793fc096SFrank Li 
1337793fc096SFrank Li 			ebdp->cbd_esc = BD_ENET_RX_INT;
1338793fc096SFrank Li 			ebdp->cbd_prot = 0;
1339793fc096SFrank Li 			ebdp->cbd_bdu = 0;
1340793fc096SFrank Li 		}
1341793fc096SFrank Li 
1342793fc096SFrank Li 		/* Update BD pointer to next entry */
134336e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
134436e24e2eSDuan Fugang-B38611 
1345793fc096SFrank Li 		/* Doing this here will keep the FEC running while we process
1346793fc096SFrank Li 		 * incoming frames.  On a heavily loaded network, we should be
1347793fc096SFrank Li 		 * able to keep up at the expense of system resources.
1348793fc096SFrank Li 		 */
1349793fc096SFrank Li 		writel(0, fep->hwp + FEC_R_DES_ACTIVE);
1350793fc096SFrank Li 	}
1351793fc096SFrank Li 	fep->cur_rx = bdp;
1352793fc096SFrank Li 
1353793fc096SFrank Li 	return pkt_received;
1354793fc096SFrank Li }
1355793fc096SFrank Li 
1356793fc096SFrank Li static irqreturn_t
1357793fc096SFrank Li fec_enet_interrupt(int irq, void *dev_id)
1358793fc096SFrank Li {
1359793fc096SFrank Li 	struct net_device *ndev = dev_id;
1360793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
13617a16807cSRussell King 	const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
1362793fc096SFrank Li 	uint int_events;
1363793fc096SFrank Li 	irqreturn_t ret = IRQ_NONE;
1364793fc096SFrank Li 
1365793fc096SFrank Li 	int_events = readl(fep->hwp + FEC_IEVENT);
13667a16807cSRussell King 	writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
1367793fc096SFrank Li 
13687a16807cSRussell King 	if (int_events & napi_mask) {
1369793fc096SFrank Li 		ret = IRQ_HANDLED;
1370793fc096SFrank Li 
13717a16807cSRussell King 		/* Disable the NAPI interrupts */
13727a16807cSRussell King 		writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
13737a16807cSRussell King 		napi_schedule(&fep->napi);
1374793fc096SFrank Li 	}
1375793fc096SFrank Li 
1376793fc096SFrank Li 	if (int_events & FEC_ENET_MII) {
1377793fc096SFrank Li 		ret = IRQ_HANDLED;
1378793fc096SFrank Li 		complete(&fep->mdio_done);
1379793fc096SFrank Li 	}
1380793fc096SFrank Li 
1381793fc096SFrank Li 	return ret;
1382793fc096SFrank Li }
1383793fc096SFrank Li 
1384793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1385793fc096SFrank Li {
1386793fc096SFrank Li 	struct net_device *ndev = napi->dev;
1387793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
13887a16807cSRussell King 	int pkts;
13897a16807cSRussell King 
13907a16807cSRussell King 	/*
13917a16807cSRussell King 	 * Clear any pending transmit or receive interrupts before
13927a16807cSRussell King 	 * processing the rings to avoid racing with the hardware.
13937a16807cSRussell King 	 */
13947a16807cSRussell King 	writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
13957a16807cSRussell King 
13967a16807cSRussell King 	pkts = fec_enet_rx(ndev, budget);
1397793fc096SFrank Li 
1398793fc096SFrank Li 	fec_enet_tx(ndev);
1399793fc096SFrank Li 
1400793fc096SFrank Li 	if (pkts < budget) {
1401793fc096SFrank Li 		napi_complete(napi);
1402793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1403793fc096SFrank Li 	}
1404793fc096SFrank Li 	return pkts;
1405793fc096SFrank Li }
1406793fc096SFrank Li 
1407793fc096SFrank Li /* ------------------------------------------------------------------------- */
1408793fc096SFrank Li static void fec_get_mac(struct net_device *ndev)
1409793fc096SFrank Li {
1410793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
141194660ba0SJingoo Han 	struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1412793fc096SFrank Li 	unsigned char *iap, tmpaddr[ETH_ALEN];
1413793fc096SFrank Li 
1414793fc096SFrank Li 	/*
1415793fc096SFrank Li 	 * try to get mac address in following order:
1416793fc096SFrank Li 	 *
1417793fc096SFrank Li 	 * 1) module parameter via kernel command line in form
1418793fc096SFrank Li 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1419793fc096SFrank Li 	 */
1420793fc096SFrank Li 	iap = macaddr;
1421793fc096SFrank Li 
1422793fc096SFrank Li 	/*
1423793fc096SFrank Li 	 * 2) from device tree data
1424793fc096SFrank Li 	 */
1425793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1426793fc096SFrank Li 		struct device_node *np = fep->pdev->dev.of_node;
1427793fc096SFrank Li 		if (np) {
1428793fc096SFrank Li 			const char *mac = of_get_mac_address(np);
1429793fc096SFrank Li 			if (mac)
1430793fc096SFrank Li 				iap = (unsigned char *) mac;
1431793fc096SFrank Li 		}
1432793fc096SFrank Li 	}
1433793fc096SFrank Li 
1434793fc096SFrank Li 	/*
1435793fc096SFrank Li 	 * 3) from flash or fuse (via platform data)
1436793fc096SFrank Li 	 */
1437793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1438793fc096SFrank Li #ifdef CONFIG_M5272
1439793fc096SFrank Li 		if (FEC_FLASHMAC)
1440793fc096SFrank Li 			iap = (unsigned char *)FEC_FLASHMAC;
1441793fc096SFrank Li #else
1442793fc096SFrank Li 		if (pdata)
1443793fc096SFrank Li 			iap = (unsigned char *)&pdata->mac;
1444793fc096SFrank Li #endif
1445793fc096SFrank Li 	}
1446793fc096SFrank Li 
1447793fc096SFrank Li 	/*
1448793fc096SFrank Li 	 * 4) FEC mac registers set by bootloader
1449793fc096SFrank Li 	 */
1450793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
14517d7628f3SDan Carpenter 		*((__be32 *) &tmpaddr[0]) =
14527d7628f3SDan Carpenter 			cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
14537d7628f3SDan Carpenter 		*((__be16 *) &tmpaddr[4]) =
14547d7628f3SDan Carpenter 			cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1455793fc096SFrank Li 		iap = &tmpaddr[0];
1456793fc096SFrank Li 	}
1457793fc096SFrank Li 
1458ff5b2fabSLucas Stach 	/*
1459ff5b2fabSLucas Stach 	 * 5) random mac address
1460ff5b2fabSLucas Stach 	 */
1461ff5b2fabSLucas Stach 	if (!is_valid_ether_addr(iap)) {
1462ff5b2fabSLucas Stach 		/* Report it and use a random ethernet address instead */
1463ff5b2fabSLucas Stach 		netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1464ff5b2fabSLucas Stach 		eth_hw_addr_random(ndev);
1465ff5b2fabSLucas Stach 		netdev_info(ndev, "Using random MAC address: %pM\n",
1466ff5b2fabSLucas Stach 			    ndev->dev_addr);
1467ff5b2fabSLucas Stach 		return;
1468ff5b2fabSLucas Stach 	}
1469ff5b2fabSLucas Stach 
1470793fc096SFrank Li 	memcpy(ndev->dev_addr, iap, ETH_ALEN);
1471793fc096SFrank Li 
1472793fc096SFrank Li 	/* Adjust MAC if using macaddr */
1473793fc096SFrank Li 	if (iap == macaddr)
1474793fc096SFrank Li 		 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1475793fc096SFrank Li }
1476793fc096SFrank Li 
1477793fc096SFrank Li /* ------------------------------------------------------------------------- */
1478793fc096SFrank Li 
1479793fc096SFrank Li /*
1480793fc096SFrank Li  * Phy section
1481793fc096SFrank Li  */
1482793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev)
1483793fc096SFrank Li {
1484793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1485793fc096SFrank Li 	struct phy_device *phy_dev = fep->phy_dev;
1486793fc096SFrank Li 	int status_change = 0;
1487793fc096SFrank Li 
1488793fc096SFrank Li 	/* Prevent a state halted on mii error */
1489793fc096SFrank Li 	if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1490793fc096SFrank Li 		phy_dev->state = PHY_RESUMING;
149154309fa6SFrank Li 		return;
1492793fc096SFrank Li 	}
1493793fc096SFrank Li 
14948ce5624fSRussell King 	/*
14958ce5624fSRussell King 	 * If the netdev is down, or is going down, we're not interested
14968ce5624fSRussell King 	 * in link state events, so just mark our idea of the link as down
14978ce5624fSRussell King 	 * and ignore the event.
14988ce5624fSRussell King 	 */
14998ce5624fSRussell King 	if (!netif_running(ndev) || !netif_device_present(ndev)) {
15008ce5624fSRussell King 		fep->link = 0;
15018ce5624fSRussell King 	} else if (phy_dev->link) {
1502793fc096SFrank Li 		if (!fep->link) {
1503793fc096SFrank Li 			fep->link = phy_dev->link;
1504793fc096SFrank Li 			status_change = 1;
1505793fc096SFrank Li 		}
1506793fc096SFrank Li 
1507ef83337dSRussell King 		if (fep->full_duplex != phy_dev->duplex) {
1508ef83337dSRussell King 			fep->full_duplex = phy_dev->duplex;
1509793fc096SFrank Li 			status_change = 1;
1510ef83337dSRussell King 		}
1511793fc096SFrank Li 
1512793fc096SFrank Li 		if (phy_dev->speed != fep->speed) {
1513793fc096SFrank Li 			fep->speed = phy_dev->speed;
1514793fc096SFrank Li 			status_change = 1;
1515793fc096SFrank Li 		}
1516793fc096SFrank Li 
1517793fc096SFrank Li 		/* if any of the above changed restart the FEC */
1518dbc64a8eSRussell King 		if (status_change) {
1519dbc64a8eSRussell King 			napi_disable(&fep->napi);
1520dbc64a8eSRussell King 			netif_tx_lock_bh(ndev);
1521ef83337dSRussell King 			fec_restart(ndev);
1522dbc64a8eSRussell King 			netif_wake_queue(ndev);
15236af42d42SRussell King 			netif_tx_unlock_bh(ndev);
1524dbc64a8eSRussell King 			napi_enable(&fep->napi);
1525dbc64a8eSRussell King 		}
1526793fc096SFrank Li 	} else {
1527793fc096SFrank Li 		if (fep->link) {
1528f208ce10SRussell King 			napi_disable(&fep->napi);
1529f208ce10SRussell King 			netif_tx_lock_bh(ndev);
1530793fc096SFrank Li 			fec_stop(ndev);
1531f208ce10SRussell King 			netif_tx_unlock_bh(ndev);
1532f208ce10SRussell King 			napi_enable(&fep->napi);
15336e0895c2SDavid S. Miller 			fep->link = phy_dev->link;
1534793fc096SFrank Li 			status_change = 1;
1535793fc096SFrank Li 		}
1536793fc096SFrank Li 	}
1537793fc096SFrank Li 
1538793fc096SFrank Li 	if (status_change)
1539793fc096SFrank Li 		phy_print_status(phy_dev);
1540793fc096SFrank Li }
1541793fc096SFrank Li 
1542793fc096SFrank Li static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1543793fc096SFrank Li {
1544793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
1545793fc096SFrank Li 	unsigned long time_left;
1546793fc096SFrank Li 
1547793fc096SFrank Li 	fep->mii_timeout = 0;
1548793fc096SFrank Li 	init_completion(&fep->mdio_done);
1549793fc096SFrank Li 
1550793fc096SFrank Li 	/* start a read op */
1551793fc096SFrank Li 	writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1552793fc096SFrank Li 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1553793fc096SFrank Li 		FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1554793fc096SFrank Li 
1555793fc096SFrank Li 	/* wait for end of transfer */
1556793fc096SFrank Li 	time_left = wait_for_completion_timeout(&fep->mdio_done,
1557793fc096SFrank Li 			usecs_to_jiffies(FEC_MII_TIMEOUT));
1558793fc096SFrank Li 	if (time_left == 0) {
1559793fc096SFrank Li 		fep->mii_timeout = 1;
156031b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO read timeout\n");
1561793fc096SFrank Li 		return -ETIMEDOUT;
1562793fc096SFrank Li 	}
1563793fc096SFrank Li 
1564793fc096SFrank Li 	/* return value */
1565793fc096SFrank Li 	return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1566793fc096SFrank Li }
1567793fc096SFrank Li 
1568793fc096SFrank Li static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1569793fc096SFrank Li 			   u16 value)
1570793fc096SFrank Li {
1571793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
1572793fc096SFrank Li 	unsigned long time_left;
1573793fc096SFrank Li 
1574793fc096SFrank Li 	fep->mii_timeout = 0;
1575793fc096SFrank Li 	init_completion(&fep->mdio_done);
1576793fc096SFrank Li 
1577793fc096SFrank Li 	/* start a write op */
1578793fc096SFrank Li 	writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1579793fc096SFrank Li 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1580793fc096SFrank Li 		FEC_MMFR_TA | FEC_MMFR_DATA(value),
1581793fc096SFrank Li 		fep->hwp + FEC_MII_DATA);
1582793fc096SFrank Li 
1583793fc096SFrank Li 	/* wait for end of transfer */
1584793fc096SFrank Li 	time_left = wait_for_completion_timeout(&fep->mdio_done,
1585793fc096SFrank Li 			usecs_to_jiffies(FEC_MII_TIMEOUT));
1586793fc096SFrank Li 	if (time_left == 0) {
1587793fc096SFrank Li 		fep->mii_timeout = 1;
158831b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO write timeout\n");
1589793fc096SFrank Li 		return -ETIMEDOUT;
1590793fc096SFrank Li 	}
1591793fc096SFrank Li 
1592793fc096SFrank Li 	return 0;
1593793fc096SFrank Li }
1594793fc096SFrank Li 
1595e8fcfcd5SNimrod Andy static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1596e8fcfcd5SNimrod Andy {
1597e8fcfcd5SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
1598e8fcfcd5SNimrod Andy 	int ret;
1599e8fcfcd5SNimrod Andy 
1600e8fcfcd5SNimrod Andy 	if (enable) {
1601e8fcfcd5SNimrod Andy 		ret = clk_prepare_enable(fep->clk_ahb);
1602e8fcfcd5SNimrod Andy 		if (ret)
1603e8fcfcd5SNimrod Andy 			return ret;
1604e8fcfcd5SNimrod Andy 		ret = clk_prepare_enable(fep->clk_ipg);
1605e8fcfcd5SNimrod Andy 		if (ret)
1606e8fcfcd5SNimrod Andy 			goto failed_clk_ipg;
1607e8fcfcd5SNimrod Andy 		if (fep->clk_enet_out) {
1608e8fcfcd5SNimrod Andy 			ret = clk_prepare_enable(fep->clk_enet_out);
1609e8fcfcd5SNimrod Andy 			if (ret)
1610e8fcfcd5SNimrod Andy 				goto failed_clk_enet_out;
1611e8fcfcd5SNimrod Andy 		}
1612e8fcfcd5SNimrod Andy 		if (fep->clk_ptp) {
1613e8fcfcd5SNimrod Andy 			ret = clk_prepare_enable(fep->clk_ptp);
1614e8fcfcd5SNimrod Andy 			if (ret)
1615e8fcfcd5SNimrod Andy 				goto failed_clk_ptp;
1616e8fcfcd5SNimrod Andy 		}
1617e8fcfcd5SNimrod Andy 	} else {
1618e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_ahb);
1619e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_ipg);
1620e8fcfcd5SNimrod Andy 		if (fep->clk_enet_out)
1621e8fcfcd5SNimrod Andy 			clk_disable_unprepare(fep->clk_enet_out);
1622e8fcfcd5SNimrod Andy 		if (fep->clk_ptp)
1623e8fcfcd5SNimrod Andy 			clk_disable_unprepare(fep->clk_ptp);
1624e8fcfcd5SNimrod Andy 	}
1625e8fcfcd5SNimrod Andy 
1626e8fcfcd5SNimrod Andy 	return 0;
1627e8fcfcd5SNimrod Andy failed_clk_ptp:
1628e8fcfcd5SNimrod Andy 	if (fep->clk_enet_out)
1629e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_enet_out);
1630e8fcfcd5SNimrod Andy failed_clk_enet_out:
1631e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_ipg);
1632e8fcfcd5SNimrod Andy failed_clk_ipg:
1633e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_ahb);
1634e8fcfcd5SNimrod Andy 
1635e8fcfcd5SNimrod Andy 	return ret;
1636e8fcfcd5SNimrod Andy }
1637e8fcfcd5SNimrod Andy 
1638793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev)
1639793fc096SFrank Li {
1640793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1641793fc096SFrank Li 	const struct platform_device_id *id_entry =
1642793fc096SFrank Li 				platform_get_device_id(fep->pdev);
1643793fc096SFrank Li 	struct phy_device *phy_dev = NULL;
1644793fc096SFrank Li 	char mdio_bus_id[MII_BUS_ID_SIZE];
1645793fc096SFrank Li 	char phy_name[MII_BUS_ID_SIZE + 3];
1646793fc096SFrank Li 	int phy_id;
1647793fc096SFrank Li 	int dev_id = fep->dev_id;
1648793fc096SFrank Li 
1649793fc096SFrank Li 	fep->phy_dev = NULL;
1650793fc096SFrank Li 
1651793fc096SFrank Li 	/* check for attached phy */
1652793fc096SFrank Li 	for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1653793fc096SFrank Li 		if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1654793fc096SFrank Li 			continue;
1655793fc096SFrank Li 		if (fep->mii_bus->phy_map[phy_id] == NULL)
1656793fc096SFrank Li 			continue;
1657793fc096SFrank Li 		if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1658793fc096SFrank Li 			continue;
1659793fc096SFrank Li 		if (dev_id--)
1660793fc096SFrank Li 			continue;
1661793fc096SFrank Li 		strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1662793fc096SFrank Li 		break;
1663793fc096SFrank Li 	}
1664793fc096SFrank Li 
1665793fc096SFrank Li 	if (phy_id >= PHY_MAX_ADDR) {
166631b7720cSJoe Perches 		netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1667793fc096SFrank Li 		strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1668793fc096SFrank Li 		phy_id = 0;
1669793fc096SFrank Li 	}
1670793fc096SFrank Li 
1671793fc096SFrank Li 	snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
1672793fc096SFrank Li 	phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1673793fc096SFrank Li 			      fep->phy_interface);
1674793fc096SFrank Li 	if (IS_ERR(phy_dev)) {
167531b7720cSJoe Perches 		netdev_err(ndev, "could not attach to PHY\n");
1676793fc096SFrank Li 		return PTR_ERR(phy_dev);
1677793fc096SFrank Li 	}
1678793fc096SFrank Li 
1679793fc096SFrank Li 	/* mask with MAC supported features */
1680793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
1681793fc096SFrank Li 		phy_dev->supported &= PHY_GBIT_FEATURES;
1682b44592ffSRussell King 		phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
1683d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1684793fc096SFrank Li 		phy_dev->supported |= SUPPORTED_Pause;
1685d1391930SGuenter Roeck #endif
1686793fc096SFrank Li 	}
1687793fc096SFrank Li 	else
1688793fc096SFrank Li 		phy_dev->supported &= PHY_BASIC_FEATURES;
1689793fc096SFrank Li 
1690793fc096SFrank Li 	phy_dev->advertising = phy_dev->supported;
1691793fc096SFrank Li 
1692793fc096SFrank Li 	fep->phy_dev = phy_dev;
1693793fc096SFrank Li 	fep->link = 0;
1694793fc096SFrank Li 	fep->full_duplex = 0;
1695793fc096SFrank Li 
169631b7720cSJoe Perches 	netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1697793fc096SFrank Li 		    fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1698793fc096SFrank Li 		    fep->phy_dev->irq);
1699793fc096SFrank Li 
1700793fc096SFrank Li 	return 0;
1701793fc096SFrank Li }
1702793fc096SFrank Li 
1703793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev)
1704793fc096SFrank Li {
1705793fc096SFrank Li 	static struct mii_bus *fec0_mii_bus;
1706793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
1707793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1708793fc096SFrank Li 	const struct platform_device_id *id_entry =
1709793fc096SFrank Li 				platform_get_device_id(fep->pdev);
1710793fc096SFrank Li 	int err = -ENXIO, i;
1711793fc096SFrank Li 
1712793fc096SFrank Li 	/*
1713793fc096SFrank Li 	 * The dual fec interfaces are not equivalent with enet-mac.
1714793fc096SFrank Li 	 * Here are the differences:
1715793fc096SFrank Li 	 *
1716793fc096SFrank Li 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
1717793fc096SFrank Li 	 *  - fec0 acts as the 1588 time master while fec1 is slave
1718793fc096SFrank Li 	 *  - external phys can only be configured by fec0
1719793fc096SFrank Li 	 *
1720793fc096SFrank Li 	 * That is to say fec1 can not work independently. It only works
1721793fc096SFrank Li 	 * when fec0 is working. The reason behind this design is that the
1722793fc096SFrank Li 	 * second interface is added primarily for Switch mode.
1723793fc096SFrank Li 	 *
1724793fc096SFrank Li 	 * Because of the last point above, both phys are attached on fec0
1725793fc096SFrank Li 	 * mdio interface in board design, and need to be configured by
1726793fc096SFrank Li 	 * fec0 mii_bus.
1727793fc096SFrank Li 	 */
1728793fc096SFrank Li 	if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
1729793fc096SFrank Li 		/* fec1 uses fec0 mii_bus */
1730793fc096SFrank Li 		if (mii_cnt && fec0_mii_bus) {
1731793fc096SFrank Li 			fep->mii_bus = fec0_mii_bus;
1732793fc096SFrank Li 			mii_cnt++;
1733793fc096SFrank Li 			return 0;
1734793fc096SFrank Li 		}
1735793fc096SFrank Li 		return -ENOENT;
1736793fc096SFrank Li 	}
1737793fc096SFrank Li 
1738793fc096SFrank Li 	fep->mii_timeout = 0;
1739793fc096SFrank Li 
1740793fc096SFrank Li 	/*
1741793fc096SFrank Li 	 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1742793fc096SFrank Li 	 *
1743793fc096SFrank Li 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1744793fc096SFrank Li 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
1745793fc096SFrank Li 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1746793fc096SFrank Li 	 * document.
1747793fc096SFrank Li 	 */
174898a6eeb8SNimrod Andy 	fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
1749793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1750793fc096SFrank Li 		fep->phy_speed--;
1751793fc096SFrank Li 	fep->phy_speed <<= 1;
1752793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1753793fc096SFrank Li 
1754793fc096SFrank Li 	fep->mii_bus = mdiobus_alloc();
1755793fc096SFrank Li 	if (fep->mii_bus == NULL) {
1756793fc096SFrank Li 		err = -ENOMEM;
1757793fc096SFrank Li 		goto err_out;
1758793fc096SFrank Li 	}
1759793fc096SFrank Li 
1760793fc096SFrank Li 	fep->mii_bus->name = "fec_enet_mii_bus";
1761793fc096SFrank Li 	fep->mii_bus->read = fec_enet_mdio_read;
1762793fc096SFrank Li 	fep->mii_bus->write = fec_enet_mdio_write;
1763793fc096SFrank Li 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1764793fc096SFrank Li 		pdev->name, fep->dev_id + 1);
1765793fc096SFrank Li 	fep->mii_bus->priv = fep;
1766793fc096SFrank Li 	fep->mii_bus->parent = &pdev->dev;
1767793fc096SFrank Li 
1768793fc096SFrank Li 	fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1769793fc096SFrank Li 	if (!fep->mii_bus->irq) {
1770793fc096SFrank Li 		err = -ENOMEM;
1771793fc096SFrank Li 		goto err_out_free_mdiobus;
1772793fc096SFrank Li 	}
1773793fc096SFrank Li 
1774793fc096SFrank Li 	for (i = 0; i < PHY_MAX_ADDR; i++)
1775793fc096SFrank Li 		fep->mii_bus->irq[i] = PHY_POLL;
1776793fc096SFrank Li 
1777793fc096SFrank Li 	if (mdiobus_register(fep->mii_bus))
1778793fc096SFrank Li 		goto err_out_free_mdio_irq;
1779793fc096SFrank Li 
1780793fc096SFrank Li 	mii_cnt++;
1781793fc096SFrank Li 
1782793fc096SFrank Li 	/* save fec0 mii_bus */
1783793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1784793fc096SFrank Li 		fec0_mii_bus = fep->mii_bus;
1785793fc096SFrank Li 
1786793fc096SFrank Li 	return 0;
1787793fc096SFrank Li 
1788793fc096SFrank Li err_out_free_mdio_irq:
1789793fc096SFrank Li 	kfree(fep->mii_bus->irq);
1790793fc096SFrank Li err_out_free_mdiobus:
1791793fc096SFrank Li 	mdiobus_free(fep->mii_bus);
1792793fc096SFrank Li err_out:
1793793fc096SFrank Li 	return err;
1794793fc096SFrank Li }
1795793fc096SFrank Li 
1796793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep)
1797793fc096SFrank Li {
1798793fc096SFrank Li 	if (--mii_cnt == 0) {
1799793fc096SFrank Li 		mdiobus_unregister(fep->mii_bus);
1800793fc096SFrank Li 		kfree(fep->mii_bus->irq);
1801793fc096SFrank Li 		mdiobus_free(fep->mii_bus);
1802793fc096SFrank Li 	}
1803793fc096SFrank Li }
1804793fc096SFrank Li 
1805793fc096SFrank Li static int fec_enet_get_settings(struct net_device *ndev,
1806793fc096SFrank Li 				  struct ethtool_cmd *cmd)
1807793fc096SFrank Li {
1808793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1809793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
1810793fc096SFrank Li 
1811793fc096SFrank Li 	if (!phydev)
1812793fc096SFrank Li 		return -ENODEV;
1813793fc096SFrank Li 
1814793fc096SFrank Li 	return phy_ethtool_gset(phydev, cmd);
1815793fc096SFrank Li }
1816793fc096SFrank Li 
1817793fc096SFrank Li static int fec_enet_set_settings(struct net_device *ndev,
1818793fc096SFrank Li 				 struct ethtool_cmd *cmd)
1819793fc096SFrank Li {
1820793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1821793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
1822793fc096SFrank Li 
1823793fc096SFrank Li 	if (!phydev)
1824793fc096SFrank Li 		return -ENODEV;
1825793fc096SFrank Li 
1826793fc096SFrank Li 	return phy_ethtool_sset(phydev, cmd);
1827793fc096SFrank Li }
1828793fc096SFrank Li 
1829793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev,
1830793fc096SFrank Li 				 struct ethtool_drvinfo *info)
1831793fc096SFrank Li {
1832793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1833793fc096SFrank Li 
1834793fc096SFrank Li 	strlcpy(info->driver, fep->pdev->dev.driver->name,
1835793fc096SFrank Li 		sizeof(info->driver));
1836793fc096SFrank Li 	strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1837793fc096SFrank Li 	strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
1838793fc096SFrank Li }
1839793fc096SFrank Li 
1840793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev,
1841793fc096SFrank Li 				struct ethtool_ts_info *info)
1842793fc096SFrank Li {
1843793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1844793fc096SFrank Li 
1845793fc096SFrank Li 	if (fep->bufdesc_ex) {
1846793fc096SFrank Li 
1847793fc096SFrank Li 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1848793fc096SFrank Li 					SOF_TIMESTAMPING_RX_SOFTWARE |
1849793fc096SFrank Li 					SOF_TIMESTAMPING_SOFTWARE |
1850793fc096SFrank Li 					SOF_TIMESTAMPING_TX_HARDWARE |
1851793fc096SFrank Li 					SOF_TIMESTAMPING_RX_HARDWARE |
1852793fc096SFrank Li 					SOF_TIMESTAMPING_RAW_HARDWARE;
1853793fc096SFrank Li 		if (fep->ptp_clock)
1854793fc096SFrank Li 			info->phc_index = ptp_clock_index(fep->ptp_clock);
1855793fc096SFrank Li 		else
1856793fc096SFrank Li 			info->phc_index = -1;
1857793fc096SFrank Li 
1858793fc096SFrank Li 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1859793fc096SFrank Li 				 (1 << HWTSTAMP_TX_ON);
1860793fc096SFrank Li 
1861793fc096SFrank Li 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1862793fc096SFrank Li 				   (1 << HWTSTAMP_FILTER_ALL);
1863793fc096SFrank Li 		return 0;
1864793fc096SFrank Li 	} else {
1865793fc096SFrank Li 		return ethtool_op_get_ts_info(ndev, info);
1866793fc096SFrank Li 	}
1867793fc096SFrank Li }
1868793fc096SFrank Li 
1869d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1870d1391930SGuenter Roeck 
1871793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev,
1872793fc096SFrank Li 				    struct ethtool_pauseparam *pause)
1873793fc096SFrank Li {
1874793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1875793fc096SFrank Li 
1876793fc096SFrank Li 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1877793fc096SFrank Li 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1878793fc096SFrank Li 	pause->rx_pause = pause->tx_pause;
1879793fc096SFrank Li }
1880793fc096SFrank Li 
1881793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev,
1882793fc096SFrank Li 				   struct ethtool_pauseparam *pause)
1883793fc096SFrank Li {
1884793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1885793fc096SFrank Li 
18860b146ca8SRussell King 	if (!fep->phy_dev)
18870b146ca8SRussell King 		return -ENODEV;
18880b146ca8SRussell King 
1889793fc096SFrank Li 	if (pause->tx_pause != pause->rx_pause) {
1890793fc096SFrank Li 		netdev_info(ndev,
1891793fc096SFrank Li 			"hardware only support enable/disable both tx and rx");
1892793fc096SFrank Li 		return -EINVAL;
1893793fc096SFrank Li 	}
1894793fc096SFrank Li 
1895793fc096SFrank Li 	fep->pause_flag = 0;
1896793fc096SFrank Li 
1897793fc096SFrank Li 	/* tx pause must be same as rx pause */
1898793fc096SFrank Li 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1899793fc096SFrank Li 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1900793fc096SFrank Li 
1901793fc096SFrank Li 	if (pause->rx_pause || pause->autoneg) {
1902793fc096SFrank Li 		fep->phy_dev->supported |= ADVERTISED_Pause;
1903793fc096SFrank Li 		fep->phy_dev->advertising |= ADVERTISED_Pause;
1904793fc096SFrank Li 	} else {
1905793fc096SFrank Li 		fep->phy_dev->supported &= ~ADVERTISED_Pause;
1906793fc096SFrank Li 		fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1907793fc096SFrank Li 	}
1908793fc096SFrank Li 
1909793fc096SFrank Li 	if (pause->autoneg) {
1910793fc096SFrank Li 		if (netif_running(ndev))
1911793fc096SFrank Li 			fec_stop(ndev);
1912793fc096SFrank Li 		phy_start_aneg(fep->phy_dev);
1913793fc096SFrank Li 	}
1914dbc64a8eSRussell King 	if (netif_running(ndev)) {
1915dbc64a8eSRussell King 		napi_disable(&fep->napi);
1916dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
1917ef83337dSRussell King 		fec_restart(ndev);
1918dbc64a8eSRussell King 		netif_wake_queue(ndev);
19196af42d42SRussell King 		netif_tx_unlock_bh(ndev);
1920dbc64a8eSRussell King 		napi_enable(&fep->napi);
1921dbc64a8eSRussell King 	}
1922793fc096SFrank Li 
1923793fc096SFrank Li 	return 0;
1924793fc096SFrank Li }
1925793fc096SFrank Li 
192638ae92dcSChris Healy static const struct fec_stat {
192738ae92dcSChris Healy 	char name[ETH_GSTRING_LEN];
192838ae92dcSChris Healy 	u16 offset;
192938ae92dcSChris Healy } fec_stats[] = {
193038ae92dcSChris Healy 	/* RMON TX */
193138ae92dcSChris Healy 	{ "tx_dropped", RMON_T_DROP },
193238ae92dcSChris Healy 	{ "tx_packets", RMON_T_PACKETS },
193338ae92dcSChris Healy 	{ "tx_broadcast", RMON_T_BC_PKT },
193438ae92dcSChris Healy 	{ "tx_multicast", RMON_T_MC_PKT },
193538ae92dcSChris Healy 	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
193638ae92dcSChris Healy 	{ "tx_undersize", RMON_T_UNDERSIZE },
193738ae92dcSChris Healy 	{ "tx_oversize", RMON_T_OVERSIZE },
193838ae92dcSChris Healy 	{ "tx_fragment", RMON_T_FRAG },
193938ae92dcSChris Healy 	{ "tx_jabber", RMON_T_JAB },
194038ae92dcSChris Healy 	{ "tx_collision", RMON_T_COL },
194138ae92dcSChris Healy 	{ "tx_64byte", RMON_T_P64 },
194238ae92dcSChris Healy 	{ "tx_65to127byte", RMON_T_P65TO127 },
194338ae92dcSChris Healy 	{ "tx_128to255byte", RMON_T_P128TO255 },
194438ae92dcSChris Healy 	{ "tx_256to511byte", RMON_T_P256TO511 },
194538ae92dcSChris Healy 	{ "tx_512to1023byte", RMON_T_P512TO1023 },
194638ae92dcSChris Healy 	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
194738ae92dcSChris Healy 	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
194838ae92dcSChris Healy 	{ "tx_octets", RMON_T_OCTETS },
194938ae92dcSChris Healy 
195038ae92dcSChris Healy 	/* IEEE TX */
195138ae92dcSChris Healy 	{ "IEEE_tx_drop", IEEE_T_DROP },
195238ae92dcSChris Healy 	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
195338ae92dcSChris Healy 	{ "IEEE_tx_1col", IEEE_T_1COL },
195438ae92dcSChris Healy 	{ "IEEE_tx_mcol", IEEE_T_MCOL },
195538ae92dcSChris Healy 	{ "IEEE_tx_def", IEEE_T_DEF },
195638ae92dcSChris Healy 	{ "IEEE_tx_lcol", IEEE_T_LCOL },
195738ae92dcSChris Healy 	{ "IEEE_tx_excol", IEEE_T_EXCOL },
195838ae92dcSChris Healy 	{ "IEEE_tx_macerr", IEEE_T_MACERR },
195938ae92dcSChris Healy 	{ "IEEE_tx_cserr", IEEE_T_CSERR },
196038ae92dcSChris Healy 	{ "IEEE_tx_sqe", IEEE_T_SQE },
196138ae92dcSChris Healy 	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
196238ae92dcSChris Healy 	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
196338ae92dcSChris Healy 
196438ae92dcSChris Healy 	/* RMON RX */
196538ae92dcSChris Healy 	{ "rx_packets", RMON_R_PACKETS },
196638ae92dcSChris Healy 	{ "rx_broadcast", RMON_R_BC_PKT },
196738ae92dcSChris Healy 	{ "rx_multicast", RMON_R_MC_PKT },
196838ae92dcSChris Healy 	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
196938ae92dcSChris Healy 	{ "rx_undersize", RMON_R_UNDERSIZE },
197038ae92dcSChris Healy 	{ "rx_oversize", RMON_R_OVERSIZE },
197138ae92dcSChris Healy 	{ "rx_fragment", RMON_R_FRAG },
197238ae92dcSChris Healy 	{ "rx_jabber", RMON_R_JAB },
197338ae92dcSChris Healy 	{ "rx_64byte", RMON_R_P64 },
197438ae92dcSChris Healy 	{ "rx_65to127byte", RMON_R_P65TO127 },
197538ae92dcSChris Healy 	{ "rx_128to255byte", RMON_R_P128TO255 },
197638ae92dcSChris Healy 	{ "rx_256to511byte", RMON_R_P256TO511 },
197738ae92dcSChris Healy 	{ "rx_512to1023byte", RMON_R_P512TO1023 },
197838ae92dcSChris Healy 	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
197938ae92dcSChris Healy 	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
198038ae92dcSChris Healy 	{ "rx_octets", RMON_R_OCTETS },
198138ae92dcSChris Healy 
198238ae92dcSChris Healy 	/* IEEE RX */
198338ae92dcSChris Healy 	{ "IEEE_rx_drop", IEEE_R_DROP },
198438ae92dcSChris Healy 	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
198538ae92dcSChris Healy 	{ "IEEE_rx_crc", IEEE_R_CRC },
198638ae92dcSChris Healy 	{ "IEEE_rx_align", IEEE_R_ALIGN },
198738ae92dcSChris Healy 	{ "IEEE_rx_macerr", IEEE_R_MACERR },
198838ae92dcSChris Healy 	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
198938ae92dcSChris Healy 	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
199038ae92dcSChris Healy };
199138ae92dcSChris Healy 
199238ae92dcSChris Healy static void fec_enet_get_ethtool_stats(struct net_device *dev,
199338ae92dcSChris Healy 	struct ethtool_stats *stats, u64 *data)
199438ae92dcSChris Healy {
199538ae92dcSChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
199638ae92dcSChris Healy 	int i;
199738ae92dcSChris Healy 
199838ae92dcSChris Healy 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
199938ae92dcSChris Healy 		data[i] = readl(fep->hwp + fec_stats[i].offset);
200038ae92dcSChris Healy }
200138ae92dcSChris Healy 
200238ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev,
200338ae92dcSChris Healy 	u32 stringset, u8 *data)
200438ae92dcSChris Healy {
200538ae92dcSChris Healy 	int i;
200638ae92dcSChris Healy 	switch (stringset) {
200738ae92dcSChris Healy 	case ETH_SS_STATS:
200838ae92dcSChris Healy 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
200938ae92dcSChris Healy 			memcpy(data + i * ETH_GSTRING_LEN,
201038ae92dcSChris Healy 				fec_stats[i].name, ETH_GSTRING_LEN);
201138ae92dcSChris Healy 		break;
201238ae92dcSChris Healy 	}
201338ae92dcSChris Healy }
201438ae92dcSChris Healy 
201538ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset)
201638ae92dcSChris Healy {
201738ae92dcSChris Healy 	switch (sset) {
201838ae92dcSChris Healy 	case ETH_SS_STATS:
201938ae92dcSChris Healy 		return ARRAY_SIZE(fec_stats);
202038ae92dcSChris Healy 	default:
202138ae92dcSChris Healy 		return -EOPNOTSUPP;
202238ae92dcSChris Healy 	}
202338ae92dcSChris Healy }
2024d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
202538ae92dcSChris Healy 
202632bc9b46SChris Healy static int fec_enet_nway_reset(struct net_device *dev)
202732bc9b46SChris Healy {
202832bc9b46SChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
202932bc9b46SChris Healy 	struct phy_device *phydev = fep->phy_dev;
203032bc9b46SChris Healy 
203132bc9b46SChris Healy 	if (!phydev)
203232bc9b46SChris Healy 		return -ENODEV;
203332bc9b46SChris Healy 
203432bc9b46SChris Healy 	return genphy_restart_aneg(phydev);
203532bc9b46SChris Healy }
203632bc9b46SChris Healy 
2037793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = {
2038793fc096SFrank Li 	.get_settings		= fec_enet_get_settings,
2039793fc096SFrank Li 	.set_settings		= fec_enet_set_settings,
2040793fc096SFrank Li 	.get_drvinfo		= fec_enet_get_drvinfo,
204132bc9b46SChris Healy 	.nway_reset		= fec_enet_nway_reset,
2042c1d7c48fSRussell King 	.get_link		= ethtool_op_get_link,
204338ae92dcSChris Healy #ifndef CONFIG_M5272
2044c1d7c48fSRussell King 	.get_pauseparam		= fec_enet_get_pauseparam,
2045c1d7c48fSRussell King 	.set_pauseparam		= fec_enet_set_pauseparam,
204638ae92dcSChris Healy 	.get_strings		= fec_enet_get_strings,
2047c1d7c48fSRussell King 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
204838ae92dcSChris Healy 	.get_sset_count		= fec_enet_get_sset_count,
204938ae92dcSChris Healy #endif
2050c1d7c48fSRussell King 	.get_ts_info		= fec_enet_get_ts_info,
2051793fc096SFrank Li };
2052793fc096SFrank Li 
2053793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2054793fc096SFrank Li {
2055793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2056793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
2057793fc096SFrank Li 
2058793fc096SFrank Li 	if (!netif_running(ndev))
2059793fc096SFrank Li 		return -EINVAL;
2060793fc096SFrank Li 
2061793fc096SFrank Li 	if (!phydev)
2062793fc096SFrank Li 		return -ENODEV;
2063793fc096SFrank Li 
20641d5244d0SBen Hutchings 	if (fep->bufdesc_ex) {
20651d5244d0SBen Hutchings 		if (cmd == SIOCSHWTSTAMP)
20661d5244d0SBen Hutchings 			return fec_ptp_set(ndev, rq);
20671d5244d0SBen Hutchings 		if (cmd == SIOCGHWTSTAMP)
20681d5244d0SBen Hutchings 			return fec_ptp_get(ndev, rq);
20691d5244d0SBen Hutchings 	}
2070793fc096SFrank Li 
2071793fc096SFrank Li 	return phy_mii_ioctl(phydev, rq, cmd);
2072793fc096SFrank Li }
2073793fc096SFrank Li 
2074793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev)
2075793fc096SFrank Li {
2076793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2077793fc096SFrank Li 	unsigned int i;
2078793fc096SFrank Li 	struct sk_buff *skb;
2079793fc096SFrank Li 	struct bufdesc	*bdp;
2080793fc096SFrank Li 
2081793fc096SFrank Li 	bdp = fep->rx_bd_base;
208236e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->rx_ring_size; i++) {
2083793fc096SFrank Li 		skb = fep->rx_skbuff[i];
2084730ee360SRussell King 		fep->rx_skbuff[i] = NULL;
2085730ee360SRussell King 		if (skb) {
2086793fc096SFrank Li 			dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
2087793fc096SFrank Li 					FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
2088793fc096SFrank Li 			dev_kfree_skb(skb);
2089730ee360SRussell King 		}
209036e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
2091793fc096SFrank Li 	}
2092793fc096SFrank Li 
2093793fc096SFrank Li 	bdp = fep->tx_bd_base;
20948b7c9efaSRussell King 	for (i = 0; i < fep->tx_ring_size; i++) {
2095793fc096SFrank Li 		kfree(fep->tx_bounce[i]);
20968b7c9efaSRussell King 		fep->tx_bounce[i] = NULL;
20978b7c9efaSRussell King 		skb = fep->tx_skbuff[i];
20988b7c9efaSRussell King 		fep->tx_skbuff[i] = NULL;
20998b7c9efaSRussell King 		dev_kfree_skb(skb);
21008b7c9efaSRussell King 	}
2101793fc096SFrank Li }
2102793fc096SFrank Li 
2103793fc096SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev)
2104793fc096SFrank Li {
2105793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2106793fc096SFrank Li 	unsigned int i;
2107793fc096SFrank Li 	struct sk_buff *skb;
2108793fc096SFrank Li 	struct bufdesc	*bdp;
2109793fc096SFrank Li 
2110793fc096SFrank Li 	bdp = fep->rx_bd_base;
211136e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->rx_ring_size; i++) {
2112730ee360SRussell King 		dma_addr_t addr;
2113730ee360SRussell King 
2114793fc096SFrank Li 		skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
2115ffdce2ccSRussell King 		if (!skb)
2116ffdce2ccSRussell King 			goto err_alloc;
2117793fc096SFrank Li 
2118730ee360SRussell King 		addr = dma_map_single(&fep->pdev->dev, skb->data,
2119793fc096SFrank Li 				FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
2120730ee360SRussell King 		if (dma_mapping_error(&fep->pdev->dev, addr)) {
2121730ee360SRussell King 			dev_kfree_skb(skb);
2122d842a31fSDuan Fugang-B38611 			if (net_ratelimit())
2123d842a31fSDuan Fugang-B38611 				netdev_err(ndev, "Rx DMA memory map failed\n");
2124ffdce2ccSRussell King 			goto err_alloc;
2125d842a31fSDuan Fugang-B38611 		}
2126730ee360SRussell King 
2127730ee360SRussell King 		fep->rx_skbuff[i] = skb;
2128730ee360SRussell King 		bdp->cbd_bufaddr = addr;
2129793fc096SFrank Li 		bdp->cbd_sc = BD_ENET_RX_EMPTY;
2130793fc096SFrank Li 
2131793fc096SFrank Li 		if (fep->bufdesc_ex) {
2132793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2133793fc096SFrank Li 			ebdp->cbd_esc = BD_ENET_RX_INT;
2134793fc096SFrank Li 		}
2135793fc096SFrank Li 
213636e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
2137793fc096SFrank Li 	}
2138793fc096SFrank Li 
2139793fc096SFrank Li 	/* Set the last buffer to wrap. */
214036e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_prevdesc(bdp, fep);
2141793fc096SFrank Li 	bdp->cbd_sc |= BD_SC_WRAP;
2142793fc096SFrank Li 
2143793fc096SFrank Li 	bdp = fep->tx_bd_base;
214436e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->tx_ring_size; i++) {
2145793fc096SFrank Li 		fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2146ffdce2ccSRussell King 		if (!fep->tx_bounce[i])
2147ffdce2ccSRussell King 			goto err_alloc;
2148793fc096SFrank Li 
2149793fc096SFrank Li 		bdp->cbd_sc = 0;
2150793fc096SFrank Li 		bdp->cbd_bufaddr = 0;
2151793fc096SFrank Li 
2152793fc096SFrank Li 		if (fep->bufdesc_ex) {
2153793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
215496d2222bSJim Baxter 			ebdp->cbd_esc = BD_ENET_TX_INT;
2155793fc096SFrank Li 		}
2156793fc096SFrank Li 
215736e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
2158793fc096SFrank Li 	}
2159793fc096SFrank Li 
2160793fc096SFrank Li 	/* Set the last buffer to wrap. */
216136e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_prevdesc(bdp, fep);
2162793fc096SFrank Li 	bdp->cbd_sc |= BD_SC_WRAP;
2163793fc096SFrank Li 
2164793fc096SFrank Li 	return 0;
2165ffdce2ccSRussell King 
2166ffdce2ccSRussell King  err_alloc:
2167ffdce2ccSRussell King 	fec_enet_free_buffers(ndev);
2168ffdce2ccSRussell King 	return -ENOMEM;
2169793fc096SFrank Li }
2170793fc096SFrank Li 
2171793fc096SFrank Li static int
2172793fc096SFrank Li fec_enet_open(struct net_device *ndev)
2173793fc096SFrank Li {
2174793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2175793fc096SFrank Li 	int ret;
2176793fc096SFrank Li 
21775bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&fep->pdev->dev);
2178e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
2179e8fcfcd5SNimrod Andy 	if (ret)
2180e8fcfcd5SNimrod Andy 		return ret;
2181e8fcfcd5SNimrod Andy 
2182793fc096SFrank Li 	/* I should reset the ring buffers here, but I don't yet know
2183793fc096SFrank Li 	 * a simple way to do that.
2184793fc096SFrank Li 	 */
2185793fc096SFrank Li 
2186793fc096SFrank Li 	ret = fec_enet_alloc_buffers(ndev);
2187793fc096SFrank Li 	if (ret)
2188793fc096SFrank Li 		return ret;
2189793fc096SFrank Li 
2190793fc096SFrank Li 	/* Probe and connect to PHY when open the interface */
2191793fc096SFrank Li 	ret = fec_enet_mii_probe(ndev);
2192793fc096SFrank Li 	if (ret) {
2193793fc096SFrank Li 		fec_enet_free_buffers(ndev);
2194793fc096SFrank Li 		return ret;
2195793fc096SFrank Li 	}
2196ce5eaf02SRussell King 
2197ef83337dSRussell King 	fec_restart(ndev);
2198ce5eaf02SRussell King 	napi_enable(&fep->napi);
2199793fc096SFrank Li 	phy_start(fep->phy_dev);
2200793fc096SFrank Li 	netif_start_queue(ndev);
2201793fc096SFrank Li 	return 0;
2202793fc096SFrank Li }
2203793fc096SFrank Li 
2204793fc096SFrank Li static int
2205793fc096SFrank Li fec_enet_close(struct net_device *ndev)
2206793fc096SFrank Li {
2207793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2208793fc096SFrank Li 
2209d76cfae9SRussell King 	phy_stop(fep->phy_dev);
2210d76cfae9SRussell King 
221131a6de34SRussell King 	if (netif_device_present(ndev)) {
2212793fc096SFrank Li 		napi_disable(&fep->napi);
2213b49cd504SRussell King 		netif_tx_disable(ndev);
2214793fc096SFrank Li 		fec_stop(ndev);
221531a6de34SRussell King 	}
2216793fc096SFrank Li 
2217793fc096SFrank Li 	phy_disconnect(fep->phy_dev);
22180b146ca8SRussell King 	fep->phy_dev = NULL;
2219793fc096SFrank Li 
2220e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
22215bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2222793fc096SFrank Li 	fec_enet_free_buffers(ndev);
2223793fc096SFrank Li 
2224793fc096SFrank Li 	return 0;
2225793fc096SFrank Li }
2226793fc096SFrank Li 
2227793fc096SFrank Li /* Set or clear the multicast filter for this adaptor.
2228793fc096SFrank Li  * Skeleton taken from sunlance driver.
2229793fc096SFrank Li  * The CPM Ethernet implementation allows Multicast as well as individual
2230793fc096SFrank Li  * MAC address filtering.  Some of the drivers check to make sure it is
2231793fc096SFrank Li  * a group multicast address, and discard those that are not.  I guess I
2232793fc096SFrank Li  * will do the same for now, but just remove the test if you want
2233793fc096SFrank Li  * individual filtering as well (do the upper net layers want or support
2234793fc096SFrank Li  * this kind of feature?).
2235793fc096SFrank Li  */
2236793fc096SFrank Li 
2237793fc096SFrank Li #define HASH_BITS	6		/* #bits in hash */
2238793fc096SFrank Li #define CRC32_POLY	0xEDB88320
2239793fc096SFrank Li 
2240793fc096SFrank Li static void set_multicast_list(struct net_device *ndev)
2241793fc096SFrank Li {
2242793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2243793fc096SFrank Li 	struct netdev_hw_addr *ha;
2244793fc096SFrank Li 	unsigned int i, bit, data, crc, tmp;
2245793fc096SFrank Li 	unsigned char hash;
2246793fc096SFrank Li 
2247793fc096SFrank Li 	if (ndev->flags & IFF_PROMISC) {
2248793fc096SFrank Li 		tmp = readl(fep->hwp + FEC_R_CNTRL);
2249793fc096SFrank Li 		tmp |= 0x8;
2250793fc096SFrank Li 		writel(tmp, fep->hwp + FEC_R_CNTRL);
2251793fc096SFrank Li 		return;
2252793fc096SFrank Li 	}
2253793fc096SFrank Li 
2254793fc096SFrank Li 	tmp = readl(fep->hwp + FEC_R_CNTRL);
2255793fc096SFrank Li 	tmp &= ~0x8;
2256793fc096SFrank Li 	writel(tmp, fep->hwp + FEC_R_CNTRL);
2257793fc096SFrank Li 
2258793fc096SFrank Li 	if (ndev->flags & IFF_ALLMULTI) {
2259793fc096SFrank Li 		/* Catch all multicast addresses, so set the
2260793fc096SFrank Li 		 * filter to all 1's
2261793fc096SFrank Li 		 */
2262793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2263793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2264793fc096SFrank Li 
2265793fc096SFrank Li 		return;
2266793fc096SFrank Li 	}
2267793fc096SFrank Li 
2268793fc096SFrank Li 	/* Clear filter and add the addresses in hash register
2269793fc096SFrank Li 	 */
2270793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2271793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2272793fc096SFrank Li 
2273793fc096SFrank Li 	netdev_for_each_mc_addr(ha, ndev) {
2274793fc096SFrank Li 		/* calculate crc32 value of mac address */
2275793fc096SFrank Li 		crc = 0xffffffff;
2276793fc096SFrank Li 
2277793fc096SFrank Li 		for (i = 0; i < ndev->addr_len; i++) {
2278793fc096SFrank Li 			data = ha->addr[i];
2279793fc096SFrank Li 			for (bit = 0; bit < 8; bit++, data >>= 1) {
2280793fc096SFrank Li 				crc = (crc >> 1) ^
2281793fc096SFrank Li 				(((crc ^ data) & 1) ? CRC32_POLY : 0);
2282793fc096SFrank Li 			}
2283793fc096SFrank Li 		}
2284793fc096SFrank Li 
2285793fc096SFrank Li 		/* only upper 6 bits (HASH_BITS) are used
2286793fc096SFrank Li 		 * which point to specific bit in he hash registers
2287793fc096SFrank Li 		 */
2288793fc096SFrank Li 		hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2289793fc096SFrank Li 
2290793fc096SFrank Li 		if (hash > 31) {
2291793fc096SFrank Li 			tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2292793fc096SFrank Li 			tmp |= 1 << (hash - 32);
2293793fc096SFrank Li 			writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2294793fc096SFrank Li 		} else {
2295793fc096SFrank Li 			tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2296793fc096SFrank Li 			tmp |= 1 << hash;
2297793fc096SFrank Li 			writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2298793fc096SFrank Li 		}
2299793fc096SFrank Li 	}
2300793fc096SFrank Li }
2301793fc096SFrank Li 
2302793fc096SFrank Li /* Set a MAC change in hardware. */
2303793fc096SFrank Li static int
2304793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p)
2305793fc096SFrank Li {
2306793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2307793fc096SFrank Li 	struct sockaddr *addr = p;
2308793fc096SFrank Li 
230944934facSLucas Stach 	if (addr) {
2310793fc096SFrank Li 		if (!is_valid_ether_addr(addr->sa_data))
2311793fc096SFrank Li 			return -EADDRNOTAVAIL;
2312793fc096SFrank Li 		memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
231344934facSLucas Stach 	}
2314793fc096SFrank Li 
2315793fc096SFrank Li 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2316793fc096SFrank Li 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
2317793fc096SFrank Li 		fep->hwp + FEC_ADDR_LOW);
2318793fc096SFrank Li 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
2319793fc096SFrank Li 		fep->hwp + FEC_ADDR_HIGH);
2320793fc096SFrank Li 	return 0;
2321793fc096SFrank Li }
2322793fc096SFrank Li 
2323793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
2324793fc096SFrank Li /**
2325793fc096SFrank Li  * fec_poll_controller - FEC Poll controller function
2326793fc096SFrank Li  * @dev: The FEC network adapter
2327793fc096SFrank Li  *
2328793fc096SFrank Li  * Polled functionality used by netconsole and others in non interrupt mode
2329793fc096SFrank Li  *
2330793fc096SFrank Li  */
2331793fc096SFrank Li static void fec_poll_controller(struct net_device *dev)
2332793fc096SFrank Li {
2333793fc096SFrank Li 	int i;
2334793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(dev);
2335793fc096SFrank Li 
2336793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
2337793fc096SFrank Li 		if (fep->irq[i] > 0) {
2338793fc096SFrank Li 			disable_irq(fep->irq[i]);
2339793fc096SFrank Li 			fec_enet_interrupt(fep->irq[i], dev);
2340793fc096SFrank Li 			enable_irq(fep->irq[i]);
2341793fc096SFrank Li 		}
2342793fc096SFrank Li 	}
2343793fc096SFrank Li }
2344793fc096SFrank Li #endif
2345793fc096SFrank Li 
23468506fa1dSRussell King #define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
23478506fa1dSRussell King 
23484c09eed9SJim Baxter static int fec_set_features(struct net_device *netdev,
23494c09eed9SJim Baxter 	netdev_features_t features)
23504c09eed9SJim Baxter {
23514c09eed9SJim Baxter 	struct fec_enet_private *fep = netdev_priv(netdev);
23524c09eed9SJim Baxter 	netdev_features_t changed = features ^ netdev->features;
23534c09eed9SJim Baxter 
23548506fa1dSRussell King 	/* Quiesce the device if necessary */
23558506fa1dSRussell King 	if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
23568506fa1dSRussell King 		napi_disable(&fep->napi);
23578506fa1dSRussell King 		netif_tx_lock_bh(netdev);
23588506fa1dSRussell King 		fec_stop(netdev);
23598506fa1dSRussell King 	}
23608506fa1dSRussell King 
23614c09eed9SJim Baxter 	netdev->features = features;
23624c09eed9SJim Baxter 
23634c09eed9SJim Baxter 	/* Receive checksum has been changed */
23644c09eed9SJim Baxter 	if (changed & NETIF_F_RXCSUM) {
23654c09eed9SJim Baxter 		if (features & NETIF_F_RXCSUM)
23664c09eed9SJim Baxter 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
23674c09eed9SJim Baxter 		else
23684c09eed9SJim Baxter 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
23698506fa1dSRussell King 	}
23704c09eed9SJim Baxter 
23718506fa1dSRussell King 	/* Resume the device after updates */
23728506fa1dSRussell King 	if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2373ef83337dSRussell King 		fec_restart(netdev);
23744c09eed9SJim Baxter 		netif_wake_queue(netdev);
23756af42d42SRussell King 		netif_tx_unlock_bh(netdev);
2376dbc64a8eSRussell King 		napi_enable(&fep->napi);
23774c09eed9SJim Baxter 	}
23784c09eed9SJim Baxter 
23794c09eed9SJim Baxter 	return 0;
23804c09eed9SJim Baxter }
23814c09eed9SJim Baxter 
2382793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = {
2383793fc096SFrank Li 	.ndo_open		= fec_enet_open,
2384793fc096SFrank Li 	.ndo_stop		= fec_enet_close,
2385793fc096SFrank Li 	.ndo_start_xmit		= fec_enet_start_xmit,
2386793fc096SFrank Li 	.ndo_set_rx_mode	= set_multicast_list,
2387793fc096SFrank Li 	.ndo_change_mtu		= eth_change_mtu,
2388793fc096SFrank Li 	.ndo_validate_addr	= eth_validate_addr,
2389793fc096SFrank Li 	.ndo_tx_timeout		= fec_timeout,
2390793fc096SFrank Li 	.ndo_set_mac_address	= fec_set_mac_address,
2391793fc096SFrank Li 	.ndo_do_ioctl		= fec_enet_ioctl,
2392793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
2393793fc096SFrank Li 	.ndo_poll_controller	= fec_poll_controller,
2394793fc096SFrank Li #endif
23954c09eed9SJim Baxter 	.ndo_set_features	= fec_set_features,
2396793fc096SFrank Li };
2397793fc096SFrank Li 
2398793fc096SFrank Li  /*
2399793fc096SFrank Li   * XXX:  We need to clean up on failure exits here.
2400793fc096SFrank Li   *
2401793fc096SFrank Li   */
2402793fc096SFrank Li static int fec_enet_init(struct net_device *ndev)
2403793fc096SFrank Li {
2404793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
240548496255SShawn Guo 	const struct platform_device_id *id_entry =
240648496255SShawn Guo 				platform_get_device_id(fep->pdev);
2407793fc096SFrank Li 	struct bufdesc *cbd_base;
240855d0218aSNimrod Andy 	int bd_size;
240955d0218aSNimrod Andy 
241055d0218aSNimrod Andy 	/* init the tx & rx ring size */
241155d0218aSNimrod Andy 	fep->tx_ring_size = TX_RING_SIZE;
241255d0218aSNimrod Andy 	fep->rx_ring_size = RX_RING_SIZE;
241355d0218aSNimrod Andy 
241479f33912SNimrod Andy 	fep->tx_stop_threshold = FEC_MAX_SKB_DESCS;
241579f33912SNimrod Andy 	fep->tx_wake_threshold = (fep->tx_ring_size - fep->tx_stop_threshold) / 2;
241679f33912SNimrod Andy 
241755d0218aSNimrod Andy 	if (fep->bufdesc_ex)
241855d0218aSNimrod Andy 		fep->bufdesc_size = sizeof(struct bufdesc_ex);
241955d0218aSNimrod Andy 	else
242055d0218aSNimrod Andy 		fep->bufdesc_size = sizeof(struct bufdesc);
242155d0218aSNimrod Andy 	bd_size = (fep->tx_ring_size + fep->rx_ring_size) *
242255d0218aSNimrod Andy 			fep->bufdesc_size;
2423793fc096SFrank Li 
2424793fc096SFrank Li 	/* Allocate memory for buffer descriptors. */
242555d0218aSNimrod Andy 	cbd_base = dma_alloc_coherent(NULL, bd_size, &fep->bd_dma,
2426793fc096SFrank Li 				      GFP_KERNEL);
2427793fc096SFrank Li 	if (!cbd_base)
2428793fc096SFrank Li 		return -ENOMEM;
2429793fc096SFrank Li 
243079f33912SNimrod Andy 	fep->tso_hdrs = dma_alloc_coherent(NULL, fep->tx_ring_size * TSO_HEADER_SIZE,
243179f33912SNimrod Andy 						&fep->tso_hdrs_dma, GFP_KERNEL);
243279f33912SNimrod Andy 	if (!fep->tso_hdrs) {
243379f33912SNimrod Andy 		dma_free_coherent(NULL, bd_size, cbd_base, fep->bd_dma);
243479f33912SNimrod Andy 		return -ENOMEM;
243579f33912SNimrod Andy 	}
243679f33912SNimrod Andy 
2437a210576cSDavid S. Miller 	memset(cbd_base, 0, PAGE_SIZE);
2438793fc096SFrank Li 
2439793fc096SFrank Li 	fep->netdev = ndev;
2440793fc096SFrank Li 
2441793fc096SFrank Li 	/* Get the Ethernet address */
2442793fc096SFrank Li 	fec_get_mac(ndev);
244344934facSLucas Stach 	/* make sure MAC we just acquired is programmed into the hw */
244444934facSLucas Stach 	fec_set_mac_address(ndev, NULL);
2445793fc096SFrank Li 
2446793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
2447793fc096SFrank Li 	fep->rx_bd_base = cbd_base;
244855d0218aSNimrod Andy 	if (fep->bufdesc_ex)
2449793fc096SFrank Li 		fep->tx_bd_base = (struct bufdesc *)
245036e24e2eSDuan Fugang-B38611 			(((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
245155d0218aSNimrod Andy 	else
245236e24e2eSDuan Fugang-B38611 		fep->tx_bd_base = cbd_base + fep->rx_ring_size;
2453793fc096SFrank Li 
2454793fc096SFrank Li 	/* The FEC Ethernet specific entries in the device structure */
2455793fc096SFrank Li 	ndev->watchdog_timeo = TX_TIMEOUT;
2456793fc096SFrank Li 	ndev->netdev_ops = &fec_netdev_ops;
2457793fc096SFrank Li 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
2458793fc096SFrank Li 
2459793fc096SFrank Li 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
2460322555f5SFabio Estevam 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
2461793fc096SFrank Li 
246209d1e541SNimrod Andy 	if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
2463cdffcf1bSJim Baxter 		/* enable hw VLAN support */
2464cdffcf1bSJim Baxter 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
2465cdffcf1bSJim Baxter 
246648496255SShawn Guo 	if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
246779f33912SNimrod Andy 		ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
246879f33912SNimrod Andy 
24694c09eed9SJim Baxter 		/* enable hw accelerator */
24704c09eed9SJim Baxter 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
247179f33912SNimrod Andy 				| NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
24724c09eed9SJim Baxter 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
247348496255SShawn Guo 	}
24744c09eed9SJim Baxter 
247509d1e541SNimrod Andy 	ndev->hw_features = ndev->features;
247609d1e541SNimrod Andy 
2477ef83337dSRussell King 	fec_restart(ndev);
2478793fc096SFrank Li 
2479793fc096SFrank Li 	return 0;
2480793fc096SFrank Li }
2481793fc096SFrank Li 
2482793fc096SFrank Li #ifdef CONFIG_OF
2483793fc096SFrank Li static void fec_reset_phy(struct platform_device *pdev)
2484793fc096SFrank Li {
2485793fc096SFrank Li 	int err, phy_reset;
2486793fc096SFrank Li 	int msec = 1;
2487793fc096SFrank Li 	struct device_node *np = pdev->dev.of_node;
2488793fc096SFrank Li 
2489793fc096SFrank Li 	if (!np)
2490793fc096SFrank Li 		return;
2491793fc096SFrank Li 
2492793fc096SFrank Li 	of_property_read_u32(np, "phy-reset-duration", &msec);
2493793fc096SFrank Li 	/* A sane reset duration should not be longer than 1s */
2494793fc096SFrank Li 	if (msec > 1000)
2495793fc096SFrank Li 		msec = 1;
2496793fc096SFrank Li 
2497793fc096SFrank Li 	phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
2498793fc096SFrank Li 	if (!gpio_is_valid(phy_reset))
2499793fc096SFrank Li 		return;
2500793fc096SFrank Li 
2501793fc096SFrank Li 	err = devm_gpio_request_one(&pdev->dev, phy_reset,
2502793fc096SFrank Li 				    GPIOF_OUT_INIT_LOW, "phy-reset");
2503793fc096SFrank Li 	if (err) {
2504793fc096SFrank Li 		dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
2505793fc096SFrank Li 		return;
2506793fc096SFrank Li 	}
2507793fc096SFrank Li 	msleep(msec);
2508793fc096SFrank Li 	gpio_set_value(phy_reset, 1);
2509793fc096SFrank Li }
2510793fc096SFrank Li #else /* CONFIG_OF */
2511793fc096SFrank Li static void fec_reset_phy(struct platform_device *pdev)
2512793fc096SFrank Li {
2513793fc096SFrank Li 	/*
2514793fc096SFrank Li 	 * In case of platform probe, the reset has been done
2515793fc096SFrank Li 	 * by machine code.
2516793fc096SFrank Li 	 */
2517793fc096SFrank Li }
2518793fc096SFrank Li #endif /* CONFIG_OF */
2519793fc096SFrank Li 
2520793fc096SFrank Li static int
2521793fc096SFrank Li fec_probe(struct platform_device *pdev)
2522793fc096SFrank Li {
2523793fc096SFrank Li 	struct fec_enet_private *fep;
2524793fc096SFrank Li 	struct fec_platform_data *pdata;
2525793fc096SFrank Li 	struct net_device *ndev;
2526793fc096SFrank Li 	int i, irq, ret = 0;
2527793fc096SFrank Li 	struct resource *r;
2528793fc096SFrank Li 	const struct of_device_id *of_id;
2529793fc096SFrank Li 	static int dev_id;
2530793fc096SFrank Li 
2531793fc096SFrank Li 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
2532793fc096SFrank Li 	if (of_id)
2533793fc096SFrank Li 		pdev->id_entry = of_id->data;
2534793fc096SFrank Li 
2535793fc096SFrank Li 	/* Init network device */
2536793fc096SFrank Li 	ndev = alloc_etherdev(sizeof(struct fec_enet_private));
2537793fc096SFrank Li 	if (!ndev)
2538793fc096SFrank Li 		return -ENOMEM;
2539793fc096SFrank Li 
2540793fc096SFrank Li 	SET_NETDEV_DEV(ndev, &pdev->dev);
2541793fc096SFrank Li 
2542793fc096SFrank Li 	/* setup board info structure */
2543793fc096SFrank Li 	fep = netdev_priv(ndev);
2544793fc096SFrank Li 
2545d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2546793fc096SFrank Li 	/* default enable pause frame auto negotiation */
2547793fc096SFrank Li 	if (pdev->id_entry &&
2548793fc096SFrank Li 	    (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2549793fc096SFrank Li 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
2550d1391930SGuenter Roeck #endif
2551793fc096SFrank Li 
25525bbde4d2SNimrod Andy 	/* Select default pin state */
25535bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&pdev->dev);
25545bbde4d2SNimrod Andy 
2555399db75bSFabio Estevam 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2556941e173aSTushar Behera 	fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2557941e173aSTushar Behera 	if (IS_ERR(fep->hwp)) {
2558941e173aSTushar Behera 		ret = PTR_ERR(fep->hwp);
2559941e173aSTushar Behera 		goto failed_ioremap;
2560941e173aSTushar Behera 	}
2561941e173aSTushar Behera 
2562793fc096SFrank Li 	fep->pdev = pdev;
2563793fc096SFrank Li 	fep->dev_id = dev_id++;
2564793fc096SFrank Li 
2565793fc096SFrank Li 	fep->bufdesc_ex = 0;
2566793fc096SFrank Li 
2567793fc096SFrank Li 	platform_set_drvdata(pdev, ndev);
2568793fc096SFrank Li 
25696c5f7808SGuenter Roeck 	ret = of_get_phy_mode(pdev->dev.of_node);
2570793fc096SFrank Li 	if (ret < 0) {
257194660ba0SJingoo Han 		pdata = dev_get_platdata(&pdev->dev);
2572793fc096SFrank Li 		if (pdata)
2573793fc096SFrank Li 			fep->phy_interface = pdata->phy;
2574793fc096SFrank Li 		else
2575793fc096SFrank Li 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
2576793fc096SFrank Li 	} else {
2577793fc096SFrank Li 		fep->phy_interface = ret;
2578793fc096SFrank Li 	}
2579793fc096SFrank Li 
2580793fc096SFrank Li 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2581793fc096SFrank Li 	if (IS_ERR(fep->clk_ipg)) {
2582793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ipg);
2583793fc096SFrank Li 		goto failed_clk;
2584793fc096SFrank Li 	}
2585793fc096SFrank Li 
2586793fc096SFrank Li 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2587793fc096SFrank Li 	if (IS_ERR(fep->clk_ahb)) {
2588793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ahb);
2589793fc096SFrank Li 		goto failed_clk;
2590793fc096SFrank Li 	}
2591793fc096SFrank Li 
259238f56f33SLinus Torvalds 	/* enet_out is optional, depends on board */
259338f56f33SLinus Torvalds 	fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
259438f56f33SLinus Torvalds 	if (IS_ERR(fep->clk_enet_out))
259538f56f33SLinus Torvalds 		fep->clk_enet_out = NULL;
259638f56f33SLinus Torvalds 
2597793fc096SFrank Li 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
2598793fc096SFrank Li 	fep->bufdesc_ex =
2599793fc096SFrank Li 		pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
2600793fc096SFrank Li 	if (IS_ERR(fep->clk_ptp)) {
260138f56f33SLinus Torvalds 		fep->clk_ptp = NULL;
2602793fc096SFrank Li 		fep->bufdesc_ex = 0;
2603793fc096SFrank Li 	}
2604793fc096SFrank Li 
2605e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
260613a097bdSFabio Estevam 	if (ret)
260713a097bdSFabio Estevam 		goto failed_clk;
260813a097bdSFabio Estevam 
2609f4e9f3d2SFabio Estevam 	fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2610f4e9f3d2SFabio Estevam 	if (!IS_ERR(fep->reg_phy)) {
2611f4e9f3d2SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
2612793fc096SFrank Li 		if (ret) {
2613793fc096SFrank Li 			dev_err(&pdev->dev,
2614793fc096SFrank Li 				"Failed to enable phy regulator: %d\n", ret);
2615793fc096SFrank Li 			goto failed_regulator;
2616793fc096SFrank Li 		}
2617f6a4d607SFabio Estevam 	} else {
2618f6a4d607SFabio Estevam 		fep->reg_phy = NULL;
2619793fc096SFrank Li 	}
2620793fc096SFrank Li 
2621793fc096SFrank Li 	fec_reset_phy(pdev);
2622793fc096SFrank Li 
2623793fc096SFrank Li 	if (fep->bufdesc_ex)
2624ca162a82SFabio Estevam 		fec_ptp_init(pdev);
2625793fc096SFrank Li 
2626793fc096SFrank Li 	ret = fec_enet_init(ndev);
2627793fc096SFrank Li 	if (ret)
2628793fc096SFrank Li 		goto failed_init;
2629793fc096SFrank Li 
2630793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
2631793fc096SFrank Li 		irq = platform_get_irq(pdev, i);
2632793fc096SFrank Li 		if (irq < 0) {
2633793fc096SFrank Li 			if (i)
2634793fc096SFrank Li 				break;
2635793fc096SFrank Li 			ret = irq;
2636793fc096SFrank Li 			goto failed_irq;
2637793fc096SFrank Li 		}
26380d9b2ab1SFabio Estevam 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
263944a272ddSMichael Opdenacker 				       0, pdev->name, ndev);
26400d9b2ab1SFabio Estevam 		if (ret)
2641793fc096SFrank Li 			goto failed_irq;
2642793fc096SFrank Li 	}
2643793fc096SFrank Li 
2644793fc096SFrank Li 	ret = fec_enet_mii_init(pdev);
2645793fc096SFrank Li 	if (ret)
2646793fc096SFrank Li 		goto failed_mii_init;
2647793fc096SFrank Li 
2648793fc096SFrank Li 	/* Carrier starts down, phylib will bring it up */
2649793fc096SFrank Li 	netif_carrier_off(ndev);
2650e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
26515bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&pdev->dev);
2652793fc096SFrank Li 
2653793fc096SFrank Li 	ret = register_netdev(ndev);
2654793fc096SFrank Li 	if (ret)
2655793fc096SFrank Li 		goto failed_register;
2656793fc096SFrank Li 
2657eb1d0640SFabio Estevam 	if (fep->bufdesc_ex && fep->ptp_clock)
2658eb1d0640SFabio Estevam 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2659eb1d0640SFabio Estevam 
266036cdc743SRussell King 	INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
2661793fc096SFrank Li 	return 0;
2662793fc096SFrank Li 
2663793fc096SFrank Li failed_register:
2664793fc096SFrank Li 	fec_enet_mii_remove(fep);
2665793fc096SFrank Li failed_mii_init:
26667a2bbd8dSFabio Estevam failed_irq:
26677a2bbd8dSFabio Estevam failed_init:
2668f6a4d607SFabio Estevam 	if (fep->reg_phy)
2669f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
2670793fc096SFrank Li failed_regulator:
2671e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
2672793fc096SFrank Li failed_clk:
2673793fc096SFrank Li failed_ioremap:
2674793fc096SFrank Li 	free_netdev(ndev);
2675793fc096SFrank Li 
2676793fc096SFrank Li 	return ret;
2677793fc096SFrank Li }
2678793fc096SFrank Li 
2679793fc096SFrank Li static int
2680793fc096SFrank Li fec_drv_remove(struct platform_device *pdev)
2681793fc096SFrank Li {
2682793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
2683793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2684793fc096SFrank Li 
268536cdc743SRussell King 	cancel_work_sync(&fep->tx_timeout_work);
2686793fc096SFrank Li 	unregister_netdev(ndev);
2687793fc096SFrank Li 	fec_enet_mii_remove(fep);
2688793fc096SFrank Li 	del_timer_sync(&fep->time_keep);
2689f6a4d607SFabio Estevam 	if (fep->reg_phy)
2690f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
2691793fc096SFrank Li 	if (fep->ptp_clock)
2692793fc096SFrank Li 		ptp_clock_unregister(fep->ptp_clock);
2693e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
2694793fc096SFrank Li 	free_netdev(ndev);
2695793fc096SFrank Li 
2696793fc096SFrank Li 	return 0;
2697793fc096SFrank Li }
2698793fc096SFrank Li 
2699bf7bfd7fSFabio Estevam #ifdef CONFIG_PM_SLEEP
2700793fc096SFrank Li static int
2701793fc096SFrank Li fec_suspend(struct device *dev)
2702793fc096SFrank Li {
2703793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
2704793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2705793fc096SFrank Li 
2706da1774e5SRussell King 	rtnl_lock();
2707793fc096SFrank Li 	if (netif_running(ndev)) {
2708d76cfae9SRussell King 		phy_stop(fep->phy_dev);
270931a6de34SRussell King 		napi_disable(&fep->napi);
271031a6de34SRussell King 		netif_tx_lock_bh(ndev);
2711793fc096SFrank Li 		netif_device_detach(ndev);
271231a6de34SRussell King 		netif_tx_unlock_bh(ndev);
271331a6de34SRussell King 		fec_stop(ndev);
2714793fc096SFrank Li 	}
2715da1774e5SRussell King 	rtnl_unlock();
2716da1774e5SRussell King 
2717e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
27185bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2719793fc096SFrank Li 
2720238f7bc7SFabio Estevam 	if (fep->reg_phy)
2721238f7bc7SFabio Estevam 		regulator_disable(fep->reg_phy);
2722238f7bc7SFabio Estevam 
2723793fc096SFrank Li 	return 0;
2724793fc096SFrank Li }
2725793fc096SFrank Li 
2726793fc096SFrank Li static int
2727793fc096SFrank Li fec_resume(struct device *dev)
2728793fc096SFrank Li {
2729793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
2730793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2731238f7bc7SFabio Estevam 	int ret;
2732238f7bc7SFabio Estevam 
2733238f7bc7SFabio Estevam 	if (fep->reg_phy) {
2734238f7bc7SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
2735238f7bc7SFabio Estevam 		if (ret)
2736238f7bc7SFabio Estevam 			return ret;
2737238f7bc7SFabio Estevam 	}
2738793fc096SFrank Li 
27395bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&fep->pdev->dev);
2740e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
274113a097bdSFabio Estevam 	if (ret)
2742e8fcfcd5SNimrod Andy 		goto failed_clk;
274313a097bdSFabio Estevam 
2744da1774e5SRussell King 	rtnl_lock();
2745793fc096SFrank Li 	if (netif_running(ndev)) {
2746ef83337dSRussell King 		fec_restart(ndev);
274731a6de34SRussell King 		netif_tx_lock_bh(ndev);
2748793fc096SFrank Li 		netif_device_attach(ndev);
27496af42d42SRussell King 		netif_tx_unlock_bh(ndev);
27506af42d42SRussell King 		napi_enable(&fep->napi);
2751d76cfae9SRussell King 		phy_start(fep->phy_dev);
2752793fc096SFrank Li 	}
2753da1774e5SRussell King 	rtnl_unlock();
2754793fc096SFrank Li 
2755793fc096SFrank Li 	return 0;
275613a097bdSFabio Estevam 
2757e8fcfcd5SNimrod Andy failed_clk:
275813a097bdSFabio Estevam 	if (fep->reg_phy)
275913a097bdSFabio Estevam 		regulator_disable(fep->reg_phy);
276013a097bdSFabio Estevam 	return ret;
2761793fc096SFrank Li }
2762bf7bfd7fSFabio Estevam #endif /* CONFIG_PM_SLEEP */
2763793fc096SFrank Li 
2764bf7bfd7fSFabio Estevam static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
2765793fc096SFrank Li 
2766793fc096SFrank Li static struct platform_driver fec_driver = {
2767793fc096SFrank Li 	.driver	= {
2768793fc096SFrank Li 		.name	= DRIVER_NAME,
2769793fc096SFrank Li 		.owner	= THIS_MODULE,
2770793fc096SFrank Li 		.pm	= &fec_pm_ops,
2771793fc096SFrank Li 		.of_match_table = fec_dt_ids,
2772793fc096SFrank Li 	},
2773793fc096SFrank Li 	.id_table = fec_devtype,
2774793fc096SFrank Li 	.probe	= fec_probe,
2775793fc096SFrank Li 	.remove	= fec_drv_remove,
2776793fc096SFrank Li };
2777793fc096SFrank Li 
2778793fc096SFrank Li module_platform_driver(fec_driver);
2779793fc096SFrank Li 
2780f8c0aca9SFabio Estevam MODULE_ALIAS("platform:"DRIVER_NAME);
2781793fc096SFrank Li MODULE_LICENSE("GPL");
2782