xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw.c (revision 6c1f0a1f)
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>
42df828598SMugunthan V N 
43dbe34724SMugunthan V N #include "cpsw.h"
44df828598SMugunthan V N #include "cpsw_ale.h"
452e5b38abSRichard Cochran #include "cpts.h"
46df828598SMugunthan V N #include "davinci_cpdma.h"
47df828598SMugunthan V N 
48df828598SMugunthan V N #define CPSW_DEBUG	(NETIF_MSG_HW		| NETIF_MSG_WOL		| \
49df828598SMugunthan V N 			 NETIF_MSG_DRV		| NETIF_MSG_LINK	| \
50df828598SMugunthan V N 			 NETIF_MSG_IFUP		| NETIF_MSG_INTR	| \
51df828598SMugunthan V N 			 NETIF_MSG_PROBE	| NETIF_MSG_TIMER	| \
52df828598SMugunthan V N 			 NETIF_MSG_IFDOWN	| NETIF_MSG_RX_ERR	| \
53df828598SMugunthan V N 			 NETIF_MSG_TX_ERR	| NETIF_MSG_TX_DONE	| \
54df828598SMugunthan V N 			 NETIF_MSG_PKTDATA	| NETIF_MSG_TX_QUEUED	| \
55df828598SMugunthan V N 			 NETIF_MSG_RX_STATUS)
56df828598SMugunthan V N 
57df828598SMugunthan V N #define cpsw_info(priv, type, format, ...)		\
58df828598SMugunthan V N do {								\
59df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
60df828598SMugunthan V N 		dev_info(priv->dev, format, ## __VA_ARGS__);	\
61df828598SMugunthan V N } while (0)
62df828598SMugunthan V N 
63df828598SMugunthan V N #define cpsw_err(priv, type, format, ...)		\
64df828598SMugunthan V N do {								\
65df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
66df828598SMugunthan V N 		dev_err(priv->dev, format, ## __VA_ARGS__);	\
67df828598SMugunthan V N } while (0)
68df828598SMugunthan V N 
69df828598SMugunthan V N #define cpsw_dbg(priv, type, format, ...)		\
70df828598SMugunthan V N do {								\
71df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
72df828598SMugunthan V N 		dev_dbg(priv->dev, format, ## __VA_ARGS__);	\
73df828598SMugunthan V N } while (0)
74df828598SMugunthan V N 
75df828598SMugunthan V N #define cpsw_notice(priv, type, format, ...)		\
76df828598SMugunthan V N do {								\
77df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
78df828598SMugunthan V N 		dev_notice(priv->dev, format, ## __VA_ARGS__);	\
79df828598SMugunthan V N } while (0)
80df828598SMugunthan V N 
815c50a856SMugunthan V N #define ALE_ALL_PORTS		0x7
825c50a856SMugunthan V N 
83df828598SMugunthan V N #define CPSW_MAJOR_VERSION(reg)		(reg >> 8 & 0x7)
84df828598SMugunthan V N #define CPSW_MINOR_VERSION(reg)		(reg & 0xff)
85df828598SMugunthan V N #define CPSW_RTL_VERSION(reg)		((reg >> 11) & 0x1f)
86df828598SMugunthan V N 
87e90cfac6SRichard Cochran #define CPSW_VERSION_1		0x19010a
88e90cfac6SRichard Cochran #define CPSW_VERSION_2		0x19010c
89c193f365SMugunthan V N #define CPSW_VERSION_3		0x19010f
90926489beSMugunthan V N #define CPSW_VERSION_4		0x190112
91549985eeSRichard Cochran 
92549985eeSRichard Cochran #define HOST_PORT_NUM		0
93c6395f12SGrygorii Strashko #define CPSW_ALE_PORTS_NUM	3
94549985eeSRichard Cochran #define SLIVER_SIZE		0x40
95549985eeSRichard Cochran 
96549985eeSRichard Cochran #define CPSW1_HOST_PORT_OFFSET	0x028
97549985eeSRichard Cochran #define CPSW1_SLAVE_OFFSET	0x050
98549985eeSRichard Cochran #define CPSW1_SLAVE_SIZE	0x040
99549985eeSRichard Cochran #define CPSW1_CPDMA_OFFSET	0x100
100549985eeSRichard Cochran #define CPSW1_STATERAM_OFFSET	0x200
101d9718546SMugunthan V N #define CPSW1_HW_STATS		0x400
102549985eeSRichard Cochran #define CPSW1_CPTS_OFFSET	0x500
103549985eeSRichard Cochran #define CPSW1_ALE_OFFSET	0x600
104549985eeSRichard Cochran #define CPSW1_SLIVER_OFFSET	0x700
105549985eeSRichard Cochran 
106549985eeSRichard Cochran #define CPSW2_HOST_PORT_OFFSET	0x108
107549985eeSRichard Cochran #define CPSW2_SLAVE_OFFSET	0x200
108549985eeSRichard Cochran #define CPSW2_SLAVE_SIZE	0x100
109549985eeSRichard Cochran #define CPSW2_CPDMA_OFFSET	0x800
110d9718546SMugunthan V N #define CPSW2_HW_STATS		0x900
111549985eeSRichard Cochran #define CPSW2_STATERAM_OFFSET	0xa00
112549985eeSRichard Cochran #define CPSW2_CPTS_OFFSET	0xc00
113549985eeSRichard Cochran #define CPSW2_ALE_OFFSET	0xd00
114549985eeSRichard Cochran #define CPSW2_SLIVER_OFFSET	0xd80
115549985eeSRichard Cochran #define CPSW2_BD_OFFSET		0x2000
116549985eeSRichard Cochran 
117df828598SMugunthan V N #define CPDMA_RXTHRESH		0x0c0
118df828598SMugunthan V N #define CPDMA_RXFREE		0x0e0
119df828598SMugunthan V N #define CPDMA_TXHDP		0x00
120df828598SMugunthan V N #define CPDMA_RXHDP		0x20
121df828598SMugunthan V N #define CPDMA_TXCP		0x40
122df828598SMugunthan V N #define CPDMA_RXCP		0x60
123df828598SMugunthan V N 
124df828598SMugunthan V N #define CPSW_POLL_WEIGHT	64
125a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_SIZE		4
1269421c901SGrygorii Strashko #define CPSW_MIN_PACKET_SIZE	(VLAN_ETH_ZLEN)
127a3a41d2fSGrygorii Strashko #define CPSW_MAX_PACKET_SIZE	(VLAN_ETH_FRAME_LEN +\
128a3a41d2fSGrygorii Strashko 				 ETH_FCS_LEN +\
129a3a41d2fSGrygorii Strashko 				 CPSW_RX_VLAN_ENCAP_HDR_SIZE)
130df828598SMugunthan V N 
131df828598SMugunthan V N #define RX_PRIORITY_MAPPING	0x76543210
132df828598SMugunthan V N #define TX_PRIORITY_MAPPING	0x33221100
1335e391dc5SIvan Khoronzhuk #define CPDMA_TX_PRIORITY_MAP	0x76543210
134df828598SMugunthan V N 
1353b72c2feSMugunthan V N #define CPSW_VLAN_AWARE		BIT(1)
136a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP	BIT(2)
1373b72c2feSMugunthan V N #define CPSW_ALE_VLAN_AWARE	1
1383b72c2feSMugunthan V N 
13935717d8dSJohn Ogness #define CPSW_FIFO_NORMAL_MODE		(0 << 16)
14035717d8dSJohn Ogness #define CPSW_FIFO_DUAL_MAC_MODE		(1 << 16)
14135717d8dSJohn Ogness #define CPSW_FIFO_RATE_LIMIT_MODE	(2 << 16)
142d9ba8f9eSMugunthan V N 
143ff5b8ef2SMugunthan V N #define CPSW_INTPACEEN		(0x3f << 16)
144ff5b8ef2SMugunthan V N #define CPSW_INTPRESCALE_MASK	(0x7FF << 0)
145ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_CNT	63
146ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_CNT	2
147ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_INTVL	(1000 / CPSW_CMINTMIN_CNT)
148ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_INTVL	((1000 / CPSW_CMINTMAX_CNT) + 1)
149ff5b8ef2SMugunthan V N 
150606f3993SIvan Khoronzhuk #define cpsw_slave_index(cpsw, priv)				\
151606f3993SIvan Khoronzhuk 		((cpsw->data.dual_emac) ? priv->emac_port :	\
152606f3993SIvan Khoronzhuk 		cpsw->data.active_slave)
153e38b5a3dSIvan Khoronzhuk #define IRQ_NUM			2
154e05107e6SIvan Khoronzhuk #define CPSW_MAX_QUEUES		8
15590225bf0SGrygorii Strashko #define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
156d3bb9c58SMugunthan V N 
157a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT	29
158a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK		GENMASK(2, 0)
159a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT	16
160a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT	8
161a3a41d2fSGrygorii Strashko #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK	GENMASK(1, 0)
162a3a41d2fSGrygorii Strashko enum {
163a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0,
164a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV,
165a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG,
166a3a41d2fSGrygorii Strashko 	CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG,
167a3a41d2fSGrygorii Strashko };
168a3a41d2fSGrygorii Strashko 
169df828598SMugunthan V N static int debug_level;
170df828598SMugunthan V N module_param(debug_level, int, 0);
171df828598SMugunthan V N MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
172df828598SMugunthan V N 
173df828598SMugunthan V N static int ale_ageout = 10;
174df828598SMugunthan V N module_param(ale_ageout, int, 0);
175df828598SMugunthan V N MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
176df828598SMugunthan V N 
177df828598SMugunthan V N static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
178df828598SMugunthan V N module_param(rx_packet_max, int, 0);
179df828598SMugunthan V N MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
180df828598SMugunthan V N 
18190225bf0SGrygorii Strashko static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
18290225bf0SGrygorii Strashko module_param(descs_pool_size, int, 0444);
18390225bf0SGrygorii Strashko MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
18490225bf0SGrygorii Strashko 
185996a5c27SRichard Cochran struct cpsw_wr_regs {
186df828598SMugunthan V N 	u32	id_ver;
187df828598SMugunthan V N 	u32	soft_reset;
188df828598SMugunthan V N 	u32	control;
189df828598SMugunthan V N 	u32	int_control;
190df828598SMugunthan V N 	u32	rx_thresh_en;
191df828598SMugunthan V N 	u32	rx_en;
192df828598SMugunthan V N 	u32	tx_en;
193df828598SMugunthan V N 	u32	misc_en;
194ff5b8ef2SMugunthan V N 	u32	mem_allign1[8];
195ff5b8ef2SMugunthan V N 	u32	rx_thresh_stat;
196ff5b8ef2SMugunthan V N 	u32	rx_stat;
197ff5b8ef2SMugunthan V N 	u32	tx_stat;
198ff5b8ef2SMugunthan V N 	u32	misc_stat;
199ff5b8ef2SMugunthan V N 	u32	mem_allign2[8];
200ff5b8ef2SMugunthan V N 	u32	rx_imax;
201ff5b8ef2SMugunthan V N 	u32	tx_imax;
202ff5b8ef2SMugunthan V N 
203df828598SMugunthan V N };
204df828598SMugunthan V N 
205996a5c27SRichard Cochran struct cpsw_ss_regs {
206df828598SMugunthan V N 	u32	id_ver;
207df828598SMugunthan V N 	u32	control;
208df828598SMugunthan V N 	u32	soft_reset;
209df828598SMugunthan V N 	u32	stat_port_en;
210df828598SMugunthan V N 	u32	ptype;
211bd357af2SRichard Cochran 	u32	soft_idle;
212bd357af2SRichard Cochran 	u32	thru_rate;
213bd357af2SRichard Cochran 	u32	gap_thresh;
214bd357af2SRichard Cochran 	u32	tx_start_wds;
215bd357af2SRichard Cochran 	u32	flow_control;
216bd357af2SRichard Cochran 	u32	vlan_ltype;
217bd357af2SRichard Cochran 	u32	ts_ltype;
218bd357af2SRichard Cochran 	u32	dlr_ltype;
219df828598SMugunthan V N };
220df828598SMugunthan V N 
2219750a3adSRichard Cochran /* CPSW_PORT_V1 */
2229750a3adSRichard Cochran #define CPSW1_MAX_BLKS      0x00 /* Maximum FIFO Blocks */
2239750a3adSRichard Cochran #define CPSW1_BLK_CNT       0x04 /* FIFO Block Usage Count (Read Only) */
2249750a3adSRichard Cochran #define CPSW1_TX_IN_CTL     0x08 /* Transmit FIFO Control */
2259750a3adSRichard Cochran #define CPSW1_PORT_VLAN     0x0c /* VLAN Register */
2269750a3adSRichard Cochran #define CPSW1_TX_PRI_MAP    0x10 /* Tx Header Priority to Switch Pri Mapping */
2279750a3adSRichard Cochran #define CPSW1_TS_CTL        0x14 /* Time Sync Control */
2289750a3adSRichard Cochran #define CPSW1_TS_SEQ_LTYPE  0x18 /* Time Sync Sequence ID Offset and Msg Type */
2299750a3adSRichard Cochran #define CPSW1_TS_VLAN       0x1c /* Time Sync VLAN1 and VLAN2 */
2309750a3adSRichard Cochran 
2319750a3adSRichard Cochran /* CPSW_PORT_V2 */
2329750a3adSRichard Cochran #define CPSW2_CONTROL       0x00 /* Control Register */
2339750a3adSRichard Cochran #define CPSW2_MAX_BLKS      0x08 /* Maximum FIFO Blocks */
2349750a3adSRichard Cochran #define CPSW2_BLK_CNT       0x0c /* FIFO Block Usage Count (Read Only) */
2359750a3adSRichard Cochran #define CPSW2_TX_IN_CTL     0x10 /* Transmit FIFO Control */
2369750a3adSRichard Cochran #define CPSW2_PORT_VLAN     0x14 /* VLAN Register */
2379750a3adSRichard Cochran #define CPSW2_TX_PRI_MAP    0x18 /* Tx Header Priority to Switch Pri Mapping */
2389750a3adSRichard Cochran #define CPSW2_TS_SEQ_MTYPE  0x1c /* Time Sync Sequence ID Offset and Msg Type */
2399750a3adSRichard Cochran 
2409750a3adSRichard Cochran /* CPSW_PORT_V1 and V2 */
2419750a3adSRichard Cochran #define SA_LO               0x20 /* CPGMAC_SL Source Address Low */
2429750a3adSRichard Cochran #define SA_HI               0x24 /* CPGMAC_SL Source Address High */
2439750a3adSRichard Cochran #define SEND_PERCENT        0x28 /* Transmit Queue Send Percentages */
2449750a3adSRichard Cochran 
2459750a3adSRichard Cochran /* CPSW_PORT_V2 only */
2469750a3adSRichard Cochran #define RX_DSCP_PRI_MAP0    0x30 /* Rx DSCP Priority to Rx Packet Mapping */
2479750a3adSRichard Cochran #define RX_DSCP_PRI_MAP1    0x34 /* Rx DSCP Priority to Rx Packet Mapping */
2489750a3adSRichard Cochran #define RX_DSCP_PRI_MAP2    0x38 /* Rx DSCP Priority to Rx Packet Mapping */
2499750a3adSRichard Cochran #define RX_DSCP_PRI_MAP3    0x3c /* Rx DSCP Priority to Rx Packet Mapping */
2509750a3adSRichard Cochran #define RX_DSCP_PRI_MAP4    0x40 /* Rx DSCP Priority to Rx Packet Mapping */
2519750a3adSRichard Cochran #define RX_DSCP_PRI_MAP5    0x44 /* Rx DSCP Priority to Rx Packet Mapping */
2529750a3adSRichard Cochran #define RX_DSCP_PRI_MAP6    0x48 /* Rx DSCP Priority to Rx Packet Mapping */
2539750a3adSRichard Cochran #define RX_DSCP_PRI_MAP7    0x4c /* Rx DSCP Priority to Rx Packet Mapping */
2549750a3adSRichard Cochran 
2559750a3adSRichard Cochran /* Bit definitions for the CPSW2_CONTROL register */
2569750a3adSRichard Cochran #define PASS_PRI_TAGGED     (1<<24) /* Pass Priority Tagged */
2579750a3adSRichard Cochran #define VLAN_LTYPE2_EN      (1<<21) /* VLAN LTYPE 2 enable */
2589750a3adSRichard Cochran #define VLAN_LTYPE1_EN      (1<<20) /* VLAN LTYPE 1 enable */
2599750a3adSRichard Cochran #define DSCP_PRI_EN         (1<<16) /* DSCP Priority Enable */
2609750a3adSRichard Cochran #define TS_320              (1<<14) /* Time Sync Dest Port 320 enable */
2619750a3adSRichard Cochran #define TS_319              (1<<13) /* Time Sync Dest Port 319 enable */
2629750a3adSRichard Cochran #define TS_132              (1<<12) /* Time Sync Dest IP Addr 132 enable */
2639750a3adSRichard Cochran #define TS_131              (1<<11) /* Time Sync Dest IP Addr 131 enable */
2649750a3adSRichard Cochran #define TS_130              (1<<10) /* Time Sync Dest IP Addr 130 enable */
2659750a3adSRichard Cochran #define TS_129              (1<<9)  /* Time Sync Dest IP Addr 129 enable */
26609c55372SGeorge Cherian #define TS_TTL_NONZERO      (1<<8)  /* Time Sync Time To Live Non-zero enable */
26709c55372SGeorge Cherian #define TS_ANNEX_F_EN       (1<<6)  /* Time Sync Annex F enable */
2689750a3adSRichard Cochran #define TS_ANNEX_D_EN       (1<<4)  /* Time Sync Annex D enable */
2699750a3adSRichard Cochran #define TS_LTYPE2_EN        (1<<3)  /* Time Sync LTYPE 2 enable */
2709750a3adSRichard Cochran #define TS_LTYPE1_EN        (1<<2)  /* Time Sync LTYPE 1 enable */
2719750a3adSRichard Cochran #define TS_TX_EN            (1<<1)  /* Time Sync Transmit Enable */
2729750a3adSRichard Cochran #define TS_RX_EN            (1<<0)  /* Time Sync Receive Enable */
2739750a3adSRichard Cochran 
27409c55372SGeorge Cherian #define CTRL_V2_TS_BITS \
27509c55372SGeorge Cherian 	(TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
27609c55372SGeorge Cherian 	 TS_TTL_NONZERO  | TS_ANNEX_D_EN | TS_LTYPE1_EN)
2779750a3adSRichard Cochran 
27809c55372SGeorge Cherian #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
27909c55372SGeorge Cherian #define CTRL_V2_TX_TS_BITS  (CTRL_V2_TS_BITS | TS_TX_EN)
28009c55372SGeorge Cherian #define CTRL_V2_RX_TS_BITS  (CTRL_V2_TS_BITS | TS_RX_EN)
28109c55372SGeorge Cherian 
28209c55372SGeorge Cherian 
28309c55372SGeorge Cherian #define CTRL_V3_TS_BITS \
28409c55372SGeorge Cherian 	(TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
28509c55372SGeorge Cherian 	 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
28609c55372SGeorge Cherian 	 TS_LTYPE1_EN)
28709c55372SGeorge Cherian 
28809c55372SGeorge Cherian #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
28909c55372SGeorge Cherian #define CTRL_V3_TX_TS_BITS  (CTRL_V3_TS_BITS | TS_TX_EN)
29009c55372SGeorge Cherian #define CTRL_V3_RX_TS_BITS  (CTRL_V3_TS_BITS | TS_RX_EN)
2919750a3adSRichard Cochran 
2929750a3adSRichard Cochran /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
2939750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_SHIFT   (16)    /* Time Sync Sequence ID Offset */
2949750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_MASK    (0x3f)
2959750a3adSRichard Cochran #define TS_MSG_TYPE_EN_SHIFT     (0)     /* Time Sync Message Type Enable */
2969750a3adSRichard Cochran #define TS_MSG_TYPE_EN_MASK      (0xffff)
2979750a3adSRichard Cochran 
2989750a3adSRichard Cochran /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
2999750a3adSRichard Cochran #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
300df828598SMugunthan V N 
3012e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_CTL register */
3022e5b38abSRichard Cochran #define CPSW_V1_TS_RX_EN		BIT(0)
3032e5b38abSRichard Cochran #define CPSW_V1_TS_TX_EN		BIT(4)
3042e5b38abSRichard Cochran #define CPSW_V1_MSG_TYPE_OFS		16
3052e5b38abSRichard Cochran 
3062e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
3072e5b38abSRichard Cochran #define CPSW_V1_SEQ_ID_OFS_SHIFT	16
3082e5b38abSRichard Cochran 
30948f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_TX		15
31048f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_TX_SHIFT		4
31148f5bcccSGrygorii Strashko #define CPSW_MAX_BLKS_RX		5
31248f5bcccSGrygorii Strashko 
313df828598SMugunthan V N struct cpsw_host_regs {
314df828598SMugunthan V N 	u32	max_blks;
315df828598SMugunthan V N 	u32	blk_cnt;
316d9ba8f9eSMugunthan V N 	u32	tx_in_ctl;
317df828598SMugunthan V N 	u32	port_vlan;
318df828598SMugunthan V N 	u32	tx_pri_map;
319df828598SMugunthan V N 	u32	cpdma_tx_pri_map;
320df828598SMugunthan V N 	u32	cpdma_rx_chan_map;
321df828598SMugunthan V N };
322df828598SMugunthan V N 
323df828598SMugunthan V N struct cpsw_sliver_regs {
324df828598SMugunthan V N 	u32	id_ver;
325df828598SMugunthan V N 	u32	mac_control;
326df828598SMugunthan V N 	u32	mac_status;
327df828598SMugunthan V N 	u32	soft_reset;
328df828598SMugunthan V N 	u32	rx_maxlen;
329df828598SMugunthan V N 	u32	__reserved_0;
330df828598SMugunthan V N 	u32	rx_pause;
331df828598SMugunthan V N 	u32	tx_pause;
332df828598SMugunthan V N 	u32	__reserved_1;
333df828598SMugunthan V N 	u32	rx_pri_map;
334df828598SMugunthan V N };
335df828598SMugunthan V N 
336d9718546SMugunthan V N struct cpsw_hw_stats {
337d9718546SMugunthan V N 	u32	rxgoodframes;
338d9718546SMugunthan V N 	u32	rxbroadcastframes;
339d9718546SMugunthan V N 	u32	rxmulticastframes;
340d9718546SMugunthan V N 	u32	rxpauseframes;
341d9718546SMugunthan V N 	u32	rxcrcerrors;
342d9718546SMugunthan V N 	u32	rxaligncodeerrors;
343d9718546SMugunthan V N 	u32	rxoversizedframes;
344d9718546SMugunthan V N 	u32	rxjabberframes;
345d9718546SMugunthan V N 	u32	rxundersizedframes;
346d9718546SMugunthan V N 	u32	rxfragments;
347d9718546SMugunthan V N 	u32	__pad_0[2];
348d9718546SMugunthan V N 	u32	rxoctets;
349d9718546SMugunthan V N 	u32	txgoodframes;
350d9718546SMugunthan V N 	u32	txbroadcastframes;
351d9718546SMugunthan V N 	u32	txmulticastframes;
352d9718546SMugunthan V N 	u32	txpauseframes;
353d9718546SMugunthan V N 	u32	txdeferredframes;
354d9718546SMugunthan V N 	u32	txcollisionframes;
355d9718546SMugunthan V N 	u32	txsinglecollframes;
356d9718546SMugunthan V N 	u32	txmultcollframes;
357d9718546SMugunthan V N 	u32	txexcessivecollisions;
358d9718546SMugunthan V N 	u32	txlatecollisions;
359d9718546SMugunthan V N 	u32	txunderrun;
360d9718546SMugunthan V N 	u32	txcarriersenseerrors;
361d9718546SMugunthan V N 	u32	txoctets;
362d9718546SMugunthan V N 	u32	octetframes64;
363d9718546SMugunthan V N 	u32	octetframes65t127;
364d9718546SMugunthan V N 	u32	octetframes128t255;
365d9718546SMugunthan V N 	u32	octetframes256t511;
366d9718546SMugunthan V N 	u32	octetframes512t1023;
367d9718546SMugunthan V N 	u32	octetframes1024tup;
368d9718546SMugunthan V N 	u32	netoctets;
369d9718546SMugunthan V N 	u32	rxsofoverruns;
370d9718546SMugunthan V N 	u32	rxmofoverruns;
371d9718546SMugunthan V N 	u32	rxdmaoverruns;
372d9718546SMugunthan V N };
373d9718546SMugunthan V N 
3742c8a14d6SGrygorii Strashko struct cpsw_slave_data {
3752c8a14d6SGrygorii Strashko 	struct device_node *phy_node;
3762c8a14d6SGrygorii Strashko 	char		phy_id[MII_BUS_ID_SIZE];
3772c8a14d6SGrygorii Strashko 	int		phy_if;
3782c8a14d6SGrygorii Strashko 	u8		mac_addr[ETH_ALEN];
3792c8a14d6SGrygorii Strashko 	u16		dual_emac_res_vlan;	/* Reserved VLAN for DualEMAC */
3802c8a14d6SGrygorii Strashko };
3812c8a14d6SGrygorii Strashko 
3822c8a14d6SGrygorii Strashko struct cpsw_platform_data {
3832c8a14d6SGrygorii Strashko 	struct cpsw_slave_data	*slave_data;
3842c8a14d6SGrygorii Strashko 	u32	ss_reg_ofs;	/* Subsystem control register offset */
3852c8a14d6SGrygorii Strashko 	u32	channels;	/* number of cpdma channels (symmetric) */
3862c8a14d6SGrygorii Strashko 	u32	slaves;		/* number of slave cpgmac ports */
3872c8a14d6SGrygorii Strashko 	u32	active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
3882c8a14d6SGrygorii Strashko 	u32	ale_entries;	/* ale table size */
3892c8a14d6SGrygorii Strashko 	u32	bd_ram_size;  /*buffer descriptor ram size */
3902c8a14d6SGrygorii Strashko 	u32	mac_control;	/* Mac control register */
3912c8a14d6SGrygorii Strashko 	u16	default_vlan;	/* Def VLAN for ALE lookup in VLAN aware mode*/
3922c8a14d6SGrygorii Strashko 	bool	dual_emac;	/* Enable Dual EMAC mode */
3932c8a14d6SGrygorii Strashko };
3942c8a14d6SGrygorii Strashko 
395df828598SMugunthan V N struct cpsw_slave {
3969750a3adSRichard Cochran 	void __iomem			*regs;
397df828598SMugunthan V N 	struct cpsw_sliver_regs __iomem	*sliver;
398df828598SMugunthan V N 	int				slave_num;
399df828598SMugunthan V N 	u32				mac_control;
400df828598SMugunthan V N 	struct cpsw_slave_data		*data;
401df828598SMugunthan V N 	struct phy_device		*phy;
402d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
403d9ba8f9eSMugunthan V N 	u32				port_vlan;
404df828598SMugunthan V N };
405df828598SMugunthan V N 
4069750a3adSRichard Cochran static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
4079750a3adSRichard Cochran {
408dda5f5feSGrygorii Strashko 	return readl_relaxed(slave->regs + offset);
4099750a3adSRichard Cochran }
4109750a3adSRichard Cochran 
4119750a3adSRichard Cochran static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
4129750a3adSRichard Cochran {
413dda5f5feSGrygorii Strashko 	writel_relaxed(val, slave->regs + offset);
4149750a3adSRichard Cochran }
4159750a3adSRichard Cochran 
4168feb0a19SIvan Khoronzhuk struct cpsw_vector {
4178feb0a19SIvan Khoronzhuk 	struct cpdma_chan *ch;
4188feb0a19SIvan Khoronzhuk 	int budget;
4198feb0a19SIvan Khoronzhuk };
4208feb0a19SIvan Khoronzhuk 
421649a1688SIvan Khoronzhuk struct cpsw_common {
42256e31bd8SIvan Khoronzhuk 	struct device			*dev;
423606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	data;
424dbc4ec52SIvan Khoronzhuk 	struct napi_struct		napi_rx;
425dbc4ec52SIvan Khoronzhuk 	struct napi_struct		napi_tx;
4265d8d0d4dSIvan Khoronzhuk 	struct cpsw_ss_regs __iomem	*regs;
4275d8d0d4dSIvan Khoronzhuk 	struct cpsw_wr_regs __iomem	*wr_regs;
4285d8d0d4dSIvan Khoronzhuk 	u8 __iomem			*hw_stats;
4295d8d0d4dSIvan Khoronzhuk 	struct cpsw_host_regs __iomem	*host_port_regs;
4302a05a622SIvan Khoronzhuk 	u32				version;
4312a05a622SIvan Khoronzhuk 	u32				coal_intvl;
4322a05a622SIvan Khoronzhuk 	u32				bus_freq_mhz;
4332a05a622SIvan Khoronzhuk 	int				rx_packet_max;
434606f3993SIvan Khoronzhuk 	struct cpsw_slave		*slaves;
4352c836bd9SIvan Khoronzhuk 	struct cpdma_ctlr		*dma;
4368feb0a19SIvan Khoronzhuk 	struct cpsw_vector		txv[CPSW_MAX_QUEUES];
4378feb0a19SIvan Khoronzhuk 	struct cpsw_vector		rxv[CPSW_MAX_QUEUES];
4382a05a622SIvan Khoronzhuk 	struct cpsw_ale			*ale;
439e38b5a3dSIvan Khoronzhuk 	bool				quirk_irq;
440e38b5a3dSIvan Khoronzhuk 	bool				rx_irq_disabled;
441e38b5a3dSIvan Khoronzhuk 	bool				tx_irq_disabled;
442e38b5a3dSIvan Khoronzhuk 	u32 irqs_table[IRQ_NUM];
4432a05a622SIvan Khoronzhuk 	struct cpts			*cpts;
444e05107e6SIvan Khoronzhuk 	int				rx_ch_num, tx_ch_num;
4450be01b8eSIvan Khoronzhuk 	int				speed;
446d5bc1613SIvan Khoronzhuk 	int				usage_count;
447649a1688SIvan Khoronzhuk };
448649a1688SIvan Khoronzhuk 
449649a1688SIvan Khoronzhuk struct cpsw_priv {
450df828598SMugunthan V N 	struct net_device		*ndev;
451df828598SMugunthan V N 	struct device			*dev;
452df828598SMugunthan V N 	u32				msg_enable;
453df828598SMugunthan V N 	u8				mac_addr[ETH_ALEN];
4541923d6e4SMugunthan V N 	bool				rx_pause;
4551923d6e4SMugunthan V N 	bool				tx_pause;
456d9ba8f9eSMugunthan V N 	u32 emac_port;
457649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw;
458df828598SMugunthan V N };
459df828598SMugunthan V N 
460d9718546SMugunthan V N struct cpsw_stats {
461d9718546SMugunthan V N 	char stat_string[ETH_GSTRING_LEN];
462d9718546SMugunthan V N 	int type;
463d9718546SMugunthan V N 	int sizeof_stat;
464d9718546SMugunthan V N 	int stat_offset;
465d9718546SMugunthan V N };
466d9718546SMugunthan V N 
467d9718546SMugunthan V N enum {
468d9718546SMugunthan V N 	CPSW_STATS,
469d9718546SMugunthan V N 	CPDMA_RX_STATS,
470d9718546SMugunthan V N 	CPDMA_TX_STATS,
471d9718546SMugunthan V N };
472d9718546SMugunthan V N 
473d9718546SMugunthan V N #define CPSW_STAT(m)		CPSW_STATS,				\
474d9718546SMugunthan V N 				sizeof(((struct cpsw_hw_stats *)0)->m), \
475d9718546SMugunthan V N 				offsetof(struct cpsw_hw_stats, m)
476d9718546SMugunthan V N #define CPDMA_RX_STAT(m)	CPDMA_RX_STATS,				   \
477d9718546SMugunthan V N 				sizeof(((struct cpdma_chan_stats *)0)->m), \
478d9718546SMugunthan V N 				offsetof(struct cpdma_chan_stats, m)
479d9718546SMugunthan V N #define CPDMA_TX_STAT(m)	CPDMA_TX_STATS,				   \
480d9718546SMugunthan V N 				sizeof(((struct cpdma_chan_stats *)0)->m), \
481d9718546SMugunthan V N 				offsetof(struct cpdma_chan_stats, m)
482d9718546SMugunthan V N 
483d9718546SMugunthan V N static const struct cpsw_stats cpsw_gstrings_stats[] = {
484d9718546SMugunthan V N 	{ "Good Rx Frames", CPSW_STAT(rxgoodframes) },
485d9718546SMugunthan V N 	{ "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
486d9718546SMugunthan V N 	{ "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
487d9718546SMugunthan V N 	{ "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
488d9718546SMugunthan V N 	{ "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
489d9718546SMugunthan V N 	{ "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
490d9718546SMugunthan V N 	{ "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
491d9718546SMugunthan V N 	{ "Rx Jabbers", CPSW_STAT(rxjabberframes) },
492d9718546SMugunthan V N 	{ "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
493d9718546SMugunthan V N 	{ "Rx Fragments", CPSW_STAT(rxfragments) },
494d9718546SMugunthan V N 	{ "Rx Octets", CPSW_STAT(rxoctets) },
495d9718546SMugunthan V N 	{ "Good Tx Frames", CPSW_STAT(txgoodframes) },
496d9718546SMugunthan V N 	{ "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
497d9718546SMugunthan V N 	{ "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
498d9718546SMugunthan V N 	{ "Pause Tx Frames", CPSW_STAT(txpauseframes) },
499d9718546SMugunthan V N 	{ "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
500d9718546SMugunthan V N 	{ "Collisions", CPSW_STAT(txcollisionframes) },
501d9718546SMugunthan V N 	{ "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
502d9718546SMugunthan V N 	{ "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
503d9718546SMugunthan V N 	{ "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
504d9718546SMugunthan V N 	{ "Late Collisions", CPSW_STAT(txlatecollisions) },
505d9718546SMugunthan V N 	{ "Tx Underrun", CPSW_STAT(txunderrun) },
506d9718546SMugunthan V N 	{ "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
507d9718546SMugunthan V N 	{ "Tx Octets", CPSW_STAT(txoctets) },
508d9718546SMugunthan V N 	{ "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
509d9718546SMugunthan V N 	{ "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
510d9718546SMugunthan V N 	{ "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
511d9718546SMugunthan V N 	{ "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
512d9718546SMugunthan V N 	{ "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
513d9718546SMugunthan V N 	{ "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
514d9718546SMugunthan V N 	{ "Net Octets", CPSW_STAT(netoctets) },
515d9718546SMugunthan V N 	{ "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
516d9718546SMugunthan V N 	{ "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
517d9718546SMugunthan V N 	{ "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
518d9718546SMugunthan V N };
519d9718546SMugunthan V N 
520e05107e6SIvan Khoronzhuk static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
521e05107e6SIvan Khoronzhuk 	{ "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
522e05107e6SIvan Khoronzhuk 	{ "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
523e05107e6SIvan Khoronzhuk 	{ "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
524e05107e6SIvan Khoronzhuk 	{ "misqueued", CPDMA_RX_STAT(misqueued) },
525e05107e6SIvan Khoronzhuk 	{ "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
526e05107e6SIvan Khoronzhuk 	{ "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
527e05107e6SIvan Khoronzhuk 	{ "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
528e05107e6SIvan Khoronzhuk 	{ "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
529e05107e6SIvan Khoronzhuk 	{ "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
530e05107e6SIvan Khoronzhuk 	{ "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
531e05107e6SIvan Khoronzhuk 	{ "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
532e05107e6SIvan Khoronzhuk 	{ "requeue", CPDMA_RX_STAT(requeue) },
533e05107e6SIvan Khoronzhuk 	{ "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
534e05107e6SIvan Khoronzhuk };
535e05107e6SIvan Khoronzhuk 
536e05107e6SIvan Khoronzhuk #define CPSW_STATS_COMMON_LEN	ARRAY_SIZE(cpsw_gstrings_stats)
537e05107e6SIvan Khoronzhuk #define CPSW_STATS_CH_LEN	ARRAY_SIZE(cpsw_gstrings_ch_stats)
538d9718546SMugunthan V N 
539649a1688SIvan Khoronzhuk #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
540dbc4ec52SIvan Khoronzhuk #define napi_to_cpsw(napi)	container_of(napi, struct cpsw_common, napi)
541df828598SMugunthan V N #define for_each_slave(priv, func, arg...)				\
542df828598SMugunthan V N 	do {								\
5436e6ceaedSSebastian Siewior 		struct cpsw_slave *slave;				\
544606f3993SIvan Khoronzhuk 		struct cpsw_common *cpsw = (priv)->cpsw;		\
5456e6ceaedSSebastian Siewior 		int n;							\
546606f3993SIvan Khoronzhuk 		if (cpsw->data.dual_emac)				\
547606f3993SIvan Khoronzhuk 			(func)((cpsw)->slaves + priv->emac_port, ##arg);\
548d9ba8f9eSMugunthan V N 		else							\
549606f3993SIvan Khoronzhuk 			for (n = cpsw->data.slaves,			\
550606f3993SIvan Khoronzhuk 					slave = cpsw->slaves;		\
5516e6ceaedSSebastian Siewior 					n; n--)				\
5526e6ceaedSSebastian Siewior 				(func)(slave++, ##arg);			\
553df828598SMugunthan V N 	} while (0)
554d9ba8f9eSMugunthan V N 
5552a05a622SIvan Khoronzhuk #define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb)		\
556d9ba8f9eSMugunthan V N 	do {								\
557606f3993SIvan Khoronzhuk 		if (!cpsw->data.dual_emac)				\
558d9ba8f9eSMugunthan V N 			break;						\
559d9ba8f9eSMugunthan V N 		if (CPDMA_RX_SOURCE_PORT(status) == 1) {		\
560606f3993SIvan Khoronzhuk 			ndev = cpsw->slaves[0].ndev;			\
561d9ba8f9eSMugunthan V N 			skb->dev = ndev;				\
562d9ba8f9eSMugunthan V N 		} else if (CPDMA_RX_SOURCE_PORT(status) == 2) {		\
563606f3993SIvan Khoronzhuk 			ndev = cpsw->slaves[1].ndev;			\
564d9ba8f9eSMugunthan V N 			skb->dev = ndev;				\
565d9ba8f9eSMugunthan V N 		}							\
566d9ba8f9eSMugunthan V N 	} while (0)
567606f3993SIvan Khoronzhuk #define cpsw_add_mcast(cpsw, priv, addr)				\
568d9ba8f9eSMugunthan V N 	do {								\
569606f3993SIvan Khoronzhuk 		if (cpsw->data.dual_emac) {				\
570606f3993SIvan Khoronzhuk 			struct cpsw_slave *slave = cpsw->slaves +	\
571d9ba8f9eSMugunthan V N 						priv->emac_port;	\
5726f1f5836SIvan Khoronzhuk 			int slave_port = cpsw_get_slave_port(		\
573d9ba8f9eSMugunthan V N 						slave->slave_num);	\
5742a05a622SIvan Khoronzhuk 			cpsw_ale_add_mcast(cpsw->ale, addr,		\
57571a2cbb7SGrygorii Strashko 				1 << slave_port | ALE_PORT_HOST,	\
576d9ba8f9eSMugunthan V N 				ALE_VLAN, slave->port_vlan, 0);		\
577d9ba8f9eSMugunthan V N 		} else {						\
5782a05a622SIvan Khoronzhuk 			cpsw_ale_add_mcast(cpsw->ale, addr,		\
57961f1cef9SGrygorii Strashko 				ALE_ALL_PORTS,				\
580d9ba8f9eSMugunthan V N 				0, 0, 0);				\
581d9ba8f9eSMugunthan V N 		}							\
582d9ba8f9eSMugunthan V N 	} while (0)
583d9ba8f9eSMugunthan V N 
5846f1f5836SIvan Khoronzhuk static inline int cpsw_get_slave_port(u32 slave_num)
585d9ba8f9eSMugunthan V N {
586d9ba8f9eSMugunthan V N 	return slave_num + 1;
587d9ba8f9eSMugunthan V N }
588df828598SMugunthan V N 
5890cd8f9ccSMugunthan V N static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
5900cd8f9ccSMugunthan V N {
5912a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
5922a05a622SIvan Khoronzhuk 	struct cpsw_ale *ale = cpsw->ale;
5930cd8f9ccSMugunthan V N 	int i;
5940cd8f9ccSMugunthan V N 
595606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
5960cd8f9ccSMugunthan V N 		bool flag = false;
5970cd8f9ccSMugunthan V N 
5980cd8f9ccSMugunthan V N 		/* Enabling promiscuous mode for one interface will be
5990cd8f9ccSMugunthan V N 		 * common for both the interface as the interface shares
6000cd8f9ccSMugunthan V N 		 * the same hardware resource.
6010cd8f9ccSMugunthan V N 		 */
602606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++)
603606f3993SIvan Khoronzhuk 			if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
6040cd8f9ccSMugunthan V N 				flag = true;
6050cd8f9ccSMugunthan V N 
6060cd8f9ccSMugunthan V N 		if (!enable && flag) {
6070cd8f9ccSMugunthan V N 			enable = true;
6080cd8f9ccSMugunthan V N 			dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
6090cd8f9ccSMugunthan V N 		}
6100cd8f9ccSMugunthan V N 
6110cd8f9ccSMugunthan V N 		if (enable) {
6120cd8f9ccSMugunthan V N 			/* Enable Bypass */
6130cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
6140cd8f9ccSMugunthan V N 
6150cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
6160cd8f9ccSMugunthan V N 		} else {
6170cd8f9ccSMugunthan V N 			/* Disable Bypass */
6180cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
6190cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
6200cd8f9ccSMugunthan V N 		}
6210cd8f9ccSMugunthan V N 	} else {
6220cd8f9ccSMugunthan V N 		if (enable) {
6230cd8f9ccSMugunthan V N 			unsigned long timeout = jiffies + HZ;
6240cd8f9ccSMugunthan V N 
6256f979eb3SLennart Sorensen 			/* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
626606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
6270cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6280cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 1);
6290cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6300cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 1);
6310cd8f9ccSMugunthan V N 			}
6320cd8f9ccSMugunthan V N 
6330cd8f9ccSMugunthan V N 			/* Clear All Untouched entries */
6340cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
6350cd8f9ccSMugunthan V N 			do {
6360cd8f9ccSMugunthan V N 				cpu_relax();
6370cd8f9ccSMugunthan V N 				if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
6380cd8f9ccSMugunthan V N 					break;
6390cd8f9ccSMugunthan V N 			} while (time_after(timeout, jiffies));
6400cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
6410cd8f9ccSMugunthan V N 
6420cd8f9ccSMugunthan V N 			/* Clear all mcast from ALE */
64361f1cef9SGrygorii Strashko 			cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
6440cd8f9ccSMugunthan V N 
6450cd8f9ccSMugunthan V N 			/* Flood All Unicast Packets to Host port */
6460cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
6470cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
6480cd8f9ccSMugunthan V N 		} else {
6496f979eb3SLennart Sorensen 			/* Don't Flood All Unicast Packets to Host port */
6500cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
6510cd8f9ccSMugunthan V N 
6526f979eb3SLennart Sorensen 			/* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
653606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
6540cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6550cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 0);
6560cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
6570cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 0);
6580cd8f9ccSMugunthan V N 			}
6590cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
6600cd8f9ccSMugunthan V N 		}
6610cd8f9ccSMugunthan V N 	}
6620cd8f9ccSMugunthan V N }
6630cd8f9ccSMugunthan V N 
6645c50a856SMugunthan V N static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
6655c50a856SMugunthan V N {
6665c50a856SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
667606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
66825906052SMugunthan V N 	int vid;
66925906052SMugunthan V N 
670606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
671606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
67225906052SMugunthan V N 	else
673606f3993SIvan Khoronzhuk 		vid = cpsw->data.default_vlan;
6745c50a856SMugunthan V N 
6755c50a856SMugunthan V N 	if (ndev->flags & IFF_PROMISC) {
6765c50a856SMugunthan V N 		/* Enable promiscuous mode */
6770cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, true);
6782a05a622SIvan Khoronzhuk 		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
6795c50a856SMugunthan V N 		return;
6800cd8f9ccSMugunthan V N 	} else {
6810cd8f9ccSMugunthan V N 		/* Disable promiscuous mode */
6820cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, false);
6835c50a856SMugunthan V N 	}
6845c50a856SMugunthan V N 
6851e5c4bc4SLennart Sorensen 	/* Restore allmulti on vlans if necessary */
6862a05a622SIvan Khoronzhuk 	cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
6871e5c4bc4SLennart Sorensen 
6885c50a856SMugunthan V N 	/* Clear all mcast from ALE */
6892a05a622SIvan Khoronzhuk 	cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
6905c50a856SMugunthan V N 
6915c50a856SMugunthan V N 	if (!netdev_mc_empty(ndev)) {
6925c50a856SMugunthan V N 		struct netdev_hw_addr *ha;
6935c50a856SMugunthan V N 
6945c50a856SMugunthan V N 		/* program multicast address list into ALE register */
6955c50a856SMugunthan V N 		netdev_for_each_mc_addr(ha, ndev) {
696606f3993SIvan Khoronzhuk 			cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
6975c50a856SMugunthan V N 		}
6985c50a856SMugunthan V N 	}
6995c50a856SMugunthan V N }
7005c50a856SMugunthan V N 
7012c836bd9SIvan Khoronzhuk static void cpsw_intr_enable(struct cpsw_common *cpsw)
702df828598SMugunthan V N {
703dda5f5feSGrygorii Strashko 	writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
704dda5f5feSGrygorii Strashko 	writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
705df828598SMugunthan V N 
7062c836bd9SIvan Khoronzhuk 	cpdma_ctlr_int_ctrl(cpsw->dma, true);
707df828598SMugunthan V N 	return;
708df828598SMugunthan V N }
709df828598SMugunthan V N 
7102c836bd9SIvan Khoronzhuk static void cpsw_intr_disable(struct cpsw_common *cpsw)
711df828598SMugunthan V N {
712dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->wr_regs->tx_en);
713dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->wr_regs->rx_en);
714df828598SMugunthan V N 
7152c836bd9SIvan Khoronzhuk 	cpdma_ctlr_int_ctrl(cpsw->dma, false);
716df828598SMugunthan V N 	return;
717df828598SMugunthan V N }
718df828598SMugunthan V N 
7191a3b5056SOlof Johansson static void cpsw_tx_handler(void *token, int len, int status)
720df828598SMugunthan V N {
721e05107e6SIvan Khoronzhuk 	struct netdev_queue	*txq;
722df828598SMugunthan V N 	struct sk_buff		*skb = token;
723df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
7242a05a622SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
725df828598SMugunthan V N 
726fae50823SMugunthan V N 	/* Check whether the queue is stopped due to stalled tx dma, if the
727fae50823SMugunthan V N 	 * queue is stopped then start the queue as we have free desc for tx
728fae50823SMugunthan V N 	 */
729e05107e6SIvan Khoronzhuk 	txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
730e05107e6SIvan Khoronzhuk 	if (unlikely(netif_tx_queue_stopped(txq)))
731e05107e6SIvan Khoronzhuk 		netif_tx_wake_queue(txq);
732e05107e6SIvan Khoronzhuk 
7332a05a622SIvan Khoronzhuk 	cpts_tx_timestamp(cpsw->cpts, skb);
7348dc43ddcSTobias Klauser 	ndev->stats.tx_packets++;
7358dc43ddcSTobias Klauser 	ndev->stats.tx_bytes += len;
736df828598SMugunthan V N 	dev_kfree_skb_any(skb);
737df828598SMugunthan V N }
738df828598SMugunthan V N 
739a3a41d2fSGrygorii Strashko static void cpsw_rx_vlan_encap(struct sk_buff *skb)
740a3a41d2fSGrygorii Strashko {
741a3a41d2fSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(skb->dev);
742a3a41d2fSGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
743a3a41d2fSGrygorii Strashko 	u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
744a3a41d2fSGrygorii Strashko 	u16 vtag, vid, prio, pkt_type;
745a3a41d2fSGrygorii Strashko 
746a3a41d2fSGrygorii Strashko 	/* Remove VLAN header encapsulation word */
747a3a41d2fSGrygorii Strashko 	skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
748a3a41d2fSGrygorii Strashko 
749a3a41d2fSGrygorii Strashko 	pkt_type = (rx_vlan_encap_hdr >>
750a3a41d2fSGrygorii Strashko 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
751a3a41d2fSGrygorii Strashko 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
752a3a41d2fSGrygorii Strashko 	/* Ignore unknown & Priority-tagged packets*/
753a3a41d2fSGrygorii Strashko 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
754a3a41d2fSGrygorii Strashko 	    pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
755a3a41d2fSGrygorii Strashko 		return;
756a3a41d2fSGrygorii Strashko 
757a3a41d2fSGrygorii Strashko 	vid = (rx_vlan_encap_hdr >>
758a3a41d2fSGrygorii Strashko 	       CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
759a3a41d2fSGrygorii Strashko 	       VLAN_VID_MASK;
760a3a41d2fSGrygorii Strashko 	/* Ignore vid 0 and pass packet as is */
761a3a41d2fSGrygorii Strashko 	if (!vid)
762a3a41d2fSGrygorii Strashko 		return;
763a3a41d2fSGrygorii Strashko 	/* Ignore default vlans in dual mac mode */
764a3a41d2fSGrygorii Strashko 	if (cpsw->data.dual_emac &&
765a3a41d2fSGrygorii Strashko 	    vid == cpsw->slaves[priv->emac_port].port_vlan)
766a3a41d2fSGrygorii Strashko 		return;
767a3a41d2fSGrygorii Strashko 
768a3a41d2fSGrygorii Strashko 	prio = (rx_vlan_encap_hdr >>
769a3a41d2fSGrygorii Strashko 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
770a3a41d2fSGrygorii Strashko 		CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
771a3a41d2fSGrygorii Strashko 
772a3a41d2fSGrygorii Strashko 	vtag = (prio << VLAN_PRIO_SHIFT) | vid;
773a3a41d2fSGrygorii Strashko 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
774a3a41d2fSGrygorii Strashko 
775a3a41d2fSGrygorii Strashko 	/* strip vlan tag for VLAN-tagged packet */
776a3a41d2fSGrygorii Strashko 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
777a3a41d2fSGrygorii Strashko 		memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
778a3a41d2fSGrygorii Strashko 		skb_pull(skb, VLAN_HLEN);
779a3a41d2fSGrygorii Strashko 	}
780a3a41d2fSGrygorii Strashko }
781a3a41d2fSGrygorii Strashko 
7821a3b5056SOlof Johansson static void cpsw_rx_handler(void *token, int len, int status)
783df828598SMugunthan V N {
784e05107e6SIvan Khoronzhuk 	struct cpdma_chan	*ch;
785df828598SMugunthan V N 	struct sk_buff		*skb = token;
786b4727e69SSebastian Siewior 	struct sk_buff		*new_skb;
787df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
788df828598SMugunthan V N 	int			ret = 0;
7892a05a622SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
790df828598SMugunthan V N 
7912a05a622SIvan Khoronzhuk 	cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
792d9ba8f9eSMugunthan V N 
79316e5c57dSMugunthan V N 	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
794a0e2c822SMugunthan V N 		/* In dual emac mode check for all interfaces */
795d5bc1613SIvan Khoronzhuk 		if (cpsw->data.dual_emac && cpsw->usage_count &&
796fe734d0aSIvan Khoronzhuk 		    (status >= 0)) {
797a0e2c822SMugunthan V N 			/* The packet received is for the interface which
798a0e2c822SMugunthan V N 			 * is already down and the other interface is up
799dbedd44eSJoe Perches 			 * and running, instead of freeing which results
800a0e2c822SMugunthan V N 			 * in reducing of the number of rx descriptor in
801a0e2c822SMugunthan V N 			 * DMA engine, requeue skb back to cpdma.
802a0e2c822SMugunthan V N 			 */
803a0e2c822SMugunthan V N 			new_skb = skb;
804a0e2c822SMugunthan V N 			goto requeue;
805a0e2c822SMugunthan V N 		}
806a0e2c822SMugunthan V N 
807b4727e69SSebastian Siewior 		/* the interface is going down, skbs are purged */
808df828598SMugunthan V N 		dev_kfree_skb_any(skb);
809df828598SMugunthan V N 		return;
810df828598SMugunthan V N 	}
811b4727e69SSebastian Siewior 
8122a05a622SIvan Khoronzhuk 	new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
813b4727e69SSebastian Siewior 	if (new_skb) {
814e05107e6SIvan Khoronzhuk 		skb_copy_queue_mapping(new_skb, skb);
815df828598SMugunthan V N 		skb_put(skb, len);
816a3a41d2fSGrygorii Strashko 		if (status & CPDMA_RX_VLAN_ENCAP)
817a3a41d2fSGrygorii Strashko 			cpsw_rx_vlan_encap(skb);
8182a05a622SIvan Khoronzhuk 		cpts_rx_timestamp(cpsw->cpts, skb);
819df828598SMugunthan V N 		skb->protocol = eth_type_trans(skb, ndev);
820df828598SMugunthan V N 		netif_receive_skb(skb);
8218dc43ddcSTobias Klauser 		ndev->stats.rx_bytes += len;
8228dc43ddcSTobias Klauser 		ndev->stats.rx_packets++;
823254a49d5SGrygorii Strashko 		kmemleak_not_leak(new_skb);
824b4727e69SSebastian Siewior 	} else {
8258dc43ddcSTobias Klauser 		ndev->stats.rx_dropped++;
826b4727e69SSebastian Siewior 		new_skb = skb;
827df828598SMugunthan V N 	}
828df828598SMugunthan V N 
829a0e2c822SMugunthan V N requeue:
830ce52c744SIvan Khoronzhuk 	if (netif_dormant(ndev)) {
831ce52c744SIvan Khoronzhuk 		dev_kfree_skb_any(new_skb);
832ce52c744SIvan Khoronzhuk 		return;
833ce52c744SIvan Khoronzhuk 	}
834ce52c744SIvan Khoronzhuk 
8358feb0a19SIvan Khoronzhuk 	ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
836e05107e6SIvan Khoronzhuk 	ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
837b4727e69SSebastian Siewior 				skb_tailroom(new_skb), 0);
838b4727e69SSebastian Siewior 	if (WARN_ON(ret < 0))
839b4727e69SSebastian Siewior 		dev_kfree_skb_any(new_skb);
840df828598SMugunthan V N }
841df828598SMugunthan V N 
84232b78d85SIvan Khoronzhuk static void cpsw_split_res(struct net_device *ndev)
84348e0a83eSIvan Khoronzhuk {
84448e0a83eSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
84532b78d85SIvan Khoronzhuk 	u32 consumed_rate = 0, bigest_rate = 0;
84648e0a83eSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
84748e0a83eSIvan Khoronzhuk 	struct cpsw_vector *txv = cpsw->txv;
84832b78d85SIvan Khoronzhuk 	int i, ch_weight, rlim_ch_num = 0;
84948e0a83eSIvan Khoronzhuk 	int budget, bigest_rate_ch = 0;
85048e0a83eSIvan Khoronzhuk 	u32 ch_rate, max_rate;
85148e0a83eSIvan Khoronzhuk 	int ch_budget = 0;
85248e0a83eSIvan Khoronzhuk 
85348e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->tx_ch_num; i++) {
85448e0a83eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
85548e0a83eSIvan Khoronzhuk 		if (!ch_rate)
85648e0a83eSIvan Khoronzhuk 			continue;
85748e0a83eSIvan Khoronzhuk 
85848e0a83eSIvan Khoronzhuk 		rlim_ch_num++;
85948e0a83eSIvan Khoronzhuk 		consumed_rate += ch_rate;
86048e0a83eSIvan Khoronzhuk 	}
86148e0a83eSIvan Khoronzhuk 
86248e0a83eSIvan Khoronzhuk 	if (cpsw->tx_ch_num == rlim_ch_num) {
86348e0a83eSIvan Khoronzhuk 		max_rate = consumed_rate;
86432b78d85SIvan Khoronzhuk 	} else if (!rlim_ch_num) {
86532b78d85SIvan Khoronzhuk 		ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
86632b78d85SIvan Khoronzhuk 		bigest_rate = 0;
86732b78d85SIvan Khoronzhuk 		max_rate = consumed_rate;
86848e0a83eSIvan Khoronzhuk 	} else {
8690be01b8eSIvan Khoronzhuk 		max_rate = cpsw->speed * 1000;
8700be01b8eSIvan Khoronzhuk 
8710be01b8eSIvan Khoronzhuk 		/* if max_rate is less then expected due to reduced link speed,
8720be01b8eSIvan Khoronzhuk 		 * split proportionally according next potential max speed
8730be01b8eSIvan Khoronzhuk 		 */
8740be01b8eSIvan Khoronzhuk 		if (max_rate < consumed_rate)
8750be01b8eSIvan Khoronzhuk 			max_rate *= 10;
8760be01b8eSIvan Khoronzhuk 
8770be01b8eSIvan Khoronzhuk 		if (max_rate < consumed_rate)
8780be01b8eSIvan Khoronzhuk 			max_rate *= 10;
87932b78d85SIvan Khoronzhuk 
88048e0a83eSIvan Khoronzhuk 		ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
88148e0a83eSIvan Khoronzhuk 		ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
88248e0a83eSIvan Khoronzhuk 			    (cpsw->tx_ch_num - rlim_ch_num);
88348e0a83eSIvan Khoronzhuk 		bigest_rate = (max_rate - consumed_rate) /
88448e0a83eSIvan Khoronzhuk 			      (cpsw->tx_ch_num - rlim_ch_num);
88548e0a83eSIvan Khoronzhuk 	}
88648e0a83eSIvan Khoronzhuk 
88732b78d85SIvan Khoronzhuk 	/* split tx weight/budget */
88848e0a83eSIvan Khoronzhuk 	budget = CPSW_POLL_WEIGHT;
88948e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->tx_ch_num; i++) {
89048e0a83eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
89148e0a83eSIvan Khoronzhuk 		if (ch_rate) {
89248e0a83eSIvan Khoronzhuk 			txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
89348e0a83eSIvan Khoronzhuk 			if (!txv[i].budget)
89432b78d85SIvan Khoronzhuk 				txv[i].budget++;
89548e0a83eSIvan Khoronzhuk 			if (ch_rate > bigest_rate) {
89648e0a83eSIvan Khoronzhuk 				bigest_rate_ch = i;
89748e0a83eSIvan Khoronzhuk 				bigest_rate = ch_rate;
89848e0a83eSIvan Khoronzhuk 			}
89932b78d85SIvan Khoronzhuk 
90032b78d85SIvan Khoronzhuk 			ch_weight = (ch_rate * 100) / max_rate;
90132b78d85SIvan Khoronzhuk 			if (!ch_weight)
90232b78d85SIvan Khoronzhuk 				ch_weight++;
90332b78d85SIvan Khoronzhuk 			cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
90448e0a83eSIvan Khoronzhuk 		} else {
90548e0a83eSIvan Khoronzhuk 			txv[i].budget = ch_budget;
90648e0a83eSIvan Khoronzhuk 			if (!bigest_rate_ch)
90748e0a83eSIvan Khoronzhuk 				bigest_rate_ch = i;
90832b78d85SIvan Khoronzhuk 			cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
90948e0a83eSIvan Khoronzhuk 		}
91048e0a83eSIvan Khoronzhuk 
91148e0a83eSIvan Khoronzhuk 		budget -= txv[i].budget;
91248e0a83eSIvan Khoronzhuk 	}
91348e0a83eSIvan Khoronzhuk 
91448e0a83eSIvan Khoronzhuk 	if (budget)
91548e0a83eSIvan Khoronzhuk 		txv[bigest_rate_ch].budget += budget;
91648e0a83eSIvan Khoronzhuk 
91748e0a83eSIvan Khoronzhuk 	/* split rx budget */
91848e0a83eSIvan Khoronzhuk 	budget = CPSW_POLL_WEIGHT;
91948e0a83eSIvan Khoronzhuk 	ch_budget = budget / cpsw->rx_ch_num;
92048e0a83eSIvan Khoronzhuk 	for (i = 0; i < cpsw->rx_ch_num; i++) {
92148e0a83eSIvan Khoronzhuk 		cpsw->rxv[i].budget = ch_budget;
92248e0a83eSIvan Khoronzhuk 		budget -= ch_budget;
92348e0a83eSIvan Khoronzhuk 	}
92448e0a83eSIvan Khoronzhuk 
92548e0a83eSIvan Khoronzhuk 	if (budget)
92648e0a83eSIvan Khoronzhuk 		cpsw->rxv[0].budget += budget;
92748e0a83eSIvan Khoronzhuk }
92848e0a83eSIvan Khoronzhuk 
929c03abd84SFelipe Balbi static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
930df828598SMugunthan V N {
931dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = dev_id;
9327ce67a38SFelipe Balbi 
9335d8d0d4dSIvan Khoronzhuk 	writel(0, &cpsw->wr_regs->tx_en);
9342c836bd9SIvan Khoronzhuk 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
935c03abd84SFelipe Balbi 
936e38b5a3dSIvan Khoronzhuk 	if (cpsw->quirk_irq) {
937e38b5a3dSIvan Khoronzhuk 		disable_irq_nosync(cpsw->irqs_table[1]);
938e38b5a3dSIvan Khoronzhuk 		cpsw->tx_irq_disabled = true;
9397da11600SMugunthan V N 	}
9407da11600SMugunthan V N 
941dbc4ec52SIvan Khoronzhuk 	napi_schedule(&cpsw->napi_tx);
942c03abd84SFelipe Balbi 	return IRQ_HANDLED;
943c03abd84SFelipe Balbi }
944c03abd84SFelipe Balbi 
945c03abd84SFelipe Balbi static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
946c03abd84SFelipe Balbi {
947dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = dev_id;
948c03abd84SFelipe Balbi 
9492c836bd9SIvan Khoronzhuk 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
9505d8d0d4dSIvan Khoronzhuk 	writel(0, &cpsw->wr_regs->rx_en);
951fd51cf19SSebastian Siewior 
952e38b5a3dSIvan Khoronzhuk 	if (cpsw->quirk_irq) {
953e38b5a3dSIvan Khoronzhuk 		disable_irq_nosync(cpsw->irqs_table[0]);
954e38b5a3dSIvan Khoronzhuk 		cpsw->rx_irq_disabled = true;
9557da11600SMugunthan V N 	}
9567da11600SMugunthan V N 
957dbc4ec52SIvan Khoronzhuk 	napi_schedule(&cpsw->napi_rx);
958df828598SMugunthan V N 	return IRQ_HANDLED;
959df828598SMugunthan V N }
960df828598SMugunthan V N 
9619611d6d6SIvan Khoronzhuk static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
962df828598SMugunthan V N {
963e05107e6SIvan Khoronzhuk 	u32			ch_map;
9648feb0a19SIvan Khoronzhuk 	int			num_tx, cur_budget, ch;
965dbc4ec52SIvan Khoronzhuk 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_tx);
9668feb0a19SIvan Khoronzhuk 	struct cpsw_vector	*txv;
96732a7432cSMugunthan V N 
968e05107e6SIvan Khoronzhuk 	/* process every unprocessed channel */
969e05107e6SIvan Khoronzhuk 	ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
970342934a5SIvan Khoronzhuk 	for (ch = 0, num_tx = 0; ch_map; ch_map >>= 1, ch++) {
971e05107e6SIvan Khoronzhuk 		if (!(ch_map & 0x01))
972e05107e6SIvan Khoronzhuk 			continue;
973e05107e6SIvan Khoronzhuk 
9748feb0a19SIvan Khoronzhuk 		txv = &cpsw->txv[ch];
9758feb0a19SIvan Khoronzhuk 		if (unlikely(txv->budget > budget - num_tx))
9768feb0a19SIvan Khoronzhuk 			cur_budget = budget - num_tx;
9778feb0a19SIvan Khoronzhuk 		else
9788feb0a19SIvan Khoronzhuk 			cur_budget = txv->budget;
9798feb0a19SIvan Khoronzhuk 
9808feb0a19SIvan Khoronzhuk 		num_tx += cpdma_chan_process(txv->ch, cur_budget);
981342934a5SIvan Khoronzhuk 		if (num_tx >= budget)
982342934a5SIvan Khoronzhuk 			break;
983e05107e6SIvan Khoronzhuk 	}
984e05107e6SIvan Khoronzhuk 
98532a7432cSMugunthan V N 	if (num_tx < budget) {
98632a7432cSMugunthan V N 		napi_complete(napi_tx);
9875d8d0d4dSIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->tx_en);
9889611d6d6SIvan Khoronzhuk 	}
9899611d6d6SIvan Khoronzhuk 
9909611d6d6SIvan Khoronzhuk 	return num_tx;
9919611d6d6SIvan Khoronzhuk }
9929611d6d6SIvan Khoronzhuk 
9939611d6d6SIvan Khoronzhuk static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
9949611d6d6SIvan Khoronzhuk {
9959611d6d6SIvan Khoronzhuk 	struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
9969611d6d6SIvan Khoronzhuk 	int num_tx;
9979611d6d6SIvan Khoronzhuk 
9989611d6d6SIvan Khoronzhuk 	num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
9999611d6d6SIvan Khoronzhuk 	if (num_tx < budget) {
10009611d6d6SIvan Khoronzhuk 		napi_complete(napi_tx);
10019611d6d6SIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->tx_en);
10029611d6d6SIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
1003e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
1004e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
10057da11600SMugunthan V N 		}
100632a7432cSMugunthan V N 	}
100732a7432cSMugunthan V N 
100832a7432cSMugunthan V N 	return num_tx;
100932a7432cSMugunthan V N }
101032a7432cSMugunthan V N 
10119611d6d6SIvan Khoronzhuk static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
101232a7432cSMugunthan V N {
1013e05107e6SIvan Khoronzhuk 	u32			ch_map;
10148feb0a19SIvan Khoronzhuk 	int			num_rx, cur_budget, ch;
1015dbc4ec52SIvan Khoronzhuk 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_rx);
10168feb0a19SIvan Khoronzhuk 	struct cpsw_vector	*rxv;
1017510a1e72SMugunthan V N 
1018e05107e6SIvan Khoronzhuk 	/* process every unprocessed channel */
1019e05107e6SIvan Khoronzhuk 	ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
1020342934a5SIvan Khoronzhuk 	for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
1021e05107e6SIvan Khoronzhuk 		if (!(ch_map & 0x01))
1022e05107e6SIvan Khoronzhuk 			continue;
1023e05107e6SIvan Khoronzhuk 
10248feb0a19SIvan Khoronzhuk 		rxv = &cpsw->rxv[ch];
10258feb0a19SIvan Khoronzhuk 		if (unlikely(rxv->budget > budget - num_rx))
10268feb0a19SIvan Khoronzhuk 			cur_budget = budget - num_rx;
10278feb0a19SIvan Khoronzhuk 		else
10288feb0a19SIvan Khoronzhuk 			cur_budget = rxv->budget;
10298feb0a19SIvan Khoronzhuk 
10308feb0a19SIvan Khoronzhuk 		num_rx += cpdma_chan_process(rxv->ch, cur_budget);
1031342934a5SIvan Khoronzhuk 		if (num_rx >= budget)
1032342934a5SIvan Khoronzhuk 			break;
1033e05107e6SIvan Khoronzhuk 	}
1034e05107e6SIvan Khoronzhuk 
1035510a1e72SMugunthan V N 	if (num_rx < budget) {
10366ad20165SEric Dumazet 		napi_complete_done(napi_rx, num_rx);
10375d8d0d4dSIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->rx_en);
10389611d6d6SIvan Khoronzhuk 	}
10399611d6d6SIvan Khoronzhuk 
10409611d6d6SIvan Khoronzhuk 	return num_rx;
10419611d6d6SIvan Khoronzhuk }
10429611d6d6SIvan Khoronzhuk 
10439611d6d6SIvan Khoronzhuk static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
10449611d6d6SIvan Khoronzhuk {
10459611d6d6SIvan Khoronzhuk 	struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
10469611d6d6SIvan Khoronzhuk 	int num_rx;
10479611d6d6SIvan Khoronzhuk 
10489611d6d6SIvan Khoronzhuk 	num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
10499611d6d6SIvan Khoronzhuk 	if (num_rx < budget) {
10509611d6d6SIvan Khoronzhuk 		napi_complete_done(napi_rx, num_rx);
10519611d6d6SIvan Khoronzhuk 		writel(0xff, &cpsw->wr_regs->rx_en);
10529611d6d6SIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
1053e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
1054e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
10557da11600SMugunthan V N 		}
1056510a1e72SMugunthan V N 	}
1057df828598SMugunthan V N 
1058df828598SMugunthan V N 	return num_rx;
1059df828598SMugunthan V N }
1060df828598SMugunthan V N 
1061df828598SMugunthan V N static inline void soft_reset(const char *module, void __iomem *reg)
1062df828598SMugunthan V N {
1063df828598SMugunthan V N 	unsigned long timeout = jiffies + HZ;
1064df828598SMugunthan V N 
1065dda5f5feSGrygorii Strashko 	writel_relaxed(1, reg);
1066df828598SMugunthan V N 	do {
1067df828598SMugunthan V N 		cpu_relax();
1068dda5f5feSGrygorii Strashko 	} while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
1069df828598SMugunthan V N 
1070dda5f5feSGrygorii Strashko 	WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
1071df828598SMugunthan V N }
1072df828598SMugunthan V N 
1073df828598SMugunthan V N static void cpsw_set_slave_mac(struct cpsw_slave *slave,
1074df828598SMugunthan V N 			       struct cpsw_priv *priv)
1075df828598SMugunthan V N {
10769750a3adSRichard Cochran 	slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
10779750a3adSRichard Cochran 	slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
1078df828598SMugunthan V N }
1079df828598SMugunthan V N 
1080df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave,
1081df828598SMugunthan V N 			      struct cpsw_priv *priv, bool *link)
1082df828598SMugunthan V N {
1083df828598SMugunthan V N 	struct phy_device	*phy = slave->phy;
1084df828598SMugunthan V N 	u32			mac_control = 0;
1085df828598SMugunthan V N 	u32			slave_port;
1086606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1087df828598SMugunthan V N 
1088df828598SMugunthan V N 	if (!phy)
1089df828598SMugunthan V N 		return;
1090df828598SMugunthan V N 
10916f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
1092df828598SMugunthan V N 
1093df828598SMugunthan V N 	if (phy->link) {
1094606f3993SIvan Khoronzhuk 		mac_control = cpsw->data.mac_control;
1095df828598SMugunthan V N 
1096df828598SMugunthan V N 		/* enable forwarding */
10972a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
1098df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1099df828598SMugunthan V N 
1100df828598SMugunthan V N 		if (phy->speed == 1000)
1101df828598SMugunthan V N 			mac_control |= BIT(7);	/* GIGABITEN	*/
1102df828598SMugunthan V N 		if (phy->duplex)
1103df828598SMugunthan V N 			mac_control |= BIT(0);	/* FULLDUPLEXEN	*/
1104342b7b74SDaniel Mack 
1105342b7b74SDaniel Mack 		/* set speed_in input in case RMII mode is used in 100Mbps */
1106342b7b74SDaniel Mack 		if (phy->speed == 100)
1107342b7b74SDaniel Mack 			mac_control |= BIT(15);
1108f9db5069SSZ Lin (林上智) 		/* in band mode only works in 10Mbps RGMII mode */
1109f9db5069SSZ Lin (林上智) 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
1110a81d8762SMugunthan V N 			mac_control |= BIT(18); /* In Band mode */
1111342b7b74SDaniel Mack 
11121923d6e4SMugunthan V N 		if (priv->rx_pause)
11131923d6e4SMugunthan V N 			mac_control |= BIT(3);
11141923d6e4SMugunthan V N 
11151923d6e4SMugunthan V N 		if (priv->tx_pause)
11161923d6e4SMugunthan V N 			mac_control |= BIT(4);
11171923d6e4SMugunthan V N 
1118df828598SMugunthan V N 		*link = true;
1119df828598SMugunthan V N 	} else {
1120df828598SMugunthan V N 		mac_control = 0;
1121df828598SMugunthan V N 		/* disable forwarding */
11222a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
1123df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1124df828598SMugunthan V N 	}
1125df828598SMugunthan V N 
1126df828598SMugunthan V N 	if (mac_control != slave->mac_control) {
1127df828598SMugunthan V N 		phy_print_status(phy);
1128dda5f5feSGrygorii Strashko 		writel_relaxed(mac_control, &slave->sliver->mac_control);
1129df828598SMugunthan V N 	}
1130df828598SMugunthan V N 
1131df828598SMugunthan V N 	slave->mac_control = mac_control;
1132df828598SMugunthan V N }
1133df828598SMugunthan V N 
11340be01b8eSIvan Khoronzhuk static int cpsw_get_common_speed(struct cpsw_common *cpsw)
11350be01b8eSIvan Khoronzhuk {
11360be01b8eSIvan Khoronzhuk 	int i, speed;
11370be01b8eSIvan Khoronzhuk 
11380be01b8eSIvan Khoronzhuk 	for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
11390be01b8eSIvan Khoronzhuk 		if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
11400be01b8eSIvan Khoronzhuk 			speed += cpsw->slaves[i].phy->speed;
11410be01b8eSIvan Khoronzhuk 
11420be01b8eSIvan Khoronzhuk 	return speed;
11430be01b8eSIvan Khoronzhuk }
11440be01b8eSIvan Khoronzhuk 
11450be01b8eSIvan Khoronzhuk static int cpsw_need_resplit(struct cpsw_common *cpsw)
11460be01b8eSIvan Khoronzhuk {
11470be01b8eSIvan Khoronzhuk 	int i, rlim_ch_num;
11480be01b8eSIvan Khoronzhuk 	int speed, ch_rate;
11490be01b8eSIvan Khoronzhuk 
11500be01b8eSIvan Khoronzhuk 	/* re-split resources only in case speed was changed */
11510be01b8eSIvan Khoronzhuk 	speed = cpsw_get_common_speed(cpsw);
11520be01b8eSIvan Khoronzhuk 	if (speed == cpsw->speed || !speed)
11530be01b8eSIvan Khoronzhuk 		return 0;
11540be01b8eSIvan Khoronzhuk 
11550be01b8eSIvan Khoronzhuk 	cpsw->speed = speed;
11560be01b8eSIvan Khoronzhuk 
11570be01b8eSIvan Khoronzhuk 	for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
11580be01b8eSIvan Khoronzhuk 		ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
11590be01b8eSIvan Khoronzhuk 		if (!ch_rate)
11600be01b8eSIvan Khoronzhuk 			break;
11610be01b8eSIvan Khoronzhuk 
11620be01b8eSIvan Khoronzhuk 		rlim_ch_num++;
11630be01b8eSIvan Khoronzhuk 	}
11640be01b8eSIvan Khoronzhuk 
11650be01b8eSIvan Khoronzhuk 	/* cases not dependent on speed */
11660be01b8eSIvan Khoronzhuk 	if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
11670be01b8eSIvan Khoronzhuk 		return 0;
11680be01b8eSIvan Khoronzhuk 
11690be01b8eSIvan Khoronzhuk 	return 1;
11700be01b8eSIvan Khoronzhuk }
11710be01b8eSIvan Khoronzhuk 
1172df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev)
1173df828598SMugunthan V N {
1174df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
11750be01b8eSIvan Khoronzhuk 	struct cpsw_common	*cpsw = priv->cpsw;
1176df828598SMugunthan V N 	bool			link = false;
1177df828598SMugunthan V N 
1178df828598SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1179df828598SMugunthan V N 
1180df828598SMugunthan V N 	if (link) {
11810be01b8eSIvan Khoronzhuk 		if (cpsw_need_resplit(cpsw))
11820be01b8eSIvan Khoronzhuk 			cpsw_split_res(ndev);
11830be01b8eSIvan Khoronzhuk 
1184df828598SMugunthan V N 		netif_carrier_on(ndev);
1185df828598SMugunthan V N 		if (netif_running(ndev))
1186e05107e6SIvan Khoronzhuk 			netif_tx_wake_all_queues(ndev);
1187df828598SMugunthan V N 	} else {
1188df828598SMugunthan V N 		netif_carrier_off(ndev);
1189e05107e6SIvan Khoronzhuk 		netif_tx_stop_all_queues(ndev);
1190df828598SMugunthan V N 	}
1191df828598SMugunthan V N }
1192df828598SMugunthan V N 
1193ff5b8ef2SMugunthan V N static int cpsw_get_coalesce(struct net_device *ndev,
1194ff5b8ef2SMugunthan V N 				struct ethtool_coalesce *coal)
1195ff5b8ef2SMugunthan V N {
11962a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1197ff5b8ef2SMugunthan V N 
11982a05a622SIvan Khoronzhuk 	coal->rx_coalesce_usecs = cpsw->coal_intvl;
1199ff5b8ef2SMugunthan V N 	return 0;
1200ff5b8ef2SMugunthan V N }
1201ff5b8ef2SMugunthan V N 
1202ff5b8ef2SMugunthan V N static int cpsw_set_coalesce(struct net_device *ndev,
1203ff5b8ef2SMugunthan V N 				struct ethtool_coalesce *coal)
1204ff5b8ef2SMugunthan V N {
1205ff5b8ef2SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1206ff5b8ef2SMugunthan V N 	u32 int_ctrl;
1207ff5b8ef2SMugunthan V N 	u32 num_interrupts = 0;
1208ff5b8ef2SMugunthan V N 	u32 prescale = 0;
1209ff5b8ef2SMugunthan V N 	u32 addnl_dvdr = 1;
1210ff5b8ef2SMugunthan V N 	u32 coal_intvl = 0;
12115d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1212ff5b8ef2SMugunthan V N 
1213ff5b8ef2SMugunthan V N 	coal_intvl = coal->rx_coalesce_usecs;
1214ff5b8ef2SMugunthan V N 
12155d8d0d4dSIvan Khoronzhuk 	int_ctrl =  readl(&cpsw->wr_regs->int_control);
12162a05a622SIvan Khoronzhuk 	prescale = cpsw->bus_freq_mhz * 4;
1217ff5b8ef2SMugunthan V N 
1218a84bc2a9SMugunthan V N 	if (!coal->rx_coalesce_usecs) {
1219a84bc2a9SMugunthan V N 		int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1220a84bc2a9SMugunthan V N 		goto update_return;
1221a84bc2a9SMugunthan V N 	}
1222a84bc2a9SMugunthan V N 
1223ff5b8ef2SMugunthan V N 	if (coal_intvl < CPSW_CMINTMIN_INTVL)
1224ff5b8ef2SMugunthan V N 		coal_intvl = CPSW_CMINTMIN_INTVL;
1225ff5b8ef2SMugunthan V N 
1226ff5b8ef2SMugunthan V N 	if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1227ff5b8ef2SMugunthan V N 		/* Interrupt pacer works with 4us Pulse, we can
1228ff5b8ef2SMugunthan V N 		 * throttle further by dilating the 4us pulse.
1229ff5b8ef2SMugunthan V N 		 */
1230ff5b8ef2SMugunthan V N 		addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1231ff5b8ef2SMugunthan V N 
1232ff5b8ef2SMugunthan V N 		if (addnl_dvdr > 1) {
1233ff5b8ef2SMugunthan V N 			prescale *= addnl_dvdr;
1234ff5b8ef2SMugunthan V N 			if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1235ff5b8ef2SMugunthan V N 				coal_intvl = (CPSW_CMINTMAX_INTVL
1236ff5b8ef2SMugunthan V N 						* addnl_dvdr);
1237ff5b8ef2SMugunthan V N 		} else {
1238ff5b8ef2SMugunthan V N 			addnl_dvdr = 1;
1239ff5b8ef2SMugunthan V N 			coal_intvl = CPSW_CMINTMAX_INTVL;
1240ff5b8ef2SMugunthan V N 		}
1241ff5b8ef2SMugunthan V N 	}
1242ff5b8ef2SMugunthan V N 
1243ff5b8ef2SMugunthan V N 	num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
12445d8d0d4dSIvan Khoronzhuk 	writel(num_interrupts, &cpsw->wr_regs->rx_imax);
12455d8d0d4dSIvan Khoronzhuk 	writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1246ff5b8ef2SMugunthan V N 
1247ff5b8ef2SMugunthan V N 	int_ctrl |= CPSW_INTPACEEN;
1248ff5b8ef2SMugunthan V N 	int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1249ff5b8ef2SMugunthan V N 	int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1250a84bc2a9SMugunthan V N 
1251a84bc2a9SMugunthan V N update_return:
12525d8d0d4dSIvan Khoronzhuk 	writel(int_ctrl, &cpsw->wr_regs->int_control);
1253ff5b8ef2SMugunthan V N 
1254ff5b8ef2SMugunthan V N 	cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
12552a05a622SIvan Khoronzhuk 	cpsw->coal_intvl = coal_intvl;
1256ff5b8ef2SMugunthan V N 
1257ff5b8ef2SMugunthan V N 	return 0;
1258ff5b8ef2SMugunthan V N }
1259ff5b8ef2SMugunthan V N 
1260d9718546SMugunthan V N static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1261d9718546SMugunthan V N {
1262e05107e6SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1263e05107e6SIvan Khoronzhuk 
1264d9718546SMugunthan V N 	switch (sset) {
1265d9718546SMugunthan V N 	case ETH_SS_STATS:
1266e05107e6SIvan Khoronzhuk 		return (CPSW_STATS_COMMON_LEN +
1267e05107e6SIvan Khoronzhuk 		       (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1268e05107e6SIvan Khoronzhuk 		       CPSW_STATS_CH_LEN);
1269d9718546SMugunthan V N 	default:
1270d9718546SMugunthan V N 		return -EOPNOTSUPP;
1271d9718546SMugunthan V N 	}
1272d9718546SMugunthan V N }
1273d9718546SMugunthan V N 
1274e05107e6SIvan Khoronzhuk static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1275e05107e6SIvan Khoronzhuk {
1276e05107e6SIvan Khoronzhuk 	int ch_stats_len;
1277e05107e6SIvan Khoronzhuk 	int line;
1278e05107e6SIvan Khoronzhuk 	int i;
1279e05107e6SIvan Khoronzhuk 
1280e05107e6SIvan Khoronzhuk 	ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1281e05107e6SIvan Khoronzhuk 	for (i = 0; i < ch_stats_len; i++) {
1282e05107e6SIvan Khoronzhuk 		line = i % CPSW_STATS_CH_LEN;
1283e05107e6SIvan Khoronzhuk 		snprintf(*p, ETH_GSTRING_LEN,
1284bf2ce3fdSFlorian Fainelli 			 "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
1285bf2ce3fdSFlorian Fainelli 			 (long)(i / CPSW_STATS_CH_LEN),
1286e05107e6SIvan Khoronzhuk 			 cpsw_gstrings_ch_stats[line].stat_string);
1287e05107e6SIvan Khoronzhuk 		*p += ETH_GSTRING_LEN;
1288e05107e6SIvan Khoronzhuk 	}
1289e05107e6SIvan Khoronzhuk }
1290e05107e6SIvan Khoronzhuk 
1291d9718546SMugunthan V N static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1292d9718546SMugunthan V N {
1293e05107e6SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1294d9718546SMugunthan V N 	u8 *p = data;
1295d9718546SMugunthan V N 	int i;
1296d9718546SMugunthan V N 
1297d9718546SMugunthan V N 	switch (stringset) {
1298d9718546SMugunthan V N 	case ETH_SS_STATS:
1299e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1300d9718546SMugunthan V N 			memcpy(p, cpsw_gstrings_stats[i].stat_string,
1301d9718546SMugunthan V N 			       ETH_GSTRING_LEN);
1302d9718546SMugunthan V N 			p += ETH_GSTRING_LEN;
1303d9718546SMugunthan V N 		}
1304e05107e6SIvan Khoronzhuk 
1305e05107e6SIvan Khoronzhuk 		cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1306e05107e6SIvan Khoronzhuk 		cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1307d9718546SMugunthan V N 		break;
1308d9718546SMugunthan V N 	}
1309d9718546SMugunthan V N }
1310d9718546SMugunthan V N 
1311d9718546SMugunthan V N static void cpsw_get_ethtool_stats(struct net_device *ndev,
1312d9718546SMugunthan V N 				    struct ethtool_stats *stats, u64 *data)
1313d9718546SMugunthan V N {
1314d9718546SMugunthan V N 	u8 *p;
13152c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1316e05107e6SIvan Khoronzhuk 	struct cpdma_chan_stats ch_stats;
1317e05107e6SIvan Khoronzhuk 	int i, l, ch;
1318d9718546SMugunthan V N 
1319d9718546SMugunthan V N 	/* Collect Davinci CPDMA stats for Rx and Tx Channel */
1320e05107e6SIvan Khoronzhuk 	for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1321e05107e6SIvan Khoronzhuk 		data[l] = readl(cpsw->hw_stats +
1322e05107e6SIvan Khoronzhuk 				cpsw_gstrings_stats[l].stat_offset);
1323d9718546SMugunthan V N 
1324e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
13258feb0a19SIvan Khoronzhuk 		cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
1326e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1327e05107e6SIvan Khoronzhuk 			p = (u8 *)&ch_stats +
1328e05107e6SIvan Khoronzhuk 				cpsw_gstrings_ch_stats[i].stat_offset;
1329e05107e6SIvan Khoronzhuk 			data[l] = *(u32 *)p;
1330e05107e6SIvan Khoronzhuk 		}
1331e05107e6SIvan Khoronzhuk 	}
1332d9718546SMugunthan V N 
1333e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
13348feb0a19SIvan Khoronzhuk 		cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
1335e05107e6SIvan Khoronzhuk 		for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1336e05107e6SIvan Khoronzhuk 			p = (u8 *)&ch_stats +
1337e05107e6SIvan Khoronzhuk 				cpsw_gstrings_ch_stats[i].stat_offset;
1338e05107e6SIvan Khoronzhuk 			data[l] = *(u32 *)p;
1339d9718546SMugunthan V N 		}
1340d9718546SMugunthan V N 	}
1341d9718546SMugunthan V N }
1342d9718546SMugunthan V N 
134327e9e103SIvan Khoronzhuk static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1344e05107e6SIvan Khoronzhuk 					struct sk_buff *skb,
1345e05107e6SIvan Khoronzhuk 					struct cpdma_chan *txch)
1346d9ba8f9eSMugunthan V N {
13472c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
13482c836bd9SIvan Khoronzhuk 
134998fdd857SIvan Khoronzhuk 	skb_tx_timestamp(skb);
1350e05107e6SIvan Khoronzhuk 	return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1351606f3993SIvan Khoronzhuk 				 priv->emac_port + cpsw->data.dual_emac);
1352d9ba8f9eSMugunthan V N }
1353d9ba8f9eSMugunthan V N 
1354d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries(
1355d9ba8f9eSMugunthan V N 		struct cpsw_priv *priv, struct cpsw_slave *slave,
1356d9ba8f9eSMugunthan V N 		u32 slave_port)
1357d9ba8f9eSMugunthan V N {
13582a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
135971a2cbb7SGrygorii Strashko 	u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1360d9ba8f9eSMugunthan V N 
13612a05a622SIvan Khoronzhuk 	if (cpsw->version == CPSW_VERSION_1)
1362d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1363d9ba8f9eSMugunthan V N 	else
1364d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
13652a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1366d9ba8f9eSMugunthan V N 			  port_mask, port_mask, 0);
13672a05a622SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1368d9ba8f9eSMugunthan V N 			   port_mask, ALE_VLAN, slave->port_vlan, 0);
13692a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
13702a05a622SIvan Khoronzhuk 			   HOST_PORT_NUM, ALE_VLAN |
13712a05a622SIvan Khoronzhuk 			   ALE_SECURE, slave->port_vlan);
13725e5add17SGrygorii Strashko 	cpsw_ale_control_set(cpsw->ale, slave_port,
13735e5add17SGrygorii Strashko 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1374d9ba8f9eSMugunthan V N }
1375d9ba8f9eSMugunthan V N 
13761e7a2e21SDaniel Mack static void soft_reset_slave(struct cpsw_slave *slave)
1377df828598SMugunthan V N {
1378df828598SMugunthan V N 	char name[32];
13791e7a2e21SDaniel Mack 
13801e7a2e21SDaniel Mack 	snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
13811e7a2e21SDaniel Mack 	soft_reset(name, &slave->sliver->soft_reset);
13821e7a2e21SDaniel Mack }
13831e7a2e21SDaniel Mack 
13841e7a2e21SDaniel Mack static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
13851e7a2e21SDaniel Mack {
1386df828598SMugunthan V N 	u32 slave_port;
138730c57f07SSekhar Nori 	struct phy_device *phy;
1388649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1389df828598SMugunthan V N 
13901e7a2e21SDaniel Mack 	soft_reset_slave(slave);
1391df828598SMugunthan V N 
1392df828598SMugunthan V N 	/* setup priority mapping */
1393dda5f5feSGrygorii Strashko 	writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
13949750a3adSRichard Cochran 
13952a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
13969750a3adSRichard Cochran 	case CPSW_VERSION_1:
13979750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
139848f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
139948f5bcccSGrygorii Strashko 		 * flow control mode
140048f5bcccSGrygorii Strashko 		 */
140148f5bcccSGrygorii Strashko 		slave_write(slave,
140248f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
140348f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
14049750a3adSRichard Cochran 		break;
14059750a3adSRichard Cochran 	case CPSW_VERSION_2:
1406c193f365SMugunthan V N 	case CPSW_VERSION_3:
1407926489beSMugunthan V N 	case CPSW_VERSION_4:
14089750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
140948f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
141048f5bcccSGrygorii Strashko 		 * flow control mode
141148f5bcccSGrygorii Strashko 		 */
141248f5bcccSGrygorii Strashko 		slave_write(slave,
141348f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
141448f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
14159750a3adSRichard Cochran 		break;
14169750a3adSRichard Cochran 	}
1417df828598SMugunthan V N 
1418df828598SMugunthan V N 	/* setup max packet size, and mac address */
1419dda5f5feSGrygorii Strashko 	writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1420df828598SMugunthan V N 	cpsw_set_slave_mac(slave, priv);
1421df828598SMugunthan V N 
1422df828598SMugunthan V N 	slave->mac_control = 0;	/* no link yet */
1423df828598SMugunthan V N 
14246f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
1425df828598SMugunthan V N 
1426606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
1427d9ba8f9eSMugunthan V N 		cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1428d9ba8f9eSMugunthan V N 	else
14292a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1430e11b220fSMugunthan V N 				   1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1431df828598SMugunthan V N 
1432d733f754SDavid Rivshin 	if (slave->data->phy_node) {
143330c57f07SSekhar Nori 		phy = of_phy_connect(priv->ndev, slave->data->phy_node,
14349e42f715SHeiko Schocher 				 &cpsw_adjust_link, 0, slave->data->phy_if);
143530c57f07SSekhar Nori 		if (!phy) {
1436f7ce9103SRob Herring 			dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1437f7ce9103SRob Herring 				slave->data->phy_node,
1438d733f754SDavid Rivshin 				slave->slave_num);
1439d733f754SDavid Rivshin 			return;
1440d733f754SDavid Rivshin 		}
1441d733f754SDavid Rivshin 	} else {
144230c57f07SSekhar Nori 		phy = phy_connect(priv->ndev, slave->data->phy_id,
1443f9a8f83bSFlorian Fainelli 				 &cpsw_adjust_link, slave->data->phy_if);
144430c57f07SSekhar Nori 		if (IS_ERR(phy)) {
1445d733f754SDavid Rivshin 			dev_err(priv->dev,
1446d733f754SDavid Rivshin 				"phy \"%s\" not found on slave %d, err %ld\n",
1447d733f754SDavid Rivshin 				slave->data->phy_id, slave->slave_num,
144830c57f07SSekhar Nori 				PTR_ERR(phy));
1449d733f754SDavid Rivshin 			return;
1450d733f754SDavid Rivshin 		}
1451d733f754SDavid Rivshin 	}
1452d733f754SDavid Rivshin 
145330c57f07SSekhar Nori 	slave->phy = phy;
145430c57f07SSekhar Nori 
14552220943aSAndrew Lunn 	phy_attached_info(slave->phy);
14562220943aSAndrew Lunn 
1457df828598SMugunthan V N 	phy_start(slave->phy);
1458388367a5SMugunthan V N 
1459388367a5SMugunthan V N 	/* Configure GMII_SEL register */
146056e31bd8SIvan Khoronzhuk 	cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
1461df828598SMugunthan V N }
1462df828598SMugunthan V N 
14633b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
14643b72c2feSMugunthan V N {
1465606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1466606f3993SIvan Khoronzhuk 	const int vlan = cpsw->data.default_vlan;
14673b72c2feSMugunthan V N 	u32 reg;
14683b72c2feSMugunthan V N 	int i;
14691e5c4bc4SLennart Sorensen 	int unreg_mcast_mask;
14703b72c2feSMugunthan V N 
14712a05a622SIvan Khoronzhuk 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
14723b72c2feSMugunthan V N 	       CPSW2_PORT_VLAN;
14733b72c2feSMugunthan V N 
14745d8d0d4dSIvan Khoronzhuk 	writel(vlan, &cpsw->host_port_regs->port_vlan);
14753b72c2feSMugunthan V N 
1476606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
1477606f3993SIvan Khoronzhuk 		slave_write(cpsw->slaves + i, vlan, reg);
14783b72c2feSMugunthan V N 
14791e5c4bc4SLennart Sorensen 	if (priv->ndev->flags & IFF_ALLMULTI)
14801e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_ALL_PORTS;
14811e5c4bc4SLennart Sorensen 	else
14821e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
14831e5c4bc4SLennart Sorensen 
14842a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
148561f1cef9SGrygorii Strashko 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
148661f1cef9SGrygorii Strashko 			  unreg_mcast_mask);
14873b72c2feSMugunthan V N }
14883b72c2feSMugunthan V N 
1489df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv)
1490df828598SMugunthan V N {
1491d9ba8f9eSMugunthan V N 	u32 fifo_mode;
14925d8d0d4dSIvan Khoronzhuk 	u32 control_reg;
14935d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
14943b72c2feSMugunthan V N 
1495df828598SMugunthan V N 	/* soft reset the controller and initialize ale */
14965d8d0d4dSIvan Khoronzhuk 	soft_reset("cpsw", &cpsw->regs->soft_reset);
14972a05a622SIvan Khoronzhuk 	cpsw_ale_start(cpsw->ale);
1498df828598SMugunthan V N 
1499df828598SMugunthan V N 	/* switch to vlan unaware mode */
15002a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
15013b72c2feSMugunthan V N 			     CPSW_ALE_VLAN_AWARE);
15025d8d0d4dSIvan Khoronzhuk 	control_reg = readl(&cpsw->regs->control);
1503a3a41d2fSGrygorii Strashko 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
15045d8d0d4dSIvan Khoronzhuk 	writel(control_reg, &cpsw->regs->control);
1505606f3993SIvan Khoronzhuk 	fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1506d9ba8f9eSMugunthan V N 		     CPSW_FIFO_NORMAL_MODE;
15075d8d0d4dSIvan Khoronzhuk 	writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1508df828598SMugunthan V N 
1509df828598SMugunthan V N 	/* setup host port priority mapping */
1510dda5f5feSGrygorii Strashko 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
15115d8d0d4dSIvan Khoronzhuk 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
1512dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1513df828598SMugunthan V N 
15142a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1515df828598SMugunthan V N 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1516df828598SMugunthan V N 
1517606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac) {
15182a05a622SIvan Khoronzhuk 		cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1519d9ba8f9eSMugunthan V N 				   0, 0);
15202a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
152171a2cbb7SGrygorii Strashko 				   ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1522df828598SMugunthan V N 	}
1523d9ba8f9eSMugunthan V N }
1524df828598SMugunthan V N 
15253802dce1SIvan Khoronzhuk static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
15263802dce1SIvan Khoronzhuk {
15273802dce1SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
15283802dce1SIvan Khoronzhuk 	struct sk_buff *skb;
15293802dce1SIvan Khoronzhuk 	int ch_buf_num;
1530e05107e6SIvan Khoronzhuk 	int ch, i, ret;
15313802dce1SIvan Khoronzhuk 
1532e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
15338feb0a19SIvan Khoronzhuk 		ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
15343802dce1SIvan Khoronzhuk 		for (i = 0; i < ch_buf_num; i++) {
15353802dce1SIvan Khoronzhuk 			skb = __netdev_alloc_skb_ip_align(priv->ndev,
15363802dce1SIvan Khoronzhuk 							  cpsw->rx_packet_max,
15373802dce1SIvan Khoronzhuk 							  GFP_KERNEL);
15383802dce1SIvan Khoronzhuk 			if (!skb) {
15393802dce1SIvan Khoronzhuk 				cpsw_err(priv, ifup, "cannot allocate skb\n");
15403802dce1SIvan Khoronzhuk 				return -ENOMEM;
15413802dce1SIvan Khoronzhuk 			}
15423802dce1SIvan Khoronzhuk 
1543e05107e6SIvan Khoronzhuk 			skb_set_queue_mapping(skb, ch);
15448feb0a19SIvan Khoronzhuk 			ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
15458feb0a19SIvan Khoronzhuk 						skb->data, skb_tailroom(skb),
15468feb0a19SIvan Khoronzhuk 						0);
15473802dce1SIvan Khoronzhuk 			if (ret < 0) {
15483802dce1SIvan Khoronzhuk 				cpsw_err(priv, ifup,
1549e05107e6SIvan Khoronzhuk 					 "cannot submit skb to channel %d rx, error %d\n",
1550e05107e6SIvan Khoronzhuk 					 ch, ret);
15513802dce1SIvan Khoronzhuk 				kfree_skb(skb);
15523802dce1SIvan Khoronzhuk 				return ret;
15533802dce1SIvan Khoronzhuk 			}
15543802dce1SIvan Khoronzhuk 			kmemleak_not_leak(skb);
15553802dce1SIvan Khoronzhuk 		}
15563802dce1SIvan Khoronzhuk 
1557e05107e6SIvan Khoronzhuk 		cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1558e05107e6SIvan Khoronzhuk 			  ch, ch_buf_num);
1559e05107e6SIvan Khoronzhuk 	}
15603802dce1SIvan Khoronzhuk 
1561e05107e6SIvan Khoronzhuk 	return 0;
15623802dce1SIvan Khoronzhuk }
15633802dce1SIvan Khoronzhuk 
15642a05a622SIvan Khoronzhuk static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1565aacebbf8SSebastian Siewior {
15663995d265SSchuyler Patton 	u32 slave_port;
15673995d265SSchuyler Patton 
15686f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
15693995d265SSchuyler Patton 
1570aacebbf8SSebastian Siewior 	if (!slave->phy)
1571aacebbf8SSebastian Siewior 		return;
1572aacebbf8SSebastian Siewior 	phy_stop(slave->phy);
1573aacebbf8SSebastian Siewior 	phy_disconnect(slave->phy);
1574aacebbf8SSebastian Siewior 	slave->phy = NULL;
15752a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, slave_port,
15763995d265SSchuyler Patton 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
15771f95ba00SGrygorii Strashko 	soft_reset_slave(slave);
1578aacebbf8SSebastian Siewior }
1579aacebbf8SSebastian Siewior 
1580df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev)
1581df828598SMugunthan V N {
1582df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1583649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
15843802dce1SIvan Khoronzhuk 	int ret;
1585df828598SMugunthan V N 	u32 reg;
1586df828598SMugunthan V N 
158756e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1588108a6537SGrygorii Strashko 	if (ret < 0) {
158956e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1590108a6537SGrygorii Strashko 		return ret;
1591108a6537SGrygorii Strashko 	}
15923fa88c51SGrygorii Strashko 
1593df828598SMugunthan V N 	netif_carrier_off(ndev);
1594df828598SMugunthan V N 
1595e05107e6SIvan Khoronzhuk 	/* Notify the stack of the actual queue counts. */
1596e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1597e05107e6SIvan Khoronzhuk 	if (ret) {
1598e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of tx queues\n");
1599e05107e6SIvan Khoronzhuk 		goto err_cleanup;
1600e05107e6SIvan Khoronzhuk 	}
1601e05107e6SIvan Khoronzhuk 
1602e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1603e05107e6SIvan Khoronzhuk 	if (ret) {
1604e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of rx queues\n");
1605e05107e6SIvan Khoronzhuk 		goto err_cleanup;
1606e05107e6SIvan Khoronzhuk 	}
1607e05107e6SIvan Khoronzhuk 
16082a05a622SIvan Khoronzhuk 	reg = cpsw->version;
1609df828598SMugunthan V N 
1610df828598SMugunthan V N 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1611df828598SMugunthan V N 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1612df828598SMugunthan V N 		 CPSW_RTL_VERSION(reg));
1613df828598SMugunthan V N 
1614d5bc1613SIvan Khoronzhuk 	/* Initialize host and slave ports */
1615d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count)
1616df828598SMugunthan V N 		cpsw_init_host_port(priv);
1617df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_open, priv);
1618df828598SMugunthan V N 
16193b72c2feSMugunthan V N 	/* Add default VLAN */
1620606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac)
16213b72c2feSMugunthan V N 		cpsw_add_default_vlan(priv);
1622e6afea0bSMugunthan V N 	else
16232a05a622SIvan Khoronzhuk 		cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
162461f1cef9SGrygorii Strashko 				  ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
16253b72c2feSMugunthan V N 
1626d5bc1613SIvan Khoronzhuk 	/* initialize shared resources for every ndev */
1627d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count) {
1628d9ba8f9eSMugunthan V N 		/* disable priority elevation */
1629dda5f5feSGrygorii Strashko 		writel_relaxed(0, &cpsw->regs->ptype);
1630df828598SMugunthan V N 
1631d9ba8f9eSMugunthan V N 		/* enable statistics collection only on all ports */
1632dda5f5feSGrygorii Strashko 		writel_relaxed(0x7, &cpsw->regs->stat_port_en);
1633df828598SMugunthan V N 
16341923d6e4SMugunthan V N 		/* Enable internal fifo flow control */
16355d8d0d4dSIvan Khoronzhuk 		writel(0x7, &cpsw->regs->flow_control);
16361923d6e4SMugunthan V N 
1637dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_rx);
1638dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_tx);
1639d354eb85SMugunthan V N 
1640e38b5a3dSIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
1641e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
1642e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
16437da11600SMugunthan V N 		}
16447da11600SMugunthan V N 
1645e38b5a3dSIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
1646e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
1647e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
16487da11600SMugunthan V N 		}
16497da11600SMugunthan V N 
16503802dce1SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
16513802dce1SIvan Khoronzhuk 		if (ret < 0)
1652aacebbf8SSebastian Siewior 			goto err_cleanup;
1653f280e89aSMugunthan V N 
16548a2c9a5aSGrygorii Strashko 		if (cpts_register(cpsw->cpts))
1655f280e89aSMugunthan V N 			dev_err(priv->dev, "error registering cpts device\n");
1656f280e89aSMugunthan V N 
1657d9ba8f9eSMugunthan V N 	}
1658df828598SMugunthan V N 
1659ff5b8ef2SMugunthan V N 	/* Enable Interrupt pacing if configured */
16602a05a622SIvan Khoronzhuk 	if (cpsw->coal_intvl != 0) {
1661ff5b8ef2SMugunthan V N 		struct ethtool_coalesce coal;
1662ff5b8ef2SMugunthan V N 
16632a05a622SIvan Khoronzhuk 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
1664ff5b8ef2SMugunthan V N 		cpsw_set_coalesce(ndev, &coal);
1665ff5b8ef2SMugunthan V N 	}
1666ff5b8ef2SMugunthan V N 
16672c836bd9SIvan Khoronzhuk 	cpdma_ctlr_start(cpsw->dma);
16682c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
1669d5bc1613SIvan Khoronzhuk 	cpsw->usage_count++;
1670f63a975eSMugunthan V N 
1671df828598SMugunthan V N 	return 0;
1672df828598SMugunthan V N 
1673aacebbf8SSebastian Siewior err_cleanup:
16742c836bd9SIvan Khoronzhuk 	cpdma_ctlr_stop(cpsw->dma);
16752a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
167656e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
1677aacebbf8SSebastian Siewior 	netif_carrier_off(priv->ndev);
1678aacebbf8SSebastian Siewior 	return ret;
1679df828598SMugunthan V N }
1680df828598SMugunthan V N 
1681df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev)
1682df828598SMugunthan V N {
1683df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1684649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1685df828598SMugunthan V N 
1686df828598SMugunthan V N 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
1687e05107e6SIvan Khoronzhuk 	netif_tx_stop_all_queues(priv->ndev);
1688df828598SMugunthan V N 	netif_carrier_off(priv->ndev);
1689d9ba8f9eSMugunthan V N 
1690d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count <= 1) {
1691dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_rx);
1692dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_tx);
16932a05a622SIvan Khoronzhuk 		cpts_unregister(cpsw->cpts);
16942c836bd9SIvan Khoronzhuk 		cpsw_intr_disable(cpsw);
16952c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
16962a05a622SIvan Khoronzhuk 		cpsw_ale_stop(cpsw->ale);
1697d9ba8f9eSMugunthan V N 	}
16982a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
16990be01b8eSIvan Khoronzhuk 
17000be01b8eSIvan Khoronzhuk 	if (cpsw_need_resplit(cpsw))
17010be01b8eSIvan Khoronzhuk 		cpsw_split_res(ndev);
17020be01b8eSIvan Khoronzhuk 
1703d5bc1613SIvan Khoronzhuk 	cpsw->usage_count--;
170456e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
1705df828598SMugunthan V N 	return 0;
1706df828598SMugunthan V N }
1707df828598SMugunthan V N 
1708df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1709df828598SMugunthan V N 				       struct net_device *ndev)
1710df828598SMugunthan V N {
1711df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
17122c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1713f44f8417SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
1714e05107e6SIvan Khoronzhuk 	struct netdev_queue *txq;
1715e05107e6SIvan Khoronzhuk 	struct cpdma_chan *txch;
1716e05107e6SIvan Khoronzhuk 	int ret, q_idx;
1717df828598SMugunthan V N 
1718df828598SMugunthan V N 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1719df828598SMugunthan V N 		cpsw_err(priv, tx_err, "packet pad failed\n");
17208dc43ddcSTobias Klauser 		ndev->stats.tx_dropped++;
17211bf96050SIvan Khoronzhuk 		return NET_XMIT_DROP;
1722df828598SMugunthan V N 	}
1723df828598SMugunthan V N 
17249232b16dSMugunthan V N 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
1725f44f8417SIvan Khoronzhuk 	    cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
17262e5b38abSRichard Cochran 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
17272e5b38abSRichard Cochran 
1728e05107e6SIvan Khoronzhuk 	q_idx = skb_get_queue_mapping(skb);
1729e05107e6SIvan Khoronzhuk 	if (q_idx >= cpsw->tx_ch_num)
1730e05107e6SIvan Khoronzhuk 		q_idx = q_idx % cpsw->tx_ch_num;
1731e05107e6SIvan Khoronzhuk 
17328feb0a19SIvan Khoronzhuk 	txch = cpsw->txv[q_idx].ch;
173362f94c21SGrygorii Strashko 	txq = netdev_get_tx_queue(ndev, q_idx);
1734e05107e6SIvan Khoronzhuk 	ret = cpsw_tx_packet_submit(priv, skb, txch);
1735df828598SMugunthan V N 	if (unlikely(ret != 0)) {
1736df828598SMugunthan V N 		cpsw_err(priv, tx_err, "desc submit failed\n");
1737df828598SMugunthan V N 		goto fail;
1738df828598SMugunthan V N 	}
1739df828598SMugunthan V N 
1740fae50823SMugunthan V N 	/* If there is no more tx desc left free then we need to
1741fae50823SMugunthan V N 	 * tell the kernel to stop sending us tx frames.
1742fae50823SMugunthan V N 	 */
1743e05107e6SIvan Khoronzhuk 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
1744e05107e6SIvan Khoronzhuk 		netif_tx_stop_queue(txq);
174562f94c21SGrygorii Strashko 
174662f94c21SGrygorii Strashko 		/* Barrier, so that stop_queue visible to other cpus */
174762f94c21SGrygorii Strashko 		smp_mb__after_atomic();
174862f94c21SGrygorii Strashko 
174962f94c21SGrygorii Strashko 		if (cpdma_check_free_tx_desc(txch))
175062f94c21SGrygorii Strashko 			netif_tx_wake_queue(txq);
1751e05107e6SIvan Khoronzhuk 	}
1752fae50823SMugunthan V N 
1753df828598SMugunthan V N 	return NETDEV_TX_OK;
1754df828598SMugunthan V N fail:
17558dc43ddcSTobias Klauser 	ndev->stats.tx_dropped++;
1756e05107e6SIvan Khoronzhuk 	netif_tx_stop_queue(txq);
175762f94c21SGrygorii Strashko 
175862f94c21SGrygorii Strashko 	/* Barrier, so that stop_queue visible to other cpus */
175962f94c21SGrygorii Strashko 	smp_mb__after_atomic();
176062f94c21SGrygorii Strashko 
176162f94c21SGrygorii Strashko 	if (cpdma_check_free_tx_desc(txch))
176262f94c21SGrygorii Strashko 		netif_tx_wake_queue(txq);
176362f94c21SGrygorii Strashko 
1764df828598SMugunthan V N 	return NETDEV_TX_BUSY;
1765df828598SMugunthan V N }
1766df828598SMugunthan V N 
1767c8395d4eSGrygorii Strashko #if IS_ENABLED(CONFIG_TI_CPTS)
17682e5b38abSRichard Cochran 
17692a05a622SIvan Khoronzhuk static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
17702e5b38abSRichard Cochran {
1771606f3993SIvan Khoronzhuk 	struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
17722e5b38abSRichard Cochran 	u32 ts_en, seq_id;
17732e5b38abSRichard Cochran 
1774b63ba58eSGrygorii Strashko 	if (!cpts_is_tx_enabled(cpsw->cpts) &&
1775b63ba58eSGrygorii Strashko 	    !cpts_is_rx_enabled(cpsw->cpts)) {
17762e5b38abSRichard Cochran 		slave_write(slave, 0, CPSW1_TS_CTL);
17772e5b38abSRichard Cochran 		return;
17782e5b38abSRichard Cochran 	}
17792e5b38abSRichard Cochran 
17802e5b38abSRichard Cochran 	seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
17812e5b38abSRichard Cochran 	ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
17822e5b38abSRichard Cochran 
1783b63ba58eSGrygorii Strashko 	if (cpts_is_tx_enabled(cpsw->cpts))
17842e5b38abSRichard Cochran 		ts_en |= CPSW_V1_TS_TX_EN;
17852e5b38abSRichard Cochran 
1786b63ba58eSGrygorii Strashko 	if (cpts_is_rx_enabled(cpsw->cpts))
17872e5b38abSRichard Cochran 		ts_en |= CPSW_V1_TS_RX_EN;
17882e5b38abSRichard Cochran 
17892e5b38abSRichard Cochran 	slave_write(slave, ts_en, CPSW1_TS_CTL);
17902e5b38abSRichard Cochran 	slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
17912e5b38abSRichard Cochran }
17922e5b38abSRichard Cochran 
17932e5b38abSRichard Cochran static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
17942e5b38abSRichard Cochran {
1795d9ba8f9eSMugunthan V N 	struct cpsw_slave *slave;
17965d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
17972e5b38abSRichard Cochran 	u32 ctrl, mtype;
17982e5b38abSRichard Cochran 
1799cb7d78d0SIvan Khoronzhuk 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1800d9ba8f9eSMugunthan V N 
18012e5b38abSRichard Cochran 	ctrl = slave_read(slave, CPSW2_CONTROL);
18022a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
180309c55372SGeorge Cherian 	case CPSW_VERSION_2:
180409c55372SGeorge Cherian 		ctrl &= ~CTRL_V2_ALL_TS_MASK;
18052e5b38abSRichard Cochran 
1806b63ba58eSGrygorii Strashko 		if (cpts_is_tx_enabled(cpsw->cpts))
180709c55372SGeorge Cherian 			ctrl |= CTRL_V2_TX_TS_BITS;
18082e5b38abSRichard Cochran 
1809b63ba58eSGrygorii Strashko 		if (cpts_is_rx_enabled(cpsw->cpts))
181009c55372SGeorge Cherian 			ctrl |= CTRL_V2_RX_TS_BITS;
181109c55372SGeorge Cherian 		break;
181209c55372SGeorge Cherian 	case CPSW_VERSION_3:
181309c55372SGeorge Cherian 	default:
181409c55372SGeorge Cherian 		ctrl &= ~CTRL_V3_ALL_TS_MASK;
181509c55372SGeorge Cherian 
1816b63ba58eSGrygorii Strashko 		if (cpts_is_tx_enabled(cpsw->cpts))
181709c55372SGeorge Cherian 			ctrl |= CTRL_V3_TX_TS_BITS;
181809c55372SGeorge Cherian 
1819b63ba58eSGrygorii Strashko 		if (cpts_is_rx_enabled(cpsw->cpts))
182009c55372SGeorge Cherian 			ctrl |= CTRL_V3_RX_TS_BITS;
182109c55372SGeorge Cherian 		break;
182209c55372SGeorge Cherian 	}
18232e5b38abSRichard Cochran 
18242e5b38abSRichard Cochran 	mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
18252e5b38abSRichard Cochran 
18262e5b38abSRichard Cochran 	slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
18272e5b38abSRichard Cochran 	slave_write(slave, ctrl, CPSW2_CONTROL);
1828dda5f5feSGrygorii Strashko 	writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
18292e5b38abSRichard Cochran }
18302e5b38abSRichard Cochran 
1831a5b4145bSBen Hutchings static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
18322e5b38abSRichard Cochran {
18333177bf6fSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(dev);
18342e5b38abSRichard Cochran 	struct hwtstamp_config cfg;
18352a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
18362a05a622SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
18372e5b38abSRichard Cochran 
18382a05a622SIvan Khoronzhuk 	if (cpsw->version != CPSW_VERSION_1 &&
18392a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_2 &&
18402a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_3)
18412ee91e54SBen Hutchings 		return -EOPNOTSUPP;
18422ee91e54SBen Hutchings 
18432e5b38abSRichard Cochran 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
18442e5b38abSRichard Cochran 		return -EFAULT;
18452e5b38abSRichard Cochran 
18462e5b38abSRichard Cochran 	/* reserved for future extensions */
18472e5b38abSRichard Cochran 	if (cfg.flags)
18482e5b38abSRichard Cochran 		return -EINVAL;
18492e5b38abSRichard Cochran 
18502ee91e54SBen Hutchings 	if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
18512e5b38abSRichard Cochran 		return -ERANGE;
18522e5b38abSRichard Cochran 
18532e5b38abSRichard Cochran 	switch (cfg.rx_filter) {
18542e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_NONE:
1855b63ba58eSGrygorii Strashko 		cpts_rx_enable(cpts, 0);
18562e5b38abSRichard Cochran 		break;
18572e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_ALL:
1858e9523a5aSGrygorii Strashko 	case HWTSTAMP_FILTER_NTP_ALL:
1859e9523a5aSGrygorii Strashko 		return -ERANGE;
18602e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
18612e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
18622e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1863e9523a5aSGrygorii Strashko 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
1864e9523a5aSGrygorii Strashko 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1865e9523a5aSGrygorii Strashko 		break;
18662e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
18672e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
18682e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
18692e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
18702e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
18712e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
18722e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
18732e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
18742e5b38abSRichard Cochran 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1875e9523a5aSGrygorii Strashko 		cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
18762e5b38abSRichard Cochran 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
18772e5b38abSRichard Cochran 		break;
18782e5b38abSRichard Cochran 	default:
18792e5b38abSRichard Cochran 		return -ERANGE;
18802e5b38abSRichard Cochran 	}
18812e5b38abSRichard Cochran 
1882b63ba58eSGrygorii Strashko 	cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
18832ee91e54SBen Hutchings 
18842a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
18852e5b38abSRichard Cochran 	case CPSW_VERSION_1:
18862a05a622SIvan Khoronzhuk 		cpsw_hwtstamp_v1(cpsw);
18872e5b38abSRichard Cochran 		break;
18882e5b38abSRichard Cochran 	case CPSW_VERSION_2:
1889f7d403cbSGeorge Cherian 	case CPSW_VERSION_3:
18902e5b38abSRichard Cochran 		cpsw_hwtstamp_v2(priv);
18912e5b38abSRichard Cochran 		break;
18922e5b38abSRichard Cochran 	default:
18932ee91e54SBen Hutchings 		WARN_ON(1);
18942e5b38abSRichard Cochran 	}
18952e5b38abSRichard Cochran 
18962e5b38abSRichard Cochran 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
18972e5b38abSRichard Cochran }
18982e5b38abSRichard Cochran 
1899a5b4145bSBen Hutchings static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1900a5b4145bSBen Hutchings {
19012a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(dev);
19022a05a622SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
1903a5b4145bSBen Hutchings 	struct hwtstamp_config cfg;
1904a5b4145bSBen Hutchings 
19052a05a622SIvan Khoronzhuk 	if (cpsw->version != CPSW_VERSION_1 &&
19062a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_2 &&
19072a05a622SIvan Khoronzhuk 	    cpsw->version != CPSW_VERSION_3)
1908a5b4145bSBen Hutchings 		return -EOPNOTSUPP;
1909a5b4145bSBen Hutchings 
1910a5b4145bSBen Hutchings 	cfg.flags = 0;
1911b63ba58eSGrygorii Strashko 	cfg.tx_type = cpts_is_tx_enabled(cpts) ?
1912b63ba58eSGrygorii Strashko 		      HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1913b63ba58eSGrygorii Strashko 	cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
1914e9523a5aSGrygorii Strashko 			 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
1915a5b4145bSBen Hutchings 
1916a5b4145bSBen Hutchings 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1917a5b4145bSBen Hutchings }
1918c8395d4eSGrygorii Strashko #else
1919c8395d4eSGrygorii Strashko static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1920c8395d4eSGrygorii Strashko {
1921c8395d4eSGrygorii Strashko 	return -EOPNOTSUPP;
1922c8395d4eSGrygorii Strashko }
1923a5b4145bSBen Hutchings 
1924c8395d4eSGrygorii Strashko static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1925c8395d4eSGrygorii Strashko {
1926c8395d4eSGrygorii Strashko 	return -EOPNOTSUPP;
1927c8395d4eSGrygorii Strashko }
19282e5b38abSRichard Cochran #endif /*CONFIG_TI_CPTS*/
19292e5b38abSRichard Cochran 
19302e5b38abSRichard Cochran static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
19312e5b38abSRichard Cochran {
193211f2c988SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(dev);
1933606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1934606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
193511f2c988SMugunthan V N 
19362e5b38abSRichard Cochran 	if (!netif_running(dev))
19372e5b38abSRichard Cochran 		return -EINVAL;
19382e5b38abSRichard Cochran 
193911f2c988SMugunthan V N 	switch (cmd) {
194011f2c988SMugunthan V N 	case SIOCSHWTSTAMP:
1941a5b4145bSBen Hutchings 		return cpsw_hwtstamp_set(dev, req);
1942a5b4145bSBen Hutchings 	case SIOCGHWTSTAMP:
1943a5b4145bSBen Hutchings 		return cpsw_hwtstamp_get(dev, req);
19442e5b38abSRichard Cochran 	}
19452e5b38abSRichard Cochran 
1946606f3993SIvan Khoronzhuk 	if (!cpsw->slaves[slave_no].phy)
1947c1b59947SStefan Sørensen 		return -EOPNOTSUPP;
1948606f3993SIvan Khoronzhuk 	return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
194911f2c988SMugunthan V N }
195011f2c988SMugunthan V N 
1951df828598SMugunthan V N static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1952df828598SMugunthan V N {
1953df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
19542c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1955e05107e6SIvan Khoronzhuk 	int ch;
1956df828598SMugunthan V N 
1957df828598SMugunthan V N 	cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
19588dc43ddcSTobias Klauser 	ndev->stats.tx_errors++;
19592c836bd9SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
1960e05107e6SIvan Khoronzhuk 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
19618feb0a19SIvan Khoronzhuk 		cpdma_chan_stop(cpsw->txv[ch].ch);
19628feb0a19SIvan Khoronzhuk 		cpdma_chan_start(cpsw->txv[ch].ch);
1963e05107e6SIvan Khoronzhuk 	}
1964e05107e6SIvan Khoronzhuk 
19652c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
196675514b66SGrygorii Strashko 	netif_trans_update(ndev);
196775514b66SGrygorii Strashko 	netif_tx_wake_all_queues(ndev);
1968df828598SMugunthan V N }
1969df828598SMugunthan V N 
1970dcfd8d58SMugunthan V N static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1971dcfd8d58SMugunthan V N {
1972dcfd8d58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1973dcfd8d58SMugunthan V N 	struct sockaddr *addr = (struct sockaddr *)p;
1974649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1975dcfd8d58SMugunthan V N 	int flags = 0;
1976dcfd8d58SMugunthan V N 	u16 vid = 0;
1977a6c5d14fSGrygorii Strashko 	int ret;
1978dcfd8d58SMugunthan V N 
1979dcfd8d58SMugunthan V N 	if (!is_valid_ether_addr(addr->sa_data))
1980dcfd8d58SMugunthan V N 		return -EADDRNOTAVAIL;
1981dcfd8d58SMugunthan V N 
198256e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1983a6c5d14fSGrygorii Strashko 	if (ret < 0) {
198456e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1985a6c5d14fSGrygorii Strashko 		return ret;
1986a6c5d14fSGrygorii Strashko 	}
1987a6c5d14fSGrygorii Strashko 
1988606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
1989606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
1990dcfd8d58SMugunthan V N 		flags = ALE_VLAN;
1991dcfd8d58SMugunthan V N 	}
1992dcfd8d58SMugunthan V N 
19932a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1994dcfd8d58SMugunthan V N 			   flags, vid);
19952a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1996dcfd8d58SMugunthan V N 			   flags, vid);
1997dcfd8d58SMugunthan V N 
1998dcfd8d58SMugunthan V N 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1999dcfd8d58SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2000dcfd8d58SMugunthan V N 	for_each_slave(priv, cpsw_set_slave_mac, priv);
2001dcfd8d58SMugunthan V N 
200256e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2003a6c5d14fSGrygorii Strashko 
2004dcfd8d58SMugunthan V N 	return 0;
2005dcfd8d58SMugunthan V N }
2006dcfd8d58SMugunthan V N 
2007df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
2008df828598SMugunthan V N static void cpsw_ndo_poll_controller(struct net_device *ndev)
2009df828598SMugunthan V N {
2010dbc4ec52SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2011df828598SMugunthan V N 
2012dbc4ec52SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2013dbc4ec52SIvan Khoronzhuk 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
2014dbc4ec52SIvan Khoronzhuk 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
2015dbc4ec52SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
2016df828598SMugunthan V N }
2017df828598SMugunthan V N #endif
2018df828598SMugunthan V N 
20193b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
20203b72c2feSMugunthan V N 				unsigned short vid)
20213b72c2feSMugunthan V N {
20223b72c2feSMugunthan V N 	int ret;
20239f6bd8faSMugunthan V N 	int unreg_mcast_mask = 0;
20249f6bd8faSMugunthan V N 	u32 port_mask;
2025606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
20269f6bd8faSMugunthan V N 
2027606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
20289f6bd8faSMugunthan V N 		port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
20299f6bd8faSMugunthan V N 
20309f6bd8faSMugunthan V N 		if (priv->ndev->flags & IFF_ALLMULTI)
20319f6bd8faSMugunthan V N 			unreg_mcast_mask = port_mask;
20329f6bd8faSMugunthan V N 	} else {
20339f6bd8faSMugunthan V N 		port_mask = ALE_ALL_PORTS;
20341e5c4bc4SLennart Sorensen 
20351e5c4bc4SLennart Sorensen 		if (priv->ndev->flags & IFF_ALLMULTI)
20361e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_ALL_PORTS;
20371e5c4bc4SLennart Sorensen 		else
20381e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
20399f6bd8faSMugunthan V N 	}
20403b72c2feSMugunthan V N 
20412a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
204261f1cef9SGrygorii Strashko 				unreg_mcast_mask);
20433b72c2feSMugunthan V N 	if (ret != 0)
20443b72c2feSMugunthan V N 		return ret;
20453b72c2feSMugunthan V N 
20462a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
204771a2cbb7SGrygorii Strashko 				 HOST_PORT_NUM, ALE_VLAN, vid);
20483b72c2feSMugunthan V N 	if (ret != 0)
20493b72c2feSMugunthan V N 		goto clean_vid;
20503b72c2feSMugunthan V N 
20512a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
20529f6bd8faSMugunthan V N 				 port_mask, ALE_VLAN, vid, 0);
20533b72c2feSMugunthan V N 	if (ret != 0)
20543b72c2feSMugunthan V N 		goto clean_vlan_ucast;
20553b72c2feSMugunthan V N 	return 0;
20563b72c2feSMugunthan V N 
20573b72c2feSMugunthan V N clean_vlan_ucast:
20582a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
205971a2cbb7SGrygorii Strashko 			   HOST_PORT_NUM, ALE_VLAN, vid);
20603b72c2feSMugunthan V N clean_vid:
20612a05a622SIvan Khoronzhuk 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
20623b72c2feSMugunthan V N 	return ret;
20633b72c2feSMugunthan V N }
20643b72c2feSMugunthan V N 
20653b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
206680d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
20673b72c2feSMugunthan V N {
20683b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2069649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2070a6c5d14fSGrygorii Strashko 	int ret;
20713b72c2feSMugunthan V N 
2072606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
20733b72c2feSMugunthan V N 		return 0;
20743b72c2feSMugunthan V N 
207556e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2076a6c5d14fSGrygorii Strashko 	if (ret < 0) {
207756e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2078a6c5d14fSGrygorii Strashko 		return ret;
2079a6c5d14fSGrygorii Strashko 	}
2080a6c5d14fSGrygorii Strashko 
2081606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
208202a54164SMugunthan V N 		/* In dual EMAC, reserved VLAN id should not be used for
208302a54164SMugunthan V N 		 * creating VLAN interfaces as this can break the dual
208402a54164SMugunthan V N 		 * EMAC port separation
208502a54164SMugunthan V N 		 */
208602a54164SMugunthan V N 		int i;
208702a54164SMugunthan V N 
2088606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
2089606f3993SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan)
209002a54164SMugunthan V N 				return -EINVAL;
209102a54164SMugunthan V N 		}
209202a54164SMugunthan V N 	}
209302a54164SMugunthan V N 
20943b72c2feSMugunthan V N 	dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
2095a6c5d14fSGrygorii Strashko 	ret = cpsw_add_vlan_ale_entry(priv, vid);
2096a6c5d14fSGrygorii Strashko 
209756e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2098a6c5d14fSGrygorii Strashko 	return ret;
20993b72c2feSMugunthan V N }
21003b72c2feSMugunthan V N 
21013b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
210280d5c368SPatrick McHardy 				     __be16 proto, u16 vid)
21033b72c2feSMugunthan V N {
21043b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2105649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
21063b72c2feSMugunthan V N 	int ret;
21073b72c2feSMugunthan V N 
2108606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
21093b72c2feSMugunthan V N 		return 0;
21103b72c2feSMugunthan V N 
211156e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
2112a6c5d14fSGrygorii Strashko 	if (ret < 0) {
211356e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
2114a6c5d14fSGrygorii Strashko 		return ret;
2115a6c5d14fSGrygorii Strashko 	}
2116a6c5d14fSGrygorii Strashko 
2117606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
211802a54164SMugunthan V N 		int i;
211902a54164SMugunthan V N 
2120606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
2121606f3993SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan)
212202a54164SMugunthan V N 				return -EINVAL;
212302a54164SMugunthan V N 		}
212402a54164SMugunthan V N 	}
212502a54164SMugunthan V N 
21263b72c2feSMugunthan V N 	dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
21272a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
21283b72c2feSMugunthan V N 	if (ret != 0)
21293b72c2feSMugunthan V N 		return ret;
21303b72c2feSMugunthan V N 
21312a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
213261f1cef9SGrygorii Strashko 				 HOST_PORT_NUM, ALE_VLAN, vid);
21333b72c2feSMugunthan V N 	if (ret != 0)
21343b72c2feSMugunthan V N 		return ret;
21353b72c2feSMugunthan V N 
21362a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
21373b72c2feSMugunthan V N 				 0, ALE_VLAN, vid);
213856e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
2139a6c5d14fSGrygorii Strashko 	return ret;
21403b72c2feSMugunthan V N }
21413b72c2feSMugunthan V N 
214283fcad0cSIvan Khoronzhuk static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
214383fcad0cSIvan Khoronzhuk {
214483fcad0cSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
214583fcad0cSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
214652986a2fSIvan Khoronzhuk 	struct cpsw_slave *slave;
214732b78d85SIvan Khoronzhuk 	u32 min_rate;
214883fcad0cSIvan Khoronzhuk 	u32 ch_rate;
214952986a2fSIvan Khoronzhuk 	int i, ret;
215083fcad0cSIvan Khoronzhuk 
215183fcad0cSIvan Khoronzhuk 	ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
215283fcad0cSIvan Khoronzhuk 	if (ch_rate == rate)
215383fcad0cSIvan Khoronzhuk 		return 0;
215483fcad0cSIvan Khoronzhuk 
215532b78d85SIvan Khoronzhuk 	ch_rate = rate * 1000;
215683fcad0cSIvan Khoronzhuk 	min_rate = cpdma_chan_get_min_rate(cpsw->dma);
215732b78d85SIvan Khoronzhuk 	if ((ch_rate < min_rate && ch_rate)) {
215832b78d85SIvan Khoronzhuk 		dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
215983fcad0cSIvan Khoronzhuk 			min_rate);
216083fcad0cSIvan Khoronzhuk 		return -EINVAL;
216183fcad0cSIvan Khoronzhuk 	}
216283fcad0cSIvan Khoronzhuk 
21630be01b8eSIvan Khoronzhuk 	if (rate > cpsw->speed) {
216432b78d85SIvan Khoronzhuk 		dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
216532b78d85SIvan Khoronzhuk 		return -EINVAL;
216632b78d85SIvan Khoronzhuk 	}
216732b78d85SIvan Khoronzhuk 
216883fcad0cSIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
216983fcad0cSIvan Khoronzhuk 	if (ret < 0) {
217083fcad0cSIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
217183fcad0cSIvan Khoronzhuk 		return ret;
217283fcad0cSIvan Khoronzhuk 	}
217383fcad0cSIvan Khoronzhuk 
217432b78d85SIvan Khoronzhuk 	ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
217583fcad0cSIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
217632b78d85SIvan Khoronzhuk 
217732b78d85SIvan Khoronzhuk 	if (ret)
217832b78d85SIvan Khoronzhuk 		return ret;
217932b78d85SIvan Khoronzhuk 
218052986a2fSIvan Khoronzhuk 	/* update rates for slaves tx queues */
218152986a2fSIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++) {
218252986a2fSIvan Khoronzhuk 		slave = &cpsw->slaves[i];
218352986a2fSIvan Khoronzhuk 		if (!slave->ndev)
218452986a2fSIvan Khoronzhuk 			continue;
218552986a2fSIvan Khoronzhuk 
218652986a2fSIvan Khoronzhuk 		netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
218752986a2fSIvan Khoronzhuk 	}
218852986a2fSIvan Khoronzhuk 
218932b78d85SIvan Khoronzhuk 	cpsw_split_res(ndev);
219083fcad0cSIvan Khoronzhuk 	return ret;
219183fcad0cSIvan Khoronzhuk }
219283fcad0cSIvan Khoronzhuk 
2193df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = {
2194df828598SMugunthan V N 	.ndo_open		= cpsw_ndo_open,
2195df828598SMugunthan V N 	.ndo_stop		= cpsw_ndo_stop,
2196df828598SMugunthan V N 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
2197dcfd8d58SMugunthan V N 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
21982e5b38abSRichard Cochran 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
2199df828598SMugunthan V N 	.ndo_validate_addr	= eth_validate_addr,
2200df828598SMugunthan V N 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
22015c50a856SMugunthan V N 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
220283fcad0cSIvan Khoronzhuk 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
2203df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
2204df828598SMugunthan V N 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
2205df828598SMugunthan V N #endif
22063b72c2feSMugunthan V N 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
22073b72c2feSMugunthan V N 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
2208df828598SMugunthan V N };
2209df828598SMugunthan V N 
221052c4f0ecSMugunthan V N static int cpsw_get_regs_len(struct net_device *ndev)
221152c4f0ecSMugunthan V N {
2212606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
221352c4f0ecSMugunthan V N 
2214606f3993SIvan Khoronzhuk 	return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
221552c4f0ecSMugunthan V N }
221652c4f0ecSMugunthan V N 
221752c4f0ecSMugunthan V N static void cpsw_get_regs(struct net_device *ndev,
221852c4f0ecSMugunthan V N 			  struct ethtool_regs *regs, void *p)
221952c4f0ecSMugunthan V N {
222052c4f0ecSMugunthan V N 	u32 *reg = p;
22212a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
222252c4f0ecSMugunthan V N 
222352c4f0ecSMugunthan V N 	/* update CPSW IP version */
22242a05a622SIvan Khoronzhuk 	regs->version = cpsw->version;
222552c4f0ecSMugunthan V N 
22262a05a622SIvan Khoronzhuk 	cpsw_ale_dump(cpsw->ale, reg);
222752c4f0ecSMugunthan V N }
222852c4f0ecSMugunthan V N 
2229df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev,
2230df828598SMugunthan V N 			     struct ethtool_drvinfo *info)
2231df828598SMugunthan V N {
2232649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
223356e31bd8SIvan Khoronzhuk 	struct platform_device	*pdev = to_platform_device(cpsw->dev);
22347826d43fSJiri Pirko 
223552c4f0ecSMugunthan V N 	strlcpy(info->driver, "cpsw", sizeof(info->driver));
22367826d43fSJiri Pirko 	strlcpy(info->version, "1.0", sizeof(info->version));
223756e31bd8SIvan Khoronzhuk 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
2238df828598SMugunthan V N }
2239df828598SMugunthan V N 
2240df828598SMugunthan V N static u32 cpsw_get_msglevel(struct net_device *ndev)
2241df828598SMugunthan V N {
2242df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2243df828598SMugunthan V N 	return priv->msg_enable;
2244df828598SMugunthan V N }
2245df828598SMugunthan V N 
2246df828598SMugunthan V N static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2247df828598SMugunthan V N {
2248df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2249df828598SMugunthan V N 	priv->msg_enable = value;
2250df828598SMugunthan V N }
2251df828598SMugunthan V N 
2252c8395d4eSGrygorii Strashko #if IS_ENABLED(CONFIG_TI_CPTS)
22532e5b38abSRichard Cochran static int cpsw_get_ts_info(struct net_device *ndev,
22542e5b38abSRichard Cochran 			    struct ethtool_ts_info *info)
22552e5b38abSRichard Cochran {
22562a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
22572e5b38abSRichard Cochran 
22582e5b38abSRichard Cochran 	info->so_timestamping =
22592e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_HARDWARE |
22602e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_SOFTWARE |
22612e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_HARDWARE |
22622e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_SOFTWARE |
22632e5b38abSRichard Cochran 		SOF_TIMESTAMPING_SOFTWARE |
22642e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RAW_HARDWARE;
22652a05a622SIvan Khoronzhuk 	info->phc_index = cpsw->cpts->phc_index;
22662e5b38abSRichard Cochran 	info->tx_types =
22672e5b38abSRichard Cochran 		(1 << HWTSTAMP_TX_OFF) |
22682e5b38abSRichard Cochran 		(1 << HWTSTAMP_TX_ON);
22692e5b38abSRichard Cochran 	info->rx_filters =
22702e5b38abSRichard Cochran 		(1 << HWTSTAMP_FILTER_NONE) |
2271e9523a5aSGrygorii Strashko 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
22722e5b38abSRichard Cochran 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2273c8395d4eSGrygorii Strashko 	return 0;
2274c8395d4eSGrygorii Strashko }
22752e5b38abSRichard Cochran #else
2276c8395d4eSGrygorii Strashko static int cpsw_get_ts_info(struct net_device *ndev,
2277c8395d4eSGrygorii Strashko 			    struct ethtool_ts_info *info)
2278c8395d4eSGrygorii Strashko {
22792e5b38abSRichard Cochran 	info->so_timestamping =
22802e5b38abSRichard Cochran 		SOF_TIMESTAMPING_TX_SOFTWARE |
22812e5b38abSRichard Cochran 		SOF_TIMESTAMPING_RX_SOFTWARE |
22822e5b38abSRichard Cochran 		SOF_TIMESTAMPING_SOFTWARE;
22832e5b38abSRichard Cochran 	info->phc_index = -1;
22842e5b38abSRichard Cochran 	info->tx_types = 0;
22852e5b38abSRichard Cochran 	info->rx_filters = 0;
22862e5b38abSRichard Cochran 	return 0;
22872e5b38abSRichard Cochran }
2288c8395d4eSGrygorii Strashko #endif
22892e5b38abSRichard Cochran 
22902479876dSPhilippe Reynes static int cpsw_get_link_ksettings(struct net_device *ndev,
22912479876dSPhilippe Reynes 				   struct ethtool_link_ksettings *ecmd)
2292d3bb9c58SMugunthan V N {
2293d3bb9c58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2294606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2295606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2296d3bb9c58SMugunthan V N 
22975514174fSyuval.shaia@oracle.com 	if (!cpsw->slaves[slave_no].phy)
2298d3bb9c58SMugunthan V N 		return -EOPNOTSUPP;
22995514174fSyuval.shaia@oracle.com 
23005514174fSyuval.shaia@oracle.com 	phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
23015514174fSyuval.shaia@oracle.com 	return 0;
2302d3bb9c58SMugunthan V N }
2303d3bb9c58SMugunthan V N 
23042479876dSPhilippe Reynes static int cpsw_set_link_ksettings(struct net_device *ndev,
23052479876dSPhilippe Reynes 				   const struct ethtool_link_ksettings *ecmd)
2306d3bb9c58SMugunthan V N {
2307d3bb9c58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
2308606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2309606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2310d3bb9c58SMugunthan V N 
2311606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
23122479876dSPhilippe Reynes 		return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
23132479876dSPhilippe Reynes 						 ecmd);
2314d3bb9c58SMugunthan V N 	else
2315d3bb9c58SMugunthan V N 		return -EOPNOTSUPP;
2316d3bb9c58SMugunthan V N }
2317d3bb9c58SMugunthan V N 
2318d8a64420SMatus Ujhelyi static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2319d8a64420SMatus Ujhelyi {
2320d8a64420SMatus Ujhelyi 	struct cpsw_priv *priv = netdev_priv(ndev);
2321606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2322606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2323d8a64420SMatus Ujhelyi 
2324d8a64420SMatus Ujhelyi 	wol->supported = 0;
2325d8a64420SMatus Ujhelyi 	wol->wolopts = 0;
2326d8a64420SMatus Ujhelyi 
2327606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
2328606f3993SIvan Khoronzhuk 		phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2329d8a64420SMatus Ujhelyi }
2330d8a64420SMatus Ujhelyi 
2331d8a64420SMatus Ujhelyi static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2332d8a64420SMatus Ujhelyi {
2333d8a64420SMatus Ujhelyi 	struct cpsw_priv *priv = netdev_priv(ndev);
2334606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2335606f3993SIvan Khoronzhuk 	int slave_no = cpsw_slave_index(cpsw, priv);
2336d8a64420SMatus Ujhelyi 
2337606f3993SIvan Khoronzhuk 	if (cpsw->slaves[slave_no].phy)
2338606f3993SIvan Khoronzhuk 		return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2339d8a64420SMatus Ujhelyi 	else
2340d8a64420SMatus Ujhelyi 		return -EOPNOTSUPP;
2341d8a64420SMatus Ujhelyi }
2342d8a64420SMatus Ujhelyi 
23431923d6e4SMugunthan V N static void cpsw_get_pauseparam(struct net_device *ndev,
23441923d6e4SMugunthan V N 				struct ethtool_pauseparam *pause)
23451923d6e4SMugunthan V N {
23461923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
23471923d6e4SMugunthan V N 
23481923d6e4SMugunthan V N 	pause->autoneg = AUTONEG_DISABLE;
23491923d6e4SMugunthan V N 	pause->rx_pause = priv->rx_pause ? true : false;
23501923d6e4SMugunthan V N 	pause->tx_pause = priv->tx_pause ? true : false;
23511923d6e4SMugunthan V N }
23521923d6e4SMugunthan V N 
23531923d6e4SMugunthan V N static int cpsw_set_pauseparam(struct net_device *ndev,
23541923d6e4SMugunthan V N 			       struct ethtool_pauseparam *pause)
23551923d6e4SMugunthan V N {
23561923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
23571923d6e4SMugunthan V N 	bool link;
23581923d6e4SMugunthan V N 
23591923d6e4SMugunthan V N 	priv->rx_pause = pause->rx_pause ? true : false;
23601923d6e4SMugunthan V N 	priv->tx_pause = pause->tx_pause ? true : false;
23611923d6e4SMugunthan V N 
23621923d6e4SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
23631923d6e4SMugunthan V N 	return 0;
23641923d6e4SMugunthan V N }
23651923d6e4SMugunthan V N 
23667898b1daSGrygorii Strashko static int cpsw_ethtool_op_begin(struct net_device *ndev)
23677898b1daSGrygorii Strashko {
23687898b1daSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
2369649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
23707898b1daSGrygorii Strashko 	int ret;
23717898b1daSGrygorii Strashko 
237256e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
23737898b1daSGrygorii Strashko 	if (ret < 0) {
23747898b1daSGrygorii Strashko 		cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
237556e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
23767898b1daSGrygorii Strashko 	}
23777898b1daSGrygorii Strashko 
23787898b1daSGrygorii Strashko 	return ret;
23797898b1daSGrygorii Strashko }
23807898b1daSGrygorii Strashko 
23817898b1daSGrygorii Strashko static void cpsw_ethtool_op_complete(struct net_device *ndev)
23827898b1daSGrygorii Strashko {
23837898b1daSGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
23847898b1daSGrygorii Strashko 	int ret;
23857898b1daSGrygorii Strashko 
238656e31bd8SIvan Khoronzhuk 	ret = pm_runtime_put(priv->cpsw->dev);
23877898b1daSGrygorii Strashko 	if (ret < 0)
23887898b1daSGrygorii Strashko 		cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
23897898b1daSGrygorii Strashko }
23907898b1daSGrygorii Strashko 
2391ce52c744SIvan Khoronzhuk static void cpsw_get_channels(struct net_device *ndev,
2392ce52c744SIvan Khoronzhuk 			      struct ethtool_channels *ch)
2393ce52c744SIvan Khoronzhuk {
2394ce52c744SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2395ce52c744SIvan Khoronzhuk 
23969611d6d6SIvan Khoronzhuk 	ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
23979611d6d6SIvan Khoronzhuk 	ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2398ce52c744SIvan Khoronzhuk 	ch->max_combined = 0;
2399ce52c744SIvan Khoronzhuk 	ch->max_other = 0;
2400ce52c744SIvan Khoronzhuk 	ch->other_count = 0;
2401ce52c744SIvan Khoronzhuk 	ch->rx_count = cpsw->rx_ch_num;
2402ce52c744SIvan Khoronzhuk 	ch->tx_count = cpsw->tx_ch_num;
2403ce52c744SIvan Khoronzhuk 	ch->combined_count = 0;
2404ce52c744SIvan Khoronzhuk }
2405ce52c744SIvan Khoronzhuk 
2406ce52c744SIvan Khoronzhuk static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2407ce52c744SIvan Khoronzhuk 				  struct ethtool_channels *ch)
2408ce52c744SIvan Khoronzhuk {
24099611d6d6SIvan Khoronzhuk 	if (cpsw->quirk_irq) {
24109611d6d6SIvan Khoronzhuk 		dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
24119611d6d6SIvan Khoronzhuk 		return -EOPNOTSUPP;
24129611d6d6SIvan Khoronzhuk 	}
24139611d6d6SIvan Khoronzhuk 
2414ce52c744SIvan Khoronzhuk 	if (ch->combined_count)
2415ce52c744SIvan Khoronzhuk 		return -EINVAL;
2416ce52c744SIvan Khoronzhuk 
2417ce52c744SIvan Khoronzhuk 	/* verify we have at least one channel in each direction */
2418ce52c744SIvan Khoronzhuk 	if (!ch->rx_count || !ch->tx_count)
2419ce52c744SIvan Khoronzhuk 		return -EINVAL;
2420ce52c744SIvan Khoronzhuk 
2421ce52c744SIvan Khoronzhuk 	if (ch->rx_count > cpsw->data.channels ||
2422ce52c744SIvan Khoronzhuk 	    ch->tx_count > cpsw->data.channels)
2423ce52c744SIvan Khoronzhuk 		return -EINVAL;
2424ce52c744SIvan Khoronzhuk 
2425ce52c744SIvan Khoronzhuk 	return 0;
2426ce52c744SIvan Khoronzhuk }
2427ce52c744SIvan Khoronzhuk 
2428ce52c744SIvan Khoronzhuk static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2429ce52c744SIvan Khoronzhuk {
2430ce52c744SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2431ce52c744SIvan Khoronzhuk 	void (*handler)(void *, int, int);
243283fcad0cSIvan Khoronzhuk 	struct netdev_queue *queue;
24338feb0a19SIvan Khoronzhuk 	struct cpsw_vector *vec;
2434ce52c744SIvan Khoronzhuk 	int ret, *ch;
2435ce52c744SIvan Khoronzhuk 
2436ce52c744SIvan Khoronzhuk 	if (rx) {
2437ce52c744SIvan Khoronzhuk 		ch = &cpsw->rx_ch_num;
24388feb0a19SIvan Khoronzhuk 		vec = cpsw->rxv;
2439ce52c744SIvan Khoronzhuk 		handler = cpsw_rx_handler;
2440ce52c744SIvan Khoronzhuk 	} else {
2441ce52c744SIvan Khoronzhuk 		ch = &cpsw->tx_ch_num;
24428feb0a19SIvan Khoronzhuk 		vec = cpsw->txv;
2443ce52c744SIvan Khoronzhuk 		handler = cpsw_tx_handler;
2444ce52c744SIvan Khoronzhuk 	}
2445ce52c744SIvan Khoronzhuk 
2446ce52c744SIvan Khoronzhuk 	while (*ch < ch_num) {
24478feb0a19SIvan Khoronzhuk 		vec[*ch].ch = cpdma_chan_create(cpsw->dma, *ch, handler, rx);
244883fcad0cSIvan Khoronzhuk 		queue = netdev_get_tx_queue(priv->ndev, *ch);
244983fcad0cSIvan Khoronzhuk 		queue->tx_maxrate = 0;
2450ce52c744SIvan Khoronzhuk 
24518feb0a19SIvan Khoronzhuk 		if (IS_ERR(vec[*ch].ch))
24528feb0a19SIvan Khoronzhuk 			return PTR_ERR(vec[*ch].ch);
2453ce52c744SIvan Khoronzhuk 
24548feb0a19SIvan Khoronzhuk 		if (!vec[*ch].ch)
2455ce52c744SIvan Khoronzhuk 			return -EINVAL;
2456ce52c744SIvan Khoronzhuk 
2457ce52c744SIvan Khoronzhuk 		cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2458ce52c744SIvan Khoronzhuk 			  (rx ? "rx" : "tx"));
2459ce52c744SIvan Khoronzhuk 		(*ch)++;
2460ce52c744SIvan Khoronzhuk 	}
2461ce52c744SIvan Khoronzhuk 
2462ce52c744SIvan Khoronzhuk 	while (*ch > ch_num) {
2463ce52c744SIvan Khoronzhuk 		(*ch)--;
2464ce52c744SIvan Khoronzhuk 
24658feb0a19SIvan Khoronzhuk 		ret = cpdma_chan_destroy(vec[*ch].ch);
2466ce52c744SIvan Khoronzhuk 		if (ret)
2467ce52c744SIvan Khoronzhuk 			return ret;
2468ce52c744SIvan Khoronzhuk 
2469ce52c744SIvan Khoronzhuk 		cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2470ce52c744SIvan Khoronzhuk 			  (rx ? "rx" : "tx"));
2471ce52c744SIvan Khoronzhuk 	}
2472ce52c744SIvan Khoronzhuk 
2473ce52c744SIvan Khoronzhuk 	return 0;
2474ce52c744SIvan Khoronzhuk }
2475ce52c744SIvan Khoronzhuk 
2476ce52c744SIvan Khoronzhuk static int cpsw_update_channels(struct cpsw_priv *priv,
2477ce52c744SIvan Khoronzhuk 				struct ethtool_channels *ch)
2478ce52c744SIvan Khoronzhuk {
2479ce52c744SIvan Khoronzhuk 	int ret;
2480ce52c744SIvan Khoronzhuk 
2481ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2482ce52c744SIvan Khoronzhuk 	if (ret)
2483ce52c744SIvan Khoronzhuk 		return ret;
2484ce52c744SIvan Khoronzhuk 
2485ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2486ce52c744SIvan Khoronzhuk 	if (ret)
2487ce52c744SIvan Khoronzhuk 		return ret;
2488ce52c744SIvan Khoronzhuk 
2489ce52c744SIvan Khoronzhuk 	return 0;
2490ce52c744SIvan Khoronzhuk }
2491ce52c744SIvan Khoronzhuk 
2492022d7ad7SIvan Khoronzhuk static void cpsw_suspend_data_pass(struct net_device *ndev)
2493ce52c744SIvan Khoronzhuk {
2494022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2495ce52c744SIvan Khoronzhuk 	struct cpsw_slave *slave;
2496022d7ad7SIvan Khoronzhuk 	int i;
2497ce52c744SIvan Khoronzhuk 
2498ce52c744SIvan Khoronzhuk 	/* Disable NAPI scheduling */
2499ce52c744SIvan Khoronzhuk 	cpsw_intr_disable(cpsw);
2500ce52c744SIvan Khoronzhuk 
2501ce52c744SIvan Khoronzhuk 	/* Stop all transmit queues for every network device.
2502ce52c744SIvan Khoronzhuk 	 * Disable re-using rx descriptors with dormant_on.
2503ce52c744SIvan Khoronzhuk 	 */
2504ce52c744SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2505ce52c744SIvan Khoronzhuk 		if (!(slave->ndev && netif_running(slave->ndev)))
2506ce52c744SIvan Khoronzhuk 			continue;
2507ce52c744SIvan Khoronzhuk 
2508ce52c744SIvan Khoronzhuk 		netif_tx_stop_all_queues(slave->ndev);
2509ce52c744SIvan Khoronzhuk 		netif_dormant_on(slave->ndev);
2510ce52c744SIvan Khoronzhuk 	}
2511ce52c744SIvan Khoronzhuk 
2512ce52c744SIvan Khoronzhuk 	/* Handle rest of tx packets and stop cpdma channels */
2513ce52c744SIvan Khoronzhuk 	cpdma_ctlr_stop(cpsw->dma);
2514022d7ad7SIvan Khoronzhuk }
2515022d7ad7SIvan Khoronzhuk 
2516022d7ad7SIvan Khoronzhuk static int cpsw_resume_data_pass(struct net_device *ndev)
2517022d7ad7SIvan Khoronzhuk {
2518022d7ad7SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
2519022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2520022d7ad7SIvan Khoronzhuk 	struct cpsw_slave *slave;
2521022d7ad7SIvan Khoronzhuk 	int i, ret;
2522022d7ad7SIvan Khoronzhuk 
2523022d7ad7SIvan Khoronzhuk 	/* Allow rx packets handling */
2524022d7ad7SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2525022d7ad7SIvan Khoronzhuk 		if (slave->ndev && netif_running(slave->ndev))
2526022d7ad7SIvan Khoronzhuk 			netif_dormant_off(slave->ndev);
2527022d7ad7SIvan Khoronzhuk 
2528022d7ad7SIvan Khoronzhuk 	/* After this receive is started */
2529d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count) {
2530022d7ad7SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
2531022d7ad7SIvan Khoronzhuk 		if (ret)
2532022d7ad7SIvan Khoronzhuk 			return ret;
2533022d7ad7SIvan Khoronzhuk 
2534022d7ad7SIvan Khoronzhuk 		cpdma_ctlr_start(cpsw->dma);
2535022d7ad7SIvan Khoronzhuk 		cpsw_intr_enable(cpsw);
2536022d7ad7SIvan Khoronzhuk 	}
2537022d7ad7SIvan Khoronzhuk 
2538022d7ad7SIvan Khoronzhuk 	/* Resume transmit for every affected interface */
2539022d7ad7SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2540022d7ad7SIvan Khoronzhuk 		if (slave->ndev && netif_running(slave->ndev))
2541022d7ad7SIvan Khoronzhuk 			netif_tx_start_all_queues(slave->ndev);
2542022d7ad7SIvan Khoronzhuk 
2543022d7ad7SIvan Khoronzhuk 	return 0;
2544022d7ad7SIvan Khoronzhuk }
2545022d7ad7SIvan Khoronzhuk 
2546022d7ad7SIvan Khoronzhuk static int cpsw_set_channels(struct net_device *ndev,
2547022d7ad7SIvan Khoronzhuk 			     struct ethtool_channels *chs)
2548022d7ad7SIvan Khoronzhuk {
2549022d7ad7SIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
2550022d7ad7SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
2551022d7ad7SIvan Khoronzhuk 	struct cpsw_slave *slave;
2552022d7ad7SIvan Khoronzhuk 	int i, ret;
2553022d7ad7SIvan Khoronzhuk 
2554022d7ad7SIvan Khoronzhuk 	ret = cpsw_check_ch_settings(cpsw, chs);
2555022d7ad7SIvan Khoronzhuk 	if (ret < 0)
2556022d7ad7SIvan Khoronzhuk 		return ret;
2557022d7ad7SIvan Khoronzhuk 
2558022d7ad7SIvan Khoronzhuk 	cpsw_suspend_data_pass(ndev);
2559ce52c744SIvan Khoronzhuk 	ret = cpsw_update_channels(priv, chs);
2560ce52c744SIvan Khoronzhuk 	if (ret)
2561ce52c744SIvan Khoronzhuk 		goto err;
2562ce52c744SIvan Khoronzhuk 
2563ce52c744SIvan Khoronzhuk 	for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2564ce52c744SIvan Khoronzhuk 		if (!(slave->ndev && netif_running(slave->ndev)))
2565ce52c744SIvan Khoronzhuk 			continue;
2566ce52c744SIvan Khoronzhuk 
2567ce52c744SIvan Khoronzhuk 		/* Inform stack about new count of queues */
2568ce52c744SIvan Khoronzhuk 		ret = netif_set_real_num_tx_queues(slave->ndev,
2569ce52c744SIvan Khoronzhuk 						   cpsw->tx_ch_num);
2570ce52c744SIvan Khoronzhuk 		if (ret) {
2571ce52c744SIvan Khoronzhuk 			dev_err(priv->dev, "cannot set real number of tx queues\n");
2572ce52c744SIvan Khoronzhuk 			goto err;
2573ce52c744SIvan Khoronzhuk 		}
2574ce52c744SIvan Khoronzhuk 
2575ce52c744SIvan Khoronzhuk 		ret = netif_set_real_num_rx_queues(slave->ndev,
2576ce52c744SIvan Khoronzhuk 						   cpsw->rx_ch_num);
2577ce52c744SIvan Khoronzhuk 		if (ret) {
2578ce52c744SIvan Khoronzhuk 			dev_err(priv->dev, "cannot set real number of rx queues\n");
2579ce52c744SIvan Khoronzhuk 			goto err;
2580ce52c744SIvan Khoronzhuk 		}
2581ce52c744SIvan Khoronzhuk 	}
2582ce52c744SIvan Khoronzhuk 
2583d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count)
258432b78d85SIvan Khoronzhuk 		cpsw_split_res(ndev);
25858feb0a19SIvan Khoronzhuk 
2586022d7ad7SIvan Khoronzhuk 	ret = cpsw_resume_data_pass(ndev);
2587022d7ad7SIvan Khoronzhuk 	if (!ret)
2588ce52c744SIvan Khoronzhuk 		return 0;
2589ce52c744SIvan Khoronzhuk err:
2590ce52c744SIvan Khoronzhuk 	dev_err(priv->dev, "cannot update channels number, closing device\n");
2591ce52c744SIvan Khoronzhuk 	dev_close(ndev);
2592ce52c744SIvan Khoronzhuk 	return ret;
2593ce52c744SIvan Khoronzhuk }
2594ce52c744SIvan Khoronzhuk 
2595a0909949SYegor Yefremov static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2596a0909949SYegor Yefremov {
2597a0909949SYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
2598a0909949SYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
2599a0909949SYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
2600a0909949SYegor Yefremov 
2601a0909949SYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
2602a0909949SYegor Yefremov 		return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2603a0909949SYegor Yefremov 	else
2604a0909949SYegor Yefremov 		return -EOPNOTSUPP;
2605a0909949SYegor Yefremov }
2606a0909949SYegor Yefremov 
2607a0909949SYegor Yefremov static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2608a0909949SYegor Yefremov {
2609a0909949SYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
2610a0909949SYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
2611a0909949SYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
2612a0909949SYegor Yefremov 
2613a0909949SYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
2614a0909949SYegor Yefremov 		return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2615a0909949SYegor Yefremov 	else
2616a0909949SYegor Yefremov 		return -EOPNOTSUPP;
2617a0909949SYegor Yefremov }
2618a0909949SYegor Yefremov 
26196bb10c2bSYegor Yefremov static int cpsw_nway_reset(struct net_device *ndev)
26206bb10c2bSYegor Yefremov {
26216bb10c2bSYegor Yefremov 	struct cpsw_priv *priv = netdev_priv(ndev);
26226bb10c2bSYegor Yefremov 	struct cpsw_common *cpsw = priv->cpsw;
26236bb10c2bSYegor Yefremov 	int slave_no = cpsw_slave_index(cpsw, priv);
26246bb10c2bSYegor Yefremov 
26256bb10c2bSYegor Yefremov 	if (cpsw->slaves[slave_no].phy)
26266bb10c2bSYegor Yefremov 		return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
26276bb10c2bSYegor Yefremov 	else
26286bb10c2bSYegor Yefremov 		return -EOPNOTSUPP;
26296bb10c2bSYegor Yefremov }
26306bb10c2bSYegor Yefremov 
2631be034fc1SGrygorii Strashko static void cpsw_get_ringparam(struct net_device *ndev,
2632be034fc1SGrygorii Strashko 			       struct ethtool_ringparam *ering)
2633be034fc1SGrygorii Strashko {
2634be034fc1SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
2635be034fc1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
2636be034fc1SGrygorii Strashko 
2637be034fc1SGrygorii Strashko 	/* not supported */
2638be034fc1SGrygorii Strashko 	ering->tx_max_pending = 0;
2639be034fc1SGrygorii Strashko 	ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
2640f89d21b9SIvan Khoronzhuk 	ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
2641be034fc1SGrygorii Strashko 	ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
2642be034fc1SGrygorii Strashko }
2643be034fc1SGrygorii Strashko 
2644be034fc1SGrygorii Strashko static int cpsw_set_ringparam(struct net_device *ndev,
2645be034fc1SGrygorii Strashko 			      struct ethtool_ringparam *ering)
2646be034fc1SGrygorii Strashko {
2647be034fc1SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
2648be034fc1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
2649022d7ad7SIvan Khoronzhuk 	int ret;
2650be034fc1SGrygorii Strashko 
2651be034fc1SGrygorii Strashko 	/* ignore ering->tx_pending - only rx_pending adjustment is supported */
2652be034fc1SGrygorii Strashko 
2653be034fc1SGrygorii Strashko 	if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
2654f89d21b9SIvan Khoronzhuk 	    ering->rx_pending < CPSW_MAX_QUEUES ||
2655f89d21b9SIvan Khoronzhuk 	    ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
2656be034fc1SGrygorii Strashko 		return -EINVAL;
2657be034fc1SGrygorii Strashko 
2658be034fc1SGrygorii Strashko 	if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
2659be034fc1SGrygorii Strashko 		return 0;
2660be034fc1SGrygorii Strashko 
2661022d7ad7SIvan Khoronzhuk 	cpsw_suspend_data_pass(ndev);
2662be034fc1SGrygorii Strashko 
2663be034fc1SGrygorii Strashko 	cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
2664be034fc1SGrygorii Strashko 
2665d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count)
2666be034fc1SGrygorii Strashko 		cpdma_chan_split_pool(cpsw->dma);
2667be034fc1SGrygorii Strashko 
2668022d7ad7SIvan Khoronzhuk 	ret = cpsw_resume_data_pass(ndev);
2669022d7ad7SIvan Khoronzhuk 	if (!ret)
2670be034fc1SGrygorii Strashko 		return 0;
2671022d7ad7SIvan Khoronzhuk 
2672022d7ad7SIvan Khoronzhuk 	dev_err(&ndev->dev, "cannot set ring params, closing device\n");
2673be034fc1SGrygorii Strashko 	dev_close(ndev);
2674be034fc1SGrygorii Strashko 	return ret;
2675be034fc1SGrygorii Strashko }
2676be034fc1SGrygorii Strashko 
2677df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = {
2678df828598SMugunthan V N 	.get_drvinfo	= cpsw_get_drvinfo,
2679df828598SMugunthan V N 	.get_msglevel	= cpsw_get_msglevel,
2680df828598SMugunthan V N 	.set_msglevel	= cpsw_set_msglevel,
2681df828598SMugunthan V N 	.get_link	= ethtool_op_get_link,
26822e5b38abSRichard Cochran 	.get_ts_info	= cpsw_get_ts_info,
2683ff5b8ef2SMugunthan V N 	.get_coalesce	= cpsw_get_coalesce,
2684ff5b8ef2SMugunthan V N 	.set_coalesce	= cpsw_set_coalesce,
2685d9718546SMugunthan V N 	.get_sset_count		= cpsw_get_sset_count,
2686d9718546SMugunthan V N 	.get_strings		= cpsw_get_strings,
2687d9718546SMugunthan V N 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
26881923d6e4SMugunthan V N 	.get_pauseparam		= cpsw_get_pauseparam,
26891923d6e4SMugunthan V N 	.set_pauseparam		= cpsw_set_pauseparam,
2690d8a64420SMatus Ujhelyi 	.get_wol	= cpsw_get_wol,
2691d8a64420SMatus Ujhelyi 	.set_wol	= cpsw_set_wol,
269252c4f0ecSMugunthan V N 	.get_regs_len	= cpsw_get_regs_len,
269352c4f0ecSMugunthan V N 	.get_regs	= cpsw_get_regs,
26947898b1daSGrygorii Strashko 	.begin		= cpsw_ethtool_op_begin,
26957898b1daSGrygorii Strashko 	.complete	= cpsw_ethtool_op_complete,
2696ce52c744SIvan Khoronzhuk 	.get_channels	= cpsw_get_channels,
2697ce52c744SIvan Khoronzhuk 	.set_channels	= cpsw_set_channels,
26982479876dSPhilippe Reynes 	.get_link_ksettings	= cpsw_get_link_ksettings,
26992479876dSPhilippe Reynes 	.set_link_ksettings	= cpsw_set_link_ksettings,
2700a0909949SYegor Yefremov 	.get_eee	= cpsw_get_eee,
2701a0909949SYegor Yefremov 	.set_eee	= cpsw_set_eee,
27026bb10c2bSYegor Yefremov 	.nway_reset	= cpsw_nway_reset,
2703be034fc1SGrygorii Strashko 	.get_ringparam = cpsw_get_ringparam,
2704be034fc1SGrygorii Strashko 	.set_ringparam = cpsw_set_ringparam,
2705df828598SMugunthan V N };
2706df828598SMugunthan V N 
2707606f3993SIvan Khoronzhuk static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
2708549985eeSRichard Cochran 			    u32 slave_reg_ofs, u32 sliver_reg_ofs)
2709df828598SMugunthan V N {
27105d8d0d4dSIvan Khoronzhuk 	void __iomem		*regs = cpsw->regs;
2711df828598SMugunthan V N 	int			slave_num = slave->slave_num;
2712606f3993SIvan Khoronzhuk 	struct cpsw_slave_data	*data = cpsw->data.slave_data + slave_num;
2713df828598SMugunthan V N 
2714df828598SMugunthan V N 	slave->data	= data;
2715549985eeSRichard Cochran 	slave->regs	= regs + slave_reg_ofs;
2716549985eeSRichard Cochran 	slave->sliver	= regs + sliver_reg_ofs;
2717d9ba8f9eSMugunthan V N 	slave->port_vlan = data->dual_emac_res_vlan;
2718df828598SMugunthan V N }
2719df828598SMugunthan V N 
2720552165bcSDavid Rivshin static int cpsw_probe_dt(struct cpsw_platform_data *data,
27212eb32b0aSMugunthan V N 			 struct platform_device *pdev)
27222eb32b0aSMugunthan V N {
27232eb32b0aSMugunthan V N 	struct device_node *node = pdev->dev.of_node;
27242eb32b0aSMugunthan V N 	struct device_node *slave_node;
27252eb32b0aSMugunthan V N 	int i = 0, ret;
27262eb32b0aSMugunthan V N 	u32 prop;
27272eb32b0aSMugunthan V N 
27282eb32b0aSMugunthan V N 	if (!node)
27292eb32b0aSMugunthan V N 		return -EINVAL;
27302eb32b0aSMugunthan V N 
27312eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "slaves", &prop)) {
273288c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
27332eb32b0aSMugunthan V N 		return -EINVAL;
27342eb32b0aSMugunthan V N 	}
27352eb32b0aSMugunthan V N 	data->slaves = prop;
27362eb32b0aSMugunthan V N 
2737e86ac13bSMugunthan V N 	if (of_property_read_u32(node, "active_slave", &prop)) {
273888c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
2739aa1a15e2SDaniel Mack 		return -EINVAL;
274078ca0b28SRichard Cochran 	}
2741e86ac13bSMugunthan V N 	data->active_slave = prop;
274278ca0b28SRichard Cochran 
2743a86854d0SKees Cook 	data->slave_data = devm_kcalloc(&pdev->dev,
2744a86854d0SKees Cook 					data->slaves,
2745a86854d0SKees Cook 					sizeof(struct cpsw_slave_data),
2746b2adaca9SJoe Perches 					GFP_KERNEL);
2747b2adaca9SJoe Perches 	if (!data->slave_data)
2748aa1a15e2SDaniel Mack 		return -ENOMEM;
27492eb32b0aSMugunthan V N 
27502eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "cpdma_channels", &prop)) {
275188c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
2752aa1a15e2SDaniel Mack 		return -EINVAL;
27532eb32b0aSMugunthan V N 	}
27542eb32b0aSMugunthan V N 	data->channels = prop;
27552eb32b0aSMugunthan V N 
27562eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "ale_entries", &prop)) {
275788c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
2758aa1a15e2SDaniel Mack 		return -EINVAL;
27592eb32b0aSMugunthan V N 	}
27602eb32b0aSMugunthan V N 	data->ale_entries = prop;
27612eb32b0aSMugunthan V N 
27622eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "bd_ram_size", &prop)) {
276388c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
2764aa1a15e2SDaniel Mack 		return -EINVAL;
27652eb32b0aSMugunthan V N 	}
27662eb32b0aSMugunthan V N 	data->bd_ram_size = prop;
27672eb32b0aSMugunthan V N 
27682eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "mac_control", &prop)) {
276988c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
2770aa1a15e2SDaniel Mack 		return -EINVAL;
27712eb32b0aSMugunthan V N 	}
27722eb32b0aSMugunthan V N 	data->mac_control = prop;
27732eb32b0aSMugunthan V N 
2774281abd96SMarkus Pargmann 	if (of_property_read_bool(node, "dual_emac"))
2775281abd96SMarkus Pargmann 		data->dual_emac = 1;
2776d9ba8f9eSMugunthan V N 
27771fb19aa7SVaibhav Hiremath 	/*
27781fb19aa7SVaibhav Hiremath 	 * Populate all the child nodes here...
27791fb19aa7SVaibhav Hiremath 	 */
27801fb19aa7SVaibhav Hiremath 	ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
27811fb19aa7SVaibhav Hiremath 	/* We do not want to force this, as in some cases may not have child */
27821fb19aa7SVaibhav Hiremath 	if (ret)
278388c99ff6SGeorge Cherian 		dev_warn(&pdev->dev, "Doesn't have any child node\n");
27841fb19aa7SVaibhav Hiremath 
27858658aaf2SBen Hutchings 	for_each_available_child_of_node(node, slave_node) {
2786549985eeSRichard Cochran 		struct cpsw_slave_data *slave_data = data->slave_data + i;
2787549985eeSRichard Cochran 		const void *mac_addr = NULL;
2788549985eeSRichard Cochran 		int lenp;
2789549985eeSRichard Cochran 		const __be32 *parp;
2790549985eeSRichard Cochran 
2791f468b10eSMarkus Pargmann 		/* This is no slave child node, continue */
2792f468b10eSMarkus Pargmann 		if (strcmp(slave_node->name, "slave"))
2793f468b10eSMarkus Pargmann 			continue;
2794f468b10eSMarkus Pargmann 
2795552165bcSDavid Rivshin 		slave_data->phy_node = of_parse_phandle(slave_node,
2796552165bcSDavid Rivshin 							"phy-handle", 0);
2797f1eea5c1SDavid Rivshin 		parp = of_get_property(slave_node, "phy_id", &lenp);
2798ae092b5bSDavid Rivshin 		if (slave_data->phy_node) {
2799ae092b5bSDavid Rivshin 			dev_dbg(&pdev->dev,
2800f7ce9103SRob Herring 				"slave[%d] using phy-handle=\"%pOF\"\n",
2801f7ce9103SRob Herring 				i, slave_data->phy_node);
2802ae092b5bSDavid Rivshin 		} else if (of_phy_is_fixed_link(slave_node)) {
2803dfc0a6d3SDavid Rivshin 			/* In the case of a fixed PHY, the DT node associated
2804dfc0a6d3SDavid Rivshin 			 * to the PHY is the Ethernet MAC DT node.
2805dfc0a6d3SDavid Rivshin 			 */
28061f71e8c9SMarkus Brunner 			ret = of_phy_register_fixed_link(slave_node);
280723a09873SJohan Hovold 			if (ret) {
280823a09873SJohan Hovold 				if (ret != -EPROBE_DEFER)
280923a09873SJohan Hovold 					dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
28101f71e8c9SMarkus Brunner 				return ret;
281123a09873SJohan Hovold 			}
281206cd6d6eSDavid Rivshin 			slave_data->phy_node = of_node_get(slave_node);
2813f1eea5c1SDavid Rivshin 		} else if (parp) {
2814f1eea5c1SDavid Rivshin 			u32 phyid;
2815f1eea5c1SDavid Rivshin 			struct device_node *mdio_node;
2816f1eea5c1SDavid Rivshin 			struct platform_device *mdio;
2817f1eea5c1SDavid Rivshin 
2818f1eea5c1SDavid Rivshin 			if (lenp != (sizeof(__be32) * 2)) {
2819f1eea5c1SDavid Rivshin 				dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
282047276fccSMugunthan V N 				goto no_phy_slave;
2821549985eeSRichard Cochran 			}
2822549985eeSRichard Cochran 			mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2823549985eeSRichard Cochran 			phyid = be32_to_cpup(parp+1);
2824549985eeSRichard Cochran 			mdio = of_find_device_by_node(mdio_node);
282560e71ab5SJohan Hovold 			of_node_put(mdio_node);
28266954cc1fSJohan Hovold 			if (!mdio) {
282756fdb2e0SMarkus Pargmann 				dev_err(&pdev->dev, "Missing mdio platform device\n");
28286954cc1fSJohan Hovold 				return -EINVAL;
28296954cc1fSJohan Hovold 			}
2830549985eeSRichard Cochran 			snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2831549985eeSRichard Cochran 				 PHY_ID_FMT, mdio->name, phyid);
283286e1d5adSJohan Hovold 			put_device(&mdio->dev);
2833f1eea5c1SDavid Rivshin 		} else {
2834ae092b5bSDavid Rivshin 			dev_err(&pdev->dev,
2835ae092b5bSDavid Rivshin 				"No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2836ae092b5bSDavid Rivshin 				i);
2837f1eea5c1SDavid Rivshin 			goto no_phy_slave;
2838f1eea5c1SDavid Rivshin 		}
283947276fccSMugunthan V N 		slave_data->phy_if = of_get_phy_mode(slave_node);
284047276fccSMugunthan V N 		if (slave_data->phy_if < 0) {
284147276fccSMugunthan V N 			dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
284247276fccSMugunthan V N 				i);
284347276fccSMugunthan V N 			return slave_data->phy_if;
284447276fccSMugunthan V N 		}
284547276fccSMugunthan V N 
284647276fccSMugunthan V N no_phy_slave:
2847549985eeSRichard Cochran 		mac_addr = of_get_mac_address(slave_node);
28480ba517b1SMarkus Pargmann 		if (mac_addr) {
2849549985eeSRichard Cochran 			memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
28500ba517b1SMarkus Pargmann 		} else {
2851b6745f6eSMugunthan V N 			ret = ti_cm_get_macid(&pdev->dev, i,
28520ba517b1SMarkus Pargmann 					      slave_data->mac_addr);
28530ba517b1SMarkus Pargmann 			if (ret)
28540ba517b1SMarkus Pargmann 				return ret;
28550ba517b1SMarkus Pargmann 		}
2856d9ba8f9eSMugunthan V N 		if (data->dual_emac) {
285791c4166cSMugunthan V N 			if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
2858d9ba8f9eSMugunthan V N 						 &prop)) {
285988c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
2860d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = i+1;
286188c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2862d9ba8f9eSMugunthan V N 					slave_data->dual_emac_res_vlan, i);
2863d9ba8f9eSMugunthan V N 			} else {
2864d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = prop;
2865d9ba8f9eSMugunthan V N 			}
2866d9ba8f9eSMugunthan V N 		}
2867d9ba8f9eSMugunthan V N 
2868549985eeSRichard Cochran 		i++;
28693a27bfacSMugunthan V N 		if (i == data->slaves)
28703a27bfacSMugunthan V N 			break;
2871549985eeSRichard Cochran 	}
2872549985eeSRichard Cochran 
28732eb32b0aSMugunthan V N 	return 0;
28742eb32b0aSMugunthan V N }
28752eb32b0aSMugunthan V N 
2876a4e32b0dSJohan Hovold static void cpsw_remove_dt(struct platform_device *pdev)
2877a4e32b0dSJohan Hovold {
28788cbcc466SJohan Hovold 	struct net_device *ndev = platform_get_drvdata(pdev);
28798cbcc466SJohan Hovold 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
28808cbcc466SJohan Hovold 	struct cpsw_platform_data *data = &cpsw->data;
28818cbcc466SJohan Hovold 	struct device_node *node = pdev->dev.of_node;
28828cbcc466SJohan Hovold 	struct device_node *slave_node;
28838cbcc466SJohan Hovold 	int i = 0;
28848cbcc466SJohan Hovold 
28858cbcc466SJohan Hovold 	for_each_available_child_of_node(node, slave_node) {
28868cbcc466SJohan Hovold 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
28878cbcc466SJohan Hovold 
28888cbcc466SJohan Hovold 		if (strcmp(slave_node->name, "slave"))
28898cbcc466SJohan Hovold 			continue;
28908cbcc466SJohan Hovold 
28913f65047cSJohan Hovold 		if (of_phy_is_fixed_link(slave_node))
28923f65047cSJohan Hovold 			of_phy_deregister_fixed_link(slave_node);
28938cbcc466SJohan Hovold 
28948cbcc466SJohan Hovold 		of_node_put(slave_data->phy_node);
28958cbcc466SJohan Hovold 
28968cbcc466SJohan Hovold 		i++;
28978cbcc466SJohan Hovold 		if (i == data->slaves)
28988cbcc466SJohan Hovold 			break;
28998cbcc466SJohan Hovold 	}
29008cbcc466SJohan Hovold 
2901a4e32b0dSJohan Hovold 	of_platform_depopulate(&pdev->dev);
2902a4e32b0dSJohan Hovold }
2903a4e32b0dSJohan Hovold 
290456e31bd8SIvan Khoronzhuk static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
2905d9ba8f9eSMugunthan V N {
2906606f3993SIvan Khoronzhuk 	struct cpsw_common		*cpsw = priv->cpsw;
2907606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	*data = &cpsw->data;
2908d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
2909d9ba8f9eSMugunthan V N 	struct cpsw_priv		*priv_sl2;
2910e38b5a3dSIvan Khoronzhuk 	int ret = 0;
2911d9ba8f9eSMugunthan V N 
2912e05107e6SIvan Khoronzhuk 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
2913d9ba8f9eSMugunthan V N 	if (!ndev) {
291456e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
2915d9ba8f9eSMugunthan V N 		return -ENOMEM;
2916d9ba8f9eSMugunthan V N 	}
2917d9ba8f9eSMugunthan V N 
2918d9ba8f9eSMugunthan V N 	priv_sl2 = netdev_priv(ndev);
2919606f3993SIvan Khoronzhuk 	priv_sl2->cpsw = cpsw;
2920d9ba8f9eSMugunthan V N 	priv_sl2->ndev = ndev;
2921d9ba8f9eSMugunthan V N 	priv_sl2->dev  = &ndev->dev;
2922d9ba8f9eSMugunthan V N 	priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2923d9ba8f9eSMugunthan V N 
2924d9ba8f9eSMugunthan V N 	if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2925d9ba8f9eSMugunthan V N 		memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2926d9ba8f9eSMugunthan V N 			ETH_ALEN);
292756e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
292856e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
2929d9ba8f9eSMugunthan V N 	} else {
29306c1f0a1fSJoe Perches 		eth_random_addr(priv_sl2->mac_addr);
293156e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
293256e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
2933d9ba8f9eSMugunthan V N 	}
2934d9ba8f9eSMugunthan V N 	memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2935d9ba8f9eSMugunthan V N 
2936d9ba8f9eSMugunthan V N 	priv_sl2->emac_port = 1;
2937606f3993SIvan Khoronzhuk 	cpsw->slaves[1].ndev = ndev;
2938f646968fSPatrick McHardy 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2939d9ba8f9eSMugunthan V N 
2940d9ba8f9eSMugunthan V N 	ndev->netdev_ops = &cpsw_netdev_ops;
29417ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &cpsw_ethtool_ops;
2942d9ba8f9eSMugunthan V N 
2943d9ba8f9eSMugunthan V N 	/* register the network device */
294456e31bd8SIvan Khoronzhuk 	SET_NETDEV_DEV(ndev, cpsw->dev);
2945d9ba8f9eSMugunthan V N 	ret = register_netdev(ndev);
2946d9ba8f9eSMugunthan V N 	if (ret) {
294756e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error registering net device\n");
2948d9ba8f9eSMugunthan V N 		free_netdev(ndev);
2949d9ba8f9eSMugunthan V N 		ret = -ENODEV;
2950d9ba8f9eSMugunthan V N 	}
2951d9ba8f9eSMugunthan V N 
2952d9ba8f9eSMugunthan V N 	return ret;
2953d9ba8f9eSMugunthan V N }
2954d9ba8f9eSMugunthan V N 
29557da11600SMugunthan V N static const struct of_device_id cpsw_of_mtable[] = {
29569611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,cpsw"},
29579611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am335x-cpsw"},
29589611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am4372-cpsw"},
29599611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,dra7-cpsw"},
29607da11600SMugunthan V N 	{ /* sentinel */ },
29617da11600SMugunthan V N };
29627da11600SMugunthan V N MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
29637da11600SMugunthan V N 
29649611d6d6SIvan Khoronzhuk static const struct soc_device_attribute cpsw_soc_devices[] = {
29659611d6d6SIvan Khoronzhuk 	{ .family = "AM33xx", .revision = "ES1.0"},
29669611d6d6SIvan Khoronzhuk 	{ /* sentinel */ }
29679611d6d6SIvan Khoronzhuk };
29689611d6d6SIvan Khoronzhuk 
2969663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev)
2970df828598SMugunthan V N {
2971ef4183a1SIvan Khoronzhuk 	struct clk			*clk;
2972d1bd9acfSSebastian Siewior 	struct cpsw_platform_data	*data;
2973df828598SMugunthan V N 	struct net_device		*ndev;
2974df828598SMugunthan V N 	struct cpsw_priv		*priv;
2975df828598SMugunthan V N 	struct cpdma_params		dma_params;
2976df828598SMugunthan V N 	struct cpsw_ale_params		ale_params;
2977aa1a15e2SDaniel Mack 	void __iomem			*ss_regs;
29788a2c9a5aSGrygorii Strashko 	void __iomem			*cpts_regs;
2979aa1a15e2SDaniel Mack 	struct resource			*res, *ss_res;
29801d147ccbSMugunthan V N 	struct gpio_descs		*mode;
2981549985eeSRichard Cochran 	u32 slave_offset, sliver_offset, slave_size;
29829611d6d6SIvan Khoronzhuk 	const struct soc_device_attribute *soc;
2983649a1688SIvan Khoronzhuk 	struct cpsw_common		*cpsw;
29845087b915SFelipe Balbi 	int ret = 0, i;
29855087b915SFelipe Balbi 	int irq;
2986df828598SMugunthan V N 
2987649a1688SIvan Khoronzhuk 	cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
29883420ea88SJohan Hovold 	if (!cpsw)
29893420ea88SJohan Hovold 		return -ENOMEM;
29903420ea88SJohan Hovold 
299156e31bd8SIvan Khoronzhuk 	cpsw->dev = &pdev->dev;
2992649a1688SIvan Khoronzhuk 
2993e05107e6SIvan Khoronzhuk 	ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
2994df828598SMugunthan V N 	if (!ndev) {
299588c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "error allocating net_device\n");
2996df828598SMugunthan V N 		return -ENOMEM;
2997df828598SMugunthan V N 	}
2998df828598SMugunthan V N 
2999df828598SMugunthan V N 	platform_set_drvdata(pdev, ndev);
3000df828598SMugunthan V N 	priv = netdev_priv(ndev);
3001649a1688SIvan Khoronzhuk 	priv->cpsw = cpsw;
3002df828598SMugunthan V N 	priv->ndev = ndev;
3003df828598SMugunthan V N 	priv->dev  = &ndev->dev;
3004df828598SMugunthan V N 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
30052a05a622SIvan Khoronzhuk 	cpsw->rx_packet_max = max(rx_packet_max, 128);
3006df828598SMugunthan V N 
30071d147ccbSMugunthan V N 	mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
30081d147ccbSMugunthan V N 	if (IS_ERR(mode)) {
30091d147ccbSMugunthan V N 		ret = PTR_ERR(mode);
30101d147ccbSMugunthan V N 		dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
30111d147ccbSMugunthan V N 		goto clean_ndev_ret;
30121d147ccbSMugunthan V N 	}
30131d147ccbSMugunthan V N 
30141fb19aa7SVaibhav Hiremath 	/*
30151fb19aa7SVaibhav Hiremath 	 * This may be required here for child devices.
30161fb19aa7SVaibhav Hiremath 	 */
30171fb19aa7SVaibhav Hiremath 	pm_runtime_enable(&pdev->dev);
30181fb19aa7SVaibhav Hiremath 
3019739683b4SMugunthan V N 	/* Select default pin state */
3020739683b4SMugunthan V N 	pinctrl_pm_select_default_state(&pdev->dev);
3021739683b4SMugunthan V N 
3022a4e32b0dSJohan Hovold 	/* Need to enable clocks with runtime PM api to access module
3023a4e32b0dSJohan Hovold 	 * registers
3024a4e32b0dSJohan Hovold 	 */
3025a4e32b0dSJohan Hovold 	ret = pm_runtime_get_sync(&pdev->dev);
3026a4e32b0dSJohan Hovold 	if (ret < 0) {
3027a4e32b0dSJohan Hovold 		pm_runtime_put_noidle(&pdev->dev);
3028aa1a15e2SDaniel Mack 		goto clean_runtime_disable_ret;
30292eb32b0aSMugunthan V N 	}
3030a4e32b0dSJohan Hovold 
303123a09873SJohan Hovold 	ret = cpsw_probe_dt(&cpsw->data, pdev);
303223a09873SJohan Hovold 	if (ret)
3033a4e32b0dSJohan Hovold 		goto clean_dt_ret;
303423a09873SJohan Hovold 
3035606f3993SIvan Khoronzhuk 	data = &cpsw->data;
3036e05107e6SIvan Khoronzhuk 	cpsw->rx_ch_num = 1;
3037e05107e6SIvan Khoronzhuk 	cpsw->tx_ch_num = 1;
30382eb32b0aSMugunthan V N 
3039df828598SMugunthan V N 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
3040df828598SMugunthan V N 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
304188c99ff6SGeorge Cherian 		dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
3042df828598SMugunthan V N 	} else {
30437efd26d0SJoe Perches 		eth_random_addr(priv->mac_addr);
304488c99ff6SGeorge Cherian 		dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
3045df828598SMugunthan V N 	}
3046df828598SMugunthan V N 
3047df828598SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
3048df828598SMugunthan V N 
3049a86854d0SKees Cook 	cpsw->slaves = devm_kcalloc(&pdev->dev,
3050a86854d0SKees Cook 				    data->slaves, sizeof(struct cpsw_slave),
3051df828598SMugunthan V N 				    GFP_KERNEL);
3052606f3993SIvan Khoronzhuk 	if (!cpsw->slaves) {
3053aa1a15e2SDaniel Mack 		ret = -ENOMEM;
3054a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3055df828598SMugunthan V N 	}
3056df828598SMugunthan V N 	for (i = 0; i < data->slaves; i++)
3057606f3993SIvan Khoronzhuk 		cpsw->slaves[i].slave_num = i;
3058df828598SMugunthan V N 
3059606f3993SIvan Khoronzhuk 	cpsw->slaves[0].ndev = ndev;
3060d9ba8f9eSMugunthan V N 	priv->emac_port = 0;
3061d9ba8f9eSMugunthan V N 
3062ef4183a1SIvan Khoronzhuk 	clk = devm_clk_get(&pdev->dev, "fck");
3063ef4183a1SIvan Khoronzhuk 	if (IS_ERR(clk)) {
3064aa1a15e2SDaniel Mack 		dev_err(priv->dev, "fck is not found\n");
3065f150bd7fSMugunthan V N 		ret = -ENODEV;
3066a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3067df828598SMugunthan V N 	}
30682a05a622SIvan Khoronzhuk 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
3069df828598SMugunthan V N 
3070aa1a15e2SDaniel Mack 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3071aa1a15e2SDaniel Mack 	ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
3072aa1a15e2SDaniel Mack 	if (IS_ERR(ss_regs)) {
3073aa1a15e2SDaniel Mack 		ret = PTR_ERR(ss_regs);
3074a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3075df828598SMugunthan V N 	}
30765d8d0d4dSIvan Khoronzhuk 	cpsw->regs = ss_regs;
3077df828598SMugunthan V N 
30782a05a622SIvan Khoronzhuk 	cpsw->version = readl(&cpsw->regs->id_ver);
3079f280e89aSMugunthan V N 
3080aa1a15e2SDaniel Mack 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
30815d8d0d4dSIvan Khoronzhuk 	cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
30825d8d0d4dSIvan Khoronzhuk 	if (IS_ERR(cpsw->wr_regs)) {
30835d8d0d4dSIvan Khoronzhuk 		ret = PTR_ERR(cpsw->wr_regs);
3084a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3085df828598SMugunthan V N 	}
3086df828598SMugunthan V N 
3087df828598SMugunthan V N 	memset(&dma_params, 0, sizeof(dma_params));
3088549985eeSRichard Cochran 	memset(&ale_params, 0, sizeof(ale_params));
3089549985eeSRichard Cochran 
30902a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
3091549985eeSRichard Cochran 	case CPSW_VERSION_1:
30925d8d0d4dSIvan Khoronzhuk 		cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
30938a2c9a5aSGrygorii Strashko 		cpts_regs		= ss_regs + CPSW1_CPTS_OFFSET;
30945d8d0d4dSIvan Khoronzhuk 		cpsw->hw_stats	     = ss_regs + CPSW1_HW_STATS;
3095549985eeSRichard Cochran 		dma_params.dmaregs   = ss_regs + CPSW1_CPDMA_OFFSET;
3096549985eeSRichard Cochran 		dma_params.txhdp     = ss_regs + CPSW1_STATERAM_OFFSET;
3097549985eeSRichard Cochran 		ale_params.ale_regs  = ss_regs + CPSW1_ALE_OFFSET;
3098549985eeSRichard Cochran 		slave_offset         = CPSW1_SLAVE_OFFSET;
3099549985eeSRichard Cochran 		slave_size           = CPSW1_SLAVE_SIZE;
3100549985eeSRichard Cochran 		sliver_offset        = CPSW1_SLIVER_OFFSET;
3101549985eeSRichard Cochran 		dma_params.desc_mem_phys = 0;
3102549985eeSRichard Cochran 		break;
3103549985eeSRichard Cochran 	case CPSW_VERSION_2:
3104c193f365SMugunthan V N 	case CPSW_VERSION_3:
3105926489beSMugunthan V N 	case CPSW_VERSION_4:
31065d8d0d4dSIvan Khoronzhuk 		cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
31078a2c9a5aSGrygorii Strashko 		cpts_regs		= ss_regs + CPSW2_CPTS_OFFSET;
31085d8d0d4dSIvan Khoronzhuk 		cpsw->hw_stats	     = ss_regs + CPSW2_HW_STATS;
3109549985eeSRichard Cochran 		dma_params.dmaregs   = ss_regs + CPSW2_CPDMA_OFFSET;
3110549985eeSRichard Cochran 		dma_params.txhdp     = ss_regs + CPSW2_STATERAM_OFFSET;
3111549985eeSRichard Cochran 		ale_params.ale_regs  = ss_regs + CPSW2_ALE_OFFSET;
3112549985eeSRichard Cochran 		slave_offset         = CPSW2_SLAVE_OFFSET;
3113549985eeSRichard Cochran 		slave_size           = CPSW2_SLAVE_SIZE;
3114549985eeSRichard Cochran 		sliver_offset        = CPSW2_SLIVER_OFFSET;
3115549985eeSRichard Cochran 		dma_params.desc_mem_phys =
3116aa1a15e2SDaniel Mack 			(u32 __force) ss_res->start + CPSW2_BD_OFFSET;
3117549985eeSRichard Cochran 		break;
3118549985eeSRichard Cochran 	default:
31192a05a622SIvan Khoronzhuk 		dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
3120549985eeSRichard Cochran 		ret = -ENODEV;
3121a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3122549985eeSRichard Cochran 	}
3123606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++) {
3124606f3993SIvan Khoronzhuk 		struct cpsw_slave *slave = &cpsw->slaves[i];
3125606f3993SIvan Khoronzhuk 
3126606f3993SIvan Khoronzhuk 		cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
3127549985eeSRichard Cochran 		slave_offset  += slave_size;
3128549985eeSRichard Cochran 		sliver_offset += SLIVER_SIZE;
3129549985eeSRichard Cochran 	}
3130549985eeSRichard Cochran 
3131df828598SMugunthan V N 	dma_params.dev		= &pdev->dev;
3132549985eeSRichard Cochran 	dma_params.rxthresh	= dma_params.dmaregs + CPDMA_RXTHRESH;
3133549985eeSRichard Cochran 	dma_params.rxfree	= dma_params.dmaregs + CPDMA_RXFREE;
3134549985eeSRichard Cochran 	dma_params.rxhdp	= dma_params.txhdp + CPDMA_RXHDP;
3135549985eeSRichard Cochran 	dma_params.txcp		= dma_params.txhdp + CPDMA_TXCP;
3136549985eeSRichard Cochran 	dma_params.rxcp		= dma_params.txhdp + CPDMA_RXCP;
3137df828598SMugunthan V N 
3138df828598SMugunthan V N 	dma_params.num_chan		= data->channels;
3139df828598SMugunthan V N 	dma_params.has_soft_reset	= true;
3140df828598SMugunthan V N 	dma_params.min_packet_size	= CPSW_MIN_PACKET_SIZE;
3141df828598SMugunthan V N 	dma_params.desc_mem_size	= data->bd_ram_size;
3142df828598SMugunthan V N 	dma_params.desc_align		= 16;
3143df828598SMugunthan V N 	dma_params.has_ext_regs		= true;
3144549985eeSRichard Cochran 	dma_params.desc_hw_addr         = dma_params.desc_mem_phys;
314583fcad0cSIvan Khoronzhuk 	dma_params.bus_freq_mhz		= cpsw->bus_freq_mhz;
314690225bf0SGrygorii Strashko 	dma_params.descs_pool_size	= descs_pool_size;
3147df828598SMugunthan V N 
31482c836bd9SIvan Khoronzhuk 	cpsw->dma = cpdma_ctlr_create(&dma_params);
31492c836bd9SIvan Khoronzhuk 	if (!cpsw->dma) {
3150df828598SMugunthan V N 		dev_err(priv->dev, "error initializing dma\n");
3151df828598SMugunthan V N 		ret = -ENOMEM;
3152a4e32b0dSJohan Hovold 		goto clean_dt_ret;
3153df828598SMugunthan V N 	}
3154df828598SMugunthan V N 
31559611d6d6SIvan Khoronzhuk 	soc = soc_device_match(cpsw_soc_devices);
31569611d6d6SIvan Khoronzhuk 	if (soc)
31579611d6d6SIvan Khoronzhuk 		cpsw->quirk_irq = 1;
31589611d6d6SIvan Khoronzhuk 
31598feb0a19SIvan Khoronzhuk 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0);
31608a83c5d7SIvan Khoronzhuk 	if (IS_ERR(cpsw->txv[0].ch)) {
31618a83c5d7SIvan Khoronzhuk 		dev_err(priv->dev, "error initializing tx dma channel\n");
31628a83c5d7SIvan Khoronzhuk 		ret = PTR_ERR(cpsw->txv[0].ch);
31638a83c5d7SIvan Khoronzhuk 		goto clean_dma_ret;
31648a83c5d7SIvan Khoronzhuk 	}
31658a83c5d7SIvan Khoronzhuk 
31668feb0a19SIvan Khoronzhuk 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
31678a83c5d7SIvan Khoronzhuk 	if (IS_ERR(cpsw->rxv[0].ch)) {
31688a83c5d7SIvan Khoronzhuk 		dev_err(priv->dev, "error initializing rx dma channel\n");
31698a83c5d7SIvan Khoronzhuk 		ret = PTR_ERR(cpsw->rxv[0].ch);
3170df828598SMugunthan V N 		goto clean_dma_ret;
3171df828598SMugunthan V N 	}
3172df828598SMugunthan V N 
31739fe9aa0bSIvan Khoronzhuk 	ale_params.dev			= &pdev->dev;
3174df828598SMugunthan V N 	ale_params.ale_ageout		= ale_ageout;
3175df828598SMugunthan V N 	ale_params.ale_entries		= data->ale_entries;
3176c6395f12SGrygorii Strashko 	ale_params.ale_ports		= CPSW_ALE_PORTS_NUM;
3177df828598SMugunthan V N 
31782a05a622SIvan Khoronzhuk 	cpsw->ale = cpsw_ale_create(&ale_params);
31792a05a622SIvan Khoronzhuk 	if (!cpsw->ale) {
3180df828598SMugunthan V N 		dev_err(priv->dev, "error initializing ale engine\n");
3181df828598SMugunthan V N 		ret = -ENODEV;
3182df828598SMugunthan V N 		goto clean_dma_ret;
3183df828598SMugunthan V N 	}
3184df828598SMugunthan V N 
31854a88fb95SGrygorii Strashko 	cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
31868a2c9a5aSGrygorii Strashko 	if (IS_ERR(cpsw->cpts)) {
31878a2c9a5aSGrygorii Strashko 		ret = PTR_ERR(cpsw->cpts);
31881971ab58SGrygorii Strashko 		goto clean_dma_ret;
31898a2c9a5aSGrygorii Strashko 	}
31908a2c9a5aSGrygorii Strashko 
3191c03abd84SFelipe Balbi 	ndev->irq = platform_get_irq(pdev, 1);
3192df828598SMugunthan V N 	if (ndev->irq < 0) {
3193df828598SMugunthan V N 		dev_err(priv->dev, "error getting irq resource\n");
3194c1e3334fSJulia Lawall 		ret = ndev->irq;
31951971ab58SGrygorii Strashko 		goto clean_dma_ret;
3196df828598SMugunthan V N 	}
3197df828598SMugunthan V N 
3198a3a41d2fSGrygorii Strashko 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3199070f9c65SKeerthy 
3200070f9c65SKeerthy 	ndev->netdev_ops = &cpsw_netdev_ops;
3201070f9c65SKeerthy 	ndev->ethtool_ops = &cpsw_ethtool_ops;
32029611d6d6SIvan Khoronzhuk 	netif_napi_add(ndev, &cpsw->napi_rx,
32039611d6d6SIvan Khoronzhuk 		       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
32049611d6d6SIvan Khoronzhuk 		       CPSW_POLL_WEIGHT);
32059611d6d6SIvan Khoronzhuk 	netif_tx_napi_add(ndev, &cpsw->napi_tx,
32069611d6d6SIvan Khoronzhuk 			  cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
32079611d6d6SIvan Khoronzhuk 			  CPSW_POLL_WEIGHT);
3208070f9c65SKeerthy 	cpsw_split_res(ndev);
3209070f9c65SKeerthy 
3210070f9c65SKeerthy 	/* register the network device */
3211070f9c65SKeerthy 	SET_NETDEV_DEV(ndev, &pdev->dev);
3212070f9c65SKeerthy 	ret = register_netdev(ndev);
3213070f9c65SKeerthy 	if (ret) {
3214070f9c65SKeerthy 		dev_err(priv->dev, "error registering net device\n");
3215070f9c65SKeerthy 		ret = -ENODEV;
32161971ab58SGrygorii Strashko 		goto clean_dma_ret;
3217070f9c65SKeerthy 	}
3218070f9c65SKeerthy 
3219070f9c65SKeerthy 	if (cpsw->data.dual_emac) {
3220070f9c65SKeerthy 		ret = cpsw_probe_dual_emac(priv);
3221070f9c65SKeerthy 		if (ret) {
3222070f9c65SKeerthy 			cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
3223070f9c65SKeerthy 			goto clean_unregister_netdev_ret;
3224070f9c65SKeerthy 		}
3225070f9c65SKeerthy 	}
3226070f9c65SKeerthy 
3227c03abd84SFelipe Balbi 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3228c03abd84SFelipe Balbi 	 * MISC IRQs which are always kept disabled with this driver so
3229c03abd84SFelipe Balbi 	 * we will not request them.
3230c03abd84SFelipe Balbi 	 *
3231c03abd84SFelipe Balbi 	 * If anyone wants to implement support for those, make sure to
3232c03abd84SFelipe Balbi 	 * first request and append them to irqs_table array.
3233c03abd84SFelipe Balbi 	 */
3234c2b32e58SDaniel Mack 
3235c03abd84SFelipe Balbi 	/* RX IRQ */
32365087b915SFelipe Balbi 	irq = platform_get_irq(pdev, 1);
3237c1e3334fSJulia Lawall 	if (irq < 0) {
3238c1e3334fSJulia Lawall 		ret = irq;
32391971ab58SGrygorii Strashko 		goto clean_dma_ret;
3240c1e3334fSJulia Lawall 	}
32415087b915SFelipe Balbi 
3242e38b5a3dSIvan Khoronzhuk 	cpsw->irqs_table[0] = irq;
3243c03abd84SFelipe Balbi 	ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
3244dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
32455087b915SFelipe Balbi 	if (ret < 0) {
32465087b915SFelipe Balbi 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
32471971ab58SGrygorii Strashko 		goto clean_dma_ret;
3248df828598SMugunthan V N 	}
3249df828598SMugunthan V N 
3250c03abd84SFelipe Balbi 	/* TX IRQ */
32515087b915SFelipe Balbi 	irq = platform_get_irq(pdev, 2);
3252c1e3334fSJulia Lawall 	if (irq < 0) {
3253c1e3334fSJulia Lawall 		ret = irq;
32541971ab58SGrygorii Strashko 		goto clean_dma_ret;
3255c1e3334fSJulia Lawall 	}
32565087b915SFelipe Balbi 
3257e38b5a3dSIvan Khoronzhuk 	cpsw->irqs_table[1] = irq;
3258c03abd84SFelipe Balbi 	ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
3259dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
32605087b915SFelipe Balbi 	if (ret < 0) {
32615087b915SFelipe Balbi 		dev_err(priv->dev, "error attaching irq (%d)\n", ret);
32621971ab58SGrygorii Strashko 		goto clean_dma_ret;
32635087b915SFelipe Balbi 	}
3264c2b32e58SDaniel Mack 
326590225bf0SGrygorii Strashko 	cpsw_notice(priv, probe,
326690225bf0SGrygorii Strashko 		    "initialized device (regs %pa, irq %d, pool size %d)\n",
326790225bf0SGrygorii Strashko 		    &ss_res->start, ndev->irq, dma_params.descs_pool_size);
3268d9ba8f9eSMugunthan V N 
3269c46ab7e0SJohan Hovold 	pm_runtime_put(&pdev->dev);
3270c46ab7e0SJohan Hovold 
3271df828598SMugunthan V N 	return 0;
3272df828598SMugunthan V N 
3273a7fe9d46SJohan Hovold clean_unregister_netdev_ret:
3274a7fe9d46SJohan Hovold 	unregister_netdev(ndev);
3275df828598SMugunthan V N clean_dma_ret:
32762c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
3277a4e32b0dSJohan Hovold clean_dt_ret:
3278a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
3279c46ab7e0SJohan Hovold 	pm_runtime_put_sync(&pdev->dev);
3280aa1a15e2SDaniel Mack clean_runtime_disable_ret:
3281f150bd7fSMugunthan V N 	pm_runtime_disable(&pdev->dev);
3282df828598SMugunthan V N clean_ndev_ret:
3283d1bd9acfSSebastian Siewior 	free_netdev(priv->ndev);
3284df828598SMugunthan V N 	return ret;
3285df828598SMugunthan V N }
3286df828598SMugunthan V N 
3287663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev)
3288df828598SMugunthan V N {
3289df828598SMugunthan V N 	struct net_device *ndev = platform_get_drvdata(pdev);
32902a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
32918a0b6dc9SGrygorii Strashko 	int ret;
32928a0b6dc9SGrygorii Strashko 
32938a0b6dc9SGrygorii Strashko 	ret = pm_runtime_get_sync(&pdev->dev);
32948a0b6dc9SGrygorii Strashko 	if (ret < 0) {
32958a0b6dc9SGrygorii Strashko 		pm_runtime_put_noidle(&pdev->dev);
32968a0b6dc9SGrygorii Strashko 		return ret;
32978a0b6dc9SGrygorii Strashko 	}
3298df828598SMugunthan V N 
3299606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
3300606f3993SIvan Khoronzhuk 		unregister_netdev(cpsw->slaves[1].ndev);
3301d1bd9acfSSebastian Siewior 	unregister_netdev(ndev);
3302df828598SMugunthan V N 
33038a2c9a5aSGrygorii Strashko 	cpts_release(cpsw->cpts);
33042c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
3305a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
33068a0b6dc9SGrygorii Strashko 	pm_runtime_put_sync(&pdev->dev);
33078a0b6dc9SGrygorii Strashko 	pm_runtime_disable(&pdev->dev);
3308606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
3309606f3993SIvan Khoronzhuk 		free_netdev(cpsw->slaves[1].ndev);
3310df828598SMugunthan V N 	free_netdev(ndev);
3311df828598SMugunthan V N 	return 0;
3312df828598SMugunthan V N }
3313df828598SMugunthan V N 
33148963a504SGrygorii Strashko #ifdef CONFIG_PM_SLEEP
3315df828598SMugunthan V N static int cpsw_suspend(struct device *dev)
3316df828598SMugunthan V N {
3317df828598SMugunthan V N 	struct platform_device	*pdev = to_platform_device(dev);
3318df828598SMugunthan V N 	struct net_device	*ndev = platform_get_drvdata(pdev);
3319606f3993SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3320df828598SMugunthan V N 
3321606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
3322618073e3SMugunthan V N 		int i;
3323618073e3SMugunthan V N 
3324606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
3325606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
3326606f3993SIvan Khoronzhuk 				cpsw_ndo_stop(cpsw->slaves[i].ndev);
3327618073e3SMugunthan V N 		}
3328618073e3SMugunthan V N 	} else {
3329df828598SMugunthan V N 		if (netif_running(ndev))
3330df828598SMugunthan V N 			cpsw_ndo_stop(ndev);
3331618073e3SMugunthan V N 	}
33321e7a2e21SDaniel Mack 
3333739683b4SMugunthan V N 	/* Select sleep pin state */
333456e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_sleep_state(dev);
3335739683b4SMugunthan V N 
3336df828598SMugunthan V N 	return 0;
3337df828598SMugunthan V N }
3338df828598SMugunthan V N 
3339df828598SMugunthan V N static int cpsw_resume(struct device *dev)
3340df828598SMugunthan V N {
3341df828598SMugunthan V N 	struct platform_device	*pdev = to_platform_device(dev);
3342df828598SMugunthan V N 	struct net_device	*ndev = platform_get_drvdata(pdev);
3343a60ced99SIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(ndev);
3344df828598SMugunthan V N 
3345739683b4SMugunthan V N 	/* Select default pin state */
334656e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_default_state(dev);
3347739683b4SMugunthan V N 
33484ccfd638SGrygorii Strashko 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
33494ccfd638SGrygorii Strashko 	rtnl_lock();
3350606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
3351618073e3SMugunthan V N 		int i;
3352618073e3SMugunthan V N 
3353606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
3354606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
3355606f3993SIvan Khoronzhuk 				cpsw_ndo_open(cpsw->slaves[i].ndev);
3356618073e3SMugunthan V N 		}
3357618073e3SMugunthan V N 	} else {
3358df828598SMugunthan V N 		if (netif_running(ndev))
3359df828598SMugunthan V N 			cpsw_ndo_open(ndev);
3360618073e3SMugunthan V N 	}
33614ccfd638SGrygorii Strashko 	rtnl_unlock();
33624ccfd638SGrygorii Strashko 
3363df828598SMugunthan V N 	return 0;
3364df828598SMugunthan V N }
33658963a504SGrygorii Strashko #endif
3366df828598SMugunthan V N 
33678963a504SGrygorii Strashko static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
3368df828598SMugunthan V N 
3369df828598SMugunthan V N static struct platform_driver cpsw_driver = {
3370df828598SMugunthan V N 	.driver = {
3371df828598SMugunthan V N 		.name	 = "cpsw",
3372df828598SMugunthan V N 		.pm	 = &cpsw_pm_ops,
33731e5c76d4SSachin Kamat 		.of_match_table = cpsw_of_mtable,
3374df828598SMugunthan V N 	},
3375df828598SMugunthan V N 	.probe = cpsw_probe,
3376663e12e6SBill Pemberton 	.remove = cpsw_remove,
3377df828598SMugunthan V N };
3378df828598SMugunthan V N 
33796fb3b6b5SGrygorii Strashko module_platform_driver(cpsw_driver);
3380df828598SMugunthan V N 
3381df828598SMugunthan V N MODULE_LICENSE("GPL");
3382df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3383df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3384df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver");
3385