xref: /linux/drivers/net/ethernet/freescale/fec_main.c (revision 857922b16bb893d26d5ecd83acf9f20cb28eaea2)
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>
59407066f8SUwe Kleine-König #include <linux/of_mdio.h>
60793fc096SFrank Li #include <linux/of_net.h>
61793fc096SFrank Li #include <linux/regulator/consumer.h>
62cdffcf1bSJim Baxter #include <linux/if_vlan.h>
63a68ab98eSFabio Estevam #include <linux/pinctrl/consumer.h>
64468ba54bSArnd Bergmann #include <linux/gpio/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>
6995698ff6SShenwei Wang #include <linux/filter.h>
7095698ff6SShenwei Wang #include <linux/bpf.h>
71793fc096SFrank Li 
72793fc096SFrank Li #include <asm/cacheflush.h>
73793fc096SFrank Li 
74793fc096SFrank Li #include "fec.h"
75793fc096SFrank Li 
76772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev);
77df727d45SRasmus Villemoes static void fec_enet_itr_coal_set(struct net_device *ndev);
78772e42b0SChristoph Müllner 
79793fc096SFrank Li #define DRIVER_NAME	"fec"
80793fc096SFrank Li 
8152c4a1a8SFugang Duan static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2};
8252c4a1a8SFugang Duan 
83793fc096SFrank Li /* Pause frame feild and FIFO threshold */
84793fc096SFrank Li #define FEC_ENET_FCE	(1 << 5)
85793fc096SFrank Li #define FEC_ENET_RSEM_V	0x84
86793fc096SFrank Li #define FEC_ENET_RSFL_V	16
87793fc096SFrank Li #define FEC_ENET_RAEM_V	0x8
88793fc096SFrank Li #define FEC_ENET_RAFL_V	0x8
89793fc096SFrank Li #define FEC_ENET_OPD_V	0xFFF0
908fff755eSAndrew Lunn #define FEC_MDIO_PM_TIMEOUT  100 /* ms */
91793fc096SFrank Li 
926d6b39f1SShenwei Wang #define FEC_ENET_XDP_PASS          0
936d6b39f1SShenwei Wang #define FEC_ENET_XDP_CONSUMED      BIT(0)
946d6b39f1SShenwei Wang #define FEC_ENET_XDP_TX            BIT(1)
956d6b39f1SShenwei Wang #define FEC_ENET_XDP_REDIR         BIT(2)
966d6b39f1SShenwei Wang 
97da722186SMartin Fuzzey struct fec_devinfo {
98da722186SMartin Fuzzey 	u32 quirks;
99da722186SMartin Fuzzey };
100da722186SMartin Fuzzey 
101da722186SMartin Fuzzey static const struct fec_devinfo fec_imx25_info = {
102da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
103abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_FRREG | FEC_QUIRK_HAS_MDIO_C45,
104da722186SMartin Fuzzey };
105da722186SMartin Fuzzey 
106da722186SMartin Fuzzey static const struct fec_devinfo fec_imx27_info = {
107abc33494SGreg Ungerer 	.quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG |
108abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
109da722186SMartin Fuzzey };
110da722186SMartin Fuzzey 
111da722186SMartin Fuzzey static const struct fec_devinfo fec_imx28_info = {
112da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
113da722186SMartin Fuzzey 		  FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
114c730ab42SLaurent Badel 		  FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII |
115abc33494SGreg Ungerer 		  FEC_QUIRK_NO_HARD_RESET | FEC_QUIRK_HAS_MDIO_C45,
116da722186SMartin Fuzzey };
117da722186SMartin Fuzzey 
118da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6q_info = {
119da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
120da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
121da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
1227d650df9SWei Fang 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII |
123abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_PMQOS | FEC_QUIRK_HAS_MDIO_C45,
124da722186SMartin Fuzzey };
125da722186SMartin Fuzzey 
126da722186SMartin Fuzzey static const struct fec_devinfo fec_mvf600_info = {
127abc33494SGreg Ungerer 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC |
128abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
129da722186SMartin Fuzzey };
130da722186SMartin Fuzzey 
131da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6x_info = {
132da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
133da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
134da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
135da722186SMartin Fuzzey 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
1361e6114f5SGreg Ungerer 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
137abc33494SGreg Ungerer 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
138abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
139da722186SMartin Fuzzey };
140da722186SMartin Fuzzey 
141da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6ul_info = {
142da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
143da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
144da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
145da722186SMartin Fuzzey 		  FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
146abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII |
147abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
148da722186SMartin Fuzzey };
149da722186SMartin Fuzzey 
150947240ebSFugang Duan static const struct fec_devinfo fec_imx8mq_info = {
151947240ebSFugang Duan 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
152947240ebSFugang Duan 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
153947240ebSFugang Duan 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
154947240ebSFugang Duan 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
155947240ebSFugang Duan 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
156947240ebSFugang Duan 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
157abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2 |
158abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
159947240ebSFugang Duan };
160947240ebSFugang Duan 
161947240ebSFugang Duan static const struct fec_devinfo fec_imx8qm_info = {
162947240ebSFugang Duan 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
163947240ebSFugang Duan 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
164947240ebSFugang Duan 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
165947240ebSFugang Duan 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
166947240ebSFugang Duan 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
167947240ebSFugang Duan 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
168abc33494SGreg Ungerer 		  FEC_QUIRK_DELAYED_CLKS_SUPPORT | FEC_QUIRK_HAS_MDIO_C45,
169947240ebSFugang Duan };
170947240ebSFugang Duan 
171167d5fe0SWei Fang static const struct fec_devinfo fec_s32v234_info = {
172167d5fe0SWei Fang 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
173167d5fe0SWei Fang 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
174167d5fe0SWei Fang 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
175abc33494SGreg Ungerer 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
176abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
177167d5fe0SWei Fang };
178167d5fe0SWei Fang 
179793fc096SFrank Li static struct platform_device_id fec_devtype[] = {
180793fc096SFrank Li 	{
181793fc096SFrank Li 		/* keep it for coldfire */
182793fc096SFrank Li 		.name = DRIVER_NAME,
183793fc096SFrank Li 		.driver_data = 0,
184793fc096SFrank Li 	}, {
185793fc096SFrank Li 		.name = "imx25-fec",
186da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx25_info,
187793fc096SFrank Li 	}, {
188793fc096SFrank Li 		.name = "imx27-fec",
189da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx27_info,
190793fc096SFrank Li 	}, {
191793fc096SFrank Li 		.name = "imx28-fec",
192da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx28_info,
193793fc096SFrank Li 	}, {
194793fc096SFrank Li 		.name = "imx6q-fec",
195da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6q_info,
196793fc096SFrank Li 	}, {
19736803542SShawn Guo 		.name = "mvf600-fec",
198da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_mvf600_info,
199ca7c4a45SJingchang Lu 	}, {
20095a77470SFugang Duan 		.name = "imx6sx-fec",
201da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6x_info,
20295a77470SFugang Duan 	}, {
203a51d3ab5SFugang Duan 		.name = "imx6ul-fec",
204da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6ul_info,
205a51d3ab5SFugang Duan 	}, {
206947240ebSFugang Duan 		.name = "imx8mq-fec",
207947240ebSFugang Duan 		.driver_data = (kernel_ulong_t)&fec_imx8mq_info,
208947240ebSFugang Duan 	}, {
209947240ebSFugang Duan 		.name = "imx8qm-fec",
210947240ebSFugang Duan 		.driver_data = (kernel_ulong_t)&fec_imx8qm_info,
211947240ebSFugang Duan 	}, {
212167d5fe0SWei Fang 		.name = "s32v234-fec",
213167d5fe0SWei Fang 		.driver_data = (kernel_ulong_t)&fec_s32v234_info,
214167d5fe0SWei Fang 	}, {
215793fc096SFrank Li 		/* sentinel */
216793fc096SFrank Li 	}
217793fc096SFrank Li };
218793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype);
219793fc096SFrank Li 
220793fc096SFrank Li enum imx_fec_type {
221793fc096SFrank Li 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
222793fc096SFrank Li 	IMX27_FEC,	/* runs on i.mx27/35/51 */
223793fc096SFrank Li 	IMX28_FEC,
224793fc096SFrank Li 	IMX6Q_FEC,
22536803542SShawn Guo 	MVF600_FEC,
226ba593e00SFugang Duan 	IMX6SX_FEC,
227a51d3ab5SFugang Duan 	IMX6UL_FEC,
228947240ebSFugang Duan 	IMX8MQ_FEC,
229947240ebSFugang Duan 	IMX8QM_FEC,
230167d5fe0SWei Fang 	S32V234_FEC,
231793fc096SFrank Li };
232793fc096SFrank Li 
233793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = {
234793fc096SFrank Li 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
235793fc096SFrank Li 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
236793fc096SFrank Li 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
237793fc096SFrank Li 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
23836803542SShawn Guo 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
239ba593e00SFugang Duan 	{ .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
240a51d3ab5SFugang Duan 	{ .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
241947240ebSFugang Duan 	{ .compatible = "fsl,imx8mq-fec", .data = &fec_devtype[IMX8MQ_FEC], },
242947240ebSFugang Duan 	{ .compatible = "fsl,imx8qm-fec", .data = &fec_devtype[IMX8QM_FEC], },
243167d5fe0SWei Fang 	{ .compatible = "fsl,s32v234-fec", .data = &fec_devtype[S32V234_FEC], },
244793fc096SFrank Li 	{ /* sentinel */ }
245793fc096SFrank Li };
246793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids);
247793fc096SFrank Li 
248793fc096SFrank Li static unsigned char macaddr[ETH_ALEN];
249793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0);
250793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
251793fc096SFrank Li 
252793fc096SFrank Li #if defined(CONFIG_M5272)
253793fc096SFrank Li /*
254793fc096SFrank Li  * Some hardware gets it MAC address out of local flash memory.
255793fc096SFrank Li  * if this is non-zero then assume it is the address to get MAC from.
256793fc096SFrank Li  */
257793fc096SFrank Li #if defined(CONFIG_NETtel)
258793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006006
259793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
260793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006000
261793fc096SFrank Li #elif defined(CONFIG_CANCam)
262793fc096SFrank Li #define	FEC_FLASHMAC	0xf0020000
263793fc096SFrank Li #elif defined (CONFIG_M5272C3)
264793fc096SFrank Li #define	FEC_FLASHMAC	(0xffe04000 + 4)
265793fc096SFrank Li #elif defined(CONFIG_MOD5272)
266793fc096SFrank Li #define FEC_FLASHMAC	0xffc0406b
267793fc096SFrank Li #else
268793fc096SFrank Li #define	FEC_FLASHMAC	0
269793fc096SFrank Li #endif
270793fc096SFrank Li #endif /* CONFIG_M5272 */
271793fc096SFrank Li 
272cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
273fbbeefddSAndrew Lunn  *
274fbbeefddSAndrew Lunn  * 2048 byte skbufs are allocated. However, alignment requirements
275fbbeefddSAndrew Lunn  * varies between FEC variants. Worst case is 64, so round down by 64.
276793fc096SFrank Li  */
277fbbeefddSAndrew Lunn #define PKT_MAXBUF_SIZE		(round_down(2048 - 64, 64))
278793fc096SFrank Li #define PKT_MINBUF_SIZE		64
279793fc096SFrank Li 
2804c09eed9SJim Baxter /* FEC receive acceleration */
2814c09eed9SJim Baxter #define FEC_RACC_IPDIS		(1 << 1)
2824c09eed9SJim Baxter #define FEC_RACC_PRODIS		(1 << 2)
2833ac72b7bSEric Nelson #define FEC_RACC_SHIFT16	BIT(7)
2844c09eed9SJim Baxter #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
2854c09eed9SJim Baxter 
2862b30842bSAndrew Lunn /* MIB Control Register */
2872b30842bSAndrew Lunn #define FEC_MIB_CTRLSTAT_DISABLE	BIT(31)
2882b30842bSAndrew Lunn 
289793fc096SFrank Li /*
290793fc096SFrank Li  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
291793fc096SFrank Li  * size bits. Other FEC hardware does not, so we need to take that into
292793fc096SFrank Li  * account when setting it.
293793fc096SFrank Li  */
294793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2953f1dcc6aSLucas Stach     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
2963f1dcc6aSLucas Stach     defined(CONFIG_ARM64)
297793fc096SFrank Li #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
298793fc096SFrank Li #else
299793fc096SFrank Li #define	OPT_FRAME_SIZE	0
300793fc096SFrank Li #endif
301793fc096SFrank Li 
302793fc096SFrank Li /* FEC MII MMFR bits definition */
303793fc096SFrank Li #define FEC_MMFR_ST		(1 << 30)
304d3ee8ec7SMarco Hartmann #define FEC_MMFR_ST_C45		(0)
305793fc096SFrank Li #define FEC_MMFR_OP_READ	(2 << 28)
306d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_READ_C45	(3 << 28)
307793fc096SFrank Li #define FEC_MMFR_OP_WRITE	(1 << 28)
308d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_ADDR_WRITE	(0)
309793fc096SFrank Li #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
310793fc096SFrank Li #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
311793fc096SFrank Li #define FEC_MMFR_TA		(2 << 16)
312793fc096SFrank Li #define FEC_MMFR_DATA(v)	(v & 0xffff)
313de40ed31SNimrod Andy /* FEC ECR bits definition */
3147b15515fSFrancesco Dolcini #define FEC_ECR_MAGICEN		(1 << 2)
3157b15515fSFrancesco Dolcini #define FEC_ECR_SLEEP		(1 << 3)
316793fc096SFrank Li 
317793fc096SFrank Li #define FEC_MII_TIMEOUT		30000 /* us */
318793fc096SFrank Li 
319793fc096SFrank Li /* Transmitter timeout */
320793fc096SFrank Li #define TX_TIMEOUT (2 * HZ)
321793fc096SFrank Li 
322793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG	0x1
323793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE	0x2
324de40ed31SNimrod Andy #define FEC_WOL_HAS_MAGIC_PACKET	(0x1 << 0)
325de40ed31SNimrod Andy #define FEC_WOL_FLAG_ENABLE		(0x1 << 1)
326de40ed31SNimrod Andy #define FEC_WOL_FLAG_SLEEP_ON		(0x1 << 2)
327793fc096SFrank Li 
3281b7bde6dSNimrod Andy #define COPYBREAK_DEFAULT	256
3291b7bde6dSNimrod Andy 
33079f33912SNimrod Andy /* Max number of allowed TCP segments for software TSO */
33179f33912SNimrod Andy #define FEC_MAX_TSO_SEGS	100
33279f33912SNimrod Andy #define FEC_MAX_SKB_DESCS	(FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
33379f33912SNimrod Andy 
33479f33912SNimrod Andy #define IS_TSO_HEADER(txq, addr) \
33579f33912SNimrod Andy 	((addr >= txq->tso_hdrs_dma) && \
3367355f276STroy Kisky 	(addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
33779f33912SNimrod Andy 
338793fc096SFrank Li static int mii_cnt;
339793fc096SFrank Li 
3407355f276STroy Kisky static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
3417355f276STroy Kisky 					     struct bufdesc_prop *bd)
342793fc096SFrank Li {
3437355f276STroy Kisky 	return (bdp >= bd->last) ? bd->base
344145d6e29SFugang Duan 			: (struct bufdesc *)(((void *)bdp) + bd->dsize);
345793fc096SFrank Li }
346793fc096SFrank Li 
3477355f276STroy Kisky static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
3487355f276STroy Kisky 					     struct bufdesc_prop *bd)
34936e24e2eSDuan Fugang-B38611 {
3507355f276STroy Kisky 	return (bdp <= bd->base) ? bd->last
351145d6e29SFugang Duan 			: (struct bufdesc *)(((void *)bdp) - bd->dsize);
35236e24e2eSDuan Fugang-B38611 }
35336e24e2eSDuan Fugang-B38611 
3547355f276STroy Kisky static int fec_enet_get_bd_index(struct bufdesc *bdp,
3557355f276STroy Kisky 				 struct bufdesc_prop *bd)
35661a4427bSNimrod Andy {
3577355f276STroy Kisky 	return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
35861a4427bSNimrod Andy }
35961a4427bSNimrod Andy 
3607355f276STroy Kisky static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
3616e909283SNimrod Andy {
3626e909283SNimrod Andy 	int entries;
3636e909283SNimrod Andy 
3647355f276STroy Kisky 	entries = (((const char *)txq->dirty_tx -
3657355f276STroy Kisky 			(const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
3666e909283SNimrod Andy 
3677355f276STroy Kisky 	return entries >= 0 ? entries : entries + txq->bd.ring_size;
3686e909283SNimrod Andy }
3696e909283SNimrod Andy 
370c20e599bSLothar Waßmann static void swap_buffer(void *bufaddr, int len)
371793fc096SFrank Li {
372793fc096SFrank Li 	int i;
373793fc096SFrank Li 	unsigned int *buf = bufaddr;
374793fc096SFrank Li 
3757b487d07SLothar Waßmann 	for (i = 0; i < len; i += 4, buf++)
376e453789aSLothar Waßmann 		swab32s(buf);
377793fc096SFrank Li }
378793fc096SFrank Li 
379344756f6SRussell King static void fec_dump(struct net_device *ndev)
380344756f6SRussell King {
381344756f6SRussell King 	struct fec_enet_private *fep = netdev_priv(ndev);
3824d494cdcSFugang Duan 	struct bufdesc *bdp;
3834d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
3844d494cdcSFugang Duan 	int index = 0;
385344756f6SRussell King 
386344756f6SRussell King 	netdev_info(ndev, "TX ring dump\n");
387344756f6SRussell King 	pr_info("Nr     SC     addr       len  SKB\n");
388344756f6SRussell King 
3894d494cdcSFugang Duan 	txq = fep->tx_queue[0];
3907355f276STroy Kisky 	bdp = txq->bd.base;
3914d494cdcSFugang Duan 
392344756f6SRussell King 	do {
3935cfa3039SJohannes Berg 		pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
394344756f6SRussell King 			index,
3957355f276STroy Kisky 			bdp == txq->bd.cur ? 'S' : ' ',
3964d494cdcSFugang Duan 			bdp == txq->dirty_tx ? 'H' : ' ',
3975cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_sc),
3985cfa3039SJohannes Berg 			fec32_to_cpu(bdp->cbd_bufaddr),
3995cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_datlen),
4004d494cdcSFugang Duan 			txq->tx_skbuff[index]);
4017355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
402344756f6SRussell King 		index++;
4037355f276STroy Kisky 	} while (bdp != txq->bd.base);
404344756f6SRussell King }
405344756f6SRussell King 
40662a02c98SFugang Duan static inline bool is_ipv4_pkt(struct sk_buff *skb)
40762a02c98SFugang Duan {
40862a02c98SFugang Duan 	return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
40962a02c98SFugang Duan }
41062a02c98SFugang Duan 
4114c09eed9SJim Baxter static int
4124c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
4134c09eed9SJim Baxter {
4144c09eed9SJim Baxter 	/* Only run for packets requiring a checksum. */
4154c09eed9SJim Baxter 	if (skb->ip_summed != CHECKSUM_PARTIAL)
4164c09eed9SJim Baxter 		return 0;
4174c09eed9SJim Baxter 
4184c09eed9SJim Baxter 	if (unlikely(skb_cow_head(skb, 0)))
4194c09eed9SJim Baxter 		return -1;
4204c09eed9SJim Baxter 
42162a02c98SFugang Duan 	if (is_ipv4_pkt(skb))
42296c50caaSNimrod Andy 		ip_hdr(skb)->check = 0;
4234c09eed9SJim Baxter 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
4244c09eed9SJim Baxter 
4254c09eed9SJim Baxter 	return 0;
4264c09eed9SJim Baxter }
4274c09eed9SJim Baxter 
42895698ff6SShenwei Wang static int
42995698ff6SShenwei Wang fec_enet_create_page_pool(struct fec_enet_private *fep,
43095698ff6SShenwei Wang 			  struct fec_enet_priv_rx_q *rxq, int size)
43195698ff6SShenwei Wang {
4326d6b39f1SShenwei Wang 	struct bpf_prog *xdp_prog = READ_ONCE(fep->xdp_prog);
43395698ff6SShenwei Wang 	struct page_pool_params pp_params = {
43495698ff6SShenwei Wang 		.order = 0,
43595698ff6SShenwei Wang 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
43695698ff6SShenwei Wang 		.pool_size = size,
43795698ff6SShenwei Wang 		.nid = dev_to_node(&fep->pdev->dev),
43895698ff6SShenwei Wang 		.dev = &fep->pdev->dev,
4396d6b39f1SShenwei Wang 		.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
44095698ff6SShenwei Wang 		.offset = FEC_ENET_XDP_HEADROOM,
44195698ff6SShenwei Wang 		.max_len = FEC_ENET_RX_FRSIZE,
44295698ff6SShenwei Wang 	};
44395698ff6SShenwei Wang 	int err;
44495698ff6SShenwei Wang 
44595698ff6SShenwei Wang 	rxq->page_pool = page_pool_create(&pp_params);
44695698ff6SShenwei Wang 	if (IS_ERR(rxq->page_pool)) {
44795698ff6SShenwei Wang 		err = PTR_ERR(rxq->page_pool);
44895698ff6SShenwei Wang 		rxq->page_pool = NULL;
44995698ff6SShenwei Wang 		return err;
45095698ff6SShenwei Wang 	}
45195698ff6SShenwei Wang 
45295698ff6SShenwei Wang 	err = xdp_rxq_info_reg(&rxq->xdp_rxq, fep->netdev, rxq->id, 0);
45395698ff6SShenwei Wang 	if (err < 0)
45495698ff6SShenwei Wang 		goto err_free_pp;
45595698ff6SShenwei Wang 
45695698ff6SShenwei Wang 	err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
45795698ff6SShenwei Wang 					 rxq->page_pool);
45895698ff6SShenwei Wang 	if (err)
45995698ff6SShenwei Wang 		goto err_unregister_rxq;
46095698ff6SShenwei Wang 
46195698ff6SShenwei Wang 	return 0;
46295698ff6SShenwei Wang 
46395698ff6SShenwei Wang err_unregister_rxq:
46495698ff6SShenwei Wang 	xdp_rxq_info_unreg(&rxq->xdp_rxq);
46595698ff6SShenwei Wang err_free_pp:
46695698ff6SShenwei Wang 	page_pool_destroy(rxq->page_pool);
46795698ff6SShenwei Wang 	rxq->page_pool = NULL;
46895698ff6SShenwei Wang 	return err;
46995698ff6SShenwei Wang }
47095698ff6SShenwei Wang 
471c4bc44c6SKevin Hao static struct bufdesc *
4724d494cdcSFugang Duan fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
4734d494cdcSFugang Duan 			     struct sk_buff *skb,
4744d494cdcSFugang Duan 			     struct net_device *ndev)
4756e909283SNimrod Andy {
4766e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
4777355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
4786e909283SNimrod Andy 	struct bufdesc_ex *ebdp;
4796e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
4806e909283SNimrod Andy 	int frag, frag_len;
4816e909283SNimrod Andy 	unsigned short status;
4826e909283SNimrod Andy 	unsigned int estatus = 0;
4836e909283SNimrod Andy 	skb_frag_t *this_frag;
4846e909283SNimrod Andy 	unsigned int index;
4856e909283SNimrod Andy 	void *bufaddr;
486d6bf3143SRussell King 	dma_addr_t addr;
4876e909283SNimrod Andy 	int i;
4886e909283SNimrod Andy 
4896e909283SNimrod Andy 	for (frag = 0; frag < nr_frags; frag++) {
4906e909283SNimrod Andy 		this_frag = &skb_shinfo(skb)->frags[frag];
4917355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
4926e909283SNimrod Andy 		ebdp = (struct bufdesc_ex *)bdp;
4936e909283SNimrod Andy 
4945cfa3039SJohannes Berg 		status = fec16_to_cpu(bdp->cbd_sc);
4956e909283SNimrod Andy 		status &= ~BD_ENET_TX_STATS;
4966e909283SNimrod Andy 		status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
497d7840976SMatthew Wilcox (Oracle) 		frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
4986e909283SNimrod Andy 
4996e909283SNimrod Andy 		/* Handle the last BD specially */
5006e909283SNimrod Andy 		if (frag == nr_frags - 1) {
5016e909283SNimrod Andy 			status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
5026e909283SNimrod Andy 			if (fep->bufdesc_ex) {
5036e909283SNimrod Andy 				estatus |= BD_ENET_TX_INT;
5046e909283SNimrod Andy 				if (unlikely(skb_shinfo(skb)->tx_flags &
5056e909283SNimrod Andy 					SKBTX_HW_TSTAMP && fep->hwts_tx_en))
5066e909283SNimrod Andy 					estatus |= BD_ENET_TX_TS;
5076e909283SNimrod Andy 			}
5086e909283SNimrod Andy 		}
5096e909283SNimrod Andy 
5106e909283SNimrod Andy 		if (fep->bufdesc_ex) {
5116b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_HAS_AVB)
51253bb20d1STroy Kisky 				estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
5136e909283SNimrod Andy 			if (skb->ip_summed == CHECKSUM_PARTIAL)
5146e909283SNimrod Andy 				estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
515471ff445SJoakim Zhang 
5166e909283SNimrod Andy 			ebdp->cbd_bdu = 0;
5175cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(estatus);
5186e909283SNimrod Andy 		}
5196e909283SNimrod Andy 
520d7840976SMatthew Wilcox (Oracle) 		bufaddr = skb_frag_address(this_frag);
5216e909283SNimrod Andy 
5227355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
52341ef84ceSFugang Duan 		if (((unsigned long) bufaddr) & fep->tx_align ||
5246b7e4008SLothar Waßmann 			fep->quirks & FEC_QUIRK_SWAP_FRAME) {
5254d494cdcSFugang Duan 			memcpy(txq->tx_bounce[index], bufaddr, frag_len);
5264d494cdcSFugang Duan 			bufaddr = txq->tx_bounce[index];
5276e909283SNimrod Andy 
5286b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
5296e909283SNimrod Andy 				swap_buffer(bufaddr, frag_len);
5306e909283SNimrod Andy 		}
5316e909283SNimrod Andy 
532d6bf3143SRussell King 		addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
533d6bf3143SRussell King 				      DMA_TO_DEVICE);
534d6bf3143SRussell King 		if (dma_mapping_error(&fep->pdev->dev, addr)) {
5356e909283SNimrod Andy 			if (net_ratelimit())
5366e909283SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
5376e909283SNimrod Andy 			goto dma_mapping_error;
5386e909283SNimrod Andy 		}
5396e909283SNimrod Andy 
5405cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(addr);
5415cfa3039SJohannes Berg 		bdp->cbd_datlen = cpu_to_fec16(frag_len);
542be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
543be293467STroy Kisky 		 * performed before transferring ownership.
544be293467STroy Kisky 		 */
545be293467STroy Kisky 		wmb();
5465cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(status);
5476e909283SNimrod Andy 	}
5486e909283SNimrod Andy 
549c4bc44c6SKevin Hao 	return bdp;
5506e909283SNimrod Andy dma_mapping_error:
5517355f276STroy Kisky 	bdp = txq->bd.cur;
5526e909283SNimrod Andy 	for (i = 0; i < frag; i++) {
5537355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
5545cfa3039SJohannes Berg 		dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
5555cfa3039SJohannes Berg 				 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
5566e909283SNimrod Andy 	}
557c4bc44c6SKevin Hao 	return ERR_PTR(-ENOMEM);
5586e909283SNimrod Andy }
5596e909283SNimrod Andy 
5604d494cdcSFugang Duan static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
5614d494cdcSFugang Duan 				   struct sk_buff *skb, struct net_device *ndev)
5626e909283SNimrod Andy {
5636e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
5646e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
5656e909283SNimrod Andy 	struct bufdesc *bdp, *last_bdp;
5666e909283SNimrod Andy 	void *bufaddr;
567d6bf3143SRussell King 	dma_addr_t addr;
5686e909283SNimrod Andy 	unsigned short status;
5696e909283SNimrod Andy 	unsigned short buflen;
5706e909283SNimrod Andy 	unsigned int estatus = 0;
5716e909283SNimrod Andy 	unsigned int index;
57279f33912SNimrod Andy 	int entries_free;
5736e909283SNimrod Andy 
5747355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
57579f33912SNimrod Andy 	if (entries_free < MAX_SKB_FRAGS + 1) {
57679f33912SNimrod Andy 		dev_kfree_skb_any(skb);
57779f33912SNimrod Andy 		if (net_ratelimit())
57879f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for SG!\n");
57979f33912SNimrod Andy 		return NETDEV_TX_OK;
58079f33912SNimrod Andy 	}
58179f33912SNimrod Andy 
5826e909283SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
5836e909283SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
5846e909283SNimrod Andy 		dev_kfree_skb_any(skb);
5856e909283SNimrod Andy 		return NETDEV_TX_OK;
5866e909283SNimrod Andy 	}
5876e909283SNimrod Andy 
5886e909283SNimrod Andy 	/* Fill in a Tx ring entry */
5897355f276STroy Kisky 	bdp = txq->bd.cur;
590c4bc44c6SKevin Hao 	last_bdp = bdp;
5915cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
5926e909283SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
5936e909283SNimrod Andy 
5946e909283SNimrod Andy 	/* Set buffer length and buffer pointer */
5956e909283SNimrod Andy 	bufaddr = skb->data;
5966e909283SNimrod Andy 	buflen = skb_headlen(skb);
5976e909283SNimrod Andy 
5987355f276STroy Kisky 	index = fec_enet_get_bd_index(bdp, &txq->bd);
59941ef84ceSFugang Duan 	if (((unsigned long) bufaddr) & fep->tx_align ||
6006b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
6014d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, buflen);
6024d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
6036e909283SNimrod Andy 
6046b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
6056e909283SNimrod Andy 			swap_buffer(bufaddr, buflen);
6066e909283SNimrod Andy 	}
6076e909283SNimrod Andy 
608d6bf3143SRussell King 	/* Push the data cache so the CPM does not get stale memory data. */
609d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
610d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
6116e909283SNimrod Andy 		dev_kfree_skb_any(skb);
6126e909283SNimrod Andy 		if (net_ratelimit())
6136e909283SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
6146e909283SNimrod Andy 		return NETDEV_TX_OK;
6156e909283SNimrod Andy 	}
6166e909283SNimrod Andy 
6176e909283SNimrod Andy 	if (nr_frags) {
618c4bc44c6SKevin Hao 		last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
619fc75ba51STroy Kisky 		if (IS_ERR(last_bdp)) {
620fc75ba51STroy Kisky 			dma_unmap_single(&fep->pdev->dev, addr,
621fc75ba51STroy Kisky 					 buflen, DMA_TO_DEVICE);
622fc75ba51STroy Kisky 			dev_kfree_skb_any(skb);
623c4bc44c6SKevin Hao 			return NETDEV_TX_OK;
624fc75ba51STroy Kisky 		}
6256e909283SNimrod Andy 	} else {
6266e909283SNimrod Andy 		status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
6276e909283SNimrod Andy 		if (fep->bufdesc_ex) {
6286e909283SNimrod Andy 			estatus = BD_ENET_TX_INT;
6296e909283SNimrod Andy 			if (unlikely(skb_shinfo(skb)->tx_flags &
6306e909283SNimrod Andy 				SKBTX_HW_TSTAMP && fep->hwts_tx_en))
6316e909283SNimrod Andy 				estatus |= BD_ENET_TX_TS;
6326e909283SNimrod Andy 		}
6336e909283SNimrod Andy 	}
634fc75ba51STroy Kisky 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
635fc75ba51STroy Kisky 	bdp->cbd_datlen = cpu_to_fec16(buflen);
6366e909283SNimrod Andy 
6376e909283SNimrod Andy 	if (fep->bufdesc_ex) {
6386e909283SNimrod Andy 
6396e909283SNimrod Andy 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
6406e909283SNimrod Andy 
6416e909283SNimrod Andy 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
6426e909283SNimrod Andy 			fep->hwts_tx_en))
6436e909283SNimrod Andy 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
6446e909283SNimrod Andy 
6456b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
64653bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
647befe8213SNimrod Andy 
6486e909283SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
6496e909283SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
6506e909283SNimrod Andy 
6516e909283SNimrod Andy 		ebdp->cbd_bdu = 0;
6525cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
6536e909283SNimrod Andy 	}
6546e909283SNimrod Andy 
6557355f276STroy Kisky 	index = fec_enet_get_bd_index(last_bdp, &txq->bd);
6566e909283SNimrod Andy 	/* Save skb pointer */
6574d494cdcSFugang Duan 	txq->tx_skbuff[index] = skb;
6586e909283SNimrod Andy 
659be293467STroy Kisky 	/* Make sure the updates to rest of the descriptor are performed before
660be293467STroy Kisky 	 * transferring ownership.
661be293467STroy Kisky 	 */
662be293467STroy Kisky 	wmb();
6636e909283SNimrod Andy 
6646e909283SNimrod Andy 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
6656e909283SNimrod Andy 	 * it's the last BD of the frame, and to put the CRC on the end.
6666e909283SNimrod Andy 	 */
6676e909283SNimrod Andy 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
6685cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
6696e909283SNimrod Andy 
670793fc096SFrank Li 	/* If this was the last BD in the ring, start at the beginning again. */
6717355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
672793fc096SFrank Li 
6737a2a8451SEric Dumazet 	skb_tx_timestamp(skb);
6747a2a8451SEric Dumazet 
675c4bc44c6SKevin Hao 	/* Make sure the update to bdp and tx_skbuff are performed before
6767355f276STroy Kisky 	 * txq->bd.cur.
677c4bc44c6SKevin Hao 	 */
678c4bc44c6SKevin Hao 	wmb();
6797355f276STroy Kisky 	txq->bd.cur = bdp;
680793fc096SFrank Li 
681793fc096SFrank Li 	/* Trigger transmission start */
68253bb20d1STroy Kisky 	writel(0, txq->bd.reg_desc_active);
683793fc096SFrank Li 
6846e909283SNimrod Andy 	return 0;
685793fc096SFrank Li }
686793fc096SFrank Li 
68779f33912SNimrod Andy static int
6884d494cdcSFugang Duan fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
6894d494cdcSFugang Duan 			  struct net_device *ndev,
69079f33912SNimrod Andy 			  struct bufdesc *bdp, int index, char *data,
69179f33912SNimrod Andy 			  int size, bool last_tcp, bool is_last)
69279f33912SNimrod Andy {
69379f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
69461cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
69579f33912SNimrod Andy 	unsigned short status;
69679f33912SNimrod Andy 	unsigned int estatus = 0;
697d6bf3143SRussell King 	dma_addr_t addr;
69879f33912SNimrod Andy 
6995cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
70079f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
70179f33912SNimrod Andy 
70279f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
70379f33912SNimrod Andy 
70441ef84ceSFugang Duan 	if (((unsigned long) data) & fep->tx_align ||
7056b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
7064d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], data, size);
7074d494cdcSFugang Duan 		data = txq->tx_bounce[index];
70879f33912SNimrod Andy 
7096b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
71079f33912SNimrod Andy 			swap_buffer(data, size);
71179f33912SNimrod Andy 	}
71279f33912SNimrod Andy 
713d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
714d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
71579f33912SNimrod Andy 		dev_kfree_skb_any(skb);
71679f33912SNimrod Andy 		if (net_ratelimit())
71779f33912SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
71806a4df58SZhang Changzhong 		return NETDEV_TX_OK;
71979f33912SNimrod Andy 	}
72079f33912SNimrod Andy 
7215cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(size);
7225cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
723d6bf3143SRussell King 
72479f33912SNimrod Andy 	if (fep->bufdesc_ex) {
7256b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
72653bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
72779f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
72879f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
72979f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
7305cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
73179f33912SNimrod Andy 	}
73279f33912SNimrod Andy 
73379f33912SNimrod Andy 	/* Handle the last BD specially */
73479f33912SNimrod Andy 	if (last_tcp)
73579f33912SNimrod Andy 		status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
73679f33912SNimrod Andy 	if (is_last) {
73779f33912SNimrod Andy 		status |= BD_ENET_TX_INTR;
73879f33912SNimrod Andy 		if (fep->bufdesc_ex)
7395cfa3039SJohannes Berg 			ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
74079f33912SNimrod Andy 	}
74179f33912SNimrod Andy 
7425cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
74379f33912SNimrod Andy 
74479f33912SNimrod Andy 	return 0;
74579f33912SNimrod Andy }
74679f33912SNimrod Andy 
74779f33912SNimrod Andy static int
7484d494cdcSFugang Duan fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
7494d494cdcSFugang Duan 			 struct sk_buff *skb, struct net_device *ndev,
75079f33912SNimrod Andy 			 struct bufdesc *bdp, int index)
75179f33912SNimrod Andy {
75279f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
753504148feSEric Dumazet 	int hdr_len = skb_tcp_all_headers(skb);
75461cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
75579f33912SNimrod Andy 	void *bufaddr;
75679f33912SNimrod Andy 	unsigned long dmabuf;
75779f33912SNimrod Andy 	unsigned short status;
75879f33912SNimrod Andy 	unsigned int estatus = 0;
75979f33912SNimrod Andy 
7605cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
76179f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
76279f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
76379f33912SNimrod Andy 
7644d494cdcSFugang Duan 	bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
7654d494cdcSFugang Duan 	dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
76641ef84ceSFugang Duan 	if (((unsigned long)bufaddr) & fep->tx_align ||
7676b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
7684d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, hdr_len);
7694d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
77079f33912SNimrod Andy 
7716b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
77279f33912SNimrod Andy 			swap_buffer(bufaddr, hdr_len);
77379f33912SNimrod Andy 
77479f33912SNimrod Andy 		dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
77579f33912SNimrod Andy 					hdr_len, DMA_TO_DEVICE);
77679f33912SNimrod Andy 		if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
77779f33912SNimrod Andy 			dev_kfree_skb_any(skb);
77879f33912SNimrod Andy 			if (net_ratelimit())
77979f33912SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
78006a4df58SZhang Changzhong 			return NETDEV_TX_OK;
78179f33912SNimrod Andy 		}
78279f33912SNimrod Andy 	}
78379f33912SNimrod Andy 
7845cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
7855cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(hdr_len);
78679f33912SNimrod Andy 
78779f33912SNimrod Andy 	if (fep->bufdesc_ex) {
7886b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
78953bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
79079f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
79179f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
79279f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
7935cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
79479f33912SNimrod Andy 	}
79579f33912SNimrod Andy 
7965cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
79779f33912SNimrod Andy 
79879f33912SNimrod Andy 	return 0;
79979f33912SNimrod Andy }
80079f33912SNimrod Andy 
8014d494cdcSFugang Duan static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
8024d494cdcSFugang Duan 				   struct sk_buff *skb,
8034d494cdcSFugang Duan 				   struct net_device *ndev)
80479f33912SNimrod Andy {
80579f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
806761b331cSEric Dumazet 	int hdr_len, total_len, data_left;
8077355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
80879f33912SNimrod Andy 	struct tso_t tso;
80979f33912SNimrod Andy 	unsigned int index = 0;
81079f33912SNimrod Andy 	int ret;
81179f33912SNimrod Andy 
8127355f276STroy Kisky 	if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
81379f33912SNimrod Andy 		dev_kfree_skb_any(skb);
81479f33912SNimrod Andy 		if (net_ratelimit())
81579f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for TSO!\n");
81679f33912SNimrod Andy 		return NETDEV_TX_OK;
81779f33912SNimrod Andy 	}
81879f33912SNimrod Andy 
81979f33912SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
82079f33912SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
82179f33912SNimrod Andy 		dev_kfree_skb_any(skb);
82279f33912SNimrod Andy 		return NETDEV_TX_OK;
82379f33912SNimrod Andy 	}
82479f33912SNimrod Andy 
82579f33912SNimrod Andy 	/* Initialize the TSO handler, and prepare the first payload */
826761b331cSEric Dumazet 	hdr_len = tso_start(skb, &tso);
82779f33912SNimrod Andy 
82879f33912SNimrod Andy 	total_len = skb->len - hdr_len;
82979f33912SNimrod Andy 	while (total_len > 0) {
83079f33912SNimrod Andy 		char *hdr;
83179f33912SNimrod Andy 
8327355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
83379f33912SNimrod Andy 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
83479f33912SNimrod Andy 		total_len -= data_left;
83579f33912SNimrod Andy 
83679f33912SNimrod Andy 		/* prepare packet headers: MAC + IP + TCP */
8374d494cdcSFugang Duan 		hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
83879f33912SNimrod Andy 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
8394d494cdcSFugang Duan 		ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
84079f33912SNimrod Andy 		if (ret)
84179f33912SNimrod Andy 			goto err_release;
84279f33912SNimrod Andy 
84379f33912SNimrod Andy 		while (data_left > 0) {
84479f33912SNimrod Andy 			int size;
84579f33912SNimrod Andy 
84679f33912SNimrod Andy 			size = min_t(int, tso.size, data_left);
8477355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
8487355f276STroy Kisky 			index = fec_enet_get_bd_index(bdp, &txq->bd);
8494d494cdcSFugang Duan 			ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
8504d494cdcSFugang Duan 							bdp, index,
8514d494cdcSFugang Duan 							tso.data, size,
8524d494cdcSFugang Duan 							size == data_left,
85379f33912SNimrod Andy 							total_len == 0);
85479f33912SNimrod Andy 			if (ret)
85579f33912SNimrod Andy 				goto err_release;
85679f33912SNimrod Andy 
85779f33912SNimrod Andy 			data_left -= size;
85879f33912SNimrod Andy 			tso_build_data(skb, &tso, size);
85979f33912SNimrod Andy 		}
86079f33912SNimrod Andy 
8617355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
86279f33912SNimrod Andy 	}
86379f33912SNimrod Andy 
86479f33912SNimrod Andy 	/* Save skb pointer */
8654d494cdcSFugang Duan 	txq->tx_skbuff[index] = skb;
86679f33912SNimrod Andy 
86779f33912SNimrod Andy 	skb_tx_timestamp(skb);
8687355f276STroy Kisky 	txq->bd.cur = bdp;
86979f33912SNimrod Andy 
87079f33912SNimrod Andy 	/* Trigger transmission start */
8716b7e4008SLothar Waßmann 	if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
87253bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
87353bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
87453bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
87553bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active))
87653bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
87779f33912SNimrod Andy 
87879f33912SNimrod Andy 	return 0;
87979f33912SNimrod Andy 
88079f33912SNimrod Andy err_release:
88179f33912SNimrod Andy 	/* TODO: Release all used data descriptors for TSO */
88279f33912SNimrod Andy 	return ret;
88379f33912SNimrod Andy }
88479f33912SNimrod Andy 
88561a4427bSNimrod Andy static netdev_tx_t
88661a4427bSNimrod Andy fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
88761a4427bSNimrod Andy {
88861a4427bSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
8896e909283SNimrod Andy 	int entries_free;
8904d494cdcSFugang Duan 	unsigned short queue;
8914d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
8924d494cdcSFugang Duan 	struct netdev_queue *nq;
89361a4427bSNimrod Andy 	int ret;
89461a4427bSNimrod Andy 
8954d494cdcSFugang Duan 	queue = skb_get_queue_mapping(skb);
8964d494cdcSFugang Duan 	txq = fep->tx_queue[queue];
8974d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue);
8984d494cdcSFugang Duan 
89979f33912SNimrod Andy 	if (skb_is_gso(skb))
9004d494cdcSFugang Duan 		ret = fec_enet_txq_submit_tso(txq, skb, ndev);
90179f33912SNimrod Andy 	else
9024d494cdcSFugang Duan 		ret = fec_enet_txq_submit_skb(txq, skb, ndev);
9036e909283SNimrod Andy 	if (ret)
9046e909283SNimrod Andy 		return ret;
90561a4427bSNimrod Andy 
9067355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
9074d494cdcSFugang Duan 	if (entries_free <= txq->tx_stop_threshold)
9084d494cdcSFugang Duan 		netif_tx_stop_queue(nq);
90961a4427bSNimrod Andy 
91061a4427bSNimrod Andy 	return NETDEV_TX_OK;
91161a4427bSNimrod Andy }
91261a4427bSNimrod Andy 
913a210576cSDavid S. Miller /* Init RX & TX buffer descriptors
914a210576cSDavid S. Miller  */
915a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev)
916a210576cSDavid S. Miller {
917a210576cSDavid S. Miller 	struct fec_enet_private *fep = netdev_priv(dev);
9184d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
9194d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
920a210576cSDavid S. Miller 	struct bufdesc *bdp;
921a210576cSDavid S. Miller 	unsigned int i;
92259d0f746SFrank Li 	unsigned int q;
923a210576cSDavid S. Miller 
92459d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
925a210576cSDavid S. Miller 		/* Initialize the receive buffer descriptors. */
92659d0f746SFrank Li 		rxq = fep->rx_queue[q];
9277355f276STroy Kisky 		bdp = rxq->bd.base;
9284d494cdcSFugang Duan 
9297355f276STroy Kisky 		for (i = 0; i < rxq->bd.ring_size; i++) {
930a210576cSDavid S. Miller 
931a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
932a210576cSDavid S. Miller 			if (bdp->cbd_bufaddr)
9335cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
934a210576cSDavid S. Miller 			else
9355cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(0);
9367355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
937a210576cSDavid S. Miller 		}
938a210576cSDavid S. Miller 
939a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
9407355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
9415cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
942a210576cSDavid S. Miller 
9437355f276STroy Kisky 		rxq->bd.cur = rxq->bd.base;
94459d0f746SFrank Li 	}
945a210576cSDavid S. Miller 
94659d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
947a210576cSDavid S. Miller 		/* ...and the same for transmit */
94859d0f746SFrank Li 		txq = fep->tx_queue[q];
9497355f276STroy Kisky 		bdp = txq->bd.base;
9507355f276STroy Kisky 		txq->bd.cur = bdp;
951a210576cSDavid S. Miller 
9527355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
953a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
9545cfa3039SJohannes Berg 			bdp->cbd_sc = cpu_to_fec16(0);
955178e5f57SFugang Duan 			if (bdp->cbd_bufaddr &&
956178e5f57SFugang Duan 			    !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
957178e5f57SFugang Duan 				dma_unmap_single(&fep->pdev->dev,
958178e5f57SFugang Duan 						 fec32_to_cpu(bdp->cbd_bufaddr),
959178e5f57SFugang Duan 						 fec16_to_cpu(bdp->cbd_datlen),
960178e5f57SFugang Duan 						 DMA_TO_DEVICE);
9614d494cdcSFugang Duan 			if (txq->tx_skbuff[i]) {
9624d494cdcSFugang Duan 				dev_kfree_skb_any(txq->tx_skbuff[i]);
9634d494cdcSFugang Duan 				txq->tx_skbuff[i] = NULL;
964a210576cSDavid S. Miller 			}
9655cfa3039SJohannes Berg 			bdp->cbd_bufaddr = cpu_to_fec32(0);
9667355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
967a210576cSDavid S. Miller 		}
968a210576cSDavid S. Miller 
969a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
9707355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
9715cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
9724d494cdcSFugang Duan 		txq->dirty_tx = bdp;
973a210576cSDavid S. Miller 	}
97459d0f746SFrank Li }
97559d0f746SFrank Li 
976ce99d0d3SFrank Li static void fec_enet_active_rxring(struct net_device *ndev)
977ce99d0d3SFrank Li {
978ce99d0d3SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
979ce99d0d3SFrank Li 	int i;
980ce99d0d3SFrank Li 
981ce99d0d3SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
98253bb20d1STroy Kisky 		writel(0, fep->rx_queue[i]->bd.reg_desc_active);
983ce99d0d3SFrank Li }
984ce99d0d3SFrank Li 
98559d0f746SFrank Li static void fec_enet_enable_ring(struct net_device *ndev)
98659d0f746SFrank Li {
98759d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
98859d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
98959d0f746SFrank Li 	struct fec_enet_priv_rx_q *rxq;
99059d0f746SFrank Li 	int i;
99159d0f746SFrank Li 
99259d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
99359d0f746SFrank Li 		rxq = fep->rx_queue[i];
9947355f276STroy Kisky 		writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
995fbbeefddSAndrew Lunn 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
99659d0f746SFrank Li 
99759d0f746SFrank Li 		/* enable DMA1/2 */
99859d0f746SFrank Li 		if (i)
99959d0f746SFrank Li 			writel(RCMR_MATCHEN | RCMR_CMP(i),
100059d0f746SFrank Li 			       fep->hwp + FEC_RCMR(i));
100159d0f746SFrank Li 	}
100259d0f746SFrank Li 
100359d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
100459d0f746SFrank Li 		txq = fep->tx_queue[i];
10057355f276STroy Kisky 		writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
100659d0f746SFrank Li 
100759d0f746SFrank Li 		/* enable DMA1/2 */
100859d0f746SFrank Li 		if (i)
100959d0f746SFrank Li 			writel(DMA_CLASS_EN | IDLE_SLOPE(i),
101059d0f746SFrank Li 			       fep->hwp + FEC_DMA_CFG(i));
101159d0f746SFrank Li 	}
101259d0f746SFrank Li }
101359d0f746SFrank Li 
1014dbc64a8eSRussell King /*
1015dbc64a8eSRussell King  * This function is called to start or restart the FEC during a link
1016dbc64a8eSRussell King  * change, transmit timeout, or to reconfigure the FEC.  The network
1017dbc64a8eSRussell King  * packet processing for this device must be stopped before this call.
1018793fc096SFrank Li  */
1019793fc096SFrank Li static void
1020ef83337dSRussell King fec_restart(struct net_device *ndev)
1021793fc096SFrank Li {
1022793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1023793fc096SFrank Li 	u32 temp_mac[2];
1024793fc096SFrank Li 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
1025793fc096SFrank Li 	u32 ecntl = 0x2; /* ETHEREN */
1026793fc096SFrank Li 
1027106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
1028106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1029106c314cSFugang Duan 	 * instead of reset MAC itself.
1030106c314cSFugang Duan 	 */
1031471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES ||
1032c730ab42SLaurent Badel 	    ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) {
1033106c314cSFugang Duan 		writel(0, fep->hwp + FEC_ECNTRL);
1034106c314cSFugang Duan 	} else {
1035793fc096SFrank Li 		writel(1, fep->hwp + FEC_ECNTRL);
1036793fc096SFrank Li 		udelay(10);
1037106c314cSFugang Duan 	}
1038793fc096SFrank Li 
1039793fc096SFrank Li 	/*
1040793fc096SFrank Li 	 * enet-mac reset will reset mac address registers too,
1041793fc096SFrank Li 	 * so need to reconfigure it.
1042793fc096SFrank Li 	 */
1043793fc096SFrank Li 	memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
10445cfa3039SJohannes Berg 	writel((__force u32)cpu_to_be32(temp_mac[0]),
10455cfa3039SJohannes Berg 	       fep->hwp + FEC_ADDR_LOW);
10465cfa3039SJohannes Berg 	writel((__force u32)cpu_to_be32(temp_mac[1]),
10475cfa3039SJohannes Berg 	       fep->hwp + FEC_ADDR_HIGH);
1048793fc096SFrank Li 
1049f166f890SAndrew Lunn 	/* Clear any outstanding interrupt, except MDIO. */
1050f166f890SAndrew Lunn 	writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
1051793fc096SFrank Li 
1052a210576cSDavid S. Miller 	fec_enet_bd_init(ndev);
1053a210576cSDavid S. Miller 
105459d0f746SFrank Li 	fec_enet_enable_ring(ndev);
1055793fc096SFrank Li 
1056793fc096SFrank Li 	/* Enable MII mode */
1057ef83337dSRussell King 	if (fep->full_duplex == DUPLEX_FULL) {
1058793fc096SFrank Li 		/* FD enable */
1059793fc096SFrank Li 		writel(0x04, fep->hwp + FEC_X_CNTRL);
1060793fc096SFrank Li 	} else {
1061793fc096SFrank Li 		/* No Rcv on Xmit */
1062793fc096SFrank Li 		rcntl |= 0x02;
1063793fc096SFrank Li 		writel(0x0, fep->hwp + FEC_X_CNTRL);
1064793fc096SFrank Li 	}
1065793fc096SFrank Li 
1066793fc096SFrank Li 	/* Set MII speed */
1067793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1068793fc096SFrank Li 
1069d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
107018803495SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_HAS_RACC) {
107132d1bbb1SGeert Uytterhoeven 		u32 val = readl(fep->hwp + FEC_RACC);
107232d1bbb1SGeert Uytterhoeven 
10733ac72b7bSEric Nelson 		/* align IP header */
10743ac72b7bSEric Nelson 		val |= FEC_RACC_SHIFT16;
10754c09eed9SJim Baxter 		if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
10763ac72b7bSEric Nelson 			/* set RX checksum */
10774c09eed9SJim Baxter 			val |= FEC_RACC_OPTIONS;
10784c09eed9SJim Baxter 		else
10794c09eed9SJim Baxter 			val &= ~FEC_RACC_OPTIONS;
10804c09eed9SJim Baxter 		writel(val, fep->hwp + FEC_RACC);
108155cd48c8STroy Kisky 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
108232867fccSFabio Estevam 	}
1083d1391930SGuenter Roeck #endif
10844c09eed9SJim Baxter 
1085793fc096SFrank Li 	/*
1086793fc096SFrank Li 	 * The phy interface and speed need to get configured
1087793fc096SFrank Li 	 * differently on enet-mac.
1088793fc096SFrank Li 	 */
10896b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1090793fc096SFrank Li 		/* Enable flow control and length check */
1091793fc096SFrank Li 		rcntl |= 0x40000000 | 0x00000020;
1092793fc096SFrank Li 
1093793fc096SFrank Li 		/* RGMII, RMII or MII */
1094e813bb2bSMarkus Pargmann 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
1095e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1096e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1097e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1098793fc096SFrank Li 			rcntl |= (1 << 6);
1099793fc096SFrank Li 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1100793fc096SFrank Li 			rcntl |= (1 << 8);
1101793fc096SFrank Li 		else
1102793fc096SFrank Li 			rcntl &= ~(1 << 8);
1103793fc096SFrank Li 
1104793fc096SFrank Li 		/* 1G, 100M or 10M */
110545f5c327SPhilippe Reynes 		if (ndev->phydev) {
110645f5c327SPhilippe Reynes 			if (ndev->phydev->speed == SPEED_1000)
1107793fc096SFrank Li 				ecntl |= (1 << 5);
110845f5c327SPhilippe Reynes 			else if (ndev->phydev->speed == SPEED_100)
1109793fc096SFrank Li 				rcntl &= ~(1 << 9);
1110793fc096SFrank Li 			else
1111793fc096SFrank Li 				rcntl |= (1 << 9);
1112793fc096SFrank Li 		}
1113793fc096SFrank Li 	} else {
1114793fc096SFrank Li #ifdef FEC_MIIGSK_ENR
11156b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1116793fc096SFrank Li 			u32 cfgr;
1117793fc096SFrank Li 			/* disable the gasket and wait */
1118793fc096SFrank Li 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
1119793fc096SFrank Li 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1120793fc096SFrank Li 				udelay(1);
1121793fc096SFrank Li 
1122793fc096SFrank Li 			/*
1123793fc096SFrank Li 			 * configure the gasket:
1124793fc096SFrank Li 			 *   RMII, 50 MHz, no loopback, no echo
1125793fc096SFrank Li 			 *   MII, 25 MHz, no loopback, no echo
1126793fc096SFrank Li 			 */
1127793fc096SFrank Li 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1128793fc096SFrank Li 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
112945f5c327SPhilippe Reynes 			if (ndev->phydev && ndev->phydev->speed == SPEED_10)
1130793fc096SFrank Li 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1131793fc096SFrank Li 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1132793fc096SFrank Li 
1133793fc096SFrank Li 			/* re-enable the gasket */
1134793fc096SFrank Li 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
1135793fc096SFrank Li 		}
1136793fc096SFrank Li #endif
1137793fc096SFrank Li 	}
1138793fc096SFrank Li 
1139d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1140793fc096SFrank Li 	/* enable pause frame*/
1141793fc096SFrank Li 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1142793fc096SFrank Li 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
114345f5c327SPhilippe Reynes 	     ndev->phydev && ndev->phydev->pause)) {
1144793fc096SFrank Li 		rcntl |= FEC_ENET_FCE;
1145793fc096SFrank Li 
11464c09eed9SJim Baxter 		/* set FIFO threshold parameter to reduce overrun */
1147793fc096SFrank Li 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1148793fc096SFrank Li 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1149793fc096SFrank Li 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1150793fc096SFrank Li 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1151793fc096SFrank Li 
1152793fc096SFrank Li 		/* OPD */
1153793fc096SFrank Li 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1154793fc096SFrank Li 	} else {
1155793fc096SFrank Li 		rcntl &= ~FEC_ENET_FCE;
1156793fc096SFrank Li 	}
1157d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
1158793fc096SFrank Li 
1159793fc096SFrank Li 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
1160793fc096SFrank Li 
116184fe6182SStefan Wahren 	/* Setup multicast filter. */
116284fe6182SStefan Wahren 	set_multicast_list(ndev);
116384fe6182SStefan Wahren #ifndef CONFIG_M5272
116484fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
116584fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
116684fe6182SStefan Wahren #endif
116784fe6182SStefan Wahren 
11686b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1169793fc096SFrank Li 		/* enable ENET endian swap */
1170793fc096SFrank Li 		ecntl |= (1 << 8);
1171793fc096SFrank Li 		/* enable ENET store and forward mode */
1172793fc096SFrank Li 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
1173793fc096SFrank Li 	}
1174793fc096SFrank Li 
1175793fc096SFrank Li 	if (fep->bufdesc_ex)
11767b15515fSFrancesco Dolcini 		ecntl |= (1 << 4);
1177793fc096SFrank Li 
1178fc539459SFugang Duan 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1179fc539459SFugang Duan 	    fep->rgmii_txc_dly)
1180fc539459SFugang Duan 		ecntl |= FEC_ENET_TXC_DLY;
1181fc539459SFugang Duan 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1182fc539459SFugang Duan 	    fep->rgmii_rxc_dly)
1183fc539459SFugang Duan 		ecntl |= FEC_ENET_RXC_DLY;
1184fc539459SFugang Duan 
118538ae92dcSChris Healy #ifndef CONFIG_M5272
1186b9eef55cSJim Baxter 	/* Enable the MIB statistic event counters */
1187b9eef55cSJim Baxter 	writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
118838ae92dcSChris Healy #endif
118938ae92dcSChris Healy 
1190793fc096SFrank Li 	/* And last, enable the transmit and receive processing */
1191793fc096SFrank Li 	writel(ecntl, fep->hwp + FEC_ECNTRL);
1192ce99d0d3SFrank Li 	fec_enet_active_rxring(ndev);
1193793fc096SFrank Li 
1194793fc096SFrank Li 	if (fep->bufdesc_ex)
1195793fc096SFrank Li 		fec_ptp_start_cyclecounter(ndev);
1196793fc096SFrank Li 
1197793fc096SFrank Li 	/* Enable interrupts we wish to service */
11980c5a3aefSNimrod Andy 	if (fep->link)
1199793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
12000c5a3aefSNimrod Andy 	else
1201f166f890SAndrew Lunn 		writel(0, fep->hwp + FEC_IMASK);
1202d851b47bSFugang Duan 
1203d851b47bSFugang Duan 	/* Init the interrupt coalescing */
12047e630356SRasmus Villemoes 	if (fep->quirks & FEC_QUIRK_HAS_COALESCE)
1205df727d45SRasmus Villemoes 		fec_enet_itr_coal_set(ndev);
1206793fc096SFrank Li }
1207793fc096SFrank Li 
120840c79ce1SWei Fang static int fec_enet_ipc_handle_init(struct fec_enet_private *fep)
120940c79ce1SWei Fang {
121040c79ce1SWei Fang 	if (!(of_machine_is_compatible("fsl,imx8qm") ||
121140c79ce1SWei Fang 	      of_machine_is_compatible("fsl,imx8qxp") ||
121240c79ce1SWei Fang 	      of_machine_is_compatible("fsl,imx8dxl")))
121340c79ce1SWei Fang 		return 0;
121440c79ce1SWei Fang 
121540c79ce1SWei Fang 	return imx_scu_get_handle(&fep->ipc_handle);
121640c79ce1SWei Fang }
121740c79ce1SWei Fang 
121840c79ce1SWei Fang static void fec_enet_ipg_stop_set(struct fec_enet_private *fep, bool enabled)
121940c79ce1SWei Fang {
122040c79ce1SWei Fang 	struct device_node *np = fep->pdev->dev.of_node;
122140c79ce1SWei Fang 	u32 rsrc_id, val;
122240c79ce1SWei Fang 	int idx;
122340c79ce1SWei Fang 
122440c79ce1SWei Fang 	if (!np || !fep->ipc_handle)
122540c79ce1SWei Fang 		return;
122640c79ce1SWei Fang 
122740c79ce1SWei Fang 	idx = of_alias_get_id(np, "ethernet");
122840c79ce1SWei Fang 	if (idx < 0)
122940c79ce1SWei Fang 		idx = 0;
123040c79ce1SWei Fang 	rsrc_id = idx ? IMX_SC_R_ENET_1 : IMX_SC_R_ENET_0;
123140c79ce1SWei Fang 
123240c79ce1SWei Fang 	val = enabled ? 1 : 0;
123340c79ce1SWei Fang 	imx_sc_misc_set_control(fep->ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val);
123440c79ce1SWei Fang }
123540c79ce1SWei Fang 
1236da722186SMartin Fuzzey static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
1237da722186SMartin Fuzzey {
1238da722186SMartin Fuzzey 	struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1239da722186SMartin Fuzzey 	struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
1240da722186SMartin Fuzzey 
1241da722186SMartin Fuzzey 	if (stop_gpr->gpr) {
1242da722186SMartin Fuzzey 		if (enabled)
1243da722186SMartin Fuzzey 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1244da722186SMartin Fuzzey 					   BIT(stop_gpr->bit),
1245da722186SMartin Fuzzey 					   BIT(stop_gpr->bit));
1246da722186SMartin Fuzzey 		else
1247da722186SMartin Fuzzey 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1248da722186SMartin Fuzzey 					   BIT(stop_gpr->bit), 0);
1249da722186SMartin Fuzzey 	} else if (pdata && pdata->sleep_mode_enable) {
1250da722186SMartin Fuzzey 		pdata->sleep_mode_enable(enabled);
125140c79ce1SWei Fang 	} else {
125240c79ce1SWei Fang 		fec_enet_ipg_stop_set(fep, enabled);
1253da722186SMartin Fuzzey 	}
1254da722186SMartin Fuzzey }
1255da722186SMartin Fuzzey 
12560b6f65c7SJoakim Zhang static void fec_irqs_disable(struct net_device *ndev)
12570b6f65c7SJoakim Zhang {
12580b6f65c7SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
12590b6f65c7SJoakim Zhang 
12600b6f65c7SJoakim Zhang 	writel(0, fep->hwp + FEC_IMASK);
12610b6f65c7SJoakim Zhang }
12620b6f65c7SJoakim Zhang 
12630b6f65c7SJoakim Zhang static void fec_irqs_disable_except_wakeup(struct net_device *ndev)
12640b6f65c7SJoakim Zhang {
12650b6f65c7SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
12660b6f65c7SJoakim Zhang 
12670b6f65c7SJoakim Zhang 	writel(0, fep->hwp + FEC_IMASK);
12680b6f65c7SJoakim Zhang 	writel(FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
12690b6f65c7SJoakim Zhang }
12700b6f65c7SJoakim Zhang 
1271793fc096SFrank Li static void
1272793fc096SFrank Li fec_stop(struct net_device *ndev)
1273793fc096SFrank Li {
1274793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1275793fc096SFrank Li 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1276de40ed31SNimrod Andy 	u32 val;
1277793fc096SFrank Li 
1278793fc096SFrank Li 	/* We cannot expect a graceful transmit stop without link !!! */
1279793fc096SFrank Li 	if (fep->link) {
1280793fc096SFrank Li 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1281793fc096SFrank Li 		udelay(10);
1282793fc096SFrank Li 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
128331b7720cSJoe Perches 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1284793fc096SFrank Li 	}
1285793fc096SFrank Li 
1286106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
1287106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1288106c314cSFugang Duan 	 * instead of reset MAC itself.
1289106c314cSFugang Duan 	 */
1290de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1291471ff445SJoakim Zhang 		if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
1292106c314cSFugang Duan 			writel(0, fep->hwp + FEC_ECNTRL);
1293106c314cSFugang Duan 		} else {
1294793fc096SFrank Li 			writel(1, fep->hwp + FEC_ECNTRL);
1295793fc096SFrank Li 			udelay(10);
1296106c314cSFugang Duan 		}
1297de40ed31SNimrod Andy 	} else {
1298de40ed31SNimrod Andy 		val = readl(fep->hwp + FEC_ECNTRL);
1299de40ed31SNimrod Andy 		val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1300de40ed31SNimrod Andy 		writel(val, fep->hwp + FEC_ECNTRL);
1301de40ed31SNimrod Andy 	}
1302de40ed31SNimrod Andy 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
13030b6f65c7SJoakim Zhang 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1304793fc096SFrank Li 
1305793fc096SFrank Li 	/* We have to keep ENET enabled to have MII interrupt stay working */
1306de40ed31SNimrod Andy 	if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1307de40ed31SNimrod Andy 		!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
13087b15515fSFrancesco Dolcini 		writel(2, fep->hwp + FEC_ECNTRL);
1309793fc096SFrank Li 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1310793fc096SFrank Li 	}
1311793fc096SFrank Li }
1312793fc096SFrank Li 
1313793fc096SFrank Li 
1314793fc096SFrank Li static void
13150290bd29SMichael S. Tsirkin fec_timeout(struct net_device *ndev, unsigned int txqueue)
1316793fc096SFrank Li {
1317793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1318793fc096SFrank Li 
1319344756f6SRussell King 	fec_dump(ndev);
1320344756f6SRussell King 
1321793fc096SFrank Li 	ndev->stats.tx_errors++;
1322793fc096SFrank Li 
132336cdc743SRussell King 	schedule_work(&fep->tx_timeout_work);
132454309fa6SFrank Li }
132554309fa6SFrank Li 
132636cdc743SRussell King static void fec_enet_timeout_work(struct work_struct *work)
132754309fa6SFrank Li {
132854309fa6SFrank Li 	struct fec_enet_private *fep =
132936cdc743SRussell King 		container_of(work, struct fec_enet_private, tx_timeout_work);
13308ce5624fSRussell King 	struct net_device *ndev = fep->netdev;
133154309fa6SFrank Li 
1332da1774e5SRussell King 	rtnl_lock();
13338ce5624fSRussell King 	if (netif_device_present(ndev) || netif_running(ndev)) {
1334dbc64a8eSRussell King 		napi_disable(&fep->napi);
1335dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
1336ef83337dSRussell King 		fec_restart(ndev);
1337657ade07SRickard x Andersson 		netif_tx_wake_all_queues(ndev);
13386af42d42SRussell King 		netif_tx_unlock_bh(ndev);
1339dbc64a8eSRussell King 		napi_enable(&fep->napi);
13408ce5624fSRussell King 	}
1341da1774e5SRussell King 	rtnl_unlock();
134254309fa6SFrank Li }
1343793fc096SFrank Li 
1344793fc096SFrank Li static void
1345bfd4ecddSRussell King fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1346bfd4ecddSRussell King 	struct skb_shared_hwtstamps *hwtstamps)
1347bfd4ecddSRussell King {
1348bfd4ecddSRussell King 	unsigned long flags;
1349bfd4ecddSRussell King 	u64 ns;
1350bfd4ecddSRussell King 
1351bfd4ecddSRussell King 	spin_lock_irqsave(&fep->tmreg_lock, flags);
1352bfd4ecddSRussell King 	ns = timecounter_cyc2time(&fep->tc, ts);
1353bfd4ecddSRussell King 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1354bfd4ecddSRussell King 
1355bfd4ecddSRussell King 	memset(hwtstamps, 0, sizeof(*hwtstamps));
1356bfd4ecddSRussell King 	hwtstamps->hwtstamp = ns_to_ktime(ns);
1357bfd4ecddSRussell King }
1358bfd4ecddSRussell King 
1359bfd4ecddSRussell King static void
13604d494cdcSFugang Duan fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1361793fc096SFrank Li {
1362793fc096SFrank Li 	struct	fec_enet_private *fep;
1363a2fe37b6SFabio Estevam 	struct bufdesc *bdp;
1364793fc096SFrank Li 	unsigned short status;
1365793fc096SFrank Li 	struct	sk_buff	*skb;
13664d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
13674d494cdcSFugang Duan 	struct netdev_queue *nq;
1368793fc096SFrank Li 	int	index = 0;
136979f33912SNimrod Andy 	int	entries_free;
1370793fc096SFrank Li 
1371793fc096SFrank Li 	fep = netdev_priv(ndev);
13724d494cdcSFugang Duan 
13734d494cdcSFugang Duan 	txq = fep->tx_queue[queue_id];
13744d494cdcSFugang Duan 	/* get next bdp of dirty_tx */
13754d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue_id);
13764d494cdcSFugang Duan 	bdp = txq->dirty_tx;
1377793fc096SFrank Li 
1378793fc096SFrank Li 	/* get next bdp of dirty_tx */
13797355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1380793fc096SFrank Li 
13817355f276STroy Kisky 	while (bdp != READ_ONCE(txq->bd.cur)) {
13827355f276STroy Kisky 		/* Order the load of bd.cur and cbd_sc */
1383c4bc44c6SKevin Hao 		rmb();
13845cfa3039SJohannes Berg 		status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1385c4bc44c6SKevin Hao 		if (status & BD_ENET_TX_READY)
1386793fc096SFrank Li 			break;
1387793fc096SFrank Li 
13887355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
13892b995f63SNimrod Andy 
1390a2fe37b6SFabio Estevam 		skb = txq->tx_skbuff[index];
1391a2fe37b6SFabio Estevam 		txq->tx_skbuff[index] = NULL;
13925cfa3039SJohannes Berg 		if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
13935cfa3039SJohannes Berg 			dma_unmap_single(&fep->pdev->dev,
13945cfa3039SJohannes Berg 					 fec32_to_cpu(bdp->cbd_bufaddr),
13955cfa3039SJohannes Berg 					 fec16_to_cpu(bdp->cbd_datlen),
13965cfa3039SJohannes Berg 					 DMA_TO_DEVICE);
13975cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
13987fafe803STroy Kisky 		if (!skb)
13997fafe803STroy Kisky 			goto skb_done;
1400793fc096SFrank Li 
1401793fc096SFrank Li 		/* Check for errors. */
1402793fc096SFrank Li 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1403793fc096SFrank Li 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
1404793fc096SFrank Li 				   BD_ENET_TX_CSL)) {
1405793fc096SFrank Li 			ndev->stats.tx_errors++;
1406793fc096SFrank Li 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
1407793fc096SFrank Li 				ndev->stats.tx_heartbeat_errors++;
1408793fc096SFrank Li 			if (status & BD_ENET_TX_LC)  /* Late collision */
1409793fc096SFrank Li 				ndev->stats.tx_window_errors++;
1410793fc096SFrank Li 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
1411793fc096SFrank Li 				ndev->stats.tx_aborted_errors++;
1412793fc096SFrank Li 			if (status & BD_ENET_TX_UN)  /* Underrun */
1413793fc096SFrank Li 				ndev->stats.tx_fifo_errors++;
1414793fc096SFrank Li 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
1415793fc096SFrank Li 				ndev->stats.tx_carrier_errors++;
1416793fc096SFrank Li 		} else {
1417793fc096SFrank Li 			ndev->stats.tx_packets++;
14186e909283SNimrod Andy 			ndev->stats.tx_bytes += skb->len;
1419793fc096SFrank Li 		}
1420793fc096SFrank Li 
142134074639SSergey Organov 		/* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who
142234074639SSergey Organov 		 * are to time stamp the packet, so we still need to check time
142334074639SSergey Organov 		 * stamping enabled flag.
142434074639SSergey Organov 		 */
142534074639SSergey Organov 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS &&
142634074639SSergey Organov 			     fep->hwts_tx_en) &&
1427793fc096SFrank Li 		    fep->bufdesc_ex) {
1428793fc096SFrank Li 			struct skb_shared_hwtstamps shhwtstamps;
1429793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1430793fc096SFrank Li 
14315cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1432793fc096SFrank Li 			skb_tstamp_tx(skb, &shhwtstamps);
1433793fc096SFrank Li 		}
1434793fc096SFrank Li 
1435793fc096SFrank Li 		/* Deferred means some collisions occurred during transmit,
1436793fc096SFrank Li 		 * but we eventually sent the packet OK.
1437793fc096SFrank Li 		 */
1438793fc096SFrank Li 		if (status & BD_ENET_TX_DEF)
1439793fc096SFrank Li 			ndev->stats.collisions++;
1440793fc096SFrank Li 
1441793fc096SFrank Li 		/* Free the sk buffer associated with this last transmit */
1442793fc096SFrank Li 		dev_kfree_skb_any(skb);
14437fafe803STroy Kisky skb_done:
1444c4bc44c6SKevin Hao 		/* Make sure the update to bdp and tx_skbuff are performed
1445c4bc44c6SKevin Hao 		 * before dirty_tx
1446c4bc44c6SKevin Hao 		 */
1447c4bc44c6SKevin Hao 		wmb();
14484d494cdcSFugang Duan 		txq->dirty_tx = bdp;
1449793fc096SFrank Li 
1450793fc096SFrank Li 		/* Update pointer to next buffer descriptor to be transmitted */
14517355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1452793fc096SFrank Li 
1453793fc096SFrank Li 		/* Since we have freed up a buffer, the ring is no longer full
1454793fc096SFrank Li 		 */
1455657ade07SRickard x Andersson 		if (netif_tx_queue_stopped(nq)) {
14567355f276STroy Kisky 			entries_free = fec_enet_get_free_txdesc_num(txq);
14574d494cdcSFugang Duan 			if (entries_free >= txq->tx_wake_threshold)
14584d494cdcSFugang Duan 				netif_tx_wake_queue(nq);
1459793fc096SFrank Li 		}
146079f33912SNimrod Andy 	}
1461ccea2968SRussell King 
1462c10bc0e7SFugang Duan 	/* ERR006358: Keep the transmitter going */
14637355f276STroy Kisky 	if (bdp != txq->bd.cur &&
146453bb20d1STroy Kisky 	    readl(txq->bd.reg_desc_active) == 0)
146553bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
14664d494cdcSFugang Duan }
14674d494cdcSFugang Duan 
14687cdaa4ccSTobias Waldekranz static void fec_enet_tx(struct net_device *ndev)
14694d494cdcSFugang Duan {
14704d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
14717cdaa4ccSTobias Waldekranz 	int i;
14727cdaa4ccSTobias Waldekranz 
14737cdaa4ccSTobias Waldekranz 	/* Make sure that AVB queues are processed first. */
14747cdaa4ccSTobias Waldekranz 	for (i = fep->num_tx_queues - 1; i >= 0; i--)
14757cdaa4ccSTobias Waldekranz 		fec_enet_tx_queue(ndev, i);
1476793fc096SFrank Li }
1477793fc096SFrank Li 
147895698ff6SShenwei Wang static void fec_enet_update_cbd(struct fec_enet_priv_rx_q *rxq,
147995698ff6SShenwei Wang 				struct bufdesc *bdp, int index)
148095698ff6SShenwei Wang {
148195698ff6SShenwei Wang 	struct page *new_page;
148295698ff6SShenwei Wang 	dma_addr_t phys_addr;
148395698ff6SShenwei Wang 
148495698ff6SShenwei Wang 	new_page = page_pool_dev_alloc_pages(rxq->page_pool);
148595698ff6SShenwei Wang 	WARN_ON(!new_page);
148695698ff6SShenwei Wang 	rxq->rx_skb_info[index].page = new_page;
148795698ff6SShenwei Wang 
148895698ff6SShenwei Wang 	rxq->rx_skb_info[index].offset = FEC_ENET_XDP_HEADROOM;
148995698ff6SShenwei Wang 	phys_addr = page_pool_get_dma_addr(new_page) + FEC_ENET_XDP_HEADROOM;
149095698ff6SShenwei Wang 	bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
149195698ff6SShenwei Wang }
149295698ff6SShenwei Wang 
14936d6b39f1SShenwei Wang static u32
14946d6b39f1SShenwei Wang fec_enet_run_xdp(struct fec_enet_private *fep, struct bpf_prog *prog,
14956d6b39f1SShenwei Wang 		 struct xdp_buff *xdp, struct fec_enet_priv_rx_q *rxq, int index)
14966d6b39f1SShenwei Wang {
14976d6b39f1SShenwei Wang 	unsigned int sync, len = xdp->data_end - xdp->data;
14986d6b39f1SShenwei Wang 	u32 ret = FEC_ENET_XDP_PASS;
14996d6b39f1SShenwei Wang 	struct page *page;
15006d6b39f1SShenwei Wang 	int err;
15016d6b39f1SShenwei Wang 	u32 act;
15026d6b39f1SShenwei Wang 
15036d6b39f1SShenwei Wang 	act = bpf_prog_run_xdp(prog, xdp);
15046d6b39f1SShenwei Wang 
15056d6b39f1SShenwei Wang 	/* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
15066d6b39f1SShenwei Wang 	sync = xdp->data_end - xdp->data_hard_start - FEC_ENET_XDP_HEADROOM;
15076d6b39f1SShenwei Wang 	sync = max(sync, len);
15086d6b39f1SShenwei Wang 
15096d6b39f1SShenwei Wang 	switch (act) {
15106d6b39f1SShenwei Wang 	case XDP_PASS:
15116970ef27SShenwei Wang 		rxq->stats[RX_XDP_PASS]++;
15126d6b39f1SShenwei Wang 		ret = FEC_ENET_XDP_PASS;
15136d6b39f1SShenwei Wang 		break;
15146d6b39f1SShenwei Wang 
15156d6b39f1SShenwei Wang 	case XDP_REDIRECT:
15166970ef27SShenwei Wang 		rxq->stats[RX_XDP_REDIRECT]++;
15176d6b39f1SShenwei Wang 		err = xdp_do_redirect(fep->netdev, xdp, prog);
15186d6b39f1SShenwei Wang 		if (!err) {
15196d6b39f1SShenwei Wang 			ret = FEC_ENET_XDP_REDIR;
15206d6b39f1SShenwei Wang 		} else {
15216d6b39f1SShenwei Wang 			ret = FEC_ENET_XDP_CONSUMED;
15226d6b39f1SShenwei Wang 			page = virt_to_head_page(xdp->data);
15236d6b39f1SShenwei Wang 			page_pool_put_page(rxq->page_pool, page, sync, true);
15246d6b39f1SShenwei Wang 		}
15256d6b39f1SShenwei Wang 		break;
15266d6b39f1SShenwei Wang 
15276d6b39f1SShenwei Wang 	default:
15286d6b39f1SShenwei Wang 		bpf_warn_invalid_xdp_action(fep->netdev, prog, act);
15296d6b39f1SShenwei Wang 		fallthrough;
15306d6b39f1SShenwei Wang 
15316d6b39f1SShenwei Wang 	case XDP_TX:
15326d6b39f1SShenwei Wang 		bpf_warn_invalid_xdp_action(fep->netdev, prog, act);
15336d6b39f1SShenwei Wang 		fallthrough;
15346d6b39f1SShenwei Wang 
15356d6b39f1SShenwei Wang 	case XDP_ABORTED:
15366d6b39f1SShenwei Wang 		fallthrough;    /* handle aborts by dropping packet */
15376d6b39f1SShenwei Wang 
15386d6b39f1SShenwei Wang 	case XDP_DROP:
15396970ef27SShenwei Wang 		rxq->stats[RX_XDP_DROP]++;
15406d6b39f1SShenwei Wang 		ret = FEC_ENET_XDP_CONSUMED;
15416d6b39f1SShenwei Wang 		page = virt_to_head_page(xdp->data);
15426d6b39f1SShenwei Wang 		page_pool_put_page(rxq->page_pool, page, sync, true);
15436d6b39f1SShenwei Wang 		break;
15446d6b39f1SShenwei Wang 	}
15456d6b39f1SShenwei Wang 
15466d6b39f1SShenwei Wang 	return ret;
15476d6b39f1SShenwei Wang }
15486d6b39f1SShenwei Wang 
15497355f276STroy Kisky /* During a receive, the bd_rx.cur points to the current incoming buffer.
1550793fc096SFrank Li  * When we update through the ring, if the next incoming buffer has
1551793fc096SFrank Li  * not been given to the system, we just set the empty indicator,
1552793fc096SFrank Li  * effectively tossing the packet.
1553793fc096SFrank Li  */
1554793fc096SFrank Li static int
15554d494cdcSFugang Duan fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1556793fc096SFrank Li {
1557793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
15584d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
1559793fc096SFrank Li 	struct bufdesc *bdp;
1560793fc096SFrank Li 	unsigned short status;
1561793fc096SFrank Li 	struct  sk_buff *skb;
1562793fc096SFrank Li 	ushort	pkt_len;
1563793fc096SFrank Li 	__u8 *data;
1564793fc096SFrank Li 	int	pkt_received = 0;
1565cdffcf1bSJim Baxter 	struct	bufdesc_ex *ebdp = NULL;
1566cdffcf1bSJim Baxter 	bool	vlan_packet_rcvd = false;
1567cdffcf1bSJim Baxter 	u16	vlan_tag;
1568d842a31fSDuan Fugang-B38611 	int	index = 0;
15696b7e4008SLothar Waßmann 	bool	need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
15706d6b39f1SShenwei Wang 	struct bpf_prog *xdp_prog = READ_ONCE(fep->xdp_prog);
15716d6b39f1SShenwei Wang 	u32 ret, xdp_result = FEC_ENET_XDP_PASS;
15726c8fae0cSShenwei Wang 	u32 data_start = FEC_ENET_XDP_HEADROOM;
15736d6b39f1SShenwei Wang 	struct xdp_buff xdp;
157495698ff6SShenwei Wang 	struct page *page;
15756c8fae0cSShenwei Wang 	u32 sub_len = 4;
15766c8fae0cSShenwei Wang 
15776c8fae0cSShenwei Wang #if !defined(CONFIG_M5272)
15786c8fae0cSShenwei Wang 	/*If it has the FEC_QUIRK_HAS_RACC quirk property, the bit of
15796c8fae0cSShenwei Wang 	 * FEC_RACC_SHIFT16 is set by default in the probe function.
15806c8fae0cSShenwei Wang 	 */
15816c8fae0cSShenwei Wang 	if (fep->quirks & FEC_QUIRK_HAS_RACC) {
15826c8fae0cSShenwei Wang 		data_start += 2;
15836c8fae0cSShenwei Wang 		sub_len += 2;
15846c8fae0cSShenwei Wang 	}
15856c8fae0cSShenwei Wang #endif
1586793fc096SFrank Li 
1587793fc096SFrank Li #ifdef CONFIG_M532x
1588793fc096SFrank Li 	flush_cache_all();
1589793fc096SFrank Li #endif
15904d494cdcSFugang Duan 	rxq = fep->rx_queue[queue_id];
1591793fc096SFrank Li 
1592793fc096SFrank Li 	/* First, grab all of the stats for the incoming packet.
1593793fc096SFrank Li 	 * These get messed up if we get called due to a busy condition.
1594793fc096SFrank Li 	 */
15957355f276STroy Kisky 	bdp = rxq->bd.cur;
15966d6b39f1SShenwei Wang 	xdp_init_buff(&xdp, PAGE_SIZE, &rxq->xdp_rxq);
1597793fc096SFrank Li 
15985cfa3039SJohannes Berg 	while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1599793fc096SFrank Li 
1600793fc096SFrank Li 		if (pkt_received >= budget)
1601793fc096SFrank Li 			break;
1602793fc096SFrank Li 		pkt_received++;
1603793fc096SFrank Li 
1604b5bd95d1SJoakim Zhang 		writel(FEC_ENET_RXF_GET(queue_id), fep->hwp + FEC_IEVENT);
1605db3421c1SRussell King 
1606793fc096SFrank Li 		/* Check for errors. */
1607095098e1STroy Kisky 		status ^= BD_ENET_RX_LAST;
1608793fc096SFrank Li 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1609095098e1STroy Kisky 			   BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1610095098e1STroy Kisky 			   BD_ENET_RX_CL)) {
1611793fc096SFrank Li 			ndev->stats.rx_errors++;
1612095098e1STroy Kisky 			if (status & BD_ENET_RX_OV) {
1613095098e1STroy Kisky 				/* FIFO overrun */
1614095098e1STroy Kisky 				ndev->stats.rx_fifo_errors++;
1615095098e1STroy Kisky 				goto rx_processing_done;
1616095098e1STroy Kisky 			}
1617095098e1STroy Kisky 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1618095098e1STroy Kisky 						| BD_ENET_RX_LAST)) {
1619793fc096SFrank Li 				/* Frame too long or too short. */
1620793fc096SFrank Li 				ndev->stats.rx_length_errors++;
1621095098e1STroy Kisky 				if (status & BD_ENET_RX_LAST)
1622095098e1STroy Kisky 					netdev_err(ndev, "rcv is not +last\n");
1623793fc096SFrank Li 			}
1624793fc096SFrank Li 			if (status & BD_ENET_RX_CR)	/* CRC Error */
1625793fc096SFrank Li 				ndev->stats.rx_crc_errors++;
1626095098e1STroy Kisky 			/* Report late collisions as a frame error. */
1627095098e1STroy Kisky 			if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1628793fc096SFrank Li 				ndev->stats.rx_frame_errors++;
1629793fc096SFrank Li 			goto rx_processing_done;
1630793fc096SFrank Li 		}
1631793fc096SFrank Li 
1632793fc096SFrank Li 		/* Process the incoming frame. */
1633793fc096SFrank Li 		ndev->stats.rx_packets++;
16345cfa3039SJohannes Berg 		pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1635793fc096SFrank Li 		ndev->stats.rx_bytes += pkt_len;
1636793fc096SFrank Li 
16377355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &rxq->bd);
163895698ff6SShenwei Wang 		page = rxq->rx_skb_info[index].page;
163995698ff6SShenwei Wang 		dma_sync_single_for_cpu(&fep->pdev->dev,
164095698ff6SShenwei Wang 					fec32_to_cpu(bdp->cbd_bufaddr),
164195698ff6SShenwei Wang 					pkt_len,
164295698ff6SShenwei Wang 					DMA_FROM_DEVICE);
164395698ff6SShenwei Wang 		prefetch(page_address(page));
164495698ff6SShenwei Wang 		fec_enet_update_cbd(rxq, bdp, index);
16451b7bde6dSNimrod Andy 
16466d6b39f1SShenwei Wang 		if (xdp_prog) {
16476d6b39f1SShenwei Wang 			xdp_buff_clear_frags_flag(&xdp);
16486c8fae0cSShenwei Wang 			/* subtract 16bit shift and FCS */
16496d6b39f1SShenwei Wang 			xdp_prepare_buff(&xdp, page_address(page),
16506c8fae0cSShenwei Wang 					 data_start, pkt_len - sub_len, false);
16516d6b39f1SShenwei Wang 			ret = fec_enet_run_xdp(fep, xdp_prog, &xdp, rxq, index);
16526d6b39f1SShenwei Wang 			xdp_result |= ret;
16536d6b39f1SShenwei Wang 			if (ret != FEC_ENET_XDP_PASS)
16546d6b39f1SShenwei Wang 				goto rx_processing_done;
16556d6b39f1SShenwei Wang 		}
16566d6b39f1SShenwei Wang 
16571b7bde6dSNimrod Andy 		/* The packet length includes FCS, but we don't want to
16581b7bde6dSNimrod Andy 		 * include that when passing upstream as it messes up
16591b7bde6dSNimrod Andy 		 * bridging applications.
16601b7bde6dSNimrod Andy 		 */
166195698ff6SShenwei Wang 		skb = build_skb(page_address(page), PAGE_SIZE);
166219e72b06SWei Fang 		if (unlikely(!skb)) {
166319e72b06SWei Fang 			page_pool_recycle_direct(rxq->page_pool, page);
166419e72b06SWei Fang 			ndev->stats.rx_dropped++;
166519e72b06SWei Fang 
166619e72b06SWei Fang 			netdev_err_once(ndev, "build_skb failed!\n");
166719e72b06SWei Fang 			goto rx_processing_done;
166819e72b06SWei Fang 		}
166919e72b06SWei Fang 
16706c8fae0cSShenwei Wang 		skb_reserve(skb, data_start);
16716c8fae0cSShenwei Wang 		skb_put(skb, pkt_len - sub_len);
167295698ff6SShenwei Wang 		skb_mark_for_recycle(skb);
16733ac72b7bSEric Nelson 
16746c8fae0cSShenwei Wang 		if (unlikely(need_swap)) {
16756c8fae0cSShenwei Wang 			data = page_address(page) + FEC_ENET_XDP_HEADROOM;
1676235bde1eSFabio Estevam 			swap_buffer(data, pkt_len);
16776c8fae0cSShenwei Wang 		}
16786c8fae0cSShenwei Wang 		data = skb->data;
16793ac72b7bSEric Nelson 
1680cdffcf1bSJim Baxter 		/* Extract the enhanced buffer descriptor */
1681cdffcf1bSJim Baxter 		ebdp = NULL;
1682cdffcf1bSJim Baxter 		if (fep->bufdesc_ex)
1683cdffcf1bSJim Baxter 			ebdp = (struct bufdesc_ex *)bdp;
1684cdffcf1bSJim Baxter 
1685cdffcf1bSJim Baxter 		/* If this is a VLAN packet remove the VLAN Tag */
1686cdffcf1bSJim Baxter 		vlan_packet_rcvd = false;
1687cdffcf1bSJim Baxter 		if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
16885cfa3039SJohannes Berg 		    fep->bufdesc_ex &&
16895cfa3039SJohannes Berg 		    (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1690cdffcf1bSJim Baxter 			/* Push and remove the vlan tag */
1691cdffcf1bSJim Baxter 			struct vlan_hdr *vlan_header =
1692cdffcf1bSJim Baxter 					(struct vlan_hdr *) (data + ETH_HLEN);
1693cdffcf1bSJim Baxter 			vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1694cdffcf1bSJim Baxter 
1695cdffcf1bSJim Baxter 			vlan_packet_rcvd = true;
16961b7bde6dSNimrod Andy 
1697af5cbc98SNimrod Andy 			memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
16981b7bde6dSNimrod Andy 			skb_pull(skb, VLAN_HLEN);
1699cdffcf1bSJim Baxter 		}
1700cdffcf1bSJim Baxter 
1701793fc096SFrank Li 		skb->protocol = eth_type_trans(skb, ndev);
1702793fc096SFrank Li 
1703793fc096SFrank Li 		/* Get receive timestamp from the skb */
1704bfd4ecddSRussell King 		if (fep->hwts_rx_en && fep->bufdesc_ex)
17055cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1706bfd4ecddSRussell King 					  skb_hwtstamps(skb));
1707793fc096SFrank Li 
17084c09eed9SJim Baxter 		if (fep->bufdesc_ex &&
17094c09eed9SJim Baxter 		    (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
17105cfa3039SJohannes Berg 			if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
17114c09eed9SJim Baxter 				/* don't check it */
17124c09eed9SJim Baxter 				skb->ip_summed = CHECKSUM_UNNECESSARY;
17134c09eed9SJim Baxter 			} else {
17144c09eed9SJim Baxter 				skb_checksum_none_assert(skb);
17154c09eed9SJim Baxter 			}
17164c09eed9SJim Baxter 		}
17174c09eed9SJim Baxter 
1718cdffcf1bSJim Baxter 		/* Handle received VLAN packets */
1719cdffcf1bSJim Baxter 		if (vlan_packet_rcvd)
1720cdffcf1bSJim Baxter 			__vlan_hwaccel_put_tag(skb,
1721cdffcf1bSJim Baxter 					       htons(ETH_P_8021Q),
1722cdffcf1bSJim Baxter 					       vlan_tag);
1723cdffcf1bSJim Baxter 
17247cdaa4ccSTobias Waldekranz 		skb_record_rx_queue(skb, queue_id);
1725793fc096SFrank Li 		napi_gro_receive(&fep->napi, skb);
1726793fc096SFrank Li 
1727793fc096SFrank Li rx_processing_done:
1728793fc096SFrank Li 		/* Clear the status flags for this buffer */
1729793fc096SFrank Li 		status &= ~BD_ENET_RX_STATS;
1730793fc096SFrank Li 
1731793fc096SFrank Li 		/* Mark the buffer empty */
1732793fc096SFrank Li 		status |= BD_ENET_RX_EMPTY;
1733793fc096SFrank Li 
1734793fc096SFrank Li 		if (fep->bufdesc_ex) {
1735793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1736793fc096SFrank Li 
17375cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1738793fc096SFrank Li 			ebdp->cbd_prot = 0;
1739793fc096SFrank Li 			ebdp->cbd_bdu = 0;
1740793fc096SFrank Li 		}
1741be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
1742be293467STroy Kisky 		 * performed before transferring ownership.
1743be293467STroy Kisky 		 */
1744be293467STroy Kisky 		wmb();
1745be293467STroy Kisky 		bdp->cbd_sc = cpu_to_fec16(status);
1746793fc096SFrank Li 
1747793fc096SFrank Li 		/* Update BD pointer to next entry */
17487355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
174936e24e2eSDuan Fugang-B38611 
1750793fc096SFrank Li 		/* Doing this here will keep the FEC running while we process
1751793fc096SFrank Li 		 * incoming frames.  On a heavily loaded network, we should be
1752793fc096SFrank Li 		 * able to keep up at the expense of system resources.
1753793fc096SFrank Li 		 */
175453bb20d1STroy Kisky 		writel(0, rxq->bd.reg_desc_active);
1755793fc096SFrank Li 	}
17567355f276STroy Kisky 	rxq->bd.cur = bdp;
17576d6b39f1SShenwei Wang 
17586d6b39f1SShenwei Wang 	if (xdp_result & FEC_ENET_XDP_REDIR)
17596d6b39f1SShenwei Wang 		xdp_do_flush_map();
17606d6b39f1SShenwei Wang 
1761793fc096SFrank Li 	return pkt_received;
1762793fc096SFrank Li }
1763793fc096SFrank Li 
17647cdaa4ccSTobias Waldekranz static int fec_enet_rx(struct net_device *ndev, int budget)
17654d494cdcSFugang Duan {
17664d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
17677cdaa4ccSTobias Waldekranz 	int i, done = 0;
17684d494cdcSFugang Duan 
17697cdaa4ccSTobias Waldekranz 	/* Make sure that AVB queues are processed first. */
17707cdaa4ccSTobias Waldekranz 	for (i = fep->num_rx_queues - 1; i >= 0; i--)
17717cdaa4ccSTobias Waldekranz 		done += fec_enet_rx_queue(ndev, budget - done, i);
17721c021bb7SUwe Kleine-König 
17737cdaa4ccSTobias Waldekranz 	return done;
17744d494cdcSFugang Duan }
17754d494cdcSFugang Duan 
17767cdaa4ccSTobias Waldekranz static bool fec_enet_collect_events(struct fec_enet_private *fep)
17774d494cdcSFugang Duan {
1778793fc096SFrank Li 	uint int_events;
1779793fc096SFrank Li 
1780793fc096SFrank Li 	int_events = readl(fep->hwp + FEC_IEVENT);
1781f166f890SAndrew Lunn 
1782f166f890SAndrew Lunn 	/* Don't clear MDIO events, we poll for those */
1783f166f890SAndrew Lunn 	int_events &= ~FEC_ENET_MII;
1784f166f890SAndrew Lunn 
178594191fd6SNimrod Andy 	writel(int_events, fep->hwp + FEC_IEVENT);
1786793fc096SFrank Li 
17877cdaa4ccSTobias Waldekranz 	return int_events != 0;
17887cdaa4ccSTobias Waldekranz }
17897cdaa4ccSTobias Waldekranz 
17907cdaa4ccSTobias Waldekranz static irqreturn_t
17917cdaa4ccSTobias Waldekranz fec_enet_interrupt(int irq, void *dev_id)
17927cdaa4ccSTobias Waldekranz {
17937cdaa4ccSTobias Waldekranz 	struct net_device *ndev = dev_id;
17947cdaa4ccSTobias Waldekranz 	struct fec_enet_private *fep = netdev_priv(ndev);
17957cdaa4ccSTobias Waldekranz 	irqreturn_t ret = IRQ_NONE;
17967cdaa4ccSTobias Waldekranz 
17977cdaa4ccSTobias Waldekranz 	if (fec_enet_collect_events(fep) && fep->link) {
1798793fc096SFrank Li 		ret = IRQ_HANDLED;
1799793fc096SFrank Li 
180094191fd6SNimrod Andy 		if (napi_schedule_prep(&fep->napi)) {
1801f166f890SAndrew Lunn 			/* Disable interrupts */
1802f166f890SAndrew Lunn 			writel(0, fep->hwp + FEC_IMASK);
180394191fd6SNimrod Andy 			__napi_schedule(&fep->napi);
180494191fd6SNimrod Andy 		}
1805793fc096SFrank Li 	}
1806793fc096SFrank Li 
1807793fc096SFrank Li 	return ret;
1808793fc096SFrank Li }
1809793fc096SFrank Li 
1810793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1811793fc096SFrank Li {
1812793fc096SFrank Li 	struct net_device *ndev = napi->dev;
1813793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
18147cdaa4ccSTobias Waldekranz 	int done = 0;
18157a16807cSRussell King 
18167cdaa4ccSTobias Waldekranz 	do {
18177cdaa4ccSTobias Waldekranz 		done += fec_enet_rx(ndev, budget - done);
1818793fc096SFrank Li 		fec_enet_tx(ndev);
18197cdaa4ccSTobias Waldekranz 	} while ((done < budget) && fec_enet_collect_events(fep));
1820793fc096SFrank Li 
18217cdaa4ccSTobias Waldekranz 	if (done < budget) {
18227cdaa4ccSTobias Waldekranz 		napi_complete_done(napi, done);
1823793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1824793fc096SFrank Li 	}
18257cdaa4ccSTobias Waldekranz 
18267cdaa4ccSTobias Waldekranz 	return done;
1827793fc096SFrank Li }
1828793fc096SFrank Li 
1829793fc096SFrank Li /* ------------------------------------------------------------------------- */
1830052fcc45SFugang Duan static int fec_get_mac(struct net_device *ndev)
1831793fc096SFrank Li {
1832793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1833793fc096SFrank Li 	unsigned char *iap, tmpaddr[ETH_ALEN];
183483216e39SMichael Walle 	int ret;
1835793fc096SFrank Li 
1836793fc096SFrank Li 	/*
1837793fc096SFrank Li 	 * try to get mac address in following order:
1838793fc096SFrank Li 	 *
1839793fc096SFrank Li 	 * 1) module parameter via kernel command line in form
1840793fc096SFrank Li 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1841793fc096SFrank Li 	 */
1842793fc096SFrank Li 	iap = macaddr;
1843793fc096SFrank Li 
1844793fc096SFrank Li 	/*
1845793fc096SFrank Li 	 * 2) from device tree data
1846793fc096SFrank Li 	 */
1847793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1848793fc096SFrank Li 		struct device_node *np = fep->pdev->dev.of_node;
1849793fc096SFrank Li 		if (np) {
185083216e39SMichael Walle 			ret = of_get_mac_address(np, tmpaddr);
185183216e39SMichael Walle 			if (!ret)
185283216e39SMichael Walle 				iap = tmpaddr;
1853052fcc45SFugang Duan 			else if (ret == -EPROBE_DEFER)
1854052fcc45SFugang Duan 				return ret;
1855793fc096SFrank Li 		}
1856793fc096SFrank Li 	}
1857793fc096SFrank Li 
1858793fc096SFrank Li 	/*
1859793fc096SFrank Li 	 * 3) from flash or fuse (via platform data)
1860793fc096SFrank Li 	 */
1861793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1862793fc096SFrank Li #ifdef CONFIG_M5272
1863793fc096SFrank Li 		if (FEC_FLASHMAC)
1864793fc096SFrank Li 			iap = (unsigned char *)FEC_FLASHMAC;
1865793fc096SFrank Li #else
186632d1bbb1SGeert Uytterhoeven 		struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
186732d1bbb1SGeert Uytterhoeven 
1868793fc096SFrank Li 		if (pdata)
1869793fc096SFrank Li 			iap = (unsigned char *)&pdata->mac;
1870793fc096SFrank Li #endif
1871793fc096SFrank Li 	}
1872793fc096SFrank Li 
1873793fc096SFrank Li 	/*
1874793fc096SFrank Li 	 * 4) FEC mac registers set by bootloader
1875793fc096SFrank Li 	 */
1876793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
18777d7628f3SDan Carpenter 		*((__be32 *) &tmpaddr[0]) =
18787d7628f3SDan Carpenter 			cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
18797d7628f3SDan Carpenter 		*((__be16 *) &tmpaddr[4]) =
18807d7628f3SDan Carpenter 			cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1881793fc096SFrank Li 		iap = &tmpaddr[0];
1882793fc096SFrank Li 	}
1883793fc096SFrank Li 
1884ff5b2fabSLucas Stach 	/*
1885ff5b2fabSLucas Stach 	 * 5) random mac address
1886ff5b2fabSLucas Stach 	 */
1887ff5b2fabSLucas Stach 	if (!is_valid_ether_addr(iap)) {
1888ff5b2fabSLucas Stach 		/* Report it and use a random ethernet address instead */
1889a19a0582SFabio Estevam 		dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
1890ff5b2fabSLucas Stach 		eth_hw_addr_random(ndev);
1891a19a0582SFabio Estevam 		dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
1892ff5b2fabSLucas Stach 			 ndev->dev_addr);
1893052fcc45SFugang Duan 		return 0;
1894ff5b2fabSLucas Stach 	}
1895ff5b2fabSLucas Stach 
1896793fc096SFrank Li 	/* Adjust MAC if using macaddr */
1897ba3fdfe3SJakub Kicinski 	eth_hw_addr_gen(ndev, iap, iap == macaddr ? fep->dev_id : 0);
1898052fcc45SFugang Duan 
1899052fcc45SFugang Duan 	return 0;
1900793fc096SFrank Li }
1901793fc096SFrank Li 
1902793fc096SFrank Li /* ------------------------------------------------------------------------- */
1903793fc096SFrank Li 
1904793fc096SFrank Li /*
1905793fc096SFrank Li  * Phy section
1906793fc096SFrank Li  */
1907793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev)
1908793fc096SFrank Li {
1909793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
191045f5c327SPhilippe Reynes 	struct phy_device *phy_dev = ndev->phydev;
1911793fc096SFrank Li 	int status_change = 0;
1912793fc096SFrank Li 
19138ce5624fSRussell King 	/*
19148ce5624fSRussell King 	 * If the netdev is down, or is going down, we're not interested
19158ce5624fSRussell King 	 * in link state events, so just mark our idea of the link as down
19168ce5624fSRussell King 	 * and ignore the event.
19178ce5624fSRussell King 	 */
19188ce5624fSRussell King 	if (!netif_running(ndev) || !netif_device_present(ndev)) {
19198ce5624fSRussell King 		fep->link = 0;
19208ce5624fSRussell King 	} else if (phy_dev->link) {
1921793fc096SFrank Li 		if (!fep->link) {
1922793fc096SFrank Li 			fep->link = phy_dev->link;
1923793fc096SFrank Li 			status_change = 1;
1924793fc096SFrank Li 		}
1925793fc096SFrank Li 
1926ef83337dSRussell King 		if (fep->full_duplex != phy_dev->duplex) {
1927ef83337dSRussell King 			fep->full_duplex = phy_dev->duplex;
1928793fc096SFrank Li 			status_change = 1;
1929ef83337dSRussell King 		}
1930793fc096SFrank Li 
1931793fc096SFrank Li 		if (phy_dev->speed != fep->speed) {
1932793fc096SFrank Li 			fep->speed = phy_dev->speed;
1933793fc096SFrank Li 			status_change = 1;
1934793fc096SFrank Li 		}
1935793fc096SFrank Li 
1936793fc096SFrank Li 		/* if any of the above changed restart the FEC */
1937dbc64a8eSRussell King 		if (status_change) {
1938dbc64a8eSRussell King 			napi_disable(&fep->napi);
1939dbc64a8eSRussell King 			netif_tx_lock_bh(ndev);
1940ef83337dSRussell King 			fec_restart(ndev);
1941657ade07SRickard x Andersson 			netif_tx_wake_all_queues(ndev);
19426af42d42SRussell King 			netif_tx_unlock_bh(ndev);
1943dbc64a8eSRussell King 			napi_enable(&fep->napi);
1944dbc64a8eSRussell King 		}
1945793fc096SFrank Li 	} else {
1946793fc096SFrank Li 		if (fep->link) {
1947f208ce10SRussell King 			napi_disable(&fep->napi);
1948f208ce10SRussell King 			netif_tx_lock_bh(ndev);
1949793fc096SFrank Li 			fec_stop(ndev);
1950f208ce10SRussell King 			netif_tx_unlock_bh(ndev);
1951f208ce10SRussell King 			napi_enable(&fep->napi);
19526e0895c2SDavid S. Miller 			fep->link = phy_dev->link;
1953793fc096SFrank Li 			status_change = 1;
1954793fc096SFrank Li 		}
1955793fc096SFrank Li 	}
1956793fc096SFrank Li 
1957793fc096SFrank Li 	if (status_change)
1958793fc096SFrank Li 		phy_print_status(phy_dev);
1959793fc096SFrank Li }
1960793fc096SFrank Li 
1961f166f890SAndrew Lunn static int fec_enet_mdio_wait(struct fec_enet_private *fep)
1962f166f890SAndrew Lunn {
1963f166f890SAndrew Lunn 	uint ievent;
1964f166f890SAndrew Lunn 	int ret;
1965f166f890SAndrew Lunn 
1966f166f890SAndrew Lunn 	ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent,
1967f166f890SAndrew Lunn 					ievent & FEC_ENET_MII, 2, 30000);
1968f166f890SAndrew Lunn 
1969f166f890SAndrew Lunn 	if (!ret)
1970f166f890SAndrew Lunn 		writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
1971f166f890SAndrew Lunn 
1972f166f890SAndrew Lunn 	return ret;
1973f166f890SAndrew Lunn }
1974f166f890SAndrew Lunn 
19758d03ad1aSAndrew Lunn static int fec_enet_mdio_read_c22(struct mii_bus *bus, int mii_id, int regnum)
1976793fc096SFrank Li {
1977793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
19788fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
1979d3ee8ec7SMarco Hartmann 	int ret = 0, frame_start, frame_addr, frame_op;
19808fff755eSAndrew Lunn 
1981da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
1982b0c6ce24SFabio Estevam 	if (ret < 0)
19838fff755eSAndrew Lunn 		return ret;
1984793fc096SFrank Li 
1985d3ee8ec7SMarco Hartmann 	/* C22 read */
1986d3ee8ec7SMarco Hartmann 	frame_op = FEC_MMFR_OP_READ;
1987d3ee8ec7SMarco Hartmann 	frame_start = FEC_MMFR_ST;
1988d3ee8ec7SMarco Hartmann 	frame_addr = regnum;
1989d3ee8ec7SMarco Hartmann 
1990793fc096SFrank Li 	/* start a read op */
1991d3ee8ec7SMarco Hartmann 	writel(frame_start | frame_op |
1992d3ee8ec7SMarco Hartmann 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1993793fc096SFrank Li 	       FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1994793fc096SFrank Li 
1995793fc096SFrank Li 	/* wait for end of transfer */
1996f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
1997f166f890SAndrew Lunn 	if (ret) {
199831b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO read timeout\n");
19998fff755eSAndrew Lunn 		goto out;
2000793fc096SFrank Li 	}
2001793fc096SFrank Li 
20028fff755eSAndrew Lunn 	ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
20038fff755eSAndrew Lunn 
20048fff755eSAndrew Lunn out:
20058fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
20068fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
20078fff755eSAndrew Lunn 
20088fff755eSAndrew Lunn 	return ret;
2009793fc096SFrank Li }
2010793fc096SFrank Li 
20118d03ad1aSAndrew Lunn static int fec_enet_mdio_read_c45(struct mii_bus *bus, int mii_id,
20128d03ad1aSAndrew Lunn 				  int devad, int regnum)
2013793fc096SFrank Li {
2014793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
20158fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
20168d03ad1aSAndrew Lunn 	int ret = 0, frame_start, frame_op;
20178fff755eSAndrew Lunn 
2018da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
2019b0c6ce24SFabio Estevam 	if (ret < 0)
20208fff755eSAndrew Lunn 		return ret;
2021793fc096SFrank Li 
2022d3ee8ec7SMarco Hartmann 	frame_start = FEC_MMFR_ST_C45;
2023d3ee8ec7SMarco Hartmann 
2024d3ee8ec7SMarco Hartmann 	/* write address */
2025d3ee8ec7SMarco Hartmann 	writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
20268d03ad1aSAndrew Lunn 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
2027d3ee8ec7SMarco Hartmann 	       FEC_MMFR_TA | (regnum & 0xFFFF),
2028d3ee8ec7SMarco Hartmann 	       fep->hwp + FEC_MII_DATA);
2029d3ee8ec7SMarco Hartmann 
2030d3ee8ec7SMarco Hartmann 	/* wait for end of transfer */
2031f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
2032f166f890SAndrew Lunn 	if (ret) {
2033d3ee8ec7SMarco Hartmann 		netdev_err(fep->netdev, "MDIO address write timeout\n");
2034d3ee8ec7SMarco Hartmann 		goto out;
2035d3ee8ec7SMarco Hartmann 	}
20368d03ad1aSAndrew Lunn 
20378d03ad1aSAndrew Lunn 	frame_op = FEC_MMFR_OP_READ_C45;
20388d03ad1aSAndrew Lunn 
20398d03ad1aSAndrew Lunn 	/* start a read op */
20408d03ad1aSAndrew Lunn 	writel(frame_start | frame_op |
20418d03ad1aSAndrew Lunn 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
20428d03ad1aSAndrew Lunn 	       FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
20438d03ad1aSAndrew Lunn 
20448d03ad1aSAndrew Lunn 	/* wait for end of transfer */
20458d03ad1aSAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
20468d03ad1aSAndrew Lunn 	if (ret) {
20478d03ad1aSAndrew Lunn 		netdev_err(fep->netdev, "MDIO read timeout\n");
20488d03ad1aSAndrew Lunn 		goto out;
20498d03ad1aSAndrew Lunn 	}
20508d03ad1aSAndrew Lunn 
20518d03ad1aSAndrew Lunn 	ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
20528d03ad1aSAndrew Lunn 
20538d03ad1aSAndrew Lunn out:
20548d03ad1aSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
20558d03ad1aSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
20568d03ad1aSAndrew Lunn 
20578d03ad1aSAndrew Lunn 	return ret;
20588d03ad1aSAndrew Lunn }
20598d03ad1aSAndrew Lunn 
20608d03ad1aSAndrew Lunn static int fec_enet_mdio_write_c22(struct mii_bus *bus, int mii_id, int regnum,
20618d03ad1aSAndrew Lunn 				   u16 value)
20628d03ad1aSAndrew Lunn {
20638d03ad1aSAndrew Lunn 	struct fec_enet_private *fep = bus->priv;
20648d03ad1aSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
20658d03ad1aSAndrew Lunn 	int ret, frame_start, frame_addr;
20668d03ad1aSAndrew Lunn 
20678d03ad1aSAndrew Lunn 	ret = pm_runtime_resume_and_get(dev);
20688d03ad1aSAndrew Lunn 	if (ret < 0)
20698d03ad1aSAndrew Lunn 		return ret;
20708d03ad1aSAndrew Lunn 
2071d3ee8ec7SMarco Hartmann 	/* C22 write */
2072d3ee8ec7SMarco Hartmann 	frame_start = FEC_MMFR_ST;
2073d3ee8ec7SMarco Hartmann 	frame_addr = regnum;
2074d3ee8ec7SMarco Hartmann 
2075793fc096SFrank Li 	/* start a write op */
2076d3ee8ec7SMarco Hartmann 	writel(frame_start | FEC_MMFR_OP_WRITE |
2077d3ee8ec7SMarco Hartmann 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
2078793fc096SFrank Li 	       FEC_MMFR_TA | FEC_MMFR_DATA(value),
2079793fc096SFrank Li 	       fep->hwp + FEC_MII_DATA);
2080793fc096SFrank Li 
2081793fc096SFrank Li 	/* wait for end of transfer */
2082f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
2083f166f890SAndrew Lunn 	if (ret)
208431b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO write timeout\n");
2085793fc096SFrank Li 
20868d03ad1aSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
20878d03ad1aSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
20888d03ad1aSAndrew Lunn 
20898d03ad1aSAndrew Lunn 	return ret;
20908d03ad1aSAndrew Lunn }
20918d03ad1aSAndrew Lunn 
20928d03ad1aSAndrew Lunn static int fec_enet_mdio_write_c45(struct mii_bus *bus, int mii_id,
20938d03ad1aSAndrew Lunn 				   int devad, int regnum, u16 value)
20948d03ad1aSAndrew Lunn {
20958d03ad1aSAndrew Lunn 	struct fec_enet_private *fep = bus->priv;
20968d03ad1aSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
20978d03ad1aSAndrew Lunn 	int ret, frame_start;
20988d03ad1aSAndrew Lunn 
20998d03ad1aSAndrew Lunn 	ret = pm_runtime_resume_and_get(dev);
21008d03ad1aSAndrew Lunn 	if (ret < 0)
21018d03ad1aSAndrew Lunn 		return ret;
21028d03ad1aSAndrew Lunn 
21038d03ad1aSAndrew Lunn 	frame_start = FEC_MMFR_ST_C45;
21048d03ad1aSAndrew Lunn 
21058d03ad1aSAndrew Lunn 	/* write address */
21068d03ad1aSAndrew Lunn 	writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
21078d03ad1aSAndrew Lunn 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
21088d03ad1aSAndrew Lunn 	       FEC_MMFR_TA | (regnum & 0xFFFF),
21098d03ad1aSAndrew Lunn 	       fep->hwp + FEC_MII_DATA);
21108d03ad1aSAndrew Lunn 
21118d03ad1aSAndrew Lunn 	/* wait for end of transfer */
21128d03ad1aSAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
21138d03ad1aSAndrew Lunn 	if (ret) {
21148d03ad1aSAndrew Lunn 		netdev_err(fep->netdev, "MDIO address write timeout\n");
21158d03ad1aSAndrew Lunn 		goto out;
21168d03ad1aSAndrew Lunn 	}
21178d03ad1aSAndrew Lunn 
21188d03ad1aSAndrew Lunn 	/* start a write op */
21198d03ad1aSAndrew Lunn 	writel(frame_start | FEC_MMFR_OP_WRITE |
21208d03ad1aSAndrew Lunn 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
21218d03ad1aSAndrew Lunn 	       FEC_MMFR_TA | FEC_MMFR_DATA(value),
21228d03ad1aSAndrew Lunn 	       fep->hwp + FEC_MII_DATA);
21238d03ad1aSAndrew Lunn 
21248d03ad1aSAndrew Lunn 	/* wait for end of transfer */
21258d03ad1aSAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
21268d03ad1aSAndrew Lunn 	if (ret)
21278d03ad1aSAndrew Lunn 		netdev_err(fep->netdev, "MDIO write timeout\n");
21288d03ad1aSAndrew Lunn 
2129d3ee8ec7SMarco Hartmann out:
21308fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
21318fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
21328fff755eSAndrew Lunn 
21338fff755eSAndrew Lunn 	return ret;
2134793fc096SFrank Li }
2135793fc096SFrank Li 
213664a632daSMarek Vasut static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
213764a632daSMarek Vasut {
213864a632daSMarek Vasut 	struct fec_enet_private *fep = netdev_priv(ndev);
213964a632daSMarek Vasut 	struct phy_device *phy_dev = ndev->phydev;
214064a632daSMarek Vasut 
214164a632daSMarek Vasut 	if (phy_dev) {
214264a632daSMarek Vasut 		phy_reset_after_clk_enable(phy_dev);
214364a632daSMarek Vasut 	} else if (fep->phy_node) {
214464a632daSMarek Vasut 		/*
214564a632daSMarek Vasut 		 * If the PHY still is not bound to the MAC, but there is
214664a632daSMarek Vasut 		 * OF PHY node and a matching PHY device instance already,
214764a632daSMarek Vasut 		 * use the OF PHY node to obtain the PHY device instance,
214864a632daSMarek Vasut 		 * and then use that PHY device instance when triggering
214964a632daSMarek Vasut 		 * the PHY reset.
215064a632daSMarek Vasut 		 */
215164a632daSMarek Vasut 		phy_dev = of_phy_find_device(fep->phy_node);
215264a632daSMarek Vasut 		phy_reset_after_clk_enable(phy_dev);
215364a632daSMarek Vasut 		put_device(&phy_dev->mdio.dev);
215464a632daSMarek Vasut 	}
215564a632daSMarek Vasut }
215664a632daSMarek Vasut 
2157e8fcfcd5SNimrod Andy static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
2158e8fcfcd5SNimrod Andy {
2159e8fcfcd5SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2160e8fcfcd5SNimrod Andy 	int ret;
2161e8fcfcd5SNimrod Andy 
2162e8fcfcd5SNimrod Andy 	if (enable) {
2163e8fcfcd5SNimrod Andy 		ret = clk_prepare_enable(fep->clk_enet_out);
2164e8fcfcd5SNimrod Andy 		if (ret)
2165d7c3a206SAndy Duan 			return ret;
216601e5943aSUwe Kleine-König 
2167e8fcfcd5SNimrod Andy 		if (fep->clk_ptp) {
216801b825f9SFrancesco Dolcini 			mutex_lock(&fep->ptp_clk_mutex);
2169e8fcfcd5SNimrod Andy 			ret = clk_prepare_enable(fep->clk_ptp);
217091c0d987SNimrod Andy 			if (ret) {
217101b825f9SFrancesco Dolcini 				mutex_unlock(&fep->ptp_clk_mutex);
2172e8fcfcd5SNimrod Andy 				goto failed_clk_ptp;
217391c0d987SNimrod Andy 			} else {
217491c0d987SNimrod Andy 				fep->ptp_clk_on = true;
217591c0d987SNimrod Andy 			}
217601b825f9SFrancesco Dolcini 			mutex_unlock(&fep->ptp_clk_mutex);
2177e8fcfcd5SNimrod Andy 		}
217801e5943aSUwe Kleine-König 
21799b5330edSFugang Duan 		ret = clk_prepare_enable(fep->clk_ref);
21809b5330edSFugang Duan 		if (ret)
21819b5330edSFugang Duan 			goto failed_clk_ref;
21821b0a83acSRichard Leitner 
2183fc539459SFugang Duan 		ret = clk_prepare_enable(fep->clk_2x_txclk);
2184fc539459SFugang Duan 		if (ret)
2185fc539459SFugang Duan 			goto failed_clk_2x_txclk;
2186fc539459SFugang Duan 
218764a632daSMarek Vasut 		fec_enet_phy_reset_after_clk_enable(ndev);
2188e8fcfcd5SNimrod Andy 	} else {
2189e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_enet_out);
219091c0d987SNimrod Andy 		if (fep->clk_ptp) {
219101b825f9SFrancesco Dolcini 			mutex_lock(&fep->ptp_clk_mutex);
2192e8fcfcd5SNimrod Andy 			clk_disable_unprepare(fep->clk_ptp);
219391c0d987SNimrod Andy 			fep->ptp_clk_on = false;
219401b825f9SFrancesco Dolcini 			mutex_unlock(&fep->ptp_clk_mutex);
219591c0d987SNimrod Andy 		}
21969b5330edSFugang Duan 		clk_disable_unprepare(fep->clk_ref);
2197fc539459SFugang Duan 		clk_disable_unprepare(fep->clk_2x_txclk);
2198e8fcfcd5SNimrod Andy 	}
2199e8fcfcd5SNimrod Andy 
2200e8fcfcd5SNimrod Andy 	return 0;
22019b5330edSFugang Duan 
2202fc539459SFugang Duan failed_clk_2x_txclk:
2203fc539459SFugang Duan 	if (fep->clk_ref)
2204fc539459SFugang Duan 		clk_disable_unprepare(fep->clk_ref);
22059b5330edSFugang Duan failed_clk_ref:
2206a74d19baSLiu Xiang 	if (fep->clk_ptp) {
220701b825f9SFrancesco Dolcini 		mutex_lock(&fep->ptp_clk_mutex);
2208a74d19baSLiu Xiang 		clk_disable_unprepare(fep->clk_ptp);
2209a74d19baSLiu Xiang 		fep->ptp_clk_on = false;
221001b825f9SFrancesco Dolcini 		mutex_unlock(&fep->ptp_clk_mutex);
2211a74d19baSLiu Xiang 	}
2212e8fcfcd5SNimrod Andy failed_clk_ptp:
2213e8fcfcd5SNimrod Andy 	clk_disable_unprepare(fep->clk_enet_out);
2214e8fcfcd5SNimrod Andy 
2215e8fcfcd5SNimrod Andy 	return ret;
2216e8fcfcd5SNimrod Andy }
2217e8fcfcd5SNimrod Andy 
2218b820c114SJoakim Zhang static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep,
2219b820c114SJoakim Zhang 				      struct device_node *np)
2220b820c114SJoakim Zhang {
2221b820c114SJoakim Zhang 	u32 rgmii_tx_delay, rgmii_rx_delay;
2222b820c114SJoakim Zhang 
2223b820c114SJoakim Zhang 	/* For rgmii tx internal delay, valid values are 0ps and 2000ps */
2224b820c114SJoakim Zhang 	if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) {
2225b820c114SJoakim Zhang 		if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) {
2226b820c114SJoakim Zhang 			dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps");
2227b820c114SJoakim Zhang 			return -EINVAL;
2228b820c114SJoakim Zhang 		} else if (rgmii_tx_delay == 2000) {
2229b820c114SJoakim Zhang 			fep->rgmii_txc_dly = true;
2230b820c114SJoakim Zhang 		}
2231b820c114SJoakim Zhang 	}
2232b820c114SJoakim Zhang 
2233b820c114SJoakim Zhang 	/* For rgmii rx internal delay, valid values are 0ps and 2000ps */
2234b820c114SJoakim Zhang 	if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) {
2235b820c114SJoakim Zhang 		if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) {
2236b820c114SJoakim Zhang 			dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps");
2237b820c114SJoakim Zhang 			return -EINVAL;
2238b820c114SJoakim Zhang 		} else if (rgmii_rx_delay == 2000) {
2239b820c114SJoakim Zhang 			fep->rgmii_rxc_dly = true;
2240b820c114SJoakim Zhang 		}
2241b820c114SJoakim Zhang 	}
2242b820c114SJoakim Zhang 
2243b820c114SJoakim Zhang 	return 0;
2244b820c114SJoakim Zhang }
2245b820c114SJoakim Zhang 
2246793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev)
2247793fc096SFrank Li {
2248793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2249793fc096SFrank Li 	struct phy_device *phy_dev = NULL;
2250793fc096SFrank Li 	char mdio_bus_id[MII_BUS_ID_SIZE];
2251793fc096SFrank Li 	char phy_name[MII_BUS_ID_SIZE + 3];
2252793fc096SFrank Li 	int phy_id;
2253793fc096SFrank Li 	int dev_id = fep->dev_id;
2254793fc096SFrank Li 
2255407066f8SUwe Kleine-König 	if (fep->phy_node) {
2256407066f8SUwe Kleine-König 		phy_dev = of_phy_connect(ndev, fep->phy_node,
2257407066f8SUwe Kleine-König 					 &fec_enet_adjust_link, 0,
2258407066f8SUwe Kleine-König 					 fep->phy_interface);
22599558df3aSAndrew Lunn 		if (!phy_dev) {
22609558df3aSAndrew Lunn 			netdev_err(ndev, "Unable to connect to phy\n");
2261213a9922SNimrod Andy 			return -ENODEV;
22629558df3aSAndrew Lunn 		}
2263407066f8SUwe Kleine-König 	} else {
2264793fc096SFrank Li 		/* check for attached phy */
2265793fc096SFrank Li 		for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
22667f854420SAndrew Lunn 			if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
2267793fc096SFrank Li 				continue;
2268793fc096SFrank Li 			if (dev_id--)
2269793fc096SFrank Li 				continue;
2270f029c781SWolfram Sang 			strscpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
2271793fc096SFrank Li 			break;
2272793fc096SFrank Li 		}
2273793fc096SFrank Li 
2274793fc096SFrank Li 		if (phy_id >= PHY_MAX_ADDR) {
227531b7720cSJoe Perches 			netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
2276f029c781SWolfram Sang 			strscpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
2277793fc096SFrank Li 			phy_id = 0;
2278793fc096SFrank Li 		}
2279793fc096SFrank Li 
2280407066f8SUwe Kleine-König 		snprintf(phy_name, sizeof(phy_name),
2281407066f8SUwe Kleine-König 			 PHY_ID_FMT, mdio_bus_id, phy_id);
2282793fc096SFrank Li 		phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
2283793fc096SFrank Li 				      fep->phy_interface);
2284407066f8SUwe Kleine-König 	}
2285407066f8SUwe Kleine-König 
2286793fc096SFrank Li 	if (IS_ERR(phy_dev)) {
228731b7720cSJoe Perches 		netdev_err(ndev, "could not attach to PHY\n");
2288793fc096SFrank Li 		return PTR_ERR(phy_dev);
2289793fc096SFrank Li 	}
2290793fc096SFrank Li 
2291793fc096SFrank Li 	/* mask with MAC supported features */
22926b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
229358056c1eSAndrew Lunn 		phy_set_max_speed(phy_dev, 1000);
229441124fa6SAndrew Lunn 		phy_remove_link_mode(phy_dev,
229541124fa6SAndrew Lunn 				     ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
2296d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2297c306ad36SAndrew Lunn 		phy_support_sym_pause(phy_dev);
2298d1391930SGuenter Roeck #endif
2299793fc096SFrank Li 	}
2300793fc096SFrank Li 	else
230158056c1eSAndrew Lunn 		phy_set_max_speed(phy_dev, 100);
2302793fc096SFrank Li 
2303793fc096SFrank Li 	fep->link = 0;
2304793fc096SFrank Li 	fep->full_duplex = 0;
2305793fc096SFrank Li 
2306eca485d2SDenis Kirjanov 	phy_dev->mac_managed_pm = true;
2307557d5dc8SHeiner Kallweit 
23082220943aSAndrew Lunn 	phy_attached_info(phy_dev);
2309793fc096SFrank Li 
2310793fc096SFrank Li 	return 0;
2311793fc096SFrank Li }
2312793fc096SFrank Li 
2313793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev)
2314793fc096SFrank Li {
2315793fc096SFrank Li 	static struct mii_bus *fec0_mii_bus;
2316793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
2317793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
23183c01eb62SAndrew Lunn 	bool suppress_preamble = false;
2319407066f8SUwe Kleine-König 	struct device_node *node;
2320e7f4dc35SAndrew Lunn 	int err = -ENXIO;
232163c60732SUwe Kleine-König 	u32 mii_speed, holdtime;
23223e782985SAndrew Lunn 	u32 bus_freq;
2323793fc096SFrank Li 
2324793fc096SFrank Li 	/*
23253d125f9cSStefan Agner 	 * The i.MX28 dual fec interfaces are not equal.
2326793fc096SFrank Li 	 * Here are the differences:
2327793fc096SFrank Li 	 *
2328793fc096SFrank Li 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
2329793fc096SFrank Li 	 *  - fec0 acts as the 1588 time master while fec1 is slave
2330793fc096SFrank Li 	 *  - external phys can only be configured by fec0
2331793fc096SFrank Li 	 *
2332793fc096SFrank Li 	 * That is to say fec1 can not work independently. It only works
2333793fc096SFrank Li 	 * when fec0 is working. The reason behind this design is that the
2334793fc096SFrank Li 	 * second interface is added primarily for Switch mode.
2335793fc096SFrank Li 	 *
2336793fc096SFrank Li 	 * Because of the last point above, both phys are attached on fec0
2337793fc096SFrank Li 	 * mdio interface in board design, and need to be configured by
2338793fc096SFrank Li 	 * fec0 mii_bus.
2339793fc096SFrank Li 	 */
23403d125f9cSStefan Agner 	if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
2341793fc096SFrank Li 		/* fec1 uses fec0 mii_bus */
2342793fc096SFrank Li 		if (mii_cnt && fec0_mii_bus) {
2343793fc096SFrank Li 			fep->mii_bus = fec0_mii_bus;
2344793fc096SFrank Li 			mii_cnt++;
2345793fc096SFrank Li 			return 0;
2346793fc096SFrank Li 		}
2347793fc096SFrank Li 		return -ENOENT;
2348793fc096SFrank Li 	}
2349793fc096SFrank Li 
23503e782985SAndrew Lunn 	bus_freq = 2500000; /* 2.5MHz by default */
23513e782985SAndrew Lunn 	node = of_get_child_by_name(pdev->dev.of_node, "mdio");
23523c01eb62SAndrew Lunn 	if (node) {
23533e782985SAndrew Lunn 		of_property_read_u32(node, "clock-frequency", &bus_freq);
23543c01eb62SAndrew Lunn 		suppress_preamble = of_property_read_bool(node,
23553c01eb62SAndrew Lunn 							  "suppress-preamble");
23563c01eb62SAndrew Lunn 	}
23573e782985SAndrew Lunn 
2358793fc096SFrank Li 	/*
23593e782985SAndrew Lunn 	 * Set MII speed (= clk_get_rate() / 2 * phy_speed)
2360793fc096SFrank Li 	 *
2361793fc096SFrank Li 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2362793fc096SFrank Li 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
2363793fc096SFrank Li 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2364793fc096SFrank Li 	 * document.
2365793fc096SFrank Li 	 */
23663e782985SAndrew Lunn 	mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2);
23676b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC)
236863c60732SUwe Kleine-König 		mii_speed--;
236963c60732SUwe Kleine-König 	if (mii_speed > 63) {
237063c60732SUwe Kleine-König 		dev_err(&pdev->dev,
2371981a0547SPeter Meerwald-Stadler 			"fec clock (%lu) too fast to get right mii speed\n",
237263c60732SUwe Kleine-König 			clk_get_rate(fep->clk_ipg));
237363c60732SUwe Kleine-König 		err = -EINVAL;
237463c60732SUwe Kleine-König 		goto err_out;
237563c60732SUwe Kleine-König 	}
237663c60732SUwe Kleine-König 
237763c60732SUwe Kleine-König 	/*
237863c60732SUwe Kleine-König 	 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
237963c60732SUwe Kleine-König 	 * MII_SPEED) register that defines the MDIO output hold time. Earlier
238063c60732SUwe Kleine-König 	 * versions are RAZ there, so just ignore the difference and write the
238163c60732SUwe Kleine-König 	 * register always.
238263c60732SUwe Kleine-König 	 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
238363c60732SUwe Kleine-König 	 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
238463c60732SUwe Kleine-König 	 * output.
238563c60732SUwe Kleine-König 	 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
238663c60732SUwe Kleine-König 	 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
238763c60732SUwe Kleine-König 	 * holdtime cannot result in a value greater than 3.
238863c60732SUwe Kleine-König 	 */
238963c60732SUwe Kleine-König 	holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
239063c60732SUwe Kleine-König 
239163c60732SUwe Kleine-König 	fep->phy_speed = mii_speed << 1 | holdtime << 8;
239263c60732SUwe Kleine-König 
23933c01eb62SAndrew Lunn 	if (suppress_preamble)
23943c01eb62SAndrew Lunn 		fep->phy_speed |= BIT(7);
23953c01eb62SAndrew Lunn 
23961e6114f5SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) {
2397f166f890SAndrew Lunn 		/* Clear MMFR to avoid to generate MII event by writing MSCR.
2398f166f890SAndrew Lunn 		 * MII event generation condition:
2399f166f890SAndrew Lunn 		 * - writing MSCR:
2400f166f890SAndrew Lunn 		 *	- mmfr[31:0]_not_zero & mscr[7:0]_is_zero &
2401f166f890SAndrew Lunn 		 *	  mscr_reg_data_in[7:0] != 0
2402f166f890SAndrew Lunn 		 * - writing MMFR:
2403f166f890SAndrew Lunn 		 *	- mscr[7:0]_not_zero
2404f166f890SAndrew Lunn 		 */
2405f166f890SAndrew Lunn 		writel(0, fep->hwp + FEC_MII_DATA);
24061e6114f5SGreg Ungerer 	}
2407f166f890SAndrew Lunn 
2408793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2409793fc096SFrank Li 
2410f166f890SAndrew Lunn 	/* Clear any pending transaction complete indication */
2411f166f890SAndrew Lunn 	writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
2412f166f890SAndrew Lunn 
2413793fc096SFrank Li 	fep->mii_bus = mdiobus_alloc();
2414793fc096SFrank Li 	if (fep->mii_bus == NULL) {
2415793fc096SFrank Li 		err = -ENOMEM;
2416793fc096SFrank Li 		goto err_out;
2417793fc096SFrank Li 	}
2418793fc096SFrank Li 
2419793fc096SFrank Li 	fep->mii_bus->name = "fec_enet_mii_bus";
24208d03ad1aSAndrew Lunn 	fep->mii_bus->read = fec_enet_mdio_read_c22;
24218d03ad1aSAndrew Lunn 	fep->mii_bus->write = fec_enet_mdio_write_c22;
2422abc33494SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_HAS_MDIO_C45) {
24238d03ad1aSAndrew Lunn 		fep->mii_bus->read_c45 = fec_enet_mdio_read_c45;
24248d03ad1aSAndrew Lunn 		fep->mii_bus->write_c45 = fec_enet_mdio_write_c45;
2425abc33494SGreg Ungerer 	}
2426793fc096SFrank Li 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2427793fc096SFrank Li 		pdev->name, fep->dev_id + 1);
2428793fc096SFrank Li 	fep->mii_bus->priv = fep;
2429793fc096SFrank Li 	fep->mii_bus->parent = &pdev->dev;
2430793fc096SFrank Li 
2431407066f8SUwe Kleine-König 	err = of_mdiobus_register(fep->mii_bus, node);
2432407066f8SUwe Kleine-König 	if (err)
2433e7f4dc35SAndrew Lunn 		goto err_out_free_mdiobus;
24340607a2cdSPan Bian 	of_node_put(node);
2435793fc096SFrank Li 
2436793fc096SFrank Li 	mii_cnt++;
2437793fc096SFrank Li 
2438793fc096SFrank Li 	/* save fec0 mii_bus */
24393d125f9cSStefan Agner 	if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2440793fc096SFrank Li 		fec0_mii_bus = fep->mii_bus;
2441793fc096SFrank Li 
2442793fc096SFrank Li 	return 0;
2443793fc096SFrank Li 
2444793fc096SFrank Li err_out_free_mdiobus:
2445793fc096SFrank Li 	mdiobus_free(fep->mii_bus);
2446793fc096SFrank Li err_out:
24470607a2cdSPan Bian 	of_node_put(node);
2448793fc096SFrank Li 	return err;
2449793fc096SFrank Li }
2450793fc096SFrank Li 
2451793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep)
2452793fc096SFrank Li {
2453793fc096SFrank Li 	if (--mii_cnt == 0) {
2454793fc096SFrank Li 		mdiobus_unregister(fep->mii_bus);
2455793fc096SFrank Li 		mdiobus_free(fep->mii_bus);
2456793fc096SFrank Li 	}
2457793fc096SFrank Li }
2458793fc096SFrank Li 
2459793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev,
2460793fc096SFrank Li 				 struct ethtool_drvinfo *info)
2461793fc096SFrank Li {
2462793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2463793fc096SFrank Li 
2464f029c781SWolfram Sang 	strscpy(info->driver, fep->pdev->dev.driver->name,
2465793fc096SFrank Li 		sizeof(info->driver));
2466f029c781SWolfram Sang 	strscpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2467793fc096SFrank Li }
2468793fc096SFrank Li 
2469db65f35fSPhilippe Reynes static int fec_enet_get_regs_len(struct net_device *ndev)
2470db65f35fSPhilippe Reynes {
2471db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2472db65f35fSPhilippe Reynes 	struct resource *r;
2473db65f35fSPhilippe Reynes 	int s = 0;
2474db65f35fSPhilippe Reynes 
2475db65f35fSPhilippe Reynes 	r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2476db65f35fSPhilippe Reynes 	if (r)
2477db65f35fSPhilippe Reynes 		s = resource_size(r);
2478db65f35fSPhilippe Reynes 
2479db65f35fSPhilippe Reynes 	return s;
2480db65f35fSPhilippe Reynes }
2481db65f35fSPhilippe Reynes 
2482db65f35fSPhilippe Reynes /* List of registers that can be safety be read to dump them with ethtool */
2483db65f35fSPhilippe Reynes #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
24843f1dcc6aSLucas Stach 	defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
248578cc6e7eSFlorian Fainelli 	defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
2486f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 2;
2487db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2488db65f35fSPhilippe Reynes 	FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2489db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2490db65f35fSPhilippe Reynes 	FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2491db65f35fSPhilippe Reynes 	FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2492db65f35fSPhilippe Reynes 	FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2493db65f35fSPhilippe Reynes 	FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2494db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2495db65f35fSPhilippe Reynes 	FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2496db65f35fSPhilippe Reynes 	FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2497db65f35fSPhilippe Reynes 	FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2498db65f35fSPhilippe Reynes 	FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2499db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2500db65f35fSPhilippe Reynes 	RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2501db65f35fSPhilippe Reynes 	RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2502db65f35fSPhilippe Reynes 	RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2503db65f35fSPhilippe Reynes 	RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2504db65f35fSPhilippe Reynes 	RMON_T_P_GTE2048, RMON_T_OCTETS,
2505db65f35fSPhilippe Reynes 	IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2506db65f35fSPhilippe Reynes 	IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2507db65f35fSPhilippe Reynes 	IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2508db65f35fSPhilippe Reynes 	RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2509db65f35fSPhilippe Reynes 	RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2510db65f35fSPhilippe Reynes 	RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2511db65f35fSPhilippe Reynes 	RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2512db65f35fSPhilippe Reynes 	RMON_R_P_GTE2048, RMON_R_OCTETS,
2513db65f35fSPhilippe Reynes 	IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2514db65f35fSPhilippe Reynes 	IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2515db65f35fSPhilippe Reynes };
25160a8b43b1SJuergen Borleis /* for i.MX6ul */
25170a8b43b1SJuergen Borleis static u32 fec_enet_register_offset_6ul[] = {
25180a8b43b1SJuergen Borleis 	FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
25190a8b43b1SJuergen Borleis 	FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
25200a8b43b1SJuergen Borleis 	FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_RXIC0,
25210a8b43b1SJuergen Borleis 	FEC_HASH_TABLE_HIGH, FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH,
25220a8b43b1SJuergen Borleis 	FEC_GRP_HASH_TABLE_LOW, FEC_X_WMRK, FEC_R_DES_START_0,
25230a8b43b1SJuergen Borleis 	FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
25240a8b43b1SJuergen Borleis 	FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC,
25250a8b43b1SJuergen Borleis 	RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
25260a8b43b1SJuergen Borleis 	RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
25270a8b43b1SJuergen Borleis 	RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
25280a8b43b1SJuergen Borleis 	RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
25290a8b43b1SJuergen Borleis 	RMON_T_P_GTE2048, RMON_T_OCTETS,
25300a8b43b1SJuergen Borleis 	IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
25310a8b43b1SJuergen Borleis 	IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
25320a8b43b1SJuergen Borleis 	IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
25330a8b43b1SJuergen Borleis 	RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
25340a8b43b1SJuergen Borleis 	RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
25350a8b43b1SJuergen Borleis 	RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
25360a8b43b1SJuergen Borleis 	RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
25370a8b43b1SJuergen Borleis 	RMON_R_P_GTE2048, RMON_R_OCTETS,
25380a8b43b1SJuergen Borleis 	IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
25390a8b43b1SJuergen Borleis 	IEEE_R_FDXFC, IEEE_R_OCTETS_OK
25400a8b43b1SJuergen Borleis };
2541db65f35fSPhilippe Reynes #else
2542f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 1;
2543db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2544db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2545db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2546db65f35fSPhilippe Reynes 	FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2547db65f35fSPhilippe Reynes 	FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2548db65f35fSPhilippe Reynes 	FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2549db65f35fSPhilippe Reynes 	FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2550db65f35fSPhilippe Reynes 	FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2551db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2552db65f35fSPhilippe Reynes 	FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2553db65f35fSPhilippe Reynes };
2554db65f35fSPhilippe Reynes #endif
2555db65f35fSPhilippe Reynes 
2556db65f35fSPhilippe Reynes static void fec_enet_get_regs(struct net_device *ndev,
2557db65f35fSPhilippe Reynes 			      struct ethtool_regs *regs, void *regbuf)
2558db65f35fSPhilippe Reynes {
2559db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2560db65f35fSPhilippe Reynes 	u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2561c72a0bc0SAndrew Lunn 	struct device *dev = &fep->pdev->dev;
2562db65f35fSPhilippe Reynes 	u32 *buf = (u32 *)regbuf;
2563db65f35fSPhilippe Reynes 	u32 i, off;
2564c72a0bc0SAndrew Lunn 	int ret;
25650a8b43b1SJuergen Borleis #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
25660a8b43b1SJuergen Borleis 	defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
25670a8b43b1SJuergen Borleis 	defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
25680a8b43b1SJuergen Borleis 	u32 *reg_list;
25690a8b43b1SJuergen Borleis 	u32 reg_cnt;
2570c72a0bc0SAndrew Lunn 
25710a8b43b1SJuergen Borleis 	if (!of_machine_is_compatible("fsl,imx6ul")) {
25720a8b43b1SJuergen Borleis 		reg_list = fec_enet_register_offset;
25730a8b43b1SJuergen Borleis 		reg_cnt = ARRAY_SIZE(fec_enet_register_offset);
25740a8b43b1SJuergen Borleis 	} else {
25750a8b43b1SJuergen Borleis 		reg_list = fec_enet_register_offset_6ul;
25760a8b43b1SJuergen Borleis 		reg_cnt = ARRAY_SIZE(fec_enet_register_offset_6ul);
25770a8b43b1SJuergen Borleis 	}
25780a8b43b1SJuergen Borleis #else
25790a8b43b1SJuergen Borleis 	/* coldfire */
25800a8b43b1SJuergen Borleis 	static u32 *reg_list = fec_enet_register_offset;
25810a8b43b1SJuergen Borleis 	static const u32 reg_cnt = ARRAY_SIZE(fec_enet_register_offset);
25820a8b43b1SJuergen Borleis #endif
2583da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
2584c72a0bc0SAndrew Lunn 	if (ret < 0)
2585c72a0bc0SAndrew Lunn 		return;
2586db65f35fSPhilippe Reynes 
2587f9bcc9f3SVivien Didelot 	regs->version = fec_enet_register_version;
2588f9bcc9f3SVivien Didelot 
2589db65f35fSPhilippe Reynes 	memset(buf, 0, regs->len);
2590db65f35fSPhilippe Reynes 
25910a8b43b1SJuergen Borleis 	for (i = 0; i < reg_cnt; i++) {
25920a8b43b1SJuergen Borleis 		off = reg_list[i];
2593ec20a63aSFugang Duan 
2594ec20a63aSFugang Duan 		if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
2595ec20a63aSFugang Duan 		    !(fep->quirks & FEC_QUIRK_HAS_FRREG))
2596ec20a63aSFugang Duan 			continue;
2597ec20a63aSFugang Duan 
2598ec20a63aSFugang Duan 		off >>= 2;
2599db65f35fSPhilippe Reynes 		buf[off] = readl(&theregs[off]);
2600db65f35fSPhilippe Reynes 	}
2601c72a0bc0SAndrew Lunn 
2602c72a0bc0SAndrew Lunn 	pm_runtime_mark_last_busy(dev);
2603c72a0bc0SAndrew Lunn 	pm_runtime_put_autosuspend(dev);
2604db65f35fSPhilippe Reynes }
2605db65f35fSPhilippe Reynes 
2606793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev,
2607793fc096SFrank Li 				struct ethtool_ts_info *info)
2608793fc096SFrank Li {
2609793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2610793fc096SFrank Li 
2611793fc096SFrank Li 	if (fep->bufdesc_ex) {
2612793fc096SFrank Li 
2613793fc096SFrank Li 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2614793fc096SFrank Li 					SOF_TIMESTAMPING_RX_SOFTWARE |
2615793fc096SFrank Li 					SOF_TIMESTAMPING_SOFTWARE |
2616793fc096SFrank Li 					SOF_TIMESTAMPING_TX_HARDWARE |
2617793fc096SFrank Li 					SOF_TIMESTAMPING_RX_HARDWARE |
2618793fc096SFrank Li 					SOF_TIMESTAMPING_RAW_HARDWARE;
2619793fc096SFrank Li 		if (fep->ptp_clock)
2620793fc096SFrank Li 			info->phc_index = ptp_clock_index(fep->ptp_clock);
2621793fc096SFrank Li 		else
2622793fc096SFrank Li 			info->phc_index = -1;
2623793fc096SFrank Li 
2624793fc096SFrank Li 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2625793fc096SFrank Li 				 (1 << HWTSTAMP_TX_ON);
2626793fc096SFrank Li 
2627793fc096SFrank Li 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2628793fc096SFrank Li 				   (1 << HWTSTAMP_FILTER_ALL);
2629793fc096SFrank Li 		return 0;
2630793fc096SFrank Li 	} else {
2631793fc096SFrank Li 		return ethtool_op_get_ts_info(ndev, info);
2632793fc096SFrank Li 	}
2633793fc096SFrank Li }
2634793fc096SFrank Li 
2635d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2636d1391930SGuenter Roeck 
2637793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev,
2638793fc096SFrank Li 				    struct ethtool_pauseparam *pause)
2639793fc096SFrank Li {
2640793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2641793fc096SFrank Li 
2642793fc096SFrank Li 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2643793fc096SFrank Li 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2644793fc096SFrank Li 	pause->rx_pause = pause->tx_pause;
2645793fc096SFrank Li }
2646793fc096SFrank Li 
2647793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev,
2648793fc096SFrank Li 				   struct ethtool_pauseparam *pause)
2649793fc096SFrank Li {
2650793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2651793fc096SFrank Li 
265245f5c327SPhilippe Reynes 	if (!ndev->phydev)
26530b146ca8SRussell King 		return -ENODEV;
26540b146ca8SRussell King 
2655793fc096SFrank Li 	if (pause->tx_pause != pause->rx_pause) {
2656793fc096SFrank Li 		netdev_info(ndev,
2657793fc096SFrank Li 			"hardware only support enable/disable both tx and rx");
2658793fc096SFrank Li 		return -EINVAL;
2659793fc096SFrank Li 	}
2660793fc096SFrank Li 
2661793fc096SFrank Li 	fep->pause_flag = 0;
2662793fc096SFrank Li 
2663793fc096SFrank Li 	/* tx pause must be same as rx pause */
2664793fc096SFrank Li 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2665793fc096SFrank Li 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2666793fc096SFrank Li 
26670c122405SAndrew Lunn 	phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
26680c122405SAndrew Lunn 			  pause->autoneg);
2669793fc096SFrank Li 
2670793fc096SFrank Li 	if (pause->autoneg) {
2671793fc096SFrank Li 		if (netif_running(ndev))
2672793fc096SFrank Li 			fec_stop(ndev);
267345f5c327SPhilippe Reynes 		phy_start_aneg(ndev->phydev);
2674793fc096SFrank Li 	}
2675dbc64a8eSRussell King 	if (netif_running(ndev)) {
2676dbc64a8eSRussell King 		napi_disable(&fep->napi);
2677dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
2678ef83337dSRussell King 		fec_restart(ndev);
2679657ade07SRickard x Andersson 		netif_tx_wake_all_queues(ndev);
26806af42d42SRussell King 		netif_tx_unlock_bh(ndev);
2681dbc64a8eSRussell King 		napi_enable(&fep->napi);
2682dbc64a8eSRussell King 	}
2683793fc096SFrank Li 
2684793fc096SFrank Li 	return 0;
2685793fc096SFrank Li }
2686793fc096SFrank Li 
268738ae92dcSChris Healy static const struct fec_stat {
268838ae92dcSChris Healy 	char name[ETH_GSTRING_LEN];
268938ae92dcSChris Healy 	u16 offset;
269038ae92dcSChris Healy } fec_stats[] = {
269138ae92dcSChris Healy 	/* RMON TX */
269238ae92dcSChris Healy 	{ "tx_dropped", RMON_T_DROP },
269338ae92dcSChris Healy 	{ "tx_packets", RMON_T_PACKETS },
269438ae92dcSChris Healy 	{ "tx_broadcast", RMON_T_BC_PKT },
269538ae92dcSChris Healy 	{ "tx_multicast", RMON_T_MC_PKT },
269638ae92dcSChris Healy 	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
269738ae92dcSChris Healy 	{ "tx_undersize", RMON_T_UNDERSIZE },
269838ae92dcSChris Healy 	{ "tx_oversize", RMON_T_OVERSIZE },
269938ae92dcSChris Healy 	{ "tx_fragment", RMON_T_FRAG },
270038ae92dcSChris Healy 	{ "tx_jabber", RMON_T_JAB },
270138ae92dcSChris Healy 	{ "tx_collision", RMON_T_COL },
270238ae92dcSChris Healy 	{ "tx_64byte", RMON_T_P64 },
270338ae92dcSChris Healy 	{ "tx_65to127byte", RMON_T_P65TO127 },
270438ae92dcSChris Healy 	{ "tx_128to255byte", RMON_T_P128TO255 },
270538ae92dcSChris Healy 	{ "tx_256to511byte", RMON_T_P256TO511 },
270638ae92dcSChris Healy 	{ "tx_512to1023byte", RMON_T_P512TO1023 },
270738ae92dcSChris Healy 	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
270838ae92dcSChris Healy 	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
270938ae92dcSChris Healy 	{ "tx_octets", RMON_T_OCTETS },
271038ae92dcSChris Healy 
271138ae92dcSChris Healy 	/* IEEE TX */
271238ae92dcSChris Healy 	{ "IEEE_tx_drop", IEEE_T_DROP },
271338ae92dcSChris Healy 	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
271438ae92dcSChris Healy 	{ "IEEE_tx_1col", IEEE_T_1COL },
271538ae92dcSChris Healy 	{ "IEEE_tx_mcol", IEEE_T_MCOL },
271638ae92dcSChris Healy 	{ "IEEE_tx_def", IEEE_T_DEF },
271738ae92dcSChris Healy 	{ "IEEE_tx_lcol", IEEE_T_LCOL },
271838ae92dcSChris Healy 	{ "IEEE_tx_excol", IEEE_T_EXCOL },
271938ae92dcSChris Healy 	{ "IEEE_tx_macerr", IEEE_T_MACERR },
272038ae92dcSChris Healy 	{ "IEEE_tx_cserr", IEEE_T_CSERR },
272138ae92dcSChris Healy 	{ "IEEE_tx_sqe", IEEE_T_SQE },
272238ae92dcSChris Healy 	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
272338ae92dcSChris Healy 	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
272438ae92dcSChris Healy 
272538ae92dcSChris Healy 	/* RMON RX */
272638ae92dcSChris Healy 	{ "rx_packets", RMON_R_PACKETS },
272738ae92dcSChris Healy 	{ "rx_broadcast", RMON_R_BC_PKT },
272838ae92dcSChris Healy 	{ "rx_multicast", RMON_R_MC_PKT },
272938ae92dcSChris Healy 	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
273038ae92dcSChris Healy 	{ "rx_undersize", RMON_R_UNDERSIZE },
273138ae92dcSChris Healy 	{ "rx_oversize", RMON_R_OVERSIZE },
273238ae92dcSChris Healy 	{ "rx_fragment", RMON_R_FRAG },
273338ae92dcSChris Healy 	{ "rx_jabber", RMON_R_JAB },
273438ae92dcSChris Healy 	{ "rx_64byte", RMON_R_P64 },
273538ae92dcSChris Healy 	{ "rx_65to127byte", RMON_R_P65TO127 },
273638ae92dcSChris Healy 	{ "rx_128to255byte", RMON_R_P128TO255 },
273738ae92dcSChris Healy 	{ "rx_256to511byte", RMON_R_P256TO511 },
273838ae92dcSChris Healy 	{ "rx_512to1023byte", RMON_R_P512TO1023 },
273938ae92dcSChris Healy 	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
274038ae92dcSChris Healy 	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
274138ae92dcSChris Healy 	{ "rx_octets", RMON_R_OCTETS },
274238ae92dcSChris Healy 
274338ae92dcSChris Healy 	/* IEEE RX */
274438ae92dcSChris Healy 	{ "IEEE_rx_drop", IEEE_R_DROP },
274538ae92dcSChris Healy 	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
274638ae92dcSChris Healy 	{ "IEEE_rx_crc", IEEE_R_CRC },
274738ae92dcSChris Healy 	{ "IEEE_rx_align", IEEE_R_ALIGN },
274838ae92dcSChris Healy 	{ "IEEE_rx_macerr", IEEE_R_MACERR },
274938ae92dcSChris Healy 	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
275038ae92dcSChris Healy 	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
275138ae92dcSChris Healy };
275238ae92dcSChris Healy 
2753f85de666SNikita Yushchenko #define FEC_STATS_SIZE		(ARRAY_SIZE(fec_stats) * sizeof(u64))
2754f85de666SNikita Yushchenko 
27556970ef27SShenwei Wang static const char *fec_xdp_stat_strs[XDP_STATS_TOTAL] = {
27566970ef27SShenwei Wang 	"rx_xdp_redirect",           /* RX_XDP_REDIRECT = 0, */
27576970ef27SShenwei Wang 	"rx_xdp_pass",               /* RX_XDP_PASS, */
27586970ef27SShenwei Wang 	"rx_xdp_drop",               /* RX_XDP_DROP, */
27596970ef27SShenwei Wang 	"rx_xdp_tx",                 /* RX_XDP_TX, */
27606970ef27SShenwei Wang 	"rx_xdp_tx_errors",          /* RX_XDP_TX_ERRORS, */
27616970ef27SShenwei Wang 	"tx_xdp_xmit",               /* TX_XDP_XMIT, */
27626970ef27SShenwei Wang 	"tx_xdp_xmit_errors",        /* TX_XDP_XMIT_ERRORS, */
27636970ef27SShenwei Wang };
27646970ef27SShenwei Wang 
276580cca775SNikita Yushchenko static void fec_enet_update_ethtool_stats(struct net_device *dev)
276638ae92dcSChris Healy {
276738ae92dcSChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
276838ae92dcSChris Healy 	int i;
276938ae92dcSChris Healy 
277038ae92dcSChris Healy 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
277180cca775SNikita Yushchenko 		fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
277280cca775SNikita Yushchenko }
277380cca775SNikita Yushchenko 
27746970ef27SShenwei Wang static void fec_enet_get_xdp_stats(struct fec_enet_private *fep, u64 *data)
27756970ef27SShenwei Wang {
27766970ef27SShenwei Wang 	u64 xdp_stats[XDP_STATS_TOTAL] = { 0 };
27776970ef27SShenwei Wang 	struct fec_enet_priv_rx_q *rxq;
27786970ef27SShenwei Wang 	int i, j;
27796970ef27SShenwei Wang 
27806970ef27SShenwei Wang 	for (i = fep->num_rx_queues - 1; i >= 0; i--) {
27816970ef27SShenwei Wang 		rxq = fep->rx_queue[i];
27826970ef27SShenwei Wang 
27836970ef27SShenwei Wang 		for (j = 0; j < XDP_STATS_TOTAL; j++)
27846970ef27SShenwei Wang 			xdp_stats[j] += rxq->stats[j];
27856970ef27SShenwei Wang 	}
27866970ef27SShenwei Wang 
27876970ef27SShenwei Wang 	memcpy(data, xdp_stats, sizeof(xdp_stats));
27886970ef27SShenwei Wang }
27896970ef27SShenwei Wang 
27906970ef27SShenwei Wang static void fec_enet_page_pool_stats(struct fec_enet_private *fep, u64 *data)
27916970ef27SShenwei Wang {
2792*857922b1SLucas Stach #ifdef CONFIG_PAGE_POOL_STATS
27936970ef27SShenwei Wang 	struct page_pool_stats stats = {};
27946970ef27SShenwei Wang 	struct fec_enet_priv_rx_q *rxq;
27956970ef27SShenwei Wang 	int i;
27966970ef27SShenwei Wang 
27976970ef27SShenwei Wang 	for (i = fep->num_rx_queues - 1; i >= 0; i--) {
27986970ef27SShenwei Wang 		rxq = fep->rx_queue[i];
27996970ef27SShenwei Wang 
28006970ef27SShenwei Wang 		if (!rxq->page_pool)
28016970ef27SShenwei Wang 			continue;
28026970ef27SShenwei Wang 
28036970ef27SShenwei Wang 		page_pool_get_stats(rxq->page_pool, &stats);
28046970ef27SShenwei Wang 	}
28056970ef27SShenwei Wang 
28066970ef27SShenwei Wang 	page_pool_ethtool_stats_get(data, &stats);
2807*857922b1SLucas Stach #endif
28086970ef27SShenwei Wang }
28096970ef27SShenwei Wang 
281080cca775SNikita Yushchenko static void fec_enet_get_ethtool_stats(struct net_device *dev,
281180cca775SNikita Yushchenko 				       struct ethtool_stats *stats, u64 *data)
281280cca775SNikita Yushchenko {
281380cca775SNikita Yushchenko 	struct fec_enet_private *fep = netdev_priv(dev);
281480cca775SNikita Yushchenko 
281580cca775SNikita Yushchenko 	if (netif_running(dev))
281680cca775SNikita Yushchenko 		fec_enet_update_ethtool_stats(dev);
281780cca775SNikita Yushchenko 
2818f85de666SNikita Yushchenko 	memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
28196970ef27SShenwei Wang 	data += FEC_STATS_SIZE / sizeof(u64);
28206970ef27SShenwei Wang 
28216970ef27SShenwei Wang 	fec_enet_get_xdp_stats(fep, data);
28226970ef27SShenwei Wang 	data += XDP_STATS_TOTAL;
28236970ef27SShenwei Wang 
28246970ef27SShenwei Wang 	fec_enet_page_pool_stats(fep, data);
282538ae92dcSChris Healy }
282638ae92dcSChris Healy 
282738ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev,
282838ae92dcSChris Healy 	u32 stringset, u8 *data)
282938ae92dcSChris Healy {
283038ae92dcSChris Healy 	int i;
283138ae92dcSChris Healy 	switch (stringset) {
283238ae92dcSChris Healy 	case ETH_SS_STATS:
28336970ef27SShenwei Wang 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++) {
28346970ef27SShenwei Wang 			memcpy(data, fec_stats[i].name, ETH_GSTRING_LEN);
28356970ef27SShenwei Wang 			data += ETH_GSTRING_LEN;
28366970ef27SShenwei Wang 		}
28376970ef27SShenwei Wang 		for (i = 0; i < ARRAY_SIZE(fec_xdp_stat_strs); i++) {
28386970ef27SShenwei Wang 			strncpy(data, fec_xdp_stat_strs[i], ETH_GSTRING_LEN);
28396970ef27SShenwei Wang 			data += ETH_GSTRING_LEN;
28406970ef27SShenwei Wang 		}
28416970ef27SShenwei Wang 		page_pool_ethtool_stats_get_strings(data);
28426970ef27SShenwei Wang 
284338ae92dcSChris Healy 		break;
28446016ba34SOleksij Rempel 	case ETH_SS_TEST:
28456016ba34SOleksij Rempel 		net_selftest_get_strings(data);
28466016ba34SOleksij Rempel 		break;
284738ae92dcSChris Healy 	}
284838ae92dcSChris Healy }
284938ae92dcSChris Healy 
285038ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset)
285138ae92dcSChris Healy {
28526970ef27SShenwei Wang 	int count;
28536970ef27SShenwei Wang 
285438ae92dcSChris Healy 	switch (sset) {
285538ae92dcSChris Healy 	case ETH_SS_STATS:
28566970ef27SShenwei Wang 		count = ARRAY_SIZE(fec_stats) + XDP_STATS_TOTAL;
28576970ef27SShenwei Wang 		count += page_pool_ethtool_stats_get_count();
28586970ef27SShenwei Wang 		return count;
28596970ef27SShenwei Wang 
28606016ba34SOleksij Rempel 	case ETH_SS_TEST:
28616016ba34SOleksij Rempel 		return net_selftest_get_count();
286238ae92dcSChris Healy 	default:
286338ae92dcSChris Healy 		return -EOPNOTSUPP;
286438ae92dcSChris Healy 	}
286538ae92dcSChris Healy }
2866f85de666SNikita Yushchenko 
28672b30842bSAndrew Lunn static void fec_enet_clear_ethtool_stats(struct net_device *dev)
28682b30842bSAndrew Lunn {
28692b30842bSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(dev);
28706970ef27SShenwei Wang 	struct fec_enet_priv_rx_q *rxq;
28716970ef27SShenwei Wang 	int i, j;
28722b30842bSAndrew Lunn 
28732b30842bSAndrew Lunn 	/* Disable MIB statistics counters */
28742b30842bSAndrew Lunn 	writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
28752b30842bSAndrew Lunn 
28762b30842bSAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
28772b30842bSAndrew Lunn 		writel(0, fep->hwp + fec_stats[i].offset);
28782b30842bSAndrew Lunn 
28796970ef27SShenwei Wang 	for (i = fep->num_rx_queues - 1; i >= 0; i--) {
28806970ef27SShenwei Wang 		rxq = fep->rx_queue[i];
28816970ef27SShenwei Wang 		for (j = 0; j < XDP_STATS_TOTAL; j++)
28826970ef27SShenwei Wang 			rxq->stats[j] = 0;
28836970ef27SShenwei Wang 	}
28846970ef27SShenwei Wang 
28852b30842bSAndrew Lunn 	/* Don't disable MIB statistics counters */
28862b30842bSAndrew Lunn 	writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
28872b30842bSAndrew Lunn }
28882b30842bSAndrew Lunn 
2889f85de666SNikita Yushchenko #else	/* !defined(CONFIG_M5272) */
2890f85de666SNikita Yushchenko #define FEC_STATS_SIZE	0
2891f85de666SNikita Yushchenko static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2892f85de666SNikita Yushchenko {
2893f85de666SNikita Yushchenko }
289441e8e404SFabio Estevam 
289541e8e404SFabio Estevam static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
289641e8e404SFabio Estevam {
289741e8e404SFabio Estevam }
2898d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
289938ae92dcSChris Healy 
2900d851b47bSFugang Duan /* ITR clock source is enet system clock (clk_ahb).
2901d851b47bSFugang Duan  * TCTT unit is cycle_ns * 64 cycle
2902d851b47bSFugang Duan  * So, the ICTT value = X us / (cycle_ns * 64)
2903d851b47bSFugang Duan  */
2904d851b47bSFugang Duan static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2905d851b47bSFugang Duan {
2906d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2907d851b47bSFugang Duan 
2908d851b47bSFugang Duan 	return us * (fep->itr_clk_rate / 64000) / 1000;
2909d851b47bSFugang Duan }
2910d851b47bSFugang Duan 
2911d851b47bSFugang Duan /* Set threshold for interrupt coalescing */
2912d851b47bSFugang Duan static void fec_enet_itr_coal_set(struct net_device *ndev)
2913d851b47bSFugang Duan {
2914d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2915d851b47bSFugang Duan 	int rx_itr, tx_itr;
2916d851b47bSFugang Duan 
2917d851b47bSFugang Duan 	/* Must be greater than zero to avoid unpredictable behavior */
2918d851b47bSFugang Duan 	if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2919d851b47bSFugang Duan 	    !fep->tx_time_itr || !fep->tx_pkts_itr)
2920d851b47bSFugang Duan 		return;
2921d851b47bSFugang Duan 
2922d851b47bSFugang Duan 	/* Select enet system clock as Interrupt Coalescing
2923d851b47bSFugang Duan 	 * timer Clock Source
2924d851b47bSFugang Duan 	 */
2925d851b47bSFugang Duan 	rx_itr = FEC_ITR_CLK_SEL;
2926d851b47bSFugang Duan 	tx_itr = FEC_ITR_CLK_SEL;
2927d851b47bSFugang Duan 
2928d851b47bSFugang Duan 	/* set ICFT and ICTT */
2929d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2930d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2931d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2932d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2933d851b47bSFugang Duan 
2934d851b47bSFugang Duan 	rx_itr |= FEC_ITR_EN;
2935d851b47bSFugang Duan 	tx_itr |= FEC_ITR_EN;
2936d851b47bSFugang Duan 
2937d851b47bSFugang Duan 	writel(tx_itr, fep->hwp + FEC_TXIC0);
2938d851b47bSFugang Duan 	writel(rx_itr, fep->hwp + FEC_RXIC0);
2939471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
2940d851b47bSFugang Duan 		writel(tx_itr, fep->hwp + FEC_TXIC1);
2941d851b47bSFugang Duan 		writel(rx_itr, fep->hwp + FEC_RXIC1);
2942d851b47bSFugang Duan 		writel(tx_itr, fep->hwp + FEC_TXIC2);
2943d851b47bSFugang Duan 		writel(rx_itr, fep->hwp + FEC_RXIC2);
2944d851b47bSFugang Duan 	}
2945ff7566b8SFugang Duan }
2946d851b47bSFugang Duan 
2947f3ccfda1SYufeng Mo static int fec_enet_get_coalesce(struct net_device *ndev,
2948f3ccfda1SYufeng Mo 				 struct ethtool_coalesce *ec,
2949f3ccfda1SYufeng Mo 				 struct kernel_ethtool_coalesce *kernel_coal,
2950f3ccfda1SYufeng Mo 				 struct netlink_ext_ack *extack)
2951d851b47bSFugang Duan {
2952d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2953d851b47bSFugang Duan 
2954ff7566b8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2955d851b47bSFugang Duan 		return -EOPNOTSUPP;
2956d851b47bSFugang Duan 
2957d851b47bSFugang Duan 	ec->rx_coalesce_usecs = fep->rx_time_itr;
2958d851b47bSFugang Duan 	ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2959d851b47bSFugang Duan 
2960d851b47bSFugang Duan 	ec->tx_coalesce_usecs = fep->tx_time_itr;
2961d851b47bSFugang Duan 	ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2962d851b47bSFugang Duan 
2963d851b47bSFugang Duan 	return 0;
2964d851b47bSFugang Duan }
2965d851b47bSFugang Duan 
2966f3ccfda1SYufeng Mo static int fec_enet_set_coalesce(struct net_device *ndev,
2967f3ccfda1SYufeng Mo 				 struct ethtool_coalesce *ec,
2968f3ccfda1SYufeng Mo 				 struct kernel_ethtool_coalesce *kernel_coal,
2969f3ccfda1SYufeng Mo 				 struct netlink_ext_ack *extack)
2970d851b47bSFugang Duan {
2971d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2972517a772cSFabio Estevam 	struct device *dev = &fep->pdev->dev;
2973d851b47bSFugang Duan 	unsigned int cycle;
2974d851b47bSFugang Duan 
2975ff7566b8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2976d851b47bSFugang Duan 		return -EOPNOTSUPP;
2977d851b47bSFugang Duan 
2978d851b47bSFugang Duan 	if (ec->rx_max_coalesced_frames > 255) {
2979517a772cSFabio Estevam 		dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
2980d851b47bSFugang Duan 		return -EINVAL;
2981d851b47bSFugang Duan 	}
2982d851b47bSFugang Duan 
2983d851b47bSFugang Duan 	if (ec->tx_max_coalesced_frames > 255) {
2984517a772cSFabio Estevam 		dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
2985d851b47bSFugang Duan 		return -EINVAL;
2986d851b47bSFugang Duan 	}
2987d851b47bSFugang Duan 
2988ab14961dSJakub Kicinski 	cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
2989d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
2990517a772cSFabio Estevam 		dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
2991d851b47bSFugang Duan 		return -EINVAL;
2992d851b47bSFugang Duan 	}
2993d851b47bSFugang Duan 
2994ab14961dSJakub Kicinski 	cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
2995d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
2996ab14961dSJakub Kicinski 		dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
2997d851b47bSFugang Duan 		return -EINVAL;
2998d851b47bSFugang Duan 	}
2999d851b47bSFugang Duan 
3000d851b47bSFugang Duan 	fep->rx_time_itr = ec->rx_coalesce_usecs;
3001d851b47bSFugang Duan 	fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
3002d851b47bSFugang Duan 
3003d851b47bSFugang Duan 	fep->tx_time_itr = ec->tx_coalesce_usecs;
3004d851b47bSFugang Duan 	fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
3005d851b47bSFugang Duan 
3006d851b47bSFugang Duan 	fec_enet_itr_coal_set(ndev);
3007d851b47bSFugang Duan 
3008d851b47bSFugang Duan 	return 0;
3009d851b47bSFugang Duan }
3010d851b47bSFugang Duan 
30111b7bde6dSNimrod Andy static int fec_enet_get_tunable(struct net_device *netdev,
30121b7bde6dSNimrod Andy 				const struct ethtool_tunable *tuna,
30131b7bde6dSNimrod Andy 				void *data)
30141b7bde6dSNimrod Andy {
30151b7bde6dSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
30161b7bde6dSNimrod Andy 	int ret = 0;
30171b7bde6dSNimrod Andy 
30181b7bde6dSNimrod Andy 	switch (tuna->id) {
30191b7bde6dSNimrod Andy 	case ETHTOOL_RX_COPYBREAK:
30201b7bde6dSNimrod Andy 		*(u32 *)data = fep->rx_copybreak;
30211b7bde6dSNimrod Andy 		break;
30221b7bde6dSNimrod Andy 	default:
30231b7bde6dSNimrod Andy 		ret = -EINVAL;
30241b7bde6dSNimrod Andy 		break;
30251b7bde6dSNimrod Andy 	}
30261b7bde6dSNimrod Andy 
30271b7bde6dSNimrod Andy 	return ret;
30281b7bde6dSNimrod Andy }
30291b7bde6dSNimrod Andy 
30301b7bde6dSNimrod Andy static int fec_enet_set_tunable(struct net_device *netdev,
30311b7bde6dSNimrod Andy 				const struct ethtool_tunable *tuna,
30321b7bde6dSNimrod Andy 				const void *data)
30331b7bde6dSNimrod Andy {
30341b7bde6dSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
30351b7bde6dSNimrod Andy 	int ret = 0;
30361b7bde6dSNimrod Andy 
30371b7bde6dSNimrod Andy 	switch (tuna->id) {
30381b7bde6dSNimrod Andy 	case ETHTOOL_RX_COPYBREAK:
30391b7bde6dSNimrod Andy 		fep->rx_copybreak = *(u32 *)data;
30401b7bde6dSNimrod Andy 		break;
30411b7bde6dSNimrod Andy 	default:
30421b7bde6dSNimrod Andy 		ret = -EINVAL;
30431b7bde6dSNimrod Andy 		break;
30441b7bde6dSNimrod Andy 	}
30451b7bde6dSNimrod Andy 
30461b7bde6dSNimrod Andy 	return ret;
30471b7bde6dSNimrod Andy }
30481b7bde6dSNimrod Andy 
3049b82f8c3fSFugang Duan /* LPI Sleep Ts count base on tx clk (clk_ref).
3050b82f8c3fSFugang Duan  * The lpi sleep cnt value = X us / (cycle_ns).
3051b82f8c3fSFugang Duan  */
3052b82f8c3fSFugang Duan static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
3053b82f8c3fSFugang Duan {
3054b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3055b82f8c3fSFugang Duan 
3056b82f8c3fSFugang Duan 	return us * (fep->clk_ref_rate / 1000) / 1000;
3057b82f8c3fSFugang Duan }
3058b82f8c3fSFugang Duan 
3059b82f8c3fSFugang Duan static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable)
3060b82f8c3fSFugang Duan {
3061b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3062b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
3063b82f8c3fSFugang Duan 	unsigned int sleep_cycle, wake_cycle;
3064b82f8c3fSFugang Duan 	int ret = 0;
3065b82f8c3fSFugang Duan 
3066b82f8c3fSFugang Duan 	if (enable) {
306753243d41SJisheng Zhang 		ret = phy_init_eee(ndev->phydev, false);
3068b82f8c3fSFugang Duan 		if (ret)
3069b82f8c3fSFugang Duan 			return ret;
3070b82f8c3fSFugang Duan 
3071b82f8c3fSFugang Duan 		sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer);
3072b82f8c3fSFugang Duan 		wake_cycle = sleep_cycle;
3073b82f8c3fSFugang Duan 	} else {
3074b82f8c3fSFugang Duan 		sleep_cycle = 0;
3075b82f8c3fSFugang Duan 		wake_cycle = 0;
3076b82f8c3fSFugang Duan 	}
3077b82f8c3fSFugang Duan 
3078b82f8c3fSFugang Duan 	p->tx_lpi_enabled = enable;
3079b82f8c3fSFugang Duan 	p->eee_enabled = enable;
3080b82f8c3fSFugang Duan 	p->eee_active = enable;
3081b82f8c3fSFugang Duan 
3082b82f8c3fSFugang Duan 	writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
3083b82f8c3fSFugang Duan 	writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
3084b82f8c3fSFugang Duan 
3085b82f8c3fSFugang Duan 	return 0;
3086b82f8c3fSFugang Duan }
3087b82f8c3fSFugang Duan 
3088b82f8c3fSFugang Duan static int
3089b82f8c3fSFugang Duan fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
3090b82f8c3fSFugang Duan {
3091b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3092b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
3093b82f8c3fSFugang Duan 
3094b82f8c3fSFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
3095b82f8c3fSFugang Duan 		return -EOPNOTSUPP;
3096b82f8c3fSFugang Duan 
3097b82f8c3fSFugang Duan 	if (!netif_running(ndev))
3098b82f8c3fSFugang Duan 		return -ENETDOWN;
3099b82f8c3fSFugang Duan 
3100b82f8c3fSFugang Duan 	edata->eee_enabled = p->eee_enabled;
3101b82f8c3fSFugang Duan 	edata->eee_active = p->eee_active;
3102b82f8c3fSFugang Duan 	edata->tx_lpi_timer = p->tx_lpi_timer;
3103b82f8c3fSFugang Duan 	edata->tx_lpi_enabled = p->tx_lpi_enabled;
3104b82f8c3fSFugang Duan 
3105b82f8c3fSFugang Duan 	return phy_ethtool_get_eee(ndev->phydev, edata);
3106b82f8c3fSFugang Duan }
3107b82f8c3fSFugang Duan 
3108b82f8c3fSFugang Duan static int
3109b82f8c3fSFugang Duan fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
3110b82f8c3fSFugang Duan {
3111b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3112b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
3113b82f8c3fSFugang Duan 	int ret = 0;
3114b82f8c3fSFugang Duan 
3115b82f8c3fSFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
3116b82f8c3fSFugang Duan 		return -EOPNOTSUPP;
3117b82f8c3fSFugang Duan 
3118b82f8c3fSFugang Duan 	if (!netif_running(ndev))
3119b82f8c3fSFugang Duan 		return -ENETDOWN;
3120b82f8c3fSFugang Duan 
3121b82f8c3fSFugang Duan 	p->tx_lpi_timer = edata->tx_lpi_timer;
3122b82f8c3fSFugang Duan 
3123b82f8c3fSFugang Duan 	if (!edata->eee_enabled || !edata->tx_lpi_enabled ||
3124b82f8c3fSFugang Duan 	    !edata->tx_lpi_timer)
3125b82f8c3fSFugang Duan 		ret = fec_enet_eee_mode_set(ndev, false);
3126b82f8c3fSFugang Duan 	else
3127b82f8c3fSFugang Duan 		ret = fec_enet_eee_mode_set(ndev, true);
3128b82f8c3fSFugang Duan 
3129b82f8c3fSFugang Duan 	if (ret)
3130b82f8c3fSFugang Duan 		return ret;
3131b82f8c3fSFugang Duan 
3132b82f8c3fSFugang Duan 	return phy_ethtool_set_eee(ndev->phydev, edata);
3133b82f8c3fSFugang Duan }
3134b82f8c3fSFugang Duan 
3135de40ed31SNimrod Andy static void
3136de40ed31SNimrod Andy fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
3137de40ed31SNimrod Andy {
3138de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
3139de40ed31SNimrod Andy 
3140de40ed31SNimrod Andy 	if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
3141de40ed31SNimrod Andy 		wol->supported = WAKE_MAGIC;
3142de40ed31SNimrod Andy 		wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
3143de40ed31SNimrod Andy 	} else {
3144de40ed31SNimrod Andy 		wol->supported = wol->wolopts = 0;
3145de40ed31SNimrod Andy 	}
3146de40ed31SNimrod Andy }
3147de40ed31SNimrod Andy 
3148de40ed31SNimrod Andy static int
3149de40ed31SNimrod Andy fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
3150de40ed31SNimrod Andy {
3151de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
3152de40ed31SNimrod Andy 
3153de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
3154de40ed31SNimrod Andy 		return -EINVAL;
3155de40ed31SNimrod Andy 
3156de40ed31SNimrod Andy 	if (wol->wolopts & ~WAKE_MAGIC)
3157de40ed31SNimrod Andy 		return -EINVAL;
3158de40ed31SNimrod Andy 
3159de40ed31SNimrod Andy 	device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
31600b6f65c7SJoakim Zhang 	if (device_may_wakeup(&ndev->dev))
3161de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
31620b6f65c7SJoakim Zhang 	else
3163de40ed31SNimrod Andy 		fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
3164de40ed31SNimrod Andy 
3165de40ed31SNimrod Andy 	return 0;
3166de40ed31SNimrod Andy }
3167de40ed31SNimrod Andy 
3168793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = {
3169d5e3c87dSJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3170d5e3c87dSJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES,
3171793fc096SFrank Li 	.get_drvinfo		= fec_enet_get_drvinfo,
3172db65f35fSPhilippe Reynes 	.get_regs_len		= fec_enet_get_regs_len,
3173db65f35fSPhilippe Reynes 	.get_regs		= fec_enet_get_regs,
317411d59289SFlorian Fainelli 	.nway_reset		= phy_ethtool_nway_reset,
3175c1d7c48fSRussell King 	.get_link		= ethtool_op_get_link,
3176d851b47bSFugang Duan 	.get_coalesce		= fec_enet_get_coalesce,
3177d851b47bSFugang Duan 	.set_coalesce		= fec_enet_set_coalesce,
317838ae92dcSChris Healy #ifndef CONFIG_M5272
3179c1d7c48fSRussell King 	.get_pauseparam		= fec_enet_get_pauseparam,
3180c1d7c48fSRussell King 	.set_pauseparam		= fec_enet_set_pauseparam,
318138ae92dcSChris Healy 	.get_strings		= fec_enet_get_strings,
3182c1d7c48fSRussell King 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
318338ae92dcSChris Healy 	.get_sset_count		= fec_enet_get_sset_count,
318438ae92dcSChris Healy #endif
3185c1d7c48fSRussell King 	.get_ts_info		= fec_enet_get_ts_info,
31861b7bde6dSNimrod Andy 	.get_tunable		= fec_enet_get_tunable,
31871b7bde6dSNimrod Andy 	.set_tunable		= fec_enet_set_tunable,
3188de40ed31SNimrod Andy 	.get_wol		= fec_enet_get_wol,
3189de40ed31SNimrod Andy 	.set_wol		= fec_enet_set_wol,
3190b82f8c3fSFugang Duan 	.get_eee		= fec_enet_get_eee,
3191b82f8c3fSFugang Duan 	.set_eee		= fec_enet_set_eee,
31929365fbf5SPhilippe Reynes 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
31939365fbf5SPhilippe Reynes 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
31946016ba34SOleksij Rempel 	.self_test		= net_selftest,
3195793fc096SFrank Li };
3196793fc096SFrank Li 
3197793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
3198793fc096SFrank Li {
3199793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
320045f5c327SPhilippe Reynes 	struct phy_device *phydev = ndev->phydev;
3201793fc096SFrank Li 
3202793fc096SFrank Li 	if (!netif_running(ndev))
3203793fc096SFrank Li 		return -EINVAL;
3204793fc096SFrank Li 
3205793fc096SFrank Li 	if (!phydev)
3206793fc096SFrank Li 		return -ENODEV;
3207793fc096SFrank Li 
32081d5244d0SBen Hutchings 	if (fep->bufdesc_ex) {
320934074639SSergey Organov 		bool use_fec_hwts = !phy_has_hwtstamp(phydev);
321034074639SSergey Organov 
321134074639SSergey Organov 		if (cmd == SIOCSHWTSTAMP) {
321234074639SSergey Organov 			if (use_fec_hwts)
32131d5244d0SBen Hutchings 				return fec_ptp_set(ndev, rq);
321434074639SSergey Organov 			fec_ptp_disable_hwts(ndev);
321534074639SSergey Organov 		} else if (cmd == SIOCGHWTSTAMP) {
321634074639SSergey Organov 			if (use_fec_hwts)
32171d5244d0SBen Hutchings 				return fec_ptp_get(ndev, rq);
32181d5244d0SBen Hutchings 		}
321934074639SSergey Organov 	}
3220793fc096SFrank Li 
3221793fc096SFrank Li 	return phy_mii_ioctl(phydev, rq, cmd);
3222793fc096SFrank Li }
3223793fc096SFrank Li 
3224793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev)
3225793fc096SFrank Li {
3226793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3227793fc096SFrank Li 	unsigned int i;
3228793fc096SFrank Li 	struct sk_buff *skb;
32294d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
32304d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
323159d0f746SFrank Li 	unsigned int q;
3232793fc096SFrank Li 
323359d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
323459d0f746SFrank Li 		rxq = fep->rx_queue[q];
323595698ff6SShenwei Wang 		for (i = 0; i < rxq->bd.ring_size; i++)
3236e38553bdSWei Fang 			page_pool_put_full_page(rxq->page_pool, rxq->rx_skb_info[i].page, false);
323795698ff6SShenwei Wang 
32386970ef27SShenwei Wang 		for (i = 0; i < XDP_STATS_TOTAL; i++)
32396970ef27SShenwei Wang 			rxq->stats[i] = 0;
32406970ef27SShenwei Wang 
324195698ff6SShenwei Wang 		if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
324295698ff6SShenwei Wang 			xdp_rxq_info_unreg(&rxq->xdp_rxq);
324395698ff6SShenwei Wang 		page_pool_destroy(rxq->page_pool);
324495698ff6SShenwei Wang 		rxq->page_pool = NULL;
3245793fc096SFrank Li 	}
3246793fc096SFrank Li 
324759d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
324859d0f746SFrank Li 		txq = fep->tx_queue[q];
32497355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
32504d494cdcSFugang Duan 			kfree(txq->tx_bounce[i]);
32514d494cdcSFugang Duan 			txq->tx_bounce[i] = NULL;
32524d494cdcSFugang Duan 			skb = txq->tx_skbuff[i];
32534d494cdcSFugang Duan 			txq->tx_skbuff[i] = NULL;
32548b7c9efaSRussell King 			dev_kfree_skb(skb);
32558b7c9efaSRussell King 		}
3256793fc096SFrank Li 	}
325759d0f746SFrank Li }
3258793fc096SFrank Li 
325959d0f746SFrank Li static void fec_enet_free_queue(struct net_device *ndev)
326059d0f746SFrank Li {
326159d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
326259d0f746SFrank Li 	int i;
326359d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
326459d0f746SFrank Li 
326559d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
326659d0f746SFrank Li 		if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
326759d0f746SFrank Li 			txq = fep->tx_queue[i];
326894920128SFugang Duan 			dma_free_coherent(&fep->pdev->dev,
32697355f276STroy Kisky 					  txq->bd.ring_size * TSO_HEADER_SIZE,
327059d0f746SFrank Li 					  txq->tso_hdrs,
327159d0f746SFrank Li 					  txq->tso_hdrs_dma);
327259d0f746SFrank Li 		}
327359d0f746SFrank Li 
327459d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
327559d0f746SFrank Li 		kfree(fep->rx_queue[i]);
327659d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
327759d0f746SFrank Li 		kfree(fep->tx_queue[i]);
327859d0f746SFrank Li }
327959d0f746SFrank Li 
328059d0f746SFrank Li static int fec_enet_alloc_queue(struct net_device *ndev)
328159d0f746SFrank Li {
328259d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
328359d0f746SFrank Li 	int i;
328459d0f746SFrank Li 	int ret = 0;
328559d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
328659d0f746SFrank Li 
328759d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
328859d0f746SFrank Li 		txq = kzalloc(sizeof(*txq), GFP_KERNEL);
328959d0f746SFrank Li 		if (!txq) {
329059d0f746SFrank Li 			ret = -ENOMEM;
329159d0f746SFrank Li 			goto alloc_failed;
329259d0f746SFrank Li 		}
329359d0f746SFrank Li 
329459d0f746SFrank Li 		fep->tx_queue[i] = txq;
32957355f276STroy Kisky 		txq->bd.ring_size = TX_RING_SIZE;
32967355f276STroy Kisky 		fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
329759d0f746SFrank Li 
329859d0f746SFrank Li 		txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
329959d0f746SFrank Li 		txq->tx_wake_threshold =
33007355f276STroy Kisky 			(txq->bd.ring_size - txq->tx_stop_threshold) / 2;
330159d0f746SFrank Li 
330294920128SFugang Duan 		txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
33037355f276STroy Kisky 					txq->bd.ring_size * TSO_HEADER_SIZE,
330459d0f746SFrank Li 					&txq->tso_hdrs_dma,
330559d0f746SFrank Li 					GFP_KERNEL);
330659d0f746SFrank Li 		if (!txq->tso_hdrs) {
330759d0f746SFrank Li 			ret = -ENOMEM;
330859d0f746SFrank Li 			goto alloc_failed;
330959d0f746SFrank Li 		}
331059d0f746SFrank Li 	}
331159d0f746SFrank Li 
331259d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
331359d0f746SFrank Li 		fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
331459d0f746SFrank Li 					   GFP_KERNEL);
331559d0f746SFrank Li 		if (!fep->rx_queue[i]) {
331659d0f746SFrank Li 			ret = -ENOMEM;
331759d0f746SFrank Li 			goto alloc_failed;
331859d0f746SFrank Li 		}
331959d0f746SFrank Li 
33207355f276STroy Kisky 		fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
33217355f276STroy Kisky 		fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
332259d0f746SFrank Li 	}
332359d0f746SFrank Li 	return ret;
332459d0f746SFrank Li 
332559d0f746SFrank Li alloc_failed:
332659d0f746SFrank Li 	fec_enet_free_queue(ndev);
332759d0f746SFrank Li 	return ret;
332859d0f746SFrank Li }
332959d0f746SFrank Li 
333059d0f746SFrank Li static int
333159d0f746SFrank Li fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
3332793fc096SFrank Li {
3333793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
33344d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
333595698ff6SShenwei Wang 	dma_addr_t phys_addr;
333695698ff6SShenwei Wang 	struct bufdesc	*bdp;
333795698ff6SShenwei Wang 	struct page *page;
333895698ff6SShenwei Wang 	int i, err;
3339793fc096SFrank Li 
334059d0f746SFrank Li 	rxq = fep->rx_queue[queue];
33417355f276STroy Kisky 	bdp = rxq->bd.base;
3342793fc096SFrank Li 
334395698ff6SShenwei Wang 	err = fec_enet_create_page_pool(fep, rxq, rxq->bd.ring_size);
334495698ff6SShenwei Wang 	if (err < 0) {
334595698ff6SShenwei Wang 		netdev_err(ndev, "%s failed queue %d (%d)\n", __func__, queue, err);
334695698ff6SShenwei Wang 		return err;
3347d842a31fSDuan Fugang-B38611 	}
3348730ee360SRussell King 
334995698ff6SShenwei Wang 	for (i = 0; i < rxq->bd.ring_size; i++) {
335095698ff6SShenwei Wang 		page = page_pool_dev_alloc_pages(rxq->page_pool);
335195698ff6SShenwei Wang 		if (!page)
335295698ff6SShenwei Wang 			goto err_alloc;
335395698ff6SShenwei Wang 
335495698ff6SShenwei Wang 		phys_addr = page_pool_get_dma_addr(page) + FEC_ENET_XDP_HEADROOM;
335595698ff6SShenwei Wang 		bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
335695698ff6SShenwei Wang 
335795698ff6SShenwei Wang 		rxq->rx_skb_info[i].page = page;
335895698ff6SShenwei Wang 		rxq->rx_skb_info[i].offset = FEC_ENET_XDP_HEADROOM;
33595cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
3360793fc096SFrank Li 
3361793fc096SFrank Li 		if (fep->bufdesc_ex) {
3362793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
33635cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
3364793fc096SFrank Li 		}
3365793fc096SFrank Li 
33667355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
3367793fc096SFrank Li 	}
3368793fc096SFrank Li 
3369793fc096SFrank Li 	/* Set the last buffer to wrap. */
33707355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
33715cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
337259d0f746SFrank Li 	return 0;
3373793fc096SFrank Li 
337459d0f746SFrank Li  err_alloc:
337559d0f746SFrank Li 	fec_enet_free_buffers(ndev);
337659d0f746SFrank Li 	return -ENOMEM;
337759d0f746SFrank Li }
337859d0f746SFrank Li 
337959d0f746SFrank Li static int
338059d0f746SFrank Li fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
338159d0f746SFrank Li {
338259d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
338359d0f746SFrank Li 	unsigned int i;
338459d0f746SFrank Li 	struct bufdesc  *bdp;
338559d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
338659d0f746SFrank Li 
338759d0f746SFrank Li 	txq = fep->tx_queue[queue];
33887355f276STroy Kisky 	bdp = txq->bd.base;
33897355f276STroy Kisky 	for (i = 0; i < txq->bd.ring_size; i++) {
33904d494cdcSFugang Duan 		txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
33914d494cdcSFugang Duan 		if (!txq->tx_bounce[i])
3392ffdce2ccSRussell King 			goto err_alloc;
3393793fc096SFrank Li 
33945cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(0);
33955cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
3396793fc096SFrank Li 
3397793fc096SFrank Li 		if (fep->bufdesc_ex) {
3398793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
33995cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
3400793fc096SFrank Li 		}
3401793fc096SFrank Li 
34027355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3403793fc096SFrank Li 	}
3404793fc096SFrank Li 
3405793fc096SFrank Li 	/* Set the last buffer to wrap. */
34067355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
34075cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3408793fc096SFrank Li 
3409793fc096SFrank Li 	return 0;
3410ffdce2ccSRussell King 
3411ffdce2ccSRussell King  err_alloc:
3412ffdce2ccSRussell King 	fec_enet_free_buffers(ndev);
3413ffdce2ccSRussell King 	return -ENOMEM;
3414793fc096SFrank Li }
3415793fc096SFrank Li 
341659d0f746SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev)
341759d0f746SFrank Li {
341859d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
341959d0f746SFrank Li 	unsigned int i;
342059d0f746SFrank Li 
342159d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
342259d0f746SFrank Li 		if (fec_enet_alloc_rxq_buffers(ndev, i))
342359d0f746SFrank Li 			return -ENOMEM;
342459d0f746SFrank Li 
342559d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
342659d0f746SFrank Li 		if (fec_enet_alloc_txq_buffers(ndev, i))
342759d0f746SFrank Li 			return -ENOMEM;
342859d0f746SFrank Li 	return 0;
342959d0f746SFrank Li }
343059d0f746SFrank Li 
3431793fc096SFrank Li static int
3432793fc096SFrank Li fec_enet_open(struct net_device *ndev)
3433793fc096SFrank Li {
3434793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3435793fc096SFrank Li 	int ret;
34361b0a83acSRichard Leitner 	bool reset_again;
3437793fc096SFrank Li 
3438da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(&fep->pdev->dev);
3439b0c6ce24SFabio Estevam 	if (ret < 0)
34408fff755eSAndrew Lunn 		return ret;
34418fff755eSAndrew Lunn 
34425bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&fep->pdev->dev);
3443e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
3444e8fcfcd5SNimrod Andy 	if (ret)
34458fff755eSAndrew Lunn 		goto clk_enable;
3446e8fcfcd5SNimrod Andy 
34471b0a83acSRichard Leitner 	/* During the first fec_enet_open call the PHY isn't probed at this
34481b0a83acSRichard Leitner 	 * point. Therefore the phy_reset_after_clk_enable() call within
34491b0a83acSRichard Leitner 	 * fec_enet_clk_enable() fails. As we need this reset in order to be
34501b0a83acSRichard Leitner 	 * sure the PHY is working correctly we check if we need to reset again
34511b0a83acSRichard Leitner 	 * later when the PHY is probed
34521b0a83acSRichard Leitner 	 */
34531b0a83acSRichard Leitner 	if (ndev->phydev && ndev->phydev->drv)
34541b0a83acSRichard Leitner 		reset_again = false;
34551b0a83acSRichard Leitner 	else
34561b0a83acSRichard Leitner 		reset_again = true;
34571b0a83acSRichard Leitner 
3458793fc096SFrank Li 	/* I should reset the ring buffers here, but I don't yet know
3459793fc096SFrank Li 	 * a simple way to do that.
3460793fc096SFrank Li 	 */
3461793fc096SFrank Li 
3462793fc096SFrank Li 	ret = fec_enet_alloc_buffers(ndev);
3463793fc096SFrank Li 	if (ret)
3464681d2421SFabio Estevam 		goto err_enet_alloc;
3465793fc096SFrank Li 
346655dd2753SNimrod Andy 	/* Init MAC prior to mii bus probe */
346755dd2753SNimrod Andy 	fec_restart(ndev);
346855dd2753SNimrod Andy 
34691b0a83acSRichard Leitner 	/* Call phy_reset_after_clk_enable() again if it failed during
34701b0a83acSRichard Leitner 	 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
34711b0a83acSRichard Leitner 	 */
34721b0a83acSRichard Leitner 	if (reset_again)
347364a632daSMarek Vasut 		fec_enet_phy_reset_after_clk_enable(ndev);
34741b0a83acSRichard Leitner 
34750da1ccbbSMarek Vasut 	/* Probe and connect to PHY when open the interface */
34760da1ccbbSMarek Vasut 	ret = fec_enet_mii_probe(ndev);
34770da1ccbbSMarek Vasut 	if (ret)
34780da1ccbbSMarek Vasut 		goto err_enet_mii_probe;
3479681d2421SFabio Estevam 
3480681d2421SFabio Estevam 	if (fep->quirks & FEC_QUIRK_ERR006687)
3481793fc096SFrank Li 		imx6q_cpuidle_fec_irqs_used();
3482793fc096SFrank Li 
34837d650df9SWei Fang 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
34847d650df9SWei Fang 		cpu_latency_qos_add_request(&fep->pm_qos_req, 0);
34857d650df9SWei Fang 
3486793fc096SFrank Li 	napi_enable(&fep->napi);
3487793fc096SFrank Li 	phy_start(ndev->phydev);
3488793fc096SFrank Li 	netif_tx_start_all_queues(ndev);
3489793fc096SFrank Li 
3490793fc096SFrank Li 	device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
3491793fc096SFrank Li 				 FEC_WOL_FLAG_ENABLE);
3492793fc096SFrank Li 
3493793fc096SFrank Li 	return 0;
3494793fc096SFrank Li 
3495793fc096SFrank Li err_enet_mii_probe:
3496793fc096SFrank Li 	fec_enet_free_buffers(ndev);
3497793fc096SFrank Li err_enet_alloc:
3498793fc096SFrank Li 	fec_enet_clk_enable(ndev, false);
3499793fc096SFrank Li clk_enable:
3500793fc096SFrank Li 	pm_runtime_mark_last_busy(&fep->pdev->dev);
3501793fc096SFrank Li 	pm_runtime_put_autosuspend(&fep->pdev->dev);
3502793fc096SFrank Li 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3503793fc096SFrank Li 	return ret;
3504d76cfae9SRussell King }
3505d76cfae9SRussell King 
350631a6de34SRussell King static int
3507793fc096SFrank Li fec_enet_close(struct net_device *ndev)
3508b49cd504SRussell King {
3509793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
351031a6de34SRussell King 
351145f5c327SPhilippe Reynes 	phy_stop(ndev->phydev);
3512793fc096SFrank Li 
3513793fc096SFrank Li 	if (netif_device_present(ndev)) {
3514793fc096SFrank Li 		napi_disable(&fep->napi);
3515793fc096SFrank Li 		netif_tx_disable(ndev);
3516793fc096SFrank Li 		fec_stop(ndev);
3517793fc096SFrank Li 	}
3518793fc096SFrank Li 
351945f5c327SPhilippe Reynes 	phy_disconnect(ndev->phydev);
3520793fc096SFrank Li 
352129380905SLucas Stach 	if (fep->quirks & FEC_QUIRK_ERR006687)
352229380905SLucas Stach 		imx6q_cpuidle_fec_irqs_unused();
352329380905SLucas Stach 
352480cca775SNikita Yushchenko 	fec_enet_update_ethtool_stats(ndev);
352580cca775SNikita Yushchenko 
3526e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
35277d650df9SWei Fang 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
35287d650df9SWei Fang 		cpu_latency_qos_remove_request(&fep->pm_qos_req);
35297d650df9SWei Fang 
35305bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
35318fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&fep->pdev->dev);
35328fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&fep->pdev->dev);
35338fff755eSAndrew Lunn 
3534793fc096SFrank Li 	fec_enet_free_buffers(ndev);
3535793fc096SFrank Li 
3536793fc096SFrank Li 	return 0;
3537793fc096SFrank Li }
3538793fc096SFrank Li 
3539793fc096SFrank Li /* Set or clear the multicast filter for this adaptor.
3540793fc096SFrank Li  * Skeleton taken from sunlance driver.
3541793fc096SFrank Li  * The CPM Ethernet implementation allows Multicast as well as individual
3542793fc096SFrank Li  * MAC address filtering.  Some of the drivers check to make sure it is
3543793fc096SFrank Li  * a group multicast address, and discard those that are not.  I guess I
3544793fc096SFrank Li  * will do the same for now, but just remove the test if you want
3545793fc096SFrank Li  * individual filtering as well (do the upper net layers want or support
3546793fc096SFrank Li  * this kind of feature?).
3547793fc096SFrank Li  */
3548793fc096SFrank Li 
35496176e89cSJiri Kosina #define FEC_HASH_BITS	6		/* #bits in hash */
3550793fc096SFrank Li 
3551793fc096SFrank Li static void set_multicast_list(struct net_device *ndev)
3552793fc096SFrank Li {
3553793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3554793fc096SFrank Li 	struct netdev_hw_addr *ha;
355516f6e983SKrzysztof Kozlowski 	unsigned int crc, tmp;
3556793fc096SFrank Li 	unsigned char hash;
355701f8902bSRui Sousa 	unsigned int hash_high = 0, hash_low = 0;
3558793fc096SFrank Li 
3559793fc096SFrank Li 	if (ndev->flags & IFF_PROMISC) {
3560793fc096SFrank Li 		tmp = readl(fep->hwp + FEC_R_CNTRL);
3561793fc096SFrank Li 		tmp |= 0x8;
3562793fc096SFrank Li 		writel(tmp, fep->hwp + FEC_R_CNTRL);
3563793fc096SFrank Li 		return;
3564793fc096SFrank Li 	}
3565793fc096SFrank Li 
3566793fc096SFrank Li 	tmp = readl(fep->hwp + FEC_R_CNTRL);
3567793fc096SFrank Li 	tmp &= ~0x8;
3568793fc096SFrank Li 	writel(tmp, fep->hwp + FEC_R_CNTRL);
3569793fc096SFrank Li 
3570793fc096SFrank Li 	if (ndev->flags & IFF_ALLMULTI) {
3571793fc096SFrank Li 		/* Catch all multicast addresses, so set the
3572793fc096SFrank Li 		 * filter to all 1's
3573793fc096SFrank Li 		 */
3574793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3575793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3576793fc096SFrank Li 
3577793fc096SFrank Li 		return;
3578793fc096SFrank Li 	}
3579793fc096SFrank Li 
358001f8902bSRui Sousa 	/* Add the addresses in hash register */
3581793fc096SFrank Li 	netdev_for_each_mc_addr(ha, ndev) {
3582793fc096SFrank Li 		/* calculate crc32 value of mac address */
358316f6e983SKrzysztof Kozlowski 		crc = ether_crc_le(ndev->addr_len, ha->addr);
3584793fc096SFrank Li 
35856176e89cSJiri Kosina 		/* only upper 6 bits (FEC_HASH_BITS) are used
3586981a0547SPeter Meerwald-Stadler 		 * which point to specific bit in the hash registers
3587793fc096SFrank Li 		 */
35886176e89cSJiri Kosina 		hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
3589793fc096SFrank Li 
359001f8902bSRui Sousa 		if (hash > 31)
359101f8902bSRui Sousa 			hash_high |= 1 << (hash - 32);
359201f8902bSRui Sousa 		else
359301f8902bSRui Sousa 			hash_low |= 1 << hash;
3594793fc096SFrank Li 	}
359501f8902bSRui Sousa 
359601f8902bSRui Sousa 	writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
359701f8902bSRui Sousa 	writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3598793fc096SFrank Li }
3599793fc096SFrank Li 
3600793fc096SFrank Li /* Set a MAC change in hardware. */
3601793fc096SFrank Li static int
3602793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p)
3603793fc096SFrank Li {
3604793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3605793fc096SFrank Li 	struct sockaddr *addr = p;
3606793fc096SFrank Li 
360744934facSLucas Stach 	if (addr) {
3608793fc096SFrank Li 		if (!is_valid_ether_addr(addr->sa_data))
3609793fc096SFrank Li 			return -EADDRNOTAVAIL;
3610a05e4c0aSJakub Kicinski 		eth_hw_addr_set(ndev, addr->sa_data);
361144934facSLucas Stach 	}
3612793fc096SFrank Li 
36139638d19eSNimrod Andy 	/* Add netif status check here to avoid system hang in below case:
36149638d19eSNimrod Andy 	 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
36159638d19eSNimrod Andy 	 * After ethx down, fec all clocks are gated off and then register
36169638d19eSNimrod Andy 	 * access causes system hang.
36179638d19eSNimrod Andy 	 */
36189638d19eSNimrod Andy 	if (!netif_running(ndev))
36199638d19eSNimrod Andy 		return 0;
36209638d19eSNimrod Andy 
3621793fc096SFrank Li 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3622793fc096SFrank Li 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3623793fc096SFrank Li 		fep->hwp + FEC_ADDR_LOW);
3624793fc096SFrank Li 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3625793fc096SFrank Li 		fep->hwp + FEC_ADDR_HIGH);
3626793fc096SFrank Li 	return 0;
3627793fc096SFrank Li }
3628793fc096SFrank Li 
3629793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3630793fc096SFrank Li /**
3631793fc096SFrank Li  * fec_poll_controller - FEC Poll controller function
3632793fc096SFrank Li  * @dev: The FEC network adapter
3633793fc096SFrank Li  *
3634793fc096SFrank Li  * Polled functionality used by netconsole and others in non interrupt mode
3635793fc096SFrank Li  *
3636793fc096SFrank Li  */
3637793fc096SFrank Li static void fec_poll_controller(struct net_device *dev)
3638793fc096SFrank Li {
3639793fc096SFrank Li 	int i;
3640793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(dev);
3641793fc096SFrank Li 
3642793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
3643793fc096SFrank Li 		if (fep->irq[i] > 0) {
3644793fc096SFrank Li 			disable_irq(fep->irq[i]);
3645793fc096SFrank Li 			fec_enet_interrupt(fep->irq[i], dev);
3646793fc096SFrank Li 			enable_irq(fep->irq[i]);
3647793fc096SFrank Li 		}
3648793fc096SFrank Li 	}
3649793fc096SFrank Li }
3650793fc096SFrank Li #endif
3651793fc096SFrank Li 
36525bc26726SNimrod Andy static inline void fec_enet_set_netdev_features(struct net_device *netdev,
36534c09eed9SJim Baxter 	netdev_features_t features)
36544c09eed9SJim Baxter {
36554c09eed9SJim Baxter 	struct fec_enet_private *fep = netdev_priv(netdev);
36564c09eed9SJim Baxter 	netdev_features_t changed = features ^ netdev->features;
36574c09eed9SJim Baxter 
36584c09eed9SJim Baxter 	netdev->features = features;
36594c09eed9SJim Baxter 
36604c09eed9SJim Baxter 	/* Receive checksum has been changed */
36614c09eed9SJim Baxter 	if (changed & NETIF_F_RXCSUM) {
36624c09eed9SJim Baxter 		if (features & NETIF_F_RXCSUM)
36634c09eed9SJim Baxter 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
36644c09eed9SJim Baxter 		else
36654c09eed9SJim Baxter 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
36668506fa1dSRussell King 	}
36675bc26726SNimrod Andy }
36684c09eed9SJim Baxter 
36695bc26726SNimrod Andy static int fec_set_features(struct net_device *netdev,
36705bc26726SNimrod Andy 	netdev_features_t features)
36715bc26726SNimrod Andy {
36725bc26726SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
36735bc26726SNimrod Andy 	netdev_features_t changed = features ^ netdev->features;
36745bc26726SNimrod Andy 
36755b40f709SFabio Estevam 	if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
36765bc26726SNimrod Andy 		napi_disable(&fep->napi);
36775bc26726SNimrod Andy 		netif_tx_lock_bh(netdev);
36785bc26726SNimrod Andy 		fec_stop(netdev);
36795bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
3680ef83337dSRussell King 		fec_restart(netdev);
36814d494cdcSFugang Duan 		netif_tx_wake_all_queues(netdev);
36826af42d42SRussell King 		netif_tx_unlock_bh(netdev);
3683dbc64a8eSRussell King 		napi_enable(&fep->napi);
36845bc26726SNimrod Andy 	} else {
36855bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
36864c09eed9SJim Baxter 	}
36874c09eed9SJim Baxter 
36884c09eed9SJim Baxter 	return 0;
36894c09eed9SJim Baxter }
36904c09eed9SJim Baxter 
369152c4a1a8SFugang Duan static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb)
369252c4a1a8SFugang Duan {
369352c4a1a8SFugang Duan 	struct vlan_ethhdr *vhdr;
369452c4a1a8SFugang Duan 	unsigned short vlan_TCI = 0;
369552c4a1a8SFugang Duan 
369652c4a1a8SFugang Duan 	if (skb->protocol == htons(ETH_P_ALL)) {
369752c4a1a8SFugang Duan 		vhdr = (struct vlan_ethhdr *)(skb->data);
369852c4a1a8SFugang Duan 		vlan_TCI = ntohs(vhdr->h_vlan_TCI);
369952c4a1a8SFugang Duan 	}
370052c4a1a8SFugang Duan 
370152c4a1a8SFugang Duan 	return vlan_TCI;
370252c4a1a8SFugang Duan }
370352c4a1a8SFugang Duan 
370452c4a1a8SFugang Duan static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
370552c4a1a8SFugang Duan 				 struct net_device *sb_dev)
370652c4a1a8SFugang Duan {
370752c4a1a8SFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
370852c4a1a8SFugang Duan 	u16 vlan_tag;
370952c4a1a8SFugang Duan 
371052c4a1a8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
371152c4a1a8SFugang Duan 		return netdev_pick_tx(ndev, skb, NULL);
371252c4a1a8SFugang Duan 
371352c4a1a8SFugang Duan 	vlan_tag = fec_enet_get_raw_vlan_tci(skb);
371452c4a1a8SFugang Duan 	if (!vlan_tag)
371552c4a1a8SFugang Duan 		return vlan_tag;
371652c4a1a8SFugang Duan 
371752c4a1a8SFugang Duan 	return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
371852c4a1a8SFugang Duan }
371952c4a1a8SFugang Duan 
37206d6b39f1SShenwei Wang static int fec_enet_bpf(struct net_device *dev, struct netdev_bpf *bpf)
37216d6b39f1SShenwei Wang {
37226d6b39f1SShenwei Wang 	struct fec_enet_private *fep = netdev_priv(dev);
37236d6b39f1SShenwei Wang 	bool is_run = netif_running(dev);
37246d6b39f1SShenwei Wang 	struct bpf_prog *old_prog;
37256d6b39f1SShenwei Wang 
37266d6b39f1SShenwei Wang 	switch (bpf->command) {
37276d6b39f1SShenwei Wang 	case XDP_SETUP_PROG:
37286c8fae0cSShenwei Wang 		/* No need to support the SoCs that require to
37296c8fae0cSShenwei Wang 		 * do the frame swap because the performance wouldn't be
37306c8fae0cSShenwei Wang 		 * better than the skb mode.
37316c8fae0cSShenwei Wang 		 */
37326c8fae0cSShenwei Wang 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
37336c8fae0cSShenwei Wang 			return -EOPNOTSUPP;
37346c8fae0cSShenwei Wang 
37356d6b39f1SShenwei Wang 		if (is_run) {
37366d6b39f1SShenwei Wang 			napi_disable(&fep->napi);
37376d6b39f1SShenwei Wang 			netif_tx_disable(dev);
37386d6b39f1SShenwei Wang 		}
37396d6b39f1SShenwei Wang 
37406d6b39f1SShenwei Wang 		old_prog = xchg(&fep->xdp_prog, bpf->prog);
37416d6b39f1SShenwei Wang 		fec_restart(dev);
37426d6b39f1SShenwei Wang 
37436d6b39f1SShenwei Wang 		if (is_run) {
37446d6b39f1SShenwei Wang 			napi_enable(&fep->napi);
37456d6b39f1SShenwei Wang 			netif_tx_start_all_queues(dev);
37466d6b39f1SShenwei Wang 		}
37476d6b39f1SShenwei Wang 
37486d6b39f1SShenwei Wang 		if (old_prog)
37496d6b39f1SShenwei Wang 			bpf_prog_put(old_prog);
37506d6b39f1SShenwei Wang 
37516d6b39f1SShenwei Wang 		return 0;
37526d6b39f1SShenwei Wang 
37536d6b39f1SShenwei Wang 	case XDP_SETUP_XSK_POOL:
37546d6b39f1SShenwei Wang 		return -EOPNOTSUPP;
37556d6b39f1SShenwei Wang 
37566d6b39f1SShenwei Wang 	default:
37576d6b39f1SShenwei Wang 		return -EOPNOTSUPP;
37586d6b39f1SShenwei Wang 	}
37596d6b39f1SShenwei Wang }
37606d6b39f1SShenwei Wang 
37616d6b39f1SShenwei Wang static int
37626c8fae0cSShenwei Wang fec_enet_xdp_get_tx_queue(struct fec_enet_private *fep, int index)
37636d6b39f1SShenwei Wang {
37646d6b39f1SShenwei Wang 	if (unlikely(index < 0))
37656c8fae0cSShenwei Wang 		return 0;
37666d6b39f1SShenwei Wang 
37676c8fae0cSShenwei Wang 	return (index % fep->num_tx_queues);
37686d6b39f1SShenwei Wang }
37696d6b39f1SShenwei Wang 
37706d6b39f1SShenwei Wang static int fec_enet_txq_xmit_frame(struct fec_enet_private *fep,
37716d6b39f1SShenwei Wang 				   struct fec_enet_priv_tx_q *txq,
37726d6b39f1SShenwei Wang 				   struct xdp_frame *frame)
37736d6b39f1SShenwei Wang {
37746d6b39f1SShenwei Wang 	unsigned int index, status, estatus;
3775bc638eabSWei Fang 	struct bufdesc *bdp;
37766d6b39f1SShenwei Wang 	dma_addr_t dma_addr;
37776d6b39f1SShenwei Wang 	int entries_free;
37786d6b39f1SShenwei Wang 
37796d6b39f1SShenwei Wang 	entries_free = fec_enet_get_free_txdesc_num(txq);
37806d6b39f1SShenwei Wang 	if (entries_free < MAX_SKB_FRAGS + 1) {
37816d6b39f1SShenwei Wang 		netdev_err(fep->netdev, "NOT enough BD for SG!\n");
3782144470c8SShenwei Wang 		return -EBUSY;
37836d6b39f1SShenwei Wang 	}
37846d6b39f1SShenwei Wang 
37856d6b39f1SShenwei Wang 	/* Fill in a Tx ring entry */
37866d6b39f1SShenwei Wang 	bdp = txq->bd.cur;
37876d6b39f1SShenwei Wang 	status = fec16_to_cpu(bdp->cbd_sc);
37886d6b39f1SShenwei Wang 	status &= ~BD_ENET_TX_STATS;
37896d6b39f1SShenwei Wang 
37906d6b39f1SShenwei Wang 	index = fec_enet_get_bd_index(bdp, &txq->bd);
37916d6b39f1SShenwei Wang 
37926d6b39f1SShenwei Wang 	dma_addr = dma_map_single(&fep->pdev->dev, frame->data,
37936d6b39f1SShenwei Wang 				  frame->len, DMA_TO_DEVICE);
37946d6b39f1SShenwei Wang 	if (dma_mapping_error(&fep->pdev->dev, dma_addr))
3795144470c8SShenwei Wang 		return -ENOMEM;
37966d6b39f1SShenwei Wang 
37976d6b39f1SShenwei Wang 	status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
37986d6b39f1SShenwei Wang 	if (fep->bufdesc_ex)
37996d6b39f1SShenwei Wang 		estatus = BD_ENET_TX_INT;
38006d6b39f1SShenwei Wang 
38016d6b39f1SShenwei Wang 	bdp->cbd_bufaddr = cpu_to_fec32(dma_addr);
38026d6b39f1SShenwei Wang 	bdp->cbd_datlen = cpu_to_fec16(frame->len);
38036d6b39f1SShenwei Wang 
38046d6b39f1SShenwei Wang 	if (fep->bufdesc_ex) {
38056d6b39f1SShenwei Wang 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
38066d6b39f1SShenwei Wang 
38076d6b39f1SShenwei Wang 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
38086d6b39f1SShenwei Wang 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
38096d6b39f1SShenwei Wang 
38106d6b39f1SShenwei Wang 		ebdp->cbd_bdu = 0;
38116d6b39f1SShenwei Wang 		ebdp->cbd_esc = cpu_to_fec32(estatus);
38126d6b39f1SShenwei Wang 	}
38136d6b39f1SShenwei Wang 
38146d6b39f1SShenwei Wang 	txq->tx_skbuff[index] = NULL;
38156d6b39f1SShenwei Wang 
38169025944fSShenwei Wang 	/* Make sure the updates to rest of the descriptor are performed before
38179025944fSShenwei Wang 	 * transferring ownership.
38189025944fSShenwei Wang 	 */
38199025944fSShenwei Wang 	dma_wmb();
38209025944fSShenwei Wang 
38216d6b39f1SShenwei Wang 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
38226d6b39f1SShenwei Wang 	 * it's the last BD of the frame, and to put the CRC on the end.
38236d6b39f1SShenwei Wang 	 */
38246d6b39f1SShenwei Wang 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
38256d6b39f1SShenwei Wang 	bdp->cbd_sc = cpu_to_fec16(status);
38266d6b39f1SShenwei Wang 
38276d6b39f1SShenwei Wang 	/* If this was the last BD in the ring, start at the beginning again. */
3828bc638eabSWei Fang 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
38296d6b39f1SShenwei Wang 
38309025944fSShenwei Wang 	/* Make sure the update to bdp are performed before txq->bd.cur. */
38319025944fSShenwei Wang 	dma_wmb();
38329025944fSShenwei Wang 
38336d6b39f1SShenwei Wang 	txq->bd.cur = bdp;
38346d6b39f1SShenwei Wang 
38359025944fSShenwei Wang 	/* Trigger transmission start */
38369025944fSShenwei Wang 	writel(0, txq->bd.reg_desc_active);
38379025944fSShenwei Wang 
38386d6b39f1SShenwei Wang 	return 0;
38396d6b39f1SShenwei Wang }
38406d6b39f1SShenwei Wang 
38416d6b39f1SShenwei Wang static int fec_enet_xdp_xmit(struct net_device *dev,
38426d6b39f1SShenwei Wang 			     int num_frames,
38436d6b39f1SShenwei Wang 			     struct xdp_frame **frames,
38446d6b39f1SShenwei Wang 			     u32 flags)
38456d6b39f1SShenwei Wang {
38466d6b39f1SShenwei Wang 	struct fec_enet_private *fep = netdev_priv(dev);
38476d6b39f1SShenwei Wang 	struct fec_enet_priv_tx_q *txq;
38486d6b39f1SShenwei Wang 	int cpu = smp_processor_id();
384926312c68SShenwei Wang 	unsigned int sent_frames = 0;
38506d6b39f1SShenwei Wang 	struct netdev_queue *nq;
38516d6b39f1SShenwei Wang 	unsigned int queue;
38526d6b39f1SShenwei Wang 	int i;
38536d6b39f1SShenwei Wang 
38546d6b39f1SShenwei Wang 	queue = fec_enet_xdp_get_tx_queue(fep, cpu);
38556d6b39f1SShenwei Wang 	txq = fep->tx_queue[queue];
38566d6b39f1SShenwei Wang 	nq = netdev_get_tx_queue(fep->netdev, queue);
38576d6b39f1SShenwei Wang 
38586d6b39f1SShenwei Wang 	__netif_tx_lock(nq, cpu);
38596d6b39f1SShenwei Wang 
386026312c68SShenwei Wang 	for (i = 0; i < num_frames; i++) {
3861144470c8SShenwei Wang 		if (fec_enet_txq_xmit_frame(fep, txq, frames[i]) < 0)
386226312c68SShenwei Wang 			break;
386326312c68SShenwei Wang 		sent_frames++;
386426312c68SShenwei Wang 	}
38656d6b39f1SShenwei Wang 
38666d6b39f1SShenwei Wang 	__netif_tx_unlock(nq);
38676d6b39f1SShenwei Wang 
386826312c68SShenwei Wang 	return sent_frames;
38696d6b39f1SShenwei Wang }
38706d6b39f1SShenwei Wang 
3871793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = {
3872793fc096SFrank Li 	.ndo_open		= fec_enet_open,
3873793fc096SFrank Li 	.ndo_stop		= fec_enet_close,
3874793fc096SFrank Li 	.ndo_start_xmit		= fec_enet_start_xmit,
387552c4a1a8SFugang Duan 	.ndo_select_queue       = fec_enet_select_queue,
3876793fc096SFrank Li 	.ndo_set_rx_mode	= set_multicast_list,
3877793fc096SFrank Li 	.ndo_validate_addr	= eth_validate_addr,
3878793fc096SFrank Li 	.ndo_tx_timeout		= fec_timeout,
3879793fc096SFrank Li 	.ndo_set_mac_address	= fec_set_mac_address,
3880a7605370SArnd Bergmann 	.ndo_eth_ioctl		= fec_enet_ioctl,
3881793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3882793fc096SFrank Li 	.ndo_poll_controller	= fec_poll_controller,
3883793fc096SFrank Li #endif
38844c09eed9SJim Baxter 	.ndo_set_features	= fec_set_features,
38856d6b39f1SShenwei Wang 	.ndo_bpf		= fec_enet_bpf,
38866d6b39f1SShenwei Wang 	.ndo_xdp_xmit		= fec_enet_xdp_xmit,
3887793fc096SFrank Li };
3888793fc096SFrank Li 
388953bb20d1STroy Kisky static const unsigned short offset_des_active_rxq[] = {
389053bb20d1STroy Kisky 	FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
389153bb20d1STroy Kisky };
389253bb20d1STroy Kisky 
389353bb20d1STroy Kisky static const unsigned short offset_des_active_txq[] = {
389453bb20d1STroy Kisky 	FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
389553bb20d1STroy Kisky };
389653bb20d1STroy Kisky 
3897793fc096SFrank Li  /*
3898793fc096SFrank Li   * XXX:  We need to clean up on failure exits here.
3899793fc096SFrank Li   *
3900793fc096SFrank Li   */
3901793fc096SFrank Li static int fec_enet_init(struct net_device *ndev)
3902793fc096SFrank Li {
3903793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3904793fc096SFrank Li 	struct bufdesc *cbd_base;
39054d494cdcSFugang Duan 	dma_addr_t bd_dma;
390655d0218aSNimrod Andy 	int bd_size;
390759d0f746SFrank Li 	unsigned int i;
39087355f276STroy Kisky 	unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
39097355f276STroy Kisky 			sizeof(struct bufdesc);
39107355f276STroy Kisky 	unsigned dsize_log2 = __fls(dsize);
3911453e9dc4SStefan Agner 	int ret;
391255d0218aSNimrod Andy 
39137355f276STroy Kisky 	WARN_ON(dsize != (1 << dsize_log2));
39143f1dcc6aSLucas Stach #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
391541ef84ceSFugang Duan 	fep->rx_align = 0xf;
391641ef84ceSFugang Duan 	fep->tx_align = 0xf;
391741ef84ceSFugang Duan #else
391841ef84ceSFugang Duan 	fep->rx_align = 0x3;
391941ef84ceSFugang Duan 	fep->tx_align = 0x3;
392041ef84ceSFugang Duan #endif
3921df727d45SRasmus Villemoes 	fep->rx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
3922df727d45SRasmus Villemoes 	fep->tx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
3923df727d45SRasmus Villemoes 	fep->rx_time_itr = FEC_ITR_ICTT_DEFAULT;
3924df727d45SRasmus Villemoes 	fep->tx_time_itr = FEC_ITR_ICTT_DEFAULT;
392541ef84ceSFugang Duan 
3926453e9dc4SStefan Agner 	/* Check mask of the streaming and coherent API */
3927453e9dc4SStefan Agner 	ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
3928453e9dc4SStefan Agner 	if (ret < 0) {
3929453e9dc4SStefan Agner 		dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
3930453e9dc4SStefan Agner 		return ret;
3931453e9dc4SStefan Agner 	}
3932453e9dc4SStefan Agner 
3933619fee9eSFugang Duan 	ret = fec_enet_alloc_queue(ndev);
3934619fee9eSFugang Duan 	if (ret)
3935619fee9eSFugang Duan 		return ret;
393679f33912SNimrod Andy 
39377355f276STroy Kisky 	bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3938793fc096SFrank Li 
3939793fc096SFrank Li 	/* Allocate memory for buffer descriptors. */
3940c0a1a0a6SLucas Stach 	cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3941793fc096SFrank Li 				       GFP_KERNEL);
39424d494cdcSFugang Duan 	if (!cbd_base) {
3943619fee9eSFugang Duan 		ret = -ENOMEM;
3944619fee9eSFugang Duan 		goto free_queue_mem;
39454d494cdcSFugang Duan 	}
3946793fc096SFrank Li 
3947793fc096SFrank Li 	/* Get the Ethernet address */
3948052fcc45SFugang Duan 	ret = fec_get_mac(ndev);
3949052fcc45SFugang Duan 	if (ret)
3950052fcc45SFugang Duan 		goto free_queue_mem;
3951052fcc45SFugang Duan 
395244934facSLucas Stach 	/* make sure MAC we just acquired is programmed into the hw */
395344934facSLucas Stach 	fec_set_mac_address(ndev, NULL);
3954793fc096SFrank Li 
3955793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
395659d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
39577355f276STroy Kisky 		struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
39587355f276STroy Kisky 		unsigned size = dsize * rxq->bd.ring_size;
39597355f276STroy Kisky 
39607355f276STroy Kisky 		rxq->bd.qid = i;
39617355f276STroy Kisky 		rxq->bd.base = cbd_base;
39627355f276STroy Kisky 		rxq->bd.cur = cbd_base;
39637355f276STroy Kisky 		rxq->bd.dma = bd_dma;
39647355f276STroy Kisky 		rxq->bd.dsize = dsize;
39657355f276STroy Kisky 		rxq->bd.dsize_log2 = dsize_log2;
396653bb20d1STroy Kisky 		rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
39677355f276STroy Kisky 		bd_dma += size;
39687355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
39697355f276STroy Kisky 		rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
397059d0f746SFrank Li 	}
397159d0f746SFrank Li 
397259d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
39737355f276STroy Kisky 		struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
39747355f276STroy Kisky 		unsigned size = dsize * txq->bd.ring_size;
39757355f276STroy Kisky 
39767355f276STroy Kisky 		txq->bd.qid = i;
39777355f276STroy Kisky 		txq->bd.base = cbd_base;
39787355f276STroy Kisky 		txq->bd.cur = cbd_base;
39797355f276STroy Kisky 		txq->bd.dma = bd_dma;
39807355f276STroy Kisky 		txq->bd.dsize = dsize;
39817355f276STroy Kisky 		txq->bd.dsize_log2 = dsize_log2;
398253bb20d1STroy Kisky 		txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
39837355f276STroy Kisky 		bd_dma += size;
39847355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
39857355f276STroy Kisky 		txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
398659d0f746SFrank Li 	}
39874d494cdcSFugang Duan 
3988793fc096SFrank Li 
3989793fc096SFrank Li 	/* The FEC Ethernet specific entries in the device structure */
3990793fc096SFrank Li 	ndev->watchdog_timeo = TX_TIMEOUT;
3991793fc096SFrank Li 	ndev->netdev_ops = &fec_netdev_ops;
3992793fc096SFrank Li 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
3993793fc096SFrank Li 
3994793fc096SFrank Li 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3995b48b89f9SJakub Kicinski 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi);
3996793fc096SFrank Li 
39976b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3998cdffcf1bSJim Baxter 		/* enable hw VLAN support */
3999cdffcf1bSJim Baxter 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
4000cdffcf1bSJim Baxter 
40016b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
4002ee8b7a11SJakub Kicinski 		netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS);
400379f33912SNimrod Andy 
40044c09eed9SJim Baxter 		/* enable hw accelerator */
40054c09eed9SJim Baxter 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
400679f33912SNimrod Andy 				| NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
40074c09eed9SJim Baxter 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
400848496255SShawn Guo 	}
40094c09eed9SJim Baxter 
4010471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
401141ef84ceSFugang Duan 		fep->tx_align = 0;
401241ef84ceSFugang Duan 		fep->rx_align = 0x3f;
401341ef84ceSFugang Duan 	}
401441ef84ceSFugang Duan 
401509d1e541SNimrod Andy 	ndev->hw_features = ndev->features;
401609d1e541SNimrod Andy 
4017e4ac7cc6SWei Fang 	if (!(fep->quirks & FEC_QUIRK_SWAP_FRAME))
4018e4ac7cc6SWei Fang 		ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
4019e4ac7cc6SWei Fang 				     NETDEV_XDP_ACT_REDIRECT |
4020e4ac7cc6SWei Fang 				     NETDEV_XDP_ACT_NDO_XMIT;
4021e4ac7cc6SWei Fang 
4022ef83337dSRussell King 	fec_restart(ndev);
4023793fc096SFrank Li 
40242b30842bSAndrew Lunn 	if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
40252b30842bSAndrew Lunn 		fec_enet_clear_ethtool_stats(ndev);
40262b30842bSAndrew Lunn 	else
402780cca775SNikita Yushchenko 		fec_enet_update_ethtool_stats(ndev);
402880cca775SNikita Yushchenko 
4029793fc096SFrank Li 	return 0;
4030619fee9eSFugang Duan 
4031619fee9eSFugang Duan free_queue_mem:
4032619fee9eSFugang Duan 	fec_enet_free_queue(ndev);
4033619fee9eSFugang Duan 	return ret;
4034793fc096SFrank Li }
4035793fc096SFrank Li 
4036793fc096SFrank Li #ifdef CONFIG_OF
40379269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev)
4038793fc096SFrank Li {
4039468ba54bSArnd Bergmann 	struct gpio_desc *phy_reset;
4040159a0760SQuentin Schulz 	int msec = 1, phy_post_delay = 0;
4041793fc096SFrank Li 	struct device_node *np = pdev->dev.of_node;
4042468ba54bSArnd Bergmann 	int err;
4043793fc096SFrank Li 
4044793fc096SFrank Li 	if (!np)
40459269e556SFugang Duan 		return 0;
4046793fc096SFrank Li 
404761e04ccbSFugang Duan 	err = of_property_read_u32(np, "phy-reset-duration", &msec);
4048793fc096SFrank Li 	/* A sane reset duration should not be longer than 1s */
404961e04ccbSFugang Duan 	if (!err && msec > 1000)
4050793fc096SFrank Li 		msec = 1;
4051793fc096SFrank Li 
4052159a0760SQuentin Schulz 	err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
4053159a0760SQuentin Schulz 	/* valid reset duration should be less than 1s */
4054159a0760SQuentin Schulz 	if (!err && phy_post_delay > 1000)
4055159a0760SQuentin Schulz 		return -EINVAL;
4056159a0760SQuentin Schulz 
4057d7b5e5ddSDmitry Torokhov 	phy_reset = devm_gpiod_get_optional(&pdev->dev, "phy-reset",
40580719bc3aSDmitry Torokhov 					    GPIOD_OUT_HIGH);
4059468ba54bSArnd Bergmann 	if (IS_ERR(phy_reset))
4060468ba54bSArnd Bergmann 		return dev_err_probe(&pdev->dev, PTR_ERR(phy_reset),
4061468ba54bSArnd Bergmann 				     "failed to get phy-reset-gpios\n");
4062eb37c563SStefan Wahren 
4063d7b5e5ddSDmitry Torokhov 	if (!phy_reset)
4064d7b5e5ddSDmitry Torokhov 		return 0;
4065d7b5e5ddSDmitry Torokhov 
4066eb37c563SStefan Wahren 	if (msec > 20)
4067793fc096SFrank Li 		msleep(msec);
4068eb37c563SStefan Wahren 	else
4069eb37c563SStefan Wahren 		usleep_range(msec * 1000, msec * 1000 + 1000);
4070eb37c563SStefan Wahren 
40710719bc3aSDmitry Torokhov 	gpiod_set_value_cansleep(phy_reset, 0);
40729269e556SFugang Duan 
4073159a0760SQuentin Schulz 	if (!phy_post_delay)
4074159a0760SQuentin Schulz 		return 0;
4075159a0760SQuentin Schulz 
4076159a0760SQuentin Schulz 	if (phy_post_delay > 20)
4077159a0760SQuentin Schulz 		msleep(phy_post_delay);
4078159a0760SQuentin Schulz 	else
4079159a0760SQuentin Schulz 		usleep_range(phy_post_delay * 1000,
4080159a0760SQuentin Schulz 			     phy_post_delay * 1000 + 1000);
4081159a0760SQuentin Schulz 
40829269e556SFugang Duan 	return 0;
4083793fc096SFrank Li }
4084793fc096SFrank Li #else /* CONFIG_OF */
40859269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev)
4086793fc096SFrank Li {
4087793fc096SFrank Li 	/*
4088793fc096SFrank Li 	 * In case of platform probe, the reset has been done
4089793fc096SFrank Li 	 * by machine code.
4090793fc096SFrank Li 	 */
40919269e556SFugang Duan 	return 0;
4092793fc096SFrank Li }
4093793fc096SFrank Li #endif /* CONFIG_OF */
4094793fc096SFrank Li 
40959fc095f1SFugang Duan static void
40969fc095f1SFugang Duan fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
40979fc095f1SFugang Duan {
40989fc095f1SFugang Duan 	struct device_node *np = pdev->dev.of_node;
40999fc095f1SFugang Duan 
41009fc095f1SFugang Duan 	*num_tx = *num_rx = 1;
41019fc095f1SFugang Duan 
41029fc095f1SFugang Duan 	if (!np || !of_device_is_available(np))
41039fc095f1SFugang Duan 		return;
41049fc095f1SFugang Duan 
41059fc095f1SFugang Duan 	/* parse the num of tx and rx queues */
410673b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
4107b7bd75cfSFrank Li 
410873b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
41099fc095f1SFugang Duan 
41109fc095f1SFugang Duan 	if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
4111b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
41129fc095f1SFugang Duan 			 *num_tx);
41139fc095f1SFugang Duan 		*num_tx = 1;
41149fc095f1SFugang Duan 		return;
41159fc095f1SFugang Duan 	}
41169fc095f1SFugang Duan 
41179fc095f1SFugang Duan 	if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
4118b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
41199fc095f1SFugang Duan 			 *num_rx);
41209fc095f1SFugang Duan 		*num_rx = 1;
41219fc095f1SFugang Duan 		return;
41229fc095f1SFugang Duan 	}
41239fc095f1SFugang Duan 
41249fc095f1SFugang Duan }
41259fc095f1SFugang Duan 
41264ad1ceecSTroy Kisky static int fec_enet_get_irq_cnt(struct platform_device *pdev)
41274ad1ceecSTroy Kisky {
41284ad1ceecSTroy Kisky 	int irq_cnt = platform_irq_count(pdev);
41294ad1ceecSTroy Kisky 
41304ad1ceecSTroy Kisky 	if (irq_cnt > FEC_IRQ_NUM)
41314ad1ceecSTroy Kisky 		irq_cnt = FEC_IRQ_NUM;	/* last for pps */
41324ad1ceecSTroy Kisky 	else if (irq_cnt == 2)
41334ad1ceecSTroy Kisky 		irq_cnt = 1;	/* last for pps */
41344ad1ceecSTroy Kisky 	else if (irq_cnt <= 0)
41354ad1ceecSTroy Kisky 		irq_cnt = 1;	/* At least 1 irq is needed */
41364ad1ceecSTroy Kisky 	return irq_cnt;
41374ad1ceecSTroy Kisky }
41384ad1ceecSTroy Kisky 
4139b7cdc965SJoakim Zhang static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
4140b7cdc965SJoakim Zhang {
4141b7cdc965SJoakim Zhang 	struct net_device *ndev = platform_get_drvdata(pdev);
4142b7cdc965SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
4143b7cdc965SJoakim Zhang 
4144b7cdc965SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
4145b7cdc965SJoakim Zhang 		fep->wake_irq = fep->irq[2];
4146b7cdc965SJoakim Zhang 	else
4147b7cdc965SJoakim Zhang 		fep->wake_irq = fep->irq[0];
4148b7cdc965SJoakim Zhang }
4149b7cdc965SJoakim Zhang 
4150da722186SMartin Fuzzey static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
4151da722186SMartin Fuzzey 				   struct device_node *np)
4152da722186SMartin Fuzzey {
4153da722186SMartin Fuzzey 	struct device_node *gpr_np;
41548a448bf8SFugang Duan 	u32 out_val[3];
4155da722186SMartin Fuzzey 	int ret = 0;
4156da722186SMartin Fuzzey 
41578a448bf8SFugang Duan 	gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
4158da722186SMartin Fuzzey 	if (!gpr_np)
4159da722186SMartin Fuzzey 		return 0;
4160da722186SMartin Fuzzey 
41618a448bf8SFugang Duan 	ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
41628a448bf8SFugang Duan 					 ARRAY_SIZE(out_val));
41638a448bf8SFugang Duan 	if (ret) {
41648a448bf8SFugang Duan 		dev_dbg(&fep->pdev->dev, "no stop mode property\n");
4165d2b52ec0SYang Yingliang 		goto out;
41668a448bf8SFugang Duan 	}
41678a448bf8SFugang Duan 
4168da722186SMartin Fuzzey 	fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
4169da722186SMartin Fuzzey 	if (IS_ERR(fep->stop_gpr.gpr)) {
4170da722186SMartin Fuzzey 		dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
4171da722186SMartin Fuzzey 		ret = PTR_ERR(fep->stop_gpr.gpr);
4172da722186SMartin Fuzzey 		fep->stop_gpr.gpr = NULL;
4173da722186SMartin Fuzzey 		goto out;
4174da722186SMartin Fuzzey 	}
4175da722186SMartin Fuzzey 
41768a448bf8SFugang Duan 	fep->stop_gpr.reg = out_val[1];
41778a448bf8SFugang Duan 	fep->stop_gpr.bit = out_val[2];
4178da722186SMartin Fuzzey 
4179da722186SMartin Fuzzey out:
4180da722186SMartin Fuzzey 	of_node_put(gpr_np);
4181da722186SMartin Fuzzey 
4182da722186SMartin Fuzzey 	return ret;
4183da722186SMartin Fuzzey }
4184da722186SMartin Fuzzey 
4185793fc096SFrank Li static int
4186793fc096SFrank Li fec_probe(struct platform_device *pdev)
4187793fc096SFrank Li {
4188793fc096SFrank Li 	struct fec_enet_private *fep;
4189793fc096SFrank Li 	struct fec_platform_data *pdata;
41900c65b2b9SAndrew Lunn 	phy_interface_t interface;
4191793fc096SFrank Li 	struct net_device *ndev;
4192793fc096SFrank Li 	int i, irq, ret = 0;
4193793fc096SFrank Li 	const struct of_device_id *of_id;
4194793fc096SFrank Li 	static int dev_id;
4195407066f8SUwe Kleine-König 	struct device_node *np = pdev->dev.of_node, *phy_node;
4196b7bd75cfSFrank Li 	int num_tx_qs;
4197b7bd75cfSFrank Li 	int num_rx_qs;
41984ad1ceecSTroy Kisky 	char irq_name[8];
41994ad1ceecSTroy Kisky 	int irq_cnt;
4200da722186SMartin Fuzzey 	struct fec_devinfo *dev_info;
4201793fc096SFrank Li 
42029fc095f1SFugang Duan 	fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
42039fc095f1SFugang Duan 
4204793fc096SFrank Li 	/* Init network device */
420580cca775SNikita Yushchenko 	ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
4206f85de666SNikita Yushchenko 				  FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
4207793fc096SFrank Li 	if (!ndev)
4208793fc096SFrank Li 		return -ENOMEM;
4209793fc096SFrank Li 
4210793fc096SFrank Li 	SET_NETDEV_DEV(ndev, &pdev->dev);
4211793fc096SFrank Li 
4212793fc096SFrank Li 	/* setup board info structure */
4213793fc096SFrank Li 	fep = netdev_priv(ndev);
4214793fc096SFrank Li 
42156b7e4008SLothar Waßmann 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
42166b7e4008SLothar Waßmann 	if (of_id)
42176b7e4008SLothar Waßmann 		pdev->id_entry = of_id->data;
4218da722186SMartin Fuzzey 	dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
4219da722186SMartin Fuzzey 	if (dev_info)
4220da722186SMartin Fuzzey 		fep->quirks = dev_info->quirks;
42216b7e4008SLothar Waßmann 
42220c818594SHubert Feurstein 	fep->netdev = ndev;
42239fc095f1SFugang Duan 	fep->num_rx_queues = num_rx_qs;
42249fc095f1SFugang Duan 	fep->num_tx_queues = num_tx_qs;
42259fc095f1SFugang Duan 
4226d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
4227793fc096SFrank Li 	/* default enable pause frame auto negotiation */
42286b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT)
4229793fc096SFrank Li 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
4230d1391930SGuenter Roeck #endif
4231793fc096SFrank Li 
42325bbde4d2SNimrod Andy 	/* Select default pin state */
42335bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&pdev->dev);
42345bbde4d2SNimrod Andy 
42354f830a5aSYueHaibing 	fep->hwp = devm_platform_ioremap_resource(pdev, 0);
4236941e173aSTushar Behera 	if (IS_ERR(fep->hwp)) {
4237941e173aSTushar Behera 		ret = PTR_ERR(fep->hwp);
4238941e173aSTushar Behera 		goto failed_ioremap;
4239941e173aSTushar Behera 	}
4240941e173aSTushar Behera 
4241793fc096SFrank Li 	fep->pdev = pdev;
4242793fc096SFrank Li 	fep->dev_id = dev_id++;
4243793fc096SFrank Li 
4244793fc096SFrank Li 	platform_set_drvdata(pdev, ndev);
4245793fc096SFrank Li 
424629380905SLucas Stach 	if ((of_machine_is_compatible("fsl,imx6q") ||
424729380905SLucas Stach 	     of_machine_is_compatible("fsl,imx6dl")) &&
424829380905SLucas Stach 	    !of_property_read_bool(np, "fsl,err006687-workaround-present"))
424929380905SLucas Stach 		fep->quirks |= FEC_QUIRK_ERR006687;
425029380905SLucas Stach 
425140c79ce1SWei Fang 	ret = fec_enet_ipc_handle_init(fep);
425240c79ce1SWei Fang 	if (ret)
425340c79ce1SWei Fang 		goto failed_ipc_init;
425440c79ce1SWei Fang 
42551a87e641SRob Herring 	if (of_property_read_bool(np, "fsl,magic-packet"))
4256de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
4257de40ed31SNimrod Andy 
42588a448bf8SFugang Duan 	ret = fec_enet_init_stop_mode(fep, np);
4259da722186SMartin Fuzzey 	if (ret)
4260da722186SMartin Fuzzey 		goto failed_stop_mode;
4261da722186SMartin Fuzzey 
4262407066f8SUwe Kleine-König 	phy_node = of_parse_phandle(np, "phy-handle", 0);
4263407066f8SUwe Kleine-König 	if (!phy_node && of_phy_is_fixed_link(np)) {
4264407066f8SUwe Kleine-König 		ret = of_phy_register_fixed_link(np);
4265407066f8SUwe Kleine-König 		if (ret < 0) {
4266407066f8SUwe Kleine-König 			dev_err(&pdev->dev,
4267407066f8SUwe Kleine-König 				"broken fixed-link specification\n");
4268407066f8SUwe Kleine-König 			goto failed_phy;
4269407066f8SUwe Kleine-König 		}
4270407066f8SUwe Kleine-König 		phy_node = of_node_get(np);
4271407066f8SUwe Kleine-König 	}
4272407066f8SUwe Kleine-König 	fep->phy_node = phy_node;
4273407066f8SUwe Kleine-König 
42740c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(pdev->dev.of_node, &interface);
42750c65b2b9SAndrew Lunn 	if (ret) {
427694660ba0SJingoo Han 		pdata = dev_get_platdata(&pdev->dev);
4277793fc096SFrank Li 		if (pdata)
4278793fc096SFrank Li 			fep->phy_interface = pdata->phy;
4279793fc096SFrank Li 		else
4280793fc096SFrank Li 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
4281793fc096SFrank Li 	} else {
42820c65b2b9SAndrew Lunn 		fep->phy_interface = interface;
4283793fc096SFrank Li 	}
4284793fc096SFrank Li 
4285b820c114SJoakim Zhang 	ret = fec_enet_parse_rgmii_delay(fep, np);
4286b820c114SJoakim Zhang 	if (ret)
4287b820c114SJoakim Zhang 		goto failed_rgmii_delay;
4288b820c114SJoakim Zhang 
4289793fc096SFrank Li 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
4290793fc096SFrank Li 	if (IS_ERR(fep->clk_ipg)) {
4291793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ipg);
4292793fc096SFrank Li 		goto failed_clk;
4293793fc096SFrank Li 	}
4294793fc096SFrank Li 
4295793fc096SFrank Li 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
4296793fc096SFrank Li 	if (IS_ERR(fep->clk_ahb)) {
4297793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ahb);
4298793fc096SFrank Li 		goto failed_clk;
4299793fc096SFrank Li 	}
4300793fc096SFrank Li 
4301d851b47bSFugang Duan 	fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
4302d851b47bSFugang Duan 
430338f56f33SLinus Torvalds 	/* enet_out is optional, depends on board */
43045ff851b7SUwe Kleine-König 	fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out");
43055ff851b7SUwe Kleine-König 	if (IS_ERR(fep->clk_enet_out)) {
43065ff851b7SUwe Kleine-König 		ret = PTR_ERR(fep->clk_enet_out);
43075ff851b7SUwe Kleine-König 		goto failed_clk;
43085ff851b7SUwe Kleine-König 	}
430938f56f33SLinus Torvalds 
431091c0d987SNimrod Andy 	fep->ptp_clk_on = false;
431101b825f9SFrancesco Dolcini 	mutex_init(&fep->ptp_clk_mutex);
43129b5330edSFugang Duan 
43139b5330edSFugang Duan 	/* clk_ref is optional, depends on board */
431443252ed1SUwe Kleine-König 	fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
431543252ed1SUwe Kleine-König 	if (IS_ERR(fep->clk_ref)) {
431643252ed1SUwe Kleine-König 		ret = PTR_ERR(fep->clk_ref);
431743252ed1SUwe Kleine-König 		goto failed_clk;
431843252ed1SUwe Kleine-König 	}
4319b82f8c3fSFugang Duan 	fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
43209b5330edSFugang Duan 
4321fc539459SFugang Duan 	/* clk_2x_txclk is optional, depends on board */
4322b820c114SJoakim Zhang 	if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
4323fc539459SFugang Duan 		fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
4324fc539459SFugang Duan 		if (IS_ERR(fep->clk_2x_txclk))
4325fc539459SFugang Duan 			fep->clk_2x_txclk = NULL;
4326b820c114SJoakim Zhang 	}
4327fc539459SFugang Duan 
43286b7e4008SLothar Waßmann 	fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
4329793fc096SFrank Li 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
4330793fc096SFrank Li 	if (IS_ERR(fep->clk_ptp)) {
433138f56f33SLinus Torvalds 		fep->clk_ptp = NULL;
4332217b5844SLothar Waßmann 		fep->bufdesc_ex = false;
4333793fc096SFrank Li 	}
4334793fc096SFrank Li 
4335e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
433613a097bdSFabio Estevam 	if (ret)
433713a097bdSFabio Estevam 		goto failed_clk;
433813a097bdSFabio Estevam 
43398fff755eSAndrew Lunn 	ret = clk_prepare_enable(fep->clk_ipg);
43408fff755eSAndrew Lunn 	if (ret)
43418fff755eSAndrew Lunn 		goto failed_clk_ipg;
4342d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ahb);
4343d7c3a206SAndy Duan 	if (ret)
4344d7c3a206SAndy Duan 		goto failed_clk_ahb;
43458fff755eSAndrew Lunn 
434625974d8aSStefan Agner 	fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
4347f4e9f3d2SFabio Estevam 	if (!IS_ERR(fep->reg_phy)) {
4348f4e9f3d2SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
4349793fc096SFrank Li 		if (ret) {
4350793fc096SFrank Li 			dev_err(&pdev->dev,
4351793fc096SFrank Li 				"Failed to enable phy regulator: %d\n", ret);
4352793fc096SFrank Li 			goto failed_regulator;
4353793fc096SFrank Li 		}
4354f6a4d607SFabio Estevam 	} else {
43553f38c683SFugang Duan 		if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
43563f38c683SFugang Duan 			ret = -EPROBE_DEFER;
43573f38c683SFugang Duan 			goto failed_regulator;
43583f38c683SFugang Duan 		}
4359f6a4d607SFabio Estevam 		fep->reg_phy = NULL;
4360793fc096SFrank Li 	}
4361793fc096SFrank Li 
43628fff755eSAndrew Lunn 	pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
43638fff755eSAndrew Lunn 	pm_runtime_use_autosuspend(&pdev->dev);
436414d2b7c1SLucas Stach 	pm_runtime_get_noresume(&pdev->dev);
43658fff755eSAndrew Lunn 	pm_runtime_set_active(&pdev->dev);
43668fff755eSAndrew Lunn 	pm_runtime_enable(&pdev->dev);
43678fff755eSAndrew Lunn 
43689269e556SFugang Duan 	ret = fec_reset_phy(pdev);
43699269e556SFugang Duan 	if (ret)
43709269e556SFugang Duan 		goto failed_reset;
4371793fc096SFrank Li 
43724ad1ceecSTroy Kisky 	irq_cnt = fec_enet_get_irq_cnt(pdev);
4373793fc096SFrank Li 	if (fep->bufdesc_ex)
43744ad1ceecSTroy Kisky 		fec_ptp_init(pdev, irq_cnt);
4375793fc096SFrank Li 
4376793fc096SFrank Li 	ret = fec_enet_init(ndev);
4377793fc096SFrank Li 	if (ret)
4378793fc096SFrank Li 		goto failed_init;
4379793fc096SFrank Li 
43804ad1ceecSTroy Kisky 	for (i = 0; i < irq_cnt; i++) {
43813ded9f2bSArnd Bergmann 		snprintf(irq_name, sizeof(irq_name), "int%d", i);
43823b56be21SAnson Huang 		irq = platform_get_irq_byname_optional(pdev, irq_name);
43834ad1ceecSTroy Kisky 		if (irq < 0)
4384793fc096SFrank Li 			irq = platform_get_irq(pdev, i);
4385793fc096SFrank Li 		if (irq < 0) {
4386793fc096SFrank Li 			ret = irq;
4387793fc096SFrank Li 			goto failed_irq;
4388793fc096SFrank Li 		}
43890d9b2ab1SFabio Estevam 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
439044a272ddSMichael Opdenacker 				       0, pdev->name, ndev);
43910d9b2ab1SFabio Estevam 		if (ret)
4392793fc096SFrank Li 			goto failed_irq;
4393de40ed31SNimrod Andy 
4394de40ed31SNimrod Andy 		fep->irq[i] = irq;
4395793fc096SFrank Li 	}
4396793fc096SFrank Li 
4397b7cdc965SJoakim Zhang 	/* Decide which interrupt line is wakeup capable */
4398b7cdc965SJoakim Zhang 	fec_enet_get_wakeup_irq(pdev);
4399b7cdc965SJoakim Zhang 
4400793fc096SFrank Li 	ret = fec_enet_mii_init(pdev);
4401793fc096SFrank Li 	if (ret)
4402793fc096SFrank Li 		goto failed_mii_init;
4403793fc096SFrank Li 
4404793fc096SFrank Li 	/* Carrier starts down, phylib will bring it up */
4405793fc096SFrank Li 	netif_carrier_off(ndev);
4406e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
44075bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&pdev->dev);
4408793fc096SFrank Li 
440959193053SAndrew Lunn 	ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN;
441059193053SAndrew Lunn 
4411793fc096SFrank Li 	ret = register_netdev(ndev);
4412793fc096SFrank Li 	if (ret)
4413793fc096SFrank Li 		goto failed_register;
4414793fc096SFrank Li 
4415de40ed31SNimrod Andy 	device_init_wakeup(&ndev->dev, fep->wol_flag &
4416de40ed31SNimrod Andy 			   FEC_WOL_HAS_MAGIC_PACKET);
4417de40ed31SNimrod Andy 
4418eb1d0640SFabio Estevam 	if (fep->bufdesc_ex && fep->ptp_clock)
4419eb1d0640SFabio Estevam 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
4420eb1d0640SFabio Estevam 
44211b7bde6dSNimrod Andy 	fep->rx_copybreak = COPYBREAK_DEFAULT;
442236cdc743SRussell King 	INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
44238fff755eSAndrew Lunn 
44248fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&pdev->dev);
44258fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&pdev->dev);
44268fff755eSAndrew Lunn 
4427793fc096SFrank Li 	return 0;
4428793fc096SFrank Li 
4429793fc096SFrank Li failed_register:
4430793fc096SFrank Li 	fec_enet_mii_remove(fep);
4431793fc096SFrank Li failed_mii_init:
44327a2bbd8dSFabio Estevam failed_irq:
44337a2bbd8dSFabio Estevam failed_init:
443432cba57bSLucas Stach 	fec_ptp_stop(pdev);
44359269e556SFugang Duan failed_reset:
4436ce8d24f9SAndy Duan 	pm_runtime_put_noidle(&pdev->dev);
44379269e556SFugang Duan 	pm_runtime_disable(&pdev->dev);
4438c6165cf0SFugang Duan 	if (fep->reg_phy)
4439c6165cf0SFugang Duan 		regulator_disable(fep->reg_phy);
4440793fc096SFrank Li failed_regulator:
4441d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
4442d7c3a206SAndy Duan failed_clk_ahb:
4443d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ipg);
44448fff755eSAndrew Lunn failed_clk_ipg:
4445e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
4446793fc096SFrank Li failed_clk:
4447b820c114SJoakim Zhang failed_rgmii_delay:
444882005b1cSJohan Hovold 	if (of_phy_is_fixed_link(np))
444982005b1cSJohan Hovold 		of_phy_deregister_fixed_link(np);
4450407066f8SUwe Kleine-König 	of_node_put(phy_node);
4451da722186SMartin Fuzzey failed_stop_mode:
445240c79ce1SWei Fang failed_ipc_init:
4453d1616f07SFugang Duan failed_phy:
4454d1616f07SFugang Duan 	dev_id--;
4455793fc096SFrank Li failed_ioremap:
4456793fc096SFrank Li 	free_netdev(ndev);
4457793fc096SFrank Li 
4458793fc096SFrank Li 	return ret;
4459793fc096SFrank Li }
4460793fc096SFrank Li 
4461793fc096SFrank Li static int
4462793fc096SFrank Li fec_drv_remove(struct platform_device *pdev)
4463793fc096SFrank Li {
4464793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
4465793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
446682005b1cSJohan Hovold 	struct device_node *np = pdev->dev.of_node;
4467a31eda65SChuhong Yuan 	int ret;
4468a31eda65SChuhong Yuan 
4469f816b982SUwe Kleine-König 	ret = pm_runtime_get_sync(&pdev->dev);
4470a31eda65SChuhong Yuan 	if (ret < 0)
4471f816b982SUwe Kleine-König 		dev_err(&pdev->dev,
4472f816b982SUwe Kleine-König 			"Failed to resume device in remove callback (%pe)\n",
4473f816b982SUwe Kleine-König 			ERR_PTR(ret));
4474793fc096SFrank Li 
447536cdc743SRussell King 	cancel_work_sync(&fep->tx_timeout_work);
447632cba57bSLucas Stach 	fec_ptp_stop(pdev);
4477793fc096SFrank Li 	unregister_netdev(ndev);
4478793fc096SFrank Li 	fec_enet_mii_remove(fep);
4479f6a4d607SFabio Estevam 	if (fep->reg_phy)
4480f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
4481a31eda65SChuhong Yuan 
448282005b1cSJohan Hovold 	if (of_phy_is_fixed_link(np))
448382005b1cSJohan Hovold 		of_phy_deregister_fixed_link(np);
4484407066f8SUwe Kleine-König 	of_node_put(fep->phy_node);
4485793fc096SFrank Li 
4486f816b982SUwe Kleine-König 	/* After pm_runtime_get_sync() failed, the clks are still off, so skip
4487f816b982SUwe Kleine-König 	 * disabling them again.
4488f816b982SUwe Kleine-König 	 */
4489f816b982SUwe Kleine-König 	if (ret >= 0) {
4490a31eda65SChuhong Yuan 		clk_disable_unprepare(fep->clk_ahb);
4491a31eda65SChuhong Yuan 		clk_disable_unprepare(fep->clk_ipg);
4492f816b982SUwe Kleine-König 	}
4493a31eda65SChuhong Yuan 	pm_runtime_put_noidle(&pdev->dev);
4494a31eda65SChuhong Yuan 	pm_runtime_disable(&pdev->dev);
4495a31eda65SChuhong Yuan 
449644712965SPavel Skripkin 	free_netdev(ndev);
4497793fc096SFrank Li 	return 0;
4498793fc096SFrank Li }
4499793fc096SFrank Li 
4500dd66d386SFabio Estevam static int __maybe_unused fec_suspend(struct device *dev)
4501793fc096SFrank Li {
4502793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
4503793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4504da970726SWei Fang 	int ret;
4505793fc096SFrank Li 
4506da1774e5SRussell King 	rtnl_lock();
4507793fc096SFrank Li 	if (netif_running(ndev)) {
4508de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
4509de40ed31SNimrod Andy 			fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
451045f5c327SPhilippe Reynes 		phy_stop(ndev->phydev);
451131a6de34SRussell King 		napi_disable(&fep->napi);
451231a6de34SRussell King 		netif_tx_lock_bh(ndev);
4513793fc096SFrank Li 		netif_device_detach(ndev);
451431a6de34SRussell King 		netif_tx_unlock_bh(ndev);
451531a6de34SRussell King 		fec_stop(ndev);
45160b6f65c7SJoakim Zhang 		if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
45170b6f65c7SJoakim Zhang 			fec_irqs_disable(ndev);
45185bbde4d2SNimrod Andy 			pinctrl_pm_select_sleep_state(&fep->pdev->dev);
45190b6f65c7SJoakim Zhang 		} else {
45200b6f65c7SJoakim Zhang 			fec_irqs_disable_except_wakeup(ndev);
45210b6f65c7SJoakim Zhang 			if (fep->wake_irq > 0) {
45220b6f65c7SJoakim Zhang 				disable_irq(fep->wake_irq);
45230b6f65c7SJoakim Zhang 				enable_irq_wake(fep->wake_irq);
45240b6f65c7SJoakim Zhang 			}
45250b6f65c7SJoakim Zhang 			fec_enet_stop_mode(fep, true);
45260b6f65c7SJoakim Zhang 		}
45270b6f65c7SJoakim Zhang 		/* It's safe to disable clocks since interrupts are masked */
45280b6f65c7SJoakim Zhang 		fec_enet_clk_enable(ndev, false);
4529da970726SWei Fang 
4530da970726SWei Fang 		fep->rpm_active = !pm_runtime_status_suspended(dev);
4531da970726SWei Fang 		if (fep->rpm_active) {
4532da970726SWei Fang 			ret = pm_runtime_force_suspend(dev);
4533da970726SWei Fang 			if (ret < 0) {
4534da970726SWei Fang 				rtnl_unlock();
4535da970726SWei Fang 				return ret;
4536da970726SWei Fang 			}
4537da970726SWei Fang 		}
4538f4c4a4e0SNimrod Andy 	}
4539f4c4a4e0SNimrod Andy 	rtnl_unlock();
4540793fc096SFrank Li 
4541de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
4542238f7bc7SFabio Estevam 		regulator_disable(fep->reg_phy);
4543238f7bc7SFabio Estevam 
4544858eeb7dSNimrod Andy 	/* SOC supply clock to phy, when clock is disabled, phy link down
4545858eeb7dSNimrod Andy 	 * SOC control phy regulator, when regulator is disabled, phy link down
4546858eeb7dSNimrod Andy 	 */
4547858eeb7dSNimrod Andy 	if (fep->clk_enet_out || fep->reg_phy)
4548858eeb7dSNimrod Andy 		fep->link = 0;
4549858eeb7dSNimrod Andy 
4550793fc096SFrank Li 	return 0;
4551793fc096SFrank Li }
4552793fc096SFrank Li 
4553dd66d386SFabio Estevam static int __maybe_unused fec_resume(struct device *dev)
4554793fc096SFrank Li {
4555793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
4556793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4557238f7bc7SFabio Estevam 	int ret;
4558de40ed31SNimrod Andy 	int val;
4559238f7bc7SFabio Estevam 
4560de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4561238f7bc7SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
4562238f7bc7SFabio Estevam 		if (ret)
4563238f7bc7SFabio Estevam 			return ret;
4564238f7bc7SFabio Estevam 	}
4565793fc096SFrank Li 
4566da1774e5SRussell King 	rtnl_lock();
4567793fc096SFrank Li 	if (netif_running(ndev)) {
4568da970726SWei Fang 		if (fep->rpm_active)
4569da970726SWei Fang 			pm_runtime_force_resume(dev);
4570da970726SWei Fang 
4571f4c4a4e0SNimrod Andy 		ret = fec_enet_clk_enable(ndev, true);
4572f4c4a4e0SNimrod Andy 		if (ret) {
4573f4c4a4e0SNimrod Andy 			rtnl_unlock();
4574f4c4a4e0SNimrod Andy 			goto failed_clk;
4575f4c4a4e0SNimrod Andy 		}
4576de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
4577da722186SMartin Fuzzey 			fec_enet_stop_mode(fep, false);
45780b6f65c7SJoakim Zhang 			if (fep->wake_irq) {
45790b6f65c7SJoakim Zhang 				disable_irq_wake(fep->wake_irq);
45800b6f65c7SJoakim Zhang 				enable_irq(fep->wake_irq);
45810b6f65c7SJoakim Zhang 			}
4582da722186SMartin Fuzzey 
4583de40ed31SNimrod Andy 			val = readl(fep->hwp + FEC_ECNTRL);
4584de40ed31SNimrod Andy 			val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
4585de40ed31SNimrod Andy 			writel(val, fep->hwp + FEC_ECNTRL);
4586de40ed31SNimrod Andy 			fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
4587de40ed31SNimrod Andy 		} else {
4588de40ed31SNimrod Andy 			pinctrl_pm_select_default_state(&fep->pdev->dev);
4589de40ed31SNimrod Andy 		}
4590ef83337dSRussell King 		fec_restart(ndev);
459131a6de34SRussell King 		netif_tx_lock_bh(ndev);
4592793fc096SFrank Li 		netif_device_attach(ndev);
45936af42d42SRussell King 		netif_tx_unlock_bh(ndev);
45946af42d42SRussell King 		napi_enable(&fep->napi);
4595557d5dc8SHeiner Kallweit 		phy_init_hw(ndev->phydev);
459645f5c327SPhilippe Reynes 		phy_start(ndev->phydev);
4597793fc096SFrank Li 	}
4598da1774e5SRussell King 	rtnl_unlock();
4599793fc096SFrank Li 
4600793fc096SFrank Li 	return 0;
460113a097bdSFabio Estevam 
4602e8fcfcd5SNimrod Andy failed_clk:
460313a097bdSFabio Estevam 	if (fep->reg_phy)
460413a097bdSFabio Estevam 		regulator_disable(fep->reg_phy);
460513a097bdSFabio Estevam 	return ret;
4606793fc096SFrank Li }
4607793fc096SFrank Li 
46088fff755eSAndrew Lunn static int __maybe_unused fec_runtime_suspend(struct device *dev)
46098fff755eSAndrew Lunn {
46108fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
46118fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
46128fff755eSAndrew Lunn 
4613d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
46148fff755eSAndrew Lunn 	clk_disable_unprepare(fep->clk_ipg);
46158fff755eSAndrew Lunn 
46168fff755eSAndrew Lunn 	return 0;
46178fff755eSAndrew Lunn }
46188fff755eSAndrew Lunn 
46198fff755eSAndrew Lunn static int __maybe_unused fec_runtime_resume(struct device *dev)
46208fff755eSAndrew Lunn {
46218fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
46228fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
4623d7c3a206SAndy Duan 	int ret;
46248fff755eSAndrew Lunn 
4625d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ahb);
4626d7c3a206SAndy Duan 	if (ret)
4627d7c3a206SAndy Duan 		return ret;
4628d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ipg);
4629d7c3a206SAndy Duan 	if (ret)
4630d7c3a206SAndy Duan 		goto failed_clk_ipg;
4631d7c3a206SAndy Duan 
4632d7c3a206SAndy Duan 	return 0;
4633d7c3a206SAndy Duan 
4634d7c3a206SAndy Duan failed_clk_ipg:
4635d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
4636d7c3a206SAndy Duan 	return ret;
46378fff755eSAndrew Lunn }
46388fff755eSAndrew Lunn 
46398fff755eSAndrew Lunn static const struct dev_pm_ops fec_pm_ops = {
46408fff755eSAndrew Lunn 	SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
46418fff755eSAndrew Lunn 	SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
46428fff755eSAndrew Lunn };
4643793fc096SFrank Li 
4644793fc096SFrank Li static struct platform_driver fec_driver = {
4645793fc096SFrank Li 	.driver	= {
4646793fc096SFrank Li 		.name	= DRIVER_NAME,
4647793fc096SFrank Li 		.pm	= &fec_pm_ops,
4648793fc096SFrank Li 		.of_match_table = fec_dt_ids,
4649272bb0e9SFabio Estevam 		.suppress_bind_attrs = true,
4650793fc096SFrank Li 	},
4651793fc096SFrank Li 	.id_table = fec_devtype,
4652793fc096SFrank Li 	.probe	= fec_probe,
4653793fc096SFrank Li 	.remove	= fec_drv_remove,
4654793fc096SFrank Li };
4655793fc096SFrank Li 
4656793fc096SFrank Li module_platform_driver(fec_driver);
4657793fc096SFrank Li 
4658793fc096SFrank Li MODULE_LICENSE("GPL");
4659