xref: /linux/drivers/net/ethernet/freescale/fec_main.c (revision a05e4c0af490ca7c22fc77120aafebebdeaaf537)
11f508124SFabio Estevam // SPDX-License-Identifier: GPL-2.0+
2793fc096SFrank Li /*
3793fc096SFrank Li  * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4793fc096SFrank Li  * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5793fc096SFrank Li  *
6793fc096SFrank Li  * Right now, I am very wasteful with the buffers.  I allocate memory
7793fc096SFrank Li  * pages and then divide them into 2K frame buffers.  This way I know I
8793fc096SFrank Li  * have buffers large enough to hold one frame within one buffer descriptor.
9793fc096SFrank Li  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
10793fc096SFrank Li  * will be much more memory efficient and will easily handle lots of
11793fc096SFrank Li  * small packets.
12793fc096SFrank Li  *
13793fc096SFrank Li  * Much better multiple PHY support by Magnus Damm.
14793fc096SFrank Li  * Copyright (c) 2000 Ericsson Radio Systems AB.
15793fc096SFrank Li  *
16793fc096SFrank Li  * Support for FEC controller of ColdFire processors.
17793fc096SFrank Li  * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
18793fc096SFrank Li  *
19793fc096SFrank Li  * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
20793fc096SFrank Li  * Copyright (c) 2004-2006 Macq Electronique SA.
21793fc096SFrank Li  *
22793fc096SFrank Li  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
23793fc096SFrank Li  */
24793fc096SFrank Li 
25793fc096SFrank Li #include <linux/module.h>
26793fc096SFrank Li #include <linux/kernel.h>
27793fc096SFrank Li #include <linux/string.h>
288fff755eSAndrew Lunn #include <linux/pm_runtime.h>
29793fc096SFrank Li #include <linux/ptrace.h>
30793fc096SFrank Li #include <linux/errno.h>
31793fc096SFrank Li #include <linux/ioport.h>
32793fc096SFrank Li #include <linux/slab.h>
33793fc096SFrank Li #include <linux/interrupt.h>
34793fc096SFrank Li #include <linux/delay.h>
35793fc096SFrank Li #include <linux/netdevice.h>
36793fc096SFrank Li #include <linux/etherdevice.h>
37793fc096SFrank Li #include <linux/skbuff.h>
384c09eed9SJim Baxter #include <linux/in.h>
394c09eed9SJim Baxter #include <linux/ip.h>
404c09eed9SJim Baxter #include <net/ip.h>
416016ba34SOleksij Rempel #include <net/selftests.h>
4279f33912SNimrod Andy #include <net/tso.h>
434c09eed9SJim Baxter #include <linux/tcp.h>
444c09eed9SJim Baxter #include <linux/udp.h>
454c09eed9SJim Baxter #include <linux/icmp.h>
46793fc096SFrank Li #include <linux/spinlock.h>
47793fc096SFrank Li #include <linux/workqueue.h>
48793fc096SFrank Li #include <linux/bitops.h>
49793fc096SFrank Li #include <linux/io.h>
50793fc096SFrank Li #include <linux/irq.h>
51793fc096SFrank Li #include <linux/clk.h>
5216f6e983SKrzysztof Kozlowski #include <linux/crc32.h>
53793fc096SFrank Li #include <linux/platform_device.h>
547f854420SAndrew Lunn #include <linux/mdio.h>
55793fc096SFrank Li #include <linux/phy.h>
56793fc096SFrank Li #include <linux/fec.h>
57793fc096SFrank Li #include <linux/of.h>
58793fc096SFrank Li #include <linux/of_device.h>
59793fc096SFrank Li #include <linux/of_gpio.h>
60407066f8SUwe Kleine-König #include <linux/of_mdio.h>
61793fc096SFrank Li #include <linux/of_net.h>
62793fc096SFrank Li #include <linux/regulator/consumer.h>
63cdffcf1bSJim Baxter #include <linux/if_vlan.h>
64a68ab98eSFabio Estevam #include <linux/pinctrl/consumer.h>
65c259c132SFrank Li #include <linux/prefetch.h>
66da722186SMartin Fuzzey #include <linux/mfd/syscon.h>
67da722186SMartin Fuzzey #include <linux/regmap.h>
6829380905SLucas Stach #include <soc/imx/cpuidle.h>
69793fc096SFrank Li 
70793fc096SFrank Li #include <asm/cacheflush.h>
71793fc096SFrank Li 
72793fc096SFrank Li #include "fec.h"
73793fc096SFrank Li 
74772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev);
75d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev);
76772e42b0SChristoph Müllner 
77793fc096SFrank Li #define DRIVER_NAME	"fec"
78793fc096SFrank Li 
7952c4a1a8SFugang Duan static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2};
8052c4a1a8SFugang Duan 
81793fc096SFrank Li /* Pause frame feild and FIFO threshold */
82793fc096SFrank Li #define FEC_ENET_FCE	(1 << 5)
83793fc096SFrank Li #define FEC_ENET_RSEM_V	0x84
84793fc096SFrank Li #define FEC_ENET_RSFL_V	16
85793fc096SFrank Li #define FEC_ENET_RAEM_V	0x8
86793fc096SFrank Li #define FEC_ENET_RAFL_V	0x8
87793fc096SFrank Li #define FEC_ENET_OPD_V	0xFFF0
888fff755eSAndrew Lunn #define FEC_MDIO_PM_TIMEOUT  100 /* ms */
89793fc096SFrank Li 
90da722186SMartin Fuzzey struct fec_devinfo {
91da722186SMartin Fuzzey 	u32 quirks;
92da722186SMartin Fuzzey };
93da722186SMartin Fuzzey 
94da722186SMartin Fuzzey static const struct fec_devinfo fec_imx25_info = {
95da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
96da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_FRREG,
97da722186SMartin Fuzzey };
98da722186SMartin Fuzzey 
99da722186SMartin Fuzzey static const struct fec_devinfo fec_imx27_info = {
100da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG,
101da722186SMartin Fuzzey };
102da722186SMartin Fuzzey 
103da722186SMartin Fuzzey static const struct fec_devinfo fec_imx28_info = {
104da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
105da722186SMartin Fuzzey 		  FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
106c730ab42SLaurent Badel 		  FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII |
107c730ab42SLaurent Badel 		  FEC_QUIRK_NO_HARD_RESET,
108da722186SMartin Fuzzey };
109da722186SMartin Fuzzey 
110da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6q_info = {
111da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
112da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
113da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
1141e6114f5SGreg Ungerer 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII,
115da722186SMartin Fuzzey };
116da722186SMartin Fuzzey 
117da722186SMartin Fuzzey static const struct fec_devinfo fec_mvf600_info = {
118da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
119da722186SMartin Fuzzey };
120da722186SMartin Fuzzey 
121da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6x_info = {
122da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
123da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
124da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
125da722186SMartin Fuzzey 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
1261e6114f5SGreg Ungerer 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
127471ff445SJoakim Zhang 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES,
128da722186SMartin Fuzzey };
129da722186SMartin Fuzzey 
130da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6ul_info = {
131da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
132da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
133da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
134da722186SMartin Fuzzey 		  FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
1351e6114f5SGreg Ungerer 		  FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII,
136da722186SMartin Fuzzey };
137da722186SMartin Fuzzey 
138947240ebSFugang Duan static const struct fec_devinfo fec_imx8mq_info = {
139947240ebSFugang Duan 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
140947240ebSFugang Duan 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
141947240ebSFugang Duan 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
142947240ebSFugang Duan 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
143947240ebSFugang Duan 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
144947240ebSFugang Duan 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
145b7cdc965SJoakim Zhang 		  FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2,
146947240ebSFugang Duan };
147947240ebSFugang Duan 
148947240ebSFugang Duan static const struct fec_devinfo fec_imx8qm_info = {
149947240ebSFugang Duan 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
150947240ebSFugang Duan 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
151947240ebSFugang Duan 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
152947240ebSFugang Duan 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
153947240ebSFugang Duan 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
154947240ebSFugang Duan 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
155947240ebSFugang Duan 		  FEC_QUIRK_DELAYED_CLKS_SUPPORT,
156947240ebSFugang Duan };
157947240ebSFugang Duan 
158793fc096SFrank Li static struct platform_device_id fec_devtype[] = {
159793fc096SFrank Li 	{
160793fc096SFrank Li 		/* keep it for coldfire */
161793fc096SFrank Li 		.name = DRIVER_NAME,
162793fc096SFrank Li 		.driver_data = 0,
163793fc096SFrank Li 	}, {
164793fc096SFrank Li 		.name = "imx25-fec",
165da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx25_info,
166793fc096SFrank Li 	}, {
167793fc096SFrank Li 		.name = "imx27-fec",
168da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx27_info,
169793fc096SFrank Li 	}, {
170793fc096SFrank Li 		.name = "imx28-fec",
171da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx28_info,
172793fc096SFrank Li 	}, {
173793fc096SFrank Li 		.name = "imx6q-fec",
174da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6q_info,
175793fc096SFrank Li 	}, {
17636803542SShawn Guo 		.name = "mvf600-fec",
177da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_mvf600_info,
178ca7c4a45SJingchang Lu 	}, {
17995a77470SFugang Duan 		.name = "imx6sx-fec",
180da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6x_info,
18195a77470SFugang Duan 	}, {
182a51d3ab5SFugang Duan 		.name = "imx6ul-fec",
183da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6ul_info,
184a51d3ab5SFugang Duan 	}, {
185947240ebSFugang Duan 		.name = "imx8mq-fec",
186947240ebSFugang Duan 		.driver_data = (kernel_ulong_t)&fec_imx8mq_info,
187947240ebSFugang Duan 	}, {
188947240ebSFugang Duan 		.name = "imx8qm-fec",
189947240ebSFugang Duan 		.driver_data = (kernel_ulong_t)&fec_imx8qm_info,
190947240ebSFugang Duan 	}, {
191793fc096SFrank Li 		/* sentinel */
192793fc096SFrank Li 	}
193793fc096SFrank Li };
194793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype);
195793fc096SFrank Li 
196793fc096SFrank Li enum imx_fec_type {
197793fc096SFrank Li 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
198793fc096SFrank Li 	IMX27_FEC,	/* runs on i.mx27/35/51 */
199793fc096SFrank Li 	IMX28_FEC,
200793fc096SFrank Li 	IMX6Q_FEC,
20136803542SShawn Guo 	MVF600_FEC,
202ba593e00SFugang Duan 	IMX6SX_FEC,
203a51d3ab5SFugang Duan 	IMX6UL_FEC,
204947240ebSFugang Duan 	IMX8MQ_FEC,
205947240ebSFugang Duan 	IMX8QM_FEC,
206793fc096SFrank Li };
207793fc096SFrank Li 
208793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = {
209793fc096SFrank Li 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
210793fc096SFrank Li 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
211793fc096SFrank Li 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
212793fc096SFrank Li 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
21336803542SShawn Guo 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
214ba593e00SFugang Duan 	{ .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
215a51d3ab5SFugang Duan 	{ .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
216947240ebSFugang Duan 	{ .compatible = "fsl,imx8mq-fec", .data = &fec_devtype[IMX8MQ_FEC], },
217947240ebSFugang Duan 	{ .compatible = "fsl,imx8qm-fec", .data = &fec_devtype[IMX8QM_FEC], },
218793fc096SFrank Li 	{ /* sentinel */ }
219793fc096SFrank Li };
220793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids);
221793fc096SFrank Li 
222793fc096SFrank Li static unsigned char macaddr[ETH_ALEN];
223793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0);
224793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
225793fc096SFrank Li 
226793fc096SFrank Li #if defined(CONFIG_M5272)
227793fc096SFrank Li /*
228793fc096SFrank Li  * Some hardware gets it MAC address out of local flash memory.
229793fc096SFrank Li  * if this is non-zero then assume it is the address to get MAC from.
230793fc096SFrank Li  */
231793fc096SFrank Li #if defined(CONFIG_NETtel)
232793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006006
233793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
234793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006000
235793fc096SFrank Li #elif defined(CONFIG_CANCam)
236793fc096SFrank Li #define	FEC_FLASHMAC	0xf0020000
237793fc096SFrank Li #elif defined (CONFIG_M5272C3)
238793fc096SFrank Li #define	FEC_FLASHMAC	(0xffe04000 + 4)
239793fc096SFrank Li #elif defined(CONFIG_MOD5272)
240793fc096SFrank Li #define FEC_FLASHMAC	0xffc0406b
241793fc096SFrank Li #else
242793fc096SFrank Li #define	FEC_FLASHMAC	0
243793fc096SFrank Li #endif
244793fc096SFrank Li #endif /* CONFIG_M5272 */
245793fc096SFrank Li 
246cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
247fbbeefddSAndrew Lunn  *
248fbbeefddSAndrew Lunn  * 2048 byte skbufs are allocated. However, alignment requirements
249fbbeefddSAndrew Lunn  * varies between FEC variants. Worst case is 64, so round down by 64.
250793fc096SFrank Li  */
251fbbeefddSAndrew Lunn #define PKT_MAXBUF_SIZE		(round_down(2048 - 64, 64))
252793fc096SFrank Li #define PKT_MINBUF_SIZE		64
253793fc096SFrank Li 
2544c09eed9SJim Baxter /* FEC receive acceleration */
2554c09eed9SJim Baxter #define FEC_RACC_IPDIS		(1 << 1)
2564c09eed9SJim Baxter #define FEC_RACC_PRODIS		(1 << 2)
2573ac72b7bSEric Nelson #define FEC_RACC_SHIFT16	BIT(7)
2584c09eed9SJim Baxter #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
2594c09eed9SJim Baxter 
2602b30842bSAndrew Lunn /* MIB Control Register */
2612b30842bSAndrew Lunn #define FEC_MIB_CTRLSTAT_DISABLE	BIT(31)
2622b30842bSAndrew Lunn 
263793fc096SFrank Li /*
264793fc096SFrank Li  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
265793fc096SFrank Li  * size bits. Other FEC hardware does not, so we need to take that into
266793fc096SFrank Li  * account when setting it.
267793fc096SFrank Li  */
268793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2693f1dcc6aSLucas Stach     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
2703f1dcc6aSLucas Stach     defined(CONFIG_ARM64)
271793fc096SFrank Li #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
272793fc096SFrank Li #else
273793fc096SFrank Li #define	OPT_FRAME_SIZE	0
274793fc096SFrank Li #endif
275793fc096SFrank Li 
276793fc096SFrank Li /* FEC MII MMFR bits definition */
277793fc096SFrank Li #define FEC_MMFR_ST		(1 << 30)
278d3ee8ec7SMarco Hartmann #define FEC_MMFR_ST_C45		(0)
279793fc096SFrank Li #define FEC_MMFR_OP_READ	(2 << 28)
280d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_READ_C45	(3 << 28)
281793fc096SFrank Li #define FEC_MMFR_OP_WRITE	(1 << 28)
282d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_ADDR_WRITE	(0)
283793fc096SFrank Li #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
284793fc096SFrank Li #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
285793fc096SFrank Li #define FEC_MMFR_TA		(2 << 16)
286793fc096SFrank Li #define FEC_MMFR_DATA(v)	(v & 0xffff)
287de40ed31SNimrod Andy /* FEC ECR bits definition */
288de40ed31SNimrod Andy #define FEC_ECR_MAGICEN		(1 << 2)
289de40ed31SNimrod Andy #define FEC_ECR_SLEEP		(1 << 3)
290793fc096SFrank Li 
291793fc096SFrank Li #define FEC_MII_TIMEOUT		30000 /* us */
292793fc096SFrank Li 
293793fc096SFrank Li /* Transmitter timeout */
294793fc096SFrank Li #define TX_TIMEOUT (2 * HZ)
295793fc096SFrank Li 
296793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG	0x1
297793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE	0x2
298de40ed31SNimrod Andy #define FEC_WOL_HAS_MAGIC_PACKET	(0x1 << 0)
299de40ed31SNimrod Andy #define FEC_WOL_FLAG_ENABLE		(0x1 << 1)
300de40ed31SNimrod Andy #define FEC_WOL_FLAG_SLEEP_ON		(0x1 << 2)
301793fc096SFrank Li 
3021b7bde6dSNimrod Andy #define COPYBREAK_DEFAULT	256
3031b7bde6dSNimrod Andy 
30479f33912SNimrod Andy /* Max number of allowed TCP segments for software TSO */
30579f33912SNimrod Andy #define FEC_MAX_TSO_SEGS	100
30679f33912SNimrod Andy #define FEC_MAX_SKB_DESCS	(FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
30779f33912SNimrod Andy 
30879f33912SNimrod Andy #define IS_TSO_HEADER(txq, addr) \
30979f33912SNimrod Andy 	((addr >= txq->tso_hdrs_dma) && \
3107355f276STroy Kisky 	(addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
31179f33912SNimrod Andy 
312793fc096SFrank Li static int mii_cnt;
313793fc096SFrank Li 
3147355f276STroy Kisky static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
3157355f276STroy Kisky 					     struct bufdesc_prop *bd)
316793fc096SFrank Li {
3177355f276STroy Kisky 	return (bdp >= bd->last) ? bd->base
318145d6e29SFugang Duan 			: (struct bufdesc *)(((void *)bdp) + bd->dsize);
319793fc096SFrank Li }
320793fc096SFrank Li 
3217355f276STroy Kisky static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
3227355f276STroy Kisky 					     struct bufdesc_prop *bd)
32336e24e2eSDuan Fugang-B38611 {
3247355f276STroy Kisky 	return (bdp <= bd->base) ? bd->last
325145d6e29SFugang Duan 			: (struct bufdesc *)(((void *)bdp) - bd->dsize);
32636e24e2eSDuan Fugang-B38611 }
32736e24e2eSDuan Fugang-B38611 
3287355f276STroy Kisky static int fec_enet_get_bd_index(struct bufdesc *bdp,
3297355f276STroy Kisky 				 struct bufdesc_prop *bd)
33061a4427bSNimrod Andy {
3317355f276STroy Kisky 	return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
33261a4427bSNimrod Andy }
33361a4427bSNimrod Andy 
3347355f276STroy Kisky static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
3356e909283SNimrod Andy {
3366e909283SNimrod Andy 	int entries;
3376e909283SNimrod Andy 
3387355f276STroy Kisky 	entries = (((const char *)txq->dirty_tx -
3397355f276STroy Kisky 			(const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
3406e909283SNimrod Andy 
3417355f276STroy Kisky 	return entries >= 0 ? entries : entries + txq->bd.ring_size;
3426e909283SNimrod Andy }
3436e909283SNimrod Andy 
344c20e599bSLothar Waßmann static void swap_buffer(void *bufaddr, int len)
345793fc096SFrank Li {
346793fc096SFrank Li 	int i;
347793fc096SFrank Li 	unsigned int *buf = bufaddr;
348793fc096SFrank Li 
3497b487d07SLothar Waßmann 	for (i = 0; i < len; i += 4, buf++)
350e453789aSLothar Waßmann 		swab32s(buf);
351793fc096SFrank Li }
352793fc096SFrank Li 
3531310b544SLothar Waßmann static void swap_buffer2(void *dst_buf, void *src_buf, int len)
3541310b544SLothar Waßmann {
3551310b544SLothar Waßmann 	int i;
3561310b544SLothar Waßmann 	unsigned int *src = src_buf;
3571310b544SLothar Waßmann 	unsigned int *dst = dst_buf;
3581310b544SLothar Waßmann 
3591310b544SLothar Waßmann 	for (i = 0; i < len; i += 4, src++, dst++)
3601310b544SLothar Waßmann 		*dst = swab32p(src);
3611310b544SLothar Waßmann }
3621310b544SLothar Waßmann 
363344756f6SRussell King static void fec_dump(struct net_device *ndev)
364344756f6SRussell King {
365344756f6SRussell King 	struct fec_enet_private *fep = netdev_priv(ndev);
3664d494cdcSFugang Duan 	struct bufdesc *bdp;
3674d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
3684d494cdcSFugang Duan 	int index = 0;
369344756f6SRussell King 
370344756f6SRussell King 	netdev_info(ndev, "TX ring dump\n");
371344756f6SRussell King 	pr_info("Nr     SC     addr       len  SKB\n");
372344756f6SRussell King 
3734d494cdcSFugang Duan 	txq = fep->tx_queue[0];
3747355f276STroy Kisky 	bdp = txq->bd.base;
3754d494cdcSFugang Duan 
376344756f6SRussell King 	do {
3775cfa3039SJohannes Berg 		pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
378344756f6SRussell King 			index,
3797355f276STroy Kisky 			bdp == txq->bd.cur ? 'S' : ' ',
3804d494cdcSFugang Duan 			bdp == txq->dirty_tx ? 'H' : ' ',
3815cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_sc),
3825cfa3039SJohannes Berg 			fec32_to_cpu(bdp->cbd_bufaddr),
3835cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_datlen),
3844d494cdcSFugang Duan 			txq->tx_skbuff[index]);
3857355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
386344756f6SRussell King 		index++;
3877355f276STroy Kisky 	} while (bdp != txq->bd.base);
388344756f6SRussell King }
389344756f6SRussell King 
39062a02c98SFugang Duan static inline bool is_ipv4_pkt(struct sk_buff *skb)
39162a02c98SFugang Duan {
39262a02c98SFugang Duan 	return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
39362a02c98SFugang Duan }
39462a02c98SFugang Duan 
3954c09eed9SJim Baxter static int
3964c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
3974c09eed9SJim Baxter {
3984c09eed9SJim Baxter 	/* Only run for packets requiring a checksum. */
3994c09eed9SJim Baxter 	if (skb->ip_summed != CHECKSUM_PARTIAL)
4004c09eed9SJim Baxter 		return 0;
4014c09eed9SJim Baxter 
4024c09eed9SJim Baxter 	if (unlikely(skb_cow_head(skb, 0)))
4034c09eed9SJim Baxter 		return -1;
4044c09eed9SJim Baxter 
40562a02c98SFugang Duan 	if (is_ipv4_pkt(skb))
40696c50caaSNimrod Andy 		ip_hdr(skb)->check = 0;
4074c09eed9SJim Baxter 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
4084c09eed9SJim Baxter 
4094c09eed9SJim Baxter 	return 0;
4104c09eed9SJim Baxter }
4114c09eed9SJim Baxter 
412c4bc44c6SKevin Hao static struct bufdesc *
4134d494cdcSFugang Duan fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
4144d494cdcSFugang Duan 			     struct sk_buff *skb,
4154d494cdcSFugang Duan 			     struct net_device *ndev)
4166e909283SNimrod Andy {
4176e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
4187355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
4196e909283SNimrod Andy 	struct bufdesc_ex *ebdp;
4206e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
4216e909283SNimrod Andy 	int frag, frag_len;
4226e909283SNimrod Andy 	unsigned short status;
4236e909283SNimrod Andy 	unsigned int estatus = 0;
4246e909283SNimrod Andy 	skb_frag_t *this_frag;
4256e909283SNimrod Andy 	unsigned int index;
4266e909283SNimrod Andy 	void *bufaddr;
427d6bf3143SRussell King 	dma_addr_t addr;
4286e909283SNimrod Andy 	int i;
4296e909283SNimrod Andy 
4306e909283SNimrod Andy 	for (frag = 0; frag < nr_frags; frag++) {
4316e909283SNimrod Andy 		this_frag = &skb_shinfo(skb)->frags[frag];
4327355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
4336e909283SNimrod Andy 		ebdp = (struct bufdesc_ex *)bdp;
4346e909283SNimrod Andy 
4355cfa3039SJohannes Berg 		status = fec16_to_cpu(bdp->cbd_sc);
4366e909283SNimrod Andy 		status &= ~BD_ENET_TX_STATS;
4376e909283SNimrod Andy 		status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
438d7840976SMatthew Wilcox (Oracle) 		frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
4396e909283SNimrod Andy 
4406e909283SNimrod Andy 		/* Handle the last BD specially */
4416e909283SNimrod Andy 		if (frag == nr_frags - 1) {
4426e909283SNimrod Andy 			status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
4436e909283SNimrod Andy 			if (fep->bufdesc_ex) {
4446e909283SNimrod Andy 				estatus |= BD_ENET_TX_INT;
4456e909283SNimrod Andy 				if (unlikely(skb_shinfo(skb)->tx_flags &
4466e909283SNimrod Andy 					SKBTX_HW_TSTAMP && fep->hwts_tx_en))
4476e909283SNimrod Andy 					estatus |= BD_ENET_TX_TS;
4486e909283SNimrod Andy 			}
4496e909283SNimrod Andy 		}
4506e909283SNimrod Andy 
4516e909283SNimrod Andy 		if (fep->bufdesc_ex) {
4526b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_HAS_AVB)
45353bb20d1STroy Kisky 				estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
4546e909283SNimrod Andy 			if (skb->ip_summed == CHECKSUM_PARTIAL)
4556e909283SNimrod Andy 				estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
456471ff445SJoakim Zhang 
4576e909283SNimrod Andy 			ebdp->cbd_bdu = 0;
4585cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(estatus);
4596e909283SNimrod Andy 		}
4606e909283SNimrod Andy 
461d7840976SMatthew Wilcox (Oracle) 		bufaddr = skb_frag_address(this_frag);
4626e909283SNimrod Andy 
4637355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
46441ef84ceSFugang Duan 		if (((unsigned long) bufaddr) & fep->tx_align ||
4656b7e4008SLothar Waßmann 			fep->quirks & FEC_QUIRK_SWAP_FRAME) {
4664d494cdcSFugang Duan 			memcpy(txq->tx_bounce[index], bufaddr, frag_len);
4674d494cdcSFugang Duan 			bufaddr = txq->tx_bounce[index];
4686e909283SNimrod Andy 
4696b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
4706e909283SNimrod Andy 				swap_buffer(bufaddr, frag_len);
4716e909283SNimrod Andy 		}
4726e909283SNimrod Andy 
473d6bf3143SRussell King 		addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
474d6bf3143SRussell King 				      DMA_TO_DEVICE);
475d6bf3143SRussell King 		if (dma_mapping_error(&fep->pdev->dev, addr)) {
4766e909283SNimrod Andy 			if (net_ratelimit())
4776e909283SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
4786e909283SNimrod Andy 			goto dma_mapping_error;
4796e909283SNimrod Andy 		}
4806e909283SNimrod Andy 
4815cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(addr);
4825cfa3039SJohannes Berg 		bdp->cbd_datlen = cpu_to_fec16(frag_len);
483be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
484be293467STroy Kisky 		 * performed before transferring ownership.
485be293467STroy Kisky 		 */
486be293467STroy Kisky 		wmb();
4875cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(status);
4886e909283SNimrod Andy 	}
4896e909283SNimrod Andy 
490c4bc44c6SKevin Hao 	return bdp;
4916e909283SNimrod Andy dma_mapping_error:
4927355f276STroy Kisky 	bdp = txq->bd.cur;
4936e909283SNimrod Andy 	for (i = 0; i < frag; i++) {
4947355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
4955cfa3039SJohannes Berg 		dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
4965cfa3039SJohannes Berg 				 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
4976e909283SNimrod Andy 	}
498c4bc44c6SKevin Hao 	return ERR_PTR(-ENOMEM);
4996e909283SNimrod Andy }
5006e909283SNimrod Andy 
5014d494cdcSFugang Duan static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
5024d494cdcSFugang Duan 				   struct sk_buff *skb, struct net_device *ndev)
5036e909283SNimrod Andy {
5046e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
5056e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
5066e909283SNimrod Andy 	struct bufdesc *bdp, *last_bdp;
5076e909283SNimrod Andy 	void *bufaddr;
508d6bf3143SRussell King 	dma_addr_t addr;
5096e909283SNimrod Andy 	unsigned short status;
5106e909283SNimrod Andy 	unsigned short buflen;
5116e909283SNimrod Andy 	unsigned int estatus = 0;
5126e909283SNimrod Andy 	unsigned int index;
51379f33912SNimrod Andy 	int entries_free;
5146e909283SNimrod Andy 
5157355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
51679f33912SNimrod Andy 	if (entries_free < MAX_SKB_FRAGS + 1) {
51779f33912SNimrod Andy 		dev_kfree_skb_any(skb);
51879f33912SNimrod Andy 		if (net_ratelimit())
51979f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for SG!\n");
52079f33912SNimrod Andy 		return NETDEV_TX_OK;
52179f33912SNimrod Andy 	}
52279f33912SNimrod Andy 
5236e909283SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
5246e909283SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
5256e909283SNimrod Andy 		dev_kfree_skb_any(skb);
5266e909283SNimrod Andy 		return NETDEV_TX_OK;
5276e909283SNimrod Andy 	}
5286e909283SNimrod Andy 
5296e909283SNimrod Andy 	/* Fill in a Tx ring entry */
5307355f276STroy Kisky 	bdp = txq->bd.cur;
531c4bc44c6SKevin Hao 	last_bdp = bdp;
5325cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
5336e909283SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
5346e909283SNimrod Andy 
5356e909283SNimrod Andy 	/* Set buffer length and buffer pointer */
5366e909283SNimrod Andy 	bufaddr = skb->data;
5376e909283SNimrod Andy 	buflen = skb_headlen(skb);
5386e909283SNimrod Andy 
5397355f276STroy Kisky 	index = fec_enet_get_bd_index(bdp, &txq->bd);
54041ef84ceSFugang Duan 	if (((unsigned long) bufaddr) & fep->tx_align ||
5416b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
5424d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, buflen);
5434d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
5446e909283SNimrod Andy 
5456b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
5466e909283SNimrod Andy 			swap_buffer(bufaddr, buflen);
5476e909283SNimrod Andy 	}
5486e909283SNimrod Andy 
549d6bf3143SRussell King 	/* Push the data cache so the CPM does not get stale memory data. */
550d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
551d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
5526e909283SNimrod Andy 		dev_kfree_skb_any(skb);
5536e909283SNimrod Andy 		if (net_ratelimit())
5546e909283SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
5556e909283SNimrod Andy 		return NETDEV_TX_OK;
5566e909283SNimrod Andy 	}
5576e909283SNimrod Andy 
5586e909283SNimrod Andy 	if (nr_frags) {
559c4bc44c6SKevin Hao 		last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
560fc75ba51STroy Kisky 		if (IS_ERR(last_bdp)) {
561fc75ba51STroy Kisky 			dma_unmap_single(&fep->pdev->dev, addr,
562fc75ba51STroy Kisky 					 buflen, DMA_TO_DEVICE);
563fc75ba51STroy Kisky 			dev_kfree_skb_any(skb);
564c4bc44c6SKevin Hao 			return NETDEV_TX_OK;
565fc75ba51STroy Kisky 		}
5666e909283SNimrod Andy 	} else {
5676e909283SNimrod Andy 		status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
5686e909283SNimrod Andy 		if (fep->bufdesc_ex) {
5696e909283SNimrod Andy 			estatus = BD_ENET_TX_INT;
5706e909283SNimrod Andy 			if (unlikely(skb_shinfo(skb)->tx_flags &
5716e909283SNimrod Andy 				SKBTX_HW_TSTAMP && fep->hwts_tx_en))
5726e909283SNimrod Andy 				estatus |= BD_ENET_TX_TS;
5736e909283SNimrod Andy 		}
5746e909283SNimrod Andy 	}
575fc75ba51STroy Kisky 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
576fc75ba51STroy Kisky 	bdp->cbd_datlen = cpu_to_fec16(buflen);
5776e909283SNimrod Andy 
5786e909283SNimrod Andy 	if (fep->bufdesc_ex) {
5796e909283SNimrod Andy 
5806e909283SNimrod Andy 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
5816e909283SNimrod Andy 
5826e909283SNimrod Andy 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
5836e909283SNimrod Andy 			fep->hwts_tx_en))
5846e909283SNimrod Andy 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
5856e909283SNimrod Andy 
5866b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
58753bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
588befe8213SNimrod Andy 
5896e909283SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
5906e909283SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
5916e909283SNimrod Andy 
5926e909283SNimrod Andy 		ebdp->cbd_bdu = 0;
5935cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
5946e909283SNimrod Andy 	}
5956e909283SNimrod Andy 
5967355f276STroy Kisky 	index = fec_enet_get_bd_index(last_bdp, &txq->bd);
5976e909283SNimrod Andy 	/* Save skb pointer */
5984d494cdcSFugang Duan 	txq->tx_skbuff[index] = skb;
5996e909283SNimrod Andy 
600be293467STroy Kisky 	/* Make sure the updates to rest of the descriptor are performed before
601be293467STroy Kisky 	 * transferring ownership.
602be293467STroy Kisky 	 */
603be293467STroy Kisky 	wmb();
6046e909283SNimrod Andy 
6056e909283SNimrod Andy 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
6066e909283SNimrod Andy 	 * it's the last BD of the frame, and to put the CRC on the end.
6076e909283SNimrod Andy 	 */
6086e909283SNimrod Andy 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
6095cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
6106e909283SNimrod Andy 
611793fc096SFrank Li 	/* If this was the last BD in the ring, start at the beginning again. */
6127355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
613793fc096SFrank Li 
6147a2a8451SEric Dumazet 	skb_tx_timestamp(skb);
6157a2a8451SEric Dumazet 
616c4bc44c6SKevin Hao 	/* Make sure the update to bdp and tx_skbuff are performed before
6177355f276STroy Kisky 	 * txq->bd.cur.
618c4bc44c6SKevin Hao 	 */
619c4bc44c6SKevin Hao 	wmb();
6207355f276STroy Kisky 	txq->bd.cur = bdp;
621793fc096SFrank Li 
622793fc096SFrank Li 	/* Trigger transmission start */
62353bb20d1STroy Kisky 	writel(0, txq->bd.reg_desc_active);
624793fc096SFrank Li 
6256e909283SNimrod Andy 	return 0;
626793fc096SFrank Li }
627793fc096SFrank Li 
62879f33912SNimrod Andy static int
6294d494cdcSFugang Duan fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
6304d494cdcSFugang Duan 			  struct net_device *ndev,
63179f33912SNimrod Andy 			  struct bufdesc *bdp, int index, char *data,
63279f33912SNimrod Andy 			  int size, bool last_tcp, bool is_last)
63379f33912SNimrod Andy {
63479f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
63561cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
63679f33912SNimrod Andy 	unsigned short status;
63779f33912SNimrod Andy 	unsigned int estatus = 0;
638d6bf3143SRussell King 	dma_addr_t addr;
63979f33912SNimrod Andy 
6405cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
64179f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
64279f33912SNimrod Andy 
64379f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
64479f33912SNimrod Andy 
64541ef84ceSFugang Duan 	if (((unsigned long) data) & fep->tx_align ||
6466b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
6474d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], data, size);
6484d494cdcSFugang Duan 		data = txq->tx_bounce[index];
64979f33912SNimrod Andy 
6506b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
65179f33912SNimrod Andy 			swap_buffer(data, size);
65279f33912SNimrod Andy 	}
65379f33912SNimrod Andy 
654d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
655d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
65679f33912SNimrod Andy 		dev_kfree_skb_any(skb);
65779f33912SNimrod Andy 		if (net_ratelimit())
65879f33912SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
65979f33912SNimrod Andy 		return NETDEV_TX_BUSY;
66079f33912SNimrod Andy 	}
66179f33912SNimrod Andy 
6625cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(size);
6635cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
664d6bf3143SRussell King 
66579f33912SNimrod Andy 	if (fep->bufdesc_ex) {
6666b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
66753bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
66879f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
66979f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
67079f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
6715cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
67279f33912SNimrod Andy 	}
67379f33912SNimrod Andy 
67479f33912SNimrod Andy 	/* Handle the last BD specially */
67579f33912SNimrod Andy 	if (last_tcp)
67679f33912SNimrod Andy 		status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
67779f33912SNimrod Andy 	if (is_last) {
67879f33912SNimrod Andy 		status |= BD_ENET_TX_INTR;
67979f33912SNimrod Andy 		if (fep->bufdesc_ex)
6805cfa3039SJohannes Berg 			ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
68179f33912SNimrod Andy 	}
68279f33912SNimrod Andy 
6835cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
68479f33912SNimrod Andy 
68579f33912SNimrod Andy 	return 0;
68679f33912SNimrod Andy }
68779f33912SNimrod Andy 
68879f33912SNimrod Andy static int
6894d494cdcSFugang Duan fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
6904d494cdcSFugang Duan 			 struct sk_buff *skb, struct net_device *ndev,
69179f33912SNimrod Andy 			 struct bufdesc *bdp, int index)
69279f33912SNimrod Andy {
69379f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
69479f33912SNimrod Andy 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
69561cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
69679f33912SNimrod Andy 	void *bufaddr;
69779f33912SNimrod Andy 	unsigned long dmabuf;
69879f33912SNimrod Andy 	unsigned short status;
69979f33912SNimrod Andy 	unsigned int estatus = 0;
70079f33912SNimrod Andy 
7015cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
70279f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
70379f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
70479f33912SNimrod Andy 
7054d494cdcSFugang Duan 	bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
7064d494cdcSFugang Duan 	dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
70741ef84ceSFugang Duan 	if (((unsigned long)bufaddr) & fep->tx_align ||
7086b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
7094d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, hdr_len);
7104d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
71179f33912SNimrod Andy 
7126b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
71379f33912SNimrod Andy 			swap_buffer(bufaddr, hdr_len);
71479f33912SNimrod Andy 
71579f33912SNimrod Andy 		dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
71679f33912SNimrod Andy 					hdr_len, DMA_TO_DEVICE);
71779f33912SNimrod Andy 		if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
71879f33912SNimrod Andy 			dev_kfree_skb_any(skb);
71979f33912SNimrod Andy 			if (net_ratelimit())
72079f33912SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
72179f33912SNimrod Andy 			return NETDEV_TX_BUSY;
72279f33912SNimrod Andy 		}
72379f33912SNimrod Andy 	}
72479f33912SNimrod Andy 
7255cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
7265cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(hdr_len);
72779f33912SNimrod Andy 
72879f33912SNimrod Andy 	if (fep->bufdesc_ex) {
7296b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
73053bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
73179f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
73279f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
73379f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
7345cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
73579f33912SNimrod Andy 	}
73679f33912SNimrod Andy 
7375cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
73879f33912SNimrod Andy 
73979f33912SNimrod Andy 	return 0;
74079f33912SNimrod Andy }
74179f33912SNimrod Andy 
7424d494cdcSFugang Duan static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
7434d494cdcSFugang Duan 				   struct sk_buff *skb,
7444d494cdcSFugang Duan 				   struct net_device *ndev)
74579f33912SNimrod Andy {
74679f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
747761b331cSEric Dumazet 	int hdr_len, total_len, data_left;
7487355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
74979f33912SNimrod Andy 	struct tso_t tso;
75079f33912SNimrod Andy 	unsigned int index = 0;
75179f33912SNimrod Andy 	int ret;
75279f33912SNimrod Andy 
7537355f276STroy Kisky 	if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
75479f33912SNimrod Andy 		dev_kfree_skb_any(skb);
75579f33912SNimrod Andy 		if (net_ratelimit())
75679f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for TSO!\n");
75779f33912SNimrod Andy 		return NETDEV_TX_OK;
75879f33912SNimrod Andy 	}
75979f33912SNimrod Andy 
76079f33912SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
76179f33912SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
76279f33912SNimrod Andy 		dev_kfree_skb_any(skb);
76379f33912SNimrod Andy 		return NETDEV_TX_OK;
76479f33912SNimrod Andy 	}
76579f33912SNimrod Andy 
76679f33912SNimrod Andy 	/* Initialize the TSO handler, and prepare the first payload */
767761b331cSEric Dumazet 	hdr_len = tso_start(skb, &tso);
76879f33912SNimrod Andy 
76979f33912SNimrod Andy 	total_len = skb->len - hdr_len;
77079f33912SNimrod Andy 	while (total_len > 0) {
77179f33912SNimrod Andy 		char *hdr;
77279f33912SNimrod Andy 
7737355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
77479f33912SNimrod Andy 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
77579f33912SNimrod Andy 		total_len -= data_left;
77679f33912SNimrod Andy 
77779f33912SNimrod Andy 		/* prepare packet headers: MAC + IP + TCP */
7784d494cdcSFugang Duan 		hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
77979f33912SNimrod Andy 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
7804d494cdcSFugang Duan 		ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
78179f33912SNimrod Andy 		if (ret)
78279f33912SNimrod Andy 			goto err_release;
78379f33912SNimrod Andy 
78479f33912SNimrod Andy 		while (data_left > 0) {
78579f33912SNimrod Andy 			int size;
78679f33912SNimrod Andy 
78779f33912SNimrod Andy 			size = min_t(int, tso.size, data_left);
7887355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
7897355f276STroy Kisky 			index = fec_enet_get_bd_index(bdp, &txq->bd);
7904d494cdcSFugang Duan 			ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
7914d494cdcSFugang Duan 							bdp, index,
7924d494cdcSFugang Duan 							tso.data, size,
7934d494cdcSFugang Duan 							size == data_left,
79479f33912SNimrod Andy 							total_len == 0);
79579f33912SNimrod Andy 			if (ret)
79679f33912SNimrod Andy 				goto err_release;
79779f33912SNimrod Andy 
79879f33912SNimrod Andy 			data_left -= size;
79979f33912SNimrod Andy 			tso_build_data(skb, &tso, size);
80079f33912SNimrod Andy 		}
80179f33912SNimrod Andy 
8027355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
80379f33912SNimrod Andy 	}
80479f33912SNimrod Andy 
80579f33912SNimrod Andy 	/* Save skb pointer */
8064d494cdcSFugang Duan 	txq->tx_skbuff[index] = skb;
80779f33912SNimrod Andy 
80879f33912SNimrod Andy 	skb_tx_timestamp(skb);
8097355f276STroy Kisky 	txq->bd.cur = bdp;
81079f33912SNimrod Andy 
81179f33912SNimrod Andy 	/* Trigger transmission start */
8126b7e4008SLothar Waßmann 	if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
81353bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
81453bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
81553bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
81653bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active))
81753bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
81879f33912SNimrod Andy 
81979f33912SNimrod Andy 	return 0;
82079f33912SNimrod Andy 
82179f33912SNimrod Andy err_release:
82279f33912SNimrod Andy 	/* TODO: Release all used data descriptors for TSO */
82379f33912SNimrod Andy 	return ret;
82479f33912SNimrod Andy }
82579f33912SNimrod Andy 
82661a4427bSNimrod Andy static netdev_tx_t
82761a4427bSNimrod Andy fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
82861a4427bSNimrod Andy {
82961a4427bSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
8306e909283SNimrod Andy 	int entries_free;
8314d494cdcSFugang Duan 	unsigned short queue;
8324d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
8334d494cdcSFugang Duan 	struct netdev_queue *nq;
83461a4427bSNimrod Andy 	int ret;
83561a4427bSNimrod Andy 
8364d494cdcSFugang Duan 	queue = skb_get_queue_mapping(skb);
8374d494cdcSFugang Duan 	txq = fep->tx_queue[queue];
8384d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue);
8394d494cdcSFugang Duan 
84079f33912SNimrod Andy 	if (skb_is_gso(skb))
8414d494cdcSFugang Duan 		ret = fec_enet_txq_submit_tso(txq, skb, ndev);
84279f33912SNimrod Andy 	else
8434d494cdcSFugang Duan 		ret = fec_enet_txq_submit_skb(txq, skb, ndev);
8446e909283SNimrod Andy 	if (ret)
8456e909283SNimrod Andy 		return ret;
84661a4427bSNimrod Andy 
8477355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
8484d494cdcSFugang Duan 	if (entries_free <= txq->tx_stop_threshold)
8494d494cdcSFugang Duan 		netif_tx_stop_queue(nq);
85061a4427bSNimrod Andy 
85161a4427bSNimrod Andy 	return NETDEV_TX_OK;
85261a4427bSNimrod Andy }
85361a4427bSNimrod Andy 
854a210576cSDavid S. Miller /* Init RX & TX buffer descriptors
855a210576cSDavid S. Miller  */
856a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev)
857a210576cSDavid S. Miller {
858a210576cSDavid S. Miller 	struct fec_enet_private *fep = netdev_priv(dev);
8594d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
8604d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
861a210576cSDavid S. Miller 	struct bufdesc *bdp;
862a210576cSDavid S. Miller 	unsigned int i;
86359d0f746SFrank Li 	unsigned int q;
864a210576cSDavid S. Miller 
86559d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
866a210576cSDavid S. Miller 		/* Initialize the receive buffer descriptors. */
86759d0f746SFrank Li 		rxq = fep->rx_queue[q];
8687355f276STroy Kisky 		bdp = rxq->bd.base;
8694d494cdcSFugang Duan 
8707355f276STroy Kisky 		for (i = 0; i < rxq->bd.ring_size; i++) {
871a210576cSDavid S. Miller 
872a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
873a210576cSDavid S. Miller 			if (bdp->cbd_bufaddr)
8745cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
875a210576cSDavid S. Miller 			else
8765cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(0);
8777355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
878a210576cSDavid S. Miller 		}
879a210576cSDavid S. Miller 
880a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
8817355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
8825cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
883a210576cSDavid S. Miller 
8847355f276STroy Kisky 		rxq->bd.cur = rxq->bd.base;
88559d0f746SFrank Li 	}
886a210576cSDavid S. Miller 
88759d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
888a210576cSDavid S. Miller 		/* ...and the same for transmit */
88959d0f746SFrank Li 		txq = fep->tx_queue[q];
8907355f276STroy Kisky 		bdp = txq->bd.base;
8917355f276STroy Kisky 		txq->bd.cur = bdp;
892a210576cSDavid S. Miller 
8937355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
894a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
8955cfa3039SJohannes Berg 			bdp->cbd_sc = cpu_to_fec16(0);
896178e5f57SFugang Duan 			if (bdp->cbd_bufaddr &&
897178e5f57SFugang Duan 			    !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
898178e5f57SFugang Duan 				dma_unmap_single(&fep->pdev->dev,
899178e5f57SFugang Duan 						 fec32_to_cpu(bdp->cbd_bufaddr),
900178e5f57SFugang Duan 						 fec16_to_cpu(bdp->cbd_datlen),
901178e5f57SFugang Duan 						 DMA_TO_DEVICE);
9024d494cdcSFugang Duan 			if (txq->tx_skbuff[i]) {
9034d494cdcSFugang Duan 				dev_kfree_skb_any(txq->tx_skbuff[i]);
9044d494cdcSFugang Duan 				txq->tx_skbuff[i] = NULL;
905a210576cSDavid S. Miller 			}
9065cfa3039SJohannes Berg 			bdp->cbd_bufaddr = cpu_to_fec32(0);
9077355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
908a210576cSDavid S. Miller 		}
909a210576cSDavid S. Miller 
910a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
9117355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
9125cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
9134d494cdcSFugang Duan 		txq->dirty_tx = bdp;
914a210576cSDavid S. Miller 	}
91559d0f746SFrank Li }
91659d0f746SFrank Li 
917ce99d0d3SFrank Li static void fec_enet_active_rxring(struct net_device *ndev)
918ce99d0d3SFrank Li {
919ce99d0d3SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
920ce99d0d3SFrank Li 	int i;
921ce99d0d3SFrank Li 
922ce99d0d3SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
92353bb20d1STroy Kisky 		writel(0, fep->rx_queue[i]->bd.reg_desc_active);
924ce99d0d3SFrank Li }
925ce99d0d3SFrank Li 
92659d0f746SFrank Li static void fec_enet_enable_ring(struct net_device *ndev)
92759d0f746SFrank Li {
92859d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
92959d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
93059d0f746SFrank Li 	struct fec_enet_priv_rx_q *rxq;
93159d0f746SFrank Li 	int i;
93259d0f746SFrank Li 
93359d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
93459d0f746SFrank Li 		rxq = fep->rx_queue[i];
9357355f276STroy Kisky 		writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
936fbbeefddSAndrew Lunn 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
93759d0f746SFrank Li 
93859d0f746SFrank Li 		/* enable DMA1/2 */
93959d0f746SFrank Li 		if (i)
94059d0f746SFrank Li 			writel(RCMR_MATCHEN | RCMR_CMP(i),
94159d0f746SFrank Li 			       fep->hwp + FEC_RCMR(i));
94259d0f746SFrank Li 	}
94359d0f746SFrank Li 
94459d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
94559d0f746SFrank Li 		txq = fep->tx_queue[i];
9467355f276STroy Kisky 		writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
94759d0f746SFrank Li 
94859d0f746SFrank Li 		/* enable DMA1/2 */
94959d0f746SFrank Li 		if (i)
95059d0f746SFrank Li 			writel(DMA_CLASS_EN | IDLE_SLOPE(i),
95159d0f746SFrank Li 			       fep->hwp + FEC_DMA_CFG(i));
95259d0f746SFrank Li 	}
95359d0f746SFrank Li }
95459d0f746SFrank Li 
95559d0f746SFrank Li static void fec_enet_reset_skb(struct net_device *ndev)
95659d0f746SFrank Li {
95759d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
95859d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
95959d0f746SFrank Li 	int i, j;
96059d0f746SFrank Li 
96159d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
96259d0f746SFrank Li 		txq = fep->tx_queue[i];
96359d0f746SFrank Li 
9647355f276STroy Kisky 		for (j = 0; j < txq->bd.ring_size; j++) {
96559d0f746SFrank Li 			if (txq->tx_skbuff[j]) {
96659d0f746SFrank Li 				dev_kfree_skb_any(txq->tx_skbuff[j]);
96759d0f746SFrank Li 				txq->tx_skbuff[j] = NULL;
96859d0f746SFrank Li 			}
96959d0f746SFrank Li 		}
97059d0f746SFrank Li 	}
97159d0f746SFrank Li }
972a210576cSDavid S. Miller 
973dbc64a8eSRussell King /*
974dbc64a8eSRussell King  * This function is called to start or restart the FEC during a link
975dbc64a8eSRussell King  * change, transmit timeout, or to reconfigure the FEC.  The network
976dbc64a8eSRussell King  * packet processing for this device must be stopped before this call.
977793fc096SFrank Li  */
978793fc096SFrank Li static void
979ef83337dSRussell King fec_restart(struct net_device *ndev)
980793fc096SFrank Li {
981793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
982793fc096SFrank Li 	u32 temp_mac[2];
983793fc096SFrank Li 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
984793fc096SFrank Li 	u32 ecntl = 0x2; /* ETHEREN */
985793fc096SFrank Li 
986106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
987106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
988106c314cSFugang Duan 	 * instead of reset MAC itself.
989106c314cSFugang Duan 	 */
990471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES ||
991c730ab42SLaurent Badel 	    ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) {
992106c314cSFugang Duan 		writel(0, fep->hwp + FEC_ECNTRL);
993106c314cSFugang Duan 	} else {
994793fc096SFrank Li 		writel(1, fep->hwp + FEC_ECNTRL);
995793fc096SFrank Li 		udelay(10);
996106c314cSFugang Duan 	}
997793fc096SFrank Li 
998793fc096SFrank Li 	/*
999793fc096SFrank Li 	 * enet-mac reset will reset mac address registers too,
1000793fc096SFrank Li 	 * so need to reconfigure it.
1001793fc096SFrank Li 	 */
1002793fc096SFrank Li 	memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
10035cfa3039SJohannes Berg 	writel((__force u32)cpu_to_be32(temp_mac[0]),
10045cfa3039SJohannes Berg 	       fep->hwp + FEC_ADDR_LOW);
10055cfa3039SJohannes Berg 	writel((__force u32)cpu_to_be32(temp_mac[1]),
10065cfa3039SJohannes Berg 	       fep->hwp + FEC_ADDR_HIGH);
1007793fc096SFrank Li 
1008f166f890SAndrew Lunn 	/* Clear any outstanding interrupt, except MDIO. */
1009f166f890SAndrew Lunn 	writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
1010793fc096SFrank Li 
1011a210576cSDavid S. Miller 	fec_enet_bd_init(ndev);
1012a210576cSDavid S. Miller 
101359d0f746SFrank Li 	fec_enet_enable_ring(ndev);
1014793fc096SFrank Li 
101559d0f746SFrank Li 	/* Reset tx SKB buffers. */
101659d0f746SFrank Li 	fec_enet_reset_skb(ndev);
1017793fc096SFrank Li 
1018793fc096SFrank Li 	/* Enable MII mode */
1019ef83337dSRussell King 	if (fep->full_duplex == DUPLEX_FULL) {
1020793fc096SFrank Li 		/* FD enable */
1021793fc096SFrank Li 		writel(0x04, fep->hwp + FEC_X_CNTRL);
1022793fc096SFrank Li 	} else {
1023793fc096SFrank Li 		/* No Rcv on Xmit */
1024793fc096SFrank Li 		rcntl |= 0x02;
1025793fc096SFrank Li 		writel(0x0, fep->hwp + FEC_X_CNTRL);
1026793fc096SFrank Li 	}
1027793fc096SFrank Li 
1028793fc096SFrank Li 	/* Set MII speed */
1029793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1030793fc096SFrank Li 
1031d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
103218803495SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_HAS_RACC) {
103332d1bbb1SGeert Uytterhoeven 		u32 val = readl(fep->hwp + FEC_RACC);
103432d1bbb1SGeert Uytterhoeven 
10353ac72b7bSEric Nelson 		/* align IP header */
10363ac72b7bSEric Nelson 		val |= FEC_RACC_SHIFT16;
10374c09eed9SJim Baxter 		if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
10383ac72b7bSEric Nelson 			/* set RX checksum */
10394c09eed9SJim Baxter 			val |= FEC_RACC_OPTIONS;
10404c09eed9SJim Baxter 		else
10414c09eed9SJim Baxter 			val &= ~FEC_RACC_OPTIONS;
10424c09eed9SJim Baxter 		writel(val, fep->hwp + FEC_RACC);
104355cd48c8STroy Kisky 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
104432867fccSFabio Estevam 	}
1045d1391930SGuenter Roeck #endif
10464c09eed9SJim Baxter 
1047793fc096SFrank Li 	/*
1048793fc096SFrank Li 	 * The phy interface and speed need to get configured
1049793fc096SFrank Li 	 * differently on enet-mac.
1050793fc096SFrank Li 	 */
10516b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1052793fc096SFrank Li 		/* Enable flow control and length check */
1053793fc096SFrank Li 		rcntl |= 0x40000000 | 0x00000020;
1054793fc096SFrank Li 
1055793fc096SFrank Li 		/* RGMII, RMII or MII */
1056e813bb2bSMarkus Pargmann 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
1057e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1058e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1059e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1060793fc096SFrank Li 			rcntl |= (1 << 6);
1061793fc096SFrank Li 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1062793fc096SFrank Li 			rcntl |= (1 << 8);
1063793fc096SFrank Li 		else
1064793fc096SFrank Li 			rcntl &= ~(1 << 8);
1065793fc096SFrank Li 
1066793fc096SFrank Li 		/* 1G, 100M or 10M */
106745f5c327SPhilippe Reynes 		if (ndev->phydev) {
106845f5c327SPhilippe Reynes 			if (ndev->phydev->speed == SPEED_1000)
1069793fc096SFrank Li 				ecntl |= (1 << 5);
107045f5c327SPhilippe Reynes 			else if (ndev->phydev->speed == SPEED_100)
1071793fc096SFrank Li 				rcntl &= ~(1 << 9);
1072793fc096SFrank Li 			else
1073793fc096SFrank Li 				rcntl |= (1 << 9);
1074793fc096SFrank Li 		}
1075793fc096SFrank Li 	} else {
1076793fc096SFrank Li #ifdef FEC_MIIGSK_ENR
10776b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1078793fc096SFrank Li 			u32 cfgr;
1079793fc096SFrank Li 			/* disable the gasket and wait */
1080793fc096SFrank Li 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
1081793fc096SFrank Li 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1082793fc096SFrank Li 				udelay(1);
1083793fc096SFrank Li 
1084793fc096SFrank Li 			/*
1085793fc096SFrank Li 			 * configure the gasket:
1086793fc096SFrank Li 			 *   RMII, 50 MHz, no loopback, no echo
1087793fc096SFrank Li 			 *   MII, 25 MHz, no loopback, no echo
1088793fc096SFrank Li 			 */
1089793fc096SFrank Li 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1090793fc096SFrank Li 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
109145f5c327SPhilippe Reynes 			if (ndev->phydev && ndev->phydev->speed == SPEED_10)
1092793fc096SFrank Li 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1093793fc096SFrank Li 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1094793fc096SFrank Li 
1095793fc096SFrank Li 			/* re-enable the gasket */
1096793fc096SFrank Li 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
1097793fc096SFrank Li 		}
1098793fc096SFrank Li #endif
1099793fc096SFrank Li 	}
1100793fc096SFrank Li 
1101d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1102793fc096SFrank Li 	/* enable pause frame*/
1103793fc096SFrank Li 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1104793fc096SFrank Li 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
110545f5c327SPhilippe Reynes 	     ndev->phydev && ndev->phydev->pause)) {
1106793fc096SFrank Li 		rcntl |= FEC_ENET_FCE;
1107793fc096SFrank Li 
11084c09eed9SJim Baxter 		/* set FIFO threshold parameter to reduce overrun */
1109793fc096SFrank Li 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1110793fc096SFrank Li 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1111793fc096SFrank Li 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1112793fc096SFrank Li 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1113793fc096SFrank Li 
1114793fc096SFrank Li 		/* OPD */
1115793fc096SFrank Li 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1116793fc096SFrank Li 	} else {
1117793fc096SFrank Li 		rcntl &= ~FEC_ENET_FCE;
1118793fc096SFrank Li 	}
1119d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
1120793fc096SFrank Li 
1121793fc096SFrank Li 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
1122793fc096SFrank Li 
112384fe6182SStefan Wahren 	/* Setup multicast filter. */
112484fe6182SStefan Wahren 	set_multicast_list(ndev);
112584fe6182SStefan Wahren #ifndef CONFIG_M5272
112684fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
112784fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
112884fe6182SStefan Wahren #endif
112984fe6182SStefan Wahren 
11306b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1131793fc096SFrank Li 		/* enable ENET endian swap */
1132793fc096SFrank Li 		ecntl |= (1 << 8);
1133793fc096SFrank Li 		/* enable ENET store and forward mode */
1134793fc096SFrank Li 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
1135793fc096SFrank Li 	}
1136793fc096SFrank Li 
1137793fc096SFrank Li 	if (fep->bufdesc_ex)
1138793fc096SFrank Li 		ecntl |= (1 << 4);
1139793fc096SFrank Li 
1140fc539459SFugang Duan 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1141fc539459SFugang Duan 	    fep->rgmii_txc_dly)
1142fc539459SFugang Duan 		ecntl |= FEC_ENET_TXC_DLY;
1143fc539459SFugang Duan 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1144fc539459SFugang Duan 	    fep->rgmii_rxc_dly)
1145fc539459SFugang Duan 		ecntl |= FEC_ENET_RXC_DLY;
1146fc539459SFugang Duan 
114738ae92dcSChris Healy #ifndef CONFIG_M5272
1148b9eef55cSJim Baxter 	/* Enable the MIB statistic event counters */
1149b9eef55cSJim Baxter 	writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
115038ae92dcSChris Healy #endif
115138ae92dcSChris Healy 
1152793fc096SFrank Li 	/* And last, enable the transmit and receive processing */
1153793fc096SFrank Li 	writel(ecntl, fep->hwp + FEC_ECNTRL);
1154ce99d0d3SFrank Li 	fec_enet_active_rxring(ndev);
1155793fc096SFrank Li 
1156793fc096SFrank Li 	if (fep->bufdesc_ex)
1157793fc096SFrank Li 		fec_ptp_start_cyclecounter(ndev);
1158793fc096SFrank Li 
1159793fc096SFrank Li 	/* Enable interrupts we wish to service */
11600c5a3aefSNimrod Andy 	if (fep->link)
1161793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
11620c5a3aefSNimrod Andy 	else
1163f166f890SAndrew Lunn 		writel(0, fep->hwp + FEC_IMASK);
1164d851b47bSFugang Duan 
1165d851b47bSFugang Duan 	/* Init the interrupt coalescing */
1166d851b47bSFugang Duan 	fec_enet_itr_coal_init(ndev);
1167d851b47bSFugang Duan 
1168793fc096SFrank Li }
1169793fc096SFrank Li 
1170da722186SMartin Fuzzey static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
1171da722186SMartin Fuzzey {
1172da722186SMartin Fuzzey 	struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1173da722186SMartin Fuzzey 	struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
1174da722186SMartin Fuzzey 
1175da722186SMartin Fuzzey 	if (stop_gpr->gpr) {
1176da722186SMartin Fuzzey 		if (enabled)
1177da722186SMartin Fuzzey 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1178da722186SMartin Fuzzey 					   BIT(stop_gpr->bit),
1179da722186SMartin Fuzzey 					   BIT(stop_gpr->bit));
1180da722186SMartin Fuzzey 		else
1181da722186SMartin Fuzzey 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1182da722186SMartin Fuzzey 					   BIT(stop_gpr->bit), 0);
1183da722186SMartin Fuzzey 	} else if (pdata && pdata->sleep_mode_enable) {
1184da722186SMartin Fuzzey 		pdata->sleep_mode_enable(enabled);
1185da722186SMartin Fuzzey 	}
1186da722186SMartin Fuzzey }
1187da722186SMartin Fuzzey 
1188793fc096SFrank Li static void
1189793fc096SFrank Li fec_stop(struct net_device *ndev)
1190793fc096SFrank Li {
1191793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1192793fc096SFrank Li 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1193de40ed31SNimrod Andy 	u32 val;
1194793fc096SFrank Li 
1195793fc096SFrank Li 	/* We cannot expect a graceful transmit stop without link !!! */
1196793fc096SFrank Li 	if (fep->link) {
1197793fc096SFrank Li 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1198793fc096SFrank Li 		udelay(10);
1199793fc096SFrank Li 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
120031b7720cSJoe Perches 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1201793fc096SFrank Li 	}
1202793fc096SFrank Li 
1203106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
1204106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1205106c314cSFugang Duan 	 * instead of reset MAC itself.
1206106c314cSFugang Duan 	 */
1207de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1208471ff445SJoakim Zhang 		if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
1209106c314cSFugang Duan 			writel(0, fep->hwp + FEC_ECNTRL);
1210106c314cSFugang Duan 		} else {
1211793fc096SFrank Li 			writel(1, fep->hwp + FEC_ECNTRL);
1212793fc096SFrank Li 			udelay(10);
1213106c314cSFugang Duan 		}
1214793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1215de40ed31SNimrod Andy 	} else {
1216de40ed31SNimrod Andy 		writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1217de40ed31SNimrod Andy 		val = readl(fep->hwp + FEC_ECNTRL);
1218de40ed31SNimrod Andy 		val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1219de40ed31SNimrod Andy 		writel(val, fep->hwp + FEC_ECNTRL);
1220da722186SMartin Fuzzey 		fec_enet_stop_mode(fep, true);
1221de40ed31SNimrod Andy 	}
1222de40ed31SNimrod Andy 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1223793fc096SFrank Li 
1224793fc096SFrank Li 	/* We have to keep ENET enabled to have MII interrupt stay working */
1225de40ed31SNimrod Andy 	if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1226de40ed31SNimrod Andy 		!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1227793fc096SFrank Li 		writel(2, fep->hwp + FEC_ECNTRL);
1228793fc096SFrank Li 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1229793fc096SFrank Li 	}
1230793fc096SFrank Li }
1231793fc096SFrank Li 
1232793fc096SFrank Li 
1233793fc096SFrank Li static void
12340290bd29SMichael S. Tsirkin fec_timeout(struct net_device *ndev, unsigned int txqueue)
1235793fc096SFrank Li {
1236793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1237793fc096SFrank Li 
1238344756f6SRussell King 	fec_dump(ndev);
1239344756f6SRussell King 
1240793fc096SFrank Li 	ndev->stats.tx_errors++;
1241793fc096SFrank Li 
124236cdc743SRussell King 	schedule_work(&fep->tx_timeout_work);
124354309fa6SFrank Li }
124454309fa6SFrank Li 
124536cdc743SRussell King static void fec_enet_timeout_work(struct work_struct *work)
124654309fa6SFrank Li {
124754309fa6SFrank Li 	struct fec_enet_private *fep =
124836cdc743SRussell King 		container_of(work, struct fec_enet_private, tx_timeout_work);
12498ce5624fSRussell King 	struct net_device *ndev = fep->netdev;
125054309fa6SFrank Li 
1251da1774e5SRussell King 	rtnl_lock();
12528ce5624fSRussell King 	if (netif_device_present(ndev) || netif_running(ndev)) {
1253dbc64a8eSRussell King 		napi_disable(&fep->napi);
1254dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
1255ef83337dSRussell King 		fec_restart(ndev);
1256657ade07SRickard x Andersson 		netif_tx_wake_all_queues(ndev);
12576af42d42SRussell King 		netif_tx_unlock_bh(ndev);
1258dbc64a8eSRussell King 		napi_enable(&fep->napi);
12598ce5624fSRussell King 	}
1260da1774e5SRussell King 	rtnl_unlock();
126154309fa6SFrank Li }
1262793fc096SFrank Li 
1263793fc096SFrank Li static void
1264bfd4ecddSRussell King fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1265bfd4ecddSRussell King 	struct skb_shared_hwtstamps *hwtstamps)
1266bfd4ecddSRussell King {
1267bfd4ecddSRussell King 	unsigned long flags;
1268bfd4ecddSRussell King 	u64 ns;
1269bfd4ecddSRussell King 
1270bfd4ecddSRussell King 	spin_lock_irqsave(&fep->tmreg_lock, flags);
1271bfd4ecddSRussell King 	ns = timecounter_cyc2time(&fep->tc, ts);
1272bfd4ecddSRussell King 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1273bfd4ecddSRussell King 
1274bfd4ecddSRussell King 	memset(hwtstamps, 0, sizeof(*hwtstamps));
1275bfd4ecddSRussell King 	hwtstamps->hwtstamp = ns_to_ktime(ns);
1276bfd4ecddSRussell King }
1277bfd4ecddSRussell King 
1278bfd4ecddSRussell King static void
12794d494cdcSFugang Duan fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1280793fc096SFrank Li {
1281793fc096SFrank Li 	struct	fec_enet_private *fep;
1282a2fe37b6SFabio Estevam 	struct bufdesc *bdp;
1283793fc096SFrank Li 	unsigned short status;
1284793fc096SFrank Li 	struct	sk_buff	*skb;
12854d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
12864d494cdcSFugang Duan 	struct netdev_queue *nq;
1287793fc096SFrank Li 	int	index = 0;
128879f33912SNimrod Andy 	int	entries_free;
1289793fc096SFrank Li 
1290793fc096SFrank Li 	fep = netdev_priv(ndev);
12914d494cdcSFugang Duan 
12924d494cdcSFugang Duan 	txq = fep->tx_queue[queue_id];
12934d494cdcSFugang Duan 	/* get next bdp of dirty_tx */
12944d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue_id);
12954d494cdcSFugang Duan 	bdp = txq->dirty_tx;
1296793fc096SFrank Li 
1297793fc096SFrank Li 	/* get next bdp of dirty_tx */
12987355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1299793fc096SFrank Li 
13007355f276STroy Kisky 	while (bdp != READ_ONCE(txq->bd.cur)) {
13017355f276STroy Kisky 		/* Order the load of bd.cur and cbd_sc */
1302c4bc44c6SKevin Hao 		rmb();
13035cfa3039SJohannes Berg 		status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1304c4bc44c6SKevin Hao 		if (status & BD_ENET_TX_READY)
1305793fc096SFrank Li 			break;
1306793fc096SFrank Li 
13077355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
13082b995f63SNimrod Andy 
1309a2fe37b6SFabio Estevam 		skb = txq->tx_skbuff[index];
1310a2fe37b6SFabio Estevam 		txq->tx_skbuff[index] = NULL;
13115cfa3039SJohannes Berg 		if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
13125cfa3039SJohannes Berg 			dma_unmap_single(&fep->pdev->dev,
13135cfa3039SJohannes Berg 					 fec32_to_cpu(bdp->cbd_bufaddr),
13145cfa3039SJohannes Berg 					 fec16_to_cpu(bdp->cbd_datlen),
13155cfa3039SJohannes Berg 					 DMA_TO_DEVICE);
13165cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
13177fafe803STroy Kisky 		if (!skb)
13187fafe803STroy Kisky 			goto skb_done;
1319793fc096SFrank Li 
1320793fc096SFrank Li 		/* Check for errors. */
1321793fc096SFrank Li 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1322793fc096SFrank Li 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
1323793fc096SFrank Li 				   BD_ENET_TX_CSL)) {
1324793fc096SFrank Li 			ndev->stats.tx_errors++;
1325793fc096SFrank Li 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
1326793fc096SFrank Li 				ndev->stats.tx_heartbeat_errors++;
1327793fc096SFrank Li 			if (status & BD_ENET_TX_LC)  /* Late collision */
1328793fc096SFrank Li 				ndev->stats.tx_window_errors++;
1329793fc096SFrank Li 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
1330793fc096SFrank Li 				ndev->stats.tx_aborted_errors++;
1331793fc096SFrank Li 			if (status & BD_ENET_TX_UN)  /* Underrun */
1332793fc096SFrank Li 				ndev->stats.tx_fifo_errors++;
1333793fc096SFrank Li 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
1334793fc096SFrank Li 				ndev->stats.tx_carrier_errors++;
1335793fc096SFrank Li 		} else {
1336793fc096SFrank Li 			ndev->stats.tx_packets++;
13376e909283SNimrod Andy 			ndev->stats.tx_bytes += skb->len;
1338793fc096SFrank Li 		}
1339793fc096SFrank Li 
134034074639SSergey Organov 		/* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who
134134074639SSergey Organov 		 * are to time stamp the packet, so we still need to check time
134234074639SSergey Organov 		 * stamping enabled flag.
134334074639SSergey Organov 		 */
134434074639SSergey Organov 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS &&
134534074639SSergey Organov 			     fep->hwts_tx_en) &&
1346793fc096SFrank Li 		    fep->bufdesc_ex) {
1347793fc096SFrank Li 			struct skb_shared_hwtstamps shhwtstamps;
1348793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1349793fc096SFrank Li 
13505cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1351793fc096SFrank Li 			skb_tstamp_tx(skb, &shhwtstamps);
1352793fc096SFrank Li 		}
1353793fc096SFrank Li 
1354793fc096SFrank Li 		/* Deferred means some collisions occurred during transmit,
1355793fc096SFrank Li 		 * but we eventually sent the packet OK.
1356793fc096SFrank Li 		 */
1357793fc096SFrank Li 		if (status & BD_ENET_TX_DEF)
1358793fc096SFrank Li 			ndev->stats.collisions++;
1359793fc096SFrank Li 
1360793fc096SFrank Li 		/* Free the sk buffer associated with this last transmit */
1361793fc096SFrank Li 		dev_kfree_skb_any(skb);
13627fafe803STroy Kisky skb_done:
1363c4bc44c6SKevin Hao 		/* Make sure the update to bdp and tx_skbuff are performed
1364c4bc44c6SKevin Hao 		 * before dirty_tx
1365c4bc44c6SKevin Hao 		 */
1366c4bc44c6SKevin Hao 		wmb();
13674d494cdcSFugang Duan 		txq->dirty_tx = bdp;
1368793fc096SFrank Li 
1369793fc096SFrank Li 		/* Update pointer to next buffer descriptor to be transmitted */
13707355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1371793fc096SFrank Li 
1372793fc096SFrank Li 		/* Since we have freed up a buffer, the ring is no longer full
1373793fc096SFrank Li 		 */
1374657ade07SRickard x Andersson 		if (netif_tx_queue_stopped(nq)) {
13757355f276STroy Kisky 			entries_free = fec_enet_get_free_txdesc_num(txq);
13764d494cdcSFugang Duan 			if (entries_free >= txq->tx_wake_threshold)
13774d494cdcSFugang Duan 				netif_tx_wake_queue(nq);
1378793fc096SFrank Li 		}
137979f33912SNimrod Andy 	}
1380ccea2968SRussell King 
1381c10bc0e7SFugang Duan 	/* ERR006358: Keep the transmitter going */
13827355f276STroy Kisky 	if (bdp != txq->bd.cur &&
138353bb20d1STroy Kisky 	    readl(txq->bd.reg_desc_active) == 0)
138453bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
13854d494cdcSFugang Duan }
13864d494cdcSFugang Duan 
13877cdaa4ccSTobias Waldekranz static void fec_enet_tx(struct net_device *ndev)
13884d494cdcSFugang Duan {
13894d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
13907cdaa4ccSTobias Waldekranz 	int i;
13917cdaa4ccSTobias Waldekranz 
13927cdaa4ccSTobias Waldekranz 	/* Make sure that AVB queues are processed first. */
13937cdaa4ccSTobias Waldekranz 	for (i = fep->num_tx_queues - 1; i >= 0; i--)
13947cdaa4ccSTobias Waldekranz 		fec_enet_tx_queue(ndev, i);
1395793fc096SFrank Li }
1396793fc096SFrank Li 
13971b7bde6dSNimrod Andy static int
13981b7bde6dSNimrod Andy fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
13991b7bde6dSNimrod Andy {
14001b7bde6dSNimrod Andy 	struct  fec_enet_private *fep = netdev_priv(ndev);
14011b7bde6dSNimrod Andy 	int off;
14021b7bde6dSNimrod Andy 
14031b7bde6dSNimrod Andy 	off = ((unsigned long)skb->data) & fep->rx_align;
14041b7bde6dSNimrod Andy 	if (off)
14051b7bde6dSNimrod Andy 		skb_reserve(skb, fep->rx_align + 1 - off);
14061b7bde6dSNimrod Andy 
14075cfa3039SJohannes 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));
14085cfa3039SJohannes Berg 	if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
14091b7bde6dSNimrod Andy 		if (net_ratelimit())
14101b7bde6dSNimrod Andy 			netdev_err(ndev, "Rx DMA memory map failed\n");
14111b7bde6dSNimrod Andy 		return -ENOMEM;
14121b7bde6dSNimrod Andy 	}
14131b7bde6dSNimrod Andy 
14141b7bde6dSNimrod Andy 	return 0;
14151b7bde6dSNimrod Andy }
14161b7bde6dSNimrod Andy 
14171b7bde6dSNimrod Andy static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
14181310b544SLothar Waßmann 			       struct bufdesc *bdp, u32 length, bool swap)
14191b7bde6dSNimrod Andy {
14201b7bde6dSNimrod Andy 	struct  fec_enet_private *fep = netdev_priv(ndev);
14211b7bde6dSNimrod Andy 	struct sk_buff *new_skb;
14221b7bde6dSNimrod Andy 
14231b7bde6dSNimrod Andy 	if (length > fep->rx_copybreak)
14241b7bde6dSNimrod Andy 		return false;
14251b7bde6dSNimrod Andy 
14261b7bde6dSNimrod Andy 	new_skb = netdev_alloc_skb(ndev, length);
14271b7bde6dSNimrod Andy 	if (!new_skb)
14281b7bde6dSNimrod Andy 		return false;
14291b7bde6dSNimrod Andy 
14305cfa3039SJohannes Berg 	dma_sync_single_for_cpu(&fep->pdev->dev,
14315cfa3039SJohannes Berg 				fec32_to_cpu(bdp->cbd_bufaddr),
14321b7bde6dSNimrod Andy 				FEC_ENET_RX_FRSIZE - fep->rx_align,
14331b7bde6dSNimrod Andy 				DMA_FROM_DEVICE);
14341310b544SLothar Waßmann 	if (!swap)
14351b7bde6dSNimrod Andy 		memcpy(new_skb->data, (*skb)->data, length);
14361310b544SLothar Waßmann 	else
14371310b544SLothar Waßmann 		swap_buffer2(new_skb->data, (*skb)->data, length);
14381b7bde6dSNimrod Andy 	*skb = new_skb;
14391b7bde6dSNimrod Andy 
14401b7bde6dSNimrod Andy 	return true;
14411b7bde6dSNimrod Andy }
14421b7bde6dSNimrod Andy 
14437355f276STroy Kisky /* During a receive, the bd_rx.cur points to the current incoming buffer.
1444793fc096SFrank Li  * When we update through the ring, if the next incoming buffer has
1445793fc096SFrank Li  * not been given to the system, we just set the empty indicator,
1446793fc096SFrank Li  * effectively tossing the packet.
1447793fc096SFrank Li  */
1448793fc096SFrank Li static int
14494d494cdcSFugang Duan fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1450793fc096SFrank Li {
1451793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
14524d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
1453793fc096SFrank Li 	struct bufdesc *bdp;
1454793fc096SFrank Li 	unsigned short status;
14551b7bde6dSNimrod Andy 	struct  sk_buff *skb_new = NULL;
1456793fc096SFrank Li 	struct  sk_buff *skb;
1457793fc096SFrank Li 	ushort	pkt_len;
1458793fc096SFrank Li 	__u8 *data;
1459793fc096SFrank Li 	int	pkt_received = 0;
1460cdffcf1bSJim Baxter 	struct	bufdesc_ex *ebdp = NULL;
1461cdffcf1bSJim Baxter 	bool	vlan_packet_rcvd = false;
1462cdffcf1bSJim Baxter 	u16	vlan_tag;
1463d842a31fSDuan Fugang-B38611 	int	index = 0;
14641b7bde6dSNimrod Andy 	bool	is_copybreak;
14656b7e4008SLothar Waßmann 	bool	need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1466793fc096SFrank Li 
1467793fc096SFrank Li #ifdef CONFIG_M532x
1468793fc096SFrank Li 	flush_cache_all();
1469793fc096SFrank Li #endif
14704d494cdcSFugang Duan 	rxq = fep->rx_queue[queue_id];
1471793fc096SFrank Li 
1472793fc096SFrank Li 	/* First, grab all of the stats for the incoming packet.
1473793fc096SFrank Li 	 * These get messed up if we get called due to a busy condition.
1474793fc096SFrank Li 	 */
14757355f276STroy Kisky 	bdp = rxq->bd.cur;
1476793fc096SFrank Li 
14775cfa3039SJohannes Berg 	while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1478793fc096SFrank Li 
1479793fc096SFrank Li 		if (pkt_received >= budget)
1480793fc096SFrank Li 			break;
1481793fc096SFrank Li 		pkt_received++;
1482793fc096SFrank Li 
1483ed63f1dcSRussell King 		writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
1484db3421c1SRussell King 
1485793fc096SFrank Li 		/* Check for errors. */
1486095098e1STroy Kisky 		status ^= BD_ENET_RX_LAST;
1487793fc096SFrank Li 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1488095098e1STroy Kisky 			   BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1489095098e1STroy Kisky 			   BD_ENET_RX_CL)) {
1490793fc096SFrank Li 			ndev->stats.rx_errors++;
1491095098e1STroy Kisky 			if (status & BD_ENET_RX_OV) {
1492095098e1STroy Kisky 				/* FIFO overrun */
1493095098e1STroy Kisky 				ndev->stats.rx_fifo_errors++;
1494095098e1STroy Kisky 				goto rx_processing_done;
1495095098e1STroy Kisky 			}
1496095098e1STroy Kisky 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1497095098e1STroy Kisky 						| BD_ENET_RX_LAST)) {
1498793fc096SFrank Li 				/* Frame too long or too short. */
1499793fc096SFrank Li 				ndev->stats.rx_length_errors++;
1500095098e1STroy Kisky 				if (status & BD_ENET_RX_LAST)
1501095098e1STroy Kisky 					netdev_err(ndev, "rcv is not +last\n");
1502793fc096SFrank Li 			}
1503793fc096SFrank Li 			if (status & BD_ENET_RX_CR)	/* CRC Error */
1504793fc096SFrank Li 				ndev->stats.rx_crc_errors++;
1505095098e1STroy Kisky 			/* Report late collisions as a frame error. */
1506095098e1STroy Kisky 			if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1507793fc096SFrank Li 				ndev->stats.rx_frame_errors++;
1508793fc096SFrank Li 			goto rx_processing_done;
1509793fc096SFrank Li 		}
1510793fc096SFrank Li 
1511793fc096SFrank Li 		/* Process the incoming frame. */
1512793fc096SFrank Li 		ndev->stats.rx_packets++;
15135cfa3039SJohannes Berg 		pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1514793fc096SFrank Li 		ndev->stats.rx_bytes += pkt_len;
1515793fc096SFrank Li 
15167355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &rxq->bd);
15171b7bde6dSNimrod Andy 		skb = rxq->rx_skbuff[index];
15181b7bde6dSNimrod Andy 
15191b7bde6dSNimrod Andy 		/* The packet length includes FCS, but we don't want to
15201b7bde6dSNimrod Andy 		 * include that when passing upstream as it messes up
15211b7bde6dSNimrod Andy 		 * bridging applications.
15221b7bde6dSNimrod Andy 		 */
15231310b544SLothar Waßmann 		is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
15241310b544SLothar Waßmann 						  need_swap);
15251b7bde6dSNimrod Andy 		if (!is_copybreak) {
15261b7bde6dSNimrod Andy 			skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
15271b7bde6dSNimrod Andy 			if (unlikely(!skb_new)) {
15281b7bde6dSNimrod Andy 				ndev->stats.rx_dropped++;
15291b7bde6dSNimrod Andy 				goto rx_processing_done;
15301b7bde6dSNimrod Andy 			}
15315cfa3039SJohannes Berg 			dma_unmap_single(&fep->pdev->dev,
15325cfa3039SJohannes Berg 					 fec32_to_cpu(bdp->cbd_bufaddr),
1533b64bf4b7SNimrod Andy 					 FEC_ENET_RX_FRSIZE - fep->rx_align,
1534b64bf4b7SNimrod Andy 					 DMA_FROM_DEVICE);
15351b7bde6dSNimrod Andy 		}
1536793fc096SFrank Li 
15371b7bde6dSNimrod Andy 		prefetch(skb->data - NET_IP_ALIGN);
15381b7bde6dSNimrod Andy 		skb_put(skb, pkt_len - 4);
15391b7bde6dSNimrod Andy 		data = skb->data;
15403ac72b7bSEric Nelson 
1541235bde1eSFabio Estevam 		if (!is_copybreak && need_swap)
1542235bde1eSFabio Estevam 			swap_buffer(data, pkt_len);
1543235bde1eSFabio Estevam 
15443ac72b7bSEric Nelson #if !defined(CONFIG_M5272)
15453ac72b7bSEric Nelson 		if (fep->quirks & FEC_QUIRK_HAS_RACC)
15463ac72b7bSEric Nelson 			data = skb_pull_inline(skb, 2);
15473ac72b7bSEric Nelson #endif
15483ac72b7bSEric Nelson 
1549cdffcf1bSJim Baxter 		/* Extract the enhanced buffer descriptor */
1550cdffcf1bSJim Baxter 		ebdp = NULL;
1551cdffcf1bSJim Baxter 		if (fep->bufdesc_ex)
1552cdffcf1bSJim Baxter 			ebdp = (struct bufdesc_ex *)bdp;
1553cdffcf1bSJim Baxter 
1554cdffcf1bSJim Baxter 		/* If this is a VLAN packet remove the VLAN Tag */
1555cdffcf1bSJim Baxter 		vlan_packet_rcvd = false;
1556cdffcf1bSJim Baxter 		if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
15575cfa3039SJohannes Berg 		    fep->bufdesc_ex &&
15585cfa3039SJohannes Berg 		    (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1559cdffcf1bSJim Baxter 			/* Push and remove the vlan tag */
1560cdffcf1bSJim Baxter 			struct vlan_hdr *vlan_header =
1561cdffcf1bSJim Baxter 					(struct vlan_hdr *) (data + ETH_HLEN);
1562cdffcf1bSJim Baxter 			vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1563cdffcf1bSJim Baxter 
1564cdffcf1bSJim Baxter 			vlan_packet_rcvd = true;
15651b7bde6dSNimrod Andy 
1566af5cbc98SNimrod Andy 			memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
15671b7bde6dSNimrod Andy 			skb_pull(skb, VLAN_HLEN);
1568cdffcf1bSJim Baxter 		}
1569cdffcf1bSJim Baxter 
1570793fc096SFrank Li 		skb->protocol = eth_type_trans(skb, ndev);
1571793fc096SFrank Li 
1572793fc096SFrank Li 		/* Get receive timestamp from the skb */
1573bfd4ecddSRussell King 		if (fep->hwts_rx_en && fep->bufdesc_ex)
15745cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1575bfd4ecddSRussell King 					  skb_hwtstamps(skb));
1576793fc096SFrank Li 
15774c09eed9SJim Baxter 		if (fep->bufdesc_ex &&
15784c09eed9SJim Baxter 		    (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
15795cfa3039SJohannes Berg 			if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
15804c09eed9SJim Baxter 				/* don't check it */
15814c09eed9SJim Baxter 				skb->ip_summed = CHECKSUM_UNNECESSARY;
15824c09eed9SJim Baxter 			} else {
15834c09eed9SJim Baxter 				skb_checksum_none_assert(skb);
15844c09eed9SJim Baxter 			}
15854c09eed9SJim Baxter 		}
15864c09eed9SJim Baxter 
1587cdffcf1bSJim Baxter 		/* Handle received VLAN packets */
1588cdffcf1bSJim Baxter 		if (vlan_packet_rcvd)
1589cdffcf1bSJim Baxter 			__vlan_hwaccel_put_tag(skb,
1590cdffcf1bSJim Baxter 					       htons(ETH_P_8021Q),
1591cdffcf1bSJim Baxter 					       vlan_tag);
1592cdffcf1bSJim Baxter 
15937cdaa4ccSTobias Waldekranz 		skb_record_rx_queue(skb, queue_id);
1594793fc096SFrank Li 		napi_gro_receive(&fep->napi, skb);
1595793fc096SFrank Li 
15961b7bde6dSNimrod Andy 		if (is_copybreak) {
15975cfa3039SJohannes Berg 			dma_sync_single_for_device(&fep->pdev->dev,
15985cfa3039SJohannes Berg 						   fec32_to_cpu(bdp->cbd_bufaddr),
1599b64bf4b7SNimrod Andy 						   FEC_ENET_RX_FRSIZE - fep->rx_align,
1600b64bf4b7SNimrod Andy 						   DMA_FROM_DEVICE);
16011b7bde6dSNimrod Andy 		} else {
16021b7bde6dSNimrod Andy 			rxq->rx_skbuff[index] = skb_new;
16031b7bde6dSNimrod Andy 			fec_enet_new_rxbdp(ndev, bdp, skb_new);
16041b7bde6dSNimrod Andy 		}
16051b7bde6dSNimrod Andy 
1606793fc096SFrank Li rx_processing_done:
1607793fc096SFrank Li 		/* Clear the status flags for this buffer */
1608793fc096SFrank Li 		status &= ~BD_ENET_RX_STATS;
1609793fc096SFrank Li 
1610793fc096SFrank Li 		/* Mark the buffer empty */
1611793fc096SFrank Li 		status |= BD_ENET_RX_EMPTY;
1612793fc096SFrank Li 
1613793fc096SFrank Li 		if (fep->bufdesc_ex) {
1614793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1615793fc096SFrank Li 
16165cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1617793fc096SFrank Li 			ebdp->cbd_prot = 0;
1618793fc096SFrank Li 			ebdp->cbd_bdu = 0;
1619793fc096SFrank Li 		}
1620be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
1621be293467STroy Kisky 		 * performed before transferring ownership.
1622be293467STroy Kisky 		 */
1623be293467STroy Kisky 		wmb();
1624be293467STroy Kisky 		bdp->cbd_sc = cpu_to_fec16(status);
1625793fc096SFrank Li 
1626793fc096SFrank Li 		/* Update BD pointer to next entry */
16277355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
162836e24e2eSDuan Fugang-B38611 
1629793fc096SFrank Li 		/* Doing this here will keep the FEC running while we process
1630793fc096SFrank Li 		 * incoming frames.  On a heavily loaded network, we should be
1631793fc096SFrank Li 		 * able to keep up at the expense of system resources.
1632793fc096SFrank Li 		 */
163353bb20d1STroy Kisky 		writel(0, rxq->bd.reg_desc_active);
1634793fc096SFrank Li 	}
16357355f276STroy Kisky 	rxq->bd.cur = bdp;
1636793fc096SFrank Li 	return pkt_received;
1637793fc096SFrank Li }
1638793fc096SFrank Li 
16397cdaa4ccSTobias Waldekranz static int fec_enet_rx(struct net_device *ndev, int budget)
16404d494cdcSFugang Duan {
16414d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
16427cdaa4ccSTobias Waldekranz 	int i, done = 0;
16434d494cdcSFugang Duan 
16447cdaa4ccSTobias Waldekranz 	/* Make sure that AVB queues are processed first. */
16457cdaa4ccSTobias Waldekranz 	for (i = fep->num_rx_queues - 1; i >= 0; i--)
16467cdaa4ccSTobias Waldekranz 		done += fec_enet_rx_queue(ndev, budget - done, i);
16471c021bb7SUwe Kleine-König 
16487cdaa4ccSTobias Waldekranz 	return done;
16494d494cdcSFugang Duan }
16504d494cdcSFugang Duan 
16517cdaa4ccSTobias Waldekranz static bool fec_enet_collect_events(struct fec_enet_private *fep)
16524d494cdcSFugang Duan {
1653793fc096SFrank Li 	uint int_events;
1654793fc096SFrank Li 
1655793fc096SFrank Li 	int_events = readl(fep->hwp + FEC_IEVENT);
1656f166f890SAndrew Lunn 
1657f166f890SAndrew Lunn 	/* Don't clear MDIO events, we poll for those */
1658f166f890SAndrew Lunn 	int_events &= ~FEC_ENET_MII;
1659f166f890SAndrew Lunn 
166094191fd6SNimrod Andy 	writel(int_events, fep->hwp + FEC_IEVENT);
1661793fc096SFrank Li 
16627cdaa4ccSTobias Waldekranz 	return int_events != 0;
16637cdaa4ccSTobias Waldekranz }
16647cdaa4ccSTobias Waldekranz 
16657cdaa4ccSTobias Waldekranz static irqreturn_t
16667cdaa4ccSTobias Waldekranz fec_enet_interrupt(int irq, void *dev_id)
16677cdaa4ccSTobias Waldekranz {
16687cdaa4ccSTobias Waldekranz 	struct net_device *ndev = dev_id;
16697cdaa4ccSTobias Waldekranz 	struct fec_enet_private *fep = netdev_priv(ndev);
16707cdaa4ccSTobias Waldekranz 	irqreturn_t ret = IRQ_NONE;
16717cdaa4ccSTobias Waldekranz 
16727cdaa4ccSTobias Waldekranz 	if (fec_enet_collect_events(fep) && fep->link) {
1673793fc096SFrank Li 		ret = IRQ_HANDLED;
1674793fc096SFrank Li 
167594191fd6SNimrod Andy 		if (napi_schedule_prep(&fep->napi)) {
1676f166f890SAndrew Lunn 			/* Disable interrupts */
1677f166f890SAndrew Lunn 			writel(0, fep->hwp + FEC_IMASK);
167894191fd6SNimrod Andy 			__napi_schedule(&fep->napi);
167994191fd6SNimrod Andy 		}
1680793fc096SFrank Li 	}
1681793fc096SFrank Li 
1682793fc096SFrank Li 	return ret;
1683793fc096SFrank Li }
1684793fc096SFrank Li 
1685793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1686793fc096SFrank Li {
1687793fc096SFrank Li 	struct net_device *ndev = napi->dev;
1688793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
16897cdaa4ccSTobias Waldekranz 	int done = 0;
16907a16807cSRussell King 
16917cdaa4ccSTobias Waldekranz 	do {
16927cdaa4ccSTobias Waldekranz 		done += fec_enet_rx(ndev, budget - done);
1693793fc096SFrank Li 		fec_enet_tx(ndev);
16947cdaa4ccSTobias Waldekranz 	} while ((done < budget) && fec_enet_collect_events(fep));
1695793fc096SFrank Li 
16967cdaa4ccSTobias Waldekranz 	if (done < budget) {
16977cdaa4ccSTobias Waldekranz 		napi_complete_done(napi, done);
1698793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1699793fc096SFrank Li 	}
17007cdaa4ccSTobias Waldekranz 
17017cdaa4ccSTobias Waldekranz 	return done;
1702793fc096SFrank Li }
1703793fc096SFrank Li 
1704793fc096SFrank Li /* ------------------------------------------------------------------------- */
1705052fcc45SFugang Duan static int fec_get_mac(struct net_device *ndev)
1706793fc096SFrank Li {
1707793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1708793fc096SFrank Li 	unsigned char *iap, tmpaddr[ETH_ALEN];
170983216e39SMichael Walle 	int ret;
1710793fc096SFrank Li 
1711793fc096SFrank Li 	/*
1712793fc096SFrank Li 	 * try to get mac address in following order:
1713793fc096SFrank Li 	 *
1714793fc096SFrank Li 	 * 1) module parameter via kernel command line in form
1715793fc096SFrank Li 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1716793fc096SFrank Li 	 */
1717793fc096SFrank Li 	iap = macaddr;
1718793fc096SFrank Li 
1719793fc096SFrank Li 	/*
1720793fc096SFrank Li 	 * 2) from device tree data
1721793fc096SFrank Li 	 */
1722793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1723793fc096SFrank Li 		struct device_node *np = fep->pdev->dev.of_node;
1724793fc096SFrank Li 		if (np) {
172583216e39SMichael Walle 			ret = of_get_mac_address(np, tmpaddr);
172683216e39SMichael Walle 			if (!ret)
172783216e39SMichael Walle 				iap = tmpaddr;
1728052fcc45SFugang Duan 			else if (ret == -EPROBE_DEFER)
1729052fcc45SFugang Duan 				return ret;
1730793fc096SFrank Li 		}
1731793fc096SFrank Li 	}
1732793fc096SFrank Li 
1733793fc096SFrank Li 	/*
1734793fc096SFrank Li 	 * 3) from flash or fuse (via platform data)
1735793fc096SFrank Li 	 */
1736793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1737793fc096SFrank Li #ifdef CONFIG_M5272
1738793fc096SFrank Li 		if (FEC_FLASHMAC)
1739793fc096SFrank Li 			iap = (unsigned char *)FEC_FLASHMAC;
1740793fc096SFrank Li #else
174132d1bbb1SGeert Uytterhoeven 		struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
174232d1bbb1SGeert Uytterhoeven 
1743793fc096SFrank Li 		if (pdata)
1744793fc096SFrank Li 			iap = (unsigned char *)&pdata->mac;
1745793fc096SFrank Li #endif
1746793fc096SFrank Li 	}
1747793fc096SFrank Li 
1748793fc096SFrank Li 	/*
1749793fc096SFrank Li 	 * 4) FEC mac registers set by bootloader
1750793fc096SFrank Li 	 */
1751793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
17527d7628f3SDan Carpenter 		*((__be32 *) &tmpaddr[0]) =
17537d7628f3SDan Carpenter 			cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
17547d7628f3SDan Carpenter 		*((__be16 *) &tmpaddr[4]) =
17557d7628f3SDan Carpenter 			cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1756793fc096SFrank Li 		iap = &tmpaddr[0];
1757793fc096SFrank Li 	}
1758793fc096SFrank Li 
1759ff5b2fabSLucas Stach 	/*
1760ff5b2fabSLucas Stach 	 * 5) random mac address
1761ff5b2fabSLucas Stach 	 */
1762ff5b2fabSLucas Stach 	if (!is_valid_ether_addr(iap)) {
1763ff5b2fabSLucas Stach 		/* Report it and use a random ethernet address instead */
1764a19a0582SFabio Estevam 		dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
1765ff5b2fabSLucas Stach 		eth_hw_addr_random(ndev);
1766a19a0582SFabio Estevam 		dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
1767ff5b2fabSLucas Stach 			 ndev->dev_addr);
1768052fcc45SFugang Duan 		return 0;
1769ff5b2fabSLucas Stach 	}
1770ff5b2fabSLucas Stach 
1771a96d317fSJakub Kicinski 	eth_hw_addr_set(ndev, iap);
1772793fc096SFrank Li 
1773793fc096SFrank Li 	/* Adjust MAC if using macaddr */
1774793fc096SFrank Li 	if (iap == macaddr)
1775793fc096SFrank Li 		 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1776052fcc45SFugang Duan 
1777052fcc45SFugang Duan 	return 0;
1778793fc096SFrank Li }
1779793fc096SFrank Li 
1780793fc096SFrank Li /* ------------------------------------------------------------------------- */
1781793fc096SFrank Li 
1782793fc096SFrank Li /*
1783793fc096SFrank Li  * Phy section
1784793fc096SFrank Li  */
1785793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev)
1786793fc096SFrank Li {
1787793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
178845f5c327SPhilippe Reynes 	struct phy_device *phy_dev = ndev->phydev;
1789793fc096SFrank Li 	int status_change = 0;
1790793fc096SFrank Li 
17918ce5624fSRussell King 	/*
17928ce5624fSRussell King 	 * If the netdev is down, or is going down, we're not interested
17938ce5624fSRussell King 	 * in link state events, so just mark our idea of the link as down
17948ce5624fSRussell King 	 * and ignore the event.
17958ce5624fSRussell King 	 */
17968ce5624fSRussell King 	if (!netif_running(ndev) || !netif_device_present(ndev)) {
17978ce5624fSRussell King 		fep->link = 0;
17988ce5624fSRussell King 	} else if (phy_dev->link) {
1799793fc096SFrank Li 		if (!fep->link) {
1800793fc096SFrank Li 			fep->link = phy_dev->link;
1801793fc096SFrank Li 			status_change = 1;
1802793fc096SFrank Li 		}
1803793fc096SFrank Li 
1804ef83337dSRussell King 		if (fep->full_duplex != phy_dev->duplex) {
1805ef83337dSRussell King 			fep->full_duplex = phy_dev->duplex;
1806793fc096SFrank Li 			status_change = 1;
1807ef83337dSRussell King 		}
1808793fc096SFrank Li 
1809793fc096SFrank Li 		if (phy_dev->speed != fep->speed) {
1810793fc096SFrank Li 			fep->speed = phy_dev->speed;
1811793fc096SFrank Li 			status_change = 1;
1812793fc096SFrank Li 		}
1813793fc096SFrank Li 
1814793fc096SFrank Li 		/* if any of the above changed restart the FEC */
1815dbc64a8eSRussell King 		if (status_change) {
1816dbc64a8eSRussell King 			napi_disable(&fep->napi);
1817dbc64a8eSRussell King 			netif_tx_lock_bh(ndev);
1818ef83337dSRussell King 			fec_restart(ndev);
1819657ade07SRickard x Andersson 			netif_tx_wake_all_queues(ndev);
18206af42d42SRussell King 			netif_tx_unlock_bh(ndev);
1821dbc64a8eSRussell King 			napi_enable(&fep->napi);
1822dbc64a8eSRussell King 		}
1823793fc096SFrank Li 	} else {
1824793fc096SFrank Li 		if (fep->link) {
1825f208ce10SRussell King 			napi_disable(&fep->napi);
1826f208ce10SRussell King 			netif_tx_lock_bh(ndev);
1827793fc096SFrank Li 			fec_stop(ndev);
1828f208ce10SRussell King 			netif_tx_unlock_bh(ndev);
1829f208ce10SRussell King 			napi_enable(&fep->napi);
18306e0895c2SDavid S. Miller 			fep->link = phy_dev->link;
1831793fc096SFrank Li 			status_change = 1;
1832793fc096SFrank Li 		}
1833793fc096SFrank Li 	}
1834793fc096SFrank Li 
1835793fc096SFrank Li 	if (status_change)
1836793fc096SFrank Li 		phy_print_status(phy_dev);
1837793fc096SFrank Li }
1838793fc096SFrank Li 
1839f166f890SAndrew Lunn static int fec_enet_mdio_wait(struct fec_enet_private *fep)
1840f166f890SAndrew Lunn {
1841f166f890SAndrew Lunn 	uint ievent;
1842f166f890SAndrew Lunn 	int ret;
1843f166f890SAndrew Lunn 
1844f166f890SAndrew Lunn 	ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent,
1845f166f890SAndrew Lunn 					ievent & FEC_ENET_MII, 2, 30000);
1846f166f890SAndrew Lunn 
1847f166f890SAndrew Lunn 	if (!ret)
1848f166f890SAndrew Lunn 		writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
1849f166f890SAndrew Lunn 
1850f166f890SAndrew Lunn 	return ret;
1851f166f890SAndrew Lunn }
1852f166f890SAndrew Lunn 
1853793fc096SFrank Li static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1854793fc096SFrank Li {
1855793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
18568fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
1857d3ee8ec7SMarco Hartmann 	int ret = 0, frame_start, frame_addr, frame_op;
1858d3ee8ec7SMarco Hartmann 	bool is_c45 = !!(regnum & MII_ADDR_C45);
18598fff755eSAndrew Lunn 
1860da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
1861b0c6ce24SFabio Estevam 	if (ret < 0)
18628fff755eSAndrew Lunn 		return ret;
1863793fc096SFrank Li 
1864d3ee8ec7SMarco Hartmann 	if (is_c45) {
1865d3ee8ec7SMarco Hartmann 		frame_start = FEC_MMFR_ST_C45;
1866d3ee8ec7SMarco Hartmann 
1867d3ee8ec7SMarco Hartmann 		/* write address */
1868d3ee8ec7SMarco Hartmann 		frame_addr = (regnum >> 16);
1869d3ee8ec7SMarco Hartmann 		writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1870d3ee8ec7SMarco Hartmann 		       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1871d3ee8ec7SMarco Hartmann 		       FEC_MMFR_TA | (regnum & 0xFFFF),
1872d3ee8ec7SMarco Hartmann 		       fep->hwp + FEC_MII_DATA);
1873d3ee8ec7SMarco Hartmann 
1874d3ee8ec7SMarco Hartmann 		/* wait for end of transfer */
1875f166f890SAndrew Lunn 		ret = fec_enet_mdio_wait(fep);
1876f166f890SAndrew Lunn 		if (ret) {
1877d3ee8ec7SMarco Hartmann 			netdev_err(fep->netdev, "MDIO address write timeout\n");
1878d3ee8ec7SMarco Hartmann 			goto out;
1879d3ee8ec7SMarco Hartmann 		}
1880d3ee8ec7SMarco Hartmann 
1881d3ee8ec7SMarco Hartmann 		frame_op = FEC_MMFR_OP_READ_C45;
1882d3ee8ec7SMarco Hartmann 
1883d3ee8ec7SMarco Hartmann 	} else {
1884d3ee8ec7SMarco Hartmann 		/* C22 read */
1885d3ee8ec7SMarco Hartmann 		frame_op = FEC_MMFR_OP_READ;
1886d3ee8ec7SMarco Hartmann 		frame_start = FEC_MMFR_ST;
1887d3ee8ec7SMarco Hartmann 		frame_addr = regnum;
1888d3ee8ec7SMarco Hartmann 	}
1889d3ee8ec7SMarco Hartmann 
1890793fc096SFrank Li 	/* start a read op */
1891d3ee8ec7SMarco Hartmann 	writel(frame_start | frame_op |
1892d3ee8ec7SMarco Hartmann 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1893793fc096SFrank Li 		FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1894793fc096SFrank Li 
1895793fc096SFrank Li 	/* wait for end of transfer */
1896f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
1897f166f890SAndrew Lunn 	if (ret) {
189831b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO read timeout\n");
18998fff755eSAndrew Lunn 		goto out;
1900793fc096SFrank Li 	}
1901793fc096SFrank Li 
19028fff755eSAndrew Lunn 	ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
19038fff755eSAndrew Lunn 
19048fff755eSAndrew Lunn out:
19058fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
19068fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
19078fff755eSAndrew Lunn 
19088fff755eSAndrew Lunn 	return ret;
1909793fc096SFrank Li }
1910793fc096SFrank Li 
1911793fc096SFrank Li static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1912793fc096SFrank Li 			   u16 value)
1913793fc096SFrank Li {
1914793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
19158fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
1916d3ee8ec7SMarco Hartmann 	int ret, frame_start, frame_addr;
1917d3ee8ec7SMarco Hartmann 	bool is_c45 = !!(regnum & MII_ADDR_C45);
19188fff755eSAndrew Lunn 
1919da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
1920b0c6ce24SFabio Estevam 	if (ret < 0)
19218fff755eSAndrew Lunn 		return ret;
1922793fc096SFrank Li 
1923d3ee8ec7SMarco Hartmann 	if (is_c45) {
1924d3ee8ec7SMarco Hartmann 		frame_start = FEC_MMFR_ST_C45;
1925d3ee8ec7SMarco Hartmann 
1926d3ee8ec7SMarco Hartmann 		/* write address */
1927d3ee8ec7SMarco Hartmann 		frame_addr = (regnum >> 16);
1928d3ee8ec7SMarco Hartmann 		writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1929d3ee8ec7SMarco Hartmann 		       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1930d3ee8ec7SMarco Hartmann 		       FEC_MMFR_TA | (regnum & 0xFFFF),
1931d3ee8ec7SMarco Hartmann 		       fep->hwp + FEC_MII_DATA);
1932d3ee8ec7SMarco Hartmann 
1933d3ee8ec7SMarco Hartmann 		/* wait for end of transfer */
1934f166f890SAndrew Lunn 		ret = fec_enet_mdio_wait(fep);
1935f166f890SAndrew Lunn 		if (ret) {
1936d3ee8ec7SMarco Hartmann 			netdev_err(fep->netdev, "MDIO address write timeout\n");
1937d3ee8ec7SMarco Hartmann 			goto out;
1938d3ee8ec7SMarco Hartmann 		}
1939d3ee8ec7SMarco Hartmann 	} else {
1940d3ee8ec7SMarco Hartmann 		/* C22 write */
1941d3ee8ec7SMarco Hartmann 		frame_start = FEC_MMFR_ST;
1942d3ee8ec7SMarco Hartmann 		frame_addr = regnum;
1943d3ee8ec7SMarco Hartmann 	}
1944d3ee8ec7SMarco Hartmann 
1945793fc096SFrank Li 	/* start a write op */
1946d3ee8ec7SMarco Hartmann 	writel(frame_start | FEC_MMFR_OP_WRITE |
1947d3ee8ec7SMarco Hartmann 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1948793fc096SFrank Li 		FEC_MMFR_TA | FEC_MMFR_DATA(value),
1949793fc096SFrank Li 		fep->hwp + FEC_MII_DATA);
1950793fc096SFrank Li 
1951793fc096SFrank Li 	/* wait for end of transfer */
1952f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
1953f166f890SAndrew Lunn 	if (ret)
195431b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO write timeout\n");
1955793fc096SFrank Li 
1956d3ee8ec7SMarco Hartmann out:
19578fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
19588fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
19598fff755eSAndrew Lunn 
19608fff755eSAndrew Lunn 	return ret;
1961793fc096SFrank Li }
1962793fc096SFrank Li 
196364a632daSMarek Vasut static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
196464a632daSMarek Vasut {
196564a632daSMarek Vasut 	struct fec_enet_private *fep = netdev_priv(ndev);
196664a632daSMarek Vasut 	struct phy_device *phy_dev = ndev->phydev;
196764a632daSMarek Vasut 
196864a632daSMarek Vasut 	if (phy_dev) {
196964a632daSMarek Vasut 		phy_reset_after_clk_enable(phy_dev);
197064a632daSMarek Vasut 	} else if (fep->phy_node) {
197164a632daSMarek Vasut 		/*
197264a632daSMarek Vasut 		 * If the PHY still is not bound to the MAC, but there is
197364a632daSMarek Vasut 		 * OF PHY node and a matching PHY device instance already,
197464a632daSMarek Vasut 		 * use the OF PHY node to obtain the PHY device instance,
197564a632daSMarek Vasut 		 * and then use that PHY device instance when triggering
197664a632daSMarek Vasut 		 * the PHY reset.
197764a632daSMarek Vasut 		 */
197864a632daSMarek Vasut 		phy_dev = of_phy_find_device(fep->phy_node);
197964a632daSMarek Vasut 		phy_reset_after_clk_enable(phy_dev);
198064a632daSMarek Vasut 		put_device(&phy_dev->mdio.dev);
198164a632daSMarek Vasut 	}
198264a632daSMarek Vasut }
198364a632daSMarek Vasut 
1984e8fcfcd5SNimrod Andy static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1985e8fcfcd5SNimrod Andy {
1986e8fcfcd5SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
1987e8fcfcd5SNimrod Andy 	int ret;
1988e8fcfcd5SNimrod Andy 
1989e8fcfcd5SNimrod Andy 	if (enable) {
1990e8fcfcd5SNimrod Andy 		ret = clk_prepare_enable(fep->clk_enet_out);
1991e8fcfcd5SNimrod Andy 		if (ret)
1992d7c3a206SAndy Duan 			return ret;
199301e5943aSUwe Kleine-König 
1994e8fcfcd5SNimrod Andy 		if (fep->clk_ptp) {
199591c0d987SNimrod Andy 			mutex_lock(&fep->ptp_clk_mutex);
1996e8fcfcd5SNimrod Andy 			ret = clk_prepare_enable(fep->clk_ptp);
199791c0d987SNimrod Andy 			if (ret) {
199891c0d987SNimrod Andy 				mutex_unlock(&fep->ptp_clk_mutex);
1999e8fcfcd5SNimrod Andy 				goto failed_clk_ptp;
200091c0d987SNimrod Andy 			} else {
200191c0d987SNimrod Andy 				fep->ptp_clk_on = true;
200291c0d987SNimrod Andy 			}
200391c0d987SNimrod Andy 			mutex_unlock(&fep->ptp_clk_mutex);
2004e8fcfcd5SNimrod Andy 		}
200501e5943aSUwe Kleine-König 
20069b5330edSFugang Duan 		ret = clk_prepare_enable(fep->clk_ref);
20079b5330edSFugang Duan 		if (ret)
20089b5330edSFugang Duan 			goto failed_clk_ref;
20091b0a83acSRichard Leitner 
2010fc539459SFugang Duan 		ret = clk_prepare_enable(fep->clk_2x_txclk);
2011fc539459SFugang Duan 		if (ret)
2012fc539459SFugang Duan 			goto failed_clk_2x_txclk;
2013fc539459SFugang Duan 
201464a632daSMarek Vasut 		fec_enet_phy_reset_after_clk_enable(ndev);
2015e8fcfcd5SNimrod Andy 	} else {
2016e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_enet_out);
201791c0d987SNimrod Andy 		if (fep->clk_ptp) {
201891c0d987SNimrod Andy 			mutex_lock(&fep->ptp_clk_mutex);
2019e8fcfcd5SNimrod Andy 			clk_disable_unprepare(fep->clk_ptp);
202091c0d987SNimrod Andy 			fep->ptp_clk_on = false;
202191c0d987SNimrod Andy 			mutex_unlock(&fep->ptp_clk_mutex);
202291c0d987SNimrod Andy 		}
20239b5330edSFugang Duan 		clk_disable_unprepare(fep->clk_ref);
2024fc539459SFugang Duan 		clk_disable_unprepare(fep->clk_2x_txclk);
2025e8fcfcd5SNimrod Andy 	}
2026e8fcfcd5SNimrod Andy 
2027e8fcfcd5SNimrod Andy 	return 0;
20289b5330edSFugang Duan 
2029fc539459SFugang Duan failed_clk_2x_txclk:
2030fc539459SFugang Duan 	if (fep->clk_ref)
2031fc539459SFugang Duan 		clk_disable_unprepare(fep->clk_ref);
20329b5330edSFugang Duan failed_clk_ref:
2033a74d19baSLiu Xiang 	if (fep->clk_ptp) {
2034a74d19baSLiu Xiang 		mutex_lock(&fep->ptp_clk_mutex);
2035a74d19baSLiu Xiang 		clk_disable_unprepare(fep->clk_ptp);
2036a74d19baSLiu Xiang 		fep->ptp_clk_on = false;
2037a74d19baSLiu Xiang 		mutex_unlock(&fep->ptp_clk_mutex);
2038a74d19baSLiu Xiang 	}
2039e8fcfcd5SNimrod Andy failed_clk_ptp:
2040e8fcfcd5SNimrod Andy 	clk_disable_unprepare(fep->clk_enet_out);
2041e8fcfcd5SNimrod Andy 
2042e8fcfcd5SNimrod Andy 	return ret;
2043e8fcfcd5SNimrod Andy }
2044e8fcfcd5SNimrod Andy 
2045b820c114SJoakim Zhang static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep,
2046b820c114SJoakim Zhang 				      struct device_node *np)
2047b820c114SJoakim Zhang {
2048b820c114SJoakim Zhang 	u32 rgmii_tx_delay, rgmii_rx_delay;
2049b820c114SJoakim Zhang 
2050b820c114SJoakim Zhang 	/* For rgmii tx internal delay, valid values are 0ps and 2000ps */
2051b820c114SJoakim Zhang 	if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) {
2052b820c114SJoakim Zhang 		if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) {
2053b820c114SJoakim Zhang 			dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps");
2054b820c114SJoakim Zhang 			return -EINVAL;
2055b820c114SJoakim Zhang 		} else if (rgmii_tx_delay == 2000) {
2056b820c114SJoakim Zhang 			fep->rgmii_txc_dly = true;
2057b820c114SJoakim Zhang 		}
2058b820c114SJoakim Zhang 	}
2059b820c114SJoakim Zhang 
2060b820c114SJoakim Zhang 	/* For rgmii rx internal delay, valid values are 0ps and 2000ps */
2061b820c114SJoakim Zhang 	if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) {
2062b820c114SJoakim Zhang 		if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) {
2063b820c114SJoakim Zhang 			dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps");
2064b820c114SJoakim Zhang 			return -EINVAL;
2065b820c114SJoakim Zhang 		} else if (rgmii_rx_delay == 2000) {
2066b820c114SJoakim Zhang 			fep->rgmii_rxc_dly = true;
2067b820c114SJoakim Zhang 		}
2068b820c114SJoakim Zhang 	}
2069b820c114SJoakim Zhang 
2070b820c114SJoakim Zhang 	return 0;
2071b820c114SJoakim Zhang }
2072b820c114SJoakim Zhang 
2073793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev)
2074793fc096SFrank Li {
2075793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2076793fc096SFrank Li 	struct phy_device *phy_dev = NULL;
2077793fc096SFrank Li 	char mdio_bus_id[MII_BUS_ID_SIZE];
2078793fc096SFrank Li 	char phy_name[MII_BUS_ID_SIZE + 3];
2079793fc096SFrank Li 	int phy_id;
2080793fc096SFrank Li 	int dev_id = fep->dev_id;
2081793fc096SFrank Li 
2082407066f8SUwe Kleine-König 	if (fep->phy_node) {
2083407066f8SUwe Kleine-König 		phy_dev = of_phy_connect(ndev, fep->phy_node,
2084407066f8SUwe Kleine-König 					 &fec_enet_adjust_link, 0,
2085407066f8SUwe Kleine-König 					 fep->phy_interface);
20869558df3aSAndrew Lunn 		if (!phy_dev) {
20879558df3aSAndrew Lunn 			netdev_err(ndev, "Unable to connect to phy\n");
2088213a9922SNimrod Andy 			return -ENODEV;
20899558df3aSAndrew Lunn 		}
2090407066f8SUwe Kleine-König 	} else {
2091793fc096SFrank Li 		/* check for attached phy */
2092793fc096SFrank Li 		for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
20937f854420SAndrew Lunn 			if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
2094793fc096SFrank Li 				continue;
2095793fc096SFrank Li 			if (dev_id--)
2096793fc096SFrank Li 				continue;
2097949bdd20SRickard Strandqvist 			strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
2098793fc096SFrank Li 			break;
2099793fc096SFrank Li 		}
2100793fc096SFrank Li 
2101793fc096SFrank Li 		if (phy_id >= PHY_MAX_ADDR) {
210231b7720cSJoe Perches 			netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
2103949bdd20SRickard Strandqvist 			strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
2104793fc096SFrank Li 			phy_id = 0;
2105793fc096SFrank Li 		}
2106793fc096SFrank Li 
2107407066f8SUwe Kleine-König 		snprintf(phy_name, sizeof(phy_name),
2108407066f8SUwe Kleine-König 			 PHY_ID_FMT, mdio_bus_id, phy_id);
2109793fc096SFrank Li 		phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
2110793fc096SFrank Li 				      fep->phy_interface);
2111407066f8SUwe Kleine-König 	}
2112407066f8SUwe Kleine-König 
2113793fc096SFrank Li 	if (IS_ERR(phy_dev)) {
211431b7720cSJoe Perches 		netdev_err(ndev, "could not attach to PHY\n");
2115793fc096SFrank Li 		return PTR_ERR(phy_dev);
2116793fc096SFrank Li 	}
2117793fc096SFrank Li 
2118793fc096SFrank Li 	/* mask with MAC supported features */
21196b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
212058056c1eSAndrew Lunn 		phy_set_max_speed(phy_dev, 1000);
212141124fa6SAndrew Lunn 		phy_remove_link_mode(phy_dev,
212241124fa6SAndrew Lunn 				     ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
2123d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2124c306ad36SAndrew Lunn 		phy_support_sym_pause(phy_dev);
2125d1391930SGuenter Roeck #endif
2126793fc096SFrank Li 	}
2127793fc096SFrank Li 	else
212858056c1eSAndrew Lunn 		phy_set_max_speed(phy_dev, 100);
2129793fc096SFrank Li 
2130793fc096SFrank Li 	fep->link = 0;
2131793fc096SFrank Li 	fep->full_duplex = 0;
2132793fc096SFrank Li 
2133557d5dc8SHeiner Kallweit 	phy_dev->mac_managed_pm = 1;
2134557d5dc8SHeiner Kallweit 
21352220943aSAndrew Lunn 	phy_attached_info(phy_dev);
2136793fc096SFrank Li 
2137793fc096SFrank Li 	return 0;
2138793fc096SFrank Li }
2139793fc096SFrank Li 
2140793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev)
2141793fc096SFrank Li {
2142793fc096SFrank Li 	static struct mii_bus *fec0_mii_bus;
2143793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
2144793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
21453c01eb62SAndrew Lunn 	bool suppress_preamble = false;
2146407066f8SUwe Kleine-König 	struct device_node *node;
2147e7f4dc35SAndrew Lunn 	int err = -ENXIO;
214863c60732SUwe Kleine-König 	u32 mii_speed, holdtime;
21493e782985SAndrew Lunn 	u32 bus_freq;
2150793fc096SFrank Li 
2151793fc096SFrank Li 	/*
21523d125f9cSStefan Agner 	 * The i.MX28 dual fec interfaces are not equal.
2153793fc096SFrank Li 	 * Here are the differences:
2154793fc096SFrank Li 	 *
2155793fc096SFrank Li 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
2156793fc096SFrank Li 	 *  - fec0 acts as the 1588 time master while fec1 is slave
2157793fc096SFrank Li 	 *  - external phys can only be configured by fec0
2158793fc096SFrank Li 	 *
2159793fc096SFrank Li 	 * That is to say fec1 can not work independently. It only works
2160793fc096SFrank Li 	 * when fec0 is working. The reason behind this design is that the
2161793fc096SFrank Li 	 * second interface is added primarily for Switch mode.
2162793fc096SFrank Li 	 *
2163793fc096SFrank Li 	 * Because of the last point above, both phys are attached on fec0
2164793fc096SFrank Li 	 * mdio interface in board design, and need to be configured by
2165793fc096SFrank Li 	 * fec0 mii_bus.
2166793fc096SFrank Li 	 */
21673d125f9cSStefan Agner 	if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
2168793fc096SFrank Li 		/* fec1 uses fec0 mii_bus */
2169793fc096SFrank Li 		if (mii_cnt && fec0_mii_bus) {
2170793fc096SFrank Li 			fep->mii_bus = fec0_mii_bus;
2171793fc096SFrank Li 			mii_cnt++;
2172793fc096SFrank Li 			return 0;
2173793fc096SFrank Li 		}
2174793fc096SFrank Li 		return -ENOENT;
2175793fc096SFrank Li 	}
2176793fc096SFrank Li 
21773e782985SAndrew Lunn 	bus_freq = 2500000; /* 2.5MHz by default */
21783e782985SAndrew Lunn 	node = of_get_child_by_name(pdev->dev.of_node, "mdio");
21793c01eb62SAndrew Lunn 	if (node) {
21803e782985SAndrew Lunn 		of_property_read_u32(node, "clock-frequency", &bus_freq);
21813c01eb62SAndrew Lunn 		suppress_preamble = of_property_read_bool(node,
21823c01eb62SAndrew Lunn 							  "suppress-preamble");
21833c01eb62SAndrew Lunn 	}
21843e782985SAndrew Lunn 
2185793fc096SFrank Li 	/*
21863e782985SAndrew Lunn 	 * Set MII speed (= clk_get_rate() / 2 * phy_speed)
2187793fc096SFrank Li 	 *
2188793fc096SFrank Li 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2189793fc096SFrank Li 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
2190793fc096SFrank Li 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2191793fc096SFrank Li 	 * document.
2192793fc096SFrank Li 	 */
21933e782985SAndrew Lunn 	mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2);
21946b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC)
219563c60732SUwe Kleine-König 		mii_speed--;
219663c60732SUwe Kleine-König 	if (mii_speed > 63) {
219763c60732SUwe Kleine-König 		dev_err(&pdev->dev,
2198981a0547SPeter Meerwald-Stadler 			"fec clock (%lu) too fast to get right mii speed\n",
219963c60732SUwe Kleine-König 			clk_get_rate(fep->clk_ipg));
220063c60732SUwe Kleine-König 		err = -EINVAL;
220163c60732SUwe Kleine-König 		goto err_out;
220263c60732SUwe Kleine-König 	}
220363c60732SUwe Kleine-König 
220463c60732SUwe Kleine-König 	/*
220563c60732SUwe Kleine-König 	 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
220663c60732SUwe Kleine-König 	 * MII_SPEED) register that defines the MDIO output hold time. Earlier
220763c60732SUwe Kleine-König 	 * versions are RAZ there, so just ignore the difference and write the
220863c60732SUwe Kleine-König 	 * register always.
220963c60732SUwe Kleine-König 	 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
221063c60732SUwe Kleine-König 	 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
221163c60732SUwe Kleine-König 	 * output.
221263c60732SUwe Kleine-König 	 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
221363c60732SUwe Kleine-König 	 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
221463c60732SUwe Kleine-König 	 * holdtime cannot result in a value greater than 3.
221563c60732SUwe Kleine-König 	 */
221663c60732SUwe Kleine-König 	holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
221763c60732SUwe Kleine-König 
221863c60732SUwe Kleine-König 	fep->phy_speed = mii_speed << 1 | holdtime << 8;
221963c60732SUwe Kleine-König 
22203c01eb62SAndrew Lunn 	if (suppress_preamble)
22213c01eb62SAndrew Lunn 		fep->phy_speed |= BIT(7);
22223c01eb62SAndrew Lunn 
22231e6114f5SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) {
2224f166f890SAndrew Lunn 		/* Clear MMFR to avoid to generate MII event by writing MSCR.
2225f166f890SAndrew Lunn 		 * MII event generation condition:
2226f166f890SAndrew Lunn 		 * - writing MSCR:
2227f166f890SAndrew Lunn 		 *	- mmfr[31:0]_not_zero & mscr[7:0]_is_zero &
2228f166f890SAndrew Lunn 		 *	  mscr_reg_data_in[7:0] != 0
2229f166f890SAndrew Lunn 		 * - writing MMFR:
2230f166f890SAndrew Lunn 		 *	- mscr[7:0]_not_zero
2231f166f890SAndrew Lunn 		 */
2232f166f890SAndrew Lunn 		writel(0, fep->hwp + FEC_MII_DATA);
22331e6114f5SGreg Ungerer 	}
2234f166f890SAndrew Lunn 
2235793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2236793fc096SFrank Li 
2237f166f890SAndrew Lunn 	/* Clear any pending transaction complete indication */
2238f166f890SAndrew Lunn 	writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
2239f166f890SAndrew Lunn 
2240793fc096SFrank Li 	fep->mii_bus = mdiobus_alloc();
2241793fc096SFrank Li 	if (fep->mii_bus == NULL) {
2242793fc096SFrank Li 		err = -ENOMEM;
2243793fc096SFrank Li 		goto err_out;
2244793fc096SFrank Li 	}
2245793fc096SFrank Li 
2246793fc096SFrank Li 	fep->mii_bus->name = "fec_enet_mii_bus";
2247793fc096SFrank Li 	fep->mii_bus->read = fec_enet_mdio_read;
2248793fc096SFrank Li 	fep->mii_bus->write = fec_enet_mdio_write;
2249793fc096SFrank Li 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2250793fc096SFrank Li 		pdev->name, fep->dev_id + 1);
2251793fc096SFrank Li 	fep->mii_bus->priv = fep;
2252793fc096SFrank Li 	fep->mii_bus->parent = &pdev->dev;
2253793fc096SFrank Li 
2254407066f8SUwe Kleine-König 	err = of_mdiobus_register(fep->mii_bus, node);
2255407066f8SUwe Kleine-König 	if (err)
2256e7f4dc35SAndrew Lunn 		goto err_out_free_mdiobus;
22570607a2cdSPan Bian 	of_node_put(node);
2258793fc096SFrank Li 
2259793fc096SFrank Li 	mii_cnt++;
2260793fc096SFrank Li 
2261793fc096SFrank Li 	/* save fec0 mii_bus */
22623d125f9cSStefan Agner 	if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2263793fc096SFrank Li 		fec0_mii_bus = fep->mii_bus;
2264793fc096SFrank Li 
2265793fc096SFrank Li 	return 0;
2266793fc096SFrank Li 
2267793fc096SFrank Li err_out_free_mdiobus:
2268793fc096SFrank Li 	mdiobus_free(fep->mii_bus);
2269793fc096SFrank Li err_out:
22700607a2cdSPan Bian 	of_node_put(node);
2271793fc096SFrank Li 	return err;
2272793fc096SFrank Li }
2273793fc096SFrank Li 
2274793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep)
2275793fc096SFrank Li {
2276793fc096SFrank Li 	if (--mii_cnt == 0) {
2277793fc096SFrank Li 		mdiobus_unregister(fep->mii_bus);
2278793fc096SFrank Li 		mdiobus_free(fep->mii_bus);
2279793fc096SFrank Li 	}
2280793fc096SFrank Li }
2281793fc096SFrank Li 
2282793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev,
2283793fc096SFrank Li 				 struct ethtool_drvinfo *info)
2284793fc096SFrank Li {
2285793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2286793fc096SFrank Li 
2287793fc096SFrank Li 	strlcpy(info->driver, fep->pdev->dev.driver->name,
2288793fc096SFrank Li 		sizeof(info->driver));
2289793fc096SFrank Li 	strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2290793fc096SFrank Li }
2291793fc096SFrank Li 
2292db65f35fSPhilippe Reynes static int fec_enet_get_regs_len(struct net_device *ndev)
2293db65f35fSPhilippe Reynes {
2294db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2295db65f35fSPhilippe Reynes 	struct resource *r;
2296db65f35fSPhilippe Reynes 	int s = 0;
2297db65f35fSPhilippe Reynes 
2298db65f35fSPhilippe Reynes 	r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2299db65f35fSPhilippe Reynes 	if (r)
2300db65f35fSPhilippe Reynes 		s = resource_size(r);
2301db65f35fSPhilippe Reynes 
2302db65f35fSPhilippe Reynes 	return s;
2303db65f35fSPhilippe Reynes }
2304db65f35fSPhilippe Reynes 
2305db65f35fSPhilippe Reynes /* List of registers that can be safety be read to dump them with ethtool */
2306db65f35fSPhilippe Reynes #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
23073f1dcc6aSLucas Stach 	defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
230878cc6e7eSFlorian Fainelli 	defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
2309f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 2;
2310db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2311db65f35fSPhilippe Reynes 	FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2312db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2313db65f35fSPhilippe Reynes 	FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2314db65f35fSPhilippe Reynes 	FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2315db65f35fSPhilippe Reynes 	FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2316db65f35fSPhilippe Reynes 	FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2317db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2318db65f35fSPhilippe Reynes 	FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2319db65f35fSPhilippe Reynes 	FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2320db65f35fSPhilippe Reynes 	FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2321db65f35fSPhilippe Reynes 	FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2322db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2323db65f35fSPhilippe Reynes 	RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2324db65f35fSPhilippe Reynes 	RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2325db65f35fSPhilippe Reynes 	RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2326db65f35fSPhilippe Reynes 	RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2327db65f35fSPhilippe Reynes 	RMON_T_P_GTE2048, RMON_T_OCTETS,
2328db65f35fSPhilippe Reynes 	IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2329db65f35fSPhilippe Reynes 	IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2330db65f35fSPhilippe Reynes 	IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2331db65f35fSPhilippe Reynes 	RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2332db65f35fSPhilippe Reynes 	RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2333db65f35fSPhilippe Reynes 	RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2334db65f35fSPhilippe Reynes 	RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2335db65f35fSPhilippe Reynes 	RMON_R_P_GTE2048, RMON_R_OCTETS,
2336db65f35fSPhilippe Reynes 	IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2337db65f35fSPhilippe Reynes 	IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2338db65f35fSPhilippe Reynes };
2339db65f35fSPhilippe Reynes #else
2340f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 1;
2341db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2342db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2343db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2344db65f35fSPhilippe Reynes 	FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2345db65f35fSPhilippe Reynes 	FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2346db65f35fSPhilippe Reynes 	FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2347db65f35fSPhilippe Reynes 	FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2348db65f35fSPhilippe Reynes 	FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2349db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2350db65f35fSPhilippe Reynes 	FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2351db65f35fSPhilippe Reynes };
2352db65f35fSPhilippe Reynes #endif
2353db65f35fSPhilippe Reynes 
2354db65f35fSPhilippe Reynes static void fec_enet_get_regs(struct net_device *ndev,
2355db65f35fSPhilippe Reynes 			      struct ethtool_regs *regs, void *regbuf)
2356db65f35fSPhilippe Reynes {
2357db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2358db65f35fSPhilippe Reynes 	u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2359c72a0bc0SAndrew Lunn 	struct device *dev = &fep->pdev->dev;
2360db65f35fSPhilippe Reynes 	u32 *buf = (u32 *)regbuf;
2361db65f35fSPhilippe Reynes 	u32 i, off;
2362c72a0bc0SAndrew Lunn 	int ret;
2363c72a0bc0SAndrew Lunn 
2364da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
2365c72a0bc0SAndrew Lunn 	if (ret < 0)
2366c72a0bc0SAndrew Lunn 		return;
2367db65f35fSPhilippe Reynes 
2368f9bcc9f3SVivien Didelot 	regs->version = fec_enet_register_version;
2369f9bcc9f3SVivien Didelot 
2370db65f35fSPhilippe Reynes 	memset(buf, 0, regs->len);
2371db65f35fSPhilippe Reynes 
2372db65f35fSPhilippe Reynes 	for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2373ec20a63aSFugang Duan 		off = fec_enet_register_offset[i];
2374ec20a63aSFugang Duan 
2375ec20a63aSFugang Duan 		if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
2376ec20a63aSFugang Duan 		    !(fep->quirks & FEC_QUIRK_HAS_FRREG))
2377ec20a63aSFugang Duan 			continue;
2378ec20a63aSFugang Duan 
2379ec20a63aSFugang Duan 		off >>= 2;
2380db65f35fSPhilippe Reynes 		buf[off] = readl(&theregs[off]);
2381db65f35fSPhilippe Reynes 	}
2382c72a0bc0SAndrew Lunn 
2383c72a0bc0SAndrew Lunn 	pm_runtime_mark_last_busy(dev);
2384c72a0bc0SAndrew Lunn 	pm_runtime_put_autosuspend(dev);
2385db65f35fSPhilippe Reynes }
2386db65f35fSPhilippe Reynes 
2387793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev,
2388793fc096SFrank Li 				struct ethtool_ts_info *info)
2389793fc096SFrank Li {
2390793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2391793fc096SFrank Li 
2392793fc096SFrank Li 	if (fep->bufdesc_ex) {
2393793fc096SFrank Li 
2394793fc096SFrank Li 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2395793fc096SFrank Li 					SOF_TIMESTAMPING_RX_SOFTWARE |
2396793fc096SFrank Li 					SOF_TIMESTAMPING_SOFTWARE |
2397793fc096SFrank Li 					SOF_TIMESTAMPING_TX_HARDWARE |
2398793fc096SFrank Li 					SOF_TIMESTAMPING_RX_HARDWARE |
2399793fc096SFrank Li 					SOF_TIMESTAMPING_RAW_HARDWARE;
2400793fc096SFrank Li 		if (fep->ptp_clock)
2401793fc096SFrank Li 			info->phc_index = ptp_clock_index(fep->ptp_clock);
2402793fc096SFrank Li 		else
2403793fc096SFrank Li 			info->phc_index = -1;
2404793fc096SFrank Li 
2405793fc096SFrank Li 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2406793fc096SFrank Li 				 (1 << HWTSTAMP_TX_ON);
2407793fc096SFrank Li 
2408793fc096SFrank Li 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2409793fc096SFrank Li 				   (1 << HWTSTAMP_FILTER_ALL);
2410793fc096SFrank Li 		return 0;
2411793fc096SFrank Li 	} else {
2412793fc096SFrank Li 		return ethtool_op_get_ts_info(ndev, info);
2413793fc096SFrank Li 	}
2414793fc096SFrank Li }
2415793fc096SFrank Li 
2416d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2417d1391930SGuenter Roeck 
2418793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev,
2419793fc096SFrank Li 				    struct ethtool_pauseparam *pause)
2420793fc096SFrank Li {
2421793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2422793fc096SFrank Li 
2423793fc096SFrank Li 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2424793fc096SFrank Li 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2425793fc096SFrank Li 	pause->rx_pause = pause->tx_pause;
2426793fc096SFrank Li }
2427793fc096SFrank Li 
2428793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev,
2429793fc096SFrank Li 				   struct ethtool_pauseparam *pause)
2430793fc096SFrank Li {
2431793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2432793fc096SFrank Li 
243345f5c327SPhilippe Reynes 	if (!ndev->phydev)
24340b146ca8SRussell King 		return -ENODEV;
24350b146ca8SRussell King 
2436793fc096SFrank Li 	if (pause->tx_pause != pause->rx_pause) {
2437793fc096SFrank Li 		netdev_info(ndev,
2438793fc096SFrank Li 			"hardware only support enable/disable both tx and rx");
2439793fc096SFrank Li 		return -EINVAL;
2440793fc096SFrank Li 	}
2441793fc096SFrank Li 
2442793fc096SFrank Li 	fep->pause_flag = 0;
2443793fc096SFrank Li 
2444793fc096SFrank Li 	/* tx pause must be same as rx pause */
2445793fc096SFrank Li 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2446793fc096SFrank Li 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2447793fc096SFrank Li 
24480c122405SAndrew Lunn 	phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
24490c122405SAndrew Lunn 			  pause->autoneg);
2450793fc096SFrank Li 
2451793fc096SFrank Li 	if (pause->autoneg) {
2452793fc096SFrank Li 		if (netif_running(ndev))
2453793fc096SFrank Li 			fec_stop(ndev);
245445f5c327SPhilippe Reynes 		phy_start_aneg(ndev->phydev);
2455793fc096SFrank Li 	}
2456dbc64a8eSRussell King 	if (netif_running(ndev)) {
2457dbc64a8eSRussell King 		napi_disable(&fep->napi);
2458dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
2459ef83337dSRussell King 		fec_restart(ndev);
2460657ade07SRickard x Andersson 		netif_tx_wake_all_queues(ndev);
24616af42d42SRussell King 		netif_tx_unlock_bh(ndev);
2462dbc64a8eSRussell King 		napi_enable(&fep->napi);
2463dbc64a8eSRussell King 	}
2464793fc096SFrank Li 
2465793fc096SFrank Li 	return 0;
2466793fc096SFrank Li }
2467793fc096SFrank Li 
246838ae92dcSChris Healy static const struct fec_stat {
246938ae92dcSChris Healy 	char name[ETH_GSTRING_LEN];
247038ae92dcSChris Healy 	u16 offset;
247138ae92dcSChris Healy } fec_stats[] = {
247238ae92dcSChris Healy 	/* RMON TX */
247338ae92dcSChris Healy 	{ "tx_dropped", RMON_T_DROP },
247438ae92dcSChris Healy 	{ "tx_packets", RMON_T_PACKETS },
247538ae92dcSChris Healy 	{ "tx_broadcast", RMON_T_BC_PKT },
247638ae92dcSChris Healy 	{ "tx_multicast", RMON_T_MC_PKT },
247738ae92dcSChris Healy 	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
247838ae92dcSChris Healy 	{ "tx_undersize", RMON_T_UNDERSIZE },
247938ae92dcSChris Healy 	{ "tx_oversize", RMON_T_OVERSIZE },
248038ae92dcSChris Healy 	{ "tx_fragment", RMON_T_FRAG },
248138ae92dcSChris Healy 	{ "tx_jabber", RMON_T_JAB },
248238ae92dcSChris Healy 	{ "tx_collision", RMON_T_COL },
248338ae92dcSChris Healy 	{ "tx_64byte", RMON_T_P64 },
248438ae92dcSChris Healy 	{ "tx_65to127byte", RMON_T_P65TO127 },
248538ae92dcSChris Healy 	{ "tx_128to255byte", RMON_T_P128TO255 },
248638ae92dcSChris Healy 	{ "tx_256to511byte", RMON_T_P256TO511 },
248738ae92dcSChris Healy 	{ "tx_512to1023byte", RMON_T_P512TO1023 },
248838ae92dcSChris Healy 	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
248938ae92dcSChris Healy 	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
249038ae92dcSChris Healy 	{ "tx_octets", RMON_T_OCTETS },
249138ae92dcSChris Healy 
249238ae92dcSChris Healy 	/* IEEE TX */
249338ae92dcSChris Healy 	{ "IEEE_tx_drop", IEEE_T_DROP },
249438ae92dcSChris Healy 	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
249538ae92dcSChris Healy 	{ "IEEE_tx_1col", IEEE_T_1COL },
249638ae92dcSChris Healy 	{ "IEEE_tx_mcol", IEEE_T_MCOL },
249738ae92dcSChris Healy 	{ "IEEE_tx_def", IEEE_T_DEF },
249838ae92dcSChris Healy 	{ "IEEE_tx_lcol", IEEE_T_LCOL },
249938ae92dcSChris Healy 	{ "IEEE_tx_excol", IEEE_T_EXCOL },
250038ae92dcSChris Healy 	{ "IEEE_tx_macerr", IEEE_T_MACERR },
250138ae92dcSChris Healy 	{ "IEEE_tx_cserr", IEEE_T_CSERR },
250238ae92dcSChris Healy 	{ "IEEE_tx_sqe", IEEE_T_SQE },
250338ae92dcSChris Healy 	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
250438ae92dcSChris Healy 	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
250538ae92dcSChris Healy 
250638ae92dcSChris Healy 	/* RMON RX */
250738ae92dcSChris Healy 	{ "rx_packets", RMON_R_PACKETS },
250838ae92dcSChris Healy 	{ "rx_broadcast", RMON_R_BC_PKT },
250938ae92dcSChris Healy 	{ "rx_multicast", RMON_R_MC_PKT },
251038ae92dcSChris Healy 	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
251138ae92dcSChris Healy 	{ "rx_undersize", RMON_R_UNDERSIZE },
251238ae92dcSChris Healy 	{ "rx_oversize", RMON_R_OVERSIZE },
251338ae92dcSChris Healy 	{ "rx_fragment", RMON_R_FRAG },
251438ae92dcSChris Healy 	{ "rx_jabber", RMON_R_JAB },
251538ae92dcSChris Healy 	{ "rx_64byte", RMON_R_P64 },
251638ae92dcSChris Healy 	{ "rx_65to127byte", RMON_R_P65TO127 },
251738ae92dcSChris Healy 	{ "rx_128to255byte", RMON_R_P128TO255 },
251838ae92dcSChris Healy 	{ "rx_256to511byte", RMON_R_P256TO511 },
251938ae92dcSChris Healy 	{ "rx_512to1023byte", RMON_R_P512TO1023 },
252038ae92dcSChris Healy 	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
252138ae92dcSChris Healy 	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
252238ae92dcSChris Healy 	{ "rx_octets", RMON_R_OCTETS },
252338ae92dcSChris Healy 
252438ae92dcSChris Healy 	/* IEEE RX */
252538ae92dcSChris Healy 	{ "IEEE_rx_drop", IEEE_R_DROP },
252638ae92dcSChris Healy 	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
252738ae92dcSChris Healy 	{ "IEEE_rx_crc", IEEE_R_CRC },
252838ae92dcSChris Healy 	{ "IEEE_rx_align", IEEE_R_ALIGN },
252938ae92dcSChris Healy 	{ "IEEE_rx_macerr", IEEE_R_MACERR },
253038ae92dcSChris Healy 	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
253138ae92dcSChris Healy 	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
253238ae92dcSChris Healy };
253338ae92dcSChris Healy 
2534f85de666SNikita Yushchenko #define FEC_STATS_SIZE		(ARRAY_SIZE(fec_stats) * sizeof(u64))
2535f85de666SNikita Yushchenko 
253680cca775SNikita Yushchenko static void fec_enet_update_ethtool_stats(struct net_device *dev)
253738ae92dcSChris Healy {
253838ae92dcSChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
253938ae92dcSChris Healy 	int i;
254038ae92dcSChris Healy 
254138ae92dcSChris Healy 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
254280cca775SNikita Yushchenko 		fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
254380cca775SNikita Yushchenko }
254480cca775SNikita Yushchenko 
254580cca775SNikita Yushchenko static void fec_enet_get_ethtool_stats(struct net_device *dev,
254680cca775SNikita Yushchenko 				       struct ethtool_stats *stats, u64 *data)
254780cca775SNikita Yushchenko {
254880cca775SNikita Yushchenko 	struct fec_enet_private *fep = netdev_priv(dev);
254980cca775SNikita Yushchenko 
255080cca775SNikita Yushchenko 	if (netif_running(dev))
255180cca775SNikita Yushchenko 		fec_enet_update_ethtool_stats(dev);
255280cca775SNikita Yushchenko 
2553f85de666SNikita Yushchenko 	memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
255438ae92dcSChris Healy }
255538ae92dcSChris Healy 
255638ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev,
255738ae92dcSChris Healy 	u32 stringset, u8 *data)
255838ae92dcSChris Healy {
255938ae92dcSChris Healy 	int i;
256038ae92dcSChris Healy 	switch (stringset) {
256138ae92dcSChris Healy 	case ETH_SS_STATS:
256238ae92dcSChris Healy 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
256338ae92dcSChris Healy 			memcpy(data + i * ETH_GSTRING_LEN,
256438ae92dcSChris Healy 				fec_stats[i].name, ETH_GSTRING_LEN);
256538ae92dcSChris Healy 		break;
25666016ba34SOleksij Rempel 	case ETH_SS_TEST:
25676016ba34SOleksij Rempel 		net_selftest_get_strings(data);
25686016ba34SOleksij Rempel 		break;
256938ae92dcSChris Healy 	}
257038ae92dcSChris Healy }
257138ae92dcSChris Healy 
257238ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset)
257338ae92dcSChris Healy {
257438ae92dcSChris Healy 	switch (sset) {
257538ae92dcSChris Healy 	case ETH_SS_STATS:
257638ae92dcSChris Healy 		return ARRAY_SIZE(fec_stats);
25776016ba34SOleksij Rempel 	case ETH_SS_TEST:
25786016ba34SOleksij Rempel 		return net_selftest_get_count();
257938ae92dcSChris Healy 	default:
258038ae92dcSChris Healy 		return -EOPNOTSUPP;
258138ae92dcSChris Healy 	}
258238ae92dcSChris Healy }
2583f85de666SNikita Yushchenko 
25842b30842bSAndrew Lunn static void fec_enet_clear_ethtool_stats(struct net_device *dev)
25852b30842bSAndrew Lunn {
25862b30842bSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(dev);
25872b30842bSAndrew Lunn 	int i;
25882b30842bSAndrew Lunn 
25892b30842bSAndrew Lunn 	/* Disable MIB statistics counters */
25902b30842bSAndrew Lunn 	writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
25912b30842bSAndrew Lunn 
25922b30842bSAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
25932b30842bSAndrew Lunn 		writel(0, fep->hwp + fec_stats[i].offset);
25942b30842bSAndrew Lunn 
25952b30842bSAndrew Lunn 	/* Don't disable MIB statistics counters */
25962b30842bSAndrew Lunn 	writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
25972b30842bSAndrew Lunn }
25982b30842bSAndrew Lunn 
2599f85de666SNikita Yushchenko #else	/* !defined(CONFIG_M5272) */
2600f85de666SNikita Yushchenko #define FEC_STATS_SIZE	0
2601f85de666SNikita Yushchenko static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2602f85de666SNikita Yushchenko {
2603f85de666SNikita Yushchenko }
260441e8e404SFabio Estevam 
260541e8e404SFabio Estevam static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
260641e8e404SFabio Estevam {
260741e8e404SFabio Estevam }
2608d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
260938ae92dcSChris Healy 
2610d851b47bSFugang Duan /* ITR clock source is enet system clock (clk_ahb).
2611d851b47bSFugang Duan  * TCTT unit is cycle_ns * 64 cycle
2612d851b47bSFugang Duan  * So, the ICTT value = X us / (cycle_ns * 64)
2613d851b47bSFugang Duan  */
2614d851b47bSFugang Duan static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2615d851b47bSFugang Duan {
2616d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2617d851b47bSFugang Duan 
2618d851b47bSFugang Duan 	return us * (fep->itr_clk_rate / 64000) / 1000;
2619d851b47bSFugang Duan }
2620d851b47bSFugang Duan 
2621d851b47bSFugang Duan /* Set threshold for interrupt coalescing */
2622d851b47bSFugang Duan static void fec_enet_itr_coal_set(struct net_device *ndev)
2623d851b47bSFugang Duan {
2624d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2625d851b47bSFugang Duan 	int rx_itr, tx_itr;
2626d851b47bSFugang Duan 
2627d851b47bSFugang Duan 	/* Must be greater than zero to avoid unpredictable behavior */
2628d851b47bSFugang Duan 	if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2629d851b47bSFugang Duan 	    !fep->tx_time_itr || !fep->tx_pkts_itr)
2630d851b47bSFugang Duan 		return;
2631d851b47bSFugang Duan 
2632d851b47bSFugang Duan 	/* Select enet system clock as Interrupt Coalescing
2633d851b47bSFugang Duan 	 * timer Clock Source
2634d851b47bSFugang Duan 	 */
2635d851b47bSFugang Duan 	rx_itr = FEC_ITR_CLK_SEL;
2636d851b47bSFugang Duan 	tx_itr = FEC_ITR_CLK_SEL;
2637d851b47bSFugang Duan 
2638d851b47bSFugang Duan 	/* set ICFT and ICTT */
2639d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2640d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2641d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2642d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2643d851b47bSFugang Duan 
2644d851b47bSFugang Duan 	rx_itr |= FEC_ITR_EN;
2645d851b47bSFugang Duan 	tx_itr |= FEC_ITR_EN;
2646d851b47bSFugang Duan 
2647d851b47bSFugang Duan 	writel(tx_itr, fep->hwp + FEC_TXIC0);
2648d851b47bSFugang Duan 	writel(rx_itr, fep->hwp + FEC_RXIC0);
2649471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
2650d851b47bSFugang Duan 		writel(tx_itr, fep->hwp + FEC_TXIC1);
2651d851b47bSFugang Duan 		writel(rx_itr, fep->hwp + FEC_RXIC1);
2652d851b47bSFugang Duan 		writel(tx_itr, fep->hwp + FEC_TXIC2);
2653d851b47bSFugang Duan 		writel(rx_itr, fep->hwp + FEC_RXIC2);
2654d851b47bSFugang Duan 	}
2655ff7566b8SFugang Duan }
2656d851b47bSFugang Duan 
2657f3ccfda1SYufeng Mo static int fec_enet_get_coalesce(struct net_device *ndev,
2658f3ccfda1SYufeng Mo 				 struct ethtool_coalesce *ec,
2659f3ccfda1SYufeng Mo 				 struct kernel_ethtool_coalesce *kernel_coal,
2660f3ccfda1SYufeng Mo 				 struct netlink_ext_ack *extack)
2661d851b47bSFugang Duan {
2662d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2663d851b47bSFugang Duan 
2664ff7566b8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2665d851b47bSFugang Duan 		return -EOPNOTSUPP;
2666d851b47bSFugang Duan 
2667d851b47bSFugang Duan 	ec->rx_coalesce_usecs = fep->rx_time_itr;
2668d851b47bSFugang Duan 	ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2669d851b47bSFugang Duan 
2670d851b47bSFugang Duan 	ec->tx_coalesce_usecs = fep->tx_time_itr;
2671d851b47bSFugang Duan 	ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2672d851b47bSFugang Duan 
2673d851b47bSFugang Duan 	return 0;
2674d851b47bSFugang Duan }
2675d851b47bSFugang Duan 
2676f3ccfda1SYufeng Mo static int fec_enet_set_coalesce(struct net_device *ndev,
2677f3ccfda1SYufeng Mo 				 struct ethtool_coalesce *ec,
2678f3ccfda1SYufeng Mo 				 struct kernel_ethtool_coalesce *kernel_coal,
2679f3ccfda1SYufeng Mo 				 struct netlink_ext_ack *extack)
2680d851b47bSFugang Duan {
2681d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2682517a772cSFabio Estevam 	struct device *dev = &fep->pdev->dev;
2683d851b47bSFugang Duan 	unsigned int cycle;
2684d851b47bSFugang Duan 
2685ff7566b8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2686d851b47bSFugang Duan 		return -EOPNOTSUPP;
2687d851b47bSFugang Duan 
2688d851b47bSFugang Duan 	if (ec->rx_max_coalesced_frames > 255) {
2689517a772cSFabio Estevam 		dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
2690d851b47bSFugang Duan 		return -EINVAL;
2691d851b47bSFugang Duan 	}
2692d851b47bSFugang Duan 
2693d851b47bSFugang Duan 	if (ec->tx_max_coalesced_frames > 255) {
2694517a772cSFabio Estevam 		dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
2695d851b47bSFugang Duan 		return -EINVAL;
2696d851b47bSFugang Duan 	}
2697d851b47bSFugang Duan 
2698ab14961dSJakub Kicinski 	cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
2699d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
2700517a772cSFabio Estevam 		dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
2701d851b47bSFugang Duan 		return -EINVAL;
2702d851b47bSFugang Duan 	}
2703d851b47bSFugang Duan 
2704ab14961dSJakub Kicinski 	cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
2705d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
2706ab14961dSJakub Kicinski 		dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
2707d851b47bSFugang Duan 		return -EINVAL;
2708d851b47bSFugang Duan 	}
2709d851b47bSFugang Duan 
2710d851b47bSFugang Duan 	fep->rx_time_itr = ec->rx_coalesce_usecs;
2711d851b47bSFugang Duan 	fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2712d851b47bSFugang Duan 
2713d851b47bSFugang Duan 	fep->tx_time_itr = ec->tx_coalesce_usecs;
2714d851b47bSFugang Duan 	fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2715d851b47bSFugang Duan 
2716d851b47bSFugang Duan 	fec_enet_itr_coal_set(ndev);
2717d851b47bSFugang Duan 
2718d851b47bSFugang Duan 	return 0;
2719d851b47bSFugang Duan }
2720d851b47bSFugang Duan 
2721d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev)
2722d851b47bSFugang Duan {
2723d851b47bSFugang Duan 	struct ethtool_coalesce ec;
2724d851b47bSFugang Duan 
2725d851b47bSFugang Duan 	ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2726d851b47bSFugang Duan 	ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2727d851b47bSFugang Duan 
2728d851b47bSFugang Duan 	ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2729d851b47bSFugang Duan 	ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2730d851b47bSFugang Duan 
2731f3ccfda1SYufeng Mo 	fec_enet_set_coalesce(ndev, &ec, NULL, NULL);
2732d851b47bSFugang Duan }
2733d851b47bSFugang Duan 
27341b7bde6dSNimrod Andy static int fec_enet_get_tunable(struct net_device *netdev,
27351b7bde6dSNimrod Andy 				const struct ethtool_tunable *tuna,
27361b7bde6dSNimrod Andy 				void *data)
27371b7bde6dSNimrod Andy {
27381b7bde6dSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
27391b7bde6dSNimrod Andy 	int ret = 0;
27401b7bde6dSNimrod Andy 
27411b7bde6dSNimrod Andy 	switch (tuna->id) {
27421b7bde6dSNimrod Andy 	case ETHTOOL_RX_COPYBREAK:
27431b7bde6dSNimrod Andy 		*(u32 *)data = fep->rx_copybreak;
27441b7bde6dSNimrod Andy 		break;
27451b7bde6dSNimrod Andy 	default:
27461b7bde6dSNimrod Andy 		ret = -EINVAL;
27471b7bde6dSNimrod Andy 		break;
27481b7bde6dSNimrod Andy 	}
27491b7bde6dSNimrod Andy 
27501b7bde6dSNimrod Andy 	return ret;
27511b7bde6dSNimrod Andy }
27521b7bde6dSNimrod Andy 
27531b7bde6dSNimrod Andy static int fec_enet_set_tunable(struct net_device *netdev,
27541b7bde6dSNimrod Andy 				const struct ethtool_tunable *tuna,
27551b7bde6dSNimrod Andy 				const void *data)
27561b7bde6dSNimrod Andy {
27571b7bde6dSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
27581b7bde6dSNimrod Andy 	int ret = 0;
27591b7bde6dSNimrod Andy 
27601b7bde6dSNimrod Andy 	switch (tuna->id) {
27611b7bde6dSNimrod Andy 	case ETHTOOL_RX_COPYBREAK:
27621b7bde6dSNimrod Andy 		fep->rx_copybreak = *(u32 *)data;
27631b7bde6dSNimrod Andy 		break;
27641b7bde6dSNimrod Andy 	default:
27651b7bde6dSNimrod Andy 		ret = -EINVAL;
27661b7bde6dSNimrod Andy 		break;
27671b7bde6dSNimrod Andy 	}
27681b7bde6dSNimrod Andy 
27691b7bde6dSNimrod Andy 	return ret;
27701b7bde6dSNimrod Andy }
27711b7bde6dSNimrod Andy 
2772b82f8c3fSFugang Duan /* LPI Sleep Ts count base on tx clk (clk_ref).
2773b82f8c3fSFugang Duan  * The lpi sleep cnt value = X us / (cycle_ns).
2774b82f8c3fSFugang Duan  */
2775b82f8c3fSFugang Duan static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
2776b82f8c3fSFugang Duan {
2777b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2778b82f8c3fSFugang Duan 
2779b82f8c3fSFugang Duan 	return us * (fep->clk_ref_rate / 1000) / 1000;
2780b82f8c3fSFugang Duan }
2781b82f8c3fSFugang Duan 
2782b82f8c3fSFugang Duan static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable)
2783b82f8c3fSFugang Duan {
2784b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2785b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
2786b82f8c3fSFugang Duan 	unsigned int sleep_cycle, wake_cycle;
2787b82f8c3fSFugang Duan 	int ret = 0;
2788b82f8c3fSFugang Duan 
2789b82f8c3fSFugang Duan 	if (enable) {
2790b82f8c3fSFugang Duan 		ret = phy_init_eee(ndev->phydev, 0);
2791b82f8c3fSFugang Duan 		if (ret)
2792b82f8c3fSFugang Duan 			return ret;
2793b82f8c3fSFugang Duan 
2794b82f8c3fSFugang Duan 		sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer);
2795b82f8c3fSFugang Duan 		wake_cycle = sleep_cycle;
2796b82f8c3fSFugang Duan 	} else {
2797b82f8c3fSFugang Duan 		sleep_cycle = 0;
2798b82f8c3fSFugang Duan 		wake_cycle = 0;
2799b82f8c3fSFugang Duan 	}
2800b82f8c3fSFugang Duan 
2801b82f8c3fSFugang Duan 	p->tx_lpi_enabled = enable;
2802b82f8c3fSFugang Duan 	p->eee_enabled = enable;
2803b82f8c3fSFugang Duan 	p->eee_active = enable;
2804b82f8c3fSFugang Duan 
2805b82f8c3fSFugang Duan 	writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
2806b82f8c3fSFugang Duan 	writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
2807b82f8c3fSFugang Duan 
2808b82f8c3fSFugang Duan 	return 0;
2809b82f8c3fSFugang Duan }
2810b82f8c3fSFugang Duan 
2811b82f8c3fSFugang Duan static int
2812b82f8c3fSFugang Duan fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2813b82f8c3fSFugang Duan {
2814b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2815b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
2816b82f8c3fSFugang Duan 
2817b82f8c3fSFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
2818b82f8c3fSFugang Duan 		return -EOPNOTSUPP;
2819b82f8c3fSFugang Duan 
2820b82f8c3fSFugang Duan 	if (!netif_running(ndev))
2821b82f8c3fSFugang Duan 		return -ENETDOWN;
2822b82f8c3fSFugang Duan 
2823b82f8c3fSFugang Duan 	edata->eee_enabled = p->eee_enabled;
2824b82f8c3fSFugang Duan 	edata->eee_active = p->eee_active;
2825b82f8c3fSFugang Duan 	edata->tx_lpi_timer = p->tx_lpi_timer;
2826b82f8c3fSFugang Duan 	edata->tx_lpi_enabled = p->tx_lpi_enabled;
2827b82f8c3fSFugang Duan 
2828b82f8c3fSFugang Duan 	return phy_ethtool_get_eee(ndev->phydev, edata);
2829b82f8c3fSFugang Duan }
2830b82f8c3fSFugang Duan 
2831b82f8c3fSFugang Duan static int
2832b82f8c3fSFugang Duan fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2833b82f8c3fSFugang Duan {
2834b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2835b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
2836b82f8c3fSFugang Duan 	int ret = 0;
2837b82f8c3fSFugang Duan 
2838b82f8c3fSFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
2839b82f8c3fSFugang Duan 		return -EOPNOTSUPP;
2840b82f8c3fSFugang Duan 
2841b82f8c3fSFugang Duan 	if (!netif_running(ndev))
2842b82f8c3fSFugang Duan 		return -ENETDOWN;
2843b82f8c3fSFugang Duan 
2844b82f8c3fSFugang Duan 	p->tx_lpi_timer = edata->tx_lpi_timer;
2845b82f8c3fSFugang Duan 
2846b82f8c3fSFugang Duan 	if (!edata->eee_enabled || !edata->tx_lpi_enabled ||
2847b82f8c3fSFugang Duan 	    !edata->tx_lpi_timer)
2848b82f8c3fSFugang Duan 		ret = fec_enet_eee_mode_set(ndev, false);
2849b82f8c3fSFugang Duan 	else
2850b82f8c3fSFugang Duan 		ret = fec_enet_eee_mode_set(ndev, true);
2851b82f8c3fSFugang Duan 
2852b82f8c3fSFugang Duan 	if (ret)
2853b82f8c3fSFugang Duan 		return ret;
2854b82f8c3fSFugang Duan 
2855b82f8c3fSFugang Duan 	return phy_ethtool_set_eee(ndev->phydev, edata);
2856b82f8c3fSFugang Duan }
2857b82f8c3fSFugang Duan 
2858de40ed31SNimrod Andy static void
2859de40ed31SNimrod Andy fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2860de40ed31SNimrod Andy {
2861de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2862de40ed31SNimrod Andy 
2863de40ed31SNimrod Andy 	if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2864de40ed31SNimrod Andy 		wol->supported = WAKE_MAGIC;
2865de40ed31SNimrod Andy 		wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2866de40ed31SNimrod Andy 	} else {
2867de40ed31SNimrod Andy 		wol->supported = wol->wolopts = 0;
2868de40ed31SNimrod Andy 	}
2869de40ed31SNimrod Andy }
2870de40ed31SNimrod Andy 
2871de40ed31SNimrod Andy static int
2872de40ed31SNimrod Andy fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2873de40ed31SNimrod Andy {
2874de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2875de40ed31SNimrod Andy 
2876de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2877de40ed31SNimrod Andy 		return -EINVAL;
2878de40ed31SNimrod Andy 
2879de40ed31SNimrod Andy 	if (wol->wolopts & ~WAKE_MAGIC)
2880de40ed31SNimrod Andy 		return -EINVAL;
2881de40ed31SNimrod Andy 
2882de40ed31SNimrod Andy 	device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2883de40ed31SNimrod Andy 	if (device_may_wakeup(&ndev->dev)) {
2884de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2885b7cdc965SJoakim Zhang 		if (fep->wake_irq > 0)
2886b7cdc965SJoakim Zhang 			enable_irq_wake(fep->wake_irq);
2887de40ed31SNimrod Andy 	} else {
2888de40ed31SNimrod Andy 		fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2889b7cdc965SJoakim Zhang 		if (fep->wake_irq > 0)
2890b7cdc965SJoakim Zhang 			disable_irq_wake(fep->wake_irq);
2891de40ed31SNimrod Andy 	}
2892de40ed31SNimrod Andy 
2893de40ed31SNimrod Andy 	return 0;
2894de40ed31SNimrod Andy }
2895de40ed31SNimrod Andy 
2896793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = {
2897d5e3c87dSJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2898d5e3c87dSJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES,
2899793fc096SFrank Li 	.get_drvinfo		= fec_enet_get_drvinfo,
2900db65f35fSPhilippe Reynes 	.get_regs_len		= fec_enet_get_regs_len,
2901db65f35fSPhilippe Reynes 	.get_regs		= fec_enet_get_regs,
290211d59289SFlorian Fainelli 	.nway_reset		= phy_ethtool_nway_reset,
2903c1d7c48fSRussell King 	.get_link		= ethtool_op_get_link,
2904d851b47bSFugang Duan 	.get_coalesce		= fec_enet_get_coalesce,
2905d851b47bSFugang Duan 	.set_coalesce		= fec_enet_set_coalesce,
290638ae92dcSChris Healy #ifndef CONFIG_M5272
2907c1d7c48fSRussell King 	.get_pauseparam		= fec_enet_get_pauseparam,
2908c1d7c48fSRussell King 	.set_pauseparam		= fec_enet_set_pauseparam,
290938ae92dcSChris Healy 	.get_strings		= fec_enet_get_strings,
2910c1d7c48fSRussell King 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
291138ae92dcSChris Healy 	.get_sset_count		= fec_enet_get_sset_count,
291238ae92dcSChris Healy #endif
2913c1d7c48fSRussell King 	.get_ts_info		= fec_enet_get_ts_info,
29141b7bde6dSNimrod Andy 	.get_tunable		= fec_enet_get_tunable,
29151b7bde6dSNimrod Andy 	.set_tunable		= fec_enet_set_tunable,
2916de40ed31SNimrod Andy 	.get_wol		= fec_enet_get_wol,
2917de40ed31SNimrod Andy 	.set_wol		= fec_enet_set_wol,
2918b82f8c3fSFugang Duan 	.get_eee		= fec_enet_get_eee,
2919b82f8c3fSFugang Duan 	.set_eee		= fec_enet_set_eee,
29209365fbf5SPhilippe Reynes 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
29219365fbf5SPhilippe Reynes 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
29226016ba34SOleksij Rempel 	.self_test		= net_selftest,
2923793fc096SFrank Li };
2924793fc096SFrank Li 
2925793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2926793fc096SFrank Li {
2927793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
292845f5c327SPhilippe Reynes 	struct phy_device *phydev = ndev->phydev;
2929793fc096SFrank Li 
2930793fc096SFrank Li 	if (!netif_running(ndev))
2931793fc096SFrank Li 		return -EINVAL;
2932793fc096SFrank Li 
2933793fc096SFrank Li 	if (!phydev)
2934793fc096SFrank Li 		return -ENODEV;
2935793fc096SFrank Li 
29361d5244d0SBen Hutchings 	if (fep->bufdesc_ex) {
293734074639SSergey Organov 		bool use_fec_hwts = !phy_has_hwtstamp(phydev);
293834074639SSergey Organov 
293934074639SSergey Organov 		if (cmd == SIOCSHWTSTAMP) {
294034074639SSergey Organov 			if (use_fec_hwts)
29411d5244d0SBen Hutchings 				return fec_ptp_set(ndev, rq);
294234074639SSergey Organov 			fec_ptp_disable_hwts(ndev);
294334074639SSergey Organov 		} else if (cmd == SIOCGHWTSTAMP) {
294434074639SSergey Organov 			if (use_fec_hwts)
29451d5244d0SBen Hutchings 				return fec_ptp_get(ndev, rq);
29461d5244d0SBen Hutchings 		}
294734074639SSergey Organov 	}
2948793fc096SFrank Li 
2949793fc096SFrank Li 	return phy_mii_ioctl(phydev, rq, cmd);
2950793fc096SFrank Li }
2951793fc096SFrank Li 
2952793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev)
2953793fc096SFrank Li {
2954793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2955793fc096SFrank Li 	unsigned int i;
2956793fc096SFrank Li 	struct sk_buff *skb;
2957793fc096SFrank Li 	struct bufdesc	*bdp;
29584d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
29594d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
296059d0f746SFrank Li 	unsigned int q;
2961793fc096SFrank Li 
296259d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
296359d0f746SFrank Li 		rxq = fep->rx_queue[q];
29647355f276STroy Kisky 		bdp = rxq->bd.base;
29657355f276STroy Kisky 		for (i = 0; i < rxq->bd.ring_size; i++) {
29664d494cdcSFugang Duan 			skb = rxq->rx_skbuff[i];
29674d494cdcSFugang Duan 			rxq->rx_skbuff[i] = NULL;
2968730ee360SRussell King 			if (skb) {
296959d0f746SFrank Li 				dma_unmap_single(&fep->pdev->dev,
29705cfa3039SJohannes Berg 						 fec32_to_cpu(bdp->cbd_bufaddr),
2971b64bf4b7SNimrod Andy 						 FEC_ENET_RX_FRSIZE - fep->rx_align,
297259d0f746SFrank Li 						 DMA_FROM_DEVICE);
2973793fc096SFrank Li 				dev_kfree_skb(skb);
2974730ee360SRussell King 			}
29757355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
297659d0f746SFrank Li 		}
2977793fc096SFrank Li 	}
2978793fc096SFrank Li 
297959d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
298059d0f746SFrank Li 		txq = fep->tx_queue[q];
29817355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
29824d494cdcSFugang Duan 			kfree(txq->tx_bounce[i]);
29834d494cdcSFugang Duan 			txq->tx_bounce[i] = NULL;
29844d494cdcSFugang Duan 			skb = txq->tx_skbuff[i];
29854d494cdcSFugang Duan 			txq->tx_skbuff[i] = NULL;
29868b7c9efaSRussell King 			dev_kfree_skb(skb);
29878b7c9efaSRussell King 		}
2988793fc096SFrank Li 	}
298959d0f746SFrank Li }
2990793fc096SFrank Li 
299159d0f746SFrank Li static void fec_enet_free_queue(struct net_device *ndev)
299259d0f746SFrank Li {
299359d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
299459d0f746SFrank Li 	int i;
299559d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
299659d0f746SFrank Li 
299759d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
299859d0f746SFrank Li 		if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
299959d0f746SFrank Li 			txq = fep->tx_queue[i];
300094920128SFugang Duan 			dma_free_coherent(&fep->pdev->dev,
30017355f276STroy Kisky 					  txq->bd.ring_size * TSO_HEADER_SIZE,
300259d0f746SFrank Li 					  txq->tso_hdrs,
300359d0f746SFrank Li 					  txq->tso_hdrs_dma);
300459d0f746SFrank Li 		}
300559d0f746SFrank Li 
300659d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
300759d0f746SFrank Li 		kfree(fep->rx_queue[i]);
300859d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
300959d0f746SFrank Li 		kfree(fep->tx_queue[i]);
301059d0f746SFrank Li }
301159d0f746SFrank Li 
301259d0f746SFrank Li static int fec_enet_alloc_queue(struct net_device *ndev)
301359d0f746SFrank Li {
301459d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
301559d0f746SFrank Li 	int i;
301659d0f746SFrank Li 	int ret = 0;
301759d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
301859d0f746SFrank Li 
301959d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
302059d0f746SFrank Li 		txq = kzalloc(sizeof(*txq), GFP_KERNEL);
302159d0f746SFrank Li 		if (!txq) {
302259d0f746SFrank Li 			ret = -ENOMEM;
302359d0f746SFrank Li 			goto alloc_failed;
302459d0f746SFrank Li 		}
302559d0f746SFrank Li 
302659d0f746SFrank Li 		fep->tx_queue[i] = txq;
30277355f276STroy Kisky 		txq->bd.ring_size = TX_RING_SIZE;
30287355f276STroy Kisky 		fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
302959d0f746SFrank Li 
303059d0f746SFrank Li 		txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
303159d0f746SFrank Li 		txq->tx_wake_threshold =
30327355f276STroy Kisky 			(txq->bd.ring_size - txq->tx_stop_threshold) / 2;
303359d0f746SFrank Li 
303494920128SFugang Duan 		txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
30357355f276STroy Kisky 					txq->bd.ring_size * TSO_HEADER_SIZE,
303659d0f746SFrank Li 					&txq->tso_hdrs_dma,
303759d0f746SFrank Li 					GFP_KERNEL);
303859d0f746SFrank Li 		if (!txq->tso_hdrs) {
303959d0f746SFrank Li 			ret = -ENOMEM;
304059d0f746SFrank Li 			goto alloc_failed;
304159d0f746SFrank Li 		}
304259d0f746SFrank Li 	}
304359d0f746SFrank Li 
304459d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
304559d0f746SFrank Li 		fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
304659d0f746SFrank Li 					   GFP_KERNEL);
304759d0f746SFrank Li 		if (!fep->rx_queue[i]) {
304859d0f746SFrank Li 			ret = -ENOMEM;
304959d0f746SFrank Li 			goto alloc_failed;
305059d0f746SFrank Li 		}
305159d0f746SFrank Li 
30527355f276STroy Kisky 		fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
30537355f276STroy Kisky 		fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
305459d0f746SFrank Li 	}
305559d0f746SFrank Li 	return ret;
305659d0f746SFrank Li 
305759d0f746SFrank Li alloc_failed:
305859d0f746SFrank Li 	fec_enet_free_queue(ndev);
305959d0f746SFrank Li 	return ret;
306059d0f746SFrank Li }
306159d0f746SFrank Li 
306259d0f746SFrank Li static int
306359d0f746SFrank Li fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
3064793fc096SFrank Li {
3065793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3066793fc096SFrank Li 	unsigned int i;
3067793fc096SFrank Li 	struct sk_buff *skb;
3068793fc096SFrank Li 	struct bufdesc	*bdp;
30694d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
3070793fc096SFrank Li 
307159d0f746SFrank Li 	rxq = fep->rx_queue[queue];
30727355f276STroy Kisky 	bdp = rxq->bd.base;
30737355f276STroy Kisky 	for (i = 0; i < rxq->bd.ring_size; i++) {
3074793fc096SFrank Li 		skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
3075ffdce2ccSRussell King 		if (!skb)
3076ffdce2ccSRussell King 			goto err_alloc;
3077793fc096SFrank Li 
30781b7bde6dSNimrod Andy 		if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
3079730ee360SRussell King 			dev_kfree_skb(skb);
3080ffdce2ccSRussell King 			goto err_alloc;
3081d842a31fSDuan Fugang-B38611 		}
3082730ee360SRussell King 
30834d494cdcSFugang Duan 		rxq->rx_skbuff[i] = skb;
30845cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
3085793fc096SFrank Li 
3086793fc096SFrank Li 		if (fep->bufdesc_ex) {
3087793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
30885cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
3089793fc096SFrank Li 		}
3090793fc096SFrank Li 
30917355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
3092793fc096SFrank Li 	}
3093793fc096SFrank Li 
3094793fc096SFrank Li 	/* Set the last buffer to wrap. */
30957355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
30965cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
309759d0f746SFrank Li 	return 0;
3098793fc096SFrank Li 
309959d0f746SFrank Li  err_alloc:
310059d0f746SFrank Li 	fec_enet_free_buffers(ndev);
310159d0f746SFrank Li 	return -ENOMEM;
310259d0f746SFrank Li }
310359d0f746SFrank Li 
310459d0f746SFrank Li static int
310559d0f746SFrank Li fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
310659d0f746SFrank Li {
310759d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
310859d0f746SFrank Li 	unsigned int i;
310959d0f746SFrank Li 	struct bufdesc  *bdp;
311059d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
311159d0f746SFrank Li 
311259d0f746SFrank Li 	txq = fep->tx_queue[queue];
31137355f276STroy Kisky 	bdp = txq->bd.base;
31147355f276STroy Kisky 	for (i = 0; i < txq->bd.ring_size; i++) {
31154d494cdcSFugang Duan 		txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
31164d494cdcSFugang Duan 		if (!txq->tx_bounce[i])
3117ffdce2ccSRussell King 			goto err_alloc;
3118793fc096SFrank Li 
31195cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(0);
31205cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
3121793fc096SFrank Li 
3122793fc096SFrank Li 		if (fep->bufdesc_ex) {
3123793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
31245cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
3125793fc096SFrank Li 		}
3126793fc096SFrank Li 
31277355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3128793fc096SFrank Li 	}
3129793fc096SFrank Li 
3130793fc096SFrank Li 	/* Set the last buffer to wrap. */
31317355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
31325cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3133793fc096SFrank Li 
3134793fc096SFrank Li 	return 0;
3135ffdce2ccSRussell King 
3136ffdce2ccSRussell King  err_alloc:
3137ffdce2ccSRussell King 	fec_enet_free_buffers(ndev);
3138ffdce2ccSRussell King 	return -ENOMEM;
3139793fc096SFrank Li }
3140793fc096SFrank Li 
314159d0f746SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev)
314259d0f746SFrank Li {
314359d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
314459d0f746SFrank Li 	unsigned int i;
314559d0f746SFrank Li 
314659d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
314759d0f746SFrank Li 		if (fec_enet_alloc_rxq_buffers(ndev, i))
314859d0f746SFrank Li 			return -ENOMEM;
314959d0f746SFrank Li 
315059d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
315159d0f746SFrank Li 		if (fec_enet_alloc_txq_buffers(ndev, i))
315259d0f746SFrank Li 			return -ENOMEM;
315359d0f746SFrank Li 	return 0;
315459d0f746SFrank Li }
315559d0f746SFrank Li 
3156793fc096SFrank Li static int
3157793fc096SFrank Li fec_enet_open(struct net_device *ndev)
3158793fc096SFrank Li {
3159793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3160793fc096SFrank Li 	int ret;
31611b0a83acSRichard Leitner 	bool reset_again;
3162793fc096SFrank Li 
3163da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(&fep->pdev->dev);
3164b0c6ce24SFabio Estevam 	if (ret < 0)
31658fff755eSAndrew Lunn 		return ret;
31668fff755eSAndrew Lunn 
31675bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&fep->pdev->dev);
3168e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
3169e8fcfcd5SNimrod Andy 	if (ret)
31708fff755eSAndrew Lunn 		goto clk_enable;
3171e8fcfcd5SNimrod Andy 
31721b0a83acSRichard Leitner 	/* During the first fec_enet_open call the PHY isn't probed at this
31731b0a83acSRichard Leitner 	 * point. Therefore the phy_reset_after_clk_enable() call within
31741b0a83acSRichard Leitner 	 * fec_enet_clk_enable() fails. As we need this reset in order to be
31751b0a83acSRichard Leitner 	 * sure the PHY is working correctly we check if we need to reset again
31761b0a83acSRichard Leitner 	 * later when the PHY is probed
31771b0a83acSRichard Leitner 	 */
31781b0a83acSRichard Leitner 	if (ndev->phydev && ndev->phydev->drv)
31791b0a83acSRichard Leitner 		reset_again = false;
31801b0a83acSRichard Leitner 	else
31811b0a83acSRichard Leitner 		reset_again = true;
31821b0a83acSRichard Leitner 
3183793fc096SFrank Li 	/* I should reset the ring buffers here, but I don't yet know
3184793fc096SFrank Li 	 * a simple way to do that.
3185793fc096SFrank Li 	 */
3186793fc096SFrank Li 
3187793fc096SFrank Li 	ret = fec_enet_alloc_buffers(ndev);
3188793fc096SFrank Li 	if (ret)
3189681d2421SFabio Estevam 		goto err_enet_alloc;
3190793fc096SFrank Li 
319155dd2753SNimrod Andy 	/* Init MAC prior to mii bus probe */
319255dd2753SNimrod Andy 	fec_restart(ndev);
319355dd2753SNimrod Andy 
31941b0a83acSRichard Leitner 	/* Call phy_reset_after_clk_enable() again if it failed during
31951b0a83acSRichard Leitner 	 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
31961b0a83acSRichard Leitner 	 */
31971b0a83acSRichard Leitner 	if (reset_again)
319864a632daSMarek Vasut 		fec_enet_phy_reset_after_clk_enable(ndev);
31991b0a83acSRichard Leitner 
32000da1ccbbSMarek Vasut 	/* Probe and connect to PHY when open the interface */
32010da1ccbbSMarek Vasut 	ret = fec_enet_mii_probe(ndev);
32020da1ccbbSMarek Vasut 	if (ret)
32030da1ccbbSMarek Vasut 		goto err_enet_mii_probe;
3204681d2421SFabio Estevam 
3205681d2421SFabio Estevam 	if (fep->quirks & FEC_QUIRK_ERR006687)
3206793fc096SFrank Li 		imx6q_cpuidle_fec_irqs_used();
3207793fc096SFrank Li 
3208793fc096SFrank Li 	napi_enable(&fep->napi);
3209793fc096SFrank Li 	phy_start(ndev->phydev);
3210793fc096SFrank Li 	netif_tx_start_all_queues(ndev);
3211793fc096SFrank Li 
3212793fc096SFrank Li 	device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
321345f5c327SPhilippe Reynes 				 FEC_WOL_FLAG_ENABLE);
3214d76cfae9SRussell King 
321531a6de34SRussell King 	return 0;
3216793fc096SFrank Li 
3217b49cd504SRussell King err_enet_mii_probe:
3218793fc096SFrank Li 	fec_enet_free_buffers(ndev);
321931a6de34SRussell King err_enet_alloc:
3220793fc096SFrank Li 	fec_enet_clk_enable(ndev, false);
322145f5c327SPhilippe Reynes clk_enable:
3222793fc096SFrank Li 	pm_runtime_mark_last_busy(&fep->pdev->dev);
322329380905SLucas Stach 	pm_runtime_put_autosuspend(&fep->pdev->dev);
322429380905SLucas Stach 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
322529380905SLucas Stach 	return ret;
322680cca775SNikita Yushchenko }
322780cca775SNikita Yushchenko 
3228e8fcfcd5SNimrod Andy static int
32295bbde4d2SNimrod Andy fec_enet_close(struct net_device *ndev)
32308fff755eSAndrew Lunn {
32318fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
32328fff755eSAndrew Lunn 
3233793fc096SFrank Li 	phy_stop(ndev->phydev);
3234793fc096SFrank Li 
3235793fc096SFrank Li 	if (netif_device_present(ndev)) {
3236793fc096SFrank Li 		napi_disable(&fep->napi);
3237793fc096SFrank Li 		netif_tx_disable(ndev);
3238793fc096SFrank Li 		fec_stop(ndev);
3239793fc096SFrank Li 	}
3240793fc096SFrank Li 
3241793fc096SFrank Li 	phy_disconnect(ndev->phydev);
3242793fc096SFrank Li 
3243793fc096SFrank Li 	if (fep->quirks & FEC_QUIRK_ERR006687)
3244793fc096SFrank Li 		imx6q_cpuidle_fec_irqs_unused();
3245793fc096SFrank Li 
3246793fc096SFrank Li 	fec_enet_update_ethtool_stats(ndev);
3247793fc096SFrank Li 
32486176e89cSJiri Kosina 	fec_enet_clk_enable(ndev, false);
3249793fc096SFrank Li 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3250793fc096SFrank Li 	pm_runtime_mark_last_busy(&fep->pdev->dev);
3251793fc096SFrank Li 	pm_runtime_put_autosuspend(&fep->pdev->dev);
3252793fc096SFrank Li 
3253793fc096SFrank Li 	fec_enet_free_buffers(ndev);
3254793fc096SFrank Li 
3255793fc096SFrank Li 	return 0;
3256793fc096SFrank Li }
325701f8902bSRui Sousa 
3258793fc096SFrank Li /* Set or clear the multicast filter for this adaptor.
3259793fc096SFrank Li  * Skeleton taken from sunlance driver.
3260793fc096SFrank Li  * The CPM Ethernet implementation allows Multicast as well as individual
3261793fc096SFrank Li  * MAC address filtering.  Some of the drivers check to make sure it is
3262793fc096SFrank Li  * a group multicast address, and discard those that are not.  I guess I
3263793fc096SFrank Li  * will do the same for now, but just remove the test if you want
3264793fc096SFrank Li  * individual filtering as well (do the upper net layers want or support
3265793fc096SFrank Li  * this kind of feature?).
3266793fc096SFrank Li  */
3267793fc096SFrank Li 
3268793fc096SFrank Li #define FEC_HASH_BITS	6		/* #bits in hash */
3269793fc096SFrank Li 
3270793fc096SFrank Li static void set_multicast_list(struct net_device *ndev)
3271793fc096SFrank Li {
3272793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3273793fc096SFrank Li 	struct netdev_hw_addr *ha;
327416f6e983SKrzysztof Kozlowski 	unsigned int crc, tmp;
3275793fc096SFrank Li 	unsigned char hash;
3276793fc096SFrank Li 	unsigned int hash_high = 0, hash_low = 0;
3277793fc096SFrank Li 
3278793fc096SFrank Li 	if (ndev->flags & IFF_PROMISC) {
3279793fc096SFrank Li 		tmp = readl(fep->hwp + FEC_R_CNTRL);
3280793fc096SFrank Li 		tmp |= 0x8;
3281793fc096SFrank Li 		writel(tmp, fep->hwp + FEC_R_CNTRL);
3282793fc096SFrank Li 		return;
3283793fc096SFrank Li 	}
3284793fc096SFrank Li 
3285793fc096SFrank Li 	tmp = readl(fep->hwp + FEC_R_CNTRL);
3286793fc096SFrank Li 	tmp &= ~0x8;
3287793fc096SFrank Li 	writel(tmp, fep->hwp + FEC_R_CNTRL);
3288793fc096SFrank Li 
3289793fc096SFrank Li 	if (ndev->flags & IFF_ALLMULTI) {
3290793fc096SFrank Li 		/* Catch all multicast addresses, so set the
3291793fc096SFrank Li 		 * filter to all 1's
3292793fc096SFrank Li 		 */
3293793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3294793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3295793fc096SFrank Li 
3296793fc096SFrank Li 		return;
3297793fc096SFrank Li 	}
3298793fc096SFrank Li 
329901f8902bSRui Sousa 	/* Add the addresses in hash register */
3300793fc096SFrank Li 	netdev_for_each_mc_addr(ha, ndev) {
3301793fc096SFrank Li 		/* calculate crc32 value of mac address */
330216f6e983SKrzysztof Kozlowski 		crc = ether_crc_le(ndev->addr_len, ha->addr);
3303793fc096SFrank Li 
33046176e89cSJiri Kosina 		/* only upper 6 bits (FEC_HASH_BITS) are used
3305981a0547SPeter Meerwald-Stadler 		 * which point to specific bit in the hash registers
3306793fc096SFrank Li 		 */
33076176e89cSJiri Kosina 		hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
3308793fc096SFrank Li 
330901f8902bSRui Sousa 		if (hash > 31)
331001f8902bSRui Sousa 			hash_high |= 1 << (hash - 32);
331101f8902bSRui Sousa 		else
331201f8902bSRui Sousa 			hash_low |= 1 << hash;
3313793fc096SFrank Li 	}
331401f8902bSRui Sousa 
331501f8902bSRui Sousa 	writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
331601f8902bSRui Sousa 	writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3317793fc096SFrank Li }
3318793fc096SFrank Li 
3319793fc096SFrank Li /* Set a MAC change in hardware. */
3320793fc096SFrank Li static int
3321793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p)
3322793fc096SFrank Li {
3323793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3324793fc096SFrank Li 	struct sockaddr *addr = p;
3325793fc096SFrank Li 
332644934facSLucas Stach 	if (addr) {
3327793fc096SFrank Li 		if (!is_valid_ether_addr(addr->sa_data))
3328793fc096SFrank Li 			return -EADDRNOTAVAIL;
3329*a05e4c0aSJakub Kicinski 		eth_hw_addr_set(ndev, addr->sa_data);
333044934facSLucas Stach 	}
3331793fc096SFrank Li 
33329638d19eSNimrod Andy 	/* Add netif status check here to avoid system hang in below case:
33339638d19eSNimrod Andy 	 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
33349638d19eSNimrod Andy 	 * After ethx down, fec all clocks are gated off and then register
33359638d19eSNimrod Andy 	 * access causes system hang.
33369638d19eSNimrod Andy 	 */
33379638d19eSNimrod Andy 	if (!netif_running(ndev))
33389638d19eSNimrod Andy 		return 0;
33399638d19eSNimrod Andy 
3340793fc096SFrank Li 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3341793fc096SFrank Li 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3342793fc096SFrank Li 		fep->hwp + FEC_ADDR_LOW);
3343793fc096SFrank Li 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3344793fc096SFrank Li 		fep->hwp + FEC_ADDR_HIGH);
3345793fc096SFrank Li 	return 0;
3346793fc096SFrank Li }
3347793fc096SFrank Li 
3348793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3349793fc096SFrank Li /**
3350793fc096SFrank Li  * fec_poll_controller - FEC Poll controller function
3351793fc096SFrank Li  * @dev: The FEC network adapter
3352793fc096SFrank Li  *
3353793fc096SFrank Li  * Polled functionality used by netconsole and others in non interrupt mode
3354793fc096SFrank Li  *
3355793fc096SFrank Li  */
3356793fc096SFrank Li static void fec_poll_controller(struct net_device *dev)
3357793fc096SFrank Li {
3358793fc096SFrank Li 	int i;
3359793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(dev);
3360793fc096SFrank Li 
3361793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
3362793fc096SFrank Li 		if (fep->irq[i] > 0) {
3363793fc096SFrank Li 			disable_irq(fep->irq[i]);
3364793fc096SFrank Li 			fec_enet_interrupt(fep->irq[i], dev);
3365793fc096SFrank Li 			enable_irq(fep->irq[i]);
3366793fc096SFrank Li 		}
3367793fc096SFrank Li 	}
3368793fc096SFrank Li }
3369793fc096SFrank Li #endif
3370793fc096SFrank Li 
33715bc26726SNimrod Andy static inline void fec_enet_set_netdev_features(struct net_device *netdev,
33724c09eed9SJim Baxter 	netdev_features_t features)
33734c09eed9SJim Baxter {
33744c09eed9SJim Baxter 	struct fec_enet_private *fep = netdev_priv(netdev);
33754c09eed9SJim Baxter 	netdev_features_t changed = features ^ netdev->features;
33764c09eed9SJim Baxter 
33774c09eed9SJim Baxter 	netdev->features = features;
33784c09eed9SJim Baxter 
33794c09eed9SJim Baxter 	/* Receive checksum has been changed */
33804c09eed9SJim Baxter 	if (changed & NETIF_F_RXCSUM) {
33814c09eed9SJim Baxter 		if (features & NETIF_F_RXCSUM)
33824c09eed9SJim Baxter 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
33834c09eed9SJim Baxter 		else
33844c09eed9SJim Baxter 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
33858506fa1dSRussell King 	}
33865bc26726SNimrod Andy }
33874c09eed9SJim Baxter 
33885bc26726SNimrod Andy static int fec_set_features(struct net_device *netdev,
33895bc26726SNimrod Andy 	netdev_features_t features)
33905bc26726SNimrod Andy {
33915bc26726SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
33925bc26726SNimrod Andy 	netdev_features_t changed = features ^ netdev->features;
33935bc26726SNimrod Andy 
33945b40f709SFabio Estevam 	if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
33955bc26726SNimrod Andy 		napi_disable(&fep->napi);
33965bc26726SNimrod Andy 		netif_tx_lock_bh(netdev);
33975bc26726SNimrod Andy 		fec_stop(netdev);
33985bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
3399ef83337dSRussell King 		fec_restart(netdev);
34004d494cdcSFugang Duan 		netif_tx_wake_all_queues(netdev);
34016af42d42SRussell King 		netif_tx_unlock_bh(netdev);
3402dbc64a8eSRussell King 		napi_enable(&fep->napi);
34035bc26726SNimrod Andy 	} else {
34045bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
34054c09eed9SJim Baxter 	}
34064c09eed9SJim Baxter 
34074c09eed9SJim Baxter 	return 0;
34084c09eed9SJim Baxter }
34094c09eed9SJim Baxter 
341052c4a1a8SFugang Duan static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb)
341152c4a1a8SFugang Duan {
341252c4a1a8SFugang Duan 	struct vlan_ethhdr *vhdr;
341352c4a1a8SFugang Duan 	unsigned short vlan_TCI = 0;
341452c4a1a8SFugang Duan 
341552c4a1a8SFugang Duan 	if (skb->protocol == htons(ETH_P_ALL)) {
341652c4a1a8SFugang Duan 		vhdr = (struct vlan_ethhdr *)(skb->data);
341752c4a1a8SFugang Duan 		vlan_TCI = ntohs(vhdr->h_vlan_TCI);
341852c4a1a8SFugang Duan 	}
341952c4a1a8SFugang Duan 
342052c4a1a8SFugang Duan 	return vlan_TCI;
342152c4a1a8SFugang Duan }
342252c4a1a8SFugang Duan 
342352c4a1a8SFugang Duan static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
342452c4a1a8SFugang Duan 				 struct net_device *sb_dev)
342552c4a1a8SFugang Duan {
342652c4a1a8SFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
342752c4a1a8SFugang Duan 	u16 vlan_tag;
342852c4a1a8SFugang Duan 
342952c4a1a8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
343052c4a1a8SFugang Duan 		return netdev_pick_tx(ndev, skb, NULL);
343152c4a1a8SFugang Duan 
343252c4a1a8SFugang Duan 	vlan_tag = fec_enet_get_raw_vlan_tci(skb);
343352c4a1a8SFugang Duan 	if (!vlan_tag)
343452c4a1a8SFugang Duan 		return vlan_tag;
343552c4a1a8SFugang Duan 
343652c4a1a8SFugang Duan 	return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
343752c4a1a8SFugang Duan }
343852c4a1a8SFugang Duan 
3439793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = {
3440793fc096SFrank Li 	.ndo_open		= fec_enet_open,
3441793fc096SFrank Li 	.ndo_stop		= fec_enet_close,
3442793fc096SFrank Li 	.ndo_start_xmit		= fec_enet_start_xmit,
344352c4a1a8SFugang Duan 	.ndo_select_queue       = fec_enet_select_queue,
3444793fc096SFrank Li 	.ndo_set_rx_mode	= set_multicast_list,
3445793fc096SFrank Li 	.ndo_validate_addr	= eth_validate_addr,
3446793fc096SFrank Li 	.ndo_tx_timeout		= fec_timeout,
3447793fc096SFrank Li 	.ndo_set_mac_address	= fec_set_mac_address,
3448a7605370SArnd Bergmann 	.ndo_eth_ioctl		= fec_enet_ioctl,
3449793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3450793fc096SFrank Li 	.ndo_poll_controller	= fec_poll_controller,
3451793fc096SFrank Li #endif
34524c09eed9SJim Baxter 	.ndo_set_features	= fec_set_features,
3453793fc096SFrank Li };
3454793fc096SFrank Li 
345553bb20d1STroy Kisky static const unsigned short offset_des_active_rxq[] = {
345653bb20d1STroy Kisky 	FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
345753bb20d1STroy Kisky };
345853bb20d1STroy Kisky 
345953bb20d1STroy Kisky static const unsigned short offset_des_active_txq[] = {
346053bb20d1STroy Kisky 	FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
346153bb20d1STroy Kisky };
346253bb20d1STroy Kisky 
3463793fc096SFrank Li  /*
3464793fc096SFrank Li   * XXX:  We need to clean up on failure exits here.
3465793fc096SFrank Li   *
3466793fc096SFrank Li   */
3467793fc096SFrank Li static int fec_enet_init(struct net_device *ndev)
3468793fc096SFrank Li {
3469793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3470793fc096SFrank Li 	struct bufdesc *cbd_base;
34714d494cdcSFugang Duan 	dma_addr_t bd_dma;
347255d0218aSNimrod Andy 	int bd_size;
347359d0f746SFrank Li 	unsigned int i;
34747355f276STroy Kisky 	unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
34757355f276STroy Kisky 			sizeof(struct bufdesc);
34767355f276STroy Kisky 	unsigned dsize_log2 = __fls(dsize);
3477453e9dc4SStefan Agner 	int ret;
347855d0218aSNimrod Andy 
34797355f276STroy Kisky 	WARN_ON(dsize != (1 << dsize_log2));
34803f1dcc6aSLucas Stach #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
348141ef84ceSFugang Duan 	fep->rx_align = 0xf;
348241ef84ceSFugang Duan 	fep->tx_align = 0xf;
348341ef84ceSFugang Duan #else
348441ef84ceSFugang Duan 	fep->rx_align = 0x3;
348541ef84ceSFugang Duan 	fep->tx_align = 0x3;
348641ef84ceSFugang Duan #endif
348741ef84ceSFugang Duan 
3488453e9dc4SStefan Agner 	/* Check mask of the streaming and coherent API */
3489453e9dc4SStefan Agner 	ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
3490453e9dc4SStefan Agner 	if (ret < 0) {
3491453e9dc4SStefan Agner 		dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
3492453e9dc4SStefan Agner 		return ret;
3493453e9dc4SStefan Agner 	}
3494453e9dc4SStefan Agner 
3495619fee9eSFugang Duan 	ret = fec_enet_alloc_queue(ndev);
3496619fee9eSFugang Duan 	if (ret)
3497619fee9eSFugang Duan 		return ret;
349879f33912SNimrod Andy 
34997355f276STroy Kisky 	bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3500793fc096SFrank Li 
3501793fc096SFrank Li 	/* Allocate memory for buffer descriptors. */
3502c0a1a0a6SLucas Stach 	cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3503793fc096SFrank Li 				       GFP_KERNEL);
35044d494cdcSFugang Duan 	if (!cbd_base) {
3505619fee9eSFugang Duan 		ret = -ENOMEM;
3506619fee9eSFugang Duan 		goto free_queue_mem;
35074d494cdcSFugang Duan 	}
3508793fc096SFrank Li 
3509793fc096SFrank Li 	/* Get the Ethernet address */
3510052fcc45SFugang Duan 	ret = fec_get_mac(ndev);
3511052fcc45SFugang Duan 	if (ret)
3512052fcc45SFugang Duan 		goto free_queue_mem;
3513052fcc45SFugang Duan 
351444934facSLucas Stach 	/* make sure MAC we just acquired is programmed into the hw */
351544934facSLucas Stach 	fec_set_mac_address(ndev, NULL);
3516793fc096SFrank Li 
3517793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
351859d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
35197355f276STroy Kisky 		struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
35207355f276STroy Kisky 		unsigned size = dsize * rxq->bd.ring_size;
35217355f276STroy Kisky 
35227355f276STroy Kisky 		rxq->bd.qid = i;
35237355f276STroy Kisky 		rxq->bd.base = cbd_base;
35247355f276STroy Kisky 		rxq->bd.cur = cbd_base;
35257355f276STroy Kisky 		rxq->bd.dma = bd_dma;
35267355f276STroy Kisky 		rxq->bd.dsize = dsize;
35277355f276STroy Kisky 		rxq->bd.dsize_log2 = dsize_log2;
352853bb20d1STroy Kisky 		rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
35297355f276STroy Kisky 		bd_dma += size;
35307355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
35317355f276STroy Kisky 		rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
353259d0f746SFrank Li 	}
353359d0f746SFrank Li 
353459d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
35357355f276STroy Kisky 		struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
35367355f276STroy Kisky 		unsigned size = dsize * txq->bd.ring_size;
35377355f276STroy Kisky 
35387355f276STroy Kisky 		txq->bd.qid = i;
35397355f276STroy Kisky 		txq->bd.base = cbd_base;
35407355f276STroy Kisky 		txq->bd.cur = cbd_base;
35417355f276STroy Kisky 		txq->bd.dma = bd_dma;
35427355f276STroy Kisky 		txq->bd.dsize = dsize;
35437355f276STroy Kisky 		txq->bd.dsize_log2 = dsize_log2;
354453bb20d1STroy Kisky 		txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
35457355f276STroy Kisky 		bd_dma += size;
35467355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
35477355f276STroy Kisky 		txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
354859d0f746SFrank Li 	}
35494d494cdcSFugang Duan 
3550793fc096SFrank Li 
3551793fc096SFrank Li 	/* The FEC Ethernet specific entries in the device structure */
3552793fc096SFrank Li 	ndev->watchdog_timeo = TX_TIMEOUT;
3553793fc096SFrank Li 	ndev->netdev_ops = &fec_netdev_ops;
3554793fc096SFrank Li 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
3555793fc096SFrank Li 
3556793fc096SFrank Li 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3557322555f5SFabio Estevam 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
3558793fc096SFrank Li 
35596b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3560cdffcf1bSJim Baxter 		/* enable hw VLAN support */
3561cdffcf1bSJim Baxter 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3562cdffcf1bSJim Baxter 
35636b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
356479f33912SNimrod Andy 		ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
356579f33912SNimrod Andy 
35664c09eed9SJim Baxter 		/* enable hw accelerator */
35674c09eed9SJim Baxter 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
356879f33912SNimrod Andy 				| NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
35694c09eed9SJim Baxter 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
357048496255SShawn Guo 	}
35714c09eed9SJim Baxter 
3572471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
357341ef84ceSFugang Duan 		fep->tx_align = 0;
357441ef84ceSFugang Duan 		fep->rx_align = 0x3f;
357541ef84ceSFugang Duan 	}
357641ef84ceSFugang Duan 
357709d1e541SNimrod Andy 	ndev->hw_features = ndev->features;
357809d1e541SNimrod Andy 
3579ef83337dSRussell King 	fec_restart(ndev);
3580793fc096SFrank Li 
35812b30842bSAndrew Lunn 	if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
35822b30842bSAndrew Lunn 		fec_enet_clear_ethtool_stats(ndev);
35832b30842bSAndrew Lunn 	else
358480cca775SNikita Yushchenko 		fec_enet_update_ethtool_stats(ndev);
358580cca775SNikita Yushchenko 
3586793fc096SFrank Li 	return 0;
3587619fee9eSFugang Duan 
3588619fee9eSFugang Duan free_queue_mem:
3589619fee9eSFugang Duan 	fec_enet_free_queue(ndev);
3590619fee9eSFugang Duan 	return ret;
3591793fc096SFrank Li }
3592793fc096SFrank Li 
3593793fc096SFrank Li #ifdef CONFIG_OF
35949269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev)
3595793fc096SFrank Li {
3596793fc096SFrank Li 	int err, phy_reset;
3597962d8cdcSBernhard Walle 	bool active_high = false;
3598159a0760SQuentin Schulz 	int msec = 1, phy_post_delay = 0;
3599793fc096SFrank Li 	struct device_node *np = pdev->dev.of_node;
3600793fc096SFrank Li 
3601793fc096SFrank Li 	if (!np)
36029269e556SFugang Duan 		return 0;
3603793fc096SFrank Li 
360461e04ccbSFugang Duan 	err = of_property_read_u32(np, "phy-reset-duration", &msec);
3605793fc096SFrank Li 	/* A sane reset duration should not be longer than 1s */
360661e04ccbSFugang Duan 	if (!err && msec > 1000)
3607793fc096SFrank Li 		msec = 1;
3608793fc096SFrank Li 
3609793fc096SFrank Li 	phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
36109269e556SFugang Duan 	if (phy_reset == -EPROBE_DEFER)
36119269e556SFugang Duan 		return phy_reset;
36129269e556SFugang Duan 	else if (!gpio_is_valid(phy_reset))
36139269e556SFugang Duan 		return 0;
3614793fc096SFrank Li 
3615159a0760SQuentin Schulz 	err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
3616159a0760SQuentin Schulz 	/* valid reset duration should be less than 1s */
3617159a0760SQuentin Schulz 	if (!err && phy_post_delay > 1000)
3618159a0760SQuentin Schulz 		return -EINVAL;
3619159a0760SQuentin Schulz 
3620962d8cdcSBernhard Walle 	active_high = of_property_read_bool(np, "phy-reset-active-high");
362164f10f6eSBernhard Walle 
3622793fc096SFrank Li 	err = devm_gpio_request_one(&pdev->dev, phy_reset,
3623962d8cdcSBernhard Walle 			active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
362464f10f6eSBernhard Walle 			"phy-reset");
3625793fc096SFrank Li 	if (err) {
3626793fc096SFrank Li 		dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
36279269e556SFugang Duan 		return err;
3628793fc096SFrank Li 	}
3629eb37c563SStefan Wahren 
3630eb37c563SStefan Wahren 	if (msec > 20)
3631793fc096SFrank Li 		msleep(msec);
3632eb37c563SStefan Wahren 	else
3633eb37c563SStefan Wahren 		usleep_range(msec * 1000, msec * 1000 + 1000);
3634eb37c563SStefan Wahren 
3635962d8cdcSBernhard Walle 	gpio_set_value_cansleep(phy_reset, !active_high);
36369269e556SFugang Duan 
3637159a0760SQuentin Schulz 	if (!phy_post_delay)
3638159a0760SQuentin Schulz 		return 0;
3639159a0760SQuentin Schulz 
3640159a0760SQuentin Schulz 	if (phy_post_delay > 20)
3641159a0760SQuentin Schulz 		msleep(phy_post_delay);
3642159a0760SQuentin Schulz 	else
3643159a0760SQuentin Schulz 		usleep_range(phy_post_delay * 1000,
3644159a0760SQuentin Schulz 			     phy_post_delay * 1000 + 1000);
3645159a0760SQuentin Schulz 
36469269e556SFugang Duan 	return 0;
3647793fc096SFrank Li }
3648793fc096SFrank Li #else /* CONFIG_OF */
36499269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev)
3650793fc096SFrank Li {
3651793fc096SFrank Li 	/*
3652793fc096SFrank Li 	 * In case of platform probe, the reset has been done
3653793fc096SFrank Li 	 * by machine code.
3654793fc096SFrank Li 	 */
36559269e556SFugang Duan 	return 0;
3656793fc096SFrank Li }
3657793fc096SFrank Li #endif /* CONFIG_OF */
3658793fc096SFrank Li 
36599fc095f1SFugang Duan static void
36609fc095f1SFugang Duan fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
36619fc095f1SFugang Duan {
36629fc095f1SFugang Duan 	struct device_node *np = pdev->dev.of_node;
36639fc095f1SFugang Duan 
36649fc095f1SFugang Duan 	*num_tx = *num_rx = 1;
36659fc095f1SFugang Duan 
36669fc095f1SFugang Duan 	if (!np || !of_device_is_available(np))
36679fc095f1SFugang Duan 		return;
36689fc095f1SFugang Duan 
36699fc095f1SFugang Duan 	/* parse the num of tx and rx queues */
367073b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3671b7bd75cfSFrank Li 
367273b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
36739fc095f1SFugang Duan 
36749fc095f1SFugang Duan 	if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3675b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
36769fc095f1SFugang Duan 			 *num_tx);
36779fc095f1SFugang Duan 		*num_tx = 1;
36789fc095f1SFugang Duan 		return;
36799fc095f1SFugang Duan 	}
36809fc095f1SFugang Duan 
36819fc095f1SFugang Duan 	if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3682b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
36839fc095f1SFugang Duan 			 *num_rx);
36849fc095f1SFugang Duan 		*num_rx = 1;
36859fc095f1SFugang Duan 		return;
36869fc095f1SFugang Duan 	}
36879fc095f1SFugang Duan 
36889fc095f1SFugang Duan }
36899fc095f1SFugang Duan 
36904ad1ceecSTroy Kisky static int fec_enet_get_irq_cnt(struct platform_device *pdev)
36914ad1ceecSTroy Kisky {
36924ad1ceecSTroy Kisky 	int irq_cnt = platform_irq_count(pdev);
36934ad1ceecSTroy Kisky 
36944ad1ceecSTroy Kisky 	if (irq_cnt > FEC_IRQ_NUM)
36954ad1ceecSTroy Kisky 		irq_cnt = FEC_IRQ_NUM;	/* last for pps */
36964ad1ceecSTroy Kisky 	else if (irq_cnt == 2)
36974ad1ceecSTroy Kisky 		irq_cnt = 1;	/* last for pps */
36984ad1ceecSTroy Kisky 	else if (irq_cnt <= 0)
36994ad1ceecSTroy Kisky 		irq_cnt = 1;	/* At least 1 irq is needed */
37004ad1ceecSTroy Kisky 	return irq_cnt;
37014ad1ceecSTroy Kisky }
37024ad1ceecSTroy Kisky 
3703b7cdc965SJoakim Zhang static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
3704b7cdc965SJoakim Zhang {
3705b7cdc965SJoakim Zhang 	struct net_device *ndev = platform_get_drvdata(pdev);
3706b7cdc965SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
3707b7cdc965SJoakim Zhang 
3708b7cdc965SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
3709b7cdc965SJoakim Zhang 		fep->wake_irq = fep->irq[2];
3710b7cdc965SJoakim Zhang 	else
3711b7cdc965SJoakim Zhang 		fep->wake_irq = fep->irq[0];
3712b7cdc965SJoakim Zhang }
3713b7cdc965SJoakim Zhang 
3714da722186SMartin Fuzzey static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
3715da722186SMartin Fuzzey 				   struct device_node *np)
3716da722186SMartin Fuzzey {
3717da722186SMartin Fuzzey 	struct device_node *gpr_np;
37188a448bf8SFugang Duan 	u32 out_val[3];
3719da722186SMartin Fuzzey 	int ret = 0;
3720da722186SMartin Fuzzey 
37218a448bf8SFugang Duan 	gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
3722da722186SMartin Fuzzey 	if (!gpr_np)
3723da722186SMartin Fuzzey 		return 0;
3724da722186SMartin Fuzzey 
37258a448bf8SFugang Duan 	ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
37268a448bf8SFugang Duan 					 ARRAY_SIZE(out_val));
37278a448bf8SFugang Duan 	if (ret) {
37288a448bf8SFugang Duan 		dev_dbg(&fep->pdev->dev, "no stop mode property\n");
37298a448bf8SFugang Duan 		return ret;
37308a448bf8SFugang Duan 	}
37318a448bf8SFugang Duan 
3732da722186SMartin Fuzzey 	fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
3733da722186SMartin Fuzzey 	if (IS_ERR(fep->stop_gpr.gpr)) {
3734da722186SMartin Fuzzey 		dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
3735da722186SMartin Fuzzey 		ret = PTR_ERR(fep->stop_gpr.gpr);
3736da722186SMartin Fuzzey 		fep->stop_gpr.gpr = NULL;
3737da722186SMartin Fuzzey 		goto out;
3738da722186SMartin Fuzzey 	}
3739da722186SMartin Fuzzey 
37408a448bf8SFugang Duan 	fep->stop_gpr.reg = out_val[1];
37418a448bf8SFugang Duan 	fep->stop_gpr.bit = out_val[2];
3742da722186SMartin Fuzzey 
3743da722186SMartin Fuzzey out:
3744da722186SMartin Fuzzey 	of_node_put(gpr_np);
3745da722186SMartin Fuzzey 
3746da722186SMartin Fuzzey 	return ret;
3747da722186SMartin Fuzzey }
3748da722186SMartin Fuzzey 
3749793fc096SFrank Li static int
3750793fc096SFrank Li fec_probe(struct platform_device *pdev)
3751793fc096SFrank Li {
3752793fc096SFrank Li 	struct fec_enet_private *fep;
3753793fc096SFrank Li 	struct fec_platform_data *pdata;
37540c65b2b9SAndrew Lunn 	phy_interface_t interface;
3755793fc096SFrank Li 	struct net_device *ndev;
3756793fc096SFrank Li 	int i, irq, ret = 0;
3757793fc096SFrank Li 	const struct of_device_id *of_id;
3758793fc096SFrank Li 	static int dev_id;
3759407066f8SUwe Kleine-König 	struct device_node *np = pdev->dev.of_node, *phy_node;
3760b7bd75cfSFrank Li 	int num_tx_qs;
3761b7bd75cfSFrank Li 	int num_rx_qs;
37624ad1ceecSTroy Kisky 	char irq_name[8];
37634ad1ceecSTroy Kisky 	int irq_cnt;
3764da722186SMartin Fuzzey 	struct fec_devinfo *dev_info;
3765793fc096SFrank Li 
37669fc095f1SFugang Duan 	fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
37679fc095f1SFugang Duan 
3768793fc096SFrank Li 	/* Init network device */
376980cca775SNikita Yushchenko 	ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
3770f85de666SNikita Yushchenko 				  FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
3771793fc096SFrank Li 	if (!ndev)
3772793fc096SFrank Li 		return -ENOMEM;
3773793fc096SFrank Li 
3774793fc096SFrank Li 	SET_NETDEV_DEV(ndev, &pdev->dev);
3775793fc096SFrank Li 
3776793fc096SFrank Li 	/* setup board info structure */
3777793fc096SFrank Li 	fep = netdev_priv(ndev);
3778793fc096SFrank Li 
37796b7e4008SLothar Waßmann 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
37806b7e4008SLothar Waßmann 	if (of_id)
37816b7e4008SLothar Waßmann 		pdev->id_entry = of_id->data;
3782da722186SMartin Fuzzey 	dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
3783da722186SMartin Fuzzey 	if (dev_info)
3784da722186SMartin Fuzzey 		fep->quirks = dev_info->quirks;
37856b7e4008SLothar Waßmann 
37860c818594SHubert Feurstein 	fep->netdev = ndev;
37879fc095f1SFugang Duan 	fep->num_rx_queues = num_rx_qs;
37889fc095f1SFugang Duan 	fep->num_tx_queues = num_tx_qs;
37899fc095f1SFugang Duan 
3790d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
3791793fc096SFrank Li 	/* default enable pause frame auto negotiation */
37926b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT)
3793793fc096SFrank Li 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3794d1391930SGuenter Roeck #endif
3795793fc096SFrank Li 
37965bbde4d2SNimrod Andy 	/* Select default pin state */
37975bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&pdev->dev);
37985bbde4d2SNimrod Andy 
37994f830a5aSYueHaibing 	fep->hwp = devm_platform_ioremap_resource(pdev, 0);
3800941e173aSTushar Behera 	if (IS_ERR(fep->hwp)) {
3801941e173aSTushar Behera 		ret = PTR_ERR(fep->hwp);
3802941e173aSTushar Behera 		goto failed_ioremap;
3803941e173aSTushar Behera 	}
3804941e173aSTushar Behera 
3805793fc096SFrank Li 	fep->pdev = pdev;
3806793fc096SFrank Li 	fep->dev_id = dev_id++;
3807793fc096SFrank Li 
3808793fc096SFrank Li 	platform_set_drvdata(pdev, ndev);
3809793fc096SFrank Li 
381029380905SLucas Stach 	if ((of_machine_is_compatible("fsl,imx6q") ||
381129380905SLucas Stach 	     of_machine_is_compatible("fsl,imx6dl")) &&
381229380905SLucas Stach 	    !of_property_read_bool(np, "fsl,err006687-workaround-present"))
381329380905SLucas Stach 		fep->quirks |= FEC_QUIRK_ERR006687;
381429380905SLucas Stach 
3815de40ed31SNimrod Andy 	if (of_get_property(np, "fsl,magic-packet", NULL))
3816de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3817de40ed31SNimrod Andy 
38188a448bf8SFugang Duan 	ret = fec_enet_init_stop_mode(fep, np);
3819da722186SMartin Fuzzey 	if (ret)
3820da722186SMartin Fuzzey 		goto failed_stop_mode;
3821da722186SMartin Fuzzey 
3822407066f8SUwe Kleine-König 	phy_node = of_parse_phandle(np, "phy-handle", 0);
3823407066f8SUwe Kleine-König 	if (!phy_node && of_phy_is_fixed_link(np)) {
3824407066f8SUwe Kleine-König 		ret = of_phy_register_fixed_link(np);
3825407066f8SUwe Kleine-König 		if (ret < 0) {
3826407066f8SUwe Kleine-König 			dev_err(&pdev->dev,
3827407066f8SUwe Kleine-König 				"broken fixed-link specification\n");
3828407066f8SUwe Kleine-König 			goto failed_phy;
3829407066f8SUwe Kleine-König 		}
3830407066f8SUwe Kleine-König 		phy_node = of_node_get(np);
3831407066f8SUwe Kleine-König 	}
3832407066f8SUwe Kleine-König 	fep->phy_node = phy_node;
3833407066f8SUwe Kleine-König 
38340c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(pdev->dev.of_node, &interface);
38350c65b2b9SAndrew Lunn 	if (ret) {
383694660ba0SJingoo Han 		pdata = dev_get_platdata(&pdev->dev);
3837793fc096SFrank Li 		if (pdata)
3838793fc096SFrank Li 			fep->phy_interface = pdata->phy;
3839793fc096SFrank Li 		else
3840793fc096SFrank Li 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
3841793fc096SFrank Li 	} else {
38420c65b2b9SAndrew Lunn 		fep->phy_interface = interface;
3843793fc096SFrank Li 	}
3844793fc096SFrank Li 
3845b820c114SJoakim Zhang 	ret = fec_enet_parse_rgmii_delay(fep, np);
3846b820c114SJoakim Zhang 	if (ret)
3847b820c114SJoakim Zhang 		goto failed_rgmii_delay;
3848b820c114SJoakim Zhang 
3849793fc096SFrank Li 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3850793fc096SFrank Li 	if (IS_ERR(fep->clk_ipg)) {
3851793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ipg);
3852793fc096SFrank Li 		goto failed_clk;
3853793fc096SFrank Li 	}
3854793fc096SFrank Li 
3855793fc096SFrank Li 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3856793fc096SFrank Li 	if (IS_ERR(fep->clk_ahb)) {
3857793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ahb);
3858793fc096SFrank Li 		goto failed_clk;
3859793fc096SFrank Li 	}
3860793fc096SFrank Li 
3861d851b47bSFugang Duan 	fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3862d851b47bSFugang Duan 
386338f56f33SLinus Torvalds 	/* enet_out is optional, depends on board */
386438f56f33SLinus Torvalds 	fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
386538f56f33SLinus Torvalds 	if (IS_ERR(fep->clk_enet_out))
386638f56f33SLinus Torvalds 		fep->clk_enet_out = NULL;
386738f56f33SLinus Torvalds 
386891c0d987SNimrod Andy 	fep->ptp_clk_on = false;
386991c0d987SNimrod Andy 	mutex_init(&fep->ptp_clk_mutex);
38709b5330edSFugang Duan 
38719b5330edSFugang Duan 	/* clk_ref is optional, depends on board */
38729b5330edSFugang Duan 	fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
38739b5330edSFugang Duan 	if (IS_ERR(fep->clk_ref))
38749b5330edSFugang Duan 		fep->clk_ref = NULL;
3875b82f8c3fSFugang Duan 	fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
38769b5330edSFugang Duan 
3877fc539459SFugang Duan 	/* clk_2x_txclk is optional, depends on board */
3878b820c114SJoakim Zhang 	if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
3879fc539459SFugang Duan 		fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
3880fc539459SFugang Duan 		if (IS_ERR(fep->clk_2x_txclk))
3881fc539459SFugang Duan 			fep->clk_2x_txclk = NULL;
3882b820c114SJoakim Zhang 	}
3883fc539459SFugang Duan 
38846b7e4008SLothar Waßmann 	fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
3885793fc096SFrank Li 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3886793fc096SFrank Li 	if (IS_ERR(fep->clk_ptp)) {
388738f56f33SLinus Torvalds 		fep->clk_ptp = NULL;
3888217b5844SLothar Waßmann 		fep->bufdesc_ex = false;
3889793fc096SFrank Li 	}
3890793fc096SFrank Li 
3891e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
389213a097bdSFabio Estevam 	if (ret)
389313a097bdSFabio Estevam 		goto failed_clk;
389413a097bdSFabio Estevam 
38958fff755eSAndrew Lunn 	ret = clk_prepare_enable(fep->clk_ipg);
38968fff755eSAndrew Lunn 	if (ret)
38978fff755eSAndrew Lunn 		goto failed_clk_ipg;
3898d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ahb);
3899d7c3a206SAndy Duan 	if (ret)
3900d7c3a206SAndy Duan 		goto failed_clk_ahb;
39018fff755eSAndrew Lunn 
390225974d8aSStefan Agner 	fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
3903f4e9f3d2SFabio Estevam 	if (!IS_ERR(fep->reg_phy)) {
3904f4e9f3d2SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
3905793fc096SFrank Li 		if (ret) {
3906793fc096SFrank Li 			dev_err(&pdev->dev,
3907793fc096SFrank Li 				"Failed to enable phy regulator: %d\n", ret);
3908793fc096SFrank Li 			goto failed_regulator;
3909793fc096SFrank Li 		}
3910f6a4d607SFabio Estevam 	} else {
39113f38c683SFugang Duan 		if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
39123f38c683SFugang Duan 			ret = -EPROBE_DEFER;
39133f38c683SFugang Duan 			goto failed_regulator;
39143f38c683SFugang Duan 		}
3915f6a4d607SFabio Estevam 		fep->reg_phy = NULL;
3916793fc096SFrank Li 	}
3917793fc096SFrank Li 
39188fff755eSAndrew Lunn 	pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
39198fff755eSAndrew Lunn 	pm_runtime_use_autosuspend(&pdev->dev);
392014d2b7c1SLucas Stach 	pm_runtime_get_noresume(&pdev->dev);
39218fff755eSAndrew Lunn 	pm_runtime_set_active(&pdev->dev);
39228fff755eSAndrew Lunn 	pm_runtime_enable(&pdev->dev);
39238fff755eSAndrew Lunn 
39249269e556SFugang Duan 	ret = fec_reset_phy(pdev);
39259269e556SFugang Duan 	if (ret)
39269269e556SFugang Duan 		goto failed_reset;
3927793fc096SFrank Li 
39284ad1ceecSTroy Kisky 	irq_cnt = fec_enet_get_irq_cnt(pdev);
3929793fc096SFrank Li 	if (fep->bufdesc_ex)
39304ad1ceecSTroy Kisky 		fec_ptp_init(pdev, irq_cnt);
3931793fc096SFrank Li 
3932793fc096SFrank Li 	ret = fec_enet_init(ndev);
3933793fc096SFrank Li 	if (ret)
3934793fc096SFrank Li 		goto failed_init;
3935793fc096SFrank Li 
39364ad1ceecSTroy Kisky 	for (i = 0; i < irq_cnt; i++) {
39373ded9f2bSArnd Bergmann 		snprintf(irq_name, sizeof(irq_name), "int%d", i);
39383b56be21SAnson Huang 		irq = platform_get_irq_byname_optional(pdev, irq_name);
39394ad1ceecSTroy Kisky 		if (irq < 0)
3940793fc096SFrank Li 			irq = platform_get_irq(pdev, i);
3941793fc096SFrank Li 		if (irq < 0) {
3942793fc096SFrank Li 			ret = irq;
3943793fc096SFrank Li 			goto failed_irq;
3944793fc096SFrank Li 		}
39450d9b2ab1SFabio Estevam 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
394644a272ddSMichael Opdenacker 				       0, pdev->name, ndev);
39470d9b2ab1SFabio Estevam 		if (ret)
3948793fc096SFrank Li 			goto failed_irq;
3949de40ed31SNimrod Andy 
3950de40ed31SNimrod Andy 		fep->irq[i] = irq;
3951793fc096SFrank Li 	}
3952793fc096SFrank Li 
3953b7cdc965SJoakim Zhang 	/* Decide which interrupt line is wakeup capable */
3954b7cdc965SJoakim Zhang 	fec_enet_get_wakeup_irq(pdev);
3955b7cdc965SJoakim Zhang 
3956793fc096SFrank Li 	ret = fec_enet_mii_init(pdev);
3957793fc096SFrank Li 	if (ret)
3958793fc096SFrank Li 		goto failed_mii_init;
3959793fc096SFrank Li 
3960793fc096SFrank Li 	/* Carrier starts down, phylib will bring it up */
3961793fc096SFrank Li 	netif_carrier_off(ndev);
3962e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
39635bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&pdev->dev);
3964793fc096SFrank Li 
396559193053SAndrew Lunn 	ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN;
396659193053SAndrew Lunn 
3967793fc096SFrank Li 	ret = register_netdev(ndev);
3968793fc096SFrank Li 	if (ret)
3969793fc096SFrank Li 		goto failed_register;
3970793fc096SFrank Li 
3971de40ed31SNimrod Andy 	device_init_wakeup(&ndev->dev, fep->wol_flag &
3972de40ed31SNimrod Andy 			   FEC_WOL_HAS_MAGIC_PACKET);
3973de40ed31SNimrod Andy 
3974eb1d0640SFabio Estevam 	if (fep->bufdesc_ex && fep->ptp_clock)
3975eb1d0640SFabio Estevam 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3976eb1d0640SFabio Estevam 
39771b7bde6dSNimrod Andy 	fep->rx_copybreak = COPYBREAK_DEFAULT;
397836cdc743SRussell King 	INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
39798fff755eSAndrew Lunn 
39808fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&pdev->dev);
39818fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&pdev->dev);
39828fff755eSAndrew Lunn 
3983793fc096SFrank Li 	return 0;
3984793fc096SFrank Li 
3985793fc096SFrank Li failed_register:
3986793fc096SFrank Li 	fec_enet_mii_remove(fep);
3987793fc096SFrank Li failed_mii_init:
39887a2bbd8dSFabio Estevam failed_irq:
39897a2bbd8dSFabio Estevam failed_init:
399032cba57bSLucas Stach 	fec_ptp_stop(pdev);
39919269e556SFugang Duan failed_reset:
3992ce8d24f9SAndy Duan 	pm_runtime_put_noidle(&pdev->dev);
39939269e556SFugang Duan 	pm_runtime_disable(&pdev->dev);
3994c6165cf0SFugang Duan 	if (fep->reg_phy)
3995c6165cf0SFugang Duan 		regulator_disable(fep->reg_phy);
3996793fc096SFrank Li failed_regulator:
3997d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
3998d7c3a206SAndy Duan failed_clk_ahb:
3999d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ipg);
40008fff755eSAndrew Lunn failed_clk_ipg:
4001e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
4002793fc096SFrank Li failed_clk:
4003b820c114SJoakim Zhang failed_rgmii_delay:
400482005b1cSJohan Hovold 	if (of_phy_is_fixed_link(np))
400582005b1cSJohan Hovold 		of_phy_deregister_fixed_link(np);
4006407066f8SUwe Kleine-König 	of_node_put(phy_node);
4007da722186SMartin Fuzzey failed_stop_mode:
4008d1616f07SFugang Duan failed_phy:
4009d1616f07SFugang Duan 	dev_id--;
4010793fc096SFrank Li failed_ioremap:
4011793fc096SFrank Li 	free_netdev(ndev);
4012793fc096SFrank Li 
4013793fc096SFrank Li 	return ret;
4014793fc096SFrank Li }
4015793fc096SFrank Li 
4016793fc096SFrank Li static int
4017793fc096SFrank Li fec_drv_remove(struct platform_device *pdev)
4018793fc096SFrank Li {
4019793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
4020793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
402182005b1cSJohan Hovold 	struct device_node *np = pdev->dev.of_node;
4022a31eda65SChuhong Yuan 	int ret;
4023a31eda65SChuhong Yuan 
4024da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(&pdev->dev);
4025a31eda65SChuhong Yuan 	if (ret < 0)
4026a31eda65SChuhong Yuan 		return ret;
4027793fc096SFrank Li 
402836cdc743SRussell King 	cancel_work_sync(&fep->tx_timeout_work);
402932cba57bSLucas Stach 	fec_ptp_stop(pdev);
4030793fc096SFrank Li 	unregister_netdev(ndev);
4031793fc096SFrank Li 	fec_enet_mii_remove(fep);
4032f6a4d607SFabio Estevam 	if (fep->reg_phy)
4033f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
4034a31eda65SChuhong Yuan 
403582005b1cSJohan Hovold 	if (of_phy_is_fixed_link(np))
403682005b1cSJohan Hovold 		of_phy_deregister_fixed_link(np);
4037407066f8SUwe Kleine-König 	of_node_put(fep->phy_node);
4038793fc096SFrank Li 
4039a31eda65SChuhong Yuan 	clk_disable_unprepare(fep->clk_ahb);
4040a31eda65SChuhong Yuan 	clk_disable_unprepare(fep->clk_ipg);
4041a31eda65SChuhong Yuan 	pm_runtime_put_noidle(&pdev->dev);
4042a31eda65SChuhong Yuan 	pm_runtime_disable(&pdev->dev);
4043a31eda65SChuhong Yuan 
404444712965SPavel Skripkin 	free_netdev(ndev);
4045793fc096SFrank Li 	return 0;
4046793fc096SFrank Li }
4047793fc096SFrank Li 
4048dd66d386SFabio Estevam static int __maybe_unused fec_suspend(struct device *dev)
4049793fc096SFrank Li {
4050793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
4051793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4052793fc096SFrank Li 
4053da1774e5SRussell King 	rtnl_lock();
4054793fc096SFrank Li 	if (netif_running(ndev)) {
4055de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
4056de40ed31SNimrod Andy 			fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
405745f5c327SPhilippe Reynes 		phy_stop(ndev->phydev);
405831a6de34SRussell King 		napi_disable(&fep->napi);
405931a6de34SRussell King 		netif_tx_lock_bh(ndev);
4060793fc096SFrank Li 		netif_device_detach(ndev);
406131a6de34SRussell King 		netif_tx_unlock_bh(ndev);
406231a6de34SRussell King 		fec_stop(ndev);
4063e8fcfcd5SNimrod Andy 		fec_enet_clk_enable(ndev, false);
4064de40ed31SNimrod Andy 		if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
40655bbde4d2SNimrod Andy 			pinctrl_pm_select_sleep_state(&fep->pdev->dev);
4066f4c4a4e0SNimrod Andy 	}
4067f4c4a4e0SNimrod Andy 	rtnl_unlock();
4068793fc096SFrank Li 
4069de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
4070238f7bc7SFabio Estevam 		regulator_disable(fep->reg_phy);
4071238f7bc7SFabio Estevam 
4072858eeb7dSNimrod Andy 	/* SOC supply clock to phy, when clock is disabled, phy link down
4073858eeb7dSNimrod Andy 	 * SOC control phy regulator, when regulator is disabled, phy link down
4074858eeb7dSNimrod Andy 	 */
4075858eeb7dSNimrod Andy 	if (fep->clk_enet_out || fep->reg_phy)
4076858eeb7dSNimrod Andy 		fep->link = 0;
4077858eeb7dSNimrod Andy 
4078793fc096SFrank Li 	return 0;
4079793fc096SFrank Li }
4080793fc096SFrank Li 
4081dd66d386SFabio Estevam static int __maybe_unused fec_resume(struct device *dev)
4082793fc096SFrank Li {
4083793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
4084793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4085238f7bc7SFabio Estevam 	int ret;
4086de40ed31SNimrod Andy 	int val;
4087238f7bc7SFabio Estevam 
4088de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4089238f7bc7SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
4090238f7bc7SFabio Estevam 		if (ret)
4091238f7bc7SFabio Estevam 			return ret;
4092238f7bc7SFabio Estevam 	}
4093793fc096SFrank Li 
4094da1774e5SRussell King 	rtnl_lock();
4095793fc096SFrank Li 	if (netif_running(ndev)) {
4096f4c4a4e0SNimrod Andy 		ret = fec_enet_clk_enable(ndev, true);
4097f4c4a4e0SNimrod Andy 		if (ret) {
4098f4c4a4e0SNimrod Andy 			rtnl_unlock();
4099f4c4a4e0SNimrod Andy 			goto failed_clk;
4100f4c4a4e0SNimrod Andy 		}
4101de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
4102da722186SMartin Fuzzey 			fec_enet_stop_mode(fep, false);
4103da722186SMartin Fuzzey 
4104de40ed31SNimrod Andy 			val = readl(fep->hwp + FEC_ECNTRL);
4105de40ed31SNimrod Andy 			val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
4106de40ed31SNimrod Andy 			writel(val, fep->hwp + FEC_ECNTRL);
4107de40ed31SNimrod Andy 			fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
4108de40ed31SNimrod Andy 		} else {
4109de40ed31SNimrod Andy 			pinctrl_pm_select_default_state(&fep->pdev->dev);
4110de40ed31SNimrod Andy 		}
4111ef83337dSRussell King 		fec_restart(ndev);
411231a6de34SRussell King 		netif_tx_lock_bh(ndev);
4113793fc096SFrank Li 		netif_device_attach(ndev);
41146af42d42SRussell King 		netif_tx_unlock_bh(ndev);
41156af42d42SRussell King 		napi_enable(&fep->napi);
4116557d5dc8SHeiner Kallweit 		phy_init_hw(ndev->phydev);
411745f5c327SPhilippe Reynes 		phy_start(ndev->phydev);
4118793fc096SFrank Li 	}
4119da1774e5SRussell King 	rtnl_unlock();
4120793fc096SFrank Li 
4121793fc096SFrank Li 	return 0;
412213a097bdSFabio Estevam 
4123e8fcfcd5SNimrod Andy failed_clk:
412413a097bdSFabio Estevam 	if (fep->reg_phy)
412513a097bdSFabio Estevam 		regulator_disable(fep->reg_phy);
412613a097bdSFabio Estevam 	return ret;
4127793fc096SFrank Li }
4128793fc096SFrank Li 
41298fff755eSAndrew Lunn static int __maybe_unused fec_runtime_suspend(struct device *dev)
41308fff755eSAndrew Lunn {
41318fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
41328fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
41338fff755eSAndrew Lunn 
4134d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
41358fff755eSAndrew Lunn 	clk_disable_unprepare(fep->clk_ipg);
41368fff755eSAndrew Lunn 
41378fff755eSAndrew Lunn 	return 0;
41388fff755eSAndrew Lunn }
41398fff755eSAndrew Lunn 
41408fff755eSAndrew Lunn static int __maybe_unused fec_runtime_resume(struct device *dev)
41418fff755eSAndrew Lunn {
41428fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
41438fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
4144d7c3a206SAndy Duan 	int ret;
41458fff755eSAndrew Lunn 
4146d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ahb);
4147d7c3a206SAndy Duan 	if (ret)
4148d7c3a206SAndy Duan 		return ret;
4149d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ipg);
4150d7c3a206SAndy Duan 	if (ret)
4151d7c3a206SAndy Duan 		goto failed_clk_ipg;
4152d7c3a206SAndy Duan 
4153d7c3a206SAndy Duan 	return 0;
4154d7c3a206SAndy Duan 
4155d7c3a206SAndy Duan failed_clk_ipg:
4156d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
4157d7c3a206SAndy Duan 	return ret;
41588fff755eSAndrew Lunn }
41598fff755eSAndrew Lunn 
41608fff755eSAndrew Lunn static const struct dev_pm_ops fec_pm_ops = {
41618fff755eSAndrew Lunn 	SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
41628fff755eSAndrew Lunn 	SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
41638fff755eSAndrew Lunn };
4164793fc096SFrank Li 
4165793fc096SFrank Li static struct platform_driver fec_driver = {
4166793fc096SFrank Li 	.driver	= {
4167793fc096SFrank Li 		.name	= DRIVER_NAME,
4168793fc096SFrank Li 		.pm	= &fec_pm_ops,
4169793fc096SFrank Li 		.of_match_table = fec_dt_ids,
4170272bb0e9SFabio Estevam 		.suppress_bind_attrs = true,
4171793fc096SFrank Li 	},
4172793fc096SFrank Li 	.id_table = fec_devtype,
4173793fc096SFrank Li 	.probe	= fec_probe,
4174793fc096SFrank Li 	.remove	= fec_drv_remove,
4175793fc096SFrank Li };
4176793fc096SFrank Li 
4177793fc096SFrank Li module_platform_driver(fec_driver);
4178793fc096SFrank Li 
4179793fc096SFrank Li MODULE_LICENSE("GPL");
4180