xref: /linux/drivers/net/ethernet/freescale/fec_main.c (revision b48b89f9c189d24eb5e2b4a0ac067da5a24ee86d)
11f508124SFabio Estevam // SPDX-License-Identifier: GPL-2.0+
2793fc096SFrank Li /*
3793fc096SFrank Li  * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4793fc096SFrank Li  * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5793fc096SFrank Li  *
6793fc096SFrank Li  * Right now, I am very wasteful with the buffers.  I allocate memory
7793fc096SFrank Li  * pages and then divide them into 2K frame buffers.  This way I know I
8793fc096SFrank Li  * have buffers large enough to hold one frame within one buffer descriptor.
9793fc096SFrank Li  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
10793fc096SFrank Li  * will be much more memory efficient and will easily handle lots of
11793fc096SFrank Li  * small packets.
12793fc096SFrank Li  *
13793fc096SFrank Li  * Much better multiple PHY support by Magnus Damm.
14793fc096SFrank Li  * Copyright (c) 2000 Ericsson Radio Systems AB.
15793fc096SFrank Li  *
16793fc096SFrank Li  * Support for FEC controller of ColdFire processors.
17793fc096SFrank Li  * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
18793fc096SFrank Li  *
19793fc096SFrank Li  * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
20793fc096SFrank Li  * Copyright (c) 2004-2006 Macq Electronique SA.
21793fc096SFrank Li  *
22793fc096SFrank Li  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
23793fc096SFrank Li  */
24793fc096SFrank Li 
25793fc096SFrank Li #include <linux/module.h>
26793fc096SFrank Li #include <linux/kernel.h>
27793fc096SFrank Li #include <linux/string.h>
288fff755eSAndrew Lunn #include <linux/pm_runtime.h>
29793fc096SFrank Li #include <linux/ptrace.h>
30793fc096SFrank Li #include <linux/errno.h>
31793fc096SFrank Li #include <linux/ioport.h>
32793fc096SFrank Li #include <linux/slab.h>
33793fc096SFrank Li #include <linux/interrupt.h>
34793fc096SFrank Li #include <linux/delay.h>
35793fc096SFrank Li #include <linux/netdevice.h>
36793fc096SFrank Li #include <linux/etherdevice.h>
37793fc096SFrank Li #include <linux/skbuff.h>
384c09eed9SJim Baxter #include <linux/in.h>
394c09eed9SJim Baxter #include <linux/ip.h>
404c09eed9SJim Baxter #include <net/ip.h>
416016ba34SOleksij Rempel #include <net/selftests.h>
4279f33912SNimrod Andy #include <net/tso.h>
434c09eed9SJim Baxter #include <linux/tcp.h>
444c09eed9SJim Baxter #include <linux/udp.h>
454c09eed9SJim Baxter #include <linux/icmp.h>
46793fc096SFrank Li #include <linux/spinlock.h>
47793fc096SFrank Li #include <linux/workqueue.h>
48793fc096SFrank Li #include <linux/bitops.h>
49793fc096SFrank Li #include <linux/io.h>
50793fc096SFrank Li #include <linux/irq.h>
51793fc096SFrank Li #include <linux/clk.h>
5216f6e983SKrzysztof Kozlowski #include <linux/crc32.h>
53793fc096SFrank Li #include <linux/platform_device.h>
547f854420SAndrew Lunn #include <linux/mdio.h>
55793fc096SFrank Li #include <linux/phy.h>
56793fc096SFrank Li #include <linux/fec.h>
57793fc096SFrank Li #include <linux/of.h>
58793fc096SFrank Li #include <linux/of_device.h>
59793fc096SFrank Li #include <linux/of_gpio.h>
60407066f8SUwe Kleine-König #include <linux/of_mdio.h>
61793fc096SFrank Li #include <linux/of_net.h>
62793fc096SFrank Li #include <linux/regulator/consumer.h>
63cdffcf1bSJim Baxter #include <linux/if_vlan.h>
64a68ab98eSFabio Estevam #include <linux/pinctrl/consumer.h>
65c259c132SFrank Li #include <linux/prefetch.h>
66da722186SMartin Fuzzey #include <linux/mfd/syscon.h>
67da722186SMartin Fuzzey #include <linux/regmap.h>
6829380905SLucas Stach #include <soc/imx/cpuidle.h>
69793fc096SFrank Li 
70793fc096SFrank Li #include <asm/cacheflush.h>
71793fc096SFrank Li 
72793fc096SFrank Li #include "fec.h"
73793fc096SFrank Li 
74772e42b0SChristoph Müllner static void set_multicast_list(struct net_device *ndev);
75d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev);
76772e42b0SChristoph Müllner 
77793fc096SFrank Li #define DRIVER_NAME	"fec"
78793fc096SFrank Li 
7952c4a1a8SFugang Duan static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2};
8052c4a1a8SFugang Duan 
81793fc096SFrank Li /* Pause frame feild and FIFO threshold */
82793fc096SFrank Li #define FEC_ENET_FCE	(1 << 5)
83793fc096SFrank Li #define FEC_ENET_RSEM_V	0x84
84793fc096SFrank Li #define FEC_ENET_RSFL_V	16
85793fc096SFrank Li #define FEC_ENET_RAEM_V	0x8
86793fc096SFrank Li #define FEC_ENET_RAFL_V	0x8
87793fc096SFrank Li #define FEC_ENET_OPD_V	0xFFF0
888fff755eSAndrew Lunn #define FEC_MDIO_PM_TIMEOUT  100 /* ms */
89793fc096SFrank Li 
90da722186SMartin Fuzzey struct fec_devinfo {
91da722186SMartin Fuzzey 	u32 quirks;
92da722186SMartin Fuzzey };
93da722186SMartin Fuzzey 
94da722186SMartin Fuzzey static const struct fec_devinfo fec_imx25_info = {
95da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
96da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_FRREG,
97da722186SMartin Fuzzey };
98da722186SMartin Fuzzey 
99da722186SMartin Fuzzey static const struct fec_devinfo fec_imx27_info = {
100da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG,
101da722186SMartin Fuzzey };
102da722186SMartin Fuzzey 
103da722186SMartin Fuzzey static const struct fec_devinfo fec_imx28_info = {
104da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
105da722186SMartin Fuzzey 		  FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
106c730ab42SLaurent Badel 		  FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII |
107c730ab42SLaurent Badel 		  FEC_QUIRK_NO_HARD_RESET,
108da722186SMartin Fuzzey };
109da722186SMartin Fuzzey 
110da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6q_info = {
111da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
112da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
113da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
1147d650df9SWei Fang 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII |
1157d650df9SWei Fang 		  FEC_QUIRK_HAS_PMQOS,
116da722186SMartin Fuzzey };
117da722186SMartin Fuzzey 
118da722186SMartin Fuzzey static const struct fec_devinfo fec_mvf600_info = {
119da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
120da722186SMartin Fuzzey };
121da722186SMartin Fuzzey 
122da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6x_info = {
123da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
124da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
125da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
126da722186SMartin Fuzzey 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
1271e6114f5SGreg Ungerer 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
128471ff445SJoakim Zhang 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES,
129da722186SMartin Fuzzey };
130da722186SMartin Fuzzey 
131da722186SMartin Fuzzey static const struct fec_devinfo fec_imx6ul_info = {
132da722186SMartin Fuzzey 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
133da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
134da722186SMartin Fuzzey 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
135da722186SMartin Fuzzey 		  FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
1361e6114f5SGreg Ungerer 		  FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII,
137da722186SMartin Fuzzey };
138da722186SMartin Fuzzey 
139947240ebSFugang Duan static const struct fec_devinfo fec_imx8mq_info = {
140947240ebSFugang Duan 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
141947240ebSFugang Duan 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
142947240ebSFugang Duan 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
143947240ebSFugang Duan 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
144947240ebSFugang Duan 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
145947240ebSFugang Duan 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
146b7cdc965SJoakim Zhang 		  FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2,
147947240ebSFugang Duan };
148947240ebSFugang Duan 
149947240ebSFugang Duan static const struct fec_devinfo fec_imx8qm_info = {
150947240ebSFugang Duan 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
151947240ebSFugang Duan 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
152947240ebSFugang Duan 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
153947240ebSFugang Duan 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
154947240ebSFugang Duan 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
155947240ebSFugang Duan 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
156947240ebSFugang Duan 		  FEC_QUIRK_DELAYED_CLKS_SUPPORT,
157947240ebSFugang Duan };
158947240ebSFugang Duan 
159167d5fe0SWei Fang static const struct fec_devinfo fec_s32v234_info = {
160167d5fe0SWei Fang 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
161167d5fe0SWei Fang 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
162167d5fe0SWei Fang 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
163167d5fe0SWei Fang 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE,
164167d5fe0SWei Fang };
165167d5fe0SWei Fang 
166793fc096SFrank Li static struct platform_device_id fec_devtype[] = {
167793fc096SFrank Li 	{
168793fc096SFrank Li 		/* keep it for coldfire */
169793fc096SFrank Li 		.name = DRIVER_NAME,
170793fc096SFrank Li 		.driver_data = 0,
171793fc096SFrank Li 	}, {
172793fc096SFrank Li 		.name = "imx25-fec",
173da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx25_info,
174793fc096SFrank Li 	}, {
175793fc096SFrank Li 		.name = "imx27-fec",
176da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx27_info,
177793fc096SFrank Li 	}, {
178793fc096SFrank Li 		.name = "imx28-fec",
179da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx28_info,
180793fc096SFrank Li 	}, {
181793fc096SFrank Li 		.name = "imx6q-fec",
182da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6q_info,
183793fc096SFrank Li 	}, {
18436803542SShawn Guo 		.name = "mvf600-fec",
185da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_mvf600_info,
186ca7c4a45SJingchang Lu 	}, {
18795a77470SFugang Duan 		.name = "imx6sx-fec",
188da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6x_info,
18995a77470SFugang Duan 	}, {
190a51d3ab5SFugang Duan 		.name = "imx6ul-fec",
191da722186SMartin Fuzzey 		.driver_data = (kernel_ulong_t)&fec_imx6ul_info,
192a51d3ab5SFugang Duan 	}, {
193947240ebSFugang Duan 		.name = "imx8mq-fec",
194947240ebSFugang Duan 		.driver_data = (kernel_ulong_t)&fec_imx8mq_info,
195947240ebSFugang Duan 	}, {
196947240ebSFugang Duan 		.name = "imx8qm-fec",
197947240ebSFugang Duan 		.driver_data = (kernel_ulong_t)&fec_imx8qm_info,
198947240ebSFugang Duan 	}, {
199167d5fe0SWei Fang 		.name = "s32v234-fec",
200167d5fe0SWei Fang 		.driver_data = (kernel_ulong_t)&fec_s32v234_info,
201167d5fe0SWei Fang 	}, {
202793fc096SFrank Li 		/* sentinel */
203793fc096SFrank Li 	}
204793fc096SFrank Li };
205793fc096SFrank Li MODULE_DEVICE_TABLE(platform, fec_devtype);
206793fc096SFrank Li 
207793fc096SFrank Li enum imx_fec_type {
208793fc096SFrank Li 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
209793fc096SFrank Li 	IMX27_FEC,	/* runs on i.mx27/35/51 */
210793fc096SFrank Li 	IMX28_FEC,
211793fc096SFrank Li 	IMX6Q_FEC,
21236803542SShawn Guo 	MVF600_FEC,
213ba593e00SFugang Duan 	IMX6SX_FEC,
214a51d3ab5SFugang Duan 	IMX6UL_FEC,
215947240ebSFugang Duan 	IMX8MQ_FEC,
216947240ebSFugang Duan 	IMX8QM_FEC,
217167d5fe0SWei Fang 	S32V234_FEC,
218793fc096SFrank Li };
219793fc096SFrank Li 
220793fc096SFrank Li static const struct of_device_id fec_dt_ids[] = {
221793fc096SFrank Li 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
222793fc096SFrank Li 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
223793fc096SFrank Li 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
224793fc096SFrank Li 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
22536803542SShawn Guo 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
226ba593e00SFugang Duan 	{ .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
227a51d3ab5SFugang Duan 	{ .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
228947240ebSFugang Duan 	{ .compatible = "fsl,imx8mq-fec", .data = &fec_devtype[IMX8MQ_FEC], },
229947240ebSFugang Duan 	{ .compatible = "fsl,imx8qm-fec", .data = &fec_devtype[IMX8QM_FEC], },
230167d5fe0SWei Fang 	{ .compatible = "fsl,s32v234-fec", .data = &fec_devtype[S32V234_FEC], },
231793fc096SFrank Li 	{ /* sentinel */ }
232793fc096SFrank Li };
233793fc096SFrank Li MODULE_DEVICE_TABLE(of, fec_dt_ids);
234793fc096SFrank Li 
235793fc096SFrank Li static unsigned char macaddr[ETH_ALEN];
236793fc096SFrank Li module_param_array(macaddr, byte, NULL, 0);
237793fc096SFrank Li MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
238793fc096SFrank Li 
239793fc096SFrank Li #if defined(CONFIG_M5272)
240793fc096SFrank Li /*
241793fc096SFrank Li  * Some hardware gets it MAC address out of local flash memory.
242793fc096SFrank Li  * if this is non-zero then assume it is the address to get MAC from.
243793fc096SFrank Li  */
244793fc096SFrank Li #if defined(CONFIG_NETtel)
245793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006006
246793fc096SFrank Li #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
247793fc096SFrank Li #define	FEC_FLASHMAC	0xf0006000
248793fc096SFrank Li #elif defined(CONFIG_CANCam)
249793fc096SFrank Li #define	FEC_FLASHMAC	0xf0020000
250793fc096SFrank Li #elif defined (CONFIG_M5272C3)
251793fc096SFrank Li #define	FEC_FLASHMAC	(0xffe04000 + 4)
252793fc096SFrank Li #elif defined(CONFIG_MOD5272)
253793fc096SFrank Li #define FEC_FLASHMAC	0xffc0406b
254793fc096SFrank Li #else
255793fc096SFrank Li #define	FEC_FLASHMAC	0
256793fc096SFrank Li #endif
257793fc096SFrank Li #endif /* CONFIG_M5272 */
258793fc096SFrank Li 
259cdffcf1bSJim Baxter /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
260fbbeefddSAndrew Lunn  *
261fbbeefddSAndrew Lunn  * 2048 byte skbufs are allocated. However, alignment requirements
262fbbeefddSAndrew Lunn  * varies between FEC variants. Worst case is 64, so round down by 64.
263793fc096SFrank Li  */
264fbbeefddSAndrew Lunn #define PKT_MAXBUF_SIZE		(round_down(2048 - 64, 64))
265793fc096SFrank Li #define PKT_MINBUF_SIZE		64
266793fc096SFrank Li 
2674c09eed9SJim Baxter /* FEC receive acceleration */
2684c09eed9SJim Baxter #define FEC_RACC_IPDIS		(1 << 1)
2694c09eed9SJim Baxter #define FEC_RACC_PRODIS		(1 << 2)
2703ac72b7bSEric Nelson #define FEC_RACC_SHIFT16	BIT(7)
2714c09eed9SJim Baxter #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
2724c09eed9SJim Baxter 
2732b30842bSAndrew Lunn /* MIB Control Register */
2742b30842bSAndrew Lunn #define FEC_MIB_CTRLSTAT_DISABLE	BIT(31)
2752b30842bSAndrew Lunn 
276793fc096SFrank Li /*
277793fc096SFrank Li  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
278793fc096SFrank Li  * size bits. Other FEC hardware does not, so we need to take that into
279793fc096SFrank Li  * account when setting it.
280793fc096SFrank Li  */
281793fc096SFrank Li #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2823f1dcc6aSLucas Stach     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
2833f1dcc6aSLucas Stach     defined(CONFIG_ARM64)
284793fc096SFrank Li #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
285793fc096SFrank Li #else
286793fc096SFrank Li #define	OPT_FRAME_SIZE	0
287793fc096SFrank Li #endif
288793fc096SFrank Li 
289793fc096SFrank Li /* FEC MII MMFR bits definition */
290793fc096SFrank Li #define FEC_MMFR_ST		(1 << 30)
291d3ee8ec7SMarco Hartmann #define FEC_MMFR_ST_C45		(0)
292793fc096SFrank Li #define FEC_MMFR_OP_READ	(2 << 28)
293d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_READ_C45	(3 << 28)
294793fc096SFrank Li #define FEC_MMFR_OP_WRITE	(1 << 28)
295d3ee8ec7SMarco Hartmann #define FEC_MMFR_OP_ADDR_WRITE	(0)
296793fc096SFrank Li #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
297793fc096SFrank Li #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
298793fc096SFrank Li #define FEC_MMFR_TA		(2 << 16)
299793fc096SFrank Li #define FEC_MMFR_DATA(v)	(v & 0xffff)
300de40ed31SNimrod Andy /* FEC ECR bits definition */
3017b15515fSFrancesco Dolcini #define FEC_ECR_MAGICEN		(1 << 2)
3027b15515fSFrancesco Dolcini #define FEC_ECR_SLEEP		(1 << 3)
303793fc096SFrank Li 
304793fc096SFrank Li #define FEC_MII_TIMEOUT		30000 /* us */
305793fc096SFrank Li 
306793fc096SFrank Li /* Transmitter timeout */
307793fc096SFrank Li #define TX_TIMEOUT (2 * HZ)
308793fc096SFrank Li 
309793fc096SFrank Li #define FEC_PAUSE_FLAG_AUTONEG	0x1
310793fc096SFrank Li #define FEC_PAUSE_FLAG_ENABLE	0x2
311de40ed31SNimrod Andy #define FEC_WOL_HAS_MAGIC_PACKET	(0x1 << 0)
312de40ed31SNimrod Andy #define FEC_WOL_FLAG_ENABLE		(0x1 << 1)
313de40ed31SNimrod Andy #define FEC_WOL_FLAG_SLEEP_ON		(0x1 << 2)
314793fc096SFrank Li 
3151b7bde6dSNimrod Andy #define COPYBREAK_DEFAULT	256
3161b7bde6dSNimrod Andy 
31779f33912SNimrod Andy /* Max number of allowed TCP segments for software TSO */
31879f33912SNimrod Andy #define FEC_MAX_TSO_SEGS	100
31979f33912SNimrod Andy #define FEC_MAX_SKB_DESCS	(FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
32079f33912SNimrod Andy 
32179f33912SNimrod Andy #define IS_TSO_HEADER(txq, addr) \
32279f33912SNimrod Andy 	((addr >= txq->tso_hdrs_dma) && \
3237355f276STroy Kisky 	(addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
32479f33912SNimrod Andy 
325793fc096SFrank Li static int mii_cnt;
326793fc096SFrank Li 
3277355f276STroy Kisky static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
3287355f276STroy Kisky 					     struct bufdesc_prop *bd)
329793fc096SFrank Li {
3307355f276STroy Kisky 	return (bdp >= bd->last) ? bd->base
331145d6e29SFugang Duan 			: (struct bufdesc *)(((void *)bdp) + bd->dsize);
332793fc096SFrank Li }
333793fc096SFrank Li 
3347355f276STroy Kisky static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
3357355f276STroy Kisky 					     struct bufdesc_prop *bd)
33636e24e2eSDuan Fugang-B38611 {
3377355f276STroy Kisky 	return (bdp <= bd->base) ? bd->last
338145d6e29SFugang Duan 			: (struct bufdesc *)(((void *)bdp) - bd->dsize);
33936e24e2eSDuan Fugang-B38611 }
34036e24e2eSDuan Fugang-B38611 
3417355f276STroy Kisky static int fec_enet_get_bd_index(struct bufdesc *bdp,
3427355f276STroy Kisky 				 struct bufdesc_prop *bd)
34361a4427bSNimrod Andy {
3447355f276STroy Kisky 	return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
34561a4427bSNimrod Andy }
34661a4427bSNimrod Andy 
3477355f276STroy Kisky static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
3486e909283SNimrod Andy {
3496e909283SNimrod Andy 	int entries;
3506e909283SNimrod Andy 
3517355f276STroy Kisky 	entries = (((const char *)txq->dirty_tx -
3527355f276STroy Kisky 			(const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
3536e909283SNimrod Andy 
3547355f276STroy Kisky 	return entries >= 0 ? entries : entries + txq->bd.ring_size;
3556e909283SNimrod Andy }
3566e909283SNimrod Andy 
357c20e599bSLothar Waßmann static void swap_buffer(void *bufaddr, int len)
358793fc096SFrank Li {
359793fc096SFrank Li 	int i;
360793fc096SFrank Li 	unsigned int *buf = bufaddr;
361793fc096SFrank Li 
3627b487d07SLothar Waßmann 	for (i = 0; i < len; i += 4, buf++)
363e453789aSLothar Waßmann 		swab32s(buf);
364793fc096SFrank Li }
365793fc096SFrank Li 
3661310b544SLothar Waßmann static void swap_buffer2(void *dst_buf, void *src_buf, int len)
3671310b544SLothar Waßmann {
3681310b544SLothar Waßmann 	int i;
3691310b544SLothar Waßmann 	unsigned int *src = src_buf;
3701310b544SLothar Waßmann 	unsigned int *dst = dst_buf;
3711310b544SLothar Waßmann 
3721310b544SLothar Waßmann 	for (i = 0; i < len; i += 4, src++, dst++)
3731310b544SLothar Waßmann 		*dst = swab32p(src);
3741310b544SLothar Waßmann }
3751310b544SLothar Waßmann 
376344756f6SRussell King static void fec_dump(struct net_device *ndev)
377344756f6SRussell King {
378344756f6SRussell King 	struct fec_enet_private *fep = netdev_priv(ndev);
3794d494cdcSFugang Duan 	struct bufdesc *bdp;
3804d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
3814d494cdcSFugang Duan 	int index = 0;
382344756f6SRussell King 
383344756f6SRussell King 	netdev_info(ndev, "TX ring dump\n");
384344756f6SRussell King 	pr_info("Nr     SC     addr       len  SKB\n");
385344756f6SRussell King 
3864d494cdcSFugang Duan 	txq = fep->tx_queue[0];
3877355f276STroy Kisky 	bdp = txq->bd.base;
3884d494cdcSFugang Duan 
389344756f6SRussell King 	do {
3905cfa3039SJohannes Berg 		pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
391344756f6SRussell King 			index,
3927355f276STroy Kisky 			bdp == txq->bd.cur ? 'S' : ' ',
3934d494cdcSFugang Duan 			bdp == txq->dirty_tx ? 'H' : ' ',
3945cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_sc),
3955cfa3039SJohannes Berg 			fec32_to_cpu(bdp->cbd_bufaddr),
3965cfa3039SJohannes Berg 			fec16_to_cpu(bdp->cbd_datlen),
3974d494cdcSFugang Duan 			txq->tx_skbuff[index]);
3987355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
399344756f6SRussell King 		index++;
4007355f276STroy Kisky 	} while (bdp != txq->bd.base);
401344756f6SRussell King }
402344756f6SRussell King 
40362a02c98SFugang Duan static inline bool is_ipv4_pkt(struct sk_buff *skb)
40462a02c98SFugang Duan {
40562a02c98SFugang Duan 	return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
40662a02c98SFugang Duan }
40762a02c98SFugang Duan 
4084c09eed9SJim Baxter static int
4094c09eed9SJim Baxter fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
4104c09eed9SJim Baxter {
4114c09eed9SJim Baxter 	/* Only run for packets requiring a checksum. */
4124c09eed9SJim Baxter 	if (skb->ip_summed != CHECKSUM_PARTIAL)
4134c09eed9SJim Baxter 		return 0;
4144c09eed9SJim Baxter 
4154c09eed9SJim Baxter 	if (unlikely(skb_cow_head(skb, 0)))
4164c09eed9SJim Baxter 		return -1;
4174c09eed9SJim Baxter 
41862a02c98SFugang Duan 	if (is_ipv4_pkt(skb))
41996c50caaSNimrod Andy 		ip_hdr(skb)->check = 0;
4204c09eed9SJim Baxter 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
4214c09eed9SJim Baxter 
4224c09eed9SJim Baxter 	return 0;
4234c09eed9SJim Baxter }
4244c09eed9SJim Baxter 
425c4bc44c6SKevin Hao static struct bufdesc *
4264d494cdcSFugang Duan fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
4274d494cdcSFugang Duan 			     struct sk_buff *skb,
4284d494cdcSFugang Duan 			     struct net_device *ndev)
4296e909283SNimrod Andy {
4306e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
4317355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
4326e909283SNimrod Andy 	struct bufdesc_ex *ebdp;
4336e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
4346e909283SNimrod Andy 	int frag, frag_len;
4356e909283SNimrod Andy 	unsigned short status;
4366e909283SNimrod Andy 	unsigned int estatus = 0;
4376e909283SNimrod Andy 	skb_frag_t *this_frag;
4386e909283SNimrod Andy 	unsigned int index;
4396e909283SNimrod Andy 	void *bufaddr;
440d6bf3143SRussell King 	dma_addr_t addr;
4416e909283SNimrod Andy 	int i;
4426e909283SNimrod Andy 
4436e909283SNimrod Andy 	for (frag = 0; frag < nr_frags; frag++) {
4446e909283SNimrod Andy 		this_frag = &skb_shinfo(skb)->frags[frag];
4457355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
4466e909283SNimrod Andy 		ebdp = (struct bufdesc_ex *)bdp;
4476e909283SNimrod Andy 
4485cfa3039SJohannes Berg 		status = fec16_to_cpu(bdp->cbd_sc);
4496e909283SNimrod Andy 		status &= ~BD_ENET_TX_STATS;
4506e909283SNimrod Andy 		status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
451d7840976SMatthew Wilcox (Oracle) 		frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
4526e909283SNimrod Andy 
4536e909283SNimrod Andy 		/* Handle the last BD specially */
4546e909283SNimrod Andy 		if (frag == nr_frags - 1) {
4556e909283SNimrod Andy 			status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
4566e909283SNimrod Andy 			if (fep->bufdesc_ex) {
4576e909283SNimrod Andy 				estatus |= BD_ENET_TX_INT;
4586e909283SNimrod Andy 				if (unlikely(skb_shinfo(skb)->tx_flags &
4596e909283SNimrod Andy 					SKBTX_HW_TSTAMP && fep->hwts_tx_en))
4606e909283SNimrod Andy 					estatus |= BD_ENET_TX_TS;
4616e909283SNimrod Andy 			}
4626e909283SNimrod Andy 		}
4636e909283SNimrod Andy 
4646e909283SNimrod Andy 		if (fep->bufdesc_ex) {
4656b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_HAS_AVB)
46653bb20d1STroy Kisky 				estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
4676e909283SNimrod Andy 			if (skb->ip_summed == CHECKSUM_PARTIAL)
4686e909283SNimrod Andy 				estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
469471ff445SJoakim Zhang 
4706e909283SNimrod Andy 			ebdp->cbd_bdu = 0;
4715cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(estatus);
4726e909283SNimrod Andy 		}
4736e909283SNimrod Andy 
474d7840976SMatthew Wilcox (Oracle) 		bufaddr = skb_frag_address(this_frag);
4756e909283SNimrod Andy 
4767355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
47741ef84ceSFugang Duan 		if (((unsigned long) bufaddr) & fep->tx_align ||
4786b7e4008SLothar Waßmann 			fep->quirks & FEC_QUIRK_SWAP_FRAME) {
4794d494cdcSFugang Duan 			memcpy(txq->tx_bounce[index], bufaddr, frag_len);
4804d494cdcSFugang Duan 			bufaddr = txq->tx_bounce[index];
4816e909283SNimrod Andy 
4826b7e4008SLothar Waßmann 			if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
4836e909283SNimrod Andy 				swap_buffer(bufaddr, frag_len);
4846e909283SNimrod Andy 		}
4856e909283SNimrod Andy 
486d6bf3143SRussell King 		addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
487d6bf3143SRussell King 				      DMA_TO_DEVICE);
488d6bf3143SRussell King 		if (dma_mapping_error(&fep->pdev->dev, addr)) {
4896e909283SNimrod Andy 			if (net_ratelimit())
4906e909283SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
4916e909283SNimrod Andy 			goto dma_mapping_error;
4926e909283SNimrod Andy 		}
4936e909283SNimrod Andy 
4945cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(addr);
4955cfa3039SJohannes Berg 		bdp->cbd_datlen = cpu_to_fec16(frag_len);
496be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
497be293467STroy Kisky 		 * performed before transferring ownership.
498be293467STroy Kisky 		 */
499be293467STroy Kisky 		wmb();
5005cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(status);
5016e909283SNimrod Andy 	}
5026e909283SNimrod Andy 
503c4bc44c6SKevin Hao 	return bdp;
5046e909283SNimrod Andy dma_mapping_error:
5057355f276STroy Kisky 	bdp = txq->bd.cur;
5066e909283SNimrod Andy 	for (i = 0; i < frag; i++) {
5077355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
5085cfa3039SJohannes Berg 		dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
5095cfa3039SJohannes Berg 				 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
5106e909283SNimrod Andy 	}
511c4bc44c6SKevin Hao 	return ERR_PTR(-ENOMEM);
5126e909283SNimrod Andy }
5136e909283SNimrod Andy 
5144d494cdcSFugang Duan static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
5154d494cdcSFugang Duan 				   struct sk_buff *skb, struct net_device *ndev)
5166e909283SNimrod Andy {
5176e909283SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
5186e909283SNimrod Andy 	int nr_frags = skb_shinfo(skb)->nr_frags;
5196e909283SNimrod Andy 	struct bufdesc *bdp, *last_bdp;
5206e909283SNimrod Andy 	void *bufaddr;
521d6bf3143SRussell King 	dma_addr_t addr;
5226e909283SNimrod Andy 	unsigned short status;
5236e909283SNimrod Andy 	unsigned short buflen;
5246e909283SNimrod Andy 	unsigned int estatus = 0;
5256e909283SNimrod Andy 	unsigned int index;
52679f33912SNimrod Andy 	int entries_free;
5276e909283SNimrod Andy 
5287355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
52979f33912SNimrod Andy 	if (entries_free < MAX_SKB_FRAGS + 1) {
53079f33912SNimrod Andy 		dev_kfree_skb_any(skb);
53179f33912SNimrod Andy 		if (net_ratelimit())
53279f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for SG!\n");
53379f33912SNimrod Andy 		return NETDEV_TX_OK;
53479f33912SNimrod Andy 	}
53579f33912SNimrod Andy 
5366e909283SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
5376e909283SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
5386e909283SNimrod Andy 		dev_kfree_skb_any(skb);
5396e909283SNimrod Andy 		return NETDEV_TX_OK;
5406e909283SNimrod Andy 	}
5416e909283SNimrod Andy 
5426e909283SNimrod Andy 	/* Fill in a Tx ring entry */
5437355f276STroy Kisky 	bdp = txq->bd.cur;
544c4bc44c6SKevin Hao 	last_bdp = bdp;
5455cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
5466e909283SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
5476e909283SNimrod Andy 
5486e909283SNimrod Andy 	/* Set buffer length and buffer pointer */
5496e909283SNimrod Andy 	bufaddr = skb->data;
5506e909283SNimrod Andy 	buflen = skb_headlen(skb);
5516e909283SNimrod Andy 
5527355f276STroy Kisky 	index = fec_enet_get_bd_index(bdp, &txq->bd);
55341ef84ceSFugang Duan 	if (((unsigned long) bufaddr) & fep->tx_align ||
5546b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
5554d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, buflen);
5564d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
5576e909283SNimrod Andy 
5586b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
5596e909283SNimrod Andy 			swap_buffer(bufaddr, buflen);
5606e909283SNimrod Andy 	}
5616e909283SNimrod Andy 
562d6bf3143SRussell King 	/* Push the data cache so the CPM does not get stale memory data. */
563d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
564d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
5656e909283SNimrod Andy 		dev_kfree_skb_any(skb);
5666e909283SNimrod Andy 		if (net_ratelimit())
5676e909283SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
5686e909283SNimrod Andy 		return NETDEV_TX_OK;
5696e909283SNimrod Andy 	}
5706e909283SNimrod Andy 
5716e909283SNimrod Andy 	if (nr_frags) {
572c4bc44c6SKevin Hao 		last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
573fc75ba51STroy Kisky 		if (IS_ERR(last_bdp)) {
574fc75ba51STroy Kisky 			dma_unmap_single(&fep->pdev->dev, addr,
575fc75ba51STroy Kisky 					 buflen, DMA_TO_DEVICE);
576fc75ba51STroy Kisky 			dev_kfree_skb_any(skb);
577c4bc44c6SKevin Hao 			return NETDEV_TX_OK;
578fc75ba51STroy Kisky 		}
5796e909283SNimrod Andy 	} else {
5806e909283SNimrod Andy 		status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
5816e909283SNimrod Andy 		if (fep->bufdesc_ex) {
5826e909283SNimrod Andy 			estatus = BD_ENET_TX_INT;
5836e909283SNimrod Andy 			if (unlikely(skb_shinfo(skb)->tx_flags &
5846e909283SNimrod Andy 				SKBTX_HW_TSTAMP && fep->hwts_tx_en))
5856e909283SNimrod Andy 				estatus |= BD_ENET_TX_TS;
5866e909283SNimrod Andy 		}
5876e909283SNimrod Andy 	}
588fc75ba51STroy Kisky 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
589fc75ba51STroy Kisky 	bdp->cbd_datlen = cpu_to_fec16(buflen);
5906e909283SNimrod Andy 
5916e909283SNimrod Andy 	if (fep->bufdesc_ex) {
5926e909283SNimrod Andy 
5936e909283SNimrod Andy 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
5946e909283SNimrod Andy 
5956e909283SNimrod Andy 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
5966e909283SNimrod Andy 			fep->hwts_tx_en))
5976e909283SNimrod Andy 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
5986e909283SNimrod Andy 
5996b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
60053bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
601befe8213SNimrod Andy 
6026e909283SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
6036e909283SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
6046e909283SNimrod Andy 
6056e909283SNimrod Andy 		ebdp->cbd_bdu = 0;
6065cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
6076e909283SNimrod Andy 	}
6086e909283SNimrod Andy 
6097355f276STroy Kisky 	index = fec_enet_get_bd_index(last_bdp, &txq->bd);
6106e909283SNimrod Andy 	/* Save skb pointer */
6114d494cdcSFugang Duan 	txq->tx_skbuff[index] = skb;
6126e909283SNimrod Andy 
613be293467STroy Kisky 	/* Make sure the updates to rest of the descriptor are performed before
614be293467STroy Kisky 	 * transferring ownership.
615be293467STroy Kisky 	 */
616be293467STroy Kisky 	wmb();
6176e909283SNimrod Andy 
6186e909283SNimrod Andy 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
6196e909283SNimrod Andy 	 * it's the last BD of the frame, and to put the CRC on the end.
6206e909283SNimrod Andy 	 */
6216e909283SNimrod Andy 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
6225cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
6236e909283SNimrod Andy 
624793fc096SFrank Li 	/* If this was the last BD in the ring, start at the beginning again. */
6257355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
626793fc096SFrank Li 
6277a2a8451SEric Dumazet 	skb_tx_timestamp(skb);
6287a2a8451SEric Dumazet 
629c4bc44c6SKevin Hao 	/* Make sure the update to bdp and tx_skbuff are performed before
6307355f276STroy Kisky 	 * txq->bd.cur.
631c4bc44c6SKevin Hao 	 */
632c4bc44c6SKevin Hao 	wmb();
6337355f276STroy Kisky 	txq->bd.cur = bdp;
634793fc096SFrank Li 
635793fc096SFrank Li 	/* Trigger transmission start */
63653bb20d1STroy Kisky 	writel(0, txq->bd.reg_desc_active);
637793fc096SFrank Li 
6386e909283SNimrod Andy 	return 0;
639793fc096SFrank Li }
640793fc096SFrank Li 
64179f33912SNimrod Andy static int
6424d494cdcSFugang Duan fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
6434d494cdcSFugang Duan 			  struct net_device *ndev,
64479f33912SNimrod Andy 			  struct bufdesc *bdp, int index, char *data,
64579f33912SNimrod Andy 			  int size, bool last_tcp, bool is_last)
64679f33912SNimrod Andy {
64779f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
64861cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
64979f33912SNimrod Andy 	unsigned short status;
65079f33912SNimrod Andy 	unsigned int estatus = 0;
651d6bf3143SRussell King 	dma_addr_t addr;
65279f33912SNimrod Andy 
6535cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
65479f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
65579f33912SNimrod Andy 
65679f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
65779f33912SNimrod Andy 
65841ef84ceSFugang Duan 	if (((unsigned long) data) & fep->tx_align ||
6596b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
6604d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], data, size);
6614d494cdcSFugang Duan 		data = txq->tx_bounce[index];
66279f33912SNimrod Andy 
6636b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
66479f33912SNimrod Andy 			swap_buffer(data, size);
66579f33912SNimrod Andy 	}
66679f33912SNimrod Andy 
667d6bf3143SRussell King 	addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
668d6bf3143SRussell King 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
66979f33912SNimrod Andy 		dev_kfree_skb_any(skb);
67079f33912SNimrod Andy 		if (net_ratelimit())
67179f33912SNimrod Andy 			netdev_err(ndev, "Tx DMA memory map failed\n");
67279f33912SNimrod Andy 		return NETDEV_TX_BUSY;
67379f33912SNimrod Andy 	}
67479f33912SNimrod Andy 
6755cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(size);
6765cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
677d6bf3143SRussell King 
67879f33912SNimrod Andy 	if (fep->bufdesc_ex) {
6796b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
68053bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
68179f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
68279f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
68379f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
6845cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
68579f33912SNimrod Andy 	}
68679f33912SNimrod Andy 
68779f33912SNimrod Andy 	/* Handle the last BD specially */
68879f33912SNimrod Andy 	if (last_tcp)
68979f33912SNimrod Andy 		status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
69079f33912SNimrod Andy 	if (is_last) {
69179f33912SNimrod Andy 		status |= BD_ENET_TX_INTR;
69279f33912SNimrod Andy 		if (fep->bufdesc_ex)
6935cfa3039SJohannes Berg 			ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
69479f33912SNimrod Andy 	}
69579f33912SNimrod Andy 
6965cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
69779f33912SNimrod Andy 
69879f33912SNimrod Andy 	return 0;
69979f33912SNimrod Andy }
70079f33912SNimrod Andy 
70179f33912SNimrod Andy static int
7024d494cdcSFugang Duan fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
7034d494cdcSFugang Duan 			 struct sk_buff *skb, struct net_device *ndev,
70479f33912SNimrod Andy 			 struct bufdesc *bdp, int index)
70579f33912SNimrod Andy {
70679f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
707504148feSEric Dumazet 	int hdr_len = skb_tcp_all_headers(skb);
70861cd2ebbSFabian Frederick 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
70979f33912SNimrod Andy 	void *bufaddr;
71079f33912SNimrod Andy 	unsigned long dmabuf;
71179f33912SNimrod Andy 	unsigned short status;
71279f33912SNimrod Andy 	unsigned int estatus = 0;
71379f33912SNimrod Andy 
7145cfa3039SJohannes Berg 	status = fec16_to_cpu(bdp->cbd_sc);
71579f33912SNimrod Andy 	status &= ~BD_ENET_TX_STATS;
71679f33912SNimrod Andy 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
71779f33912SNimrod Andy 
7184d494cdcSFugang Duan 	bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
7194d494cdcSFugang Duan 	dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
72041ef84ceSFugang Duan 	if (((unsigned long)bufaddr) & fep->tx_align ||
7216b7e4008SLothar Waßmann 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
7224d494cdcSFugang Duan 		memcpy(txq->tx_bounce[index], skb->data, hdr_len);
7234d494cdcSFugang Duan 		bufaddr = txq->tx_bounce[index];
72479f33912SNimrod Andy 
7256b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
72679f33912SNimrod Andy 			swap_buffer(bufaddr, hdr_len);
72779f33912SNimrod Andy 
72879f33912SNimrod Andy 		dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
72979f33912SNimrod Andy 					hdr_len, DMA_TO_DEVICE);
73079f33912SNimrod Andy 		if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
73179f33912SNimrod Andy 			dev_kfree_skb_any(skb);
73279f33912SNimrod Andy 			if (net_ratelimit())
73379f33912SNimrod Andy 				netdev_err(ndev, "Tx DMA memory map failed\n");
73479f33912SNimrod Andy 			return NETDEV_TX_BUSY;
73579f33912SNimrod Andy 		}
73679f33912SNimrod Andy 	}
73779f33912SNimrod Andy 
7385cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
7395cfa3039SJohannes Berg 	bdp->cbd_datlen = cpu_to_fec16(hdr_len);
74079f33912SNimrod Andy 
74179f33912SNimrod Andy 	if (fep->bufdesc_ex) {
7426b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
74353bb20d1STroy Kisky 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
74479f33912SNimrod Andy 		if (skb->ip_summed == CHECKSUM_PARTIAL)
74579f33912SNimrod Andy 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
74679f33912SNimrod Andy 		ebdp->cbd_bdu = 0;
7475cfa3039SJohannes Berg 		ebdp->cbd_esc = cpu_to_fec32(estatus);
74879f33912SNimrod Andy 	}
74979f33912SNimrod Andy 
7505cfa3039SJohannes Berg 	bdp->cbd_sc = cpu_to_fec16(status);
75179f33912SNimrod Andy 
75279f33912SNimrod Andy 	return 0;
75379f33912SNimrod Andy }
75479f33912SNimrod Andy 
7554d494cdcSFugang Duan static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
7564d494cdcSFugang Duan 				   struct sk_buff *skb,
7574d494cdcSFugang Duan 				   struct net_device *ndev)
75879f33912SNimrod Andy {
75979f33912SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
760761b331cSEric Dumazet 	int hdr_len, total_len, data_left;
7617355f276STroy Kisky 	struct bufdesc *bdp = txq->bd.cur;
76279f33912SNimrod Andy 	struct tso_t tso;
76379f33912SNimrod Andy 	unsigned int index = 0;
76479f33912SNimrod Andy 	int ret;
76579f33912SNimrod Andy 
7667355f276STroy Kisky 	if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
76779f33912SNimrod Andy 		dev_kfree_skb_any(skb);
76879f33912SNimrod Andy 		if (net_ratelimit())
76979f33912SNimrod Andy 			netdev_err(ndev, "NOT enough BD for TSO!\n");
77079f33912SNimrod Andy 		return NETDEV_TX_OK;
77179f33912SNimrod Andy 	}
77279f33912SNimrod Andy 
77379f33912SNimrod Andy 	/* Protocol checksum off-load for TCP and UDP. */
77479f33912SNimrod Andy 	if (fec_enet_clear_csum(skb, ndev)) {
77579f33912SNimrod Andy 		dev_kfree_skb_any(skb);
77679f33912SNimrod Andy 		return NETDEV_TX_OK;
77779f33912SNimrod Andy 	}
77879f33912SNimrod Andy 
77979f33912SNimrod Andy 	/* Initialize the TSO handler, and prepare the first payload */
780761b331cSEric Dumazet 	hdr_len = tso_start(skb, &tso);
78179f33912SNimrod Andy 
78279f33912SNimrod Andy 	total_len = skb->len - hdr_len;
78379f33912SNimrod Andy 	while (total_len > 0) {
78479f33912SNimrod Andy 		char *hdr;
78579f33912SNimrod Andy 
7867355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
78779f33912SNimrod Andy 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
78879f33912SNimrod Andy 		total_len -= data_left;
78979f33912SNimrod Andy 
79079f33912SNimrod Andy 		/* prepare packet headers: MAC + IP + TCP */
7914d494cdcSFugang Duan 		hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
79279f33912SNimrod Andy 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
7934d494cdcSFugang Duan 		ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
79479f33912SNimrod Andy 		if (ret)
79579f33912SNimrod Andy 			goto err_release;
79679f33912SNimrod Andy 
79779f33912SNimrod Andy 		while (data_left > 0) {
79879f33912SNimrod Andy 			int size;
79979f33912SNimrod Andy 
80079f33912SNimrod Andy 			size = min_t(int, tso.size, data_left);
8017355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
8027355f276STroy Kisky 			index = fec_enet_get_bd_index(bdp, &txq->bd);
8034d494cdcSFugang Duan 			ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
8044d494cdcSFugang Duan 							bdp, index,
8054d494cdcSFugang Duan 							tso.data, size,
8064d494cdcSFugang Duan 							size == data_left,
80779f33912SNimrod Andy 							total_len == 0);
80879f33912SNimrod Andy 			if (ret)
80979f33912SNimrod Andy 				goto err_release;
81079f33912SNimrod Andy 
81179f33912SNimrod Andy 			data_left -= size;
81279f33912SNimrod Andy 			tso_build_data(skb, &tso, size);
81379f33912SNimrod Andy 		}
81479f33912SNimrod Andy 
8157355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
81679f33912SNimrod Andy 	}
81779f33912SNimrod Andy 
81879f33912SNimrod Andy 	/* Save skb pointer */
8194d494cdcSFugang Duan 	txq->tx_skbuff[index] = skb;
82079f33912SNimrod Andy 
82179f33912SNimrod Andy 	skb_tx_timestamp(skb);
8227355f276STroy Kisky 	txq->bd.cur = bdp;
82379f33912SNimrod Andy 
82479f33912SNimrod Andy 	/* Trigger transmission start */
8256b7e4008SLothar Waßmann 	if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
82653bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
82753bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
82853bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active) ||
82953bb20d1STroy Kisky 	    !readl(txq->bd.reg_desc_active))
83053bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
83179f33912SNimrod Andy 
83279f33912SNimrod Andy 	return 0;
83379f33912SNimrod Andy 
83479f33912SNimrod Andy err_release:
83579f33912SNimrod Andy 	/* TODO: Release all used data descriptors for TSO */
83679f33912SNimrod Andy 	return ret;
83779f33912SNimrod Andy }
83879f33912SNimrod Andy 
83961a4427bSNimrod Andy static netdev_tx_t
84061a4427bSNimrod Andy fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
84161a4427bSNimrod Andy {
84261a4427bSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
8436e909283SNimrod Andy 	int entries_free;
8444d494cdcSFugang Duan 	unsigned short queue;
8454d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
8464d494cdcSFugang Duan 	struct netdev_queue *nq;
84761a4427bSNimrod Andy 	int ret;
84861a4427bSNimrod Andy 
8494d494cdcSFugang Duan 	queue = skb_get_queue_mapping(skb);
8504d494cdcSFugang Duan 	txq = fep->tx_queue[queue];
8514d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue);
8524d494cdcSFugang Duan 
85379f33912SNimrod Andy 	if (skb_is_gso(skb))
8544d494cdcSFugang Duan 		ret = fec_enet_txq_submit_tso(txq, skb, ndev);
85579f33912SNimrod Andy 	else
8564d494cdcSFugang Duan 		ret = fec_enet_txq_submit_skb(txq, skb, ndev);
8576e909283SNimrod Andy 	if (ret)
8586e909283SNimrod Andy 		return ret;
85961a4427bSNimrod Andy 
8607355f276STroy Kisky 	entries_free = fec_enet_get_free_txdesc_num(txq);
8614d494cdcSFugang Duan 	if (entries_free <= txq->tx_stop_threshold)
8624d494cdcSFugang Duan 		netif_tx_stop_queue(nq);
86361a4427bSNimrod Andy 
86461a4427bSNimrod Andy 	return NETDEV_TX_OK;
86561a4427bSNimrod Andy }
86661a4427bSNimrod Andy 
867a210576cSDavid S. Miller /* Init RX & TX buffer descriptors
868a210576cSDavid S. Miller  */
869a210576cSDavid S. Miller static void fec_enet_bd_init(struct net_device *dev)
870a210576cSDavid S. Miller {
871a210576cSDavid S. Miller 	struct fec_enet_private *fep = netdev_priv(dev);
8724d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
8734d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
874a210576cSDavid S. Miller 	struct bufdesc *bdp;
875a210576cSDavid S. Miller 	unsigned int i;
87659d0f746SFrank Li 	unsigned int q;
877a210576cSDavid S. Miller 
87859d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
879a210576cSDavid S. Miller 		/* Initialize the receive buffer descriptors. */
88059d0f746SFrank Li 		rxq = fep->rx_queue[q];
8817355f276STroy Kisky 		bdp = rxq->bd.base;
8824d494cdcSFugang Duan 
8837355f276STroy Kisky 		for (i = 0; i < rxq->bd.ring_size; i++) {
884a210576cSDavid S. Miller 
885a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
886a210576cSDavid S. Miller 			if (bdp->cbd_bufaddr)
8875cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
888a210576cSDavid S. Miller 			else
8895cfa3039SJohannes Berg 				bdp->cbd_sc = cpu_to_fec16(0);
8907355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
891a210576cSDavid S. Miller 		}
892a210576cSDavid S. Miller 
893a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
8947355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
8955cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
896a210576cSDavid S. Miller 
8977355f276STroy Kisky 		rxq->bd.cur = rxq->bd.base;
89859d0f746SFrank Li 	}
899a210576cSDavid S. Miller 
90059d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
901a210576cSDavid S. Miller 		/* ...and the same for transmit */
90259d0f746SFrank Li 		txq = fep->tx_queue[q];
9037355f276STroy Kisky 		bdp = txq->bd.base;
9047355f276STroy Kisky 		txq->bd.cur = bdp;
905a210576cSDavid S. Miller 
9067355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
907a210576cSDavid S. Miller 			/* Initialize the BD for every fragment in the page. */
9085cfa3039SJohannes Berg 			bdp->cbd_sc = cpu_to_fec16(0);
909178e5f57SFugang Duan 			if (bdp->cbd_bufaddr &&
910178e5f57SFugang Duan 			    !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
911178e5f57SFugang Duan 				dma_unmap_single(&fep->pdev->dev,
912178e5f57SFugang Duan 						 fec32_to_cpu(bdp->cbd_bufaddr),
913178e5f57SFugang Duan 						 fec16_to_cpu(bdp->cbd_datlen),
914178e5f57SFugang Duan 						 DMA_TO_DEVICE);
9154d494cdcSFugang Duan 			if (txq->tx_skbuff[i]) {
9164d494cdcSFugang Duan 				dev_kfree_skb_any(txq->tx_skbuff[i]);
9174d494cdcSFugang Duan 				txq->tx_skbuff[i] = NULL;
918a210576cSDavid S. Miller 			}
9195cfa3039SJohannes Berg 			bdp->cbd_bufaddr = cpu_to_fec32(0);
9207355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
921a210576cSDavid S. Miller 		}
922a210576cSDavid S. Miller 
923a210576cSDavid S. Miller 		/* Set the last buffer to wrap */
9247355f276STroy Kisky 		bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
9255cfa3039SJohannes Berg 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
9264d494cdcSFugang Duan 		txq->dirty_tx = bdp;
927a210576cSDavid S. Miller 	}
92859d0f746SFrank Li }
92959d0f746SFrank Li 
930ce99d0d3SFrank Li static void fec_enet_active_rxring(struct net_device *ndev)
931ce99d0d3SFrank Li {
932ce99d0d3SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
933ce99d0d3SFrank Li 	int i;
934ce99d0d3SFrank Li 
935ce99d0d3SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
93653bb20d1STroy Kisky 		writel(0, fep->rx_queue[i]->bd.reg_desc_active);
937ce99d0d3SFrank Li }
938ce99d0d3SFrank Li 
93959d0f746SFrank Li static void fec_enet_enable_ring(struct net_device *ndev)
94059d0f746SFrank Li {
94159d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
94259d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
94359d0f746SFrank Li 	struct fec_enet_priv_rx_q *rxq;
94459d0f746SFrank Li 	int i;
94559d0f746SFrank Li 
94659d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
94759d0f746SFrank Li 		rxq = fep->rx_queue[i];
9487355f276STroy Kisky 		writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
949fbbeefddSAndrew Lunn 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
95059d0f746SFrank Li 
95159d0f746SFrank Li 		/* enable DMA1/2 */
95259d0f746SFrank Li 		if (i)
95359d0f746SFrank Li 			writel(RCMR_MATCHEN | RCMR_CMP(i),
95459d0f746SFrank Li 			       fep->hwp + FEC_RCMR(i));
95559d0f746SFrank Li 	}
95659d0f746SFrank Li 
95759d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
95859d0f746SFrank Li 		txq = fep->tx_queue[i];
9597355f276STroy Kisky 		writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
96059d0f746SFrank Li 
96159d0f746SFrank Li 		/* enable DMA1/2 */
96259d0f746SFrank Li 		if (i)
96359d0f746SFrank Li 			writel(DMA_CLASS_EN | IDLE_SLOPE(i),
96459d0f746SFrank Li 			       fep->hwp + FEC_DMA_CFG(i));
96559d0f746SFrank Li 	}
96659d0f746SFrank Li }
96759d0f746SFrank Li 
96859d0f746SFrank Li static void fec_enet_reset_skb(struct net_device *ndev)
96959d0f746SFrank Li {
97059d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
97159d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
97259d0f746SFrank Li 	int i, j;
97359d0f746SFrank Li 
97459d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
97559d0f746SFrank Li 		txq = fep->tx_queue[i];
97659d0f746SFrank Li 
9777355f276STroy Kisky 		for (j = 0; j < txq->bd.ring_size; j++) {
97859d0f746SFrank Li 			if (txq->tx_skbuff[j]) {
97959d0f746SFrank Li 				dev_kfree_skb_any(txq->tx_skbuff[j]);
98059d0f746SFrank Li 				txq->tx_skbuff[j] = NULL;
98159d0f746SFrank Li 			}
98259d0f746SFrank Li 		}
98359d0f746SFrank Li 	}
98459d0f746SFrank Li }
985a210576cSDavid S. Miller 
986dbc64a8eSRussell King /*
987dbc64a8eSRussell King  * This function is called to start or restart the FEC during a link
988dbc64a8eSRussell King  * change, transmit timeout, or to reconfigure the FEC.  The network
989dbc64a8eSRussell King  * packet processing for this device must be stopped before this call.
990793fc096SFrank Li  */
991793fc096SFrank Li static void
992ef83337dSRussell King fec_restart(struct net_device *ndev)
993793fc096SFrank Li {
994793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
995793fc096SFrank Li 	u32 temp_mac[2];
996793fc096SFrank Li 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
997793fc096SFrank Li 	u32 ecntl = 0x2; /* ETHEREN */
998793fc096SFrank Li 
999106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
1000106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1001106c314cSFugang Duan 	 * instead of reset MAC itself.
1002106c314cSFugang Duan 	 */
1003471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES ||
1004c730ab42SLaurent Badel 	    ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) {
1005106c314cSFugang Duan 		writel(0, fep->hwp + FEC_ECNTRL);
1006106c314cSFugang Duan 	} else {
1007793fc096SFrank Li 		writel(1, fep->hwp + FEC_ECNTRL);
1008793fc096SFrank Li 		udelay(10);
1009106c314cSFugang Duan 	}
1010793fc096SFrank Li 
1011793fc096SFrank Li 	/*
1012793fc096SFrank Li 	 * enet-mac reset will reset mac address registers too,
1013793fc096SFrank Li 	 * so need to reconfigure it.
1014793fc096SFrank Li 	 */
1015793fc096SFrank Li 	memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
10165cfa3039SJohannes Berg 	writel((__force u32)cpu_to_be32(temp_mac[0]),
10175cfa3039SJohannes Berg 	       fep->hwp + FEC_ADDR_LOW);
10185cfa3039SJohannes Berg 	writel((__force u32)cpu_to_be32(temp_mac[1]),
10195cfa3039SJohannes Berg 	       fep->hwp + FEC_ADDR_HIGH);
1020793fc096SFrank Li 
1021f166f890SAndrew Lunn 	/* Clear any outstanding interrupt, except MDIO. */
1022f166f890SAndrew Lunn 	writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
1023793fc096SFrank Li 
1024a210576cSDavid S. Miller 	fec_enet_bd_init(ndev);
1025a210576cSDavid S. Miller 
102659d0f746SFrank Li 	fec_enet_enable_ring(ndev);
1027793fc096SFrank Li 
102859d0f746SFrank Li 	/* Reset tx SKB buffers. */
102959d0f746SFrank Li 	fec_enet_reset_skb(ndev);
1030793fc096SFrank Li 
1031793fc096SFrank Li 	/* Enable MII mode */
1032ef83337dSRussell King 	if (fep->full_duplex == DUPLEX_FULL) {
1033793fc096SFrank Li 		/* FD enable */
1034793fc096SFrank Li 		writel(0x04, fep->hwp + FEC_X_CNTRL);
1035793fc096SFrank Li 	} else {
1036793fc096SFrank Li 		/* No Rcv on Xmit */
1037793fc096SFrank Li 		rcntl |= 0x02;
1038793fc096SFrank Li 		writel(0x0, fep->hwp + FEC_X_CNTRL);
1039793fc096SFrank Li 	}
1040793fc096SFrank Li 
1041793fc096SFrank Li 	/* Set MII speed */
1042793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1043793fc096SFrank Li 
1044d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
104518803495SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_HAS_RACC) {
104632d1bbb1SGeert Uytterhoeven 		u32 val = readl(fep->hwp + FEC_RACC);
104732d1bbb1SGeert Uytterhoeven 
10483ac72b7bSEric Nelson 		/* align IP header */
10493ac72b7bSEric Nelson 		val |= FEC_RACC_SHIFT16;
10504c09eed9SJim Baxter 		if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
10513ac72b7bSEric Nelson 			/* set RX checksum */
10524c09eed9SJim Baxter 			val |= FEC_RACC_OPTIONS;
10534c09eed9SJim Baxter 		else
10544c09eed9SJim Baxter 			val &= ~FEC_RACC_OPTIONS;
10554c09eed9SJim Baxter 		writel(val, fep->hwp + FEC_RACC);
105655cd48c8STroy Kisky 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
105732867fccSFabio Estevam 	}
1058d1391930SGuenter Roeck #endif
10594c09eed9SJim Baxter 
1060793fc096SFrank Li 	/*
1061793fc096SFrank Li 	 * The phy interface and speed need to get configured
1062793fc096SFrank Li 	 * differently on enet-mac.
1063793fc096SFrank Li 	 */
10646b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1065793fc096SFrank Li 		/* Enable flow control and length check */
1066793fc096SFrank Li 		rcntl |= 0x40000000 | 0x00000020;
1067793fc096SFrank Li 
1068793fc096SFrank Li 		/* RGMII, RMII or MII */
1069e813bb2bSMarkus Pargmann 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
1070e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1071e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1072e813bb2bSMarkus Pargmann 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1073793fc096SFrank Li 			rcntl |= (1 << 6);
1074793fc096SFrank Li 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1075793fc096SFrank Li 			rcntl |= (1 << 8);
1076793fc096SFrank Li 		else
1077793fc096SFrank Li 			rcntl &= ~(1 << 8);
1078793fc096SFrank Li 
1079793fc096SFrank Li 		/* 1G, 100M or 10M */
108045f5c327SPhilippe Reynes 		if (ndev->phydev) {
108145f5c327SPhilippe Reynes 			if (ndev->phydev->speed == SPEED_1000)
1082793fc096SFrank Li 				ecntl |= (1 << 5);
108345f5c327SPhilippe Reynes 			else if (ndev->phydev->speed == SPEED_100)
1084793fc096SFrank Li 				rcntl &= ~(1 << 9);
1085793fc096SFrank Li 			else
1086793fc096SFrank Li 				rcntl |= (1 << 9);
1087793fc096SFrank Li 		}
1088793fc096SFrank Li 	} else {
1089793fc096SFrank Li #ifdef FEC_MIIGSK_ENR
10906b7e4008SLothar Waßmann 		if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1091793fc096SFrank Li 			u32 cfgr;
1092793fc096SFrank Li 			/* disable the gasket and wait */
1093793fc096SFrank Li 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
1094793fc096SFrank Li 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1095793fc096SFrank Li 				udelay(1);
1096793fc096SFrank Li 
1097793fc096SFrank Li 			/*
1098793fc096SFrank Li 			 * configure the gasket:
1099793fc096SFrank Li 			 *   RMII, 50 MHz, no loopback, no echo
1100793fc096SFrank Li 			 *   MII, 25 MHz, no loopback, no echo
1101793fc096SFrank Li 			 */
1102793fc096SFrank Li 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1103793fc096SFrank Li 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
110445f5c327SPhilippe Reynes 			if (ndev->phydev && ndev->phydev->speed == SPEED_10)
1105793fc096SFrank Li 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1106793fc096SFrank Li 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1107793fc096SFrank Li 
1108793fc096SFrank Li 			/* re-enable the gasket */
1109793fc096SFrank Li 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
1110793fc096SFrank Li 		}
1111793fc096SFrank Li #endif
1112793fc096SFrank Li 	}
1113793fc096SFrank Li 
1114d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
1115793fc096SFrank Li 	/* enable pause frame*/
1116793fc096SFrank Li 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1117793fc096SFrank Li 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
111845f5c327SPhilippe Reynes 	     ndev->phydev && ndev->phydev->pause)) {
1119793fc096SFrank Li 		rcntl |= FEC_ENET_FCE;
1120793fc096SFrank Li 
11214c09eed9SJim Baxter 		/* set FIFO threshold parameter to reduce overrun */
1122793fc096SFrank Li 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1123793fc096SFrank Li 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1124793fc096SFrank Li 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1125793fc096SFrank Li 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1126793fc096SFrank Li 
1127793fc096SFrank Li 		/* OPD */
1128793fc096SFrank Li 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1129793fc096SFrank Li 	} else {
1130793fc096SFrank Li 		rcntl &= ~FEC_ENET_FCE;
1131793fc096SFrank Li 	}
1132d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
1133793fc096SFrank Li 
1134793fc096SFrank Li 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
1135793fc096SFrank Li 
113684fe6182SStefan Wahren 	/* Setup multicast filter. */
113784fe6182SStefan Wahren 	set_multicast_list(ndev);
113884fe6182SStefan Wahren #ifndef CONFIG_M5272
113984fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
114084fe6182SStefan Wahren 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
114184fe6182SStefan Wahren #endif
114284fe6182SStefan Wahren 
11436b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1144793fc096SFrank Li 		/* enable ENET endian swap */
1145793fc096SFrank Li 		ecntl |= (1 << 8);
1146793fc096SFrank Li 		/* enable ENET store and forward mode */
1147793fc096SFrank Li 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
1148793fc096SFrank Li 	}
1149793fc096SFrank Li 
1150793fc096SFrank Li 	if (fep->bufdesc_ex)
11517b15515fSFrancesco Dolcini 		ecntl |= (1 << 4);
1152793fc096SFrank Li 
1153fc539459SFugang Duan 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1154fc539459SFugang Duan 	    fep->rgmii_txc_dly)
1155fc539459SFugang Duan 		ecntl |= FEC_ENET_TXC_DLY;
1156fc539459SFugang Duan 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1157fc539459SFugang Duan 	    fep->rgmii_rxc_dly)
1158fc539459SFugang Duan 		ecntl |= FEC_ENET_RXC_DLY;
1159fc539459SFugang Duan 
116038ae92dcSChris Healy #ifndef CONFIG_M5272
1161b9eef55cSJim Baxter 	/* Enable the MIB statistic event counters */
1162b9eef55cSJim Baxter 	writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
116338ae92dcSChris Healy #endif
116438ae92dcSChris Healy 
1165793fc096SFrank Li 	/* And last, enable the transmit and receive processing */
1166793fc096SFrank Li 	writel(ecntl, fep->hwp + FEC_ECNTRL);
1167ce99d0d3SFrank Li 	fec_enet_active_rxring(ndev);
1168793fc096SFrank Li 
1169793fc096SFrank Li 	if (fep->bufdesc_ex)
1170793fc096SFrank Li 		fec_ptp_start_cyclecounter(ndev);
1171793fc096SFrank Li 
1172793fc096SFrank Li 	/* Enable interrupts we wish to service */
11730c5a3aefSNimrod Andy 	if (fep->link)
1174793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
11750c5a3aefSNimrod Andy 	else
1176f166f890SAndrew Lunn 		writel(0, fep->hwp + FEC_IMASK);
1177d851b47bSFugang Duan 
1178d851b47bSFugang Duan 	/* Init the interrupt coalescing */
1179d851b47bSFugang Duan 	fec_enet_itr_coal_init(ndev);
1180d851b47bSFugang Duan 
1181793fc096SFrank Li }
1182793fc096SFrank Li 
118340c79ce1SWei Fang static int fec_enet_ipc_handle_init(struct fec_enet_private *fep)
118440c79ce1SWei Fang {
118540c79ce1SWei Fang 	if (!(of_machine_is_compatible("fsl,imx8qm") ||
118640c79ce1SWei Fang 	      of_machine_is_compatible("fsl,imx8qxp") ||
118740c79ce1SWei Fang 	      of_machine_is_compatible("fsl,imx8dxl")))
118840c79ce1SWei Fang 		return 0;
118940c79ce1SWei Fang 
119040c79ce1SWei Fang 	return imx_scu_get_handle(&fep->ipc_handle);
119140c79ce1SWei Fang }
119240c79ce1SWei Fang 
119340c79ce1SWei Fang static void fec_enet_ipg_stop_set(struct fec_enet_private *fep, bool enabled)
119440c79ce1SWei Fang {
119540c79ce1SWei Fang 	struct device_node *np = fep->pdev->dev.of_node;
119640c79ce1SWei Fang 	u32 rsrc_id, val;
119740c79ce1SWei Fang 	int idx;
119840c79ce1SWei Fang 
119940c79ce1SWei Fang 	if (!np || !fep->ipc_handle)
120040c79ce1SWei Fang 		return;
120140c79ce1SWei Fang 
120240c79ce1SWei Fang 	idx = of_alias_get_id(np, "ethernet");
120340c79ce1SWei Fang 	if (idx < 0)
120440c79ce1SWei Fang 		idx = 0;
120540c79ce1SWei Fang 	rsrc_id = idx ? IMX_SC_R_ENET_1 : IMX_SC_R_ENET_0;
120640c79ce1SWei Fang 
120740c79ce1SWei Fang 	val = enabled ? 1 : 0;
120840c79ce1SWei Fang 	imx_sc_misc_set_control(fep->ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val);
120940c79ce1SWei Fang }
121040c79ce1SWei Fang 
1211da722186SMartin Fuzzey static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
1212da722186SMartin Fuzzey {
1213da722186SMartin Fuzzey 	struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1214da722186SMartin Fuzzey 	struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
1215da722186SMartin Fuzzey 
1216da722186SMartin Fuzzey 	if (stop_gpr->gpr) {
1217da722186SMartin Fuzzey 		if (enabled)
1218da722186SMartin Fuzzey 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1219da722186SMartin Fuzzey 					   BIT(stop_gpr->bit),
1220da722186SMartin Fuzzey 					   BIT(stop_gpr->bit));
1221da722186SMartin Fuzzey 		else
1222da722186SMartin Fuzzey 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1223da722186SMartin Fuzzey 					   BIT(stop_gpr->bit), 0);
1224da722186SMartin Fuzzey 	} else if (pdata && pdata->sleep_mode_enable) {
1225da722186SMartin Fuzzey 		pdata->sleep_mode_enable(enabled);
122640c79ce1SWei Fang 	} else {
122740c79ce1SWei Fang 		fec_enet_ipg_stop_set(fep, enabled);
1228da722186SMartin Fuzzey 	}
1229da722186SMartin Fuzzey }
1230da722186SMartin Fuzzey 
12310b6f65c7SJoakim Zhang static void fec_irqs_disable(struct net_device *ndev)
12320b6f65c7SJoakim Zhang {
12330b6f65c7SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
12340b6f65c7SJoakim Zhang 
12350b6f65c7SJoakim Zhang 	writel(0, fep->hwp + FEC_IMASK);
12360b6f65c7SJoakim Zhang }
12370b6f65c7SJoakim Zhang 
12380b6f65c7SJoakim Zhang static void fec_irqs_disable_except_wakeup(struct net_device *ndev)
12390b6f65c7SJoakim Zhang {
12400b6f65c7SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
12410b6f65c7SJoakim Zhang 
12420b6f65c7SJoakim Zhang 	writel(0, fep->hwp + FEC_IMASK);
12430b6f65c7SJoakim Zhang 	writel(FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
12440b6f65c7SJoakim Zhang }
12450b6f65c7SJoakim Zhang 
1246793fc096SFrank Li static void
1247793fc096SFrank Li fec_stop(struct net_device *ndev)
1248793fc096SFrank Li {
1249793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1250793fc096SFrank Li 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1251de40ed31SNimrod Andy 	u32 val;
1252793fc096SFrank Li 
1253793fc096SFrank Li 	/* We cannot expect a graceful transmit stop without link !!! */
1254793fc096SFrank Li 	if (fep->link) {
1255793fc096SFrank Li 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1256793fc096SFrank Li 		udelay(10);
1257793fc096SFrank Li 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
125831b7720cSJoe Perches 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1259793fc096SFrank Li 	}
1260793fc096SFrank Li 
1261106c314cSFugang Duan 	/* Whack a reset.  We should wait for this.
1262106c314cSFugang Duan 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1263106c314cSFugang Duan 	 * instead of reset MAC itself.
1264106c314cSFugang Duan 	 */
1265de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1266471ff445SJoakim Zhang 		if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
1267106c314cSFugang Duan 			writel(0, fep->hwp + FEC_ECNTRL);
1268106c314cSFugang Duan 		} else {
1269793fc096SFrank Li 			writel(1, fep->hwp + FEC_ECNTRL);
1270793fc096SFrank Li 			udelay(10);
1271106c314cSFugang Duan 		}
1272de40ed31SNimrod Andy 	} else {
1273de40ed31SNimrod Andy 		val = readl(fep->hwp + FEC_ECNTRL);
1274de40ed31SNimrod Andy 		val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1275de40ed31SNimrod Andy 		writel(val, fep->hwp + FEC_ECNTRL);
1276de40ed31SNimrod Andy 	}
1277de40ed31SNimrod Andy 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
12780b6f65c7SJoakim Zhang 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1279793fc096SFrank Li 
1280793fc096SFrank Li 	/* We have to keep ENET enabled to have MII interrupt stay working */
1281de40ed31SNimrod Andy 	if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1282de40ed31SNimrod Andy 		!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
12837b15515fSFrancesco Dolcini 		writel(2, fep->hwp + FEC_ECNTRL);
1284793fc096SFrank Li 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1285793fc096SFrank Li 	}
1286793fc096SFrank Li }
1287793fc096SFrank Li 
1288793fc096SFrank Li 
1289793fc096SFrank Li static void
12900290bd29SMichael S. Tsirkin fec_timeout(struct net_device *ndev, unsigned int txqueue)
1291793fc096SFrank Li {
1292793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1293793fc096SFrank Li 
1294344756f6SRussell King 	fec_dump(ndev);
1295344756f6SRussell King 
1296793fc096SFrank Li 	ndev->stats.tx_errors++;
1297793fc096SFrank Li 
129836cdc743SRussell King 	schedule_work(&fep->tx_timeout_work);
129954309fa6SFrank Li }
130054309fa6SFrank Li 
130136cdc743SRussell King static void fec_enet_timeout_work(struct work_struct *work)
130254309fa6SFrank Li {
130354309fa6SFrank Li 	struct fec_enet_private *fep =
130436cdc743SRussell King 		container_of(work, struct fec_enet_private, tx_timeout_work);
13058ce5624fSRussell King 	struct net_device *ndev = fep->netdev;
130654309fa6SFrank Li 
1307da1774e5SRussell King 	rtnl_lock();
13088ce5624fSRussell King 	if (netif_device_present(ndev) || netif_running(ndev)) {
1309dbc64a8eSRussell King 		napi_disable(&fep->napi);
1310dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
1311ef83337dSRussell King 		fec_restart(ndev);
1312657ade07SRickard x Andersson 		netif_tx_wake_all_queues(ndev);
13136af42d42SRussell King 		netif_tx_unlock_bh(ndev);
1314dbc64a8eSRussell King 		napi_enable(&fep->napi);
13158ce5624fSRussell King 	}
1316da1774e5SRussell King 	rtnl_unlock();
131754309fa6SFrank Li }
1318793fc096SFrank Li 
1319793fc096SFrank Li static void
1320bfd4ecddSRussell King fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1321bfd4ecddSRussell King 	struct skb_shared_hwtstamps *hwtstamps)
1322bfd4ecddSRussell King {
1323bfd4ecddSRussell King 	unsigned long flags;
1324bfd4ecddSRussell King 	u64 ns;
1325bfd4ecddSRussell King 
1326bfd4ecddSRussell King 	spin_lock_irqsave(&fep->tmreg_lock, flags);
1327bfd4ecddSRussell King 	ns = timecounter_cyc2time(&fep->tc, ts);
1328bfd4ecddSRussell King 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1329bfd4ecddSRussell King 
1330bfd4ecddSRussell King 	memset(hwtstamps, 0, sizeof(*hwtstamps));
1331bfd4ecddSRussell King 	hwtstamps->hwtstamp = ns_to_ktime(ns);
1332bfd4ecddSRussell King }
1333bfd4ecddSRussell King 
1334bfd4ecddSRussell King static void
13354d494cdcSFugang Duan fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1336793fc096SFrank Li {
1337793fc096SFrank Li 	struct	fec_enet_private *fep;
1338a2fe37b6SFabio Estevam 	struct bufdesc *bdp;
1339793fc096SFrank Li 	unsigned short status;
1340793fc096SFrank Li 	struct	sk_buff	*skb;
13414d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
13424d494cdcSFugang Duan 	struct netdev_queue *nq;
1343793fc096SFrank Li 	int	index = 0;
134479f33912SNimrod Andy 	int	entries_free;
1345793fc096SFrank Li 
1346793fc096SFrank Li 	fep = netdev_priv(ndev);
13474d494cdcSFugang Duan 
13484d494cdcSFugang Duan 	txq = fep->tx_queue[queue_id];
13494d494cdcSFugang Duan 	/* get next bdp of dirty_tx */
13504d494cdcSFugang Duan 	nq = netdev_get_tx_queue(ndev, queue_id);
13514d494cdcSFugang Duan 	bdp = txq->dirty_tx;
1352793fc096SFrank Li 
1353793fc096SFrank Li 	/* get next bdp of dirty_tx */
13547355f276STroy Kisky 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1355793fc096SFrank Li 
13567355f276STroy Kisky 	while (bdp != READ_ONCE(txq->bd.cur)) {
13577355f276STroy Kisky 		/* Order the load of bd.cur and cbd_sc */
1358c4bc44c6SKevin Hao 		rmb();
13595cfa3039SJohannes Berg 		status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1360c4bc44c6SKevin Hao 		if (status & BD_ENET_TX_READY)
1361793fc096SFrank Li 			break;
1362793fc096SFrank Li 
13637355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &txq->bd);
13642b995f63SNimrod Andy 
1365a2fe37b6SFabio Estevam 		skb = txq->tx_skbuff[index];
1366a2fe37b6SFabio Estevam 		txq->tx_skbuff[index] = NULL;
13675cfa3039SJohannes Berg 		if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
13685cfa3039SJohannes Berg 			dma_unmap_single(&fep->pdev->dev,
13695cfa3039SJohannes Berg 					 fec32_to_cpu(bdp->cbd_bufaddr),
13705cfa3039SJohannes Berg 					 fec16_to_cpu(bdp->cbd_datlen),
13715cfa3039SJohannes Berg 					 DMA_TO_DEVICE);
13725cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
13737fafe803STroy Kisky 		if (!skb)
13747fafe803STroy Kisky 			goto skb_done;
1375793fc096SFrank Li 
1376793fc096SFrank Li 		/* Check for errors. */
1377793fc096SFrank Li 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1378793fc096SFrank Li 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
1379793fc096SFrank Li 				   BD_ENET_TX_CSL)) {
1380793fc096SFrank Li 			ndev->stats.tx_errors++;
1381793fc096SFrank Li 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
1382793fc096SFrank Li 				ndev->stats.tx_heartbeat_errors++;
1383793fc096SFrank Li 			if (status & BD_ENET_TX_LC)  /* Late collision */
1384793fc096SFrank Li 				ndev->stats.tx_window_errors++;
1385793fc096SFrank Li 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
1386793fc096SFrank Li 				ndev->stats.tx_aborted_errors++;
1387793fc096SFrank Li 			if (status & BD_ENET_TX_UN)  /* Underrun */
1388793fc096SFrank Li 				ndev->stats.tx_fifo_errors++;
1389793fc096SFrank Li 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
1390793fc096SFrank Li 				ndev->stats.tx_carrier_errors++;
1391793fc096SFrank Li 		} else {
1392793fc096SFrank Li 			ndev->stats.tx_packets++;
13936e909283SNimrod Andy 			ndev->stats.tx_bytes += skb->len;
1394793fc096SFrank Li 		}
1395793fc096SFrank Li 
139634074639SSergey Organov 		/* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who
139734074639SSergey Organov 		 * are to time stamp the packet, so we still need to check time
139834074639SSergey Organov 		 * stamping enabled flag.
139934074639SSergey Organov 		 */
140034074639SSergey Organov 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS &&
140134074639SSergey Organov 			     fep->hwts_tx_en) &&
1402793fc096SFrank Li 		    fep->bufdesc_ex) {
1403793fc096SFrank Li 			struct skb_shared_hwtstamps shhwtstamps;
1404793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1405793fc096SFrank Li 
14065cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1407793fc096SFrank Li 			skb_tstamp_tx(skb, &shhwtstamps);
1408793fc096SFrank Li 		}
1409793fc096SFrank Li 
1410793fc096SFrank Li 		/* Deferred means some collisions occurred during transmit,
1411793fc096SFrank Li 		 * but we eventually sent the packet OK.
1412793fc096SFrank Li 		 */
1413793fc096SFrank Li 		if (status & BD_ENET_TX_DEF)
1414793fc096SFrank Li 			ndev->stats.collisions++;
1415793fc096SFrank Li 
1416793fc096SFrank Li 		/* Free the sk buffer associated with this last transmit */
1417793fc096SFrank Li 		dev_kfree_skb_any(skb);
14187fafe803STroy Kisky skb_done:
1419c4bc44c6SKevin Hao 		/* Make sure the update to bdp and tx_skbuff are performed
1420c4bc44c6SKevin Hao 		 * before dirty_tx
1421c4bc44c6SKevin Hao 		 */
1422c4bc44c6SKevin Hao 		wmb();
14234d494cdcSFugang Duan 		txq->dirty_tx = bdp;
1424793fc096SFrank Li 
1425793fc096SFrank Li 		/* Update pointer to next buffer descriptor to be transmitted */
14267355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1427793fc096SFrank Li 
1428793fc096SFrank Li 		/* Since we have freed up a buffer, the ring is no longer full
1429793fc096SFrank Li 		 */
1430657ade07SRickard x Andersson 		if (netif_tx_queue_stopped(nq)) {
14317355f276STroy Kisky 			entries_free = fec_enet_get_free_txdesc_num(txq);
14324d494cdcSFugang Duan 			if (entries_free >= txq->tx_wake_threshold)
14334d494cdcSFugang Duan 				netif_tx_wake_queue(nq);
1434793fc096SFrank Li 		}
143579f33912SNimrod Andy 	}
1436ccea2968SRussell King 
1437c10bc0e7SFugang Duan 	/* ERR006358: Keep the transmitter going */
14387355f276STroy Kisky 	if (bdp != txq->bd.cur &&
143953bb20d1STroy Kisky 	    readl(txq->bd.reg_desc_active) == 0)
144053bb20d1STroy Kisky 		writel(0, txq->bd.reg_desc_active);
14414d494cdcSFugang Duan }
14424d494cdcSFugang Duan 
14437cdaa4ccSTobias Waldekranz static void fec_enet_tx(struct net_device *ndev)
14444d494cdcSFugang Duan {
14454d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
14467cdaa4ccSTobias Waldekranz 	int i;
14477cdaa4ccSTobias Waldekranz 
14487cdaa4ccSTobias Waldekranz 	/* Make sure that AVB queues are processed first. */
14497cdaa4ccSTobias Waldekranz 	for (i = fep->num_tx_queues - 1; i >= 0; i--)
14507cdaa4ccSTobias Waldekranz 		fec_enet_tx_queue(ndev, i);
1451793fc096SFrank Li }
1452793fc096SFrank Li 
14531b7bde6dSNimrod Andy static int
14541b7bde6dSNimrod Andy fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
14551b7bde6dSNimrod Andy {
14561b7bde6dSNimrod Andy 	struct  fec_enet_private *fep = netdev_priv(ndev);
14571b7bde6dSNimrod Andy 	int off;
14581b7bde6dSNimrod Andy 
14591b7bde6dSNimrod Andy 	off = ((unsigned long)skb->data) & fep->rx_align;
14601b7bde6dSNimrod Andy 	if (off)
14611b7bde6dSNimrod Andy 		skb_reserve(skb, fep->rx_align + 1 - off);
14621b7bde6dSNimrod Andy 
14635cfa3039SJohannes Berg 	bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
14645cfa3039SJohannes Berg 	if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
14651b7bde6dSNimrod Andy 		if (net_ratelimit())
14661b7bde6dSNimrod Andy 			netdev_err(ndev, "Rx DMA memory map failed\n");
14671b7bde6dSNimrod Andy 		return -ENOMEM;
14681b7bde6dSNimrod Andy 	}
14691b7bde6dSNimrod Andy 
14701b7bde6dSNimrod Andy 	return 0;
14711b7bde6dSNimrod Andy }
14721b7bde6dSNimrod Andy 
14731b7bde6dSNimrod Andy static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
14741310b544SLothar Waßmann 			       struct bufdesc *bdp, u32 length, bool swap)
14751b7bde6dSNimrod Andy {
14761b7bde6dSNimrod Andy 	struct  fec_enet_private *fep = netdev_priv(ndev);
14771b7bde6dSNimrod Andy 	struct sk_buff *new_skb;
14781b7bde6dSNimrod Andy 
14791b7bde6dSNimrod Andy 	if (length > fep->rx_copybreak)
14801b7bde6dSNimrod Andy 		return false;
14811b7bde6dSNimrod Andy 
14821b7bde6dSNimrod Andy 	new_skb = netdev_alloc_skb(ndev, length);
14831b7bde6dSNimrod Andy 	if (!new_skb)
14841b7bde6dSNimrod Andy 		return false;
14851b7bde6dSNimrod Andy 
14865cfa3039SJohannes Berg 	dma_sync_single_for_cpu(&fep->pdev->dev,
14875cfa3039SJohannes Berg 				fec32_to_cpu(bdp->cbd_bufaddr),
14881b7bde6dSNimrod Andy 				FEC_ENET_RX_FRSIZE - fep->rx_align,
14891b7bde6dSNimrod Andy 				DMA_FROM_DEVICE);
14901310b544SLothar Waßmann 	if (!swap)
14911b7bde6dSNimrod Andy 		memcpy(new_skb->data, (*skb)->data, length);
14921310b544SLothar Waßmann 	else
14931310b544SLothar Waßmann 		swap_buffer2(new_skb->data, (*skb)->data, length);
14941b7bde6dSNimrod Andy 	*skb = new_skb;
14951b7bde6dSNimrod Andy 
14961b7bde6dSNimrod Andy 	return true;
14971b7bde6dSNimrod Andy }
14981b7bde6dSNimrod Andy 
14997355f276STroy Kisky /* During a receive, the bd_rx.cur points to the current incoming buffer.
1500793fc096SFrank Li  * When we update through the ring, if the next incoming buffer has
1501793fc096SFrank Li  * not been given to the system, we just set the empty indicator,
1502793fc096SFrank Li  * effectively tossing the packet.
1503793fc096SFrank Li  */
1504793fc096SFrank Li static int
15054d494cdcSFugang Duan fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1506793fc096SFrank Li {
1507793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
15084d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
1509793fc096SFrank Li 	struct bufdesc *bdp;
1510793fc096SFrank Li 	unsigned short status;
15111b7bde6dSNimrod Andy 	struct  sk_buff *skb_new = NULL;
1512793fc096SFrank Li 	struct  sk_buff *skb;
1513793fc096SFrank Li 	ushort	pkt_len;
1514793fc096SFrank Li 	__u8 *data;
1515793fc096SFrank Li 	int	pkt_received = 0;
1516cdffcf1bSJim Baxter 	struct	bufdesc_ex *ebdp = NULL;
1517cdffcf1bSJim Baxter 	bool	vlan_packet_rcvd = false;
1518cdffcf1bSJim Baxter 	u16	vlan_tag;
1519d842a31fSDuan Fugang-B38611 	int	index = 0;
15201b7bde6dSNimrod Andy 	bool	is_copybreak;
15216b7e4008SLothar Waßmann 	bool	need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1522793fc096SFrank Li 
1523793fc096SFrank Li #ifdef CONFIG_M532x
1524793fc096SFrank Li 	flush_cache_all();
1525793fc096SFrank Li #endif
15264d494cdcSFugang Duan 	rxq = fep->rx_queue[queue_id];
1527793fc096SFrank Li 
1528793fc096SFrank Li 	/* First, grab all of the stats for the incoming packet.
1529793fc096SFrank Li 	 * These get messed up if we get called due to a busy condition.
1530793fc096SFrank Li 	 */
15317355f276STroy Kisky 	bdp = rxq->bd.cur;
1532793fc096SFrank Li 
15335cfa3039SJohannes Berg 	while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1534793fc096SFrank Li 
1535793fc096SFrank Li 		if (pkt_received >= budget)
1536793fc096SFrank Li 			break;
1537793fc096SFrank Li 		pkt_received++;
1538793fc096SFrank Li 
1539b5bd95d1SJoakim Zhang 		writel(FEC_ENET_RXF_GET(queue_id), fep->hwp + FEC_IEVENT);
1540db3421c1SRussell King 
1541793fc096SFrank Li 		/* Check for errors. */
1542095098e1STroy Kisky 		status ^= BD_ENET_RX_LAST;
1543793fc096SFrank Li 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1544095098e1STroy Kisky 			   BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1545095098e1STroy Kisky 			   BD_ENET_RX_CL)) {
1546793fc096SFrank Li 			ndev->stats.rx_errors++;
1547095098e1STroy Kisky 			if (status & BD_ENET_RX_OV) {
1548095098e1STroy Kisky 				/* FIFO overrun */
1549095098e1STroy Kisky 				ndev->stats.rx_fifo_errors++;
1550095098e1STroy Kisky 				goto rx_processing_done;
1551095098e1STroy Kisky 			}
1552095098e1STroy Kisky 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1553095098e1STroy Kisky 						| BD_ENET_RX_LAST)) {
1554793fc096SFrank Li 				/* Frame too long or too short. */
1555793fc096SFrank Li 				ndev->stats.rx_length_errors++;
1556095098e1STroy Kisky 				if (status & BD_ENET_RX_LAST)
1557095098e1STroy Kisky 					netdev_err(ndev, "rcv is not +last\n");
1558793fc096SFrank Li 			}
1559793fc096SFrank Li 			if (status & BD_ENET_RX_CR)	/* CRC Error */
1560793fc096SFrank Li 				ndev->stats.rx_crc_errors++;
1561095098e1STroy Kisky 			/* Report late collisions as a frame error. */
1562095098e1STroy Kisky 			if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1563793fc096SFrank Li 				ndev->stats.rx_frame_errors++;
1564793fc096SFrank Li 			goto rx_processing_done;
1565793fc096SFrank Li 		}
1566793fc096SFrank Li 
1567793fc096SFrank Li 		/* Process the incoming frame. */
1568793fc096SFrank Li 		ndev->stats.rx_packets++;
15695cfa3039SJohannes Berg 		pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1570793fc096SFrank Li 		ndev->stats.rx_bytes += pkt_len;
1571793fc096SFrank Li 
15727355f276STroy Kisky 		index = fec_enet_get_bd_index(bdp, &rxq->bd);
15731b7bde6dSNimrod Andy 		skb = rxq->rx_skbuff[index];
15741b7bde6dSNimrod Andy 
15751b7bde6dSNimrod Andy 		/* The packet length includes FCS, but we don't want to
15761b7bde6dSNimrod Andy 		 * include that when passing upstream as it messes up
15771b7bde6dSNimrod Andy 		 * bridging applications.
15781b7bde6dSNimrod Andy 		 */
15791310b544SLothar Waßmann 		is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
15801310b544SLothar Waßmann 						  need_swap);
15811b7bde6dSNimrod Andy 		if (!is_copybreak) {
15821b7bde6dSNimrod Andy 			skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
15831b7bde6dSNimrod Andy 			if (unlikely(!skb_new)) {
15841b7bde6dSNimrod Andy 				ndev->stats.rx_dropped++;
15851b7bde6dSNimrod Andy 				goto rx_processing_done;
15861b7bde6dSNimrod Andy 			}
15875cfa3039SJohannes Berg 			dma_unmap_single(&fep->pdev->dev,
15885cfa3039SJohannes Berg 					 fec32_to_cpu(bdp->cbd_bufaddr),
1589b64bf4b7SNimrod Andy 					 FEC_ENET_RX_FRSIZE - fep->rx_align,
1590b64bf4b7SNimrod Andy 					 DMA_FROM_DEVICE);
15911b7bde6dSNimrod Andy 		}
1592793fc096SFrank Li 
15931b7bde6dSNimrod Andy 		prefetch(skb->data - NET_IP_ALIGN);
15941b7bde6dSNimrod Andy 		skb_put(skb, pkt_len - 4);
15951b7bde6dSNimrod Andy 		data = skb->data;
15963ac72b7bSEric Nelson 
1597235bde1eSFabio Estevam 		if (!is_copybreak && need_swap)
1598235bde1eSFabio Estevam 			swap_buffer(data, pkt_len);
1599235bde1eSFabio Estevam 
16003ac72b7bSEric Nelson #if !defined(CONFIG_M5272)
16013ac72b7bSEric Nelson 		if (fep->quirks & FEC_QUIRK_HAS_RACC)
16023ac72b7bSEric Nelson 			data = skb_pull_inline(skb, 2);
16033ac72b7bSEric Nelson #endif
16043ac72b7bSEric Nelson 
1605cdffcf1bSJim Baxter 		/* Extract the enhanced buffer descriptor */
1606cdffcf1bSJim Baxter 		ebdp = NULL;
1607cdffcf1bSJim Baxter 		if (fep->bufdesc_ex)
1608cdffcf1bSJim Baxter 			ebdp = (struct bufdesc_ex *)bdp;
1609cdffcf1bSJim Baxter 
1610cdffcf1bSJim Baxter 		/* If this is a VLAN packet remove the VLAN Tag */
1611cdffcf1bSJim Baxter 		vlan_packet_rcvd = false;
1612cdffcf1bSJim Baxter 		if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
16135cfa3039SJohannes Berg 		    fep->bufdesc_ex &&
16145cfa3039SJohannes Berg 		    (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1615cdffcf1bSJim Baxter 			/* Push and remove the vlan tag */
1616cdffcf1bSJim Baxter 			struct vlan_hdr *vlan_header =
1617cdffcf1bSJim Baxter 					(struct vlan_hdr *) (data + ETH_HLEN);
1618cdffcf1bSJim Baxter 			vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1619cdffcf1bSJim Baxter 
1620cdffcf1bSJim Baxter 			vlan_packet_rcvd = true;
16211b7bde6dSNimrod Andy 
1622af5cbc98SNimrod Andy 			memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
16231b7bde6dSNimrod Andy 			skb_pull(skb, VLAN_HLEN);
1624cdffcf1bSJim Baxter 		}
1625cdffcf1bSJim Baxter 
1626793fc096SFrank Li 		skb->protocol = eth_type_trans(skb, ndev);
1627793fc096SFrank Li 
1628793fc096SFrank Li 		/* Get receive timestamp from the skb */
1629bfd4ecddSRussell King 		if (fep->hwts_rx_en && fep->bufdesc_ex)
16305cfa3039SJohannes Berg 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1631bfd4ecddSRussell King 					  skb_hwtstamps(skb));
1632793fc096SFrank Li 
16334c09eed9SJim Baxter 		if (fep->bufdesc_ex &&
16344c09eed9SJim Baxter 		    (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
16355cfa3039SJohannes Berg 			if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
16364c09eed9SJim Baxter 				/* don't check it */
16374c09eed9SJim Baxter 				skb->ip_summed = CHECKSUM_UNNECESSARY;
16384c09eed9SJim Baxter 			} else {
16394c09eed9SJim Baxter 				skb_checksum_none_assert(skb);
16404c09eed9SJim Baxter 			}
16414c09eed9SJim Baxter 		}
16424c09eed9SJim Baxter 
1643cdffcf1bSJim Baxter 		/* Handle received VLAN packets */
1644cdffcf1bSJim Baxter 		if (vlan_packet_rcvd)
1645cdffcf1bSJim Baxter 			__vlan_hwaccel_put_tag(skb,
1646cdffcf1bSJim Baxter 					       htons(ETH_P_8021Q),
1647cdffcf1bSJim Baxter 					       vlan_tag);
1648cdffcf1bSJim Baxter 
16497cdaa4ccSTobias Waldekranz 		skb_record_rx_queue(skb, queue_id);
1650793fc096SFrank Li 		napi_gro_receive(&fep->napi, skb);
1651793fc096SFrank Li 
16521b7bde6dSNimrod Andy 		if (is_copybreak) {
16535cfa3039SJohannes Berg 			dma_sync_single_for_device(&fep->pdev->dev,
16545cfa3039SJohannes Berg 						   fec32_to_cpu(bdp->cbd_bufaddr),
1655b64bf4b7SNimrod Andy 						   FEC_ENET_RX_FRSIZE - fep->rx_align,
1656b64bf4b7SNimrod Andy 						   DMA_FROM_DEVICE);
16571b7bde6dSNimrod Andy 		} else {
16581b7bde6dSNimrod Andy 			rxq->rx_skbuff[index] = skb_new;
16591b7bde6dSNimrod Andy 			fec_enet_new_rxbdp(ndev, bdp, skb_new);
16601b7bde6dSNimrod Andy 		}
16611b7bde6dSNimrod Andy 
1662793fc096SFrank Li rx_processing_done:
1663793fc096SFrank Li 		/* Clear the status flags for this buffer */
1664793fc096SFrank Li 		status &= ~BD_ENET_RX_STATS;
1665793fc096SFrank Li 
1666793fc096SFrank Li 		/* Mark the buffer empty */
1667793fc096SFrank Li 		status |= BD_ENET_RX_EMPTY;
1668793fc096SFrank Li 
1669793fc096SFrank Li 		if (fep->bufdesc_ex) {
1670793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1671793fc096SFrank Li 
16725cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1673793fc096SFrank Li 			ebdp->cbd_prot = 0;
1674793fc096SFrank Li 			ebdp->cbd_bdu = 0;
1675793fc096SFrank Li 		}
1676be293467STroy Kisky 		/* Make sure the updates to rest of the descriptor are
1677be293467STroy Kisky 		 * performed before transferring ownership.
1678be293467STroy Kisky 		 */
1679be293467STroy Kisky 		wmb();
1680be293467STroy Kisky 		bdp->cbd_sc = cpu_to_fec16(status);
1681793fc096SFrank Li 
1682793fc096SFrank Li 		/* Update BD pointer to next entry */
16837355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
168436e24e2eSDuan Fugang-B38611 
1685793fc096SFrank Li 		/* Doing this here will keep the FEC running while we process
1686793fc096SFrank Li 		 * incoming frames.  On a heavily loaded network, we should be
1687793fc096SFrank Li 		 * able to keep up at the expense of system resources.
1688793fc096SFrank Li 		 */
168953bb20d1STroy Kisky 		writel(0, rxq->bd.reg_desc_active);
1690793fc096SFrank Li 	}
16917355f276STroy Kisky 	rxq->bd.cur = bdp;
1692793fc096SFrank Li 	return pkt_received;
1693793fc096SFrank Li }
1694793fc096SFrank Li 
16957cdaa4ccSTobias Waldekranz static int fec_enet_rx(struct net_device *ndev, int budget)
16964d494cdcSFugang Duan {
16974d494cdcSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
16987cdaa4ccSTobias Waldekranz 	int i, done = 0;
16994d494cdcSFugang Duan 
17007cdaa4ccSTobias Waldekranz 	/* Make sure that AVB queues are processed first. */
17017cdaa4ccSTobias Waldekranz 	for (i = fep->num_rx_queues - 1; i >= 0; i--)
17027cdaa4ccSTobias Waldekranz 		done += fec_enet_rx_queue(ndev, budget - done, i);
17031c021bb7SUwe Kleine-König 
17047cdaa4ccSTobias Waldekranz 	return done;
17054d494cdcSFugang Duan }
17064d494cdcSFugang Duan 
17077cdaa4ccSTobias Waldekranz static bool fec_enet_collect_events(struct fec_enet_private *fep)
17084d494cdcSFugang Duan {
1709793fc096SFrank Li 	uint int_events;
1710793fc096SFrank Li 
1711793fc096SFrank Li 	int_events = readl(fep->hwp + FEC_IEVENT);
1712f166f890SAndrew Lunn 
1713f166f890SAndrew Lunn 	/* Don't clear MDIO events, we poll for those */
1714f166f890SAndrew Lunn 	int_events &= ~FEC_ENET_MII;
1715f166f890SAndrew Lunn 
171694191fd6SNimrod Andy 	writel(int_events, fep->hwp + FEC_IEVENT);
1717793fc096SFrank Li 
17187cdaa4ccSTobias Waldekranz 	return int_events != 0;
17197cdaa4ccSTobias Waldekranz }
17207cdaa4ccSTobias Waldekranz 
17217cdaa4ccSTobias Waldekranz static irqreturn_t
17227cdaa4ccSTobias Waldekranz fec_enet_interrupt(int irq, void *dev_id)
17237cdaa4ccSTobias Waldekranz {
17247cdaa4ccSTobias Waldekranz 	struct net_device *ndev = dev_id;
17257cdaa4ccSTobias Waldekranz 	struct fec_enet_private *fep = netdev_priv(ndev);
17267cdaa4ccSTobias Waldekranz 	irqreturn_t ret = IRQ_NONE;
17277cdaa4ccSTobias Waldekranz 
17287cdaa4ccSTobias Waldekranz 	if (fec_enet_collect_events(fep) && fep->link) {
1729793fc096SFrank Li 		ret = IRQ_HANDLED;
1730793fc096SFrank Li 
173194191fd6SNimrod Andy 		if (napi_schedule_prep(&fep->napi)) {
1732f166f890SAndrew Lunn 			/* Disable interrupts */
1733f166f890SAndrew Lunn 			writel(0, fep->hwp + FEC_IMASK);
173494191fd6SNimrod Andy 			__napi_schedule(&fep->napi);
173594191fd6SNimrod Andy 		}
1736793fc096SFrank Li 	}
1737793fc096SFrank Li 
1738793fc096SFrank Li 	return ret;
1739793fc096SFrank Li }
1740793fc096SFrank Li 
1741793fc096SFrank Li static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1742793fc096SFrank Li {
1743793fc096SFrank Li 	struct net_device *ndev = napi->dev;
1744793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
17457cdaa4ccSTobias Waldekranz 	int done = 0;
17467a16807cSRussell King 
17477cdaa4ccSTobias Waldekranz 	do {
17487cdaa4ccSTobias Waldekranz 		done += fec_enet_rx(ndev, budget - done);
1749793fc096SFrank Li 		fec_enet_tx(ndev);
17507cdaa4ccSTobias Waldekranz 	} while ((done < budget) && fec_enet_collect_events(fep));
1751793fc096SFrank Li 
17527cdaa4ccSTobias Waldekranz 	if (done < budget) {
17537cdaa4ccSTobias Waldekranz 		napi_complete_done(napi, done);
1754793fc096SFrank Li 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1755793fc096SFrank Li 	}
17567cdaa4ccSTobias Waldekranz 
17577cdaa4ccSTobias Waldekranz 	return done;
1758793fc096SFrank Li }
1759793fc096SFrank Li 
1760793fc096SFrank Li /* ------------------------------------------------------------------------- */
1761052fcc45SFugang Duan static int fec_get_mac(struct net_device *ndev)
1762793fc096SFrank Li {
1763793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
1764793fc096SFrank Li 	unsigned char *iap, tmpaddr[ETH_ALEN];
176583216e39SMichael Walle 	int ret;
1766793fc096SFrank Li 
1767793fc096SFrank Li 	/*
1768793fc096SFrank Li 	 * try to get mac address in following order:
1769793fc096SFrank Li 	 *
1770793fc096SFrank Li 	 * 1) module parameter via kernel command line in form
1771793fc096SFrank Li 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1772793fc096SFrank Li 	 */
1773793fc096SFrank Li 	iap = macaddr;
1774793fc096SFrank Li 
1775793fc096SFrank Li 	/*
1776793fc096SFrank Li 	 * 2) from device tree data
1777793fc096SFrank Li 	 */
1778793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1779793fc096SFrank Li 		struct device_node *np = fep->pdev->dev.of_node;
1780793fc096SFrank Li 		if (np) {
178183216e39SMichael Walle 			ret = of_get_mac_address(np, tmpaddr);
178283216e39SMichael Walle 			if (!ret)
178383216e39SMichael Walle 				iap = tmpaddr;
1784052fcc45SFugang Duan 			else if (ret == -EPROBE_DEFER)
1785052fcc45SFugang Duan 				return ret;
1786793fc096SFrank Li 		}
1787793fc096SFrank Li 	}
1788793fc096SFrank Li 
1789793fc096SFrank Li 	/*
1790793fc096SFrank Li 	 * 3) from flash or fuse (via platform data)
1791793fc096SFrank Li 	 */
1792793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
1793793fc096SFrank Li #ifdef CONFIG_M5272
1794793fc096SFrank Li 		if (FEC_FLASHMAC)
1795793fc096SFrank Li 			iap = (unsigned char *)FEC_FLASHMAC;
1796793fc096SFrank Li #else
179732d1bbb1SGeert Uytterhoeven 		struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
179832d1bbb1SGeert Uytterhoeven 
1799793fc096SFrank Li 		if (pdata)
1800793fc096SFrank Li 			iap = (unsigned char *)&pdata->mac;
1801793fc096SFrank Li #endif
1802793fc096SFrank Li 	}
1803793fc096SFrank Li 
1804793fc096SFrank Li 	/*
1805793fc096SFrank Li 	 * 4) FEC mac registers set by bootloader
1806793fc096SFrank Li 	 */
1807793fc096SFrank Li 	if (!is_valid_ether_addr(iap)) {
18087d7628f3SDan Carpenter 		*((__be32 *) &tmpaddr[0]) =
18097d7628f3SDan Carpenter 			cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
18107d7628f3SDan Carpenter 		*((__be16 *) &tmpaddr[4]) =
18117d7628f3SDan Carpenter 			cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1812793fc096SFrank Li 		iap = &tmpaddr[0];
1813793fc096SFrank Li 	}
1814793fc096SFrank Li 
1815ff5b2fabSLucas Stach 	/*
1816ff5b2fabSLucas Stach 	 * 5) random mac address
1817ff5b2fabSLucas Stach 	 */
1818ff5b2fabSLucas Stach 	if (!is_valid_ether_addr(iap)) {
1819ff5b2fabSLucas Stach 		/* Report it and use a random ethernet address instead */
1820a19a0582SFabio Estevam 		dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
1821ff5b2fabSLucas Stach 		eth_hw_addr_random(ndev);
1822a19a0582SFabio Estevam 		dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
1823ff5b2fabSLucas Stach 			 ndev->dev_addr);
1824052fcc45SFugang Duan 		return 0;
1825ff5b2fabSLucas Stach 	}
1826ff5b2fabSLucas Stach 
1827793fc096SFrank Li 	/* Adjust MAC if using macaddr */
1828ba3fdfe3SJakub Kicinski 	eth_hw_addr_gen(ndev, iap, iap == macaddr ? fep->dev_id : 0);
1829052fcc45SFugang Duan 
1830052fcc45SFugang Duan 	return 0;
1831793fc096SFrank Li }
1832793fc096SFrank Li 
1833793fc096SFrank Li /* ------------------------------------------------------------------------- */
1834793fc096SFrank Li 
1835793fc096SFrank Li /*
1836793fc096SFrank Li  * Phy section
1837793fc096SFrank Li  */
1838793fc096SFrank Li static void fec_enet_adjust_link(struct net_device *ndev)
1839793fc096SFrank Li {
1840793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
184145f5c327SPhilippe Reynes 	struct phy_device *phy_dev = ndev->phydev;
1842793fc096SFrank Li 	int status_change = 0;
1843793fc096SFrank Li 
18448ce5624fSRussell King 	/*
18458ce5624fSRussell King 	 * If the netdev is down, or is going down, we're not interested
18468ce5624fSRussell King 	 * in link state events, so just mark our idea of the link as down
18478ce5624fSRussell King 	 * and ignore the event.
18488ce5624fSRussell King 	 */
18498ce5624fSRussell King 	if (!netif_running(ndev) || !netif_device_present(ndev)) {
18508ce5624fSRussell King 		fep->link = 0;
18518ce5624fSRussell King 	} else if (phy_dev->link) {
1852793fc096SFrank Li 		if (!fep->link) {
1853793fc096SFrank Li 			fep->link = phy_dev->link;
1854793fc096SFrank Li 			status_change = 1;
1855793fc096SFrank Li 		}
1856793fc096SFrank Li 
1857ef83337dSRussell King 		if (fep->full_duplex != phy_dev->duplex) {
1858ef83337dSRussell King 			fep->full_duplex = phy_dev->duplex;
1859793fc096SFrank Li 			status_change = 1;
1860ef83337dSRussell King 		}
1861793fc096SFrank Li 
1862793fc096SFrank Li 		if (phy_dev->speed != fep->speed) {
1863793fc096SFrank Li 			fep->speed = phy_dev->speed;
1864793fc096SFrank Li 			status_change = 1;
1865793fc096SFrank Li 		}
1866793fc096SFrank Li 
1867793fc096SFrank Li 		/* if any of the above changed restart the FEC */
1868dbc64a8eSRussell King 		if (status_change) {
1869dbc64a8eSRussell King 			napi_disable(&fep->napi);
1870dbc64a8eSRussell King 			netif_tx_lock_bh(ndev);
1871ef83337dSRussell King 			fec_restart(ndev);
1872657ade07SRickard x Andersson 			netif_tx_wake_all_queues(ndev);
18736af42d42SRussell King 			netif_tx_unlock_bh(ndev);
1874dbc64a8eSRussell King 			napi_enable(&fep->napi);
1875dbc64a8eSRussell King 		}
1876793fc096SFrank Li 	} else {
1877793fc096SFrank Li 		if (fep->link) {
1878f208ce10SRussell King 			napi_disable(&fep->napi);
1879f208ce10SRussell King 			netif_tx_lock_bh(ndev);
1880793fc096SFrank Li 			fec_stop(ndev);
1881f208ce10SRussell King 			netif_tx_unlock_bh(ndev);
1882f208ce10SRussell King 			napi_enable(&fep->napi);
18836e0895c2SDavid S. Miller 			fep->link = phy_dev->link;
1884793fc096SFrank Li 			status_change = 1;
1885793fc096SFrank Li 		}
1886793fc096SFrank Li 	}
1887793fc096SFrank Li 
1888793fc096SFrank Li 	if (status_change)
1889793fc096SFrank Li 		phy_print_status(phy_dev);
1890793fc096SFrank Li }
1891793fc096SFrank Li 
1892f166f890SAndrew Lunn static int fec_enet_mdio_wait(struct fec_enet_private *fep)
1893f166f890SAndrew Lunn {
1894f166f890SAndrew Lunn 	uint ievent;
1895f166f890SAndrew Lunn 	int ret;
1896f166f890SAndrew Lunn 
1897f166f890SAndrew Lunn 	ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent,
1898f166f890SAndrew Lunn 					ievent & FEC_ENET_MII, 2, 30000);
1899f166f890SAndrew Lunn 
1900f166f890SAndrew Lunn 	if (!ret)
1901f166f890SAndrew Lunn 		writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
1902f166f890SAndrew Lunn 
1903f166f890SAndrew Lunn 	return ret;
1904f166f890SAndrew Lunn }
1905f166f890SAndrew Lunn 
1906793fc096SFrank Li static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1907793fc096SFrank Li {
1908793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
19098fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
1910d3ee8ec7SMarco Hartmann 	int ret = 0, frame_start, frame_addr, frame_op;
1911d3ee8ec7SMarco Hartmann 	bool is_c45 = !!(regnum & MII_ADDR_C45);
19128fff755eSAndrew Lunn 
1913da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
1914b0c6ce24SFabio Estevam 	if (ret < 0)
19158fff755eSAndrew Lunn 		return ret;
1916793fc096SFrank Li 
1917d3ee8ec7SMarco Hartmann 	if (is_c45) {
1918d3ee8ec7SMarco Hartmann 		frame_start = FEC_MMFR_ST_C45;
1919d3ee8ec7SMarco Hartmann 
1920d3ee8ec7SMarco Hartmann 		/* write address */
1921d3ee8ec7SMarco Hartmann 		frame_addr = (regnum >> 16);
1922d3ee8ec7SMarco Hartmann 		writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1923d3ee8ec7SMarco Hartmann 		       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1924d3ee8ec7SMarco Hartmann 		       FEC_MMFR_TA | (regnum & 0xFFFF),
1925d3ee8ec7SMarco Hartmann 		       fep->hwp + FEC_MII_DATA);
1926d3ee8ec7SMarco Hartmann 
1927d3ee8ec7SMarco Hartmann 		/* wait for end of transfer */
1928f166f890SAndrew Lunn 		ret = fec_enet_mdio_wait(fep);
1929f166f890SAndrew Lunn 		if (ret) {
1930d3ee8ec7SMarco Hartmann 			netdev_err(fep->netdev, "MDIO address write timeout\n");
1931d3ee8ec7SMarco Hartmann 			goto out;
1932d3ee8ec7SMarco Hartmann 		}
1933d3ee8ec7SMarco Hartmann 
1934d3ee8ec7SMarco Hartmann 		frame_op = FEC_MMFR_OP_READ_C45;
1935d3ee8ec7SMarco Hartmann 
1936d3ee8ec7SMarco Hartmann 	} else {
1937d3ee8ec7SMarco Hartmann 		/* C22 read */
1938d3ee8ec7SMarco Hartmann 		frame_op = FEC_MMFR_OP_READ;
1939d3ee8ec7SMarco Hartmann 		frame_start = FEC_MMFR_ST;
1940d3ee8ec7SMarco Hartmann 		frame_addr = regnum;
1941d3ee8ec7SMarco Hartmann 	}
1942d3ee8ec7SMarco Hartmann 
1943793fc096SFrank Li 	/* start a read op */
1944d3ee8ec7SMarco Hartmann 	writel(frame_start | frame_op |
1945d3ee8ec7SMarco Hartmann 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1946793fc096SFrank Li 		FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1947793fc096SFrank Li 
1948793fc096SFrank Li 	/* wait for end of transfer */
1949f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
1950f166f890SAndrew Lunn 	if (ret) {
195131b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO read timeout\n");
19528fff755eSAndrew Lunn 		goto out;
1953793fc096SFrank Li 	}
1954793fc096SFrank Li 
19558fff755eSAndrew Lunn 	ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
19568fff755eSAndrew Lunn 
19578fff755eSAndrew Lunn out:
19588fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
19598fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
19608fff755eSAndrew Lunn 
19618fff755eSAndrew Lunn 	return ret;
1962793fc096SFrank Li }
1963793fc096SFrank Li 
1964793fc096SFrank Li static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1965793fc096SFrank Li 			   u16 value)
1966793fc096SFrank Li {
1967793fc096SFrank Li 	struct fec_enet_private *fep = bus->priv;
19688fff755eSAndrew Lunn 	struct device *dev = &fep->pdev->dev;
1969d3ee8ec7SMarco Hartmann 	int ret, frame_start, frame_addr;
1970d3ee8ec7SMarco Hartmann 	bool is_c45 = !!(regnum & MII_ADDR_C45);
19718fff755eSAndrew Lunn 
1972da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
1973b0c6ce24SFabio Estevam 	if (ret < 0)
19748fff755eSAndrew Lunn 		return ret;
1975793fc096SFrank Li 
1976d3ee8ec7SMarco Hartmann 	if (is_c45) {
1977d3ee8ec7SMarco Hartmann 		frame_start = FEC_MMFR_ST_C45;
1978d3ee8ec7SMarco Hartmann 
1979d3ee8ec7SMarco Hartmann 		/* write address */
1980d3ee8ec7SMarco Hartmann 		frame_addr = (regnum >> 16);
1981d3ee8ec7SMarco Hartmann 		writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1982d3ee8ec7SMarco Hartmann 		       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1983d3ee8ec7SMarco Hartmann 		       FEC_MMFR_TA | (regnum & 0xFFFF),
1984d3ee8ec7SMarco Hartmann 		       fep->hwp + FEC_MII_DATA);
1985d3ee8ec7SMarco Hartmann 
1986d3ee8ec7SMarco Hartmann 		/* wait for end of transfer */
1987f166f890SAndrew Lunn 		ret = fec_enet_mdio_wait(fep);
1988f166f890SAndrew Lunn 		if (ret) {
1989d3ee8ec7SMarco Hartmann 			netdev_err(fep->netdev, "MDIO address write timeout\n");
1990d3ee8ec7SMarco Hartmann 			goto out;
1991d3ee8ec7SMarco Hartmann 		}
1992d3ee8ec7SMarco Hartmann 	} else {
1993d3ee8ec7SMarco Hartmann 		/* C22 write */
1994d3ee8ec7SMarco Hartmann 		frame_start = FEC_MMFR_ST;
1995d3ee8ec7SMarco Hartmann 		frame_addr = regnum;
1996d3ee8ec7SMarco Hartmann 	}
1997d3ee8ec7SMarco Hartmann 
1998793fc096SFrank Li 	/* start a write op */
1999d3ee8ec7SMarco Hartmann 	writel(frame_start | FEC_MMFR_OP_WRITE |
2000d3ee8ec7SMarco Hartmann 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
2001793fc096SFrank Li 		FEC_MMFR_TA | FEC_MMFR_DATA(value),
2002793fc096SFrank Li 		fep->hwp + FEC_MII_DATA);
2003793fc096SFrank Li 
2004793fc096SFrank Li 	/* wait for end of transfer */
2005f166f890SAndrew Lunn 	ret = fec_enet_mdio_wait(fep);
2006f166f890SAndrew Lunn 	if (ret)
200731b7720cSJoe Perches 		netdev_err(fep->netdev, "MDIO write timeout\n");
2008793fc096SFrank Li 
2009d3ee8ec7SMarco Hartmann out:
20108fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(dev);
20118fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(dev);
20128fff755eSAndrew Lunn 
20138fff755eSAndrew Lunn 	return ret;
2014793fc096SFrank Li }
2015793fc096SFrank Li 
201664a632daSMarek Vasut static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
201764a632daSMarek Vasut {
201864a632daSMarek Vasut 	struct fec_enet_private *fep = netdev_priv(ndev);
201964a632daSMarek Vasut 	struct phy_device *phy_dev = ndev->phydev;
202064a632daSMarek Vasut 
202164a632daSMarek Vasut 	if (phy_dev) {
202264a632daSMarek Vasut 		phy_reset_after_clk_enable(phy_dev);
202364a632daSMarek Vasut 	} else if (fep->phy_node) {
202464a632daSMarek Vasut 		/*
202564a632daSMarek Vasut 		 * If the PHY still is not bound to the MAC, but there is
202664a632daSMarek Vasut 		 * OF PHY node and a matching PHY device instance already,
202764a632daSMarek Vasut 		 * use the OF PHY node to obtain the PHY device instance,
202864a632daSMarek Vasut 		 * and then use that PHY device instance when triggering
202964a632daSMarek Vasut 		 * the PHY reset.
203064a632daSMarek Vasut 		 */
203164a632daSMarek Vasut 		phy_dev = of_phy_find_device(fep->phy_node);
203264a632daSMarek Vasut 		phy_reset_after_clk_enable(phy_dev);
203364a632daSMarek Vasut 		put_device(&phy_dev->mdio.dev);
203464a632daSMarek Vasut 	}
203564a632daSMarek Vasut }
203664a632daSMarek Vasut 
2037e8fcfcd5SNimrod Andy static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
2038e8fcfcd5SNimrod Andy {
2039e8fcfcd5SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2040e8fcfcd5SNimrod Andy 	int ret;
2041e8fcfcd5SNimrod Andy 
2042e8fcfcd5SNimrod Andy 	if (enable) {
2043e8fcfcd5SNimrod Andy 		ret = clk_prepare_enable(fep->clk_enet_out);
2044e8fcfcd5SNimrod Andy 		if (ret)
2045d7c3a206SAndy Duan 			return ret;
204601e5943aSUwe Kleine-König 
2047e8fcfcd5SNimrod Andy 		if (fep->clk_ptp) {
204801b825f9SFrancesco Dolcini 			mutex_lock(&fep->ptp_clk_mutex);
2049e8fcfcd5SNimrod Andy 			ret = clk_prepare_enable(fep->clk_ptp);
205091c0d987SNimrod Andy 			if (ret) {
205101b825f9SFrancesco Dolcini 				mutex_unlock(&fep->ptp_clk_mutex);
2052e8fcfcd5SNimrod Andy 				goto failed_clk_ptp;
205391c0d987SNimrod Andy 			} else {
205491c0d987SNimrod Andy 				fep->ptp_clk_on = true;
205591c0d987SNimrod Andy 			}
205601b825f9SFrancesco Dolcini 			mutex_unlock(&fep->ptp_clk_mutex);
2057e8fcfcd5SNimrod Andy 		}
205801e5943aSUwe Kleine-König 
20599b5330edSFugang Duan 		ret = clk_prepare_enable(fep->clk_ref);
20609b5330edSFugang Duan 		if (ret)
20619b5330edSFugang Duan 			goto failed_clk_ref;
20621b0a83acSRichard Leitner 
2063fc539459SFugang Duan 		ret = clk_prepare_enable(fep->clk_2x_txclk);
2064fc539459SFugang Duan 		if (ret)
2065fc539459SFugang Duan 			goto failed_clk_2x_txclk;
2066fc539459SFugang Duan 
206764a632daSMarek Vasut 		fec_enet_phy_reset_after_clk_enable(ndev);
2068e8fcfcd5SNimrod Andy 	} else {
2069e8fcfcd5SNimrod Andy 		clk_disable_unprepare(fep->clk_enet_out);
207091c0d987SNimrod Andy 		if (fep->clk_ptp) {
207101b825f9SFrancesco Dolcini 			mutex_lock(&fep->ptp_clk_mutex);
2072e8fcfcd5SNimrod Andy 			clk_disable_unprepare(fep->clk_ptp);
207391c0d987SNimrod Andy 			fep->ptp_clk_on = false;
207401b825f9SFrancesco Dolcini 			mutex_unlock(&fep->ptp_clk_mutex);
207591c0d987SNimrod Andy 		}
20769b5330edSFugang Duan 		clk_disable_unprepare(fep->clk_ref);
2077fc539459SFugang Duan 		clk_disable_unprepare(fep->clk_2x_txclk);
2078e8fcfcd5SNimrod Andy 	}
2079e8fcfcd5SNimrod Andy 
2080e8fcfcd5SNimrod Andy 	return 0;
20819b5330edSFugang Duan 
2082fc539459SFugang Duan failed_clk_2x_txclk:
2083fc539459SFugang Duan 	if (fep->clk_ref)
2084fc539459SFugang Duan 		clk_disable_unprepare(fep->clk_ref);
20859b5330edSFugang Duan failed_clk_ref:
2086a74d19baSLiu Xiang 	if (fep->clk_ptp) {
208701b825f9SFrancesco Dolcini 		mutex_lock(&fep->ptp_clk_mutex);
2088a74d19baSLiu Xiang 		clk_disable_unprepare(fep->clk_ptp);
2089a74d19baSLiu Xiang 		fep->ptp_clk_on = false;
209001b825f9SFrancesco Dolcini 		mutex_unlock(&fep->ptp_clk_mutex);
2091a74d19baSLiu Xiang 	}
2092e8fcfcd5SNimrod Andy failed_clk_ptp:
2093e8fcfcd5SNimrod Andy 	clk_disable_unprepare(fep->clk_enet_out);
2094e8fcfcd5SNimrod Andy 
2095e8fcfcd5SNimrod Andy 	return ret;
2096e8fcfcd5SNimrod Andy }
2097e8fcfcd5SNimrod Andy 
2098b820c114SJoakim Zhang static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep,
2099b820c114SJoakim Zhang 				      struct device_node *np)
2100b820c114SJoakim Zhang {
2101b820c114SJoakim Zhang 	u32 rgmii_tx_delay, rgmii_rx_delay;
2102b820c114SJoakim Zhang 
2103b820c114SJoakim Zhang 	/* For rgmii tx internal delay, valid values are 0ps and 2000ps */
2104b820c114SJoakim Zhang 	if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) {
2105b820c114SJoakim Zhang 		if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) {
2106b820c114SJoakim Zhang 			dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps");
2107b820c114SJoakim Zhang 			return -EINVAL;
2108b820c114SJoakim Zhang 		} else if (rgmii_tx_delay == 2000) {
2109b820c114SJoakim Zhang 			fep->rgmii_txc_dly = true;
2110b820c114SJoakim Zhang 		}
2111b820c114SJoakim Zhang 	}
2112b820c114SJoakim Zhang 
2113b820c114SJoakim Zhang 	/* For rgmii rx internal delay, valid values are 0ps and 2000ps */
2114b820c114SJoakim Zhang 	if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) {
2115b820c114SJoakim Zhang 		if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) {
2116b820c114SJoakim Zhang 			dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps");
2117b820c114SJoakim Zhang 			return -EINVAL;
2118b820c114SJoakim Zhang 		} else if (rgmii_rx_delay == 2000) {
2119b820c114SJoakim Zhang 			fep->rgmii_rxc_dly = true;
2120b820c114SJoakim Zhang 		}
2121b820c114SJoakim Zhang 	}
2122b820c114SJoakim Zhang 
2123b820c114SJoakim Zhang 	return 0;
2124b820c114SJoakim Zhang }
2125b820c114SJoakim Zhang 
2126793fc096SFrank Li static int fec_enet_mii_probe(struct net_device *ndev)
2127793fc096SFrank Li {
2128793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2129793fc096SFrank Li 	struct phy_device *phy_dev = NULL;
2130793fc096SFrank Li 	char mdio_bus_id[MII_BUS_ID_SIZE];
2131793fc096SFrank Li 	char phy_name[MII_BUS_ID_SIZE + 3];
2132793fc096SFrank Li 	int phy_id;
2133793fc096SFrank Li 	int dev_id = fep->dev_id;
2134793fc096SFrank Li 
2135407066f8SUwe Kleine-König 	if (fep->phy_node) {
2136407066f8SUwe Kleine-König 		phy_dev = of_phy_connect(ndev, fep->phy_node,
2137407066f8SUwe Kleine-König 					 &fec_enet_adjust_link, 0,
2138407066f8SUwe Kleine-König 					 fep->phy_interface);
21399558df3aSAndrew Lunn 		if (!phy_dev) {
21409558df3aSAndrew Lunn 			netdev_err(ndev, "Unable to connect to phy\n");
2141213a9922SNimrod Andy 			return -ENODEV;
21429558df3aSAndrew Lunn 		}
2143407066f8SUwe Kleine-König 	} else {
2144793fc096SFrank Li 		/* check for attached phy */
2145793fc096SFrank Li 		for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
21467f854420SAndrew Lunn 			if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
2147793fc096SFrank Li 				continue;
2148793fc096SFrank Li 			if (dev_id--)
2149793fc096SFrank Li 				continue;
2150f029c781SWolfram Sang 			strscpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
2151793fc096SFrank Li 			break;
2152793fc096SFrank Li 		}
2153793fc096SFrank Li 
2154793fc096SFrank Li 		if (phy_id >= PHY_MAX_ADDR) {
215531b7720cSJoe Perches 			netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
2156f029c781SWolfram Sang 			strscpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
2157793fc096SFrank Li 			phy_id = 0;
2158793fc096SFrank Li 		}
2159793fc096SFrank Li 
2160407066f8SUwe Kleine-König 		snprintf(phy_name, sizeof(phy_name),
2161407066f8SUwe Kleine-König 			 PHY_ID_FMT, mdio_bus_id, phy_id);
2162793fc096SFrank Li 		phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
2163793fc096SFrank Li 				      fep->phy_interface);
2164407066f8SUwe Kleine-König 	}
2165407066f8SUwe Kleine-König 
2166793fc096SFrank Li 	if (IS_ERR(phy_dev)) {
216731b7720cSJoe Perches 		netdev_err(ndev, "could not attach to PHY\n");
2168793fc096SFrank Li 		return PTR_ERR(phy_dev);
2169793fc096SFrank Li 	}
2170793fc096SFrank Li 
2171793fc096SFrank Li 	/* mask with MAC supported features */
21726b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
217358056c1eSAndrew Lunn 		phy_set_max_speed(phy_dev, 1000);
217441124fa6SAndrew Lunn 		phy_remove_link_mode(phy_dev,
217541124fa6SAndrew Lunn 				     ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
2176d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2177c306ad36SAndrew Lunn 		phy_support_sym_pause(phy_dev);
2178d1391930SGuenter Roeck #endif
2179793fc096SFrank Li 	}
2180793fc096SFrank Li 	else
218158056c1eSAndrew Lunn 		phy_set_max_speed(phy_dev, 100);
2182793fc096SFrank Li 
2183793fc096SFrank Li 	fep->link = 0;
2184793fc096SFrank Li 	fep->full_duplex = 0;
2185793fc096SFrank Li 
2186557d5dc8SHeiner Kallweit 	phy_dev->mac_managed_pm = 1;
2187557d5dc8SHeiner Kallweit 
21882220943aSAndrew Lunn 	phy_attached_info(phy_dev);
2189793fc096SFrank Li 
2190793fc096SFrank Li 	return 0;
2191793fc096SFrank Li }
2192793fc096SFrank Li 
2193793fc096SFrank Li static int fec_enet_mii_init(struct platform_device *pdev)
2194793fc096SFrank Li {
2195793fc096SFrank Li 	static struct mii_bus *fec0_mii_bus;
2196793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
2197793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
21983c01eb62SAndrew Lunn 	bool suppress_preamble = false;
2199407066f8SUwe Kleine-König 	struct device_node *node;
2200e7f4dc35SAndrew Lunn 	int err = -ENXIO;
220163c60732SUwe Kleine-König 	u32 mii_speed, holdtime;
22023e782985SAndrew Lunn 	u32 bus_freq;
2203793fc096SFrank Li 
2204793fc096SFrank Li 	/*
22053d125f9cSStefan Agner 	 * The i.MX28 dual fec interfaces are not equal.
2206793fc096SFrank Li 	 * Here are the differences:
2207793fc096SFrank Li 	 *
2208793fc096SFrank Li 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
2209793fc096SFrank Li 	 *  - fec0 acts as the 1588 time master while fec1 is slave
2210793fc096SFrank Li 	 *  - external phys can only be configured by fec0
2211793fc096SFrank Li 	 *
2212793fc096SFrank Li 	 * That is to say fec1 can not work independently. It only works
2213793fc096SFrank Li 	 * when fec0 is working. The reason behind this design is that the
2214793fc096SFrank Li 	 * second interface is added primarily for Switch mode.
2215793fc096SFrank Li 	 *
2216793fc096SFrank Li 	 * Because of the last point above, both phys are attached on fec0
2217793fc096SFrank Li 	 * mdio interface in board design, and need to be configured by
2218793fc096SFrank Li 	 * fec0 mii_bus.
2219793fc096SFrank Li 	 */
22203d125f9cSStefan Agner 	if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
2221793fc096SFrank Li 		/* fec1 uses fec0 mii_bus */
2222793fc096SFrank Li 		if (mii_cnt && fec0_mii_bus) {
2223793fc096SFrank Li 			fep->mii_bus = fec0_mii_bus;
2224793fc096SFrank Li 			mii_cnt++;
2225793fc096SFrank Li 			return 0;
2226793fc096SFrank Li 		}
2227793fc096SFrank Li 		return -ENOENT;
2228793fc096SFrank Li 	}
2229793fc096SFrank Li 
22303e782985SAndrew Lunn 	bus_freq = 2500000; /* 2.5MHz by default */
22313e782985SAndrew Lunn 	node = of_get_child_by_name(pdev->dev.of_node, "mdio");
22323c01eb62SAndrew Lunn 	if (node) {
22333e782985SAndrew Lunn 		of_property_read_u32(node, "clock-frequency", &bus_freq);
22343c01eb62SAndrew Lunn 		suppress_preamble = of_property_read_bool(node,
22353c01eb62SAndrew Lunn 							  "suppress-preamble");
22363c01eb62SAndrew Lunn 	}
22373e782985SAndrew Lunn 
2238793fc096SFrank Li 	/*
22393e782985SAndrew Lunn 	 * Set MII speed (= clk_get_rate() / 2 * phy_speed)
2240793fc096SFrank Li 	 *
2241793fc096SFrank Li 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2242793fc096SFrank Li 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
2243793fc096SFrank Li 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2244793fc096SFrank Li 	 * document.
2245793fc096SFrank Li 	 */
22463e782985SAndrew Lunn 	mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2);
22476b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_ENET_MAC)
224863c60732SUwe Kleine-König 		mii_speed--;
224963c60732SUwe Kleine-König 	if (mii_speed > 63) {
225063c60732SUwe Kleine-König 		dev_err(&pdev->dev,
2251981a0547SPeter Meerwald-Stadler 			"fec clock (%lu) too fast to get right mii speed\n",
225263c60732SUwe Kleine-König 			clk_get_rate(fep->clk_ipg));
225363c60732SUwe Kleine-König 		err = -EINVAL;
225463c60732SUwe Kleine-König 		goto err_out;
225563c60732SUwe Kleine-König 	}
225663c60732SUwe Kleine-König 
225763c60732SUwe Kleine-König 	/*
225863c60732SUwe Kleine-König 	 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
225963c60732SUwe Kleine-König 	 * MII_SPEED) register that defines the MDIO output hold time. Earlier
226063c60732SUwe Kleine-König 	 * versions are RAZ there, so just ignore the difference and write the
226163c60732SUwe Kleine-König 	 * register always.
226263c60732SUwe Kleine-König 	 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
226363c60732SUwe Kleine-König 	 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
226463c60732SUwe Kleine-König 	 * output.
226563c60732SUwe Kleine-König 	 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
226663c60732SUwe Kleine-König 	 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
226763c60732SUwe Kleine-König 	 * holdtime cannot result in a value greater than 3.
226863c60732SUwe Kleine-König 	 */
226963c60732SUwe Kleine-König 	holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
227063c60732SUwe Kleine-König 
227163c60732SUwe Kleine-König 	fep->phy_speed = mii_speed << 1 | holdtime << 8;
227263c60732SUwe Kleine-König 
22733c01eb62SAndrew Lunn 	if (suppress_preamble)
22743c01eb62SAndrew Lunn 		fep->phy_speed |= BIT(7);
22753c01eb62SAndrew Lunn 
22761e6114f5SGreg Ungerer 	if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) {
2277f166f890SAndrew Lunn 		/* Clear MMFR to avoid to generate MII event by writing MSCR.
2278f166f890SAndrew Lunn 		 * MII event generation condition:
2279f166f890SAndrew Lunn 		 * - writing MSCR:
2280f166f890SAndrew Lunn 		 *	- mmfr[31:0]_not_zero & mscr[7:0]_is_zero &
2281f166f890SAndrew Lunn 		 *	  mscr_reg_data_in[7:0] != 0
2282f166f890SAndrew Lunn 		 * - writing MMFR:
2283f166f890SAndrew Lunn 		 *	- mscr[7:0]_not_zero
2284f166f890SAndrew Lunn 		 */
2285f166f890SAndrew Lunn 		writel(0, fep->hwp + FEC_MII_DATA);
22861e6114f5SGreg Ungerer 	}
2287f166f890SAndrew Lunn 
2288793fc096SFrank Li 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2289793fc096SFrank Li 
2290f166f890SAndrew Lunn 	/* Clear any pending transaction complete indication */
2291f166f890SAndrew Lunn 	writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
2292f166f890SAndrew Lunn 
2293793fc096SFrank Li 	fep->mii_bus = mdiobus_alloc();
2294793fc096SFrank Li 	if (fep->mii_bus == NULL) {
2295793fc096SFrank Li 		err = -ENOMEM;
2296793fc096SFrank Li 		goto err_out;
2297793fc096SFrank Li 	}
2298793fc096SFrank Li 
2299793fc096SFrank Li 	fep->mii_bus->name = "fec_enet_mii_bus";
2300793fc096SFrank Li 	fep->mii_bus->read = fec_enet_mdio_read;
2301793fc096SFrank Li 	fep->mii_bus->write = fec_enet_mdio_write;
2302793fc096SFrank Li 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2303793fc096SFrank Li 		pdev->name, fep->dev_id + 1);
2304793fc096SFrank Li 	fep->mii_bus->priv = fep;
2305793fc096SFrank Li 	fep->mii_bus->parent = &pdev->dev;
2306793fc096SFrank Li 
2307407066f8SUwe Kleine-König 	err = of_mdiobus_register(fep->mii_bus, node);
2308407066f8SUwe Kleine-König 	if (err)
2309e7f4dc35SAndrew Lunn 		goto err_out_free_mdiobus;
23100607a2cdSPan Bian 	of_node_put(node);
2311793fc096SFrank Li 
2312793fc096SFrank Li 	mii_cnt++;
2313793fc096SFrank Li 
2314793fc096SFrank Li 	/* save fec0 mii_bus */
23153d125f9cSStefan Agner 	if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2316793fc096SFrank Li 		fec0_mii_bus = fep->mii_bus;
2317793fc096SFrank Li 
2318793fc096SFrank Li 	return 0;
2319793fc096SFrank Li 
2320793fc096SFrank Li err_out_free_mdiobus:
2321793fc096SFrank Li 	mdiobus_free(fep->mii_bus);
2322793fc096SFrank Li err_out:
23230607a2cdSPan Bian 	of_node_put(node);
2324793fc096SFrank Li 	return err;
2325793fc096SFrank Li }
2326793fc096SFrank Li 
2327793fc096SFrank Li static void fec_enet_mii_remove(struct fec_enet_private *fep)
2328793fc096SFrank Li {
2329793fc096SFrank Li 	if (--mii_cnt == 0) {
2330793fc096SFrank Li 		mdiobus_unregister(fep->mii_bus);
2331793fc096SFrank Li 		mdiobus_free(fep->mii_bus);
2332793fc096SFrank Li 	}
2333793fc096SFrank Li }
2334793fc096SFrank Li 
2335793fc096SFrank Li static void fec_enet_get_drvinfo(struct net_device *ndev,
2336793fc096SFrank Li 				 struct ethtool_drvinfo *info)
2337793fc096SFrank Li {
2338793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2339793fc096SFrank Li 
2340f029c781SWolfram Sang 	strscpy(info->driver, fep->pdev->dev.driver->name,
2341793fc096SFrank Li 		sizeof(info->driver));
2342f029c781SWolfram Sang 	strscpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2343793fc096SFrank Li }
2344793fc096SFrank Li 
2345db65f35fSPhilippe Reynes static int fec_enet_get_regs_len(struct net_device *ndev)
2346db65f35fSPhilippe Reynes {
2347db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2348db65f35fSPhilippe Reynes 	struct resource *r;
2349db65f35fSPhilippe Reynes 	int s = 0;
2350db65f35fSPhilippe Reynes 
2351db65f35fSPhilippe Reynes 	r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2352db65f35fSPhilippe Reynes 	if (r)
2353db65f35fSPhilippe Reynes 		s = resource_size(r);
2354db65f35fSPhilippe Reynes 
2355db65f35fSPhilippe Reynes 	return s;
2356db65f35fSPhilippe Reynes }
2357db65f35fSPhilippe Reynes 
2358db65f35fSPhilippe Reynes /* List of registers that can be safety be read to dump them with ethtool */
2359db65f35fSPhilippe Reynes #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
23603f1dcc6aSLucas Stach 	defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
236178cc6e7eSFlorian Fainelli 	defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
2362f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 2;
2363db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2364db65f35fSPhilippe Reynes 	FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2365db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2366db65f35fSPhilippe Reynes 	FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2367db65f35fSPhilippe Reynes 	FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2368db65f35fSPhilippe Reynes 	FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2369db65f35fSPhilippe Reynes 	FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2370db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2371db65f35fSPhilippe Reynes 	FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2372db65f35fSPhilippe Reynes 	FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2373db65f35fSPhilippe Reynes 	FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2374db65f35fSPhilippe Reynes 	FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2375db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2376db65f35fSPhilippe Reynes 	RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2377db65f35fSPhilippe Reynes 	RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2378db65f35fSPhilippe Reynes 	RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2379db65f35fSPhilippe Reynes 	RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2380db65f35fSPhilippe Reynes 	RMON_T_P_GTE2048, RMON_T_OCTETS,
2381db65f35fSPhilippe Reynes 	IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2382db65f35fSPhilippe Reynes 	IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2383db65f35fSPhilippe Reynes 	IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2384db65f35fSPhilippe Reynes 	RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2385db65f35fSPhilippe Reynes 	RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2386db65f35fSPhilippe Reynes 	RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2387db65f35fSPhilippe Reynes 	RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2388db65f35fSPhilippe Reynes 	RMON_R_P_GTE2048, RMON_R_OCTETS,
2389db65f35fSPhilippe Reynes 	IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2390db65f35fSPhilippe Reynes 	IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2391db65f35fSPhilippe Reynes };
2392db65f35fSPhilippe Reynes #else
2393f9bcc9f3SVivien Didelot static __u32 fec_enet_register_version = 1;
2394db65f35fSPhilippe Reynes static u32 fec_enet_register_offset[] = {
2395db65f35fSPhilippe Reynes 	FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2396db65f35fSPhilippe Reynes 	FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2397db65f35fSPhilippe Reynes 	FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2398db65f35fSPhilippe Reynes 	FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2399db65f35fSPhilippe Reynes 	FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2400db65f35fSPhilippe Reynes 	FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2401db65f35fSPhilippe Reynes 	FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2402db65f35fSPhilippe Reynes 	FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2403db65f35fSPhilippe Reynes 	FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2404db65f35fSPhilippe Reynes };
2405db65f35fSPhilippe Reynes #endif
2406db65f35fSPhilippe Reynes 
2407db65f35fSPhilippe Reynes static void fec_enet_get_regs(struct net_device *ndev,
2408db65f35fSPhilippe Reynes 			      struct ethtool_regs *regs, void *regbuf)
2409db65f35fSPhilippe Reynes {
2410db65f35fSPhilippe Reynes 	struct fec_enet_private *fep = netdev_priv(ndev);
2411db65f35fSPhilippe Reynes 	u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2412c72a0bc0SAndrew Lunn 	struct device *dev = &fep->pdev->dev;
2413db65f35fSPhilippe Reynes 	u32 *buf = (u32 *)regbuf;
2414db65f35fSPhilippe Reynes 	u32 i, off;
2415c72a0bc0SAndrew Lunn 	int ret;
2416c72a0bc0SAndrew Lunn 
2417da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(dev);
2418c72a0bc0SAndrew Lunn 	if (ret < 0)
2419c72a0bc0SAndrew Lunn 		return;
2420db65f35fSPhilippe Reynes 
2421f9bcc9f3SVivien Didelot 	regs->version = fec_enet_register_version;
2422f9bcc9f3SVivien Didelot 
2423db65f35fSPhilippe Reynes 	memset(buf, 0, regs->len);
2424db65f35fSPhilippe Reynes 
2425db65f35fSPhilippe Reynes 	for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2426ec20a63aSFugang Duan 		off = fec_enet_register_offset[i];
2427ec20a63aSFugang Duan 
2428ec20a63aSFugang Duan 		if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
2429ec20a63aSFugang Duan 		    !(fep->quirks & FEC_QUIRK_HAS_FRREG))
2430ec20a63aSFugang Duan 			continue;
2431ec20a63aSFugang Duan 
2432ec20a63aSFugang Duan 		off >>= 2;
2433db65f35fSPhilippe Reynes 		buf[off] = readl(&theregs[off]);
2434db65f35fSPhilippe Reynes 	}
2435c72a0bc0SAndrew Lunn 
2436c72a0bc0SAndrew Lunn 	pm_runtime_mark_last_busy(dev);
2437c72a0bc0SAndrew Lunn 	pm_runtime_put_autosuspend(dev);
2438db65f35fSPhilippe Reynes }
2439db65f35fSPhilippe Reynes 
2440793fc096SFrank Li static int fec_enet_get_ts_info(struct net_device *ndev,
2441793fc096SFrank Li 				struct ethtool_ts_info *info)
2442793fc096SFrank Li {
2443793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2444793fc096SFrank Li 
2445793fc096SFrank Li 	if (fep->bufdesc_ex) {
2446793fc096SFrank Li 
2447793fc096SFrank Li 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2448793fc096SFrank Li 					SOF_TIMESTAMPING_RX_SOFTWARE |
2449793fc096SFrank Li 					SOF_TIMESTAMPING_SOFTWARE |
2450793fc096SFrank Li 					SOF_TIMESTAMPING_TX_HARDWARE |
2451793fc096SFrank Li 					SOF_TIMESTAMPING_RX_HARDWARE |
2452793fc096SFrank Li 					SOF_TIMESTAMPING_RAW_HARDWARE;
2453793fc096SFrank Li 		if (fep->ptp_clock)
2454793fc096SFrank Li 			info->phc_index = ptp_clock_index(fep->ptp_clock);
2455793fc096SFrank Li 		else
2456793fc096SFrank Li 			info->phc_index = -1;
2457793fc096SFrank Li 
2458793fc096SFrank Li 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2459793fc096SFrank Li 				 (1 << HWTSTAMP_TX_ON);
2460793fc096SFrank Li 
2461793fc096SFrank Li 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2462793fc096SFrank Li 				   (1 << HWTSTAMP_FILTER_ALL);
2463793fc096SFrank Li 		return 0;
2464793fc096SFrank Li 	} else {
2465793fc096SFrank Li 		return ethtool_op_get_ts_info(ndev, info);
2466793fc096SFrank Li 	}
2467793fc096SFrank Li }
2468793fc096SFrank Li 
2469d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
2470d1391930SGuenter Roeck 
2471793fc096SFrank Li static void fec_enet_get_pauseparam(struct net_device *ndev,
2472793fc096SFrank Li 				    struct ethtool_pauseparam *pause)
2473793fc096SFrank Li {
2474793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2475793fc096SFrank Li 
2476793fc096SFrank Li 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2477793fc096SFrank Li 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2478793fc096SFrank Li 	pause->rx_pause = pause->tx_pause;
2479793fc096SFrank Li }
2480793fc096SFrank Li 
2481793fc096SFrank Li static int fec_enet_set_pauseparam(struct net_device *ndev,
2482793fc096SFrank Li 				   struct ethtool_pauseparam *pause)
2483793fc096SFrank Li {
2484793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
2485793fc096SFrank Li 
248645f5c327SPhilippe Reynes 	if (!ndev->phydev)
24870b146ca8SRussell King 		return -ENODEV;
24880b146ca8SRussell King 
2489793fc096SFrank Li 	if (pause->tx_pause != pause->rx_pause) {
2490793fc096SFrank Li 		netdev_info(ndev,
2491793fc096SFrank Li 			"hardware only support enable/disable both tx and rx");
2492793fc096SFrank Li 		return -EINVAL;
2493793fc096SFrank Li 	}
2494793fc096SFrank Li 
2495793fc096SFrank Li 	fep->pause_flag = 0;
2496793fc096SFrank Li 
2497793fc096SFrank Li 	/* tx pause must be same as rx pause */
2498793fc096SFrank Li 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2499793fc096SFrank Li 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2500793fc096SFrank Li 
25010c122405SAndrew Lunn 	phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
25020c122405SAndrew Lunn 			  pause->autoneg);
2503793fc096SFrank Li 
2504793fc096SFrank Li 	if (pause->autoneg) {
2505793fc096SFrank Li 		if (netif_running(ndev))
2506793fc096SFrank Li 			fec_stop(ndev);
250745f5c327SPhilippe Reynes 		phy_start_aneg(ndev->phydev);
2508793fc096SFrank Li 	}
2509dbc64a8eSRussell King 	if (netif_running(ndev)) {
2510dbc64a8eSRussell King 		napi_disable(&fep->napi);
2511dbc64a8eSRussell King 		netif_tx_lock_bh(ndev);
2512ef83337dSRussell King 		fec_restart(ndev);
2513657ade07SRickard x Andersson 		netif_tx_wake_all_queues(ndev);
25146af42d42SRussell King 		netif_tx_unlock_bh(ndev);
2515dbc64a8eSRussell King 		napi_enable(&fep->napi);
2516dbc64a8eSRussell King 	}
2517793fc096SFrank Li 
2518793fc096SFrank Li 	return 0;
2519793fc096SFrank Li }
2520793fc096SFrank Li 
252138ae92dcSChris Healy static const struct fec_stat {
252238ae92dcSChris Healy 	char name[ETH_GSTRING_LEN];
252338ae92dcSChris Healy 	u16 offset;
252438ae92dcSChris Healy } fec_stats[] = {
252538ae92dcSChris Healy 	/* RMON TX */
252638ae92dcSChris Healy 	{ "tx_dropped", RMON_T_DROP },
252738ae92dcSChris Healy 	{ "tx_packets", RMON_T_PACKETS },
252838ae92dcSChris Healy 	{ "tx_broadcast", RMON_T_BC_PKT },
252938ae92dcSChris Healy 	{ "tx_multicast", RMON_T_MC_PKT },
253038ae92dcSChris Healy 	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
253138ae92dcSChris Healy 	{ "tx_undersize", RMON_T_UNDERSIZE },
253238ae92dcSChris Healy 	{ "tx_oversize", RMON_T_OVERSIZE },
253338ae92dcSChris Healy 	{ "tx_fragment", RMON_T_FRAG },
253438ae92dcSChris Healy 	{ "tx_jabber", RMON_T_JAB },
253538ae92dcSChris Healy 	{ "tx_collision", RMON_T_COL },
253638ae92dcSChris Healy 	{ "tx_64byte", RMON_T_P64 },
253738ae92dcSChris Healy 	{ "tx_65to127byte", RMON_T_P65TO127 },
253838ae92dcSChris Healy 	{ "tx_128to255byte", RMON_T_P128TO255 },
253938ae92dcSChris Healy 	{ "tx_256to511byte", RMON_T_P256TO511 },
254038ae92dcSChris Healy 	{ "tx_512to1023byte", RMON_T_P512TO1023 },
254138ae92dcSChris Healy 	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
254238ae92dcSChris Healy 	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
254338ae92dcSChris Healy 	{ "tx_octets", RMON_T_OCTETS },
254438ae92dcSChris Healy 
254538ae92dcSChris Healy 	/* IEEE TX */
254638ae92dcSChris Healy 	{ "IEEE_tx_drop", IEEE_T_DROP },
254738ae92dcSChris Healy 	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
254838ae92dcSChris Healy 	{ "IEEE_tx_1col", IEEE_T_1COL },
254938ae92dcSChris Healy 	{ "IEEE_tx_mcol", IEEE_T_MCOL },
255038ae92dcSChris Healy 	{ "IEEE_tx_def", IEEE_T_DEF },
255138ae92dcSChris Healy 	{ "IEEE_tx_lcol", IEEE_T_LCOL },
255238ae92dcSChris Healy 	{ "IEEE_tx_excol", IEEE_T_EXCOL },
255338ae92dcSChris Healy 	{ "IEEE_tx_macerr", IEEE_T_MACERR },
255438ae92dcSChris Healy 	{ "IEEE_tx_cserr", IEEE_T_CSERR },
255538ae92dcSChris Healy 	{ "IEEE_tx_sqe", IEEE_T_SQE },
255638ae92dcSChris Healy 	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
255738ae92dcSChris Healy 	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
255838ae92dcSChris Healy 
255938ae92dcSChris Healy 	/* RMON RX */
256038ae92dcSChris Healy 	{ "rx_packets", RMON_R_PACKETS },
256138ae92dcSChris Healy 	{ "rx_broadcast", RMON_R_BC_PKT },
256238ae92dcSChris Healy 	{ "rx_multicast", RMON_R_MC_PKT },
256338ae92dcSChris Healy 	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
256438ae92dcSChris Healy 	{ "rx_undersize", RMON_R_UNDERSIZE },
256538ae92dcSChris Healy 	{ "rx_oversize", RMON_R_OVERSIZE },
256638ae92dcSChris Healy 	{ "rx_fragment", RMON_R_FRAG },
256738ae92dcSChris Healy 	{ "rx_jabber", RMON_R_JAB },
256838ae92dcSChris Healy 	{ "rx_64byte", RMON_R_P64 },
256938ae92dcSChris Healy 	{ "rx_65to127byte", RMON_R_P65TO127 },
257038ae92dcSChris Healy 	{ "rx_128to255byte", RMON_R_P128TO255 },
257138ae92dcSChris Healy 	{ "rx_256to511byte", RMON_R_P256TO511 },
257238ae92dcSChris Healy 	{ "rx_512to1023byte", RMON_R_P512TO1023 },
257338ae92dcSChris Healy 	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
257438ae92dcSChris Healy 	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
257538ae92dcSChris Healy 	{ "rx_octets", RMON_R_OCTETS },
257638ae92dcSChris Healy 
257738ae92dcSChris Healy 	/* IEEE RX */
257838ae92dcSChris Healy 	{ "IEEE_rx_drop", IEEE_R_DROP },
257938ae92dcSChris Healy 	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
258038ae92dcSChris Healy 	{ "IEEE_rx_crc", IEEE_R_CRC },
258138ae92dcSChris Healy 	{ "IEEE_rx_align", IEEE_R_ALIGN },
258238ae92dcSChris Healy 	{ "IEEE_rx_macerr", IEEE_R_MACERR },
258338ae92dcSChris Healy 	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
258438ae92dcSChris Healy 	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
258538ae92dcSChris Healy };
258638ae92dcSChris Healy 
2587f85de666SNikita Yushchenko #define FEC_STATS_SIZE		(ARRAY_SIZE(fec_stats) * sizeof(u64))
2588f85de666SNikita Yushchenko 
258980cca775SNikita Yushchenko static void fec_enet_update_ethtool_stats(struct net_device *dev)
259038ae92dcSChris Healy {
259138ae92dcSChris Healy 	struct fec_enet_private *fep = netdev_priv(dev);
259238ae92dcSChris Healy 	int i;
259338ae92dcSChris Healy 
259438ae92dcSChris Healy 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
259580cca775SNikita Yushchenko 		fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
259680cca775SNikita Yushchenko }
259780cca775SNikita Yushchenko 
259880cca775SNikita Yushchenko static void fec_enet_get_ethtool_stats(struct net_device *dev,
259980cca775SNikita Yushchenko 				       struct ethtool_stats *stats, u64 *data)
260080cca775SNikita Yushchenko {
260180cca775SNikita Yushchenko 	struct fec_enet_private *fep = netdev_priv(dev);
260280cca775SNikita Yushchenko 
260380cca775SNikita Yushchenko 	if (netif_running(dev))
260480cca775SNikita Yushchenko 		fec_enet_update_ethtool_stats(dev);
260580cca775SNikita Yushchenko 
2606f85de666SNikita Yushchenko 	memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
260738ae92dcSChris Healy }
260838ae92dcSChris Healy 
260938ae92dcSChris Healy static void fec_enet_get_strings(struct net_device *netdev,
261038ae92dcSChris Healy 	u32 stringset, u8 *data)
261138ae92dcSChris Healy {
261238ae92dcSChris Healy 	int i;
261338ae92dcSChris Healy 	switch (stringset) {
261438ae92dcSChris Healy 	case ETH_SS_STATS:
261538ae92dcSChris Healy 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
261638ae92dcSChris Healy 			memcpy(data + i * ETH_GSTRING_LEN,
261738ae92dcSChris Healy 				fec_stats[i].name, ETH_GSTRING_LEN);
261838ae92dcSChris Healy 		break;
26196016ba34SOleksij Rempel 	case ETH_SS_TEST:
26206016ba34SOleksij Rempel 		net_selftest_get_strings(data);
26216016ba34SOleksij Rempel 		break;
262238ae92dcSChris Healy 	}
262338ae92dcSChris Healy }
262438ae92dcSChris Healy 
262538ae92dcSChris Healy static int fec_enet_get_sset_count(struct net_device *dev, int sset)
262638ae92dcSChris Healy {
262738ae92dcSChris Healy 	switch (sset) {
262838ae92dcSChris Healy 	case ETH_SS_STATS:
262938ae92dcSChris Healy 		return ARRAY_SIZE(fec_stats);
26306016ba34SOleksij Rempel 	case ETH_SS_TEST:
26316016ba34SOleksij Rempel 		return net_selftest_get_count();
263238ae92dcSChris Healy 	default:
263338ae92dcSChris Healy 		return -EOPNOTSUPP;
263438ae92dcSChris Healy 	}
263538ae92dcSChris Healy }
2636f85de666SNikita Yushchenko 
26372b30842bSAndrew Lunn static void fec_enet_clear_ethtool_stats(struct net_device *dev)
26382b30842bSAndrew Lunn {
26392b30842bSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(dev);
26402b30842bSAndrew Lunn 	int i;
26412b30842bSAndrew Lunn 
26422b30842bSAndrew Lunn 	/* Disable MIB statistics counters */
26432b30842bSAndrew Lunn 	writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
26442b30842bSAndrew Lunn 
26452b30842bSAndrew Lunn 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
26462b30842bSAndrew Lunn 		writel(0, fep->hwp + fec_stats[i].offset);
26472b30842bSAndrew Lunn 
26482b30842bSAndrew Lunn 	/* Don't disable MIB statistics counters */
26492b30842bSAndrew Lunn 	writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
26502b30842bSAndrew Lunn }
26512b30842bSAndrew Lunn 
2652f85de666SNikita Yushchenko #else	/* !defined(CONFIG_M5272) */
2653f85de666SNikita Yushchenko #define FEC_STATS_SIZE	0
2654f85de666SNikita Yushchenko static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2655f85de666SNikita Yushchenko {
2656f85de666SNikita Yushchenko }
265741e8e404SFabio Estevam 
265841e8e404SFabio Estevam static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
265941e8e404SFabio Estevam {
266041e8e404SFabio Estevam }
2661d1391930SGuenter Roeck #endif /* !defined(CONFIG_M5272) */
266238ae92dcSChris Healy 
2663d851b47bSFugang Duan /* ITR clock source is enet system clock (clk_ahb).
2664d851b47bSFugang Duan  * TCTT unit is cycle_ns * 64 cycle
2665d851b47bSFugang Duan  * So, the ICTT value = X us / (cycle_ns * 64)
2666d851b47bSFugang Duan  */
2667d851b47bSFugang Duan static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2668d851b47bSFugang Duan {
2669d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2670d851b47bSFugang Duan 
2671d851b47bSFugang Duan 	return us * (fep->itr_clk_rate / 64000) / 1000;
2672d851b47bSFugang Duan }
2673d851b47bSFugang Duan 
2674d851b47bSFugang Duan /* Set threshold for interrupt coalescing */
2675d851b47bSFugang Duan static void fec_enet_itr_coal_set(struct net_device *ndev)
2676d851b47bSFugang Duan {
2677d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2678d851b47bSFugang Duan 	int rx_itr, tx_itr;
2679d851b47bSFugang Duan 
2680d851b47bSFugang Duan 	/* Must be greater than zero to avoid unpredictable behavior */
2681d851b47bSFugang Duan 	if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2682d851b47bSFugang Duan 	    !fep->tx_time_itr || !fep->tx_pkts_itr)
2683d851b47bSFugang Duan 		return;
2684d851b47bSFugang Duan 
2685d851b47bSFugang Duan 	/* Select enet system clock as Interrupt Coalescing
2686d851b47bSFugang Duan 	 * timer Clock Source
2687d851b47bSFugang Duan 	 */
2688d851b47bSFugang Duan 	rx_itr = FEC_ITR_CLK_SEL;
2689d851b47bSFugang Duan 	tx_itr = FEC_ITR_CLK_SEL;
2690d851b47bSFugang Duan 
2691d851b47bSFugang Duan 	/* set ICFT and ICTT */
2692d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2693d851b47bSFugang Duan 	rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2694d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2695d851b47bSFugang Duan 	tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2696d851b47bSFugang Duan 
2697d851b47bSFugang Duan 	rx_itr |= FEC_ITR_EN;
2698d851b47bSFugang Duan 	tx_itr |= FEC_ITR_EN;
2699d851b47bSFugang Duan 
2700d851b47bSFugang Duan 	writel(tx_itr, fep->hwp + FEC_TXIC0);
2701d851b47bSFugang Duan 	writel(rx_itr, fep->hwp + FEC_RXIC0);
2702471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
2703d851b47bSFugang Duan 		writel(tx_itr, fep->hwp + FEC_TXIC1);
2704d851b47bSFugang Duan 		writel(rx_itr, fep->hwp + FEC_RXIC1);
2705d851b47bSFugang Duan 		writel(tx_itr, fep->hwp + FEC_TXIC2);
2706d851b47bSFugang Duan 		writel(rx_itr, fep->hwp + FEC_RXIC2);
2707d851b47bSFugang Duan 	}
2708ff7566b8SFugang Duan }
2709d851b47bSFugang Duan 
2710f3ccfda1SYufeng Mo static int fec_enet_get_coalesce(struct net_device *ndev,
2711f3ccfda1SYufeng Mo 				 struct ethtool_coalesce *ec,
2712f3ccfda1SYufeng Mo 				 struct kernel_ethtool_coalesce *kernel_coal,
2713f3ccfda1SYufeng Mo 				 struct netlink_ext_ack *extack)
2714d851b47bSFugang Duan {
2715d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2716d851b47bSFugang Duan 
2717ff7566b8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2718d851b47bSFugang Duan 		return -EOPNOTSUPP;
2719d851b47bSFugang Duan 
2720d851b47bSFugang Duan 	ec->rx_coalesce_usecs = fep->rx_time_itr;
2721d851b47bSFugang Duan 	ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2722d851b47bSFugang Duan 
2723d851b47bSFugang Duan 	ec->tx_coalesce_usecs = fep->tx_time_itr;
2724d851b47bSFugang Duan 	ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2725d851b47bSFugang Duan 
2726d851b47bSFugang Duan 	return 0;
2727d851b47bSFugang Duan }
2728d851b47bSFugang Duan 
2729f3ccfda1SYufeng Mo static int fec_enet_set_coalesce(struct net_device *ndev,
2730f3ccfda1SYufeng Mo 				 struct ethtool_coalesce *ec,
2731f3ccfda1SYufeng Mo 				 struct kernel_ethtool_coalesce *kernel_coal,
2732f3ccfda1SYufeng Mo 				 struct netlink_ext_ack *extack)
2733d851b47bSFugang Duan {
2734d851b47bSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2735517a772cSFabio Estevam 	struct device *dev = &fep->pdev->dev;
2736d851b47bSFugang Duan 	unsigned int cycle;
2737d851b47bSFugang Duan 
2738ff7566b8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2739d851b47bSFugang Duan 		return -EOPNOTSUPP;
2740d851b47bSFugang Duan 
2741d851b47bSFugang Duan 	if (ec->rx_max_coalesced_frames > 255) {
2742517a772cSFabio Estevam 		dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
2743d851b47bSFugang Duan 		return -EINVAL;
2744d851b47bSFugang Duan 	}
2745d851b47bSFugang Duan 
2746d851b47bSFugang Duan 	if (ec->tx_max_coalesced_frames > 255) {
2747517a772cSFabio Estevam 		dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
2748d851b47bSFugang Duan 		return -EINVAL;
2749d851b47bSFugang Duan 	}
2750d851b47bSFugang Duan 
2751ab14961dSJakub Kicinski 	cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
2752d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
2753517a772cSFabio Estevam 		dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
2754d851b47bSFugang Duan 		return -EINVAL;
2755d851b47bSFugang Duan 	}
2756d851b47bSFugang Duan 
2757ab14961dSJakub Kicinski 	cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
2758d851b47bSFugang Duan 	if (cycle > 0xFFFF) {
2759ab14961dSJakub Kicinski 		dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
2760d851b47bSFugang Duan 		return -EINVAL;
2761d851b47bSFugang Duan 	}
2762d851b47bSFugang Duan 
2763d851b47bSFugang Duan 	fep->rx_time_itr = ec->rx_coalesce_usecs;
2764d851b47bSFugang Duan 	fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2765d851b47bSFugang Duan 
2766d851b47bSFugang Duan 	fep->tx_time_itr = ec->tx_coalesce_usecs;
2767d851b47bSFugang Duan 	fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2768d851b47bSFugang Duan 
2769d851b47bSFugang Duan 	fec_enet_itr_coal_set(ndev);
2770d851b47bSFugang Duan 
2771d851b47bSFugang Duan 	return 0;
2772d851b47bSFugang Duan }
2773d851b47bSFugang Duan 
2774d851b47bSFugang Duan static void fec_enet_itr_coal_init(struct net_device *ndev)
2775d851b47bSFugang Duan {
2776d851b47bSFugang Duan 	struct ethtool_coalesce ec;
2777d851b47bSFugang Duan 
2778d851b47bSFugang Duan 	ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2779d851b47bSFugang Duan 	ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2780d851b47bSFugang Duan 
2781d851b47bSFugang Duan 	ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2782d851b47bSFugang Duan 	ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2783d851b47bSFugang Duan 
2784f3ccfda1SYufeng Mo 	fec_enet_set_coalesce(ndev, &ec, NULL, NULL);
2785d851b47bSFugang Duan }
2786d851b47bSFugang Duan 
27871b7bde6dSNimrod Andy static int fec_enet_get_tunable(struct net_device *netdev,
27881b7bde6dSNimrod Andy 				const struct ethtool_tunable *tuna,
27891b7bde6dSNimrod Andy 				void *data)
27901b7bde6dSNimrod Andy {
27911b7bde6dSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
27921b7bde6dSNimrod Andy 	int ret = 0;
27931b7bde6dSNimrod Andy 
27941b7bde6dSNimrod Andy 	switch (tuna->id) {
27951b7bde6dSNimrod Andy 	case ETHTOOL_RX_COPYBREAK:
27961b7bde6dSNimrod Andy 		*(u32 *)data = fep->rx_copybreak;
27971b7bde6dSNimrod Andy 		break;
27981b7bde6dSNimrod Andy 	default:
27991b7bde6dSNimrod Andy 		ret = -EINVAL;
28001b7bde6dSNimrod Andy 		break;
28011b7bde6dSNimrod Andy 	}
28021b7bde6dSNimrod Andy 
28031b7bde6dSNimrod Andy 	return ret;
28041b7bde6dSNimrod Andy }
28051b7bde6dSNimrod Andy 
28061b7bde6dSNimrod Andy static int fec_enet_set_tunable(struct net_device *netdev,
28071b7bde6dSNimrod Andy 				const struct ethtool_tunable *tuna,
28081b7bde6dSNimrod Andy 				const void *data)
28091b7bde6dSNimrod Andy {
28101b7bde6dSNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
28111b7bde6dSNimrod Andy 	int ret = 0;
28121b7bde6dSNimrod Andy 
28131b7bde6dSNimrod Andy 	switch (tuna->id) {
28141b7bde6dSNimrod Andy 	case ETHTOOL_RX_COPYBREAK:
28151b7bde6dSNimrod Andy 		fep->rx_copybreak = *(u32 *)data;
28161b7bde6dSNimrod Andy 		break;
28171b7bde6dSNimrod Andy 	default:
28181b7bde6dSNimrod Andy 		ret = -EINVAL;
28191b7bde6dSNimrod Andy 		break;
28201b7bde6dSNimrod Andy 	}
28211b7bde6dSNimrod Andy 
28221b7bde6dSNimrod Andy 	return ret;
28231b7bde6dSNimrod Andy }
28241b7bde6dSNimrod Andy 
2825b82f8c3fSFugang Duan /* LPI Sleep Ts count base on tx clk (clk_ref).
2826b82f8c3fSFugang Duan  * The lpi sleep cnt value = X us / (cycle_ns).
2827b82f8c3fSFugang Duan  */
2828b82f8c3fSFugang Duan static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
2829b82f8c3fSFugang Duan {
2830b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2831b82f8c3fSFugang Duan 
2832b82f8c3fSFugang Duan 	return us * (fep->clk_ref_rate / 1000) / 1000;
2833b82f8c3fSFugang Duan }
2834b82f8c3fSFugang Duan 
2835b82f8c3fSFugang Duan static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable)
2836b82f8c3fSFugang Duan {
2837b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2838b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
2839b82f8c3fSFugang Duan 	unsigned int sleep_cycle, wake_cycle;
2840b82f8c3fSFugang Duan 	int ret = 0;
2841b82f8c3fSFugang Duan 
2842b82f8c3fSFugang Duan 	if (enable) {
284353243d41SJisheng Zhang 		ret = phy_init_eee(ndev->phydev, false);
2844b82f8c3fSFugang Duan 		if (ret)
2845b82f8c3fSFugang Duan 			return ret;
2846b82f8c3fSFugang Duan 
2847b82f8c3fSFugang Duan 		sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer);
2848b82f8c3fSFugang Duan 		wake_cycle = sleep_cycle;
2849b82f8c3fSFugang Duan 	} else {
2850b82f8c3fSFugang Duan 		sleep_cycle = 0;
2851b82f8c3fSFugang Duan 		wake_cycle = 0;
2852b82f8c3fSFugang Duan 	}
2853b82f8c3fSFugang Duan 
2854b82f8c3fSFugang Duan 	p->tx_lpi_enabled = enable;
2855b82f8c3fSFugang Duan 	p->eee_enabled = enable;
2856b82f8c3fSFugang Duan 	p->eee_active = enable;
2857b82f8c3fSFugang Duan 
2858b82f8c3fSFugang Duan 	writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
2859b82f8c3fSFugang Duan 	writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
2860b82f8c3fSFugang Duan 
2861b82f8c3fSFugang Duan 	return 0;
2862b82f8c3fSFugang Duan }
2863b82f8c3fSFugang Duan 
2864b82f8c3fSFugang Duan static int
2865b82f8c3fSFugang Duan fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2866b82f8c3fSFugang Duan {
2867b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2868b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
2869b82f8c3fSFugang Duan 
2870b82f8c3fSFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
2871b82f8c3fSFugang Duan 		return -EOPNOTSUPP;
2872b82f8c3fSFugang Duan 
2873b82f8c3fSFugang Duan 	if (!netif_running(ndev))
2874b82f8c3fSFugang Duan 		return -ENETDOWN;
2875b82f8c3fSFugang Duan 
2876b82f8c3fSFugang Duan 	edata->eee_enabled = p->eee_enabled;
2877b82f8c3fSFugang Duan 	edata->eee_active = p->eee_active;
2878b82f8c3fSFugang Duan 	edata->tx_lpi_timer = p->tx_lpi_timer;
2879b82f8c3fSFugang Duan 	edata->tx_lpi_enabled = p->tx_lpi_enabled;
2880b82f8c3fSFugang Duan 
2881b82f8c3fSFugang Duan 	return phy_ethtool_get_eee(ndev->phydev, edata);
2882b82f8c3fSFugang Duan }
2883b82f8c3fSFugang Duan 
2884b82f8c3fSFugang Duan static int
2885b82f8c3fSFugang Duan fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2886b82f8c3fSFugang Duan {
2887b82f8c3fSFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
2888b82f8c3fSFugang Duan 	struct ethtool_eee *p = &fep->eee;
2889b82f8c3fSFugang Duan 	int ret = 0;
2890b82f8c3fSFugang Duan 
2891b82f8c3fSFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
2892b82f8c3fSFugang Duan 		return -EOPNOTSUPP;
2893b82f8c3fSFugang Duan 
2894b82f8c3fSFugang Duan 	if (!netif_running(ndev))
2895b82f8c3fSFugang Duan 		return -ENETDOWN;
2896b82f8c3fSFugang Duan 
2897b82f8c3fSFugang Duan 	p->tx_lpi_timer = edata->tx_lpi_timer;
2898b82f8c3fSFugang Duan 
2899b82f8c3fSFugang Duan 	if (!edata->eee_enabled || !edata->tx_lpi_enabled ||
2900b82f8c3fSFugang Duan 	    !edata->tx_lpi_timer)
2901b82f8c3fSFugang Duan 		ret = fec_enet_eee_mode_set(ndev, false);
2902b82f8c3fSFugang Duan 	else
2903b82f8c3fSFugang Duan 		ret = fec_enet_eee_mode_set(ndev, true);
2904b82f8c3fSFugang Duan 
2905b82f8c3fSFugang Duan 	if (ret)
2906b82f8c3fSFugang Duan 		return ret;
2907b82f8c3fSFugang Duan 
2908b82f8c3fSFugang Duan 	return phy_ethtool_set_eee(ndev->phydev, edata);
2909b82f8c3fSFugang Duan }
2910b82f8c3fSFugang Duan 
2911de40ed31SNimrod Andy static void
2912de40ed31SNimrod Andy fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2913de40ed31SNimrod Andy {
2914de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2915de40ed31SNimrod Andy 
2916de40ed31SNimrod Andy 	if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2917de40ed31SNimrod Andy 		wol->supported = WAKE_MAGIC;
2918de40ed31SNimrod Andy 		wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2919de40ed31SNimrod Andy 	} else {
2920de40ed31SNimrod Andy 		wol->supported = wol->wolopts = 0;
2921de40ed31SNimrod Andy 	}
2922de40ed31SNimrod Andy }
2923de40ed31SNimrod Andy 
2924de40ed31SNimrod Andy static int
2925de40ed31SNimrod Andy fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2926de40ed31SNimrod Andy {
2927de40ed31SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(ndev);
2928de40ed31SNimrod Andy 
2929de40ed31SNimrod Andy 	if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2930de40ed31SNimrod Andy 		return -EINVAL;
2931de40ed31SNimrod Andy 
2932de40ed31SNimrod Andy 	if (wol->wolopts & ~WAKE_MAGIC)
2933de40ed31SNimrod Andy 		return -EINVAL;
2934de40ed31SNimrod Andy 
2935de40ed31SNimrod Andy 	device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
29360b6f65c7SJoakim Zhang 	if (device_may_wakeup(&ndev->dev))
2937de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
29380b6f65c7SJoakim Zhang 	else
2939de40ed31SNimrod Andy 		fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2940de40ed31SNimrod Andy 
2941de40ed31SNimrod Andy 	return 0;
2942de40ed31SNimrod Andy }
2943de40ed31SNimrod Andy 
2944793fc096SFrank Li static const struct ethtool_ops fec_enet_ethtool_ops = {
2945d5e3c87dSJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2946d5e3c87dSJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES,
2947793fc096SFrank Li 	.get_drvinfo		= fec_enet_get_drvinfo,
2948db65f35fSPhilippe Reynes 	.get_regs_len		= fec_enet_get_regs_len,
2949db65f35fSPhilippe Reynes 	.get_regs		= fec_enet_get_regs,
295011d59289SFlorian Fainelli 	.nway_reset		= phy_ethtool_nway_reset,
2951c1d7c48fSRussell King 	.get_link		= ethtool_op_get_link,
2952d851b47bSFugang Duan 	.get_coalesce		= fec_enet_get_coalesce,
2953d851b47bSFugang Duan 	.set_coalesce		= fec_enet_set_coalesce,
295438ae92dcSChris Healy #ifndef CONFIG_M5272
2955c1d7c48fSRussell King 	.get_pauseparam		= fec_enet_get_pauseparam,
2956c1d7c48fSRussell King 	.set_pauseparam		= fec_enet_set_pauseparam,
295738ae92dcSChris Healy 	.get_strings		= fec_enet_get_strings,
2958c1d7c48fSRussell King 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
295938ae92dcSChris Healy 	.get_sset_count		= fec_enet_get_sset_count,
296038ae92dcSChris Healy #endif
2961c1d7c48fSRussell King 	.get_ts_info		= fec_enet_get_ts_info,
29621b7bde6dSNimrod Andy 	.get_tunable		= fec_enet_get_tunable,
29631b7bde6dSNimrod Andy 	.set_tunable		= fec_enet_set_tunable,
2964de40ed31SNimrod Andy 	.get_wol		= fec_enet_get_wol,
2965de40ed31SNimrod Andy 	.set_wol		= fec_enet_set_wol,
2966b82f8c3fSFugang Duan 	.get_eee		= fec_enet_get_eee,
2967b82f8c3fSFugang Duan 	.set_eee		= fec_enet_set_eee,
29689365fbf5SPhilippe Reynes 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
29699365fbf5SPhilippe Reynes 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
29706016ba34SOleksij Rempel 	.self_test		= net_selftest,
2971793fc096SFrank Li };
2972793fc096SFrank Li 
2973793fc096SFrank Li static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2974793fc096SFrank Li {
2975793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
297645f5c327SPhilippe Reynes 	struct phy_device *phydev = ndev->phydev;
2977793fc096SFrank Li 
2978793fc096SFrank Li 	if (!netif_running(ndev))
2979793fc096SFrank Li 		return -EINVAL;
2980793fc096SFrank Li 
2981793fc096SFrank Li 	if (!phydev)
2982793fc096SFrank Li 		return -ENODEV;
2983793fc096SFrank Li 
29841d5244d0SBen Hutchings 	if (fep->bufdesc_ex) {
298534074639SSergey Organov 		bool use_fec_hwts = !phy_has_hwtstamp(phydev);
298634074639SSergey Organov 
298734074639SSergey Organov 		if (cmd == SIOCSHWTSTAMP) {
298834074639SSergey Organov 			if (use_fec_hwts)
29891d5244d0SBen Hutchings 				return fec_ptp_set(ndev, rq);
299034074639SSergey Organov 			fec_ptp_disable_hwts(ndev);
299134074639SSergey Organov 		} else if (cmd == SIOCGHWTSTAMP) {
299234074639SSergey Organov 			if (use_fec_hwts)
29931d5244d0SBen Hutchings 				return fec_ptp_get(ndev, rq);
29941d5244d0SBen Hutchings 		}
299534074639SSergey Organov 	}
2996793fc096SFrank Li 
2997793fc096SFrank Li 	return phy_mii_ioctl(phydev, rq, cmd);
2998793fc096SFrank Li }
2999793fc096SFrank Li 
3000793fc096SFrank Li static void fec_enet_free_buffers(struct net_device *ndev)
3001793fc096SFrank Li {
3002793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3003793fc096SFrank Li 	unsigned int i;
3004793fc096SFrank Li 	struct sk_buff *skb;
3005793fc096SFrank Li 	struct bufdesc	*bdp;
30064d494cdcSFugang Duan 	struct fec_enet_priv_tx_q *txq;
30074d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
300859d0f746SFrank Li 	unsigned int q;
3009793fc096SFrank Li 
301059d0f746SFrank Li 	for (q = 0; q < fep->num_rx_queues; q++) {
301159d0f746SFrank Li 		rxq = fep->rx_queue[q];
30127355f276STroy Kisky 		bdp = rxq->bd.base;
30137355f276STroy Kisky 		for (i = 0; i < rxq->bd.ring_size; i++) {
30144d494cdcSFugang Duan 			skb = rxq->rx_skbuff[i];
30154d494cdcSFugang Duan 			rxq->rx_skbuff[i] = NULL;
3016730ee360SRussell King 			if (skb) {
301759d0f746SFrank Li 				dma_unmap_single(&fep->pdev->dev,
30185cfa3039SJohannes Berg 						 fec32_to_cpu(bdp->cbd_bufaddr),
3019b64bf4b7SNimrod Andy 						 FEC_ENET_RX_FRSIZE - fep->rx_align,
302059d0f746SFrank Li 						 DMA_FROM_DEVICE);
3021793fc096SFrank Li 				dev_kfree_skb(skb);
3022730ee360SRussell King 			}
30237355f276STroy Kisky 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
302459d0f746SFrank Li 		}
3025793fc096SFrank Li 	}
3026793fc096SFrank Li 
302759d0f746SFrank Li 	for (q = 0; q < fep->num_tx_queues; q++) {
302859d0f746SFrank Li 		txq = fep->tx_queue[q];
30297355f276STroy Kisky 		for (i = 0; i < txq->bd.ring_size; i++) {
30304d494cdcSFugang Duan 			kfree(txq->tx_bounce[i]);
30314d494cdcSFugang Duan 			txq->tx_bounce[i] = NULL;
30324d494cdcSFugang Duan 			skb = txq->tx_skbuff[i];
30334d494cdcSFugang Duan 			txq->tx_skbuff[i] = NULL;
30348b7c9efaSRussell King 			dev_kfree_skb(skb);
30358b7c9efaSRussell King 		}
3036793fc096SFrank Li 	}
303759d0f746SFrank Li }
3038793fc096SFrank Li 
303959d0f746SFrank Li static void fec_enet_free_queue(struct net_device *ndev)
304059d0f746SFrank Li {
304159d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
304259d0f746SFrank Li 	int i;
304359d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
304459d0f746SFrank Li 
304559d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
304659d0f746SFrank Li 		if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
304759d0f746SFrank Li 			txq = fep->tx_queue[i];
304894920128SFugang Duan 			dma_free_coherent(&fep->pdev->dev,
30497355f276STroy Kisky 					  txq->bd.ring_size * TSO_HEADER_SIZE,
305059d0f746SFrank Li 					  txq->tso_hdrs,
305159d0f746SFrank Li 					  txq->tso_hdrs_dma);
305259d0f746SFrank Li 		}
305359d0f746SFrank Li 
305459d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
305559d0f746SFrank Li 		kfree(fep->rx_queue[i]);
305659d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
305759d0f746SFrank Li 		kfree(fep->tx_queue[i]);
305859d0f746SFrank Li }
305959d0f746SFrank Li 
306059d0f746SFrank Li static int fec_enet_alloc_queue(struct net_device *ndev)
306159d0f746SFrank Li {
306259d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
306359d0f746SFrank Li 	int i;
306459d0f746SFrank Li 	int ret = 0;
306559d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
306659d0f746SFrank Li 
306759d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
306859d0f746SFrank Li 		txq = kzalloc(sizeof(*txq), GFP_KERNEL);
306959d0f746SFrank Li 		if (!txq) {
307059d0f746SFrank Li 			ret = -ENOMEM;
307159d0f746SFrank Li 			goto alloc_failed;
307259d0f746SFrank Li 		}
307359d0f746SFrank Li 
307459d0f746SFrank Li 		fep->tx_queue[i] = txq;
30757355f276STroy Kisky 		txq->bd.ring_size = TX_RING_SIZE;
30767355f276STroy Kisky 		fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
307759d0f746SFrank Li 
307859d0f746SFrank Li 		txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
307959d0f746SFrank Li 		txq->tx_wake_threshold =
30807355f276STroy Kisky 			(txq->bd.ring_size - txq->tx_stop_threshold) / 2;
308159d0f746SFrank Li 
308294920128SFugang Duan 		txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
30837355f276STroy Kisky 					txq->bd.ring_size * TSO_HEADER_SIZE,
308459d0f746SFrank Li 					&txq->tso_hdrs_dma,
308559d0f746SFrank Li 					GFP_KERNEL);
308659d0f746SFrank Li 		if (!txq->tso_hdrs) {
308759d0f746SFrank Li 			ret = -ENOMEM;
308859d0f746SFrank Li 			goto alloc_failed;
308959d0f746SFrank Li 		}
309059d0f746SFrank Li 	}
309159d0f746SFrank Li 
309259d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
309359d0f746SFrank Li 		fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
309459d0f746SFrank Li 					   GFP_KERNEL);
309559d0f746SFrank Li 		if (!fep->rx_queue[i]) {
309659d0f746SFrank Li 			ret = -ENOMEM;
309759d0f746SFrank Li 			goto alloc_failed;
309859d0f746SFrank Li 		}
309959d0f746SFrank Li 
31007355f276STroy Kisky 		fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
31017355f276STroy Kisky 		fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
310259d0f746SFrank Li 	}
310359d0f746SFrank Li 	return ret;
310459d0f746SFrank Li 
310559d0f746SFrank Li alloc_failed:
310659d0f746SFrank Li 	fec_enet_free_queue(ndev);
310759d0f746SFrank Li 	return ret;
310859d0f746SFrank Li }
310959d0f746SFrank Li 
311059d0f746SFrank Li static int
311159d0f746SFrank Li fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
3112793fc096SFrank Li {
3113793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3114793fc096SFrank Li 	unsigned int i;
3115793fc096SFrank Li 	struct sk_buff *skb;
3116793fc096SFrank Li 	struct bufdesc	*bdp;
31174d494cdcSFugang Duan 	struct fec_enet_priv_rx_q *rxq;
3118793fc096SFrank Li 
311959d0f746SFrank Li 	rxq = fep->rx_queue[queue];
31207355f276STroy Kisky 	bdp = rxq->bd.base;
31217355f276STroy Kisky 	for (i = 0; i < rxq->bd.ring_size; i++) {
3122b885aab3SMichael Trimarchi 		skb = __netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE, GFP_KERNEL);
3123ffdce2ccSRussell King 		if (!skb)
3124ffdce2ccSRussell King 			goto err_alloc;
3125793fc096SFrank Li 
31261b7bde6dSNimrod Andy 		if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
3127730ee360SRussell King 			dev_kfree_skb(skb);
3128ffdce2ccSRussell King 			goto err_alloc;
3129d842a31fSDuan Fugang-B38611 		}
3130730ee360SRussell King 
31314d494cdcSFugang Duan 		rxq->rx_skbuff[i] = skb;
31325cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
3133793fc096SFrank Li 
3134793fc096SFrank Li 		if (fep->bufdesc_ex) {
3135793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
31365cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
3137793fc096SFrank Li 		}
3138793fc096SFrank Li 
31397355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
3140793fc096SFrank Li 	}
3141793fc096SFrank Li 
3142793fc096SFrank Li 	/* Set the last buffer to wrap. */
31437355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
31445cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
314559d0f746SFrank Li 	return 0;
3146793fc096SFrank Li 
314759d0f746SFrank Li  err_alloc:
314859d0f746SFrank Li 	fec_enet_free_buffers(ndev);
314959d0f746SFrank Li 	return -ENOMEM;
315059d0f746SFrank Li }
315159d0f746SFrank Li 
315259d0f746SFrank Li static int
315359d0f746SFrank Li fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
315459d0f746SFrank Li {
315559d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
315659d0f746SFrank Li 	unsigned int i;
315759d0f746SFrank Li 	struct bufdesc  *bdp;
315859d0f746SFrank Li 	struct fec_enet_priv_tx_q *txq;
315959d0f746SFrank Li 
316059d0f746SFrank Li 	txq = fep->tx_queue[queue];
31617355f276STroy Kisky 	bdp = txq->bd.base;
31627355f276STroy Kisky 	for (i = 0; i < txq->bd.ring_size; i++) {
31634d494cdcSFugang Duan 		txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
31644d494cdcSFugang Duan 		if (!txq->tx_bounce[i])
3165ffdce2ccSRussell King 			goto err_alloc;
3166793fc096SFrank Li 
31675cfa3039SJohannes Berg 		bdp->cbd_sc = cpu_to_fec16(0);
31685cfa3039SJohannes Berg 		bdp->cbd_bufaddr = cpu_to_fec32(0);
3169793fc096SFrank Li 
3170793fc096SFrank Li 		if (fep->bufdesc_ex) {
3171793fc096SFrank Li 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
31725cfa3039SJohannes Berg 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
3173793fc096SFrank Li 		}
3174793fc096SFrank Li 
31757355f276STroy Kisky 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3176793fc096SFrank Li 	}
3177793fc096SFrank Li 
3178793fc096SFrank Li 	/* Set the last buffer to wrap. */
31797355f276STroy Kisky 	bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
31805cfa3039SJohannes Berg 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3181793fc096SFrank Li 
3182793fc096SFrank Li 	return 0;
3183ffdce2ccSRussell King 
3184ffdce2ccSRussell King  err_alloc:
3185ffdce2ccSRussell King 	fec_enet_free_buffers(ndev);
3186ffdce2ccSRussell King 	return -ENOMEM;
3187793fc096SFrank Li }
3188793fc096SFrank Li 
318959d0f746SFrank Li static int fec_enet_alloc_buffers(struct net_device *ndev)
319059d0f746SFrank Li {
319159d0f746SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
319259d0f746SFrank Li 	unsigned int i;
319359d0f746SFrank Li 
319459d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++)
319559d0f746SFrank Li 		if (fec_enet_alloc_rxq_buffers(ndev, i))
319659d0f746SFrank Li 			return -ENOMEM;
319759d0f746SFrank Li 
319859d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++)
319959d0f746SFrank Li 		if (fec_enet_alloc_txq_buffers(ndev, i))
320059d0f746SFrank Li 			return -ENOMEM;
320159d0f746SFrank Li 	return 0;
320259d0f746SFrank Li }
320359d0f746SFrank Li 
3204793fc096SFrank Li static int
3205793fc096SFrank Li fec_enet_open(struct net_device *ndev)
3206793fc096SFrank Li {
3207793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3208793fc096SFrank Li 	int ret;
32091b0a83acSRichard Leitner 	bool reset_again;
3210793fc096SFrank Li 
3211da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(&fep->pdev->dev);
3212b0c6ce24SFabio Estevam 	if (ret < 0)
32138fff755eSAndrew Lunn 		return ret;
32148fff755eSAndrew Lunn 
32155bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&fep->pdev->dev);
3216e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
3217e8fcfcd5SNimrod Andy 	if (ret)
32188fff755eSAndrew Lunn 		goto clk_enable;
3219e8fcfcd5SNimrod Andy 
32201b0a83acSRichard Leitner 	/* During the first fec_enet_open call the PHY isn't probed at this
32211b0a83acSRichard Leitner 	 * point. Therefore the phy_reset_after_clk_enable() call within
32221b0a83acSRichard Leitner 	 * fec_enet_clk_enable() fails. As we need this reset in order to be
32231b0a83acSRichard Leitner 	 * sure the PHY is working correctly we check if we need to reset again
32241b0a83acSRichard Leitner 	 * later when the PHY is probed
32251b0a83acSRichard Leitner 	 */
32261b0a83acSRichard Leitner 	if (ndev->phydev && ndev->phydev->drv)
32271b0a83acSRichard Leitner 		reset_again = false;
32281b0a83acSRichard Leitner 	else
32291b0a83acSRichard Leitner 		reset_again = true;
32301b0a83acSRichard Leitner 
3231793fc096SFrank Li 	/* I should reset the ring buffers here, but I don't yet know
3232793fc096SFrank Li 	 * a simple way to do that.
3233793fc096SFrank Li 	 */
3234793fc096SFrank Li 
3235793fc096SFrank Li 	ret = fec_enet_alloc_buffers(ndev);
3236793fc096SFrank Li 	if (ret)
3237681d2421SFabio Estevam 		goto err_enet_alloc;
3238793fc096SFrank Li 
323955dd2753SNimrod Andy 	/* Init MAC prior to mii bus probe */
324055dd2753SNimrod Andy 	fec_restart(ndev);
324155dd2753SNimrod Andy 
32421b0a83acSRichard Leitner 	/* Call phy_reset_after_clk_enable() again if it failed during
32431b0a83acSRichard Leitner 	 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
32441b0a83acSRichard Leitner 	 */
32451b0a83acSRichard Leitner 	if (reset_again)
324664a632daSMarek Vasut 		fec_enet_phy_reset_after_clk_enable(ndev);
32471b0a83acSRichard Leitner 
32480da1ccbbSMarek Vasut 	/* Probe and connect to PHY when open the interface */
32490da1ccbbSMarek Vasut 	ret = fec_enet_mii_probe(ndev);
32500da1ccbbSMarek Vasut 	if (ret)
32510da1ccbbSMarek Vasut 		goto err_enet_mii_probe;
3252681d2421SFabio Estevam 
3253681d2421SFabio Estevam 	if (fep->quirks & FEC_QUIRK_ERR006687)
3254793fc096SFrank Li 		imx6q_cpuidle_fec_irqs_used();
3255793fc096SFrank Li 
32567d650df9SWei Fang 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
32577d650df9SWei Fang 		cpu_latency_qos_add_request(&fep->pm_qos_req, 0);
32587d650df9SWei Fang 
3259793fc096SFrank Li 	napi_enable(&fep->napi);
3260793fc096SFrank Li 	phy_start(ndev->phydev);
3261793fc096SFrank Li 	netif_tx_start_all_queues(ndev);
3262793fc096SFrank Li 
3263793fc096SFrank Li 	device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
3264793fc096SFrank Li 				 FEC_WOL_FLAG_ENABLE);
3265793fc096SFrank Li 
3266793fc096SFrank Li 	return 0;
3267793fc096SFrank Li 
3268793fc096SFrank Li err_enet_mii_probe:
3269793fc096SFrank Li 	fec_enet_free_buffers(ndev);
3270793fc096SFrank Li err_enet_alloc:
3271793fc096SFrank Li 	fec_enet_clk_enable(ndev, false);
3272793fc096SFrank Li clk_enable:
3273793fc096SFrank Li 	pm_runtime_mark_last_busy(&fep->pdev->dev);
3274793fc096SFrank Li 	pm_runtime_put_autosuspend(&fep->pdev->dev);
3275793fc096SFrank Li 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3276793fc096SFrank Li 	return ret;
3277d76cfae9SRussell King }
3278d76cfae9SRussell King 
327931a6de34SRussell King static int
3280793fc096SFrank Li fec_enet_close(struct net_device *ndev)
3281b49cd504SRussell King {
3282793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
328331a6de34SRussell King 
328445f5c327SPhilippe Reynes 	phy_stop(ndev->phydev);
3285793fc096SFrank Li 
3286793fc096SFrank Li 	if (netif_device_present(ndev)) {
3287793fc096SFrank Li 		napi_disable(&fep->napi);
3288793fc096SFrank Li 		netif_tx_disable(ndev);
3289793fc096SFrank Li 		fec_stop(ndev);
3290793fc096SFrank Li 	}
3291793fc096SFrank Li 
329245f5c327SPhilippe Reynes 	phy_disconnect(ndev->phydev);
3293793fc096SFrank Li 
329429380905SLucas Stach 	if (fep->quirks & FEC_QUIRK_ERR006687)
329529380905SLucas Stach 		imx6q_cpuidle_fec_irqs_unused();
329629380905SLucas Stach 
329780cca775SNikita Yushchenko 	fec_enet_update_ethtool_stats(ndev);
329880cca775SNikita Yushchenko 
3299e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
33007d650df9SWei Fang 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
33017d650df9SWei Fang 		cpu_latency_qos_remove_request(&fep->pm_qos_req);
33027d650df9SWei Fang 
33035bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
33048fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&fep->pdev->dev);
33058fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&fep->pdev->dev);
33068fff755eSAndrew Lunn 
3307793fc096SFrank Li 	fec_enet_free_buffers(ndev);
3308793fc096SFrank Li 
3309793fc096SFrank Li 	return 0;
3310793fc096SFrank Li }
3311793fc096SFrank Li 
3312793fc096SFrank Li /* Set or clear the multicast filter for this adaptor.
3313793fc096SFrank Li  * Skeleton taken from sunlance driver.
3314793fc096SFrank Li  * The CPM Ethernet implementation allows Multicast as well as individual
3315793fc096SFrank Li  * MAC address filtering.  Some of the drivers check to make sure it is
3316793fc096SFrank Li  * a group multicast address, and discard those that are not.  I guess I
3317793fc096SFrank Li  * will do the same for now, but just remove the test if you want
3318793fc096SFrank Li  * individual filtering as well (do the upper net layers want or support
3319793fc096SFrank Li  * this kind of feature?).
3320793fc096SFrank Li  */
3321793fc096SFrank Li 
33226176e89cSJiri Kosina #define FEC_HASH_BITS	6		/* #bits in hash */
3323793fc096SFrank Li 
3324793fc096SFrank Li static void set_multicast_list(struct net_device *ndev)
3325793fc096SFrank Li {
3326793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3327793fc096SFrank Li 	struct netdev_hw_addr *ha;
332816f6e983SKrzysztof Kozlowski 	unsigned int crc, tmp;
3329793fc096SFrank Li 	unsigned char hash;
333001f8902bSRui Sousa 	unsigned int hash_high = 0, hash_low = 0;
3331793fc096SFrank Li 
3332793fc096SFrank Li 	if (ndev->flags & IFF_PROMISC) {
3333793fc096SFrank Li 		tmp = readl(fep->hwp + FEC_R_CNTRL);
3334793fc096SFrank Li 		tmp |= 0x8;
3335793fc096SFrank Li 		writel(tmp, fep->hwp + FEC_R_CNTRL);
3336793fc096SFrank Li 		return;
3337793fc096SFrank Li 	}
3338793fc096SFrank Li 
3339793fc096SFrank Li 	tmp = readl(fep->hwp + FEC_R_CNTRL);
3340793fc096SFrank Li 	tmp &= ~0x8;
3341793fc096SFrank Li 	writel(tmp, fep->hwp + FEC_R_CNTRL);
3342793fc096SFrank Li 
3343793fc096SFrank Li 	if (ndev->flags & IFF_ALLMULTI) {
3344793fc096SFrank Li 		/* Catch all multicast addresses, so set the
3345793fc096SFrank Li 		 * filter to all 1's
3346793fc096SFrank Li 		 */
3347793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3348793fc096SFrank Li 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3349793fc096SFrank Li 
3350793fc096SFrank Li 		return;
3351793fc096SFrank Li 	}
3352793fc096SFrank Li 
335301f8902bSRui Sousa 	/* Add the addresses in hash register */
3354793fc096SFrank Li 	netdev_for_each_mc_addr(ha, ndev) {
3355793fc096SFrank Li 		/* calculate crc32 value of mac address */
335616f6e983SKrzysztof Kozlowski 		crc = ether_crc_le(ndev->addr_len, ha->addr);
3357793fc096SFrank Li 
33586176e89cSJiri Kosina 		/* only upper 6 bits (FEC_HASH_BITS) are used
3359981a0547SPeter Meerwald-Stadler 		 * which point to specific bit in the hash registers
3360793fc096SFrank Li 		 */
33616176e89cSJiri Kosina 		hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
3362793fc096SFrank Li 
336301f8902bSRui Sousa 		if (hash > 31)
336401f8902bSRui Sousa 			hash_high |= 1 << (hash - 32);
336501f8902bSRui Sousa 		else
336601f8902bSRui Sousa 			hash_low |= 1 << hash;
3367793fc096SFrank Li 	}
336801f8902bSRui Sousa 
336901f8902bSRui Sousa 	writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
337001f8902bSRui Sousa 	writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3371793fc096SFrank Li }
3372793fc096SFrank Li 
3373793fc096SFrank Li /* Set a MAC change in hardware. */
3374793fc096SFrank Li static int
3375793fc096SFrank Li fec_set_mac_address(struct net_device *ndev, void *p)
3376793fc096SFrank Li {
3377793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3378793fc096SFrank Li 	struct sockaddr *addr = p;
3379793fc096SFrank Li 
338044934facSLucas Stach 	if (addr) {
3381793fc096SFrank Li 		if (!is_valid_ether_addr(addr->sa_data))
3382793fc096SFrank Li 			return -EADDRNOTAVAIL;
3383a05e4c0aSJakub Kicinski 		eth_hw_addr_set(ndev, addr->sa_data);
338444934facSLucas Stach 	}
3385793fc096SFrank Li 
33869638d19eSNimrod Andy 	/* Add netif status check here to avoid system hang in below case:
33879638d19eSNimrod Andy 	 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
33889638d19eSNimrod Andy 	 * After ethx down, fec all clocks are gated off and then register
33899638d19eSNimrod Andy 	 * access causes system hang.
33909638d19eSNimrod Andy 	 */
33919638d19eSNimrod Andy 	if (!netif_running(ndev))
33929638d19eSNimrod Andy 		return 0;
33939638d19eSNimrod Andy 
3394793fc096SFrank Li 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3395793fc096SFrank Li 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3396793fc096SFrank Li 		fep->hwp + FEC_ADDR_LOW);
3397793fc096SFrank Li 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3398793fc096SFrank Li 		fep->hwp + FEC_ADDR_HIGH);
3399793fc096SFrank Li 	return 0;
3400793fc096SFrank Li }
3401793fc096SFrank Li 
3402793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3403793fc096SFrank Li /**
3404793fc096SFrank Li  * fec_poll_controller - FEC Poll controller function
3405793fc096SFrank Li  * @dev: The FEC network adapter
3406793fc096SFrank Li  *
3407793fc096SFrank Li  * Polled functionality used by netconsole and others in non interrupt mode
3408793fc096SFrank Li  *
3409793fc096SFrank Li  */
3410793fc096SFrank Li static void fec_poll_controller(struct net_device *dev)
3411793fc096SFrank Li {
3412793fc096SFrank Li 	int i;
3413793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(dev);
3414793fc096SFrank Li 
3415793fc096SFrank Li 	for (i = 0; i < FEC_IRQ_NUM; i++) {
3416793fc096SFrank Li 		if (fep->irq[i] > 0) {
3417793fc096SFrank Li 			disable_irq(fep->irq[i]);
3418793fc096SFrank Li 			fec_enet_interrupt(fep->irq[i], dev);
3419793fc096SFrank Li 			enable_irq(fep->irq[i]);
3420793fc096SFrank Li 		}
3421793fc096SFrank Li 	}
3422793fc096SFrank Li }
3423793fc096SFrank Li #endif
3424793fc096SFrank Li 
34255bc26726SNimrod Andy static inline void fec_enet_set_netdev_features(struct net_device *netdev,
34264c09eed9SJim Baxter 	netdev_features_t features)
34274c09eed9SJim Baxter {
34284c09eed9SJim Baxter 	struct fec_enet_private *fep = netdev_priv(netdev);
34294c09eed9SJim Baxter 	netdev_features_t changed = features ^ netdev->features;
34304c09eed9SJim Baxter 
34314c09eed9SJim Baxter 	netdev->features = features;
34324c09eed9SJim Baxter 
34334c09eed9SJim Baxter 	/* Receive checksum has been changed */
34344c09eed9SJim Baxter 	if (changed & NETIF_F_RXCSUM) {
34354c09eed9SJim Baxter 		if (features & NETIF_F_RXCSUM)
34364c09eed9SJim Baxter 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
34374c09eed9SJim Baxter 		else
34384c09eed9SJim Baxter 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
34398506fa1dSRussell King 	}
34405bc26726SNimrod Andy }
34414c09eed9SJim Baxter 
34425bc26726SNimrod Andy static int fec_set_features(struct net_device *netdev,
34435bc26726SNimrod Andy 	netdev_features_t features)
34445bc26726SNimrod Andy {
34455bc26726SNimrod Andy 	struct fec_enet_private *fep = netdev_priv(netdev);
34465bc26726SNimrod Andy 	netdev_features_t changed = features ^ netdev->features;
34475bc26726SNimrod Andy 
34485b40f709SFabio Estevam 	if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
34495bc26726SNimrod Andy 		napi_disable(&fep->napi);
34505bc26726SNimrod Andy 		netif_tx_lock_bh(netdev);
34515bc26726SNimrod Andy 		fec_stop(netdev);
34525bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
3453ef83337dSRussell King 		fec_restart(netdev);
34544d494cdcSFugang Duan 		netif_tx_wake_all_queues(netdev);
34556af42d42SRussell King 		netif_tx_unlock_bh(netdev);
3456dbc64a8eSRussell King 		napi_enable(&fep->napi);
34575bc26726SNimrod Andy 	} else {
34585bc26726SNimrod Andy 		fec_enet_set_netdev_features(netdev, features);
34594c09eed9SJim Baxter 	}
34604c09eed9SJim Baxter 
34614c09eed9SJim Baxter 	return 0;
34624c09eed9SJim Baxter }
34634c09eed9SJim Baxter 
346452c4a1a8SFugang Duan static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb)
346552c4a1a8SFugang Duan {
346652c4a1a8SFugang Duan 	struct vlan_ethhdr *vhdr;
346752c4a1a8SFugang Duan 	unsigned short vlan_TCI = 0;
346852c4a1a8SFugang Duan 
346952c4a1a8SFugang Duan 	if (skb->protocol == htons(ETH_P_ALL)) {
347052c4a1a8SFugang Duan 		vhdr = (struct vlan_ethhdr *)(skb->data);
347152c4a1a8SFugang Duan 		vlan_TCI = ntohs(vhdr->h_vlan_TCI);
347252c4a1a8SFugang Duan 	}
347352c4a1a8SFugang Duan 
347452c4a1a8SFugang Duan 	return vlan_TCI;
347552c4a1a8SFugang Duan }
347652c4a1a8SFugang Duan 
347752c4a1a8SFugang Duan static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
347852c4a1a8SFugang Duan 				 struct net_device *sb_dev)
347952c4a1a8SFugang Duan {
348052c4a1a8SFugang Duan 	struct fec_enet_private *fep = netdev_priv(ndev);
348152c4a1a8SFugang Duan 	u16 vlan_tag;
348252c4a1a8SFugang Duan 
348352c4a1a8SFugang Duan 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
348452c4a1a8SFugang Duan 		return netdev_pick_tx(ndev, skb, NULL);
348552c4a1a8SFugang Duan 
348652c4a1a8SFugang Duan 	vlan_tag = fec_enet_get_raw_vlan_tci(skb);
348752c4a1a8SFugang Duan 	if (!vlan_tag)
348852c4a1a8SFugang Duan 		return vlan_tag;
348952c4a1a8SFugang Duan 
349052c4a1a8SFugang Duan 	return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
349152c4a1a8SFugang Duan }
349252c4a1a8SFugang Duan 
3493793fc096SFrank Li static const struct net_device_ops fec_netdev_ops = {
3494793fc096SFrank Li 	.ndo_open		= fec_enet_open,
3495793fc096SFrank Li 	.ndo_stop		= fec_enet_close,
3496793fc096SFrank Li 	.ndo_start_xmit		= fec_enet_start_xmit,
349752c4a1a8SFugang Duan 	.ndo_select_queue       = fec_enet_select_queue,
3498793fc096SFrank Li 	.ndo_set_rx_mode	= set_multicast_list,
3499793fc096SFrank Li 	.ndo_validate_addr	= eth_validate_addr,
3500793fc096SFrank Li 	.ndo_tx_timeout		= fec_timeout,
3501793fc096SFrank Li 	.ndo_set_mac_address	= fec_set_mac_address,
3502a7605370SArnd Bergmann 	.ndo_eth_ioctl		= fec_enet_ioctl,
3503793fc096SFrank Li #ifdef CONFIG_NET_POLL_CONTROLLER
3504793fc096SFrank Li 	.ndo_poll_controller	= fec_poll_controller,
3505793fc096SFrank Li #endif
35064c09eed9SJim Baxter 	.ndo_set_features	= fec_set_features,
3507793fc096SFrank Li };
3508793fc096SFrank Li 
350953bb20d1STroy Kisky static const unsigned short offset_des_active_rxq[] = {
351053bb20d1STroy Kisky 	FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
351153bb20d1STroy Kisky };
351253bb20d1STroy Kisky 
351353bb20d1STroy Kisky static const unsigned short offset_des_active_txq[] = {
351453bb20d1STroy Kisky 	FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
351553bb20d1STroy Kisky };
351653bb20d1STroy Kisky 
3517793fc096SFrank Li  /*
3518793fc096SFrank Li   * XXX:  We need to clean up on failure exits here.
3519793fc096SFrank Li   *
3520793fc096SFrank Li   */
3521793fc096SFrank Li static int fec_enet_init(struct net_device *ndev)
3522793fc096SFrank Li {
3523793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
3524793fc096SFrank Li 	struct bufdesc *cbd_base;
35254d494cdcSFugang Duan 	dma_addr_t bd_dma;
352655d0218aSNimrod Andy 	int bd_size;
352759d0f746SFrank Li 	unsigned int i;
35287355f276STroy Kisky 	unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
35297355f276STroy Kisky 			sizeof(struct bufdesc);
35307355f276STroy Kisky 	unsigned dsize_log2 = __fls(dsize);
3531453e9dc4SStefan Agner 	int ret;
353255d0218aSNimrod Andy 
35337355f276STroy Kisky 	WARN_ON(dsize != (1 << dsize_log2));
35343f1dcc6aSLucas Stach #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
353541ef84ceSFugang Duan 	fep->rx_align = 0xf;
353641ef84ceSFugang Duan 	fep->tx_align = 0xf;
353741ef84ceSFugang Duan #else
353841ef84ceSFugang Duan 	fep->rx_align = 0x3;
353941ef84ceSFugang Duan 	fep->tx_align = 0x3;
354041ef84ceSFugang Duan #endif
354141ef84ceSFugang Duan 
3542453e9dc4SStefan Agner 	/* Check mask of the streaming and coherent API */
3543453e9dc4SStefan Agner 	ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
3544453e9dc4SStefan Agner 	if (ret < 0) {
3545453e9dc4SStefan Agner 		dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
3546453e9dc4SStefan Agner 		return ret;
3547453e9dc4SStefan Agner 	}
3548453e9dc4SStefan Agner 
3549619fee9eSFugang Duan 	ret = fec_enet_alloc_queue(ndev);
3550619fee9eSFugang Duan 	if (ret)
3551619fee9eSFugang Duan 		return ret;
355279f33912SNimrod Andy 
35537355f276STroy Kisky 	bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3554793fc096SFrank Li 
3555793fc096SFrank Li 	/* Allocate memory for buffer descriptors. */
3556c0a1a0a6SLucas Stach 	cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3557793fc096SFrank Li 				       GFP_KERNEL);
35584d494cdcSFugang Duan 	if (!cbd_base) {
3559619fee9eSFugang Duan 		ret = -ENOMEM;
3560619fee9eSFugang Duan 		goto free_queue_mem;
35614d494cdcSFugang Duan 	}
3562793fc096SFrank Li 
3563793fc096SFrank Li 	/* Get the Ethernet address */
3564052fcc45SFugang Duan 	ret = fec_get_mac(ndev);
3565052fcc45SFugang Duan 	if (ret)
3566052fcc45SFugang Duan 		goto free_queue_mem;
3567052fcc45SFugang Duan 
356844934facSLucas Stach 	/* make sure MAC we just acquired is programmed into the hw */
356944934facSLucas Stach 	fec_set_mac_address(ndev, NULL);
3570793fc096SFrank Li 
3571793fc096SFrank Li 	/* Set receive and transmit descriptor base. */
357259d0f746SFrank Li 	for (i = 0; i < fep->num_rx_queues; i++) {
35737355f276STroy Kisky 		struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
35747355f276STroy Kisky 		unsigned size = dsize * rxq->bd.ring_size;
35757355f276STroy Kisky 
35767355f276STroy Kisky 		rxq->bd.qid = i;
35777355f276STroy Kisky 		rxq->bd.base = cbd_base;
35787355f276STroy Kisky 		rxq->bd.cur = cbd_base;
35797355f276STroy Kisky 		rxq->bd.dma = bd_dma;
35807355f276STroy Kisky 		rxq->bd.dsize = dsize;
35817355f276STroy Kisky 		rxq->bd.dsize_log2 = dsize_log2;
358253bb20d1STroy Kisky 		rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
35837355f276STroy Kisky 		bd_dma += size;
35847355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
35857355f276STroy Kisky 		rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
358659d0f746SFrank Li 	}
358759d0f746SFrank Li 
358859d0f746SFrank Li 	for (i = 0; i < fep->num_tx_queues; i++) {
35897355f276STroy Kisky 		struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
35907355f276STroy Kisky 		unsigned size = dsize * txq->bd.ring_size;
35917355f276STroy Kisky 
35927355f276STroy Kisky 		txq->bd.qid = i;
35937355f276STroy Kisky 		txq->bd.base = cbd_base;
35947355f276STroy Kisky 		txq->bd.cur = cbd_base;
35957355f276STroy Kisky 		txq->bd.dma = bd_dma;
35967355f276STroy Kisky 		txq->bd.dsize = dsize;
35977355f276STroy Kisky 		txq->bd.dsize_log2 = dsize_log2;
359853bb20d1STroy Kisky 		txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
35997355f276STroy Kisky 		bd_dma += size;
36007355f276STroy Kisky 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
36017355f276STroy Kisky 		txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
360259d0f746SFrank Li 	}
36034d494cdcSFugang Duan 
3604793fc096SFrank Li 
3605793fc096SFrank Li 	/* The FEC Ethernet specific entries in the device structure */
3606793fc096SFrank Li 	ndev->watchdog_timeo = TX_TIMEOUT;
3607793fc096SFrank Li 	ndev->netdev_ops = &fec_netdev_ops;
3608793fc096SFrank Li 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
3609793fc096SFrank Li 
3610793fc096SFrank Li 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3611*b48b89f9SJakub Kicinski 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi);
3612793fc096SFrank Li 
36136b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3614cdffcf1bSJim Baxter 		/* enable hw VLAN support */
3615cdffcf1bSJim Baxter 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3616cdffcf1bSJim Baxter 
36176b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
3618ee8b7a11SJakub Kicinski 		netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS);
361979f33912SNimrod Andy 
36204c09eed9SJim Baxter 		/* enable hw accelerator */
36214c09eed9SJim Baxter 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
362279f33912SNimrod Andy 				| NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
36234c09eed9SJim Baxter 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
362448496255SShawn Guo 	}
36254c09eed9SJim Baxter 
3626471ff445SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
362741ef84ceSFugang Duan 		fep->tx_align = 0;
362841ef84ceSFugang Duan 		fep->rx_align = 0x3f;
362941ef84ceSFugang Duan 	}
363041ef84ceSFugang Duan 
363109d1e541SNimrod Andy 	ndev->hw_features = ndev->features;
363209d1e541SNimrod Andy 
3633ef83337dSRussell King 	fec_restart(ndev);
3634793fc096SFrank Li 
36352b30842bSAndrew Lunn 	if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
36362b30842bSAndrew Lunn 		fec_enet_clear_ethtool_stats(ndev);
36372b30842bSAndrew Lunn 	else
363880cca775SNikita Yushchenko 		fec_enet_update_ethtool_stats(ndev);
363980cca775SNikita Yushchenko 
3640793fc096SFrank Li 	return 0;
3641619fee9eSFugang Duan 
3642619fee9eSFugang Duan free_queue_mem:
3643619fee9eSFugang Duan 	fec_enet_free_queue(ndev);
3644619fee9eSFugang Duan 	return ret;
3645793fc096SFrank Li }
3646793fc096SFrank Li 
3647793fc096SFrank Li #ifdef CONFIG_OF
36489269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev)
3649793fc096SFrank Li {
3650793fc096SFrank Li 	int err, phy_reset;
3651962d8cdcSBernhard Walle 	bool active_high = false;
3652159a0760SQuentin Schulz 	int msec = 1, phy_post_delay = 0;
3653793fc096SFrank Li 	struct device_node *np = pdev->dev.of_node;
3654793fc096SFrank Li 
3655793fc096SFrank Li 	if (!np)
36569269e556SFugang Duan 		return 0;
3657793fc096SFrank Li 
365861e04ccbSFugang Duan 	err = of_property_read_u32(np, "phy-reset-duration", &msec);
3659793fc096SFrank Li 	/* A sane reset duration should not be longer than 1s */
366061e04ccbSFugang Duan 	if (!err && msec > 1000)
3661793fc096SFrank Li 		msec = 1;
3662793fc096SFrank Li 
3663793fc096SFrank Li 	phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
36649269e556SFugang Duan 	if (phy_reset == -EPROBE_DEFER)
36659269e556SFugang Duan 		return phy_reset;
36669269e556SFugang Duan 	else if (!gpio_is_valid(phy_reset))
36679269e556SFugang Duan 		return 0;
3668793fc096SFrank Li 
3669159a0760SQuentin Schulz 	err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
3670159a0760SQuentin Schulz 	/* valid reset duration should be less than 1s */
3671159a0760SQuentin Schulz 	if (!err && phy_post_delay > 1000)
3672159a0760SQuentin Schulz 		return -EINVAL;
3673159a0760SQuentin Schulz 
3674962d8cdcSBernhard Walle 	active_high = of_property_read_bool(np, "phy-reset-active-high");
367564f10f6eSBernhard Walle 
3676793fc096SFrank Li 	err = devm_gpio_request_one(&pdev->dev, phy_reset,
3677962d8cdcSBernhard Walle 			active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
367864f10f6eSBernhard Walle 			"phy-reset");
3679793fc096SFrank Li 	if (err) {
3680793fc096SFrank Li 		dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
36819269e556SFugang Duan 		return err;
3682793fc096SFrank Li 	}
3683eb37c563SStefan Wahren 
3684eb37c563SStefan Wahren 	if (msec > 20)
3685793fc096SFrank Li 		msleep(msec);
3686eb37c563SStefan Wahren 	else
3687eb37c563SStefan Wahren 		usleep_range(msec * 1000, msec * 1000 + 1000);
3688eb37c563SStefan Wahren 
3689962d8cdcSBernhard Walle 	gpio_set_value_cansleep(phy_reset, !active_high);
36909269e556SFugang Duan 
3691159a0760SQuentin Schulz 	if (!phy_post_delay)
3692159a0760SQuentin Schulz 		return 0;
3693159a0760SQuentin Schulz 
3694159a0760SQuentin Schulz 	if (phy_post_delay > 20)
3695159a0760SQuentin Schulz 		msleep(phy_post_delay);
3696159a0760SQuentin Schulz 	else
3697159a0760SQuentin Schulz 		usleep_range(phy_post_delay * 1000,
3698159a0760SQuentin Schulz 			     phy_post_delay * 1000 + 1000);
3699159a0760SQuentin Schulz 
37009269e556SFugang Duan 	return 0;
3701793fc096SFrank Li }
3702793fc096SFrank Li #else /* CONFIG_OF */
37039269e556SFugang Duan static int fec_reset_phy(struct platform_device *pdev)
3704793fc096SFrank Li {
3705793fc096SFrank Li 	/*
3706793fc096SFrank Li 	 * In case of platform probe, the reset has been done
3707793fc096SFrank Li 	 * by machine code.
3708793fc096SFrank Li 	 */
37099269e556SFugang Duan 	return 0;
3710793fc096SFrank Li }
3711793fc096SFrank Li #endif /* CONFIG_OF */
3712793fc096SFrank Li 
37139fc095f1SFugang Duan static void
37149fc095f1SFugang Duan fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
37159fc095f1SFugang Duan {
37169fc095f1SFugang Duan 	struct device_node *np = pdev->dev.of_node;
37179fc095f1SFugang Duan 
37189fc095f1SFugang Duan 	*num_tx = *num_rx = 1;
37199fc095f1SFugang Duan 
37209fc095f1SFugang Duan 	if (!np || !of_device_is_available(np))
37219fc095f1SFugang Duan 		return;
37229fc095f1SFugang Duan 
37239fc095f1SFugang Duan 	/* parse the num of tx and rx queues */
372473b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3725b7bd75cfSFrank Li 
372673b1c90dSSaurabh Sengar 	of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
37279fc095f1SFugang Duan 
37289fc095f1SFugang Duan 	if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3729b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
37309fc095f1SFugang Duan 			 *num_tx);
37319fc095f1SFugang Duan 		*num_tx = 1;
37329fc095f1SFugang Duan 		return;
37339fc095f1SFugang Duan 	}
37349fc095f1SFugang Duan 
37359fc095f1SFugang Duan 	if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3736b7bd75cfSFrank Li 		dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
37379fc095f1SFugang Duan 			 *num_rx);
37389fc095f1SFugang Duan 		*num_rx = 1;
37399fc095f1SFugang Duan 		return;
37409fc095f1SFugang Duan 	}
37419fc095f1SFugang Duan 
37429fc095f1SFugang Duan }
37439fc095f1SFugang Duan 
37444ad1ceecSTroy Kisky static int fec_enet_get_irq_cnt(struct platform_device *pdev)
37454ad1ceecSTroy Kisky {
37464ad1ceecSTroy Kisky 	int irq_cnt = platform_irq_count(pdev);
37474ad1ceecSTroy Kisky 
37484ad1ceecSTroy Kisky 	if (irq_cnt > FEC_IRQ_NUM)
37494ad1ceecSTroy Kisky 		irq_cnt = FEC_IRQ_NUM;	/* last for pps */
37504ad1ceecSTroy Kisky 	else if (irq_cnt == 2)
37514ad1ceecSTroy Kisky 		irq_cnt = 1;	/* last for pps */
37524ad1ceecSTroy Kisky 	else if (irq_cnt <= 0)
37534ad1ceecSTroy Kisky 		irq_cnt = 1;	/* At least 1 irq is needed */
37544ad1ceecSTroy Kisky 	return irq_cnt;
37554ad1ceecSTroy Kisky }
37564ad1ceecSTroy Kisky 
3757b7cdc965SJoakim Zhang static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
3758b7cdc965SJoakim Zhang {
3759b7cdc965SJoakim Zhang 	struct net_device *ndev = platform_get_drvdata(pdev);
3760b7cdc965SJoakim Zhang 	struct fec_enet_private *fep = netdev_priv(ndev);
3761b7cdc965SJoakim Zhang 
3762b7cdc965SJoakim Zhang 	if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
3763b7cdc965SJoakim Zhang 		fep->wake_irq = fep->irq[2];
3764b7cdc965SJoakim Zhang 	else
3765b7cdc965SJoakim Zhang 		fep->wake_irq = fep->irq[0];
3766b7cdc965SJoakim Zhang }
3767b7cdc965SJoakim Zhang 
3768da722186SMartin Fuzzey static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
3769da722186SMartin Fuzzey 				   struct device_node *np)
3770da722186SMartin Fuzzey {
3771da722186SMartin Fuzzey 	struct device_node *gpr_np;
37728a448bf8SFugang Duan 	u32 out_val[3];
3773da722186SMartin Fuzzey 	int ret = 0;
3774da722186SMartin Fuzzey 
37758a448bf8SFugang Duan 	gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
3776da722186SMartin Fuzzey 	if (!gpr_np)
3777da722186SMartin Fuzzey 		return 0;
3778da722186SMartin Fuzzey 
37798a448bf8SFugang Duan 	ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
37808a448bf8SFugang Duan 					 ARRAY_SIZE(out_val));
37818a448bf8SFugang Duan 	if (ret) {
37828a448bf8SFugang Duan 		dev_dbg(&fep->pdev->dev, "no stop mode property\n");
3783d2b52ec0SYang Yingliang 		goto out;
37848a448bf8SFugang Duan 	}
37858a448bf8SFugang Duan 
3786da722186SMartin Fuzzey 	fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
3787da722186SMartin Fuzzey 	if (IS_ERR(fep->stop_gpr.gpr)) {
3788da722186SMartin Fuzzey 		dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
3789da722186SMartin Fuzzey 		ret = PTR_ERR(fep->stop_gpr.gpr);
3790da722186SMartin Fuzzey 		fep->stop_gpr.gpr = NULL;
3791da722186SMartin Fuzzey 		goto out;
3792da722186SMartin Fuzzey 	}
3793da722186SMartin Fuzzey 
37948a448bf8SFugang Duan 	fep->stop_gpr.reg = out_val[1];
37958a448bf8SFugang Duan 	fep->stop_gpr.bit = out_val[2];
3796da722186SMartin Fuzzey 
3797da722186SMartin Fuzzey out:
3798da722186SMartin Fuzzey 	of_node_put(gpr_np);
3799da722186SMartin Fuzzey 
3800da722186SMartin Fuzzey 	return ret;
3801da722186SMartin Fuzzey }
3802da722186SMartin Fuzzey 
3803793fc096SFrank Li static int
3804793fc096SFrank Li fec_probe(struct platform_device *pdev)
3805793fc096SFrank Li {
3806793fc096SFrank Li 	struct fec_enet_private *fep;
3807793fc096SFrank Li 	struct fec_platform_data *pdata;
38080c65b2b9SAndrew Lunn 	phy_interface_t interface;
3809793fc096SFrank Li 	struct net_device *ndev;
3810793fc096SFrank Li 	int i, irq, ret = 0;
3811793fc096SFrank Li 	const struct of_device_id *of_id;
3812793fc096SFrank Li 	static int dev_id;
3813407066f8SUwe Kleine-König 	struct device_node *np = pdev->dev.of_node, *phy_node;
3814b7bd75cfSFrank Li 	int num_tx_qs;
3815b7bd75cfSFrank Li 	int num_rx_qs;
38164ad1ceecSTroy Kisky 	char irq_name[8];
38174ad1ceecSTroy Kisky 	int irq_cnt;
3818da722186SMartin Fuzzey 	struct fec_devinfo *dev_info;
3819793fc096SFrank Li 
38209fc095f1SFugang Duan 	fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
38219fc095f1SFugang Duan 
3822793fc096SFrank Li 	/* Init network device */
382380cca775SNikita Yushchenko 	ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
3824f85de666SNikita Yushchenko 				  FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
3825793fc096SFrank Li 	if (!ndev)
3826793fc096SFrank Li 		return -ENOMEM;
3827793fc096SFrank Li 
3828793fc096SFrank Li 	SET_NETDEV_DEV(ndev, &pdev->dev);
3829793fc096SFrank Li 
3830793fc096SFrank Li 	/* setup board info structure */
3831793fc096SFrank Li 	fep = netdev_priv(ndev);
3832793fc096SFrank Li 
38336b7e4008SLothar Waßmann 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
38346b7e4008SLothar Waßmann 	if (of_id)
38356b7e4008SLothar Waßmann 		pdev->id_entry = of_id->data;
3836da722186SMartin Fuzzey 	dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
3837da722186SMartin Fuzzey 	if (dev_info)
3838da722186SMartin Fuzzey 		fep->quirks = dev_info->quirks;
38396b7e4008SLothar Waßmann 
38400c818594SHubert Feurstein 	fep->netdev = ndev;
38419fc095f1SFugang Duan 	fep->num_rx_queues = num_rx_qs;
38429fc095f1SFugang Duan 	fep->num_tx_queues = num_tx_qs;
38439fc095f1SFugang Duan 
3844d1391930SGuenter Roeck #if !defined(CONFIG_M5272)
3845793fc096SFrank Li 	/* default enable pause frame auto negotiation */
38466b7e4008SLothar Waßmann 	if (fep->quirks & FEC_QUIRK_HAS_GBIT)
3847793fc096SFrank Li 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3848d1391930SGuenter Roeck #endif
3849793fc096SFrank Li 
38505bbde4d2SNimrod Andy 	/* Select default pin state */
38515bbde4d2SNimrod Andy 	pinctrl_pm_select_default_state(&pdev->dev);
38525bbde4d2SNimrod Andy 
38534f830a5aSYueHaibing 	fep->hwp = devm_platform_ioremap_resource(pdev, 0);
3854941e173aSTushar Behera 	if (IS_ERR(fep->hwp)) {
3855941e173aSTushar Behera 		ret = PTR_ERR(fep->hwp);
3856941e173aSTushar Behera 		goto failed_ioremap;
3857941e173aSTushar Behera 	}
3858941e173aSTushar Behera 
3859793fc096SFrank Li 	fep->pdev = pdev;
3860793fc096SFrank Li 	fep->dev_id = dev_id++;
3861793fc096SFrank Li 
3862793fc096SFrank Li 	platform_set_drvdata(pdev, ndev);
3863793fc096SFrank Li 
386429380905SLucas Stach 	if ((of_machine_is_compatible("fsl,imx6q") ||
386529380905SLucas Stach 	     of_machine_is_compatible("fsl,imx6dl")) &&
386629380905SLucas Stach 	    !of_property_read_bool(np, "fsl,err006687-workaround-present"))
386729380905SLucas Stach 		fep->quirks |= FEC_QUIRK_ERR006687;
386829380905SLucas Stach 
386940c79ce1SWei Fang 	ret = fec_enet_ipc_handle_init(fep);
387040c79ce1SWei Fang 	if (ret)
387140c79ce1SWei Fang 		goto failed_ipc_init;
387240c79ce1SWei Fang 
3873de40ed31SNimrod Andy 	if (of_get_property(np, "fsl,magic-packet", NULL))
3874de40ed31SNimrod Andy 		fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3875de40ed31SNimrod Andy 
38768a448bf8SFugang Duan 	ret = fec_enet_init_stop_mode(fep, np);
3877da722186SMartin Fuzzey 	if (ret)
3878da722186SMartin Fuzzey 		goto failed_stop_mode;
3879da722186SMartin Fuzzey 
3880407066f8SUwe Kleine-König 	phy_node = of_parse_phandle(np, "phy-handle", 0);
3881407066f8SUwe Kleine-König 	if (!phy_node && of_phy_is_fixed_link(np)) {
3882407066f8SUwe Kleine-König 		ret = of_phy_register_fixed_link(np);
3883407066f8SUwe Kleine-König 		if (ret < 0) {
3884407066f8SUwe Kleine-König 			dev_err(&pdev->dev,
3885407066f8SUwe Kleine-König 				"broken fixed-link specification\n");
3886407066f8SUwe Kleine-König 			goto failed_phy;
3887407066f8SUwe Kleine-König 		}
3888407066f8SUwe Kleine-König 		phy_node = of_node_get(np);
3889407066f8SUwe Kleine-König 	}
3890407066f8SUwe Kleine-König 	fep->phy_node = phy_node;
3891407066f8SUwe Kleine-König 
38920c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(pdev->dev.of_node, &interface);
38930c65b2b9SAndrew Lunn 	if (ret) {
389494660ba0SJingoo Han 		pdata = dev_get_platdata(&pdev->dev);
3895793fc096SFrank Li 		if (pdata)
3896793fc096SFrank Li 			fep->phy_interface = pdata->phy;
3897793fc096SFrank Li 		else
3898793fc096SFrank Li 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
3899793fc096SFrank Li 	} else {
39000c65b2b9SAndrew Lunn 		fep->phy_interface = interface;
3901793fc096SFrank Li 	}
3902793fc096SFrank Li 
3903b820c114SJoakim Zhang 	ret = fec_enet_parse_rgmii_delay(fep, np);
3904b820c114SJoakim Zhang 	if (ret)
3905b820c114SJoakim Zhang 		goto failed_rgmii_delay;
3906b820c114SJoakim Zhang 
3907793fc096SFrank Li 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3908793fc096SFrank Li 	if (IS_ERR(fep->clk_ipg)) {
3909793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ipg);
3910793fc096SFrank Li 		goto failed_clk;
3911793fc096SFrank Li 	}
3912793fc096SFrank Li 
3913793fc096SFrank Li 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3914793fc096SFrank Li 	if (IS_ERR(fep->clk_ahb)) {
3915793fc096SFrank Li 		ret = PTR_ERR(fep->clk_ahb);
3916793fc096SFrank Li 		goto failed_clk;
3917793fc096SFrank Li 	}
3918793fc096SFrank Li 
3919d851b47bSFugang Duan 	fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3920d851b47bSFugang Duan 
392138f56f33SLinus Torvalds 	/* enet_out is optional, depends on board */
39225ff851b7SUwe Kleine-König 	fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out");
39235ff851b7SUwe Kleine-König 	if (IS_ERR(fep->clk_enet_out)) {
39245ff851b7SUwe Kleine-König 		ret = PTR_ERR(fep->clk_enet_out);
39255ff851b7SUwe Kleine-König 		goto failed_clk;
39265ff851b7SUwe Kleine-König 	}
392738f56f33SLinus Torvalds 
392891c0d987SNimrod Andy 	fep->ptp_clk_on = false;
392901b825f9SFrancesco Dolcini 	mutex_init(&fep->ptp_clk_mutex);
39309b5330edSFugang Duan 
39319b5330edSFugang Duan 	/* clk_ref is optional, depends on board */
393243252ed1SUwe Kleine-König 	fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
393343252ed1SUwe Kleine-König 	if (IS_ERR(fep->clk_ref)) {
393443252ed1SUwe Kleine-König 		ret = PTR_ERR(fep->clk_ref);
393543252ed1SUwe Kleine-König 		goto failed_clk;
393643252ed1SUwe Kleine-König 	}
3937b82f8c3fSFugang Duan 	fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
39389b5330edSFugang Duan 
3939fc539459SFugang Duan 	/* clk_2x_txclk is optional, depends on board */
3940b820c114SJoakim Zhang 	if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
3941fc539459SFugang Duan 		fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
3942fc539459SFugang Duan 		if (IS_ERR(fep->clk_2x_txclk))
3943fc539459SFugang Duan 			fep->clk_2x_txclk = NULL;
3944b820c114SJoakim Zhang 	}
3945fc539459SFugang Duan 
39466b7e4008SLothar Waßmann 	fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
3947793fc096SFrank Li 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3948793fc096SFrank Li 	if (IS_ERR(fep->clk_ptp)) {
394938f56f33SLinus Torvalds 		fep->clk_ptp = NULL;
3950217b5844SLothar Waßmann 		fep->bufdesc_ex = false;
3951793fc096SFrank Li 	}
3952793fc096SFrank Li 
3953e8fcfcd5SNimrod Andy 	ret = fec_enet_clk_enable(ndev, true);
395413a097bdSFabio Estevam 	if (ret)
395513a097bdSFabio Estevam 		goto failed_clk;
395613a097bdSFabio Estevam 
39578fff755eSAndrew Lunn 	ret = clk_prepare_enable(fep->clk_ipg);
39588fff755eSAndrew Lunn 	if (ret)
39598fff755eSAndrew Lunn 		goto failed_clk_ipg;
3960d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ahb);
3961d7c3a206SAndy Duan 	if (ret)
3962d7c3a206SAndy Duan 		goto failed_clk_ahb;
39638fff755eSAndrew Lunn 
396425974d8aSStefan Agner 	fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
3965f4e9f3d2SFabio Estevam 	if (!IS_ERR(fep->reg_phy)) {
3966f4e9f3d2SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
3967793fc096SFrank Li 		if (ret) {
3968793fc096SFrank Li 			dev_err(&pdev->dev,
3969793fc096SFrank Li 				"Failed to enable phy regulator: %d\n", ret);
3970793fc096SFrank Li 			goto failed_regulator;
3971793fc096SFrank Li 		}
3972f6a4d607SFabio Estevam 	} else {
39733f38c683SFugang Duan 		if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
39743f38c683SFugang Duan 			ret = -EPROBE_DEFER;
39753f38c683SFugang Duan 			goto failed_regulator;
39763f38c683SFugang Duan 		}
3977f6a4d607SFabio Estevam 		fep->reg_phy = NULL;
3978793fc096SFrank Li 	}
3979793fc096SFrank Li 
39808fff755eSAndrew Lunn 	pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
39818fff755eSAndrew Lunn 	pm_runtime_use_autosuspend(&pdev->dev);
398214d2b7c1SLucas Stach 	pm_runtime_get_noresume(&pdev->dev);
39838fff755eSAndrew Lunn 	pm_runtime_set_active(&pdev->dev);
39848fff755eSAndrew Lunn 	pm_runtime_enable(&pdev->dev);
39858fff755eSAndrew Lunn 
39869269e556SFugang Duan 	ret = fec_reset_phy(pdev);
39879269e556SFugang Duan 	if (ret)
39889269e556SFugang Duan 		goto failed_reset;
3989793fc096SFrank Li 
39904ad1ceecSTroy Kisky 	irq_cnt = fec_enet_get_irq_cnt(pdev);
3991793fc096SFrank Li 	if (fep->bufdesc_ex)
39924ad1ceecSTroy Kisky 		fec_ptp_init(pdev, irq_cnt);
3993793fc096SFrank Li 
3994793fc096SFrank Li 	ret = fec_enet_init(ndev);
3995793fc096SFrank Li 	if (ret)
3996793fc096SFrank Li 		goto failed_init;
3997793fc096SFrank Li 
39984ad1ceecSTroy Kisky 	for (i = 0; i < irq_cnt; i++) {
39993ded9f2bSArnd Bergmann 		snprintf(irq_name, sizeof(irq_name), "int%d", i);
40003b56be21SAnson Huang 		irq = platform_get_irq_byname_optional(pdev, irq_name);
40014ad1ceecSTroy Kisky 		if (irq < 0)
4002793fc096SFrank Li 			irq = platform_get_irq(pdev, i);
4003793fc096SFrank Li 		if (irq < 0) {
4004793fc096SFrank Li 			ret = irq;
4005793fc096SFrank Li 			goto failed_irq;
4006793fc096SFrank Li 		}
40070d9b2ab1SFabio Estevam 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
400844a272ddSMichael Opdenacker 				       0, pdev->name, ndev);
40090d9b2ab1SFabio Estevam 		if (ret)
4010793fc096SFrank Li 			goto failed_irq;
4011de40ed31SNimrod Andy 
4012de40ed31SNimrod Andy 		fep->irq[i] = irq;
4013793fc096SFrank Li 	}
4014793fc096SFrank Li 
4015b7cdc965SJoakim Zhang 	/* Decide which interrupt line is wakeup capable */
4016b7cdc965SJoakim Zhang 	fec_enet_get_wakeup_irq(pdev);
4017b7cdc965SJoakim Zhang 
4018793fc096SFrank Li 	ret = fec_enet_mii_init(pdev);
4019793fc096SFrank Li 	if (ret)
4020793fc096SFrank Li 		goto failed_mii_init;
4021793fc096SFrank Li 
4022793fc096SFrank Li 	/* Carrier starts down, phylib will bring it up */
4023793fc096SFrank Li 	netif_carrier_off(ndev);
4024e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
40255bbde4d2SNimrod Andy 	pinctrl_pm_select_sleep_state(&pdev->dev);
4026793fc096SFrank Li 
402759193053SAndrew Lunn 	ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN;
402859193053SAndrew Lunn 
4029793fc096SFrank Li 	ret = register_netdev(ndev);
4030793fc096SFrank Li 	if (ret)
4031793fc096SFrank Li 		goto failed_register;
4032793fc096SFrank Li 
4033de40ed31SNimrod Andy 	device_init_wakeup(&ndev->dev, fep->wol_flag &
4034de40ed31SNimrod Andy 			   FEC_WOL_HAS_MAGIC_PACKET);
4035de40ed31SNimrod Andy 
4036eb1d0640SFabio Estevam 	if (fep->bufdesc_ex && fep->ptp_clock)
4037eb1d0640SFabio Estevam 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
4038eb1d0640SFabio Estevam 
40391b7bde6dSNimrod Andy 	fep->rx_copybreak = COPYBREAK_DEFAULT;
404036cdc743SRussell King 	INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
40418fff755eSAndrew Lunn 
40428fff755eSAndrew Lunn 	pm_runtime_mark_last_busy(&pdev->dev);
40438fff755eSAndrew Lunn 	pm_runtime_put_autosuspend(&pdev->dev);
40448fff755eSAndrew Lunn 
4045793fc096SFrank Li 	return 0;
4046793fc096SFrank Li 
4047793fc096SFrank Li failed_register:
4048793fc096SFrank Li 	fec_enet_mii_remove(fep);
4049793fc096SFrank Li failed_mii_init:
40507a2bbd8dSFabio Estevam failed_irq:
40517a2bbd8dSFabio Estevam failed_init:
405232cba57bSLucas Stach 	fec_ptp_stop(pdev);
40539269e556SFugang Duan failed_reset:
4054ce8d24f9SAndy Duan 	pm_runtime_put_noidle(&pdev->dev);
40559269e556SFugang Duan 	pm_runtime_disable(&pdev->dev);
4056c6165cf0SFugang Duan 	if (fep->reg_phy)
4057c6165cf0SFugang Duan 		regulator_disable(fep->reg_phy);
4058793fc096SFrank Li failed_regulator:
4059d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
4060d7c3a206SAndy Duan failed_clk_ahb:
4061d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ipg);
40628fff755eSAndrew Lunn failed_clk_ipg:
4063e8fcfcd5SNimrod Andy 	fec_enet_clk_enable(ndev, false);
4064793fc096SFrank Li failed_clk:
4065b820c114SJoakim Zhang failed_rgmii_delay:
406682005b1cSJohan Hovold 	if (of_phy_is_fixed_link(np))
406782005b1cSJohan Hovold 		of_phy_deregister_fixed_link(np);
4068407066f8SUwe Kleine-König 	of_node_put(phy_node);
4069da722186SMartin Fuzzey failed_stop_mode:
407040c79ce1SWei Fang failed_ipc_init:
4071d1616f07SFugang Duan failed_phy:
4072d1616f07SFugang Duan 	dev_id--;
4073793fc096SFrank Li failed_ioremap:
4074793fc096SFrank Li 	free_netdev(ndev);
4075793fc096SFrank Li 
4076793fc096SFrank Li 	return ret;
4077793fc096SFrank Li }
4078793fc096SFrank Li 
4079793fc096SFrank Li static int
4080793fc096SFrank Li fec_drv_remove(struct platform_device *pdev)
4081793fc096SFrank Li {
4082793fc096SFrank Li 	struct net_device *ndev = platform_get_drvdata(pdev);
4083793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
408482005b1cSJohan Hovold 	struct device_node *np = pdev->dev.of_node;
4085a31eda65SChuhong Yuan 	int ret;
4086a31eda65SChuhong Yuan 
4087da875fa5SZhang Qilong 	ret = pm_runtime_resume_and_get(&pdev->dev);
4088a31eda65SChuhong Yuan 	if (ret < 0)
4089a31eda65SChuhong Yuan 		return ret;
4090793fc096SFrank Li 
409136cdc743SRussell King 	cancel_work_sync(&fep->tx_timeout_work);
409232cba57bSLucas Stach 	fec_ptp_stop(pdev);
4093793fc096SFrank Li 	unregister_netdev(ndev);
4094793fc096SFrank Li 	fec_enet_mii_remove(fep);
4095f6a4d607SFabio Estevam 	if (fep->reg_phy)
4096f6a4d607SFabio Estevam 		regulator_disable(fep->reg_phy);
4097a31eda65SChuhong Yuan 
409882005b1cSJohan Hovold 	if (of_phy_is_fixed_link(np))
409982005b1cSJohan Hovold 		of_phy_deregister_fixed_link(np);
4100407066f8SUwe Kleine-König 	of_node_put(fep->phy_node);
4101793fc096SFrank Li 
4102a31eda65SChuhong Yuan 	clk_disable_unprepare(fep->clk_ahb);
4103a31eda65SChuhong Yuan 	clk_disable_unprepare(fep->clk_ipg);
4104a31eda65SChuhong Yuan 	pm_runtime_put_noidle(&pdev->dev);
4105a31eda65SChuhong Yuan 	pm_runtime_disable(&pdev->dev);
4106a31eda65SChuhong Yuan 
410744712965SPavel Skripkin 	free_netdev(ndev);
4108793fc096SFrank Li 	return 0;
4109793fc096SFrank Li }
4110793fc096SFrank Li 
4111dd66d386SFabio Estevam static int __maybe_unused fec_suspend(struct device *dev)
4112793fc096SFrank Li {
4113793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
4114793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4115da970726SWei Fang 	int ret;
4116793fc096SFrank Li 
4117da1774e5SRussell King 	rtnl_lock();
4118793fc096SFrank Li 	if (netif_running(ndev)) {
4119de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
4120de40ed31SNimrod Andy 			fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
412145f5c327SPhilippe Reynes 		phy_stop(ndev->phydev);
412231a6de34SRussell King 		napi_disable(&fep->napi);
412331a6de34SRussell King 		netif_tx_lock_bh(ndev);
4124793fc096SFrank Li 		netif_device_detach(ndev);
412531a6de34SRussell King 		netif_tx_unlock_bh(ndev);
412631a6de34SRussell King 		fec_stop(ndev);
41270b6f65c7SJoakim Zhang 		if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
41280b6f65c7SJoakim Zhang 			fec_irqs_disable(ndev);
41295bbde4d2SNimrod Andy 			pinctrl_pm_select_sleep_state(&fep->pdev->dev);
41300b6f65c7SJoakim Zhang 		} else {
41310b6f65c7SJoakim Zhang 			fec_irqs_disable_except_wakeup(ndev);
41320b6f65c7SJoakim Zhang 			if (fep->wake_irq > 0) {
41330b6f65c7SJoakim Zhang 				disable_irq(fep->wake_irq);
41340b6f65c7SJoakim Zhang 				enable_irq_wake(fep->wake_irq);
41350b6f65c7SJoakim Zhang 			}
41360b6f65c7SJoakim Zhang 			fec_enet_stop_mode(fep, true);
41370b6f65c7SJoakim Zhang 		}
41380b6f65c7SJoakim Zhang 		/* It's safe to disable clocks since interrupts are masked */
41390b6f65c7SJoakim Zhang 		fec_enet_clk_enable(ndev, false);
4140da970726SWei Fang 
4141da970726SWei Fang 		fep->rpm_active = !pm_runtime_status_suspended(dev);
4142da970726SWei Fang 		if (fep->rpm_active) {
4143da970726SWei Fang 			ret = pm_runtime_force_suspend(dev);
4144da970726SWei Fang 			if (ret < 0) {
4145da970726SWei Fang 				rtnl_unlock();
4146da970726SWei Fang 				return ret;
4147da970726SWei Fang 			}
4148da970726SWei Fang 		}
4149f4c4a4e0SNimrod Andy 	}
4150f4c4a4e0SNimrod Andy 	rtnl_unlock();
4151793fc096SFrank Li 
4152de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
4153238f7bc7SFabio Estevam 		regulator_disable(fep->reg_phy);
4154238f7bc7SFabio Estevam 
4155858eeb7dSNimrod Andy 	/* SOC supply clock to phy, when clock is disabled, phy link down
4156858eeb7dSNimrod Andy 	 * SOC control phy regulator, when regulator is disabled, phy link down
4157858eeb7dSNimrod Andy 	 */
4158858eeb7dSNimrod Andy 	if (fep->clk_enet_out || fep->reg_phy)
4159858eeb7dSNimrod Andy 		fep->link = 0;
4160858eeb7dSNimrod Andy 
4161793fc096SFrank Li 	return 0;
4162793fc096SFrank Li }
4163793fc096SFrank Li 
4164dd66d386SFabio Estevam static int __maybe_unused fec_resume(struct device *dev)
4165793fc096SFrank Li {
4166793fc096SFrank Li 	struct net_device *ndev = dev_get_drvdata(dev);
4167793fc096SFrank Li 	struct fec_enet_private *fep = netdev_priv(ndev);
4168238f7bc7SFabio Estevam 	int ret;
4169de40ed31SNimrod Andy 	int val;
4170238f7bc7SFabio Estevam 
4171de40ed31SNimrod Andy 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4172238f7bc7SFabio Estevam 		ret = regulator_enable(fep->reg_phy);
4173238f7bc7SFabio Estevam 		if (ret)
4174238f7bc7SFabio Estevam 			return ret;
4175238f7bc7SFabio Estevam 	}
4176793fc096SFrank Li 
4177da1774e5SRussell King 	rtnl_lock();
4178793fc096SFrank Li 	if (netif_running(ndev)) {
4179da970726SWei Fang 		if (fep->rpm_active)
4180da970726SWei Fang 			pm_runtime_force_resume(dev);
4181da970726SWei Fang 
4182f4c4a4e0SNimrod Andy 		ret = fec_enet_clk_enable(ndev, true);
4183f4c4a4e0SNimrod Andy 		if (ret) {
4184f4c4a4e0SNimrod Andy 			rtnl_unlock();
4185f4c4a4e0SNimrod Andy 			goto failed_clk;
4186f4c4a4e0SNimrod Andy 		}
4187de40ed31SNimrod Andy 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
4188da722186SMartin Fuzzey 			fec_enet_stop_mode(fep, false);
41890b6f65c7SJoakim Zhang 			if (fep->wake_irq) {
41900b6f65c7SJoakim Zhang 				disable_irq_wake(fep->wake_irq);
41910b6f65c7SJoakim Zhang 				enable_irq(fep->wake_irq);
41920b6f65c7SJoakim Zhang 			}
4193da722186SMartin Fuzzey 
4194de40ed31SNimrod Andy 			val = readl(fep->hwp + FEC_ECNTRL);
4195de40ed31SNimrod Andy 			val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
4196de40ed31SNimrod Andy 			writel(val, fep->hwp + FEC_ECNTRL);
4197de40ed31SNimrod Andy 			fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
4198de40ed31SNimrod Andy 		} else {
4199de40ed31SNimrod Andy 			pinctrl_pm_select_default_state(&fep->pdev->dev);
4200de40ed31SNimrod Andy 		}
4201ef83337dSRussell King 		fec_restart(ndev);
420231a6de34SRussell King 		netif_tx_lock_bh(ndev);
4203793fc096SFrank Li 		netif_device_attach(ndev);
42046af42d42SRussell King 		netif_tx_unlock_bh(ndev);
42056af42d42SRussell King 		napi_enable(&fep->napi);
4206557d5dc8SHeiner Kallweit 		phy_init_hw(ndev->phydev);
420745f5c327SPhilippe Reynes 		phy_start(ndev->phydev);
4208793fc096SFrank Li 	}
4209da1774e5SRussell King 	rtnl_unlock();
4210793fc096SFrank Li 
4211793fc096SFrank Li 	return 0;
421213a097bdSFabio Estevam 
4213e8fcfcd5SNimrod Andy failed_clk:
421413a097bdSFabio Estevam 	if (fep->reg_phy)
421513a097bdSFabio Estevam 		regulator_disable(fep->reg_phy);
421613a097bdSFabio Estevam 	return ret;
4217793fc096SFrank Li }
4218793fc096SFrank Li 
42198fff755eSAndrew Lunn static int __maybe_unused fec_runtime_suspend(struct device *dev)
42208fff755eSAndrew Lunn {
42218fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
42228fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
42238fff755eSAndrew Lunn 
4224d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
42258fff755eSAndrew Lunn 	clk_disable_unprepare(fep->clk_ipg);
42268fff755eSAndrew Lunn 
42278fff755eSAndrew Lunn 	return 0;
42288fff755eSAndrew Lunn }
42298fff755eSAndrew Lunn 
42308fff755eSAndrew Lunn static int __maybe_unused fec_runtime_resume(struct device *dev)
42318fff755eSAndrew Lunn {
42328fff755eSAndrew Lunn 	struct net_device *ndev = dev_get_drvdata(dev);
42338fff755eSAndrew Lunn 	struct fec_enet_private *fep = netdev_priv(ndev);
4234d7c3a206SAndy Duan 	int ret;
42358fff755eSAndrew Lunn 
4236d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ahb);
4237d7c3a206SAndy Duan 	if (ret)
4238d7c3a206SAndy Duan 		return ret;
4239d7c3a206SAndy Duan 	ret = clk_prepare_enable(fep->clk_ipg);
4240d7c3a206SAndy Duan 	if (ret)
4241d7c3a206SAndy Duan 		goto failed_clk_ipg;
4242d7c3a206SAndy Duan 
4243d7c3a206SAndy Duan 	return 0;
4244d7c3a206SAndy Duan 
4245d7c3a206SAndy Duan failed_clk_ipg:
4246d7c3a206SAndy Duan 	clk_disable_unprepare(fep->clk_ahb);
4247d7c3a206SAndy Duan 	return ret;
42488fff755eSAndrew Lunn }
42498fff755eSAndrew Lunn 
42508fff755eSAndrew Lunn static const struct dev_pm_ops fec_pm_ops = {
42518fff755eSAndrew Lunn 	SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
42528fff755eSAndrew Lunn 	SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
42538fff755eSAndrew Lunn };
4254793fc096SFrank Li 
4255793fc096SFrank Li static struct platform_driver fec_driver = {
4256793fc096SFrank Li 	.driver	= {
4257793fc096SFrank Li 		.name	= DRIVER_NAME,
4258793fc096SFrank Li 		.pm	= &fec_pm_ops,
4259793fc096SFrank Li 		.of_match_table = fec_dt_ids,
4260272bb0e9SFabio Estevam 		.suppress_bind_attrs = true,
4261793fc096SFrank Li 	},
4262793fc096SFrank Li 	.id_table = fec_devtype,
4263793fc096SFrank Li 	.probe	= fec_probe,
4264793fc096SFrank Li 	.remove	= fec_drv_remove,
4265793fc096SFrank Li };
4266793fc096SFrank Li 
4267793fc096SFrank Li module_platform_driver(fec_driver);
4268793fc096SFrank Li 
4269793fc096SFrank Li MODULE_LICENSE("GPL");
4270