xref: /linux/drivers/net/ethernet/freescale/fec_main.c (revision 84fe61821e4ebab6322eeae3f3c27f77f0031978)
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>
394c09eed9SJim Baxter #include <linux/tcp.h>
404c09eed9SJim Baxter #include <linux/udp.h>
414c09eed9SJim Baxter #include <linux/icmp.h>
42793fc096SFrank Li #include <linux/spinlock.h>
43793fc096SFrank Li #include <linux/workqueue.h>
44793fc096SFrank Li #include <linux/bitops.h>
45793fc096SFrank Li #include <linux/io.h>
46793fc096SFrank Li #include <linux/irq.h>
47793fc096SFrank Li #include <linux/clk.h>
48793fc096SFrank Li #include <linux/platform_device.h>
49793fc096SFrank Li #include <linux/phy.h>
50793fc096SFrank Li #include <linux/fec.h>
51793fc096SFrank Li #include <linux/of.h>
52793fc096SFrank Li #include <linux/of_device.h>
53793fc096SFrank Li #include <linux/of_gpio.h>
54793fc096SFrank Li #include <linux/of_net.h>
55793fc096SFrank Li #include <linux/regulator/consumer.h>
56cdffcf1bSJim Baxter #include <linux/if_vlan.h>
57793fc096SFrank Li 
58793fc096SFrank Li #include <asm/cacheflush.h>
59793fc096SFrank Li 
60793fc096SFrank Li #include "fec.h"
61793fc096SFrank Li 
62772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev);
63772e42b0SChristoph Müllner 
64793fc096SFrank Li #if defined(CONFIG_ARM)
65793fc096SFrank Li #define FEC_ALIGNMENT	0xf
66793fc096SFrank Li #else
67793fc096SFrank Li #define FEC_ALIGNMENT	0x3
68793fc096SFrank Li #endif
69793fc096SFrank Li 
70793fc096SFrank Li #define DRIVER_NAME	"fec"
71793fc096SFrank Li 
72793fc096SFrank Li /* Pause frame feild and FIFO threshold */
73793fc096SFrank Li #define FEC_ENET_FCE	(1 << 5)
74793fc096SFrank Li #define FEC_ENET_RSEM_V	0x84
75793fc096SFrank Li #define FEC_ENET_RSFL_V	16
76793fc096SFrank Li #define FEC_ENET_RAEM_V	0x8
77793fc096SFrank Li #define FEC_ENET_RAFL_V	0x8
78793fc096SFrank Li #define FEC_ENET_OPD_V	0xFFF0
79793fc096SFrank Li 
80793fc096SFrank Li /* Controller is ENET-MAC */
81793fc096SFrank Li #define FEC_QUIRK_ENET_MAC		(1 << 0)
82793fc096SFrank Li /* Controller needs driver to swap frame */
83793fc096SFrank Li #define FEC_QUIRK_SWAP_FRAME		(1 << 1)
84793fc096SFrank Li /* Controller uses gasket */
85793fc096SFrank Li #define FEC_QUIRK_USE_GASKET		(1 << 2)
86793fc096SFrank Li /* Controller has GBIT support */
87793fc096SFrank Li #define FEC_QUIRK_HAS_GBIT		(1 << 3)
88793fc096SFrank Li /* Controller has extend desc buffer */
89793fc096SFrank Li #define FEC_QUIRK_HAS_BUFDESC_EX	(1 << 4)
9048496255SShawn Guo /* Controller has hardware checksum support */
9148496255SShawn Guo #define FEC_QUIRK_HAS_CSUM		(1 << 5)
92cdffcf1bSJim Baxter /* Controller has hardware vlan support */
93cdffcf1bSJim Baxter #define FEC_QUIRK_HAS_VLAN		(1 << 6)
9403191656SFrank Li /* ENET IP errata ERR006358
9503191656SFrank Li  *
9603191656SFrank Li  * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
9703191656SFrank Li  * detected as not set during a prior frame transmission, then the
9803191656SFrank Li  * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
9903191656SFrank Li  * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
10003191656SFrank Li  * frames not being transmitted until there is a 0-to-1 transition on
10103191656SFrank Li  * ENET_TDAR[TDAR].
10203191656SFrank Li  */
10303191656SFrank Li #define FEC_QUIRK_ERR006358            (1 << 7)
104793fc096SFrank Li 
105793fc096SFrank Li static struct platform_device_id fec_devtype[] = {
106793fc096SFrank Li 	{
107793fc096SFrank Li 		/* keep it for coldfire */
108793fc096SFrank Li 		.name = DRIVER_NAME,
109793fc096SFrank Li 		.driver_data = 0,
110793fc096SFrank Li 	}, {
111793fc096SFrank Li 		.name = "imx25-fec",
112793fc096SFrank Li 		.driver_data = FEC_QUIRK_USE_GASKET,
113793fc096SFrank Li 	}, {
114793fc096SFrank Li 		.name = "imx27-fec",
115793fc096SFrank Li 		.driver_data = 0,
116793fc096SFrank Li 	}, {
117793fc096SFrank Li 		.name = "imx28-fec",
118793fc096SFrank Li 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
119793fc096SFrank Li 	}, {
120793fc096SFrank Li 		.name = "imx6q-fec",
121793fc096SFrank Li 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
122cdffcf1bSJim Baxter 				FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
12303191656SFrank Li 				FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
124793fc096SFrank Li 	}, {
12536803542SShawn Guo 		.name = "mvf600-fec",
126ca7c4a45SJingchang Lu 		.driver_data = FEC_QUIRK_ENET_MAC,
127ca7c4a45SJingchang Lu 	}, {
128793fc096SFrank Li 		/* sentinel */
129793fc096SFrank Li 	}
130793fc096SFrank Li };
131793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype);
132793fc096SFrank Li 
133793fc096SFrank Li enum imx_fec_type {
134793fc096SFrank Li 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
135793fc096SFrank Li 	IMX27_FEC,	/* runs on i.mx27/35/51 */
136793fc096SFrank Li 	IMX28_FEC,
137793fc096SFrank Li 	IMX6Q_FEC,
13836803542SShawn Guo 	MVF600_FEC,
139793fc096SFrank Li };
140793fc096SFrank Li 
141793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = {
142793fc096SFrank Li 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
143793fc096SFrank Li 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
144793fc096SFrank Li 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
145793fc096SFrank Li 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
14636803542SShawn Guo 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
147793fc096SFrank Li 	{ /* sentinel */ }
148793fc096SFrank Li };
149793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids);
150793fc096SFrank Li 
151793fc096SFrank Li static unsigned char macaddr[ETH_ALEN];
152793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0);
153793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
154793fc096SFrank Li 
155793fc096SFrank Li #if defined(CONFIG_M5272)
156793fc096SFrank Li /*
157793fc096SFrank Li  * Some hardware gets it MAC address out of local flash memory.
158793fc096SFrank Li  * if this is non-zero then assume it is the address to get MAC from.
159793fc096SFrank Li  */
160793fc096SFrank Li #if defined(CONFIG_NETtel)
161793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006006
162793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
163793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006000
164793fc096SFrank Li #elif defined(CONFIG_CANCam)
165793fc096SFrank Li #define	FEC_FLASHMAC	0xf0020000
166793fc096SFrank Li #elif defined (CONFIG_M5272C3)
167793fc096SFrank Li #define	FEC_FLASHMAC	(0xffe04000 + 4)
168793fc096SFrank Li #elif defined(CONFIG_MOD5272)
169793fc096SFrank Li #define FEC_FLASHMAC	0xffc0406b
170793fc096SFrank Li #else
171793fc096SFrank Li #define	FEC_FLASHMAC	0
172793fc096SFrank Li #endif
173793fc096SFrank Li #endif /* CONFIG_M5272 */
174793fc096SFrank Li 
175793fc096SFrank Li #if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
176793fc096SFrank Li #error "FEC: descriptor ring size constants too large"
177793fc096SFrank Li #endif
178793fc096SFrank Li 
179793fc096SFrank Li /* Interrupt events/masks. */
180793fc096SFrank Li #define FEC_ENET_HBERR	((uint)0x80000000)	/* Heartbeat error */
181793fc096SFrank Li #define FEC_ENET_BABR	((uint)0x40000000)	/* Babbling receiver */
182793fc096SFrank Li #define FEC_ENET_BABT	((uint)0x20000000)	/* Babbling transmitter */
183793fc096SFrank Li #define FEC_ENET_GRA	((uint)0x10000000)	/* Graceful stop complete */
184793fc096SFrank Li #define FEC_ENET_TXF	((uint)0x08000000)	/* Full frame transmitted */
185793fc096SFrank Li #define FEC_ENET_TXB	((uint)0x04000000)	/* A buffer was transmitted */
186793fc096SFrank Li #define FEC_ENET_RXF	((uint)0x02000000)	/* Full frame received */
187793fc096SFrank Li #define FEC_ENET_RXB	((uint)0x01000000)	/* A buffer was received */
188793fc096SFrank Li #define FEC_ENET_MII	((uint)0x00800000)	/* MII interrupt */
189793fc096SFrank Li #define FEC_ENET_EBERR	((uint)0x00400000)	/* SDMA bus error */
190793fc096SFrank Li 
191793fc096SFrank Li #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
192793fc096SFrank Li #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
193793fc096SFrank Li 
194cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
195793fc096SFrank Li  */
196cdffcf1bSJim Baxter #define PKT_MAXBUF_SIZE		1522
197793fc096SFrank Li #define PKT_MINBUF_SIZE		64
198cdffcf1bSJim Baxter #define PKT_MAXBLR_SIZE		1536
199793fc096SFrank Li 
2004c09eed9SJim Baxter /* FEC receive acceleration */
2014c09eed9SJim Baxter #define FEC_RACC_IPDIS		(1 << 1)
2024c09eed9SJim Baxter #define FEC_RACC_PRODIS		(1 << 2)
2034c09eed9SJim Baxter #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
2044c09eed9SJim Baxter 
205793fc096SFrank Li /*
206793fc096SFrank Li  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
207793fc096SFrank Li  * size bits. Other FEC hardware does not, so we need to take that into
208793fc096SFrank Li  * account when setting it.
209793fc096SFrank Li  */
210793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
211793fc096SFrank Li     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
212793fc096SFrank Li #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
213793fc096SFrank Li #else
214793fc096SFrank Li #define	OPT_FRAME_SIZE	0
215793fc096SFrank Li #endif
216793fc096SFrank Li 
217793fc096SFrank Li /* FEC MII MMFR bits definition */
218793fc096SFrank Li #define FEC_MMFR_ST		(1 << 30)
219793fc096SFrank Li #define FEC_MMFR_OP_READ	(2 << 28)
220793fc096SFrank Li #define FEC_MMFR_OP_WRITE	(1 << 28)
221793fc096SFrank Li #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
222793fc096SFrank Li #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
223793fc096SFrank Li #define FEC_MMFR_TA		(2 << 16)
224793fc096SFrank Li #define FEC_MMFR_DATA(v)	(v & 0xffff)
225793fc096SFrank Li 
226793fc096SFrank Li #define FEC_MII_TIMEOUT		30000 /* us */
227793fc096SFrank Li 
228793fc096SFrank Li /* Transmitter timeout */
229793fc096SFrank Li #define TX_TIMEOUT (2 * HZ)
230793fc096SFrank Li 
231793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG	0x1
232793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE	0x2
233793fc096SFrank Li 
234793fc096SFrank Li static int mii_cnt;
235793fc096SFrank Li 
23636e24e2eSDuan Fugang-B38611 static inline
23736e24e2eSDuan Fugang-B38611 struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
238793fc096SFrank Li {
23936e24e2eSDuan Fugang-B38611 	struct bufdesc *new_bd = bdp + 1;
24036e24e2eSDuan Fugang-B38611 	struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
24136e24e2eSDuan Fugang-B38611 	struct bufdesc_ex *ex_base;
24236e24e2eSDuan Fugang-B38611 	struct bufdesc *base;
24336e24e2eSDuan Fugang-B38611 	int ring_size;
24436e24e2eSDuan Fugang-B38611 
24536e24e2eSDuan Fugang-B38611 	if (bdp >= fep->tx_bd_base) {
24636e24e2eSDuan Fugang-B38611 		base = fep->tx_bd_base;
24736e24e2eSDuan Fugang-B38611 		ring_size = fep->tx_ring_size;
24836e24e2eSDuan Fugang-B38611 		ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
24936e24e2eSDuan Fugang-B38611 	} else {
25036e24e2eSDuan Fugang-B38611 		base = fep->rx_bd_base;
25136e24e2eSDuan Fugang-B38611 		ring_size = fep->rx_ring_size;
25236e24e2eSDuan Fugang-B38611 		ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
253793fc096SFrank Li 	}
254793fc096SFrank Li 
25536e24e2eSDuan Fugang-B38611 	if (fep->bufdesc_ex)
25636e24e2eSDuan Fugang-B38611 		return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
25736e24e2eSDuan Fugang-B38611 			ex_base : ex_new_bd);
258793fc096SFrank Li 	else
25936e24e2eSDuan Fugang-B38611 		return (new_bd >= (base + ring_size)) ?
26036e24e2eSDuan Fugang-B38611 			base : new_bd;
26136e24e2eSDuan Fugang-B38611 }
26236e24e2eSDuan Fugang-B38611 
26336e24e2eSDuan Fugang-B38611 static inline
26436e24e2eSDuan Fugang-B38611 struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
26536e24e2eSDuan Fugang-B38611 {
26636e24e2eSDuan Fugang-B38611 	struct bufdesc *new_bd = bdp - 1;
26736e24e2eSDuan Fugang-B38611 	struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
26836e24e2eSDuan Fugang-B38611 	struct bufdesc_ex *ex_base;
26936e24e2eSDuan Fugang-B38611 	struct bufdesc *base;
27036e24e2eSDuan Fugang-B38611 	int ring_size;
27136e24e2eSDuan Fugang-B38611 
27236e24e2eSDuan Fugang-B38611 	if (bdp >= fep->tx_bd_base) {
27336e24e2eSDuan Fugang-B38611 		base = fep->tx_bd_base;
27436e24e2eSDuan Fugang-B38611 		ring_size = fep->tx_ring_size;
27536e24e2eSDuan Fugang-B38611 		ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
27636e24e2eSDuan Fugang-B38611 	} else {
27736e24e2eSDuan Fugang-B38611 		base = fep->rx_bd_base;
27836e24e2eSDuan Fugang-B38611 		ring_size = fep->rx_ring_size;
27936e24e2eSDuan Fugang-B38611 		ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
28036e24e2eSDuan Fugang-B38611 	}
28136e24e2eSDuan Fugang-B38611 
28236e24e2eSDuan Fugang-B38611 	if (fep->bufdesc_ex)
28336e24e2eSDuan Fugang-B38611 		return (struct bufdesc *)((ex_new_bd < ex_base) ?
28436e24e2eSDuan Fugang-B38611 			(ex_new_bd + ring_size) : ex_new_bd);
28536e24e2eSDuan Fugang-B38611 	else
28636e24e2eSDuan Fugang-B38611 		return (new_bd < base) ? (new_bd + ring_size) : new_bd;
287793fc096SFrank Li }
288793fc096SFrank Li 
289793fc096SFrank Li static void *swap_buffer(void *bufaddr, int len)
290793fc096SFrank Li {
291793fc096SFrank Li 	int i;
292793fc096SFrank Li 	unsigned int *buf = bufaddr;
293793fc096SFrank Li 
294ffed61e6SFabio Estevam 	for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
295793fc096SFrank Li 		*buf = cpu_to_be32(*buf);
296793fc096SFrank Li 
297793fc096SFrank Li 	return bufaddr;
298793fc096SFrank Li }
299793fc096SFrank Li 
3004c09eed9SJim Baxter static int
3014c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
3024c09eed9SJim Baxter {
3034c09eed9SJim Baxter 	/* Only run for packets requiring a checksum. */
3044c09eed9SJim Baxter 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3054c09eed9SJim Baxter 		return 0;
3064c09eed9SJim Baxter 
3074c09eed9SJim Baxter 	if (unlikely(skb_cow_head(skb, 0)))
3084c09eed9SJim Baxter 		return -1;
3094c09eed9SJim Baxter 
3104c09eed9SJim Baxter 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
3114c09eed9SJim Baxter 
3124c09eed9SJim Baxter 	return 0;
3134c09eed9SJim Baxter }
3144c09eed9SJim Baxter 
315793fc096SFrank Li static netdev_tx_t
316793fc096SFrank Li fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
317793fc096SFrank Li {
318793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
319793fc096SFrank Li 	const struct platform_device_id *id_entry =
320793fc096SFrank Li 				platform_get_device_id(fep->pdev);
32103191656SFrank Li 	struct bufdesc *bdp, *bdp_pre;
322793fc096SFrank Li 	void *bufaddr;
323793fc096SFrank Li 	unsigned short	status;
324793fc096SFrank Li 	unsigned int index;
325793fc096SFrank Li 
326793fc096SFrank Li 	/* Fill in a Tx ring entry */
327793fc096SFrank Li 	bdp = fep->cur_tx;
328793fc096SFrank Li 
329793fc096SFrank Li 	status = bdp->cbd_sc;
330793fc096SFrank Li 
331793fc096SFrank Li 	if (status & BD_ENET_TX_READY) {
332793fc096SFrank Li 		/* Ooops.  All transmit buffers are full.  Bail out.
333793fc096SFrank Li 		 * This should not happen, since ndev->tbusy should be set.
334793fc096SFrank Li 		 */
33531b7720cSJoe Perches 		netdev_err(ndev, "tx queue full!\n");
336793fc096SFrank Li 		return NETDEV_TX_BUSY;
337793fc096SFrank Li 	}
338793fc096SFrank Li 
3394c09eed9SJim Baxter 	/* Protocol checksum off-load for TCP and UDP. */
3404c09eed9SJim Baxter 	if (fec_enet_clear_csum(skb, ndev)) {
3414c09eed9SJim Baxter 		kfree_skb(skb);
3424c09eed9SJim Baxter 		return NETDEV_TX_OK;
3434c09eed9SJim Baxter 	}
3444c09eed9SJim Baxter 
345793fc096SFrank Li 	/* Clear all of the status flags */
346793fc096SFrank Li 	status &= ~BD_ENET_TX_STATS;
347793fc096SFrank Li 
348793fc096SFrank Li 	/* Set buffer length and buffer pointer */
349793fc096SFrank Li 	bufaddr = skb->data;
350793fc096SFrank Li 	bdp->cbd_datlen = skb->len;
351793fc096SFrank Li 
352793fc096SFrank Li 	/*
353793fc096SFrank Li 	 * On some FEC implementations data must be aligned on
354793fc096SFrank Li 	 * 4-byte boundaries. Use bounce buffers to copy data
355793fc096SFrank Li 	 * and get it aligned. Ugh.
356793fc096SFrank Li 	 */
357793fc096SFrank Li 	if (fep->bufdesc_ex)
358793fc096SFrank Li 		index = (struct bufdesc_ex *)bdp -
359793fc096SFrank Li 			(struct bufdesc_ex *)fep->tx_bd_base;
360793fc096SFrank Li 	else
361793fc096SFrank Li 		index = bdp - fep->tx_bd_base;
362793fc096SFrank Li 
363793fc096SFrank Li 	if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
364793fc096SFrank Li 		memcpy(fep->tx_bounce[index], skb->data, skb->len);
365793fc096SFrank Li 		bufaddr = fep->tx_bounce[index];
366793fc096SFrank Li 	}
367793fc096SFrank Li 
368793fc096SFrank Li 	/*
369793fc096SFrank Li 	 * Some design made an incorrect assumption on endian mode of
370793fc096SFrank Li 	 * the system that it's running on. As the result, driver has to
371793fc096SFrank Li 	 * swap every frame going to and coming from the controller.
372793fc096SFrank Li 	 */
373793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
374793fc096SFrank Li 		swap_buffer(bufaddr, skb->len);
375793fc096SFrank Li 
376793fc096SFrank Li 	/* Save skb pointer */
377793fc096SFrank Li 	fep->tx_skbuff[index] = skb;
378793fc096SFrank Li 
379793fc096SFrank Li 	/* Push the data cache so the CPM does not get stale memory
380793fc096SFrank Li 	 * data.
381793fc096SFrank Li 	 */
382793fc096SFrank Li 	bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
3832488a54eSSebastian Siewior 			skb->len, DMA_TO_DEVICE);
384d842a31fSDuan Fugang-B38611 	if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
385d842a31fSDuan Fugang-B38611 		bdp->cbd_bufaddr = 0;
386d842a31fSDuan Fugang-B38611 		fep->tx_skbuff[index] = NULL;
387d842a31fSDuan Fugang-B38611 		dev_kfree_skb_any(skb);
388d842a31fSDuan Fugang-B38611 		if (net_ratelimit())
389d842a31fSDuan Fugang-B38611 			netdev_err(ndev, "Tx DMA memory map failed\n");
390d842a31fSDuan Fugang-B38611 		return NETDEV_TX_OK;
391d842a31fSDuan Fugang-B38611 	}
392793fc096SFrank Li 
393793fc096SFrank Li 	if (fep->bufdesc_ex) {
394793fc096SFrank Li 
395793fc096SFrank Li 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
396793fc096SFrank Li 		ebdp->cbd_bdu = 0;
397793fc096SFrank Li 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
398793fc096SFrank Li 			fep->hwts_tx_en)) {
399793fc096SFrank Li 			ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
400793fc096SFrank Li 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
401793fc096SFrank Li 		} else {
402793fc096SFrank Li 			ebdp->cbd_esc = BD_ENET_TX_INT;
4034c09eed9SJim Baxter 
4044c09eed9SJim Baxter 			/* Enable protocol checksum flags
4054c09eed9SJim Baxter 			 * We do not bother with the IP Checksum bits as they
4064c09eed9SJim Baxter 			 * are done by the kernel
4074c09eed9SJim Baxter 			 */
4084c09eed9SJim Baxter 			if (skb->ip_summed == CHECKSUM_PARTIAL)
4094c09eed9SJim Baxter 				ebdp->cbd_esc |= BD_ENET_TX_PINS;
410793fc096SFrank Li 		}
411793fc096SFrank Li 	}
41203191656SFrank Li 
413fb8ef788SDuan Fugang-B38611 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
414fb8ef788SDuan Fugang-B38611 	 * it's the last BD of the frame, and to put the CRC on the end.
415fb8ef788SDuan Fugang-B38611 	 */
416fb8ef788SDuan Fugang-B38611 	status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
417fb8ef788SDuan Fugang-B38611 			| BD_ENET_TX_LAST | BD_ENET_TX_TC);
418fb8ef788SDuan Fugang-B38611 	bdp->cbd_sc = status;
419fb8ef788SDuan Fugang-B38611 
42036e24e2eSDuan Fugang-B38611 	bdp_pre = fec_enet_get_prevdesc(bdp, fep);
42103191656SFrank Li 	if ((id_entry->driver_data & FEC_QUIRK_ERR006358) &&
42203191656SFrank Li 	    !(bdp_pre->cbd_sc & BD_ENET_TX_READY)) {
42303191656SFrank Li 		fep->delay_work.trig_tx = true;
42403191656SFrank Li 		schedule_delayed_work(&(fep->delay_work.delay_work),
42503191656SFrank Li 					msecs_to_jiffies(1));
42603191656SFrank Li 	}
42703191656SFrank Li 
428793fc096SFrank Li 	/* If this was the last BD in the ring, start at the beginning again. */
42936e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_nextdesc(bdp, fep);
430793fc096SFrank Li 
4317a2a8451SEric Dumazet 	skb_tx_timestamp(skb);
4327a2a8451SEric Dumazet 
433793fc096SFrank Li 	fep->cur_tx = bdp;
434793fc096SFrank Li 
435793fc096SFrank Li 	if (fep->cur_tx == fep->dirty_tx)
436793fc096SFrank Li 		netif_stop_queue(ndev);
437793fc096SFrank Li 
438793fc096SFrank Li 	/* Trigger transmission start */
439793fc096SFrank Li 	writel(0, fep->hwp + FEC_X_DES_ACTIVE);
440793fc096SFrank Li 
441793fc096SFrank Li 	return NETDEV_TX_OK;
442793fc096SFrank Li }
443793fc096SFrank Li 
444a210576cSDavid S. Miller /* Init RX & TX buffer descriptors
445a210576cSDavid S. Miller  */
446a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev)
447a210576cSDavid S. Miller {
448a210576cSDavid S. Miller 	struct fec_enet_private *fep = netdev_priv(dev);
449a210576cSDavid S. Miller 	struct bufdesc *bdp;
450a210576cSDavid S. Miller 	unsigned int i;
451a210576cSDavid S. Miller 
452a210576cSDavid S. Miller 	/* Initialize the receive buffer descriptors. */
453a210576cSDavid S. Miller 	bdp = fep->rx_bd_base;
45436e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->rx_ring_size; i++) {
455a210576cSDavid S. Miller 
456a210576cSDavid S. Miller 		/* Initialize the BD for every fragment in the page. */
457a210576cSDavid S. Miller 		if (bdp->cbd_bufaddr)
458a210576cSDavid S. Miller 			bdp->cbd_sc = BD_ENET_RX_EMPTY;
459a210576cSDavid S. Miller 		else
460a210576cSDavid S. Miller 			bdp->cbd_sc = 0;
46136e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
462a210576cSDavid S. Miller 	}
463a210576cSDavid S. Miller 
464a210576cSDavid S. Miller 	/* Set the last buffer to wrap */
46536e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_prevdesc(bdp, fep);
466a210576cSDavid S. Miller 	bdp->cbd_sc |= BD_SC_WRAP;
467a210576cSDavid S. Miller 
468a210576cSDavid S. Miller 	fep->cur_rx = fep->rx_bd_base;
469a210576cSDavid S. Miller 
470a210576cSDavid S. Miller 	/* ...and the same for transmit */
471a210576cSDavid S. Miller 	bdp = fep->tx_bd_base;
472a210576cSDavid S. Miller 	fep->cur_tx = bdp;
47336e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->tx_ring_size; i++) {
474a210576cSDavid S. Miller 
475a210576cSDavid S. Miller 		/* Initialize the BD for every fragment in the page. */
476a210576cSDavid S. Miller 		bdp->cbd_sc = 0;
477a210576cSDavid S. Miller 		if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
478a210576cSDavid S. Miller 			dev_kfree_skb_any(fep->tx_skbuff[i]);
479a210576cSDavid S. Miller 			fep->tx_skbuff[i] = NULL;
480a210576cSDavid S. Miller 		}
481a210576cSDavid S. Miller 		bdp->cbd_bufaddr = 0;
48236e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
483a210576cSDavid S. Miller 	}
484a210576cSDavid S. Miller 
485a210576cSDavid S. Miller 	/* Set the last buffer to wrap */
48636e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_prevdesc(bdp, fep);
487a210576cSDavid S. Miller 	bdp->cbd_sc |= BD_SC_WRAP;
488a210576cSDavid S. Miller 	fep->dirty_tx = bdp;
489a210576cSDavid S. Miller }
490a210576cSDavid S. Miller 
491793fc096SFrank Li /* This function is called to start or restart the FEC during a link
492793fc096SFrank Li  * change.  This only happens when switching between half and full
493793fc096SFrank Li  * duplex.
494793fc096SFrank Li  */
495793fc096SFrank Li static void
496793fc096SFrank Li fec_restart(struct net_device *ndev, int duplex)
497793fc096SFrank Li {
498793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
499793fc096SFrank Li 	const struct platform_device_id *id_entry =
500793fc096SFrank Li 				platform_get_device_id(fep->pdev);
501793fc096SFrank Li 	int i;
5024c09eed9SJim Baxter 	u32 val;
503793fc096SFrank Li 	u32 temp_mac[2];
504793fc096SFrank Li 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
505793fc096SFrank Li 	u32 ecntl = 0x2; /* ETHEREN */
506793fc096SFrank Li 
50754309fa6SFrank Li 	if (netif_running(ndev)) {
50854309fa6SFrank Li 		netif_device_detach(ndev);
50954309fa6SFrank Li 		napi_disable(&fep->napi);
51054309fa6SFrank Li 		netif_stop_queue(ndev);
51131691344SFabio Estevam 		netif_tx_lock_bh(ndev);
51254309fa6SFrank Li 	}
51354309fa6SFrank Li 
514793fc096SFrank Li 	/* Whack a reset.  We should wait for this. */
515793fc096SFrank Li 	writel(1, fep->hwp + FEC_ECNTRL);
516793fc096SFrank Li 	udelay(10);
517793fc096SFrank Li 
518793fc096SFrank Li 	/*
519793fc096SFrank Li 	 * enet-mac reset will reset mac address registers too,
520793fc096SFrank Li 	 * so need to reconfigure it.
521793fc096SFrank Li 	 */
522793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
523793fc096SFrank Li 		memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
524793fc096SFrank Li 		writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
525793fc096SFrank Li 		writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
526793fc096SFrank Li 	}
527793fc096SFrank Li 
528793fc096SFrank Li 	/* Clear any outstanding interrupt. */
529793fc096SFrank Li 	writel(0xffc00000, fep->hwp + FEC_IEVENT);
530793fc096SFrank Li 
531793fc096SFrank Li 	/* Set maximum receive buffer size. */
532793fc096SFrank Li 	writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
533793fc096SFrank Li 
534a210576cSDavid S. Miller 	fec_enet_bd_init(ndev);
535a210576cSDavid S. Miller 
536793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
537793fc096SFrank Li 	writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
538793fc096SFrank Li 	if (fep->bufdesc_ex)
539793fc096SFrank Li 		writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
54036e24e2eSDuan Fugang-B38611 			* fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
541793fc096SFrank Li 	else
542793fc096SFrank Li 		writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
54336e24e2eSDuan Fugang-B38611 			* fep->rx_ring_size,	fep->hwp + FEC_X_DES_START);
544793fc096SFrank Li 
545793fc096SFrank Li 
546793fc096SFrank Li 	for (i = 0; i <= TX_RING_MOD_MASK; i++) {
547793fc096SFrank Li 		if (fep->tx_skbuff[i]) {
548793fc096SFrank Li 			dev_kfree_skb_any(fep->tx_skbuff[i]);
549793fc096SFrank Li 			fep->tx_skbuff[i] = NULL;
550793fc096SFrank Li 		}
551793fc096SFrank Li 	}
552793fc096SFrank Li 
553793fc096SFrank Li 	/* Enable MII mode */
554793fc096SFrank Li 	if (duplex) {
555793fc096SFrank Li 		/* FD enable */
556793fc096SFrank Li 		writel(0x04, fep->hwp + FEC_X_CNTRL);
557793fc096SFrank Li 	} else {
558793fc096SFrank Li 		/* No Rcv on Xmit */
559793fc096SFrank Li 		rcntl |= 0x02;
560793fc096SFrank Li 		writel(0x0, fep->hwp + FEC_X_CNTRL);
561793fc096SFrank Li 	}
562793fc096SFrank Li 
563793fc096SFrank Li 	fep->full_duplex = duplex;
564793fc096SFrank Li 
565793fc096SFrank Li 	/* Set MII speed */
566793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
567793fc096SFrank Li 
568d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
5694c09eed9SJim Baxter 	/* set RX checksum */
5704c09eed9SJim Baxter 	val = readl(fep->hwp + FEC_RACC);
5714c09eed9SJim Baxter 	if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
5724c09eed9SJim Baxter 		val |= FEC_RACC_OPTIONS;
5734c09eed9SJim Baxter 	else
5744c09eed9SJim Baxter 		val &= ~FEC_RACC_OPTIONS;
5754c09eed9SJim Baxter 	writel(val, fep->hwp + FEC_RACC);
576d1391930SGuenter Roeck #endif
5774c09eed9SJim Baxter 
578793fc096SFrank Li 	/*
579793fc096SFrank Li 	 * The phy interface and speed need to get configured
580793fc096SFrank Li 	 * differently on enet-mac.
581793fc096SFrank Li 	 */
582793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
583793fc096SFrank Li 		/* Enable flow control and length check */
584793fc096SFrank Li 		rcntl |= 0x40000000 | 0x00000020;
585793fc096SFrank Li 
586793fc096SFrank Li 		/* RGMII, RMII or MII */
587793fc096SFrank Li 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
588793fc096SFrank Li 			rcntl |= (1 << 6);
589793fc096SFrank Li 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
590793fc096SFrank Li 			rcntl |= (1 << 8);
591793fc096SFrank Li 		else
592793fc096SFrank Li 			rcntl &= ~(1 << 8);
593793fc096SFrank Li 
594793fc096SFrank Li 		/* 1G, 100M or 10M */
595793fc096SFrank Li 		if (fep->phy_dev) {
596793fc096SFrank Li 			if (fep->phy_dev->speed == SPEED_1000)
597793fc096SFrank Li 				ecntl |= (1 << 5);
598793fc096SFrank Li 			else if (fep->phy_dev->speed == SPEED_100)
599793fc096SFrank Li 				rcntl &= ~(1 << 9);
600793fc096SFrank Li 			else
601793fc096SFrank Li 				rcntl |= (1 << 9);
602793fc096SFrank Li 		}
603793fc096SFrank Li 	} else {
604793fc096SFrank Li #ifdef FEC_MIIGSK_ENR
605793fc096SFrank Li 		if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
606793fc096SFrank Li 			u32 cfgr;
607793fc096SFrank Li 			/* disable the gasket and wait */
608793fc096SFrank Li 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
609793fc096SFrank Li 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
610793fc096SFrank Li 				udelay(1);
611793fc096SFrank Li 
612793fc096SFrank Li 			/*
613793fc096SFrank Li 			 * configure the gasket:
614793fc096SFrank Li 			 *   RMII, 50 MHz, no loopback, no echo
615793fc096SFrank Li 			 *   MII, 25 MHz, no loopback, no echo
616793fc096SFrank Li 			 */
617793fc096SFrank Li 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
618793fc096SFrank Li 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
619793fc096SFrank Li 			if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
620793fc096SFrank Li 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
621793fc096SFrank Li 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
622793fc096SFrank Li 
623793fc096SFrank Li 			/* re-enable the gasket */
624793fc096SFrank Li 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
625793fc096SFrank Li 		}
626793fc096SFrank Li #endif
627793fc096SFrank Li 	}
628793fc096SFrank Li 
629d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
630793fc096SFrank Li 	/* enable pause frame*/
631793fc096SFrank Li 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
632793fc096SFrank Li 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
633793fc096SFrank Li 	     fep->phy_dev && fep->phy_dev->pause)) {
634793fc096SFrank Li 		rcntl |= FEC_ENET_FCE;
635793fc096SFrank Li 
6364c09eed9SJim Baxter 		/* set FIFO threshold parameter to reduce overrun */
637793fc096SFrank Li 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
638793fc096SFrank Li 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
639793fc096SFrank Li 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
640793fc096SFrank Li 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
641793fc096SFrank Li 
642793fc096SFrank Li 		/* OPD */
643793fc096SFrank Li 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
644793fc096SFrank Li 	} else {
645793fc096SFrank Li 		rcntl &= ~FEC_ENET_FCE;
646793fc096SFrank Li 	}
647d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
648793fc096SFrank Li 
649793fc096SFrank Li 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
650793fc096SFrank Li 
651*84fe6182SStefan Wahren 	/* Setup multicast filter. */
652*84fe6182SStefan Wahren 	set_multicast_list(ndev);
653*84fe6182SStefan Wahren #ifndef CONFIG_M5272
654*84fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
655*84fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
656*84fe6182SStefan Wahren #endif
657*84fe6182SStefan Wahren 
658793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
659793fc096SFrank Li 		/* enable ENET endian swap */
660793fc096SFrank Li 		ecntl |= (1 << 8);
661793fc096SFrank Li 		/* enable ENET store and forward mode */
662793fc096SFrank Li 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
663793fc096SFrank Li 	}
664793fc096SFrank Li 
665793fc096SFrank Li 	if (fep->bufdesc_ex)
666793fc096SFrank Li 		ecntl |= (1 << 4);
667793fc096SFrank Li 
66838ae92dcSChris Healy #ifndef CONFIG_M5272
669b9eef55cSJim Baxter 	/* Enable the MIB statistic event counters */
670b9eef55cSJim Baxter 	writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
67138ae92dcSChris Healy #endif
67238ae92dcSChris Healy 
673793fc096SFrank Li 	/* And last, enable the transmit and receive processing */
674793fc096SFrank Li 	writel(ecntl, fep->hwp + FEC_ECNTRL);
675793fc096SFrank Li 	writel(0, fep->hwp + FEC_R_DES_ACTIVE);
676793fc096SFrank Li 
677793fc096SFrank Li 	if (fep->bufdesc_ex)
678793fc096SFrank Li 		fec_ptp_start_cyclecounter(ndev);
679793fc096SFrank Li 
680793fc096SFrank Li 	/* Enable interrupts we wish to service */
681793fc096SFrank Li 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
68254309fa6SFrank Li 
68354309fa6SFrank Li 	if (netif_running(ndev)) {
68431691344SFabio Estevam 		netif_tx_unlock_bh(ndev);
68554309fa6SFrank Li 		netif_wake_queue(ndev);
6861ed0d56cSFabio Estevam 		napi_enable(&fep->napi);
6871ed0d56cSFabio Estevam 		netif_device_attach(ndev);
68854309fa6SFrank Li 	}
689793fc096SFrank Li }
690793fc096SFrank Li 
691793fc096SFrank Li static void
692793fc096SFrank Li fec_stop(struct net_device *ndev)
693793fc096SFrank Li {
694793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
695793fc096SFrank Li 	const struct platform_device_id *id_entry =
696793fc096SFrank Li 				platform_get_device_id(fep->pdev);
697793fc096SFrank Li 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
698793fc096SFrank Li 
699793fc096SFrank Li 	/* We cannot expect a graceful transmit stop without link !!! */
700793fc096SFrank Li 	if (fep->link) {
701793fc096SFrank Li 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
702793fc096SFrank Li 		udelay(10);
703793fc096SFrank Li 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
70431b7720cSJoe Perches 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
705793fc096SFrank Li 	}
706793fc096SFrank Li 
707793fc096SFrank Li 	/* Whack a reset.  We should wait for this. */
708793fc096SFrank Li 	writel(1, fep->hwp + FEC_ECNTRL);
709793fc096SFrank Li 	udelay(10);
710793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
711793fc096SFrank Li 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
712793fc096SFrank Li 
713793fc096SFrank Li 	/* We have to keep ENET enabled to have MII interrupt stay working */
714793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
715793fc096SFrank Li 		writel(2, fep->hwp + FEC_ECNTRL);
716793fc096SFrank Li 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
717793fc096SFrank Li 	}
718793fc096SFrank Li }
719793fc096SFrank Li 
720793fc096SFrank Li 
721793fc096SFrank Li static void
722793fc096SFrank Li fec_timeout(struct net_device *ndev)
723793fc096SFrank Li {
724793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
725793fc096SFrank Li 
726793fc096SFrank Li 	ndev->stats.tx_errors++;
727793fc096SFrank Li 
72854309fa6SFrank Li 	fep->delay_work.timeout = true;
72954309fa6SFrank Li 	schedule_delayed_work(&(fep->delay_work.delay_work), 0);
73054309fa6SFrank Li }
73154309fa6SFrank Li 
73254309fa6SFrank Li static void fec_enet_work(struct work_struct *work)
73354309fa6SFrank Li {
73454309fa6SFrank Li 	struct fec_enet_private *fep =
73554309fa6SFrank Li 		container_of(work,
73654309fa6SFrank Li 			     struct fec_enet_private,
73754309fa6SFrank Li 			     delay_work.delay_work.work);
73854309fa6SFrank Li 
73954309fa6SFrank Li 	if (fep->delay_work.timeout) {
74054309fa6SFrank Li 		fep->delay_work.timeout = false;
74154309fa6SFrank Li 		fec_restart(fep->netdev, fep->full_duplex);
74254309fa6SFrank Li 		netif_wake_queue(fep->netdev);
74354309fa6SFrank Li 	}
74403191656SFrank Li 
74503191656SFrank Li 	if (fep->delay_work.trig_tx) {
74603191656SFrank Li 		fep->delay_work.trig_tx = false;
74703191656SFrank Li 		writel(0, fep->hwp + FEC_X_DES_ACTIVE);
74803191656SFrank Li 	}
749793fc096SFrank Li }
750793fc096SFrank Li 
751793fc096SFrank Li static void
752793fc096SFrank Li fec_enet_tx(struct net_device *ndev)
753793fc096SFrank Li {
754793fc096SFrank Li 	struct	fec_enet_private *fep;
755793fc096SFrank Li 	struct bufdesc *bdp;
756793fc096SFrank Li 	unsigned short status;
757793fc096SFrank Li 	struct	sk_buff	*skb;
758793fc096SFrank Li 	int	index = 0;
759793fc096SFrank Li 
760793fc096SFrank Li 	fep = netdev_priv(ndev);
761793fc096SFrank Li 	bdp = fep->dirty_tx;
762793fc096SFrank Li 
763793fc096SFrank Li 	/* get next bdp of dirty_tx */
76436e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_nextdesc(bdp, fep);
765793fc096SFrank Li 
766793fc096SFrank Li 	while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
767793fc096SFrank Li 
768793fc096SFrank Li 		/* current queue is empty */
769793fc096SFrank Li 		if (bdp == fep->cur_tx)
770793fc096SFrank Li 			break;
771793fc096SFrank Li 
772793fc096SFrank Li 		if (fep->bufdesc_ex)
773793fc096SFrank Li 			index = (struct bufdesc_ex *)bdp -
774793fc096SFrank Li 				(struct bufdesc_ex *)fep->tx_bd_base;
775793fc096SFrank Li 		else
776793fc096SFrank Li 			index = bdp - fep->tx_bd_base;
777793fc096SFrank Li 
778793fc096SFrank Li 		skb = fep->tx_skbuff[index];
7792488a54eSSebastian Siewior 		dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr, skb->len,
7802488a54eSSebastian Siewior 				DMA_TO_DEVICE);
7812488a54eSSebastian Siewior 		bdp->cbd_bufaddr = 0;
782793fc096SFrank Li 
783793fc096SFrank Li 		/* Check for errors. */
784793fc096SFrank Li 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
785793fc096SFrank Li 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
786793fc096SFrank Li 				   BD_ENET_TX_CSL)) {
787793fc096SFrank Li 			ndev->stats.tx_errors++;
788793fc096SFrank Li 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
789793fc096SFrank Li 				ndev->stats.tx_heartbeat_errors++;
790793fc096SFrank Li 			if (status & BD_ENET_TX_LC)  /* Late collision */
791793fc096SFrank Li 				ndev->stats.tx_window_errors++;
792793fc096SFrank Li 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
793793fc096SFrank Li 				ndev->stats.tx_aborted_errors++;
794793fc096SFrank Li 			if (status & BD_ENET_TX_UN)  /* Underrun */
795793fc096SFrank Li 				ndev->stats.tx_fifo_errors++;
796793fc096SFrank Li 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
797793fc096SFrank Li 				ndev->stats.tx_carrier_errors++;
798793fc096SFrank Li 		} else {
799793fc096SFrank Li 			ndev->stats.tx_packets++;
80006efce71SJim Baxter 			ndev->stats.tx_bytes += bdp->cbd_datlen;
801793fc096SFrank Li 		}
802793fc096SFrank Li 
803793fc096SFrank Li 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
804793fc096SFrank Li 			fep->bufdesc_ex) {
805793fc096SFrank Li 			struct skb_shared_hwtstamps shhwtstamps;
806793fc096SFrank Li 			unsigned long flags;
807793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
808793fc096SFrank Li 
809793fc096SFrank Li 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
810793fc096SFrank Li 			spin_lock_irqsave(&fep->tmreg_lock, flags);
811793fc096SFrank Li 			shhwtstamps.hwtstamp = ns_to_ktime(
812793fc096SFrank Li 				timecounter_cyc2time(&fep->tc, ebdp->ts));
813793fc096SFrank Li 			spin_unlock_irqrestore(&fep->tmreg_lock, flags);
814793fc096SFrank Li 			skb_tstamp_tx(skb, &shhwtstamps);
815793fc096SFrank Li 		}
816793fc096SFrank Li 
817793fc096SFrank Li 		if (status & BD_ENET_TX_READY)
81831b7720cSJoe Perches 			netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
819793fc096SFrank Li 
820793fc096SFrank Li 		/* Deferred means some collisions occurred during transmit,
821793fc096SFrank Li 		 * but we eventually sent the packet OK.
822793fc096SFrank Li 		 */
823793fc096SFrank Li 		if (status & BD_ENET_TX_DEF)
824793fc096SFrank Li 			ndev->stats.collisions++;
825793fc096SFrank Li 
826793fc096SFrank Li 		/* Free the sk buffer associated with this last transmit */
827793fc096SFrank Li 		dev_kfree_skb_any(skb);
828793fc096SFrank Li 		fep->tx_skbuff[index] = NULL;
829793fc096SFrank Li 
830793fc096SFrank Li 		fep->dirty_tx = bdp;
831793fc096SFrank Li 
832793fc096SFrank Li 		/* Update pointer to next buffer descriptor to be transmitted */
83336e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
834793fc096SFrank Li 
835793fc096SFrank Li 		/* Since we have freed up a buffer, the ring is no longer full
836793fc096SFrank Li 		 */
837793fc096SFrank Li 		if (fep->dirty_tx != fep->cur_tx) {
838793fc096SFrank Li 			if (netif_queue_stopped(ndev))
839793fc096SFrank Li 				netif_wake_queue(ndev);
840793fc096SFrank Li 		}
841793fc096SFrank Li 	}
842793fc096SFrank Li 	return;
843793fc096SFrank Li }
844793fc096SFrank Li 
845793fc096SFrank Li 
846793fc096SFrank Li /* During a receive, the cur_rx points to the current incoming buffer.
847793fc096SFrank Li  * When we update through the ring, if the next incoming buffer has
848793fc096SFrank Li  * not been given to the system, we just set the empty indicator,
849793fc096SFrank Li  * effectively tossing the packet.
850793fc096SFrank Li  */
851793fc096SFrank Li static int
852793fc096SFrank Li fec_enet_rx(struct net_device *ndev, int budget)
853793fc096SFrank Li {
854793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
855793fc096SFrank Li 	const struct platform_device_id *id_entry =
856793fc096SFrank Li 				platform_get_device_id(fep->pdev);
857793fc096SFrank Li 	struct bufdesc *bdp;
858793fc096SFrank Li 	unsigned short status;
859793fc096SFrank Li 	struct	sk_buff	*skb;
860793fc096SFrank Li 	ushort	pkt_len;
861793fc096SFrank Li 	__u8 *data;
862793fc096SFrank Li 	int	pkt_received = 0;
863cdffcf1bSJim Baxter 	struct	bufdesc_ex *ebdp = NULL;
864cdffcf1bSJim Baxter 	bool	vlan_packet_rcvd = false;
865cdffcf1bSJim Baxter 	u16	vlan_tag;
866d842a31fSDuan Fugang-B38611 	int	index = 0;
867793fc096SFrank Li 
868793fc096SFrank Li #ifdef CONFIG_M532x
869793fc096SFrank Li 	flush_cache_all();
870793fc096SFrank Li #endif
871793fc096SFrank Li 
872793fc096SFrank Li 	/* First, grab all of the stats for the incoming packet.
873793fc096SFrank Li 	 * These get messed up if we get called due to a busy condition.
874793fc096SFrank Li 	 */
875793fc096SFrank Li 	bdp = fep->cur_rx;
876793fc096SFrank Li 
877793fc096SFrank Li 	while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
878793fc096SFrank Li 
879793fc096SFrank Li 		if (pkt_received >= budget)
880793fc096SFrank Li 			break;
881793fc096SFrank Li 		pkt_received++;
882793fc096SFrank Li 
883793fc096SFrank Li 		/* Since we have allocated space to hold a complete frame,
884793fc096SFrank Li 		 * the last indicator should be set.
885793fc096SFrank Li 		 */
886793fc096SFrank Li 		if ((status & BD_ENET_RX_LAST) == 0)
88731b7720cSJoe Perches 			netdev_err(ndev, "rcv is not +last\n");
888793fc096SFrank Li 
889793fc096SFrank Li 		if (!fep->opened)
890793fc096SFrank Li 			goto rx_processing_done;
891793fc096SFrank Li 
892793fc096SFrank Li 		/* Check for errors. */
893793fc096SFrank Li 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
894793fc096SFrank Li 			   BD_ENET_RX_CR | BD_ENET_RX_OV)) {
895793fc096SFrank Li 			ndev->stats.rx_errors++;
896793fc096SFrank Li 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
897793fc096SFrank Li 				/* Frame too long or too short. */
898793fc096SFrank Li 				ndev->stats.rx_length_errors++;
899793fc096SFrank Li 			}
900793fc096SFrank Li 			if (status & BD_ENET_RX_NO)	/* Frame alignment */
901793fc096SFrank Li 				ndev->stats.rx_frame_errors++;
902793fc096SFrank Li 			if (status & BD_ENET_RX_CR)	/* CRC Error */
903793fc096SFrank Li 				ndev->stats.rx_crc_errors++;
904793fc096SFrank Li 			if (status & BD_ENET_RX_OV)	/* FIFO overrun */
905793fc096SFrank Li 				ndev->stats.rx_fifo_errors++;
906793fc096SFrank Li 		}
907793fc096SFrank Li 
908793fc096SFrank Li 		/* Report late collisions as a frame error.
909793fc096SFrank Li 		 * On this error, the BD is closed, but we don't know what we
910793fc096SFrank Li 		 * have in the buffer.  So, just drop this frame on the floor.
911793fc096SFrank Li 		 */
912793fc096SFrank Li 		if (status & BD_ENET_RX_CL) {
913793fc096SFrank Li 			ndev->stats.rx_errors++;
914793fc096SFrank Li 			ndev->stats.rx_frame_errors++;
915793fc096SFrank Li 			goto rx_processing_done;
916793fc096SFrank Li 		}
917793fc096SFrank Li 
918793fc096SFrank Li 		/* Process the incoming frame. */
919793fc096SFrank Li 		ndev->stats.rx_packets++;
920793fc096SFrank Li 		pkt_len = bdp->cbd_datlen;
921793fc096SFrank Li 		ndev->stats.rx_bytes += pkt_len;
922793fc096SFrank Li 
923d842a31fSDuan Fugang-B38611 		if (fep->bufdesc_ex)
924d842a31fSDuan Fugang-B38611 			index = (struct bufdesc_ex *)bdp -
925d842a31fSDuan Fugang-B38611 				(struct bufdesc_ex *)fep->rx_bd_base;
926d842a31fSDuan Fugang-B38611 		else
927d842a31fSDuan Fugang-B38611 			index = bdp - fep->rx_bd_base;
928d842a31fSDuan Fugang-B38611 		data = fep->rx_skbuff[index]->data;
929d842a31fSDuan Fugang-B38611 		dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
930d842a31fSDuan Fugang-B38611 					FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
931793fc096SFrank Li 
932793fc096SFrank Li 		if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
933793fc096SFrank Li 			swap_buffer(data, pkt_len);
934793fc096SFrank Li 
935cdffcf1bSJim Baxter 		/* Extract the enhanced buffer descriptor */
936cdffcf1bSJim Baxter 		ebdp = NULL;
937cdffcf1bSJim Baxter 		if (fep->bufdesc_ex)
938cdffcf1bSJim Baxter 			ebdp = (struct bufdesc_ex *)bdp;
939cdffcf1bSJim Baxter 
940cdffcf1bSJim Baxter 		/* If this is a VLAN packet remove the VLAN Tag */
941cdffcf1bSJim Baxter 		vlan_packet_rcvd = false;
942cdffcf1bSJim Baxter 		if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
943cdffcf1bSJim Baxter 		    fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
944cdffcf1bSJim Baxter 			/* Push and remove the vlan tag */
945cdffcf1bSJim Baxter 			struct vlan_hdr *vlan_header =
946cdffcf1bSJim Baxter 					(struct vlan_hdr *) (data + ETH_HLEN);
947cdffcf1bSJim Baxter 			vlan_tag = ntohs(vlan_header->h_vlan_TCI);
948cdffcf1bSJim Baxter 			pkt_len -= VLAN_HLEN;
949cdffcf1bSJim Baxter 
950cdffcf1bSJim Baxter 			vlan_packet_rcvd = true;
951cdffcf1bSJim Baxter 		}
952cdffcf1bSJim Baxter 
953793fc096SFrank Li 		/* This does 16 byte alignment, exactly what we need.
954793fc096SFrank Li 		 * The packet length includes FCS, but we don't want to
955793fc096SFrank Li 		 * include that when passing upstream as it messes up
956793fc096SFrank Li 		 * bridging applications.
957793fc096SFrank Li 		 */
958793fc096SFrank Li 		skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
959793fc096SFrank Li 
960793fc096SFrank Li 		if (unlikely(!skb)) {
961793fc096SFrank Li 			ndev->stats.rx_dropped++;
962793fc096SFrank Li 		} else {
963cdffcf1bSJim Baxter 			int payload_offset = (2 * ETH_ALEN);
964793fc096SFrank Li 			skb_reserve(skb, NET_IP_ALIGN);
965793fc096SFrank Li 			skb_put(skb, pkt_len - 4);	/* Make room */
966cdffcf1bSJim Baxter 
967cdffcf1bSJim Baxter 			/* Extract the frame data without the VLAN header. */
968cdffcf1bSJim Baxter 			skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
969cdffcf1bSJim Baxter 			if (vlan_packet_rcvd)
970cdffcf1bSJim Baxter 				payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
971cdffcf1bSJim Baxter 			skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
972cdffcf1bSJim Baxter 						       data + payload_offset,
973cdffcf1bSJim Baxter 						       pkt_len - 4 - (2 * ETH_ALEN));
974cdffcf1bSJim Baxter 
975793fc096SFrank Li 			skb->protocol = eth_type_trans(skb, ndev);
976793fc096SFrank Li 
977793fc096SFrank Li 			/* Get receive timestamp from the skb */
978793fc096SFrank Li 			if (fep->hwts_rx_en && fep->bufdesc_ex) {
979793fc096SFrank Li 				struct skb_shared_hwtstamps *shhwtstamps =
980793fc096SFrank Li 							    skb_hwtstamps(skb);
981793fc096SFrank Li 				unsigned long flags;
982793fc096SFrank Li 
983793fc096SFrank Li 				memset(shhwtstamps, 0, sizeof(*shhwtstamps));
984793fc096SFrank Li 
985793fc096SFrank Li 				spin_lock_irqsave(&fep->tmreg_lock, flags);
986793fc096SFrank Li 				shhwtstamps->hwtstamp = ns_to_ktime(
987793fc096SFrank Li 				    timecounter_cyc2time(&fep->tc, ebdp->ts));
988793fc096SFrank Li 				spin_unlock_irqrestore(&fep->tmreg_lock, flags);
989793fc096SFrank Li 			}
990793fc096SFrank Li 
9914c09eed9SJim Baxter 			if (fep->bufdesc_ex &&
9924c09eed9SJim Baxter 			    (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
9934c09eed9SJim Baxter 				if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
9944c09eed9SJim Baxter 					/* don't check it */
9954c09eed9SJim Baxter 					skb->ip_summed = CHECKSUM_UNNECESSARY;
9964c09eed9SJim Baxter 				} else {
9974c09eed9SJim Baxter 					skb_checksum_none_assert(skb);
9984c09eed9SJim Baxter 				}
9994c09eed9SJim Baxter 			}
10004c09eed9SJim Baxter 
1001cdffcf1bSJim Baxter 			/* Handle received VLAN packets */
1002cdffcf1bSJim Baxter 			if (vlan_packet_rcvd)
1003cdffcf1bSJim Baxter 				__vlan_hwaccel_put_tag(skb,
1004cdffcf1bSJim Baxter 						       htons(ETH_P_8021Q),
1005cdffcf1bSJim Baxter 						       vlan_tag);
1006cdffcf1bSJim Baxter 
1007793fc096SFrank Li 			napi_gro_receive(&fep->napi, skb);
1008793fc096SFrank Li 		}
1009793fc096SFrank Li 
1010d842a31fSDuan Fugang-B38611 		dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1011d842a31fSDuan Fugang-B38611 					FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1012793fc096SFrank Li rx_processing_done:
1013793fc096SFrank Li 		/* Clear the status flags for this buffer */
1014793fc096SFrank Li 		status &= ~BD_ENET_RX_STATS;
1015793fc096SFrank Li 
1016793fc096SFrank Li 		/* Mark the buffer empty */
1017793fc096SFrank Li 		status |= BD_ENET_RX_EMPTY;
1018793fc096SFrank Li 		bdp->cbd_sc = status;
1019793fc096SFrank Li 
1020793fc096SFrank Li 		if (fep->bufdesc_ex) {
1021793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1022793fc096SFrank Li 
1023793fc096SFrank Li 			ebdp->cbd_esc = BD_ENET_RX_INT;
1024793fc096SFrank Li 			ebdp->cbd_prot = 0;
1025793fc096SFrank Li 			ebdp->cbd_bdu = 0;
1026793fc096SFrank Li 		}
1027793fc096SFrank Li 
1028793fc096SFrank Li 		/* Update BD pointer to next entry */
102936e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
103036e24e2eSDuan Fugang-B38611 
1031793fc096SFrank Li 		/* Doing this here will keep the FEC running while we process
1032793fc096SFrank Li 		 * incoming frames.  On a heavily loaded network, we should be
1033793fc096SFrank Li 		 * able to keep up at the expense of system resources.
1034793fc096SFrank Li 		 */
1035793fc096SFrank Li 		writel(0, fep->hwp + FEC_R_DES_ACTIVE);
1036793fc096SFrank Li 	}
1037793fc096SFrank Li 	fep->cur_rx = bdp;
1038793fc096SFrank Li 
1039793fc096SFrank Li 	return pkt_received;
1040793fc096SFrank Li }
1041793fc096SFrank Li 
1042793fc096SFrank Li static irqreturn_t
1043793fc096SFrank Li fec_enet_interrupt(int irq, void *dev_id)
1044793fc096SFrank Li {
1045793fc096SFrank Li 	struct net_device *ndev = dev_id;
1046793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1047793fc096SFrank Li 	uint int_events;
1048793fc096SFrank Li 	irqreturn_t ret = IRQ_NONE;
1049793fc096SFrank Li 
1050793fc096SFrank Li 	do {
1051793fc096SFrank Li 		int_events = readl(fep->hwp + FEC_IEVENT);
1052793fc096SFrank Li 		writel(int_events, fep->hwp + FEC_IEVENT);
1053793fc096SFrank Li 
1054793fc096SFrank Li 		if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
1055793fc096SFrank Li 			ret = IRQ_HANDLED;
1056793fc096SFrank Li 
1057793fc096SFrank Li 			/* Disable the RX interrupt */
1058793fc096SFrank Li 			if (napi_schedule_prep(&fep->napi)) {
1059793fc096SFrank Li 				writel(FEC_RX_DISABLED_IMASK,
1060793fc096SFrank Li 					fep->hwp + FEC_IMASK);
1061793fc096SFrank Li 				__napi_schedule(&fep->napi);
1062793fc096SFrank Li 			}
1063793fc096SFrank Li 		}
1064793fc096SFrank Li 
1065793fc096SFrank Li 		if (int_events & FEC_ENET_MII) {
1066793fc096SFrank Li 			ret = IRQ_HANDLED;
1067793fc096SFrank Li 			complete(&fep->mdio_done);
1068793fc096SFrank Li 		}
1069793fc096SFrank Li 	} while (int_events);
1070793fc096SFrank Li 
1071793fc096SFrank Li 	return ret;
1072793fc096SFrank Li }
1073793fc096SFrank Li 
1074793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1075793fc096SFrank Li {
1076793fc096SFrank Li 	struct net_device *ndev = napi->dev;
1077793fc096SFrank Li 	int pkts = fec_enet_rx(ndev, budget);
1078793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1079793fc096SFrank Li 
1080793fc096SFrank Li 	fec_enet_tx(ndev);
1081793fc096SFrank Li 
1082793fc096SFrank Li 	if (pkts < budget) {
1083793fc096SFrank Li 		napi_complete(napi);
1084793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1085793fc096SFrank Li 	}
1086793fc096SFrank Li 	return pkts;
1087793fc096SFrank Li }
1088793fc096SFrank Li 
1089793fc096SFrank Li /* ------------------------------------------------------------------------- */
1090793fc096SFrank Li static void fec_get_mac(struct net_device *ndev)
1091793fc096SFrank Li {
1092793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
109394660ba0SJingoo Han 	struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1094793fc096SFrank Li 	unsigned char *iap, tmpaddr[ETH_ALEN];
1095793fc096SFrank Li 
1096793fc096SFrank Li 	/*
1097793fc096SFrank Li 	 * try to get mac address in following order:
1098793fc096SFrank Li 	 *
1099793fc096SFrank Li 	 * 1) module parameter via kernel command line in form
1100793fc096SFrank Li 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1101793fc096SFrank Li 	 */
1102793fc096SFrank Li 	iap = macaddr;
1103793fc096SFrank Li 
1104793fc096SFrank Li 	/*
1105793fc096SFrank Li 	 * 2) from device tree data
1106793fc096SFrank Li 	 */
1107793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1108793fc096SFrank Li 		struct device_node *np = fep->pdev->dev.of_node;
1109793fc096SFrank Li 		if (np) {
1110793fc096SFrank Li 			const char *mac = of_get_mac_address(np);
1111793fc096SFrank Li 			if (mac)
1112793fc096SFrank Li 				iap = (unsigned char *) mac;
1113793fc096SFrank Li 		}
1114793fc096SFrank Li 	}
1115793fc096SFrank Li 
1116793fc096SFrank Li 	/*
1117793fc096SFrank Li 	 * 3) from flash or fuse (via platform data)
1118793fc096SFrank Li 	 */
1119793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1120793fc096SFrank Li #ifdef CONFIG_M5272
1121793fc096SFrank Li 		if (FEC_FLASHMAC)
1122793fc096SFrank Li 			iap = (unsigned char *)FEC_FLASHMAC;
1123793fc096SFrank Li #else
1124793fc096SFrank Li 		if (pdata)
1125793fc096SFrank Li 			iap = (unsigned char *)&pdata->mac;
1126793fc096SFrank Li #endif
1127793fc096SFrank Li 	}
1128793fc096SFrank Li 
1129793fc096SFrank Li 	/*
1130793fc096SFrank Li 	 * 4) FEC mac registers set by bootloader
1131793fc096SFrank Li 	 */
1132793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
11337d7628f3SDan Carpenter 		*((__be32 *) &tmpaddr[0]) =
11347d7628f3SDan Carpenter 			cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
11357d7628f3SDan Carpenter 		*((__be16 *) &tmpaddr[4]) =
11367d7628f3SDan Carpenter 			cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1137793fc096SFrank Li 		iap = &tmpaddr[0];
1138793fc096SFrank Li 	}
1139793fc096SFrank Li 
1140ff5b2fabSLucas Stach 	/*
1141ff5b2fabSLucas Stach 	 * 5) random mac address
1142ff5b2fabSLucas Stach 	 */
1143ff5b2fabSLucas Stach 	if (!is_valid_ether_addr(iap)) {
1144ff5b2fabSLucas Stach 		/* Report it and use a random ethernet address instead */
1145ff5b2fabSLucas Stach 		netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1146ff5b2fabSLucas Stach 		eth_hw_addr_random(ndev);
1147ff5b2fabSLucas Stach 		netdev_info(ndev, "Using random MAC address: %pM\n",
1148ff5b2fabSLucas Stach 			    ndev->dev_addr);
1149ff5b2fabSLucas Stach 		return;
1150ff5b2fabSLucas Stach 	}
1151ff5b2fabSLucas Stach 
1152793fc096SFrank Li 	memcpy(ndev->dev_addr, iap, ETH_ALEN);
1153793fc096SFrank Li 
1154793fc096SFrank Li 	/* Adjust MAC if using macaddr */
1155793fc096SFrank Li 	if (iap == macaddr)
1156793fc096SFrank Li 		 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1157793fc096SFrank Li }
1158793fc096SFrank Li 
1159793fc096SFrank Li /* ------------------------------------------------------------------------- */
1160793fc096SFrank Li 
1161793fc096SFrank Li /*
1162793fc096SFrank Li  * Phy section
1163793fc096SFrank Li  */
1164793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev)
1165793fc096SFrank Li {
1166793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1167793fc096SFrank Li 	struct phy_device *phy_dev = fep->phy_dev;
1168793fc096SFrank Li 	int status_change = 0;
1169793fc096SFrank Li 
1170793fc096SFrank Li 	/* Prevent a state halted on mii error */
1171793fc096SFrank Li 	if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1172793fc096SFrank Li 		phy_dev->state = PHY_RESUMING;
117354309fa6SFrank Li 		return;
1174793fc096SFrank Li 	}
1175793fc096SFrank Li 
1176793fc096SFrank Li 	if (phy_dev->link) {
1177793fc096SFrank Li 		if (!fep->link) {
1178793fc096SFrank Li 			fep->link = phy_dev->link;
1179793fc096SFrank Li 			status_change = 1;
1180793fc096SFrank Li 		}
1181793fc096SFrank Li 
1182793fc096SFrank Li 		if (fep->full_duplex != phy_dev->duplex)
1183793fc096SFrank Li 			status_change = 1;
1184793fc096SFrank Li 
1185793fc096SFrank Li 		if (phy_dev->speed != fep->speed) {
1186793fc096SFrank Li 			fep->speed = phy_dev->speed;
1187793fc096SFrank Li 			status_change = 1;
1188793fc096SFrank Li 		}
1189793fc096SFrank Li 
1190793fc096SFrank Li 		/* if any of the above changed restart the FEC */
1191793fc096SFrank Li 		if (status_change)
1192793fc096SFrank Li 			fec_restart(ndev, phy_dev->duplex);
1193793fc096SFrank Li 	} else {
1194793fc096SFrank Li 		if (fep->link) {
1195793fc096SFrank Li 			fec_stop(ndev);
11966e0895c2SDavid S. Miller 			fep->link = phy_dev->link;
1197793fc096SFrank Li 			status_change = 1;
1198793fc096SFrank Li 		}
1199793fc096SFrank Li 	}
1200793fc096SFrank Li 
1201793fc096SFrank Li 	if (status_change)
1202793fc096SFrank Li 		phy_print_status(phy_dev);
1203793fc096SFrank Li }
1204793fc096SFrank Li 
1205793fc096SFrank Li static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1206793fc096SFrank Li {
1207793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
1208793fc096SFrank Li 	unsigned long time_left;
1209793fc096SFrank Li 
1210793fc096SFrank Li 	fep->mii_timeout = 0;
1211793fc096SFrank Li 	init_completion(&fep->mdio_done);
1212793fc096SFrank Li 
1213793fc096SFrank Li 	/* start a read op */
1214793fc096SFrank Li 	writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1215793fc096SFrank Li 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1216793fc096SFrank Li 		FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1217793fc096SFrank Li 
1218793fc096SFrank Li 	/* wait for end of transfer */
1219793fc096SFrank Li 	time_left = wait_for_completion_timeout(&fep->mdio_done,
1220793fc096SFrank Li 			usecs_to_jiffies(FEC_MII_TIMEOUT));
1221793fc096SFrank Li 	if (time_left == 0) {
1222793fc096SFrank Li 		fep->mii_timeout = 1;
122331b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO read timeout\n");
1224793fc096SFrank Li 		return -ETIMEDOUT;
1225793fc096SFrank Li 	}
1226793fc096SFrank Li 
1227793fc096SFrank Li 	/* return value */
1228793fc096SFrank Li 	return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1229793fc096SFrank Li }
1230793fc096SFrank Li 
1231793fc096SFrank Li static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1232793fc096SFrank Li 			   u16 value)
1233793fc096SFrank Li {
1234793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
1235793fc096SFrank Li 	unsigned long time_left;
1236793fc096SFrank Li 
1237793fc096SFrank Li 	fep->mii_timeout = 0;
1238793fc096SFrank Li 	init_completion(&fep->mdio_done);
1239793fc096SFrank Li 
1240793fc096SFrank Li 	/* start a write op */
1241793fc096SFrank Li 	writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1242793fc096SFrank Li 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1243793fc096SFrank Li 		FEC_MMFR_TA | FEC_MMFR_DATA(value),
1244793fc096SFrank Li 		fep->hwp + FEC_MII_DATA);
1245793fc096SFrank Li 
1246793fc096SFrank Li 	/* wait for end of transfer */
1247793fc096SFrank Li 	time_left = wait_for_completion_timeout(&fep->mdio_done,
1248793fc096SFrank Li 			usecs_to_jiffies(FEC_MII_TIMEOUT));
1249793fc096SFrank Li 	if (time_left == 0) {
1250793fc096SFrank Li 		fep->mii_timeout = 1;
125131b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO write timeout\n");
1252793fc096SFrank Li 		return -ETIMEDOUT;
1253793fc096SFrank Li 	}
1254793fc096SFrank Li 
1255793fc096SFrank Li 	return 0;
1256793fc096SFrank Li }
1257793fc096SFrank Li 
1258793fc096SFrank Li static int fec_enet_mdio_reset(struct mii_bus *bus)
1259793fc096SFrank Li {
1260793fc096SFrank Li 	return 0;
1261793fc096SFrank Li }
1262793fc096SFrank Li 
1263793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev)
1264793fc096SFrank Li {
1265793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1266793fc096SFrank Li 	const struct platform_device_id *id_entry =
1267793fc096SFrank Li 				platform_get_device_id(fep->pdev);
1268793fc096SFrank Li 	struct phy_device *phy_dev = NULL;
1269793fc096SFrank Li 	char mdio_bus_id[MII_BUS_ID_SIZE];
1270793fc096SFrank Li 	char phy_name[MII_BUS_ID_SIZE + 3];
1271793fc096SFrank Li 	int phy_id;
1272793fc096SFrank Li 	int dev_id = fep->dev_id;
1273793fc096SFrank Li 
1274793fc096SFrank Li 	fep->phy_dev = NULL;
1275793fc096SFrank Li 
1276793fc096SFrank Li 	/* check for attached phy */
1277793fc096SFrank Li 	for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1278793fc096SFrank Li 		if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1279793fc096SFrank Li 			continue;
1280793fc096SFrank Li 		if (fep->mii_bus->phy_map[phy_id] == NULL)
1281793fc096SFrank Li 			continue;
1282793fc096SFrank Li 		if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1283793fc096SFrank Li 			continue;
1284793fc096SFrank Li 		if (dev_id--)
1285793fc096SFrank Li 			continue;
1286793fc096SFrank Li 		strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1287793fc096SFrank Li 		break;
1288793fc096SFrank Li 	}
1289793fc096SFrank Li 
1290793fc096SFrank Li 	if (phy_id >= PHY_MAX_ADDR) {
129131b7720cSJoe Perches 		netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1292793fc096SFrank Li 		strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1293793fc096SFrank Li 		phy_id = 0;
1294793fc096SFrank Li 	}
1295793fc096SFrank Li 
1296793fc096SFrank Li 	snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
1297793fc096SFrank Li 	phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1298793fc096SFrank Li 			      fep->phy_interface);
1299793fc096SFrank Li 	if (IS_ERR(phy_dev)) {
130031b7720cSJoe Perches 		netdev_err(ndev, "could not attach to PHY\n");
1301793fc096SFrank Li 		return PTR_ERR(phy_dev);
1302793fc096SFrank Li 	}
1303793fc096SFrank Li 
1304793fc096SFrank Li 	/* mask with MAC supported features */
1305793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
1306793fc096SFrank Li 		phy_dev->supported &= PHY_GBIT_FEATURES;
1307d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1308793fc096SFrank Li 		phy_dev->supported |= SUPPORTED_Pause;
1309d1391930SGuenter Roeck #endif
1310793fc096SFrank Li 	}
1311793fc096SFrank Li 	else
1312793fc096SFrank Li 		phy_dev->supported &= PHY_BASIC_FEATURES;
1313793fc096SFrank Li 
1314793fc096SFrank Li 	phy_dev->advertising = phy_dev->supported;
1315793fc096SFrank Li 
1316793fc096SFrank Li 	fep->phy_dev = phy_dev;
1317793fc096SFrank Li 	fep->link = 0;
1318793fc096SFrank Li 	fep->full_duplex = 0;
1319793fc096SFrank Li 
132031b7720cSJoe Perches 	netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1321793fc096SFrank Li 		    fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1322793fc096SFrank Li 		    fep->phy_dev->irq);
1323793fc096SFrank Li 
1324793fc096SFrank Li 	return 0;
1325793fc096SFrank Li }
1326793fc096SFrank Li 
1327793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev)
1328793fc096SFrank Li {
1329793fc096SFrank Li 	static struct mii_bus *fec0_mii_bus;
1330793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
1331793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1332793fc096SFrank Li 	const struct platform_device_id *id_entry =
1333793fc096SFrank Li 				platform_get_device_id(fep->pdev);
1334793fc096SFrank Li 	int err = -ENXIO, i;
1335793fc096SFrank Li 
1336793fc096SFrank Li 	/*
1337793fc096SFrank Li 	 * The dual fec interfaces are not equivalent with enet-mac.
1338793fc096SFrank Li 	 * Here are the differences:
1339793fc096SFrank Li 	 *
1340793fc096SFrank Li 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
1341793fc096SFrank Li 	 *  - fec0 acts as the 1588 time master while fec1 is slave
1342793fc096SFrank Li 	 *  - external phys can only be configured by fec0
1343793fc096SFrank Li 	 *
1344793fc096SFrank Li 	 * That is to say fec1 can not work independently. It only works
1345793fc096SFrank Li 	 * when fec0 is working. The reason behind this design is that the
1346793fc096SFrank Li 	 * second interface is added primarily for Switch mode.
1347793fc096SFrank Li 	 *
1348793fc096SFrank Li 	 * Because of the last point above, both phys are attached on fec0
1349793fc096SFrank Li 	 * mdio interface in board design, and need to be configured by
1350793fc096SFrank Li 	 * fec0 mii_bus.
1351793fc096SFrank Li 	 */
1352793fc096SFrank Li 	if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
1353793fc096SFrank Li 		/* fec1 uses fec0 mii_bus */
1354793fc096SFrank Li 		if (mii_cnt && fec0_mii_bus) {
1355793fc096SFrank Li 			fep->mii_bus = fec0_mii_bus;
1356793fc096SFrank Li 			mii_cnt++;
1357793fc096SFrank Li 			return 0;
1358793fc096SFrank Li 		}
1359793fc096SFrank Li 		return -ENOENT;
1360793fc096SFrank Li 	}
1361793fc096SFrank Li 
1362793fc096SFrank Li 	fep->mii_timeout = 0;
1363793fc096SFrank Li 
1364793fc096SFrank Li 	/*
1365793fc096SFrank Li 	 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1366793fc096SFrank Li 	 *
1367793fc096SFrank Li 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1368793fc096SFrank Li 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
1369793fc096SFrank Li 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1370793fc096SFrank Li 	 * document.
1371793fc096SFrank Li 	 */
1372793fc096SFrank Li 	fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
1373793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1374793fc096SFrank Li 		fep->phy_speed--;
1375793fc096SFrank Li 	fep->phy_speed <<= 1;
1376793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1377793fc096SFrank Li 
1378793fc096SFrank Li 	fep->mii_bus = mdiobus_alloc();
1379793fc096SFrank Li 	if (fep->mii_bus == NULL) {
1380793fc096SFrank Li 		err = -ENOMEM;
1381793fc096SFrank Li 		goto err_out;
1382793fc096SFrank Li 	}
1383793fc096SFrank Li 
1384793fc096SFrank Li 	fep->mii_bus->name = "fec_enet_mii_bus";
1385793fc096SFrank Li 	fep->mii_bus->read = fec_enet_mdio_read;
1386793fc096SFrank Li 	fep->mii_bus->write = fec_enet_mdio_write;
1387793fc096SFrank Li 	fep->mii_bus->reset = fec_enet_mdio_reset;
1388793fc096SFrank Li 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1389793fc096SFrank Li 		pdev->name, fep->dev_id + 1);
1390793fc096SFrank Li 	fep->mii_bus->priv = fep;
1391793fc096SFrank Li 	fep->mii_bus->parent = &pdev->dev;
1392793fc096SFrank Li 
1393793fc096SFrank Li 	fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1394793fc096SFrank Li 	if (!fep->mii_bus->irq) {
1395793fc096SFrank Li 		err = -ENOMEM;
1396793fc096SFrank Li 		goto err_out_free_mdiobus;
1397793fc096SFrank Li 	}
1398793fc096SFrank Li 
1399793fc096SFrank Li 	for (i = 0; i < PHY_MAX_ADDR; i++)
1400793fc096SFrank Li 		fep->mii_bus->irq[i] = PHY_POLL;
1401793fc096SFrank Li 
1402793fc096SFrank Li 	if (mdiobus_register(fep->mii_bus))
1403793fc096SFrank Li 		goto err_out_free_mdio_irq;
1404793fc096SFrank Li 
1405793fc096SFrank Li 	mii_cnt++;
1406793fc096SFrank Li 
1407793fc096SFrank Li 	/* save fec0 mii_bus */
1408793fc096SFrank Li 	if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1409793fc096SFrank Li 		fec0_mii_bus = fep->mii_bus;
1410793fc096SFrank Li 
1411793fc096SFrank Li 	return 0;
1412793fc096SFrank Li 
1413793fc096SFrank Li err_out_free_mdio_irq:
1414793fc096SFrank Li 	kfree(fep->mii_bus->irq);
1415793fc096SFrank Li err_out_free_mdiobus:
1416793fc096SFrank Li 	mdiobus_free(fep->mii_bus);
1417793fc096SFrank Li err_out:
1418793fc096SFrank Li 	return err;
1419793fc096SFrank Li }
1420793fc096SFrank Li 
1421793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep)
1422793fc096SFrank Li {
1423793fc096SFrank Li 	if (--mii_cnt == 0) {
1424793fc096SFrank Li 		mdiobus_unregister(fep->mii_bus);
1425793fc096SFrank Li 		kfree(fep->mii_bus->irq);
1426793fc096SFrank Li 		mdiobus_free(fep->mii_bus);
1427793fc096SFrank Li 	}
1428793fc096SFrank Li }
1429793fc096SFrank Li 
1430793fc096SFrank Li static int fec_enet_get_settings(struct net_device *ndev,
1431793fc096SFrank Li 				  struct ethtool_cmd *cmd)
1432793fc096SFrank Li {
1433793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1434793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
1435793fc096SFrank Li 
1436793fc096SFrank Li 	if (!phydev)
1437793fc096SFrank Li 		return -ENODEV;
1438793fc096SFrank Li 
1439793fc096SFrank Li 	return phy_ethtool_gset(phydev, cmd);
1440793fc096SFrank Li }
1441793fc096SFrank Li 
1442793fc096SFrank Li static int fec_enet_set_settings(struct net_device *ndev,
1443793fc096SFrank Li 				 struct ethtool_cmd *cmd)
1444793fc096SFrank Li {
1445793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1446793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
1447793fc096SFrank Li 
1448793fc096SFrank Li 	if (!phydev)
1449793fc096SFrank Li 		return -ENODEV;
1450793fc096SFrank Li 
1451793fc096SFrank Li 	return phy_ethtool_sset(phydev, cmd);
1452793fc096SFrank Li }
1453793fc096SFrank Li 
1454793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev,
1455793fc096SFrank Li 				 struct ethtool_drvinfo *info)
1456793fc096SFrank Li {
1457793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1458793fc096SFrank Li 
1459793fc096SFrank Li 	strlcpy(info->driver, fep->pdev->dev.driver->name,
1460793fc096SFrank Li 		sizeof(info->driver));
1461793fc096SFrank Li 	strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1462793fc096SFrank Li 	strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
1463793fc096SFrank Li }
1464793fc096SFrank Li 
1465793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev,
1466793fc096SFrank Li 				struct ethtool_ts_info *info)
1467793fc096SFrank Li {
1468793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1469793fc096SFrank Li 
1470793fc096SFrank Li 	if (fep->bufdesc_ex) {
1471793fc096SFrank Li 
1472793fc096SFrank Li 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1473793fc096SFrank Li 					SOF_TIMESTAMPING_RX_SOFTWARE |
1474793fc096SFrank Li 					SOF_TIMESTAMPING_SOFTWARE |
1475793fc096SFrank Li 					SOF_TIMESTAMPING_TX_HARDWARE |
1476793fc096SFrank Li 					SOF_TIMESTAMPING_RX_HARDWARE |
1477793fc096SFrank Li 					SOF_TIMESTAMPING_RAW_HARDWARE;
1478793fc096SFrank Li 		if (fep->ptp_clock)
1479793fc096SFrank Li 			info->phc_index = ptp_clock_index(fep->ptp_clock);
1480793fc096SFrank Li 		else
1481793fc096SFrank Li 			info->phc_index = -1;
1482793fc096SFrank Li 
1483793fc096SFrank Li 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1484793fc096SFrank Li 				 (1 << HWTSTAMP_TX_ON);
1485793fc096SFrank Li 
1486793fc096SFrank Li 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1487793fc096SFrank Li 				   (1 << HWTSTAMP_FILTER_ALL);
1488793fc096SFrank Li 		return 0;
1489793fc096SFrank Li 	} else {
1490793fc096SFrank Li 		return ethtool_op_get_ts_info(ndev, info);
1491793fc096SFrank Li 	}
1492793fc096SFrank Li }
1493793fc096SFrank Li 
1494d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1495d1391930SGuenter Roeck 
1496793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev,
1497793fc096SFrank Li 				    struct ethtool_pauseparam *pause)
1498793fc096SFrank Li {
1499793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1500793fc096SFrank Li 
1501793fc096SFrank Li 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1502793fc096SFrank Li 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1503793fc096SFrank Li 	pause->rx_pause = pause->tx_pause;
1504793fc096SFrank Li }
1505793fc096SFrank Li 
1506793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev,
1507793fc096SFrank Li 				   struct ethtool_pauseparam *pause)
1508793fc096SFrank Li {
1509793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1510793fc096SFrank Li 
1511793fc096SFrank Li 	if (pause->tx_pause != pause->rx_pause) {
1512793fc096SFrank Li 		netdev_info(ndev,
1513793fc096SFrank Li 			"hardware only support enable/disable both tx and rx");
1514793fc096SFrank Li 		return -EINVAL;
1515793fc096SFrank Li 	}
1516793fc096SFrank Li 
1517793fc096SFrank Li 	fep->pause_flag = 0;
1518793fc096SFrank Li 
1519793fc096SFrank Li 	/* tx pause must be same as rx pause */
1520793fc096SFrank Li 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1521793fc096SFrank Li 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1522793fc096SFrank Li 
1523793fc096SFrank Li 	if (pause->rx_pause || pause->autoneg) {
1524793fc096SFrank Li 		fep->phy_dev->supported |= ADVERTISED_Pause;
1525793fc096SFrank Li 		fep->phy_dev->advertising |= ADVERTISED_Pause;
1526793fc096SFrank Li 	} else {
1527793fc096SFrank Li 		fep->phy_dev->supported &= ~ADVERTISED_Pause;
1528793fc096SFrank Li 		fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1529793fc096SFrank Li 	}
1530793fc096SFrank Li 
1531793fc096SFrank Li 	if (pause->autoneg) {
1532793fc096SFrank Li 		if (netif_running(ndev))
1533793fc096SFrank Li 			fec_stop(ndev);
1534793fc096SFrank Li 		phy_start_aneg(fep->phy_dev);
1535793fc096SFrank Li 	}
1536793fc096SFrank Li 	if (netif_running(ndev))
1537793fc096SFrank Li 		fec_restart(ndev, 0);
1538793fc096SFrank Li 
1539793fc096SFrank Li 	return 0;
1540793fc096SFrank Li }
1541793fc096SFrank Li 
154238ae92dcSChris Healy static const struct fec_stat {
154338ae92dcSChris Healy 	char name[ETH_GSTRING_LEN];
154438ae92dcSChris Healy 	u16 offset;
154538ae92dcSChris Healy } fec_stats[] = {
154638ae92dcSChris Healy 	/* RMON TX */
154738ae92dcSChris Healy 	{ "tx_dropped", RMON_T_DROP },
154838ae92dcSChris Healy 	{ "tx_packets", RMON_T_PACKETS },
154938ae92dcSChris Healy 	{ "tx_broadcast", RMON_T_BC_PKT },
155038ae92dcSChris Healy 	{ "tx_multicast", RMON_T_MC_PKT },
155138ae92dcSChris Healy 	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
155238ae92dcSChris Healy 	{ "tx_undersize", RMON_T_UNDERSIZE },
155338ae92dcSChris Healy 	{ "tx_oversize", RMON_T_OVERSIZE },
155438ae92dcSChris Healy 	{ "tx_fragment", RMON_T_FRAG },
155538ae92dcSChris Healy 	{ "tx_jabber", RMON_T_JAB },
155638ae92dcSChris Healy 	{ "tx_collision", RMON_T_COL },
155738ae92dcSChris Healy 	{ "tx_64byte", RMON_T_P64 },
155838ae92dcSChris Healy 	{ "tx_65to127byte", RMON_T_P65TO127 },
155938ae92dcSChris Healy 	{ "tx_128to255byte", RMON_T_P128TO255 },
156038ae92dcSChris Healy 	{ "tx_256to511byte", RMON_T_P256TO511 },
156138ae92dcSChris Healy 	{ "tx_512to1023byte", RMON_T_P512TO1023 },
156238ae92dcSChris Healy 	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
156338ae92dcSChris Healy 	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
156438ae92dcSChris Healy 	{ "tx_octets", RMON_T_OCTETS },
156538ae92dcSChris Healy 
156638ae92dcSChris Healy 	/* IEEE TX */
156738ae92dcSChris Healy 	{ "IEEE_tx_drop", IEEE_T_DROP },
156838ae92dcSChris Healy 	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
156938ae92dcSChris Healy 	{ "IEEE_tx_1col", IEEE_T_1COL },
157038ae92dcSChris Healy 	{ "IEEE_tx_mcol", IEEE_T_MCOL },
157138ae92dcSChris Healy 	{ "IEEE_tx_def", IEEE_T_DEF },
157238ae92dcSChris Healy 	{ "IEEE_tx_lcol", IEEE_T_LCOL },
157338ae92dcSChris Healy 	{ "IEEE_tx_excol", IEEE_T_EXCOL },
157438ae92dcSChris Healy 	{ "IEEE_tx_macerr", IEEE_T_MACERR },
157538ae92dcSChris Healy 	{ "IEEE_tx_cserr", IEEE_T_CSERR },
157638ae92dcSChris Healy 	{ "IEEE_tx_sqe", IEEE_T_SQE },
157738ae92dcSChris Healy 	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
157838ae92dcSChris Healy 	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
157938ae92dcSChris Healy 
158038ae92dcSChris Healy 	/* RMON RX */
158138ae92dcSChris Healy 	{ "rx_packets", RMON_R_PACKETS },
158238ae92dcSChris Healy 	{ "rx_broadcast", RMON_R_BC_PKT },
158338ae92dcSChris Healy 	{ "rx_multicast", RMON_R_MC_PKT },
158438ae92dcSChris Healy 	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
158538ae92dcSChris Healy 	{ "rx_undersize", RMON_R_UNDERSIZE },
158638ae92dcSChris Healy 	{ "rx_oversize", RMON_R_OVERSIZE },
158738ae92dcSChris Healy 	{ "rx_fragment", RMON_R_FRAG },
158838ae92dcSChris Healy 	{ "rx_jabber", RMON_R_JAB },
158938ae92dcSChris Healy 	{ "rx_64byte", RMON_R_P64 },
159038ae92dcSChris Healy 	{ "rx_65to127byte", RMON_R_P65TO127 },
159138ae92dcSChris Healy 	{ "rx_128to255byte", RMON_R_P128TO255 },
159238ae92dcSChris Healy 	{ "rx_256to511byte", RMON_R_P256TO511 },
159338ae92dcSChris Healy 	{ "rx_512to1023byte", RMON_R_P512TO1023 },
159438ae92dcSChris Healy 	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
159538ae92dcSChris Healy 	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
159638ae92dcSChris Healy 	{ "rx_octets", RMON_R_OCTETS },
159738ae92dcSChris Healy 
159838ae92dcSChris Healy 	/* IEEE RX */
159938ae92dcSChris Healy 	{ "IEEE_rx_drop", IEEE_R_DROP },
160038ae92dcSChris Healy 	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
160138ae92dcSChris Healy 	{ "IEEE_rx_crc", IEEE_R_CRC },
160238ae92dcSChris Healy 	{ "IEEE_rx_align", IEEE_R_ALIGN },
160338ae92dcSChris Healy 	{ "IEEE_rx_macerr", IEEE_R_MACERR },
160438ae92dcSChris Healy 	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
160538ae92dcSChris Healy 	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
160638ae92dcSChris Healy };
160738ae92dcSChris Healy 
160838ae92dcSChris Healy static void fec_enet_get_ethtool_stats(struct net_device *dev,
160938ae92dcSChris Healy 	struct ethtool_stats *stats, u64 *data)
161038ae92dcSChris Healy {
161138ae92dcSChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
161238ae92dcSChris Healy 	int i;
161338ae92dcSChris Healy 
161438ae92dcSChris Healy 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
161538ae92dcSChris Healy 		data[i] = readl(fep->hwp + fec_stats[i].offset);
161638ae92dcSChris Healy }
161738ae92dcSChris Healy 
161838ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev,
161938ae92dcSChris Healy 	u32 stringset, u8 *data)
162038ae92dcSChris Healy {
162138ae92dcSChris Healy 	int i;
162238ae92dcSChris Healy 	switch (stringset) {
162338ae92dcSChris Healy 	case ETH_SS_STATS:
162438ae92dcSChris Healy 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
162538ae92dcSChris Healy 			memcpy(data + i * ETH_GSTRING_LEN,
162638ae92dcSChris Healy 				fec_stats[i].name, ETH_GSTRING_LEN);
162738ae92dcSChris Healy 		break;
162838ae92dcSChris Healy 	}
162938ae92dcSChris Healy }
163038ae92dcSChris Healy 
163138ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset)
163238ae92dcSChris Healy {
163338ae92dcSChris Healy 	switch (sset) {
163438ae92dcSChris Healy 	case ETH_SS_STATS:
163538ae92dcSChris Healy 		return ARRAY_SIZE(fec_stats);
163638ae92dcSChris Healy 	default:
163738ae92dcSChris Healy 		return -EOPNOTSUPP;
163838ae92dcSChris Healy 	}
163938ae92dcSChris Healy }
1640d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
164138ae92dcSChris Healy 
164232bc9b46SChris Healy static int fec_enet_nway_reset(struct net_device *dev)
164332bc9b46SChris Healy {
164432bc9b46SChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
164532bc9b46SChris Healy 	struct phy_device *phydev = fep->phy_dev;
164632bc9b46SChris Healy 
164732bc9b46SChris Healy 	if (!phydev)
164832bc9b46SChris Healy 		return -ENODEV;
164932bc9b46SChris Healy 
165032bc9b46SChris Healy 	return genphy_restart_aneg(phydev);
165132bc9b46SChris Healy }
165232bc9b46SChris Healy 
1653793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = {
1654d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1655793fc096SFrank Li 	.get_pauseparam		= fec_enet_get_pauseparam,
1656793fc096SFrank Li 	.set_pauseparam		= fec_enet_set_pauseparam,
1657d1391930SGuenter Roeck #endif
1658793fc096SFrank Li 	.get_settings		= fec_enet_get_settings,
1659793fc096SFrank Li 	.set_settings		= fec_enet_set_settings,
1660793fc096SFrank Li 	.get_drvinfo		= fec_enet_get_drvinfo,
1661793fc096SFrank Li 	.get_link		= ethtool_op_get_link,
1662793fc096SFrank Li 	.get_ts_info		= fec_enet_get_ts_info,
166332bc9b46SChris Healy 	.nway_reset		= fec_enet_nway_reset,
166438ae92dcSChris Healy #ifndef CONFIG_M5272
166538ae92dcSChris Healy 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
166638ae92dcSChris Healy 	.get_strings		= fec_enet_get_strings,
166738ae92dcSChris Healy 	.get_sset_count		= fec_enet_get_sset_count,
166838ae92dcSChris Healy #endif
1669793fc096SFrank Li };
1670793fc096SFrank Li 
1671793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1672793fc096SFrank Li {
1673793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1674793fc096SFrank Li 	struct phy_device *phydev = fep->phy_dev;
1675793fc096SFrank Li 
1676793fc096SFrank Li 	if (!netif_running(ndev))
1677793fc096SFrank Li 		return -EINVAL;
1678793fc096SFrank Li 
1679793fc096SFrank Li 	if (!phydev)
1680793fc096SFrank Li 		return -ENODEV;
1681793fc096SFrank Li 
16821d5244d0SBen Hutchings 	if (fep->bufdesc_ex) {
16831d5244d0SBen Hutchings 		if (cmd == SIOCSHWTSTAMP)
16841d5244d0SBen Hutchings 			return fec_ptp_set(ndev, rq);
16851d5244d0SBen Hutchings 		if (cmd == SIOCGHWTSTAMP)
16861d5244d0SBen Hutchings 			return fec_ptp_get(ndev, rq);
16871d5244d0SBen Hutchings 	}
1688793fc096SFrank Li 
1689793fc096SFrank Li 	return phy_mii_ioctl(phydev, rq, cmd);
1690793fc096SFrank Li }
1691793fc096SFrank Li 
1692793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev)
1693793fc096SFrank Li {
1694793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1695793fc096SFrank Li 	unsigned int i;
1696793fc096SFrank Li 	struct sk_buff *skb;
1697793fc096SFrank Li 	struct bufdesc	*bdp;
1698793fc096SFrank Li 
1699793fc096SFrank Li 	bdp = fep->rx_bd_base;
170036e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->rx_ring_size; i++) {
1701793fc096SFrank Li 		skb = fep->rx_skbuff[i];
1702793fc096SFrank Li 
1703793fc096SFrank Li 		if (bdp->cbd_bufaddr)
1704793fc096SFrank Li 			dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1705793fc096SFrank Li 					FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1706793fc096SFrank Li 		if (skb)
1707793fc096SFrank Li 			dev_kfree_skb(skb);
170836e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
1709793fc096SFrank Li 	}
1710793fc096SFrank Li 
1711793fc096SFrank Li 	bdp = fep->tx_bd_base;
171236e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->tx_ring_size; i++)
1713793fc096SFrank Li 		kfree(fep->tx_bounce[i]);
1714793fc096SFrank Li }
1715793fc096SFrank Li 
1716793fc096SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev)
1717793fc096SFrank Li {
1718793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1719793fc096SFrank Li 	unsigned int i;
1720793fc096SFrank Li 	struct sk_buff *skb;
1721793fc096SFrank Li 	struct bufdesc	*bdp;
1722793fc096SFrank Li 
1723793fc096SFrank Li 	bdp = fep->rx_bd_base;
172436e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->rx_ring_size; i++) {
1725793fc096SFrank Li 		skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1726793fc096SFrank Li 		if (!skb) {
1727793fc096SFrank Li 			fec_enet_free_buffers(ndev);
1728793fc096SFrank Li 			return -ENOMEM;
1729793fc096SFrank Li 		}
1730793fc096SFrank Li 		fep->rx_skbuff[i] = skb;
1731793fc096SFrank Li 
1732793fc096SFrank Li 		bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1733793fc096SFrank Li 				FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1734d842a31fSDuan Fugang-B38611 		if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1735d842a31fSDuan Fugang-B38611 			fec_enet_free_buffers(ndev);
1736d842a31fSDuan Fugang-B38611 			if (net_ratelimit())
1737d842a31fSDuan Fugang-B38611 				netdev_err(ndev, "Rx DMA memory map failed\n");
1738d842a31fSDuan Fugang-B38611 			return -ENOMEM;
1739d842a31fSDuan Fugang-B38611 		}
1740793fc096SFrank Li 		bdp->cbd_sc = BD_ENET_RX_EMPTY;
1741793fc096SFrank Li 
1742793fc096SFrank Li 		if (fep->bufdesc_ex) {
1743793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1744793fc096SFrank Li 			ebdp->cbd_esc = BD_ENET_RX_INT;
1745793fc096SFrank Li 		}
1746793fc096SFrank Li 
174736e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
1748793fc096SFrank Li 	}
1749793fc096SFrank Li 
1750793fc096SFrank Li 	/* Set the last buffer to wrap. */
175136e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_prevdesc(bdp, fep);
1752793fc096SFrank Li 	bdp->cbd_sc |= BD_SC_WRAP;
1753793fc096SFrank Li 
1754793fc096SFrank Li 	bdp = fep->tx_bd_base;
175536e24e2eSDuan Fugang-B38611 	for (i = 0; i < fep->tx_ring_size; i++) {
1756793fc096SFrank Li 		fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1757793fc096SFrank Li 
1758793fc096SFrank Li 		bdp->cbd_sc = 0;
1759793fc096SFrank Li 		bdp->cbd_bufaddr = 0;
1760793fc096SFrank Li 
1761793fc096SFrank Li 		if (fep->bufdesc_ex) {
1762793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
176396d2222bSJim Baxter 			ebdp->cbd_esc = BD_ENET_TX_INT;
1764793fc096SFrank Li 		}
1765793fc096SFrank Li 
176636e24e2eSDuan Fugang-B38611 		bdp = fec_enet_get_nextdesc(bdp, fep);
1767793fc096SFrank Li 	}
1768793fc096SFrank Li 
1769793fc096SFrank Li 	/* Set the last buffer to wrap. */
177036e24e2eSDuan Fugang-B38611 	bdp = fec_enet_get_prevdesc(bdp, fep);
1771793fc096SFrank Li 	bdp->cbd_sc |= BD_SC_WRAP;
1772793fc096SFrank Li 
1773793fc096SFrank Li 	return 0;
1774793fc096SFrank Li }
1775793fc096SFrank Li 
1776793fc096SFrank Li static int
1777793fc096SFrank Li fec_enet_open(struct net_device *ndev)
1778793fc096SFrank Li {
1779793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1780793fc096SFrank Li 	int ret;
1781793fc096SFrank Li 
1782793fc096SFrank Li 	/* I should reset the ring buffers here, but I don't yet know
1783793fc096SFrank Li 	 * a simple way to do that.
1784793fc096SFrank Li 	 */
1785793fc096SFrank Li 
1786793fc096SFrank Li 	ret = fec_enet_alloc_buffers(ndev);
1787793fc096SFrank Li 	if (ret)
1788793fc096SFrank Li 		return ret;
1789793fc096SFrank Li 
1790793fc096SFrank Li 	/* Probe and connect to PHY when open the interface */
1791793fc096SFrank Li 	ret = fec_enet_mii_probe(ndev);
1792793fc096SFrank Li 	if (ret) {
1793793fc096SFrank Li 		fec_enet_free_buffers(ndev);
1794793fc096SFrank Li 		return ret;
1795793fc096SFrank Li 	}
1796ce5eaf02SRussell King 
1797ce5eaf02SRussell King 	napi_enable(&fep->napi);
1798793fc096SFrank Li 	phy_start(fep->phy_dev);
1799793fc096SFrank Li 	netif_start_queue(ndev);
1800793fc096SFrank Li 	fep->opened = 1;
1801793fc096SFrank Li 	return 0;
1802793fc096SFrank Li }
1803793fc096SFrank Li 
1804793fc096SFrank Li static int
1805793fc096SFrank Li fec_enet_close(struct net_device *ndev)
1806793fc096SFrank Li {
1807793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1808793fc096SFrank Li 
1809793fc096SFrank Li 	/* Don't know what to do yet. */
1810793fc096SFrank Li 	napi_disable(&fep->napi);
1811793fc096SFrank Li 	fep->opened = 0;
1812793fc096SFrank Li 	netif_stop_queue(ndev);
1813793fc096SFrank Li 	fec_stop(ndev);
1814793fc096SFrank Li 
1815793fc096SFrank Li 	if (fep->phy_dev) {
1816793fc096SFrank Li 		phy_stop(fep->phy_dev);
1817793fc096SFrank Li 		phy_disconnect(fep->phy_dev);
1818793fc096SFrank Li 	}
1819793fc096SFrank Li 
1820793fc096SFrank Li 	fec_enet_free_buffers(ndev);
1821793fc096SFrank Li 
1822793fc096SFrank Li 	return 0;
1823793fc096SFrank Li }
1824793fc096SFrank Li 
1825793fc096SFrank Li /* Set or clear the multicast filter for this adaptor.
1826793fc096SFrank Li  * Skeleton taken from sunlance driver.
1827793fc096SFrank Li  * The CPM Ethernet implementation allows Multicast as well as individual
1828793fc096SFrank Li  * MAC address filtering.  Some of the drivers check to make sure it is
1829793fc096SFrank Li  * a group multicast address, and discard those that are not.  I guess I
1830793fc096SFrank Li  * will do the same for now, but just remove the test if you want
1831793fc096SFrank Li  * individual filtering as well (do the upper net layers want or support
1832793fc096SFrank Li  * this kind of feature?).
1833793fc096SFrank Li  */
1834793fc096SFrank Li 
1835793fc096SFrank Li #define HASH_BITS	6		/* #bits in hash */
1836793fc096SFrank Li #define CRC32_POLY	0xEDB88320
1837793fc096SFrank Li 
1838793fc096SFrank Li static void set_multicast_list(struct net_device *ndev)
1839793fc096SFrank Li {
1840793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1841793fc096SFrank Li 	struct netdev_hw_addr *ha;
1842793fc096SFrank Li 	unsigned int i, bit, data, crc, tmp;
1843793fc096SFrank Li 	unsigned char hash;
1844793fc096SFrank Li 
1845793fc096SFrank Li 	if (ndev->flags & IFF_PROMISC) {
1846793fc096SFrank Li 		tmp = readl(fep->hwp + FEC_R_CNTRL);
1847793fc096SFrank Li 		tmp |= 0x8;
1848793fc096SFrank Li 		writel(tmp, fep->hwp + FEC_R_CNTRL);
1849793fc096SFrank Li 		return;
1850793fc096SFrank Li 	}
1851793fc096SFrank Li 
1852793fc096SFrank Li 	tmp = readl(fep->hwp + FEC_R_CNTRL);
1853793fc096SFrank Li 	tmp &= ~0x8;
1854793fc096SFrank Li 	writel(tmp, fep->hwp + FEC_R_CNTRL);
1855793fc096SFrank Li 
1856793fc096SFrank Li 	if (ndev->flags & IFF_ALLMULTI) {
1857793fc096SFrank Li 		/* Catch all multicast addresses, so set the
1858793fc096SFrank Li 		 * filter to all 1's
1859793fc096SFrank Li 		 */
1860793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1861793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1862793fc096SFrank Li 
1863793fc096SFrank Li 		return;
1864793fc096SFrank Li 	}
1865793fc096SFrank Li 
1866793fc096SFrank Li 	/* Clear filter and add the addresses in hash register
1867793fc096SFrank Li 	 */
1868793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1869793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1870793fc096SFrank Li 
1871793fc096SFrank Li 	netdev_for_each_mc_addr(ha, ndev) {
1872793fc096SFrank Li 		/* calculate crc32 value of mac address */
1873793fc096SFrank Li 		crc = 0xffffffff;
1874793fc096SFrank Li 
1875793fc096SFrank Li 		for (i = 0; i < ndev->addr_len; i++) {
1876793fc096SFrank Li 			data = ha->addr[i];
1877793fc096SFrank Li 			for (bit = 0; bit < 8; bit++, data >>= 1) {
1878793fc096SFrank Li 				crc = (crc >> 1) ^
1879793fc096SFrank Li 				(((crc ^ data) & 1) ? CRC32_POLY : 0);
1880793fc096SFrank Li 			}
1881793fc096SFrank Li 		}
1882793fc096SFrank Li 
1883793fc096SFrank Li 		/* only upper 6 bits (HASH_BITS) are used
1884793fc096SFrank Li 		 * which point to specific bit in he hash registers
1885793fc096SFrank Li 		 */
1886793fc096SFrank Li 		hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1887793fc096SFrank Li 
1888793fc096SFrank Li 		if (hash > 31) {
1889793fc096SFrank Li 			tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1890793fc096SFrank Li 			tmp |= 1 << (hash - 32);
1891793fc096SFrank Li 			writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1892793fc096SFrank Li 		} else {
1893793fc096SFrank Li 			tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1894793fc096SFrank Li 			tmp |= 1 << hash;
1895793fc096SFrank Li 			writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1896793fc096SFrank Li 		}
1897793fc096SFrank Li 	}
1898793fc096SFrank Li }
1899793fc096SFrank Li 
1900793fc096SFrank Li /* Set a MAC change in hardware. */
1901793fc096SFrank Li static int
1902793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p)
1903793fc096SFrank Li {
1904793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1905793fc096SFrank Li 	struct sockaddr *addr = p;
1906793fc096SFrank Li 
1907793fc096SFrank Li 	if (!is_valid_ether_addr(addr->sa_data))
1908793fc096SFrank Li 		return -EADDRNOTAVAIL;
1909793fc096SFrank Li 
1910793fc096SFrank Li 	memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
1911793fc096SFrank Li 
1912793fc096SFrank Li 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1913793fc096SFrank Li 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
1914793fc096SFrank Li 		fep->hwp + FEC_ADDR_LOW);
1915793fc096SFrank Li 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
1916793fc096SFrank Li 		fep->hwp + FEC_ADDR_HIGH);
1917793fc096SFrank Li 	return 0;
1918793fc096SFrank Li }
1919793fc096SFrank Li 
1920793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
1921793fc096SFrank Li /**
1922793fc096SFrank Li  * fec_poll_controller - FEC Poll controller function
1923793fc096SFrank Li  * @dev: The FEC network adapter
1924793fc096SFrank Li  *
1925793fc096SFrank Li  * Polled functionality used by netconsole and others in non interrupt mode
1926793fc096SFrank Li  *
1927793fc096SFrank Li  */
1928793fc096SFrank Li static void fec_poll_controller(struct net_device *dev)
1929793fc096SFrank Li {
1930793fc096SFrank Li 	int i;
1931793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(dev);
1932793fc096SFrank Li 
1933793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
1934793fc096SFrank Li 		if (fep->irq[i] > 0) {
1935793fc096SFrank Li 			disable_irq(fep->irq[i]);
1936793fc096SFrank Li 			fec_enet_interrupt(fep->irq[i], dev);
1937793fc096SFrank Li 			enable_irq(fep->irq[i]);
1938793fc096SFrank Li 		}
1939793fc096SFrank Li 	}
1940793fc096SFrank Li }
1941793fc096SFrank Li #endif
1942793fc096SFrank Li 
19434c09eed9SJim Baxter static int fec_set_features(struct net_device *netdev,
19444c09eed9SJim Baxter 	netdev_features_t features)
19454c09eed9SJim Baxter {
19464c09eed9SJim Baxter 	struct fec_enet_private *fep = netdev_priv(netdev);
19474c09eed9SJim Baxter 	netdev_features_t changed = features ^ netdev->features;
19484c09eed9SJim Baxter 
19494c09eed9SJim Baxter 	netdev->features = features;
19504c09eed9SJim Baxter 
19514c09eed9SJim Baxter 	/* Receive checksum has been changed */
19524c09eed9SJim Baxter 	if (changed & NETIF_F_RXCSUM) {
19534c09eed9SJim Baxter 		if (features & NETIF_F_RXCSUM)
19544c09eed9SJim Baxter 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
19554c09eed9SJim Baxter 		else
19564c09eed9SJim Baxter 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
19574c09eed9SJim Baxter 
19584c09eed9SJim Baxter 		if (netif_running(netdev)) {
19594c09eed9SJim Baxter 			fec_stop(netdev);
19604c09eed9SJim Baxter 			fec_restart(netdev, fep->phy_dev->duplex);
19614c09eed9SJim Baxter 			netif_wake_queue(netdev);
19624c09eed9SJim Baxter 		} else {
19634c09eed9SJim Baxter 			fec_restart(netdev, fep->phy_dev->duplex);
19644c09eed9SJim Baxter 		}
19654c09eed9SJim Baxter 	}
19664c09eed9SJim Baxter 
19674c09eed9SJim Baxter 	return 0;
19684c09eed9SJim Baxter }
19694c09eed9SJim Baxter 
1970793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = {
1971793fc096SFrank Li 	.ndo_open		= fec_enet_open,
1972793fc096SFrank Li 	.ndo_stop		= fec_enet_close,
1973793fc096SFrank Li 	.ndo_start_xmit		= fec_enet_start_xmit,
1974793fc096SFrank Li 	.ndo_set_rx_mode	= set_multicast_list,
1975793fc096SFrank Li 	.ndo_change_mtu		= eth_change_mtu,
1976793fc096SFrank Li 	.ndo_validate_addr	= eth_validate_addr,
1977793fc096SFrank Li 	.ndo_tx_timeout		= fec_timeout,
1978793fc096SFrank Li 	.ndo_set_mac_address	= fec_set_mac_address,
1979793fc096SFrank Li 	.ndo_do_ioctl		= fec_enet_ioctl,
1980793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
1981793fc096SFrank Li 	.ndo_poll_controller	= fec_poll_controller,
1982793fc096SFrank Li #endif
19834c09eed9SJim Baxter 	.ndo_set_features	= fec_set_features,
1984793fc096SFrank Li };
1985793fc096SFrank Li 
1986793fc096SFrank Li  /*
1987793fc096SFrank Li   * XXX:  We need to clean up on failure exits here.
1988793fc096SFrank Li   *
1989793fc096SFrank Li   */
1990793fc096SFrank Li static int fec_enet_init(struct net_device *ndev)
1991793fc096SFrank Li {
1992793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
199348496255SShawn Guo 	const struct platform_device_id *id_entry =
199448496255SShawn Guo 				platform_get_device_id(fep->pdev);
1995793fc096SFrank Li 	struct bufdesc *cbd_base;
1996793fc096SFrank Li 
1997793fc096SFrank Li 	/* Allocate memory for buffer descriptors. */
1998793fc096SFrank Li 	cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1999793fc096SFrank Li 				      GFP_KERNEL);
2000793fc096SFrank Li 	if (!cbd_base)
2001793fc096SFrank Li 		return -ENOMEM;
2002793fc096SFrank Li 
2003a210576cSDavid S. Miller 	memset(cbd_base, 0, PAGE_SIZE);
2004793fc096SFrank Li 
2005793fc096SFrank Li 	fep->netdev = ndev;
2006793fc096SFrank Li 
2007793fc096SFrank Li 	/* Get the Ethernet address */
2008793fc096SFrank Li 	fec_get_mac(ndev);
2009793fc096SFrank Li 
201036e24e2eSDuan Fugang-B38611 	/* init the tx & rx ring size */
201136e24e2eSDuan Fugang-B38611 	fep->tx_ring_size = TX_RING_SIZE;
201236e24e2eSDuan Fugang-B38611 	fep->rx_ring_size = RX_RING_SIZE;
201336e24e2eSDuan Fugang-B38611 
2014793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
2015793fc096SFrank Li 	fep->rx_bd_base = cbd_base;
2016793fc096SFrank Li 	if (fep->bufdesc_ex)
2017793fc096SFrank Li 		fep->tx_bd_base = (struct bufdesc *)
201836e24e2eSDuan Fugang-B38611 			(((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
2019793fc096SFrank Li 	else
202036e24e2eSDuan Fugang-B38611 		fep->tx_bd_base = cbd_base + fep->rx_ring_size;
2021793fc096SFrank Li 
2022793fc096SFrank Li 	/* The FEC Ethernet specific entries in the device structure */
2023793fc096SFrank Li 	ndev->watchdog_timeo = TX_TIMEOUT;
2024793fc096SFrank Li 	ndev->netdev_ops = &fec_netdev_ops;
2025793fc096SFrank Li 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
2026793fc096SFrank Li 
2027793fc096SFrank Li 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
2028322555f5SFabio Estevam 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
2029793fc096SFrank Li 
2030cdffcf1bSJim Baxter 	if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN) {
2031cdffcf1bSJim Baxter 		/* enable hw VLAN support */
2032cdffcf1bSJim Baxter 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
2033cdffcf1bSJim Baxter 		ndev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2034cdffcf1bSJim Baxter 	}
2035cdffcf1bSJim Baxter 
203648496255SShawn Guo 	if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
20374c09eed9SJim Baxter 		/* enable hw accelerator */
20384c09eed9SJim Baxter 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
20394c09eed9SJim Baxter 				| NETIF_F_RXCSUM);
20404c09eed9SJim Baxter 		ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
20414c09eed9SJim Baxter 				| NETIF_F_RXCSUM);
20424c09eed9SJim Baxter 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
204348496255SShawn Guo 	}
20444c09eed9SJim Baxter 
2045793fc096SFrank Li 	fec_restart(ndev, 0);
2046793fc096SFrank Li 
2047793fc096SFrank Li 	return 0;
2048793fc096SFrank Li }
2049793fc096SFrank Li 
2050793fc096SFrank Li #ifdef CONFIG_OF
2051793fc096SFrank Li static void fec_reset_phy(struct platform_device *pdev)
2052793fc096SFrank Li {
2053793fc096SFrank Li 	int err, phy_reset;
2054793fc096SFrank Li 	int msec = 1;
2055793fc096SFrank Li 	struct device_node *np = pdev->dev.of_node;
2056793fc096SFrank Li 
2057793fc096SFrank Li 	if (!np)
2058793fc096SFrank Li 		return;
2059793fc096SFrank Li 
2060793fc096SFrank Li 	of_property_read_u32(np, "phy-reset-duration", &msec);
2061793fc096SFrank Li 	/* A sane reset duration should not be longer than 1s */
2062793fc096SFrank Li 	if (msec > 1000)
2063793fc096SFrank Li 		msec = 1;
2064793fc096SFrank Li 
2065793fc096SFrank Li 	phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
2066793fc096SFrank Li 	if (!gpio_is_valid(phy_reset))
2067793fc096SFrank Li 		return;
2068793fc096SFrank Li 
2069793fc096SFrank Li 	err = devm_gpio_request_one(&pdev->dev, phy_reset,
2070793fc096SFrank Li 				    GPIOF_OUT_INIT_LOW, "phy-reset");
2071793fc096SFrank Li 	if (err) {
2072793fc096SFrank Li 		dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
2073793fc096SFrank Li 		return;
2074793fc096SFrank Li 	}
2075793fc096SFrank Li 	msleep(msec);
2076793fc096SFrank Li 	gpio_set_value(phy_reset, 1);
2077793fc096SFrank Li }
2078793fc096SFrank Li #else /* CONFIG_OF */
2079793fc096SFrank Li static void fec_reset_phy(struct platform_device *pdev)
2080793fc096SFrank Li {
2081793fc096SFrank Li 	/*
2082793fc096SFrank Li 	 * In case of platform probe, the reset has been done
2083793fc096SFrank Li 	 * by machine code.
2084793fc096SFrank Li 	 */
2085793fc096SFrank Li }
2086793fc096SFrank Li #endif /* CONFIG_OF */
2087793fc096SFrank Li 
2088793fc096SFrank Li static int
2089793fc096SFrank Li fec_probe(struct platform_device *pdev)
2090793fc096SFrank Li {
2091793fc096SFrank Li 	struct fec_enet_private *fep;
2092793fc096SFrank Li 	struct fec_platform_data *pdata;
2093793fc096SFrank Li 	struct net_device *ndev;
2094793fc096SFrank Li 	int i, irq, ret = 0;
2095793fc096SFrank Li 	struct resource *r;
2096793fc096SFrank Li 	const struct of_device_id *of_id;
2097793fc096SFrank Li 	static int dev_id;
2098793fc096SFrank Li 
2099793fc096SFrank Li 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
2100793fc096SFrank Li 	if (of_id)
2101793fc096SFrank Li 		pdev->id_entry = of_id->data;
2102793fc096SFrank Li 
2103793fc096SFrank Li 	/* Init network device */
2104793fc096SFrank Li 	ndev = alloc_etherdev(sizeof(struct fec_enet_private));
2105793fc096SFrank Li 	if (!ndev)
2106793fc096SFrank Li 		return -ENOMEM;
2107793fc096SFrank Li 
2108793fc096SFrank Li 	SET_NETDEV_DEV(ndev, &pdev->dev);
2109793fc096SFrank Li 
2110793fc096SFrank Li 	/* setup board info structure */
2111793fc096SFrank Li 	fep = netdev_priv(ndev);
2112793fc096SFrank Li 
2113d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2114793fc096SFrank Li 	/* default enable pause frame auto negotiation */
2115793fc096SFrank Li 	if (pdev->id_entry &&
2116793fc096SFrank Li 	    (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2117793fc096SFrank Li 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
2118d1391930SGuenter Roeck #endif
2119793fc096SFrank Li 
2120399db75bSFabio Estevam 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2121941e173aSTushar Behera 	fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2122941e173aSTushar Behera 	if (IS_ERR(fep->hwp)) {
2123941e173aSTushar Behera 		ret = PTR_ERR(fep->hwp);
2124941e173aSTushar Behera 		goto failed_ioremap;
2125941e173aSTushar Behera 	}
2126941e173aSTushar Behera 
2127793fc096SFrank Li 	fep->pdev = pdev;
2128793fc096SFrank Li 	fep->dev_id = dev_id++;
2129793fc096SFrank Li 
2130793fc096SFrank Li 	fep->bufdesc_ex = 0;
2131793fc096SFrank Li 
2132793fc096SFrank Li 	platform_set_drvdata(pdev, ndev);
2133793fc096SFrank Li 
21346c5f7808SGuenter Roeck 	ret = of_get_phy_mode(pdev->dev.of_node);
2135793fc096SFrank Li 	if (ret < 0) {
213694660ba0SJingoo Han 		pdata = dev_get_platdata(&pdev->dev);
2137793fc096SFrank Li 		if (pdata)
2138793fc096SFrank Li 			fep->phy_interface = pdata->phy;
2139793fc096SFrank Li 		else
2140793fc096SFrank Li 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
2141793fc096SFrank Li 	} else {
2142793fc096SFrank Li 		fep->phy_interface = ret;
2143793fc096SFrank Li 	}
2144793fc096SFrank Li 
2145793fc096SFrank Li 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2146793fc096SFrank Li 	if (IS_ERR(fep->clk_ipg)) {
2147793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ipg);
2148793fc096SFrank Li 		goto failed_clk;
2149793fc096SFrank Li 	}
2150793fc096SFrank Li 
2151793fc096SFrank Li 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2152793fc096SFrank Li 	if (IS_ERR(fep->clk_ahb)) {
2153793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ahb);
2154793fc096SFrank Li 		goto failed_clk;
2155793fc096SFrank Li 	}
2156793fc096SFrank Li 
215738f56f33SLinus Torvalds 	/* enet_out is optional, depends on board */
215838f56f33SLinus Torvalds 	fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
215938f56f33SLinus Torvalds 	if (IS_ERR(fep->clk_enet_out))
216038f56f33SLinus Torvalds 		fep->clk_enet_out = NULL;
216138f56f33SLinus Torvalds 
2162793fc096SFrank Li 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
2163793fc096SFrank Li 	fep->bufdesc_ex =
2164793fc096SFrank Li 		pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
2165793fc096SFrank Li 	if (IS_ERR(fep->clk_ptp)) {
216638f56f33SLinus Torvalds 		fep->clk_ptp = NULL;
2167793fc096SFrank Li 		fep->bufdesc_ex = 0;
2168793fc096SFrank Li 	}
2169793fc096SFrank Li 
217013a097bdSFabio Estevam 	ret = clk_prepare_enable(fep->clk_ahb);
217113a097bdSFabio Estevam 	if (ret)
217213a097bdSFabio Estevam 		goto failed_clk;
217313a097bdSFabio Estevam 
217413a097bdSFabio Estevam 	ret = clk_prepare_enable(fep->clk_ipg);
217513a097bdSFabio Estevam 	if (ret)
217613a097bdSFabio Estevam 		goto failed_clk_ipg;
217713a097bdSFabio Estevam 
217813a097bdSFabio Estevam 	if (fep->clk_enet_out) {
217913a097bdSFabio Estevam 		ret = clk_prepare_enable(fep->clk_enet_out);
218013a097bdSFabio Estevam 		if (ret)
218113a097bdSFabio Estevam 			goto failed_clk_enet_out;
218213a097bdSFabio Estevam 	}
218313a097bdSFabio Estevam 
218413a097bdSFabio Estevam 	if (fep->clk_ptp) {
218513a097bdSFabio Estevam 		ret = clk_prepare_enable(fep->clk_ptp);
218613a097bdSFabio Estevam 		if (ret)
218713a097bdSFabio Estevam 			goto failed_clk_ptp;
218813a097bdSFabio Estevam 	}
2189793fc096SFrank Li 
2190f4e9f3d2SFabio Estevam 	fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2191f4e9f3d2SFabio Estevam 	if (!IS_ERR(fep->reg_phy)) {
2192f4e9f3d2SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
2193793fc096SFrank Li 		if (ret) {
2194793fc096SFrank Li 			dev_err(&pdev->dev,
2195793fc096SFrank Li 				"Failed to enable phy regulator: %d\n", ret);
2196793fc096SFrank Li 			goto failed_regulator;
2197793fc096SFrank Li 		}
2198f6a4d607SFabio Estevam 	} else {
2199f6a4d607SFabio Estevam 		fep->reg_phy = NULL;
2200793fc096SFrank Li 	}
2201793fc096SFrank Li 
2202793fc096SFrank Li 	fec_reset_phy(pdev);
2203793fc096SFrank Li 
2204793fc096SFrank Li 	if (fep->bufdesc_ex)
2205ca162a82SFabio Estevam 		fec_ptp_init(pdev);
2206793fc096SFrank Li 
2207793fc096SFrank Li 	ret = fec_enet_init(ndev);
2208793fc096SFrank Li 	if (ret)
2209793fc096SFrank Li 		goto failed_init;
2210793fc096SFrank Li 
2211793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
2212793fc096SFrank Li 		irq = platform_get_irq(pdev, i);
2213793fc096SFrank Li 		if (irq < 0) {
2214793fc096SFrank Li 			if (i)
2215793fc096SFrank Li 				break;
2216793fc096SFrank Li 			ret = irq;
2217793fc096SFrank Li 			goto failed_irq;
2218793fc096SFrank Li 		}
22190d9b2ab1SFabio Estevam 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
222044a272ddSMichael Opdenacker 				       0, pdev->name, ndev);
22210d9b2ab1SFabio Estevam 		if (ret)
2222793fc096SFrank Li 			goto failed_irq;
2223793fc096SFrank Li 	}
2224793fc096SFrank Li 
2225793fc096SFrank Li 	ret = fec_enet_mii_init(pdev);
2226793fc096SFrank Li 	if (ret)
2227793fc096SFrank Li 		goto failed_mii_init;
2228793fc096SFrank Li 
2229793fc096SFrank Li 	/* Carrier starts down, phylib will bring it up */
2230793fc096SFrank Li 	netif_carrier_off(ndev);
2231793fc096SFrank Li 
2232793fc096SFrank Li 	ret = register_netdev(ndev);
2233793fc096SFrank Li 	if (ret)
2234793fc096SFrank Li 		goto failed_register;
2235793fc096SFrank Li 
2236eb1d0640SFabio Estevam 	if (fep->bufdesc_ex && fep->ptp_clock)
2237eb1d0640SFabio Estevam 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2238eb1d0640SFabio Estevam 
223954309fa6SFrank Li 	INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
2240793fc096SFrank Li 	return 0;
2241793fc096SFrank Li 
2242793fc096SFrank Li failed_register:
2243793fc096SFrank Li 	fec_enet_mii_remove(fep);
2244793fc096SFrank Li failed_mii_init:
22457a2bbd8dSFabio Estevam failed_irq:
22467a2bbd8dSFabio Estevam failed_init:
2247f6a4d607SFabio Estevam 	if (fep->reg_phy)
2248f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
2249793fc096SFrank Li failed_regulator:
22509514fe7aSFabio Estevam 	if (fep->clk_ptp)
2251793fc096SFrank Li 		clk_disable_unprepare(fep->clk_ptp);
225213a097bdSFabio Estevam failed_clk_ptp:
2253d265cf48SFabio Estevam 	if (fep->clk_enet_out)
2254d265cf48SFabio Estevam 		clk_disable_unprepare(fep->clk_enet_out);
225513a097bdSFabio Estevam failed_clk_enet_out:
2256d265cf48SFabio Estevam 	clk_disable_unprepare(fep->clk_ipg);
225713a097bdSFabio Estevam failed_clk_ipg:
2258d265cf48SFabio Estevam 	clk_disable_unprepare(fep->clk_ahb);
2259793fc096SFrank Li failed_clk:
2260793fc096SFrank Li failed_ioremap:
2261793fc096SFrank Li 	free_netdev(ndev);
2262793fc096SFrank Li 
2263793fc096SFrank Li 	return ret;
2264793fc096SFrank Li }
2265793fc096SFrank Li 
2266793fc096SFrank Li static int
2267793fc096SFrank Li fec_drv_remove(struct platform_device *pdev)
2268793fc096SFrank Li {
2269793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
2270793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2271793fc096SFrank Li 
227254309fa6SFrank Li 	cancel_delayed_work_sync(&(fep->delay_work.delay_work));
2273793fc096SFrank Li 	unregister_netdev(ndev);
2274793fc096SFrank Li 	fec_enet_mii_remove(fep);
2275793fc096SFrank Li 	del_timer_sync(&fep->time_keep);
2276f6a4d607SFabio Estevam 	if (fep->reg_phy)
2277f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
22789514fe7aSFabio Estevam 	if (fep->clk_ptp)
2279793fc096SFrank Li 		clk_disable_unprepare(fep->clk_ptp);
2280793fc096SFrank Li 	if (fep->ptp_clock)
2281793fc096SFrank Li 		ptp_clock_unregister(fep->ptp_clock);
22829514fe7aSFabio Estevam 	if (fep->clk_enet_out)
228338f56f33SLinus Torvalds 		clk_disable_unprepare(fep->clk_enet_out);
2284793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ipg);
2285d265cf48SFabio Estevam 	clk_disable_unprepare(fep->clk_ahb);
2286793fc096SFrank Li 	free_netdev(ndev);
2287793fc096SFrank Li 
2288793fc096SFrank Li 	return 0;
2289793fc096SFrank Li }
2290793fc096SFrank Li 
2291bf7bfd7fSFabio Estevam #ifdef CONFIG_PM_SLEEP
2292793fc096SFrank Li static int
2293793fc096SFrank Li fec_suspend(struct device *dev)
2294793fc096SFrank Li {
2295793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
2296793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2297793fc096SFrank Li 
2298793fc096SFrank Li 	if (netif_running(ndev)) {
2299793fc096SFrank Li 		fec_stop(ndev);
2300793fc096SFrank Li 		netif_device_detach(ndev);
2301793fc096SFrank Li 	}
230279820e72SFabio Estevam 	if (fep->clk_ptp)
230379820e72SFabio Estevam 		clk_disable_unprepare(fep->clk_ptp);
23049514fe7aSFabio Estevam 	if (fep->clk_enet_out)
230538f56f33SLinus Torvalds 		clk_disable_unprepare(fep->clk_enet_out);
2306793fc096SFrank Li 	clk_disable_unprepare(fep->clk_ipg);
2307d265cf48SFabio Estevam 	clk_disable_unprepare(fep->clk_ahb);
2308793fc096SFrank Li 
2309238f7bc7SFabio Estevam 	if (fep->reg_phy)
2310238f7bc7SFabio Estevam 		regulator_disable(fep->reg_phy);
2311238f7bc7SFabio Estevam 
2312793fc096SFrank Li 	return 0;
2313793fc096SFrank Li }
2314793fc096SFrank Li 
2315793fc096SFrank Li static int
2316793fc096SFrank Li fec_resume(struct device *dev)
2317793fc096SFrank Li {
2318793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
2319793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2320238f7bc7SFabio Estevam 	int ret;
2321238f7bc7SFabio Estevam 
2322238f7bc7SFabio Estevam 	if (fep->reg_phy) {
2323238f7bc7SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
2324238f7bc7SFabio Estevam 		if (ret)
2325238f7bc7SFabio Estevam 			return ret;
2326238f7bc7SFabio Estevam 	}
2327793fc096SFrank Li 
232813a097bdSFabio Estevam 	ret = clk_prepare_enable(fep->clk_ahb);
232913a097bdSFabio Estevam 	if (ret)
233013a097bdSFabio Estevam 		goto failed_clk_ahb;
233113a097bdSFabio Estevam 
233213a097bdSFabio Estevam 	ret = clk_prepare_enable(fep->clk_ipg);
233313a097bdSFabio Estevam 	if (ret)
233413a097bdSFabio Estevam 		goto failed_clk_ipg;
233513a097bdSFabio Estevam 
233613a097bdSFabio Estevam 	if (fep->clk_enet_out) {
233713a097bdSFabio Estevam 		ret = clk_prepare_enable(fep->clk_enet_out);
233813a097bdSFabio Estevam 		if (ret)
233913a097bdSFabio Estevam 			goto failed_clk_enet_out;
234013a097bdSFabio Estevam 	}
234113a097bdSFabio Estevam 
234213a097bdSFabio Estevam 	if (fep->clk_ptp) {
234313a097bdSFabio Estevam 		ret = clk_prepare_enable(fep->clk_ptp);
234413a097bdSFabio Estevam 		if (ret)
234513a097bdSFabio Estevam 			goto failed_clk_ptp;
234613a097bdSFabio Estevam 	}
234713a097bdSFabio Estevam 
2348793fc096SFrank Li 	if (netif_running(ndev)) {
2349793fc096SFrank Li 		fec_restart(ndev, fep->full_duplex);
2350793fc096SFrank Li 		netif_device_attach(ndev);
2351793fc096SFrank Li 	}
2352793fc096SFrank Li 
2353793fc096SFrank Li 	return 0;
235413a097bdSFabio Estevam 
235513a097bdSFabio Estevam failed_clk_ptp:
235613a097bdSFabio Estevam 	if (fep->clk_enet_out)
235713a097bdSFabio Estevam 		clk_disable_unprepare(fep->clk_enet_out);
235813a097bdSFabio Estevam failed_clk_enet_out:
235913a097bdSFabio Estevam 	clk_disable_unprepare(fep->clk_ipg);
236013a097bdSFabio Estevam failed_clk_ipg:
236113a097bdSFabio Estevam 	clk_disable_unprepare(fep->clk_ahb);
236213a097bdSFabio Estevam failed_clk_ahb:
236313a097bdSFabio Estevam 	if (fep->reg_phy)
236413a097bdSFabio Estevam 		regulator_disable(fep->reg_phy);
236513a097bdSFabio Estevam 	return ret;
2366793fc096SFrank Li }
2367bf7bfd7fSFabio Estevam #endif /* CONFIG_PM_SLEEP */
2368793fc096SFrank Li 
2369bf7bfd7fSFabio Estevam static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
2370793fc096SFrank Li 
2371793fc096SFrank Li static struct platform_driver fec_driver = {
2372793fc096SFrank Li 	.driver	= {
2373793fc096SFrank Li 		.name	= DRIVER_NAME,
2374793fc096SFrank Li 		.owner	= THIS_MODULE,
2375793fc096SFrank Li 		.pm	= &fec_pm_ops,
2376793fc096SFrank Li 		.of_match_table = fec_dt_ids,
2377793fc096SFrank Li 	},
2378793fc096SFrank Li 	.id_table = fec_devtype,
2379793fc096SFrank Li 	.probe	= fec_probe,
2380793fc096SFrank Li 	.remove	= fec_drv_remove,
2381793fc096SFrank Li };
2382793fc096SFrank Li 
2383793fc096SFrank Li module_platform_driver(fec_driver);
2384793fc096SFrank Li 
2385f8c0aca9SFabio Estevam MODULE_ALIAS("platform:"DRIVER_NAME);
2386793fc096SFrank Li MODULE_LICENSE("GPL");
2387