xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw.c (revision 5da19489)
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 
5686f1f5836SIvan Khoronzhuk static inline int cpsw_get_slave_port(u32 slave_num)
569d9ba8f9eSMugunthan V N {
570d9ba8f9eSMugunthan V N 	return slave_num + 1;
571d9ba8f9eSMugunthan V N }
572df828598SMugunthan V N 
5735da19489SIvan Khoronzhuk static void cpsw_add_mcast(struct cpsw_priv *priv, const u8 *addr)
574fea49f60SIvan Khoronzhuk {
575fea49f60SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
576fea49f60SIvan Khoronzhuk 
577fea49f60SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
578fea49f60SIvan Khoronzhuk 		struct cpsw_slave *slave = cpsw->slaves + priv->emac_port;
579fea49f60SIvan Khoronzhuk 		int slave_port = cpsw_get_slave_port(slave->slave_num);
580fea49f60SIvan Khoronzhuk 
581fea49f60SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, addr,
582fea49f60SIvan Khoronzhuk 				   1 << slave_port | ALE_PORT_HOST,
583fea49f60SIvan Khoronzhuk 				   ALE_VLAN, slave->port_vlan, 0);
584fea49f60SIvan Khoronzhuk 		return;
585fea49f60SIvan Khoronzhuk 	}
586fea49f60SIvan Khoronzhuk 
587fea49f60SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, addr, ALE_ALL_PORTS, 0, 0, 0);
588fea49f60SIvan Khoronzhuk }
589fea49f60SIvan Khoronzhuk 
5900cd8f9ccSMugunthan V N static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
5910cd8f9ccSMugunthan V N {
5922a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
5932a05a622SIvan Khoronzhuk 	struct cpsw_ale *ale = cpsw->ale;
5940cd8f9ccSMugunthan V N 	int i;
5950cd8f9ccSMugunthan V N 
596606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
5970cd8f9ccSMugunthan V N 		bool flag = false;
5980cd8f9ccSMugunthan V N 
5990cd8f9ccSMugunthan V N 		/* Enabling promiscuous mode for one interface will be
6000cd8f9ccSMugunthan V N 		 * common for both the interface as the interface shares
6010cd8f9ccSMugunthan V N 		 * the same hardware resource.
6020cd8f9ccSMugunthan V N 		 */
603606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++)
604606f3993SIvan Khoronzhuk 			if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
6050cd8f9ccSMugunthan V N 				flag = true;
6060cd8f9ccSMugunthan V N 
6070cd8f9ccSMugunthan V N 		if (!enable && flag) {
6080cd8f9ccSMugunthan V N 			enable = true;
6090cd8f9ccSMugunthan V N 			dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
6100cd8f9ccSMugunthan V N 		}
6110cd8f9ccSMugunthan V N 
6120cd8f9ccSMugunthan V N 		if (enable) {
6130cd8f9ccSMugunthan V N 			/* Enable Bypass */
6140cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
6150cd8f9ccSMugunthan V N 
6160cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
6170cd8f9ccSMugunthan V N 		} else {
6180cd8f9ccSMugunthan V N 			/* Disable Bypass */
6190cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
6200cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
6210cd8f9ccSMugunthan V N 		}
6220cd8f9ccSMugunthan V N 	} else {
6230cd8f9ccSMugunthan V N 		if (enable) {
6240cd8f9ccSMugunthan V N 			unsigned long timeout = jiffies + HZ;
6250cd8f9ccSMugunthan V N 
6266f979eb3SLennart Sorensen 			/* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
627606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
6280cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6290cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 1);
6300cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6310cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 1);
6320cd8f9ccSMugunthan V N 			}
6330cd8f9ccSMugunthan V N 
6340cd8f9ccSMugunthan V N 			/* Clear All Untouched entries */
6350cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
6360cd8f9ccSMugunthan V N 			do {
6370cd8f9ccSMugunthan V N 				cpu_relax();
6380cd8f9ccSMugunthan V N 				if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
6390cd8f9ccSMugunthan V N 					break;
6400cd8f9ccSMugunthan V N 			} while (time_after(timeout, jiffies));
6410cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
6420cd8f9ccSMugunthan V N 
6430cd8f9ccSMugunthan V N 			/* Clear all mcast from ALE */
64461f1cef9SGrygorii Strashko 			cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
6450cd8f9ccSMugunthan V N 
6460cd8f9ccSMugunthan V N 			/* Flood All Unicast Packets to Host port */
6470cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
6480cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
6490cd8f9ccSMugunthan V N 		} else {
6506f979eb3SLennart Sorensen 			/* Don't Flood All Unicast Packets to Host port */
6510cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
6520cd8f9ccSMugunthan V N 
6536f979eb3SLennart Sorensen 			/* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
654606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
6550cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6560cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 0);
6570cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6580cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 0);
6590cd8f9ccSMugunthan V N 			}
6600cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
6610cd8f9ccSMugunthan V N 		}
6620cd8f9ccSMugunthan V N 	}
6630cd8f9ccSMugunthan V N }
6640cd8f9ccSMugunthan V N 
6655da19489SIvan Khoronzhuk static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr)
6665da19489SIvan Khoronzhuk {
6675da19489SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
6685da19489SIvan Khoronzhuk 
6695da19489SIvan Khoronzhuk 	cpsw_add_mcast(priv, addr);
6705da19489SIvan Khoronzhuk 	return 0;
6715da19489SIvan Khoronzhuk }
6725da19489SIvan Khoronzhuk 
6735da19489SIvan Khoronzhuk static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr)
6745c50a856SMugunthan V N {
6755c50a856SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
676606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
6775da19489SIvan Khoronzhuk 	int vid, flags;
67825906052SMugunthan V N 
6795da19489SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
680606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
6815da19489SIvan Khoronzhuk 		flags = ALE_VLAN;
6825da19489SIvan Khoronzhuk 	} else {
6835da19489SIvan Khoronzhuk 		vid = 0;
6845da19489SIvan Khoronzhuk 		flags = 0;
6855da19489SIvan Khoronzhuk 	}
6865da19489SIvan Khoronzhuk 
6875da19489SIvan Khoronzhuk 	cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
6885da19489SIvan Khoronzhuk 	return 0;
6895da19489SIvan Khoronzhuk }
6905da19489SIvan Khoronzhuk 
6915da19489SIvan Khoronzhuk static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
6925da19489SIvan Khoronzhuk {
6935da19489SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
6945c50a856SMugunthan V N 
6955c50a856SMugunthan V N 	if (ndev->flags & IFF_PROMISC) {
6965c50a856SMugunthan V N 		/* Enable promiscuous mode */
6970cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, true);
6982a05a622SIvan Khoronzhuk 		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
6995c50a856SMugunthan V N 		return;
7000cd8f9ccSMugunthan V N 	} else {
7010cd8f9ccSMugunthan V N 		/* Disable promiscuous mode */
7020cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, false);
7035c50a856SMugunthan V N 	}
7045c50a856SMugunthan V N 
7051e5c4bc4SLennart Sorensen 	/* Restore allmulti on vlans if necessary */
7065da19489SIvan Khoronzhuk 	cpsw_ale_set_allmulti(cpsw->ale, ndev->flags & IFF_ALLMULTI);
7071e5c4bc4SLennart Sorensen 
7085da19489SIvan Khoronzhuk 	__dev_mc_sync(ndev, cpsw_add_mc_addr, cpsw_del_mc_addr);
7095c50a856SMugunthan V N }
7105c50a856SMugunthan V N 
7112c836bd9SIvan Khoronzhuk static void cpsw_intr_enable(struct cpsw_common *cpsw)
712df828598SMugunthan V N {
713dda5f5feSGrygorii Strashko 	writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
714dda5f5feSGrygorii Strashko 	writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
715df828598SMugunthan V N 
7162c836bd9SIvan Khoronzhuk 	cpdma_ctlr_int_ctrl(cpsw->dma, true);
717df828598SMugunthan V N 	return;
718df828598SMugunthan V N }
719df828598SMugunthan V N 
7202c836bd9SIvan Khoronzhuk static void cpsw_intr_disable(struct cpsw_common *cpsw)
721df828598SMugunthan V N {
722dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->wr_regs->tx_en);
723dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->wr_regs->rx_en);
724df828598SMugunthan V N 
7252c836bd9SIvan Khoronzhuk 	cpdma_ctlr_int_ctrl(cpsw->dma, false);
726df828598SMugunthan V N 	return;
727df828598SMugunthan V N }
728df828598SMugunthan V N 
7291a3b5056SOlof Johansson static void cpsw_tx_handler(void *token, int len, int status)
730df828598SMugunthan V N {
731e05107e6SIvan Khoronzhuk 	struct netdev_queue	*txq;
732df828598SMugunthan V N 	struct sk_buff		*skb = token;
733df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
7342a05a622SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
735df828598SMugunthan V N 
736fae50823SMugunthan V N 	/* Check whether the queue is stopped due to stalled tx dma, if the
737fae50823SMugunthan V N 	 * queue is stopped then start the queue as we have free desc for tx
738fae50823SMugunthan V N 	 */
739e05107e6SIvan Khoronzhuk 	txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
740e05107e6SIvan Khoronzhuk 	if (unlikely(netif_tx_queue_stopped(txq)))
741e05107e6SIvan Khoronzhuk 		netif_tx_wake_queue(txq);
742e05107e6SIvan Khoronzhuk 
7432a05a622SIvan Khoronzhuk 	cpts_tx_timestamp(cpsw->cpts, skb);
7448dc43ddcSTobias Klauser 	ndev->stats.tx_packets++;
7458dc43ddcSTobias Klauser 	ndev->stats.tx_bytes += len;
746df828598SMugunthan V N 	dev_kfree_skb_any(skb);
747df828598SMugunthan V N }
748df828598SMugunthan V N 
749a3a41d2fSGrygorii Strashko static void cpsw_rx_vlan_encap(struct sk_buff *skb)
750a3a41d2fSGrygorii Strashko {
751a3a41d2fSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(skb->dev);
752a3a41d2fSGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
753a3a41d2fSGrygorii Strashko 	u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
754a3a41d2fSGrygorii Strashko 	u16 vtag, vid, prio, pkt_type;
755a3a41d2fSGrygorii Strashko 
756a3a41d2fSGrygorii Strashko 	/* Remove VLAN header encapsulation word */
757a3a41d2fSGrygorii Strashko 	skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
758a3a41d2fSGrygorii Strashko 
759a3a41d2fSGrygorii Strashko 	pkt_type = (rx_vlan_encap_hdr >>
760a3a41d2fSGrygorii Strashko 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
761a3a41d2fSGrygorii Strashko 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
762a3a41d2fSGrygorii Strashko 	/* Ignore unknown & Priority-tagged packets*/
763a3a41d2fSGrygorii Strashko 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
764a3a41d2fSGrygorii Strashko 	    pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
765a3a41d2fSGrygorii Strashko 		return;
766a3a41d2fSGrygorii Strashko 
767a3a41d2fSGrygorii Strashko 	vid = (rx_vlan_encap_hdr >>
768a3a41d2fSGrygorii Strashko 	       CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
769a3a41d2fSGrygorii Strashko 	       VLAN_VID_MASK;
770a3a41d2fSGrygorii Strashko 	/* Ignore vid 0 and pass packet as is */
771a3a41d2fSGrygorii Strashko 	if (!vid)
772a3a41d2fSGrygorii Strashko 		return;
773a3a41d2fSGrygorii Strashko 	/* Ignore default vlans in dual mac mode */
774a3a41d2fSGrygorii Strashko 	if (cpsw->data.dual_emac &&
775a3a41d2fSGrygorii Strashko 	    vid == cpsw->slaves[priv->emac_port].port_vlan)
776a3a41d2fSGrygorii Strashko 		return;
777a3a41d2fSGrygorii Strashko 
778a3a41d2fSGrygorii Strashko 	prio = (rx_vlan_encap_hdr >>
779a3a41d2fSGrygorii Strashko 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
780a3a41d2fSGrygorii Strashko 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
781a3a41d2fSGrygorii Strashko 
782a3a41d2fSGrygorii Strashko 	vtag = (prio << VLAN_PRIO_SHIFT) | vid;
783a3a41d2fSGrygorii Strashko 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
784a3a41d2fSGrygorii Strashko 
785a3a41d2fSGrygorii Strashko 	/* strip vlan tag for VLAN-tagged packet */
786a3a41d2fSGrygorii Strashko 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
787a3a41d2fSGrygorii Strashko 		memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
788a3a41d2fSGrygorii Strashko 		skb_pull(skb, VLAN_HLEN);
789a3a41d2fSGrygorii Strashko 	}
790a3a41d2fSGrygorii Strashko }
791a3a41d2fSGrygorii Strashko 
7921a3b5056SOlof Johansson static void cpsw_rx_handler(void *token, int len, int status)
793df828598SMugunthan V N {
794e05107e6SIvan Khoronzhuk 	struct cpdma_chan	*ch;
795df828598SMugunthan V N 	struct sk_buff		*skb = token;
796b4727e69SSebastian Siewior 	struct sk_buff		*new_skb;
797df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
798fea49f60SIvan Khoronzhuk 	int			ret = 0, port;
7992a05a622SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
800df828598SMugunthan V N 
801fea49f60SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
802fea49f60SIvan Khoronzhuk 		port = CPDMA_RX_SOURCE_PORT(status);
803fea49f60SIvan Khoronzhuk 		if (port) {
804fea49f60SIvan Khoronzhuk 			ndev = cpsw->slaves[--port].ndev;
805fea49f60SIvan Khoronzhuk 			skb->dev = ndev;
806fea49f60SIvan Khoronzhuk 		}
807fea49f60SIvan Khoronzhuk 	}
808d9ba8f9eSMugunthan V N 
80916e5c57dSMugunthan V N 	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
810a0e2c822SMugunthan V N 		/* In dual emac mode check for all interfaces */
811d5bc1613SIvan Khoronzhuk 		if (cpsw->data.dual_emac && cpsw->usage_count &&
812fe734d0aSIvan Khoronzhuk 		    (status >= 0)) {
813a0e2c822SMugunthan V N 			/* The packet received is for the interface which
814a0e2c822SMugunthan V N 			 * is already down and the other interface is up
815dbedd44eSJoe Perches 			 * and running, instead of freeing which results
816a0e2c822SMugunthan V N 			 * in reducing of the number of rx descriptor in
817a0e2c822SMugunthan V N 			 * DMA engine, requeue skb back to cpdma.
818a0e2c822SMugunthan V N 			 */
819a0e2c822SMugunthan V N 			new_skb = skb;
820a0e2c822SMugunthan V N 			goto requeue;
821a0e2c822SMugunthan V N 		}
822a0e2c822SMugunthan V N 
823b4727e69SSebastian Siewior 		/* the interface is going down, skbs are purged */
824df828598SMugunthan V N 		dev_kfree_skb_any(skb);
825df828598SMugunthan V N 		return;
826df828598SMugunthan V N 	}
827b4727e69SSebastian Siewior 
8282a05a622SIvan Khoronzhuk 	new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
829b4727e69SSebastian Siewior 	if (new_skb) {
830e05107e6SIvan Khoronzhuk 		skb_copy_queue_mapping(new_skb, skb);
831df828598SMugunthan V N 		skb_put(skb, len);
832a3a41d2fSGrygorii Strashko 		if (status & CPDMA_RX_VLAN_ENCAP)
833a3a41d2fSGrygorii Strashko 			cpsw_rx_vlan_encap(skb);
8342a05a622SIvan Khoronzhuk 		cpts_rx_timestamp(cpsw->cpts, skb);
835df828598SMugunthan V N 		skb->protocol = eth_type_trans(skb, ndev);
836df828598SMugunthan V N 		netif_receive_skb(skb);
8378dc43ddcSTobias Klauser 		ndev->stats.rx_bytes += len;
8388dc43ddcSTobias Klauser 		ndev->stats.rx_packets++;
839254a49d5SGrygorii Strashko 		kmemleak_not_leak(new_skb);
840b4727e69SSebastian Siewior 	} else {
8418dc43ddcSTobias Klauser 		ndev->stats.rx_dropped++;
842b4727e69SSebastian Siewior 		new_skb = skb;
843df828598SMugunthan V N 	}
844df828598SMugunthan V N 
845a0e2c822SMugunthan V N requeue:
846ce52c744SIvan Khoronzhuk 	if (netif_dormant(ndev)) {
847ce52c744SIvan Khoronzhuk 		dev_kfree_skb_any(new_skb);
848ce52c744SIvan Khoronzhuk 		return;
849ce52c744SIvan Khoronzhuk 	}
850ce52c744SIvan Khoronzhuk 
8518feb0a19SIvan Khoronzhuk 	ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
852e05107e6SIvan Khoronzhuk 	ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
853b4727e69SSebastian Siewior 				skb_tailroom(new_skb), 0);
854b4727e69SSebastian Siewior 	if (WARN_ON(ret < 0))
855b4727e69SSebastian Siewior 		dev_kfree_skb_any(new_skb);
856df828598SMugunthan V N }
857df828598SMugunthan V N 
85832b78d85SIvan Khoronzhuk static void cpsw_split_res(struct net_device *ndev)
85948e0a83eSIvan Khoronzhuk {
86048e0a83eSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
86132b78d85SIvan Khoronzhuk 	u32 consumed_rate = 0, bigest_rate = 0;
86248e0a83eSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
86348e0a83eSIvan Khoronzhuk 	struct cpsw_vector *txv = cpsw->txv;
86432b78d85SIvan Khoronzhuk 	int i, ch_weight, rlim_ch_num = 0;
86548e0a83eSIvan Khoronzhuk 	int budget, bigest_rate_ch = 0;
86648e0a83eSIvan Khoronzhuk 	u32 ch_rate, max_rate;
86748e0a83eSIvan Khoronzhuk 	int ch_budget = 0;
86848e0a83eSIvan Khoronzhuk 
86948e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->tx_ch_num; i++) {
87048e0a83eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
87148e0a83eSIvan Khoronzhuk 		if (!ch_rate)
87248e0a83eSIvan Khoronzhuk 			continue;
87348e0a83eSIvan Khoronzhuk 
87448e0a83eSIvan Khoronzhuk 		rlim_ch_num++;
87548e0a83eSIvan Khoronzhuk 		consumed_rate += ch_rate;
87648e0a83eSIvan Khoronzhuk 	}
87748e0a83eSIvan Khoronzhuk 
87848e0a83eSIvan Khoronzhuk 	if (cpsw->tx_ch_num == rlim_ch_num) {
87948e0a83eSIvan Khoronzhuk 		max_rate = consumed_rate;
88032b78d85SIvan Khoronzhuk 	} else if (!rlim_ch_num) {
88132b78d85SIvan Khoronzhuk 		ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
88232b78d85SIvan Khoronzhuk 		bigest_rate = 0;
88332b78d85SIvan Khoronzhuk 		max_rate = consumed_rate;
88448e0a83eSIvan Khoronzhuk 	} else {
8850be01b8eSIvan Khoronzhuk 		max_rate = cpsw->speed * 1000;
8860be01b8eSIvan Khoronzhuk 
8870be01b8eSIvan Khoronzhuk 		/* if max_rate is less then expected due to reduced link speed,
8880be01b8eSIvan Khoronzhuk 		 * split proportionally according next potential max speed
8890be01b8eSIvan Khoronzhuk 		 */
8900be01b8eSIvan Khoronzhuk 		if (max_rate < consumed_rate)
8910be01b8eSIvan Khoronzhuk 			max_rate *= 10;
8920be01b8eSIvan Khoronzhuk 
8930be01b8eSIvan Khoronzhuk 		if (max_rate < consumed_rate)
8940be01b8eSIvan Khoronzhuk 			max_rate *= 10;
89532b78d85SIvan Khoronzhuk 
89648e0a83eSIvan Khoronzhuk 		ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
89748e0a83eSIvan Khoronzhuk 		ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
89848e0a83eSIvan Khoronzhuk 			    (cpsw->tx_ch_num - rlim_ch_num);
89948e0a83eSIvan Khoronzhuk 		bigest_rate = (max_rate - consumed_rate) /
90048e0a83eSIvan Khoronzhuk 			      (cpsw->tx_ch_num - rlim_ch_num);
90148e0a83eSIvan Khoronzhuk 	}
90248e0a83eSIvan Khoronzhuk 
90332b78d85SIvan Khoronzhuk 	/* split tx weight/budget */
90448e0a83eSIvan Khoronzhuk 	budget = CPSW_POLL_WEIGHT;
90548e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->tx_ch_num; i++) {
90648e0a83eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
90748e0a83eSIvan Khoronzhuk 		if (ch_rate) {
90848e0a83eSIvan Khoronzhuk 			txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
90948e0a83eSIvan Khoronzhuk 			if (!txv[i].budget)
91032b78d85SIvan Khoronzhuk 				txv[i].budget++;
91148e0a83eSIvan Khoronzhuk 			if (ch_rate > bigest_rate) {
91248e0a83eSIvan Khoronzhuk 				bigest_rate_ch = i;
91348e0a83eSIvan Khoronzhuk 				bigest_rate = ch_rate;
91448e0a83eSIvan Khoronzhuk 			}
91532b78d85SIvan Khoronzhuk 
91632b78d85SIvan Khoronzhuk 			ch_weight = (ch_rate * 100) / max_rate;
91732b78d85SIvan Khoronzhuk 			if (!ch_weight)
91832b78d85SIvan Khoronzhuk 				ch_weight++;
91932b78d85SIvan Khoronzhuk 			cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
92048e0a83eSIvan Khoronzhuk 		} else {
92148e0a83eSIvan Khoronzhuk 			txv[i].budget = ch_budget;
92248e0a83eSIvan Khoronzhuk 			if (!bigest_rate_ch)
92348e0a83eSIvan Khoronzhuk 				bigest_rate_ch = i;
92432b78d85SIvan Khoronzhuk 			cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
92548e0a83eSIvan Khoronzhuk 		}
92648e0a83eSIvan Khoronzhuk 
92748e0a83eSIvan Khoronzhuk 		budget -= txv[i].budget;
92848e0a83eSIvan Khoronzhuk 	}
92948e0a83eSIvan Khoronzhuk 
93048e0a83eSIvan Khoronzhuk 	if (budget)
93148e0a83eSIvan Khoronzhuk 		txv[bigest_rate_ch].budget += budget;
93248e0a83eSIvan Khoronzhuk 
93348e0a83eSIvan Khoronzhuk 	/* split rx budget */
93448e0a83eSIvan Khoronzhuk 	budget = CPSW_POLL_WEIGHT;
93548e0a83eSIvan Khoronzhuk 	ch_budget = budget / cpsw->rx_ch_num;
93648e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->rx_ch_num; i++) {
93748e0a83eSIvan Khoronzhuk 		cpsw->rxv[i].budget = ch_budget;
93848e0a83eSIvan Khoronzhuk 		budget -= ch_budget;
93948e0a83eSIvan Khoronzhuk 	}
94048e0a83eSIvan Khoronzhuk 
94148e0a83eSIvan Khoronzhuk 	if (budget)
94248e0a83eSIvan Khoronzhuk 		cpsw->rxv[0].budget += budget;
94348e0a83eSIvan Khoronzhuk }
94448e0a83eSIvan Khoronzhuk 
945c03abd84SFelipe Balbi static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
946df828598SMugunthan V N {
947dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = dev_id;
9487ce67a38SFelipe Balbi 
9495d8d0d4dSIvan Khoronzhuk 	writel(0, &cpsw->wr_regs->tx_en);
9502c836bd9SIvan Khoronzhuk 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
951c03abd84SFelipe Balbi 
952e38b5a3dSIvan Khoronzhuk 	if (cpsw->quirk_irq) {
953e38b5a3dSIvan Khoronzhuk 		disable_irq_nosync(cpsw->irqs_table[1]);
954e38b5a3dSIvan Khoronzhuk 		cpsw->tx_irq_disabled = true;
9557da11600SMugunthan V N 	}
9567da11600SMugunthan V N 
957dbc4ec52SIvan Khoronzhuk 	napi_schedule(&cpsw->napi_tx);
958c03abd84SFelipe Balbi 	return IRQ_HANDLED;
959c03abd84SFelipe Balbi }
960c03abd84SFelipe Balbi 
961c03abd84SFelipe Balbi static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
962c03abd84SFelipe Balbi {
963dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = dev_id;
964c03abd84SFelipe Balbi 
9652c836bd9SIvan Khoronzhuk 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
9665d8d0d4dSIvan Khoronzhuk 	writel(0, &cpsw->wr_regs->rx_en);
967fd51cf19SSebastian Siewior 
968e38b5a3dSIvan Khoronzhuk 	if (cpsw->quirk_irq) {
969e38b5a3dSIvan Khoronzhuk 		disable_irq_nosync(cpsw->irqs_table[0]);
970e38b5a3dSIvan Khoronzhuk 		cpsw->rx_irq_disabled = true;
9717da11600SMugunthan V N 	}
9727da11600SMugunthan V N 
973dbc4ec52SIvan Khoronzhuk 	napi_schedule(&cpsw->napi_rx);
974df828598SMugunthan V N 	return IRQ_HANDLED;
975df828598SMugunthan V N }
976df828598SMugunthan V N 
9779611d6d6SIvan Khoronzhuk static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
978df828598SMugunthan V N {
979e05107e6SIvan Khoronzhuk 	u32			ch_map;
9808feb0a19SIvan Khoronzhuk 	int			num_tx, cur_budget, ch;
981dbc4ec52SIvan Khoronzhuk 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_tx);
9828feb0a19SIvan Khoronzhuk 	struct cpsw_vector	*txv;
98332a7432cSMugunthan V N 
984e05107e6SIvan Khoronzhuk 	/* process every unprocessed channel */
985e05107e6SIvan Khoronzhuk 	ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
98679b3325dSIvan Khoronzhuk 	for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
98779b3325dSIvan Khoronzhuk 		if (!(ch_map & 0x80))
988e05107e6SIvan Khoronzhuk 			continue;
989e05107e6SIvan Khoronzhuk 
9908feb0a19SIvan Khoronzhuk 		txv = &cpsw->txv[ch];
9918feb0a19SIvan Khoronzhuk 		if (unlikely(txv->budget > budget - num_tx))
9928feb0a19SIvan Khoronzhuk 			cur_budget = budget - num_tx;
9938feb0a19SIvan Khoronzhuk 		else
9948feb0a19SIvan Khoronzhuk 			cur_budget = txv->budget;
9958feb0a19SIvan Khoronzhuk 
9968feb0a19SIvan Khoronzhuk 		num_tx += cpdma_chan_process(txv->ch, cur_budget);
997342934a5SIvan Khoronzhuk 		if (num_tx >= budget)
998342934a5SIvan Khoronzhuk 			break;
999e05107e6SIvan Khoronzhuk 	}
1000e05107e6SIvan Khoronzhuk 
100132a7432cSMugunthan V N 	if (num_tx < budget) {
100232a7432cSMugunthan V N 		napi_complete(napi_tx);
10035d8d0d4dSIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->tx_en);
10049611d6d6SIvan Khoronzhuk 	}
10059611d6d6SIvan Khoronzhuk 
10069611d6d6SIvan Khoronzhuk 	return num_tx;
10079611d6d6SIvan Khoronzhuk }
10089611d6d6SIvan Khoronzhuk 
10099611d6d6SIvan Khoronzhuk static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
10109611d6d6SIvan Khoronzhuk {
10119611d6d6SIvan Khoronzhuk 	struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
10129611d6d6SIvan Khoronzhuk 	int num_tx;
10139611d6d6SIvan Khoronzhuk 
10149611d6d6SIvan Khoronzhuk 	num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
10159611d6d6SIvan Khoronzhuk 	if (num_tx < budget) {
10169611d6d6SIvan Khoronzhuk 		napi_complete(napi_tx);
10179611d6d6SIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->tx_en);
10189611d6d6SIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
1019e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
1020e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
10217da11600SMugunthan V N 		}
102232a7432cSMugunthan V N 	}
102332a7432cSMugunthan V N 
102432a7432cSMugunthan V N 	return num_tx;
102532a7432cSMugunthan V N }
102632a7432cSMugunthan V N 
10279611d6d6SIvan Khoronzhuk static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
102832a7432cSMugunthan V N {
1029e05107e6SIvan Khoronzhuk 	u32			ch_map;
10308feb0a19SIvan Khoronzhuk 	int			num_rx, cur_budget, ch;
1031dbc4ec52SIvan Khoronzhuk 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_rx);
10328feb0a19SIvan Khoronzhuk 	struct cpsw_vector	*rxv;
1033510a1e72SMugunthan V N 
1034e05107e6SIvan Khoronzhuk 	/* process every unprocessed channel */
1035e05107e6SIvan Khoronzhuk 	ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
1036342934a5SIvan Khoronzhuk 	for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
1037e05107e6SIvan Khoronzhuk 		if (!(ch_map & 0x01))
1038e05107e6SIvan Khoronzhuk 			continue;
1039e05107e6SIvan Khoronzhuk 
10408feb0a19SIvan Khoronzhuk 		rxv = &cpsw->rxv[ch];
10418feb0a19SIvan Khoronzhuk 		if (unlikely(rxv->budget > budget - num_rx))
10428feb0a19SIvan Khoronzhuk 			cur_budget = budget - num_rx;
10438feb0a19SIvan Khoronzhuk 		else
10448feb0a19SIvan Khoronzhuk 			cur_budget = rxv->budget;
10458feb0a19SIvan Khoronzhuk 
10468feb0a19SIvan Khoronzhuk 		num_rx += cpdma_chan_process(rxv->ch, cur_budget);
1047342934a5SIvan Khoronzhuk 		if (num_rx >= budget)
1048342934a5SIvan Khoronzhuk 			break;
1049e05107e6SIvan Khoronzhuk 	}
1050e05107e6SIvan Khoronzhuk 
1051510a1e72SMugunthan V N 	if (num_rx < budget) {
10526ad20165SEric Dumazet 		napi_complete_done(napi_rx, num_rx);
10535d8d0d4dSIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->rx_en);
10549611d6d6SIvan Khoronzhuk 	}
10559611d6d6SIvan Khoronzhuk 
10569611d6d6SIvan Khoronzhuk 	return num_rx;
10579611d6d6SIvan Khoronzhuk }
10589611d6d6SIvan Khoronzhuk 
10599611d6d6SIvan Khoronzhuk static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
10609611d6d6SIvan Khoronzhuk {
10619611d6d6SIvan Khoronzhuk 	struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
10629611d6d6SIvan Khoronzhuk 	int num_rx;
10639611d6d6SIvan Khoronzhuk 
10649611d6d6SIvan Khoronzhuk 	num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
10659611d6d6SIvan Khoronzhuk 	if (num_rx < budget) {
10669611d6d6SIvan Khoronzhuk 		napi_complete_done(napi_rx, num_rx);
10679611d6d6SIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->rx_en);
10689611d6d6SIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
1069e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
1070e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
10717da11600SMugunthan V N 		}
1072510a1e72SMugunthan V N 	}
1073df828598SMugunthan V N 
1074df828598SMugunthan V N 	return num_rx;
1075df828598SMugunthan V N }
1076df828598SMugunthan V N 
1077df828598SMugunthan V N static inline void soft_reset(const char *module, void __iomem *reg)
1078df828598SMugunthan V N {
1079df828598SMugunthan V N 	unsigned long timeout = jiffies + HZ;
1080df828598SMugunthan V N 
1081dda5f5feSGrygorii Strashko 	writel_relaxed(1, reg);
1082df828598SMugunthan V N 	do {
1083df828598SMugunthan V N 		cpu_relax();
1084dda5f5feSGrygorii Strashko 	} while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
1085df828598SMugunthan V N 
1086dda5f5feSGrygorii Strashko 	WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
1087df828598SMugunthan V N }
1088df828598SMugunthan V N 
1089df828598SMugunthan V N static void cpsw_set_slave_mac(struct cpsw_slave *slave,
1090df828598SMugunthan V N 			       struct cpsw_priv *priv)
1091df828598SMugunthan V N {
10929750a3adSRichard Cochran 	slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
10939750a3adSRichard Cochran 	slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
1094df828598SMugunthan V N }
1095df828598SMugunthan V N 
109657d90148SIvan Khoronzhuk static bool cpsw_shp_is_off(struct cpsw_priv *priv)
109757d90148SIvan Khoronzhuk {
109857d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
109957d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
110057d90148SIvan Khoronzhuk 	u32 shift, mask, val;
110157d90148SIvan Khoronzhuk 
110257d90148SIvan Khoronzhuk 	val = readl_relaxed(&cpsw->regs->ptype);
110357d90148SIvan Khoronzhuk 
110457d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
110557d90148SIvan Khoronzhuk 	shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
110657d90148SIvan Khoronzhuk 	mask = 7 << shift;
110757d90148SIvan Khoronzhuk 	val = val & mask;
110857d90148SIvan Khoronzhuk 
110957d90148SIvan Khoronzhuk 	return !val;
111057d90148SIvan Khoronzhuk }
111157d90148SIvan Khoronzhuk 
111257d90148SIvan Khoronzhuk static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
111357d90148SIvan Khoronzhuk {
111457d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
111557d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
111657d90148SIvan Khoronzhuk 	u32 shift, mask, val;
111757d90148SIvan Khoronzhuk 
111857d90148SIvan Khoronzhuk 	val = readl_relaxed(&cpsw->regs->ptype);
111957d90148SIvan Khoronzhuk 
112057d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
112157d90148SIvan Khoronzhuk 	shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
112257d90148SIvan Khoronzhuk 	mask = (1 << --fifo) << shift;
112357d90148SIvan Khoronzhuk 	val = on ? val | mask : val & ~mask;
112457d90148SIvan Khoronzhuk 
112557d90148SIvan Khoronzhuk 	writel_relaxed(val, &cpsw->regs->ptype);
112657d90148SIvan Khoronzhuk }
112757d90148SIvan Khoronzhuk 
1128df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave,
1129df828598SMugunthan V N 			      struct cpsw_priv *priv, bool *link)
1130df828598SMugunthan V N {
1131df828598SMugunthan V N 	struct phy_device	*phy = slave->phy;
1132df828598SMugunthan V N 	u32			mac_control = 0;
1133df828598SMugunthan V N 	u32			slave_port;
1134606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1135df828598SMugunthan V N 
1136df828598SMugunthan V N 	if (!phy)
1137df828598SMugunthan V N 		return;
1138df828598SMugunthan V N 
11396f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
1140df828598SMugunthan V N 
1141df828598SMugunthan V N 	if (phy->link) {
1142606f3993SIvan Khoronzhuk 		mac_control = cpsw->data.mac_control;
1143df828598SMugunthan V N 
1144df828598SMugunthan V N 		/* enable forwarding */
11452a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
1146df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1147df828598SMugunthan V N 
1148df828598SMugunthan V N 		if (phy->speed == 1000)
1149df828598SMugunthan V N 			mac_control |= BIT(7);	/* GIGABITEN	*/
1150df828598SMugunthan V N 		if (phy->duplex)
1151df828598SMugunthan V N 			mac_control |= BIT(0);	/* FULLDUPLEXEN	*/
1152342b7b74SDaniel Mack 
1153342b7b74SDaniel Mack 		/* set speed_in input in case RMII mode is used in 100Mbps */
1154342b7b74SDaniel Mack 		if (phy->speed == 100)
1155342b7b74SDaniel Mack 			mac_control |= BIT(15);
1156f9db5069SSZ Lin (林上智) 		/* in band mode only works in 10Mbps RGMII mode */
1157f9db5069SSZ Lin (林上智) 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
1158a81d8762SMugunthan V N 			mac_control |= BIT(18); /* In Band mode */
1159342b7b74SDaniel Mack 
11601923d6e4SMugunthan V N 		if (priv->rx_pause)
11611923d6e4SMugunthan V N 			mac_control |= BIT(3);
11621923d6e4SMugunthan V N 
11631923d6e4SMugunthan V N 		if (priv->tx_pause)
11641923d6e4SMugunthan V N 			mac_control |= BIT(4);
11651923d6e4SMugunthan V N 
1166df828598SMugunthan V N 		*link = true;
116757d90148SIvan Khoronzhuk 
116857d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
116957d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed &&
117057d90148SIvan Khoronzhuk 		    !cpsw_shp_is_off(priv))
117157d90148SIvan Khoronzhuk 			dev_warn(priv->dev,
117257d90148SIvan Khoronzhuk 				 "Speed was changed, CBS shaper speeds are changed!");
1173df828598SMugunthan V N 	} else {
1174df828598SMugunthan V N 		mac_control = 0;
1175df828598SMugunthan V N 		/* disable forwarding */
11762a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
1177df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1178df828598SMugunthan V N 	}
1179df828598SMugunthan V N 
1180df828598SMugunthan V N 	if (mac_control != slave->mac_control) {
1181df828598SMugunthan V N 		phy_print_status(phy);
1182dda5f5feSGrygorii Strashko 		writel_relaxed(mac_control, &slave->sliver->mac_control);
1183df828598SMugunthan V N 	}
1184df828598SMugunthan V N 
1185df828598SMugunthan V N 	slave->mac_control = mac_control;
1186df828598SMugunthan V N }
1187df828598SMugunthan V N 
11880be01b8eSIvan Khoronzhuk static int cpsw_get_common_speed(struct cpsw_common *cpsw)
11890be01b8eSIvan Khoronzhuk {
11900be01b8eSIvan Khoronzhuk 	int i, speed;
11910be01b8eSIvan Khoronzhuk 
11920be01b8eSIvan Khoronzhuk 	for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
11930be01b8eSIvan Khoronzhuk 		if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
11940be01b8eSIvan Khoronzhuk 			speed += cpsw->slaves[i].phy->speed;
11950be01b8eSIvan Khoronzhuk 
11960be01b8eSIvan Khoronzhuk 	return speed;
11970be01b8eSIvan Khoronzhuk }
11980be01b8eSIvan Khoronzhuk 
11990be01b8eSIvan Khoronzhuk static int cpsw_need_resplit(struct cpsw_common *cpsw)
12000be01b8eSIvan Khoronzhuk {
12010be01b8eSIvan Khoronzhuk 	int i, rlim_ch_num;
12020be01b8eSIvan Khoronzhuk 	int speed, ch_rate;
12030be01b8eSIvan Khoronzhuk 
12040be01b8eSIvan Khoronzhuk 	/* re-split resources only in case speed was changed */
12050be01b8eSIvan Khoronzhuk 	speed = cpsw_get_common_speed(cpsw);
12060be01b8eSIvan Khoronzhuk 	if (speed == cpsw->speed || !speed)
12070be01b8eSIvan Khoronzhuk 		return 0;
12080be01b8eSIvan Khoronzhuk 
12090be01b8eSIvan Khoronzhuk 	cpsw->speed = speed;
12100be01b8eSIvan Khoronzhuk 
12110be01b8eSIvan Khoronzhuk 	for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
12120be01b8eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
12130be01b8eSIvan Khoronzhuk 		if (!ch_rate)
12140be01b8eSIvan Khoronzhuk 			break;
12150be01b8eSIvan Khoronzhuk 
12160be01b8eSIvan Khoronzhuk 		rlim_ch_num++;
12170be01b8eSIvan Khoronzhuk 	}
12180be01b8eSIvan Khoronzhuk 
12190be01b8eSIvan Khoronzhuk 	/* cases not dependent on speed */
12200be01b8eSIvan Khoronzhuk 	if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
12210be01b8eSIvan Khoronzhuk 		return 0;
12220be01b8eSIvan Khoronzhuk 
12230be01b8eSIvan Khoronzhuk 	return 1;
12240be01b8eSIvan Khoronzhuk }
12250be01b8eSIvan Khoronzhuk 
1226df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev)
1227df828598SMugunthan V N {
1228df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
12290be01b8eSIvan Khoronzhuk 	struct cpsw_common	*cpsw = priv->cpsw;
1230df828598SMugunthan V N 	bool			link = false;
1231df828598SMugunthan V N 
1232df828598SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1233df828598SMugunthan V N 
1234df828598SMugunthan V N 	if (link) {
12350be01b8eSIvan Khoronzhuk 		if (cpsw_need_resplit(cpsw))
12360be01b8eSIvan Khoronzhuk 			cpsw_split_res(ndev);
12370be01b8eSIvan Khoronzhuk 
1238df828598SMugunthan V N 		netif_carrier_on(ndev);
1239df828598SMugunthan V N 		if (netif_running(ndev))
1240e05107e6SIvan Khoronzhuk 			netif_tx_wake_all_queues(ndev);
1241df828598SMugunthan V N 	} else {
1242df828598SMugunthan V N 		netif_carrier_off(ndev);
1243e05107e6SIvan Khoronzhuk 		netif_tx_stop_all_queues(ndev);
1244df828598SMugunthan V N 	}
1245df828598SMugunthan V N }
1246df828598SMugunthan V N 
1247ff5b8ef2SMugunthan V N static int cpsw_get_coalesce(struct net_device *ndev,
1248ff5b8ef2SMugunthan V N 				struct ethtool_coalesce *coal)
1249ff5b8ef2SMugunthan V N {
12502a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1251ff5b8ef2SMugunthan V N 
12522a05a622SIvan Khoronzhuk 	coal->rx_coalesce_usecs = cpsw->coal_intvl;
1253ff5b8ef2SMugunthan V N 	return 0;
1254ff5b8ef2SMugunthan V N }
1255ff5b8ef2SMugunthan V N 
1256ff5b8ef2SMugunthan V N static int cpsw_set_coalesce(struct net_device *ndev,
1257ff5b8ef2SMugunthan V N 				struct ethtool_coalesce *coal)
1258ff5b8ef2SMugunthan V N {
1259ff5b8ef2SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1260ff5b8ef2SMugunthan V N 	u32 int_ctrl;
1261ff5b8ef2SMugunthan V N 	u32 num_interrupts = 0;
1262ff5b8ef2SMugunthan V N 	u32 prescale = 0;
1263ff5b8ef2SMugunthan V N 	u32 addnl_dvdr = 1;
1264ff5b8ef2SMugunthan V N 	u32 coal_intvl = 0;
12655d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1266ff5b8ef2SMugunthan V N 
1267ff5b8ef2SMugunthan V N 	coal_intvl = coal->rx_coalesce_usecs;
1268ff5b8ef2SMugunthan V N 
12695d8d0d4dSIvan Khoronzhuk 	int_ctrl =  readl(&cpsw->wr_regs->int_control);
12702a05a622SIvan Khoronzhuk 	prescale = cpsw->bus_freq_mhz * 4;
1271ff5b8ef2SMugunthan V N 
1272a84bc2a9SMugunthan V N 	if (!coal->rx_coalesce_usecs) {
1273a84bc2a9SMugunthan V N 		int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1274a84bc2a9SMugunthan V N 		goto update_return;
1275a84bc2a9SMugunthan V N 	}
1276a84bc2a9SMugunthan V N 
1277ff5b8ef2SMugunthan V N 	if (coal_intvl < CPSW_CMINTMIN_INTVL)
1278ff5b8ef2SMugunthan V N 		coal_intvl = CPSW_CMINTMIN_INTVL;
1279ff5b8ef2SMugunthan V N 
1280ff5b8ef2SMugunthan V N 	if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1281ff5b8ef2SMugunthan V N 		/* Interrupt pacer works with 4us Pulse, we can
1282ff5b8ef2SMugunthan V N 		 * throttle further by dilating the 4us pulse.
1283ff5b8ef2SMugunthan V N 		 */
1284ff5b8ef2SMugunthan V N 		addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1285ff5b8ef2SMugunthan V N 
1286ff5b8ef2SMugunthan V N 		if (addnl_dvdr > 1) {
1287ff5b8ef2SMugunthan V N 			prescale *= addnl_dvdr;
1288ff5b8ef2SMugunthan V N 			if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1289ff5b8ef2SMugunthan V N 				coal_intvl = (CPSW_CMINTMAX_INTVL
1290ff5b8ef2SMugunthan V N 						* addnl_dvdr);
1291ff5b8ef2SMugunthan V N 		} else {
1292ff5b8ef2SMugunthan V N 			addnl_dvdr = 1;
1293ff5b8ef2SMugunthan V N 			coal_intvl = CPSW_CMINTMAX_INTVL;
1294ff5b8ef2SMugunthan V N 		}
1295ff5b8ef2SMugunthan V N 	}
1296ff5b8ef2SMugunthan V N 
1297ff5b8ef2SMugunthan V N 	num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
12985d8d0d4dSIvan Khoronzhuk 	writel(num_interrupts, &cpsw->wr_regs->rx_imax);
12995d8d0d4dSIvan Khoronzhuk 	writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1300ff5b8ef2SMugunthan V N 
1301ff5b8ef2SMugunthan V N 	int_ctrl |= CPSW_INTPACEEN;
1302ff5b8ef2SMugunthan V N 	int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1303ff5b8ef2SMugunthan V N 	int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1304a84bc2a9SMugunthan V N 
1305a84bc2a9SMugunthan V N update_return:
13065d8d0d4dSIvan Khoronzhuk 	writel(int_ctrl, &cpsw->wr_regs->int_control);
1307ff5b8ef2SMugunthan V N 
1308ff5b8ef2SMugunthan V N 	cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
13092a05a622SIvan Khoronzhuk 	cpsw->coal_intvl = coal_intvl;
1310ff5b8ef2SMugunthan V N 
1311ff5b8ef2SMugunthan V N 	return 0;
1312ff5b8ef2SMugunthan V N }
1313ff5b8ef2SMugunthan V N 
1314d9718546SMugunthan V N static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1315d9718546SMugunthan V N {
1316e05107e6SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1317e05107e6SIvan Khoronzhuk 
1318d9718546SMugunthan V N 	switch (sset) {
1319d9718546SMugunthan V N 	case ETH_SS_STATS:
1320e05107e6SIvan Khoronzhuk 		return (CPSW_STATS_COMMON_LEN +
1321e05107e6SIvan Khoronzhuk 		       (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1322e05107e6SIvan Khoronzhuk 		       CPSW_STATS_CH_LEN);
1323d9718546SMugunthan V N 	default:
1324d9718546SMugunthan V N 		return -EOPNOTSUPP;
1325d9718546SMugunthan V N 	}
1326d9718546SMugunthan V N }
1327d9718546SMugunthan V N 
1328e05107e6SIvan Khoronzhuk static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1329e05107e6SIvan Khoronzhuk {
1330e05107e6SIvan Khoronzhuk 	int ch_stats_len;
1331e05107e6SIvan Khoronzhuk 	int line;
1332e05107e6SIvan Khoronzhuk 	int i;
1333e05107e6SIvan Khoronzhuk 
1334e05107e6SIvan Khoronzhuk 	ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1335e05107e6SIvan Khoronzhuk 	for (i = 0; i < ch_stats_len; i++) {
1336e05107e6SIvan Khoronzhuk 		line = i % CPSW_STATS_CH_LEN;
1337e05107e6SIvan Khoronzhuk 		snprintf(*p, ETH_GSTRING_LEN,
1338bf2ce3fdSFlorian Fainelli 			 "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
1339bf2ce3fdSFlorian Fainelli 			 (long)(i / CPSW_STATS_CH_LEN),
1340e05107e6SIvan Khoronzhuk 			 cpsw_gstrings_ch_stats[line].stat_string);
1341e05107e6SIvan Khoronzhuk 		*p += ETH_GSTRING_LEN;
1342e05107e6SIvan Khoronzhuk 	}
1343e05107e6SIvan Khoronzhuk }
1344e05107e6SIvan Khoronzhuk 
1345d9718546SMugunthan V N static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1346d9718546SMugunthan V N {
1347e05107e6SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1348d9718546SMugunthan V N 	u8 *p = data;
1349d9718546SMugunthan V N 	int i;
1350d9718546SMugunthan V N 
1351d9718546SMugunthan V N 	switch (stringset) {
1352d9718546SMugunthan V N 	case ETH_SS_STATS:
1353e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1354d9718546SMugunthan V N 			memcpy(p, cpsw_gstrings_stats[i].stat_string,
1355d9718546SMugunthan V N 			       ETH_GSTRING_LEN);
1356d9718546SMugunthan V N 			p += ETH_GSTRING_LEN;
1357d9718546SMugunthan V N 		}
1358e05107e6SIvan Khoronzhuk 
1359e05107e6SIvan Khoronzhuk 		cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1360e05107e6SIvan Khoronzhuk 		cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1361d9718546SMugunthan V N 		break;
1362d9718546SMugunthan V N 	}
1363d9718546SMugunthan V N }
1364d9718546SMugunthan V N 
1365d9718546SMugunthan V N static void cpsw_get_ethtool_stats(struct net_device *ndev,
1366d9718546SMugunthan V N 				    struct ethtool_stats *stats, u64 *data)
1367d9718546SMugunthan V N {
1368d9718546SMugunthan V N 	u8 *p;
13692c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1370e05107e6SIvan Khoronzhuk 	struct cpdma_chan_stats ch_stats;
1371e05107e6SIvan Khoronzhuk 	int i, l, ch;
1372d9718546SMugunthan V N 
1373d9718546SMugunthan V N 	/* Collect Davinci CPDMA stats for Rx and Tx Channel */
1374e05107e6SIvan Khoronzhuk 	for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1375e05107e6SIvan Khoronzhuk 		data[l] = readl(cpsw->hw_stats +
1376e05107e6SIvan Khoronzhuk 				cpsw_gstrings_stats[l].stat_offset);
1377d9718546SMugunthan V N 
1378e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
13798feb0a19SIvan Khoronzhuk 		cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
1380e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1381e05107e6SIvan Khoronzhuk 			p = (u8 *)&ch_stats +
1382e05107e6SIvan Khoronzhuk 				cpsw_gstrings_ch_stats[i].stat_offset;
1383e05107e6SIvan Khoronzhuk 			data[l] = *(u32 *)p;
1384e05107e6SIvan Khoronzhuk 		}
1385e05107e6SIvan Khoronzhuk 	}
1386d9718546SMugunthan V N 
1387e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
13888feb0a19SIvan Khoronzhuk 		cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
1389e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1390e05107e6SIvan Khoronzhuk 			p = (u8 *)&ch_stats +
1391e05107e6SIvan Khoronzhuk 				cpsw_gstrings_ch_stats[i].stat_offset;
1392e05107e6SIvan Khoronzhuk 			data[l] = *(u32 *)p;
1393d9718546SMugunthan V N 		}
1394d9718546SMugunthan V N 	}
1395d9718546SMugunthan V N }
1396d9718546SMugunthan V N 
139727e9e103SIvan Khoronzhuk static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1398e05107e6SIvan Khoronzhuk 					struct sk_buff *skb,
1399e05107e6SIvan Khoronzhuk 					struct cpdma_chan *txch)
1400d9ba8f9eSMugunthan V N {
14012c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
14022c836bd9SIvan Khoronzhuk 
140398fdd857SIvan Khoronzhuk 	skb_tx_timestamp(skb);
1404e05107e6SIvan Khoronzhuk 	return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1405606f3993SIvan Khoronzhuk 				 priv->emac_port + cpsw->data.dual_emac);
1406d9ba8f9eSMugunthan V N }
1407d9ba8f9eSMugunthan V N 
1408d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries(
1409d9ba8f9eSMugunthan V N 		struct cpsw_priv *priv, struct cpsw_slave *slave,
1410d9ba8f9eSMugunthan V N 		u32 slave_port)
1411d9ba8f9eSMugunthan V N {
14122a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
141371a2cbb7SGrygorii Strashko 	u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1414d9ba8f9eSMugunthan V N 
14152a05a622SIvan Khoronzhuk 	if (cpsw->version == CPSW_VERSION_1)
1416d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1417d9ba8f9eSMugunthan V N 	else
1418d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
14192a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1420d9ba8f9eSMugunthan V N 			  port_mask, port_mask, 0);
14212a05a622SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1422d9ba8f9eSMugunthan V N 			   port_mask, ALE_VLAN, slave->port_vlan, 0);
14232a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
14242a05a622SIvan Khoronzhuk 			   HOST_PORT_NUM, ALE_VLAN |
14252a05a622SIvan Khoronzhuk 			   ALE_SECURE, slave->port_vlan);
14265e5add17SGrygorii Strashko 	cpsw_ale_control_set(cpsw->ale, slave_port,
14275e5add17SGrygorii Strashko 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1428d9ba8f9eSMugunthan V N }
1429d9ba8f9eSMugunthan V N 
14301e7a2e21SDaniel Mack static void soft_reset_slave(struct cpsw_slave *slave)
1431df828598SMugunthan V N {
1432df828598SMugunthan V N 	char name[32];
14331e7a2e21SDaniel Mack 
14341e7a2e21SDaniel Mack 	snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
14351e7a2e21SDaniel Mack 	soft_reset(name, &slave->sliver->soft_reset);
14361e7a2e21SDaniel Mack }
14371e7a2e21SDaniel Mack 
14381e7a2e21SDaniel Mack static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
14391e7a2e21SDaniel Mack {
1440df828598SMugunthan V N 	u32 slave_port;
144130c57f07SSekhar Nori 	struct phy_device *phy;
1442649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1443df828598SMugunthan V N 
14441e7a2e21SDaniel Mack 	soft_reset_slave(slave);
1445df828598SMugunthan V N 
1446df828598SMugunthan V N 	/* setup priority mapping */
1447dda5f5feSGrygorii Strashko 	writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
14489750a3adSRichard Cochran 
14492a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
14509750a3adSRichard Cochran 	case CPSW_VERSION_1:
14519750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
145248f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
145348f5bcccSGrygorii Strashko 		 * flow control mode
145448f5bcccSGrygorii Strashko 		 */
145548f5bcccSGrygorii Strashko 		slave_write(slave,
145648f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
145748f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
14589750a3adSRichard Cochran 		break;
14599750a3adSRichard Cochran 	case CPSW_VERSION_2:
1460c193f365SMugunthan V N 	case CPSW_VERSION_3:
1461926489beSMugunthan V N 	case CPSW_VERSION_4:
14629750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
146348f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
146448f5bcccSGrygorii Strashko 		 * flow control mode
146548f5bcccSGrygorii Strashko 		 */
146648f5bcccSGrygorii Strashko 		slave_write(slave,
146748f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
146848f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
14699750a3adSRichard Cochran 		break;
14709750a3adSRichard Cochran 	}
1471df828598SMugunthan V N 
1472df828598SMugunthan V N 	/* setup max packet size, and mac address */
1473dda5f5feSGrygorii Strashko 	writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1474df828598SMugunthan V N 	cpsw_set_slave_mac(slave, priv);
1475df828598SMugunthan V N 
1476df828598SMugunthan V N 	slave->mac_control = 0;	/* no link yet */
1477df828598SMugunthan V N 
14786f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
1479df828598SMugunthan V N 
1480606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
1481d9ba8f9eSMugunthan V N 		cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1482d9ba8f9eSMugunthan V N 	else
14832a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1484e11b220fSMugunthan V N 				   1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1485df828598SMugunthan V N 
1486d733f754SDavid Rivshin 	if (slave->data->phy_node) {
148730c57f07SSekhar Nori 		phy = of_phy_connect(priv->ndev, slave->data->phy_node,
14889e42f715SHeiko Schocher 				 &cpsw_adjust_link, 0, slave->data->phy_if);
148930c57f07SSekhar Nori 		if (!phy) {
1490f7ce9103SRob Herring 			dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1491f7ce9103SRob Herring 				slave->data->phy_node,
1492d733f754SDavid Rivshin 				slave->slave_num);
1493d733f754SDavid Rivshin 			return;
1494d733f754SDavid Rivshin 		}
1495d733f754SDavid Rivshin 	} else {
149630c57f07SSekhar Nori 		phy = phy_connect(priv->ndev, slave->data->phy_id,
1497f9a8f83bSFlorian Fainelli 				 &cpsw_adjust_link, slave->data->phy_if);
149830c57f07SSekhar Nori 		if (IS_ERR(phy)) {
1499d733f754SDavid Rivshin 			dev_err(priv->dev,
1500d733f754SDavid Rivshin 				"phy \"%s\" not found on slave %d, err %ld\n",
1501d733f754SDavid Rivshin 				slave->data->phy_id, slave->slave_num,
150230c57f07SSekhar Nori 				PTR_ERR(phy));
1503d733f754SDavid Rivshin 			return;
1504d733f754SDavid Rivshin 		}
1505d733f754SDavid Rivshin 	}
1506d733f754SDavid Rivshin 
150730c57f07SSekhar Nori 	slave->phy = phy;
150830c57f07SSekhar Nori 
15092220943aSAndrew Lunn 	phy_attached_info(slave->phy);
15102220943aSAndrew Lunn 
1511df828598SMugunthan V N 	phy_start(slave->phy);
1512388367a5SMugunthan V N 
1513388367a5SMugunthan V N 	/* Configure GMII_SEL register */
151456e31bd8SIvan Khoronzhuk 	cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
1515df828598SMugunthan V N }
1516df828598SMugunthan V N 
15173b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
15183b72c2feSMugunthan V N {
1519606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1520606f3993SIvan Khoronzhuk 	const int vlan = cpsw->data.default_vlan;
15213b72c2feSMugunthan V N 	u32 reg;
15223b72c2feSMugunthan V N 	int i;
15231e5c4bc4SLennart Sorensen 	int unreg_mcast_mask;
15243b72c2feSMugunthan V N 
15252a05a622SIvan Khoronzhuk 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
15263b72c2feSMugunthan V N 	       CPSW2_PORT_VLAN;
15273b72c2feSMugunthan V N 
15285d8d0d4dSIvan Khoronzhuk 	writel(vlan, &cpsw->host_port_regs->port_vlan);
15293b72c2feSMugunthan V N 
1530606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
1531606f3993SIvan Khoronzhuk 		slave_write(cpsw->slaves + i, vlan, reg);
15323b72c2feSMugunthan V N 
15331e5c4bc4SLennart Sorensen 	if (priv->ndev->flags & IFF_ALLMULTI)
15341e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_ALL_PORTS;
15351e5c4bc4SLennart Sorensen 	else
15361e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
15371e5c4bc4SLennart Sorensen 
15382a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
153961f1cef9SGrygorii Strashko 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
154061f1cef9SGrygorii Strashko 			  unreg_mcast_mask);
15413b72c2feSMugunthan V N }
15423b72c2feSMugunthan V N 
1543df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv)
1544df828598SMugunthan V N {
1545d9ba8f9eSMugunthan V N 	u32 fifo_mode;
15465d8d0d4dSIvan Khoronzhuk 	u32 control_reg;
15475d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
15483b72c2feSMugunthan V N 
1549df828598SMugunthan V N 	/* soft reset the controller and initialize ale */
15505d8d0d4dSIvan Khoronzhuk 	soft_reset("cpsw", &cpsw->regs->soft_reset);
15512a05a622SIvan Khoronzhuk 	cpsw_ale_start(cpsw->ale);
1552df828598SMugunthan V N 
1553df828598SMugunthan V N 	/* switch to vlan unaware mode */
15542a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
15553b72c2feSMugunthan V N 			     CPSW_ALE_VLAN_AWARE);
15565d8d0d4dSIvan Khoronzhuk 	control_reg = readl(&cpsw->regs->control);
1557a3a41d2fSGrygorii Strashko 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
15585d8d0d4dSIvan Khoronzhuk 	writel(control_reg, &cpsw->regs->control);
1559606f3993SIvan Khoronzhuk 	fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1560d9ba8f9eSMugunthan V N 		     CPSW_FIFO_NORMAL_MODE;
15615d8d0d4dSIvan Khoronzhuk 	writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1562df828598SMugunthan V N 
1563df828598SMugunthan V N 	/* setup host port priority mapping */
1564dda5f5feSGrygorii Strashko 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
15655d8d0d4dSIvan Khoronzhuk 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
1566dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1567df828598SMugunthan V N 
15682a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1569df828598SMugunthan V N 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1570df828598SMugunthan V N 
1571606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac) {
15722a05a622SIvan Khoronzhuk 		cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1573d9ba8f9eSMugunthan V N 				   0, 0);
15742a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
157571a2cbb7SGrygorii Strashko 				   ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1576df828598SMugunthan V N 	}
1577d9ba8f9eSMugunthan V N }
1578df828598SMugunthan V N 
15793802dce1SIvan Khoronzhuk static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
15803802dce1SIvan Khoronzhuk {
15813802dce1SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
15823802dce1SIvan Khoronzhuk 	struct sk_buff *skb;
15833802dce1SIvan Khoronzhuk 	int ch_buf_num;
1584e05107e6SIvan Khoronzhuk 	int ch, i, ret;
15853802dce1SIvan Khoronzhuk 
1586e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
15878feb0a19SIvan Khoronzhuk 		ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
15883802dce1SIvan Khoronzhuk 		for (i = 0; i < ch_buf_num; i++) {
15893802dce1SIvan Khoronzhuk 			skb = __netdev_alloc_skb_ip_align(priv->ndev,
15903802dce1SIvan Khoronzhuk 							  cpsw->rx_packet_max,
15913802dce1SIvan Khoronzhuk 							  GFP_KERNEL);
15923802dce1SIvan Khoronzhuk 			if (!skb) {
15933802dce1SIvan Khoronzhuk 				cpsw_err(priv, ifup, "cannot allocate skb\n");
15943802dce1SIvan Khoronzhuk 				return -ENOMEM;
15953802dce1SIvan Khoronzhuk 			}
15963802dce1SIvan Khoronzhuk 
1597e05107e6SIvan Khoronzhuk 			skb_set_queue_mapping(skb, ch);
15988feb0a19SIvan Khoronzhuk 			ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
15998feb0a19SIvan Khoronzhuk 						skb->data, skb_tailroom(skb),
16008feb0a19SIvan Khoronzhuk 						0);
16013802dce1SIvan Khoronzhuk 			if (ret < 0) {
16023802dce1SIvan Khoronzhuk 				cpsw_err(priv, ifup,
1603e05107e6SIvan Khoronzhuk 					 "cannot submit skb to channel %d rx, error %d\n",
1604e05107e6SIvan Khoronzhuk 					 ch, ret);
16053802dce1SIvan Khoronzhuk 				kfree_skb(skb);
16063802dce1SIvan Khoronzhuk 				return ret;
16073802dce1SIvan Khoronzhuk 			}
16083802dce1SIvan Khoronzhuk 			kmemleak_not_leak(skb);
16093802dce1SIvan Khoronzhuk 		}
16103802dce1SIvan Khoronzhuk 
1611e05107e6SIvan Khoronzhuk 		cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1612e05107e6SIvan Khoronzhuk 			  ch, ch_buf_num);
1613e05107e6SIvan Khoronzhuk 	}
16143802dce1SIvan Khoronzhuk 
1615e05107e6SIvan Khoronzhuk 	return 0;
16163802dce1SIvan Khoronzhuk }
16173802dce1SIvan Khoronzhuk 
16182a05a622SIvan Khoronzhuk static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1619aacebbf8SSebastian Siewior {
16203995d265SSchuyler Patton 	u32 slave_port;
16213995d265SSchuyler Patton 
16226f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
16233995d265SSchuyler Patton 
1624aacebbf8SSebastian Siewior 	if (!slave->phy)
1625aacebbf8SSebastian Siewior 		return;
1626aacebbf8SSebastian Siewior 	phy_stop(slave->phy);
1627aacebbf8SSebastian Siewior 	phy_disconnect(slave->phy);
1628aacebbf8SSebastian Siewior 	slave->phy = NULL;
16292a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, slave_port,
16303995d265SSchuyler Patton 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
16311f95ba00SGrygorii Strashko 	soft_reset_slave(slave);
1632aacebbf8SSebastian Siewior }
1633aacebbf8SSebastian Siewior 
16347929a668SIvan Khoronzhuk static int cpsw_tc_to_fifo(int tc, int num_tc)
16357929a668SIvan Khoronzhuk {
16367929a668SIvan Khoronzhuk 	if (tc == num_tc - 1)
16377929a668SIvan Khoronzhuk 		return 0;
16387929a668SIvan Khoronzhuk 
16397929a668SIvan Khoronzhuk 	return CPSW_FIFO_SHAPERS_NUM - tc;
16407929a668SIvan Khoronzhuk }
16417929a668SIvan Khoronzhuk 
164257d90148SIvan Khoronzhuk static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
164357d90148SIvan Khoronzhuk {
164457d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
164557d90148SIvan Khoronzhuk 	u32 val = 0, send_pct, shift;
164657d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
164757d90148SIvan Khoronzhuk 	int pct = 0, i;
164857d90148SIvan Khoronzhuk 
164957d90148SIvan Khoronzhuk 	if (bw > priv->shp_cfg_speed * 1000)
165057d90148SIvan Khoronzhuk 		goto err;
165157d90148SIvan Khoronzhuk 
165257d90148SIvan Khoronzhuk 	/* shaping has to stay enabled for highest fifos linearly
165357d90148SIvan Khoronzhuk 	 * and fifo bw no more then interface can allow
165457d90148SIvan Khoronzhuk 	 */
165557d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
165657d90148SIvan Khoronzhuk 	send_pct = slave_read(slave, SEND_PERCENT);
165757d90148SIvan Khoronzhuk 	for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
165857d90148SIvan Khoronzhuk 		if (!bw) {
165957d90148SIvan Khoronzhuk 			if (i >= fifo || !priv->fifo_bw[i])
166057d90148SIvan Khoronzhuk 				continue;
166157d90148SIvan Khoronzhuk 
166257d90148SIvan Khoronzhuk 			dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
166357d90148SIvan Khoronzhuk 			continue;
166457d90148SIvan Khoronzhuk 		}
166557d90148SIvan Khoronzhuk 
166657d90148SIvan Khoronzhuk 		if (!priv->fifo_bw[i] && i > fifo) {
166757d90148SIvan Khoronzhuk 			dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
166857d90148SIvan Khoronzhuk 			return -EINVAL;
166957d90148SIvan Khoronzhuk 		}
167057d90148SIvan Khoronzhuk 
167157d90148SIvan Khoronzhuk 		shift = (i - 1) * 8;
167257d90148SIvan Khoronzhuk 		if (i == fifo) {
167357d90148SIvan Khoronzhuk 			send_pct &= ~(CPSW_PCT_MASK << shift);
167457d90148SIvan Khoronzhuk 			val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
167557d90148SIvan Khoronzhuk 			if (!val)
167657d90148SIvan Khoronzhuk 				val = 1;
167757d90148SIvan Khoronzhuk 
167857d90148SIvan Khoronzhuk 			send_pct |= val << shift;
167957d90148SIvan Khoronzhuk 			pct += val;
168057d90148SIvan Khoronzhuk 			continue;
168157d90148SIvan Khoronzhuk 		}
168257d90148SIvan Khoronzhuk 
168357d90148SIvan Khoronzhuk 		if (priv->fifo_bw[i])
168457d90148SIvan Khoronzhuk 			pct += (send_pct >> shift) & CPSW_PCT_MASK;
168557d90148SIvan Khoronzhuk 	}
168657d90148SIvan Khoronzhuk 
168757d90148SIvan Khoronzhuk 	if (pct >= 100)
168857d90148SIvan Khoronzhuk 		goto err;
168957d90148SIvan Khoronzhuk 
169057d90148SIvan Khoronzhuk 	slave_write(slave, send_pct, SEND_PERCENT);
169157d90148SIvan Khoronzhuk 	priv->fifo_bw[fifo] = bw;
169257d90148SIvan Khoronzhuk 
169357d90148SIvan Khoronzhuk 	dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
169457d90148SIvan Khoronzhuk 		 DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
169557d90148SIvan Khoronzhuk 
169657d90148SIvan Khoronzhuk 	return 0;
169757d90148SIvan Khoronzhuk err:
169857d90148SIvan Khoronzhuk 	dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
169957d90148SIvan Khoronzhuk 	return -EINVAL;
170057d90148SIvan Khoronzhuk }
170157d90148SIvan Khoronzhuk 
170257d90148SIvan Khoronzhuk static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
170357d90148SIvan Khoronzhuk {
170457d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
170557d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
170657d90148SIvan Khoronzhuk 	u32 tx_in_ctl_rg, val;
170757d90148SIvan Khoronzhuk 	int ret;
170857d90148SIvan Khoronzhuk 
170957d90148SIvan Khoronzhuk 	ret = cpsw_set_fifo_bw(priv, fifo, bw);
171057d90148SIvan Khoronzhuk 	if (ret)
171157d90148SIvan Khoronzhuk 		return ret;
171257d90148SIvan Khoronzhuk 
171357d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
171457d90148SIvan Khoronzhuk 	tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
171557d90148SIvan Khoronzhuk 		       CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
171657d90148SIvan Khoronzhuk 
171757d90148SIvan Khoronzhuk 	if (!bw)
171857d90148SIvan Khoronzhuk 		cpsw_fifo_shp_on(priv, fifo, bw);
171957d90148SIvan Khoronzhuk 
172057d90148SIvan Khoronzhuk 	val = slave_read(slave, tx_in_ctl_rg);
172157d90148SIvan Khoronzhuk 	if (cpsw_shp_is_off(priv)) {
172257d90148SIvan Khoronzhuk 		/* disable FIFOs rate limited queues */
172357d90148SIvan Khoronzhuk 		val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
172457d90148SIvan Khoronzhuk 
172557d90148SIvan Khoronzhuk 		/* set type of FIFO queues to normal priority mode */
172657d90148SIvan Khoronzhuk 		val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
172757d90148SIvan Khoronzhuk 
172857d90148SIvan Khoronzhuk 		/* set type of FIFO queues to be rate limited */
172957d90148SIvan Khoronzhuk 		if (bw)
173057d90148SIvan Khoronzhuk 			val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
173157d90148SIvan Khoronzhuk 		else
173257d90148SIvan Khoronzhuk 			priv->shp_cfg_speed = 0;
173357d90148SIvan Khoronzhuk 	}
173457d90148SIvan Khoronzhuk 
173557d90148SIvan Khoronzhuk 	/* toggle a FIFO rate limited queue */
173657d90148SIvan Khoronzhuk 	if (bw)
173757d90148SIvan Khoronzhuk 		val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
173857d90148SIvan Khoronzhuk 	else
173957d90148SIvan Khoronzhuk 		val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
174057d90148SIvan Khoronzhuk 	slave_write(slave, val, tx_in_ctl_rg);
174157d90148SIvan Khoronzhuk 
174257d90148SIvan Khoronzhuk 	/* FIFO transmit shape enable */
174357d90148SIvan Khoronzhuk 	cpsw_fifo_shp_on(priv, fifo, bw);
174457d90148SIvan Khoronzhuk 	return 0;
174557d90148SIvan Khoronzhuk }
174657d90148SIvan Khoronzhuk 
174757d90148SIvan Khoronzhuk /* Defaults:
174857d90148SIvan Khoronzhuk  * class A - prio 3
174957d90148SIvan Khoronzhuk  * class B - prio 2
175057d90148SIvan Khoronzhuk  * shaping for class A should be set first
175157d90148SIvan Khoronzhuk  */
175257d90148SIvan Khoronzhuk static int cpsw_set_cbs(struct net_device *ndev,
175357d90148SIvan Khoronzhuk 			struct tc_cbs_qopt_offload *qopt)
175457d90148SIvan Khoronzhuk {
175557d90148SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
175657d90148SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
175757d90148SIvan Khoronzhuk 	struct cpsw_slave *slave;
175857d90148SIvan Khoronzhuk 	int prev_speed = 0;
175957d90148SIvan Khoronzhuk 	int tc, ret, fifo;
176057d90148SIvan Khoronzhuk 	u32 bw = 0;
176157d90148SIvan Khoronzhuk 
176257d90148SIvan Khoronzhuk 	tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
176357d90148SIvan Khoronzhuk 
176457d90148SIvan Khoronzhuk 	/* enable channels in backward order, as highest FIFOs must be rate
176557d90148SIvan Khoronzhuk 	 * limited first and for compliance with CPDMA rate limited channels
176657d90148SIvan Khoronzhuk 	 * that also used in bacward order. FIFO0 cannot be rate limited.
176757d90148SIvan Khoronzhuk 	 */
176857d90148SIvan Khoronzhuk 	fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
176957d90148SIvan Khoronzhuk 	if (!fifo) {
177057d90148SIvan Khoronzhuk 		dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
177157d90148SIvan Khoronzhuk 		return -EINVAL;
177257d90148SIvan Khoronzhuk 	}
177357d90148SIvan Khoronzhuk 
177457d90148SIvan Khoronzhuk 	/* do nothing, it's disabled anyway */
177557d90148SIvan Khoronzhuk 	if (!qopt->enable && !priv->fifo_bw[fifo])
177657d90148SIvan Khoronzhuk 		return 0;
177757d90148SIvan Khoronzhuk 
177857d90148SIvan Khoronzhuk 	/* shapers can be set if link speed is known */
177957d90148SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
178057d90148SIvan Khoronzhuk 	if (slave->phy && slave->phy->link) {
178157d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
178257d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed)
178357d90148SIvan Khoronzhuk 			prev_speed = priv->shp_cfg_speed;
178457d90148SIvan Khoronzhuk 
178557d90148SIvan Khoronzhuk 		priv->shp_cfg_speed = slave->phy->speed;
178657d90148SIvan Khoronzhuk 	}
178757d90148SIvan Khoronzhuk 
178857d90148SIvan Khoronzhuk 	if (!priv->shp_cfg_speed) {
178957d90148SIvan Khoronzhuk 		dev_err(priv->dev, "Link speed is not known");
179057d90148SIvan Khoronzhuk 		return -1;
179157d90148SIvan Khoronzhuk 	}
179257d90148SIvan Khoronzhuk 
179357d90148SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
179457d90148SIvan Khoronzhuk 	if (ret < 0) {
179557d90148SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
179657d90148SIvan Khoronzhuk 		return ret;
179757d90148SIvan Khoronzhuk 	}
179857d90148SIvan Khoronzhuk 
179957d90148SIvan Khoronzhuk 	bw = qopt->enable ? qopt->idleslope : 0;
180057d90148SIvan Khoronzhuk 	ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
180157d90148SIvan Khoronzhuk 	if (ret) {
180257d90148SIvan Khoronzhuk 		priv->shp_cfg_speed = prev_speed;
180357d90148SIvan Khoronzhuk 		prev_speed = 0;
180457d90148SIvan Khoronzhuk 	}
180557d90148SIvan Khoronzhuk 
180657d90148SIvan Khoronzhuk 	if (bw && prev_speed)
180757d90148SIvan Khoronzhuk 		dev_warn(priv->dev,
180857d90148SIvan Khoronzhuk 			 "Speed was changed, CBS shaper speeds are changed!");
180957d90148SIvan Khoronzhuk 
181057d90148SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
181157d90148SIvan Khoronzhuk 	return ret;
181257d90148SIvan Khoronzhuk }
181357d90148SIvan Khoronzhuk 
18144b4255edSIvan Khoronzhuk static void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
18154b4255edSIvan Khoronzhuk {
18164b4255edSIvan Khoronzhuk 	int fifo, bw;
18174b4255edSIvan Khoronzhuk 
18184b4255edSIvan Khoronzhuk 	for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
18194b4255edSIvan Khoronzhuk 		bw = priv->fifo_bw[fifo];
18204b4255edSIvan Khoronzhuk 		if (!bw)
18214b4255edSIvan Khoronzhuk 			continue;
18224b4255edSIvan Khoronzhuk 
18234b4255edSIvan Khoronzhuk 		cpsw_set_fifo_rlimit(priv, fifo, bw);
18244b4255edSIvan Khoronzhuk 	}
18254b4255edSIvan Khoronzhuk }
18264b4255edSIvan Khoronzhuk 
18274b4255edSIvan Khoronzhuk static void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
18284b4255edSIvan Khoronzhuk {
18294b4255edSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
18304b4255edSIvan Khoronzhuk 	u32 tx_prio_map = 0;
18314b4255edSIvan Khoronzhuk 	int i, tc, fifo;
18324b4255edSIvan Khoronzhuk 	u32 tx_prio_rg;
18334b4255edSIvan Khoronzhuk 
18344b4255edSIvan Khoronzhuk 	if (!priv->mqprio_hw)
18354b4255edSIvan Khoronzhuk 		return;
18364b4255edSIvan Khoronzhuk 
18374b4255edSIvan Khoronzhuk 	for (i = 0; i < 8; i++) {
18384b4255edSIvan Khoronzhuk 		tc = netdev_get_prio_tc_map(priv->ndev, i);
18394b4255edSIvan Khoronzhuk 		fifo = CPSW_FIFO_SHAPERS_NUM - tc;
18404b4255edSIvan Khoronzhuk 		tx_prio_map |= fifo << (4 * i);
18414b4255edSIvan Khoronzhuk 	}
18424b4255edSIvan Khoronzhuk 
18434b4255edSIvan Khoronzhuk 	tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
18444b4255edSIvan Khoronzhuk 		     CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
18454b4255edSIvan Khoronzhuk 
18464b4255edSIvan Khoronzhuk 	slave_write(slave, tx_prio_map, tx_prio_rg);
18474b4255edSIvan Khoronzhuk }
18484b4255edSIvan Khoronzhuk 
18494b4255edSIvan Khoronzhuk /* restore resources after port reset */
18504b4255edSIvan Khoronzhuk static void cpsw_restore(struct cpsw_priv *priv)
18514b4255edSIvan Khoronzhuk {
18524b4255edSIvan Khoronzhuk 	/* restore MQPRIO offload */
18534b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_mqprio_resume, priv);
18544b4255edSIvan Khoronzhuk 
18554b4255edSIvan Khoronzhuk 	/* restore CBS offload */
18564b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_cbs_resume, priv);
18574b4255edSIvan Khoronzhuk }
18584b4255edSIvan Khoronzhuk 
1859df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev)
1860df828598SMugunthan V N {
1861df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1862649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
18633802dce1SIvan Khoronzhuk 	int ret;
1864df828598SMugunthan V N 	u32 reg;
1865df828598SMugunthan V N 
186656e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1867108a6537SGrygorii Strashko 	if (ret < 0) {
186856e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1869108a6537SGrygorii Strashko 		return ret;
1870108a6537SGrygorii Strashko 	}
18713fa88c51SGrygorii Strashko 
1872df828598SMugunthan V N 	netif_carrier_off(ndev);
1873df828598SMugunthan V N 
1874e05107e6SIvan Khoronzhuk 	/* Notify the stack of the actual queue counts. */
1875e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1876e05107e6SIvan Khoronzhuk 	if (ret) {
1877e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of tx queues\n");
1878e05107e6SIvan Khoronzhuk 		goto err_cleanup;
1879e05107e6SIvan Khoronzhuk 	}
1880e05107e6SIvan Khoronzhuk 
1881e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1882e05107e6SIvan Khoronzhuk 	if (ret) {
1883e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of rx queues\n");
1884e05107e6SIvan Khoronzhuk 		goto err_cleanup;
1885e05107e6SIvan Khoronzhuk 	}
1886e05107e6SIvan Khoronzhuk 
18872a05a622SIvan Khoronzhuk 	reg = cpsw->version;
1888df828598SMugunthan V N 
1889df828598SMugunthan V N 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1890df828598SMugunthan V N 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1891df828598SMugunthan V N 		 CPSW_RTL_VERSION(reg));
1892df828598SMugunthan V N 
1893d5bc1613SIvan Khoronzhuk 	/* Initialize host and slave ports */
1894d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count)
1895df828598SMugunthan V N 		cpsw_init_host_port(priv);
1896df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_open, priv);
1897df828598SMugunthan V N 
18983b72c2feSMugunthan V N 	/* Add default VLAN */
1899606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac)
19003b72c2feSMugunthan V N 		cpsw_add_default_vlan(priv);
1901e6afea0bSMugunthan V N 	else
19022a05a622SIvan Khoronzhuk 		cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
190361f1cef9SGrygorii Strashko 				  ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
19043b72c2feSMugunthan V N 
1905d5bc1613SIvan Khoronzhuk 	/* initialize shared resources for every ndev */
1906d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count) {
1907d9ba8f9eSMugunthan V N 		/* disable priority elevation */
1908dda5f5feSGrygorii Strashko 		writel_relaxed(0, &cpsw->regs->ptype);
1909df828598SMugunthan V N 
1910d9ba8f9eSMugunthan V N 		/* enable statistics collection only on all ports */
1911dda5f5feSGrygorii Strashko 		writel_relaxed(0x7, &cpsw->regs->stat_port_en);
1912df828598SMugunthan V N 
19131923d6e4SMugunthan V N 		/* Enable internal fifo flow control */
19145d8d0d4dSIvan Khoronzhuk 		writel(0x7, &cpsw->regs->flow_control);
19151923d6e4SMugunthan V N 
1916dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_rx);
1917dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_tx);
1918d354eb85SMugunthan V N 
1919e38b5a3dSIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
1920e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
1921e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
19227da11600SMugunthan V N 		}
19237da11600SMugunthan V N 
1924e38b5a3dSIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
1925e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
1926e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
19277da11600SMugunthan V N 		}
19287da11600SMugunthan V N 
19293802dce1SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
19303802dce1SIvan Khoronzhuk 		if (ret < 0)
1931aacebbf8SSebastian Siewior 			goto err_cleanup;
1932f280e89aSMugunthan V N 
19338a2c9a5aSGrygorii Strashko 		if (cpts_register(cpsw->cpts))
1934f280e89aSMugunthan V N 			dev_err(priv->dev, "error registering cpts device\n");
1935f280e89aSMugunthan V N 
1936d9ba8f9eSMugunthan V N 	}
1937df828598SMugunthan V N 
19384b4255edSIvan Khoronzhuk 	cpsw_restore(priv);
19394b4255edSIvan Khoronzhuk 
1940ff5b8ef2SMugunthan V N 	/* Enable Interrupt pacing if configured */
19412a05a622SIvan Khoronzhuk 	if (cpsw->coal_intvl != 0) {
1942ff5b8ef2SMugunthan V N 		struct ethtool_coalesce coal;
1943ff5b8ef2SMugunthan V N 
19442a05a622SIvan Khoronzhuk 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
1945ff5b8ef2SMugunthan V N 		cpsw_set_coalesce(ndev, &coal);
1946ff5b8ef2SMugunthan V N 	}
1947ff5b8ef2SMugunthan V N 
19482c836bd9SIvan Khoronzhuk 	cpdma_ctlr_start(cpsw->dma);
19492c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
1950d5bc1613SIvan Khoronzhuk 	cpsw->usage_count++;
1951f63a975eSMugunthan V N 
1952df828598SMugunthan V N 	return 0;
1953df828598SMugunthan V N 
1954aacebbf8SSebastian Siewior err_cleanup:
19552c836bd9SIvan Khoronzhuk 	cpdma_ctlr_stop(cpsw->dma);
19562a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
195756e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
1958aacebbf8SSebastian Siewior 	netif_carrier_off(priv->ndev);
1959aacebbf8SSebastian Siewior 	return ret;
1960df828598SMugunthan V N }
1961df828598SMugunthan V N 
1962df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev)
1963df828598SMugunthan V N {
1964df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1965649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1966df828598SMugunthan V N 
1967df828598SMugunthan V N 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
19685da19489SIvan Khoronzhuk 	__dev_mc_unsync(priv->ndev, cpsw_del_mc_addr);
1969e05107e6SIvan Khoronzhuk 	netif_tx_stop_all_queues(priv->ndev);
1970df828598SMugunthan V N 	netif_carrier_off(priv->ndev);
1971d9ba8f9eSMugunthan V N 
1972d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count <= 1) {
1973dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_rx);
1974dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_tx);
19752a05a622SIvan Khoronzhuk 		cpts_unregister(cpsw->cpts);
19762c836bd9SIvan Khoronzhuk 		cpsw_intr_disable(cpsw);
19772c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
19782a05a622SIvan Khoronzhuk 		cpsw_ale_stop(cpsw->ale);
1979d9ba8f9eSMugunthan V N 	}
19802a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
19810be01b8eSIvan Khoronzhuk 
19820be01b8eSIvan Khoronzhuk 	if (cpsw_need_resplit(cpsw))
19830be01b8eSIvan Khoronzhuk 		cpsw_split_res(ndev);
19840be01b8eSIvan Khoronzhuk 
1985d5bc1613SIvan Khoronzhuk 	cpsw->usage_count--;
198656e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
1987df828598SMugunthan V N 	return 0;
1988df828598SMugunthan V N }
1989df828598SMugunthan V N 
1990df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1991df828598SMugunthan V N 				       struct net_device *ndev)
1992df828598SMugunthan V N {
1993df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
19942c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1995f44f8417SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
1996e05107e6SIvan Khoronzhuk 	struct netdev_queue *txq;
1997e05107e6SIvan Khoronzhuk 	struct cpdma_chan *txch;
1998e05107e6SIvan Khoronzhuk 	int ret, q_idx;
1999df828598SMugunthan V N 
2000df828598SMugunthan V N 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
2001df828598SMugunthan V N 		cpsw_err(priv, tx_err, "packet pad failed\n");
20028dc43ddcSTobias Klauser 		ndev->stats.tx_dropped++;
20031bf96050SIvan Khoronzhuk 		return NET_XMIT_DROP;
2004df828598SMugunthan V N 	}
2005df828598SMugunthan V N 
20069232b16dSMugunthan V N 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
2007f44f8417SIvan Khoronzhuk 	    cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
20082e5b38abSRichard Cochran 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
20092e5b38abSRichard Cochran 
2010e05107e6SIvan Khoronzhuk 	q_idx = skb_get_queue_mapping(skb);
2011e05107e6SIvan Khoronzhuk 	if (q_idx >= cpsw->tx_ch_num)
2012e05107e6SIvan Khoronzhuk 		q_idx = q_idx % cpsw->tx_ch_num;
2013e05107e6SIvan Khoronzhuk 
20148feb0a19SIvan Khoronzhuk 	txch = cpsw->txv[q_idx].ch;
201562f94c21SGrygorii Strashko 	txq = netdev_get_tx_queue(ndev, q_idx);
2016e05107e6SIvan Khoronzhuk 	ret = cpsw_tx_packet_submit(priv, skb, txch);
2017df828598SMugunthan V N 	if (unlikely(ret != 0)) {
2018df828598SMugunthan V N 		cpsw_err(priv, tx_err, "desc submit failed\n");
2019df828598SMugunthan V N 		goto fail;
2020df828598SMugunthan V N 	}
2021df828598SMugunthan V N 
2022fae50823SMugunthan V N 	/* If there is no more tx desc left free then we need to
2023fae50823SMugunthan V N 	 * tell the kernel to stop sending us tx frames.
2024fae50823SMugunthan V N 	 */
2025e05107e6SIvan Khoronzhuk 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
2026e05107e6SIvan Khoronzhuk 		netif_tx_stop_queue(txq);
202762f94c21SGrygorii Strashko 
202862f94c21SGrygorii Strashko 		/* Barrier, so that stop_queue visible to other cpus */
202962f94c21SGrygorii Strashko 		smp_mb__after_atomic();
203062f94c21SGrygorii Strashko 
203162f94c21SGrygorii Strashko 		if (cpdma_check_free_tx_desc(txch))
203262f94c21SGrygorii Strashko 			netif_tx_wake_queue(txq);
2033e05107e6SIvan Khoronzhuk 	}
2034fae50823SMugunthan V N 
2035df828598SMugunthan V N 	return NETDEV_TX_OK;
2036df828598SMugunthan V N fail:
20378dc43ddcSTobias Klauser 	ndev->stats.tx_dropped++;
2038e05107e6SIvan Khoronzhuk 	netif_tx_stop_queue(txq);
203962f94c21SGrygorii Strashko 
204062f94c21SGrygorii Strashko 	/* Barrier, so that stop_queue visible to other cpus */
204162f94c21SGrygorii Strashko 	smp_mb__after_atomic();
204262f94c21SGrygorii Strashko 
204362f94c21SGrygorii Strashko 	if (cpdma_check_free_tx_desc(txch))
204462f94c21SGrygorii Strashko 		netif_tx_wake_queue(txq);
204562f94c21SGrygorii Strashko 
2046df828598SMugunthan V N 	return NETDEV_TX_BUSY;
2047df828598SMugunthan V N }
2048df828598SMugunthan V N 
2049c8395d4eSGrygorii Strashko #if IS_ENABLED(CONFIG_TI_CPTS)
20502e5b38abSRichard Cochran 
20512a05a622SIvan Khoronzhuk static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
20522e5b38abSRichard Cochran {
2053606f3993SIvan Khoronzhuk 	struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
20542e5b38abSRichard Cochran 	u32 ts_en, seq_id;
20552e5b38abSRichard Cochran 
2056b63ba58eSGrygorii Strashko 	if (!cpts_is_tx_enabled(cpsw->cpts) &&
2057b63ba58eSGrygorii Strashko 	    !cpts_is_rx_enabled(cpsw->cpts)) {
20582e5b38abSRichard Cochran 		slave_write(slave, 0, CPSW1_TS_CTL);
20592e5b38abSRichard Cochran 		return;
20602e5b38abSRichard Cochran 	}
20612e5b38abSRichard Cochran 
20622e5b38abSRichard Cochran 	seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
20632e5b38abSRichard Cochran 	ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
20642e5b38abSRichard Cochran 
2065b63ba58eSGrygorii Strashko 	if (cpts_is_tx_enabled(cpsw->cpts))
20662e5b38abSRichard Cochran 		ts_en |= CPSW_V1_TS_TX_EN;
20672e5b38abSRichard Cochran 
2068b63ba58eSGrygorii Strashko 	if (cpts_is_rx_enabled(cpsw->cpts))
20692e5b38abSRichard Cochran 		ts_en |= CPSW_V1_TS_RX_EN;
20702e5b38abSRichard Cochran 
20712e5b38abSRichard Cochran 	slave_write(slave, ts_en, CPSW1_TS_CTL);
20722e5b38abSRichard Cochran 	slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
20732e5b38abSRichard Cochran }
20742e5b38abSRichard Cochran 
20752e5b38abSRichard Cochran static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
20762e5b38abSRichard Cochran {
2077d9ba8f9eSMugunthan V N 	struct cpsw_slave *slave;
20785d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
20792e5b38abSRichard Cochran 	u32 ctrl, mtype;
20802e5b38abSRichard Cochran 
2081cb7d78d0SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
2082d9ba8f9eSMugunthan V N 
20832e5b38abSRichard Cochran 	ctrl = slave_read(slave, CPSW2_CONTROL);
20842a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
208509c55372SGeorge Cherian 	case CPSW_VERSION_2:
208609c55372SGeorge Cherian 		ctrl &= ~CTRL_V2_ALL_TS_MASK;
20872e5b38abSRichard Cochran 
2088b63ba58eSGrygorii Strashko 		if (cpts_is_tx_enabled(cpsw->cpts))
208909c55372SGeorge Cherian 			ctrl |= CTRL_V2_TX_TS_BITS;
20902e5b38abSRichard Cochran 
2091b63ba58eSGrygorii Strashko 		if (cpts_is_rx_enabled(cpsw->cpts))
209209c55372SGeorge Cherian 			ctrl |= CTRL_V2_RX_TS_BITS;
209309c55372SGeorge Cherian 		break;
209409c55372SGeorge Cherian 	case CPSW_VERSION_3:
209509c55372SGeorge Cherian 	default:
209609c55372SGeorge Cherian 		ctrl &= ~CTRL_V3_ALL_TS_MASK;
209709c55372SGeorge Cherian 
2098b63ba58eSGrygorii Strashko 		if (cpts_is_tx_enabled(cpsw->cpts))
209909c55372SGeorge Cherian 			ctrl |= CTRL_V3_TX_TS_BITS;
210009c55372SGeorge Cherian 
2101b63ba58eSGrygorii Strashko 		if (cpts_is_rx_enabled(cpsw->cpts))
210209c55372SGeorge Cherian 			ctrl |= CTRL_V3_RX_TS_BITS;
210309c55372SGeorge Cherian 		break;
210409c55372SGeorge Cherian 	}
21052e5b38abSRichard Cochran 
21062e5b38abSRichard Cochran 	mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
21072e5b38abSRichard Cochran 
21082e5b38abSRichard Cochran 	slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
21092e5b38abSRichard Cochran 	slave_write(slave, ctrl, CPSW2_CONTROL);
2110dda5f5feSGrygorii Strashko 	writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
21112e5b38abSRichard Cochran }
21122e5b38abSRichard Cochran 
2113a5b4145bSBen Hutchings static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
21142e5b38abSRichard Cochran {
21153177bf6fSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(dev);
21162e5b38abSRichard Cochran 	struct hwtstamp_config cfg;
21172a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
21182a05a622SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
21192e5b38abSRichard Cochran 
21202a05a622SIvan Khoronzhuk 	if (cpsw->version != CPSW_VERSION_1 &&
21212a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_2 &&
21222a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_3)
21232ee91e54SBen Hutchings 		return -EOPNOTSUPP;
21242ee91e54SBen Hutchings 
21252e5b38abSRichard Cochran 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
21262e5b38abSRichard Cochran 		return -EFAULT;
21272e5b38abSRichard Cochran 
21282e5b38abSRichard Cochran 	/* reserved for future extensions */
21292e5b38abSRichard Cochran 	if (cfg.flags)
21302e5b38abSRichard Cochran 		return -EINVAL;
21312e5b38abSRichard Cochran 
21322ee91e54SBen Hutchings 	if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
21332e5b38abSRichard Cochran 		return -ERANGE;
21342e5b38abSRichard Cochran 
21352e5b38abSRichard Cochran 	switch (cfg.rx_filter) {
21362e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_NONE:
2137b63ba58eSGrygorii Strashko 		cpts_rx_enable(cpts, 0);
21382e5b38abSRichard Cochran 		break;
21392e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_ALL:
2140e9523a5aSGrygorii Strashko 	case HWTSTAMP_FILTER_NTP_ALL:
2141e9523a5aSGrygorii Strashko 		return -ERANGE;
21422e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
21432e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
21442e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2145e9523a5aSGrygorii Strashko 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
2146e9523a5aSGrygorii Strashko 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
2147e9523a5aSGrygorii Strashko 		break;
21482e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
21492e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
21502e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
21512e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
21522e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
21532e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
21542e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
21552e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
21562e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2157e9523a5aSGrygorii Strashko 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
21582e5b38abSRichard Cochran 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
21592e5b38abSRichard Cochran 		break;
21602e5b38abSRichard Cochran 	default:
21612e5b38abSRichard Cochran 		return -ERANGE;
21622e5b38abSRichard Cochran 	}
21632e5b38abSRichard Cochran 
2164b63ba58eSGrygorii Strashko 	cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
21652ee91e54SBen Hutchings 
21662a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
21672e5b38abSRichard Cochran 	case CPSW_VERSION_1:
21682a05a622SIvan Khoronzhuk 		cpsw_hwtstamp_v1(cpsw);
21692e5b38abSRichard Cochran 		break;
21702e5b38abSRichard Cochran 	case CPSW_VERSION_2:
2171f7d403cbSGeorge Cherian 	case CPSW_VERSION_3:
21722e5b38abSRichard Cochran 		cpsw_hwtstamp_v2(priv);
21732e5b38abSRichard Cochran 		break;
21742e5b38abSRichard Cochran 	default:
21752ee91e54SBen Hutchings 		WARN_ON(1);
21762e5b38abSRichard Cochran 	}
21772e5b38abSRichard Cochran 
21782e5b38abSRichard Cochran 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
21792e5b38abSRichard Cochran }
21802e5b38abSRichard Cochran 
2181a5b4145bSBen Hutchings static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2182a5b4145bSBen Hutchings {
21832a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(dev);
21842a05a622SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
2185a5b4145bSBen Hutchings 	struct hwtstamp_config cfg;
2186a5b4145bSBen Hutchings 
21872a05a622SIvan Khoronzhuk 	if (cpsw->version != CPSW_VERSION_1 &&
21882a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_2 &&
21892a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_3)
2190a5b4145bSBen Hutchings 		return -EOPNOTSUPP;
2191a5b4145bSBen Hutchings 
2192a5b4145bSBen Hutchings 	cfg.flags = 0;
2193b63ba58eSGrygorii Strashko 	cfg.tx_type = cpts_is_tx_enabled(cpts) ?
2194b63ba58eSGrygorii Strashko 		      HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
2195b63ba58eSGrygorii Strashko 	cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
2196e9523a5aSGrygorii Strashko 			 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
2197a5b4145bSBen Hutchings 
2198a5b4145bSBen Hutchings 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
2199a5b4145bSBen Hutchings }
2200c8395d4eSGrygorii Strashko #else
2201c8395d4eSGrygorii Strashko static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2202c8395d4eSGrygorii Strashko {
2203c8395d4eSGrygorii Strashko 	return -EOPNOTSUPP;
2204c8395d4eSGrygorii Strashko }
2205a5b4145bSBen Hutchings 
2206c8395d4eSGrygorii Strashko static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2207c8395d4eSGrygorii Strashko {
2208c8395d4eSGrygorii Strashko 	return -EOPNOTSUPP;
2209c8395d4eSGrygorii Strashko }
22102e5b38abSRichard Cochran #endif /*CONFIG_TI_CPTS*/
22112e5b38abSRichard Cochran 
22122e5b38abSRichard Cochran static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
22132e5b38abSRichard Cochran {
221411f2c988SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(dev);
2215606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2216606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
221711f2c988SMugunthan V N 
22182e5b38abSRichard Cochran 	if (!netif_running(dev))
22192e5b38abSRichard Cochran 		return -EINVAL;
22202e5b38abSRichard Cochran 
222111f2c988SMugunthan V N 	switch (cmd) {
222211f2c988SMugunthan V N 	case SIOCSHWTSTAMP:
2223a5b4145bSBen Hutchings 		return cpsw_hwtstamp_set(dev, req);
2224a5b4145bSBen Hutchings 	case SIOCGHWTSTAMP:
2225a5b4145bSBen Hutchings 		return cpsw_hwtstamp_get(dev, req);
22262e5b38abSRichard Cochran 	}
22272e5b38abSRichard Cochran 
2228606f3993SIvan Khoronzhuk 	if (!cpsw->slaves[slave_no].phy)
2229c1b59947SStefan Sørensen 		return -EOPNOTSUPP;
2230606f3993SIvan Khoronzhuk 	return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
223111f2c988SMugunthan V N }
223211f2c988SMugunthan V N 
2233df828598SMugunthan V N static void cpsw_ndo_tx_timeout(struct net_device *ndev)
2234df828598SMugunthan V N {
2235df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
22362c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2237e05107e6SIvan Khoronzhuk 	int ch;
2238df828598SMugunthan V N 
2239df828598SMugunthan V N 	cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
22408dc43ddcSTobias Klauser 	ndev->stats.tx_errors++;
22412c836bd9SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2242e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
22438feb0a19SIvan Khoronzhuk 		cpdma_chan_stop(cpsw->txv[ch].ch);
22448feb0a19SIvan Khoronzhuk 		cpdma_chan_start(cpsw->txv[ch].ch);
2245e05107e6SIvan Khoronzhuk 	}
2246e05107e6SIvan Khoronzhuk 
22472c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
224875514b66SGrygorii Strashko 	netif_trans_update(ndev);
224975514b66SGrygorii Strashko 	netif_tx_wake_all_queues(ndev);
2250df828598SMugunthan V N }
2251df828598SMugunthan V N 
2252dcfd8d58SMugunthan V N static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
2253dcfd8d58SMugunthan V N {
2254dcfd8d58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2255dcfd8d58SMugunthan V N 	struct sockaddr *addr = (struct sockaddr *)p;
2256649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2257dcfd8d58SMugunthan V N 	int flags = 0;
2258dcfd8d58SMugunthan V N 	u16 vid = 0;
2259a6c5d14fSGrygorii Strashko 	int ret;
2260dcfd8d58SMugunthan V N 
2261dcfd8d58SMugunthan V N 	if (!is_valid_ether_addr(addr->sa_data))
2262dcfd8d58SMugunthan V N 		return -EADDRNOTAVAIL;
2263dcfd8d58SMugunthan V N 
226456e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2265a6c5d14fSGrygorii Strashko 	if (ret < 0) {
226656e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2267a6c5d14fSGrygorii Strashko 		return ret;
2268a6c5d14fSGrygorii Strashko 	}
2269a6c5d14fSGrygorii Strashko 
2270606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
2271606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
2272dcfd8d58SMugunthan V N 		flags = ALE_VLAN;
2273dcfd8d58SMugunthan V N 	}
2274dcfd8d58SMugunthan V N 
22752a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
2276dcfd8d58SMugunthan V N 			   flags, vid);
22772a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
2278dcfd8d58SMugunthan V N 			   flags, vid);
2279dcfd8d58SMugunthan V N 
2280dcfd8d58SMugunthan V N 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
2281dcfd8d58SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2282dcfd8d58SMugunthan V N 	for_each_slave(priv, cpsw_set_slave_mac, priv);
2283dcfd8d58SMugunthan V N 
228456e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2285a6c5d14fSGrygorii Strashko 
2286dcfd8d58SMugunthan V N 	return 0;
2287dcfd8d58SMugunthan V N }
2288dcfd8d58SMugunthan V N 
2289df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
2290df828598SMugunthan V N static void cpsw_ndo_poll_controller(struct net_device *ndev)
2291df828598SMugunthan V N {
2292dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2293df828598SMugunthan V N 
2294dbc4ec52SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2295dbc4ec52SIvan Khoronzhuk 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
2296dbc4ec52SIvan Khoronzhuk 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
2297dbc4ec52SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
2298df828598SMugunthan V N }
2299df828598SMugunthan V N #endif
2300df828598SMugunthan V N 
23013b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
23023b72c2feSMugunthan V N 				unsigned short vid)
23033b72c2feSMugunthan V N {
23043b72c2feSMugunthan V N 	int ret;
23059f6bd8faSMugunthan V N 	int unreg_mcast_mask = 0;
23069f6bd8faSMugunthan V N 	u32 port_mask;
2307606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
23089f6bd8faSMugunthan V N 
2309606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
23109f6bd8faSMugunthan V N 		port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
23119f6bd8faSMugunthan V N 
23129f6bd8faSMugunthan V N 		if (priv->ndev->flags & IFF_ALLMULTI)
23139f6bd8faSMugunthan V N 			unreg_mcast_mask = port_mask;
23149f6bd8faSMugunthan V N 	} else {
23159f6bd8faSMugunthan V N 		port_mask = ALE_ALL_PORTS;
23161e5c4bc4SLennart Sorensen 
23171e5c4bc4SLennart Sorensen 		if (priv->ndev->flags & IFF_ALLMULTI)
23181e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_ALL_PORTS;
23191e5c4bc4SLennart Sorensen 		else
23201e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
23219f6bd8faSMugunthan V N 	}
23223b72c2feSMugunthan V N 
23232a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
232461f1cef9SGrygorii Strashko 				unreg_mcast_mask);
23253b72c2feSMugunthan V N 	if (ret != 0)
23263b72c2feSMugunthan V N 		return ret;
23273b72c2feSMugunthan V N 
23282a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
232971a2cbb7SGrygorii Strashko 				 HOST_PORT_NUM, ALE_VLAN, vid);
23303b72c2feSMugunthan V N 	if (ret != 0)
23313b72c2feSMugunthan V N 		goto clean_vid;
23323b72c2feSMugunthan V N 
23332a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
23349f6bd8faSMugunthan V N 				 port_mask, ALE_VLAN, vid, 0);
23353b72c2feSMugunthan V N 	if (ret != 0)
23363b72c2feSMugunthan V N 		goto clean_vlan_ucast;
23373b72c2feSMugunthan V N 	return 0;
23383b72c2feSMugunthan V N 
23393b72c2feSMugunthan V N clean_vlan_ucast:
23402a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
234171a2cbb7SGrygorii Strashko 			   HOST_PORT_NUM, ALE_VLAN, vid);
23423b72c2feSMugunthan V N clean_vid:
23432a05a622SIvan Khoronzhuk 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
23443b72c2feSMugunthan V N 	return ret;
23453b72c2feSMugunthan V N }
23463b72c2feSMugunthan V N 
23473b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
234880d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
23493b72c2feSMugunthan V N {
23503b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2351649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2352a6c5d14fSGrygorii Strashko 	int ret;
23533b72c2feSMugunthan V N 
2354606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
23553b72c2feSMugunthan V N 		return 0;
23563b72c2feSMugunthan V N 
235756e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2358a6c5d14fSGrygorii Strashko 	if (ret < 0) {
235956e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2360a6c5d14fSGrygorii Strashko 		return ret;
2361a6c5d14fSGrygorii Strashko 	}
2362a6c5d14fSGrygorii Strashko 
2363606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
236402a54164SMugunthan V N 		/* In dual EMAC, reserved VLAN id should not be used for
236502a54164SMugunthan V N 		 * creating VLAN interfaces as this can break the dual
236602a54164SMugunthan V N 		 * EMAC port separation
236702a54164SMugunthan V N 		 */
236802a54164SMugunthan V N 		int i;
236902a54164SMugunthan V N 
2370606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
2371803c4f64SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan) {
2372803c4f64SIvan Khoronzhuk 				ret = -EINVAL;
2373803c4f64SIvan Khoronzhuk 				goto err;
2374803c4f64SIvan Khoronzhuk 			}
237502a54164SMugunthan V N 		}
237602a54164SMugunthan V N 	}
237702a54164SMugunthan V N 
23783b72c2feSMugunthan V N 	dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
2379a6c5d14fSGrygorii Strashko 	ret = cpsw_add_vlan_ale_entry(priv, vid);
2380803c4f64SIvan Khoronzhuk err:
238156e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2382a6c5d14fSGrygorii Strashko 	return ret;
23833b72c2feSMugunthan V N }
23843b72c2feSMugunthan V N 
23853b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
238680d5c368SPatrick McHardy 				     __be16 proto, u16 vid)
23873b72c2feSMugunthan V N {
23883b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2389649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
23903b72c2feSMugunthan V N 	int ret;
23913b72c2feSMugunthan V N 
2392606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
23933b72c2feSMugunthan V N 		return 0;
23943b72c2feSMugunthan V N 
239556e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2396a6c5d14fSGrygorii Strashko 	if (ret < 0) {
239756e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2398a6c5d14fSGrygorii Strashko 		return ret;
2399a6c5d14fSGrygorii Strashko 	}
2400a6c5d14fSGrygorii Strashko 
2401606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
240202a54164SMugunthan V N 		int i;
240302a54164SMugunthan V N 
2404606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
2405606f3993SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan)
2406803c4f64SIvan Khoronzhuk 				goto err;
240702a54164SMugunthan V N 		}
240802a54164SMugunthan V N 	}
240902a54164SMugunthan V N 
24103b72c2feSMugunthan V N 	dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
24112a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2412be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
241361f1cef9SGrygorii Strashko 				  HOST_PORT_NUM, ALE_VLAN, vid);
2414be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
24153b72c2feSMugunthan V N 				  0, ALE_VLAN, vid);
2416803c4f64SIvan Khoronzhuk err:
241756e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2418a6c5d14fSGrygorii Strashko 	return ret;
24193b72c2feSMugunthan V N }
24203b72c2feSMugunthan V N 
242183fcad0cSIvan Khoronzhuk static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
242283fcad0cSIvan Khoronzhuk {
242383fcad0cSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
242483fcad0cSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
242552986a2fSIvan Khoronzhuk 	struct cpsw_slave *slave;
242632b78d85SIvan Khoronzhuk 	u32 min_rate;
242783fcad0cSIvan Khoronzhuk 	u32 ch_rate;
242852986a2fSIvan Khoronzhuk 	int i, ret;
242983fcad0cSIvan Khoronzhuk 
243083fcad0cSIvan Khoronzhuk 	ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
243183fcad0cSIvan Khoronzhuk 	if (ch_rate == rate)
243283fcad0cSIvan Khoronzhuk 		return 0;
243383fcad0cSIvan Khoronzhuk 
243432b78d85SIvan Khoronzhuk 	ch_rate = rate * 1000;
243583fcad0cSIvan Khoronzhuk 	min_rate = cpdma_chan_get_min_rate(cpsw->dma);
243632b78d85SIvan Khoronzhuk 	if ((ch_rate < min_rate && ch_rate)) {
243732b78d85SIvan Khoronzhuk 		dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
243883fcad0cSIvan Khoronzhuk 			min_rate);
243983fcad0cSIvan Khoronzhuk 		return -EINVAL;
244083fcad0cSIvan Khoronzhuk 	}
244183fcad0cSIvan Khoronzhuk 
24420be01b8eSIvan Khoronzhuk 	if (rate > cpsw->speed) {
244332b78d85SIvan Khoronzhuk 		dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
244432b78d85SIvan Khoronzhuk 		return -EINVAL;
244532b78d85SIvan Khoronzhuk 	}
244632b78d85SIvan Khoronzhuk 
244783fcad0cSIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
244883fcad0cSIvan Khoronzhuk 	if (ret < 0) {
244983fcad0cSIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
245083fcad0cSIvan Khoronzhuk 		return ret;
245183fcad0cSIvan Khoronzhuk 	}
245283fcad0cSIvan Khoronzhuk 
245332b78d85SIvan Khoronzhuk 	ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
245483fcad0cSIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
245532b78d85SIvan Khoronzhuk 
245632b78d85SIvan Khoronzhuk 	if (ret)
245732b78d85SIvan Khoronzhuk 		return ret;
245832b78d85SIvan Khoronzhuk 
245952986a2fSIvan Khoronzhuk 	/* update rates for slaves tx queues */
246052986a2fSIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++) {
246152986a2fSIvan Khoronzhuk 		slave = &cpsw->slaves[i];
246252986a2fSIvan Khoronzhuk 		if (!slave->ndev)
246352986a2fSIvan Khoronzhuk 			continue;
246452986a2fSIvan Khoronzhuk 
246552986a2fSIvan Khoronzhuk 		netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
246652986a2fSIvan Khoronzhuk 	}
246752986a2fSIvan Khoronzhuk 
246832b78d85SIvan Khoronzhuk 	cpsw_split_res(ndev);
246983fcad0cSIvan Khoronzhuk 	return ret;
247083fcad0cSIvan Khoronzhuk }
247183fcad0cSIvan Khoronzhuk 
24727929a668SIvan Khoronzhuk static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
24737929a668SIvan Khoronzhuk {
24747929a668SIvan Khoronzhuk 	struct tc_mqprio_qopt_offload *mqprio = type_data;
24757929a668SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
24767929a668SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
24777929a668SIvan Khoronzhuk 	int fifo, num_tc, count, offset;
24787929a668SIvan Khoronzhuk 	struct cpsw_slave *slave;
24797929a668SIvan Khoronzhuk 	u32 tx_prio_map = 0;
24807929a668SIvan Khoronzhuk 	int i, tc, ret;
24817929a668SIvan Khoronzhuk 
24827929a668SIvan Khoronzhuk 	num_tc = mqprio->qopt.num_tc;
24837929a668SIvan Khoronzhuk 	if (num_tc > CPSW_TC_NUM)
24847929a668SIvan Khoronzhuk 		return -EINVAL;
24857929a668SIvan Khoronzhuk 
24867929a668SIvan Khoronzhuk 	if (mqprio->mode != TC_MQPRIO_MODE_DCB)
24877929a668SIvan Khoronzhuk 		return -EINVAL;
24887929a668SIvan Khoronzhuk 
24897929a668SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
24907929a668SIvan Khoronzhuk 	if (ret < 0) {
24917929a668SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
24927929a668SIvan Khoronzhuk 		return ret;
24937929a668SIvan Khoronzhuk 	}
24947929a668SIvan Khoronzhuk 
24957929a668SIvan Khoronzhuk 	if (num_tc) {
24967929a668SIvan Khoronzhuk 		for (i = 0; i < 8; i++) {
24977929a668SIvan Khoronzhuk 			tc = mqprio->qopt.prio_tc_map[i];
24987929a668SIvan Khoronzhuk 			fifo = cpsw_tc_to_fifo(tc, num_tc);
24997929a668SIvan Khoronzhuk 			tx_prio_map |= fifo << (4 * i);
25007929a668SIvan Khoronzhuk 		}
25017929a668SIvan Khoronzhuk 
25027929a668SIvan Khoronzhuk 		netdev_set_num_tc(ndev, num_tc);
25037929a668SIvan Khoronzhuk 		for (i = 0; i < num_tc; i++) {
25047929a668SIvan Khoronzhuk 			count = mqprio->qopt.count[i];
25057929a668SIvan Khoronzhuk 			offset = mqprio->qopt.offset[i];
25067929a668SIvan Khoronzhuk 			netdev_set_tc_queue(ndev, i, count, offset);
25077929a668SIvan Khoronzhuk 		}
25087929a668SIvan Khoronzhuk 	}
25097929a668SIvan Khoronzhuk 
25107929a668SIvan Khoronzhuk 	if (!mqprio->qopt.hw) {
25117929a668SIvan Khoronzhuk 		/* restore default configuration */
25127929a668SIvan Khoronzhuk 		netdev_reset_tc(ndev);
25137929a668SIvan Khoronzhuk 		tx_prio_map = TX_PRIORITY_MAPPING;
25147929a668SIvan Khoronzhuk 	}
25157929a668SIvan Khoronzhuk 
25167929a668SIvan Khoronzhuk 	priv->mqprio_hw = mqprio->qopt.hw;
25177929a668SIvan Khoronzhuk 
25187929a668SIvan Khoronzhuk 	offset = cpsw->version == CPSW_VERSION_1 ?
25197929a668SIvan Khoronzhuk 		 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
25207929a668SIvan Khoronzhuk 
25217929a668SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
25227929a668SIvan Khoronzhuk 	slave_write(slave, tx_prio_map, offset);
25237929a668SIvan Khoronzhuk 
25247929a668SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
25257929a668SIvan Khoronzhuk 
25267929a668SIvan Khoronzhuk 	return 0;
25277929a668SIvan Khoronzhuk }
25287929a668SIvan Khoronzhuk 
25297929a668SIvan Khoronzhuk static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
25307929a668SIvan Khoronzhuk 			     void *type_data)
25317929a668SIvan Khoronzhuk {
25327929a668SIvan Khoronzhuk 	switch (type) {
253357d90148SIvan Khoronzhuk 	case TC_SETUP_QDISC_CBS:
253457d90148SIvan Khoronzhuk 		return cpsw_set_cbs(ndev, type_data);
253557d90148SIvan Khoronzhuk 
25367929a668SIvan Khoronzhuk 	case TC_SETUP_QDISC_MQPRIO:
25377929a668SIvan Khoronzhuk 		return cpsw_set_mqprio(ndev, type_data);
25387929a668SIvan Khoronzhuk 
25397929a668SIvan Khoronzhuk 	default:
25407929a668SIvan Khoronzhuk 		return -EOPNOTSUPP;
25417929a668SIvan Khoronzhuk 	}
25427929a668SIvan Khoronzhuk }
25437929a668SIvan Khoronzhuk 
2544df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = {
2545df828598SMugunthan V N 	.ndo_open		= cpsw_ndo_open,
2546df828598SMugunthan V N 	.ndo_stop		= cpsw_ndo_stop,
2547df828598SMugunthan V N 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
2548dcfd8d58SMugunthan V N 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
25492e5b38abSRichard Cochran 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
2550df828598SMugunthan V N 	.ndo_validate_addr	= eth_validate_addr,
2551df828598SMugunthan V N 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
25525c50a856SMugunthan V N 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
255383fcad0cSIvan Khoronzhuk 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
2554df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
2555df828598SMugunthan V N 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
2556df828598SMugunthan V N #endif
25573b72c2feSMugunthan V N 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
25583b72c2feSMugunthan V N 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
25597929a668SIvan Khoronzhuk 	.ndo_setup_tc           = cpsw_ndo_setup_tc,
2560df828598SMugunthan V N };
2561df828598SMugunthan V N 
256252c4f0ecSMugunthan V N static int cpsw_get_regs_len(struct net_device *ndev)
256352c4f0ecSMugunthan V N {
2564606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
256552c4f0ecSMugunthan V N 
2566606f3993SIvan Khoronzhuk 	return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
256752c4f0ecSMugunthan V N }
256852c4f0ecSMugunthan V N 
256952c4f0ecSMugunthan V N static void cpsw_get_regs(struct net_device *ndev,
257052c4f0ecSMugunthan V N 			  struct ethtool_regs *regs, void *p)
257152c4f0ecSMugunthan V N {
257252c4f0ecSMugunthan V N 	u32 *reg = p;
25732a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
257452c4f0ecSMugunthan V N 
257552c4f0ecSMugunthan V N 	/* update CPSW IP version */
25762a05a622SIvan Khoronzhuk 	regs->version = cpsw->version;
257752c4f0ecSMugunthan V N 
25782a05a622SIvan Khoronzhuk 	cpsw_ale_dump(cpsw->ale, reg);
257952c4f0ecSMugunthan V N }
258052c4f0ecSMugunthan V N 
2581df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev,
2582df828598SMugunthan V N 			     struct ethtool_drvinfo *info)
2583df828598SMugunthan V N {
2584649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
258556e31bd8SIvan Khoronzhuk 	struct platform_device	*pdev = to_platform_device(cpsw->dev);
25867826d43fSJiri Pirko 
258752c4f0ecSMugunthan V N 	strlcpy(info->driver, "cpsw", sizeof(info->driver));
25887826d43fSJiri Pirko 	strlcpy(info->version, "1.0", sizeof(info->version));
258956e31bd8SIvan Khoronzhuk 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
2590df828598SMugunthan V N }
2591df828598SMugunthan V N 
2592df828598SMugunthan V N static u32 cpsw_get_msglevel(struct net_device *ndev)
2593df828598SMugunthan V N {
2594df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2595df828598SMugunthan V N 	return priv->msg_enable;
2596df828598SMugunthan V N }
2597df828598SMugunthan V N 
2598df828598SMugunthan V N static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2599df828598SMugunthan V N {
2600df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2601df828598SMugunthan V N 	priv->msg_enable = value;
2602df828598SMugunthan V N }
2603df828598SMugunthan V N 
2604c8395d4eSGrygorii Strashko #if IS_ENABLED(CONFIG_TI_CPTS)
26052e5b38abSRichard Cochran static int cpsw_get_ts_info(struct net_device *ndev,
26062e5b38abSRichard Cochran 			    struct ethtool_ts_info *info)
26072e5b38abSRichard Cochran {
26082a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
26092e5b38abSRichard Cochran 
26102e5b38abSRichard Cochran 	info->so_timestamping =
26112e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_HARDWARE |
26122e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_SOFTWARE |
26132e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_HARDWARE |
26142e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_SOFTWARE |
26152e5b38abSRichard Cochran 		SOF_TIMESTAMPING_SOFTWARE |
26162e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RAW_HARDWARE;
26172a05a622SIvan Khoronzhuk 	info->phc_index = cpsw->cpts->phc_index;
26182e5b38abSRichard Cochran 	info->tx_types =
26192e5b38abSRichard Cochran 		(1 << HWTSTAMP_TX_OFF) |
26202e5b38abSRichard Cochran 		(1 << HWTSTAMP_TX_ON);
26212e5b38abSRichard Cochran 	info->rx_filters =
26222e5b38abSRichard Cochran 		(1 << HWTSTAMP_FILTER_NONE) |
2623e9523a5aSGrygorii Strashko 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
26242e5b38abSRichard Cochran 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2625c8395d4eSGrygorii Strashko 	return 0;
2626c8395d4eSGrygorii Strashko }
26272e5b38abSRichard Cochran #else
2628c8395d4eSGrygorii Strashko static int cpsw_get_ts_info(struct net_device *ndev,
2629c8395d4eSGrygorii Strashko 			    struct ethtool_ts_info *info)
2630c8395d4eSGrygorii Strashko {
26312e5b38abSRichard Cochran 	info->so_timestamping =
26322e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_SOFTWARE |
26332e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_SOFTWARE |
26342e5b38abSRichard Cochran 		SOF_TIMESTAMPING_SOFTWARE;
26352e5b38abSRichard Cochran 	info->phc_index = -1;
26362e5b38abSRichard Cochran 	info->tx_types = 0;
26372e5b38abSRichard Cochran 	info->rx_filters = 0;
26382e5b38abSRichard Cochran 	return 0;
26392e5b38abSRichard Cochran }
2640c8395d4eSGrygorii Strashko #endif
26412e5b38abSRichard Cochran 
26422479876dSPhilippe Reynes static int cpsw_get_link_ksettings(struct net_device *ndev,
26432479876dSPhilippe Reynes 				   struct ethtool_link_ksettings *ecmd)
2644d3bb9c58SMugunthan V N {
2645d3bb9c58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2646606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2647606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2648d3bb9c58SMugunthan V N 
26495514174fSyuval.shaia@oracle.com 	if (!cpsw->slaves[slave_no].phy)
2650d3bb9c58SMugunthan V N 		return -EOPNOTSUPP;
26515514174fSyuval.shaia@oracle.com 
26525514174fSyuval.shaia@oracle.com 	phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
26535514174fSyuval.shaia@oracle.com 	return 0;
2654d3bb9c58SMugunthan V N }
2655d3bb9c58SMugunthan V N 
26562479876dSPhilippe Reynes static int cpsw_set_link_ksettings(struct net_device *ndev,
26572479876dSPhilippe Reynes 				   const struct ethtool_link_ksettings *ecmd)
2658d3bb9c58SMugunthan V N {
2659d3bb9c58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2660606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2661606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2662d3bb9c58SMugunthan V N 
2663606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
26642479876dSPhilippe Reynes 		return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
26652479876dSPhilippe Reynes 						 ecmd);
2666d3bb9c58SMugunthan V N 	else
2667d3bb9c58SMugunthan V N 		return -EOPNOTSUPP;
2668d3bb9c58SMugunthan V N }
2669d3bb9c58SMugunthan V N 
2670d8a64420SMatus Ujhelyi static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2671d8a64420SMatus Ujhelyi {
2672d8a64420SMatus Ujhelyi 	struct cpsw_priv *priv = netdev_priv(ndev);
2673606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2674606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2675d8a64420SMatus Ujhelyi 
2676d8a64420SMatus Ujhelyi 	wol->supported = 0;
2677d8a64420SMatus Ujhelyi 	wol->wolopts = 0;
2678d8a64420SMatus Ujhelyi 
2679606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
2680606f3993SIvan Khoronzhuk 		phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2681d8a64420SMatus Ujhelyi }
2682d8a64420SMatus Ujhelyi 
2683d8a64420SMatus Ujhelyi static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2684d8a64420SMatus Ujhelyi {
2685d8a64420SMatus Ujhelyi 	struct cpsw_priv *priv = netdev_priv(ndev);
2686606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2687606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2688d8a64420SMatus Ujhelyi 
2689606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
2690606f3993SIvan Khoronzhuk 		return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2691d8a64420SMatus Ujhelyi 	else
2692d8a64420SMatus Ujhelyi 		return -EOPNOTSUPP;
2693d8a64420SMatus Ujhelyi }
2694d8a64420SMatus Ujhelyi 
26951923d6e4SMugunthan V N static void cpsw_get_pauseparam(struct net_device *ndev,
26961923d6e4SMugunthan V N 				struct ethtool_pauseparam *pause)
26971923d6e4SMugunthan V N {
26981923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
26991923d6e4SMugunthan V N 
27001923d6e4SMugunthan V N 	pause->autoneg = AUTONEG_DISABLE;
27011923d6e4SMugunthan V N 	pause->rx_pause = priv->rx_pause ? true : false;
27021923d6e4SMugunthan V N 	pause->tx_pause = priv->tx_pause ? true : false;
27031923d6e4SMugunthan V N }
27041923d6e4SMugunthan V N 
27051923d6e4SMugunthan V N static int cpsw_set_pauseparam(struct net_device *ndev,
27061923d6e4SMugunthan V N 			       struct ethtool_pauseparam *pause)
27071923d6e4SMugunthan V N {
27081923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
27091923d6e4SMugunthan V N 	bool link;
27101923d6e4SMugunthan V N 
27111923d6e4SMugunthan V N 	priv->rx_pause = pause->rx_pause ? true : false;
27121923d6e4SMugunthan V N 	priv->tx_pause = pause->tx_pause ? true : false;
27131923d6e4SMugunthan V N 
27141923d6e4SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
27151923d6e4SMugunthan V N 	return 0;
27161923d6e4SMugunthan V N }
27171923d6e4SMugunthan V N 
27187898b1daSGrygorii Strashko static int cpsw_ethtool_op_begin(struct net_device *ndev)
27197898b1daSGrygorii Strashko {
27207898b1daSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
2721649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
27227898b1daSGrygorii Strashko 	int ret;
27237898b1daSGrygorii Strashko 
272456e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
27257898b1daSGrygorii Strashko 	if (ret < 0) {
27267898b1daSGrygorii Strashko 		cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
272756e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
27287898b1daSGrygorii Strashko 	}
27297898b1daSGrygorii Strashko 
27307898b1daSGrygorii Strashko 	return ret;
27317898b1daSGrygorii Strashko }
27327898b1daSGrygorii Strashko 
27337898b1daSGrygorii Strashko static void cpsw_ethtool_op_complete(struct net_device *ndev)
27347898b1daSGrygorii Strashko {
27357898b1daSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
27367898b1daSGrygorii Strashko 	int ret;
27377898b1daSGrygorii Strashko 
273856e31bd8SIvan Khoronzhuk 	ret = pm_runtime_put(priv->cpsw->dev);
27397898b1daSGrygorii Strashko 	if (ret < 0)
27407898b1daSGrygorii Strashko 		cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
27417898b1daSGrygorii Strashko }
27427898b1daSGrygorii Strashko 
2743ce52c744SIvan Khoronzhuk static void cpsw_get_channels(struct net_device *ndev,
2744ce52c744SIvan Khoronzhuk 			      struct ethtool_channels *ch)
2745ce52c744SIvan Khoronzhuk {
2746ce52c744SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2747ce52c744SIvan Khoronzhuk 
27489611d6d6SIvan Khoronzhuk 	ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
27499611d6d6SIvan Khoronzhuk 	ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2750ce52c744SIvan Khoronzhuk 	ch->max_combined = 0;
2751ce52c744SIvan Khoronzhuk 	ch->max_other = 0;
2752ce52c744SIvan Khoronzhuk 	ch->other_count = 0;
2753ce52c744SIvan Khoronzhuk 	ch->rx_count = cpsw->rx_ch_num;
2754ce52c744SIvan Khoronzhuk 	ch->tx_count = cpsw->tx_ch_num;
2755ce52c744SIvan Khoronzhuk 	ch->combined_count = 0;
2756ce52c744SIvan Khoronzhuk }
2757ce52c744SIvan Khoronzhuk 
2758ce52c744SIvan Khoronzhuk static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2759ce52c744SIvan Khoronzhuk 				  struct ethtool_channels *ch)
2760ce52c744SIvan Khoronzhuk {
27619611d6d6SIvan Khoronzhuk 	if (cpsw->quirk_irq) {
27629611d6d6SIvan Khoronzhuk 		dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
27639611d6d6SIvan Khoronzhuk 		return -EOPNOTSUPP;
27649611d6d6SIvan Khoronzhuk 	}
27659611d6d6SIvan Khoronzhuk 
2766ce52c744SIvan Khoronzhuk 	if (ch->combined_count)
2767ce52c744SIvan Khoronzhuk 		return -EINVAL;
2768ce52c744SIvan Khoronzhuk 
2769ce52c744SIvan Khoronzhuk 	/* verify we have at least one channel in each direction */
2770ce52c744SIvan Khoronzhuk 	if (!ch->rx_count || !ch->tx_count)
2771ce52c744SIvan Khoronzhuk 		return -EINVAL;
2772ce52c744SIvan Khoronzhuk 
2773ce52c744SIvan Khoronzhuk 	if (ch->rx_count > cpsw->data.channels ||
2774ce52c744SIvan Khoronzhuk 	    ch->tx_count > cpsw->data.channels)
2775ce52c744SIvan Khoronzhuk 		return -EINVAL;
2776ce52c744SIvan Khoronzhuk 
2777ce52c744SIvan Khoronzhuk 	return 0;
2778ce52c744SIvan Khoronzhuk }
2779ce52c744SIvan Khoronzhuk 
2780ce52c744SIvan Khoronzhuk static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2781ce52c744SIvan Khoronzhuk {
2782ce52c744SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2783ce52c744SIvan Khoronzhuk 	void (*handler)(void *, int, int);
278483fcad0cSIvan Khoronzhuk 	struct netdev_queue *queue;
27858feb0a19SIvan Khoronzhuk 	struct cpsw_vector *vec;
278679b3325dSIvan Khoronzhuk 	int ret, *ch, vch;
2787ce52c744SIvan Khoronzhuk 
2788ce52c744SIvan Khoronzhuk 	if (rx) {
2789ce52c744SIvan Khoronzhuk 		ch = &cpsw->rx_ch_num;
27908feb0a19SIvan Khoronzhuk 		vec = cpsw->rxv;
2791ce52c744SIvan Khoronzhuk 		handler = cpsw_rx_handler;
2792ce52c744SIvan Khoronzhuk 	} else {
2793ce52c744SIvan Khoronzhuk 		ch = &cpsw->tx_ch_num;
27948feb0a19SIvan Khoronzhuk 		vec = cpsw->txv;
2795ce52c744SIvan Khoronzhuk 		handler = cpsw_tx_handler;
2796ce52c744SIvan Khoronzhuk 	}
2797ce52c744SIvan Khoronzhuk 
2798ce52c744SIvan Khoronzhuk 	while (*ch < ch_num) {
279979b3325dSIvan Khoronzhuk 		vch = rx ? *ch : 7 - *ch;
280079b3325dSIvan Khoronzhuk 		vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx);
280183fcad0cSIvan Khoronzhuk 		queue = netdev_get_tx_queue(priv->ndev, *ch);
280283fcad0cSIvan Khoronzhuk 		queue->tx_maxrate = 0;
2803ce52c744SIvan Khoronzhuk 
28048feb0a19SIvan Khoronzhuk 		if (IS_ERR(vec[*ch].ch))
28058feb0a19SIvan Khoronzhuk 			return PTR_ERR(vec[*ch].ch);
2806ce52c744SIvan Khoronzhuk 
28078feb0a19SIvan Khoronzhuk 		if (!vec[*ch].ch)
2808ce52c744SIvan Khoronzhuk 			return -EINVAL;
2809ce52c744SIvan Khoronzhuk 
2810ce52c744SIvan Khoronzhuk 		cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2811ce52c744SIvan Khoronzhuk 			  (rx ? "rx" : "tx"));
2812ce52c744SIvan Khoronzhuk 		(*ch)++;
2813ce52c744SIvan Khoronzhuk 	}
2814ce52c744SIvan Khoronzhuk 
2815ce52c744SIvan Khoronzhuk 	while (*ch > ch_num) {
2816ce52c744SIvan Khoronzhuk 		(*ch)--;
2817ce52c744SIvan Khoronzhuk 
28188feb0a19SIvan Khoronzhuk 		ret = cpdma_chan_destroy(vec[*ch].ch);
2819ce52c744SIvan Khoronzhuk 		if (ret)
2820ce52c744SIvan Khoronzhuk 			return ret;
2821ce52c744SIvan Khoronzhuk 
2822ce52c744SIvan Khoronzhuk 		cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2823ce52c744SIvan Khoronzhuk 			  (rx ? "rx" : "tx"));
2824ce52c744SIvan Khoronzhuk 	}
2825ce52c744SIvan Khoronzhuk 
2826ce52c744SIvan Khoronzhuk 	return 0;
2827ce52c744SIvan Khoronzhuk }
2828ce52c744SIvan Khoronzhuk 
2829ce52c744SIvan Khoronzhuk static int cpsw_update_channels(struct cpsw_priv *priv,
2830ce52c744SIvan Khoronzhuk 				struct ethtool_channels *ch)
2831ce52c744SIvan Khoronzhuk {
2832ce52c744SIvan Khoronzhuk 	int ret;
2833ce52c744SIvan Khoronzhuk 
2834ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2835ce52c744SIvan Khoronzhuk 	if (ret)
2836ce52c744SIvan Khoronzhuk 		return ret;
2837ce52c744SIvan Khoronzhuk 
2838ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2839ce52c744SIvan Khoronzhuk 	if (ret)
2840ce52c744SIvan Khoronzhuk 		return ret;
2841ce52c744SIvan Khoronzhuk 
2842ce52c744SIvan Khoronzhuk 	return 0;
2843ce52c744SIvan Khoronzhuk }
2844ce52c744SIvan Khoronzhuk 
2845022d7ad7SIvan Khoronzhuk static void cpsw_suspend_data_pass(struct net_device *ndev)
2846ce52c744SIvan Khoronzhuk {
2847022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2848ce52c744SIvan Khoronzhuk 	struct cpsw_slave *slave;
2849022d7ad7SIvan Khoronzhuk 	int i;
2850ce52c744SIvan Khoronzhuk 
2851ce52c744SIvan Khoronzhuk 	/* Disable NAPI scheduling */
2852ce52c744SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2853ce52c744SIvan Khoronzhuk 
2854ce52c744SIvan Khoronzhuk 	/* Stop all transmit queues for every network device.
2855ce52c744SIvan Khoronzhuk 	 * Disable re-using rx descriptors with dormant_on.
2856ce52c744SIvan Khoronzhuk 	 */
2857ce52c744SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2858ce52c744SIvan Khoronzhuk 		if (!(slave->ndev && netif_running(slave->ndev)))
2859ce52c744SIvan Khoronzhuk 			continue;
2860ce52c744SIvan Khoronzhuk 
2861ce52c744SIvan Khoronzhuk 		netif_tx_stop_all_queues(slave->ndev);
2862ce52c744SIvan Khoronzhuk 		netif_dormant_on(slave->ndev);
2863ce52c744SIvan Khoronzhuk 	}
2864ce52c744SIvan Khoronzhuk 
2865ce52c744SIvan Khoronzhuk 	/* Handle rest of tx packets and stop cpdma channels */
2866ce52c744SIvan Khoronzhuk 	cpdma_ctlr_stop(cpsw->dma);
2867022d7ad7SIvan Khoronzhuk }
2868022d7ad7SIvan Khoronzhuk 
2869022d7ad7SIvan Khoronzhuk static int cpsw_resume_data_pass(struct net_device *ndev)
2870022d7ad7SIvan Khoronzhuk {
2871022d7ad7SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
2872022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2873022d7ad7SIvan Khoronzhuk 	struct cpsw_slave *slave;
2874022d7ad7SIvan Khoronzhuk 	int i, ret;
2875022d7ad7SIvan Khoronzhuk 
2876022d7ad7SIvan Khoronzhuk 	/* Allow rx packets handling */
2877022d7ad7SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2878022d7ad7SIvan Khoronzhuk 		if (slave->ndev && netif_running(slave->ndev))
2879022d7ad7SIvan Khoronzhuk 			netif_dormant_off(slave->ndev);
2880022d7ad7SIvan Khoronzhuk 
2881022d7ad7SIvan Khoronzhuk 	/* After this receive is started */
2882d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count) {
2883022d7ad7SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
2884022d7ad7SIvan Khoronzhuk 		if (ret)
2885022d7ad7SIvan Khoronzhuk 			return ret;
2886022d7ad7SIvan Khoronzhuk 
2887022d7ad7SIvan Khoronzhuk 		cpdma_ctlr_start(cpsw->dma);
2888022d7ad7SIvan Khoronzhuk 		cpsw_intr_enable(cpsw);
2889022d7ad7SIvan Khoronzhuk 	}
2890022d7ad7SIvan Khoronzhuk 
2891022d7ad7SIvan Khoronzhuk 	/* Resume transmit for every affected interface */
2892022d7ad7SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2893022d7ad7SIvan Khoronzhuk 		if (slave->ndev && netif_running(slave->ndev))
2894022d7ad7SIvan Khoronzhuk 			netif_tx_start_all_queues(slave->ndev);
2895022d7ad7SIvan Khoronzhuk 
2896022d7ad7SIvan Khoronzhuk 	return 0;
2897022d7ad7SIvan Khoronzhuk }
2898022d7ad7SIvan Khoronzhuk 
2899022d7ad7SIvan Khoronzhuk static int cpsw_set_channels(struct net_device *ndev,
2900022d7ad7SIvan Khoronzhuk 			     struct ethtool_channels *chs)
2901022d7ad7SIvan Khoronzhuk {
2902022d7ad7SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
2903022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2904022d7ad7SIvan Khoronzhuk 	struct cpsw_slave *slave;
2905022d7ad7SIvan Khoronzhuk 	int i, ret;
2906022d7ad7SIvan Khoronzhuk 
2907022d7ad7SIvan Khoronzhuk 	ret = cpsw_check_ch_settings(cpsw, chs);
2908022d7ad7SIvan Khoronzhuk 	if (ret < 0)
2909022d7ad7SIvan Khoronzhuk 		return ret;
2910022d7ad7SIvan Khoronzhuk 
2911022d7ad7SIvan Khoronzhuk 	cpsw_suspend_data_pass(ndev);
2912ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels(priv, chs);
2913ce52c744SIvan Khoronzhuk 	if (ret)
2914ce52c744SIvan Khoronzhuk 		goto err;
2915ce52c744SIvan Khoronzhuk 
2916ce52c744SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2917ce52c744SIvan Khoronzhuk 		if (!(slave->ndev && netif_running(slave->ndev)))
2918ce52c744SIvan Khoronzhuk 			continue;
2919ce52c744SIvan Khoronzhuk 
2920ce52c744SIvan Khoronzhuk 		/* Inform stack about new count of queues */
2921ce52c744SIvan Khoronzhuk 		ret = netif_set_real_num_tx_queues(slave->ndev,
2922ce52c744SIvan Khoronzhuk 						   cpsw->tx_ch_num);
2923ce52c744SIvan Khoronzhuk 		if (ret) {
2924ce52c744SIvan Khoronzhuk 			dev_err(priv->dev, "cannot set real number of tx queues\n");
2925ce52c744SIvan Khoronzhuk 			goto err;
2926ce52c744SIvan Khoronzhuk 		}
2927ce52c744SIvan Khoronzhuk 
2928ce52c744SIvan Khoronzhuk 		ret = netif_set_real_num_rx_queues(slave->ndev,
2929ce52c744SIvan Khoronzhuk 						   cpsw->rx_ch_num);
2930ce52c744SIvan Khoronzhuk 		if (ret) {
2931ce52c744SIvan Khoronzhuk 			dev_err(priv->dev, "cannot set real number of rx queues\n");
2932ce52c744SIvan Khoronzhuk 			goto err;
2933ce52c744SIvan Khoronzhuk 		}
2934ce52c744SIvan Khoronzhuk 	}
2935ce52c744SIvan Khoronzhuk 
2936d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count)
293732b78d85SIvan Khoronzhuk 		cpsw_split_res(ndev);
29388feb0a19SIvan Khoronzhuk 
2939022d7ad7SIvan Khoronzhuk 	ret = cpsw_resume_data_pass(ndev);
2940022d7ad7SIvan Khoronzhuk 	if (!ret)
2941ce52c744SIvan Khoronzhuk 		return 0;
2942ce52c744SIvan Khoronzhuk err:
2943ce52c744SIvan Khoronzhuk 	dev_err(priv->dev, "cannot update channels number, closing device\n");
2944ce52c744SIvan Khoronzhuk 	dev_close(ndev);
2945ce52c744SIvan Khoronzhuk 	return ret;
2946ce52c744SIvan Khoronzhuk }
2947ce52c744SIvan Khoronzhuk 
2948a0909949SYegor Yefremov static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2949a0909949SYegor Yefremov {
2950a0909949SYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
2951a0909949SYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
2952a0909949SYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
2953a0909949SYegor Yefremov 
2954a0909949SYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
2955a0909949SYegor Yefremov 		return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2956a0909949SYegor Yefremov 	else
2957a0909949SYegor Yefremov 		return -EOPNOTSUPP;
2958a0909949SYegor Yefremov }
2959a0909949SYegor Yefremov 
2960a0909949SYegor Yefremov static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2961a0909949SYegor Yefremov {
2962a0909949SYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
2963a0909949SYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
2964a0909949SYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
2965a0909949SYegor Yefremov 
2966a0909949SYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
2967a0909949SYegor Yefremov 		return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2968a0909949SYegor Yefremov 	else
2969a0909949SYegor Yefremov 		return -EOPNOTSUPP;
2970a0909949SYegor Yefremov }
2971a0909949SYegor Yefremov 
29726bb10c2bSYegor Yefremov static int cpsw_nway_reset(struct net_device *ndev)
29736bb10c2bSYegor Yefremov {
29746bb10c2bSYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
29756bb10c2bSYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
29766bb10c2bSYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
29776bb10c2bSYegor Yefremov 
29786bb10c2bSYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
29796bb10c2bSYegor Yefremov 		return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
29806bb10c2bSYegor Yefremov 	else
29816bb10c2bSYegor Yefremov 		return -EOPNOTSUPP;
29826bb10c2bSYegor Yefremov }
29836bb10c2bSYegor Yefremov 
2984be034fc1SGrygorii Strashko static void cpsw_get_ringparam(struct net_device *ndev,
2985be034fc1SGrygorii Strashko 			       struct ethtool_ringparam *ering)
2986be034fc1SGrygorii Strashko {
2987be034fc1SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
2988be034fc1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
2989be034fc1SGrygorii Strashko 
2990be034fc1SGrygorii Strashko 	/* not supported */
2991be034fc1SGrygorii Strashko 	ering->tx_max_pending = 0;
2992be034fc1SGrygorii Strashko 	ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
2993f89d21b9SIvan Khoronzhuk 	ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
2994be034fc1SGrygorii Strashko 	ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
2995be034fc1SGrygorii Strashko }
2996be034fc1SGrygorii Strashko 
2997be034fc1SGrygorii Strashko static int cpsw_set_ringparam(struct net_device *ndev,
2998be034fc1SGrygorii Strashko 			      struct ethtool_ringparam *ering)
2999be034fc1SGrygorii Strashko {
3000be034fc1SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
3001be034fc1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
3002022d7ad7SIvan Khoronzhuk 	int ret;
3003be034fc1SGrygorii Strashko 
3004be034fc1SGrygorii Strashko 	/* ignore ering->tx_pending - only rx_pending adjustment is supported */
3005be034fc1SGrygorii Strashko 
3006be034fc1SGrygorii Strashko 	if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
3007f89d21b9SIvan Khoronzhuk 	    ering->rx_pending < CPSW_MAX_QUEUES ||
3008f89d21b9SIvan Khoronzhuk 	    ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
3009be034fc1SGrygorii Strashko 		return -EINVAL;
3010be034fc1SGrygorii Strashko 
3011be034fc1SGrygorii Strashko 	if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
3012be034fc1SGrygorii Strashko 		return 0;
3013be034fc1SGrygorii Strashko 
3014022d7ad7SIvan Khoronzhuk 	cpsw_suspend_data_pass(ndev);
3015be034fc1SGrygorii Strashko 
3016be034fc1SGrygorii Strashko 	cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
3017be034fc1SGrygorii Strashko 
3018d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count)
3019be034fc1SGrygorii Strashko 		cpdma_chan_split_pool(cpsw->dma);
3020be034fc1SGrygorii Strashko 
3021022d7ad7SIvan Khoronzhuk 	ret = cpsw_resume_data_pass(ndev);
3022022d7ad7SIvan Khoronzhuk 	if (!ret)
3023be034fc1SGrygorii Strashko 		return 0;
3024022d7ad7SIvan Khoronzhuk 
3025022d7ad7SIvan Khoronzhuk 	dev_err(&ndev->dev, "cannot set ring params, closing device\n");
3026be034fc1SGrygorii Strashko 	dev_close(ndev);
3027be034fc1SGrygorii Strashko 	return ret;
3028be034fc1SGrygorii Strashko }
3029be034fc1SGrygorii Strashko 
3030df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = {
3031df828598SMugunthan V N 	.get_drvinfo	= cpsw_get_drvinfo,
3032df828598SMugunthan V N 	.get_msglevel	= cpsw_get_msglevel,
3033df828598SMugunthan V N 	.set_msglevel	= cpsw_set_msglevel,
3034df828598SMugunthan V N 	.get_link	= ethtool_op_get_link,
30352e5b38abSRichard Cochran 	.get_ts_info	= cpsw_get_ts_info,
3036ff5b8ef2SMugunthan V N 	.get_coalesce	= cpsw_get_coalesce,
3037ff5b8ef2SMugunthan V N 	.set_coalesce	= cpsw_set_coalesce,
3038d9718546SMugunthan V N 	.get_sset_count		= cpsw_get_sset_count,
3039d9718546SMugunthan V N 	.get_strings		= cpsw_get_strings,
3040d9718546SMugunthan V N 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
30411923d6e4SMugunthan V N 	.get_pauseparam		= cpsw_get_pauseparam,
30421923d6e4SMugunthan V N 	.set_pauseparam		= cpsw_set_pauseparam,
3043d8a64420SMatus Ujhelyi 	.get_wol	= cpsw_get_wol,
3044d8a64420SMatus Ujhelyi 	.set_wol	= cpsw_set_wol,
304552c4f0ecSMugunthan V N 	.get_regs_len	= cpsw_get_regs_len,
304652c4f0ecSMugunthan V N 	.get_regs	= cpsw_get_regs,
30477898b1daSGrygorii Strashko 	.begin		= cpsw_ethtool_op_begin,
30487898b1daSGrygorii Strashko 	.complete	= cpsw_ethtool_op_complete,
3049ce52c744SIvan Khoronzhuk 	.get_channels	= cpsw_get_channels,
3050ce52c744SIvan Khoronzhuk 	.set_channels	= cpsw_set_channels,
30512479876dSPhilippe Reynes 	.get_link_ksettings	= cpsw_get_link_ksettings,
30522479876dSPhilippe Reynes 	.set_link_ksettings	= cpsw_set_link_ksettings,
3053a0909949SYegor Yefremov 	.get_eee	= cpsw_get_eee,
3054a0909949SYegor Yefremov 	.set_eee	= cpsw_set_eee,
30556bb10c2bSYegor Yefremov 	.nway_reset	= cpsw_nway_reset,
3056be034fc1SGrygorii Strashko 	.get_ringparam = cpsw_get_ringparam,
3057be034fc1SGrygorii Strashko 	.set_ringparam = cpsw_set_ringparam,
3058df828598SMugunthan V N };
3059df828598SMugunthan V N 
3060606f3993SIvan Khoronzhuk static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
3061549985eeSRichard Cochran 			    u32 slave_reg_ofs, u32 sliver_reg_ofs)
3062df828598SMugunthan V N {
30635d8d0d4dSIvan Khoronzhuk 	void __iomem		*regs = cpsw->regs;
3064df828598SMugunthan V N 	int			slave_num = slave->slave_num;
3065606f3993SIvan Khoronzhuk 	struct cpsw_slave_data	*data = cpsw->data.slave_data + slave_num;
3066df828598SMugunthan V N 
3067df828598SMugunthan V N 	slave->data	= data;
3068549985eeSRichard Cochran 	slave->regs	= regs + slave_reg_ofs;
3069549985eeSRichard Cochran 	slave->sliver	= regs + sliver_reg_ofs;
3070d9ba8f9eSMugunthan V N 	slave->port_vlan = data->dual_emac_res_vlan;
3071df828598SMugunthan V N }
3072df828598SMugunthan V N 
3073552165bcSDavid Rivshin static int cpsw_probe_dt(struct cpsw_platform_data *data,
30742eb32b0aSMugunthan V N 			 struct platform_device *pdev)
30752eb32b0aSMugunthan V N {
30762eb32b0aSMugunthan V N 	struct device_node *node = pdev->dev.of_node;
30772eb32b0aSMugunthan V N 	struct device_node *slave_node;
30782eb32b0aSMugunthan V N 	int i = 0, ret;
30792eb32b0aSMugunthan V N 	u32 prop;
30802eb32b0aSMugunthan V N 
30812eb32b0aSMugunthan V N 	if (!node)
30822eb32b0aSMugunthan V N 		return -EINVAL;
30832eb32b0aSMugunthan V N 
30842eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "slaves", &prop)) {
308588c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
30862eb32b0aSMugunthan V N 		return -EINVAL;
30872eb32b0aSMugunthan V N 	}
30882eb32b0aSMugunthan V N 	data->slaves = prop;
30892eb32b0aSMugunthan V N 
3090e86ac13bSMugunthan V N 	if (of_property_read_u32(node, "active_slave", &prop)) {
309188c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
3092aa1a15e2SDaniel Mack 		return -EINVAL;
309378ca0b28SRichard Cochran 	}
3094e86ac13bSMugunthan V N 	data->active_slave = prop;
309578ca0b28SRichard Cochran 
3096a86854d0SKees Cook 	data->slave_data = devm_kcalloc(&pdev->dev,
3097a86854d0SKees Cook 					data->slaves,
3098a86854d0SKees Cook 					sizeof(struct cpsw_slave_data),
3099b2adaca9SJoe Perches 					GFP_KERNEL);
3100b2adaca9SJoe Perches 	if (!data->slave_data)
3101aa1a15e2SDaniel Mack 		return -ENOMEM;
31022eb32b0aSMugunthan V N 
31032eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "cpdma_channels", &prop)) {
310488c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
3105aa1a15e2SDaniel Mack 		return -EINVAL;
31062eb32b0aSMugunthan V N 	}
31072eb32b0aSMugunthan V N 	data->channels = prop;
31082eb32b0aSMugunthan V N 
31092eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "ale_entries", &prop)) {
311088c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
3111aa1a15e2SDaniel Mack 		return -EINVAL;
31122eb32b0aSMugunthan V N 	}
31132eb32b0aSMugunthan V N 	data->ale_entries = prop;
31142eb32b0aSMugunthan V N 
31152eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "bd_ram_size", &prop)) {
311688c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
3117aa1a15e2SDaniel Mack 		return -EINVAL;
31182eb32b0aSMugunthan V N 	}
31192eb32b0aSMugunthan V N 	data->bd_ram_size = prop;
31202eb32b0aSMugunthan V N 
31212eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "mac_control", &prop)) {
312288c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
3123aa1a15e2SDaniel Mack 		return -EINVAL;
31242eb32b0aSMugunthan V N 	}
31252eb32b0aSMugunthan V N 	data->mac_control = prop;
31262eb32b0aSMugunthan V N 
3127281abd96SMarkus Pargmann 	if (of_property_read_bool(node, "dual_emac"))
3128281abd96SMarkus Pargmann 		data->dual_emac = 1;
3129d9ba8f9eSMugunthan V N 
31301fb19aa7SVaibhav Hiremath 	/*
31311fb19aa7SVaibhav Hiremath 	 * Populate all the child nodes here...
31321fb19aa7SVaibhav Hiremath 	 */
31331fb19aa7SVaibhav Hiremath 	ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
31341fb19aa7SVaibhav Hiremath 	/* We do not want to force this, as in some cases may not have child */
31351fb19aa7SVaibhav Hiremath 	if (ret)
313688c99ff6SGeorge Cherian 		dev_warn(&pdev->dev, "Doesn't have any child node\n");
31371fb19aa7SVaibhav Hiremath 
31388658aaf2SBen Hutchings 	for_each_available_child_of_node(node, slave_node) {
3139549985eeSRichard Cochran 		struct cpsw_slave_data *slave_data = data->slave_data + i;
3140549985eeSRichard Cochran 		const void *mac_addr = NULL;
3141549985eeSRichard Cochran 		int lenp;
3142549985eeSRichard Cochran 		const __be32 *parp;
3143549985eeSRichard Cochran 
3144f468b10eSMarkus Pargmann 		/* This is no slave child node, continue */
3145f468b10eSMarkus Pargmann 		if (strcmp(slave_node->name, "slave"))
3146f468b10eSMarkus Pargmann 			continue;
3147f468b10eSMarkus Pargmann 
3148552165bcSDavid Rivshin 		slave_data->phy_node = of_parse_phandle(slave_node,
3149552165bcSDavid Rivshin 							"phy-handle", 0);
3150f1eea5c1SDavid Rivshin 		parp = of_get_property(slave_node, "phy_id", &lenp);
3151ae092b5bSDavid Rivshin 		if (slave_data->phy_node) {
3152ae092b5bSDavid Rivshin 			dev_dbg(&pdev->dev,
3153f7ce9103SRob Herring 				"slave[%d] using phy-handle=\"%pOF\"\n",
3154f7ce9103SRob Herring 				i, slave_data->phy_node);
3155ae092b5bSDavid Rivshin 		} else if (of_phy_is_fixed_link(slave_node)) {
3156dfc0a6d3SDavid Rivshin 			/* In the case of a fixed PHY, the DT node associated
3157dfc0a6d3SDavid Rivshin 			 * to the PHY is the Ethernet MAC DT node.
3158dfc0a6d3SDavid Rivshin 			 */
31591f71e8c9SMarkus Brunner 			ret = of_phy_register_fixed_link(slave_node);
316023a09873SJohan Hovold 			if (ret) {
316123a09873SJohan Hovold 				if (ret != -EPROBE_DEFER)
316223a09873SJohan Hovold 					dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
31631f71e8c9SMarkus Brunner 				return ret;
316423a09873SJohan Hovold 			}
316506cd6d6eSDavid Rivshin 			slave_data->phy_node = of_node_get(slave_node);
3166f1eea5c1SDavid Rivshin 		} else if (parp) {
3167f1eea5c1SDavid Rivshin 			u32 phyid;
3168f1eea5c1SDavid Rivshin 			struct device_node *mdio_node;
3169f1eea5c1SDavid Rivshin 			struct platform_device *mdio;
3170f1eea5c1SDavid Rivshin 
3171f1eea5c1SDavid Rivshin 			if (lenp != (sizeof(__be32) * 2)) {
3172f1eea5c1SDavid Rivshin 				dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
317347276fccSMugunthan V N 				goto no_phy_slave;
3174549985eeSRichard Cochran 			}
3175549985eeSRichard Cochran 			mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
3176549985eeSRichard Cochran 			phyid = be32_to_cpup(parp+1);
3177549985eeSRichard Cochran 			mdio = of_find_device_by_node(mdio_node);
317860e71ab5SJohan Hovold 			of_node_put(mdio_node);
31796954cc1fSJohan Hovold 			if (!mdio) {
318056fdb2e0SMarkus Pargmann 				dev_err(&pdev->dev, "Missing mdio platform device\n");
31816954cc1fSJohan Hovold 				return -EINVAL;
31826954cc1fSJohan Hovold 			}
3183549985eeSRichard Cochran 			snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
3184549985eeSRichard Cochran 				 PHY_ID_FMT, mdio->name, phyid);
318586e1d5adSJohan Hovold 			put_device(&mdio->dev);
3186f1eea5c1SDavid Rivshin 		} else {
3187ae092b5bSDavid Rivshin 			dev_err(&pdev->dev,
3188ae092b5bSDavid Rivshin 				"No slave[%d] phy_id, phy-handle, or fixed-link property\n",
3189ae092b5bSDavid Rivshin 				i);
3190f1eea5c1SDavid Rivshin 			goto no_phy_slave;
3191f1eea5c1SDavid Rivshin 		}
319247276fccSMugunthan V N 		slave_data->phy_if = of_get_phy_mode(slave_node);
319347276fccSMugunthan V N 		if (slave_data->phy_if < 0) {
319447276fccSMugunthan V N 			dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
319547276fccSMugunthan V N 				i);
319647276fccSMugunthan V N 			return slave_data->phy_if;
319747276fccSMugunthan V N 		}
319847276fccSMugunthan V N 
319947276fccSMugunthan V N no_phy_slave:
3200549985eeSRichard Cochran 		mac_addr = of_get_mac_address(slave_node);
32010ba517b1SMarkus Pargmann 		if (mac_addr) {
3202549985eeSRichard Cochran 			memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
32030ba517b1SMarkus Pargmann 		} else {
3204b6745f6eSMugunthan V N 			ret = ti_cm_get_macid(&pdev->dev, i,
32050ba517b1SMarkus Pargmann 					      slave_data->mac_addr);
32060ba517b1SMarkus Pargmann 			if (ret)
32070ba517b1SMarkus Pargmann 				return ret;
32080ba517b1SMarkus Pargmann 		}
3209d9ba8f9eSMugunthan V N 		if (data->dual_emac) {
321091c4166cSMugunthan V N 			if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
3211d9ba8f9eSMugunthan V N 						 &prop)) {
321288c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
3213d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = i+1;
321488c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
3215d9ba8f9eSMugunthan V N 					slave_data->dual_emac_res_vlan, i);
3216d9ba8f9eSMugunthan V N 			} else {
3217d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = prop;
3218d9ba8f9eSMugunthan V N 			}
3219d9ba8f9eSMugunthan V N 		}
3220d9ba8f9eSMugunthan V N 
3221549985eeSRichard Cochran 		i++;
32223a27bfacSMugunthan V N 		if (i == data->slaves)
32233a27bfacSMugunthan V N 			break;
3224549985eeSRichard Cochran 	}
3225549985eeSRichard Cochran 
32262eb32b0aSMugunthan V N 	return 0;
32272eb32b0aSMugunthan V N }
32282eb32b0aSMugunthan V N 
3229a4e32b0dSJohan Hovold static void cpsw_remove_dt(struct platform_device *pdev)
3230a4e32b0dSJohan Hovold {
32318cbcc466SJohan Hovold 	struct net_device *ndev = platform_get_drvdata(pdev);
32328cbcc466SJohan Hovold 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
32338cbcc466SJohan Hovold 	struct cpsw_platform_data *data = &cpsw->data;
32348cbcc466SJohan Hovold 	struct device_node *node = pdev->dev.of_node;
32358cbcc466SJohan Hovold 	struct device_node *slave_node;
32368cbcc466SJohan Hovold 	int i = 0;
32378cbcc466SJohan Hovold 
32388cbcc466SJohan Hovold 	for_each_available_child_of_node(node, slave_node) {
32398cbcc466SJohan Hovold 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
32408cbcc466SJohan Hovold 
32418cbcc466SJohan Hovold 		if (strcmp(slave_node->name, "slave"))
32428cbcc466SJohan Hovold 			continue;
32438cbcc466SJohan Hovold 
32443f65047cSJohan Hovold 		if (of_phy_is_fixed_link(slave_node))
32453f65047cSJohan Hovold 			of_phy_deregister_fixed_link(slave_node);
32468cbcc466SJohan Hovold 
32478cbcc466SJohan Hovold 		of_node_put(slave_data->phy_node);
32488cbcc466SJohan Hovold 
32498cbcc466SJohan Hovold 		i++;
32508cbcc466SJohan Hovold 		if (i == data->slaves)
32518cbcc466SJohan Hovold 			break;
32528cbcc466SJohan Hovold 	}
32538cbcc466SJohan Hovold 
3254a4e32b0dSJohan Hovold 	of_platform_depopulate(&pdev->dev);
3255a4e32b0dSJohan Hovold }
3256a4e32b0dSJohan Hovold 
325756e31bd8SIvan Khoronzhuk static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
3258d9ba8f9eSMugunthan V N {
3259606f3993SIvan Khoronzhuk 	struct cpsw_common		*cpsw = priv->cpsw;
3260606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	*data = &cpsw->data;
3261d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
3262d9ba8f9eSMugunthan V N 	struct cpsw_priv		*priv_sl2;
3263e38b5a3dSIvan Khoronzhuk 	int ret = 0;
3264d9ba8f9eSMugunthan V N 
3265e05107e6SIvan Khoronzhuk 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3266d9ba8f9eSMugunthan V N 	if (!ndev) {
326756e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
3268d9ba8f9eSMugunthan V N 		return -ENOMEM;
3269d9ba8f9eSMugunthan V N 	}
3270d9ba8f9eSMugunthan V N 
3271d9ba8f9eSMugunthan V N 	priv_sl2 = netdev_priv(ndev);
3272606f3993SIvan Khoronzhuk 	priv_sl2->cpsw = cpsw;
3273d9ba8f9eSMugunthan V N 	priv_sl2->ndev = ndev;
3274d9ba8f9eSMugunthan V N 	priv_sl2->dev  = &ndev->dev;
3275d9ba8f9eSMugunthan V N 	priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
3276d9ba8f9eSMugunthan V N 
3277d9ba8f9eSMugunthan V N 	if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
3278d9ba8f9eSMugunthan V N 		memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
3279d9ba8f9eSMugunthan V N 			ETH_ALEN);
328056e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
328156e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
3282d9ba8f9eSMugunthan V N 	} else {
32836c1f0a1fSJoe Perches 		eth_random_addr(priv_sl2->mac_addr);
328456e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
328556e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
3286d9ba8f9eSMugunthan V N 	}
3287d9ba8f9eSMugunthan V N 	memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
3288d9ba8f9eSMugunthan V N 
3289d9ba8f9eSMugunthan V N 	priv_sl2->emac_port = 1;
3290606f3993SIvan Khoronzhuk 	cpsw->slaves[1].ndev = ndev;
3291193736c8SIvan Khoronzhuk 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3292d9ba8f9eSMugunthan V N 
3293d9ba8f9eSMugunthan V N 	ndev->netdev_ops = &cpsw_netdev_ops;
32947ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &cpsw_ethtool_ops;
3295d9ba8f9eSMugunthan V N 
3296d9ba8f9eSMugunthan V N 	/* register the network device */
329756e31bd8SIvan Khoronzhuk 	SET_NETDEV_DEV(ndev, cpsw->dev);
3298d9ba8f9eSMugunthan V N 	ret = register_netdev(ndev);
3299d9ba8f9eSMugunthan V N 	if (ret) {
330056e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error registering net device\n");
3301d9ba8f9eSMugunthan V N 		free_netdev(ndev);
3302d9ba8f9eSMugunthan V N 		ret = -ENODEV;
3303d9ba8f9eSMugunthan V N 	}
3304d9ba8f9eSMugunthan V N 
3305d9ba8f9eSMugunthan V N 	return ret;
3306d9ba8f9eSMugunthan V N }
3307d9ba8f9eSMugunthan V N 
33087da11600SMugunthan V N static const struct of_device_id cpsw_of_mtable[] = {
33099611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,cpsw"},
33109611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am335x-cpsw"},
33119611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am4372-cpsw"},
33129611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,dra7-cpsw"},
33137da11600SMugunthan V N 	{ /* sentinel */ },
33147da11600SMugunthan V N };
33157da11600SMugunthan V N MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
33167da11600SMugunthan V N 
33179611d6d6SIvan Khoronzhuk static const struct soc_device_attribute cpsw_soc_devices[] = {
33189611d6d6SIvan Khoronzhuk 	{ .family = "AM33xx", .revision = "ES1.0"},
33199611d6d6SIvan Khoronzhuk 	{ /* sentinel */ }
33209611d6d6SIvan Khoronzhuk };
33219611d6d6SIvan Khoronzhuk 
3322663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev)
3323df828598SMugunthan V N {
3324ef4183a1SIvan Khoronzhuk 	struct clk			*clk;
3325d1bd9acfSSebastian Siewior 	struct cpsw_platform_data	*data;
3326df828598SMugunthan V N 	struct net_device		*ndev;
3327df828598SMugunthan V N 	struct cpsw_priv		*priv;
3328df828598SMugunthan V N 	struct cpdma_params		dma_params;
3329df828598SMugunthan V N 	struct cpsw_ale_params		ale_params;
3330aa1a15e2SDaniel Mack 	void __iomem			*ss_regs;
33318a2c9a5aSGrygorii Strashko 	void __iomem			*cpts_regs;
3332aa1a15e2SDaniel Mack 	struct resource			*res, *ss_res;
33331d147ccbSMugunthan V N 	struct gpio_descs		*mode;
3334549985eeSRichard Cochran 	u32 slave_offset, sliver_offset, slave_size;
33359611d6d6SIvan Khoronzhuk 	const struct soc_device_attribute *soc;
3336649a1688SIvan Khoronzhuk 	struct cpsw_common		*cpsw;
333779b3325dSIvan Khoronzhuk 	int ret = 0, i, ch;
33385087b915SFelipe Balbi 	int irq;
3339df828598SMugunthan V N 
3340649a1688SIvan Khoronzhuk 	cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
33413420ea88SJohan Hovold 	if (!cpsw)
33423420ea88SJohan Hovold 		return -ENOMEM;
33433420ea88SJohan Hovold 
334456e31bd8SIvan Khoronzhuk 	cpsw->dev = &pdev->dev;
3345649a1688SIvan Khoronzhuk 
3346e05107e6SIvan Khoronzhuk 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3347df828598SMugunthan V N 	if (!ndev) {
334888c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "error allocating net_device\n");
3349df828598SMugunthan V N 		return -ENOMEM;
3350df828598SMugunthan V N 	}
3351df828598SMugunthan V N 
3352df828598SMugunthan V N 	platform_set_drvdata(pdev, ndev);
3353df828598SMugunthan V N 	priv = netdev_priv(ndev);
3354649a1688SIvan Khoronzhuk 	priv->cpsw = cpsw;
3355df828598SMugunthan V N 	priv->ndev = ndev;
3356df828598SMugunthan V N 	priv->dev  = &ndev->dev;
3357df828598SMugunthan V N 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
33582a05a622SIvan Khoronzhuk 	cpsw->rx_packet_max = max(rx_packet_max, 128);
3359df828598SMugunthan V N 
33601d147ccbSMugunthan V N 	mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
33611d147ccbSMugunthan V N 	if (IS_ERR(mode)) {
33621d147ccbSMugunthan V N 		ret = PTR_ERR(mode);
33631d147ccbSMugunthan V N 		dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
33641d147ccbSMugunthan V N 		goto clean_ndev_ret;
33651d147ccbSMugunthan V N 	}
33661d147ccbSMugunthan V N 
33671fb19aa7SVaibhav Hiremath 	/*
33681fb19aa7SVaibhav Hiremath 	 * This may be required here for child devices.
33691fb19aa7SVaibhav Hiremath 	 */
33701fb19aa7SVaibhav Hiremath 	pm_runtime_enable(&pdev->dev);
33711fb19aa7SVaibhav Hiremath 
3372739683b4SMugunthan V N 	/* Select default pin state */
3373739683b4SMugunthan V N 	pinctrl_pm_select_default_state(&pdev->dev);
3374739683b4SMugunthan V N 
3375a4e32b0dSJohan Hovold 	/* Need to enable clocks with runtime PM api to access module
3376a4e32b0dSJohan Hovold 	 * registers
3377a4e32b0dSJohan Hovold 	 */
3378a4e32b0dSJohan Hovold 	ret = pm_runtime_get_sync(&pdev->dev);
3379a4e32b0dSJohan Hovold 	if (ret < 0) {
3380a4e32b0dSJohan Hovold 		pm_runtime_put_noidle(&pdev->dev);
3381aa1a15e2SDaniel Mack 		goto clean_runtime_disable_ret;
33822eb32b0aSMugunthan V N 	}
3383a4e32b0dSJohan Hovold 
338423a09873SJohan Hovold 	ret = cpsw_probe_dt(&cpsw->data, pdev);
338523a09873SJohan Hovold 	if (ret)
3386a4e32b0dSJohan Hovold 		goto clean_dt_ret;
338723a09873SJohan Hovold 
3388606f3993SIvan Khoronzhuk 	data = &cpsw->data;
3389e05107e6SIvan Khoronzhuk 	cpsw->rx_ch_num = 1;
3390e05107e6SIvan Khoronzhuk 	cpsw->tx_ch_num = 1;
33912eb32b0aSMugunthan V N 
3392df828598SMugunthan V N 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
3393df828598SMugunthan V N 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
339488c99ff6SGeorge Cherian 		dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
3395df828598SMugunthan V N 	} else {
33967efd26d0SJoe Perches 		eth_random_addr(priv->mac_addr);
339788c99ff6SGeorge Cherian 		dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
3398df828598SMugunthan V N 	}
3399df828598SMugunthan V N 
3400df828598SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
3401df828598SMugunthan V N 
3402a86854d0SKees Cook 	cpsw->slaves = devm_kcalloc(&pdev->dev,
3403a86854d0SKees Cook 				    data->slaves, sizeof(struct cpsw_slave),
3404df828598SMugunthan V N 				    GFP_KERNEL);
3405606f3993SIvan Khoronzhuk 	if (!cpsw->slaves) {
3406aa1a15e2SDaniel Mack 		ret = -ENOMEM;
3407a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3408df828598SMugunthan V N 	}
3409df828598SMugunthan V N 	for (i = 0; i < data->slaves; i++)
3410606f3993SIvan Khoronzhuk 		cpsw->slaves[i].slave_num = i;
3411df828598SMugunthan V N 
3412606f3993SIvan Khoronzhuk 	cpsw->slaves[0].ndev = ndev;
3413d9ba8f9eSMugunthan V N 	priv->emac_port = 0;
3414d9ba8f9eSMugunthan V N 
3415ef4183a1SIvan Khoronzhuk 	clk = devm_clk_get(&pdev->dev, "fck");
3416ef4183a1SIvan Khoronzhuk 	if (IS_ERR(clk)) {
3417aa1a15e2SDaniel Mack 		dev_err(priv->dev, "fck is not found\n");
3418f150bd7fSMugunthan V N 		ret = -ENODEV;
3419a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3420df828598SMugunthan V N 	}
34212a05a622SIvan Khoronzhuk 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
3422df828598SMugunthan V N 
3423aa1a15e2SDaniel Mack 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3424aa1a15e2SDaniel Mack 	ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
3425aa1a15e2SDaniel Mack 	if (IS_ERR(ss_regs)) {
3426aa1a15e2SDaniel Mack 		ret = PTR_ERR(ss_regs);
3427a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3428df828598SMugunthan V N 	}
34295d8d0d4dSIvan Khoronzhuk 	cpsw->regs = ss_regs;
3430df828598SMugunthan V N 
34312a05a622SIvan Khoronzhuk 	cpsw->version = readl(&cpsw->regs->id_ver);
3432f280e89aSMugunthan V N 
3433aa1a15e2SDaniel Mack 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
34345d8d0d4dSIvan Khoronzhuk 	cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
34355d8d0d4dSIvan Khoronzhuk 	if (IS_ERR(cpsw->wr_regs)) {
34365d8d0d4dSIvan Khoronzhuk 		ret = PTR_ERR(cpsw->wr_regs);
3437a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3438df828598SMugunthan V N 	}
3439df828598SMugunthan V N 
3440df828598SMugunthan V N 	memset(&dma_params, 0, sizeof(dma_params));
3441549985eeSRichard Cochran 	memset(&ale_params, 0, sizeof(ale_params));
3442549985eeSRichard Cochran 
34432a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
3444549985eeSRichard Cochran 	case CPSW_VERSION_1:
34455d8d0d4dSIvan Khoronzhuk 		cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
34468a2c9a5aSGrygorii Strashko 		cpts_regs		= ss_regs + CPSW1_CPTS_OFFSET;
34475d8d0d4dSIvan Khoronzhuk 		cpsw->hw_stats	     = ss_regs + CPSW1_HW_STATS;
3448549985eeSRichard Cochran 		dma_params.dmaregs   = ss_regs + CPSW1_CPDMA_OFFSET;
3449549985eeSRichard Cochran 		dma_params.txhdp     = ss_regs + CPSW1_STATERAM_OFFSET;
3450549985eeSRichard Cochran 		ale_params.ale_regs  = ss_regs + CPSW1_ALE_OFFSET;
3451549985eeSRichard Cochran 		slave_offset         = CPSW1_SLAVE_OFFSET;
3452549985eeSRichard Cochran 		slave_size           = CPSW1_SLAVE_SIZE;
3453549985eeSRichard Cochran 		sliver_offset        = CPSW1_SLIVER_OFFSET;
3454549985eeSRichard Cochran 		dma_params.desc_mem_phys = 0;
3455549985eeSRichard Cochran 		break;
3456549985eeSRichard Cochran 	case CPSW_VERSION_2:
3457c193f365SMugunthan V N 	case CPSW_VERSION_3:
3458926489beSMugunthan V N 	case CPSW_VERSION_4:
34595d8d0d4dSIvan Khoronzhuk 		cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
34608a2c9a5aSGrygorii Strashko 		cpts_regs		= ss_regs + CPSW2_CPTS_OFFSET;
34615d8d0d4dSIvan Khoronzhuk 		cpsw->hw_stats	     = ss_regs + CPSW2_HW_STATS;
3462549985eeSRichard Cochran 		dma_params.dmaregs   = ss_regs + CPSW2_CPDMA_OFFSET;
3463549985eeSRichard Cochran 		dma_params.txhdp     = ss_regs + CPSW2_STATERAM_OFFSET;
3464549985eeSRichard Cochran 		ale_params.ale_regs  = ss_regs + CPSW2_ALE_OFFSET;
3465549985eeSRichard Cochran 		slave_offset         = CPSW2_SLAVE_OFFSET;
3466549985eeSRichard Cochran 		slave_size           = CPSW2_SLAVE_SIZE;
3467549985eeSRichard Cochran 		sliver_offset        = CPSW2_SLIVER_OFFSET;
3468549985eeSRichard Cochran 		dma_params.desc_mem_phys =
3469aa1a15e2SDaniel Mack 			(u32 __force) ss_res->start + CPSW2_BD_OFFSET;
3470549985eeSRichard Cochran 		break;
3471549985eeSRichard Cochran 	default:
34722a05a622SIvan Khoronzhuk 		dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
3473549985eeSRichard Cochran 		ret = -ENODEV;
3474a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3475549985eeSRichard Cochran 	}
3476606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++) {
3477606f3993SIvan Khoronzhuk 		struct cpsw_slave *slave = &cpsw->slaves[i];
3478606f3993SIvan Khoronzhuk 
3479606f3993SIvan Khoronzhuk 		cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
3480549985eeSRichard Cochran 		slave_offset  += slave_size;
3481549985eeSRichard Cochran 		sliver_offset += SLIVER_SIZE;
3482549985eeSRichard Cochran 	}
3483549985eeSRichard Cochran 
3484df828598SMugunthan V N 	dma_params.dev		= &pdev->dev;
3485549985eeSRichard Cochran 	dma_params.rxthresh	= dma_params.dmaregs + CPDMA_RXTHRESH;
3486549985eeSRichard Cochran 	dma_params.rxfree	= dma_params.dmaregs + CPDMA_RXFREE;
3487549985eeSRichard Cochran 	dma_params.rxhdp	= dma_params.txhdp + CPDMA_RXHDP;
3488549985eeSRichard Cochran 	dma_params.txcp		= dma_params.txhdp + CPDMA_TXCP;
3489549985eeSRichard Cochran 	dma_params.rxcp		= dma_params.txhdp + CPDMA_RXCP;
3490df828598SMugunthan V N 
3491df828598SMugunthan V N 	dma_params.num_chan		= data->channels;
3492df828598SMugunthan V N 	dma_params.has_soft_reset	= true;
3493df828598SMugunthan V N 	dma_params.min_packet_size	= CPSW_MIN_PACKET_SIZE;
3494df828598SMugunthan V N 	dma_params.desc_mem_size	= data->bd_ram_size;
3495df828598SMugunthan V N 	dma_params.desc_align		= 16;
3496df828598SMugunthan V N 	dma_params.has_ext_regs		= true;
3497549985eeSRichard Cochran 	dma_params.desc_hw_addr         = dma_params.desc_mem_phys;
349883fcad0cSIvan Khoronzhuk 	dma_params.bus_freq_mhz		= cpsw->bus_freq_mhz;
349990225bf0SGrygorii Strashko 	dma_params.descs_pool_size	= descs_pool_size;
3500df828598SMugunthan V N 
35012c836bd9SIvan Khoronzhuk 	cpsw->dma = cpdma_ctlr_create(&dma_params);
35022c836bd9SIvan Khoronzhuk 	if (!cpsw->dma) {
3503df828598SMugunthan V N 		dev_err(priv->dev, "error initializing dma\n");
3504df828598SMugunthan V N 		ret = -ENOMEM;
3505a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3506df828598SMugunthan V N 	}
3507df828598SMugunthan V N 
35089611d6d6SIvan Khoronzhuk 	soc = soc_device_match(cpsw_soc_devices);
35099611d6d6SIvan Khoronzhuk 	if (soc)
35109611d6d6SIvan Khoronzhuk 		cpsw->quirk_irq = 1;
35119611d6d6SIvan Khoronzhuk 
351279b3325dSIvan Khoronzhuk 	ch = cpsw->quirk_irq ? 0 : 7;
351379b3325dSIvan Khoronzhuk 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
35148a83c5d7SIvan Khoronzhuk 	if (IS_ERR(cpsw->txv[0].ch)) {
35158a83c5d7SIvan Khoronzhuk 		dev_err(priv->dev, "error initializing tx dma channel\n");
35168a83c5d7SIvan Khoronzhuk 		ret = PTR_ERR(cpsw->txv[0].ch);
35178a83c5d7SIvan Khoronzhuk 		goto clean_dma_ret;
35188a83c5d7SIvan Khoronzhuk 	}
35198a83c5d7SIvan Khoronzhuk 
35208feb0a19SIvan Khoronzhuk 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
35218a83c5d7SIvan Khoronzhuk 	if (IS_ERR(cpsw->rxv[0].ch)) {
35228a83c5d7SIvan Khoronzhuk 		dev_err(priv->dev, "error initializing rx dma channel\n");
35238a83c5d7SIvan Khoronzhuk 		ret = PTR_ERR(cpsw->rxv[0].ch);
3524df828598SMugunthan V N 		goto clean_dma_ret;
3525df828598SMugunthan V N 	}
3526df828598SMugunthan V N 
35279fe9aa0bSIvan Khoronzhuk 	ale_params.dev			= &pdev->dev;
3528df828598SMugunthan V N 	ale_params.ale_ageout		= ale_ageout;
3529df828598SMugunthan V N 	ale_params.ale_entries		= data->ale_entries;
3530c6395f12SGrygorii Strashko 	ale_params.ale_ports		= CPSW_ALE_PORTS_NUM;
3531df828598SMugunthan V N 
35322a05a622SIvan Khoronzhuk 	cpsw->ale = cpsw_ale_create(&ale_params);
35332a05a622SIvan Khoronzhuk 	if (!cpsw->ale) {
3534df828598SMugunthan V N 		dev_err(priv->dev, "error initializing ale engine\n");
3535df828598SMugunthan V N 		ret = -ENODEV;
3536df828598SMugunthan V N 		goto clean_dma_ret;
3537df828598SMugunthan V N 	}
3538df828598SMugunthan V N 
35394a88fb95SGrygorii Strashko 	cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
35408a2c9a5aSGrygorii Strashko 	if (IS_ERR(cpsw->cpts)) {
35418a2c9a5aSGrygorii Strashko 		ret = PTR_ERR(cpsw->cpts);
35421971ab58SGrygorii Strashko 		goto clean_dma_ret;
35438a2c9a5aSGrygorii Strashko 	}
35448a2c9a5aSGrygorii Strashko 
3545c03abd84SFelipe Balbi 	ndev->irq = platform_get_irq(pdev, 1);
3546df828598SMugunthan V N 	if (ndev->irq < 0) {
3547df828598SMugunthan V N 		dev_err(priv->dev, "error getting irq resource\n");
3548c1e3334fSJulia Lawall 		ret = ndev->irq;
35491971ab58SGrygorii Strashko 		goto clean_dma_ret;
3550df828598SMugunthan V N 	}
3551df828598SMugunthan V N 
3552a3a41d2fSGrygorii Strashko 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3553070f9c65SKeerthy 
3554070f9c65SKeerthy 	ndev->netdev_ops = &cpsw_netdev_ops;
3555070f9c65SKeerthy 	ndev->ethtool_ops = &cpsw_ethtool_ops;
35569611d6d6SIvan Khoronzhuk 	netif_napi_add(ndev, &cpsw->napi_rx,
35579611d6d6SIvan Khoronzhuk 		       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
35589611d6d6SIvan Khoronzhuk 		       CPSW_POLL_WEIGHT);
35599611d6d6SIvan Khoronzhuk 	netif_tx_napi_add(ndev, &cpsw->napi_tx,
35609611d6d6SIvan Khoronzhuk 			  cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
35619611d6d6SIvan Khoronzhuk 			  CPSW_POLL_WEIGHT);
3562070f9c65SKeerthy 	cpsw_split_res(ndev);
3563070f9c65SKeerthy 
3564070f9c65SKeerthy 	/* register the network device */
3565070f9c65SKeerthy 	SET_NETDEV_DEV(ndev, &pdev->dev);
3566070f9c65SKeerthy 	ret = register_netdev(ndev);
3567070f9c65SKeerthy 	if (ret) {
3568070f9c65SKeerthy 		dev_err(priv->dev, "error registering net device\n");
3569070f9c65SKeerthy 		ret = -ENODEV;
35701971ab58SGrygorii Strashko 		goto clean_dma_ret;
3571070f9c65SKeerthy 	}
3572070f9c65SKeerthy 
3573070f9c65SKeerthy 	if (cpsw->data.dual_emac) {
3574070f9c65SKeerthy 		ret = cpsw_probe_dual_emac(priv);
3575070f9c65SKeerthy 		if (ret) {
3576070f9c65SKeerthy 			cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
3577070f9c65SKeerthy 			goto clean_unregister_netdev_ret;
3578070f9c65SKeerthy 		}
3579070f9c65SKeerthy 	}
3580070f9c65SKeerthy 
3581c03abd84SFelipe Balbi 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3582c03abd84SFelipe Balbi 	 * MISC IRQs which are always kept disabled with this driver so
3583c03abd84SFelipe Balbi 	 * we will not request them.
3584c03abd84SFelipe Balbi 	 *
3585c03abd84SFelipe Balbi 	 * If anyone wants to implement support for those, make sure to
3586c03abd84SFelipe Balbi 	 * first request and append them to irqs_table array.
3587c03abd84SFelipe Balbi 	 */
3588c2b32e58SDaniel Mack 
3589c03abd84SFelipe Balbi 	/* RX IRQ */
35905087b915SFelipe Balbi 	irq = platform_get_irq(pdev, 1);
3591c1e3334fSJulia Lawall 	if (irq < 0) {
3592c1e3334fSJulia Lawall 		ret = irq;
35931971ab58SGrygorii Strashko 		goto clean_dma_ret;
3594c1e3334fSJulia Lawall 	}
35955087b915SFelipe Balbi 
3596e38b5a3dSIvan Khoronzhuk 	cpsw->irqs_table[0] = irq;
3597c03abd84SFelipe Balbi 	ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
3598dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
35995087b915SFelipe Balbi 	if (ret < 0) {
36005087b915SFelipe Balbi 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
36011971ab58SGrygorii Strashko 		goto clean_dma_ret;
3602df828598SMugunthan V N 	}
3603df828598SMugunthan V N 
3604c03abd84SFelipe Balbi 	/* TX IRQ */
36055087b915SFelipe Balbi 	irq = platform_get_irq(pdev, 2);
3606c1e3334fSJulia Lawall 	if (irq < 0) {
3607c1e3334fSJulia Lawall 		ret = irq;
36081971ab58SGrygorii Strashko 		goto clean_dma_ret;
3609c1e3334fSJulia Lawall 	}
36105087b915SFelipe Balbi 
3611e38b5a3dSIvan Khoronzhuk 	cpsw->irqs_table[1] = irq;
3612c03abd84SFelipe Balbi 	ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
3613dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
36145087b915SFelipe Balbi 	if (ret < 0) {
36155087b915SFelipe Balbi 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
36161971ab58SGrygorii Strashko 		goto clean_dma_ret;
36175087b915SFelipe Balbi 	}
3618c2b32e58SDaniel Mack 
361990225bf0SGrygorii Strashko 	cpsw_notice(priv, probe,
362090225bf0SGrygorii Strashko 		    "initialized device (regs %pa, irq %d, pool size %d)\n",
362190225bf0SGrygorii Strashko 		    &ss_res->start, ndev->irq, dma_params.descs_pool_size);
3622d9ba8f9eSMugunthan V N 
3623c46ab7e0SJohan Hovold 	pm_runtime_put(&pdev->dev);
3624c46ab7e0SJohan Hovold 
3625df828598SMugunthan V N 	return 0;
3626df828598SMugunthan V N 
3627a7fe9d46SJohan Hovold clean_unregister_netdev_ret:
3628a7fe9d46SJohan Hovold 	unregister_netdev(ndev);
3629df828598SMugunthan V N clean_dma_ret:
36302c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
3631a4e32b0dSJohan Hovold clean_dt_ret:
3632a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
3633c46ab7e0SJohan Hovold 	pm_runtime_put_sync(&pdev->dev);
3634aa1a15e2SDaniel Mack clean_runtime_disable_ret:
3635f150bd7fSMugunthan V N 	pm_runtime_disable(&pdev->dev);
3636df828598SMugunthan V N clean_ndev_ret:
3637d1bd9acfSSebastian Siewior 	free_netdev(priv->ndev);
3638df828598SMugunthan V N 	return ret;
3639df828598SMugunthan V N }
3640df828598SMugunthan V N 
3641663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev)
3642df828598SMugunthan V N {
3643df828598SMugunthan V N 	struct net_device *ndev = platform_get_drvdata(pdev);
36442a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
36458a0b6dc9SGrygorii Strashko 	int ret;
36468a0b6dc9SGrygorii Strashko 
36478a0b6dc9SGrygorii Strashko 	ret = pm_runtime_get_sync(&pdev->dev);
36488a0b6dc9SGrygorii Strashko 	if (ret < 0) {
36498a0b6dc9SGrygorii Strashko 		pm_runtime_put_noidle(&pdev->dev);
36508a0b6dc9SGrygorii Strashko 		return ret;
36518a0b6dc9SGrygorii Strashko 	}
3652df828598SMugunthan V N 
3653606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
3654606f3993SIvan Khoronzhuk 		unregister_netdev(cpsw->slaves[1].ndev);
3655d1bd9acfSSebastian Siewior 	unregister_netdev(ndev);
3656df828598SMugunthan V N 
36578a2c9a5aSGrygorii Strashko 	cpts_release(cpsw->cpts);
36582c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
3659a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
36608a0b6dc9SGrygorii Strashko 	pm_runtime_put_sync(&pdev->dev);
36618a0b6dc9SGrygorii Strashko 	pm_runtime_disable(&pdev->dev);
3662606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
3663606f3993SIvan Khoronzhuk 		free_netdev(cpsw->slaves[1].ndev);
3664df828598SMugunthan V N 	free_netdev(ndev);
3665df828598SMugunthan V N 	return 0;
3666df828598SMugunthan V N }
3667df828598SMugunthan V N 
36688963a504SGrygorii Strashko #ifdef CONFIG_PM_SLEEP
3669df828598SMugunthan V N static int cpsw_suspend(struct device *dev)
3670df828598SMugunthan V N {
3671df828598SMugunthan V N 	struct platform_device	*pdev = to_platform_device(dev);
3672df828598SMugunthan V N 	struct net_device	*ndev = platform_get_drvdata(pdev);
3673606f3993SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3674df828598SMugunthan V N 
3675606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
3676618073e3SMugunthan V N 		int i;
3677618073e3SMugunthan V N 
3678606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
3679606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
3680606f3993SIvan Khoronzhuk 				cpsw_ndo_stop(cpsw->slaves[i].ndev);
3681618073e3SMugunthan V N 		}
3682618073e3SMugunthan V N 	} else {
3683df828598SMugunthan V N 		if (netif_running(ndev))
3684df828598SMugunthan V N 			cpsw_ndo_stop(ndev);
3685618073e3SMugunthan V N 	}
36861e7a2e21SDaniel Mack 
3687739683b4SMugunthan V N 	/* Select sleep pin state */
368856e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_sleep_state(dev);
3689739683b4SMugunthan V N 
3690df828598SMugunthan V N 	return 0;
3691df828598SMugunthan V N }
3692df828598SMugunthan V N 
3693df828598SMugunthan V N static int cpsw_resume(struct device *dev)
3694df828598SMugunthan V N {
3695df828598SMugunthan V N 	struct platform_device	*pdev = to_platform_device(dev);
3696df828598SMugunthan V N 	struct net_device	*ndev = platform_get_drvdata(pdev);
3697a60ced99SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3698df828598SMugunthan V N 
3699739683b4SMugunthan V N 	/* Select default pin state */
370056e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_default_state(dev);
3701739683b4SMugunthan V N 
37024ccfd638SGrygorii Strashko 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
37034ccfd638SGrygorii Strashko 	rtnl_lock();
3704606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
3705618073e3SMugunthan V N 		int i;
3706618073e3SMugunthan V N 
3707606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
3708606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
3709606f3993SIvan Khoronzhuk 				cpsw_ndo_open(cpsw->slaves[i].ndev);
3710618073e3SMugunthan V N 		}
3711618073e3SMugunthan V N 	} else {
3712df828598SMugunthan V N 		if (netif_running(ndev))
3713df828598SMugunthan V N 			cpsw_ndo_open(ndev);
3714618073e3SMugunthan V N 	}
37154ccfd638SGrygorii Strashko 	rtnl_unlock();
37164ccfd638SGrygorii Strashko 
3717df828598SMugunthan V N 	return 0;
3718df828598SMugunthan V N }
37198963a504SGrygorii Strashko #endif
3720df828598SMugunthan V N 
37218963a504SGrygorii Strashko static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
3722df828598SMugunthan V N 
3723df828598SMugunthan V N static struct platform_driver cpsw_driver = {
3724df828598SMugunthan V N 	.driver = {
3725df828598SMugunthan V N 		.name	 = "cpsw",
3726df828598SMugunthan V N 		.pm	 = &cpsw_pm_ops,
37271e5c76d4SSachin Kamat 		.of_match_table = cpsw_of_mtable,
3728df828598SMugunthan V N 	},
3729df828598SMugunthan V N 	.probe = cpsw_probe,
3730663e12e6SBill Pemberton 	.remove = cpsw_remove,
3731df828598SMugunthan V N };
3732df828598SMugunthan V N 
37336fb3b6b5SGrygorii Strashko module_platform_driver(cpsw_driver);
3734df828598SMugunthan V N 
3735df828598SMugunthan V N MODULE_LICENSE("GPL");
3736df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3737df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3738df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver");
3739