xref: /linux/drivers/net/ethernet/freescale/fec_main.c (revision 9540329452b737340960b4273600e61a6a236a78)
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>
41a9ca9f9cSYunsheng Lin #include <net/page_pool/helpers.h>
426016ba34SOleksij Rempel #include <net/selftests.h>
4379f33912SNimrod Andy #include <net/tso.h>
444c09eed9SJim Baxter #include <linux/tcp.h>
454c09eed9SJim Baxter #include <linux/udp.h>
464c09eed9SJim Baxter #include <linux/icmp.h>
47793fc096SFrank Li #include <linux/spinlock.h>
48793fc096SFrank Li #include <linux/workqueue.h>
49793fc096SFrank Li #include <linux/bitops.h>
50793fc096SFrank Li #include <linux/io.h>
51793fc096SFrank Li #include <linux/irq.h>
52793fc096SFrank Li #include <linux/clk.h>
5316f6e983SKrzysztof Kozlowski #include <linux/crc32.h>
54793fc096SFrank Li #include <linux/platform_device.h>
557f854420SAndrew Lunn #include <linux/mdio.h>
56793fc096SFrank Li #include <linux/phy.h>
57793fc096SFrank Li #include <linux/fec.h>
58793fc096SFrank Li #include <linux/of.h>
59793fc096SFrank Li #include <linux/of_device.h>
60407066f8SUwe Kleine-König #include <linux/of_mdio.h>
61793fc096SFrank Li #include <linux/of_net.h>
62793fc096SFrank Li #include <linux/regulator/consumer.h>
63cdffcf1bSJim Baxter #include <linux/if_vlan.h>
64a68ab98eSFabio Estevam #include <linux/pinctrl/consumer.h>
65468ba54bSArnd Bergmann #include <linux/gpio/consumer.h>
66c259c132SFrank Li #include <linux/prefetch.h>
67da722186SMartin Fuzzey #include <linux/mfd/syscon.h>
68da722186SMartin Fuzzey #include <linux/regmap.h>
6929380905SLucas Stach #include <soc/imx/cpuidle.h>
7095698ff6SShenwei Wang #include <linux/filter.h>
7195698ff6SShenwei Wang #include <linux/bpf.h>
72f601899eSWei Fang #include <linux/bpf_trace.h>
73793fc096SFrank Li 
74793fc096SFrank Li #include <asm/cacheflush.h>
75793fc096SFrank Li 
76793fc096SFrank Li #include "fec.h"
77793fc096SFrank Li 
78772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev);
79df727d45SRasmus Villemoes static void fec_enet_itr_coal_set(struct net_device *ndev);
80f601899eSWei Fang static int fec_enet_xdp_tx_xmit(struct fec_enet_private *fep,
81f601899eSWei Fang 				int cpu, struct xdp_buff *xdp,
82f601899eSWei Fang 				u32 dma_sync_len);
83772e42b0SChristoph Müllner 
84793fc096SFrank Li #define DRIVER_NAME	"fec"
85793fc096SFrank Li 
8652c4a1a8SFugang Duan static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2};
8752c4a1a8SFugang Duan 
88793fc096SFrank Li /* Pause frame feild and FIFO threshold */
89793fc096SFrank Li #define FEC_ENET_FCE	(1 << 5)
90793fc096SFrank Li #define FEC_ENET_RSEM_V	0x84
91793fc096SFrank Li #define FEC_ENET_RSFL_V	16
92793fc096SFrank Li #define FEC_ENET_RAEM_V	0x8
93793fc096SFrank Li #define FEC_ENET_RAFL_V	0x8
94793fc096SFrank Li #define FEC_ENET_OPD_V	0xFFF0
958fff755eSAndrew Lunn #define FEC_MDIO_PM_TIMEOUT  100 /* ms */
96793fc096SFrank Li 
976d6b39f1SShenwei Wang #define FEC_ENET_XDP_PASS          0
986d6b39f1SShenwei Wang #define FEC_ENET_XDP_CONSUMED      BIT(0)
996d6b39f1SShenwei Wang #define FEC_ENET_XDP_TX            BIT(1)
1006d6b39f1SShenwei Wang #define FEC_ENET_XDP_REDIR         BIT(2)
1016d6b39f1SShenwei Wang 
102da722186SMartin Fuzzey struct fec_devinfo {
103da722186SMartin Fuzzey 	u32 quirks;
104da722186SMartin Fuzzey };
105da722186SMartin Fuzzey 
106da722186SMartin Fuzzey static const struct fec_devinfo fec_imx25_info = {
107da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
108abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_FRREG | FEC_QUIRK_HAS_MDIO_C45,
109da722186SMartin Fuzzey };
110da722186SMartin Fuzzey 
111da722186SMartin Fuzzey static const struct fec_devinfo fec_imx27_info = {
112abc33494SGreg Ungerer 	.quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG |
113abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
114da722186SMartin Fuzzey };
115da722186SMartin Fuzzey 
116da722186SMartin Fuzzey static const struct fec_devinfo fec_imx28_info = {
117da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
118da722186SMartin Fuzzey 		  FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
119c730ab42SLaurent Badel 		  FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII |
120abc33494SGreg Ungerer 		  FEC_QUIRK_NO_HARD_RESET | FEC_QUIRK_HAS_MDIO_C45,
121da722186SMartin Fuzzey };
122da722186SMartin Fuzzey 
123da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6q_info = {
124da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
125da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
126da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
1277d650df9SWei Fang 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII |
128abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_PMQOS | FEC_QUIRK_HAS_MDIO_C45,
129da722186SMartin Fuzzey };
130da722186SMartin Fuzzey 
131da722186SMartin Fuzzey static const struct fec_devinfo fec_mvf600_info = {
132abc33494SGreg Ungerer 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC |
133abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
134da722186SMartin Fuzzey };
135da722186SMartin Fuzzey 
136da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6x_info = {
137da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
138da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
139da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
140da722186SMartin Fuzzey 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
1411e6114f5SGreg Ungerer 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
142abc33494SGreg Ungerer 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
143abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
144da722186SMartin Fuzzey };
145da722186SMartin Fuzzey 
146da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6ul_info = {
147da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
148da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
149da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
150da722186SMartin Fuzzey 		  FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
151abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII |
152abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
153da722186SMartin Fuzzey };
154da722186SMartin Fuzzey 
155947240ebSFugang Duan static const struct fec_devinfo fec_imx8mq_info = {
156947240ebSFugang Duan 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
157947240ebSFugang Duan 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
158947240ebSFugang Duan 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
159947240ebSFugang Duan 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
160947240ebSFugang Duan 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
161947240ebSFugang Duan 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
162abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2 |
163abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
164947240ebSFugang Duan };
165947240ebSFugang Duan 
166947240ebSFugang Duan static const struct fec_devinfo fec_imx8qm_info = {
167947240ebSFugang Duan 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
168947240ebSFugang Duan 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
169947240ebSFugang Duan 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
170947240ebSFugang Duan 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
171947240ebSFugang Duan 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
172947240ebSFugang Duan 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
173abc33494SGreg Ungerer 		  FEC_QUIRK_DELAYED_CLKS_SUPPORT | FEC_QUIRK_HAS_MDIO_C45,
174947240ebSFugang Duan };
175947240ebSFugang Duan 
176167d5fe0SWei Fang static const struct fec_devinfo fec_s32v234_info = {
177167d5fe0SWei Fang 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
178167d5fe0SWei Fang 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
179167d5fe0SWei Fang 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
180abc33494SGreg Ungerer 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
181abc33494SGreg Ungerer 		  FEC_QUIRK_HAS_MDIO_C45,
182167d5fe0SWei Fang };
183167d5fe0SWei Fang 
184793fc096SFrank Li static struct platform_device_id fec_devtype[] = {
185793fc096SFrank Li 	{
186793fc096SFrank Li 		/* keep it for coldfire */
187793fc096SFrank Li 		.name = DRIVER_NAME,
188793fc096SFrank Li 		.driver_data = 0,
189793fc096SFrank Li 	}, {
190793fc096SFrank Li 		.name = "imx25-fec",
191da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx25_info,
192793fc096SFrank Li 	}, {
193793fc096SFrank Li 		.name = "imx27-fec",
194da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx27_info,
195793fc096SFrank Li 	}, {
196793fc096SFrank Li 		.name = "imx28-fec",
197da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx28_info,
198793fc096SFrank Li 	}, {
199793fc096SFrank Li 		.name = "imx6q-fec",
200da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6q_info,
201793fc096SFrank Li 	}, {
20236803542SShawn Guo 		.name = "mvf600-fec",
203da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_mvf600_info,
204ca7c4a45SJingchang Lu 	}, {
20595a77470SFugang Duan 		.name = "imx6sx-fec",
206da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6x_info,
20795a77470SFugang Duan 	}, {
208a51d3ab5SFugang Duan 		.name = "imx6ul-fec",
209da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6ul_info,
210a51d3ab5SFugang Duan 	}, {
211947240ebSFugang Duan 		.name = "imx8mq-fec",
212947240ebSFugang Duan 		.driver_data = (kernel_ulong_t)&fec_imx8mq_info,
213947240ebSFugang Duan 	}, {
214947240ebSFugang Duan 		.name = "imx8qm-fec",
215947240ebSFugang Duan 		.driver_data = (kernel_ulong_t)&fec_imx8qm_info,
216947240ebSFugang Duan 	}, {
217167d5fe0SWei Fang 		.name = "s32v234-fec",
218167d5fe0SWei Fang 		.driver_data = (kernel_ulong_t)&fec_s32v234_info,
219167d5fe0SWei Fang 	}, {
220793fc096SFrank Li 		/* sentinel */
221793fc096SFrank Li 	}
222793fc096SFrank Li };
223793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype);
224793fc096SFrank Li 
225793fc096SFrank Li enum imx_fec_type {
226793fc096SFrank Li 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
227793fc096SFrank Li 	IMX27_FEC,	/* runs on i.mx27/35/51 */
228793fc096SFrank Li 	IMX28_FEC,
229793fc096SFrank Li 	IMX6Q_FEC,
23036803542SShawn Guo 	MVF600_FEC,
231ba593e00SFugang Duan 	IMX6SX_FEC,
232a51d3ab5SFugang Duan 	IMX6UL_FEC,
233947240ebSFugang Duan 	IMX8MQ_FEC,
234947240ebSFugang Duan 	IMX8QM_FEC,
235167d5fe0SWei Fang 	S32V234_FEC,
236793fc096SFrank Li };
237793fc096SFrank Li 
238793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = {
239793fc096SFrank Li 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
240793fc096SFrank Li 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
241793fc096SFrank Li 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
242793fc096SFrank Li 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
24336803542SShawn Guo 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
244ba593e00SFugang Duan 	{ .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
245a51d3ab5SFugang Duan 	{ .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
246947240ebSFugang Duan 	{ .compatible = "fsl,imx8mq-fec", .data = &fec_devtype[IMX8MQ_FEC], },
247947240ebSFugang Duan 	{ .compatible = "fsl,imx8qm-fec", .data = &fec_devtype[IMX8QM_FEC], },
248167d5fe0SWei Fang 	{ .compatible = "fsl,s32v234-fec", .data = &fec_devtype[S32V234_FEC], },
249793fc096SFrank Li 	{ /* sentinel */ }
250793fc096SFrank Li };
251793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids);
252793fc096SFrank Li 
253793fc096SFrank Li static unsigned char macaddr[ETH_ALEN];
254793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0);
255793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
256793fc096SFrank Li 
257793fc096SFrank Li #if defined(CONFIG_M5272)
258793fc096SFrank Li /*
259793fc096SFrank Li  * Some hardware gets it MAC address out of local flash memory.
260793fc096SFrank Li  * if this is non-zero then assume it is the address to get MAC from.
261793fc096SFrank Li  */
262793fc096SFrank Li #if defined(CONFIG_NETtel)
263793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006006
264793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
265793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006000
266793fc096SFrank Li #elif defined(CONFIG_CANCam)
267793fc096SFrank Li #define	FEC_FLASHMAC	0xf0020000
268793fc096SFrank Li #elif defined (CONFIG_M5272C3)
269793fc096SFrank Li #define	FEC_FLASHMAC	(0xffe04000 + 4)
270793fc096SFrank Li #elif defined(CONFIG_MOD5272)
271793fc096SFrank Li #define FEC_FLASHMAC	0xffc0406b
272793fc096SFrank Li #else
273793fc096SFrank Li #define	FEC_FLASHMAC	0
274793fc096SFrank Li #endif
275793fc096SFrank Li #endif /* CONFIG_M5272 */
276793fc096SFrank Li 
277cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
278fbbeefddSAndrew Lunn  *
279fbbeefddSAndrew Lunn  * 2048 byte skbufs are allocated. However, alignment requirements
280fbbeefddSAndrew Lunn  * varies between FEC variants. Worst case is 64, so round down by 64.
281793fc096SFrank Li  */
282fbbeefddSAndrew Lunn #define PKT_MAXBUF_SIZE		(round_down(2048 - 64, 64))
283793fc096SFrank Li #define PKT_MINBUF_SIZE		64
284793fc096SFrank Li 
2854c09eed9SJim Baxter /* FEC receive acceleration */
2864c09eed9SJim Baxter #define FEC_RACC_IPDIS		(1 << 1)
2874c09eed9SJim Baxter #define FEC_RACC_PRODIS		(1 << 2)
2883ac72b7bSEric Nelson #define FEC_RACC_SHIFT16	BIT(7)
2894c09eed9SJim Baxter #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
2904c09eed9SJim Baxter 
2912b30842bSAndrew Lunn /* MIB Control Register */
2922b30842bSAndrew Lunn #define FEC_MIB_CTRLSTAT_DISABLE	BIT(31)
2932b30842bSAndrew Lunn 
294793fc096SFrank Li /*
295793fc096SFrank Li  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
296793fc096SFrank Li  * size bits. Other FEC hardware does not, so we need to take that into
297793fc096SFrank Li  * account when setting it.
298793fc096SFrank Li  */
299793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
3003f1dcc6aSLucas Stach     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
3013f1dcc6aSLucas Stach     defined(CONFIG_ARM64)
302793fc096SFrank Li #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
303793fc096SFrank Li #else
304793fc096SFrank Li #define	OPT_FRAME_SIZE	0
305793fc096SFrank Li #endif
306793fc096SFrank Li 
307793fc096SFrank Li /* FEC MII MMFR bits definition */
308793fc096SFrank Li #define FEC_MMFR_ST		(1 << 30)
309d3ee8ec7SMarco Hartmann #define FEC_MMFR_ST_C45		(0)
310793fc096SFrank Li #define FEC_MMFR_OP_READ	(2 << 28)
311d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_READ_C45	(3 << 28)
312793fc096SFrank Li #define FEC_MMFR_OP_WRITE	(1 << 28)
313d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_ADDR_WRITE	(0)
314793fc096SFrank Li #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
315793fc096SFrank Li #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
316793fc096SFrank Li #define FEC_MMFR_TA		(2 << 16)
317793fc096SFrank Li #define FEC_MMFR_DATA(v)	(v & 0xffff)
318de40ed31SNimrod Andy /* FEC ECR bits definition */
3197b15515fSFrancesco Dolcini #define FEC_ECR_MAGICEN		(1 << 2)
3207b15515fSFrancesco Dolcini #define FEC_ECR_SLEEP		(1 << 3)
321793fc096SFrank Li 
322793fc096SFrank Li #define FEC_MII_TIMEOUT		30000 /* us */
323793fc096SFrank Li 
324793fc096SFrank Li /* Transmitter timeout */
325793fc096SFrank Li #define TX_TIMEOUT (2 * HZ)
326793fc096SFrank Li 
327793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG	0x1
328793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE	0x2
329de40ed31SNimrod Andy #define FEC_WOL_HAS_MAGIC_PACKET	(0x1 << 0)
330de40ed31SNimrod Andy #define FEC_WOL_FLAG_ENABLE		(0x1 << 1)
331de40ed31SNimrod Andy #define FEC_WOL_FLAG_SLEEP_ON		(0x1 << 2)
332793fc096SFrank Li 
33379f33912SNimrod Andy /* Max number of allowed TCP segments for software TSO */
33479f33912SNimrod Andy #define FEC_MAX_TSO_SEGS	100
33579f33912SNimrod Andy #define FEC_MAX_SKB_DESCS	(FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
33679f33912SNimrod Andy 
33779f33912SNimrod Andy #define IS_TSO_HEADER(txq, addr) \
33879f33912SNimrod Andy 	((addr >= txq->tso_hdrs_dma) && \
3397355f276STroy Kisky 	(addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
34079f33912SNimrod Andy 
341793fc096SFrank Li static int mii_cnt;
342793fc096SFrank Li 
3437355f276STroy Kisky static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
3447355f276STroy Kisky 					     struct bufdesc_prop *bd)
345793fc096SFrank Li {
3467355f276STroy Kisky 	return (bdp >= bd->last) ? bd->base
347145d6e29SFugang Duan 			: (struct bufdesc *)(((void *)bdp) + bd->dsize);
348793fc096SFrank Li }
349793fc096SFrank Li 
3507355f276STroy Kisky static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
3517355f276STroy Kisky 					     struct bufdesc_prop *bd)
35236e24e2eSDuan Fugang-B38611 {
3537355f276STroy Kisky 	return (bdp <= bd->base) ? bd->last
354145d6e29SFugang Duan 			: (struct bufdesc *)(((void *)bdp) - bd->dsize);
35536e24e2eSDuan Fugang-B38611 }
35636e24e2eSDuan Fugang-B38611 
3577355f276STroy Kisky static int fec_enet_get_bd_index(struct bufdesc *bdp,
3587355f276STroy Kisky 				 struct bufdesc_prop *bd)
35961a4427bSNimrod Andy {
3607355f276STroy Kisky 	return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
36161a4427bSNimrod Andy }
36261a4427bSNimrod Andy 
3637355f276STroy Kisky static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
3646e909283SNimrod Andy {
3656e909283SNimrod Andy 	int entries;
3666e909283SNimrod Andy 
3677355f276STroy Kisky 	entries = (((const char *)txq->dirty_tx -
3687355f276STroy Kisky 			(const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
3696e909283SNimrod Andy 
3707355f276STroy Kisky 	return entries >= 0 ? entries : entries + txq->bd.ring_size;
3716e909283SNimrod Andy }
3726e909283SNimrod Andy 
373c20e599bSLothar Waßmann static void swap_buffer(void *bufaddr, int len)
374793fc096SFrank Li {
375793fc096SFrank Li 	int i;
376793fc096SFrank Li 	unsigned int *buf = bufaddr;
377793fc096SFrank Li 
3787b487d07SLothar Waßmann 	for (i = 0; i < len; i += 4, buf++)
379e453789aSLothar Waßmann 		swab32s(buf);
380793fc096SFrank Li }
381793fc096SFrank Li 
382344756f6SRussell King static void fec_dump(struct net_device *ndev)
383344756f6SRussell King {
384344756f6SRussell King 	struct fec_enet_private *fep = netdev_priv(ndev);
3854d494cdcSFugang Duan 	struct bufdesc *bdp;
3864d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
3874d494cdcSFugang Duan 	int index = 0;
388344756f6SRussell King 
389344756f6SRussell King 	netdev_info(ndev, "TX ring dump\n");
390344756f6SRussell King 	pr_info("Nr     SC     addr       len  SKB\n");
391344756f6SRussell King 
3924d494cdcSFugang Duan 	txq = fep->tx_queue[0];
3937355f276STroy Kisky 	bdp = txq->bd.base;
3944d494cdcSFugang Duan 
395344756f6SRussell King 	do {
3965cfa3039SJohannes Berg 		pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
397344756f6SRussell King 			index,
3987355f276STroy Kisky 			bdp == txq->bd.cur ? 'S' : ' ',
3994d494cdcSFugang Duan 			bdp == txq->dirty_tx ? 'H' : ' ',
4005cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_sc),
4015cfa3039SJohannes Berg 			fec32_to_cpu(bdp->cbd_bufaddr),
4025cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_datlen),
403af6f4791SWei Fang 			txq->tx_buf[index].buf_p);
4047355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
405344756f6SRussell King 		index++;
4067355f276STroy Kisky 	} while (bdp != txq->bd.base);
407344756f6SRussell King }
408344756f6SRussell King 
40962a02c98SFugang Duan static inline bool is_ipv4_pkt(struct sk_buff *skb)
41062a02c98SFugang Duan {
41162a02c98SFugang Duan 	return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
41262a02c98SFugang Duan }
41362a02c98SFugang Duan 
4144c09eed9SJim Baxter static int
4154c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
4164c09eed9SJim Baxter {
4174c09eed9SJim Baxter 	/* Only run for packets requiring a checksum. */
4184c09eed9SJim Baxter 	if (skb->ip_summed != CHECKSUM_PARTIAL)
4194c09eed9SJim Baxter 		return 0;
4204c09eed9SJim Baxter 
4214c09eed9SJim Baxter 	if (unlikely(skb_cow_head(skb, 0)))
4224c09eed9SJim Baxter 		return -1;
4234c09eed9SJim Baxter 
42462a02c98SFugang Duan 	if (is_ipv4_pkt(skb))
42596c50caaSNimrod Andy 		ip_hdr(skb)->check = 0;
4264c09eed9SJim Baxter 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
4274c09eed9SJim Baxter 
4284c09eed9SJim Baxter 	return 0;
4294c09eed9SJim Baxter }
4304c09eed9SJim Baxter 
43195698ff6SShenwei Wang static int
43295698ff6SShenwei Wang fec_enet_create_page_pool(struct fec_enet_private *fep,
43395698ff6SShenwei Wang 			  struct fec_enet_priv_rx_q *rxq, int size)
43495698ff6SShenwei Wang {
4356d6b39f1SShenwei Wang 	struct bpf_prog *xdp_prog = READ_ONCE(fep->xdp_prog);
43695698ff6SShenwei Wang 	struct page_pool_params pp_params = {
43795698ff6SShenwei Wang 		.order = 0,
43895698ff6SShenwei Wang 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
43995698ff6SShenwei Wang 		.pool_size = size,
44095698ff6SShenwei Wang 		.nid = dev_to_node(&fep->pdev->dev),
44195698ff6SShenwei Wang 		.dev = &fep->pdev->dev,
4426d6b39f1SShenwei Wang 		.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
44395698ff6SShenwei Wang 		.offset = FEC_ENET_XDP_HEADROOM,
44495698ff6SShenwei Wang 		.max_len = FEC_ENET_RX_FRSIZE,
44595698ff6SShenwei Wang 	};
44695698ff6SShenwei Wang 	int err;
44795698ff6SShenwei Wang 
44895698ff6SShenwei Wang 	rxq->page_pool = page_pool_create(&pp_params);
44995698ff6SShenwei Wang 	if (IS_ERR(rxq->page_pool)) {
45095698ff6SShenwei Wang 		err = PTR_ERR(rxq->page_pool);
45195698ff6SShenwei Wang 		rxq->page_pool = NULL;
45295698ff6SShenwei Wang 		return err;
45395698ff6SShenwei Wang 	}
45495698ff6SShenwei Wang 
45595698ff6SShenwei Wang 	err = xdp_rxq_info_reg(&rxq->xdp_rxq, fep->netdev, rxq->id, 0);
45695698ff6SShenwei Wang 	if (err < 0)
45795698ff6SShenwei Wang 		goto err_free_pp;
45895698ff6SShenwei Wang 
45995698ff6SShenwei Wang 	err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
46095698ff6SShenwei Wang 					 rxq->page_pool);
46195698ff6SShenwei Wang 	if (err)
46295698ff6SShenwei Wang 		goto err_unregister_rxq;
46395698ff6SShenwei Wang 
46495698ff6SShenwei Wang 	return 0;
46595698ff6SShenwei Wang 
46695698ff6SShenwei Wang err_unregister_rxq:
46795698ff6SShenwei Wang 	xdp_rxq_info_unreg(&rxq->xdp_rxq);
46895698ff6SShenwei Wang err_free_pp:
46995698ff6SShenwei Wang 	page_pool_destroy(rxq->page_pool);
47095698ff6SShenwei Wang 	rxq->page_pool = NULL;
47195698ff6SShenwei Wang 	return err;
47295698ff6SShenwei Wang }
47395698ff6SShenwei Wang 
474c4bc44c6SKevin Hao static struct bufdesc *
4754d494cdcSFugang Duan fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
4764d494cdcSFugang Duan 			     struct sk_buff *skb,
4774d494cdcSFugang Duan 			     struct net_device *ndev)
4786e909283SNimrod Andy {
4796e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
4807355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
4816e909283SNimrod Andy 	struct bufdesc_ex *ebdp;
4826e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
4836e909283SNimrod Andy 	int frag, frag_len;
4846e909283SNimrod Andy 	unsigned short status;
4856e909283SNimrod Andy 	unsigned int estatus = 0;
4866e909283SNimrod Andy 	skb_frag_t *this_frag;
4876e909283SNimrod Andy 	unsigned int index;
4886e909283SNimrod Andy 	void *bufaddr;
489d6bf3143SRussell King 	dma_addr_t addr;
4906e909283SNimrod Andy 	int i;
4916e909283SNimrod Andy 
4926e909283SNimrod Andy 	for (frag = 0; frag < nr_frags; frag++) {
4936e909283SNimrod Andy 		this_frag = &skb_shinfo(skb)->frags[frag];
4947355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
4956e909283SNimrod Andy 		ebdp = (struct bufdesc_ex *)bdp;
4966e909283SNimrod Andy 
4975cfa3039SJohannes Berg 		status = fec16_to_cpu(bdp->cbd_sc);
4986e909283SNimrod Andy 		status &= ~BD_ENET_TX_STATS;
4996e909283SNimrod Andy 		status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
500d7840976SMatthew Wilcox (Oracle) 		frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
5016e909283SNimrod Andy 
5026e909283SNimrod Andy 		/* Handle the last BD specially */
5036e909283SNimrod Andy 		if (frag == nr_frags - 1) {
5046e909283SNimrod Andy 			status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
5056e909283SNimrod Andy 			if (fep->bufdesc_ex) {
5066e909283SNimrod Andy 				estatus |= BD_ENET_TX_INT;
5076e909283SNimrod Andy 				if (unlikely(skb_shinfo(skb)->tx_flags &
5086e909283SNimrod Andy 					SKBTX_HW_TSTAMP && fep->hwts_tx_en))
5096e909283SNimrod Andy 					estatus |= BD_ENET_TX_TS;
5106e909283SNimrod Andy 			}
5116e909283SNimrod Andy 		}
5126e909283SNimrod Andy 
5136e909283SNimrod Andy 		if (fep->bufdesc_ex) {
5146b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_HAS_AVB)
51553bb20d1STroy Kisky 				estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
5166e909283SNimrod Andy 			if (skb->ip_summed == CHECKSUM_PARTIAL)
5176e909283SNimrod Andy 				estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
518471ff445SJoakim Zhang 
5196e909283SNimrod Andy 			ebdp->cbd_bdu = 0;
5205cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(estatus);
5216e909283SNimrod Andy 		}
5226e909283SNimrod Andy 
523d7840976SMatthew Wilcox (Oracle) 		bufaddr = skb_frag_address(this_frag);
5246e909283SNimrod Andy 
5257355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
52641ef84ceSFugang Duan 		if (((unsigned long) bufaddr) & fep->tx_align ||
5276b7e4008SLothar Waßmann 			fep->quirks & FEC_QUIRK_SWAP_FRAME) {
5284d494cdcSFugang Duan 			memcpy(txq->tx_bounce[index], bufaddr, frag_len);
5294d494cdcSFugang Duan 			bufaddr = txq->tx_bounce[index];
5306e909283SNimrod Andy 
5316b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
5326e909283SNimrod Andy 				swap_buffer(bufaddr, frag_len);
5336e909283SNimrod Andy 		}
5346e909283SNimrod Andy 
535d6bf3143SRussell King 		addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
536d6bf3143SRussell King 				      DMA_TO_DEVICE);
537d6bf3143SRussell King 		if (dma_mapping_error(&fep->pdev->dev, addr)) {
5386e909283SNimrod Andy 			if (net_ratelimit())
5396e909283SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
5406e909283SNimrod Andy 			goto dma_mapping_error;
5416e909283SNimrod Andy 		}
5426e909283SNimrod Andy 
5435cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(addr);
5445cfa3039SJohannes Berg 		bdp->cbd_datlen = cpu_to_fec16(frag_len);
545be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
546be293467STroy Kisky 		 * performed before transferring ownership.
547be293467STroy Kisky 		 */
548be293467STroy Kisky 		wmb();
5495cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(status);
5506e909283SNimrod Andy 	}
5516e909283SNimrod Andy 
552c4bc44c6SKevin Hao 	return bdp;
5536e909283SNimrod Andy dma_mapping_error:
5547355f276STroy Kisky 	bdp = txq->bd.cur;
5556e909283SNimrod Andy 	for (i = 0; i < frag; i++) {
5567355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
5575cfa3039SJohannes Berg 		dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
5585cfa3039SJohannes Berg 				 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
5596e909283SNimrod Andy 	}
560c4bc44c6SKevin Hao 	return ERR_PTR(-ENOMEM);
5616e909283SNimrod Andy }
5626e909283SNimrod Andy 
5634d494cdcSFugang Duan static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
5644d494cdcSFugang Duan 				   struct sk_buff *skb, struct net_device *ndev)
5656e909283SNimrod Andy {
5666e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
5676e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
5686e909283SNimrod Andy 	struct bufdesc *bdp, *last_bdp;
5696e909283SNimrod Andy 	void *bufaddr;
570d6bf3143SRussell King 	dma_addr_t addr;
5716e909283SNimrod Andy 	unsigned short status;
5726e909283SNimrod Andy 	unsigned short buflen;
5736e909283SNimrod Andy 	unsigned int estatus = 0;
5746e909283SNimrod Andy 	unsigned int index;
57579f33912SNimrod Andy 	int entries_free;
5766e909283SNimrod Andy 
5777355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
57879f33912SNimrod Andy 	if (entries_free < MAX_SKB_FRAGS + 1) {
57979f33912SNimrod Andy 		dev_kfree_skb_any(skb);
58079f33912SNimrod Andy 		if (net_ratelimit())
58179f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for SG!\n");
58279f33912SNimrod Andy 		return NETDEV_TX_OK;
58379f33912SNimrod Andy 	}
58479f33912SNimrod Andy 
5856e909283SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
5866e909283SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
5876e909283SNimrod Andy 		dev_kfree_skb_any(skb);
5886e909283SNimrod Andy 		return NETDEV_TX_OK;
5896e909283SNimrod Andy 	}
5906e909283SNimrod Andy 
5916e909283SNimrod Andy 	/* Fill in a Tx ring entry */
5927355f276STroy Kisky 	bdp = txq->bd.cur;
593c4bc44c6SKevin Hao 	last_bdp = bdp;
5945cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
5956e909283SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
5966e909283SNimrod Andy 
5976e909283SNimrod Andy 	/* Set buffer length and buffer pointer */
5986e909283SNimrod Andy 	bufaddr = skb->data;
5996e909283SNimrod Andy 	buflen = skb_headlen(skb);
6006e909283SNimrod Andy 
6017355f276STroy Kisky 	index = fec_enet_get_bd_index(bdp, &txq->bd);
60241ef84ceSFugang Duan 	if (((unsigned long) bufaddr) & fep->tx_align ||
6036b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
6044d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, buflen);
6054d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
6066e909283SNimrod Andy 
6076b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
6086e909283SNimrod Andy 			swap_buffer(bufaddr, buflen);
6096e909283SNimrod Andy 	}
6106e909283SNimrod Andy 
611d6bf3143SRussell King 	/* Push the data cache so the CPM does not get stale memory data. */
612d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
613d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
6146e909283SNimrod Andy 		dev_kfree_skb_any(skb);
6156e909283SNimrod Andy 		if (net_ratelimit())
6166e909283SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
6176e909283SNimrod Andy 		return NETDEV_TX_OK;
6186e909283SNimrod Andy 	}
6196e909283SNimrod Andy 
6206e909283SNimrod Andy 	if (nr_frags) {
621c4bc44c6SKevin Hao 		last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
622fc75ba51STroy Kisky 		if (IS_ERR(last_bdp)) {
623fc75ba51STroy Kisky 			dma_unmap_single(&fep->pdev->dev, addr,
624fc75ba51STroy Kisky 					 buflen, DMA_TO_DEVICE);
625fc75ba51STroy Kisky 			dev_kfree_skb_any(skb);
626c4bc44c6SKevin Hao 			return NETDEV_TX_OK;
627fc75ba51STroy Kisky 		}
6286e909283SNimrod Andy 	} else {
6296e909283SNimrod Andy 		status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
6306e909283SNimrod Andy 		if (fep->bufdesc_ex) {
6316e909283SNimrod Andy 			estatus = BD_ENET_TX_INT;
6326e909283SNimrod Andy 			if (unlikely(skb_shinfo(skb)->tx_flags &
6336e909283SNimrod Andy 				SKBTX_HW_TSTAMP && fep->hwts_tx_en))
6346e909283SNimrod Andy 				estatus |= BD_ENET_TX_TS;
6356e909283SNimrod Andy 		}
6366e909283SNimrod Andy 	}
637fc75ba51STroy Kisky 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
638fc75ba51STroy Kisky 	bdp->cbd_datlen = cpu_to_fec16(buflen);
6396e909283SNimrod Andy 
6406e909283SNimrod Andy 	if (fep->bufdesc_ex) {
6416e909283SNimrod Andy 
6426e909283SNimrod Andy 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
6436e909283SNimrod Andy 
6446e909283SNimrod Andy 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
6456e909283SNimrod Andy 			fep->hwts_tx_en))
6466e909283SNimrod Andy 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
6476e909283SNimrod Andy 
6486b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
64953bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
650befe8213SNimrod Andy 
6516e909283SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
6526e909283SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
6536e909283SNimrod Andy 
6546e909283SNimrod Andy 		ebdp->cbd_bdu = 0;
6555cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
6566e909283SNimrod Andy 	}
6576e909283SNimrod Andy 
6587355f276STroy Kisky 	index = fec_enet_get_bd_index(last_bdp, &txq->bd);
6596e909283SNimrod Andy 	/* Save skb pointer */
660af6f4791SWei Fang 	txq->tx_buf[index].buf_p = skb;
6616e909283SNimrod Andy 
662be293467STroy Kisky 	/* Make sure the updates to rest of the descriptor are performed before
663be293467STroy Kisky 	 * transferring ownership.
664be293467STroy Kisky 	 */
665be293467STroy Kisky 	wmb();
6666e909283SNimrod Andy 
6676e909283SNimrod Andy 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
6686e909283SNimrod Andy 	 * it's the last BD of the frame, and to put the CRC on the end.
6696e909283SNimrod Andy 	 */
6706e909283SNimrod Andy 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
6715cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
6726e909283SNimrod Andy 
673793fc096SFrank Li 	/* If this was the last BD in the ring, start at the beginning again. */
6747355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
675793fc096SFrank Li 
6767a2a8451SEric Dumazet 	skb_tx_timestamp(skb);
6777a2a8451SEric Dumazet 
67820f79739SWei Fang 	/* Make sure the update to bdp is performed before txq->bd.cur. */
679c4bc44c6SKevin Hao 	wmb();
6807355f276STroy Kisky 	txq->bd.cur = bdp;
681793fc096SFrank Li 
682793fc096SFrank Li 	/* Trigger transmission start */
68353bb20d1STroy Kisky 	writel(0, txq->bd.reg_desc_active);
684793fc096SFrank Li 
6856e909283SNimrod Andy 	return 0;
686793fc096SFrank Li }
687793fc096SFrank Li 
68879f33912SNimrod Andy static int
6894d494cdcSFugang Duan fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
6904d494cdcSFugang Duan 			  struct net_device *ndev,
69179f33912SNimrod Andy 			  struct bufdesc *bdp, int index, char *data,
69279f33912SNimrod Andy 			  int size, bool last_tcp, bool is_last)
69379f33912SNimrod Andy {
69479f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
69561cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
69679f33912SNimrod Andy 	unsigned short status;
69779f33912SNimrod Andy 	unsigned int estatus = 0;
698d6bf3143SRussell King 	dma_addr_t addr;
69979f33912SNimrod Andy 
7005cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
70179f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
70279f33912SNimrod Andy 
70379f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
70479f33912SNimrod Andy 
70541ef84ceSFugang Duan 	if (((unsigned long) data) & fep->tx_align ||
7066b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
7074d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], data, size);
7084d494cdcSFugang Duan 		data = txq->tx_bounce[index];
70979f33912SNimrod Andy 
7106b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
71179f33912SNimrod Andy 			swap_buffer(data, size);
71279f33912SNimrod Andy 	}
71379f33912SNimrod Andy 
714d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
715d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
71679f33912SNimrod Andy 		dev_kfree_skb_any(skb);
71779f33912SNimrod Andy 		if (net_ratelimit())
71879f33912SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
71906a4df58SZhang Changzhong 		return NETDEV_TX_OK;
72079f33912SNimrod Andy 	}
72179f33912SNimrod Andy 
7225cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(size);
7235cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
724d6bf3143SRussell King 
72579f33912SNimrod Andy 	if (fep->bufdesc_ex) {
7266b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
72753bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
72879f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
72979f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
73079f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
7315cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
73279f33912SNimrod Andy 	}
73379f33912SNimrod Andy 
73479f33912SNimrod Andy 	/* Handle the last BD specially */
73579f33912SNimrod Andy 	if (last_tcp)
73679f33912SNimrod Andy 		status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
73779f33912SNimrod Andy 	if (is_last) {
73879f33912SNimrod Andy 		status |= BD_ENET_TX_INTR;
73979f33912SNimrod Andy 		if (fep->bufdesc_ex)
7405cfa3039SJohannes Berg 			ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
74179f33912SNimrod Andy 	}
74279f33912SNimrod Andy 
7435cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
74479f33912SNimrod Andy 
74579f33912SNimrod Andy 	return 0;
74679f33912SNimrod Andy }
74779f33912SNimrod Andy 
74879f33912SNimrod Andy static int
7494d494cdcSFugang Duan fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
7504d494cdcSFugang Duan 			 struct sk_buff *skb, struct net_device *ndev,
75179f33912SNimrod Andy 			 struct bufdesc *bdp, int index)
75279f33912SNimrod Andy {
75379f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
754504148feSEric Dumazet 	int hdr_len = skb_tcp_all_headers(skb);
75561cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
75679f33912SNimrod Andy 	void *bufaddr;
75779f33912SNimrod Andy 	unsigned long dmabuf;
75879f33912SNimrod Andy 	unsigned short status;
75979f33912SNimrod Andy 	unsigned int estatus = 0;
76079f33912SNimrod Andy 
7615cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
76279f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
76379f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
76479f33912SNimrod Andy 
7654d494cdcSFugang Duan 	bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
7664d494cdcSFugang Duan 	dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
76741ef84ceSFugang Duan 	if (((unsigned long)bufaddr) & fep->tx_align ||
7686b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
7694d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, hdr_len);
7704d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
77179f33912SNimrod Andy 
7726b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
77379f33912SNimrod Andy 			swap_buffer(bufaddr, hdr_len);
77479f33912SNimrod Andy 
77579f33912SNimrod Andy 		dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
77679f33912SNimrod Andy 					hdr_len, DMA_TO_DEVICE);
77779f33912SNimrod Andy 		if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
77879f33912SNimrod Andy 			dev_kfree_skb_any(skb);
77979f33912SNimrod Andy 			if (net_ratelimit())
78079f33912SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
78106a4df58SZhang Changzhong 			return NETDEV_TX_OK;
78279f33912SNimrod Andy 		}
78379f33912SNimrod Andy 	}
78479f33912SNimrod Andy 
7855cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
7865cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(hdr_len);
78779f33912SNimrod Andy 
78879f33912SNimrod Andy 	if (fep->bufdesc_ex) {
7896b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
79053bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
79179f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
79279f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
79379f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
7945cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
79579f33912SNimrod Andy 	}
79679f33912SNimrod Andy 
7975cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
79879f33912SNimrod Andy 
79979f33912SNimrod Andy 	return 0;
80079f33912SNimrod Andy }
80179f33912SNimrod Andy 
8024d494cdcSFugang Duan static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
8034d494cdcSFugang Duan 				   struct sk_buff *skb,
8044d494cdcSFugang Duan 				   struct net_device *ndev)
80579f33912SNimrod Andy {
80679f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
807761b331cSEric Dumazet 	int hdr_len, total_len, data_left;
8087355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
80979f33912SNimrod Andy 	struct tso_t tso;
81079f33912SNimrod Andy 	unsigned int index = 0;
81179f33912SNimrod Andy 	int ret;
81279f33912SNimrod Andy 
8137355f276STroy Kisky 	if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
81479f33912SNimrod Andy 		dev_kfree_skb_any(skb);
81579f33912SNimrod Andy 		if (net_ratelimit())
81679f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for TSO!\n");
81779f33912SNimrod Andy 		return NETDEV_TX_OK;
81879f33912SNimrod Andy 	}
81979f33912SNimrod Andy 
82079f33912SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
82179f33912SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
82279f33912SNimrod Andy 		dev_kfree_skb_any(skb);
82379f33912SNimrod Andy 		return NETDEV_TX_OK;
82479f33912SNimrod Andy 	}
82579f33912SNimrod Andy 
82679f33912SNimrod Andy 	/* Initialize the TSO handler, and prepare the first payload */
827761b331cSEric Dumazet 	hdr_len = tso_start(skb, &tso);
82879f33912SNimrod Andy 
82979f33912SNimrod Andy 	total_len = skb->len - hdr_len;
83079f33912SNimrod Andy 	while (total_len > 0) {
83179f33912SNimrod Andy 		char *hdr;
83279f33912SNimrod Andy 
8337355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
83479f33912SNimrod Andy 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
83579f33912SNimrod Andy 		total_len -= data_left;
83679f33912SNimrod Andy 
83779f33912SNimrod Andy 		/* prepare packet headers: MAC + IP + TCP */
8384d494cdcSFugang Duan 		hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
83979f33912SNimrod Andy 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
8404d494cdcSFugang Duan 		ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
84179f33912SNimrod Andy 		if (ret)
84279f33912SNimrod Andy 			goto err_release;
84379f33912SNimrod Andy 
84479f33912SNimrod Andy 		while (data_left > 0) {
84579f33912SNimrod Andy 			int size;
84679f33912SNimrod Andy 
84779f33912SNimrod Andy 			size = min_t(int, tso.size, data_left);
8487355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
8497355f276STroy Kisky 			index = fec_enet_get_bd_index(bdp, &txq->bd);
8504d494cdcSFugang Duan 			ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
8514d494cdcSFugang Duan 							bdp, index,
8524d494cdcSFugang Duan 							tso.data, size,
8534d494cdcSFugang Duan 							size == data_left,
85479f33912SNimrod Andy 							total_len == 0);
85579f33912SNimrod Andy 			if (ret)
85679f33912SNimrod Andy 				goto err_release;
85779f33912SNimrod Andy 
85879f33912SNimrod Andy 			data_left -= size;
85979f33912SNimrod Andy 			tso_build_data(skb, &tso, size);
86079f33912SNimrod Andy 		}
86179f33912SNimrod Andy 
8627355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
86379f33912SNimrod Andy 	}
86479f33912SNimrod Andy 
86579f33912SNimrod Andy 	/* Save skb pointer */
866af6f4791SWei Fang 	txq->tx_buf[index].buf_p = skb;
86779f33912SNimrod Andy 
86879f33912SNimrod Andy 	skb_tx_timestamp(skb);
8697355f276STroy Kisky 	txq->bd.cur = bdp;
87079f33912SNimrod Andy 
87179f33912SNimrod Andy 	/* Trigger transmission start */
8726b7e4008SLothar Waßmann 	if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
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 	    !readl(txq->bd.reg_desc_active))
87753bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
87879f33912SNimrod Andy 
87979f33912SNimrod Andy 	return 0;
88079f33912SNimrod Andy 
88179f33912SNimrod Andy err_release:
88279f33912SNimrod Andy 	/* TODO: Release all used data descriptors for TSO */
88379f33912SNimrod Andy 	return ret;
88479f33912SNimrod Andy }
88579f33912SNimrod Andy 
88661a4427bSNimrod Andy static netdev_tx_t
88761a4427bSNimrod Andy fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
88861a4427bSNimrod Andy {
88961a4427bSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
8906e909283SNimrod Andy 	int entries_free;
8914d494cdcSFugang Duan 	unsigned short queue;
8924d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
8934d494cdcSFugang Duan 	struct netdev_queue *nq;
89461a4427bSNimrod Andy 	int ret;
89561a4427bSNimrod Andy 
8964d494cdcSFugang Duan 	queue = skb_get_queue_mapping(skb);
8974d494cdcSFugang Duan 	txq = fep->tx_queue[queue];
8984d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue);
8994d494cdcSFugang Duan 
90079f33912SNimrod Andy 	if (skb_is_gso(skb))
9014d494cdcSFugang Duan 		ret = fec_enet_txq_submit_tso(txq, skb, ndev);
90279f33912SNimrod Andy 	else
9034d494cdcSFugang Duan 		ret = fec_enet_txq_submit_skb(txq, skb, ndev);
9046e909283SNimrod Andy 	if (ret)
9056e909283SNimrod Andy 		return ret;
90661a4427bSNimrod Andy 
9077355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
9084d494cdcSFugang Duan 	if (entries_free <= txq->tx_stop_threshold)
9094d494cdcSFugang Duan 		netif_tx_stop_queue(nq);
91061a4427bSNimrod Andy 
91161a4427bSNimrod Andy 	return NETDEV_TX_OK;
91261a4427bSNimrod Andy }
91361a4427bSNimrod Andy 
914a210576cSDavid S. Miller /* Init RX & TX buffer descriptors
915a210576cSDavid S. Miller  */
916a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev)
917a210576cSDavid S. Miller {
918a210576cSDavid S. Miller 	struct fec_enet_private *fep = netdev_priv(dev);
9194d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
9204d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
921a210576cSDavid S. Miller 	struct bufdesc *bdp;
922a210576cSDavid S. Miller 	unsigned int i;
92359d0f746SFrank Li 	unsigned int q;
924a210576cSDavid S. Miller 
92559d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
926a210576cSDavid S. Miller 		/* Initialize the receive buffer descriptors. */
92759d0f746SFrank Li 		rxq = fep->rx_queue[q];
9287355f276STroy Kisky 		bdp = rxq->bd.base;
9294d494cdcSFugang Duan 
9307355f276STroy Kisky 		for (i = 0; i < rxq->bd.ring_size; i++) {
931a210576cSDavid S. Miller 
932a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
933a210576cSDavid S. Miller 			if (bdp->cbd_bufaddr)
9345cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
935a210576cSDavid S. Miller 			else
9365cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(0);
9377355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
938a210576cSDavid S. Miller 		}
939a210576cSDavid S. Miller 
940a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
9417355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
9425cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
943a210576cSDavid S. Miller 
9447355f276STroy Kisky 		rxq->bd.cur = rxq->bd.base;
94559d0f746SFrank Li 	}
946a210576cSDavid S. Miller 
94759d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
948a210576cSDavid S. Miller 		/* ...and the same for transmit */
94959d0f746SFrank Li 		txq = fep->tx_queue[q];
9507355f276STroy Kisky 		bdp = txq->bd.base;
9517355f276STroy Kisky 		txq->bd.cur = bdp;
952a210576cSDavid S. Miller 
9537355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
954a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
9555cfa3039SJohannes Berg 			bdp->cbd_sc = cpu_to_fec16(0);
95620f79739SWei Fang 			if (txq->tx_buf[i].type == FEC_TXBUF_T_SKB) {
957178e5f57SFugang Duan 				if (bdp->cbd_bufaddr &&
958178e5f57SFugang Duan 				    !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
959178e5f57SFugang Duan 					dma_unmap_single(&fep->pdev->dev,
960178e5f57SFugang Duan 							 fec32_to_cpu(bdp->cbd_bufaddr),
961178e5f57SFugang Duan 							 fec16_to_cpu(bdp->cbd_datlen),
962178e5f57SFugang Duan 							 DMA_TO_DEVICE);
963af6f4791SWei Fang 				if (txq->tx_buf[i].buf_p)
964af6f4791SWei Fang 					dev_kfree_skb_any(txq->tx_buf[i].buf_p);
965af6f4791SWei Fang 			} else if (txq->tx_buf[i].type == FEC_TXBUF_T_XDP_NDO) {
966af6f4791SWei Fang 				if (bdp->cbd_bufaddr)
96720f79739SWei Fang 					dma_unmap_single(&fep->pdev->dev,
96820f79739SWei Fang 							 fec32_to_cpu(bdp->cbd_bufaddr),
96920f79739SWei Fang 							 fec16_to_cpu(bdp->cbd_datlen),
97020f79739SWei Fang 							 DMA_TO_DEVICE);
97120f79739SWei Fang 
972af6f4791SWei Fang 				if (txq->tx_buf[i].buf_p)
973af6f4791SWei Fang 					xdp_return_frame(txq->tx_buf[i].buf_p);
974af6f4791SWei Fang 			} else {
975af6f4791SWei Fang 				struct page *page = txq->tx_buf[i].buf_p;
976af6f4791SWei Fang 
977af6f4791SWei Fang 				if (page)
978af6f4791SWei Fang 					page_pool_put_page(page->pp, page, 0, false);
97920f79739SWei Fang 			}
98020f79739SWei Fang 
981af6f4791SWei Fang 			txq->tx_buf[i].buf_p = NULL;
98220f79739SWei Fang 			/* restore default tx buffer type: FEC_TXBUF_T_SKB */
98320f79739SWei Fang 			txq->tx_buf[i].type = FEC_TXBUF_T_SKB;
9845cfa3039SJohannes Berg 			bdp->cbd_bufaddr = cpu_to_fec32(0);
9857355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
986a210576cSDavid S. Miller 		}
987a210576cSDavid S. Miller 
988a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
9897355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
9905cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
9914d494cdcSFugang Duan 		txq->dirty_tx = bdp;
992a210576cSDavid S. Miller 	}
99359d0f746SFrank Li }
99459d0f746SFrank Li 
995ce99d0d3SFrank Li static void fec_enet_active_rxring(struct net_device *ndev)
996ce99d0d3SFrank Li {
997ce99d0d3SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
998ce99d0d3SFrank Li 	int i;
999ce99d0d3SFrank Li 
1000ce99d0d3SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
100153bb20d1STroy Kisky 		writel(0, fep->rx_queue[i]->bd.reg_desc_active);
1002ce99d0d3SFrank Li }
1003ce99d0d3SFrank Li 
100459d0f746SFrank Li static void fec_enet_enable_ring(struct net_device *ndev)
100559d0f746SFrank Li {
100659d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
100759d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
100859d0f746SFrank Li 	struct fec_enet_priv_rx_q *rxq;
100959d0f746SFrank Li 	int i;
101059d0f746SFrank Li 
101159d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
101259d0f746SFrank Li 		rxq = fep->rx_queue[i];
10137355f276STroy Kisky 		writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
1014fbbeefddSAndrew Lunn 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
101559d0f746SFrank Li 
101659d0f746SFrank Li 		/* enable DMA1/2 */
101759d0f746SFrank Li 		if (i)
101859d0f746SFrank Li 			writel(RCMR_MATCHEN | RCMR_CMP(i),
101959d0f746SFrank Li 			       fep->hwp + FEC_RCMR(i));
102059d0f746SFrank Li 	}
102159d0f746SFrank Li 
102259d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
102359d0f746SFrank Li 		txq = fep->tx_queue[i];
10247355f276STroy Kisky 		writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
102559d0f746SFrank Li 
102659d0f746SFrank Li 		/* enable DMA1/2 */
102759d0f746SFrank Li 		if (i)
102859d0f746SFrank Li 			writel(DMA_CLASS_EN | IDLE_SLOPE(i),
102959d0f746SFrank Li 			       fep->hwp + FEC_DMA_CFG(i));
103059d0f746SFrank Li 	}
103159d0f746SFrank Li }
103259d0f746SFrank Li 
1033dbc64a8eSRussell King /*
1034dbc64a8eSRussell King  * This function is called to start or restart the FEC during a link
1035dbc64a8eSRussell King  * change, transmit timeout, or to reconfigure the FEC.  The network
1036dbc64a8eSRussell King  * packet processing for this device must be stopped before this call.
1037793fc096SFrank Li  */
1038793fc096SFrank Li static void
1039ef83337dSRussell King fec_restart(struct net_device *ndev)
1040793fc096SFrank Li {
1041793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1042793fc096SFrank Li 	u32 temp_mac[2];
1043793fc096SFrank Li 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
1044793fc096SFrank Li 	u32 ecntl = 0x2; /* ETHEREN */
1045793fc096SFrank Li 
1046106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
1047106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1048106c314cSFugang Duan 	 * instead of reset MAC itself.
1049106c314cSFugang Duan 	 */
1050471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES ||
1051c730ab42SLaurent Badel 	    ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) {
1052106c314cSFugang Duan 		writel(0, fep->hwp + FEC_ECNTRL);
1053106c314cSFugang Duan 	} else {
1054793fc096SFrank Li 		writel(1, fep->hwp + FEC_ECNTRL);
1055793fc096SFrank Li 		udelay(10);
1056106c314cSFugang Duan 	}
1057793fc096SFrank Li 
1058793fc096SFrank Li 	/*
1059793fc096SFrank Li 	 * enet-mac reset will reset mac address registers too,
1060793fc096SFrank Li 	 * so need to reconfigure it.
1061793fc096SFrank Li 	 */
1062793fc096SFrank Li 	memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
10635cfa3039SJohannes Berg 	writel((__force u32)cpu_to_be32(temp_mac[0]),
10645cfa3039SJohannes Berg 	       fep->hwp + FEC_ADDR_LOW);
10655cfa3039SJohannes Berg 	writel((__force u32)cpu_to_be32(temp_mac[1]),
10665cfa3039SJohannes Berg 	       fep->hwp + FEC_ADDR_HIGH);
1067793fc096SFrank Li 
1068f166f890SAndrew Lunn 	/* Clear any outstanding interrupt, except MDIO. */
1069f166f890SAndrew Lunn 	writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
1070793fc096SFrank Li 
1071a210576cSDavid S. Miller 	fec_enet_bd_init(ndev);
1072a210576cSDavid S. Miller 
107359d0f746SFrank Li 	fec_enet_enable_ring(ndev);
1074793fc096SFrank Li 
1075793fc096SFrank Li 	/* Enable MII mode */
1076ef83337dSRussell King 	if (fep->full_duplex == DUPLEX_FULL) {
1077793fc096SFrank Li 		/* FD enable */
1078793fc096SFrank Li 		writel(0x04, fep->hwp + FEC_X_CNTRL);
1079793fc096SFrank Li 	} else {
1080793fc096SFrank Li 		/* No Rcv on Xmit */
1081793fc096SFrank Li 		rcntl |= 0x02;
1082793fc096SFrank Li 		writel(0x0, fep->hwp + FEC_X_CNTRL);
1083793fc096SFrank Li 	}
1084793fc096SFrank Li 
1085793fc096SFrank Li 	/* Set MII speed */
1086793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1087793fc096SFrank Li 
1088d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
108918803495SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_HAS_RACC) {
109032d1bbb1SGeert Uytterhoeven 		u32 val = readl(fep->hwp + FEC_RACC);
109132d1bbb1SGeert Uytterhoeven 
10923ac72b7bSEric Nelson 		/* align IP header */
10933ac72b7bSEric Nelson 		val |= FEC_RACC_SHIFT16;
10944c09eed9SJim Baxter 		if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
10953ac72b7bSEric Nelson 			/* set RX checksum */
10964c09eed9SJim Baxter 			val |= FEC_RACC_OPTIONS;
10974c09eed9SJim Baxter 		else
10984c09eed9SJim Baxter 			val &= ~FEC_RACC_OPTIONS;
10994c09eed9SJim Baxter 		writel(val, fep->hwp + FEC_RACC);
110055cd48c8STroy Kisky 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
110132867fccSFabio Estevam 	}
1102d1391930SGuenter Roeck #endif
11034c09eed9SJim Baxter 
1104793fc096SFrank Li 	/*
1105793fc096SFrank Li 	 * The phy interface and speed need to get configured
1106793fc096SFrank Li 	 * differently on enet-mac.
1107793fc096SFrank Li 	 */
11086b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1109793fc096SFrank Li 		/* Enable flow control and length check */
1110793fc096SFrank Li 		rcntl |= 0x40000000 | 0x00000020;
1111793fc096SFrank Li 
1112793fc096SFrank Li 		/* RGMII, RMII or MII */
1113e813bb2bSMarkus Pargmann 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
1114e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1115e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1116e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1117793fc096SFrank Li 			rcntl |= (1 << 6);
1118793fc096SFrank Li 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1119793fc096SFrank Li 			rcntl |= (1 << 8);
1120793fc096SFrank Li 		else
1121793fc096SFrank Li 			rcntl &= ~(1 << 8);
1122793fc096SFrank Li 
1123793fc096SFrank Li 		/* 1G, 100M or 10M */
112445f5c327SPhilippe Reynes 		if (ndev->phydev) {
112545f5c327SPhilippe Reynes 			if (ndev->phydev->speed == SPEED_1000)
1126793fc096SFrank Li 				ecntl |= (1 << 5);
112745f5c327SPhilippe Reynes 			else if (ndev->phydev->speed == SPEED_100)
1128793fc096SFrank Li 				rcntl &= ~(1 << 9);
1129793fc096SFrank Li 			else
1130793fc096SFrank Li 				rcntl |= (1 << 9);
1131793fc096SFrank Li 		}
1132793fc096SFrank Li 	} else {
1133793fc096SFrank Li #ifdef FEC_MIIGSK_ENR
11346b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1135793fc096SFrank Li 			u32 cfgr;
1136793fc096SFrank Li 			/* disable the gasket and wait */
1137793fc096SFrank Li 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
1138793fc096SFrank Li 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1139793fc096SFrank Li 				udelay(1);
1140793fc096SFrank Li 
1141793fc096SFrank Li 			/*
1142793fc096SFrank Li 			 * configure the gasket:
1143793fc096SFrank Li 			 *   RMII, 50 MHz, no loopback, no echo
1144793fc096SFrank Li 			 *   MII, 25 MHz, no loopback, no echo
1145793fc096SFrank Li 			 */
1146793fc096SFrank Li 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1147793fc096SFrank Li 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
114845f5c327SPhilippe Reynes 			if (ndev->phydev && ndev->phydev->speed == SPEED_10)
1149793fc096SFrank Li 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1150793fc096SFrank Li 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1151793fc096SFrank Li 
1152793fc096SFrank Li 			/* re-enable the gasket */
1153793fc096SFrank Li 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
1154793fc096SFrank Li 		}
1155793fc096SFrank Li #endif
1156793fc096SFrank Li 	}
1157793fc096SFrank Li 
1158d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1159793fc096SFrank Li 	/* enable pause frame*/
1160793fc096SFrank Li 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1161793fc096SFrank Li 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
116245f5c327SPhilippe Reynes 	     ndev->phydev && ndev->phydev->pause)) {
1163793fc096SFrank Li 		rcntl |= FEC_ENET_FCE;
1164793fc096SFrank Li 
11654c09eed9SJim Baxter 		/* set FIFO threshold parameter to reduce overrun */
1166793fc096SFrank Li 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1167793fc096SFrank Li 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1168793fc096SFrank Li 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1169793fc096SFrank Li 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1170793fc096SFrank Li 
1171793fc096SFrank Li 		/* OPD */
1172793fc096SFrank Li 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1173793fc096SFrank Li 	} else {
1174793fc096SFrank Li 		rcntl &= ~FEC_ENET_FCE;
1175793fc096SFrank Li 	}
1176d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
1177793fc096SFrank Li 
1178793fc096SFrank Li 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
1179793fc096SFrank Li 
118084fe6182SStefan Wahren 	/* Setup multicast filter. */
118184fe6182SStefan Wahren 	set_multicast_list(ndev);
118284fe6182SStefan Wahren #ifndef CONFIG_M5272
118384fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
118484fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
118584fe6182SStefan Wahren #endif
118684fe6182SStefan Wahren 
11876b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1188793fc096SFrank Li 		/* enable ENET endian swap */
1189793fc096SFrank Li 		ecntl |= (1 << 8);
1190793fc096SFrank Li 		/* enable ENET store and forward mode */
1191793fc096SFrank Li 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
1192793fc096SFrank Li 	}
1193793fc096SFrank Li 
1194793fc096SFrank Li 	if (fep->bufdesc_ex)
11957b15515fSFrancesco Dolcini 		ecntl |= (1 << 4);
1196793fc096SFrank Li 
1197fc539459SFugang Duan 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1198fc539459SFugang Duan 	    fep->rgmii_txc_dly)
1199fc539459SFugang Duan 		ecntl |= FEC_ENET_TXC_DLY;
1200fc539459SFugang Duan 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1201fc539459SFugang Duan 	    fep->rgmii_rxc_dly)
1202fc539459SFugang Duan 		ecntl |= FEC_ENET_RXC_DLY;
1203fc539459SFugang Duan 
120438ae92dcSChris Healy #ifndef CONFIG_M5272
1205b9eef55cSJim Baxter 	/* Enable the MIB statistic event counters */
1206b9eef55cSJim Baxter 	writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
120738ae92dcSChris Healy #endif
120838ae92dcSChris Healy 
1209793fc096SFrank Li 	/* And last, enable the transmit and receive processing */
1210793fc096SFrank Li 	writel(ecntl, fep->hwp + FEC_ECNTRL);
1211ce99d0d3SFrank Li 	fec_enet_active_rxring(ndev);
1212793fc096SFrank Li 
1213793fc096SFrank Li 	if (fep->bufdesc_ex)
1214793fc096SFrank Li 		fec_ptp_start_cyclecounter(ndev);
1215793fc096SFrank Li 
1216793fc096SFrank Li 	/* Enable interrupts we wish to service */
12170c5a3aefSNimrod Andy 	if (fep->link)
1218793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
12190c5a3aefSNimrod Andy 	else
1220f166f890SAndrew Lunn 		writel(0, fep->hwp + FEC_IMASK);
1221d851b47bSFugang Duan 
1222d851b47bSFugang Duan 	/* Init the interrupt coalescing */
12237e630356SRasmus Villemoes 	if (fep->quirks & FEC_QUIRK_HAS_COALESCE)
1224df727d45SRasmus Villemoes 		fec_enet_itr_coal_set(ndev);
1225793fc096SFrank Li }
1226793fc096SFrank Li 
122740c79ce1SWei Fang static int fec_enet_ipc_handle_init(struct fec_enet_private *fep)
122840c79ce1SWei Fang {
122940c79ce1SWei Fang 	if (!(of_machine_is_compatible("fsl,imx8qm") ||
123040c79ce1SWei Fang 	      of_machine_is_compatible("fsl,imx8qxp") ||
123140c79ce1SWei Fang 	      of_machine_is_compatible("fsl,imx8dxl")))
123240c79ce1SWei Fang 		return 0;
123340c79ce1SWei Fang 
123440c79ce1SWei Fang 	return imx_scu_get_handle(&fep->ipc_handle);
123540c79ce1SWei Fang }
123640c79ce1SWei Fang 
123740c79ce1SWei Fang static void fec_enet_ipg_stop_set(struct fec_enet_private *fep, bool enabled)
123840c79ce1SWei Fang {
123940c79ce1SWei Fang 	struct device_node *np = fep->pdev->dev.of_node;
124040c79ce1SWei Fang 	u32 rsrc_id, val;
124140c79ce1SWei Fang 	int idx;
124240c79ce1SWei Fang 
124340c79ce1SWei Fang 	if (!np || !fep->ipc_handle)
124440c79ce1SWei Fang 		return;
124540c79ce1SWei Fang 
124640c79ce1SWei Fang 	idx = of_alias_get_id(np, "ethernet");
124740c79ce1SWei Fang 	if (idx < 0)
124840c79ce1SWei Fang 		idx = 0;
124940c79ce1SWei Fang 	rsrc_id = idx ? IMX_SC_R_ENET_1 : IMX_SC_R_ENET_0;
125040c79ce1SWei Fang 
125140c79ce1SWei Fang 	val = enabled ? 1 : 0;
125240c79ce1SWei Fang 	imx_sc_misc_set_control(fep->ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val);
125340c79ce1SWei Fang }
125440c79ce1SWei Fang 
1255da722186SMartin Fuzzey static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
1256da722186SMartin Fuzzey {
1257da722186SMartin Fuzzey 	struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1258da722186SMartin Fuzzey 	struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
1259da722186SMartin Fuzzey 
1260da722186SMartin Fuzzey 	if (stop_gpr->gpr) {
1261da722186SMartin Fuzzey 		if (enabled)
1262da722186SMartin Fuzzey 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1263da722186SMartin Fuzzey 					   BIT(stop_gpr->bit),
1264da722186SMartin Fuzzey 					   BIT(stop_gpr->bit));
1265da722186SMartin Fuzzey 		else
1266da722186SMartin Fuzzey 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1267da722186SMartin Fuzzey 					   BIT(stop_gpr->bit), 0);
1268da722186SMartin Fuzzey 	} else if (pdata && pdata->sleep_mode_enable) {
1269da722186SMartin Fuzzey 		pdata->sleep_mode_enable(enabled);
127040c79ce1SWei Fang 	} else {
127140c79ce1SWei Fang 		fec_enet_ipg_stop_set(fep, enabled);
1272da722186SMartin Fuzzey 	}
1273da722186SMartin Fuzzey }
1274da722186SMartin Fuzzey 
12750b6f65c7SJoakim Zhang static void fec_irqs_disable(struct net_device *ndev)
12760b6f65c7SJoakim Zhang {
12770b6f65c7SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
12780b6f65c7SJoakim Zhang 
12790b6f65c7SJoakim Zhang 	writel(0, fep->hwp + FEC_IMASK);
12800b6f65c7SJoakim Zhang }
12810b6f65c7SJoakim Zhang 
12820b6f65c7SJoakim Zhang static void fec_irqs_disable_except_wakeup(struct net_device *ndev)
12830b6f65c7SJoakim Zhang {
12840b6f65c7SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
12850b6f65c7SJoakim Zhang 
12860b6f65c7SJoakim Zhang 	writel(0, fep->hwp + FEC_IMASK);
12870b6f65c7SJoakim Zhang 	writel(FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
12880b6f65c7SJoakim Zhang }
12890b6f65c7SJoakim Zhang 
1290793fc096SFrank Li static void
1291793fc096SFrank Li fec_stop(struct net_device *ndev)
1292793fc096SFrank Li {
1293793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1294793fc096SFrank Li 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1295de40ed31SNimrod Andy 	u32 val;
1296793fc096SFrank Li 
1297793fc096SFrank Li 	/* We cannot expect a graceful transmit stop without link !!! */
1298793fc096SFrank Li 	if (fep->link) {
1299793fc096SFrank Li 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1300793fc096SFrank Li 		udelay(10);
1301793fc096SFrank Li 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
130231b7720cSJoe Perches 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1303793fc096SFrank Li 	}
1304793fc096SFrank Li 
1305106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
1306106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1307106c314cSFugang Duan 	 * instead of reset MAC itself.
1308106c314cSFugang Duan 	 */
1309de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1310471ff445SJoakim Zhang 		if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
1311106c314cSFugang Duan 			writel(0, fep->hwp + FEC_ECNTRL);
1312106c314cSFugang Duan 		} else {
1313793fc096SFrank Li 			writel(1, fep->hwp + FEC_ECNTRL);
1314793fc096SFrank Li 			udelay(10);
1315106c314cSFugang Duan 		}
1316de40ed31SNimrod Andy 	} else {
1317de40ed31SNimrod Andy 		val = readl(fep->hwp + FEC_ECNTRL);
1318de40ed31SNimrod Andy 		val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1319de40ed31SNimrod Andy 		writel(val, fep->hwp + FEC_ECNTRL);
1320de40ed31SNimrod Andy 	}
1321de40ed31SNimrod Andy 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
13220b6f65c7SJoakim Zhang 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1323793fc096SFrank Li 
1324793fc096SFrank Li 	/* We have to keep ENET enabled to have MII interrupt stay working */
1325de40ed31SNimrod Andy 	if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1326de40ed31SNimrod Andy 		!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
13277b15515fSFrancesco Dolcini 		writel(2, fep->hwp + FEC_ECNTRL);
1328793fc096SFrank Li 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1329793fc096SFrank Li 	}
1330793fc096SFrank Li }
1331793fc096SFrank Li 
1332793fc096SFrank Li 
1333793fc096SFrank Li static void
13340290bd29SMichael S. Tsirkin fec_timeout(struct net_device *ndev, unsigned int txqueue)
1335793fc096SFrank Li {
1336793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1337793fc096SFrank Li 
1338344756f6SRussell King 	fec_dump(ndev);
1339344756f6SRussell King 
1340793fc096SFrank Li 	ndev->stats.tx_errors++;
1341793fc096SFrank Li 
134236cdc743SRussell King 	schedule_work(&fep->tx_timeout_work);
134354309fa6SFrank Li }
134454309fa6SFrank Li 
134536cdc743SRussell King static void fec_enet_timeout_work(struct work_struct *work)
134654309fa6SFrank Li {
134754309fa6SFrank Li 	struct fec_enet_private *fep =
134836cdc743SRussell King 		container_of(work, struct fec_enet_private, tx_timeout_work);
13498ce5624fSRussell King 	struct net_device *ndev = fep->netdev;
135054309fa6SFrank Li 
1351da1774e5SRussell King 	rtnl_lock();
13528ce5624fSRussell King 	if (netif_device_present(ndev) || netif_running(ndev)) {
1353dbc64a8eSRussell King 		napi_disable(&fep->napi);
1354dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
1355ef83337dSRussell King 		fec_restart(ndev);
1356657ade07SRickard x Andersson 		netif_tx_wake_all_queues(ndev);
13576af42d42SRussell King 		netif_tx_unlock_bh(ndev);
1358dbc64a8eSRussell King 		napi_enable(&fep->napi);
13598ce5624fSRussell King 	}
1360da1774e5SRussell King 	rtnl_unlock();
136154309fa6SFrank Li }
1362793fc096SFrank Li 
1363793fc096SFrank Li static void
1364bfd4ecddSRussell King fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1365bfd4ecddSRussell King 	struct skb_shared_hwtstamps *hwtstamps)
1366bfd4ecddSRussell King {
1367bfd4ecddSRussell King 	unsigned long flags;
1368bfd4ecddSRussell King 	u64 ns;
1369bfd4ecddSRussell King 
1370bfd4ecddSRussell King 	spin_lock_irqsave(&fep->tmreg_lock, flags);
1371bfd4ecddSRussell King 	ns = timecounter_cyc2time(&fep->tc, ts);
1372bfd4ecddSRussell King 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1373bfd4ecddSRussell King 
1374bfd4ecddSRussell King 	memset(hwtstamps, 0, sizeof(*hwtstamps));
1375bfd4ecddSRussell King 	hwtstamps->hwtstamp = ns_to_ktime(ns);
1376bfd4ecddSRussell King }
1377bfd4ecddSRussell King 
1378bfd4ecddSRussell King static void
137915cec633SWei Fang fec_enet_tx_queue(struct net_device *ndev, u16 queue_id, int budget)
1380793fc096SFrank Li {
1381793fc096SFrank Li 	struct	fec_enet_private *fep;
138220f79739SWei Fang 	struct xdp_frame *xdpf;
1383a2fe37b6SFabio Estevam 	struct bufdesc *bdp;
1384793fc096SFrank Li 	unsigned short status;
1385793fc096SFrank Li 	struct	sk_buff	*skb;
13864d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
13874d494cdcSFugang Duan 	struct netdev_queue *nq;
1388793fc096SFrank Li 	int	index = 0;
138979f33912SNimrod Andy 	int	entries_free;
1390af6f4791SWei Fang 	struct page *page;
1391af6f4791SWei Fang 	int frame_len;
1392793fc096SFrank Li 
1393793fc096SFrank Li 	fep = netdev_priv(ndev);
13944d494cdcSFugang Duan 
13954d494cdcSFugang Duan 	txq = fep->tx_queue[queue_id];
13964d494cdcSFugang Duan 	/* get next bdp of dirty_tx */
13974d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue_id);
13984d494cdcSFugang Duan 	bdp = txq->dirty_tx;
1399793fc096SFrank Li 
1400793fc096SFrank Li 	/* get next bdp of dirty_tx */
14017355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1402793fc096SFrank Li 
14037355f276STroy Kisky 	while (bdp != READ_ONCE(txq->bd.cur)) {
14047355f276STroy Kisky 		/* Order the load of bd.cur and cbd_sc */
1405c4bc44c6SKevin Hao 		rmb();
14065cfa3039SJohannes Berg 		status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1407c4bc44c6SKevin Hao 		if (status & BD_ENET_TX_READY)
1408793fc096SFrank Li 			break;
1409793fc096SFrank Li 
14107355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
14112b995f63SNimrod Andy 
141220f79739SWei Fang 		if (txq->tx_buf[index].type == FEC_TXBUF_T_SKB) {
1413af6f4791SWei Fang 			skb = txq->tx_buf[index].buf_p;
141420f79739SWei Fang 			if (bdp->cbd_bufaddr &&
141520f79739SWei Fang 			    !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
14165cfa3039SJohannes Berg 				dma_unmap_single(&fep->pdev->dev,
14175cfa3039SJohannes Berg 						 fec32_to_cpu(bdp->cbd_bufaddr),
14185cfa3039SJohannes Berg 						 fec16_to_cpu(bdp->cbd_datlen),
14195cfa3039SJohannes Berg 						 DMA_TO_DEVICE);
14205cfa3039SJohannes Berg 			bdp->cbd_bufaddr = cpu_to_fec32(0);
14217fafe803STroy Kisky 			if (!skb)
142220f79739SWei Fang 				goto tx_buf_done;
142320f79739SWei Fang 		} else {
142415cec633SWei Fang 			/* Tx processing cannot call any XDP (or page pool) APIs if
142515cec633SWei Fang 			 * the "budget" is 0. Because NAPI is called with budget of
142615cec633SWei Fang 			 * 0 (such as netpoll) indicates we may be in an IRQ context,
142715cec633SWei Fang 			 * however, we can't use the page pool from IRQ context.
142815cec633SWei Fang 			 */
142915cec633SWei Fang 			if (unlikely(!budget))
143015cec633SWei Fang 				break;
143115cec633SWei Fang 
1432af6f4791SWei Fang 			if (txq->tx_buf[index].type == FEC_TXBUF_T_XDP_NDO) {
1433af6f4791SWei Fang 				xdpf = txq->tx_buf[index].buf_p;
1434af6f4791SWei Fang 				if (bdp->cbd_bufaddr)
143520f79739SWei Fang 					dma_unmap_single(&fep->pdev->dev,
143620f79739SWei Fang 							 fec32_to_cpu(bdp->cbd_bufaddr),
143720f79739SWei Fang 							 fec16_to_cpu(bdp->cbd_datlen),
143820f79739SWei Fang 							 DMA_TO_DEVICE);
1439af6f4791SWei Fang 			} else {
1440af6f4791SWei Fang 				page = txq->tx_buf[index].buf_p;
1441af6f4791SWei Fang 			}
1442af6f4791SWei Fang 
144320f79739SWei Fang 			bdp->cbd_bufaddr = cpu_to_fec32(0);
1444af6f4791SWei Fang 			if (unlikely(!txq->tx_buf[index].buf_p)) {
144520f79739SWei Fang 				txq->tx_buf[index].type = FEC_TXBUF_T_SKB;
144620f79739SWei Fang 				goto tx_buf_done;
144720f79739SWei Fang 			}
1448af6f4791SWei Fang 
1449af6f4791SWei Fang 			frame_len = fec16_to_cpu(bdp->cbd_datlen);
145020f79739SWei Fang 		}
1451793fc096SFrank Li 
1452793fc096SFrank Li 		/* Check for errors. */
1453793fc096SFrank Li 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1454793fc096SFrank Li 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
1455793fc096SFrank Li 				   BD_ENET_TX_CSL)) {
1456793fc096SFrank Li 			ndev->stats.tx_errors++;
1457793fc096SFrank Li 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
1458793fc096SFrank Li 				ndev->stats.tx_heartbeat_errors++;
1459793fc096SFrank Li 			if (status & BD_ENET_TX_LC)  /* Late collision */
1460793fc096SFrank Li 				ndev->stats.tx_window_errors++;
1461793fc096SFrank Li 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
1462793fc096SFrank Li 				ndev->stats.tx_aborted_errors++;
1463793fc096SFrank Li 			if (status & BD_ENET_TX_UN)  /* Underrun */
1464793fc096SFrank Li 				ndev->stats.tx_fifo_errors++;
1465793fc096SFrank Li 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
1466793fc096SFrank Li 				ndev->stats.tx_carrier_errors++;
1467793fc096SFrank Li 		} else {
1468793fc096SFrank Li 			ndev->stats.tx_packets++;
146920f79739SWei Fang 
147020f79739SWei Fang 			if (txq->tx_buf[index].type == FEC_TXBUF_T_SKB)
14716e909283SNimrod Andy 				ndev->stats.tx_bytes += skb->len;
147220f79739SWei Fang 			else
1473af6f4791SWei Fang 				ndev->stats.tx_bytes += frame_len;
1474793fc096SFrank Li 		}
1475793fc096SFrank Li 
1476793fc096SFrank Li 		/* Deferred means some collisions occurred during transmit,
1477793fc096SFrank Li 		 * but we eventually sent the packet OK.
1478793fc096SFrank Li 		 */
1479793fc096SFrank Li 		if (status & BD_ENET_TX_DEF)
1480793fc096SFrank Li 			ndev->stats.collisions++;
1481793fc096SFrank Li 
148220f79739SWei Fang 		if (txq->tx_buf[index].type == FEC_TXBUF_T_SKB) {
148320f79739SWei Fang 			/* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who
148420f79739SWei Fang 			 * are to time stamp the packet, so we still need to check time
148520f79739SWei Fang 			 * stamping enabled flag.
148620f79739SWei Fang 			 */
148720f79739SWei Fang 			if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS &&
148820f79739SWei Fang 				     fep->hwts_tx_en) && fep->bufdesc_ex) {
148920f79739SWei Fang 				struct skb_shared_hwtstamps shhwtstamps;
149020f79739SWei Fang 				struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
149120f79739SWei Fang 
149220f79739SWei Fang 				fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
149320f79739SWei Fang 				skb_tstamp_tx(skb, &shhwtstamps);
149420f79739SWei Fang 			}
149520f79739SWei Fang 
1496793fc096SFrank Li 			/* Free the sk buffer associated with this last transmit */
149791a10efcSWei Fang 			napi_consume_skb(skb, budget);
1498af6f4791SWei Fang 		} else if (txq->tx_buf[index].type == FEC_TXBUF_T_XDP_NDO) {
1499f601899eSWei Fang 			xdp_return_frame_rx_napi(xdpf);
1500f601899eSWei Fang 		} else { /* recycle pages of XDP_TX frames */
1501af6f4791SWei Fang 			/* The dma_sync_size = 0 as XDP_TX has already synced DMA for_device */
1502f601899eSWei Fang 			page_pool_put_page(page->pp, page, 0, true);
1503f601899eSWei Fang 		}
150420f79739SWei Fang 
1505af6f4791SWei Fang 		txq->tx_buf[index].buf_p = NULL;
150620f79739SWei Fang 		/* restore default tx buffer type: FEC_TXBUF_T_SKB */
150720f79739SWei Fang 		txq->tx_buf[index].type = FEC_TXBUF_T_SKB;
150820f79739SWei Fang 
150920f79739SWei Fang tx_buf_done:
151020f79739SWei Fang 		/* Make sure the update to bdp and tx_buf are performed
1511c4bc44c6SKevin Hao 		 * before dirty_tx
1512c4bc44c6SKevin Hao 		 */
1513c4bc44c6SKevin Hao 		wmb();
15144d494cdcSFugang Duan 		txq->dirty_tx = bdp;
1515793fc096SFrank Li 
1516793fc096SFrank Li 		/* Update pointer to next buffer descriptor to be transmitted */
15177355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1518793fc096SFrank Li 
1519793fc096SFrank Li 		/* Since we have freed up a buffer, the ring is no longer full
1520793fc096SFrank Li 		 */
1521657ade07SRickard x Andersson 		if (netif_tx_queue_stopped(nq)) {
15227355f276STroy Kisky 			entries_free = fec_enet_get_free_txdesc_num(txq);
15234d494cdcSFugang Duan 			if (entries_free >= txq->tx_wake_threshold)
15244d494cdcSFugang Duan 				netif_tx_wake_queue(nq);
1525793fc096SFrank Li 		}
152679f33912SNimrod Andy 	}
1527ccea2968SRussell King 
1528c10bc0e7SFugang Duan 	/* ERR006358: Keep the transmitter going */
15297355f276STroy Kisky 	if (bdp != txq->bd.cur &&
153053bb20d1STroy Kisky 	    readl(txq->bd.reg_desc_active) == 0)
153153bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
15324d494cdcSFugang Duan }
15334d494cdcSFugang Duan 
153415cec633SWei Fang static void fec_enet_tx(struct net_device *ndev, int budget)
15354d494cdcSFugang Duan {
15364d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
15377cdaa4ccSTobias Waldekranz 	int i;
15387cdaa4ccSTobias Waldekranz 
15397cdaa4ccSTobias Waldekranz 	/* Make sure that AVB queues are processed first. */
15407cdaa4ccSTobias Waldekranz 	for (i = fep->num_tx_queues - 1; i >= 0; i--)
154115cec633SWei Fang 		fec_enet_tx_queue(ndev, i, budget);
1542793fc096SFrank Li }
1543793fc096SFrank Li 
154495698ff6SShenwei Wang static void fec_enet_update_cbd(struct fec_enet_priv_rx_q *rxq,
154595698ff6SShenwei Wang 				struct bufdesc *bdp, int index)
154695698ff6SShenwei Wang {
154795698ff6SShenwei Wang 	struct page *new_page;
154895698ff6SShenwei Wang 	dma_addr_t phys_addr;
154995698ff6SShenwei Wang 
155095698ff6SShenwei Wang 	new_page = page_pool_dev_alloc_pages(rxq->page_pool);
155195698ff6SShenwei Wang 	WARN_ON(!new_page);
155295698ff6SShenwei Wang 	rxq->rx_skb_info[index].page = new_page;
155395698ff6SShenwei Wang 
155495698ff6SShenwei Wang 	rxq->rx_skb_info[index].offset = FEC_ENET_XDP_HEADROOM;
155595698ff6SShenwei Wang 	phys_addr = page_pool_get_dma_addr(new_page) + FEC_ENET_XDP_HEADROOM;
155695698ff6SShenwei Wang 	bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
155795698ff6SShenwei Wang }
155895698ff6SShenwei Wang 
15596d6b39f1SShenwei Wang static u32
15606d6b39f1SShenwei Wang fec_enet_run_xdp(struct fec_enet_private *fep, struct bpf_prog *prog,
1561f601899eSWei Fang 		 struct xdp_buff *xdp, struct fec_enet_priv_rx_q *rxq, int cpu)
15626d6b39f1SShenwei Wang {
15636d6b39f1SShenwei Wang 	unsigned int sync, len = xdp->data_end - xdp->data;
15646d6b39f1SShenwei Wang 	u32 ret = FEC_ENET_XDP_PASS;
15656d6b39f1SShenwei Wang 	struct page *page;
15666d6b39f1SShenwei Wang 	int err;
15676d6b39f1SShenwei Wang 	u32 act;
15686d6b39f1SShenwei Wang 
15696d6b39f1SShenwei Wang 	act = bpf_prog_run_xdp(prog, xdp);
15706d6b39f1SShenwei Wang 
1571f601899eSWei Fang 	/* Due xdp_adjust_tail and xdp_adjust_head: DMA sync for_device cover
1572f601899eSWei Fang 	 * max len CPU touch
1573f601899eSWei Fang 	 */
1574f601899eSWei Fang 	sync = xdp->data_end - xdp->data;
15756d6b39f1SShenwei Wang 	sync = max(sync, len);
15766d6b39f1SShenwei Wang 
15776d6b39f1SShenwei Wang 	switch (act) {
15786d6b39f1SShenwei Wang 	case XDP_PASS:
15796970ef27SShenwei Wang 		rxq->stats[RX_XDP_PASS]++;
15806d6b39f1SShenwei Wang 		ret = FEC_ENET_XDP_PASS;
15816d6b39f1SShenwei Wang 		break;
15826d6b39f1SShenwei Wang 
15836d6b39f1SShenwei Wang 	case XDP_REDIRECT:
15846970ef27SShenwei Wang 		rxq->stats[RX_XDP_REDIRECT]++;
15856d6b39f1SShenwei Wang 		err = xdp_do_redirect(fep->netdev, xdp, prog);
1586e83fabb7SWei Fang 		if (unlikely(err))
1587e83fabb7SWei Fang 			goto xdp_err;
1588e83fabb7SWei Fang 
15896d6b39f1SShenwei Wang 		ret = FEC_ENET_XDP_REDIR;
15906d6b39f1SShenwei Wang 		break;
15916d6b39f1SShenwei Wang 
15926d6b39f1SShenwei Wang 	case XDP_TX:
1593*95403294SWei Fang 		rxq->stats[RX_XDP_TX]++;
1594f601899eSWei Fang 		err = fec_enet_xdp_tx_xmit(fep, cpu, xdp, sync);
1595*95403294SWei Fang 		if (unlikely(err)) {
1596*95403294SWei Fang 			rxq->stats[RX_XDP_TX_ERRORS]++;
1597e83fabb7SWei Fang 			goto xdp_err;
1598*95403294SWei Fang 		}
1599e83fabb7SWei Fang 
1600f601899eSWei Fang 		ret = FEC_ENET_XDP_TX;
1601f601899eSWei Fang 		break;
1602f601899eSWei Fang 
1603f601899eSWei Fang 	default:
16046d6b39f1SShenwei Wang 		bpf_warn_invalid_xdp_action(fep->netdev, prog, act);
16056d6b39f1SShenwei Wang 		fallthrough;
16066d6b39f1SShenwei Wang 
16076d6b39f1SShenwei Wang 	case XDP_ABORTED:
16086d6b39f1SShenwei Wang 		fallthrough;    /* handle aborts by dropping packet */
16096d6b39f1SShenwei Wang 
16106d6b39f1SShenwei Wang 	case XDP_DROP:
16116970ef27SShenwei Wang 		rxq->stats[RX_XDP_DROP]++;
1612e83fabb7SWei Fang xdp_err:
16136d6b39f1SShenwei Wang 		ret = FEC_ENET_XDP_CONSUMED;
16146d6b39f1SShenwei Wang 		page = virt_to_head_page(xdp->data);
16156d6b39f1SShenwei Wang 		page_pool_put_page(rxq->page_pool, page, sync, true);
1616e83fabb7SWei Fang 		if (act != XDP_DROP)
1617e83fabb7SWei Fang 			trace_xdp_exception(fep->netdev, prog, act);
16186d6b39f1SShenwei Wang 		break;
16196d6b39f1SShenwei Wang 	}
16206d6b39f1SShenwei Wang 
16216d6b39f1SShenwei Wang 	return ret;
16226d6b39f1SShenwei Wang }
16236d6b39f1SShenwei Wang 
16247355f276STroy Kisky /* During a receive, the bd_rx.cur points to the current incoming buffer.
1625793fc096SFrank Li  * When we update through the ring, if the next incoming buffer has
1626793fc096SFrank Li  * not been given to the system, we just set the empty indicator,
1627793fc096SFrank Li  * effectively tossing the packet.
1628793fc096SFrank Li  */
1629793fc096SFrank Li static int
16304d494cdcSFugang Duan fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1631793fc096SFrank Li {
1632793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
16334d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
1634793fc096SFrank Li 	struct bufdesc *bdp;
1635793fc096SFrank Li 	unsigned short status;
1636793fc096SFrank Li 	struct  sk_buff *skb;
1637793fc096SFrank Li 	ushort	pkt_len;
1638793fc096SFrank Li 	__u8 *data;
1639793fc096SFrank Li 	int	pkt_received = 0;
1640cdffcf1bSJim Baxter 	struct	bufdesc_ex *ebdp = NULL;
1641cdffcf1bSJim Baxter 	bool	vlan_packet_rcvd = false;
1642cdffcf1bSJim Baxter 	u16	vlan_tag;
1643d842a31fSDuan Fugang-B38611 	int	index = 0;
16446b7e4008SLothar Waßmann 	bool	need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
16456d6b39f1SShenwei Wang 	struct bpf_prog *xdp_prog = READ_ONCE(fep->xdp_prog);
16466d6b39f1SShenwei Wang 	u32 ret, xdp_result = FEC_ENET_XDP_PASS;
16476c8fae0cSShenwei Wang 	u32 data_start = FEC_ENET_XDP_HEADROOM;
1648f601899eSWei Fang 	int cpu = smp_processor_id();
16496d6b39f1SShenwei Wang 	struct xdp_buff xdp;
165095698ff6SShenwei Wang 	struct page *page;
16516c8fae0cSShenwei Wang 	u32 sub_len = 4;
16526c8fae0cSShenwei Wang 
16536c8fae0cSShenwei Wang #if !defined(CONFIG_M5272)
16546c8fae0cSShenwei Wang 	/*If it has the FEC_QUIRK_HAS_RACC quirk property, the bit of
16556c8fae0cSShenwei Wang 	 * FEC_RACC_SHIFT16 is set by default in the probe function.
16566c8fae0cSShenwei Wang 	 */
16576c8fae0cSShenwei Wang 	if (fep->quirks & FEC_QUIRK_HAS_RACC) {
16586c8fae0cSShenwei Wang 		data_start += 2;
16596c8fae0cSShenwei Wang 		sub_len += 2;
16606c8fae0cSShenwei Wang 	}
16616c8fae0cSShenwei Wang #endif
1662793fc096SFrank Li 
1663793fc096SFrank Li #ifdef CONFIG_M532x
1664793fc096SFrank Li 	flush_cache_all();
1665793fc096SFrank Li #endif
16664d494cdcSFugang Duan 	rxq = fep->rx_queue[queue_id];
1667793fc096SFrank Li 
1668793fc096SFrank Li 	/* First, grab all of the stats for the incoming packet.
1669793fc096SFrank Li 	 * These get messed up if we get called due to a busy condition.
1670793fc096SFrank Li 	 */
16717355f276STroy Kisky 	bdp = rxq->bd.cur;
16726d6b39f1SShenwei Wang 	xdp_init_buff(&xdp, PAGE_SIZE, &rxq->xdp_rxq);
1673793fc096SFrank Li 
16745cfa3039SJohannes Berg 	while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1675793fc096SFrank Li 
1676793fc096SFrank Li 		if (pkt_received >= budget)
1677793fc096SFrank Li 			break;
1678793fc096SFrank Li 		pkt_received++;
1679793fc096SFrank Li 
1680b5bd95d1SJoakim Zhang 		writel(FEC_ENET_RXF_GET(queue_id), fep->hwp + FEC_IEVENT);
1681db3421c1SRussell King 
1682793fc096SFrank Li 		/* Check for errors. */
1683095098e1STroy Kisky 		status ^= BD_ENET_RX_LAST;
1684793fc096SFrank Li 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1685095098e1STroy Kisky 			   BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1686095098e1STroy Kisky 			   BD_ENET_RX_CL)) {
1687793fc096SFrank Li 			ndev->stats.rx_errors++;
1688095098e1STroy Kisky 			if (status & BD_ENET_RX_OV) {
1689095098e1STroy Kisky 				/* FIFO overrun */
1690095098e1STroy Kisky 				ndev->stats.rx_fifo_errors++;
1691095098e1STroy Kisky 				goto rx_processing_done;
1692095098e1STroy Kisky 			}
1693095098e1STroy Kisky 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1694095098e1STroy Kisky 						| BD_ENET_RX_LAST)) {
1695793fc096SFrank Li 				/* Frame too long or too short. */
1696793fc096SFrank Li 				ndev->stats.rx_length_errors++;
1697095098e1STroy Kisky 				if (status & BD_ENET_RX_LAST)
1698095098e1STroy Kisky 					netdev_err(ndev, "rcv is not +last\n");
1699793fc096SFrank Li 			}
1700793fc096SFrank Li 			if (status & BD_ENET_RX_CR)	/* CRC Error */
1701793fc096SFrank Li 				ndev->stats.rx_crc_errors++;
1702095098e1STroy Kisky 			/* Report late collisions as a frame error. */
1703095098e1STroy Kisky 			if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1704793fc096SFrank Li 				ndev->stats.rx_frame_errors++;
1705793fc096SFrank Li 			goto rx_processing_done;
1706793fc096SFrank Li 		}
1707793fc096SFrank Li 
1708793fc096SFrank Li 		/* Process the incoming frame. */
1709793fc096SFrank Li 		ndev->stats.rx_packets++;
17105cfa3039SJohannes Berg 		pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1711793fc096SFrank Li 		ndev->stats.rx_bytes += pkt_len;
1712793fc096SFrank Li 
17137355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &rxq->bd);
171495698ff6SShenwei Wang 		page = rxq->rx_skb_info[index].page;
171595698ff6SShenwei Wang 		dma_sync_single_for_cpu(&fep->pdev->dev,
171695698ff6SShenwei Wang 					fec32_to_cpu(bdp->cbd_bufaddr),
171795698ff6SShenwei Wang 					pkt_len,
171895698ff6SShenwei Wang 					DMA_FROM_DEVICE);
171995698ff6SShenwei Wang 		prefetch(page_address(page));
172095698ff6SShenwei Wang 		fec_enet_update_cbd(rxq, bdp, index);
17211b7bde6dSNimrod Andy 
17226d6b39f1SShenwei Wang 		if (xdp_prog) {
17236d6b39f1SShenwei Wang 			xdp_buff_clear_frags_flag(&xdp);
17246c8fae0cSShenwei Wang 			/* subtract 16bit shift and FCS */
17256d6b39f1SShenwei Wang 			xdp_prepare_buff(&xdp, page_address(page),
17266c8fae0cSShenwei Wang 					 data_start, pkt_len - sub_len, false);
1727f601899eSWei Fang 			ret = fec_enet_run_xdp(fep, xdp_prog, &xdp, rxq, cpu);
17286d6b39f1SShenwei Wang 			xdp_result |= ret;
17296d6b39f1SShenwei Wang 			if (ret != FEC_ENET_XDP_PASS)
17306d6b39f1SShenwei Wang 				goto rx_processing_done;
17316d6b39f1SShenwei Wang 		}
17326d6b39f1SShenwei Wang 
17331b7bde6dSNimrod Andy 		/* The packet length includes FCS, but we don't want to
17341b7bde6dSNimrod Andy 		 * include that when passing upstream as it messes up
17351b7bde6dSNimrod Andy 		 * bridging applications.
17361b7bde6dSNimrod Andy 		 */
173795698ff6SShenwei Wang 		skb = build_skb(page_address(page), PAGE_SIZE);
173819e72b06SWei Fang 		if (unlikely(!skb)) {
173919e72b06SWei Fang 			page_pool_recycle_direct(rxq->page_pool, page);
174019e72b06SWei Fang 			ndev->stats.rx_dropped++;
174119e72b06SWei Fang 
174219e72b06SWei Fang 			netdev_err_once(ndev, "build_skb failed!\n");
174319e72b06SWei Fang 			goto rx_processing_done;
174419e72b06SWei Fang 		}
174519e72b06SWei Fang 
17466c8fae0cSShenwei Wang 		skb_reserve(skb, data_start);
17476c8fae0cSShenwei Wang 		skb_put(skb, pkt_len - sub_len);
174895698ff6SShenwei Wang 		skb_mark_for_recycle(skb);
17493ac72b7bSEric Nelson 
17506c8fae0cSShenwei Wang 		if (unlikely(need_swap)) {
17516c8fae0cSShenwei Wang 			data = page_address(page) + FEC_ENET_XDP_HEADROOM;
1752235bde1eSFabio Estevam 			swap_buffer(data, pkt_len);
17536c8fae0cSShenwei Wang 		}
17546c8fae0cSShenwei Wang 		data = skb->data;
17553ac72b7bSEric Nelson 
1756cdffcf1bSJim Baxter 		/* Extract the enhanced buffer descriptor */
1757cdffcf1bSJim Baxter 		ebdp = NULL;
1758cdffcf1bSJim Baxter 		if (fep->bufdesc_ex)
1759cdffcf1bSJim Baxter 			ebdp = (struct bufdesc_ex *)bdp;
1760cdffcf1bSJim Baxter 
1761cdffcf1bSJim Baxter 		/* If this is a VLAN packet remove the VLAN Tag */
1762cdffcf1bSJim Baxter 		vlan_packet_rcvd = false;
1763cdffcf1bSJim Baxter 		if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
17645cfa3039SJohannes Berg 		    fep->bufdesc_ex &&
17655cfa3039SJohannes Berg 		    (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1766cdffcf1bSJim Baxter 			/* Push and remove the vlan tag */
1767cdffcf1bSJim Baxter 			struct vlan_hdr *vlan_header =
1768cdffcf1bSJim Baxter 					(struct vlan_hdr *) (data + ETH_HLEN);
1769cdffcf1bSJim Baxter 			vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1770cdffcf1bSJim Baxter 
1771cdffcf1bSJim Baxter 			vlan_packet_rcvd = true;
17721b7bde6dSNimrod Andy 
1773af5cbc98SNimrod Andy 			memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
17741b7bde6dSNimrod Andy 			skb_pull(skb, VLAN_HLEN);
1775cdffcf1bSJim Baxter 		}
1776cdffcf1bSJim Baxter 
1777793fc096SFrank Li 		skb->protocol = eth_type_trans(skb, ndev);
1778793fc096SFrank Li 
1779793fc096SFrank Li 		/* Get receive timestamp from the skb */
1780bfd4ecddSRussell King 		if (fep->hwts_rx_en && fep->bufdesc_ex)
17815cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1782bfd4ecddSRussell King 					  skb_hwtstamps(skb));
1783793fc096SFrank Li 
17844c09eed9SJim Baxter 		if (fep->bufdesc_ex &&
17854c09eed9SJim Baxter 		    (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
17865cfa3039SJohannes Berg 			if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
17874c09eed9SJim Baxter 				/* don't check it */
17884c09eed9SJim Baxter 				skb->ip_summed = CHECKSUM_UNNECESSARY;
17894c09eed9SJim Baxter 			} else {
17904c09eed9SJim Baxter 				skb_checksum_none_assert(skb);
17914c09eed9SJim Baxter 			}
17924c09eed9SJim Baxter 		}
17934c09eed9SJim Baxter 
1794cdffcf1bSJim Baxter 		/* Handle received VLAN packets */
1795cdffcf1bSJim Baxter 		if (vlan_packet_rcvd)
1796cdffcf1bSJim Baxter 			__vlan_hwaccel_put_tag(skb,
1797cdffcf1bSJim Baxter 					       htons(ETH_P_8021Q),
1798cdffcf1bSJim Baxter 					       vlan_tag);
1799cdffcf1bSJim Baxter 
18007cdaa4ccSTobias Waldekranz 		skb_record_rx_queue(skb, queue_id);
1801793fc096SFrank Li 		napi_gro_receive(&fep->napi, skb);
1802793fc096SFrank Li 
1803793fc096SFrank Li rx_processing_done:
1804793fc096SFrank Li 		/* Clear the status flags for this buffer */
1805793fc096SFrank Li 		status &= ~BD_ENET_RX_STATS;
1806793fc096SFrank Li 
1807793fc096SFrank Li 		/* Mark the buffer empty */
1808793fc096SFrank Li 		status |= BD_ENET_RX_EMPTY;
1809793fc096SFrank Li 
1810793fc096SFrank Li 		if (fep->bufdesc_ex) {
1811793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1812793fc096SFrank Li 
18135cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1814793fc096SFrank Li 			ebdp->cbd_prot = 0;
1815793fc096SFrank Li 			ebdp->cbd_bdu = 0;
1816793fc096SFrank Li 		}
1817be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
1818be293467STroy Kisky 		 * performed before transferring ownership.
1819be293467STroy Kisky 		 */
1820be293467STroy Kisky 		wmb();
1821be293467STroy Kisky 		bdp->cbd_sc = cpu_to_fec16(status);
1822793fc096SFrank Li 
1823793fc096SFrank Li 		/* Update BD pointer to next entry */
18247355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
182536e24e2eSDuan Fugang-B38611 
1826793fc096SFrank Li 		/* Doing this here will keep the FEC running while we process
1827793fc096SFrank Li 		 * incoming frames.  On a heavily loaded network, we should be
1828793fc096SFrank Li 		 * able to keep up at the expense of system resources.
1829793fc096SFrank Li 		 */
183053bb20d1STroy Kisky 		writel(0, rxq->bd.reg_desc_active);
1831793fc096SFrank Li 	}
18327355f276STroy Kisky 	rxq->bd.cur = bdp;
18336d6b39f1SShenwei Wang 
18346d6b39f1SShenwei Wang 	if (xdp_result & FEC_ENET_XDP_REDIR)
18356d6b39f1SShenwei Wang 		xdp_do_flush_map();
18366d6b39f1SShenwei Wang 
1837793fc096SFrank Li 	return pkt_received;
1838793fc096SFrank Li }
1839793fc096SFrank Li 
18407cdaa4ccSTobias Waldekranz static int fec_enet_rx(struct net_device *ndev, int budget)
18414d494cdcSFugang Duan {
18424d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
18437cdaa4ccSTobias Waldekranz 	int i, done = 0;
18444d494cdcSFugang Duan 
18457cdaa4ccSTobias Waldekranz 	/* Make sure that AVB queues are processed first. */
18467cdaa4ccSTobias Waldekranz 	for (i = fep->num_rx_queues - 1; i >= 0; i--)
18477cdaa4ccSTobias Waldekranz 		done += fec_enet_rx_queue(ndev, budget - done, i);
18481c021bb7SUwe Kleine-König 
18497cdaa4ccSTobias Waldekranz 	return done;
18504d494cdcSFugang Duan }
18514d494cdcSFugang Duan 
18527cdaa4ccSTobias Waldekranz static bool fec_enet_collect_events(struct fec_enet_private *fep)
18534d494cdcSFugang Duan {
1854793fc096SFrank Li 	uint int_events;
1855793fc096SFrank Li 
1856793fc096SFrank Li 	int_events = readl(fep->hwp + FEC_IEVENT);
1857f166f890SAndrew Lunn 
1858f166f890SAndrew Lunn 	/* Don't clear MDIO events, we poll for those */
1859f166f890SAndrew Lunn 	int_events &= ~FEC_ENET_MII;
1860f166f890SAndrew Lunn 
186194191fd6SNimrod Andy 	writel(int_events, fep->hwp + FEC_IEVENT);
1862793fc096SFrank Li 
18637cdaa4ccSTobias Waldekranz 	return int_events != 0;
18647cdaa4ccSTobias Waldekranz }
18657cdaa4ccSTobias Waldekranz 
18667cdaa4ccSTobias Waldekranz static irqreturn_t
18677cdaa4ccSTobias Waldekranz fec_enet_interrupt(int irq, void *dev_id)
18687cdaa4ccSTobias Waldekranz {
18697cdaa4ccSTobias Waldekranz 	struct net_device *ndev = dev_id;
18707cdaa4ccSTobias Waldekranz 	struct fec_enet_private *fep = netdev_priv(ndev);
18717cdaa4ccSTobias Waldekranz 	irqreturn_t ret = IRQ_NONE;
18727cdaa4ccSTobias Waldekranz 
18737cdaa4ccSTobias Waldekranz 	if (fec_enet_collect_events(fep) && fep->link) {
1874793fc096SFrank Li 		ret = IRQ_HANDLED;
1875793fc096SFrank Li 
187694191fd6SNimrod Andy 		if (napi_schedule_prep(&fep->napi)) {
1877f166f890SAndrew Lunn 			/* Disable interrupts */
1878f166f890SAndrew Lunn 			writel(0, fep->hwp + FEC_IMASK);
187994191fd6SNimrod Andy 			__napi_schedule(&fep->napi);
188094191fd6SNimrod Andy 		}
1881793fc096SFrank Li 	}
1882793fc096SFrank Li 
1883793fc096SFrank Li 	return ret;
1884793fc096SFrank Li }
1885793fc096SFrank Li 
1886793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1887793fc096SFrank Li {
1888793fc096SFrank Li 	struct net_device *ndev = napi->dev;
1889793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
18907cdaa4ccSTobias Waldekranz 	int done = 0;
18917a16807cSRussell King 
18927cdaa4ccSTobias Waldekranz 	do {
18937cdaa4ccSTobias Waldekranz 		done += fec_enet_rx(ndev, budget - done);
189415cec633SWei Fang 		fec_enet_tx(ndev, budget);
18957cdaa4ccSTobias Waldekranz 	} while ((done < budget) && fec_enet_collect_events(fep));
1896793fc096SFrank Li 
18977cdaa4ccSTobias Waldekranz 	if (done < budget) {
18987cdaa4ccSTobias Waldekranz 		napi_complete_done(napi, done);
1899793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1900793fc096SFrank Li 	}
19017cdaa4ccSTobias Waldekranz 
19027cdaa4ccSTobias Waldekranz 	return done;
1903793fc096SFrank Li }
1904793fc096SFrank Li 
1905793fc096SFrank Li /* ------------------------------------------------------------------------- */
1906052fcc45SFugang Duan static int fec_get_mac(struct net_device *ndev)
1907793fc096SFrank Li {
1908793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1909793fc096SFrank Li 	unsigned char *iap, tmpaddr[ETH_ALEN];
191083216e39SMichael Walle 	int ret;
1911793fc096SFrank Li 
1912793fc096SFrank Li 	/*
1913793fc096SFrank Li 	 * try to get mac address in following order:
1914793fc096SFrank Li 	 *
1915793fc096SFrank Li 	 * 1) module parameter via kernel command line in form
1916793fc096SFrank Li 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1917793fc096SFrank Li 	 */
1918793fc096SFrank Li 	iap = macaddr;
1919793fc096SFrank Li 
1920793fc096SFrank Li 	/*
1921793fc096SFrank Li 	 * 2) from device tree data
1922793fc096SFrank Li 	 */
1923793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1924793fc096SFrank Li 		struct device_node *np = fep->pdev->dev.of_node;
1925793fc096SFrank Li 		if (np) {
192683216e39SMichael Walle 			ret = of_get_mac_address(np, tmpaddr);
192783216e39SMichael Walle 			if (!ret)
192883216e39SMichael Walle 				iap = tmpaddr;
1929052fcc45SFugang Duan 			else if (ret == -EPROBE_DEFER)
1930052fcc45SFugang Duan 				return ret;
1931793fc096SFrank Li 		}
1932793fc096SFrank Li 	}
1933793fc096SFrank Li 
1934793fc096SFrank Li 	/*
1935793fc096SFrank Li 	 * 3) from flash or fuse (via platform data)
1936793fc096SFrank Li 	 */
1937793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1938793fc096SFrank Li #ifdef CONFIG_M5272
1939793fc096SFrank Li 		if (FEC_FLASHMAC)
1940793fc096SFrank Li 			iap = (unsigned char *)FEC_FLASHMAC;
1941793fc096SFrank Li #else
194232d1bbb1SGeert Uytterhoeven 		struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
194332d1bbb1SGeert Uytterhoeven 
1944793fc096SFrank Li 		if (pdata)
1945793fc096SFrank Li 			iap = (unsigned char *)&pdata->mac;
1946793fc096SFrank Li #endif
1947793fc096SFrank Li 	}
1948793fc096SFrank Li 
1949793fc096SFrank Li 	/*
1950793fc096SFrank Li 	 * 4) FEC mac registers set by bootloader
1951793fc096SFrank Li 	 */
1952793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
19537d7628f3SDan Carpenter 		*((__be32 *) &tmpaddr[0]) =
19547d7628f3SDan Carpenter 			cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
19557d7628f3SDan Carpenter 		*((__be16 *) &tmpaddr[4]) =
19567d7628f3SDan Carpenter 			cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1957793fc096SFrank Li 		iap = &tmpaddr[0];
1958793fc096SFrank Li 	}
1959793fc096SFrank Li 
1960ff5b2fabSLucas Stach 	/*
1961ff5b2fabSLucas Stach 	 * 5) random mac address
1962ff5b2fabSLucas Stach 	 */
1963ff5b2fabSLucas Stach 	if (!is_valid_ether_addr(iap)) {
1964ff5b2fabSLucas Stach 		/* Report it and use a random ethernet address instead */
1965a19a0582SFabio Estevam 		dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
1966ff5b2fabSLucas Stach 		eth_hw_addr_random(ndev);
1967a19a0582SFabio Estevam 		dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
1968ff5b2fabSLucas Stach 			 ndev->dev_addr);
1969052fcc45SFugang Duan 		return 0;
1970ff5b2fabSLucas Stach 	}
1971ff5b2fabSLucas Stach 
1972793fc096SFrank Li 	/* Adjust MAC if using macaddr */
1973ba3fdfe3SJakub Kicinski 	eth_hw_addr_gen(ndev, iap, iap == macaddr ? fep->dev_id : 0);
1974052fcc45SFugang Duan 
1975052fcc45SFugang Duan 	return 0;
1976793fc096SFrank Li }
1977793fc096SFrank Li 
1978793fc096SFrank Li /* ------------------------------------------------------------------------- */
1979793fc096SFrank Li 
1980793fc096SFrank Li /*
1981793fc096SFrank Li  * Phy section
1982793fc096SFrank Li  */
1983793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev)
1984793fc096SFrank Li {
1985793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
198645f5c327SPhilippe Reynes 	struct phy_device *phy_dev = ndev->phydev;
1987793fc096SFrank Li 	int status_change = 0;
1988793fc096SFrank Li 
19898ce5624fSRussell King 	/*
19908ce5624fSRussell King 	 * If the netdev is down, or is going down, we're not interested
19918ce5624fSRussell King 	 * in link state events, so just mark our idea of the link as down
19928ce5624fSRussell King 	 * and ignore the event.
19938ce5624fSRussell King 	 */
19948ce5624fSRussell King 	if (!netif_running(ndev) || !netif_device_present(ndev)) {
19958ce5624fSRussell King 		fep->link = 0;
19968ce5624fSRussell King 	} else if (phy_dev->link) {
1997793fc096SFrank Li 		if (!fep->link) {
1998793fc096SFrank Li 			fep->link = phy_dev->link;
1999793fc096SFrank Li 			status_change = 1;
2000793fc096SFrank Li 		}
2001793fc096SFrank Li 
2002ef83337dSRussell King 		if (fep->full_duplex != phy_dev->duplex) {
2003ef83337dSRussell King 			fep->full_duplex = phy_dev->duplex;
2004793fc096SFrank Li 			status_change = 1;
2005ef83337dSRussell King 		}
2006793fc096SFrank Li 
2007793fc096SFrank Li 		if (phy_dev->speed != fep->speed) {
2008793fc096SFrank Li 			fep->speed = phy_dev->speed;
2009793fc096SFrank Li 			status_change = 1;
2010793fc096SFrank Li 		}
2011793fc096SFrank Li 
2012793fc096SFrank Li 		/* if any of the above changed restart the FEC */
2013dbc64a8eSRussell King 		if (status_change) {
2014dbc64a8eSRussell King 			napi_disable(&fep->napi);
2015dbc64a8eSRussell King 			netif_tx_lock_bh(ndev);
2016ef83337dSRussell King 			fec_restart(ndev);
2017657ade07SRickard x Andersson 			netif_tx_wake_all_queues(ndev);
20186af42d42SRussell King 			netif_tx_unlock_bh(ndev);
2019dbc64a8eSRussell King 			napi_enable(&fep->napi);
2020dbc64a8eSRussell King 		}
2021793fc096SFrank Li 	} else {
2022793fc096SFrank Li 		if (fep->link) {
2023f208ce10SRussell King 			napi_disable(&fep->napi);
2024f208ce10SRussell King 			netif_tx_lock_bh(ndev);
2025793fc096SFrank Li 			fec_stop(ndev);
2026f208ce10SRussell King 			netif_tx_unlock_bh(ndev);
2027f208ce10SRussell King 			napi_enable(&fep->napi);
20286e0895c2SDavid S. Miller 			fep->link = phy_dev->link;
2029793fc096SFrank Li 			status_change = 1;
2030793fc096SFrank Li 		}
2031793fc096SFrank Li 	}
2032793fc096SFrank Li 
2033793fc096SFrank Li 	if (status_change)
2034793fc096SFrank Li 		phy_print_status(phy_dev);
2035793fc096SFrank Li }
2036793fc096SFrank Li 
2037f166f890SAndrew Lunn static int fec_enet_mdio_wait(struct fec_enet_private *fep)
2038f166f890SAndrew Lunn {
2039f166f890SAndrew Lunn 	uint ievent;
2040f166f890SAndrew Lunn 	int ret;
2041f166f890SAndrew Lunn 
2042f166f890SAndrew Lunn 	ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent,
2043f166f890SAndrew Lunn 					ievent & FEC_ENET_MII, 2, 30000);
2044f166f890SAndrew Lunn 
2045f166f890SAndrew Lunn 	if (!ret)
2046f166f890SAndrew Lunn 		writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
2047f166f890SAndrew Lunn 
2048f166f890SAndrew Lunn 	return ret;
2049f166f890SAndrew Lunn }
2050f166f890SAndrew Lunn 
20518d03ad1aSAndrew Lunn static int fec_enet_mdio_read_c22(struct mii_bus *bus, int mii_id, int regnum)
2052793fc096SFrank Li {
2053793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
20548fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
2055d3ee8ec7SMarco Hartmann 	int ret = 0, frame_start, frame_addr, frame_op;
20568fff755eSAndrew Lunn 
2057da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
2058b0c6ce24SFabio Estevam 	if (ret < 0)
20598fff755eSAndrew Lunn 		return ret;
2060793fc096SFrank Li 
2061d3ee8ec7SMarco Hartmann 	/* C22 read */
2062d3ee8ec7SMarco Hartmann 	frame_op = FEC_MMFR_OP_READ;
2063d3ee8ec7SMarco Hartmann 	frame_start = FEC_MMFR_ST;
2064d3ee8ec7SMarco Hartmann 	frame_addr = regnum;
2065d3ee8ec7SMarco Hartmann 
2066793fc096SFrank Li 	/* start a read op */
2067d3ee8ec7SMarco Hartmann 	writel(frame_start | frame_op |
2068d3ee8ec7SMarco Hartmann 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
2069793fc096SFrank Li 	       FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
2070793fc096SFrank Li 
2071793fc096SFrank Li 	/* wait for end of transfer */
2072f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
2073f166f890SAndrew Lunn 	if (ret) {
207431b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO read timeout\n");
20758fff755eSAndrew Lunn 		goto out;
2076793fc096SFrank Li 	}
2077793fc096SFrank Li 
20788fff755eSAndrew Lunn 	ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
20798fff755eSAndrew Lunn 
20808fff755eSAndrew Lunn out:
20818fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
20828fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
20838fff755eSAndrew Lunn 
20848fff755eSAndrew Lunn 	return ret;
2085793fc096SFrank Li }
2086793fc096SFrank Li 
20878d03ad1aSAndrew Lunn static int fec_enet_mdio_read_c45(struct mii_bus *bus, int mii_id,
20888d03ad1aSAndrew Lunn 				  int devad, int regnum)
2089793fc096SFrank Li {
2090793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
20918fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
20928d03ad1aSAndrew Lunn 	int ret = 0, frame_start, frame_op;
20938fff755eSAndrew Lunn 
2094da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
2095b0c6ce24SFabio Estevam 	if (ret < 0)
20968fff755eSAndrew Lunn 		return ret;
2097793fc096SFrank Li 
2098d3ee8ec7SMarco Hartmann 	frame_start = FEC_MMFR_ST_C45;
2099d3ee8ec7SMarco Hartmann 
2100d3ee8ec7SMarco Hartmann 	/* write address */
2101d3ee8ec7SMarco Hartmann 	writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
21028d03ad1aSAndrew Lunn 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
2103d3ee8ec7SMarco Hartmann 	       FEC_MMFR_TA | (regnum & 0xFFFF),
2104d3ee8ec7SMarco Hartmann 	       fep->hwp + FEC_MII_DATA);
2105d3ee8ec7SMarco Hartmann 
2106d3ee8ec7SMarco Hartmann 	/* wait for end of transfer */
2107f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
2108f166f890SAndrew Lunn 	if (ret) {
2109d3ee8ec7SMarco Hartmann 		netdev_err(fep->netdev, "MDIO address write timeout\n");
2110d3ee8ec7SMarco Hartmann 		goto out;
2111d3ee8ec7SMarco Hartmann 	}
21128d03ad1aSAndrew Lunn 
21138d03ad1aSAndrew Lunn 	frame_op = FEC_MMFR_OP_READ_C45;
21148d03ad1aSAndrew Lunn 
21158d03ad1aSAndrew Lunn 	/* start a read op */
21168d03ad1aSAndrew Lunn 	writel(frame_start | frame_op |
21178d03ad1aSAndrew Lunn 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
21188d03ad1aSAndrew Lunn 	       FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
21198d03ad1aSAndrew Lunn 
21208d03ad1aSAndrew Lunn 	/* wait for end of transfer */
21218d03ad1aSAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
21228d03ad1aSAndrew Lunn 	if (ret) {
21238d03ad1aSAndrew Lunn 		netdev_err(fep->netdev, "MDIO read timeout\n");
21248d03ad1aSAndrew Lunn 		goto out;
21258d03ad1aSAndrew Lunn 	}
21268d03ad1aSAndrew Lunn 
21278d03ad1aSAndrew Lunn 	ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
21288d03ad1aSAndrew Lunn 
21298d03ad1aSAndrew Lunn out:
21308d03ad1aSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
21318d03ad1aSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
21328d03ad1aSAndrew Lunn 
21338d03ad1aSAndrew Lunn 	return ret;
21348d03ad1aSAndrew Lunn }
21358d03ad1aSAndrew Lunn 
21368d03ad1aSAndrew Lunn static int fec_enet_mdio_write_c22(struct mii_bus *bus, int mii_id, int regnum,
21378d03ad1aSAndrew Lunn 				   u16 value)
21388d03ad1aSAndrew Lunn {
21398d03ad1aSAndrew Lunn 	struct fec_enet_private *fep = bus->priv;
21408d03ad1aSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
21418d03ad1aSAndrew Lunn 	int ret, frame_start, frame_addr;
21428d03ad1aSAndrew Lunn 
21438d03ad1aSAndrew Lunn 	ret = pm_runtime_resume_and_get(dev);
21448d03ad1aSAndrew Lunn 	if (ret < 0)
21458d03ad1aSAndrew Lunn 		return ret;
21468d03ad1aSAndrew Lunn 
2147d3ee8ec7SMarco Hartmann 	/* C22 write */
2148d3ee8ec7SMarco Hartmann 	frame_start = FEC_MMFR_ST;
2149d3ee8ec7SMarco Hartmann 	frame_addr = regnum;
2150d3ee8ec7SMarco Hartmann 
2151793fc096SFrank Li 	/* start a write op */
2152d3ee8ec7SMarco Hartmann 	writel(frame_start | FEC_MMFR_OP_WRITE |
2153d3ee8ec7SMarco Hartmann 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
2154793fc096SFrank Li 	       FEC_MMFR_TA | FEC_MMFR_DATA(value),
2155793fc096SFrank Li 	       fep->hwp + FEC_MII_DATA);
2156793fc096SFrank Li 
2157793fc096SFrank Li 	/* wait for end of transfer */
2158f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
2159f166f890SAndrew Lunn 	if (ret)
216031b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO write timeout\n");
2161793fc096SFrank Li 
21628d03ad1aSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
21638d03ad1aSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
21648d03ad1aSAndrew Lunn 
21658d03ad1aSAndrew Lunn 	return ret;
21668d03ad1aSAndrew Lunn }
21678d03ad1aSAndrew Lunn 
21688d03ad1aSAndrew Lunn static int fec_enet_mdio_write_c45(struct mii_bus *bus, int mii_id,
21698d03ad1aSAndrew Lunn 				   int devad, int regnum, u16 value)
21708d03ad1aSAndrew Lunn {
21718d03ad1aSAndrew Lunn 	struct fec_enet_private *fep = bus->priv;
21728d03ad1aSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
21738d03ad1aSAndrew Lunn 	int ret, frame_start;
21748d03ad1aSAndrew Lunn 
21758d03ad1aSAndrew Lunn 	ret = pm_runtime_resume_and_get(dev);
21768d03ad1aSAndrew Lunn 	if (ret < 0)
21778d03ad1aSAndrew Lunn 		return ret;
21788d03ad1aSAndrew Lunn 
21798d03ad1aSAndrew Lunn 	frame_start = FEC_MMFR_ST_C45;
21808d03ad1aSAndrew Lunn 
21818d03ad1aSAndrew Lunn 	/* write address */
21828d03ad1aSAndrew Lunn 	writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
21838d03ad1aSAndrew Lunn 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
21848d03ad1aSAndrew Lunn 	       FEC_MMFR_TA | (regnum & 0xFFFF),
21858d03ad1aSAndrew Lunn 	       fep->hwp + FEC_MII_DATA);
21868d03ad1aSAndrew Lunn 
21878d03ad1aSAndrew Lunn 	/* wait for end of transfer */
21888d03ad1aSAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
21898d03ad1aSAndrew Lunn 	if (ret) {
21908d03ad1aSAndrew Lunn 		netdev_err(fep->netdev, "MDIO address write timeout\n");
21918d03ad1aSAndrew Lunn 		goto out;
21928d03ad1aSAndrew Lunn 	}
21938d03ad1aSAndrew Lunn 
21948d03ad1aSAndrew Lunn 	/* start a write op */
21958d03ad1aSAndrew Lunn 	writel(frame_start | FEC_MMFR_OP_WRITE |
21968d03ad1aSAndrew Lunn 	       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
21978d03ad1aSAndrew Lunn 	       FEC_MMFR_TA | FEC_MMFR_DATA(value),
21988d03ad1aSAndrew Lunn 	       fep->hwp + FEC_MII_DATA);
21998d03ad1aSAndrew Lunn 
22008d03ad1aSAndrew Lunn 	/* wait for end of transfer */
22018d03ad1aSAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
22028d03ad1aSAndrew Lunn 	if (ret)
22038d03ad1aSAndrew Lunn 		netdev_err(fep->netdev, "MDIO write timeout\n");
22048d03ad1aSAndrew Lunn 
2205d3ee8ec7SMarco Hartmann out:
22068fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
22078fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
22088fff755eSAndrew Lunn 
22098fff755eSAndrew Lunn 	return ret;
2210793fc096SFrank Li }
2211793fc096SFrank Li 
221264a632daSMarek Vasut static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
221364a632daSMarek Vasut {
221464a632daSMarek Vasut 	struct fec_enet_private *fep = netdev_priv(ndev);
221564a632daSMarek Vasut 	struct phy_device *phy_dev = ndev->phydev;
221664a632daSMarek Vasut 
221764a632daSMarek Vasut 	if (phy_dev) {
221864a632daSMarek Vasut 		phy_reset_after_clk_enable(phy_dev);
221964a632daSMarek Vasut 	} else if (fep->phy_node) {
222064a632daSMarek Vasut 		/*
222164a632daSMarek Vasut 		 * If the PHY still is not bound to the MAC, but there is
222264a632daSMarek Vasut 		 * OF PHY node and a matching PHY device instance already,
222364a632daSMarek Vasut 		 * use the OF PHY node to obtain the PHY device instance,
222464a632daSMarek Vasut 		 * and then use that PHY device instance when triggering
222564a632daSMarek Vasut 		 * the PHY reset.
222664a632daSMarek Vasut 		 */
222764a632daSMarek Vasut 		phy_dev = of_phy_find_device(fep->phy_node);
222864a632daSMarek Vasut 		phy_reset_after_clk_enable(phy_dev);
222964a632daSMarek Vasut 		put_device(&phy_dev->mdio.dev);
223064a632daSMarek Vasut 	}
223164a632daSMarek Vasut }
223264a632daSMarek Vasut 
2233e8fcfcd5SNimrod Andy static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
2234e8fcfcd5SNimrod Andy {
2235e8fcfcd5SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2236e8fcfcd5SNimrod Andy 	int ret;
2237e8fcfcd5SNimrod Andy 
2238e8fcfcd5SNimrod Andy 	if (enable) {
2239e8fcfcd5SNimrod Andy 		ret = clk_prepare_enable(fep->clk_enet_out);
2240e8fcfcd5SNimrod Andy 		if (ret)
2241d7c3a206SAndy Duan 			return ret;
224201e5943aSUwe Kleine-König 
2243e8fcfcd5SNimrod Andy 		if (fep->clk_ptp) {
224401b825f9SFrancesco Dolcini 			mutex_lock(&fep->ptp_clk_mutex);
2245e8fcfcd5SNimrod Andy 			ret = clk_prepare_enable(fep->clk_ptp);
224691c0d987SNimrod Andy 			if (ret) {
224701b825f9SFrancesco Dolcini 				mutex_unlock(&fep->ptp_clk_mutex);
2248e8fcfcd5SNimrod Andy 				goto failed_clk_ptp;
224991c0d987SNimrod Andy 			} else {
225091c0d987SNimrod Andy 				fep->ptp_clk_on = true;
225191c0d987SNimrod Andy 			}
225201b825f9SFrancesco Dolcini 			mutex_unlock(&fep->ptp_clk_mutex);
2253e8fcfcd5SNimrod Andy 		}
225401e5943aSUwe Kleine-König 
22559b5330edSFugang Duan 		ret = clk_prepare_enable(fep->clk_ref);
22569b5330edSFugang Duan 		if (ret)
22579b5330edSFugang Duan 			goto failed_clk_ref;
22581b0a83acSRichard Leitner 
2259fc539459SFugang Duan 		ret = clk_prepare_enable(fep->clk_2x_txclk);
2260fc539459SFugang Duan 		if (ret)
2261fc539459SFugang Duan 			goto failed_clk_2x_txclk;
2262fc539459SFugang Duan 
226364a632daSMarek Vasut 		fec_enet_phy_reset_after_clk_enable(ndev);
2264e8fcfcd5SNimrod Andy 	} else {
2265e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_enet_out);
226691c0d987SNimrod Andy 		if (fep->clk_ptp) {
226701b825f9SFrancesco Dolcini 			mutex_lock(&fep->ptp_clk_mutex);
2268e8fcfcd5SNimrod Andy 			clk_disable_unprepare(fep->clk_ptp);
226991c0d987SNimrod Andy 			fep->ptp_clk_on = false;
227001b825f9SFrancesco Dolcini 			mutex_unlock(&fep->ptp_clk_mutex);
227191c0d987SNimrod Andy 		}
22729b5330edSFugang Duan 		clk_disable_unprepare(fep->clk_ref);
2273fc539459SFugang Duan 		clk_disable_unprepare(fep->clk_2x_txclk);
2274e8fcfcd5SNimrod Andy 	}
2275e8fcfcd5SNimrod Andy 
2276e8fcfcd5SNimrod Andy 	return 0;
22779b5330edSFugang Duan 
2278fc539459SFugang Duan failed_clk_2x_txclk:
2279fc539459SFugang Duan 	if (fep->clk_ref)
2280fc539459SFugang Duan 		clk_disable_unprepare(fep->clk_ref);
22819b5330edSFugang Duan failed_clk_ref:
2282a74d19baSLiu Xiang 	if (fep->clk_ptp) {
228301b825f9SFrancesco Dolcini 		mutex_lock(&fep->ptp_clk_mutex);
2284a74d19baSLiu Xiang 		clk_disable_unprepare(fep->clk_ptp);
2285a74d19baSLiu Xiang 		fep->ptp_clk_on = false;
228601b825f9SFrancesco Dolcini 		mutex_unlock(&fep->ptp_clk_mutex);
2287a74d19baSLiu Xiang 	}
2288e8fcfcd5SNimrod Andy failed_clk_ptp:
2289e8fcfcd5SNimrod Andy 	clk_disable_unprepare(fep->clk_enet_out);
2290e8fcfcd5SNimrod Andy 
2291e8fcfcd5SNimrod Andy 	return ret;
2292e8fcfcd5SNimrod Andy }
2293e8fcfcd5SNimrod Andy 
2294b820c114SJoakim Zhang static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep,
2295b820c114SJoakim Zhang 				      struct device_node *np)
2296b820c114SJoakim Zhang {
2297b820c114SJoakim Zhang 	u32 rgmii_tx_delay, rgmii_rx_delay;
2298b820c114SJoakim Zhang 
2299b820c114SJoakim Zhang 	/* For rgmii tx internal delay, valid values are 0ps and 2000ps */
2300b820c114SJoakim Zhang 	if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) {
2301b820c114SJoakim Zhang 		if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) {
2302b820c114SJoakim Zhang 			dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps");
2303b820c114SJoakim Zhang 			return -EINVAL;
2304b820c114SJoakim Zhang 		} else if (rgmii_tx_delay == 2000) {
2305b820c114SJoakim Zhang 			fep->rgmii_txc_dly = true;
2306b820c114SJoakim Zhang 		}
2307b820c114SJoakim Zhang 	}
2308b820c114SJoakim Zhang 
2309b820c114SJoakim Zhang 	/* For rgmii rx internal delay, valid values are 0ps and 2000ps */
2310b820c114SJoakim Zhang 	if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) {
2311b820c114SJoakim Zhang 		if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) {
2312b820c114SJoakim Zhang 			dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps");
2313b820c114SJoakim Zhang 			return -EINVAL;
2314b820c114SJoakim Zhang 		} else if (rgmii_rx_delay == 2000) {
2315b820c114SJoakim Zhang 			fep->rgmii_rxc_dly = true;
2316b820c114SJoakim Zhang 		}
2317b820c114SJoakim Zhang 	}
2318b820c114SJoakim Zhang 
2319b820c114SJoakim Zhang 	return 0;
2320b820c114SJoakim Zhang }
2321b820c114SJoakim Zhang 
2322793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev)
2323793fc096SFrank Li {
2324793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2325793fc096SFrank Li 	struct phy_device *phy_dev = NULL;
2326793fc096SFrank Li 	char mdio_bus_id[MII_BUS_ID_SIZE];
2327793fc096SFrank Li 	char phy_name[MII_BUS_ID_SIZE + 3];
2328793fc096SFrank Li 	int phy_id;
2329793fc096SFrank Li 	int dev_id = fep->dev_id;
2330793fc096SFrank Li 
2331407066f8SUwe Kleine-König 	if (fep->phy_node) {
2332407066f8SUwe Kleine-König 		phy_dev = of_phy_connect(ndev, fep->phy_node,
2333407066f8SUwe Kleine-König 					 &fec_enet_adjust_link, 0,
2334407066f8SUwe Kleine-König 					 fep->phy_interface);
23359558df3aSAndrew Lunn 		if (!phy_dev) {
23369558df3aSAndrew Lunn 			netdev_err(ndev, "Unable to connect to phy\n");
2337213a9922SNimrod Andy 			return -ENODEV;
23389558df3aSAndrew Lunn 		}
2339407066f8SUwe Kleine-König 	} else {
2340793fc096SFrank Li 		/* check for attached phy */
2341793fc096SFrank Li 		for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
23427f854420SAndrew Lunn 			if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
2343793fc096SFrank Li 				continue;
2344793fc096SFrank Li 			if (dev_id--)
2345793fc096SFrank Li 				continue;
2346f029c781SWolfram Sang 			strscpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
2347793fc096SFrank Li 			break;
2348793fc096SFrank Li 		}
2349793fc096SFrank Li 
2350793fc096SFrank Li 		if (phy_id >= PHY_MAX_ADDR) {
235131b7720cSJoe Perches 			netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
2352f029c781SWolfram Sang 			strscpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
2353793fc096SFrank Li 			phy_id = 0;
2354793fc096SFrank Li 		}
2355793fc096SFrank Li 
2356407066f8SUwe Kleine-König 		snprintf(phy_name, sizeof(phy_name),
2357407066f8SUwe Kleine-König 			 PHY_ID_FMT, mdio_bus_id, phy_id);
2358793fc096SFrank Li 		phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
2359793fc096SFrank Li 				      fep->phy_interface);
2360407066f8SUwe Kleine-König 	}
2361407066f8SUwe Kleine-König 
2362793fc096SFrank Li 	if (IS_ERR(phy_dev)) {
236331b7720cSJoe Perches 		netdev_err(ndev, "could not attach to PHY\n");
2364793fc096SFrank Li 		return PTR_ERR(phy_dev);
2365793fc096SFrank Li 	}
2366793fc096SFrank Li 
2367793fc096SFrank Li 	/* mask with MAC supported features */
23686b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
236958056c1eSAndrew Lunn 		phy_set_max_speed(phy_dev, 1000);
237041124fa6SAndrew Lunn 		phy_remove_link_mode(phy_dev,
237141124fa6SAndrew Lunn 				     ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
2372d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2373c306ad36SAndrew Lunn 		phy_support_sym_pause(phy_dev);
2374d1391930SGuenter Roeck #endif
2375793fc096SFrank Li 	}
2376793fc096SFrank Li 	else
237758056c1eSAndrew Lunn 		phy_set_max_speed(phy_dev, 100);
2378793fc096SFrank Li 
2379793fc096SFrank Li 	fep->link = 0;
2380793fc096SFrank Li 	fep->full_duplex = 0;
2381793fc096SFrank Li 
2382eca485d2SDenis Kirjanov 	phy_dev->mac_managed_pm = true;
2383557d5dc8SHeiner Kallweit 
23842220943aSAndrew Lunn 	phy_attached_info(phy_dev);
2385793fc096SFrank Li 
2386793fc096SFrank Li 	return 0;
2387793fc096SFrank Li }
2388793fc096SFrank Li 
2389793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev)
2390793fc096SFrank Li {
2391793fc096SFrank Li 	static struct mii_bus *fec0_mii_bus;
2392793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
2393793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
23943c01eb62SAndrew Lunn 	bool suppress_preamble = false;
2395407066f8SUwe Kleine-König 	struct device_node *node;
2396e7f4dc35SAndrew Lunn 	int err = -ENXIO;
239763c60732SUwe Kleine-König 	u32 mii_speed, holdtime;
23983e782985SAndrew Lunn 	u32 bus_freq;
2399793fc096SFrank Li 
2400793fc096SFrank Li 	/*
24013d125f9cSStefan Agner 	 * The i.MX28 dual fec interfaces are not equal.
2402793fc096SFrank Li 	 * Here are the differences:
2403793fc096SFrank Li 	 *
2404793fc096SFrank Li 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
2405793fc096SFrank Li 	 *  - fec0 acts as the 1588 time master while fec1 is slave
2406793fc096SFrank Li 	 *  - external phys can only be configured by fec0
2407793fc096SFrank Li 	 *
2408793fc096SFrank Li 	 * That is to say fec1 can not work independently. It only works
2409793fc096SFrank Li 	 * when fec0 is working. The reason behind this design is that the
2410793fc096SFrank Li 	 * second interface is added primarily for Switch mode.
2411793fc096SFrank Li 	 *
2412793fc096SFrank Li 	 * Because of the last point above, both phys are attached on fec0
2413793fc096SFrank Li 	 * mdio interface in board design, and need to be configured by
2414793fc096SFrank Li 	 * fec0 mii_bus.
2415793fc096SFrank Li 	 */
24163d125f9cSStefan Agner 	if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
2417793fc096SFrank Li 		/* fec1 uses fec0 mii_bus */
2418793fc096SFrank Li 		if (mii_cnt && fec0_mii_bus) {
2419793fc096SFrank Li 			fep->mii_bus = fec0_mii_bus;
2420793fc096SFrank Li 			mii_cnt++;
2421793fc096SFrank Li 			return 0;
2422793fc096SFrank Li 		}
2423793fc096SFrank Li 		return -ENOENT;
2424793fc096SFrank Li 	}
2425793fc096SFrank Li 
24263e782985SAndrew Lunn 	bus_freq = 2500000; /* 2.5MHz by default */
24273e782985SAndrew Lunn 	node = of_get_child_by_name(pdev->dev.of_node, "mdio");
24283c01eb62SAndrew Lunn 	if (node) {
24293e782985SAndrew Lunn 		of_property_read_u32(node, "clock-frequency", &bus_freq);
24303c01eb62SAndrew Lunn 		suppress_preamble = of_property_read_bool(node,
24313c01eb62SAndrew Lunn 							  "suppress-preamble");
24323c01eb62SAndrew Lunn 	}
24333e782985SAndrew Lunn 
2434793fc096SFrank Li 	/*
24353e782985SAndrew Lunn 	 * Set MII speed (= clk_get_rate() / 2 * phy_speed)
2436793fc096SFrank Li 	 *
2437793fc096SFrank Li 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2438793fc096SFrank Li 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
2439793fc096SFrank Li 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2440793fc096SFrank Li 	 * document.
2441793fc096SFrank Li 	 */
24423e782985SAndrew Lunn 	mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2);
24436b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC)
244463c60732SUwe Kleine-König 		mii_speed--;
244563c60732SUwe Kleine-König 	if (mii_speed > 63) {
244663c60732SUwe Kleine-König 		dev_err(&pdev->dev,
2447981a0547SPeter Meerwald-Stadler 			"fec clock (%lu) too fast to get right mii speed\n",
244863c60732SUwe Kleine-König 			clk_get_rate(fep->clk_ipg));
244963c60732SUwe Kleine-König 		err = -EINVAL;
245063c60732SUwe Kleine-König 		goto err_out;
245163c60732SUwe Kleine-König 	}
245263c60732SUwe Kleine-König 
245363c60732SUwe Kleine-König 	/*
245463c60732SUwe Kleine-König 	 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
245563c60732SUwe Kleine-König 	 * MII_SPEED) register that defines the MDIO output hold time. Earlier
245663c60732SUwe Kleine-König 	 * versions are RAZ there, so just ignore the difference and write the
245763c60732SUwe Kleine-König 	 * register always.
245863c60732SUwe Kleine-König 	 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
245963c60732SUwe Kleine-König 	 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
246063c60732SUwe Kleine-König 	 * output.
246163c60732SUwe Kleine-König 	 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
246263c60732SUwe Kleine-König 	 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
246363c60732SUwe Kleine-König 	 * holdtime cannot result in a value greater than 3.
246463c60732SUwe Kleine-König 	 */
246563c60732SUwe Kleine-König 	holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
246663c60732SUwe Kleine-König 
246763c60732SUwe Kleine-König 	fep->phy_speed = mii_speed << 1 | holdtime << 8;
246863c60732SUwe Kleine-König 
24693c01eb62SAndrew Lunn 	if (suppress_preamble)
24703c01eb62SAndrew Lunn 		fep->phy_speed |= BIT(7);
24713c01eb62SAndrew Lunn 
24721e6114f5SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) {
2473f166f890SAndrew Lunn 		/* Clear MMFR to avoid to generate MII event by writing MSCR.
2474f166f890SAndrew Lunn 		 * MII event generation condition:
2475f166f890SAndrew Lunn 		 * - writing MSCR:
2476f166f890SAndrew Lunn 		 *	- mmfr[31:0]_not_zero & mscr[7:0]_is_zero &
2477f166f890SAndrew Lunn 		 *	  mscr_reg_data_in[7:0] != 0
2478f166f890SAndrew Lunn 		 * - writing MMFR:
2479f166f890SAndrew Lunn 		 *	- mscr[7:0]_not_zero
2480f166f890SAndrew Lunn 		 */
2481f166f890SAndrew Lunn 		writel(0, fep->hwp + FEC_MII_DATA);
24821e6114f5SGreg Ungerer 	}
2483f166f890SAndrew Lunn 
2484793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2485793fc096SFrank Li 
2486f166f890SAndrew Lunn 	/* Clear any pending transaction complete indication */
2487f166f890SAndrew Lunn 	writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
2488f166f890SAndrew Lunn 
2489793fc096SFrank Li 	fep->mii_bus = mdiobus_alloc();
2490793fc096SFrank Li 	if (fep->mii_bus == NULL) {
2491793fc096SFrank Li 		err = -ENOMEM;
2492793fc096SFrank Li 		goto err_out;
2493793fc096SFrank Li 	}
2494793fc096SFrank Li 
2495793fc096SFrank Li 	fep->mii_bus->name = "fec_enet_mii_bus";
24968d03ad1aSAndrew Lunn 	fep->mii_bus->read = fec_enet_mdio_read_c22;
24978d03ad1aSAndrew Lunn 	fep->mii_bus->write = fec_enet_mdio_write_c22;
2498abc33494SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_HAS_MDIO_C45) {
24998d03ad1aSAndrew Lunn 		fep->mii_bus->read_c45 = fec_enet_mdio_read_c45;
25008d03ad1aSAndrew Lunn 		fep->mii_bus->write_c45 = fec_enet_mdio_write_c45;
2501abc33494SGreg Ungerer 	}
2502793fc096SFrank Li 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2503793fc096SFrank Li 		pdev->name, fep->dev_id + 1);
2504793fc096SFrank Li 	fep->mii_bus->priv = fep;
2505793fc096SFrank Li 	fep->mii_bus->parent = &pdev->dev;
2506793fc096SFrank Li 
2507407066f8SUwe Kleine-König 	err = of_mdiobus_register(fep->mii_bus, node);
2508407066f8SUwe Kleine-König 	if (err)
2509e7f4dc35SAndrew Lunn 		goto err_out_free_mdiobus;
25100607a2cdSPan Bian 	of_node_put(node);
2511793fc096SFrank Li 
2512793fc096SFrank Li 	mii_cnt++;
2513793fc096SFrank Li 
2514793fc096SFrank Li 	/* save fec0 mii_bus */
25153d125f9cSStefan Agner 	if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2516793fc096SFrank Li 		fec0_mii_bus = fep->mii_bus;
2517793fc096SFrank Li 
2518793fc096SFrank Li 	return 0;
2519793fc096SFrank Li 
2520793fc096SFrank Li err_out_free_mdiobus:
2521793fc096SFrank Li 	mdiobus_free(fep->mii_bus);
2522793fc096SFrank Li err_out:
25230607a2cdSPan Bian 	of_node_put(node);
2524793fc096SFrank Li 	return err;
2525793fc096SFrank Li }
2526793fc096SFrank Li 
2527793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep)
2528793fc096SFrank Li {
2529793fc096SFrank Li 	if (--mii_cnt == 0) {
2530793fc096SFrank Li 		mdiobus_unregister(fep->mii_bus);
2531793fc096SFrank Li 		mdiobus_free(fep->mii_bus);
2532793fc096SFrank Li 	}
2533793fc096SFrank Li }
2534793fc096SFrank Li 
2535793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev,
2536793fc096SFrank Li 				 struct ethtool_drvinfo *info)
2537793fc096SFrank Li {
2538793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2539793fc096SFrank Li 
2540f029c781SWolfram Sang 	strscpy(info->driver, fep->pdev->dev.driver->name,
2541793fc096SFrank Li 		sizeof(info->driver));
2542f029c781SWolfram Sang 	strscpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2543793fc096SFrank Li }
2544793fc096SFrank Li 
2545db65f35fSPhilippe Reynes static int fec_enet_get_regs_len(struct net_device *ndev)
2546db65f35fSPhilippe Reynes {
2547db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2548db65f35fSPhilippe Reynes 	struct resource *r;
2549db65f35fSPhilippe Reynes 	int s = 0;
2550db65f35fSPhilippe Reynes 
2551db65f35fSPhilippe Reynes 	r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2552db65f35fSPhilippe Reynes 	if (r)
2553db65f35fSPhilippe Reynes 		s = resource_size(r);
2554db65f35fSPhilippe Reynes 
2555db65f35fSPhilippe Reynes 	return s;
2556db65f35fSPhilippe Reynes }
2557db65f35fSPhilippe Reynes 
2558db65f35fSPhilippe Reynes /* List of registers that can be safety be read to dump them with ethtool */
2559db65f35fSPhilippe Reynes #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
25603f1dcc6aSLucas Stach 	defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
256178cc6e7eSFlorian Fainelli 	defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
2562f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 2;
2563db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2564db65f35fSPhilippe Reynes 	FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2565db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2566db65f35fSPhilippe Reynes 	FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2567db65f35fSPhilippe Reynes 	FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2568db65f35fSPhilippe Reynes 	FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2569db65f35fSPhilippe Reynes 	FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2570db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2571db65f35fSPhilippe Reynes 	FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2572db65f35fSPhilippe Reynes 	FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2573db65f35fSPhilippe Reynes 	FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2574db65f35fSPhilippe Reynes 	FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2575db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2576db65f35fSPhilippe Reynes 	RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2577db65f35fSPhilippe Reynes 	RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2578db65f35fSPhilippe Reynes 	RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2579db65f35fSPhilippe Reynes 	RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2580db65f35fSPhilippe Reynes 	RMON_T_P_GTE2048, RMON_T_OCTETS,
2581db65f35fSPhilippe Reynes 	IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2582db65f35fSPhilippe Reynes 	IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2583db65f35fSPhilippe Reynes 	IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2584db65f35fSPhilippe Reynes 	RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2585db65f35fSPhilippe Reynes 	RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2586db65f35fSPhilippe Reynes 	RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2587db65f35fSPhilippe Reynes 	RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2588db65f35fSPhilippe Reynes 	RMON_R_P_GTE2048, RMON_R_OCTETS,
2589db65f35fSPhilippe Reynes 	IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2590db65f35fSPhilippe Reynes 	IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2591db65f35fSPhilippe Reynes };
25920a8b43b1SJuergen Borleis /* for i.MX6ul */
25930a8b43b1SJuergen Borleis static u32 fec_enet_register_offset_6ul[] = {
25940a8b43b1SJuergen Borleis 	FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
25950a8b43b1SJuergen Borleis 	FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
25960a8b43b1SJuergen Borleis 	FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_RXIC0,
25970a8b43b1SJuergen Borleis 	FEC_HASH_TABLE_HIGH, FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH,
25980a8b43b1SJuergen Borleis 	FEC_GRP_HASH_TABLE_LOW, FEC_X_WMRK, FEC_R_DES_START_0,
25990a8b43b1SJuergen Borleis 	FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
26000a8b43b1SJuergen Borleis 	FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC,
26010a8b43b1SJuergen Borleis 	RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
26020a8b43b1SJuergen Borleis 	RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
26030a8b43b1SJuergen Borleis 	RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
26040a8b43b1SJuergen Borleis 	RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
26050a8b43b1SJuergen Borleis 	RMON_T_P_GTE2048, RMON_T_OCTETS,
26060a8b43b1SJuergen Borleis 	IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
26070a8b43b1SJuergen Borleis 	IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
26080a8b43b1SJuergen Borleis 	IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
26090a8b43b1SJuergen Borleis 	RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
26100a8b43b1SJuergen Borleis 	RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
26110a8b43b1SJuergen Borleis 	RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
26120a8b43b1SJuergen Borleis 	RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
26130a8b43b1SJuergen Borleis 	RMON_R_P_GTE2048, RMON_R_OCTETS,
26140a8b43b1SJuergen Borleis 	IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
26150a8b43b1SJuergen Borleis 	IEEE_R_FDXFC, IEEE_R_OCTETS_OK
26160a8b43b1SJuergen Borleis };
2617db65f35fSPhilippe Reynes #else
2618f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 1;
2619db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2620db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2621db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2622db65f35fSPhilippe Reynes 	FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2623db65f35fSPhilippe Reynes 	FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2624db65f35fSPhilippe Reynes 	FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2625db65f35fSPhilippe Reynes 	FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2626db65f35fSPhilippe Reynes 	FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2627db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2628db65f35fSPhilippe Reynes 	FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2629db65f35fSPhilippe Reynes };
2630db65f35fSPhilippe Reynes #endif
2631db65f35fSPhilippe Reynes 
2632db65f35fSPhilippe Reynes static void fec_enet_get_regs(struct net_device *ndev,
2633db65f35fSPhilippe Reynes 			      struct ethtool_regs *regs, void *regbuf)
2634db65f35fSPhilippe Reynes {
2635db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2636db65f35fSPhilippe Reynes 	u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2637c72a0bc0SAndrew Lunn 	struct device *dev = &fep->pdev->dev;
2638db65f35fSPhilippe Reynes 	u32 *buf = (u32 *)regbuf;
2639db65f35fSPhilippe Reynes 	u32 i, off;
2640c72a0bc0SAndrew Lunn 	int ret;
26410a8b43b1SJuergen Borleis #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
26420a8b43b1SJuergen Borleis 	defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
26430a8b43b1SJuergen Borleis 	defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
26440a8b43b1SJuergen Borleis 	u32 *reg_list;
26450a8b43b1SJuergen Borleis 	u32 reg_cnt;
2646c72a0bc0SAndrew Lunn 
26470a8b43b1SJuergen Borleis 	if (!of_machine_is_compatible("fsl,imx6ul")) {
26480a8b43b1SJuergen Borleis 		reg_list = fec_enet_register_offset;
26490a8b43b1SJuergen Borleis 		reg_cnt = ARRAY_SIZE(fec_enet_register_offset);
26500a8b43b1SJuergen Borleis 	} else {
26510a8b43b1SJuergen Borleis 		reg_list = fec_enet_register_offset_6ul;
26520a8b43b1SJuergen Borleis 		reg_cnt = ARRAY_SIZE(fec_enet_register_offset_6ul);
26530a8b43b1SJuergen Borleis 	}
26540a8b43b1SJuergen Borleis #else
26550a8b43b1SJuergen Borleis 	/* coldfire */
26560a8b43b1SJuergen Borleis 	static u32 *reg_list = fec_enet_register_offset;
26570a8b43b1SJuergen Borleis 	static const u32 reg_cnt = ARRAY_SIZE(fec_enet_register_offset);
26580a8b43b1SJuergen Borleis #endif
2659da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
2660c72a0bc0SAndrew Lunn 	if (ret < 0)
2661c72a0bc0SAndrew Lunn 		return;
2662db65f35fSPhilippe Reynes 
2663f9bcc9f3SVivien Didelot 	regs->version = fec_enet_register_version;
2664f9bcc9f3SVivien Didelot 
2665db65f35fSPhilippe Reynes 	memset(buf, 0, regs->len);
2666db65f35fSPhilippe Reynes 
26670a8b43b1SJuergen Borleis 	for (i = 0; i < reg_cnt; i++) {
26680a8b43b1SJuergen Borleis 		off = reg_list[i];
2669ec20a63aSFugang Duan 
2670ec20a63aSFugang Duan 		if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
2671ec20a63aSFugang Duan 		    !(fep->quirks & FEC_QUIRK_HAS_FRREG))
2672ec20a63aSFugang Duan 			continue;
2673ec20a63aSFugang Duan 
2674ec20a63aSFugang Duan 		off >>= 2;
2675db65f35fSPhilippe Reynes 		buf[off] = readl(&theregs[off]);
2676db65f35fSPhilippe Reynes 	}
2677c72a0bc0SAndrew Lunn 
2678c72a0bc0SAndrew Lunn 	pm_runtime_mark_last_busy(dev);
2679c72a0bc0SAndrew Lunn 	pm_runtime_put_autosuspend(dev);
2680db65f35fSPhilippe Reynes }
2681db65f35fSPhilippe Reynes 
2682793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev,
2683793fc096SFrank Li 				struct ethtool_ts_info *info)
2684793fc096SFrank Li {
2685793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2686793fc096SFrank Li 
2687793fc096SFrank Li 	if (fep->bufdesc_ex) {
2688793fc096SFrank Li 
2689793fc096SFrank Li 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2690793fc096SFrank Li 					SOF_TIMESTAMPING_RX_SOFTWARE |
2691793fc096SFrank Li 					SOF_TIMESTAMPING_SOFTWARE |
2692793fc096SFrank Li 					SOF_TIMESTAMPING_TX_HARDWARE |
2693793fc096SFrank Li 					SOF_TIMESTAMPING_RX_HARDWARE |
2694793fc096SFrank Li 					SOF_TIMESTAMPING_RAW_HARDWARE;
2695793fc096SFrank Li 		if (fep->ptp_clock)
2696793fc096SFrank Li 			info->phc_index = ptp_clock_index(fep->ptp_clock);
2697793fc096SFrank Li 		else
2698793fc096SFrank Li 			info->phc_index = -1;
2699793fc096SFrank Li 
2700793fc096SFrank Li 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2701793fc096SFrank Li 				 (1 << HWTSTAMP_TX_ON);
2702793fc096SFrank Li 
2703793fc096SFrank Li 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2704793fc096SFrank Li 				   (1 << HWTSTAMP_FILTER_ALL);
2705793fc096SFrank Li 		return 0;
2706793fc096SFrank Li 	} else {
2707793fc096SFrank Li 		return ethtool_op_get_ts_info(ndev, info);
2708793fc096SFrank Li 	}
2709793fc096SFrank Li }
2710793fc096SFrank Li 
2711d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2712d1391930SGuenter Roeck 
2713793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev,
2714793fc096SFrank Li 				    struct ethtool_pauseparam *pause)
2715793fc096SFrank Li {
2716793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2717793fc096SFrank Li 
2718793fc096SFrank Li 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2719793fc096SFrank Li 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2720793fc096SFrank Li 	pause->rx_pause = pause->tx_pause;
2721793fc096SFrank Li }
2722793fc096SFrank Li 
2723793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev,
2724793fc096SFrank Li 				   struct ethtool_pauseparam *pause)
2725793fc096SFrank Li {
2726793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2727793fc096SFrank Li 
272845f5c327SPhilippe Reynes 	if (!ndev->phydev)
27290b146ca8SRussell King 		return -ENODEV;
27300b146ca8SRussell King 
2731793fc096SFrank Li 	if (pause->tx_pause != pause->rx_pause) {
2732793fc096SFrank Li 		netdev_info(ndev,
2733793fc096SFrank Li 			"hardware only support enable/disable both tx and rx");
2734793fc096SFrank Li 		return -EINVAL;
2735793fc096SFrank Li 	}
2736793fc096SFrank Li 
2737793fc096SFrank Li 	fep->pause_flag = 0;
2738793fc096SFrank Li 
2739793fc096SFrank Li 	/* tx pause must be same as rx pause */
2740793fc096SFrank Li 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2741793fc096SFrank Li 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2742793fc096SFrank Li 
27430c122405SAndrew Lunn 	phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
27440c122405SAndrew Lunn 			  pause->autoneg);
2745793fc096SFrank Li 
2746793fc096SFrank Li 	if (pause->autoneg) {
2747793fc096SFrank Li 		if (netif_running(ndev))
2748793fc096SFrank Li 			fec_stop(ndev);
274945f5c327SPhilippe Reynes 		phy_start_aneg(ndev->phydev);
2750793fc096SFrank Li 	}
2751dbc64a8eSRussell King 	if (netif_running(ndev)) {
2752dbc64a8eSRussell King 		napi_disable(&fep->napi);
2753dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
2754ef83337dSRussell King 		fec_restart(ndev);
2755657ade07SRickard x Andersson 		netif_tx_wake_all_queues(ndev);
27566af42d42SRussell King 		netif_tx_unlock_bh(ndev);
2757dbc64a8eSRussell King 		napi_enable(&fep->napi);
2758dbc64a8eSRussell King 	}
2759793fc096SFrank Li 
2760793fc096SFrank Li 	return 0;
2761793fc096SFrank Li }
2762793fc096SFrank Li 
276338ae92dcSChris Healy static const struct fec_stat {
276438ae92dcSChris Healy 	char name[ETH_GSTRING_LEN];
276538ae92dcSChris Healy 	u16 offset;
276638ae92dcSChris Healy } fec_stats[] = {
276738ae92dcSChris Healy 	/* RMON TX */
276838ae92dcSChris Healy 	{ "tx_dropped", RMON_T_DROP },
276938ae92dcSChris Healy 	{ "tx_packets", RMON_T_PACKETS },
277038ae92dcSChris Healy 	{ "tx_broadcast", RMON_T_BC_PKT },
277138ae92dcSChris Healy 	{ "tx_multicast", RMON_T_MC_PKT },
277238ae92dcSChris Healy 	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
277338ae92dcSChris Healy 	{ "tx_undersize", RMON_T_UNDERSIZE },
277438ae92dcSChris Healy 	{ "tx_oversize", RMON_T_OVERSIZE },
277538ae92dcSChris Healy 	{ "tx_fragment", RMON_T_FRAG },
277638ae92dcSChris Healy 	{ "tx_jabber", RMON_T_JAB },
277738ae92dcSChris Healy 	{ "tx_collision", RMON_T_COL },
277838ae92dcSChris Healy 	{ "tx_64byte", RMON_T_P64 },
277938ae92dcSChris Healy 	{ "tx_65to127byte", RMON_T_P65TO127 },
278038ae92dcSChris Healy 	{ "tx_128to255byte", RMON_T_P128TO255 },
278138ae92dcSChris Healy 	{ "tx_256to511byte", RMON_T_P256TO511 },
278238ae92dcSChris Healy 	{ "tx_512to1023byte", RMON_T_P512TO1023 },
278338ae92dcSChris Healy 	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
278438ae92dcSChris Healy 	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
278538ae92dcSChris Healy 	{ "tx_octets", RMON_T_OCTETS },
278638ae92dcSChris Healy 
278738ae92dcSChris Healy 	/* IEEE TX */
278838ae92dcSChris Healy 	{ "IEEE_tx_drop", IEEE_T_DROP },
278938ae92dcSChris Healy 	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
279038ae92dcSChris Healy 	{ "IEEE_tx_1col", IEEE_T_1COL },
279138ae92dcSChris Healy 	{ "IEEE_tx_mcol", IEEE_T_MCOL },
279238ae92dcSChris Healy 	{ "IEEE_tx_def", IEEE_T_DEF },
279338ae92dcSChris Healy 	{ "IEEE_tx_lcol", IEEE_T_LCOL },
279438ae92dcSChris Healy 	{ "IEEE_tx_excol", IEEE_T_EXCOL },
279538ae92dcSChris Healy 	{ "IEEE_tx_macerr", IEEE_T_MACERR },
279638ae92dcSChris Healy 	{ "IEEE_tx_cserr", IEEE_T_CSERR },
279738ae92dcSChris Healy 	{ "IEEE_tx_sqe", IEEE_T_SQE },
279838ae92dcSChris Healy 	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
279938ae92dcSChris Healy 	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
280038ae92dcSChris Healy 
280138ae92dcSChris Healy 	/* RMON RX */
280238ae92dcSChris Healy 	{ "rx_packets", RMON_R_PACKETS },
280338ae92dcSChris Healy 	{ "rx_broadcast", RMON_R_BC_PKT },
280438ae92dcSChris Healy 	{ "rx_multicast", RMON_R_MC_PKT },
280538ae92dcSChris Healy 	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
280638ae92dcSChris Healy 	{ "rx_undersize", RMON_R_UNDERSIZE },
280738ae92dcSChris Healy 	{ "rx_oversize", RMON_R_OVERSIZE },
280838ae92dcSChris Healy 	{ "rx_fragment", RMON_R_FRAG },
280938ae92dcSChris Healy 	{ "rx_jabber", RMON_R_JAB },
281038ae92dcSChris Healy 	{ "rx_64byte", RMON_R_P64 },
281138ae92dcSChris Healy 	{ "rx_65to127byte", RMON_R_P65TO127 },
281238ae92dcSChris Healy 	{ "rx_128to255byte", RMON_R_P128TO255 },
281338ae92dcSChris Healy 	{ "rx_256to511byte", RMON_R_P256TO511 },
281438ae92dcSChris Healy 	{ "rx_512to1023byte", RMON_R_P512TO1023 },
281538ae92dcSChris Healy 	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
281638ae92dcSChris Healy 	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
281738ae92dcSChris Healy 	{ "rx_octets", RMON_R_OCTETS },
281838ae92dcSChris Healy 
281938ae92dcSChris Healy 	/* IEEE RX */
282038ae92dcSChris Healy 	{ "IEEE_rx_drop", IEEE_R_DROP },
282138ae92dcSChris Healy 	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
282238ae92dcSChris Healy 	{ "IEEE_rx_crc", IEEE_R_CRC },
282338ae92dcSChris Healy 	{ "IEEE_rx_align", IEEE_R_ALIGN },
282438ae92dcSChris Healy 	{ "IEEE_rx_macerr", IEEE_R_MACERR },
282538ae92dcSChris Healy 	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
282638ae92dcSChris Healy 	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
282738ae92dcSChris Healy };
282838ae92dcSChris Healy 
2829f85de666SNikita Yushchenko #define FEC_STATS_SIZE		(ARRAY_SIZE(fec_stats) * sizeof(u64))
2830f85de666SNikita Yushchenko 
28316970ef27SShenwei Wang static const char *fec_xdp_stat_strs[XDP_STATS_TOTAL] = {
28326970ef27SShenwei Wang 	"rx_xdp_redirect",           /* RX_XDP_REDIRECT = 0, */
28336970ef27SShenwei Wang 	"rx_xdp_pass",               /* RX_XDP_PASS, */
28346970ef27SShenwei Wang 	"rx_xdp_drop",               /* RX_XDP_DROP, */
28356970ef27SShenwei Wang 	"rx_xdp_tx",                 /* RX_XDP_TX, */
28366970ef27SShenwei Wang 	"rx_xdp_tx_errors",          /* RX_XDP_TX_ERRORS, */
28376970ef27SShenwei Wang 	"tx_xdp_xmit",               /* TX_XDP_XMIT, */
28386970ef27SShenwei Wang 	"tx_xdp_xmit_errors",        /* TX_XDP_XMIT_ERRORS, */
28396970ef27SShenwei Wang };
28406970ef27SShenwei Wang 
284180cca775SNikita Yushchenko static void fec_enet_update_ethtool_stats(struct net_device *dev)
284238ae92dcSChris Healy {
284338ae92dcSChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
284438ae92dcSChris Healy 	int i;
284538ae92dcSChris Healy 
284638ae92dcSChris Healy 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
284780cca775SNikita Yushchenko 		fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
284880cca775SNikita Yushchenko }
284980cca775SNikita Yushchenko 
28506970ef27SShenwei Wang static void fec_enet_get_xdp_stats(struct fec_enet_private *fep, u64 *data)
28516970ef27SShenwei Wang {
28526970ef27SShenwei Wang 	u64 xdp_stats[XDP_STATS_TOTAL] = { 0 };
28536970ef27SShenwei Wang 	struct fec_enet_priv_rx_q *rxq;
28546970ef27SShenwei Wang 	int i, j;
28556970ef27SShenwei Wang 
28566970ef27SShenwei Wang 	for (i = fep->num_rx_queues - 1; i >= 0; i--) {
28576970ef27SShenwei Wang 		rxq = fep->rx_queue[i];
28586970ef27SShenwei Wang 
28596970ef27SShenwei Wang 		for (j = 0; j < XDP_STATS_TOTAL; j++)
28606970ef27SShenwei Wang 			xdp_stats[j] += rxq->stats[j];
28616970ef27SShenwei Wang 	}
28626970ef27SShenwei Wang 
28636970ef27SShenwei Wang 	memcpy(data, xdp_stats, sizeof(xdp_stats));
28646970ef27SShenwei Wang }
28656970ef27SShenwei Wang 
28666970ef27SShenwei Wang static void fec_enet_page_pool_stats(struct fec_enet_private *fep, u64 *data)
28676970ef27SShenwei Wang {
2868857922b1SLucas Stach #ifdef CONFIG_PAGE_POOL_STATS
28696970ef27SShenwei Wang 	struct page_pool_stats stats = {};
28706970ef27SShenwei Wang 	struct fec_enet_priv_rx_q *rxq;
28716970ef27SShenwei Wang 	int i;
28726970ef27SShenwei Wang 
28736970ef27SShenwei Wang 	for (i = fep->num_rx_queues - 1; i >= 0; i--) {
28746970ef27SShenwei Wang 		rxq = fep->rx_queue[i];
28756970ef27SShenwei Wang 
28766970ef27SShenwei Wang 		if (!rxq->page_pool)
28776970ef27SShenwei Wang 			continue;
28786970ef27SShenwei Wang 
28796970ef27SShenwei Wang 		page_pool_get_stats(rxq->page_pool, &stats);
28806970ef27SShenwei Wang 	}
28816970ef27SShenwei Wang 
28826970ef27SShenwei Wang 	page_pool_ethtool_stats_get(data, &stats);
2883857922b1SLucas Stach #endif
28846970ef27SShenwei Wang }
28856970ef27SShenwei Wang 
288680cca775SNikita Yushchenko static void fec_enet_get_ethtool_stats(struct net_device *dev,
288780cca775SNikita Yushchenko 				       struct ethtool_stats *stats, u64 *data)
288880cca775SNikita Yushchenko {
288980cca775SNikita Yushchenko 	struct fec_enet_private *fep = netdev_priv(dev);
289080cca775SNikita Yushchenko 
289180cca775SNikita Yushchenko 	if (netif_running(dev))
289280cca775SNikita Yushchenko 		fec_enet_update_ethtool_stats(dev);
289380cca775SNikita Yushchenko 
2894f85de666SNikita Yushchenko 	memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
28956970ef27SShenwei Wang 	data += FEC_STATS_SIZE / sizeof(u64);
28966970ef27SShenwei Wang 
28976970ef27SShenwei Wang 	fec_enet_get_xdp_stats(fep, data);
28986970ef27SShenwei Wang 	data += XDP_STATS_TOTAL;
28996970ef27SShenwei Wang 
29006970ef27SShenwei Wang 	fec_enet_page_pool_stats(fep, data);
290138ae92dcSChris Healy }
290238ae92dcSChris Healy 
290338ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev,
290438ae92dcSChris Healy 	u32 stringset, u8 *data)
290538ae92dcSChris Healy {
290638ae92dcSChris Healy 	int i;
290738ae92dcSChris Healy 	switch (stringset) {
290838ae92dcSChris Healy 	case ETH_SS_STATS:
29096970ef27SShenwei Wang 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++) {
29106970ef27SShenwei Wang 			memcpy(data, fec_stats[i].name, ETH_GSTRING_LEN);
29116970ef27SShenwei Wang 			data += ETH_GSTRING_LEN;
29126970ef27SShenwei Wang 		}
29136970ef27SShenwei Wang 		for (i = 0; i < ARRAY_SIZE(fec_xdp_stat_strs); i++) {
29146970ef27SShenwei Wang 			strncpy(data, fec_xdp_stat_strs[i], ETH_GSTRING_LEN);
29156970ef27SShenwei Wang 			data += ETH_GSTRING_LEN;
29166970ef27SShenwei Wang 		}
29176970ef27SShenwei Wang 		page_pool_ethtool_stats_get_strings(data);
29186970ef27SShenwei Wang 
291938ae92dcSChris Healy 		break;
29206016ba34SOleksij Rempel 	case ETH_SS_TEST:
29216016ba34SOleksij Rempel 		net_selftest_get_strings(data);
29226016ba34SOleksij Rempel 		break;
292338ae92dcSChris Healy 	}
292438ae92dcSChris Healy }
292538ae92dcSChris Healy 
292638ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset)
292738ae92dcSChris Healy {
29286970ef27SShenwei Wang 	int count;
29296970ef27SShenwei Wang 
293038ae92dcSChris Healy 	switch (sset) {
293138ae92dcSChris Healy 	case ETH_SS_STATS:
29326970ef27SShenwei Wang 		count = ARRAY_SIZE(fec_stats) + XDP_STATS_TOTAL;
29336970ef27SShenwei Wang 		count += page_pool_ethtool_stats_get_count();
29346970ef27SShenwei Wang 		return count;
29356970ef27SShenwei Wang 
29366016ba34SOleksij Rempel 	case ETH_SS_TEST:
29376016ba34SOleksij Rempel 		return net_selftest_get_count();
293838ae92dcSChris Healy 	default:
293938ae92dcSChris Healy 		return -EOPNOTSUPP;
294038ae92dcSChris Healy 	}
294138ae92dcSChris Healy }
2942f85de666SNikita Yushchenko 
29432b30842bSAndrew Lunn static void fec_enet_clear_ethtool_stats(struct net_device *dev)
29442b30842bSAndrew Lunn {
29452b30842bSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(dev);
29466970ef27SShenwei Wang 	struct fec_enet_priv_rx_q *rxq;
29476970ef27SShenwei Wang 	int i, j;
29482b30842bSAndrew Lunn 
29492b30842bSAndrew Lunn 	/* Disable MIB statistics counters */
29502b30842bSAndrew Lunn 	writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
29512b30842bSAndrew Lunn 
29522b30842bSAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
29532b30842bSAndrew Lunn 		writel(0, fep->hwp + fec_stats[i].offset);
29542b30842bSAndrew Lunn 
29556970ef27SShenwei Wang 	for (i = fep->num_rx_queues - 1; i >= 0; i--) {
29566970ef27SShenwei Wang 		rxq = fep->rx_queue[i];
29576970ef27SShenwei Wang 		for (j = 0; j < XDP_STATS_TOTAL; j++)
29586970ef27SShenwei Wang 			rxq->stats[j] = 0;
29596970ef27SShenwei Wang 	}
29606970ef27SShenwei Wang 
29612b30842bSAndrew Lunn 	/* Don't disable MIB statistics counters */
29622b30842bSAndrew Lunn 	writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
29632b30842bSAndrew Lunn }
29642b30842bSAndrew Lunn 
2965f85de666SNikita Yushchenko #else	/* !defined(CONFIG_M5272) */
2966f85de666SNikita Yushchenko #define FEC_STATS_SIZE	0
2967f85de666SNikita Yushchenko static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2968f85de666SNikita Yushchenko {
2969f85de666SNikita Yushchenko }
297041e8e404SFabio Estevam 
297141e8e404SFabio Estevam static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
297241e8e404SFabio Estevam {
297341e8e404SFabio Estevam }
2974d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
297538ae92dcSChris Healy 
2976d851b47bSFugang Duan /* ITR clock source is enet system clock (clk_ahb).
2977d851b47bSFugang Duan  * TCTT unit is cycle_ns * 64 cycle
2978d851b47bSFugang Duan  * So, the ICTT value = X us / (cycle_ns * 64)
2979d851b47bSFugang Duan  */
2980d851b47bSFugang Duan static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2981d851b47bSFugang Duan {
2982d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2983d851b47bSFugang Duan 
2984d851b47bSFugang Duan 	return us * (fep->itr_clk_rate / 64000) / 1000;
2985d851b47bSFugang Duan }
2986d851b47bSFugang Duan 
2987d851b47bSFugang Duan /* Set threshold for interrupt coalescing */
2988d851b47bSFugang Duan static void fec_enet_itr_coal_set(struct net_device *ndev)
2989d851b47bSFugang Duan {
2990d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2991d851b47bSFugang Duan 	int rx_itr, tx_itr;
2992d851b47bSFugang Duan 
2993d851b47bSFugang Duan 	/* Must be greater than zero to avoid unpredictable behavior */
2994d851b47bSFugang Duan 	if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2995d851b47bSFugang Duan 	    !fep->tx_time_itr || !fep->tx_pkts_itr)
2996d851b47bSFugang Duan 		return;
2997d851b47bSFugang Duan 
2998d851b47bSFugang Duan 	/* Select enet system clock as Interrupt Coalescing
2999d851b47bSFugang Duan 	 * timer Clock Source
3000d851b47bSFugang Duan 	 */
3001d851b47bSFugang Duan 	rx_itr = FEC_ITR_CLK_SEL;
3002d851b47bSFugang Duan 	tx_itr = FEC_ITR_CLK_SEL;
3003d851b47bSFugang Duan 
3004d851b47bSFugang Duan 	/* set ICFT and ICTT */
3005d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
3006d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
3007d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
3008d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
3009d851b47bSFugang Duan 
3010d851b47bSFugang Duan 	rx_itr |= FEC_ITR_EN;
3011d851b47bSFugang Duan 	tx_itr |= FEC_ITR_EN;
3012d851b47bSFugang Duan 
3013d851b47bSFugang Duan 	writel(tx_itr, fep->hwp + FEC_TXIC0);
3014d851b47bSFugang Duan 	writel(rx_itr, fep->hwp + FEC_RXIC0);
3015471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
3016d851b47bSFugang Duan 		writel(tx_itr, fep->hwp + FEC_TXIC1);
3017d851b47bSFugang Duan 		writel(rx_itr, fep->hwp + FEC_RXIC1);
3018d851b47bSFugang Duan 		writel(tx_itr, fep->hwp + FEC_TXIC2);
3019d851b47bSFugang Duan 		writel(rx_itr, fep->hwp + FEC_RXIC2);
3020d851b47bSFugang Duan 	}
3021ff7566b8SFugang Duan }
3022d851b47bSFugang Duan 
3023f3ccfda1SYufeng Mo static int fec_enet_get_coalesce(struct net_device *ndev,
3024f3ccfda1SYufeng Mo 				 struct ethtool_coalesce *ec,
3025f3ccfda1SYufeng Mo 				 struct kernel_ethtool_coalesce *kernel_coal,
3026f3ccfda1SYufeng Mo 				 struct netlink_ext_ack *extack)
3027d851b47bSFugang Duan {
3028d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3029d851b47bSFugang Duan 
3030ff7566b8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
3031d851b47bSFugang Duan 		return -EOPNOTSUPP;
3032d851b47bSFugang Duan 
3033d851b47bSFugang Duan 	ec->rx_coalesce_usecs = fep->rx_time_itr;
3034d851b47bSFugang Duan 	ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
3035d851b47bSFugang Duan 
3036d851b47bSFugang Duan 	ec->tx_coalesce_usecs = fep->tx_time_itr;
3037d851b47bSFugang Duan 	ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
3038d851b47bSFugang Duan 
3039d851b47bSFugang Duan 	return 0;
3040d851b47bSFugang Duan }
3041d851b47bSFugang Duan 
3042f3ccfda1SYufeng Mo static int fec_enet_set_coalesce(struct net_device *ndev,
3043f3ccfda1SYufeng Mo 				 struct ethtool_coalesce *ec,
3044f3ccfda1SYufeng Mo 				 struct kernel_ethtool_coalesce *kernel_coal,
3045f3ccfda1SYufeng Mo 				 struct netlink_ext_ack *extack)
3046d851b47bSFugang Duan {
3047d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3048517a772cSFabio Estevam 	struct device *dev = &fep->pdev->dev;
3049d851b47bSFugang Duan 	unsigned int cycle;
3050d851b47bSFugang Duan 
3051ff7566b8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
3052d851b47bSFugang Duan 		return -EOPNOTSUPP;
3053d851b47bSFugang Duan 
3054d851b47bSFugang Duan 	if (ec->rx_max_coalesced_frames > 255) {
3055517a772cSFabio Estevam 		dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
3056d851b47bSFugang Duan 		return -EINVAL;
3057d851b47bSFugang Duan 	}
3058d851b47bSFugang Duan 
3059d851b47bSFugang Duan 	if (ec->tx_max_coalesced_frames > 255) {
3060517a772cSFabio Estevam 		dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
3061d851b47bSFugang Duan 		return -EINVAL;
3062d851b47bSFugang Duan 	}
3063d851b47bSFugang Duan 
3064ab14961dSJakub Kicinski 	cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
3065d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
3066517a772cSFabio Estevam 		dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
3067d851b47bSFugang Duan 		return -EINVAL;
3068d851b47bSFugang Duan 	}
3069d851b47bSFugang Duan 
3070ab14961dSJakub Kicinski 	cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
3071d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
3072ab14961dSJakub Kicinski 		dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
3073d851b47bSFugang Duan 		return -EINVAL;
3074d851b47bSFugang Duan 	}
3075d851b47bSFugang Duan 
3076d851b47bSFugang Duan 	fep->rx_time_itr = ec->rx_coalesce_usecs;
3077d851b47bSFugang Duan 	fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
3078d851b47bSFugang Duan 
3079d851b47bSFugang Duan 	fep->tx_time_itr = ec->tx_coalesce_usecs;
3080d851b47bSFugang Duan 	fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
3081d851b47bSFugang Duan 
3082d851b47bSFugang Duan 	fec_enet_itr_coal_set(ndev);
3083d851b47bSFugang Duan 
3084d851b47bSFugang Duan 	return 0;
3085d851b47bSFugang Duan }
3086d851b47bSFugang Duan 
3087b82f8c3fSFugang Duan /* LPI Sleep Ts count base on tx clk (clk_ref).
3088b82f8c3fSFugang Duan  * The lpi sleep cnt value = X us / (cycle_ns).
3089b82f8c3fSFugang Duan  */
3090b82f8c3fSFugang Duan static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
3091b82f8c3fSFugang Duan {
3092b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3093b82f8c3fSFugang Duan 
3094b82f8c3fSFugang Duan 	return us * (fep->clk_ref_rate / 1000) / 1000;
3095b82f8c3fSFugang Duan }
3096b82f8c3fSFugang Duan 
3097b82f8c3fSFugang Duan static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable)
3098b82f8c3fSFugang Duan {
3099b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3100b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
3101b82f8c3fSFugang Duan 	unsigned int sleep_cycle, wake_cycle;
3102b82f8c3fSFugang Duan 	int ret = 0;
3103b82f8c3fSFugang Duan 
3104b82f8c3fSFugang Duan 	if (enable) {
310553243d41SJisheng Zhang 		ret = phy_init_eee(ndev->phydev, false);
3106b82f8c3fSFugang Duan 		if (ret)
3107b82f8c3fSFugang Duan 			return ret;
3108b82f8c3fSFugang Duan 
3109b82f8c3fSFugang Duan 		sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer);
3110b82f8c3fSFugang Duan 		wake_cycle = sleep_cycle;
3111b82f8c3fSFugang Duan 	} else {
3112b82f8c3fSFugang Duan 		sleep_cycle = 0;
3113b82f8c3fSFugang Duan 		wake_cycle = 0;
3114b82f8c3fSFugang Duan 	}
3115b82f8c3fSFugang Duan 
3116b82f8c3fSFugang Duan 	p->tx_lpi_enabled = enable;
3117b82f8c3fSFugang Duan 	p->eee_enabled = enable;
3118b82f8c3fSFugang Duan 	p->eee_active = enable;
3119b82f8c3fSFugang Duan 
3120b82f8c3fSFugang Duan 	writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
3121b82f8c3fSFugang Duan 	writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
3122b82f8c3fSFugang Duan 
3123b82f8c3fSFugang Duan 	return 0;
3124b82f8c3fSFugang Duan }
3125b82f8c3fSFugang Duan 
3126b82f8c3fSFugang Duan static int
3127b82f8c3fSFugang Duan fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
3128b82f8c3fSFugang Duan {
3129b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3130b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
3131b82f8c3fSFugang Duan 
3132b82f8c3fSFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
3133b82f8c3fSFugang Duan 		return -EOPNOTSUPP;
3134b82f8c3fSFugang Duan 
3135b82f8c3fSFugang Duan 	if (!netif_running(ndev))
3136b82f8c3fSFugang Duan 		return -ENETDOWN;
3137b82f8c3fSFugang Duan 
3138b82f8c3fSFugang Duan 	edata->eee_enabled = p->eee_enabled;
3139b82f8c3fSFugang Duan 	edata->eee_active = p->eee_active;
3140b82f8c3fSFugang Duan 	edata->tx_lpi_timer = p->tx_lpi_timer;
3141b82f8c3fSFugang Duan 	edata->tx_lpi_enabled = p->tx_lpi_enabled;
3142b82f8c3fSFugang Duan 
3143b82f8c3fSFugang Duan 	return phy_ethtool_get_eee(ndev->phydev, edata);
3144b82f8c3fSFugang Duan }
3145b82f8c3fSFugang Duan 
3146b82f8c3fSFugang Duan static int
3147b82f8c3fSFugang Duan fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
3148b82f8c3fSFugang Duan {
3149b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
3150b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
3151b82f8c3fSFugang Duan 	int ret = 0;
3152b82f8c3fSFugang Duan 
3153b82f8c3fSFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
3154b82f8c3fSFugang Duan 		return -EOPNOTSUPP;
3155b82f8c3fSFugang Duan 
3156b82f8c3fSFugang Duan 	if (!netif_running(ndev))
3157b82f8c3fSFugang Duan 		return -ENETDOWN;
3158b82f8c3fSFugang Duan 
3159b82f8c3fSFugang Duan 	p->tx_lpi_timer = edata->tx_lpi_timer;
3160b82f8c3fSFugang Duan 
3161b82f8c3fSFugang Duan 	if (!edata->eee_enabled || !edata->tx_lpi_enabled ||
3162b82f8c3fSFugang Duan 	    !edata->tx_lpi_timer)
3163b82f8c3fSFugang Duan 		ret = fec_enet_eee_mode_set(ndev, false);
3164b82f8c3fSFugang Duan 	else
3165b82f8c3fSFugang Duan 		ret = fec_enet_eee_mode_set(ndev, true);
3166b82f8c3fSFugang Duan 
3167b82f8c3fSFugang Duan 	if (ret)
3168b82f8c3fSFugang Duan 		return ret;
3169b82f8c3fSFugang Duan 
3170b82f8c3fSFugang Duan 	return phy_ethtool_set_eee(ndev->phydev, edata);
3171b82f8c3fSFugang Duan }
3172b82f8c3fSFugang Duan 
3173de40ed31SNimrod Andy static void
3174de40ed31SNimrod Andy fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
3175de40ed31SNimrod Andy {
3176de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
3177de40ed31SNimrod Andy 
3178de40ed31SNimrod Andy 	if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
3179de40ed31SNimrod Andy 		wol->supported = WAKE_MAGIC;
3180de40ed31SNimrod Andy 		wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
3181de40ed31SNimrod Andy 	} else {
3182de40ed31SNimrod Andy 		wol->supported = wol->wolopts = 0;
3183de40ed31SNimrod Andy 	}
3184de40ed31SNimrod Andy }
3185de40ed31SNimrod Andy 
3186de40ed31SNimrod Andy static int
3187de40ed31SNimrod Andy fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
3188de40ed31SNimrod Andy {
3189de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
3190de40ed31SNimrod Andy 
3191de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
3192de40ed31SNimrod Andy 		return -EINVAL;
3193de40ed31SNimrod Andy 
3194de40ed31SNimrod Andy 	if (wol->wolopts & ~WAKE_MAGIC)
3195de40ed31SNimrod Andy 		return -EINVAL;
3196de40ed31SNimrod Andy 
3197de40ed31SNimrod Andy 	device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
31980b6f65c7SJoakim Zhang 	if (device_may_wakeup(&ndev->dev))
3199de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
32000b6f65c7SJoakim Zhang 	else
3201de40ed31SNimrod Andy 		fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
3202de40ed31SNimrod Andy 
3203de40ed31SNimrod Andy 	return 0;
3204de40ed31SNimrod Andy }
3205de40ed31SNimrod Andy 
3206793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = {
3207d5e3c87dSJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3208d5e3c87dSJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES,
3209793fc096SFrank Li 	.get_drvinfo		= fec_enet_get_drvinfo,
3210db65f35fSPhilippe Reynes 	.get_regs_len		= fec_enet_get_regs_len,
3211db65f35fSPhilippe Reynes 	.get_regs		= fec_enet_get_regs,
321211d59289SFlorian Fainelli 	.nway_reset		= phy_ethtool_nway_reset,
3213c1d7c48fSRussell King 	.get_link		= ethtool_op_get_link,
3214d851b47bSFugang Duan 	.get_coalesce		= fec_enet_get_coalesce,
3215d851b47bSFugang Duan 	.set_coalesce		= fec_enet_set_coalesce,
321638ae92dcSChris Healy #ifndef CONFIG_M5272
3217c1d7c48fSRussell King 	.get_pauseparam		= fec_enet_get_pauseparam,
3218c1d7c48fSRussell King 	.set_pauseparam		= fec_enet_set_pauseparam,
321938ae92dcSChris Healy 	.get_strings		= fec_enet_get_strings,
3220c1d7c48fSRussell King 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
322138ae92dcSChris Healy 	.get_sset_count		= fec_enet_get_sset_count,
322238ae92dcSChris Healy #endif
3223c1d7c48fSRussell King 	.get_ts_info		= fec_enet_get_ts_info,
3224de40ed31SNimrod Andy 	.get_wol		= fec_enet_get_wol,
3225de40ed31SNimrod Andy 	.set_wol		= fec_enet_set_wol,
3226b82f8c3fSFugang Duan 	.get_eee		= fec_enet_get_eee,
3227b82f8c3fSFugang Duan 	.set_eee		= fec_enet_set_eee,
32289365fbf5SPhilippe Reynes 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
32299365fbf5SPhilippe Reynes 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
32306016ba34SOleksij Rempel 	.self_test		= net_selftest,
3231793fc096SFrank Li };
3232793fc096SFrank Li 
3233793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev)
3234793fc096SFrank Li {
3235793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3236793fc096SFrank Li 	unsigned int i;
32374d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
32384d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
323959d0f746SFrank Li 	unsigned int q;
3240793fc096SFrank Li 
324159d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
324259d0f746SFrank Li 		rxq = fep->rx_queue[q];
324395698ff6SShenwei Wang 		for (i = 0; i < rxq->bd.ring_size; i++)
3244e38553bdSWei Fang 			page_pool_put_full_page(rxq->page_pool, rxq->rx_skb_info[i].page, false);
324595698ff6SShenwei Wang 
32466970ef27SShenwei Wang 		for (i = 0; i < XDP_STATS_TOTAL; i++)
32476970ef27SShenwei Wang 			rxq->stats[i] = 0;
32486970ef27SShenwei Wang 
324995698ff6SShenwei Wang 		if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
325095698ff6SShenwei Wang 			xdp_rxq_info_unreg(&rxq->xdp_rxq);
325195698ff6SShenwei Wang 		page_pool_destroy(rxq->page_pool);
325295698ff6SShenwei Wang 		rxq->page_pool = NULL;
3253793fc096SFrank Li 	}
3254793fc096SFrank Li 
325559d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
325659d0f746SFrank Li 		txq = fep->tx_queue[q];
32577355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
32584d494cdcSFugang Duan 			kfree(txq->tx_bounce[i]);
32594d494cdcSFugang Duan 			txq->tx_bounce[i] = NULL;
326020f79739SWei Fang 
3261af6f4791SWei Fang 			if (!txq->tx_buf[i].buf_p) {
3262af6f4791SWei Fang 				txq->tx_buf[i].type = FEC_TXBUF_T_SKB;
3263af6f4791SWei Fang 				continue;
326420f79739SWei Fang 			}
326520f79739SWei Fang 
3266af6f4791SWei Fang 			if (txq->tx_buf[i].type == FEC_TXBUF_T_SKB) {
3267af6f4791SWei Fang 				dev_kfree_skb(txq->tx_buf[i].buf_p);
3268af6f4791SWei Fang 			} else if (txq->tx_buf[i].type == FEC_TXBUF_T_XDP_NDO) {
3269af6f4791SWei Fang 				xdp_return_frame(txq->tx_buf[i].buf_p);
3270af6f4791SWei Fang 			} else {
3271af6f4791SWei Fang 				struct page *page = txq->tx_buf[i].buf_p;
3272af6f4791SWei Fang 
3273af6f4791SWei Fang 				page_pool_put_page(page->pp, page, 0, false);
327420f79739SWei Fang 			}
3275af6f4791SWei Fang 
3276af6f4791SWei Fang 			txq->tx_buf[i].buf_p = NULL;
3277af6f4791SWei Fang 			txq->tx_buf[i].type = FEC_TXBUF_T_SKB;
32788b7c9efaSRussell King 		}
3279793fc096SFrank Li 	}
328059d0f746SFrank Li }
3281793fc096SFrank Li 
328259d0f746SFrank Li static void fec_enet_free_queue(struct net_device *ndev)
328359d0f746SFrank Li {
328459d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
328559d0f746SFrank Li 	int i;
328659d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
328759d0f746SFrank Li 
328859d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
328959d0f746SFrank Li 		if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
329059d0f746SFrank Li 			txq = fep->tx_queue[i];
329194920128SFugang Duan 			dma_free_coherent(&fep->pdev->dev,
32927355f276STroy Kisky 					  txq->bd.ring_size * TSO_HEADER_SIZE,
329359d0f746SFrank Li 					  txq->tso_hdrs,
329459d0f746SFrank Li 					  txq->tso_hdrs_dma);
329559d0f746SFrank Li 		}
329659d0f746SFrank Li 
329759d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
329859d0f746SFrank Li 		kfree(fep->rx_queue[i]);
329959d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
330059d0f746SFrank Li 		kfree(fep->tx_queue[i]);
330159d0f746SFrank Li }
330259d0f746SFrank Li 
330359d0f746SFrank Li static int fec_enet_alloc_queue(struct net_device *ndev)
330459d0f746SFrank Li {
330559d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
330659d0f746SFrank Li 	int i;
330759d0f746SFrank Li 	int ret = 0;
330859d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
330959d0f746SFrank Li 
331059d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
331159d0f746SFrank Li 		txq = kzalloc(sizeof(*txq), GFP_KERNEL);
331259d0f746SFrank Li 		if (!txq) {
331359d0f746SFrank Li 			ret = -ENOMEM;
331459d0f746SFrank Li 			goto alloc_failed;
331559d0f746SFrank Li 		}
331659d0f746SFrank Li 
331759d0f746SFrank Li 		fep->tx_queue[i] = txq;
33187355f276STroy Kisky 		txq->bd.ring_size = TX_RING_SIZE;
33197355f276STroy Kisky 		fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
332059d0f746SFrank Li 
332159d0f746SFrank Li 		txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
332256b3c6baSWei Fang 		txq->tx_wake_threshold = FEC_MAX_SKB_DESCS + 2 * MAX_SKB_FRAGS;
332359d0f746SFrank Li 
332494920128SFugang Duan 		txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
33257355f276STroy Kisky 					txq->bd.ring_size * TSO_HEADER_SIZE,
332659d0f746SFrank Li 					&txq->tso_hdrs_dma,
332759d0f746SFrank Li 					GFP_KERNEL);
332859d0f746SFrank Li 		if (!txq->tso_hdrs) {
332959d0f746SFrank Li 			ret = -ENOMEM;
333059d0f746SFrank Li 			goto alloc_failed;
333159d0f746SFrank Li 		}
333259d0f746SFrank Li 	}
333359d0f746SFrank Li 
333459d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
333559d0f746SFrank Li 		fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
333659d0f746SFrank Li 					   GFP_KERNEL);
333759d0f746SFrank Li 		if (!fep->rx_queue[i]) {
333859d0f746SFrank Li 			ret = -ENOMEM;
333959d0f746SFrank Li 			goto alloc_failed;
334059d0f746SFrank Li 		}
334159d0f746SFrank Li 
33427355f276STroy Kisky 		fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
33437355f276STroy Kisky 		fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
334459d0f746SFrank Li 	}
334559d0f746SFrank Li 	return ret;
334659d0f746SFrank Li 
334759d0f746SFrank Li alloc_failed:
334859d0f746SFrank Li 	fec_enet_free_queue(ndev);
334959d0f746SFrank Li 	return ret;
335059d0f746SFrank Li }
335159d0f746SFrank Li 
335259d0f746SFrank Li static int
335359d0f746SFrank Li fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
3354793fc096SFrank Li {
3355793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
33564d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
335795698ff6SShenwei Wang 	dma_addr_t phys_addr;
335895698ff6SShenwei Wang 	struct bufdesc	*bdp;
335995698ff6SShenwei Wang 	struct page *page;
336095698ff6SShenwei Wang 	int i, err;
3361793fc096SFrank Li 
336259d0f746SFrank Li 	rxq = fep->rx_queue[queue];
33637355f276STroy Kisky 	bdp = rxq->bd.base;
3364793fc096SFrank Li 
336595698ff6SShenwei Wang 	err = fec_enet_create_page_pool(fep, rxq, rxq->bd.ring_size);
336695698ff6SShenwei Wang 	if (err < 0) {
336795698ff6SShenwei Wang 		netdev_err(ndev, "%s failed queue %d (%d)\n", __func__, queue, err);
336895698ff6SShenwei Wang 		return err;
3369d842a31fSDuan Fugang-B38611 	}
3370730ee360SRussell King 
337195698ff6SShenwei Wang 	for (i = 0; i < rxq->bd.ring_size; i++) {
337295698ff6SShenwei Wang 		page = page_pool_dev_alloc_pages(rxq->page_pool);
337395698ff6SShenwei Wang 		if (!page)
337495698ff6SShenwei Wang 			goto err_alloc;
337595698ff6SShenwei Wang 
337695698ff6SShenwei Wang 		phys_addr = page_pool_get_dma_addr(page) + FEC_ENET_XDP_HEADROOM;
337795698ff6SShenwei Wang 		bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
337895698ff6SShenwei Wang 
337995698ff6SShenwei Wang 		rxq->rx_skb_info[i].page = page;
338095698ff6SShenwei Wang 		rxq->rx_skb_info[i].offset = FEC_ENET_XDP_HEADROOM;
33815cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
3382793fc096SFrank Li 
3383793fc096SFrank Li 		if (fep->bufdesc_ex) {
3384793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
33855cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
3386793fc096SFrank Li 		}
3387793fc096SFrank Li 
33887355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
3389793fc096SFrank Li 	}
3390793fc096SFrank Li 
3391793fc096SFrank Li 	/* Set the last buffer to wrap. */
33927355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
33935cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
339459d0f746SFrank Li 	return 0;
3395793fc096SFrank Li 
339659d0f746SFrank Li  err_alloc:
339759d0f746SFrank Li 	fec_enet_free_buffers(ndev);
339859d0f746SFrank Li 	return -ENOMEM;
339959d0f746SFrank Li }
340059d0f746SFrank Li 
340159d0f746SFrank Li static int
340259d0f746SFrank Li fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
340359d0f746SFrank Li {
340459d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
340559d0f746SFrank Li 	unsigned int i;
340659d0f746SFrank Li 	struct bufdesc  *bdp;
340759d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
340859d0f746SFrank Li 
340959d0f746SFrank Li 	txq = fep->tx_queue[queue];
34107355f276STroy Kisky 	bdp = txq->bd.base;
34117355f276STroy Kisky 	for (i = 0; i < txq->bd.ring_size; i++) {
34124d494cdcSFugang Duan 		txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
34134d494cdcSFugang Duan 		if (!txq->tx_bounce[i])
3414ffdce2ccSRussell King 			goto err_alloc;
3415793fc096SFrank Li 
34165cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(0);
34175cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
3418793fc096SFrank Li 
3419793fc096SFrank Li 		if (fep->bufdesc_ex) {
3420793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
34215cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
3422793fc096SFrank Li 		}
3423793fc096SFrank Li 
34247355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3425793fc096SFrank Li 	}
3426793fc096SFrank Li 
3427793fc096SFrank Li 	/* Set the last buffer to wrap. */
34287355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
34295cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3430793fc096SFrank Li 
3431793fc096SFrank Li 	return 0;
3432ffdce2ccSRussell King 
3433ffdce2ccSRussell King  err_alloc:
3434ffdce2ccSRussell King 	fec_enet_free_buffers(ndev);
3435ffdce2ccSRussell King 	return -ENOMEM;
3436793fc096SFrank Li }
3437793fc096SFrank Li 
343859d0f746SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev)
343959d0f746SFrank Li {
344059d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
344159d0f746SFrank Li 	unsigned int i;
344259d0f746SFrank Li 
344359d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
344459d0f746SFrank Li 		if (fec_enet_alloc_rxq_buffers(ndev, i))
344559d0f746SFrank Li 			return -ENOMEM;
344659d0f746SFrank Li 
344759d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
344859d0f746SFrank Li 		if (fec_enet_alloc_txq_buffers(ndev, i))
344959d0f746SFrank Li 			return -ENOMEM;
345059d0f746SFrank Li 	return 0;
345159d0f746SFrank Li }
345259d0f746SFrank Li 
3453793fc096SFrank Li static int
3454793fc096SFrank Li fec_enet_open(struct net_device *ndev)
3455793fc096SFrank Li {
3456793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3457793fc096SFrank Li 	int ret;
34581b0a83acSRichard Leitner 	bool reset_again;
3459793fc096SFrank Li 
3460da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(&fep->pdev->dev);
3461b0c6ce24SFabio Estevam 	if (ret < 0)
34628fff755eSAndrew Lunn 		return ret;
34638fff755eSAndrew Lunn 
34645bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&fep->pdev->dev);
3465e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
3466e8fcfcd5SNimrod Andy 	if (ret)
34678fff755eSAndrew Lunn 		goto clk_enable;
3468e8fcfcd5SNimrod Andy 
34691b0a83acSRichard Leitner 	/* During the first fec_enet_open call the PHY isn't probed at this
34701b0a83acSRichard Leitner 	 * point. Therefore the phy_reset_after_clk_enable() call within
34711b0a83acSRichard Leitner 	 * fec_enet_clk_enable() fails. As we need this reset in order to be
34721b0a83acSRichard Leitner 	 * sure the PHY is working correctly we check if we need to reset again
34731b0a83acSRichard Leitner 	 * later when the PHY is probed
34741b0a83acSRichard Leitner 	 */
34751b0a83acSRichard Leitner 	if (ndev->phydev && ndev->phydev->drv)
34761b0a83acSRichard Leitner 		reset_again = false;
34771b0a83acSRichard Leitner 	else
34781b0a83acSRichard Leitner 		reset_again = true;
34791b0a83acSRichard Leitner 
3480793fc096SFrank Li 	/* I should reset the ring buffers here, but I don't yet know
3481793fc096SFrank Li 	 * a simple way to do that.
3482793fc096SFrank Li 	 */
3483793fc096SFrank Li 
3484793fc096SFrank Li 	ret = fec_enet_alloc_buffers(ndev);
3485793fc096SFrank Li 	if (ret)
3486681d2421SFabio Estevam 		goto err_enet_alloc;
3487793fc096SFrank Li 
348855dd2753SNimrod Andy 	/* Init MAC prior to mii bus probe */
348955dd2753SNimrod Andy 	fec_restart(ndev);
349055dd2753SNimrod Andy 
34911b0a83acSRichard Leitner 	/* Call phy_reset_after_clk_enable() again if it failed during
34921b0a83acSRichard Leitner 	 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
34931b0a83acSRichard Leitner 	 */
34941b0a83acSRichard Leitner 	if (reset_again)
349564a632daSMarek Vasut 		fec_enet_phy_reset_after_clk_enable(ndev);
34961b0a83acSRichard Leitner 
34970da1ccbbSMarek Vasut 	/* Probe and connect to PHY when open the interface */
34980da1ccbbSMarek Vasut 	ret = fec_enet_mii_probe(ndev);
34990da1ccbbSMarek Vasut 	if (ret)
35000da1ccbbSMarek Vasut 		goto err_enet_mii_probe;
3501681d2421SFabio Estevam 
3502681d2421SFabio Estevam 	if (fep->quirks & FEC_QUIRK_ERR006687)
3503793fc096SFrank Li 		imx6q_cpuidle_fec_irqs_used();
3504793fc096SFrank Li 
35057d650df9SWei Fang 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
35067d650df9SWei Fang 		cpu_latency_qos_add_request(&fep->pm_qos_req, 0);
35077d650df9SWei Fang 
3508793fc096SFrank Li 	napi_enable(&fep->napi);
3509793fc096SFrank Li 	phy_start(ndev->phydev);
3510793fc096SFrank Li 	netif_tx_start_all_queues(ndev);
3511793fc096SFrank Li 
3512793fc096SFrank Li 	device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
3513793fc096SFrank Li 				 FEC_WOL_FLAG_ENABLE);
3514793fc096SFrank Li 
3515793fc096SFrank Li 	return 0;
3516793fc096SFrank Li 
3517793fc096SFrank Li err_enet_mii_probe:
3518793fc096SFrank Li 	fec_enet_free_buffers(ndev);
3519793fc096SFrank Li err_enet_alloc:
3520793fc096SFrank Li 	fec_enet_clk_enable(ndev, false);
3521793fc096SFrank Li clk_enable:
3522793fc096SFrank Li 	pm_runtime_mark_last_busy(&fep->pdev->dev);
3523793fc096SFrank Li 	pm_runtime_put_autosuspend(&fep->pdev->dev);
3524793fc096SFrank Li 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3525793fc096SFrank Li 	return ret;
3526d76cfae9SRussell King }
3527d76cfae9SRussell King 
352831a6de34SRussell King static int
3529793fc096SFrank Li fec_enet_close(struct net_device *ndev)
3530b49cd504SRussell King {
3531793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
353231a6de34SRussell King 
353345f5c327SPhilippe Reynes 	phy_stop(ndev->phydev);
3534793fc096SFrank Li 
3535793fc096SFrank Li 	if (netif_device_present(ndev)) {
3536793fc096SFrank Li 		napi_disable(&fep->napi);
3537793fc096SFrank Li 		netif_tx_disable(ndev);
3538793fc096SFrank Li 		fec_stop(ndev);
3539793fc096SFrank Li 	}
3540793fc096SFrank Li 
354145f5c327SPhilippe Reynes 	phy_disconnect(ndev->phydev);
3542793fc096SFrank Li 
354329380905SLucas Stach 	if (fep->quirks & FEC_QUIRK_ERR006687)
354429380905SLucas Stach 		imx6q_cpuidle_fec_irqs_unused();
354529380905SLucas Stach 
354680cca775SNikita Yushchenko 	fec_enet_update_ethtool_stats(ndev);
354780cca775SNikita Yushchenko 
3548e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
35497d650df9SWei Fang 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
35507d650df9SWei Fang 		cpu_latency_qos_remove_request(&fep->pm_qos_req);
35517d650df9SWei Fang 
35525bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
35538fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&fep->pdev->dev);
35548fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&fep->pdev->dev);
35558fff755eSAndrew Lunn 
3556793fc096SFrank Li 	fec_enet_free_buffers(ndev);
3557793fc096SFrank Li 
3558793fc096SFrank Li 	return 0;
3559793fc096SFrank Li }
3560793fc096SFrank Li 
3561793fc096SFrank Li /* Set or clear the multicast filter for this adaptor.
3562793fc096SFrank Li  * Skeleton taken from sunlance driver.
3563793fc096SFrank Li  * The CPM Ethernet implementation allows Multicast as well as individual
3564793fc096SFrank Li  * MAC address filtering.  Some of the drivers check to make sure it is
3565793fc096SFrank Li  * a group multicast address, and discard those that are not.  I guess I
3566793fc096SFrank Li  * will do the same for now, but just remove the test if you want
3567793fc096SFrank Li  * individual filtering as well (do the upper net layers want or support
3568793fc096SFrank Li  * this kind of feature?).
3569793fc096SFrank Li  */
3570793fc096SFrank Li 
35716176e89cSJiri Kosina #define FEC_HASH_BITS	6		/* #bits in hash */
3572793fc096SFrank Li 
3573793fc096SFrank Li static void set_multicast_list(struct net_device *ndev)
3574793fc096SFrank Li {
3575793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3576793fc096SFrank Li 	struct netdev_hw_addr *ha;
357716f6e983SKrzysztof Kozlowski 	unsigned int crc, tmp;
3578793fc096SFrank Li 	unsigned char hash;
357901f8902bSRui Sousa 	unsigned int hash_high = 0, hash_low = 0;
3580793fc096SFrank Li 
3581793fc096SFrank Li 	if (ndev->flags & IFF_PROMISC) {
3582793fc096SFrank Li 		tmp = readl(fep->hwp + FEC_R_CNTRL);
3583793fc096SFrank Li 		tmp |= 0x8;
3584793fc096SFrank Li 		writel(tmp, fep->hwp + FEC_R_CNTRL);
3585793fc096SFrank Li 		return;
3586793fc096SFrank Li 	}
3587793fc096SFrank Li 
3588793fc096SFrank Li 	tmp = readl(fep->hwp + FEC_R_CNTRL);
3589793fc096SFrank Li 	tmp &= ~0x8;
3590793fc096SFrank Li 	writel(tmp, fep->hwp + FEC_R_CNTRL);
3591793fc096SFrank Li 
3592793fc096SFrank Li 	if (ndev->flags & IFF_ALLMULTI) {
3593793fc096SFrank Li 		/* Catch all multicast addresses, so set the
3594793fc096SFrank Li 		 * filter to all 1's
3595793fc096SFrank Li 		 */
3596793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3597793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3598793fc096SFrank Li 
3599793fc096SFrank Li 		return;
3600793fc096SFrank Li 	}
3601793fc096SFrank Li 
360201f8902bSRui Sousa 	/* Add the addresses in hash register */
3603793fc096SFrank Li 	netdev_for_each_mc_addr(ha, ndev) {
3604793fc096SFrank Li 		/* calculate crc32 value of mac address */
360516f6e983SKrzysztof Kozlowski 		crc = ether_crc_le(ndev->addr_len, ha->addr);
3606793fc096SFrank Li 
36076176e89cSJiri Kosina 		/* only upper 6 bits (FEC_HASH_BITS) are used
3608981a0547SPeter Meerwald-Stadler 		 * which point to specific bit in the hash registers
3609793fc096SFrank Li 		 */
36106176e89cSJiri Kosina 		hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
3611793fc096SFrank Li 
361201f8902bSRui Sousa 		if (hash > 31)
361301f8902bSRui Sousa 			hash_high |= 1 << (hash - 32);
361401f8902bSRui Sousa 		else
361501f8902bSRui Sousa 			hash_low |= 1 << hash;
3616793fc096SFrank Li 	}
361701f8902bSRui Sousa 
361801f8902bSRui Sousa 	writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
361901f8902bSRui Sousa 	writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3620793fc096SFrank Li }
3621793fc096SFrank Li 
3622793fc096SFrank Li /* Set a MAC change in hardware. */
3623793fc096SFrank Li static int
3624793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p)
3625793fc096SFrank Li {
3626793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3627793fc096SFrank Li 	struct sockaddr *addr = p;
3628793fc096SFrank Li 
362944934facSLucas Stach 	if (addr) {
3630793fc096SFrank Li 		if (!is_valid_ether_addr(addr->sa_data))
3631793fc096SFrank Li 			return -EADDRNOTAVAIL;
3632a05e4c0aSJakub Kicinski 		eth_hw_addr_set(ndev, addr->sa_data);
363344934facSLucas Stach 	}
3634793fc096SFrank Li 
36359638d19eSNimrod Andy 	/* Add netif status check here to avoid system hang in below case:
36369638d19eSNimrod Andy 	 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
36379638d19eSNimrod Andy 	 * After ethx down, fec all clocks are gated off and then register
36389638d19eSNimrod Andy 	 * access causes system hang.
36399638d19eSNimrod Andy 	 */
36409638d19eSNimrod Andy 	if (!netif_running(ndev))
36419638d19eSNimrod Andy 		return 0;
36429638d19eSNimrod Andy 
3643793fc096SFrank Li 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3644793fc096SFrank Li 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3645793fc096SFrank Li 		fep->hwp + FEC_ADDR_LOW);
3646793fc096SFrank Li 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3647793fc096SFrank Li 		fep->hwp + FEC_ADDR_HIGH);
3648793fc096SFrank Li 	return 0;
3649793fc096SFrank Li }
3650793fc096SFrank Li 
3651793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3652793fc096SFrank Li /**
3653793fc096SFrank Li  * fec_poll_controller - FEC Poll controller function
3654793fc096SFrank Li  * @dev: The FEC network adapter
3655793fc096SFrank Li  *
3656793fc096SFrank Li  * Polled functionality used by netconsole and others in non interrupt mode
3657793fc096SFrank Li  *
3658793fc096SFrank Li  */
3659793fc096SFrank Li static void fec_poll_controller(struct net_device *dev)
3660793fc096SFrank Li {
3661793fc096SFrank Li 	int i;
3662793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(dev);
3663793fc096SFrank Li 
3664793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
3665793fc096SFrank Li 		if (fep->irq[i] > 0) {
3666793fc096SFrank Li 			disable_irq(fep->irq[i]);
3667793fc096SFrank Li 			fec_enet_interrupt(fep->irq[i], dev);
3668793fc096SFrank Li 			enable_irq(fep->irq[i]);
3669793fc096SFrank Li 		}
3670793fc096SFrank Li 	}
3671793fc096SFrank Li }
3672793fc096SFrank Li #endif
3673793fc096SFrank Li 
36745bc26726SNimrod Andy static inline void fec_enet_set_netdev_features(struct net_device *netdev,
36754c09eed9SJim Baxter 	netdev_features_t features)
36764c09eed9SJim Baxter {
36774c09eed9SJim Baxter 	struct fec_enet_private *fep = netdev_priv(netdev);
36784c09eed9SJim Baxter 	netdev_features_t changed = features ^ netdev->features;
36794c09eed9SJim Baxter 
36804c09eed9SJim Baxter 	netdev->features = features;
36814c09eed9SJim Baxter 
36824c09eed9SJim Baxter 	/* Receive checksum has been changed */
36834c09eed9SJim Baxter 	if (changed & NETIF_F_RXCSUM) {
36844c09eed9SJim Baxter 		if (features & NETIF_F_RXCSUM)
36854c09eed9SJim Baxter 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
36864c09eed9SJim Baxter 		else
36874c09eed9SJim Baxter 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
36888506fa1dSRussell King 	}
36895bc26726SNimrod Andy }
36904c09eed9SJim Baxter 
36915bc26726SNimrod Andy static int fec_set_features(struct net_device *netdev,
36925bc26726SNimrod Andy 	netdev_features_t features)
36935bc26726SNimrod Andy {
36945bc26726SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
36955bc26726SNimrod Andy 	netdev_features_t changed = features ^ netdev->features;
36965bc26726SNimrod Andy 
36975b40f709SFabio Estevam 	if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
36985bc26726SNimrod Andy 		napi_disable(&fep->napi);
36995bc26726SNimrod Andy 		netif_tx_lock_bh(netdev);
37005bc26726SNimrod Andy 		fec_stop(netdev);
37015bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
3702ef83337dSRussell King 		fec_restart(netdev);
37034d494cdcSFugang Duan 		netif_tx_wake_all_queues(netdev);
37046af42d42SRussell King 		netif_tx_unlock_bh(netdev);
3705dbc64a8eSRussell King 		napi_enable(&fep->napi);
37065bc26726SNimrod Andy 	} else {
37075bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
37084c09eed9SJim Baxter 	}
37094c09eed9SJim Baxter 
37104c09eed9SJim Baxter 	return 0;
37114c09eed9SJim Baxter }
37124c09eed9SJim Baxter 
371352c4a1a8SFugang Duan static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb)
371452c4a1a8SFugang Duan {
371552c4a1a8SFugang Duan 	struct vlan_ethhdr *vhdr;
371652c4a1a8SFugang Duan 	unsigned short vlan_TCI = 0;
371752c4a1a8SFugang Duan 
371852c4a1a8SFugang Duan 	if (skb->protocol == htons(ETH_P_ALL)) {
371952c4a1a8SFugang Duan 		vhdr = (struct vlan_ethhdr *)(skb->data);
372052c4a1a8SFugang Duan 		vlan_TCI = ntohs(vhdr->h_vlan_TCI);
372152c4a1a8SFugang Duan 	}
372252c4a1a8SFugang Duan 
372352c4a1a8SFugang Duan 	return vlan_TCI;
372452c4a1a8SFugang Duan }
372552c4a1a8SFugang Duan 
372652c4a1a8SFugang Duan static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
372752c4a1a8SFugang Duan 				 struct net_device *sb_dev)
372852c4a1a8SFugang Duan {
372952c4a1a8SFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
373052c4a1a8SFugang Duan 	u16 vlan_tag;
373152c4a1a8SFugang Duan 
373252c4a1a8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
373352c4a1a8SFugang Duan 		return netdev_pick_tx(ndev, skb, NULL);
373452c4a1a8SFugang Duan 
373552c4a1a8SFugang Duan 	vlan_tag = fec_enet_get_raw_vlan_tci(skb);
373652c4a1a8SFugang Duan 	if (!vlan_tag)
373752c4a1a8SFugang Duan 		return vlan_tag;
373852c4a1a8SFugang Duan 
373952c4a1a8SFugang Duan 	return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
374052c4a1a8SFugang Duan }
374152c4a1a8SFugang Duan 
37426d6b39f1SShenwei Wang static int fec_enet_bpf(struct net_device *dev, struct netdev_bpf *bpf)
37436d6b39f1SShenwei Wang {
37446d6b39f1SShenwei Wang 	struct fec_enet_private *fep = netdev_priv(dev);
37456d6b39f1SShenwei Wang 	bool is_run = netif_running(dev);
37466d6b39f1SShenwei Wang 	struct bpf_prog *old_prog;
37476d6b39f1SShenwei Wang 
37486d6b39f1SShenwei Wang 	switch (bpf->command) {
37496d6b39f1SShenwei Wang 	case XDP_SETUP_PROG:
37506c8fae0cSShenwei Wang 		/* No need to support the SoCs that require to
37516c8fae0cSShenwei Wang 		 * do the frame swap because the performance wouldn't be
37526c8fae0cSShenwei Wang 		 * better than the skb mode.
37536c8fae0cSShenwei Wang 		 */
37546c8fae0cSShenwei Wang 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
37556c8fae0cSShenwei Wang 			return -EOPNOTSUPP;
37566c8fae0cSShenwei Wang 
3757be7ecbe7SWei Fang 		if (!bpf->prog)
3758be7ecbe7SWei Fang 			xdp_features_clear_redirect_target(dev);
3759be7ecbe7SWei Fang 
37606d6b39f1SShenwei Wang 		if (is_run) {
37616d6b39f1SShenwei Wang 			napi_disable(&fep->napi);
37626d6b39f1SShenwei Wang 			netif_tx_disable(dev);
37636d6b39f1SShenwei Wang 		}
37646d6b39f1SShenwei Wang 
37656d6b39f1SShenwei Wang 		old_prog = xchg(&fep->xdp_prog, bpf->prog);
3766be7ecbe7SWei Fang 		if (old_prog)
3767be7ecbe7SWei Fang 			bpf_prog_put(old_prog);
3768be7ecbe7SWei Fang 
37696d6b39f1SShenwei Wang 		fec_restart(dev);
37706d6b39f1SShenwei Wang 
37716d6b39f1SShenwei Wang 		if (is_run) {
37726d6b39f1SShenwei Wang 			napi_enable(&fep->napi);
37736d6b39f1SShenwei Wang 			netif_tx_start_all_queues(dev);
37746d6b39f1SShenwei Wang 		}
37756d6b39f1SShenwei Wang 
3776be7ecbe7SWei Fang 		if (bpf->prog)
3777be7ecbe7SWei Fang 			xdp_features_set_redirect_target(dev, false);
37786d6b39f1SShenwei Wang 
37796d6b39f1SShenwei Wang 		return 0;
37806d6b39f1SShenwei Wang 
37816d6b39f1SShenwei Wang 	case XDP_SETUP_XSK_POOL:
37826d6b39f1SShenwei Wang 		return -EOPNOTSUPP;
37836d6b39f1SShenwei Wang 
37846d6b39f1SShenwei Wang 	default:
37856d6b39f1SShenwei Wang 		return -EOPNOTSUPP;
37866d6b39f1SShenwei Wang 	}
37876d6b39f1SShenwei Wang }
37886d6b39f1SShenwei Wang 
37896d6b39f1SShenwei Wang static int
37906c8fae0cSShenwei Wang fec_enet_xdp_get_tx_queue(struct fec_enet_private *fep, int index)
37916d6b39f1SShenwei Wang {
37926d6b39f1SShenwei Wang 	if (unlikely(index < 0))
37936c8fae0cSShenwei Wang 		return 0;
37946d6b39f1SShenwei Wang 
37956c8fae0cSShenwei Wang 	return (index % fep->num_tx_queues);
37966d6b39f1SShenwei Wang }
37976d6b39f1SShenwei Wang 
37986d6b39f1SShenwei Wang static int fec_enet_txq_xmit_frame(struct fec_enet_private *fep,
37996d6b39f1SShenwei Wang 				   struct fec_enet_priv_tx_q *txq,
3800af6f4791SWei Fang 				   void *frame, u32 dma_sync_len,
3801af6f4791SWei Fang 				   bool ndo_xmit)
38026d6b39f1SShenwei Wang {
38036d6b39f1SShenwei Wang 	unsigned int index, status, estatus;
3804bc638eabSWei Fang 	struct bufdesc *bdp;
38056d6b39f1SShenwei Wang 	dma_addr_t dma_addr;
38066d6b39f1SShenwei Wang 	int entries_free;
3807af6f4791SWei Fang 	u16 frame_len;
38086d6b39f1SShenwei Wang 
38096d6b39f1SShenwei Wang 	entries_free = fec_enet_get_free_txdesc_num(txq);
38106d6b39f1SShenwei Wang 	if (entries_free < MAX_SKB_FRAGS + 1) {
381184a10947SWei Fang 		netdev_err_once(fep->netdev, "NOT enough BD for SG!\n");
3812144470c8SShenwei Wang 		return -EBUSY;
38136d6b39f1SShenwei Wang 	}
38146d6b39f1SShenwei Wang 
38156d6b39f1SShenwei Wang 	/* Fill in a Tx ring entry */
38166d6b39f1SShenwei Wang 	bdp = txq->bd.cur;
38176d6b39f1SShenwei Wang 	status = fec16_to_cpu(bdp->cbd_sc);
38186d6b39f1SShenwei Wang 	status &= ~BD_ENET_TX_STATS;
38196d6b39f1SShenwei Wang 
38206d6b39f1SShenwei Wang 	index = fec_enet_get_bd_index(bdp, &txq->bd);
38216d6b39f1SShenwei Wang 
3822f601899eSWei Fang 	if (ndo_xmit) {
3823af6f4791SWei Fang 		struct xdp_frame *xdpf = frame;
3824af6f4791SWei Fang 
3825af6f4791SWei Fang 		dma_addr = dma_map_single(&fep->pdev->dev, xdpf->data,
3826af6f4791SWei Fang 					  xdpf->len, DMA_TO_DEVICE);
38276d6b39f1SShenwei Wang 		if (dma_mapping_error(&fep->pdev->dev, dma_addr))
3828144470c8SShenwei Wang 			return -ENOMEM;
38296d6b39f1SShenwei Wang 
3830af6f4791SWei Fang 		frame_len = xdpf->len;
3831af6f4791SWei Fang 		txq->tx_buf[index].buf_p = xdpf;
3832f601899eSWei Fang 		txq->tx_buf[index].type = FEC_TXBUF_T_XDP_NDO;
3833f601899eSWei Fang 	} else {
3834af6f4791SWei Fang 		struct xdp_buff *xdpb = frame;
3835af6f4791SWei Fang 		struct page *page;
3836f601899eSWei Fang 
3837af6f4791SWei Fang 		page = virt_to_page(xdpb->data);
3838af6f4791SWei Fang 		dma_addr = page_pool_get_dma_addr(page) +
3839af6f4791SWei Fang 			   (xdpb->data - xdpb->data_hard_start);
3840f601899eSWei Fang 		dma_sync_single_for_device(&fep->pdev->dev, dma_addr,
3841f601899eSWei Fang 					   dma_sync_len, DMA_BIDIRECTIONAL);
3842af6f4791SWei Fang 		frame_len = xdpb->data_end - xdpb->data;
3843af6f4791SWei Fang 		txq->tx_buf[index].buf_p = page;
3844f601899eSWei Fang 		txq->tx_buf[index].type = FEC_TXBUF_T_XDP_TX;
3845f601899eSWei Fang 	}
3846f601899eSWei Fang 
38476d6b39f1SShenwei Wang 	status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
38486d6b39f1SShenwei Wang 	if (fep->bufdesc_ex)
38496d6b39f1SShenwei Wang 		estatus = BD_ENET_TX_INT;
38506d6b39f1SShenwei Wang 
38516d6b39f1SShenwei Wang 	bdp->cbd_bufaddr = cpu_to_fec32(dma_addr);
3852af6f4791SWei Fang 	bdp->cbd_datlen = cpu_to_fec16(frame_len);
38536d6b39f1SShenwei Wang 
38546d6b39f1SShenwei Wang 	if (fep->bufdesc_ex) {
38556d6b39f1SShenwei Wang 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
38566d6b39f1SShenwei Wang 
38576d6b39f1SShenwei Wang 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
38586d6b39f1SShenwei Wang 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
38596d6b39f1SShenwei Wang 
38606d6b39f1SShenwei Wang 		ebdp->cbd_bdu = 0;
38616d6b39f1SShenwei Wang 		ebdp->cbd_esc = cpu_to_fec32(estatus);
38626d6b39f1SShenwei Wang 	}
38636d6b39f1SShenwei Wang 
38649025944fSShenwei Wang 	/* Make sure the updates to rest of the descriptor are performed before
38659025944fSShenwei Wang 	 * transferring ownership.
38669025944fSShenwei Wang 	 */
38679025944fSShenwei Wang 	dma_wmb();
38689025944fSShenwei Wang 
38696d6b39f1SShenwei Wang 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
38706d6b39f1SShenwei Wang 	 * it's the last BD of the frame, and to put the CRC on the end.
38716d6b39f1SShenwei Wang 	 */
38726d6b39f1SShenwei Wang 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
38736d6b39f1SShenwei Wang 	bdp->cbd_sc = cpu_to_fec16(status);
38746d6b39f1SShenwei Wang 
38756d6b39f1SShenwei Wang 	/* If this was the last BD in the ring, start at the beginning again. */
3876bc638eabSWei Fang 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
38776d6b39f1SShenwei Wang 
38789025944fSShenwei Wang 	/* Make sure the update to bdp are performed before txq->bd.cur. */
38799025944fSShenwei Wang 	dma_wmb();
38809025944fSShenwei Wang 
38816d6b39f1SShenwei Wang 	txq->bd.cur = bdp;
38826d6b39f1SShenwei Wang 
38839025944fSShenwei Wang 	/* Trigger transmission start */
38849025944fSShenwei Wang 	writel(0, txq->bd.reg_desc_active);
38859025944fSShenwei Wang 
38866d6b39f1SShenwei Wang 	return 0;
38876d6b39f1SShenwei Wang }
38886d6b39f1SShenwei Wang 
3889f601899eSWei Fang static int fec_enet_xdp_tx_xmit(struct fec_enet_private *fep,
3890f601899eSWei Fang 				int cpu, struct xdp_buff *xdp,
3891f601899eSWei Fang 				u32 dma_sync_len)
3892f601899eSWei Fang {
3893f601899eSWei Fang 	struct fec_enet_priv_tx_q *txq;
3894f601899eSWei Fang 	struct netdev_queue *nq;
3895f601899eSWei Fang 	int queue, ret;
3896f601899eSWei Fang 
3897f601899eSWei Fang 	queue = fec_enet_xdp_get_tx_queue(fep, cpu);
3898f601899eSWei Fang 	txq = fep->tx_queue[queue];
3899f601899eSWei Fang 	nq = netdev_get_tx_queue(fep->netdev, queue);
3900f601899eSWei Fang 
3901f601899eSWei Fang 	__netif_tx_lock(nq, cpu);
3902f601899eSWei Fang 
3903f601899eSWei Fang 	/* Avoid tx timeout as XDP shares the queue with kernel stack */
3904f601899eSWei Fang 	txq_trans_cond_update(nq);
3905af6f4791SWei Fang 	ret = fec_enet_txq_xmit_frame(fep, txq, xdp, dma_sync_len, false);
3906f601899eSWei Fang 
3907f601899eSWei Fang 	__netif_tx_unlock(nq);
3908f601899eSWei Fang 
3909f601899eSWei Fang 	return ret;
3910f601899eSWei Fang }
3911f601899eSWei Fang 
39126d6b39f1SShenwei Wang static int fec_enet_xdp_xmit(struct net_device *dev,
39136d6b39f1SShenwei Wang 			     int num_frames,
39146d6b39f1SShenwei Wang 			     struct xdp_frame **frames,
39156d6b39f1SShenwei Wang 			     u32 flags)
39166d6b39f1SShenwei Wang {
39176d6b39f1SShenwei Wang 	struct fec_enet_private *fep = netdev_priv(dev);
39186d6b39f1SShenwei Wang 	struct fec_enet_priv_tx_q *txq;
39196d6b39f1SShenwei Wang 	int cpu = smp_processor_id();
392026312c68SShenwei Wang 	unsigned int sent_frames = 0;
39216d6b39f1SShenwei Wang 	struct netdev_queue *nq;
39226d6b39f1SShenwei Wang 	unsigned int queue;
39236d6b39f1SShenwei Wang 	int i;
39246d6b39f1SShenwei Wang 
39256d6b39f1SShenwei Wang 	queue = fec_enet_xdp_get_tx_queue(fep, cpu);
39266d6b39f1SShenwei Wang 	txq = fep->tx_queue[queue];
39276d6b39f1SShenwei Wang 	nq = netdev_get_tx_queue(fep->netdev, queue);
39286d6b39f1SShenwei Wang 
39296d6b39f1SShenwei Wang 	__netif_tx_lock(nq, cpu);
39306d6b39f1SShenwei Wang 
3931bb7a0156SWei Fang 	/* Avoid tx timeout as XDP shares the queue with kernel stack */
3932bb7a0156SWei Fang 	txq_trans_cond_update(nq);
393326312c68SShenwei Wang 	for (i = 0; i < num_frames; i++) {
3934f601899eSWei Fang 		if (fec_enet_txq_xmit_frame(fep, txq, frames[i], 0, true) < 0)
393526312c68SShenwei Wang 			break;
393626312c68SShenwei Wang 		sent_frames++;
393726312c68SShenwei Wang 	}
39386d6b39f1SShenwei Wang 
39396d6b39f1SShenwei Wang 	__netif_tx_unlock(nq);
39406d6b39f1SShenwei Wang 
394126312c68SShenwei Wang 	return sent_frames;
39426d6b39f1SShenwei Wang }
39436d6b39f1SShenwei Wang 
3944ef5eb9c5SVladimir Oltean static int fec_hwtstamp_get(struct net_device *ndev,
3945ef5eb9c5SVladimir Oltean 			    struct kernel_hwtstamp_config *config)
3946ef5eb9c5SVladimir Oltean {
3947ef5eb9c5SVladimir Oltean 	struct fec_enet_private *fep = netdev_priv(ndev);
3948ef5eb9c5SVladimir Oltean 
3949ef5eb9c5SVladimir Oltean 	if (!netif_running(ndev))
3950ef5eb9c5SVladimir Oltean 		return -EINVAL;
3951ef5eb9c5SVladimir Oltean 
3952ef5eb9c5SVladimir Oltean 	if (!fep->bufdesc_ex)
3953ef5eb9c5SVladimir Oltean 		return -EOPNOTSUPP;
3954ef5eb9c5SVladimir Oltean 
3955ef5eb9c5SVladimir Oltean 	fec_ptp_get(ndev, config);
3956ef5eb9c5SVladimir Oltean 
3957ef5eb9c5SVladimir Oltean 	return 0;
3958ef5eb9c5SVladimir Oltean }
3959ef5eb9c5SVladimir Oltean 
3960ef5eb9c5SVladimir Oltean static int fec_hwtstamp_set(struct net_device *ndev,
3961ef5eb9c5SVladimir Oltean 			    struct kernel_hwtstamp_config *config,
3962ef5eb9c5SVladimir Oltean 			    struct netlink_ext_ack *extack)
3963ef5eb9c5SVladimir Oltean {
3964ef5eb9c5SVladimir Oltean 	struct fec_enet_private *fep = netdev_priv(ndev);
3965ef5eb9c5SVladimir Oltean 
3966ef5eb9c5SVladimir Oltean 	if (!netif_running(ndev))
3967ef5eb9c5SVladimir Oltean 		return -EINVAL;
3968ef5eb9c5SVladimir Oltean 
3969ef5eb9c5SVladimir Oltean 	if (!fep->bufdesc_ex)
3970ef5eb9c5SVladimir Oltean 		return -EOPNOTSUPP;
3971ef5eb9c5SVladimir Oltean 
3972ef5eb9c5SVladimir Oltean 	return fec_ptp_set(ndev, config, extack);
3973ef5eb9c5SVladimir Oltean }
3974ef5eb9c5SVladimir Oltean 
3975793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = {
3976793fc096SFrank Li 	.ndo_open		= fec_enet_open,
3977793fc096SFrank Li 	.ndo_stop		= fec_enet_close,
3978793fc096SFrank Li 	.ndo_start_xmit		= fec_enet_start_xmit,
397952c4a1a8SFugang Duan 	.ndo_select_queue       = fec_enet_select_queue,
3980793fc096SFrank Li 	.ndo_set_rx_mode	= set_multicast_list,
3981793fc096SFrank Li 	.ndo_validate_addr	= eth_validate_addr,
3982793fc096SFrank Li 	.ndo_tx_timeout		= fec_timeout,
3983793fc096SFrank Li 	.ndo_set_mac_address	= fec_set_mac_address,
3984ef5eb9c5SVladimir Oltean 	.ndo_eth_ioctl		= phy_do_ioctl_running,
3985793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3986793fc096SFrank Li 	.ndo_poll_controller	= fec_poll_controller,
3987793fc096SFrank Li #endif
39884c09eed9SJim Baxter 	.ndo_set_features	= fec_set_features,
39896d6b39f1SShenwei Wang 	.ndo_bpf		= fec_enet_bpf,
39906d6b39f1SShenwei Wang 	.ndo_xdp_xmit		= fec_enet_xdp_xmit,
3991ef5eb9c5SVladimir Oltean 	.ndo_hwtstamp_get	= fec_hwtstamp_get,
3992ef5eb9c5SVladimir Oltean 	.ndo_hwtstamp_set	= fec_hwtstamp_set,
3993793fc096SFrank Li };
3994793fc096SFrank Li 
399553bb20d1STroy Kisky static const unsigned short offset_des_active_rxq[] = {
399653bb20d1STroy Kisky 	FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
399753bb20d1STroy Kisky };
399853bb20d1STroy Kisky 
399953bb20d1STroy Kisky static const unsigned short offset_des_active_txq[] = {
400053bb20d1STroy Kisky 	FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
400153bb20d1STroy Kisky };
400253bb20d1STroy Kisky 
4003793fc096SFrank Li  /*
4004793fc096SFrank Li   * XXX:  We need to clean up on failure exits here.
4005793fc096SFrank Li   *
4006793fc096SFrank Li   */
4007793fc096SFrank Li static int fec_enet_init(struct net_device *ndev)
4008793fc096SFrank Li {
4009793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4010793fc096SFrank Li 	struct bufdesc *cbd_base;
40114d494cdcSFugang Duan 	dma_addr_t bd_dma;
401255d0218aSNimrod Andy 	int bd_size;
401359d0f746SFrank Li 	unsigned int i;
40147355f276STroy Kisky 	unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
40157355f276STroy Kisky 			sizeof(struct bufdesc);
40167355f276STroy Kisky 	unsigned dsize_log2 = __fls(dsize);
4017453e9dc4SStefan Agner 	int ret;
401855d0218aSNimrod Andy 
40197355f276STroy Kisky 	WARN_ON(dsize != (1 << dsize_log2));
40203f1dcc6aSLucas Stach #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
402141ef84ceSFugang Duan 	fep->rx_align = 0xf;
402241ef84ceSFugang Duan 	fep->tx_align = 0xf;
402341ef84ceSFugang Duan #else
402441ef84ceSFugang Duan 	fep->rx_align = 0x3;
402541ef84ceSFugang Duan 	fep->tx_align = 0x3;
402641ef84ceSFugang Duan #endif
4027df727d45SRasmus Villemoes 	fep->rx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
4028df727d45SRasmus Villemoes 	fep->tx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
4029df727d45SRasmus Villemoes 	fep->rx_time_itr = FEC_ITR_ICTT_DEFAULT;
4030df727d45SRasmus Villemoes 	fep->tx_time_itr = FEC_ITR_ICTT_DEFAULT;
403141ef84ceSFugang Duan 
4032453e9dc4SStefan Agner 	/* Check mask of the streaming and coherent API */
4033453e9dc4SStefan Agner 	ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
4034453e9dc4SStefan Agner 	if (ret < 0) {
4035453e9dc4SStefan Agner 		dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
4036453e9dc4SStefan Agner 		return ret;
4037453e9dc4SStefan Agner 	}
4038453e9dc4SStefan Agner 
4039619fee9eSFugang Duan 	ret = fec_enet_alloc_queue(ndev);
4040619fee9eSFugang Duan 	if (ret)
4041619fee9eSFugang Duan 		return ret;
404279f33912SNimrod Andy 
40437355f276STroy Kisky 	bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
4044793fc096SFrank Li 
4045793fc096SFrank Li 	/* Allocate memory for buffer descriptors. */
4046c0a1a0a6SLucas Stach 	cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
4047793fc096SFrank Li 				       GFP_KERNEL);
40484d494cdcSFugang Duan 	if (!cbd_base) {
4049619fee9eSFugang Duan 		ret = -ENOMEM;
4050619fee9eSFugang Duan 		goto free_queue_mem;
40514d494cdcSFugang Duan 	}
4052793fc096SFrank Li 
4053793fc096SFrank Li 	/* Get the Ethernet address */
4054052fcc45SFugang Duan 	ret = fec_get_mac(ndev);
4055052fcc45SFugang Duan 	if (ret)
4056052fcc45SFugang Duan 		goto free_queue_mem;
4057052fcc45SFugang Duan 
4058793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
405959d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
40607355f276STroy Kisky 		struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
40617355f276STroy Kisky 		unsigned size = dsize * rxq->bd.ring_size;
40627355f276STroy Kisky 
40637355f276STroy Kisky 		rxq->bd.qid = i;
40647355f276STroy Kisky 		rxq->bd.base = cbd_base;
40657355f276STroy Kisky 		rxq->bd.cur = cbd_base;
40667355f276STroy Kisky 		rxq->bd.dma = bd_dma;
40677355f276STroy Kisky 		rxq->bd.dsize = dsize;
40687355f276STroy Kisky 		rxq->bd.dsize_log2 = dsize_log2;
406953bb20d1STroy Kisky 		rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
40707355f276STroy Kisky 		bd_dma += size;
40717355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
40727355f276STroy Kisky 		rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
407359d0f746SFrank Li 	}
407459d0f746SFrank Li 
407559d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
40767355f276STroy Kisky 		struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
40777355f276STroy Kisky 		unsigned size = dsize * txq->bd.ring_size;
40787355f276STroy Kisky 
40797355f276STroy Kisky 		txq->bd.qid = i;
40807355f276STroy Kisky 		txq->bd.base = cbd_base;
40817355f276STroy Kisky 		txq->bd.cur = cbd_base;
40827355f276STroy Kisky 		txq->bd.dma = bd_dma;
40837355f276STroy Kisky 		txq->bd.dsize = dsize;
40847355f276STroy Kisky 		txq->bd.dsize_log2 = dsize_log2;
408553bb20d1STroy Kisky 		txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
40867355f276STroy Kisky 		bd_dma += size;
40877355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
40887355f276STroy Kisky 		txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
408959d0f746SFrank Li 	}
40904d494cdcSFugang Duan 
4091793fc096SFrank Li 
4092793fc096SFrank Li 	/* The FEC Ethernet specific entries in the device structure */
4093793fc096SFrank Li 	ndev->watchdog_timeo = TX_TIMEOUT;
4094793fc096SFrank Li 	ndev->netdev_ops = &fec_netdev_ops;
4095793fc096SFrank Li 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
4096793fc096SFrank Li 
4097793fc096SFrank Li 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
4098b48b89f9SJakub Kicinski 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi);
4099793fc096SFrank Li 
41006b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_VLAN)
4101cdffcf1bSJim Baxter 		/* enable hw VLAN support */
4102cdffcf1bSJim Baxter 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
4103cdffcf1bSJim Baxter 
41046b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
4105ee8b7a11SJakub Kicinski 		netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS);
410679f33912SNimrod Andy 
41074c09eed9SJim Baxter 		/* enable hw accelerator */
41084c09eed9SJim Baxter 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
410979f33912SNimrod Andy 				| NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
41104c09eed9SJim Baxter 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
411148496255SShawn Guo 	}
41124c09eed9SJim Baxter 
4113471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
411441ef84ceSFugang Duan 		fep->tx_align = 0;
411541ef84ceSFugang Duan 		fep->rx_align = 0x3f;
411641ef84ceSFugang Duan 	}
411741ef84ceSFugang Duan 
411809d1e541SNimrod Andy 	ndev->hw_features = ndev->features;
411909d1e541SNimrod Andy 
4120e4ac7cc6SWei Fang 	if (!(fep->quirks & FEC_QUIRK_SWAP_FRAME))
4121e4ac7cc6SWei Fang 		ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
4122be7ecbe7SWei Fang 				     NETDEV_XDP_ACT_REDIRECT;
4123e4ac7cc6SWei Fang 
4124ef83337dSRussell King 	fec_restart(ndev);
4125793fc096SFrank Li 
41262b30842bSAndrew Lunn 	if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
41272b30842bSAndrew Lunn 		fec_enet_clear_ethtool_stats(ndev);
41282b30842bSAndrew Lunn 	else
412980cca775SNikita Yushchenko 		fec_enet_update_ethtool_stats(ndev);
413080cca775SNikita Yushchenko 
4131793fc096SFrank Li 	return 0;
4132619fee9eSFugang Duan 
4133619fee9eSFugang Duan free_queue_mem:
4134619fee9eSFugang Duan 	fec_enet_free_queue(ndev);
4135619fee9eSFugang Duan 	return ret;
4136793fc096SFrank Li }
4137793fc096SFrank Li 
4138793fc096SFrank Li #ifdef CONFIG_OF
41399269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev)
4140793fc096SFrank Li {
4141468ba54bSArnd Bergmann 	struct gpio_desc *phy_reset;
4142159a0760SQuentin Schulz 	int msec = 1, phy_post_delay = 0;
4143793fc096SFrank Li 	struct device_node *np = pdev->dev.of_node;
4144468ba54bSArnd Bergmann 	int err;
4145793fc096SFrank Li 
4146793fc096SFrank Li 	if (!np)
41479269e556SFugang Duan 		return 0;
4148793fc096SFrank Li 
414961e04ccbSFugang Duan 	err = of_property_read_u32(np, "phy-reset-duration", &msec);
4150793fc096SFrank Li 	/* A sane reset duration should not be longer than 1s */
415161e04ccbSFugang Duan 	if (!err && msec > 1000)
4152793fc096SFrank Li 		msec = 1;
4153793fc096SFrank Li 
4154159a0760SQuentin Schulz 	err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
4155159a0760SQuentin Schulz 	/* valid reset duration should be less than 1s */
4156159a0760SQuentin Schulz 	if (!err && phy_post_delay > 1000)
4157159a0760SQuentin Schulz 		return -EINVAL;
4158159a0760SQuentin Schulz 
4159d7b5e5ddSDmitry Torokhov 	phy_reset = devm_gpiod_get_optional(&pdev->dev, "phy-reset",
41600719bc3aSDmitry Torokhov 					    GPIOD_OUT_HIGH);
4161468ba54bSArnd Bergmann 	if (IS_ERR(phy_reset))
4162468ba54bSArnd Bergmann 		return dev_err_probe(&pdev->dev, PTR_ERR(phy_reset),
4163468ba54bSArnd Bergmann 				     "failed to get phy-reset-gpios\n");
4164eb37c563SStefan Wahren 
4165d7b5e5ddSDmitry Torokhov 	if (!phy_reset)
4166d7b5e5ddSDmitry Torokhov 		return 0;
4167d7b5e5ddSDmitry Torokhov 
4168eb37c563SStefan Wahren 	if (msec > 20)
4169793fc096SFrank Li 		msleep(msec);
4170eb37c563SStefan Wahren 	else
4171eb37c563SStefan Wahren 		usleep_range(msec * 1000, msec * 1000 + 1000);
4172eb37c563SStefan Wahren 
41730719bc3aSDmitry Torokhov 	gpiod_set_value_cansleep(phy_reset, 0);
41749269e556SFugang Duan 
4175159a0760SQuentin Schulz 	if (!phy_post_delay)
4176159a0760SQuentin Schulz 		return 0;
4177159a0760SQuentin Schulz 
4178159a0760SQuentin Schulz 	if (phy_post_delay > 20)
4179159a0760SQuentin Schulz 		msleep(phy_post_delay);
4180159a0760SQuentin Schulz 	else
4181159a0760SQuentin Schulz 		usleep_range(phy_post_delay * 1000,
4182159a0760SQuentin Schulz 			     phy_post_delay * 1000 + 1000);
4183159a0760SQuentin Schulz 
41849269e556SFugang Duan 	return 0;
4185793fc096SFrank Li }
4186793fc096SFrank Li #else /* CONFIG_OF */
41879269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev)
4188793fc096SFrank Li {
4189793fc096SFrank Li 	/*
4190793fc096SFrank Li 	 * In case of platform probe, the reset has been done
4191793fc096SFrank Li 	 * by machine code.
4192793fc096SFrank Li 	 */
41939269e556SFugang Duan 	return 0;
4194793fc096SFrank Li }
4195793fc096SFrank Li #endif /* CONFIG_OF */
4196793fc096SFrank Li 
41979fc095f1SFugang Duan static void
41989fc095f1SFugang Duan fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
41999fc095f1SFugang Duan {
42009fc095f1SFugang Duan 	struct device_node *np = pdev->dev.of_node;
42019fc095f1SFugang Duan 
42029fc095f1SFugang Duan 	*num_tx = *num_rx = 1;
42039fc095f1SFugang Duan 
42049fc095f1SFugang Duan 	if (!np || !of_device_is_available(np))
42059fc095f1SFugang Duan 		return;
42069fc095f1SFugang Duan 
42079fc095f1SFugang Duan 	/* parse the num of tx and rx queues */
420873b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
4209b7bd75cfSFrank Li 
421073b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
42119fc095f1SFugang Duan 
42129fc095f1SFugang Duan 	if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
4213b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
42149fc095f1SFugang Duan 			 *num_tx);
42159fc095f1SFugang Duan 		*num_tx = 1;
42169fc095f1SFugang Duan 		return;
42179fc095f1SFugang Duan 	}
42189fc095f1SFugang Duan 
42199fc095f1SFugang Duan 	if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
4220b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
42219fc095f1SFugang Duan 			 *num_rx);
42229fc095f1SFugang Duan 		*num_rx = 1;
42239fc095f1SFugang Duan 		return;
42249fc095f1SFugang Duan 	}
42259fc095f1SFugang Duan 
42269fc095f1SFugang Duan }
42279fc095f1SFugang Duan 
42284ad1ceecSTroy Kisky static int fec_enet_get_irq_cnt(struct platform_device *pdev)
42294ad1ceecSTroy Kisky {
42304ad1ceecSTroy Kisky 	int irq_cnt = platform_irq_count(pdev);
42314ad1ceecSTroy Kisky 
42324ad1ceecSTroy Kisky 	if (irq_cnt > FEC_IRQ_NUM)
42334ad1ceecSTroy Kisky 		irq_cnt = FEC_IRQ_NUM;	/* last for pps */
42344ad1ceecSTroy Kisky 	else if (irq_cnt == 2)
42354ad1ceecSTroy Kisky 		irq_cnt = 1;	/* last for pps */
42364ad1ceecSTroy Kisky 	else if (irq_cnt <= 0)
42374ad1ceecSTroy Kisky 		irq_cnt = 1;	/* At least 1 irq is needed */
42384ad1ceecSTroy Kisky 	return irq_cnt;
42394ad1ceecSTroy Kisky }
42404ad1ceecSTroy Kisky 
4241b7cdc965SJoakim Zhang static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
4242b7cdc965SJoakim Zhang {
4243b7cdc965SJoakim Zhang 	struct net_device *ndev = platform_get_drvdata(pdev);
4244b7cdc965SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
4245b7cdc965SJoakim Zhang 
4246b7cdc965SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
4247b7cdc965SJoakim Zhang 		fep->wake_irq = fep->irq[2];
4248b7cdc965SJoakim Zhang 	else
4249b7cdc965SJoakim Zhang 		fep->wake_irq = fep->irq[0];
4250b7cdc965SJoakim Zhang }
4251b7cdc965SJoakim Zhang 
4252da722186SMartin Fuzzey static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
4253da722186SMartin Fuzzey 				   struct device_node *np)
4254da722186SMartin Fuzzey {
4255da722186SMartin Fuzzey 	struct device_node *gpr_np;
42568a448bf8SFugang Duan 	u32 out_val[3];
4257da722186SMartin Fuzzey 	int ret = 0;
4258da722186SMartin Fuzzey 
42598a448bf8SFugang Duan 	gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
4260da722186SMartin Fuzzey 	if (!gpr_np)
4261da722186SMartin Fuzzey 		return 0;
4262da722186SMartin Fuzzey 
42638a448bf8SFugang Duan 	ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
42648a448bf8SFugang Duan 					 ARRAY_SIZE(out_val));
42658a448bf8SFugang Duan 	if (ret) {
42668a448bf8SFugang Duan 		dev_dbg(&fep->pdev->dev, "no stop mode property\n");
4267d2b52ec0SYang Yingliang 		goto out;
42688a448bf8SFugang Duan 	}
42698a448bf8SFugang Duan 
4270da722186SMartin Fuzzey 	fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
4271da722186SMartin Fuzzey 	if (IS_ERR(fep->stop_gpr.gpr)) {
4272da722186SMartin Fuzzey 		dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
4273da722186SMartin Fuzzey 		ret = PTR_ERR(fep->stop_gpr.gpr);
4274da722186SMartin Fuzzey 		fep->stop_gpr.gpr = NULL;
4275da722186SMartin Fuzzey 		goto out;
4276da722186SMartin Fuzzey 	}
4277da722186SMartin Fuzzey 
42788a448bf8SFugang Duan 	fep->stop_gpr.reg = out_val[1];
42798a448bf8SFugang Duan 	fep->stop_gpr.bit = out_val[2];
4280da722186SMartin Fuzzey 
4281da722186SMartin Fuzzey out:
4282da722186SMartin Fuzzey 	of_node_put(gpr_np);
4283da722186SMartin Fuzzey 
4284da722186SMartin Fuzzey 	return ret;
4285da722186SMartin Fuzzey }
4286da722186SMartin Fuzzey 
4287793fc096SFrank Li static int
4288793fc096SFrank Li fec_probe(struct platform_device *pdev)
4289793fc096SFrank Li {
4290793fc096SFrank Li 	struct fec_enet_private *fep;
4291793fc096SFrank Li 	struct fec_platform_data *pdata;
42920c65b2b9SAndrew Lunn 	phy_interface_t interface;
4293793fc096SFrank Li 	struct net_device *ndev;
4294793fc096SFrank Li 	int i, irq, ret = 0;
4295793fc096SFrank Li 	const struct of_device_id *of_id;
4296793fc096SFrank Li 	static int dev_id;
4297407066f8SUwe Kleine-König 	struct device_node *np = pdev->dev.of_node, *phy_node;
4298b7bd75cfSFrank Li 	int num_tx_qs;
4299b7bd75cfSFrank Li 	int num_rx_qs;
43004ad1ceecSTroy Kisky 	char irq_name[8];
43014ad1ceecSTroy Kisky 	int irq_cnt;
4302da722186SMartin Fuzzey 	struct fec_devinfo *dev_info;
4303793fc096SFrank Li 
43049fc095f1SFugang Duan 	fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
43059fc095f1SFugang Duan 
4306793fc096SFrank Li 	/* Init network device */
430780cca775SNikita Yushchenko 	ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
4308f85de666SNikita Yushchenko 				  FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
4309793fc096SFrank Li 	if (!ndev)
4310793fc096SFrank Li 		return -ENOMEM;
4311793fc096SFrank Li 
4312793fc096SFrank Li 	SET_NETDEV_DEV(ndev, &pdev->dev);
4313793fc096SFrank Li 
4314793fc096SFrank Li 	/* setup board info structure */
4315793fc096SFrank Li 	fep = netdev_priv(ndev);
4316793fc096SFrank Li 
43176b7e4008SLothar Waßmann 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
43186b7e4008SLothar Waßmann 	if (of_id)
43196b7e4008SLothar Waßmann 		pdev->id_entry = of_id->data;
4320da722186SMartin Fuzzey 	dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
4321da722186SMartin Fuzzey 	if (dev_info)
4322da722186SMartin Fuzzey 		fep->quirks = dev_info->quirks;
43236b7e4008SLothar Waßmann 
43240c818594SHubert Feurstein 	fep->netdev = ndev;
43259fc095f1SFugang Duan 	fep->num_rx_queues = num_rx_qs;
43269fc095f1SFugang Duan 	fep->num_tx_queues = num_tx_qs;
43279fc095f1SFugang Duan 
4328d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
4329793fc096SFrank Li 	/* default enable pause frame auto negotiation */
43306b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT)
4331793fc096SFrank Li 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
4332d1391930SGuenter Roeck #endif
4333793fc096SFrank Li 
43345bbde4d2SNimrod Andy 	/* Select default pin state */
43355bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&pdev->dev);
43365bbde4d2SNimrod Andy 
43374f830a5aSYueHaibing 	fep->hwp = devm_platform_ioremap_resource(pdev, 0);
4338941e173aSTushar Behera 	if (IS_ERR(fep->hwp)) {
4339941e173aSTushar Behera 		ret = PTR_ERR(fep->hwp);
4340941e173aSTushar Behera 		goto failed_ioremap;
4341941e173aSTushar Behera 	}
4342941e173aSTushar Behera 
4343793fc096SFrank Li 	fep->pdev = pdev;
4344793fc096SFrank Li 	fep->dev_id = dev_id++;
4345793fc096SFrank Li 
4346793fc096SFrank Li 	platform_set_drvdata(pdev, ndev);
4347793fc096SFrank Li 
434829380905SLucas Stach 	if ((of_machine_is_compatible("fsl,imx6q") ||
434929380905SLucas Stach 	     of_machine_is_compatible("fsl,imx6dl")) &&
435029380905SLucas Stach 	    !of_property_read_bool(np, "fsl,err006687-workaround-present"))
435129380905SLucas Stach 		fep->quirks |= FEC_QUIRK_ERR006687;
435229380905SLucas Stach 
435340c79ce1SWei Fang 	ret = fec_enet_ipc_handle_init(fep);
435440c79ce1SWei Fang 	if (ret)
435540c79ce1SWei Fang 		goto failed_ipc_init;
435640c79ce1SWei Fang 
43571a87e641SRob Herring 	if (of_property_read_bool(np, "fsl,magic-packet"))
4358de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
4359de40ed31SNimrod Andy 
43608a448bf8SFugang Duan 	ret = fec_enet_init_stop_mode(fep, np);
4361da722186SMartin Fuzzey 	if (ret)
4362da722186SMartin Fuzzey 		goto failed_stop_mode;
4363da722186SMartin Fuzzey 
4364407066f8SUwe Kleine-König 	phy_node = of_parse_phandle(np, "phy-handle", 0);
4365407066f8SUwe Kleine-König 	if (!phy_node && of_phy_is_fixed_link(np)) {
4366407066f8SUwe Kleine-König 		ret = of_phy_register_fixed_link(np);
4367407066f8SUwe Kleine-König 		if (ret < 0) {
4368407066f8SUwe Kleine-König 			dev_err(&pdev->dev,
4369407066f8SUwe Kleine-König 				"broken fixed-link specification\n");
4370407066f8SUwe Kleine-König 			goto failed_phy;
4371407066f8SUwe Kleine-König 		}
4372407066f8SUwe Kleine-König 		phy_node = of_node_get(np);
4373407066f8SUwe Kleine-König 	}
4374407066f8SUwe Kleine-König 	fep->phy_node = phy_node;
4375407066f8SUwe Kleine-König 
43760c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(pdev->dev.of_node, &interface);
43770c65b2b9SAndrew Lunn 	if (ret) {
437894660ba0SJingoo Han 		pdata = dev_get_platdata(&pdev->dev);
4379793fc096SFrank Li 		if (pdata)
4380793fc096SFrank Li 			fep->phy_interface = pdata->phy;
4381793fc096SFrank Li 		else
4382793fc096SFrank Li 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
4383793fc096SFrank Li 	} else {
43840c65b2b9SAndrew Lunn 		fep->phy_interface = interface;
4385793fc096SFrank Li 	}
4386793fc096SFrank Li 
4387b820c114SJoakim Zhang 	ret = fec_enet_parse_rgmii_delay(fep, np);
4388b820c114SJoakim Zhang 	if (ret)
4389b820c114SJoakim Zhang 		goto failed_rgmii_delay;
4390b820c114SJoakim Zhang 
4391793fc096SFrank Li 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
4392793fc096SFrank Li 	if (IS_ERR(fep->clk_ipg)) {
4393793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ipg);
4394793fc096SFrank Li 		goto failed_clk;
4395793fc096SFrank Li 	}
4396793fc096SFrank Li 
4397793fc096SFrank Li 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
4398793fc096SFrank Li 	if (IS_ERR(fep->clk_ahb)) {
4399793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ahb);
4400793fc096SFrank Li 		goto failed_clk;
4401793fc096SFrank Li 	}
4402793fc096SFrank Li 
4403d851b47bSFugang Duan 	fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
4404d851b47bSFugang Duan 
440538f56f33SLinus Torvalds 	/* enet_out is optional, depends on board */
44065ff851b7SUwe Kleine-König 	fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out");
44075ff851b7SUwe Kleine-König 	if (IS_ERR(fep->clk_enet_out)) {
44085ff851b7SUwe Kleine-König 		ret = PTR_ERR(fep->clk_enet_out);
44095ff851b7SUwe Kleine-König 		goto failed_clk;
44105ff851b7SUwe Kleine-König 	}
441138f56f33SLinus Torvalds 
441291c0d987SNimrod Andy 	fep->ptp_clk_on = false;
441301b825f9SFrancesco Dolcini 	mutex_init(&fep->ptp_clk_mutex);
44149b5330edSFugang Duan 
44159b5330edSFugang Duan 	/* clk_ref is optional, depends on board */
441643252ed1SUwe Kleine-König 	fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
441743252ed1SUwe Kleine-König 	if (IS_ERR(fep->clk_ref)) {
441843252ed1SUwe Kleine-König 		ret = PTR_ERR(fep->clk_ref);
441943252ed1SUwe Kleine-König 		goto failed_clk;
442043252ed1SUwe Kleine-König 	}
4421b82f8c3fSFugang Duan 	fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
44229b5330edSFugang Duan 
4423fc539459SFugang Duan 	/* clk_2x_txclk is optional, depends on board */
4424b820c114SJoakim Zhang 	if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
4425fc539459SFugang Duan 		fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
4426fc539459SFugang Duan 		if (IS_ERR(fep->clk_2x_txclk))
4427fc539459SFugang Duan 			fep->clk_2x_txclk = NULL;
4428b820c114SJoakim Zhang 	}
4429fc539459SFugang Duan 
44306b7e4008SLothar Waßmann 	fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
4431793fc096SFrank Li 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
4432793fc096SFrank Li 	if (IS_ERR(fep->clk_ptp)) {
443338f56f33SLinus Torvalds 		fep->clk_ptp = NULL;
4434217b5844SLothar Waßmann 		fep->bufdesc_ex = false;
4435793fc096SFrank Li 	}
4436793fc096SFrank Li 
4437e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
443813a097bdSFabio Estevam 	if (ret)
443913a097bdSFabio Estevam 		goto failed_clk;
444013a097bdSFabio Estevam 
44418fff755eSAndrew Lunn 	ret = clk_prepare_enable(fep->clk_ipg);
44428fff755eSAndrew Lunn 	if (ret)
44438fff755eSAndrew Lunn 		goto failed_clk_ipg;
4444d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ahb);
4445d7c3a206SAndy Duan 	if (ret)
4446d7c3a206SAndy Duan 		goto failed_clk_ahb;
44478fff755eSAndrew Lunn 
444825974d8aSStefan Agner 	fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
4449f4e9f3d2SFabio Estevam 	if (!IS_ERR(fep->reg_phy)) {
4450f4e9f3d2SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
4451793fc096SFrank Li 		if (ret) {
4452793fc096SFrank Li 			dev_err(&pdev->dev,
4453793fc096SFrank Li 				"Failed to enable phy regulator: %d\n", ret);
4454793fc096SFrank Li 			goto failed_regulator;
4455793fc096SFrank Li 		}
4456f6a4d607SFabio Estevam 	} else {
44573f38c683SFugang Duan 		if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
44583f38c683SFugang Duan 			ret = -EPROBE_DEFER;
44593f38c683SFugang Duan 			goto failed_regulator;
44603f38c683SFugang Duan 		}
4461f6a4d607SFabio Estevam 		fep->reg_phy = NULL;
4462793fc096SFrank Li 	}
4463793fc096SFrank Li 
44648fff755eSAndrew Lunn 	pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
44658fff755eSAndrew Lunn 	pm_runtime_use_autosuspend(&pdev->dev);
446614d2b7c1SLucas Stach 	pm_runtime_get_noresume(&pdev->dev);
44678fff755eSAndrew Lunn 	pm_runtime_set_active(&pdev->dev);
44688fff755eSAndrew Lunn 	pm_runtime_enable(&pdev->dev);
44698fff755eSAndrew Lunn 
44709269e556SFugang Duan 	ret = fec_reset_phy(pdev);
44719269e556SFugang Duan 	if (ret)
44729269e556SFugang Duan 		goto failed_reset;
4473793fc096SFrank Li 
44744ad1ceecSTroy Kisky 	irq_cnt = fec_enet_get_irq_cnt(pdev);
4475793fc096SFrank Li 	if (fep->bufdesc_ex)
44764ad1ceecSTroy Kisky 		fec_ptp_init(pdev, irq_cnt);
4477793fc096SFrank Li 
4478793fc096SFrank Li 	ret = fec_enet_init(ndev);
4479793fc096SFrank Li 	if (ret)
4480793fc096SFrank Li 		goto failed_init;
4481793fc096SFrank Li 
44824ad1ceecSTroy Kisky 	for (i = 0; i < irq_cnt; i++) {
44833ded9f2bSArnd Bergmann 		snprintf(irq_name, sizeof(irq_name), "int%d", i);
44843b56be21SAnson Huang 		irq = platform_get_irq_byname_optional(pdev, irq_name);
44854ad1ceecSTroy Kisky 		if (irq < 0)
4486793fc096SFrank Li 			irq = platform_get_irq(pdev, i);
4487793fc096SFrank Li 		if (irq < 0) {
4488793fc096SFrank Li 			ret = irq;
4489793fc096SFrank Li 			goto failed_irq;
4490793fc096SFrank Li 		}
44910d9b2ab1SFabio Estevam 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
449244a272ddSMichael Opdenacker 				       0, pdev->name, ndev);
44930d9b2ab1SFabio Estevam 		if (ret)
4494793fc096SFrank Li 			goto failed_irq;
4495de40ed31SNimrod Andy 
4496de40ed31SNimrod Andy 		fep->irq[i] = irq;
4497793fc096SFrank Li 	}
4498793fc096SFrank Li 
4499b7cdc965SJoakim Zhang 	/* Decide which interrupt line is wakeup capable */
4500b7cdc965SJoakim Zhang 	fec_enet_get_wakeup_irq(pdev);
4501b7cdc965SJoakim Zhang 
4502793fc096SFrank Li 	ret = fec_enet_mii_init(pdev);
4503793fc096SFrank Li 	if (ret)
4504793fc096SFrank Li 		goto failed_mii_init;
4505793fc096SFrank Li 
4506793fc096SFrank Li 	/* Carrier starts down, phylib will bring it up */
4507793fc096SFrank Li 	netif_carrier_off(ndev);
4508e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
45095bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&pdev->dev);
4510793fc096SFrank Li 
451159193053SAndrew Lunn 	ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN;
451259193053SAndrew Lunn 
4513793fc096SFrank Li 	ret = register_netdev(ndev);
4514793fc096SFrank Li 	if (ret)
4515793fc096SFrank Li 		goto failed_register;
4516793fc096SFrank Li 
4517de40ed31SNimrod Andy 	device_init_wakeup(&ndev->dev, fep->wol_flag &
4518de40ed31SNimrod Andy 			   FEC_WOL_HAS_MAGIC_PACKET);
4519de40ed31SNimrod Andy 
4520eb1d0640SFabio Estevam 	if (fep->bufdesc_ex && fep->ptp_clock)
4521eb1d0640SFabio Estevam 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
4522eb1d0640SFabio Estevam 
452336cdc743SRussell King 	INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
45248fff755eSAndrew Lunn 
45258fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&pdev->dev);
45268fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&pdev->dev);
45278fff755eSAndrew Lunn 
4528793fc096SFrank Li 	return 0;
4529793fc096SFrank Li 
4530793fc096SFrank Li failed_register:
4531793fc096SFrank Li 	fec_enet_mii_remove(fep);
4532793fc096SFrank Li failed_mii_init:
45337a2bbd8dSFabio Estevam failed_irq:
45347a2bbd8dSFabio Estevam failed_init:
453532cba57bSLucas Stach 	fec_ptp_stop(pdev);
45369269e556SFugang Duan failed_reset:
4537ce8d24f9SAndy Duan 	pm_runtime_put_noidle(&pdev->dev);
45389269e556SFugang Duan 	pm_runtime_disable(&pdev->dev);
4539c6165cf0SFugang Duan 	if (fep->reg_phy)
4540c6165cf0SFugang Duan 		regulator_disable(fep->reg_phy);
4541793fc096SFrank Li failed_regulator:
4542d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
4543d7c3a206SAndy Duan failed_clk_ahb:
4544d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ipg);
45458fff755eSAndrew Lunn failed_clk_ipg:
4546e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
4547793fc096SFrank Li failed_clk:
4548b820c114SJoakim Zhang failed_rgmii_delay:
454982005b1cSJohan Hovold 	if (of_phy_is_fixed_link(np))
455082005b1cSJohan Hovold 		of_phy_deregister_fixed_link(np);
4551407066f8SUwe Kleine-König 	of_node_put(phy_node);
4552da722186SMartin Fuzzey failed_stop_mode:
455340c79ce1SWei Fang failed_ipc_init:
4554d1616f07SFugang Duan failed_phy:
4555d1616f07SFugang Duan 	dev_id--;
4556793fc096SFrank Li failed_ioremap:
4557793fc096SFrank Li 	free_netdev(ndev);
4558793fc096SFrank Li 
4559793fc096SFrank Li 	return ret;
4560793fc096SFrank Li }
4561793fc096SFrank Li 
456212d6cc19SUwe Kleine-König static void
4563793fc096SFrank Li fec_drv_remove(struct platform_device *pdev)
4564793fc096SFrank Li {
4565793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
4566793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
456782005b1cSJohan Hovold 	struct device_node *np = pdev->dev.of_node;
4568a31eda65SChuhong Yuan 	int ret;
4569a31eda65SChuhong Yuan 
4570f816b982SUwe Kleine-König 	ret = pm_runtime_get_sync(&pdev->dev);
4571a31eda65SChuhong Yuan 	if (ret < 0)
4572f816b982SUwe Kleine-König 		dev_err(&pdev->dev,
4573f816b982SUwe Kleine-König 			"Failed to resume device in remove callback (%pe)\n",
4574f816b982SUwe Kleine-König 			ERR_PTR(ret));
4575793fc096SFrank Li 
457636cdc743SRussell King 	cancel_work_sync(&fep->tx_timeout_work);
457732cba57bSLucas Stach 	fec_ptp_stop(pdev);
4578793fc096SFrank Li 	unregister_netdev(ndev);
4579793fc096SFrank Li 	fec_enet_mii_remove(fep);
4580f6a4d607SFabio Estevam 	if (fep->reg_phy)
4581f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
4582a31eda65SChuhong Yuan 
458382005b1cSJohan Hovold 	if (of_phy_is_fixed_link(np))
458482005b1cSJohan Hovold 		of_phy_deregister_fixed_link(np);
4585407066f8SUwe Kleine-König 	of_node_put(fep->phy_node);
4586793fc096SFrank Li 
4587f816b982SUwe Kleine-König 	/* After pm_runtime_get_sync() failed, the clks are still off, so skip
4588f816b982SUwe Kleine-König 	 * disabling them again.
4589f816b982SUwe Kleine-König 	 */
4590f816b982SUwe Kleine-König 	if (ret >= 0) {
4591a31eda65SChuhong Yuan 		clk_disable_unprepare(fep->clk_ahb);
4592a31eda65SChuhong Yuan 		clk_disable_unprepare(fep->clk_ipg);
4593f816b982SUwe Kleine-König 	}
4594a31eda65SChuhong Yuan 	pm_runtime_put_noidle(&pdev->dev);
4595a31eda65SChuhong Yuan 	pm_runtime_disable(&pdev->dev);
4596a31eda65SChuhong Yuan 
459744712965SPavel Skripkin 	free_netdev(ndev);
4598793fc096SFrank Li }
4599793fc096SFrank Li 
4600dd66d386SFabio Estevam static int __maybe_unused fec_suspend(struct device *dev)
4601793fc096SFrank Li {
4602793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
4603793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4604da970726SWei Fang 	int ret;
4605793fc096SFrank Li 
4606da1774e5SRussell King 	rtnl_lock();
4607793fc096SFrank Li 	if (netif_running(ndev)) {
4608de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
4609de40ed31SNimrod Andy 			fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
461045f5c327SPhilippe Reynes 		phy_stop(ndev->phydev);
461131a6de34SRussell King 		napi_disable(&fep->napi);
461231a6de34SRussell King 		netif_tx_lock_bh(ndev);
4613793fc096SFrank Li 		netif_device_detach(ndev);
461431a6de34SRussell King 		netif_tx_unlock_bh(ndev);
461531a6de34SRussell King 		fec_stop(ndev);
46160b6f65c7SJoakim Zhang 		if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
46170b6f65c7SJoakim Zhang 			fec_irqs_disable(ndev);
46185bbde4d2SNimrod Andy 			pinctrl_pm_select_sleep_state(&fep->pdev->dev);
46190b6f65c7SJoakim Zhang 		} else {
46200b6f65c7SJoakim Zhang 			fec_irqs_disable_except_wakeup(ndev);
46210b6f65c7SJoakim Zhang 			if (fep->wake_irq > 0) {
46220b6f65c7SJoakim Zhang 				disable_irq(fep->wake_irq);
46230b6f65c7SJoakim Zhang 				enable_irq_wake(fep->wake_irq);
46240b6f65c7SJoakim Zhang 			}
46250b6f65c7SJoakim Zhang 			fec_enet_stop_mode(fep, true);
46260b6f65c7SJoakim Zhang 		}
46270b6f65c7SJoakim Zhang 		/* It's safe to disable clocks since interrupts are masked */
46280b6f65c7SJoakim Zhang 		fec_enet_clk_enable(ndev, false);
4629da970726SWei Fang 
4630da970726SWei Fang 		fep->rpm_active = !pm_runtime_status_suspended(dev);
4631da970726SWei Fang 		if (fep->rpm_active) {
4632da970726SWei Fang 			ret = pm_runtime_force_suspend(dev);
4633da970726SWei Fang 			if (ret < 0) {
4634da970726SWei Fang 				rtnl_unlock();
4635da970726SWei Fang 				return ret;
4636da970726SWei Fang 			}
4637da970726SWei Fang 		}
4638f4c4a4e0SNimrod Andy 	}
4639f4c4a4e0SNimrod Andy 	rtnl_unlock();
4640793fc096SFrank Li 
4641de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
4642238f7bc7SFabio Estevam 		regulator_disable(fep->reg_phy);
4643238f7bc7SFabio Estevam 
4644858eeb7dSNimrod Andy 	/* SOC supply clock to phy, when clock is disabled, phy link down
4645858eeb7dSNimrod Andy 	 * SOC control phy regulator, when regulator is disabled, phy link down
4646858eeb7dSNimrod Andy 	 */
4647858eeb7dSNimrod Andy 	if (fep->clk_enet_out || fep->reg_phy)
4648858eeb7dSNimrod Andy 		fep->link = 0;
4649858eeb7dSNimrod Andy 
4650793fc096SFrank Li 	return 0;
4651793fc096SFrank Li }
4652793fc096SFrank Li 
4653dd66d386SFabio Estevam static int __maybe_unused fec_resume(struct device *dev)
4654793fc096SFrank Li {
4655793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
4656793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4657238f7bc7SFabio Estevam 	int ret;
4658de40ed31SNimrod Andy 	int val;
4659238f7bc7SFabio Estevam 
4660de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4661238f7bc7SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
4662238f7bc7SFabio Estevam 		if (ret)
4663238f7bc7SFabio Estevam 			return ret;
4664238f7bc7SFabio Estevam 	}
4665793fc096SFrank Li 
4666da1774e5SRussell King 	rtnl_lock();
4667793fc096SFrank Li 	if (netif_running(ndev)) {
4668da970726SWei Fang 		if (fep->rpm_active)
4669da970726SWei Fang 			pm_runtime_force_resume(dev);
4670da970726SWei Fang 
4671f4c4a4e0SNimrod Andy 		ret = fec_enet_clk_enable(ndev, true);
4672f4c4a4e0SNimrod Andy 		if (ret) {
4673f4c4a4e0SNimrod Andy 			rtnl_unlock();
4674f4c4a4e0SNimrod Andy 			goto failed_clk;
4675f4c4a4e0SNimrod Andy 		}
4676de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
4677da722186SMartin Fuzzey 			fec_enet_stop_mode(fep, false);
46780b6f65c7SJoakim Zhang 			if (fep->wake_irq) {
46790b6f65c7SJoakim Zhang 				disable_irq_wake(fep->wake_irq);
46800b6f65c7SJoakim Zhang 				enable_irq(fep->wake_irq);
46810b6f65c7SJoakim Zhang 			}
4682da722186SMartin Fuzzey 
4683de40ed31SNimrod Andy 			val = readl(fep->hwp + FEC_ECNTRL);
4684de40ed31SNimrod Andy 			val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
4685de40ed31SNimrod Andy 			writel(val, fep->hwp + FEC_ECNTRL);
4686de40ed31SNimrod Andy 			fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
4687de40ed31SNimrod Andy 		} else {
4688de40ed31SNimrod Andy 			pinctrl_pm_select_default_state(&fep->pdev->dev);
4689de40ed31SNimrod Andy 		}
4690ef83337dSRussell King 		fec_restart(ndev);
469131a6de34SRussell King 		netif_tx_lock_bh(ndev);
4692793fc096SFrank Li 		netif_device_attach(ndev);
46936af42d42SRussell King 		netif_tx_unlock_bh(ndev);
46946af42d42SRussell King 		napi_enable(&fep->napi);
4695557d5dc8SHeiner Kallweit 		phy_init_hw(ndev->phydev);
469645f5c327SPhilippe Reynes 		phy_start(ndev->phydev);
4697793fc096SFrank Li 	}
4698da1774e5SRussell King 	rtnl_unlock();
4699793fc096SFrank Li 
4700793fc096SFrank Li 	return 0;
470113a097bdSFabio Estevam 
4702e8fcfcd5SNimrod Andy failed_clk:
470313a097bdSFabio Estevam 	if (fep->reg_phy)
470413a097bdSFabio Estevam 		regulator_disable(fep->reg_phy);
470513a097bdSFabio Estevam 	return ret;
4706793fc096SFrank Li }
4707793fc096SFrank Li 
47088fff755eSAndrew Lunn static int __maybe_unused fec_runtime_suspend(struct device *dev)
47098fff755eSAndrew Lunn {
47108fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
47118fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
47128fff755eSAndrew Lunn 
4713d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
47148fff755eSAndrew Lunn 	clk_disable_unprepare(fep->clk_ipg);
47158fff755eSAndrew Lunn 
47168fff755eSAndrew Lunn 	return 0;
47178fff755eSAndrew Lunn }
47188fff755eSAndrew Lunn 
47198fff755eSAndrew Lunn static int __maybe_unused fec_runtime_resume(struct device *dev)
47208fff755eSAndrew Lunn {
47218fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
47228fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
4723d7c3a206SAndy Duan 	int ret;
47248fff755eSAndrew Lunn 
4725d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ahb);
4726d7c3a206SAndy Duan 	if (ret)
4727d7c3a206SAndy Duan 		return ret;
4728d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ipg);
4729d7c3a206SAndy Duan 	if (ret)
4730d7c3a206SAndy Duan 		goto failed_clk_ipg;
4731d7c3a206SAndy Duan 
4732d7c3a206SAndy Duan 	return 0;
4733d7c3a206SAndy Duan 
4734d7c3a206SAndy Duan failed_clk_ipg:
4735d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
4736d7c3a206SAndy Duan 	return ret;
47378fff755eSAndrew Lunn }
47388fff755eSAndrew Lunn 
47398fff755eSAndrew Lunn static const struct dev_pm_ops fec_pm_ops = {
47408fff755eSAndrew Lunn 	SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
47418fff755eSAndrew Lunn 	SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
47428fff755eSAndrew Lunn };
4743793fc096SFrank Li 
4744793fc096SFrank Li static struct platform_driver fec_driver = {
4745793fc096SFrank Li 	.driver	= {
4746793fc096SFrank Li 		.name	= DRIVER_NAME,
4747793fc096SFrank Li 		.pm	= &fec_pm_ops,
4748793fc096SFrank Li 		.of_match_table = fec_dt_ids,
4749272bb0e9SFabio Estevam 		.suppress_bind_attrs = true,
4750793fc096SFrank Li 	},
4751793fc096SFrank Li 	.id_table = fec_devtype,
4752793fc096SFrank Li 	.probe	= fec_probe,
475312d6cc19SUwe Kleine-König 	.remove_new = fec_drv_remove,
4754793fc096SFrank Li };
4755793fc096SFrank Li 
4756793fc096SFrank Li module_platform_driver(fec_driver);
4757793fc096SFrank Li 
4758793fc096SFrank Li MODULE_LICENSE("GPL");
4759