xref: /linux/drivers/net/ethernet/ti/cpsw.c (revision 00fe47120502076e8a377ba259c10a30ba21d777)
1df828598SMugunthan V N /*
2df828598SMugunthan V N  * Texas Instruments Ethernet Switch Driver
3df828598SMugunthan V N  *
4df828598SMugunthan V N  * Copyright (C) 2012 Texas Instruments
5df828598SMugunthan V N  *
6df828598SMugunthan V N  * This program is free software; you can redistribute it and/or
7df828598SMugunthan V N  * modify it under the terms of the GNU General Public License as
8df828598SMugunthan V N  * published by the Free Software Foundation version 2.
9df828598SMugunthan V N  *
10df828598SMugunthan V N  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11df828598SMugunthan V N  * kind, whether express or implied; without even the implied warranty
12df828598SMugunthan V N  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13df828598SMugunthan V N  * GNU General Public License for more details.
14df828598SMugunthan V N  */
15df828598SMugunthan V N 
16df828598SMugunthan V N #include <linux/kernel.h>
17df828598SMugunthan V N #include <linux/io.h>
18df828598SMugunthan V N #include <linux/clk.h>
19df828598SMugunthan V N #include <linux/timer.h>
20df828598SMugunthan V N #include <linux/module.h>
21df828598SMugunthan V N #include <linux/platform_device.h>
22df828598SMugunthan V N #include <linux/irqreturn.h>
23df828598SMugunthan V N #include <linux/interrupt.h>
24df828598SMugunthan V N #include <linux/if_ether.h>
25df828598SMugunthan V N #include <linux/etherdevice.h>
26df828598SMugunthan V N #include <linux/netdevice.h>
272e5b38abSRichard Cochran #include <linux/net_tstamp.h>
28df828598SMugunthan V N #include <linux/phy.h>
29df828598SMugunthan V N #include <linux/workqueue.h>
30df828598SMugunthan V N #include <linux/delay.h>
31f150bd7fSMugunthan V N #include <linux/pm_runtime.h>
32e2b3e493SArnd Bergmann #include <linux/gpio/consumer.h>
332eb32b0aSMugunthan V N #include <linux/of.h>
349e42f715SHeiko Schocher #include <linux/of_mdio.h>
352eb32b0aSMugunthan V N #include <linux/of_net.h>
362eb32b0aSMugunthan V N #include <linux/of_device.h>
373b72c2feSMugunthan V N #include <linux/if_vlan.h>
38514c6032SRandy Dunlap #include <linux/kmemleak.h>
399611d6d6SIvan Khoronzhuk #include <linux/sys_soc.h>
40df828598SMugunthan V N 
41739683b4SMugunthan V N #include <linux/pinctrl/consumer.h>
427929a668SIvan Khoronzhuk #include <net/pkt_cls.h>
43df828598SMugunthan V N 
44dbe34724SMugunthan V N #include "cpsw.h"
45df828598SMugunthan V N #include "cpsw_ale.h"
462e5b38abSRichard Cochran #include "cpts.h"
47df828598SMugunthan V N #include "davinci_cpdma.h"
48df828598SMugunthan V N 
4957d90148SIvan Khoronzhuk #include <net/pkt_sched.h>
5057d90148SIvan Khoronzhuk 
51df828598SMugunthan V N #define CPSW_DEBUG	(NETIF_MSG_HW		| NETIF_MSG_WOL		| \
52df828598SMugunthan V N 			 NETIF_MSG_DRV		| NETIF_MSG_LINK	| \
53df828598SMugunthan V N 			 NETIF_MSG_IFUP		| NETIF_MSG_INTR	| \
54df828598SMugunthan V N 			 NETIF_MSG_PROBE	| NETIF_MSG_TIMER	| \
55df828598SMugunthan V N 			 NETIF_MSG_IFDOWN	| NETIF_MSG_RX_ERR	| \
56df828598SMugunthan V N 			 NETIF_MSG_TX_ERR	| NETIF_MSG_TX_DONE	| \
57df828598SMugunthan V N 			 NETIF_MSG_PKTDATA	| NETIF_MSG_TX_QUEUED	| \
58df828598SMugunthan V N 			 NETIF_MSG_RX_STATUS)
59df828598SMugunthan V N 
60df828598SMugunthan V N #define cpsw_info(priv, type, format, ...)		\
61df828598SMugunthan V N do {								\
62df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
63df828598SMugunthan V N 		dev_info(priv->dev, format, ## __VA_ARGS__);	\
64df828598SMugunthan V N } while (0)
65df828598SMugunthan V N 
66df828598SMugunthan V N #define cpsw_err(priv, type, format, ...)		\
67df828598SMugunthan V N do {								\
68df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
69df828598SMugunthan V N 		dev_err(priv->dev, format, ## __VA_ARGS__);	\
70df828598SMugunthan V N } while (0)
71df828598SMugunthan V N 
72df828598SMugunthan V N #define cpsw_dbg(priv, type, format, ...)		\
73df828598SMugunthan V N do {								\
74df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
75df828598SMugunthan V N 		dev_dbg(priv->dev, format, ## __VA_ARGS__);	\
76df828598SMugunthan V N } while (0)
77df828598SMugunthan V N 
78df828598SMugunthan V N #define cpsw_notice(priv, type, format, ...)		\
79df828598SMugunthan V N do {								\
80df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
81df828598SMugunthan V N 		dev_notice(priv->dev, format, ## __VA_ARGS__);	\
82df828598SMugunthan V N } while (0)
83df828598SMugunthan V N 
845c50a856SMugunthan V N #define ALE_ALL_PORTS		0x7
855c50a856SMugunthan V N 
86df828598SMugunthan V N #define CPSW_MAJOR_VERSION(reg)		(reg >> 8 & 0x7)
87df828598SMugunthan V N #define CPSW_MINOR_VERSION(reg)		(reg & 0xff)
88df828598SMugunthan V N #define CPSW_RTL_VERSION(reg)		((reg >> 11) & 0x1f)
89df828598SMugunthan V N 
90e90cfac6SRichard Cochran #define CPSW_VERSION_1		0x19010a
91e90cfac6SRichard Cochran #define CPSW_VERSION_2		0x19010c
92c193f365SMugunthan V N #define CPSW_VERSION_3		0x19010f
93926489beSMugunthan V N #define CPSW_VERSION_4		0x190112
94549985eeSRichard Cochran 
95549985eeSRichard Cochran #define HOST_PORT_NUM		0
96c6395f12SGrygorii Strashko #define CPSW_ALE_PORTS_NUM	3
97549985eeSRichard Cochran #define SLIVER_SIZE		0x40
98549985eeSRichard Cochran 
99549985eeSRichard Cochran #define CPSW1_HOST_PORT_OFFSET	0x028
100549985eeSRichard Cochran #define CPSW1_SLAVE_OFFSET	0x050
101549985eeSRichard Cochran #define CPSW1_SLAVE_SIZE	0x040
102549985eeSRichard Cochran #define CPSW1_CPDMA_OFFSET	0x100
103549985eeSRichard Cochran #define CPSW1_STATERAM_OFFSET	0x200
104d9718546SMugunthan V N #define CPSW1_HW_STATS		0x400
105549985eeSRichard Cochran #define CPSW1_CPTS_OFFSET	0x500
106549985eeSRichard Cochran #define CPSW1_ALE_OFFSET	0x600
107549985eeSRichard Cochran #define CPSW1_SLIVER_OFFSET	0x700
108549985eeSRichard Cochran 
109549985eeSRichard Cochran #define CPSW2_HOST_PORT_OFFSET	0x108
110549985eeSRichard Cochran #define CPSW2_SLAVE_OFFSET	0x200
111549985eeSRichard Cochran #define CPSW2_SLAVE_SIZE	0x100
112549985eeSRichard Cochran #define CPSW2_CPDMA_OFFSET	0x800
113d9718546SMugunthan V N #define CPSW2_HW_STATS		0x900
114549985eeSRichard Cochran #define CPSW2_STATERAM_OFFSET	0xa00
115549985eeSRichard Cochran #define CPSW2_CPTS_OFFSET	0xc00
116549985eeSRichard Cochran #define CPSW2_ALE_OFFSET	0xd00
117549985eeSRichard Cochran #define CPSW2_SLIVER_OFFSET	0xd80
118549985eeSRichard Cochran #define CPSW2_BD_OFFSET		0x2000
119549985eeSRichard Cochran 
120df828598SMugunthan V N #define CPDMA_RXTHRESH		0x0c0
121df828598SMugunthan V N #define CPDMA_RXFREE		0x0e0
122df828598SMugunthan V N #define CPDMA_TXHDP		0x00
123df828598SMugunthan V N #define CPDMA_RXHDP		0x20
124df828598SMugunthan V N #define CPDMA_TXCP		0x40
125df828598SMugunthan V N #define CPDMA_RXCP		0x60
126df828598SMugunthan V N 
127df828598SMugunthan V N #define CPSW_POLL_WEIGHT	64
128a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_SIZE		4
1299421c901SGrygorii Strashko #define CPSW_MIN_PACKET_SIZE	(VLAN_ETH_ZLEN)
130a3a41d2fSGrygorii Strashko #define CPSW_MAX_PACKET_SIZE	(VLAN_ETH_FRAME_LEN +\
131a3a41d2fSGrygorii Strashko 				 ETH_FCS_LEN +\
132a3a41d2fSGrygorii Strashko 				 CPSW_RX_VLAN_ENCAP_HDR_SIZE)
133df828598SMugunthan V N 
134df828598SMugunthan V N #define RX_PRIORITY_MAPPING	0x76543210
135df828598SMugunthan V N #define TX_PRIORITY_MAPPING	0x33221100
1365e391dc5SIvan Khoronzhuk #define CPDMA_TX_PRIORITY_MAP	0x76543210
137df828598SMugunthan V N 
1383b72c2feSMugunthan V N #define CPSW_VLAN_AWARE		BIT(1)
139a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP	BIT(2)
1403b72c2feSMugunthan V N #define CPSW_ALE_VLAN_AWARE	1
1413b72c2feSMugunthan V N 
14235717d8dSJohn Ogness #define CPSW_FIFO_NORMAL_MODE		(0 << 16)
14335717d8dSJohn Ogness #define CPSW_FIFO_DUAL_MAC_MODE		(1 << 16)
14435717d8dSJohn Ogness #define CPSW_FIFO_RATE_LIMIT_MODE	(2 << 16)
145d9ba8f9eSMugunthan V N 
146ff5b8ef2SMugunthan V N #define CPSW_INTPACEEN		(0x3f << 16)
147ff5b8ef2SMugunthan V N #define CPSW_INTPRESCALE_MASK	(0x7FF << 0)
148ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_CNT	63
149ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_CNT	2
150ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_INTVL	(1000 / CPSW_CMINTMIN_CNT)
151ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_INTVL	((1000 / CPSW_CMINTMAX_CNT) + 1)
152ff5b8ef2SMugunthan V N 
153606f3993SIvan Khoronzhuk #define cpsw_slave_index(cpsw, priv)				\
154606f3993SIvan Khoronzhuk 		((cpsw->data.dual_emac) ? priv->emac_port :	\
155606f3993SIvan Khoronzhuk 		cpsw->data.active_slave)
156e38b5a3dSIvan Khoronzhuk #define IRQ_NUM			2
157e05107e6SIvan Khoronzhuk #define CPSW_MAX_QUEUES		8
15890225bf0SGrygorii Strashko #define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
15957d90148SIvan Khoronzhuk #define CPSW_FIFO_QUEUE_TYPE_SHIFT	16
16057d90148SIvan Khoronzhuk #define CPSW_FIFO_SHAPE_EN_SHIFT	16
16157d90148SIvan Khoronzhuk #define CPSW_FIFO_RATE_EN_SHIFT		20
1627929a668SIvan Khoronzhuk #define CPSW_TC_NUM			4
1637929a668SIvan Khoronzhuk #define CPSW_FIFO_SHAPERS_NUM		(CPSW_TC_NUM - 1)
16457d90148SIvan Khoronzhuk #define CPSW_PCT_MASK			0x7f
165d3bb9c58SMugunthan V N 
166a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT	29
167a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK		GENMASK(2, 0)
168a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT	16
169a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT	8
170a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK	GENMASK(1, 0)
171a3a41d2fSGrygorii Strashko enum {
172a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0,
173a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV,
174a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG,
175a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG,
176a3a41d2fSGrygorii Strashko };
177a3a41d2fSGrygorii Strashko 
178df828598SMugunthan V N static int debug_level;
179df828598SMugunthan V N module_param(debug_level, int, 0);
180df828598SMugunthan V N MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
181df828598SMugunthan V N 
182df828598SMugunthan V N static int ale_ageout = 10;
183df828598SMugunthan V N module_param(ale_ageout, int, 0);
184df828598SMugunthan V N MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
185df828598SMugunthan V N 
186df828598SMugunthan V N static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
187df828598SMugunthan V N module_param(rx_packet_max, int, 0);
188df828598SMugunthan V N MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
189df828598SMugunthan V N 
19090225bf0SGrygorii Strashko static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
19190225bf0SGrygorii Strashko module_param(descs_pool_size, int, 0444);
19290225bf0SGrygorii Strashko MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
19390225bf0SGrygorii Strashko 
194996a5c27SRichard Cochran struct cpsw_wr_regs {
195df828598SMugunthan V N 	u32	id_ver;
196df828598SMugunthan V N 	u32	soft_reset;
197df828598SMugunthan V N 	u32	control;
198df828598SMugunthan V N 	u32	int_control;
199df828598SMugunthan V N 	u32	rx_thresh_en;
200df828598SMugunthan V N 	u32	rx_en;
201df828598SMugunthan V N 	u32	tx_en;
202df828598SMugunthan V N 	u32	misc_en;
203ff5b8ef2SMugunthan V N 	u32	mem_allign1[8];
204ff5b8ef2SMugunthan V N 	u32	rx_thresh_stat;
205ff5b8ef2SMugunthan V N 	u32	rx_stat;
206ff5b8ef2SMugunthan V N 	u32	tx_stat;
207ff5b8ef2SMugunthan V N 	u32	misc_stat;
208ff5b8ef2SMugunthan V N 	u32	mem_allign2[8];
209ff5b8ef2SMugunthan V N 	u32	rx_imax;
210ff5b8ef2SMugunthan V N 	u32	tx_imax;
211ff5b8ef2SMugunthan V N 
212df828598SMugunthan V N };
213df828598SMugunthan V N 
214996a5c27SRichard Cochran struct cpsw_ss_regs {
215df828598SMugunthan V N 	u32	id_ver;
216df828598SMugunthan V N 	u32	control;
217df828598SMugunthan V N 	u32	soft_reset;
218df828598SMugunthan V N 	u32	stat_port_en;
219df828598SMugunthan V N 	u32	ptype;
220bd357af2SRichard Cochran 	u32	soft_idle;
221bd357af2SRichard Cochran 	u32	thru_rate;
222bd357af2SRichard Cochran 	u32	gap_thresh;
223bd357af2SRichard Cochran 	u32	tx_start_wds;
224bd357af2SRichard Cochran 	u32	flow_control;
225bd357af2SRichard Cochran 	u32	vlan_ltype;
226bd357af2SRichard Cochran 	u32	ts_ltype;
227bd357af2SRichard Cochran 	u32	dlr_ltype;
228df828598SMugunthan V N };
229df828598SMugunthan V N 
2309750a3adSRichard Cochran /* CPSW_PORT_V1 */
2319750a3adSRichard Cochran #define CPSW1_MAX_BLKS      0x00 /* Maximum FIFO Blocks */
2329750a3adSRichard Cochran #define CPSW1_BLK_CNT       0x04 /* FIFO Block Usage Count (Read Only) */
2339750a3adSRichard Cochran #define CPSW1_TX_IN_CTL     0x08 /* Transmit FIFO Control */
2349750a3adSRichard Cochran #define CPSW1_PORT_VLAN     0x0c /* VLAN Register */
2359750a3adSRichard Cochran #define CPSW1_TX_PRI_MAP    0x10 /* Tx Header Priority to Switch Pri Mapping */
2369750a3adSRichard Cochran #define CPSW1_TS_CTL        0x14 /* Time Sync Control */
2379750a3adSRichard Cochran #define CPSW1_TS_SEQ_LTYPE  0x18 /* Time Sync Sequence ID Offset and Msg Type */
2389750a3adSRichard Cochran #define CPSW1_TS_VLAN       0x1c /* Time Sync VLAN1 and VLAN2 */
2399750a3adSRichard Cochran 
2409750a3adSRichard Cochran /* CPSW_PORT_V2 */
2419750a3adSRichard Cochran #define CPSW2_CONTROL       0x00 /* Control Register */
2429750a3adSRichard Cochran #define CPSW2_MAX_BLKS      0x08 /* Maximum FIFO Blocks */
2439750a3adSRichard Cochran #define CPSW2_BLK_CNT       0x0c /* FIFO Block Usage Count (Read Only) */
2449750a3adSRichard Cochran #define CPSW2_TX_IN_CTL     0x10 /* Transmit FIFO Control */
2459750a3adSRichard Cochran #define CPSW2_PORT_VLAN     0x14 /* VLAN Register */
2469750a3adSRichard Cochran #define CPSW2_TX_PRI_MAP    0x18 /* Tx Header Priority to Switch Pri Mapping */
2479750a3adSRichard Cochran #define CPSW2_TS_SEQ_MTYPE  0x1c /* Time Sync Sequence ID Offset and Msg Type */
2489750a3adSRichard Cochran 
2499750a3adSRichard Cochran /* CPSW_PORT_V1 and V2 */
2509750a3adSRichard Cochran #define SA_LO               0x20 /* CPGMAC_SL Source Address Low */
2519750a3adSRichard Cochran #define SA_HI               0x24 /* CPGMAC_SL Source Address High */
2529750a3adSRichard Cochran #define SEND_PERCENT        0x28 /* Transmit Queue Send Percentages */
2539750a3adSRichard Cochran 
2549750a3adSRichard Cochran /* CPSW_PORT_V2 only */
2559750a3adSRichard Cochran #define RX_DSCP_PRI_MAP0    0x30 /* Rx DSCP Priority to Rx Packet Mapping */
2569750a3adSRichard Cochran #define RX_DSCP_PRI_MAP1    0x34 /* Rx DSCP Priority to Rx Packet Mapping */
2579750a3adSRichard Cochran #define RX_DSCP_PRI_MAP2    0x38 /* Rx DSCP Priority to Rx Packet Mapping */
2589750a3adSRichard Cochran #define RX_DSCP_PRI_MAP3    0x3c /* Rx DSCP Priority to Rx Packet Mapping */
2599750a3adSRichard Cochran #define RX_DSCP_PRI_MAP4    0x40 /* Rx DSCP Priority to Rx Packet Mapping */
2609750a3adSRichard Cochran #define RX_DSCP_PRI_MAP5    0x44 /* Rx DSCP Priority to Rx Packet Mapping */
2619750a3adSRichard Cochran #define RX_DSCP_PRI_MAP6    0x48 /* Rx DSCP Priority to Rx Packet Mapping */
2629750a3adSRichard Cochran #define RX_DSCP_PRI_MAP7    0x4c /* Rx DSCP Priority to Rx Packet Mapping */
2639750a3adSRichard Cochran 
2649750a3adSRichard Cochran /* Bit definitions for the CPSW2_CONTROL register */
2651239a96aSIvan Khoronzhuk #define PASS_PRI_TAGGED     BIT(24) /* Pass Priority Tagged */
2661239a96aSIvan Khoronzhuk #define VLAN_LTYPE2_EN      BIT(21) /* VLAN LTYPE 2 enable */
2671239a96aSIvan Khoronzhuk #define VLAN_LTYPE1_EN      BIT(20) /* VLAN LTYPE 1 enable */
2681239a96aSIvan Khoronzhuk #define DSCP_PRI_EN         BIT(16) /* DSCP Priority Enable */
2691c0e8123SIvan Khoronzhuk #define TS_107              BIT(15) /* Tyme Sync Dest IP Address 107 */
2701239a96aSIvan Khoronzhuk #define TS_320              BIT(14) /* Time Sync Dest Port 320 enable */
2711239a96aSIvan Khoronzhuk #define TS_319              BIT(13) /* Time Sync Dest Port 319 enable */
2721239a96aSIvan Khoronzhuk #define TS_132              BIT(12) /* Time Sync Dest IP Addr 132 enable */
2731239a96aSIvan Khoronzhuk #define TS_131              BIT(11) /* Time Sync Dest IP Addr 131 enable */
2741239a96aSIvan Khoronzhuk #define TS_130              BIT(10) /* Time Sync Dest IP Addr 130 enable */
2751239a96aSIvan Khoronzhuk #define TS_129              BIT(9)  /* Time Sync Dest IP Addr 129 enable */
2761239a96aSIvan Khoronzhuk #define TS_TTL_NONZERO      BIT(8)  /* Time Sync Time To Live Non-zero enable */
2771239a96aSIvan Khoronzhuk #define TS_ANNEX_F_EN       BIT(6)  /* Time Sync Annex F enable */
2781239a96aSIvan Khoronzhuk #define TS_ANNEX_D_EN       BIT(4)  /* Time Sync Annex D enable */
2791239a96aSIvan Khoronzhuk #define TS_LTYPE2_EN        BIT(3)  /* Time Sync LTYPE 2 enable */
2801239a96aSIvan Khoronzhuk #define TS_LTYPE1_EN        BIT(2)  /* Time Sync LTYPE 1 enable */
2811239a96aSIvan Khoronzhuk #define TS_TX_EN            BIT(1)  /* Time Sync Transmit Enable */
2821239a96aSIvan Khoronzhuk #define TS_RX_EN            BIT(0)  /* Time Sync Receive Enable */
2839750a3adSRichard Cochran 
28409c55372SGeorge Cherian #define CTRL_V2_TS_BITS \
28509c55372SGeorge Cherian 	(TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
28609c55372SGeorge Cherian 	 TS_TTL_NONZERO  | TS_ANNEX_D_EN | TS_LTYPE1_EN)
2879750a3adSRichard Cochran 
28809c55372SGeorge Cherian #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
28909c55372SGeorge Cherian #define CTRL_V2_TX_TS_BITS  (CTRL_V2_TS_BITS | TS_TX_EN)
29009c55372SGeorge Cherian #define CTRL_V2_RX_TS_BITS  (CTRL_V2_TS_BITS | TS_RX_EN)
29109c55372SGeorge Cherian 
29209c55372SGeorge Cherian 
29309c55372SGeorge Cherian #define CTRL_V3_TS_BITS \
2941c0e8123SIvan Khoronzhuk 	(TS_107 | TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
29509c55372SGeorge Cherian 	 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
29609c55372SGeorge Cherian 	 TS_LTYPE1_EN)
29709c55372SGeorge Cherian 
29809c55372SGeorge Cherian #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
29909c55372SGeorge Cherian #define CTRL_V3_TX_TS_BITS  (CTRL_V3_TS_BITS | TS_TX_EN)
30009c55372SGeorge Cherian #define CTRL_V3_RX_TS_BITS  (CTRL_V3_TS_BITS | TS_RX_EN)
3019750a3adSRichard Cochran 
3029750a3adSRichard Cochran /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
3039750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_SHIFT   (16)    /* Time Sync Sequence ID Offset */
3049750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_MASK    (0x3f)
3059750a3adSRichard Cochran #define TS_MSG_TYPE_EN_SHIFT     (0)     /* Time Sync Message Type Enable */
3069750a3adSRichard Cochran #define TS_MSG_TYPE_EN_MASK      (0xffff)
3079750a3adSRichard Cochran 
3089750a3adSRichard Cochran /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
3099750a3adSRichard Cochran #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
310df828598SMugunthan V N 
3112e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_CTL register */
3122e5b38abSRichard Cochran #define CPSW_V1_TS_RX_EN		BIT(0)
3132e5b38abSRichard Cochran #define CPSW_V1_TS_TX_EN		BIT(4)
3142e5b38abSRichard Cochran #define CPSW_V1_MSG_TYPE_OFS		16
3152e5b38abSRichard Cochran 
3162e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
3172e5b38abSRichard Cochran #define CPSW_V1_SEQ_ID_OFS_SHIFT	16
3182e5b38abSRichard Cochran 
31948f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_TX		15
32048f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_TX_SHIFT		4
32148f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_RX		5
32248f5bcccSGrygorii Strashko 
323df828598SMugunthan V N struct cpsw_host_regs {
324df828598SMugunthan V N 	u32	max_blks;
325df828598SMugunthan V N 	u32	blk_cnt;
326d9ba8f9eSMugunthan V N 	u32	tx_in_ctl;
327df828598SMugunthan V N 	u32	port_vlan;
328df828598SMugunthan V N 	u32	tx_pri_map;
329df828598SMugunthan V N 	u32	cpdma_tx_pri_map;
330df828598SMugunthan V N 	u32	cpdma_rx_chan_map;
331df828598SMugunthan V N };
332df828598SMugunthan V N 
333df828598SMugunthan V N struct cpsw_sliver_regs {
334df828598SMugunthan V N 	u32	id_ver;
335df828598SMugunthan V N 	u32	mac_control;
336df828598SMugunthan V N 	u32	mac_status;
337df828598SMugunthan V N 	u32	soft_reset;
338df828598SMugunthan V N 	u32	rx_maxlen;
339df828598SMugunthan V N 	u32	__reserved_0;
340df828598SMugunthan V N 	u32	rx_pause;
341df828598SMugunthan V N 	u32	tx_pause;
342df828598SMugunthan V N 	u32	__reserved_1;
343df828598SMugunthan V N 	u32	rx_pri_map;
344df828598SMugunthan V N };
345df828598SMugunthan V N 
346d9718546SMugunthan V N struct cpsw_hw_stats {
347d9718546SMugunthan V N 	u32	rxgoodframes;
348d9718546SMugunthan V N 	u32	rxbroadcastframes;
349d9718546SMugunthan V N 	u32	rxmulticastframes;
350d9718546SMugunthan V N 	u32	rxpauseframes;
351d9718546SMugunthan V N 	u32	rxcrcerrors;
352d9718546SMugunthan V N 	u32	rxaligncodeerrors;
353d9718546SMugunthan V N 	u32	rxoversizedframes;
354d9718546SMugunthan V N 	u32	rxjabberframes;
355d9718546SMugunthan V N 	u32	rxundersizedframes;
356d9718546SMugunthan V N 	u32	rxfragments;
357d9718546SMugunthan V N 	u32	__pad_0[2];
358d9718546SMugunthan V N 	u32	rxoctets;
359d9718546SMugunthan V N 	u32	txgoodframes;
360d9718546SMugunthan V N 	u32	txbroadcastframes;
361d9718546SMugunthan V N 	u32	txmulticastframes;
362d9718546SMugunthan V N 	u32	txpauseframes;
363d9718546SMugunthan V N 	u32	txdeferredframes;
364d9718546SMugunthan V N 	u32	txcollisionframes;
365d9718546SMugunthan V N 	u32	txsinglecollframes;
366d9718546SMugunthan V N 	u32	txmultcollframes;
367d9718546SMugunthan V N 	u32	txexcessivecollisions;
368d9718546SMugunthan V N 	u32	txlatecollisions;
369d9718546SMugunthan V N 	u32	txunderrun;
370d9718546SMugunthan V N 	u32	txcarriersenseerrors;
371d9718546SMugunthan V N 	u32	txoctets;
372d9718546SMugunthan V N 	u32	octetframes64;
373d9718546SMugunthan V N 	u32	octetframes65t127;
374d9718546SMugunthan V N 	u32	octetframes128t255;
375d9718546SMugunthan V N 	u32	octetframes256t511;
376d9718546SMugunthan V N 	u32	octetframes512t1023;
377d9718546SMugunthan V N 	u32	octetframes1024tup;
378d9718546SMugunthan V N 	u32	netoctets;
379d9718546SMugunthan V N 	u32	rxsofoverruns;
380d9718546SMugunthan V N 	u32	rxmofoverruns;
381d9718546SMugunthan V N 	u32	rxdmaoverruns;
382d9718546SMugunthan V N };
383d9718546SMugunthan V N 
3842c8a14d6SGrygorii Strashko struct cpsw_slave_data {
3852c8a14d6SGrygorii Strashko 	struct device_node *phy_node;
3862c8a14d6SGrygorii Strashko 	char		phy_id[MII_BUS_ID_SIZE];
3872c8a14d6SGrygorii Strashko 	int		phy_if;
3882c8a14d6SGrygorii Strashko 	u8		mac_addr[ETH_ALEN];
3892c8a14d6SGrygorii Strashko 	u16		dual_emac_res_vlan;	/* Reserved VLAN for DualEMAC */
3902c8a14d6SGrygorii Strashko };
3912c8a14d6SGrygorii Strashko 
3922c8a14d6SGrygorii Strashko struct cpsw_platform_data {
3932c8a14d6SGrygorii Strashko 	struct cpsw_slave_data	*slave_data;
3942c8a14d6SGrygorii Strashko 	u32	ss_reg_ofs;	/* Subsystem control register offset */
3952c8a14d6SGrygorii Strashko 	u32	channels;	/* number of cpdma channels (symmetric) */
3962c8a14d6SGrygorii Strashko 	u32	slaves;		/* number of slave cpgmac ports */
3972c8a14d6SGrygorii Strashko 	u32	active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
3982c8a14d6SGrygorii Strashko 	u32	ale_entries;	/* ale table size */
3992c8a14d6SGrygorii Strashko 	u32	bd_ram_size;  /*buffer descriptor ram size */
4002c8a14d6SGrygorii Strashko 	u32	mac_control;	/* Mac control register */
4012c8a14d6SGrygorii Strashko 	u16	default_vlan;	/* Def VLAN for ALE lookup in VLAN aware mode*/
4022c8a14d6SGrygorii Strashko 	bool	dual_emac;	/* Enable Dual EMAC mode */
4032c8a14d6SGrygorii Strashko };
4042c8a14d6SGrygorii Strashko 
405df828598SMugunthan V N struct cpsw_slave {
4069750a3adSRichard Cochran 	void __iomem			*regs;
407df828598SMugunthan V N 	struct cpsw_sliver_regs __iomem	*sliver;
408df828598SMugunthan V N 	int				slave_num;
409df828598SMugunthan V N 	u32				mac_control;
410df828598SMugunthan V N 	struct cpsw_slave_data		*data;
411df828598SMugunthan V N 	struct phy_device		*phy;
412d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
413d9ba8f9eSMugunthan V N 	u32				port_vlan;
414df828598SMugunthan V N };
415df828598SMugunthan V N 
4169750a3adSRichard Cochran static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
4179750a3adSRichard Cochran {
418dda5f5feSGrygorii Strashko 	return readl_relaxed(slave->regs + offset);
4199750a3adSRichard Cochran }
4209750a3adSRichard Cochran 
4219750a3adSRichard Cochran static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
4229750a3adSRichard Cochran {
423dda5f5feSGrygorii Strashko 	writel_relaxed(val, slave->regs + offset);
4249750a3adSRichard Cochran }
4259750a3adSRichard Cochran 
4268feb0a19SIvan Khoronzhuk struct cpsw_vector {
4278feb0a19SIvan Khoronzhuk 	struct cpdma_chan *ch;
4288feb0a19SIvan Khoronzhuk 	int budget;
4298feb0a19SIvan Khoronzhuk };
4308feb0a19SIvan Khoronzhuk 
431649a1688SIvan Khoronzhuk struct cpsw_common {
43256e31bd8SIvan Khoronzhuk 	struct device			*dev;
433606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	data;
434dbc4ec52SIvan Khoronzhuk 	struct napi_struct		napi_rx;
435dbc4ec52SIvan Khoronzhuk 	struct napi_struct		napi_tx;
4365d8d0d4dSIvan Khoronzhuk 	struct cpsw_ss_regs __iomem	*regs;
4375d8d0d4dSIvan Khoronzhuk 	struct cpsw_wr_regs __iomem	*wr_regs;
4385d8d0d4dSIvan Khoronzhuk 	u8 __iomem			*hw_stats;
4395d8d0d4dSIvan Khoronzhuk 	struct cpsw_host_regs __iomem	*host_port_regs;
4402a05a622SIvan Khoronzhuk 	u32				version;
4412a05a622SIvan Khoronzhuk 	u32				coal_intvl;
4422a05a622SIvan Khoronzhuk 	u32				bus_freq_mhz;
4432a05a622SIvan Khoronzhuk 	int				rx_packet_max;
444606f3993SIvan Khoronzhuk 	struct cpsw_slave		*slaves;
4452c836bd9SIvan Khoronzhuk 	struct cpdma_ctlr		*dma;
4468feb0a19SIvan Khoronzhuk 	struct cpsw_vector		txv[CPSW_MAX_QUEUES];
4478feb0a19SIvan Khoronzhuk 	struct cpsw_vector		rxv[CPSW_MAX_QUEUES];
4482a05a622SIvan Khoronzhuk 	struct cpsw_ale			*ale;
449e38b5a3dSIvan Khoronzhuk 	bool				quirk_irq;
450e38b5a3dSIvan Khoronzhuk 	bool				rx_irq_disabled;
451e38b5a3dSIvan Khoronzhuk 	bool				tx_irq_disabled;
452e38b5a3dSIvan Khoronzhuk 	u32 irqs_table[IRQ_NUM];
4532a05a622SIvan Khoronzhuk 	struct cpts			*cpts;
454e05107e6SIvan Khoronzhuk 	int				rx_ch_num, tx_ch_num;
4550be01b8eSIvan Khoronzhuk 	int				speed;
456d5bc1613SIvan Khoronzhuk 	int				usage_count;
457649a1688SIvan Khoronzhuk };
458649a1688SIvan Khoronzhuk 
459649a1688SIvan Khoronzhuk struct cpsw_priv {
460df828598SMugunthan V N 	struct net_device		*ndev;
461df828598SMugunthan V N 	struct device			*dev;
462df828598SMugunthan V N 	u32				msg_enable;
463df828598SMugunthan V N 	u8				mac_addr[ETH_ALEN];
4641923d6e4SMugunthan V N 	bool				rx_pause;
4651923d6e4SMugunthan V N 	bool				tx_pause;
4667929a668SIvan Khoronzhuk 	bool				mqprio_hw;
46757d90148SIvan Khoronzhuk 	int				fifo_bw[CPSW_TC_NUM];
46857d90148SIvan Khoronzhuk 	int				shp_cfg_speed;
469d9ba8f9eSMugunthan V N 	u32 emac_port;
470649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw;
471df828598SMugunthan V N };
472df828598SMugunthan V N 
473d9718546SMugunthan V N struct cpsw_stats {
474d9718546SMugunthan V N 	char stat_string[ETH_GSTRING_LEN];
475d9718546SMugunthan V N 	int type;
476d9718546SMugunthan V N 	int sizeof_stat;
477d9718546SMugunthan V N 	int stat_offset;
478d9718546SMugunthan V N };
479d9718546SMugunthan V N 
480d9718546SMugunthan V N enum {
481d9718546SMugunthan V N 	CPSW_STATS,
482d9718546SMugunthan V N 	CPDMA_RX_STATS,
483d9718546SMugunthan V N 	CPDMA_TX_STATS,
484d9718546SMugunthan V N };
485d9718546SMugunthan V N 
486d9718546SMugunthan V N #define CPSW_STAT(m)		CPSW_STATS,				\
487a90546e8Szhong jiang 				FIELD_SIZEOF(struct cpsw_hw_stats, m), \
488d9718546SMugunthan V N 				offsetof(struct cpsw_hw_stats, m)
489d9718546SMugunthan V N #define CPDMA_RX_STAT(m)	CPDMA_RX_STATS,				   \
490a90546e8Szhong jiang 				FIELD_SIZEOF(struct cpdma_chan_stats, m), \
491d9718546SMugunthan V N 				offsetof(struct cpdma_chan_stats, m)
492d9718546SMugunthan V N #define CPDMA_TX_STAT(m)	CPDMA_TX_STATS,				   \
493a90546e8Szhong jiang 				FIELD_SIZEOF(struct cpdma_chan_stats, m), \
494d9718546SMugunthan V N 				offsetof(struct cpdma_chan_stats, m)
495d9718546SMugunthan V N 
496d9718546SMugunthan V N static const struct cpsw_stats cpsw_gstrings_stats[] = {
497d9718546SMugunthan V N 	{ "Good Rx Frames", CPSW_STAT(rxgoodframes) },
498d9718546SMugunthan V N 	{ "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
499d9718546SMugunthan V N 	{ "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
500d9718546SMugunthan V N 	{ "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
501d9718546SMugunthan V N 	{ "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
502d9718546SMugunthan V N 	{ "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
503d9718546SMugunthan V N 	{ "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
504d9718546SMugunthan V N 	{ "Rx Jabbers", CPSW_STAT(rxjabberframes) },
505d9718546SMugunthan V N 	{ "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
506d9718546SMugunthan V N 	{ "Rx Fragments", CPSW_STAT(rxfragments) },
507d9718546SMugunthan V N 	{ "Rx Octets", CPSW_STAT(rxoctets) },
508d9718546SMugunthan V N 	{ "Good Tx Frames", CPSW_STAT(txgoodframes) },
509d9718546SMugunthan V N 	{ "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
510d9718546SMugunthan V N 	{ "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
511d9718546SMugunthan V N 	{ "Pause Tx Frames", CPSW_STAT(txpauseframes) },
512d9718546SMugunthan V N 	{ "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
513d9718546SMugunthan V N 	{ "Collisions", CPSW_STAT(txcollisionframes) },
514d9718546SMugunthan V N 	{ "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
515d9718546SMugunthan V N 	{ "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
516d9718546SMugunthan V N 	{ "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
517d9718546SMugunthan V N 	{ "Late Collisions", CPSW_STAT(txlatecollisions) },
518d9718546SMugunthan V N 	{ "Tx Underrun", CPSW_STAT(txunderrun) },
519d9718546SMugunthan V N 	{ "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
520d9718546SMugunthan V N 	{ "Tx Octets", CPSW_STAT(txoctets) },
521d9718546SMugunthan V N 	{ "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
522d9718546SMugunthan V N 	{ "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
523d9718546SMugunthan V N 	{ "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
524d9718546SMugunthan V N 	{ "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
525d9718546SMugunthan V N 	{ "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
526d9718546SMugunthan V N 	{ "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
527d9718546SMugunthan V N 	{ "Net Octets", CPSW_STAT(netoctets) },
528d9718546SMugunthan V N 	{ "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
529d9718546SMugunthan V N 	{ "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
530d9718546SMugunthan V N 	{ "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
531d9718546SMugunthan V N };
532d9718546SMugunthan V N 
533e05107e6SIvan Khoronzhuk static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
534e05107e6SIvan Khoronzhuk 	{ "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
535e05107e6SIvan Khoronzhuk 	{ "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
536e05107e6SIvan Khoronzhuk 	{ "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
537e05107e6SIvan Khoronzhuk 	{ "misqueued", CPDMA_RX_STAT(misqueued) },
538e05107e6SIvan Khoronzhuk 	{ "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
539e05107e6SIvan Khoronzhuk 	{ "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
540e05107e6SIvan Khoronzhuk 	{ "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
541e05107e6SIvan Khoronzhuk 	{ "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
542e05107e6SIvan Khoronzhuk 	{ "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
543e05107e6SIvan Khoronzhuk 	{ "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
544e05107e6SIvan Khoronzhuk 	{ "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
545e05107e6SIvan Khoronzhuk 	{ "requeue", CPDMA_RX_STAT(requeue) },
546e05107e6SIvan Khoronzhuk 	{ "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
547e05107e6SIvan Khoronzhuk };
548e05107e6SIvan Khoronzhuk 
549e05107e6SIvan Khoronzhuk #define CPSW_STATS_COMMON_LEN	ARRAY_SIZE(cpsw_gstrings_stats)
550e05107e6SIvan Khoronzhuk #define CPSW_STATS_CH_LEN	ARRAY_SIZE(cpsw_gstrings_ch_stats)
551d9718546SMugunthan V N 
552649a1688SIvan Khoronzhuk #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
553dbc4ec52SIvan Khoronzhuk #define napi_to_cpsw(napi)	container_of(napi, struct cpsw_common, napi)
554df828598SMugunthan V N #define for_each_slave(priv, func, arg...)				\
555df828598SMugunthan V N 	do {								\
5566e6ceaedSSebastian Siewior 		struct cpsw_slave *slave;				\
557606f3993SIvan Khoronzhuk 		struct cpsw_common *cpsw = (priv)->cpsw;		\
5586e6ceaedSSebastian Siewior 		int n;							\
559606f3993SIvan Khoronzhuk 		if (cpsw->data.dual_emac)				\
560606f3993SIvan Khoronzhuk 			(func)((cpsw)->slaves + priv->emac_port, ##arg);\
561d9ba8f9eSMugunthan V N 		else							\
562606f3993SIvan Khoronzhuk 			for (n = cpsw->data.slaves,			\
563606f3993SIvan Khoronzhuk 					slave = cpsw->slaves;		\
5646e6ceaedSSebastian Siewior 					n; n--)				\
5656e6ceaedSSebastian Siewior 				(func)(slave++, ##arg);			\
566df828598SMugunthan V N 	} while (0)
567d9ba8f9eSMugunthan V N 
568*00fe4712SIvan Khoronzhuk static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
569*00fe4712SIvan Khoronzhuk 				    __be16 proto, u16 vid);
570*00fe4712SIvan Khoronzhuk 
5716f1f5836SIvan Khoronzhuk static inline int cpsw_get_slave_port(u32 slave_num)
572d9ba8f9eSMugunthan V N {
573d9ba8f9eSMugunthan V N 	return slave_num + 1;
574d9ba8f9eSMugunthan V N }
575df828598SMugunthan V N 
5760cd8f9ccSMugunthan V N static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
5770cd8f9ccSMugunthan V N {
5782a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
5792a05a622SIvan Khoronzhuk 	struct cpsw_ale *ale = cpsw->ale;
5800cd8f9ccSMugunthan V N 	int i;
5810cd8f9ccSMugunthan V N 
582606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
5830cd8f9ccSMugunthan V N 		bool flag = false;
5840cd8f9ccSMugunthan V N 
5850cd8f9ccSMugunthan V N 		/* Enabling promiscuous mode for one interface will be
5860cd8f9ccSMugunthan V N 		 * common for both the interface as the interface shares
5870cd8f9ccSMugunthan V N 		 * the same hardware resource.
5880cd8f9ccSMugunthan V N 		 */
589606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++)
590606f3993SIvan Khoronzhuk 			if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
5910cd8f9ccSMugunthan V N 				flag = true;
5920cd8f9ccSMugunthan V N 
5930cd8f9ccSMugunthan V N 		if (!enable && flag) {
5940cd8f9ccSMugunthan V N 			enable = true;
5950cd8f9ccSMugunthan V N 			dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
5960cd8f9ccSMugunthan V N 		}
5970cd8f9ccSMugunthan V N 
5980cd8f9ccSMugunthan V N 		if (enable) {
5990cd8f9ccSMugunthan V N 			/* Enable Bypass */
6000cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
6010cd8f9ccSMugunthan V N 
6020cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
6030cd8f9ccSMugunthan V N 		} else {
6040cd8f9ccSMugunthan V N 			/* Disable Bypass */
6050cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
6060cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
6070cd8f9ccSMugunthan V N 		}
6080cd8f9ccSMugunthan V N 	} else {
6090cd8f9ccSMugunthan V N 		if (enable) {
6100cd8f9ccSMugunthan V N 			unsigned long timeout = jiffies + HZ;
6110cd8f9ccSMugunthan V N 
6126f979eb3SLennart Sorensen 			/* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
613606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
6140cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6150cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 1);
6160cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6170cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 1);
6180cd8f9ccSMugunthan V N 			}
6190cd8f9ccSMugunthan V N 
6200cd8f9ccSMugunthan V N 			/* Clear All Untouched entries */
6210cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
6220cd8f9ccSMugunthan V N 			do {
6230cd8f9ccSMugunthan V N 				cpu_relax();
6240cd8f9ccSMugunthan V N 				if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
6250cd8f9ccSMugunthan V N 					break;
6260cd8f9ccSMugunthan V N 			} while (time_after(timeout, jiffies));
6270cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
6280cd8f9ccSMugunthan V N 
6290cd8f9ccSMugunthan V N 			/* Clear all mcast from ALE */
63061f1cef9SGrygorii Strashko 			cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
63115180ecaSIvan Khoronzhuk 			__hw_addr_ref_unsync_dev(&ndev->mc, ndev, NULL);
6320cd8f9ccSMugunthan V N 
6330cd8f9ccSMugunthan V N 			/* Flood All Unicast Packets to Host port */
6340cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
6350cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
6360cd8f9ccSMugunthan V N 		} else {
6376f979eb3SLennart Sorensen 			/* Don't Flood All Unicast Packets to Host port */
6380cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
6390cd8f9ccSMugunthan V N 
6406f979eb3SLennart Sorensen 			/* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
641606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
6420cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6430cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 0);
6440cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6450cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 0);
6460cd8f9ccSMugunthan V N 			}
6470cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
6480cd8f9ccSMugunthan V N 		}
6490cd8f9ccSMugunthan V N 	}
6500cd8f9ccSMugunthan V N }
6510cd8f9ccSMugunthan V N 
65215180ecaSIvan Khoronzhuk struct addr_sync_ctx {
65315180ecaSIvan Khoronzhuk 	struct net_device *ndev;
65415180ecaSIvan Khoronzhuk 	const u8 *addr;		/* address to be synched */
65515180ecaSIvan Khoronzhuk 	int consumed;		/* number of address instances */
65615180ecaSIvan Khoronzhuk 	int flush;		/* flush flag */
65715180ecaSIvan Khoronzhuk };
6585da19489SIvan Khoronzhuk 
65915180ecaSIvan Khoronzhuk /**
66015180ecaSIvan Khoronzhuk  * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes
66115180ecaSIvan Khoronzhuk  * if it's not deleted
66215180ecaSIvan Khoronzhuk  * @ndev: device to sync
66315180ecaSIvan Khoronzhuk  * @addr: address to be added or deleted
66415180ecaSIvan Khoronzhuk  * @vid: vlan id, if vid < 0 set/unset address for real device
66515180ecaSIvan Khoronzhuk  * @add: add address if the flag is set or remove otherwise
66615180ecaSIvan Khoronzhuk  */
66715180ecaSIvan Khoronzhuk static int cpsw_set_mc(struct net_device *ndev, const u8 *addr,
66815180ecaSIvan Khoronzhuk 		       int vid, int add)
6695c50a856SMugunthan V N {
6705c50a856SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
671606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
67215180ecaSIvan Khoronzhuk 	int mask, flags, ret;
67325906052SMugunthan V N 
67415180ecaSIvan Khoronzhuk 	if (vid < 0) {
67515180ecaSIvan Khoronzhuk 		if (cpsw->data.dual_emac)
676606f3993SIvan Khoronzhuk 			vid = cpsw->slaves[priv->emac_port].port_vlan;
67715180ecaSIvan Khoronzhuk 		else
6785da19489SIvan Khoronzhuk 			vid = 0;
6795da19489SIvan Khoronzhuk 	}
6805da19489SIvan Khoronzhuk 
68115180ecaSIvan Khoronzhuk 	mask = cpsw->data.dual_emac ? ALE_PORT_HOST : ALE_ALL_PORTS;
68215180ecaSIvan Khoronzhuk 	flags = vid ? ALE_VLAN : 0;
68315180ecaSIvan Khoronzhuk 
68415180ecaSIvan Khoronzhuk 	if (add)
68515180ecaSIvan Khoronzhuk 		ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0);
68615180ecaSIvan Khoronzhuk 	else
68715180ecaSIvan Khoronzhuk 		ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
68815180ecaSIvan Khoronzhuk 
68915180ecaSIvan Khoronzhuk 	return ret;
69015180ecaSIvan Khoronzhuk }
69115180ecaSIvan Khoronzhuk 
69215180ecaSIvan Khoronzhuk static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx)
69315180ecaSIvan Khoronzhuk {
69415180ecaSIvan Khoronzhuk 	struct addr_sync_ctx *sync_ctx = ctx;
69515180ecaSIvan Khoronzhuk 	struct netdev_hw_addr *ha;
69615180ecaSIvan Khoronzhuk 	int found = 0, ret = 0;
69715180ecaSIvan Khoronzhuk 
69815180ecaSIvan Khoronzhuk 	if (!vdev || !(vdev->flags & IFF_UP))
69915180ecaSIvan Khoronzhuk 		return 0;
70015180ecaSIvan Khoronzhuk 
70115180ecaSIvan Khoronzhuk 	/* vlan address is relevant if its sync_cnt != 0 */
70215180ecaSIvan Khoronzhuk 	netdev_for_each_mc_addr(ha, vdev) {
70315180ecaSIvan Khoronzhuk 		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
70415180ecaSIvan Khoronzhuk 			found = ha->sync_cnt;
70515180ecaSIvan Khoronzhuk 			break;
70615180ecaSIvan Khoronzhuk 		}
70715180ecaSIvan Khoronzhuk 	}
70815180ecaSIvan Khoronzhuk 
70915180ecaSIvan Khoronzhuk 	if (found)
71015180ecaSIvan Khoronzhuk 		sync_ctx->consumed++;
71115180ecaSIvan Khoronzhuk 
71215180ecaSIvan Khoronzhuk 	if (sync_ctx->flush) {
71315180ecaSIvan Khoronzhuk 		if (!found)
71415180ecaSIvan Khoronzhuk 			cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
71515180ecaSIvan Khoronzhuk 		return 0;
71615180ecaSIvan Khoronzhuk 	}
71715180ecaSIvan Khoronzhuk 
71815180ecaSIvan Khoronzhuk 	if (found)
71915180ecaSIvan Khoronzhuk 		ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1);
72015180ecaSIvan Khoronzhuk 
72115180ecaSIvan Khoronzhuk 	return ret;
72215180ecaSIvan Khoronzhuk }
72315180ecaSIvan Khoronzhuk 
72415180ecaSIvan Khoronzhuk static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num)
72515180ecaSIvan Khoronzhuk {
72615180ecaSIvan Khoronzhuk 	struct addr_sync_ctx sync_ctx;
72715180ecaSIvan Khoronzhuk 	int ret;
72815180ecaSIvan Khoronzhuk 
72915180ecaSIvan Khoronzhuk 	sync_ctx.consumed = 0;
73015180ecaSIvan Khoronzhuk 	sync_ctx.addr = addr;
73115180ecaSIvan Khoronzhuk 	sync_ctx.ndev = ndev;
73215180ecaSIvan Khoronzhuk 	sync_ctx.flush = 0;
73315180ecaSIvan Khoronzhuk 
73415180ecaSIvan Khoronzhuk 	ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
73515180ecaSIvan Khoronzhuk 	if (sync_ctx.consumed < num && !ret)
73615180ecaSIvan Khoronzhuk 		ret = cpsw_set_mc(ndev, addr, -1, 1);
73715180ecaSIvan Khoronzhuk 
73815180ecaSIvan Khoronzhuk 	return ret;
73915180ecaSIvan Khoronzhuk }
74015180ecaSIvan Khoronzhuk 
74115180ecaSIvan Khoronzhuk static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num)
74215180ecaSIvan Khoronzhuk {
74315180ecaSIvan Khoronzhuk 	struct addr_sync_ctx sync_ctx;
74415180ecaSIvan Khoronzhuk 
74515180ecaSIvan Khoronzhuk 	sync_ctx.consumed = 0;
74615180ecaSIvan Khoronzhuk 	sync_ctx.addr = addr;
74715180ecaSIvan Khoronzhuk 	sync_ctx.ndev = ndev;
74815180ecaSIvan Khoronzhuk 	sync_ctx.flush = 1;
74915180ecaSIvan Khoronzhuk 
75015180ecaSIvan Khoronzhuk 	vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
75115180ecaSIvan Khoronzhuk 	if (sync_ctx.consumed == num)
75215180ecaSIvan Khoronzhuk 		cpsw_set_mc(ndev, addr, -1, 0);
75315180ecaSIvan Khoronzhuk 
75415180ecaSIvan Khoronzhuk 	return 0;
75515180ecaSIvan Khoronzhuk }
75615180ecaSIvan Khoronzhuk 
75715180ecaSIvan Khoronzhuk static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx)
75815180ecaSIvan Khoronzhuk {
75915180ecaSIvan Khoronzhuk 	struct addr_sync_ctx *sync_ctx = ctx;
76015180ecaSIvan Khoronzhuk 	struct netdev_hw_addr *ha;
76115180ecaSIvan Khoronzhuk 	int found = 0;
76215180ecaSIvan Khoronzhuk 
76315180ecaSIvan Khoronzhuk 	if (!vdev || !(vdev->flags & IFF_UP))
76415180ecaSIvan Khoronzhuk 		return 0;
76515180ecaSIvan Khoronzhuk 
76615180ecaSIvan Khoronzhuk 	/* vlan address is relevant if its sync_cnt != 0 */
76715180ecaSIvan Khoronzhuk 	netdev_for_each_mc_addr(ha, vdev) {
76815180ecaSIvan Khoronzhuk 		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
76915180ecaSIvan Khoronzhuk 			found = ha->sync_cnt;
77015180ecaSIvan Khoronzhuk 			break;
77115180ecaSIvan Khoronzhuk 		}
77215180ecaSIvan Khoronzhuk 	}
77315180ecaSIvan Khoronzhuk 
77415180ecaSIvan Khoronzhuk 	if (!found)
77515180ecaSIvan Khoronzhuk 		return 0;
77615180ecaSIvan Khoronzhuk 
77715180ecaSIvan Khoronzhuk 	sync_ctx->consumed++;
77815180ecaSIvan Khoronzhuk 	cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
77915180ecaSIvan Khoronzhuk 	return 0;
78015180ecaSIvan Khoronzhuk }
78115180ecaSIvan Khoronzhuk 
78215180ecaSIvan Khoronzhuk static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num)
78315180ecaSIvan Khoronzhuk {
78415180ecaSIvan Khoronzhuk 	struct addr_sync_ctx sync_ctx;
78515180ecaSIvan Khoronzhuk 
78615180ecaSIvan Khoronzhuk 	sync_ctx.addr = addr;
78715180ecaSIvan Khoronzhuk 	sync_ctx.ndev = ndev;
78815180ecaSIvan Khoronzhuk 	sync_ctx.consumed = 0;
78915180ecaSIvan Khoronzhuk 
79015180ecaSIvan Khoronzhuk 	vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx);
79115180ecaSIvan Khoronzhuk 	if (sync_ctx.consumed < num)
79215180ecaSIvan Khoronzhuk 		cpsw_set_mc(ndev, addr, -1, 0);
79315180ecaSIvan Khoronzhuk 
7945da19489SIvan Khoronzhuk 	return 0;
7955da19489SIvan Khoronzhuk }
7965da19489SIvan Khoronzhuk 
7975da19489SIvan Khoronzhuk static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
7985da19489SIvan Khoronzhuk {
7995da19489SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
8005c50a856SMugunthan V N 
8015c50a856SMugunthan V N 	if (ndev->flags & IFF_PROMISC) {
8025c50a856SMugunthan V N 		/* Enable promiscuous mode */
8030cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, true);
8042a05a622SIvan Khoronzhuk 		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
8055c50a856SMugunthan V N 		return;
8060cd8f9ccSMugunthan V N 	} else {
8070cd8f9ccSMugunthan V N 		/* Disable promiscuous mode */
8080cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, false);
8095c50a856SMugunthan V N 	}
8105c50a856SMugunthan V N 
8111e5c4bc4SLennart Sorensen 	/* Restore allmulti on vlans if necessary */
8125da19489SIvan Khoronzhuk 	cpsw_ale_set_allmulti(cpsw->ale, ndev->flags & IFF_ALLMULTI);
8131e5c4bc4SLennart Sorensen 
81415180ecaSIvan Khoronzhuk 	/* add/remove mcast address either for real netdev or for vlan */
81515180ecaSIvan Khoronzhuk 	__hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr,
81615180ecaSIvan Khoronzhuk 			       cpsw_del_mc_addr);
8175c50a856SMugunthan V N }
8185c50a856SMugunthan V N 
8192c836bd9SIvan Khoronzhuk static void cpsw_intr_enable(struct cpsw_common *cpsw)
820df828598SMugunthan V N {
821dda5f5feSGrygorii Strashko 	writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
822dda5f5feSGrygorii Strashko 	writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
823df828598SMugunthan V N 
8242c836bd9SIvan Khoronzhuk 	cpdma_ctlr_int_ctrl(cpsw->dma, true);
825df828598SMugunthan V N 	return;
826df828598SMugunthan V N }
827df828598SMugunthan V N 
8282c836bd9SIvan Khoronzhuk static void cpsw_intr_disable(struct cpsw_common *cpsw)
829df828598SMugunthan V N {
830dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->wr_regs->tx_en);
831dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->wr_regs->rx_en);
832df828598SMugunthan V N 
8332c836bd9SIvan Khoronzhuk 	cpdma_ctlr_int_ctrl(cpsw->dma, false);
834df828598SMugunthan V N 	return;
835df828598SMugunthan V N }
836df828598SMugunthan V N 
8371a3b5056SOlof Johansson static void cpsw_tx_handler(void *token, int len, int status)
838df828598SMugunthan V N {
839e05107e6SIvan Khoronzhuk 	struct netdev_queue	*txq;
840df828598SMugunthan V N 	struct sk_buff		*skb = token;
841df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
8422a05a622SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
843df828598SMugunthan V N 
844fae50823SMugunthan V N 	/* Check whether the queue is stopped due to stalled tx dma, if the
845fae50823SMugunthan V N 	 * queue is stopped then start the queue as we have free desc for tx
846fae50823SMugunthan V N 	 */
847e05107e6SIvan Khoronzhuk 	txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
848e05107e6SIvan Khoronzhuk 	if (unlikely(netif_tx_queue_stopped(txq)))
849e05107e6SIvan Khoronzhuk 		netif_tx_wake_queue(txq);
850e05107e6SIvan Khoronzhuk 
8512a05a622SIvan Khoronzhuk 	cpts_tx_timestamp(cpsw->cpts, skb);
8528dc43ddcSTobias Klauser 	ndev->stats.tx_packets++;
8538dc43ddcSTobias Klauser 	ndev->stats.tx_bytes += len;
854df828598SMugunthan V N 	dev_kfree_skb_any(skb);
855df828598SMugunthan V N }
856df828598SMugunthan V N 
857a3a41d2fSGrygorii Strashko static void cpsw_rx_vlan_encap(struct sk_buff *skb)
858a3a41d2fSGrygorii Strashko {
859a3a41d2fSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(skb->dev);
860a3a41d2fSGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
861a3a41d2fSGrygorii Strashko 	u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
862a3a41d2fSGrygorii Strashko 	u16 vtag, vid, prio, pkt_type;
863a3a41d2fSGrygorii Strashko 
864a3a41d2fSGrygorii Strashko 	/* Remove VLAN header encapsulation word */
865a3a41d2fSGrygorii Strashko 	skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
866a3a41d2fSGrygorii Strashko 
867a3a41d2fSGrygorii Strashko 	pkt_type = (rx_vlan_encap_hdr >>
868a3a41d2fSGrygorii Strashko 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
869a3a41d2fSGrygorii Strashko 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
870a3a41d2fSGrygorii Strashko 	/* Ignore unknown & Priority-tagged packets*/
871a3a41d2fSGrygorii Strashko 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
872a3a41d2fSGrygorii Strashko 	    pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
873a3a41d2fSGrygorii Strashko 		return;
874a3a41d2fSGrygorii Strashko 
875a3a41d2fSGrygorii Strashko 	vid = (rx_vlan_encap_hdr >>
876a3a41d2fSGrygorii Strashko 	       CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
877a3a41d2fSGrygorii Strashko 	       VLAN_VID_MASK;
878a3a41d2fSGrygorii Strashko 	/* Ignore vid 0 and pass packet as is */
879a3a41d2fSGrygorii Strashko 	if (!vid)
880a3a41d2fSGrygorii Strashko 		return;
881a3a41d2fSGrygorii Strashko 	/* Ignore default vlans in dual mac mode */
882a3a41d2fSGrygorii Strashko 	if (cpsw->data.dual_emac &&
883a3a41d2fSGrygorii Strashko 	    vid == cpsw->slaves[priv->emac_port].port_vlan)
884a3a41d2fSGrygorii Strashko 		return;
885a3a41d2fSGrygorii Strashko 
886a3a41d2fSGrygorii Strashko 	prio = (rx_vlan_encap_hdr >>
887a3a41d2fSGrygorii Strashko 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
888a3a41d2fSGrygorii Strashko 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
889a3a41d2fSGrygorii Strashko 
890a3a41d2fSGrygorii Strashko 	vtag = (prio << VLAN_PRIO_SHIFT) | vid;
891a3a41d2fSGrygorii Strashko 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
892a3a41d2fSGrygorii Strashko 
893a3a41d2fSGrygorii Strashko 	/* strip vlan tag for VLAN-tagged packet */
894a3a41d2fSGrygorii Strashko 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
895a3a41d2fSGrygorii Strashko 		memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
896a3a41d2fSGrygorii Strashko 		skb_pull(skb, VLAN_HLEN);
897a3a41d2fSGrygorii Strashko 	}
898a3a41d2fSGrygorii Strashko }
899a3a41d2fSGrygorii Strashko 
9001a3b5056SOlof Johansson static void cpsw_rx_handler(void *token, int len, int status)
901df828598SMugunthan V N {
902e05107e6SIvan Khoronzhuk 	struct cpdma_chan	*ch;
903df828598SMugunthan V N 	struct sk_buff		*skb = token;
904b4727e69SSebastian Siewior 	struct sk_buff		*new_skb;
905df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
906fea49f60SIvan Khoronzhuk 	int			ret = 0, port;
9072a05a622SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
908df828598SMugunthan V N 
909fea49f60SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
910fea49f60SIvan Khoronzhuk 		port = CPDMA_RX_SOURCE_PORT(status);
911fea49f60SIvan Khoronzhuk 		if (port) {
912fea49f60SIvan Khoronzhuk 			ndev = cpsw->slaves[--port].ndev;
913fea49f60SIvan Khoronzhuk 			skb->dev = ndev;
914fea49f60SIvan Khoronzhuk 		}
915fea49f60SIvan Khoronzhuk 	}
916d9ba8f9eSMugunthan V N 
91716e5c57dSMugunthan V N 	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
918a0e2c822SMugunthan V N 		/* In dual emac mode check for all interfaces */
919d5bc1613SIvan Khoronzhuk 		if (cpsw->data.dual_emac && cpsw->usage_count &&
920fe734d0aSIvan Khoronzhuk 		    (status >= 0)) {
921a0e2c822SMugunthan V N 			/* The packet received is for the interface which
922a0e2c822SMugunthan V N 			 * is already down and the other interface is up
923dbedd44eSJoe Perches 			 * and running, instead of freeing which results
924a0e2c822SMugunthan V N 			 * in reducing of the number of rx descriptor in
925a0e2c822SMugunthan V N 			 * DMA engine, requeue skb back to cpdma.
926a0e2c822SMugunthan V N 			 */
927a0e2c822SMugunthan V N 			new_skb = skb;
928a0e2c822SMugunthan V N 			goto requeue;
929a0e2c822SMugunthan V N 		}
930a0e2c822SMugunthan V N 
931b4727e69SSebastian Siewior 		/* the interface is going down, skbs are purged */
932df828598SMugunthan V N 		dev_kfree_skb_any(skb);
933df828598SMugunthan V N 		return;
934df828598SMugunthan V N 	}
935b4727e69SSebastian Siewior 
9362a05a622SIvan Khoronzhuk 	new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
937b4727e69SSebastian Siewior 	if (new_skb) {
938e05107e6SIvan Khoronzhuk 		skb_copy_queue_mapping(new_skb, skb);
939df828598SMugunthan V N 		skb_put(skb, len);
940a3a41d2fSGrygorii Strashko 		if (status & CPDMA_RX_VLAN_ENCAP)
941a3a41d2fSGrygorii Strashko 			cpsw_rx_vlan_encap(skb);
9422a05a622SIvan Khoronzhuk 		cpts_rx_timestamp(cpsw->cpts, skb);
943df828598SMugunthan V N 		skb->protocol = eth_type_trans(skb, ndev);
944df828598SMugunthan V N 		netif_receive_skb(skb);
9458dc43ddcSTobias Klauser 		ndev->stats.rx_bytes += len;
9468dc43ddcSTobias Klauser 		ndev->stats.rx_packets++;
947254a49d5SGrygorii Strashko 		kmemleak_not_leak(new_skb);
948b4727e69SSebastian Siewior 	} else {
9498dc43ddcSTobias Klauser 		ndev->stats.rx_dropped++;
950b4727e69SSebastian Siewior 		new_skb = skb;
951df828598SMugunthan V N 	}
952df828598SMugunthan V N 
953a0e2c822SMugunthan V N requeue:
954ce52c744SIvan Khoronzhuk 	if (netif_dormant(ndev)) {
955ce52c744SIvan Khoronzhuk 		dev_kfree_skb_any(new_skb);
956ce52c744SIvan Khoronzhuk 		return;
957ce52c744SIvan Khoronzhuk 	}
958ce52c744SIvan Khoronzhuk 
9598feb0a19SIvan Khoronzhuk 	ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
960e05107e6SIvan Khoronzhuk 	ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
961b4727e69SSebastian Siewior 				skb_tailroom(new_skb), 0);
962b4727e69SSebastian Siewior 	if (WARN_ON(ret < 0))
963b4727e69SSebastian Siewior 		dev_kfree_skb_any(new_skb);
964df828598SMugunthan V N }
965df828598SMugunthan V N 
96632b78d85SIvan Khoronzhuk static void cpsw_split_res(struct net_device *ndev)
96748e0a83eSIvan Khoronzhuk {
96848e0a83eSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
96932b78d85SIvan Khoronzhuk 	u32 consumed_rate = 0, bigest_rate = 0;
97048e0a83eSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
97148e0a83eSIvan Khoronzhuk 	struct cpsw_vector *txv = cpsw->txv;
97232b78d85SIvan Khoronzhuk 	int i, ch_weight, rlim_ch_num = 0;
97348e0a83eSIvan Khoronzhuk 	int budget, bigest_rate_ch = 0;
97448e0a83eSIvan Khoronzhuk 	u32 ch_rate, max_rate;
97548e0a83eSIvan Khoronzhuk 	int ch_budget = 0;
97648e0a83eSIvan Khoronzhuk 
97748e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->tx_ch_num; i++) {
97848e0a83eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
97948e0a83eSIvan Khoronzhuk 		if (!ch_rate)
98048e0a83eSIvan Khoronzhuk 			continue;
98148e0a83eSIvan Khoronzhuk 
98248e0a83eSIvan Khoronzhuk 		rlim_ch_num++;
98348e0a83eSIvan Khoronzhuk 		consumed_rate += ch_rate;
98448e0a83eSIvan Khoronzhuk 	}
98548e0a83eSIvan Khoronzhuk 
98648e0a83eSIvan Khoronzhuk 	if (cpsw->tx_ch_num == rlim_ch_num) {
98748e0a83eSIvan Khoronzhuk 		max_rate = consumed_rate;
98832b78d85SIvan Khoronzhuk 	} else if (!rlim_ch_num) {
98932b78d85SIvan Khoronzhuk 		ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
99032b78d85SIvan Khoronzhuk 		bigest_rate = 0;
99132b78d85SIvan Khoronzhuk 		max_rate = consumed_rate;
99248e0a83eSIvan Khoronzhuk 	} else {
9930be01b8eSIvan Khoronzhuk 		max_rate = cpsw->speed * 1000;
9940be01b8eSIvan Khoronzhuk 
9950be01b8eSIvan Khoronzhuk 		/* if max_rate is less then expected due to reduced link speed,
9960be01b8eSIvan Khoronzhuk 		 * split proportionally according next potential max speed
9970be01b8eSIvan Khoronzhuk 		 */
9980be01b8eSIvan Khoronzhuk 		if (max_rate < consumed_rate)
9990be01b8eSIvan Khoronzhuk 			max_rate *= 10;
10000be01b8eSIvan Khoronzhuk 
10010be01b8eSIvan Khoronzhuk 		if (max_rate < consumed_rate)
10020be01b8eSIvan Khoronzhuk 			max_rate *= 10;
100332b78d85SIvan Khoronzhuk 
100448e0a83eSIvan Khoronzhuk 		ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
100548e0a83eSIvan Khoronzhuk 		ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
100648e0a83eSIvan Khoronzhuk 			    (cpsw->tx_ch_num - rlim_ch_num);
100748e0a83eSIvan Khoronzhuk 		bigest_rate = (max_rate - consumed_rate) /
100848e0a83eSIvan Khoronzhuk 			      (cpsw->tx_ch_num - rlim_ch_num);
100948e0a83eSIvan Khoronzhuk 	}
101048e0a83eSIvan Khoronzhuk 
101132b78d85SIvan Khoronzhuk 	/* split tx weight/budget */
101248e0a83eSIvan Khoronzhuk 	budget = CPSW_POLL_WEIGHT;
101348e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->tx_ch_num; i++) {
101448e0a83eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
101548e0a83eSIvan Khoronzhuk 		if (ch_rate) {
101648e0a83eSIvan Khoronzhuk 			txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
101748e0a83eSIvan Khoronzhuk 			if (!txv[i].budget)
101832b78d85SIvan Khoronzhuk 				txv[i].budget++;
101948e0a83eSIvan Khoronzhuk 			if (ch_rate > bigest_rate) {
102048e0a83eSIvan Khoronzhuk 				bigest_rate_ch = i;
102148e0a83eSIvan Khoronzhuk 				bigest_rate = ch_rate;
102248e0a83eSIvan Khoronzhuk 			}
102332b78d85SIvan Khoronzhuk 
102432b78d85SIvan Khoronzhuk 			ch_weight = (ch_rate * 100) / max_rate;
102532b78d85SIvan Khoronzhuk 			if (!ch_weight)
102632b78d85SIvan Khoronzhuk 				ch_weight++;
102732b78d85SIvan Khoronzhuk 			cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
102848e0a83eSIvan Khoronzhuk 		} else {
102948e0a83eSIvan Khoronzhuk 			txv[i].budget = ch_budget;
103048e0a83eSIvan Khoronzhuk 			if (!bigest_rate_ch)
103148e0a83eSIvan Khoronzhuk 				bigest_rate_ch = i;
103232b78d85SIvan Khoronzhuk 			cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
103348e0a83eSIvan Khoronzhuk 		}
103448e0a83eSIvan Khoronzhuk 
103548e0a83eSIvan Khoronzhuk 		budget -= txv[i].budget;
103648e0a83eSIvan Khoronzhuk 	}
103748e0a83eSIvan Khoronzhuk 
103848e0a83eSIvan Khoronzhuk 	if (budget)
103948e0a83eSIvan Khoronzhuk 		txv[bigest_rate_ch].budget += budget;
104048e0a83eSIvan Khoronzhuk 
104148e0a83eSIvan Khoronzhuk 	/* split rx budget */
104248e0a83eSIvan Khoronzhuk 	budget = CPSW_POLL_WEIGHT;
104348e0a83eSIvan Khoronzhuk 	ch_budget = budget / cpsw->rx_ch_num;
104448e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->rx_ch_num; i++) {
104548e0a83eSIvan Khoronzhuk 		cpsw->rxv[i].budget = ch_budget;
104648e0a83eSIvan Khoronzhuk 		budget -= ch_budget;
104748e0a83eSIvan Khoronzhuk 	}
104848e0a83eSIvan Khoronzhuk 
104948e0a83eSIvan Khoronzhuk 	if (budget)
105048e0a83eSIvan Khoronzhuk 		cpsw->rxv[0].budget += budget;
105148e0a83eSIvan Khoronzhuk }
105248e0a83eSIvan Khoronzhuk 
1053c03abd84SFelipe Balbi static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
1054df828598SMugunthan V N {
1055dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = dev_id;
10567ce67a38SFelipe Balbi 
10575d8d0d4dSIvan Khoronzhuk 	writel(0, &cpsw->wr_regs->tx_en);
10582c836bd9SIvan Khoronzhuk 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
1059c03abd84SFelipe Balbi 
1060e38b5a3dSIvan Khoronzhuk 	if (cpsw->quirk_irq) {
1061e38b5a3dSIvan Khoronzhuk 		disable_irq_nosync(cpsw->irqs_table[1]);
1062e38b5a3dSIvan Khoronzhuk 		cpsw->tx_irq_disabled = true;
10637da11600SMugunthan V N 	}
10647da11600SMugunthan V N 
1065dbc4ec52SIvan Khoronzhuk 	napi_schedule(&cpsw->napi_tx);
1066c03abd84SFelipe Balbi 	return IRQ_HANDLED;
1067c03abd84SFelipe Balbi }
1068c03abd84SFelipe Balbi 
1069c03abd84SFelipe Balbi static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
1070c03abd84SFelipe Balbi {
1071dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = dev_id;
1072c03abd84SFelipe Balbi 
10732c836bd9SIvan Khoronzhuk 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
10745d8d0d4dSIvan Khoronzhuk 	writel(0, &cpsw->wr_regs->rx_en);
1075fd51cf19SSebastian Siewior 
1076e38b5a3dSIvan Khoronzhuk 	if (cpsw->quirk_irq) {
1077e38b5a3dSIvan Khoronzhuk 		disable_irq_nosync(cpsw->irqs_table[0]);
1078e38b5a3dSIvan Khoronzhuk 		cpsw->rx_irq_disabled = true;
10797da11600SMugunthan V N 	}
10807da11600SMugunthan V N 
1081dbc4ec52SIvan Khoronzhuk 	napi_schedule(&cpsw->napi_rx);
1082df828598SMugunthan V N 	return IRQ_HANDLED;
1083df828598SMugunthan V N }
1084df828598SMugunthan V N 
10859611d6d6SIvan Khoronzhuk static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
1086df828598SMugunthan V N {
1087e05107e6SIvan Khoronzhuk 	u32			ch_map;
10888feb0a19SIvan Khoronzhuk 	int			num_tx, cur_budget, ch;
1089dbc4ec52SIvan Khoronzhuk 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_tx);
10908feb0a19SIvan Khoronzhuk 	struct cpsw_vector	*txv;
109132a7432cSMugunthan V N 
1092e05107e6SIvan Khoronzhuk 	/* process every unprocessed channel */
1093e05107e6SIvan Khoronzhuk 	ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
109479b3325dSIvan Khoronzhuk 	for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
109579b3325dSIvan Khoronzhuk 		if (!(ch_map & 0x80))
1096e05107e6SIvan Khoronzhuk 			continue;
1097e05107e6SIvan Khoronzhuk 
10988feb0a19SIvan Khoronzhuk 		txv = &cpsw->txv[ch];
10998feb0a19SIvan Khoronzhuk 		if (unlikely(txv->budget > budget - num_tx))
11008feb0a19SIvan Khoronzhuk 			cur_budget = budget - num_tx;
11018feb0a19SIvan Khoronzhuk 		else
11028feb0a19SIvan Khoronzhuk 			cur_budget = txv->budget;
11038feb0a19SIvan Khoronzhuk 
11048feb0a19SIvan Khoronzhuk 		num_tx += cpdma_chan_process(txv->ch, cur_budget);
1105342934a5SIvan Khoronzhuk 		if (num_tx >= budget)
1106342934a5SIvan Khoronzhuk 			break;
1107e05107e6SIvan Khoronzhuk 	}
1108e05107e6SIvan Khoronzhuk 
110932a7432cSMugunthan V N 	if (num_tx < budget) {
111032a7432cSMugunthan V N 		napi_complete(napi_tx);
11115d8d0d4dSIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->tx_en);
11129611d6d6SIvan Khoronzhuk 	}
11139611d6d6SIvan Khoronzhuk 
11149611d6d6SIvan Khoronzhuk 	return num_tx;
11159611d6d6SIvan Khoronzhuk }
11169611d6d6SIvan Khoronzhuk 
11179611d6d6SIvan Khoronzhuk static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
11189611d6d6SIvan Khoronzhuk {
11199611d6d6SIvan Khoronzhuk 	struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
11209611d6d6SIvan Khoronzhuk 	int num_tx;
11219611d6d6SIvan Khoronzhuk 
11229611d6d6SIvan Khoronzhuk 	num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
11239611d6d6SIvan Khoronzhuk 	if (num_tx < budget) {
11249611d6d6SIvan Khoronzhuk 		napi_complete(napi_tx);
11259611d6d6SIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->tx_en);
11269611d6d6SIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
1127e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
1128e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
11297da11600SMugunthan V N 		}
113032a7432cSMugunthan V N 	}
113132a7432cSMugunthan V N 
113232a7432cSMugunthan V N 	return num_tx;
113332a7432cSMugunthan V N }
113432a7432cSMugunthan V N 
11359611d6d6SIvan Khoronzhuk static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
113632a7432cSMugunthan V N {
1137e05107e6SIvan Khoronzhuk 	u32			ch_map;
11388feb0a19SIvan Khoronzhuk 	int			num_rx, cur_budget, ch;
1139dbc4ec52SIvan Khoronzhuk 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_rx);
11408feb0a19SIvan Khoronzhuk 	struct cpsw_vector	*rxv;
1141510a1e72SMugunthan V N 
1142e05107e6SIvan Khoronzhuk 	/* process every unprocessed channel */
1143e05107e6SIvan Khoronzhuk 	ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
1144342934a5SIvan Khoronzhuk 	for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
1145e05107e6SIvan Khoronzhuk 		if (!(ch_map & 0x01))
1146e05107e6SIvan Khoronzhuk 			continue;
1147e05107e6SIvan Khoronzhuk 
11488feb0a19SIvan Khoronzhuk 		rxv = &cpsw->rxv[ch];
11498feb0a19SIvan Khoronzhuk 		if (unlikely(rxv->budget > budget - num_rx))
11508feb0a19SIvan Khoronzhuk 			cur_budget = budget - num_rx;
11518feb0a19SIvan Khoronzhuk 		else
11528feb0a19SIvan Khoronzhuk 			cur_budget = rxv->budget;
11538feb0a19SIvan Khoronzhuk 
11548feb0a19SIvan Khoronzhuk 		num_rx += cpdma_chan_process(rxv->ch, cur_budget);
1155342934a5SIvan Khoronzhuk 		if (num_rx >= budget)
1156342934a5SIvan Khoronzhuk 			break;
1157e05107e6SIvan Khoronzhuk 	}
1158e05107e6SIvan Khoronzhuk 
1159510a1e72SMugunthan V N 	if (num_rx < budget) {
11606ad20165SEric Dumazet 		napi_complete_done(napi_rx, num_rx);
11615d8d0d4dSIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->rx_en);
11629611d6d6SIvan Khoronzhuk 	}
11639611d6d6SIvan Khoronzhuk 
11649611d6d6SIvan Khoronzhuk 	return num_rx;
11659611d6d6SIvan Khoronzhuk }
11669611d6d6SIvan Khoronzhuk 
11679611d6d6SIvan Khoronzhuk static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
11689611d6d6SIvan Khoronzhuk {
11699611d6d6SIvan Khoronzhuk 	struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
11709611d6d6SIvan Khoronzhuk 	int num_rx;
11719611d6d6SIvan Khoronzhuk 
11729611d6d6SIvan Khoronzhuk 	num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
11739611d6d6SIvan Khoronzhuk 	if (num_rx < budget) {
11749611d6d6SIvan Khoronzhuk 		napi_complete_done(napi_rx, num_rx);
11759611d6d6SIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->rx_en);
11769611d6d6SIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
1177e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
1178e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
11797da11600SMugunthan V N 		}
1180510a1e72SMugunthan V N 	}
1181df828598SMugunthan V N 
1182df828598SMugunthan V N 	return num_rx;
1183df828598SMugunthan V N }
1184df828598SMugunthan V N 
1185df828598SMugunthan V N static inline void soft_reset(const char *module, void __iomem *reg)
1186df828598SMugunthan V N {
1187df828598SMugunthan V N 	unsigned long timeout = jiffies + HZ;
1188df828598SMugunthan V N 
1189dda5f5feSGrygorii Strashko 	writel_relaxed(1, reg);
1190df828598SMugunthan V N 	do {
1191df828598SMugunthan V N 		cpu_relax();
1192dda5f5feSGrygorii Strashko 	} while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
1193df828598SMugunthan V N 
1194dda5f5feSGrygorii Strashko 	WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
1195df828598SMugunthan V N }
1196df828598SMugunthan V N 
1197df828598SMugunthan V N static void cpsw_set_slave_mac(struct cpsw_slave *slave,
1198df828598SMugunthan V N 			       struct cpsw_priv *priv)
1199df828598SMugunthan V N {
12009750a3adSRichard Cochran 	slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
12019750a3adSRichard Cochran 	slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
1202df828598SMugunthan V N }
1203df828598SMugunthan V N 
120457d90148SIvan Khoronzhuk static bool cpsw_shp_is_off(struct cpsw_priv *priv)
120557d90148SIvan Khoronzhuk {
120657d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
120757d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
120857d90148SIvan Khoronzhuk 	u32 shift, mask, val;
120957d90148SIvan Khoronzhuk 
121057d90148SIvan Khoronzhuk 	val = readl_relaxed(&cpsw->regs->ptype);
121157d90148SIvan Khoronzhuk 
121257d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
121357d90148SIvan Khoronzhuk 	shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
121457d90148SIvan Khoronzhuk 	mask = 7 << shift;
121557d90148SIvan Khoronzhuk 	val = val & mask;
121657d90148SIvan Khoronzhuk 
121757d90148SIvan Khoronzhuk 	return !val;
121857d90148SIvan Khoronzhuk }
121957d90148SIvan Khoronzhuk 
122057d90148SIvan Khoronzhuk static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
122157d90148SIvan Khoronzhuk {
122257d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
122357d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
122457d90148SIvan Khoronzhuk 	u32 shift, mask, val;
122557d90148SIvan Khoronzhuk 
122657d90148SIvan Khoronzhuk 	val = readl_relaxed(&cpsw->regs->ptype);
122757d90148SIvan Khoronzhuk 
122857d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
122957d90148SIvan Khoronzhuk 	shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
123057d90148SIvan Khoronzhuk 	mask = (1 << --fifo) << shift;
123157d90148SIvan Khoronzhuk 	val = on ? val | mask : val & ~mask;
123257d90148SIvan Khoronzhuk 
123357d90148SIvan Khoronzhuk 	writel_relaxed(val, &cpsw->regs->ptype);
123457d90148SIvan Khoronzhuk }
123557d90148SIvan Khoronzhuk 
1236df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave,
1237df828598SMugunthan V N 			      struct cpsw_priv *priv, bool *link)
1238df828598SMugunthan V N {
1239df828598SMugunthan V N 	struct phy_device	*phy = slave->phy;
1240df828598SMugunthan V N 	u32			mac_control = 0;
1241df828598SMugunthan V N 	u32			slave_port;
1242606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1243df828598SMugunthan V N 
1244df828598SMugunthan V N 	if (!phy)
1245df828598SMugunthan V N 		return;
1246df828598SMugunthan V N 
12476f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
1248df828598SMugunthan V N 
1249df828598SMugunthan V N 	if (phy->link) {
1250606f3993SIvan Khoronzhuk 		mac_control = cpsw->data.mac_control;
1251df828598SMugunthan V N 
1252df828598SMugunthan V N 		/* enable forwarding */
12532a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
1254df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1255df828598SMugunthan V N 
1256df828598SMugunthan V N 		if (phy->speed == 1000)
1257df828598SMugunthan V N 			mac_control |= BIT(7);	/* GIGABITEN	*/
1258df828598SMugunthan V N 		if (phy->duplex)
1259df828598SMugunthan V N 			mac_control |= BIT(0);	/* FULLDUPLEXEN	*/
1260342b7b74SDaniel Mack 
1261342b7b74SDaniel Mack 		/* set speed_in input in case RMII mode is used in 100Mbps */
1262342b7b74SDaniel Mack 		if (phy->speed == 100)
1263342b7b74SDaniel Mack 			mac_control |= BIT(15);
1264f9db5069SSZ Lin (林上智) 		/* in band mode only works in 10Mbps RGMII mode */
1265f9db5069SSZ Lin (林上智) 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
1266a81d8762SMugunthan V N 			mac_control |= BIT(18); /* In Band mode */
1267342b7b74SDaniel Mack 
12681923d6e4SMugunthan V N 		if (priv->rx_pause)
12691923d6e4SMugunthan V N 			mac_control |= BIT(3);
12701923d6e4SMugunthan V N 
12711923d6e4SMugunthan V N 		if (priv->tx_pause)
12721923d6e4SMugunthan V N 			mac_control |= BIT(4);
12731923d6e4SMugunthan V N 
1274df828598SMugunthan V N 		*link = true;
127557d90148SIvan Khoronzhuk 
127657d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
127757d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed &&
127857d90148SIvan Khoronzhuk 		    !cpsw_shp_is_off(priv))
127957d90148SIvan Khoronzhuk 			dev_warn(priv->dev,
128057d90148SIvan Khoronzhuk 				 "Speed was changed, CBS shaper speeds are changed!");
1281df828598SMugunthan V N 	} else {
1282df828598SMugunthan V N 		mac_control = 0;
1283df828598SMugunthan V N 		/* disable forwarding */
12842a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
1285df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1286df828598SMugunthan V N 	}
1287df828598SMugunthan V N 
1288df828598SMugunthan V N 	if (mac_control != slave->mac_control) {
1289df828598SMugunthan V N 		phy_print_status(phy);
1290dda5f5feSGrygorii Strashko 		writel_relaxed(mac_control, &slave->sliver->mac_control);
1291df828598SMugunthan V N 	}
1292df828598SMugunthan V N 
1293df828598SMugunthan V N 	slave->mac_control = mac_control;
1294df828598SMugunthan V N }
1295df828598SMugunthan V N 
12960be01b8eSIvan Khoronzhuk static int cpsw_get_common_speed(struct cpsw_common *cpsw)
12970be01b8eSIvan Khoronzhuk {
12980be01b8eSIvan Khoronzhuk 	int i, speed;
12990be01b8eSIvan Khoronzhuk 
13000be01b8eSIvan Khoronzhuk 	for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
13010be01b8eSIvan Khoronzhuk 		if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
13020be01b8eSIvan Khoronzhuk 			speed += cpsw->slaves[i].phy->speed;
13030be01b8eSIvan Khoronzhuk 
13040be01b8eSIvan Khoronzhuk 	return speed;
13050be01b8eSIvan Khoronzhuk }
13060be01b8eSIvan Khoronzhuk 
13070be01b8eSIvan Khoronzhuk static int cpsw_need_resplit(struct cpsw_common *cpsw)
13080be01b8eSIvan Khoronzhuk {
13090be01b8eSIvan Khoronzhuk 	int i, rlim_ch_num;
13100be01b8eSIvan Khoronzhuk 	int speed, ch_rate;
13110be01b8eSIvan Khoronzhuk 
13120be01b8eSIvan Khoronzhuk 	/* re-split resources only in case speed was changed */
13130be01b8eSIvan Khoronzhuk 	speed = cpsw_get_common_speed(cpsw);
13140be01b8eSIvan Khoronzhuk 	if (speed == cpsw->speed || !speed)
13150be01b8eSIvan Khoronzhuk 		return 0;
13160be01b8eSIvan Khoronzhuk 
13170be01b8eSIvan Khoronzhuk 	cpsw->speed = speed;
13180be01b8eSIvan Khoronzhuk 
13190be01b8eSIvan Khoronzhuk 	for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
13200be01b8eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
13210be01b8eSIvan Khoronzhuk 		if (!ch_rate)
13220be01b8eSIvan Khoronzhuk 			break;
13230be01b8eSIvan Khoronzhuk 
13240be01b8eSIvan Khoronzhuk 		rlim_ch_num++;
13250be01b8eSIvan Khoronzhuk 	}
13260be01b8eSIvan Khoronzhuk 
13270be01b8eSIvan Khoronzhuk 	/* cases not dependent on speed */
13280be01b8eSIvan Khoronzhuk 	if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
13290be01b8eSIvan Khoronzhuk 		return 0;
13300be01b8eSIvan Khoronzhuk 
13310be01b8eSIvan Khoronzhuk 	return 1;
13320be01b8eSIvan Khoronzhuk }
13330be01b8eSIvan Khoronzhuk 
1334df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev)
1335df828598SMugunthan V N {
1336df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
13370be01b8eSIvan Khoronzhuk 	struct cpsw_common	*cpsw = priv->cpsw;
1338df828598SMugunthan V N 	bool			link = false;
1339df828598SMugunthan V N 
1340df828598SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1341df828598SMugunthan V N 
1342df828598SMugunthan V N 	if (link) {
13430be01b8eSIvan Khoronzhuk 		if (cpsw_need_resplit(cpsw))
13440be01b8eSIvan Khoronzhuk 			cpsw_split_res(ndev);
13450be01b8eSIvan Khoronzhuk 
1346df828598SMugunthan V N 		netif_carrier_on(ndev);
1347df828598SMugunthan V N 		if (netif_running(ndev))
1348e05107e6SIvan Khoronzhuk 			netif_tx_wake_all_queues(ndev);
1349df828598SMugunthan V N 	} else {
1350df828598SMugunthan V N 		netif_carrier_off(ndev);
1351e05107e6SIvan Khoronzhuk 		netif_tx_stop_all_queues(ndev);
1352df828598SMugunthan V N 	}
1353df828598SMugunthan V N }
1354df828598SMugunthan V N 
1355ff5b8ef2SMugunthan V N static int cpsw_get_coalesce(struct net_device *ndev,
1356ff5b8ef2SMugunthan V N 				struct ethtool_coalesce *coal)
1357ff5b8ef2SMugunthan V N {
13582a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1359ff5b8ef2SMugunthan V N 
13602a05a622SIvan Khoronzhuk 	coal->rx_coalesce_usecs = cpsw->coal_intvl;
1361ff5b8ef2SMugunthan V N 	return 0;
1362ff5b8ef2SMugunthan V N }
1363ff5b8ef2SMugunthan V N 
1364ff5b8ef2SMugunthan V N static int cpsw_set_coalesce(struct net_device *ndev,
1365ff5b8ef2SMugunthan V N 				struct ethtool_coalesce *coal)
1366ff5b8ef2SMugunthan V N {
1367ff5b8ef2SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1368ff5b8ef2SMugunthan V N 	u32 int_ctrl;
1369ff5b8ef2SMugunthan V N 	u32 num_interrupts = 0;
1370ff5b8ef2SMugunthan V N 	u32 prescale = 0;
1371ff5b8ef2SMugunthan V N 	u32 addnl_dvdr = 1;
1372ff5b8ef2SMugunthan V N 	u32 coal_intvl = 0;
13735d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1374ff5b8ef2SMugunthan V N 
1375ff5b8ef2SMugunthan V N 	coal_intvl = coal->rx_coalesce_usecs;
1376ff5b8ef2SMugunthan V N 
13775d8d0d4dSIvan Khoronzhuk 	int_ctrl =  readl(&cpsw->wr_regs->int_control);
13782a05a622SIvan Khoronzhuk 	prescale = cpsw->bus_freq_mhz * 4;
1379ff5b8ef2SMugunthan V N 
1380a84bc2a9SMugunthan V N 	if (!coal->rx_coalesce_usecs) {
1381a84bc2a9SMugunthan V N 		int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1382a84bc2a9SMugunthan V N 		goto update_return;
1383a84bc2a9SMugunthan V N 	}
1384a84bc2a9SMugunthan V N 
1385ff5b8ef2SMugunthan V N 	if (coal_intvl < CPSW_CMINTMIN_INTVL)
1386ff5b8ef2SMugunthan V N 		coal_intvl = CPSW_CMINTMIN_INTVL;
1387ff5b8ef2SMugunthan V N 
1388ff5b8ef2SMugunthan V N 	if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1389ff5b8ef2SMugunthan V N 		/* Interrupt pacer works with 4us Pulse, we can
1390ff5b8ef2SMugunthan V N 		 * throttle further by dilating the 4us pulse.
1391ff5b8ef2SMugunthan V N 		 */
1392ff5b8ef2SMugunthan V N 		addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1393ff5b8ef2SMugunthan V N 
1394ff5b8ef2SMugunthan V N 		if (addnl_dvdr > 1) {
1395ff5b8ef2SMugunthan V N 			prescale *= addnl_dvdr;
1396ff5b8ef2SMugunthan V N 			if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1397ff5b8ef2SMugunthan V N 				coal_intvl = (CPSW_CMINTMAX_INTVL
1398ff5b8ef2SMugunthan V N 						* addnl_dvdr);
1399ff5b8ef2SMugunthan V N 		} else {
1400ff5b8ef2SMugunthan V N 			addnl_dvdr = 1;
1401ff5b8ef2SMugunthan V N 			coal_intvl = CPSW_CMINTMAX_INTVL;
1402ff5b8ef2SMugunthan V N 		}
1403ff5b8ef2SMugunthan V N 	}
1404ff5b8ef2SMugunthan V N 
1405ff5b8ef2SMugunthan V N 	num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
14065d8d0d4dSIvan Khoronzhuk 	writel(num_interrupts, &cpsw->wr_regs->rx_imax);
14075d8d0d4dSIvan Khoronzhuk 	writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1408ff5b8ef2SMugunthan V N 
1409ff5b8ef2SMugunthan V N 	int_ctrl |= CPSW_INTPACEEN;
1410ff5b8ef2SMugunthan V N 	int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1411ff5b8ef2SMugunthan V N 	int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1412a84bc2a9SMugunthan V N 
1413a84bc2a9SMugunthan V N update_return:
14145d8d0d4dSIvan Khoronzhuk 	writel(int_ctrl, &cpsw->wr_regs->int_control);
1415ff5b8ef2SMugunthan V N 
1416ff5b8ef2SMugunthan V N 	cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
14172a05a622SIvan Khoronzhuk 	cpsw->coal_intvl = coal_intvl;
1418ff5b8ef2SMugunthan V N 
1419ff5b8ef2SMugunthan V N 	return 0;
1420ff5b8ef2SMugunthan V N }
1421ff5b8ef2SMugunthan V N 
1422d9718546SMugunthan V N static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1423d9718546SMugunthan V N {
1424e05107e6SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1425e05107e6SIvan Khoronzhuk 
1426d9718546SMugunthan V N 	switch (sset) {
1427d9718546SMugunthan V N 	case ETH_SS_STATS:
1428e05107e6SIvan Khoronzhuk 		return (CPSW_STATS_COMMON_LEN +
1429e05107e6SIvan Khoronzhuk 		       (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1430e05107e6SIvan Khoronzhuk 		       CPSW_STATS_CH_LEN);
1431d9718546SMugunthan V N 	default:
1432d9718546SMugunthan V N 		return -EOPNOTSUPP;
1433d9718546SMugunthan V N 	}
1434d9718546SMugunthan V N }
1435d9718546SMugunthan V N 
1436e05107e6SIvan Khoronzhuk static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1437e05107e6SIvan Khoronzhuk {
1438e05107e6SIvan Khoronzhuk 	int ch_stats_len;
1439e05107e6SIvan Khoronzhuk 	int line;
1440e05107e6SIvan Khoronzhuk 	int i;
1441e05107e6SIvan Khoronzhuk 
1442e05107e6SIvan Khoronzhuk 	ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1443e05107e6SIvan Khoronzhuk 	for (i = 0; i < ch_stats_len; i++) {
1444e05107e6SIvan Khoronzhuk 		line = i % CPSW_STATS_CH_LEN;
1445e05107e6SIvan Khoronzhuk 		snprintf(*p, ETH_GSTRING_LEN,
1446bf2ce3fdSFlorian Fainelli 			 "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
1447bf2ce3fdSFlorian Fainelli 			 (long)(i / CPSW_STATS_CH_LEN),
1448e05107e6SIvan Khoronzhuk 			 cpsw_gstrings_ch_stats[line].stat_string);
1449e05107e6SIvan Khoronzhuk 		*p += ETH_GSTRING_LEN;
1450e05107e6SIvan Khoronzhuk 	}
1451e05107e6SIvan Khoronzhuk }
1452e05107e6SIvan Khoronzhuk 
1453d9718546SMugunthan V N static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1454d9718546SMugunthan V N {
1455e05107e6SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1456d9718546SMugunthan V N 	u8 *p = data;
1457d9718546SMugunthan V N 	int i;
1458d9718546SMugunthan V N 
1459d9718546SMugunthan V N 	switch (stringset) {
1460d9718546SMugunthan V N 	case ETH_SS_STATS:
1461e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1462d9718546SMugunthan V N 			memcpy(p, cpsw_gstrings_stats[i].stat_string,
1463d9718546SMugunthan V N 			       ETH_GSTRING_LEN);
1464d9718546SMugunthan V N 			p += ETH_GSTRING_LEN;
1465d9718546SMugunthan V N 		}
1466e05107e6SIvan Khoronzhuk 
1467e05107e6SIvan Khoronzhuk 		cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1468e05107e6SIvan Khoronzhuk 		cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1469d9718546SMugunthan V N 		break;
1470d9718546SMugunthan V N 	}
1471d9718546SMugunthan V N }
1472d9718546SMugunthan V N 
1473d9718546SMugunthan V N static void cpsw_get_ethtool_stats(struct net_device *ndev,
1474d9718546SMugunthan V N 				    struct ethtool_stats *stats, u64 *data)
1475d9718546SMugunthan V N {
1476d9718546SMugunthan V N 	u8 *p;
14772c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1478e05107e6SIvan Khoronzhuk 	struct cpdma_chan_stats ch_stats;
1479e05107e6SIvan Khoronzhuk 	int i, l, ch;
1480d9718546SMugunthan V N 
1481d9718546SMugunthan V N 	/* Collect Davinci CPDMA stats for Rx and Tx Channel */
1482e05107e6SIvan Khoronzhuk 	for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1483e05107e6SIvan Khoronzhuk 		data[l] = readl(cpsw->hw_stats +
1484e05107e6SIvan Khoronzhuk 				cpsw_gstrings_stats[l].stat_offset);
1485d9718546SMugunthan V N 
1486e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
14878feb0a19SIvan Khoronzhuk 		cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
1488e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1489e05107e6SIvan Khoronzhuk 			p = (u8 *)&ch_stats +
1490e05107e6SIvan Khoronzhuk 				cpsw_gstrings_ch_stats[i].stat_offset;
1491e05107e6SIvan Khoronzhuk 			data[l] = *(u32 *)p;
1492e05107e6SIvan Khoronzhuk 		}
1493e05107e6SIvan Khoronzhuk 	}
1494d9718546SMugunthan V N 
1495e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
14968feb0a19SIvan Khoronzhuk 		cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
1497e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1498e05107e6SIvan Khoronzhuk 			p = (u8 *)&ch_stats +
1499e05107e6SIvan Khoronzhuk 				cpsw_gstrings_ch_stats[i].stat_offset;
1500e05107e6SIvan Khoronzhuk 			data[l] = *(u32 *)p;
1501d9718546SMugunthan V N 		}
1502d9718546SMugunthan V N 	}
1503d9718546SMugunthan V N }
1504d9718546SMugunthan V N 
150527e9e103SIvan Khoronzhuk static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1506e05107e6SIvan Khoronzhuk 					struct sk_buff *skb,
1507e05107e6SIvan Khoronzhuk 					struct cpdma_chan *txch)
1508d9ba8f9eSMugunthan V N {
15092c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
15102c836bd9SIvan Khoronzhuk 
151198fdd857SIvan Khoronzhuk 	skb_tx_timestamp(skb);
1512e05107e6SIvan Khoronzhuk 	return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1513606f3993SIvan Khoronzhuk 				 priv->emac_port + cpsw->data.dual_emac);
1514d9ba8f9eSMugunthan V N }
1515d9ba8f9eSMugunthan V N 
1516d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries(
1517d9ba8f9eSMugunthan V N 		struct cpsw_priv *priv, struct cpsw_slave *slave,
1518d9ba8f9eSMugunthan V N 		u32 slave_port)
1519d9ba8f9eSMugunthan V N {
15202a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
152171a2cbb7SGrygorii Strashko 	u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1522d9ba8f9eSMugunthan V N 
15232a05a622SIvan Khoronzhuk 	if (cpsw->version == CPSW_VERSION_1)
1524d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1525d9ba8f9eSMugunthan V N 	else
1526d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
15272a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1528d9ba8f9eSMugunthan V N 			  port_mask, port_mask, 0);
15292a05a622SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
15305b3a5a14SIvan Khoronzhuk 			   ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 0);
15312a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
15322a05a622SIvan Khoronzhuk 			   HOST_PORT_NUM, ALE_VLAN |
15332a05a622SIvan Khoronzhuk 			   ALE_SECURE, slave->port_vlan);
15345e5add17SGrygorii Strashko 	cpsw_ale_control_set(cpsw->ale, slave_port,
15355e5add17SGrygorii Strashko 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1536d9ba8f9eSMugunthan V N }
1537d9ba8f9eSMugunthan V N 
15381e7a2e21SDaniel Mack static void soft_reset_slave(struct cpsw_slave *slave)
1539df828598SMugunthan V N {
1540df828598SMugunthan V N 	char name[32];
15411e7a2e21SDaniel Mack 
15421e7a2e21SDaniel Mack 	snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
15431e7a2e21SDaniel Mack 	soft_reset(name, &slave->sliver->soft_reset);
15441e7a2e21SDaniel Mack }
15451e7a2e21SDaniel Mack 
15461e7a2e21SDaniel Mack static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
15471e7a2e21SDaniel Mack {
1548df828598SMugunthan V N 	u32 slave_port;
154930c57f07SSekhar Nori 	struct phy_device *phy;
1550649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1551df828598SMugunthan V N 
15521e7a2e21SDaniel Mack 	soft_reset_slave(slave);
1553df828598SMugunthan V N 
1554df828598SMugunthan V N 	/* setup priority mapping */
1555dda5f5feSGrygorii Strashko 	writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
15569750a3adSRichard Cochran 
15572a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
15589750a3adSRichard Cochran 	case CPSW_VERSION_1:
15599750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
156048f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
156148f5bcccSGrygorii Strashko 		 * flow control mode
156248f5bcccSGrygorii Strashko 		 */
156348f5bcccSGrygorii Strashko 		slave_write(slave,
156448f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
156548f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
15669750a3adSRichard Cochran 		break;
15679750a3adSRichard Cochran 	case CPSW_VERSION_2:
1568c193f365SMugunthan V N 	case CPSW_VERSION_3:
1569926489beSMugunthan V N 	case CPSW_VERSION_4:
15709750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
157148f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
157248f5bcccSGrygorii Strashko 		 * flow control mode
157348f5bcccSGrygorii Strashko 		 */
157448f5bcccSGrygorii Strashko 		slave_write(slave,
157548f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
157648f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
15779750a3adSRichard Cochran 		break;
15789750a3adSRichard Cochran 	}
1579df828598SMugunthan V N 
1580df828598SMugunthan V N 	/* setup max packet size, and mac address */
1581dda5f5feSGrygorii Strashko 	writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1582df828598SMugunthan V N 	cpsw_set_slave_mac(slave, priv);
1583df828598SMugunthan V N 
1584df828598SMugunthan V N 	slave->mac_control = 0;	/* no link yet */
1585df828598SMugunthan V N 
15866f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
1587df828598SMugunthan V N 
1588606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
1589d9ba8f9eSMugunthan V N 		cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1590d9ba8f9eSMugunthan V N 	else
15912a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1592e11b220fSMugunthan V N 				   1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1593df828598SMugunthan V N 
1594d733f754SDavid Rivshin 	if (slave->data->phy_node) {
159530c57f07SSekhar Nori 		phy = of_phy_connect(priv->ndev, slave->data->phy_node,
15969e42f715SHeiko Schocher 				 &cpsw_adjust_link, 0, slave->data->phy_if);
159730c57f07SSekhar Nori 		if (!phy) {
1598f7ce9103SRob Herring 			dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1599f7ce9103SRob Herring 				slave->data->phy_node,
1600d733f754SDavid Rivshin 				slave->slave_num);
1601d733f754SDavid Rivshin 			return;
1602d733f754SDavid Rivshin 		}
1603d733f754SDavid Rivshin 	} else {
160430c57f07SSekhar Nori 		phy = phy_connect(priv->ndev, slave->data->phy_id,
1605f9a8f83bSFlorian Fainelli 				 &cpsw_adjust_link, slave->data->phy_if);
160630c57f07SSekhar Nori 		if (IS_ERR(phy)) {
1607d733f754SDavid Rivshin 			dev_err(priv->dev,
1608d733f754SDavid Rivshin 				"phy \"%s\" not found on slave %d, err %ld\n",
1609d733f754SDavid Rivshin 				slave->data->phy_id, slave->slave_num,
161030c57f07SSekhar Nori 				PTR_ERR(phy));
1611d733f754SDavid Rivshin 			return;
1612d733f754SDavid Rivshin 		}
1613d733f754SDavid Rivshin 	}
1614d733f754SDavid Rivshin 
161530c57f07SSekhar Nori 	slave->phy = phy;
161630c57f07SSekhar Nori 
16172220943aSAndrew Lunn 	phy_attached_info(slave->phy);
16182220943aSAndrew Lunn 
1619df828598SMugunthan V N 	phy_start(slave->phy);
1620388367a5SMugunthan V N 
1621388367a5SMugunthan V N 	/* Configure GMII_SEL register */
162256e31bd8SIvan Khoronzhuk 	cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
1623df828598SMugunthan V N }
1624df828598SMugunthan V N 
16253b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
16263b72c2feSMugunthan V N {
1627606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1628606f3993SIvan Khoronzhuk 	const int vlan = cpsw->data.default_vlan;
16293b72c2feSMugunthan V N 	u32 reg;
16303b72c2feSMugunthan V N 	int i;
16311e5c4bc4SLennart Sorensen 	int unreg_mcast_mask;
16323b72c2feSMugunthan V N 
16332a05a622SIvan Khoronzhuk 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
16343b72c2feSMugunthan V N 	       CPSW2_PORT_VLAN;
16353b72c2feSMugunthan V N 
16365d8d0d4dSIvan Khoronzhuk 	writel(vlan, &cpsw->host_port_regs->port_vlan);
16373b72c2feSMugunthan V N 
1638606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
1639606f3993SIvan Khoronzhuk 		slave_write(cpsw->slaves + i, vlan, reg);
16403b72c2feSMugunthan V N 
16411e5c4bc4SLennart Sorensen 	if (priv->ndev->flags & IFF_ALLMULTI)
16421e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_ALL_PORTS;
16431e5c4bc4SLennart Sorensen 	else
16441e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
16451e5c4bc4SLennart Sorensen 
16462a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
164761f1cef9SGrygorii Strashko 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
164861f1cef9SGrygorii Strashko 			  unreg_mcast_mask);
16493b72c2feSMugunthan V N }
16503b72c2feSMugunthan V N 
1651df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv)
1652df828598SMugunthan V N {
1653d9ba8f9eSMugunthan V N 	u32 fifo_mode;
16545d8d0d4dSIvan Khoronzhuk 	u32 control_reg;
16555d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
16563b72c2feSMugunthan V N 
1657df828598SMugunthan V N 	/* soft reset the controller and initialize ale */
16585d8d0d4dSIvan Khoronzhuk 	soft_reset("cpsw", &cpsw->regs->soft_reset);
16592a05a622SIvan Khoronzhuk 	cpsw_ale_start(cpsw->ale);
1660df828598SMugunthan V N 
1661df828598SMugunthan V N 	/* switch to vlan unaware mode */
16622a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
16633b72c2feSMugunthan V N 			     CPSW_ALE_VLAN_AWARE);
16645d8d0d4dSIvan Khoronzhuk 	control_reg = readl(&cpsw->regs->control);
1665a3a41d2fSGrygorii Strashko 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
16665d8d0d4dSIvan Khoronzhuk 	writel(control_reg, &cpsw->regs->control);
1667606f3993SIvan Khoronzhuk 	fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1668d9ba8f9eSMugunthan V N 		     CPSW_FIFO_NORMAL_MODE;
16695d8d0d4dSIvan Khoronzhuk 	writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1670df828598SMugunthan V N 
1671df828598SMugunthan V N 	/* setup host port priority mapping */
1672dda5f5feSGrygorii Strashko 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
16735d8d0d4dSIvan Khoronzhuk 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
1674dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1675df828598SMugunthan V N 
16762a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1677df828598SMugunthan V N 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1678df828598SMugunthan V N 
1679606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac) {
16802a05a622SIvan Khoronzhuk 		cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1681d9ba8f9eSMugunthan V N 				   0, 0);
16822a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
168371a2cbb7SGrygorii Strashko 				   ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1684df828598SMugunthan V N 	}
1685d9ba8f9eSMugunthan V N }
1686df828598SMugunthan V N 
16873802dce1SIvan Khoronzhuk static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
16883802dce1SIvan Khoronzhuk {
16893802dce1SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
16903802dce1SIvan Khoronzhuk 	struct sk_buff *skb;
16913802dce1SIvan Khoronzhuk 	int ch_buf_num;
1692e05107e6SIvan Khoronzhuk 	int ch, i, ret;
16933802dce1SIvan Khoronzhuk 
1694e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
16958feb0a19SIvan Khoronzhuk 		ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
16963802dce1SIvan Khoronzhuk 		for (i = 0; i < ch_buf_num; i++) {
16973802dce1SIvan Khoronzhuk 			skb = __netdev_alloc_skb_ip_align(priv->ndev,
16983802dce1SIvan Khoronzhuk 							  cpsw->rx_packet_max,
16993802dce1SIvan Khoronzhuk 							  GFP_KERNEL);
17003802dce1SIvan Khoronzhuk 			if (!skb) {
17013802dce1SIvan Khoronzhuk 				cpsw_err(priv, ifup, "cannot allocate skb\n");
17023802dce1SIvan Khoronzhuk 				return -ENOMEM;
17033802dce1SIvan Khoronzhuk 			}
17043802dce1SIvan Khoronzhuk 
1705e05107e6SIvan Khoronzhuk 			skb_set_queue_mapping(skb, ch);
17068feb0a19SIvan Khoronzhuk 			ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
17078feb0a19SIvan Khoronzhuk 						skb->data, skb_tailroom(skb),
17088feb0a19SIvan Khoronzhuk 						0);
17093802dce1SIvan Khoronzhuk 			if (ret < 0) {
17103802dce1SIvan Khoronzhuk 				cpsw_err(priv, ifup,
1711e05107e6SIvan Khoronzhuk 					 "cannot submit skb to channel %d rx, error %d\n",
1712e05107e6SIvan Khoronzhuk 					 ch, ret);
17133802dce1SIvan Khoronzhuk 				kfree_skb(skb);
17143802dce1SIvan Khoronzhuk 				return ret;
17153802dce1SIvan Khoronzhuk 			}
17163802dce1SIvan Khoronzhuk 			kmemleak_not_leak(skb);
17173802dce1SIvan Khoronzhuk 		}
17183802dce1SIvan Khoronzhuk 
1719e05107e6SIvan Khoronzhuk 		cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1720e05107e6SIvan Khoronzhuk 			  ch, ch_buf_num);
1721e05107e6SIvan Khoronzhuk 	}
17223802dce1SIvan Khoronzhuk 
1723e05107e6SIvan Khoronzhuk 	return 0;
17243802dce1SIvan Khoronzhuk }
17253802dce1SIvan Khoronzhuk 
17262a05a622SIvan Khoronzhuk static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1727aacebbf8SSebastian Siewior {
17283995d265SSchuyler Patton 	u32 slave_port;
17293995d265SSchuyler Patton 
17306f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
17313995d265SSchuyler Patton 
1732aacebbf8SSebastian Siewior 	if (!slave->phy)
1733aacebbf8SSebastian Siewior 		return;
1734aacebbf8SSebastian Siewior 	phy_stop(slave->phy);
1735aacebbf8SSebastian Siewior 	phy_disconnect(slave->phy);
1736aacebbf8SSebastian Siewior 	slave->phy = NULL;
17372a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, slave_port,
17383995d265SSchuyler Patton 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
17391f95ba00SGrygorii Strashko 	soft_reset_slave(slave);
1740aacebbf8SSebastian Siewior }
1741aacebbf8SSebastian Siewior 
17427929a668SIvan Khoronzhuk static int cpsw_tc_to_fifo(int tc, int num_tc)
17437929a668SIvan Khoronzhuk {
17447929a668SIvan Khoronzhuk 	if (tc == num_tc - 1)
17457929a668SIvan Khoronzhuk 		return 0;
17467929a668SIvan Khoronzhuk 
17477929a668SIvan Khoronzhuk 	return CPSW_FIFO_SHAPERS_NUM - tc;
17487929a668SIvan Khoronzhuk }
17497929a668SIvan Khoronzhuk 
175057d90148SIvan Khoronzhuk static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
175157d90148SIvan Khoronzhuk {
175257d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
175357d90148SIvan Khoronzhuk 	u32 val = 0, send_pct, shift;
175457d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
175557d90148SIvan Khoronzhuk 	int pct = 0, i;
175657d90148SIvan Khoronzhuk 
175757d90148SIvan Khoronzhuk 	if (bw > priv->shp_cfg_speed * 1000)
175857d90148SIvan Khoronzhuk 		goto err;
175957d90148SIvan Khoronzhuk 
176057d90148SIvan Khoronzhuk 	/* shaping has to stay enabled for highest fifos linearly
176157d90148SIvan Khoronzhuk 	 * and fifo bw no more then interface can allow
176257d90148SIvan Khoronzhuk 	 */
176357d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
176457d90148SIvan Khoronzhuk 	send_pct = slave_read(slave, SEND_PERCENT);
176557d90148SIvan Khoronzhuk 	for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
176657d90148SIvan Khoronzhuk 		if (!bw) {
176757d90148SIvan Khoronzhuk 			if (i >= fifo || !priv->fifo_bw[i])
176857d90148SIvan Khoronzhuk 				continue;
176957d90148SIvan Khoronzhuk 
177057d90148SIvan Khoronzhuk 			dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
177157d90148SIvan Khoronzhuk 			continue;
177257d90148SIvan Khoronzhuk 		}
177357d90148SIvan Khoronzhuk 
177457d90148SIvan Khoronzhuk 		if (!priv->fifo_bw[i] && i > fifo) {
177557d90148SIvan Khoronzhuk 			dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
177657d90148SIvan Khoronzhuk 			return -EINVAL;
177757d90148SIvan Khoronzhuk 		}
177857d90148SIvan Khoronzhuk 
177957d90148SIvan Khoronzhuk 		shift = (i - 1) * 8;
178057d90148SIvan Khoronzhuk 		if (i == fifo) {
178157d90148SIvan Khoronzhuk 			send_pct &= ~(CPSW_PCT_MASK << shift);
178257d90148SIvan Khoronzhuk 			val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
178357d90148SIvan Khoronzhuk 			if (!val)
178457d90148SIvan Khoronzhuk 				val = 1;
178557d90148SIvan Khoronzhuk 
178657d90148SIvan Khoronzhuk 			send_pct |= val << shift;
178757d90148SIvan Khoronzhuk 			pct += val;
178857d90148SIvan Khoronzhuk 			continue;
178957d90148SIvan Khoronzhuk 		}
179057d90148SIvan Khoronzhuk 
179157d90148SIvan Khoronzhuk 		if (priv->fifo_bw[i])
179257d90148SIvan Khoronzhuk 			pct += (send_pct >> shift) & CPSW_PCT_MASK;
179357d90148SIvan Khoronzhuk 	}
179457d90148SIvan Khoronzhuk 
179557d90148SIvan Khoronzhuk 	if (pct >= 100)
179657d90148SIvan Khoronzhuk 		goto err;
179757d90148SIvan Khoronzhuk 
179857d90148SIvan Khoronzhuk 	slave_write(slave, send_pct, SEND_PERCENT);
179957d90148SIvan Khoronzhuk 	priv->fifo_bw[fifo] = bw;
180057d90148SIvan Khoronzhuk 
180157d90148SIvan Khoronzhuk 	dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
180257d90148SIvan Khoronzhuk 		 DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
180357d90148SIvan Khoronzhuk 
180457d90148SIvan Khoronzhuk 	return 0;
180557d90148SIvan Khoronzhuk err:
180657d90148SIvan Khoronzhuk 	dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
180757d90148SIvan Khoronzhuk 	return -EINVAL;
180857d90148SIvan Khoronzhuk }
180957d90148SIvan Khoronzhuk 
181057d90148SIvan Khoronzhuk static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
181157d90148SIvan Khoronzhuk {
181257d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
181357d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
181457d90148SIvan Khoronzhuk 	u32 tx_in_ctl_rg, val;
181557d90148SIvan Khoronzhuk 	int ret;
181657d90148SIvan Khoronzhuk 
181757d90148SIvan Khoronzhuk 	ret = cpsw_set_fifo_bw(priv, fifo, bw);
181857d90148SIvan Khoronzhuk 	if (ret)
181957d90148SIvan Khoronzhuk 		return ret;
182057d90148SIvan Khoronzhuk 
182157d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
182257d90148SIvan Khoronzhuk 	tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
182357d90148SIvan Khoronzhuk 		       CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
182457d90148SIvan Khoronzhuk 
182557d90148SIvan Khoronzhuk 	if (!bw)
182657d90148SIvan Khoronzhuk 		cpsw_fifo_shp_on(priv, fifo, bw);
182757d90148SIvan Khoronzhuk 
182857d90148SIvan Khoronzhuk 	val = slave_read(slave, tx_in_ctl_rg);
182957d90148SIvan Khoronzhuk 	if (cpsw_shp_is_off(priv)) {
183057d90148SIvan Khoronzhuk 		/* disable FIFOs rate limited queues */
183157d90148SIvan Khoronzhuk 		val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
183257d90148SIvan Khoronzhuk 
183357d90148SIvan Khoronzhuk 		/* set type of FIFO queues to normal priority mode */
183457d90148SIvan Khoronzhuk 		val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
183557d90148SIvan Khoronzhuk 
183657d90148SIvan Khoronzhuk 		/* set type of FIFO queues to be rate limited */
183757d90148SIvan Khoronzhuk 		if (bw)
183857d90148SIvan Khoronzhuk 			val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
183957d90148SIvan Khoronzhuk 		else
184057d90148SIvan Khoronzhuk 			priv->shp_cfg_speed = 0;
184157d90148SIvan Khoronzhuk 	}
184257d90148SIvan Khoronzhuk 
184357d90148SIvan Khoronzhuk 	/* toggle a FIFO rate limited queue */
184457d90148SIvan Khoronzhuk 	if (bw)
184557d90148SIvan Khoronzhuk 		val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
184657d90148SIvan Khoronzhuk 	else
184757d90148SIvan Khoronzhuk 		val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
184857d90148SIvan Khoronzhuk 	slave_write(slave, val, tx_in_ctl_rg);
184957d90148SIvan Khoronzhuk 
185057d90148SIvan Khoronzhuk 	/* FIFO transmit shape enable */
185157d90148SIvan Khoronzhuk 	cpsw_fifo_shp_on(priv, fifo, bw);
185257d90148SIvan Khoronzhuk 	return 0;
185357d90148SIvan Khoronzhuk }
185457d90148SIvan Khoronzhuk 
185557d90148SIvan Khoronzhuk /* Defaults:
185657d90148SIvan Khoronzhuk  * class A - prio 3
185757d90148SIvan Khoronzhuk  * class B - prio 2
185857d90148SIvan Khoronzhuk  * shaping for class A should be set first
185957d90148SIvan Khoronzhuk  */
186057d90148SIvan Khoronzhuk static int cpsw_set_cbs(struct net_device *ndev,
186157d90148SIvan Khoronzhuk 			struct tc_cbs_qopt_offload *qopt)
186257d90148SIvan Khoronzhuk {
186357d90148SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
186457d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
186557d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
186657d90148SIvan Khoronzhuk 	int prev_speed = 0;
186757d90148SIvan Khoronzhuk 	int tc, ret, fifo;
186857d90148SIvan Khoronzhuk 	u32 bw = 0;
186957d90148SIvan Khoronzhuk 
187057d90148SIvan Khoronzhuk 	tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
187157d90148SIvan Khoronzhuk 
187257d90148SIvan Khoronzhuk 	/* enable channels in backward order, as highest FIFOs must be rate
187357d90148SIvan Khoronzhuk 	 * limited first and for compliance with CPDMA rate limited channels
187457d90148SIvan Khoronzhuk 	 * that also used in bacward order. FIFO0 cannot be rate limited.
187557d90148SIvan Khoronzhuk 	 */
187657d90148SIvan Khoronzhuk 	fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
187757d90148SIvan Khoronzhuk 	if (!fifo) {
187857d90148SIvan Khoronzhuk 		dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
187957d90148SIvan Khoronzhuk 		return -EINVAL;
188057d90148SIvan Khoronzhuk 	}
188157d90148SIvan Khoronzhuk 
188257d90148SIvan Khoronzhuk 	/* do nothing, it's disabled anyway */
188357d90148SIvan Khoronzhuk 	if (!qopt->enable && !priv->fifo_bw[fifo])
188457d90148SIvan Khoronzhuk 		return 0;
188557d90148SIvan Khoronzhuk 
188657d90148SIvan Khoronzhuk 	/* shapers can be set if link speed is known */
188757d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
188857d90148SIvan Khoronzhuk 	if (slave->phy && slave->phy->link) {
188957d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
189057d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed)
189157d90148SIvan Khoronzhuk 			prev_speed = priv->shp_cfg_speed;
189257d90148SIvan Khoronzhuk 
189357d90148SIvan Khoronzhuk 		priv->shp_cfg_speed = slave->phy->speed;
189457d90148SIvan Khoronzhuk 	}
189557d90148SIvan Khoronzhuk 
189657d90148SIvan Khoronzhuk 	if (!priv->shp_cfg_speed) {
189757d90148SIvan Khoronzhuk 		dev_err(priv->dev, "Link speed is not known");
189857d90148SIvan Khoronzhuk 		return -1;
189957d90148SIvan Khoronzhuk 	}
190057d90148SIvan Khoronzhuk 
190157d90148SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
190257d90148SIvan Khoronzhuk 	if (ret < 0) {
190357d90148SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
190457d90148SIvan Khoronzhuk 		return ret;
190557d90148SIvan Khoronzhuk 	}
190657d90148SIvan Khoronzhuk 
190757d90148SIvan Khoronzhuk 	bw = qopt->enable ? qopt->idleslope : 0;
190857d90148SIvan Khoronzhuk 	ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
190957d90148SIvan Khoronzhuk 	if (ret) {
191057d90148SIvan Khoronzhuk 		priv->shp_cfg_speed = prev_speed;
191157d90148SIvan Khoronzhuk 		prev_speed = 0;
191257d90148SIvan Khoronzhuk 	}
191357d90148SIvan Khoronzhuk 
191457d90148SIvan Khoronzhuk 	if (bw && prev_speed)
191557d90148SIvan Khoronzhuk 		dev_warn(priv->dev,
191657d90148SIvan Khoronzhuk 			 "Speed was changed, CBS shaper speeds are changed!");
191757d90148SIvan Khoronzhuk 
191857d90148SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
191957d90148SIvan Khoronzhuk 	return ret;
192057d90148SIvan Khoronzhuk }
192157d90148SIvan Khoronzhuk 
19224b4255edSIvan Khoronzhuk static void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
19234b4255edSIvan Khoronzhuk {
19244b4255edSIvan Khoronzhuk 	int fifo, bw;
19254b4255edSIvan Khoronzhuk 
19264b4255edSIvan Khoronzhuk 	for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
19274b4255edSIvan Khoronzhuk 		bw = priv->fifo_bw[fifo];
19284b4255edSIvan Khoronzhuk 		if (!bw)
19294b4255edSIvan Khoronzhuk 			continue;
19304b4255edSIvan Khoronzhuk 
19314b4255edSIvan Khoronzhuk 		cpsw_set_fifo_rlimit(priv, fifo, bw);
19324b4255edSIvan Khoronzhuk 	}
19334b4255edSIvan Khoronzhuk }
19344b4255edSIvan Khoronzhuk 
19354b4255edSIvan Khoronzhuk static void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
19364b4255edSIvan Khoronzhuk {
19374b4255edSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
19384b4255edSIvan Khoronzhuk 	u32 tx_prio_map = 0;
19394b4255edSIvan Khoronzhuk 	int i, tc, fifo;
19404b4255edSIvan Khoronzhuk 	u32 tx_prio_rg;
19414b4255edSIvan Khoronzhuk 
19424b4255edSIvan Khoronzhuk 	if (!priv->mqprio_hw)
19434b4255edSIvan Khoronzhuk 		return;
19444b4255edSIvan Khoronzhuk 
19454b4255edSIvan Khoronzhuk 	for (i = 0; i < 8; i++) {
19464b4255edSIvan Khoronzhuk 		tc = netdev_get_prio_tc_map(priv->ndev, i);
19474b4255edSIvan Khoronzhuk 		fifo = CPSW_FIFO_SHAPERS_NUM - tc;
19484b4255edSIvan Khoronzhuk 		tx_prio_map |= fifo << (4 * i);
19494b4255edSIvan Khoronzhuk 	}
19504b4255edSIvan Khoronzhuk 
19514b4255edSIvan Khoronzhuk 	tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
19524b4255edSIvan Khoronzhuk 		     CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
19534b4255edSIvan Khoronzhuk 
19544b4255edSIvan Khoronzhuk 	slave_write(slave, tx_prio_map, tx_prio_rg);
19554b4255edSIvan Khoronzhuk }
19564b4255edSIvan Khoronzhuk 
1957*00fe4712SIvan Khoronzhuk static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
1958*00fe4712SIvan Khoronzhuk {
1959*00fe4712SIvan Khoronzhuk 	struct cpsw_priv *priv = arg;
1960*00fe4712SIvan Khoronzhuk 
1961*00fe4712SIvan Khoronzhuk 	if (!vdev)
1962*00fe4712SIvan Khoronzhuk 		return 0;
1963*00fe4712SIvan Khoronzhuk 
1964*00fe4712SIvan Khoronzhuk 	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
1965*00fe4712SIvan Khoronzhuk 	return 0;
1966*00fe4712SIvan Khoronzhuk }
1967*00fe4712SIvan Khoronzhuk 
19684b4255edSIvan Khoronzhuk /* restore resources after port reset */
19694b4255edSIvan Khoronzhuk static void cpsw_restore(struct cpsw_priv *priv)
19704b4255edSIvan Khoronzhuk {
1971*00fe4712SIvan Khoronzhuk 	/* restore vlan configurations */
1972*00fe4712SIvan Khoronzhuk 	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
1973*00fe4712SIvan Khoronzhuk 
19744b4255edSIvan Khoronzhuk 	/* restore MQPRIO offload */
19754b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_mqprio_resume, priv);
19764b4255edSIvan Khoronzhuk 
19774b4255edSIvan Khoronzhuk 	/* restore CBS offload */
19784b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_cbs_resume, priv);
19794b4255edSIvan Khoronzhuk }
19804b4255edSIvan Khoronzhuk 
1981df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev)
1982df828598SMugunthan V N {
1983df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1984649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
19853802dce1SIvan Khoronzhuk 	int ret;
1986df828598SMugunthan V N 	u32 reg;
1987df828598SMugunthan V N 
198856e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1989108a6537SGrygorii Strashko 	if (ret < 0) {
199056e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1991108a6537SGrygorii Strashko 		return ret;
1992108a6537SGrygorii Strashko 	}
19933fa88c51SGrygorii Strashko 
1994df828598SMugunthan V N 	netif_carrier_off(ndev);
1995df828598SMugunthan V N 
1996e05107e6SIvan Khoronzhuk 	/* Notify the stack of the actual queue counts. */
1997e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1998e05107e6SIvan Khoronzhuk 	if (ret) {
1999e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of tx queues\n");
2000e05107e6SIvan Khoronzhuk 		goto err_cleanup;
2001e05107e6SIvan Khoronzhuk 	}
2002e05107e6SIvan Khoronzhuk 
2003e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
2004e05107e6SIvan Khoronzhuk 	if (ret) {
2005e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of rx queues\n");
2006e05107e6SIvan Khoronzhuk 		goto err_cleanup;
2007e05107e6SIvan Khoronzhuk 	}
2008e05107e6SIvan Khoronzhuk 
20092a05a622SIvan Khoronzhuk 	reg = cpsw->version;
2010df828598SMugunthan V N 
2011df828598SMugunthan V N 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
2012df828598SMugunthan V N 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
2013df828598SMugunthan V N 		 CPSW_RTL_VERSION(reg));
2014df828598SMugunthan V N 
2015d5bc1613SIvan Khoronzhuk 	/* Initialize host and slave ports */
2016d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count)
2017df828598SMugunthan V N 		cpsw_init_host_port(priv);
2018df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_open, priv);
2019df828598SMugunthan V N 
20203b72c2feSMugunthan V N 	/* Add default VLAN */
2021606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac)
20223b72c2feSMugunthan V N 		cpsw_add_default_vlan(priv);
2023e6afea0bSMugunthan V N 	else
20242a05a622SIvan Khoronzhuk 		cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
202561f1cef9SGrygorii Strashko 				  ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
20263b72c2feSMugunthan V N 
2027d5bc1613SIvan Khoronzhuk 	/* initialize shared resources for every ndev */
2028d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count) {
2029d9ba8f9eSMugunthan V N 		/* disable priority elevation */
2030dda5f5feSGrygorii Strashko 		writel_relaxed(0, &cpsw->regs->ptype);
2031df828598SMugunthan V N 
2032d9ba8f9eSMugunthan V N 		/* enable statistics collection only on all ports */
2033dda5f5feSGrygorii Strashko 		writel_relaxed(0x7, &cpsw->regs->stat_port_en);
2034df828598SMugunthan V N 
20351923d6e4SMugunthan V N 		/* Enable internal fifo flow control */
20365d8d0d4dSIvan Khoronzhuk 		writel(0x7, &cpsw->regs->flow_control);
20371923d6e4SMugunthan V N 
2038dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_rx);
2039dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_tx);
2040d354eb85SMugunthan V N 
2041e38b5a3dSIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
2042e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
2043e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
20447da11600SMugunthan V N 		}
20457da11600SMugunthan V N 
2046e38b5a3dSIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
2047e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
2048e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
20497da11600SMugunthan V N 		}
20507da11600SMugunthan V N 
20513802dce1SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
20523802dce1SIvan Khoronzhuk 		if (ret < 0)
2053aacebbf8SSebastian Siewior 			goto err_cleanup;
2054f280e89aSMugunthan V N 
20558a2c9a5aSGrygorii Strashko 		if (cpts_register(cpsw->cpts))
2056f280e89aSMugunthan V N 			dev_err(priv->dev, "error registering cpts device\n");
2057f280e89aSMugunthan V N 
2058d9ba8f9eSMugunthan V N 	}
2059df828598SMugunthan V N 
20604b4255edSIvan Khoronzhuk 	cpsw_restore(priv);
20614b4255edSIvan Khoronzhuk 
2062ff5b8ef2SMugunthan V N 	/* Enable Interrupt pacing if configured */
20632a05a622SIvan Khoronzhuk 	if (cpsw->coal_intvl != 0) {
2064ff5b8ef2SMugunthan V N 		struct ethtool_coalesce coal;
2065ff5b8ef2SMugunthan V N 
20662a05a622SIvan Khoronzhuk 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
2067ff5b8ef2SMugunthan V N 		cpsw_set_coalesce(ndev, &coal);
2068ff5b8ef2SMugunthan V N 	}
2069ff5b8ef2SMugunthan V N 
20702c836bd9SIvan Khoronzhuk 	cpdma_ctlr_start(cpsw->dma);
20712c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
2072d5bc1613SIvan Khoronzhuk 	cpsw->usage_count++;
2073f63a975eSMugunthan V N 
2074df828598SMugunthan V N 	return 0;
2075df828598SMugunthan V N 
2076aacebbf8SSebastian Siewior err_cleanup:
20772c836bd9SIvan Khoronzhuk 	cpdma_ctlr_stop(cpsw->dma);
20782a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
207956e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
2080aacebbf8SSebastian Siewior 	netif_carrier_off(priv->ndev);
2081aacebbf8SSebastian Siewior 	return ret;
2082df828598SMugunthan V N }
2083df828598SMugunthan V N 
2084df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev)
2085df828598SMugunthan V N {
2086df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2087649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2088df828598SMugunthan V N 
2089df828598SMugunthan V N 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
209015180ecaSIvan Khoronzhuk 	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
2091e05107e6SIvan Khoronzhuk 	netif_tx_stop_all_queues(priv->ndev);
2092df828598SMugunthan V N 	netif_carrier_off(priv->ndev);
2093d9ba8f9eSMugunthan V N 
2094d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count <= 1) {
2095dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_rx);
2096dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_tx);
20972a05a622SIvan Khoronzhuk 		cpts_unregister(cpsw->cpts);
20982c836bd9SIvan Khoronzhuk 		cpsw_intr_disable(cpsw);
20992c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
21002a05a622SIvan Khoronzhuk 		cpsw_ale_stop(cpsw->ale);
2101d9ba8f9eSMugunthan V N 	}
21022a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
21030be01b8eSIvan Khoronzhuk 
21040be01b8eSIvan Khoronzhuk 	if (cpsw_need_resplit(cpsw))
21050be01b8eSIvan Khoronzhuk 		cpsw_split_res(ndev);
21060be01b8eSIvan Khoronzhuk 
2107d5bc1613SIvan Khoronzhuk 	cpsw->usage_count--;
210856e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
2109df828598SMugunthan V N 	return 0;
2110df828598SMugunthan V N }
2111df828598SMugunthan V N 
2112df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
2113df828598SMugunthan V N 				       struct net_device *ndev)
2114df828598SMugunthan V N {
2115df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
21162c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2117f44f8417SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
2118e05107e6SIvan Khoronzhuk 	struct netdev_queue *txq;
2119e05107e6SIvan Khoronzhuk 	struct cpdma_chan *txch;
2120e05107e6SIvan Khoronzhuk 	int ret, q_idx;
2121df828598SMugunthan V N 
2122df828598SMugunthan V N 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
2123df828598SMugunthan V N 		cpsw_err(priv, tx_err, "packet pad failed\n");
21248dc43ddcSTobias Klauser 		ndev->stats.tx_dropped++;
21251bf96050SIvan Khoronzhuk 		return NET_XMIT_DROP;
2126df828598SMugunthan V N 	}
2127df828598SMugunthan V N 
21289232b16dSMugunthan V N 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
2129f44f8417SIvan Khoronzhuk 	    cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
21302e5b38abSRichard Cochran 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
21312e5b38abSRichard Cochran 
2132e05107e6SIvan Khoronzhuk 	q_idx = skb_get_queue_mapping(skb);
2133e05107e6SIvan Khoronzhuk 	if (q_idx >= cpsw->tx_ch_num)
2134e05107e6SIvan Khoronzhuk 		q_idx = q_idx % cpsw->tx_ch_num;
2135e05107e6SIvan Khoronzhuk 
21368feb0a19SIvan Khoronzhuk 	txch = cpsw->txv[q_idx].ch;
213762f94c21SGrygorii Strashko 	txq = netdev_get_tx_queue(ndev, q_idx);
2138e05107e6SIvan Khoronzhuk 	ret = cpsw_tx_packet_submit(priv, skb, txch);
2139df828598SMugunthan V N 	if (unlikely(ret != 0)) {
2140df828598SMugunthan V N 		cpsw_err(priv, tx_err, "desc submit failed\n");
2141df828598SMugunthan V N 		goto fail;
2142df828598SMugunthan V N 	}
2143df828598SMugunthan V N 
2144fae50823SMugunthan V N 	/* If there is no more tx desc left free then we need to
2145fae50823SMugunthan V N 	 * tell the kernel to stop sending us tx frames.
2146fae50823SMugunthan V N 	 */
2147e05107e6SIvan Khoronzhuk 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
2148e05107e6SIvan Khoronzhuk 		netif_tx_stop_queue(txq);
214962f94c21SGrygorii Strashko 
215062f94c21SGrygorii Strashko 		/* Barrier, so that stop_queue visible to other cpus */
215162f94c21SGrygorii Strashko 		smp_mb__after_atomic();
215262f94c21SGrygorii Strashko 
215362f94c21SGrygorii Strashko 		if (cpdma_check_free_tx_desc(txch))
215462f94c21SGrygorii Strashko 			netif_tx_wake_queue(txq);
2155e05107e6SIvan Khoronzhuk 	}
2156fae50823SMugunthan V N 
2157df828598SMugunthan V N 	return NETDEV_TX_OK;
2158df828598SMugunthan V N fail:
21598dc43ddcSTobias Klauser 	ndev->stats.tx_dropped++;
2160e05107e6SIvan Khoronzhuk 	netif_tx_stop_queue(txq);
216162f94c21SGrygorii Strashko 
216262f94c21SGrygorii Strashko 	/* Barrier, so that stop_queue visible to other cpus */
216362f94c21SGrygorii Strashko 	smp_mb__after_atomic();
216462f94c21SGrygorii Strashko 
216562f94c21SGrygorii Strashko 	if (cpdma_check_free_tx_desc(txch))
216662f94c21SGrygorii Strashko 		netif_tx_wake_queue(txq);
216762f94c21SGrygorii Strashko 
2168df828598SMugunthan V N 	return NETDEV_TX_BUSY;
2169df828598SMugunthan V N }
2170df828598SMugunthan V N 
2171c8395d4eSGrygorii Strashko #if IS_ENABLED(CONFIG_TI_CPTS)
21722e5b38abSRichard Cochran 
21732a05a622SIvan Khoronzhuk static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
21742e5b38abSRichard Cochran {
2175606f3993SIvan Khoronzhuk 	struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
21762e5b38abSRichard Cochran 	u32 ts_en, seq_id;
21772e5b38abSRichard Cochran 
2178b63ba58eSGrygorii Strashko 	if (!cpts_is_tx_enabled(cpsw->cpts) &&
2179b63ba58eSGrygorii Strashko 	    !cpts_is_rx_enabled(cpsw->cpts)) {
21802e5b38abSRichard Cochran 		slave_write(slave, 0, CPSW1_TS_CTL);
21812e5b38abSRichard Cochran 		return;
21822e5b38abSRichard Cochran 	}
21832e5b38abSRichard Cochran 
21842e5b38abSRichard Cochran 	seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
21852e5b38abSRichard Cochran 	ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
21862e5b38abSRichard Cochran 
2187b63ba58eSGrygorii Strashko 	if (cpts_is_tx_enabled(cpsw->cpts))
21882e5b38abSRichard Cochran 		ts_en |= CPSW_V1_TS_TX_EN;
21892e5b38abSRichard Cochran 
2190b63ba58eSGrygorii Strashko 	if (cpts_is_rx_enabled(cpsw->cpts))
21912e5b38abSRichard Cochran 		ts_en |= CPSW_V1_TS_RX_EN;
21922e5b38abSRichard Cochran 
21932e5b38abSRichard Cochran 	slave_write(slave, ts_en, CPSW1_TS_CTL);
21942e5b38abSRichard Cochran 	slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
21952e5b38abSRichard Cochran }
21962e5b38abSRichard Cochran 
21972e5b38abSRichard Cochran static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
21982e5b38abSRichard Cochran {
2199d9ba8f9eSMugunthan V N 	struct cpsw_slave *slave;
22005d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
22012e5b38abSRichard Cochran 	u32 ctrl, mtype;
22022e5b38abSRichard Cochran 
2203cb7d78d0SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
2204d9ba8f9eSMugunthan V N 
22052e5b38abSRichard Cochran 	ctrl = slave_read(slave, CPSW2_CONTROL);
22062a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
220709c55372SGeorge Cherian 	case CPSW_VERSION_2:
220809c55372SGeorge Cherian 		ctrl &= ~CTRL_V2_ALL_TS_MASK;
22092e5b38abSRichard Cochran 
2210b63ba58eSGrygorii Strashko 		if (cpts_is_tx_enabled(cpsw->cpts))
221109c55372SGeorge Cherian 			ctrl |= CTRL_V2_TX_TS_BITS;
22122e5b38abSRichard Cochran 
2213b63ba58eSGrygorii Strashko 		if (cpts_is_rx_enabled(cpsw->cpts))
221409c55372SGeorge Cherian 			ctrl |= CTRL_V2_RX_TS_BITS;
221509c55372SGeorge Cherian 		break;
221609c55372SGeorge Cherian 	case CPSW_VERSION_3:
221709c55372SGeorge Cherian 	default:
221809c55372SGeorge Cherian 		ctrl &= ~CTRL_V3_ALL_TS_MASK;
221909c55372SGeorge Cherian 
2220b63ba58eSGrygorii Strashko 		if (cpts_is_tx_enabled(cpsw->cpts))
222109c55372SGeorge Cherian 			ctrl |= CTRL_V3_TX_TS_BITS;
222209c55372SGeorge Cherian 
2223b63ba58eSGrygorii Strashko 		if (cpts_is_rx_enabled(cpsw->cpts))
222409c55372SGeorge Cherian 			ctrl |= CTRL_V3_RX_TS_BITS;
222509c55372SGeorge Cherian 		break;
222609c55372SGeorge Cherian 	}
22272e5b38abSRichard Cochran 
22282e5b38abSRichard Cochran 	mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
22292e5b38abSRichard Cochran 
22302e5b38abSRichard Cochran 	slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
22312e5b38abSRichard Cochran 	slave_write(slave, ctrl, CPSW2_CONTROL);
2232dda5f5feSGrygorii Strashko 	writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
22332e5b38abSRichard Cochran }
22342e5b38abSRichard Cochran 
2235a5b4145bSBen Hutchings static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
22362e5b38abSRichard Cochran {
22373177bf6fSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(dev);
22382e5b38abSRichard Cochran 	struct hwtstamp_config cfg;
22392a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
22402a05a622SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
22412e5b38abSRichard Cochran 
22422a05a622SIvan Khoronzhuk 	if (cpsw->version != CPSW_VERSION_1 &&
22432a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_2 &&
22442a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_3)
22452ee91e54SBen Hutchings 		return -EOPNOTSUPP;
22462ee91e54SBen Hutchings 
22472e5b38abSRichard Cochran 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
22482e5b38abSRichard Cochran 		return -EFAULT;
22492e5b38abSRichard Cochran 
22502e5b38abSRichard Cochran 	/* reserved for future extensions */
22512e5b38abSRichard Cochran 	if (cfg.flags)
22522e5b38abSRichard Cochran 		return -EINVAL;
22532e5b38abSRichard Cochran 
22542ee91e54SBen Hutchings 	if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
22552e5b38abSRichard Cochran 		return -ERANGE;
22562e5b38abSRichard Cochran 
22572e5b38abSRichard Cochran 	switch (cfg.rx_filter) {
22582e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_NONE:
2259b63ba58eSGrygorii Strashko 		cpts_rx_enable(cpts, 0);
22602e5b38abSRichard Cochran 		break;
22612e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_ALL:
2262e9523a5aSGrygorii Strashko 	case HWTSTAMP_FILTER_NTP_ALL:
2263e9523a5aSGrygorii Strashko 		return -ERANGE;
22642e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
22652e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
22662e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2267e9523a5aSGrygorii Strashko 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
2268e9523a5aSGrygorii Strashko 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
2269e9523a5aSGrygorii Strashko 		break;
22702e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
22712e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
22722e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
22732e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
22742e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
22752e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
22762e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
22772e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
22782e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2279e9523a5aSGrygorii Strashko 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
22802e5b38abSRichard Cochran 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
22812e5b38abSRichard Cochran 		break;
22822e5b38abSRichard Cochran 	default:
22832e5b38abSRichard Cochran 		return -ERANGE;
22842e5b38abSRichard Cochran 	}
22852e5b38abSRichard Cochran 
2286b63ba58eSGrygorii Strashko 	cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
22872ee91e54SBen Hutchings 
22882a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
22892e5b38abSRichard Cochran 	case CPSW_VERSION_1:
22902a05a622SIvan Khoronzhuk 		cpsw_hwtstamp_v1(cpsw);
22912e5b38abSRichard Cochran 		break;
22922e5b38abSRichard Cochran 	case CPSW_VERSION_2:
2293f7d403cbSGeorge Cherian 	case CPSW_VERSION_3:
22942e5b38abSRichard Cochran 		cpsw_hwtstamp_v2(priv);
22952e5b38abSRichard Cochran 		break;
22962e5b38abSRichard Cochran 	default:
22972ee91e54SBen Hutchings 		WARN_ON(1);
22982e5b38abSRichard Cochran 	}
22992e5b38abSRichard Cochran 
23002e5b38abSRichard Cochran 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
23012e5b38abSRichard Cochran }
23022e5b38abSRichard Cochran 
2303a5b4145bSBen Hutchings static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2304a5b4145bSBen Hutchings {
23052a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(dev);
23062a05a622SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
2307a5b4145bSBen Hutchings 	struct hwtstamp_config cfg;
2308a5b4145bSBen Hutchings 
23092a05a622SIvan Khoronzhuk 	if (cpsw->version != CPSW_VERSION_1 &&
23102a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_2 &&
23112a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_3)
2312a5b4145bSBen Hutchings 		return -EOPNOTSUPP;
2313a5b4145bSBen Hutchings 
2314a5b4145bSBen Hutchings 	cfg.flags = 0;
2315b63ba58eSGrygorii Strashko 	cfg.tx_type = cpts_is_tx_enabled(cpts) ?
2316b63ba58eSGrygorii Strashko 		      HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
2317b63ba58eSGrygorii Strashko 	cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
2318e9523a5aSGrygorii Strashko 			 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
2319a5b4145bSBen Hutchings 
2320a5b4145bSBen Hutchings 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
2321a5b4145bSBen Hutchings }
2322c8395d4eSGrygorii Strashko #else
2323c8395d4eSGrygorii Strashko static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2324c8395d4eSGrygorii Strashko {
2325c8395d4eSGrygorii Strashko 	return -EOPNOTSUPP;
2326c8395d4eSGrygorii Strashko }
2327a5b4145bSBen Hutchings 
2328c8395d4eSGrygorii Strashko static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2329c8395d4eSGrygorii Strashko {
2330c8395d4eSGrygorii Strashko 	return -EOPNOTSUPP;
2331c8395d4eSGrygorii Strashko }
23322e5b38abSRichard Cochran #endif /*CONFIG_TI_CPTS*/
23332e5b38abSRichard Cochran 
23342e5b38abSRichard Cochran static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
23352e5b38abSRichard Cochran {
233611f2c988SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(dev);
2337606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2338606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
233911f2c988SMugunthan V N 
23402e5b38abSRichard Cochran 	if (!netif_running(dev))
23412e5b38abSRichard Cochran 		return -EINVAL;
23422e5b38abSRichard Cochran 
234311f2c988SMugunthan V N 	switch (cmd) {
234411f2c988SMugunthan V N 	case SIOCSHWTSTAMP:
2345a5b4145bSBen Hutchings 		return cpsw_hwtstamp_set(dev, req);
2346a5b4145bSBen Hutchings 	case SIOCGHWTSTAMP:
2347a5b4145bSBen Hutchings 		return cpsw_hwtstamp_get(dev, req);
23482e5b38abSRichard Cochran 	}
23492e5b38abSRichard Cochran 
2350606f3993SIvan Khoronzhuk 	if (!cpsw->slaves[slave_no].phy)
2351c1b59947SStefan Sørensen 		return -EOPNOTSUPP;
2352606f3993SIvan Khoronzhuk 	return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
235311f2c988SMugunthan V N }
235411f2c988SMugunthan V N 
2355df828598SMugunthan V N static void cpsw_ndo_tx_timeout(struct net_device *ndev)
2356df828598SMugunthan V N {
2357df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
23582c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2359e05107e6SIvan Khoronzhuk 	int ch;
2360df828598SMugunthan V N 
2361df828598SMugunthan V N 	cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
23628dc43ddcSTobias Klauser 	ndev->stats.tx_errors++;
23632c836bd9SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2364e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
23658feb0a19SIvan Khoronzhuk 		cpdma_chan_stop(cpsw->txv[ch].ch);
23668feb0a19SIvan Khoronzhuk 		cpdma_chan_start(cpsw->txv[ch].ch);
2367e05107e6SIvan Khoronzhuk 	}
2368e05107e6SIvan Khoronzhuk 
23692c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
237075514b66SGrygorii Strashko 	netif_trans_update(ndev);
237175514b66SGrygorii Strashko 	netif_tx_wake_all_queues(ndev);
2372df828598SMugunthan V N }
2373df828598SMugunthan V N 
2374dcfd8d58SMugunthan V N static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
2375dcfd8d58SMugunthan V N {
2376dcfd8d58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2377dcfd8d58SMugunthan V N 	struct sockaddr *addr = (struct sockaddr *)p;
2378649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2379dcfd8d58SMugunthan V N 	int flags = 0;
2380dcfd8d58SMugunthan V N 	u16 vid = 0;
2381a6c5d14fSGrygorii Strashko 	int ret;
2382dcfd8d58SMugunthan V N 
2383dcfd8d58SMugunthan V N 	if (!is_valid_ether_addr(addr->sa_data))
2384dcfd8d58SMugunthan V N 		return -EADDRNOTAVAIL;
2385dcfd8d58SMugunthan V N 
238656e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2387a6c5d14fSGrygorii Strashko 	if (ret < 0) {
238856e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2389a6c5d14fSGrygorii Strashko 		return ret;
2390a6c5d14fSGrygorii Strashko 	}
2391a6c5d14fSGrygorii Strashko 
2392606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
2393606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
2394dcfd8d58SMugunthan V N 		flags = ALE_VLAN;
2395dcfd8d58SMugunthan V N 	}
2396dcfd8d58SMugunthan V N 
23972a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
2398dcfd8d58SMugunthan V N 			   flags, vid);
23992a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
2400dcfd8d58SMugunthan V N 			   flags, vid);
2401dcfd8d58SMugunthan V N 
2402dcfd8d58SMugunthan V N 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
2403dcfd8d58SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2404dcfd8d58SMugunthan V N 	for_each_slave(priv, cpsw_set_slave_mac, priv);
2405dcfd8d58SMugunthan V N 
240656e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2407a6c5d14fSGrygorii Strashko 
2408dcfd8d58SMugunthan V N 	return 0;
2409dcfd8d58SMugunthan V N }
2410dcfd8d58SMugunthan V N 
2411df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
2412df828598SMugunthan V N static void cpsw_ndo_poll_controller(struct net_device *ndev)
2413df828598SMugunthan V N {
2414dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2415df828598SMugunthan V N 
2416dbc4ec52SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2417dbc4ec52SIvan Khoronzhuk 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
2418dbc4ec52SIvan Khoronzhuk 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
2419dbc4ec52SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
2420df828598SMugunthan V N }
2421df828598SMugunthan V N #endif
2422df828598SMugunthan V N 
24233b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
24243b72c2feSMugunthan V N 				unsigned short vid)
24253b72c2feSMugunthan V N {
24263b72c2feSMugunthan V N 	int ret;
24279f6bd8faSMugunthan V N 	int unreg_mcast_mask = 0;
24285b3a5a14SIvan Khoronzhuk 	int mcast_mask;
24299f6bd8faSMugunthan V N 	u32 port_mask;
2430606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
24319f6bd8faSMugunthan V N 
2432606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
24339f6bd8faSMugunthan V N 		port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
24349f6bd8faSMugunthan V N 
24355b3a5a14SIvan Khoronzhuk 		mcast_mask = ALE_PORT_HOST;
24369f6bd8faSMugunthan V N 		if (priv->ndev->flags & IFF_ALLMULTI)
24375b3a5a14SIvan Khoronzhuk 			unreg_mcast_mask = mcast_mask;
24389f6bd8faSMugunthan V N 	} else {
24399f6bd8faSMugunthan V N 		port_mask = ALE_ALL_PORTS;
24405b3a5a14SIvan Khoronzhuk 		mcast_mask = port_mask;
24411e5c4bc4SLennart Sorensen 
24421e5c4bc4SLennart Sorensen 		if (priv->ndev->flags & IFF_ALLMULTI)
24431e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_ALL_PORTS;
24441e5c4bc4SLennart Sorensen 		else
24451e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
24469f6bd8faSMugunthan V N 	}
24473b72c2feSMugunthan V N 
24482a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
244961f1cef9SGrygorii Strashko 				unreg_mcast_mask);
24503b72c2feSMugunthan V N 	if (ret != 0)
24513b72c2feSMugunthan V N 		return ret;
24523b72c2feSMugunthan V N 
24532a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
245471a2cbb7SGrygorii Strashko 				 HOST_PORT_NUM, ALE_VLAN, vid);
24553b72c2feSMugunthan V N 	if (ret != 0)
24563b72c2feSMugunthan V N 		goto clean_vid;
24573b72c2feSMugunthan V N 
24582a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
24595b3a5a14SIvan Khoronzhuk 				 mcast_mask, ALE_VLAN, vid, 0);
24603b72c2feSMugunthan V N 	if (ret != 0)
24613b72c2feSMugunthan V N 		goto clean_vlan_ucast;
24623b72c2feSMugunthan V N 	return 0;
24633b72c2feSMugunthan V N 
24643b72c2feSMugunthan V N clean_vlan_ucast:
24652a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
246671a2cbb7SGrygorii Strashko 			   HOST_PORT_NUM, ALE_VLAN, vid);
24673b72c2feSMugunthan V N clean_vid:
24682a05a622SIvan Khoronzhuk 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
24693b72c2feSMugunthan V N 	return ret;
24703b72c2feSMugunthan V N }
24713b72c2feSMugunthan V N 
24723b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
247380d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
24743b72c2feSMugunthan V N {
24753b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2476649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2477a6c5d14fSGrygorii Strashko 	int ret;
24783b72c2feSMugunthan V N 
2479606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
24803b72c2feSMugunthan V N 		return 0;
24813b72c2feSMugunthan V N 
248256e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2483a6c5d14fSGrygorii Strashko 	if (ret < 0) {
248456e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2485a6c5d14fSGrygorii Strashko 		return ret;
2486a6c5d14fSGrygorii Strashko 	}
2487a6c5d14fSGrygorii Strashko 
2488606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
248902a54164SMugunthan V N 		/* In dual EMAC, reserved VLAN id should not be used for
249002a54164SMugunthan V N 		 * creating VLAN interfaces as this can break the dual
249102a54164SMugunthan V N 		 * EMAC port separation
249202a54164SMugunthan V N 		 */
249302a54164SMugunthan V N 		int i;
249402a54164SMugunthan V N 
2495606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
2496803c4f64SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan) {
2497803c4f64SIvan Khoronzhuk 				ret = -EINVAL;
2498803c4f64SIvan Khoronzhuk 				goto err;
2499803c4f64SIvan Khoronzhuk 			}
250002a54164SMugunthan V N 		}
250102a54164SMugunthan V N 	}
250202a54164SMugunthan V N 
25033b72c2feSMugunthan V N 	dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
2504a6c5d14fSGrygorii Strashko 	ret = cpsw_add_vlan_ale_entry(priv, vid);
2505803c4f64SIvan Khoronzhuk err:
250656e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2507a6c5d14fSGrygorii Strashko 	return ret;
25083b72c2feSMugunthan V N }
25093b72c2feSMugunthan V N 
25103b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
251180d5c368SPatrick McHardy 				     __be16 proto, u16 vid)
25123b72c2feSMugunthan V N {
25133b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2514649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
25153b72c2feSMugunthan V N 	int ret;
25163b72c2feSMugunthan V N 
2517606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
25183b72c2feSMugunthan V N 		return 0;
25193b72c2feSMugunthan V N 
252056e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2521a6c5d14fSGrygorii Strashko 	if (ret < 0) {
252256e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2523a6c5d14fSGrygorii Strashko 		return ret;
2524a6c5d14fSGrygorii Strashko 	}
2525a6c5d14fSGrygorii Strashko 
2526606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
252702a54164SMugunthan V N 		int i;
252802a54164SMugunthan V N 
2529606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
2530606f3993SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan)
2531803c4f64SIvan Khoronzhuk 				goto err;
253202a54164SMugunthan V N 		}
253302a54164SMugunthan V N 	}
253402a54164SMugunthan V N 
25353b72c2feSMugunthan V N 	dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
25362a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2537be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
253861f1cef9SGrygorii Strashko 				  HOST_PORT_NUM, ALE_VLAN, vid);
2539be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
25403b72c2feSMugunthan V N 				  0, ALE_VLAN, vid);
254115180ecaSIvan Khoronzhuk 	ret |= cpsw_ale_flush_multicast(cpsw->ale, 0, vid);
2542803c4f64SIvan Khoronzhuk err:
254356e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2544a6c5d14fSGrygorii Strashko 	return ret;
25453b72c2feSMugunthan V N }
25463b72c2feSMugunthan V N 
254783fcad0cSIvan Khoronzhuk static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
254883fcad0cSIvan Khoronzhuk {
254983fcad0cSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
255083fcad0cSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
255152986a2fSIvan Khoronzhuk 	struct cpsw_slave *slave;
255232b78d85SIvan Khoronzhuk 	u32 min_rate;
255383fcad0cSIvan Khoronzhuk 	u32 ch_rate;
255452986a2fSIvan Khoronzhuk 	int i, ret;
255583fcad0cSIvan Khoronzhuk 
255683fcad0cSIvan Khoronzhuk 	ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
255783fcad0cSIvan Khoronzhuk 	if (ch_rate == rate)
255883fcad0cSIvan Khoronzhuk 		return 0;
255983fcad0cSIvan Khoronzhuk 
256032b78d85SIvan Khoronzhuk 	ch_rate = rate * 1000;
256183fcad0cSIvan Khoronzhuk 	min_rate = cpdma_chan_get_min_rate(cpsw->dma);
256232b78d85SIvan Khoronzhuk 	if ((ch_rate < min_rate && ch_rate)) {
256332b78d85SIvan Khoronzhuk 		dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
256483fcad0cSIvan Khoronzhuk 			min_rate);
256583fcad0cSIvan Khoronzhuk 		return -EINVAL;
256683fcad0cSIvan Khoronzhuk 	}
256783fcad0cSIvan Khoronzhuk 
25680be01b8eSIvan Khoronzhuk 	if (rate > cpsw->speed) {
256932b78d85SIvan Khoronzhuk 		dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
257032b78d85SIvan Khoronzhuk 		return -EINVAL;
257132b78d85SIvan Khoronzhuk 	}
257232b78d85SIvan Khoronzhuk 
257383fcad0cSIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
257483fcad0cSIvan Khoronzhuk 	if (ret < 0) {
257583fcad0cSIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
257683fcad0cSIvan Khoronzhuk 		return ret;
257783fcad0cSIvan Khoronzhuk 	}
257883fcad0cSIvan Khoronzhuk 
257932b78d85SIvan Khoronzhuk 	ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
258083fcad0cSIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
258132b78d85SIvan Khoronzhuk 
258232b78d85SIvan Khoronzhuk 	if (ret)
258332b78d85SIvan Khoronzhuk 		return ret;
258432b78d85SIvan Khoronzhuk 
258552986a2fSIvan Khoronzhuk 	/* update rates for slaves tx queues */
258652986a2fSIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++) {
258752986a2fSIvan Khoronzhuk 		slave = &cpsw->slaves[i];
258852986a2fSIvan Khoronzhuk 		if (!slave->ndev)
258952986a2fSIvan Khoronzhuk 			continue;
259052986a2fSIvan Khoronzhuk 
259152986a2fSIvan Khoronzhuk 		netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
259252986a2fSIvan Khoronzhuk 	}
259352986a2fSIvan Khoronzhuk 
259432b78d85SIvan Khoronzhuk 	cpsw_split_res(ndev);
259583fcad0cSIvan Khoronzhuk 	return ret;
259683fcad0cSIvan Khoronzhuk }
259783fcad0cSIvan Khoronzhuk 
25987929a668SIvan Khoronzhuk static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
25997929a668SIvan Khoronzhuk {
26007929a668SIvan Khoronzhuk 	struct tc_mqprio_qopt_offload *mqprio = type_data;
26017929a668SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
26027929a668SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
26037929a668SIvan Khoronzhuk 	int fifo, num_tc, count, offset;
26047929a668SIvan Khoronzhuk 	struct cpsw_slave *slave;
26057929a668SIvan Khoronzhuk 	u32 tx_prio_map = 0;
26067929a668SIvan Khoronzhuk 	int i, tc, ret;
26077929a668SIvan Khoronzhuk 
26087929a668SIvan Khoronzhuk 	num_tc = mqprio->qopt.num_tc;
26097929a668SIvan Khoronzhuk 	if (num_tc > CPSW_TC_NUM)
26107929a668SIvan Khoronzhuk 		return -EINVAL;
26117929a668SIvan Khoronzhuk 
26127929a668SIvan Khoronzhuk 	if (mqprio->mode != TC_MQPRIO_MODE_DCB)
26137929a668SIvan Khoronzhuk 		return -EINVAL;
26147929a668SIvan Khoronzhuk 
26157929a668SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
26167929a668SIvan Khoronzhuk 	if (ret < 0) {
26177929a668SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
26187929a668SIvan Khoronzhuk 		return ret;
26197929a668SIvan Khoronzhuk 	}
26207929a668SIvan Khoronzhuk 
26217929a668SIvan Khoronzhuk 	if (num_tc) {
26227929a668SIvan Khoronzhuk 		for (i = 0; i < 8; i++) {
26237929a668SIvan Khoronzhuk 			tc = mqprio->qopt.prio_tc_map[i];
26247929a668SIvan Khoronzhuk 			fifo = cpsw_tc_to_fifo(tc, num_tc);
26257929a668SIvan Khoronzhuk 			tx_prio_map |= fifo << (4 * i);
26267929a668SIvan Khoronzhuk 		}
26277929a668SIvan Khoronzhuk 
26287929a668SIvan Khoronzhuk 		netdev_set_num_tc(ndev, num_tc);
26297929a668SIvan Khoronzhuk 		for (i = 0; i < num_tc; i++) {
26307929a668SIvan Khoronzhuk 			count = mqprio->qopt.count[i];
26317929a668SIvan Khoronzhuk 			offset = mqprio->qopt.offset[i];
26327929a668SIvan Khoronzhuk 			netdev_set_tc_queue(ndev, i, count, offset);
26337929a668SIvan Khoronzhuk 		}
26347929a668SIvan Khoronzhuk 	}
26357929a668SIvan Khoronzhuk 
26367929a668SIvan Khoronzhuk 	if (!mqprio->qopt.hw) {
26377929a668SIvan Khoronzhuk 		/* restore default configuration */
26387929a668SIvan Khoronzhuk 		netdev_reset_tc(ndev);
26397929a668SIvan Khoronzhuk 		tx_prio_map = TX_PRIORITY_MAPPING;
26407929a668SIvan Khoronzhuk 	}
26417929a668SIvan Khoronzhuk 
26427929a668SIvan Khoronzhuk 	priv->mqprio_hw = mqprio->qopt.hw;
26437929a668SIvan Khoronzhuk 
26447929a668SIvan Khoronzhuk 	offset = cpsw->version == CPSW_VERSION_1 ?
26457929a668SIvan Khoronzhuk 		 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
26467929a668SIvan Khoronzhuk 
26477929a668SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
26487929a668SIvan Khoronzhuk 	slave_write(slave, tx_prio_map, offset);
26497929a668SIvan Khoronzhuk 
26507929a668SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
26517929a668SIvan Khoronzhuk 
26527929a668SIvan Khoronzhuk 	return 0;
26537929a668SIvan Khoronzhuk }
26547929a668SIvan Khoronzhuk 
26557929a668SIvan Khoronzhuk static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
26567929a668SIvan Khoronzhuk 			     void *type_data)
26577929a668SIvan Khoronzhuk {
26587929a668SIvan Khoronzhuk 	switch (type) {
265957d90148SIvan Khoronzhuk 	case TC_SETUP_QDISC_CBS:
266057d90148SIvan Khoronzhuk 		return cpsw_set_cbs(ndev, type_data);
266157d90148SIvan Khoronzhuk 
26627929a668SIvan Khoronzhuk 	case TC_SETUP_QDISC_MQPRIO:
26637929a668SIvan Khoronzhuk 		return cpsw_set_mqprio(ndev, type_data);
26647929a668SIvan Khoronzhuk 
26657929a668SIvan Khoronzhuk 	default:
26667929a668SIvan Khoronzhuk 		return -EOPNOTSUPP;
26677929a668SIvan Khoronzhuk 	}
26687929a668SIvan Khoronzhuk }
26697929a668SIvan Khoronzhuk 
2670df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = {
2671df828598SMugunthan V N 	.ndo_open		= cpsw_ndo_open,
2672df828598SMugunthan V N 	.ndo_stop		= cpsw_ndo_stop,
2673df828598SMugunthan V N 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
2674dcfd8d58SMugunthan V N 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
26752e5b38abSRichard Cochran 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
2676df828598SMugunthan V N 	.ndo_validate_addr	= eth_validate_addr,
2677df828598SMugunthan V N 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
26785c50a856SMugunthan V N 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
267983fcad0cSIvan Khoronzhuk 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
2680df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
2681df828598SMugunthan V N 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
2682df828598SMugunthan V N #endif
26833b72c2feSMugunthan V N 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
26843b72c2feSMugunthan V N 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
26857929a668SIvan Khoronzhuk 	.ndo_setup_tc           = cpsw_ndo_setup_tc,
2686df828598SMugunthan V N };
2687df828598SMugunthan V N 
268852c4f0ecSMugunthan V N static int cpsw_get_regs_len(struct net_device *ndev)
268952c4f0ecSMugunthan V N {
2690606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
269152c4f0ecSMugunthan V N 
2692606f3993SIvan Khoronzhuk 	return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
269352c4f0ecSMugunthan V N }
269452c4f0ecSMugunthan V N 
269552c4f0ecSMugunthan V N static void cpsw_get_regs(struct net_device *ndev,
269652c4f0ecSMugunthan V N 			  struct ethtool_regs *regs, void *p)
269752c4f0ecSMugunthan V N {
269852c4f0ecSMugunthan V N 	u32 *reg = p;
26992a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
270052c4f0ecSMugunthan V N 
270152c4f0ecSMugunthan V N 	/* update CPSW IP version */
27022a05a622SIvan Khoronzhuk 	regs->version = cpsw->version;
270352c4f0ecSMugunthan V N 
27042a05a622SIvan Khoronzhuk 	cpsw_ale_dump(cpsw->ale, reg);
270552c4f0ecSMugunthan V N }
270652c4f0ecSMugunthan V N 
2707df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev,
2708df828598SMugunthan V N 			     struct ethtool_drvinfo *info)
2709df828598SMugunthan V N {
2710649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
271156e31bd8SIvan Khoronzhuk 	struct platform_device	*pdev = to_platform_device(cpsw->dev);
27127826d43fSJiri Pirko 
271352c4f0ecSMugunthan V N 	strlcpy(info->driver, "cpsw", sizeof(info->driver));
27147826d43fSJiri Pirko 	strlcpy(info->version, "1.0", sizeof(info->version));
271556e31bd8SIvan Khoronzhuk 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
2716df828598SMugunthan V N }
2717df828598SMugunthan V N 
2718df828598SMugunthan V N static u32 cpsw_get_msglevel(struct net_device *ndev)
2719df828598SMugunthan V N {
2720df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2721df828598SMugunthan V N 	return priv->msg_enable;
2722df828598SMugunthan V N }
2723df828598SMugunthan V N 
2724df828598SMugunthan V N static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2725df828598SMugunthan V N {
2726df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2727df828598SMugunthan V N 	priv->msg_enable = value;
2728df828598SMugunthan V N }
2729df828598SMugunthan V N 
2730c8395d4eSGrygorii Strashko #if IS_ENABLED(CONFIG_TI_CPTS)
27312e5b38abSRichard Cochran static int cpsw_get_ts_info(struct net_device *ndev,
27322e5b38abSRichard Cochran 			    struct ethtool_ts_info *info)
27332e5b38abSRichard Cochran {
27342a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
27352e5b38abSRichard Cochran 
27362e5b38abSRichard Cochran 	info->so_timestamping =
27372e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_HARDWARE |
27382e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_SOFTWARE |
27392e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_HARDWARE |
27402e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_SOFTWARE |
27412e5b38abSRichard Cochran 		SOF_TIMESTAMPING_SOFTWARE |
27422e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RAW_HARDWARE;
27432a05a622SIvan Khoronzhuk 	info->phc_index = cpsw->cpts->phc_index;
27442e5b38abSRichard Cochran 	info->tx_types =
27452e5b38abSRichard Cochran 		(1 << HWTSTAMP_TX_OFF) |
27462e5b38abSRichard Cochran 		(1 << HWTSTAMP_TX_ON);
27472e5b38abSRichard Cochran 	info->rx_filters =
27482e5b38abSRichard Cochran 		(1 << HWTSTAMP_FILTER_NONE) |
2749e9523a5aSGrygorii Strashko 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
27502e5b38abSRichard Cochran 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2751c8395d4eSGrygorii Strashko 	return 0;
2752c8395d4eSGrygorii Strashko }
27532e5b38abSRichard Cochran #else
2754c8395d4eSGrygorii Strashko static int cpsw_get_ts_info(struct net_device *ndev,
2755c8395d4eSGrygorii Strashko 			    struct ethtool_ts_info *info)
2756c8395d4eSGrygorii Strashko {
27572e5b38abSRichard Cochran 	info->so_timestamping =
27582e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_SOFTWARE |
27592e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_SOFTWARE |
27602e5b38abSRichard Cochran 		SOF_TIMESTAMPING_SOFTWARE;
27612e5b38abSRichard Cochran 	info->phc_index = -1;
27622e5b38abSRichard Cochran 	info->tx_types = 0;
27632e5b38abSRichard Cochran 	info->rx_filters = 0;
27642e5b38abSRichard Cochran 	return 0;
27652e5b38abSRichard Cochran }
2766c8395d4eSGrygorii Strashko #endif
27672e5b38abSRichard Cochran 
27682479876dSPhilippe Reynes static int cpsw_get_link_ksettings(struct net_device *ndev,
27692479876dSPhilippe Reynes 				   struct ethtool_link_ksettings *ecmd)
2770d3bb9c58SMugunthan V N {
2771d3bb9c58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2772606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2773606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2774d3bb9c58SMugunthan V N 
27755514174fSyuval.shaia@oracle.com 	if (!cpsw->slaves[slave_no].phy)
2776d3bb9c58SMugunthan V N 		return -EOPNOTSUPP;
27775514174fSyuval.shaia@oracle.com 
27785514174fSyuval.shaia@oracle.com 	phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
27795514174fSyuval.shaia@oracle.com 	return 0;
2780d3bb9c58SMugunthan V N }
2781d3bb9c58SMugunthan V N 
27822479876dSPhilippe Reynes static int cpsw_set_link_ksettings(struct net_device *ndev,
27832479876dSPhilippe Reynes 				   const struct ethtool_link_ksettings *ecmd)
2784d3bb9c58SMugunthan V N {
2785d3bb9c58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2786606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2787606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2788d3bb9c58SMugunthan V N 
2789606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
27902479876dSPhilippe Reynes 		return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
27912479876dSPhilippe Reynes 						 ecmd);
2792d3bb9c58SMugunthan V N 	else
2793d3bb9c58SMugunthan V N 		return -EOPNOTSUPP;
2794d3bb9c58SMugunthan V N }
2795d3bb9c58SMugunthan V N 
2796d8a64420SMatus Ujhelyi static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2797d8a64420SMatus Ujhelyi {
2798d8a64420SMatus Ujhelyi 	struct cpsw_priv *priv = netdev_priv(ndev);
2799606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2800606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2801d8a64420SMatus Ujhelyi 
2802d8a64420SMatus Ujhelyi 	wol->supported = 0;
2803d8a64420SMatus Ujhelyi 	wol->wolopts = 0;
2804d8a64420SMatus Ujhelyi 
2805606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
2806606f3993SIvan Khoronzhuk 		phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2807d8a64420SMatus Ujhelyi }
2808d8a64420SMatus Ujhelyi 
2809d8a64420SMatus Ujhelyi static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2810d8a64420SMatus Ujhelyi {
2811d8a64420SMatus Ujhelyi 	struct cpsw_priv *priv = netdev_priv(ndev);
2812606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2813606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2814d8a64420SMatus Ujhelyi 
2815606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
2816606f3993SIvan Khoronzhuk 		return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2817d8a64420SMatus Ujhelyi 	else
2818d8a64420SMatus Ujhelyi 		return -EOPNOTSUPP;
2819d8a64420SMatus Ujhelyi }
2820d8a64420SMatus Ujhelyi 
28211923d6e4SMugunthan V N static void cpsw_get_pauseparam(struct net_device *ndev,
28221923d6e4SMugunthan V N 				struct ethtool_pauseparam *pause)
28231923d6e4SMugunthan V N {
28241923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
28251923d6e4SMugunthan V N 
28261923d6e4SMugunthan V N 	pause->autoneg = AUTONEG_DISABLE;
28271923d6e4SMugunthan V N 	pause->rx_pause = priv->rx_pause ? true : false;
28281923d6e4SMugunthan V N 	pause->tx_pause = priv->tx_pause ? true : false;
28291923d6e4SMugunthan V N }
28301923d6e4SMugunthan V N 
28311923d6e4SMugunthan V N static int cpsw_set_pauseparam(struct net_device *ndev,
28321923d6e4SMugunthan V N 			       struct ethtool_pauseparam *pause)
28331923d6e4SMugunthan V N {
28341923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
28351923d6e4SMugunthan V N 	bool link;
28361923d6e4SMugunthan V N 
28371923d6e4SMugunthan V N 	priv->rx_pause = pause->rx_pause ? true : false;
28381923d6e4SMugunthan V N 	priv->tx_pause = pause->tx_pause ? true : false;
28391923d6e4SMugunthan V N 
28401923d6e4SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
28411923d6e4SMugunthan V N 	return 0;
28421923d6e4SMugunthan V N }
28431923d6e4SMugunthan V N 
28447898b1daSGrygorii Strashko static int cpsw_ethtool_op_begin(struct net_device *ndev)
28457898b1daSGrygorii Strashko {
28467898b1daSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
2847649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
28487898b1daSGrygorii Strashko 	int ret;
28497898b1daSGrygorii Strashko 
285056e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
28517898b1daSGrygorii Strashko 	if (ret < 0) {
28527898b1daSGrygorii Strashko 		cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
285356e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
28547898b1daSGrygorii Strashko 	}
28557898b1daSGrygorii Strashko 
28567898b1daSGrygorii Strashko 	return ret;
28577898b1daSGrygorii Strashko }
28587898b1daSGrygorii Strashko 
28597898b1daSGrygorii Strashko static void cpsw_ethtool_op_complete(struct net_device *ndev)
28607898b1daSGrygorii Strashko {
28617898b1daSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
28627898b1daSGrygorii Strashko 	int ret;
28637898b1daSGrygorii Strashko 
286456e31bd8SIvan Khoronzhuk 	ret = pm_runtime_put(priv->cpsw->dev);
28657898b1daSGrygorii Strashko 	if (ret < 0)
28667898b1daSGrygorii Strashko 		cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
28677898b1daSGrygorii Strashko }
28687898b1daSGrygorii Strashko 
2869ce52c744SIvan Khoronzhuk static void cpsw_get_channels(struct net_device *ndev,
2870ce52c744SIvan Khoronzhuk 			      struct ethtool_channels *ch)
2871ce52c744SIvan Khoronzhuk {
2872ce52c744SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2873ce52c744SIvan Khoronzhuk 
28749611d6d6SIvan Khoronzhuk 	ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
28759611d6d6SIvan Khoronzhuk 	ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2876ce52c744SIvan Khoronzhuk 	ch->max_combined = 0;
2877ce52c744SIvan Khoronzhuk 	ch->max_other = 0;
2878ce52c744SIvan Khoronzhuk 	ch->other_count = 0;
2879ce52c744SIvan Khoronzhuk 	ch->rx_count = cpsw->rx_ch_num;
2880ce52c744SIvan Khoronzhuk 	ch->tx_count = cpsw->tx_ch_num;
2881ce52c744SIvan Khoronzhuk 	ch->combined_count = 0;
2882ce52c744SIvan Khoronzhuk }
2883ce52c744SIvan Khoronzhuk 
2884ce52c744SIvan Khoronzhuk static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2885ce52c744SIvan Khoronzhuk 				  struct ethtool_channels *ch)
2886ce52c744SIvan Khoronzhuk {
28879611d6d6SIvan Khoronzhuk 	if (cpsw->quirk_irq) {
28889611d6d6SIvan Khoronzhuk 		dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
28899611d6d6SIvan Khoronzhuk 		return -EOPNOTSUPP;
28909611d6d6SIvan Khoronzhuk 	}
28919611d6d6SIvan Khoronzhuk 
2892ce52c744SIvan Khoronzhuk 	if (ch->combined_count)
2893ce52c744SIvan Khoronzhuk 		return -EINVAL;
2894ce52c744SIvan Khoronzhuk 
2895ce52c744SIvan Khoronzhuk 	/* verify we have at least one channel in each direction */
2896ce52c744SIvan Khoronzhuk 	if (!ch->rx_count || !ch->tx_count)
2897ce52c744SIvan Khoronzhuk 		return -EINVAL;
2898ce52c744SIvan Khoronzhuk 
2899ce52c744SIvan Khoronzhuk 	if (ch->rx_count > cpsw->data.channels ||
2900ce52c744SIvan Khoronzhuk 	    ch->tx_count > cpsw->data.channels)
2901ce52c744SIvan Khoronzhuk 		return -EINVAL;
2902ce52c744SIvan Khoronzhuk 
2903ce52c744SIvan Khoronzhuk 	return 0;
2904ce52c744SIvan Khoronzhuk }
2905ce52c744SIvan Khoronzhuk 
2906ce52c744SIvan Khoronzhuk static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2907ce52c744SIvan Khoronzhuk {
2908ce52c744SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2909ce52c744SIvan Khoronzhuk 	void (*handler)(void *, int, int);
291083fcad0cSIvan Khoronzhuk 	struct netdev_queue *queue;
29118feb0a19SIvan Khoronzhuk 	struct cpsw_vector *vec;
291279b3325dSIvan Khoronzhuk 	int ret, *ch, vch;
2913ce52c744SIvan Khoronzhuk 
2914ce52c744SIvan Khoronzhuk 	if (rx) {
2915ce52c744SIvan Khoronzhuk 		ch = &cpsw->rx_ch_num;
29168feb0a19SIvan Khoronzhuk 		vec = cpsw->rxv;
2917ce52c744SIvan Khoronzhuk 		handler = cpsw_rx_handler;
2918ce52c744SIvan Khoronzhuk 	} else {
2919ce52c744SIvan Khoronzhuk 		ch = &cpsw->tx_ch_num;
29208feb0a19SIvan Khoronzhuk 		vec = cpsw->txv;
2921ce52c744SIvan Khoronzhuk 		handler = cpsw_tx_handler;
2922ce52c744SIvan Khoronzhuk 	}
2923ce52c744SIvan Khoronzhuk 
2924ce52c744SIvan Khoronzhuk 	while (*ch < ch_num) {
292579b3325dSIvan Khoronzhuk 		vch = rx ? *ch : 7 - *ch;
292679b3325dSIvan Khoronzhuk 		vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx);
292783fcad0cSIvan Khoronzhuk 		queue = netdev_get_tx_queue(priv->ndev, *ch);
292883fcad0cSIvan Khoronzhuk 		queue->tx_maxrate = 0;
2929ce52c744SIvan Khoronzhuk 
29308feb0a19SIvan Khoronzhuk 		if (IS_ERR(vec[*ch].ch))
29318feb0a19SIvan Khoronzhuk 			return PTR_ERR(vec[*ch].ch);
2932ce52c744SIvan Khoronzhuk 
29338feb0a19SIvan Khoronzhuk 		if (!vec[*ch].ch)
2934ce52c744SIvan Khoronzhuk 			return -EINVAL;
2935ce52c744SIvan Khoronzhuk 
2936ce52c744SIvan Khoronzhuk 		cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2937ce52c744SIvan Khoronzhuk 			  (rx ? "rx" : "tx"));
2938ce52c744SIvan Khoronzhuk 		(*ch)++;
2939ce52c744SIvan Khoronzhuk 	}
2940ce52c744SIvan Khoronzhuk 
2941ce52c744SIvan Khoronzhuk 	while (*ch > ch_num) {
2942ce52c744SIvan Khoronzhuk 		(*ch)--;
2943ce52c744SIvan Khoronzhuk 
29448feb0a19SIvan Khoronzhuk 		ret = cpdma_chan_destroy(vec[*ch].ch);
2945ce52c744SIvan Khoronzhuk 		if (ret)
2946ce52c744SIvan Khoronzhuk 			return ret;
2947ce52c744SIvan Khoronzhuk 
2948ce52c744SIvan Khoronzhuk 		cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2949ce52c744SIvan Khoronzhuk 			  (rx ? "rx" : "tx"));
2950ce52c744SIvan Khoronzhuk 	}
2951ce52c744SIvan Khoronzhuk 
2952ce52c744SIvan Khoronzhuk 	return 0;
2953ce52c744SIvan Khoronzhuk }
2954ce52c744SIvan Khoronzhuk 
2955ce52c744SIvan Khoronzhuk static int cpsw_update_channels(struct cpsw_priv *priv,
2956ce52c744SIvan Khoronzhuk 				struct ethtool_channels *ch)
2957ce52c744SIvan Khoronzhuk {
2958ce52c744SIvan Khoronzhuk 	int ret;
2959ce52c744SIvan Khoronzhuk 
2960ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2961ce52c744SIvan Khoronzhuk 	if (ret)
2962ce52c744SIvan Khoronzhuk 		return ret;
2963ce52c744SIvan Khoronzhuk 
2964ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2965ce52c744SIvan Khoronzhuk 	if (ret)
2966ce52c744SIvan Khoronzhuk 		return ret;
2967ce52c744SIvan Khoronzhuk 
2968ce52c744SIvan Khoronzhuk 	return 0;
2969ce52c744SIvan Khoronzhuk }
2970ce52c744SIvan Khoronzhuk 
2971022d7ad7SIvan Khoronzhuk static void cpsw_suspend_data_pass(struct net_device *ndev)
2972ce52c744SIvan Khoronzhuk {
2973022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2974ce52c744SIvan Khoronzhuk 	struct cpsw_slave *slave;
2975022d7ad7SIvan Khoronzhuk 	int i;
2976ce52c744SIvan Khoronzhuk 
2977ce52c744SIvan Khoronzhuk 	/* Disable NAPI scheduling */
2978ce52c744SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2979ce52c744SIvan Khoronzhuk 
2980ce52c744SIvan Khoronzhuk 	/* Stop all transmit queues for every network device.
2981ce52c744SIvan Khoronzhuk 	 * Disable re-using rx descriptors with dormant_on.
2982ce52c744SIvan Khoronzhuk 	 */
2983ce52c744SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2984ce52c744SIvan Khoronzhuk 		if (!(slave->ndev && netif_running(slave->ndev)))
2985ce52c744SIvan Khoronzhuk 			continue;
2986ce52c744SIvan Khoronzhuk 
2987ce52c744SIvan Khoronzhuk 		netif_tx_stop_all_queues(slave->ndev);
2988ce52c744SIvan Khoronzhuk 		netif_dormant_on(slave->ndev);
2989ce52c744SIvan Khoronzhuk 	}
2990ce52c744SIvan Khoronzhuk 
2991ce52c744SIvan Khoronzhuk 	/* Handle rest of tx packets and stop cpdma channels */
2992ce52c744SIvan Khoronzhuk 	cpdma_ctlr_stop(cpsw->dma);
2993022d7ad7SIvan Khoronzhuk }
2994022d7ad7SIvan Khoronzhuk 
2995022d7ad7SIvan Khoronzhuk static int cpsw_resume_data_pass(struct net_device *ndev)
2996022d7ad7SIvan Khoronzhuk {
2997022d7ad7SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
2998022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2999022d7ad7SIvan Khoronzhuk 	struct cpsw_slave *slave;
3000022d7ad7SIvan Khoronzhuk 	int i, ret;
3001022d7ad7SIvan Khoronzhuk 
3002022d7ad7SIvan Khoronzhuk 	/* Allow rx packets handling */
3003022d7ad7SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
3004022d7ad7SIvan Khoronzhuk 		if (slave->ndev && netif_running(slave->ndev))
3005022d7ad7SIvan Khoronzhuk 			netif_dormant_off(slave->ndev);
3006022d7ad7SIvan Khoronzhuk 
3007022d7ad7SIvan Khoronzhuk 	/* After this receive is started */
3008d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count) {
3009022d7ad7SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
3010022d7ad7SIvan Khoronzhuk 		if (ret)
3011022d7ad7SIvan Khoronzhuk 			return ret;
3012022d7ad7SIvan Khoronzhuk 
3013022d7ad7SIvan Khoronzhuk 		cpdma_ctlr_start(cpsw->dma);
3014022d7ad7SIvan Khoronzhuk 		cpsw_intr_enable(cpsw);
3015022d7ad7SIvan Khoronzhuk 	}
3016022d7ad7SIvan Khoronzhuk 
3017022d7ad7SIvan Khoronzhuk 	/* Resume transmit for every affected interface */
3018022d7ad7SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
3019022d7ad7SIvan Khoronzhuk 		if (slave->ndev && netif_running(slave->ndev))
3020022d7ad7SIvan Khoronzhuk 			netif_tx_start_all_queues(slave->ndev);
3021022d7ad7SIvan Khoronzhuk 
3022022d7ad7SIvan Khoronzhuk 	return 0;
3023022d7ad7SIvan Khoronzhuk }
3024022d7ad7SIvan Khoronzhuk 
3025022d7ad7SIvan Khoronzhuk static int cpsw_set_channels(struct net_device *ndev,
3026022d7ad7SIvan Khoronzhuk 			     struct ethtool_channels *chs)
3027022d7ad7SIvan Khoronzhuk {
3028022d7ad7SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
3029022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
3030022d7ad7SIvan Khoronzhuk 	struct cpsw_slave *slave;
3031022d7ad7SIvan Khoronzhuk 	int i, ret;
3032022d7ad7SIvan Khoronzhuk 
3033022d7ad7SIvan Khoronzhuk 	ret = cpsw_check_ch_settings(cpsw, chs);
3034022d7ad7SIvan Khoronzhuk 	if (ret < 0)
3035022d7ad7SIvan Khoronzhuk 		return ret;
3036022d7ad7SIvan Khoronzhuk 
3037022d7ad7SIvan Khoronzhuk 	cpsw_suspend_data_pass(ndev);
3038ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels(priv, chs);
3039ce52c744SIvan Khoronzhuk 	if (ret)
3040ce52c744SIvan Khoronzhuk 		goto err;
3041ce52c744SIvan Khoronzhuk 
3042ce52c744SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
3043ce52c744SIvan Khoronzhuk 		if (!(slave->ndev && netif_running(slave->ndev)))
3044ce52c744SIvan Khoronzhuk 			continue;
3045ce52c744SIvan Khoronzhuk 
3046ce52c744SIvan Khoronzhuk 		/* Inform stack about new count of queues */
3047ce52c744SIvan Khoronzhuk 		ret = netif_set_real_num_tx_queues(slave->ndev,
3048ce52c744SIvan Khoronzhuk 						   cpsw->tx_ch_num);
3049ce52c744SIvan Khoronzhuk 		if (ret) {
3050ce52c744SIvan Khoronzhuk 			dev_err(priv->dev, "cannot set real number of tx queues\n");
3051ce52c744SIvan Khoronzhuk 			goto err;
3052ce52c744SIvan Khoronzhuk 		}
3053ce52c744SIvan Khoronzhuk 
3054ce52c744SIvan Khoronzhuk 		ret = netif_set_real_num_rx_queues(slave->ndev,
3055ce52c744SIvan Khoronzhuk 						   cpsw->rx_ch_num);
3056ce52c744SIvan Khoronzhuk 		if (ret) {
3057ce52c744SIvan Khoronzhuk 			dev_err(priv->dev, "cannot set real number of rx queues\n");
3058ce52c744SIvan Khoronzhuk 			goto err;
3059ce52c744SIvan Khoronzhuk 		}
3060ce52c744SIvan Khoronzhuk 	}
3061ce52c744SIvan Khoronzhuk 
3062d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count)
306332b78d85SIvan Khoronzhuk 		cpsw_split_res(ndev);
30648feb0a19SIvan Khoronzhuk 
3065022d7ad7SIvan Khoronzhuk 	ret = cpsw_resume_data_pass(ndev);
3066022d7ad7SIvan Khoronzhuk 	if (!ret)
3067ce52c744SIvan Khoronzhuk 		return 0;
3068ce52c744SIvan Khoronzhuk err:
3069ce52c744SIvan Khoronzhuk 	dev_err(priv->dev, "cannot update channels number, closing device\n");
3070ce52c744SIvan Khoronzhuk 	dev_close(ndev);
3071ce52c744SIvan Khoronzhuk 	return ret;
3072ce52c744SIvan Khoronzhuk }
3073ce52c744SIvan Khoronzhuk 
3074a0909949SYegor Yefremov static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
3075a0909949SYegor Yefremov {
3076a0909949SYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
3077a0909949SYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
3078a0909949SYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
3079a0909949SYegor Yefremov 
3080a0909949SYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
3081a0909949SYegor Yefremov 		return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
3082a0909949SYegor Yefremov 	else
3083a0909949SYegor Yefremov 		return -EOPNOTSUPP;
3084a0909949SYegor Yefremov }
3085a0909949SYegor Yefremov 
3086a0909949SYegor Yefremov static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
3087a0909949SYegor Yefremov {
3088a0909949SYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
3089a0909949SYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
3090a0909949SYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
3091a0909949SYegor Yefremov 
3092a0909949SYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
3093a0909949SYegor Yefremov 		return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
3094a0909949SYegor Yefremov 	else
3095a0909949SYegor Yefremov 		return -EOPNOTSUPP;
3096a0909949SYegor Yefremov }
3097a0909949SYegor Yefremov 
30986bb10c2bSYegor Yefremov static int cpsw_nway_reset(struct net_device *ndev)
30996bb10c2bSYegor Yefremov {
31006bb10c2bSYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
31016bb10c2bSYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
31026bb10c2bSYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
31036bb10c2bSYegor Yefremov 
31046bb10c2bSYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
31056bb10c2bSYegor Yefremov 		return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
31066bb10c2bSYegor Yefremov 	else
31076bb10c2bSYegor Yefremov 		return -EOPNOTSUPP;
31086bb10c2bSYegor Yefremov }
31096bb10c2bSYegor Yefremov 
3110be034fc1SGrygorii Strashko static void cpsw_get_ringparam(struct net_device *ndev,
3111be034fc1SGrygorii Strashko 			       struct ethtool_ringparam *ering)
3112be034fc1SGrygorii Strashko {
3113be034fc1SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
3114be034fc1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
3115be034fc1SGrygorii Strashko 
3116be034fc1SGrygorii Strashko 	/* not supported */
3117be034fc1SGrygorii Strashko 	ering->tx_max_pending = 0;
3118be034fc1SGrygorii Strashko 	ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
3119f89d21b9SIvan Khoronzhuk 	ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
3120be034fc1SGrygorii Strashko 	ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
3121be034fc1SGrygorii Strashko }
3122be034fc1SGrygorii Strashko 
3123be034fc1SGrygorii Strashko static int cpsw_set_ringparam(struct net_device *ndev,
3124be034fc1SGrygorii Strashko 			      struct ethtool_ringparam *ering)
3125be034fc1SGrygorii Strashko {
3126be034fc1SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
3127be034fc1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
3128022d7ad7SIvan Khoronzhuk 	int ret;
3129be034fc1SGrygorii Strashko 
3130be034fc1SGrygorii Strashko 	/* ignore ering->tx_pending - only rx_pending adjustment is supported */
3131be034fc1SGrygorii Strashko 
3132be034fc1SGrygorii Strashko 	if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
3133f89d21b9SIvan Khoronzhuk 	    ering->rx_pending < CPSW_MAX_QUEUES ||
3134f89d21b9SIvan Khoronzhuk 	    ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
3135be034fc1SGrygorii Strashko 		return -EINVAL;
3136be034fc1SGrygorii Strashko 
3137be034fc1SGrygorii Strashko 	if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
3138be034fc1SGrygorii Strashko 		return 0;
3139be034fc1SGrygorii Strashko 
3140022d7ad7SIvan Khoronzhuk 	cpsw_suspend_data_pass(ndev);
3141be034fc1SGrygorii Strashko 
3142be034fc1SGrygorii Strashko 	cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
3143be034fc1SGrygorii Strashko 
3144d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count)
3145be034fc1SGrygorii Strashko 		cpdma_chan_split_pool(cpsw->dma);
3146be034fc1SGrygorii Strashko 
3147022d7ad7SIvan Khoronzhuk 	ret = cpsw_resume_data_pass(ndev);
3148022d7ad7SIvan Khoronzhuk 	if (!ret)
3149be034fc1SGrygorii Strashko 		return 0;
3150022d7ad7SIvan Khoronzhuk 
3151022d7ad7SIvan Khoronzhuk 	dev_err(&ndev->dev, "cannot set ring params, closing device\n");
3152be034fc1SGrygorii Strashko 	dev_close(ndev);
3153be034fc1SGrygorii Strashko 	return ret;
3154be034fc1SGrygorii Strashko }
3155be034fc1SGrygorii Strashko 
3156df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = {
3157df828598SMugunthan V N 	.get_drvinfo	= cpsw_get_drvinfo,
3158df828598SMugunthan V N 	.get_msglevel	= cpsw_get_msglevel,
3159df828598SMugunthan V N 	.set_msglevel	= cpsw_set_msglevel,
3160df828598SMugunthan V N 	.get_link	= ethtool_op_get_link,
31612e5b38abSRichard Cochran 	.get_ts_info	= cpsw_get_ts_info,
3162ff5b8ef2SMugunthan V N 	.get_coalesce	= cpsw_get_coalesce,
3163ff5b8ef2SMugunthan V N 	.set_coalesce	= cpsw_set_coalesce,
3164d9718546SMugunthan V N 	.get_sset_count		= cpsw_get_sset_count,
3165d9718546SMugunthan V N 	.get_strings		= cpsw_get_strings,
3166d9718546SMugunthan V N 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
31671923d6e4SMugunthan V N 	.get_pauseparam		= cpsw_get_pauseparam,
31681923d6e4SMugunthan V N 	.set_pauseparam		= cpsw_set_pauseparam,
3169d8a64420SMatus Ujhelyi 	.get_wol	= cpsw_get_wol,
3170d8a64420SMatus Ujhelyi 	.set_wol	= cpsw_set_wol,
317152c4f0ecSMugunthan V N 	.get_regs_len	= cpsw_get_regs_len,
317252c4f0ecSMugunthan V N 	.get_regs	= cpsw_get_regs,
31737898b1daSGrygorii Strashko 	.begin		= cpsw_ethtool_op_begin,
31747898b1daSGrygorii Strashko 	.complete	= cpsw_ethtool_op_complete,
3175ce52c744SIvan Khoronzhuk 	.get_channels	= cpsw_get_channels,
3176ce52c744SIvan Khoronzhuk 	.set_channels	= cpsw_set_channels,
31772479876dSPhilippe Reynes 	.get_link_ksettings	= cpsw_get_link_ksettings,
31782479876dSPhilippe Reynes 	.set_link_ksettings	= cpsw_set_link_ksettings,
3179a0909949SYegor Yefremov 	.get_eee	= cpsw_get_eee,
3180a0909949SYegor Yefremov 	.set_eee	= cpsw_set_eee,
31816bb10c2bSYegor Yefremov 	.nway_reset	= cpsw_nway_reset,
3182be034fc1SGrygorii Strashko 	.get_ringparam = cpsw_get_ringparam,
3183be034fc1SGrygorii Strashko 	.set_ringparam = cpsw_set_ringparam,
3184df828598SMugunthan V N };
3185df828598SMugunthan V N 
3186606f3993SIvan Khoronzhuk static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
3187549985eeSRichard Cochran 			    u32 slave_reg_ofs, u32 sliver_reg_ofs)
3188df828598SMugunthan V N {
31895d8d0d4dSIvan Khoronzhuk 	void __iomem		*regs = cpsw->regs;
3190df828598SMugunthan V N 	int			slave_num = slave->slave_num;
3191606f3993SIvan Khoronzhuk 	struct cpsw_slave_data	*data = cpsw->data.slave_data + slave_num;
3192df828598SMugunthan V N 
3193df828598SMugunthan V N 	slave->data	= data;
3194549985eeSRichard Cochran 	slave->regs	= regs + slave_reg_ofs;
3195549985eeSRichard Cochran 	slave->sliver	= regs + sliver_reg_ofs;
3196d9ba8f9eSMugunthan V N 	slave->port_vlan = data->dual_emac_res_vlan;
3197df828598SMugunthan V N }
3198df828598SMugunthan V N 
3199552165bcSDavid Rivshin static int cpsw_probe_dt(struct cpsw_platform_data *data,
32002eb32b0aSMugunthan V N 			 struct platform_device *pdev)
32012eb32b0aSMugunthan V N {
32022eb32b0aSMugunthan V N 	struct device_node *node = pdev->dev.of_node;
32032eb32b0aSMugunthan V N 	struct device_node *slave_node;
32042eb32b0aSMugunthan V N 	int i = 0, ret;
32052eb32b0aSMugunthan V N 	u32 prop;
32062eb32b0aSMugunthan V N 
32072eb32b0aSMugunthan V N 	if (!node)
32082eb32b0aSMugunthan V N 		return -EINVAL;
32092eb32b0aSMugunthan V N 
32102eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "slaves", &prop)) {
321188c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
32122eb32b0aSMugunthan V N 		return -EINVAL;
32132eb32b0aSMugunthan V N 	}
32142eb32b0aSMugunthan V N 	data->slaves = prop;
32152eb32b0aSMugunthan V N 
3216e86ac13bSMugunthan V N 	if (of_property_read_u32(node, "active_slave", &prop)) {
321788c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
3218aa1a15e2SDaniel Mack 		return -EINVAL;
321978ca0b28SRichard Cochran 	}
3220e86ac13bSMugunthan V N 	data->active_slave = prop;
322178ca0b28SRichard Cochran 
3222a86854d0SKees Cook 	data->slave_data = devm_kcalloc(&pdev->dev,
3223a86854d0SKees Cook 					data->slaves,
3224a86854d0SKees Cook 					sizeof(struct cpsw_slave_data),
3225b2adaca9SJoe Perches 					GFP_KERNEL);
3226b2adaca9SJoe Perches 	if (!data->slave_data)
3227aa1a15e2SDaniel Mack 		return -ENOMEM;
32282eb32b0aSMugunthan V N 
32292eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "cpdma_channels", &prop)) {
323088c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
3231aa1a15e2SDaniel Mack 		return -EINVAL;
32322eb32b0aSMugunthan V N 	}
32332eb32b0aSMugunthan V N 	data->channels = prop;
32342eb32b0aSMugunthan V N 
32352eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "ale_entries", &prop)) {
323688c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
3237aa1a15e2SDaniel Mack 		return -EINVAL;
32382eb32b0aSMugunthan V N 	}
32392eb32b0aSMugunthan V N 	data->ale_entries = prop;
32402eb32b0aSMugunthan V N 
32412eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "bd_ram_size", &prop)) {
324288c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
3243aa1a15e2SDaniel Mack 		return -EINVAL;
32442eb32b0aSMugunthan V N 	}
32452eb32b0aSMugunthan V N 	data->bd_ram_size = prop;
32462eb32b0aSMugunthan V N 
32472eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "mac_control", &prop)) {
324888c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
3249aa1a15e2SDaniel Mack 		return -EINVAL;
32502eb32b0aSMugunthan V N 	}
32512eb32b0aSMugunthan V N 	data->mac_control = prop;
32522eb32b0aSMugunthan V N 
3253281abd96SMarkus Pargmann 	if (of_property_read_bool(node, "dual_emac"))
3254281abd96SMarkus Pargmann 		data->dual_emac = 1;
3255d9ba8f9eSMugunthan V N 
32561fb19aa7SVaibhav Hiremath 	/*
32571fb19aa7SVaibhav Hiremath 	 * Populate all the child nodes here...
32581fb19aa7SVaibhav Hiremath 	 */
32591fb19aa7SVaibhav Hiremath 	ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
32601fb19aa7SVaibhav Hiremath 	/* We do not want to force this, as in some cases may not have child */
32611fb19aa7SVaibhav Hiremath 	if (ret)
326288c99ff6SGeorge Cherian 		dev_warn(&pdev->dev, "Doesn't have any child node\n");
32631fb19aa7SVaibhav Hiremath 
32648658aaf2SBen Hutchings 	for_each_available_child_of_node(node, slave_node) {
3265549985eeSRichard Cochran 		struct cpsw_slave_data *slave_data = data->slave_data + i;
3266549985eeSRichard Cochran 		const void *mac_addr = NULL;
3267549985eeSRichard Cochran 		int lenp;
3268549985eeSRichard Cochran 		const __be32 *parp;
3269549985eeSRichard Cochran 
3270f468b10eSMarkus Pargmann 		/* This is no slave child node, continue */
3271f468b10eSMarkus Pargmann 		if (strcmp(slave_node->name, "slave"))
3272f468b10eSMarkus Pargmann 			continue;
3273f468b10eSMarkus Pargmann 
3274552165bcSDavid Rivshin 		slave_data->phy_node = of_parse_phandle(slave_node,
3275552165bcSDavid Rivshin 							"phy-handle", 0);
3276f1eea5c1SDavid Rivshin 		parp = of_get_property(slave_node, "phy_id", &lenp);
3277ae092b5bSDavid Rivshin 		if (slave_data->phy_node) {
3278ae092b5bSDavid Rivshin 			dev_dbg(&pdev->dev,
3279f7ce9103SRob Herring 				"slave[%d] using phy-handle=\"%pOF\"\n",
3280f7ce9103SRob Herring 				i, slave_data->phy_node);
3281ae092b5bSDavid Rivshin 		} else if (of_phy_is_fixed_link(slave_node)) {
3282dfc0a6d3SDavid Rivshin 			/* In the case of a fixed PHY, the DT node associated
3283dfc0a6d3SDavid Rivshin 			 * to the PHY is the Ethernet MAC DT node.
3284dfc0a6d3SDavid Rivshin 			 */
32851f71e8c9SMarkus Brunner 			ret = of_phy_register_fixed_link(slave_node);
328623a09873SJohan Hovold 			if (ret) {
328723a09873SJohan Hovold 				if (ret != -EPROBE_DEFER)
328823a09873SJohan Hovold 					dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
32891f71e8c9SMarkus Brunner 				return ret;
329023a09873SJohan Hovold 			}
329106cd6d6eSDavid Rivshin 			slave_data->phy_node = of_node_get(slave_node);
3292f1eea5c1SDavid Rivshin 		} else if (parp) {
3293f1eea5c1SDavid Rivshin 			u32 phyid;
3294f1eea5c1SDavid Rivshin 			struct device_node *mdio_node;
3295f1eea5c1SDavid Rivshin 			struct platform_device *mdio;
3296f1eea5c1SDavid Rivshin 
3297f1eea5c1SDavid Rivshin 			if (lenp != (sizeof(__be32) * 2)) {
3298f1eea5c1SDavid Rivshin 				dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
329947276fccSMugunthan V N 				goto no_phy_slave;
3300549985eeSRichard Cochran 			}
3301549985eeSRichard Cochran 			mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
3302549985eeSRichard Cochran 			phyid = be32_to_cpup(parp+1);
3303549985eeSRichard Cochran 			mdio = of_find_device_by_node(mdio_node);
330460e71ab5SJohan Hovold 			of_node_put(mdio_node);
33056954cc1fSJohan Hovold 			if (!mdio) {
330656fdb2e0SMarkus Pargmann 				dev_err(&pdev->dev, "Missing mdio platform device\n");
33076954cc1fSJohan Hovold 				return -EINVAL;
33086954cc1fSJohan Hovold 			}
3309549985eeSRichard Cochran 			snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
3310549985eeSRichard Cochran 				 PHY_ID_FMT, mdio->name, phyid);
331186e1d5adSJohan Hovold 			put_device(&mdio->dev);
3312f1eea5c1SDavid Rivshin 		} else {
3313ae092b5bSDavid Rivshin 			dev_err(&pdev->dev,
3314ae092b5bSDavid Rivshin 				"No slave[%d] phy_id, phy-handle, or fixed-link property\n",
3315ae092b5bSDavid Rivshin 				i);
3316f1eea5c1SDavid Rivshin 			goto no_phy_slave;
3317f1eea5c1SDavid Rivshin 		}
331847276fccSMugunthan V N 		slave_data->phy_if = of_get_phy_mode(slave_node);
331947276fccSMugunthan V N 		if (slave_data->phy_if < 0) {
332047276fccSMugunthan V N 			dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
332147276fccSMugunthan V N 				i);
332247276fccSMugunthan V N 			return slave_data->phy_if;
332347276fccSMugunthan V N 		}
332447276fccSMugunthan V N 
332547276fccSMugunthan V N no_phy_slave:
3326549985eeSRichard Cochran 		mac_addr = of_get_mac_address(slave_node);
33270ba517b1SMarkus Pargmann 		if (mac_addr) {
3328549985eeSRichard Cochran 			memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
33290ba517b1SMarkus Pargmann 		} else {
3330b6745f6eSMugunthan V N 			ret = ti_cm_get_macid(&pdev->dev, i,
33310ba517b1SMarkus Pargmann 					      slave_data->mac_addr);
33320ba517b1SMarkus Pargmann 			if (ret)
33330ba517b1SMarkus Pargmann 				return ret;
33340ba517b1SMarkus Pargmann 		}
3335d9ba8f9eSMugunthan V N 		if (data->dual_emac) {
333691c4166cSMugunthan V N 			if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
3337d9ba8f9eSMugunthan V N 						 &prop)) {
333888c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
3339d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = i+1;
334088c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
3341d9ba8f9eSMugunthan V N 					slave_data->dual_emac_res_vlan, i);
3342d9ba8f9eSMugunthan V N 			} else {
3343d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = prop;
3344d9ba8f9eSMugunthan V N 			}
3345d9ba8f9eSMugunthan V N 		}
3346d9ba8f9eSMugunthan V N 
3347549985eeSRichard Cochran 		i++;
33483a27bfacSMugunthan V N 		if (i == data->slaves)
33493a27bfacSMugunthan V N 			break;
3350549985eeSRichard Cochran 	}
3351549985eeSRichard Cochran 
33522eb32b0aSMugunthan V N 	return 0;
33532eb32b0aSMugunthan V N }
33542eb32b0aSMugunthan V N 
3355a4e32b0dSJohan Hovold static void cpsw_remove_dt(struct platform_device *pdev)
3356a4e32b0dSJohan Hovold {
33578cbcc466SJohan Hovold 	struct net_device *ndev = platform_get_drvdata(pdev);
33588cbcc466SJohan Hovold 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
33598cbcc466SJohan Hovold 	struct cpsw_platform_data *data = &cpsw->data;
33608cbcc466SJohan Hovold 	struct device_node *node = pdev->dev.of_node;
33618cbcc466SJohan Hovold 	struct device_node *slave_node;
33628cbcc466SJohan Hovold 	int i = 0;
33638cbcc466SJohan Hovold 
33648cbcc466SJohan Hovold 	for_each_available_child_of_node(node, slave_node) {
33658cbcc466SJohan Hovold 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
33668cbcc466SJohan Hovold 
33678cbcc466SJohan Hovold 		if (strcmp(slave_node->name, "slave"))
33688cbcc466SJohan Hovold 			continue;
33698cbcc466SJohan Hovold 
33703f65047cSJohan Hovold 		if (of_phy_is_fixed_link(slave_node))
33713f65047cSJohan Hovold 			of_phy_deregister_fixed_link(slave_node);
33728cbcc466SJohan Hovold 
33738cbcc466SJohan Hovold 		of_node_put(slave_data->phy_node);
33748cbcc466SJohan Hovold 
33758cbcc466SJohan Hovold 		i++;
33768cbcc466SJohan Hovold 		if (i == data->slaves)
33778cbcc466SJohan Hovold 			break;
33788cbcc466SJohan Hovold 	}
33798cbcc466SJohan Hovold 
3380a4e32b0dSJohan Hovold 	of_platform_depopulate(&pdev->dev);
3381a4e32b0dSJohan Hovold }
3382a4e32b0dSJohan Hovold 
338356e31bd8SIvan Khoronzhuk static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
3384d9ba8f9eSMugunthan V N {
3385606f3993SIvan Khoronzhuk 	struct cpsw_common		*cpsw = priv->cpsw;
3386606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	*data = &cpsw->data;
3387d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
3388d9ba8f9eSMugunthan V N 	struct cpsw_priv		*priv_sl2;
3389e38b5a3dSIvan Khoronzhuk 	int ret = 0;
3390d9ba8f9eSMugunthan V N 
3391e05107e6SIvan Khoronzhuk 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3392d9ba8f9eSMugunthan V N 	if (!ndev) {
339356e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
3394d9ba8f9eSMugunthan V N 		return -ENOMEM;
3395d9ba8f9eSMugunthan V N 	}
3396d9ba8f9eSMugunthan V N 
3397d9ba8f9eSMugunthan V N 	priv_sl2 = netdev_priv(ndev);
3398606f3993SIvan Khoronzhuk 	priv_sl2->cpsw = cpsw;
3399d9ba8f9eSMugunthan V N 	priv_sl2->ndev = ndev;
3400d9ba8f9eSMugunthan V N 	priv_sl2->dev  = &ndev->dev;
3401d9ba8f9eSMugunthan V N 	priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
3402d9ba8f9eSMugunthan V N 
3403d9ba8f9eSMugunthan V N 	if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
3404d9ba8f9eSMugunthan V N 		memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
3405d9ba8f9eSMugunthan V N 			ETH_ALEN);
340656e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
340756e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
3408d9ba8f9eSMugunthan V N 	} else {
34096c1f0a1fSJoe Perches 		eth_random_addr(priv_sl2->mac_addr);
341056e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
341156e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
3412d9ba8f9eSMugunthan V N 	}
3413d9ba8f9eSMugunthan V N 	memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
3414d9ba8f9eSMugunthan V N 
3415d9ba8f9eSMugunthan V N 	priv_sl2->emac_port = 1;
3416606f3993SIvan Khoronzhuk 	cpsw->slaves[1].ndev = ndev;
3417193736c8SIvan Khoronzhuk 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3418d9ba8f9eSMugunthan V N 
3419d9ba8f9eSMugunthan V N 	ndev->netdev_ops = &cpsw_netdev_ops;
34207ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &cpsw_ethtool_ops;
3421d9ba8f9eSMugunthan V N 
3422d9ba8f9eSMugunthan V N 	/* register the network device */
342356e31bd8SIvan Khoronzhuk 	SET_NETDEV_DEV(ndev, cpsw->dev);
3424d9ba8f9eSMugunthan V N 	ret = register_netdev(ndev);
3425d9ba8f9eSMugunthan V N 	if (ret) {
342656e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error registering net device\n");
3427d9ba8f9eSMugunthan V N 		free_netdev(ndev);
3428d9ba8f9eSMugunthan V N 		ret = -ENODEV;
3429d9ba8f9eSMugunthan V N 	}
3430d9ba8f9eSMugunthan V N 
3431d9ba8f9eSMugunthan V N 	return ret;
3432d9ba8f9eSMugunthan V N }
3433d9ba8f9eSMugunthan V N 
34347da11600SMugunthan V N static const struct of_device_id cpsw_of_mtable[] = {
34359611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,cpsw"},
34369611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am335x-cpsw"},
34379611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am4372-cpsw"},
34389611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,dra7-cpsw"},
34397da11600SMugunthan V N 	{ /* sentinel */ },
34407da11600SMugunthan V N };
34417da11600SMugunthan V N MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
34427da11600SMugunthan V N 
34439611d6d6SIvan Khoronzhuk static const struct soc_device_attribute cpsw_soc_devices[] = {
34449611d6d6SIvan Khoronzhuk 	{ .family = "AM33xx", .revision = "ES1.0"},
34459611d6d6SIvan Khoronzhuk 	{ /* sentinel */ }
34469611d6d6SIvan Khoronzhuk };
34479611d6d6SIvan Khoronzhuk 
3448663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev)
3449df828598SMugunthan V N {
3450ef4183a1SIvan Khoronzhuk 	struct clk			*clk;
3451d1bd9acfSSebastian Siewior 	struct cpsw_platform_data	*data;
3452df828598SMugunthan V N 	struct net_device		*ndev;
3453df828598SMugunthan V N 	struct cpsw_priv		*priv;
3454df828598SMugunthan V N 	struct cpdma_params		dma_params;
3455df828598SMugunthan V N 	struct cpsw_ale_params		ale_params;
3456aa1a15e2SDaniel Mack 	void __iomem			*ss_regs;
34578a2c9a5aSGrygorii Strashko 	void __iomem			*cpts_regs;
3458aa1a15e2SDaniel Mack 	struct resource			*res, *ss_res;
34591d147ccbSMugunthan V N 	struct gpio_descs		*mode;
3460549985eeSRichard Cochran 	u32 slave_offset, sliver_offset, slave_size;
34619611d6d6SIvan Khoronzhuk 	const struct soc_device_attribute *soc;
3462649a1688SIvan Khoronzhuk 	struct cpsw_common		*cpsw;
346379b3325dSIvan Khoronzhuk 	int ret = 0, i, ch;
34645087b915SFelipe Balbi 	int irq;
3465df828598SMugunthan V N 
3466649a1688SIvan Khoronzhuk 	cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
34673420ea88SJohan Hovold 	if (!cpsw)
34683420ea88SJohan Hovold 		return -ENOMEM;
34693420ea88SJohan Hovold 
347056e31bd8SIvan Khoronzhuk 	cpsw->dev = &pdev->dev;
3471649a1688SIvan Khoronzhuk 
3472e05107e6SIvan Khoronzhuk 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3473df828598SMugunthan V N 	if (!ndev) {
347488c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "error allocating net_device\n");
3475df828598SMugunthan V N 		return -ENOMEM;
3476df828598SMugunthan V N 	}
3477df828598SMugunthan V N 
3478df828598SMugunthan V N 	platform_set_drvdata(pdev, ndev);
3479df828598SMugunthan V N 	priv = netdev_priv(ndev);
3480649a1688SIvan Khoronzhuk 	priv->cpsw = cpsw;
3481df828598SMugunthan V N 	priv->ndev = ndev;
3482df828598SMugunthan V N 	priv->dev  = &ndev->dev;
3483df828598SMugunthan V N 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
34842a05a622SIvan Khoronzhuk 	cpsw->rx_packet_max = max(rx_packet_max, 128);
3485df828598SMugunthan V N 
34861d147ccbSMugunthan V N 	mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
34871d147ccbSMugunthan V N 	if (IS_ERR(mode)) {
34881d147ccbSMugunthan V N 		ret = PTR_ERR(mode);
34891d147ccbSMugunthan V N 		dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
34901d147ccbSMugunthan V N 		goto clean_ndev_ret;
34911d147ccbSMugunthan V N 	}
34921d147ccbSMugunthan V N 
34931fb19aa7SVaibhav Hiremath 	/*
34941fb19aa7SVaibhav Hiremath 	 * This may be required here for child devices.
34951fb19aa7SVaibhav Hiremath 	 */
34961fb19aa7SVaibhav Hiremath 	pm_runtime_enable(&pdev->dev);
34971fb19aa7SVaibhav Hiremath 
3498739683b4SMugunthan V N 	/* Select default pin state */
3499739683b4SMugunthan V N 	pinctrl_pm_select_default_state(&pdev->dev);
3500739683b4SMugunthan V N 
3501a4e32b0dSJohan Hovold 	/* Need to enable clocks with runtime PM api to access module
3502a4e32b0dSJohan Hovold 	 * registers
3503a4e32b0dSJohan Hovold 	 */
3504a4e32b0dSJohan Hovold 	ret = pm_runtime_get_sync(&pdev->dev);
3505a4e32b0dSJohan Hovold 	if (ret < 0) {
3506a4e32b0dSJohan Hovold 		pm_runtime_put_noidle(&pdev->dev);
3507aa1a15e2SDaniel Mack 		goto clean_runtime_disable_ret;
35082eb32b0aSMugunthan V N 	}
3509a4e32b0dSJohan Hovold 
351023a09873SJohan Hovold 	ret = cpsw_probe_dt(&cpsw->data, pdev);
351123a09873SJohan Hovold 	if (ret)
3512a4e32b0dSJohan Hovold 		goto clean_dt_ret;
351323a09873SJohan Hovold 
3514606f3993SIvan Khoronzhuk 	data = &cpsw->data;
3515e05107e6SIvan Khoronzhuk 	cpsw->rx_ch_num = 1;
3516e05107e6SIvan Khoronzhuk 	cpsw->tx_ch_num = 1;
35172eb32b0aSMugunthan V N 
3518df828598SMugunthan V N 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
3519df828598SMugunthan V N 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
352088c99ff6SGeorge Cherian 		dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
3521df828598SMugunthan V N 	} else {
35227efd26d0SJoe Perches 		eth_random_addr(priv->mac_addr);
352388c99ff6SGeorge Cherian 		dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
3524df828598SMugunthan V N 	}
3525df828598SMugunthan V N 
3526df828598SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
3527df828598SMugunthan V N 
3528a86854d0SKees Cook 	cpsw->slaves = devm_kcalloc(&pdev->dev,
3529a86854d0SKees Cook 				    data->slaves, sizeof(struct cpsw_slave),
3530df828598SMugunthan V N 				    GFP_KERNEL);
3531606f3993SIvan Khoronzhuk 	if (!cpsw->slaves) {
3532aa1a15e2SDaniel Mack 		ret = -ENOMEM;
3533a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3534df828598SMugunthan V N 	}
3535df828598SMugunthan V N 	for (i = 0; i < data->slaves; i++)
3536606f3993SIvan Khoronzhuk 		cpsw->slaves[i].slave_num = i;
3537df828598SMugunthan V N 
3538606f3993SIvan Khoronzhuk 	cpsw->slaves[0].ndev = ndev;
3539d9ba8f9eSMugunthan V N 	priv->emac_port = 0;
3540d9ba8f9eSMugunthan V N 
3541ef4183a1SIvan Khoronzhuk 	clk = devm_clk_get(&pdev->dev, "fck");
3542ef4183a1SIvan Khoronzhuk 	if (IS_ERR(clk)) {
3543aa1a15e2SDaniel Mack 		dev_err(priv->dev, "fck is not found\n");
3544f150bd7fSMugunthan V N 		ret = -ENODEV;
3545a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3546df828598SMugunthan V N 	}
35472a05a622SIvan Khoronzhuk 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
3548df828598SMugunthan V N 
3549aa1a15e2SDaniel Mack 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3550aa1a15e2SDaniel Mack 	ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
3551aa1a15e2SDaniel Mack 	if (IS_ERR(ss_regs)) {
3552aa1a15e2SDaniel Mack 		ret = PTR_ERR(ss_regs);
3553a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3554df828598SMugunthan V N 	}
35555d8d0d4dSIvan Khoronzhuk 	cpsw->regs = ss_regs;
3556df828598SMugunthan V N 
35572a05a622SIvan Khoronzhuk 	cpsw->version = readl(&cpsw->regs->id_ver);
3558f280e89aSMugunthan V N 
3559aa1a15e2SDaniel Mack 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
35605d8d0d4dSIvan Khoronzhuk 	cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
35615d8d0d4dSIvan Khoronzhuk 	if (IS_ERR(cpsw->wr_regs)) {
35625d8d0d4dSIvan Khoronzhuk 		ret = PTR_ERR(cpsw->wr_regs);
3563a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3564df828598SMugunthan V N 	}
3565df828598SMugunthan V N 
3566df828598SMugunthan V N 	memset(&dma_params, 0, sizeof(dma_params));
3567549985eeSRichard Cochran 	memset(&ale_params, 0, sizeof(ale_params));
3568549985eeSRichard Cochran 
35692a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
3570549985eeSRichard Cochran 	case CPSW_VERSION_1:
35715d8d0d4dSIvan Khoronzhuk 		cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
35728a2c9a5aSGrygorii Strashko 		cpts_regs		= ss_regs + CPSW1_CPTS_OFFSET;
35735d8d0d4dSIvan Khoronzhuk 		cpsw->hw_stats	     = ss_regs + CPSW1_HW_STATS;
3574549985eeSRichard Cochran 		dma_params.dmaregs   = ss_regs + CPSW1_CPDMA_OFFSET;
3575549985eeSRichard Cochran 		dma_params.txhdp     = ss_regs + CPSW1_STATERAM_OFFSET;
3576549985eeSRichard Cochran 		ale_params.ale_regs  = ss_regs + CPSW1_ALE_OFFSET;
3577549985eeSRichard Cochran 		slave_offset         = CPSW1_SLAVE_OFFSET;
3578549985eeSRichard Cochran 		slave_size           = CPSW1_SLAVE_SIZE;
3579549985eeSRichard Cochran 		sliver_offset        = CPSW1_SLIVER_OFFSET;
3580549985eeSRichard Cochran 		dma_params.desc_mem_phys = 0;
3581549985eeSRichard Cochran 		break;
3582549985eeSRichard Cochran 	case CPSW_VERSION_2:
3583c193f365SMugunthan V N 	case CPSW_VERSION_3:
3584926489beSMugunthan V N 	case CPSW_VERSION_4:
35855d8d0d4dSIvan Khoronzhuk 		cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
35868a2c9a5aSGrygorii Strashko 		cpts_regs		= ss_regs + CPSW2_CPTS_OFFSET;
35875d8d0d4dSIvan Khoronzhuk 		cpsw->hw_stats	     = ss_regs + CPSW2_HW_STATS;
3588549985eeSRichard Cochran 		dma_params.dmaregs   = ss_regs + CPSW2_CPDMA_OFFSET;
3589549985eeSRichard Cochran 		dma_params.txhdp     = ss_regs + CPSW2_STATERAM_OFFSET;
3590549985eeSRichard Cochran 		ale_params.ale_regs  = ss_regs + CPSW2_ALE_OFFSET;
3591549985eeSRichard Cochran 		slave_offset         = CPSW2_SLAVE_OFFSET;
3592549985eeSRichard Cochran 		slave_size           = CPSW2_SLAVE_SIZE;
3593549985eeSRichard Cochran 		sliver_offset        = CPSW2_SLIVER_OFFSET;
3594549985eeSRichard Cochran 		dma_params.desc_mem_phys =
3595aa1a15e2SDaniel Mack 			(u32 __force) ss_res->start + CPSW2_BD_OFFSET;
3596549985eeSRichard Cochran 		break;
3597549985eeSRichard Cochran 	default:
35982a05a622SIvan Khoronzhuk 		dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
3599549985eeSRichard Cochran 		ret = -ENODEV;
3600a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3601549985eeSRichard Cochran 	}
3602606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++) {
3603606f3993SIvan Khoronzhuk 		struct cpsw_slave *slave = &cpsw->slaves[i];
3604606f3993SIvan Khoronzhuk 
3605606f3993SIvan Khoronzhuk 		cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
3606549985eeSRichard Cochran 		slave_offset  += slave_size;
3607549985eeSRichard Cochran 		sliver_offset += SLIVER_SIZE;
3608549985eeSRichard Cochran 	}
3609549985eeSRichard Cochran 
3610df828598SMugunthan V N 	dma_params.dev		= &pdev->dev;
3611549985eeSRichard Cochran 	dma_params.rxthresh	= dma_params.dmaregs + CPDMA_RXTHRESH;
3612549985eeSRichard Cochran 	dma_params.rxfree	= dma_params.dmaregs + CPDMA_RXFREE;
3613549985eeSRichard Cochran 	dma_params.rxhdp	= dma_params.txhdp + CPDMA_RXHDP;
3614549985eeSRichard Cochran 	dma_params.txcp		= dma_params.txhdp + CPDMA_TXCP;
3615549985eeSRichard Cochran 	dma_params.rxcp		= dma_params.txhdp + CPDMA_RXCP;
3616df828598SMugunthan V N 
3617df828598SMugunthan V N 	dma_params.num_chan		= data->channels;
3618df828598SMugunthan V N 	dma_params.has_soft_reset	= true;
3619df828598SMugunthan V N 	dma_params.min_packet_size	= CPSW_MIN_PACKET_SIZE;
3620df828598SMugunthan V N 	dma_params.desc_mem_size	= data->bd_ram_size;
3621df828598SMugunthan V N 	dma_params.desc_align		= 16;
3622df828598SMugunthan V N 	dma_params.has_ext_regs		= true;
3623549985eeSRichard Cochran 	dma_params.desc_hw_addr         = dma_params.desc_mem_phys;
362483fcad0cSIvan Khoronzhuk 	dma_params.bus_freq_mhz		= cpsw->bus_freq_mhz;
362590225bf0SGrygorii Strashko 	dma_params.descs_pool_size	= descs_pool_size;
3626df828598SMugunthan V N 
36272c836bd9SIvan Khoronzhuk 	cpsw->dma = cpdma_ctlr_create(&dma_params);
36282c836bd9SIvan Khoronzhuk 	if (!cpsw->dma) {
3629df828598SMugunthan V N 		dev_err(priv->dev, "error initializing dma\n");
3630df828598SMugunthan V N 		ret = -ENOMEM;
3631a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3632df828598SMugunthan V N 	}
3633df828598SMugunthan V N 
36349611d6d6SIvan Khoronzhuk 	soc = soc_device_match(cpsw_soc_devices);
36359611d6d6SIvan Khoronzhuk 	if (soc)
36369611d6d6SIvan Khoronzhuk 		cpsw->quirk_irq = 1;
36379611d6d6SIvan Khoronzhuk 
363879b3325dSIvan Khoronzhuk 	ch = cpsw->quirk_irq ? 0 : 7;
363979b3325dSIvan Khoronzhuk 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
36408a83c5d7SIvan Khoronzhuk 	if (IS_ERR(cpsw->txv[0].ch)) {
36418a83c5d7SIvan Khoronzhuk 		dev_err(priv->dev, "error initializing tx dma channel\n");
36428a83c5d7SIvan Khoronzhuk 		ret = PTR_ERR(cpsw->txv[0].ch);
36438a83c5d7SIvan Khoronzhuk 		goto clean_dma_ret;
36448a83c5d7SIvan Khoronzhuk 	}
36458a83c5d7SIvan Khoronzhuk 
36468feb0a19SIvan Khoronzhuk 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
36478a83c5d7SIvan Khoronzhuk 	if (IS_ERR(cpsw->rxv[0].ch)) {
36488a83c5d7SIvan Khoronzhuk 		dev_err(priv->dev, "error initializing rx dma channel\n");
36498a83c5d7SIvan Khoronzhuk 		ret = PTR_ERR(cpsw->rxv[0].ch);
3650df828598SMugunthan V N 		goto clean_dma_ret;
3651df828598SMugunthan V N 	}
3652df828598SMugunthan V N 
36539fe9aa0bSIvan Khoronzhuk 	ale_params.dev			= &pdev->dev;
3654df828598SMugunthan V N 	ale_params.ale_ageout		= ale_ageout;
3655df828598SMugunthan V N 	ale_params.ale_entries		= data->ale_entries;
3656c6395f12SGrygorii Strashko 	ale_params.ale_ports		= CPSW_ALE_PORTS_NUM;
3657df828598SMugunthan V N 
36582a05a622SIvan Khoronzhuk 	cpsw->ale = cpsw_ale_create(&ale_params);
36592a05a622SIvan Khoronzhuk 	if (!cpsw->ale) {
3660df828598SMugunthan V N 		dev_err(priv->dev, "error initializing ale engine\n");
3661df828598SMugunthan V N 		ret = -ENODEV;
3662df828598SMugunthan V N 		goto clean_dma_ret;
3663df828598SMugunthan V N 	}
3664df828598SMugunthan V N 
36654a88fb95SGrygorii Strashko 	cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
36668a2c9a5aSGrygorii Strashko 	if (IS_ERR(cpsw->cpts)) {
36678a2c9a5aSGrygorii Strashko 		ret = PTR_ERR(cpsw->cpts);
36681971ab58SGrygorii Strashko 		goto clean_dma_ret;
36698a2c9a5aSGrygorii Strashko 	}
36708a2c9a5aSGrygorii Strashko 
3671c03abd84SFelipe Balbi 	ndev->irq = platform_get_irq(pdev, 1);
3672df828598SMugunthan V N 	if (ndev->irq < 0) {
3673df828598SMugunthan V N 		dev_err(priv->dev, "error getting irq resource\n");
3674c1e3334fSJulia Lawall 		ret = ndev->irq;
36751971ab58SGrygorii Strashko 		goto clean_dma_ret;
3676df828598SMugunthan V N 	}
3677df828598SMugunthan V N 
3678a3a41d2fSGrygorii Strashko 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3679070f9c65SKeerthy 
3680070f9c65SKeerthy 	ndev->netdev_ops = &cpsw_netdev_ops;
3681070f9c65SKeerthy 	ndev->ethtool_ops = &cpsw_ethtool_ops;
36829611d6d6SIvan Khoronzhuk 	netif_napi_add(ndev, &cpsw->napi_rx,
36839611d6d6SIvan Khoronzhuk 		       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
36849611d6d6SIvan Khoronzhuk 		       CPSW_POLL_WEIGHT);
36859611d6d6SIvan Khoronzhuk 	netif_tx_napi_add(ndev, &cpsw->napi_tx,
36869611d6d6SIvan Khoronzhuk 			  cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
36879611d6d6SIvan Khoronzhuk 			  CPSW_POLL_WEIGHT);
3688070f9c65SKeerthy 	cpsw_split_res(ndev);
3689070f9c65SKeerthy 
3690070f9c65SKeerthy 	/* register the network device */
3691070f9c65SKeerthy 	SET_NETDEV_DEV(ndev, &pdev->dev);
3692070f9c65SKeerthy 	ret = register_netdev(ndev);
3693070f9c65SKeerthy 	if (ret) {
3694070f9c65SKeerthy 		dev_err(priv->dev, "error registering net device\n");
3695070f9c65SKeerthy 		ret = -ENODEV;
36961971ab58SGrygorii Strashko 		goto clean_dma_ret;
3697070f9c65SKeerthy 	}
3698070f9c65SKeerthy 
3699070f9c65SKeerthy 	if (cpsw->data.dual_emac) {
3700070f9c65SKeerthy 		ret = cpsw_probe_dual_emac(priv);
3701070f9c65SKeerthy 		if (ret) {
3702070f9c65SKeerthy 			cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
3703070f9c65SKeerthy 			goto clean_unregister_netdev_ret;
3704070f9c65SKeerthy 		}
3705070f9c65SKeerthy 	}
3706070f9c65SKeerthy 
3707c03abd84SFelipe Balbi 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3708c03abd84SFelipe Balbi 	 * MISC IRQs which are always kept disabled with this driver so
3709c03abd84SFelipe Balbi 	 * we will not request them.
3710c03abd84SFelipe Balbi 	 *
3711c03abd84SFelipe Balbi 	 * If anyone wants to implement support for those, make sure to
3712c03abd84SFelipe Balbi 	 * first request and append them to irqs_table array.
3713c03abd84SFelipe Balbi 	 */
3714c2b32e58SDaniel Mack 
3715c03abd84SFelipe Balbi 	/* RX IRQ */
37165087b915SFelipe Balbi 	irq = platform_get_irq(pdev, 1);
3717c1e3334fSJulia Lawall 	if (irq < 0) {
3718c1e3334fSJulia Lawall 		ret = irq;
37191971ab58SGrygorii Strashko 		goto clean_dma_ret;
3720c1e3334fSJulia Lawall 	}
37215087b915SFelipe Balbi 
3722e38b5a3dSIvan Khoronzhuk 	cpsw->irqs_table[0] = irq;
3723c03abd84SFelipe Balbi 	ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
3724dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
37255087b915SFelipe Balbi 	if (ret < 0) {
37265087b915SFelipe Balbi 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
37271971ab58SGrygorii Strashko 		goto clean_dma_ret;
3728df828598SMugunthan V N 	}
3729df828598SMugunthan V N 
3730c03abd84SFelipe Balbi 	/* TX IRQ */
37315087b915SFelipe Balbi 	irq = platform_get_irq(pdev, 2);
3732c1e3334fSJulia Lawall 	if (irq < 0) {
3733c1e3334fSJulia Lawall 		ret = irq;
37341971ab58SGrygorii Strashko 		goto clean_dma_ret;
3735c1e3334fSJulia Lawall 	}
37365087b915SFelipe Balbi 
3737e38b5a3dSIvan Khoronzhuk 	cpsw->irqs_table[1] = irq;
3738c03abd84SFelipe Balbi 	ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
3739dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
37405087b915SFelipe Balbi 	if (ret < 0) {
37415087b915SFelipe Balbi 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
37421971ab58SGrygorii Strashko 		goto clean_dma_ret;
37435087b915SFelipe Balbi 	}
3744c2b32e58SDaniel Mack 
374590225bf0SGrygorii Strashko 	cpsw_notice(priv, probe,
374690225bf0SGrygorii Strashko 		    "initialized device (regs %pa, irq %d, pool size %d)\n",
374790225bf0SGrygorii Strashko 		    &ss_res->start, ndev->irq, dma_params.descs_pool_size);
3748d9ba8f9eSMugunthan V N 
3749c46ab7e0SJohan Hovold 	pm_runtime_put(&pdev->dev);
3750c46ab7e0SJohan Hovold 
3751df828598SMugunthan V N 	return 0;
3752df828598SMugunthan V N 
3753a7fe9d46SJohan Hovold clean_unregister_netdev_ret:
3754a7fe9d46SJohan Hovold 	unregister_netdev(ndev);
3755df828598SMugunthan V N clean_dma_ret:
37562c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
3757a4e32b0dSJohan Hovold clean_dt_ret:
3758a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
3759c46ab7e0SJohan Hovold 	pm_runtime_put_sync(&pdev->dev);
3760aa1a15e2SDaniel Mack clean_runtime_disable_ret:
3761f150bd7fSMugunthan V N 	pm_runtime_disable(&pdev->dev);
3762df828598SMugunthan V N clean_ndev_ret:
3763d1bd9acfSSebastian Siewior 	free_netdev(priv->ndev);
3764df828598SMugunthan V N 	return ret;
3765df828598SMugunthan V N }
3766df828598SMugunthan V N 
3767663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev)
3768df828598SMugunthan V N {
3769df828598SMugunthan V N 	struct net_device *ndev = platform_get_drvdata(pdev);
37702a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
37718a0b6dc9SGrygorii Strashko 	int ret;
37728a0b6dc9SGrygorii Strashko 
37738a0b6dc9SGrygorii Strashko 	ret = pm_runtime_get_sync(&pdev->dev);
37748a0b6dc9SGrygorii Strashko 	if (ret < 0) {
37758a0b6dc9SGrygorii Strashko 		pm_runtime_put_noidle(&pdev->dev);
37768a0b6dc9SGrygorii Strashko 		return ret;
37778a0b6dc9SGrygorii Strashko 	}
3778df828598SMugunthan V N 
3779606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
3780606f3993SIvan Khoronzhuk 		unregister_netdev(cpsw->slaves[1].ndev);
3781d1bd9acfSSebastian Siewior 	unregister_netdev(ndev);
3782df828598SMugunthan V N 
37838a2c9a5aSGrygorii Strashko 	cpts_release(cpsw->cpts);
37842c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
3785a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
37868a0b6dc9SGrygorii Strashko 	pm_runtime_put_sync(&pdev->dev);
37878a0b6dc9SGrygorii Strashko 	pm_runtime_disable(&pdev->dev);
3788606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
3789606f3993SIvan Khoronzhuk 		free_netdev(cpsw->slaves[1].ndev);
3790df828598SMugunthan V N 	free_netdev(ndev);
3791df828598SMugunthan V N 	return 0;
3792df828598SMugunthan V N }
3793df828598SMugunthan V N 
37948963a504SGrygorii Strashko #ifdef CONFIG_PM_SLEEP
3795df828598SMugunthan V N static int cpsw_suspend(struct device *dev)
3796df828598SMugunthan V N {
37974e13c252SWolfram Sang 	struct net_device	*ndev = dev_get_drvdata(dev);
3798606f3993SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3799df828598SMugunthan V N 
3800606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
3801618073e3SMugunthan V N 		int i;
3802618073e3SMugunthan V N 
3803606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
3804606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
3805606f3993SIvan Khoronzhuk 				cpsw_ndo_stop(cpsw->slaves[i].ndev);
3806618073e3SMugunthan V N 		}
3807618073e3SMugunthan V N 	} else {
3808df828598SMugunthan V N 		if (netif_running(ndev))
3809df828598SMugunthan V N 			cpsw_ndo_stop(ndev);
3810618073e3SMugunthan V N 	}
38111e7a2e21SDaniel Mack 
3812739683b4SMugunthan V N 	/* Select sleep pin state */
381356e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_sleep_state(dev);
3814739683b4SMugunthan V N 
3815df828598SMugunthan V N 	return 0;
3816df828598SMugunthan V N }
3817df828598SMugunthan V N 
3818df828598SMugunthan V N static int cpsw_resume(struct device *dev)
3819df828598SMugunthan V N {
38204e13c252SWolfram Sang 	struct net_device	*ndev = dev_get_drvdata(dev);
3821a60ced99SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3822df828598SMugunthan V N 
3823739683b4SMugunthan V N 	/* Select default pin state */
382456e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_default_state(dev);
3825739683b4SMugunthan V N 
38264ccfd638SGrygorii Strashko 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
38274ccfd638SGrygorii Strashko 	rtnl_lock();
3828606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
3829618073e3SMugunthan V N 		int i;
3830618073e3SMugunthan V N 
3831606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
3832606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
3833606f3993SIvan Khoronzhuk 				cpsw_ndo_open(cpsw->slaves[i].ndev);
3834618073e3SMugunthan V N 		}
3835618073e3SMugunthan V N 	} else {
3836df828598SMugunthan V N 		if (netif_running(ndev))
3837df828598SMugunthan V N 			cpsw_ndo_open(ndev);
3838618073e3SMugunthan V N 	}
38394ccfd638SGrygorii Strashko 	rtnl_unlock();
38404ccfd638SGrygorii Strashko 
3841df828598SMugunthan V N 	return 0;
3842df828598SMugunthan V N }
38438963a504SGrygorii Strashko #endif
3844df828598SMugunthan V N 
38458963a504SGrygorii Strashko static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
3846df828598SMugunthan V N 
3847df828598SMugunthan V N static struct platform_driver cpsw_driver = {
3848df828598SMugunthan V N 	.driver = {
3849df828598SMugunthan V N 		.name	 = "cpsw",
3850df828598SMugunthan V N 		.pm	 = &cpsw_pm_ops,
38511e5c76d4SSachin Kamat 		.of_match_table = cpsw_of_mtable,
3852df828598SMugunthan V N 	},
3853df828598SMugunthan V N 	.probe = cpsw_probe,
3854663e12e6SBill Pemberton 	.remove = cpsw_remove,
3855df828598SMugunthan V N };
3856df828598SMugunthan V N 
38576fb3b6b5SGrygorii Strashko module_platform_driver(cpsw_driver);
3858df828598SMugunthan V N 
3859df828598SMugunthan V N MODULE_LICENSE("GPL");
3860df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3861df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3862df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver");
3863