xref: /linux/drivers/net/ethernet/freescale/fec_main.c (revision 29380905565655bb797bf670a173bddb8e641da6)
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>
278fff755eSAndrew Lunn #include <linux/pm_runtime.h>
28793fc096SFrank Li #include <linux/ptrace.h>
29793fc096SFrank Li #include <linux/errno.h>
30793fc096SFrank Li #include <linux/ioport.h>
31793fc096SFrank Li #include <linux/slab.h>
32793fc096SFrank Li #include <linux/interrupt.h>
33793fc096SFrank Li #include <linux/delay.h>
34793fc096SFrank Li #include <linux/netdevice.h>
35793fc096SFrank Li #include <linux/etherdevice.h>
36793fc096SFrank Li #include <linux/skbuff.h>
374c09eed9SJim Baxter #include <linux/in.h>
384c09eed9SJim Baxter #include <linux/ip.h>
394c09eed9SJim Baxter #include <net/ip.h>
4079f33912SNimrod Andy #include <net/tso.h>
414c09eed9SJim Baxter #include <linux/tcp.h>
424c09eed9SJim Baxter #include <linux/udp.h>
434c09eed9SJim Baxter #include <linux/icmp.h>
44793fc096SFrank Li #include <linux/spinlock.h>
45793fc096SFrank Li #include <linux/workqueue.h>
46793fc096SFrank Li #include <linux/bitops.h>
47793fc096SFrank Li #include <linux/io.h>
48793fc096SFrank Li #include <linux/irq.h>
49793fc096SFrank Li #include <linux/clk.h>
50793fc096SFrank Li #include <linux/platform_device.h>
517f854420SAndrew Lunn #include <linux/mdio.h>
52793fc096SFrank Li #include <linux/phy.h>
53793fc096SFrank Li #include <linux/fec.h>
54793fc096SFrank Li #include <linux/of.h>
55793fc096SFrank Li #include <linux/of_device.h>
56793fc096SFrank Li #include <linux/of_gpio.h>
57407066f8SUwe Kleine-König #include <linux/of_mdio.h>
58793fc096SFrank Li #include <linux/of_net.h>
59793fc096SFrank Li #include <linux/regulator/consumer.h>
60cdffcf1bSJim Baxter #include <linux/if_vlan.h>
61a68ab98eSFabio Estevam #include <linux/pinctrl/consumer.h>
62c259c132SFrank Li #include <linux/prefetch.h>
63*29380905SLucas Stach #include <soc/imx/cpuidle.h>
64793fc096SFrank Li 
65793fc096SFrank Li #include <asm/cacheflush.h>
66793fc096SFrank Li 
67793fc096SFrank Li #include "fec.h"
68793fc096SFrank Li 
69772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev);
70d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev);
71772e42b0SChristoph Müllner 
72793fc096SFrank Li #define DRIVER_NAME	"fec"
73793fc096SFrank Li 
744d494cdcSFugang Duan #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
754d494cdcSFugang Duan 
76793fc096SFrank Li /* Pause frame feild and FIFO threshold */
77793fc096SFrank Li #define FEC_ENET_FCE	(1 << 5)
78793fc096SFrank Li #define FEC_ENET_RSEM_V	0x84
79793fc096SFrank Li #define FEC_ENET_RSFL_V	16
80793fc096SFrank Li #define FEC_ENET_RAEM_V	0x8
81793fc096SFrank Li #define FEC_ENET_RAFL_V	0x8
82793fc096SFrank Li #define FEC_ENET_OPD_V	0xFFF0
838fff755eSAndrew Lunn #define FEC_MDIO_PM_TIMEOUT  100 /* ms */
84793fc096SFrank Li 
85793fc096SFrank Li static struct platform_device_id fec_devtype[] = {
86793fc096SFrank Li 	{
87793fc096SFrank Li 		/* keep it for coldfire */
88793fc096SFrank Li 		.name = DRIVER_NAME,
89793fc096SFrank Li 		.driver_data = 0,
90793fc096SFrank Li 	}, {
91793fc096SFrank Li 		.name = "imx25-fec",
9218803495SGreg Ungerer 		.driver_data = FEC_QUIRK_USE_GASKET | FEC_QUIRK_HAS_RACC,
93793fc096SFrank Li 	}, {
94793fc096SFrank Li 		.name = "imx27-fec",
9518803495SGreg Ungerer 		.driver_data = FEC_QUIRK_HAS_RACC,
96793fc096SFrank Li 	}, {
97793fc096SFrank Li 		.name = "imx28-fec",
983d125f9cSStefan Agner 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
9918803495SGreg Ungerer 				FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC,
100793fc096SFrank Li 	}, {
101793fc096SFrank Li 		.name = "imx6q-fec",
102793fc096SFrank Li 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
103cdffcf1bSJim Baxter 				FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
10418803495SGreg Ungerer 				FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
10518803495SGreg Ungerer 				FEC_QUIRK_HAS_RACC,
106793fc096SFrank Li 	}, {
10736803542SShawn Guo 		.name = "mvf600-fec",
10818803495SGreg Ungerer 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
109ca7c4a45SJingchang Lu 	}, {
11095a77470SFugang Duan 		.name = "imx6sx-fec",
11195a77470SFugang Duan 		.driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
11295a77470SFugang Duan 				FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
113f88c7edeSNimrod Andy 				FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
11418803495SGreg Ungerer 				FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
11518803495SGreg Ungerer 				FEC_QUIRK_HAS_RACC,
11695a77470SFugang Duan 	}, {
117793fc096SFrank Li 		/* sentinel */
118793fc096SFrank Li 	}
119793fc096SFrank Li };
120793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype);
121793fc096SFrank Li 
122793fc096SFrank Li enum imx_fec_type {
123793fc096SFrank Li 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
124793fc096SFrank Li 	IMX27_FEC,	/* runs on i.mx27/35/51 */
125793fc096SFrank Li 	IMX28_FEC,
126793fc096SFrank Li 	IMX6Q_FEC,
12736803542SShawn Guo 	MVF600_FEC,
128ba593e00SFugang Duan 	IMX6SX_FEC,
129793fc096SFrank Li };
130793fc096SFrank Li 
131793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = {
132793fc096SFrank Li 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
133793fc096SFrank Li 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
134793fc096SFrank Li 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
135793fc096SFrank Li 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
13636803542SShawn Guo 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
137ba593e00SFugang Duan 	{ .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
138793fc096SFrank Li 	{ /* sentinel */ }
139793fc096SFrank Li };
140793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids);
141793fc096SFrank Li 
142793fc096SFrank Li static unsigned char macaddr[ETH_ALEN];
143793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0);
144793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
145793fc096SFrank Li 
146793fc096SFrank Li #if defined(CONFIG_M5272)
147793fc096SFrank Li /*
148793fc096SFrank Li  * Some hardware gets it MAC address out of local flash memory.
149793fc096SFrank Li  * if this is non-zero then assume it is the address to get MAC from.
150793fc096SFrank Li  */
151793fc096SFrank Li #if defined(CONFIG_NETtel)
152793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006006
153793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
154793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006000
155793fc096SFrank Li #elif defined(CONFIG_CANCam)
156793fc096SFrank Li #define	FEC_FLASHMAC	0xf0020000
157793fc096SFrank Li #elif defined (CONFIG_M5272C3)
158793fc096SFrank Li #define	FEC_FLASHMAC	(0xffe04000 + 4)
159793fc096SFrank Li #elif defined(CONFIG_MOD5272)
160793fc096SFrank Li #define FEC_FLASHMAC	0xffc0406b
161793fc096SFrank Li #else
162793fc096SFrank Li #define	FEC_FLASHMAC	0
163793fc096SFrank Li #endif
164793fc096SFrank Li #endif /* CONFIG_M5272 */
165793fc096SFrank Li 
166cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
167793fc096SFrank Li  */
168cdffcf1bSJim Baxter #define PKT_MAXBUF_SIZE		1522
169793fc096SFrank Li #define PKT_MINBUF_SIZE		64
170cdffcf1bSJim Baxter #define PKT_MAXBLR_SIZE		1536
171793fc096SFrank Li 
1724c09eed9SJim Baxter /* FEC receive acceleration */
1734c09eed9SJim Baxter #define FEC_RACC_IPDIS		(1 << 1)
1744c09eed9SJim Baxter #define FEC_RACC_PRODIS		(1 << 2)
1754c09eed9SJim Baxter #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
1764c09eed9SJim Baxter 
177793fc096SFrank Li /*
178793fc096SFrank Li  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
179793fc096SFrank Li  * size bits. Other FEC hardware does not, so we need to take that into
180793fc096SFrank Li  * account when setting it.
181793fc096SFrank Li  */
182793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
183793fc096SFrank Li     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
184793fc096SFrank Li #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
185793fc096SFrank Li #else
186793fc096SFrank Li #define	OPT_FRAME_SIZE	0
187793fc096SFrank Li #endif
188793fc096SFrank Li 
189793fc096SFrank Li /* FEC MII MMFR bits definition */
190793fc096SFrank Li #define FEC_MMFR_ST		(1 << 30)
191793fc096SFrank Li #define FEC_MMFR_OP_READ	(2 << 28)
192793fc096SFrank Li #define FEC_MMFR_OP_WRITE	(1 << 28)
193793fc096SFrank Li #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
194793fc096SFrank Li #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
195793fc096SFrank Li #define FEC_MMFR_TA		(2 << 16)
196793fc096SFrank Li #define FEC_MMFR_DATA(v)	(v & 0xffff)
197de40ed31SNimrod Andy /* FEC ECR bits definition */
198de40ed31SNimrod Andy #define FEC_ECR_MAGICEN		(1 << 2)
199de40ed31SNimrod Andy #define FEC_ECR_SLEEP		(1 << 3)
200793fc096SFrank Li 
201793fc096SFrank Li #define FEC_MII_TIMEOUT		30000 /* us */
202793fc096SFrank Li 
203793fc096SFrank Li /* Transmitter timeout */
204793fc096SFrank Li #define TX_TIMEOUT (2 * HZ)
205793fc096SFrank Li 
206793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG	0x1
207793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE	0x2
208de40ed31SNimrod Andy #define FEC_WOL_HAS_MAGIC_PACKET	(0x1 << 0)
209de40ed31SNimrod Andy #define FEC_WOL_FLAG_ENABLE		(0x1 << 1)
210de40ed31SNimrod Andy #define FEC_WOL_FLAG_SLEEP_ON		(0x1 << 2)
211793fc096SFrank Li 
2121b7bde6dSNimrod Andy #define COPYBREAK_DEFAULT	256
2131b7bde6dSNimrod Andy 
21479f33912SNimrod Andy #define TSO_HEADER_SIZE		128
21579f33912SNimrod Andy /* Max number of allowed TCP segments for software TSO */
21679f33912SNimrod Andy #define FEC_MAX_TSO_SEGS	100
21779f33912SNimrod Andy #define FEC_MAX_SKB_DESCS	(FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
21879f33912SNimrod Andy 
21979f33912SNimrod Andy #define IS_TSO_HEADER(txq, addr) \
22079f33912SNimrod Andy 	((addr >= txq->tso_hdrs_dma) && \
2217355f276STroy Kisky 	(addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
22279f33912SNimrod Andy 
223793fc096SFrank Li static int mii_cnt;
224793fc096SFrank Li 
2257355f276STroy Kisky static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
2267355f276STroy Kisky 					     struct bufdesc_prop *bd)
227793fc096SFrank Li {
2287355f276STroy Kisky 	return (bdp >= bd->last) ? bd->base
2297355f276STroy Kisky 			: (struct bufdesc *)(((unsigned)bdp) + bd->dsize);
230793fc096SFrank Li }
231793fc096SFrank Li 
2327355f276STroy Kisky static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
2337355f276STroy Kisky 					     struct bufdesc_prop *bd)
23436e24e2eSDuan Fugang-B38611 {
2357355f276STroy Kisky 	return (bdp <= bd->base) ? bd->last
2367355f276STroy Kisky 			: (struct bufdesc *)(((unsigned)bdp) - bd->dsize);
23736e24e2eSDuan Fugang-B38611 }
23836e24e2eSDuan Fugang-B38611 
2397355f276STroy Kisky static int fec_enet_get_bd_index(struct bufdesc *bdp,
2407355f276STroy Kisky 				 struct bufdesc_prop *bd)
24161a4427bSNimrod Andy {
2427355f276STroy Kisky 	return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
24361a4427bSNimrod Andy }
24461a4427bSNimrod Andy 
2457355f276STroy Kisky static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
2466e909283SNimrod Andy {
2476e909283SNimrod Andy 	int entries;
2486e909283SNimrod Andy 
2497355f276STroy Kisky 	entries = (((const char *)txq->dirty_tx -
2507355f276STroy Kisky 			(const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
2516e909283SNimrod Andy 
2527355f276STroy Kisky 	return entries >= 0 ? entries : entries + txq->bd.ring_size;
2536e909283SNimrod Andy }
2546e909283SNimrod Andy 
255c20e599bSLothar Waßmann static void swap_buffer(void *bufaddr, int len)
256793fc096SFrank Li {
257793fc096SFrank Li 	int i;
258793fc096SFrank Li 	unsigned int *buf = bufaddr;
259793fc096SFrank Li 
2607b487d07SLothar Waßmann 	for (i = 0; i < len; i += 4, buf++)
261e453789aSLothar Waßmann 		swab32s(buf);
262793fc096SFrank Li }
263793fc096SFrank Li 
2641310b544SLothar Waßmann static void swap_buffer2(void *dst_buf, void *src_buf, int len)
2651310b544SLothar Waßmann {
2661310b544SLothar Waßmann 	int i;
2671310b544SLothar Waßmann 	unsigned int *src = src_buf;
2681310b544SLothar Waßmann 	unsigned int *dst = dst_buf;
2691310b544SLothar Waßmann 
2701310b544SLothar Waßmann 	for (i = 0; i < len; i += 4, src++, dst++)
2711310b544SLothar Waßmann 		*dst = swab32p(src);
2721310b544SLothar Waßmann }
2731310b544SLothar Waßmann 
274344756f6SRussell King static void fec_dump(struct net_device *ndev)
275344756f6SRussell King {
276344756f6SRussell King 	struct fec_enet_private *fep = netdev_priv(ndev);
2774d494cdcSFugang Duan 	struct bufdesc *bdp;
2784d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
2794d494cdcSFugang Duan 	int index = 0;
280344756f6SRussell King 
281344756f6SRussell King 	netdev_info(ndev, "TX ring dump\n");
282344756f6SRussell King 	pr_info("Nr     SC     addr       len  SKB\n");
283344756f6SRussell King 
2844d494cdcSFugang Duan 	txq = fep->tx_queue[0];
2857355f276STroy Kisky 	bdp = txq->bd.base;
2864d494cdcSFugang Duan 
287344756f6SRussell King 	do {
2885cfa3039SJohannes Berg 		pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
289344756f6SRussell King 			index,
2907355f276STroy Kisky 			bdp == txq->bd.cur ? 'S' : ' ',
2914d494cdcSFugang Duan 			bdp == txq->dirty_tx ? 'H' : ' ',
2925cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_sc),
2935cfa3039SJohannes Berg 			fec32_to_cpu(bdp->cbd_bufaddr),
2945cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_datlen),
2954d494cdcSFugang Duan 			txq->tx_skbuff[index]);
2967355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
297344756f6SRussell King 		index++;
2987355f276STroy Kisky 	} while (bdp != txq->bd.base);
299344756f6SRussell King }
300344756f6SRussell King 
30162a02c98SFugang Duan static inline bool is_ipv4_pkt(struct sk_buff *skb)
30262a02c98SFugang Duan {
30362a02c98SFugang Duan 	return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
30462a02c98SFugang Duan }
30562a02c98SFugang Duan 
3064c09eed9SJim Baxter static int
3074c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
3084c09eed9SJim Baxter {
3094c09eed9SJim Baxter 	/* Only run for packets requiring a checksum. */
3104c09eed9SJim Baxter 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3114c09eed9SJim Baxter 		return 0;
3124c09eed9SJim Baxter 
3134c09eed9SJim Baxter 	if (unlikely(skb_cow_head(skb, 0)))
3144c09eed9SJim Baxter 		return -1;
3154c09eed9SJim Baxter 
31662a02c98SFugang Duan 	if (is_ipv4_pkt(skb))
31796c50caaSNimrod Andy 		ip_hdr(skb)->check = 0;
3184c09eed9SJim Baxter 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
3194c09eed9SJim Baxter 
3204c09eed9SJim Baxter 	return 0;
3214c09eed9SJim Baxter }
3224c09eed9SJim Baxter 
323c4bc44c6SKevin Hao static struct bufdesc *
3244d494cdcSFugang Duan fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
3254d494cdcSFugang Duan 			     struct sk_buff *skb,
3264d494cdcSFugang Duan 			     struct net_device *ndev)
3276e909283SNimrod Andy {
3286e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
3297355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
3306e909283SNimrod Andy 	struct bufdesc_ex *ebdp;
3316e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
3326e909283SNimrod Andy 	int frag, frag_len;
3336e909283SNimrod Andy 	unsigned short status;
3346e909283SNimrod Andy 	unsigned int estatus = 0;
3356e909283SNimrod Andy 	skb_frag_t *this_frag;
3366e909283SNimrod Andy 	unsigned int index;
3376e909283SNimrod Andy 	void *bufaddr;
338d6bf3143SRussell King 	dma_addr_t addr;
3396e909283SNimrod Andy 	int i;
3406e909283SNimrod Andy 
3416e909283SNimrod Andy 	for (frag = 0; frag < nr_frags; frag++) {
3426e909283SNimrod Andy 		this_frag = &skb_shinfo(skb)->frags[frag];
3437355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3446e909283SNimrod Andy 		ebdp = (struct bufdesc_ex *)bdp;
3456e909283SNimrod Andy 
3465cfa3039SJohannes Berg 		status = fec16_to_cpu(bdp->cbd_sc);
3476e909283SNimrod Andy 		status &= ~BD_ENET_TX_STATS;
3486e909283SNimrod Andy 		status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
3496e909283SNimrod Andy 		frag_len = skb_shinfo(skb)->frags[frag].size;
3506e909283SNimrod Andy 
3516e909283SNimrod Andy 		/* Handle the last BD specially */
3526e909283SNimrod Andy 		if (frag == nr_frags - 1) {
3536e909283SNimrod Andy 			status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
3546e909283SNimrod Andy 			if (fep->bufdesc_ex) {
3556e909283SNimrod Andy 				estatus |= BD_ENET_TX_INT;
3566e909283SNimrod Andy 				if (unlikely(skb_shinfo(skb)->tx_flags &
3576e909283SNimrod Andy 					SKBTX_HW_TSTAMP && fep->hwts_tx_en))
3586e909283SNimrod Andy 					estatus |= BD_ENET_TX_TS;
3596e909283SNimrod Andy 			}
3606e909283SNimrod Andy 		}
3616e909283SNimrod Andy 
3626e909283SNimrod Andy 		if (fep->bufdesc_ex) {
3636b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_HAS_AVB)
36453bb20d1STroy Kisky 				estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
3656e909283SNimrod Andy 			if (skb->ip_summed == CHECKSUM_PARTIAL)
3666e909283SNimrod Andy 				estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
3676e909283SNimrod Andy 			ebdp->cbd_bdu = 0;
3685cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(estatus);
3696e909283SNimrod Andy 		}
3706e909283SNimrod Andy 
3716e909283SNimrod Andy 		bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
3726e909283SNimrod Andy 
3737355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
37441ef84ceSFugang Duan 		if (((unsigned long) bufaddr) & fep->tx_align ||
3756b7e4008SLothar Waßmann 			fep->quirks & FEC_QUIRK_SWAP_FRAME) {
3764d494cdcSFugang Duan 			memcpy(txq->tx_bounce[index], bufaddr, frag_len);
3774d494cdcSFugang Duan 			bufaddr = txq->tx_bounce[index];
3786e909283SNimrod Andy 
3796b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
3806e909283SNimrod Andy 				swap_buffer(bufaddr, frag_len);
3816e909283SNimrod Andy 		}
3826e909283SNimrod Andy 
383d6bf3143SRussell King 		addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
384d6bf3143SRussell King 				      DMA_TO_DEVICE);
385d6bf3143SRussell King 		if (dma_mapping_error(&fep->pdev->dev, addr)) {
3866e909283SNimrod Andy 			if (net_ratelimit())
3876e909283SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
3886e909283SNimrod Andy 			goto dma_mapping_error;
3896e909283SNimrod Andy 		}
3906e909283SNimrod Andy 
3915cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(addr);
3925cfa3039SJohannes Berg 		bdp->cbd_datlen = cpu_to_fec16(frag_len);
393be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
394be293467STroy Kisky 		 * performed before transferring ownership.
395be293467STroy Kisky 		 */
396be293467STroy Kisky 		wmb();
3975cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(status);
3986e909283SNimrod Andy 	}
3996e909283SNimrod Andy 
400c4bc44c6SKevin Hao 	return bdp;
4016e909283SNimrod Andy dma_mapping_error:
4027355f276STroy Kisky 	bdp = txq->bd.cur;
4036e909283SNimrod Andy 	for (i = 0; i < frag; i++) {
4047355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
4055cfa3039SJohannes Berg 		dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
4065cfa3039SJohannes Berg 				 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
4076e909283SNimrod Andy 	}
408c4bc44c6SKevin Hao 	return ERR_PTR(-ENOMEM);
4096e909283SNimrod Andy }
4106e909283SNimrod Andy 
4114d494cdcSFugang Duan static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
4124d494cdcSFugang Duan 				   struct sk_buff *skb, struct net_device *ndev)
4136e909283SNimrod Andy {
4146e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
4156e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
4166e909283SNimrod Andy 	struct bufdesc *bdp, *last_bdp;
4176e909283SNimrod Andy 	void *bufaddr;
418d6bf3143SRussell King 	dma_addr_t addr;
4196e909283SNimrod Andy 	unsigned short status;
4206e909283SNimrod Andy 	unsigned short buflen;
4216e909283SNimrod Andy 	unsigned int estatus = 0;
4226e909283SNimrod Andy 	unsigned int index;
42379f33912SNimrod Andy 	int entries_free;
4246e909283SNimrod Andy 
4257355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
42679f33912SNimrod Andy 	if (entries_free < MAX_SKB_FRAGS + 1) {
42779f33912SNimrod Andy 		dev_kfree_skb_any(skb);
42879f33912SNimrod Andy 		if (net_ratelimit())
42979f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for SG!\n");
43079f33912SNimrod Andy 		return NETDEV_TX_OK;
43179f33912SNimrod Andy 	}
43279f33912SNimrod Andy 
4336e909283SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
4346e909283SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
4356e909283SNimrod Andy 		dev_kfree_skb_any(skb);
4366e909283SNimrod Andy 		return NETDEV_TX_OK;
4376e909283SNimrod Andy 	}
4386e909283SNimrod Andy 
4396e909283SNimrod Andy 	/* Fill in a Tx ring entry */
4407355f276STroy Kisky 	bdp = txq->bd.cur;
441c4bc44c6SKevin Hao 	last_bdp = bdp;
4425cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
4436e909283SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
4446e909283SNimrod Andy 
4456e909283SNimrod Andy 	/* Set buffer length and buffer pointer */
4466e909283SNimrod Andy 	bufaddr = skb->data;
4476e909283SNimrod Andy 	buflen = skb_headlen(skb);
4486e909283SNimrod Andy 
4497355f276STroy Kisky 	index = fec_enet_get_bd_index(bdp, &txq->bd);
45041ef84ceSFugang Duan 	if (((unsigned long) bufaddr) & fep->tx_align ||
4516b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
4524d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, buflen);
4534d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
4546e909283SNimrod Andy 
4556b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
4566e909283SNimrod Andy 			swap_buffer(bufaddr, buflen);
4576e909283SNimrod Andy 	}
4586e909283SNimrod Andy 
459d6bf3143SRussell King 	/* Push the data cache so the CPM does not get stale memory data. */
460d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
461d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
4626e909283SNimrod Andy 		dev_kfree_skb_any(skb);
4636e909283SNimrod Andy 		if (net_ratelimit())
4646e909283SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
4656e909283SNimrod Andy 		return NETDEV_TX_OK;
4666e909283SNimrod Andy 	}
4676e909283SNimrod Andy 
4686e909283SNimrod Andy 	if (nr_frags) {
469c4bc44c6SKevin Hao 		last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
470fc75ba51STroy Kisky 		if (IS_ERR(last_bdp)) {
471fc75ba51STroy Kisky 			dma_unmap_single(&fep->pdev->dev, addr,
472fc75ba51STroy Kisky 					 buflen, DMA_TO_DEVICE);
473fc75ba51STroy Kisky 			dev_kfree_skb_any(skb);
474c4bc44c6SKevin Hao 			return NETDEV_TX_OK;
475fc75ba51STroy Kisky 		}
4766e909283SNimrod Andy 	} else {
4776e909283SNimrod Andy 		status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
4786e909283SNimrod Andy 		if (fep->bufdesc_ex) {
4796e909283SNimrod Andy 			estatus = BD_ENET_TX_INT;
4806e909283SNimrod Andy 			if (unlikely(skb_shinfo(skb)->tx_flags &
4816e909283SNimrod Andy 				SKBTX_HW_TSTAMP && fep->hwts_tx_en))
4826e909283SNimrod Andy 				estatus |= BD_ENET_TX_TS;
4836e909283SNimrod Andy 		}
4846e909283SNimrod Andy 	}
485fc75ba51STroy Kisky 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
486fc75ba51STroy Kisky 	bdp->cbd_datlen = cpu_to_fec16(buflen);
4876e909283SNimrod Andy 
4886e909283SNimrod Andy 	if (fep->bufdesc_ex) {
4896e909283SNimrod Andy 
4906e909283SNimrod Andy 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
4916e909283SNimrod Andy 
4926e909283SNimrod Andy 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
4936e909283SNimrod Andy 			fep->hwts_tx_en))
4946e909283SNimrod Andy 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4956e909283SNimrod Andy 
4966b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
49753bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
498befe8213SNimrod Andy 
4996e909283SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
5006e909283SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
5016e909283SNimrod Andy 
5026e909283SNimrod Andy 		ebdp->cbd_bdu = 0;
5035cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
5046e909283SNimrod Andy 	}
5056e909283SNimrod Andy 
5067355f276STroy Kisky 	index = fec_enet_get_bd_index(last_bdp, &txq->bd);
5076e909283SNimrod Andy 	/* Save skb pointer */
5084d494cdcSFugang Duan 	txq->tx_skbuff[index] = skb;
5096e909283SNimrod Andy 
510be293467STroy Kisky 	/* Make sure the updates to rest of the descriptor are performed before
511be293467STroy Kisky 	 * transferring ownership.
512be293467STroy Kisky 	 */
513be293467STroy Kisky 	wmb();
5146e909283SNimrod Andy 
5156e909283SNimrod Andy 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
5166e909283SNimrod Andy 	 * it's the last BD of the frame, and to put the CRC on the end.
5176e909283SNimrod Andy 	 */
5186e909283SNimrod Andy 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
5195cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
5206e909283SNimrod Andy 
521793fc096SFrank Li 	/* If this was the last BD in the ring, start at the beginning again. */
5227355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
523793fc096SFrank Li 
5247a2a8451SEric Dumazet 	skb_tx_timestamp(skb);
5257a2a8451SEric Dumazet 
526c4bc44c6SKevin Hao 	/* Make sure the update to bdp and tx_skbuff are performed before
5277355f276STroy Kisky 	 * txq->bd.cur.
528c4bc44c6SKevin Hao 	 */
529c4bc44c6SKevin Hao 	wmb();
5307355f276STroy Kisky 	txq->bd.cur = bdp;
531793fc096SFrank Li 
532793fc096SFrank Li 	/* Trigger transmission start */
53353bb20d1STroy Kisky 	writel(0, txq->bd.reg_desc_active);
534793fc096SFrank Li 
5356e909283SNimrod Andy 	return 0;
536793fc096SFrank Li }
537793fc096SFrank Li 
53879f33912SNimrod Andy static int
5394d494cdcSFugang Duan fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
5404d494cdcSFugang Duan 			  struct net_device *ndev,
54179f33912SNimrod Andy 			  struct bufdesc *bdp, int index, char *data,
54279f33912SNimrod Andy 			  int size, bool last_tcp, bool is_last)
54379f33912SNimrod Andy {
54479f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
54561cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
54679f33912SNimrod Andy 	unsigned short status;
54779f33912SNimrod Andy 	unsigned int estatus = 0;
548d6bf3143SRussell King 	dma_addr_t addr;
54979f33912SNimrod Andy 
5505cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
55179f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
55279f33912SNimrod Andy 
55379f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
55479f33912SNimrod Andy 
55541ef84ceSFugang Duan 	if (((unsigned long) data) & fep->tx_align ||
5566b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
5574d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], data, size);
5584d494cdcSFugang Duan 		data = txq->tx_bounce[index];
55979f33912SNimrod Andy 
5606b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
56179f33912SNimrod Andy 			swap_buffer(data, size);
56279f33912SNimrod Andy 	}
56379f33912SNimrod Andy 
564d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
565d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
56679f33912SNimrod Andy 		dev_kfree_skb_any(skb);
56779f33912SNimrod Andy 		if (net_ratelimit())
56879f33912SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
56979f33912SNimrod Andy 		return NETDEV_TX_BUSY;
57079f33912SNimrod Andy 	}
57179f33912SNimrod Andy 
5725cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(size);
5735cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
574d6bf3143SRussell King 
57579f33912SNimrod Andy 	if (fep->bufdesc_ex) {
5766b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
57753bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
57879f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
57979f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
58079f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
5815cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
58279f33912SNimrod Andy 	}
58379f33912SNimrod Andy 
58479f33912SNimrod Andy 	/* Handle the last BD specially */
58579f33912SNimrod Andy 	if (last_tcp)
58679f33912SNimrod Andy 		status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
58779f33912SNimrod Andy 	if (is_last) {
58879f33912SNimrod Andy 		status |= BD_ENET_TX_INTR;
58979f33912SNimrod Andy 		if (fep->bufdesc_ex)
5905cfa3039SJohannes Berg 			ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
59179f33912SNimrod Andy 	}
59279f33912SNimrod Andy 
5935cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
59479f33912SNimrod Andy 
59579f33912SNimrod Andy 	return 0;
59679f33912SNimrod Andy }
59779f33912SNimrod Andy 
59879f33912SNimrod Andy static int
5994d494cdcSFugang Duan fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
6004d494cdcSFugang Duan 			 struct sk_buff *skb, struct net_device *ndev,
60179f33912SNimrod Andy 			 struct bufdesc *bdp, int index)
60279f33912SNimrod Andy {
60379f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
60479f33912SNimrod Andy 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
60561cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
60679f33912SNimrod Andy 	void *bufaddr;
60779f33912SNimrod Andy 	unsigned long dmabuf;
60879f33912SNimrod Andy 	unsigned short status;
60979f33912SNimrod Andy 	unsigned int estatus = 0;
61079f33912SNimrod Andy 
6115cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
61279f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
61379f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
61479f33912SNimrod Andy 
6154d494cdcSFugang Duan 	bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
6164d494cdcSFugang Duan 	dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
61741ef84ceSFugang Duan 	if (((unsigned long)bufaddr) & fep->tx_align ||
6186b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
6194d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, hdr_len);
6204d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
62179f33912SNimrod Andy 
6226b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
62379f33912SNimrod Andy 			swap_buffer(bufaddr, hdr_len);
62479f33912SNimrod Andy 
62579f33912SNimrod Andy 		dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
62679f33912SNimrod Andy 					hdr_len, DMA_TO_DEVICE);
62779f33912SNimrod Andy 		if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
62879f33912SNimrod Andy 			dev_kfree_skb_any(skb);
62979f33912SNimrod Andy 			if (net_ratelimit())
63079f33912SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
63179f33912SNimrod Andy 			return NETDEV_TX_BUSY;
63279f33912SNimrod Andy 		}
63379f33912SNimrod Andy 	}
63479f33912SNimrod Andy 
6355cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
6365cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(hdr_len);
63779f33912SNimrod Andy 
63879f33912SNimrod Andy 	if (fep->bufdesc_ex) {
6396b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
64053bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
64179f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
64279f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
64379f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
6445cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
64579f33912SNimrod Andy 	}
64679f33912SNimrod Andy 
6475cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
64879f33912SNimrod Andy 
64979f33912SNimrod Andy 	return 0;
65079f33912SNimrod Andy }
65179f33912SNimrod Andy 
6524d494cdcSFugang Duan static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
6534d494cdcSFugang Duan 				   struct sk_buff *skb,
6544d494cdcSFugang Duan 				   struct net_device *ndev)
65579f33912SNimrod Andy {
65679f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
65779f33912SNimrod Andy 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
65879f33912SNimrod Andy 	int total_len, data_left;
6597355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
66079f33912SNimrod Andy 	struct tso_t tso;
66179f33912SNimrod Andy 	unsigned int index = 0;
66279f33912SNimrod Andy 	int ret;
66379f33912SNimrod Andy 
6647355f276STroy Kisky 	if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
66579f33912SNimrod Andy 		dev_kfree_skb_any(skb);
66679f33912SNimrod Andy 		if (net_ratelimit())
66779f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for TSO!\n");
66879f33912SNimrod Andy 		return NETDEV_TX_OK;
66979f33912SNimrod Andy 	}
67079f33912SNimrod Andy 
67179f33912SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
67279f33912SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
67379f33912SNimrod Andy 		dev_kfree_skb_any(skb);
67479f33912SNimrod Andy 		return NETDEV_TX_OK;
67579f33912SNimrod Andy 	}
67679f33912SNimrod Andy 
67779f33912SNimrod Andy 	/* Initialize the TSO handler, and prepare the first payload */
67879f33912SNimrod Andy 	tso_start(skb, &tso);
67979f33912SNimrod Andy 
68079f33912SNimrod Andy 	total_len = skb->len - hdr_len;
68179f33912SNimrod Andy 	while (total_len > 0) {
68279f33912SNimrod Andy 		char *hdr;
68379f33912SNimrod Andy 
6847355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
68579f33912SNimrod Andy 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
68679f33912SNimrod Andy 		total_len -= data_left;
68779f33912SNimrod Andy 
68879f33912SNimrod Andy 		/* prepare packet headers: MAC + IP + TCP */
6894d494cdcSFugang Duan 		hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
69079f33912SNimrod Andy 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
6914d494cdcSFugang Duan 		ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
69279f33912SNimrod Andy 		if (ret)
69379f33912SNimrod Andy 			goto err_release;
69479f33912SNimrod Andy 
69579f33912SNimrod Andy 		while (data_left > 0) {
69679f33912SNimrod Andy 			int size;
69779f33912SNimrod Andy 
69879f33912SNimrod Andy 			size = min_t(int, tso.size, data_left);
6997355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
7007355f276STroy Kisky 			index = fec_enet_get_bd_index(bdp, &txq->bd);
7014d494cdcSFugang Duan 			ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
7024d494cdcSFugang Duan 							bdp, index,
7034d494cdcSFugang Duan 							tso.data, size,
7044d494cdcSFugang Duan 							size == data_left,
70579f33912SNimrod Andy 							total_len == 0);
70679f33912SNimrod Andy 			if (ret)
70779f33912SNimrod Andy 				goto err_release;
70879f33912SNimrod Andy 
70979f33912SNimrod Andy 			data_left -= size;
71079f33912SNimrod Andy 			tso_build_data(skb, &tso, size);
71179f33912SNimrod Andy 		}
71279f33912SNimrod Andy 
7137355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
71479f33912SNimrod Andy 	}
71579f33912SNimrod Andy 
71679f33912SNimrod Andy 	/* Save skb pointer */
7174d494cdcSFugang Duan 	txq->tx_skbuff[index] = skb;
71879f33912SNimrod Andy 
71979f33912SNimrod Andy 	skb_tx_timestamp(skb);
7207355f276STroy Kisky 	txq->bd.cur = bdp;
72179f33912SNimrod Andy 
72279f33912SNimrod Andy 	/* Trigger transmission start */
7236b7e4008SLothar Waßmann 	if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
72453bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
72553bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
72653bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
72753bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active))
72853bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
72979f33912SNimrod Andy 
73079f33912SNimrod Andy 	return 0;
73179f33912SNimrod Andy 
73279f33912SNimrod Andy err_release:
73379f33912SNimrod Andy 	/* TODO: Release all used data descriptors for TSO */
73479f33912SNimrod Andy 	return ret;
73579f33912SNimrod Andy }
73679f33912SNimrod Andy 
73761a4427bSNimrod Andy static netdev_tx_t
73861a4427bSNimrod Andy fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
73961a4427bSNimrod Andy {
74061a4427bSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
7416e909283SNimrod Andy 	int entries_free;
7424d494cdcSFugang Duan 	unsigned short queue;
7434d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
7444d494cdcSFugang Duan 	struct netdev_queue *nq;
74561a4427bSNimrod Andy 	int ret;
74661a4427bSNimrod Andy 
7474d494cdcSFugang Duan 	queue = skb_get_queue_mapping(skb);
7484d494cdcSFugang Duan 	txq = fep->tx_queue[queue];
7494d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue);
7504d494cdcSFugang Duan 
75179f33912SNimrod Andy 	if (skb_is_gso(skb))
7524d494cdcSFugang Duan 		ret = fec_enet_txq_submit_tso(txq, skb, ndev);
75379f33912SNimrod Andy 	else
7544d494cdcSFugang Duan 		ret = fec_enet_txq_submit_skb(txq, skb, ndev);
7556e909283SNimrod Andy 	if (ret)
7566e909283SNimrod Andy 		return ret;
75761a4427bSNimrod Andy 
7587355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
7594d494cdcSFugang Duan 	if (entries_free <= txq->tx_stop_threshold)
7604d494cdcSFugang Duan 		netif_tx_stop_queue(nq);
76161a4427bSNimrod Andy 
76261a4427bSNimrod Andy 	return NETDEV_TX_OK;
76361a4427bSNimrod Andy }
76461a4427bSNimrod Andy 
765a210576cSDavid S. Miller /* Init RX & TX buffer descriptors
766a210576cSDavid S. Miller  */
767a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev)
768a210576cSDavid S. Miller {
769a210576cSDavid S. Miller 	struct fec_enet_private *fep = netdev_priv(dev);
7704d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
7714d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
772a210576cSDavid S. Miller 	struct bufdesc *bdp;
773a210576cSDavid S. Miller 	unsigned int i;
77459d0f746SFrank Li 	unsigned int q;
775a210576cSDavid S. Miller 
77659d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
777a210576cSDavid S. Miller 		/* Initialize the receive buffer descriptors. */
77859d0f746SFrank Li 		rxq = fep->rx_queue[q];
7797355f276STroy Kisky 		bdp = rxq->bd.base;
7804d494cdcSFugang Duan 
7817355f276STroy Kisky 		for (i = 0; i < rxq->bd.ring_size; i++) {
782a210576cSDavid S. Miller 
783a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
784a210576cSDavid S. Miller 			if (bdp->cbd_bufaddr)
7855cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
786a210576cSDavid S. Miller 			else
7875cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(0);
7887355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
789a210576cSDavid S. Miller 		}
790a210576cSDavid S. Miller 
791a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
7927355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
7935cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
794a210576cSDavid S. Miller 
7957355f276STroy Kisky 		rxq->bd.cur = rxq->bd.base;
79659d0f746SFrank Li 	}
797a210576cSDavid S. Miller 
79859d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
799a210576cSDavid S. Miller 		/* ...and the same for transmit */
80059d0f746SFrank Li 		txq = fep->tx_queue[q];
8017355f276STroy Kisky 		bdp = txq->bd.base;
8027355f276STroy Kisky 		txq->bd.cur = bdp;
803a210576cSDavid S. Miller 
8047355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
805a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
8065cfa3039SJohannes Berg 			bdp->cbd_sc = cpu_to_fec16(0);
8074d494cdcSFugang Duan 			if (txq->tx_skbuff[i]) {
8084d494cdcSFugang Duan 				dev_kfree_skb_any(txq->tx_skbuff[i]);
8094d494cdcSFugang Duan 				txq->tx_skbuff[i] = NULL;
810a210576cSDavid S. Miller 			}
8115cfa3039SJohannes Berg 			bdp->cbd_bufaddr = cpu_to_fec32(0);
8127355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
813a210576cSDavid S. Miller 		}
814a210576cSDavid S. Miller 
815a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
8167355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
8175cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
8184d494cdcSFugang Duan 		txq->dirty_tx = bdp;
819a210576cSDavid S. Miller 	}
82059d0f746SFrank Li }
82159d0f746SFrank Li 
822ce99d0d3SFrank Li static void fec_enet_active_rxring(struct net_device *ndev)
823ce99d0d3SFrank Li {
824ce99d0d3SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
825ce99d0d3SFrank Li 	int i;
826ce99d0d3SFrank Li 
827ce99d0d3SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
82853bb20d1STroy Kisky 		writel(0, fep->rx_queue[i]->bd.reg_desc_active);
829ce99d0d3SFrank Li }
830ce99d0d3SFrank Li 
83159d0f746SFrank Li static void fec_enet_enable_ring(struct net_device *ndev)
83259d0f746SFrank Li {
83359d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
83459d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
83559d0f746SFrank Li 	struct fec_enet_priv_rx_q *rxq;
83659d0f746SFrank Li 	int i;
83759d0f746SFrank Li 
83859d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
83959d0f746SFrank Li 		rxq = fep->rx_queue[i];
8407355f276STroy Kisky 		writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
841d543a762SNimrod Andy 		writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
84259d0f746SFrank Li 
84359d0f746SFrank Li 		/* enable DMA1/2 */
84459d0f746SFrank Li 		if (i)
84559d0f746SFrank Li 			writel(RCMR_MATCHEN | RCMR_CMP(i),
84659d0f746SFrank Li 			       fep->hwp + FEC_RCMR(i));
84759d0f746SFrank Li 	}
84859d0f746SFrank Li 
84959d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
85059d0f746SFrank Li 		txq = fep->tx_queue[i];
8517355f276STroy Kisky 		writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
85259d0f746SFrank Li 
85359d0f746SFrank Li 		/* enable DMA1/2 */
85459d0f746SFrank Li 		if (i)
85559d0f746SFrank Li 			writel(DMA_CLASS_EN | IDLE_SLOPE(i),
85659d0f746SFrank Li 			       fep->hwp + FEC_DMA_CFG(i));
85759d0f746SFrank Li 	}
85859d0f746SFrank Li }
85959d0f746SFrank Li 
86059d0f746SFrank Li static void fec_enet_reset_skb(struct net_device *ndev)
86159d0f746SFrank Li {
86259d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
86359d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
86459d0f746SFrank Li 	int i, j;
86559d0f746SFrank Li 
86659d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
86759d0f746SFrank Li 		txq = fep->tx_queue[i];
86859d0f746SFrank Li 
8697355f276STroy Kisky 		for (j = 0; j < txq->bd.ring_size; j++) {
87059d0f746SFrank Li 			if (txq->tx_skbuff[j]) {
87159d0f746SFrank Li 				dev_kfree_skb_any(txq->tx_skbuff[j]);
87259d0f746SFrank Li 				txq->tx_skbuff[j] = NULL;
87359d0f746SFrank Li 			}
87459d0f746SFrank Li 		}
87559d0f746SFrank Li 	}
87659d0f746SFrank Li }
877a210576cSDavid S. Miller 
878dbc64a8eSRussell King /*
879dbc64a8eSRussell King  * This function is called to start or restart the FEC during a link
880dbc64a8eSRussell King  * change, transmit timeout, or to reconfigure the FEC.  The network
881dbc64a8eSRussell King  * packet processing for this device must be stopped before this call.
882793fc096SFrank Li  */
883793fc096SFrank Li static void
884ef83337dSRussell King fec_restart(struct net_device *ndev)
885793fc096SFrank Li {
886793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
8874c09eed9SJim Baxter 	u32 val;
888793fc096SFrank Li 	u32 temp_mac[2];
889793fc096SFrank Li 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
890793fc096SFrank Li 	u32 ecntl = 0x2; /* ETHEREN */
891793fc096SFrank Li 
892106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
893106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
894106c314cSFugang Duan 	 * instead of reset MAC itself.
895106c314cSFugang Duan 	 */
8966b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_AVB) {
897106c314cSFugang Duan 		writel(0, fep->hwp + FEC_ECNTRL);
898106c314cSFugang Duan 	} else {
899793fc096SFrank Li 		writel(1, fep->hwp + FEC_ECNTRL);
900793fc096SFrank Li 		udelay(10);
901106c314cSFugang Duan 	}
902793fc096SFrank Li 
903793fc096SFrank Li 	/*
904793fc096SFrank Li 	 * enet-mac reset will reset mac address registers too,
905793fc096SFrank Li 	 * so need to reconfigure it.
906793fc096SFrank Li 	 */
9076b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
908793fc096SFrank Li 		memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
9095cfa3039SJohannes Berg 		writel((__force u32)cpu_to_be32(temp_mac[0]),
9105cfa3039SJohannes Berg 		       fep->hwp + FEC_ADDR_LOW);
9115cfa3039SJohannes Berg 		writel((__force u32)cpu_to_be32(temp_mac[1]),
9125cfa3039SJohannes Berg 		       fep->hwp + FEC_ADDR_HIGH);
913793fc096SFrank Li 	}
914793fc096SFrank Li 
915793fc096SFrank Li 	/* Clear any outstanding interrupt. */
916e17f7fecSNimrod Andy 	writel(0xffffffff, fep->hwp + FEC_IEVENT);
917793fc096SFrank Li 
918a210576cSDavid S. Miller 	fec_enet_bd_init(ndev);
919a210576cSDavid S. Miller 
92059d0f746SFrank Li 	fec_enet_enable_ring(ndev);
921793fc096SFrank Li 
92259d0f746SFrank Li 	/* Reset tx SKB buffers. */
92359d0f746SFrank Li 	fec_enet_reset_skb(ndev);
924793fc096SFrank Li 
925793fc096SFrank Li 	/* Enable MII mode */
926ef83337dSRussell King 	if (fep->full_duplex == DUPLEX_FULL) {
927793fc096SFrank Li 		/* FD enable */
928793fc096SFrank Li 		writel(0x04, fep->hwp + FEC_X_CNTRL);
929793fc096SFrank Li 	} else {
930793fc096SFrank Li 		/* No Rcv on Xmit */
931793fc096SFrank Li 		rcntl |= 0x02;
932793fc096SFrank Li 		writel(0x0, fep->hwp + FEC_X_CNTRL);
933793fc096SFrank Li 	}
934793fc096SFrank Li 
935793fc096SFrank Li 	/* Set MII speed */
936793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
937793fc096SFrank Li 
938d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
93918803495SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_HAS_RACC) {
9404c09eed9SJim Baxter 		/* set RX checksum */
9414c09eed9SJim Baxter 		val = readl(fep->hwp + FEC_RACC);
9424c09eed9SJim Baxter 		if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
9434c09eed9SJim Baxter 			val |= FEC_RACC_OPTIONS;
9444c09eed9SJim Baxter 		else
9454c09eed9SJim Baxter 			val &= ~FEC_RACC_OPTIONS;
9464c09eed9SJim Baxter 		writel(val, fep->hwp + FEC_RACC);
94755cd48c8STroy Kisky 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
94832867fccSFabio Estevam 	}
949d1391930SGuenter Roeck #endif
9504c09eed9SJim Baxter 
951793fc096SFrank Li 	/*
952793fc096SFrank Li 	 * The phy interface and speed need to get configured
953793fc096SFrank Li 	 * differently on enet-mac.
954793fc096SFrank Li 	 */
9556b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
956793fc096SFrank Li 		/* Enable flow control and length check */
957793fc096SFrank Li 		rcntl |= 0x40000000 | 0x00000020;
958793fc096SFrank Li 
959793fc096SFrank Li 		/* RGMII, RMII or MII */
960e813bb2bSMarkus Pargmann 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
961e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
962e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
963e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
964793fc096SFrank Li 			rcntl |= (1 << 6);
965793fc096SFrank Li 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
966793fc096SFrank Li 			rcntl |= (1 << 8);
967793fc096SFrank Li 		else
968793fc096SFrank Li 			rcntl &= ~(1 << 8);
969793fc096SFrank Li 
970793fc096SFrank Li 		/* 1G, 100M or 10M */
97145f5c327SPhilippe Reynes 		if (ndev->phydev) {
97245f5c327SPhilippe Reynes 			if (ndev->phydev->speed == SPEED_1000)
973793fc096SFrank Li 				ecntl |= (1 << 5);
97445f5c327SPhilippe Reynes 			else if (ndev->phydev->speed == SPEED_100)
975793fc096SFrank Li 				rcntl &= ~(1 << 9);
976793fc096SFrank Li 			else
977793fc096SFrank Li 				rcntl |= (1 << 9);
978793fc096SFrank Li 		}
979793fc096SFrank Li 	} else {
980793fc096SFrank Li #ifdef FEC_MIIGSK_ENR
9816b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_USE_GASKET) {
982793fc096SFrank Li 			u32 cfgr;
983793fc096SFrank Li 			/* disable the gasket and wait */
984793fc096SFrank Li 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
985793fc096SFrank Li 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
986793fc096SFrank Li 				udelay(1);
987793fc096SFrank Li 
988793fc096SFrank Li 			/*
989793fc096SFrank Li 			 * configure the gasket:
990793fc096SFrank Li 			 *   RMII, 50 MHz, no loopback, no echo
991793fc096SFrank Li 			 *   MII, 25 MHz, no loopback, no echo
992793fc096SFrank Li 			 */
993793fc096SFrank Li 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
994793fc096SFrank Li 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
99545f5c327SPhilippe Reynes 			if (ndev->phydev && ndev->phydev->speed == SPEED_10)
996793fc096SFrank Li 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
997793fc096SFrank Li 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
998793fc096SFrank Li 
999793fc096SFrank Li 			/* re-enable the gasket */
1000793fc096SFrank Li 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
1001793fc096SFrank Li 		}
1002793fc096SFrank Li #endif
1003793fc096SFrank Li 	}
1004793fc096SFrank Li 
1005d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1006793fc096SFrank Li 	/* enable pause frame*/
1007793fc096SFrank Li 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1008793fc096SFrank Li 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
100945f5c327SPhilippe Reynes 	     ndev->phydev && ndev->phydev->pause)) {
1010793fc096SFrank Li 		rcntl |= FEC_ENET_FCE;
1011793fc096SFrank Li 
10124c09eed9SJim Baxter 		/* set FIFO threshold parameter to reduce overrun */
1013793fc096SFrank Li 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1014793fc096SFrank Li 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1015793fc096SFrank Li 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1016793fc096SFrank Li 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1017793fc096SFrank Li 
1018793fc096SFrank Li 		/* OPD */
1019793fc096SFrank Li 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1020793fc096SFrank Li 	} else {
1021793fc096SFrank Li 		rcntl &= ~FEC_ENET_FCE;
1022793fc096SFrank Li 	}
1023d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
1024793fc096SFrank Li 
1025793fc096SFrank Li 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
1026793fc096SFrank Li 
102784fe6182SStefan Wahren 	/* Setup multicast filter. */
102884fe6182SStefan Wahren 	set_multicast_list(ndev);
102984fe6182SStefan Wahren #ifndef CONFIG_M5272
103084fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
103184fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
103284fe6182SStefan Wahren #endif
103384fe6182SStefan Wahren 
10346b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1035793fc096SFrank Li 		/* enable ENET endian swap */
1036793fc096SFrank Li 		ecntl |= (1 << 8);
1037793fc096SFrank Li 		/* enable ENET store and forward mode */
1038793fc096SFrank Li 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
1039793fc096SFrank Li 	}
1040793fc096SFrank Li 
1041793fc096SFrank Li 	if (fep->bufdesc_ex)
1042793fc096SFrank Li 		ecntl |= (1 << 4);
1043793fc096SFrank Li 
104438ae92dcSChris Healy #ifndef CONFIG_M5272
1045b9eef55cSJim Baxter 	/* Enable the MIB statistic event counters */
1046b9eef55cSJim Baxter 	writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
104738ae92dcSChris Healy #endif
104838ae92dcSChris Healy 
1049793fc096SFrank Li 	/* And last, enable the transmit and receive processing */
1050793fc096SFrank Li 	writel(ecntl, fep->hwp + FEC_ECNTRL);
1051ce99d0d3SFrank Li 	fec_enet_active_rxring(ndev);
1052793fc096SFrank Li 
1053793fc096SFrank Li 	if (fep->bufdesc_ex)
1054793fc096SFrank Li 		fec_ptp_start_cyclecounter(ndev);
1055793fc096SFrank Li 
1056793fc096SFrank Li 	/* Enable interrupts we wish to service */
10570c5a3aefSNimrod Andy 	if (fep->link)
1058793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
10590c5a3aefSNimrod Andy 	else
10600c5a3aefSNimrod Andy 		writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1061d851b47bSFugang Duan 
1062d851b47bSFugang Duan 	/* Init the interrupt coalescing */
1063d851b47bSFugang Duan 	fec_enet_itr_coal_init(ndev);
1064d851b47bSFugang Duan 
1065793fc096SFrank Li }
1066793fc096SFrank Li 
1067793fc096SFrank Li static void
1068793fc096SFrank Li fec_stop(struct net_device *ndev)
1069793fc096SFrank Li {
1070793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1071de40ed31SNimrod Andy 	struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1072793fc096SFrank Li 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1073de40ed31SNimrod Andy 	u32 val;
1074793fc096SFrank Li 
1075793fc096SFrank Li 	/* We cannot expect a graceful transmit stop without link !!! */
1076793fc096SFrank Li 	if (fep->link) {
1077793fc096SFrank Li 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1078793fc096SFrank Li 		udelay(10);
1079793fc096SFrank Li 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
108031b7720cSJoe Perches 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1081793fc096SFrank Li 	}
1082793fc096SFrank Li 
1083106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
1084106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1085106c314cSFugang Duan 	 * instead of reset MAC itself.
1086106c314cSFugang Duan 	 */
1087de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
10886b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1089106c314cSFugang Duan 			writel(0, fep->hwp + FEC_ECNTRL);
1090106c314cSFugang Duan 		} else {
1091793fc096SFrank Li 			writel(1, fep->hwp + FEC_ECNTRL);
1092793fc096SFrank Li 			udelay(10);
1093106c314cSFugang Duan 		}
1094793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1095de40ed31SNimrod Andy 	} else {
1096de40ed31SNimrod Andy 		writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1097de40ed31SNimrod Andy 		val = readl(fep->hwp + FEC_ECNTRL);
1098de40ed31SNimrod Andy 		val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1099de40ed31SNimrod Andy 		writel(val, fep->hwp + FEC_ECNTRL);
1100de40ed31SNimrod Andy 
1101de40ed31SNimrod Andy 		if (pdata && pdata->sleep_mode_enable)
1102de40ed31SNimrod Andy 			pdata->sleep_mode_enable(true);
1103de40ed31SNimrod Andy 	}
1104de40ed31SNimrod Andy 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1105793fc096SFrank Li 
1106793fc096SFrank Li 	/* We have to keep ENET enabled to have MII interrupt stay working */
1107de40ed31SNimrod Andy 	if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1108de40ed31SNimrod Andy 		!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1109793fc096SFrank Li 		writel(2, fep->hwp + FEC_ECNTRL);
1110793fc096SFrank Li 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1111793fc096SFrank Li 	}
1112793fc096SFrank Li }
1113793fc096SFrank Li 
1114793fc096SFrank Li 
1115793fc096SFrank Li static void
1116793fc096SFrank Li fec_timeout(struct net_device *ndev)
1117793fc096SFrank Li {
1118793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1119793fc096SFrank Li 
1120344756f6SRussell King 	fec_dump(ndev);
1121344756f6SRussell King 
1122793fc096SFrank Li 	ndev->stats.tx_errors++;
1123793fc096SFrank Li 
112436cdc743SRussell King 	schedule_work(&fep->tx_timeout_work);
112554309fa6SFrank Li }
112654309fa6SFrank Li 
112736cdc743SRussell King static void fec_enet_timeout_work(struct work_struct *work)
112854309fa6SFrank Li {
112954309fa6SFrank Li 	struct fec_enet_private *fep =
113036cdc743SRussell King 		container_of(work, struct fec_enet_private, tx_timeout_work);
11318ce5624fSRussell King 	struct net_device *ndev = fep->netdev;
113254309fa6SFrank Li 
1133da1774e5SRussell King 	rtnl_lock();
11348ce5624fSRussell King 	if (netif_device_present(ndev) || netif_running(ndev)) {
1135dbc64a8eSRussell King 		napi_disable(&fep->napi);
1136dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
1137ef83337dSRussell King 		fec_restart(ndev);
11388ce5624fSRussell King 		netif_wake_queue(ndev);
11396af42d42SRussell King 		netif_tx_unlock_bh(ndev);
1140dbc64a8eSRussell King 		napi_enable(&fep->napi);
11418ce5624fSRussell King 	}
1142da1774e5SRussell King 	rtnl_unlock();
114354309fa6SFrank Li }
1144793fc096SFrank Li 
1145793fc096SFrank Li static void
1146bfd4ecddSRussell King fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1147bfd4ecddSRussell King 	struct skb_shared_hwtstamps *hwtstamps)
1148bfd4ecddSRussell King {
1149bfd4ecddSRussell King 	unsigned long flags;
1150bfd4ecddSRussell King 	u64 ns;
1151bfd4ecddSRussell King 
1152bfd4ecddSRussell King 	spin_lock_irqsave(&fep->tmreg_lock, flags);
1153bfd4ecddSRussell King 	ns = timecounter_cyc2time(&fep->tc, ts);
1154bfd4ecddSRussell King 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1155bfd4ecddSRussell King 
1156bfd4ecddSRussell King 	memset(hwtstamps, 0, sizeof(*hwtstamps));
1157bfd4ecddSRussell King 	hwtstamps->hwtstamp = ns_to_ktime(ns);
1158bfd4ecddSRussell King }
1159bfd4ecddSRussell King 
1160bfd4ecddSRussell King static void
11614d494cdcSFugang Duan fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1162793fc096SFrank Li {
1163793fc096SFrank Li 	struct	fec_enet_private *fep;
1164a2fe37b6SFabio Estevam 	struct bufdesc *bdp;
1165793fc096SFrank Li 	unsigned short status;
1166793fc096SFrank Li 	struct	sk_buff	*skb;
11674d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
11684d494cdcSFugang Duan 	struct netdev_queue *nq;
1169793fc096SFrank Li 	int	index = 0;
117079f33912SNimrod Andy 	int	entries_free;
1171793fc096SFrank Li 
1172793fc096SFrank Li 	fep = netdev_priv(ndev);
11734d494cdcSFugang Duan 
11744d494cdcSFugang Duan 	queue_id = FEC_ENET_GET_QUQUE(queue_id);
11754d494cdcSFugang Duan 
11764d494cdcSFugang Duan 	txq = fep->tx_queue[queue_id];
11774d494cdcSFugang Duan 	/* get next bdp of dirty_tx */
11784d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue_id);
11794d494cdcSFugang Duan 	bdp = txq->dirty_tx;
1180793fc096SFrank Li 
1181793fc096SFrank Li 	/* get next bdp of dirty_tx */
11827355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1183793fc096SFrank Li 
11847355f276STroy Kisky 	while (bdp != READ_ONCE(txq->bd.cur)) {
11857355f276STroy Kisky 		/* Order the load of bd.cur and cbd_sc */
1186c4bc44c6SKevin Hao 		rmb();
11875cfa3039SJohannes Berg 		status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1188c4bc44c6SKevin Hao 		if (status & BD_ENET_TX_READY)
1189793fc096SFrank Li 			break;
1190793fc096SFrank Li 
11917355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
11922b995f63SNimrod Andy 
1193a2fe37b6SFabio Estevam 		skb = txq->tx_skbuff[index];
1194a2fe37b6SFabio Estevam 		txq->tx_skbuff[index] = NULL;
11955cfa3039SJohannes Berg 		if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
11965cfa3039SJohannes Berg 			dma_unmap_single(&fep->pdev->dev,
11975cfa3039SJohannes Berg 					 fec32_to_cpu(bdp->cbd_bufaddr),
11985cfa3039SJohannes Berg 					 fec16_to_cpu(bdp->cbd_datlen),
11995cfa3039SJohannes Berg 					 DMA_TO_DEVICE);
12005cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
1201a2fe37b6SFabio Estevam 		if (!skb) {
12027355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1203a2fe37b6SFabio Estevam 			continue;
12046e909283SNimrod Andy 		}
1205793fc096SFrank Li 
1206793fc096SFrank Li 		/* Check for errors. */
1207793fc096SFrank Li 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1208793fc096SFrank Li 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
1209793fc096SFrank Li 				   BD_ENET_TX_CSL)) {
1210793fc096SFrank Li 			ndev->stats.tx_errors++;
1211793fc096SFrank Li 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
1212793fc096SFrank Li 				ndev->stats.tx_heartbeat_errors++;
1213793fc096SFrank Li 			if (status & BD_ENET_TX_LC)  /* Late collision */
1214793fc096SFrank Li 				ndev->stats.tx_window_errors++;
1215793fc096SFrank Li 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
1216793fc096SFrank Li 				ndev->stats.tx_aborted_errors++;
1217793fc096SFrank Li 			if (status & BD_ENET_TX_UN)  /* Underrun */
1218793fc096SFrank Li 				ndev->stats.tx_fifo_errors++;
1219793fc096SFrank Li 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
1220793fc096SFrank Li 				ndev->stats.tx_carrier_errors++;
1221793fc096SFrank Li 		} else {
1222793fc096SFrank Li 			ndev->stats.tx_packets++;
12236e909283SNimrod Andy 			ndev->stats.tx_bytes += skb->len;
1224793fc096SFrank Li 		}
1225793fc096SFrank Li 
1226793fc096SFrank Li 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1227793fc096SFrank Li 			fep->bufdesc_ex) {
1228793fc096SFrank Li 			struct skb_shared_hwtstamps shhwtstamps;
1229793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1230793fc096SFrank Li 
12315cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1232793fc096SFrank Li 			skb_tstamp_tx(skb, &shhwtstamps);
1233793fc096SFrank Li 		}
1234793fc096SFrank Li 
1235793fc096SFrank Li 		/* Deferred means some collisions occurred during transmit,
1236793fc096SFrank Li 		 * but we eventually sent the packet OK.
1237793fc096SFrank Li 		 */
1238793fc096SFrank Li 		if (status & BD_ENET_TX_DEF)
1239793fc096SFrank Li 			ndev->stats.collisions++;
1240793fc096SFrank Li 
1241793fc096SFrank Li 		/* Free the sk buffer associated with this last transmit */
1242793fc096SFrank Li 		dev_kfree_skb_any(skb);
1243793fc096SFrank Li 
1244c4bc44c6SKevin Hao 		/* Make sure the update to bdp and tx_skbuff are performed
1245c4bc44c6SKevin Hao 		 * before dirty_tx
1246c4bc44c6SKevin Hao 		 */
1247c4bc44c6SKevin Hao 		wmb();
12484d494cdcSFugang Duan 		txq->dirty_tx = bdp;
1249793fc096SFrank Li 
1250793fc096SFrank Li 		/* Update pointer to next buffer descriptor to be transmitted */
12517355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1252793fc096SFrank Li 
1253793fc096SFrank Li 		/* Since we have freed up a buffer, the ring is no longer full
1254793fc096SFrank Li 		 */
125579f33912SNimrod Andy 		if (netif_queue_stopped(ndev)) {
12567355f276STroy Kisky 			entries_free = fec_enet_get_free_txdesc_num(txq);
12574d494cdcSFugang Duan 			if (entries_free >= txq->tx_wake_threshold)
12584d494cdcSFugang Duan 				netif_tx_wake_queue(nq);
1259793fc096SFrank Li 		}
126079f33912SNimrod Andy 	}
1261ccea2968SRussell King 
1262ccea2968SRussell King 	/* ERR006538: Keep the transmitter going */
12637355f276STroy Kisky 	if (bdp != txq->bd.cur &&
126453bb20d1STroy Kisky 	    readl(txq->bd.reg_desc_active) == 0)
126553bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
12664d494cdcSFugang Duan }
12674d494cdcSFugang Duan 
12684d494cdcSFugang Duan static void
12694d494cdcSFugang Duan fec_enet_tx(struct net_device *ndev)
12704d494cdcSFugang Duan {
12714d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
12724d494cdcSFugang Duan 	u16 queue_id;
12734d494cdcSFugang Duan 	/* First process class A queue, then Class B and Best Effort queue */
12744d494cdcSFugang Duan 	for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
12754d494cdcSFugang Duan 		clear_bit(queue_id, &fep->work_tx);
12764d494cdcSFugang Duan 		fec_enet_tx_queue(ndev, queue_id);
12774d494cdcSFugang Duan 	}
12784d494cdcSFugang Duan 	return;
1279793fc096SFrank Li }
1280793fc096SFrank Li 
12811b7bde6dSNimrod Andy static int
12821b7bde6dSNimrod Andy fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
12831b7bde6dSNimrod Andy {
12841b7bde6dSNimrod Andy 	struct  fec_enet_private *fep = netdev_priv(ndev);
12851b7bde6dSNimrod Andy 	int off;
12861b7bde6dSNimrod Andy 
12871b7bde6dSNimrod Andy 	off = ((unsigned long)skb->data) & fep->rx_align;
12881b7bde6dSNimrod Andy 	if (off)
12891b7bde6dSNimrod Andy 		skb_reserve(skb, fep->rx_align + 1 - off);
12901b7bde6dSNimrod Andy 
12915cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
12925cfa3039SJohannes Berg 	if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
12931b7bde6dSNimrod Andy 		if (net_ratelimit())
12941b7bde6dSNimrod Andy 			netdev_err(ndev, "Rx DMA memory map failed\n");
12951b7bde6dSNimrod Andy 		return -ENOMEM;
12961b7bde6dSNimrod Andy 	}
12971b7bde6dSNimrod Andy 
12981b7bde6dSNimrod Andy 	return 0;
12991b7bde6dSNimrod Andy }
13001b7bde6dSNimrod Andy 
13011b7bde6dSNimrod Andy static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
13021310b544SLothar Waßmann 			       struct bufdesc *bdp, u32 length, bool swap)
13031b7bde6dSNimrod Andy {
13041b7bde6dSNimrod Andy 	struct  fec_enet_private *fep = netdev_priv(ndev);
13051b7bde6dSNimrod Andy 	struct sk_buff *new_skb;
13061b7bde6dSNimrod Andy 
13071b7bde6dSNimrod Andy 	if (length > fep->rx_copybreak)
13081b7bde6dSNimrod Andy 		return false;
13091b7bde6dSNimrod Andy 
13101b7bde6dSNimrod Andy 	new_skb = netdev_alloc_skb(ndev, length);
13111b7bde6dSNimrod Andy 	if (!new_skb)
13121b7bde6dSNimrod Andy 		return false;
13131b7bde6dSNimrod Andy 
13145cfa3039SJohannes Berg 	dma_sync_single_for_cpu(&fep->pdev->dev,
13155cfa3039SJohannes Berg 				fec32_to_cpu(bdp->cbd_bufaddr),
13161b7bde6dSNimrod Andy 				FEC_ENET_RX_FRSIZE - fep->rx_align,
13171b7bde6dSNimrod Andy 				DMA_FROM_DEVICE);
13181310b544SLothar Waßmann 	if (!swap)
13191b7bde6dSNimrod Andy 		memcpy(new_skb->data, (*skb)->data, length);
13201310b544SLothar Waßmann 	else
13211310b544SLothar Waßmann 		swap_buffer2(new_skb->data, (*skb)->data, length);
13221b7bde6dSNimrod Andy 	*skb = new_skb;
13231b7bde6dSNimrod Andy 
13241b7bde6dSNimrod Andy 	return true;
13251b7bde6dSNimrod Andy }
13261b7bde6dSNimrod Andy 
13277355f276STroy Kisky /* During a receive, the bd_rx.cur points to the current incoming buffer.
1328793fc096SFrank Li  * When we update through the ring, if the next incoming buffer has
1329793fc096SFrank Li  * not been given to the system, we just set the empty indicator,
1330793fc096SFrank Li  * effectively tossing the packet.
1331793fc096SFrank Li  */
1332793fc096SFrank Li static int
13334d494cdcSFugang Duan fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1334793fc096SFrank Li {
1335793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
13364d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
1337793fc096SFrank Li 	struct bufdesc *bdp;
1338793fc096SFrank Li 	unsigned short status;
13391b7bde6dSNimrod Andy 	struct  sk_buff *skb_new = NULL;
1340793fc096SFrank Li 	struct  sk_buff *skb;
1341793fc096SFrank Li 	ushort	pkt_len;
1342793fc096SFrank Li 	__u8 *data;
1343793fc096SFrank Li 	int	pkt_received = 0;
1344cdffcf1bSJim Baxter 	struct	bufdesc_ex *ebdp = NULL;
1345cdffcf1bSJim Baxter 	bool	vlan_packet_rcvd = false;
1346cdffcf1bSJim Baxter 	u16	vlan_tag;
1347d842a31fSDuan Fugang-B38611 	int	index = 0;
13481b7bde6dSNimrod Andy 	bool	is_copybreak;
13496b7e4008SLothar Waßmann 	bool	need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1350793fc096SFrank Li 
1351793fc096SFrank Li #ifdef CONFIG_M532x
1352793fc096SFrank Li 	flush_cache_all();
1353793fc096SFrank Li #endif
13544d494cdcSFugang Duan 	queue_id = FEC_ENET_GET_QUQUE(queue_id);
13554d494cdcSFugang Duan 	rxq = fep->rx_queue[queue_id];
1356793fc096SFrank Li 
1357793fc096SFrank Li 	/* First, grab all of the stats for the incoming packet.
1358793fc096SFrank Li 	 * These get messed up if we get called due to a busy condition.
1359793fc096SFrank Li 	 */
13607355f276STroy Kisky 	bdp = rxq->bd.cur;
1361793fc096SFrank Li 
13625cfa3039SJohannes Berg 	while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1363793fc096SFrank Li 
1364793fc096SFrank Li 		if (pkt_received >= budget)
1365793fc096SFrank Li 			break;
1366793fc096SFrank Li 		pkt_received++;
1367793fc096SFrank Li 
1368ed63f1dcSRussell King 		writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
1369db3421c1SRussell King 
1370793fc096SFrank Li 		/* Check for errors. */
1371095098e1STroy Kisky 		status ^= BD_ENET_RX_LAST;
1372793fc096SFrank Li 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1373095098e1STroy Kisky 			   BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1374095098e1STroy Kisky 			   BD_ENET_RX_CL)) {
1375793fc096SFrank Li 			ndev->stats.rx_errors++;
1376095098e1STroy Kisky 			if (status & BD_ENET_RX_OV) {
1377095098e1STroy Kisky 				/* FIFO overrun */
1378095098e1STroy Kisky 				ndev->stats.rx_fifo_errors++;
1379095098e1STroy Kisky 				goto rx_processing_done;
1380095098e1STroy Kisky 			}
1381095098e1STroy Kisky 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1382095098e1STroy Kisky 						| BD_ENET_RX_LAST)) {
1383793fc096SFrank Li 				/* Frame too long or too short. */
1384793fc096SFrank Li 				ndev->stats.rx_length_errors++;
1385095098e1STroy Kisky 				if (status & BD_ENET_RX_LAST)
1386095098e1STroy Kisky 					netdev_err(ndev, "rcv is not +last\n");
1387793fc096SFrank Li 			}
1388793fc096SFrank Li 			if (status & BD_ENET_RX_CR)	/* CRC Error */
1389793fc096SFrank Li 				ndev->stats.rx_crc_errors++;
1390095098e1STroy Kisky 			/* Report late collisions as a frame error. */
1391095098e1STroy Kisky 			if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1392793fc096SFrank Li 				ndev->stats.rx_frame_errors++;
1393793fc096SFrank Li 			goto rx_processing_done;
1394793fc096SFrank Li 		}
1395793fc096SFrank Li 
1396793fc096SFrank Li 		/* Process the incoming frame. */
1397793fc096SFrank Li 		ndev->stats.rx_packets++;
13985cfa3039SJohannes Berg 		pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1399793fc096SFrank Li 		ndev->stats.rx_bytes += pkt_len;
1400793fc096SFrank Li 
14017355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &rxq->bd);
14021b7bde6dSNimrod Andy 		skb = rxq->rx_skbuff[index];
14031b7bde6dSNimrod Andy 
14041b7bde6dSNimrod Andy 		/* The packet length includes FCS, but we don't want to
14051b7bde6dSNimrod Andy 		 * include that when passing upstream as it messes up
14061b7bde6dSNimrod Andy 		 * bridging applications.
14071b7bde6dSNimrod Andy 		 */
14081310b544SLothar Waßmann 		is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
14091310b544SLothar Waßmann 						  need_swap);
14101b7bde6dSNimrod Andy 		if (!is_copybreak) {
14111b7bde6dSNimrod Andy 			skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
14121b7bde6dSNimrod Andy 			if (unlikely(!skb_new)) {
14131b7bde6dSNimrod Andy 				ndev->stats.rx_dropped++;
14141b7bde6dSNimrod Andy 				goto rx_processing_done;
14151b7bde6dSNimrod Andy 			}
14165cfa3039SJohannes Berg 			dma_unmap_single(&fep->pdev->dev,
14175cfa3039SJohannes Berg 					 fec32_to_cpu(bdp->cbd_bufaddr),
1418b64bf4b7SNimrod Andy 					 FEC_ENET_RX_FRSIZE - fep->rx_align,
1419b64bf4b7SNimrod Andy 					 DMA_FROM_DEVICE);
14201b7bde6dSNimrod Andy 		}
1421793fc096SFrank Li 
14221b7bde6dSNimrod Andy 		prefetch(skb->data - NET_IP_ALIGN);
14231b7bde6dSNimrod Andy 		skb_put(skb, pkt_len - 4);
14241b7bde6dSNimrod Andy 		data = skb->data;
14251310b544SLothar Waßmann 		if (!is_copybreak && need_swap)
1426793fc096SFrank Li 			swap_buffer(data, pkt_len);
1427793fc096SFrank Li 
1428cdffcf1bSJim Baxter 		/* Extract the enhanced buffer descriptor */
1429cdffcf1bSJim Baxter 		ebdp = NULL;
1430cdffcf1bSJim Baxter 		if (fep->bufdesc_ex)
1431cdffcf1bSJim Baxter 			ebdp = (struct bufdesc_ex *)bdp;
1432cdffcf1bSJim Baxter 
1433cdffcf1bSJim Baxter 		/* If this is a VLAN packet remove the VLAN Tag */
1434cdffcf1bSJim Baxter 		vlan_packet_rcvd = false;
1435cdffcf1bSJim Baxter 		if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
14365cfa3039SJohannes Berg 		    fep->bufdesc_ex &&
14375cfa3039SJohannes Berg 		    (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1438cdffcf1bSJim Baxter 			/* Push and remove the vlan tag */
1439cdffcf1bSJim Baxter 			struct vlan_hdr *vlan_header =
1440cdffcf1bSJim Baxter 					(struct vlan_hdr *) (data + ETH_HLEN);
1441cdffcf1bSJim Baxter 			vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1442cdffcf1bSJim Baxter 
1443cdffcf1bSJim Baxter 			vlan_packet_rcvd = true;
14441b7bde6dSNimrod Andy 
1445af5cbc98SNimrod Andy 			memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
14461b7bde6dSNimrod Andy 			skb_pull(skb, VLAN_HLEN);
1447cdffcf1bSJim Baxter 		}
1448cdffcf1bSJim Baxter 
1449793fc096SFrank Li 		skb->protocol = eth_type_trans(skb, ndev);
1450793fc096SFrank Li 
1451793fc096SFrank Li 		/* Get receive timestamp from the skb */
1452bfd4ecddSRussell King 		if (fep->hwts_rx_en && fep->bufdesc_ex)
14535cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1454bfd4ecddSRussell King 					  skb_hwtstamps(skb));
1455793fc096SFrank Li 
14564c09eed9SJim Baxter 		if (fep->bufdesc_ex &&
14574c09eed9SJim Baxter 		    (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
14585cfa3039SJohannes Berg 			if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
14594c09eed9SJim Baxter 				/* don't check it */
14604c09eed9SJim Baxter 				skb->ip_summed = CHECKSUM_UNNECESSARY;
14614c09eed9SJim Baxter 			} else {
14624c09eed9SJim Baxter 				skb_checksum_none_assert(skb);
14634c09eed9SJim Baxter 			}
14644c09eed9SJim Baxter 		}
14654c09eed9SJim Baxter 
1466cdffcf1bSJim Baxter 		/* Handle received VLAN packets */
1467cdffcf1bSJim Baxter 		if (vlan_packet_rcvd)
1468cdffcf1bSJim Baxter 			__vlan_hwaccel_put_tag(skb,
1469cdffcf1bSJim Baxter 					       htons(ETH_P_8021Q),
1470cdffcf1bSJim Baxter 					       vlan_tag);
1471cdffcf1bSJim Baxter 
1472793fc096SFrank Li 		napi_gro_receive(&fep->napi, skb);
1473793fc096SFrank Li 
14741b7bde6dSNimrod Andy 		if (is_copybreak) {
14755cfa3039SJohannes Berg 			dma_sync_single_for_device(&fep->pdev->dev,
14765cfa3039SJohannes Berg 						   fec32_to_cpu(bdp->cbd_bufaddr),
1477b64bf4b7SNimrod Andy 						   FEC_ENET_RX_FRSIZE - fep->rx_align,
1478b64bf4b7SNimrod Andy 						   DMA_FROM_DEVICE);
14791b7bde6dSNimrod Andy 		} else {
14801b7bde6dSNimrod Andy 			rxq->rx_skbuff[index] = skb_new;
14811b7bde6dSNimrod Andy 			fec_enet_new_rxbdp(ndev, bdp, skb_new);
14821b7bde6dSNimrod Andy 		}
14831b7bde6dSNimrod Andy 
1484793fc096SFrank Li rx_processing_done:
1485793fc096SFrank Li 		/* Clear the status flags for this buffer */
1486793fc096SFrank Li 		status &= ~BD_ENET_RX_STATS;
1487793fc096SFrank Li 
1488793fc096SFrank Li 		/* Mark the buffer empty */
1489793fc096SFrank Li 		status |= BD_ENET_RX_EMPTY;
1490793fc096SFrank Li 
1491793fc096SFrank Li 		if (fep->bufdesc_ex) {
1492793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1493793fc096SFrank Li 
14945cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1495793fc096SFrank Li 			ebdp->cbd_prot = 0;
1496793fc096SFrank Li 			ebdp->cbd_bdu = 0;
1497793fc096SFrank Li 		}
1498be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
1499be293467STroy Kisky 		 * performed before transferring ownership.
1500be293467STroy Kisky 		 */
1501be293467STroy Kisky 		wmb();
1502be293467STroy Kisky 		bdp->cbd_sc = cpu_to_fec16(status);
1503793fc096SFrank Li 
1504793fc096SFrank Li 		/* Update BD pointer to next entry */
15057355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
150636e24e2eSDuan Fugang-B38611 
1507793fc096SFrank Li 		/* Doing this here will keep the FEC running while we process
1508793fc096SFrank Li 		 * incoming frames.  On a heavily loaded network, we should be
1509793fc096SFrank Li 		 * able to keep up at the expense of system resources.
1510793fc096SFrank Li 		 */
151153bb20d1STroy Kisky 		writel(0, rxq->bd.reg_desc_active);
1512793fc096SFrank Li 	}
15137355f276STroy Kisky 	rxq->bd.cur = bdp;
1514793fc096SFrank Li 	return pkt_received;
1515793fc096SFrank Li }
1516793fc096SFrank Li 
15174d494cdcSFugang Duan static int
15184d494cdcSFugang Duan fec_enet_rx(struct net_device *ndev, int budget)
15194d494cdcSFugang Duan {
15204d494cdcSFugang Duan 	int     pkt_received = 0;
15214d494cdcSFugang Duan 	u16	queue_id;
15224d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
15234d494cdcSFugang Duan 
15244d494cdcSFugang Duan 	for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
15251c021bb7SUwe Kleine-König 		int ret;
15261c021bb7SUwe Kleine-König 
15271c021bb7SUwe Kleine-König 		ret = fec_enet_rx_queue(ndev,
15284d494cdcSFugang Duan 					budget - pkt_received, queue_id);
15291c021bb7SUwe Kleine-König 
15301c021bb7SUwe Kleine-König 		if (ret < budget - pkt_received)
15311c021bb7SUwe Kleine-König 			clear_bit(queue_id, &fep->work_rx);
15321c021bb7SUwe Kleine-König 
15331c021bb7SUwe Kleine-König 		pkt_received += ret;
15344d494cdcSFugang Duan 	}
15354d494cdcSFugang Duan 	return pkt_received;
15364d494cdcSFugang Duan }
15374d494cdcSFugang Duan 
15384d494cdcSFugang Duan static bool
15394d494cdcSFugang Duan fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
15404d494cdcSFugang Duan {
15414d494cdcSFugang Duan 	if (int_events == 0)
15424d494cdcSFugang Duan 		return false;
15434d494cdcSFugang Duan 
15444d494cdcSFugang Duan 	if (int_events & FEC_ENET_RXF)
15454d494cdcSFugang Duan 		fep->work_rx |= (1 << 2);
1546ce99d0d3SFrank Li 	if (int_events & FEC_ENET_RXF_1)
1547ce99d0d3SFrank Li 		fep->work_rx |= (1 << 0);
1548ce99d0d3SFrank Li 	if (int_events & FEC_ENET_RXF_2)
1549ce99d0d3SFrank Li 		fep->work_rx |= (1 << 1);
15504d494cdcSFugang Duan 
15514d494cdcSFugang Duan 	if (int_events & FEC_ENET_TXF)
15524d494cdcSFugang Duan 		fep->work_tx |= (1 << 2);
1553ce99d0d3SFrank Li 	if (int_events & FEC_ENET_TXF_1)
1554ce99d0d3SFrank Li 		fep->work_tx |= (1 << 0);
1555ce99d0d3SFrank Li 	if (int_events & FEC_ENET_TXF_2)
1556ce99d0d3SFrank Li 		fep->work_tx |= (1 << 1);
15574d494cdcSFugang Duan 
15584d494cdcSFugang Duan 	return true;
15594d494cdcSFugang Duan }
15604d494cdcSFugang Duan 
1561793fc096SFrank Li static irqreturn_t
1562793fc096SFrank Li fec_enet_interrupt(int irq, void *dev_id)
1563793fc096SFrank Li {
1564793fc096SFrank Li 	struct net_device *ndev = dev_id;
1565793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1566793fc096SFrank Li 	uint int_events;
1567793fc096SFrank Li 	irqreturn_t ret = IRQ_NONE;
1568793fc096SFrank Li 
1569793fc096SFrank Li 	int_events = readl(fep->hwp + FEC_IEVENT);
157094191fd6SNimrod Andy 	writel(int_events, fep->hwp + FEC_IEVENT);
15714d494cdcSFugang Duan 	fec_enet_collect_events(fep, int_events);
1572793fc096SFrank Li 
157361615cd2SFugang Duan 	if ((fep->work_tx || fep->work_rx) && fep->link) {
1574793fc096SFrank Li 		ret = IRQ_HANDLED;
1575793fc096SFrank Li 
157694191fd6SNimrod Andy 		if (napi_schedule_prep(&fep->napi)) {
15777a16807cSRussell King 			/* Disable the NAPI interrupts */
157880dc6a9fSTroy Kisky 			writel(FEC_NAPI_IMASK, fep->hwp + FEC_IMASK);
157994191fd6SNimrod Andy 			__napi_schedule(&fep->napi);
158094191fd6SNimrod Andy 		}
1581793fc096SFrank Li 	}
1582793fc096SFrank Li 
1583793fc096SFrank Li 	if (int_events & FEC_ENET_MII) {
1584793fc096SFrank Li 		ret = IRQ_HANDLED;
1585793fc096SFrank Li 		complete(&fep->mdio_done);
1586793fc096SFrank Li 	}
1587793fc096SFrank Li 
158881f35ffdSPhilipp Zabel 	if (fep->ptp_clock)
1589278d2404SLuwei Zhou 		fec_ptp_check_pps_event(fep);
1590278d2404SLuwei Zhou 
1591793fc096SFrank Li 	return ret;
1592793fc096SFrank Li }
1593793fc096SFrank Li 
1594793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1595793fc096SFrank Li {
1596793fc096SFrank Li 	struct net_device *ndev = napi->dev;
1597793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
15987a16807cSRussell King 	int pkts;
15997a16807cSRussell King 
16007a16807cSRussell King 	pkts = fec_enet_rx(ndev, budget);
1601793fc096SFrank Li 
1602793fc096SFrank Li 	fec_enet_tx(ndev);
1603793fc096SFrank Li 
1604793fc096SFrank Li 	if (pkts < budget) {
1605793fc096SFrank Li 		napi_complete(napi);
1606793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1607793fc096SFrank Li 	}
1608793fc096SFrank Li 	return pkts;
1609793fc096SFrank Li }
1610793fc096SFrank Li 
1611793fc096SFrank Li /* ------------------------------------------------------------------------- */
1612793fc096SFrank Li static void fec_get_mac(struct net_device *ndev)
1613793fc096SFrank Li {
1614793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
161594660ba0SJingoo Han 	struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1616793fc096SFrank Li 	unsigned char *iap, tmpaddr[ETH_ALEN];
1617793fc096SFrank Li 
1618793fc096SFrank Li 	/*
1619793fc096SFrank Li 	 * try to get mac address in following order:
1620793fc096SFrank Li 	 *
1621793fc096SFrank Li 	 * 1) module parameter via kernel command line in form
1622793fc096SFrank Li 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1623793fc096SFrank Li 	 */
1624793fc096SFrank Li 	iap = macaddr;
1625793fc096SFrank Li 
1626793fc096SFrank Li 	/*
1627793fc096SFrank Li 	 * 2) from device tree data
1628793fc096SFrank Li 	 */
1629793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1630793fc096SFrank Li 		struct device_node *np = fep->pdev->dev.of_node;
1631793fc096SFrank Li 		if (np) {
1632793fc096SFrank Li 			const char *mac = of_get_mac_address(np);
1633793fc096SFrank Li 			if (mac)
1634793fc096SFrank Li 				iap = (unsigned char *) mac;
1635793fc096SFrank Li 		}
1636793fc096SFrank Li 	}
1637793fc096SFrank Li 
1638793fc096SFrank Li 	/*
1639793fc096SFrank Li 	 * 3) from flash or fuse (via platform data)
1640793fc096SFrank Li 	 */
1641793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1642793fc096SFrank Li #ifdef CONFIG_M5272
1643793fc096SFrank Li 		if (FEC_FLASHMAC)
1644793fc096SFrank Li 			iap = (unsigned char *)FEC_FLASHMAC;
1645793fc096SFrank Li #else
1646793fc096SFrank Li 		if (pdata)
1647793fc096SFrank Li 			iap = (unsigned char *)&pdata->mac;
1648793fc096SFrank Li #endif
1649793fc096SFrank Li 	}
1650793fc096SFrank Li 
1651793fc096SFrank Li 	/*
1652793fc096SFrank Li 	 * 4) FEC mac registers set by bootloader
1653793fc096SFrank Li 	 */
1654793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
16557d7628f3SDan Carpenter 		*((__be32 *) &tmpaddr[0]) =
16567d7628f3SDan Carpenter 			cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
16577d7628f3SDan Carpenter 		*((__be16 *) &tmpaddr[4]) =
16587d7628f3SDan Carpenter 			cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1659793fc096SFrank Li 		iap = &tmpaddr[0];
1660793fc096SFrank Li 	}
1661793fc096SFrank Li 
1662ff5b2fabSLucas Stach 	/*
1663ff5b2fabSLucas Stach 	 * 5) random mac address
1664ff5b2fabSLucas Stach 	 */
1665ff5b2fabSLucas Stach 	if (!is_valid_ether_addr(iap)) {
1666ff5b2fabSLucas Stach 		/* Report it and use a random ethernet address instead */
1667ff5b2fabSLucas Stach 		netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1668ff5b2fabSLucas Stach 		eth_hw_addr_random(ndev);
1669ff5b2fabSLucas Stach 		netdev_info(ndev, "Using random MAC address: %pM\n",
1670ff5b2fabSLucas Stach 			    ndev->dev_addr);
1671ff5b2fabSLucas Stach 		return;
1672ff5b2fabSLucas Stach 	}
1673ff5b2fabSLucas Stach 
1674793fc096SFrank Li 	memcpy(ndev->dev_addr, iap, ETH_ALEN);
1675793fc096SFrank Li 
1676793fc096SFrank Li 	/* Adjust MAC if using macaddr */
1677793fc096SFrank Li 	if (iap == macaddr)
1678793fc096SFrank Li 		 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1679793fc096SFrank Li }
1680793fc096SFrank Li 
1681793fc096SFrank Li /* ------------------------------------------------------------------------- */
1682793fc096SFrank Li 
1683793fc096SFrank Li /*
1684793fc096SFrank Li  * Phy section
1685793fc096SFrank Li  */
1686793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev)
1687793fc096SFrank Li {
1688793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
168945f5c327SPhilippe Reynes 	struct phy_device *phy_dev = ndev->phydev;
1690793fc096SFrank Li 	int status_change = 0;
1691793fc096SFrank Li 
1692793fc096SFrank Li 	/* Prevent a state halted on mii error */
1693793fc096SFrank Li 	if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1694793fc096SFrank Li 		phy_dev->state = PHY_RESUMING;
169554309fa6SFrank Li 		return;
1696793fc096SFrank Li 	}
1697793fc096SFrank Li 
16988ce5624fSRussell King 	/*
16998ce5624fSRussell King 	 * If the netdev is down, or is going down, we're not interested
17008ce5624fSRussell King 	 * in link state events, so just mark our idea of the link as down
17018ce5624fSRussell King 	 * and ignore the event.
17028ce5624fSRussell King 	 */
17038ce5624fSRussell King 	if (!netif_running(ndev) || !netif_device_present(ndev)) {
17048ce5624fSRussell King 		fep->link = 0;
17058ce5624fSRussell King 	} else if (phy_dev->link) {
1706793fc096SFrank Li 		if (!fep->link) {
1707793fc096SFrank Li 			fep->link = phy_dev->link;
1708793fc096SFrank Li 			status_change = 1;
1709793fc096SFrank Li 		}
1710793fc096SFrank Li 
1711ef83337dSRussell King 		if (fep->full_duplex != phy_dev->duplex) {
1712ef83337dSRussell King 			fep->full_duplex = phy_dev->duplex;
1713793fc096SFrank Li 			status_change = 1;
1714ef83337dSRussell King 		}
1715793fc096SFrank Li 
1716793fc096SFrank Li 		if (phy_dev->speed != fep->speed) {
1717793fc096SFrank Li 			fep->speed = phy_dev->speed;
1718793fc096SFrank Li 			status_change = 1;
1719793fc096SFrank Li 		}
1720793fc096SFrank Li 
1721793fc096SFrank Li 		/* if any of the above changed restart the FEC */
1722dbc64a8eSRussell King 		if (status_change) {
1723dbc64a8eSRussell King 			napi_disable(&fep->napi);
1724dbc64a8eSRussell King 			netif_tx_lock_bh(ndev);
1725ef83337dSRussell King 			fec_restart(ndev);
1726dbc64a8eSRussell King 			netif_wake_queue(ndev);
17276af42d42SRussell King 			netif_tx_unlock_bh(ndev);
1728dbc64a8eSRussell King 			napi_enable(&fep->napi);
1729dbc64a8eSRussell King 		}
1730793fc096SFrank Li 	} else {
1731793fc096SFrank Li 		if (fep->link) {
1732f208ce10SRussell King 			napi_disable(&fep->napi);
1733f208ce10SRussell King 			netif_tx_lock_bh(ndev);
1734793fc096SFrank Li 			fec_stop(ndev);
1735f208ce10SRussell King 			netif_tx_unlock_bh(ndev);
1736f208ce10SRussell King 			napi_enable(&fep->napi);
17376e0895c2SDavid S. Miller 			fep->link = phy_dev->link;
1738793fc096SFrank Li 			status_change = 1;
1739793fc096SFrank Li 		}
1740793fc096SFrank Li 	}
1741793fc096SFrank Li 
1742793fc096SFrank Li 	if (status_change)
1743793fc096SFrank Li 		phy_print_status(phy_dev);
1744793fc096SFrank Li }
1745793fc096SFrank Li 
1746793fc096SFrank Li static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1747793fc096SFrank Li {
1748793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
17498fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
1750793fc096SFrank Li 	unsigned long time_left;
17518fff755eSAndrew Lunn 	int ret = 0;
17528fff755eSAndrew Lunn 
17538fff755eSAndrew Lunn 	ret = pm_runtime_get_sync(dev);
1754b0c6ce24SFabio Estevam 	if (ret < 0)
17558fff755eSAndrew Lunn 		return ret;
1756793fc096SFrank Li 
1757793fc096SFrank Li 	fep->mii_timeout = 0;
1758aac27c7aSRussell King 	reinit_completion(&fep->mdio_done);
1759793fc096SFrank Li 
1760793fc096SFrank Li 	/* start a read op */
1761793fc096SFrank Li 	writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1762793fc096SFrank Li 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1763793fc096SFrank Li 		FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1764793fc096SFrank Li 
1765793fc096SFrank Li 	/* wait for end of transfer */
1766793fc096SFrank Li 	time_left = wait_for_completion_timeout(&fep->mdio_done,
1767793fc096SFrank Li 			usecs_to_jiffies(FEC_MII_TIMEOUT));
1768793fc096SFrank Li 	if (time_left == 0) {
1769793fc096SFrank Li 		fep->mii_timeout = 1;
177031b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO read timeout\n");
17718fff755eSAndrew Lunn 		ret = -ETIMEDOUT;
17728fff755eSAndrew Lunn 		goto out;
1773793fc096SFrank Li 	}
1774793fc096SFrank Li 
17758fff755eSAndrew Lunn 	ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
17768fff755eSAndrew Lunn 
17778fff755eSAndrew Lunn out:
17788fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
17798fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
17808fff755eSAndrew Lunn 
17818fff755eSAndrew Lunn 	return ret;
1782793fc096SFrank Li }
1783793fc096SFrank Li 
1784793fc096SFrank Li static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1785793fc096SFrank Li 			   u16 value)
1786793fc096SFrank Li {
1787793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
17888fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
1789793fc096SFrank Li 	unsigned long time_left;
179042ea4457SMaciej S. Szmigiero 	int ret;
17918fff755eSAndrew Lunn 
17928fff755eSAndrew Lunn 	ret = pm_runtime_get_sync(dev);
1793b0c6ce24SFabio Estevam 	if (ret < 0)
17948fff755eSAndrew Lunn 		return ret;
179542ea4457SMaciej S. Szmigiero 	else
179642ea4457SMaciej S. Szmigiero 		ret = 0;
1797793fc096SFrank Li 
1798793fc096SFrank Li 	fep->mii_timeout = 0;
1799aac27c7aSRussell King 	reinit_completion(&fep->mdio_done);
1800793fc096SFrank Li 
1801793fc096SFrank Li 	/* start a write op */
1802793fc096SFrank Li 	writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1803793fc096SFrank Li 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1804793fc096SFrank Li 		FEC_MMFR_TA | FEC_MMFR_DATA(value),
1805793fc096SFrank Li 		fep->hwp + FEC_MII_DATA);
1806793fc096SFrank Li 
1807793fc096SFrank Li 	/* wait for end of transfer */
1808793fc096SFrank Li 	time_left = wait_for_completion_timeout(&fep->mdio_done,
1809793fc096SFrank Li 			usecs_to_jiffies(FEC_MII_TIMEOUT));
1810793fc096SFrank Li 	if (time_left == 0) {
1811793fc096SFrank Li 		fep->mii_timeout = 1;
181231b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO write timeout\n");
18138fff755eSAndrew Lunn 		ret  = -ETIMEDOUT;
1814793fc096SFrank Li 	}
1815793fc096SFrank Li 
18168fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
18178fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
18188fff755eSAndrew Lunn 
18198fff755eSAndrew Lunn 	return ret;
1820793fc096SFrank Li }
1821793fc096SFrank Li 
1822e8fcfcd5SNimrod Andy static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1823e8fcfcd5SNimrod Andy {
1824e8fcfcd5SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
1825e8fcfcd5SNimrod Andy 	int ret;
1826e8fcfcd5SNimrod Andy 
1827e8fcfcd5SNimrod Andy 	if (enable) {
1828e8fcfcd5SNimrod Andy 		ret = clk_prepare_enable(fep->clk_ahb);
1829e8fcfcd5SNimrod Andy 		if (ret)
1830e8fcfcd5SNimrod Andy 			return ret;
1831e8fcfcd5SNimrod Andy 		if (fep->clk_enet_out) {
1832e8fcfcd5SNimrod Andy 			ret = clk_prepare_enable(fep->clk_enet_out);
1833e8fcfcd5SNimrod Andy 			if (ret)
1834e8fcfcd5SNimrod Andy 				goto failed_clk_enet_out;
1835e8fcfcd5SNimrod Andy 		}
1836e8fcfcd5SNimrod Andy 		if (fep->clk_ptp) {
183791c0d987SNimrod Andy 			mutex_lock(&fep->ptp_clk_mutex);
1838e8fcfcd5SNimrod Andy 			ret = clk_prepare_enable(fep->clk_ptp);
183991c0d987SNimrod Andy 			if (ret) {
184091c0d987SNimrod Andy 				mutex_unlock(&fep->ptp_clk_mutex);
1841e8fcfcd5SNimrod Andy 				goto failed_clk_ptp;
184291c0d987SNimrod Andy 			} else {
184391c0d987SNimrod Andy 				fep->ptp_clk_on = true;
184491c0d987SNimrod Andy 			}
184591c0d987SNimrod Andy 			mutex_unlock(&fep->ptp_clk_mutex);
1846e8fcfcd5SNimrod Andy 		}
18479b5330edSFugang Duan 		if (fep->clk_ref) {
18489b5330edSFugang Duan 			ret = clk_prepare_enable(fep->clk_ref);
18499b5330edSFugang Duan 			if (ret)
18509b5330edSFugang Duan 				goto failed_clk_ref;
18519b5330edSFugang Duan 		}
1852e8fcfcd5SNimrod Andy 	} else {
1853e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_ahb);
1854e8fcfcd5SNimrod Andy 		if (fep->clk_enet_out)
1855e8fcfcd5SNimrod Andy 			clk_disable_unprepare(fep->clk_enet_out);
185691c0d987SNimrod Andy 		if (fep->clk_ptp) {
185791c0d987SNimrod Andy 			mutex_lock(&fep->ptp_clk_mutex);
1858e8fcfcd5SNimrod Andy 			clk_disable_unprepare(fep->clk_ptp);
185991c0d987SNimrod Andy 			fep->ptp_clk_on = false;
186091c0d987SNimrod Andy 			mutex_unlock(&fep->ptp_clk_mutex);
186191c0d987SNimrod Andy 		}
18629b5330edSFugang Duan 		if (fep->clk_ref)
18639b5330edSFugang Duan 			clk_disable_unprepare(fep->clk_ref);
1864e8fcfcd5SNimrod Andy 	}
1865e8fcfcd5SNimrod Andy 
1866e8fcfcd5SNimrod Andy 	return 0;
18679b5330edSFugang Duan 
18689b5330edSFugang Duan failed_clk_ref:
18699b5330edSFugang Duan 	if (fep->clk_ref)
18709b5330edSFugang Duan 		clk_disable_unprepare(fep->clk_ref);
1871e8fcfcd5SNimrod Andy failed_clk_ptp:
1872e8fcfcd5SNimrod Andy 	if (fep->clk_enet_out)
1873e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_enet_out);
1874e8fcfcd5SNimrod Andy failed_clk_enet_out:
1875e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_ahb);
1876e8fcfcd5SNimrod Andy 
1877e8fcfcd5SNimrod Andy 	return ret;
1878e8fcfcd5SNimrod Andy }
1879e8fcfcd5SNimrod Andy 
1880793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev)
1881793fc096SFrank Li {
1882793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1883793fc096SFrank Li 	struct phy_device *phy_dev = NULL;
1884793fc096SFrank Li 	char mdio_bus_id[MII_BUS_ID_SIZE];
1885793fc096SFrank Li 	char phy_name[MII_BUS_ID_SIZE + 3];
1886793fc096SFrank Li 	int phy_id;
1887793fc096SFrank Li 	int dev_id = fep->dev_id;
1888793fc096SFrank Li 
1889407066f8SUwe Kleine-König 	if (fep->phy_node) {
1890407066f8SUwe Kleine-König 		phy_dev = of_phy_connect(ndev, fep->phy_node,
1891407066f8SUwe Kleine-König 					 &fec_enet_adjust_link, 0,
1892407066f8SUwe Kleine-König 					 fep->phy_interface);
1893213a9922SNimrod Andy 		if (!phy_dev)
1894213a9922SNimrod Andy 			return -ENODEV;
1895407066f8SUwe Kleine-König 	} else {
1896793fc096SFrank Li 		/* check for attached phy */
1897793fc096SFrank Li 		for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
18987f854420SAndrew Lunn 			if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
1899793fc096SFrank Li 				continue;
1900793fc096SFrank Li 			if (dev_id--)
1901793fc096SFrank Li 				continue;
1902949bdd20SRickard Strandqvist 			strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1903793fc096SFrank Li 			break;
1904793fc096SFrank Li 		}
1905793fc096SFrank Li 
1906793fc096SFrank Li 		if (phy_id >= PHY_MAX_ADDR) {
190731b7720cSJoe Perches 			netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1908949bdd20SRickard Strandqvist 			strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1909793fc096SFrank Li 			phy_id = 0;
1910793fc096SFrank Li 		}
1911793fc096SFrank Li 
1912407066f8SUwe Kleine-König 		snprintf(phy_name, sizeof(phy_name),
1913407066f8SUwe Kleine-König 			 PHY_ID_FMT, mdio_bus_id, phy_id);
1914793fc096SFrank Li 		phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1915793fc096SFrank Li 				      fep->phy_interface);
1916407066f8SUwe Kleine-König 	}
1917407066f8SUwe Kleine-König 
1918793fc096SFrank Li 	if (IS_ERR(phy_dev)) {
191931b7720cSJoe Perches 		netdev_err(ndev, "could not attach to PHY\n");
1920793fc096SFrank Li 		return PTR_ERR(phy_dev);
1921793fc096SFrank Li 	}
1922793fc096SFrank Li 
1923793fc096SFrank Li 	/* mask with MAC supported features */
19246b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
1925793fc096SFrank Li 		phy_dev->supported &= PHY_GBIT_FEATURES;
1926b44592ffSRussell King 		phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
1927d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1928793fc096SFrank Li 		phy_dev->supported |= SUPPORTED_Pause;
1929d1391930SGuenter Roeck #endif
1930793fc096SFrank Li 	}
1931793fc096SFrank Li 	else
1932793fc096SFrank Li 		phy_dev->supported &= PHY_BASIC_FEATURES;
1933793fc096SFrank Li 
1934793fc096SFrank Li 	phy_dev->advertising = phy_dev->supported;
1935793fc096SFrank Li 
1936793fc096SFrank Li 	fep->link = 0;
1937793fc096SFrank Li 	fep->full_duplex = 0;
1938793fc096SFrank Li 
19392220943aSAndrew Lunn 	phy_attached_info(phy_dev);
1940793fc096SFrank Li 
1941793fc096SFrank Li 	return 0;
1942793fc096SFrank Li }
1943793fc096SFrank Li 
1944793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev)
1945793fc096SFrank Li {
1946793fc096SFrank Li 	static struct mii_bus *fec0_mii_bus;
1947793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
1948793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1949407066f8SUwe Kleine-König 	struct device_node *node;
1950e7f4dc35SAndrew Lunn 	int err = -ENXIO;
195163c60732SUwe Kleine-König 	u32 mii_speed, holdtime;
1952793fc096SFrank Li 
1953793fc096SFrank Li 	/*
19543d125f9cSStefan Agner 	 * The i.MX28 dual fec interfaces are not equal.
1955793fc096SFrank Li 	 * Here are the differences:
1956793fc096SFrank Li 	 *
1957793fc096SFrank Li 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
1958793fc096SFrank Li 	 *  - fec0 acts as the 1588 time master while fec1 is slave
1959793fc096SFrank Li 	 *  - external phys can only be configured by fec0
1960793fc096SFrank Li 	 *
1961793fc096SFrank Li 	 * That is to say fec1 can not work independently. It only works
1962793fc096SFrank Li 	 * when fec0 is working. The reason behind this design is that the
1963793fc096SFrank Li 	 * second interface is added primarily for Switch mode.
1964793fc096SFrank Li 	 *
1965793fc096SFrank Li 	 * Because of the last point above, both phys are attached on fec0
1966793fc096SFrank Li 	 * mdio interface in board design, and need to be configured by
1967793fc096SFrank Li 	 * fec0 mii_bus.
1968793fc096SFrank Li 	 */
19693d125f9cSStefan Agner 	if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
1970793fc096SFrank Li 		/* fec1 uses fec0 mii_bus */
1971793fc096SFrank Li 		if (mii_cnt && fec0_mii_bus) {
1972793fc096SFrank Li 			fep->mii_bus = fec0_mii_bus;
1973793fc096SFrank Li 			mii_cnt++;
1974793fc096SFrank Li 			return 0;
1975793fc096SFrank Li 		}
1976793fc096SFrank Li 		return -ENOENT;
1977793fc096SFrank Li 	}
1978793fc096SFrank Li 
1979793fc096SFrank Li 	fep->mii_timeout = 0;
1980793fc096SFrank Li 
1981793fc096SFrank Li 	/*
1982793fc096SFrank Li 	 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1983793fc096SFrank Li 	 *
1984793fc096SFrank Li 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1985793fc096SFrank Li 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
1986793fc096SFrank Li 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1987793fc096SFrank Li 	 * document.
1988793fc096SFrank Li 	 */
198963c60732SUwe Kleine-König 	mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
19906b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC)
199163c60732SUwe Kleine-König 		mii_speed--;
199263c60732SUwe Kleine-König 	if (mii_speed > 63) {
199363c60732SUwe Kleine-König 		dev_err(&pdev->dev,
199463c60732SUwe Kleine-König 			"fec clock (%lu) to fast to get right mii speed\n",
199563c60732SUwe Kleine-König 			clk_get_rate(fep->clk_ipg));
199663c60732SUwe Kleine-König 		err = -EINVAL;
199763c60732SUwe Kleine-König 		goto err_out;
199863c60732SUwe Kleine-König 	}
199963c60732SUwe Kleine-König 
200063c60732SUwe Kleine-König 	/*
200163c60732SUwe Kleine-König 	 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
200263c60732SUwe Kleine-König 	 * MII_SPEED) register that defines the MDIO output hold time. Earlier
200363c60732SUwe Kleine-König 	 * versions are RAZ there, so just ignore the difference and write the
200463c60732SUwe Kleine-König 	 * register always.
200563c60732SUwe Kleine-König 	 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
200663c60732SUwe Kleine-König 	 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
200763c60732SUwe Kleine-König 	 * output.
200863c60732SUwe Kleine-König 	 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
200963c60732SUwe Kleine-König 	 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
201063c60732SUwe Kleine-König 	 * holdtime cannot result in a value greater than 3.
201163c60732SUwe Kleine-König 	 */
201263c60732SUwe Kleine-König 	holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
201363c60732SUwe Kleine-König 
201463c60732SUwe Kleine-König 	fep->phy_speed = mii_speed << 1 | holdtime << 8;
201563c60732SUwe Kleine-König 
2016793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2017793fc096SFrank Li 
2018793fc096SFrank Li 	fep->mii_bus = mdiobus_alloc();
2019793fc096SFrank Li 	if (fep->mii_bus == NULL) {
2020793fc096SFrank Li 		err = -ENOMEM;
2021793fc096SFrank Li 		goto err_out;
2022793fc096SFrank Li 	}
2023793fc096SFrank Li 
2024793fc096SFrank Li 	fep->mii_bus->name = "fec_enet_mii_bus";
2025793fc096SFrank Li 	fep->mii_bus->read = fec_enet_mdio_read;
2026793fc096SFrank Li 	fep->mii_bus->write = fec_enet_mdio_write;
2027793fc096SFrank Li 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2028793fc096SFrank Li 		pdev->name, fep->dev_id + 1);
2029793fc096SFrank Li 	fep->mii_bus->priv = fep;
2030793fc096SFrank Li 	fep->mii_bus->parent = &pdev->dev;
2031793fc096SFrank Li 
2032407066f8SUwe Kleine-König 	node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2033407066f8SUwe Kleine-König 	if (node) {
2034407066f8SUwe Kleine-König 		err = of_mdiobus_register(fep->mii_bus, node);
2035407066f8SUwe Kleine-König 		of_node_put(node);
2036407066f8SUwe Kleine-König 	} else {
2037407066f8SUwe Kleine-König 		err = mdiobus_register(fep->mii_bus);
2038407066f8SUwe Kleine-König 	}
2039407066f8SUwe Kleine-König 
2040407066f8SUwe Kleine-König 	if (err)
2041e7f4dc35SAndrew Lunn 		goto err_out_free_mdiobus;
2042793fc096SFrank Li 
2043793fc096SFrank Li 	mii_cnt++;
2044793fc096SFrank Li 
2045793fc096SFrank Li 	/* save fec0 mii_bus */
20463d125f9cSStefan Agner 	if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2047793fc096SFrank Li 		fec0_mii_bus = fep->mii_bus;
2048793fc096SFrank Li 
2049793fc096SFrank Li 	return 0;
2050793fc096SFrank Li 
2051793fc096SFrank Li err_out_free_mdiobus:
2052793fc096SFrank Li 	mdiobus_free(fep->mii_bus);
2053793fc096SFrank Li err_out:
2054793fc096SFrank Li 	return err;
2055793fc096SFrank Li }
2056793fc096SFrank Li 
2057793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep)
2058793fc096SFrank Li {
2059793fc096SFrank Li 	if (--mii_cnt == 0) {
2060793fc096SFrank Li 		mdiobus_unregister(fep->mii_bus);
2061793fc096SFrank Li 		mdiobus_free(fep->mii_bus);
2062793fc096SFrank Li 	}
2063793fc096SFrank Li }
2064793fc096SFrank Li 
2065793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev,
2066793fc096SFrank Li 				 struct ethtool_drvinfo *info)
2067793fc096SFrank Li {
2068793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2069793fc096SFrank Li 
2070793fc096SFrank Li 	strlcpy(info->driver, fep->pdev->dev.driver->name,
2071793fc096SFrank Li 		sizeof(info->driver));
2072793fc096SFrank Li 	strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2073793fc096SFrank Li 	strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2074793fc096SFrank Li }
2075793fc096SFrank Li 
2076db65f35fSPhilippe Reynes static int fec_enet_get_regs_len(struct net_device *ndev)
2077db65f35fSPhilippe Reynes {
2078db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2079db65f35fSPhilippe Reynes 	struct resource *r;
2080db65f35fSPhilippe Reynes 	int s = 0;
2081db65f35fSPhilippe Reynes 
2082db65f35fSPhilippe Reynes 	r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2083db65f35fSPhilippe Reynes 	if (r)
2084db65f35fSPhilippe Reynes 		s = resource_size(r);
2085db65f35fSPhilippe Reynes 
2086db65f35fSPhilippe Reynes 	return s;
2087db65f35fSPhilippe Reynes }
2088db65f35fSPhilippe Reynes 
2089db65f35fSPhilippe Reynes /* List of registers that can be safety be read to dump them with ethtool */
2090db65f35fSPhilippe Reynes #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
209105f3b50eSJohannes Berg 	defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
2092db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2093db65f35fSPhilippe Reynes 	FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2094db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2095db65f35fSPhilippe Reynes 	FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2096db65f35fSPhilippe Reynes 	FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2097db65f35fSPhilippe Reynes 	FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2098db65f35fSPhilippe Reynes 	FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2099db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2100db65f35fSPhilippe Reynes 	FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2101db65f35fSPhilippe Reynes 	FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2102db65f35fSPhilippe Reynes 	FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2103db65f35fSPhilippe Reynes 	FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2104db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2105db65f35fSPhilippe Reynes 	RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2106db65f35fSPhilippe Reynes 	RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2107db65f35fSPhilippe Reynes 	RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2108db65f35fSPhilippe Reynes 	RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2109db65f35fSPhilippe Reynes 	RMON_T_P_GTE2048, RMON_T_OCTETS,
2110db65f35fSPhilippe Reynes 	IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2111db65f35fSPhilippe Reynes 	IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2112db65f35fSPhilippe Reynes 	IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2113db65f35fSPhilippe Reynes 	RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2114db65f35fSPhilippe Reynes 	RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2115db65f35fSPhilippe Reynes 	RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2116db65f35fSPhilippe Reynes 	RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2117db65f35fSPhilippe Reynes 	RMON_R_P_GTE2048, RMON_R_OCTETS,
2118db65f35fSPhilippe Reynes 	IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2119db65f35fSPhilippe Reynes 	IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2120db65f35fSPhilippe Reynes };
2121db65f35fSPhilippe Reynes #else
2122db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2123db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2124db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2125db65f35fSPhilippe Reynes 	FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2126db65f35fSPhilippe Reynes 	FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2127db65f35fSPhilippe Reynes 	FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2128db65f35fSPhilippe Reynes 	FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2129db65f35fSPhilippe Reynes 	FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2130db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2131db65f35fSPhilippe Reynes 	FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2132db65f35fSPhilippe Reynes };
2133db65f35fSPhilippe Reynes #endif
2134db65f35fSPhilippe Reynes 
2135db65f35fSPhilippe Reynes static void fec_enet_get_regs(struct net_device *ndev,
2136db65f35fSPhilippe Reynes 			      struct ethtool_regs *regs, void *regbuf)
2137db65f35fSPhilippe Reynes {
2138db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2139db65f35fSPhilippe Reynes 	u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2140db65f35fSPhilippe Reynes 	u32 *buf = (u32 *)regbuf;
2141db65f35fSPhilippe Reynes 	u32 i, off;
2142db65f35fSPhilippe Reynes 
2143db65f35fSPhilippe Reynes 	memset(buf, 0, regs->len);
2144db65f35fSPhilippe Reynes 
2145db65f35fSPhilippe Reynes 	for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2146db65f35fSPhilippe Reynes 		off = fec_enet_register_offset[i] / 4;
2147db65f35fSPhilippe Reynes 		buf[off] = readl(&theregs[off]);
2148db65f35fSPhilippe Reynes 	}
2149db65f35fSPhilippe Reynes }
2150db65f35fSPhilippe Reynes 
2151793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev,
2152793fc096SFrank Li 				struct ethtool_ts_info *info)
2153793fc096SFrank Li {
2154793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2155793fc096SFrank Li 
2156793fc096SFrank Li 	if (fep->bufdesc_ex) {
2157793fc096SFrank Li 
2158793fc096SFrank Li 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2159793fc096SFrank Li 					SOF_TIMESTAMPING_RX_SOFTWARE |
2160793fc096SFrank Li 					SOF_TIMESTAMPING_SOFTWARE |
2161793fc096SFrank Li 					SOF_TIMESTAMPING_TX_HARDWARE |
2162793fc096SFrank Li 					SOF_TIMESTAMPING_RX_HARDWARE |
2163793fc096SFrank Li 					SOF_TIMESTAMPING_RAW_HARDWARE;
2164793fc096SFrank Li 		if (fep->ptp_clock)
2165793fc096SFrank Li 			info->phc_index = ptp_clock_index(fep->ptp_clock);
2166793fc096SFrank Li 		else
2167793fc096SFrank Li 			info->phc_index = -1;
2168793fc096SFrank Li 
2169793fc096SFrank Li 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2170793fc096SFrank Li 				 (1 << HWTSTAMP_TX_ON);
2171793fc096SFrank Li 
2172793fc096SFrank Li 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2173793fc096SFrank Li 				   (1 << HWTSTAMP_FILTER_ALL);
2174793fc096SFrank Li 		return 0;
2175793fc096SFrank Li 	} else {
2176793fc096SFrank Li 		return ethtool_op_get_ts_info(ndev, info);
2177793fc096SFrank Li 	}
2178793fc096SFrank Li }
2179793fc096SFrank Li 
2180d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2181d1391930SGuenter Roeck 
2182793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev,
2183793fc096SFrank Li 				    struct ethtool_pauseparam *pause)
2184793fc096SFrank Li {
2185793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2186793fc096SFrank Li 
2187793fc096SFrank Li 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2188793fc096SFrank Li 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2189793fc096SFrank Li 	pause->rx_pause = pause->tx_pause;
2190793fc096SFrank Li }
2191793fc096SFrank Li 
2192793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev,
2193793fc096SFrank Li 				   struct ethtool_pauseparam *pause)
2194793fc096SFrank Li {
2195793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2196793fc096SFrank Li 
219745f5c327SPhilippe Reynes 	if (!ndev->phydev)
21980b146ca8SRussell King 		return -ENODEV;
21990b146ca8SRussell King 
2200793fc096SFrank Li 	if (pause->tx_pause != pause->rx_pause) {
2201793fc096SFrank Li 		netdev_info(ndev,
2202793fc096SFrank Li 			"hardware only support enable/disable both tx and rx");
2203793fc096SFrank Li 		return -EINVAL;
2204793fc096SFrank Li 	}
2205793fc096SFrank Li 
2206793fc096SFrank Li 	fep->pause_flag = 0;
2207793fc096SFrank Li 
2208793fc096SFrank Li 	/* tx pause must be same as rx pause */
2209793fc096SFrank Li 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2210793fc096SFrank Li 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2211793fc096SFrank Li 
2212793fc096SFrank Li 	if (pause->rx_pause || pause->autoneg) {
221345f5c327SPhilippe Reynes 		ndev->phydev->supported |= ADVERTISED_Pause;
221445f5c327SPhilippe Reynes 		ndev->phydev->advertising |= ADVERTISED_Pause;
2215793fc096SFrank Li 	} else {
221645f5c327SPhilippe Reynes 		ndev->phydev->supported &= ~ADVERTISED_Pause;
221745f5c327SPhilippe Reynes 		ndev->phydev->advertising &= ~ADVERTISED_Pause;
2218793fc096SFrank Li 	}
2219793fc096SFrank Li 
2220793fc096SFrank Li 	if (pause->autoneg) {
2221793fc096SFrank Li 		if (netif_running(ndev))
2222793fc096SFrank Li 			fec_stop(ndev);
222345f5c327SPhilippe Reynes 		phy_start_aneg(ndev->phydev);
2224793fc096SFrank Li 	}
2225dbc64a8eSRussell King 	if (netif_running(ndev)) {
2226dbc64a8eSRussell King 		napi_disable(&fep->napi);
2227dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
2228ef83337dSRussell King 		fec_restart(ndev);
2229dbc64a8eSRussell King 		netif_wake_queue(ndev);
22306af42d42SRussell King 		netif_tx_unlock_bh(ndev);
2231dbc64a8eSRussell King 		napi_enable(&fep->napi);
2232dbc64a8eSRussell King 	}
2233793fc096SFrank Li 
2234793fc096SFrank Li 	return 0;
2235793fc096SFrank Li }
2236793fc096SFrank Li 
223738ae92dcSChris Healy static const struct fec_stat {
223838ae92dcSChris Healy 	char name[ETH_GSTRING_LEN];
223938ae92dcSChris Healy 	u16 offset;
224038ae92dcSChris Healy } fec_stats[] = {
224138ae92dcSChris Healy 	/* RMON TX */
224238ae92dcSChris Healy 	{ "tx_dropped", RMON_T_DROP },
224338ae92dcSChris Healy 	{ "tx_packets", RMON_T_PACKETS },
224438ae92dcSChris Healy 	{ "tx_broadcast", RMON_T_BC_PKT },
224538ae92dcSChris Healy 	{ "tx_multicast", RMON_T_MC_PKT },
224638ae92dcSChris Healy 	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
224738ae92dcSChris Healy 	{ "tx_undersize", RMON_T_UNDERSIZE },
224838ae92dcSChris Healy 	{ "tx_oversize", RMON_T_OVERSIZE },
224938ae92dcSChris Healy 	{ "tx_fragment", RMON_T_FRAG },
225038ae92dcSChris Healy 	{ "tx_jabber", RMON_T_JAB },
225138ae92dcSChris Healy 	{ "tx_collision", RMON_T_COL },
225238ae92dcSChris Healy 	{ "tx_64byte", RMON_T_P64 },
225338ae92dcSChris Healy 	{ "tx_65to127byte", RMON_T_P65TO127 },
225438ae92dcSChris Healy 	{ "tx_128to255byte", RMON_T_P128TO255 },
225538ae92dcSChris Healy 	{ "tx_256to511byte", RMON_T_P256TO511 },
225638ae92dcSChris Healy 	{ "tx_512to1023byte", RMON_T_P512TO1023 },
225738ae92dcSChris Healy 	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
225838ae92dcSChris Healy 	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
225938ae92dcSChris Healy 	{ "tx_octets", RMON_T_OCTETS },
226038ae92dcSChris Healy 
226138ae92dcSChris Healy 	/* IEEE TX */
226238ae92dcSChris Healy 	{ "IEEE_tx_drop", IEEE_T_DROP },
226338ae92dcSChris Healy 	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
226438ae92dcSChris Healy 	{ "IEEE_tx_1col", IEEE_T_1COL },
226538ae92dcSChris Healy 	{ "IEEE_tx_mcol", IEEE_T_MCOL },
226638ae92dcSChris Healy 	{ "IEEE_tx_def", IEEE_T_DEF },
226738ae92dcSChris Healy 	{ "IEEE_tx_lcol", IEEE_T_LCOL },
226838ae92dcSChris Healy 	{ "IEEE_tx_excol", IEEE_T_EXCOL },
226938ae92dcSChris Healy 	{ "IEEE_tx_macerr", IEEE_T_MACERR },
227038ae92dcSChris Healy 	{ "IEEE_tx_cserr", IEEE_T_CSERR },
227138ae92dcSChris Healy 	{ "IEEE_tx_sqe", IEEE_T_SQE },
227238ae92dcSChris Healy 	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
227338ae92dcSChris Healy 	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
227438ae92dcSChris Healy 
227538ae92dcSChris Healy 	/* RMON RX */
227638ae92dcSChris Healy 	{ "rx_packets", RMON_R_PACKETS },
227738ae92dcSChris Healy 	{ "rx_broadcast", RMON_R_BC_PKT },
227838ae92dcSChris Healy 	{ "rx_multicast", RMON_R_MC_PKT },
227938ae92dcSChris Healy 	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
228038ae92dcSChris Healy 	{ "rx_undersize", RMON_R_UNDERSIZE },
228138ae92dcSChris Healy 	{ "rx_oversize", RMON_R_OVERSIZE },
228238ae92dcSChris Healy 	{ "rx_fragment", RMON_R_FRAG },
228338ae92dcSChris Healy 	{ "rx_jabber", RMON_R_JAB },
228438ae92dcSChris Healy 	{ "rx_64byte", RMON_R_P64 },
228538ae92dcSChris Healy 	{ "rx_65to127byte", RMON_R_P65TO127 },
228638ae92dcSChris Healy 	{ "rx_128to255byte", RMON_R_P128TO255 },
228738ae92dcSChris Healy 	{ "rx_256to511byte", RMON_R_P256TO511 },
228838ae92dcSChris Healy 	{ "rx_512to1023byte", RMON_R_P512TO1023 },
228938ae92dcSChris Healy 	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
229038ae92dcSChris Healy 	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
229138ae92dcSChris Healy 	{ "rx_octets", RMON_R_OCTETS },
229238ae92dcSChris Healy 
229338ae92dcSChris Healy 	/* IEEE RX */
229438ae92dcSChris Healy 	{ "IEEE_rx_drop", IEEE_R_DROP },
229538ae92dcSChris Healy 	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
229638ae92dcSChris Healy 	{ "IEEE_rx_crc", IEEE_R_CRC },
229738ae92dcSChris Healy 	{ "IEEE_rx_align", IEEE_R_ALIGN },
229838ae92dcSChris Healy 	{ "IEEE_rx_macerr", IEEE_R_MACERR },
229938ae92dcSChris Healy 	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
230038ae92dcSChris Healy 	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
230138ae92dcSChris Healy };
230238ae92dcSChris Healy 
230338ae92dcSChris Healy static void fec_enet_get_ethtool_stats(struct net_device *dev,
230438ae92dcSChris Healy 	struct ethtool_stats *stats, u64 *data)
230538ae92dcSChris Healy {
230638ae92dcSChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
230738ae92dcSChris Healy 	int i;
230838ae92dcSChris Healy 
230938ae92dcSChris Healy 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
231038ae92dcSChris Healy 		data[i] = readl(fep->hwp + fec_stats[i].offset);
231138ae92dcSChris Healy }
231238ae92dcSChris Healy 
231338ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev,
231438ae92dcSChris Healy 	u32 stringset, u8 *data)
231538ae92dcSChris Healy {
231638ae92dcSChris Healy 	int i;
231738ae92dcSChris Healy 	switch (stringset) {
231838ae92dcSChris Healy 	case ETH_SS_STATS:
231938ae92dcSChris Healy 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
232038ae92dcSChris Healy 			memcpy(data + i * ETH_GSTRING_LEN,
232138ae92dcSChris Healy 				fec_stats[i].name, ETH_GSTRING_LEN);
232238ae92dcSChris Healy 		break;
232338ae92dcSChris Healy 	}
232438ae92dcSChris Healy }
232538ae92dcSChris Healy 
232638ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset)
232738ae92dcSChris Healy {
232838ae92dcSChris Healy 	switch (sset) {
232938ae92dcSChris Healy 	case ETH_SS_STATS:
233038ae92dcSChris Healy 		return ARRAY_SIZE(fec_stats);
233138ae92dcSChris Healy 	default:
233238ae92dcSChris Healy 		return -EOPNOTSUPP;
233338ae92dcSChris Healy 	}
233438ae92dcSChris Healy }
2335d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
233638ae92dcSChris Healy 
233732bc9b46SChris Healy static int fec_enet_nway_reset(struct net_device *dev)
233832bc9b46SChris Healy {
233945f5c327SPhilippe Reynes 	struct phy_device *phydev = dev->phydev;
234032bc9b46SChris Healy 
234132bc9b46SChris Healy 	if (!phydev)
234232bc9b46SChris Healy 		return -ENODEV;
234332bc9b46SChris Healy 
234432bc9b46SChris Healy 	return genphy_restart_aneg(phydev);
234532bc9b46SChris Healy }
234632bc9b46SChris Healy 
2347d851b47bSFugang Duan /* ITR clock source is enet system clock (clk_ahb).
2348d851b47bSFugang Duan  * TCTT unit is cycle_ns * 64 cycle
2349d851b47bSFugang Duan  * So, the ICTT value = X us / (cycle_ns * 64)
2350d851b47bSFugang Duan  */
2351d851b47bSFugang Duan static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2352d851b47bSFugang Duan {
2353d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2354d851b47bSFugang Duan 
2355d851b47bSFugang Duan 	return us * (fep->itr_clk_rate / 64000) / 1000;
2356d851b47bSFugang Duan }
2357d851b47bSFugang Duan 
2358d851b47bSFugang Duan /* Set threshold for interrupt coalescing */
2359d851b47bSFugang Duan static void fec_enet_itr_coal_set(struct net_device *ndev)
2360d851b47bSFugang Duan {
2361d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2362d851b47bSFugang Duan 	int rx_itr, tx_itr;
2363d851b47bSFugang Duan 
23646b7e4008SLothar Waßmann 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2365d851b47bSFugang Duan 		return;
2366d851b47bSFugang Duan 
2367d851b47bSFugang Duan 	/* Must be greater than zero to avoid unpredictable behavior */
2368d851b47bSFugang Duan 	if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2369d851b47bSFugang Duan 	    !fep->tx_time_itr || !fep->tx_pkts_itr)
2370d851b47bSFugang Duan 		return;
2371d851b47bSFugang Duan 
2372d851b47bSFugang Duan 	/* Select enet system clock as Interrupt Coalescing
2373d851b47bSFugang Duan 	 * timer Clock Source
2374d851b47bSFugang Duan 	 */
2375d851b47bSFugang Duan 	rx_itr = FEC_ITR_CLK_SEL;
2376d851b47bSFugang Duan 	tx_itr = FEC_ITR_CLK_SEL;
2377d851b47bSFugang Duan 
2378d851b47bSFugang Duan 	/* set ICFT and ICTT */
2379d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2380d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2381d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2382d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2383d851b47bSFugang Duan 
2384d851b47bSFugang Duan 	rx_itr |= FEC_ITR_EN;
2385d851b47bSFugang Duan 	tx_itr |= FEC_ITR_EN;
2386d851b47bSFugang Duan 
2387d851b47bSFugang Duan 	writel(tx_itr, fep->hwp + FEC_TXIC0);
2388d851b47bSFugang Duan 	writel(rx_itr, fep->hwp + FEC_RXIC0);
2389d851b47bSFugang Duan 	writel(tx_itr, fep->hwp + FEC_TXIC1);
2390d851b47bSFugang Duan 	writel(rx_itr, fep->hwp + FEC_RXIC1);
2391d851b47bSFugang Duan 	writel(tx_itr, fep->hwp + FEC_TXIC2);
2392d851b47bSFugang Duan 	writel(rx_itr, fep->hwp + FEC_RXIC2);
2393d851b47bSFugang Duan }
2394d851b47bSFugang Duan 
2395d851b47bSFugang Duan static int
2396d851b47bSFugang Duan fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2397d851b47bSFugang Duan {
2398d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2399d851b47bSFugang Duan 
24006b7e4008SLothar Waßmann 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2401d851b47bSFugang Duan 		return -EOPNOTSUPP;
2402d851b47bSFugang Duan 
2403d851b47bSFugang Duan 	ec->rx_coalesce_usecs = fep->rx_time_itr;
2404d851b47bSFugang Duan 	ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2405d851b47bSFugang Duan 
2406d851b47bSFugang Duan 	ec->tx_coalesce_usecs = fep->tx_time_itr;
2407d851b47bSFugang Duan 	ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2408d851b47bSFugang Duan 
2409d851b47bSFugang Duan 	return 0;
2410d851b47bSFugang Duan }
2411d851b47bSFugang Duan 
2412d851b47bSFugang Duan static int
2413d851b47bSFugang Duan fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2414d851b47bSFugang Duan {
2415d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2416d851b47bSFugang Duan 	unsigned int cycle;
2417d851b47bSFugang Duan 
24186b7e4008SLothar Waßmann 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2419d851b47bSFugang Duan 		return -EOPNOTSUPP;
2420d851b47bSFugang Duan 
2421d851b47bSFugang Duan 	if (ec->rx_max_coalesced_frames > 255) {
2422d851b47bSFugang Duan 		pr_err("Rx coalesced frames exceed hardware limiation");
2423d851b47bSFugang Duan 		return -EINVAL;
2424d851b47bSFugang Duan 	}
2425d851b47bSFugang Duan 
2426d851b47bSFugang Duan 	if (ec->tx_max_coalesced_frames > 255) {
2427d851b47bSFugang Duan 		pr_err("Tx coalesced frame exceed hardware limiation");
2428d851b47bSFugang Duan 		return -EINVAL;
2429d851b47bSFugang Duan 	}
2430d851b47bSFugang Duan 
2431d851b47bSFugang Duan 	cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2432d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
2433d851b47bSFugang Duan 		pr_err("Rx coalesed usec exceeed hardware limiation");
2434d851b47bSFugang Duan 		return -EINVAL;
2435d851b47bSFugang Duan 	}
2436d851b47bSFugang Duan 
2437d851b47bSFugang Duan 	cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2438d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
2439d851b47bSFugang Duan 		pr_err("Rx coalesed usec exceeed hardware limiation");
2440d851b47bSFugang Duan 		return -EINVAL;
2441d851b47bSFugang Duan 	}
2442d851b47bSFugang Duan 
2443d851b47bSFugang Duan 	fep->rx_time_itr = ec->rx_coalesce_usecs;
2444d851b47bSFugang Duan 	fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2445d851b47bSFugang Duan 
2446d851b47bSFugang Duan 	fep->tx_time_itr = ec->tx_coalesce_usecs;
2447d851b47bSFugang Duan 	fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2448d851b47bSFugang Duan 
2449d851b47bSFugang Duan 	fec_enet_itr_coal_set(ndev);
2450d851b47bSFugang Duan 
2451d851b47bSFugang Duan 	return 0;
2452d851b47bSFugang Duan }
2453d851b47bSFugang Duan 
2454d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev)
2455d851b47bSFugang Duan {
2456d851b47bSFugang Duan 	struct ethtool_coalesce ec;
2457d851b47bSFugang Duan 
2458d851b47bSFugang Duan 	ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2459d851b47bSFugang Duan 	ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2460d851b47bSFugang Duan 
2461d851b47bSFugang Duan 	ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2462d851b47bSFugang Duan 	ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2463d851b47bSFugang Duan 
2464d851b47bSFugang Duan 	fec_enet_set_coalesce(ndev, &ec);
2465d851b47bSFugang Duan }
2466d851b47bSFugang Duan 
24671b7bde6dSNimrod Andy static int fec_enet_get_tunable(struct net_device *netdev,
24681b7bde6dSNimrod Andy 				const struct ethtool_tunable *tuna,
24691b7bde6dSNimrod Andy 				void *data)
24701b7bde6dSNimrod Andy {
24711b7bde6dSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
24721b7bde6dSNimrod Andy 	int ret = 0;
24731b7bde6dSNimrod Andy 
24741b7bde6dSNimrod Andy 	switch (tuna->id) {
24751b7bde6dSNimrod Andy 	case ETHTOOL_RX_COPYBREAK:
24761b7bde6dSNimrod Andy 		*(u32 *)data = fep->rx_copybreak;
24771b7bde6dSNimrod Andy 		break;
24781b7bde6dSNimrod Andy 	default:
24791b7bde6dSNimrod Andy 		ret = -EINVAL;
24801b7bde6dSNimrod Andy 		break;
24811b7bde6dSNimrod Andy 	}
24821b7bde6dSNimrod Andy 
24831b7bde6dSNimrod Andy 	return ret;
24841b7bde6dSNimrod Andy }
24851b7bde6dSNimrod Andy 
24861b7bde6dSNimrod Andy static int fec_enet_set_tunable(struct net_device *netdev,
24871b7bde6dSNimrod Andy 				const struct ethtool_tunable *tuna,
24881b7bde6dSNimrod Andy 				const void *data)
24891b7bde6dSNimrod Andy {
24901b7bde6dSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
24911b7bde6dSNimrod Andy 	int ret = 0;
24921b7bde6dSNimrod Andy 
24931b7bde6dSNimrod Andy 	switch (tuna->id) {
24941b7bde6dSNimrod Andy 	case ETHTOOL_RX_COPYBREAK:
24951b7bde6dSNimrod Andy 		fep->rx_copybreak = *(u32 *)data;
24961b7bde6dSNimrod Andy 		break;
24971b7bde6dSNimrod Andy 	default:
24981b7bde6dSNimrod Andy 		ret = -EINVAL;
24991b7bde6dSNimrod Andy 		break;
25001b7bde6dSNimrod Andy 	}
25011b7bde6dSNimrod Andy 
25021b7bde6dSNimrod Andy 	return ret;
25031b7bde6dSNimrod Andy }
25041b7bde6dSNimrod Andy 
2505de40ed31SNimrod Andy static void
2506de40ed31SNimrod Andy fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2507de40ed31SNimrod Andy {
2508de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2509de40ed31SNimrod Andy 
2510de40ed31SNimrod Andy 	if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2511de40ed31SNimrod Andy 		wol->supported = WAKE_MAGIC;
2512de40ed31SNimrod Andy 		wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2513de40ed31SNimrod Andy 	} else {
2514de40ed31SNimrod Andy 		wol->supported = wol->wolopts = 0;
2515de40ed31SNimrod Andy 	}
2516de40ed31SNimrod Andy }
2517de40ed31SNimrod Andy 
2518de40ed31SNimrod Andy static int
2519de40ed31SNimrod Andy fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2520de40ed31SNimrod Andy {
2521de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2522de40ed31SNimrod Andy 
2523de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2524de40ed31SNimrod Andy 		return -EINVAL;
2525de40ed31SNimrod Andy 
2526de40ed31SNimrod Andy 	if (wol->wolopts & ~WAKE_MAGIC)
2527de40ed31SNimrod Andy 		return -EINVAL;
2528de40ed31SNimrod Andy 
2529de40ed31SNimrod Andy 	device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2530de40ed31SNimrod Andy 	if (device_may_wakeup(&ndev->dev)) {
2531de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2532de40ed31SNimrod Andy 		if (fep->irq[0] > 0)
2533de40ed31SNimrod Andy 			enable_irq_wake(fep->irq[0]);
2534de40ed31SNimrod Andy 	} else {
2535de40ed31SNimrod Andy 		fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2536de40ed31SNimrod Andy 		if (fep->irq[0] > 0)
2537de40ed31SNimrod Andy 			disable_irq_wake(fep->irq[0]);
2538de40ed31SNimrod Andy 	}
2539de40ed31SNimrod Andy 
2540de40ed31SNimrod Andy 	return 0;
2541de40ed31SNimrod Andy }
2542de40ed31SNimrod Andy 
2543793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = {
2544793fc096SFrank Li 	.get_drvinfo		= fec_enet_get_drvinfo,
2545db65f35fSPhilippe Reynes 	.get_regs_len		= fec_enet_get_regs_len,
2546db65f35fSPhilippe Reynes 	.get_regs		= fec_enet_get_regs,
254732bc9b46SChris Healy 	.nway_reset		= fec_enet_nway_reset,
2548c1d7c48fSRussell King 	.get_link		= ethtool_op_get_link,
2549d851b47bSFugang Duan 	.get_coalesce		= fec_enet_get_coalesce,
2550d851b47bSFugang Duan 	.set_coalesce		= fec_enet_set_coalesce,
255138ae92dcSChris Healy #ifndef CONFIG_M5272
2552c1d7c48fSRussell King 	.get_pauseparam		= fec_enet_get_pauseparam,
2553c1d7c48fSRussell King 	.set_pauseparam		= fec_enet_set_pauseparam,
255438ae92dcSChris Healy 	.get_strings		= fec_enet_get_strings,
2555c1d7c48fSRussell King 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
255638ae92dcSChris Healy 	.get_sset_count		= fec_enet_get_sset_count,
255738ae92dcSChris Healy #endif
2558c1d7c48fSRussell King 	.get_ts_info		= fec_enet_get_ts_info,
25591b7bde6dSNimrod Andy 	.get_tunable		= fec_enet_get_tunable,
25601b7bde6dSNimrod Andy 	.set_tunable		= fec_enet_set_tunable,
2561de40ed31SNimrod Andy 	.get_wol		= fec_enet_get_wol,
2562de40ed31SNimrod Andy 	.set_wol		= fec_enet_set_wol,
25639365fbf5SPhilippe Reynes 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
25649365fbf5SPhilippe Reynes 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
2565793fc096SFrank Li };
2566793fc096SFrank Li 
2567793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2568793fc096SFrank Li {
2569793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
257045f5c327SPhilippe Reynes 	struct phy_device *phydev = ndev->phydev;
2571793fc096SFrank Li 
2572793fc096SFrank Li 	if (!netif_running(ndev))
2573793fc096SFrank Li 		return -EINVAL;
2574793fc096SFrank Li 
2575793fc096SFrank Li 	if (!phydev)
2576793fc096SFrank Li 		return -ENODEV;
2577793fc096SFrank Li 
25781d5244d0SBen Hutchings 	if (fep->bufdesc_ex) {
25791d5244d0SBen Hutchings 		if (cmd == SIOCSHWTSTAMP)
25801d5244d0SBen Hutchings 			return fec_ptp_set(ndev, rq);
25811d5244d0SBen Hutchings 		if (cmd == SIOCGHWTSTAMP)
25821d5244d0SBen Hutchings 			return fec_ptp_get(ndev, rq);
25831d5244d0SBen Hutchings 	}
2584793fc096SFrank Li 
2585793fc096SFrank Li 	return phy_mii_ioctl(phydev, rq, cmd);
2586793fc096SFrank Li }
2587793fc096SFrank Li 
2588793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev)
2589793fc096SFrank Li {
2590793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2591793fc096SFrank Li 	unsigned int i;
2592793fc096SFrank Li 	struct sk_buff *skb;
2593793fc096SFrank Li 	struct bufdesc	*bdp;
25944d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
25954d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
259659d0f746SFrank Li 	unsigned int q;
2597793fc096SFrank Li 
259859d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
259959d0f746SFrank Li 		rxq = fep->rx_queue[q];
26007355f276STroy Kisky 		bdp = rxq->bd.base;
26017355f276STroy Kisky 		for (i = 0; i < rxq->bd.ring_size; i++) {
26024d494cdcSFugang Duan 			skb = rxq->rx_skbuff[i];
26034d494cdcSFugang Duan 			rxq->rx_skbuff[i] = NULL;
2604730ee360SRussell King 			if (skb) {
260559d0f746SFrank Li 				dma_unmap_single(&fep->pdev->dev,
26065cfa3039SJohannes Berg 						 fec32_to_cpu(bdp->cbd_bufaddr),
2607b64bf4b7SNimrod Andy 						 FEC_ENET_RX_FRSIZE - fep->rx_align,
260859d0f746SFrank Li 						 DMA_FROM_DEVICE);
2609793fc096SFrank Li 				dev_kfree_skb(skb);
2610730ee360SRussell King 			}
26117355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
261259d0f746SFrank Li 		}
2613793fc096SFrank Li 	}
2614793fc096SFrank Li 
261559d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
261659d0f746SFrank Li 		txq = fep->tx_queue[q];
26177355f276STroy Kisky 		bdp = txq->bd.base;
26187355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
26194d494cdcSFugang Duan 			kfree(txq->tx_bounce[i]);
26204d494cdcSFugang Duan 			txq->tx_bounce[i] = NULL;
26214d494cdcSFugang Duan 			skb = txq->tx_skbuff[i];
26224d494cdcSFugang Duan 			txq->tx_skbuff[i] = NULL;
26238b7c9efaSRussell King 			dev_kfree_skb(skb);
26248b7c9efaSRussell King 		}
2625793fc096SFrank Li 	}
262659d0f746SFrank Li }
2627793fc096SFrank Li 
262859d0f746SFrank Li static void fec_enet_free_queue(struct net_device *ndev)
262959d0f746SFrank Li {
263059d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
263159d0f746SFrank Li 	int i;
263259d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
263359d0f746SFrank Li 
263459d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
263559d0f746SFrank Li 		if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
263659d0f746SFrank Li 			txq = fep->tx_queue[i];
263759d0f746SFrank Li 			dma_free_coherent(NULL,
26387355f276STroy Kisky 					  txq->bd.ring_size * TSO_HEADER_SIZE,
263959d0f746SFrank Li 					  txq->tso_hdrs,
264059d0f746SFrank Li 					  txq->tso_hdrs_dma);
264159d0f746SFrank Li 		}
264259d0f746SFrank Li 
264359d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
264459d0f746SFrank Li 		kfree(fep->rx_queue[i]);
264559d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
264659d0f746SFrank Li 		kfree(fep->tx_queue[i]);
264759d0f746SFrank Li }
264859d0f746SFrank Li 
264959d0f746SFrank Li static int fec_enet_alloc_queue(struct net_device *ndev)
265059d0f746SFrank Li {
265159d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
265259d0f746SFrank Li 	int i;
265359d0f746SFrank Li 	int ret = 0;
265459d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
265559d0f746SFrank Li 
265659d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
265759d0f746SFrank Li 		txq = kzalloc(sizeof(*txq), GFP_KERNEL);
265859d0f746SFrank Li 		if (!txq) {
265959d0f746SFrank Li 			ret = -ENOMEM;
266059d0f746SFrank Li 			goto alloc_failed;
266159d0f746SFrank Li 		}
266259d0f746SFrank Li 
266359d0f746SFrank Li 		fep->tx_queue[i] = txq;
26647355f276STroy Kisky 		txq->bd.ring_size = TX_RING_SIZE;
26657355f276STroy Kisky 		fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
266659d0f746SFrank Li 
266759d0f746SFrank Li 		txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
266859d0f746SFrank Li 		txq->tx_wake_threshold =
26697355f276STroy Kisky 			(txq->bd.ring_size - txq->tx_stop_threshold) / 2;
267059d0f746SFrank Li 
267159d0f746SFrank Li 		txq->tso_hdrs = dma_alloc_coherent(NULL,
26727355f276STroy Kisky 					txq->bd.ring_size * TSO_HEADER_SIZE,
267359d0f746SFrank Li 					&txq->tso_hdrs_dma,
267459d0f746SFrank Li 					GFP_KERNEL);
267559d0f746SFrank Li 		if (!txq->tso_hdrs) {
267659d0f746SFrank Li 			ret = -ENOMEM;
267759d0f746SFrank Li 			goto alloc_failed;
267859d0f746SFrank Li 		}
267959d0f746SFrank Li 	}
268059d0f746SFrank Li 
268159d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
268259d0f746SFrank Li 		fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
268359d0f746SFrank Li 					   GFP_KERNEL);
268459d0f746SFrank Li 		if (!fep->rx_queue[i]) {
268559d0f746SFrank Li 			ret = -ENOMEM;
268659d0f746SFrank Li 			goto alloc_failed;
268759d0f746SFrank Li 		}
268859d0f746SFrank Li 
26897355f276STroy Kisky 		fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
26907355f276STroy Kisky 		fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
269159d0f746SFrank Li 	}
269259d0f746SFrank Li 	return ret;
269359d0f746SFrank Li 
269459d0f746SFrank Li alloc_failed:
269559d0f746SFrank Li 	fec_enet_free_queue(ndev);
269659d0f746SFrank Li 	return ret;
269759d0f746SFrank Li }
269859d0f746SFrank Li 
269959d0f746SFrank Li static int
270059d0f746SFrank Li fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
2701793fc096SFrank Li {
2702793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2703793fc096SFrank Li 	unsigned int i;
2704793fc096SFrank Li 	struct sk_buff *skb;
2705793fc096SFrank Li 	struct bufdesc	*bdp;
27064d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
2707793fc096SFrank Li 
270859d0f746SFrank Li 	rxq = fep->rx_queue[queue];
27097355f276STroy Kisky 	bdp = rxq->bd.base;
27107355f276STroy Kisky 	for (i = 0; i < rxq->bd.ring_size; i++) {
2711793fc096SFrank Li 		skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
2712ffdce2ccSRussell King 		if (!skb)
2713ffdce2ccSRussell King 			goto err_alloc;
2714793fc096SFrank Li 
27151b7bde6dSNimrod Andy 		if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
2716730ee360SRussell King 			dev_kfree_skb(skb);
2717ffdce2ccSRussell King 			goto err_alloc;
2718d842a31fSDuan Fugang-B38611 		}
2719730ee360SRussell King 
27204d494cdcSFugang Duan 		rxq->rx_skbuff[i] = skb;
27215cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
2722793fc096SFrank Li 
2723793fc096SFrank Li 		if (fep->bufdesc_ex) {
2724793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
27255cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
2726793fc096SFrank Li 		}
2727793fc096SFrank Li 
27287355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
2729793fc096SFrank Li 	}
2730793fc096SFrank Li 
2731793fc096SFrank Li 	/* Set the last buffer to wrap. */
27327355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
27335cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
273459d0f746SFrank Li 	return 0;
2735793fc096SFrank Li 
273659d0f746SFrank Li  err_alloc:
273759d0f746SFrank Li 	fec_enet_free_buffers(ndev);
273859d0f746SFrank Li 	return -ENOMEM;
273959d0f746SFrank Li }
274059d0f746SFrank Li 
274159d0f746SFrank Li static int
274259d0f746SFrank Li fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
274359d0f746SFrank Li {
274459d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
274559d0f746SFrank Li 	unsigned int i;
274659d0f746SFrank Li 	struct bufdesc  *bdp;
274759d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
274859d0f746SFrank Li 
274959d0f746SFrank Li 	txq = fep->tx_queue[queue];
27507355f276STroy Kisky 	bdp = txq->bd.base;
27517355f276STroy Kisky 	for (i = 0; i < txq->bd.ring_size; i++) {
27524d494cdcSFugang Duan 		txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
27534d494cdcSFugang Duan 		if (!txq->tx_bounce[i])
2754ffdce2ccSRussell King 			goto err_alloc;
2755793fc096SFrank Li 
27565cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(0);
27575cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
2758793fc096SFrank Li 
2759793fc096SFrank Li 		if (fep->bufdesc_ex) {
2760793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
27615cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
2762793fc096SFrank Li 		}
2763793fc096SFrank Li 
27647355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
2765793fc096SFrank Li 	}
2766793fc096SFrank Li 
2767793fc096SFrank Li 	/* Set the last buffer to wrap. */
27687355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
27695cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
2770793fc096SFrank Li 
2771793fc096SFrank Li 	return 0;
2772ffdce2ccSRussell King 
2773ffdce2ccSRussell King  err_alloc:
2774ffdce2ccSRussell King 	fec_enet_free_buffers(ndev);
2775ffdce2ccSRussell King 	return -ENOMEM;
2776793fc096SFrank Li }
2777793fc096SFrank Li 
277859d0f746SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev)
277959d0f746SFrank Li {
278059d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
278159d0f746SFrank Li 	unsigned int i;
278259d0f746SFrank Li 
278359d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
278459d0f746SFrank Li 		if (fec_enet_alloc_rxq_buffers(ndev, i))
278559d0f746SFrank Li 			return -ENOMEM;
278659d0f746SFrank Li 
278759d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
278859d0f746SFrank Li 		if (fec_enet_alloc_txq_buffers(ndev, i))
278959d0f746SFrank Li 			return -ENOMEM;
279059d0f746SFrank Li 	return 0;
279159d0f746SFrank Li }
279259d0f746SFrank Li 
2793793fc096SFrank Li static int
2794793fc096SFrank Li fec_enet_open(struct net_device *ndev)
2795793fc096SFrank Li {
2796793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2797793fc096SFrank Li 	int ret;
2798793fc096SFrank Li 
27998fff755eSAndrew Lunn 	ret = pm_runtime_get_sync(&fep->pdev->dev);
2800b0c6ce24SFabio Estevam 	if (ret < 0)
28018fff755eSAndrew Lunn 		return ret;
28028fff755eSAndrew Lunn 
28035bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&fep->pdev->dev);
2804e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
2805e8fcfcd5SNimrod Andy 	if (ret)
28068fff755eSAndrew Lunn 		goto clk_enable;
2807e8fcfcd5SNimrod Andy 
2808793fc096SFrank Li 	/* I should reset the ring buffers here, but I don't yet know
2809793fc096SFrank Li 	 * a simple way to do that.
2810793fc096SFrank Li 	 */
2811793fc096SFrank Li 
2812793fc096SFrank Li 	ret = fec_enet_alloc_buffers(ndev);
2813793fc096SFrank Li 	if (ret)
2814681d2421SFabio Estevam 		goto err_enet_alloc;
2815793fc096SFrank Li 
281655dd2753SNimrod Andy 	/* Init MAC prior to mii bus probe */
281755dd2753SNimrod Andy 	fec_restart(ndev);
281855dd2753SNimrod Andy 
2819793fc096SFrank Li 	/* Probe and connect to PHY when open the interface */
2820793fc096SFrank Li 	ret = fec_enet_mii_probe(ndev);
2821681d2421SFabio Estevam 	if (ret)
2822681d2421SFabio Estevam 		goto err_enet_mii_probe;
2823ce5eaf02SRussell King 
2824*29380905SLucas Stach 	if (fep->quirks & FEC_QUIRK_ERR006687)
2825*29380905SLucas Stach 		imx6q_cpuidle_fec_irqs_used();
2826*29380905SLucas Stach 
2827ce5eaf02SRussell King 	napi_enable(&fep->napi);
282845f5c327SPhilippe Reynes 	phy_start(ndev->phydev);
28294d494cdcSFugang Duan 	netif_tx_start_all_queues(ndev);
28304d494cdcSFugang Duan 
2831de40ed31SNimrod Andy 	device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2832de40ed31SNimrod Andy 				 FEC_WOL_FLAG_ENABLE);
2833de40ed31SNimrod Andy 
2834793fc096SFrank Li 	return 0;
2835681d2421SFabio Estevam 
2836681d2421SFabio Estevam err_enet_mii_probe:
2837681d2421SFabio Estevam 	fec_enet_free_buffers(ndev);
2838681d2421SFabio Estevam err_enet_alloc:
2839681d2421SFabio Estevam 	fec_enet_clk_enable(ndev, false);
28408fff755eSAndrew Lunn clk_enable:
28418fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&fep->pdev->dev);
28428fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&fep->pdev->dev);
2843681d2421SFabio Estevam 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2844681d2421SFabio Estevam 	return ret;
2845793fc096SFrank Li }
2846793fc096SFrank Li 
2847793fc096SFrank Li static int
2848793fc096SFrank Li fec_enet_close(struct net_device *ndev)
2849793fc096SFrank Li {
2850793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2851793fc096SFrank Li 
285245f5c327SPhilippe Reynes 	phy_stop(ndev->phydev);
2853d76cfae9SRussell King 
285431a6de34SRussell King 	if (netif_device_present(ndev)) {
2855793fc096SFrank Li 		napi_disable(&fep->napi);
2856b49cd504SRussell King 		netif_tx_disable(ndev);
2857793fc096SFrank Li 		fec_stop(ndev);
285831a6de34SRussell King 	}
2859793fc096SFrank Li 
286045f5c327SPhilippe Reynes 	phy_disconnect(ndev->phydev);
2861793fc096SFrank Li 
2862*29380905SLucas Stach 	if (fep->quirks & FEC_QUIRK_ERR006687)
2863*29380905SLucas Stach 		imx6q_cpuidle_fec_irqs_unused();
2864*29380905SLucas Stach 
2865e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
28665bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
28678fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&fep->pdev->dev);
28688fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&fep->pdev->dev);
28698fff755eSAndrew Lunn 
2870793fc096SFrank Li 	fec_enet_free_buffers(ndev);
2871793fc096SFrank Li 
2872793fc096SFrank Li 	return 0;
2873793fc096SFrank Li }
2874793fc096SFrank Li 
2875793fc096SFrank Li /* Set or clear the multicast filter for this adaptor.
2876793fc096SFrank Li  * Skeleton taken from sunlance driver.
2877793fc096SFrank Li  * The CPM Ethernet implementation allows Multicast as well as individual
2878793fc096SFrank Li  * MAC address filtering.  Some of the drivers check to make sure it is
2879793fc096SFrank Li  * a group multicast address, and discard those that are not.  I guess I
2880793fc096SFrank Li  * will do the same for now, but just remove the test if you want
2881793fc096SFrank Li  * individual filtering as well (do the upper net layers want or support
2882793fc096SFrank Li  * this kind of feature?).
2883793fc096SFrank Li  */
2884793fc096SFrank Li 
2885793fc096SFrank Li #define HASH_BITS	6		/* #bits in hash */
2886793fc096SFrank Li #define CRC32_POLY	0xEDB88320
2887793fc096SFrank Li 
2888793fc096SFrank Li static void set_multicast_list(struct net_device *ndev)
2889793fc096SFrank Li {
2890793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2891793fc096SFrank Li 	struct netdev_hw_addr *ha;
2892793fc096SFrank Li 	unsigned int i, bit, data, crc, tmp;
2893793fc096SFrank Li 	unsigned char hash;
2894793fc096SFrank Li 
2895793fc096SFrank Li 	if (ndev->flags & IFF_PROMISC) {
2896793fc096SFrank Li 		tmp = readl(fep->hwp + FEC_R_CNTRL);
2897793fc096SFrank Li 		tmp |= 0x8;
2898793fc096SFrank Li 		writel(tmp, fep->hwp + FEC_R_CNTRL);
2899793fc096SFrank Li 		return;
2900793fc096SFrank Li 	}
2901793fc096SFrank Li 
2902793fc096SFrank Li 	tmp = readl(fep->hwp + FEC_R_CNTRL);
2903793fc096SFrank Li 	tmp &= ~0x8;
2904793fc096SFrank Li 	writel(tmp, fep->hwp + FEC_R_CNTRL);
2905793fc096SFrank Li 
2906793fc096SFrank Li 	if (ndev->flags & IFF_ALLMULTI) {
2907793fc096SFrank Li 		/* Catch all multicast addresses, so set the
2908793fc096SFrank Li 		 * filter to all 1's
2909793fc096SFrank Li 		 */
2910793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2911793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2912793fc096SFrank Li 
2913793fc096SFrank Li 		return;
2914793fc096SFrank Li 	}
2915793fc096SFrank Li 
2916793fc096SFrank Li 	/* Clear filter and add the addresses in hash register
2917793fc096SFrank Li 	 */
2918793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2919793fc096SFrank Li 	writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2920793fc096SFrank Li 
2921793fc096SFrank Li 	netdev_for_each_mc_addr(ha, ndev) {
2922793fc096SFrank Li 		/* calculate crc32 value of mac address */
2923793fc096SFrank Li 		crc = 0xffffffff;
2924793fc096SFrank Li 
2925793fc096SFrank Li 		for (i = 0; i < ndev->addr_len; i++) {
2926793fc096SFrank Li 			data = ha->addr[i];
2927793fc096SFrank Li 			for (bit = 0; bit < 8; bit++, data >>= 1) {
2928793fc096SFrank Li 				crc = (crc >> 1) ^
2929793fc096SFrank Li 				(((crc ^ data) & 1) ? CRC32_POLY : 0);
2930793fc096SFrank Li 			}
2931793fc096SFrank Li 		}
2932793fc096SFrank Li 
2933793fc096SFrank Li 		/* only upper 6 bits (HASH_BITS) are used
2934793fc096SFrank Li 		 * which point to specific bit in he hash registers
2935793fc096SFrank Li 		 */
2936793fc096SFrank Li 		hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2937793fc096SFrank Li 
2938793fc096SFrank Li 		if (hash > 31) {
2939793fc096SFrank Li 			tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2940793fc096SFrank Li 			tmp |= 1 << (hash - 32);
2941793fc096SFrank Li 			writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2942793fc096SFrank Li 		} else {
2943793fc096SFrank Li 			tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2944793fc096SFrank Li 			tmp |= 1 << hash;
2945793fc096SFrank Li 			writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2946793fc096SFrank Li 		}
2947793fc096SFrank Li 	}
2948793fc096SFrank Li }
2949793fc096SFrank Li 
2950793fc096SFrank Li /* Set a MAC change in hardware. */
2951793fc096SFrank Li static int
2952793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p)
2953793fc096SFrank Li {
2954793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2955793fc096SFrank Li 	struct sockaddr *addr = p;
2956793fc096SFrank Li 
295744934facSLucas Stach 	if (addr) {
2958793fc096SFrank Li 		if (!is_valid_ether_addr(addr->sa_data))
2959793fc096SFrank Li 			return -EADDRNOTAVAIL;
2960793fc096SFrank Li 		memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
296144934facSLucas Stach 	}
2962793fc096SFrank Li 
29639638d19eSNimrod Andy 	/* Add netif status check here to avoid system hang in below case:
29649638d19eSNimrod Andy 	 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
29659638d19eSNimrod Andy 	 * After ethx down, fec all clocks are gated off and then register
29669638d19eSNimrod Andy 	 * access causes system hang.
29679638d19eSNimrod Andy 	 */
29689638d19eSNimrod Andy 	if (!netif_running(ndev))
29699638d19eSNimrod Andy 		return 0;
29709638d19eSNimrod Andy 
2971793fc096SFrank Li 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2972793fc096SFrank Li 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
2973793fc096SFrank Li 		fep->hwp + FEC_ADDR_LOW);
2974793fc096SFrank Li 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
2975793fc096SFrank Li 		fep->hwp + FEC_ADDR_HIGH);
2976793fc096SFrank Li 	return 0;
2977793fc096SFrank Li }
2978793fc096SFrank Li 
2979793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
2980793fc096SFrank Li /**
2981793fc096SFrank Li  * fec_poll_controller - FEC Poll controller function
2982793fc096SFrank Li  * @dev: The FEC network adapter
2983793fc096SFrank Li  *
2984793fc096SFrank Li  * Polled functionality used by netconsole and others in non interrupt mode
2985793fc096SFrank Li  *
2986793fc096SFrank Li  */
2987793fc096SFrank Li static void fec_poll_controller(struct net_device *dev)
2988793fc096SFrank Li {
2989793fc096SFrank Li 	int i;
2990793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(dev);
2991793fc096SFrank Li 
2992793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
2993793fc096SFrank Li 		if (fep->irq[i] > 0) {
2994793fc096SFrank Li 			disable_irq(fep->irq[i]);
2995793fc096SFrank Li 			fec_enet_interrupt(fep->irq[i], dev);
2996793fc096SFrank Li 			enable_irq(fep->irq[i]);
2997793fc096SFrank Li 		}
2998793fc096SFrank Li 	}
2999793fc096SFrank Li }
3000793fc096SFrank Li #endif
3001793fc096SFrank Li 
30025bc26726SNimrod Andy static inline void fec_enet_set_netdev_features(struct net_device *netdev,
30034c09eed9SJim Baxter 	netdev_features_t features)
30044c09eed9SJim Baxter {
30054c09eed9SJim Baxter 	struct fec_enet_private *fep = netdev_priv(netdev);
30064c09eed9SJim Baxter 	netdev_features_t changed = features ^ netdev->features;
30074c09eed9SJim Baxter 
30084c09eed9SJim Baxter 	netdev->features = features;
30094c09eed9SJim Baxter 
30104c09eed9SJim Baxter 	/* Receive checksum has been changed */
30114c09eed9SJim Baxter 	if (changed & NETIF_F_RXCSUM) {
30124c09eed9SJim Baxter 		if (features & NETIF_F_RXCSUM)
30134c09eed9SJim Baxter 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
30144c09eed9SJim Baxter 		else
30154c09eed9SJim Baxter 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
30168506fa1dSRussell King 	}
30175bc26726SNimrod Andy }
30184c09eed9SJim Baxter 
30195bc26726SNimrod Andy static int fec_set_features(struct net_device *netdev,
30205bc26726SNimrod Andy 	netdev_features_t features)
30215bc26726SNimrod Andy {
30225bc26726SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
30235bc26726SNimrod Andy 	netdev_features_t changed = features ^ netdev->features;
30245bc26726SNimrod Andy 
30255b40f709SFabio Estevam 	if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
30265bc26726SNimrod Andy 		napi_disable(&fep->napi);
30275bc26726SNimrod Andy 		netif_tx_lock_bh(netdev);
30285bc26726SNimrod Andy 		fec_stop(netdev);
30295bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
3030ef83337dSRussell King 		fec_restart(netdev);
30314d494cdcSFugang Duan 		netif_tx_wake_all_queues(netdev);
30326af42d42SRussell King 		netif_tx_unlock_bh(netdev);
3033dbc64a8eSRussell King 		napi_enable(&fep->napi);
30345bc26726SNimrod Andy 	} else {
30355bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
30364c09eed9SJim Baxter 	}
30374c09eed9SJim Baxter 
30384c09eed9SJim Baxter 	return 0;
30394c09eed9SJim Baxter }
30404c09eed9SJim Baxter 
3041793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = {
3042793fc096SFrank Li 	.ndo_open		= fec_enet_open,
3043793fc096SFrank Li 	.ndo_stop		= fec_enet_close,
3044793fc096SFrank Li 	.ndo_start_xmit		= fec_enet_start_xmit,
3045793fc096SFrank Li 	.ndo_set_rx_mode	= set_multicast_list,
3046793fc096SFrank Li 	.ndo_change_mtu		= eth_change_mtu,
3047793fc096SFrank Li 	.ndo_validate_addr	= eth_validate_addr,
3048793fc096SFrank Li 	.ndo_tx_timeout		= fec_timeout,
3049793fc096SFrank Li 	.ndo_set_mac_address	= fec_set_mac_address,
3050793fc096SFrank Li 	.ndo_do_ioctl		= fec_enet_ioctl,
3051793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3052793fc096SFrank Li 	.ndo_poll_controller	= fec_poll_controller,
3053793fc096SFrank Li #endif
30544c09eed9SJim Baxter 	.ndo_set_features	= fec_set_features,
3055793fc096SFrank Li };
3056793fc096SFrank Li 
305753bb20d1STroy Kisky static const unsigned short offset_des_active_rxq[] = {
305853bb20d1STroy Kisky 	FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
305953bb20d1STroy Kisky };
306053bb20d1STroy Kisky 
306153bb20d1STroy Kisky static const unsigned short offset_des_active_txq[] = {
306253bb20d1STroy Kisky 	FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
306353bb20d1STroy Kisky };
306453bb20d1STroy Kisky 
3065793fc096SFrank Li  /*
3066793fc096SFrank Li   * XXX:  We need to clean up on failure exits here.
3067793fc096SFrank Li   *
3068793fc096SFrank Li   */
3069793fc096SFrank Li static int fec_enet_init(struct net_device *ndev)
3070793fc096SFrank Li {
3071793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3072793fc096SFrank Li 	struct bufdesc *cbd_base;
30734d494cdcSFugang Duan 	dma_addr_t bd_dma;
307455d0218aSNimrod Andy 	int bd_size;
307559d0f746SFrank Li 	unsigned int i;
30767355f276STroy Kisky 	unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
30777355f276STroy Kisky 			sizeof(struct bufdesc);
30787355f276STroy Kisky 	unsigned dsize_log2 = __fls(dsize);
307955d0218aSNimrod Andy 
30807355f276STroy Kisky 	WARN_ON(dsize != (1 << dsize_log2));
308141ef84ceSFugang Duan #if defined(CONFIG_ARM)
308241ef84ceSFugang Duan 	fep->rx_align = 0xf;
308341ef84ceSFugang Duan 	fep->tx_align = 0xf;
308441ef84ceSFugang Duan #else
308541ef84ceSFugang Duan 	fep->rx_align = 0x3;
308641ef84ceSFugang Duan 	fep->tx_align = 0x3;
308741ef84ceSFugang Duan #endif
308841ef84ceSFugang Duan 
308959d0f746SFrank Li 	fec_enet_alloc_queue(ndev);
309079f33912SNimrod Andy 
30917355f276STroy Kisky 	bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3092793fc096SFrank Li 
3093793fc096SFrank Li 	/* Allocate memory for buffer descriptors. */
3094c0a1a0a6SLucas Stach 	cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3095793fc096SFrank Li 				       GFP_KERNEL);
30964d494cdcSFugang Duan 	if (!cbd_base) {
30974d494cdcSFugang Duan 		return -ENOMEM;
30984d494cdcSFugang Duan 	}
3099793fc096SFrank Li 
31004d494cdcSFugang Duan 	memset(cbd_base, 0, bd_size);
3101793fc096SFrank Li 
3102793fc096SFrank Li 	/* Get the Ethernet address */
3103793fc096SFrank Li 	fec_get_mac(ndev);
310444934facSLucas Stach 	/* make sure MAC we just acquired is programmed into the hw */
310544934facSLucas Stach 	fec_set_mac_address(ndev, NULL);
3106793fc096SFrank Li 
3107793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
310859d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
31097355f276STroy Kisky 		struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
31107355f276STroy Kisky 		unsigned size = dsize * rxq->bd.ring_size;
31117355f276STroy Kisky 
31127355f276STroy Kisky 		rxq->bd.qid = i;
31137355f276STroy Kisky 		rxq->bd.base = cbd_base;
31147355f276STroy Kisky 		rxq->bd.cur = cbd_base;
31157355f276STroy Kisky 		rxq->bd.dma = bd_dma;
31167355f276STroy Kisky 		rxq->bd.dsize = dsize;
31177355f276STroy Kisky 		rxq->bd.dsize_log2 = dsize_log2;
311853bb20d1STroy Kisky 		rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
31197355f276STroy Kisky 		bd_dma += size;
31207355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
31217355f276STroy Kisky 		rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
312259d0f746SFrank Li 	}
312359d0f746SFrank Li 
312459d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
31257355f276STroy Kisky 		struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
31267355f276STroy Kisky 		unsigned size = dsize * txq->bd.ring_size;
31277355f276STroy Kisky 
31287355f276STroy Kisky 		txq->bd.qid = i;
31297355f276STroy Kisky 		txq->bd.base = cbd_base;
31307355f276STroy Kisky 		txq->bd.cur = cbd_base;
31317355f276STroy Kisky 		txq->bd.dma = bd_dma;
31327355f276STroy Kisky 		txq->bd.dsize = dsize;
31337355f276STroy Kisky 		txq->bd.dsize_log2 = dsize_log2;
313453bb20d1STroy Kisky 		txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
31357355f276STroy Kisky 		bd_dma += size;
31367355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
31377355f276STroy Kisky 		txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
313859d0f746SFrank Li 	}
31394d494cdcSFugang Duan 
3140793fc096SFrank Li 
3141793fc096SFrank Li 	/* The FEC Ethernet specific entries in the device structure */
3142793fc096SFrank Li 	ndev->watchdog_timeo = TX_TIMEOUT;
3143793fc096SFrank Li 	ndev->netdev_ops = &fec_netdev_ops;
3144793fc096SFrank Li 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
3145793fc096SFrank Li 
3146793fc096SFrank Li 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3147322555f5SFabio Estevam 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
3148793fc096SFrank Li 
31496b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3150cdffcf1bSJim Baxter 		/* enable hw VLAN support */
3151cdffcf1bSJim Baxter 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3152cdffcf1bSJim Baxter 
31536b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
315479f33912SNimrod Andy 		ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
315579f33912SNimrod Andy 
31564c09eed9SJim Baxter 		/* enable hw accelerator */
31574c09eed9SJim Baxter 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
315879f33912SNimrod Andy 				| NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
31594c09eed9SJim Baxter 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
316048496255SShawn Guo 	}
31614c09eed9SJim Baxter 
31626b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_AVB) {
316341ef84ceSFugang Duan 		fep->tx_align = 0;
316441ef84ceSFugang Duan 		fep->rx_align = 0x3f;
316541ef84ceSFugang Duan 	}
316641ef84ceSFugang Duan 
316709d1e541SNimrod Andy 	ndev->hw_features = ndev->features;
316809d1e541SNimrod Andy 
3169ef83337dSRussell King 	fec_restart(ndev);
3170793fc096SFrank Li 
3171793fc096SFrank Li 	return 0;
3172793fc096SFrank Li }
3173793fc096SFrank Li 
3174793fc096SFrank Li #ifdef CONFIG_OF
3175793fc096SFrank Li static void fec_reset_phy(struct platform_device *pdev)
3176793fc096SFrank Li {
3177793fc096SFrank Li 	int err, phy_reset;
3178962d8cdcSBernhard Walle 	bool active_high = false;
3179793fc096SFrank Li 	int msec = 1;
3180793fc096SFrank Li 	struct device_node *np = pdev->dev.of_node;
3181793fc096SFrank Li 
3182793fc096SFrank Li 	if (!np)
3183793fc096SFrank Li 		return;
3184793fc096SFrank Li 
3185793fc096SFrank Li 	of_property_read_u32(np, "phy-reset-duration", &msec);
3186793fc096SFrank Li 	/* A sane reset duration should not be longer than 1s */
3187793fc096SFrank Li 	if (msec > 1000)
3188793fc096SFrank Li 		msec = 1;
3189793fc096SFrank Li 
3190793fc096SFrank Li 	phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
3191793fc096SFrank Li 	if (!gpio_is_valid(phy_reset))
3192793fc096SFrank Li 		return;
3193793fc096SFrank Li 
3194962d8cdcSBernhard Walle 	active_high = of_property_read_bool(np, "phy-reset-active-high");
319564f10f6eSBernhard Walle 
3196793fc096SFrank Li 	err = devm_gpio_request_one(&pdev->dev, phy_reset,
3197962d8cdcSBernhard Walle 			active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
319864f10f6eSBernhard Walle 			"phy-reset");
3199793fc096SFrank Li 	if (err) {
3200793fc096SFrank Li 		dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
3201793fc096SFrank Li 		return;
3202793fc096SFrank Li 	}
3203793fc096SFrank Li 	msleep(msec);
3204962d8cdcSBernhard Walle 	gpio_set_value_cansleep(phy_reset, !active_high);
3205793fc096SFrank Li }
3206793fc096SFrank Li #else /* CONFIG_OF */
3207793fc096SFrank Li static void fec_reset_phy(struct platform_device *pdev)
3208793fc096SFrank Li {
3209793fc096SFrank Li 	/*
3210793fc096SFrank Li 	 * In case of platform probe, the reset has been done
3211793fc096SFrank Li 	 * by machine code.
3212793fc096SFrank Li 	 */
3213793fc096SFrank Li }
3214793fc096SFrank Li #endif /* CONFIG_OF */
3215793fc096SFrank Li 
32169fc095f1SFugang Duan static void
32179fc095f1SFugang Duan fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
32189fc095f1SFugang Duan {
32199fc095f1SFugang Duan 	struct device_node *np = pdev->dev.of_node;
32209fc095f1SFugang Duan 
32219fc095f1SFugang Duan 	*num_tx = *num_rx = 1;
32229fc095f1SFugang Duan 
32239fc095f1SFugang Duan 	if (!np || !of_device_is_available(np))
32249fc095f1SFugang Duan 		return;
32259fc095f1SFugang Duan 
32269fc095f1SFugang Duan 	/* parse the num of tx and rx queues */
322773b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3228b7bd75cfSFrank Li 
322973b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
32309fc095f1SFugang Duan 
32319fc095f1SFugang Duan 	if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3232b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
32339fc095f1SFugang Duan 			 *num_tx);
32349fc095f1SFugang Duan 		*num_tx = 1;
32359fc095f1SFugang Duan 		return;
32369fc095f1SFugang Duan 	}
32379fc095f1SFugang Duan 
32389fc095f1SFugang Duan 	if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3239b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
32409fc095f1SFugang Duan 			 *num_rx);
32419fc095f1SFugang Duan 		*num_rx = 1;
32429fc095f1SFugang Duan 		return;
32439fc095f1SFugang Duan 	}
32449fc095f1SFugang Duan 
32459fc095f1SFugang Duan }
32469fc095f1SFugang Duan 
3247793fc096SFrank Li static int
3248793fc096SFrank Li fec_probe(struct platform_device *pdev)
3249793fc096SFrank Li {
3250793fc096SFrank Li 	struct fec_enet_private *fep;
3251793fc096SFrank Li 	struct fec_platform_data *pdata;
3252793fc096SFrank Li 	struct net_device *ndev;
3253793fc096SFrank Li 	int i, irq, ret = 0;
3254793fc096SFrank Li 	struct resource *r;
3255793fc096SFrank Li 	const struct of_device_id *of_id;
3256793fc096SFrank Li 	static int dev_id;
3257407066f8SUwe Kleine-König 	struct device_node *np = pdev->dev.of_node, *phy_node;
3258b7bd75cfSFrank Li 	int num_tx_qs;
3259b7bd75cfSFrank Li 	int num_rx_qs;
3260793fc096SFrank Li 
32619fc095f1SFugang Duan 	fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
32629fc095f1SFugang Duan 
3263793fc096SFrank Li 	/* Init network device */
32649fc095f1SFugang Duan 	ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
32659fc095f1SFugang Duan 				  num_tx_qs, num_rx_qs);
3266793fc096SFrank Li 	if (!ndev)
3267793fc096SFrank Li 		return -ENOMEM;
3268793fc096SFrank Li 
3269793fc096SFrank Li 	SET_NETDEV_DEV(ndev, &pdev->dev);
3270793fc096SFrank Li 
3271793fc096SFrank Li 	/* setup board info structure */
3272793fc096SFrank Li 	fep = netdev_priv(ndev);
3273793fc096SFrank Li 
32746b7e4008SLothar Waßmann 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
32756b7e4008SLothar Waßmann 	if (of_id)
32766b7e4008SLothar Waßmann 		pdev->id_entry = of_id->data;
32776b7e4008SLothar Waßmann 	fep->quirks = pdev->id_entry->driver_data;
32786b7e4008SLothar Waßmann 
32790c818594SHubert Feurstein 	fep->netdev = ndev;
32809fc095f1SFugang Duan 	fep->num_rx_queues = num_rx_qs;
32819fc095f1SFugang Duan 	fep->num_tx_queues = num_tx_qs;
32829fc095f1SFugang Duan 
3283d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
3284793fc096SFrank Li 	/* default enable pause frame auto negotiation */
32856b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT)
3286793fc096SFrank Li 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3287d1391930SGuenter Roeck #endif
3288793fc096SFrank Li 
32895bbde4d2SNimrod Andy 	/* Select default pin state */
32905bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&pdev->dev);
32915bbde4d2SNimrod Andy 
3292399db75bSFabio Estevam 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3293941e173aSTushar Behera 	fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3294941e173aSTushar Behera 	if (IS_ERR(fep->hwp)) {
3295941e173aSTushar Behera 		ret = PTR_ERR(fep->hwp);
3296941e173aSTushar Behera 		goto failed_ioremap;
3297941e173aSTushar Behera 	}
3298941e173aSTushar Behera 
3299793fc096SFrank Li 	fep->pdev = pdev;
3300793fc096SFrank Li 	fep->dev_id = dev_id++;
3301793fc096SFrank Li 
3302793fc096SFrank Li 	platform_set_drvdata(pdev, ndev);
3303793fc096SFrank Li 
3304*29380905SLucas Stach 	if ((of_machine_is_compatible("fsl,imx6q") ||
3305*29380905SLucas Stach 	     of_machine_is_compatible("fsl,imx6dl")) &&
3306*29380905SLucas Stach 	    !of_property_read_bool(np, "fsl,err006687-workaround-present"))
3307*29380905SLucas Stach 		fep->quirks |= FEC_QUIRK_ERR006687;
3308*29380905SLucas Stach 
3309de40ed31SNimrod Andy 	if (of_get_property(np, "fsl,magic-packet", NULL))
3310de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3311de40ed31SNimrod Andy 
3312407066f8SUwe Kleine-König 	phy_node = of_parse_phandle(np, "phy-handle", 0);
3313407066f8SUwe Kleine-König 	if (!phy_node && of_phy_is_fixed_link(np)) {
3314407066f8SUwe Kleine-König 		ret = of_phy_register_fixed_link(np);
3315407066f8SUwe Kleine-König 		if (ret < 0) {
3316407066f8SUwe Kleine-König 			dev_err(&pdev->dev,
3317407066f8SUwe Kleine-König 				"broken fixed-link specification\n");
3318407066f8SUwe Kleine-König 			goto failed_phy;
3319407066f8SUwe Kleine-König 		}
3320407066f8SUwe Kleine-König 		phy_node = of_node_get(np);
3321407066f8SUwe Kleine-König 	}
3322407066f8SUwe Kleine-König 	fep->phy_node = phy_node;
3323407066f8SUwe Kleine-König 
33246c5f7808SGuenter Roeck 	ret = of_get_phy_mode(pdev->dev.of_node);
3325793fc096SFrank Li 	if (ret < 0) {
332694660ba0SJingoo Han 		pdata = dev_get_platdata(&pdev->dev);
3327793fc096SFrank Li 		if (pdata)
3328793fc096SFrank Li 			fep->phy_interface = pdata->phy;
3329793fc096SFrank Li 		else
3330793fc096SFrank Li 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
3331793fc096SFrank Li 	} else {
3332793fc096SFrank Li 		fep->phy_interface = ret;
3333793fc096SFrank Li 	}
3334793fc096SFrank Li 
3335793fc096SFrank Li 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3336793fc096SFrank Li 	if (IS_ERR(fep->clk_ipg)) {
3337793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ipg);
3338793fc096SFrank Li 		goto failed_clk;
3339793fc096SFrank Li 	}
3340793fc096SFrank Li 
3341793fc096SFrank Li 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3342793fc096SFrank Li 	if (IS_ERR(fep->clk_ahb)) {
3343793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ahb);
3344793fc096SFrank Li 		goto failed_clk;
3345793fc096SFrank Li 	}
3346793fc096SFrank Li 
3347d851b47bSFugang Duan 	fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3348d851b47bSFugang Duan 
334938f56f33SLinus Torvalds 	/* enet_out is optional, depends on board */
335038f56f33SLinus Torvalds 	fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
335138f56f33SLinus Torvalds 	if (IS_ERR(fep->clk_enet_out))
335238f56f33SLinus Torvalds 		fep->clk_enet_out = NULL;
335338f56f33SLinus Torvalds 
335491c0d987SNimrod Andy 	fep->ptp_clk_on = false;
335591c0d987SNimrod Andy 	mutex_init(&fep->ptp_clk_mutex);
33569b5330edSFugang Duan 
33579b5330edSFugang Duan 	/* clk_ref is optional, depends on board */
33589b5330edSFugang Duan 	fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
33599b5330edSFugang Duan 	if (IS_ERR(fep->clk_ref))
33609b5330edSFugang Duan 		fep->clk_ref = NULL;
33619b5330edSFugang Duan 
33626b7e4008SLothar Waßmann 	fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
3363793fc096SFrank Li 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3364793fc096SFrank Li 	if (IS_ERR(fep->clk_ptp)) {
336538f56f33SLinus Torvalds 		fep->clk_ptp = NULL;
3366217b5844SLothar Waßmann 		fep->bufdesc_ex = false;
3367793fc096SFrank Li 	}
3368793fc096SFrank Li 
3369e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
337013a097bdSFabio Estevam 	if (ret)
337113a097bdSFabio Estevam 		goto failed_clk;
337213a097bdSFabio Estevam 
33738fff755eSAndrew Lunn 	ret = clk_prepare_enable(fep->clk_ipg);
33748fff755eSAndrew Lunn 	if (ret)
33758fff755eSAndrew Lunn 		goto failed_clk_ipg;
33768fff755eSAndrew Lunn 
3377f4e9f3d2SFabio Estevam 	fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3378f4e9f3d2SFabio Estevam 	if (!IS_ERR(fep->reg_phy)) {
3379f4e9f3d2SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
3380793fc096SFrank Li 		if (ret) {
3381793fc096SFrank Li 			dev_err(&pdev->dev,
3382793fc096SFrank Li 				"Failed to enable phy regulator: %d\n", ret);
3383793fc096SFrank Li 			goto failed_regulator;
3384793fc096SFrank Li 		}
3385f6a4d607SFabio Estevam 	} else {
3386f6a4d607SFabio Estevam 		fep->reg_phy = NULL;
3387793fc096SFrank Li 	}
3388793fc096SFrank Li 
33898fff755eSAndrew Lunn 	pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
33908fff755eSAndrew Lunn 	pm_runtime_use_autosuspend(&pdev->dev);
339114d2b7c1SLucas Stach 	pm_runtime_get_noresume(&pdev->dev);
33928fff755eSAndrew Lunn 	pm_runtime_set_active(&pdev->dev);
33938fff755eSAndrew Lunn 	pm_runtime_enable(&pdev->dev);
33948fff755eSAndrew Lunn 
3395793fc096SFrank Li 	fec_reset_phy(pdev);
3396793fc096SFrank Li 
3397793fc096SFrank Li 	if (fep->bufdesc_ex)
3398ca162a82SFabio Estevam 		fec_ptp_init(pdev);
3399793fc096SFrank Li 
3400793fc096SFrank Li 	ret = fec_enet_init(ndev);
3401793fc096SFrank Li 	if (ret)
3402793fc096SFrank Li 		goto failed_init;
3403793fc096SFrank Li 
3404793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
3405793fc096SFrank Li 		irq = platform_get_irq(pdev, i);
3406793fc096SFrank Li 		if (irq < 0) {
3407793fc096SFrank Li 			if (i)
3408793fc096SFrank Li 				break;
3409793fc096SFrank Li 			ret = irq;
3410793fc096SFrank Li 			goto failed_irq;
3411793fc096SFrank Li 		}
34120d9b2ab1SFabio Estevam 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
341344a272ddSMichael Opdenacker 				       0, pdev->name, ndev);
34140d9b2ab1SFabio Estevam 		if (ret)
3415793fc096SFrank Li 			goto failed_irq;
3416de40ed31SNimrod Andy 
3417de40ed31SNimrod Andy 		fep->irq[i] = irq;
3418793fc096SFrank Li 	}
3419793fc096SFrank Li 
3420b4d39b53SFugang Duan 	init_completion(&fep->mdio_done);
3421793fc096SFrank Li 	ret = fec_enet_mii_init(pdev);
3422793fc096SFrank Li 	if (ret)
3423793fc096SFrank Li 		goto failed_mii_init;
3424793fc096SFrank Li 
3425793fc096SFrank Li 	/* Carrier starts down, phylib will bring it up */
3426793fc096SFrank Li 	netif_carrier_off(ndev);
3427e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
34285bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&pdev->dev);
3429793fc096SFrank Li 
3430793fc096SFrank Li 	ret = register_netdev(ndev);
3431793fc096SFrank Li 	if (ret)
3432793fc096SFrank Li 		goto failed_register;
3433793fc096SFrank Li 
3434de40ed31SNimrod Andy 	device_init_wakeup(&ndev->dev, fep->wol_flag &
3435de40ed31SNimrod Andy 			   FEC_WOL_HAS_MAGIC_PACKET);
3436de40ed31SNimrod Andy 
3437eb1d0640SFabio Estevam 	if (fep->bufdesc_ex && fep->ptp_clock)
3438eb1d0640SFabio Estevam 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3439eb1d0640SFabio Estevam 
34401b7bde6dSNimrod Andy 	fep->rx_copybreak = COPYBREAK_DEFAULT;
344136cdc743SRussell King 	INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
34428fff755eSAndrew Lunn 
34438fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&pdev->dev);
34448fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&pdev->dev);
34458fff755eSAndrew Lunn 
3446793fc096SFrank Li 	return 0;
3447793fc096SFrank Li 
3448793fc096SFrank Li failed_register:
3449793fc096SFrank Li 	fec_enet_mii_remove(fep);
3450793fc096SFrank Li failed_mii_init:
34517a2bbd8dSFabio Estevam failed_irq:
34527a2bbd8dSFabio Estevam failed_init:
345332cba57bSLucas Stach 	fec_ptp_stop(pdev);
3454f6a4d607SFabio Estevam 	if (fep->reg_phy)
3455f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
3456793fc096SFrank Li failed_regulator:
34578fff755eSAndrew Lunn 	clk_disable_unprepare(fep->clk_ipg);
34588fff755eSAndrew Lunn failed_clk_ipg:
3459e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
3460793fc096SFrank Li failed_clk:
3461407066f8SUwe Kleine-König failed_phy:
3462407066f8SUwe Kleine-König 	of_node_put(phy_node);
3463793fc096SFrank Li failed_ioremap:
3464793fc096SFrank Li 	free_netdev(ndev);
3465793fc096SFrank Li 
3466793fc096SFrank Li 	return ret;
3467793fc096SFrank Li }
3468793fc096SFrank Li 
3469793fc096SFrank Li static int
3470793fc096SFrank Li fec_drv_remove(struct platform_device *pdev)
3471793fc096SFrank Li {
3472793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
3473793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3474793fc096SFrank Li 
347536cdc743SRussell King 	cancel_work_sync(&fep->tx_timeout_work);
347632cba57bSLucas Stach 	fec_ptp_stop(pdev);
3477793fc096SFrank Li 	unregister_netdev(ndev);
3478793fc096SFrank Li 	fec_enet_mii_remove(fep);
3479f6a4d607SFabio Estevam 	if (fep->reg_phy)
3480f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
3481407066f8SUwe Kleine-König 	of_node_put(fep->phy_node);
3482793fc096SFrank Li 	free_netdev(ndev);
3483793fc096SFrank Li 
3484793fc096SFrank Li 	return 0;
3485793fc096SFrank Li }
3486793fc096SFrank Li 
3487dd66d386SFabio Estevam static int __maybe_unused fec_suspend(struct device *dev)
3488793fc096SFrank Li {
3489793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
3490793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3491793fc096SFrank Li 
3492da1774e5SRussell King 	rtnl_lock();
3493793fc096SFrank Li 	if (netif_running(ndev)) {
3494de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3495de40ed31SNimrod Andy 			fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
349645f5c327SPhilippe Reynes 		phy_stop(ndev->phydev);
349731a6de34SRussell King 		napi_disable(&fep->napi);
349831a6de34SRussell King 		netif_tx_lock_bh(ndev);
3499793fc096SFrank Li 		netif_device_detach(ndev);
350031a6de34SRussell King 		netif_tx_unlock_bh(ndev);
350131a6de34SRussell King 		fec_stop(ndev);
3502e8fcfcd5SNimrod Andy 		fec_enet_clk_enable(ndev, false);
3503de40ed31SNimrod Andy 		if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
35045bbde4d2SNimrod Andy 			pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3505f4c4a4e0SNimrod Andy 	}
3506f4c4a4e0SNimrod Andy 	rtnl_unlock();
3507793fc096SFrank Li 
3508de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3509238f7bc7SFabio Estevam 		regulator_disable(fep->reg_phy);
3510238f7bc7SFabio Estevam 
3511858eeb7dSNimrod Andy 	/* SOC supply clock to phy, when clock is disabled, phy link down
3512858eeb7dSNimrod Andy 	 * SOC control phy regulator, when regulator is disabled, phy link down
3513858eeb7dSNimrod Andy 	 */
3514858eeb7dSNimrod Andy 	if (fep->clk_enet_out || fep->reg_phy)
3515858eeb7dSNimrod Andy 		fep->link = 0;
3516858eeb7dSNimrod Andy 
3517793fc096SFrank Li 	return 0;
3518793fc096SFrank Li }
3519793fc096SFrank Li 
3520dd66d386SFabio Estevam static int __maybe_unused fec_resume(struct device *dev)
3521793fc096SFrank Li {
3522793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
3523793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3524de40ed31SNimrod Andy 	struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
3525238f7bc7SFabio Estevam 	int ret;
3526de40ed31SNimrod Andy 	int val;
3527238f7bc7SFabio Estevam 
3528de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
3529238f7bc7SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
3530238f7bc7SFabio Estevam 		if (ret)
3531238f7bc7SFabio Estevam 			return ret;
3532238f7bc7SFabio Estevam 	}
3533793fc096SFrank Li 
3534da1774e5SRussell King 	rtnl_lock();
3535793fc096SFrank Li 	if (netif_running(ndev)) {
3536f4c4a4e0SNimrod Andy 		ret = fec_enet_clk_enable(ndev, true);
3537f4c4a4e0SNimrod Andy 		if (ret) {
3538f4c4a4e0SNimrod Andy 			rtnl_unlock();
3539f4c4a4e0SNimrod Andy 			goto failed_clk;
3540f4c4a4e0SNimrod Andy 		}
3541de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3542de40ed31SNimrod Andy 			if (pdata && pdata->sleep_mode_enable)
3543de40ed31SNimrod Andy 				pdata->sleep_mode_enable(false);
3544de40ed31SNimrod Andy 			val = readl(fep->hwp + FEC_ECNTRL);
3545de40ed31SNimrod Andy 			val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3546de40ed31SNimrod Andy 			writel(val, fep->hwp + FEC_ECNTRL);
3547de40ed31SNimrod Andy 			fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3548de40ed31SNimrod Andy 		} else {
3549de40ed31SNimrod Andy 			pinctrl_pm_select_default_state(&fep->pdev->dev);
3550de40ed31SNimrod Andy 		}
3551ef83337dSRussell King 		fec_restart(ndev);
355231a6de34SRussell King 		netif_tx_lock_bh(ndev);
3553793fc096SFrank Li 		netif_device_attach(ndev);
35546af42d42SRussell King 		netif_tx_unlock_bh(ndev);
35556af42d42SRussell King 		napi_enable(&fep->napi);
355645f5c327SPhilippe Reynes 		phy_start(ndev->phydev);
3557793fc096SFrank Li 	}
3558da1774e5SRussell King 	rtnl_unlock();
3559793fc096SFrank Li 
3560793fc096SFrank Li 	return 0;
356113a097bdSFabio Estevam 
3562e8fcfcd5SNimrod Andy failed_clk:
356313a097bdSFabio Estevam 	if (fep->reg_phy)
356413a097bdSFabio Estevam 		regulator_disable(fep->reg_phy);
356513a097bdSFabio Estevam 	return ret;
3566793fc096SFrank Li }
3567793fc096SFrank Li 
35688fff755eSAndrew Lunn static int __maybe_unused fec_runtime_suspend(struct device *dev)
35698fff755eSAndrew Lunn {
35708fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
35718fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
35728fff755eSAndrew Lunn 
35738fff755eSAndrew Lunn 	clk_disable_unprepare(fep->clk_ipg);
35748fff755eSAndrew Lunn 
35758fff755eSAndrew Lunn 	return 0;
35768fff755eSAndrew Lunn }
35778fff755eSAndrew Lunn 
35788fff755eSAndrew Lunn static int __maybe_unused fec_runtime_resume(struct device *dev)
35798fff755eSAndrew Lunn {
35808fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
35818fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
35828fff755eSAndrew Lunn 
35838fff755eSAndrew Lunn 	return clk_prepare_enable(fep->clk_ipg);
35848fff755eSAndrew Lunn }
35858fff755eSAndrew Lunn 
35868fff755eSAndrew Lunn static const struct dev_pm_ops fec_pm_ops = {
35878fff755eSAndrew Lunn 	SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
35888fff755eSAndrew Lunn 	SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
35898fff755eSAndrew Lunn };
3590793fc096SFrank Li 
3591793fc096SFrank Li static struct platform_driver fec_driver = {
3592793fc096SFrank Li 	.driver	= {
3593793fc096SFrank Li 		.name	= DRIVER_NAME,
3594793fc096SFrank Li 		.pm	= &fec_pm_ops,
3595793fc096SFrank Li 		.of_match_table = fec_dt_ids,
3596793fc096SFrank Li 	},
3597793fc096SFrank Li 	.id_table = fec_devtype,
3598793fc096SFrank Li 	.probe	= fec_probe,
3599793fc096SFrank Li 	.remove	= fec_drv_remove,
3600793fc096SFrank Li };
3601793fc096SFrank Li 
3602793fc096SFrank Li module_platform_driver(fec_driver);
3603793fc096SFrank Li 
3604f8c0aca9SFabio Estevam MODULE_ALIAS("platform:"DRIVER_NAME);
3605793fc096SFrank Li MODULE_LICENSE("GPL");
3606